xref: /linux/drivers/crypto/talitos.c (revision b286e00304f9c1d27b93753536ac5246a287dd55)
19c4a7965SKim Phillips /*
29c4a7965SKim Phillips  * talitos - Freescale Integrated Security Engine (SEC) device driver
39c4a7965SKim Phillips  *
45228f0f7SKim Phillips  * Copyright (c) 2008-2011 Freescale Semiconductor, Inc.
59c4a7965SKim Phillips  *
69c4a7965SKim Phillips  * Scatterlist Crypto API glue code copied from files with the following:
79c4a7965SKim Phillips  * Copyright (c) 2006-2007 Herbert Xu <herbert@gondor.apana.org.au>
89c4a7965SKim Phillips  *
99c4a7965SKim Phillips  * Crypto algorithm registration code copied from hifn driver:
109c4a7965SKim Phillips  * 2007+ Copyright (c) Evgeniy Polyakov <johnpol@2ka.mipt.ru>
119c4a7965SKim Phillips  * All rights reserved.
129c4a7965SKim Phillips  *
139c4a7965SKim Phillips  * This program is free software; you can redistribute it and/or modify
149c4a7965SKim Phillips  * it under the terms of the GNU General Public License as published by
159c4a7965SKim Phillips  * the Free Software Foundation; either version 2 of the License, or
169c4a7965SKim Phillips  * (at your option) any later version.
179c4a7965SKim Phillips  *
189c4a7965SKim Phillips  * This program is distributed in the hope that it will be useful,
199c4a7965SKim Phillips  * but WITHOUT ANY WARRANTY; without even the implied warranty of
209c4a7965SKim Phillips  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
219c4a7965SKim Phillips  * GNU General Public License for more details.
229c4a7965SKim Phillips  *
239c4a7965SKim Phillips  * You should have received a copy of the GNU General Public License
249c4a7965SKim Phillips  * along with this program; if not, write to the Free Software
259c4a7965SKim Phillips  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
269c4a7965SKim Phillips  */
279c4a7965SKim Phillips 
289c4a7965SKim Phillips #include <linux/kernel.h>
299c4a7965SKim Phillips #include <linux/module.h>
309c4a7965SKim Phillips #include <linux/mod_devicetable.h>
319c4a7965SKim Phillips #include <linux/device.h>
329c4a7965SKim Phillips #include <linux/interrupt.h>
339c4a7965SKim Phillips #include <linux/crypto.h>
349c4a7965SKim Phillips #include <linux/hw_random.h>
359c4a7965SKim Phillips #include <linux/of_platform.h>
369c4a7965SKim Phillips #include <linux/dma-mapping.h>
379c4a7965SKim Phillips #include <linux/io.h>
389c4a7965SKim Phillips #include <linux/spinlock.h>
399c4a7965SKim Phillips #include <linux/rtnetlink.h>
405a0e3ad6STejun Heo #include <linux/slab.h>
419c4a7965SKim Phillips 
429c4a7965SKim Phillips #include <crypto/algapi.h>
439c4a7965SKim Phillips #include <crypto/aes.h>
443952f17eSLee Nipper #include <crypto/des.h>
459c4a7965SKim Phillips #include <crypto/sha.h>
46497f2e6bSLee Nipper #include <crypto/md5.h>
479c4a7965SKim Phillips #include <crypto/aead.h>
489c4a7965SKim Phillips #include <crypto/authenc.h>
494de9d0b5SLee Nipper #include <crypto/skcipher.h>
50acbf7c62SLee Nipper #include <crypto/hash.h>
51acbf7c62SLee Nipper #include <crypto/internal/hash.h>
524de9d0b5SLee Nipper #include <crypto/scatterwalk.h>
539c4a7965SKim Phillips 
549c4a7965SKim Phillips #include "talitos.h"
559c4a7965SKim Phillips 
5681eb024cSKim Phillips static void to_talitos_ptr(struct talitos_ptr *talitos_ptr, dma_addr_t dma_addr)
5781eb024cSKim Phillips {
5881eb024cSKim Phillips 	talitos_ptr->ptr = cpu_to_be32(lower_32_bits(dma_addr));
59a752447aSKim Phillips 	talitos_ptr->eptr = upper_32_bits(dma_addr);
6081eb024cSKim Phillips }
6181eb024cSKim Phillips 
629c4a7965SKim Phillips /*
639c4a7965SKim Phillips  * map virtual single (contiguous) pointer to h/w descriptor pointer
649c4a7965SKim Phillips  */
659c4a7965SKim Phillips static void map_single_talitos_ptr(struct device *dev,
669c4a7965SKim Phillips 				   struct talitos_ptr *talitos_ptr,
679c4a7965SKim Phillips 				   unsigned short len, void *data,
689c4a7965SKim Phillips 				   unsigned char extent,
699c4a7965SKim Phillips 				   enum dma_data_direction dir)
709c4a7965SKim Phillips {
7181eb024cSKim Phillips 	dma_addr_t dma_addr = dma_map_single(dev, data, len, dir);
7281eb024cSKim Phillips 
739c4a7965SKim Phillips 	talitos_ptr->len = cpu_to_be16(len);
7481eb024cSKim Phillips 	to_talitos_ptr(talitos_ptr, dma_addr);
759c4a7965SKim Phillips 	talitos_ptr->j_extent = extent;
769c4a7965SKim Phillips }
779c4a7965SKim Phillips 
789c4a7965SKim Phillips /*
799c4a7965SKim Phillips  * unmap bus single (contiguous) h/w descriptor pointer
809c4a7965SKim Phillips  */
819c4a7965SKim Phillips static void unmap_single_talitos_ptr(struct device *dev,
829c4a7965SKim Phillips 				     struct talitos_ptr *talitos_ptr,
839c4a7965SKim Phillips 				     enum dma_data_direction dir)
849c4a7965SKim Phillips {
859c4a7965SKim Phillips 	dma_unmap_single(dev, be32_to_cpu(talitos_ptr->ptr),
869c4a7965SKim Phillips 			 be16_to_cpu(talitos_ptr->len), dir);
879c4a7965SKim Phillips }
889c4a7965SKim Phillips 
899c4a7965SKim Phillips static int reset_channel(struct device *dev, int ch)
909c4a7965SKim Phillips {
919c4a7965SKim Phillips 	struct talitos_private *priv = dev_get_drvdata(dev);
929c4a7965SKim Phillips 	unsigned int timeout = TALITOS_TIMEOUT;
939c4a7965SKim Phillips 
94ad42d5fcSKim Phillips 	setbits32(priv->chan[ch].reg + TALITOS_CCCR, TALITOS_CCCR_RESET);
959c4a7965SKim Phillips 
96ad42d5fcSKim Phillips 	while ((in_be32(priv->chan[ch].reg + TALITOS_CCCR) & TALITOS_CCCR_RESET)
979c4a7965SKim Phillips 	       && --timeout)
989c4a7965SKim Phillips 		cpu_relax();
999c4a7965SKim Phillips 
1009c4a7965SKim Phillips 	if (timeout == 0) {
1019c4a7965SKim Phillips 		dev_err(dev, "failed to reset channel %d\n", ch);
1029c4a7965SKim Phillips 		return -EIO;
1039c4a7965SKim Phillips 	}
1049c4a7965SKim Phillips 
10581eb024cSKim Phillips 	/* set 36-bit addressing, done writeback enable and done IRQ enable */
106ad42d5fcSKim Phillips 	setbits32(priv->chan[ch].reg + TALITOS_CCCR_LO, TALITOS_CCCR_LO_EAE |
10781eb024cSKim Phillips 		  TALITOS_CCCR_LO_CDWE | TALITOS_CCCR_LO_CDIE);
1089c4a7965SKim Phillips 
109fe5720e2SKim Phillips 	/* and ICCR writeback, if available */
110fe5720e2SKim Phillips 	if (priv->features & TALITOS_FTR_HW_AUTH_CHECK)
111ad42d5fcSKim Phillips 		setbits32(priv->chan[ch].reg + TALITOS_CCCR_LO,
112fe5720e2SKim Phillips 		          TALITOS_CCCR_LO_IWSE);
113fe5720e2SKim Phillips 
1149c4a7965SKim Phillips 	return 0;
1159c4a7965SKim Phillips }
1169c4a7965SKim Phillips 
1179c4a7965SKim Phillips static int reset_device(struct device *dev)
1189c4a7965SKim Phillips {
1199c4a7965SKim Phillips 	struct talitos_private *priv = dev_get_drvdata(dev);
1209c4a7965SKim Phillips 	unsigned int timeout = TALITOS_TIMEOUT;
121c3e337f8SKim Phillips 	u32 mcr = TALITOS_MCR_SWR;
1229c4a7965SKim Phillips 
123c3e337f8SKim Phillips 	setbits32(priv->reg + TALITOS_MCR, mcr);
1249c4a7965SKim Phillips 
1259c4a7965SKim Phillips 	while ((in_be32(priv->reg + TALITOS_MCR) & TALITOS_MCR_SWR)
1269c4a7965SKim Phillips 	       && --timeout)
1279c4a7965SKim Phillips 		cpu_relax();
1289c4a7965SKim Phillips 
1292cdba3cfSKim Phillips 	if (priv->irq[1]) {
130c3e337f8SKim Phillips 		mcr = TALITOS_MCR_RCA1 | TALITOS_MCR_RCA3;
131c3e337f8SKim Phillips 		setbits32(priv->reg + TALITOS_MCR, mcr);
132c3e337f8SKim Phillips 	}
133c3e337f8SKim Phillips 
1349c4a7965SKim Phillips 	if (timeout == 0) {
1359c4a7965SKim Phillips 		dev_err(dev, "failed to reset device\n");
1369c4a7965SKim Phillips 		return -EIO;
1379c4a7965SKim Phillips 	}
1389c4a7965SKim Phillips 
1399c4a7965SKim Phillips 	return 0;
1409c4a7965SKim Phillips }
1419c4a7965SKim Phillips 
1429c4a7965SKim Phillips /*
1439c4a7965SKim Phillips  * Reset and initialize the device
1449c4a7965SKim Phillips  */
1459c4a7965SKim Phillips static int init_device(struct device *dev)
1469c4a7965SKim Phillips {
1479c4a7965SKim Phillips 	struct talitos_private *priv = dev_get_drvdata(dev);
1489c4a7965SKim Phillips 	int ch, err;
1499c4a7965SKim Phillips 
1509c4a7965SKim Phillips 	/*
1519c4a7965SKim Phillips 	 * Master reset
1529c4a7965SKim Phillips 	 * errata documentation: warning: certain SEC interrupts
1539c4a7965SKim Phillips 	 * are not fully cleared by writing the MCR:SWR bit,
1549c4a7965SKim Phillips 	 * set bit twice to completely reset
1559c4a7965SKim Phillips 	 */
1569c4a7965SKim Phillips 	err = reset_device(dev);
1579c4a7965SKim Phillips 	if (err)
1589c4a7965SKim Phillips 		return err;
1599c4a7965SKim Phillips 
1609c4a7965SKim Phillips 	err = reset_device(dev);
1619c4a7965SKim Phillips 	if (err)
1629c4a7965SKim Phillips 		return err;
1639c4a7965SKim Phillips 
1649c4a7965SKim Phillips 	/* reset channels */
1659c4a7965SKim Phillips 	for (ch = 0; ch < priv->num_channels; ch++) {
1669c4a7965SKim Phillips 		err = reset_channel(dev, ch);
1679c4a7965SKim Phillips 		if (err)
1689c4a7965SKim Phillips 			return err;
1699c4a7965SKim Phillips 	}
1709c4a7965SKim Phillips 
1719c4a7965SKim Phillips 	/* enable channel done and error interrupts */
1729c4a7965SKim Phillips 	setbits32(priv->reg + TALITOS_IMR, TALITOS_IMR_INIT);
1739c4a7965SKim Phillips 	setbits32(priv->reg + TALITOS_IMR_LO, TALITOS_IMR_LO_INIT);
1749c4a7965SKim Phillips 
175fe5720e2SKim Phillips 	/* disable integrity check error interrupts (use writeback instead) */
176fe5720e2SKim Phillips 	if (priv->features & TALITOS_FTR_HW_AUTH_CHECK)
177fe5720e2SKim Phillips 		setbits32(priv->reg + TALITOS_MDEUICR_LO,
178fe5720e2SKim Phillips 		          TALITOS_MDEUICR_LO_ICE);
179fe5720e2SKim Phillips 
1809c4a7965SKim Phillips 	return 0;
1819c4a7965SKim Phillips }
1829c4a7965SKim Phillips 
1839c4a7965SKim Phillips /**
1849c4a7965SKim Phillips  * talitos_submit - submits a descriptor to the device for processing
1859c4a7965SKim Phillips  * @dev:	the SEC device to be used
1865228f0f7SKim Phillips  * @ch:		the SEC device channel to be used
1879c4a7965SKim Phillips  * @desc:	the descriptor to be processed by the device
1889c4a7965SKim Phillips  * @callback:	whom to call when processing is complete
1899c4a7965SKim Phillips  * @context:	a handle for use by caller (optional)
1909c4a7965SKim Phillips  *
1919c4a7965SKim Phillips  * desc must contain valid dma-mapped (bus physical) address pointers.
1929c4a7965SKim Phillips  * callback must check err and feedback in descriptor header
1939c4a7965SKim Phillips  * for device processing status.
1949c4a7965SKim Phillips  */
195865d5061SHoria Geanta int talitos_submit(struct device *dev, int ch, struct talitos_desc *desc,
1969c4a7965SKim Phillips 		   void (*callback)(struct device *dev,
1979c4a7965SKim Phillips 				    struct talitos_desc *desc,
1989c4a7965SKim Phillips 				    void *context, int error),
1999c4a7965SKim Phillips 		   void *context)
2009c4a7965SKim Phillips {
2019c4a7965SKim Phillips 	struct talitos_private *priv = dev_get_drvdata(dev);
2029c4a7965SKim Phillips 	struct talitos_request *request;
2035228f0f7SKim Phillips 	unsigned long flags;
2049c4a7965SKim Phillips 	int head;
2059c4a7965SKim Phillips 
2064b992628SKim Phillips 	spin_lock_irqsave(&priv->chan[ch].head_lock, flags);
2079c4a7965SKim Phillips 
2084b992628SKim Phillips 	if (!atomic_inc_not_zero(&priv->chan[ch].submit_count)) {
209ec6644d6SKim Phillips 		/* h/w fifo is full */
2104b992628SKim Phillips 		spin_unlock_irqrestore(&priv->chan[ch].head_lock, flags);
2119c4a7965SKim Phillips 		return -EAGAIN;
2129c4a7965SKim Phillips 	}
2139c4a7965SKim Phillips 
2144b992628SKim Phillips 	head = priv->chan[ch].head;
2154b992628SKim Phillips 	request = &priv->chan[ch].fifo[head];
216ec6644d6SKim Phillips 
2179c4a7965SKim Phillips 	/* map descriptor and save caller data */
2189c4a7965SKim Phillips 	request->dma_desc = dma_map_single(dev, desc, sizeof(*desc),
2199c4a7965SKim Phillips 					   DMA_BIDIRECTIONAL);
2209c4a7965SKim Phillips 	request->callback = callback;
2219c4a7965SKim Phillips 	request->context = context;
2229c4a7965SKim Phillips 
2239c4a7965SKim Phillips 	/* increment fifo head */
2244b992628SKim Phillips 	priv->chan[ch].head = (priv->chan[ch].head + 1) & (priv->fifo_len - 1);
2259c4a7965SKim Phillips 
2269c4a7965SKim Phillips 	smp_wmb();
2279c4a7965SKim Phillips 	request->desc = desc;
2289c4a7965SKim Phillips 
2299c4a7965SKim Phillips 	/* GO! */
2309c4a7965SKim Phillips 	wmb();
231ad42d5fcSKim Phillips 	out_be32(priv->chan[ch].reg + TALITOS_FF,
232ad42d5fcSKim Phillips 		 upper_32_bits(request->dma_desc));
233ad42d5fcSKim Phillips 	out_be32(priv->chan[ch].reg + TALITOS_FF_LO,
234a752447aSKim Phillips 		 lower_32_bits(request->dma_desc));
2359c4a7965SKim Phillips 
2364b992628SKim Phillips 	spin_unlock_irqrestore(&priv->chan[ch].head_lock, flags);
2379c4a7965SKim Phillips 
2389c4a7965SKim Phillips 	return -EINPROGRESS;
2399c4a7965SKim Phillips }
240865d5061SHoria Geanta EXPORT_SYMBOL(talitos_submit);
2419c4a7965SKim Phillips 
2429c4a7965SKim Phillips /*
2439c4a7965SKim Phillips  * process what was done, notify callback of error if not
2449c4a7965SKim Phillips  */
2459c4a7965SKim Phillips static void flush_channel(struct device *dev, int ch, int error, int reset_ch)
2469c4a7965SKim Phillips {
2479c4a7965SKim Phillips 	struct talitos_private *priv = dev_get_drvdata(dev);
2489c4a7965SKim Phillips 	struct talitos_request *request, saved_req;
2499c4a7965SKim Phillips 	unsigned long flags;
2509c4a7965SKim Phillips 	int tail, status;
2519c4a7965SKim Phillips 
2524b992628SKim Phillips 	spin_lock_irqsave(&priv->chan[ch].tail_lock, flags);
2539c4a7965SKim Phillips 
2544b992628SKim Phillips 	tail = priv->chan[ch].tail;
2554b992628SKim Phillips 	while (priv->chan[ch].fifo[tail].desc) {
2564b992628SKim Phillips 		request = &priv->chan[ch].fifo[tail];
2579c4a7965SKim Phillips 
2589c4a7965SKim Phillips 		/* descriptors with their done bits set don't get the error */
2599c4a7965SKim Phillips 		rmb();
260ca38a814SLee Nipper 		if ((request->desc->hdr & DESC_HDR_DONE) == DESC_HDR_DONE)
2619c4a7965SKim Phillips 			status = 0;
262ca38a814SLee Nipper 		else
2639c4a7965SKim Phillips 			if (!error)
2649c4a7965SKim Phillips 				break;
2659c4a7965SKim Phillips 			else
2669c4a7965SKim Phillips 				status = error;
2679c4a7965SKim Phillips 
2689c4a7965SKim Phillips 		dma_unmap_single(dev, request->dma_desc,
269e938e465SKim Phillips 				 sizeof(struct talitos_desc),
270e938e465SKim Phillips 				 DMA_BIDIRECTIONAL);
2719c4a7965SKim Phillips 
2729c4a7965SKim Phillips 		/* copy entries so we can call callback outside lock */
2739c4a7965SKim Phillips 		saved_req.desc = request->desc;
2749c4a7965SKim Phillips 		saved_req.callback = request->callback;
2759c4a7965SKim Phillips 		saved_req.context = request->context;
2769c4a7965SKim Phillips 
2779c4a7965SKim Phillips 		/* release request entry in fifo */
2789c4a7965SKim Phillips 		smp_wmb();
2799c4a7965SKim Phillips 		request->desc = NULL;
2809c4a7965SKim Phillips 
2819c4a7965SKim Phillips 		/* increment fifo tail */
2824b992628SKim Phillips 		priv->chan[ch].tail = (tail + 1) & (priv->fifo_len - 1);
2839c4a7965SKim Phillips 
2844b992628SKim Phillips 		spin_unlock_irqrestore(&priv->chan[ch].tail_lock, flags);
285ec6644d6SKim Phillips 
2864b992628SKim Phillips 		atomic_dec(&priv->chan[ch].submit_count);
287ec6644d6SKim Phillips 
2889c4a7965SKim Phillips 		saved_req.callback(dev, saved_req.desc, saved_req.context,
2899c4a7965SKim Phillips 				   status);
2909c4a7965SKim Phillips 		/* channel may resume processing in single desc error case */
2919c4a7965SKim Phillips 		if (error && !reset_ch && status == error)
2929c4a7965SKim Phillips 			return;
2934b992628SKim Phillips 		spin_lock_irqsave(&priv->chan[ch].tail_lock, flags);
2944b992628SKim Phillips 		tail = priv->chan[ch].tail;
2959c4a7965SKim Phillips 	}
2969c4a7965SKim Phillips 
2974b992628SKim Phillips 	spin_unlock_irqrestore(&priv->chan[ch].tail_lock, flags);
2989c4a7965SKim Phillips }
2999c4a7965SKim Phillips 
3009c4a7965SKim Phillips /*
3019c4a7965SKim Phillips  * process completed requests for channels that have done status
3029c4a7965SKim Phillips  */
303c3e337f8SKim Phillips #define DEF_TALITOS_DONE(name, ch_done_mask)				\
304c3e337f8SKim Phillips static void talitos_done_##name(unsigned long data)			\
305c3e337f8SKim Phillips {									\
306c3e337f8SKim Phillips 	struct device *dev = (struct device *)data;			\
307c3e337f8SKim Phillips 	struct talitos_private *priv = dev_get_drvdata(dev);		\
308511d63cbSHoria Geanta 	unsigned long flags;						\
309c3e337f8SKim Phillips 									\
310c3e337f8SKim Phillips 	if (ch_done_mask & 1)						\
311c3e337f8SKim Phillips 		flush_channel(dev, 0, 0, 0);				\
312c3e337f8SKim Phillips 	if (priv->num_channels == 1)					\
313c3e337f8SKim Phillips 		goto out;						\
314c3e337f8SKim Phillips 	if (ch_done_mask & (1 << 2))					\
315c3e337f8SKim Phillips 		flush_channel(dev, 1, 0, 0);				\
316c3e337f8SKim Phillips 	if (ch_done_mask & (1 << 4))					\
317c3e337f8SKim Phillips 		flush_channel(dev, 2, 0, 0);				\
318c3e337f8SKim Phillips 	if (ch_done_mask & (1 << 6))					\
319c3e337f8SKim Phillips 		flush_channel(dev, 3, 0, 0);				\
320c3e337f8SKim Phillips 									\
321c3e337f8SKim Phillips out:									\
322c3e337f8SKim Phillips 	/* At this point, all completed channels have been processed */	\
323c3e337f8SKim Phillips 	/* Unmask done interrupts for channels completed later on. */	\
324511d63cbSHoria Geanta 	spin_lock_irqsave(&priv->reg_lock, flags);			\
325c3e337f8SKim Phillips 	setbits32(priv->reg + TALITOS_IMR, ch_done_mask);		\
326c3e337f8SKim Phillips 	setbits32(priv->reg + TALITOS_IMR_LO, TALITOS_IMR_LO_INIT);	\
327511d63cbSHoria Geanta 	spin_unlock_irqrestore(&priv->reg_lock, flags);			\
3289c4a7965SKim Phillips }
329c3e337f8SKim Phillips DEF_TALITOS_DONE(4ch, TALITOS_ISR_4CHDONE)
330c3e337f8SKim Phillips DEF_TALITOS_DONE(ch0_2, TALITOS_ISR_CH_0_2_DONE)
331c3e337f8SKim Phillips DEF_TALITOS_DONE(ch1_3, TALITOS_ISR_CH_1_3_DONE)
3329c4a7965SKim Phillips 
3339c4a7965SKim Phillips /*
3349c4a7965SKim Phillips  * locate current (offending) descriptor
3359c4a7965SKim Phillips  */
3363e721aebSKim Phillips static u32 current_desc_hdr(struct device *dev, int ch)
3379c4a7965SKim Phillips {
3389c4a7965SKim Phillips 	struct talitos_private *priv = dev_get_drvdata(dev);
3394b992628SKim Phillips 	int tail = priv->chan[ch].tail;
3409c4a7965SKim Phillips 	dma_addr_t cur_desc;
3419c4a7965SKim Phillips 
342ad42d5fcSKim Phillips 	cur_desc = in_be32(priv->chan[ch].reg + TALITOS_CDPR_LO);
3439c4a7965SKim Phillips 
3444b992628SKim Phillips 	while (priv->chan[ch].fifo[tail].dma_desc != cur_desc) {
3459c4a7965SKim Phillips 		tail = (tail + 1) & (priv->fifo_len - 1);
3464b992628SKim Phillips 		if (tail == priv->chan[ch].tail) {
3479c4a7965SKim Phillips 			dev_err(dev, "couldn't locate current descriptor\n");
3483e721aebSKim Phillips 			return 0;
3499c4a7965SKim Phillips 		}
3509c4a7965SKim Phillips 	}
3519c4a7965SKim Phillips 
3523e721aebSKim Phillips 	return priv->chan[ch].fifo[tail].desc->hdr;
3539c4a7965SKim Phillips }
3549c4a7965SKim Phillips 
3559c4a7965SKim Phillips /*
3569c4a7965SKim Phillips  * user diagnostics; report root cause of error based on execution unit status
3579c4a7965SKim Phillips  */
3583e721aebSKim Phillips static void report_eu_error(struct device *dev, int ch, u32 desc_hdr)
3599c4a7965SKim Phillips {
3609c4a7965SKim Phillips 	struct talitos_private *priv = dev_get_drvdata(dev);
3619c4a7965SKim Phillips 	int i;
3629c4a7965SKim Phillips 
3633e721aebSKim Phillips 	if (!desc_hdr)
364ad42d5fcSKim Phillips 		desc_hdr = in_be32(priv->chan[ch].reg + TALITOS_DESCBUF);
3653e721aebSKim Phillips 
3663e721aebSKim Phillips 	switch (desc_hdr & DESC_HDR_SEL0_MASK) {
3679c4a7965SKim Phillips 	case DESC_HDR_SEL0_AFEU:
3689c4a7965SKim Phillips 		dev_err(dev, "AFEUISR 0x%08x_%08x\n",
3699c4a7965SKim Phillips 			in_be32(priv->reg + TALITOS_AFEUISR),
3709c4a7965SKim Phillips 			in_be32(priv->reg + TALITOS_AFEUISR_LO));
3719c4a7965SKim Phillips 		break;
3729c4a7965SKim Phillips 	case DESC_HDR_SEL0_DEU:
3739c4a7965SKim Phillips 		dev_err(dev, "DEUISR 0x%08x_%08x\n",
3749c4a7965SKim Phillips 			in_be32(priv->reg + TALITOS_DEUISR),
3759c4a7965SKim Phillips 			in_be32(priv->reg + TALITOS_DEUISR_LO));
3769c4a7965SKim Phillips 		break;
3779c4a7965SKim Phillips 	case DESC_HDR_SEL0_MDEUA:
3789c4a7965SKim Phillips 	case DESC_HDR_SEL0_MDEUB:
3799c4a7965SKim Phillips 		dev_err(dev, "MDEUISR 0x%08x_%08x\n",
3809c4a7965SKim Phillips 			in_be32(priv->reg + TALITOS_MDEUISR),
3819c4a7965SKim Phillips 			in_be32(priv->reg + TALITOS_MDEUISR_LO));
3829c4a7965SKim Phillips 		break;
3839c4a7965SKim Phillips 	case DESC_HDR_SEL0_RNG:
3849c4a7965SKim Phillips 		dev_err(dev, "RNGUISR 0x%08x_%08x\n",
3859c4a7965SKim Phillips 			in_be32(priv->reg + TALITOS_RNGUISR),
3869c4a7965SKim Phillips 			in_be32(priv->reg + TALITOS_RNGUISR_LO));
3879c4a7965SKim Phillips 		break;
3889c4a7965SKim Phillips 	case DESC_HDR_SEL0_PKEU:
3899c4a7965SKim Phillips 		dev_err(dev, "PKEUISR 0x%08x_%08x\n",
3909c4a7965SKim Phillips 			in_be32(priv->reg + TALITOS_PKEUISR),
3919c4a7965SKim Phillips 			in_be32(priv->reg + TALITOS_PKEUISR_LO));
3929c4a7965SKim Phillips 		break;
3939c4a7965SKim Phillips 	case DESC_HDR_SEL0_AESU:
3949c4a7965SKim Phillips 		dev_err(dev, "AESUISR 0x%08x_%08x\n",
3959c4a7965SKim Phillips 			in_be32(priv->reg + TALITOS_AESUISR),
3969c4a7965SKim Phillips 			in_be32(priv->reg + TALITOS_AESUISR_LO));
3979c4a7965SKim Phillips 		break;
3989c4a7965SKim Phillips 	case DESC_HDR_SEL0_CRCU:
3999c4a7965SKim Phillips 		dev_err(dev, "CRCUISR 0x%08x_%08x\n",
4009c4a7965SKim Phillips 			in_be32(priv->reg + TALITOS_CRCUISR),
4019c4a7965SKim Phillips 			in_be32(priv->reg + TALITOS_CRCUISR_LO));
4029c4a7965SKim Phillips 		break;
4039c4a7965SKim Phillips 	case DESC_HDR_SEL0_KEU:
4049c4a7965SKim Phillips 		dev_err(dev, "KEUISR 0x%08x_%08x\n",
4059c4a7965SKim Phillips 			in_be32(priv->reg + TALITOS_KEUISR),
4069c4a7965SKim Phillips 			in_be32(priv->reg + TALITOS_KEUISR_LO));
4079c4a7965SKim Phillips 		break;
4089c4a7965SKim Phillips 	}
4099c4a7965SKim Phillips 
4103e721aebSKim Phillips 	switch (desc_hdr & DESC_HDR_SEL1_MASK) {
4119c4a7965SKim Phillips 	case DESC_HDR_SEL1_MDEUA:
4129c4a7965SKim Phillips 	case DESC_HDR_SEL1_MDEUB:
4139c4a7965SKim Phillips 		dev_err(dev, "MDEUISR 0x%08x_%08x\n",
4149c4a7965SKim Phillips 			in_be32(priv->reg + TALITOS_MDEUISR),
4159c4a7965SKim Phillips 			in_be32(priv->reg + TALITOS_MDEUISR_LO));
4169c4a7965SKim Phillips 		break;
4179c4a7965SKim Phillips 	case DESC_HDR_SEL1_CRCU:
4189c4a7965SKim Phillips 		dev_err(dev, "CRCUISR 0x%08x_%08x\n",
4199c4a7965SKim Phillips 			in_be32(priv->reg + TALITOS_CRCUISR),
4209c4a7965SKim Phillips 			in_be32(priv->reg + TALITOS_CRCUISR_LO));
4219c4a7965SKim Phillips 		break;
4229c4a7965SKim Phillips 	}
4239c4a7965SKim Phillips 
4249c4a7965SKim Phillips 	for (i = 0; i < 8; i++)
4259c4a7965SKim Phillips 		dev_err(dev, "DESCBUF 0x%08x_%08x\n",
426ad42d5fcSKim Phillips 			in_be32(priv->chan[ch].reg + TALITOS_DESCBUF + 8*i),
427ad42d5fcSKim Phillips 			in_be32(priv->chan[ch].reg + TALITOS_DESCBUF_LO + 8*i));
4289c4a7965SKim Phillips }
4299c4a7965SKim Phillips 
4309c4a7965SKim Phillips /*
4319c4a7965SKim Phillips  * recover from error interrupts
4329c4a7965SKim Phillips  */
4335e718a09SKim Phillips static void talitos_error(struct device *dev, u32 isr, u32 isr_lo)
4349c4a7965SKim Phillips {
4359c4a7965SKim Phillips 	struct talitos_private *priv = dev_get_drvdata(dev);
4369c4a7965SKim Phillips 	unsigned int timeout = TALITOS_TIMEOUT;
4379c4a7965SKim Phillips 	int ch, error, reset_dev = 0, reset_ch = 0;
43840405f10SKim Phillips 	u32 v, v_lo;
4399c4a7965SKim Phillips 
4409c4a7965SKim Phillips 	for (ch = 0; ch < priv->num_channels; ch++) {
4419c4a7965SKim Phillips 		/* skip channels without errors */
4429c4a7965SKim Phillips 		if (!(isr & (1 << (ch * 2 + 1))))
4439c4a7965SKim Phillips 			continue;
4449c4a7965SKim Phillips 
4459c4a7965SKim Phillips 		error = -EINVAL;
4469c4a7965SKim Phillips 
447ad42d5fcSKim Phillips 		v = in_be32(priv->chan[ch].reg + TALITOS_CCPSR);
448ad42d5fcSKim Phillips 		v_lo = in_be32(priv->chan[ch].reg + TALITOS_CCPSR_LO);
4499c4a7965SKim Phillips 
4509c4a7965SKim Phillips 		if (v_lo & TALITOS_CCPSR_LO_DOF) {
4519c4a7965SKim Phillips 			dev_err(dev, "double fetch fifo overflow error\n");
4529c4a7965SKim Phillips 			error = -EAGAIN;
4539c4a7965SKim Phillips 			reset_ch = 1;
4549c4a7965SKim Phillips 		}
4559c4a7965SKim Phillips 		if (v_lo & TALITOS_CCPSR_LO_SOF) {
4569c4a7965SKim Phillips 			/* h/w dropped descriptor */
4579c4a7965SKim Phillips 			dev_err(dev, "single fetch fifo overflow error\n");
4589c4a7965SKim Phillips 			error = -EAGAIN;
4599c4a7965SKim Phillips 		}
4609c4a7965SKim Phillips 		if (v_lo & TALITOS_CCPSR_LO_MDTE)
4619c4a7965SKim Phillips 			dev_err(dev, "master data transfer error\n");
4629c4a7965SKim Phillips 		if (v_lo & TALITOS_CCPSR_LO_SGDLZ)
4639c4a7965SKim Phillips 			dev_err(dev, "s/g data length zero error\n");
4649c4a7965SKim Phillips 		if (v_lo & TALITOS_CCPSR_LO_FPZ)
4659c4a7965SKim Phillips 			dev_err(dev, "fetch pointer zero error\n");
4669c4a7965SKim Phillips 		if (v_lo & TALITOS_CCPSR_LO_IDH)
4679c4a7965SKim Phillips 			dev_err(dev, "illegal descriptor header error\n");
4689c4a7965SKim Phillips 		if (v_lo & TALITOS_CCPSR_LO_IEU)
4699c4a7965SKim Phillips 			dev_err(dev, "invalid execution unit error\n");
4709c4a7965SKim Phillips 		if (v_lo & TALITOS_CCPSR_LO_EU)
4713e721aebSKim Phillips 			report_eu_error(dev, ch, current_desc_hdr(dev, ch));
4729c4a7965SKim Phillips 		if (v_lo & TALITOS_CCPSR_LO_GB)
4739c4a7965SKim Phillips 			dev_err(dev, "gather boundary error\n");
4749c4a7965SKim Phillips 		if (v_lo & TALITOS_CCPSR_LO_GRL)
4759c4a7965SKim Phillips 			dev_err(dev, "gather return/length error\n");
4769c4a7965SKim Phillips 		if (v_lo & TALITOS_CCPSR_LO_SB)
4779c4a7965SKim Phillips 			dev_err(dev, "scatter boundary error\n");
4789c4a7965SKim Phillips 		if (v_lo & TALITOS_CCPSR_LO_SRL)
4799c4a7965SKim Phillips 			dev_err(dev, "scatter return/length error\n");
4809c4a7965SKim Phillips 
4819c4a7965SKim Phillips 		flush_channel(dev, ch, error, reset_ch);
4829c4a7965SKim Phillips 
4839c4a7965SKim Phillips 		if (reset_ch) {
4849c4a7965SKim Phillips 			reset_channel(dev, ch);
4859c4a7965SKim Phillips 		} else {
486ad42d5fcSKim Phillips 			setbits32(priv->chan[ch].reg + TALITOS_CCCR,
4879c4a7965SKim Phillips 				  TALITOS_CCCR_CONT);
488ad42d5fcSKim Phillips 			setbits32(priv->chan[ch].reg + TALITOS_CCCR_LO, 0);
489ad42d5fcSKim Phillips 			while ((in_be32(priv->chan[ch].reg + TALITOS_CCCR) &
4909c4a7965SKim Phillips 			       TALITOS_CCCR_CONT) && --timeout)
4919c4a7965SKim Phillips 				cpu_relax();
4929c4a7965SKim Phillips 			if (timeout == 0) {
4939c4a7965SKim Phillips 				dev_err(dev, "failed to restart channel %d\n",
4949c4a7965SKim Phillips 					ch);
4959c4a7965SKim Phillips 				reset_dev = 1;
4969c4a7965SKim Phillips 			}
4979c4a7965SKim Phillips 		}
4989c4a7965SKim Phillips 	}
499c3e337f8SKim Phillips 	if (reset_dev || isr & ~TALITOS_ISR_4CHERR || isr_lo) {
5009c4a7965SKim Phillips 		dev_err(dev, "done overflow, internal time out, or rngu error: "
5019c4a7965SKim Phillips 		        "ISR 0x%08x_%08x\n", isr, isr_lo);
5029c4a7965SKim Phillips 
5039c4a7965SKim Phillips 		/* purge request queues */
5049c4a7965SKim Phillips 		for (ch = 0; ch < priv->num_channels; ch++)
5059c4a7965SKim Phillips 			flush_channel(dev, ch, -EIO, 1);
5069c4a7965SKim Phillips 
5079c4a7965SKim Phillips 		/* reset and reinitialize the device */
5089c4a7965SKim Phillips 		init_device(dev);
5099c4a7965SKim Phillips 	}
5109c4a7965SKim Phillips }
5119c4a7965SKim Phillips 
512c3e337f8SKim Phillips #define DEF_TALITOS_INTERRUPT(name, ch_done_mask, ch_err_mask, tlet)	       \
513c3e337f8SKim Phillips static irqreturn_t talitos_interrupt_##name(int irq, void *data)	       \
514c3e337f8SKim Phillips {									       \
515c3e337f8SKim Phillips 	struct device *dev = data;					       \
516c3e337f8SKim Phillips 	struct talitos_private *priv = dev_get_drvdata(dev);		       \
517c3e337f8SKim Phillips 	u32 isr, isr_lo;						       \
518511d63cbSHoria Geanta 	unsigned long flags;						       \
519c3e337f8SKim Phillips 									       \
520511d63cbSHoria Geanta 	spin_lock_irqsave(&priv->reg_lock, flags);			       \
521c3e337f8SKim Phillips 	isr = in_be32(priv->reg + TALITOS_ISR);				       \
522c3e337f8SKim Phillips 	isr_lo = in_be32(priv->reg + TALITOS_ISR_LO);			       \
523c3e337f8SKim Phillips 	/* Acknowledge interrupt */					       \
524c3e337f8SKim Phillips 	out_be32(priv->reg + TALITOS_ICR, isr & (ch_done_mask | ch_err_mask)); \
525c3e337f8SKim Phillips 	out_be32(priv->reg + TALITOS_ICR_LO, isr_lo);			       \
526c3e337f8SKim Phillips 									       \
527511d63cbSHoria Geanta 	if (unlikely(isr & ch_err_mask || isr_lo)) {			       \
528511d63cbSHoria Geanta 		spin_unlock_irqrestore(&priv->reg_lock, flags);		       \
529511d63cbSHoria Geanta 		talitos_error(dev, isr & ch_err_mask, isr_lo);		       \
530511d63cbSHoria Geanta 	}								       \
531511d63cbSHoria Geanta 	else {								       \
532c3e337f8SKim Phillips 		if (likely(isr & ch_done_mask)) {			       \
533c3e337f8SKim Phillips 			/* mask further done interrupts. */		       \
534c3e337f8SKim Phillips 			clrbits32(priv->reg + TALITOS_IMR, ch_done_mask);      \
535c3e337f8SKim Phillips 			/* done_task will unmask done interrupts at exit */    \
536c3e337f8SKim Phillips 			tasklet_schedule(&priv->done_task[tlet]);	       \
537c3e337f8SKim Phillips 		}							       \
538511d63cbSHoria Geanta 		spin_unlock_irqrestore(&priv->reg_lock, flags);		       \
539511d63cbSHoria Geanta 	}								       \
540c3e337f8SKim Phillips 									       \
541c3e337f8SKim Phillips 	return (isr & (ch_done_mask | ch_err_mask) || isr_lo) ? IRQ_HANDLED :  \
542c3e337f8SKim Phillips 								IRQ_NONE;      \
5431c2e8811SLee Nipper }
544c3e337f8SKim Phillips DEF_TALITOS_INTERRUPT(4ch, TALITOS_ISR_4CHDONE, TALITOS_ISR_4CHERR, 0)
545c3e337f8SKim Phillips DEF_TALITOS_INTERRUPT(ch0_2, TALITOS_ISR_CH_0_2_DONE, TALITOS_ISR_CH_0_2_ERR, 0)
546c3e337f8SKim Phillips DEF_TALITOS_INTERRUPT(ch1_3, TALITOS_ISR_CH_1_3_DONE, TALITOS_ISR_CH_1_3_ERR, 1)
5479c4a7965SKim Phillips 
5489c4a7965SKim Phillips /*
5499c4a7965SKim Phillips  * hwrng
5509c4a7965SKim Phillips  */
5519c4a7965SKim Phillips static int talitos_rng_data_present(struct hwrng *rng, int wait)
5529c4a7965SKim Phillips {
5539c4a7965SKim Phillips 	struct device *dev = (struct device *)rng->priv;
5549c4a7965SKim Phillips 	struct talitos_private *priv = dev_get_drvdata(dev);
5559c4a7965SKim Phillips 	u32 ofl;
5569c4a7965SKim Phillips 	int i;
5579c4a7965SKim Phillips 
5589c4a7965SKim Phillips 	for (i = 0; i < 20; i++) {
5599c4a7965SKim Phillips 		ofl = in_be32(priv->reg + TALITOS_RNGUSR_LO) &
5609c4a7965SKim Phillips 		      TALITOS_RNGUSR_LO_OFL;
5619c4a7965SKim Phillips 		if (ofl || !wait)
5629c4a7965SKim Phillips 			break;
5639c4a7965SKim Phillips 		udelay(10);
5649c4a7965SKim Phillips 	}
5659c4a7965SKim Phillips 
5669c4a7965SKim Phillips 	return !!ofl;
5679c4a7965SKim Phillips }
5689c4a7965SKim Phillips 
5699c4a7965SKim Phillips static int talitos_rng_data_read(struct hwrng *rng, u32 *data)
5709c4a7965SKim Phillips {
5719c4a7965SKim Phillips 	struct device *dev = (struct device *)rng->priv;
5729c4a7965SKim Phillips 	struct talitos_private *priv = dev_get_drvdata(dev);
5739c4a7965SKim Phillips 
5749c4a7965SKim Phillips 	/* rng fifo requires 64-bit accesses */
5759c4a7965SKim Phillips 	*data = in_be32(priv->reg + TALITOS_RNGU_FIFO);
5769c4a7965SKim Phillips 	*data = in_be32(priv->reg + TALITOS_RNGU_FIFO_LO);
5779c4a7965SKim Phillips 
5789c4a7965SKim Phillips 	return sizeof(u32);
5799c4a7965SKim Phillips }
5809c4a7965SKim Phillips 
5819c4a7965SKim Phillips static int talitos_rng_init(struct hwrng *rng)
5829c4a7965SKim Phillips {
5839c4a7965SKim Phillips 	struct device *dev = (struct device *)rng->priv;
5849c4a7965SKim Phillips 	struct talitos_private *priv = dev_get_drvdata(dev);
5859c4a7965SKim Phillips 	unsigned int timeout = TALITOS_TIMEOUT;
5869c4a7965SKim Phillips 
5879c4a7965SKim Phillips 	setbits32(priv->reg + TALITOS_RNGURCR_LO, TALITOS_RNGURCR_LO_SR);
5889c4a7965SKim Phillips 	while (!(in_be32(priv->reg + TALITOS_RNGUSR_LO) & TALITOS_RNGUSR_LO_RD)
5899c4a7965SKim Phillips 	       && --timeout)
5909c4a7965SKim Phillips 		cpu_relax();
5919c4a7965SKim Phillips 	if (timeout == 0) {
5929c4a7965SKim Phillips 		dev_err(dev, "failed to reset rng hw\n");
5939c4a7965SKim Phillips 		return -ENODEV;
5949c4a7965SKim Phillips 	}
5959c4a7965SKim Phillips 
5969c4a7965SKim Phillips 	/* start generating */
5979c4a7965SKim Phillips 	setbits32(priv->reg + TALITOS_RNGUDSR_LO, 0);
5989c4a7965SKim Phillips 
5999c4a7965SKim Phillips 	return 0;
6009c4a7965SKim Phillips }
6019c4a7965SKim Phillips 
6029c4a7965SKim Phillips static int talitos_register_rng(struct device *dev)
6039c4a7965SKim Phillips {
6049c4a7965SKim Phillips 	struct talitos_private *priv = dev_get_drvdata(dev);
6059c4a7965SKim Phillips 
6069c4a7965SKim Phillips 	priv->rng.name		= dev_driver_string(dev),
6079c4a7965SKim Phillips 	priv->rng.init		= talitos_rng_init,
6089c4a7965SKim Phillips 	priv->rng.data_present	= talitos_rng_data_present,
6099c4a7965SKim Phillips 	priv->rng.data_read	= talitos_rng_data_read,
6109c4a7965SKim Phillips 	priv->rng.priv		= (unsigned long)dev;
6119c4a7965SKim Phillips 
6129c4a7965SKim Phillips 	return hwrng_register(&priv->rng);
6139c4a7965SKim Phillips }
6149c4a7965SKim Phillips 
6159c4a7965SKim Phillips static void talitos_unregister_rng(struct device *dev)
6169c4a7965SKim Phillips {
6179c4a7965SKim Phillips 	struct talitos_private *priv = dev_get_drvdata(dev);
6189c4a7965SKim Phillips 
6199c4a7965SKim Phillips 	hwrng_unregister(&priv->rng);
6209c4a7965SKim Phillips }
6219c4a7965SKim Phillips 
6229c4a7965SKim Phillips /*
6239c4a7965SKim Phillips  * crypto alg
6249c4a7965SKim Phillips  */
6259c4a7965SKim Phillips #define TALITOS_CRA_PRIORITY		3000
626357fb605SHoria Geanta #define TALITOS_MAX_KEY_SIZE		96
6273952f17eSLee Nipper #define TALITOS_MAX_IV_LENGTH		16 /* max of AES_BLOCK_SIZE, DES3_EDE_BLOCK_SIZE */
62870bcaca7SLee Nipper 
629497f2e6bSLee Nipper #define MD5_BLOCK_SIZE    64
6309c4a7965SKim Phillips 
6319c4a7965SKim Phillips struct talitos_ctx {
6329c4a7965SKim Phillips 	struct device *dev;
6335228f0f7SKim Phillips 	int ch;
6349c4a7965SKim Phillips 	__be32 desc_hdr_template;
6359c4a7965SKim Phillips 	u8 key[TALITOS_MAX_KEY_SIZE];
63670bcaca7SLee Nipper 	u8 iv[TALITOS_MAX_IV_LENGTH];
6379c4a7965SKim Phillips 	unsigned int keylen;
6389c4a7965SKim Phillips 	unsigned int enckeylen;
6399c4a7965SKim Phillips 	unsigned int authkeylen;
6409c4a7965SKim Phillips 	unsigned int authsize;
6419c4a7965SKim Phillips };
6429c4a7965SKim Phillips 
643497f2e6bSLee Nipper #define HASH_MAX_BLOCK_SIZE		SHA512_BLOCK_SIZE
644497f2e6bSLee Nipper #define TALITOS_MDEU_MAX_CONTEXT_SIZE	TALITOS_MDEU_CONTEXT_SIZE_SHA384_SHA512
645497f2e6bSLee Nipper 
646497f2e6bSLee Nipper struct talitos_ahash_req_ctx {
64760f208d7SKim Phillips 	u32 hw_context[TALITOS_MDEU_MAX_CONTEXT_SIZE / sizeof(u32)];
648497f2e6bSLee Nipper 	unsigned int hw_context_size;
649497f2e6bSLee Nipper 	u8 buf[HASH_MAX_BLOCK_SIZE];
650497f2e6bSLee Nipper 	u8 bufnext[HASH_MAX_BLOCK_SIZE];
65160f208d7SKim Phillips 	unsigned int swinit;
652497f2e6bSLee Nipper 	unsigned int first;
653497f2e6bSLee Nipper 	unsigned int last;
654497f2e6bSLee Nipper 	unsigned int to_hash_later;
6555e833bc4SLee Nipper 	u64 nbuf;
656497f2e6bSLee Nipper 	struct scatterlist bufsl[2];
657497f2e6bSLee Nipper 	struct scatterlist *psrc;
658497f2e6bSLee Nipper };
659497f2e6bSLee Nipper 
66056af8cd4SLee Nipper static int aead_setauthsize(struct crypto_aead *authenc,
6619c4a7965SKim Phillips 			    unsigned int authsize)
6629c4a7965SKim Phillips {
6639c4a7965SKim Phillips 	struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
6649c4a7965SKim Phillips 
6659c4a7965SKim Phillips 	ctx->authsize = authsize;
6669c4a7965SKim Phillips 
6679c4a7965SKim Phillips 	return 0;
6689c4a7965SKim Phillips }
6699c4a7965SKim Phillips 
67056af8cd4SLee Nipper static int aead_setkey(struct crypto_aead *authenc,
6719c4a7965SKim Phillips 		       const u8 *key, unsigned int keylen)
6729c4a7965SKim Phillips {
6739c4a7965SKim Phillips 	struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
6749c4a7965SKim Phillips 	struct rtattr *rta = (void *)key;
6759c4a7965SKim Phillips 	struct crypto_authenc_key_param *param;
6769c4a7965SKim Phillips 	unsigned int authkeylen;
6779c4a7965SKim Phillips 	unsigned int enckeylen;
6789c4a7965SKim Phillips 
6799c4a7965SKim Phillips 	if (!RTA_OK(rta, keylen))
6809c4a7965SKim Phillips 		goto badkey;
6819c4a7965SKim Phillips 
6829c4a7965SKim Phillips 	if (rta->rta_type != CRYPTO_AUTHENC_KEYA_PARAM)
6839c4a7965SKim Phillips 		goto badkey;
6849c4a7965SKim Phillips 
6859c4a7965SKim Phillips 	if (RTA_PAYLOAD(rta) < sizeof(*param))
6869c4a7965SKim Phillips 		goto badkey;
6879c4a7965SKim Phillips 
6889c4a7965SKim Phillips 	param = RTA_DATA(rta);
6899c4a7965SKim Phillips 	enckeylen = be32_to_cpu(param->enckeylen);
6909c4a7965SKim Phillips 
6919c4a7965SKim Phillips 	key += RTA_ALIGN(rta->rta_len);
6929c4a7965SKim Phillips 	keylen -= RTA_ALIGN(rta->rta_len);
6939c4a7965SKim Phillips 
6949c4a7965SKim Phillips 	if (keylen < enckeylen)
6959c4a7965SKim Phillips 		goto badkey;
6969c4a7965SKim Phillips 
6979c4a7965SKim Phillips 	authkeylen = keylen - enckeylen;
6989c4a7965SKim Phillips 
6999c4a7965SKim Phillips 	if (keylen > TALITOS_MAX_KEY_SIZE)
7009c4a7965SKim Phillips 		goto badkey;
7019c4a7965SKim Phillips 
7029c4a7965SKim Phillips 	memcpy(&ctx->key, key, keylen);
7039c4a7965SKim Phillips 
7049c4a7965SKim Phillips 	ctx->keylen = keylen;
7059c4a7965SKim Phillips 	ctx->enckeylen = enckeylen;
7069c4a7965SKim Phillips 	ctx->authkeylen = authkeylen;
7079c4a7965SKim Phillips 
7089c4a7965SKim Phillips 	return 0;
7099c4a7965SKim Phillips 
7109c4a7965SKim Phillips badkey:
7119c4a7965SKim Phillips 	crypto_aead_set_flags(authenc, CRYPTO_TFM_RES_BAD_KEY_LEN);
7129c4a7965SKim Phillips 	return -EINVAL;
7139c4a7965SKim Phillips }
7149c4a7965SKim Phillips 
7159c4a7965SKim Phillips /*
71656af8cd4SLee Nipper  * talitos_edesc - s/w-extended descriptor
7179c4a7965SKim Phillips  * @src_nents: number of segments in input scatterlist
7189c4a7965SKim Phillips  * @dst_nents: number of segments in output scatterlist
7199c4a7965SKim Phillips  * @dma_len: length of dma mapped link_tbl space
7209c4a7965SKim Phillips  * @dma_link_tbl: bus physical address of link_tbl
7219c4a7965SKim Phillips  * @desc: h/w descriptor
7229c4a7965SKim Phillips  * @link_tbl: input and output h/w link tables (if {src,dst}_nents > 1)
7239c4a7965SKim Phillips  *
7249c4a7965SKim Phillips  * if decrypting (with authcheck), or either one of src_nents or dst_nents
7259c4a7965SKim Phillips  * is greater than 1, an integrity check value is concatenated to the end
7269c4a7965SKim Phillips  * of link_tbl data
7279c4a7965SKim Phillips  */
72856af8cd4SLee Nipper struct talitos_edesc {
7299c4a7965SKim Phillips 	int src_nents;
7309c4a7965SKim Phillips 	int dst_nents;
7314de9d0b5SLee Nipper 	int src_is_chained;
7324de9d0b5SLee Nipper 	int dst_is_chained;
7339c4a7965SKim Phillips 	int dma_len;
7349c4a7965SKim Phillips 	dma_addr_t dma_link_tbl;
7359c4a7965SKim Phillips 	struct talitos_desc desc;
7369c4a7965SKim Phillips 	struct talitos_ptr link_tbl[0];
7379c4a7965SKim Phillips };
7389c4a7965SKim Phillips 
7394de9d0b5SLee Nipper static int talitos_map_sg(struct device *dev, struct scatterlist *sg,
7404de9d0b5SLee Nipper 			  unsigned int nents, enum dma_data_direction dir,
7414de9d0b5SLee Nipper 			  int chained)
7424de9d0b5SLee Nipper {
7434de9d0b5SLee Nipper 	if (unlikely(chained))
7444de9d0b5SLee Nipper 		while (sg) {
7454de9d0b5SLee Nipper 			dma_map_sg(dev, sg, 1, dir);
7464de9d0b5SLee Nipper 			sg = scatterwalk_sg_next(sg);
7474de9d0b5SLee Nipper 		}
7484de9d0b5SLee Nipper 	else
7494de9d0b5SLee Nipper 		dma_map_sg(dev, sg, nents, dir);
7504de9d0b5SLee Nipper 	return nents;
7514de9d0b5SLee Nipper }
7524de9d0b5SLee Nipper 
7534de9d0b5SLee Nipper static void talitos_unmap_sg_chain(struct device *dev, struct scatterlist *sg,
7544de9d0b5SLee Nipper 				   enum dma_data_direction dir)
7554de9d0b5SLee Nipper {
7564de9d0b5SLee Nipper 	while (sg) {
7574de9d0b5SLee Nipper 		dma_unmap_sg(dev, sg, 1, dir);
7584de9d0b5SLee Nipper 		sg = scatterwalk_sg_next(sg);
7594de9d0b5SLee Nipper 	}
7604de9d0b5SLee Nipper }
7614de9d0b5SLee Nipper 
7624de9d0b5SLee Nipper static void talitos_sg_unmap(struct device *dev,
7634de9d0b5SLee Nipper 			     struct talitos_edesc *edesc,
7644de9d0b5SLee Nipper 			     struct scatterlist *src,
7654de9d0b5SLee Nipper 			     struct scatterlist *dst)
7664de9d0b5SLee Nipper {
7674de9d0b5SLee Nipper 	unsigned int src_nents = edesc->src_nents ? : 1;
7684de9d0b5SLee Nipper 	unsigned int dst_nents = edesc->dst_nents ? : 1;
7694de9d0b5SLee Nipper 
7704de9d0b5SLee Nipper 	if (src != dst) {
7714de9d0b5SLee Nipper 		if (edesc->src_is_chained)
7724de9d0b5SLee Nipper 			talitos_unmap_sg_chain(dev, src, DMA_TO_DEVICE);
7734de9d0b5SLee Nipper 		else
7744de9d0b5SLee Nipper 			dma_unmap_sg(dev, src, src_nents, DMA_TO_DEVICE);
7754de9d0b5SLee Nipper 
776497f2e6bSLee Nipper 		if (dst) {
7774de9d0b5SLee Nipper 			if (edesc->dst_is_chained)
778497f2e6bSLee Nipper 				talitos_unmap_sg_chain(dev, dst,
779497f2e6bSLee Nipper 						       DMA_FROM_DEVICE);
7804de9d0b5SLee Nipper 			else
781497f2e6bSLee Nipper 				dma_unmap_sg(dev, dst, dst_nents,
782497f2e6bSLee Nipper 					     DMA_FROM_DEVICE);
783497f2e6bSLee Nipper 		}
7844de9d0b5SLee Nipper 	} else
7854de9d0b5SLee Nipper 		if (edesc->src_is_chained)
7864de9d0b5SLee Nipper 			talitos_unmap_sg_chain(dev, src, DMA_BIDIRECTIONAL);
7874de9d0b5SLee Nipper 		else
7884de9d0b5SLee Nipper 			dma_unmap_sg(dev, src, src_nents, DMA_BIDIRECTIONAL);
7894de9d0b5SLee Nipper }
7904de9d0b5SLee Nipper 
7919c4a7965SKim Phillips static void ipsec_esp_unmap(struct device *dev,
79256af8cd4SLee Nipper 			    struct talitos_edesc *edesc,
7939c4a7965SKim Phillips 			    struct aead_request *areq)
7949c4a7965SKim Phillips {
7959c4a7965SKim Phillips 	unmap_single_talitos_ptr(dev, &edesc->desc.ptr[6], DMA_FROM_DEVICE);
7969c4a7965SKim Phillips 	unmap_single_talitos_ptr(dev, &edesc->desc.ptr[3], DMA_TO_DEVICE);
7979c4a7965SKim Phillips 	unmap_single_talitos_ptr(dev, &edesc->desc.ptr[2], DMA_TO_DEVICE);
7989c4a7965SKim Phillips 	unmap_single_talitos_ptr(dev, &edesc->desc.ptr[0], DMA_TO_DEVICE);
7999c4a7965SKim Phillips 
8009c4a7965SKim Phillips 	dma_unmap_sg(dev, areq->assoc, 1, DMA_TO_DEVICE);
8019c4a7965SKim Phillips 
8024de9d0b5SLee Nipper 	talitos_sg_unmap(dev, edesc, areq->src, areq->dst);
8039c4a7965SKim Phillips 
8049c4a7965SKim Phillips 	if (edesc->dma_len)
8059c4a7965SKim Phillips 		dma_unmap_single(dev, edesc->dma_link_tbl, edesc->dma_len,
8069c4a7965SKim Phillips 				 DMA_BIDIRECTIONAL);
8079c4a7965SKim Phillips }
8089c4a7965SKim Phillips 
8099c4a7965SKim Phillips /*
8109c4a7965SKim Phillips  * ipsec_esp descriptor callbacks
8119c4a7965SKim Phillips  */
8129c4a7965SKim Phillips static void ipsec_esp_encrypt_done(struct device *dev,
8139c4a7965SKim Phillips 				   struct talitos_desc *desc, void *context,
8149c4a7965SKim Phillips 				   int err)
8159c4a7965SKim Phillips {
8169c4a7965SKim Phillips 	struct aead_request *areq = context;
8179c4a7965SKim Phillips 	struct crypto_aead *authenc = crypto_aead_reqtfm(areq);
8189c4a7965SKim Phillips 	struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
81919bbbc63SKim Phillips 	struct talitos_edesc *edesc;
8209c4a7965SKim Phillips 	struct scatterlist *sg;
8219c4a7965SKim Phillips 	void *icvdata;
8229c4a7965SKim Phillips 
82319bbbc63SKim Phillips 	edesc = container_of(desc, struct talitos_edesc, desc);
82419bbbc63SKim Phillips 
8259c4a7965SKim Phillips 	ipsec_esp_unmap(dev, edesc, areq);
8269c4a7965SKim Phillips 
8279c4a7965SKim Phillips 	/* copy the generated ICV to dst */
8289c4a7965SKim Phillips 	if (edesc->dma_len) {
8299c4a7965SKim Phillips 		icvdata = &edesc->link_tbl[edesc->src_nents +
830f3c85bc1SLee Nipper 					   edesc->dst_nents + 2];
8319c4a7965SKim Phillips 		sg = sg_last(areq->dst, edesc->dst_nents);
8329c4a7965SKim Phillips 		memcpy((char *)sg_virt(sg) + sg->length - ctx->authsize,
8339c4a7965SKim Phillips 		       icvdata, ctx->authsize);
8349c4a7965SKim Phillips 	}
8359c4a7965SKim Phillips 
8369c4a7965SKim Phillips 	kfree(edesc);
8379c4a7965SKim Phillips 
8389c4a7965SKim Phillips 	aead_request_complete(areq, err);
8399c4a7965SKim Phillips }
8409c4a7965SKim Phillips 
841fe5720e2SKim Phillips static void ipsec_esp_decrypt_swauth_done(struct device *dev,
842e938e465SKim Phillips 					  struct talitos_desc *desc,
843e938e465SKim Phillips 					  void *context, int err)
8449c4a7965SKim Phillips {
8459c4a7965SKim Phillips 	struct aead_request *req = context;
8469c4a7965SKim Phillips 	struct crypto_aead *authenc = crypto_aead_reqtfm(req);
8479c4a7965SKim Phillips 	struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
84819bbbc63SKim Phillips 	struct talitos_edesc *edesc;
8499c4a7965SKim Phillips 	struct scatterlist *sg;
8509c4a7965SKim Phillips 	void *icvdata;
8519c4a7965SKim Phillips 
85219bbbc63SKim Phillips 	edesc = container_of(desc, struct talitos_edesc, desc);
85319bbbc63SKim Phillips 
8549c4a7965SKim Phillips 	ipsec_esp_unmap(dev, edesc, req);
8559c4a7965SKim Phillips 
8569c4a7965SKim Phillips 	if (!err) {
8579c4a7965SKim Phillips 		/* auth check */
8589c4a7965SKim Phillips 		if (edesc->dma_len)
8599c4a7965SKim Phillips 			icvdata = &edesc->link_tbl[edesc->src_nents +
860f3c85bc1SLee Nipper 						   edesc->dst_nents + 2];
8619c4a7965SKim Phillips 		else
8629c4a7965SKim Phillips 			icvdata = &edesc->link_tbl[0];
8639c4a7965SKim Phillips 
8649c4a7965SKim Phillips 		sg = sg_last(req->dst, edesc->dst_nents ? : 1);
8659c4a7965SKim Phillips 		err = memcmp(icvdata, (char *)sg_virt(sg) + sg->length -
8669c4a7965SKim Phillips 			     ctx->authsize, ctx->authsize) ? -EBADMSG : 0;
8679c4a7965SKim Phillips 	}
8689c4a7965SKim Phillips 
8699c4a7965SKim Phillips 	kfree(edesc);
8709c4a7965SKim Phillips 
8719c4a7965SKim Phillips 	aead_request_complete(req, err);
8729c4a7965SKim Phillips }
8739c4a7965SKim Phillips 
874fe5720e2SKim Phillips static void ipsec_esp_decrypt_hwauth_done(struct device *dev,
875e938e465SKim Phillips 					  struct talitos_desc *desc,
876e938e465SKim Phillips 					  void *context, int err)
877fe5720e2SKim Phillips {
878fe5720e2SKim Phillips 	struct aead_request *req = context;
87919bbbc63SKim Phillips 	struct talitos_edesc *edesc;
88019bbbc63SKim Phillips 
88119bbbc63SKim Phillips 	edesc = container_of(desc, struct talitos_edesc, desc);
882fe5720e2SKim Phillips 
883fe5720e2SKim Phillips 	ipsec_esp_unmap(dev, edesc, req);
884fe5720e2SKim Phillips 
885fe5720e2SKim Phillips 	/* check ICV auth status */
886e938e465SKim Phillips 	if (!err && ((desc->hdr_lo & DESC_HDR_LO_ICCR1_MASK) !=
887e938e465SKim Phillips 		     DESC_HDR_LO_ICCR1_PASS))
888fe5720e2SKim Phillips 		err = -EBADMSG;
889fe5720e2SKim Phillips 
890fe5720e2SKim Phillips 	kfree(edesc);
891fe5720e2SKim Phillips 
892fe5720e2SKim Phillips 	aead_request_complete(req, err);
893fe5720e2SKim Phillips }
894fe5720e2SKim Phillips 
8959c4a7965SKim Phillips /*
8969c4a7965SKim Phillips  * convert scatterlist to SEC h/w link table format
8979c4a7965SKim Phillips  * stop at cryptlen bytes
8989c4a7965SKim Phillips  */
89970bcaca7SLee Nipper static int sg_to_link_tbl(struct scatterlist *sg, int sg_count,
9009c4a7965SKim Phillips 			   int cryptlen, struct talitos_ptr *link_tbl_ptr)
9019c4a7965SKim Phillips {
90270bcaca7SLee Nipper 	int n_sg = sg_count;
90370bcaca7SLee Nipper 
90470bcaca7SLee Nipper 	while (n_sg--) {
90581eb024cSKim Phillips 		to_talitos_ptr(link_tbl_ptr, sg_dma_address(sg));
9069c4a7965SKim Phillips 		link_tbl_ptr->len = cpu_to_be16(sg_dma_len(sg));
9079c4a7965SKim Phillips 		link_tbl_ptr->j_extent = 0;
9089c4a7965SKim Phillips 		link_tbl_ptr++;
9099c4a7965SKim Phillips 		cryptlen -= sg_dma_len(sg);
9104de9d0b5SLee Nipper 		sg = scatterwalk_sg_next(sg);
9119c4a7965SKim Phillips 	}
9129c4a7965SKim Phillips 
91370bcaca7SLee Nipper 	/* adjust (decrease) last one (or two) entry's len to cryptlen */
9149c4a7965SKim Phillips 	link_tbl_ptr--;
915c0e741d4SKim Phillips 	while (be16_to_cpu(link_tbl_ptr->len) <= (-cryptlen)) {
91670bcaca7SLee Nipper 		/* Empty this entry, and move to previous one */
91770bcaca7SLee Nipper 		cryptlen += be16_to_cpu(link_tbl_ptr->len);
91870bcaca7SLee Nipper 		link_tbl_ptr->len = 0;
91970bcaca7SLee Nipper 		sg_count--;
92070bcaca7SLee Nipper 		link_tbl_ptr--;
92170bcaca7SLee Nipper 	}
9229c4a7965SKim Phillips 	link_tbl_ptr->len = cpu_to_be16(be16_to_cpu(link_tbl_ptr->len)
9239c4a7965SKim Phillips 					+ cryptlen);
9249c4a7965SKim Phillips 
9259c4a7965SKim Phillips 	/* tag end of link table */
9269c4a7965SKim Phillips 	link_tbl_ptr->j_extent = DESC_PTR_LNKTBL_RETURN;
92770bcaca7SLee Nipper 
92870bcaca7SLee Nipper 	return sg_count;
9299c4a7965SKim Phillips }
9309c4a7965SKim Phillips 
9319c4a7965SKim Phillips /*
9329c4a7965SKim Phillips  * fill in and submit ipsec_esp descriptor
9339c4a7965SKim Phillips  */
93456af8cd4SLee Nipper static int ipsec_esp(struct talitos_edesc *edesc, struct aead_request *areq,
9359c4a7965SKim Phillips 		     u8 *giv, u64 seq,
9369c4a7965SKim Phillips 		     void (*callback) (struct device *dev,
9379c4a7965SKim Phillips 				       struct talitos_desc *desc,
9389c4a7965SKim Phillips 				       void *context, int error))
9399c4a7965SKim Phillips {
9409c4a7965SKim Phillips 	struct crypto_aead *aead = crypto_aead_reqtfm(areq);
9419c4a7965SKim Phillips 	struct talitos_ctx *ctx = crypto_aead_ctx(aead);
9429c4a7965SKim Phillips 	struct device *dev = ctx->dev;
9439c4a7965SKim Phillips 	struct talitos_desc *desc = &edesc->desc;
9449c4a7965SKim Phillips 	unsigned int cryptlen = areq->cryptlen;
9459c4a7965SKim Phillips 	unsigned int authsize = ctx->authsize;
946e41256f1SKim Phillips 	unsigned int ivsize = crypto_aead_ivsize(aead);
947fa86a267SKim Phillips 	int sg_count, ret;
948fe5720e2SKim Phillips 	int sg_link_tbl_len;
9499c4a7965SKim Phillips 
9509c4a7965SKim Phillips 	/* hmac key */
9519c4a7965SKim Phillips 	map_single_talitos_ptr(dev, &desc->ptr[0], ctx->authkeylen, &ctx->key,
9529c4a7965SKim Phillips 			       0, DMA_TO_DEVICE);
9539c4a7965SKim Phillips 	/* hmac data */
954e41256f1SKim Phillips 	map_single_talitos_ptr(dev, &desc->ptr[1], areq->assoclen + ivsize,
955e41256f1SKim Phillips 			       sg_virt(areq->assoc), 0, DMA_TO_DEVICE);
9569c4a7965SKim Phillips 	/* cipher iv */
9579c4a7965SKim Phillips 	map_single_talitos_ptr(dev, &desc->ptr[2], ivsize, giv ?: areq->iv, 0,
9589c4a7965SKim Phillips 			       DMA_TO_DEVICE);
9599c4a7965SKim Phillips 
9609c4a7965SKim Phillips 	/* cipher key */
9619c4a7965SKim Phillips 	map_single_talitos_ptr(dev, &desc->ptr[3], ctx->enckeylen,
9629c4a7965SKim Phillips 			       (char *)&ctx->key + ctx->authkeylen, 0,
9639c4a7965SKim Phillips 			       DMA_TO_DEVICE);
9649c4a7965SKim Phillips 
9659c4a7965SKim Phillips 	/*
9669c4a7965SKim Phillips 	 * cipher in
9679c4a7965SKim Phillips 	 * map and adjust cipher len to aead request cryptlen.
9689c4a7965SKim Phillips 	 * extent is bytes of HMAC postpended to ciphertext,
9699c4a7965SKim Phillips 	 * typically 12 for ipsec
9709c4a7965SKim Phillips 	 */
9719c4a7965SKim Phillips 	desc->ptr[4].len = cpu_to_be16(cryptlen);
9729c4a7965SKim Phillips 	desc->ptr[4].j_extent = authsize;
9739c4a7965SKim Phillips 
974e938e465SKim Phillips 	sg_count = talitos_map_sg(dev, areq->src, edesc->src_nents ? : 1,
975e938e465SKim Phillips 				  (areq->src == areq->dst) ? DMA_BIDIRECTIONAL
976e938e465SKim Phillips 							   : DMA_TO_DEVICE,
9774de9d0b5SLee Nipper 				  edesc->src_is_chained);
9789c4a7965SKim Phillips 
9799c4a7965SKim Phillips 	if (sg_count == 1) {
98081eb024cSKim Phillips 		to_talitos_ptr(&desc->ptr[4], sg_dma_address(areq->src));
9819c4a7965SKim Phillips 	} else {
982fe5720e2SKim Phillips 		sg_link_tbl_len = cryptlen;
983fe5720e2SKim Phillips 
984962a9c99SKim Phillips 		if (edesc->desc.hdr & DESC_HDR_MODE1_MDEU_CICV)
985fe5720e2SKim Phillips 			sg_link_tbl_len = cryptlen + authsize;
986e938e465SKim Phillips 
987fe5720e2SKim Phillips 		sg_count = sg_to_link_tbl(areq->src, sg_count, sg_link_tbl_len,
9889c4a7965SKim Phillips 					  &edesc->link_tbl[0]);
98970bcaca7SLee Nipper 		if (sg_count > 1) {
9909c4a7965SKim Phillips 			desc->ptr[4].j_extent |= DESC_PTR_LNKTBL_JUMP;
99181eb024cSKim Phillips 			to_talitos_ptr(&desc->ptr[4], edesc->dma_link_tbl);
992e938e465SKim Phillips 			dma_sync_single_for_device(dev, edesc->dma_link_tbl,
993e938e465SKim Phillips 						   edesc->dma_len,
994e938e465SKim Phillips 						   DMA_BIDIRECTIONAL);
99570bcaca7SLee Nipper 		} else {
99670bcaca7SLee Nipper 			/* Only one segment now, so no link tbl needed */
99781eb024cSKim Phillips 			to_talitos_ptr(&desc->ptr[4],
99881eb024cSKim Phillips 				       sg_dma_address(areq->src));
99970bcaca7SLee Nipper 		}
10009c4a7965SKim Phillips 	}
10019c4a7965SKim Phillips 
10029c4a7965SKim Phillips 	/* cipher out */
10039c4a7965SKim Phillips 	desc->ptr[5].len = cpu_to_be16(cryptlen);
10049c4a7965SKim Phillips 	desc->ptr[5].j_extent = authsize;
10059c4a7965SKim Phillips 
1006e938e465SKim Phillips 	if (areq->src != areq->dst)
10074de9d0b5SLee Nipper 		sg_count = talitos_map_sg(dev, areq->dst,
10084de9d0b5SLee Nipper 					  edesc->dst_nents ? : 1,
10094de9d0b5SLee Nipper 					  DMA_FROM_DEVICE,
10104de9d0b5SLee Nipper 					  edesc->dst_is_chained);
10119c4a7965SKim Phillips 
10129c4a7965SKim Phillips 	if (sg_count == 1) {
101381eb024cSKim Phillips 		to_talitos_ptr(&desc->ptr[5], sg_dma_address(areq->dst));
10149c4a7965SKim Phillips 	} else {
10159c4a7965SKim Phillips 		struct talitos_ptr *link_tbl_ptr =
1016f3c85bc1SLee Nipper 			&edesc->link_tbl[edesc->src_nents + 1];
10179c4a7965SKim Phillips 
101881eb024cSKim Phillips 		to_talitos_ptr(&desc->ptr[5], edesc->dma_link_tbl +
101981eb024cSKim Phillips 			       (edesc->src_nents + 1) *
102081eb024cSKim Phillips 			       sizeof(struct talitos_ptr));
102170bcaca7SLee Nipper 		sg_count = sg_to_link_tbl(areq->dst, sg_count, cryptlen,
10229c4a7965SKim Phillips 					  link_tbl_ptr);
1023fe5720e2SKim Phillips 
1024f3c85bc1SLee Nipper 		/* Add an entry to the link table for ICV data */
10259c4a7965SKim Phillips 		link_tbl_ptr += sg_count - 1;
10269c4a7965SKim Phillips 		link_tbl_ptr->j_extent = 0;
1027f3c85bc1SLee Nipper 		sg_count++;
10289c4a7965SKim Phillips 		link_tbl_ptr++;
10299c4a7965SKim Phillips 		link_tbl_ptr->j_extent = DESC_PTR_LNKTBL_RETURN;
10309c4a7965SKim Phillips 		link_tbl_ptr->len = cpu_to_be16(authsize);
10319c4a7965SKim Phillips 
10329c4a7965SKim Phillips 		/* icv data follows link tables */
103381eb024cSKim Phillips 		to_talitos_ptr(link_tbl_ptr, edesc->dma_link_tbl +
103481eb024cSKim Phillips 			       (edesc->src_nents + edesc->dst_nents + 2) *
103581eb024cSKim Phillips 			       sizeof(struct talitos_ptr));
10369c4a7965SKim Phillips 		desc->ptr[5].j_extent |= DESC_PTR_LNKTBL_JUMP;
10379c4a7965SKim Phillips 		dma_sync_single_for_device(ctx->dev, edesc->dma_link_tbl,
10389c4a7965SKim Phillips 					   edesc->dma_len, DMA_BIDIRECTIONAL);
10399c4a7965SKim Phillips 	}
10409c4a7965SKim Phillips 
10419c4a7965SKim Phillips 	/* iv out */
10429c4a7965SKim Phillips 	map_single_talitos_ptr(dev, &desc->ptr[6], ivsize, ctx->iv, 0,
10439c4a7965SKim Phillips 			       DMA_FROM_DEVICE);
10449c4a7965SKim Phillips 
10455228f0f7SKim Phillips 	ret = talitos_submit(dev, ctx->ch, desc, callback, areq);
1046fa86a267SKim Phillips 	if (ret != -EINPROGRESS) {
1047fa86a267SKim Phillips 		ipsec_esp_unmap(dev, edesc, areq);
1048fa86a267SKim Phillips 		kfree(edesc);
1049fa86a267SKim Phillips 	}
1050fa86a267SKim Phillips 	return ret;
10519c4a7965SKim Phillips }
10529c4a7965SKim Phillips 
10539c4a7965SKim Phillips /*
10549c4a7965SKim Phillips  * derive number of elements in scatterlist
10559c4a7965SKim Phillips  */
10564de9d0b5SLee Nipper static int sg_count(struct scatterlist *sg_list, int nbytes, int *chained)
10579c4a7965SKim Phillips {
10589c4a7965SKim Phillips 	struct scatterlist *sg = sg_list;
10599c4a7965SKim Phillips 	int sg_nents = 0;
10609c4a7965SKim Phillips 
10614de9d0b5SLee Nipper 	*chained = 0;
10624de9d0b5SLee Nipper 	while (nbytes > 0) {
10639c4a7965SKim Phillips 		sg_nents++;
10649c4a7965SKim Phillips 		nbytes -= sg->length;
10654de9d0b5SLee Nipper 		if (!sg_is_last(sg) && (sg + 1)->length == 0)
10664de9d0b5SLee Nipper 			*chained = 1;
10674de9d0b5SLee Nipper 		sg = scatterwalk_sg_next(sg);
10689c4a7965SKim Phillips 	}
10699c4a7965SKim Phillips 
10709c4a7965SKim Phillips 	return sg_nents;
10719c4a7965SKim Phillips }
10729c4a7965SKim Phillips 
1073497f2e6bSLee Nipper /**
1074497f2e6bSLee Nipper  * sg_copy_end_to_buffer - Copy end data from SG list to a linear buffer
1075497f2e6bSLee Nipper  * @sgl:		 The SG list
1076497f2e6bSLee Nipper  * @nents:		 Number of SG entries
1077497f2e6bSLee Nipper  * @buf:		 Where to copy to
1078497f2e6bSLee Nipper  * @buflen:		 The number of bytes to copy
1079497f2e6bSLee Nipper  * @skip:		 The number of bytes to skip before copying.
1080497f2e6bSLee Nipper  *                       Note: skip + buflen should equal SG total size.
1081497f2e6bSLee Nipper  *
1082497f2e6bSLee Nipper  * Returns the number of copied bytes.
1083497f2e6bSLee Nipper  *
1084497f2e6bSLee Nipper  **/
1085497f2e6bSLee Nipper static size_t sg_copy_end_to_buffer(struct scatterlist *sgl, unsigned int nents,
1086497f2e6bSLee Nipper 				    void *buf, size_t buflen, unsigned int skip)
1087497f2e6bSLee Nipper {
1088497f2e6bSLee Nipper 	unsigned int offset = 0;
1089497f2e6bSLee Nipper 	unsigned int boffset = 0;
1090497f2e6bSLee Nipper 	struct sg_mapping_iter miter;
1091497f2e6bSLee Nipper 	unsigned long flags;
1092497f2e6bSLee Nipper 	unsigned int sg_flags = SG_MITER_ATOMIC;
1093497f2e6bSLee Nipper 	size_t total_buffer = buflen + skip;
1094497f2e6bSLee Nipper 
1095497f2e6bSLee Nipper 	sg_flags |= SG_MITER_FROM_SG;
1096497f2e6bSLee Nipper 
1097497f2e6bSLee Nipper 	sg_miter_start(&miter, sgl, nents, sg_flags);
1098497f2e6bSLee Nipper 
1099497f2e6bSLee Nipper 	local_irq_save(flags);
1100497f2e6bSLee Nipper 
1101497f2e6bSLee Nipper 	while (sg_miter_next(&miter) && offset < total_buffer) {
1102497f2e6bSLee Nipper 		unsigned int len;
1103497f2e6bSLee Nipper 		unsigned int ignore;
1104497f2e6bSLee Nipper 
1105497f2e6bSLee Nipper 		if ((offset + miter.length) > skip) {
1106497f2e6bSLee Nipper 			if (offset < skip) {
1107497f2e6bSLee Nipper 				/* Copy part of this segment */
1108497f2e6bSLee Nipper 				ignore = skip - offset;
1109497f2e6bSLee Nipper 				len = miter.length - ignore;
11107260042bSLee Nipper 				if (boffset + len > buflen)
11117260042bSLee Nipper 					len = buflen - boffset;
1112497f2e6bSLee Nipper 				memcpy(buf + boffset, miter.addr + ignore, len);
1113497f2e6bSLee Nipper 			} else {
11147260042bSLee Nipper 				/* Copy all of this segment (up to buflen) */
1115497f2e6bSLee Nipper 				len = miter.length;
11167260042bSLee Nipper 				if (boffset + len > buflen)
11177260042bSLee Nipper 					len = buflen - boffset;
1118497f2e6bSLee Nipper 				memcpy(buf + boffset, miter.addr, len);
1119497f2e6bSLee Nipper 			}
1120497f2e6bSLee Nipper 			boffset += len;
1121497f2e6bSLee Nipper 		}
1122497f2e6bSLee Nipper 		offset += miter.length;
1123497f2e6bSLee Nipper 	}
1124497f2e6bSLee Nipper 
1125497f2e6bSLee Nipper 	sg_miter_stop(&miter);
1126497f2e6bSLee Nipper 
1127497f2e6bSLee Nipper 	local_irq_restore(flags);
1128497f2e6bSLee Nipper 	return boffset;
1129497f2e6bSLee Nipper }
1130497f2e6bSLee Nipper 
11319c4a7965SKim Phillips /*
113256af8cd4SLee Nipper  * allocate and map the extended descriptor
11339c4a7965SKim Phillips  */
11344de9d0b5SLee Nipper static struct talitos_edesc *talitos_edesc_alloc(struct device *dev,
11354de9d0b5SLee Nipper 						 struct scatterlist *src,
11364de9d0b5SLee Nipper 						 struct scatterlist *dst,
1137497f2e6bSLee Nipper 						 int hash_result,
11384de9d0b5SLee Nipper 						 unsigned int cryptlen,
11394de9d0b5SLee Nipper 						 unsigned int authsize,
11404de9d0b5SLee Nipper 						 int icv_stashing,
11414de9d0b5SLee Nipper 						 u32 cryptoflags)
11429c4a7965SKim Phillips {
114356af8cd4SLee Nipper 	struct talitos_edesc *edesc;
11449c4a7965SKim Phillips 	int src_nents, dst_nents, alloc_len, dma_len;
11454de9d0b5SLee Nipper 	int src_chained, dst_chained = 0;
11464de9d0b5SLee Nipper 	gfp_t flags = cryptoflags & CRYPTO_TFM_REQ_MAY_SLEEP ? GFP_KERNEL :
1147586725f8SKim Phillips 		      GFP_ATOMIC;
11489c4a7965SKim Phillips 
11494de9d0b5SLee Nipper 	if (cryptlen + authsize > TALITOS_MAX_DATA_LEN) {
11504de9d0b5SLee Nipper 		dev_err(dev, "length exceeds h/w max limit\n");
11519c4a7965SKim Phillips 		return ERR_PTR(-EINVAL);
11529c4a7965SKim Phillips 	}
11539c4a7965SKim Phillips 
11544de9d0b5SLee Nipper 	src_nents = sg_count(src, cryptlen + authsize, &src_chained);
11559c4a7965SKim Phillips 	src_nents = (src_nents == 1) ? 0 : src_nents;
11569c4a7965SKim Phillips 
1157497f2e6bSLee Nipper 	if (hash_result) {
1158497f2e6bSLee Nipper 		dst_nents = 0;
1159497f2e6bSLee Nipper 	} else {
11604de9d0b5SLee Nipper 		if (dst == src) {
11619c4a7965SKim Phillips 			dst_nents = src_nents;
11629c4a7965SKim Phillips 		} else {
1163497f2e6bSLee Nipper 			dst_nents = sg_count(dst, cryptlen + authsize,
1164497f2e6bSLee Nipper 					     &dst_chained);
1165695ad589SLee Nipper 			dst_nents = (dst_nents == 1) ? 0 : dst_nents;
11669c4a7965SKim Phillips 		}
1167497f2e6bSLee Nipper 	}
11689c4a7965SKim Phillips 
11699c4a7965SKim Phillips 	/*
11709c4a7965SKim Phillips 	 * allocate space for base edesc plus the link tables,
1171f3c85bc1SLee Nipper 	 * allowing for two separate entries for ICV and generated ICV (+ 2),
11729c4a7965SKim Phillips 	 * and the ICV data itself
11739c4a7965SKim Phillips 	 */
117456af8cd4SLee Nipper 	alloc_len = sizeof(struct talitos_edesc);
11759c4a7965SKim Phillips 	if (src_nents || dst_nents) {
1176f3c85bc1SLee Nipper 		dma_len = (src_nents + dst_nents + 2) *
11774de9d0b5SLee Nipper 				 sizeof(struct talitos_ptr) + authsize;
11789c4a7965SKim Phillips 		alloc_len += dma_len;
11799c4a7965SKim Phillips 	} else {
11809c4a7965SKim Phillips 		dma_len = 0;
11814de9d0b5SLee Nipper 		alloc_len += icv_stashing ? authsize : 0;
11829c4a7965SKim Phillips 	}
11839c4a7965SKim Phillips 
1184586725f8SKim Phillips 	edesc = kmalloc(alloc_len, GFP_DMA | flags);
11859c4a7965SKim Phillips 	if (!edesc) {
11864de9d0b5SLee Nipper 		dev_err(dev, "could not allocate edescriptor\n");
11879c4a7965SKim Phillips 		return ERR_PTR(-ENOMEM);
11889c4a7965SKim Phillips 	}
11899c4a7965SKim Phillips 
11909c4a7965SKim Phillips 	edesc->src_nents = src_nents;
11919c4a7965SKim Phillips 	edesc->dst_nents = dst_nents;
11924de9d0b5SLee Nipper 	edesc->src_is_chained = src_chained;
11934de9d0b5SLee Nipper 	edesc->dst_is_chained = dst_chained;
11949c4a7965SKim Phillips 	edesc->dma_len = dma_len;
1195497f2e6bSLee Nipper 	if (dma_len)
11964de9d0b5SLee Nipper 		edesc->dma_link_tbl = dma_map_single(dev, &edesc->link_tbl[0],
1197497f2e6bSLee Nipper 						     edesc->dma_len,
1198497f2e6bSLee Nipper 						     DMA_BIDIRECTIONAL);
11999c4a7965SKim Phillips 
12009c4a7965SKim Phillips 	return edesc;
12019c4a7965SKim Phillips }
12029c4a7965SKim Phillips 
12034de9d0b5SLee Nipper static struct talitos_edesc *aead_edesc_alloc(struct aead_request *areq,
12044de9d0b5SLee Nipper 					      int icv_stashing)
12054de9d0b5SLee Nipper {
12064de9d0b5SLee Nipper 	struct crypto_aead *authenc = crypto_aead_reqtfm(areq);
12074de9d0b5SLee Nipper 	struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
12084de9d0b5SLee Nipper 
1209497f2e6bSLee Nipper 	return talitos_edesc_alloc(ctx->dev, areq->src, areq->dst, 0,
12104de9d0b5SLee Nipper 				   areq->cryptlen, ctx->authsize, icv_stashing,
12114de9d0b5SLee Nipper 				   areq->base.flags);
12124de9d0b5SLee Nipper }
12134de9d0b5SLee Nipper 
121456af8cd4SLee Nipper static int aead_encrypt(struct aead_request *req)
12159c4a7965SKim Phillips {
12169c4a7965SKim Phillips 	struct crypto_aead *authenc = crypto_aead_reqtfm(req);
12179c4a7965SKim Phillips 	struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
121856af8cd4SLee Nipper 	struct talitos_edesc *edesc;
12199c4a7965SKim Phillips 
12209c4a7965SKim Phillips 	/* allocate extended descriptor */
12214de9d0b5SLee Nipper 	edesc = aead_edesc_alloc(req, 0);
12229c4a7965SKim Phillips 	if (IS_ERR(edesc))
12239c4a7965SKim Phillips 		return PTR_ERR(edesc);
12249c4a7965SKim Phillips 
12259c4a7965SKim Phillips 	/* set encrypt */
122670bcaca7SLee Nipper 	edesc->desc.hdr = ctx->desc_hdr_template | DESC_HDR_MODE0_ENCRYPT;
12279c4a7965SKim Phillips 
12289c4a7965SKim Phillips 	return ipsec_esp(edesc, req, NULL, 0, ipsec_esp_encrypt_done);
12299c4a7965SKim Phillips }
12309c4a7965SKim Phillips 
123156af8cd4SLee Nipper static int aead_decrypt(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);
12359c4a7965SKim Phillips 	unsigned int authsize = ctx->authsize;
1236fe5720e2SKim Phillips 	struct talitos_private *priv = dev_get_drvdata(ctx->dev);
123756af8cd4SLee Nipper 	struct talitos_edesc *edesc;
12389c4a7965SKim Phillips 	struct scatterlist *sg;
12399c4a7965SKim Phillips 	void *icvdata;
12409c4a7965SKim Phillips 
12419c4a7965SKim Phillips 	req->cryptlen -= authsize;
12429c4a7965SKim Phillips 
12439c4a7965SKim Phillips 	/* allocate extended descriptor */
12444de9d0b5SLee Nipper 	edesc = aead_edesc_alloc(req, 1);
12459c4a7965SKim Phillips 	if (IS_ERR(edesc))
12469c4a7965SKim Phillips 		return PTR_ERR(edesc);
12479c4a7965SKim Phillips 
1248fe5720e2SKim Phillips 	if ((priv->features & TALITOS_FTR_HW_AUTH_CHECK) &&
1249e938e465SKim Phillips 	    ((!edesc->src_nents && !edesc->dst_nents) ||
1250e938e465SKim Phillips 	     priv->features & TALITOS_FTR_SRC_LINK_TBL_LEN_INCLUDES_EXTENT)) {
1251fe5720e2SKim Phillips 
1252fe5720e2SKim Phillips 		/* decrypt and check the ICV */
1253e938e465SKim Phillips 		edesc->desc.hdr = ctx->desc_hdr_template |
1254e938e465SKim Phillips 				  DESC_HDR_DIR_INBOUND |
1255fe5720e2SKim Phillips 				  DESC_HDR_MODE1_MDEU_CICV;
1256fe5720e2SKim Phillips 
1257fe5720e2SKim Phillips 		/* reset integrity check result bits */
1258fe5720e2SKim Phillips 		edesc->desc.hdr_lo = 0;
1259fe5720e2SKim Phillips 
1260e938e465SKim Phillips 		return ipsec_esp(edesc, req, NULL, 0,
1261e938e465SKim Phillips 				 ipsec_esp_decrypt_hwauth_done);
1262fe5720e2SKim Phillips 
1263e938e465SKim Phillips 	}
1264fe5720e2SKim Phillips 
1265fe5720e2SKim Phillips 	/* Have to check the ICV with software */
1266fe5720e2SKim Phillips 	edesc->desc.hdr = ctx->desc_hdr_template | DESC_HDR_DIR_INBOUND;
1267fe5720e2SKim Phillips 
12689c4a7965SKim Phillips 	/* stash incoming ICV for later cmp with ICV generated by the h/w */
12699c4a7965SKim Phillips 	if (edesc->dma_len)
12709c4a7965SKim Phillips 		icvdata = &edesc->link_tbl[edesc->src_nents +
1271f3c85bc1SLee Nipper 					   edesc->dst_nents + 2];
12729c4a7965SKim Phillips 	else
12739c4a7965SKim Phillips 		icvdata = &edesc->link_tbl[0];
12749c4a7965SKim Phillips 
12759c4a7965SKim Phillips 	sg = sg_last(req->src, edesc->src_nents ? : 1);
12769c4a7965SKim Phillips 
12779c4a7965SKim Phillips 	memcpy(icvdata, (char *)sg_virt(sg) + sg->length - ctx->authsize,
12789c4a7965SKim Phillips 	       ctx->authsize);
12799c4a7965SKim Phillips 
1280fe5720e2SKim Phillips 	return ipsec_esp(edesc, req, NULL, 0, ipsec_esp_decrypt_swauth_done);
1281fe5720e2SKim Phillips }
12829c4a7965SKim Phillips 
128356af8cd4SLee Nipper static int aead_givencrypt(struct aead_givcrypt_request *req)
12849c4a7965SKim Phillips {
12859c4a7965SKim Phillips 	struct aead_request *areq = &req->areq;
12869c4a7965SKim Phillips 	struct crypto_aead *authenc = crypto_aead_reqtfm(areq);
12879c4a7965SKim Phillips 	struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
128856af8cd4SLee Nipper 	struct talitos_edesc *edesc;
12899c4a7965SKim Phillips 
12909c4a7965SKim Phillips 	/* allocate extended descriptor */
12914de9d0b5SLee Nipper 	edesc = aead_edesc_alloc(areq, 0);
12929c4a7965SKim Phillips 	if (IS_ERR(edesc))
12939c4a7965SKim Phillips 		return PTR_ERR(edesc);
12949c4a7965SKim Phillips 
12959c4a7965SKim Phillips 	/* set encrypt */
129670bcaca7SLee Nipper 	edesc->desc.hdr = ctx->desc_hdr_template | DESC_HDR_MODE0_ENCRYPT;
12979c4a7965SKim Phillips 
12989c4a7965SKim Phillips 	memcpy(req->giv, ctx->iv, crypto_aead_ivsize(authenc));
1299ba95487dSKim Phillips 	/* avoid consecutive packets going out with same IV */
1300ba95487dSKim Phillips 	*(__be64 *)req->giv ^= cpu_to_be64(req->seq);
13019c4a7965SKim Phillips 
13029c4a7965SKim Phillips 	return ipsec_esp(edesc, areq, req->giv, req->seq,
13039c4a7965SKim Phillips 			 ipsec_esp_encrypt_done);
13049c4a7965SKim Phillips }
13059c4a7965SKim Phillips 
13064de9d0b5SLee Nipper static int ablkcipher_setkey(struct crypto_ablkcipher *cipher,
13074de9d0b5SLee Nipper 			     const u8 *key, unsigned int keylen)
13084de9d0b5SLee Nipper {
13094de9d0b5SLee Nipper 	struct talitos_ctx *ctx = crypto_ablkcipher_ctx(cipher);
13104de9d0b5SLee Nipper 
13114de9d0b5SLee Nipper 	memcpy(&ctx->key, key, keylen);
13124de9d0b5SLee Nipper 	ctx->keylen = keylen;
13134de9d0b5SLee Nipper 
13144de9d0b5SLee Nipper 	return 0;
13154de9d0b5SLee Nipper }
13164de9d0b5SLee Nipper 
13174de9d0b5SLee Nipper static void common_nonsnoop_unmap(struct device *dev,
13184de9d0b5SLee Nipper 				  struct talitos_edesc *edesc,
13194de9d0b5SLee Nipper 				  struct ablkcipher_request *areq)
13204de9d0b5SLee Nipper {
13214de9d0b5SLee Nipper 	unmap_single_talitos_ptr(dev, &edesc->desc.ptr[5], DMA_FROM_DEVICE);
13224de9d0b5SLee Nipper 	unmap_single_talitos_ptr(dev, &edesc->desc.ptr[2], DMA_TO_DEVICE);
13234de9d0b5SLee Nipper 	unmap_single_talitos_ptr(dev, &edesc->desc.ptr[1], DMA_TO_DEVICE);
13244de9d0b5SLee Nipper 
13254de9d0b5SLee Nipper 	talitos_sg_unmap(dev, edesc, areq->src, areq->dst);
13264de9d0b5SLee Nipper 
13274de9d0b5SLee Nipper 	if (edesc->dma_len)
13284de9d0b5SLee Nipper 		dma_unmap_single(dev, edesc->dma_link_tbl, edesc->dma_len,
13294de9d0b5SLee Nipper 				 DMA_BIDIRECTIONAL);
13304de9d0b5SLee Nipper }
13314de9d0b5SLee Nipper 
13324de9d0b5SLee Nipper static void ablkcipher_done(struct device *dev,
13334de9d0b5SLee Nipper 			    struct talitos_desc *desc, void *context,
13344de9d0b5SLee Nipper 			    int err)
13354de9d0b5SLee Nipper {
13364de9d0b5SLee Nipper 	struct ablkcipher_request *areq = context;
133719bbbc63SKim Phillips 	struct talitos_edesc *edesc;
133819bbbc63SKim Phillips 
133919bbbc63SKim Phillips 	edesc = container_of(desc, struct talitos_edesc, desc);
13404de9d0b5SLee Nipper 
13414de9d0b5SLee Nipper 	common_nonsnoop_unmap(dev, edesc, areq);
13424de9d0b5SLee Nipper 
13434de9d0b5SLee Nipper 	kfree(edesc);
13444de9d0b5SLee Nipper 
13454de9d0b5SLee Nipper 	areq->base.complete(&areq->base, err);
13464de9d0b5SLee Nipper }
13474de9d0b5SLee Nipper 
13484de9d0b5SLee Nipper static int common_nonsnoop(struct talitos_edesc *edesc,
13494de9d0b5SLee Nipper 			   struct ablkcipher_request *areq,
13504de9d0b5SLee Nipper 			   void (*callback) (struct device *dev,
13514de9d0b5SLee Nipper 					     struct talitos_desc *desc,
13524de9d0b5SLee Nipper 					     void *context, int error))
13534de9d0b5SLee Nipper {
13544de9d0b5SLee Nipper 	struct crypto_ablkcipher *cipher = crypto_ablkcipher_reqtfm(areq);
13554de9d0b5SLee Nipper 	struct talitos_ctx *ctx = crypto_ablkcipher_ctx(cipher);
13564de9d0b5SLee Nipper 	struct device *dev = ctx->dev;
13574de9d0b5SLee Nipper 	struct talitos_desc *desc = &edesc->desc;
13584de9d0b5SLee Nipper 	unsigned int cryptlen = areq->nbytes;
13594de9d0b5SLee Nipper 	unsigned int ivsize;
13604de9d0b5SLee Nipper 	int sg_count, ret;
13614de9d0b5SLee Nipper 
13624de9d0b5SLee Nipper 	/* first DWORD empty */
13634de9d0b5SLee Nipper 	desc->ptr[0].len = 0;
136481eb024cSKim Phillips 	to_talitos_ptr(&desc->ptr[0], 0);
13654de9d0b5SLee Nipper 	desc->ptr[0].j_extent = 0;
13664de9d0b5SLee Nipper 
13674de9d0b5SLee Nipper 	/* cipher iv */
13684de9d0b5SLee Nipper 	ivsize = crypto_ablkcipher_ivsize(cipher);
1369febec542SKim Phillips 	map_single_talitos_ptr(dev, &desc->ptr[1], ivsize, areq->info, 0,
13704de9d0b5SLee Nipper 			       DMA_TO_DEVICE);
13714de9d0b5SLee Nipper 
13724de9d0b5SLee Nipper 	/* cipher key */
13734de9d0b5SLee Nipper 	map_single_talitos_ptr(dev, &desc->ptr[2], ctx->keylen,
13744de9d0b5SLee Nipper 			       (char *)&ctx->key, 0, DMA_TO_DEVICE);
13754de9d0b5SLee Nipper 
13764de9d0b5SLee Nipper 	/*
13774de9d0b5SLee Nipper 	 * cipher in
13784de9d0b5SLee Nipper 	 */
13794de9d0b5SLee Nipper 	desc->ptr[3].len = cpu_to_be16(cryptlen);
13804de9d0b5SLee Nipper 	desc->ptr[3].j_extent = 0;
13814de9d0b5SLee Nipper 
13824de9d0b5SLee Nipper 	sg_count = talitos_map_sg(dev, areq->src, edesc->src_nents ? : 1,
13834de9d0b5SLee Nipper 				  (areq->src == areq->dst) ? DMA_BIDIRECTIONAL
13844de9d0b5SLee Nipper 							   : DMA_TO_DEVICE,
13854de9d0b5SLee Nipper 				  edesc->src_is_chained);
13864de9d0b5SLee Nipper 
13874de9d0b5SLee Nipper 	if (sg_count == 1) {
138881eb024cSKim Phillips 		to_talitos_ptr(&desc->ptr[3], sg_dma_address(areq->src));
13894de9d0b5SLee Nipper 	} else {
13904de9d0b5SLee Nipper 		sg_count = sg_to_link_tbl(areq->src, sg_count, cryptlen,
13914de9d0b5SLee Nipper 					  &edesc->link_tbl[0]);
13924de9d0b5SLee Nipper 		if (sg_count > 1) {
139381eb024cSKim Phillips 			to_talitos_ptr(&desc->ptr[3], edesc->dma_link_tbl);
13944de9d0b5SLee Nipper 			desc->ptr[3].j_extent |= DESC_PTR_LNKTBL_JUMP;
1395e938e465SKim Phillips 			dma_sync_single_for_device(dev, edesc->dma_link_tbl,
1396e938e465SKim Phillips 						   edesc->dma_len,
1397e938e465SKim Phillips 						   DMA_BIDIRECTIONAL);
13984de9d0b5SLee Nipper 		} else {
13994de9d0b5SLee Nipper 			/* Only one segment now, so no link tbl needed */
140081eb024cSKim Phillips 			to_talitos_ptr(&desc->ptr[3],
140181eb024cSKim Phillips 				       sg_dma_address(areq->src));
14024de9d0b5SLee Nipper 		}
14034de9d0b5SLee Nipper 	}
14044de9d0b5SLee Nipper 
14054de9d0b5SLee Nipper 	/* cipher out */
14064de9d0b5SLee Nipper 	desc->ptr[4].len = cpu_to_be16(cryptlen);
14074de9d0b5SLee Nipper 	desc->ptr[4].j_extent = 0;
14084de9d0b5SLee Nipper 
14094de9d0b5SLee Nipper 	if (areq->src != areq->dst)
14104de9d0b5SLee Nipper 		sg_count = talitos_map_sg(dev, areq->dst,
14114de9d0b5SLee Nipper 					  edesc->dst_nents ? : 1,
14124de9d0b5SLee Nipper 					  DMA_FROM_DEVICE,
14134de9d0b5SLee Nipper 					  edesc->dst_is_chained);
14144de9d0b5SLee Nipper 
14154de9d0b5SLee Nipper 	if (sg_count == 1) {
141681eb024cSKim Phillips 		to_talitos_ptr(&desc->ptr[4], sg_dma_address(areq->dst));
14174de9d0b5SLee Nipper 	} else {
14184de9d0b5SLee Nipper 		struct talitos_ptr *link_tbl_ptr =
14194de9d0b5SLee Nipper 			&edesc->link_tbl[edesc->src_nents + 1];
14204de9d0b5SLee Nipper 
142181eb024cSKim Phillips 		to_talitos_ptr(&desc->ptr[4], edesc->dma_link_tbl +
142281eb024cSKim Phillips 					      (edesc->src_nents + 1) *
142381eb024cSKim Phillips 					      sizeof(struct talitos_ptr));
14244de9d0b5SLee Nipper 		desc->ptr[4].j_extent |= DESC_PTR_LNKTBL_JUMP;
14254de9d0b5SLee Nipper 		sg_count = sg_to_link_tbl(areq->dst, sg_count, cryptlen,
14264de9d0b5SLee Nipper 					  link_tbl_ptr);
14274de9d0b5SLee Nipper 		dma_sync_single_for_device(ctx->dev, edesc->dma_link_tbl,
14284de9d0b5SLee Nipper 					   edesc->dma_len, DMA_BIDIRECTIONAL);
14294de9d0b5SLee Nipper 	}
14304de9d0b5SLee Nipper 
14314de9d0b5SLee Nipper 	/* iv out */
14324de9d0b5SLee Nipper 	map_single_talitos_ptr(dev, &desc->ptr[5], ivsize, ctx->iv, 0,
14334de9d0b5SLee Nipper 			       DMA_FROM_DEVICE);
14344de9d0b5SLee Nipper 
14354de9d0b5SLee Nipper 	/* last DWORD empty */
14364de9d0b5SLee Nipper 	desc->ptr[6].len = 0;
143781eb024cSKim Phillips 	to_talitos_ptr(&desc->ptr[6], 0);
14384de9d0b5SLee Nipper 	desc->ptr[6].j_extent = 0;
14394de9d0b5SLee Nipper 
14405228f0f7SKim Phillips 	ret = talitos_submit(dev, ctx->ch, desc, callback, areq);
14414de9d0b5SLee Nipper 	if (ret != -EINPROGRESS) {
14424de9d0b5SLee Nipper 		common_nonsnoop_unmap(dev, edesc, areq);
14434de9d0b5SLee Nipper 		kfree(edesc);
14444de9d0b5SLee Nipper 	}
14454de9d0b5SLee Nipper 	return ret;
14464de9d0b5SLee Nipper }
14474de9d0b5SLee Nipper 
1448e938e465SKim Phillips static struct talitos_edesc *ablkcipher_edesc_alloc(struct ablkcipher_request *
1449e938e465SKim Phillips 						    areq)
14504de9d0b5SLee Nipper {
14514de9d0b5SLee Nipper 	struct crypto_ablkcipher *cipher = crypto_ablkcipher_reqtfm(areq);
14524de9d0b5SLee Nipper 	struct talitos_ctx *ctx = crypto_ablkcipher_ctx(cipher);
14534de9d0b5SLee Nipper 
1454497f2e6bSLee Nipper 	return talitos_edesc_alloc(ctx->dev, areq->src, areq->dst, 0,
1455497f2e6bSLee Nipper 				   areq->nbytes, 0, 0, areq->base.flags);
14564de9d0b5SLee Nipper }
14574de9d0b5SLee Nipper 
14584de9d0b5SLee Nipper static int ablkcipher_encrypt(struct ablkcipher_request *areq)
14594de9d0b5SLee Nipper {
14604de9d0b5SLee Nipper 	struct crypto_ablkcipher *cipher = crypto_ablkcipher_reqtfm(areq);
14614de9d0b5SLee Nipper 	struct talitos_ctx *ctx = crypto_ablkcipher_ctx(cipher);
14624de9d0b5SLee Nipper 	struct talitos_edesc *edesc;
14634de9d0b5SLee Nipper 
14644de9d0b5SLee Nipper 	/* allocate extended descriptor */
14654de9d0b5SLee Nipper 	edesc = ablkcipher_edesc_alloc(areq);
14664de9d0b5SLee Nipper 	if (IS_ERR(edesc))
14674de9d0b5SLee Nipper 		return PTR_ERR(edesc);
14684de9d0b5SLee Nipper 
14694de9d0b5SLee Nipper 	/* set encrypt */
14704de9d0b5SLee Nipper 	edesc->desc.hdr = ctx->desc_hdr_template | DESC_HDR_MODE0_ENCRYPT;
14714de9d0b5SLee Nipper 
1472febec542SKim Phillips 	return common_nonsnoop(edesc, areq, ablkcipher_done);
14734de9d0b5SLee Nipper }
14744de9d0b5SLee Nipper 
14754de9d0b5SLee Nipper static int ablkcipher_decrypt(struct ablkcipher_request *areq)
14764de9d0b5SLee Nipper {
14774de9d0b5SLee Nipper 	struct crypto_ablkcipher *cipher = crypto_ablkcipher_reqtfm(areq);
14784de9d0b5SLee Nipper 	struct talitos_ctx *ctx = crypto_ablkcipher_ctx(cipher);
14794de9d0b5SLee Nipper 	struct talitos_edesc *edesc;
14804de9d0b5SLee Nipper 
14814de9d0b5SLee Nipper 	/* allocate extended descriptor */
14824de9d0b5SLee Nipper 	edesc = ablkcipher_edesc_alloc(areq);
14834de9d0b5SLee Nipper 	if (IS_ERR(edesc))
14844de9d0b5SLee Nipper 		return PTR_ERR(edesc);
14854de9d0b5SLee Nipper 
14864de9d0b5SLee Nipper 	edesc->desc.hdr = ctx->desc_hdr_template | DESC_HDR_DIR_INBOUND;
14874de9d0b5SLee Nipper 
1488febec542SKim Phillips 	return common_nonsnoop(edesc, areq, ablkcipher_done);
14894de9d0b5SLee Nipper }
14904de9d0b5SLee Nipper 
1491497f2e6bSLee Nipper static void common_nonsnoop_hash_unmap(struct device *dev,
1492497f2e6bSLee Nipper 				       struct talitos_edesc *edesc,
1493497f2e6bSLee Nipper 				       struct ahash_request *areq)
1494497f2e6bSLee Nipper {
1495497f2e6bSLee Nipper 	struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1496497f2e6bSLee Nipper 
1497497f2e6bSLee Nipper 	unmap_single_talitos_ptr(dev, &edesc->desc.ptr[5], DMA_FROM_DEVICE);
1498497f2e6bSLee Nipper 
1499497f2e6bSLee Nipper 	/* When using hashctx-in, must unmap it. */
1500497f2e6bSLee Nipper 	if (edesc->desc.ptr[1].len)
1501497f2e6bSLee Nipper 		unmap_single_talitos_ptr(dev, &edesc->desc.ptr[1],
1502497f2e6bSLee Nipper 					 DMA_TO_DEVICE);
1503497f2e6bSLee Nipper 
1504497f2e6bSLee Nipper 	if (edesc->desc.ptr[2].len)
1505497f2e6bSLee Nipper 		unmap_single_talitos_ptr(dev, &edesc->desc.ptr[2],
1506497f2e6bSLee Nipper 					 DMA_TO_DEVICE);
1507497f2e6bSLee Nipper 
1508497f2e6bSLee Nipper 	talitos_sg_unmap(dev, edesc, req_ctx->psrc, NULL);
1509497f2e6bSLee Nipper 
1510497f2e6bSLee Nipper 	if (edesc->dma_len)
1511497f2e6bSLee Nipper 		dma_unmap_single(dev, edesc->dma_link_tbl, edesc->dma_len,
1512497f2e6bSLee Nipper 				 DMA_BIDIRECTIONAL);
1513497f2e6bSLee Nipper 
1514497f2e6bSLee Nipper }
1515497f2e6bSLee Nipper 
1516497f2e6bSLee Nipper static void ahash_done(struct device *dev,
1517497f2e6bSLee Nipper 		       struct talitos_desc *desc, void *context,
1518497f2e6bSLee Nipper 		       int err)
1519497f2e6bSLee Nipper {
1520497f2e6bSLee Nipper 	struct ahash_request *areq = context;
1521497f2e6bSLee Nipper 	struct talitos_edesc *edesc =
1522497f2e6bSLee Nipper 		 container_of(desc, struct talitos_edesc, desc);
1523497f2e6bSLee Nipper 	struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1524497f2e6bSLee Nipper 
1525497f2e6bSLee Nipper 	if (!req_ctx->last && req_ctx->to_hash_later) {
1526497f2e6bSLee Nipper 		/* Position any partial block for next update/final/finup */
1527497f2e6bSLee Nipper 		memcpy(req_ctx->buf, req_ctx->bufnext, req_ctx->to_hash_later);
15285e833bc4SLee Nipper 		req_ctx->nbuf = req_ctx->to_hash_later;
1529497f2e6bSLee Nipper 	}
1530497f2e6bSLee Nipper 	common_nonsnoop_hash_unmap(dev, edesc, areq);
1531497f2e6bSLee Nipper 
1532497f2e6bSLee Nipper 	kfree(edesc);
1533497f2e6bSLee Nipper 
1534497f2e6bSLee Nipper 	areq->base.complete(&areq->base, err);
1535497f2e6bSLee Nipper }
1536497f2e6bSLee Nipper 
1537497f2e6bSLee Nipper static int common_nonsnoop_hash(struct talitos_edesc *edesc,
1538497f2e6bSLee Nipper 				struct ahash_request *areq, unsigned int length,
1539497f2e6bSLee Nipper 				void (*callback) (struct device *dev,
1540497f2e6bSLee Nipper 						  struct talitos_desc *desc,
1541497f2e6bSLee Nipper 						  void *context, int error))
1542497f2e6bSLee Nipper {
1543497f2e6bSLee Nipper 	struct crypto_ahash *tfm = crypto_ahash_reqtfm(areq);
1544497f2e6bSLee Nipper 	struct talitos_ctx *ctx = crypto_ahash_ctx(tfm);
1545497f2e6bSLee Nipper 	struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1546497f2e6bSLee Nipper 	struct device *dev = ctx->dev;
1547497f2e6bSLee Nipper 	struct talitos_desc *desc = &edesc->desc;
1548497f2e6bSLee Nipper 	int sg_count, ret;
1549497f2e6bSLee Nipper 
1550497f2e6bSLee Nipper 	/* first DWORD empty */
1551497f2e6bSLee Nipper 	desc->ptr[0] = zero_entry;
1552497f2e6bSLee Nipper 
155360f208d7SKim Phillips 	/* hash context in */
155460f208d7SKim Phillips 	if (!req_ctx->first || req_ctx->swinit) {
1555497f2e6bSLee Nipper 		map_single_talitos_ptr(dev, &desc->ptr[1],
1556497f2e6bSLee Nipper 				       req_ctx->hw_context_size,
1557497f2e6bSLee Nipper 				       (char *)req_ctx->hw_context, 0,
1558497f2e6bSLee Nipper 				       DMA_TO_DEVICE);
155960f208d7SKim Phillips 		req_ctx->swinit = 0;
1560497f2e6bSLee Nipper 	} else {
1561497f2e6bSLee Nipper 		desc->ptr[1] = zero_entry;
1562497f2e6bSLee Nipper 		/* Indicate next op is not the first. */
1563497f2e6bSLee Nipper 		req_ctx->first = 0;
1564497f2e6bSLee Nipper 	}
1565497f2e6bSLee Nipper 
1566497f2e6bSLee Nipper 	/* HMAC key */
1567497f2e6bSLee Nipper 	if (ctx->keylen)
1568497f2e6bSLee Nipper 		map_single_talitos_ptr(dev, &desc->ptr[2], ctx->keylen,
1569497f2e6bSLee Nipper 				       (char *)&ctx->key, 0, DMA_TO_DEVICE);
1570497f2e6bSLee Nipper 	else
1571497f2e6bSLee Nipper 		desc->ptr[2] = zero_entry;
1572497f2e6bSLee Nipper 
1573497f2e6bSLee Nipper 	/*
1574497f2e6bSLee Nipper 	 * data in
1575497f2e6bSLee Nipper 	 */
1576497f2e6bSLee Nipper 	desc->ptr[3].len = cpu_to_be16(length);
1577497f2e6bSLee Nipper 	desc->ptr[3].j_extent = 0;
1578497f2e6bSLee Nipper 
1579497f2e6bSLee Nipper 	sg_count = talitos_map_sg(dev, req_ctx->psrc,
1580497f2e6bSLee Nipper 				  edesc->src_nents ? : 1,
1581497f2e6bSLee Nipper 				  DMA_TO_DEVICE,
1582497f2e6bSLee Nipper 				  edesc->src_is_chained);
1583497f2e6bSLee Nipper 
1584497f2e6bSLee Nipper 	if (sg_count == 1) {
1585497f2e6bSLee Nipper 		to_talitos_ptr(&desc->ptr[3], sg_dma_address(req_ctx->psrc));
1586497f2e6bSLee Nipper 	} else {
1587497f2e6bSLee Nipper 		sg_count = sg_to_link_tbl(req_ctx->psrc, sg_count, length,
1588497f2e6bSLee Nipper 					  &edesc->link_tbl[0]);
1589497f2e6bSLee Nipper 		if (sg_count > 1) {
1590497f2e6bSLee Nipper 			desc->ptr[3].j_extent |= DESC_PTR_LNKTBL_JUMP;
1591497f2e6bSLee Nipper 			to_talitos_ptr(&desc->ptr[3], edesc->dma_link_tbl);
1592497f2e6bSLee Nipper 			dma_sync_single_for_device(ctx->dev,
1593497f2e6bSLee Nipper 						   edesc->dma_link_tbl,
1594497f2e6bSLee Nipper 						   edesc->dma_len,
1595497f2e6bSLee Nipper 						   DMA_BIDIRECTIONAL);
1596497f2e6bSLee Nipper 		} else {
1597497f2e6bSLee Nipper 			/* Only one segment now, so no link tbl needed */
1598497f2e6bSLee Nipper 			to_talitos_ptr(&desc->ptr[3],
1599497f2e6bSLee Nipper 				       sg_dma_address(req_ctx->psrc));
1600497f2e6bSLee Nipper 		}
1601497f2e6bSLee Nipper 	}
1602497f2e6bSLee Nipper 
1603497f2e6bSLee Nipper 	/* fifth DWORD empty */
1604497f2e6bSLee Nipper 	desc->ptr[4] = zero_entry;
1605497f2e6bSLee Nipper 
1606497f2e6bSLee Nipper 	/* hash/HMAC out -or- hash context out */
1607497f2e6bSLee Nipper 	if (req_ctx->last)
1608497f2e6bSLee Nipper 		map_single_talitos_ptr(dev, &desc->ptr[5],
1609497f2e6bSLee Nipper 				       crypto_ahash_digestsize(tfm),
1610497f2e6bSLee Nipper 				       areq->result, 0, DMA_FROM_DEVICE);
1611497f2e6bSLee Nipper 	else
1612497f2e6bSLee Nipper 		map_single_talitos_ptr(dev, &desc->ptr[5],
1613497f2e6bSLee Nipper 				       req_ctx->hw_context_size,
1614497f2e6bSLee Nipper 				       req_ctx->hw_context, 0, DMA_FROM_DEVICE);
1615497f2e6bSLee Nipper 
1616497f2e6bSLee Nipper 	/* last DWORD empty */
1617497f2e6bSLee Nipper 	desc->ptr[6] = zero_entry;
1618497f2e6bSLee Nipper 
16195228f0f7SKim Phillips 	ret = talitos_submit(dev, ctx->ch, desc, callback, areq);
1620497f2e6bSLee Nipper 	if (ret != -EINPROGRESS) {
1621497f2e6bSLee Nipper 		common_nonsnoop_hash_unmap(dev, edesc, areq);
1622497f2e6bSLee Nipper 		kfree(edesc);
1623497f2e6bSLee Nipper 	}
1624497f2e6bSLee Nipper 	return ret;
1625497f2e6bSLee Nipper }
1626497f2e6bSLee Nipper 
1627497f2e6bSLee Nipper static struct talitos_edesc *ahash_edesc_alloc(struct ahash_request *areq,
1628497f2e6bSLee Nipper 					       unsigned int nbytes)
1629497f2e6bSLee Nipper {
1630497f2e6bSLee Nipper 	struct crypto_ahash *tfm = crypto_ahash_reqtfm(areq);
1631497f2e6bSLee Nipper 	struct talitos_ctx *ctx = crypto_ahash_ctx(tfm);
1632497f2e6bSLee Nipper 	struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1633497f2e6bSLee Nipper 
1634497f2e6bSLee Nipper 	return talitos_edesc_alloc(ctx->dev, req_ctx->psrc, NULL, 1,
1635497f2e6bSLee Nipper 				   nbytes, 0, 0, areq->base.flags);
1636497f2e6bSLee Nipper }
1637497f2e6bSLee Nipper 
1638497f2e6bSLee Nipper static int ahash_init(struct ahash_request *areq)
1639497f2e6bSLee Nipper {
1640497f2e6bSLee Nipper 	struct crypto_ahash *tfm = crypto_ahash_reqtfm(areq);
1641497f2e6bSLee Nipper 	struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1642497f2e6bSLee Nipper 
1643497f2e6bSLee Nipper 	/* Initialize the context */
16445e833bc4SLee Nipper 	req_ctx->nbuf = 0;
164560f208d7SKim Phillips 	req_ctx->first = 1; /* first indicates h/w must init its context */
164660f208d7SKim Phillips 	req_ctx->swinit = 0; /* assume h/w init of context */
1647497f2e6bSLee Nipper 	req_ctx->hw_context_size =
1648497f2e6bSLee Nipper 		(crypto_ahash_digestsize(tfm) <= SHA256_DIGEST_SIZE)
1649497f2e6bSLee Nipper 			? TALITOS_MDEU_CONTEXT_SIZE_MD5_SHA1_SHA256
1650497f2e6bSLee Nipper 			: TALITOS_MDEU_CONTEXT_SIZE_SHA384_SHA512;
1651497f2e6bSLee Nipper 
1652497f2e6bSLee Nipper 	return 0;
1653497f2e6bSLee Nipper }
1654497f2e6bSLee Nipper 
165560f208d7SKim Phillips /*
165660f208d7SKim Phillips  * on h/w without explicit sha224 support, we initialize h/w context
165760f208d7SKim Phillips  * manually with sha224 constants, and tell it to run sha256.
165860f208d7SKim Phillips  */
165960f208d7SKim Phillips static int ahash_init_sha224_swinit(struct ahash_request *areq)
166060f208d7SKim Phillips {
166160f208d7SKim Phillips 	struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
166260f208d7SKim Phillips 
166360f208d7SKim Phillips 	ahash_init(areq);
166460f208d7SKim Phillips 	req_ctx->swinit = 1;/* prevent h/w initting context with sha256 values*/
166560f208d7SKim Phillips 
1666a752447aSKim Phillips 	req_ctx->hw_context[0] = SHA224_H0;
1667a752447aSKim Phillips 	req_ctx->hw_context[1] = SHA224_H1;
1668a752447aSKim Phillips 	req_ctx->hw_context[2] = SHA224_H2;
1669a752447aSKim Phillips 	req_ctx->hw_context[3] = SHA224_H3;
1670a752447aSKim Phillips 	req_ctx->hw_context[4] = SHA224_H4;
1671a752447aSKim Phillips 	req_ctx->hw_context[5] = SHA224_H5;
1672a752447aSKim Phillips 	req_ctx->hw_context[6] = SHA224_H6;
1673a752447aSKim Phillips 	req_ctx->hw_context[7] = SHA224_H7;
167460f208d7SKim Phillips 
167560f208d7SKim Phillips 	/* init 64-bit count */
167660f208d7SKim Phillips 	req_ctx->hw_context[8] = 0;
167760f208d7SKim Phillips 	req_ctx->hw_context[9] = 0;
167860f208d7SKim Phillips 
167960f208d7SKim Phillips 	return 0;
168060f208d7SKim Phillips }
168160f208d7SKim Phillips 
1682497f2e6bSLee Nipper static int ahash_process_req(struct ahash_request *areq, unsigned int nbytes)
1683497f2e6bSLee Nipper {
1684497f2e6bSLee Nipper 	struct crypto_ahash *tfm = crypto_ahash_reqtfm(areq);
1685497f2e6bSLee Nipper 	struct talitos_ctx *ctx = crypto_ahash_ctx(tfm);
1686497f2e6bSLee Nipper 	struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1687497f2e6bSLee Nipper 	struct talitos_edesc *edesc;
1688497f2e6bSLee Nipper 	unsigned int blocksize =
1689497f2e6bSLee Nipper 			crypto_tfm_alg_blocksize(crypto_ahash_tfm(tfm));
1690497f2e6bSLee Nipper 	unsigned int nbytes_to_hash;
1691497f2e6bSLee Nipper 	unsigned int to_hash_later;
16925e833bc4SLee Nipper 	unsigned int nsg;
1693497f2e6bSLee Nipper 	int chained;
1694497f2e6bSLee Nipper 
16955e833bc4SLee Nipper 	if (!req_ctx->last && (nbytes + req_ctx->nbuf <= blocksize)) {
16965e833bc4SLee Nipper 		/* Buffer up to one whole block */
1697497f2e6bSLee Nipper 		sg_copy_to_buffer(areq->src,
1698497f2e6bSLee Nipper 				  sg_count(areq->src, nbytes, &chained),
16995e833bc4SLee Nipper 				  req_ctx->buf + req_ctx->nbuf, nbytes);
17005e833bc4SLee Nipper 		req_ctx->nbuf += nbytes;
1701497f2e6bSLee Nipper 		return 0;
1702497f2e6bSLee Nipper 	}
1703497f2e6bSLee Nipper 
17045e833bc4SLee Nipper 	/* At least (blocksize + 1) bytes are available to hash */
17055e833bc4SLee Nipper 	nbytes_to_hash = nbytes + req_ctx->nbuf;
17065e833bc4SLee Nipper 	to_hash_later = nbytes_to_hash & (blocksize - 1);
17075e833bc4SLee Nipper 
17085e833bc4SLee Nipper 	if (req_ctx->last)
17095e833bc4SLee Nipper 		to_hash_later = 0;
17105e833bc4SLee Nipper 	else if (to_hash_later)
17115e833bc4SLee Nipper 		/* There is a partial block. Hash the full block(s) now */
17125e833bc4SLee Nipper 		nbytes_to_hash -= to_hash_later;
17135e833bc4SLee Nipper 	else {
17145e833bc4SLee Nipper 		/* Keep one block buffered */
17155e833bc4SLee Nipper 		nbytes_to_hash -= blocksize;
17165e833bc4SLee Nipper 		to_hash_later = blocksize;
1717497f2e6bSLee Nipper 	}
17185e833bc4SLee Nipper 
17195e833bc4SLee Nipper 	/* Chain in any previously buffered data */
17205e833bc4SLee Nipper 	if (req_ctx->nbuf) {
17215e833bc4SLee Nipper 		nsg = (req_ctx->nbuf < nbytes_to_hash) ? 2 : 1;
17225e833bc4SLee Nipper 		sg_init_table(req_ctx->bufsl, nsg);
17235e833bc4SLee Nipper 		sg_set_buf(req_ctx->bufsl, req_ctx->buf, req_ctx->nbuf);
17245e833bc4SLee Nipper 		if (nsg > 1)
17255e833bc4SLee Nipper 			scatterwalk_sg_chain(req_ctx->bufsl, 2, areq->src);
17265e833bc4SLee Nipper 		req_ctx->psrc = req_ctx->bufsl;
17275e833bc4SLee Nipper 	} else
17285e833bc4SLee Nipper 		req_ctx->psrc = areq->src;
17295e833bc4SLee Nipper 
1730497f2e6bSLee Nipper 	if (to_hash_later) {
17315e833bc4SLee Nipper 		int nents = sg_count(areq->src, nbytes, &chained);
1732497f2e6bSLee Nipper 		sg_copy_end_to_buffer(areq->src, nents,
1733497f2e6bSLee Nipper 				      req_ctx->bufnext,
1734497f2e6bSLee Nipper 				      to_hash_later,
1735497f2e6bSLee Nipper 				      nbytes - to_hash_later);
1736497f2e6bSLee Nipper 	}
1737497f2e6bSLee Nipper 	req_ctx->to_hash_later = to_hash_later;
1738497f2e6bSLee Nipper 
17395e833bc4SLee Nipper 	/* Allocate extended descriptor */
1740497f2e6bSLee Nipper 	edesc = ahash_edesc_alloc(areq, nbytes_to_hash);
1741497f2e6bSLee Nipper 	if (IS_ERR(edesc))
1742497f2e6bSLee Nipper 		return PTR_ERR(edesc);
1743497f2e6bSLee Nipper 
1744497f2e6bSLee Nipper 	edesc->desc.hdr = ctx->desc_hdr_template;
1745497f2e6bSLee Nipper 
1746497f2e6bSLee Nipper 	/* On last one, request SEC to pad; otherwise continue */
1747497f2e6bSLee Nipper 	if (req_ctx->last)
1748497f2e6bSLee Nipper 		edesc->desc.hdr |= DESC_HDR_MODE0_MDEU_PAD;
1749497f2e6bSLee Nipper 	else
1750497f2e6bSLee Nipper 		edesc->desc.hdr |= DESC_HDR_MODE0_MDEU_CONT;
1751497f2e6bSLee Nipper 
175260f208d7SKim Phillips 	/* request SEC to INIT hash. */
175360f208d7SKim Phillips 	if (req_ctx->first && !req_ctx->swinit)
1754497f2e6bSLee Nipper 		edesc->desc.hdr |= DESC_HDR_MODE0_MDEU_INIT;
1755497f2e6bSLee Nipper 
1756497f2e6bSLee Nipper 	/* When the tfm context has a keylen, it's an HMAC.
1757497f2e6bSLee Nipper 	 * A first or last (ie. not middle) descriptor must request HMAC.
1758497f2e6bSLee Nipper 	 */
1759497f2e6bSLee Nipper 	if (ctx->keylen && (req_ctx->first || req_ctx->last))
1760497f2e6bSLee Nipper 		edesc->desc.hdr |= DESC_HDR_MODE0_MDEU_HMAC;
1761497f2e6bSLee Nipper 
1762497f2e6bSLee Nipper 	return common_nonsnoop_hash(edesc, areq, nbytes_to_hash,
1763497f2e6bSLee Nipper 				    ahash_done);
1764497f2e6bSLee Nipper }
1765497f2e6bSLee Nipper 
1766497f2e6bSLee Nipper static int ahash_update(struct ahash_request *areq)
1767497f2e6bSLee Nipper {
1768497f2e6bSLee Nipper 	struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1769497f2e6bSLee Nipper 
1770497f2e6bSLee Nipper 	req_ctx->last = 0;
1771497f2e6bSLee Nipper 
1772497f2e6bSLee Nipper 	return ahash_process_req(areq, areq->nbytes);
1773497f2e6bSLee Nipper }
1774497f2e6bSLee Nipper 
1775497f2e6bSLee Nipper static int ahash_final(struct ahash_request *areq)
1776497f2e6bSLee Nipper {
1777497f2e6bSLee Nipper 	struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1778497f2e6bSLee Nipper 
1779497f2e6bSLee Nipper 	req_ctx->last = 1;
1780497f2e6bSLee Nipper 
1781497f2e6bSLee Nipper 	return ahash_process_req(areq, 0);
1782497f2e6bSLee Nipper }
1783497f2e6bSLee Nipper 
1784497f2e6bSLee Nipper static int ahash_finup(struct ahash_request *areq)
1785497f2e6bSLee Nipper {
1786497f2e6bSLee Nipper 	struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1787497f2e6bSLee Nipper 
1788497f2e6bSLee Nipper 	req_ctx->last = 1;
1789497f2e6bSLee Nipper 
1790497f2e6bSLee Nipper 	return ahash_process_req(areq, areq->nbytes);
1791497f2e6bSLee Nipper }
1792497f2e6bSLee Nipper 
1793497f2e6bSLee Nipper static int ahash_digest(struct ahash_request *areq)
1794497f2e6bSLee Nipper {
1795497f2e6bSLee Nipper 	struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
179660f208d7SKim Phillips 	struct crypto_ahash *ahash = crypto_ahash_reqtfm(areq);
1797497f2e6bSLee Nipper 
179860f208d7SKim Phillips 	ahash->init(areq);
1799497f2e6bSLee Nipper 	req_ctx->last = 1;
1800497f2e6bSLee Nipper 
1801497f2e6bSLee Nipper 	return ahash_process_req(areq, areq->nbytes);
1802497f2e6bSLee Nipper }
1803497f2e6bSLee Nipper 
180479b3a418SLee Nipper struct keyhash_result {
180579b3a418SLee Nipper 	struct completion completion;
180679b3a418SLee Nipper 	int err;
180779b3a418SLee Nipper };
180879b3a418SLee Nipper 
180979b3a418SLee Nipper static void keyhash_complete(struct crypto_async_request *req, int err)
181079b3a418SLee Nipper {
181179b3a418SLee Nipper 	struct keyhash_result *res = req->data;
181279b3a418SLee Nipper 
181379b3a418SLee Nipper 	if (err == -EINPROGRESS)
181479b3a418SLee Nipper 		return;
181579b3a418SLee Nipper 
181679b3a418SLee Nipper 	res->err = err;
181779b3a418SLee Nipper 	complete(&res->completion);
181879b3a418SLee Nipper }
181979b3a418SLee Nipper 
182079b3a418SLee Nipper static int keyhash(struct crypto_ahash *tfm, const u8 *key, unsigned int keylen,
182179b3a418SLee Nipper 		   u8 *hash)
182279b3a418SLee Nipper {
182379b3a418SLee Nipper 	struct talitos_ctx *ctx = crypto_tfm_ctx(crypto_ahash_tfm(tfm));
182479b3a418SLee Nipper 
182579b3a418SLee Nipper 	struct scatterlist sg[1];
182679b3a418SLee Nipper 	struct ahash_request *req;
182779b3a418SLee Nipper 	struct keyhash_result hresult;
182879b3a418SLee Nipper 	int ret;
182979b3a418SLee Nipper 
183079b3a418SLee Nipper 	init_completion(&hresult.completion);
183179b3a418SLee Nipper 
183279b3a418SLee Nipper 	req = ahash_request_alloc(tfm, GFP_KERNEL);
183379b3a418SLee Nipper 	if (!req)
183479b3a418SLee Nipper 		return -ENOMEM;
183579b3a418SLee Nipper 
183679b3a418SLee Nipper 	/* Keep tfm keylen == 0 during hash of the long key */
183779b3a418SLee Nipper 	ctx->keylen = 0;
183879b3a418SLee Nipper 	ahash_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
183979b3a418SLee Nipper 				   keyhash_complete, &hresult);
184079b3a418SLee Nipper 
184179b3a418SLee Nipper 	sg_init_one(&sg[0], key, keylen);
184279b3a418SLee Nipper 
184379b3a418SLee Nipper 	ahash_request_set_crypt(req, sg, hash, keylen);
184479b3a418SLee Nipper 	ret = crypto_ahash_digest(req);
184579b3a418SLee Nipper 	switch (ret) {
184679b3a418SLee Nipper 	case 0:
184779b3a418SLee Nipper 		break;
184879b3a418SLee Nipper 	case -EINPROGRESS:
184979b3a418SLee Nipper 	case -EBUSY:
185079b3a418SLee Nipper 		ret = wait_for_completion_interruptible(
185179b3a418SLee Nipper 			&hresult.completion);
185279b3a418SLee Nipper 		if (!ret)
185379b3a418SLee Nipper 			ret = hresult.err;
185479b3a418SLee Nipper 		break;
185579b3a418SLee Nipper 	default:
185679b3a418SLee Nipper 		break;
185779b3a418SLee Nipper 	}
185879b3a418SLee Nipper 	ahash_request_free(req);
185979b3a418SLee Nipper 
186079b3a418SLee Nipper 	return ret;
186179b3a418SLee Nipper }
186279b3a418SLee Nipper 
186379b3a418SLee Nipper static int ahash_setkey(struct crypto_ahash *tfm, const u8 *key,
186479b3a418SLee Nipper 			unsigned int keylen)
186579b3a418SLee Nipper {
186679b3a418SLee Nipper 	struct talitos_ctx *ctx = crypto_tfm_ctx(crypto_ahash_tfm(tfm));
186779b3a418SLee Nipper 	unsigned int blocksize =
186879b3a418SLee Nipper 			crypto_tfm_alg_blocksize(crypto_ahash_tfm(tfm));
186979b3a418SLee Nipper 	unsigned int digestsize = crypto_ahash_digestsize(tfm);
187079b3a418SLee Nipper 	unsigned int keysize = keylen;
187179b3a418SLee Nipper 	u8 hash[SHA512_DIGEST_SIZE];
187279b3a418SLee Nipper 	int ret;
187379b3a418SLee Nipper 
187479b3a418SLee Nipper 	if (keylen <= blocksize)
187579b3a418SLee Nipper 		memcpy(ctx->key, key, keysize);
187679b3a418SLee Nipper 	else {
187779b3a418SLee Nipper 		/* Must get the hash of the long key */
187879b3a418SLee Nipper 		ret = keyhash(tfm, key, keylen, hash);
187979b3a418SLee Nipper 
188079b3a418SLee Nipper 		if (ret) {
188179b3a418SLee Nipper 			crypto_ahash_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
188279b3a418SLee Nipper 			return -EINVAL;
188379b3a418SLee Nipper 		}
188479b3a418SLee Nipper 
188579b3a418SLee Nipper 		keysize = digestsize;
188679b3a418SLee Nipper 		memcpy(ctx->key, hash, digestsize);
188779b3a418SLee Nipper 	}
188879b3a418SLee Nipper 
188979b3a418SLee Nipper 	ctx->keylen = keysize;
189079b3a418SLee Nipper 
189179b3a418SLee Nipper 	return 0;
189279b3a418SLee Nipper }
189379b3a418SLee Nipper 
189479b3a418SLee Nipper 
18959c4a7965SKim Phillips struct talitos_alg_template {
1896d5e4aaefSLee Nipper 	u32 type;
1897d5e4aaefSLee Nipper 	union {
1898d5e4aaefSLee Nipper 		struct crypto_alg crypto;
1899acbf7c62SLee Nipper 		struct ahash_alg hash;
1900d5e4aaefSLee Nipper 	} alg;
19019c4a7965SKim Phillips 	__be32 desc_hdr_template;
19029c4a7965SKim Phillips };
19039c4a7965SKim Phillips 
19049c4a7965SKim Phillips static struct talitos_alg_template driver_algs[] = {
190556af8cd4SLee Nipper 	/* AEAD algorithms.  These use a single-pass ipsec_esp descriptor */
1906d5e4aaefSLee Nipper 	{	.type = CRYPTO_ALG_TYPE_AEAD,
1907d5e4aaefSLee Nipper 		.alg.crypto = {
190856af8cd4SLee Nipper 			.cra_name = "authenc(hmac(sha1),cbc(aes))",
190956af8cd4SLee Nipper 			.cra_driver_name = "authenc-hmac-sha1-cbc-aes-talitos",
191056af8cd4SLee Nipper 			.cra_blocksize = AES_BLOCK_SIZE,
191156af8cd4SLee Nipper 			.cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
191256af8cd4SLee Nipper 			.cra_aead = {
19133952f17eSLee Nipper 				.ivsize = AES_BLOCK_SIZE,
19143952f17eSLee Nipper 				.maxauthsize = SHA1_DIGEST_SIZE,
191556af8cd4SLee Nipper 			}
19169c4a7965SKim Phillips 		},
19179c4a7965SKim Phillips 		.desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
19189c4a7965SKim Phillips 			             DESC_HDR_SEL0_AESU |
19199c4a7965SKim Phillips 		                     DESC_HDR_MODE0_AESU_CBC |
19209c4a7965SKim Phillips 		                     DESC_HDR_SEL1_MDEUA |
19219c4a7965SKim Phillips 		                     DESC_HDR_MODE1_MDEU_INIT |
19229c4a7965SKim Phillips 		                     DESC_HDR_MODE1_MDEU_PAD |
19239c4a7965SKim Phillips 		                     DESC_HDR_MODE1_MDEU_SHA1_HMAC,
192470bcaca7SLee Nipper 	},
1925d5e4aaefSLee Nipper 	{	.type = CRYPTO_ALG_TYPE_AEAD,
1926d5e4aaefSLee Nipper 		.alg.crypto = {
192756af8cd4SLee Nipper 			.cra_name = "authenc(hmac(sha1),cbc(des3_ede))",
192856af8cd4SLee Nipper 			.cra_driver_name = "authenc-hmac-sha1-cbc-3des-talitos",
192956af8cd4SLee Nipper 			.cra_blocksize = DES3_EDE_BLOCK_SIZE,
193056af8cd4SLee Nipper 			.cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
193156af8cd4SLee Nipper 			.cra_aead = {
19323952f17eSLee Nipper 				.ivsize = DES3_EDE_BLOCK_SIZE,
19333952f17eSLee Nipper 				.maxauthsize = SHA1_DIGEST_SIZE,
193456af8cd4SLee Nipper 			}
193570bcaca7SLee Nipper 		},
193670bcaca7SLee Nipper 		.desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
193770bcaca7SLee Nipper 			             DESC_HDR_SEL0_DEU |
193870bcaca7SLee Nipper 		                     DESC_HDR_MODE0_DEU_CBC |
193970bcaca7SLee Nipper 		                     DESC_HDR_MODE0_DEU_3DES |
194070bcaca7SLee Nipper 		                     DESC_HDR_SEL1_MDEUA |
194170bcaca7SLee Nipper 		                     DESC_HDR_MODE1_MDEU_INIT |
194270bcaca7SLee Nipper 		                     DESC_HDR_MODE1_MDEU_PAD |
194370bcaca7SLee Nipper 		                     DESC_HDR_MODE1_MDEU_SHA1_HMAC,
19443952f17eSLee Nipper 	},
1945d5e4aaefSLee Nipper 	{       .type = CRYPTO_ALG_TYPE_AEAD,
1946d5e4aaefSLee Nipper 		.alg.crypto = {
1947357fb605SHoria Geanta 			.cra_name = "authenc(hmac(sha224),cbc(aes))",
1948357fb605SHoria Geanta 			.cra_driver_name = "authenc-hmac-sha224-cbc-aes-talitos",
1949357fb605SHoria Geanta 			.cra_blocksize = AES_BLOCK_SIZE,
1950357fb605SHoria Geanta 			.cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
1951357fb605SHoria Geanta 			.cra_aead = {
1952357fb605SHoria Geanta 				.ivsize = AES_BLOCK_SIZE,
1953357fb605SHoria Geanta 				.maxauthsize = SHA224_DIGEST_SIZE,
1954357fb605SHoria Geanta 			}
1955357fb605SHoria Geanta 		},
1956357fb605SHoria Geanta 		.desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
1957357fb605SHoria Geanta 				     DESC_HDR_SEL0_AESU |
1958357fb605SHoria Geanta 				     DESC_HDR_MODE0_AESU_CBC |
1959357fb605SHoria Geanta 				     DESC_HDR_SEL1_MDEUA |
1960357fb605SHoria Geanta 				     DESC_HDR_MODE1_MDEU_INIT |
1961357fb605SHoria Geanta 				     DESC_HDR_MODE1_MDEU_PAD |
1962357fb605SHoria Geanta 				     DESC_HDR_MODE1_MDEU_SHA224_HMAC,
1963357fb605SHoria Geanta 	},
1964357fb605SHoria Geanta 	{	.type = CRYPTO_ALG_TYPE_AEAD,
1965357fb605SHoria Geanta 		.alg.crypto = {
1966357fb605SHoria Geanta 			.cra_name = "authenc(hmac(sha224),cbc(des3_ede))",
1967357fb605SHoria Geanta 			.cra_driver_name = "authenc-hmac-sha224-cbc-3des-talitos",
1968357fb605SHoria Geanta 			.cra_blocksize = DES3_EDE_BLOCK_SIZE,
1969357fb605SHoria Geanta 			.cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
1970357fb605SHoria Geanta 			.cra_aead = {
1971357fb605SHoria Geanta 				.ivsize = DES3_EDE_BLOCK_SIZE,
1972357fb605SHoria Geanta 				.maxauthsize = SHA224_DIGEST_SIZE,
1973357fb605SHoria Geanta 			}
1974357fb605SHoria Geanta 		},
1975357fb605SHoria Geanta 		.desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
1976357fb605SHoria Geanta 			             DESC_HDR_SEL0_DEU |
1977357fb605SHoria Geanta 		                     DESC_HDR_MODE0_DEU_CBC |
1978357fb605SHoria Geanta 		                     DESC_HDR_MODE0_DEU_3DES |
1979357fb605SHoria Geanta 		                     DESC_HDR_SEL1_MDEUA |
1980357fb605SHoria Geanta 		                     DESC_HDR_MODE1_MDEU_INIT |
1981357fb605SHoria Geanta 		                     DESC_HDR_MODE1_MDEU_PAD |
1982357fb605SHoria Geanta 		                     DESC_HDR_MODE1_MDEU_SHA224_HMAC,
1983357fb605SHoria Geanta 	},
1984357fb605SHoria Geanta 	{	.type = CRYPTO_ALG_TYPE_AEAD,
1985357fb605SHoria Geanta 		.alg.crypto = {
198656af8cd4SLee Nipper 			.cra_name = "authenc(hmac(sha256),cbc(aes))",
198756af8cd4SLee Nipper 			.cra_driver_name = "authenc-hmac-sha256-cbc-aes-talitos",
198856af8cd4SLee Nipper 			.cra_blocksize = AES_BLOCK_SIZE,
198956af8cd4SLee Nipper 			.cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
199056af8cd4SLee Nipper 			.cra_aead = {
19913952f17eSLee Nipper 				.ivsize = AES_BLOCK_SIZE,
19923952f17eSLee Nipper 				.maxauthsize = SHA256_DIGEST_SIZE,
199356af8cd4SLee Nipper 			}
19943952f17eSLee Nipper 		},
19953952f17eSLee Nipper 		.desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
19963952f17eSLee Nipper 			             DESC_HDR_SEL0_AESU |
19973952f17eSLee Nipper 		                     DESC_HDR_MODE0_AESU_CBC |
19983952f17eSLee Nipper 		                     DESC_HDR_SEL1_MDEUA |
19993952f17eSLee Nipper 		                     DESC_HDR_MODE1_MDEU_INIT |
20003952f17eSLee Nipper 		                     DESC_HDR_MODE1_MDEU_PAD |
20013952f17eSLee Nipper 		                     DESC_HDR_MODE1_MDEU_SHA256_HMAC,
20023952f17eSLee Nipper 	},
2003d5e4aaefSLee Nipper 	{	.type = CRYPTO_ALG_TYPE_AEAD,
2004d5e4aaefSLee Nipper 		.alg.crypto = {
200556af8cd4SLee Nipper 			.cra_name = "authenc(hmac(sha256),cbc(des3_ede))",
200656af8cd4SLee Nipper 			.cra_driver_name = "authenc-hmac-sha256-cbc-3des-talitos",
200756af8cd4SLee Nipper 			.cra_blocksize = DES3_EDE_BLOCK_SIZE,
200856af8cd4SLee Nipper 			.cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
200956af8cd4SLee Nipper 			.cra_aead = {
20103952f17eSLee Nipper 				.ivsize = DES3_EDE_BLOCK_SIZE,
20113952f17eSLee Nipper 				.maxauthsize = SHA256_DIGEST_SIZE,
201256af8cd4SLee Nipper 			}
20133952f17eSLee Nipper 		},
20143952f17eSLee Nipper 		.desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
20153952f17eSLee Nipper 			             DESC_HDR_SEL0_DEU |
20163952f17eSLee Nipper 		                     DESC_HDR_MODE0_DEU_CBC |
20173952f17eSLee Nipper 		                     DESC_HDR_MODE0_DEU_3DES |
20183952f17eSLee Nipper 		                     DESC_HDR_SEL1_MDEUA |
20193952f17eSLee Nipper 		                     DESC_HDR_MODE1_MDEU_INIT |
20203952f17eSLee Nipper 		                     DESC_HDR_MODE1_MDEU_PAD |
20213952f17eSLee Nipper 		                     DESC_HDR_MODE1_MDEU_SHA256_HMAC,
20223952f17eSLee Nipper 	},
2023d5e4aaefSLee Nipper 	{	.type = CRYPTO_ALG_TYPE_AEAD,
2024d5e4aaefSLee Nipper 		.alg.crypto = {
2025357fb605SHoria Geanta 			.cra_name = "authenc(hmac(sha384),cbc(aes))",
2026357fb605SHoria Geanta 			.cra_driver_name = "authenc-hmac-sha384-cbc-aes-talitos",
2027357fb605SHoria Geanta 			.cra_blocksize = AES_BLOCK_SIZE,
2028357fb605SHoria Geanta 			.cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
2029357fb605SHoria Geanta 			.cra_aead = {
2030357fb605SHoria Geanta 				.ivsize = AES_BLOCK_SIZE,
2031357fb605SHoria Geanta 				.maxauthsize = SHA384_DIGEST_SIZE,
2032357fb605SHoria Geanta 			}
2033357fb605SHoria Geanta 		},
2034357fb605SHoria Geanta 		.desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
2035357fb605SHoria Geanta 			             DESC_HDR_SEL0_AESU |
2036357fb605SHoria Geanta 		                     DESC_HDR_MODE0_AESU_CBC |
2037357fb605SHoria Geanta 		                     DESC_HDR_SEL1_MDEUB |
2038357fb605SHoria Geanta 		                     DESC_HDR_MODE1_MDEU_INIT |
2039357fb605SHoria Geanta 		                     DESC_HDR_MODE1_MDEU_PAD |
2040357fb605SHoria Geanta 		                     DESC_HDR_MODE1_MDEUB_SHA384_HMAC,
2041357fb605SHoria Geanta 	},
2042357fb605SHoria Geanta 	{	.type = CRYPTO_ALG_TYPE_AEAD,
2043357fb605SHoria Geanta 		.alg.crypto = {
2044357fb605SHoria Geanta 			.cra_name = "authenc(hmac(sha384),cbc(des3_ede))",
2045357fb605SHoria Geanta 			.cra_driver_name = "authenc-hmac-sha384-cbc-3des-talitos",
2046357fb605SHoria Geanta 			.cra_blocksize = DES3_EDE_BLOCK_SIZE,
2047357fb605SHoria Geanta 			.cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
2048357fb605SHoria Geanta 			.cra_aead = {
2049357fb605SHoria Geanta 				.ivsize = DES3_EDE_BLOCK_SIZE,
2050357fb605SHoria Geanta 				.maxauthsize = SHA384_DIGEST_SIZE,
2051357fb605SHoria Geanta 			}
2052357fb605SHoria Geanta 		},
2053357fb605SHoria Geanta 		.desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
2054357fb605SHoria Geanta 			             DESC_HDR_SEL0_DEU |
2055357fb605SHoria Geanta 		                     DESC_HDR_MODE0_DEU_CBC |
2056357fb605SHoria Geanta 		                     DESC_HDR_MODE0_DEU_3DES |
2057357fb605SHoria Geanta 		                     DESC_HDR_SEL1_MDEUB |
2058357fb605SHoria Geanta 		                     DESC_HDR_MODE1_MDEU_INIT |
2059357fb605SHoria Geanta 		                     DESC_HDR_MODE1_MDEU_PAD |
2060357fb605SHoria Geanta 		                     DESC_HDR_MODE1_MDEUB_SHA384_HMAC,
2061357fb605SHoria Geanta 	},
2062357fb605SHoria Geanta 	{	.type = CRYPTO_ALG_TYPE_AEAD,
2063357fb605SHoria Geanta 		.alg.crypto = {
2064357fb605SHoria Geanta 			.cra_name = "authenc(hmac(sha512),cbc(aes))",
2065357fb605SHoria Geanta 			.cra_driver_name = "authenc-hmac-sha512-cbc-aes-talitos",
2066357fb605SHoria Geanta 			.cra_blocksize = AES_BLOCK_SIZE,
2067357fb605SHoria Geanta 			.cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
2068357fb605SHoria Geanta 			.cra_aead = {
2069357fb605SHoria Geanta 				.ivsize = AES_BLOCK_SIZE,
2070357fb605SHoria Geanta 				.maxauthsize = SHA512_DIGEST_SIZE,
2071357fb605SHoria Geanta 			}
2072357fb605SHoria Geanta 		},
2073357fb605SHoria Geanta 		.desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
2074357fb605SHoria Geanta 			             DESC_HDR_SEL0_AESU |
2075357fb605SHoria Geanta 		                     DESC_HDR_MODE0_AESU_CBC |
2076357fb605SHoria Geanta 		                     DESC_HDR_SEL1_MDEUB |
2077357fb605SHoria Geanta 		                     DESC_HDR_MODE1_MDEU_INIT |
2078357fb605SHoria Geanta 		                     DESC_HDR_MODE1_MDEU_PAD |
2079357fb605SHoria Geanta 		                     DESC_HDR_MODE1_MDEUB_SHA512_HMAC,
2080357fb605SHoria Geanta 	},
2081357fb605SHoria Geanta 	{	.type = CRYPTO_ALG_TYPE_AEAD,
2082357fb605SHoria Geanta 		.alg.crypto = {
2083357fb605SHoria Geanta 			.cra_name = "authenc(hmac(sha512),cbc(des3_ede))",
2084357fb605SHoria Geanta 			.cra_driver_name = "authenc-hmac-sha512-cbc-3des-talitos",
2085357fb605SHoria Geanta 			.cra_blocksize = DES3_EDE_BLOCK_SIZE,
2086357fb605SHoria Geanta 			.cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
2087357fb605SHoria Geanta 			.cra_aead = {
2088357fb605SHoria Geanta 				.ivsize = DES3_EDE_BLOCK_SIZE,
2089357fb605SHoria Geanta 				.maxauthsize = SHA512_DIGEST_SIZE,
2090357fb605SHoria Geanta 			}
2091357fb605SHoria Geanta 		},
2092357fb605SHoria Geanta 		.desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
2093357fb605SHoria Geanta 			             DESC_HDR_SEL0_DEU |
2094357fb605SHoria Geanta 		                     DESC_HDR_MODE0_DEU_CBC |
2095357fb605SHoria Geanta 		                     DESC_HDR_MODE0_DEU_3DES |
2096357fb605SHoria Geanta 		                     DESC_HDR_SEL1_MDEUB |
2097357fb605SHoria Geanta 		                     DESC_HDR_MODE1_MDEU_INIT |
2098357fb605SHoria Geanta 		                     DESC_HDR_MODE1_MDEU_PAD |
2099357fb605SHoria Geanta 		                     DESC_HDR_MODE1_MDEUB_SHA512_HMAC,
2100357fb605SHoria Geanta 	},
2101357fb605SHoria Geanta 	{	.type = CRYPTO_ALG_TYPE_AEAD,
2102357fb605SHoria Geanta 		.alg.crypto = {
210356af8cd4SLee Nipper 			.cra_name = "authenc(hmac(md5),cbc(aes))",
210456af8cd4SLee Nipper 			.cra_driver_name = "authenc-hmac-md5-cbc-aes-talitos",
210556af8cd4SLee Nipper 			.cra_blocksize = AES_BLOCK_SIZE,
210656af8cd4SLee Nipper 			.cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
210756af8cd4SLee Nipper 			.cra_aead = {
21083952f17eSLee Nipper 				.ivsize = AES_BLOCK_SIZE,
21093952f17eSLee Nipper 				.maxauthsize = MD5_DIGEST_SIZE,
211056af8cd4SLee Nipper 			}
21113952f17eSLee Nipper 		},
21123952f17eSLee Nipper 		.desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
21133952f17eSLee Nipper 			             DESC_HDR_SEL0_AESU |
21143952f17eSLee Nipper 		                     DESC_HDR_MODE0_AESU_CBC |
21153952f17eSLee Nipper 		                     DESC_HDR_SEL1_MDEUA |
21163952f17eSLee Nipper 		                     DESC_HDR_MODE1_MDEU_INIT |
21173952f17eSLee Nipper 		                     DESC_HDR_MODE1_MDEU_PAD |
21183952f17eSLee Nipper 		                     DESC_HDR_MODE1_MDEU_MD5_HMAC,
21193952f17eSLee Nipper 	},
2120d5e4aaefSLee Nipper 	{	.type = CRYPTO_ALG_TYPE_AEAD,
2121d5e4aaefSLee Nipper 		.alg.crypto = {
212256af8cd4SLee Nipper 			.cra_name = "authenc(hmac(md5),cbc(des3_ede))",
212356af8cd4SLee Nipper 			.cra_driver_name = "authenc-hmac-md5-cbc-3des-talitos",
212456af8cd4SLee Nipper 			.cra_blocksize = DES3_EDE_BLOCK_SIZE,
212556af8cd4SLee Nipper 			.cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
212656af8cd4SLee Nipper 			.cra_aead = {
21273952f17eSLee Nipper 				.ivsize = DES3_EDE_BLOCK_SIZE,
21283952f17eSLee Nipper 				.maxauthsize = MD5_DIGEST_SIZE,
212956af8cd4SLee Nipper 			}
21303952f17eSLee Nipper 		},
21313952f17eSLee Nipper 		.desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
21323952f17eSLee Nipper 			             DESC_HDR_SEL0_DEU |
21333952f17eSLee Nipper 		                     DESC_HDR_MODE0_DEU_CBC |
21343952f17eSLee Nipper 		                     DESC_HDR_MODE0_DEU_3DES |
21353952f17eSLee Nipper 		                     DESC_HDR_SEL1_MDEUA |
21363952f17eSLee Nipper 		                     DESC_HDR_MODE1_MDEU_INIT |
21373952f17eSLee Nipper 		                     DESC_HDR_MODE1_MDEU_PAD |
21383952f17eSLee Nipper 		                     DESC_HDR_MODE1_MDEU_MD5_HMAC,
21394de9d0b5SLee Nipper 	},
21404de9d0b5SLee Nipper 	/* ABLKCIPHER algorithms. */
2141d5e4aaefSLee Nipper 	{	.type = CRYPTO_ALG_TYPE_ABLKCIPHER,
2142d5e4aaefSLee Nipper 		.alg.crypto = {
21434de9d0b5SLee Nipper 			.cra_name = "cbc(aes)",
21444de9d0b5SLee Nipper 			.cra_driver_name = "cbc-aes-talitos",
21454de9d0b5SLee Nipper 			.cra_blocksize = AES_BLOCK_SIZE,
21464de9d0b5SLee Nipper 			.cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
21474de9d0b5SLee Nipper                                      CRYPTO_ALG_ASYNC,
21484de9d0b5SLee Nipper 			.cra_ablkcipher = {
21494de9d0b5SLee Nipper 				.min_keysize = AES_MIN_KEY_SIZE,
21504de9d0b5SLee Nipper 				.max_keysize = AES_MAX_KEY_SIZE,
21514de9d0b5SLee Nipper 				.ivsize = AES_BLOCK_SIZE,
21524de9d0b5SLee Nipper 			}
21534de9d0b5SLee Nipper 		},
21544de9d0b5SLee Nipper 		.desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
21554de9d0b5SLee Nipper 				     DESC_HDR_SEL0_AESU |
21564de9d0b5SLee Nipper 				     DESC_HDR_MODE0_AESU_CBC,
21574de9d0b5SLee Nipper 	},
2158d5e4aaefSLee Nipper 	{	.type = CRYPTO_ALG_TYPE_ABLKCIPHER,
2159d5e4aaefSLee Nipper 		.alg.crypto = {
21604de9d0b5SLee Nipper 			.cra_name = "cbc(des3_ede)",
21614de9d0b5SLee Nipper 			.cra_driver_name = "cbc-3des-talitos",
21624de9d0b5SLee Nipper 			.cra_blocksize = DES3_EDE_BLOCK_SIZE,
21634de9d0b5SLee Nipper 			.cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
21644de9d0b5SLee Nipper                                      CRYPTO_ALG_ASYNC,
21654de9d0b5SLee Nipper 			.cra_ablkcipher = {
21664de9d0b5SLee Nipper 				.min_keysize = DES3_EDE_KEY_SIZE,
21674de9d0b5SLee Nipper 				.max_keysize = DES3_EDE_KEY_SIZE,
21684de9d0b5SLee Nipper 				.ivsize = DES3_EDE_BLOCK_SIZE,
21694de9d0b5SLee Nipper 			}
21704de9d0b5SLee Nipper 		},
21714de9d0b5SLee Nipper 		.desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
21724de9d0b5SLee Nipper 			             DESC_HDR_SEL0_DEU |
21734de9d0b5SLee Nipper 		                     DESC_HDR_MODE0_DEU_CBC |
21744de9d0b5SLee Nipper 		                     DESC_HDR_MODE0_DEU_3DES,
2175497f2e6bSLee Nipper 	},
2176497f2e6bSLee Nipper 	/* AHASH algorithms. */
2177497f2e6bSLee Nipper 	{	.type = CRYPTO_ALG_TYPE_AHASH,
2178497f2e6bSLee Nipper 		.alg.hash = {
2179497f2e6bSLee Nipper 			.halg.digestsize = MD5_DIGEST_SIZE,
2180497f2e6bSLee Nipper 			.halg.base = {
2181497f2e6bSLee Nipper 				.cra_name = "md5",
2182497f2e6bSLee Nipper 				.cra_driver_name = "md5-talitos",
2183497f2e6bSLee Nipper 				.cra_blocksize = MD5_BLOCK_SIZE,
2184497f2e6bSLee Nipper 				.cra_flags = CRYPTO_ALG_TYPE_AHASH |
2185497f2e6bSLee Nipper 					     CRYPTO_ALG_ASYNC,
21869c4a7965SKim Phillips 			}
2187497f2e6bSLee Nipper 		},
2188497f2e6bSLee Nipper 		.desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2189497f2e6bSLee Nipper 				     DESC_HDR_SEL0_MDEUA |
2190497f2e6bSLee Nipper 				     DESC_HDR_MODE0_MDEU_MD5,
2191497f2e6bSLee Nipper 	},
2192497f2e6bSLee Nipper 	{	.type = CRYPTO_ALG_TYPE_AHASH,
2193497f2e6bSLee Nipper 		.alg.hash = {
2194497f2e6bSLee Nipper 			.halg.digestsize = SHA1_DIGEST_SIZE,
2195497f2e6bSLee Nipper 			.halg.base = {
2196497f2e6bSLee Nipper 				.cra_name = "sha1",
2197497f2e6bSLee Nipper 				.cra_driver_name = "sha1-talitos",
2198497f2e6bSLee Nipper 				.cra_blocksize = SHA1_BLOCK_SIZE,
2199497f2e6bSLee Nipper 				.cra_flags = CRYPTO_ALG_TYPE_AHASH |
2200497f2e6bSLee Nipper 					     CRYPTO_ALG_ASYNC,
2201497f2e6bSLee Nipper 			}
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_SHA1,
2206497f2e6bSLee Nipper 	},
2207497f2e6bSLee Nipper 	{	.type = CRYPTO_ALG_TYPE_AHASH,
2208497f2e6bSLee Nipper 		.alg.hash = {
220960f208d7SKim Phillips 			.halg.digestsize = SHA224_DIGEST_SIZE,
221060f208d7SKim Phillips 			.halg.base = {
221160f208d7SKim Phillips 				.cra_name = "sha224",
221260f208d7SKim Phillips 				.cra_driver_name = "sha224-talitos",
221360f208d7SKim Phillips 				.cra_blocksize = SHA224_BLOCK_SIZE,
221460f208d7SKim Phillips 				.cra_flags = CRYPTO_ALG_TYPE_AHASH |
221560f208d7SKim Phillips 					     CRYPTO_ALG_ASYNC,
221660f208d7SKim Phillips 			}
221760f208d7SKim Phillips 		},
221860f208d7SKim Phillips 		.desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
221960f208d7SKim Phillips 				     DESC_HDR_SEL0_MDEUA |
222060f208d7SKim Phillips 				     DESC_HDR_MODE0_MDEU_SHA224,
222160f208d7SKim Phillips 	},
222260f208d7SKim Phillips 	{	.type = CRYPTO_ALG_TYPE_AHASH,
222360f208d7SKim Phillips 		.alg.hash = {
2224497f2e6bSLee Nipper 			.halg.digestsize = SHA256_DIGEST_SIZE,
2225497f2e6bSLee Nipper 			.halg.base = {
2226497f2e6bSLee Nipper 				.cra_name = "sha256",
2227497f2e6bSLee Nipper 				.cra_driver_name = "sha256-talitos",
2228497f2e6bSLee Nipper 				.cra_blocksize = SHA256_BLOCK_SIZE,
2229497f2e6bSLee Nipper 				.cra_flags = CRYPTO_ALG_TYPE_AHASH |
2230497f2e6bSLee Nipper 					     CRYPTO_ALG_ASYNC,
2231497f2e6bSLee Nipper 			}
2232497f2e6bSLee Nipper 		},
2233497f2e6bSLee Nipper 		.desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2234497f2e6bSLee Nipper 				     DESC_HDR_SEL0_MDEUA |
2235497f2e6bSLee Nipper 				     DESC_HDR_MODE0_MDEU_SHA256,
2236497f2e6bSLee Nipper 	},
2237497f2e6bSLee Nipper 	{	.type = CRYPTO_ALG_TYPE_AHASH,
2238497f2e6bSLee Nipper 		.alg.hash = {
2239497f2e6bSLee Nipper 			.halg.digestsize = SHA384_DIGEST_SIZE,
2240497f2e6bSLee Nipper 			.halg.base = {
2241497f2e6bSLee Nipper 				.cra_name = "sha384",
2242497f2e6bSLee Nipper 				.cra_driver_name = "sha384-talitos",
2243497f2e6bSLee Nipper 				.cra_blocksize = SHA384_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_MDEUB |
2250497f2e6bSLee Nipper 				     DESC_HDR_MODE0_MDEUB_SHA384,
2251497f2e6bSLee Nipper 	},
2252497f2e6bSLee Nipper 	{	.type = CRYPTO_ALG_TYPE_AHASH,
2253497f2e6bSLee Nipper 		.alg.hash = {
2254497f2e6bSLee Nipper 			.halg.digestsize = SHA512_DIGEST_SIZE,
2255497f2e6bSLee Nipper 			.halg.base = {
2256497f2e6bSLee Nipper 				.cra_name = "sha512",
2257497f2e6bSLee Nipper 				.cra_driver_name = "sha512-talitos",
2258497f2e6bSLee Nipper 				.cra_blocksize = SHA512_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_SHA512,
2266497f2e6bSLee Nipper 	},
226779b3a418SLee Nipper 	{	.type = CRYPTO_ALG_TYPE_AHASH,
226879b3a418SLee Nipper 		.alg.hash = {
226979b3a418SLee Nipper 			.halg.digestsize = MD5_DIGEST_SIZE,
227079b3a418SLee Nipper 			.halg.base = {
227179b3a418SLee Nipper 				.cra_name = "hmac(md5)",
227279b3a418SLee Nipper 				.cra_driver_name = "hmac-md5-talitos",
227379b3a418SLee Nipper 				.cra_blocksize = MD5_BLOCK_SIZE,
227479b3a418SLee Nipper 				.cra_flags = CRYPTO_ALG_TYPE_AHASH |
227579b3a418SLee Nipper 					     CRYPTO_ALG_ASYNC,
227679b3a418SLee Nipper 			}
227779b3a418SLee Nipper 		},
227879b3a418SLee Nipper 		.desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
227979b3a418SLee Nipper 				     DESC_HDR_SEL0_MDEUA |
228079b3a418SLee Nipper 				     DESC_HDR_MODE0_MDEU_MD5,
228179b3a418SLee Nipper 	},
228279b3a418SLee Nipper 	{	.type = CRYPTO_ALG_TYPE_AHASH,
228379b3a418SLee Nipper 		.alg.hash = {
228479b3a418SLee Nipper 			.halg.digestsize = SHA1_DIGEST_SIZE,
228579b3a418SLee Nipper 			.halg.base = {
228679b3a418SLee Nipper 				.cra_name = "hmac(sha1)",
228779b3a418SLee Nipper 				.cra_driver_name = "hmac-sha1-talitos",
228879b3a418SLee Nipper 				.cra_blocksize = SHA1_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_SHA1,
229679b3a418SLee Nipper 	},
229779b3a418SLee Nipper 	{	.type = CRYPTO_ALG_TYPE_AHASH,
229879b3a418SLee Nipper 		.alg.hash = {
229979b3a418SLee Nipper 			.halg.digestsize = SHA224_DIGEST_SIZE,
230079b3a418SLee Nipper 			.halg.base = {
230179b3a418SLee Nipper 				.cra_name = "hmac(sha224)",
230279b3a418SLee Nipper 				.cra_driver_name = "hmac-sha224-talitos",
230379b3a418SLee Nipper 				.cra_blocksize = SHA224_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_SHA224,
231179b3a418SLee Nipper 	},
231279b3a418SLee Nipper 	{	.type = CRYPTO_ALG_TYPE_AHASH,
231379b3a418SLee Nipper 		.alg.hash = {
231479b3a418SLee Nipper 			.halg.digestsize = SHA256_DIGEST_SIZE,
231579b3a418SLee Nipper 			.halg.base = {
231679b3a418SLee Nipper 				.cra_name = "hmac(sha256)",
231779b3a418SLee Nipper 				.cra_driver_name = "hmac-sha256-talitos",
231879b3a418SLee Nipper 				.cra_blocksize = SHA256_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_SHA256,
232679b3a418SLee Nipper 	},
232779b3a418SLee Nipper 	{	.type = CRYPTO_ALG_TYPE_AHASH,
232879b3a418SLee Nipper 		.alg.hash = {
232979b3a418SLee Nipper 			.halg.digestsize = SHA384_DIGEST_SIZE,
233079b3a418SLee Nipper 			.halg.base = {
233179b3a418SLee Nipper 				.cra_name = "hmac(sha384)",
233279b3a418SLee Nipper 				.cra_driver_name = "hmac-sha384-talitos",
233379b3a418SLee Nipper 				.cra_blocksize = SHA384_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_MDEUB |
234079b3a418SLee Nipper 				     DESC_HDR_MODE0_MDEUB_SHA384,
234179b3a418SLee Nipper 	},
234279b3a418SLee Nipper 	{	.type = CRYPTO_ALG_TYPE_AHASH,
234379b3a418SLee Nipper 		.alg.hash = {
234479b3a418SLee Nipper 			.halg.digestsize = SHA512_DIGEST_SIZE,
234579b3a418SLee Nipper 			.halg.base = {
234679b3a418SLee Nipper 				.cra_name = "hmac(sha512)",
234779b3a418SLee Nipper 				.cra_driver_name = "hmac-sha512-talitos",
234879b3a418SLee Nipper 				.cra_blocksize = SHA512_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_SHA512,
235679b3a418SLee Nipper 	}
23579c4a7965SKim Phillips };
23589c4a7965SKim Phillips 
23599c4a7965SKim Phillips struct talitos_crypto_alg {
23609c4a7965SKim Phillips 	struct list_head entry;
23619c4a7965SKim Phillips 	struct device *dev;
2362acbf7c62SLee Nipper 	struct talitos_alg_template algt;
23639c4a7965SKim Phillips };
23649c4a7965SKim Phillips 
23659c4a7965SKim Phillips static int talitos_cra_init(struct crypto_tfm *tfm)
23669c4a7965SKim Phillips {
23679c4a7965SKim Phillips 	struct crypto_alg *alg = tfm->__crt_alg;
236819bbbc63SKim Phillips 	struct talitos_crypto_alg *talitos_alg;
23699c4a7965SKim Phillips 	struct talitos_ctx *ctx = crypto_tfm_ctx(tfm);
23705228f0f7SKim Phillips 	struct talitos_private *priv;
23719c4a7965SKim Phillips 
2372497f2e6bSLee Nipper 	if ((alg->cra_flags & CRYPTO_ALG_TYPE_MASK) == CRYPTO_ALG_TYPE_AHASH)
2373497f2e6bSLee Nipper 		talitos_alg = container_of(__crypto_ahash_alg(alg),
2374497f2e6bSLee Nipper 					   struct talitos_crypto_alg,
2375497f2e6bSLee Nipper 					   algt.alg.hash);
2376497f2e6bSLee Nipper 	else
2377acbf7c62SLee Nipper 		talitos_alg = container_of(alg, struct talitos_crypto_alg,
2378acbf7c62SLee Nipper 					   algt.alg.crypto);
237919bbbc63SKim Phillips 
23809c4a7965SKim Phillips 	/* update context with ptr to dev */
23819c4a7965SKim Phillips 	ctx->dev = talitos_alg->dev;
238219bbbc63SKim Phillips 
23835228f0f7SKim Phillips 	/* assign SEC channel to tfm in round-robin fashion */
23845228f0f7SKim Phillips 	priv = dev_get_drvdata(ctx->dev);
23855228f0f7SKim Phillips 	ctx->ch = atomic_inc_return(&priv->last_chan) &
23865228f0f7SKim Phillips 		  (priv->num_channels - 1);
23875228f0f7SKim Phillips 
23889c4a7965SKim Phillips 	/* copy descriptor header template value */
2389acbf7c62SLee Nipper 	ctx->desc_hdr_template = talitos_alg->algt.desc_hdr_template;
23909c4a7965SKim Phillips 
2391602dba5aSKim Phillips 	/* select done notification */
2392602dba5aSKim Phillips 	ctx->desc_hdr_template |= DESC_HDR_DONE_NOTIFY;
2393602dba5aSKim Phillips 
2394497f2e6bSLee Nipper 	return 0;
2395497f2e6bSLee Nipper }
2396497f2e6bSLee Nipper 
2397497f2e6bSLee Nipper static int talitos_cra_init_aead(struct crypto_tfm *tfm)
2398497f2e6bSLee Nipper {
2399497f2e6bSLee Nipper 	struct talitos_ctx *ctx = crypto_tfm_ctx(tfm);
2400497f2e6bSLee Nipper 
2401497f2e6bSLee Nipper 	talitos_cra_init(tfm);
24029c4a7965SKim Phillips 
24039c4a7965SKim Phillips 	/* random first IV */
240470bcaca7SLee Nipper 	get_random_bytes(ctx->iv, TALITOS_MAX_IV_LENGTH);
24059c4a7965SKim Phillips 
24069c4a7965SKim Phillips 	return 0;
24079c4a7965SKim Phillips }
24089c4a7965SKim Phillips 
2409497f2e6bSLee Nipper static int talitos_cra_init_ahash(struct crypto_tfm *tfm)
2410497f2e6bSLee Nipper {
2411497f2e6bSLee Nipper 	struct talitos_ctx *ctx = crypto_tfm_ctx(tfm);
2412497f2e6bSLee Nipper 
2413497f2e6bSLee Nipper 	talitos_cra_init(tfm);
2414497f2e6bSLee Nipper 
2415497f2e6bSLee Nipper 	ctx->keylen = 0;
2416497f2e6bSLee Nipper 	crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
2417497f2e6bSLee Nipper 				 sizeof(struct talitos_ahash_req_ctx));
2418497f2e6bSLee Nipper 
2419497f2e6bSLee Nipper 	return 0;
2420497f2e6bSLee Nipper }
2421497f2e6bSLee Nipper 
24229c4a7965SKim Phillips /*
24239c4a7965SKim Phillips  * given the alg's descriptor header template, determine whether descriptor
24249c4a7965SKim Phillips  * type and primary/secondary execution units required match the hw
24259c4a7965SKim Phillips  * capabilities description provided in the device tree node.
24269c4a7965SKim Phillips  */
24279c4a7965SKim Phillips static int hw_supports(struct device *dev, __be32 desc_hdr_template)
24289c4a7965SKim Phillips {
24299c4a7965SKim Phillips 	struct talitos_private *priv = dev_get_drvdata(dev);
24309c4a7965SKim Phillips 	int ret;
24319c4a7965SKim Phillips 
24329c4a7965SKim Phillips 	ret = (1 << DESC_TYPE(desc_hdr_template) & priv->desc_types) &&
24339c4a7965SKim Phillips 	      (1 << PRIMARY_EU(desc_hdr_template) & priv->exec_units);
24349c4a7965SKim Phillips 
24359c4a7965SKim Phillips 	if (SECONDARY_EU(desc_hdr_template))
24369c4a7965SKim Phillips 		ret = ret && (1 << SECONDARY_EU(desc_hdr_template)
24379c4a7965SKim Phillips 		              & priv->exec_units);
24389c4a7965SKim Phillips 
24399c4a7965SKim Phillips 	return ret;
24409c4a7965SKim Phillips }
24419c4a7965SKim Phillips 
24422dc11581SGrant Likely static int talitos_remove(struct platform_device *ofdev)
24439c4a7965SKim Phillips {
24449c4a7965SKim Phillips 	struct device *dev = &ofdev->dev;
24459c4a7965SKim Phillips 	struct talitos_private *priv = dev_get_drvdata(dev);
24469c4a7965SKim Phillips 	struct talitos_crypto_alg *t_alg, *n;
24479c4a7965SKim Phillips 	int i;
24489c4a7965SKim Phillips 
24499c4a7965SKim Phillips 	list_for_each_entry_safe(t_alg, n, &priv->alg_list, entry) {
2450acbf7c62SLee Nipper 		switch (t_alg->algt.type) {
2451acbf7c62SLee Nipper 		case CRYPTO_ALG_TYPE_ABLKCIPHER:
2452acbf7c62SLee Nipper 		case CRYPTO_ALG_TYPE_AEAD:
2453acbf7c62SLee Nipper 			crypto_unregister_alg(&t_alg->algt.alg.crypto);
2454acbf7c62SLee Nipper 			break;
2455acbf7c62SLee Nipper 		case CRYPTO_ALG_TYPE_AHASH:
2456acbf7c62SLee Nipper 			crypto_unregister_ahash(&t_alg->algt.alg.hash);
2457acbf7c62SLee Nipper 			break;
2458acbf7c62SLee Nipper 		}
24599c4a7965SKim Phillips 		list_del(&t_alg->entry);
24609c4a7965SKim Phillips 		kfree(t_alg);
24619c4a7965SKim Phillips 	}
24629c4a7965SKim Phillips 
24639c4a7965SKim Phillips 	if (hw_supports(dev, DESC_HDR_SEL0_RNG))
24649c4a7965SKim Phillips 		talitos_unregister_rng(dev);
24659c4a7965SKim Phillips 
24669c4a7965SKim Phillips 	for (i = 0; i < priv->num_channels; i++)
24674b992628SKim Phillips 		kfree(priv->chan[i].fifo);
24689c4a7965SKim Phillips 
24694b992628SKim Phillips 	kfree(priv->chan);
24709c4a7965SKim Phillips 
2471c3e337f8SKim Phillips 	for (i = 0; i < 2; i++)
24722cdba3cfSKim Phillips 		if (priv->irq[i]) {
2473c3e337f8SKim Phillips 			free_irq(priv->irq[i], dev);
2474c3e337f8SKim Phillips 			irq_dispose_mapping(priv->irq[i]);
24759c4a7965SKim Phillips 		}
24769c4a7965SKim Phillips 
2477c3e337f8SKim Phillips 	tasklet_kill(&priv->done_task[0]);
24782cdba3cfSKim Phillips 	if (priv->irq[1])
2479c3e337f8SKim Phillips 		tasklet_kill(&priv->done_task[1]);
24809c4a7965SKim Phillips 
24819c4a7965SKim Phillips 	iounmap(priv->reg);
24829c4a7965SKim Phillips 
24839c4a7965SKim Phillips 	dev_set_drvdata(dev, NULL);
24849c4a7965SKim Phillips 
24859c4a7965SKim Phillips 	kfree(priv);
24869c4a7965SKim Phillips 
24879c4a7965SKim Phillips 	return 0;
24889c4a7965SKim Phillips }
24899c4a7965SKim Phillips 
24909c4a7965SKim Phillips static struct talitos_crypto_alg *talitos_alg_alloc(struct device *dev,
24919c4a7965SKim Phillips 						    struct talitos_alg_template
24929c4a7965SKim Phillips 						           *template)
24939c4a7965SKim Phillips {
249460f208d7SKim Phillips 	struct talitos_private *priv = dev_get_drvdata(dev);
24959c4a7965SKim Phillips 	struct talitos_crypto_alg *t_alg;
24969c4a7965SKim Phillips 	struct crypto_alg *alg;
24979c4a7965SKim Phillips 
24989c4a7965SKim Phillips 	t_alg = kzalloc(sizeof(struct talitos_crypto_alg), GFP_KERNEL);
24999c4a7965SKim Phillips 	if (!t_alg)
25009c4a7965SKim Phillips 		return ERR_PTR(-ENOMEM);
25019c4a7965SKim Phillips 
2502acbf7c62SLee Nipper 	t_alg->algt = *template;
2503acbf7c62SLee Nipper 
2504acbf7c62SLee Nipper 	switch (t_alg->algt.type) {
2505acbf7c62SLee Nipper 	case CRYPTO_ALG_TYPE_ABLKCIPHER:
2506497f2e6bSLee Nipper 		alg = &t_alg->algt.alg.crypto;
2507497f2e6bSLee Nipper 		alg->cra_init = talitos_cra_init;
2508d4cd3283SKim Phillips 		alg->cra_type = &crypto_ablkcipher_type;
2509*b286e003SKim Phillips 		alg->cra_ablkcipher.setkey = ablkcipher_setkey;
2510*b286e003SKim Phillips 		alg->cra_ablkcipher.encrypt = ablkcipher_encrypt;
2511*b286e003SKim Phillips 		alg->cra_ablkcipher.decrypt = ablkcipher_decrypt;
2512*b286e003SKim Phillips 		alg->cra_ablkcipher.geniv = "eseqiv";
2513497f2e6bSLee Nipper 		break;
2514acbf7c62SLee Nipper 	case CRYPTO_ALG_TYPE_AEAD:
2515acbf7c62SLee Nipper 		alg = &t_alg->algt.alg.crypto;
2516497f2e6bSLee Nipper 		alg->cra_init = talitos_cra_init_aead;
2517d4cd3283SKim Phillips 		alg->cra_type = &crypto_aead_type;
2518*b286e003SKim Phillips 		alg->cra_aead.setkey = aead_setkey;
2519*b286e003SKim Phillips 		alg->cra_aead.setauthsize = aead_setauthsize;
2520*b286e003SKim Phillips 		alg->cra_aead.encrypt = aead_encrypt;
2521*b286e003SKim Phillips 		alg->cra_aead.decrypt = aead_decrypt;
2522*b286e003SKim Phillips 		alg->cra_aead.givencrypt = aead_givencrypt;
2523*b286e003SKim Phillips 		alg->cra_aead.geniv = "<built-in>";
2524acbf7c62SLee Nipper 		break;
2525acbf7c62SLee Nipper 	case CRYPTO_ALG_TYPE_AHASH:
2526acbf7c62SLee Nipper 		alg = &t_alg->algt.alg.hash.halg.base;
2527497f2e6bSLee Nipper 		alg->cra_init = talitos_cra_init_ahash;
2528d4cd3283SKim Phillips 		alg->cra_type = &crypto_ahash_type;
2529*b286e003SKim Phillips 		t_alg->algt.alg.hash.init = ahash_init;
2530*b286e003SKim Phillips 		t_alg->algt.alg.hash.update = ahash_update;
2531*b286e003SKim Phillips 		t_alg->algt.alg.hash.final = ahash_final;
2532*b286e003SKim Phillips 		t_alg->algt.alg.hash.finup = ahash_finup;
2533*b286e003SKim Phillips 		t_alg->algt.alg.hash.digest = ahash_digest;
2534*b286e003SKim Phillips 		t_alg->algt.alg.hash.setkey = ahash_setkey;
2535*b286e003SKim Phillips 
253679b3a418SLee Nipper 		if (!(priv->features & TALITOS_FTR_HMAC_OK) &&
25370b2730d8SKim Phillips 		    !strncmp(alg->cra_name, "hmac", 4)) {
25380b2730d8SKim Phillips 			kfree(t_alg);
253979b3a418SLee Nipper 			return ERR_PTR(-ENOTSUPP);
25400b2730d8SKim Phillips 		}
254160f208d7SKim Phillips 		if (!(priv->features & TALITOS_FTR_SHA224_HWINIT) &&
254279b3a418SLee Nipper 		    (!strcmp(alg->cra_name, "sha224") ||
254379b3a418SLee Nipper 		     !strcmp(alg->cra_name, "hmac(sha224)"))) {
254460f208d7SKim Phillips 			t_alg->algt.alg.hash.init = ahash_init_sha224_swinit;
254560f208d7SKim Phillips 			t_alg->algt.desc_hdr_template =
254660f208d7SKim Phillips 					DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
254760f208d7SKim Phillips 					DESC_HDR_SEL0_MDEUA |
254860f208d7SKim Phillips 					DESC_HDR_MODE0_MDEU_SHA256;
254960f208d7SKim Phillips 		}
2550497f2e6bSLee Nipper 		break;
25511d11911aSKim Phillips 	default:
25521d11911aSKim Phillips 		dev_err(dev, "unknown algorithm type %d\n", t_alg->algt.type);
25531d11911aSKim Phillips 		return ERR_PTR(-EINVAL);
2554acbf7c62SLee Nipper 	}
25559c4a7965SKim Phillips 
25569c4a7965SKim Phillips 	alg->cra_module = THIS_MODULE;
25579c4a7965SKim Phillips 	alg->cra_priority = TALITOS_CRA_PRIORITY;
25589c4a7965SKim Phillips 	alg->cra_alignmask = 0;
25599c4a7965SKim Phillips 	alg->cra_ctxsize = sizeof(struct talitos_ctx);
2560d912bb76SNikos Mavrogiannopoulos 	alg->cra_flags |= CRYPTO_ALG_KERN_DRIVER_ONLY;
25619c4a7965SKim Phillips 
25629c4a7965SKim Phillips 	t_alg->dev = dev;
25639c4a7965SKim Phillips 
25649c4a7965SKim Phillips 	return t_alg;
25659c4a7965SKim Phillips }
25669c4a7965SKim Phillips 
2567c3e337f8SKim Phillips static int talitos_probe_irq(struct platform_device *ofdev)
2568c3e337f8SKim Phillips {
2569c3e337f8SKim Phillips 	struct device *dev = &ofdev->dev;
2570c3e337f8SKim Phillips 	struct device_node *np = ofdev->dev.of_node;
2571c3e337f8SKim Phillips 	struct talitos_private *priv = dev_get_drvdata(dev);
2572c3e337f8SKim Phillips 	int err;
2573c3e337f8SKim Phillips 
2574c3e337f8SKim Phillips 	priv->irq[0] = irq_of_parse_and_map(np, 0);
25752cdba3cfSKim Phillips 	if (!priv->irq[0]) {
2576c3e337f8SKim Phillips 		dev_err(dev, "failed to map irq\n");
2577c3e337f8SKim Phillips 		return -EINVAL;
2578c3e337f8SKim Phillips 	}
2579c3e337f8SKim Phillips 
2580c3e337f8SKim Phillips 	priv->irq[1] = irq_of_parse_and_map(np, 1);
2581c3e337f8SKim Phillips 
2582c3e337f8SKim Phillips 	/* get the primary irq line */
25832cdba3cfSKim Phillips 	if (!priv->irq[1]) {
2584c3e337f8SKim Phillips 		err = request_irq(priv->irq[0], talitos_interrupt_4ch, 0,
2585c3e337f8SKim Phillips 				  dev_driver_string(dev), dev);
2586c3e337f8SKim Phillips 		goto primary_out;
2587c3e337f8SKim Phillips 	}
2588c3e337f8SKim Phillips 
2589c3e337f8SKim Phillips 	err = request_irq(priv->irq[0], talitos_interrupt_ch0_2, 0,
2590c3e337f8SKim Phillips 			  dev_driver_string(dev), dev);
2591c3e337f8SKim Phillips 	if (err)
2592c3e337f8SKim Phillips 		goto primary_out;
2593c3e337f8SKim Phillips 
2594c3e337f8SKim Phillips 	/* get the secondary irq line */
2595c3e337f8SKim Phillips 	err = request_irq(priv->irq[1], talitos_interrupt_ch1_3, 0,
2596c3e337f8SKim Phillips 			  dev_driver_string(dev), dev);
2597c3e337f8SKim Phillips 	if (err) {
2598c3e337f8SKim Phillips 		dev_err(dev, "failed to request secondary irq\n");
2599c3e337f8SKim Phillips 		irq_dispose_mapping(priv->irq[1]);
26002cdba3cfSKim Phillips 		priv->irq[1] = 0;
2601c3e337f8SKim Phillips 	}
2602c3e337f8SKim Phillips 
2603c3e337f8SKim Phillips 	return err;
2604c3e337f8SKim Phillips 
2605c3e337f8SKim Phillips primary_out:
2606c3e337f8SKim Phillips 	if (err) {
2607c3e337f8SKim Phillips 		dev_err(dev, "failed to request primary irq\n");
2608c3e337f8SKim Phillips 		irq_dispose_mapping(priv->irq[0]);
26092cdba3cfSKim Phillips 		priv->irq[0] = 0;
2610c3e337f8SKim Phillips 	}
2611c3e337f8SKim Phillips 
2612c3e337f8SKim Phillips 	return err;
2613c3e337f8SKim Phillips }
2614c3e337f8SKim Phillips 
26151c48a5c9SGrant Likely static int talitos_probe(struct platform_device *ofdev)
26169c4a7965SKim Phillips {
26179c4a7965SKim Phillips 	struct device *dev = &ofdev->dev;
261861c7a080SGrant Likely 	struct device_node *np = ofdev->dev.of_node;
26199c4a7965SKim Phillips 	struct talitos_private *priv;
26209c4a7965SKim Phillips 	const unsigned int *prop;
26219c4a7965SKim Phillips 	int i, err;
26229c4a7965SKim Phillips 
26239c4a7965SKim Phillips 	priv = kzalloc(sizeof(struct talitos_private), GFP_KERNEL);
26249c4a7965SKim Phillips 	if (!priv)
26259c4a7965SKim Phillips 		return -ENOMEM;
26269c4a7965SKim Phillips 
26279c4a7965SKim Phillips 	dev_set_drvdata(dev, priv);
26289c4a7965SKim Phillips 
26299c4a7965SKim Phillips 	priv->ofdev = ofdev;
26309c4a7965SKim Phillips 
2631511d63cbSHoria Geanta 	spin_lock_init(&priv->reg_lock);
2632511d63cbSHoria Geanta 
2633c3e337f8SKim Phillips 	err = talitos_probe_irq(ofdev);
2634c3e337f8SKim Phillips 	if (err)
2635c3e337f8SKim Phillips 		goto err_out;
2636c3e337f8SKim Phillips 
26372cdba3cfSKim Phillips 	if (!priv->irq[1]) {
2638c3e337f8SKim Phillips 		tasklet_init(&priv->done_task[0], talitos_done_4ch,
2639c3e337f8SKim Phillips 			     (unsigned long)dev);
2640c3e337f8SKim Phillips 	} else {
2641c3e337f8SKim Phillips 		tasklet_init(&priv->done_task[0], talitos_done_ch0_2,
2642c3e337f8SKim Phillips 			     (unsigned long)dev);
2643c3e337f8SKim Phillips 		tasklet_init(&priv->done_task[1], talitos_done_ch1_3,
2644c3e337f8SKim Phillips 			     (unsigned long)dev);
2645c3e337f8SKim Phillips 	}
26469c4a7965SKim Phillips 
2647fe5720e2SKim Phillips 	INIT_LIST_HEAD(&priv->alg_list);
2648fe5720e2SKim Phillips 
26499c4a7965SKim Phillips 	priv->reg = of_iomap(np, 0);
26509c4a7965SKim Phillips 	if (!priv->reg) {
26519c4a7965SKim Phillips 		dev_err(dev, "failed to of_iomap\n");
26529c4a7965SKim Phillips 		err = -ENOMEM;
26539c4a7965SKim Phillips 		goto err_out;
26549c4a7965SKim Phillips 	}
26559c4a7965SKim Phillips 
26569c4a7965SKim Phillips 	/* get SEC version capabilities from device tree */
26579c4a7965SKim Phillips 	prop = of_get_property(np, "fsl,num-channels", NULL);
26589c4a7965SKim Phillips 	if (prop)
26599c4a7965SKim Phillips 		priv->num_channels = *prop;
26609c4a7965SKim Phillips 
26619c4a7965SKim Phillips 	prop = of_get_property(np, "fsl,channel-fifo-len", NULL);
26629c4a7965SKim Phillips 	if (prop)
26639c4a7965SKim Phillips 		priv->chfifo_len = *prop;
26649c4a7965SKim Phillips 
26659c4a7965SKim Phillips 	prop = of_get_property(np, "fsl,exec-units-mask", NULL);
26669c4a7965SKim Phillips 	if (prop)
26679c4a7965SKim Phillips 		priv->exec_units = *prop;
26689c4a7965SKim Phillips 
26699c4a7965SKim Phillips 	prop = of_get_property(np, "fsl,descriptor-types-mask", NULL);
26709c4a7965SKim Phillips 	if (prop)
26719c4a7965SKim Phillips 		priv->desc_types = *prop;
26729c4a7965SKim Phillips 
26739c4a7965SKim Phillips 	if (!is_power_of_2(priv->num_channels) || !priv->chfifo_len ||
26749c4a7965SKim Phillips 	    !priv->exec_units || !priv->desc_types) {
26759c4a7965SKim Phillips 		dev_err(dev, "invalid property data in device tree node\n");
26769c4a7965SKim Phillips 		err = -EINVAL;
26779c4a7965SKim Phillips 		goto err_out;
26789c4a7965SKim Phillips 	}
26799c4a7965SKim Phillips 
2680f3c85bc1SLee Nipper 	if (of_device_is_compatible(np, "fsl,sec3.0"))
2681f3c85bc1SLee Nipper 		priv->features |= TALITOS_FTR_SRC_LINK_TBL_LEN_INCLUDES_EXTENT;
2682f3c85bc1SLee Nipper 
2683fe5720e2SKim Phillips 	if (of_device_is_compatible(np, "fsl,sec2.1"))
268460f208d7SKim Phillips 		priv->features |= TALITOS_FTR_HW_AUTH_CHECK |
268579b3a418SLee Nipper 				  TALITOS_FTR_SHA224_HWINIT |
268679b3a418SLee Nipper 				  TALITOS_FTR_HMAC_OK;
2687fe5720e2SKim Phillips 
26884b992628SKim Phillips 	priv->chan = kzalloc(sizeof(struct talitos_channel) *
26894b992628SKim Phillips 			     priv->num_channels, GFP_KERNEL);
26904b992628SKim Phillips 	if (!priv->chan) {
26914b992628SKim Phillips 		dev_err(dev, "failed to allocate channel management space\n");
26929c4a7965SKim Phillips 		err = -ENOMEM;
26939c4a7965SKim Phillips 		goto err_out;
26949c4a7965SKim Phillips 	}
26959c4a7965SKim Phillips 
2696c3e337f8SKim Phillips 	for (i = 0; i < priv->num_channels; i++) {
2697c3e337f8SKim Phillips 		priv->chan[i].reg = priv->reg + TALITOS_CH_STRIDE * (i + 1);
26982cdba3cfSKim Phillips 		if (!priv->irq[1] || !(i & 1))
2699c3e337f8SKim Phillips 			priv->chan[i].reg += TALITOS_CH_BASE_OFFSET;
2700c3e337f8SKim Phillips 	}
2701ad42d5fcSKim Phillips 
27029c4a7965SKim Phillips 	for (i = 0; i < priv->num_channels; i++) {
27034b992628SKim Phillips 		spin_lock_init(&priv->chan[i].head_lock);
27044b992628SKim Phillips 		spin_lock_init(&priv->chan[i].tail_lock);
27059c4a7965SKim Phillips 	}
27069c4a7965SKim Phillips 
27079c4a7965SKim Phillips 	priv->fifo_len = roundup_pow_of_two(priv->chfifo_len);
27089c4a7965SKim Phillips 
27099c4a7965SKim Phillips 	for (i = 0; i < priv->num_channels; i++) {
27104b992628SKim Phillips 		priv->chan[i].fifo = kzalloc(sizeof(struct talitos_request) *
27119c4a7965SKim Phillips 					     priv->fifo_len, GFP_KERNEL);
27124b992628SKim Phillips 		if (!priv->chan[i].fifo) {
27139c4a7965SKim Phillips 			dev_err(dev, "failed to allocate request fifo %d\n", i);
27149c4a7965SKim Phillips 			err = -ENOMEM;
27159c4a7965SKim Phillips 			goto err_out;
27169c4a7965SKim Phillips 		}
27179c4a7965SKim Phillips 	}
27189c4a7965SKim Phillips 
2719ec6644d6SKim Phillips 	for (i = 0; i < priv->num_channels; i++)
27204b992628SKim Phillips 		atomic_set(&priv->chan[i].submit_count,
27214b992628SKim Phillips 			   -(priv->chfifo_len - 1));
27229c4a7965SKim Phillips 
272381eb024cSKim Phillips 	dma_set_mask(dev, DMA_BIT_MASK(36));
272481eb024cSKim Phillips 
27259c4a7965SKim Phillips 	/* reset and initialize the h/w */
27269c4a7965SKim Phillips 	err = init_device(dev);
27279c4a7965SKim Phillips 	if (err) {
27289c4a7965SKim Phillips 		dev_err(dev, "failed to initialize device\n");
27299c4a7965SKim Phillips 		goto err_out;
27309c4a7965SKim Phillips 	}
27319c4a7965SKim Phillips 
27329c4a7965SKim Phillips 	/* register the RNG, if available */
27339c4a7965SKim Phillips 	if (hw_supports(dev, DESC_HDR_SEL0_RNG)) {
27349c4a7965SKim Phillips 		err = talitos_register_rng(dev);
27359c4a7965SKim Phillips 		if (err) {
27369c4a7965SKim Phillips 			dev_err(dev, "failed to register hwrng: %d\n", err);
27379c4a7965SKim Phillips 			goto err_out;
27389c4a7965SKim Phillips 		} else
27399c4a7965SKim Phillips 			dev_info(dev, "hwrng\n");
27409c4a7965SKim Phillips 	}
27419c4a7965SKim Phillips 
27429c4a7965SKim Phillips 	/* register crypto algorithms the device supports */
27439c4a7965SKim Phillips 	for (i = 0; i < ARRAY_SIZE(driver_algs); i++) {
27449c4a7965SKim Phillips 		if (hw_supports(dev, driver_algs[i].desc_hdr_template)) {
27459c4a7965SKim Phillips 			struct talitos_crypto_alg *t_alg;
2746acbf7c62SLee Nipper 			char *name = NULL;
27479c4a7965SKim Phillips 
27489c4a7965SKim Phillips 			t_alg = talitos_alg_alloc(dev, &driver_algs[i]);
27499c4a7965SKim Phillips 			if (IS_ERR(t_alg)) {
27509c4a7965SKim Phillips 				err = PTR_ERR(t_alg);
27510b2730d8SKim Phillips 				if (err == -ENOTSUPP)
275279b3a418SLee Nipper 					continue;
27539c4a7965SKim Phillips 				goto err_out;
27549c4a7965SKim Phillips 			}
27559c4a7965SKim Phillips 
2756acbf7c62SLee Nipper 			switch (t_alg->algt.type) {
2757acbf7c62SLee Nipper 			case CRYPTO_ALG_TYPE_ABLKCIPHER:
2758acbf7c62SLee Nipper 			case CRYPTO_ALG_TYPE_AEAD:
2759acbf7c62SLee Nipper 				err = crypto_register_alg(
2760acbf7c62SLee Nipper 						&t_alg->algt.alg.crypto);
2761acbf7c62SLee Nipper 				name = t_alg->algt.alg.crypto.cra_driver_name;
2762acbf7c62SLee Nipper 				break;
2763acbf7c62SLee Nipper 			case CRYPTO_ALG_TYPE_AHASH:
2764acbf7c62SLee Nipper 				err = crypto_register_ahash(
2765acbf7c62SLee Nipper 						&t_alg->algt.alg.hash);
2766acbf7c62SLee Nipper 				name =
2767acbf7c62SLee Nipper 				 t_alg->algt.alg.hash.halg.base.cra_driver_name;
2768acbf7c62SLee Nipper 				break;
2769acbf7c62SLee Nipper 			}
27709c4a7965SKim Phillips 			if (err) {
27719c4a7965SKim Phillips 				dev_err(dev, "%s alg registration failed\n",
2772acbf7c62SLee Nipper 					name);
27739c4a7965SKim Phillips 				kfree(t_alg);
27745b859b6eSKim Phillips 			} else
27759c4a7965SKim Phillips 				list_add_tail(&t_alg->entry, &priv->alg_list);
27769c4a7965SKim Phillips 		}
27779c4a7965SKim Phillips 	}
27785b859b6eSKim Phillips 	if (!list_empty(&priv->alg_list))
27795b859b6eSKim Phillips 		dev_info(dev, "%s algorithms registered in /proc/crypto\n",
27805b859b6eSKim Phillips 			 (char *)of_get_property(np, "compatible", NULL));
27819c4a7965SKim Phillips 
27829c4a7965SKim Phillips 	return 0;
27839c4a7965SKim Phillips 
27849c4a7965SKim Phillips err_out:
27859c4a7965SKim Phillips 	talitos_remove(ofdev);
27869c4a7965SKim Phillips 
27879c4a7965SKim Phillips 	return err;
27889c4a7965SKim Phillips }
27899c4a7965SKim Phillips 
27906c3f975aSMárton Németh static const struct of_device_id talitos_match[] = {
27919c4a7965SKim Phillips 	{
27929c4a7965SKim Phillips 		.compatible = "fsl,sec2.0",
27939c4a7965SKim Phillips 	},
27949c4a7965SKim Phillips 	{},
27959c4a7965SKim Phillips };
27969c4a7965SKim Phillips MODULE_DEVICE_TABLE(of, talitos_match);
27979c4a7965SKim Phillips 
27981c48a5c9SGrant Likely static struct platform_driver talitos_driver = {
27994018294bSGrant Likely 	.driver = {
28009c4a7965SKim Phillips 		.name = "talitos",
28014018294bSGrant Likely 		.owner = THIS_MODULE,
28024018294bSGrant Likely 		.of_match_table = talitos_match,
28034018294bSGrant Likely 	},
28049c4a7965SKim Phillips 	.probe = talitos_probe,
2805596f1034SAl Viro 	.remove = talitos_remove,
28069c4a7965SKim Phillips };
28079c4a7965SKim Phillips 
2808741e8c2dSAxel Lin module_platform_driver(talitos_driver);
28099c4a7965SKim Phillips 
28109c4a7965SKim Phillips MODULE_LICENSE("GPL");
28119c4a7965SKim Phillips MODULE_AUTHOR("Kim Phillips <kim.phillips@freescale.com>");
28129c4a7965SKim Phillips MODULE_DESCRIPTION("Freescale integrated security engine (SEC) driver");
2813