xref: /linux/drivers/crypto/talitos.c (revision 5af5073004071cedd0343eee51d77955037ec6f3)
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>
35*5af50730SRob Herring #include <linux/of_address.h>
36*5af50730SRob Herring #include <linux/of_irq.h>
379c4a7965SKim Phillips #include <linux/of_platform.h>
389c4a7965SKim Phillips #include <linux/dma-mapping.h>
399c4a7965SKim Phillips #include <linux/io.h>
409c4a7965SKim Phillips #include <linux/spinlock.h>
419c4a7965SKim Phillips #include <linux/rtnetlink.h>
425a0e3ad6STejun Heo #include <linux/slab.h>
439c4a7965SKim Phillips 
449c4a7965SKim Phillips #include <crypto/algapi.h>
459c4a7965SKim Phillips #include <crypto/aes.h>
463952f17eSLee Nipper #include <crypto/des.h>
479c4a7965SKim Phillips #include <crypto/sha.h>
48497f2e6bSLee Nipper #include <crypto/md5.h>
499c4a7965SKim Phillips #include <crypto/aead.h>
509c4a7965SKim Phillips #include <crypto/authenc.h>
514de9d0b5SLee Nipper #include <crypto/skcipher.h>
52acbf7c62SLee Nipper #include <crypto/hash.h>
53acbf7c62SLee Nipper #include <crypto/internal/hash.h>
544de9d0b5SLee Nipper #include <crypto/scatterwalk.h>
559c4a7965SKim Phillips 
569c4a7965SKim Phillips #include "talitos.h"
579c4a7965SKim Phillips 
5881eb024cSKim Phillips static void to_talitos_ptr(struct talitos_ptr *talitos_ptr, dma_addr_t dma_addr)
5981eb024cSKim Phillips {
6081eb024cSKim Phillips 	talitos_ptr->ptr = cpu_to_be32(lower_32_bits(dma_addr));
61a752447aSKim Phillips 	talitos_ptr->eptr = upper_32_bits(dma_addr);
6281eb024cSKim Phillips }
6381eb024cSKim Phillips 
649c4a7965SKim Phillips /*
659c4a7965SKim Phillips  * map virtual single (contiguous) pointer to h/w descriptor pointer
669c4a7965SKim Phillips  */
679c4a7965SKim Phillips static void map_single_talitos_ptr(struct device *dev,
689c4a7965SKim Phillips 				   struct talitos_ptr *talitos_ptr,
699c4a7965SKim Phillips 				   unsigned short len, void *data,
709c4a7965SKim Phillips 				   unsigned char extent,
719c4a7965SKim Phillips 				   enum dma_data_direction dir)
729c4a7965SKim Phillips {
7381eb024cSKim Phillips 	dma_addr_t dma_addr = dma_map_single(dev, data, len, dir);
7481eb024cSKim Phillips 
759c4a7965SKim Phillips 	talitos_ptr->len = cpu_to_be16(len);
7681eb024cSKim Phillips 	to_talitos_ptr(talitos_ptr, dma_addr);
779c4a7965SKim Phillips 	talitos_ptr->j_extent = extent;
789c4a7965SKim Phillips }
799c4a7965SKim Phillips 
809c4a7965SKim Phillips /*
819c4a7965SKim Phillips  * unmap bus single (contiguous) h/w descriptor pointer
829c4a7965SKim Phillips  */
839c4a7965SKim Phillips static void unmap_single_talitos_ptr(struct device *dev,
849c4a7965SKim Phillips 				     struct talitos_ptr *talitos_ptr,
859c4a7965SKim Phillips 				     enum dma_data_direction dir)
869c4a7965SKim Phillips {
879c4a7965SKim Phillips 	dma_unmap_single(dev, be32_to_cpu(talitos_ptr->ptr),
889c4a7965SKim Phillips 			 be16_to_cpu(talitos_ptr->len), dir);
899c4a7965SKim Phillips }
909c4a7965SKim Phillips 
919c4a7965SKim Phillips static int reset_channel(struct device *dev, int ch)
929c4a7965SKim Phillips {
939c4a7965SKim Phillips 	struct talitos_private *priv = dev_get_drvdata(dev);
949c4a7965SKim Phillips 	unsigned int timeout = TALITOS_TIMEOUT;
959c4a7965SKim Phillips 
96ad42d5fcSKim Phillips 	setbits32(priv->chan[ch].reg + TALITOS_CCCR, TALITOS_CCCR_RESET);
979c4a7965SKim Phillips 
98ad42d5fcSKim Phillips 	while ((in_be32(priv->chan[ch].reg + TALITOS_CCCR) & TALITOS_CCCR_RESET)
999c4a7965SKim Phillips 	       && --timeout)
1009c4a7965SKim Phillips 		cpu_relax();
1019c4a7965SKim Phillips 
1029c4a7965SKim Phillips 	if (timeout == 0) {
1039c4a7965SKim Phillips 		dev_err(dev, "failed to reset channel %d\n", ch);
1049c4a7965SKim Phillips 		return -EIO;
1059c4a7965SKim Phillips 	}
1069c4a7965SKim Phillips 
10781eb024cSKim Phillips 	/* set 36-bit addressing, done writeback enable and done IRQ enable */
108ad42d5fcSKim Phillips 	setbits32(priv->chan[ch].reg + TALITOS_CCCR_LO, TALITOS_CCCR_LO_EAE |
10981eb024cSKim Phillips 		  TALITOS_CCCR_LO_CDWE | TALITOS_CCCR_LO_CDIE);
1109c4a7965SKim Phillips 
111fe5720e2SKim Phillips 	/* and ICCR writeback, if available */
112fe5720e2SKim Phillips 	if (priv->features & TALITOS_FTR_HW_AUTH_CHECK)
113ad42d5fcSKim Phillips 		setbits32(priv->chan[ch].reg + TALITOS_CCCR_LO,
114fe5720e2SKim Phillips 		          TALITOS_CCCR_LO_IWSE);
115fe5720e2SKim Phillips 
1169c4a7965SKim Phillips 	return 0;
1179c4a7965SKim Phillips }
1189c4a7965SKim Phillips 
1199c4a7965SKim Phillips static int reset_device(struct device *dev)
1209c4a7965SKim Phillips {
1219c4a7965SKim Phillips 	struct talitos_private *priv = dev_get_drvdata(dev);
1229c4a7965SKim Phillips 	unsigned int timeout = TALITOS_TIMEOUT;
123c3e337f8SKim Phillips 	u32 mcr = TALITOS_MCR_SWR;
1249c4a7965SKim Phillips 
125c3e337f8SKim Phillips 	setbits32(priv->reg + TALITOS_MCR, mcr);
1269c4a7965SKim Phillips 
1279c4a7965SKim Phillips 	while ((in_be32(priv->reg + TALITOS_MCR) & TALITOS_MCR_SWR)
1289c4a7965SKim Phillips 	       && --timeout)
1299c4a7965SKim Phillips 		cpu_relax();
1309c4a7965SKim Phillips 
1312cdba3cfSKim Phillips 	if (priv->irq[1]) {
132c3e337f8SKim Phillips 		mcr = TALITOS_MCR_RCA1 | TALITOS_MCR_RCA3;
133c3e337f8SKim Phillips 		setbits32(priv->reg + TALITOS_MCR, mcr);
134c3e337f8SKim Phillips 	}
135c3e337f8SKim Phillips 
1369c4a7965SKim Phillips 	if (timeout == 0) {
1379c4a7965SKim Phillips 		dev_err(dev, "failed to reset device\n");
1389c4a7965SKim Phillips 		return -EIO;
1399c4a7965SKim Phillips 	}
1409c4a7965SKim Phillips 
1419c4a7965SKim Phillips 	return 0;
1429c4a7965SKim Phillips }
1439c4a7965SKim Phillips 
1449c4a7965SKim Phillips /*
1459c4a7965SKim Phillips  * Reset and initialize the device
1469c4a7965SKim Phillips  */
1479c4a7965SKim Phillips static int init_device(struct device *dev)
1489c4a7965SKim Phillips {
1499c4a7965SKim Phillips 	struct talitos_private *priv = dev_get_drvdata(dev);
1509c4a7965SKim Phillips 	int ch, err;
1519c4a7965SKim Phillips 
1529c4a7965SKim Phillips 	/*
1539c4a7965SKim Phillips 	 * Master reset
1549c4a7965SKim Phillips 	 * errata documentation: warning: certain SEC interrupts
1559c4a7965SKim Phillips 	 * are not fully cleared by writing the MCR:SWR bit,
1569c4a7965SKim Phillips 	 * set bit twice to completely reset
1579c4a7965SKim Phillips 	 */
1589c4a7965SKim Phillips 	err = reset_device(dev);
1599c4a7965SKim Phillips 	if (err)
1609c4a7965SKim Phillips 		return err;
1619c4a7965SKim Phillips 
1629c4a7965SKim Phillips 	err = reset_device(dev);
1639c4a7965SKim Phillips 	if (err)
1649c4a7965SKim Phillips 		return err;
1659c4a7965SKim Phillips 
1669c4a7965SKim Phillips 	/* reset channels */
1679c4a7965SKim Phillips 	for (ch = 0; ch < priv->num_channels; ch++) {
1689c4a7965SKim Phillips 		err = reset_channel(dev, ch);
1699c4a7965SKim Phillips 		if (err)
1709c4a7965SKim Phillips 			return err;
1719c4a7965SKim Phillips 	}
1729c4a7965SKim Phillips 
1739c4a7965SKim Phillips 	/* enable channel done and error interrupts */
1749c4a7965SKim Phillips 	setbits32(priv->reg + TALITOS_IMR, TALITOS_IMR_INIT);
1759c4a7965SKim Phillips 	setbits32(priv->reg + TALITOS_IMR_LO, TALITOS_IMR_LO_INIT);
1769c4a7965SKim Phillips 
177fe5720e2SKim Phillips 	/* disable integrity check error interrupts (use writeback instead) */
178fe5720e2SKim Phillips 	if (priv->features & TALITOS_FTR_HW_AUTH_CHECK)
179fe5720e2SKim Phillips 		setbits32(priv->reg + TALITOS_MDEUICR_LO,
180fe5720e2SKim Phillips 		          TALITOS_MDEUICR_LO_ICE);
181fe5720e2SKim Phillips 
1829c4a7965SKim Phillips 	return 0;
1839c4a7965SKim Phillips }
1849c4a7965SKim Phillips 
1859c4a7965SKim Phillips /**
1869c4a7965SKim Phillips  * talitos_submit - submits a descriptor to the device for processing
1879c4a7965SKim Phillips  * @dev:	the SEC device to be used
1885228f0f7SKim Phillips  * @ch:		the SEC device channel to be used
1899c4a7965SKim Phillips  * @desc:	the descriptor to be processed by the device
1909c4a7965SKim Phillips  * @callback:	whom to call when processing is complete
1919c4a7965SKim Phillips  * @context:	a handle for use by caller (optional)
1929c4a7965SKim Phillips  *
1939c4a7965SKim Phillips  * desc must contain valid dma-mapped (bus physical) address pointers.
1949c4a7965SKim Phillips  * callback must check err and feedback in descriptor header
1959c4a7965SKim Phillips  * for device processing status.
1969c4a7965SKim Phillips  */
197865d5061SHoria Geanta int talitos_submit(struct device *dev, int ch, struct talitos_desc *desc,
1989c4a7965SKim Phillips 		   void (*callback)(struct device *dev,
1999c4a7965SKim Phillips 				    struct talitos_desc *desc,
2009c4a7965SKim Phillips 				    void *context, int error),
2019c4a7965SKim Phillips 		   void *context)
2029c4a7965SKim Phillips {
2039c4a7965SKim Phillips 	struct talitos_private *priv = dev_get_drvdata(dev);
2049c4a7965SKim Phillips 	struct talitos_request *request;
2055228f0f7SKim Phillips 	unsigned long flags;
2069c4a7965SKim Phillips 	int head;
2079c4a7965SKim Phillips 
2084b992628SKim Phillips 	spin_lock_irqsave(&priv->chan[ch].head_lock, flags);
2099c4a7965SKim Phillips 
2104b992628SKim Phillips 	if (!atomic_inc_not_zero(&priv->chan[ch].submit_count)) {
211ec6644d6SKim Phillips 		/* h/w fifo is full */
2124b992628SKim Phillips 		spin_unlock_irqrestore(&priv->chan[ch].head_lock, flags);
2139c4a7965SKim Phillips 		return -EAGAIN;
2149c4a7965SKim Phillips 	}
2159c4a7965SKim Phillips 
2164b992628SKim Phillips 	head = priv->chan[ch].head;
2174b992628SKim Phillips 	request = &priv->chan[ch].fifo[head];
218ec6644d6SKim Phillips 
2199c4a7965SKim Phillips 	/* map descriptor and save caller data */
2209c4a7965SKim Phillips 	request->dma_desc = dma_map_single(dev, desc, sizeof(*desc),
2219c4a7965SKim Phillips 					   DMA_BIDIRECTIONAL);
2229c4a7965SKim Phillips 	request->callback = callback;
2239c4a7965SKim Phillips 	request->context = context;
2249c4a7965SKim Phillips 
2259c4a7965SKim Phillips 	/* increment fifo head */
2264b992628SKim Phillips 	priv->chan[ch].head = (priv->chan[ch].head + 1) & (priv->fifo_len - 1);
2279c4a7965SKim Phillips 
2289c4a7965SKim Phillips 	smp_wmb();
2299c4a7965SKim Phillips 	request->desc = desc;
2309c4a7965SKim Phillips 
2319c4a7965SKim Phillips 	/* GO! */
2329c4a7965SKim Phillips 	wmb();
233ad42d5fcSKim Phillips 	out_be32(priv->chan[ch].reg + TALITOS_FF,
234ad42d5fcSKim Phillips 		 upper_32_bits(request->dma_desc));
235ad42d5fcSKim Phillips 	out_be32(priv->chan[ch].reg + TALITOS_FF_LO,
236a752447aSKim Phillips 		 lower_32_bits(request->dma_desc));
2379c4a7965SKim Phillips 
2384b992628SKim Phillips 	spin_unlock_irqrestore(&priv->chan[ch].head_lock, flags);
2399c4a7965SKim Phillips 
2409c4a7965SKim Phillips 	return -EINPROGRESS;
2419c4a7965SKim Phillips }
242865d5061SHoria Geanta EXPORT_SYMBOL(talitos_submit);
2439c4a7965SKim Phillips 
2449c4a7965SKim Phillips /*
2459c4a7965SKim Phillips  * process what was done, notify callback of error if not
2469c4a7965SKim Phillips  */
2479c4a7965SKim Phillips static void flush_channel(struct device *dev, int ch, int error, int reset_ch)
2489c4a7965SKim Phillips {
2499c4a7965SKim Phillips 	struct talitos_private *priv = dev_get_drvdata(dev);
2509c4a7965SKim Phillips 	struct talitos_request *request, saved_req;
2519c4a7965SKim Phillips 	unsigned long flags;
2529c4a7965SKim Phillips 	int tail, status;
2539c4a7965SKim Phillips 
2544b992628SKim Phillips 	spin_lock_irqsave(&priv->chan[ch].tail_lock, flags);
2559c4a7965SKim Phillips 
2564b992628SKim Phillips 	tail = priv->chan[ch].tail;
2574b992628SKim Phillips 	while (priv->chan[ch].fifo[tail].desc) {
2584b992628SKim Phillips 		request = &priv->chan[ch].fifo[tail];
2599c4a7965SKim Phillips 
2609c4a7965SKim Phillips 		/* descriptors with their done bits set don't get the error */
2619c4a7965SKim Phillips 		rmb();
262ca38a814SLee Nipper 		if ((request->desc->hdr & DESC_HDR_DONE) == DESC_HDR_DONE)
2639c4a7965SKim Phillips 			status = 0;
264ca38a814SLee Nipper 		else
2659c4a7965SKim Phillips 			if (!error)
2669c4a7965SKim Phillips 				break;
2679c4a7965SKim Phillips 			else
2689c4a7965SKim Phillips 				status = error;
2699c4a7965SKim Phillips 
2709c4a7965SKim Phillips 		dma_unmap_single(dev, request->dma_desc,
271e938e465SKim Phillips 				 sizeof(struct talitos_desc),
272e938e465SKim Phillips 				 DMA_BIDIRECTIONAL);
2739c4a7965SKim Phillips 
2749c4a7965SKim Phillips 		/* copy entries so we can call callback outside lock */
2759c4a7965SKim Phillips 		saved_req.desc = request->desc;
2769c4a7965SKim Phillips 		saved_req.callback = request->callback;
2779c4a7965SKim Phillips 		saved_req.context = request->context;
2789c4a7965SKim Phillips 
2799c4a7965SKim Phillips 		/* release request entry in fifo */
2809c4a7965SKim Phillips 		smp_wmb();
2819c4a7965SKim Phillips 		request->desc = NULL;
2829c4a7965SKim Phillips 
2839c4a7965SKim Phillips 		/* increment fifo tail */
2844b992628SKim Phillips 		priv->chan[ch].tail = (tail + 1) & (priv->fifo_len - 1);
2859c4a7965SKim Phillips 
2864b992628SKim Phillips 		spin_unlock_irqrestore(&priv->chan[ch].tail_lock, flags);
287ec6644d6SKim Phillips 
2884b992628SKim Phillips 		atomic_dec(&priv->chan[ch].submit_count);
289ec6644d6SKim Phillips 
2909c4a7965SKim Phillips 		saved_req.callback(dev, saved_req.desc, saved_req.context,
2919c4a7965SKim Phillips 				   status);
2929c4a7965SKim Phillips 		/* channel may resume processing in single desc error case */
2939c4a7965SKim Phillips 		if (error && !reset_ch && status == error)
2949c4a7965SKim Phillips 			return;
2954b992628SKim Phillips 		spin_lock_irqsave(&priv->chan[ch].tail_lock, flags);
2964b992628SKim Phillips 		tail = priv->chan[ch].tail;
2979c4a7965SKim Phillips 	}
2989c4a7965SKim Phillips 
2994b992628SKim Phillips 	spin_unlock_irqrestore(&priv->chan[ch].tail_lock, flags);
3009c4a7965SKim Phillips }
3019c4a7965SKim Phillips 
3029c4a7965SKim Phillips /*
3039c4a7965SKim Phillips  * process completed requests for channels that have done status
3049c4a7965SKim Phillips  */
305c3e337f8SKim Phillips #define DEF_TALITOS_DONE(name, ch_done_mask)				\
306c3e337f8SKim Phillips static void talitos_done_##name(unsigned long data)			\
307c3e337f8SKim Phillips {									\
308c3e337f8SKim Phillips 	struct device *dev = (struct device *)data;			\
309c3e337f8SKim Phillips 	struct talitos_private *priv = dev_get_drvdata(dev);		\
310511d63cbSHoria Geanta 	unsigned long flags;						\
311c3e337f8SKim Phillips 									\
312c3e337f8SKim Phillips 	if (ch_done_mask & 1)						\
313c3e337f8SKim Phillips 		flush_channel(dev, 0, 0, 0);				\
314c3e337f8SKim Phillips 	if (priv->num_channels == 1)					\
315c3e337f8SKim Phillips 		goto out;						\
316c3e337f8SKim Phillips 	if (ch_done_mask & (1 << 2))					\
317c3e337f8SKim Phillips 		flush_channel(dev, 1, 0, 0);				\
318c3e337f8SKim Phillips 	if (ch_done_mask & (1 << 4))					\
319c3e337f8SKim Phillips 		flush_channel(dev, 2, 0, 0);				\
320c3e337f8SKim Phillips 	if (ch_done_mask & (1 << 6))					\
321c3e337f8SKim Phillips 		flush_channel(dev, 3, 0, 0);				\
322c3e337f8SKim Phillips 									\
323c3e337f8SKim Phillips out:									\
324c3e337f8SKim Phillips 	/* At this point, all completed channels have been processed */	\
325c3e337f8SKim Phillips 	/* Unmask done interrupts for channels completed later on. */	\
326511d63cbSHoria Geanta 	spin_lock_irqsave(&priv->reg_lock, flags);			\
327c3e337f8SKim Phillips 	setbits32(priv->reg + TALITOS_IMR, ch_done_mask);		\
328c3e337f8SKim Phillips 	setbits32(priv->reg + TALITOS_IMR_LO, TALITOS_IMR_LO_INIT);	\
329511d63cbSHoria Geanta 	spin_unlock_irqrestore(&priv->reg_lock, flags);			\
3309c4a7965SKim Phillips }
331c3e337f8SKim Phillips DEF_TALITOS_DONE(4ch, TALITOS_ISR_4CHDONE)
332c3e337f8SKim Phillips DEF_TALITOS_DONE(ch0_2, TALITOS_ISR_CH_0_2_DONE)
333c3e337f8SKim Phillips DEF_TALITOS_DONE(ch1_3, TALITOS_ISR_CH_1_3_DONE)
3349c4a7965SKim Phillips 
3359c4a7965SKim Phillips /*
3369c4a7965SKim Phillips  * locate current (offending) descriptor
3379c4a7965SKim Phillips  */
3383e721aebSKim Phillips static u32 current_desc_hdr(struct device *dev, int ch)
3399c4a7965SKim Phillips {
3409c4a7965SKim Phillips 	struct talitos_private *priv = dev_get_drvdata(dev);
3414b992628SKim Phillips 	int tail = priv->chan[ch].tail;
3429c4a7965SKim Phillips 	dma_addr_t cur_desc;
3439c4a7965SKim Phillips 
344ad42d5fcSKim Phillips 	cur_desc = in_be32(priv->chan[ch].reg + TALITOS_CDPR_LO);
3459c4a7965SKim Phillips 
3464b992628SKim Phillips 	while (priv->chan[ch].fifo[tail].dma_desc != cur_desc) {
3479c4a7965SKim Phillips 		tail = (tail + 1) & (priv->fifo_len - 1);
3484b992628SKim Phillips 		if (tail == priv->chan[ch].tail) {
3499c4a7965SKim Phillips 			dev_err(dev, "couldn't locate current descriptor\n");
3503e721aebSKim Phillips 			return 0;
3519c4a7965SKim Phillips 		}
3529c4a7965SKim Phillips 	}
3539c4a7965SKim Phillips 
3543e721aebSKim Phillips 	return priv->chan[ch].fifo[tail].desc->hdr;
3559c4a7965SKim Phillips }
3569c4a7965SKim Phillips 
3579c4a7965SKim Phillips /*
3589c4a7965SKim Phillips  * user diagnostics; report root cause of error based on execution unit status
3599c4a7965SKim Phillips  */
3603e721aebSKim Phillips static void report_eu_error(struct device *dev, int ch, u32 desc_hdr)
3619c4a7965SKim Phillips {
3629c4a7965SKim Phillips 	struct talitos_private *priv = dev_get_drvdata(dev);
3639c4a7965SKim Phillips 	int i;
3649c4a7965SKim Phillips 
3653e721aebSKim Phillips 	if (!desc_hdr)
366ad42d5fcSKim Phillips 		desc_hdr = in_be32(priv->chan[ch].reg + TALITOS_DESCBUF);
3673e721aebSKim Phillips 
3683e721aebSKim Phillips 	switch (desc_hdr & DESC_HDR_SEL0_MASK) {
3699c4a7965SKim Phillips 	case DESC_HDR_SEL0_AFEU:
3709c4a7965SKim Phillips 		dev_err(dev, "AFEUISR 0x%08x_%08x\n",
3719c4a7965SKim Phillips 			in_be32(priv->reg + TALITOS_AFEUISR),
3729c4a7965SKim Phillips 			in_be32(priv->reg + TALITOS_AFEUISR_LO));
3739c4a7965SKim Phillips 		break;
3749c4a7965SKim Phillips 	case DESC_HDR_SEL0_DEU:
3759c4a7965SKim Phillips 		dev_err(dev, "DEUISR 0x%08x_%08x\n",
3769c4a7965SKim Phillips 			in_be32(priv->reg + TALITOS_DEUISR),
3779c4a7965SKim Phillips 			in_be32(priv->reg + TALITOS_DEUISR_LO));
3789c4a7965SKim Phillips 		break;
3799c4a7965SKim Phillips 	case DESC_HDR_SEL0_MDEUA:
3809c4a7965SKim Phillips 	case DESC_HDR_SEL0_MDEUB:
3819c4a7965SKim Phillips 		dev_err(dev, "MDEUISR 0x%08x_%08x\n",
3829c4a7965SKim Phillips 			in_be32(priv->reg + TALITOS_MDEUISR),
3839c4a7965SKim Phillips 			in_be32(priv->reg + TALITOS_MDEUISR_LO));
3849c4a7965SKim Phillips 		break;
3859c4a7965SKim Phillips 	case DESC_HDR_SEL0_RNG:
3869c4a7965SKim Phillips 		dev_err(dev, "RNGUISR 0x%08x_%08x\n",
3879c4a7965SKim Phillips 			in_be32(priv->reg + TALITOS_RNGUISR),
3889c4a7965SKim Phillips 			in_be32(priv->reg + TALITOS_RNGUISR_LO));
3899c4a7965SKim Phillips 		break;
3909c4a7965SKim Phillips 	case DESC_HDR_SEL0_PKEU:
3919c4a7965SKim Phillips 		dev_err(dev, "PKEUISR 0x%08x_%08x\n",
3929c4a7965SKim Phillips 			in_be32(priv->reg + TALITOS_PKEUISR),
3939c4a7965SKim Phillips 			in_be32(priv->reg + TALITOS_PKEUISR_LO));
3949c4a7965SKim Phillips 		break;
3959c4a7965SKim Phillips 	case DESC_HDR_SEL0_AESU:
3969c4a7965SKim Phillips 		dev_err(dev, "AESUISR 0x%08x_%08x\n",
3979c4a7965SKim Phillips 			in_be32(priv->reg + TALITOS_AESUISR),
3989c4a7965SKim Phillips 			in_be32(priv->reg + TALITOS_AESUISR_LO));
3999c4a7965SKim Phillips 		break;
4009c4a7965SKim Phillips 	case DESC_HDR_SEL0_CRCU:
4019c4a7965SKim Phillips 		dev_err(dev, "CRCUISR 0x%08x_%08x\n",
4029c4a7965SKim Phillips 			in_be32(priv->reg + TALITOS_CRCUISR),
4039c4a7965SKim Phillips 			in_be32(priv->reg + TALITOS_CRCUISR_LO));
4049c4a7965SKim Phillips 		break;
4059c4a7965SKim Phillips 	case DESC_HDR_SEL0_KEU:
4069c4a7965SKim Phillips 		dev_err(dev, "KEUISR 0x%08x_%08x\n",
4079c4a7965SKim Phillips 			in_be32(priv->reg + TALITOS_KEUISR),
4089c4a7965SKim Phillips 			in_be32(priv->reg + TALITOS_KEUISR_LO));
4099c4a7965SKim Phillips 		break;
4109c4a7965SKim Phillips 	}
4119c4a7965SKim Phillips 
4123e721aebSKim Phillips 	switch (desc_hdr & DESC_HDR_SEL1_MASK) {
4139c4a7965SKim Phillips 	case DESC_HDR_SEL1_MDEUA:
4149c4a7965SKim Phillips 	case DESC_HDR_SEL1_MDEUB:
4159c4a7965SKim Phillips 		dev_err(dev, "MDEUISR 0x%08x_%08x\n",
4169c4a7965SKim Phillips 			in_be32(priv->reg + TALITOS_MDEUISR),
4179c4a7965SKim Phillips 			in_be32(priv->reg + TALITOS_MDEUISR_LO));
4189c4a7965SKim Phillips 		break;
4199c4a7965SKim Phillips 	case DESC_HDR_SEL1_CRCU:
4209c4a7965SKim Phillips 		dev_err(dev, "CRCUISR 0x%08x_%08x\n",
4219c4a7965SKim Phillips 			in_be32(priv->reg + TALITOS_CRCUISR),
4229c4a7965SKim Phillips 			in_be32(priv->reg + TALITOS_CRCUISR_LO));
4239c4a7965SKim Phillips 		break;
4249c4a7965SKim Phillips 	}
4259c4a7965SKim Phillips 
4269c4a7965SKim Phillips 	for (i = 0; i < 8; i++)
4279c4a7965SKim Phillips 		dev_err(dev, "DESCBUF 0x%08x_%08x\n",
428ad42d5fcSKim Phillips 			in_be32(priv->chan[ch].reg + TALITOS_DESCBUF + 8*i),
429ad42d5fcSKim Phillips 			in_be32(priv->chan[ch].reg + TALITOS_DESCBUF_LO + 8*i));
4309c4a7965SKim Phillips }
4319c4a7965SKim Phillips 
4329c4a7965SKim Phillips /*
4339c4a7965SKim Phillips  * recover from error interrupts
4349c4a7965SKim Phillips  */
4355e718a09SKim Phillips static void talitos_error(struct device *dev, u32 isr, u32 isr_lo)
4369c4a7965SKim Phillips {
4379c4a7965SKim Phillips 	struct talitos_private *priv = dev_get_drvdata(dev);
4389c4a7965SKim Phillips 	unsigned int timeout = TALITOS_TIMEOUT;
4399c4a7965SKim Phillips 	int ch, error, reset_dev = 0, reset_ch = 0;
44040405f10SKim Phillips 	u32 v, v_lo;
4419c4a7965SKim Phillips 
4429c4a7965SKim Phillips 	for (ch = 0; ch < priv->num_channels; ch++) {
4439c4a7965SKim Phillips 		/* skip channels without errors */
4449c4a7965SKim Phillips 		if (!(isr & (1 << (ch * 2 + 1))))
4459c4a7965SKim Phillips 			continue;
4469c4a7965SKim Phillips 
4479c4a7965SKim Phillips 		error = -EINVAL;
4489c4a7965SKim Phillips 
449ad42d5fcSKim Phillips 		v = in_be32(priv->chan[ch].reg + TALITOS_CCPSR);
450ad42d5fcSKim Phillips 		v_lo = in_be32(priv->chan[ch].reg + TALITOS_CCPSR_LO);
4519c4a7965SKim Phillips 
4529c4a7965SKim Phillips 		if (v_lo & TALITOS_CCPSR_LO_DOF) {
4539c4a7965SKim Phillips 			dev_err(dev, "double fetch fifo overflow error\n");
4549c4a7965SKim Phillips 			error = -EAGAIN;
4559c4a7965SKim Phillips 			reset_ch = 1;
4569c4a7965SKim Phillips 		}
4579c4a7965SKim Phillips 		if (v_lo & TALITOS_CCPSR_LO_SOF) {
4589c4a7965SKim Phillips 			/* h/w dropped descriptor */
4599c4a7965SKim Phillips 			dev_err(dev, "single fetch fifo overflow error\n");
4609c4a7965SKim Phillips 			error = -EAGAIN;
4619c4a7965SKim Phillips 		}
4629c4a7965SKim Phillips 		if (v_lo & TALITOS_CCPSR_LO_MDTE)
4639c4a7965SKim Phillips 			dev_err(dev, "master data transfer error\n");
4649c4a7965SKim Phillips 		if (v_lo & TALITOS_CCPSR_LO_SGDLZ)
4659c4a7965SKim Phillips 			dev_err(dev, "s/g data length zero error\n");
4669c4a7965SKim Phillips 		if (v_lo & TALITOS_CCPSR_LO_FPZ)
4679c4a7965SKim Phillips 			dev_err(dev, "fetch pointer zero error\n");
4689c4a7965SKim Phillips 		if (v_lo & TALITOS_CCPSR_LO_IDH)
4699c4a7965SKim Phillips 			dev_err(dev, "illegal descriptor header error\n");
4709c4a7965SKim Phillips 		if (v_lo & TALITOS_CCPSR_LO_IEU)
4719c4a7965SKim Phillips 			dev_err(dev, "invalid execution unit error\n");
4729c4a7965SKim Phillips 		if (v_lo & TALITOS_CCPSR_LO_EU)
4733e721aebSKim Phillips 			report_eu_error(dev, ch, current_desc_hdr(dev, ch));
4749c4a7965SKim Phillips 		if (v_lo & TALITOS_CCPSR_LO_GB)
4759c4a7965SKim Phillips 			dev_err(dev, "gather boundary error\n");
4769c4a7965SKim Phillips 		if (v_lo & TALITOS_CCPSR_LO_GRL)
4779c4a7965SKim Phillips 			dev_err(dev, "gather return/length error\n");
4789c4a7965SKim Phillips 		if (v_lo & TALITOS_CCPSR_LO_SB)
4799c4a7965SKim Phillips 			dev_err(dev, "scatter boundary error\n");
4809c4a7965SKim Phillips 		if (v_lo & TALITOS_CCPSR_LO_SRL)
4819c4a7965SKim Phillips 			dev_err(dev, "scatter return/length error\n");
4829c4a7965SKim Phillips 
4839c4a7965SKim Phillips 		flush_channel(dev, ch, error, reset_ch);
4849c4a7965SKim Phillips 
4859c4a7965SKim Phillips 		if (reset_ch) {
4869c4a7965SKim Phillips 			reset_channel(dev, ch);
4879c4a7965SKim Phillips 		} else {
488ad42d5fcSKim Phillips 			setbits32(priv->chan[ch].reg + TALITOS_CCCR,
4899c4a7965SKim Phillips 				  TALITOS_CCCR_CONT);
490ad42d5fcSKim Phillips 			setbits32(priv->chan[ch].reg + TALITOS_CCCR_LO, 0);
491ad42d5fcSKim Phillips 			while ((in_be32(priv->chan[ch].reg + TALITOS_CCCR) &
4929c4a7965SKim Phillips 			       TALITOS_CCCR_CONT) && --timeout)
4939c4a7965SKim Phillips 				cpu_relax();
4949c4a7965SKim Phillips 			if (timeout == 0) {
4959c4a7965SKim Phillips 				dev_err(dev, "failed to restart channel %d\n",
4969c4a7965SKim Phillips 					ch);
4979c4a7965SKim Phillips 				reset_dev = 1;
4989c4a7965SKim Phillips 			}
4999c4a7965SKim Phillips 		}
5009c4a7965SKim Phillips 	}
501c3e337f8SKim Phillips 	if (reset_dev || isr & ~TALITOS_ISR_4CHERR || isr_lo) {
5029c4a7965SKim Phillips 		dev_err(dev, "done overflow, internal time out, or rngu error: "
5039c4a7965SKim Phillips 		        "ISR 0x%08x_%08x\n", isr, isr_lo);
5049c4a7965SKim Phillips 
5059c4a7965SKim Phillips 		/* purge request queues */
5069c4a7965SKim Phillips 		for (ch = 0; ch < priv->num_channels; ch++)
5079c4a7965SKim Phillips 			flush_channel(dev, ch, -EIO, 1);
5089c4a7965SKim Phillips 
5099c4a7965SKim Phillips 		/* reset and reinitialize the device */
5109c4a7965SKim Phillips 		init_device(dev);
5119c4a7965SKim Phillips 	}
5129c4a7965SKim Phillips }
5139c4a7965SKim Phillips 
514c3e337f8SKim Phillips #define DEF_TALITOS_INTERRUPT(name, ch_done_mask, ch_err_mask, tlet)	       \
515c3e337f8SKim Phillips static irqreturn_t talitos_interrupt_##name(int irq, void *data)	       \
516c3e337f8SKim Phillips {									       \
517c3e337f8SKim Phillips 	struct device *dev = data;					       \
518c3e337f8SKim Phillips 	struct talitos_private *priv = dev_get_drvdata(dev);		       \
519c3e337f8SKim Phillips 	u32 isr, isr_lo;						       \
520511d63cbSHoria Geanta 	unsigned long flags;						       \
521c3e337f8SKim Phillips 									       \
522511d63cbSHoria Geanta 	spin_lock_irqsave(&priv->reg_lock, flags);			       \
523c3e337f8SKim Phillips 	isr = in_be32(priv->reg + TALITOS_ISR);				       \
524c3e337f8SKim Phillips 	isr_lo = in_be32(priv->reg + TALITOS_ISR_LO);			       \
525c3e337f8SKim Phillips 	/* Acknowledge interrupt */					       \
526c3e337f8SKim Phillips 	out_be32(priv->reg + TALITOS_ICR, isr & (ch_done_mask | ch_err_mask)); \
527c3e337f8SKim Phillips 	out_be32(priv->reg + TALITOS_ICR_LO, isr_lo);			       \
528c3e337f8SKim Phillips 									       \
529511d63cbSHoria Geanta 	if (unlikely(isr & ch_err_mask || isr_lo)) {			       \
530511d63cbSHoria Geanta 		spin_unlock_irqrestore(&priv->reg_lock, flags);		       \
531511d63cbSHoria Geanta 		talitos_error(dev, isr & ch_err_mask, isr_lo);		       \
532511d63cbSHoria Geanta 	}								       \
533511d63cbSHoria Geanta 	else {								       \
534c3e337f8SKim Phillips 		if (likely(isr & ch_done_mask)) {			       \
535c3e337f8SKim Phillips 			/* mask further done interrupts. */		       \
536c3e337f8SKim Phillips 			clrbits32(priv->reg + TALITOS_IMR, ch_done_mask);      \
537c3e337f8SKim Phillips 			/* done_task will unmask done interrupts at exit */    \
538c3e337f8SKim Phillips 			tasklet_schedule(&priv->done_task[tlet]);	       \
539c3e337f8SKim Phillips 		}							       \
540511d63cbSHoria Geanta 		spin_unlock_irqrestore(&priv->reg_lock, flags);		       \
541511d63cbSHoria Geanta 	}								       \
542c3e337f8SKim Phillips 									       \
543c3e337f8SKim Phillips 	return (isr & (ch_done_mask | ch_err_mask) || isr_lo) ? IRQ_HANDLED :  \
544c3e337f8SKim Phillips 								IRQ_NONE;      \
5451c2e8811SLee Nipper }
546c3e337f8SKim Phillips DEF_TALITOS_INTERRUPT(4ch, TALITOS_ISR_4CHDONE, TALITOS_ISR_4CHERR, 0)
547c3e337f8SKim Phillips DEF_TALITOS_INTERRUPT(ch0_2, TALITOS_ISR_CH_0_2_DONE, TALITOS_ISR_CH_0_2_ERR, 0)
548c3e337f8SKim Phillips DEF_TALITOS_INTERRUPT(ch1_3, TALITOS_ISR_CH_1_3_DONE, TALITOS_ISR_CH_1_3_ERR, 1)
5499c4a7965SKim Phillips 
5509c4a7965SKim Phillips /*
5519c4a7965SKim Phillips  * hwrng
5529c4a7965SKim Phillips  */
5539c4a7965SKim Phillips static int talitos_rng_data_present(struct hwrng *rng, int wait)
5549c4a7965SKim Phillips {
5559c4a7965SKim Phillips 	struct device *dev = (struct device *)rng->priv;
5569c4a7965SKim Phillips 	struct talitos_private *priv = dev_get_drvdata(dev);
5579c4a7965SKim Phillips 	u32 ofl;
5589c4a7965SKim Phillips 	int i;
5599c4a7965SKim Phillips 
5609c4a7965SKim Phillips 	for (i = 0; i < 20; i++) {
5619c4a7965SKim Phillips 		ofl = in_be32(priv->reg + TALITOS_RNGUSR_LO) &
5629c4a7965SKim Phillips 		      TALITOS_RNGUSR_LO_OFL;
5639c4a7965SKim Phillips 		if (ofl || !wait)
5649c4a7965SKim Phillips 			break;
5659c4a7965SKim Phillips 		udelay(10);
5669c4a7965SKim Phillips 	}
5679c4a7965SKim Phillips 
5689c4a7965SKim Phillips 	return !!ofl;
5699c4a7965SKim Phillips }
5709c4a7965SKim Phillips 
5719c4a7965SKim Phillips static int talitos_rng_data_read(struct hwrng *rng, u32 *data)
5729c4a7965SKim Phillips {
5739c4a7965SKim Phillips 	struct device *dev = (struct device *)rng->priv;
5749c4a7965SKim Phillips 	struct talitos_private *priv = dev_get_drvdata(dev);
5759c4a7965SKim Phillips 
5769c4a7965SKim Phillips 	/* rng fifo requires 64-bit accesses */
5779c4a7965SKim Phillips 	*data = in_be32(priv->reg + TALITOS_RNGU_FIFO);
5789c4a7965SKim Phillips 	*data = in_be32(priv->reg + TALITOS_RNGU_FIFO_LO);
5799c4a7965SKim Phillips 
5809c4a7965SKim Phillips 	return sizeof(u32);
5819c4a7965SKim Phillips }
5829c4a7965SKim Phillips 
5839c4a7965SKim Phillips static int talitos_rng_init(struct hwrng *rng)
5849c4a7965SKim Phillips {
5859c4a7965SKim Phillips 	struct device *dev = (struct device *)rng->priv;
5869c4a7965SKim Phillips 	struct talitos_private *priv = dev_get_drvdata(dev);
5879c4a7965SKim Phillips 	unsigned int timeout = TALITOS_TIMEOUT;
5889c4a7965SKim Phillips 
5899c4a7965SKim Phillips 	setbits32(priv->reg + TALITOS_RNGURCR_LO, TALITOS_RNGURCR_LO_SR);
5909c4a7965SKim Phillips 	while (!(in_be32(priv->reg + TALITOS_RNGUSR_LO) & TALITOS_RNGUSR_LO_RD)
5919c4a7965SKim Phillips 	       && --timeout)
5929c4a7965SKim Phillips 		cpu_relax();
5939c4a7965SKim Phillips 	if (timeout == 0) {
5949c4a7965SKim Phillips 		dev_err(dev, "failed to reset rng hw\n");
5959c4a7965SKim Phillips 		return -ENODEV;
5969c4a7965SKim Phillips 	}
5979c4a7965SKim Phillips 
5989c4a7965SKim Phillips 	/* start generating */
5999c4a7965SKim Phillips 	setbits32(priv->reg + TALITOS_RNGUDSR_LO, 0);
6009c4a7965SKim Phillips 
6019c4a7965SKim Phillips 	return 0;
6029c4a7965SKim Phillips }
6039c4a7965SKim Phillips 
6049c4a7965SKim Phillips static int talitos_register_rng(struct device *dev)
6059c4a7965SKim Phillips {
6069c4a7965SKim Phillips 	struct talitos_private *priv = dev_get_drvdata(dev);
6079c4a7965SKim Phillips 
6089c4a7965SKim Phillips 	priv->rng.name		= dev_driver_string(dev),
6099c4a7965SKim Phillips 	priv->rng.init		= talitos_rng_init,
6109c4a7965SKim Phillips 	priv->rng.data_present	= talitos_rng_data_present,
6119c4a7965SKim Phillips 	priv->rng.data_read	= talitos_rng_data_read,
6129c4a7965SKim Phillips 	priv->rng.priv		= (unsigned long)dev;
6139c4a7965SKim Phillips 
6149c4a7965SKim Phillips 	return hwrng_register(&priv->rng);
6159c4a7965SKim Phillips }
6169c4a7965SKim Phillips 
6179c4a7965SKim Phillips static void talitos_unregister_rng(struct device *dev)
6189c4a7965SKim Phillips {
6199c4a7965SKim Phillips 	struct talitos_private *priv = dev_get_drvdata(dev);
6209c4a7965SKim Phillips 
6219c4a7965SKim Phillips 	hwrng_unregister(&priv->rng);
6229c4a7965SKim Phillips }
6239c4a7965SKim Phillips 
6249c4a7965SKim Phillips /*
6259c4a7965SKim Phillips  * crypto alg
6269c4a7965SKim Phillips  */
6279c4a7965SKim Phillips #define TALITOS_CRA_PRIORITY		3000
628357fb605SHoria Geanta #define TALITOS_MAX_KEY_SIZE		96
6293952f17eSLee Nipper #define TALITOS_MAX_IV_LENGTH		16 /* max of AES_BLOCK_SIZE, DES3_EDE_BLOCK_SIZE */
63070bcaca7SLee Nipper 
631497f2e6bSLee Nipper #define MD5_BLOCK_SIZE    64
6329c4a7965SKim Phillips 
6339c4a7965SKim Phillips struct talitos_ctx {
6349c4a7965SKim Phillips 	struct device *dev;
6355228f0f7SKim Phillips 	int ch;
6369c4a7965SKim Phillips 	__be32 desc_hdr_template;
6379c4a7965SKim Phillips 	u8 key[TALITOS_MAX_KEY_SIZE];
63870bcaca7SLee Nipper 	u8 iv[TALITOS_MAX_IV_LENGTH];
6399c4a7965SKim Phillips 	unsigned int keylen;
6409c4a7965SKim Phillips 	unsigned int enckeylen;
6419c4a7965SKim Phillips 	unsigned int authkeylen;
6429c4a7965SKim Phillips 	unsigned int authsize;
6439c4a7965SKim Phillips };
6449c4a7965SKim Phillips 
645497f2e6bSLee Nipper #define HASH_MAX_BLOCK_SIZE		SHA512_BLOCK_SIZE
646497f2e6bSLee Nipper #define TALITOS_MDEU_MAX_CONTEXT_SIZE	TALITOS_MDEU_CONTEXT_SIZE_SHA384_SHA512
647497f2e6bSLee Nipper 
648497f2e6bSLee Nipper struct talitos_ahash_req_ctx {
64960f208d7SKim Phillips 	u32 hw_context[TALITOS_MDEU_MAX_CONTEXT_SIZE / sizeof(u32)];
650497f2e6bSLee Nipper 	unsigned int hw_context_size;
651497f2e6bSLee Nipper 	u8 buf[HASH_MAX_BLOCK_SIZE];
652497f2e6bSLee Nipper 	u8 bufnext[HASH_MAX_BLOCK_SIZE];
65360f208d7SKim Phillips 	unsigned int swinit;
654497f2e6bSLee Nipper 	unsigned int first;
655497f2e6bSLee Nipper 	unsigned int last;
656497f2e6bSLee Nipper 	unsigned int to_hash_later;
6575e833bc4SLee Nipper 	u64 nbuf;
658497f2e6bSLee Nipper 	struct scatterlist bufsl[2];
659497f2e6bSLee Nipper 	struct scatterlist *psrc;
660497f2e6bSLee Nipper };
661497f2e6bSLee Nipper 
66256af8cd4SLee Nipper static int aead_setauthsize(struct crypto_aead *authenc,
6639c4a7965SKim Phillips 			    unsigned int authsize)
6649c4a7965SKim Phillips {
6659c4a7965SKim Phillips 	struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
6669c4a7965SKim Phillips 
6679c4a7965SKim Phillips 	ctx->authsize = authsize;
6689c4a7965SKim Phillips 
6699c4a7965SKim Phillips 	return 0;
6709c4a7965SKim Phillips }
6719c4a7965SKim Phillips 
67256af8cd4SLee Nipper static int aead_setkey(struct crypto_aead *authenc,
6739c4a7965SKim Phillips 		       const u8 *key, unsigned int keylen)
6749c4a7965SKim Phillips {
6759c4a7965SKim Phillips 	struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
6769c4a7965SKim Phillips 	struct rtattr *rta = (void *)key;
6779c4a7965SKim Phillips 	struct crypto_authenc_key_param *param;
6789c4a7965SKim Phillips 	unsigned int authkeylen;
6799c4a7965SKim Phillips 	unsigned int enckeylen;
6809c4a7965SKim Phillips 
6819c4a7965SKim Phillips 	if (!RTA_OK(rta, keylen))
6829c4a7965SKim Phillips 		goto badkey;
6839c4a7965SKim Phillips 
6849c4a7965SKim Phillips 	if (rta->rta_type != CRYPTO_AUTHENC_KEYA_PARAM)
6859c4a7965SKim Phillips 		goto badkey;
6869c4a7965SKim Phillips 
6879c4a7965SKim Phillips 	if (RTA_PAYLOAD(rta) < sizeof(*param))
6889c4a7965SKim Phillips 		goto badkey;
6899c4a7965SKim Phillips 
6909c4a7965SKim Phillips 	param = RTA_DATA(rta);
6919c4a7965SKim Phillips 	enckeylen = be32_to_cpu(param->enckeylen);
6929c4a7965SKim Phillips 
6939c4a7965SKim Phillips 	key += RTA_ALIGN(rta->rta_len);
6949c4a7965SKim Phillips 	keylen -= RTA_ALIGN(rta->rta_len);
6959c4a7965SKim Phillips 
6969c4a7965SKim Phillips 	if (keylen < enckeylen)
6979c4a7965SKim Phillips 		goto badkey;
6989c4a7965SKim Phillips 
6999c4a7965SKim Phillips 	authkeylen = keylen - enckeylen;
7009c4a7965SKim Phillips 
7019c4a7965SKim Phillips 	if (keylen > TALITOS_MAX_KEY_SIZE)
7029c4a7965SKim Phillips 		goto badkey;
7039c4a7965SKim Phillips 
7049c4a7965SKim Phillips 	memcpy(&ctx->key, key, keylen);
7059c4a7965SKim Phillips 
7069c4a7965SKim Phillips 	ctx->keylen = keylen;
7079c4a7965SKim Phillips 	ctx->enckeylen = enckeylen;
7089c4a7965SKim Phillips 	ctx->authkeylen = authkeylen;
7099c4a7965SKim Phillips 
7109c4a7965SKim Phillips 	return 0;
7119c4a7965SKim Phillips 
7129c4a7965SKim Phillips badkey:
7139c4a7965SKim Phillips 	crypto_aead_set_flags(authenc, CRYPTO_TFM_RES_BAD_KEY_LEN);
7149c4a7965SKim Phillips 	return -EINVAL;
7159c4a7965SKim Phillips }
7169c4a7965SKim Phillips 
7179c4a7965SKim Phillips /*
71856af8cd4SLee Nipper  * talitos_edesc - s/w-extended descriptor
71979fd31d3SHoria Geanta  * @assoc_nents: number of segments in associated data scatterlist
7209c4a7965SKim Phillips  * @src_nents: number of segments in input scatterlist
7219c4a7965SKim Phillips  * @dst_nents: number of segments in output scatterlist
72279fd31d3SHoria Geanta  * @assoc_chained: whether assoc is chained or not
7232a1cfe46SHoria Geanta  * @src_chained: whether src is chained or not
7242a1cfe46SHoria Geanta  * @dst_chained: whether dst is chained or not
72579fd31d3SHoria Geanta  * @iv_dma: dma address of iv for checking continuity and link table
7269c4a7965SKim Phillips  * @dma_len: length of dma mapped link_tbl space
7279c4a7965SKim Phillips  * @dma_link_tbl: bus physical address of link_tbl
7289c4a7965SKim Phillips  * @desc: h/w descriptor
7299c4a7965SKim Phillips  * @link_tbl: input and output h/w link tables (if {src,dst}_nents > 1)
7309c4a7965SKim Phillips  *
7319c4a7965SKim Phillips  * if decrypting (with authcheck), or either one of src_nents or dst_nents
7329c4a7965SKim Phillips  * is greater than 1, an integrity check value is concatenated to the end
7339c4a7965SKim Phillips  * of link_tbl data
7349c4a7965SKim Phillips  */
73556af8cd4SLee Nipper struct talitos_edesc {
73679fd31d3SHoria Geanta 	int assoc_nents;
7379c4a7965SKim Phillips 	int src_nents;
7389c4a7965SKim Phillips 	int dst_nents;
73979fd31d3SHoria Geanta 	bool assoc_chained;
7402a1cfe46SHoria Geanta 	bool src_chained;
7412a1cfe46SHoria Geanta 	bool dst_chained;
74279fd31d3SHoria Geanta 	dma_addr_t iv_dma;
7439c4a7965SKim Phillips 	int dma_len;
7449c4a7965SKim Phillips 	dma_addr_t dma_link_tbl;
7459c4a7965SKim Phillips 	struct talitos_desc desc;
7469c4a7965SKim Phillips 	struct talitos_ptr link_tbl[0];
7479c4a7965SKim Phillips };
7489c4a7965SKim Phillips 
7494de9d0b5SLee Nipper static int talitos_map_sg(struct device *dev, struct scatterlist *sg,
7504de9d0b5SLee Nipper 			  unsigned int nents, enum dma_data_direction dir,
7512a1cfe46SHoria Geanta 			  bool chained)
7524de9d0b5SLee Nipper {
7534de9d0b5SLee Nipper 	if (unlikely(chained))
7544de9d0b5SLee Nipper 		while (sg) {
7554de9d0b5SLee Nipper 			dma_map_sg(dev, sg, 1, dir);
7564de9d0b5SLee Nipper 			sg = scatterwalk_sg_next(sg);
7574de9d0b5SLee Nipper 		}
7584de9d0b5SLee Nipper 	else
7594de9d0b5SLee Nipper 		dma_map_sg(dev, sg, nents, dir);
7604de9d0b5SLee Nipper 	return nents;
7614de9d0b5SLee Nipper }
7624de9d0b5SLee Nipper 
7634de9d0b5SLee Nipper static void talitos_unmap_sg_chain(struct device *dev, struct scatterlist *sg,
7644de9d0b5SLee Nipper 				   enum dma_data_direction dir)
7654de9d0b5SLee Nipper {
7664de9d0b5SLee Nipper 	while (sg) {
7674de9d0b5SLee Nipper 		dma_unmap_sg(dev, sg, 1, dir);
7684de9d0b5SLee Nipper 		sg = scatterwalk_sg_next(sg);
7694de9d0b5SLee Nipper 	}
7704de9d0b5SLee Nipper }
7714de9d0b5SLee Nipper 
7724de9d0b5SLee Nipper static void talitos_sg_unmap(struct device *dev,
7734de9d0b5SLee Nipper 			     struct talitos_edesc *edesc,
7744de9d0b5SLee Nipper 			     struct scatterlist *src,
7754de9d0b5SLee Nipper 			     struct scatterlist *dst)
7764de9d0b5SLee Nipper {
7774de9d0b5SLee Nipper 	unsigned int src_nents = edesc->src_nents ? : 1;
7784de9d0b5SLee Nipper 	unsigned int dst_nents = edesc->dst_nents ? : 1;
7794de9d0b5SLee Nipper 
7804de9d0b5SLee Nipper 	if (src != dst) {
7812a1cfe46SHoria Geanta 		if (edesc->src_chained)
7824de9d0b5SLee Nipper 			talitos_unmap_sg_chain(dev, src, DMA_TO_DEVICE);
7834de9d0b5SLee Nipper 		else
7844de9d0b5SLee Nipper 			dma_unmap_sg(dev, src, src_nents, DMA_TO_DEVICE);
7854de9d0b5SLee Nipper 
786497f2e6bSLee Nipper 		if (dst) {
7872a1cfe46SHoria Geanta 			if (edesc->dst_chained)
788497f2e6bSLee Nipper 				talitos_unmap_sg_chain(dev, dst,
789497f2e6bSLee Nipper 						       DMA_FROM_DEVICE);
7904de9d0b5SLee Nipper 			else
791497f2e6bSLee Nipper 				dma_unmap_sg(dev, dst, dst_nents,
792497f2e6bSLee Nipper 					     DMA_FROM_DEVICE);
793497f2e6bSLee Nipper 		}
7944de9d0b5SLee Nipper 	} else
7952a1cfe46SHoria Geanta 		if (edesc->src_chained)
7964de9d0b5SLee Nipper 			talitos_unmap_sg_chain(dev, src, DMA_BIDIRECTIONAL);
7974de9d0b5SLee Nipper 		else
7984de9d0b5SLee Nipper 			dma_unmap_sg(dev, src, src_nents, DMA_BIDIRECTIONAL);
7994de9d0b5SLee Nipper }
8004de9d0b5SLee Nipper 
8019c4a7965SKim Phillips static void ipsec_esp_unmap(struct device *dev,
80256af8cd4SLee Nipper 			    struct talitos_edesc *edesc,
8039c4a7965SKim Phillips 			    struct aead_request *areq)
8049c4a7965SKim Phillips {
8059c4a7965SKim Phillips 	unmap_single_talitos_ptr(dev, &edesc->desc.ptr[6], DMA_FROM_DEVICE);
8069c4a7965SKim Phillips 	unmap_single_talitos_ptr(dev, &edesc->desc.ptr[3], DMA_TO_DEVICE);
8079c4a7965SKim Phillips 	unmap_single_talitos_ptr(dev, &edesc->desc.ptr[2], DMA_TO_DEVICE);
8089c4a7965SKim Phillips 	unmap_single_talitos_ptr(dev, &edesc->desc.ptr[0], DMA_TO_DEVICE);
8099c4a7965SKim Phillips 
81079fd31d3SHoria Geanta 	if (edesc->assoc_chained)
81179fd31d3SHoria Geanta 		talitos_unmap_sg_chain(dev, areq->assoc, DMA_TO_DEVICE);
81279fd31d3SHoria Geanta 	else
81379fd31d3SHoria Geanta 		/* assoc_nents counts also for IV in non-contiguous cases */
81479fd31d3SHoria Geanta 		dma_unmap_sg(dev, areq->assoc,
81579fd31d3SHoria Geanta 			     edesc->assoc_nents ? edesc->assoc_nents - 1 : 1,
81679fd31d3SHoria Geanta 			     DMA_TO_DEVICE);
8179c4a7965SKim Phillips 
8184de9d0b5SLee Nipper 	talitos_sg_unmap(dev, edesc, areq->src, areq->dst);
8199c4a7965SKim Phillips 
8209c4a7965SKim Phillips 	if (edesc->dma_len)
8219c4a7965SKim Phillips 		dma_unmap_single(dev, edesc->dma_link_tbl, edesc->dma_len,
8229c4a7965SKim Phillips 				 DMA_BIDIRECTIONAL);
8239c4a7965SKim Phillips }
8249c4a7965SKim Phillips 
8259c4a7965SKim Phillips /*
8269c4a7965SKim Phillips  * ipsec_esp descriptor callbacks
8279c4a7965SKim Phillips  */
8289c4a7965SKim Phillips static void ipsec_esp_encrypt_done(struct device *dev,
8299c4a7965SKim Phillips 				   struct talitos_desc *desc, void *context,
8309c4a7965SKim Phillips 				   int err)
8319c4a7965SKim Phillips {
8329c4a7965SKim Phillips 	struct aead_request *areq = context;
8339c4a7965SKim Phillips 	struct crypto_aead *authenc = crypto_aead_reqtfm(areq);
8349c4a7965SKim Phillips 	struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
83519bbbc63SKim Phillips 	struct talitos_edesc *edesc;
8369c4a7965SKim Phillips 	struct scatterlist *sg;
8379c4a7965SKim Phillips 	void *icvdata;
8389c4a7965SKim Phillips 
83919bbbc63SKim Phillips 	edesc = container_of(desc, struct talitos_edesc, desc);
84019bbbc63SKim Phillips 
8419c4a7965SKim Phillips 	ipsec_esp_unmap(dev, edesc, areq);
8429c4a7965SKim Phillips 
8439c4a7965SKim Phillips 	/* copy the generated ICV to dst */
84460542505SHoria Geanta 	if (edesc->dst_nents) {
8459c4a7965SKim Phillips 		icvdata = &edesc->link_tbl[edesc->src_nents +
84679fd31d3SHoria Geanta 					   edesc->dst_nents + 2 +
84779fd31d3SHoria Geanta 					   edesc->assoc_nents];
8489c4a7965SKim Phillips 		sg = sg_last(areq->dst, edesc->dst_nents);
8499c4a7965SKim Phillips 		memcpy((char *)sg_virt(sg) + sg->length - ctx->authsize,
8509c4a7965SKim Phillips 		       icvdata, ctx->authsize);
8519c4a7965SKim Phillips 	}
8529c4a7965SKim Phillips 
8539c4a7965SKim Phillips 	kfree(edesc);
8549c4a7965SKim Phillips 
8559c4a7965SKim Phillips 	aead_request_complete(areq, err);
8569c4a7965SKim Phillips }
8579c4a7965SKim Phillips 
858fe5720e2SKim Phillips static void ipsec_esp_decrypt_swauth_done(struct device *dev,
859e938e465SKim Phillips 					  struct talitos_desc *desc,
860e938e465SKim Phillips 					  void *context, int err)
8619c4a7965SKim Phillips {
8629c4a7965SKim Phillips 	struct aead_request *req = context;
8639c4a7965SKim Phillips 	struct crypto_aead *authenc = crypto_aead_reqtfm(req);
8649c4a7965SKim Phillips 	struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
86519bbbc63SKim Phillips 	struct talitos_edesc *edesc;
8669c4a7965SKim Phillips 	struct scatterlist *sg;
8679c4a7965SKim Phillips 	void *icvdata;
8689c4a7965SKim Phillips 
86919bbbc63SKim Phillips 	edesc = container_of(desc, struct talitos_edesc, desc);
87019bbbc63SKim Phillips 
8719c4a7965SKim Phillips 	ipsec_esp_unmap(dev, edesc, req);
8729c4a7965SKim Phillips 
8739c4a7965SKim Phillips 	if (!err) {
8749c4a7965SKim Phillips 		/* auth check */
8759c4a7965SKim Phillips 		if (edesc->dma_len)
8769c4a7965SKim Phillips 			icvdata = &edesc->link_tbl[edesc->src_nents +
87779fd31d3SHoria Geanta 						   edesc->dst_nents + 2 +
87879fd31d3SHoria Geanta 						   edesc->assoc_nents];
8799c4a7965SKim Phillips 		else
8809c4a7965SKim Phillips 			icvdata = &edesc->link_tbl[0];
8819c4a7965SKim Phillips 
8829c4a7965SKim Phillips 		sg = sg_last(req->dst, edesc->dst_nents ? : 1);
8839c4a7965SKim Phillips 		err = memcmp(icvdata, (char *)sg_virt(sg) + sg->length -
8849c4a7965SKim Phillips 			     ctx->authsize, ctx->authsize) ? -EBADMSG : 0;
8859c4a7965SKim Phillips 	}
8869c4a7965SKim Phillips 
8879c4a7965SKim Phillips 	kfree(edesc);
8889c4a7965SKim Phillips 
8899c4a7965SKim Phillips 	aead_request_complete(req, err);
8909c4a7965SKim Phillips }
8919c4a7965SKim Phillips 
892fe5720e2SKim Phillips static void ipsec_esp_decrypt_hwauth_done(struct device *dev,
893e938e465SKim Phillips 					  struct talitos_desc *desc,
894e938e465SKim Phillips 					  void *context, int err)
895fe5720e2SKim Phillips {
896fe5720e2SKim Phillips 	struct aead_request *req = context;
89719bbbc63SKim Phillips 	struct talitos_edesc *edesc;
89819bbbc63SKim Phillips 
89919bbbc63SKim Phillips 	edesc = container_of(desc, struct talitos_edesc, desc);
900fe5720e2SKim Phillips 
901fe5720e2SKim Phillips 	ipsec_esp_unmap(dev, edesc, req);
902fe5720e2SKim Phillips 
903fe5720e2SKim Phillips 	/* check ICV auth status */
904e938e465SKim Phillips 	if (!err && ((desc->hdr_lo & DESC_HDR_LO_ICCR1_MASK) !=
905e938e465SKim Phillips 		     DESC_HDR_LO_ICCR1_PASS))
906fe5720e2SKim Phillips 		err = -EBADMSG;
907fe5720e2SKim Phillips 
908fe5720e2SKim Phillips 	kfree(edesc);
909fe5720e2SKim Phillips 
910fe5720e2SKim Phillips 	aead_request_complete(req, err);
911fe5720e2SKim Phillips }
912fe5720e2SKim Phillips 
9139c4a7965SKim Phillips /*
9149c4a7965SKim Phillips  * convert scatterlist to SEC h/w link table format
9159c4a7965SKim Phillips  * stop at cryptlen bytes
9169c4a7965SKim Phillips  */
91770bcaca7SLee Nipper static int sg_to_link_tbl(struct scatterlist *sg, int sg_count,
9189c4a7965SKim Phillips 			   int cryptlen, struct talitos_ptr *link_tbl_ptr)
9199c4a7965SKim Phillips {
92070bcaca7SLee Nipper 	int n_sg = sg_count;
92170bcaca7SLee Nipper 
92270bcaca7SLee Nipper 	while (n_sg--) {
92381eb024cSKim Phillips 		to_talitos_ptr(link_tbl_ptr, sg_dma_address(sg));
9249c4a7965SKim Phillips 		link_tbl_ptr->len = cpu_to_be16(sg_dma_len(sg));
9259c4a7965SKim Phillips 		link_tbl_ptr->j_extent = 0;
9269c4a7965SKim Phillips 		link_tbl_ptr++;
9279c4a7965SKim Phillips 		cryptlen -= sg_dma_len(sg);
9284de9d0b5SLee Nipper 		sg = scatterwalk_sg_next(sg);
9299c4a7965SKim Phillips 	}
9309c4a7965SKim Phillips 
93170bcaca7SLee Nipper 	/* adjust (decrease) last one (or two) entry's len to cryptlen */
9329c4a7965SKim Phillips 	link_tbl_ptr--;
933c0e741d4SKim Phillips 	while (be16_to_cpu(link_tbl_ptr->len) <= (-cryptlen)) {
93470bcaca7SLee Nipper 		/* Empty this entry, and move to previous one */
93570bcaca7SLee Nipper 		cryptlen += be16_to_cpu(link_tbl_ptr->len);
93670bcaca7SLee Nipper 		link_tbl_ptr->len = 0;
93770bcaca7SLee Nipper 		sg_count--;
93870bcaca7SLee Nipper 		link_tbl_ptr--;
93970bcaca7SLee Nipper 	}
9407291a932SWei Yongjun 	be16_add_cpu(&link_tbl_ptr->len, cryptlen);
9419c4a7965SKim Phillips 
9429c4a7965SKim Phillips 	/* tag end of link table */
9439c4a7965SKim Phillips 	link_tbl_ptr->j_extent = DESC_PTR_LNKTBL_RETURN;
94470bcaca7SLee Nipper 
94570bcaca7SLee Nipper 	return sg_count;
9469c4a7965SKim Phillips }
9479c4a7965SKim Phillips 
9489c4a7965SKim Phillips /*
9499c4a7965SKim Phillips  * fill in and submit ipsec_esp descriptor
9509c4a7965SKim Phillips  */
95156af8cd4SLee Nipper static int ipsec_esp(struct talitos_edesc *edesc, struct aead_request *areq,
95279fd31d3SHoria Geanta 		     u64 seq, void (*callback) (struct device *dev,
9539c4a7965SKim Phillips 						struct talitos_desc *desc,
9549c4a7965SKim Phillips 						void *context, int error))
9559c4a7965SKim Phillips {
9569c4a7965SKim Phillips 	struct crypto_aead *aead = crypto_aead_reqtfm(areq);
9579c4a7965SKim Phillips 	struct talitos_ctx *ctx = crypto_aead_ctx(aead);
9589c4a7965SKim Phillips 	struct device *dev = ctx->dev;
9599c4a7965SKim Phillips 	struct talitos_desc *desc = &edesc->desc;
9609c4a7965SKim Phillips 	unsigned int cryptlen = areq->cryptlen;
9619c4a7965SKim Phillips 	unsigned int authsize = ctx->authsize;
962e41256f1SKim Phillips 	unsigned int ivsize = crypto_aead_ivsize(aead);
963fa86a267SKim Phillips 	int sg_count, ret;
964fe5720e2SKim Phillips 	int sg_link_tbl_len;
9659c4a7965SKim Phillips 
9669c4a7965SKim Phillips 	/* hmac key */
9679c4a7965SKim Phillips 	map_single_talitos_ptr(dev, &desc->ptr[0], ctx->authkeylen, &ctx->key,
9689c4a7965SKim Phillips 			       0, DMA_TO_DEVICE);
96979fd31d3SHoria Geanta 
9709c4a7965SKim Phillips 	/* hmac data */
97179fd31d3SHoria Geanta 	desc->ptr[1].len = cpu_to_be16(areq->assoclen + ivsize);
97279fd31d3SHoria Geanta 	if (edesc->assoc_nents) {
97379fd31d3SHoria Geanta 		int tbl_off = edesc->src_nents + edesc->dst_nents + 2;
97479fd31d3SHoria Geanta 		struct talitos_ptr *tbl_ptr = &edesc->link_tbl[tbl_off];
97579fd31d3SHoria Geanta 
97679fd31d3SHoria Geanta 		to_talitos_ptr(&desc->ptr[1], edesc->dma_link_tbl + tbl_off *
97779fd31d3SHoria Geanta 			       sizeof(struct talitos_ptr));
97879fd31d3SHoria Geanta 		desc->ptr[1].j_extent = DESC_PTR_LNKTBL_JUMP;
97979fd31d3SHoria Geanta 
98079fd31d3SHoria Geanta 		/* assoc_nents - 1 entries for assoc, 1 for IV */
98179fd31d3SHoria Geanta 		sg_count = sg_to_link_tbl(areq->assoc, edesc->assoc_nents - 1,
98279fd31d3SHoria Geanta 					  areq->assoclen, tbl_ptr);
98379fd31d3SHoria Geanta 
98479fd31d3SHoria Geanta 		/* add IV to link table */
98579fd31d3SHoria Geanta 		tbl_ptr += sg_count - 1;
98679fd31d3SHoria Geanta 		tbl_ptr->j_extent = 0;
98779fd31d3SHoria Geanta 		tbl_ptr++;
98879fd31d3SHoria Geanta 		to_talitos_ptr(tbl_ptr, edesc->iv_dma);
98979fd31d3SHoria Geanta 		tbl_ptr->len = cpu_to_be16(ivsize);
99079fd31d3SHoria Geanta 		tbl_ptr->j_extent = DESC_PTR_LNKTBL_RETURN;
99179fd31d3SHoria Geanta 
99279fd31d3SHoria Geanta 		dma_sync_single_for_device(dev, edesc->dma_link_tbl,
99379fd31d3SHoria Geanta 					   edesc->dma_len, DMA_BIDIRECTIONAL);
99479fd31d3SHoria Geanta 	} else {
99579fd31d3SHoria Geanta 		to_talitos_ptr(&desc->ptr[1], sg_dma_address(areq->assoc));
99679fd31d3SHoria Geanta 		desc->ptr[1].j_extent = 0;
99779fd31d3SHoria Geanta 	}
99879fd31d3SHoria Geanta 
9999c4a7965SKim Phillips 	/* cipher iv */
100079fd31d3SHoria Geanta 	to_talitos_ptr(&desc->ptr[2], edesc->iv_dma);
100179fd31d3SHoria Geanta 	desc->ptr[2].len = cpu_to_be16(ivsize);
100279fd31d3SHoria Geanta 	desc->ptr[2].j_extent = 0;
100379fd31d3SHoria Geanta 	/* Sync needed for the aead_givencrypt case */
100479fd31d3SHoria Geanta 	dma_sync_single_for_device(dev, edesc->iv_dma, ivsize, DMA_TO_DEVICE);
10059c4a7965SKim Phillips 
10069c4a7965SKim Phillips 	/* cipher key */
10079c4a7965SKim Phillips 	map_single_talitos_ptr(dev, &desc->ptr[3], ctx->enckeylen,
10089c4a7965SKim Phillips 			       (char *)&ctx->key + ctx->authkeylen, 0,
10099c4a7965SKim Phillips 			       DMA_TO_DEVICE);
10109c4a7965SKim Phillips 
10119c4a7965SKim Phillips 	/*
10129c4a7965SKim Phillips 	 * cipher in
10139c4a7965SKim Phillips 	 * map and adjust cipher len to aead request cryptlen.
10149c4a7965SKim Phillips 	 * extent is bytes of HMAC postpended to ciphertext,
10159c4a7965SKim Phillips 	 * typically 12 for ipsec
10169c4a7965SKim Phillips 	 */
10179c4a7965SKim Phillips 	desc->ptr[4].len = cpu_to_be16(cryptlen);
10189c4a7965SKim Phillips 	desc->ptr[4].j_extent = authsize;
10199c4a7965SKim Phillips 
1020e938e465SKim Phillips 	sg_count = talitos_map_sg(dev, areq->src, edesc->src_nents ? : 1,
1021e938e465SKim Phillips 				  (areq->src == areq->dst) ? DMA_BIDIRECTIONAL
1022e938e465SKim Phillips 							   : DMA_TO_DEVICE,
10232a1cfe46SHoria Geanta 				  edesc->src_chained);
10249c4a7965SKim Phillips 
10259c4a7965SKim Phillips 	if (sg_count == 1) {
102681eb024cSKim Phillips 		to_talitos_ptr(&desc->ptr[4], sg_dma_address(areq->src));
10279c4a7965SKim Phillips 	} else {
1028fe5720e2SKim Phillips 		sg_link_tbl_len = cryptlen;
1029fe5720e2SKim Phillips 
1030962a9c99SKim Phillips 		if (edesc->desc.hdr & DESC_HDR_MODE1_MDEU_CICV)
1031fe5720e2SKim Phillips 			sg_link_tbl_len = cryptlen + authsize;
1032e938e465SKim Phillips 
1033fe5720e2SKim Phillips 		sg_count = sg_to_link_tbl(areq->src, sg_count, sg_link_tbl_len,
10349c4a7965SKim Phillips 					  &edesc->link_tbl[0]);
103570bcaca7SLee Nipper 		if (sg_count > 1) {
10369c4a7965SKim Phillips 			desc->ptr[4].j_extent |= DESC_PTR_LNKTBL_JUMP;
103781eb024cSKim Phillips 			to_talitos_ptr(&desc->ptr[4], edesc->dma_link_tbl);
1038e938e465SKim Phillips 			dma_sync_single_for_device(dev, edesc->dma_link_tbl,
1039e938e465SKim Phillips 						   edesc->dma_len,
1040e938e465SKim Phillips 						   DMA_BIDIRECTIONAL);
104170bcaca7SLee Nipper 		} else {
104270bcaca7SLee Nipper 			/* Only one segment now, so no link tbl needed */
104381eb024cSKim Phillips 			to_talitos_ptr(&desc->ptr[4],
104481eb024cSKim Phillips 				       sg_dma_address(areq->src));
104570bcaca7SLee Nipper 		}
10469c4a7965SKim Phillips 	}
10479c4a7965SKim Phillips 
10489c4a7965SKim Phillips 	/* cipher out */
10499c4a7965SKim Phillips 	desc->ptr[5].len = cpu_to_be16(cryptlen);
10509c4a7965SKim Phillips 	desc->ptr[5].j_extent = authsize;
10519c4a7965SKim Phillips 
1052e938e465SKim Phillips 	if (areq->src != areq->dst)
10534de9d0b5SLee Nipper 		sg_count = talitos_map_sg(dev, areq->dst,
10544de9d0b5SLee Nipper 					  edesc->dst_nents ? : 1,
10552a1cfe46SHoria Geanta 					  DMA_FROM_DEVICE, edesc->dst_chained);
10569c4a7965SKim Phillips 
10579c4a7965SKim Phillips 	if (sg_count == 1) {
105881eb024cSKim Phillips 		to_talitos_ptr(&desc->ptr[5], sg_dma_address(areq->dst));
10599c4a7965SKim Phillips 	} else {
106079fd31d3SHoria Geanta 		int tbl_off = edesc->src_nents + 1;
106179fd31d3SHoria Geanta 		struct talitos_ptr *tbl_ptr = &edesc->link_tbl[tbl_off];
10629c4a7965SKim Phillips 
106381eb024cSKim Phillips 		to_talitos_ptr(&desc->ptr[5], edesc->dma_link_tbl +
106479fd31d3SHoria Geanta 			       tbl_off * sizeof(struct talitos_ptr));
106570bcaca7SLee Nipper 		sg_count = sg_to_link_tbl(areq->dst, sg_count, cryptlen,
106679fd31d3SHoria Geanta 					  tbl_ptr);
1067fe5720e2SKim Phillips 
1068f3c85bc1SLee Nipper 		/* Add an entry to the link table for ICV data */
106979fd31d3SHoria Geanta 		tbl_ptr += sg_count - 1;
107079fd31d3SHoria Geanta 		tbl_ptr->j_extent = 0;
107179fd31d3SHoria Geanta 		tbl_ptr++;
107279fd31d3SHoria Geanta 		tbl_ptr->j_extent = DESC_PTR_LNKTBL_RETURN;
107379fd31d3SHoria Geanta 		tbl_ptr->len = cpu_to_be16(authsize);
10749c4a7965SKim Phillips 
10759c4a7965SKim Phillips 		/* icv data follows link tables */
107679fd31d3SHoria Geanta 		to_talitos_ptr(tbl_ptr, edesc->dma_link_tbl +
107779fd31d3SHoria Geanta 			       (tbl_off + edesc->dst_nents + 1 +
107879fd31d3SHoria Geanta 				edesc->assoc_nents) *
107981eb024cSKim Phillips 			       sizeof(struct talitos_ptr));
10809c4a7965SKim Phillips 		desc->ptr[5].j_extent |= DESC_PTR_LNKTBL_JUMP;
10819c4a7965SKim Phillips 		dma_sync_single_for_device(ctx->dev, edesc->dma_link_tbl,
10829c4a7965SKim Phillips 					   edesc->dma_len, DMA_BIDIRECTIONAL);
10839c4a7965SKim Phillips 	}
10849c4a7965SKim Phillips 
10859c4a7965SKim Phillips 	/* iv out */
10869c4a7965SKim Phillips 	map_single_talitos_ptr(dev, &desc->ptr[6], ivsize, ctx->iv, 0,
10879c4a7965SKim Phillips 			       DMA_FROM_DEVICE);
10889c4a7965SKim Phillips 
10895228f0f7SKim Phillips 	ret = talitos_submit(dev, ctx->ch, desc, callback, areq);
1090fa86a267SKim Phillips 	if (ret != -EINPROGRESS) {
1091fa86a267SKim Phillips 		ipsec_esp_unmap(dev, edesc, areq);
1092fa86a267SKim Phillips 		kfree(edesc);
1093fa86a267SKim Phillips 	}
1094fa86a267SKim Phillips 	return ret;
10959c4a7965SKim Phillips }
10969c4a7965SKim Phillips 
10979c4a7965SKim Phillips /*
10989c4a7965SKim Phillips  * derive number of elements in scatterlist
10999c4a7965SKim Phillips  */
11002a1cfe46SHoria Geanta static int sg_count(struct scatterlist *sg_list, int nbytes, bool *chained)
11019c4a7965SKim Phillips {
11029c4a7965SKim Phillips 	struct scatterlist *sg = sg_list;
11039c4a7965SKim Phillips 	int sg_nents = 0;
11049c4a7965SKim Phillips 
11052a1cfe46SHoria Geanta 	*chained = false;
11064de9d0b5SLee Nipper 	while (nbytes > 0) {
11079c4a7965SKim Phillips 		sg_nents++;
11089c4a7965SKim Phillips 		nbytes -= sg->length;
11094de9d0b5SLee Nipper 		if (!sg_is_last(sg) && (sg + 1)->length == 0)
11102a1cfe46SHoria Geanta 			*chained = true;
11114de9d0b5SLee Nipper 		sg = scatterwalk_sg_next(sg);
11129c4a7965SKim Phillips 	}
11139c4a7965SKim Phillips 
11149c4a7965SKim Phillips 	return sg_nents;
11159c4a7965SKim Phillips }
11169c4a7965SKim Phillips 
11179c4a7965SKim Phillips /*
111856af8cd4SLee Nipper  * allocate and map the extended descriptor
11199c4a7965SKim Phillips  */
11204de9d0b5SLee Nipper static struct talitos_edesc *talitos_edesc_alloc(struct device *dev,
112179fd31d3SHoria Geanta 						 struct scatterlist *assoc,
11224de9d0b5SLee Nipper 						 struct scatterlist *src,
11234de9d0b5SLee Nipper 						 struct scatterlist *dst,
112479fd31d3SHoria Geanta 						 u8 *iv,
112579fd31d3SHoria Geanta 						 unsigned int assoclen,
11264de9d0b5SLee Nipper 						 unsigned int cryptlen,
11274de9d0b5SLee Nipper 						 unsigned int authsize,
112879fd31d3SHoria Geanta 						 unsigned int ivsize,
11294de9d0b5SLee Nipper 						 int icv_stashing,
11304de9d0b5SLee Nipper 						 u32 cryptoflags)
11319c4a7965SKim Phillips {
113256af8cd4SLee Nipper 	struct talitos_edesc *edesc;
113379fd31d3SHoria Geanta 	int assoc_nents = 0, src_nents, dst_nents, alloc_len, dma_len;
113479fd31d3SHoria Geanta 	bool assoc_chained = false, src_chained = false, dst_chained = false;
113579fd31d3SHoria Geanta 	dma_addr_t iv_dma = 0;
11364de9d0b5SLee Nipper 	gfp_t flags = cryptoflags & CRYPTO_TFM_REQ_MAY_SLEEP ? GFP_KERNEL :
1137586725f8SKim Phillips 		      GFP_ATOMIC;
11389c4a7965SKim Phillips 
11394de9d0b5SLee Nipper 	if (cryptlen + authsize > TALITOS_MAX_DATA_LEN) {
11404de9d0b5SLee Nipper 		dev_err(dev, "length exceeds h/w max limit\n");
11419c4a7965SKim Phillips 		return ERR_PTR(-EINVAL);
11429c4a7965SKim Phillips 	}
11439c4a7965SKim Phillips 
114479fd31d3SHoria Geanta 	if (iv)
114579fd31d3SHoria Geanta 		iv_dma = dma_map_single(dev, iv, ivsize, DMA_TO_DEVICE);
114679fd31d3SHoria Geanta 
114779fd31d3SHoria Geanta 	if (assoc) {
114879fd31d3SHoria Geanta 		/*
114979fd31d3SHoria Geanta 		 * Currently it is assumed that iv is provided whenever assoc
115079fd31d3SHoria Geanta 		 * is.
115179fd31d3SHoria Geanta 		 */
115279fd31d3SHoria Geanta 		BUG_ON(!iv);
115379fd31d3SHoria Geanta 
115479fd31d3SHoria Geanta 		assoc_nents = sg_count(assoc, assoclen, &assoc_chained);
115579fd31d3SHoria Geanta 		talitos_map_sg(dev, assoc, assoc_nents, DMA_TO_DEVICE,
115679fd31d3SHoria Geanta 			       assoc_chained);
115779fd31d3SHoria Geanta 		assoc_nents = (assoc_nents == 1) ? 0 : assoc_nents;
115879fd31d3SHoria Geanta 
115979fd31d3SHoria Geanta 		if (assoc_nents || sg_dma_address(assoc) + assoclen != iv_dma)
116079fd31d3SHoria Geanta 			assoc_nents = assoc_nents ? assoc_nents + 1 : 2;
116179fd31d3SHoria Geanta 	}
116279fd31d3SHoria Geanta 
11634de9d0b5SLee Nipper 	src_nents = sg_count(src, cryptlen + authsize, &src_chained);
11649c4a7965SKim Phillips 	src_nents = (src_nents == 1) ? 0 : src_nents;
11659c4a7965SKim Phillips 
1166602499a3SHoria Geanta 	if (!dst) {
1167497f2e6bSLee Nipper 		dst_nents = 0;
1168497f2e6bSLee Nipper 	} else {
11694de9d0b5SLee Nipper 		if (dst == src) {
11709c4a7965SKim Phillips 			dst_nents = src_nents;
11719c4a7965SKim Phillips 		} else {
1172497f2e6bSLee Nipper 			dst_nents = sg_count(dst, cryptlen + authsize,
1173497f2e6bSLee Nipper 					     &dst_chained);
1174695ad589SLee Nipper 			dst_nents = (dst_nents == 1) ? 0 : dst_nents;
11759c4a7965SKim Phillips 		}
1176497f2e6bSLee Nipper 	}
11779c4a7965SKim Phillips 
11789c4a7965SKim Phillips 	/*
11799c4a7965SKim Phillips 	 * allocate space for base edesc plus the link tables,
1180f3c85bc1SLee Nipper 	 * allowing for two separate entries for ICV and generated ICV (+ 2),
11819c4a7965SKim Phillips 	 * and the ICV data itself
11829c4a7965SKim Phillips 	 */
118356af8cd4SLee Nipper 	alloc_len = sizeof(struct talitos_edesc);
118479fd31d3SHoria Geanta 	if (assoc_nents || src_nents || dst_nents) {
118579fd31d3SHoria Geanta 		dma_len = (src_nents + dst_nents + 2 + assoc_nents) *
11864de9d0b5SLee Nipper 			  sizeof(struct talitos_ptr) + authsize;
11879c4a7965SKim Phillips 		alloc_len += dma_len;
11889c4a7965SKim Phillips 	} else {
11899c4a7965SKim Phillips 		dma_len = 0;
11904de9d0b5SLee Nipper 		alloc_len += icv_stashing ? authsize : 0;
11919c4a7965SKim Phillips 	}
11929c4a7965SKim Phillips 
1193586725f8SKim Phillips 	edesc = kmalloc(alloc_len, GFP_DMA | flags);
11949c4a7965SKim Phillips 	if (!edesc) {
119579fd31d3SHoria Geanta 		talitos_unmap_sg_chain(dev, assoc, DMA_TO_DEVICE);
119679fd31d3SHoria Geanta 		if (iv_dma)
119779fd31d3SHoria Geanta 			dma_unmap_single(dev, iv_dma, ivsize, DMA_TO_DEVICE);
11984de9d0b5SLee Nipper 		dev_err(dev, "could not allocate edescriptor\n");
11999c4a7965SKim Phillips 		return ERR_PTR(-ENOMEM);
12009c4a7965SKim Phillips 	}
12019c4a7965SKim Phillips 
120279fd31d3SHoria Geanta 	edesc->assoc_nents = assoc_nents;
12039c4a7965SKim Phillips 	edesc->src_nents = src_nents;
12049c4a7965SKim Phillips 	edesc->dst_nents = dst_nents;
120579fd31d3SHoria Geanta 	edesc->assoc_chained = assoc_chained;
12062a1cfe46SHoria Geanta 	edesc->src_chained = src_chained;
12072a1cfe46SHoria Geanta 	edesc->dst_chained = dst_chained;
120879fd31d3SHoria Geanta 	edesc->iv_dma = iv_dma;
12099c4a7965SKim Phillips 	edesc->dma_len = dma_len;
1210497f2e6bSLee Nipper 	if (dma_len)
12114de9d0b5SLee Nipper 		edesc->dma_link_tbl = dma_map_single(dev, &edesc->link_tbl[0],
1212497f2e6bSLee Nipper 						     edesc->dma_len,
1213497f2e6bSLee Nipper 						     DMA_BIDIRECTIONAL);
12149c4a7965SKim Phillips 
12159c4a7965SKim Phillips 	return edesc;
12169c4a7965SKim Phillips }
12179c4a7965SKim Phillips 
121879fd31d3SHoria Geanta static struct talitos_edesc *aead_edesc_alloc(struct aead_request *areq, u8 *iv,
12194de9d0b5SLee Nipper 					      int icv_stashing)
12204de9d0b5SLee Nipper {
12214de9d0b5SLee Nipper 	struct crypto_aead *authenc = crypto_aead_reqtfm(areq);
12224de9d0b5SLee Nipper 	struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
122379fd31d3SHoria Geanta 	unsigned int ivsize = crypto_aead_ivsize(authenc);
12244de9d0b5SLee Nipper 
122579fd31d3SHoria Geanta 	return talitos_edesc_alloc(ctx->dev, areq->assoc, areq->src, areq->dst,
122679fd31d3SHoria Geanta 				   iv, areq->assoclen, areq->cryptlen,
122779fd31d3SHoria Geanta 				   ctx->authsize, ivsize, icv_stashing,
12284de9d0b5SLee Nipper 				   areq->base.flags);
12294de9d0b5SLee Nipper }
12304de9d0b5SLee Nipper 
123156af8cd4SLee Nipper static int aead_encrypt(struct aead_request *req)
12329c4a7965SKim Phillips {
12339c4a7965SKim Phillips 	struct crypto_aead *authenc = crypto_aead_reqtfm(req);
12349c4a7965SKim Phillips 	struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
123556af8cd4SLee Nipper 	struct talitos_edesc *edesc;
12369c4a7965SKim Phillips 
12379c4a7965SKim Phillips 	/* allocate extended descriptor */
123879fd31d3SHoria Geanta 	edesc = aead_edesc_alloc(req, req->iv, 0);
12399c4a7965SKim Phillips 	if (IS_ERR(edesc))
12409c4a7965SKim Phillips 		return PTR_ERR(edesc);
12419c4a7965SKim Phillips 
12429c4a7965SKim Phillips 	/* set encrypt */
124370bcaca7SLee Nipper 	edesc->desc.hdr = ctx->desc_hdr_template | DESC_HDR_MODE0_ENCRYPT;
12449c4a7965SKim Phillips 
124579fd31d3SHoria Geanta 	return ipsec_esp(edesc, req, 0, ipsec_esp_encrypt_done);
12469c4a7965SKim Phillips }
12479c4a7965SKim Phillips 
124856af8cd4SLee Nipper static int aead_decrypt(struct aead_request *req)
12499c4a7965SKim Phillips {
12509c4a7965SKim Phillips 	struct crypto_aead *authenc = crypto_aead_reqtfm(req);
12519c4a7965SKim Phillips 	struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
12529c4a7965SKim Phillips 	unsigned int authsize = ctx->authsize;
1253fe5720e2SKim Phillips 	struct talitos_private *priv = dev_get_drvdata(ctx->dev);
125456af8cd4SLee Nipper 	struct talitos_edesc *edesc;
12559c4a7965SKim Phillips 	struct scatterlist *sg;
12569c4a7965SKim Phillips 	void *icvdata;
12579c4a7965SKim Phillips 
12589c4a7965SKim Phillips 	req->cryptlen -= authsize;
12599c4a7965SKim Phillips 
12609c4a7965SKim Phillips 	/* allocate extended descriptor */
126179fd31d3SHoria Geanta 	edesc = aead_edesc_alloc(req, req->iv, 1);
12629c4a7965SKim Phillips 	if (IS_ERR(edesc))
12639c4a7965SKim Phillips 		return PTR_ERR(edesc);
12649c4a7965SKim Phillips 
1265fe5720e2SKim Phillips 	if ((priv->features & TALITOS_FTR_HW_AUTH_CHECK) &&
1266e938e465SKim Phillips 	    ((!edesc->src_nents && !edesc->dst_nents) ||
1267e938e465SKim Phillips 	     priv->features & TALITOS_FTR_SRC_LINK_TBL_LEN_INCLUDES_EXTENT)) {
1268fe5720e2SKim Phillips 
1269fe5720e2SKim Phillips 		/* decrypt and check the ICV */
1270e938e465SKim Phillips 		edesc->desc.hdr = ctx->desc_hdr_template |
1271e938e465SKim Phillips 				  DESC_HDR_DIR_INBOUND |
1272fe5720e2SKim Phillips 				  DESC_HDR_MODE1_MDEU_CICV;
1273fe5720e2SKim Phillips 
1274fe5720e2SKim Phillips 		/* reset integrity check result bits */
1275fe5720e2SKim Phillips 		edesc->desc.hdr_lo = 0;
1276fe5720e2SKim Phillips 
127779fd31d3SHoria Geanta 		return ipsec_esp(edesc, req, 0, ipsec_esp_decrypt_hwauth_done);
1278e938e465SKim Phillips 	}
1279fe5720e2SKim Phillips 
1280fe5720e2SKim Phillips 	/* Have to check the ICV with software */
1281fe5720e2SKim Phillips 	edesc->desc.hdr = ctx->desc_hdr_template | DESC_HDR_DIR_INBOUND;
1282fe5720e2SKim Phillips 
12839c4a7965SKim Phillips 	/* stash incoming ICV for later cmp with ICV generated by the h/w */
12849c4a7965SKim Phillips 	if (edesc->dma_len)
12859c4a7965SKim Phillips 		icvdata = &edesc->link_tbl[edesc->src_nents +
128679fd31d3SHoria Geanta 					   edesc->dst_nents + 2 +
128779fd31d3SHoria Geanta 					   edesc->assoc_nents];
12889c4a7965SKim Phillips 	else
12899c4a7965SKim Phillips 		icvdata = &edesc->link_tbl[0];
12909c4a7965SKim Phillips 
12919c4a7965SKim Phillips 	sg = sg_last(req->src, edesc->src_nents ? : 1);
12929c4a7965SKim Phillips 
12939c4a7965SKim Phillips 	memcpy(icvdata, (char *)sg_virt(sg) + sg->length - ctx->authsize,
12949c4a7965SKim Phillips 	       ctx->authsize);
12959c4a7965SKim Phillips 
129679fd31d3SHoria Geanta 	return ipsec_esp(edesc, req, 0, ipsec_esp_decrypt_swauth_done);
1297fe5720e2SKim Phillips }
12989c4a7965SKim Phillips 
129956af8cd4SLee Nipper static int aead_givencrypt(struct aead_givcrypt_request *req)
13009c4a7965SKim Phillips {
13019c4a7965SKim Phillips 	struct aead_request *areq = &req->areq;
13029c4a7965SKim Phillips 	struct crypto_aead *authenc = crypto_aead_reqtfm(areq);
13039c4a7965SKim Phillips 	struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
130456af8cd4SLee Nipper 	struct talitos_edesc *edesc;
13059c4a7965SKim Phillips 
13069c4a7965SKim Phillips 	/* allocate extended descriptor */
130779fd31d3SHoria Geanta 	edesc = aead_edesc_alloc(areq, req->giv, 0);
13089c4a7965SKim Phillips 	if (IS_ERR(edesc))
13099c4a7965SKim Phillips 		return PTR_ERR(edesc);
13109c4a7965SKim Phillips 
13119c4a7965SKim Phillips 	/* set encrypt */
131270bcaca7SLee Nipper 	edesc->desc.hdr = ctx->desc_hdr_template | DESC_HDR_MODE0_ENCRYPT;
13139c4a7965SKim Phillips 
13149c4a7965SKim Phillips 	memcpy(req->giv, ctx->iv, crypto_aead_ivsize(authenc));
1315ba95487dSKim Phillips 	/* avoid consecutive packets going out with same IV */
1316ba95487dSKim Phillips 	*(__be64 *)req->giv ^= cpu_to_be64(req->seq);
13179c4a7965SKim Phillips 
131879fd31d3SHoria Geanta 	return ipsec_esp(edesc, areq, req->seq, ipsec_esp_encrypt_done);
13199c4a7965SKim Phillips }
13209c4a7965SKim Phillips 
13214de9d0b5SLee Nipper static int ablkcipher_setkey(struct crypto_ablkcipher *cipher,
13224de9d0b5SLee Nipper 			     const u8 *key, unsigned int keylen)
13234de9d0b5SLee Nipper {
13244de9d0b5SLee Nipper 	struct talitos_ctx *ctx = crypto_ablkcipher_ctx(cipher);
13254de9d0b5SLee Nipper 
13264de9d0b5SLee Nipper 	memcpy(&ctx->key, key, keylen);
13274de9d0b5SLee Nipper 	ctx->keylen = keylen;
13284de9d0b5SLee Nipper 
13294de9d0b5SLee Nipper 	return 0;
13304de9d0b5SLee Nipper }
13314de9d0b5SLee Nipper 
13324de9d0b5SLee Nipper static void common_nonsnoop_unmap(struct device *dev,
13334de9d0b5SLee Nipper 				  struct talitos_edesc *edesc,
13344de9d0b5SLee Nipper 				  struct ablkcipher_request *areq)
13354de9d0b5SLee Nipper {
13364de9d0b5SLee Nipper 	unmap_single_talitos_ptr(dev, &edesc->desc.ptr[5], DMA_FROM_DEVICE);
13374de9d0b5SLee Nipper 	unmap_single_talitos_ptr(dev, &edesc->desc.ptr[2], DMA_TO_DEVICE);
13384de9d0b5SLee Nipper 	unmap_single_talitos_ptr(dev, &edesc->desc.ptr[1], DMA_TO_DEVICE);
13394de9d0b5SLee Nipper 
13404de9d0b5SLee Nipper 	talitos_sg_unmap(dev, edesc, areq->src, areq->dst);
13414de9d0b5SLee Nipper 
13424de9d0b5SLee Nipper 	if (edesc->dma_len)
13434de9d0b5SLee Nipper 		dma_unmap_single(dev, edesc->dma_link_tbl, edesc->dma_len,
13444de9d0b5SLee Nipper 				 DMA_BIDIRECTIONAL);
13454de9d0b5SLee Nipper }
13464de9d0b5SLee Nipper 
13474de9d0b5SLee Nipper static void ablkcipher_done(struct device *dev,
13484de9d0b5SLee Nipper 			    struct talitos_desc *desc, void *context,
13494de9d0b5SLee Nipper 			    int err)
13504de9d0b5SLee Nipper {
13514de9d0b5SLee Nipper 	struct ablkcipher_request *areq = context;
135219bbbc63SKim Phillips 	struct talitos_edesc *edesc;
135319bbbc63SKim Phillips 
135419bbbc63SKim Phillips 	edesc = container_of(desc, struct talitos_edesc, desc);
13554de9d0b5SLee Nipper 
13564de9d0b5SLee Nipper 	common_nonsnoop_unmap(dev, edesc, areq);
13574de9d0b5SLee Nipper 
13584de9d0b5SLee Nipper 	kfree(edesc);
13594de9d0b5SLee Nipper 
13604de9d0b5SLee Nipper 	areq->base.complete(&areq->base, err);
13614de9d0b5SLee Nipper }
13624de9d0b5SLee Nipper 
13634de9d0b5SLee Nipper static int common_nonsnoop(struct talitos_edesc *edesc,
13644de9d0b5SLee Nipper 			   struct ablkcipher_request *areq,
13654de9d0b5SLee Nipper 			   void (*callback) (struct device *dev,
13664de9d0b5SLee Nipper 					     struct talitos_desc *desc,
13674de9d0b5SLee Nipper 					     void *context, int error))
13684de9d0b5SLee Nipper {
13694de9d0b5SLee Nipper 	struct crypto_ablkcipher *cipher = crypto_ablkcipher_reqtfm(areq);
13704de9d0b5SLee Nipper 	struct talitos_ctx *ctx = crypto_ablkcipher_ctx(cipher);
13714de9d0b5SLee Nipper 	struct device *dev = ctx->dev;
13724de9d0b5SLee Nipper 	struct talitos_desc *desc = &edesc->desc;
13734de9d0b5SLee Nipper 	unsigned int cryptlen = areq->nbytes;
137479fd31d3SHoria Geanta 	unsigned int ivsize = crypto_ablkcipher_ivsize(cipher);
13754de9d0b5SLee Nipper 	int sg_count, ret;
13764de9d0b5SLee Nipper 
13774de9d0b5SLee Nipper 	/* first DWORD empty */
13784de9d0b5SLee Nipper 	desc->ptr[0].len = 0;
137981eb024cSKim Phillips 	to_talitos_ptr(&desc->ptr[0], 0);
13804de9d0b5SLee Nipper 	desc->ptr[0].j_extent = 0;
13814de9d0b5SLee Nipper 
13824de9d0b5SLee Nipper 	/* cipher iv */
138379fd31d3SHoria Geanta 	to_talitos_ptr(&desc->ptr[1], edesc->iv_dma);
138479fd31d3SHoria Geanta 	desc->ptr[1].len = cpu_to_be16(ivsize);
138579fd31d3SHoria Geanta 	desc->ptr[1].j_extent = 0;
13864de9d0b5SLee Nipper 
13874de9d0b5SLee Nipper 	/* cipher key */
13884de9d0b5SLee Nipper 	map_single_talitos_ptr(dev, &desc->ptr[2], ctx->keylen,
13894de9d0b5SLee Nipper 			       (char *)&ctx->key, 0, DMA_TO_DEVICE);
13904de9d0b5SLee Nipper 
13914de9d0b5SLee Nipper 	/*
13924de9d0b5SLee Nipper 	 * cipher in
13934de9d0b5SLee Nipper 	 */
13944de9d0b5SLee Nipper 	desc->ptr[3].len = cpu_to_be16(cryptlen);
13954de9d0b5SLee Nipper 	desc->ptr[3].j_extent = 0;
13964de9d0b5SLee Nipper 
13974de9d0b5SLee Nipper 	sg_count = talitos_map_sg(dev, areq->src, edesc->src_nents ? : 1,
13984de9d0b5SLee Nipper 				  (areq->src == areq->dst) ? DMA_BIDIRECTIONAL
13994de9d0b5SLee Nipper 							   : DMA_TO_DEVICE,
14002a1cfe46SHoria Geanta 				  edesc->src_chained);
14014de9d0b5SLee Nipper 
14024de9d0b5SLee Nipper 	if (sg_count == 1) {
140381eb024cSKim Phillips 		to_talitos_ptr(&desc->ptr[3], sg_dma_address(areq->src));
14044de9d0b5SLee Nipper 	} else {
14054de9d0b5SLee Nipper 		sg_count = sg_to_link_tbl(areq->src, sg_count, cryptlen,
14064de9d0b5SLee Nipper 					  &edesc->link_tbl[0]);
14074de9d0b5SLee Nipper 		if (sg_count > 1) {
140881eb024cSKim Phillips 			to_talitos_ptr(&desc->ptr[3], edesc->dma_link_tbl);
14094de9d0b5SLee Nipper 			desc->ptr[3].j_extent |= DESC_PTR_LNKTBL_JUMP;
1410e938e465SKim Phillips 			dma_sync_single_for_device(dev, edesc->dma_link_tbl,
1411e938e465SKim Phillips 						   edesc->dma_len,
1412e938e465SKim Phillips 						   DMA_BIDIRECTIONAL);
14134de9d0b5SLee Nipper 		} else {
14144de9d0b5SLee Nipper 			/* Only one segment now, so no link tbl needed */
141581eb024cSKim Phillips 			to_talitos_ptr(&desc->ptr[3],
141681eb024cSKim Phillips 				       sg_dma_address(areq->src));
14174de9d0b5SLee Nipper 		}
14184de9d0b5SLee Nipper 	}
14194de9d0b5SLee Nipper 
14204de9d0b5SLee Nipper 	/* cipher out */
14214de9d0b5SLee Nipper 	desc->ptr[4].len = cpu_to_be16(cryptlen);
14224de9d0b5SLee Nipper 	desc->ptr[4].j_extent = 0;
14234de9d0b5SLee Nipper 
14244de9d0b5SLee Nipper 	if (areq->src != areq->dst)
14254de9d0b5SLee Nipper 		sg_count = talitos_map_sg(dev, areq->dst,
14264de9d0b5SLee Nipper 					  edesc->dst_nents ? : 1,
14272a1cfe46SHoria Geanta 					  DMA_FROM_DEVICE, edesc->dst_chained);
14284de9d0b5SLee Nipper 
14294de9d0b5SLee Nipper 	if (sg_count == 1) {
143081eb024cSKim Phillips 		to_talitos_ptr(&desc->ptr[4], sg_dma_address(areq->dst));
14314de9d0b5SLee Nipper 	} else {
14324de9d0b5SLee Nipper 		struct talitos_ptr *link_tbl_ptr =
14334de9d0b5SLee Nipper 			&edesc->link_tbl[edesc->src_nents + 1];
14344de9d0b5SLee Nipper 
143581eb024cSKim Phillips 		to_talitos_ptr(&desc->ptr[4], edesc->dma_link_tbl +
143681eb024cSKim Phillips 					      (edesc->src_nents + 1) *
143781eb024cSKim Phillips 					      sizeof(struct talitos_ptr));
14384de9d0b5SLee Nipper 		desc->ptr[4].j_extent |= DESC_PTR_LNKTBL_JUMP;
14394de9d0b5SLee Nipper 		sg_count = sg_to_link_tbl(areq->dst, sg_count, cryptlen,
14404de9d0b5SLee Nipper 					  link_tbl_ptr);
14414de9d0b5SLee Nipper 		dma_sync_single_for_device(ctx->dev, edesc->dma_link_tbl,
14424de9d0b5SLee Nipper 					   edesc->dma_len, DMA_BIDIRECTIONAL);
14434de9d0b5SLee Nipper 	}
14444de9d0b5SLee Nipper 
14454de9d0b5SLee Nipper 	/* iv out */
14464de9d0b5SLee Nipper 	map_single_talitos_ptr(dev, &desc->ptr[5], ivsize, ctx->iv, 0,
14474de9d0b5SLee Nipper 			       DMA_FROM_DEVICE);
14484de9d0b5SLee Nipper 
14494de9d0b5SLee Nipper 	/* last DWORD empty */
14504de9d0b5SLee Nipper 	desc->ptr[6].len = 0;
145181eb024cSKim Phillips 	to_talitos_ptr(&desc->ptr[6], 0);
14524de9d0b5SLee Nipper 	desc->ptr[6].j_extent = 0;
14534de9d0b5SLee Nipper 
14545228f0f7SKim Phillips 	ret = talitos_submit(dev, ctx->ch, desc, callback, areq);
14554de9d0b5SLee Nipper 	if (ret != -EINPROGRESS) {
14564de9d0b5SLee Nipper 		common_nonsnoop_unmap(dev, edesc, areq);
14574de9d0b5SLee Nipper 		kfree(edesc);
14584de9d0b5SLee Nipper 	}
14594de9d0b5SLee Nipper 	return ret;
14604de9d0b5SLee Nipper }
14614de9d0b5SLee Nipper 
1462e938e465SKim Phillips static struct talitos_edesc *ablkcipher_edesc_alloc(struct ablkcipher_request *
1463e938e465SKim Phillips 						    areq)
14644de9d0b5SLee Nipper {
14654de9d0b5SLee Nipper 	struct crypto_ablkcipher *cipher = crypto_ablkcipher_reqtfm(areq);
14664de9d0b5SLee Nipper 	struct talitos_ctx *ctx = crypto_ablkcipher_ctx(cipher);
146779fd31d3SHoria Geanta 	unsigned int ivsize = crypto_ablkcipher_ivsize(cipher);
14684de9d0b5SLee Nipper 
146979fd31d3SHoria Geanta 	return talitos_edesc_alloc(ctx->dev, NULL, areq->src, areq->dst,
147079fd31d3SHoria Geanta 				   areq->info, 0, areq->nbytes, 0, ivsize, 0,
147179fd31d3SHoria Geanta 				   areq->base.flags);
14724de9d0b5SLee Nipper }
14734de9d0b5SLee Nipper 
14744de9d0b5SLee Nipper static int ablkcipher_encrypt(struct ablkcipher_request *areq)
14754de9d0b5SLee Nipper {
14764de9d0b5SLee Nipper 	struct crypto_ablkcipher *cipher = crypto_ablkcipher_reqtfm(areq);
14774de9d0b5SLee Nipper 	struct talitos_ctx *ctx = crypto_ablkcipher_ctx(cipher);
14784de9d0b5SLee Nipper 	struct talitos_edesc *edesc;
14794de9d0b5SLee Nipper 
14804de9d0b5SLee Nipper 	/* allocate extended descriptor */
14814de9d0b5SLee Nipper 	edesc = ablkcipher_edesc_alloc(areq);
14824de9d0b5SLee Nipper 	if (IS_ERR(edesc))
14834de9d0b5SLee Nipper 		return PTR_ERR(edesc);
14844de9d0b5SLee Nipper 
14854de9d0b5SLee Nipper 	/* set encrypt */
14864de9d0b5SLee Nipper 	edesc->desc.hdr = ctx->desc_hdr_template | DESC_HDR_MODE0_ENCRYPT;
14874de9d0b5SLee Nipper 
1488febec542SKim Phillips 	return common_nonsnoop(edesc, areq, ablkcipher_done);
14894de9d0b5SLee Nipper }
14904de9d0b5SLee Nipper 
14914de9d0b5SLee Nipper static int ablkcipher_decrypt(struct ablkcipher_request *areq)
14924de9d0b5SLee Nipper {
14934de9d0b5SLee Nipper 	struct crypto_ablkcipher *cipher = crypto_ablkcipher_reqtfm(areq);
14944de9d0b5SLee Nipper 	struct talitos_ctx *ctx = crypto_ablkcipher_ctx(cipher);
14954de9d0b5SLee Nipper 	struct talitos_edesc *edesc;
14964de9d0b5SLee Nipper 
14974de9d0b5SLee Nipper 	/* allocate extended descriptor */
14984de9d0b5SLee Nipper 	edesc = ablkcipher_edesc_alloc(areq);
14994de9d0b5SLee Nipper 	if (IS_ERR(edesc))
15004de9d0b5SLee Nipper 		return PTR_ERR(edesc);
15014de9d0b5SLee Nipper 
15024de9d0b5SLee Nipper 	edesc->desc.hdr = ctx->desc_hdr_template | DESC_HDR_DIR_INBOUND;
15034de9d0b5SLee Nipper 
1504febec542SKim Phillips 	return common_nonsnoop(edesc, areq, ablkcipher_done);
15054de9d0b5SLee Nipper }
15064de9d0b5SLee Nipper 
1507497f2e6bSLee Nipper static void common_nonsnoop_hash_unmap(struct device *dev,
1508497f2e6bSLee Nipper 				       struct talitos_edesc *edesc,
1509497f2e6bSLee Nipper 				       struct ahash_request *areq)
1510497f2e6bSLee Nipper {
1511497f2e6bSLee Nipper 	struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1512497f2e6bSLee Nipper 
1513497f2e6bSLee Nipper 	unmap_single_talitos_ptr(dev, &edesc->desc.ptr[5], DMA_FROM_DEVICE);
1514497f2e6bSLee Nipper 
1515497f2e6bSLee Nipper 	/* When using hashctx-in, must unmap it. */
1516497f2e6bSLee Nipper 	if (edesc->desc.ptr[1].len)
1517497f2e6bSLee Nipper 		unmap_single_talitos_ptr(dev, &edesc->desc.ptr[1],
1518497f2e6bSLee Nipper 					 DMA_TO_DEVICE);
1519497f2e6bSLee Nipper 
1520497f2e6bSLee Nipper 	if (edesc->desc.ptr[2].len)
1521497f2e6bSLee Nipper 		unmap_single_talitos_ptr(dev, &edesc->desc.ptr[2],
1522497f2e6bSLee Nipper 					 DMA_TO_DEVICE);
1523497f2e6bSLee Nipper 
1524497f2e6bSLee Nipper 	talitos_sg_unmap(dev, edesc, req_ctx->psrc, NULL);
1525497f2e6bSLee Nipper 
1526497f2e6bSLee Nipper 	if (edesc->dma_len)
1527497f2e6bSLee Nipper 		dma_unmap_single(dev, edesc->dma_link_tbl, edesc->dma_len,
1528497f2e6bSLee Nipper 				 DMA_BIDIRECTIONAL);
1529497f2e6bSLee Nipper 
1530497f2e6bSLee Nipper }
1531497f2e6bSLee Nipper 
1532497f2e6bSLee Nipper static void ahash_done(struct device *dev,
1533497f2e6bSLee Nipper 		       struct talitos_desc *desc, void *context,
1534497f2e6bSLee Nipper 		       int err)
1535497f2e6bSLee Nipper {
1536497f2e6bSLee Nipper 	struct ahash_request *areq = context;
1537497f2e6bSLee Nipper 	struct talitos_edesc *edesc =
1538497f2e6bSLee Nipper 		 container_of(desc, struct talitos_edesc, desc);
1539497f2e6bSLee Nipper 	struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1540497f2e6bSLee Nipper 
1541497f2e6bSLee Nipper 	if (!req_ctx->last && req_ctx->to_hash_later) {
1542497f2e6bSLee Nipper 		/* Position any partial block for next update/final/finup */
1543497f2e6bSLee Nipper 		memcpy(req_ctx->buf, req_ctx->bufnext, req_ctx->to_hash_later);
15445e833bc4SLee Nipper 		req_ctx->nbuf = req_ctx->to_hash_later;
1545497f2e6bSLee Nipper 	}
1546497f2e6bSLee Nipper 	common_nonsnoop_hash_unmap(dev, edesc, areq);
1547497f2e6bSLee Nipper 
1548497f2e6bSLee Nipper 	kfree(edesc);
1549497f2e6bSLee Nipper 
1550497f2e6bSLee Nipper 	areq->base.complete(&areq->base, err);
1551497f2e6bSLee Nipper }
1552497f2e6bSLee Nipper 
1553497f2e6bSLee Nipper static int common_nonsnoop_hash(struct talitos_edesc *edesc,
1554497f2e6bSLee Nipper 				struct ahash_request *areq, unsigned int length,
1555497f2e6bSLee Nipper 				void (*callback) (struct device *dev,
1556497f2e6bSLee Nipper 						  struct talitos_desc *desc,
1557497f2e6bSLee Nipper 						  void *context, int error))
1558497f2e6bSLee Nipper {
1559497f2e6bSLee Nipper 	struct crypto_ahash *tfm = crypto_ahash_reqtfm(areq);
1560497f2e6bSLee Nipper 	struct talitos_ctx *ctx = crypto_ahash_ctx(tfm);
1561497f2e6bSLee Nipper 	struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1562497f2e6bSLee Nipper 	struct device *dev = ctx->dev;
1563497f2e6bSLee Nipper 	struct talitos_desc *desc = &edesc->desc;
1564497f2e6bSLee Nipper 	int sg_count, ret;
1565497f2e6bSLee Nipper 
1566497f2e6bSLee Nipper 	/* first DWORD empty */
1567497f2e6bSLee Nipper 	desc->ptr[0] = zero_entry;
1568497f2e6bSLee Nipper 
156960f208d7SKim Phillips 	/* hash context in */
157060f208d7SKim Phillips 	if (!req_ctx->first || req_ctx->swinit) {
1571497f2e6bSLee Nipper 		map_single_talitos_ptr(dev, &desc->ptr[1],
1572497f2e6bSLee Nipper 				       req_ctx->hw_context_size,
1573497f2e6bSLee Nipper 				       (char *)req_ctx->hw_context, 0,
1574497f2e6bSLee Nipper 				       DMA_TO_DEVICE);
157560f208d7SKim Phillips 		req_ctx->swinit = 0;
1576497f2e6bSLee Nipper 	} else {
1577497f2e6bSLee Nipper 		desc->ptr[1] = zero_entry;
1578497f2e6bSLee Nipper 		/* Indicate next op is not the first. */
1579497f2e6bSLee Nipper 		req_ctx->first = 0;
1580497f2e6bSLee Nipper 	}
1581497f2e6bSLee Nipper 
1582497f2e6bSLee Nipper 	/* HMAC key */
1583497f2e6bSLee Nipper 	if (ctx->keylen)
1584497f2e6bSLee Nipper 		map_single_talitos_ptr(dev, &desc->ptr[2], ctx->keylen,
1585497f2e6bSLee Nipper 				       (char *)&ctx->key, 0, DMA_TO_DEVICE);
1586497f2e6bSLee Nipper 	else
1587497f2e6bSLee Nipper 		desc->ptr[2] = zero_entry;
1588497f2e6bSLee Nipper 
1589497f2e6bSLee Nipper 	/*
1590497f2e6bSLee Nipper 	 * data in
1591497f2e6bSLee Nipper 	 */
1592497f2e6bSLee Nipper 	desc->ptr[3].len = cpu_to_be16(length);
1593497f2e6bSLee Nipper 	desc->ptr[3].j_extent = 0;
1594497f2e6bSLee Nipper 
1595497f2e6bSLee Nipper 	sg_count = talitos_map_sg(dev, req_ctx->psrc,
1596497f2e6bSLee Nipper 				  edesc->src_nents ? : 1,
15972a1cfe46SHoria Geanta 				  DMA_TO_DEVICE, edesc->src_chained);
1598497f2e6bSLee Nipper 
1599497f2e6bSLee Nipper 	if (sg_count == 1) {
1600497f2e6bSLee Nipper 		to_talitos_ptr(&desc->ptr[3], sg_dma_address(req_ctx->psrc));
1601497f2e6bSLee Nipper 	} else {
1602497f2e6bSLee Nipper 		sg_count = sg_to_link_tbl(req_ctx->psrc, sg_count, length,
1603497f2e6bSLee Nipper 					  &edesc->link_tbl[0]);
1604497f2e6bSLee Nipper 		if (sg_count > 1) {
1605497f2e6bSLee Nipper 			desc->ptr[3].j_extent |= DESC_PTR_LNKTBL_JUMP;
1606497f2e6bSLee Nipper 			to_talitos_ptr(&desc->ptr[3], edesc->dma_link_tbl);
1607497f2e6bSLee Nipper 			dma_sync_single_for_device(ctx->dev,
1608497f2e6bSLee Nipper 						   edesc->dma_link_tbl,
1609497f2e6bSLee Nipper 						   edesc->dma_len,
1610497f2e6bSLee Nipper 						   DMA_BIDIRECTIONAL);
1611497f2e6bSLee Nipper 		} else {
1612497f2e6bSLee Nipper 			/* Only one segment now, so no link tbl needed */
1613497f2e6bSLee Nipper 			to_talitos_ptr(&desc->ptr[3],
1614497f2e6bSLee Nipper 				       sg_dma_address(req_ctx->psrc));
1615497f2e6bSLee Nipper 		}
1616497f2e6bSLee Nipper 	}
1617497f2e6bSLee Nipper 
1618497f2e6bSLee Nipper 	/* fifth DWORD empty */
1619497f2e6bSLee Nipper 	desc->ptr[4] = zero_entry;
1620497f2e6bSLee Nipper 
1621497f2e6bSLee Nipper 	/* hash/HMAC out -or- hash context out */
1622497f2e6bSLee Nipper 	if (req_ctx->last)
1623497f2e6bSLee Nipper 		map_single_talitos_ptr(dev, &desc->ptr[5],
1624497f2e6bSLee Nipper 				       crypto_ahash_digestsize(tfm),
1625497f2e6bSLee Nipper 				       areq->result, 0, DMA_FROM_DEVICE);
1626497f2e6bSLee Nipper 	else
1627497f2e6bSLee Nipper 		map_single_talitos_ptr(dev, &desc->ptr[5],
1628497f2e6bSLee Nipper 				       req_ctx->hw_context_size,
1629497f2e6bSLee Nipper 				       req_ctx->hw_context, 0, DMA_FROM_DEVICE);
1630497f2e6bSLee Nipper 
1631497f2e6bSLee Nipper 	/* last DWORD empty */
1632497f2e6bSLee Nipper 	desc->ptr[6] = zero_entry;
1633497f2e6bSLee Nipper 
16345228f0f7SKim Phillips 	ret = talitos_submit(dev, ctx->ch, desc, callback, areq);
1635497f2e6bSLee Nipper 	if (ret != -EINPROGRESS) {
1636497f2e6bSLee Nipper 		common_nonsnoop_hash_unmap(dev, edesc, areq);
1637497f2e6bSLee Nipper 		kfree(edesc);
1638497f2e6bSLee Nipper 	}
1639497f2e6bSLee Nipper 	return ret;
1640497f2e6bSLee Nipper }
1641497f2e6bSLee Nipper 
1642497f2e6bSLee Nipper static struct talitos_edesc *ahash_edesc_alloc(struct ahash_request *areq,
1643497f2e6bSLee Nipper 					       unsigned int nbytes)
1644497f2e6bSLee Nipper {
1645497f2e6bSLee Nipper 	struct crypto_ahash *tfm = crypto_ahash_reqtfm(areq);
1646497f2e6bSLee Nipper 	struct talitos_ctx *ctx = crypto_ahash_ctx(tfm);
1647497f2e6bSLee Nipper 	struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1648497f2e6bSLee Nipper 
164979fd31d3SHoria Geanta 	return talitos_edesc_alloc(ctx->dev, NULL, req_ctx->psrc, NULL, NULL, 0,
165079fd31d3SHoria Geanta 				   nbytes, 0, 0, 0, areq->base.flags);
1651497f2e6bSLee Nipper }
1652497f2e6bSLee Nipper 
1653497f2e6bSLee Nipper static int ahash_init(struct ahash_request *areq)
1654497f2e6bSLee Nipper {
1655497f2e6bSLee Nipper 	struct crypto_ahash *tfm = crypto_ahash_reqtfm(areq);
1656497f2e6bSLee Nipper 	struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1657497f2e6bSLee Nipper 
1658497f2e6bSLee Nipper 	/* Initialize the context */
16595e833bc4SLee Nipper 	req_ctx->nbuf = 0;
166060f208d7SKim Phillips 	req_ctx->first = 1; /* first indicates h/w must init its context */
166160f208d7SKim Phillips 	req_ctx->swinit = 0; /* assume h/w init of context */
1662497f2e6bSLee Nipper 	req_ctx->hw_context_size =
1663497f2e6bSLee Nipper 		(crypto_ahash_digestsize(tfm) <= SHA256_DIGEST_SIZE)
1664497f2e6bSLee Nipper 			? TALITOS_MDEU_CONTEXT_SIZE_MD5_SHA1_SHA256
1665497f2e6bSLee Nipper 			: TALITOS_MDEU_CONTEXT_SIZE_SHA384_SHA512;
1666497f2e6bSLee Nipper 
1667497f2e6bSLee Nipper 	return 0;
1668497f2e6bSLee Nipper }
1669497f2e6bSLee Nipper 
167060f208d7SKim Phillips /*
167160f208d7SKim Phillips  * on h/w without explicit sha224 support, we initialize h/w context
167260f208d7SKim Phillips  * manually with sha224 constants, and tell it to run sha256.
167360f208d7SKim Phillips  */
167460f208d7SKim Phillips static int ahash_init_sha224_swinit(struct ahash_request *areq)
167560f208d7SKim Phillips {
167660f208d7SKim Phillips 	struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
167760f208d7SKim Phillips 
167860f208d7SKim Phillips 	ahash_init(areq);
167960f208d7SKim Phillips 	req_ctx->swinit = 1;/* prevent h/w initting context with sha256 values*/
168060f208d7SKim Phillips 
1681a752447aSKim Phillips 	req_ctx->hw_context[0] = SHA224_H0;
1682a752447aSKim Phillips 	req_ctx->hw_context[1] = SHA224_H1;
1683a752447aSKim Phillips 	req_ctx->hw_context[2] = SHA224_H2;
1684a752447aSKim Phillips 	req_ctx->hw_context[3] = SHA224_H3;
1685a752447aSKim Phillips 	req_ctx->hw_context[4] = SHA224_H4;
1686a752447aSKim Phillips 	req_ctx->hw_context[5] = SHA224_H5;
1687a752447aSKim Phillips 	req_ctx->hw_context[6] = SHA224_H6;
1688a752447aSKim Phillips 	req_ctx->hw_context[7] = SHA224_H7;
168960f208d7SKim Phillips 
169060f208d7SKim Phillips 	/* init 64-bit count */
169160f208d7SKim Phillips 	req_ctx->hw_context[8] = 0;
169260f208d7SKim Phillips 	req_ctx->hw_context[9] = 0;
169360f208d7SKim Phillips 
169460f208d7SKim Phillips 	return 0;
169560f208d7SKim Phillips }
169660f208d7SKim Phillips 
1697497f2e6bSLee Nipper static int ahash_process_req(struct ahash_request *areq, unsigned int nbytes)
1698497f2e6bSLee Nipper {
1699497f2e6bSLee Nipper 	struct crypto_ahash *tfm = crypto_ahash_reqtfm(areq);
1700497f2e6bSLee Nipper 	struct talitos_ctx *ctx = crypto_ahash_ctx(tfm);
1701497f2e6bSLee Nipper 	struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1702497f2e6bSLee Nipper 	struct talitos_edesc *edesc;
1703497f2e6bSLee Nipper 	unsigned int blocksize =
1704497f2e6bSLee Nipper 			crypto_tfm_alg_blocksize(crypto_ahash_tfm(tfm));
1705497f2e6bSLee Nipper 	unsigned int nbytes_to_hash;
1706497f2e6bSLee Nipper 	unsigned int to_hash_later;
17075e833bc4SLee Nipper 	unsigned int nsg;
17082a1cfe46SHoria Geanta 	bool chained;
1709497f2e6bSLee Nipper 
17105e833bc4SLee Nipper 	if (!req_ctx->last && (nbytes + req_ctx->nbuf <= blocksize)) {
17115e833bc4SLee Nipper 		/* Buffer up to one whole block */
1712497f2e6bSLee Nipper 		sg_copy_to_buffer(areq->src,
1713497f2e6bSLee Nipper 				  sg_count(areq->src, nbytes, &chained),
17145e833bc4SLee Nipper 				  req_ctx->buf + req_ctx->nbuf, nbytes);
17155e833bc4SLee Nipper 		req_ctx->nbuf += nbytes;
1716497f2e6bSLee Nipper 		return 0;
1717497f2e6bSLee Nipper 	}
1718497f2e6bSLee Nipper 
17195e833bc4SLee Nipper 	/* At least (blocksize + 1) bytes are available to hash */
17205e833bc4SLee Nipper 	nbytes_to_hash = nbytes + req_ctx->nbuf;
17215e833bc4SLee Nipper 	to_hash_later = nbytes_to_hash & (blocksize - 1);
17225e833bc4SLee Nipper 
17235e833bc4SLee Nipper 	if (req_ctx->last)
17245e833bc4SLee Nipper 		to_hash_later = 0;
17255e833bc4SLee Nipper 	else if (to_hash_later)
17265e833bc4SLee Nipper 		/* There is a partial block. Hash the full block(s) now */
17275e833bc4SLee Nipper 		nbytes_to_hash -= to_hash_later;
17285e833bc4SLee Nipper 	else {
17295e833bc4SLee Nipper 		/* Keep one block buffered */
17305e833bc4SLee Nipper 		nbytes_to_hash -= blocksize;
17315e833bc4SLee Nipper 		to_hash_later = blocksize;
1732497f2e6bSLee Nipper 	}
17335e833bc4SLee Nipper 
17345e833bc4SLee Nipper 	/* Chain in any previously buffered data */
17355e833bc4SLee Nipper 	if (req_ctx->nbuf) {
17365e833bc4SLee Nipper 		nsg = (req_ctx->nbuf < nbytes_to_hash) ? 2 : 1;
17375e833bc4SLee Nipper 		sg_init_table(req_ctx->bufsl, nsg);
17385e833bc4SLee Nipper 		sg_set_buf(req_ctx->bufsl, req_ctx->buf, req_ctx->nbuf);
17395e833bc4SLee Nipper 		if (nsg > 1)
17405e833bc4SLee Nipper 			scatterwalk_sg_chain(req_ctx->bufsl, 2, areq->src);
17415e833bc4SLee Nipper 		req_ctx->psrc = req_ctx->bufsl;
17425e833bc4SLee Nipper 	} else
17435e833bc4SLee Nipper 		req_ctx->psrc = areq->src;
17445e833bc4SLee Nipper 
1745497f2e6bSLee Nipper 	if (to_hash_later) {
17465e833bc4SLee Nipper 		int nents = sg_count(areq->src, nbytes, &chained);
1747d0525723SAkinobu Mita 		sg_pcopy_to_buffer(areq->src, nents,
1748497f2e6bSLee Nipper 				      req_ctx->bufnext,
1749497f2e6bSLee Nipper 				      to_hash_later,
1750497f2e6bSLee Nipper 				      nbytes - to_hash_later);
1751497f2e6bSLee Nipper 	}
1752497f2e6bSLee Nipper 	req_ctx->to_hash_later = to_hash_later;
1753497f2e6bSLee Nipper 
17545e833bc4SLee Nipper 	/* Allocate extended descriptor */
1755497f2e6bSLee Nipper 	edesc = ahash_edesc_alloc(areq, nbytes_to_hash);
1756497f2e6bSLee Nipper 	if (IS_ERR(edesc))
1757497f2e6bSLee Nipper 		return PTR_ERR(edesc);
1758497f2e6bSLee Nipper 
1759497f2e6bSLee Nipper 	edesc->desc.hdr = ctx->desc_hdr_template;
1760497f2e6bSLee Nipper 
1761497f2e6bSLee Nipper 	/* On last one, request SEC to pad; otherwise continue */
1762497f2e6bSLee Nipper 	if (req_ctx->last)
1763497f2e6bSLee Nipper 		edesc->desc.hdr |= DESC_HDR_MODE0_MDEU_PAD;
1764497f2e6bSLee Nipper 	else
1765497f2e6bSLee Nipper 		edesc->desc.hdr |= DESC_HDR_MODE0_MDEU_CONT;
1766497f2e6bSLee Nipper 
176760f208d7SKim Phillips 	/* request SEC to INIT hash. */
176860f208d7SKim Phillips 	if (req_ctx->first && !req_ctx->swinit)
1769497f2e6bSLee Nipper 		edesc->desc.hdr |= DESC_HDR_MODE0_MDEU_INIT;
1770497f2e6bSLee Nipper 
1771497f2e6bSLee Nipper 	/* When the tfm context has a keylen, it's an HMAC.
1772497f2e6bSLee Nipper 	 * A first or last (ie. not middle) descriptor must request HMAC.
1773497f2e6bSLee Nipper 	 */
1774497f2e6bSLee Nipper 	if (ctx->keylen && (req_ctx->first || req_ctx->last))
1775497f2e6bSLee Nipper 		edesc->desc.hdr |= DESC_HDR_MODE0_MDEU_HMAC;
1776497f2e6bSLee Nipper 
1777497f2e6bSLee Nipper 	return common_nonsnoop_hash(edesc, areq, nbytes_to_hash,
1778497f2e6bSLee Nipper 				    ahash_done);
1779497f2e6bSLee Nipper }
1780497f2e6bSLee Nipper 
1781497f2e6bSLee Nipper static int ahash_update(struct ahash_request *areq)
1782497f2e6bSLee Nipper {
1783497f2e6bSLee Nipper 	struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1784497f2e6bSLee Nipper 
1785497f2e6bSLee Nipper 	req_ctx->last = 0;
1786497f2e6bSLee Nipper 
1787497f2e6bSLee Nipper 	return ahash_process_req(areq, areq->nbytes);
1788497f2e6bSLee Nipper }
1789497f2e6bSLee Nipper 
1790497f2e6bSLee Nipper static int ahash_final(struct ahash_request *areq)
1791497f2e6bSLee Nipper {
1792497f2e6bSLee Nipper 	struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1793497f2e6bSLee Nipper 
1794497f2e6bSLee Nipper 	req_ctx->last = 1;
1795497f2e6bSLee Nipper 
1796497f2e6bSLee Nipper 	return ahash_process_req(areq, 0);
1797497f2e6bSLee Nipper }
1798497f2e6bSLee Nipper 
1799497f2e6bSLee Nipper static int ahash_finup(struct ahash_request *areq)
1800497f2e6bSLee Nipper {
1801497f2e6bSLee Nipper 	struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1802497f2e6bSLee Nipper 
1803497f2e6bSLee Nipper 	req_ctx->last = 1;
1804497f2e6bSLee Nipper 
1805497f2e6bSLee Nipper 	return ahash_process_req(areq, areq->nbytes);
1806497f2e6bSLee Nipper }
1807497f2e6bSLee Nipper 
1808497f2e6bSLee Nipper static int ahash_digest(struct ahash_request *areq)
1809497f2e6bSLee Nipper {
1810497f2e6bSLee Nipper 	struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
181160f208d7SKim Phillips 	struct crypto_ahash *ahash = crypto_ahash_reqtfm(areq);
1812497f2e6bSLee Nipper 
181360f208d7SKim Phillips 	ahash->init(areq);
1814497f2e6bSLee Nipper 	req_ctx->last = 1;
1815497f2e6bSLee Nipper 
1816497f2e6bSLee Nipper 	return ahash_process_req(areq, areq->nbytes);
1817497f2e6bSLee Nipper }
1818497f2e6bSLee Nipper 
181979b3a418SLee Nipper struct keyhash_result {
182079b3a418SLee Nipper 	struct completion completion;
182179b3a418SLee Nipper 	int err;
182279b3a418SLee Nipper };
182379b3a418SLee Nipper 
182479b3a418SLee Nipper static void keyhash_complete(struct crypto_async_request *req, int err)
182579b3a418SLee Nipper {
182679b3a418SLee Nipper 	struct keyhash_result *res = req->data;
182779b3a418SLee Nipper 
182879b3a418SLee Nipper 	if (err == -EINPROGRESS)
182979b3a418SLee Nipper 		return;
183079b3a418SLee Nipper 
183179b3a418SLee Nipper 	res->err = err;
183279b3a418SLee Nipper 	complete(&res->completion);
183379b3a418SLee Nipper }
183479b3a418SLee Nipper 
183579b3a418SLee Nipper static int keyhash(struct crypto_ahash *tfm, const u8 *key, unsigned int keylen,
183679b3a418SLee Nipper 		   u8 *hash)
183779b3a418SLee Nipper {
183879b3a418SLee Nipper 	struct talitos_ctx *ctx = crypto_tfm_ctx(crypto_ahash_tfm(tfm));
183979b3a418SLee Nipper 
184079b3a418SLee Nipper 	struct scatterlist sg[1];
184179b3a418SLee Nipper 	struct ahash_request *req;
184279b3a418SLee Nipper 	struct keyhash_result hresult;
184379b3a418SLee Nipper 	int ret;
184479b3a418SLee Nipper 
184579b3a418SLee Nipper 	init_completion(&hresult.completion);
184679b3a418SLee Nipper 
184779b3a418SLee Nipper 	req = ahash_request_alloc(tfm, GFP_KERNEL);
184879b3a418SLee Nipper 	if (!req)
184979b3a418SLee Nipper 		return -ENOMEM;
185079b3a418SLee Nipper 
185179b3a418SLee Nipper 	/* Keep tfm keylen == 0 during hash of the long key */
185279b3a418SLee Nipper 	ctx->keylen = 0;
185379b3a418SLee Nipper 	ahash_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
185479b3a418SLee Nipper 				   keyhash_complete, &hresult);
185579b3a418SLee Nipper 
185679b3a418SLee Nipper 	sg_init_one(&sg[0], key, keylen);
185779b3a418SLee Nipper 
185879b3a418SLee Nipper 	ahash_request_set_crypt(req, sg, hash, keylen);
185979b3a418SLee Nipper 	ret = crypto_ahash_digest(req);
186079b3a418SLee Nipper 	switch (ret) {
186179b3a418SLee Nipper 	case 0:
186279b3a418SLee Nipper 		break;
186379b3a418SLee Nipper 	case -EINPROGRESS:
186479b3a418SLee Nipper 	case -EBUSY:
186579b3a418SLee Nipper 		ret = wait_for_completion_interruptible(
186679b3a418SLee Nipper 			&hresult.completion);
186779b3a418SLee Nipper 		if (!ret)
186879b3a418SLee Nipper 			ret = hresult.err;
186979b3a418SLee Nipper 		break;
187079b3a418SLee Nipper 	default:
187179b3a418SLee Nipper 		break;
187279b3a418SLee Nipper 	}
187379b3a418SLee Nipper 	ahash_request_free(req);
187479b3a418SLee Nipper 
187579b3a418SLee Nipper 	return ret;
187679b3a418SLee Nipper }
187779b3a418SLee Nipper 
187879b3a418SLee Nipper static int ahash_setkey(struct crypto_ahash *tfm, const u8 *key,
187979b3a418SLee Nipper 			unsigned int keylen)
188079b3a418SLee Nipper {
188179b3a418SLee Nipper 	struct talitos_ctx *ctx = crypto_tfm_ctx(crypto_ahash_tfm(tfm));
188279b3a418SLee Nipper 	unsigned int blocksize =
188379b3a418SLee Nipper 			crypto_tfm_alg_blocksize(crypto_ahash_tfm(tfm));
188479b3a418SLee Nipper 	unsigned int digestsize = crypto_ahash_digestsize(tfm);
188579b3a418SLee Nipper 	unsigned int keysize = keylen;
188679b3a418SLee Nipper 	u8 hash[SHA512_DIGEST_SIZE];
188779b3a418SLee Nipper 	int ret;
188879b3a418SLee Nipper 
188979b3a418SLee Nipper 	if (keylen <= blocksize)
189079b3a418SLee Nipper 		memcpy(ctx->key, key, keysize);
189179b3a418SLee Nipper 	else {
189279b3a418SLee Nipper 		/* Must get the hash of the long key */
189379b3a418SLee Nipper 		ret = keyhash(tfm, key, keylen, hash);
189479b3a418SLee Nipper 
189579b3a418SLee Nipper 		if (ret) {
189679b3a418SLee Nipper 			crypto_ahash_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
189779b3a418SLee Nipper 			return -EINVAL;
189879b3a418SLee Nipper 		}
189979b3a418SLee Nipper 
190079b3a418SLee Nipper 		keysize = digestsize;
190179b3a418SLee Nipper 		memcpy(ctx->key, hash, digestsize);
190279b3a418SLee Nipper 	}
190379b3a418SLee Nipper 
190479b3a418SLee Nipper 	ctx->keylen = keysize;
190579b3a418SLee Nipper 
190679b3a418SLee Nipper 	return 0;
190779b3a418SLee Nipper }
190879b3a418SLee Nipper 
190979b3a418SLee Nipper 
19109c4a7965SKim Phillips struct talitos_alg_template {
1911d5e4aaefSLee Nipper 	u32 type;
1912d5e4aaefSLee Nipper 	union {
1913d5e4aaefSLee Nipper 		struct crypto_alg crypto;
1914acbf7c62SLee Nipper 		struct ahash_alg hash;
1915d5e4aaefSLee Nipper 	} alg;
19169c4a7965SKim Phillips 	__be32 desc_hdr_template;
19179c4a7965SKim Phillips };
19189c4a7965SKim Phillips 
19199c4a7965SKim Phillips static struct talitos_alg_template driver_algs[] = {
1920991155baSHoria Geanta 	/* AEAD algorithms.  These use a single-pass ipsec_esp descriptor */
1921d5e4aaefSLee Nipper 	{	.type = CRYPTO_ALG_TYPE_AEAD,
1922d5e4aaefSLee Nipper 		.alg.crypto = {
192356af8cd4SLee Nipper 			.cra_name = "authenc(hmac(sha1),cbc(aes))",
192456af8cd4SLee Nipper 			.cra_driver_name = "authenc-hmac-sha1-cbc-aes-talitos",
192556af8cd4SLee Nipper 			.cra_blocksize = AES_BLOCK_SIZE,
192656af8cd4SLee Nipper 			.cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
192756af8cd4SLee Nipper 			.cra_aead = {
19283952f17eSLee Nipper 				.ivsize = AES_BLOCK_SIZE,
19293952f17eSLee Nipper 				.maxauthsize = SHA1_DIGEST_SIZE,
193056af8cd4SLee Nipper 			}
19319c4a7965SKim Phillips 		},
19329c4a7965SKim Phillips 		.desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
19339c4a7965SKim Phillips 			             DESC_HDR_SEL0_AESU |
19349c4a7965SKim Phillips 		                     DESC_HDR_MODE0_AESU_CBC |
19359c4a7965SKim Phillips 		                     DESC_HDR_SEL1_MDEUA |
19369c4a7965SKim Phillips 		                     DESC_HDR_MODE1_MDEU_INIT |
19379c4a7965SKim Phillips 		                     DESC_HDR_MODE1_MDEU_PAD |
19389c4a7965SKim Phillips 		                     DESC_HDR_MODE1_MDEU_SHA1_HMAC,
193970bcaca7SLee Nipper 	},
1940d5e4aaefSLee Nipper 	{	.type = CRYPTO_ALG_TYPE_AEAD,
1941d5e4aaefSLee Nipper 		.alg.crypto = {
194256af8cd4SLee Nipper 			.cra_name = "authenc(hmac(sha1),cbc(des3_ede))",
194356af8cd4SLee Nipper 			.cra_driver_name = "authenc-hmac-sha1-cbc-3des-talitos",
194456af8cd4SLee Nipper 			.cra_blocksize = DES3_EDE_BLOCK_SIZE,
194556af8cd4SLee Nipper 			.cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
194656af8cd4SLee Nipper 			.cra_aead = {
19473952f17eSLee Nipper 				.ivsize = DES3_EDE_BLOCK_SIZE,
19483952f17eSLee Nipper 				.maxauthsize = SHA1_DIGEST_SIZE,
194956af8cd4SLee Nipper 			}
195070bcaca7SLee Nipper 		},
195170bcaca7SLee Nipper 		.desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
195270bcaca7SLee Nipper 			             DESC_HDR_SEL0_DEU |
195370bcaca7SLee Nipper 		                     DESC_HDR_MODE0_DEU_CBC |
195470bcaca7SLee Nipper 		                     DESC_HDR_MODE0_DEU_3DES |
195570bcaca7SLee Nipper 		                     DESC_HDR_SEL1_MDEUA |
195670bcaca7SLee Nipper 		                     DESC_HDR_MODE1_MDEU_INIT |
195770bcaca7SLee Nipper 		                     DESC_HDR_MODE1_MDEU_PAD |
195870bcaca7SLee Nipper 		                     DESC_HDR_MODE1_MDEU_SHA1_HMAC,
19593952f17eSLee Nipper 	},
1960d5e4aaefSLee Nipper 	{       .type = CRYPTO_ALG_TYPE_AEAD,
1961d5e4aaefSLee Nipper 		.alg.crypto = {
1962357fb605SHoria Geanta 			.cra_name = "authenc(hmac(sha224),cbc(aes))",
1963357fb605SHoria Geanta 			.cra_driver_name = "authenc-hmac-sha224-cbc-aes-talitos",
1964357fb605SHoria Geanta 			.cra_blocksize = AES_BLOCK_SIZE,
1965357fb605SHoria Geanta 			.cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
1966357fb605SHoria Geanta 			.cra_aead = {
1967357fb605SHoria Geanta 				.ivsize = AES_BLOCK_SIZE,
1968357fb605SHoria Geanta 				.maxauthsize = SHA224_DIGEST_SIZE,
1969357fb605SHoria Geanta 			}
1970357fb605SHoria Geanta 		},
1971357fb605SHoria Geanta 		.desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
1972357fb605SHoria Geanta 				     DESC_HDR_SEL0_AESU |
1973357fb605SHoria Geanta 				     DESC_HDR_MODE0_AESU_CBC |
1974357fb605SHoria Geanta 				     DESC_HDR_SEL1_MDEUA |
1975357fb605SHoria Geanta 				     DESC_HDR_MODE1_MDEU_INIT |
1976357fb605SHoria Geanta 				     DESC_HDR_MODE1_MDEU_PAD |
1977357fb605SHoria Geanta 				     DESC_HDR_MODE1_MDEU_SHA224_HMAC,
1978357fb605SHoria Geanta 	},
1979357fb605SHoria Geanta 	{	.type = CRYPTO_ALG_TYPE_AEAD,
1980357fb605SHoria Geanta 		.alg.crypto = {
1981357fb605SHoria Geanta 			.cra_name = "authenc(hmac(sha224),cbc(des3_ede))",
1982357fb605SHoria Geanta 			.cra_driver_name = "authenc-hmac-sha224-cbc-3des-talitos",
1983357fb605SHoria Geanta 			.cra_blocksize = DES3_EDE_BLOCK_SIZE,
1984357fb605SHoria Geanta 			.cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
1985357fb605SHoria Geanta 			.cra_aead = {
1986357fb605SHoria Geanta 				.ivsize = DES3_EDE_BLOCK_SIZE,
1987357fb605SHoria Geanta 				.maxauthsize = SHA224_DIGEST_SIZE,
1988357fb605SHoria Geanta 			}
1989357fb605SHoria Geanta 		},
1990357fb605SHoria Geanta 		.desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
1991357fb605SHoria Geanta 			             DESC_HDR_SEL0_DEU |
1992357fb605SHoria Geanta 		                     DESC_HDR_MODE0_DEU_CBC |
1993357fb605SHoria Geanta 		                     DESC_HDR_MODE0_DEU_3DES |
1994357fb605SHoria Geanta 		                     DESC_HDR_SEL1_MDEUA |
1995357fb605SHoria Geanta 		                     DESC_HDR_MODE1_MDEU_INIT |
1996357fb605SHoria Geanta 		                     DESC_HDR_MODE1_MDEU_PAD |
1997357fb605SHoria Geanta 		                     DESC_HDR_MODE1_MDEU_SHA224_HMAC,
1998357fb605SHoria Geanta 	},
1999357fb605SHoria Geanta 	{	.type = CRYPTO_ALG_TYPE_AEAD,
2000357fb605SHoria Geanta 		.alg.crypto = {
200156af8cd4SLee Nipper 			.cra_name = "authenc(hmac(sha256),cbc(aes))",
200256af8cd4SLee Nipper 			.cra_driver_name = "authenc-hmac-sha256-cbc-aes-talitos",
200356af8cd4SLee Nipper 			.cra_blocksize = AES_BLOCK_SIZE,
200456af8cd4SLee Nipper 			.cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
200556af8cd4SLee Nipper 			.cra_aead = {
20063952f17eSLee Nipper 				.ivsize = AES_BLOCK_SIZE,
20073952f17eSLee Nipper 				.maxauthsize = SHA256_DIGEST_SIZE,
200856af8cd4SLee Nipper 			}
20093952f17eSLee Nipper 		},
20103952f17eSLee Nipper 		.desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
20113952f17eSLee Nipper 			             DESC_HDR_SEL0_AESU |
20123952f17eSLee Nipper 		                     DESC_HDR_MODE0_AESU_CBC |
20133952f17eSLee Nipper 		                     DESC_HDR_SEL1_MDEUA |
20143952f17eSLee Nipper 		                     DESC_HDR_MODE1_MDEU_INIT |
20153952f17eSLee Nipper 		                     DESC_HDR_MODE1_MDEU_PAD |
20163952f17eSLee Nipper 		                     DESC_HDR_MODE1_MDEU_SHA256_HMAC,
20173952f17eSLee Nipper 	},
2018d5e4aaefSLee Nipper 	{	.type = CRYPTO_ALG_TYPE_AEAD,
2019d5e4aaefSLee Nipper 		.alg.crypto = {
202056af8cd4SLee Nipper 			.cra_name = "authenc(hmac(sha256),cbc(des3_ede))",
202156af8cd4SLee Nipper 			.cra_driver_name = "authenc-hmac-sha256-cbc-3des-talitos",
202256af8cd4SLee Nipper 			.cra_blocksize = DES3_EDE_BLOCK_SIZE,
202356af8cd4SLee Nipper 			.cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
202456af8cd4SLee Nipper 			.cra_aead = {
20253952f17eSLee Nipper 				.ivsize = DES3_EDE_BLOCK_SIZE,
20263952f17eSLee Nipper 				.maxauthsize = SHA256_DIGEST_SIZE,
202756af8cd4SLee Nipper 			}
20283952f17eSLee Nipper 		},
20293952f17eSLee Nipper 		.desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
20303952f17eSLee Nipper 			             DESC_HDR_SEL0_DEU |
20313952f17eSLee Nipper 		                     DESC_HDR_MODE0_DEU_CBC |
20323952f17eSLee Nipper 		                     DESC_HDR_MODE0_DEU_3DES |
20333952f17eSLee Nipper 		                     DESC_HDR_SEL1_MDEUA |
20343952f17eSLee Nipper 		                     DESC_HDR_MODE1_MDEU_INIT |
20353952f17eSLee Nipper 		                     DESC_HDR_MODE1_MDEU_PAD |
20363952f17eSLee Nipper 		                     DESC_HDR_MODE1_MDEU_SHA256_HMAC,
20373952f17eSLee Nipper 	},
2038d5e4aaefSLee Nipper 	{	.type = CRYPTO_ALG_TYPE_AEAD,
2039d5e4aaefSLee Nipper 		.alg.crypto = {
2040357fb605SHoria Geanta 			.cra_name = "authenc(hmac(sha384),cbc(aes))",
2041357fb605SHoria Geanta 			.cra_driver_name = "authenc-hmac-sha384-cbc-aes-talitos",
2042357fb605SHoria Geanta 			.cra_blocksize = AES_BLOCK_SIZE,
2043357fb605SHoria Geanta 			.cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
2044357fb605SHoria Geanta 			.cra_aead = {
2045357fb605SHoria Geanta 				.ivsize = AES_BLOCK_SIZE,
2046357fb605SHoria Geanta 				.maxauthsize = SHA384_DIGEST_SIZE,
2047357fb605SHoria Geanta 			}
2048357fb605SHoria Geanta 		},
2049357fb605SHoria Geanta 		.desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
2050357fb605SHoria Geanta 			             DESC_HDR_SEL0_AESU |
2051357fb605SHoria Geanta 		                     DESC_HDR_MODE0_AESU_CBC |
2052357fb605SHoria Geanta 		                     DESC_HDR_SEL1_MDEUB |
2053357fb605SHoria Geanta 		                     DESC_HDR_MODE1_MDEU_INIT |
2054357fb605SHoria Geanta 		                     DESC_HDR_MODE1_MDEU_PAD |
2055357fb605SHoria Geanta 		                     DESC_HDR_MODE1_MDEUB_SHA384_HMAC,
2056357fb605SHoria Geanta 	},
2057357fb605SHoria Geanta 	{	.type = CRYPTO_ALG_TYPE_AEAD,
2058357fb605SHoria Geanta 		.alg.crypto = {
2059357fb605SHoria Geanta 			.cra_name = "authenc(hmac(sha384),cbc(des3_ede))",
2060357fb605SHoria Geanta 			.cra_driver_name = "authenc-hmac-sha384-cbc-3des-talitos",
2061357fb605SHoria Geanta 			.cra_blocksize = DES3_EDE_BLOCK_SIZE,
2062357fb605SHoria Geanta 			.cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
2063357fb605SHoria Geanta 			.cra_aead = {
2064357fb605SHoria Geanta 				.ivsize = DES3_EDE_BLOCK_SIZE,
2065357fb605SHoria Geanta 				.maxauthsize = SHA384_DIGEST_SIZE,
2066357fb605SHoria Geanta 			}
2067357fb605SHoria Geanta 		},
2068357fb605SHoria Geanta 		.desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
2069357fb605SHoria Geanta 			             DESC_HDR_SEL0_DEU |
2070357fb605SHoria Geanta 		                     DESC_HDR_MODE0_DEU_CBC |
2071357fb605SHoria Geanta 		                     DESC_HDR_MODE0_DEU_3DES |
2072357fb605SHoria Geanta 		                     DESC_HDR_SEL1_MDEUB |
2073357fb605SHoria Geanta 		                     DESC_HDR_MODE1_MDEU_INIT |
2074357fb605SHoria Geanta 		                     DESC_HDR_MODE1_MDEU_PAD |
2075357fb605SHoria Geanta 		                     DESC_HDR_MODE1_MDEUB_SHA384_HMAC,
2076357fb605SHoria Geanta 	},
2077357fb605SHoria Geanta 	{	.type = CRYPTO_ALG_TYPE_AEAD,
2078357fb605SHoria Geanta 		.alg.crypto = {
2079357fb605SHoria Geanta 			.cra_name = "authenc(hmac(sha512),cbc(aes))",
2080357fb605SHoria Geanta 			.cra_driver_name = "authenc-hmac-sha512-cbc-aes-talitos",
2081357fb605SHoria Geanta 			.cra_blocksize = AES_BLOCK_SIZE,
2082357fb605SHoria Geanta 			.cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
2083357fb605SHoria Geanta 			.cra_aead = {
2084357fb605SHoria Geanta 				.ivsize = AES_BLOCK_SIZE,
2085357fb605SHoria Geanta 				.maxauthsize = SHA512_DIGEST_SIZE,
2086357fb605SHoria Geanta 			}
2087357fb605SHoria Geanta 		},
2088357fb605SHoria Geanta 		.desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
2089357fb605SHoria Geanta 			             DESC_HDR_SEL0_AESU |
2090357fb605SHoria Geanta 		                     DESC_HDR_MODE0_AESU_CBC |
2091357fb605SHoria Geanta 		                     DESC_HDR_SEL1_MDEUB |
2092357fb605SHoria Geanta 		                     DESC_HDR_MODE1_MDEU_INIT |
2093357fb605SHoria Geanta 		                     DESC_HDR_MODE1_MDEU_PAD |
2094357fb605SHoria Geanta 		                     DESC_HDR_MODE1_MDEUB_SHA512_HMAC,
2095357fb605SHoria Geanta 	},
2096357fb605SHoria Geanta 	{	.type = CRYPTO_ALG_TYPE_AEAD,
2097357fb605SHoria Geanta 		.alg.crypto = {
2098357fb605SHoria Geanta 			.cra_name = "authenc(hmac(sha512),cbc(des3_ede))",
2099357fb605SHoria Geanta 			.cra_driver_name = "authenc-hmac-sha512-cbc-3des-talitos",
2100357fb605SHoria Geanta 			.cra_blocksize = DES3_EDE_BLOCK_SIZE,
2101357fb605SHoria Geanta 			.cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
2102357fb605SHoria Geanta 			.cra_aead = {
2103357fb605SHoria Geanta 				.ivsize = DES3_EDE_BLOCK_SIZE,
2104357fb605SHoria Geanta 				.maxauthsize = SHA512_DIGEST_SIZE,
2105357fb605SHoria Geanta 			}
2106357fb605SHoria Geanta 		},
2107357fb605SHoria Geanta 		.desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
2108357fb605SHoria Geanta 			             DESC_HDR_SEL0_DEU |
2109357fb605SHoria Geanta 		                     DESC_HDR_MODE0_DEU_CBC |
2110357fb605SHoria Geanta 		                     DESC_HDR_MODE0_DEU_3DES |
2111357fb605SHoria Geanta 		                     DESC_HDR_SEL1_MDEUB |
2112357fb605SHoria Geanta 		                     DESC_HDR_MODE1_MDEU_INIT |
2113357fb605SHoria Geanta 		                     DESC_HDR_MODE1_MDEU_PAD |
2114357fb605SHoria Geanta 		                     DESC_HDR_MODE1_MDEUB_SHA512_HMAC,
2115357fb605SHoria Geanta 	},
2116357fb605SHoria Geanta 	{	.type = CRYPTO_ALG_TYPE_AEAD,
2117357fb605SHoria Geanta 		.alg.crypto = {
211856af8cd4SLee Nipper 			.cra_name = "authenc(hmac(md5),cbc(aes))",
211956af8cd4SLee Nipper 			.cra_driver_name = "authenc-hmac-md5-cbc-aes-talitos",
212056af8cd4SLee Nipper 			.cra_blocksize = AES_BLOCK_SIZE,
212156af8cd4SLee Nipper 			.cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
212256af8cd4SLee Nipper 			.cra_aead = {
21233952f17eSLee Nipper 				.ivsize = AES_BLOCK_SIZE,
21243952f17eSLee Nipper 				.maxauthsize = MD5_DIGEST_SIZE,
212556af8cd4SLee Nipper 			}
21263952f17eSLee Nipper 		},
21273952f17eSLee Nipper 		.desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
21283952f17eSLee Nipper 			             DESC_HDR_SEL0_AESU |
21293952f17eSLee Nipper 		                     DESC_HDR_MODE0_AESU_CBC |
21303952f17eSLee Nipper 		                     DESC_HDR_SEL1_MDEUA |
21313952f17eSLee Nipper 		                     DESC_HDR_MODE1_MDEU_INIT |
21323952f17eSLee Nipper 		                     DESC_HDR_MODE1_MDEU_PAD |
21333952f17eSLee Nipper 		                     DESC_HDR_MODE1_MDEU_MD5_HMAC,
21343952f17eSLee Nipper 	},
2135d5e4aaefSLee Nipper 	{	.type = CRYPTO_ALG_TYPE_AEAD,
2136d5e4aaefSLee Nipper 		.alg.crypto = {
213756af8cd4SLee Nipper 			.cra_name = "authenc(hmac(md5),cbc(des3_ede))",
213856af8cd4SLee Nipper 			.cra_driver_name = "authenc-hmac-md5-cbc-3des-talitos",
213956af8cd4SLee Nipper 			.cra_blocksize = DES3_EDE_BLOCK_SIZE,
214056af8cd4SLee Nipper 			.cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
214156af8cd4SLee Nipper 			.cra_aead = {
21423952f17eSLee Nipper 				.ivsize = DES3_EDE_BLOCK_SIZE,
21433952f17eSLee Nipper 				.maxauthsize = MD5_DIGEST_SIZE,
214456af8cd4SLee Nipper 			}
21453952f17eSLee Nipper 		},
21463952f17eSLee Nipper 		.desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
21473952f17eSLee Nipper 			             DESC_HDR_SEL0_DEU |
21483952f17eSLee Nipper 		                     DESC_HDR_MODE0_DEU_CBC |
21493952f17eSLee Nipper 		                     DESC_HDR_MODE0_DEU_3DES |
21503952f17eSLee Nipper 		                     DESC_HDR_SEL1_MDEUA |
21513952f17eSLee Nipper 		                     DESC_HDR_MODE1_MDEU_INIT |
21523952f17eSLee Nipper 		                     DESC_HDR_MODE1_MDEU_PAD |
21533952f17eSLee Nipper 		                     DESC_HDR_MODE1_MDEU_MD5_HMAC,
21544de9d0b5SLee Nipper 	},
21554de9d0b5SLee Nipper 	/* ABLKCIPHER algorithms. */
2156d5e4aaefSLee Nipper 	{	.type = CRYPTO_ALG_TYPE_ABLKCIPHER,
2157d5e4aaefSLee Nipper 		.alg.crypto = {
21584de9d0b5SLee Nipper 			.cra_name = "cbc(aes)",
21594de9d0b5SLee Nipper 			.cra_driver_name = "cbc-aes-talitos",
21604de9d0b5SLee Nipper 			.cra_blocksize = AES_BLOCK_SIZE,
21614de9d0b5SLee Nipper 			.cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
21624de9d0b5SLee Nipper                                      CRYPTO_ALG_ASYNC,
21634de9d0b5SLee Nipper 			.cra_ablkcipher = {
21644de9d0b5SLee Nipper 				.min_keysize = AES_MIN_KEY_SIZE,
21654de9d0b5SLee Nipper 				.max_keysize = AES_MAX_KEY_SIZE,
21664de9d0b5SLee Nipper 				.ivsize = AES_BLOCK_SIZE,
21674de9d0b5SLee Nipper 			}
21684de9d0b5SLee Nipper 		},
21694de9d0b5SLee Nipper 		.desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
21704de9d0b5SLee Nipper 				     DESC_HDR_SEL0_AESU |
21714de9d0b5SLee Nipper 				     DESC_HDR_MODE0_AESU_CBC,
21724de9d0b5SLee Nipper 	},
2173d5e4aaefSLee Nipper 	{	.type = CRYPTO_ALG_TYPE_ABLKCIPHER,
2174d5e4aaefSLee Nipper 		.alg.crypto = {
21754de9d0b5SLee Nipper 			.cra_name = "cbc(des3_ede)",
21764de9d0b5SLee Nipper 			.cra_driver_name = "cbc-3des-talitos",
21774de9d0b5SLee Nipper 			.cra_blocksize = DES3_EDE_BLOCK_SIZE,
21784de9d0b5SLee Nipper 			.cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
21794de9d0b5SLee Nipper                                      CRYPTO_ALG_ASYNC,
21804de9d0b5SLee Nipper 			.cra_ablkcipher = {
21814de9d0b5SLee Nipper 				.min_keysize = DES3_EDE_KEY_SIZE,
21824de9d0b5SLee Nipper 				.max_keysize = DES3_EDE_KEY_SIZE,
21834de9d0b5SLee Nipper 				.ivsize = DES3_EDE_BLOCK_SIZE,
21844de9d0b5SLee Nipper 			}
21854de9d0b5SLee Nipper 		},
21864de9d0b5SLee Nipper 		.desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
21874de9d0b5SLee Nipper 			             DESC_HDR_SEL0_DEU |
21884de9d0b5SLee Nipper 		                     DESC_HDR_MODE0_DEU_CBC |
21894de9d0b5SLee Nipper 		                     DESC_HDR_MODE0_DEU_3DES,
2190497f2e6bSLee Nipper 	},
2191497f2e6bSLee Nipper 	/* AHASH algorithms. */
2192497f2e6bSLee Nipper 	{	.type = CRYPTO_ALG_TYPE_AHASH,
2193497f2e6bSLee Nipper 		.alg.hash = {
2194497f2e6bSLee Nipper 			.halg.digestsize = MD5_DIGEST_SIZE,
2195497f2e6bSLee Nipper 			.halg.base = {
2196497f2e6bSLee Nipper 				.cra_name = "md5",
2197497f2e6bSLee Nipper 				.cra_driver_name = "md5-talitos",
2198497f2e6bSLee Nipper 				.cra_blocksize = MD5_BLOCK_SIZE,
2199497f2e6bSLee Nipper 				.cra_flags = CRYPTO_ALG_TYPE_AHASH |
2200497f2e6bSLee Nipper 					     CRYPTO_ALG_ASYNC,
22019c4a7965SKim Phillips 			}
2202497f2e6bSLee Nipper 		},
2203497f2e6bSLee Nipper 		.desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2204497f2e6bSLee Nipper 				     DESC_HDR_SEL0_MDEUA |
2205497f2e6bSLee Nipper 				     DESC_HDR_MODE0_MDEU_MD5,
2206497f2e6bSLee Nipper 	},
2207497f2e6bSLee Nipper 	{	.type = CRYPTO_ALG_TYPE_AHASH,
2208497f2e6bSLee Nipper 		.alg.hash = {
2209497f2e6bSLee Nipper 			.halg.digestsize = SHA1_DIGEST_SIZE,
2210497f2e6bSLee Nipper 			.halg.base = {
2211497f2e6bSLee Nipper 				.cra_name = "sha1",
2212497f2e6bSLee Nipper 				.cra_driver_name = "sha1-talitos",
2213497f2e6bSLee Nipper 				.cra_blocksize = SHA1_BLOCK_SIZE,
2214497f2e6bSLee Nipper 				.cra_flags = CRYPTO_ALG_TYPE_AHASH |
2215497f2e6bSLee Nipper 					     CRYPTO_ALG_ASYNC,
2216497f2e6bSLee Nipper 			}
2217497f2e6bSLee Nipper 		},
2218497f2e6bSLee Nipper 		.desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2219497f2e6bSLee Nipper 				     DESC_HDR_SEL0_MDEUA |
2220497f2e6bSLee Nipper 				     DESC_HDR_MODE0_MDEU_SHA1,
2221497f2e6bSLee Nipper 	},
2222497f2e6bSLee Nipper 	{	.type = CRYPTO_ALG_TYPE_AHASH,
2223497f2e6bSLee Nipper 		.alg.hash = {
222460f208d7SKim Phillips 			.halg.digestsize = SHA224_DIGEST_SIZE,
222560f208d7SKim Phillips 			.halg.base = {
222660f208d7SKim Phillips 				.cra_name = "sha224",
222760f208d7SKim Phillips 				.cra_driver_name = "sha224-talitos",
222860f208d7SKim Phillips 				.cra_blocksize = SHA224_BLOCK_SIZE,
222960f208d7SKim Phillips 				.cra_flags = CRYPTO_ALG_TYPE_AHASH |
223060f208d7SKim Phillips 					     CRYPTO_ALG_ASYNC,
223160f208d7SKim Phillips 			}
223260f208d7SKim Phillips 		},
223360f208d7SKim Phillips 		.desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
223460f208d7SKim Phillips 				     DESC_HDR_SEL0_MDEUA |
223560f208d7SKim Phillips 				     DESC_HDR_MODE0_MDEU_SHA224,
223660f208d7SKim Phillips 	},
223760f208d7SKim Phillips 	{	.type = CRYPTO_ALG_TYPE_AHASH,
223860f208d7SKim Phillips 		.alg.hash = {
2239497f2e6bSLee Nipper 			.halg.digestsize = SHA256_DIGEST_SIZE,
2240497f2e6bSLee Nipper 			.halg.base = {
2241497f2e6bSLee Nipper 				.cra_name = "sha256",
2242497f2e6bSLee Nipper 				.cra_driver_name = "sha256-talitos",
2243497f2e6bSLee Nipper 				.cra_blocksize = SHA256_BLOCK_SIZE,
2244497f2e6bSLee Nipper 				.cra_flags = CRYPTO_ALG_TYPE_AHASH |
2245497f2e6bSLee Nipper 					     CRYPTO_ALG_ASYNC,
2246497f2e6bSLee Nipper 			}
2247497f2e6bSLee Nipper 		},
2248497f2e6bSLee Nipper 		.desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2249497f2e6bSLee Nipper 				     DESC_HDR_SEL0_MDEUA |
2250497f2e6bSLee Nipper 				     DESC_HDR_MODE0_MDEU_SHA256,
2251497f2e6bSLee Nipper 	},
2252497f2e6bSLee Nipper 	{	.type = CRYPTO_ALG_TYPE_AHASH,
2253497f2e6bSLee Nipper 		.alg.hash = {
2254497f2e6bSLee Nipper 			.halg.digestsize = SHA384_DIGEST_SIZE,
2255497f2e6bSLee Nipper 			.halg.base = {
2256497f2e6bSLee Nipper 				.cra_name = "sha384",
2257497f2e6bSLee Nipper 				.cra_driver_name = "sha384-talitos",
2258497f2e6bSLee Nipper 				.cra_blocksize = SHA384_BLOCK_SIZE,
2259497f2e6bSLee Nipper 				.cra_flags = CRYPTO_ALG_TYPE_AHASH |
2260497f2e6bSLee Nipper 					     CRYPTO_ALG_ASYNC,
2261497f2e6bSLee Nipper 			}
2262497f2e6bSLee Nipper 		},
2263497f2e6bSLee Nipper 		.desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2264497f2e6bSLee Nipper 				     DESC_HDR_SEL0_MDEUB |
2265497f2e6bSLee Nipper 				     DESC_HDR_MODE0_MDEUB_SHA384,
2266497f2e6bSLee Nipper 	},
2267497f2e6bSLee Nipper 	{	.type = CRYPTO_ALG_TYPE_AHASH,
2268497f2e6bSLee Nipper 		.alg.hash = {
2269497f2e6bSLee Nipper 			.halg.digestsize = SHA512_DIGEST_SIZE,
2270497f2e6bSLee Nipper 			.halg.base = {
2271497f2e6bSLee Nipper 				.cra_name = "sha512",
2272497f2e6bSLee Nipper 				.cra_driver_name = "sha512-talitos",
2273497f2e6bSLee Nipper 				.cra_blocksize = SHA512_BLOCK_SIZE,
2274497f2e6bSLee Nipper 				.cra_flags = CRYPTO_ALG_TYPE_AHASH |
2275497f2e6bSLee Nipper 					     CRYPTO_ALG_ASYNC,
2276497f2e6bSLee Nipper 			}
2277497f2e6bSLee Nipper 		},
2278497f2e6bSLee Nipper 		.desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2279497f2e6bSLee Nipper 				     DESC_HDR_SEL0_MDEUB |
2280497f2e6bSLee Nipper 				     DESC_HDR_MODE0_MDEUB_SHA512,
2281497f2e6bSLee Nipper 	},
228279b3a418SLee Nipper 	{	.type = CRYPTO_ALG_TYPE_AHASH,
228379b3a418SLee Nipper 		.alg.hash = {
228479b3a418SLee Nipper 			.halg.digestsize = MD5_DIGEST_SIZE,
228579b3a418SLee Nipper 			.halg.base = {
228679b3a418SLee Nipper 				.cra_name = "hmac(md5)",
228779b3a418SLee Nipper 				.cra_driver_name = "hmac-md5-talitos",
228879b3a418SLee Nipper 				.cra_blocksize = MD5_BLOCK_SIZE,
228979b3a418SLee Nipper 				.cra_flags = CRYPTO_ALG_TYPE_AHASH |
229079b3a418SLee Nipper 					     CRYPTO_ALG_ASYNC,
229179b3a418SLee Nipper 			}
229279b3a418SLee Nipper 		},
229379b3a418SLee Nipper 		.desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
229479b3a418SLee Nipper 				     DESC_HDR_SEL0_MDEUA |
229579b3a418SLee Nipper 				     DESC_HDR_MODE0_MDEU_MD5,
229679b3a418SLee Nipper 	},
229779b3a418SLee Nipper 	{	.type = CRYPTO_ALG_TYPE_AHASH,
229879b3a418SLee Nipper 		.alg.hash = {
229979b3a418SLee Nipper 			.halg.digestsize = SHA1_DIGEST_SIZE,
230079b3a418SLee Nipper 			.halg.base = {
230179b3a418SLee Nipper 				.cra_name = "hmac(sha1)",
230279b3a418SLee Nipper 				.cra_driver_name = "hmac-sha1-talitos",
230379b3a418SLee Nipper 				.cra_blocksize = SHA1_BLOCK_SIZE,
230479b3a418SLee Nipper 				.cra_flags = CRYPTO_ALG_TYPE_AHASH |
230579b3a418SLee Nipper 					     CRYPTO_ALG_ASYNC,
230679b3a418SLee Nipper 			}
230779b3a418SLee Nipper 		},
230879b3a418SLee Nipper 		.desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
230979b3a418SLee Nipper 				     DESC_HDR_SEL0_MDEUA |
231079b3a418SLee Nipper 				     DESC_HDR_MODE0_MDEU_SHA1,
231179b3a418SLee Nipper 	},
231279b3a418SLee Nipper 	{	.type = CRYPTO_ALG_TYPE_AHASH,
231379b3a418SLee Nipper 		.alg.hash = {
231479b3a418SLee Nipper 			.halg.digestsize = SHA224_DIGEST_SIZE,
231579b3a418SLee Nipper 			.halg.base = {
231679b3a418SLee Nipper 				.cra_name = "hmac(sha224)",
231779b3a418SLee Nipper 				.cra_driver_name = "hmac-sha224-talitos",
231879b3a418SLee Nipper 				.cra_blocksize = SHA224_BLOCK_SIZE,
231979b3a418SLee Nipper 				.cra_flags = CRYPTO_ALG_TYPE_AHASH |
232079b3a418SLee Nipper 					     CRYPTO_ALG_ASYNC,
232179b3a418SLee Nipper 			}
232279b3a418SLee Nipper 		},
232379b3a418SLee Nipper 		.desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
232479b3a418SLee Nipper 				     DESC_HDR_SEL0_MDEUA |
232579b3a418SLee Nipper 				     DESC_HDR_MODE0_MDEU_SHA224,
232679b3a418SLee Nipper 	},
232779b3a418SLee Nipper 	{	.type = CRYPTO_ALG_TYPE_AHASH,
232879b3a418SLee Nipper 		.alg.hash = {
232979b3a418SLee Nipper 			.halg.digestsize = SHA256_DIGEST_SIZE,
233079b3a418SLee Nipper 			.halg.base = {
233179b3a418SLee Nipper 				.cra_name = "hmac(sha256)",
233279b3a418SLee Nipper 				.cra_driver_name = "hmac-sha256-talitos",
233379b3a418SLee Nipper 				.cra_blocksize = SHA256_BLOCK_SIZE,
233479b3a418SLee Nipper 				.cra_flags = CRYPTO_ALG_TYPE_AHASH |
233579b3a418SLee Nipper 					     CRYPTO_ALG_ASYNC,
233679b3a418SLee Nipper 			}
233779b3a418SLee Nipper 		},
233879b3a418SLee Nipper 		.desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
233979b3a418SLee Nipper 				     DESC_HDR_SEL0_MDEUA |
234079b3a418SLee Nipper 				     DESC_HDR_MODE0_MDEU_SHA256,
234179b3a418SLee Nipper 	},
234279b3a418SLee Nipper 	{	.type = CRYPTO_ALG_TYPE_AHASH,
234379b3a418SLee Nipper 		.alg.hash = {
234479b3a418SLee Nipper 			.halg.digestsize = SHA384_DIGEST_SIZE,
234579b3a418SLee Nipper 			.halg.base = {
234679b3a418SLee Nipper 				.cra_name = "hmac(sha384)",
234779b3a418SLee Nipper 				.cra_driver_name = "hmac-sha384-talitos",
234879b3a418SLee Nipper 				.cra_blocksize = SHA384_BLOCK_SIZE,
234979b3a418SLee Nipper 				.cra_flags = CRYPTO_ALG_TYPE_AHASH |
235079b3a418SLee Nipper 					     CRYPTO_ALG_ASYNC,
235179b3a418SLee Nipper 			}
235279b3a418SLee Nipper 		},
235379b3a418SLee Nipper 		.desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
235479b3a418SLee Nipper 				     DESC_HDR_SEL0_MDEUB |
235579b3a418SLee Nipper 				     DESC_HDR_MODE0_MDEUB_SHA384,
235679b3a418SLee Nipper 	},
235779b3a418SLee Nipper 	{	.type = CRYPTO_ALG_TYPE_AHASH,
235879b3a418SLee Nipper 		.alg.hash = {
235979b3a418SLee Nipper 			.halg.digestsize = SHA512_DIGEST_SIZE,
236079b3a418SLee Nipper 			.halg.base = {
236179b3a418SLee Nipper 				.cra_name = "hmac(sha512)",
236279b3a418SLee Nipper 				.cra_driver_name = "hmac-sha512-talitos",
236379b3a418SLee Nipper 				.cra_blocksize = SHA512_BLOCK_SIZE,
236479b3a418SLee Nipper 				.cra_flags = CRYPTO_ALG_TYPE_AHASH |
236579b3a418SLee Nipper 					     CRYPTO_ALG_ASYNC,
236679b3a418SLee Nipper 			}
236779b3a418SLee Nipper 		},
236879b3a418SLee Nipper 		.desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
236979b3a418SLee Nipper 				     DESC_HDR_SEL0_MDEUB |
237079b3a418SLee Nipper 				     DESC_HDR_MODE0_MDEUB_SHA512,
237179b3a418SLee Nipper 	}
23729c4a7965SKim Phillips };
23739c4a7965SKim Phillips 
23749c4a7965SKim Phillips struct talitos_crypto_alg {
23759c4a7965SKim Phillips 	struct list_head entry;
23769c4a7965SKim Phillips 	struct device *dev;
2377acbf7c62SLee Nipper 	struct talitos_alg_template algt;
23789c4a7965SKim Phillips };
23799c4a7965SKim Phillips 
23809c4a7965SKim Phillips static int talitos_cra_init(struct crypto_tfm *tfm)
23819c4a7965SKim Phillips {
23829c4a7965SKim Phillips 	struct crypto_alg *alg = tfm->__crt_alg;
238319bbbc63SKim Phillips 	struct talitos_crypto_alg *talitos_alg;
23849c4a7965SKim Phillips 	struct talitos_ctx *ctx = crypto_tfm_ctx(tfm);
23855228f0f7SKim Phillips 	struct talitos_private *priv;
23869c4a7965SKim Phillips 
2387497f2e6bSLee Nipper 	if ((alg->cra_flags & CRYPTO_ALG_TYPE_MASK) == CRYPTO_ALG_TYPE_AHASH)
2388497f2e6bSLee Nipper 		talitos_alg = container_of(__crypto_ahash_alg(alg),
2389497f2e6bSLee Nipper 					   struct talitos_crypto_alg,
2390497f2e6bSLee Nipper 					   algt.alg.hash);
2391497f2e6bSLee Nipper 	else
2392acbf7c62SLee Nipper 		talitos_alg = container_of(alg, struct talitos_crypto_alg,
2393acbf7c62SLee Nipper 					   algt.alg.crypto);
239419bbbc63SKim Phillips 
23959c4a7965SKim Phillips 	/* update context with ptr to dev */
23969c4a7965SKim Phillips 	ctx->dev = talitos_alg->dev;
239719bbbc63SKim Phillips 
23985228f0f7SKim Phillips 	/* assign SEC channel to tfm in round-robin fashion */
23995228f0f7SKim Phillips 	priv = dev_get_drvdata(ctx->dev);
24005228f0f7SKim Phillips 	ctx->ch = atomic_inc_return(&priv->last_chan) &
24015228f0f7SKim Phillips 		  (priv->num_channels - 1);
24025228f0f7SKim Phillips 
24039c4a7965SKim Phillips 	/* copy descriptor header template value */
2404acbf7c62SLee Nipper 	ctx->desc_hdr_template = talitos_alg->algt.desc_hdr_template;
24059c4a7965SKim Phillips 
2406602dba5aSKim Phillips 	/* select done notification */
2407602dba5aSKim Phillips 	ctx->desc_hdr_template |= DESC_HDR_DONE_NOTIFY;
2408602dba5aSKim Phillips 
2409497f2e6bSLee Nipper 	return 0;
2410497f2e6bSLee Nipper }
2411497f2e6bSLee Nipper 
2412497f2e6bSLee Nipper static int talitos_cra_init_aead(struct crypto_tfm *tfm)
2413497f2e6bSLee Nipper {
2414497f2e6bSLee Nipper 	struct talitos_ctx *ctx = crypto_tfm_ctx(tfm);
2415497f2e6bSLee Nipper 
2416497f2e6bSLee Nipper 	talitos_cra_init(tfm);
24179c4a7965SKim Phillips 
24189c4a7965SKim Phillips 	/* random first IV */
241970bcaca7SLee Nipper 	get_random_bytes(ctx->iv, TALITOS_MAX_IV_LENGTH);
24209c4a7965SKim Phillips 
24219c4a7965SKim Phillips 	return 0;
24229c4a7965SKim Phillips }
24239c4a7965SKim Phillips 
2424497f2e6bSLee Nipper static int talitos_cra_init_ahash(struct crypto_tfm *tfm)
2425497f2e6bSLee Nipper {
2426497f2e6bSLee Nipper 	struct talitos_ctx *ctx = crypto_tfm_ctx(tfm);
2427497f2e6bSLee Nipper 
2428497f2e6bSLee Nipper 	talitos_cra_init(tfm);
2429497f2e6bSLee Nipper 
2430497f2e6bSLee Nipper 	ctx->keylen = 0;
2431497f2e6bSLee Nipper 	crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
2432497f2e6bSLee Nipper 				 sizeof(struct talitos_ahash_req_ctx));
2433497f2e6bSLee Nipper 
2434497f2e6bSLee Nipper 	return 0;
2435497f2e6bSLee Nipper }
2436497f2e6bSLee Nipper 
24379c4a7965SKim Phillips /*
24389c4a7965SKim Phillips  * given the alg's descriptor header template, determine whether descriptor
24399c4a7965SKim Phillips  * type and primary/secondary execution units required match the hw
24409c4a7965SKim Phillips  * capabilities description provided in the device tree node.
24419c4a7965SKim Phillips  */
24429c4a7965SKim Phillips static int hw_supports(struct device *dev, __be32 desc_hdr_template)
24439c4a7965SKim Phillips {
24449c4a7965SKim Phillips 	struct talitos_private *priv = dev_get_drvdata(dev);
24459c4a7965SKim Phillips 	int ret;
24469c4a7965SKim Phillips 
24479c4a7965SKim Phillips 	ret = (1 << DESC_TYPE(desc_hdr_template) & priv->desc_types) &&
24489c4a7965SKim Phillips 	      (1 << PRIMARY_EU(desc_hdr_template) & priv->exec_units);
24499c4a7965SKim Phillips 
24509c4a7965SKim Phillips 	if (SECONDARY_EU(desc_hdr_template))
24519c4a7965SKim Phillips 		ret = ret && (1 << SECONDARY_EU(desc_hdr_template)
24529c4a7965SKim Phillips 		              & priv->exec_units);
24539c4a7965SKim Phillips 
24549c4a7965SKim Phillips 	return ret;
24559c4a7965SKim Phillips }
24569c4a7965SKim Phillips 
24572dc11581SGrant Likely static int talitos_remove(struct platform_device *ofdev)
24589c4a7965SKim Phillips {
24599c4a7965SKim Phillips 	struct device *dev = &ofdev->dev;
24609c4a7965SKim Phillips 	struct talitos_private *priv = dev_get_drvdata(dev);
24619c4a7965SKim Phillips 	struct talitos_crypto_alg *t_alg, *n;
24629c4a7965SKim Phillips 	int i;
24639c4a7965SKim Phillips 
24649c4a7965SKim Phillips 	list_for_each_entry_safe(t_alg, n, &priv->alg_list, entry) {
2465acbf7c62SLee Nipper 		switch (t_alg->algt.type) {
2466acbf7c62SLee Nipper 		case CRYPTO_ALG_TYPE_ABLKCIPHER:
2467acbf7c62SLee Nipper 		case CRYPTO_ALG_TYPE_AEAD:
2468acbf7c62SLee Nipper 			crypto_unregister_alg(&t_alg->algt.alg.crypto);
2469acbf7c62SLee Nipper 			break;
2470acbf7c62SLee Nipper 		case CRYPTO_ALG_TYPE_AHASH:
2471acbf7c62SLee Nipper 			crypto_unregister_ahash(&t_alg->algt.alg.hash);
2472acbf7c62SLee Nipper 			break;
2473acbf7c62SLee Nipper 		}
24749c4a7965SKim Phillips 		list_del(&t_alg->entry);
24759c4a7965SKim Phillips 		kfree(t_alg);
24769c4a7965SKim Phillips 	}
24779c4a7965SKim Phillips 
24789c4a7965SKim Phillips 	if (hw_supports(dev, DESC_HDR_SEL0_RNG))
24799c4a7965SKim Phillips 		talitos_unregister_rng(dev);
24809c4a7965SKim Phillips 
24819c4a7965SKim Phillips 	for (i = 0; i < priv->num_channels; i++)
24824b992628SKim Phillips 		kfree(priv->chan[i].fifo);
24839c4a7965SKim Phillips 
24844b992628SKim Phillips 	kfree(priv->chan);
24859c4a7965SKim Phillips 
2486c3e337f8SKim Phillips 	for (i = 0; i < 2; i++)
24872cdba3cfSKim Phillips 		if (priv->irq[i]) {
2488c3e337f8SKim Phillips 			free_irq(priv->irq[i], dev);
2489c3e337f8SKim Phillips 			irq_dispose_mapping(priv->irq[i]);
24909c4a7965SKim Phillips 		}
24919c4a7965SKim Phillips 
2492c3e337f8SKim Phillips 	tasklet_kill(&priv->done_task[0]);
24932cdba3cfSKim Phillips 	if (priv->irq[1])
2494c3e337f8SKim Phillips 		tasklet_kill(&priv->done_task[1]);
24959c4a7965SKim Phillips 
24969c4a7965SKim Phillips 	iounmap(priv->reg);
24979c4a7965SKim Phillips 
24989c4a7965SKim Phillips 	dev_set_drvdata(dev, NULL);
24999c4a7965SKim Phillips 
25009c4a7965SKim Phillips 	kfree(priv);
25019c4a7965SKim Phillips 
25029c4a7965SKim Phillips 	return 0;
25039c4a7965SKim Phillips }
25049c4a7965SKim Phillips 
25059c4a7965SKim Phillips static struct talitos_crypto_alg *talitos_alg_alloc(struct device *dev,
25069c4a7965SKim Phillips 						    struct talitos_alg_template
25079c4a7965SKim Phillips 						           *template)
25089c4a7965SKim Phillips {
250960f208d7SKim Phillips 	struct talitos_private *priv = dev_get_drvdata(dev);
25109c4a7965SKim Phillips 	struct talitos_crypto_alg *t_alg;
25119c4a7965SKim Phillips 	struct crypto_alg *alg;
25129c4a7965SKim Phillips 
25139c4a7965SKim Phillips 	t_alg = kzalloc(sizeof(struct talitos_crypto_alg), GFP_KERNEL);
25149c4a7965SKim Phillips 	if (!t_alg)
25159c4a7965SKim Phillips 		return ERR_PTR(-ENOMEM);
25169c4a7965SKim Phillips 
2517acbf7c62SLee Nipper 	t_alg->algt = *template;
2518acbf7c62SLee Nipper 
2519acbf7c62SLee Nipper 	switch (t_alg->algt.type) {
2520acbf7c62SLee Nipper 	case CRYPTO_ALG_TYPE_ABLKCIPHER:
2521497f2e6bSLee Nipper 		alg = &t_alg->algt.alg.crypto;
2522497f2e6bSLee Nipper 		alg->cra_init = talitos_cra_init;
2523d4cd3283SKim Phillips 		alg->cra_type = &crypto_ablkcipher_type;
2524b286e003SKim Phillips 		alg->cra_ablkcipher.setkey = ablkcipher_setkey;
2525b286e003SKim Phillips 		alg->cra_ablkcipher.encrypt = ablkcipher_encrypt;
2526b286e003SKim Phillips 		alg->cra_ablkcipher.decrypt = ablkcipher_decrypt;
2527b286e003SKim Phillips 		alg->cra_ablkcipher.geniv = "eseqiv";
2528497f2e6bSLee Nipper 		break;
2529acbf7c62SLee Nipper 	case CRYPTO_ALG_TYPE_AEAD:
2530acbf7c62SLee Nipper 		alg = &t_alg->algt.alg.crypto;
2531497f2e6bSLee Nipper 		alg->cra_init = talitos_cra_init_aead;
2532d4cd3283SKim Phillips 		alg->cra_type = &crypto_aead_type;
2533b286e003SKim Phillips 		alg->cra_aead.setkey = aead_setkey;
2534b286e003SKim Phillips 		alg->cra_aead.setauthsize = aead_setauthsize;
2535b286e003SKim Phillips 		alg->cra_aead.encrypt = aead_encrypt;
2536b286e003SKim Phillips 		alg->cra_aead.decrypt = aead_decrypt;
2537b286e003SKim Phillips 		alg->cra_aead.givencrypt = aead_givencrypt;
2538b286e003SKim Phillips 		alg->cra_aead.geniv = "<built-in>";
2539acbf7c62SLee Nipper 		break;
2540acbf7c62SLee Nipper 	case CRYPTO_ALG_TYPE_AHASH:
2541acbf7c62SLee Nipper 		alg = &t_alg->algt.alg.hash.halg.base;
2542497f2e6bSLee Nipper 		alg->cra_init = talitos_cra_init_ahash;
2543d4cd3283SKim Phillips 		alg->cra_type = &crypto_ahash_type;
2544b286e003SKim Phillips 		t_alg->algt.alg.hash.init = ahash_init;
2545b286e003SKim Phillips 		t_alg->algt.alg.hash.update = ahash_update;
2546b286e003SKim Phillips 		t_alg->algt.alg.hash.final = ahash_final;
2547b286e003SKim Phillips 		t_alg->algt.alg.hash.finup = ahash_finup;
2548b286e003SKim Phillips 		t_alg->algt.alg.hash.digest = ahash_digest;
2549b286e003SKim Phillips 		t_alg->algt.alg.hash.setkey = ahash_setkey;
2550b286e003SKim Phillips 
255179b3a418SLee Nipper 		if (!(priv->features & TALITOS_FTR_HMAC_OK) &&
25520b2730d8SKim Phillips 		    !strncmp(alg->cra_name, "hmac", 4)) {
25530b2730d8SKim Phillips 			kfree(t_alg);
255479b3a418SLee Nipper 			return ERR_PTR(-ENOTSUPP);
25550b2730d8SKim Phillips 		}
255660f208d7SKim Phillips 		if (!(priv->features & TALITOS_FTR_SHA224_HWINIT) &&
255779b3a418SLee Nipper 		    (!strcmp(alg->cra_name, "sha224") ||
255879b3a418SLee Nipper 		     !strcmp(alg->cra_name, "hmac(sha224)"))) {
255960f208d7SKim Phillips 			t_alg->algt.alg.hash.init = ahash_init_sha224_swinit;
256060f208d7SKim Phillips 			t_alg->algt.desc_hdr_template =
256160f208d7SKim Phillips 					DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
256260f208d7SKim Phillips 					DESC_HDR_SEL0_MDEUA |
256360f208d7SKim Phillips 					DESC_HDR_MODE0_MDEU_SHA256;
256460f208d7SKim Phillips 		}
2565497f2e6bSLee Nipper 		break;
25661d11911aSKim Phillips 	default:
25671d11911aSKim Phillips 		dev_err(dev, "unknown algorithm type %d\n", t_alg->algt.type);
25681d11911aSKim Phillips 		return ERR_PTR(-EINVAL);
2569acbf7c62SLee Nipper 	}
25709c4a7965SKim Phillips 
25719c4a7965SKim Phillips 	alg->cra_module = THIS_MODULE;
25729c4a7965SKim Phillips 	alg->cra_priority = TALITOS_CRA_PRIORITY;
25739c4a7965SKim Phillips 	alg->cra_alignmask = 0;
25749c4a7965SKim Phillips 	alg->cra_ctxsize = sizeof(struct talitos_ctx);
2575d912bb76SNikos Mavrogiannopoulos 	alg->cra_flags |= CRYPTO_ALG_KERN_DRIVER_ONLY;
25769c4a7965SKim Phillips 
25779c4a7965SKim Phillips 	t_alg->dev = dev;
25789c4a7965SKim Phillips 
25799c4a7965SKim Phillips 	return t_alg;
25809c4a7965SKim Phillips }
25819c4a7965SKim Phillips 
2582c3e337f8SKim Phillips static int talitos_probe_irq(struct platform_device *ofdev)
2583c3e337f8SKim Phillips {
2584c3e337f8SKim Phillips 	struct device *dev = &ofdev->dev;
2585c3e337f8SKim Phillips 	struct device_node *np = ofdev->dev.of_node;
2586c3e337f8SKim Phillips 	struct talitos_private *priv = dev_get_drvdata(dev);
2587c3e337f8SKim Phillips 	int err;
2588c3e337f8SKim Phillips 
2589c3e337f8SKim Phillips 	priv->irq[0] = irq_of_parse_and_map(np, 0);
25902cdba3cfSKim Phillips 	if (!priv->irq[0]) {
2591c3e337f8SKim Phillips 		dev_err(dev, "failed to map irq\n");
2592c3e337f8SKim Phillips 		return -EINVAL;
2593c3e337f8SKim Phillips 	}
2594c3e337f8SKim Phillips 
2595c3e337f8SKim Phillips 	priv->irq[1] = irq_of_parse_and_map(np, 1);
2596c3e337f8SKim Phillips 
2597c3e337f8SKim Phillips 	/* get the primary irq line */
25982cdba3cfSKim Phillips 	if (!priv->irq[1]) {
2599c3e337f8SKim Phillips 		err = request_irq(priv->irq[0], talitos_interrupt_4ch, 0,
2600c3e337f8SKim Phillips 				  dev_driver_string(dev), dev);
2601c3e337f8SKim Phillips 		goto primary_out;
2602c3e337f8SKim Phillips 	}
2603c3e337f8SKim Phillips 
2604c3e337f8SKim Phillips 	err = request_irq(priv->irq[0], talitos_interrupt_ch0_2, 0,
2605c3e337f8SKim Phillips 			  dev_driver_string(dev), dev);
2606c3e337f8SKim Phillips 	if (err)
2607c3e337f8SKim Phillips 		goto primary_out;
2608c3e337f8SKim Phillips 
2609c3e337f8SKim Phillips 	/* get the secondary irq line */
2610c3e337f8SKim Phillips 	err = request_irq(priv->irq[1], talitos_interrupt_ch1_3, 0,
2611c3e337f8SKim Phillips 			  dev_driver_string(dev), dev);
2612c3e337f8SKim Phillips 	if (err) {
2613c3e337f8SKim Phillips 		dev_err(dev, "failed to request secondary irq\n");
2614c3e337f8SKim Phillips 		irq_dispose_mapping(priv->irq[1]);
26152cdba3cfSKim Phillips 		priv->irq[1] = 0;
2616c3e337f8SKim Phillips 	}
2617c3e337f8SKim Phillips 
2618c3e337f8SKim Phillips 	return err;
2619c3e337f8SKim Phillips 
2620c3e337f8SKim Phillips primary_out:
2621c3e337f8SKim Phillips 	if (err) {
2622c3e337f8SKim Phillips 		dev_err(dev, "failed to request primary irq\n");
2623c3e337f8SKim Phillips 		irq_dispose_mapping(priv->irq[0]);
26242cdba3cfSKim Phillips 		priv->irq[0] = 0;
2625c3e337f8SKim Phillips 	}
2626c3e337f8SKim Phillips 
2627c3e337f8SKim Phillips 	return err;
2628c3e337f8SKim Phillips }
2629c3e337f8SKim Phillips 
26301c48a5c9SGrant Likely static int talitos_probe(struct platform_device *ofdev)
26319c4a7965SKim Phillips {
26329c4a7965SKim Phillips 	struct device *dev = &ofdev->dev;
263361c7a080SGrant Likely 	struct device_node *np = ofdev->dev.of_node;
26349c4a7965SKim Phillips 	struct talitos_private *priv;
26359c4a7965SKim Phillips 	const unsigned int *prop;
26369c4a7965SKim Phillips 	int i, err;
26379c4a7965SKim Phillips 
26389c4a7965SKim Phillips 	priv = kzalloc(sizeof(struct talitos_private), GFP_KERNEL);
26399c4a7965SKim Phillips 	if (!priv)
26409c4a7965SKim Phillips 		return -ENOMEM;
26419c4a7965SKim Phillips 
26429c4a7965SKim Phillips 	dev_set_drvdata(dev, priv);
26439c4a7965SKim Phillips 
26449c4a7965SKim Phillips 	priv->ofdev = ofdev;
26459c4a7965SKim Phillips 
2646511d63cbSHoria Geanta 	spin_lock_init(&priv->reg_lock);
2647511d63cbSHoria Geanta 
2648c3e337f8SKim Phillips 	err = talitos_probe_irq(ofdev);
2649c3e337f8SKim Phillips 	if (err)
2650c3e337f8SKim Phillips 		goto err_out;
2651c3e337f8SKim Phillips 
26522cdba3cfSKim Phillips 	if (!priv->irq[1]) {
2653c3e337f8SKim Phillips 		tasklet_init(&priv->done_task[0], talitos_done_4ch,
2654c3e337f8SKim Phillips 			     (unsigned long)dev);
2655c3e337f8SKim Phillips 	} else {
2656c3e337f8SKim Phillips 		tasklet_init(&priv->done_task[0], talitos_done_ch0_2,
2657c3e337f8SKim Phillips 			     (unsigned long)dev);
2658c3e337f8SKim Phillips 		tasklet_init(&priv->done_task[1], talitos_done_ch1_3,
2659c3e337f8SKim Phillips 			     (unsigned long)dev);
2660c3e337f8SKim Phillips 	}
26619c4a7965SKim Phillips 
2662fe5720e2SKim Phillips 	INIT_LIST_HEAD(&priv->alg_list);
2663fe5720e2SKim Phillips 
26649c4a7965SKim Phillips 	priv->reg = of_iomap(np, 0);
26659c4a7965SKim Phillips 	if (!priv->reg) {
26669c4a7965SKim Phillips 		dev_err(dev, "failed to of_iomap\n");
26679c4a7965SKim Phillips 		err = -ENOMEM;
26689c4a7965SKim Phillips 		goto err_out;
26699c4a7965SKim Phillips 	}
26709c4a7965SKim Phillips 
26719c4a7965SKim Phillips 	/* get SEC version capabilities from device tree */
26729c4a7965SKim Phillips 	prop = of_get_property(np, "fsl,num-channels", NULL);
26739c4a7965SKim Phillips 	if (prop)
26749c4a7965SKim Phillips 		priv->num_channels = *prop;
26759c4a7965SKim Phillips 
26769c4a7965SKim Phillips 	prop = of_get_property(np, "fsl,channel-fifo-len", NULL);
26779c4a7965SKim Phillips 	if (prop)
26789c4a7965SKim Phillips 		priv->chfifo_len = *prop;
26799c4a7965SKim Phillips 
26809c4a7965SKim Phillips 	prop = of_get_property(np, "fsl,exec-units-mask", NULL);
26819c4a7965SKim Phillips 	if (prop)
26829c4a7965SKim Phillips 		priv->exec_units = *prop;
26839c4a7965SKim Phillips 
26849c4a7965SKim Phillips 	prop = of_get_property(np, "fsl,descriptor-types-mask", NULL);
26859c4a7965SKim Phillips 	if (prop)
26869c4a7965SKim Phillips 		priv->desc_types = *prop;
26879c4a7965SKim Phillips 
26889c4a7965SKim Phillips 	if (!is_power_of_2(priv->num_channels) || !priv->chfifo_len ||
26899c4a7965SKim Phillips 	    !priv->exec_units || !priv->desc_types) {
26909c4a7965SKim Phillips 		dev_err(dev, "invalid property data in device tree node\n");
26919c4a7965SKim Phillips 		err = -EINVAL;
26929c4a7965SKim Phillips 		goto err_out;
26939c4a7965SKim Phillips 	}
26949c4a7965SKim Phillips 
2695f3c85bc1SLee Nipper 	if (of_device_is_compatible(np, "fsl,sec3.0"))
2696f3c85bc1SLee Nipper 		priv->features |= TALITOS_FTR_SRC_LINK_TBL_LEN_INCLUDES_EXTENT;
2697f3c85bc1SLee Nipper 
2698fe5720e2SKim Phillips 	if (of_device_is_compatible(np, "fsl,sec2.1"))
269960f208d7SKim Phillips 		priv->features |= TALITOS_FTR_HW_AUTH_CHECK |
270079b3a418SLee Nipper 				  TALITOS_FTR_SHA224_HWINIT |
270179b3a418SLee Nipper 				  TALITOS_FTR_HMAC_OK;
2702fe5720e2SKim Phillips 
27034b992628SKim Phillips 	priv->chan = kzalloc(sizeof(struct talitos_channel) *
27044b992628SKim Phillips 			     priv->num_channels, GFP_KERNEL);
27054b992628SKim Phillips 	if (!priv->chan) {
27064b992628SKim Phillips 		dev_err(dev, "failed to allocate channel management space\n");
27079c4a7965SKim Phillips 		err = -ENOMEM;
27089c4a7965SKim Phillips 		goto err_out;
27099c4a7965SKim Phillips 	}
27109c4a7965SKim Phillips 
2711c3e337f8SKim Phillips 	for (i = 0; i < priv->num_channels; i++) {
2712c3e337f8SKim Phillips 		priv->chan[i].reg = priv->reg + TALITOS_CH_STRIDE * (i + 1);
27132cdba3cfSKim Phillips 		if (!priv->irq[1] || !(i & 1))
2714c3e337f8SKim Phillips 			priv->chan[i].reg += TALITOS_CH_BASE_OFFSET;
2715c3e337f8SKim Phillips 	}
2716ad42d5fcSKim Phillips 
27179c4a7965SKim Phillips 	for (i = 0; i < priv->num_channels; i++) {
27184b992628SKim Phillips 		spin_lock_init(&priv->chan[i].head_lock);
27194b992628SKim Phillips 		spin_lock_init(&priv->chan[i].tail_lock);
27209c4a7965SKim Phillips 	}
27219c4a7965SKim Phillips 
27229c4a7965SKim Phillips 	priv->fifo_len = roundup_pow_of_two(priv->chfifo_len);
27239c4a7965SKim Phillips 
27249c4a7965SKim Phillips 	for (i = 0; i < priv->num_channels; i++) {
27254b992628SKim Phillips 		priv->chan[i].fifo = kzalloc(sizeof(struct talitos_request) *
27269c4a7965SKim Phillips 					     priv->fifo_len, GFP_KERNEL);
27274b992628SKim Phillips 		if (!priv->chan[i].fifo) {
27289c4a7965SKim Phillips 			dev_err(dev, "failed to allocate request fifo %d\n", i);
27299c4a7965SKim Phillips 			err = -ENOMEM;
27309c4a7965SKim Phillips 			goto err_out;
27319c4a7965SKim Phillips 		}
27329c4a7965SKim Phillips 	}
27339c4a7965SKim Phillips 
2734ec6644d6SKim Phillips 	for (i = 0; i < priv->num_channels; i++)
27354b992628SKim Phillips 		atomic_set(&priv->chan[i].submit_count,
27364b992628SKim Phillips 			   -(priv->chfifo_len - 1));
27379c4a7965SKim Phillips 
273881eb024cSKim Phillips 	dma_set_mask(dev, DMA_BIT_MASK(36));
273981eb024cSKim Phillips 
27409c4a7965SKim Phillips 	/* reset and initialize the h/w */
27419c4a7965SKim Phillips 	err = init_device(dev);
27429c4a7965SKim Phillips 	if (err) {
27439c4a7965SKim Phillips 		dev_err(dev, "failed to initialize device\n");
27449c4a7965SKim Phillips 		goto err_out;
27459c4a7965SKim Phillips 	}
27469c4a7965SKim Phillips 
27479c4a7965SKim Phillips 	/* register the RNG, if available */
27489c4a7965SKim Phillips 	if (hw_supports(dev, DESC_HDR_SEL0_RNG)) {
27499c4a7965SKim Phillips 		err = talitos_register_rng(dev);
27509c4a7965SKim Phillips 		if (err) {
27519c4a7965SKim Phillips 			dev_err(dev, "failed to register hwrng: %d\n", err);
27529c4a7965SKim Phillips 			goto err_out;
27539c4a7965SKim Phillips 		} else
27549c4a7965SKim Phillips 			dev_info(dev, "hwrng\n");
27559c4a7965SKim Phillips 	}
27569c4a7965SKim Phillips 
27579c4a7965SKim Phillips 	/* register crypto algorithms the device supports */
27589c4a7965SKim Phillips 	for (i = 0; i < ARRAY_SIZE(driver_algs); i++) {
27599c4a7965SKim Phillips 		if (hw_supports(dev, driver_algs[i].desc_hdr_template)) {
27609c4a7965SKim Phillips 			struct talitos_crypto_alg *t_alg;
2761acbf7c62SLee Nipper 			char *name = NULL;
27629c4a7965SKim Phillips 
27639c4a7965SKim Phillips 			t_alg = talitos_alg_alloc(dev, &driver_algs[i]);
27649c4a7965SKim Phillips 			if (IS_ERR(t_alg)) {
27659c4a7965SKim Phillips 				err = PTR_ERR(t_alg);
27660b2730d8SKim Phillips 				if (err == -ENOTSUPP)
276779b3a418SLee Nipper 					continue;
27689c4a7965SKim Phillips 				goto err_out;
27699c4a7965SKim Phillips 			}
27709c4a7965SKim Phillips 
2771acbf7c62SLee Nipper 			switch (t_alg->algt.type) {
2772acbf7c62SLee Nipper 			case CRYPTO_ALG_TYPE_ABLKCIPHER:
2773acbf7c62SLee Nipper 			case CRYPTO_ALG_TYPE_AEAD:
2774acbf7c62SLee Nipper 				err = crypto_register_alg(
2775acbf7c62SLee Nipper 						&t_alg->algt.alg.crypto);
2776acbf7c62SLee Nipper 				name = t_alg->algt.alg.crypto.cra_driver_name;
2777acbf7c62SLee Nipper 				break;
2778acbf7c62SLee Nipper 			case CRYPTO_ALG_TYPE_AHASH:
2779acbf7c62SLee Nipper 				err = crypto_register_ahash(
2780acbf7c62SLee Nipper 						&t_alg->algt.alg.hash);
2781acbf7c62SLee Nipper 				name =
2782acbf7c62SLee Nipper 				 t_alg->algt.alg.hash.halg.base.cra_driver_name;
2783acbf7c62SLee Nipper 				break;
2784acbf7c62SLee Nipper 			}
27859c4a7965SKim Phillips 			if (err) {
27869c4a7965SKim Phillips 				dev_err(dev, "%s alg registration failed\n",
2787acbf7c62SLee Nipper 					name);
27889c4a7965SKim Phillips 				kfree(t_alg);
2789991155baSHoria Geanta 			} else
27909c4a7965SKim Phillips 				list_add_tail(&t_alg->entry, &priv->alg_list);
27919c4a7965SKim Phillips 		}
27929c4a7965SKim Phillips 	}
27935b859b6eSKim Phillips 	if (!list_empty(&priv->alg_list))
27945b859b6eSKim Phillips 		dev_info(dev, "%s algorithms registered in /proc/crypto\n",
27955b859b6eSKim Phillips 			 (char *)of_get_property(np, "compatible", NULL));
27969c4a7965SKim Phillips 
27979c4a7965SKim Phillips 	return 0;
27989c4a7965SKim Phillips 
27999c4a7965SKim Phillips err_out:
28009c4a7965SKim Phillips 	talitos_remove(ofdev);
28019c4a7965SKim Phillips 
28029c4a7965SKim Phillips 	return err;
28039c4a7965SKim Phillips }
28049c4a7965SKim Phillips 
28056c3f975aSMárton Németh static const struct of_device_id talitos_match[] = {
28069c4a7965SKim Phillips 	{
28079c4a7965SKim Phillips 		.compatible = "fsl,sec2.0",
28089c4a7965SKim Phillips 	},
28099c4a7965SKim Phillips 	{},
28109c4a7965SKim Phillips };
28119c4a7965SKim Phillips MODULE_DEVICE_TABLE(of, talitos_match);
28129c4a7965SKim Phillips 
28131c48a5c9SGrant Likely static struct platform_driver talitos_driver = {
28144018294bSGrant Likely 	.driver = {
28159c4a7965SKim Phillips 		.name = "talitos",
28164018294bSGrant Likely 		.owner = THIS_MODULE,
28174018294bSGrant Likely 		.of_match_table = talitos_match,
28184018294bSGrant Likely 	},
28199c4a7965SKim Phillips 	.probe = talitos_probe,
2820596f1034SAl Viro 	.remove = talitos_remove,
28219c4a7965SKim Phillips };
28229c4a7965SKim Phillips 
2823741e8c2dSAxel Lin module_platform_driver(talitos_driver);
28249c4a7965SKim Phillips 
28259c4a7965SKim Phillips MODULE_LICENSE("GPL");
28269c4a7965SKim Phillips MODULE_AUTHOR("Kim Phillips <kim.phillips@freescale.com>");
28279c4a7965SKim Phillips MODULE_DESCRIPTION("Freescale integrated security engine (SEC) driver");
2828