xref: /linux/drivers/crypto/talitos.c (revision 1d11911a8c572041880c8d86229f6ed971f6f7e2)
19c4a7965SKim Phillips /*
29c4a7965SKim Phillips  * talitos - Freescale Integrated Security Engine (SEC) device driver
39c4a7965SKim Phillips  *
460f208d7SKim Phillips  * Copyright (c) 2008-2010 Freescale Semiconductor, Inc.
59c4a7965SKim Phillips  *
69c4a7965SKim Phillips  * Scatterlist Crypto API glue code copied from files with the following:
79c4a7965SKim Phillips  * Copyright (c) 2006-2007 Herbert Xu <herbert@gondor.apana.org.au>
89c4a7965SKim Phillips  *
99c4a7965SKim Phillips  * Crypto algorithm registration code copied from hifn driver:
109c4a7965SKim Phillips  * 2007+ Copyright (c) Evgeniy Polyakov <johnpol@2ka.mipt.ru>
119c4a7965SKim Phillips  * All rights reserved.
129c4a7965SKim Phillips  *
139c4a7965SKim Phillips  * This program is free software; you can redistribute it and/or modify
149c4a7965SKim Phillips  * it under the terms of the GNU General Public License as published by
159c4a7965SKim Phillips  * the Free Software Foundation; either version 2 of the License, or
169c4a7965SKim Phillips  * (at your option) any later version.
179c4a7965SKim Phillips  *
189c4a7965SKim Phillips  * This program is distributed in the hope that it will be useful,
199c4a7965SKim Phillips  * but WITHOUT ANY WARRANTY; without even the implied warranty of
209c4a7965SKim Phillips  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
219c4a7965SKim Phillips  * GNU General Public License for more details.
229c4a7965SKim Phillips  *
239c4a7965SKim Phillips  * You should have received a copy of the GNU General Public License
249c4a7965SKim Phillips  * along with this program; if not, write to the Free Software
259c4a7965SKim Phillips  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
269c4a7965SKim Phillips  */
279c4a7965SKim Phillips 
289c4a7965SKim Phillips #include <linux/kernel.h>
299c4a7965SKim Phillips #include <linux/module.h>
309c4a7965SKim Phillips #include <linux/mod_devicetable.h>
319c4a7965SKim Phillips #include <linux/device.h>
329c4a7965SKim Phillips #include <linux/interrupt.h>
339c4a7965SKim Phillips #include <linux/crypto.h>
349c4a7965SKim Phillips #include <linux/hw_random.h>
359c4a7965SKim Phillips #include <linux/of_platform.h>
369c4a7965SKim Phillips #include <linux/dma-mapping.h>
379c4a7965SKim Phillips #include <linux/io.h>
389c4a7965SKim Phillips #include <linux/spinlock.h>
399c4a7965SKim Phillips #include <linux/rtnetlink.h>
405a0e3ad6STejun Heo #include <linux/slab.h>
419c4a7965SKim Phillips 
429c4a7965SKim Phillips #include <crypto/algapi.h>
439c4a7965SKim Phillips #include <crypto/aes.h>
443952f17eSLee Nipper #include <crypto/des.h>
459c4a7965SKim Phillips #include <crypto/sha.h>
46497f2e6bSLee Nipper #include <crypto/md5.h>
479c4a7965SKim Phillips #include <crypto/aead.h>
489c4a7965SKim Phillips #include <crypto/authenc.h>
494de9d0b5SLee Nipper #include <crypto/skcipher.h>
50acbf7c62SLee Nipper #include <crypto/hash.h>
51acbf7c62SLee Nipper #include <crypto/internal/hash.h>
524de9d0b5SLee Nipper #include <crypto/scatterwalk.h>
539c4a7965SKim Phillips 
549c4a7965SKim Phillips #include "talitos.h"
559c4a7965SKim Phillips 
569c4a7965SKim Phillips #define TALITOS_TIMEOUT 100000
579c4a7965SKim Phillips #define TALITOS_MAX_DATA_LEN 65535
589c4a7965SKim Phillips 
599c4a7965SKim Phillips #define DESC_TYPE(desc_hdr) ((be32_to_cpu(desc_hdr) >> 3) & 0x1f)
609c4a7965SKim Phillips #define PRIMARY_EU(desc_hdr) ((be32_to_cpu(desc_hdr) >> 28) & 0xf)
619c4a7965SKim Phillips #define SECONDARY_EU(desc_hdr) ((be32_to_cpu(desc_hdr) >> 16) & 0xf)
629c4a7965SKim Phillips 
639c4a7965SKim Phillips /* descriptor pointer entry */
649c4a7965SKim Phillips struct talitos_ptr {
659c4a7965SKim Phillips 	__be16 len;	/* length */
669c4a7965SKim Phillips 	u8 j_extent;	/* jump to sg link table and/or extent */
679c4a7965SKim Phillips 	u8 eptr;	/* extended address */
689c4a7965SKim Phillips 	__be32 ptr;	/* address */
699c4a7965SKim Phillips };
709c4a7965SKim Phillips 
71497f2e6bSLee Nipper static const struct talitos_ptr zero_entry = {
72497f2e6bSLee Nipper 	.len = 0,
73497f2e6bSLee Nipper 	.j_extent = 0,
74497f2e6bSLee Nipper 	.eptr = 0,
75497f2e6bSLee Nipper 	.ptr = 0
76497f2e6bSLee Nipper };
77497f2e6bSLee Nipper 
789c4a7965SKim Phillips /* descriptor */
799c4a7965SKim Phillips struct talitos_desc {
809c4a7965SKim Phillips 	__be32 hdr;			/* header high bits */
819c4a7965SKim Phillips 	__be32 hdr_lo;			/* header low bits */
829c4a7965SKim Phillips 	struct talitos_ptr ptr[7];	/* ptr/len pair array */
839c4a7965SKim Phillips };
849c4a7965SKim Phillips 
859c4a7965SKim Phillips /**
869c4a7965SKim Phillips  * talitos_request - descriptor submission request
879c4a7965SKim Phillips  * @desc: descriptor pointer (kernel virtual)
889c4a7965SKim Phillips  * @dma_desc: descriptor's physical bus address
899c4a7965SKim Phillips  * @callback: whom to call when descriptor processing is done
909c4a7965SKim Phillips  * @context: caller context (optional)
919c4a7965SKim Phillips  */
929c4a7965SKim Phillips struct talitos_request {
939c4a7965SKim Phillips 	struct talitos_desc *desc;
949c4a7965SKim Phillips 	dma_addr_t dma_desc;
959c4a7965SKim Phillips 	void (*callback) (struct device *dev, struct talitos_desc *desc,
969c4a7965SKim Phillips 	                  void *context, int error);
979c4a7965SKim Phillips 	void *context;
989c4a7965SKim Phillips };
999c4a7965SKim Phillips 
1004b992628SKim Phillips /* per-channel fifo management */
1014b992628SKim Phillips struct talitos_channel {
1024b992628SKim Phillips 	/* request fifo */
1034b992628SKim Phillips 	struct talitos_request *fifo;
1044b992628SKim Phillips 
1054b992628SKim Phillips 	/* number of requests pending in channel h/w fifo */
1064b992628SKim Phillips 	atomic_t submit_count ____cacheline_aligned;
1074b992628SKim Phillips 
1084b992628SKim Phillips 	/* request submission (head) lock */
1094b992628SKim Phillips 	spinlock_t head_lock ____cacheline_aligned;
1104b992628SKim Phillips 	/* index to next free descriptor request */
1114b992628SKim Phillips 	int head;
1124b992628SKim Phillips 
1134b992628SKim Phillips 	/* request release (tail) lock */
1144b992628SKim Phillips 	spinlock_t tail_lock ____cacheline_aligned;
1154b992628SKim Phillips 	/* index to next in-progress/done descriptor request */
1164b992628SKim Phillips 	int tail;
1174b992628SKim Phillips };
1184b992628SKim Phillips 
1199c4a7965SKim Phillips struct talitos_private {
1209c4a7965SKim Phillips 	struct device *dev;
1219c4a7965SKim Phillips 	struct of_device *ofdev;
1229c4a7965SKim Phillips 	void __iomem *reg;
1239c4a7965SKim Phillips 	int irq;
1249c4a7965SKim Phillips 
1259c4a7965SKim Phillips 	/* SEC version geometry (from device tree node) */
1269c4a7965SKim Phillips 	unsigned int num_channels;
1279c4a7965SKim Phillips 	unsigned int chfifo_len;
1289c4a7965SKim Phillips 	unsigned int exec_units;
1299c4a7965SKim Phillips 	unsigned int desc_types;
1309c4a7965SKim Phillips 
131f3c85bc1SLee Nipper 	/* SEC Compatibility info */
132f3c85bc1SLee Nipper 	unsigned long features;
133f3c85bc1SLee Nipper 
1349c4a7965SKim Phillips 	/*
1359c4a7965SKim Phillips 	 * length of the request fifo
1369c4a7965SKim Phillips 	 * fifo_len is chfifo_len rounded up to next power of 2
1379c4a7965SKim Phillips 	 * so we can use bitwise ops to wrap
1389c4a7965SKim Phillips 	 */
1399c4a7965SKim Phillips 	unsigned int fifo_len;
1409c4a7965SKim Phillips 
1414b992628SKim Phillips 	struct talitos_channel *chan;
1429c4a7965SKim Phillips 
1434b992628SKim Phillips 	/* next channel to be assigned next incoming descriptor */
1444b992628SKim Phillips 	atomic_t last_chan ____cacheline_aligned;
1459c4a7965SKim Phillips 
1469c4a7965SKim Phillips 	/* request callback tasklet */
1479c4a7965SKim Phillips 	struct tasklet_struct done_task;
1489c4a7965SKim Phillips 
1499c4a7965SKim Phillips 	/* list of registered algorithms */
1509c4a7965SKim Phillips 	struct list_head alg_list;
1519c4a7965SKim Phillips 
1529c4a7965SKim Phillips 	/* hwrng device */
1539c4a7965SKim Phillips 	struct hwrng rng;
1549c4a7965SKim Phillips };
1559c4a7965SKim Phillips 
156f3c85bc1SLee Nipper /* .features flag */
157f3c85bc1SLee Nipper #define TALITOS_FTR_SRC_LINK_TBL_LEN_INCLUDES_EXTENT 0x00000001
158fe5720e2SKim Phillips #define TALITOS_FTR_HW_AUTH_CHECK 0x00000002
15960f208d7SKim Phillips #define TALITOS_FTR_SHA224_HWINIT 0x00000004
160f3c85bc1SLee Nipper 
16181eb024cSKim Phillips static void to_talitos_ptr(struct talitos_ptr *talitos_ptr, dma_addr_t dma_addr)
16281eb024cSKim Phillips {
16381eb024cSKim Phillips 	talitos_ptr->ptr = cpu_to_be32(lower_32_bits(dma_addr));
16481eb024cSKim Phillips 	talitos_ptr->eptr = cpu_to_be32(upper_32_bits(dma_addr));
16581eb024cSKim Phillips }
16681eb024cSKim Phillips 
1679c4a7965SKim Phillips /*
1689c4a7965SKim Phillips  * map virtual single (contiguous) pointer to h/w descriptor pointer
1699c4a7965SKim Phillips  */
1709c4a7965SKim Phillips static void map_single_talitos_ptr(struct device *dev,
1719c4a7965SKim Phillips 				   struct talitos_ptr *talitos_ptr,
1729c4a7965SKim Phillips 				   unsigned short len, void *data,
1739c4a7965SKim Phillips 				   unsigned char extent,
1749c4a7965SKim Phillips 				   enum dma_data_direction dir)
1759c4a7965SKim Phillips {
17681eb024cSKim Phillips 	dma_addr_t dma_addr = dma_map_single(dev, data, len, dir);
17781eb024cSKim Phillips 
1789c4a7965SKim Phillips 	talitos_ptr->len = cpu_to_be16(len);
17981eb024cSKim Phillips 	to_talitos_ptr(talitos_ptr, dma_addr);
1809c4a7965SKim Phillips 	talitos_ptr->j_extent = extent;
1819c4a7965SKim Phillips }
1829c4a7965SKim Phillips 
1839c4a7965SKim Phillips /*
1849c4a7965SKim Phillips  * unmap bus single (contiguous) h/w descriptor pointer
1859c4a7965SKim Phillips  */
1869c4a7965SKim Phillips static void unmap_single_talitos_ptr(struct device *dev,
1879c4a7965SKim Phillips 				     struct talitos_ptr *talitos_ptr,
1889c4a7965SKim Phillips 				     enum dma_data_direction dir)
1899c4a7965SKim Phillips {
1909c4a7965SKim Phillips 	dma_unmap_single(dev, be32_to_cpu(talitos_ptr->ptr),
1919c4a7965SKim Phillips 			 be16_to_cpu(talitos_ptr->len), dir);
1929c4a7965SKim Phillips }
1939c4a7965SKim Phillips 
1949c4a7965SKim Phillips static int reset_channel(struct device *dev, int ch)
1959c4a7965SKim Phillips {
1969c4a7965SKim Phillips 	struct talitos_private *priv = dev_get_drvdata(dev);
1979c4a7965SKim Phillips 	unsigned int timeout = TALITOS_TIMEOUT;
1989c4a7965SKim Phillips 
1999c4a7965SKim Phillips 	setbits32(priv->reg + TALITOS_CCCR(ch), TALITOS_CCCR_RESET);
2009c4a7965SKim Phillips 
2019c4a7965SKim Phillips 	while ((in_be32(priv->reg + TALITOS_CCCR(ch)) & TALITOS_CCCR_RESET)
2029c4a7965SKim Phillips 	       && --timeout)
2039c4a7965SKim Phillips 		cpu_relax();
2049c4a7965SKim Phillips 
2059c4a7965SKim Phillips 	if (timeout == 0) {
2069c4a7965SKim Phillips 		dev_err(dev, "failed to reset channel %d\n", ch);
2079c4a7965SKim Phillips 		return -EIO;
2089c4a7965SKim Phillips 	}
2099c4a7965SKim Phillips 
21081eb024cSKim Phillips 	/* set 36-bit addressing, done writeback enable and done IRQ enable */
21181eb024cSKim Phillips 	setbits32(priv->reg + TALITOS_CCCR_LO(ch), TALITOS_CCCR_LO_EAE |
21281eb024cSKim Phillips 		  TALITOS_CCCR_LO_CDWE | TALITOS_CCCR_LO_CDIE);
2139c4a7965SKim Phillips 
214fe5720e2SKim Phillips 	/* and ICCR writeback, if available */
215fe5720e2SKim Phillips 	if (priv->features & TALITOS_FTR_HW_AUTH_CHECK)
216fe5720e2SKim Phillips 		setbits32(priv->reg + TALITOS_CCCR_LO(ch),
217fe5720e2SKim Phillips 		          TALITOS_CCCR_LO_IWSE);
218fe5720e2SKim Phillips 
2199c4a7965SKim Phillips 	return 0;
2209c4a7965SKim Phillips }
2219c4a7965SKim Phillips 
2229c4a7965SKim Phillips static int reset_device(struct device *dev)
2239c4a7965SKim Phillips {
2249c4a7965SKim Phillips 	struct talitos_private *priv = dev_get_drvdata(dev);
2259c4a7965SKim Phillips 	unsigned int timeout = TALITOS_TIMEOUT;
2269c4a7965SKim Phillips 
2279c4a7965SKim Phillips 	setbits32(priv->reg + TALITOS_MCR, TALITOS_MCR_SWR);
2289c4a7965SKim Phillips 
2299c4a7965SKim Phillips 	while ((in_be32(priv->reg + TALITOS_MCR) & TALITOS_MCR_SWR)
2309c4a7965SKim Phillips 	       && --timeout)
2319c4a7965SKim Phillips 		cpu_relax();
2329c4a7965SKim Phillips 
2339c4a7965SKim Phillips 	if (timeout == 0) {
2349c4a7965SKim Phillips 		dev_err(dev, "failed to reset device\n");
2359c4a7965SKim Phillips 		return -EIO;
2369c4a7965SKim Phillips 	}
2379c4a7965SKim Phillips 
2389c4a7965SKim Phillips 	return 0;
2399c4a7965SKim Phillips }
2409c4a7965SKim Phillips 
2419c4a7965SKim Phillips /*
2429c4a7965SKim Phillips  * Reset and initialize the device
2439c4a7965SKim Phillips  */
2449c4a7965SKim Phillips static int init_device(struct device *dev)
2459c4a7965SKim Phillips {
2469c4a7965SKim Phillips 	struct talitos_private *priv = dev_get_drvdata(dev);
2479c4a7965SKim Phillips 	int ch, err;
2489c4a7965SKim Phillips 
2499c4a7965SKim Phillips 	/*
2509c4a7965SKim Phillips 	 * Master reset
2519c4a7965SKim Phillips 	 * errata documentation: warning: certain SEC interrupts
2529c4a7965SKim Phillips 	 * are not fully cleared by writing the MCR:SWR bit,
2539c4a7965SKim Phillips 	 * set bit twice to completely reset
2549c4a7965SKim Phillips 	 */
2559c4a7965SKim Phillips 	err = reset_device(dev);
2569c4a7965SKim Phillips 	if (err)
2579c4a7965SKim Phillips 		return err;
2589c4a7965SKim Phillips 
2599c4a7965SKim Phillips 	err = reset_device(dev);
2609c4a7965SKim Phillips 	if (err)
2619c4a7965SKim Phillips 		return err;
2629c4a7965SKim Phillips 
2639c4a7965SKim Phillips 	/* reset channels */
2649c4a7965SKim Phillips 	for (ch = 0; ch < priv->num_channels; ch++) {
2659c4a7965SKim Phillips 		err = reset_channel(dev, ch);
2669c4a7965SKim Phillips 		if (err)
2679c4a7965SKim Phillips 			return err;
2689c4a7965SKim Phillips 	}
2699c4a7965SKim Phillips 
2709c4a7965SKim Phillips 	/* enable channel done and error interrupts */
2719c4a7965SKim Phillips 	setbits32(priv->reg + TALITOS_IMR, TALITOS_IMR_INIT);
2729c4a7965SKim Phillips 	setbits32(priv->reg + TALITOS_IMR_LO, TALITOS_IMR_LO_INIT);
2739c4a7965SKim Phillips 
274fe5720e2SKim Phillips 	/* disable integrity check error interrupts (use writeback instead) */
275fe5720e2SKim Phillips 	if (priv->features & TALITOS_FTR_HW_AUTH_CHECK)
276fe5720e2SKim Phillips 		setbits32(priv->reg + TALITOS_MDEUICR_LO,
277fe5720e2SKim Phillips 		          TALITOS_MDEUICR_LO_ICE);
278fe5720e2SKim Phillips 
2799c4a7965SKim Phillips 	return 0;
2809c4a7965SKim Phillips }
2819c4a7965SKim Phillips 
2829c4a7965SKim Phillips /**
2839c4a7965SKim Phillips  * talitos_submit - submits a descriptor to the device for processing
2849c4a7965SKim Phillips  * @dev:	the SEC device to be used
2859c4a7965SKim Phillips  * @desc:	the descriptor to be processed by the device
2869c4a7965SKim Phillips  * @callback:	whom to call when processing is complete
2879c4a7965SKim Phillips  * @context:	a handle for use by caller (optional)
2889c4a7965SKim Phillips  *
2899c4a7965SKim Phillips  * desc must contain valid dma-mapped (bus physical) address pointers.
2909c4a7965SKim Phillips  * callback must check err and feedback in descriptor header
2919c4a7965SKim Phillips  * for device processing status.
2929c4a7965SKim Phillips  */
2939c4a7965SKim Phillips static int talitos_submit(struct device *dev, struct talitos_desc *desc,
2949c4a7965SKim Phillips 			  void (*callback)(struct device *dev,
2959c4a7965SKim Phillips 					   struct talitos_desc *desc,
2969c4a7965SKim Phillips 					   void *context, int error),
2979c4a7965SKim Phillips 			  void *context)
2989c4a7965SKim Phillips {
2999c4a7965SKim Phillips 	struct talitos_private *priv = dev_get_drvdata(dev);
3009c4a7965SKim Phillips 	struct talitos_request *request;
3019c4a7965SKim Phillips 	unsigned long flags, ch;
3029c4a7965SKim Phillips 	int head;
3039c4a7965SKim Phillips 
3049c4a7965SKim Phillips 	/* select done notification */
3059c4a7965SKim Phillips 	desc->hdr |= DESC_HDR_DONE_NOTIFY;
3069c4a7965SKim Phillips 
3079c4a7965SKim Phillips 	/* emulate SEC's round-robin channel fifo polling scheme */
3089c4a7965SKim Phillips 	ch = atomic_inc_return(&priv->last_chan) & (priv->num_channels - 1);
3099c4a7965SKim Phillips 
3104b992628SKim Phillips 	spin_lock_irqsave(&priv->chan[ch].head_lock, flags);
3119c4a7965SKim Phillips 
3124b992628SKim Phillips 	if (!atomic_inc_not_zero(&priv->chan[ch].submit_count)) {
313ec6644d6SKim Phillips 		/* h/w fifo is full */
3144b992628SKim Phillips 		spin_unlock_irqrestore(&priv->chan[ch].head_lock, flags);
3159c4a7965SKim Phillips 		return -EAGAIN;
3169c4a7965SKim Phillips 	}
3179c4a7965SKim Phillips 
3184b992628SKim Phillips 	head = priv->chan[ch].head;
3194b992628SKim Phillips 	request = &priv->chan[ch].fifo[head];
320ec6644d6SKim Phillips 
3219c4a7965SKim Phillips 	/* map descriptor and save caller data */
3229c4a7965SKim Phillips 	request->dma_desc = dma_map_single(dev, desc, sizeof(*desc),
3239c4a7965SKim Phillips 					   DMA_BIDIRECTIONAL);
3249c4a7965SKim Phillips 	request->callback = callback;
3259c4a7965SKim Phillips 	request->context = context;
3269c4a7965SKim Phillips 
3279c4a7965SKim Phillips 	/* increment fifo head */
3284b992628SKim Phillips 	priv->chan[ch].head = (priv->chan[ch].head + 1) & (priv->fifo_len - 1);
3299c4a7965SKim Phillips 
3309c4a7965SKim Phillips 	smp_wmb();
3319c4a7965SKim Phillips 	request->desc = desc;
3329c4a7965SKim Phillips 
3339c4a7965SKim Phillips 	/* GO! */
3349c4a7965SKim Phillips 	wmb();
33581eb024cSKim Phillips 	out_be32(priv->reg + TALITOS_FF(ch),
33681eb024cSKim Phillips 		 cpu_to_be32(upper_32_bits(request->dma_desc)));
33781eb024cSKim Phillips 	out_be32(priv->reg + TALITOS_FF_LO(ch),
33881eb024cSKim Phillips 		 cpu_to_be32(lower_32_bits(request->dma_desc)));
3399c4a7965SKim Phillips 
3404b992628SKim Phillips 	spin_unlock_irqrestore(&priv->chan[ch].head_lock, flags);
3419c4a7965SKim Phillips 
3429c4a7965SKim Phillips 	return -EINPROGRESS;
3439c4a7965SKim Phillips }
3449c4a7965SKim Phillips 
3459c4a7965SKim Phillips /*
3469c4a7965SKim Phillips  * process what was done, notify callback of error if not
3479c4a7965SKim Phillips  */
3489c4a7965SKim Phillips static void flush_channel(struct device *dev, int ch, int error, int reset_ch)
3499c4a7965SKim Phillips {
3509c4a7965SKim Phillips 	struct talitos_private *priv = dev_get_drvdata(dev);
3519c4a7965SKim Phillips 	struct talitos_request *request, saved_req;
3529c4a7965SKim Phillips 	unsigned long flags;
3539c4a7965SKim Phillips 	int tail, status;
3549c4a7965SKim Phillips 
3554b992628SKim Phillips 	spin_lock_irqsave(&priv->chan[ch].tail_lock, flags);
3569c4a7965SKim Phillips 
3574b992628SKim Phillips 	tail = priv->chan[ch].tail;
3584b992628SKim Phillips 	while (priv->chan[ch].fifo[tail].desc) {
3594b992628SKim Phillips 		request = &priv->chan[ch].fifo[tail];
3609c4a7965SKim Phillips 
3619c4a7965SKim Phillips 		/* descriptors with their done bits set don't get the error */
3629c4a7965SKim Phillips 		rmb();
363ca38a814SLee Nipper 		if ((request->desc->hdr & DESC_HDR_DONE) == DESC_HDR_DONE)
3649c4a7965SKim Phillips 			status = 0;
365ca38a814SLee Nipper 		else
3669c4a7965SKim Phillips 			if (!error)
3679c4a7965SKim Phillips 				break;
3689c4a7965SKim Phillips 			else
3699c4a7965SKim Phillips 				status = error;
3709c4a7965SKim Phillips 
3719c4a7965SKim Phillips 		dma_unmap_single(dev, request->dma_desc,
372e938e465SKim Phillips 				 sizeof(struct talitos_desc),
373e938e465SKim Phillips 				 DMA_BIDIRECTIONAL);
3749c4a7965SKim Phillips 
3759c4a7965SKim Phillips 		/* copy entries so we can call callback outside lock */
3769c4a7965SKim Phillips 		saved_req.desc = request->desc;
3779c4a7965SKim Phillips 		saved_req.callback = request->callback;
3789c4a7965SKim Phillips 		saved_req.context = request->context;
3799c4a7965SKim Phillips 
3809c4a7965SKim Phillips 		/* release request entry in fifo */
3819c4a7965SKim Phillips 		smp_wmb();
3829c4a7965SKim Phillips 		request->desc = NULL;
3839c4a7965SKim Phillips 
3849c4a7965SKim Phillips 		/* increment fifo tail */
3854b992628SKim Phillips 		priv->chan[ch].tail = (tail + 1) & (priv->fifo_len - 1);
3869c4a7965SKim Phillips 
3874b992628SKim Phillips 		spin_unlock_irqrestore(&priv->chan[ch].tail_lock, flags);
388ec6644d6SKim Phillips 
3894b992628SKim Phillips 		atomic_dec(&priv->chan[ch].submit_count);
390ec6644d6SKim Phillips 
3919c4a7965SKim Phillips 		saved_req.callback(dev, saved_req.desc, saved_req.context,
3929c4a7965SKim Phillips 				   status);
3939c4a7965SKim Phillips 		/* channel may resume processing in single desc error case */
3949c4a7965SKim Phillips 		if (error && !reset_ch && status == error)
3959c4a7965SKim Phillips 			return;
3964b992628SKim Phillips 		spin_lock_irqsave(&priv->chan[ch].tail_lock, flags);
3974b992628SKim Phillips 		tail = priv->chan[ch].tail;
3989c4a7965SKim Phillips 	}
3999c4a7965SKim Phillips 
4004b992628SKim Phillips 	spin_unlock_irqrestore(&priv->chan[ch].tail_lock, flags);
4019c4a7965SKim Phillips }
4029c4a7965SKim Phillips 
4039c4a7965SKim Phillips /*
4049c4a7965SKim Phillips  * process completed requests for channels that have done status
4059c4a7965SKim Phillips  */
4069c4a7965SKim Phillips static void talitos_done(unsigned long data)
4079c4a7965SKim Phillips {
4089c4a7965SKim Phillips 	struct device *dev = (struct device *)data;
4099c4a7965SKim Phillips 	struct talitos_private *priv = dev_get_drvdata(dev);
4109c4a7965SKim Phillips 	int ch;
4119c4a7965SKim Phillips 
4129c4a7965SKim Phillips 	for (ch = 0; ch < priv->num_channels; ch++)
4139c4a7965SKim Phillips 		flush_channel(dev, ch, 0, 0);
4141c2e8811SLee Nipper 
4151c2e8811SLee Nipper 	/* At this point, all completed channels have been processed.
4161c2e8811SLee Nipper 	 * Unmask done interrupts for channels completed later on.
4171c2e8811SLee Nipper 	 */
418fe5720e2SKim Phillips 	setbits32(priv->reg + TALITOS_IMR, TALITOS_IMR_INIT);
419fe5720e2SKim Phillips 	setbits32(priv->reg + TALITOS_IMR_LO, TALITOS_IMR_LO_INIT);
4209c4a7965SKim Phillips }
4219c4a7965SKim Phillips 
4229c4a7965SKim Phillips /*
4239c4a7965SKim Phillips  * locate current (offending) descriptor
4249c4a7965SKim Phillips  */
4259c4a7965SKim Phillips static struct talitos_desc *current_desc(struct device *dev, int ch)
4269c4a7965SKim Phillips {
4279c4a7965SKim Phillips 	struct talitos_private *priv = dev_get_drvdata(dev);
4284b992628SKim Phillips 	int tail = priv->chan[ch].tail;
4299c4a7965SKim Phillips 	dma_addr_t cur_desc;
4309c4a7965SKim Phillips 
4319c4a7965SKim Phillips 	cur_desc = in_be32(priv->reg + TALITOS_CDPR_LO(ch));
4329c4a7965SKim Phillips 
4334b992628SKim Phillips 	while (priv->chan[ch].fifo[tail].dma_desc != cur_desc) {
4349c4a7965SKim Phillips 		tail = (tail + 1) & (priv->fifo_len - 1);
4354b992628SKim Phillips 		if (tail == priv->chan[ch].tail) {
4369c4a7965SKim Phillips 			dev_err(dev, "couldn't locate current descriptor\n");
4379c4a7965SKim Phillips 			return NULL;
4389c4a7965SKim Phillips 		}
4399c4a7965SKim Phillips 	}
4409c4a7965SKim Phillips 
4414b992628SKim Phillips 	return priv->chan[ch].fifo[tail].desc;
4429c4a7965SKim Phillips }
4439c4a7965SKim Phillips 
4449c4a7965SKim Phillips /*
4459c4a7965SKim Phillips  * user diagnostics; report root cause of error based on execution unit status
4469c4a7965SKim Phillips  */
447e938e465SKim Phillips static void report_eu_error(struct device *dev, int ch,
448e938e465SKim Phillips 			    struct talitos_desc *desc)
4499c4a7965SKim Phillips {
4509c4a7965SKim Phillips 	struct talitos_private *priv = dev_get_drvdata(dev);
4519c4a7965SKim Phillips 	int i;
4529c4a7965SKim Phillips 
4539c4a7965SKim Phillips 	switch (desc->hdr & DESC_HDR_SEL0_MASK) {
4549c4a7965SKim Phillips 	case DESC_HDR_SEL0_AFEU:
4559c4a7965SKim Phillips 		dev_err(dev, "AFEUISR 0x%08x_%08x\n",
4569c4a7965SKim Phillips 			in_be32(priv->reg + TALITOS_AFEUISR),
4579c4a7965SKim Phillips 			in_be32(priv->reg + TALITOS_AFEUISR_LO));
4589c4a7965SKim Phillips 		break;
4599c4a7965SKim Phillips 	case DESC_HDR_SEL0_DEU:
4609c4a7965SKim Phillips 		dev_err(dev, "DEUISR 0x%08x_%08x\n",
4619c4a7965SKim Phillips 			in_be32(priv->reg + TALITOS_DEUISR),
4629c4a7965SKim Phillips 			in_be32(priv->reg + TALITOS_DEUISR_LO));
4639c4a7965SKim Phillips 		break;
4649c4a7965SKim Phillips 	case DESC_HDR_SEL0_MDEUA:
4659c4a7965SKim Phillips 	case DESC_HDR_SEL0_MDEUB:
4669c4a7965SKim Phillips 		dev_err(dev, "MDEUISR 0x%08x_%08x\n",
4679c4a7965SKim Phillips 			in_be32(priv->reg + TALITOS_MDEUISR),
4689c4a7965SKim Phillips 			in_be32(priv->reg + TALITOS_MDEUISR_LO));
4699c4a7965SKim Phillips 		break;
4709c4a7965SKim Phillips 	case DESC_HDR_SEL0_RNG:
4719c4a7965SKim Phillips 		dev_err(dev, "RNGUISR 0x%08x_%08x\n",
4729c4a7965SKim Phillips 			in_be32(priv->reg + TALITOS_RNGUISR),
4739c4a7965SKim Phillips 			in_be32(priv->reg + TALITOS_RNGUISR_LO));
4749c4a7965SKim Phillips 		break;
4759c4a7965SKim Phillips 	case DESC_HDR_SEL0_PKEU:
4769c4a7965SKim Phillips 		dev_err(dev, "PKEUISR 0x%08x_%08x\n",
4779c4a7965SKim Phillips 			in_be32(priv->reg + TALITOS_PKEUISR),
4789c4a7965SKim Phillips 			in_be32(priv->reg + TALITOS_PKEUISR_LO));
4799c4a7965SKim Phillips 		break;
4809c4a7965SKim Phillips 	case DESC_HDR_SEL0_AESU:
4819c4a7965SKim Phillips 		dev_err(dev, "AESUISR 0x%08x_%08x\n",
4829c4a7965SKim Phillips 			in_be32(priv->reg + TALITOS_AESUISR),
4839c4a7965SKim Phillips 			in_be32(priv->reg + TALITOS_AESUISR_LO));
4849c4a7965SKim Phillips 		break;
4859c4a7965SKim Phillips 	case DESC_HDR_SEL0_CRCU:
4869c4a7965SKim Phillips 		dev_err(dev, "CRCUISR 0x%08x_%08x\n",
4879c4a7965SKim Phillips 			in_be32(priv->reg + TALITOS_CRCUISR),
4889c4a7965SKim Phillips 			in_be32(priv->reg + TALITOS_CRCUISR_LO));
4899c4a7965SKim Phillips 		break;
4909c4a7965SKim Phillips 	case DESC_HDR_SEL0_KEU:
4919c4a7965SKim Phillips 		dev_err(dev, "KEUISR 0x%08x_%08x\n",
4929c4a7965SKim Phillips 			in_be32(priv->reg + TALITOS_KEUISR),
4939c4a7965SKim Phillips 			in_be32(priv->reg + TALITOS_KEUISR_LO));
4949c4a7965SKim Phillips 		break;
4959c4a7965SKim Phillips 	}
4969c4a7965SKim Phillips 
4979c4a7965SKim Phillips 	switch (desc->hdr & DESC_HDR_SEL1_MASK) {
4989c4a7965SKim Phillips 	case DESC_HDR_SEL1_MDEUA:
4999c4a7965SKim Phillips 	case DESC_HDR_SEL1_MDEUB:
5009c4a7965SKim Phillips 		dev_err(dev, "MDEUISR 0x%08x_%08x\n",
5019c4a7965SKim Phillips 			in_be32(priv->reg + TALITOS_MDEUISR),
5029c4a7965SKim Phillips 			in_be32(priv->reg + TALITOS_MDEUISR_LO));
5039c4a7965SKim Phillips 		break;
5049c4a7965SKim Phillips 	case DESC_HDR_SEL1_CRCU:
5059c4a7965SKim Phillips 		dev_err(dev, "CRCUISR 0x%08x_%08x\n",
5069c4a7965SKim Phillips 			in_be32(priv->reg + TALITOS_CRCUISR),
5079c4a7965SKim Phillips 			in_be32(priv->reg + TALITOS_CRCUISR_LO));
5089c4a7965SKim Phillips 		break;
5099c4a7965SKim Phillips 	}
5109c4a7965SKim Phillips 
5119c4a7965SKim Phillips 	for (i = 0; i < 8; i++)
5129c4a7965SKim Phillips 		dev_err(dev, "DESCBUF 0x%08x_%08x\n",
5139c4a7965SKim Phillips 			in_be32(priv->reg + TALITOS_DESCBUF(ch) + 8*i),
5149c4a7965SKim Phillips 			in_be32(priv->reg + TALITOS_DESCBUF_LO(ch) + 8*i));
5159c4a7965SKim Phillips }
5169c4a7965SKim Phillips 
5179c4a7965SKim Phillips /*
5189c4a7965SKim Phillips  * recover from error interrupts
5199c4a7965SKim Phillips  */
52040405f10SKim Phillips static void talitos_error(unsigned long data, u32 isr, u32 isr_lo)
5219c4a7965SKim Phillips {
5229c4a7965SKim Phillips 	struct device *dev = (struct device *)data;
5239c4a7965SKim Phillips 	struct talitos_private *priv = dev_get_drvdata(dev);
5249c4a7965SKim Phillips 	unsigned int timeout = TALITOS_TIMEOUT;
5259c4a7965SKim Phillips 	int ch, error, reset_dev = 0, reset_ch = 0;
52640405f10SKim Phillips 	u32 v, v_lo;
5279c4a7965SKim Phillips 
5289c4a7965SKim Phillips 	for (ch = 0; ch < priv->num_channels; ch++) {
5299c4a7965SKim Phillips 		/* skip channels without errors */
5309c4a7965SKim Phillips 		if (!(isr & (1 << (ch * 2 + 1))))
5319c4a7965SKim Phillips 			continue;
5329c4a7965SKim Phillips 
5339c4a7965SKim Phillips 		error = -EINVAL;
5349c4a7965SKim Phillips 
5359c4a7965SKim Phillips 		v = in_be32(priv->reg + TALITOS_CCPSR(ch));
5369c4a7965SKim Phillips 		v_lo = in_be32(priv->reg + TALITOS_CCPSR_LO(ch));
5379c4a7965SKim Phillips 
5389c4a7965SKim Phillips 		if (v_lo & TALITOS_CCPSR_LO_DOF) {
5399c4a7965SKim Phillips 			dev_err(dev, "double fetch fifo overflow error\n");
5409c4a7965SKim Phillips 			error = -EAGAIN;
5419c4a7965SKim Phillips 			reset_ch = 1;
5429c4a7965SKim Phillips 		}
5439c4a7965SKim Phillips 		if (v_lo & TALITOS_CCPSR_LO_SOF) {
5449c4a7965SKim Phillips 			/* h/w dropped descriptor */
5459c4a7965SKim Phillips 			dev_err(dev, "single fetch fifo overflow error\n");
5469c4a7965SKim Phillips 			error = -EAGAIN;
5479c4a7965SKim Phillips 		}
5489c4a7965SKim Phillips 		if (v_lo & TALITOS_CCPSR_LO_MDTE)
5499c4a7965SKim Phillips 			dev_err(dev, "master data transfer error\n");
5509c4a7965SKim Phillips 		if (v_lo & TALITOS_CCPSR_LO_SGDLZ)
5519c4a7965SKim Phillips 			dev_err(dev, "s/g data length zero error\n");
5529c4a7965SKim Phillips 		if (v_lo & TALITOS_CCPSR_LO_FPZ)
5539c4a7965SKim Phillips 			dev_err(dev, "fetch pointer zero error\n");
5549c4a7965SKim Phillips 		if (v_lo & TALITOS_CCPSR_LO_IDH)
5559c4a7965SKim Phillips 			dev_err(dev, "illegal descriptor header error\n");
5569c4a7965SKim Phillips 		if (v_lo & TALITOS_CCPSR_LO_IEU)
5579c4a7965SKim Phillips 			dev_err(dev, "invalid execution unit error\n");
5589c4a7965SKim Phillips 		if (v_lo & TALITOS_CCPSR_LO_EU)
5599c4a7965SKim Phillips 			report_eu_error(dev, ch, current_desc(dev, ch));
5609c4a7965SKim Phillips 		if (v_lo & TALITOS_CCPSR_LO_GB)
5619c4a7965SKim Phillips 			dev_err(dev, "gather boundary error\n");
5629c4a7965SKim Phillips 		if (v_lo & TALITOS_CCPSR_LO_GRL)
5639c4a7965SKim Phillips 			dev_err(dev, "gather return/length error\n");
5649c4a7965SKim Phillips 		if (v_lo & TALITOS_CCPSR_LO_SB)
5659c4a7965SKim Phillips 			dev_err(dev, "scatter boundary error\n");
5669c4a7965SKim Phillips 		if (v_lo & TALITOS_CCPSR_LO_SRL)
5679c4a7965SKim Phillips 			dev_err(dev, "scatter return/length error\n");
5689c4a7965SKim Phillips 
5699c4a7965SKim Phillips 		flush_channel(dev, ch, error, reset_ch);
5709c4a7965SKim Phillips 
5719c4a7965SKim Phillips 		if (reset_ch) {
5729c4a7965SKim Phillips 			reset_channel(dev, ch);
5739c4a7965SKim Phillips 		} else {
5749c4a7965SKim Phillips 			setbits32(priv->reg + TALITOS_CCCR(ch),
5759c4a7965SKim Phillips 				  TALITOS_CCCR_CONT);
5769c4a7965SKim Phillips 			setbits32(priv->reg + TALITOS_CCCR_LO(ch), 0);
5779c4a7965SKim Phillips 			while ((in_be32(priv->reg + TALITOS_CCCR(ch)) &
5789c4a7965SKim Phillips 			       TALITOS_CCCR_CONT) && --timeout)
5799c4a7965SKim Phillips 				cpu_relax();
5809c4a7965SKim Phillips 			if (timeout == 0) {
5819c4a7965SKim Phillips 				dev_err(dev, "failed to restart channel %d\n",
5829c4a7965SKim Phillips 					ch);
5839c4a7965SKim Phillips 				reset_dev = 1;
5849c4a7965SKim Phillips 			}
5859c4a7965SKim Phillips 		}
5869c4a7965SKim Phillips 	}
5879c4a7965SKim Phillips 	if (reset_dev || isr & ~TALITOS_ISR_CHERR || isr_lo) {
5889c4a7965SKim Phillips 		dev_err(dev, "done overflow, internal time out, or rngu error: "
5899c4a7965SKim Phillips 		        "ISR 0x%08x_%08x\n", isr, isr_lo);
5909c4a7965SKim Phillips 
5919c4a7965SKim Phillips 		/* purge request queues */
5929c4a7965SKim Phillips 		for (ch = 0; ch < priv->num_channels; ch++)
5939c4a7965SKim Phillips 			flush_channel(dev, ch, -EIO, 1);
5949c4a7965SKim Phillips 
5959c4a7965SKim Phillips 		/* reset and reinitialize the device */
5969c4a7965SKim Phillips 		init_device(dev);
5979c4a7965SKim Phillips 	}
5989c4a7965SKim Phillips }
5999c4a7965SKim Phillips 
6009c4a7965SKim Phillips static irqreturn_t talitos_interrupt(int irq, void *data)
6019c4a7965SKim Phillips {
6029c4a7965SKim Phillips 	struct device *dev = data;
6039c4a7965SKim Phillips 	struct talitos_private *priv = dev_get_drvdata(dev);
6049c4a7965SKim Phillips 	u32 isr, isr_lo;
6059c4a7965SKim Phillips 
6069c4a7965SKim Phillips 	isr = in_be32(priv->reg + TALITOS_ISR);
6079c4a7965SKim Phillips 	isr_lo = in_be32(priv->reg + TALITOS_ISR_LO);
608ca38a814SLee Nipper 	/* Acknowledge interrupt */
6099c4a7965SKim Phillips 	out_be32(priv->reg + TALITOS_ICR, isr);
6109c4a7965SKim Phillips 	out_be32(priv->reg + TALITOS_ICR_LO, isr_lo);
6119c4a7965SKim Phillips 
612ca38a814SLee Nipper 	if (unlikely((isr & ~TALITOS_ISR_CHDONE) || isr_lo))
61340405f10SKim Phillips 		talitos_error((unsigned long)data, isr, isr_lo);
614ca38a814SLee Nipper 	else
6151c2e8811SLee Nipper 		if (likely(isr & TALITOS_ISR_CHDONE)) {
6161c2e8811SLee Nipper 			/* mask further done interrupts. */
6171c2e8811SLee Nipper 			clrbits32(priv->reg + TALITOS_IMR, TALITOS_IMR_DONE);
6181c2e8811SLee Nipper 			/* done_task will unmask done interrupts at exit */
6199c4a7965SKim Phillips 			tasklet_schedule(&priv->done_task);
6201c2e8811SLee Nipper 		}
6219c4a7965SKim Phillips 
6229c4a7965SKim Phillips 	return (isr || isr_lo) ? IRQ_HANDLED : IRQ_NONE;
6239c4a7965SKim Phillips }
6249c4a7965SKim Phillips 
6259c4a7965SKim Phillips /*
6269c4a7965SKim Phillips  * hwrng
6279c4a7965SKim Phillips  */
6289c4a7965SKim Phillips static int talitos_rng_data_present(struct hwrng *rng, int wait)
6299c4a7965SKim Phillips {
6309c4a7965SKim Phillips 	struct device *dev = (struct device *)rng->priv;
6319c4a7965SKim Phillips 	struct talitos_private *priv = dev_get_drvdata(dev);
6329c4a7965SKim Phillips 	u32 ofl;
6339c4a7965SKim Phillips 	int i;
6349c4a7965SKim Phillips 
6359c4a7965SKim Phillips 	for (i = 0; i < 20; i++) {
6369c4a7965SKim Phillips 		ofl = in_be32(priv->reg + TALITOS_RNGUSR_LO) &
6379c4a7965SKim Phillips 		      TALITOS_RNGUSR_LO_OFL;
6389c4a7965SKim Phillips 		if (ofl || !wait)
6399c4a7965SKim Phillips 			break;
6409c4a7965SKim Phillips 		udelay(10);
6419c4a7965SKim Phillips 	}
6429c4a7965SKim Phillips 
6439c4a7965SKim Phillips 	return !!ofl;
6449c4a7965SKim Phillips }
6459c4a7965SKim Phillips 
6469c4a7965SKim Phillips static int talitos_rng_data_read(struct hwrng *rng, u32 *data)
6479c4a7965SKim Phillips {
6489c4a7965SKim Phillips 	struct device *dev = (struct device *)rng->priv;
6499c4a7965SKim Phillips 	struct talitos_private *priv = dev_get_drvdata(dev);
6509c4a7965SKim Phillips 
6519c4a7965SKim Phillips 	/* rng fifo requires 64-bit accesses */
6529c4a7965SKim Phillips 	*data = in_be32(priv->reg + TALITOS_RNGU_FIFO);
6539c4a7965SKim Phillips 	*data = in_be32(priv->reg + TALITOS_RNGU_FIFO_LO);
6549c4a7965SKim Phillips 
6559c4a7965SKim Phillips 	return sizeof(u32);
6569c4a7965SKim Phillips }
6579c4a7965SKim Phillips 
6589c4a7965SKim Phillips static int talitos_rng_init(struct hwrng *rng)
6599c4a7965SKim Phillips {
6609c4a7965SKim Phillips 	struct device *dev = (struct device *)rng->priv;
6619c4a7965SKim Phillips 	struct talitos_private *priv = dev_get_drvdata(dev);
6629c4a7965SKim Phillips 	unsigned int timeout = TALITOS_TIMEOUT;
6639c4a7965SKim Phillips 
6649c4a7965SKim Phillips 	setbits32(priv->reg + TALITOS_RNGURCR_LO, TALITOS_RNGURCR_LO_SR);
6659c4a7965SKim Phillips 	while (!(in_be32(priv->reg + TALITOS_RNGUSR_LO) & TALITOS_RNGUSR_LO_RD)
6669c4a7965SKim Phillips 	       && --timeout)
6679c4a7965SKim Phillips 		cpu_relax();
6689c4a7965SKim Phillips 	if (timeout == 0) {
6699c4a7965SKim Phillips 		dev_err(dev, "failed to reset rng hw\n");
6709c4a7965SKim Phillips 		return -ENODEV;
6719c4a7965SKim Phillips 	}
6729c4a7965SKim Phillips 
6739c4a7965SKim Phillips 	/* start generating */
6749c4a7965SKim Phillips 	setbits32(priv->reg + TALITOS_RNGUDSR_LO, 0);
6759c4a7965SKim Phillips 
6769c4a7965SKim Phillips 	return 0;
6779c4a7965SKim Phillips }
6789c4a7965SKim Phillips 
6799c4a7965SKim Phillips static int talitos_register_rng(struct device *dev)
6809c4a7965SKim Phillips {
6819c4a7965SKim Phillips 	struct talitos_private *priv = dev_get_drvdata(dev);
6829c4a7965SKim Phillips 
6839c4a7965SKim Phillips 	priv->rng.name		= dev_driver_string(dev),
6849c4a7965SKim Phillips 	priv->rng.init		= talitos_rng_init,
6859c4a7965SKim Phillips 	priv->rng.data_present	= talitos_rng_data_present,
6869c4a7965SKim Phillips 	priv->rng.data_read	= talitos_rng_data_read,
6879c4a7965SKim Phillips 	priv->rng.priv		= (unsigned long)dev;
6889c4a7965SKim Phillips 
6899c4a7965SKim Phillips 	return hwrng_register(&priv->rng);
6909c4a7965SKim Phillips }
6919c4a7965SKim Phillips 
6929c4a7965SKim Phillips static void talitos_unregister_rng(struct device *dev)
6939c4a7965SKim Phillips {
6949c4a7965SKim Phillips 	struct talitos_private *priv = dev_get_drvdata(dev);
6959c4a7965SKim Phillips 
6969c4a7965SKim Phillips 	hwrng_unregister(&priv->rng);
6979c4a7965SKim Phillips }
6989c4a7965SKim Phillips 
6999c4a7965SKim Phillips /*
7009c4a7965SKim Phillips  * crypto alg
7019c4a7965SKim Phillips  */
7029c4a7965SKim Phillips #define TALITOS_CRA_PRIORITY		3000
7039c4a7965SKim Phillips #define TALITOS_MAX_KEY_SIZE		64
7043952f17eSLee Nipper #define TALITOS_MAX_IV_LENGTH		16 /* max of AES_BLOCK_SIZE, DES3_EDE_BLOCK_SIZE */
70570bcaca7SLee Nipper 
706497f2e6bSLee Nipper #define MD5_BLOCK_SIZE    64
7079c4a7965SKim Phillips 
7089c4a7965SKim Phillips struct talitos_ctx {
7099c4a7965SKim Phillips 	struct device *dev;
7109c4a7965SKim Phillips 	__be32 desc_hdr_template;
7119c4a7965SKim Phillips 	u8 key[TALITOS_MAX_KEY_SIZE];
71270bcaca7SLee Nipper 	u8 iv[TALITOS_MAX_IV_LENGTH];
7139c4a7965SKim Phillips 	unsigned int keylen;
7149c4a7965SKim Phillips 	unsigned int enckeylen;
7159c4a7965SKim Phillips 	unsigned int authkeylen;
7169c4a7965SKim Phillips 	unsigned int authsize;
7179c4a7965SKim Phillips };
7189c4a7965SKim Phillips 
719497f2e6bSLee Nipper #define HASH_MAX_BLOCK_SIZE		SHA512_BLOCK_SIZE
720497f2e6bSLee Nipper #define TALITOS_MDEU_MAX_CONTEXT_SIZE	TALITOS_MDEU_CONTEXT_SIZE_SHA384_SHA512
721497f2e6bSLee Nipper 
722497f2e6bSLee Nipper struct talitos_ahash_req_ctx {
72360f208d7SKim Phillips 	u32 hw_context[TALITOS_MDEU_MAX_CONTEXT_SIZE / sizeof(u32)];
724497f2e6bSLee Nipper 	unsigned int hw_context_size;
725497f2e6bSLee Nipper 	u8 buf[HASH_MAX_BLOCK_SIZE];
726497f2e6bSLee Nipper 	u8 bufnext[HASH_MAX_BLOCK_SIZE];
72760f208d7SKim Phillips 	unsigned int swinit;
728497f2e6bSLee Nipper 	unsigned int first;
729497f2e6bSLee Nipper 	unsigned int last;
730497f2e6bSLee Nipper 	unsigned int to_hash_later;
7315e833bc4SLee Nipper 	u64 nbuf;
732497f2e6bSLee Nipper 	struct scatterlist bufsl[2];
733497f2e6bSLee Nipper 	struct scatterlist *psrc;
734497f2e6bSLee Nipper };
735497f2e6bSLee Nipper 
73656af8cd4SLee Nipper static int aead_setauthsize(struct crypto_aead *authenc,
7379c4a7965SKim Phillips 			    unsigned int authsize)
7389c4a7965SKim Phillips {
7399c4a7965SKim Phillips 	struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
7409c4a7965SKim Phillips 
7419c4a7965SKim Phillips 	ctx->authsize = authsize;
7429c4a7965SKim Phillips 
7439c4a7965SKim Phillips 	return 0;
7449c4a7965SKim Phillips }
7459c4a7965SKim Phillips 
74656af8cd4SLee Nipper static int aead_setkey(struct crypto_aead *authenc,
7479c4a7965SKim Phillips 		       const u8 *key, unsigned int keylen)
7489c4a7965SKim Phillips {
7499c4a7965SKim Phillips 	struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
7509c4a7965SKim Phillips 	struct rtattr *rta = (void *)key;
7519c4a7965SKim Phillips 	struct crypto_authenc_key_param *param;
7529c4a7965SKim Phillips 	unsigned int authkeylen;
7539c4a7965SKim Phillips 	unsigned int enckeylen;
7549c4a7965SKim Phillips 
7559c4a7965SKim Phillips 	if (!RTA_OK(rta, keylen))
7569c4a7965SKim Phillips 		goto badkey;
7579c4a7965SKim Phillips 
7589c4a7965SKim Phillips 	if (rta->rta_type != CRYPTO_AUTHENC_KEYA_PARAM)
7599c4a7965SKim Phillips 		goto badkey;
7609c4a7965SKim Phillips 
7619c4a7965SKim Phillips 	if (RTA_PAYLOAD(rta) < sizeof(*param))
7629c4a7965SKim Phillips 		goto badkey;
7639c4a7965SKim Phillips 
7649c4a7965SKim Phillips 	param = RTA_DATA(rta);
7659c4a7965SKim Phillips 	enckeylen = be32_to_cpu(param->enckeylen);
7669c4a7965SKim Phillips 
7679c4a7965SKim Phillips 	key += RTA_ALIGN(rta->rta_len);
7689c4a7965SKim Phillips 	keylen -= RTA_ALIGN(rta->rta_len);
7699c4a7965SKim Phillips 
7709c4a7965SKim Phillips 	if (keylen < enckeylen)
7719c4a7965SKim Phillips 		goto badkey;
7729c4a7965SKim Phillips 
7739c4a7965SKim Phillips 	authkeylen = keylen - enckeylen;
7749c4a7965SKim Phillips 
7759c4a7965SKim Phillips 	if (keylen > TALITOS_MAX_KEY_SIZE)
7769c4a7965SKim Phillips 		goto badkey;
7779c4a7965SKim Phillips 
7789c4a7965SKim Phillips 	memcpy(&ctx->key, key, keylen);
7799c4a7965SKim Phillips 
7809c4a7965SKim Phillips 	ctx->keylen = keylen;
7819c4a7965SKim Phillips 	ctx->enckeylen = enckeylen;
7829c4a7965SKim Phillips 	ctx->authkeylen = authkeylen;
7839c4a7965SKim Phillips 
7849c4a7965SKim Phillips 	return 0;
7859c4a7965SKim Phillips 
7869c4a7965SKim Phillips badkey:
7879c4a7965SKim Phillips 	crypto_aead_set_flags(authenc, CRYPTO_TFM_RES_BAD_KEY_LEN);
7889c4a7965SKim Phillips 	return -EINVAL;
7899c4a7965SKim Phillips }
7909c4a7965SKim Phillips 
7919c4a7965SKim Phillips /*
79256af8cd4SLee Nipper  * talitos_edesc - s/w-extended descriptor
7939c4a7965SKim Phillips  * @src_nents: number of segments in input scatterlist
7949c4a7965SKim Phillips  * @dst_nents: number of segments in output scatterlist
7959c4a7965SKim Phillips  * @dma_len: length of dma mapped link_tbl space
7969c4a7965SKim Phillips  * @dma_link_tbl: bus physical address of link_tbl
7979c4a7965SKim Phillips  * @desc: h/w descriptor
7989c4a7965SKim Phillips  * @link_tbl: input and output h/w link tables (if {src,dst}_nents > 1)
7999c4a7965SKim Phillips  *
8009c4a7965SKim Phillips  * if decrypting (with authcheck), or either one of src_nents or dst_nents
8019c4a7965SKim Phillips  * is greater than 1, an integrity check value is concatenated to the end
8029c4a7965SKim Phillips  * of link_tbl data
8039c4a7965SKim Phillips  */
80456af8cd4SLee Nipper struct talitos_edesc {
8059c4a7965SKim Phillips 	int src_nents;
8069c4a7965SKim Phillips 	int dst_nents;
8074de9d0b5SLee Nipper 	int src_is_chained;
8084de9d0b5SLee Nipper 	int dst_is_chained;
8099c4a7965SKim Phillips 	int dma_len;
8109c4a7965SKim Phillips 	dma_addr_t dma_link_tbl;
8119c4a7965SKim Phillips 	struct talitos_desc desc;
8129c4a7965SKim Phillips 	struct talitos_ptr link_tbl[0];
8139c4a7965SKim Phillips };
8149c4a7965SKim Phillips 
8154de9d0b5SLee Nipper static int talitos_map_sg(struct device *dev, struct scatterlist *sg,
8164de9d0b5SLee Nipper 			  unsigned int nents, enum dma_data_direction dir,
8174de9d0b5SLee Nipper 			  int chained)
8184de9d0b5SLee Nipper {
8194de9d0b5SLee Nipper 	if (unlikely(chained))
8204de9d0b5SLee Nipper 		while (sg) {
8214de9d0b5SLee Nipper 			dma_map_sg(dev, sg, 1, dir);
8224de9d0b5SLee Nipper 			sg = scatterwalk_sg_next(sg);
8234de9d0b5SLee Nipper 		}
8244de9d0b5SLee Nipper 	else
8254de9d0b5SLee Nipper 		dma_map_sg(dev, sg, nents, dir);
8264de9d0b5SLee Nipper 	return nents;
8274de9d0b5SLee Nipper }
8284de9d0b5SLee Nipper 
8294de9d0b5SLee Nipper static void talitos_unmap_sg_chain(struct device *dev, struct scatterlist *sg,
8304de9d0b5SLee Nipper 				   enum dma_data_direction dir)
8314de9d0b5SLee Nipper {
8324de9d0b5SLee Nipper 	while (sg) {
8334de9d0b5SLee Nipper 		dma_unmap_sg(dev, sg, 1, dir);
8344de9d0b5SLee Nipper 		sg = scatterwalk_sg_next(sg);
8354de9d0b5SLee Nipper 	}
8364de9d0b5SLee Nipper }
8374de9d0b5SLee Nipper 
8384de9d0b5SLee Nipper static void talitos_sg_unmap(struct device *dev,
8394de9d0b5SLee Nipper 			     struct talitos_edesc *edesc,
8404de9d0b5SLee Nipper 			     struct scatterlist *src,
8414de9d0b5SLee Nipper 			     struct scatterlist *dst)
8424de9d0b5SLee Nipper {
8434de9d0b5SLee Nipper 	unsigned int src_nents = edesc->src_nents ? : 1;
8444de9d0b5SLee Nipper 	unsigned int dst_nents = edesc->dst_nents ? : 1;
8454de9d0b5SLee Nipper 
8464de9d0b5SLee Nipper 	if (src != dst) {
8474de9d0b5SLee Nipper 		if (edesc->src_is_chained)
8484de9d0b5SLee Nipper 			talitos_unmap_sg_chain(dev, src, DMA_TO_DEVICE);
8494de9d0b5SLee Nipper 		else
8504de9d0b5SLee Nipper 			dma_unmap_sg(dev, src, src_nents, DMA_TO_DEVICE);
8514de9d0b5SLee Nipper 
852497f2e6bSLee Nipper 		if (dst) {
8534de9d0b5SLee Nipper 			if (edesc->dst_is_chained)
854497f2e6bSLee Nipper 				talitos_unmap_sg_chain(dev, dst,
855497f2e6bSLee Nipper 						       DMA_FROM_DEVICE);
8564de9d0b5SLee Nipper 			else
857497f2e6bSLee Nipper 				dma_unmap_sg(dev, dst, dst_nents,
858497f2e6bSLee Nipper 					     DMA_FROM_DEVICE);
859497f2e6bSLee Nipper 		}
8604de9d0b5SLee Nipper 	} else
8614de9d0b5SLee Nipper 		if (edesc->src_is_chained)
8624de9d0b5SLee Nipper 			talitos_unmap_sg_chain(dev, src, DMA_BIDIRECTIONAL);
8634de9d0b5SLee Nipper 		else
8644de9d0b5SLee Nipper 			dma_unmap_sg(dev, src, src_nents, DMA_BIDIRECTIONAL);
8654de9d0b5SLee Nipper }
8664de9d0b5SLee Nipper 
8679c4a7965SKim Phillips static void ipsec_esp_unmap(struct device *dev,
86856af8cd4SLee Nipper 			    struct talitos_edesc *edesc,
8699c4a7965SKim Phillips 			    struct aead_request *areq)
8709c4a7965SKim Phillips {
8719c4a7965SKim Phillips 	unmap_single_talitos_ptr(dev, &edesc->desc.ptr[6], DMA_FROM_DEVICE);
8729c4a7965SKim Phillips 	unmap_single_talitos_ptr(dev, &edesc->desc.ptr[3], DMA_TO_DEVICE);
8739c4a7965SKim Phillips 	unmap_single_talitos_ptr(dev, &edesc->desc.ptr[2], DMA_TO_DEVICE);
8749c4a7965SKim Phillips 	unmap_single_talitos_ptr(dev, &edesc->desc.ptr[0], DMA_TO_DEVICE);
8759c4a7965SKim Phillips 
8769c4a7965SKim Phillips 	dma_unmap_sg(dev, areq->assoc, 1, DMA_TO_DEVICE);
8779c4a7965SKim Phillips 
8784de9d0b5SLee Nipper 	talitos_sg_unmap(dev, edesc, areq->src, areq->dst);
8799c4a7965SKim Phillips 
8809c4a7965SKim Phillips 	if (edesc->dma_len)
8819c4a7965SKim Phillips 		dma_unmap_single(dev, edesc->dma_link_tbl, edesc->dma_len,
8829c4a7965SKim Phillips 				 DMA_BIDIRECTIONAL);
8839c4a7965SKim Phillips }
8849c4a7965SKim Phillips 
8859c4a7965SKim Phillips /*
8869c4a7965SKim Phillips  * ipsec_esp descriptor callbacks
8879c4a7965SKim Phillips  */
8889c4a7965SKim Phillips static void ipsec_esp_encrypt_done(struct device *dev,
8899c4a7965SKim Phillips 				   struct talitos_desc *desc, void *context,
8909c4a7965SKim Phillips 				   int err)
8919c4a7965SKim Phillips {
8929c4a7965SKim Phillips 	struct aead_request *areq = context;
8939c4a7965SKim Phillips 	struct crypto_aead *authenc = crypto_aead_reqtfm(areq);
8949c4a7965SKim Phillips 	struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
89519bbbc63SKim Phillips 	struct talitos_edesc *edesc;
8969c4a7965SKim Phillips 	struct scatterlist *sg;
8979c4a7965SKim Phillips 	void *icvdata;
8989c4a7965SKim Phillips 
89919bbbc63SKim Phillips 	edesc = container_of(desc, struct talitos_edesc, desc);
90019bbbc63SKim Phillips 
9019c4a7965SKim Phillips 	ipsec_esp_unmap(dev, edesc, areq);
9029c4a7965SKim Phillips 
9039c4a7965SKim Phillips 	/* copy the generated ICV to dst */
9049c4a7965SKim Phillips 	if (edesc->dma_len) {
9059c4a7965SKim Phillips 		icvdata = &edesc->link_tbl[edesc->src_nents +
906f3c85bc1SLee Nipper 					   edesc->dst_nents + 2];
9079c4a7965SKim Phillips 		sg = sg_last(areq->dst, edesc->dst_nents);
9089c4a7965SKim Phillips 		memcpy((char *)sg_virt(sg) + sg->length - ctx->authsize,
9099c4a7965SKim Phillips 		       icvdata, ctx->authsize);
9109c4a7965SKim Phillips 	}
9119c4a7965SKim Phillips 
9129c4a7965SKim Phillips 	kfree(edesc);
9139c4a7965SKim Phillips 
9149c4a7965SKim Phillips 	aead_request_complete(areq, err);
9159c4a7965SKim Phillips }
9169c4a7965SKim Phillips 
917fe5720e2SKim Phillips static void ipsec_esp_decrypt_swauth_done(struct device *dev,
918e938e465SKim Phillips 					  struct talitos_desc *desc,
919e938e465SKim Phillips 					  void *context, int err)
9209c4a7965SKim Phillips {
9219c4a7965SKim Phillips 	struct aead_request *req = context;
9229c4a7965SKim Phillips 	struct crypto_aead *authenc = crypto_aead_reqtfm(req);
9239c4a7965SKim Phillips 	struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
92419bbbc63SKim Phillips 	struct talitos_edesc *edesc;
9259c4a7965SKim Phillips 	struct scatterlist *sg;
9269c4a7965SKim Phillips 	void *icvdata;
9279c4a7965SKim Phillips 
92819bbbc63SKim Phillips 	edesc = container_of(desc, struct talitos_edesc, desc);
92919bbbc63SKim Phillips 
9309c4a7965SKim Phillips 	ipsec_esp_unmap(dev, edesc, req);
9319c4a7965SKim Phillips 
9329c4a7965SKim Phillips 	if (!err) {
9339c4a7965SKim Phillips 		/* auth check */
9349c4a7965SKim Phillips 		if (edesc->dma_len)
9359c4a7965SKim Phillips 			icvdata = &edesc->link_tbl[edesc->src_nents +
936f3c85bc1SLee Nipper 						   edesc->dst_nents + 2];
9379c4a7965SKim Phillips 		else
9389c4a7965SKim Phillips 			icvdata = &edesc->link_tbl[0];
9399c4a7965SKim Phillips 
9409c4a7965SKim Phillips 		sg = sg_last(req->dst, edesc->dst_nents ? : 1);
9419c4a7965SKim Phillips 		err = memcmp(icvdata, (char *)sg_virt(sg) + sg->length -
9429c4a7965SKim Phillips 			     ctx->authsize, ctx->authsize) ? -EBADMSG : 0;
9439c4a7965SKim Phillips 	}
9449c4a7965SKim Phillips 
9459c4a7965SKim Phillips 	kfree(edesc);
9469c4a7965SKim Phillips 
9479c4a7965SKim Phillips 	aead_request_complete(req, err);
9489c4a7965SKim Phillips }
9499c4a7965SKim Phillips 
950fe5720e2SKim Phillips static void ipsec_esp_decrypt_hwauth_done(struct device *dev,
951e938e465SKim Phillips 					  struct talitos_desc *desc,
952e938e465SKim Phillips 					  void *context, int err)
953fe5720e2SKim Phillips {
954fe5720e2SKim Phillips 	struct aead_request *req = context;
95519bbbc63SKim Phillips 	struct talitos_edesc *edesc;
95619bbbc63SKim Phillips 
95719bbbc63SKim Phillips 	edesc = container_of(desc, struct talitos_edesc, desc);
958fe5720e2SKim Phillips 
959fe5720e2SKim Phillips 	ipsec_esp_unmap(dev, edesc, req);
960fe5720e2SKim Phillips 
961fe5720e2SKim Phillips 	/* check ICV auth status */
962e938e465SKim Phillips 	if (!err && ((desc->hdr_lo & DESC_HDR_LO_ICCR1_MASK) !=
963e938e465SKim Phillips 		     DESC_HDR_LO_ICCR1_PASS))
964fe5720e2SKim Phillips 		err = -EBADMSG;
965fe5720e2SKim Phillips 
966fe5720e2SKim Phillips 	kfree(edesc);
967fe5720e2SKim Phillips 
968fe5720e2SKim Phillips 	aead_request_complete(req, err);
969fe5720e2SKim Phillips }
970fe5720e2SKim Phillips 
9719c4a7965SKim Phillips /*
9729c4a7965SKim Phillips  * convert scatterlist to SEC h/w link table format
9739c4a7965SKim Phillips  * stop at cryptlen bytes
9749c4a7965SKim Phillips  */
97570bcaca7SLee Nipper static int sg_to_link_tbl(struct scatterlist *sg, int sg_count,
9769c4a7965SKim Phillips 			   int cryptlen, struct talitos_ptr *link_tbl_ptr)
9779c4a7965SKim Phillips {
97870bcaca7SLee Nipper 	int n_sg = sg_count;
97970bcaca7SLee Nipper 
98070bcaca7SLee Nipper 	while (n_sg--) {
98181eb024cSKim Phillips 		to_talitos_ptr(link_tbl_ptr, sg_dma_address(sg));
9829c4a7965SKim Phillips 		link_tbl_ptr->len = cpu_to_be16(sg_dma_len(sg));
9839c4a7965SKim Phillips 		link_tbl_ptr->j_extent = 0;
9849c4a7965SKim Phillips 		link_tbl_ptr++;
9859c4a7965SKim Phillips 		cryptlen -= sg_dma_len(sg);
9864de9d0b5SLee Nipper 		sg = scatterwalk_sg_next(sg);
9879c4a7965SKim Phillips 	}
9889c4a7965SKim Phillips 
98970bcaca7SLee Nipper 	/* adjust (decrease) last one (or two) entry's len to cryptlen */
9909c4a7965SKim Phillips 	link_tbl_ptr--;
991c0e741d4SKim Phillips 	while (be16_to_cpu(link_tbl_ptr->len) <= (-cryptlen)) {
99270bcaca7SLee Nipper 		/* Empty this entry, and move to previous one */
99370bcaca7SLee Nipper 		cryptlen += be16_to_cpu(link_tbl_ptr->len);
99470bcaca7SLee Nipper 		link_tbl_ptr->len = 0;
99570bcaca7SLee Nipper 		sg_count--;
99670bcaca7SLee Nipper 		link_tbl_ptr--;
99770bcaca7SLee Nipper 	}
9989c4a7965SKim Phillips 	link_tbl_ptr->len = cpu_to_be16(be16_to_cpu(link_tbl_ptr->len)
9999c4a7965SKim Phillips 					+ cryptlen);
10009c4a7965SKim Phillips 
10019c4a7965SKim Phillips 	/* tag end of link table */
10029c4a7965SKim Phillips 	link_tbl_ptr->j_extent = DESC_PTR_LNKTBL_RETURN;
100370bcaca7SLee Nipper 
100470bcaca7SLee Nipper 	return sg_count;
10059c4a7965SKim Phillips }
10069c4a7965SKim Phillips 
10079c4a7965SKim Phillips /*
10089c4a7965SKim Phillips  * fill in and submit ipsec_esp descriptor
10099c4a7965SKim Phillips  */
101056af8cd4SLee Nipper static int ipsec_esp(struct talitos_edesc *edesc, struct aead_request *areq,
10119c4a7965SKim Phillips 		     u8 *giv, u64 seq,
10129c4a7965SKim Phillips 		     void (*callback) (struct device *dev,
10139c4a7965SKim Phillips 				       struct talitos_desc *desc,
10149c4a7965SKim Phillips 				       void *context, int error))
10159c4a7965SKim Phillips {
10169c4a7965SKim Phillips 	struct crypto_aead *aead = crypto_aead_reqtfm(areq);
10179c4a7965SKim Phillips 	struct talitos_ctx *ctx = crypto_aead_ctx(aead);
10189c4a7965SKim Phillips 	struct device *dev = ctx->dev;
10199c4a7965SKim Phillips 	struct talitos_desc *desc = &edesc->desc;
10209c4a7965SKim Phillips 	unsigned int cryptlen = areq->cryptlen;
10219c4a7965SKim Phillips 	unsigned int authsize = ctx->authsize;
1022e41256f1SKim Phillips 	unsigned int ivsize = crypto_aead_ivsize(aead);
1023fa86a267SKim Phillips 	int sg_count, ret;
1024fe5720e2SKim Phillips 	int sg_link_tbl_len;
10259c4a7965SKim Phillips 
10269c4a7965SKim Phillips 	/* hmac key */
10279c4a7965SKim Phillips 	map_single_talitos_ptr(dev, &desc->ptr[0], ctx->authkeylen, &ctx->key,
10289c4a7965SKim Phillips 			       0, DMA_TO_DEVICE);
10299c4a7965SKim Phillips 	/* hmac data */
1030e41256f1SKim Phillips 	map_single_talitos_ptr(dev, &desc->ptr[1], areq->assoclen + ivsize,
1031e41256f1SKim Phillips 			       sg_virt(areq->assoc), 0, DMA_TO_DEVICE);
10329c4a7965SKim Phillips 	/* cipher iv */
10339c4a7965SKim Phillips 	map_single_talitos_ptr(dev, &desc->ptr[2], ivsize, giv ?: areq->iv, 0,
10349c4a7965SKim Phillips 			       DMA_TO_DEVICE);
10359c4a7965SKim Phillips 
10369c4a7965SKim Phillips 	/* cipher key */
10379c4a7965SKim Phillips 	map_single_talitos_ptr(dev, &desc->ptr[3], ctx->enckeylen,
10389c4a7965SKim Phillips 			       (char *)&ctx->key + ctx->authkeylen, 0,
10399c4a7965SKim Phillips 			       DMA_TO_DEVICE);
10409c4a7965SKim Phillips 
10419c4a7965SKim Phillips 	/*
10429c4a7965SKim Phillips 	 * cipher in
10439c4a7965SKim Phillips 	 * map and adjust cipher len to aead request cryptlen.
10449c4a7965SKim Phillips 	 * extent is bytes of HMAC postpended to ciphertext,
10459c4a7965SKim Phillips 	 * typically 12 for ipsec
10469c4a7965SKim Phillips 	 */
10479c4a7965SKim Phillips 	desc->ptr[4].len = cpu_to_be16(cryptlen);
10489c4a7965SKim Phillips 	desc->ptr[4].j_extent = authsize;
10499c4a7965SKim Phillips 
1050e938e465SKim Phillips 	sg_count = talitos_map_sg(dev, areq->src, edesc->src_nents ? : 1,
1051e938e465SKim Phillips 				  (areq->src == areq->dst) ? DMA_BIDIRECTIONAL
1052e938e465SKim Phillips 							   : DMA_TO_DEVICE,
10534de9d0b5SLee Nipper 				  edesc->src_is_chained);
10549c4a7965SKim Phillips 
10559c4a7965SKim Phillips 	if (sg_count == 1) {
105681eb024cSKim Phillips 		to_talitos_ptr(&desc->ptr[4], sg_dma_address(areq->src));
10579c4a7965SKim Phillips 	} else {
1058fe5720e2SKim Phillips 		sg_link_tbl_len = cryptlen;
1059fe5720e2SKim Phillips 
1060962a9c99SKim Phillips 		if (edesc->desc.hdr & DESC_HDR_MODE1_MDEU_CICV)
1061fe5720e2SKim Phillips 			sg_link_tbl_len = cryptlen + authsize;
1062e938e465SKim Phillips 
1063fe5720e2SKim Phillips 		sg_count = sg_to_link_tbl(areq->src, sg_count, sg_link_tbl_len,
10649c4a7965SKim Phillips 					  &edesc->link_tbl[0]);
106570bcaca7SLee Nipper 		if (sg_count > 1) {
10669c4a7965SKim Phillips 			desc->ptr[4].j_extent |= DESC_PTR_LNKTBL_JUMP;
106781eb024cSKim Phillips 			to_talitos_ptr(&desc->ptr[4], edesc->dma_link_tbl);
1068e938e465SKim Phillips 			dma_sync_single_for_device(dev, edesc->dma_link_tbl,
1069e938e465SKim Phillips 						   edesc->dma_len,
1070e938e465SKim Phillips 						   DMA_BIDIRECTIONAL);
107170bcaca7SLee Nipper 		} else {
107270bcaca7SLee Nipper 			/* Only one segment now, so no link tbl needed */
107381eb024cSKim Phillips 			to_talitos_ptr(&desc->ptr[4],
107481eb024cSKim Phillips 				       sg_dma_address(areq->src));
107570bcaca7SLee Nipper 		}
10769c4a7965SKim Phillips 	}
10779c4a7965SKim Phillips 
10789c4a7965SKim Phillips 	/* cipher out */
10799c4a7965SKim Phillips 	desc->ptr[5].len = cpu_to_be16(cryptlen);
10809c4a7965SKim Phillips 	desc->ptr[5].j_extent = authsize;
10819c4a7965SKim Phillips 
1082e938e465SKim Phillips 	if (areq->src != areq->dst)
10834de9d0b5SLee Nipper 		sg_count = talitos_map_sg(dev, areq->dst,
10844de9d0b5SLee Nipper 					  edesc->dst_nents ? : 1,
10854de9d0b5SLee Nipper 					  DMA_FROM_DEVICE,
10864de9d0b5SLee Nipper 					  edesc->dst_is_chained);
10879c4a7965SKim Phillips 
10889c4a7965SKim Phillips 	if (sg_count == 1) {
108981eb024cSKim Phillips 		to_talitos_ptr(&desc->ptr[5], sg_dma_address(areq->dst));
10909c4a7965SKim Phillips 	} else {
10919c4a7965SKim Phillips 		struct talitos_ptr *link_tbl_ptr =
1092f3c85bc1SLee Nipper 			&edesc->link_tbl[edesc->src_nents + 1];
10939c4a7965SKim Phillips 
109481eb024cSKim Phillips 		to_talitos_ptr(&desc->ptr[5], edesc->dma_link_tbl +
109581eb024cSKim Phillips 			       (edesc->src_nents + 1) *
109681eb024cSKim Phillips 			       sizeof(struct talitos_ptr));
109770bcaca7SLee Nipper 		sg_count = sg_to_link_tbl(areq->dst, sg_count, cryptlen,
10989c4a7965SKim Phillips 					  link_tbl_ptr);
1099fe5720e2SKim Phillips 
1100f3c85bc1SLee Nipper 		/* Add an entry to the link table for ICV data */
11019c4a7965SKim Phillips 		link_tbl_ptr += sg_count - 1;
11029c4a7965SKim Phillips 		link_tbl_ptr->j_extent = 0;
1103f3c85bc1SLee Nipper 		sg_count++;
11049c4a7965SKim Phillips 		link_tbl_ptr++;
11059c4a7965SKim Phillips 		link_tbl_ptr->j_extent = DESC_PTR_LNKTBL_RETURN;
11069c4a7965SKim Phillips 		link_tbl_ptr->len = cpu_to_be16(authsize);
11079c4a7965SKim Phillips 
11089c4a7965SKim Phillips 		/* icv data follows link tables */
110981eb024cSKim Phillips 		to_talitos_ptr(link_tbl_ptr, edesc->dma_link_tbl +
111081eb024cSKim Phillips 			       (edesc->src_nents + edesc->dst_nents + 2) *
111181eb024cSKim Phillips 			       sizeof(struct talitos_ptr));
11129c4a7965SKim Phillips 		desc->ptr[5].j_extent |= DESC_PTR_LNKTBL_JUMP;
11139c4a7965SKim Phillips 		dma_sync_single_for_device(ctx->dev, edesc->dma_link_tbl,
11149c4a7965SKim Phillips 					   edesc->dma_len, DMA_BIDIRECTIONAL);
11159c4a7965SKim Phillips 	}
11169c4a7965SKim Phillips 
11179c4a7965SKim Phillips 	/* iv out */
11189c4a7965SKim Phillips 	map_single_talitos_ptr(dev, &desc->ptr[6], ivsize, ctx->iv, 0,
11199c4a7965SKim Phillips 			       DMA_FROM_DEVICE);
11209c4a7965SKim Phillips 
1121fa86a267SKim Phillips 	ret = talitos_submit(dev, desc, callback, areq);
1122fa86a267SKim Phillips 	if (ret != -EINPROGRESS) {
1123fa86a267SKim Phillips 		ipsec_esp_unmap(dev, edesc, areq);
1124fa86a267SKim Phillips 		kfree(edesc);
1125fa86a267SKim Phillips 	}
1126fa86a267SKim Phillips 	return ret;
11279c4a7965SKim Phillips }
11289c4a7965SKim Phillips 
11299c4a7965SKim Phillips /*
11309c4a7965SKim Phillips  * derive number of elements in scatterlist
11319c4a7965SKim Phillips  */
11324de9d0b5SLee Nipper static int sg_count(struct scatterlist *sg_list, int nbytes, int *chained)
11339c4a7965SKim Phillips {
11349c4a7965SKim Phillips 	struct scatterlist *sg = sg_list;
11359c4a7965SKim Phillips 	int sg_nents = 0;
11369c4a7965SKim Phillips 
11374de9d0b5SLee Nipper 	*chained = 0;
11384de9d0b5SLee Nipper 	while (nbytes > 0) {
11399c4a7965SKim Phillips 		sg_nents++;
11409c4a7965SKim Phillips 		nbytes -= sg->length;
11414de9d0b5SLee Nipper 		if (!sg_is_last(sg) && (sg + 1)->length == 0)
11424de9d0b5SLee Nipper 			*chained = 1;
11434de9d0b5SLee Nipper 		sg = scatterwalk_sg_next(sg);
11449c4a7965SKim Phillips 	}
11459c4a7965SKim Phillips 
11469c4a7965SKim Phillips 	return sg_nents;
11479c4a7965SKim Phillips }
11489c4a7965SKim Phillips 
1149497f2e6bSLee Nipper /**
1150497f2e6bSLee Nipper  * sg_copy_end_to_buffer - Copy end data from SG list to a linear buffer
1151497f2e6bSLee Nipper  * @sgl:		 The SG list
1152497f2e6bSLee Nipper  * @nents:		 Number of SG entries
1153497f2e6bSLee Nipper  * @buf:		 Where to copy to
1154497f2e6bSLee Nipper  * @buflen:		 The number of bytes to copy
1155497f2e6bSLee Nipper  * @skip:		 The number of bytes to skip before copying.
1156497f2e6bSLee Nipper  *                       Note: skip + buflen should equal SG total size.
1157497f2e6bSLee Nipper  *
1158497f2e6bSLee Nipper  * Returns the number of copied bytes.
1159497f2e6bSLee Nipper  *
1160497f2e6bSLee Nipper  **/
1161497f2e6bSLee Nipper static size_t sg_copy_end_to_buffer(struct scatterlist *sgl, unsigned int nents,
1162497f2e6bSLee Nipper 				    void *buf, size_t buflen, unsigned int skip)
1163497f2e6bSLee Nipper {
1164497f2e6bSLee Nipper 	unsigned int offset = 0;
1165497f2e6bSLee Nipper 	unsigned int boffset = 0;
1166497f2e6bSLee Nipper 	struct sg_mapping_iter miter;
1167497f2e6bSLee Nipper 	unsigned long flags;
1168497f2e6bSLee Nipper 	unsigned int sg_flags = SG_MITER_ATOMIC;
1169497f2e6bSLee Nipper 	size_t total_buffer = buflen + skip;
1170497f2e6bSLee Nipper 
1171497f2e6bSLee Nipper 	sg_flags |= SG_MITER_FROM_SG;
1172497f2e6bSLee Nipper 
1173497f2e6bSLee Nipper 	sg_miter_start(&miter, sgl, nents, sg_flags);
1174497f2e6bSLee Nipper 
1175497f2e6bSLee Nipper 	local_irq_save(flags);
1176497f2e6bSLee Nipper 
1177497f2e6bSLee Nipper 	while (sg_miter_next(&miter) && offset < total_buffer) {
1178497f2e6bSLee Nipper 		unsigned int len;
1179497f2e6bSLee Nipper 		unsigned int ignore;
1180497f2e6bSLee Nipper 
1181497f2e6bSLee Nipper 		if ((offset + miter.length) > skip) {
1182497f2e6bSLee Nipper 			if (offset < skip) {
1183497f2e6bSLee Nipper 				/* Copy part of this segment */
1184497f2e6bSLee Nipper 				ignore = skip - offset;
1185497f2e6bSLee Nipper 				len = miter.length - ignore;
11867260042bSLee Nipper 				if (boffset + len > buflen)
11877260042bSLee Nipper 					len = buflen - boffset;
1188497f2e6bSLee Nipper 				memcpy(buf + boffset, miter.addr + ignore, len);
1189497f2e6bSLee Nipper 			} else {
11907260042bSLee Nipper 				/* Copy all of this segment (up to buflen) */
1191497f2e6bSLee Nipper 				len = miter.length;
11927260042bSLee Nipper 				if (boffset + len > buflen)
11937260042bSLee Nipper 					len = buflen - boffset;
1194497f2e6bSLee Nipper 				memcpy(buf + boffset, miter.addr, len);
1195497f2e6bSLee Nipper 			}
1196497f2e6bSLee Nipper 			boffset += len;
1197497f2e6bSLee Nipper 		}
1198497f2e6bSLee Nipper 		offset += miter.length;
1199497f2e6bSLee Nipper 	}
1200497f2e6bSLee Nipper 
1201497f2e6bSLee Nipper 	sg_miter_stop(&miter);
1202497f2e6bSLee Nipper 
1203497f2e6bSLee Nipper 	local_irq_restore(flags);
1204497f2e6bSLee Nipper 	return boffset;
1205497f2e6bSLee Nipper }
1206497f2e6bSLee Nipper 
12079c4a7965SKim Phillips /*
120856af8cd4SLee Nipper  * allocate and map the extended descriptor
12099c4a7965SKim Phillips  */
12104de9d0b5SLee Nipper static struct talitos_edesc *talitos_edesc_alloc(struct device *dev,
12114de9d0b5SLee Nipper 						 struct scatterlist *src,
12124de9d0b5SLee Nipper 						 struct scatterlist *dst,
1213497f2e6bSLee Nipper 						 int hash_result,
12144de9d0b5SLee Nipper 						 unsigned int cryptlen,
12154de9d0b5SLee Nipper 						 unsigned int authsize,
12164de9d0b5SLee Nipper 						 int icv_stashing,
12174de9d0b5SLee Nipper 						 u32 cryptoflags)
12189c4a7965SKim Phillips {
121956af8cd4SLee Nipper 	struct talitos_edesc *edesc;
12209c4a7965SKim Phillips 	int src_nents, dst_nents, alloc_len, dma_len;
12214de9d0b5SLee Nipper 	int src_chained, dst_chained = 0;
12224de9d0b5SLee Nipper 	gfp_t flags = cryptoflags & CRYPTO_TFM_REQ_MAY_SLEEP ? GFP_KERNEL :
1223586725f8SKim Phillips 		      GFP_ATOMIC;
12249c4a7965SKim Phillips 
12254de9d0b5SLee Nipper 	if (cryptlen + authsize > TALITOS_MAX_DATA_LEN) {
12264de9d0b5SLee Nipper 		dev_err(dev, "length exceeds h/w max limit\n");
12279c4a7965SKim Phillips 		return ERR_PTR(-EINVAL);
12289c4a7965SKim Phillips 	}
12299c4a7965SKim Phillips 
12304de9d0b5SLee Nipper 	src_nents = sg_count(src, cryptlen + authsize, &src_chained);
12319c4a7965SKim Phillips 	src_nents = (src_nents == 1) ? 0 : src_nents;
12329c4a7965SKim Phillips 
1233497f2e6bSLee Nipper 	if (hash_result) {
1234497f2e6bSLee Nipper 		dst_nents = 0;
1235497f2e6bSLee Nipper 	} else {
12364de9d0b5SLee Nipper 		if (dst == src) {
12379c4a7965SKim Phillips 			dst_nents = src_nents;
12389c4a7965SKim Phillips 		} else {
1239497f2e6bSLee Nipper 			dst_nents = sg_count(dst, cryptlen + authsize,
1240497f2e6bSLee Nipper 					     &dst_chained);
1241695ad589SLee Nipper 			dst_nents = (dst_nents == 1) ? 0 : dst_nents;
12429c4a7965SKim Phillips 		}
1243497f2e6bSLee Nipper 	}
12449c4a7965SKim Phillips 
12459c4a7965SKim Phillips 	/*
12469c4a7965SKim Phillips 	 * allocate space for base edesc plus the link tables,
1247f3c85bc1SLee Nipper 	 * allowing for two separate entries for ICV and generated ICV (+ 2),
12489c4a7965SKim Phillips 	 * and the ICV data itself
12499c4a7965SKim Phillips 	 */
125056af8cd4SLee Nipper 	alloc_len = sizeof(struct talitos_edesc);
12519c4a7965SKim Phillips 	if (src_nents || dst_nents) {
1252f3c85bc1SLee Nipper 		dma_len = (src_nents + dst_nents + 2) *
12534de9d0b5SLee Nipper 				 sizeof(struct talitos_ptr) + authsize;
12549c4a7965SKim Phillips 		alloc_len += dma_len;
12559c4a7965SKim Phillips 	} else {
12569c4a7965SKim Phillips 		dma_len = 0;
12574de9d0b5SLee Nipper 		alloc_len += icv_stashing ? authsize : 0;
12589c4a7965SKim Phillips 	}
12599c4a7965SKim Phillips 
1260586725f8SKim Phillips 	edesc = kmalloc(alloc_len, GFP_DMA | flags);
12619c4a7965SKim Phillips 	if (!edesc) {
12624de9d0b5SLee Nipper 		dev_err(dev, "could not allocate edescriptor\n");
12639c4a7965SKim Phillips 		return ERR_PTR(-ENOMEM);
12649c4a7965SKim Phillips 	}
12659c4a7965SKim Phillips 
12669c4a7965SKim Phillips 	edesc->src_nents = src_nents;
12679c4a7965SKim Phillips 	edesc->dst_nents = dst_nents;
12684de9d0b5SLee Nipper 	edesc->src_is_chained = src_chained;
12694de9d0b5SLee Nipper 	edesc->dst_is_chained = dst_chained;
12709c4a7965SKim Phillips 	edesc->dma_len = dma_len;
1271497f2e6bSLee Nipper 	if (dma_len)
12724de9d0b5SLee Nipper 		edesc->dma_link_tbl = dma_map_single(dev, &edesc->link_tbl[0],
1273497f2e6bSLee Nipper 						     edesc->dma_len,
1274497f2e6bSLee Nipper 						     DMA_BIDIRECTIONAL);
12759c4a7965SKim Phillips 
12769c4a7965SKim Phillips 	return edesc;
12779c4a7965SKim Phillips }
12789c4a7965SKim Phillips 
12794de9d0b5SLee Nipper static struct talitos_edesc *aead_edesc_alloc(struct aead_request *areq,
12804de9d0b5SLee Nipper 					      int icv_stashing)
12814de9d0b5SLee Nipper {
12824de9d0b5SLee Nipper 	struct crypto_aead *authenc = crypto_aead_reqtfm(areq);
12834de9d0b5SLee Nipper 	struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
12844de9d0b5SLee Nipper 
1285497f2e6bSLee Nipper 	return talitos_edesc_alloc(ctx->dev, areq->src, areq->dst, 0,
12864de9d0b5SLee Nipper 				   areq->cryptlen, ctx->authsize, icv_stashing,
12874de9d0b5SLee Nipper 				   areq->base.flags);
12884de9d0b5SLee Nipper }
12894de9d0b5SLee Nipper 
129056af8cd4SLee Nipper static int aead_encrypt(struct aead_request *req)
12919c4a7965SKim Phillips {
12929c4a7965SKim Phillips 	struct crypto_aead *authenc = crypto_aead_reqtfm(req);
12939c4a7965SKim Phillips 	struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
129456af8cd4SLee Nipper 	struct talitos_edesc *edesc;
12959c4a7965SKim Phillips 
12969c4a7965SKim Phillips 	/* allocate extended descriptor */
12974de9d0b5SLee Nipper 	edesc = aead_edesc_alloc(req, 0);
12989c4a7965SKim Phillips 	if (IS_ERR(edesc))
12999c4a7965SKim Phillips 		return PTR_ERR(edesc);
13009c4a7965SKim Phillips 
13019c4a7965SKim Phillips 	/* set encrypt */
130270bcaca7SLee Nipper 	edesc->desc.hdr = ctx->desc_hdr_template | DESC_HDR_MODE0_ENCRYPT;
13039c4a7965SKim Phillips 
13049c4a7965SKim Phillips 	return ipsec_esp(edesc, req, NULL, 0, ipsec_esp_encrypt_done);
13059c4a7965SKim Phillips }
13069c4a7965SKim Phillips 
130756af8cd4SLee Nipper static int aead_decrypt(struct aead_request *req)
13089c4a7965SKim Phillips {
13099c4a7965SKim Phillips 	struct crypto_aead *authenc = crypto_aead_reqtfm(req);
13109c4a7965SKim Phillips 	struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
13119c4a7965SKim Phillips 	unsigned int authsize = ctx->authsize;
1312fe5720e2SKim Phillips 	struct talitos_private *priv = dev_get_drvdata(ctx->dev);
131356af8cd4SLee Nipper 	struct talitos_edesc *edesc;
13149c4a7965SKim Phillips 	struct scatterlist *sg;
13159c4a7965SKim Phillips 	void *icvdata;
13169c4a7965SKim Phillips 
13179c4a7965SKim Phillips 	req->cryptlen -= authsize;
13189c4a7965SKim Phillips 
13199c4a7965SKim Phillips 	/* allocate extended descriptor */
13204de9d0b5SLee Nipper 	edesc = aead_edesc_alloc(req, 1);
13219c4a7965SKim Phillips 	if (IS_ERR(edesc))
13229c4a7965SKim Phillips 		return PTR_ERR(edesc);
13239c4a7965SKim Phillips 
1324fe5720e2SKim Phillips 	if ((priv->features & TALITOS_FTR_HW_AUTH_CHECK) &&
1325e938e465SKim Phillips 	    ((!edesc->src_nents && !edesc->dst_nents) ||
1326e938e465SKim Phillips 	     priv->features & TALITOS_FTR_SRC_LINK_TBL_LEN_INCLUDES_EXTENT)) {
1327fe5720e2SKim Phillips 
1328fe5720e2SKim Phillips 		/* decrypt and check the ICV */
1329e938e465SKim Phillips 		edesc->desc.hdr = ctx->desc_hdr_template |
1330e938e465SKim Phillips 				  DESC_HDR_DIR_INBOUND |
1331fe5720e2SKim Phillips 				  DESC_HDR_MODE1_MDEU_CICV;
1332fe5720e2SKim Phillips 
1333fe5720e2SKim Phillips 		/* reset integrity check result bits */
1334fe5720e2SKim Phillips 		edesc->desc.hdr_lo = 0;
1335fe5720e2SKim Phillips 
1336e938e465SKim Phillips 		return ipsec_esp(edesc, req, NULL, 0,
1337e938e465SKim Phillips 				 ipsec_esp_decrypt_hwauth_done);
1338fe5720e2SKim Phillips 
1339e938e465SKim Phillips 	}
1340fe5720e2SKim Phillips 
1341fe5720e2SKim Phillips 	/* Have to check the ICV with software */
1342fe5720e2SKim Phillips 	edesc->desc.hdr = ctx->desc_hdr_template | DESC_HDR_DIR_INBOUND;
1343fe5720e2SKim Phillips 
13449c4a7965SKim Phillips 	/* stash incoming ICV for later cmp with ICV generated by the h/w */
13459c4a7965SKim Phillips 	if (edesc->dma_len)
13469c4a7965SKim Phillips 		icvdata = &edesc->link_tbl[edesc->src_nents +
1347f3c85bc1SLee Nipper 					   edesc->dst_nents + 2];
13489c4a7965SKim Phillips 	else
13499c4a7965SKim Phillips 		icvdata = &edesc->link_tbl[0];
13509c4a7965SKim Phillips 
13519c4a7965SKim Phillips 	sg = sg_last(req->src, edesc->src_nents ? : 1);
13529c4a7965SKim Phillips 
13539c4a7965SKim Phillips 	memcpy(icvdata, (char *)sg_virt(sg) + sg->length - ctx->authsize,
13549c4a7965SKim Phillips 	       ctx->authsize);
13559c4a7965SKim Phillips 
1356fe5720e2SKim Phillips 	return ipsec_esp(edesc, req, NULL, 0, ipsec_esp_decrypt_swauth_done);
1357fe5720e2SKim Phillips }
13589c4a7965SKim Phillips 
135956af8cd4SLee Nipper static int aead_givencrypt(struct aead_givcrypt_request *req)
13609c4a7965SKim Phillips {
13619c4a7965SKim Phillips 	struct aead_request *areq = &req->areq;
13629c4a7965SKim Phillips 	struct crypto_aead *authenc = crypto_aead_reqtfm(areq);
13639c4a7965SKim Phillips 	struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
136456af8cd4SLee Nipper 	struct talitos_edesc *edesc;
13659c4a7965SKim Phillips 
13669c4a7965SKim Phillips 	/* allocate extended descriptor */
13674de9d0b5SLee Nipper 	edesc = aead_edesc_alloc(areq, 0);
13689c4a7965SKim Phillips 	if (IS_ERR(edesc))
13699c4a7965SKim Phillips 		return PTR_ERR(edesc);
13709c4a7965SKim Phillips 
13719c4a7965SKim Phillips 	/* set encrypt */
137270bcaca7SLee Nipper 	edesc->desc.hdr = ctx->desc_hdr_template | DESC_HDR_MODE0_ENCRYPT;
13739c4a7965SKim Phillips 
13749c4a7965SKim Phillips 	memcpy(req->giv, ctx->iv, crypto_aead_ivsize(authenc));
1375ba95487dSKim Phillips 	/* avoid consecutive packets going out with same IV */
1376ba95487dSKim Phillips 	*(__be64 *)req->giv ^= cpu_to_be64(req->seq);
13779c4a7965SKim Phillips 
13789c4a7965SKim Phillips 	return ipsec_esp(edesc, areq, req->giv, req->seq,
13799c4a7965SKim Phillips 			 ipsec_esp_encrypt_done);
13809c4a7965SKim Phillips }
13819c4a7965SKim Phillips 
13824de9d0b5SLee Nipper static int ablkcipher_setkey(struct crypto_ablkcipher *cipher,
13834de9d0b5SLee Nipper 			     const u8 *key, unsigned int keylen)
13844de9d0b5SLee Nipper {
13854de9d0b5SLee Nipper 	struct talitos_ctx *ctx = crypto_ablkcipher_ctx(cipher);
13864de9d0b5SLee Nipper 	struct ablkcipher_alg *alg = crypto_ablkcipher_alg(cipher);
13874de9d0b5SLee Nipper 
13884de9d0b5SLee Nipper 	if (keylen > TALITOS_MAX_KEY_SIZE)
13894de9d0b5SLee Nipper 		goto badkey;
13904de9d0b5SLee Nipper 
13914de9d0b5SLee Nipper 	if (keylen < alg->min_keysize || keylen > alg->max_keysize)
13924de9d0b5SLee Nipper 		goto badkey;
13934de9d0b5SLee Nipper 
13944de9d0b5SLee Nipper 	memcpy(&ctx->key, key, keylen);
13954de9d0b5SLee Nipper 	ctx->keylen = keylen;
13964de9d0b5SLee Nipper 
13974de9d0b5SLee Nipper 	return 0;
13984de9d0b5SLee Nipper 
13994de9d0b5SLee Nipper badkey:
14004de9d0b5SLee Nipper 	crypto_ablkcipher_set_flags(cipher, CRYPTO_TFM_RES_BAD_KEY_LEN);
14014de9d0b5SLee Nipper 	return -EINVAL;
14024de9d0b5SLee Nipper }
14034de9d0b5SLee Nipper 
14044de9d0b5SLee Nipper static void common_nonsnoop_unmap(struct device *dev,
14054de9d0b5SLee Nipper 				  struct talitos_edesc *edesc,
14064de9d0b5SLee Nipper 				  struct ablkcipher_request *areq)
14074de9d0b5SLee Nipper {
14084de9d0b5SLee Nipper 	unmap_single_talitos_ptr(dev, &edesc->desc.ptr[5], DMA_FROM_DEVICE);
14094de9d0b5SLee Nipper 	unmap_single_talitos_ptr(dev, &edesc->desc.ptr[2], DMA_TO_DEVICE);
14104de9d0b5SLee Nipper 	unmap_single_talitos_ptr(dev, &edesc->desc.ptr[1], DMA_TO_DEVICE);
14114de9d0b5SLee Nipper 
14124de9d0b5SLee Nipper 	talitos_sg_unmap(dev, edesc, areq->src, areq->dst);
14134de9d0b5SLee Nipper 
14144de9d0b5SLee Nipper 	if (edesc->dma_len)
14154de9d0b5SLee Nipper 		dma_unmap_single(dev, edesc->dma_link_tbl, edesc->dma_len,
14164de9d0b5SLee Nipper 				 DMA_BIDIRECTIONAL);
14174de9d0b5SLee Nipper }
14184de9d0b5SLee Nipper 
14194de9d0b5SLee Nipper static void ablkcipher_done(struct device *dev,
14204de9d0b5SLee Nipper 			    struct talitos_desc *desc, void *context,
14214de9d0b5SLee Nipper 			    int err)
14224de9d0b5SLee Nipper {
14234de9d0b5SLee Nipper 	struct ablkcipher_request *areq = context;
142419bbbc63SKim Phillips 	struct talitos_edesc *edesc;
142519bbbc63SKim Phillips 
142619bbbc63SKim Phillips 	edesc = container_of(desc, struct talitos_edesc, desc);
14274de9d0b5SLee Nipper 
14284de9d0b5SLee Nipper 	common_nonsnoop_unmap(dev, edesc, areq);
14294de9d0b5SLee Nipper 
14304de9d0b5SLee Nipper 	kfree(edesc);
14314de9d0b5SLee Nipper 
14324de9d0b5SLee Nipper 	areq->base.complete(&areq->base, err);
14334de9d0b5SLee Nipper }
14344de9d0b5SLee Nipper 
14354de9d0b5SLee Nipper static int common_nonsnoop(struct talitos_edesc *edesc,
14364de9d0b5SLee Nipper 			   struct ablkcipher_request *areq,
14374de9d0b5SLee Nipper 			   u8 *giv,
14384de9d0b5SLee Nipper 			   void (*callback) (struct device *dev,
14394de9d0b5SLee Nipper 					     struct talitos_desc *desc,
14404de9d0b5SLee Nipper 					     void *context, int error))
14414de9d0b5SLee Nipper {
14424de9d0b5SLee Nipper 	struct crypto_ablkcipher *cipher = crypto_ablkcipher_reqtfm(areq);
14434de9d0b5SLee Nipper 	struct talitos_ctx *ctx = crypto_ablkcipher_ctx(cipher);
14444de9d0b5SLee Nipper 	struct device *dev = ctx->dev;
14454de9d0b5SLee Nipper 	struct talitos_desc *desc = &edesc->desc;
14464de9d0b5SLee Nipper 	unsigned int cryptlen = areq->nbytes;
14474de9d0b5SLee Nipper 	unsigned int ivsize;
14484de9d0b5SLee Nipper 	int sg_count, ret;
14494de9d0b5SLee Nipper 
14504de9d0b5SLee Nipper 	/* first DWORD empty */
14514de9d0b5SLee Nipper 	desc->ptr[0].len = 0;
145281eb024cSKim Phillips 	to_talitos_ptr(&desc->ptr[0], 0);
14534de9d0b5SLee Nipper 	desc->ptr[0].j_extent = 0;
14544de9d0b5SLee Nipper 
14554de9d0b5SLee Nipper 	/* cipher iv */
14564de9d0b5SLee Nipper 	ivsize = crypto_ablkcipher_ivsize(cipher);
14574de9d0b5SLee Nipper 	map_single_talitos_ptr(dev, &desc->ptr[1], ivsize, giv ?: areq->info, 0,
14584de9d0b5SLee Nipper 			       DMA_TO_DEVICE);
14594de9d0b5SLee Nipper 
14604de9d0b5SLee Nipper 	/* cipher key */
14614de9d0b5SLee Nipper 	map_single_talitos_ptr(dev, &desc->ptr[2], ctx->keylen,
14624de9d0b5SLee Nipper 			       (char *)&ctx->key, 0, DMA_TO_DEVICE);
14634de9d0b5SLee Nipper 
14644de9d0b5SLee Nipper 	/*
14654de9d0b5SLee Nipper 	 * cipher in
14664de9d0b5SLee Nipper 	 */
14674de9d0b5SLee Nipper 	desc->ptr[3].len = cpu_to_be16(cryptlen);
14684de9d0b5SLee Nipper 	desc->ptr[3].j_extent = 0;
14694de9d0b5SLee Nipper 
14704de9d0b5SLee Nipper 	sg_count = talitos_map_sg(dev, areq->src, edesc->src_nents ? : 1,
14714de9d0b5SLee Nipper 				  (areq->src == areq->dst) ? DMA_BIDIRECTIONAL
14724de9d0b5SLee Nipper 							   : DMA_TO_DEVICE,
14734de9d0b5SLee Nipper 				  edesc->src_is_chained);
14744de9d0b5SLee Nipper 
14754de9d0b5SLee Nipper 	if (sg_count == 1) {
147681eb024cSKim Phillips 		to_talitos_ptr(&desc->ptr[3], sg_dma_address(areq->src));
14774de9d0b5SLee Nipper 	} else {
14784de9d0b5SLee Nipper 		sg_count = sg_to_link_tbl(areq->src, sg_count, cryptlen,
14794de9d0b5SLee Nipper 					  &edesc->link_tbl[0]);
14804de9d0b5SLee Nipper 		if (sg_count > 1) {
148181eb024cSKim Phillips 			to_talitos_ptr(&desc->ptr[3], edesc->dma_link_tbl);
14824de9d0b5SLee Nipper 			desc->ptr[3].j_extent |= DESC_PTR_LNKTBL_JUMP;
1483e938e465SKim Phillips 			dma_sync_single_for_device(dev, edesc->dma_link_tbl,
1484e938e465SKim Phillips 						   edesc->dma_len,
1485e938e465SKim Phillips 						   DMA_BIDIRECTIONAL);
14864de9d0b5SLee Nipper 		} else {
14874de9d0b5SLee Nipper 			/* Only one segment now, so no link tbl needed */
148881eb024cSKim Phillips 			to_talitos_ptr(&desc->ptr[3],
148981eb024cSKim Phillips 				       sg_dma_address(areq->src));
14904de9d0b5SLee Nipper 		}
14914de9d0b5SLee Nipper 	}
14924de9d0b5SLee Nipper 
14934de9d0b5SLee Nipper 	/* cipher out */
14944de9d0b5SLee Nipper 	desc->ptr[4].len = cpu_to_be16(cryptlen);
14954de9d0b5SLee Nipper 	desc->ptr[4].j_extent = 0;
14964de9d0b5SLee Nipper 
14974de9d0b5SLee Nipper 	if (areq->src != areq->dst)
14984de9d0b5SLee Nipper 		sg_count = talitos_map_sg(dev, areq->dst,
14994de9d0b5SLee Nipper 					  edesc->dst_nents ? : 1,
15004de9d0b5SLee Nipper 					  DMA_FROM_DEVICE,
15014de9d0b5SLee Nipper 					  edesc->dst_is_chained);
15024de9d0b5SLee Nipper 
15034de9d0b5SLee Nipper 	if (sg_count == 1) {
150481eb024cSKim Phillips 		to_talitos_ptr(&desc->ptr[4], sg_dma_address(areq->dst));
15054de9d0b5SLee Nipper 	} else {
15064de9d0b5SLee Nipper 		struct talitos_ptr *link_tbl_ptr =
15074de9d0b5SLee Nipper 			&edesc->link_tbl[edesc->src_nents + 1];
15084de9d0b5SLee Nipper 
150981eb024cSKim Phillips 		to_talitos_ptr(&desc->ptr[4], edesc->dma_link_tbl +
151081eb024cSKim Phillips 					      (edesc->src_nents + 1) *
151181eb024cSKim Phillips 					      sizeof(struct talitos_ptr));
15124de9d0b5SLee Nipper 		desc->ptr[4].j_extent |= DESC_PTR_LNKTBL_JUMP;
15134de9d0b5SLee Nipper 		sg_count = sg_to_link_tbl(areq->dst, sg_count, cryptlen,
15144de9d0b5SLee Nipper 					  link_tbl_ptr);
15154de9d0b5SLee Nipper 		dma_sync_single_for_device(ctx->dev, edesc->dma_link_tbl,
15164de9d0b5SLee Nipper 					   edesc->dma_len, DMA_BIDIRECTIONAL);
15174de9d0b5SLee Nipper 	}
15184de9d0b5SLee Nipper 
15194de9d0b5SLee Nipper 	/* iv out */
15204de9d0b5SLee Nipper 	map_single_talitos_ptr(dev, &desc->ptr[5], ivsize, ctx->iv, 0,
15214de9d0b5SLee Nipper 			       DMA_FROM_DEVICE);
15224de9d0b5SLee Nipper 
15234de9d0b5SLee Nipper 	/* last DWORD empty */
15244de9d0b5SLee Nipper 	desc->ptr[6].len = 0;
152581eb024cSKim Phillips 	to_talitos_ptr(&desc->ptr[6], 0);
15264de9d0b5SLee Nipper 	desc->ptr[6].j_extent = 0;
15274de9d0b5SLee Nipper 
15284de9d0b5SLee Nipper 	ret = talitos_submit(dev, desc, callback, areq);
15294de9d0b5SLee Nipper 	if (ret != -EINPROGRESS) {
15304de9d0b5SLee Nipper 		common_nonsnoop_unmap(dev, edesc, areq);
15314de9d0b5SLee Nipper 		kfree(edesc);
15324de9d0b5SLee Nipper 	}
15334de9d0b5SLee Nipper 	return ret;
15344de9d0b5SLee Nipper }
15354de9d0b5SLee Nipper 
1536e938e465SKim Phillips static struct talitos_edesc *ablkcipher_edesc_alloc(struct ablkcipher_request *
1537e938e465SKim Phillips 						    areq)
15384de9d0b5SLee Nipper {
15394de9d0b5SLee Nipper 	struct crypto_ablkcipher *cipher = crypto_ablkcipher_reqtfm(areq);
15404de9d0b5SLee Nipper 	struct talitos_ctx *ctx = crypto_ablkcipher_ctx(cipher);
15414de9d0b5SLee Nipper 
1542497f2e6bSLee Nipper 	return talitos_edesc_alloc(ctx->dev, areq->src, areq->dst, 0,
1543497f2e6bSLee Nipper 				   areq->nbytes, 0, 0, areq->base.flags);
15444de9d0b5SLee Nipper }
15454de9d0b5SLee Nipper 
15464de9d0b5SLee Nipper static int ablkcipher_encrypt(struct ablkcipher_request *areq)
15474de9d0b5SLee Nipper {
15484de9d0b5SLee Nipper 	struct crypto_ablkcipher *cipher = crypto_ablkcipher_reqtfm(areq);
15494de9d0b5SLee Nipper 	struct talitos_ctx *ctx = crypto_ablkcipher_ctx(cipher);
15504de9d0b5SLee Nipper 	struct talitos_edesc *edesc;
15514de9d0b5SLee Nipper 
15524de9d0b5SLee Nipper 	/* allocate extended descriptor */
15534de9d0b5SLee Nipper 	edesc = ablkcipher_edesc_alloc(areq);
15544de9d0b5SLee Nipper 	if (IS_ERR(edesc))
15554de9d0b5SLee Nipper 		return PTR_ERR(edesc);
15564de9d0b5SLee Nipper 
15574de9d0b5SLee Nipper 	/* set encrypt */
15584de9d0b5SLee Nipper 	edesc->desc.hdr = ctx->desc_hdr_template | DESC_HDR_MODE0_ENCRYPT;
15594de9d0b5SLee Nipper 
15604de9d0b5SLee Nipper 	return common_nonsnoop(edesc, areq, NULL, ablkcipher_done);
15614de9d0b5SLee Nipper }
15624de9d0b5SLee Nipper 
15634de9d0b5SLee Nipper static int ablkcipher_decrypt(struct ablkcipher_request *areq)
15644de9d0b5SLee Nipper {
15654de9d0b5SLee Nipper 	struct crypto_ablkcipher *cipher = crypto_ablkcipher_reqtfm(areq);
15664de9d0b5SLee Nipper 	struct talitos_ctx *ctx = crypto_ablkcipher_ctx(cipher);
15674de9d0b5SLee Nipper 	struct talitos_edesc *edesc;
15684de9d0b5SLee Nipper 
15694de9d0b5SLee Nipper 	/* allocate extended descriptor */
15704de9d0b5SLee Nipper 	edesc = ablkcipher_edesc_alloc(areq);
15714de9d0b5SLee Nipper 	if (IS_ERR(edesc))
15724de9d0b5SLee Nipper 		return PTR_ERR(edesc);
15734de9d0b5SLee Nipper 
15744de9d0b5SLee Nipper 	edesc->desc.hdr = ctx->desc_hdr_template | DESC_HDR_DIR_INBOUND;
15754de9d0b5SLee Nipper 
15764de9d0b5SLee Nipper 	return common_nonsnoop(edesc, areq, NULL, ablkcipher_done);
15774de9d0b5SLee Nipper }
15784de9d0b5SLee Nipper 
1579497f2e6bSLee Nipper static void common_nonsnoop_hash_unmap(struct device *dev,
1580497f2e6bSLee Nipper 				       struct talitos_edesc *edesc,
1581497f2e6bSLee Nipper 				       struct ahash_request *areq)
1582497f2e6bSLee Nipper {
1583497f2e6bSLee Nipper 	struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1584497f2e6bSLee Nipper 
1585497f2e6bSLee Nipper 	unmap_single_talitos_ptr(dev, &edesc->desc.ptr[5], DMA_FROM_DEVICE);
1586497f2e6bSLee Nipper 
1587497f2e6bSLee Nipper 	/* When using hashctx-in, must unmap it. */
1588497f2e6bSLee Nipper 	if (edesc->desc.ptr[1].len)
1589497f2e6bSLee Nipper 		unmap_single_talitos_ptr(dev, &edesc->desc.ptr[1],
1590497f2e6bSLee Nipper 					 DMA_TO_DEVICE);
1591497f2e6bSLee Nipper 
1592497f2e6bSLee Nipper 	if (edesc->desc.ptr[2].len)
1593497f2e6bSLee Nipper 		unmap_single_talitos_ptr(dev, &edesc->desc.ptr[2],
1594497f2e6bSLee Nipper 					 DMA_TO_DEVICE);
1595497f2e6bSLee Nipper 
1596497f2e6bSLee Nipper 	talitos_sg_unmap(dev, edesc, req_ctx->psrc, NULL);
1597497f2e6bSLee Nipper 
1598497f2e6bSLee Nipper 	if (edesc->dma_len)
1599497f2e6bSLee Nipper 		dma_unmap_single(dev, edesc->dma_link_tbl, edesc->dma_len,
1600497f2e6bSLee Nipper 				 DMA_BIDIRECTIONAL);
1601497f2e6bSLee Nipper 
1602497f2e6bSLee Nipper }
1603497f2e6bSLee Nipper 
1604497f2e6bSLee Nipper static void ahash_done(struct device *dev,
1605497f2e6bSLee Nipper 		       struct talitos_desc *desc, void *context,
1606497f2e6bSLee Nipper 		       int err)
1607497f2e6bSLee Nipper {
1608497f2e6bSLee Nipper 	struct ahash_request *areq = context;
1609497f2e6bSLee Nipper 	struct talitos_edesc *edesc =
1610497f2e6bSLee Nipper 		 container_of(desc, struct talitos_edesc, desc);
1611497f2e6bSLee Nipper 	struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1612497f2e6bSLee Nipper 
1613497f2e6bSLee Nipper 	if (!req_ctx->last && req_ctx->to_hash_later) {
1614497f2e6bSLee Nipper 		/* Position any partial block for next update/final/finup */
1615497f2e6bSLee Nipper 		memcpy(req_ctx->buf, req_ctx->bufnext, req_ctx->to_hash_later);
16165e833bc4SLee Nipper 		req_ctx->nbuf = req_ctx->to_hash_later;
1617497f2e6bSLee Nipper 	}
1618497f2e6bSLee Nipper 	common_nonsnoop_hash_unmap(dev, edesc, areq);
1619497f2e6bSLee Nipper 
1620497f2e6bSLee Nipper 	kfree(edesc);
1621497f2e6bSLee Nipper 
1622497f2e6bSLee Nipper 	areq->base.complete(&areq->base, err);
1623497f2e6bSLee Nipper }
1624497f2e6bSLee Nipper 
1625497f2e6bSLee Nipper static int common_nonsnoop_hash(struct talitos_edesc *edesc,
1626497f2e6bSLee Nipper 				struct ahash_request *areq, unsigned int length,
1627497f2e6bSLee Nipper 				void (*callback) (struct device *dev,
1628497f2e6bSLee Nipper 						  struct talitos_desc *desc,
1629497f2e6bSLee Nipper 						  void *context, int error))
1630497f2e6bSLee Nipper {
1631497f2e6bSLee Nipper 	struct crypto_ahash *tfm = crypto_ahash_reqtfm(areq);
1632497f2e6bSLee Nipper 	struct talitos_ctx *ctx = crypto_ahash_ctx(tfm);
1633497f2e6bSLee Nipper 	struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1634497f2e6bSLee Nipper 	struct device *dev = ctx->dev;
1635497f2e6bSLee Nipper 	struct talitos_desc *desc = &edesc->desc;
1636497f2e6bSLee Nipper 	int sg_count, ret;
1637497f2e6bSLee Nipper 
1638497f2e6bSLee Nipper 	/* first DWORD empty */
1639497f2e6bSLee Nipper 	desc->ptr[0] = zero_entry;
1640497f2e6bSLee Nipper 
164160f208d7SKim Phillips 	/* hash context in */
164260f208d7SKim Phillips 	if (!req_ctx->first || req_ctx->swinit) {
1643497f2e6bSLee Nipper 		map_single_talitos_ptr(dev, &desc->ptr[1],
1644497f2e6bSLee Nipper 				       req_ctx->hw_context_size,
1645497f2e6bSLee Nipper 				       (char *)req_ctx->hw_context, 0,
1646497f2e6bSLee Nipper 				       DMA_TO_DEVICE);
164760f208d7SKim Phillips 		req_ctx->swinit = 0;
1648497f2e6bSLee Nipper 	} else {
1649497f2e6bSLee Nipper 		desc->ptr[1] = zero_entry;
1650497f2e6bSLee Nipper 		/* Indicate next op is not the first. */
1651497f2e6bSLee Nipper 		req_ctx->first = 0;
1652497f2e6bSLee Nipper 	}
1653497f2e6bSLee Nipper 
1654497f2e6bSLee Nipper 	/* HMAC key */
1655497f2e6bSLee Nipper 	if (ctx->keylen)
1656497f2e6bSLee Nipper 		map_single_talitos_ptr(dev, &desc->ptr[2], ctx->keylen,
1657497f2e6bSLee Nipper 				       (char *)&ctx->key, 0, DMA_TO_DEVICE);
1658497f2e6bSLee Nipper 	else
1659497f2e6bSLee Nipper 		desc->ptr[2] = zero_entry;
1660497f2e6bSLee Nipper 
1661497f2e6bSLee Nipper 	/*
1662497f2e6bSLee Nipper 	 * data in
1663497f2e6bSLee Nipper 	 */
1664497f2e6bSLee Nipper 	desc->ptr[3].len = cpu_to_be16(length);
1665497f2e6bSLee Nipper 	desc->ptr[3].j_extent = 0;
1666497f2e6bSLee Nipper 
1667497f2e6bSLee Nipper 	sg_count = talitos_map_sg(dev, req_ctx->psrc,
1668497f2e6bSLee Nipper 				  edesc->src_nents ? : 1,
1669497f2e6bSLee Nipper 				  DMA_TO_DEVICE,
1670497f2e6bSLee Nipper 				  edesc->src_is_chained);
1671497f2e6bSLee Nipper 
1672497f2e6bSLee Nipper 	if (sg_count == 1) {
1673497f2e6bSLee Nipper 		to_talitos_ptr(&desc->ptr[3], sg_dma_address(req_ctx->psrc));
1674497f2e6bSLee Nipper 	} else {
1675497f2e6bSLee Nipper 		sg_count = sg_to_link_tbl(req_ctx->psrc, sg_count, length,
1676497f2e6bSLee Nipper 					  &edesc->link_tbl[0]);
1677497f2e6bSLee Nipper 		if (sg_count > 1) {
1678497f2e6bSLee Nipper 			desc->ptr[3].j_extent |= DESC_PTR_LNKTBL_JUMP;
1679497f2e6bSLee Nipper 			to_talitos_ptr(&desc->ptr[3], edesc->dma_link_tbl);
1680497f2e6bSLee Nipper 			dma_sync_single_for_device(ctx->dev,
1681497f2e6bSLee Nipper 						   edesc->dma_link_tbl,
1682497f2e6bSLee Nipper 						   edesc->dma_len,
1683497f2e6bSLee Nipper 						   DMA_BIDIRECTIONAL);
1684497f2e6bSLee Nipper 		} else {
1685497f2e6bSLee Nipper 			/* Only one segment now, so no link tbl needed */
1686497f2e6bSLee Nipper 			to_talitos_ptr(&desc->ptr[3],
1687497f2e6bSLee Nipper 				       sg_dma_address(req_ctx->psrc));
1688497f2e6bSLee Nipper 		}
1689497f2e6bSLee Nipper 	}
1690497f2e6bSLee Nipper 
1691497f2e6bSLee Nipper 	/* fifth DWORD empty */
1692497f2e6bSLee Nipper 	desc->ptr[4] = zero_entry;
1693497f2e6bSLee Nipper 
1694497f2e6bSLee Nipper 	/* hash/HMAC out -or- hash context out */
1695497f2e6bSLee Nipper 	if (req_ctx->last)
1696497f2e6bSLee Nipper 		map_single_talitos_ptr(dev, &desc->ptr[5],
1697497f2e6bSLee Nipper 				       crypto_ahash_digestsize(tfm),
1698497f2e6bSLee Nipper 				       areq->result, 0, DMA_FROM_DEVICE);
1699497f2e6bSLee Nipper 	else
1700497f2e6bSLee Nipper 		map_single_talitos_ptr(dev, &desc->ptr[5],
1701497f2e6bSLee Nipper 				       req_ctx->hw_context_size,
1702497f2e6bSLee Nipper 				       req_ctx->hw_context, 0, DMA_FROM_DEVICE);
1703497f2e6bSLee Nipper 
1704497f2e6bSLee Nipper 	/* last DWORD empty */
1705497f2e6bSLee Nipper 	desc->ptr[6] = zero_entry;
1706497f2e6bSLee Nipper 
1707497f2e6bSLee Nipper 	ret = talitos_submit(dev, desc, callback, areq);
1708497f2e6bSLee Nipper 	if (ret != -EINPROGRESS) {
1709497f2e6bSLee Nipper 		common_nonsnoop_hash_unmap(dev, edesc, areq);
1710497f2e6bSLee Nipper 		kfree(edesc);
1711497f2e6bSLee Nipper 	}
1712497f2e6bSLee Nipper 	return ret;
1713497f2e6bSLee Nipper }
1714497f2e6bSLee Nipper 
1715497f2e6bSLee Nipper static struct talitos_edesc *ahash_edesc_alloc(struct ahash_request *areq,
1716497f2e6bSLee Nipper 					       unsigned int nbytes)
1717497f2e6bSLee Nipper {
1718497f2e6bSLee Nipper 	struct crypto_ahash *tfm = crypto_ahash_reqtfm(areq);
1719497f2e6bSLee Nipper 	struct talitos_ctx *ctx = crypto_ahash_ctx(tfm);
1720497f2e6bSLee Nipper 	struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1721497f2e6bSLee Nipper 
1722497f2e6bSLee Nipper 	return talitos_edesc_alloc(ctx->dev, req_ctx->psrc, NULL, 1,
1723497f2e6bSLee Nipper 				   nbytes, 0, 0, areq->base.flags);
1724497f2e6bSLee Nipper }
1725497f2e6bSLee Nipper 
1726497f2e6bSLee Nipper static int ahash_init(struct ahash_request *areq)
1727497f2e6bSLee Nipper {
1728497f2e6bSLee Nipper 	struct crypto_ahash *tfm = crypto_ahash_reqtfm(areq);
1729497f2e6bSLee Nipper 	struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1730497f2e6bSLee Nipper 
1731497f2e6bSLee Nipper 	/* Initialize the context */
17325e833bc4SLee Nipper 	req_ctx->nbuf = 0;
173360f208d7SKim Phillips 	req_ctx->first = 1; /* first indicates h/w must init its context */
173460f208d7SKim Phillips 	req_ctx->swinit = 0; /* assume h/w init of context */
1735497f2e6bSLee Nipper 	req_ctx->hw_context_size =
1736497f2e6bSLee Nipper 		(crypto_ahash_digestsize(tfm) <= SHA256_DIGEST_SIZE)
1737497f2e6bSLee Nipper 			? TALITOS_MDEU_CONTEXT_SIZE_MD5_SHA1_SHA256
1738497f2e6bSLee Nipper 			: TALITOS_MDEU_CONTEXT_SIZE_SHA384_SHA512;
1739497f2e6bSLee Nipper 
1740497f2e6bSLee Nipper 	return 0;
1741497f2e6bSLee Nipper }
1742497f2e6bSLee Nipper 
174360f208d7SKim Phillips /*
174460f208d7SKim Phillips  * on h/w without explicit sha224 support, we initialize h/w context
174560f208d7SKim Phillips  * manually with sha224 constants, and tell it to run sha256.
174660f208d7SKim Phillips  */
174760f208d7SKim Phillips static int ahash_init_sha224_swinit(struct ahash_request *areq)
174860f208d7SKim Phillips {
174960f208d7SKim Phillips 	struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
175060f208d7SKim Phillips 
175160f208d7SKim Phillips 	ahash_init(areq);
175260f208d7SKim Phillips 	req_ctx->swinit = 1;/* prevent h/w initting context with sha256 values*/
175360f208d7SKim Phillips 
175460f208d7SKim Phillips 	req_ctx->hw_context[0] = cpu_to_be32(SHA224_H0);
175560f208d7SKim Phillips 	req_ctx->hw_context[1] = cpu_to_be32(SHA224_H1);
175660f208d7SKim Phillips 	req_ctx->hw_context[2] = cpu_to_be32(SHA224_H2);
175760f208d7SKim Phillips 	req_ctx->hw_context[3] = cpu_to_be32(SHA224_H3);
175860f208d7SKim Phillips 	req_ctx->hw_context[4] = cpu_to_be32(SHA224_H4);
175960f208d7SKim Phillips 	req_ctx->hw_context[5] = cpu_to_be32(SHA224_H5);
176060f208d7SKim Phillips 	req_ctx->hw_context[6] = cpu_to_be32(SHA224_H6);
176160f208d7SKim Phillips 	req_ctx->hw_context[7] = cpu_to_be32(SHA224_H7);
176260f208d7SKim Phillips 
176360f208d7SKim Phillips 	/* init 64-bit count */
176460f208d7SKim Phillips 	req_ctx->hw_context[8] = 0;
176560f208d7SKim Phillips 	req_ctx->hw_context[9] = 0;
176660f208d7SKim Phillips 
176760f208d7SKim Phillips 	return 0;
176860f208d7SKim Phillips }
176960f208d7SKim Phillips 
1770497f2e6bSLee Nipper static int ahash_process_req(struct ahash_request *areq, unsigned int nbytes)
1771497f2e6bSLee Nipper {
1772497f2e6bSLee Nipper 	struct crypto_ahash *tfm = crypto_ahash_reqtfm(areq);
1773497f2e6bSLee Nipper 	struct talitos_ctx *ctx = crypto_ahash_ctx(tfm);
1774497f2e6bSLee Nipper 	struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1775497f2e6bSLee Nipper 	struct talitos_edesc *edesc;
1776497f2e6bSLee Nipper 	unsigned int blocksize =
1777497f2e6bSLee Nipper 			crypto_tfm_alg_blocksize(crypto_ahash_tfm(tfm));
1778497f2e6bSLee Nipper 	unsigned int nbytes_to_hash;
1779497f2e6bSLee Nipper 	unsigned int to_hash_later;
17805e833bc4SLee Nipper 	unsigned int nsg;
1781497f2e6bSLee Nipper 	int chained;
1782497f2e6bSLee Nipper 
17835e833bc4SLee Nipper 	if (!req_ctx->last && (nbytes + req_ctx->nbuf <= blocksize)) {
17845e833bc4SLee Nipper 		/* Buffer up to one whole block */
1785497f2e6bSLee Nipper 		sg_copy_to_buffer(areq->src,
1786497f2e6bSLee Nipper 				  sg_count(areq->src, nbytes, &chained),
17875e833bc4SLee Nipper 				  req_ctx->buf + req_ctx->nbuf, nbytes);
17885e833bc4SLee Nipper 		req_ctx->nbuf += nbytes;
1789497f2e6bSLee Nipper 		return 0;
1790497f2e6bSLee Nipper 	}
1791497f2e6bSLee Nipper 
17925e833bc4SLee Nipper 	/* At least (blocksize + 1) bytes are available to hash */
17935e833bc4SLee Nipper 	nbytes_to_hash = nbytes + req_ctx->nbuf;
17945e833bc4SLee Nipper 	to_hash_later = nbytes_to_hash & (blocksize - 1);
17955e833bc4SLee Nipper 
17965e833bc4SLee Nipper 	if (req_ctx->last)
17975e833bc4SLee Nipper 		to_hash_later = 0;
17985e833bc4SLee Nipper 	else if (to_hash_later)
17995e833bc4SLee Nipper 		/* There is a partial block. Hash the full block(s) now */
18005e833bc4SLee Nipper 		nbytes_to_hash -= to_hash_later;
18015e833bc4SLee Nipper 	else {
18025e833bc4SLee Nipper 		/* Keep one block buffered */
18035e833bc4SLee Nipper 		nbytes_to_hash -= blocksize;
18045e833bc4SLee Nipper 		to_hash_later = blocksize;
1805497f2e6bSLee Nipper 	}
18065e833bc4SLee Nipper 
18075e833bc4SLee Nipper 	/* Chain in any previously buffered data */
18085e833bc4SLee Nipper 	if (req_ctx->nbuf) {
18095e833bc4SLee Nipper 		nsg = (req_ctx->nbuf < nbytes_to_hash) ? 2 : 1;
18105e833bc4SLee Nipper 		sg_init_table(req_ctx->bufsl, nsg);
18115e833bc4SLee Nipper 		sg_set_buf(req_ctx->bufsl, req_ctx->buf, req_ctx->nbuf);
18125e833bc4SLee Nipper 		if (nsg > 1)
18135e833bc4SLee Nipper 			scatterwalk_sg_chain(req_ctx->bufsl, 2, areq->src);
18145e833bc4SLee Nipper 		req_ctx->psrc = req_ctx->bufsl;
18155e833bc4SLee Nipper 	} else
18165e833bc4SLee Nipper 		req_ctx->psrc = areq->src;
18175e833bc4SLee Nipper 
1818497f2e6bSLee Nipper 	if (to_hash_later) {
18195e833bc4SLee Nipper 		int nents = sg_count(areq->src, nbytes, &chained);
1820497f2e6bSLee Nipper 		sg_copy_end_to_buffer(areq->src, nents,
1821497f2e6bSLee Nipper 				      req_ctx->bufnext,
1822497f2e6bSLee Nipper 				      to_hash_later,
1823497f2e6bSLee Nipper 				      nbytes - to_hash_later);
1824497f2e6bSLee Nipper 	}
1825497f2e6bSLee Nipper 	req_ctx->to_hash_later = to_hash_later;
1826497f2e6bSLee Nipper 
18275e833bc4SLee Nipper 	/* Allocate extended descriptor */
1828497f2e6bSLee Nipper 	edesc = ahash_edesc_alloc(areq, nbytes_to_hash);
1829497f2e6bSLee Nipper 	if (IS_ERR(edesc))
1830497f2e6bSLee Nipper 		return PTR_ERR(edesc);
1831497f2e6bSLee Nipper 
1832497f2e6bSLee Nipper 	edesc->desc.hdr = ctx->desc_hdr_template;
1833497f2e6bSLee Nipper 
1834497f2e6bSLee Nipper 	/* On last one, request SEC to pad; otherwise continue */
1835497f2e6bSLee Nipper 	if (req_ctx->last)
1836497f2e6bSLee Nipper 		edesc->desc.hdr |= DESC_HDR_MODE0_MDEU_PAD;
1837497f2e6bSLee Nipper 	else
1838497f2e6bSLee Nipper 		edesc->desc.hdr |= DESC_HDR_MODE0_MDEU_CONT;
1839497f2e6bSLee Nipper 
184060f208d7SKim Phillips 	/* request SEC to INIT hash. */
184160f208d7SKim Phillips 	if (req_ctx->first && !req_ctx->swinit)
1842497f2e6bSLee Nipper 		edesc->desc.hdr |= DESC_HDR_MODE0_MDEU_INIT;
1843497f2e6bSLee Nipper 
1844497f2e6bSLee Nipper 	/* When the tfm context has a keylen, it's an HMAC.
1845497f2e6bSLee Nipper 	 * A first or last (ie. not middle) descriptor must request HMAC.
1846497f2e6bSLee Nipper 	 */
1847497f2e6bSLee Nipper 	if (ctx->keylen && (req_ctx->first || req_ctx->last))
1848497f2e6bSLee Nipper 		edesc->desc.hdr |= DESC_HDR_MODE0_MDEU_HMAC;
1849497f2e6bSLee Nipper 
1850497f2e6bSLee Nipper 	return common_nonsnoop_hash(edesc, areq, nbytes_to_hash,
1851497f2e6bSLee Nipper 				    ahash_done);
1852497f2e6bSLee Nipper }
1853497f2e6bSLee Nipper 
1854497f2e6bSLee Nipper static int ahash_update(struct ahash_request *areq)
1855497f2e6bSLee Nipper {
1856497f2e6bSLee Nipper 	struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1857497f2e6bSLee Nipper 
1858497f2e6bSLee Nipper 	req_ctx->last = 0;
1859497f2e6bSLee Nipper 
1860497f2e6bSLee Nipper 	return ahash_process_req(areq, areq->nbytes);
1861497f2e6bSLee Nipper }
1862497f2e6bSLee Nipper 
1863497f2e6bSLee Nipper static int ahash_final(struct ahash_request *areq)
1864497f2e6bSLee Nipper {
1865497f2e6bSLee Nipper 	struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1866497f2e6bSLee Nipper 
1867497f2e6bSLee Nipper 	req_ctx->last = 1;
1868497f2e6bSLee Nipper 
1869497f2e6bSLee Nipper 	return ahash_process_req(areq, 0);
1870497f2e6bSLee Nipper }
1871497f2e6bSLee Nipper 
1872497f2e6bSLee Nipper static int ahash_finup(struct ahash_request *areq)
1873497f2e6bSLee Nipper {
1874497f2e6bSLee Nipper 	struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1875497f2e6bSLee Nipper 
1876497f2e6bSLee Nipper 	req_ctx->last = 1;
1877497f2e6bSLee Nipper 
1878497f2e6bSLee Nipper 	return ahash_process_req(areq, areq->nbytes);
1879497f2e6bSLee Nipper }
1880497f2e6bSLee Nipper 
1881497f2e6bSLee Nipper static int ahash_digest(struct ahash_request *areq)
1882497f2e6bSLee Nipper {
1883497f2e6bSLee Nipper 	struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
188460f208d7SKim Phillips 	struct crypto_ahash *ahash = crypto_ahash_reqtfm(areq);
1885497f2e6bSLee Nipper 
188660f208d7SKim Phillips 	ahash->init(areq);
1887497f2e6bSLee Nipper 	req_ctx->last = 1;
1888497f2e6bSLee Nipper 
1889497f2e6bSLee Nipper 	return ahash_process_req(areq, areq->nbytes);
1890497f2e6bSLee Nipper }
1891497f2e6bSLee Nipper 
18929c4a7965SKim Phillips struct talitos_alg_template {
1893d5e4aaefSLee Nipper 	u32 type;
1894d5e4aaefSLee Nipper 	union {
1895d5e4aaefSLee Nipper 		struct crypto_alg crypto;
1896acbf7c62SLee Nipper 		struct ahash_alg hash;
1897d5e4aaefSLee Nipper 	} alg;
18989c4a7965SKim Phillips 	__be32 desc_hdr_template;
18999c4a7965SKim Phillips };
19009c4a7965SKim Phillips 
19019c4a7965SKim Phillips static struct talitos_alg_template driver_algs[] = {
190256af8cd4SLee Nipper 	/* AEAD algorithms.  These use a single-pass ipsec_esp descriptor */
1903d5e4aaefSLee Nipper 	{	.type = CRYPTO_ALG_TYPE_AEAD,
1904d5e4aaefSLee Nipper 		.alg.crypto = {
190556af8cd4SLee Nipper 			.cra_name = "authenc(hmac(sha1),cbc(aes))",
190656af8cd4SLee Nipper 			.cra_driver_name = "authenc-hmac-sha1-cbc-aes-talitos",
190756af8cd4SLee Nipper 			.cra_blocksize = AES_BLOCK_SIZE,
190856af8cd4SLee Nipper 			.cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
190956af8cd4SLee Nipper 			.cra_type = &crypto_aead_type,
191056af8cd4SLee Nipper 			.cra_aead = {
191156af8cd4SLee Nipper 				.setkey = aead_setkey,
191256af8cd4SLee Nipper 				.setauthsize = aead_setauthsize,
191356af8cd4SLee Nipper 				.encrypt = aead_encrypt,
191456af8cd4SLee Nipper 				.decrypt = aead_decrypt,
191556af8cd4SLee Nipper 				.givencrypt = aead_givencrypt,
19169c4a7965SKim Phillips 				.geniv = "<built-in>",
19173952f17eSLee Nipper 				.ivsize = AES_BLOCK_SIZE,
19183952f17eSLee Nipper 				.maxauthsize = SHA1_DIGEST_SIZE,
191956af8cd4SLee Nipper 			}
19209c4a7965SKim Phillips 		},
19219c4a7965SKim Phillips 		.desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
19229c4a7965SKim Phillips 			             DESC_HDR_SEL0_AESU |
19239c4a7965SKim Phillips 		                     DESC_HDR_MODE0_AESU_CBC |
19249c4a7965SKim Phillips 		                     DESC_HDR_SEL1_MDEUA |
19259c4a7965SKim Phillips 		                     DESC_HDR_MODE1_MDEU_INIT |
19269c4a7965SKim Phillips 		                     DESC_HDR_MODE1_MDEU_PAD |
19279c4a7965SKim Phillips 		                     DESC_HDR_MODE1_MDEU_SHA1_HMAC,
192870bcaca7SLee Nipper 	},
1929d5e4aaefSLee Nipper 	{	.type = CRYPTO_ALG_TYPE_AEAD,
1930d5e4aaefSLee Nipper 		.alg.crypto = {
193156af8cd4SLee Nipper 			.cra_name = "authenc(hmac(sha1),cbc(des3_ede))",
193256af8cd4SLee Nipper 			.cra_driver_name = "authenc-hmac-sha1-cbc-3des-talitos",
193356af8cd4SLee Nipper 			.cra_blocksize = DES3_EDE_BLOCK_SIZE,
193456af8cd4SLee Nipper 			.cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
193556af8cd4SLee Nipper 			.cra_type = &crypto_aead_type,
193656af8cd4SLee Nipper 			.cra_aead = {
193756af8cd4SLee Nipper 				.setkey = aead_setkey,
193856af8cd4SLee Nipper 				.setauthsize = aead_setauthsize,
193956af8cd4SLee Nipper 				.encrypt = aead_encrypt,
194056af8cd4SLee Nipper 				.decrypt = aead_decrypt,
194156af8cd4SLee Nipper 				.givencrypt = aead_givencrypt,
194270bcaca7SLee Nipper 				.geniv = "<built-in>",
19433952f17eSLee Nipper 				.ivsize = DES3_EDE_BLOCK_SIZE,
19443952f17eSLee Nipper 				.maxauthsize = SHA1_DIGEST_SIZE,
194556af8cd4SLee Nipper 			}
194670bcaca7SLee Nipper 		},
194770bcaca7SLee Nipper 		.desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
194870bcaca7SLee Nipper 			             DESC_HDR_SEL0_DEU |
194970bcaca7SLee Nipper 		                     DESC_HDR_MODE0_DEU_CBC |
195070bcaca7SLee Nipper 		                     DESC_HDR_MODE0_DEU_3DES |
195170bcaca7SLee Nipper 		                     DESC_HDR_SEL1_MDEUA |
195270bcaca7SLee Nipper 		                     DESC_HDR_MODE1_MDEU_INIT |
195370bcaca7SLee Nipper 		                     DESC_HDR_MODE1_MDEU_PAD |
195470bcaca7SLee Nipper 		                     DESC_HDR_MODE1_MDEU_SHA1_HMAC,
19553952f17eSLee Nipper 	},
1956d5e4aaefSLee Nipper 	{	.type = CRYPTO_ALG_TYPE_AEAD,
1957d5e4aaefSLee Nipper 		.alg.crypto = {
195856af8cd4SLee Nipper 			.cra_name = "authenc(hmac(sha256),cbc(aes))",
195956af8cd4SLee Nipper 			.cra_driver_name = "authenc-hmac-sha256-cbc-aes-talitos",
196056af8cd4SLee Nipper 			.cra_blocksize = AES_BLOCK_SIZE,
196156af8cd4SLee Nipper 			.cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
196256af8cd4SLee Nipper 			.cra_type = &crypto_aead_type,
196356af8cd4SLee Nipper 			.cra_aead = {
196456af8cd4SLee Nipper 				.setkey = aead_setkey,
196556af8cd4SLee Nipper 				.setauthsize = aead_setauthsize,
196656af8cd4SLee Nipper 				.encrypt = aead_encrypt,
196756af8cd4SLee Nipper 				.decrypt = aead_decrypt,
196856af8cd4SLee Nipper 				.givencrypt = aead_givencrypt,
19693952f17eSLee Nipper 				.geniv = "<built-in>",
19703952f17eSLee Nipper 				.ivsize = AES_BLOCK_SIZE,
19713952f17eSLee Nipper 				.maxauthsize = SHA256_DIGEST_SIZE,
197256af8cd4SLee Nipper 			}
19733952f17eSLee Nipper 		},
19743952f17eSLee Nipper 		.desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
19753952f17eSLee Nipper 			             DESC_HDR_SEL0_AESU |
19763952f17eSLee Nipper 		                     DESC_HDR_MODE0_AESU_CBC |
19773952f17eSLee Nipper 		                     DESC_HDR_SEL1_MDEUA |
19783952f17eSLee Nipper 		                     DESC_HDR_MODE1_MDEU_INIT |
19793952f17eSLee Nipper 		                     DESC_HDR_MODE1_MDEU_PAD |
19803952f17eSLee Nipper 		                     DESC_HDR_MODE1_MDEU_SHA256_HMAC,
19813952f17eSLee Nipper 	},
1982d5e4aaefSLee Nipper 	{	.type = CRYPTO_ALG_TYPE_AEAD,
1983d5e4aaefSLee Nipper 		.alg.crypto = {
198456af8cd4SLee Nipper 			.cra_name = "authenc(hmac(sha256),cbc(des3_ede))",
198556af8cd4SLee Nipper 			.cra_driver_name = "authenc-hmac-sha256-cbc-3des-talitos",
198656af8cd4SLee Nipper 			.cra_blocksize = DES3_EDE_BLOCK_SIZE,
198756af8cd4SLee Nipper 			.cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
198856af8cd4SLee Nipper 			.cra_type = &crypto_aead_type,
198956af8cd4SLee Nipper 			.cra_aead = {
199056af8cd4SLee Nipper 				.setkey = aead_setkey,
199156af8cd4SLee Nipper 				.setauthsize = aead_setauthsize,
199256af8cd4SLee Nipper 				.encrypt = aead_encrypt,
199356af8cd4SLee Nipper 				.decrypt = aead_decrypt,
199456af8cd4SLee Nipper 				.givencrypt = aead_givencrypt,
19953952f17eSLee Nipper 				.geniv = "<built-in>",
19963952f17eSLee Nipper 				.ivsize = DES3_EDE_BLOCK_SIZE,
19973952f17eSLee Nipper 				.maxauthsize = SHA256_DIGEST_SIZE,
199856af8cd4SLee Nipper 			}
19993952f17eSLee Nipper 		},
20003952f17eSLee Nipper 		.desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
20013952f17eSLee Nipper 			             DESC_HDR_SEL0_DEU |
20023952f17eSLee Nipper 		                     DESC_HDR_MODE0_DEU_CBC |
20033952f17eSLee Nipper 		                     DESC_HDR_MODE0_DEU_3DES |
20043952f17eSLee Nipper 		                     DESC_HDR_SEL1_MDEUA |
20053952f17eSLee Nipper 		                     DESC_HDR_MODE1_MDEU_INIT |
20063952f17eSLee Nipper 		                     DESC_HDR_MODE1_MDEU_PAD |
20073952f17eSLee Nipper 		                     DESC_HDR_MODE1_MDEU_SHA256_HMAC,
20083952f17eSLee Nipper 	},
2009d5e4aaefSLee Nipper 	{	.type = CRYPTO_ALG_TYPE_AEAD,
2010d5e4aaefSLee Nipper 		.alg.crypto = {
201156af8cd4SLee Nipper 			.cra_name = "authenc(hmac(md5),cbc(aes))",
201256af8cd4SLee Nipper 			.cra_driver_name = "authenc-hmac-md5-cbc-aes-talitos",
201356af8cd4SLee Nipper 			.cra_blocksize = AES_BLOCK_SIZE,
201456af8cd4SLee Nipper 			.cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
201556af8cd4SLee Nipper 			.cra_type = &crypto_aead_type,
201656af8cd4SLee Nipper 			.cra_aead = {
201756af8cd4SLee Nipper 				.setkey = aead_setkey,
201856af8cd4SLee Nipper 				.setauthsize = aead_setauthsize,
201956af8cd4SLee Nipper 				.encrypt = aead_encrypt,
202056af8cd4SLee Nipper 				.decrypt = aead_decrypt,
202156af8cd4SLee Nipper 				.givencrypt = aead_givencrypt,
20223952f17eSLee Nipper 				.geniv = "<built-in>",
20233952f17eSLee Nipper 				.ivsize = AES_BLOCK_SIZE,
20243952f17eSLee Nipper 				.maxauthsize = MD5_DIGEST_SIZE,
202556af8cd4SLee Nipper 			}
20263952f17eSLee Nipper 		},
20273952f17eSLee Nipper 		.desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
20283952f17eSLee Nipper 			             DESC_HDR_SEL0_AESU |
20293952f17eSLee Nipper 		                     DESC_HDR_MODE0_AESU_CBC |
20303952f17eSLee Nipper 		                     DESC_HDR_SEL1_MDEUA |
20313952f17eSLee Nipper 		                     DESC_HDR_MODE1_MDEU_INIT |
20323952f17eSLee Nipper 		                     DESC_HDR_MODE1_MDEU_PAD |
20333952f17eSLee Nipper 		                     DESC_HDR_MODE1_MDEU_MD5_HMAC,
20343952f17eSLee Nipper 	},
2035d5e4aaefSLee Nipper 	{	.type = CRYPTO_ALG_TYPE_AEAD,
2036d5e4aaefSLee Nipper 		.alg.crypto = {
203756af8cd4SLee Nipper 			.cra_name = "authenc(hmac(md5),cbc(des3_ede))",
203856af8cd4SLee Nipper 			.cra_driver_name = "authenc-hmac-md5-cbc-3des-talitos",
203956af8cd4SLee Nipper 			.cra_blocksize = DES3_EDE_BLOCK_SIZE,
204056af8cd4SLee Nipper 			.cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
204156af8cd4SLee Nipper 			.cra_type = &crypto_aead_type,
204256af8cd4SLee Nipper 			.cra_aead = {
204356af8cd4SLee Nipper 				.setkey = aead_setkey,
204456af8cd4SLee Nipper 				.setauthsize = aead_setauthsize,
204556af8cd4SLee Nipper 				.encrypt = aead_encrypt,
204656af8cd4SLee Nipper 				.decrypt = aead_decrypt,
204756af8cd4SLee Nipper 				.givencrypt = aead_givencrypt,
20483952f17eSLee Nipper 				.geniv = "<built-in>",
20493952f17eSLee Nipper 				.ivsize = DES3_EDE_BLOCK_SIZE,
20503952f17eSLee Nipper 				.maxauthsize = MD5_DIGEST_SIZE,
205156af8cd4SLee Nipper 			}
20523952f17eSLee Nipper 		},
20533952f17eSLee Nipper 		.desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
20543952f17eSLee Nipper 			             DESC_HDR_SEL0_DEU |
20553952f17eSLee Nipper 		                     DESC_HDR_MODE0_DEU_CBC |
20563952f17eSLee Nipper 		                     DESC_HDR_MODE0_DEU_3DES |
20573952f17eSLee Nipper 		                     DESC_HDR_SEL1_MDEUA |
20583952f17eSLee Nipper 		                     DESC_HDR_MODE1_MDEU_INIT |
20593952f17eSLee Nipper 		                     DESC_HDR_MODE1_MDEU_PAD |
20603952f17eSLee Nipper 		                     DESC_HDR_MODE1_MDEU_MD5_HMAC,
20614de9d0b5SLee Nipper 	},
20624de9d0b5SLee Nipper 	/* ABLKCIPHER algorithms. */
2063d5e4aaefSLee Nipper 	{	.type = CRYPTO_ALG_TYPE_ABLKCIPHER,
2064d5e4aaefSLee Nipper 		.alg.crypto = {
20654de9d0b5SLee Nipper 			.cra_name = "cbc(aes)",
20664de9d0b5SLee Nipper 			.cra_driver_name = "cbc-aes-talitos",
20674de9d0b5SLee Nipper 			.cra_blocksize = AES_BLOCK_SIZE,
20684de9d0b5SLee Nipper 			.cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
20694de9d0b5SLee Nipper                                      CRYPTO_ALG_ASYNC,
20704de9d0b5SLee Nipper 			.cra_type = &crypto_ablkcipher_type,
20714de9d0b5SLee Nipper 			.cra_ablkcipher = {
20724de9d0b5SLee Nipper 				.setkey = ablkcipher_setkey,
20734de9d0b5SLee Nipper 				.encrypt = ablkcipher_encrypt,
20744de9d0b5SLee Nipper 				.decrypt = ablkcipher_decrypt,
20754de9d0b5SLee Nipper 				.geniv = "eseqiv",
20764de9d0b5SLee Nipper 				.min_keysize = AES_MIN_KEY_SIZE,
20774de9d0b5SLee Nipper 				.max_keysize = AES_MAX_KEY_SIZE,
20784de9d0b5SLee Nipper 				.ivsize = AES_BLOCK_SIZE,
20794de9d0b5SLee Nipper 			}
20804de9d0b5SLee Nipper 		},
20814de9d0b5SLee Nipper 		.desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
20824de9d0b5SLee Nipper 				     DESC_HDR_SEL0_AESU |
20834de9d0b5SLee Nipper 				     DESC_HDR_MODE0_AESU_CBC,
20844de9d0b5SLee Nipper 	},
2085d5e4aaefSLee Nipper 	{	.type = CRYPTO_ALG_TYPE_ABLKCIPHER,
2086d5e4aaefSLee Nipper 		.alg.crypto = {
20874de9d0b5SLee Nipper 			.cra_name = "cbc(des3_ede)",
20884de9d0b5SLee Nipper 			.cra_driver_name = "cbc-3des-talitos",
20894de9d0b5SLee Nipper 			.cra_blocksize = DES3_EDE_BLOCK_SIZE,
20904de9d0b5SLee Nipper 			.cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
20914de9d0b5SLee Nipper                                      CRYPTO_ALG_ASYNC,
20924de9d0b5SLee Nipper 			.cra_type = &crypto_ablkcipher_type,
20934de9d0b5SLee Nipper 			.cra_ablkcipher = {
20944de9d0b5SLee Nipper 				.setkey = ablkcipher_setkey,
20954de9d0b5SLee Nipper 				.encrypt = ablkcipher_encrypt,
20964de9d0b5SLee Nipper 				.decrypt = ablkcipher_decrypt,
20974de9d0b5SLee Nipper 				.geniv = "eseqiv",
20984de9d0b5SLee Nipper 				.min_keysize = DES3_EDE_KEY_SIZE,
20994de9d0b5SLee Nipper 				.max_keysize = DES3_EDE_KEY_SIZE,
21004de9d0b5SLee Nipper 				.ivsize = DES3_EDE_BLOCK_SIZE,
21014de9d0b5SLee Nipper 			}
21024de9d0b5SLee Nipper 		},
21034de9d0b5SLee Nipper 		.desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
21044de9d0b5SLee Nipper 			             DESC_HDR_SEL0_DEU |
21054de9d0b5SLee Nipper 		                     DESC_HDR_MODE0_DEU_CBC |
21064de9d0b5SLee Nipper 		                     DESC_HDR_MODE0_DEU_3DES,
2107497f2e6bSLee Nipper 	},
2108497f2e6bSLee Nipper 	/* AHASH algorithms. */
2109497f2e6bSLee Nipper 	{	.type = CRYPTO_ALG_TYPE_AHASH,
2110497f2e6bSLee Nipper 		.alg.hash = {
2111497f2e6bSLee Nipper 			.init = ahash_init,
2112497f2e6bSLee Nipper 			.update = ahash_update,
2113497f2e6bSLee Nipper 			.final = ahash_final,
2114497f2e6bSLee Nipper 			.finup = ahash_finup,
2115497f2e6bSLee Nipper 			.digest = ahash_digest,
2116497f2e6bSLee Nipper 			.halg.digestsize = MD5_DIGEST_SIZE,
2117497f2e6bSLee Nipper 			.halg.base = {
2118497f2e6bSLee Nipper 				.cra_name = "md5",
2119497f2e6bSLee Nipper 				.cra_driver_name = "md5-talitos",
2120497f2e6bSLee Nipper 				.cra_blocksize = MD5_BLOCK_SIZE,
2121497f2e6bSLee Nipper 				.cra_flags = CRYPTO_ALG_TYPE_AHASH |
2122497f2e6bSLee Nipper 					     CRYPTO_ALG_ASYNC,
2123497f2e6bSLee Nipper 				.cra_type = &crypto_ahash_type
21249c4a7965SKim Phillips 			}
2125497f2e6bSLee Nipper 		},
2126497f2e6bSLee Nipper 		.desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2127497f2e6bSLee Nipper 				     DESC_HDR_SEL0_MDEUA |
2128497f2e6bSLee Nipper 				     DESC_HDR_MODE0_MDEU_MD5,
2129497f2e6bSLee Nipper 	},
2130497f2e6bSLee Nipper 	{	.type = CRYPTO_ALG_TYPE_AHASH,
2131497f2e6bSLee Nipper 		.alg.hash = {
2132497f2e6bSLee Nipper 			.init = ahash_init,
2133497f2e6bSLee Nipper 			.update = ahash_update,
2134497f2e6bSLee Nipper 			.final = ahash_final,
2135497f2e6bSLee Nipper 			.finup = ahash_finup,
2136497f2e6bSLee Nipper 			.digest = ahash_digest,
2137497f2e6bSLee Nipper 			.halg.digestsize = SHA1_DIGEST_SIZE,
2138497f2e6bSLee Nipper 			.halg.base = {
2139497f2e6bSLee Nipper 				.cra_name = "sha1",
2140497f2e6bSLee Nipper 				.cra_driver_name = "sha1-talitos",
2141497f2e6bSLee Nipper 				.cra_blocksize = SHA1_BLOCK_SIZE,
2142497f2e6bSLee Nipper 				.cra_flags = CRYPTO_ALG_TYPE_AHASH |
2143497f2e6bSLee Nipper 					     CRYPTO_ALG_ASYNC,
2144497f2e6bSLee Nipper 				.cra_type = &crypto_ahash_type
2145497f2e6bSLee Nipper 			}
2146497f2e6bSLee Nipper 		},
2147497f2e6bSLee Nipper 		.desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2148497f2e6bSLee Nipper 				     DESC_HDR_SEL0_MDEUA |
2149497f2e6bSLee Nipper 				     DESC_HDR_MODE0_MDEU_SHA1,
2150497f2e6bSLee Nipper 	},
2151497f2e6bSLee Nipper 	{	.type = CRYPTO_ALG_TYPE_AHASH,
2152497f2e6bSLee Nipper 		.alg.hash = {
2153497f2e6bSLee Nipper 			.init = ahash_init,
2154497f2e6bSLee Nipper 			.update = ahash_update,
2155497f2e6bSLee Nipper 			.final = ahash_final,
2156497f2e6bSLee Nipper 			.finup = ahash_finup,
2157497f2e6bSLee Nipper 			.digest = ahash_digest,
215860f208d7SKim Phillips 			.halg.digestsize = SHA224_DIGEST_SIZE,
215960f208d7SKim Phillips 			.halg.base = {
216060f208d7SKim Phillips 				.cra_name = "sha224",
216160f208d7SKim Phillips 				.cra_driver_name = "sha224-talitos",
216260f208d7SKim Phillips 				.cra_blocksize = SHA224_BLOCK_SIZE,
216360f208d7SKim Phillips 				.cra_flags = CRYPTO_ALG_TYPE_AHASH |
216460f208d7SKim Phillips 					     CRYPTO_ALG_ASYNC,
216560f208d7SKim Phillips 				.cra_type = &crypto_ahash_type
216660f208d7SKim Phillips 			}
216760f208d7SKim Phillips 		},
216860f208d7SKim Phillips 		.desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
216960f208d7SKim Phillips 				     DESC_HDR_SEL0_MDEUA |
217060f208d7SKim Phillips 				     DESC_HDR_MODE0_MDEU_SHA224,
217160f208d7SKim Phillips 	},
217260f208d7SKim Phillips 	{	.type = CRYPTO_ALG_TYPE_AHASH,
217360f208d7SKim Phillips 		.alg.hash = {
217460f208d7SKim Phillips 			.init = ahash_init,
217560f208d7SKim Phillips 			.update = ahash_update,
217660f208d7SKim Phillips 			.final = ahash_final,
217760f208d7SKim Phillips 			.finup = ahash_finup,
217860f208d7SKim Phillips 			.digest = ahash_digest,
2179497f2e6bSLee Nipper 			.halg.digestsize = SHA256_DIGEST_SIZE,
2180497f2e6bSLee Nipper 			.halg.base = {
2181497f2e6bSLee Nipper 				.cra_name = "sha256",
2182497f2e6bSLee Nipper 				.cra_driver_name = "sha256-talitos",
2183497f2e6bSLee Nipper 				.cra_blocksize = SHA256_BLOCK_SIZE,
2184497f2e6bSLee Nipper 				.cra_flags = CRYPTO_ALG_TYPE_AHASH |
2185497f2e6bSLee Nipper 					     CRYPTO_ALG_ASYNC,
2186497f2e6bSLee Nipper 				.cra_type = &crypto_ahash_type
2187497f2e6bSLee Nipper 			}
2188497f2e6bSLee Nipper 		},
2189497f2e6bSLee Nipper 		.desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2190497f2e6bSLee Nipper 				     DESC_HDR_SEL0_MDEUA |
2191497f2e6bSLee Nipper 				     DESC_HDR_MODE0_MDEU_SHA256,
2192497f2e6bSLee Nipper 	},
2193497f2e6bSLee Nipper 	{	.type = CRYPTO_ALG_TYPE_AHASH,
2194497f2e6bSLee Nipper 		.alg.hash = {
2195497f2e6bSLee Nipper 			.init = ahash_init,
2196497f2e6bSLee Nipper 			.update = ahash_update,
2197497f2e6bSLee Nipper 			.final = ahash_final,
2198497f2e6bSLee Nipper 			.finup = ahash_finup,
2199497f2e6bSLee Nipper 			.digest = ahash_digest,
2200497f2e6bSLee Nipper 			.halg.digestsize = SHA384_DIGEST_SIZE,
2201497f2e6bSLee Nipper 			.halg.base = {
2202497f2e6bSLee Nipper 				.cra_name = "sha384",
2203497f2e6bSLee Nipper 				.cra_driver_name = "sha384-talitos",
2204497f2e6bSLee Nipper 				.cra_blocksize = SHA384_BLOCK_SIZE,
2205497f2e6bSLee Nipper 				.cra_flags = CRYPTO_ALG_TYPE_AHASH |
2206497f2e6bSLee Nipper 					     CRYPTO_ALG_ASYNC,
2207497f2e6bSLee Nipper 				.cra_type = &crypto_ahash_type
2208497f2e6bSLee Nipper 			}
2209497f2e6bSLee Nipper 		},
2210497f2e6bSLee Nipper 		.desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2211497f2e6bSLee Nipper 				     DESC_HDR_SEL0_MDEUB |
2212497f2e6bSLee Nipper 				     DESC_HDR_MODE0_MDEUB_SHA384,
2213497f2e6bSLee Nipper 	},
2214497f2e6bSLee Nipper 	{	.type = CRYPTO_ALG_TYPE_AHASH,
2215497f2e6bSLee Nipper 		.alg.hash = {
2216497f2e6bSLee Nipper 			.init = ahash_init,
2217497f2e6bSLee Nipper 			.update = ahash_update,
2218497f2e6bSLee Nipper 			.final = ahash_final,
2219497f2e6bSLee Nipper 			.finup = ahash_finup,
2220497f2e6bSLee Nipper 			.digest = ahash_digest,
2221497f2e6bSLee Nipper 			.halg.digestsize = SHA512_DIGEST_SIZE,
2222497f2e6bSLee Nipper 			.halg.base = {
2223497f2e6bSLee Nipper 				.cra_name = "sha512",
2224497f2e6bSLee Nipper 				.cra_driver_name = "sha512-talitos",
2225497f2e6bSLee Nipper 				.cra_blocksize = SHA512_BLOCK_SIZE,
2226497f2e6bSLee Nipper 				.cra_flags = CRYPTO_ALG_TYPE_AHASH |
2227497f2e6bSLee Nipper 					     CRYPTO_ALG_ASYNC,
2228497f2e6bSLee Nipper 				.cra_type = &crypto_ahash_type
2229497f2e6bSLee Nipper 			}
2230497f2e6bSLee Nipper 		},
2231497f2e6bSLee Nipper 		.desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2232497f2e6bSLee Nipper 				     DESC_HDR_SEL0_MDEUB |
2233497f2e6bSLee Nipper 				     DESC_HDR_MODE0_MDEUB_SHA512,
2234497f2e6bSLee Nipper 	},
22359c4a7965SKim Phillips };
22369c4a7965SKim Phillips 
22379c4a7965SKim Phillips struct talitos_crypto_alg {
22389c4a7965SKim Phillips 	struct list_head entry;
22399c4a7965SKim Phillips 	struct device *dev;
2240acbf7c62SLee Nipper 	struct talitos_alg_template algt;
22419c4a7965SKim Phillips };
22429c4a7965SKim Phillips 
22439c4a7965SKim Phillips static int talitos_cra_init(struct crypto_tfm *tfm)
22449c4a7965SKim Phillips {
22459c4a7965SKim Phillips 	struct crypto_alg *alg = tfm->__crt_alg;
224619bbbc63SKim Phillips 	struct talitos_crypto_alg *talitos_alg;
22479c4a7965SKim Phillips 	struct talitos_ctx *ctx = crypto_tfm_ctx(tfm);
22489c4a7965SKim Phillips 
2249497f2e6bSLee Nipper 	if ((alg->cra_flags & CRYPTO_ALG_TYPE_MASK) == CRYPTO_ALG_TYPE_AHASH)
2250497f2e6bSLee Nipper 		talitos_alg = container_of(__crypto_ahash_alg(alg),
2251497f2e6bSLee Nipper 					   struct talitos_crypto_alg,
2252497f2e6bSLee Nipper 					   algt.alg.hash);
2253497f2e6bSLee Nipper 	else
2254acbf7c62SLee Nipper 		talitos_alg = container_of(alg, struct talitos_crypto_alg,
2255acbf7c62SLee Nipper 					   algt.alg.crypto);
225619bbbc63SKim Phillips 
22579c4a7965SKim Phillips 	/* update context with ptr to dev */
22589c4a7965SKim Phillips 	ctx->dev = talitos_alg->dev;
225919bbbc63SKim Phillips 
22609c4a7965SKim Phillips 	/* copy descriptor header template value */
2261acbf7c62SLee Nipper 	ctx->desc_hdr_template = talitos_alg->algt.desc_hdr_template;
22629c4a7965SKim Phillips 
2263497f2e6bSLee Nipper 	return 0;
2264497f2e6bSLee Nipper }
2265497f2e6bSLee Nipper 
2266497f2e6bSLee Nipper static int talitos_cra_init_aead(struct crypto_tfm *tfm)
2267497f2e6bSLee Nipper {
2268497f2e6bSLee Nipper 	struct talitos_ctx *ctx = crypto_tfm_ctx(tfm);
2269497f2e6bSLee Nipper 
2270497f2e6bSLee Nipper 	talitos_cra_init(tfm);
22719c4a7965SKim Phillips 
22729c4a7965SKim Phillips 	/* random first IV */
227370bcaca7SLee Nipper 	get_random_bytes(ctx->iv, TALITOS_MAX_IV_LENGTH);
22749c4a7965SKim Phillips 
22759c4a7965SKim Phillips 	return 0;
22769c4a7965SKim Phillips }
22779c4a7965SKim Phillips 
2278497f2e6bSLee Nipper static int talitos_cra_init_ahash(struct crypto_tfm *tfm)
2279497f2e6bSLee Nipper {
2280497f2e6bSLee Nipper 	struct talitos_ctx *ctx = crypto_tfm_ctx(tfm);
2281497f2e6bSLee Nipper 
2282497f2e6bSLee Nipper 	talitos_cra_init(tfm);
2283497f2e6bSLee Nipper 
2284497f2e6bSLee Nipper 	ctx->keylen = 0;
2285497f2e6bSLee Nipper 	crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
2286497f2e6bSLee Nipper 				 sizeof(struct talitos_ahash_req_ctx));
2287497f2e6bSLee Nipper 
2288497f2e6bSLee Nipper 	return 0;
2289497f2e6bSLee Nipper }
2290497f2e6bSLee Nipper 
22919c4a7965SKim Phillips /*
22929c4a7965SKim Phillips  * given the alg's descriptor header template, determine whether descriptor
22939c4a7965SKim Phillips  * type and primary/secondary execution units required match the hw
22949c4a7965SKim Phillips  * capabilities description provided in the device tree node.
22959c4a7965SKim Phillips  */
22969c4a7965SKim Phillips static int hw_supports(struct device *dev, __be32 desc_hdr_template)
22979c4a7965SKim Phillips {
22989c4a7965SKim Phillips 	struct talitos_private *priv = dev_get_drvdata(dev);
22999c4a7965SKim Phillips 	int ret;
23009c4a7965SKim Phillips 
23019c4a7965SKim Phillips 	ret = (1 << DESC_TYPE(desc_hdr_template) & priv->desc_types) &&
23029c4a7965SKim Phillips 	      (1 << PRIMARY_EU(desc_hdr_template) & priv->exec_units);
23039c4a7965SKim Phillips 
23049c4a7965SKim Phillips 	if (SECONDARY_EU(desc_hdr_template))
23059c4a7965SKim Phillips 		ret = ret && (1 << SECONDARY_EU(desc_hdr_template)
23069c4a7965SKim Phillips 		              & priv->exec_units);
23079c4a7965SKim Phillips 
23089c4a7965SKim Phillips 	return ret;
23099c4a7965SKim Phillips }
23109c4a7965SKim Phillips 
2311596f1034SAl Viro static int talitos_remove(struct of_device *ofdev)
23129c4a7965SKim Phillips {
23139c4a7965SKim Phillips 	struct device *dev = &ofdev->dev;
23149c4a7965SKim Phillips 	struct talitos_private *priv = dev_get_drvdata(dev);
23159c4a7965SKim Phillips 	struct talitos_crypto_alg *t_alg, *n;
23169c4a7965SKim Phillips 	int i;
23179c4a7965SKim Phillips 
23189c4a7965SKim Phillips 	list_for_each_entry_safe(t_alg, n, &priv->alg_list, entry) {
2319acbf7c62SLee Nipper 		switch (t_alg->algt.type) {
2320acbf7c62SLee Nipper 		case CRYPTO_ALG_TYPE_ABLKCIPHER:
2321acbf7c62SLee Nipper 		case CRYPTO_ALG_TYPE_AEAD:
2322acbf7c62SLee Nipper 			crypto_unregister_alg(&t_alg->algt.alg.crypto);
2323acbf7c62SLee Nipper 			break;
2324acbf7c62SLee Nipper 		case CRYPTO_ALG_TYPE_AHASH:
2325acbf7c62SLee Nipper 			crypto_unregister_ahash(&t_alg->algt.alg.hash);
2326acbf7c62SLee Nipper 			break;
2327acbf7c62SLee Nipper 		}
23289c4a7965SKim Phillips 		list_del(&t_alg->entry);
23299c4a7965SKim Phillips 		kfree(t_alg);
23309c4a7965SKim Phillips 	}
23319c4a7965SKim Phillips 
23329c4a7965SKim Phillips 	if (hw_supports(dev, DESC_HDR_SEL0_RNG))
23339c4a7965SKim Phillips 		talitos_unregister_rng(dev);
23349c4a7965SKim Phillips 
23359c4a7965SKim Phillips 	for (i = 0; i < priv->num_channels; i++)
23364b992628SKim Phillips 		if (priv->chan[i].fifo)
23374b992628SKim Phillips 			kfree(priv->chan[i].fifo);
23389c4a7965SKim Phillips 
23394b992628SKim Phillips 	kfree(priv->chan);
23409c4a7965SKim Phillips 
23419c4a7965SKim Phillips 	if (priv->irq != NO_IRQ) {
23429c4a7965SKim Phillips 		free_irq(priv->irq, dev);
23439c4a7965SKim Phillips 		irq_dispose_mapping(priv->irq);
23449c4a7965SKim Phillips 	}
23459c4a7965SKim Phillips 
23469c4a7965SKim Phillips 	tasklet_kill(&priv->done_task);
23479c4a7965SKim Phillips 
23489c4a7965SKim Phillips 	iounmap(priv->reg);
23499c4a7965SKim Phillips 
23509c4a7965SKim Phillips 	dev_set_drvdata(dev, NULL);
23519c4a7965SKim Phillips 
23529c4a7965SKim Phillips 	kfree(priv);
23539c4a7965SKim Phillips 
23549c4a7965SKim Phillips 	return 0;
23559c4a7965SKim Phillips }
23569c4a7965SKim Phillips 
23579c4a7965SKim Phillips static struct talitos_crypto_alg *talitos_alg_alloc(struct device *dev,
23589c4a7965SKim Phillips 						    struct talitos_alg_template
23599c4a7965SKim Phillips 						           *template)
23609c4a7965SKim Phillips {
236160f208d7SKim Phillips 	struct talitos_private *priv = dev_get_drvdata(dev);
23629c4a7965SKim Phillips 	struct talitos_crypto_alg *t_alg;
23639c4a7965SKim Phillips 	struct crypto_alg *alg;
23649c4a7965SKim Phillips 
23659c4a7965SKim Phillips 	t_alg = kzalloc(sizeof(struct talitos_crypto_alg), GFP_KERNEL);
23669c4a7965SKim Phillips 	if (!t_alg)
23679c4a7965SKim Phillips 		return ERR_PTR(-ENOMEM);
23689c4a7965SKim Phillips 
2369acbf7c62SLee Nipper 	t_alg->algt = *template;
2370acbf7c62SLee Nipper 
2371acbf7c62SLee Nipper 	switch (t_alg->algt.type) {
2372acbf7c62SLee Nipper 	case CRYPTO_ALG_TYPE_ABLKCIPHER:
2373497f2e6bSLee Nipper 		alg = &t_alg->algt.alg.crypto;
2374497f2e6bSLee Nipper 		alg->cra_init = talitos_cra_init;
2375497f2e6bSLee Nipper 		break;
2376acbf7c62SLee Nipper 	case CRYPTO_ALG_TYPE_AEAD:
2377acbf7c62SLee Nipper 		alg = &t_alg->algt.alg.crypto;
2378497f2e6bSLee Nipper 		alg->cra_init = talitos_cra_init_aead;
2379acbf7c62SLee Nipper 		break;
2380acbf7c62SLee Nipper 	case CRYPTO_ALG_TYPE_AHASH:
2381acbf7c62SLee Nipper 		alg = &t_alg->algt.alg.hash.halg.base;
2382497f2e6bSLee Nipper 		alg->cra_init = talitos_cra_init_ahash;
238360f208d7SKim Phillips 		if (!(priv->features & TALITOS_FTR_SHA224_HWINIT) &&
238460f208d7SKim Phillips 		    !strcmp(alg->cra_name, "sha224")) {
238560f208d7SKim Phillips 			t_alg->algt.alg.hash.init = ahash_init_sha224_swinit;
238660f208d7SKim Phillips 			t_alg->algt.desc_hdr_template =
238760f208d7SKim Phillips 					DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
238860f208d7SKim Phillips 					DESC_HDR_SEL0_MDEUA |
238960f208d7SKim Phillips 					DESC_HDR_MODE0_MDEU_SHA256;
239060f208d7SKim Phillips 		}
2391497f2e6bSLee Nipper 		break;
2392*1d11911aSKim Phillips 	default:
2393*1d11911aSKim Phillips 		dev_err(dev, "unknown algorithm type %d\n", t_alg->algt.type);
2394*1d11911aSKim Phillips 		return ERR_PTR(-EINVAL);
2395acbf7c62SLee Nipper 	}
23969c4a7965SKim Phillips 
23979c4a7965SKim Phillips 	alg->cra_module = THIS_MODULE;
23989c4a7965SKim Phillips 	alg->cra_priority = TALITOS_CRA_PRIORITY;
23999c4a7965SKim Phillips 	alg->cra_alignmask = 0;
24009c4a7965SKim Phillips 	alg->cra_ctxsize = sizeof(struct talitos_ctx);
24019c4a7965SKim Phillips 
24029c4a7965SKim Phillips 	t_alg->dev = dev;
24039c4a7965SKim Phillips 
24049c4a7965SKim Phillips 	return t_alg;
24059c4a7965SKim Phillips }
24069c4a7965SKim Phillips 
24079c4a7965SKim Phillips static int talitos_probe(struct of_device *ofdev,
24089c4a7965SKim Phillips 			 const struct of_device_id *match)
24099c4a7965SKim Phillips {
24109c4a7965SKim Phillips 	struct device *dev = &ofdev->dev;
241161c7a080SGrant Likely 	struct device_node *np = ofdev->dev.of_node;
24129c4a7965SKim Phillips 	struct talitos_private *priv;
24139c4a7965SKim Phillips 	const unsigned int *prop;
24149c4a7965SKim Phillips 	int i, err;
24159c4a7965SKim Phillips 
24169c4a7965SKim Phillips 	priv = kzalloc(sizeof(struct talitos_private), GFP_KERNEL);
24179c4a7965SKim Phillips 	if (!priv)
24189c4a7965SKim Phillips 		return -ENOMEM;
24199c4a7965SKim Phillips 
24209c4a7965SKim Phillips 	dev_set_drvdata(dev, priv);
24219c4a7965SKim Phillips 
24229c4a7965SKim Phillips 	priv->ofdev = ofdev;
24239c4a7965SKim Phillips 
24249c4a7965SKim Phillips 	tasklet_init(&priv->done_task, talitos_done, (unsigned long)dev);
24259c4a7965SKim Phillips 
2426fe5720e2SKim Phillips 	INIT_LIST_HEAD(&priv->alg_list);
2427fe5720e2SKim Phillips 
24289c4a7965SKim Phillips 	priv->irq = irq_of_parse_and_map(np, 0);
24299c4a7965SKim Phillips 
24309c4a7965SKim Phillips 	if (priv->irq == NO_IRQ) {
24319c4a7965SKim Phillips 		dev_err(dev, "failed to map irq\n");
24329c4a7965SKim Phillips 		err = -EINVAL;
24339c4a7965SKim Phillips 		goto err_out;
24349c4a7965SKim Phillips 	}
24359c4a7965SKim Phillips 
24369c4a7965SKim Phillips 	/* get the irq line */
24379c4a7965SKim Phillips 	err = request_irq(priv->irq, talitos_interrupt, 0,
24389c4a7965SKim Phillips 			  dev_driver_string(dev), dev);
24399c4a7965SKim Phillips 	if (err) {
24409c4a7965SKim Phillips 		dev_err(dev, "failed to request irq %d\n", priv->irq);
24419c4a7965SKim Phillips 		irq_dispose_mapping(priv->irq);
24429c4a7965SKim Phillips 		priv->irq = NO_IRQ;
24439c4a7965SKim Phillips 		goto err_out;
24449c4a7965SKim Phillips 	}
24459c4a7965SKim Phillips 
24469c4a7965SKim Phillips 	priv->reg = of_iomap(np, 0);
24479c4a7965SKim Phillips 	if (!priv->reg) {
24489c4a7965SKim Phillips 		dev_err(dev, "failed to of_iomap\n");
24499c4a7965SKim Phillips 		err = -ENOMEM;
24509c4a7965SKim Phillips 		goto err_out;
24519c4a7965SKim Phillips 	}
24529c4a7965SKim Phillips 
24539c4a7965SKim Phillips 	/* get SEC version capabilities from device tree */
24549c4a7965SKim Phillips 	prop = of_get_property(np, "fsl,num-channels", NULL);
24559c4a7965SKim Phillips 	if (prop)
24569c4a7965SKim Phillips 		priv->num_channels = *prop;
24579c4a7965SKim Phillips 
24589c4a7965SKim Phillips 	prop = of_get_property(np, "fsl,channel-fifo-len", NULL);
24599c4a7965SKim Phillips 	if (prop)
24609c4a7965SKim Phillips 		priv->chfifo_len = *prop;
24619c4a7965SKim Phillips 
24629c4a7965SKim Phillips 	prop = of_get_property(np, "fsl,exec-units-mask", NULL);
24639c4a7965SKim Phillips 	if (prop)
24649c4a7965SKim Phillips 		priv->exec_units = *prop;
24659c4a7965SKim Phillips 
24669c4a7965SKim Phillips 	prop = of_get_property(np, "fsl,descriptor-types-mask", NULL);
24679c4a7965SKim Phillips 	if (prop)
24689c4a7965SKim Phillips 		priv->desc_types = *prop;
24699c4a7965SKim Phillips 
24709c4a7965SKim Phillips 	if (!is_power_of_2(priv->num_channels) || !priv->chfifo_len ||
24719c4a7965SKim Phillips 	    !priv->exec_units || !priv->desc_types) {
24729c4a7965SKim Phillips 		dev_err(dev, "invalid property data in device tree node\n");
24739c4a7965SKim Phillips 		err = -EINVAL;
24749c4a7965SKim Phillips 		goto err_out;
24759c4a7965SKim Phillips 	}
24769c4a7965SKim Phillips 
2477f3c85bc1SLee Nipper 	if (of_device_is_compatible(np, "fsl,sec3.0"))
2478f3c85bc1SLee Nipper 		priv->features |= TALITOS_FTR_SRC_LINK_TBL_LEN_INCLUDES_EXTENT;
2479f3c85bc1SLee Nipper 
2480fe5720e2SKim Phillips 	if (of_device_is_compatible(np, "fsl,sec2.1"))
248160f208d7SKim Phillips 		priv->features |= TALITOS_FTR_HW_AUTH_CHECK |
248260f208d7SKim Phillips 				  TALITOS_FTR_SHA224_HWINIT;
2483fe5720e2SKim Phillips 
24844b992628SKim Phillips 	priv->chan = kzalloc(sizeof(struct talitos_channel) *
24854b992628SKim Phillips 			     priv->num_channels, GFP_KERNEL);
24864b992628SKim Phillips 	if (!priv->chan) {
24874b992628SKim Phillips 		dev_err(dev, "failed to allocate channel management space\n");
24889c4a7965SKim Phillips 		err = -ENOMEM;
24899c4a7965SKim Phillips 		goto err_out;
24909c4a7965SKim Phillips 	}
24919c4a7965SKim Phillips 
24929c4a7965SKim Phillips 	for (i = 0; i < priv->num_channels; i++) {
24934b992628SKim Phillips 		spin_lock_init(&priv->chan[i].head_lock);
24944b992628SKim Phillips 		spin_lock_init(&priv->chan[i].tail_lock);
24959c4a7965SKim Phillips 	}
24969c4a7965SKim Phillips 
24979c4a7965SKim Phillips 	priv->fifo_len = roundup_pow_of_two(priv->chfifo_len);
24989c4a7965SKim Phillips 
24999c4a7965SKim Phillips 	for (i = 0; i < priv->num_channels; i++) {
25004b992628SKim Phillips 		priv->chan[i].fifo = kzalloc(sizeof(struct talitos_request) *
25019c4a7965SKim Phillips 					     priv->fifo_len, GFP_KERNEL);
25024b992628SKim Phillips 		if (!priv->chan[i].fifo) {
25039c4a7965SKim Phillips 			dev_err(dev, "failed to allocate request fifo %d\n", i);
25049c4a7965SKim Phillips 			err = -ENOMEM;
25059c4a7965SKim Phillips 			goto err_out;
25069c4a7965SKim Phillips 		}
25079c4a7965SKim Phillips 	}
25089c4a7965SKim Phillips 
2509ec6644d6SKim Phillips 	for (i = 0; i < priv->num_channels; i++)
25104b992628SKim Phillips 		atomic_set(&priv->chan[i].submit_count,
25114b992628SKim Phillips 			   -(priv->chfifo_len - 1));
25129c4a7965SKim Phillips 
251381eb024cSKim Phillips 	dma_set_mask(dev, DMA_BIT_MASK(36));
251481eb024cSKim Phillips 
25159c4a7965SKim Phillips 	/* reset and initialize the h/w */
25169c4a7965SKim Phillips 	err = init_device(dev);
25179c4a7965SKim Phillips 	if (err) {
25189c4a7965SKim Phillips 		dev_err(dev, "failed to initialize device\n");
25199c4a7965SKim Phillips 		goto err_out;
25209c4a7965SKim Phillips 	}
25219c4a7965SKim Phillips 
25229c4a7965SKim Phillips 	/* register the RNG, if available */
25239c4a7965SKim Phillips 	if (hw_supports(dev, DESC_HDR_SEL0_RNG)) {
25249c4a7965SKim Phillips 		err = talitos_register_rng(dev);
25259c4a7965SKim Phillips 		if (err) {
25269c4a7965SKim Phillips 			dev_err(dev, "failed to register hwrng: %d\n", err);
25279c4a7965SKim Phillips 			goto err_out;
25289c4a7965SKim Phillips 		} else
25299c4a7965SKim Phillips 			dev_info(dev, "hwrng\n");
25309c4a7965SKim Phillips 	}
25319c4a7965SKim Phillips 
25329c4a7965SKim Phillips 	/* register crypto algorithms the device supports */
25339c4a7965SKim Phillips 	for (i = 0; i < ARRAY_SIZE(driver_algs); i++) {
25349c4a7965SKim Phillips 		if (hw_supports(dev, driver_algs[i].desc_hdr_template)) {
25359c4a7965SKim Phillips 			struct talitos_crypto_alg *t_alg;
2536acbf7c62SLee Nipper 			char *name = NULL;
25379c4a7965SKim Phillips 
25389c4a7965SKim Phillips 			t_alg = talitos_alg_alloc(dev, &driver_algs[i]);
25399c4a7965SKim Phillips 			if (IS_ERR(t_alg)) {
25409c4a7965SKim Phillips 				err = PTR_ERR(t_alg);
25419c4a7965SKim Phillips 				goto err_out;
25429c4a7965SKim Phillips 			}
25439c4a7965SKim Phillips 
2544acbf7c62SLee Nipper 			switch (t_alg->algt.type) {
2545acbf7c62SLee Nipper 			case CRYPTO_ALG_TYPE_ABLKCIPHER:
2546acbf7c62SLee Nipper 			case CRYPTO_ALG_TYPE_AEAD:
2547acbf7c62SLee Nipper 				err = crypto_register_alg(
2548acbf7c62SLee Nipper 						&t_alg->algt.alg.crypto);
2549acbf7c62SLee Nipper 				name = t_alg->algt.alg.crypto.cra_driver_name;
2550acbf7c62SLee Nipper 				break;
2551acbf7c62SLee Nipper 			case CRYPTO_ALG_TYPE_AHASH:
2552acbf7c62SLee Nipper 				err = crypto_register_ahash(
2553acbf7c62SLee Nipper 						&t_alg->algt.alg.hash);
2554acbf7c62SLee Nipper 				name =
2555acbf7c62SLee Nipper 				 t_alg->algt.alg.hash.halg.base.cra_driver_name;
2556acbf7c62SLee Nipper 				break;
2557acbf7c62SLee Nipper 			}
25589c4a7965SKim Phillips 			if (err) {
25599c4a7965SKim Phillips 				dev_err(dev, "%s alg registration failed\n",
2560acbf7c62SLee Nipper 					name);
25619c4a7965SKim Phillips 				kfree(t_alg);
25629c4a7965SKim Phillips 			} else {
25639c4a7965SKim Phillips 				list_add_tail(&t_alg->entry, &priv->alg_list);
2564acbf7c62SLee Nipper 				dev_info(dev, "%s\n", name);
25659c4a7965SKim Phillips 			}
25669c4a7965SKim Phillips 		}
25679c4a7965SKim Phillips 	}
25689c4a7965SKim Phillips 
25699c4a7965SKim Phillips 	return 0;
25709c4a7965SKim Phillips 
25719c4a7965SKim Phillips err_out:
25729c4a7965SKim Phillips 	talitos_remove(ofdev);
25739c4a7965SKim Phillips 
25749c4a7965SKim Phillips 	return err;
25759c4a7965SKim Phillips }
25769c4a7965SKim Phillips 
25776c3f975aSMárton Németh static const struct of_device_id talitos_match[] = {
25789c4a7965SKim Phillips 	{
25799c4a7965SKim Phillips 		.compatible = "fsl,sec2.0",
25809c4a7965SKim Phillips 	},
25819c4a7965SKim Phillips 	{},
25829c4a7965SKim Phillips };
25839c4a7965SKim Phillips MODULE_DEVICE_TABLE(of, talitos_match);
25849c4a7965SKim Phillips 
25859c4a7965SKim Phillips static struct of_platform_driver talitos_driver = {
25864018294bSGrant Likely 	.driver = {
25879c4a7965SKim Phillips 		.name = "talitos",
25884018294bSGrant Likely 		.owner = THIS_MODULE,
25894018294bSGrant Likely 		.of_match_table = talitos_match,
25904018294bSGrant Likely 	},
25919c4a7965SKim Phillips 	.probe = talitos_probe,
2592596f1034SAl Viro 	.remove = talitos_remove,
25939c4a7965SKim Phillips };
25949c4a7965SKim Phillips 
25959c4a7965SKim Phillips static int __init talitos_init(void)
25969c4a7965SKim Phillips {
25979c4a7965SKim Phillips 	return of_register_platform_driver(&talitos_driver);
25989c4a7965SKim Phillips }
25999c4a7965SKim Phillips module_init(talitos_init);
26009c4a7965SKim Phillips 
26019c4a7965SKim Phillips static void __exit talitos_exit(void)
26029c4a7965SKim Phillips {
26039c4a7965SKim Phillips 	of_unregister_platform_driver(&talitos_driver);
26049c4a7965SKim Phillips }
26059c4a7965SKim Phillips module_exit(talitos_exit);
26069c4a7965SKim Phillips 
26079c4a7965SKim Phillips MODULE_LICENSE("GPL");
26089c4a7965SKim Phillips MODULE_AUTHOR("Kim Phillips <kim.phillips@freescale.com>");
26099c4a7965SKim Phillips MODULE_DESCRIPTION("Freescale integrated security engine (SEC) driver");
2610