xref: /linux/drivers/crypto/talitos.c (revision 79fd31d355f7e315025a330adc2183fb5ae8f22d)
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 
5681eb024cSKim Phillips static void to_talitos_ptr(struct talitos_ptr *talitos_ptr, dma_addr_t dma_addr)
5781eb024cSKim Phillips {
5881eb024cSKim Phillips 	talitos_ptr->ptr = cpu_to_be32(lower_32_bits(dma_addr));
59a752447aSKim Phillips 	talitos_ptr->eptr = upper_32_bits(dma_addr);
6081eb024cSKim Phillips }
6181eb024cSKim Phillips 
629c4a7965SKim Phillips /*
639c4a7965SKim Phillips  * map virtual single (contiguous) pointer to h/w descriptor pointer
649c4a7965SKim Phillips  */
659c4a7965SKim Phillips static void map_single_talitos_ptr(struct device *dev,
669c4a7965SKim Phillips 				   struct talitos_ptr *talitos_ptr,
679c4a7965SKim Phillips 				   unsigned short len, void *data,
689c4a7965SKim Phillips 				   unsigned char extent,
699c4a7965SKim Phillips 				   enum dma_data_direction dir)
709c4a7965SKim Phillips {
7181eb024cSKim Phillips 	dma_addr_t dma_addr = dma_map_single(dev, data, len, dir);
7281eb024cSKim Phillips 
739c4a7965SKim Phillips 	talitos_ptr->len = cpu_to_be16(len);
7481eb024cSKim Phillips 	to_talitos_ptr(talitos_ptr, dma_addr);
759c4a7965SKim Phillips 	talitos_ptr->j_extent = extent;
769c4a7965SKim Phillips }
779c4a7965SKim Phillips 
789c4a7965SKim Phillips /*
799c4a7965SKim Phillips  * unmap bus single (contiguous) h/w descriptor pointer
809c4a7965SKim Phillips  */
819c4a7965SKim Phillips static void unmap_single_talitos_ptr(struct device *dev,
829c4a7965SKim Phillips 				     struct talitos_ptr *talitos_ptr,
839c4a7965SKim Phillips 				     enum dma_data_direction dir)
849c4a7965SKim Phillips {
859c4a7965SKim Phillips 	dma_unmap_single(dev, be32_to_cpu(talitos_ptr->ptr),
869c4a7965SKim Phillips 			 be16_to_cpu(talitos_ptr->len), dir);
879c4a7965SKim Phillips }
889c4a7965SKim Phillips 
899c4a7965SKim Phillips static int reset_channel(struct device *dev, int ch)
909c4a7965SKim Phillips {
919c4a7965SKim Phillips 	struct talitos_private *priv = dev_get_drvdata(dev);
929c4a7965SKim Phillips 	unsigned int timeout = TALITOS_TIMEOUT;
939c4a7965SKim Phillips 
94ad42d5fcSKim Phillips 	setbits32(priv->chan[ch].reg + TALITOS_CCCR, TALITOS_CCCR_RESET);
959c4a7965SKim Phillips 
96ad42d5fcSKim Phillips 	while ((in_be32(priv->chan[ch].reg + TALITOS_CCCR) & TALITOS_CCCR_RESET)
979c4a7965SKim Phillips 	       && --timeout)
989c4a7965SKim Phillips 		cpu_relax();
999c4a7965SKim Phillips 
1009c4a7965SKim Phillips 	if (timeout == 0) {
1019c4a7965SKim Phillips 		dev_err(dev, "failed to reset channel %d\n", ch);
1029c4a7965SKim Phillips 		return -EIO;
1039c4a7965SKim Phillips 	}
1049c4a7965SKim Phillips 
10581eb024cSKim Phillips 	/* set 36-bit addressing, done writeback enable and done IRQ enable */
106ad42d5fcSKim Phillips 	setbits32(priv->chan[ch].reg + TALITOS_CCCR_LO, TALITOS_CCCR_LO_EAE |
10781eb024cSKim Phillips 		  TALITOS_CCCR_LO_CDWE | TALITOS_CCCR_LO_CDIE);
1089c4a7965SKim Phillips 
109fe5720e2SKim Phillips 	/* and ICCR writeback, if available */
110fe5720e2SKim Phillips 	if (priv->features & TALITOS_FTR_HW_AUTH_CHECK)
111ad42d5fcSKim Phillips 		setbits32(priv->chan[ch].reg + TALITOS_CCCR_LO,
112fe5720e2SKim Phillips 		          TALITOS_CCCR_LO_IWSE);
113fe5720e2SKim Phillips 
1149c4a7965SKim Phillips 	return 0;
1159c4a7965SKim Phillips }
1169c4a7965SKim Phillips 
1179c4a7965SKim Phillips static int reset_device(struct device *dev)
1189c4a7965SKim Phillips {
1199c4a7965SKim Phillips 	struct talitos_private *priv = dev_get_drvdata(dev);
1209c4a7965SKim Phillips 	unsigned int timeout = TALITOS_TIMEOUT;
121c3e337f8SKim Phillips 	u32 mcr = TALITOS_MCR_SWR;
1229c4a7965SKim Phillips 
123c3e337f8SKim Phillips 	setbits32(priv->reg + TALITOS_MCR, mcr);
1249c4a7965SKim Phillips 
1259c4a7965SKim Phillips 	while ((in_be32(priv->reg + TALITOS_MCR) & TALITOS_MCR_SWR)
1269c4a7965SKim Phillips 	       && --timeout)
1279c4a7965SKim Phillips 		cpu_relax();
1289c4a7965SKim Phillips 
1292cdba3cfSKim Phillips 	if (priv->irq[1]) {
130c3e337f8SKim Phillips 		mcr = TALITOS_MCR_RCA1 | TALITOS_MCR_RCA3;
131c3e337f8SKim Phillips 		setbits32(priv->reg + TALITOS_MCR, mcr);
132c3e337f8SKim Phillips 	}
133c3e337f8SKim Phillips 
1349c4a7965SKim Phillips 	if (timeout == 0) {
1359c4a7965SKim Phillips 		dev_err(dev, "failed to reset device\n");
1369c4a7965SKim Phillips 		return -EIO;
1379c4a7965SKim Phillips 	}
1389c4a7965SKim Phillips 
1399c4a7965SKim Phillips 	return 0;
1409c4a7965SKim Phillips }
1419c4a7965SKim Phillips 
1429c4a7965SKim Phillips /*
1439c4a7965SKim Phillips  * Reset and initialize the device
1449c4a7965SKim Phillips  */
1459c4a7965SKim Phillips static int init_device(struct device *dev)
1469c4a7965SKim Phillips {
1479c4a7965SKim Phillips 	struct talitos_private *priv = dev_get_drvdata(dev);
1489c4a7965SKim Phillips 	int ch, err;
1499c4a7965SKim Phillips 
1509c4a7965SKim Phillips 	/*
1519c4a7965SKim Phillips 	 * Master reset
1529c4a7965SKim Phillips 	 * errata documentation: warning: certain SEC interrupts
1539c4a7965SKim Phillips 	 * are not fully cleared by writing the MCR:SWR bit,
1549c4a7965SKim Phillips 	 * set bit twice to completely reset
1559c4a7965SKim Phillips 	 */
1569c4a7965SKim Phillips 	err = reset_device(dev);
1579c4a7965SKim Phillips 	if (err)
1589c4a7965SKim Phillips 		return err;
1599c4a7965SKim Phillips 
1609c4a7965SKim Phillips 	err = reset_device(dev);
1619c4a7965SKim Phillips 	if (err)
1629c4a7965SKim Phillips 		return err;
1639c4a7965SKim Phillips 
1649c4a7965SKim Phillips 	/* reset channels */
1659c4a7965SKim Phillips 	for (ch = 0; ch < priv->num_channels; ch++) {
1669c4a7965SKim Phillips 		err = reset_channel(dev, ch);
1679c4a7965SKim Phillips 		if (err)
1689c4a7965SKim Phillips 			return err;
1699c4a7965SKim Phillips 	}
1709c4a7965SKim Phillips 
1719c4a7965SKim Phillips 	/* enable channel done and error interrupts */
1729c4a7965SKim Phillips 	setbits32(priv->reg + TALITOS_IMR, TALITOS_IMR_INIT);
1739c4a7965SKim Phillips 	setbits32(priv->reg + TALITOS_IMR_LO, TALITOS_IMR_LO_INIT);
1749c4a7965SKim Phillips 
175fe5720e2SKim Phillips 	/* disable integrity check error interrupts (use writeback instead) */
176fe5720e2SKim Phillips 	if (priv->features & TALITOS_FTR_HW_AUTH_CHECK)
177fe5720e2SKim Phillips 		setbits32(priv->reg + TALITOS_MDEUICR_LO,
178fe5720e2SKim Phillips 		          TALITOS_MDEUICR_LO_ICE);
179fe5720e2SKim Phillips 
1809c4a7965SKim Phillips 	return 0;
1819c4a7965SKim Phillips }
1829c4a7965SKim Phillips 
1839c4a7965SKim Phillips /**
1849c4a7965SKim Phillips  * talitos_submit - submits a descriptor to the device for processing
1859c4a7965SKim Phillips  * @dev:	the SEC device to be used
1865228f0f7SKim Phillips  * @ch:		the SEC device channel to be used
1879c4a7965SKim Phillips  * @desc:	the descriptor to be processed by the device
1889c4a7965SKim Phillips  * @callback:	whom to call when processing is complete
1899c4a7965SKim Phillips  * @context:	a handle for use by caller (optional)
1909c4a7965SKim Phillips  *
1919c4a7965SKim Phillips  * desc must contain valid dma-mapped (bus physical) address pointers.
1929c4a7965SKim Phillips  * callback must check err and feedback in descriptor header
1939c4a7965SKim Phillips  * for device processing status.
1949c4a7965SKim Phillips  */
195865d5061SHoria Geanta int talitos_submit(struct device *dev, int ch, struct talitos_desc *desc,
1969c4a7965SKim Phillips 		   void (*callback)(struct device *dev,
1979c4a7965SKim Phillips 				    struct talitos_desc *desc,
1989c4a7965SKim Phillips 				    void *context, int error),
1999c4a7965SKim Phillips 		   void *context)
2009c4a7965SKim Phillips {
2019c4a7965SKim Phillips 	struct talitos_private *priv = dev_get_drvdata(dev);
2029c4a7965SKim Phillips 	struct talitos_request *request;
2035228f0f7SKim Phillips 	unsigned long flags;
2049c4a7965SKim Phillips 	int head;
2059c4a7965SKim Phillips 
2064b992628SKim Phillips 	spin_lock_irqsave(&priv->chan[ch].head_lock, flags);
2079c4a7965SKim Phillips 
2084b992628SKim Phillips 	if (!atomic_inc_not_zero(&priv->chan[ch].submit_count)) {
209ec6644d6SKim Phillips 		/* h/w fifo is full */
2104b992628SKim Phillips 		spin_unlock_irqrestore(&priv->chan[ch].head_lock, flags);
2119c4a7965SKim Phillips 		return -EAGAIN;
2129c4a7965SKim Phillips 	}
2139c4a7965SKim Phillips 
2144b992628SKim Phillips 	head = priv->chan[ch].head;
2154b992628SKim Phillips 	request = &priv->chan[ch].fifo[head];
216ec6644d6SKim Phillips 
2179c4a7965SKim Phillips 	/* map descriptor and save caller data */
2189c4a7965SKim Phillips 	request->dma_desc = dma_map_single(dev, desc, sizeof(*desc),
2199c4a7965SKim Phillips 					   DMA_BIDIRECTIONAL);
2209c4a7965SKim Phillips 	request->callback = callback;
2219c4a7965SKim Phillips 	request->context = context;
2229c4a7965SKim Phillips 
2239c4a7965SKim Phillips 	/* increment fifo head */
2244b992628SKim Phillips 	priv->chan[ch].head = (priv->chan[ch].head + 1) & (priv->fifo_len - 1);
2259c4a7965SKim Phillips 
2269c4a7965SKim Phillips 	smp_wmb();
2279c4a7965SKim Phillips 	request->desc = desc;
2289c4a7965SKim Phillips 
2299c4a7965SKim Phillips 	/* GO! */
2309c4a7965SKim Phillips 	wmb();
231ad42d5fcSKim Phillips 	out_be32(priv->chan[ch].reg + TALITOS_FF,
232ad42d5fcSKim Phillips 		 upper_32_bits(request->dma_desc));
233ad42d5fcSKim Phillips 	out_be32(priv->chan[ch].reg + TALITOS_FF_LO,
234a752447aSKim Phillips 		 lower_32_bits(request->dma_desc));
2359c4a7965SKim Phillips 
2364b992628SKim Phillips 	spin_unlock_irqrestore(&priv->chan[ch].head_lock, flags);
2379c4a7965SKim Phillips 
2389c4a7965SKim Phillips 	return -EINPROGRESS;
2399c4a7965SKim Phillips }
240865d5061SHoria Geanta EXPORT_SYMBOL(talitos_submit);
2419c4a7965SKim Phillips 
2429c4a7965SKim Phillips /*
2439c4a7965SKim Phillips  * process what was done, notify callback of error if not
2449c4a7965SKim Phillips  */
2459c4a7965SKim Phillips static void flush_channel(struct device *dev, int ch, int error, int reset_ch)
2469c4a7965SKim Phillips {
2479c4a7965SKim Phillips 	struct talitos_private *priv = dev_get_drvdata(dev);
2489c4a7965SKim Phillips 	struct talitos_request *request, saved_req;
2499c4a7965SKim Phillips 	unsigned long flags;
2509c4a7965SKim Phillips 	int tail, status;
2519c4a7965SKim Phillips 
2524b992628SKim Phillips 	spin_lock_irqsave(&priv->chan[ch].tail_lock, flags);
2539c4a7965SKim Phillips 
2544b992628SKim Phillips 	tail = priv->chan[ch].tail;
2554b992628SKim Phillips 	while (priv->chan[ch].fifo[tail].desc) {
2564b992628SKim Phillips 		request = &priv->chan[ch].fifo[tail];
2579c4a7965SKim Phillips 
2589c4a7965SKim Phillips 		/* descriptors with their done bits set don't get the error */
2599c4a7965SKim Phillips 		rmb();
260ca38a814SLee Nipper 		if ((request->desc->hdr & DESC_HDR_DONE) == DESC_HDR_DONE)
2619c4a7965SKim Phillips 			status = 0;
262ca38a814SLee Nipper 		else
2639c4a7965SKim Phillips 			if (!error)
2649c4a7965SKim Phillips 				break;
2659c4a7965SKim Phillips 			else
2669c4a7965SKim Phillips 				status = error;
2679c4a7965SKim Phillips 
2689c4a7965SKim Phillips 		dma_unmap_single(dev, request->dma_desc,
269e938e465SKim Phillips 				 sizeof(struct talitos_desc),
270e938e465SKim Phillips 				 DMA_BIDIRECTIONAL);
2719c4a7965SKim Phillips 
2729c4a7965SKim Phillips 		/* copy entries so we can call callback outside lock */
2739c4a7965SKim Phillips 		saved_req.desc = request->desc;
2749c4a7965SKim Phillips 		saved_req.callback = request->callback;
2759c4a7965SKim Phillips 		saved_req.context = request->context;
2769c4a7965SKim Phillips 
2779c4a7965SKim Phillips 		/* release request entry in fifo */
2789c4a7965SKim Phillips 		smp_wmb();
2799c4a7965SKim Phillips 		request->desc = NULL;
2809c4a7965SKim Phillips 
2819c4a7965SKim Phillips 		/* increment fifo tail */
2824b992628SKim Phillips 		priv->chan[ch].tail = (tail + 1) & (priv->fifo_len - 1);
2839c4a7965SKim Phillips 
2844b992628SKim Phillips 		spin_unlock_irqrestore(&priv->chan[ch].tail_lock, flags);
285ec6644d6SKim Phillips 
2864b992628SKim Phillips 		atomic_dec(&priv->chan[ch].submit_count);
287ec6644d6SKim Phillips 
2889c4a7965SKim Phillips 		saved_req.callback(dev, saved_req.desc, saved_req.context,
2899c4a7965SKim Phillips 				   status);
2909c4a7965SKim Phillips 		/* channel may resume processing in single desc error case */
2919c4a7965SKim Phillips 		if (error && !reset_ch && status == error)
2929c4a7965SKim Phillips 			return;
2934b992628SKim Phillips 		spin_lock_irqsave(&priv->chan[ch].tail_lock, flags);
2944b992628SKim Phillips 		tail = priv->chan[ch].tail;
2959c4a7965SKim Phillips 	}
2969c4a7965SKim Phillips 
2974b992628SKim Phillips 	spin_unlock_irqrestore(&priv->chan[ch].tail_lock, flags);
2989c4a7965SKim Phillips }
2999c4a7965SKim Phillips 
3009c4a7965SKim Phillips /*
3019c4a7965SKim Phillips  * process completed requests for channels that have done status
3029c4a7965SKim Phillips  */
303c3e337f8SKim Phillips #define DEF_TALITOS_DONE(name, ch_done_mask)				\
304c3e337f8SKim Phillips static void talitos_done_##name(unsigned long data)			\
305c3e337f8SKim Phillips {									\
306c3e337f8SKim Phillips 	struct device *dev = (struct device *)data;			\
307c3e337f8SKim Phillips 	struct talitos_private *priv = dev_get_drvdata(dev);		\
308511d63cbSHoria Geanta 	unsigned long flags;						\
309c3e337f8SKim Phillips 									\
310c3e337f8SKim Phillips 	if (ch_done_mask & 1)						\
311c3e337f8SKim Phillips 		flush_channel(dev, 0, 0, 0);				\
312c3e337f8SKim Phillips 	if (priv->num_channels == 1)					\
313c3e337f8SKim Phillips 		goto out;						\
314c3e337f8SKim Phillips 	if (ch_done_mask & (1 << 2))					\
315c3e337f8SKim Phillips 		flush_channel(dev, 1, 0, 0);				\
316c3e337f8SKim Phillips 	if (ch_done_mask & (1 << 4))					\
317c3e337f8SKim Phillips 		flush_channel(dev, 2, 0, 0);				\
318c3e337f8SKim Phillips 	if (ch_done_mask & (1 << 6))					\
319c3e337f8SKim Phillips 		flush_channel(dev, 3, 0, 0);				\
320c3e337f8SKim Phillips 									\
321c3e337f8SKim Phillips out:									\
322c3e337f8SKim Phillips 	/* At this point, all completed channels have been processed */	\
323c3e337f8SKim Phillips 	/* Unmask done interrupts for channels completed later on. */	\
324511d63cbSHoria Geanta 	spin_lock_irqsave(&priv->reg_lock, flags);			\
325c3e337f8SKim Phillips 	setbits32(priv->reg + TALITOS_IMR, ch_done_mask);		\
326c3e337f8SKim Phillips 	setbits32(priv->reg + TALITOS_IMR_LO, TALITOS_IMR_LO_INIT);	\
327511d63cbSHoria Geanta 	spin_unlock_irqrestore(&priv->reg_lock, flags);			\
3289c4a7965SKim Phillips }
329c3e337f8SKim Phillips DEF_TALITOS_DONE(4ch, TALITOS_ISR_4CHDONE)
330c3e337f8SKim Phillips DEF_TALITOS_DONE(ch0_2, TALITOS_ISR_CH_0_2_DONE)
331c3e337f8SKim Phillips DEF_TALITOS_DONE(ch1_3, TALITOS_ISR_CH_1_3_DONE)
3329c4a7965SKim Phillips 
3339c4a7965SKim Phillips /*
3349c4a7965SKim Phillips  * locate current (offending) descriptor
3359c4a7965SKim Phillips  */
3363e721aebSKim Phillips static u32 current_desc_hdr(struct device *dev, int ch)
3379c4a7965SKim Phillips {
3389c4a7965SKim Phillips 	struct talitos_private *priv = dev_get_drvdata(dev);
3394b992628SKim Phillips 	int tail = priv->chan[ch].tail;
3409c4a7965SKim Phillips 	dma_addr_t cur_desc;
3419c4a7965SKim Phillips 
342ad42d5fcSKim Phillips 	cur_desc = in_be32(priv->chan[ch].reg + TALITOS_CDPR_LO);
3439c4a7965SKim Phillips 
3444b992628SKim Phillips 	while (priv->chan[ch].fifo[tail].dma_desc != cur_desc) {
3459c4a7965SKim Phillips 		tail = (tail + 1) & (priv->fifo_len - 1);
3464b992628SKim Phillips 		if (tail == priv->chan[ch].tail) {
3479c4a7965SKim Phillips 			dev_err(dev, "couldn't locate current descriptor\n");
3483e721aebSKim Phillips 			return 0;
3499c4a7965SKim Phillips 		}
3509c4a7965SKim Phillips 	}
3519c4a7965SKim Phillips 
3523e721aebSKim Phillips 	return priv->chan[ch].fifo[tail].desc->hdr;
3539c4a7965SKim Phillips }
3549c4a7965SKim Phillips 
3559c4a7965SKim Phillips /*
3569c4a7965SKim Phillips  * user diagnostics; report root cause of error based on execution unit status
3579c4a7965SKim Phillips  */
3583e721aebSKim Phillips static void report_eu_error(struct device *dev, int ch, u32 desc_hdr)
3599c4a7965SKim Phillips {
3609c4a7965SKim Phillips 	struct talitos_private *priv = dev_get_drvdata(dev);
3619c4a7965SKim Phillips 	int i;
3629c4a7965SKim Phillips 
3633e721aebSKim Phillips 	if (!desc_hdr)
364ad42d5fcSKim Phillips 		desc_hdr = in_be32(priv->chan[ch].reg + TALITOS_DESCBUF);
3653e721aebSKim Phillips 
3663e721aebSKim Phillips 	switch (desc_hdr & DESC_HDR_SEL0_MASK) {
3679c4a7965SKim Phillips 	case DESC_HDR_SEL0_AFEU:
3689c4a7965SKim Phillips 		dev_err(dev, "AFEUISR 0x%08x_%08x\n",
3699c4a7965SKim Phillips 			in_be32(priv->reg + TALITOS_AFEUISR),
3709c4a7965SKim Phillips 			in_be32(priv->reg + TALITOS_AFEUISR_LO));
3719c4a7965SKim Phillips 		break;
3729c4a7965SKim Phillips 	case DESC_HDR_SEL0_DEU:
3739c4a7965SKim Phillips 		dev_err(dev, "DEUISR 0x%08x_%08x\n",
3749c4a7965SKim Phillips 			in_be32(priv->reg + TALITOS_DEUISR),
3759c4a7965SKim Phillips 			in_be32(priv->reg + TALITOS_DEUISR_LO));
3769c4a7965SKim Phillips 		break;
3779c4a7965SKim Phillips 	case DESC_HDR_SEL0_MDEUA:
3789c4a7965SKim Phillips 	case DESC_HDR_SEL0_MDEUB:
3799c4a7965SKim Phillips 		dev_err(dev, "MDEUISR 0x%08x_%08x\n",
3809c4a7965SKim Phillips 			in_be32(priv->reg + TALITOS_MDEUISR),
3819c4a7965SKim Phillips 			in_be32(priv->reg + TALITOS_MDEUISR_LO));
3829c4a7965SKim Phillips 		break;
3839c4a7965SKim Phillips 	case DESC_HDR_SEL0_RNG:
3849c4a7965SKim Phillips 		dev_err(dev, "RNGUISR 0x%08x_%08x\n",
3859c4a7965SKim Phillips 			in_be32(priv->reg + TALITOS_RNGUISR),
3869c4a7965SKim Phillips 			in_be32(priv->reg + TALITOS_RNGUISR_LO));
3879c4a7965SKim Phillips 		break;
3889c4a7965SKim Phillips 	case DESC_HDR_SEL0_PKEU:
3899c4a7965SKim Phillips 		dev_err(dev, "PKEUISR 0x%08x_%08x\n",
3909c4a7965SKim Phillips 			in_be32(priv->reg + TALITOS_PKEUISR),
3919c4a7965SKim Phillips 			in_be32(priv->reg + TALITOS_PKEUISR_LO));
3929c4a7965SKim Phillips 		break;
3939c4a7965SKim Phillips 	case DESC_HDR_SEL0_AESU:
3949c4a7965SKim Phillips 		dev_err(dev, "AESUISR 0x%08x_%08x\n",
3959c4a7965SKim Phillips 			in_be32(priv->reg + TALITOS_AESUISR),
3969c4a7965SKim Phillips 			in_be32(priv->reg + TALITOS_AESUISR_LO));
3979c4a7965SKim Phillips 		break;
3989c4a7965SKim Phillips 	case DESC_HDR_SEL0_CRCU:
3999c4a7965SKim Phillips 		dev_err(dev, "CRCUISR 0x%08x_%08x\n",
4009c4a7965SKim Phillips 			in_be32(priv->reg + TALITOS_CRCUISR),
4019c4a7965SKim Phillips 			in_be32(priv->reg + TALITOS_CRCUISR_LO));
4029c4a7965SKim Phillips 		break;
4039c4a7965SKim Phillips 	case DESC_HDR_SEL0_KEU:
4049c4a7965SKim Phillips 		dev_err(dev, "KEUISR 0x%08x_%08x\n",
4059c4a7965SKim Phillips 			in_be32(priv->reg + TALITOS_KEUISR),
4069c4a7965SKim Phillips 			in_be32(priv->reg + TALITOS_KEUISR_LO));
4079c4a7965SKim Phillips 		break;
4089c4a7965SKim Phillips 	}
4099c4a7965SKim Phillips 
4103e721aebSKim Phillips 	switch (desc_hdr & DESC_HDR_SEL1_MASK) {
4119c4a7965SKim Phillips 	case DESC_HDR_SEL1_MDEUA:
4129c4a7965SKim Phillips 	case DESC_HDR_SEL1_MDEUB:
4139c4a7965SKim Phillips 		dev_err(dev, "MDEUISR 0x%08x_%08x\n",
4149c4a7965SKim Phillips 			in_be32(priv->reg + TALITOS_MDEUISR),
4159c4a7965SKim Phillips 			in_be32(priv->reg + TALITOS_MDEUISR_LO));
4169c4a7965SKim Phillips 		break;
4179c4a7965SKim Phillips 	case DESC_HDR_SEL1_CRCU:
4189c4a7965SKim Phillips 		dev_err(dev, "CRCUISR 0x%08x_%08x\n",
4199c4a7965SKim Phillips 			in_be32(priv->reg + TALITOS_CRCUISR),
4209c4a7965SKim Phillips 			in_be32(priv->reg + TALITOS_CRCUISR_LO));
4219c4a7965SKim Phillips 		break;
4229c4a7965SKim Phillips 	}
4239c4a7965SKim Phillips 
4249c4a7965SKim Phillips 	for (i = 0; i < 8; i++)
4259c4a7965SKim Phillips 		dev_err(dev, "DESCBUF 0x%08x_%08x\n",
426ad42d5fcSKim Phillips 			in_be32(priv->chan[ch].reg + TALITOS_DESCBUF + 8*i),
427ad42d5fcSKim Phillips 			in_be32(priv->chan[ch].reg + TALITOS_DESCBUF_LO + 8*i));
4289c4a7965SKim Phillips }
4299c4a7965SKim Phillips 
4309c4a7965SKim Phillips /*
4319c4a7965SKim Phillips  * recover from error interrupts
4329c4a7965SKim Phillips  */
4335e718a09SKim Phillips static void talitos_error(struct device *dev, u32 isr, u32 isr_lo)
4349c4a7965SKim Phillips {
4359c4a7965SKim Phillips 	struct talitos_private *priv = dev_get_drvdata(dev);
4369c4a7965SKim Phillips 	unsigned int timeout = TALITOS_TIMEOUT;
4379c4a7965SKim Phillips 	int ch, error, reset_dev = 0, reset_ch = 0;
43840405f10SKim Phillips 	u32 v, v_lo;
4399c4a7965SKim Phillips 
4409c4a7965SKim Phillips 	for (ch = 0; ch < priv->num_channels; ch++) {
4419c4a7965SKim Phillips 		/* skip channels without errors */
4429c4a7965SKim Phillips 		if (!(isr & (1 << (ch * 2 + 1))))
4439c4a7965SKim Phillips 			continue;
4449c4a7965SKim Phillips 
4459c4a7965SKim Phillips 		error = -EINVAL;
4469c4a7965SKim Phillips 
447ad42d5fcSKim Phillips 		v = in_be32(priv->chan[ch].reg + TALITOS_CCPSR);
448ad42d5fcSKim Phillips 		v_lo = in_be32(priv->chan[ch].reg + TALITOS_CCPSR_LO);
4499c4a7965SKim Phillips 
4509c4a7965SKim Phillips 		if (v_lo & TALITOS_CCPSR_LO_DOF) {
4519c4a7965SKim Phillips 			dev_err(dev, "double fetch fifo overflow error\n");
4529c4a7965SKim Phillips 			error = -EAGAIN;
4539c4a7965SKim Phillips 			reset_ch = 1;
4549c4a7965SKim Phillips 		}
4559c4a7965SKim Phillips 		if (v_lo & TALITOS_CCPSR_LO_SOF) {
4569c4a7965SKim Phillips 			/* h/w dropped descriptor */
4579c4a7965SKim Phillips 			dev_err(dev, "single fetch fifo overflow error\n");
4589c4a7965SKim Phillips 			error = -EAGAIN;
4599c4a7965SKim Phillips 		}
4609c4a7965SKim Phillips 		if (v_lo & TALITOS_CCPSR_LO_MDTE)
4619c4a7965SKim Phillips 			dev_err(dev, "master data transfer error\n");
4629c4a7965SKim Phillips 		if (v_lo & TALITOS_CCPSR_LO_SGDLZ)
4639c4a7965SKim Phillips 			dev_err(dev, "s/g data length zero error\n");
4649c4a7965SKim Phillips 		if (v_lo & TALITOS_CCPSR_LO_FPZ)
4659c4a7965SKim Phillips 			dev_err(dev, "fetch pointer zero error\n");
4669c4a7965SKim Phillips 		if (v_lo & TALITOS_CCPSR_LO_IDH)
4679c4a7965SKim Phillips 			dev_err(dev, "illegal descriptor header error\n");
4689c4a7965SKim Phillips 		if (v_lo & TALITOS_CCPSR_LO_IEU)
4699c4a7965SKim Phillips 			dev_err(dev, "invalid execution unit error\n");
4709c4a7965SKim Phillips 		if (v_lo & TALITOS_CCPSR_LO_EU)
4713e721aebSKim Phillips 			report_eu_error(dev, ch, current_desc_hdr(dev, ch));
4729c4a7965SKim Phillips 		if (v_lo & TALITOS_CCPSR_LO_GB)
4739c4a7965SKim Phillips 			dev_err(dev, "gather boundary error\n");
4749c4a7965SKim Phillips 		if (v_lo & TALITOS_CCPSR_LO_GRL)
4759c4a7965SKim Phillips 			dev_err(dev, "gather return/length error\n");
4769c4a7965SKim Phillips 		if (v_lo & TALITOS_CCPSR_LO_SB)
4779c4a7965SKim Phillips 			dev_err(dev, "scatter boundary error\n");
4789c4a7965SKim Phillips 		if (v_lo & TALITOS_CCPSR_LO_SRL)
4799c4a7965SKim Phillips 			dev_err(dev, "scatter return/length error\n");
4809c4a7965SKim Phillips 
4819c4a7965SKim Phillips 		flush_channel(dev, ch, error, reset_ch);
4829c4a7965SKim Phillips 
4839c4a7965SKim Phillips 		if (reset_ch) {
4849c4a7965SKim Phillips 			reset_channel(dev, ch);
4859c4a7965SKim Phillips 		} else {
486ad42d5fcSKim Phillips 			setbits32(priv->chan[ch].reg + TALITOS_CCCR,
4879c4a7965SKim Phillips 				  TALITOS_CCCR_CONT);
488ad42d5fcSKim Phillips 			setbits32(priv->chan[ch].reg + TALITOS_CCCR_LO, 0);
489ad42d5fcSKim Phillips 			while ((in_be32(priv->chan[ch].reg + TALITOS_CCCR) &
4909c4a7965SKim Phillips 			       TALITOS_CCCR_CONT) && --timeout)
4919c4a7965SKim Phillips 				cpu_relax();
4929c4a7965SKim Phillips 			if (timeout == 0) {
4939c4a7965SKim Phillips 				dev_err(dev, "failed to restart channel %d\n",
4949c4a7965SKim Phillips 					ch);
4959c4a7965SKim Phillips 				reset_dev = 1;
4969c4a7965SKim Phillips 			}
4979c4a7965SKim Phillips 		}
4989c4a7965SKim Phillips 	}
499c3e337f8SKim Phillips 	if (reset_dev || isr & ~TALITOS_ISR_4CHERR || isr_lo) {
5009c4a7965SKim Phillips 		dev_err(dev, "done overflow, internal time out, or rngu error: "
5019c4a7965SKim Phillips 		        "ISR 0x%08x_%08x\n", isr, isr_lo);
5029c4a7965SKim Phillips 
5039c4a7965SKim Phillips 		/* purge request queues */
5049c4a7965SKim Phillips 		for (ch = 0; ch < priv->num_channels; ch++)
5059c4a7965SKim Phillips 			flush_channel(dev, ch, -EIO, 1);
5069c4a7965SKim Phillips 
5079c4a7965SKim Phillips 		/* reset and reinitialize the device */
5089c4a7965SKim Phillips 		init_device(dev);
5099c4a7965SKim Phillips 	}
5109c4a7965SKim Phillips }
5119c4a7965SKim Phillips 
512c3e337f8SKim Phillips #define DEF_TALITOS_INTERRUPT(name, ch_done_mask, ch_err_mask, tlet)	       \
513c3e337f8SKim Phillips static irqreturn_t talitos_interrupt_##name(int irq, void *data)	       \
514c3e337f8SKim Phillips {									       \
515c3e337f8SKim Phillips 	struct device *dev = data;					       \
516c3e337f8SKim Phillips 	struct talitos_private *priv = dev_get_drvdata(dev);		       \
517c3e337f8SKim Phillips 	u32 isr, isr_lo;						       \
518511d63cbSHoria Geanta 	unsigned long flags;						       \
519c3e337f8SKim Phillips 									       \
520511d63cbSHoria Geanta 	spin_lock_irqsave(&priv->reg_lock, flags);			       \
521c3e337f8SKim Phillips 	isr = in_be32(priv->reg + TALITOS_ISR);				       \
522c3e337f8SKim Phillips 	isr_lo = in_be32(priv->reg + TALITOS_ISR_LO);			       \
523c3e337f8SKim Phillips 	/* Acknowledge interrupt */					       \
524c3e337f8SKim Phillips 	out_be32(priv->reg + TALITOS_ICR, isr & (ch_done_mask | ch_err_mask)); \
525c3e337f8SKim Phillips 	out_be32(priv->reg + TALITOS_ICR_LO, isr_lo);			       \
526c3e337f8SKim Phillips 									       \
527511d63cbSHoria Geanta 	if (unlikely(isr & ch_err_mask || isr_lo)) {			       \
528511d63cbSHoria Geanta 		spin_unlock_irqrestore(&priv->reg_lock, flags);		       \
529511d63cbSHoria Geanta 		talitos_error(dev, isr & ch_err_mask, isr_lo);		       \
530511d63cbSHoria Geanta 	}								       \
531511d63cbSHoria Geanta 	else {								       \
532c3e337f8SKim Phillips 		if (likely(isr & ch_done_mask)) {			       \
533c3e337f8SKim Phillips 			/* mask further done interrupts. */		       \
534c3e337f8SKim Phillips 			clrbits32(priv->reg + TALITOS_IMR, ch_done_mask);      \
535c3e337f8SKim Phillips 			/* done_task will unmask done interrupts at exit */    \
536c3e337f8SKim Phillips 			tasklet_schedule(&priv->done_task[tlet]);	       \
537c3e337f8SKim Phillips 		}							       \
538511d63cbSHoria Geanta 		spin_unlock_irqrestore(&priv->reg_lock, flags);		       \
539511d63cbSHoria Geanta 	}								       \
540c3e337f8SKim Phillips 									       \
541c3e337f8SKim Phillips 	return (isr & (ch_done_mask | ch_err_mask) || isr_lo) ? IRQ_HANDLED :  \
542c3e337f8SKim Phillips 								IRQ_NONE;      \
5431c2e8811SLee Nipper }
544c3e337f8SKim Phillips DEF_TALITOS_INTERRUPT(4ch, TALITOS_ISR_4CHDONE, TALITOS_ISR_4CHERR, 0)
545c3e337f8SKim Phillips DEF_TALITOS_INTERRUPT(ch0_2, TALITOS_ISR_CH_0_2_DONE, TALITOS_ISR_CH_0_2_ERR, 0)
546c3e337f8SKim Phillips DEF_TALITOS_INTERRUPT(ch1_3, TALITOS_ISR_CH_1_3_DONE, TALITOS_ISR_CH_1_3_ERR, 1)
5479c4a7965SKim Phillips 
5489c4a7965SKim Phillips /*
5499c4a7965SKim Phillips  * hwrng
5509c4a7965SKim Phillips  */
5519c4a7965SKim Phillips static int talitos_rng_data_present(struct hwrng *rng, int wait)
5529c4a7965SKim Phillips {
5539c4a7965SKim Phillips 	struct device *dev = (struct device *)rng->priv;
5549c4a7965SKim Phillips 	struct talitos_private *priv = dev_get_drvdata(dev);
5559c4a7965SKim Phillips 	u32 ofl;
5569c4a7965SKim Phillips 	int i;
5579c4a7965SKim Phillips 
5589c4a7965SKim Phillips 	for (i = 0; i < 20; i++) {
5599c4a7965SKim Phillips 		ofl = in_be32(priv->reg + TALITOS_RNGUSR_LO) &
5609c4a7965SKim Phillips 		      TALITOS_RNGUSR_LO_OFL;
5619c4a7965SKim Phillips 		if (ofl || !wait)
5629c4a7965SKim Phillips 			break;
5639c4a7965SKim Phillips 		udelay(10);
5649c4a7965SKim Phillips 	}
5659c4a7965SKim Phillips 
5669c4a7965SKim Phillips 	return !!ofl;
5679c4a7965SKim Phillips }
5689c4a7965SKim Phillips 
5699c4a7965SKim Phillips static int talitos_rng_data_read(struct hwrng *rng, u32 *data)
5709c4a7965SKim Phillips {
5719c4a7965SKim Phillips 	struct device *dev = (struct device *)rng->priv;
5729c4a7965SKim Phillips 	struct talitos_private *priv = dev_get_drvdata(dev);
5739c4a7965SKim Phillips 
5749c4a7965SKim Phillips 	/* rng fifo requires 64-bit accesses */
5759c4a7965SKim Phillips 	*data = in_be32(priv->reg + TALITOS_RNGU_FIFO);
5769c4a7965SKim Phillips 	*data = in_be32(priv->reg + TALITOS_RNGU_FIFO_LO);
5779c4a7965SKim Phillips 
5789c4a7965SKim Phillips 	return sizeof(u32);
5799c4a7965SKim Phillips }
5809c4a7965SKim Phillips 
5819c4a7965SKim Phillips static int talitos_rng_init(struct hwrng *rng)
5829c4a7965SKim Phillips {
5839c4a7965SKim Phillips 	struct device *dev = (struct device *)rng->priv;
5849c4a7965SKim Phillips 	struct talitos_private *priv = dev_get_drvdata(dev);
5859c4a7965SKim Phillips 	unsigned int timeout = TALITOS_TIMEOUT;
5869c4a7965SKim Phillips 
5879c4a7965SKim Phillips 	setbits32(priv->reg + TALITOS_RNGURCR_LO, TALITOS_RNGURCR_LO_SR);
5889c4a7965SKim Phillips 	while (!(in_be32(priv->reg + TALITOS_RNGUSR_LO) & TALITOS_RNGUSR_LO_RD)
5899c4a7965SKim Phillips 	       && --timeout)
5909c4a7965SKim Phillips 		cpu_relax();
5919c4a7965SKim Phillips 	if (timeout == 0) {
5929c4a7965SKim Phillips 		dev_err(dev, "failed to reset rng hw\n");
5939c4a7965SKim Phillips 		return -ENODEV;
5949c4a7965SKim Phillips 	}
5959c4a7965SKim Phillips 
5969c4a7965SKim Phillips 	/* start generating */
5979c4a7965SKim Phillips 	setbits32(priv->reg + TALITOS_RNGUDSR_LO, 0);
5989c4a7965SKim Phillips 
5999c4a7965SKim Phillips 	return 0;
6009c4a7965SKim Phillips }
6019c4a7965SKim Phillips 
6029c4a7965SKim Phillips static int talitos_register_rng(struct device *dev)
6039c4a7965SKim Phillips {
6049c4a7965SKim Phillips 	struct talitos_private *priv = dev_get_drvdata(dev);
6059c4a7965SKim Phillips 
6069c4a7965SKim Phillips 	priv->rng.name		= dev_driver_string(dev),
6079c4a7965SKim Phillips 	priv->rng.init		= talitos_rng_init,
6089c4a7965SKim Phillips 	priv->rng.data_present	= talitos_rng_data_present,
6099c4a7965SKim Phillips 	priv->rng.data_read	= talitos_rng_data_read,
6109c4a7965SKim Phillips 	priv->rng.priv		= (unsigned long)dev;
6119c4a7965SKim Phillips 
6129c4a7965SKim Phillips 	return hwrng_register(&priv->rng);
6139c4a7965SKim Phillips }
6149c4a7965SKim Phillips 
6159c4a7965SKim Phillips static void talitos_unregister_rng(struct device *dev)
6169c4a7965SKim Phillips {
6179c4a7965SKim Phillips 	struct talitos_private *priv = dev_get_drvdata(dev);
6189c4a7965SKim Phillips 
6199c4a7965SKim Phillips 	hwrng_unregister(&priv->rng);
6209c4a7965SKim Phillips }
6219c4a7965SKim Phillips 
6229c4a7965SKim Phillips /*
6239c4a7965SKim Phillips  * crypto alg
6249c4a7965SKim Phillips  */
6259c4a7965SKim Phillips #define TALITOS_CRA_PRIORITY		3000
626357fb605SHoria Geanta #define TALITOS_MAX_KEY_SIZE		96
6273952f17eSLee Nipper #define TALITOS_MAX_IV_LENGTH		16 /* max of AES_BLOCK_SIZE, DES3_EDE_BLOCK_SIZE */
62870bcaca7SLee Nipper 
629497f2e6bSLee Nipper #define MD5_BLOCK_SIZE    64
6309c4a7965SKim Phillips 
6319c4a7965SKim Phillips struct talitos_ctx {
6329c4a7965SKim Phillips 	struct device *dev;
6335228f0f7SKim Phillips 	int ch;
6349c4a7965SKim Phillips 	__be32 desc_hdr_template;
6359c4a7965SKim Phillips 	u8 key[TALITOS_MAX_KEY_SIZE];
63670bcaca7SLee Nipper 	u8 iv[TALITOS_MAX_IV_LENGTH];
6379c4a7965SKim Phillips 	unsigned int keylen;
6389c4a7965SKim Phillips 	unsigned int enckeylen;
6399c4a7965SKim Phillips 	unsigned int authkeylen;
6409c4a7965SKim Phillips 	unsigned int authsize;
6419c4a7965SKim Phillips };
6429c4a7965SKim Phillips 
643497f2e6bSLee Nipper #define HASH_MAX_BLOCK_SIZE		SHA512_BLOCK_SIZE
644497f2e6bSLee Nipper #define TALITOS_MDEU_MAX_CONTEXT_SIZE	TALITOS_MDEU_CONTEXT_SIZE_SHA384_SHA512
645497f2e6bSLee Nipper 
646497f2e6bSLee Nipper struct talitos_ahash_req_ctx {
64760f208d7SKim Phillips 	u32 hw_context[TALITOS_MDEU_MAX_CONTEXT_SIZE / sizeof(u32)];
648497f2e6bSLee Nipper 	unsigned int hw_context_size;
649497f2e6bSLee Nipper 	u8 buf[HASH_MAX_BLOCK_SIZE];
650497f2e6bSLee Nipper 	u8 bufnext[HASH_MAX_BLOCK_SIZE];
65160f208d7SKim Phillips 	unsigned int swinit;
652497f2e6bSLee Nipper 	unsigned int first;
653497f2e6bSLee Nipper 	unsigned int last;
654497f2e6bSLee Nipper 	unsigned int to_hash_later;
6555e833bc4SLee Nipper 	u64 nbuf;
656497f2e6bSLee Nipper 	struct scatterlist bufsl[2];
657497f2e6bSLee Nipper 	struct scatterlist *psrc;
658497f2e6bSLee Nipper };
659497f2e6bSLee Nipper 
66056af8cd4SLee Nipper static int aead_setauthsize(struct crypto_aead *authenc,
6619c4a7965SKim Phillips 			    unsigned int authsize)
6629c4a7965SKim Phillips {
6639c4a7965SKim Phillips 	struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
6649c4a7965SKim Phillips 
6659c4a7965SKim Phillips 	ctx->authsize = authsize;
6669c4a7965SKim Phillips 
6679c4a7965SKim Phillips 	return 0;
6689c4a7965SKim Phillips }
6699c4a7965SKim Phillips 
67056af8cd4SLee Nipper static int aead_setkey(struct crypto_aead *authenc,
6719c4a7965SKim Phillips 		       const u8 *key, unsigned int keylen)
6729c4a7965SKim Phillips {
6739c4a7965SKim Phillips 	struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
6749c4a7965SKim Phillips 	struct rtattr *rta = (void *)key;
6759c4a7965SKim Phillips 	struct crypto_authenc_key_param *param;
6769c4a7965SKim Phillips 	unsigned int authkeylen;
6779c4a7965SKim Phillips 	unsigned int enckeylen;
6789c4a7965SKim Phillips 
6799c4a7965SKim Phillips 	if (!RTA_OK(rta, keylen))
6809c4a7965SKim Phillips 		goto badkey;
6819c4a7965SKim Phillips 
6829c4a7965SKim Phillips 	if (rta->rta_type != CRYPTO_AUTHENC_KEYA_PARAM)
6839c4a7965SKim Phillips 		goto badkey;
6849c4a7965SKim Phillips 
6859c4a7965SKim Phillips 	if (RTA_PAYLOAD(rta) < sizeof(*param))
6869c4a7965SKim Phillips 		goto badkey;
6879c4a7965SKim Phillips 
6889c4a7965SKim Phillips 	param = RTA_DATA(rta);
6899c4a7965SKim Phillips 	enckeylen = be32_to_cpu(param->enckeylen);
6909c4a7965SKim Phillips 
6919c4a7965SKim Phillips 	key += RTA_ALIGN(rta->rta_len);
6929c4a7965SKim Phillips 	keylen -= RTA_ALIGN(rta->rta_len);
6939c4a7965SKim Phillips 
6949c4a7965SKim Phillips 	if (keylen < enckeylen)
6959c4a7965SKim Phillips 		goto badkey;
6969c4a7965SKim Phillips 
6979c4a7965SKim Phillips 	authkeylen = keylen - enckeylen;
6989c4a7965SKim Phillips 
6999c4a7965SKim Phillips 	if (keylen > TALITOS_MAX_KEY_SIZE)
7009c4a7965SKim Phillips 		goto badkey;
7019c4a7965SKim Phillips 
7029c4a7965SKim Phillips 	memcpy(&ctx->key, key, keylen);
7039c4a7965SKim Phillips 
7049c4a7965SKim Phillips 	ctx->keylen = keylen;
7059c4a7965SKim Phillips 	ctx->enckeylen = enckeylen;
7069c4a7965SKim Phillips 	ctx->authkeylen = authkeylen;
7079c4a7965SKim Phillips 
7089c4a7965SKim Phillips 	return 0;
7099c4a7965SKim Phillips 
7109c4a7965SKim Phillips badkey:
7119c4a7965SKim Phillips 	crypto_aead_set_flags(authenc, CRYPTO_TFM_RES_BAD_KEY_LEN);
7129c4a7965SKim Phillips 	return -EINVAL;
7139c4a7965SKim Phillips }
7149c4a7965SKim Phillips 
7159c4a7965SKim Phillips /*
71656af8cd4SLee Nipper  * talitos_edesc - s/w-extended descriptor
717*79fd31d3SHoria Geanta  * @assoc_nents: number of segments in associated data scatterlist
7189c4a7965SKim Phillips  * @src_nents: number of segments in input scatterlist
7199c4a7965SKim Phillips  * @dst_nents: number of segments in output scatterlist
720*79fd31d3SHoria Geanta  * @assoc_chained: whether assoc is chained or not
7212a1cfe46SHoria Geanta  * @src_chained: whether src is chained or not
7222a1cfe46SHoria Geanta  * @dst_chained: whether dst is chained or not
723*79fd31d3SHoria Geanta  * @iv_dma: dma address of iv for checking continuity and link table
7249c4a7965SKim Phillips  * @dma_len: length of dma mapped link_tbl space
7259c4a7965SKim Phillips  * @dma_link_tbl: bus physical address of link_tbl
7269c4a7965SKim Phillips  * @desc: h/w descriptor
7279c4a7965SKim Phillips  * @link_tbl: input and output h/w link tables (if {src,dst}_nents > 1)
7289c4a7965SKim Phillips  *
7299c4a7965SKim Phillips  * if decrypting (with authcheck), or either one of src_nents or dst_nents
7309c4a7965SKim Phillips  * is greater than 1, an integrity check value is concatenated to the end
7319c4a7965SKim Phillips  * of link_tbl data
7329c4a7965SKim Phillips  */
73356af8cd4SLee Nipper struct talitos_edesc {
734*79fd31d3SHoria Geanta 	int assoc_nents;
7359c4a7965SKim Phillips 	int src_nents;
7369c4a7965SKim Phillips 	int dst_nents;
737*79fd31d3SHoria Geanta 	bool assoc_chained;
7382a1cfe46SHoria Geanta 	bool src_chained;
7392a1cfe46SHoria Geanta 	bool dst_chained;
740*79fd31d3SHoria Geanta 	dma_addr_t iv_dma;
7419c4a7965SKim Phillips 	int dma_len;
7429c4a7965SKim Phillips 	dma_addr_t dma_link_tbl;
7439c4a7965SKim Phillips 	struct talitos_desc desc;
7449c4a7965SKim Phillips 	struct talitos_ptr link_tbl[0];
7459c4a7965SKim Phillips };
7469c4a7965SKim Phillips 
7474de9d0b5SLee Nipper static int talitos_map_sg(struct device *dev, struct scatterlist *sg,
7484de9d0b5SLee Nipper 			  unsigned int nents, enum dma_data_direction dir,
7492a1cfe46SHoria Geanta 			  bool chained)
7504de9d0b5SLee Nipper {
7514de9d0b5SLee Nipper 	if (unlikely(chained))
7524de9d0b5SLee Nipper 		while (sg) {
7534de9d0b5SLee Nipper 			dma_map_sg(dev, sg, 1, dir);
7544de9d0b5SLee Nipper 			sg = scatterwalk_sg_next(sg);
7554de9d0b5SLee Nipper 		}
7564de9d0b5SLee Nipper 	else
7574de9d0b5SLee Nipper 		dma_map_sg(dev, sg, nents, dir);
7584de9d0b5SLee Nipper 	return nents;
7594de9d0b5SLee Nipper }
7604de9d0b5SLee Nipper 
7614de9d0b5SLee Nipper static void talitos_unmap_sg_chain(struct device *dev, struct scatterlist *sg,
7624de9d0b5SLee Nipper 				   enum dma_data_direction dir)
7634de9d0b5SLee Nipper {
7644de9d0b5SLee Nipper 	while (sg) {
7654de9d0b5SLee Nipper 		dma_unmap_sg(dev, sg, 1, dir);
7664de9d0b5SLee Nipper 		sg = scatterwalk_sg_next(sg);
7674de9d0b5SLee Nipper 	}
7684de9d0b5SLee Nipper }
7694de9d0b5SLee Nipper 
7704de9d0b5SLee Nipper static void talitos_sg_unmap(struct device *dev,
7714de9d0b5SLee Nipper 			     struct talitos_edesc *edesc,
7724de9d0b5SLee Nipper 			     struct scatterlist *src,
7734de9d0b5SLee Nipper 			     struct scatterlist *dst)
7744de9d0b5SLee Nipper {
7754de9d0b5SLee Nipper 	unsigned int src_nents = edesc->src_nents ? : 1;
7764de9d0b5SLee Nipper 	unsigned int dst_nents = edesc->dst_nents ? : 1;
7774de9d0b5SLee Nipper 
7784de9d0b5SLee Nipper 	if (src != dst) {
7792a1cfe46SHoria Geanta 		if (edesc->src_chained)
7804de9d0b5SLee Nipper 			talitos_unmap_sg_chain(dev, src, DMA_TO_DEVICE);
7814de9d0b5SLee Nipper 		else
7824de9d0b5SLee Nipper 			dma_unmap_sg(dev, src, src_nents, DMA_TO_DEVICE);
7834de9d0b5SLee Nipper 
784497f2e6bSLee Nipper 		if (dst) {
7852a1cfe46SHoria Geanta 			if (edesc->dst_chained)
786497f2e6bSLee Nipper 				talitos_unmap_sg_chain(dev, dst,
787497f2e6bSLee Nipper 						       DMA_FROM_DEVICE);
7884de9d0b5SLee Nipper 			else
789497f2e6bSLee Nipper 				dma_unmap_sg(dev, dst, dst_nents,
790497f2e6bSLee Nipper 					     DMA_FROM_DEVICE);
791497f2e6bSLee Nipper 		}
7924de9d0b5SLee Nipper 	} else
7932a1cfe46SHoria Geanta 		if (edesc->src_chained)
7944de9d0b5SLee Nipper 			talitos_unmap_sg_chain(dev, src, DMA_BIDIRECTIONAL);
7954de9d0b5SLee Nipper 		else
7964de9d0b5SLee Nipper 			dma_unmap_sg(dev, src, src_nents, DMA_BIDIRECTIONAL);
7974de9d0b5SLee Nipper }
7984de9d0b5SLee Nipper 
7999c4a7965SKim Phillips static void ipsec_esp_unmap(struct device *dev,
80056af8cd4SLee Nipper 			    struct talitos_edesc *edesc,
8019c4a7965SKim Phillips 			    struct aead_request *areq)
8029c4a7965SKim Phillips {
8039c4a7965SKim Phillips 	unmap_single_talitos_ptr(dev, &edesc->desc.ptr[6], DMA_FROM_DEVICE);
8049c4a7965SKim Phillips 	unmap_single_talitos_ptr(dev, &edesc->desc.ptr[3], DMA_TO_DEVICE);
8059c4a7965SKim Phillips 	unmap_single_talitos_ptr(dev, &edesc->desc.ptr[2], DMA_TO_DEVICE);
8069c4a7965SKim Phillips 	unmap_single_talitos_ptr(dev, &edesc->desc.ptr[0], DMA_TO_DEVICE);
8079c4a7965SKim Phillips 
808*79fd31d3SHoria Geanta 	if (edesc->assoc_chained)
809*79fd31d3SHoria Geanta 		talitos_unmap_sg_chain(dev, areq->assoc, DMA_TO_DEVICE);
810*79fd31d3SHoria Geanta 	else
811*79fd31d3SHoria Geanta 		/* assoc_nents counts also for IV in non-contiguous cases */
812*79fd31d3SHoria Geanta 		dma_unmap_sg(dev, areq->assoc,
813*79fd31d3SHoria Geanta 			     edesc->assoc_nents ? edesc->assoc_nents - 1 : 1,
814*79fd31d3SHoria Geanta 			     DMA_TO_DEVICE);
8159c4a7965SKim Phillips 
8164de9d0b5SLee Nipper 	talitos_sg_unmap(dev, edesc, areq->src, areq->dst);
8179c4a7965SKim Phillips 
8189c4a7965SKim Phillips 	if (edesc->dma_len)
8199c4a7965SKim Phillips 		dma_unmap_single(dev, edesc->dma_link_tbl, edesc->dma_len,
8209c4a7965SKim Phillips 				 DMA_BIDIRECTIONAL);
8219c4a7965SKim Phillips }
8229c4a7965SKim Phillips 
8239c4a7965SKim Phillips /*
8249c4a7965SKim Phillips  * ipsec_esp descriptor callbacks
8259c4a7965SKim Phillips  */
8269c4a7965SKim Phillips static void ipsec_esp_encrypt_done(struct device *dev,
8279c4a7965SKim Phillips 				   struct talitos_desc *desc, void *context,
8289c4a7965SKim Phillips 				   int err)
8299c4a7965SKim Phillips {
8309c4a7965SKim Phillips 	struct aead_request *areq = context;
8319c4a7965SKim Phillips 	struct crypto_aead *authenc = crypto_aead_reqtfm(areq);
8329c4a7965SKim Phillips 	struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
83319bbbc63SKim Phillips 	struct talitos_edesc *edesc;
8349c4a7965SKim Phillips 	struct scatterlist *sg;
8359c4a7965SKim Phillips 	void *icvdata;
8369c4a7965SKim Phillips 
83719bbbc63SKim Phillips 	edesc = container_of(desc, struct talitos_edesc, desc);
83819bbbc63SKim Phillips 
8399c4a7965SKim Phillips 	ipsec_esp_unmap(dev, edesc, areq);
8409c4a7965SKim Phillips 
8419c4a7965SKim Phillips 	/* copy the generated ICV to dst */
84260542505SHoria Geanta 	if (edesc->dst_nents) {
8439c4a7965SKim Phillips 		icvdata = &edesc->link_tbl[edesc->src_nents +
844*79fd31d3SHoria Geanta 					   edesc->dst_nents + 2 +
845*79fd31d3SHoria Geanta 					   edesc->assoc_nents];
8469c4a7965SKim Phillips 		sg = sg_last(areq->dst, edesc->dst_nents);
8479c4a7965SKim Phillips 		memcpy((char *)sg_virt(sg) + sg->length - ctx->authsize,
8489c4a7965SKim Phillips 		       icvdata, ctx->authsize);
8499c4a7965SKim Phillips 	}
8509c4a7965SKim Phillips 
8519c4a7965SKim Phillips 	kfree(edesc);
8529c4a7965SKim Phillips 
8539c4a7965SKim Phillips 	aead_request_complete(areq, err);
8549c4a7965SKim Phillips }
8559c4a7965SKim Phillips 
856fe5720e2SKim Phillips static void ipsec_esp_decrypt_swauth_done(struct device *dev,
857e938e465SKim Phillips 					  struct talitos_desc *desc,
858e938e465SKim Phillips 					  void *context, int err)
8599c4a7965SKim Phillips {
8609c4a7965SKim Phillips 	struct aead_request *req = context;
8619c4a7965SKim Phillips 	struct crypto_aead *authenc = crypto_aead_reqtfm(req);
8629c4a7965SKim Phillips 	struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
86319bbbc63SKim Phillips 	struct talitos_edesc *edesc;
8649c4a7965SKim Phillips 	struct scatterlist *sg;
8659c4a7965SKim Phillips 	void *icvdata;
8669c4a7965SKim Phillips 
86719bbbc63SKim Phillips 	edesc = container_of(desc, struct talitos_edesc, desc);
86819bbbc63SKim Phillips 
8699c4a7965SKim Phillips 	ipsec_esp_unmap(dev, edesc, req);
8709c4a7965SKim Phillips 
8719c4a7965SKim Phillips 	if (!err) {
8729c4a7965SKim Phillips 		/* auth check */
8739c4a7965SKim Phillips 		if (edesc->dma_len)
8749c4a7965SKim Phillips 			icvdata = &edesc->link_tbl[edesc->src_nents +
875*79fd31d3SHoria Geanta 						   edesc->dst_nents + 2 +
876*79fd31d3SHoria Geanta 						   edesc->assoc_nents];
8779c4a7965SKim Phillips 		else
8789c4a7965SKim Phillips 			icvdata = &edesc->link_tbl[0];
8799c4a7965SKim Phillips 
8809c4a7965SKim Phillips 		sg = sg_last(req->dst, edesc->dst_nents ? : 1);
8819c4a7965SKim Phillips 		err = memcmp(icvdata, (char *)sg_virt(sg) + sg->length -
8829c4a7965SKim Phillips 			     ctx->authsize, ctx->authsize) ? -EBADMSG : 0;
8839c4a7965SKim Phillips 	}
8849c4a7965SKim Phillips 
8859c4a7965SKim Phillips 	kfree(edesc);
8869c4a7965SKim Phillips 
8879c4a7965SKim Phillips 	aead_request_complete(req, err);
8889c4a7965SKim Phillips }
8899c4a7965SKim Phillips 
890fe5720e2SKim Phillips static void ipsec_esp_decrypt_hwauth_done(struct device *dev,
891e938e465SKim Phillips 					  struct talitos_desc *desc,
892e938e465SKim Phillips 					  void *context, int err)
893fe5720e2SKim Phillips {
894fe5720e2SKim Phillips 	struct aead_request *req = context;
89519bbbc63SKim Phillips 	struct talitos_edesc *edesc;
89619bbbc63SKim Phillips 
89719bbbc63SKim Phillips 	edesc = container_of(desc, struct talitos_edesc, desc);
898fe5720e2SKim Phillips 
899fe5720e2SKim Phillips 	ipsec_esp_unmap(dev, edesc, req);
900fe5720e2SKim Phillips 
901fe5720e2SKim Phillips 	/* check ICV auth status */
902e938e465SKim Phillips 	if (!err && ((desc->hdr_lo & DESC_HDR_LO_ICCR1_MASK) !=
903e938e465SKim Phillips 		     DESC_HDR_LO_ICCR1_PASS))
904fe5720e2SKim Phillips 		err = -EBADMSG;
905fe5720e2SKim Phillips 
906fe5720e2SKim Phillips 	kfree(edesc);
907fe5720e2SKim Phillips 
908fe5720e2SKim Phillips 	aead_request_complete(req, err);
909fe5720e2SKim Phillips }
910fe5720e2SKim Phillips 
9119c4a7965SKim Phillips /*
9129c4a7965SKim Phillips  * convert scatterlist to SEC h/w link table format
9139c4a7965SKim Phillips  * stop at cryptlen bytes
9149c4a7965SKim Phillips  */
91570bcaca7SLee Nipper static int sg_to_link_tbl(struct scatterlist *sg, int sg_count,
9169c4a7965SKim Phillips 			   int cryptlen, struct talitos_ptr *link_tbl_ptr)
9179c4a7965SKim Phillips {
91870bcaca7SLee Nipper 	int n_sg = sg_count;
91970bcaca7SLee Nipper 
92070bcaca7SLee Nipper 	while (n_sg--) {
92181eb024cSKim Phillips 		to_talitos_ptr(link_tbl_ptr, sg_dma_address(sg));
9229c4a7965SKim Phillips 		link_tbl_ptr->len = cpu_to_be16(sg_dma_len(sg));
9239c4a7965SKim Phillips 		link_tbl_ptr->j_extent = 0;
9249c4a7965SKim Phillips 		link_tbl_ptr++;
9259c4a7965SKim Phillips 		cryptlen -= sg_dma_len(sg);
9264de9d0b5SLee Nipper 		sg = scatterwalk_sg_next(sg);
9279c4a7965SKim Phillips 	}
9289c4a7965SKim Phillips 
92970bcaca7SLee Nipper 	/* adjust (decrease) last one (or two) entry's len to cryptlen */
9309c4a7965SKim Phillips 	link_tbl_ptr--;
931c0e741d4SKim Phillips 	while (be16_to_cpu(link_tbl_ptr->len) <= (-cryptlen)) {
93270bcaca7SLee Nipper 		/* Empty this entry, and move to previous one */
93370bcaca7SLee Nipper 		cryptlen += be16_to_cpu(link_tbl_ptr->len);
93470bcaca7SLee Nipper 		link_tbl_ptr->len = 0;
93570bcaca7SLee Nipper 		sg_count--;
93670bcaca7SLee Nipper 		link_tbl_ptr--;
93770bcaca7SLee Nipper 	}
9389c4a7965SKim Phillips 	link_tbl_ptr->len = cpu_to_be16(be16_to_cpu(link_tbl_ptr->len)
9399c4a7965SKim Phillips 					+ cryptlen);
9409c4a7965SKim Phillips 
9419c4a7965SKim Phillips 	/* tag end of link table */
9429c4a7965SKim Phillips 	link_tbl_ptr->j_extent = DESC_PTR_LNKTBL_RETURN;
94370bcaca7SLee Nipper 
94470bcaca7SLee Nipper 	return sg_count;
9459c4a7965SKim Phillips }
9469c4a7965SKim Phillips 
9479c4a7965SKim Phillips /*
9489c4a7965SKim Phillips  * fill in and submit ipsec_esp descriptor
9499c4a7965SKim Phillips  */
95056af8cd4SLee Nipper static int ipsec_esp(struct talitos_edesc *edesc, struct aead_request *areq,
951*79fd31d3SHoria Geanta 		     u64 seq, void (*callback) (struct device *dev,
9529c4a7965SKim Phillips 						struct talitos_desc *desc,
9539c4a7965SKim Phillips 						void *context, int error))
9549c4a7965SKim Phillips {
9559c4a7965SKim Phillips 	struct crypto_aead *aead = crypto_aead_reqtfm(areq);
9569c4a7965SKim Phillips 	struct talitos_ctx *ctx = crypto_aead_ctx(aead);
9579c4a7965SKim Phillips 	struct device *dev = ctx->dev;
9589c4a7965SKim Phillips 	struct talitos_desc *desc = &edesc->desc;
9599c4a7965SKim Phillips 	unsigned int cryptlen = areq->cryptlen;
9609c4a7965SKim Phillips 	unsigned int authsize = ctx->authsize;
961e41256f1SKim Phillips 	unsigned int ivsize = crypto_aead_ivsize(aead);
962fa86a267SKim Phillips 	int sg_count, ret;
963fe5720e2SKim Phillips 	int sg_link_tbl_len;
9649c4a7965SKim Phillips 
9659c4a7965SKim Phillips 	/* hmac key */
9669c4a7965SKim Phillips 	map_single_talitos_ptr(dev, &desc->ptr[0], ctx->authkeylen, &ctx->key,
9679c4a7965SKim Phillips 			       0, DMA_TO_DEVICE);
968*79fd31d3SHoria Geanta 
9699c4a7965SKim Phillips 	/* hmac data */
970*79fd31d3SHoria Geanta 	desc->ptr[1].len = cpu_to_be16(areq->assoclen + ivsize);
971*79fd31d3SHoria Geanta 	if (edesc->assoc_nents) {
972*79fd31d3SHoria Geanta 		int tbl_off = edesc->src_nents + edesc->dst_nents + 2;
973*79fd31d3SHoria Geanta 		struct talitos_ptr *tbl_ptr = &edesc->link_tbl[tbl_off];
974*79fd31d3SHoria Geanta 
975*79fd31d3SHoria Geanta 		to_talitos_ptr(&desc->ptr[1], edesc->dma_link_tbl + tbl_off *
976*79fd31d3SHoria Geanta 			       sizeof(struct talitos_ptr));
977*79fd31d3SHoria Geanta 		desc->ptr[1].j_extent = DESC_PTR_LNKTBL_JUMP;
978*79fd31d3SHoria Geanta 
979*79fd31d3SHoria Geanta 		/* assoc_nents - 1 entries for assoc, 1 for IV */
980*79fd31d3SHoria Geanta 		sg_count = sg_to_link_tbl(areq->assoc, edesc->assoc_nents - 1,
981*79fd31d3SHoria Geanta 					  areq->assoclen, tbl_ptr);
982*79fd31d3SHoria Geanta 
983*79fd31d3SHoria Geanta 		/* add IV to link table */
984*79fd31d3SHoria Geanta 		tbl_ptr += sg_count - 1;
985*79fd31d3SHoria Geanta 		tbl_ptr->j_extent = 0;
986*79fd31d3SHoria Geanta 		tbl_ptr++;
987*79fd31d3SHoria Geanta 		to_talitos_ptr(tbl_ptr, edesc->iv_dma);
988*79fd31d3SHoria Geanta 		tbl_ptr->len = cpu_to_be16(ivsize);
989*79fd31d3SHoria Geanta 		tbl_ptr->j_extent = DESC_PTR_LNKTBL_RETURN;
990*79fd31d3SHoria Geanta 
991*79fd31d3SHoria Geanta 		dma_sync_single_for_device(dev, edesc->dma_link_tbl,
992*79fd31d3SHoria Geanta 					   edesc->dma_len, DMA_BIDIRECTIONAL);
993*79fd31d3SHoria Geanta 	} else {
994*79fd31d3SHoria Geanta 		to_talitos_ptr(&desc->ptr[1], sg_dma_address(areq->assoc));
995*79fd31d3SHoria Geanta 		desc->ptr[1].j_extent = 0;
996*79fd31d3SHoria Geanta 	}
997*79fd31d3SHoria Geanta 
9989c4a7965SKim Phillips 	/* cipher iv */
999*79fd31d3SHoria Geanta 	to_talitos_ptr(&desc->ptr[2], edesc->iv_dma);
1000*79fd31d3SHoria Geanta 	desc->ptr[2].len = cpu_to_be16(ivsize);
1001*79fd31d3SHoria Geanta 	desc->ptr[2].j_extent = 0;
1002*79fd31d3SHoria Geanta 	/* Sync needed for the aead_givencrypt case */
1003*79fd31d3SHoria Geanta 	dma_sync_single_for_device(dev, edesc->iv_dma, ivsize, DMA_TO_DEVICE);
10049c4a7965SKim Phillips 
10059c4a7965SKim Phillips 	/* cipher key */
10069c4a7965SKim Phillips 	map_single_talitos_ptr(dev, &desc->ptr[3], ctx->enckeylen,
10079c4a7965SKim Phillips 			       (char *)&ctx->key + ctx->authkeylen, 0,
10089c4a7965SKim Phillips 			       DMA_TO_DEVICE);
10099c4a7965SKim Phillips 
10109c4a7965SKim Phillips 	/*
10119c4a7965SKim Phillips 	 * cipher in
10129c4a7965SKim Phillips 	 * map and adjust cipher len to aead request cryptlen.
10139c4a7965SKim Phillips 	 * extent is bytes of HMAC postpended to ciphertext,
10149c4a7965SKim Phillips 	 * typically 12 for ipsec
10159c4a7965SKim Phillips 	 */
10169c4a7965SKim Phillips 	desc->ptr[4].len = cpu_to_be16(cryptlen);
10179c4a7965SKim Phillips 	desc->ptr[4].j_extent = authsize;
10189c4a7965SKim Phillips 
1019e938e465SKim Phillips 	sg_count = talitos_map_sg(dev, areq->src, edesc->src_nents ? : 1,
1020e938e465SKim Phillips 				  (areq->src == areq->dst) ? DMA_BIDIRECTIONAL
1021e938e465SKim Phillips 							   : DMA_TO_DEVICE,
10222a1cfe46SHoria Geanta 				  edesc->src_chained);
10239c4a7965SKim Phillips 
10249c4a7965SKim Phillips 	if (sg_count == 1) {
102581eb024cSKim Phillips 		to_talitos_ptr(&desc->ptr[4], sg_dma_address(areq->src));
10269c4a7965SKim Phillips 	} else {
1027fe5720e2SKim Phillips 		sg_link_tbl_len = cryptlen;
1028fe5720e2SKim Phillips 
1029962a9c99SKim Phillips 		if (edesc->desc.hdr & DESC_HDR_MODE1_MDEU_CICV)
1030fe5720e2SKim Phillips 			sg_link_tbl_len = cryptlen + authsize;
1031e938e465SKim Phillips 
1032fe5720e2SKim Phillips 		sg_count = sg_to_link_tbl(areq->src, sg_count, sg_link_tbl_len,
10339c4a7965SKim Phillips 					  &edesc->link_tbl[0]);
103470bcaca7SLee Nipper 		if (sg_count > 1) {
10359c4a7965SKim Phillips 			desc->ptr[4].j_extent |= DESC_PTR_LNKTBL_JUMP;
103681eb024cSKim Phillips 			to_talitos_ptr(&desc->ptr[4], edesc->dma_link_tbl);
1037e938e465SKim Phillips 			dma_sync_single_for_device(dev, edesc->dma_link_tbl,
1038e938e465SKim Phillips 						   edesc->dma_len,
1039e938e465SKim Phillips 						   DMA_BIDIRECTIONAL);
104070bcaca7SLee Nipper 		} else {
104170bcaca7SLee Nipper 			/* Only one segment now, so no link tbl needed */
104281eb024cSKim Phillips 			to_talitos_ptr(&desc->ptr[4],
104381eb024cSKim Phillips 				       sg_dma_address(areq->src));
104470bcaca7SLee Nipper 		}
10459c4a7965SKim Phillips 	}
10469c4a7965SKim Phillips 
10479c4a7965SKim Phillips 	/* cipher out */
10489c4a7965SKim Phillips 	desc->ptr[5].len = cpu_to_be16(cryptlen);
10499c4a7965SKim Phillips 	desc->ptr[5].j_extent = authsize;
10509c4a7965SKim Phillips 
1051e938e465SKim Phillips 	if (areq->src != areq->dst)
10524de9d0b5SLee Nipper 		sg_count = talitos_map_sg(dev, areq->dst,
10534de9d0b5SLee Nipper 					  edesc->dst_nents ? : 1,
10542a1cfe46SHoria Geanta 					  DMA_FROM_DEVICE, edesc->dst_chained);
10559c4a7965SKim Phillips 
10569c4a7965SKim Phillips 	if (sg_count == 1) {
105781eb024cSKim Phillips 		to_talitos_ptr(&desc->ptr[5], sg_dma_address(areq->dst));
10589c4a7965SKim Phillips 	} else {
1059*79fd31d3SHoria Geanta 		int tbl_off = edesc->src_nents + 1;
1060*79fd31d3SHoria Geanta 		struct talitos_ptr *tbl_ptr = &edesc->link_tbl[tbl_off];
10619c4a7965SKim Phillips 
106281eb024cSKim Phillips 		to_talitos_ptr(&desc->ptr[5], edesc->dma_link_tbl +
1063*79fd31d3SHoria Geanta 			       tbl_off * sizeof(struct talitos_ptr));
106470bcaca7SLee Nipper 		sg_count = sg_to_link_tbl(areq->dst, sg_count, cryptlen,
1065*79fd31d3SHoria Geanta 					  tbl_ptr);
1066fe5720e2SKim Phillips 
1067f3c85bc1SLee Nipper 		/* Add an entry to the link table for ICV data */
1068*79fd31d3SHoria Geanta 		tbl_ptr += sg_count - 1;
1069*79fd31d3SHoria Geanta 		tbl_ptr->j_extent = 0;
1070*79fd31d3SHoria Geanta 		tbl_ptr++;
1071*79fd31d3SHoria Geanta 		tbl_ptr->j_extent = DESC_PTR_LNKTBL_RETURN;
1072*79fd31d3SHoria Geanta 		tbl_ptr->len = cpu_to_be16(authsize);
10739c4a7965SKim Phillips 
10749c4a7965SKim Phillips 		/* icv data follows link tables */
1075*79fd31d3SHoria Geanta 		to_talitos_ptr(tbl_ptr, edesc->dma_link_tbl +
1076*79fd31d3SHoria Geanta 			       (tbl_off + edesc->dst_nents + 1 +
1077*79fd31d3SHoria Geanta 				edesc->assoc_nents) *
107881eb024cSKim Phillips 			       sizeof(struct talitos_ptr));
10799c4a7965SKim Phillips 		desc->ptr[5].j_extent |= DESC_PTR_LNKTBL_JUMP;
10809c4a7965SKim Phillips 		dma_sync_single_for_device(ctx->dev, edesc->dma_link_tbl,
10819c4a7965SKim Phillips 					   edesc->dma_len, DMA_BIDIRECTIONAL);
10829c4a7965SKim Phillips 	}
10839c4a7965SKim Phillips 
10849c4a7965SKim Phillips 	/* iv out */
10859c4a7965SKim Phillips 	map_single_talitos_ptr(dev, &desc->ptr[6], ivsize, ctx->iv, 0,
10869c4a7965SKim Phillips 			       DMA_FROM_DEVICE);
10879c4a7965SKim Phillips 
10885228f0f7SKim Phillips 	ret = talitos_submit(dev, ctx->ch, desc, callback, areq);
1089fa86a267SKim Phillips 	if (ret != -EINPROGRESS) {
1090fa86a267SKim Phillips 		ipsec_esp_unmap(dev, edesc, areq);
1091fa86a267SKim Phillips 		kfree(edesc);
1092fa86a267SKim Phillips 	}
1093fa86a267SKim Phillips 	return ret;
10949c4a7965SKim Phillips }
10959c4a7965SKim Phillips 
10969c4a7965SKim Phillips /*
10979c4a7965SKim Phillips  * derive number of elements in scatterlist
10989c4a7965SKim Phillips  */
10992a1cfe46SHoria Geanta static int sg_count(struct scatterlist *sg_list, int nbytes, bool *chained)
11009c4a7965SKim Phillips {
11019c4a7965SKim Phillips 	struct scatterlist *sg = sg_list;
11029c4a7965SKim Phillips 	int sg_nents = 0;
11039c4a7965SKim Phillips 
11042a1cfe46SHoria Geanta 	*chained = false;
11054de9d0b5SLee Nipper 	while (nbytes > 0) {
11069c4a7965SKim Phillips 		sg_nents++;
11079c4a7965SKim Phillips 		nbytes -= sg->length;
11084de9d0b5SLee Nipper 		if (!sg_is_last(sg) && (sg + 1)->length == 0)
11092a1cfe46SHoria Geanta 			*chained = true;
11104de9d0b5SLee Nipper 		sg = scatterwalk_sg_next(sg);
11119c4a7965SKim Phillips 	}
11129c4a7965SKim Phillips 
11139c4a7965SKim Phillips 	return sg_nents;
11149c4a7965SKim Phillips }
11159c4a7965SKim Phillips 
1116497f2e6bSLee Nipper /**
1117497f2e6bSLee Nipper  * sg_copy_end_to_buffer - Copy end data from SG list to a linear buffer
1118497f2e6bSLee Nipper  * @sgl:		 The SG list
1119497f2e6bSLee Nipper  * @nents:		 Number of SG entries
1120497f2e6bSLee Nipper  * @buf:		 Where to copy to
1121497f2e6bSLee Nipper  * @buflen:		 The number of bytes to copy
1122497f2e6bSLee Nipper  * @skip:		 The number of bytes to skip before copying.
1123497f2e6bSLee Nipper  *                       Note: skip + buflen should equal SG total size.
1124497f2e6bSLee Nipper  *
1125497f2e6bSLee Nipper  * Returns the number of copied bytes.
1126497f2e6bSLee Nipper  *
1127497f2e6bSLee Nipper  **/
1128497f2e6bSLee Nipper static size_t sg_copy_end_to_buffer(struct scatterlist *sgl, unsigned int nents,
1129497f2e6bSLee Nipper 				    void *buf, size_t buflen, unsigned int skip)
1130497f2e6bSLee Nipper {
1131497f2e6bSLee Nipper 	unsigned int offset = 0;
1132497f2e6bSLee Nipper 	unsigned int boffset = 0;
1133497f2e6bSLee Nipper 	struct sg_mapping_iter miter;
1134497f2e6bSLee Nipper 	unsigned long flags;
1135497f2e6bSLee Nipper 	unsigned int sg_flags = SG_MITER_ATOMIC;
1136497f2e6bSLee Nipper 	size_t total_buffer = buflen + skip;
1137497f2e6bSLee Nipper 
1138497f2e6bSLee Nipper 	sg_flags |= SG_MITER_FROM_SG;
1139497f2e6bSLee Nipper 
1140497f2e6bSLee Nipper 	sg_miter_start(&miter, sgl, nents, sg_flags);
1141497f2e6bSLee Nipper 
1142497f2e6bSLee Nipper 	local_irq_save(flags);
1143497f2e6bSLee Nipper 
1144497f2e6bSLee Nipper 	while (sg_miter_next(&miter) && offset < total_buffer) {
1145497f2e6bSLee Nipper 		unsigned int len;
1146497f2e6bSLee Nipper 		unsigned int ignore;
1147497f2e6bSLee Nipper 
1148497f2e6bSLee Nipper 		if ((offset + miter.length) > skip) {
1149497f2e6bSLee Nipper 			if (offset < skip) {
1150497f2e6bSLee Nipper 				/* Copy part of this segment */
1151497f2e6bSLee Nipper 				ignore = skip - offset;
1152497f2e6bSLee Nipper 				len = miter.length - ignore;
11537260042bSLee Nipper 				if (boffset + len > buflen)
11547260042bSLee Nipper 					len = buflen - boffset;
1155497f2e6bSLee Nipper 				memcpy(buf + boffset, miter.addr + ignore, len);
1156497f2e6bSLee Nipper 			} else {
11577260042bSLee Nipper 				/* Copy all of this segment (up to buflen) */
1158497f2e6bSLee Nipper 				len = miter.length;
11597260042bSLee Nipper 				if (boffset + len > buflen)
11607260042bSLee Nipper 					len = buflen - boffset;
1161497f2e6bSLee Nipper 				memcpy(buf + boffset, miter.addr, len);
1162497f2e6bSLee Nipper 			}
1163497f2e6bSLee Nipper 			boffset += len;
1164497f2e6bSLee Nipper 		}
1165497f2e6bSLee Nipper 		offset += miter.length;
1166497f2e6bSLee Nipper 	}
1167497f2e6bSLee Nipper 
1168497f2e6bSLee Nipper 	sg_miter_stop(&miter);
1169497f2e6bSLee Nipper 
1170497f2e6bSLee Nipper 	local_irq_restore(flags);
1171497f2e6bSLee Nipper 	return boffset;
1172497f2e6bSLee Nipper }
1173497f2e6bSLee Nipper 
11749c4a7965SKim Phillips /*
117556af8cd4SLee Nipper  * allocate and map the extended descriptor
11769c4a7965SKim Phillips  */
11774de9d0b5SLee Nipper static struct talitos_edesc *talitos_edesc_alloc(struct device *dev,
1178*79fd31d3SHoria Geanta 						 struct scatterlist *assoc,
11794de9d0b5SLee Nipper 						 struct scatterlist *src,
11804de9d0b5SLee Nipper 						 struct scatterlist *dst,
1181*79fd31d3SHoria Geanta 						 u8 *iv,
1182*79fd31d3SHoria Geanta 						 unsigned int assoclen,
11834de9d0b5SLee Nipper 						 unsigned int cryptlen,
11844de9d0b5SLee Nipper 						 unsigned int authsize,
1185*79fd31d3SHoria Geanta 						 unsigned int ivsize,
11864de9d0b5SLee Nipper 						 int icv_stashing,
11874de9d0b5SLee Nipper 						 u32 cryptoflags)
11889c4a7965SKim Phillips {
118956af8cd4SLee Nipper 	struct talitos_edesc *edesc;
1190*79fd31d3SHoria Geanta 	int assoc_nents = 0, src_nents, dst_nents, alloc_len, dma_len;
1191*79fd31d3SHoria Geanta 	bool assoc_chained = false, src_chained = false, dst_chained = false;
1192*79fd31d3SHoria Geanta 	dma_addr_t iv_dma = 0;
11934de9d0b5SLee Nipper 	gfp_t flags = cryptoflags & CRYPTO_TFM_REQ_MAY_SLEEP ? GFP_KERNEL :
1194586725f8SKim Phillips 		      GFP_ATOMIC;
11959c4a7965SKim Phillips 
11964de9d0b5SLee Nipper 	if (cryptlen + authsize > TALITOS_MAX_DATA_LEN) {
11974de9d0b5SLee Nipper 		dev_err(dev, "length exceeds h/w max limit\n");
11989c4a7965SKim Phillips 		return ERR_PTR(-EINVAL);
11999c4a7965SKim Phillips 	}
12009c4a7965SKim Phillips 
1201*79fd31d3SHoria Geanta 	if (iv)
1202*79fd31d3SHoria Geanta 		iv_dma = dma_map_single(dev, iv, ivsize, DMA_TO_DEVICE);
1203*79fd31d3SHoria Geanta 
1204*79fd31d3SHoria Geanta 	if (assoc) {
1205*79fd31d3SHoria Geanta 		/*
1206*79fd31d3SHoria Geanta 		 * Currently it is assumed that iv is provided whenever assoc
1207*79fd31d3SHoria Geanta 		 * is.
1208*79fd31d3SHoria Geanta 		 */
1209*79fd31d3SHoria Geanta 		BUG_ON(!iv);
1210*79fd31d3SHoria Geanta 
1211*79fd31d3SHoria Geanta 		assoc_nents = sg_count(assoc, assoclen, &assoc_chained);
1212*79fd31d3SHoria Geanta 		talitos_map_sg(dev, assoc, assoc_nents, DMA_TO_DEVICE,
1213*79fd31d3SHoria Geanta 			       assoc_chained);
1214*79fd31d3SHoria Geanta 		assoc_nents = (assoc_nents == 1) ? 0 : assoc_nents;
1215*79fd31d3SHoria Geanta 
1216*79fd31d3SHoria Geanta 		if (assoc_nents || sg_dma_address(assoc) + assoclen != iv_dma)
1217*79fd31d3SHoria Geanta 			assoc_nents = assoc_nents ? assoc_nents + 1 : 2;
1218*79fd31d3SHoria Geanta 	}
1219*79fd31d3SHoria Geanta 
12204de9d0b5SLee Nipper 	src_nents = sg_count(src, cryptlen + authsize, &src_chained);
12219c4a7965SKim Phillips 	src_nents = (src_nents == 1) ? 0 : src_nents;
12229c4a7965SKim Phillips 
1223602499a3SHoria Geanta 	if (!dst) {
1224497f2e6bSLee Nipper 		dst_nents = 0;
1225497f2e6bSLee Nipper 	} else {
12264de9d0b5SLee Nipper 		if (dst == src) {
12279c4a7965SKim Phillips 			dst_nents = src_nents;
12289c4a7965SKim Phillips 		} else {
1229497f2e6bSLee Nipper 			dst_nents = sg_count(dst, cryptlen + authsize,
1230497f2e6bSLee Nipper 					     &dst_chained);
1231695ad589SLee Nipper 			dst_nents = (dst_nents == 1) ? 0 : dst_nents;
12329c4a7965SKim Phillips 		}
1233497f2e6bSLee Nipper 	}
12349c4a7965SKim Phillips 
12359c4a7965SKim Phillips 	/*
12369c4a7965SKim Phillips 	 * allocate space for base edesc plus the link tables,
1237f3c85bc1SLee Nipper 	 * allowing for two separate entries for ICV and generated ICV (+ 2),
12389c4a7965SKim Phillips 	 * and the ICV data itself
12399c4a7965SKim Phillips 	 */
124056af8cd4SLee Nipper 	alloc_len = sizeof(struct talitos_edesc);
1241*79fd31d3SHoria Geanta 	if (assoc_nents || src_nents || dst_nents) {
1242*79fd31d3SHoria Geanta 		dma_len = (src_nents + dst_nents + 2 + assoc_nents) *
12434de9d0b5SLee Nipper 			  sizeof(struct talitos_ptr) + authsize;
12449c4a7965SKim Phillips 		alloc_len += dma_len;
12459c4a7965SKim Phillips 	} else {
12469c4a7965SKim Phillips 		dma_len = 0;
12474de9d0b5SLee Nipper 		alloc_len += icv_stashing ? authsize : 0;
12489c4a7965SKim Phillips 	}
12499c4a7965SKim Phillips 
1250586725f8SKim Phillips 	edesc = kmalloc(alloc_len, GFP_DMA | flags);
12519c4a7965SKim Phillips 	if (!edesc) {
1252*79fd31d3SHoria Geanta 		talitos_unmap_sg_chain(dev, assoc, DMA_TO_DEVICE);
1253*79fd31d3SHoria Geanta 		if (iv_dma)
1254*79fd31d3SHoria Geanta 			dma_unmap_single(dev, iv_dma, ivsize, DMA_TO_DEVICE);
12554de9d0b5SLee Nipper 		dev_err(dev, "could not allocate edescriptor\n");
12569c4a7965SKim Phillips 		return ERR_PTR(-ENOMEM);
12579c4a7965SKim Phillips 	}
12589c4a7965SKim Phillips 
1259*79fd31d3SHoria Geanta 	edesc->assoc_nents = assoc_nents;
12609c4a7965SKim Phillips 	edesc->src_nents = src_nents;
12619c4a7965SKim Phillips 	edesc->dst_nents = dst_nents;
1262*79fd31d3SHoria Geanta 	edesc->assoc_chained = assoc_chained;
12632a1cfe46SHoria Geanta 	edesc->src_chained = src_chained;
12642a1cfe46SHoria Geanta 	edesc->dst_chained = dst_chained;
1265*79fd31d3SHoria Geanta 	edesc->iv_dma = iv_dma;
12669c4a7965SKim Phillips 	edesc->dma_len = dma_len;
1267497f2e6bSLee Nipper 	if (dma_len)
12684de9d0b5SLee Nipper 		edesc->dma_link_tbl = dma_map_single(dev, &edesc->link_tbl[0],
1269497f2e6bSLee Nipper 						     edesc->dma_len,
1270497f2e6bSLee Nipper 						     DMA_BIDIRECTIONAL);
12719c4a7965SKim Phillips 
12729c4a7965SKim Phillips 	return edesc;
12739c4a7965SKim Phillips }
12749c4a7965SKim Phillips 
1275*79fd31d3SHoria Geanta static struct talitos_edesc *aead_edesc_alloc(struct aead_request *areq, u8 *iv,
12764de9d0b5SLee Nipper 					      int icv_stashing)
12774de9d0b5SLee Nipper {
12784de9d0b5SLee Nipper 	struct crypto_aead *authenc = crypto_aead_reqtfm(areq);
12794de9d0b5SLee Nipper 	struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
1280*79fd31d3SHoria Geanta 	unsigned int ivsize = crypto_aead_ivsize(authenc);
12814de9d0b5SLee Nipper 
1282*79fd31d3SHoria Geanta 	return talitos_edesc_alloc(ctx->dev, areq->assoc, areq->src, areq->dst,
1283*79fd31d3SHoria Geanta 				   iv, areq->assoclen, areq->cryptlen,
1284*79fd31d3SHoria Geanta 				   ctx->authsize, ivsize, icv_stashing,
12854de9d0b5SLee Nipper 				   areq->base.flags);
12864de9d0b5SLee Nipper }
12874de9d0b5SLee Nipper 
128856af8cd4SLee Nipper static int aead_encrypt(struct aead_request *req)
12899c4a7965SKim Phillips {
12909c4a7965SKim Phillips 	struct crypto_aead *authenc = crypto_aead_reqtfm(req);
12919c4a7965SKim Phillips 	struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
129256af8cd4SLee Nipper 	struct talitos_edesc *edesc;
12939c4a7965SKim Phillips 
12949c4a7965SKim Phillips 	/* allocate extended descriptor */
1295*79fd31d3SHoria Geanta 	edesc = aead_edesc_alloc(req, req->iv, 0);
12969c4a7965SKim Phillips 	if (IS_ERR(edesc))
12979c4a7965SKim Phillips 		return PTR_ERR(edesc);
12989c4a7965SKim Phillips 
12999c4a7965SKim Phillips 	/* set encrypt */
130070bcaca7SLee Nipper 	edesc->desc.hdr = ctx->desc_hdr_template | DESC_HDR_MODE0_ENCRYPT;
13019c4a7965SKim Phillips 
1302*79fd31d3SHoria Geanta 	return ipsec_esp(edesc, req, 0, ipsec_esp_encrypt_done);
13039c4a7965SKim Phillips }
13049c4a7965SKim Phillips 
130556af8cd4SLee Nipper static int aead_decrypt(struct aead_request *req)
13069c4a7965SKim Phillips {
13079c4a7965SKim Phillips 	struct crypto_aead *authenc = crypto_aead_reqtfm(req);
13089c4a7965SKim Phillips 	struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
13099c4a7965SKim Phillips 	unsigned int authsize = ctx->authsize;
1310fe5720e2SKim Phillips 	struct talitos_private *priv = dev_get_drvdata(ctx->dev);
131156af8cd4SLee Nipper 	struct talitos_edesc *edesc;
13129c4a7965SKim Phillips 	struct scatterlist *sg;
13139c4a7965SKim Phillips 	void *icvdata;
13149c4a7965SKim Phillips 
13159c4a7965SKim Phillips 	req->cryptlen -= authsize;
13169c4a7965SKim Phillips 
13179c4a7965SKim Phillips 	/* allocate extended descriptor */
1318*79fd31d3SHoria Geanta 	edesc = aead_edesc_alloc(req, req->iv, 1);
13199c4a7965SKim Phillips 	if (IS_ERR(edesc))
13209c4a7965SKim Phillips 		return PTR_ERR(edesc);
13219c4a7965SKim Phillips 
1322fe5720e2SKim Phillips 	if ((priv->features & TALITOS_FTR_HW_AUTH_CHECK) &&
1323e938e465SKim Phillips 	    ((!edesc->src_nents && !edesc->dst_nents) ||
1324e938e465SKim Phillips 	     priv->features & TALITOS_FTR_SRC_LINK_TBL_LEN_INCLUDES_EXTENT)) {
1325fe5720e2SKim Phillips 
1326fe5720e2SKim Phillips 		/* decrypt and check the ICV */
1327e938e465SKim Phillips 		edesc->desc.hdr = ctx->desc_hdr_template |
1328e938e465SKim Phillips 				  DESC_HDR_DIR_INBOUND |
1329fe5720e2SKim Phillips 				  DESC_HDR_MODE1_MDEU_CICV;
1330fe5720e2SKim Phillips 
1331fe5720e2SKim Phillips 		/* reset integrity check result bits */
1332fe5720e2SKim Phillips 		edesc->desc.hdr_lo = 0;
1333fe5720e2SKim Phillips 
1334*79fd31d3SHoria Geanta 		return ipsec_esp(edesc, req, 0, ipsec_esp_decrypt_hwauth_done);
1335e938e465SKim Phillips 	}
1336fe5720e2SKim Phillips 
1337fe5720e2SKim Phillips 	/* Have to check the ICV with software */
1338fe5720e2SKim Phillips 	edesc->desc.hdr = ctx->desc_hdr_template | DESC_HDR_DIR_INBOUND;
1339fe5720e2SKim Phillips 
13409c4a7965SKim Phillips 	/* stash incoming ICV for later cmp with ICV generated by the h/w */
13419c4a7965SKim Phillips 	if (edesc->dma_len)
13429c4a7965SKim Phillips 		icvdata = &edesc->link_tbl[edesc->src_nents +
1343*79fd31d3SHoria Geanta 					   edesc->dst_nents + 2 +
1344*79fd31d3SHoria Geanta 					   edesc->assoc_nents];
13459c4a7965SKim Phillips 	else
13469c4a7965SKim Phillips 		icvdata = &edesc->link_tbl[0];
13479c4a7965SKim Phillips 
13489c4a7965SKim Phillips 	sg = sg_last(req->src, edesc->src_nents ? : 1);
13499c4a7965SKim Phillips 
13509c4a7965SKim Phillips 	memcpy(icvdata, (char *)sg_virt(sg) + sg->length - ctx->authsize,
13519c4a7965SKim Phillips 	       ctx->authsize);
13529c4a7965SKim Phillips 
1353*79fd31d3SHoria Geanta 	return ipsec_esp(edesc, req, 0, ipsec_esp_decrypt_swauth_done);
1354fe5720e2SKim Phillips }
13559c4a7965SKim Phillips 
135656af8cd4SLee Nipper static int aead_givencrypt(struct aead_givcrypt_request *req)
13579c4a7965SKim Phillips {
13589c4a7965SKim Phillips 	struct aead_request *areq = &req->areq;
13599c4a7965SKim Phillips 	struct crypto_aead *authenc = crypto_aead_reqtfm(areq);
13609c4a7965SKim Phillips 	struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
136156af8cd4SLee Nipper 	struct talitos_edesc *edesc;
13629c4a7965SKim Phillips 
13639c4a7965SKim Phillips 	/* allocate extended descriptor */
1364*79fd31d3SHoria Geanta 	edesc = aead_edesc_alloc(areq, req->giv, 0);
13659c4a7965SKim Phillips 	if (IS_ERR(edesc))
13669c4a7965SKim Phillips 		return PTR_ERR(edesc);
13679c4a7965SKim Phillips 
13689c4a7965SKim Phillips 	/* set encrypt */
136970bcaca7SLee Nipper 	edesc->desc.hdr = ctx->desc_hdr_template | DESC_HDR_MODE0_ENCRYPT;
13709c4a7965SKim Phillips 
13719c4a7965SKim Phillips 	memcpy(req->giv, ctx->iv, crypto_aead_ivsize(authenc));
1372ba95487dSKim Phillips 	/* avoid consecutive packets going out with same IV */
1373ba95487dSKim Phillips 	*(__be64 *)req->giv ^= cpu_to_be64(req->seq);
13749c4a7965SKim Phillips 
1375*79fd31d3SHoria Geanta 	return ipsec_esp(edesc, areq, req->seq, ipsec_esp_encrypt_done);
13769c4a7965SKim Phillips }
13779c4a7965SKim Phillips 
13784de9d0b5SLee Nipper static int ablkcipher_setkey(struct crypto_ablkcipher *cipher,
13794de9d0b5SLee Nipper 			     const u8 *key, unsigned int keylen)
13804de9d0b5SLee Nipper {
13814de9d0b5SLee Nipper 	struct talitos_ctx *ctx = crypto_ablkcipher_ctx(cipher);
13824de9d0b5SLee Nipper 
13834de9d0b5SLee Nipper 	memcpy(&ctx->key, key, keylen);
13844de9d0b5SLee Nipper 	ctx->keylen = keylen;
13854de9d0b5SLee Nipper 
13864de9d0b5SLee Nipper 	return 0;
13874de9d0b5SLee Nipper }
13884de9d0b5SLee Nipper 
13894de9d0b5SLee Nipper static void common_nonsnoop_unmap(struct device *dev,
13904de9d0b5SLee Nipper 				  struct talitos_edesc *edesc,
13914de9d0b5SLee Nipper 				  struct ablkcipher_request *areq)
13924de9d0b5SLee Nipper {
13934de9d0b5SLee Nipper 	unmap_single_talitos_ptr(dev, &edesc->desc.ptr[5], DMA_FROM_DEVICE);
13944de9d0b5SLee Nipper 	unmap_single_talitos_ptr(dev, &edesc->desc.ptr[2], DMA_TO_DEVICE);
13954de9d0b5SLee Nipper 	unmap_single_talitos_ptr(dev, &edesc->desc.ptr[1], DMA_TO_DEVICE);
13964de9d0b5SLee Nipper 
13974de9d0b5SLee Nipper 	talitos_sg_unmap(dev, edesc, areq->src, areq->dst);
13984de9d0b5SLee Nipper 
13994de9d0b5SLee Nipper 	if (edesc->dma_len)
14004de9d0b5SLee Nipper 		dma_unmap_single(dev, edesc->dma_link_tbl, edesc->dma_len,
14014de9d0b5SLee Nipper 				 DMA_BIDIRECTIONAL);
14024de9d0b5SLee Nipper }
14034de9d0b5SLee Nipper 
14044de9d0b5SLee Nipper static void ablkcipher_done(struct device *dev,
14054de9d0b5SLee Nipper 			    struct talitos_desc *desc, void *context,
14064de9d0b5SLee Nipper 			    int err)
14074de9d0b5SLee Nipper {
14084de9d0b5SLee Nipper 	struct ablkcipher_request *areq = context;
140919bbbc63SKim Phillips 	struct talitos_edesc *edesc;
141019bbbc63SKim Phillips 
141119bbbc63SKim Phillips 	edesc = container_of(desc, struct talitos_edesc, desc);
14124de9d0b5SLee Nipper 
14134de9d0b5SLee Nipper 	common_nonsnoop_unmap(dev, edesc, areq);
14144de9d0b5SLee Nipper 
14154de9d0b5SLee Nipper 	kfree(edesc);
14164de9d0b5SLee Nipper 
14174de9d0b5SLee Nipper 	areq->base.complete(&areq->base, err);
14184de9d0b5SLee Nipper }
14194de9d0b5SLee Nipper 
14204de9d0b5SLee Nipper static int common_nonsnoop(struct talitos_edesc *edesc,
14214de9d0b5SLee Nipper 			   struct ablkcipher_request *areq,
14224de9d0b5SLee Nipper 			   void (*callback) (struct device *dev,
14234de9d0b5SLee Nipper 					     struct talitos_desc *desc,
14244de9d0b5SLee Nipper 					     void *context, int error))
14254de9d0b5SLee Nipper {
14264de9d0b5SLee Nipper 	struct crypto_ablkcipher *cipher = crypto_ablkcipher_reqtfm(areq);
14274de9d0b5SLee Nipper 	struct talitos_ctx *ctx = crypto_ablkcipher_ctx(cipher);
14284de9d0b5SLee Nipper 	struct device *dev = ctx->dev;
14294de9d0b5SLee Nipper 	struct talitos_desc *desc = &edesc->desc;
14304de9d0b5SLee Nipper 	unsigned int cryptlen = areq->nbytes;
1431*79fd31d3SHoria Geanta 	unsigned int ivsize = crypto_ablkcipher_ivsize(cipher);
14324de9d0b5SLee Nipper 	int sg_count, ret;
14334de9d0b5SLee Nipper 
14344de9d0b5SLee Nipper 	/* first DWORD empty */
14354de9d0b5SLee Nipper 	desc->ptr[0].len = 0;
143681eb024cSKim Phillips 	to_talitos_ptr(&desc->ptr[0], 0);
14374de9d0b5SLee Nipper 	desc->ptr[0].j_extent = 0;
14384de9d0b5SLee Nipper 
14394de9d0b5SLee Nipper 	/* cipher iv */
1440*79fd31d3SHoria Geanta 	to_talitos_ptr(&desc->ptr[1], edesc->iv_dma);
1441*79fd31d3SHoria Geanta 	desc->ptr[1].len = cpu_to_be16(ivsize);
1442*79fd31d3SHoria Geanta 	desc->ptr[1].j_extent = 0;
14434de9d0b5SLee Nipper 
14444de9d0b5SLee Nipper 	/* cipher key */
14454de9d0b5SLee Nipper 	map_single_talitos_ptr(dev, &desc->ptr[2], ctx->keylen,
14464de9d0b5SLee Nipper 			       (char *)&ctx->key, 0, DMA_TO_DEVICE);
14474de9d0b5SLee Nipper 
14484de9d0b5SLee Nipper 	/*
14494de9d0b5SLee Nipper 	 * cipher in
14504de9d0b5SLee Nipper 	 */
14514de9d0b5SLee Nipper 	desc->ptr[3].len = cpu_to_be16(cryptlen);
14524de9d0b5SLee Nipper 	desc->ptr[3].j_extent = 0;
14534de9d0b5SLee Nipper 
14544de9d0b5SLee Nipper 	sg_count = talitos_map_sg(dev, areq->src, edesc->src_nents ? : 1,
14554de9d0b5SLee Nipper 				  (areq->src == areq->dst) ? DMA_BIDIRECTIONAL
14564de9d0b5SLee Nipper 							   : DMA_TO_DEVICE,
14572a1cfe46SHoria Geanta 				  edesc->src_chained);
14584de9d0b5SLee Nipper 
14594de9d0b5SLee Nipper 	if (sg_count == 1) {
146081eb024cSKim Phillips 		to_talitos_ptr(&desc->ptr[3], sg_dma_address(areq->src));
14614de9d0b5SLee Nipper 	} else {
14624de9d0b5SLee Nipper 		sg_count = sg_to_link_tbl(areq->src, sg_count, cryptlen,
14634de9d0b5SLee Nipper 					  &edesc->link_tbl[0]);
14644de9d0b5SLee Nipper 		if (sg_count > 1) {
146581eb024cSKim Phillips 			to_talitos_ptr(&desc->ptr[3], edesc->dma_link_tbl);
14664de9d0b5SLee Nipper 			desc->ptr[3].j_extent |= DESC_PTR_LNKTBL_JUMP;
1467e938e465SKim Phillips 			dma_sync_single_for_device(dev, edesc->dma_link_tbl,
1468e938e465SKim Phillips 						   edesc->dma_len,
1469e938e465SKim Phillips 						   DMA_BIDIRECTIONAL);
14704de9d0b5SLee Nipper 		} else {
14714de9d0b5SLee Nipper 			/* Only one segment now, so no link tbl needed */
147281eb024cSKim Phillips 			to_talitos_ptr(&desc->ptr[3],
147381eb024cSKim Phillips 				       sg_dma_address(areq->src));
14744de9d0b5SLee Nipper 		}
14754de9d0b5SLee Nipper 	}
14764de9d0b5SLee Nipper 
14774de9d0b5SLee Nipper 	/* cipher out */
14784de9d0b5SLee Nipper 	desc->ptr[4].len = cpu_to_be16(cryptlen);
14794de9d0b5SLee Nipper 	desc->ptr[4].j_extent = 0;
14804de9d0b5SLee Nipper 
14814de9d0b5SLee Nipper 	if (areq->src != areq->dst)
14824de9d0b5SLee Nipper 		sg_count = talitos_map_sg(dev, areq->dst,
14834de9d0b5SLee Nipper 					  edesc->dst_nents ? : 1,
14842a1cfe46SHoria Geanta 					  DMA_FROM_DEVICE, edesc->dst_chained);
14854de9d0b5SLee Nipper 
14864de9d0b5SLee Nipper 	if (sg_count == 1) {
148781eb024cSKim Phillips 		to_talitos_ptr(&desc->ptr[4], sg_dma_address(areq->dst));
14884de9d0b5SLee Nipper 	} else {
14894de9d0b5SLee Nipper 		struct talitos_ptr *link_tbl_ptr =
14904de9d0b5SLee Nipper 			&edesc->link_tbl[edesc->src_nents + 1];
14914de9d0b5SLee Nipper 
149281eb024cSKim Phillips 		to_talitos_ptr(&desc->ptr[4], edesc->dma_link_tbl +
149381eb024cSKim Phillips 					      (edesc->src_nents + 1) *
149481eb024cSKim Phillips 					      sizeof(struct talitos_ptr));
14954de9d0b5SLee Nipper 		desc->ptr[4].j_extent |= DESC_PTR_LNKTBL_JUMP;
14964de9d0b5SLee Nipper 		sg_count = sg_to_link_tbl(areq->dst, sg_count, cryptlen,
14974de9d0b5SLee Nipper 					  link_tbl_ptr);
14984de9d0b5SLee Nipper 		dma_sync_single_for_device(ctx->dev, edesc->dma_link_tbl,
14994de9d0b5SLee Nipper 					   edesc->dma_len, DMA_BIDIRECTIONAL);
15004de9d0b5SLee Nipper 	}
15014de9d0b5SLee Nipper 
15024de9d0b5SLee Nipper 	/* iv out */
15034de9d0b5SLee Nipper 	map_single_talitos_ptr(dev, &desc->ptr[5], ivsize, ctx->iv, 0,
15044de9d0b5SLee Nipper 			       DMA_FROM_DEVICE);
15054de9d0b5SLee Nipper 
15064de9d0b5SLee Nipper 	/* last DWORD empty */
15074de9d0b5SLee Nipper 	desc->ptr[6].len = 0;
150881eb024cSKim Phillips 	to_talitos_ptr(&desc->ptr[6], 0);
15094de9d0b5SLee Nipper 	desc->ptr[6].j_extent = 0;
15104de9d0b5SLee Nipper 
15115228f0f7SKim Phillips 	ret = talitos_submit(dev, ctx->ch, desc, callback, areq);
15124de9d0b5SLee Nipper 	if (ret != -EINPROGRESS) {
15134de9d0b5SLee Nipper 		common_nonsnoop_unmap(dev, edesc, areq);
15144de9d0b5SLee Nipper 		kfree(edesc);
15154de9d0b5SLee Nipper 	}
15164de9d0b5SLee Nipper 	return ret;
15174de9d0b5SLee Nipper }
15184de9d0b5SLee Nipper 
1519e938e465SKim Phillips static struct talitos_edesc *ablkcipher_edesc_alloc(struct ablkcipher_request *
1520e938e465SKim Phillips 						    areq)
15214de9d0b5SLee Nipper {
15224de9d0b5SLee Nipper 	struct crypto_ablkcipher *cipher = crypto_ablkcipher_reqtfm(areq);
15234de9d0b5SLee Nipper 	struct talitos_ctx *ctx = crypto_ablkcipher_ctx(cipher);
1524*79fd31d3SHoria Geanta 	unsigned int ivsize = crypto_ablkcipher_ivsize(cipher);
15254de9d0b5SLee Nipper 
1526*79fd31d3SHoria Geanta 	return talitos_edesc_alloc(ctx->dev, NULL, areq->src, areq->dst,
1527*79fd31d3SHoria Geanta 				   areq->info, 0, areq->nbytes, 0, ivsize, 0,
1528*79fd31d3SHoria Geanta 				   areq->base.flags);
15294de9d0b5SLee Nipper }
15304de9d0b5SLee Nipper 
15314de9d0b5SLee Nipper static int ablkcipher_encrypt(struct ablkcipher_request *areq)
15324de9d0b5SLee Nipper {
15334de9d0b5SLee Nipper 	struct crypto_ablkcipher *cipher = crypto_ablkcipher_reqtfm(areq);
15344de9d0b5SLee Nipper 	struct talitos_ctx *ctx = crypto_ablkcipher_ctx(cipher);
15354de9d0b5SLee Nipper 	struct talitos_edesc *edesc;
15364de9d0b5SLee Nipper 
15374de9d0b5SLee Nipper 	/* allocate extended descriptor */
15384de9d0b5SLee Nipper 	edesc = ablkcipher_edesc_alloc(areq);
15394de9d0b5SLee Nipper 	if (IS_ERR(edesc))
15404de9d0b5SLee Nipper 		return PTR_ERR(edesc);
15414de9d0b5SLee Nipper 
15424de9d0b5SLee Nipper 	/* set encrypt */
15434de9d0b5SLee Nipper 	edesc->desc.hdr = ctx->desc_hdr_template | DESC_HDR_MODE0_ENCRYPT;
15444de9d0b5SLee Nipper 
1545febec542SKim Phillips 	return common_nonsnoop(edesc, areq, ablkcipher_done);
15464de9d0b5SLee Nipper }
15474de9d0b5SLee Nipper 
15484de9d0b5SLee Nipper static int ablkcipher_decrypt(struct ablkcipher_request *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 	struct talitos_edesc *edesc;
15534de9d0b5SLee Nipper 
15544de9d0b5SLee Nipper 	/* allocate extended descriptor */
15554de9d0b5SLee Nipper 	edesc = ablkcipher_edesc_alloc(areq);
15564de9d0b5SLee Nipper 	if (IS_ERR(edesc))
15574de9d0b5SLee Nipper 		return PTR_ERR(edesc);
15584de9d0b5SLee Nipper 
15594de9d0b5SLee Nipper 	edesc->desc.hdr = ctx->desc_hdr_template | DESC_HDR_DIR_INBOUND;
15604de9d0b5SLee Nipper 
1561febec542SKim Phillips 	return common_nonsnoop(edesc, areq, ablkcipher_done);
15624de9d0b5SLee Nipper }
15634de9d0b5SLee Nipper 
1564497f2e6bSLee Nipper static void common_nonsnoop_hash_unmap(struct device *dev,
1565497f2e6bSLee Nipper 				       struct talitos_edesc *edesc,
1566497f2e6bSLee Nipper 				       struct ahash_request *areq)
1567497f2e6bSLee Nipper {
1568497f2e6bSLee Nipper 	struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1569497f2e6bSLee Nipper 
1570497f2e6bSLee Nipper 	unmap_single_talitos_ptr(dev, &edesc->desc.ptr[5], DMA_FROM_DEVICE);
1571497f2e6bSLee Nipper 
1572497f2e6bSLee Nipper 	/* When using hashctx-in, must unmap it. */
1573497f2e6bSLee Nipper 	if (edesc->desc.ptr[1].len)
1574497f2e6bSLee Nipper 		unmap_single_talitos_ptr(dev, &edesc->desc.ptr[1],
1575497f2e6bSLee Nipper 					 DMA_TO_DEVICE);
1576497f2e6bSLee Nipper 
1577497f2e6bSLee Nipper 	if (edesc->desc.ptr[2].len)
1578497f2e6bSLee Nipper 		unmap_single_talitos_ptr(dev, &edesc->desc.ptr[2],
1579497f2e6bSLee Nipper 					 DMA_TO_DEVICE);
1580497f2e6bSLee Nipper 
1581497f2e6bSLee Nipper 	talitos_sg_unmap(dev, edesc, req_ctx->psrc, NULL);
1582497f2e6bSLee Nipper 
1583497f2e6bSLee Nipper 	if (edesc->dma_len)
1584497f2e6bSLee Nipper 		dma_unmap_single(dev, edesc->dma_link_tbl, edesc->dma_len,
1585497f2e6bSLee Nipper 				 DMA_BIDIRECTIONAL);
1586497f2e6bSLee Nipper 
1587497f2e6bSLee Nipper }
1588497f2e6bSLee Nipper 
1589497f2e6bSLee Nipper static void ahash_done(struct device *dev,
1590497f2e6bSLee Nipper 		       struct talitos_desc *desc, void *context,
1591497f2e6bSLee Nipper 		       int err)
1592497f2e6bSLee Nipper {
1593497f2e6bSLee Nipper 	struct ahash_request *areq = context;
1594497f2e6bSLee Nipper 	struct talitos_edesc *edesc =
1595497f2e6bSLee Nipper 		 container_of(desc, struct talitos_edesc, desc);
1596497f2e6bSLee Nipper 	struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1597497f2e6bSLee Nipper 
1598497f2e6bSLee Nipper 	if (!req_ctx->last && req_ctx->to_hash_later) {
1599497f2e6bSLee Nipper 		/* Position any partial block for next update/final/finup */
1600497f2e6bSLee Nipper 		memcpy(req_ctx->buf, req_ctx->bufnext, req_ctx->to_hash_later);
16015e833bc4SLee Nipper 		req_ctx->nbuf = req_ctx->to_hash_later;
1602497f2e6bSLee Nipper 	}
1603497f2e6bSLee Nipper 	common_nonsnoop_hash_unmap(dev, edesc, areq);
1604497f2e6bSLee Nipper 
1605497f2e6bSLee Nipper 	kfree(edesc);
1606497f2e6bSLee Nipper 
1607497f2e6bSLee Nipper 	areq->base.complete(&areq->base, err);
1608497f2e6bSLee Nipper }
1609497f2e6bSLee Nipper 
1610497f2e6bSLee Nipper static int common_nonsnoop_hash(struct talitos_edesc *edesc,
1611497f2e6bSLee Nipper 				struct ahash_request *areq, unsigned int length,
1612497f2e6bSLee Nipper 				void (*callback) (struct device *dev,
1613497f2e6bSLee Nipper 						  struct talitos_desc *desc,
1614497f2e6bSLee Nipper 						  void *context, int error))
1615497f2e6bSLee Nipper {
1616497f2e6bSLee Nipper 	struct crypto_ahash *tfm = crypto_ahash_reqtfm(areq);
1617497f2e6bSLee Nipper 	struct talitos_ctx *ctx = crypto_ahash_ctx(tfm);
1618497f2e6bSLee Nipper 	struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1619497f2e6bSLee Nipper 	struct device *dev = ctx->dev;
1620497f2e6bSLee Nipper 	struct talitos_desc *desc = &edesc->desc;
1621497f2e6bSLee Nipper 	int sg_count, ret;
1622497f2e6bSLee Nipper 
1623497f2e6bSLee Nipper 	/* first DWORD empty */
1624497f2e6bSLee Nipper 	desc->ptr[0] = zero_entry;
1625497f2e6bSLee Nipper 
162660f208d7SKim Phillips 	/* hash context in */
162760f208d7SKim Phillips 	if (!req_ctx->first || req_ctx->swinit) {
1628497f2e6bSLee Nipper 		map_single_talitos_ptr(dev, &desc->ptr[1],
1629497f2e6bSLee Nipper 				       req_ctx->hw_context_size,
1630497f2e6bSLee Nipper 				       (char *)req_ctx->hw_context, 0,
1631497f2e6bSLee Nipper 				       DMA_TO_DEVICE);
163260f208d7SKim Phillips 		req_ctx->swinit = 0;
1633497f2e6bSLee Nipper 	} else {
1634497f2e6bSLee Nipper 		desc->ptr[1] = zero_entry;
1635497f2e6bSLee Nipper 		/* Indicate next op is not the first. */
1636497f2e6bSLee Nipper 		req_ctx->first = 0;
1637497f2e6bSLee Nipper 	}
1638497f2e6bSLee Nipper 
1639497f2e6bSLee Nipper 	/* HMAC key */
1640497f2e6bSLee Nipper 	if (ctx->keylen)
1641497f2e6bSLee Nipper 		map_single_talitos_ptr(dev, &desc->ptr[2], ctx->keylen,
1642497f2e6bSLee Nipper 				       (char *)&ctx->key, 0, DMA_TO_DEVICE);
1643497f2e6bSLee Nipper 	else
1644497f2e6bSLee Nipper 		desc->ptr[2] = zero_entry;
1645497f2e6bSLee Nipper 
1646497f2e6bSLee Nipper 	/*
1647497f2e6bSLee Nipper 	 * data in
1648497f2e6bSLee Nipper 	 */
1649497f2e6bSLee Nipper 	desc->ptr[3].len = cpu_to_be16(length);
1650497f2e6bSLee Nipper 	desc->ptr[3].j_extent = 0;
1651497f2e6bSLee Nipper 
1652497f2e6bSLee Nipper 	sg_count = talitos_map_sg(dev, req_ctx->psrc,
1653497f2e6bSLee Nipper 				  edesc->src_nents ? : 1,
16542a1cfe46SHoria Geanta 				  DMA_TO_DEVICE, edesc->src_chained);
1655497f2e6bSLee Nipper 
1656497f2e6bSLee Nipper 	if (sg_count == 1) {
1657497f2e6bSLee Nipper 		to_talitos_ptr(&desc->ptr[3], sg_dma_address(req_ctx->psrc));
1658497f2e6bSLee Nipper 	} else {
1659497f2e6bSLee Nipper 		sg_count = sg_to_link_tbl(req_ctx->psrc, sg_count, length,
1660497f2e6bSLee Nipper 					  &edesc->link_tbl[0]);
1661497f2e6bSLee Nipper 		if (sg_count > 1) {
1662497f2e6bSLee Nipper 			desc->ptr[3].j_extent |= DESC_PTR_LNKTBL_JUMP;
1663497f2e6bSLee Nipper 			to_talitos_ptr(&desc->ptr[3], edesc->dma_link_tbl);
1664497f2e6bSLee Nipper 			dma_sync_single_for_device(ctx->dev,
1665497f2e6bSLee Nipper 						   edesc->dma_link_tbl,
1666497f2e6bSLee Nipper 						   edesc->dma_len,
1667497f2e6bSLee Nipper 						   DMA_BIDIRECTIONAL);
1668497f2e6bSLee Nipper 		} else {
1669497f2e6bSLee Nipper 			/* Only one segment now, so no link tbl needed */
1670497f2e6bSLee Nipper 			to_talitos_ptr(&desc->ptr[3],
1671497f2e6bSLee Nipper 				       sg_dma_address(req_ctx->psrc));
1672497f2e6bSLee Nipper 		}
1673497f2e6bSLee Nipper 	}
1674497f2e6bSLee Nipper 
1675497f2e6bSLee Nipper 	/* fifth DWORD empty */
1676497f2e6bSLee Nipper 	desc->ptr[4] = zero_entry;
1677497f2e6bSLee Nipper 
1678497f2e6bSLee Nipper 	/* hash/HMAC out -or- hash context out */
1679497f2e6bSLee Nipper 	if (req_ctx->last)
1680497f2e6bSLee Nipper 		map_single_talitos_ptr(dev, &desc->ptr[5],
1681497f2e6bSLee Nipper 				       crypto_ahash_digestsize(tfm),
1682497f2e6bSLee Nipper 				       areq->result, 0, DMA_FROM_DEVICE);
1683497f2e6bSLee Nipper 	else
1684497f2e6bSLee Nipper 		map_single_talitos_ptr(dev, &desc->ptr[5],
1685497f2e6bSLee Nipper 				       req_ctx->hw_context_size,
1686497f2e6bSLee Nipper 				       req_ctx->hw_context, 0, DMA_FROM_DEVICE);
1687497f2e6bSLee Nipper 
1688497f2e6bSLee Nipper 	/* last DWORD empty */
1689497f2e6bSLee Nipper 	desc->ptr[6] = zero_entry;
1690497f2e6bSLee Nipper 
16915228f0f7SKim Phillips 	ret = talitos_submit(dev, ctx->ch, desc, callback, areq);
1692497f2e6bSLee Nipper 	if (ret != -EINPROGRESS) {
1693497f2e6bSLee Nipper 		common_nonsnoop_hash_unmap(dev, edesc, areq);
1694497f2e6bSLee Nipper 		kfree(edesc);
1695497f2e6bSLee Nipper 	}
1696497f2e6bSLee Nipper 	return ret;
1697497f2e6bSLee Nipper }
1698497f2e6bSLee Nipper 
1699497f2e6bSLee Nipper static struct talitos_edesc *ahash_edesc_alloc(struct ahash_request *areq,
1700497f2e6bSLee Nipper 					       unsigned int nbytes)
1701497f2e6bSLee Nipper {
1702497f2e6bSLee Nipper 	struct crypto_ahash *tfm = crypto_ahash_reqtfm(areq);
1703497f2e6bSLee Nipper 	struct talitos_ctx *ctx = crypto_ahash_ctx(tfm);
1704497f2e6bSLee Nipper 	struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1705497f2e6bSLee Nipper 
1706*79fd31d3SHoria Geanta 	return talitos_edesc_alloc(ctx->dev, NULL, req_ctx->psrc, NULL, NULL, 0,
1707*79fd31d3SHoria Geanta 				   nbytes, 0, 0, 0, areq->base.flags);
1708497f2e6bSLee Nipper }
1709497f2e6bSLee Nipper 
1710497f2e6bSLee Nipper static int ahash_init(struct ahash_request *areq)
1711497f2e6bSLee Nipper {
1712497f2e6bSLee Nipper 	struct crypto_ahash *tfm = crypto_ahash_reqtfm(areq);
1713497f2e6bSLee Nipper 	struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1714497f2e6bSLee Nipper 
1715497f2e6bSLee Nipper 	/* Initialize the context */
17165e833bc4SLee Nipper 	req_ctx->nbuf = 0;
171760f208d7SKim Phillips 	req_ctx->first = 1; /* first indicates h/w must init its context */
171860f208d7SKim Phillips 	req_ctx->swinit = 0; /* assume h/w init of context */
1719497f2e6bSLee Nipper 	req_ctx->hw_context_size =
1720497f2e6bSLee Nipper 		(crypto_ahash_digestsize(tfm) <= SHA256_DIGEST_SIZE)
1721497f2e6bSLee Nipper 			? TALITOS_MDEU_CONTEXT_SIZE_MD5_SHA1_SHA256
1722497f2e6bSLee Nipper 			: TALITOS_MDEU_CONTEXT_SIZE_SHA384_SHA512;
1723497f2e6bSLee Nipper 
1724497f2e6bSLee Nipper 	return 0;
1725497f2e6bSLee Nipper }
1726497f2e6bSLee Nipper 
172760f208d7SKim Phillips /*
172860f208d7SKim Phillips  * on h/w without explicit sha224 support, we initialize h/w context
172960f208d7SKim Phillips  * manually with sha224 constants, and tell it to run sha256.
173060f208d7SKim Phillips  */
173160f208d7SKim Phillips static int ahash_init_sha224_swinit(struct ahash_request *areq)
173260f208d7SKim Phillips {
173360f208d7SKim Phillips 	struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
173460f208d7SKim Phillips 
173560f208d7SKim Phillips 	ahash_init(areq);
173660f208d7SKim Phillips 	req_ctx->swinit = 1;/* prevent h/w initting context with sha256 values*/
173760f208d7SKim Phillips 
1738a752447aSKim Phillips 	req_ctx->hw_context[0] = SHA224_H0;
1739a752447aSKim Phillips 	req_ctx->hw_context[1] = SHA224_H1;
1740a752447aSKim Phillips 	req_ctx->hw_context[2] = SHA224_H2;
1741a752447aSKim Phillips 	req_ctx->hw_context[3] = SHA224_H3;
1742a752447aSKim Phillips 	req_ctx->hw_context[4] = SHA224_H4;
1743a752447aSKim Phillips 	req_ctx->hw_context[5] = SHA224_H5;
1744a752447aSKim Phillips 	req_ctx->hw_context[6] = SHA224_H6;
1745a752447aSKim Phillips 	req_ctx->hw_context[7] = SHA224_H7;
174660f208d7SKim Phillips 
174760f208d7SKim Phillips 	/* init 64-bit count */
174860f208d7SKim Phillips 	req_ctx->hw_context[8] = 0;
174960f208d7SKim Phillips 	req_ctx->hw_context[9] = 0;
175060f208d7SKim Phillips 
175160f208d7SKim Phillips 	return 0;
175260f208d7SKim Phillips }
175360f208d7SKim Phillips 
1754497f2e6bSLee Nipper static int ahash_process_req(struct ahash_request *areq, unsigned int nbytes)
1755497f2e6bSLee Nipper {
1756497f2e6bSLee Nipper 	struct crypto_ahash *tfm = crypto_ahash_reqtfm(areq);
1757497f2e6bSLee Nipper 	struct talitos_ctx *ctx = crypto_ahash_ctx(tfm);
1758497f2e6bSLee Nipper 	struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1759497f2e6bSLee Nipper 	struct talitos_edesc *edesc;
1760497f2e6bSLee Nipper 	unsigned int blocksize =
1761497f2e6bSLee Nipper 			crypto_tfm_alg_blocksize(crypto_ahash_tfm(tfm));
1762497f2e6bSLee Nipper 	unsigned int nbytes_to_hash;
1763497f2e6bSLee Nipper 	unsigned int to_hash_later;
17645e833bc4SLee Nipper 	unsigned int nsg;
17652a1cfe46SHoria Geanta 	bool chained;
1766497f2e6bSLee Nipper 
17675e833bc4SLee Nipper 	if (!req_ctx->last && (nbytes + req_ctx->nbuf <= blocksize)) {
17685e833bc4SLee Nipper 		/* Buffer up to one whole block */
1769497f2e6bSLee Nipper 		sg_copy_to_buffer(areq->src,
1770497f2e6bSLee Nipper 				  sg_count(areq->src, nbytes, &chained),
17715e833bc4SLee Nipper 				  req_ctx->buf + req_ctx->nbuf, nbytes);
17725e833bc4SLee Nipper 		req_ctx->nbuf += nbytes;
1773497f2e6bSLee Nipper 		return 0;
1774497f2e6bSLee Nipper 	}
1775497f2e6bSLee Nipper 
17765e833bc4SLee Nipper 	/* At least (blocksize + 1) bytes are available to hash */
17775e833bc4SLee Nipper 	nbytes_to_hash = nbytes + req_ctx->nbuf;
17785e833bc4SLee Nipper 	to_hash_later = nbytes_to_hash & (blocksize - 1);
17795e833bc4SLee Nipper 
17805e833bc4SLee Nipper 	if (req_ctx->last)
17815e833bc4SLee Nipper 		to_hash_later = 0;
17825e833bc4SLee Nipper 	else if (to_hash_later)
17835e833bc4SLee Nipper 		/* There is a partial block. Hash the full block(s) now */
17845e833bc4SLee Nipper 		nbytes_to_hash -= to_hash_later;
17855e833bc4SLee Nipper 	else {
17865e833bc4SLee Nipper 		/* Keep one block buffered */
17875e833bc4SLee Nipper 		nbytes_to_hash -= blocksize;
17885e833bc4SLee Nipper 		to_hash_later = blocksize;
1789497f2e6bSLee Nipper 	}
17905e833bc4SLee Nipper 
17915e833bc4SLee Nipper 	/* Chain in any previously buffered data */
17925e833bc4SLee Nipper 	if (req_ctx->nbuf) {
17935e833bc4SLee Nipper 		nsg = (req_ctx->nbuf < nbytes_to_hash) ? 2 : 1;
17945e833bc4SLee Nipper 		sg_init_table(req_ctx->bufsl, nsg);
17955e833bc4SLee Nipper 		sg_set_buf(req_ctx->bufsl, req_ctx->buf, req_ctx->nbuf);
17965e833bc4SLee Nipper 		if (nsg > 1)
17975e833bc4SLee Nipper 			scatterwalk_sg_chain(req_ctx->bufsl, 2, areq->src);
17985e833bc4SLee Nipper 		req_ctx->psrc = req_ctx->bufsl;
17995e833bc4SLee Nipper 	} else
18005e833bc4SLee Nipper 		req_ctx->psrc = areq->src;
18015e833bc4SLee Nipper 
1802497f2e6bSLee Nipper 	if (to_hash_later) {
18035e833bc4SLee Nipper 		int nents = sg_count(areq->src, nbytes, &chained);
1804497f2e6bSLee Nipper 		sg_copy_end_to_buffer(areq->src, nents,
1805497f2e6bSLee Nipper 				      req_ctx->bufnext,
1806497f2e6bSLee Nipper 				      to_hash_later,
1807497f2e6bSLee Nipper 				      nbytes - to_hash_later);
1808497f2e6bSLee Nipper 	}
1809497f2e6bSLee Nipper 	req_ctx->to_hash_later = to_hash_later;
1810497f2e6bSLee Nipper 
18115e833bc4SLee Nipper 	/* Allocate extended descriptor */
1812497f2e6bSLee Nipper 	edesc = ahash_edesc_alloc(areq, nbytes_to_hash);
1813497f2e6bSLee Nipper 	if (IS_ERR(edesc))
1814497f2e6bSLee Nipper 		return PTR_ERR(edesc);
1815497f2e6bSLee Nipper 
1816497f2e6bSLee Nipper 	edesc->desc.hdr = ctx->desc_hdr_template;
1817497f2e6bSLee Nipper 
1818497f2e6bSLee Nipper 	/* On last one, request SEC to pad; otherwise continue */
1819497f2e6bSLee Nipper 	if (req_ctx->last)
1820497f2e6bSLee Nipper 		edesc->desc.hdr |= DESC_HDR_MODE0_MDEU_PAD;
1821497f2e6bSLee Nipper 	else
1822497f2e6bSLee Nipper 		edesc->desc.hdr |= DESC_HDR_MODE0_MDEU_CONT;
1823497f2e6bSLee Nipper 
182460f208d7SKim Phillips 	/* request SEC to INIT hash. */
182560f208d7SKim Phillips 	if (req_ctx->first && !req_ctx->swinit)
1826497f2e6bSLee Nipper 		edesc->desc.hdr |= DESC_HDR_MODE0_MDEU_INIT;
1827497f2e6bSLee Nipper 
1828497f2e6bSLee Nipper 	/* When the tfm context has a keylen, it's an HMAC.
1829497f2e6bSLee Nipper 	 * A first or last (ie. not middle) descriptor must request HMAC.
1830497f2e6bSLee Nipper 	 */
1831497f2e6bSLee Nipper 	if (ctx->keylen && (req_ctx->first || req_ctx->last))
1832497f2e6bSLee Nipper 		edesc->desc.hdr |= DESC_HDR_MODE0_MDEU_HMAC;
1833497f2e6bSLee Nipper 
1834497f2e6bSLee Nipper 	return common_nonsnoop_hash(edesc, areq, nbytes_to_hash,
1835497f2e6bSLee Nipper 				    ahash_done);
1836497f2e6bSLee Nipper }
1837497f2e6bSLee Nipper 
1838497f2e6bSLee Nipper static int ahash_update(struct ahash_request *areq)
1839497f2e6bSLee Nipper {
1840497f2e6bSLee Nipper 	struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1841497f2e6bSLee Nipper 
1842497f2e6bSLee Nipper 	req_ctx->last = 0;
1843497f2e6bSLee Nipper 
1844497f2e6bSLee Nipper 	return ahash_process_req(areq, areq->nbytes);
1845497f2e6bSLee Nipper }
1846497f2e6bSLee Nipper 
1847497f2e6bSLee Nipper static int ahash_final(struct ahash_request *areq)
1848497f2e6bSLee Nipper {
1849497f2e6bSLee Nipper 	struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1850497f2e6bSLee Nipper 
1851497f2e6bSLee Nipper 	req_ctx->last = 1;
1852497f2e6bSLee Nipper 
1853497f2e6bSLee Nipper 	return ahash_process_req(areq, 0);
1854497f2e6bSLee Nipper }
1855497f2e6bSLee Nipper 
1856497f2e6bSLee Nipper static int ahash_finup(struct ahash_request *areq)
1857497f2e6bSLee Nipper {
1858497f2e6bSLee Nipper 	struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1859497f2e6bSLee Nipper 
1860497f2e6bSLee Nipper 	req_ctx->last = 1;
1861497f2e6bSLee Nipper 
1862497f2e6bSLee Nipper 	return ahash_process_req(areq, areq->nbytes);
1863497f2e6bSLee Nipper }
1864497f2e6bSLee Nipper 
1865497f2e6bSLee Nipper static int ahash_digest(struct ahash_request *areq)
1866497f2e6bSLee Nipper {
1867497f2e6bSLee Nipper 	struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
186860f208d7SKim Phillips 	struct crypto_ahash *ahash = crypto_ahash_reqtfm(areq);
1869497f2e6bSLee Nipper 
187060f208d7SKim Phillips 	ahash->init(areq);
1871497f2e6bSLee Nipper 	req_ctx->last = 1;
1872497f2e6bSLee Nipper 
1873497f2e6bSLee Nipper 	return ahash_process_req(areq, areq->nbytes);
1874497f2e6bSLee Nipper }
1875497f2e6bSLee Nipper 
187679b3a418SLee Nipper struct keyhash_result {
187779b3a418SLee Nipper 	struct completion completion;
187879b3a418SLee Nipper 	int err;
187979b3a418SLee Nipper };
188079b3a418SLee Nipper 
188179b3a418SLee Nipper static void keyhash_complete(struct crypto_async_request *req, int err)
188279b3a418SLee Nipper {
188379b3a418SLee Nipper 	struct keyhash_result *res = req->data;
188479b3a418SLee Nipper 
188579b3a418SLee Nipper 	if (err == -EINPROGRESS)
188679b3a418SLee Nipper 		return;
188779b3a418SLee Nipper 
188879b3a418SLee Nipper 	res->err = err;
188979b3a418SLee Nipper 	complete(&res->completion);
189079b3a418SLee Nipper }
189179b3a418SLee Nipper 
189279b3a418SLee Nipper static int keyhash(struct crypto_ahash *tfm, const u8 *key, unsigned int keylen,
189379b3a418SLee Nipper 		   u8 *hash)
189479b3a418SLee Nipper {
189579b3a418SLee Nipper 	struct talitos_ctx *ctx = crypto_tfm_ctx(crypto_ahash_tfm(tfm));
189679b3a418SLee Nipper 
189779b3a418SLee Nipper 	struct scatterlist sg[1];
189879b3a418SLee Nipper 	struct ahash_request *req;
189979b3a418SLee Nipper 	struct keyhash_result hresult;
190079b3a418SLee Nipper 	int ret;
190179b3a418SLee Nipper 
190279b3a418SLee Nipper 	init_completion(&hresult.completion);
190379b3a418SLee Nipper 
190479b3a418SLee Nipper 	req = ahash_request_alloc(tfm, GFP_KERNEL);
190579b3a418SLee Nipper 	if (!req)
190679b3a418SLee Nipper 		return -ENOMEM;
190779b3a418SLee Nipper 
190879b3a418SLee Nipper 	/* Keep tfm keylen == 0 during hash of the long key */
190979b3a418SLee Nipper 	ctx->keylen = 0;
191079b3a418SLee Nipper 	ahash_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
191179b3a418SLee Nipper 				   keyhash_complete, &hresult);
191279b3a418SLee Nipper 
191379b3a418SLee Nipper 	sg_init_one(&sg[0], key, keylen);
191479b3a418SLee Nipper 
191579b3a418SLee Nipper 	ahash_request_set_crypt(req, sg, hash, keylen);
191679b3a418SLee Nipper 	ret = crypto_ahash_digest(req);
191779b3a418SLee Nipper 	switch (ret) {
191879b3a418SLee Nipper 	case 0:
191979b3a418SLee Nipper 		break;
192079b3a418SLee Nipper 	case -EINPROGRESS:
192179b3a418SLee Nipper 	case -EBUSY:
192279b3a418SLee Nipper 		ret = wait_for_completion_interruptible(
192379b3a418SLee Nipper 			&hresult.completion);
192479b3a418SLee Nipper 		if (!ret)
192579b3a418SLee Nipper 			ret = hresult.err;
192679b3a418SLee Nipper 		break;
192779b3a418SLee Nipper 	default:
192879b3a418SLee Nipper 		break;
192979b3a418SLee Nipper 	}
193079b3a418SLee Nipper 	ahash_request_free(req);
193179b3a418SLee Nipper 
193279b3a418SLee Nipper 	return ret;
193379b3a418SLee Nipper }
193479b3a418SLee Nipper 
193579b3a418SLee Nipper static int ahash_setkey(struct crypto_ahash *tfm, const u8 *key,
193679b3a418SLee Nipper 			unsigned int keylen)
193779b3a418SLee Nipper {
193879b3a418SLee Nipper 	struct talitos_ctx *ctx = crypto_tfm_ctx(crypto_ahash_tfm(tfm));
193979b3a418SLee Nipper 	unsigned int blocksize =
194079b3a418SLee Nipper 			crypto_tfm_alg_blocksize(crypto_ahash_tfm(tfm));
194179b3a418SLee Nipper 	unsigned int digestsize = crypto_ahash_digestsize(tfm);
194279b3a418SLee Nipper 	unsigned int keysize = keylen;
194379b3a418SLee Nipper 	u8 hash[SHA512_DIGEST_SIZE];
194479b3a418SLee Nipper 	int ret;
194579b3a418SLee Nipper 
194679b3a418SLee Nipper 	if (keylen <= blocksize)
194779b3a418SLee Nipper 		memcpy(ctx->key, key, keysize);
194879b3a418SLee Nipper 	else {
194979b3a418SLee Nipper 		/* Must get the hash of the long key */
195079b3a418SLee Nipper 		ret = keyhash(tfm, key, keylen, hash);
195179b3a418SLee Nipper 
195279b3a418SLee Nipper 		if (ret) {
195379b3a418SLee Nipper 			crypto_ahash_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
195479b3a418SLee Nipper 			return -EINVAL;
195579b3a418SLee Nipper 		}
195679b3a418SLee Nipper 
195779b3a418SLee Nipper 		keysize = digestsize;
195879b3a418SLee Nipper 		memcpy(ctx->key, hash, digestsize);
195979b3a418SLee Nipper 	}
196079b3a418SLee Nipper 
196179b3a418SLee Nipper 	ctx->keylen = keysize;
196279b3a418SLee Nipper 
196379b3a418SLee Nipper 	return 0;
196479b3a418SLee Nipper }
196579b3a418SLee Nipper 
196679b3a418SLee Nipper 
19679c4a7965SKim Phillips struct talitos_alg_template {
1968d5e4aaefSLee Nipper 	u32 type;
1969d5e4aaefSLee Nipper 	union {
1970d5e4aaefSLee Nipper 		struct crypto_alg crypto;
1971acbf7c62SLee Nipper 		struct ahash_alg hash;
1972d5e4aaefSLee Nipper 	} alg;
19739c4a7965SKim Phillips 	__be32 desc_hdr_template;
19749c4a7965SKim Phillips };
19759c4a7965SKim Phillips 
19769c4a7965SKim Phillips static struct talitos_alg_template driver_algs[] = {
197756af8cd4SLee Nipper 	/* AEAD algorithms.  These use a single-pass ipsec_esp descriptor */
1978d5e4aaefSLee Nipper 	{	.type = CRYPTO_ALG_TYPE_AEAD,
1979d5e4aaefSLee Nipper 		.alg.crypto = {
198056af8cd4SLee Nipper 			.cra_name = "authenc(hmac(sha1),cbc(aes))",
198156af8cd4SLee Nipper 			.cra_driver_name = "authenc-hmac-sha1-cbc-aes-talitos",
198256af8cd4SLee Nipper 			.cra_blocksize = AES_BLOCK_SIZE,
198356af8cd4SLee Nipper 			.cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
198456af8cd4SLee Nipper 			.cra_aead = {
19853952f17eSLee Nipper 				.ivsize = AES_BLOCK_SIZE,
19863952f17eSLee Nipper 				.maxauthsize = SHA1_DIGEST_SIZE,
198756af8cd4SLee Nipper 			}
19889c4a7965SKim Phillips 		},
19899c4a7965SKim Phillips 		.desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
19909c4a7965SKim Phillips 			             DESC_HDR_SEL0_AESU |
19919c4a7965SKim Phillips 		                     DESC_HDR_MODE0_AESU_CBC |
19929c4a7965SKim Phillips 		                     DESC_HDR_SEL1_MDEUA |
19939c4a7965SKim Phillips 		                     DESC_HDR_MODE1_MDEU_INIT |
19949c4a7965SKim Phillips 		                     DESC_HDR_MODE1_MDEU_PAD |
19959c4a7965SKim Phillips 		                     DESC_HDR_MODE1_MDEU_SHA1_HMAC,
199670bcaca7SLee Nipper 	},
1997d5e4aaefSLee Nipper 	{	.type = CRYPTO_ALG_TYPE_AEAD,
1998d5e4aaefSLee Nipper 		.alg.crypto = {
199956af8cd4SLee Nipper 			.cra_name = "authenc(hmac(sha1),cbc(des3_ede))",
200056af8cd4SLee Nipper 			.cra_driver_name = "authenc-hmac-sha1-cbc-3des-talitos",
200156af8cd4SLee Nipper 			.cra_blocksize = DES3_EDE_BLOCK_SIZE,
200256af8cd4SLee Nipper 			.cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
200356af8cd4SLee Nipper 			.cra_aead = {
20043952f17eSLee Nipper 				.ivsize = DES3_EDE_BLOCK_SIZE,
20053952f17eSLee Nipper 				.maxauthsize = SHA1_DIGEST_SIZE,
200656af8cd4SLee Nipper 			}
200770bcaca7SLee Nipper 		},
200870bcaca7SLee Nipper 		.desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
200970bcaca7SLee Nipper 			             DESC_HDR_SEL0_DEU |
201070bcaca7SLee Nipper 		                     DESC_HDR_MODE0_DEU_CBC |
201170bcaca7SLee Nipper 		                     DESC_HDR_MODE0_DEU_3DES |
201270bcaca7SLee Nipper 		                     DESC_HDR_SEL1_MDEUA |
201370bcaca7SLee Nipper 		                     DESC_HDR_MODE1_MDEU_INIT |
201470bcaca7SLee Nipper 		                     DESC_HDR_MODE1_MDEU_PAD |
201570bcaca7SLee Nipper 		                     DESC_HDR_MODE1_MDEU_SHA1_HMAC,
20163952f17eSLee Nipper 	},
2017d5e4aaefSLee Nipper 	{       .type = CRYPTO_ALG_TYPE_AEAD,
2018d5e4aaefSLee Nipper 		.alg.crypto = {
2019357fb605SHoria Geanta 			.cra_name = "authenc(hmac(sha224),cbc(aes))",
2020357fb605SHoria Geanta 			.cra_driver_name = "authenc-hmac-sha224-cbc-aes-talitos",
2021357fb605SHoria Geanta 			.cra_blocksize = AES_BLOCK_SIZE,
2022357fb605SHoria Geanta 			.cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
2023357fb605SHoria Geanta 			.cra_aead = {
2024357fb605SHoria Geanta 				.ivsize = AES_BLOCK_SIZE,
2025357fb605SHoria Geanta 				.maxauthsize = SHA224_DIGEST_SIZE,
2026357fb605SHoria Geanta 			}
2027357fb605SHoria Geanta 		},
2028357fb605SHoria Geanta 		.desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
2029357fb605SHoria Geanta 				     DESC_HDR_SEL0_AESU |
2030357fb605SHoria Geanta 				     DESC_HDR_MODE0_AESU_CBC |
2031357fb605SHoria Geanta 				     DESC_HDR_SEL1_MDEUA |
2032357fb605SHoria Geanta 				     DESC_HDR_MODE1_MDEU_INIT |
2033357fb605SHoria Geanta 				     DESC_HDR_MODE1_MDEU_PAD |
2034357fb605SHoria Geanta 				     DESC_HDR_MODE1_MDEU_SHA224_HMAC,
2035357fb605SHoria Geanta 	},
2036357fb605SHoria Geanta 	{	.type = CRYPTO_ALG_TYPE_AEAD,
2037357fb605SHoria Geanta 		.alg.crypto = {
2038357fb605SHoria Geanta 			.cra_name = "authenc(hmac(sha224),cbc(des3_ede))",
2039357fb605SHoria Geanta 			.cra_driver_name = "authenc-hmac-sha224-cbc-3des-talitos",
2040357fb605SHoria Geanta 			.cra_blocksize = DES3_EDE_BLOCK_SIZE,
2041357fb605SHoria Geanta 			.cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
2042357fb605SHoria Geanta 			.cra_aead = {
2043357fb605SHoria Geanta 				.ivsize = DES3_EDE_BLOCK_SIZE,
2044357fb605SHoria Geanta 				.maxauthsize = SHA224_DIGEST_SIZE,
2045357fb605SHoria Geanta 			}
2046357fb605SHoria Geanta 		},
2047357fb605SHoria Geanta 		.desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
2048357fb605SHoria Geanta 			             DESC_HDR_SEL0_DEU |
2049357fb605SHoria Geanta 		                     DESC_HDR_MODE0_DEU_CBC |
2050357fb605SHoria Geanta 		                     DESC_HDR_MODE0_DEU_3DES |
2051357fb605SHoria Geanta 		                     DESC_HDR_SEL1_MDEUA |
2052357fb605SHoria Geanta 		                     DESC_HDR_MODE1_MDEU_INIT |
2053357fb605SHoria Geanta 		                     DESC_HDR_MODE1_MDEU_PAD |
2054357fb605SHoria Geanta 		                     DESC_HDR_MODE1_MDEU_SHA224_HMAC,
2055357fb605SHoria Geanta 	},
2056357fb605SHoria Geanta 	{	.type = CRYPTO_ALG_TYPE_AEAD,
2057357fb605SHoria Geanta 		.alg.crypto = {
205856af8cd4SLee Nipper 			.cra_name = "authenc(hmac(sha256),cbc(aes))",
205956af8cd4SLee Nipper 			.cra_driver_name = "authenc-hmac-sha256-cbc-aes-talitos",
206056af8cd4SLee Nipper 			.cra_blocksize = AES_BLOCK_SIZE,
206156af8cd4SLee Nipper 			.cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
206256af8cd4SLee Nipper 			.cra_aead = {
20633952f17eSLee Nipper 				.ivsize = AES_BLOCK_SIZE,
20643952f17eSLee Nipper 				.maxauthsize = SHA256_DIGEST_SIZE,
206556af8cd4SLee Nipper 			}
20663952f17eSLee Nipper 		},
20673952f17eSLee Nipper 		.desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
20683952f17eSLee Nipper 			             DESC_HDR_SEL0_AESU |
20693952f17eSLee Nipper 		                     DESC_HDR_MODE0_AESU_CBC |
20703952f17eSLee Nipper 		                     DESC_HDR_SEL1_MDEUA |
20713952f17eSLee Nipper 		                     DESC_HDR_MODE1_MDEU_INIT |
20723952f17eSLee Nipper 		                     DESC_HDR_MODE1_MDEU_PAD |
20733952f17eSLee Nipper 		                     DESC_HDR_MODE1_MDEU_SHA256_HMAC,
20743952f17eSLee Nipper 	},
2075d5e4aaefSLee Nipper 	{	.type = CRYPTO_ALG_TYPE_AEAD,
2076d5e4aaefSLee Nipper 		.alg.crypto = {
207756af8cd4SLee Nipper 			.cra_name = "authenc(hmac(sha256),cbc(des3_ede))",
207856af8cd4SLee Nipper 			.cra_driver_name = "authenc-hmac-sha256-cbc-3des-talitos",
207956af8cd4SLee Nipper 			.cra_blocksize = DES3_EDE_BLOCK_SIZE,
208056af8cd4SLee Nipper 			.cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
208156af8cd4SLee Nipper 			.cra_aead = {
20823952f17eSLee Nipper 				.ivsize = DES3_EDE_BLOCK_SIZE,
20833952f17eSLee Nipper 				.maxauthsize = SHA256_DIGEST_SIZE,
208456af8cd4SLee Nipper 			}
20853952f17eSLee Nipper 		},
20863952f17eSLee Nipper 		.desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
20873952f17eSLee Nipper 			             DESC_HDR_SEL0_DEU |
20883952f17eSLee Nipper 		                     DESC_HDR_MODE0_DEU_CBC |
20893952f17eSLee Nipper 		                     DESC_HDR_MODE0_DEU_3DES |
20903952f17eSLee Nipper 		                     DESC_HDR_SEL1_MDEUA |
20913952f17eSLee Nipper 		                     DESC_HDR_MODE1_MDEU_INIT |
20923952f17eSLee Nipper 		                     DESC_HDR_MODE1_MDEU_PAD |
20933952f17eSLee Nipper 		                     DESC_HDR_MODE1_MDEU_SHA256_HMAC,
20943952f17eSLee Nipper 	},
2095d5e4aaefSLee Nipper 	{	.type = CRYPTO_ALG_TYPE_AEAD,
2096d5e4aaefSLee Nipper 		.alg.crypto = {
2097357fb605SHoria Geanta 			.cra_name = "authenc(hmac(sha384),cbc(aes))",
2098357fb605SHoria Geanta 			.cra_driver_name = "authenc-hmac-sha384-cbc-aes-talitos",
2099357fb605SHoria Geanta 			.cra_blocksize = AES_BLOCK_SIZE,
2100357fb605SHoria Geanta 			.cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
2101357fb605SHoria Geanta 			.cra_aead = {
2102357fb605SHoria Geanta 				.ivsize = AES_BLOCK_SIZE,
2103357fb605SHoria Geanta 				.maxauthsize = SHA384_DIGEST_SIZE,
2104357fb605SHoria Geanta 			}
2105357fb605SHoria Geanta 		},
2106357fb605SHoria Geanta 		.desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
2107357fb605SHoria Geanta 			             DESC_HDR_SEL0_AESU |
2108357fb605SHoria Geanta 		                     DESC_HDR_MODE0_AESU_CBC |
2109357fb605SHoria Geanta 		                     DESC_HDR_SEL1_MDEUB |
2110357fb605SHoria Geanta 		                     DESC_HDR_MODE1_MDEU_INIT |
2111357fb605SHoria Geanta 		                     DESC_HDR_MODE1_MDEU_PAD |
2112357fb605SHoria Geanta 		                     DESC_HDR_MODE1_MDEUB_SHA384_HMAC,
2113357fb605SHoria Geanta 	},
2114357fb605SHoria Geanta 	{	.type = CRYPTO_ALG_TYPE_AEAD,
2115357fb605SHoria Geanta 		.alg.crypto = {
2116357fb605SHoria Geanta 			.cra_name = "authenc(hmac(sha384),cbc(des3_ede))",
2117357fb605SHoria Geanta 			.cra_driver_name = "authenc-hmac-sha384-cbc-3des-talitos",
2118357fb605SHoria Geanta 			.cra_blocksize = DES3_EDE_BLOCK_SIZE,
2119357fb605SHoria Geanta 			.cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
2120357fb605SHoria Geanta 			.cra_aead = {
2121357fb605SHoria Geanta 				.ivsize = DES3_EDE_BLOCK_SIZE,
2122357fb605SHoria Geanta 				.maxauthsize = SHA384_DIGEST_SIZE,
2123357fb605SHoria Geanta 			}
2124357fb605SHoria Geanta 		},
2125357fb605SHoria Geanta 		.desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
2126357fb605SHoria Geanta 			             DESC_HDR_SEL0_DEU |
2127357fb605SHoria Geanta 		                     DESC_HDR_MODE0_DEU_CBC |
2128357fb605SHoria Geanta 		                     DESC_HDR_MODE0_DEU_3DES |
2129357fb605SHoria Geanta 		                     DESC_HDR_SEL1_MDEUB |
2130357fb605SHoria Geanta 		                     DESC_HDR_MODE1_MDEU_INIT |
2131357fb605SHoria Geanta 		                     DESC_HDR_MODE1_MDEU_PAD |
2132357fb605SHoria Geanta 		                     DESC_HDR_MODE1_MDEUB_SHA384_HMAC,
2133357fb605SHoria Geanta 	},
2134357fb605SHoria Geanta 	{	.type = CRYPTO_ALG_TYPE_AEAD,
2135357fb605SHoria Geanta 		.alg.crypto = {
2136357fb605SHoria Geanta 			.cra_name = "authenc(hmac(sha512),cbc(aes))",
2137357fb605SHoria Geanta 			.cra_driver_name = "authenc-hmac-sha512-cbc-aes-talitos",
2138357fb605SHoria Geanta 			.cra_blocksize = AES_BLOCK_SIZE,
2139357fb605SHoria Geanta 			.cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
2140357fb605SHoria Geanta 			.cra_aead = {
2141357fb605SHoria Geanta 				.ivsize = AES_BLOCK_SIZE,
2142357fb605SHoria Geanta 				.maxauthsize = SHA512_DIGEST_SIZE,
2143357fb605SHoria Geanta 			}
2144357fb605SHoria Geanta 		},
2145357fb605SHoria Geanta 		.desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
2146357fb605SHoria Geanta 			             DESC_HDR_SEL0_AESU |
2147357fb605SHoria Geanta 		                     DESC_HDR_MODE0_AESU_CBC |
2148357fb605SHoria Geanta 		                     DESC_HDR_SEL1_MDEUB |
2149357fb605SHoria Geanta 		                     DESC_HDR_MODE1_MDEU_INIT |
2150357fb605SHoria Geanta 		                     DESC_HDR_MODE1_MDEU_PAD |
2151357fb605SHoria Geanta 		                     DESC_HDR_MODE1_MDEUB_SHA512_HMAC,
2152357fb605SHoria Geanta 	},
2153357fb605SHoria Geanta 	{	.type = CRYPTO_ALG_TYPE_AEAD,
2154357fb605SHoria Geanta 		.alg.crypto = {
2155357fb605SHoria Geanta 			.cra_name = "authenc(hmac(sha512),cbc(des3_ede))",
2156357fb605SHoria Geanta 			.cra_driver_name = "authenc-hmac-sha512-cbc-3des-talitos",
2157357fb605SHoria Geanta 			.cra_blocksize = DES3_EDE_BLOCK_SIZE,
2158357fb605SHoria Geanta 			.cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
2159357fb605SHoria Geanta 			.cra_aead = {
2160357fb605SHoria Geanta 				.ivsize = DES3_EDE_BLOCK_SIZE,
2161357fb605SHoria Geanta 				.maxauthsize = SHA512_DIGEST_SIZE,
2162357fb605SHoria Geanta 			}
2163357fb605SHoria Geanta 		},
2164357fb605SHoria Geanta 		.desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
2165357fb605SHoria Geanta 			             DESC_HDR_SEL0_DEU |
2166357fb605SHoria Geanta 		                     DESC_HDR_MODE0_DEU_CBC |
2167357fb605SHoria Geanta 		                     DESC_HDR_MODE0_DEU_3DES |
2168357fb605SHoria Geanta 		                     DESC_HDR_SEL1_MDEUB |
2169357fb605SHoria Geanta 		                     DESC_HDR_MODE1_MDEU_INIT |
2170357fb605SHoria Geanta 		                     DESC_HDR_MODE1_MDEU_PAD |
2171357fb605SHoria Geanta 		                     DESC_HDR_MODE1_MDEUB_SHA512_HMAC,
2172357fb605SHoria Geanta 	},
2173357fb605SHoria Geanta 	{	.type = CRYPTO_ALG_TYPE_AEAD,
2174357fb605SHoria Geanta 		.alg.crypto = {
217556af8cd4SLee Nipper 			.cra_name = "authenc(hmac(md5),cbc(aes))",
217656af8cd4SLee Nipper 			.cra_driver_name = "authenc-hmac-md5-cbc-aes-talitos",
217756af8cd4SLee Nipper 			.cra_blocksize = AES_BLOCK_SIZE,
217856af8cd4SLee Nipper 			.cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
217956af8cd4SLee Nipper 			.cra_aead = {
21803952f17eSLee Nipper 				.ivsize = AES_BLOCK_SIZE,
21813952f17eSLee Nipper 				.maxauthsize = MD5_DIGEST_SIZE,
218256af8cd4SLee Nipper 			}
21833952f17eSLee Nipper 		},
21843952f17eSLee Nipper 		.desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
21853952f17eSLee Nipper 			             DESC_HDR_SEL0_AESU |
21863952f17eSLee Nipper 		                     DESC_HDR_MODE0_AESU_CBC |
21873952f17eSLee Nipper 		                     DESC_HDR_SEL1_MDEUA |
21883952f17eSLee Nipper 		                     DESC_HDR_MODE1_MDEU_INIT |
21893952f17eSLee Nipper 		                     DESC_HDR_MODE1_MDEU_PAD |
21903952f17eSLee Nipper 		                     DESC_HDR_MODE1_MDEU_MD5_HMAC,
21913952f17eSLee Nipper 	},
2192d5e4aaefSLee Nipper 	{	.type = CRYPTO_ALG_TYPE_AEAD,
2193d5e4aaefSLee Nipper 		.alg.crypto = {
219456af8cd4SLee Nipper 			.cra_name = "authenc(hmac(md5),cbc(des3_ede))",
219556af8cd4SLee Nipper 			.cra_driver_name = "authenc-hmac-md5-cbc-3des-talitos",
219656af8cd4SLee Nipper 			.cra_blocksize = DES3_EDE_BLOCK_SIZE,
219756af8cd4SLee Nipper 			.cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
219856af8cd4SLee Nipper 			.cra_aead = {
21993952f17eSLee Nipper 				.ivsize = DES3_EDE_BLOCK_SIZE,
22003952f17eSLee Nipper 				.maxauthsize = MD5_DIGEST_SIZE,
220156af8cd4SLee Nipper 			}
22023952f17eSLee Nipper 		},
22033952f17eSLee Nipper 		.desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
22043952f17eSLee Nipper 			             DESC_HDR_SEL0_DEU |
22053952f17eSLee Nipper 		                     DESC_HDR_MODE0_DEU_CBC |
22063952f17eSLee Nipper 		                     DESC_HDR_MODE0_DEU_3DES |
22073952f17eSLee Nipper 		                     DESC_HDR_SEL1_MDEUA |
22083952f17eSLee Nipper 		                     DESC_HDR_MODE1_MDEU_INIT |
22093952f17eSLee Nipper 		                     DESC_HDR_MODE1_MDEU_PAD |
22103952f17eSLee Nipper 		                     DESC_HDR_MODE1_MDEU_MD5_HMAC,
22114de9d0b5SLee Nipper 	},
22124de9d0b5SLee Nipper 	/* ABLKCIPHER algorithms. */
2213d5e4aaefSLee Nipper 	{	.type = CRYPTO_ALG_TYPE_ABLKCIPHER,
2214d5e4aaefSLee Nipper 		.alg.crypto = {
22154de9d0b5SLee Nipper 			.cra_name = "cbc(aes)",
22164de9d0b5SLee Nipper 			.cra_driver_name = "cbc-aes-talitos",
22174de9d0b5SLee Nipper 			.cra_blocksize = AES_BLOCK_SIZE,
22184de9d0b5SLee Nipper 			.cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
22194de9d0b5SLee Nipper                                      CRYPTO_ALG_ASYNC,
22204de9d0b5SLee Nipper 			.cra_ablkcipher = {
22214de9d0b5SLee Nipper 				.min_keysize = AES_MIN_KEY_SIZE,
22224de9d0b5SLee Nipper 				.max_keysize = AES_MAX_KEY_SIZE,
22234de9d0b5SLee Nipper 				.ivsize = AES_BLOCK_SIZE,
22244de9d0b5SLee Nipper 			}
22254de9d0b5SLee Nipper 		},
22264de9d0b5SLee Nipper 		.desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
22274de9d0b5SLee Nipper 				     DESC_HDR_SEL0_AESU |
22284de9d0b5SLee Nipper 				     DESC_HDR_MODE0_AESU_CBC,
22294de9d0b5SLee Nipper 	},
2230d5e4aaefSLee Nipper 	{	.type = CRYPTO_ALG_TYPE_ABLKCIPHER,
2231d5e4aaefSLee Nipper 		.alg.crypto = {
22324de9d0b5SLee Nipper 			.cra_name = "cbc(des3_ede)",
22334de9d0b5SLee Nipper 			.cra_driver_name = "cbc-3des-talitos",
22344de9d0b5SLee Nipper 			.cra_blocksize = DES3_EDE_BLOCK_SIZE,
22354de9d0b5SLee Nipper 			.cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
22364de9d0b5SLee Nipper                                      CRYPTO_ALG_ASYNC,
22374de9d0b5SLee Nipper 			.cra_ablkcipher = {
22384de9d0b5SLee Nipper 				.min_keysize = DES3_EDE_KEY_SIZE,
22394de9d0b5SLee Nipper 				.max_keysize = DES3_EDE_KEY_SIZE,
22404de9d0b5SLee Nipper 				.ivsize = DES3_EDE_BLOCK_SIZE,
22414de9d0b5SLee Nipper 			}
22424de9d0b5SLee Nipper 		},
22434de9d0b5SLee Nipper 		.desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
22444de9d0b5SLee Nipper 			             DESC_HDR_SEL0_DEU |
22454de9d0b5SLee Nipper 		                     DESC_HDR_MODE0_DEU_CBC |
22464de9d0b5SLee Nipper 		                     DESC_HDR_MODE0_DEU_3DES,
2247497f2e6bSLee Nipper 	},
2248497f2e6bSLee Nipper 	/* AHASH algorithms. */
2249497f2e6bSLee Nipper 	{	.type = CRYPTO_ALG_TYPE_AHASH,
2250497f2e6bSLee Nipper 		.alg.hash = {
2251497f2e6bSLee Nipper 			.halg.digestsize = MD5_DIGEST_SIZE,
2252497f2e6bSLee Nipper 			.halg.base = {
2253497f2e6bSLee Nipper 				.cra_name = "md5",
2254497f2e6bSLee Nipper 				.cra_driver_name = "md5-talitos",
2255497f2e6bSLee Nipper 				.cra_blocksize = MD5_BLOCK_SIZE,
2256497f2e6bSLee Nipper 				.cra_flags = CRYPTO_ALG_TYPE_AHASH |
2257497f2e6bSLee Nipper 					     CRYPTO_ALG_ASYNC,
22589c4a7965SKim Phillips 			}
2259497f2e6bSLee Nipper 		},
2260497f2e6bSLee Nipper 		.desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2261497f2e6bSLee Nipper 				     DESC_HDR_SEL0_MDEUA |
2262497f2e6bSLee Nipper 				     DESC_HDR_MODE0_MDEU_MD5,
2263497f2e6bSLee Nipper 	},
2264497f2e6bSLee Nipper 	{	.type = CRYPTO_ALG_TYPE_AHASH,
2265497f2e6bSLee Nipper 		.alg.hash = {
2266497f2e6bSLee Nipper 			.halg.digestsize = SHA1_DIGEST_SIZE,
2267497f2e6bSLee Nipper 			.halg.base = {
2268497f2e6bSLee Nipper 				.cra_name = "sha1",
2269497f2e6bSLee Nipper 				.cra_driver_name = "sha1-talitos",
2270497f2e6bSLee Nipper 				.cra_blocksize = SHA1_BLOCK_SIZE,
2271497f2e6bSLee Nipper 				.cra_flags = CRYPTO_ALG_TYPE_AHASH |
2272497f2e6bSLee Nipper 					     CRYPTO_ALG_ASYNC,
2273497f2e6bSLee Nipper 			}
2274497f2e6bSLee Nipper 		},
2275497f2e6bSLee Nipper 		.desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2276497f2e6bSLee Nipper 				     DESC_HDR_SEL0_MDEUA |
2277497f2e6bSLee Nipper 				     DESC_HDR_MODE0_MDEU_SHA1,
2278497f2e6bSLee Nipper 	},
2279497f2e6bSLee Nipper 	{	.type = CRYPTO_ALG_TYPE_AHASH,
2280497f2e6bSLee Nipper 		.alg.hash = {
228160f208d7SKim Phillips 			.halg.digestsize = SHA224_DIGEST_SIZE,
228260f208d7SKim Phillips 			.halg.base = {
228360f208d7SKim Phillips 				.cra_name = "sha224",
228460f208d7SKim Phillips 				.cra_driver_name = "sha224-talitos",
228560f208d7SKim Phillips 				.cra_blocksize = SHA224_BLOCK_SIZE,
228660f208d7SKim Phillips 				.cra_flags = CRYPTO_ALG_TYPE_AHASH |
228760f208d7SKim Phillips 					     CRYPTO_ALG_ASYNC,
228860f208d7SKim Phillips 			}
228960f208d7SKim Phillips 		},
229060f208d7SKim Phillips 		.desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
229160f208d7SKim Phillips 				     DESC_HDR_SEL0_MDEUA |
229260f208d7SKim Phillips 				     DESC_HDR_MODE0_MDEU_SHA224,
229360f208d7SKim Phillips 	},
229460f208d7SKim Phillips 	{	.type = CRYPTO_ALG_TYPE_AHASH,
229560f208d7SKim Phillips 		.alg.hash = {
2296497f2e6bSLee Nipper 			.halg.digestsize = SHA256_DIGEST_SIZE,
2297497f2e6bSLee Nipper 			.halg.base = {
2298497f2e6bSLee Nipper 				.cra_name = "sha256",
2299497f2e6bSLee Nipper 				.cra_driver_name = "sha256-talitos",
2300497f2e6bSLee Nipper 				.cra_blocksize = SHA256_BLOCK_SIZE,
2301497f2e6bSLee Nipper 				.cra_flags = CRYPTO_ALG_TYPE_AHASH |
2302497f2e6bSLee Nipper 					     CRYPTO_ALG_ASYNC,
2303497f2e6bSLee Nipper 			}
2304497f2e6bSLee Nipper 		},
2305497f2e6bSLee Nipper 		.desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2306497f2e6bSLee Nipper 				     DESC_HDR_SEL0_MDEUA |
2307497f2e6bSLee Nipper 				     DESC_HDR_MODE0_MDEU_SHA256,
2308497f2e6bSLee Nipper 	},
2309497f2e6bSLee Nipper 	{	.type = CRYPTO_ALG_TYPE_AHASH,
2310497f2e6bSLee Nipper 		.alg.hash = {
2311497f2e6bSLee Nipper 			.halg.digestsize = SHA384_DIGEST_SIZE,
2312497f2e6bSLee Nipper 			.halg.base = {
2313497f2e6bSLee Nipper 				.cra_name = "sha384",
2314497f2e6bSLee Nipper 				.cra_driver_name = "sha384-talitos",
2315497f2e6bSLee Nipper 				.cra_blocksize = SHA384_BLOCK_SIZE,
2316497f2e6bSLee Nipper 				.cra_flags = CRYPTO_ALG_TYPE_AHASH |
2317497f2e6bSLee Nipper 					     CRYPTO_ALG_ASYNC,
2318497f2e6bSLee Nipper 			}
2319497f2e6bSLee Nipper 		},
2320497f2e6bSLee Nipper 		.desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2321497f2e6bSLee Nipper 				     DESC_HDR_SEL0_MDEUB |
2322497f2e6bSLee Nipper 				     DESC_HDR_MODE0_MDEUB_SHA384,
2323497f2e6bSLee Nipper 	},
2324497f2e6bSLee Nipper 	{	.type = CRYPTO_ALG_TYPE_AHASH,
2325497f2e6bSLee Nipper 		.alg.hash = {
2326497f2e6bSLee Nipper 			.halg.digestsize = SHA512_DIGEST_SIZE,
2327497f2e6bSLee Nipper 			.halg.base = {
2328497f2e6bSLee Nipper 				.cra_name = "sha512",
2329497f2e6bSLee Nipper 				.cra_driver_name = "sha512-talitos",
2330497f2e6bSLee Nipper 				.cra_blocksize = SHA512_BLOCK_SIZE,
2331497f2e6bSLee Nipper 				.cra_flags = CRYPTO_ALG_TYPE_AHASH |
2332497f2e6bSLee Nipper 					     CRYPTO_ALG_ASYNC,
2333497f2e6bSLee Nipper 			}
2334497f2e6bSLee Nipper 		},
2335497f2e6bSLee Nipper 		.desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2336497f2e6bSLee Nipper 				     DESC_HDR_SEL0_MDEUB |
2337497f2e6bSLee Nipper 				     DESC_HDR_MODE0_MDEUB_SHA512,
2338497f2e6bSLee Nipper 	},
233979b3a418SLee Nipper 	{	.type = CRYPTO_ALG_TYPE_AHASH,
234079b3a418SLee Nipper 		.alg.hash = {
234179b3a418SLee Nipper 			.halg.digestsize = MD5_DIGEST_SIZE,
234279b3a418SLee Nipper 			.halg.base = {
234379b3a418SLee Nipper 				.cra_name = "hmac(md5)",
234479b3a418SLee Nipper 				.cra_driver_name = "hmac-md5-talitos",
234579b3a418SLee Nipper 				.cra_blocksize = MD5_BLOCK_SIZE,
234679b3a418SLee Nipper 				.cra_flags = CRYPTO_ALG_TYPE_AHASH |
234779b3a418SLee Nipper 					     CRYPTO_ALG_ASYNC,
234879b3a418SLee Nipper 			}
234979b3a418SLee Nipper 		},
235079b3a418SLee Nipper 		.desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
235179b3a418SLee Nipper 				     DESC_HDR_SEL0_MDEUA |
235279b3a418SLee Nipper 				     DESC_HDR_MODE0_MDEU_MD5,
235379b3a418SLee Nipper 	},
235479b3a418SLee Nipper 	{	.type = CRYPTO_ALG_TYPE_AHASH,
235579b3a418SLee Nipper 		.alg.hash = {
235679b3a418SLee Nipper 			.halg.digestsize = SHA1_DIGEST_SIZE,
235779b3a418SLee Nipper 			.halg.base = {
235879b3a418SLee Nipper 				.cra_name = "hmac(sha1)",
235979b3a418SLee Nipper 				.cra_driver_name = "hmac-sha1-talitos",
236079b3a418SLee Nipper 				.cra_blocksize = SHA1_BLOCK_SIZE,
236179b3a418SLee Nipper 				.cra_flags = CRYPTO_ALG_TYPE_AHASH |
236279b3a418SLee Nipper 					     CRYPTO_ALG_ASYNC,
236379b3a418SLee Nipper 			}
236479b3a418SLee Nipper 		},
236579b3a418SLee Nipper 		.desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
236679b3a418SLee Nipper 				     DESC_HDR_SEL0_MDEUA |
236779b3a418SLee Nipper 				     DESC_HDR_MODE0_MDEU_SHA1,
236879b3a418SLee Nipper 	},
236979b3a418SLee Nipper 	{	.type = CRYPTO_ALG_TYPE_AHASH,
237079b3a418SLee Nipper 		.alg.hash = {
237179b3a418SLee Nipper 			.halg.digestsize = SHA224_DIGEST_SIZE,
237279b3a418SLee Nipper 			.halg.base = {
237379b3a418SLee Nipper 				.cra_name = "hmac(sha224)",
237479b3a418SLee Nipper 				.cra_driver_name = "hmac-sha224-talitos",
237579b3a418SLee Nipper 				.cra_blocksize = SHA224_BLOCK_SIZE,
237679b3a418SLee Nipper 				.cra_flags = CRYPTO_ALG_TYPE_AHASH |
237779b3a418SLee Nipper 					     CRYPTO_ALG_ASYNC,
237879b3a418SLee Nipper 			}
237979b3a418SLee Nipper 		},
238079b3a418SLee Nipper 		.desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
238179b3a418SLee Nipper 				     DESC_HDR_SEL0_MDEUA |
238279b3a418SLee Nipper 				     DESC_HDR_MODE0_MDEU_SHA224,
238379b3a418SLee Nipper 	},
238479b3a418SLee Nipper 	{	.type = CRYPTO_ALG_TYPE_AHASH,
238579b3a418SLee Nipper 		.alg.hash = {
238679b3a418SLee Nipper 			.halg.digestsize = SHA256_DIGEST_SIZE,
238779b3a418SLee Nipper 			.halg.base = {
238879b3a418SLee Nipper 				.cra_name = "hmac(sha256)",
238979b3a418SLee Nipper 				.cra_driver_name = "hmac-sha256-talitos",
239079b3a418SLee Nipper 				.cra_blocksize = SHA256_BLOCK_SIZE,
239179b3a418SLee Nipper 				.cra_flags = CRYPTO_ALG_TYPE_AHASH |
239279b3a418SLee Nipper 					     CRYPTO_ALG_ASYNC,
239379b3a418SLee Nipper 			}
239479b3a418SLee Nipper 		},
239579b3a418SLee Nipper 		.desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
239679b3a418SLee Nipper 				     DESC_HDR_SEL0_MDEUA |
239779b3a418SLee Nipper 				     DESC_HDR_MODE0_MDEU_SHA256,
239879b3a418SLee Nipper 	},
239979b3a418SLee Nipper 	{	.type = CRYPTO_ALG_TYPE_AHASH,
240079b3a418SLee Nipper 		.alg.hash = {
240179b3a418SLee Nipper 			.halg.digestsize = SHA384_DIGEST_SIZE,
240279b3a418SLee Nipper 			.halg.base = {
240379b3a418SLee Nipper 				.cra_name = "hmac(sha384)",
240479b3a418SLee Nipper 				.cra_driver_name = "hmac-sha384-talitos",
240579b3a418SLee Nipper 				.cra_blocksize = SHA384_BLOCK_SIZE,
240679b3a418SLee Nipper 				.cra_flags = CRYPTO_ALG_TYPE_AHASH |
240779b3a418SLee Nipper 					     CRYPTO_ALG_ASYNC,
240879b3a418SLee Nipper 			}
240979b3a418SLee Nipper 		},
241079b3a418SLee Nipper 		.desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
241179b3a418SLee Nipper 				     DESC_HDR_SEL0_MDEUB |
241279b3a418SLee Nipper 				     DESC_HDR_MODE0_MDEUB_SHA384,
241379b3a418SLee Nipper 	},
241479b3a418SLee Nipper 	{	.type = CRYPTO_ALG_TYPE_AHASH,
241579b3a418SLee Nipper 		.alg.hash = {
241679b3a418SLee Nipper 			.halg.digestsize = SHA512_DIGEST_SIZE,
241779b3a418SLee Nipper 			.halg.base = {
241879b3a418SLee Nipper 				.cra_name = "hmac(sha512)",
241979b3a418SLee Nipper 				.cra_driver_name = "hmac-sha512-talitos",
242079b3a418SLee Nipper 				.cra_blocksize = SHA512_BLOCK_SIZE,
242179b3a418SLee Nipper 				.cra_flags = CRYPTO_ALG_TYPE_AHASH |
242279b3a418SLee Nipper 					     CRYPTO_ALG_ASYNC,
242379b3a418SLee Nipper 			}
242479b3a418SLee Nipper 		},
242579b3a418SLee Nipper 		.desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
242679b3a418SLee Nipper 				     DESC_HDR_SEL0_MDEUB |
242779b3a418SLee Nipper 				     DESC_HDR_MODE0_MDEUB_SHA512,
242879b3a418SLee Nipper 	}
24299c4a7965SKim Phillips };
24309c4a7965SKim Phillips 
24319c4a7965SKim Phillips struct talitos_crypto_alg {
24329c4a7965SKim Phillips 	struct list_head entry;
24339c4a7965SKim Phillips 	struct device *dev;
2434acbf7c62SLee Nipper 	struct talitos_alg_template algt;
24359c4a7965SKim Phillips };
24369c4a7965SKim Phillips 
24379c4a7965SKim Phillips static int talitos_cra_init(struct crypto_tfm *tfm)
24389c4a7965SKim Phillips {
24399c4a7965SKim Phillips 	struct crypto_alg *alg = tfm->__crt_alg;
244019bbbc63SKim Phillips 	struct talitos_crypto_alg *talitos_alg;
24419c4a7965SKim Phillips 	struct talitos_ctx *ctx = crypto_tfm_ctx(tfm);
24425228f0f7SKim Phillips 	struct talitos_private *priv;
24439c4a7965SKim Phillips 
2444497f2e6bSLee Nipper 	if ((alg->cra_flags & CRYPTO_ALG_TYPE_MASK) == CRYPTO_ALG_TYPE_AHASH)
2445497f2e6bSLee Nipper 		talitos_alg = container_of(__crypto_ahash_alg(alg),
2446497f2e6bSLee Nipper 					   struct talitos_crypto_alg,
2447497f2e6bSLee Nipper 					   algt.alg.hash);
2448497f2e6bSLee Nipper 	else
2449acbf7c62SLee Nipper 		talitos_alg = container_of(alg, struct talitos_crypto_alg,
2450acbf7c62SLee Nipper 					   algt.alg.crypto);
245119bbbc63SKim Phillips 
24529c4a7965SKim Phillips 	/* update context with ptr to dev */
24539c4a7965SKim Phillips 	ctx->dev = talitos_alg->dev;
245419bbbc63SKim Phillips 
24555228f0f7SKim Phillips 	/* assign SEC channel to tfm in round-robin fashion */
24565228f0f7SKim Phillips 	priv = dev_get_drvdata(ctx->dev);
24575228f0f7SKim Phillips 	ctx->ch = atomic_inc_return(&priv->last_chan) &
24585228f0f7SKim Phillips 		  (priv->num_channels - 1);
24595228f0f7SKim Phillips 
24609c4a7965SKim Phillips 	/* copy descriptor header template value */
2461acbf7c62SLee Nipper 	ctx->desc_hdr_template = talitos_alg->algt.desc_hdr_template;
24629c4a7965SKim Phillips 
2463602dba5aSKim Phillips 	/* select done notification */
2464602dba5aSKim Phillips 	ctx->desc_hdr_template |= DESC_HDR_DONE_NOTIFY;
2465602dba5aSKim Phillips 
2466497f2e6bSLee Nipper 	return 0;
2467497f2e6bSLee Nipper }
2468497f2e6bSLee Nipper 
2469497f2e6bSLee Nipper static int talitos_cra_init_aead(struct crypto_tfm *tfm)
2470497f2e6bSLee Nipper {
2471497f2e6bSLee Nipper 	struct talitos_ctx *ctx = crypto_tfm_ctx(tfm);
2472497f2e6bSLee Nipper 
2473497f2e6bSLee Nipper 	talitos_cra_init(tfm);
24749c4a7965SKim Phillips 
24759c4a7965SKim Phillips 	/* random first IV */
247670bcaca7SLee Nipper 	get_random_bytes(ctx->iv, TALITOS_MAX_IV_LENGTH);
24779c4a7965SKim Phillips 
24789c4a7965SKim Phillips 	return 0;
24799c4a7965SKim Phillips }
24809c4a7965SKim Phillips 
2481497f2e6bSLee Nipper static int talitos_cra_init_ahash(struct crypto_tfm *tfm)
2482497f2e6bSLee Nipper {
2483497f2e6bSLee Nipper 	struct talitos_ctx *ctx = crypto_tfm_ctx(tfm);
2484497f2e6bSLee Nipper 
2485497f2e6bSLee Nipper 	talitos_cra_init(tfm);
2486497f2e6bSLee Nipper 
2487497f2e6bSLee Nipper 	ctx->keylen = 0;
2488497f2e6bSLee Nipper 	crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
2489497f2e6bSLee Nipper 				 sizeof(struct talitos_ahash_req_ctx));
2490497f2e6bSLee Nipper 
2491497f2e6bSLee Nipper 	return 0;
2492497f2e6bSLee Nipper }
2493497f2e6bSLee Nipper 
24949c4a7965SKim Phillips /*
24959c4a7965SKim Phillips  * given the alg's descriptor header template, determine whether descriptor
24969c4a7965SKim Phillips  * type and primary/secondary execution units required match the hw
24979c4a7965SKim Phillips  * capabilities description provided in the device tree node.
24989c4a7965SKim Phillips  */
24999c4a7965SKim Phillips static int hw_supports(struct device *dev, __be32 desc_hdr_template)
25009c4a7965SKim Phillips {
25019c4a7965SKim Phillips 	struct talitos_private *priv = dev_get_drvdata(dev);
25029c4a7965SKim Phillips 	int ret;
25039c4a7965SKim Phillips 
25049c4a7965SKim Phillips 	ret = (1 << DESC_TYPE(desc_hdr_template) & priv->desc_types) &&
25059c4a7965SKim Phillips 	      (1 << PRIMARY_EU(desc_hdr_template) & priv->exec_units);
25069c4a7965SKim Phillips 
25079c4a7965SKim Phillips 	if (SECONDARY_EU(desc_hdr_template))
25089c4a7965SKim Phillips 		ret = ret && (1 << SECONDARY_EU(desc_hdr_template)
25099c4a7965SKim Phillips 		              & priv->exec_units);
25109c4a7965SKim Phillips 
25119c4a7965SKim Phillips 	return ret;
25129c4a7965SKim Phillips }
25139c4a7965SKim Phillips 
25142dc11581SGrant Likely static int talitos_remove(struct platform_device *ofdev)
25159c4a7965SKim Phillips {
25169c4a7965SKim Phillips 	struct device *dev = &ofdev->dev;
25179c4a7965SKim Phillips 	struct talitos_private *priv = dev_get_drvdata(dev);
25189c4a7965SKim Phillips 	struct talitos_crypto_alg *t_alg, *n;
25199c4a7965SKim Phillips 	int i;
25209c4a7965SKim Phillips 
25219c4a7965SKim Phillips 	list_for_each_entry_safe(t_alg, n, &priv->alg_list, entry) {
2522acbf7c62SLee Nipper 		switch (t_alg->algt.type) {
2523acbf7c62SLee Nipper 		case CRYPTO_ALG_TYPE_ABLKCIPHER:
2524acbf7c62SLee Nipper 		case CRYPTO_ALG_TYPE_AEAD:
2525acbf7c62SLee Nipper 			crypto_unregister_alg(&t_alg->algt.alg.crypto);
2526acbf7c62SLee Nipper 			break;
2527acbf7c62SLee Nipper 		case CRYPTO_ALG_TYPE_AHASH:
2528acbf7c62SLee Nipper 			crypto_unregister_ahash(&t_alg->algt.alg.hash);
2529acbf7c62SLee Nipper 			break;
2530acbf7c62SLee Nipper 		}
25319c4a7965SKim Phillips 		list_del(&t_alg->entry);
25329c4a7965SKim Phillips 		kfree(t_alg);
25339c4a7965SKim Phillips 	}
25349c4a7965SKim Phillips 
25359c4a7965SKim Phillips 	if (hw_supports(dev, DESC_HDR_SEL0_RNG))
25369c4a7965SKim Phillips 		talitos_unregister_rng(dev);
25379c4a7965SKim Phillips 
25389c4a7965SKim Phillips 	for (i = 0; i < priv->num_channels; i++)
25394b992628SKim Phillips 		kfree(priv->chan[i].fifo);
25409c4a7965SKim Phillips 
25414b992628SKim Phillips 	kfree(priv->chan);
25429c4a7965SKim Phillips 
2543c3e337f8SKim Phillips 	for (i = 0; i < 2; i++)
25442cdba3cfSKim Phillips 		if (priv->irq[i]) {
2545c3e337f8SKim Phillips 			free_irq(priv->irq[i], dev);
2546c3e337f8SKim Phillips 			irq_dispose_mapping(priv->irq[i]);
25479c4a7965SKim Phillips 		}
25489c4a7965SKim Phillips 
2549c3e337f8SKim Phillips 	tasklet_kill(&priv->done_task[0]);
25502cdba3cfSKim Phillips 	if (priv->irq[1])
2551c3e337f8SKim Phillips 		tasklet_kill(&priv->done_task[1]);
25529c4a7965SKim Phillips 
25539c4a7965SKim Phillips 	iounmap(priv->reg);
25549c4a7965SKim Phillips 
25559c4a7965SKim Phillips 	dev_set_drvdata(dev, NULL);
25569c4a7965SKim Phillips 
25579c4a7965SKim Phillips 	kfree(priv);
25589c4a7965SKim Phillips 
25599c4a7965SKim Phillips 	return 0;
25609c4a7965SKim Phillips }
25619c4a7965SKim Phillips 
25629c4a7965SKim Phillips static struct talitos_crypto_alg *talitos_alg_alloc(struct device *dev,
25639c4a7965SKim Phillips 						    struct talitos_alg_template
25649c4a7965SKim Phillips 						           *template)
25659c4a7965SKim Phillips {
256660f208d7SKim Phillips 	struct talitos_private *priv = dev_get_drvdata(dev);
25679c4a7965SKim Phillips 	struct talitos_crypto_alg *t_alg;
25689c4a7965SKim Phillips 	struct crypto_alg *alg;
25699c4a7965SKim Phillips 
25709c4a7965SKim Phillips 	t_alg = kzalloc(sizeof(struct talitos_crypto_alg), GFP_KERNEL);
25719c4a7965SKim Phillips 	if (!t_alg)
25729c4a7965SKim Phillips 		return ERR_PTR(-ENOMEM);
25739c4a7965SKim Phillips 
2574acbf7c62SLee Nipper 	t_alg->algt = *template;
2575acbf7c62SLee Nipper 
2576acbf7c62SLee Nipper 	switch (t_alg->algt.type) {
2577acbf7c62SLee Nipper 	case CRYPTO_ALG_TYPE_ABLKCIPHER:
2578497f2e6bSLee Nipper 		alg = &t_alg->algt.alg.crypto;
2579497f2e6bSLee Nipper 		alg->cra_init = talitos_cra_init;
2580d4cd3283SKim Phillips 		alg->cra_type = &crypto_ablkcipher_type;
2581b286e003SKim Phillips 		alg->cra_ablkcipher.setkey = ablkcipher_setkey;
2582b286e003SKim Phillips 		alg->cra_ablkcipher.encrypt = ablkcipher_encrypt;
2583b286e003SKim Phillips 		alg->cra_ablkcipher.decrypt = ablkcipher_decrypt;
2584b286e003SKim Phillips 		alg->cra_ablkcipher.geniv = "eseqiv";
2585497f2e6bSLee Nipper 		break;
2586acbf7c62SLee Nipper 	case CRYPTO_ALG_TYPE_AEAD:
2587acbf7c62SLee Nipper 		alg = &t_alg->algt.alg.crypto;
2588497f2e6bSLee Nipper 		alg->cra_init = talitos_cra_init_aead;
2589d4cd3283SKim Phillips 		alg->cra_type = &crypto_aead_type;
2590b286e003SKim Phillips 		alg->cra_aead.setkey = aead_setkey;
2591b286e003SKim Phillips 		alg->cra_aead.setauthsize = aead_setauthsize;
2592b286e003SKim Phillips 		alg->cra_aead.encrypt = aead_encrypt;
2593b286e003SKim Phillips 		alg->cra_aead.decrypt = aead_decrypt;
2594b286e003SKim Phillips 		alg->cra_aead.givencrypt = aead_givencrypt;
2595b286e003SKim Phillips 		alg->cra_aead.geniv = "<built-in>";
2596acbf7c62SLee Nipper 		break;
2597acbf7c62SLee Nipper 	case CRYPTO_ALG_TYPE_AHASH:
2598acbf7c62SLee Nipper 		alg = &t_alg->algt.alg.hash.halg.base;
2599497f2e6bSLee Nipper 		alg->cra_init = talitos_cra_init_ahash;
2600d4cd3283SKim Phillips 		alg->cra_type = &crypto_ahash_type;
2601b286e003SKim Phillips 		t_alg->algt.alg.hash.init = ahash_init;
2602b286e003SKim Phillips 		t_alg->algt.alg.hash.update = ahash_update;
2603b286e003SKim Phillips 		t_alg->algt.alg.hash.final = ahash_final;
2604b286e003SKim Phillips 		t_alg->algt.alg.hash.finup = ahash_finup;
2605b286e003SKim Phillips 		t_alg->algt.alg.hash.digest = ahash_digest;
2606b286e003SKim Phillips 		t_alg->algt.alg.hash.setkey = ahash_setkey;
2607b286e003SKim Phillips 
260879b3a418SLee Nipper 		if (!(priv->features & TALITOS_FTR_HMAC_OK) &&
26090b2730d8SKim Phillips 		    !strncmp(alg->cra_name, "hmac", 4)) {
26100b2730d8SKim Phillips 			kfree(t_alg);
261179b3a418SLee Nipper 			return ERR_PTR(-ENOTSUPP);
26120b2730d8SKim Phillips 		}
261360f208d7SKim Phillips 		if (!(priv->features & TALITOS_FTR_SHA224_HWINIT) &&
261479b3a418SLee Nipper 		    (!strcmp(alg->cra_name, "sha224") ||
261579b3a418SLee Nipper 		     !strcmp(alg->cra_name, "hmac(sha224)"))) {
261660f208d7SKim Phillips 			t_alg->algt.alg.hash.init = ahash_init_sha224_swinit;
261760f208d7SKim Phillips 			t_alg->algt.desc_hdr_template =
261860f208d7SKim Phillips 					DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
261960f208d7SKim Phillips 					DESC_HDR_SEL0_MDEUA |
262060f208d7SKim Phillips 					DESC_HDR_MODE0_MDEU_SHA256;
262160f208d7SKim Phillips 		}
2622497f2e6bSLee Nipper 		break;
26231d11911aSKim Phillips 	default:
26241d11911aSKim Phillips 		dev_err(dev, "unknown algorithm type %d\n", t_alg->algt.type);
26251d11911aSKim Phillips 		return ERR_PTR(-EINVAL);
2626acbf7c62SLee Nipper 	}
26279c4a7965SKim Phillips 
26289c4a7965SKim Phillips 	alg->cra_module = THIS_MODULE;
26299c4a7965SKim Phillips 	alg->cra_priority = TALITOS_CRA_PRIORITY;
26309c4a7965SKim Phillips 	alg->cra_alignmask = 0;
26319c4a7965SKim Phillips 	alg->cra_ctxsize = sizeof(struct talitos_ctx);
2632d912bb76SNikos Mavrogiannopoulos 	alg->cra_flags |= CRYPTO_ALG_KERN_DRIVER_ONLY;
26339c4a7965SKim Phillips 
26349c4a7965SKim Phillips 	t_alg->dev = dev;
26359c4a7965SKim Phillips 
26369c4a7965SKim Phillips 	return t_alg;
26379c4a7965SKim Phillips }
26389c4a7965SKim Phillips 
2639c3e337f8SKim Phillips static int talitos_probe_irq(struct platform_device *ofdev)
2640c3e337f8SKim Phillips {
2641c3e337f8SKim Phillips 	struct device *dev = &ofdev->dev;
2642c3e337f8SKim Phillips 	struct device_node *np = ofdev->dev.of_node;
2643c3e337f8SKim Phillips 	struct talitos_private *priv = dev_get_drvdata(dev);
2644c3e337f8SKim Phillips 	int err;
2645c3e337f8SKim Phillips 
2646c3e337f8SKim Phillips 	priv->irq[0] = irq_of_parse_and_map(np, 0);
26472cdba3cfSKim Phillips 	if (!priv->irq[0]) {
2648c3e337f8SKim Phillips 		dev_err(dev, "failed to map irq\n");
2649c3e337f8SKim Phillips 		return -EINVAL;
2650c3e337f8SKim Phillips 	}
2651c3e337f8SKim Phillips 
2652c3e337f8SKim Phillips 	priv->irq[1] = irq_of_parse_and_map(np, 1);
2653c3e337f8SKim Phillips 
2654c3e337f8SKim Phillips 	/* get the primary irq line */
26552cdba3cfSKim Phillips 	if (!priv->irq[1]) {
2656c3e337f8SKim Phillips 		err = request_irq(priv->irq[0], talitos_interrupt_4ch, 0,
2657c3e337f8SKim Phillips 				  dev_driver_string(dev), dev);
2658c3e337f8SKim Phillips 		goto primary_out;
2659c3e337f8SKim Phillips 	}
2660c3e337f8SKim Phillips 
2661c3e337f8SKim Phillips 	err = request_irq(priv->irq[0], talitos_interrupt_ch0_2, 0,
2662c3e337f8SKim Phillips 			  dev_driver_string(dev), dev);
2663c3e337f8SKim Phillips 	if (err)
2664c3e337f8SKim Phillips 		goto primary_out;
2665c3e337f8SKim Phillips 
2666c3e337f8SKim Phillips 	/* get the secondary irq line */
2667c3e337f8SKim Phillips 	err = request_irq(priv->irq[1], talitos_interrupt_ch1_3, 0,
2668c3e337f8SKim Phillips 			  dev_driver_string(dev), dev);
2669c3e337f8SKim Phillips 	if (err) {
2670c3e337f8SKim Phillips 		dev_err(dev, "failed to request secondary irq\n");
2671c3e337f8SKim Phillips 		irq_dispose_mapping(priv->irq[1]);
26722cdba3cfSKim Phillips 		priv->irq[1] = 0;
2673c3e337f8SKim Phillips 	}
2674c3e337f8SKim Phillips 
2675c3e337f8SKim Phillips 	return err;
2676c3e337f8SKim Phillips 
2677c3e337f8SKim Phillips primary_out:
2678c3e337f8SKim Phillips 	if (err) {
2679c3e337f8SKim Phillips 		dev_err(dev, "failed to request primary irq\n");
2680c3e337f8SKim Phillips 		irq_dispose_mapping(priv->irq[0]);
26812cdba3cfSKim Phillips 		priv->irq[0] = 0;
2682c3e337f8SKim Phillips 	}
2683c3e337f8SKim Phillips 
2684c3e337f8SKim Phillips 	return err;
2685c3e337f8SKim Phillips }
2686c3e337f8SKim Phillips 
26871c48a5c9SGrant Likely static int talitos_probe(struct platform_device *ofdev)
26889c4a7965SKim Phillips {
26899c4a7965SKim Phillips 	struct device *dev = &ofdev->dev;
269061c7a080SGrant Likely 	struct device_node *np = ofdev->dev.of_node;
26919c4a7965SKim Phillips 	struct talitos_private *priv;
26929c4a7965SKim Phillips 	const unsigned int *prop;
26939c4a7965SKim Phillips 	int i, err;
26949c4a7965SKim Phillips 
26959c4a7965SKim Phillips 	priv = kzalloc(sizeof(struct talitos_private), GFP_KERNEL);
26969c4a7965SKim Phillips 	if (!priv)
26979c4a7965SKim Phillips 		return -ENOMEM;
26989c4a7965SKim Phillips 
26999c4a7965SKim Phillips 	dev_set_drvdata(dev, priv);
27009c4a7965SKim Phillips 
27019c4a7965SKim Phillips 	priv->ofdev = ofdev;
27029c4a7965SKim Phillips 
2703511d63cbSHoria Geanta 	spin_lock_init(&priv->reg_lock);
2704511d63cbSHoria Geanta 
2705c3e337f8SKim Phillips 	err = talitos_probe_irq(ofdev);
2706c3e337f8SKim Phillips 	if (err)
2707c3e337f8SKim Phillips 		goto err_out;
2708c3e337f8SKim Phillips 
27092cdba3cfSKim Phillips 	if (!priv->irq[1]) {
2710c3e337f8SKim Phillips 		tasklet_init(&priv->done_task[0], talitos_done_4ch,
2711c3e337f8SKim Phillips 			     (unsigned long)dev);
2712c3e337f8SKim Phillips 	} else {
2713c3e337f8SKim Phillips 		tasklet_init(&priv->done_task[0], talitos_done_ch0_2,
2714c3e337f8SKim Phillips 			     (unsigned long)dev);
2715c3e337f8SKim Phillips 		tasklet_init(&priv->done_task[1], talitos_done_ch1_3,
2716c3e337f8SKim Phillips 			     (unsigned long)dev);
2717c3e337f8SKim Phillips 	}
27189c4a7965SKim Phillips 
2719fe5720e2SKim Phillips 	INIT_LIST_HEAD(&priv->alg_list);
2720fe5720e2SKim Phillips 
27219c4a7965SKim Phillips 	priv->reg = of_iomap(np, 0);
27229c4a7965SKim Phillips 	if (!priv->reg) {
27239c4a7965SKim Phillips 		dev_err(dev, "failed to of_iomap\n");
27249c4a7965SKim Phillips 		err = -ENOMEM;
27259c4a7965SKim Phillips 		goto err_out;
27269c4a7965SKim Phillips 	}
27279c4a7965SKim Phillips 
27289c4a7965SKim Phillips 	/* get SEC version capabilities from device tree */
27299c4a7965SKim Phillips 	prop = of_get_property(np, "fsl,num-channels", NULL);
27309c4a7965SKim Phillips 	if (prop)
27319c4a7965SKim Phillips 		priv->num_channels = *prop;
27329c4a7965SKim Phillips 
27339c4a7965SKim Phillips 	prop = of_get_property(np, "fsl,channel-fifo-len", NULL);
27349c4a7965SKim Phillips 	if (prop)
27359c4a7965SKim Phillips 		priv->chfifo_len = *prop;
27369c4a7965SKim Phillips 
27379c4a7965SKim Phillips 	prop = of_get_property(np, "fsl,exec-units-mask", NULL);
27389c4a7965SKim Phillips 	if (prop)
27399c4a7965SKim Phillips 		priv->exec_units = *prop;
27409c4a7965SKim Phillips 
27419c4a7965SKim Phillips 	prop = of_get_property(np, "fsl,descriptor-types-mask", NULL);
27429c4a7965SKim Phillips 	if (prop)
27439c4a7965SKim Phillips 		priv->desc_types = *prop;
27449c4a7965SKim Phillips 
27459c4a7965SKim Phillips 	if (!is_power_of_2(priv->num_channels) || !priv->chfifo_len ||
27469c4a7965SKim Phillips 	    !priv->exec_units || !priv->desc_types) {
27479c4a7965SKim Phillips 		dev_err(dev, "invalid property data in device tree node\n");
27489c4a7965SKim Phillips 		err = -EINVAL;
27499c4a7965SKim Phillips 		goto err_out;
27509c4a7965SKim Phillips 	}
27519c4a7965SKim Phillips 
2752f3c85bc1SLee Nipper 	if (of_device_is_compatible(np, "fsl,sec3.0"))
2753f3c85bc1SLee Nipper 		priv->features |= TALITOS_FTR_SRC_LINK_TBL_LEN_INCLUDES_EXTENT;
2754f3c85bc1SLee Nipper 
2755fe5720e2SKim Phillips 	if (of_device_is_compatible(np, "fsl,sec2.1"))
275660f208d7SKim Phillips 		priv->features |= TALITOS_FTR_HW_AUTH_CHECK |
275779b3a418SLee Nipper 				  TALITOS_FTR_SHA224_HWINIT |
275879b3a418SLee Nipper 				  TALITOS_FTR_HMAC_OK;
2759fe5720e2SKim Phillips 
27604b992628SKim Phillips 	priv->chan = kzalloc(sizeof(struct talitos_channel) *
27614b992628SKim Phillips 			     priv->num_channels, GFP_KERNEL);
27624b992628SKim Phillips 	if (!priv->chan) {
27634b992628SKim Phillips 		dev_err(dev, "failed to allocate channel management space\n");
27649c4a7965SKim Phillips 		err = -ENOMEM;
27659c4a7965SKim Phillips 		goto err_out;
27669c4a7965SKim Phillips 	}
27679c4a7965SKim Phillips 
2768c3e337f8SKim Phillips 	for (i = 0; i < priv->num_channels; i++) {
2769c3e337f8SKim Phillips 		priv->chan[i].reg = priv->reg + TALITOS_CH_STRIDE * (i + 1);
27702cdba3cfSKim Phillips 		if (!priv->irq[1] || !(i & 1))
2771c3e337f8SKim Phillips 			priv->chan[i].reg += TALITOS_CH_BASE_OFFSET;
2772c3e337f8SKim Phillips 	}
2773ad42d5fcSKim Phillips 
27749c4a7965SKim Phillips 	for (i = 0; i < priv->num_channels; i++) {
27754b992628SKim Phillips 		spin_lock_init(&priv->chan[i].head_lock);
27764b992628SKim Phillips 		spin_lock_init(&priv->chan[i].tail_lock);
27779c4a7965SKim Phillips 	}
27789c4a7965SKim Phillips 
27799c4a7965SKim Phillips 	priv->fifo_len = roundup_pow_of_two(priv->chfifo_len);
27809c4a7965SKim Phillips 
27819c4a7965SKim Phillips 	for (i = 0; i < priv->num_channels; i++) {
27824b992628SKim Phillips 		priv->chan[i].fifo = kzalloc(sizeof(struct talitos_request) *
27839c4a7965SKim Phillips 					     priv->fifo_len, GFP_KERNEL);
27844b992628SKim Phillips 		if (!priv->chan[i].fifo) {
27859c4a7965SKim Phillips 			dev_err(dev, "failed to allocate request fifo %d\n", i);
27869c4a7965SKim Phillips 			err = -ENOMEM;
27879c4a7965SKim Phillips 			goto err_out;
27889c4a7965SKim Phillips 		}
27899c4a7965SKim Phillips 	}
27909c4a7965SKim Phillips 
2791ec6644d6SKim Phillips 	for (i = 0; i < priv->num_channels; i++)
27924b992628SKim Phillips 		atomic_set(&priv->chan[i].submit_count,
27934b992628SKim Phillips 			   -(priv->chfifo_len - 1));
27949c4a7965SKim Phillips 
279581eb024cSKim Phillips 	dma_set_mask(dev, DMA_BIT_MASK(36));
279681eb024cSKim Phillips 
27979c4a7965SKim Phillips 	/* reset and initialize the h/w */
27989c4a7965SKim Phillips 	err = init_device(dev);
27999c4a7965SKim Phillips 	if (err) {
28009c4a7965SKim Phillips 		dev_err(dev, "failed to initialize device\n");
28019c4a7965SKim Phillips 		goto err_out;
28029c4a7965SKim Phillips 	}
28039c4a7965SKim Phillips 
28049c4a7965SKim Phillips 	/* register the RNG, if available */
28059c4a7965SKim Phillips 	if (hw_supports(dev, DESC_HDR_SEL0_RNG)) {
28069c4a7965SKim Phillips 		err = talitos_register_rng(dev);
28079c4a7965SKim Phillips 		if (err) {
28089c4a7965SKim Phillips 			dev_err(dev, "failed to register hwrng: %d\n", err);
28099c4a7965SKim Phillips 			goto err_out;
28109c4a7965SKim Phillips 		} else
28119c4a7965SKim Phillips 			dev_info(dev, "hwrng\n");
28129c4a7965SKim Phillips 	}
28139c4a7965SKim Phillips 
28149c4a7965SKim Phillips 	/* register crypto algorithms the device supports */
28159c4a7965SKim Phillips 	for (i = 0; i < ARRAY_SIZE(driver_algs); i++) {
28169c4a7965SKim Phillips 		if (hw_supports(dev, driver_algs[i].desc_hdr_template)) {
28179c4a7965SKim Phillips 			struct talitos_crypto_alg *t_alg;
2818acbf7c62SLee Nipper 			char *name = NULL;
28199c4a7965SKim Phillips 
28209c4a7965SKim Phillips 			t_alg = talitos_alg_alloc(dev, &driver_algs[i]);
28219c4a7965SKim Phillips 			if (IS_ERR(t_alg)) {
28229c4a7965SKim Phillips 				err = PTR_ERR(t_alg);
28230b2730d8SKim Phillips 				if (err == -ENOTSUPP)
282479b3a418SLee Nipper 					continue;
28259c4a7965SKim Phillips 				goto err_out;
28269c4a7965SKim Phillips 			}
28279c4a7965SKim Phillips 
2828acbf7c62SLee Nipper 			switch (t_alg->algt.type) {
2829acbf7c62SLee Nipper 			case CRYPTO_ALG_TYPE_ABLKCIPHER:
2830acbf7c62SLee Nipper 			case CRYPTO_ALG_TYPE_AEAD:
2831acbf7c62SLee Nipper 				err = crypto_register_alg(
2832acbf7c62SLee Nipper 						&t_alg->algt.alg.crypto);
2833acbf7c62SLee Nipper 				name = t_alg->algt.alg.crypto.cra_driver_name;
2834acbf7c62SLee Nipper 				break;
2835acbf7c62SLee Nipper 			case CRYPTO_ALG_TYPE_AHASH:
2836acbf7c62SLee Nipper 				err = crypto_register_ahash(
2837acbf7c62SLee Nipper 						&t_alg->algt.alg.hash);
2838acbf7c62SLee Nipper 				name =
2839acbf7c62SLee Nipper 				 t_alg->algt.alg.hash.halg.base.cra_driver_name;
2840acbf7c62SLee Nipper 				break;
2841acbf7c62SLee Nipper 			}
28429c4a7965SKim Phillips 			if (err) {
28439c4a7965SKim Phillips 				dev_err(dev, "%s alg registration failed\n",
2844acbf7c62SLee Nipper 					name);
28459c4a7965SKim Phillips 				kfree(t_alg);
28465b859b6eSKim Phillips 			} else
28479c4a7965SKim Phillips 				list_add_tail(&t_alg->entry, &priv->alg_list);
28489c4a7965SKim Phillips 		}
28499c4a7965SKim Phillips 	}
28505b859b6eSKim Phillips 	if (!list_empty(&priv->alg_list))
28515b859b6eSKim Phillips 		dev_info(dev, "%s algorithms registered in /proc/crypto\n",
28525b859b6eSKim Phillips 			 (char *)of_get_property(np, "compatible", NULL));
28539c4a7965SKim Phillips 
28549c4a7965SKim Phillips 	return 0;
28559c4a7965SKim Phillips 
28569c4a7965SKim Phillips err_out:
28579c4a7965SKim Phillips 	talitos_remove(ofdev);
28589c4a7965SKim Phillips 
28599c4a7965SKim Phillips 	return err;
28609c4a7965SKim Phillips }
28619c4a7965SKim Phillips 
28626c3f975aSMárton Németh static const struct of_device_id talitos_match[] = {
28639c4a7965SKim Phillips 	{
28649c4a7965SKim Phillips 		.compatible = "fsl,sec2.0",
28659c4a7965SKim Phillips 	},
28669c4a7965SKim Phillips 	{},
28679c4a7965SKim Phillips };
28689c4a7965SKim Phillips MODULE_DEVICE_TABLE(of, talitos_match);
28699c4a7965SKim Phillips 
28701c48a5c9SGrant Likely static struct platform_driver talitos_driver = {
28714018294bSGrant Likely 	.driver = {
28729c4a7965SKim Phillips 		.name = "talitos",
28734018294bSGrant Likely 		.owner = THIS_MODULE,
28744018294bSGrant Likely 		.of_match_table = talitos_match,
28754018294bSGrant Likely 	},
28769c4a7965SKim Phillips 	.probe = talitos_probe,
2877596f1034SAl Viro 	.remove = talitos_remove,
28789c4a7965SKim Phillips };
28799c4a7965SKim Phillips 
2880741e8c2dSAxel Lin module_platform_driver(talitos_driver);
28819c4a7965SKim Phillips 
28829c4a7965SKim Phillips MODULE_LICENSE("GPL");
28839c4a7965SKim Phillips MODULE_AUTHOR("Kim Phillips <kim.phillips@freescale.com>");
28849c4a7965SKim Phillips MODULE_DESCRIPTION("Freescale integrated security engine (SEC) driver");
2885