xref: /linux/drivers/crypto/talitos.c (revision 56af8cd44b05bd9649103b76a6e1e575682990e4)
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>
479c4a7965SKim Phillips 
489c4a7965SKim Phillips #include "talitos.h"
499c4a7965SKim Phillips 
509c4a7965SKim Phillips #define TALITOS_TIMEOUT 100000
519c4a7965SKim Phillips #define TALITOS_MAX_DATA_LEN 65535
529c4a7965SKim Phillips 
539c4a7965SKim Phillips #define DESC_TYPE(desc_hdr) ((be32_to_cpu(desc_hdr) >> 3) & 0x1f)
549c4a7965SKim Phillips #define PRIMARY_EU(desc_hdr) ((be32_to_cpu(desc_hdr) >> 28) & 0xf)
559c4a7965SKim Phillips #define SECONDARY_EU(desc_hdr) ((be32_to_cpu(desc_hdr) >> 16) & 0xf)
569c4a7965SKim Phillips 
579c4a7965SKim Phillips /* descriptor pointer entry */
589c4a7965SKim Phillips struct talitos_ptr {
599c4a7965SKim Phillips 	__be16 len;	/* length */
609c4a7965SKim Phillips 	u8 j_extent;	/* jump to sg link table and/or extent */
619c4a7965SKim Phillips 	u8 eptr;	/* extended address */
629c4a7965SKim Phillips 	__be32 ptr;	/* address */
639c4a7965SKim Phillips };
649c4a7965SKim Phillips 
659c4a7965SKim Phillips /* descriptor */
669c4a7965SKim Phillips struct talitos_desc {
679c4a7965SKim Phillips 	__be32 hdr;			/* header high bits */
689c4a7965SKim Phillips 	__be32 hdr_lo;			/* header low bits */
699c4a7965SKim Phillips 	struct talitos_ptr ptr[7];	/* ptr/len pair array */
709c4a7965SKim Phillips };
719c4a7965SKim Phillips 
729c4a7965SKim Phillips /**
739c4a7965SKim Phillips  * talitos_request - descriptor submission request
749c4a7965SKim Phillips  * @desc: descriptor pointer (kernel virtual)
759c4a7965SKim Phillips  * @dma_desc: descriptor's physical bus address
769c4a7965SKim Phillips  * @callback: whom to call when descriptor processing is done
779c4a7965SKim Phillips  * @context: caller context (optional)
789c4a7965SKim Phillips  */
799c4a7965SKim Phillips struct talitos_request {
809c4a7965SKim Phillips 	struct talitos_desc *desc;
819c4a7965SKim Phillips 	dma_addr_t dma_desc;
829c4a7965SKim Phillips 	void (*callback) (struct device *dev, struct talitos_desc *desc,
839c4a7965SKim Phillips 	                  void *context, int error);
849c4a7965SKim Phillips 	void *context;
859c4a7965SKim Phillips };
869c4a7965SKim Phillips 
879c4a7965SKim Phillips struct talitos_private {
889c4a7965SKim Phillips 	struct device *dev;
899c4a7965SKim Phillips 	struct of_device *ofdev;
909c4a7965SKim Phillips 	void __iomem *reg;
919c4a7965SKim Phillips 	int irq;
929c4a7965SKim Phillips 
939c4a7965SKim Phillips 	/* SEC version geometry (from device tree node) */
949c4a7965SKim Phillips 	unsigned int num_channels;
959c4a7965SKim Phillips 	unsigned int chfifo_len;
969c4a7965SKim Phillips 	unsigned int exec_units;
979c4a7965SKim Phillips 	unsigned int desc_types;
989c4a7965SKim Phillips 
99f3c85bc1SLee Nipper 	/* SEC Compatibility info */
100f3c85bc1SLee Nipper 	unsigned long features;
101f3c85bc1SLee Nipper 
1029c4a7965SKim Phillips 	/* next channel to be assigned next incoming descriptor */
1039c4a7965SKim Phillips 	atomic_t last_chan;
1049c4a7965SKim Phillips 
105ec6644d6SKim Phillips 	/* per-channel number of requests pending in channel h/w fifo */
106ec6644d6SKim Phillips 	atomic_t *submit_count;
107ec6644d6SKim Phillips 
1089c4a7965SKim Phillips 	/* per-channel request fifo */
1099c4a7965SKim Phillips 	struct talitos_request **fifo;
1109c4a7965SKim Phillips 
1119c4a7965SKim Phillips 	/*
1129c4a7965SKim Phillips 	 * length of the request fifo
1139c4a7965SKim Phillips 	 * fifo_len is chfifo_len rounded up to next power of 2
1149c4a7965SKim Phillips 	 * so we can use bitwise ops to wrap
1159c4a7965SKim Phillips 	 */
1169c4a7965SKim Phillips 	unsigned int fifo_len;
1179c4a7965SKim Phillips 
1189c4a7965SKim Phillips 	/* per-channel index to next free descriptor request */
1199c4a7965SKim Phillips 	int *head;
1209c4a7965SKim Phillips 
1219c4a7965SKim Phillips 	/* per-channel index to next in-progress/done descriptor request */
1229c4a7965SKim Phillips 	int *tail;
1239c4a7965SKim Phillips 
1249c4a7965SKim Phillips 	/* per-channel request submission (head) and release (tail) locks */
1259c4a7965SKim Phillips 	spinlock_t *head_lock;
1269c4a7965SKim Phillips 	spinlock_t *tail_lock;
1279c4a7965SKim Phillips 
1289c4a7965SKim Phillips 	/* request callback tasklet */
1299c4a7965SKim Phillips 	struct tasklet_struct done_task;
1309c4a7965SKim Phillips 
1319c4a7965SKim Phillips 	/* list of registered algorithms */
1329c4a7965SKim Phillips 	struct list_head alg_list;
1339c4a7965SKim Phillips 
1349c4a7965SKim Phillips 	/* hwrng device */
1359c4a7965SKim Phillips 	struct hwrng rng;
1369c4a7965SKim Phillips };
1379c4a7965SKim Phillips 
138f3c85bc1SLee Nipper /* .features flag */
139f3c85bc1SLee Nipper #define TALITOS_FTR_SRC_LINK_TBL_LEN_INCLUDES_EXTENT 0x00000001
140fe5720e2SKim Phillips #define TALITOS_FTR_HW_AUTH_CHECK 0x00000002
141f3c85bc1SLee Nipper 
1429c4a7965SKim Phillips /*
1439c4a7965SKim Phillips  * map virtual single (contiguous) pointer to h/w descriptor pointer
1449c4a7965SKim Phillips  */
1459c4a7965SKim Phillips static void map_single_talitos_ptr(struct device *dev,
1469c4a7965SKim Phillips 				   struct talitos_ptr *talitos_ptr,
1479c4a7965SKim Phillips 				   unsigned short len, void *data,
1489c4a7965SKim Phillips 				   unsigned char extent,
1499c4a7965SKim Phillips 				   enum dma_data_direction dir)
1509c4a7965SKim Phillips {
1519c4a7965SKim Phillips 	talitos_ptr->len = cpu_to_be16(len);
1529c4a7965SKim Phillips 	talitos_ptr->ptr = cpu_to_be32(dma_map_single(dev, data, len, dir));
1539c4a7965SKim Phillips 	talitos_ptr->j_extent = extent;
1549c4a7965SKim Phillips }
1559c4a7965SKim Phillips 
1569c4a7965SKim Phillips /*
1579c4a7965SKim Phillips  * unmap bus single (contiguous) h/w descriptor pointer
1589c4a7965SKim Phillips  */
1599c4a7965SKim Phillips static void unmap_single_talitos_ptr(struct device *dev,
1609c4a7965SKim Phillips 				     struct talitos_ptr *talitos_ptr,
1619c4a7965SKim Phillips 				     enum dma_data_direction dir)
1629c4a7965SKim Phillips {
1639c4a7965SKim Phillips 	dma_unmap_single(dev, be32_to_cpu(talitos_ptr->ptr),
1649c4a7965SKim Phillips 			 be16_to_cpu(talitos_ptr->len), dir);
1659c4a7965SKim Phillips }
1669c4a7965SKim Phillips 
1679c4a7965SKim Phillips static int reset_channel(struct device *dev, int ch)
1689c4a7965SKim Phillips {
1699c4a7965SKim Phillips 	struct talitos_private *priv = dev_get_drvdata(dev);
1709c4a7965SKim Phillips 	unsigned int timeout = TALITOS_TIMEOUT;
1719c4a7965SKim Phillips 
1729c4a7965SKim Phillips 	setbits32(priv->reg + TALITOS_CCCR(ch), TALITOS_CCCR_RESET);
1739c4a7965SKim Phillips 
1749c4a7965SKim Phillips 	while ((in_be32(priv->reg + TALITOS_CCCR(ch)) & TALITOS_CCCR_RESET)
1759c4a7965SKim Phillips 	       && --timeout)
1769c4a7965SKim Phillips 		cpu_relax();
1779c4a7965SKim Phillips 
1789c4a7965SKim Phillips 	if (timeout == 0) {
1799c4a7965SKim Phillips 		dev_err(dev, "failed to reset channel %d\n", ch);
1809c4a7965SKim Phillips 		return -EIO;
1819c4a7965SKim Phillips 	}
1829c4a7965SKim Phillips 
1839c4a7965SKim Phillips 	/* set done writeback and IRQ */
1849c4a7965SKim Phillips 	setbits32(priv->reg + TALITOS_CCCR_LO(ch), TALITOS_CCCR_LO_CDWE |
1859c4a7965SKim Phillips 		  TALITOS_CCCR_LO_CDIE);
1869c4a7965SKim Phillips 
187fe5720e2SKim Phillips 	/* and ICCR writeback, if available */
188fe5720e2SKim Phillips 	if (priv->features & TALITOS_FTR_HW_AUTH_CHECK)
189fe5720e2SKim Phillips 		setbits32(priv->reg + TALITOS_CCCR_LO(ch),
190fe5720e2SKim Phillips 		          TALITOS_CCCR_LO_IWSE);
191fe5720e2SKim Phillips 
1929c4a7965SKim Phillips 	return 0;
1939c4a7965SKim Phillips }
1949c4a7965SKim Phillips 
1959c4a7965SKim Phillips static int reset_device(struct device *dev)
1969c4a7965SKim Phillips {
1979c4a7965SKim Phillips 	struct talitos_private *priv = dev_get_drvdata(dev);
1989c4a7965SKim Phillips 	unsigned int timeout = TALITOS_TIMEOUT;
1999c4a7965SKim Phillips 
2009c4a7965SKim Phillips 	setbits32(priv->reg + TALITOS_MCR, TALITOS_MCR_SWR);
2019c4a7965SKim Phillips 
2029c4a7965SKim Phillips 	while ((in_be32(priv->reg + TALITOS_MCR) & TALITOS_MCR_SWR)
2039c4a7965SKim Phillips 	       && --timeout)
2049c4a7965SKim Phillips 		cpu_relax();
2059c4a7965SKim Phillips 
2069c4a7965SKim Phillips 	if (timeout == 0) {
2079c4a7965SKim Phillips 		dev_err(dev, "failed to reset device\n");
2089c4a7965SKim Phillips 		return -EIO;
2099c4a7965SKim Phillips 	}
2109c4a7965SKim Phillips 
2119c4a7965SKim Phillips 	return 0;
2129c4a7965SKim Phillips }
2139c4a7965SKim Phillips 
2149c4a7965SKim Phillips /*
2159c4a7965SKim Phillips  * Reset and initialize the device
2169c4a7965SKim Phillips  */
2179c4a7965SKim Phillips static int init_device(struct device *dev)
2189c4a7965SKim Phillips {
2199c4a7965SKim Phillips 	struct talitos_private *priv = dev_get_drvdata(dev);
2209c4a7965SKim Phillips 	int ch, err;
2219c4a7965SKim Phillips 
2229c4a7965SKim Phillips 	/*
2239c4a7965SKim Phillips 	 * Master reset
2249c4a7965SKim Phillips 	 * errata documentation: warning: certain SEC interrupts
2259c4a7965SKim Phillips 	 * are not fully cleared by writing the MCR:SWR bit,
2269c4a7965SKim Phillips 	 * set bit twice to completely reset
2279c4a7965SKim Phillips 	 */
2289c4a7965SKim Phillips 	err = reset_device(dev);
2299c4a7965SKim Phillips 	if (err)
2309c4a7965SKim Phillips 		return err;
2319c4a7965SKim Phillips 
2329c4a7965SKim Phillips 	err = reset_device(dev);
2339c4a7965SKim Phillips 	if (err)
2349c4a7965SKim Phillips 		return err;
2359c4a7965SKim Phillips 
2369c4a7965SKim Phillips 	/* reset channels */
2379c4a7965SKim Phillips 	for (ch = 0; ch < priv->num_channels; ch++) {
2389c4a7965SKim Phillips 		err = reset_channel(dev, ch);
2399c4a7965SKim Phillips 		if (err)
2409c4a7965SKim Phillips 			return err;
2419c4a7965SKim Phillips 	}
2429c4a7965SKim Phillips 
2439c4a7965SKim Phillips 	/* enable channel done and error interrupts */
2449c4a7965SKim Phillips 	setbits32(priv->reg + TALITOS_IMR, TALITOS_IMR_INIT);
2459c4a7965SKim Phillips 	setbits32(priv->reg + TALITOS_IMR_LO, TALITOS_IMR_LO_INIT);
2469c4a7965SKim Phillips 
247fe5720e2SKim Phillips 	/* disable integrity check error interrupts (use writeback instead) */
248fe5720e2SKim Phillips 	if (priv->features & TALITOS_FTR_HW_AUTH_CHECK)
249fe5720e2SKim Phillips 		setbits32(priv->reg + TALITOS_MDEUICR_LO,
250fe5720e2SKim Phillips 		          TALITOS_MDEUICR_LO_ICE);
251fe5720e2SKim Phillips 
2529c4a7965SKim Phillips 	return 0;
2539c4a7965SKim Phillips }
2549c4a7965SKim Phillips 
2559c4a7965SKim Phillips /**
2569c4a7965SKim Phillips  * talitos_submit - submits a descriptor to the device for processing
2579c4a7965SKim Phillips  * @dev:	the SEC device to be used
2589c4a7965SKim Phillips  * @desc:	the descriptor to be processed by the device
2599c4a7965SKim Phillips  * @callback:	whom to call when processing is complete
2609c4a7965SKim Phillips  * @context:	a handle for use by caller (optional)
2619c4a7965SKim Phillips  *
2629c4a7965SKim Phillips  * desc must contain valid dma-mapped (bus physical) address pointers.
2639c4a7965SKim Phillips  * callback must check err and feedback in descriptor header
2649c4a7965SKim Phillips  * for device processing status.
2659c4a7965SKim Phillips  */
2669c4a7965SKim Phillips static int talitos_submit(struct device *dev, struct talitos_desc *desc,
2679c4a7965SKim Phillips 			  void (*callback)(struct device *dev,
2689c4a7965SKim Phillips 					   struct talitos_desc *desc,
2699c4a7965SKim Phillips 					   void *context, int error),
2709c4a7965SKim Phillips 			  void *context)
2719c4a7965SKim Phillips {
2729c4a7965SKim Phillips 	struct talitos_private *priv = dev_get_drvdata(dev);
2739c4a7965SKim Phillips 	struct talitos_request *request;
2749c4a7965SKim Phillips 	unsigned long flags, ch;
2759c4a7965SKim Phillips 	int head;
2769c4a7965SKim Phillips 
2779c4a7965SKim Phillips 	/* select done notification */
2789c4a7965SKim Phillips 	desc->hdr |= DESC_HDR_DONE_NOTIFY;
2799c4a7965SKim Phillips 
2809c4a7965SKim Phillips 	/* emulate SEC's round-robin channel fifo polling scheme */
2819c4a7965SKim Phillips 	ch = atomic_inc_return(&priv->last_chan) & (priv->num_channels - 1);
2829c4a7965SKim Phillips 
2839c4a7965SKim Phillips 	spin_lock_irqsave(&priv->head_lock[ch], flags);
2849c4a7965SKim Phillips 
285ec6644d6SKim Phillips 	if (!atomic_inc_not_zero(&priv->submit_count[ch])) {
286ec6644d6SKim Phillips 		/* h/w fifo is full */
2879c4a7965SKim Phillips 		spin_unlock_irqrestore(&priv->head_lock[ch], flags);
2889c4a7965SKim Phillips 		return -EAGAIN;
2899c4a7965SKim Phillips 	}
2909c4a7965SKim Phillips 
291ec6644d6SKim Phillips 	head = priv->head[ch];
292ec6644d6SKim Phillips 	request = &priv->fifo[ch][head];
293ec6644d6SKim Phillips 
2949c4a7965SKim Phillips 	/* map descriptor and save caller data */
2959c4a7965SKim Phillips 	request->dma_desc = dma_map_single(dev, desc, sizeof(*desc),
2969c4a7965SKim Phillips 					   DMA_BIDIRECTIONAL);
2979c4a7965SKim Phillips 	request->callback = callback;
2989c4a7965SKim Phillips 	request->context = context;
2999c4a7965SKim Phillips 
3009c4a7965SKim Phillips 	/* increment fifo head */
3019c4a7965SKim Phillips 	priv->head[ch] = (priv->head[ch] + 1) & (priv->fifo_len - 1);
3029c4a7965SKim Phillips 
3039c4a7965SKim Phillips 	smp_wmb();
3049c4a7965SKim Phillips 	request->desc = desc;
3059c4a7965SKim Phillips 
3069c4a7965SKim Phillips 	/* GO! */
3079c4a7965SKim Phillips 	wmb();
3089c4a7965SKim Phillips 	out_be32(priv->reg + TALITOS_FF_LO(ch), request->dma_desc);
3099c4a7965SKim Phillips 
3109c4a7965SKim Phillips 	spin_unlock_irqrestore(&priv->head_lock[ch], flags);
3119c4a7965SKim Phillips 
3129c4a7965SKim Phillips 	return -EINPROGRESS;
3139c4a7965SKim Phillips }
3149c4a7965SKim Phillips 
3159c4a7965SKim Phillips /*
3169c4a7965SKim Phillips  * process what was done, notify callback of error if not
3179c4a7965SKim Phillips  */
3189c4a7965SKim Phillips static void flush_channel(struct device *dev, int ch, int error, int reset_ch)
3199c4a7965SKim Phillips {
3209c4a7965SKim Phillips 	struct talitos_private *priv = dev_get_drvdata(dev);
3219c4a7965SKim Phillips 	struct talitos_request *request, saved_req;
3229c4a7965SKim Phillips 	unsigned long flags;
3239c4a7965SKim Phillips 	int tail, status;
3249c4a7965SKim Phillips 
3259c4a7965SKim Phillips 	spin_lock_irqsave(&priv->tail_lock[ch], flags);
3269c4a7965SKim Phillips 
3279c4a7965SKim Phillips 	tail = priv->tail[ch];
3289c4a7965SKim Phillips 	while (priv->fifo[ch][tail].desc) {
3299c4a7965SKim Phillips 		request = &priv->fifo[ch][tail];
3309c4a7965SKim Phillips 
3319c4a7965SKim Phillips 		/* descriptors with their done bits set don't get the error */
3329c4a7965SKim Phillips 		rmb();
333ca38a814SLee Nipper 		if ((request->desc->hdr & DESC_HDR_DONE) == DESC_HDR_DONE)
3349c4a7965SKim Phillips 			status = 0;
335ca38a814SLee Nipper 		else
3369c4a7965SKim Phillips 			if (!error)
3379c4a7965SKim Phillips 				break;
3389c4a7965SKim Phillips 			else
3399c4a7965SKim Phillips 				status = error;
3409c4a7965SKim Phillips 
3419c4a7965SKim Phillips 		dma_unmap_single(dev, request->dma_desc,
3429c4a7965SKim Phillips 			sizeof(struct talitos_desc), DMA_BIDIRECTIONAL);
3439c4a7965SKim Phillips 
3449c4a7965SKim Phillips 		/* copy entries so we can call callback outside lock */
3459c4a7965SKim Phillips 		saved_req.desc = request->desc;
3469c4a7965SKim Phillips 		saved_req.callback = request->callback;
3479c4a7965SKim Phillips 		saved_req.context = request->context;
3489c4a7965SKim Phillips 
3499c4a7965SKim Phillips 		/* release request entry in fifo */
3509c4a7965SKim Phillips 		smp_wmb();
3519c4a7965SKim Phillips 		request->desc = NULL;
3529c4a7965SKim Phillips 
3539c4a7965SKim Phillips 		/* increment fifo tail */
3549c4a7965SKim Phillips 		priv->tail[ch] = (tail + 1) & (priv->fifo_len - 1);
3559c4a7965SKim Phillips 
3569c4a7965SKim Phillips 		spin_unlock_irqrestore(&priv->tail_lock[ch], flags);
357ec6644d6SKim Phillips 
358ec6644d6SKim Phillips 		atomic_dec(&priv->submit_count[ch]);
359ec6644d6SKim Phillips 
3609c4a7965SKim Phillips 		saved_req.callback(dev, saved_req.desc, saved_req.context,
3619c4a7965SKim Phillips 				   status);
3629c4a7965SKim Phillips 		/* channel may resume processing in single desc error case */
3639c4a7965SKim Phillips 		if (error && !reset_ch && status == error)
3649c4a7965SKim Phillips 			return;
3659c4a7965SKim Phillips 		spin_lock_irqsave(&priv->tail_lock[ch], flags);
3669c4a7965SKim Phillips 		tail = priv->tail[ch];
3679c4a7965SKim Phillips 	}
3689c4a7965SKim Phillips 
3699c4a7965SKim Phillips 	spin_unlock_irqrestore(&priv->tail_lock[ch], flags);
3709c4a7965SKim Phillips }
3719c4a7965SKim Phillips 
3729c4a7965SKim Phillips /*
3739c4a7965SKim Phillips  * process completed requests for channels that have done status
3749c4a7965SKim Phillips  */
3759c4a7965SKim Phillips static void talitos_done(unsigned long data)
3769c4a7965SKim Phillips {
3779c4a7965SKim Phillips 	struct device *dev = (struct device *)data;
3789c4a7965SKim Phillips 	struct talitos_private *priv = dev_get_drvdata(dev);
3799c4a7965SKim Phillips 	int ch;
3809c4a7965SKim Phillips 
3819c4a7965SKim Phillips 	for (ch = 0; ch < priv->num_channels; ch++)
3829c4a7965SKim Phillips 		flush_channel(dev, ch, 0, 0);
3831c2e8811SLee Nipper 
3841c2e8811SLee Nipper 	/* At this point, all completed channels have been processed.
3851c2e8811SLee Nipper 	 * Unmask done interrupts for channels completed later on.
3861c2e8811SLee Nipper 	 */
387fe5720e2SKim Phillips 	setbits32(priv->reg + TALITOS_IMR, TALITOS_IMR_INIT);
388fe5720e2SKim Phillips 	setbits32(priv->reg + TALITOS_IMR_LO, TALITOS_IMR_LO_INIT);
3899c4a7965SKim Phillips }
3909c4a7965SKim Phillips 
3919c4a7965SKim Phillips /*
3929c4a7965SKim Phillips  * locate current (offending) descriptor
3939c4a7965SKim Phillips  */
3949c4a7965SKim Phillips static struct talitos_desc *current_desc(struct device *dev, int ch)
3959c4a7965SKim Phillips {
3969c4a7965SKim Phillips 	struct talitos_private *priv = dev_get_drvdata(dev);
3979c4a7965SKim Phillips 	int tail = priv->tail[ch];
3989c4a7965SKim Phillips 	dma_addr_t cur_desc;
3999c4a7965SKim Phillips 
4009c4a7965SKim Phillips 	cur_desc = in_be32(priv->reg + TALITOS_CDPR_LO(ch));
4019c4a7965SKim Phillips 
4029c4a7965SKim Phillips 	while (priv->fifo[ch][tail].dma_desc != cur_desc) {
4039c4a7965SKim Phillips 		tail = (tail + 1) & (priv->fifo_len - 1);
4049c4a7965SKim Phillips 		if (tail == priv->tail[ch]) {
4059c4a7965SKim Phillips 			dev_err(dev, "couldn't locate current descriptor\n");
4069c4a7965SKim Phillips 			return NULL;
4079c4a7965SKim Phillips 		}
4089c4a7965SKim Phillips 	}
4099c4a7965SKim Phillips 
4109c4a7965SKim Phillips 	return priv->fifo[ch][tail].desc;
4119c4a7965SKim Phillips }
4129c4a7965SKim Phillips 
4139c4a7965SKim Phillips /*
4149c4a7965SKim Phillips  * user diagnostics; report root cause of error based on execution unit status
4159c4a7965SKim Phillips  */
4169c4a7965SKim Phillips static void report_eu_error(struct device *dev, int ch, struct talitos_desc *desc)
4179c4a7965SKim Phillips {
4189c4a7965SKim Phillips 	struct talitos_private *priv = dev_get_drvdata(dev);
4199c4a7965SKim Phillips 	int i;
4209c4a7965SKim Phillips 
4219c4a7965SKim Phillips 	switch (desc->hdr & DESC_HDR_SEL0_MASK) {
4229c4a7965SKim Phillips 	case DESC_HDR_SEL0_AFEU:
4239c4a7965SKim Phillips 		dev_err(dev, "AFEUISR 0x%08x_%08x\n",
4249c4a7965SKim Phillips 			in_be32(priv->reg + TALITOS_AFEUISR),
4259c4a7965SKim Phillips 			in_be32(priv->reg + TALITOS_AFEUISR_LO));
4269c4a7965SKim Phillips 		break;
4279c4a7965SKim Phillips 	case DESC_HDR_SEL0_DEU:
4289c4a7965SKim Phillips 		dev_err(dev, "DEUISR 0x%08x_%08x\n",
4299c4a7965SKim Phillips 			in_be32(priv->reg + TALITOS_DEUISR),
4309c4a7965SKim Phillips 			in_be32(priv->reg + TALITOS_DEUISR_LO));
4319c4a7965SKim Phillips 		break;
4329c4a7965SKim Phillips 	case DESC_HDR_SEL0_MDEUA:
4339c4a7965SKim Phillips 	case DESC_HDR_SEL0_MDEUB:
4349c4a7965SKim Phillips 		dev_err(dev, "MDEUISR 0x%08x_%08x\n",
4359c4a7965SKim Phillips 			in_be32(priv->reg + TALITOS_MDEUISR),
4369c4a7965SKim Phillips 			in_be32(priv->reg + TALITOS_MDEUISR_LO));
4379c4a7965SKim Phillips 		break;
4389c4a7965SKim Phillips 	case DESC_HDR_SEL0_RNG:
4399c4a7965SKim Phillips 		dev_err(dev, "RNGUISR 0x%08x_%08x\n",
4409c4a7965SKim Phillips 			in_be32(priv->reg + TALITOS_RNGUISR),
4419c4a7965SKim Phillips 			in_be32(priv->reg + TALITOS_RNGUISR_LO));
4429c4a7965SKim Phillips 		break;
4439c4a7965SKim Phillips 	case DESC_HDR_SEL0_PKEU:
4449c4a7965SKim Phillips 		dev_err(dev, "PKEUISR 0x%08x_%08x\n",
4459c4a7965SKim Phillips 			in_be32(priv->reg + TALITOS_PKEUISR),
4469c4a7965SKim Phillips 			in_be32(priv->reg + TALITOS_PKEUISR_LO));
4479c4a7965SKim Phillips 		break;
4489c4a7965SKim Phillips 	case DESC_HDR_SEL0_AESU:
4499c4a7965SKim Phillips 		dev_err(dev, "AESUISR 0x%08x_%08x\n",
4509c4a7965SKim Phillips 			in_be32(priv->reg + TALITOS_AESUISR),
4519c4a7965SKim Phillips 			in_be32(priv->reg + TALITOS_AESUISR_LO));
4529c4a7965SKim Phillips 		break;
4539c4a7965SKim Phillips 	case DESC_HDR_SEL0_CRCU:
4549c4a7965SKim Phillips 		dev_err(dev, "CRCUISR 0x%08x_%08x\n",
4559c4a7965SKim Phillips 			in_be32(priv->reg + TALITOS_CRCUISR),
4569c4a7965SKim Phillips 			in_be32(priv->reg + TALITOS_CRCUISR_LO));
4579c4a7965SKim Phillips 		break;
4589c4a7965SKim Phillips 	case DESC_HDR_SEL0_KEU:
4599c4a7965SKim Phillips 		dev_err(dev, "KEUISR 0x%08x_%08x\n",
4609c4a7965SKim Phillips 			in_be32(priv->reg + TALITOS_KEUISR),
4619c4a7965SKim Phillips 			in_be32(priv->reg + TALITOS_KEUISR_LO));
4629c4a7965SKim Phillips 		break;
4639c4a7965SKim Phillips 	}
4649c4a7965SKim Phillips 
4659c4a7965SKim Phillips 	switch (desc->hdr & DESC_HDR_SEL1_MASK) {
4669c4a7965SKim Phillips 	case DESC_HDR_SEL1_MDEUA:
4679c4a7965SKim Phillips 	case DESC_HDR_SEL1_MDEUB:
4689c4a7965SKim Phillips 		dev_err(dev, "MDEUISR 0x%08x_%08x\n",
4699c4a7965SKim Phillips 			in_be32(priv->reg + TALITOS_MDEUISR),
4709c4a7965SKim Phillips 			in_be32(priv->reg + TALITOS_MDEUISR_LO));
4719c4a7965SKim Phillips 		break;
4729c4a7965SKim Phillips 	case DESC_HDR_SEL1_CRCU:
4739c4a7965SKim Phillips 		dev_err(dev, "CRCUISR 0x%08x_%08x\n",
4749c4a7965SKim Phillips 			in_be32(priv->reg + TALITOS_CRCUISR),
4759c4a7965SKim Phillips 			in_be32(priv->reg + TALITOS_CRCUISR_LO));
4769c4a7965SKim Phillips 		break;
4779c4a7965SKim Phillips 	}
4789c4a7965SKim Phillips 
4799c4a7965SKim Phillips 	for (i = 0; i < 8; i++)
4809c4a7965SKim Phillips 		dev_err(dev, "DESCBUF 0x%08x_%08x\n",
4819c4a7965SKim Phillips 			in_be32(priv->reg + TALITOS_DESCBUF(ch) + 8*i),
4829c4a7965SKim Phillips 			in_be32(priv->reg + TALITOS_DESCBUF_LO(ch) + 8*i));
4839c4a7965SKim Phillips }
4849c4a7965SKim Phillips 
4859c4a7965SKim Phillips /*
4869c4a7965SKim Phillips  * recover from error interrupts
4879c4a7965SKim Phillips  */
48840405f10SKim Phillips static void talitos_error(unsigned long data, u32 isr, u32 isr_lo)
4899c4a7965SKim Phillips {
4909c4a7965SKim Phillips 	struct device *dev = (struct device *)data;
4919c4a7965SKim Phillips 	struct talitos_private *priv = dev_get_drvdata(dev);
4929c4a7965SKim Phillips 	unsigned int timeout = TALITOS_TIMEOUT;
4939c4a7965SKim Phillips 	int ch, error, reset_dev = 0, reset_ch = 0;
49440405f10SKim Phillips 	u32 v, v_lo;
4959c4a7965SKim Phillips 
4969c4a7965SKim Phillips 	for (ch = 0; ch < priv->num_channels; ch++) {
4979c4a7965SKim Phillips 		/* skip channels without errors */
4989c4a7965SKim Phillips 		if (!(isr & (1 << (ch * 2 + 1))))
4999c4a7965SKim Phillips 			continue;
5009c4a7965SKim Phillips 
5019c4a7965SKim Phillips 		error = -EINVAL;
5029c4a7965SKim Phillips 
5039c4a7965SKim Phillips 		v = in_be32(priv->reg + TALITOS_CCPSR(ch));
5049c4a7965SKim Phillips 		v_lo = in_be32(priv->reg + TALITOS_CCPSR_LO(ch));
5059c4a7965SKim Phillips 
5069c4a7965SKim Phillips 		if (v_lo & TALITOS_CCPSR_LO_DOF) {
5079c4a7965SKim Phillips 			dev_err(dev, "double fetch fifo overflow error\n");
5089c4a7965SKim Phillips 			error = -EAGAIN;
5099c4a7965SKim Phillips 			reset_ch = 1;
5109c4a7965SKim Phillips 		}
5119c4a7965SKim Phillips 		if (v_lo & TALITOS_CCPSR_LO_SOF) {
5129c4a7965SKim Phillips 			/* h/w dropped descriptor */
5139c4a7965SKim Phillips 			dev_err(dev, "single fetch fifo overflow error\n");
5149c4a7965SKim Phillips 			error = -EAGAIN;
5159c4a7965SKim Phillips 		}
5169c4a7965SKim Phillips 		if (v_lo & TALITOS_CCPSR_LO_MDTE)
5179c4a7965SKim Phillips 			dev_err(dev, "master data transfer error\n");
5189c4a7965SKim Phillips 		if (v_lo & TALITOS_CCPSR_LO_SGDLZ)
5199c4a7965SKim Phillips 			dev_err(dev, "s/g data length zero error\n");
5209c4a7965SKim Phillips 		if (v_lo & TALITOS_CCPSR_LO_FPZ)
5219c4a7965SKim Phillips 			dev_err(dev, "fetch pointer zero error\n");
5229c4a7965SKim Phillips 		if (v_lo & TALITOS_CCPSR_LO_IDH)
5239c4a7965SKim Phillips 			dev_err(dev, "illegal descriptor header error\n");
5249c4a7965SKim Phillips 		if (v_lo & TALITOS_CCPSR_LO_IEU)
5259c4a7965SKim Phillips 			dev_err(dev, "invalid execution unit error\n");
5269c4a7965SKim Phillips 		if (v_lo & TALITOS_CCPSR_LO_EU)
5279c4a7965SKim Phillips 			report_eu_error(dev, ch, current_desc(dev, ch));
5289c4a7965SKim Phillips 		if (v_lo & TALITOS_CCPSR_LO_GB)
5299c4a7965SKim Phillips 			dev_err(dev, "gather boundary error\n");
5309c4a7965SKim Phillips 		if (v_lo & TALITOS_CCPSR_LO_GRL)
5319c4a7965SKim Phillips 			dev_err(dev, "gather return/length error\n");
5329c4a7965SKim Phillips 		if (v_lo & TALITOS_CCPSR_LO_SB)
5339c4a7965SKim Phillips 			dev_err(dev, "scatter boundary error\n");
5349c4a7965SKim Phillips 		if (v_lo & TALITOS_CCPSR_LO_SRL)
5359c4a7965SKim Phillips 			dev_err(dev, "scatter return/length error\n");
5369c4a7965SKim Phillips 
5379c4a7965SKim Phillips 		flush_channel(dev, ch, error, reset_ch);
5389c4a7965SKim Phillips 
5399c4a7965SKim Phillips 		if (reset_ch) {
5409c4a7965SKim Phillips 			reset_channel(dev, ch);
5419c4a7965SKim Phillips 		} else {
5429c4a7965SKim Phillips 			setbits32(priv->reg + TALITOS_CCCR(ch),
5439c4a7965SKim Phillips 				  TALITOS_CCCR_CONT);
5449c4a7965SKim Phillips 			setbits32(priv->reg + TALITOS_CCCR_LO(ch), 0);
5459c4a7965SKim Phillips 			while ((in_be32(priv->reg + TALITOS_CCCR(ch)) &
5469c4a7965SKim Phillips 			       TALITOS_CCCR_CONT) && --timeout)
5479c4a7965SKim Phillips 				cpu_relax();
5489c4a7965SKim Phillips 			if (timeout == 0) {
5499c4a7965SKim Phillips 				dev_err(dev, "failed to restart channel %d\n",
5509c4a7965SKim Phillips 					ch);
5519c4a7965SKim Phillips 				reset_dev = 1;
5529c4a7965SKim Phillips 			}
5539c4a7965SKim Phillips 		}
5549c4a7965SKim Phillips 	}
5559c4a7965SKim Phillips 	if (reset_dev || isr & ~TALITOS_ISR_CHERR || isr_lo) {
5569c4a7965SKim Phillips 		dev_err(dev, "done overflow, internal time out, or rngu error: "
5579c4a7965SKim Phillips 		        "ISR 0x%08x_%08x\n", isr, isr_lo);
5589c4a7965SKim Phillips 
5599c4a7965SKim Phillips 		/* purge request queues */
5609c4a7965SKim Phillips 		for (ch = 0; ch < priv->num_channels; ch++)
5619c4a7965SKim Phillips 			flush_channel(dev, ch, -EIO, 1);
5629c4a7965SKim Phillips 
5639c4a7965SKim Phillips 		/* reset and reinitialize the device */
5649c4a7965SKim Phillips 		init_device(dev);
5659c4a7965SKim Phillips 	}
5669c4a7965SKim Phillips }
5679c4a7965SKim Phillips 
5689c4a7965SKim Phillips static irqreturn_t talitos_interrupt(int irq, void *data)
5699c4a7965SKim Phillips {
5709c4a7965SKim Phillips 	struct device *dev = data;
5719c4a7965SKim Phillips 	struct talitos_private *priv = dev_get_drvdata(dev);
5729c4a7965SKim Phillips 	u32 isr, isr_lo;
5739c4a7965SKim Phillips 
5749c4a7965SKim Phillips 	isr = in_be32(priv->reg + TALITOS_ISR);
5759c4a7965SKim Phillips 	isr_lo = in_be32(priv->reg + TALITOS_ISR_LO);
576ca38a814SLee Nipper 	/* Acknowledge interrupt */
5779c4a7965SKim Phillips 	out_be32(priv->reg + TALITOS_ICR, isr);
5789c4a7965SKim Phillips 	out_be32(priv->reg + TALITOS_ICR_LO, isr_lo);
5799c4a7965SKim Phillips 
580ca38a814SLee Nipper 	if (unlikely((isr & ~TALITOS_ISR_CHDONE) || isr_lo))
58140405f10SKim Phillips 		talitos_error((unsigned long)data, isr, isr_lo);
582ca38a814SLee Nipper 	else
5831c2e8811SLee Nipper 		if (likely(isr & TALITOS_ISR_CHDONE)) {
5841c2e8811SLee Nipper 			/* mask further done interrupts. */
5851c2e8811SLee Nipper 			clrbits32(priv->reg + TALITOS_IMR, TALITOS_IMR_DONE);
5861c2e8811SLee Nipper 			/* done_task will unmask done interrupts at exit */
5879c4a7965SKim Phillips 			tasklet_schedule(&priv->done_task);
5881c2e8811SLee Nipper 		}
5899c4a7965SKim Phillips 
5909c4a7965SKim Phillips 	return (isr || isr_lo) ? IRQ_HANDLED : IRQ_NONE;
5919c4a7965SKim Phillips }
5929c4a7965SKim Phillips 
5939c4a7965SKim Phillips /*
5949c4a7965SKim Phillips  * hwrng
5959c4a7965SKim Phillips  */
5969c4a7965SKim Phillips static int talitos_rng_data_present(struct hwrng *rng, int wait)
5979c4a7965SKim Phillips {
5989c4a7965SKim Phillips 	struct device *dev = (struct device *)rng->priv;
5999c4a7965SKim Phillips 	struct talitos_private *priv = dev_get_drvdata(dev);
6009c4a7965SKim Phillips 	u32 ofl;
6019c4a7965SKim Phillips 	int i;
6029c4a7965SKim Phillips 
6039c4a7965SKim Phillips 	for (i = 0; i < 20; i++) {
6049c4a7965SKim Phillips 		ofl = in_be32(priv->reg + TALITOS_RNGUSR_LO) &
6059c4a7965SKim Phillips 		      TALITOS_RNGUSR_LO_OFL;
6069c4a7965SKim Phillips 		if (ofl || !wait)
6079c4a7965SKim Phillips 			break;
6089c4a7965SKim Phillips 		udelay(10);
6099c4a7965SKim Phillips 	}
6109c4a7965SKim Phillips 
6119c4a7965SKim Phillips 	return !!ofl;
6129c4a7965SKim Phillips }
6139c4a7965SKim Phillips 
6149c4a7965SKim Phillips static int talitos_rng_data_read(struct hwrng *rng, u32 *data)
6159c4a7965SKim Phillips {
6169c4a7965SKim Phillips 	struct device *dev = (struct device *)rng->priv;
6179c4a7965SKim Phillips 	struct talitos_private *priv = dev_get_drvdata(dev);
6189c4a7965SKim Phillips 
6199c4a7965SKim Phillips 	/* rng fifo requires 64-bit accesses */
6209c4a7965SKim Phillips 	*data = in_be32(priv->reg + TALITOS_RNGU_FIFO);
6219c4a7965SKim Phillips 	*data = in_be32(priv->reg + TALITOS_RNGU_FIFO_LO);
6229c4a7965SKim Phillips 
6239c4a7965SKim Phillips 	return sizeof(u32);
6249c4a7965SKim Phillips }
6259c4a7965SKim Phillips 
6269c4a7965SKim Phillips static int talitos_rng_init(struct hwrng *rng)
6279c4a7965SKim Phillips {
6289c4a7965SKim Phillips 	struct device *dev = (struct device *)rng->priv;
6299c4a7965SKim Phillips 	struct talitos_private *priv = dev_get_drvdata(dev);
6309c4a7965SKim Phillips 	unsigned int timeout = TALITOS_TIMEOUT;
6319c4a7965SKim Phillips 
6329c4a7965SKim Phillips 	setbits32(priv->reg + TALITOS_RNGURCR_LO, TALITOS_RNGURCR_LO_SR);
6339c4a7965SKim Phillips 	while (!(in_be32(priv->reg + TALITOS_RNGUSR_LO) & TALITOS_RNGUSR_LO_RD)
6349c4a7965SKim Phillips 	       && --timeout)
6359c4a7965SKim Phillips 		cpu_relax();
6369c4a7965SKim Phillips 	if (timeout == 0) {
6379c4a7965SKim Phillips 		dev_err(dev, "failed to reset rng hw\n");
6389c4a7965SKim Phillips 		return -ENODEV;
6399c4a7965SKim Phillips 	}
6409c4a7965SKim Phillips 
6419c4a7965SKim Phillips 	/* start generating */
6429c4a7965SKim Phillips 	setbits32(priv->reg + TALITOS_RNGUDSR_LO, 0);
6439c4a7965SKim Phillips 
6449c4a7965SKim Phillips 	return 0;
6459c4a7965SKim Phillips }
6469c4a7965SKim Phillips 
6479c4a7965SKim Phillips static int talitos_register_rng(struct device *dev)
6489c4a7965SKim Phillips {
6499c4a7965SKim Phillips 	struct talitos_private *priv = dev_get_drvdata(dev);
6509c4a7965SKim Phillips 
6519c4a7965SKim Phillips 	priv->rng.name		= dev_driver_string(dev),
6529c4a7965SKim Phillips 	priv->rng.init		= talitos_rng_init,
6539c4a7965SKim Phillips 	priv->rng.data_present	= talitos_rng_data_present,
6549c4a7965SKim Phillips 	priv->rng.data_read	= talitos_rng_data_read,
6559c4a7965SKim Phillips 	priv->rng.priv		= (unsigned long)dev;
6569c4a7965SKim Phillips 
6579c4a7965SKim Phillips 	return hwrng_register(&priv->rng);
6589c4a7965SKim Phillips }
6599c4a7965SKim Phillips 
6609c4a7965SKim Phillips static void talitos_unregister_rng(struct device *dev)
6619c4a7965SKim Phillips {
6629c4a7965SKim Phillips 	struct talitos_private *priv = dev_get_drvdata(dev);
6639c4a7965SKim Phillips 
6649c4a7965SKim Phillips 	hwrng_unregister(&priv->rng);
6659c4a7965SKim Phillips }
6669c4a7965SKim Phillips 
6679c4a7965SKim Phillips /*
6689c4a7965SKim Phillips  * crypto alg
6699c4a7965SKim Phillips  */
6709c4a7965SKim Phillips #define TALITOS_CRA_PRIORITY		3000
6719c4a7965SKim Phillips #define TALITOS_MAX_KEY_SIZE		64
6723952f17eSLee Nipper #define TALITOS_MAX_IV_LENGTH		16 /* max of AES_BLOCK_SIZE, DES3_EDE_BLOCK_SIZE */
67370bcaca7SLee Nipper 
6743952f17eSLee Nipper #define MD5_DIGEST_SIZE   16
6759c4a7965SKim Phillips 
6769c4a7965SKim Phillips struct talitos_ctx {
6779c4a7965SKim Phillips 	struct device *dev;
6789c4a7965SKim Phillips 	__be32 desc_hdr_template;
6799c4a7965SKim Phillips 	u8 key[TALITOS_MAX_KEY_SIZE];
68070bcaca7SLee Nipper 	u8 iv[TALITOS_MAX_IV_LENGTH];
6819c4a7965SKim Phillips 	unsigned int keylen;
6829c4a7965SKim Phillips 	unsigned int enckeylen;
6839c4a7965SKim Phillips 	unsigned int authkeylen;
6849c4a7965SKim Phillips 	unsigned int authsize;
6859c4a7965SKim Phillips };
6869c4a7965SKim Phillips 
687*56af8cd4SLee Nipper static int aead_setauthsize(struct crypto_aead *authenc,
6889c4a7965SKim Phillips 			    unsigned int authsize)
6899c4a7965SKim Phillips {
6909c4a7965SKim Phillips 	struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
6919c4a7965SKim Phillips 
6929c4a7965SKim Phillips 	ctx->authsize = authsize;
6939c4a7965SKim Phillips 
6949c4a7965SKim Phillips 	return 0;
6959c4a7965SKim Phillips }
6969c4a7965SKim Phillips 
697*56af8cd4SLee Nipper static int aead_setkey(struct crypto_aead *authenc,
6989c4a7965SKim Phillips 		       const u8 *key, unsigned int keylen)
6999c4a7965SKim Phillips {
7009c4a7965SKim Phillips 	struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
7019c4a7965SKim Phillips 	struct rtattr *rta = (void *)key;
7029c4a7965SKim Phillips 	struct crypto_authenc_key_param *param;
7039c4a7965SKim Phillips 	unsigned int authkeylen;
7049c4a7965SKim Phillips 	unsigned int enckeylen;
7059c4a7965SKim Phillips 
7069c4a7965SKim Phillips 	if (!RTA_OK(rta, keylen))
7079c4a7965SKim Phillips 		goto badkey;
7089c4a7965SKim Phillips 
7099c4a7965SKim Phillips 	if (rta->rta_type != CRYPTO_AUTHENC_KEYA_PARAM)
7109c4a7965SKim Phillips 		goto badkey;
7119c4a7965SKim Phillips 
7129c4a7965SKim Phillips 	if (RTA_PAYLOAD(rta) < sizeof(*param))
7139c4a7965SKim Phillips 		goto badkey;
7149c4a7965SKim Phillips 
7159c4a7965SKim Phillips 	param = RTA_DATA(rta);
7169c4a7965SKim Phillips 	enckeylen = be32_to_cpu(param->enckeylen);
7179c4a7965SKim Phillips 
7189c4a7965SKim Phillips 	key += RTA_ALIGN(rta->rta_len);
7199c4a7965SKim Phillips 	keylen -= RTA_ALIGN(rta->rta_len);
7209c4a7965SKim Phillips 
7219c4a7965SKim Phillips 	if (keylen < enckeylen)
7229c4a7965SKim Phillips 		goto badkey;
7239c4a7965SKim Phillips 
7249c4a7965SKim Phillips 	authkeylen = keylen - enckeylen;
7259c4a7965SKim Phillips 
7269c4a7965SKim Phillips 	if (keylen > TALITOS_MAX_KEY_SIZE)
7279c4a7965SKim Phillips 		goto badkey;
7289c4a7965SKim Phillips 
7299c4a7965SKim Phillips 	memcpy(&ctx->key, key, keylen);
7309c4a7965SKim Phillips 
7319c4a7965SKim Phillips 	ctx->keylen = keylen;
7329c4a7965SKim Phillips 	ctx->enckeylen = enckeylen;
7339c4a7965SKim Phillips 	ctx->authkeylen = authkeylen;
7349c4a7965SKim Phillips 
7359c4a7965SKim Phillips 	return 0;
7369c4a7965SKim Phillips 
7379c4a7965SKim Phillips badkey:
7389c4a7965SKim Phillips 	crypto_aead_set_flags(authenc, CRYPTO_TFM_RES_BAD_KEY_LEN);
7399c4a7965SKim Phillips 	return -EINVAL;
7409c4a7965SKim Phillips }
7419c4a7965SKim Phillips 
7429c4a7965SKim Phillips /*
743*56af8cd4SLee Nipper  * talitos_edesc - s/w-extended descriptor
7449c4a7965SKim Phillips  * @src_nents: number of segments in input scatterlist
7459c4a7965SKim Phillips  * @dst_nents: number of segments in output scatterlist
7469c4a7965SKim Phillips  * @dma_len: length of dma mapped link_tbl space
7479c4a7965SKim Phillips  * @dma_link_tbl: bus physical address of link_tbl
7489c4a7965SKim Phillips  * @desc: h/w descriptor
7499c4a7965SKim Phillips  * @link_tbl: input and output h/w link tables (if {src,dst}_nents > 1)
7509c4a7965SKim Phillips  *
7519c4a7965SKim Phillips  * if decrypting (with authcheck), or either one of src_nents or dst_nents
7529c4a7965SKim Phillips  * is greater than 1, an integrity check value is concatenated to the end
7539c4a7965SKim Phillips  * of link_tbl data
7549c4a7965SKim Phillips  */
755*56af8cd4SLee Nipper struct talitos_edesc {
7569c4a7965SKim Phillips 	int src_nents;
7579c4a7965SKim Phillips 	int dst_nents;
7589c4a7965SKim Phillips 	int dma_len;
7599c4a7965SKim Phillips 	dma_addr_t dma_link_tbl;
7609c4a7965SKim Phillips 	struct talitos_desc desc;
7619c4a7965SKim Phillips 	struct talitos_ptr link_tbl[0];
7629c4a7965SKim Phillips };
7639c4a7965SKim Phillips 
7649c4a7965SKim Phillips static void ipsec_esp_unmap(struct device *dev,
765*56af8cd4SLee Nipper 			    struct talitos_edesc *edesc,
7669c4a7965SKim Phillips 			    struct aead_request *areq)
7679c4a7965SKim Phillips {
7689c4a7965SKim Phillips 	unmap_single_talitos_ptr(dev, &edesc->desc.ptr[6], DMA_FROM_DEVICE);
7699c4a7965SKim Phillips 	unmap_single_talitos_ptr(dev, &edesc->desc.ptr[3], DMA_TO_DEVICE);
7709c4a7965SKim Phillips 	unmap_single_talitos_ptr(dev, &edesc->desc.ptr[2], DMA_TO_DEVICE);
7719c4a7965SKim Phillips 	unmap_single_talitos_ptr(dev, &edesc->desc.ptr[0], DMA_TO_DEVICE);
7729c4a7965SKim Phillips 
7739c4a7965SKim Phillips 	dma_unmap_sg(dev, areq->assoc, 1, DMA_TO_DEVICE);
7749c4a7965SKim Phillips 
7759c4a7965SKim Phillips 	if (areq->src != areq->dst) {
7769c4a7965SKim Phillips 		dma_unmap_sg(dev, areq->src, edesc->src_nents ? : 1,
7779c4a7965SKim Phillips 			     DMA_TO_DEVICE);
7789c4a7965SKim Phillips 		dma_unmap_sg(dev, areq->dst, edesc->dst_nents ? : 1,
7799c4a7965SKim Phillips 			     DMA_FROM_DEVICE);
7809c4a7965SKim Phillips 	} else {
7819c4a7965SKim Phillips 		dma_unmap_sg(dev, areq->src, edesc->src_nents ? : 1,
7829c4a7965SKim Phillips 			     DMA_BIDIRECTIONAL);
7839c4a7965SKim Phillips 	}
7849c4a7965SKim Phillips 
7859c4a7965SKim Phillips 	if (edesc->dma_len)
7869c4a7965SKim Phillips 		dma_unmap_single(dev, edesc->dma_link_tbl, edesc->dma_len,
7879c4a7965SKim Phillips 				 DMA_BIDIRECTIONAL);
7889c4a7965SKim Phillips }
7899c4a7965SKim Phillips 
7909c4a7965SKim Phillips /*
7919c4a7965SKim Phillips  * ipsec_esp descriptor callbacks
7929c4a7965SKim Phillips  */
7939c4a7965SKim Phillips static void ipsec_esp_encrypt_done(struct device *dev,
7949c4a7965SKim Phillips 				   struct talitos_desc *desc, void *context,
7959c4a7965SKim Phillips 				   int err)
7969c4a7965SKim Phillips {
7979c4a7965SKim Phillips 	struct aead_request *areq = context;
798*56af8cd4SLee Nipper 	struct talitos_edesc *edesc =
799*56af8cd4SLee Nipper 		 container_of(desc, struct talitos_edesc, desc);
8009c4a7965SKim Phillips 	struct crypto_aead *authenc = crypto_aead_reqtfm(areq);
8019c4a7965SKim Phillips 	struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
8029c4a7965SKim Phillips 	struct scatterlist *sg;
8039c4a7965SKim Phillips 	void *icvdata;
8049c4a7965SKim Phillips 
8059c4a7965SKim Phillips 	ipsec_esp_unmap(dev, edesc, areq);
8069c4a7965SKim Phillips 
8079c4a7965SKim Phillips 	/* copy the generated ICV to dst */
8089c4a7965SKim Phillips 	if (edesc->dma_len) {
8099c4a7965SKim Phillips 		icvdata = &edesc->link_tbl[edesc->src_nents +
810f3c85bc1SLee Nipper 					   edesc->dst_nents + 2];
8119c4a7965SKim Phillips 		sg = sg_last(areq->dst, edesc->dst_nents);
8129c4a7965SKim Phillips 		memcpy((char *)sg_virt(sg) + sg->length - ctx->authsize,
8139c4a7965SKim Phillips 		       icvdata, ctx->authsize);
8149c4a7965SKim Phillips 	}
8159c4a7965SKim Phillips 
8169c4a7965SKim Phillips 	kfree(edesc);
8179c4a7965SKim Phillips 
8189c4a7965SKim Phillips 	aead_request_complete(areq, err);
8199c4a7965SKim Phillips }
8209c4a7965SKim Phillips 
821fe5720e2SKim Phillips static void ipsec_esp_decrypt_swauth_done(struct device *dev,
8229c4a7965SKim Phillips 				   struct talitos_desc *desc, void *context,
8239c4a7965SKim Phillips 				   int err)
8249c4a7965SKim Phillips {
8259c4a7965SKim Phillips 	struct aead_request *req = context;
826*56af8cd4SLee Nipper 	struct talitos_edesc *edesc =
827*56af8cd4SLee Nipper 		 container_of(desc, struct talitos_edesc, desc);
8289c4a7965SKim Phillips 	struct crypto_aead *authenc = crypto_aead_reqtfm(req);
8299c4a7965SKim Phillips 	struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
8309c4a7965SKim Phillips 	struct scatterlist *sg;
8319c4a7965SKim Phillips 	void *icvdata;
8329c4a7965SKim Phillips 
8339c4a7965SKim Phillips 	ipsec_esp_unmap(dev, edesc, req);
8349c4a7965SKim Phillips 
8359c4a7965SKim Phillips 	if (!err) {
8369c4a7965SKim Phillips 		/* auth check */
8379c4a7965SKim Phillips 		if (edesc->dma_len)
8389c4a7965SKim Phillips 			icvdata = &edesc->link_tbl[edesc->src_nents +
839f3c85bc1SLee Nipper 						   edesc->dst_nents + 2];
8409c4a7965SKim Phillips 		else
8419c4a7965SKim Phillips 			icvdata = &edesc->link_tbl[0];
8429c4a7965SKim Phillips 
8439c4a7965SKim Phillips 		sg = sg_last(req->dst, edesc->dst_nents ? : 1);
8449c4a7965SKim Phillips 		err = memcmp(icvdata, (char *)sg_virt(sg) + sg->length -
8459c4a7965SKim Phillips 			     ctx->authsize, ctx->authsize) ? -EBADMSG : 0;
8469c4a7965SKim Phillips 	}
8479c4a7965SKim Phillips 
8489c4a7965SKim Phillips 	kfree(edesc);
8499c4a7965SKim Phillips 
8509c4a7965SKim Phillips 	aead_request_complete(req, err);
8519c4a7965SKim Phillips }
8529c4a7965SKim Phillips 
853fe5720e2SKim Phillips static void ipsec_esp_decrypt_hwauth_done(struct device *dev,
854fe5720e2SKim Phillips 				   struct talitos_desc *desc, void *context,
855fe5720e2SKim Phillips 				   int err)
856fe5720e2SKim Phillips {
857fe5720e2SKim Phillips 	struct aead_request *req = context;
858*56af8cd4SLee Nipper 	struct talitos_edesc *edesc =
859*56af8cd4SLee Nipper 		 container_of(desc, struct talitos_edesc, desc);
860fe5720e2SKim Phillips 
861fe5720e2SKim Phillips 	ipsec_esp_unmap(dev, edesc, req);
862fe5720e2SKim Phillips 
863fe5720e2SKim Phillips 	/* check ICV auth status */
864fe5720e2SKim Phillips 	if (!err)
865fe5720e2SKim Phillips 		if ((desc->hdr_lo & DESC_HDR_LO_ICCR1_MASK) !=
866fe5720e2SKim Phillips 		    DESC_HDR_LO_ICCR1_PASS)
867fe5720e2SKim Phillips 			err = -EBADMSG;
868fe5720e2SKim Phillips 
869fe5720e2SKim Phillips 	kfree(edesc);
870fe5720e2SKim Phillips 
871fe5720e2SKim Phillips 	aead_request_complete(req, err);
872fe5720e2SKim Phillips }
873fe5720e2SKim Phillips 
8749c4a7965SKim Phillips /*
8759c4a7965SKim Phillips  * convert scatterlist to SEC h/w link table format
8769c4a7965SKim Phillips  * stop at cryptlen bytes
8779c4a7965SKim Phillips  */
87870bcaca7SLee Nipper static int sg_to_link_tbl(struct scatterlist *sg, int sg_count,
8799c4a7965SKim Phillips 			   int cryptlen, struct talitos_ptr *link_tbl_ptr)
8809c4a7965SKim Phillips {
88170bcaca7SLee Nipper 	int n_sg = sg_count;
88270bcaca7SLee Nipper 
88370bcaca7SLee Nipper 	while (n_sg--) {
8849c4a7965SKim Phillips 		link_tbl_ptr->ptr = cpu_to_be32(sg_dma_address(sg));
8859c4a7965SKim Phillips 		link_tbl_ptr->len = cpu_to_be16(sg_dma_len(sg));
8869c4a7965SKim Phillips 		link_tbl_ptr->j_extent = 0;
8879c4a7965SKim Phillips 		link_tbl_ptr++;
8889c4a7965SKim Phillips 		cryptlen -= sg_dma_len(sg);
8899c4a7965SKim Phillips 		sg = sg_next(sg);
8909c4a7965SKim Phillips 	}
8919c4a7965SKim Phillips 
89270bcaca7SLee Nipper 	/* adjust (decrease) last one (or two) entry's len to cryptlen */
8939c4a7965SKim Phillips 	link_tbl_ptr--;
894c0e741d4SKim Phillips 	while (be16_to_cpu(link_tbl_ptr->len) <= (-cryptlen)) {
89570bcaca7SLee Nipper 		/* Empty this entry, and move to previous one */
89670bcaca7SLee Nipper 		cryptlen += be16_to_cpu(link_tbl_ptr->len);
89770bcaca7SLee Nipper 		link_tbl_ptr->len = 0;
89870bcaca7SLee Nipper 		sg_count--;
89970bcaca7SLee Nipper 		link_tbl_ptr--;
90070bcaca7SLee Nipper 	}
9019c4a7965SKim Phillips 	link_tbl_ptr->len = cpu_to_be16(be16_to_cpu(link_tbl_ptr->len)
9029c4a7965SKim Phillips 					+ cryptlen);
9039c4a7965SKim Phillips 
9049c4a7965SKim Phillips 	/* tag end of link table */
9059c4a7965SKim Phillips 	link_tbl_ptr->j_extent = DESC_PTR_LNKTBL_RETURN;
90670bcaca7SLee Nipper 
90770bcaca7SLee Nipper 	return sg_count;
9089c4a7965SKim Phillips }
9099c4a7965SKim Phillips 
9109c4a7965SKim Phillips /*
9119c4a7965SKim Phillips  * fill in and submit ipsec_esp descriptor
9129c4a7965SKim Phillips  */
913*56af8cd4SLee Nipper static int ipsec_esp(struct talitos_edesc *edesc, struct aead_request *areq,
9149c4a7965SKim Phillips 		     u8 *giv, u64 seq,
9159c4a7965SKim Phillips 		     void (*callback) (struct device *dev,
9169c4a7965SKim Phillips 				       struct talitos_desc *desc,
9179c4a7965SKim Phillips 				       void *context, int error))
9189c4a7965SKim Phillips {
9199c4a7965SKim Phillips 	struct crypto_aead *aead = crypto_aead_reqtfm(areq);
9209c4a7965SKim Phillips 	struct talitos_ctx *ctx = crypto_aead_ctx(aead);
9219c4a7965SKim Phillips 	struct device *dev = ctx->dev;
9229c4a7965SKim Phillips 	struct talitos_desc *desc = &edesc->desc;
9239c4a7965SKim Phillips 	unsigned int cryptlen = areq->cryptlen;
9249c4a7965SKim Phillips 	unsigned int authsize = ctx->authsize;
9259c4a7965SKim Phillips 	unsigned int ivsize;
926fa86a267SKim Phillips 	int sg_count, ret;
927fe5720e2SKim Phillips 	int sg_link_tbl_len;
9289c4a7965SKim Phillips 
9299c4a7965SKim Phillips 	/* hmac key */
9309c4a7965SKim Phillips 	map_single_talitos_ptr(dev, &desc->ptr[0], ctx->authkeylen, &ctx->key,
9319c4a7965SKim Phillips 			       0, DMA_TO_DEVICE);
9329c4a7965SKim Phillips 	/* hmac data */
9339c4a7965SKim Phillips 	map_single_talitos_ptr(dev, &desc->ptr[1], sg_virt(areq->src) -
9349c4a7965SKim Phillips 			       sg_virt(areq->assoc), sg_virt(areq->assoc), 0,
9359c4a7965SKim Phillips 			       DMA_TO_DEVICE);
9369c4a7965SKim Phillips 	/* cipher iv */
9379c4a7965SKim Phillips 	ivsize = crypto_aead_ivsize(aead);
9389c4a7965SKim Phillips 	map_single_talitos_ptr(dev, &desc->ptr[2], ivsize, giv ?: areq->iv, 0,
9399c4a7965SKim Phillips 			       DMA_TO_DEVICE);
9409c4a7965SKim Phillips 
9419c4a7965SKim Phillips 	/* cipher key */
9429c4a7965SKim Phillips 	map_single_talitos_ptr(dev, &desc->ptr[3], ctx->enckeylen,
9439c4a7965SKim Phillips 			       (char *)&ctx->key + ctx->authkeylen, 0,
9449c4a7965SKim Phillips 			       DMA_TO_DEVICE);
9459c4a7965SKim Phillips 
9469c4a7965SKim Phillips 	/*
9479c4a7965SKim Phillips 	 * cipher in
9489c4a7965SKim Phillips 	 * map and adjust cipher len to aead request cryptlen.
9499c4a7965SKim Phillips 	 * extent is bytes of HMAC postpended to ciphertext,
9509c4a7965SKim Phillips 	 * typically 12 for ipsec
9519c4a7965SKim Phillips 	 */
9529c4a7965SKim Phillips 	desc->ptr[4].len = cpu_to_be16(cryptlen);
9539c4a7965SKim Phillips 	desc->ptr[4].j_extent = authsize;
9549c4a7965SKim Phillips 
9559c4a7965SKim Phillips 	if (areq->src == areq->dst)
9569c4a7965SKim Phillips 		sg_count = dma_map_sg(dev, areq->src, edesc->src_nents ? : 1,
9579c4a7965SKim Phillips 				      DMA_BIDIRECTIONAL);
9589c4a7965SKim Phillips 	else
9599c4a7965SKim Phillips 		sg_count = dma_map_sg(dev, areq->src, edesc->src_nents ? : 1,
9609c4a7965SKim Phillips 				      DMA_TO_DEVICE);
9619c4a7965SKim Phillips 
9629c4a7965SKim Phillips 	if (sg_count == 1) {
9639c4a7965SKim Phillips 		desc->ptr[4].ptr = cpu_to_be32(sg_dma_address(areq->src));
9649c4a7965SKim Phillips 	} else {
965fe5720e2SKim Phillips 		sg_link_tbl_len = cryptlen;
966fe5720e2SKim Phillips 
967fe5720e2SKim Phillips 		if ((edesc->desc.hdr & DESC_HDR_MODE1_MDEU_CICV) &&
968fe5720e2SKim Phillips 			(edesc->desc.hdr & DESC_HDR_MODE0_ENCRYPT) == 0) {
969fe5720e2SKim Phillips 			sg_link_tbl_len = cryptlen + authsize;
970fe5720e2SKim Phillips 		}
971fe5720e2SKim Phillips 		sg_count = sg_to_link_tbl(areq->src, sg_count, sg_link_tbl_len,
9729c4a7965SKim Phillips 					  &edesc->link_tbl[0]);
97370bcaca7SLee Nipper 		if (sg_count > 1) {
9749c4a7965SKim Phillips 			desc->ptr[4].j_extent |= DESC_PTR_LNKTBL_JUMP;
9759c4a7965SKim Phillips 			desc->ptr[4].ptr = cpu_to_be32(edesc->dma_link_tbl);
9769c4a7965SKim Phillips 			dma_sync_single_for_device(ctx->dev, edesc->dma_link_tbl,
9779c4a7965SKim Phillips 						   edesc->dma_len, DMA_BIDIRECTIONAL);
97870bcaca7SLee Nipper 		} else {
97970bcaca7SLee Nipper 			/* Only one segment now, so no link tbl needed */
98070bcaca7SLee Nipper 			desc->ptr[4].ptr = cpu_to_be32(sg_dma_address(areq->src));
98170bcaca7SLee Nipper 		}
9829c4a7965SKim Phillips 	}
9839c4a7965SKim Phillips 
9849c4a7965SKim Phillips 	/* cipher out */
9859c4a7965SKim Phillips 	desc->ptr[5].len = cpu_to_be16(cryptlen);
9869c4a7965SKim Phillips 	desc->ptr[5].j_extent = authsize;
9879c4a7965SKim Phillips 
9889c4a7965SKim Phillips 	if (areq->src != areq->dst) {
9899c4a7965SKim Phillips 		sg_count = dma_map_sg(dev, areq->dst, edesc->dst_nents ? : 1,
9909c4a7965SKim Phillips 				      DMA_FROM_DEVICE);
9919c4a7965SKim Phillips 	}
9929c4a7965SKim Phillips 
9939c4a7965SKim Phillips 	if (sg_count == 1) {
9949c4a7965SKim Phillips 		desc->ptr[5].ptr = cpu_to_be32(sg_dma_address(areq->dst));
9959c4a7965SKim Phillips 	} else {
9969c4a7965SKim Phillips 		struct talitos_ptr *link_tbl_ptr =
997f3c85bc1SLee Nipper 			&edesc->link_tbl[edesc->src_nents + 1];
9989c4a7965SKim Phillips 
9999c4a7965SKim Phillips 		desc->ptr[5].ptr = cpu_to_be32((struct talitos_ptr *)
10009c4a7965SKim Phillips 					       edesc->dma_link_tbl +
1001f3c85bc1SLee Nipper 					       edesc->src_nents + 1);
100270bcaca7SLee Nipper 		sg_count = sg_to_link_tbl(areq->dst, sg_count, cryptlen,
10039c4a7965SKim Phillips 					  link_tbl_ptr);
1004fe5720e2SKim Phillips 
1005f3c85bc1SLee Nipper 		/* Add an entry to the link table for ICV data */
10069c4a7965SKim Phillips 		link_tbl_ptr += sg_count - 1;
10079c4a7965SKim Phillips 		link_tbl_ptr->j_extent = 0;
1008f3c85bc1SLee Nipper 		sg_count++;
10099c4a7965SKim Phillips 		link_tbl_ptr++;
10109c4a7965SKim Phillips 		link_tbl_ptr->j_extent = DESC_PTR_LNKTBL_RETURN;
10119c4a7965SKim Phillips 		link_tbl_ptr->len = cpu_to_be16(authsize);
10129c4a7965SKim Phillips 
10139c4a7965SKim Phillips 		/* icv data follows link tables */
10149c4a7965SKim Phillips 		link_tbl_ptr->ptr = cpu_to_be32((struct talitos_ptr *)
10159c4a7965SKim Phillips 						edesc->dma_link_tbl +
10169c4a7965SKim Phillips 					        edesc->src_nents +
1017f3c85bc1SLee Nipper 						edesc->dst_nents + 2);
10189c4a7965SKim Phillips 
10199c4a7965SKim Phillips 		desc->ptr[5].j_extent |= DESC_PTR_LNKTBL_JUMP;
10209c4a7965SKim Phillips 		dma_sync_single_for_device(ctx->dev, edesc->dma_link_tbl,
10219c4a7965SKim Phillips 					   edesc->dma_len, DMA_BIDIRECTIONAL);
10229c4a7965SKim Phillips 	}
10239c4a7965SKim Phillips 
10249c4a7965SKim Phillips 	/* iv out */
10259c4a7965SKim Phillips 	map_single_talitos_ptr(dev, &desc->ptr[6], ivsize, ctx->iv, 0,
10269c4a7965SKim Phillips 			       DMA_FROM_DEVICE);
10279c4a7965SKim Phillips 
1028fa86a267SKim Phillips 	ret = talitos_submit(dev, desc, callback, areq);
1029fa86a267SKim Phillips 	if (ret != -EINPROGRESS) {
1030fa86a267SKim Phillips 		ipsec_esp_unmap(dev, edesc, areq);
1031fa86a267SKim Phillips 		kfree(edesc);
1032fa86a267SKim Phillips 	}
1033fa86a267SKim Phillips 	return ret;
10349c4a7965SKim Phillips }
10359c4a7965SKim Phillips 
10369c4a7965SKim Phillips 
10379c4a7965SKim Phillips /*
10389c4a7965SKim Phillips  * derive number of elements in scatterlist
10399c4a7965SKim Phillips  */
10409c4a7965SKim Phillips static int sg_count(struct scatterlist *sg_list, int nbytes)
10419c4a7965SKim Phillips {
10429c4a7965SKim Phillips 	struct scatterlist *sg = sg_list;
10439c4a7965SKim Phillips 	int sg_nents = 0;
10449c4a7965SKim Phillips 
10459c4a7965SKim Phillips 	while (nbytes) {
10469c4a7965SKim Phillips 		sg_nents++;
10479c4a7965SKim Phillips 		nbytes -= sg->length;
10489c4a7965SKim Phillips 		sg = sg_next(sg);
10499c4a7965SKim Phillips 	}
10509c4a7965SKim Phillips 
10519c4a7965SKim Phillips 	return sg_nents;
10529c4a7965SKim Phillips }
10539c4a7965SKim Phillips 
10549c4a7965SKim Phillips /*
1055*56af8cd4SLee Nipper  * allocate and map the extended descriptor
10569c4a7965SKim Phillips  */
1057*56af8cd4SLee Nipper static struct talitos_edesc *ipsec_esp_edesc_alloc(struct aead_request *areq,
10589c4a7965SKim Phillips 						     int icv_stashing)
10599c4a7965SKim Phillips {
10609c4a7965SKim Phillips 	struct crypto_aead *authenc = crypto_aead_reqtfm(areq);
10619c4a7965SKim Phillips 	struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
1062*56af8cd4SLee Nipper 	struct talitos_edesc *edesc;
10639c4a7965SKim Phillips 	int src_nents, dst_nents, alloc_len, dma_len;
1064586725f8SKim Phillips 	gfp_t flags = areq->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP ? GFP_KERNEL :
1065586725f8SKim Phillips 		      GFP_ATOMIC;
10669c4a7965SKim Phillips 
10679c4a7965SKim Phillips 	if (areq->cryptlen + ctx->authsize > TALITOS_MAX_DATA_LEN) {
10689c4a7965SKim Phillips 		dev_err(ctx->dev, "cryptlen exceeds h/w max limit\n");
10699c4a7965SKim Phillips 		return ERR_PTR(-EINVAL);
10709c4a7965SKim Phillips 	}
10719c4a7965SKim Phillips 
10729c4a7965SKim Phillips 	src_nents = sg_count(areq->src, areq->cryptlen + ctx->authsize);
10739c4a7965SKim Phillips 	src_nents = (src_nents == 1) ? 0 : src_nents;
10749c4a7965SKim Phillips 
10759c4a7965SKim Phillips 	if (areq->dst == areq->src) {
10769c4a7965SKim Phillips 		dst_nents = src_nents;
10779c4a7965SKim Phillips 	} else {
10789c4a7965SKim Phillips 		dst_nents = sg_count(areq->dst, areq->cryptlen + ctx->authsize);
1079695ad589SLee Nipper 		dst_nents = (dst_nents == 1) ? 0 : dst_nents;
10809c4a7965SKim Phillips 	}
10819c4a7965SKim Phillips 
10829c4a7965SKim Phillips 	/*
10839c4a7965SKim Phillips 	 * allocate space for base edesc plus the link tables,
1084f3c85bc1SLee Nipper 	 * allowing for two separate entries for ICV and generated ICV (+ 2),
10859c4a7965SKim Phillips 	 * and the ICV data itself
10869c4a7965SKim Phillips 	 */
1087*56af8cd4SLee Nipper 	alloc_len = sizeof(struct talitos_edesc);
10889c4a7965SKim Phillips 	if (src_nents || dst_nents) {
1089f3c85bc1SLee Nipper 		dma_len = (src_nents + dst_nents + 2) *
10909c4a7965SKim Phillips 				 sizeof(struct talitos_ptr) + ctx->authsize;
10919c4a7965SKim Phillips 		alloc_len += dma_len;
10929c4a7965SKim Phillips 	} else {
10939c4a7965SKim Phillips 		dma_len = 0;
10949c4a7965SKim Phillips 		alloc_len += icv_stashing ? ctx->authsize : 0;
10959c4a7965SKim Phillips 	}
10969c4a7965SKim Phillips 
1097586725f8SKim Phillips 	edesc = kmalloc(alloc_len, GFP_DMA | flags);
10989c4a7965SKim Phillips 	if (!edesc) {
10999c4a7965SKim Phillips 		dev_err(ctx->dev, "could not allocate edescriptor\n");
11009c4a7965SKim Phillips 		return ERR_PTR(-ENOMEM);
11019c4a7965SKim Phillips 	}
11029c4a7965SKim Phillips 
11039c4a7965SKim Phillips 	edesc->src_nents = src_nents;
11049c4a7965SKim Phillips 	edesc->dst_nents = dst_nents;
11059c4a7965SKim Phillips 	edesc->dma_len = dma_len;
11069c4a7965SKim Phillips 	edesc->dma_link_tbl = dma_map_single(ctx->dev, &edesc->link_tbl[0],
11079c4a7965SKim Phillips 					     edesc->dma_len, DMA_BIDIRECTIONAL);
11089c4a7965SKim Phillips 
11099c4a7965SKim Phillips 	return edesc;
11109c4a7965SKim Phillips }
11119c4a7965SKim Phillips 
1112*56af8cd4SLee Nipper static int aead_encrypt(struct aead_request *req)
11139c4a7965SKim Phillips {
11149c4a7965SKim Phillips 	struct crypto_aead *authenc = crypto_aead_reqtfm(req);
11159c4a7965SKim Phillips 	struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
1116*56af8cd4SLee Nipper 	struct talitos_edesc *edesc;
11179c4a7965SKim Phillips 
11189c4a7965SKim Phillips 	/* allocate extended descriptor */
11199c4a7965SKim Phillips 	edesc = ipsec_esp_edesc_alloc(req, 0);
11209c4a7965SKim Phillips 	if (IS_ERR(edesc))
11219c4a7965SKim Phillips 		return PTR_ERR(edesc);
11229c4a7965SKim Phillips 
11239c4a7965SKim Phillips 	/* set encrypt */
112470bcaca7SLee Nipper 	edesc->desc.hdr = ctx->desc_hdr_template | DESC_HDR_MODE0_ENCRYPT;
11259c4a7965SKim Phillips 
11269c4a7965SKim Phillips 	return ipsec_esp(edesc, req, NULL, 0, ipsec_esp_encrypt_done);
11279c4a7965SKim Phillips }
11289c4a7965SKim Phillips 
1129fe5720e2SKim Phillips 
1130fe5720e2SKim Phillips 
1131*56af8cd4SLee Nipper static int aead_decrypt(struct aead_request *req)
11329c4a7965SKim Phillips {
11339c4a7965SKim Phillips 	struct crypto_aead *authenc = crypto_aead_reqtfm(req);
11349c4a7965SKim Phillips 	struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
11359c4a7965SKim Phillips 	unsigned int authsize = ctx->authsize;
1136fe5720e2SKim Phillips 	struct talitos_private *priv = dev_get_drvdata(ctx->dev);
1137*56af8cd4SLee Nipper 	struct talitos_edesc *edesc;
11389c4a7965SKim Phillips 	struct scatterlist *sg;
11399c4a7965SKim Phillips 	void *icvdata;
11409c4a7965SKim Phillips 
11419c4a7965SKim Phillips 	req->cryptlen -= authsize;
11429c4a7965SKim Phillips 
11439c4a7965SKim Phillips 	/* allocate extended descriptor */
11449c4a7965SKim Phillips 	edesc = ipsec_esp_edesc_alloc(req, 1);
11459c4a7965SKim Phillips 	if (IS_ERR(edesc))
11469c4a7965SKim Phillips 		return PTR_ERR(edesc);
11479c4a7965SKim Phillips 
1148fe5720e2SKim Phillips 	if ((priv->features & TALITOS_FTR_HW_AUTH_CHECK) &&
1149fe5720e2SKim Phillips 	    (((!edesc->src_nents && !edesc->dst_nents) ||
1150fe5720e2SKim Phillips 		priv->features & TALITOS_FTR_SRC_LINK_TBL_LEN_INCLUDES_EXTENT))) {
1151fe5720e2SKim Phillips 
1152fe5720e2SKim Phillips 		/* decrypt and check the ICV */
1153fe5720e2SKim Phillips 		edesc->desc.hdr = ctx->desc_hdr_template | DESC_HDR_DIR_INBOUND |
1154fe5720e2SKim Phillips 				  DESC_HDR_MODE1_MDEU_CICV;
1155fe5720e2SKim Phillips 
1156fe5720e2SKim Phillips 		/* reset integrity check result bits */
1157fe5720e2SKim Phillips 		edesc->desc.hdr_lo = 0;
1158fe5720e2SKim Phillips 
1159fe5720e2SKim Phillips 		return ipsec_esp(edesc, req, NULL, 0, ipsec_esp_decrypt_hwauth_done);
1160fe5720e2SKim Phillips 
1161fe5720e2SKim Phillips 	} else {
1162fe5720e2SKim Phillips 
1163fe5720e2SKim Phillips 		/* Have to check the ICV with software */
1164fe5720e2SKim Phillips 
1165fe5720e2SKim Phillips 		edesc->desc.hdr = ctx->desc_hdr_template | DESC_HDR_DIR_INBOUND;
1166fe5720e2SKim Phillips 
11679c4a7965SKim Phillips 		/* stash incoming ICV for later cmp with ICV generated by the h/w */
11689c4a7965SKim Phillips 		if (edesc->dma_len)
11699c4a7965SKim Phillips 			icvdata = &edesc->link_tbl[edesc->src_nents +
1170f3c85bc1SLee Nipper 						   edesc->dst_nents + 2];
11719c4a7965SKim Phillips 		else
11729c4a7965SKim Phillips 			icvdata = &edesc->link_tbl[0];
11739c4a7965SKim Phillips 
11749c4a7965SKim Phillips 		sg = sg_last(req->src, edesc->src_nents ? : 1);
11759c4a7965SKim Phillips 
11769c4a7965SKim Phillips 		memcpy(icvdata, (char *)sg_virt(sg) + sg->length - ctx->authsize,
11779c4a7965SKim Phillips 		       ctx->authsize);
11789c4a7965SKim Phillips 
1179fe5720e2SKim Phillips 		return ipsec_esp(edesc, req, NULL, 0, ipsec_esp_decrypt_swauth_done);
1180fe5720e2SKim Phillips 	}
11819c4a7965SKim Phillips }
11829c4a7965SKim Phillips 
1183*56af8cd4SLee Nipper static int aead_givencrypt(struct aead_givcrypt_request *req)
11849c4a7965SKim Phillips {
11859c4a7965SKim Phillips 	struct aead_request *areq = &req->areq;
11869c4a7965SKim Phillips 	struct crypto_aead *authenc = crypto_aead_reqtfm(areq);
11879c4a7965SKim Phillips 	struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
1188*56af8cd4SLee Nipper 	struct talitos_edesc *edesc;
11899c4a7965SKim Phillips 
11909c4a7965SKim Phillips 	/* allocate extended descriptor */
11919c4a7965SKim Phillips 	edesc = ipsec_esp_edesc_alloc(areq, 0);
11929c4a7965SKim Phillips 	if (IS_ERR(edesc))
11939c4a7965SKim Phillips 		return PTR_ERR(edesc);
11949c4a7965SKim Phillips 
11959c4a7965SKim Phillips 	/* set encrypt */
119670bcaca7SLee Nipper 	edesc->desc.hdr = ctx->desc_hdr_template | DESC_HDR_MODE0_ENCRYPT;
11979c4a7965SKim Phillips 
11989c4a7965SKim Phillips 	memcpy(req->giv, ctx->iv, crypto_aead_ivsize(authenc));
1199ba95487dSKim Phillips 	/* avoid consecutive packets going out with same IV */
1200ba95487dSKim Phillips 	*(__be64 *)req->giv ^= cpu_to_be64(req->seq);
12019c4a7965SKim Phillips 
12029c4a7965SKim Phillips 	return ipsec_esp(edesc, areq, req->giv, req->seq,
12039c4a7965SKim Phillips 			 ipsec_esp_encrypt_done);
12049c4a7965SKim Phillips }
12059c4a7965SKim Phillips 
12069c4a7965SKim Phillips struct talitos_alg_template {
1207*56af8cd4SLee Nipper 	struct crypto_alg alg;
12089c4a7965SKim Phillips 	__be32 desc_hdr_template;
12099c4a7965SKim Phillips };
12109c4a7965SKim Phillips 
12119c4a7965SKim Phillips static struct talitos_alg_template driver_algs[] = {
1212*56af8cd4SLee Nipper 	/* AEAD algorithms.  These use a single-pass ipsec_esp descriptor */
12139c4a7965SKim Phillips 	{
1214*56af8cd4SLee Nipper 		.alg = {
1215*56af8cd4SLee Nipper 			.cra_name = "authenc(hmac(sha1),cbc(aes))",
1216*56af8cd4SLee Nipper 			.cra_driver_name = "authenc-hmac-sha1-cbc-aes-talitos",
1217*56af8cd4SLee Nipper 			.cra_blocksize = AES_BLOCK_SIZE,
1218*56af8cd4SLee Nipper 			.cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
1219*56af8cd4SLee Nipper 			.cra_type = &crypto_aead_type,
1220*56af8cd4SLee Nipper 			.cra_aead = {
1221*56af8cd4SLee Nipper 				.setkey = aead_setkey,
1222*56af8cd4SLee Nipper 				.setauthsize = aead_setauthsize,
1223*56af8cd4SLee Nipper 				.encrypt = aead_encrypt,
1224*56af8cd4SLee Nipper 				.decrypt = aead_decrypt,
1225*56af8cd4SLee Nipper 				.givencrypt = aead_givencrypt,
12269c4a7965SKim Phillips 				.geniv = "<built-in>",
12273952f17eSLee Nipper 				.ivsize = AES_BLOCK_SIZE,
12283952f17eSLee Nipper 				.maxauthsize = SHA1_DIGEST_SIZE,
1229*56af8cd4SLee Nipper 			}
12309c4a7965SKim Phillips 		},
12319c4a7965SKim Phillips 		.desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
12329c4a7965SKim Phillips 			             DESC_HDR_SEL0_AESU |
12339c4a7965SKim Phillips 		                     DESC_HDR_MODE0_AESU_CBC |
12349c4a7965SKim Phillips 		                     DESC_HDR_SEL1_MDEUA |
12359c4a7965SKim Phillips 		                     DESC_HDR_MODE1_MDEU_INIT |
12369c4a7965SKim Phillips 		                     DESC_HDR_MODE1_MDEU_PAD |
12379c4a7965SKim Phillips 		                     DESC_HDR_MODE1_MDEU_SHA1_HMAC,
123870bcaca7SLee Nipper 	},
123970bcaca7SLee Nipper 	{
1240*56af8cd4SLee Nipper 		.alg = {
1241*56af8cd4SLee Nipper 			.cra_name = "authenc(hmac(sha1),cbc(des3_ede))",
1242*56af8cd4SLee Nipper 			.cra_driver_name = "authenc-hmac-sha1-cbc-3des-talitos",
1243*56af8cd4SLee Nipper 			.cra_blocksize = DES3_EDE_BLOCK_SIZE,
1244*56af8cd4SLee Nipper 			.cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
1245*56af8cd4SLee Nipper 			.cra_type = &crypto_aead_type,
1246*56af8cd4SLee Nipper 			.cra_aead = {
1247*56af8cd4SLee Nipper 				.setkey = aead_setkey,
1248*56af8cd4SLee Nipper 				.setauthsize = aead_setauthsize,
1249*56af8cd4SLee Nipper 				.encrypt = aead_encrypt,
1250*56af8cd4SLee Nipper 				.decrypt = aead_decrypt,
1251*56af8cd4SLee Nipper 				.givencrypt = aead_givencrypt,
125270bcaca7SLee Nipper 				.geniv = "<built-in>",
12533952f17eSLee Nipper 				.ivsize = DES3_EDE_BLOCK_SIZE,
12543952f17eSLee Nipper 				.maxauthsize = SHA1_DIGEST_SIZE,
1255*56af8cd4SLee Nipper 			}
125670bcaca7SLee Nipper 		},
125770bcaca7SLee Nipper 		.desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
125870bcaca7SLee Nipper 			             DESC_HDR_SEL0_DEU |
125970bcaca7SLee Nipper 		                     DESC_HDR_MODE0_DEU_CBC |
126070bcaca7SLee Nipper 		                     DESC_HDR_MODE0_DEU_3DES |
126170bcaca7SLee Nipper 		                     DESC_HDR_SEL1_MDEUA |
126270bcaca7SLee Nipper 		                     DESC_HDR_MODE1_MDEU_INIT |
126370bcaca7SLee Nipper 		                     DESC_HDR_MODE1_MDEU_PAD |
126470bcaca7SLee Nipper 		                     DESC_HDR_MODE1_MDEU_SHA1_HMAC,
12653952f17eSLee Nipper 	},
12663952f17eSLee Nipper 	{
1267*56af8cd4SLee Nipper 		.alg = {
1268*56af8cd4SLee Nipper 			.cra_name = "authenc(hmac(sha256),cbc(aes))",
1269*56af8cd4SLee Nipper 			.cra_driver_name = "authenc-hmac-sha256-cbc-aes-talitos",
1270*56af8cd4SLee Nipper 			.cra_blocksize = AES_BLOCK_SIZE,
1271*56af8cd4SLee Nipper 			.cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
1272*56af8cd4SLee Nipper 			.cra_type = &crypto_aead_type,
1273*56af8cd4SLee Nipper 			.cra_aead = {
1274*56af8cd4SLee Nipper 				.setkey = aead_setkey,
1275*56af8cd4SLee Nipper 				.setauthsize = aead_setauthsize,
1276*56af8cd4SLee Nipper 				.encrypt = aead_encrypt,
1277*56af8cd4SLee Nipper 				.decrypt = aead_decrypt,
1278*56af8cd4SLee Nipper 				.givencrypt = aead_givencrypt,
12793952f17eSLee Nipper 				.geniv = "<built-in>",
12803952f17eSLee Nipper 				.ivsize = AES_BLOCK_SIZE,
12813952f17eSLee Nipper 				.maxauthsize = SHA256_DIGEST_SIZE,
1282*56af8cd4SLee Nipper 			}
12833952f17eSLee Nipper 		},
12843952f17eSLee Nipper 		.desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
12853952f17eSLee Nipper 			             DESC_HDR_SEL0_AESU |
12863952f17eSLee Nipper 		                     DESC_HDR_MODE0_AESU_CBC |
12873952f17eSLee Nipper 		                     DESC_HDR_SEL1_MDEUA |
12883952f17eSLee Nipper 		                     DESC_HDR_MODE1_MDEU_INIT |
12893952f17eSLee Nipper 		                     DESC_HDR_MODE1_MDEU_PAD |
12903952f17eSLee Nipper 		                     DESC_HDR_MODE1_MDEU_SHA256_HMAC,
12913952f17eSLee Nipper 	},
12923952f17eSLee Nipper 	{
1293*56af8cd4SLee Nipper 		.alg = {
1294*56af8cd4SLee Nipper 			.cra_name = "authenc(hmac(sha256),cbc(des3_ede))",
1295*56af8cd4SLee Nipper 			.cra_driver_name = "authenc-hmac-sha256-cbc-3des-talitos",
1296*56af8cd4SLee Nipper 			.cra_blocksize = DES3_EDE_BLOCK_SIZE,
1297*56af8cd4SLee Nipper 			.cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
1298*56af8cd4SLee Nipper 			.cra_type = &crypto_aead_type,
1299*56af8cd4SLee Nipper 			.cra_aead = {
1300*56af8cd4SLee Nipper 				.setkey = aead_setkey,
1301*56af8cd4SLee Nipper 				.setauthsize = aead_setauthsize,
1302*56af8cd4SLee Nipper 				.encrypt = aead_encrypt,
1303*56af8cd4SLee Nipper 				.decrypt = aead_decrypt,
1304*56af8cd4SLee Nipper 				.givencrypt = aead_givencrypt,
13053952f17eSLee Nipper 				.geniv = "<built-in>",
13063952f17eSLee Nipper 				.ivsize = DES3_EDE_BLOCK_SIZE,
13073952f17eSLee Nipper 				.maxauthsize = SHA256_DIGEST_SIZE,
1308*56af8cd4SLee Nipper 			}
13093952f17eSLee Nipper 		},
13103952f17eSLee Nipper 		.desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
13113952f17eSLee Nipper 			             DESC_HDR_SEL0_DEU |
13123952f17eSLee Nipper 		                     DESC_HDR_MODE0_DEU_CBC |
13133952f17eSLee Nipper 		                     DESC_HDR_MODE0_DEU_3DES |
13143952f17eSLee Nipper 		                     DESC_HDR_SEL1_MDEUA |
13153952f17eSLee Nipper 		                     DESC_HDR_MODE1_MDEU_INIT |
13163952f17eSLee Nipper 		                     DESC_HDR_MODE1_MDEU_PAD |
13173952f17eSLee Nipper 		                     DESC_HDR_MODE1_MDEU_SHA256_HMAC,
13183952f17eSLee Nipper 	},
13193952f17eSLee Nipper 	{
1320*56af8cd4SLee Nipper 		.alg = {
1321*56af8cd4SLee Nipper 			.cra_name = "authenc(hmac(md5),cbc(aes))",
1322*56af8cd4SLee Nipper 			.cra_driver_name = "authenc-hmac-md5-cbc-aes-talitos",
1323*56af8cd4SLee Nipper 			.cra_blocksize = AES_BLOCK_SIZE,
1324*56af8cd4SLee Nipper 			.cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
1325*56af8cd4SLee Nipper 			.cra_type = &crypto_aead_type,
1326*56af8cd4SLee Nipper 			.cra_aead = {
1327*56af8cd4SLee Nipper 				.setkey = aead_setkey,
1328*56af8cd4SLee Nipper 				.setauthsize = aead_setauthsize,
1329*56af8cd4SLee Nipper 				.encrypt = aead_encrypt,
1330*56af8cd4SLee Nipper 				.decrypt = aead_decrypt,
1331*56af8cd4SLee Nipper 				.givencrypt = aead_givencrypt,
13323952f17eSLee Nipper 				.geniv = "<built-in>",
13333952f17eSLee Nipper 				.ivsize = AES_BLOCK_SIZE,
13343952f17eSLee Nipper 				.maxauthsize = MD5_DIGEST_SIZE,
1335*56af8cd4SLee Nipper 			}
13363952f17eSLee Nipper 		},
13373952f17eSLee Nipper 		.desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
13383952f17eSLee Nipper 			             DESC_HDR_SEL0_AESU |
13393952f17eSLee Nipper 		                     DESC_HDR_MODE0_AESU_CBC |
13403952f17eSLee Nipper 		                     DESC_HDR_SEL1_MDEUA |
13413952f17eSLee Nipper 		                     DESC_HDR_MODE1_MDEU_INIT |
13423952f17eSLee Nipper 		                     DESC_HDR_MODE1_MDEU_PAD |
13433952f17eSLee Nipper 		                     DESC_HDR_MODE1_MDEU_MD5_HMAC,
13443952f17eSLee Nipper 	},
13453952f17eSLee Nipper 	{
1346*56af8cd4SLee Nipper 		.alg = {
1347*56af8cd4SLee Nipper 			.cra_name = "authenc(hmac(md5),cbc(des3_ede))",
1348*56af8cd4SLee Nipper 			.cra_driver_name = "authenc-hmac-md5-cbc-3des-talitos",
1349*56af8cd4SLee Nipper 			.cra_blocksize = DES3_EDE_BLOCK_SIZE,
1350*56af8cd4SLee Nipper 			.cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
1351*56af8cd4SLee Nipper 			.cra_type = &crypto_aead_type,
1352*56af8cd4SLee Nipper 			.cra_aead = {
1353*56af8cd4SLee Nipper 				.setkey = aead_setkey,
1354*56af8cd4SLee Nipper 				.setauthsize = aead_setauthsize,
1355*56af8cd4SLee Nipper 				.encrypt = aead_encrypt,
1356*56af8cd4SLee Nipper 				.decrypt = aead_decrypt,
1357*56af8cd4SLee Nipper 				.givencrypt = aead_givencrypt,
13583952f17eSLee Nipper 				.geniv = "<built-in>",
13593952f17eSLee Nipper 				.ivsize = DES3_EDE_BLOCK_SIZE,
13603952f17eSLee Nipper 				.maxauthsize = MD5_DIGEST_SIZE,
1361*56af8cd4SLee Nipper 			}
13623952f17eSLee Nipper 		},
13633952f17eSLee Nipper 		.desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
13643952f17eSLee Nipper 			             DESC_HDR_SEL0_DEU |
13653952f17eSLee Nipper 		                     DESC_HDR_MODE0_DEU_CBC |
13663952f17eSLee Nipper 		                     DESC_HDR_MODE0_DEU_3DES |
13673952f17eSLee Nipper 		                     DESC_HDR_SEL1_MDEUA |
13683952f17eSLee Nipper 		                     DESC_HDR_MODE1_MDEU_INIT |
13693952f17eSLee Nipper 		                     DESC_HDR_MODE1_MDEU_PAD |
13703952f17eSLee Nipper 		                     DESC_HDR_MODE1_MDEU_MD5_HMAC,
13719c4a7965SKim Phillips 	}
13729c4a7965SKim Phillips };
13739c4a7965SKim Phillips 
13749c4a7965SKim Phillips struct talitos_crypto_alg {
13759c4a7965SKim Phillips 	struct list_head entry;
13769c4a7965SKim Phillips 	struct device *dev;
13779c4a7965SKim Phillips 	__be32 desc_hdr_template;
13789c4a7965SKim Phillips 	struct crypto_alg crypto_alg;
13799c4a7965SKim Phillips };
13809c4a7965SKim Phillips 
13819c4a7965SKim Phillips static int talitos_cra_init(struct crypto_tfm *tfm)
13829c4a7965SKim Phillips {
13839c4a7965SKim Phillips 	struct crypto_alg *alg = tfm->__crt_alg;
13849c4a7965SKim Phillips 	struct talitos_crypto_alg *talitos_alg =
13859c4a7965SKim Phillips 		 container_of(alg, struct talitos_crypto_alg, crypto_alg);
13869c4a7965SKim Phillips 	struct talitos_ctx *ctx = crypto_tfm_ctx(tfm);
13879c4a7965SKim Phillips 
13889c4a7965SKim Phillips 	/* update context with ptr to dev */
13899c4a7965SKim Phillips 	ctx->dev = talitos_alg->dev;
13909c4a7965SKim Phillips 	/* copy descriptor header template value */
13919c4a7965SKim Phillips 	ctx->desc_hdr_template = talitos_alg->desc_hdr_template;
13929c4a7965SKim Phillips 
13939c4a7965SKim Phillips 	/* random first IV */
139470bcaca7SLee Nipper 	get_random_bytes(ctx->iv, TALITOS_MAX_IV_LENGTH);
13959c4a7965SKim Phillips 
13969c4a7965SKim Phillips 	return 0;
13979c4a7965SKim Phillips }
13989c4a7965SKim Phillips 
13999c4a7965SKim Phillips /*
14009c4a7965SKim Phillips  * given the alg's descriptor header template, determine whether descriptor
14019c4a7965SKim Phillips  * type and primary/secondary execution units required match the hw
14029c4a7965SKim Phillips  * capabilities description provided in the device tree node.
14039c4a7965SKim Phillips  */
14049c4a7965SKim Phillips static int hw_supports(struct device *dev, __be32 desc_hdr_template)
14059c4a7965SKim Phillips {
14069c4a7965SKim Phillips 	struct talitos_private *priv = dev_get_drvdata(dev);
14079c4a7965SKim Phillips 	int ret;
14089c4a7965SKim Phillips 
14099c4a7965SKim Phillips 	ret = (1 << DESC_TYPE(desc_hdr_template) & priv->desc_types) &&
14109c4a7965SKim Phillips 	      (1 << PRIMARY_EU(desc_hdr_template) & priv->exec_units);
14119c4a7965SKim Phillips 
14129c4a7965SKim Phillips 	if (SECONDARY_EU(desc_hdr_template))
14139c4a7965SKim Phillips 		ret = ret && (1 << SECONDARY_EU(desc_hdr_template)
14149c4a7965SKim Phillips 		              & priv->exec_units);
14159c4a7965SKim Phillips 
14169c4a7965SKim Phillips 	return ret;
14179c4a7965SKim Phillips }
14189c4a7965SKim Phillips 
1419596f1034SAl Viro static int talitos_remove(struct of_device *ofdev)
14209c4a7965SKim Phillips {
14219c4a7965SKim Phillips 	struct device *dev = &ofdev->dev;
14229c4a7965SKim Phillips 	struct talitos_private *priv = dev_get_drvdata(dev);
14239c4a7965SKim Phillips 	struct talitos_crypto_alg *t_alg, *n;
14249c4a7965SKim Phillips 	int i;
14259c4a7965SKim Phillips 
14269c4a7965SKim Phillips 	list_for_each_entry_safe(t_alg, n, &priv->alg_list, entry) {
14279c4a7965SKim Phillips 		crypto_unregister_alg(&t_alg->crypto_alg);
14289c4a7965SKim Phillips 		list_del(&t_alg->entry);
14299c4a7965SKim Phillips 		kfree(t_alg);
14309c4a7965SKim Phillips 	}
14319c4a7965SKim Phillips 
14329c4a7965SKim Phillips 	if (hw_supports(dev, DESC_HDR_SEL0_RNG))
14339c4a7965SKim Phillips 		talitos_unregister_rng(dev);
14349c4a7965SKim Phillips 
1435ec6644d6SKim Phillips 	kfree(priv->submit_count);
14369c4a7965SKim Phillips 	kfree(priv->tail);
14379c4a7965SKim Phillips 	kfree(priv->head);
14389c4a7965SKim Phillips 
14399c4a7965SKim Phillips 	if (priv->fifo)
14409c4a7965SKim Phillips 		for (i = 0; i < priv->num_channels; i++)
14419c4a7965SKim Phillips 			kfree(priv->fifo[i]);
14429c4a7965SKim Phillips 
14439c4a7965SKim Phillips 	kfree(priv->fifo);
14449c4a7965SKim Phillips 	kfree(priv->head_lock);
14459c4a7965SKim Phillips 	kfree(priv->tail_lock);
14469c4a7965SKim Phillips 
14479c4a7965SKim Phillips 	if (priv->irq != NO_IRQ) {
14489c4a7965SKim Phillips 		free_irq(priv->irq, dev);
14499c4a7965SKim Phillips 		irq_dispose_mapping(priv->irq);
14509c4a7965SKim Phillips 	}
14519c4a7965SKim Phillips 
14529c4a7965SKim Phillips 	tasklet_kill(&priv->done_task);
14539c4a7965SKim Phillips 
14549c4a7965SKim Phillips 	iounmap(priv->reg);
14559c4a7965SKim Phillips 
14569c4a7965SKim Phillips 	dev_set_drvdata(dev, NULL);
14579c4a7965SKim Phillips 
14589c4a7965SKim Phillips 	kfree(priv);
14599c4a7965SKim Phillips 
14609c4a7965SKim Phillips 	return 0;
14619c4a7965SKim Phillips }
14629c4a7965SKim Phillips 
14639c4a7965SKim Phillips static struct talitos_crypto_alg *talitos_alg_alloc(struct device *dev,
14649c4a7965SKim Phillips 						    struct talitos_alg_template
14659c4a7965SKim Phillips 						           *template)
14669c4a7965SKim Phillips {
14679c4a7965SKim Phillips 	struct talitos_crypto_alg *t_alg;
14689c4a7965SKim Phillips 	struct crypto_alg *alg;
14699c4a7965SKim Phillips 
14709c4a7965SKim Phillips 	t_alg = kzalloc(sizeof(struct talitos_crypto_alg), GFP_KERNEL);
14719c4a7965SKim Phillips 	if (!t_alg)
14729c4a7965SKim Phillips 		return ERR_PTR(-ENOMEM);
14739c4a7965SKim Phillips 
14749c4a7965SKim Phillips 	alg = &t_alg->crypto_alg;
1475*56af8cd4SLee Nipper 	*alg = template->alg;
14769c4a7965SKim Phillips 
14779c4a7965SKim Phillips 	alg->cra_module = THIS_MODULE;
14789c4a7965SKim Phillips 	alg->cra_init = talitos_cra_init;
14799c4a7965SKim Phillips 	alg->cra_priority = TALITOS_CRA_PRIORITY;
14809c4a7965SKim Phillips 	alg->cra_alignmask = 0;
14819c4a7965SKim Phillips 	alg->cra_ctxsize = sizeof(struct talitos_ctx);
14829c4a7965SKim Phillips 
14839c4a7965SKim Phillips 	t_alg->desc_hdr_template = template->desc_hdr_template;
14849c4a7965SKim Phillips 	t_alg->dev = dev;
14859c4a7965SKim Phillips 
14869c4a7965SKim Phillips 	return t_alg;
14879c4a7965SKim Phillips }
14889c4a7965SKim Phillips 
14899c4a7965SKim Phillips static int talitos_probe(struct of_device *ofdev,
14909c4a7965SKim Phillips 			 const struct of_device_id *match)
14919c4a7965SKim Phillips {
14929c4a7965SKim Phillips 	struct device *dev = &ofdev->dev;
14939c4a7965SKim Phillips 	struct device_node *np = ofdev->node;
14949c4a7965SKim Phillips 	struct talitos_private *priv;
14959c4a7965SKim Phillips 	const unsigned int *prop;
14969c4a7965SKim Phillips 	int i, err;
14979c4a7965SKim Phillips 
14989c4a7965SKim Phillips 	priv = kzalloc(sizeof(struct talitos_private), GFP_KERNEL);
14999c4a7965SKim Phillips 	if (!priv)
15009c4a7965SKim Phillips 		return -ENOMEM;
15019c4a7965SKim Phillips 
15029c4a7965SKim Phillips 	dev_set_drvdata(dev, priv);
15039c4a7965SKim Phillips 
15049c4a7965SKim Phillips 	priv->ofdev = ofdev;
15059c4a7965SKim Phillips 
15069c4a7965SKim Phillips 	tasklet_init(&priv->done_task, talitos_done, (unsigned long)dev);
15079c4a7965SKim Phillips 
1508fe5720e2SKim Phillips 	INIT_LIST_HEAD(&priv->alg_list);
1509fe5720e2SKim Phillips 
15109c4a7965SKim Phillips 	priv->irq = irq_of_parse_and_map(np, 0);
15119c4a7965SKim Phillips 
15129c4a7965SKim Phillips 	if (priv->irq == NO_IRQ) {
15139c4a7965SKim Phillips 		dev_err(dev, "failed to map irq\n");
15149c4a7965SKim Phillips 		err = -EINVAL;
15159c4a7965SKim Phillips 		goto err_out;
15169c4a7965SKim Phillips 	}
15179c4a7965SKim Phillips 
15189c4a7965SKim Phillips 	/* get the irq line */
15199c4a7965SKim Phillips 	err = request_irq(priv->irq, talitos_interrupt, 0,
15209c4a7965SKim Phillips 			  dev_driver_string(dev), dev);
15219c4a7965SKim Phillips 	if (err) {
15229c4a7965SKim Phillips 		dev_err(dev, "failed to request irq %d\n", priv->irq);
15239c4a7965SKim Phillips 		irq_dispose_mapping(priv->irq);
15249c4a7965SKim Phillips 		priv->irq = NO_IRQ;
15259c4a7965SKim Phillips 		goto err_out;
15269c4a7965SKim Phillips 	}
15279c4a7965SKim Phillips 
15289c4a7965SKim Phillips 	priv->reg = of_iomap(np, 0);
15299c4a7965SKim Phillips 	if (!priv->reg) {
15309c4a7965SKim Phillips 		dev_err(dev, "failed to of_iomap\n");
15319c4a7965SKim Phillips 		err = -ENOMEM;
15329c4a7965SKim Phillips 		goto err_out;
15339c4a7965SKim Phillips 	}
15349c4a7965SKim Phillips 
15359c4a7965SKim Phillips 	/* get SEC version capabilities from device tree */
15369c4a7965SKim Phillips 	prop = of_get_property(np, "fsl,num-channels", NULL);
15379c4a7965SKim Phillips 	if (prop)
15389c4a7965SKim Phillips 		priv->num_channels = *prop;
15399c4a7965SKim Phillips 
15409c4a7965SKim Phillips 	prop = of_get_property(np, "fsl,channel-fifo-len", NULL);
15419c4a7965SKim Phillips 	if (prop)
15429c4a7965SKim Phillips 		priv->chfifo_len = *prop;
15439c4a7965SKim Phillips 
15449c4a7965SKim Phillips 	prop = of_get_property(np, "fsl,exec-units-mask", NULL);
15459c4a7965SKim Phillips 	if (prop)
15469c4a7965SKim Phillips 		priv->exec_units = *prop;
15479c4a7965SKim Phillips 
15489c4a7965SKim Phillips 	prop = of_get_property(np, "fsl,descriptor-types-mask", NULL);
15499c4a7965SKim Phillips 	if (prop)
15509c4a7965SKim Phillips 		priv->desc_types = *prop;
15519c4a7965SKim Phillips 
15529c4a7965SKim Phillips 	if (!is_power_of_2(priv->num_channels) || !priv->chfifo_len ||
15539c4a7965SKim Phillips 	    !priv->exec_units || !priv->desc_types) {
15549c4a7965SKim Phillips 		dev_err(dev, "invalid property data in device tree node\n");
15559c4a7965SKim Phillips 		err = -EINVAL;
15569c4a7965SKim Phillips 		goto err_out;
15579c4a7965SKim Phillips 	}
15589c4a7965SKim Phillips 
1559f3c85bc1SLee Nipper 	if (of_device_is_compatible(np, "fsl,sec3.0"))
1560f3c85bc1SLee Nipper 		priv->features |= TALITOS_FTR_SRC_LINK_TBL_LEN_INCLUDES_EXTENT;
1561f3c85bc1SLee Nipper 
1562fe5720e2SKim Phillips 	if (of_device_is_compatible(np, "fsl,sec2.1"))
1563fe5720e2SKim Phillips 		priv->features |= TALITOS_FTR_HW_AUTH_CHECK;
1564fe5720e2SKim Phillips 
15659c4a7965SKim Phillips 	priv->head_lock = kmalloc(sizeof(spinlock_t) * priv->num_channels,
15669c4a7965SKim Phillips 				  GFP_KERNEL);
15679c4a7965SKim Phillips 	priv->tail_lock = kmalloc(sizeof(spinlock_t) * priv->num_channels,
15689c4a7965SKim Phillips 				  GFP_KERNEL);
15699c4a7965SKim Phillips 	if (!priv->head_lock || !priv->tail_lock) {
15709c4a7965SKim Phillips 		dev_err(dev, "failed to allocate fifo locks\n");
15719c4a7965SKim Phillips 		err = -ENOMEM;
15729c4a7965SKim Phillips 		goto err_out;
15739c4a7965SKim Phillips 	}
15749c4a7965SKim Phillips 
15759c4a7965SKim Phillips 	for (i = 0; i < priv->num_channels; i++) {
15769c4a7965SKim Phillips 		spin_lock_init(&priv->head_lock[i]);
15779c4a7965SKim Phillips 		spin_lock_init(&priv->tail_lock[i]);
15789c4a7965SKim Phillips 	}
15799c4a7965SKim Phillips 
15809c4a7965SKim Phillips 	priv->fifo = kmalloc(sizeof(struct talitos_request *) *
15819c4a7965SKim Phillips 			     priv->num_channels, GFP_KERNEL);
15829c4a7965SKim Phillips 	if (!priv->fifo) {
15839c4a7965SKim Phillips 		dev_err(dev, "failed to allocate request fifo\n");
15849c4a7965SKim Phillips 		err = -ENOMEM;
15859c4a7965SKim Phillips 		goto err_out;
15869c4a7965SKim Phillips 	}
15879c4a7965SKim Phillips 
15889c4a7965SKim Phillips 	priv->fifo_len = roundup_pow_of_two(priv->chfifo_len);
15899c4a7965SKim Phillips 
15909c4a7965SKim Phillips 	for (i = 0; i < priv->num_channels; i++) {
15919c4a7965SKim Phillips 		priv->fifo[i] = kzalloc(sizeof(struct talitos_request) *
15929c4a7965SKim Phillips 					priv->fifo_len, GFP_KERNEL);
15939c4a7965SKim Phillips 		if (!priv->fifo[i]) {
15949c4a7965SKim Phillips 			dev_err(dev, "failed to allocate request fifo %d\n", i);
15959c4a7965SKim Phillips 			err = -ENOMEM;
15969c4a7965SKim Phillips 			goto err_out;
15979c4a7965SKim Phillips 		}
15989c4a7965SKim Phillips 	}
15999c4a7965SKim Phillips 
1600586725f8SKim Phillips 	priv->submit_count = kmalloc(sizeof(atomic_t) * priv->num_channels,
1601ec6644d6SKim Phillips 				     GFP_KERNEL);
1602ec6644d6SKim Phillips 	if (!priv->submit_count) {
1603ec6644d6SKim Phillips 		dev_err(dev, "failed to allocate fifo submit count space\n");
1604ec6644d6SKim Phillips 		err = -ENOMEM;
1605ec6644d6SKim Phillips 		goto err_out;
1606ec6644d6SKim Phillips 	}
1607ec6644d6SKim Phillips 	for (i = 0; i < priv->num_channels; i++)
16084b24ea97SVishnu Suresh 		atomic_set(&priv->submit_count[i], -(priv->chfifo_len - 1));
1609ec6644d6SKim Phillips 
16109c4a7965SKim Phillips 	priv->head = kzalloc(sizeof(int) * priv->num_channels, GFP_KERNEL);
16119c4a7965SKim Phillips 	priv->tail = kzalloc(sizeof(int) * priv->num_channels, GFP_KERNEL);
16129c4a7965SKim Phillips 	if (!priv->head || !priv->tail) {
16139c4a7965SKim Phillips 		dev_err(dev, "failed to allocate request index space\n");
16149c4a7965SKim Phillips 		err = -ENOMEM;
16159c4a7965SKim Phillips 		goto err_out;
16169c4a7965SKim Phillips 	}
16179c4a7965SKim Phillips 
16189c4a7965SKim Phillips 	/* reset and initialize the h/w */
16199c4a7965SKim Phillips 	err = init_device(dev);
16209c4a7965SKim Phillips 	if (err) {
16219c4a7965SKim Phillips 		dev_err(dev, "failed to initialize device\n");
16229c4a7965SKim Phillips 		goto err_out;
16239c4a7965SKim Phillips 	}
16249c4a7965SKim Phillips 
16259c4a7965SKim Phillips 	/* register the RNG, if available */
16269c4a7965SKim Phillips 	if (hw_supports(dev, DESC_HDR_SEL0_RNG)) {
16279c4a7965SKim Phillips 		err = talitos_register_rng(dev);
16289c4a7965SKim Phillips 		if (err) {
16299c4a7965SKim Phillips 			dev_err(dev, "failed to register hwrng: %d\n", err);
16309c4a7965SKim Phillips 			goto err_out;
16319c4a7965SKim Phillips 		} else
16329c4a7965SKim Phillips 			dev_info(dev, "hwrng\n");
16339c4a7965SKim Phillips 	}
16349c4a7965SKim Phillips 
16359c4a7965SKim Phillips 	/* register crypto algorithms the device supports */
16369c4a7965SKim Phillips 	for (i = 0; i < ARRAY_SIZE(driver_algs); i++) {
16379c4a7965SKim Phillips 		if (hw_supports(dev, driver_algs[i].desc_hdr_template)) {
16389c4a7965SKim Phillips 			struct talitos_crypto_alg *t_alg;
16399c4a7965SKim Phillips 
16409c4a7965SKim Phillips 			t_alg = talitos_alg_alloc(dev, &driver_algs[i]);
16419c4a7965SKim Phillips 			if (IS_ERR(t_alg)) {
16429c4a7965SKim Phillips 				err = PTR_ERR(t_alg);
16439c4a7965SKim Phillips 				goto err_out;
16449c4a7965SKim Phillips 			}
16459c4a7965SKim Phillips 
16469c4a7965SKim Phillips 			err = crypto_register_alg(&t_alg->crypto_alg);
16479c4a7965SKim Phillips 			if (err) {
16489c4a7965SKim Phillips 				dev_err(dev, "%s alg registration failed\n",
16499c4a7965SKim Phillips 					t_alg->crypto_alg.cra_driver_name);
16509c4a7965SKim Phillips 				kfree(t_alg);
16519c4a7965SKim Phillips 			} else {
16529c4a7965SKim Phillips 				list_add_tail(&t_alg->entry, &priv->alg_list);
16539c4a7965SKim Phillips 				dev_info(dev, "%s\n",
16549c4a7965SKim Phillips 					 t_alg->crypto_alg.cra_driver_name);
16559c4a7965SKim Phillips 			}
16569c4a7965SKim Phillips 		}
16579c4a7965SKim Phillips 	}
16589c4a7965SKim Phillips 
16599c4a7965SKim Phillips 	return 0;
16609c4a7965SKim Phillips 
16619c4a7965SKim Phillips err_out:
16629c4a7965SKim Phillips 	talitos_remove(ofdev);
16639c4a7965SKim Phillips 
16649c4a7965SKim Phillips 	return err;
16659c4a7965SKim Phillips }
16669c4a7965SKim Phillips 
16679c4a7965SKim Phillips static struct of_device_id talitos_match[] = {
16689c4a7965SKim Phillips 	{
16699c4a7965SKim Phillips 		.compatible = "fsl,sec2.0",
16709c4a7965SKim Phillips 	},
16719c4a7965SKim Phillips 	{},
16729c4a7965SKim Phillips };
16739c4a7965SKim Phillips MODULE_DEVICE_TABLE(of, talitos_match);
16749c4a7965SKim Phillips 
16759c4a7965SKim Phillips static struct of_platform_driver talitos_driver = {
16769c4a7965SKim Phillips 	.name = "talitos",
16779c4a7965SKim Phillips 	.match_table = talitos_match,
16789c4a7965SKim Phillips 	.probe = talitos_probe,
1679596f1034SAl Viro 	.remove = talitos_remove,
16809c4a7965SKim Phillips };
16819c4a7965SKim Phillips 
16829c4a7965SKim Phillips static int __init talitos_init(void)
16839c4a7965SKim Phillips {
16849c4a7965SKim Phillips 	return of_register_platform_driver(&talitos_driver);
16859c4a7965SKim Phillips }
16869c4a7965SKim Phillips module_init(talitos_init);
16879c4a7965SKim Phillips 
16889c4a7965SKim Phillips static void __exit talitos_exit(void)
16899c4a7965SKim Phillips {
16909c4a7965SKim Phillips 	of_unregister_platform_driver(&talitos_driver);
16919c4a7965SKim Phillips }
16929c4a7965SKim Phillips module_exit(talitos_exit);
16939c4a7965SKim Phillips 
16949c4a7965SKim Phillips MODULE_LICENSE("GPL");
16959c4a7965SKim Phillips MODULE_AUTHOR("Kim Phillips <kim.phillips@freescale.com>");
16969c4a7965SKim Phillips MODULE_DESCRIPTION("Freescale integrated security engine (SEC) driver");
1697