xref: /linux/drivers/crypto/talitos.c (revision 4de9d0b547b97e40c93a885ac6246c2c5fef05cb)
19c4a7965SKim Phillips /*
29c4a7965SKim Phillips  * talitos - Freescale Integrated Security Engine (SEC) device driver
39c4a7965SKim Phillips  *
49c4a7965SKim Phillips  * Copyright (c) 2008 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>
409c4a7965SKim Phillips 
419c4a7965SKim Phillips #include <crypto/algapi.h>
429c4a7965SKim Phillips #include <crypto/aes.h>
433952f17eSLee Nipper #include <crypto/des.h>
449c4a7965SKim Phillips #include <crypto/sha.h>
459c4a7965SKim Phillips #include <crypto/aead.h>
469c4a7965SKim Phillips #include <crypto/authenc.h>
47*4de9d0b5SLee Nipper #include <crypto/skcipher.h>
48*4de9d0b5SLee Nipper #include <crypto/scatterwalk.h>
499c4a7965SKim Phillips 
509c4a7965SKim Phillips #include "talitos.h"
519c4a7965SKim Phillips 
529c4a7965SKim Phillips #define TALITOS_TIMEOUT 100000
539c4a7965SKim Phillips #define TALITOS_MAX_DATA_LEN 65535
549c4a7965SKim Phillips 
559c4a7965SKim Phillips #define DESC_TYPE(desc_hdr) ((be32_to_cpu(desc_hdr) >> 3) & 0x1f)
569c4a7965SKim Phillips #define PRIMARY_EU(desc_hdr) ((be32_to_cpu(desc_hdr) >> 28) & 0xf)
579c4a7965SKim Phillips #define SECONDARY_EU(desc_hdr) ((be32_to_cpu(desc_hdr) >> 16) & 0xf)
589c4a7965SKim Phillips 
599c4a7965SKim Phillips /* descriptor pointer entry */
609c4a7965SKim Phillips struct talitos_ptr {
619c4a7965SKim Phillips 	__be16 len;	/* length */
629c4a7965SKim Phillips 	u8 j_extent;	/* jump to sg link table and/or extent */
639c4a7965SKim Phillips 	u8 eptr;	/* extended address */
649c4a7965SKim Phillips 	__be32 ptr;	/* address */
659c4a7965SKim Phillips };
669c4a7965SKim Phillips 
679c4a7965SKim Phillips /* descriptor */
689c4a7965SKim Phillips struct talitos_desc {
699c4a7965SKim Phillips 	__be32 hdr;			/* header high bits */
709c4a7965SKim Phillips 	__be32 hdr_lo;			/* header low bits */
719c4a7965SKim Phillips 	struct talitos_ptr ptr[7];	/* ptr/len pair array */
729c4a7965SKim Phillips };
739c4a7965SKim Phillips 
749c4a7965SKim Phillips /**
759c4a7965SKim Phillips  * talitos_request - descriptor submission request
769c4a7965SKim Phillips  * @desc: descriptor pointer (kernel virtual)
779c4a7965SKim Phillips  * @dma_desc: descriptor's physical bus address
789c4a7965SKim Phillips  * @callback: whom to call when descriptor processing is done
799c4a7965SKim Phillips  * @context: caller context (optional)
809c4a7965SKim Phillips  */
819c4a7965SKim Phillips struct talitos_request {
829c4a7965SKim Phillips 	struct talitos_desc *desc;
839c4a7965SKim Phillips 	dma_addr_t dma_desc;
849c4a7965SKim Phillips 	void (*callback) (struct device *dev, struct talitos_desc *desc,
859c4a7965SKim Phillips 	                  void *context, int error);
869c4a7965SKim Phillips 	void *context;
879c4a7965SKim Phillips };
889c4a7965SKim Phillips 
899c4a7965SKim Phillips struct talitos_private {
909c4a7965SKim Phillips 	struct device *dev;
919c4a7965SKim Phillips 	struct of_device *ofdev;
929c4a7965SKim Phillips 	void __iomem *reg;
939c4a7965SKim Phillips 	int irq;
949c4a7965SKim Phillips 
959c4a7965SKim Phillips 	/* SEC version geometry (from device tree node) */
969c4a7965SKim Phillips 	unsigned int num_channels;
979c4a7965SKim Phillips 	unsigned int chfifo_len;
989c4a7965SKim Phillips 	unsigned int exec_units;
999c4a7965SKim Phillips 	unsigned int desc_types;
1009c4a7965SKim Phillips 
101f3c85bc1SLee Nipper 	/* SEC Compatibility info */
102f3c85bc1SLee Nipper 	unsigned long features;
103f3c85bc1SLee Nipper 
1049c4a7965SKim Phillips 	/* next channel to be assigned next incoming descriptor */
1059c4a7965SKim Phillips 	atomic_t last_chan;
1069c4a7965SKim Phillips 
107ec6644d6SKim Phillips 	/* per-channel number of requests pending in channel h/w fifo */
108ec6644d6SKim Phillips 	atomic_t *submit_count;
109ec6644d6SKim Phillips 
1109c4a7965SKim Phillips 	/* per-channel request fifo */
1119c4a7965SKim Phillips 	struct talitos_request **fifo;
1129c4a7965SKim Phillips 
1139c4a7965SKim Phillips 	/*
1149c4a7965SKim Phillips 	 * length of the request fifo
1159c4a7965SKim Phillips 	 * fifo_len is chfifo_len rounded up to next power of 2
1169c4a7965SKim Phillips 	 * so we can use bitwise ops to wrap
1179c4a7965SKim Phillips 	 */
1189c4a7965SKim Phillips 	unsigned int fifo_len;
1199c4a7965SKim Phillips 
1209c4a7965SKim Phillips 	/* per-channel index to next free descriptor request */
1219c4a7965SKim Phillips 	int *head;
1229c4a7965SKim Phillips 
1239c4a7965SKim Phillips 	/* per-channel index to next in-progress/done descriptor request */
1249c4a7965SKim Phillips 	int *tail;
1259c4a7965SKim Phillips 
1269c4a7965SKim Phillips 	/* per-channel request submission (head) and release (tail) locks */
1279c4a7965SKim Phillips 	spinlock_t *head_lock;
1289c4a7965SKim Phillips 	spinlock_t *tail_lock;
1299c4a7965SKim Phillips 
1309c4a7965SKim Phillips 	/* request callback tasklet */
1319c4a7965SKim Phillips 	struct tasklet_struct done_task;
1329c4a7965SKim Phillips 
1339c4a7965SKim Phillips 	/* list of registered algorithms */
1349c4a7965SKim Phillips 	struct list_head alg_list;
1359c4a7965SKim Phillips 
1369c4a7965SKim Phillips 	/* hwrng device */
1379c4a7965SKim Phillips 	struct hwrng rng;
1389c4a7965SKim Phillips };
1399c4a7965SKim Phillips 
140f3c85bc1SLee Nipper /* .features flag */
141f3c85bc1SLee Nipper #define TALITOS_FTR_SRC_LINK_TBL_LEN_INCLUDES_EXTENT 0x00000001
142fe5720e2SKim Phillips #define TALITOS_FTR_HW_AUTH_CHECK 0x00000002
143f3c85bc1SLee Nipper 
1449c4a7965SKim Phillips /*
1459c4a7965SKim Phillips  * map virtual single (contiguous) pointer to h/w descriptor pointer
1469c4a7965SKim Phillips  */
1479c4a7965SKim Phillips static void map_single_talitos_ptr(struct device *dev,
1489c4a7965SKim Phillips 				   struct talitos_ptr *talitos_ptr,
1499c4a7965SKim Phillips 				   unsigned short len, void *data,
1509c4a7965SKim Phillips 				   unsigned char extent,
1519c4a7965SKim Phillips 				   enum dma_data_direction dir)
1529c4a7965SKim Phillips {
1539c4a7965SKim Phillips 	talitos_ptr->len = cpu_to_be16(len);
1549c4a7965SKim Phillips 	talitos_ptr->ptr = cpu_to_be32(dma_map_single(dev, data, len, dir));
1559c4a7965SKim Phillips 	talitos_ptr->j_extent = extent;
1569c4a7965SKim Phillips }
1579c4a7965SKim Phillips 
1589c4a7965SKim Phillips /*
1599c4a7965SKim Phillips  * unmap bus single (contiguous) h/w descriptor pointer
1609c4a7965SKim Phillips  */
1619c4a7965SKim Phillips static void unmap_single_talitos_ptr(struct device *dev,
1629c4a7965SKim Phillips 				     struct talitos_ptr *talitos_ptr,
1639c4a7965SKim Phillips 				     enum dma_data_direction dir)
1649c4a7965SKim Phillips {
1659c4a7965SKim Phillips 	dma_unmap_single(dev, be32_to_cpu(talitos_ptr->ptr),
1669c4a7965SKim Phillips 			 be16_to_cpu(talitos_ptr->len), dir);
1679c4a7965SKim Phillips }
1689c4a7965SKim Phillips 
1699c4a7965SKim Phillips static int reset_channel(struct device *dev, int ch)
1709c4a7965SKim Phillips {
1719c4a7965SKim Phillips 	struct talitos_private *priv = dev_get_drvdata(dev);
1729c4a7965SKim Phillips 	unsigned int timeout = TALITOS_TIMEOUT;
1739c4a7965SKim Phillips 
1749c4a7965SKim Phillips 	setbits32(priv->reg + TALITOS_CCCR(ch), TALITOS_CCCR_RESET);
1759c4a7965SKim Phillips 
1769c4a7965SKim Phillips 	while ((in_be32(priv->reg + TALITOS_CCCR(ch)) & TALITOS_CCCR_RESET)
1779c4a7965SKim Phillips 	       && --timeout)
1789c4a7965SKim Phillips 		cpu_relax();
1799c4a7965SKim Phillips 
1809c4a7965SKim Phillips 	if (timeout == 0) {
1819c4a7965SKim Phillips 		dev_err(dev, "failed to reset channel %d\n", ch);
1829c4a7965SKim Phillips 		return -EIO;
1839c4a7965SKim Phillips 	}
1849c4a7965SKim Phillips 
1859c4a7965SKim Phillips 	/* set done writeback and IRQ */
1869c4a7965SKim Phillips 	setbits32(priv->reg + TALITOS_CCCR_LO(ch), TALITOS_CCCR_LO_CDWE |
1879c4a7965SKim Phillips 		  TALITOS_CCCR_LO_CDIE);
1889c4a7965SKim Phillips 
189fe5720e2SKim Phillips 	/* and ICCR writeback, if available */
190fe5720e2SKim Phillips 	if (priv->features & TALITOS_FTR_HW_AUTH_CHECK)
191fe5720e2SKim Phillips 		setbits32(priv->reg + TALITOS_CCCR_LO(ch),
192fe5720e2SKim Phillips 		          TALITOS_CCCR_LO_IWSE);
193fe5720e2SKim Phillips 
1949c4a7965SKim Phillips 	return 0;
1959c4a7965SKim Phillips }
1969c4a7965SKim Phillips 
1979c4a7965SKim Phillips static int reset_device(struct device *dev)
1989c4a7965SKim Phillips {
1999c4a7965SKim Phillips 	struct talitos_private *priv = dev_get_drvdata(dev);
2009c4a7965SKim Phillips 	unsigned int timeout = TALITOS_TIMEOUT;
2019c4a7965SKim Phillips 
2029c4a7965SKim Phillips 	setbits32(priv->reg + TALITOS_MCR, TALITOS_MCR_SWR);
2039c4a7965SKim Phillips 
2049c4a7965SKim Phillips 	while ((in_be32(priv->reg + TALITOS_MCR) & TALITOS_MCR_SWR)
2059c4a7965SKim Phillips 	       && --timeout)
2069c4a7965SKim Phillips 		cpu_relax();
2079c4a7965SKim Phillips 
2089c4a7965SKim Phillips 	if (timeout == 0) {
2099c4a7965SKim Phillips 		dev_err(dev, "failed to reset device\n");
2109c4a7965SKim Phillips 		return -EIO;
2119c4a7965SKim Phillips 	}
2129c4a7965SKim Phillips 
2139c4a7965SKim Phillips 	return 0;
2149c4a7965SKim Phillips }
2159c4a7965SKim Phillips 
2169c4a7965SKim Phillips /*
2179c4a7965SKim Phillips  * Reset and initialize the device
2189c4a7965SKim Phillips  */
2199c4a7965SKim Phillips static int init_device(struct device *dev)
2209c4a7965SKim Phillips {
2219c4a7965SKim Phillips 	struct talitos_private *priv = dev_get_drvdata(dev);
2229c4a7965SKim Phillips 	int ch, err;
2239c4a7965SKim Phillips 
2249c4a7965SKim Phillips 	/*
2259c4a7965SKim Phillips 	 * Master reset
2269c4a7965SKim Phillips 	 * errata documentation: warning: certain SEC interrupts
2279c4a7965SKim Phillips 	 * are not fully cleared by writing the MCR:SWR bit,
2289c4a7965SKim Phillips 	 * set bit twice to completely reset
2299c4a7965SKim Phillips 	 */
2309c4a7965SKim Phillips 	err = reset_device(dev);
2319c4a7965SKim Phillips 	if (err)
2329c4a7965SKim Phillips 		return err;
2339c4a7965SKim Phillips 
2349c4a7965SKim Phillips 	err = reset_device(dev);
2359c4a7965SKim Phillips 	if (err)
2369c4a7965SKim Phillips 		return err;
2379c4a7965SKim Phillips 
2389c4a7965SKim Phillips 	/* reset channels */
2399c4a7965SKim Phillips 	for (ch = 0; ch < priv->num_channels; ch++) {
2409c4a7965SKim Phillips 		err = reset_channel(dev, ch);
2419c4a7965SKim Phillips 		if (err)
2429c4a7965SKim Phillips 			return err;
2439c4a7965SKim Phillips 	}
2449c4a7965SKim Phillips 
2459c4a7965SKim Phillips 	/* enable channel done and error interrupts */
2469c4a7965SKim Phillips 	setbits32(priv->reg + TALITOS_IMR, TALITOS_IMR_INIT);
2479c4a7965SKim Phillips 	setbits32(priv->reg + TALITOS_IMR_LO, TALITOS_IMR_LO_INIT);
2489c4a7965SKim Phillips 
249fe5720e2SKim Phillips 	/* disable integrity check error interrupts (use writeback instead) */
250fe5720e2SKim Phillips 	if (priv->features & TALITOS_FTR_HW_AUTH_CHECK)
251fe5720e2SKim Phillips 		setbits32(priv->reg + TALITOS_MDEUICR_LO,
252fe5720e2SKim Phillips 		          TALITOS_MDEUICR_LO_ICE);
253fe5720e2SKim Phillips 
2549c4a7965SKim Phillips 	return 0;
2559c4a7965SKim Phillips }
2569c4a7965SKim Phillips 
2579c4a7965SKim Phillips /**
2589c4a7965SKim Phillips  * talitos_submit - submits a descriptor to the device for processing
2599c4a7965SKim Phillips  * @dev:	the SEC device to be used
2609c4a7965SKim Phillips  * @desc:	the descriptor to be processed by the device
2619c4a7965SKim Phillips  * @callback:	whom to call when processing is complete
2629c4a7965SKim Phillips  * @context:	a handle for use by caller (optional)
2639c4a7965SKim Phillips  *
2649c4a7965SKim Phillips  * desc must contain valid dma-mapped (bus physical) address pointers.
2659c4a7965SKim Phillips  * callback must check err and feedback in descriptor header
2669c4a7965SKim Phillips  * for device processing status.
2679c4a7965SKim Phillips  */
2689c4a7965SKim Phillips static int talitos_submit(struct device *dev, struct talitos_desc *desc,
2699c4a7965SKim Phillips 			  void (*callback)(struct device *dev,
2709c4a7965SKim Phillips 					   struct talitos_desc *desc,
2719c4a7965SKim Phillips 					   void *context, int error),
2729c4a7965SKim Phillips 			  void *context)
2739c4a7965SKim Phillips {
2749c4a7965SKim Phillips 	struct talitos_private *priv = dev_get_drvdata(dev);
2759c4a7965SKim Phillips 	struct talitos_request *request;
2769c4a7965SKim Phillips 	unsigned long flags, ch;
2779c4a7965SKim Phillips 	int head;
2789c4a7965SKim Phillips 
2799c4a7965SKim Phillips 	/* select done notification */
2809c4a7965SKim Phillips 	desc->hdr |= DESC_HDR_DONE_NOTIFY;
2819c4a7965SKim Phillips 
2829c4a7965SKim Phillips 	/* emulate SEC's round-robin channel fifo polling scheme */
2839c4a7965SKim Phillips 	ch = atomic_inc_return(&priv->last_chan) & (priv->num_channels - 1);
2849c4a7965SKim Phillips 
2859c4a7965SKim Phillips 	spin_lock_irqsave(&priv->head_lock[ch], flags);
2869c4a7965SKim Phillips 
287ec6644d6SKim Phillips 	if (!atomic_inc_not_zero(&priv->submit_count[ch])) {
288ec6644d6SKim Phillips 		/* h/w fifo is full */
2899c4a7965SKim Phillips 		spin_unlock_irqrestore(&priv->head_lock[ch], flags);
2909c4a7965SKim Phillips 		return -EAGAIN;
2919c4a7965SKim Phillips 	}
2929c4a7965SKim Phillips 
293ec6644d6SKim Phillips 	head = priv->head[ch];
294ec6644d6SKim Phillips 	request = &priv->fifo[ch][head];
295ec6644d6SKim Phillips 
2969c4a7965SKim Phillips 	/* map descriptor and save caller data */
2979c4a7965SKim Phillips 	request->dma_desc = dma_map_single(dev, desc, sizeof(*desc),
2989c4a7965SKim Phillips 					   DMA_BIDIRECTIONAL);
2999c4a7965SKim Phillips 	request->callback = callback;
3009c4a7965SKim Phillips 	request->context = context;
3019c4a7965SKim Phillips 
3029c4a7965SKim Phillips 	/* increment fifo head */
3039c4a7965SKim Phillips 	priv->head[ch] = (priv->head[ch] + 1) & (priv->fifo_len - 1);
3049c4a7965SKim Phillips 
3059c4a7965SKim Phillips 	smp_wmb();
3069c4a7965SKim Phillips 	request->desc = desc;
3079c4a7965SKim Phillips 
3089c4a7965SKim Phillips 	/* GO! */
3099c4a7965SKim Phillips 	wmb();
3109c4a7965SKim Phillips 	out_be32(priv->reg + TALITOS_FF_LO(ch), request->dma_desc);
3119c4a7965SKim Phillips 
3129c4a7965SKim Phillips 	spin_unlock_irqrestore(&priv->head_lock[ch], flags);
3139c4a7965SKim Phillips 
3149c4a7965SKim Phillips 	return -EINPROGRESS;
3159c4a7965SKim Phillips }
3169c4a7965SKim Phillips 
3179c4a7965SKim Phillips /*
3189c4a7965SKim Phillips  * process what was done, notify callback of error if not
3199c4a7965SKim Phillips  */
3209c4a7965SKim Phillips static void flush_channel(struct device *dev, int ch, int error, int reset_ch)
3219c4a7965SKim Phillips {
3229c4a7965SKim Phillips 	struct talitos_private *priv = dev_get_drvdata(dev);
3239c4a7965SKim Phillips 	struct talitos_request *request, saved_req;
3249c4a7965SKim Phillips 	unsigned long flags;
3259c4a7965SKim Phillips 	int tail, status;
3269c4a7965SKim Phillips 
3279c4a7965SKim Phillips 	spin_lock_irqsave(&priv->tail_lock[ch], flags);
3289c4a7965SKim Phillips 
3299c4a7965SKim Phillips 	tail = priv->tail[ch];
3309c4a7965SKim Phillips 	while (priv->fifo[ch][tail].desc) {
3319c4a7965SKim Phillips 		request = &priv->fifo[ch][tail];
3329c4a7965SKim Phillips 
3339c4a7965SKim Phillips 		/* descriptors with their done bits set don't get the error */
3349c4a7965SKim Phillips 		rmb();
335ca38a814SLee Nipper 		if ((request->desc->hdr & DESC_HDR_DONE) == DESC_HDR_DONE)
3369c4a7965SKim Phillips 			status = 0;
337ca38a814SLee Nipper 		else
3389c4a7965SKim Phillips 			if (!error)
3399c4a7965SKim Phillips 				break;
3409c4a7965SKim Phillips 			else
3419c4a7965SKim Phillips 				status = error;
3429c4a7965SKim Phillips 
3439c4a7965SKim Phillips 		dma_unmap_single(dev, request->dma_desc,
3449c4a7965SKim Phillips 			sizeof(struct talitos_desc), DMA_BIDIRECTIONAL);
3459c4a7965SKim Phillips 
3469c4a7965SKim Phillips 		/* copy entries so we can call callback outside lock */
3479c4a7965SKim Phillips 		saved_req.desc = request->desc;
3489c4a7965SKim Phillips 		saved_req.callback = request->callback;
3499c4a7965SKim Phillips 		saved_req.context = request->context;
3509c4a7965SKim Phillips 
3519c4a7965SKim Phillips 		/* release request entry in fifo */
3529c4a7965SKim Phillips 		smp_wmb();
3539c4a7965SKim Phillips 		request->desc = NULL;
3549c4a7965SKim Phillips 
3559c4a7965SKim Phillips 		/* increment fifo tail */
3569c4a7965SKim Phillips 		priv->tail[ch] = (tail + 1) & (priv->fifo_len - 1);
3579c4a7965SKim Phillips 
3589c4a7965SKim Phillips 		spin_unlock_irqrestore(&priv->tail_lock[ch], flags);
359ec6644d6SKim Phillips 
360ec6644d6SKim Phillips 		atomic_dec(&priv->submit_count[ch]);
361ec6644d6SKim Phillips 
3629c4a7965SKim Phillips 		saved_req.callback(dev, saved_req.desc, saved_req.context,
3639c4a7965SKim Phillips 				   status);
3649c4a7965SKim Phillips 		/* channel may resume processing in single desc error case */
3659c4a7965SKim Phillips 		if (error && !reset_ch && status == error)
3669c4a7965SKim Phillips 			return;
3679c4a7965SKim Phillips 		spin_lock_irqsave(&priv->tail_lock[ch], flags);
3689c4a7965SKim Phillips 		tail = priv->tail[ch];
3699c4a7965SKim Phillips 	}
3709c4a7965SKim Phillips 
3719c4a7965SKim Phillips 	spin_unlock_irqrestore(&priv->tail_lock[ch], flags);
3729c4a7965SKim Phillips }
3739c4a7965SKim Phillips 
3749c4a7965SKim Phillips /*
3759c4a7965SKim Phillips  * process completed requests for channels that have done status
3769c4a7965SKim Phillips  */
3779c4a7965SKim Phillips static void talitos_done(unsigned long data)
3789c4a7965SKim Phillips {
3799c4a7965SKim Phillips 	struct device *dev = (struct device *)data;
3809c4a7965SKim Phillips 	struct talitos_private *priv = dev_get_drvdata(dev);
3819c4a7965SKim Phillips 	int ch;
3829c4a7965SKim Phillips 
3839c4a7965SKim Phillips 	for (ch = 0; ch < priv->num_channels; ch++)
3849c4a7965SKim Phillips 		flush_channel(dev, ch, 0, 0);
3851c2e8811SLee Nipper 
3861c2e8811SLee Nipper 	/* At this point, all completed channels have been processed.
3871c2e8811SLee Nipper 	 * Unmask done interrupts for channels completed later on.
3881c2e8811SLee Nipper 	 */
389fe5720e2SKim Phillips 	setbits32(priv->reg + TALITOS_IMR, TALITOS_IMR_INIT);
390fe5720e2SKim Phillips 	setbits32(priv->reg + TALITOS_IMR_LO, TALITOS_IMR_LO_INIT);
3919c4a7965SKim Phillips }
3929c4a7965SKim Phillips 
3939c4a7965SKim Phillips /*
3949c4a7965SKim Phillips  * locate current (offending) descriptor
3959c4a7965SKim Phillips  */
3969c4a7965SKim Phillips static struct talitos_desc *current_desc(struct device *dev, int ch)
3979c4a7965SKim Phillips {
3989c4a7965SKim Phillips 	struct talitos_private *priv = dev_get_drvdata(dev);
3999c4a7965SKim Phillips 	int tail = priv->tail[ch];
4009c4a7965SKim Phillips 	dma_addr_t cur_desc;
4019c4a7965SKim Phillips 
4029c4a7965SKim Phillips 	cur_desc = in_be32(priv->reg + TALITOS_CDPR_LO(ch));
4039c4a7965SKim Phillips 
4049c4a7965SKim Phillips 	while (priv->fifo[ch][tail].dma_desc != cur_desc) {
4059c4a7965SKim Phillips 		tail = (tail + 1) & (priv->fifo_len - 1);
4069c4a7965SKim Phillips 		if (tail == priv->tail[ch]) {
4079c4a7965SKim Phillips 			dev_err(dev, "couldn't locate current descriptor\n");
4089c4a7965SKim Phillips 			return NULL;
4099c4a7965SKim Phillips 		}
4109c4a7965SKim Phillips 	}
4119c4a7965SKim Phillips 
4129c4a7965SKim Phillips 	return priv->fifo[ch][tail].desc;
4139c4a7965SKim Phillips }
4149c4a7965SKim Phillips 
4159c4a7965SKim Phillips /*
4169c4a7965SKim Phillips  * user diagnostics; report root cause of error based on execution unit status
4179c4a7965SKim Phillips  */
4189c4a7965SKim Phillips static void report_eu_error(struct device *dev, int ch, struct talitos_desc *desc)
4199c4a7965SKim Phillips {
4209c4a7965SKim Phillips 	struct talitos_private *priv = dev_get_drvdata(dev);
4219c4a7965SKim Phillips 	int i;
4229c4a7965SKim Phillips 
4239c4a7965SKim Phillips 	switch (desc->hdr & DESC_HDR_SEL0_MASK) {
4249c4a7965SKim Phillips 	case DESC_HDR_SEL0_AFEU:
4259c4a7965SKim Phillips 		dev_err(dev, "AFEUISR 0x%08x_%08x\n",
4269c4a7965SKim Phillips 			in_be32(priv->reg + TALITOS_AFEUISR),
4279c4a7965SKim Phillips 			in_be32(priv->reg + TALITOS_AFEUISR_LO));
4289c4a7965SKim Phillips 		break;
4299c4a7965SKim Phillips 	case DESC_HDR_SEL0_DEU:
4309c4a7965SKim Phillips 		dev_err(dev, "DEUISR 0x%08x_%08x\n",
4319c4a7965SKim Phillips 			in_be32(priv->reg + TALITOS_DEUISR),
4329c4a7965SKim Phillips 			in_be32(priv->reg + TALITOS_DEUISR_LO));
4339c4a7965SKim Phillips 		break;
4349c4a7965SKim Phillips 	case DESC_HDR_SEL0_MDEUA:
4359c4a7965SKim Phillips 	case DESC_HDR_SEL0_MDEUB:
4369c4a7965SKim Phillips 		dev_err(dev, "MDEUISR 0x%08x_%08x\n",
4379c4a7965SKim Phillips 			in_be32(priv->reg + TALITOS_MDEUISR),
4389c4a7965SKim Phillips 			in_be32(priv->reg + TALITOS_MDEUISR_LO));
4399c4a7965SKim Phillips 		break;
4409c4a7965SKim Phillips 	case DESC_HDR_SEL0_RNG:
4419c4a7965SKim Phillips 		dev_err(dev, "RNGUISR 0x%08x_%08x\n",
4429c4a7965SKim Phillips 			in_be32(priv->reg + TALITOS_RNGUISR),
4439c4a7965SKim Phillips 			in_be32(priv->reg + TALITOS_RNGUISR_LO));
4449c4a7965SKim Phillips 		break;
4459c4a7965SKim Phillips 	case DESC_HDR_SEL0_PKEU:
4469c4a7965SKim Phillips 		dev_err(dev, "PKEUISR 0x%08x_%08x\n",
4479c4a7965SKim Phillips 			in_be32(priv->reg + TALITOS_PKEUISR),
4489c4a7965SKim Phillips 			in_be32(priv->reg + TALITOS_PKEUISR_LO));
4499c4a7965SKim Phillips 		break;
4509c4a7965SKim Phillips 	case DESC_HDR_SEL0_AESU:
4519c4a7965SKim Phillips 		dev_err(dev, "AESUISR 0x%08x_%08x\n",
4529c4a7965SKim Phillips 			in_be32(priv->reg + TALITOS_AESUISR),
4539c4a7965SKim Phillips 			in_be32(priv->reg + TALITOS_AESUISR_LO));
4549c4a7965SKim Phillips 		break;
4559c4a7965SKim Phillips 	case DESC_HDR_SEL0_CRCU:
4569c4a7965SKim Phillips 		dev_err(dev, "CRCUISR 0x%08x_%08x\n",
4579c4a7965SKim Phillips 			in_be32(priv->reg + TALITOS_CRCUISR),
4589c4a7965SKim Phillips 			in_be32(priv->reg + TALITOS_CRCUISR_LO));
4599c4a7965SKim Phillips 		break;
4609c4a7965SKim Phillips 	case DESC_HDR_SEL0_KEU:
4619c4a7965SKim Phillips 		dev_err(dev, "KEUISR 0x%08x_%08x\n",
4629c4a7965SKim Phillips 			in_be32(priv->reg + TALITOS_KEUISR),
4639c4a7965SKim Phillips 			in_be32(priv->reg + TALITOS_KEUISR_LO));
4649c4a7965SKim Phillips 		break;
4659c4a7965SKim Phillips 	}
4669c4a7965SKim Phillips 
4679c4a7965SKim Phillips 	switch (desc->hdr & DESC_HDR_SEL1_MASK) {
4689c4a7965SKim Phillips 	case DESC_HDR_SEL1_MDEUA:
4699c4a7965SKim Phillips 	case DESC_HDR_SEL1_MDEUB:
4709c4a7965SKim Phillips 		dev_err(dev, "MDEUISR 0x%08x_%08x\n",
4719c4a7965SKim Phillips 			in_be32(priv->reg + TALITOS_MDEUISR),
4729c4a7965SKim Phillips 			in_be32(priv->reg + TALITOS_MDEUISR_LO));
4739c4a7965SKim Phillips 		break;
4749c4a7965SKim Phillips 	case DESC_HDR_SEL1_CRCU:
4759c4a7965SKim Phillips 		dev_err(dev, "CRCUISR 0x%08x_%08x\n",
4769c4a7965SKim Phillips 			in_be32(priv->reg + TALITOS_CRCUISR),
4779c4a7965SKim Phillips 			in_be32(priv->reg + TALITOS_CRCUISR_LO));
4789c4a7965SKim Phillips 		break;
4799c4a7965SKim Phillips 	}
4809c4a7965SKim Phillips 
4819c4a7965SKim Phillips 	for (i = 0; i < 8; i++)
4829c4a7965SKim Phillips 		dev_err(dev, "DESCBUF 0x%08x_%08x\n",
4839c4a7965SKim Phillips 			in_be32(priv->reg + TALITOS_DESCBUF(ch) + 8*i),
4849c4a7965SKim Phillips 			in_be32(priv->reg + TALITOS_DESCBUF_LO(ch) + 8*i));
4859c4a7965SKim Phillips }
4869c4a7965SKim Phillips 
4879c4a7965SKim Phillips /*
4889c4a7965SKim Phillips  * recover from error interrupts
4899c4a7965SKim Phillips  */
49040405f10SKim Phillips static void talitos_error(unsigned long data, u32 isr, u32 isr_lo)
4919c4a7965SKim Phillips {
4929c4a7965SKim Phillips 	struct device *dev = (struct device *)data;
4939c4a7965SKim Phillips 	struct talitos_private *priv = dev_get_drvdata(dev);
4949c4a7965SKim Phillips 	unsigned int timeout = TALITOS_TIMEOUT;
4959c4a7965SKim Phillips 	int ch, error, reset_dev = 0, reset_ch = 0;
49640405f10SKim Phillips 	u32 v, v_lo;
4979c4a7965SKim Phillips 
4989c4a7965SKim Phillips 	for (ch = 0; ch < priv->num_channels; ch++) {
4999c4a7965SKim Phillips 		/* skip channels without errors */
5009c4a7965SKim Phillips 		if (!(isr & (1 << (ch * 2 + 1))))
5019c4a7965SKim Phillips 			continue;
5029c4a7965SKim Phillips 
5039c4a7965SKim Phillips 		error = -EINVAL;
5049c4a7965SKim Phillips 
5059c4a7965SKim Phillips 		v = in_be32(priv->reg + TALITOS_CCPSR(ch));
5069c4a7965SKim Phillips 		v_lo = in_be32(priv->reg + TALITOS_CCPSR_LO(ch));
5079c4a7965SKim Phillips 
5089c4a7965SKim Phillips 		if (v_lo & TALITOS_CCPSR_LO_DOF) {
5099c4a7965SKim Phillips 			dev_err(dev, "double fetch fifo overflow error\n");
5109c4a7965SKim Phillips 			error = -EAGAIN;
5119c4a7965SKim Phillips 			reset_ch = 1;
5129c4a7965SKim Phillips 		}
5139c4a7965SKim Phillips 		if (v_lo & TALITOS_CCPSR_LO_SOF) {
5149c4a7965SKim Phillips 			/* h/w dropped descriptor */
5159c4a7965SKim Phillips 			dev_err(dev, "single fetch fifo overflow error\n");
5169c4a7965SKim Phillips 			error = -EAGAIN;
5179c4a7965SKim Phillips 		}
5189c4a7965SKim Phillips 		if (v_lo & TALITOS_CCPSR_LO_MDTE)
5199c4a7965SKim Phillips 			dev_err(dev, "master data transfer error\n");
5209c4a7965SKim Phillips 		if (v_lo & TALITOS_CCPSR_LO_SGDLZ)
5219c4a7965SKim Phillips 			dev_err(dev, "s/g data length zero error\n");
5229c4a7965SKim Phillips 		if (v_lo & TALITOS_CCPSR_LO_FPZ)
5239c4a7965SKim Phillips 			dev_err(dev, "fetch pointer zero error\n");
5249c4a7965SKim Phillips 		if (v_lo & TALITOS_CCPSR_LO_IDH)
5259c4a7965SKim Phillips 			dev_err(dev, "illegal descriptor header error\n");
5269c4a7965SKim Phillips 		if (v_lo & TALITOS_CCPSR_LO_IEU)
5279c4a7965SKim Phillips 			dev_err(dev, "invalid execution unit error\n");
5289c4a7965SKim Phillips 		if (v_lo & TALITOS_CCPSR_LO_EU)
5299c4a7965SKim Phillips 			report_eu_error(dev, ch, current_desc(dev, ch));
5309c4a7965SKim Phillips 		if (v_lo & TALITOS_CCPSR_LO_GB)
5319c4a7965SKim Phillips 			dev_err(dev, "gather boundary error\n");
5329c4a7965SKim Phillips 		if (v_lo & TALITOS_CCPSR_LO_GRL)
5339c4a7965SKim Phillips 			dev_err(dev, "gather return/length error\n");
5349c4a7965SKim Phillips 		if (v_lo & TALITOS_CCPSR_LO_SB)
5359c4a7965SKim Phillips 			dev_err(dev, "scatter boundary error\n");
5369c4a7965SKim Phillips 		if (v_lo & TALITOS_CCPSR_LO_SRL)
5379c4a7965SKim Phillips 			dev_err(dev, "scatter return/length error\n");
5389c4a7965SKim Phillips 
5399c4a7965SKim Phillips 		flush_channel(dev, ch, error, reset_ch);
5409c4a7965SKim Phillips 
5419c4a7965SKim Phillips 		if (reset_ch) {
5429c4a7965SKim Phillips 			reset_channel(dev, ch);
5439c4a7965SKim Phillips 		} else {
5449c4a7965SKim Phillips 			setbits32(priv->reg + TALITOS_CCCR(ch),
5459c4a7965SKim Phillips 				  TALITOS_CCCR_CONT);
5469c4a7965SKim Phillips 			setbits32(priv->reg + TALITOS_CCCR_LO(ch), 0);
5479c4a7965SKim Phillips 			while ((in_be32(priv->reg + TALITOS_CCCR(ch)) &
5489c4a7965SKim Phillips 			       TALITOS_CCCR_CONT) && --timeout)
5499c4a7965SKim Phillips 				cpu_relax();
5509c4a7965SKim Phillips 			if (timeout == 0) {
5519c4a7965SKim Phillips 				dev_err(dev, "failed to restart channel %d\n",
5529c4a7965SKim Phillips 					ch);
5539c4a7965SKim Phillips 				reset_dev = 1;
5549c4a7965SKim Phillips 			}
5559c4a7965SKim Phillips 		}
5569c4a7965SKim Phillips 	}
5579c4a7965SKim Phillips 	if (reset_dev || isr & ~TALITOS_ISR_CHERR || isr_lo) {
5589c4a7965SKim Phillips 		dev_err(dev, "done overflow, internal time out, or rngu error: "
5599c4a7965SKim Phillips 		        "ISR 0x%08x_%08x\n", isr, isr_lo);
5609c4a7965SKim Phillips 
5619c4a7965SKim Phillips 		/* purge request queues */
5629c4a7965SKim Phillips 		for (ch = 0; ch < priv->num_channels; ch++)
5639c4a7965SKim Phillips 			flush_channel(dev, ch, -EIO, 1);
5649c4a7965SKim Phillips 
5659c4a7965SKim Phillips 		/* reset and reinitialize the device */
5669c4a7965SKim Phillips 		init_device(dev);
5679c4a7965SKim Phillips 	}
5689c4a7965SKim Phillips }
5699c4a7965SKim Phillips 
5709c4a7965SKim Phillips static irqreturn_t talitos_interrupt(int irq, void *data)
5719c4a7965SKim Phillips {
5729c4a7965SKim Phillips 	struct device *dev = data;
5739c4a7965SKim Phillips 	struct talitos_private *priv = dev_get_drvdata(dev);
5749c4a7965SKim Phillips 	u32 isr, isr_lo;
5759c4a7965SKim Phillips 
5769c4a7965SKim Phillips 	isr = in_be32(priv->reg + TALITOS_ISR);
5779c4a7965SKim Phillips 	isr_lo = in_be32(priv->reg + TALITOS_ISR_LO);
578ca38a814SLee Nipper 	/* Acknowledge interrupt */
5799c4a7965SKim Phillips 	out_be32(priv->reg + TALITOS_ICR, isr);
5809c4a7965SKim Phillips 	out_be32(priv->reg + TALITOS_ICR_LO, isr_lo);
5819c4a7965SKim Phillips 
582ca38a814SLee Nipper 	if (unlikely((isr & ~TALITOS_ISR_CHDONE) || isr_lo))
58340405f10SKim Phillips 		talitos_error((unsigned long)data, isr, isr_lo);
584ca38a814SLee Nipper 	else
5851c2e8811SLee Nipper 		if (likely(isr & TALITOS_ISR_CHDONE)) {
5861c2e8811SLee Nipper 			/* mask further done interrupts. */
5871c2e8811SLee Nipper 			clrbits32(priv->reg + TALITOS_IMR, TALITOS_IMR_DONE);
5881c2e8811SLee Nipper 			/* done_task will unmask done interrupts at exit */
5899c4a7965SKim Phillips 			tasklet_schedule(&priv->done_task);
5901c2e8811SLee Nipper 		}
5919c4a7965SKim Phillips 
5929c4a7965SKim Phillips 	return (isr || isr_lo) ? IRQ_HANDLED : IRQ_NONE;
5939c4a7965SKim Phillips }
5949c4a7965SKim Phillips 
5959c4a7965SKim Phillips /*
5969c4a7965SKim Phillips  * hwrng
5979c4a7965SKim Phillips  */
5989c4a7965SKim Phillips static int talitos_rng_data_present(struct hwrng *rng, int wait)
5999c4a7965SKim Phillips {
6009c4a7965SKim Phillips 	struct device *dev = (struct device *)rng->priv;
6019c4a7965SKim Phillips 	struct talitos_private *priv = dev_get_drvdata(dev);
6029c4a7965SKim Phillips 	u32 ofl;
6039c4a7965SKim Phillips 	int i;
6049c4a7965SKim Phillips 
6059c4a7965SKim Phillips 	for (i = 0; i < 20; i++) {
6069c4a7965SKim Phillips 		ofl = in_be32(priv->reg + TALITOS_RNGUSR_LO) &
6079c4a7965SKim Phillips 		      TALITOS_RNGUSR_LO_OFL;
6089c4a7965SKim Phillips 		if (ofl || !wait)
6099c4a7965SKim Phillips 			break;
6109c4a7965SKim Phillips 		udelay(10);
6119c4a7965SKim Phillips 	}
6129c4a7965SKim Phillips 
6139c4a7965SKim Phillips 	return !!ofl;
6149c4a7965SKim Phillips }
6159c4a7965SKim Phillips 
6169c4a7965SKim Phillips static int talitos_rng_data_read(struct hwrng *rng, u32 *data)
6179c4a7965SKim Phillips {
6189c4a7965SKim Phillips 	struct device *dev = (struct device *)rng->priv;
6199c4a7965SKim Phillips 	struct talitos_private *priv = dev_get_drvdata(dev);
6209c4a7965SKim Phillips 
6219c4a7965SKim Phillips 	/* rng fifo requires 64-bit accesses */
6229c4a7965SKim Phillips 	*data = in_be32(priv->reg + TALITOS_RNGU_FIFO);
6239c4a7965SKim Phillips 	*data = in_be32(priv->reg + TALITOS_RNGU_FIFO_LO);
6249c4a7965SKim Phillips 
6259c4a7965SKim Phillips 	return sizeof(u32);
6269c4a7965SKim Phillips }
6279c4a7965SKim Phillips 
6289c4a7965SKim Phillips static int talitos_rng_init(struct hwrng *rng)
6299c4a7965SKim Phillips {
6309c4a7965SKim Phillips 	struct device *dev = (struct device *)rng->priv;
6319c4a7965SKim Phillips 	struct talitos_private *priv = dev_get_drvdata(dev);
6329c4a7965SKim Phillips 	unsigned int timeout = TALITOS_TIMEOUT;
6339c4a7965SKim Phillips 
6349c4a7965SKim Phillips 	setbits32(priv->reg + TALITOS_RNGURCR_LO, TALITOS_RNGURCR_LO_SR);
6359c4a7965SKim Phillips 	while (!(in_be32(priv->reg + TALITOS_RNGUSR_LO) & TALITOS_RNGUSR_LO_RD)
6369c4a7965SKim Phillips 	       && --timeout)
6379c4a7965SKim Phillips 		cpu_relax();
6389c4a7965SKim Phillips 	if (timeout == 0) {
6399c4a7965SKim Phillips 		dev_err(dev, "failed to reset rng hw\n");
6409c4a7965SKim Phillips 		return -ENODEV;
6419c4a7965SKim Phillips 	}
6429c4a7965SKim Phillips 
6439c4a7965SKim Phillips 	/* start generating */
6449c4a7965SKim Phillips 	setbits32(priv->reg + TALITOS_RNGUDSR_LO, 0);
6459c4a7965SKim Phillips 
6469c4a7965SKim Phillips 	return 0;
6479c4a7965SKim Phillips }
6489c4a7965SKim Phillips 
6499c4a7965SKim Phillips static int talitos_register_rng(struct device *dev)
6509c4a7965SKim Phillips {
6519c4a7965SKim Phillips 	struct talitos_private *priv = dev_get_drvdata(dev);
6529c4a7965SKim Phillips 
6539c4a7965SKim Phillips 	priv->rng.name		= dev_driver_string(dev),
6549c4a7965SKim Phillips 	priv->rng.init		= talitos_rng_init,
6559c4a7965SKim Phillips 	priv->rng.data_present	= talitos_rng_data_present,
6569c4a7965SKim Phillips 	priv->rng.data_read	= talitos_rng_data_read,
6579c4a7965SKim Phillips 	priv->rng.priv		= (unsigned long)dev;
6589c4a7965SKim Phillips 
6599c4a7965SKim Phillips 	return hwrng_register(&priv->rng);
6609c4a7965SKim Phillips }
6619c4a7965SKim Phillips 
6629c4a7965SKim Phillips static void talitos_unregister_rng(struct device *dev)
6639c4a7965SKim Phillips {
6649c4a7965SKim Phillips 	struct talitos_private *priv = dev_get_drvdata(dev);
6659c4a7965SKim Phillips 
6669c4a7965SKim Phillips 	hwrng_unregister(&priv->rng);
6679c4a7965SKim Phillips }
6689c4a7965SKim Phillips 
6699c4a7965SKim Phillips /*
6709c4a7965SKim Phillips  * crypto alg
6719c4a7965SKim Phillips  */
6729c4a7965SKim Phillips #define TALITOS_CRA_PRIORITY		3000
6739c4a7965SKim Phillips #define TALITOS_MAX_KEY_SIZE		64
6743952f17eSLee Nipper #define TALITOS_MAX_IV_LENGTH		16 /* max of AES_BLOCK_SIZE, DES3_EDE_BLOCK_SIZE */
67570bcaca7SLee Nipper 
6763952f17eSLee Nipper #define MD5_DIGEST_SIZE   16
6779c4a7965SKim Phillips 
6789c4a7965SKim Phillips struct talitos_ctx {
6799c4a7965SKim Phillips 	struct device *dev;
6809c4a7965SKim Phillips 	__be32 desc_hdr_template;
6819c4a7965SKim Phillips 	u8 key[TALITOS_MAX_KEY_SIZE];
68270bcaca7SLee Nipper 	u8 iv[TALITOS_MAX_IV_LENGTH];
6839c4a7965SKim Phillips 	unsigned int keylen;
6849c4a7965SKim Phillips 	unsigned int enckeylen;
6859c4a7965SKim Phillips 	unsigned int authkeylen;
6869c4a7965SKim Phillips 	unsigned int authsize;
6879c4a7965SKim Phillips };
6889c4a7965SKim Phillips 
68956af8cd4SLee Nipper static int aead_setauthsize(struct crypto_aead *authenc,
6909c4a7965SKim Phillips 			    unsigned int authsize)
6919c4a7965SKim Phillips {
6929c4a7965SKim Phillips 	struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
6939c4a7965SKim Phillips 
6949c4a7965SKim Phillips 	ctx->authsize = authsize;
6959c4a7965SKim Phillips 
6969c4a7965SKim Phillips 	return 0;
6979c4a7965SKim Phillips }
6989c4a7965SKim Phillips 
69956af8cd4SLee Nipper static int aead_setkey(struct crypto_aead *authenc,
7009c4a7965SKim Phillips 		       const u8 *key, unsigned int keylen)
7019c4a7965SKim Phillips {
7029c4a7965SKim Phillips 	struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
7039c4a7965SKim Phillips 	struct rtattr *rta = (void *)key;
7049c4a7965SKim Phillips 	struct crypto_authenc_key_param *param;
7059c4a7965SKim Phillips 	unsigned int authkeylen;
7069c4a7965SKim Phillips 	unsigned int enckeylen;
7079c4a7965SKim Phillips 
7089c4a7965SKim Phillips 	if (!RTA_OK(rta, keylen))
7099c4a7965SKim Phillips 		goto badkey;
7109c4a7965SKim Phillips 
7119c4a7965SKim Phillips 	if (rta->rta_type != CRYPTO_AUTHENC_KEYA_PARAM)
7129c4a7965SKim Phillips 		goto badkey;
7139c4a7965SKim Phillips 
7149c4a7965SKim Phillips 	if (RTA_PAYLOAD(rta) < sizeof(*param))
7159c4a7965SKim Phillips 		goto badkey;
7169c4a7965SKim Phillips 
7179c4a7965SKim Phillips 	param = RTA_DATA(rta);
7189c4a7965SKim Phillips 	enckeylen = be32_to_cpu(param->enckeylen);
7199c4a7965SKim Phillips 
7209c4a7965SKim Phillips 	key += RTA_ALIGN(rta->rta_len);
7219c4a7965SKim Phillips 	keylen -= RTA_ALIGN(rta->rta_len);
7229c4a7965SKim Phillips 
7239c4a7965SKim Phillips 	if (keylen < enckeylen)
7249c4a7965SKim Phillips 		goto badkey;
7259c4a7965SKim Phillips 
7269c4a7965SKim Phillips 	authkeylen = keylen - enckeylen;
7279c4a7965SKim Phillips 
7289c4a7965SKim Phillips 	if (keylen > TALITOS_MAX_KEY_SIZE)
7299c4a7965SKim Phillips 		goto badkey;
7309c4a7965SKim Phillips 
7319c4a7965SKim Phillips 	memcpy(&ctx->key, key, keylen);
7329c4a7965SKim Phillips 
7339c4a7965SKim Phillips 	ctx->keylen = keylen;
7349c4a7965SKim Phillips 	ctx->enckeylen = enckeylen;
7359c4a7965SKim Phillips 	ctx->authkeylen = authkeylen;
7369c4a7965SKim Phillips 
7379c4a7965SKim Phillips 	return 0;
7389c4a7965SKim Phillips 
7399c4a7965SKim Phillips badkey:
7409c4a7965SKim Phillips 	crypto_aead_set_flags(authenc, CRYPTO_TFM_RES_BAD_KEY_LEN);
7419c4a7965SKim Phillips 	return -EINVAL;
7429c4a7965SKim Phillips }
7439c4a7965SKim Phillips 
7449c4a7965SKim Phillips /*
74556af8cd4SLee Nipper  * talitos_edesc - s/w-extended descriptor
7469c4a7965SKim Phillips  * @src_nents: number of segments in input scatterlist
7479c4a7965SKim Phillips  * @dst_nents: number of segments in output scatterlist
7489c4a7965SKim Phillips  * @dma_len: length of dma mapped link_tbl space
7499c4a7965SKim Phillips  * @dma_link_tbl: bus physical address of link_tbl
7509c4a7965SKim Phillips  * @desc: h/w descriptor
7519c4a7965SKim Phillips  * @link_tbl: input and output h/w link tables (if {src,dst}_nents > 1)
7529c4a7965SKim Phillips  *
7539c4a7965SKim Phillips  * if decrypting (with authcheck), or either one of src_nents or dst_nents
7549c4a7965SKim Phillips  * is greater than 1, an integrity check value is concatenated to the end
7559c4a7965SKim Phillips  * of link_tbl data
7569c4a7965SKim Phillips  */
75756af8cd4SLee Nipper struct talitos_edesc {
7589c4a7965SKim Phillips 	int src_nents;
7599c4a7965SKim Phillips 	int dst_nents;
760*4de9d0b5SLee Nipper 	int src_is_chained;
761*4de9d0b5SLee Nipper 	int dst_is_chained;
7629c4a7965SKim Phillips 	int dma_len;
7639c4a7965SKim Phillips 	dma_addr_t dma_link_tbl;
7649c4a7965SKim Phillips 	struct talitos_desc desc;
7659c4a7965SKim Phillips 	struct talitos_ptr link_tbl[0];
7669c4a7965SKim Phillips };
7679c4a7965SKim Phillips 
768*4de9d0b5SLee Nipper static int talitos_map_sg(struct device *dev, struct scatterlist *sg,
769*4de9d0b5SLee Nipper 			  unsigned int nents, enum dma_data_direction dir,
770*4de9d0b5SLee Nipper 			  int chained)
771*4de9d0b5SLee Nipper {
772*4de9d0b5SLee Nipper 	if (unlikely(chained))
773*4de9d0b5SLee Nipper 		while (sg) {
774*4de9d0b5SLee Nipper 			dma_map_sg(dev, sg, 1, dir);
775*4de9d0b5SLee Nipper 			sg = scatterwalk_sg_next(sg);
776*4de9d0b5SLee Nipper 		}
777*4de9d0b5SLee Nipper 	else
778*4de9d0b5SLee Nipper 		dma_map_sg(dev, sg, nents, dir);
779*4de9d0b5SLee Nipper 	return nents;
780*4de9d0b5SLee Nipper }
781*4de9d0b5SLee Nipper 
782*4de9d0b5SLee Nipper static void talitos_unmap_sg_chain(struct device *dev, struct scatterlist *sg,
783*4de9d0b5SLee Nipper 				   enum dma_data_direction dir)
784*4de9d0b5SLee Nipper {
785*4de9d0b5SLee Nipper 	while (sg) {
786*4de9d0b5SLee Nipper 		dma_unmap_sg(dev, sg, 1, dir);
787*4de9d0b5SLee Nipper 		sg = scatterwalk_sg_next(sg);
788*4de9d0b5SLee Nipper 	}
789*4de9d0b5SLee Nipper }
790*4de9d0b5SLee Nipper 
791*4de9d0b5SLee Nipper static void talitos_sg_unmap(struct device *dev,
792*4de9d0b5SLee Nipper 			     struct talitos_edesc *edesc,
793*4de9d0b5SLee Nipper 			     struct scatterlist *src,
794*4de9d0b5SLee Nipper 			     struct scatterlist *dst)
795*4de9d0b5SLee Nipper {
796*4de9d0b5SLee Nipper 	unsigned int src_nents = edesc->src_nents ? : 1;
797*4de9d0b5SLee Nipper 	unsigned int dst_nents = edesc->dst_nents ? : 1;
798*4de9d0b5SLee Nipper 
799*4de9d0b5SLee Nipper 	if (src != dst) {
800*4de9d0b5SLee Nipper 		if (edesc->src_is_chained)
801*4de9d0b5SLee Nipper 			talitos_unmap_sg_chain(dev, src, DMA_TO_DEVICE);
802*4de9d0b5SLee Nipper 		else
803*4de9d0b5SLee Nipper 			dma_unmap_sg(dev, src, src_nents, DMA_TO_DEVICE);
804*4de9d0b5SLee Nipper 
805*4de9d0b5SLee Nipper 		if (edesc->dst_is_chained)
806*4de9d0b5SLee Nipper 			talitos_unmap_sg_chain(dev, dst, DMA_FROM_DEVICE);
807*4de9d0b5SLee Nipper 		else
808*4de9d0b5SLee Nipper 			dma_unmap_sg(dev, dst, dst_nents, DMA_FROM_DEVICE);
809*4de9d0b5SLee Nipper 	} else
810*4de9d0b5SLee Nipper 		if (edesc->src_is_chained)
811*4de9d0b5SLee Nipper 			talitos_unmap_sg_chain(dev, src, DMA_BIDIRECTIONAL);
812*4de9d0b5SLee Nipper 		else
813*4de9d0b5SLee Nipper 			dma_unmap_sg(dev, src, src_nents, DMA_BIDIRECTIONAL);
814*4de9d0b5SLee Nipper }
815*4de9d0b5SLee Nipper 
8169c4a7965SKim Phillips static void ipsec_esp_unmap(struct device *dev,
81756af8cd4SLee Nipper 			    struct talitos_edesc *edesc,
8189c4a7965SKim Phillips 			    struct aead_request *areq)
8199c4a7965SKim Phillips {
8209c4a7965SKim Phillips 	unmap_single_talitos_ptr(dev, &edesc->desc.ptr[6], DMA_FROM_DEVICE);
8219c4a7965SKim Phillips 	unmap_single_talitos_ptr(dev, &edesc->desc.ptr[3], DMA_TO_DEVICE);
8229c4a7965SKim Phillips 	unmap_single_talitos_ptr(dev, &edesc->desc.ptr[2], DMA_TO_DEVICE);
8239c4a7965SKim Phillips 	unmap_single_talitos_ptr(dev, &edesc->desc.ptr[0], DMA_TO_DEVICE);
8249c4a7965SKim Phillips 
8259c4a7965SKim Phillips 	dma_unmap_sg(dev, areq->assoc, 1, DMA_TO_DEVICE);
8269c4a7965SKim Phillips 
827*4de9d0b5SLee Nipper 	talitos_sg_unmap(dev, edesc, areq->src, areq->dst);
8289c4a7965SKim Phillips 
8299c4a7965SKim Phillips 	if (edesc->dma_len)
8309c4a7965SKim Phillips 		dma_unmap_single(dev, edesc->dma_link_tbl, edesc->dma_len,
8319c4a7965SKim Phillips 				 DMA_BIDIRECTIONAL);
8329c4a7965SKim Phillips }
8339c4a7965SKim Phillips 
8349c4a7965SKim Phillips /*
8359c4a7965SKim Phillips  * ipsec_esp descriptor callbacks
8369c4a7965SKim Phillips  */
8379c4a7965SKim Phillips static void ipsec_esp_encrypt_done(struct device *dev,
8389c4a7965SKim Phillips 				   struct talitos_desc *desc, void *context,
8399c4a7965SKim Phillips 				   int err)
8409c4a7965SKim Phillips {
8419c4a7965SKim Phillips 	struct aead_request *areq = context;
84256af8cd4SLee Nipper 	struct talitos_edesc *edesc =
84356af8cd4SLee Nipper 		 container_of(desc, struct talitos_edesc, desc);
8449c4a7965SKim Phillips 	struct crypto_aead *authenc = crypto_aead_reqtfm(areq);
8459c4a7965SKim Phillips 	struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
8469c4a7965SKim Phillips 	struct scatterlist *sg;
8479c4a7965SKim Phillips 	void *icvdata;
8489c4a7965SKim Phillips 
8499c4a7965SKim Phillips 	ipsec_esp_unmap(dev, edesc, areq);
8509c4a7965SKim Phillips 
8519c4a7965SKim Phillips 	/* copy the generated ICV to dst */
8529c4a7965SKim Phillips 	if (edesc->dma_len) {
8539c4a7965SKim Phillips 		icvdata = &edesc->link_tbl[edesc->src_nents +
854f3c85bc1SLee Nipper 					   edesc->dst_nents + 2];
8559c4a7965SKim Phillips 		sg = sg_last(areq->dst, edesc->dst_nents);
8569c4a7965SKim Phillips 		memcpy((char *)sg_virt(sg) + sg->length - ctx->authsize,
8579c4a7965SKim Phillips 		       icvdata, ctx->authsize);
8589c4a7965SKim Phillips 	}
8599c4a7965SKim Phillips 
8609c4a7965SKim Phillips 	kfree(edesc);
8619c4a7965SKim Phillips 
8629c4a7965SKim Phillips 	aead_request_complete(areq, err);
8639c4a7965SKim Phillips }
8649c4a7965SKim Phillips 
865fe5720e2SKim Phillips static void ipsec_esp_decrypt_swauth_done(struct device *dev,
8669c4a7965SKim Phillips 				   struct talitos_desc *desc, void *context,
8679c4a7965SKim Phillips 				   int err)
8689c4a7965SKim Phillips {
8699c4a7965SKim Phillips 	struct aead_request *req = context;
87056af8cd4SLee Nipper 	struct talitos_edesc *edesc =
87156af8cd4SLee Nipper 		 container_of(desc, struct talitos_edesc, desc);
8729c4a7965SKim Phillips 	struct crypto_aead *authenc = crypto_aead_reqtfm(req);
8739c4a7965SKim Phillips 	struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
8749c4a7965SKim Phillips 	struct scatterlist *sg;
8759c4a7965SKim Phillips 	void *icvdata;
8769c4a7965SKim Phillips 
8779c4a7965SKim Phillips 	ipsec_esp_unmap(dev, edesc, req);
8789c4a7965SKim Phillips 
8799c4a7965SKim Phillips 	if (!err) {
8809c4a7965SKim Phillips 		/* auth check */
8819c4a7965SKim Phillips 		if (edesc->dma_len)
8829c4a7965SKim Phillips 			icvdata = &edesc->link_tbl[edesc->src_nents +
883f3c85bc1SLee Nipper 						   edesc->dst_nents + 2];
8849c4a7965SKim Phillips 		else
8859c4a7965SKim Phillips 			icvdata = &edesc->link_tbl[0];
8869c4a7965SKim Phillips 
8879c4a7965SKim Phillips 		sg = sg_last(req->dst, edesc->dst_nents ? : 1);
8889c4a7965SKim Phillips 		err = memcmp(icvdata, (char *)sg_virt(sg) + sg->length -
8899c4a7965SKim Phillips 			     ctx->authsize, ctx->authsize) ? -EBADMSG : 0;
8909c4a7965SKim Phillips 	}
8919c4a7965SKim Phillips 
8929c4a7965SKim Phillips 	kfree(edesc);
8939c4a7965SKim Phillips 
8949c4a7965SKim Phillips 	aead_request_complete(req, err);
8959c4a7965SKim Phillips }
8969c4a7965SKim Phillips 
897fe5720e2SKim Phillips static void ipsec_esp_decrypt_hwauth_done(struct device *dev,
898fe5720e2SKim Phillips 				   struct talitos_desc *desc, void *context,
899fe5720e2SKim Phillips 				   int err)
900fe5720e2SKim Phillips {
901fe5720e2SKim Phillips 	struct aead_request *req = context;
90256af8cd4SLee Nipper 	struct talitos_edesc *edesc =
90356af8cd4SLee Nipper 		 container_of(desc, struct talitos_edesc, desc);
904fe5720e2SKim Phillips 
905fe5720e2SKim Phillips 	ipsec_esp_unmap(dev, edesc, req);
906fe5720e2SKim Phillips 
907fe5720e2SKim Phillips 	/* check ICV auth status */
908fe5720e2SKim Phillips 	if (!err)
909fe5720e2SKim Phillips 		if ((desc->hdr_lo & DESC_HDR_LO_ICCR1_MASK) !=
910fe5720e2SKim Phillips 		    DESC_HDR_LO_ICCR1_PASS)
911fe5720e2SKim Phillips 			err = -EBADMSG;
912fe5720e2SKim Phillips 
913fe5720e2SKim Phillips 	kfree(edesc);
914fe5720e2SKim Phillips 
915fe5720e2SKim Phillips 	aead_request_complete(req, err);
916fe5720e2SKim Phillips }
917fe5720e2SKim Phillips 
9189c4a7965SKim Phillips /*
9199c4a7965SKim Phillips  * convert scatterlist to SEC h/w link table format
9209c4a7965SKim Phillips  * stop at cryptlen bytes
9219c4a7965SKim Phillips  */
92270bcaca7SLee Nipper static int sg_to_link_tbl(struct scatterlist *sg, int sg_count,
9239c4a7965SKim Phillips 			   int cryptlen, struct talitos_ptr *link_tbl_ptr)
9249c4a7965SKim Phillips {
92570bcaca7SLee Nipper 	int n_sg = sg_count;
92670bcaca7SLee Nipper 
92770bcaca7SLee Nipper 	while (n_sg--) {
9289c4a7965SKim Phillips 		link_tbl_ptr->ptr = cpu_to_be32(sg_dma_address(sg));
9299c4a7965SKim Phillips 		link_tbl_ptr->len = cpu_to_be16(sg_dma_len(sg));
9309c4a7965SKim Phillips 		link_tbl_ptr->j_extent = 0;
9319c4a7965SKim Phillips 		link_tbl_ptr++;
9329c4a7965SKim Phillips 		cryptlen -= sg_dma_len(sg);
933*4de9d0b5SLee Nipper 		sg = scatterwalk_sg_next(sg);
9349c4a7965SKim Phillips 	}
9359c4a7965SKim Phillips 
93670bcaca7SLee Nipper 	/* adjust (decrease) last one (or two) entry's len to cryptlen */
9379c4a7965SKim Phillips 	link_tbl_ptr--;
938c0e741d4SKim Phillips 	while (be16_to_cpu(link_tbl_ptr->len) <= (-cryptlen)) {
93970bcaca7SLee Nipper 		/* Empty this entry, and move to previous one */
94070bcaca7SLee Nipper 		cryptlen += be16_to_cpu(link_tbl_ptr->len);
94170bcaca7SLee Nipper 		link_tbl_ptr->len = 0;
94270bcaca7SLee Nipper 		sg_count--;
94370bcaca7SLee Nipper 		link_tbl_ptr--;
94470bcaca7SLee Nipper 	}
9459c4a7965SKim Phillips 	link_tbl_ptr->len = cpu_to_be16(be16_to_cpu(link_tbl_ptr->len)
9469c4a7965SKim Phillips 					+ cryptlen);
9479c4a7965SKim Phillips 
9489c4a7965SKim Phillips 	/* tag end of link table */
9499c4a7965SKim Phillips 	link_tbl_ptr->j_extent = DESC_PTR_LNKTBL_RETURN;
95070bcaca7SLee Nipper 
95170bcaca7SLee Nipper 	return sg_count;
9529c4a7965SKim Phillips }
9539c4a7965SKim Phillips 
9549c4a7965SKim Phillips /*
9559c4a7965SKim Phillips  * fill in and submit ipsec_esp descriptor
9569c4a7965SKim Phillips  */
95756af8cd4SLee Nipper static int ipsec_esp(struct talitos_edesc *edesc, struct aead_request *areq,
9589c4a7965SKim Phillips 		     u8 *giv, u64 seq,
9599c4a7965SKim Phillips 		     void (*callback) (struct device *dev,
9609c4a7965SKim Phillips 				       struct talitos_desc *desc,
9619c4a7965SKim Phillips 				       void *context, int error))
9629c4a7965SKim Phillips {
9639c4a7965SKim Phillips 	struct crypto_aead *aead = crypto_aead_reqtfm(areq);
9649c4a7965SKim Phillips 	struct talitos_ctx *ctx = crypto_aead_ctx(aead);
9659c4a7965SKim Phillips 	struct device *dev = ctx->dev;
9669c4a7965SKim Phillips 	struct talitos_desc *desc = &edesc->desc;
9679c4a7965SKim Phillips 	unsigned int cryptlen = areq->cryptlen;
9689c4a7965SKim Phillips 	unsigned int authsize = ctx->authsize;
9699c4a7965SKim Phillips 	unsigned int ivsize;
970fa86a267SKim Phillips 	int sg_count, ret;
971fe5720e2SKim Phillips 	int sg_link_tbl_len;
9729c4a7965SKim Phillips 
9739c4a7965SKim Phillips 	/* hmac key */
9749c4a7965SKim Phillips 	map_single_talitos_ptr(dev, &desc->ptr[0], ctx->authkeylen, &ctx->key,
9759c4a7965SKim Phillips 			       0, DMA_TO_DEVICE);
9769c4a7965SKim Phillips 	/* hmac data */
9779c4a7965SKim Phillips 	map_single_talitos_ptr(dev, &desc->ptr[1], sg_virt(areq->src) -
9789c4a7965SKim Phillips 			       sg_virt(areq->assoc), sg_virt(areq->assoc), 0,
9799c4a7965SKim Phillips 			       DMA_TO_DEVICE);
9809c4a7965SKim Phillips 	/* cipher iv */
9819c4a7965SKim Phillips 	ivsize = crypto_aead_ivsize(aead);
9829c4a7965SKim Phillips 	map_single_talitos_ptr(dev, &desc->ptr[2], ivsize, giv ?: areq->iv, 0,
9839c4a7965SKim Phillips 			       DMA_TO_DEVICE);
9849c4a7965SKim Phillips 
9859c4a7965SKim Phillips 	/* cipher key */
9869c4a7965SKim Phillips 	map_single_talitos_ptr(dev, &desc->ptr[3], ctx->enckeylen,
9879c4a7965SKim Phillips 			       (char *)&ctx->key + ctx->authkeylen, 0,
9889c4a7965SKim Phillips 			       DMA_TO_DEVICE);
9899c4a7965SKim Phillips 
9909c4a7965SKim Phillips 	/*
9919c4a7965SKim Phillips 	 * cipher in
9929c4a7965SKim Phillips 	 * map and adjust cipher len to aead request cryptlen.
9939c4a7965SKim Phillips 	 * extent is bytes of HMAC postpended to ciphertext,
9949c4a7965SKim Phillips 	 * typically 12 for ipsec
9959c4a7965SKim Phillips 	 */
9969c4a7965SKim Phillips 	desc->ptr[4].len = cpu_to_be16(cryptlen);
9979c4a7965SKim Phillips 	desc->ptr[4].j_extent = authsize;
9989c4a7965SKim Phillips 
999*4de9d0b5SLee Nipper 	sg_count = talitos_map_sg(dev, areq->src,
1000*4de9d0b5SLee Nipper 				  edesc->src_nents ? : 1,
1001*4de9d0b5SLee Nipper 				  (areq->src == areq->dst) ? DMA_BIDIRECTIONAL :
1002*4de9d0b5SLee Nipper 				      DMA_TO_DEVICE,
1003*4de9d0b5SLee Nipper 				  edesc->src_is_chained);
10049c4a7965SKim Phillips 
10059c4a7965SKim Phillips 	if (sg_count == 1) {
10069c4a7965SKim Phillips 		desc->ptr[4].ptr = cpu_to_be32(sg_dma_address(areq->src));
10079c4a7965SKim Phillips 	} else {
1008fe5720e2SKim Phillips 		sg_link_tbl_len = cryptlen;
1009fe5720e2SKim Phillips 
1010fe5720e2SKim Phillips 		if ((edesc->desc.hdr & DESC_HDR_MODE1_MDEU_CICV) &&
1011fe5720e2SKim Phillips 			(edesc->desc.hdr & DESC_HDR_MODE0_ENCRYPT) == 0) {
1012fe5720e2SKim Phillips 			sg_link_tbl_len = cryptlen + authsize;
1013fe5720e2SKim Phillips 		}
1014fe5720e2SKim Phillips 		sg_count = sg_to_link_tbl(areq->src, sg_count, sg_link_tbl_len,
10159c4a7965SKim Phillips 					  &edesc->link_tbl[0]);
101670bcaca7SLee Nipper 		if (sg_count > 1) {
10179c4a7965SKim Phillips 			desc->ptr[4].j_extent |= DESC_PTR_LNKTBL_JUMP;
10189c4a7965SKim Phillips 			desc->ptr[4].ptr = cpu_to_be32(edesc->dma_link_tbl);
10199c4a7965SKim Phillips 			dma_sync_single_for_device(ctx->dev, edesc->dma_link_tbl,
10209c4a7965SKim Phillips 						   edesc->dma_len, DMA_BIDIRECTIONAL);
102170bcaca7SLee Nipper 		} else {
102270bcaca7SLee Nipper 			/* Only one segment now, so no link tbl needed */
102370bcaca7SLee Nipper 			desc->ptr[4].ptr = cpu_to_be32(sg_dma_address(areq->src));
102470bcaca7SLee Nipper 		}
10259c4a7965SKim Phillips 	}
10269c4a7965SKim Phillips 
10279c4a7965SKim Phillips 	/* cipher out */
10289c4a7965SKim Phillips 	desc->ptr[5].len = cpu_to_be16(cryptlen);
10299c4a7965SKim Phillips 	desc->ptr[5].j_extent = authsize;
10309c4a7965SKim Phillips 
10319c4a7965SKim Phillips 	if (areq->src != areq->dst) {
1032*4de9d0b5SLee Nipper 		sg_count = talitos_map_sg(dev, areq->dst,
1033*4de9d0b5SLee Nipper 					  edesc->dst_nents ? : 1,
1034*4de9d0b5SLee Nipper 					  DMA_FROM_DEVICE,
1035*4de9d0b5SLee Nipper 					  edesc->dst_is_chained);
10369c4a7965SKim Phillips 	}
10379c4a7965SKim Phillips 
10389c4a7965SKim Phillips 	if (sg_count == 1) {
10399c4a7965SKim Phillips 		desc->ptr[5].ptr = cpu_to_be32(sg_dma_address(areq->dst));
10409c4a7965SKim Phillips 	} else {
10419c4a7965SKim Phillips 		struct talitos_ptr *link_tbl_ptr =
1042f3c85bc1SLee Nipper 			&edesc->link_tbl[edesc->src_nents + 1];
10439c4a7965SKim Phillips 
10449c4a7965SKim Phillips 		desc->ptr[5].ptr = cpu_to_be32((struct talitos_ptr *)
10459c4a7965SKim Phillips 					       edesc->dma_link_tbl +
1046f3c85bc1SLee Nipper 					       edesc->src_nents + 1);
104770bcaca7SLee Nipper 		sg_count = sg_to_link_tbl(areq->dst, sg_count, cryptlen,
10489c4a7965SKim Phillips 					  link_tbl_ptr);
1049fe5720e2SKim Phillips 
1050f3c85bc1SLee Nipper 		/* Add an entry to the link table for ICV data */
10519c4a7965SKim Phillips 		link_tbl_ptr += sg_count - 1;
10529c4a7965SKim Phillips 		link_tbl_ptr->j_extent = 0;
1053f3c85bc1SLee Nipper 		sg_count++;
10549c4a7965SKim Phillips 		link_tbl_ptr++;
10559c4a7965SKim Phillips 		link_tbl_ptr->j_extent = DESC_PTR_LNKTBL_RETURN;
10569c4a7965SKim Phillips 		link_tbl_ptr->len = cpu_to_be16(authsize);
10579c4a7965SKim Phillips 
10589c4a7965SKim Phillips 		/* icv data follows link tables */
10599c4a7965SKim Phillips 		link_tbl_ptr->ptr = cpu_to_be32((struct talitos_ptr *)
10609c4a7965SKim Phillips 						edesc->dma_link_tbl +
10619c4a7965SKim Phillips 					        edesc->src_nents +
1062f3c85bc1SLee Nipper 						edesc->dst_nents + 2);
10639c4a7965SKim Phillips 
10649c4a7965SKim Phillips 		desc->ptr[5].j_extent |= DESC_PTR_LNKTBL_JUMP;
10659c4a7965SKim Phillips 		dma_sync_single_for_device(ctx->dev, edesc->dma_link_tbl,
10669c4a7965SKim Phillips 					   edesc->dma_len, DMA_BIDIRECTIONAL);
10679c4a7965SKim Phillips 	}
10689c4a7965SKim Phillips 
10699c4a7965SKim Phillips 	/* iv out */
10709c4a7965SKim Phillips 	map_single_talitos_ptr(dev, &desc->ptr[6], ivsize, ctx->iv, 0,
10719c4a7965SKim Phillips 			       DMA_FROM_DEVICE);
10729c4a7965SKim Phillips 
1073fa86a267SKim Phillips 	ret = talitos_submit(dev, desc, callback, areq);
1074fa86a267SKim Phillips 	if (ret != -EINPROGRESS) {
1075fa86a267SKim Phillips 		ipsec_esp_unmap(dev, edesc, areq);
1076fa86a267SKim Phillips 		kfree(edesc);
1077fa86a267SKim Phillips 	}
1078fa86a267SKim Phillips 	return ret;
10799c4a7965SKim Phillips }
10809c4a7965SKim Phillips 
10819c4a7965SKim Phillips 
10829c4a7965SKim Phillips /*
10839c4a7965SKim Phillips  * derive number of elements in scatterlist
10849c4a7965SKim Phillips  */
1085*4de9d0b5SLee Nipper static int sg_count(struct scatterlist *sg_list, int nbytes, int *chained)
10869c4a7965SKim Phillips {
10879c4a7965SKim Phillips 	struct scatterlist *sg = sg_list;
10889c4a7965SKim Phillips 	int sg_nents = 0;
10899c4a7965SKim Phillips 
1090*4de9d0b5SLee Nipper 	*chained = 0;
1091*4de9d0b5SLee Nipper 	while (nbytes > 0) {
10929c4a7965SKim Phillips 		sg_nents++;
10939c4a7965SKim Phillips 		nbytes -= sg->length;
1094*4de9d0b5SLee Nipper 		if (!sg_is_last(sg) && (sg + 1)->length == 0)
1095*4de9d0b5SLee Nipper 			*chained = 1;
1096*4de9d0b5SLee Nipper 		sg = scatterwalk_sg_next(sg);
10979c4a7965SKim Phillips 	}
10989c4a7965SKim Phillips 
10999c4a7965SKim Phillips 	return sg_nents;
11009c4a7965SKim Phillips }
11019c4a7965SKim Phillips 
11029c4a7965SKim Phillips /*
110356af8cd4SLee Nipper  * allocate and map the extended descriptor
11049c4a7965SKim Phillips  */
1105*4de9d0b5SLee Nipper static struct talitos_edesc *talitos_edesc_alloc(struct device *dev,
1106*4de9d0b5SLee Nipper 						 struct scatterlist *src,
1107*4de9d0b5SLee Nipper 						 struct scatterlist *dst,
1108*4de9d0b5SLee Nipper 						 unsigned int cryptlen,
1109*4de9d0b5SLee Nipper 						 unsigned int authsize,
1110*4de9d0b5SLee Nipper 						 int icv_stashing,
1111*4de9d0b5SLee Nipper 						 u32 cryptoflags)
11129c4a7965SKim Phillips {
111356af8cd4SLee Nipper 	struct talitos_edesc *edesc;
11149c4a7965SKim Phillips 	int src_nents, dst_nents, alloc_len, dma_len;
1115*4de9d0b5SLee Nipper 	int src_chained, dst_chained = 0;
1116*4de9d0b5SLee Nipper 	gfp_t flags = cryptoflags & CRYPTO_TFM_REQ_MAY_SLEEP ? GFP_KERNEL :
1117586725f8SKim Phillips 		      GFP_ATOMIC;
11189c4a7965SKim Phillips 
1119*4de9d0b5SLee Nipper 	if (cryptlen + authsize > TALITOS_MAX_DATA_LEN) {
1120*4de9d0b5SLee Nipper 		dev_err(dev, "length exceeds h/w max limit\n");
11219c4a7965SKim Phillips 		return ERR_PTR(-EINVAL);
11229c4a7965SKim Phillips 	}
11239c4a7965SKim Phillips 
1124*4de9d0b5SLee Nipper 	src_nents = sg_count(src, cryptlen + authsize, &src_chained);
11259c4a7965SKim Phillips 	src_nents = (src_nents == 1) ? 0 : src_nents;
11269c4a7965SKim Phillips 
1127*4de9d0b5SLee Nipper 	if (dst == src) {
11289c4a7965SKim Phillips 		dst_nents = src_nents;
11299c4a7965SKim Phillips 	} else {
1130*4de9d0b5SLee Nipper 		dst_nents = sg_count(dst, cryptlen + authsize, &dst_chained);
1131695ad589SLee Nipper 		dst_nents = (dst_nents == 1) ? 0 : dst_nents;
11329c4a7965SKim Phillips 	}
11339c4a7965SKim Phillips 
11349c4a7965SKim Phillips 	/*
11359c4a7965SKim Phillips 	 * allocate space for base edesc plus the link tables,
1136f3c85bc1SLee Nipper 	 * allowing for two separate entries for ICV and generated ICV (+ 2),
11379c4a7965SKim Phillips 	 * and the ICV data itself
11389c4a7965SKim Phillips 	 */
113956af8cd4SLee Nipper 	alloc_len = sizeof(struct talitos_edesc);
11409c4a7965SKim Phillips 	if (src_nents || dst_nents) {
1141f3c85bc1SLee Nipper 		dma_len = (src_nents + dst_nents + 2) *
1142*4de9d0b5SLee Nipper 				 sizeof(struct talitos_ptr) + authsize;
11439c4a7965SKim Phillips 		alloc_len += dma_len;
11449c4a7965SKim Phillips 	} else {
11459c4a7965SKim Phillips 		dma_len = 0;
1146*4de9d0b5SLee Nipper 		alloc_len += icv_stashing ? authsize : 0;
11479c4a7965SKim Phillips 	}
11489c4a7965SKim Phillips 
1149586725f8SKim Phillips 	edesc = kmalloc(alloc_len, GFP_DMA | flags);
11509c4a7965SKim Phillips 	if (!edesc) {
1151*4de9d0b5SLee Nipper 		dev_err(dev, "could not allocate edescriptor\n");
11529c4a7965SKim Phillips 		return ERR_PTR(-ENOMEM);
11539c4a7965SKim Phillips 	}
11549c4a7965SKim Phillips 
11559c4a7965SKim Phillips 	edesc->src_nents = src_nents;
11569c4a7965SKim Phillips 	edesc->dst_nents = dst_nents;
1157*4de9d0b5SLee Nipper 	edesc->src_is_chained = src_chained;
1158*4de9d0b5SLee Nipper 	edesc->dst_is_chained = dst_chained;
11599c4a7965SKim Phillips 	edesc->dma_len = dma_len;
1160*4de9d0b5SLee Nipper 	edesc->dma_link_tbl = dma_map_single(dev, &edesc->link_tbl[0],
11619c4a7965SKim Phillips 					     edesc->dma_len, DMA_BIDIRECTIONAL);
11629c4a7965SKim Phillips 
11639c4a7965SKim Phillips 	return edesc;
11649c4a7965SKim Phillips }
11659c4a7965SKim Phillips 
1166*4de9d0b5SLee Nipper static struct talitos_edesc *aead_edesc_alloc(struct aead_request *areq,
1167*4de9d0b5SLee Nipper 					      int icv_stashing)
1168*4de9d0b5SLee Nipper {
1169*4de9d0b5SLee Nipper 	struct crypto_aead *authenc = crypto_aead_reqtfm(areq);
1170*4de9d0b5SLee Nipper 	struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
1171*4de9d0b5SLee Nipper 
1172*4de9d0b5SLee Nipper 	return talitos_edesc_alloc(ctx->dev, areq->src, areq->dst,
1173*4de9d0b5SLee Nipper 				   areq->cryptlen, ctx->authsize, icv_stashing,
1174*4de9d0b5SLee Nipper 				   areq->base.flags);
1175*4de9d0b5SLee Nipper }
1176*4de9d0b5SLee Nipper 
117756af8cd4SLee Nipper static int aead_encrypt(struct aead_request *req)
11789c4a7965SKim Phillips {
11799c4a7965SKim Phillips 	struct crypto_aead *authenc = crypto_aead_reqtfm(req);
11809c4a7965SKim Phillips 	struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
118156af8cd4SLee Nipper 	struct talitos_edesc *edesc;
11829c4a7965SKim Phillips 
11839c4a7965SKim Phillips 	/* allocate extended descriptor */
1184*4de9d0b5SLee Nipper 	edesc = aead_edesc_alloc(req, 0);
11859c4a7965SKim Phillips 	if (IS_ERR(edesc))
11869c4a7965SKim Phillips 		return PTR_ERR(edesc);
11879c4a7965SKim Phillips 
11889c4a7965SKim Phillips 	/* set encrypt */
118970bcaca7SLee Nipper 	edesc->desc.hdr = ctx->desc_hdr_template | DESC_HDR_MODE0_ENCRYPT;
11909c4a7965SKim Phillips 
11919c4a7965SKim Phillips 	return ipsec_esp(edesc, req, NULL, 0, ipsec_esp_encrypt_done);
11929c4a7965SKim Phillips }
11939c4a7965SKim Phillips 
1194fe5720e2SKim Phillips 
1195fe5720e2SKim Phillips 
119656af8cd4SLee Nipper static int aead_decrypt(struct aead_request *req)
11979c4a7965SKim Phillips {
11989c4a7965SKim Phillips 	struct crypto_aead *authenc = crypto_aead_reqtfm(req);
11999c4a7965SKim Phillips 	struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
12009c4a7965SKim Phillips 	unsigned int authsize = ctx->authsize;
1201fe5720e2SKim Phillips 	struct talitos_private *priv = dev_get_drvdata(ctx->dev);
120256af8cd4SLee Nipper 	struct talitos_edesc *edesc;
12039c4a7965SKim Phillips 	struct scatterlist *sg;
12049c4a7965SKim Phillips 	void *icvdata;
12059c4a7965SKim Phillips 
12069c4a7965SKim Phillips 	req->cryptlen -= authsize;
12079c4a7965SKim Phillips 
12089c4a7965SKim Phillips 	/* allocate extended descriptor */
1209*4de9d0b5SLee Nipper 	edesc = aead_edesc_alloc(req, 1);
12109c4a7965SKim Phillips 	if (IS_ERR(edesc))
12119c4a7965SKim Phillips 		return PTR_ERR(edesc);
12129c4a7965SKim Phillips 
1213fe5720e2SKim Phillips 	if ((priv->features & TALITOS_FTR_HW_AUTH_CHECK) &&
1214fe5720e2SKim Phillips 	    (((!edesc->src_nents && !edesc->dst_nents) ||
1215fe5720e2SKim Phillips 		priv->features & TALITOS_FTR_SRC_LINK_TBL_LEN_INCLUDES_EXTENT))) {
1216fe5720e2SKim Phillips 
1217fe5720e2SKim Phillips 		/* decrypt and check the ICV */
1218fe5720e2SKim Phillips 		edesc->desc.hdr = ctx->desc_hdr_template | DESC_HDR_DIR_INBOUND |
1219fe5720e2SKim Phillips 				  DESC_HDR_MODE1_MDEU_CICV;
1220fe5720e2SKim Phillips 
1221fe5720e2SKim Phillips 		/* reset integrity check result bits */
1222fe5720e2SKim Phillips 		edesc->desc.hdr_lo = 0;
1223fe5720e2SKim Phillips 
1224fe5720e2SKim Phillips 		return ipsec_esp(edesc, req, NULL, 0, ipsec_esp_decrypt_hwauth_done);
1225fe5720e2SKim Phillips 
1226fe5720e2SKim Phillips 	} else {
1227fe5720e2SKim Phillips 
1228fe5720e2SKim Phillips 		/* Have to check the ICV with software */
1229fe5720e2SKim Phillips 
1230fe5720e2SKim Phillips 		edesc->desc.hdr = ctx->desc_hdr_template | DESC_HDR_DIR_INBOUND;
1231fe5720e2SKim Phillips 
12329c4a7965SKim Phillips 		/* stash incoming ICV for later cmp with ICV generated by the h/w */
12339c4a7965SKim Phillips 		if (edesc->dma_len)
12349c4a7965SKim Phillips 			icvdata = &edesc->link_tbl[edesc->src_nents +
1235f3c85bc1SLee Nipper 						   edesc->dst_nents + 2];
12369c4a7965SKim Phillips 		else
12379c4a7965SKim Phillips 			icvdata = &edesc->link_tbl[0];
12389c4a7965SKim Phillips 
12399c4a7965SKim Phillips 		sg = sg_last(req->src, edesc->src_nents ? : 1);
12409c4a7965SKim Phillips 
12419c4a7965SKim Phillips 		memcpy(icvdata, (char *)sg_virt(sg) + sg->length - ctx->authsize,
12429c4a7965SKim Phillips 		       ctx->authsize);
12439c4a7965SKim Phillips 
1244fe5720e2SKim Phillips 		return ipsec_esp(edesc, req, NULL, 0, ipsec_esp_decrypt_swauth_done);
1245fe5720e2SKim Phillips 	}
12469c4a7965SKim Phillips }
12479c4a7965SKim Phillips 
124856af8cd4SLee Nipper static int aead_givencrypt(struct aead_givcrypt_request *req)
12499c4a7965SKim Phillips {
12509c4a7965SKim Phillips 	struct aead_request *areq = &req->areq;
12519c4a7965SKim Phillips 	struct crypto_aead *authenc = crypto_aead_reqtfm(areq);
12529c4a7965SKim Phillips 	struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
125356af8cd4SLee Nipper 	struct talitos_edesc *edesc;
12549c4a7965SKim Phillips 
12559c4a7965SKim Phillips 	/* allocate extended descriptor */
1256*4de9d0b5SLee Nipper 	edesc = aead_edesc_alloc(areq, 0);
12579c4a7965SKim Phillips 	if (IS_ERR(edesc))
12589c4a7965SKim Phillips 		return PTR_ERR(edesc);
12599c4a7965SKim Phillips 
12609c4a7965SKim Phillips 	/* set encrypt */
126170bcaca7SLee Nipper 	edesc->desc.hdr = ctx->desc_hdr_template | DESC_HDR_MODE0_ENCRYPT;
12629c4a7965SKim Phillips 
12639c4a7965SKim Phillips 	memcpy(req->giv, ctx->iv, crypto_aead_ivsize(authenc));
1264ba95487dSKim Phillips 	/* avoid consecutive packets going out with same IV */
1265ba95487dSKim Phillips 	*(__be64 *)req->giv ^= cpu_to_be64(req->seq);
12669c4a7965SKim Phillips 
12679c4a7965SKim Phillips 	return ipsec_esp(edesc, areq, req->giv, req->seq,
12689c4a7965SKim Phillips 			 ipsec_esp_encrypt_done);
12699c4a7965SKim Phillips }
12709c4a7965SKim Phillips 
1271*4de9d0b5SLee Nipper static int ablkcipher_setkey(struct crypto_ablkcipher *cipher,
1272*4de9d0b5SLee Nipper 			     const u8 *key, unsigned int keylen)
1273*4de9d0b5SLee Nipper {
1274*4de9d0b5SLee Nipper 	struct talitos_ctx *ctx = crypto_ablkcipher_ctx(cipher);
1275*4de9d0b5SLee Nipper 	struct ablkcipher_alg *alg = crypto_ablkcipher_alg(cipher);
1276*4de9d0b5SLee Nipper 
1277*4de9d0b5SLee Nipper 	if (keylen > TALITOS_MAX_KEY_SIZE)
1278*4de9d0b5SLee Nipper 		goto badkey;
1279*4de9d0b5SLee Nipper 
1280*4de9d0b5SLee Nipper 	if (keylen < alg->min_keysize || keylen > alg->max_keysize)
1281*4de9d0b5SLee Nipper 		goto badkey;
1282*4de9d0b5SLee Nipper 
1283*4de9d0b5SLee Nipper 	memcpy(&ctx->key, key, keylen);
1284*4de9d0b5SLee Nipper 	ctx->keylen = keylen;
1285*4de9d0b5SLee Nipper 
1286*4de9d0b5SLee Nipper 	return 0;
1287*4de9d0b5SLee Nipper 
1288*4de9d0b5SLee Nipper badkey:
1289*4de9d0b5SLee Nipper 	crypto_ablkcipher_set_flags(cipher, CRYPTO_TFM_RES_BAD_KEY_LEN);
1290*4de9d0b5SLee Nipper 	return -EINVAL;
1291*4de9d0b5SLee Nipper }
1292*4de9d0b5SLee Nipper 
1293*4de9d0b5SLee Nipper static void common_nonsnoop_unmap(struct device *dev,
1294*4de9d0b5SLee Nipper 				  struct talitos_edesc *edesc,
1295*4de9d0b5SLee Nipper 				  struct ablkcipher_request *areq)
1296*4de9d0b5SLee Nipper {
1297*4de9d0b5SLee Nipper 	unmap_single_talitos_ptr(dev, &edesc->desc.ptr[5], DMA_FROM_DEVICE);
1298*4de9d0b5SLee Nipper 	unmap_single_talitos_ptr(dev, &edesc->desc.ptr[2], DMA_TO_DEVICE);
1299*4de9d0b5SLee Nipper 	unmap_single_talitos_ptr(dev, &edesc->desc.ptr[1], DMA_TO_DEVICE);
1300*4de9d0b5SLee Nipper 
1301*4de9d0b5SLee Nipper 	talitos_sg_unmap(dev, edesc, areq->src, areq->dst);
1302*4de9d0b5SLee Nipper 
1303*4de9d0b5SLee Nipper 	if (edesc->dma_len)
1304*4de9d0b5SLee Nipper 		dma_unmap_single(dev, edesc->dma_link_tbl, edesc->dma_len,
1305*4de9d0b5SLee Nipper 				 DMA_BIDIRECTIONAL);
1306*4de9d0b5SLee Nipper }
1307*4de9d0b5SLee Nipper 
1308*4de9d0b5SLee Nipper static void ablkcipher_done(struct device *dev,
1309*4de9d0b5SLee Nipper 			    struct talitos_desc *desc, void *context,
1310*4de9d0b5SLee Nipper 			    int err)
1311*4de9d0b5SLee Nipper {
1312*4de9d0b5SLee Nipper 	struct ablkcipher_request *areq = context;
1313*4de9d0b5SLee Nipper 	struct talitos_edesc *edesc =
1314*4de9d0b5SLee Nipper 		 container_of(desc, struct talitos_edesc, desc);
1315*4de9d0b5SLee Nipper 
1316*4de9d0b5SLee Nipper 	common_nonsnoop_unmap(dev, edesc, areq);
1317*4de9d0b5SLee Nipper 
1318*4de9d0b5SLee Nipper 	kfree(edesc);
1319*4de9d0b5SLee Nipper 
1320*4de9d0b5SLee Nipper 	areq->base.complete(&areq->base, err);
1321*4de9d0b5SLee Nipper }
1322*4de9d0b5SLee Nipper 
1323*4de9d0b5SLee Nipper static int common_nonsnoop(struct talitos_edesc *edesc,
1324*4de9d0b5SLee Nipper 			   struct ablkcipher_request *areq,
1325*4de9d0b5SLee Nipper 			   u8 *giv,
1326*4de9d0b5SLee Nipper 			   void (*callback) (struct device *dev,
1327*4de9d0b5SLee Nipper 					     struct talitos_desc *desc,
1328*4de9d0b5SLee Nipper 					     void *context, int error))
1329*4de9d0b5SLee Nipper {
1330*4de9d0b5SLee Nipper 	struct crypto_ablkcipher *cipher = crypto_ablkcipher_reqtfm(areq);
1331*4de9d0b5SLee Nipper 	struct talitos_ctx *ctx = crypto_ablkcipher_ctx(cipher);
1332*4de9d0b5SLee Nipper 	struct device *dev = ctx->dev;
1333*4de9d0b5SLee Nipper 	struct talitos_desc *desc = &edesc->desc;
1334*4de9d0b5SLee Nipper 	unsigned int cryptlen = areq->nbytes;
1335*4de9d0b5SLee Nipper 	unsigned int ivsize;
1336*4de9d0b5SLee Nipper 	int sg_count, ret;
1337*4de9d0b5SLee Nipper 
1338*4de9d0b5SLee Nipper 	/* first DWORD empty */
1339*4de9d0b5SLee Nipper 	desc->ptr[0].len = 0;
1340*4de9d0b5SLee Nipper 	desc->ptr[0].ptr = 0;
1341*4de9d0b5SLee Nipper 	desc->ptr[0].j_extent = 0;
1342*4de9d0b5SLee Nipper 
1343*4de9d0b5SLee Nipper 	/* cipher iv */
1344*4de9d0b5SLee Nipper 	ivsize = crypto_ablkcipher_ivsize(cipher);
1345*4de9d0b5SLee Nipper 	map_single_talitos_ptr(dev, &desc->ptr[1], ivsize, giv ?: areq->info, 0,
1346*4de9d0b5SLee Nipper 			       DMA_TO_DEVICE);
1347*4de9d0b5SLee Nipper 
1348*4de9d0b5SLee Nipper 	/* cipher key */
1349*4de9d0b5SLee Nipper 	map_single_talitos_ptr(dev, &desc->ptr[2], ctx->keylen,
1350*4de9d0b5SLee Nipper 			       (char *)&ctx->key, 0, DMA_TO_DEVICE);
1351*4de9d0b5SLee Nipper 
1352*4de9d0b5SLee Nipper 	/*
1353*4de9d0b5SLee Nipper 	 * cipher in
1354*4de9d0b5SLee Nipper 	 */
1355*4de9d0b5SLee Nipper 	desc->ptr[3].len = cpu_to_be16(cryptlen);
1356*4de9d0b5SLee Nipper 	desc->ptr[3].j_extent = 0;
1357*4de9d0b5SLee Nipper 
1358*4de9d0b5SLee Nipper 	sg_count = talitos_map_sg(dev, areq->src, edesc->src_nents ? : 1,
1359*4de9d0b5SLee Nipper 				  (areq->src == areq->dst) ? DMA_BIDIRECTIONAL
1360*4de9d0b5SLee Nipper 							   : DMA_TO_DEVICE,
1361*4de9d0b5SLee Nipper 				  edesc->src_is_chained);
1362*4de9d0b5SLee Nipper 
1363*4de9d0b5SLee Nipper 	if (sg_count == 1) {
1364*4de9d0b5SLee Nipper 		desc->ptr[3].ptr = cpu_to_be32(sg_dma_address(areq->src));
1365*4de9d0b5SLee Nipper 	} else {
1366*4de9d0b5SLee Nipper 		sg_count = sg_to_link_tbl(areq->src, sg_count, cryptlen,
1367*4de9d0b5SLee Nipper 					  &edesc->link_tbl[0]);
1368*4de9d0b5SLee Nipper 		if (sg_count > 1) {
1369*4de9d0b5SLee Nipper 			desc->ptr[3].j_extent |= DESC_PTR_LNKTBL_JUMP;
1370*4de9d0b5SLee Nipper 			desc->ptr[3].ptr = cpu_to_be32(edesc->dma_link_tbl);
1371*4de9d0b5SLee Nipper 			dma_sync_single_for_device(ctx->dev, edesc->dma_link_tbl,
1372*4de9d0b5SLee Nipper 						   edesc->dma_len, DMA_BIDIRECTIONAL);
1373*4de9d0b5SLee Nipper 		} else {
1374*4de9d0b5SLee Nipper 			/* Only one segment now, so no link tbl needed */
1375*4de9d0b5SLee Nipper 			desc->ptr[3].ptr = cpu_to_be32(sg_dma_address(areq->src));
1376*4de9d0b5SLee Nipper 		}
1377*4de9d0b5SLee Nipper 	}
1378*4de9d0b5SLee Nipper 
1379*4de9d0b5SLee Nipper 	/* cipher out */
1380*4de9d0b5SLee Nipper 	desc->ptr[4].len = cpu_to_be16(cryptlen);
1381*4de9d0b5SLee Nipper 	desc->ptr[4].j_extent = 0;
1382*4de9d0b5SLee Nipper 
1383*4de9d0b5SLee Nipper 	if (areq->src != areq->dst)
1384*4de9d0b5SLee Nipper 		sg_count = talitos_map_sg(dev, areq->dst,
1385*4de9d0b5SLee Nipper 					  edesc->dst_nents ? : 1,
1386*4de9d0b5SLee Nipper 					  DMA_FROM_DEVICE,
1387*4de9d0b5SLee Nipper 					  edesc->dst_is_chained);
1388*4de9d0b5SLee Nipper 
1389*4de9d0b5SLee Nipper 	if (sg_count == 1) {
1390*4de9d0b5SLee Nipper 		desc->ptr[4].ptr = cpu_to_be32(sg_dma_address(areq->dst));
1391*4de9d0b5SLee Nipper 	} else {
1392*4de9d0b5SLee Nipper 		struct talitos_ptr *link_tbl_ptr =
1393*4de9d0b5SLee Nipper 			&edesc->link_tbl[edesc->src_nents + 1];
1394*4de9d0b5SLee Nipper 
1395*4de9d0b5SLee Nipper 		desc->ptr[4].j_extent |= DESC_PTR_LNKTBL_JUMP;
1396*4de9d0b5SLee Nipper 		desc->ptr[4].ptr = cpu_to_be32((struct talitos_ptr *)
1397*4de9d0b5SLee Nipper 					       edesc->dma_link_tbl +
1398*4de9d0b5SLee Nipper 					       edesc->src_nents + 1);
1399*4de9d0b5SLee Nipper 		sg_count = sg_to_link_tbl(areq->dst, sg_count, cryptlen,
1400*4de9d0b5SLee Nipper 					  link_tbl_ptr);
1401*4de9d0b5SLee Nipper 		dma_sync_single_for_device(ctx->dev, edesc->dma_link_tbl,
1402*4de9d0b5SLee Nipper 					   edesc->dma_len, DMA_BIDIRECTIONAL);
1403*4de9d0b5SLee Nipper 	}
1404*4de9d0b5SLee Nipper 
1405*4de9d0b5SLee Nipper 	/* iv out */
1406*4de9d0b5SLee Nipper 	map_single_talitos_ptr(dev, &desc->ptr[5], ivsize, ctx->iv, 0,
1407*4de9d0b5SLee Nipper 			       DMA_FROM_DEVICE);
1408*4de9d0b5SLee Nipper 
1409*4de9d0b5SLee Nipper 	/* last DWORD empty */
1410*4de9d0b5SLee Nipper 	desc->ptr[6].len = 0;
1411*4de9d0b5SLee Nipper 	desc->ptr[6].ptr = 0;
1412*4de9d0b5SLee Nipper 	desc->ptr[6].j_extent = 0;
1413*4de9d0b5SLee Nipper 
1414*4de9d0b5SLee Nipper 	ret = talitos_submit(dev, desc, callback, areq);
1415*4de9d0b5SLee Nipper 	if (ret != -EINPROGRESS) {
1416*4de9d0b5SLee Nipper 		common_nonsnoop_unmap(dev, edesc, areq);
1417*4de9d0b5SLee Nipper 		kfree(edesc);
1418*4de9d0b5SLee Nipper 	}
1419*4de9d0b5SLee Nipper 	return ret;
1420*4de9d0b5SLee Nipper }
1421*4de9d0b5SLee Nipper 
1422*4de9d0b5SLee Nipper static struct talitos_edesc *ablkcipher_edesc_alloc(struct ablkcipher_request *areq)
1423*4de9d0b5SLee Nipper {
1424*4de9d0b5SLee Nipper 	struct crypto_ablkcipher *cipher = crypto_ablkcipher_reqtfm(areq);
1425*4de9d0b5SLee Nipper 	struct talitos_ctx *ctx = crypto_ablkcipher_ctx(cipher);
1426*4de9d0b5SLee Nipper 
1427*4de9d0b5SLee Nipper 	return talitos_edesc_alloc(ctx->dev, areq->src, areq->dst, areq->nbytes,
1428*4de9d0b5SLee Nipper 				   0, 0, areq->base.flags);
1429*4de9d0b5SLee Nipper }
1430*4de9d0b5SLee Nipper 
1431*4de9d0b5SLee Nipper static int ablkcipher_encrypt(struct ablkcipher_request *areq)
1432*4de9d0b5SLee Nipper {
1433*4de9d0b5SLee Nipper 	struct crypto_ablkcipher *cipher = crypto_ablkcipher_reqtfm(areq);
1434*4de9d0b5SLee Nipper 	struct talitos_ctx *ctx = crypto_ablkcipher_ctx(cipher);
1435*4de9d0b5SLee Nipper 	struct talitos_edesc *edesc;
1436*4de9d0b5SLee Nipper 
1437*4de9d0b5SLee Nipper 	/* allocate extended descriptor */
1438*4de9d0b5SLee Nipper 	edesc = ablkcipher_edesc_alloc(areq);
1439*4de9d0b5SLee Nipper 	if (IS_ERR(edesc))
1440*4de9d0b5SLee Nipper 		return PTR_ERR(edesc);
1441*4de9d0b5SLee Nipper 
1442*4de9d0b5SLee Nipper 	/* set encrypt */
1443*4de9d0b5SLee Nipper 	edesc->desc.hdr = ctx->desc_hdr_template | DESC_HDR_MODE0_ENCRYPT;
1444*4de9d0b5SLee Nipper 
1445*4de9d0b5SLee Nipper 	return common_nonsnoop(edesc, areq, NULL, ablkcipher_done);
1446*4de9d0b5SLee Nipper }
1447*4de9d0b5SLee Nipper 
1448*4de9d0b5SLee Nipper static int ablkcipher_decrypt(struct ablkcipher_request *areq)
1449*4de9d0b5SLee Nipper {
1450*4de9d0b5SLee Nipper 	struct crypto_ablkcipher *cipher = crypto_ablkcipher_reqtfm(areq);
1451*4de9d0b5SLee Nipper 	struct talitos_ctx *ctx = crypto_ablkcipher_ctx(cipher);
1452*4de9d0b5SLee Nipper 	struct talitos_edesc *edesc;
1453*4de9d0b5SLee Nipper 
1454*4de9d0b5SLee Nipper 	/* allocate extended descriptor */
1455*4de9d0b5SLee Nipper 	edesc = ablkcipher_edesc_alloc(areq);
1456*4de9d0b5SLee Nipper 	if (IS_ERR(edesc))
1457*4de9d0b5SLee Nipper 		return PTR_ERR(edesc);
1458*4de9d0b5SLee Nipper 
1459*4de9d0b5SLee Nipper 	edesc->desc.hdr = ctx->desc_hdr_template | DESC_HDR_DIR_INBOUND;
1460*4de9d0b5SLee Nipper 
1461*4de9d0b5SLee Nipper 	return common_nonsnoop(edesc, areq, NULL, ablkcipher_done);
1462*4de9d0b5SLee Nipper }
1463*4de9d0b5SLee Nipper 
14649c4a7965SKim Phillips struct talitos_alg_template {
146556af8cd4SLee Nipper 	struct crypto_alg alg;
14669c4a7965SKim Phillips 	__be32 desc_hdr_template;
14679c4a7965SKim Phillips };
14689c4a7965SKim Phillips 
14699c4a7965SKim Phillips static struct talitos_alg_template driver_algs[] = {
147056af8cd4SLee Nipper 	/* AEAD algorithms.  These use a single-pass ipsec_esp descriptor */
14719c4a7965SKim Phillips 	{
147256af8cd4SLee Nipper 		.alg = {
147356af8cd4SLee Nipper 			.cra_name = "authenc(hmac(sha1),cbc(aes))",
147456af8cd4SLee Nipper 			.cra_driver_name = "authenc-hmac-sha1-cbc-aes-talitos",
147556af8cd4SLee Nipper 			.cra_blocksize = AES_BLOCK_SIZE,
147656af8cd4SLee Nipper 			.cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
147756af8cd4SLee Nipper 			.cra_type = &crypto_aead_type,
147856af8cd4SLee Nipper 			.cra_aead = {
147956af8cd4SLee Nipper 				.setkey = aead_setkey,
148056af8cd4SLee Nipper 				.setauthsize = aead_setauthsize,
148156af8cd4SLee Nipper 				.encrypt = aead_encrypt,
148256af8cd4SLee Nipper 				.decrypt = aead_decrypt,
148356af8cd4SLee Nipper 				.givencrypt = aead_givencrypt,
14849c4a7965SKim Phillips 				.geniv = "<built-in>",
14853952f17eSLee Nipper 				.ivsize = AES_BLOCK_SIZE,
14863952f17eSLee Nipper 				.maxauthsize = SHA1_DIGEST_SIZE,
148756af8cd4SLee Nipper 			}
14889c4a7965SKim Phillips 		},
14899c4a7965SKim Phillips 		.desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
14909c4a7965SKim Phillips 			             DESC_HDR_SEL0_AESU |
14919c4a7965SKim Phillips 		                     DESC_HDR_MODE0_AESU_CBC |
14929c4a7965SKim Phillips 		                     DESC_HDR_SEL1_MDEUA |
14939c4a7965SKim Phillips 		                     DESC_HDR_MODE1_MDEU_INIT |
14949c4a7965SKim Phillips 		                     DESC_HDR_MODE1_MDEU_PAD |
14959c4a7965SKim Phillips 		                     DESC_HDR_MODE1_MDEU_SHA1_HMAC,
149670bcaca7SLee Nipper 	},
149770bcaca7SLee Nipper 	{
149856af8cd4SLee Nipper 		.alg = {
149956af8cd4SLee Nipper 			.cra_name = "authenc(hmac(sha1),cbc(des3_ede))",
150056af8cd4SLee Nipper 			.cra_driver_name = "authenc-hmac-sha1-cbc-3des-talitos",
150156af8cd4SLee Nipper 			.cra_blocksize = DES3_EDE_BLOCK_SIZE,
150256af8cd4SLee Nipper 			.cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
150356af8cd4SLee Nipper 			.cra_type = &crypto_aead_type,
150456af8cd4SLee Nipper 			.cra_aead = {
150556af8cd4SLee Nipper 				.setkey = aead_setkey,
150656af8cd4SLee Nipper 				.setauthsize = aead_setauthsize,
150756af8cd4SLee Nipper 				.encrypt = aead_encrypt,
150856af8cd4SLee Nipper 				.decrypt = aead_decrypt,
150956af8cd4SLee Nipper 				.givencrypt = aead_givencrypt,
151070bcaca7SLee Nipper 				.geniv = "<built-in>",
15113952f17eSLee Nipper 				.ivsize = DES3_EDE_BLOCK_SIZE,
15123952f17eSLee Nipper 				.maxauthsize = SHA1_DIGEST_SIZE,
151356af8cd4SLee Nipper 			}
151470bcaca7SLee Nipper 		},
151570bcaca7SLee Nipper 		.desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
151670bcaca7SLee Nipper 			             DESC_HDR_SEL0_DEU |
151770bcaca7SLee Nipper 		                     DESC_HDR_MODE0_DEU_CBC |
151870bcaca7SLee Nipper 		                     DESC_HDR_MODE0_DEU_3DES |
151970bcaca7SLee Nipper 		                     DESC_HDR_SEL1_MDEUA |
152070bcaca7SLee Nipper 		                     DESC_HDR_MODE1_MDEU_INIT |
152170bcaca7SLee Nipper 		                     DESC_HDR_MODE1_MDEU_PAD |
152270bcaca7SLee Nipper 		                     DESC_HDR_MODE1_MDEU_SHA1_HMAC,
15233952f17eSLee Nipper 	},
15243952f17eSLee Nipper 	{
152556af8cd4SLee Nipper 		.alg = {
152656af8cd4SLee Nipper 			.cra_name = "authenc(hmac(sha256),cbc(aes))",
152756af8cd4SLee Nipper 			.cra_driver_name = "authenc-hmac-sha256-cbc-aes-talitos",
152856af8cd4SLee Nipper 			.cra_blocksize = AES_BLOCK_SIZE,
152956af8cd4SLee Nipper 			.cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
153056af8cd4SLee Nipper 			.cra_type = &crypto_aead_type,
153156af8cd4SLee Nipper 			.cra_aead = {
153256af8cd4SLee Nipper 				.setkey = aead_setkey,
153356af8cd4SLee Nipper 				.setauthsize = aead_setauthsize,
153456af8cd4SLee Nipper 				.encrypt = aead_encrypt,
153556af8cd4SLee Nipper 				.decrypt = aead_decrypt,
153656af8cd4SLee Nipper 				.givencrypt = aead_givencrypt,
15373952f17eSLee Nipper 				.geniv = "<built-in>",
15383952f17eSLee Nipper 				.ivsize = AES_BLOCK_SIZE,
15393952f17eSLee Nipper 				.maxauthsize = SHA256_DIGEST_SIZE,
154056af8cd4SLee Nipper 			}
15413952f17eSLee Nipper 		},
15423952f17eSLee Nipper 		.desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
15433952f17eSLee Nipper 			             DESC_HDR_SEL0_AESU |
15443952f17eSLee Nipper 		                     DESC_HDR_MODE0_AESU_CBC |
15453952f17eSLee Nipper 		                     DESC_HDR_SEL1_MDEUA |
15463952f17eSLee Nipper 		                     DESC_HDR_MODE1_MDEU_INIT |
15473952f17eSLee Nipper 		                     DESC_HDR_MODE1_MDEU_PAD |
15483952f17eSLee Nipper 		                     DESC_HDR_MODE1_MDEU_SHA256_HMAC,
15493952f17eSLee Nipper 	},
15503952f17eSLee Nipper 	{
155156af8cd4SLee Nipper 		.alg = {
155256af8cd4SLee Nipper 			.cra_name = "authenc(hmac(sha256),cbc(des3_ede))",
155356af8cd4SLee Nipper 			.cra_driver_name = "authenc-hmac-sha256-cbc-3des-talitos",
155456af8cd4SLee Nipper 			.cra_blocksize = DES3_EDE_BLOCK_SIZE,
155556af8cd4SLee Nipper 			.cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
155656af8cd4SLee Nipper 			.cra_type = &crypto_aead_type,
155756af8cd4SLee Nipper 			.cra_aead = {
155856af8cd4SLee Nipper 				.setkey = aead_setkey,
155956af8cd4SLee Nipper 				.setauthsize = aead_setauthsize,
156056af8cd4SLee Nipper 				.encrypt = aead_encrypt,
156156af8cd4SLee Nipper 				.decrypt = aead_decrypt,
156256af8cd4SLee Nipper 				.givencrypt = aead_givencrypt,
15633952f17eSLee Nipper 				.geniv = "<built-in>",
15643952f17eSLee Nipper 				.ivsize = DES3_EDE_BLOCK_SIZE,
15653952f17eSLee Nipper 				.maxauthsize = SHA256_DIGEST_SIZE,
156656af8cd4SLee Nipper 			}
15673952f17eSLee Nipper 		},
15683952f17eSLee Nipper 		.desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
15693952f17eSLee Nipper 			             DESC_HDR_SEL0_DEU |
15703952f17eSLee Nipper 		                     DESC_HDR_MODE0_DEU_CBC |
15713952f17eSLee Nipper 		                     DESC_HDR_MODE0_DEU_3DES |
15723952f17eSLee Nipper 		                     DESC_HDR_SEL1_MDEUA |
15733952f17eSLee Nipper 		                     DESC_HDR_MODE1_MDEU_INIT |
15743952f17eSLee Nipper 		                     DESC_HDR_MODE1_MDEU_PAD |
15753952f17eSLee Nipper 		                     DESC_HDR_MODE1_MDEU_SHA256_HMAC,
15763952f17eSLee Nipper 	},
15773952f17eSLee Nipper 	{
157856af8cd4SLee Nipper 		.alg = {
157956af8cd4SLee Nipper 			.cra_name = "authenc(hmac(md5),cbc(aes))",
158056af8cd4SLee Nipper 			.cra_driver_name = "authenc-hmac-md5-cbc-aes-talitos",
158156af8cd4SLee Nipper 			.cra_blocksize = AES_BLOCK_SIZE,
158256af8cd4SLee Nipper 			.cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
158356af8cd4SLee Nipper 			.cra_type = &crypto_aead_type,
158456af8cd4SLee Nipper 			.cra_aead = {
158556af8cd4SLee Nipper 				.setkey = aead_setkey,
158656af8cd4SLee Nipper 				.setauthsize = aead_setauthsize,
158756af8cd4SLee Nipper 				.encrypt = aead_encrypt,
158856af8cd4SLee Nipper 				.decrypt = aead_decrypt,
158956af8cd4SLee Nipper 				.givencrypt = aead_givencrypt,
15903952f17eSLee Nipper 				.geniv = "<built-in>",
15913952f17eSLee Nipper 				.ivsize = AES_BLOCK_SIZE,
15923952f17eSLee Nipper 				.maxauthsize = MD5_DIGEST_SIZE,
159356af8cd4SLee Nipper 			}
15943952f17eSLee Nipper 		},
15953952f17eSLee Nipper 		.desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
15963952f17eSLee Nipper 			             DESC_HDR_SEL0_AESU |
15973952f17eSLee Nipper 		                     DESC_HDR_MODE0_AESU_CBC |
15983952f17eSLee Nipper 		                     DESC_HDR_SEL1_MDEUA |
15993952f17eSLee Nipper 		                     DESC_HDR_MODE1_MDEU_INIT |
16003952f17eSLee Nipper 		                     DESC_HDR_MODE1_MDEU_PAD |
16013952f17eSLee Nipper 		                     DESC_HDR_MODE1_MDEU_MD5_HMAC,
16023952f17eSLee Nipper 	},
16033952f17eSLee Nipper 	{
160456af8cd4SLee Nipper 		.alg = {
160556af8cd4SLee Nipper 			.cra_name = "authenc(hmac(md5),cbc(des3_ede))",
160656af8cd4SLee Nipper 			.cra_driver_name = "authenc-hmac-md5-cbc-3des-talitos",
160756af8cd4SLee Nipper 			.cra_blocksize = DES3_EDE_BLOCK_SIZE,
160856af8cd4SLee Nipper 			.cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
160956af8cd4SLee Nipper 			.cra_type = &crypto_aead_type,
161056af8cd4SLee Nipper 			.cra_aead = {
161156af8cd4SLee Nipper 				.setkey = aead_setkey,
161256af8cd4SLee Nipper 				.setauthsize = aead_setauthsize,
161356af8cd4SLee Nipper 				.encrypt = aead_encrypt,
161456af8cd4SLee Nipper 				.decrypt = aead_decrypt,
161556af8cd4SLee Nipper 				.givencrypt = aead_givencrypt,
16163952f17eSLee Nipper 				.geniv = "<built-in>",
16173952f17eSLee Nipper 				.ivsize = DES3_EDE_BLOCK_SIZE,
16183952f17eSLee Nipper 				.maxauthsize = MD5_DIGEST_SIZE,
161956af8cd4SLee Nipper 			}
16203952f17eSLee Nipper 		},
16213952f17eSLee Nipper 		.desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
16223952f17eSLee Nipper 			             DESC_HDR_SEL0_DEU |
16233952f17eSLee Nipper 		                     DESC_HDR_MODE0_DEU_CBC |
16243952f17eSLee Nipper 		                     DESC_HDR_MODE0_DEU_3DES |
16253952f17eSLee Nipper 		                     DESC_HDR_SEL1_MDEUA |
16263952f17eSLee Nipper 		                     DESC_HDR_MODE1_MDEU_INIT |
16273952f17eSLee Nipper 		                     DESC_HDR_MODE1_MDEU_PAD |
16283952f17eSLee Nipper 		                     DESC_HDR_MODE1_MDEU_MD5_HMAC,
1629*4de9d0b5SLee Nipper 	},
1630*4de9d0b5SLee Nipper 	/* ABLKCIPHER algorithms. */
1631*4de9d0b5SLee Nipper 	{
1632*4de9d0b5SLee Nipper 		.alg = {
1633*4de9d0b5SLee Nipper 			.cra_name = "cbc(aes)",
1634*4de9d0b5SLee Nipper 			.cra_driver_name = "cbc-aes-talitos",
1635*4de9d0b5SLee Nipper 			.cra_blocksize = AES_BLOCK_SIZE,
1636*4de9d0b5SLee Nipper 			.cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
1637*4de9d0b5SLee Nipper                                      CRYPTO_ALG_ASYNC,
1638*4de9d0b5SLee Nipper 			.cra_type = &crypto_ablkcipher_type,
1639*4de9d0b5SLee Nipper 			.cra_ablkcipher = {
1640*4de9d0b5SLee Nipper 				.setkey = ablkcipher_setkey,
1641*4de9d0b5SLee Nipper 				.encrypt = ablkcipher_encrypt,
1642*4de9d0b5SLee Nipper 				.decrypt = ablkcipher_decrypt,
1643*4de9d0b5SLee Nipper 				.geniv = "eseqiv",
1644*4de9d0b5SLee Nipper 				.min_keysize = AES_MIN_KEY_SIZE,
1645*4de9d0b5SLee Nipper 				.max_keysize = AES_MAX_KEY_SIZE,
1646*4de9d0b5SLee Nipper 				.ivsize = AES_BLOCK_SIZE,
1647*4de9d0b5SLee Nipper 			}
1648*4de9d0b5SLee Nipper 		},
1649*4de9d0b5SLee Nipper 		.desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
1650*4de9d0b5SLee Nipper 				     DESC_HDR_SEL0_AESU |
1651*4de9d0b5SLee Nipper 				     DESC_HDR_MODE0_AESU_CBC,
1652*4de9d0b5SLee Nipper 	},
1653*4de9d0b5SLee Nipper 	{
1654*4de9d0b5SLee Nipper 		.alg = {
1655*4de9d0b5SLee Nipper 			.cra_name = "cbc(des3_ede)",
1656*4de9d0b5SLee Nipper 			.cra_driver_name = "cbc-3des-talitos",
1657*4de9d0b5SLee Nipper 			.cra_blocksize = DES3_EDE_BLOCK_SIZE,
1658*4de9d0b5SLee Nipper 			.cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
1659*4de9d0b5SLee Nipper                                      CRYPTO_ALG_ASYNC,
1660*4de9d0b5SLee Nipper 			.cra_type = &crypto_ablkcipher_type,
1661*4de9d0b5SLee Nipper 			.cra_ablkcipher = {
1662*4de9d0b5SLee Nipper 				.setkey = ablkcipher_setkey,
1663*4de9d0b5SLee Nipper 				.encrypt = ablkcipher_encrypt,
1664*4de9d0b5SLee Nipper 				.decrypt = ablkcipher_decrypt,
1665*4de9d0b5SLee Nipper 				.geniv = "eseqiv",
1666*4de9d0b5SLee Nipper 				.min_keysize = DES3_EDE_KEY_SIZE,
1667*4de9d0b5SLee Nipper 				.max_keysize = DES3_EDE_KEY_SIZE,
1668*4de9d0b5SLee Nipper 				.ivsize = DES3_EDE_BLOCK_SIZE,
1669*4de9d0b5SLee Nipper 			}
1670*4de9d0b5SLee Nipper 		},
1671*4de9d0b5SLee Nipper 		.desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
1672*4de9d0b5SLee Nipper 			             DESC_HDR_SEL0_DEU |
1673*4de9d0b5SLee Nipper 		                     DESC_HDR_MODE0_DEU_CBC |
1674*4de9d0b5SLee Nipper 		                     DESC_HDR_MODE0_DEU_3DES,
16759c4a7965SKim Phillips 	}
16769c4a7965SKim Phillips };
16779c4a7965SKim Phillips 
16789c4a7965SKim Phillips struct talitos_crypto_alg {
16799c4a7965SKim Phillips 	struct list_head entry;
16809c4a7965SKim Phillips 	struct device *dev;
16819c4a7965SKim Phillips 	__be32 desc_hdr_template;
16829c4a7965SKim Phillips 	struct crypto_alg crypto_alg;
16839c4a7965SKim Phillips };
16849c4a7965SKim Phillips 
16859c4a7965SKim Phillips static int talitos_cra_init(struct crypto_tfm *tfm)
16869c4a7965SKim Phillips {
16879c4a7965SKim Phillips 	struct crypto_alg *alg = tfm->__crt_alg;
16889c4a7965SKim Phillips 	struct talitos_crypto_alg *talitos_alg =
16899c4a7965SKim Phillips 		 container_of(alg, struct talitos_crypto_alg, crypto_alg);
16909c4a7965SKim Phillips 	struct talitos_ctx *ctx = crypto_tfm_ctx(tfm);
16919c4a7965SKim Phillips 
16929c4a7965SKim Phillips 	/* update context with ptr to dev */
16939c4a7965SKim Phillips 	ctx->dev = talitos_alg->dev;
16949c4a7965SKim Phillips 	/* copy descriptor header template value */
16959c4a7965SKim Phillips 	ctx->desc_hdr_template = talitos_alg->desc_hdr_template;
16969c4a7965SKim Phillips 
16979c4a7965SKim Phillips 	/* random first IV */
169870bcaca7SLee Nipper 	get_random_bytes(ctx->iv, TALITOS_MAX_IV_LENGTH);
16999c4a7965SKim Phillips 
17009c4a7965SKim Phillips 	return 0;
17019c4a7965SKim Phillips }
17029c4a7965SKim Phillips 
17039c4a7965SKim Phillips /*
17049c4a7965SKim Phillips  * given the alg's descriptor header template, determine whether descriptor
17059c4a7965SKim Phillips  * type and primary/secondary execution units required match the hw
17069c4a7965SKim Phillips  * capabilities description provided in the device tree node.
17079c4a7965SKim Phillips  */
17089c4a7965SKim Phillips static int hw_supports(struct device *dev, __be32 desc_hdr_template)
17099c4a7965SKim Phillips {
17109c4a7965SKim Phillips 	struct talitos_private *priv = dev_get_drvdata(dev);
17119c4a7965SKim Phillips 	int ret;
17129c4a7965SKim Phillips 
17139c4a7965SKim Phillips 	ret = (1 << DESC_TYPE(desc_hdr_template) & priv->desc_types) &&
17149c4a7965SKim Phillips 	      (1 << PRIMARY_EU(desc_hdr_template) & priv->exec_units);
17159c4a7965SKim Phillips 
17169c4a7965SKim Phillips 	if (SECONDARY_EU(desc_hdr_template))
17179c4a7965SKim Phillips 		ret = ret && (1 << SECONDARY_EU(desc_hdr_template)
17189c4a7965SKim Phillips 		              & priv->exec_units);
17199c4a7965SKim Phillips 
17209c4a7965SKim Phillips 	return ret;
17219c4a7965SKim Phillips }
17229c4a7965SKim Phillips 
1723596f1034SAl Viro static int talitos_remove(struct of_device *ofdev)
17249c4a7965SKim Phillips {
17259c4a7965SKim Phillips 	struct device *dev = &ofdev->dev;
17269c4a7965SKim Phillips 	struct talitos_private *priv = dev_get_drvdata(dev);
17279c4a7965SKim Phillips 	struct talitos_crypto_alg *t_alg, *n;
17289c4a7965SKim Phillips 	int i;
17299c4a7965SKim Phillips 
17309c4a7965SKim Phillips 	list_for_each_entry_safe(t_alg, n, &priv->alg_list, entry) {
17319c4a7965SKim Phillips 		crypto_unregister_alg(&t_alg->crypto_alg);
17329c4a7965SKim Phillips 		list_del(&t_alg->entry);
17339c4a7965SKim Phillips 		kfree(t_alg);
17349c4a7965SKim Phillips 	}
17359c4a7965SKim Phillips 
17369c4a7965SKim Phillips 	if (hw_supports(dev, DESC_HDR_SEL0_RNG))
17379c4a7965SKim Phillips 		talitos_unregister_rng(dev);
17389c4a7965SKim Phillips 
1739ec6644d6SKim Phillips 	kfree(priv->submit_count);
17409c4a7965SKim Phillips 	kfree(priv->tail);
17419c4a7965SKim Phillips 	kfree(priv->head);
17429c4a7965SKim Phillips 
17439c4a7965SKim Phillips 	if (priv->fifo)
17449c4a7965SKim Phillips 		for (i = 0; i < priv->num_channels; i++)
17459c4a7965SKim Phillips 			kfree(priv->fifo[i]);
17469c4a7965SKim Phillips 
17479c4a7965SKim Phillips 	kfree(priv->fifo);
17489c4a7965SKim Phillips 	kfree(priv->head_lock);
17499c4a7965SKim Phillips 	kfree(priv->tail_lock);
17509c4a7965SKim Phillips 
17519c4a7965SKim Phillips 	if (priv->irq != NO_IRQ) {
17529c4a7965SKim Phillips 		free_irq(priv->irq, dev);
17539c4a7965SKim Phillips 		irq_dispose_mapping(priv->irq);
17549c4a7965SKim Phillips 	}
17559c4a7965SKim Phillips 
17569c4a7965SKim Phillips 	tasklet_kill(&priv->done_task);
17579c4a7965SKim Phillips 
17589c4a7965SKim Phillips 	iounmap(priv->reg);
17599c4a7965SKim Phillips 
17609c4a7965SKim Phillips 	dev_set_drvdata(dev, NULL);
17619c4a7965SKim Phillips 
17629c4a7965SKim Phillips 	kfree(priv);
17639c4a7965SKim Phillips 
17649c4a7965SKim Phillips 	return 0;
17659c4a7965SKim Phillips }
17669c4a7965SKim Phillips 
17679c4a7965SKim Phillips static struct talitos_crypto_alg *talitos_alg_alloc(struct device *dev,
17689c4a7965SKim Phillips 						    struct talitos_alg_template
17699c4a7965SKim Phillips 						           *template)
17709c4a7965SKim Phillips {
17719c4a7965SKim Phillips 	struct talitos_crypto_alg *t_alg;
17729c4a7965SKim Phillips 	struct crypto_alg *alg;
17739c4a7965SKim Phillips 
17749c4a7965SKim Phillips 	t_alg = kzalloc(sizeof(struct talitos_crypto_alg), GFP_KERNEL);
17759c4a7965SKim Phillips 	if (!t_alg)
17769c4a7965SKim Phillips 		return ERR_PTR(-ENOMEM);
17779c4a7965SKim Phillips 
17789c4a7965SKim Phillips 	alg = &t_alg->crypto_alg;
177956af8cd4SLee Nipper 	*alg = template->alg;
17809c4a7965SKim Phillips 
17819c4a7965SKim Phillips 	alg->cra_module = THIS_MODULE;
17829c4a7965SKim Phillips 	alg->cra_init = talitos_cra_init;
17839c4a7965SKim Phillips 	alg->cra_priority = TALITOS_CRA_PRIORITY;
17849c4a7965SKim Phillips 	alg->cra_alignmask = 0;
17859c4a7965SKim Phillips 	alg->cra_ctxsize = sizeof(struct talitos_ctx);
17869c4a7965SKim Phillips 
17879c4a7965SKim Phillips 	t_alg->desc_hdr_template = template->desc_hdr_template;
17889c4a7965SKim Phillips 	t_alg->dev = dev;
17899c4a7965SKim Phillips 
17909c4a7965SKim Phillips 	return t_alg;
17919c4a7965SKim Phillips }
17929c4a7965SKim Phillips 
17939c4a7965SKim Phillips static int talitos_probe(struct of_device *ofdev,
17949c4a7965SKim Phillips 			 const struct of_device_id *match)
17959c4a7965SKim Phillips {
17969c4a7965SKim Phillips 	struct device *dev = &ofdev->dev;
17979c4a7965SKim Phillips 	struct device_node *np = ofdev->node;
17989c4a7965SKim Phillips 	struct talitos_private *priv;
17999c4a7965SKim Phillips 	const unsigned int *prop;
18009c4a7965SKim Phillips 	int i, err;
18019c4a7965SKim Phillips 
18029c4a7965SKim Phillips 	priv = kzalloc(sizeof(struct talitos_private), GFP_KERNEL);
18039c4a7965SKim Phillips 	if (!priv)
18049c4a7965SKim Phillips 		return -ENOMEM;
18059c4a7965SKim Phillips 
18069c4a7965SKim Phillips 	dev_set_drvdata(dev, priv);
18079c4a7965SKim Phillips 
18089c4a7965SKim Phillips 	priv->ofdev = ofdev;
18099c4a7965SKim Phillips 
18109c4a7965SKim Phillips 	tasklet_init(&priv->done_task, talitos_done, (unsigned long)dev);
18119c4a7965SKim Phillips 
1812fe5720e2SKim Phillips 	INIT_LIST_HEAD(&priv->alg_list);
1813fe5720e2SKim Phillips 
18149c4a7965SKim Phillips 	priv->irq = irq_of_parse_and_map(np, 0);
18159c4a7965SKim Phillips 
18169c4a7965SKim Phillips 	if (priv->irq == NO_IRQ) {
18179c4a7965SKim Phillips 		dev_err(dev, "failed to map irq\n");
18189c4a7965SKim Phillips 		err = -EINVAL;
18199c4a7965SKim Phillips 		goto err_out;
18209c4a7965SKim Phillips 	}
18219c4a7965SKim Phillips 
18229c4a7965SKim Phillips 	/* get the irq line */
18239c4a7965SKim Phillips 	err = request_irq(priv->irq, talitos_interrupt, 0,
18249c4a7965SKim Phillips 			  dev_driver_string(dev), dev);
18259c4a7965SKim Phillips 	if (err) {
18269c4a7965SKim Phillips 		dev_err(dev, "failed to request irq %d\n", priv->irq);
18279c4a7965SKim Phillips 		irq_dispose_mapping(priv->irq);
18289c4a7965SKim Phillips 		priv->irq = NO_IRQ;
18299c4a7965SKim Phillips 		goto err_out;
18309c4a7965SKim Phillips 	}
18319c4a7965SKim Phillips 
18329c4a7965SKim Phillips 	priv->reg = of_iomap(np, 0);
18339c4a7965SKim Phillips 	if (!priv->reg) {
18349c4a7965SKim Phillips 		dev_err(dev, "failed to of_iomap\n");
18359c4a7965SKim Phillips 		err = -ENOMEM;
18369c4a7965SKim Phillips 		goto err_out;
18379c4a7965SKim Phillips 	}
18389c4a7965SKim Phillips 
18399c4a7965SKim Phillips 	/* get SEC version capabilities from device tree */
18409c4a7965SKim Phillips 	prop = of_get_property(np, "fsl,num-channels", NULL);
18419c4a7965SKim Phillips 	if (prop)
18429c4a7965SKim Phillips 		priv->num_channels = *prop;
18439c4a7965SKim Phillips 
18449c4a7965SKim Phillips 	prop = of_get_property(np, "fsl,channel-fifo-len", NULL);
18459c4a7965SKim Phillips 	if (prop)
18469c4a7965SKim Phillips 		priv->chfifo_len = *prop;
18479c4a7965SKim Phillips 
18489c4a7965SKim Phillips 	prop = of_get_property(np, "fsl,exec-units-mask", NULL);
18499c4a7965SKim Phillips 	if (prop)
18509c4a7965SKim Phillips 		priv->exec_units = *prop;
18519c4a7965SKim Phillips 
18529c4a7965SKim Phillips 	prop = of_get_property(np, "fsl,descriptor-types-mask", NULL);
18539c4a7965SKim Phillips 	if (prop)
18549c4a7965SKim Phillips 		priv->desc_types = *prop;
18559c4a7965SKim Phillips 
18569c4a7965SKim Phillips 	if (!is_power_of_2(priv->num_channels) || !priv->chfifo_len ||
18579c4a7965SKim Phillips 	    !priv->exec_units || !priv->desc_types) {
18589c4a7965SKim Phillips 		dev_err(dev, "invalid property data in device tree node\n");
18599c4a7965SKim Phillips 		err = -EINVAL;
18609c4a7965SKim Phillips 		goto err_out;
18619c4a7965SKim Phillips 	}
18629c4a7965SKim Phillips 
1863f3c85bc1SLee Nipper 	if (of_device_is_compatible(np, "fsl,sec3.0"))
1864f3c85bc1SLee Nipper 		priv->features |= TALITOS_FTR_SRC_LINK_TBL_LEN_INCLUDES_EXTENT;
1865f3c85bc1SLee Nipper 
1866fe5720e2SKim Phillips 	if (of_device_is_compatible(np, "fsl,sec2.1"))
1867fe5720e2SKim Phillips 		priv->features |= TALITOS_FTR_HW_AUTH_CHECK;
1868fe5720e2SKim Phillips 
18699c4a7965SKim Phillips 	priv->head_lock = kmalloc(sizeof(spinlock_t) * priv->num_channels,
18709c4a7965SKim Phillips 				  GFP_KERNEL);
18719c4a7965SKim Phillips 	priv->tail_lock = kmalloc(sizeof(spinlock_t) * priv->num_channels,
18729c4a7965SKim Phillips 				  GFP_KERNEL);
18739c4a7965SKim Phillips 	if (!priv->head_lock || !priv->tail_lock) {
18749c4a7965SKim Phillips 		dev_err(dev, "failed to allocate fifo locks\n");
18759c4a7965SKim Phillips 		err = -ENOMEM;
18769c4a7965SKim Phillips 		goto err_out;
18779c4a7965SKim Phillips 	}
18789c4a7965SKim Phillips 
18799c4a7965SKim Phillips 	for (i = 0; i < priv->num_channels; i++) {
18809c4a7965SKim Phillips 		spin_lock_init(&priv->head_lock[i]);
18819c4a7965SKim Phillips 		spin_lock_init(&priv->tail_lock[i]);
18829c4a7965SKim Phillips 	}
18839c4a7965SKim Phillips 
18849c4a7965SKim Phillips 	priv->fifo = kmalloc(sizeof(struct talitos_request *) *
18859c4a7965SKim Phillips 			     priv->num_channels, GFP_KERNEL);
18869c4a7965SKim Phillips 	if (!priv->fifo) {
18879c4a7965SKim Phillips 		dev_err(dev, "failed to allocate request fifo\n");
18889c4a7965SKim Phillips 		err = -ENOMEM;
18899c4a7965SKim Phillips 		goto err_out;
18909c4a7965SKim Phillips 	}
18919c4a7965SKim Phillips 
18929c4a7965SKim Phillips 	priv->fifo_len = roundup_pow_of_two(priv->chfifo_len);
18939c4a7965SKim Phillips 
18949c4a7965SKim Phillips 	for (i = 0; i < priv->num_channels; i++) {
18959c4a7965SKim Phillips 		priv->fifo[i] = kzalloc(sizeof(struct talitos_request) *
18969c4a7965SKim Phillips 					priv->fifo_len, GFP_KERNEL);
18979c4a7965SKim Phillips 		if (!priv->fifo[i]) {
18989c4a7965SKim Phillips 			dev_err(dev, "failed to allocate request fifo %d\n", i);
18999c4a7965SKim Phillips 			err = -ENOMEM;
19009c4a7965SKim Phillips 			goto err_out;
19019c4a7965SKim Phillips 		}
19029c4a7965SKim Phillips 	}
19039c4a7965SKim Phillips 
1904586725f8SKim Phillips 	priv->submit_count = kmalloc(sizeof(atomic_t) * priv->num_channels,
1905ec6644d6SKim Phillips 				     GFP_KERNEL);
1906ec6644d6SKim Phillips 	if (!priv->submit_count) {
1907ec6644d6SKim Phillips 		dev_err(dev, "failed to allocate fifo submit count space\n");
1908ec6644d6SKim Phillips 		err = -ENOMEM;
1909ec6644d6SKim Phillips 		goto err_out;
1910ec6644d6SKim Phillips 	}
1911ec6644d6SKim Phillips 	for (i = 0; i < priv->num_channels; i++)
19124b24ea97SVishnu Suresh 		atomic_set(&priv->submit_count[i], -(priv->chfifo_len - 1));
1913ec6644d6SKim Phillips 
19149c4a7965SKim Phillips 	priv->head = kzalloc(sizeof(int) * priv->num_channels, GFP_KERNEL);
19159c4a7965SKim Phillips 	priv->tail = kzalloc(sizeof(int) * priv->num_channels, GFP_KERNEL);
19169c4a7965SKim Phillips 	if (!priv->head || !priv->tail) {
19179c4a7965SKim Phillips 		dev_err(dev, "failed to allocate request index space\n");
19189c4a7965SKim Phillips 		err = -ENOMEM;
19199c4a7965SKim Phillips 		goto err_out;
19209c4a7965SKim Phillips 	}
19219c4a7965SKim Phillips 
19229c4a7965SKim Phillips 	/* reset and initialize the h/w */
19239c4a7965SKim Phillips 	err = init_device(dev);
19249c4a7965SKim Phillips 	if (err) {
19259c4a7965SKim Phillips 		dev_err(dev, "failed to initialize device\n");
19269c4a7965SKim Phillips 		goto err_out;
19279c4a7965SKim Phillips 	}
19289c4a7965SKim Phillips 
19299c4a7965SKim Phillips 	/* register the RNG, if available */
19309c4a7965SKim Phillips 	if (hw_supports(dev, DESC_HDR_SEL0_RNG)) {
19319c4a7965SKim Phillips 		err = talitos_register_rng(dev);
19329c4a7965SKim Phillips 		if (err) {
19339c4a7965SKim Phillips 			dev_err(dev, "failed to register hwrng: %d\n", err);
19349c4a7965SKim Phillips 			goto err_out;
19359c4a7965SKim Phillips 		} else
19369c4a7965SKim Phillips 			dev_info(dev, "hwrng\n");
19379c4a7965SKim Phillips 	}
19389c4a7965SKim Phillips 
19399c4a7965SKim Phillips 	/* register crypto algorithms the device supports */
19409c4a7965SKim Phillips 	for (i = 0; i < ARRAY_SIZE(driver_algs); i++) {
19419c4a7965SKim Phillips 		if (hw_supports(dev, driver_algs[i].desc_hdr_template)) {
19429c4a7965SKim Phillips 			struct talitos_crypto_alg *t_alg;
19439c4a7965SKim Phillips 
19449c4a7965SKim Phillips 			t_alg = talitos_alg_alloc(dev, &driver_algs[i]);
19459c4a7965SKim Phillips 			if (IS_ERR(t_alg)) {
19469c4a7965SKim Phillips 				err = PTR_ERR(t_alg);
19479c4a7965SKim Phillips 				goto err_out;
19489c4a7965SKim Phillips 			}
19499c4a7965SKim Phillips 
19509c4a7965SKim Phillips 			err = crypto_register_alg(&t_alg->crypto_alg);
19519c4a7965SKim Phillips 			if (err) {
19529c4a7965SKim Phillips 				dev_err(dev, "%s alg registration failed\n",
19539c4a7965SKim Phillips 					t_alg->crypto_alg.cra_driver_name);
19549c4a7965SKim Phillips 				kfree(t_alg);
19559c4a7965SKim Phillips 			} else {
19569c4a7965SKim Phillips 				list_add_tail(&t_alg->entry, &priv->alg_list);
19579c4a7965SKim Phillips 				dev_info(dev, "%s\n",
19589c4a7965SKim Phillips 					 t_alg->crypto_alg.cra_driver_name);
19599c4a7965SKim Phillips 			}
19609c4a7965SKim Phillips 		}
19619c4a7965SKim Phillips 	}
19629c4a7965SKim Phillips 
19639c4a7965SKim Phillips 	return 0;
19649c4a7965SKim Phillips 
19659c4a7965SKim Phillips err_out:
19669c4a7965SKim Phillips 	talitos_remove(ofdev);
19679c4a7965SKim Phillips 
19689c4a7965SKim Phillips 	return err;
19699c4a7965SKim Phillips }
19709c4a7965SKim Phillips 
19719c4a7965SKim Phillips static struct of_device_id talitos_match[] = {
19729c4a7965SKim Phillips 	{
19739c4a7965SKim Phillips 		.compatible = "fsl,sec2.0",
19749c4a7965SKim Phillips 	},
19759c4a7965SKim Phillips 	{},
19769c4a7965SKim Phillips };
19779c4a7965SKim Phillips MODULE_DEVICE_TABLE(of, talitos_match);
19789c4a7965SKim Phillips 
19799c4a7965SKim Phillips static struct of_platform_driver talitos_driver = {
19809c4a7965SKim Phillips 	.name = "talitos",
19819c4a7965SKim Phillips 	.match_table = talitos_match,
19829c4a7965SKim Phillips 	.probe = talitos_probe,
1983596f1034SAl Viro 	.remove = talitos_remove,
19849c4a7965SKim Phillips };
19859c4a7965SKim Phillips 
19869c4a7965SKim Phillips static int __init talitos_init(void)
19879c4a7965SKim Phillips {
19889c4a7965SKim Phillips 	return of_register_platform_driver(&talitos_driver);
19899c4a7965SKim Phillips }
19909c4a7965SKim Phillips module_init(talitos_init);
19919c4a7965SKim Phillips 
19929c4a7965SKim Phillips static void __exit talitos_exit(void)
19939c4a7965SKim Phillips {
19949c4a7965SKim Phillips 	of_unregister_platform_driver(&talitos_driver);
19959c4a7965SKim Phillips }
19969c4a7965SKim Phillips module_exit(talitos_exit);
19979c4a7965SKim Phillips 
19989c4a7965SKim Phillips MODULE_LICENSE("GPL");
19999c4a7965SKim Phillips MODULE_AUTHOR("Kim Phillips <kim.phillips@freescale.com>");
20009c4a7965SKim Phillips MODULE_DESCRIPTION("Freescale integrated security engine (SEC) driver");
2001