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 999c4a7965SKim Phillips /* next channel to be assigned next incoming descriptor */ 1009c4a7965SKim Phillips atomic_t last_chan; 1019c4a7965SKim Phillips 102ec6644d6SKim Phillips /* per-channel number of requests pending in channel h/w fifo */ 103ec6644d6SKim Phillips atomic_t *submit_count; 104ec6644d6SKim Phillips 1059c4a7965SKim Phillips /* per-channel request fifo */ 1069c4a7965SKim Phillips struct talitos_request **fifo; 1079c4a7965SKim Phillips 1089c4a7965SKim Phillips /* 1099c4a7965SKim Phillips * length of the request fifo 1109c4a7965SKim Phillips * fifo_len is chfifo_len rounded up to next power of 2 1119c4a7965SKim Phillips * so we can use bitwise ops to wrap 1129c4a7965SKim Phillips */ 1139c4a7965SKim Phillips unsigned int fifo_len; 1149c4a7965SKim Phillips 1159c4a7965SKim Phillips /* per-channel index to next free descriptor request */ 1169c4a7965SKim Phillips int *head; 1179c4a7965SKim Phillips 1189c4a7965SKim Phillips /* per-channel index to next in-progress/done descriptor request */ 1199c4a7965SKim Phillips int *tail; 1209c4a7965SKim Phillips 1219c4a7965SKim Phillips /* per-channel request submission (head) and release (tail) locks */ 1229c4a7965SKim Phillips spinlock_t *head_lock; 1239c4a7965SKim Phillips spinlock_t *tail_lock; 1249c4a7965SKim Phillips 1259c4a7965SKim Phillips /* request callback tasklet */ 1269c4a7965SKim Phillips struct tasklet_struct done_task; 1279c4a7965SKim Phillips struct tasklet_struct error_task; 1289c4a7965SKim Phillips 1299c4a7965SKim Phillips /* list of registered algorithms */ 1309c4a7965SKim Phillips struct list_head alg_list; 1319c4a7965SKim Phillips 1329c4a7965SKim Phillips /* hwrng device */ 1339c4a7965SKim Phillips struct hwrng rng; 1349c4a7965SKim Phillips }; 1359c4a7965SKim Phillips 1369c4a7965SKim Phillips /* 1379c4a7965SKim Phillips * map virtual single (contiguous) pointer to h/w descriptor pointer 1389c4a7965SKim Phillips */ 1399c4a7965SKim Phillips static void map_single_talitos_ptr(struct device *dev, 1409c4a7965SKim Phillips struct talitos_ptr *talitos_ptr, 1419c4a7965SKim Phillips unsigned short len, void *data, 1429c4a7965SKim Phillips unsigned char extent, 1439c4a7965SKim Phillips enum dma_data_direction dir) 1449c4a7965SKim Phillips { 1459c4a7965SKim Phillips talitos_ptr->len = cpu_to_be16(len); 1469c4a7965SKim Phillips talitos_ptr->ptr = cpu_to_be32(dma_map_single(dev, data, len, dir)); 1479c4a7965SKim Phillips talitos_ptr->j_extent = extent; 1489c4a7965SKim Phillips } 1499c4a7965SKim Phillips 1509c4a7965SKim Phillips /* 1519c4a7965SKim Phillips * unmap bus single (contiguous) h/w descriptor pointer 1529c4a7965SKim Phillips */ 1539c4a7965SKim Phillips static void unmap_single_talitos_ptr(struct device *dev, 1549c4a7965SKim Phillips struct talitos_ptr *talitos_ptr, 1559c4a7965SKim Phillips enum dma_data_direction dir) 1569c4a7965SKim Phillips { 1579c4a7965SKim Phillips dma_unmap_single(dev, be32_to_cpu(talitos_ptr->ptr), 1589c4a7965SKim Phillips be16_to_cpu(talitos_ptr->len), dir); 1599c4a7965SKim Phillips } 1609c4a7965SKim Phillips 1619c4a7965SKim Phillips static int reset_channel(struct device *dev, int ch) 1629c4a7965SKim Phillips { 1639c4a7965SKim Phillips struct talitos_private *priv = dev_get_drvdata(dev); 1649c4a7965SKim Phillips unsigned int timeout = TALITOS_TIMEOUT; 1659c4a7965SKim Phillips 1669c4a7965SKim Phillips setbits32(priv->reg + TALITOS_CCCR(ch), TALITOS_CCCR_RESET); 1679c4a7965SKim Phillips 1689c4a7965SKim Phillips while ((in_be32(priv->reg + TALITOS_CCCR(ch)) & TALITOS_CCCR_RESET) 1699c4a7965SKim Phillips && --timeout) 1709c4a7965SKim Phillips cpu_relax(); 1719c4a7965SKim Phillips 1729c4a7965SKim Phillips if (timeout == 0) { 1739c4a7965SKim Phillips dev_err(dev, "failed to reset channel %d\n", ch); 1749c4a7965SKim Phillips return -EIO; 1759c4a7965SKim Phillips } 1769c4a7965SKim Phillips 1779c4a7965SKim Phillips /* set done writeback and IRQ */ 1789c4a7965SKim Phillips setbits32(priv->reg + TALITOS_CCCR_LO(ch), TALITOS_CCCR_LO_CDWE | 1799c4a7965SKim Phillips TALITOS_CCCR_LO_CDIE); 1809c4a7965SKim Phillips 1819c4a7965SKim Phillips return 0; 1829c4a7965SKim Phillips } 1839c4a7965SKim Phillips 1849c4a7965SKim Phillips static int reset_device(struct device *dev) 1859c4a7965SKim Phillips { 1869c4a7965SKim Phillips struct talitos_private *priv = dev_get_drvdata(dev); 1879c4a7965SKim Phillips unsigned int timeout = TALITOS_TIMEOUT; 1889c4a7965SKim Phillips 1899c4a7965SKim Phillips setbits32(priv->reg + TALITOS_MCR, TALITOS_MCR_SWR); 1909c4a7965SKim Phillips 1919c4a7965SKim Phillips while ((in_be32(priv->reg + TALITOS_MCR) & TALITOS_MCR_SWR) 1929c4a7965SKim Phillips && --timeout) 1939c4a7965SKim Phillips cpu_relax(); 1949c4a7965SKim Phillips 1959c4a7965SKim Phillips if (timeout == 0) { 1969c4a7965SKim Phillips dev_err(dev, "failed to reset device\n"); 1979c4a7965SKim Phillips return -EIO; 1989c4a7965SKim Phillips } 1999c4a7965SKim Phillips 2009c4a7965SKim Phillips return 0; 2019c4a7965SKim Phillips } 2029c4a7965SKim Phillips 2039c4a7965SKim Phillips /* 2049c4a7965SKim Phillips * Reset and initialize the device 2059c4a7965SKim Phillips */ 2069c4a7965SKim Phillips static int init_device(struct device *dev) 2079c4a7965SKim Phillips { 2089c4a7965SKim Phillips struct talitos_private *priv = dev_get_drvdata(dev); 2099c4a7965SKim Phillips int ch, err; 2109c4a7965SKim Phillips 2119c4a7965SKim Phillips /* 2129c4a7965SKim Phillips * Master reset 2139c4a7965SKim Phillips * errata documentation: warning: certain SEC interrupts 2149c4a7965SKim Phillips * are not fully cleared by writing the MCR:SWR bit, 2159c4a7965SKim Phillips * set bit twice to completely reset 2169c4a7965SKim Phillips */ 2179c4a7965SKim Phillips err = reset_device(dev); 2189c4a7965SKim Phillips if (err) 2199c4a7965SKim Phillips return err; 2209c4a7965SKim Phillips 2219c4a7965SKim Phillips err = reset_device(dev); 2229c4a7965SKim Phillips if (err) 2239c4a7965SKim Phillips return err; 2249c4a7965SKim Phillips 2259c4a7965SKim Phillips /* reset channels */ 2269c4a7965SKim Phillips for (ch = 0; ch < priv->num_channels; ch++) { 2279c4a7965SKim Phillips err = reset_channel(dev, ch); 2289c4a7965SKim Phillips if (err) 2299c4a7965SKim Phillips return err; 2309c4a7965SKim Phillips } 2319c4a7965SKim Phillips 2329c4a7965SKim Phillips /* enable channel done and error interrupts */ 2339c4a7965SKim Phillips setbits32(priv->reg + TALITOS_IMR, TALITOS_IMR_INIT); 2349c4a7965SKim Phillips setbits32(priv->reg + TALITOS_IMR_LO, TALITOS_IMR_LO_INIT); 2359c4a7965SKim Phillips 2369c4a7965SKim Phillips return 0; 2379c4a7965SKim Phillips } 2389c4a7965SKim Phillips 2399c4a7965SKim Phillips /** 2409c4a7965SKim Phillips * talitos_submit - submits a descriptor to the device for processing 2419c4a7965SKim Phillips * @dev: the SEC device to be used 2429c4a7965SKim Phillips * @desc: the descriptor to be processed by the device 2439c4a7965SKim Phillips * @callback: whom to call when processing is complete 2449c4a7965SKim Phillips * @context: a handle for use by caller (optional) 2459c4a7965SKim Phillips * 2469c4a7965SKim Phillips * desc must contain valid dma-mapped (bus physical) address pointers. 2479c4a7965SKim Phillips * callback must check err and feedback in descriptor header 2489c4a7965SKim Phillips * for device processing status. 2499c4a7965SKim Phillips */ 2509c4a7965SKim Phillips static int talitos_submit(struct device *dev, struct talitos_desc *desc, 2519c4a7965SKim Phillips void (*callback)(struct device *dev, 2529c4a7965SKim Phillips struct talitos_desc *desc, 2539c4a7965SKim Phillips void *context, int error), 2549c4a7965SKim Phillips void *context) 2559c4a7965SKim Phillips { 2569c4a7965SKim Phillips struct talitos_private *priv = dev_get_drvdata(dev); 2579c4a7965SKim Phillips struct talitos_request *request; 2589c4a7965SKim Phillips unsigned long flags, ch; 2599c4a7965SKim Phillips int head; 2609c4a7965SKim Phillips 2619c4a7965SKim Phillips /* select done notification */ 2629c4a7965SKim Phillips desc->hdr |= DESC_HDR_DONE_NOTIFY; 2639c4a7965SKim Phillips 2649c4a7965SKim Phillips /* emulate SEC's round-robin channel fifo polling scheme */ 2659c4a7965SKim Phillips ch = atomic_inc_return(&priv->last_chan) & (priv->num_channels - 1); 2669c4a7965SKim Phillips 2679c4a7965SKim Phillips spin_lock_irqsave(&priv->head_lock[ch], flags); 2689c4a7965SKim Phillips 269ec6644d6SKim Phillips if (!atomic_inc_not_zero(&priv->submit_count[ch])) { 270ec6644d6SKim Phillips /* h/w fifo is full */ 2719c4a7965SKim Phillips spin_unlock_irqrestore(&priv->head_lock[ch], flags); 2729c4a7965SKim Phillips return -EAGAIN; 2739c4a7965SKim Phillips } 2749c4a7965SKim Phillips 275ec6644d6SKim Phillips head = priv->head[ch]; 276ec6644d6SKim Phillips request = &priv->fifo[ch][head]; 277ec6644d6SKim Phillips 2789c4a7965SKim Phillips /* map descriptor and save caller data */ 2799c4a7965SKim Phillips request->dma_desc = dma_map_single(dev, desc, sizeof(*desc), 2809c4a7965SKim Phillips DMA_BIDIRECTIONAL); 2819c4a7965SKim Phillips request->callback = callback; 2829c4a7965SKim Phillips request->context = context; 2839c4a7965SKim Phillips 2849c4a7965SKim Phillips /* increment fifo head */ 2859c4a7965SKim Phillips priv->head[ch] = (priv->head[ch] + 1) & (priv->fifo_len - 1); 2869c4a7965SKim Phillips 2879c4a7965SKim Phillips smp_wmb(); 2889c4a7965SKim Phillips request->desc = desc; 2899c4a7965SKim Phillips 2909c4a7965SKim Phillips /* GO! */ 2919c4a7965SKim Phillips wmb(); 2929c4a7965SKim Phillips out_be32(priv->reg + TALITOS_FF_LO(ch), request->dma_desc); 2939c4a7965SKim Phillips 2949c4a7965SKim Phillips spin_unlock_irqrestore(&priv->head_lock[ch], flags); 2959c4a7965SKim Phillips 2969c4a7965SKim Phillips return -EINPROGRESS; 2979c4a7965SKim Phillips } 2989c4a7965SKim Phillips 2999c4a7965SKim Phillips /* 3009c4a7965SKim Phillips * process what was done, notify callback of error if not 3019c4a7965SKim Phillips */ 3029c4a7965SKim Phillips static void flush_channel(struct device *dev, int ch, int error, int reset_ch) 3039c4a7965SKim Phillips { 3049c4a7965SKim Phillips struct talitos_private *priv = dev_get_drvdata(dev); 3059c4a7965SKim Phillips struct talitos_request *request, saved_req; 3069c4a7965SKim Phillips unsigned long flags; 3079c4a7965SKim Phillips int tail, status; 3089c4a7965SKim Phillips 3099c4a7965SKim Phillips spin_lock_irqsave(&priv->tail_lock[ch], flags); 3109c4a7965SKim Phillips 3119c4a7965SKim Phillips tail = priv->tail[ch]; 3129c4a7965SKim Phillips while (priv->fifo[ch][tail].desc) { 3139c4a7965SKim Phillips request = &priv->fifo[ch][tail]; 3149c4a7965SKim Phillips 3159c4a7965SKim Phillips /* descriptors with their done bits set don't get the error */ 3169c4a7965SKim Phillips rmb(); 3179c4a7965SKim Phillips if ((request->desc->hdr & DESC_HDR_DONE) == DESC_HDR_DONE) 3189c4a7965SKim Phillips status = 0; 3199c4a7965SKim Phillips else 3209c4a7965SKim Phillips if (!error) 3219c4a7965SKim Phillips break; 3229c4a7965SKim Phillips else 3239c4a7965SKim Phillips status = error; 3249c4a7965SKim Phillips 3259c4a7965SKim Phillips dma_unmap_single(dev, request->dma_desc, 3269c4a7965SKim Phillips sizeof(struct talitos_desc), DMA_BIDIRECTIONAL); 3279c4a7965SKim Phillips 3289c4a7965SKim Phillips /* copy entries so we can call callback outside lock */ 3299c4a7965SKim Phillips saved_req.desc = request->desc; 3309c4a7965SKim Phillips saved_req.callback = request->callback; 3319c4a7965SKim Phillips saved_req.context = request->context; 3329c4a7965SKim Phillips 3339c4a7965SKim Phillips /* release request entry in fifo */ 3349c4a7965SKim Phillips smp_wmb(); 3359c4a7965SKim Phillips request->desc = NULL; 3369c4a7965SKim Phillips 3379c4a7965SKim Phillips /* increment fifo tail */ 3389c4a7965SKim Phillips priv->tail[ch] = (tail + 1) & (priv->fifo_len - 1); 3399c4a7965SKim Phillips 3409c4a7965SKim Phillips spin_unlock_irqrestore(&priv->tail_lock[ch], flags); 341ec6644d6SKim Phillips 342ec6644d6SKim Phillips atomic_dec(&priv->submit_count[ch]); 343ec6644d6SKim Phillips 3449c4a7965SKim Phillips saved_req.callback(dev, saved_req.desc, saved_req.context, 3459c4a7965SKim Phillips status); 3469c4a7965SKim Phillips /* channel may resume processing in single desc error case */ 3479c4a7965SKim Phillips if (error && !reset_ch && status == error) 3489c4a7965SKim Phillips return; 3499c4a7965SKim Phillips spin_lock_irqsave(&priv->tail_lock[ch], flags); 3509c4a7965SKim Phillips tail = priv->tail[ch]; 3519c4a7965SKim Phillips } 3529c4a7965SKim Phillips 3539c4a7965SKim Phillips spin_unlock_irqrestore(&priv->tail_lock[ch], flags); 3549c4a7965SKim Phillips } 3559c4a7965SKim Phillips 3569c4a7965SKim Phillips /* 3579c4a7965SKim Phillips * process completed requests for channels that have done status 3589c4a7965SKim Phillips */ 3599c4a7965SKim Phillips static void talitos_done(unsigned long data) 3609c4a7965SKim Phillips { 3619c4a7965SKim Phillips struct device *dev = (struct device *)data; 3629c4a7965SKim Phillips struct talitos_private *priv = dev_get_drvdata(dev); 3639c4a7965SKim Phillips int ch; 3649c4a7965SKim Phillips 3659c4a7965SKim Phillips for (ch = 0; ch < priv->num_channels; ch++) 3669c4a7965SKim Phillips flush_channel(dev, ch, 0, 0); 3679c4a7965SKim Phillips } 3689c4a7965SKim Phillips 3699c4a7965SKim Phillips /* 3709c4a7965SKim Phillips * locate current (offending) descriptor 3719c4a7965SKim Phillips */ 3729c4a7965SKim Phillips static struct talitos_desc *current_desc(struct device *dev, int ch) 3739c4a7965SKim Phillips { 3749c4a7965SKim Phillips struct talitos_private *priv = dev_get_drvdata(dev); 3759c4a7965SKim Phillips int tail = priv->tail[ch]; 3769c4a7965SKim Phillips dma_addr_t cur_desc; 3779c4a7965SKim Phillips 3789c4a7965SKim Phillips cur_desc = in_be32(priv->reg + TALITOS_CDPR_LO(ch)); 3799c4a7965SKim Phillips 3809c4a7965SKim Phillips while (priv->fifo[ch][tail].dma_desc != cur_desc) { 3819c4a7965SKim Phillips tail = (tail + 1) & (priv->fifo_len - 1); 3829c4a7965SKim Phillips if (tail == priv->tail[ch]) { 3839c4a7965SKim Phillips dev_err(dev, "couldn't locate current descriptor\n"); 3849c4a7965SKim Phillips return NULL; 3859c4a7965SKim Phillips } 3869c4a7965SKim Phillips } 3879c4a7965SKim Phillips 3889c4a7965SKim Phillips return priv->fifo[ch][tail].desc; 3899c4a7965SKim Phillips } 3909c4a7965SKim Phillips 3919c4a7965SKim Phillips /* 3929c4a7965SKim Phillips * user diagnostics; report root cause of error based on execution unit status 3939c4a7965SKim Phillips */ 3949c4a7965SKim Phillips static void report_eu_error(struct device *dev, int ch, struct talitos_desc *desc) 3959c4a7965SKim Phillips { 3969c4a7965SKim Phillips struct talitos_private *priv = dev_get_drvdata(dev); 3979c4a7965SKim Phillips int i; 3989c4a7965SKim Phillips 3999c4a7965SKim Phillips switch (desc->hdr & DESC_HDR_SEL0_MASK) { 4009c4a7965SKim Phillips case DESC_HDR_SEL0_AFEU: 4019c4a7965SKim Phillips dev_err(dev, "AFEUISR 0x%08x_%08x\n", 4029c4a7965SKim Phillips in_be32(priv->reg + TALITOS_AFEUISR), 4039c4a7965SKim Phillips in_be32(priv->reg + TALITOS_AFEUISR_LO)); 4049c4a7965SKim Phillips break; 4059c4a7965SKim Phillips case DESC_HDR_SEL0_DEU: 4069c4a7965SKim Phillips dev_err(dev, "DEUISR 0x%08x_%08x\n", 4079c4a7965SKim Phillips in_be32(priv->reg + TALITOS_DEUISR), 4089c4a7965SKim Phillips in_be32(priv->reg + TALITOS_DEUISR_LO)); 4099c4a7965SKim Phillips break; 4109c4a7965SKim Phillips case DESC_HDR_SEL0_MDEUA: 4119c4a7965SKim Phillips case DESC_HDR_SEL0_MDEUB: 4129c4a7965SKim Phillips dev_err(dev, "MDEUISR 0x%08x_%08x\n", 4139c4a7965SKim Phillips in_be32(priv->reg + TALITOS_MDEUISR), 4149c4a7965SKim Phillips in_be32(priv->reg + TALITOS_MDEUISR_LO)); 4159c4a7965SKim Phillips break; 4169c4a7965SKim Phillips case DESC_HDR_SEL0_RNG: 4179c4a7965SKim Phillips dev_err(dev, "RNGUISR 0x%08x_%08x\n", 4189c4a7965SKim Phillips in_be32(priv->reg + TALITOS_RNGUISR), 4199c4a7965SKim Phillips in_be32(priv->reg + TALITOS_RNGUISR_LO)); 4209c4a7965SKim Phillips break; 4219c4a7965SKim Phillips case DESC_HDR_SEL0_PKEU: 4229c4a7965SKim Phillips dev_err(dev, "PKEUISR 0x%08x_%08x\n", 4239c4a7965SKim Phillips in_be32(priv->reg + TALITOS_PKEUISR), 4249c4a7965SKim Phillips in_be32(priv->reg + TALITOS_PKEUISR_LO)); 4259c4a7965SKim Phillips break; 4269c4a7965SKim Phillips case DESC_HDR_SEL0_AESU: 4279c4a7965SKim Phillips dev_err(dev, "AESUISR 0x%08x_%08x\n", 4289c4a7965SKim Phillips in_be32(priv->reg + TALITOS_AESUISR), 4299c4a7965SKim Phillips in_be32(priv->reg + TALITOS_AESUISR_LO)); 4309c4a7965SKim Phillips break; 4319c4a7965SKim Phillips case DESC_HDR_SEL0_CRCU: 4329c4a7965SKim Phillips dev_err(dev, "CRCUISR 0x%08x_%08x\n", 4339c4a7965SKim Phillips in_be32(priv->reg + TALITOS_CRCUISR), 4349c4a7965SKim Phillips in_be32(priv->reg + TALITOS_CRCUISR_LO)); 4359c4a7965SKim Phillips break; 4369c4a7965SKim Phillips case DESC_HDR_SEL0_KEU: 4379c4a7965SKim Phillips dev_err(dev, "KEUISR 0x%08x_%08x\n", 4389c4a7965SKim Phillips in_be32(priv->reg + TALITOS_KEUISR), 4399c4a7965SKim Phillips in_be32(priv->reg + TALITOS_KEUISR_LO)); 4409c4a7965SKim Phillips break; 4419c4a7965SKim Phillips } 4429c4a7965SKim Phillips 4439c4a7965SKim Phillips switch (desc->hdr & DESC_HDR_SEL1_MASK) { 4449c4a7965SKim Phillips case DESC_HDR_SEL1_MDEUA: 4459c4a7965SKim Phillips case DESC_HDR_SEL1_MDEUB: 4469c4a7965SKim Phillips dev_err(dev, "MDEUISR 0x%08x_%08x\n", 4479c4a7965SKim Phillips in_be32(priv->reg + TALITOS_MDEUISR), 4489c4a7965SKim Phillips in_be32(priv->reg + TALITOS_MDEUISR_LO)); 4499c4a7965SKim Phillips break; 4509c4a7965SKim Phillips case DESC_HDR_SEL1_CRCU: 4519c4a7965SKim Phillips dev_err(dev, "CRCUISR 0x%08x_%08x\n", 4529c4a7965SKim Phillips in_be32(priv->reg + TALITOS_CRCUISR), 4539c4a7965SKim Phillips in_be32(priv->reg + TALITOS_CRCUISR_LO)); 4549c4a7965SKim Phillips break; 4559c4a7965SKim Phillips } 4569c4a7965SKim Phillips 4579c4a7965SKim Phillips for (i = 0; i < 8; i++) 4589c4a7965SKim Phillips dev_err(dev, "DESCBUF 0x%08x_%08x\n", 4599c4a7965SKim Phillips in_be32(priv->reg + TALITOS_DESCBUF(ch) + 8*i), 4609c4a7965SKim Phillips in_be32(priv->reg + TALITOS_DESCBUF_LO(ch) + 8*i)); 4619c4a7965SKim Phillips } 4629c4a7965SKim Phillips 4639c4a7965SKim Phillips /* 4649c4a7965SKim Phillips * recover from error interrupts 4659c4a7965SKim Phillips */ 4669c4a7965SKim Phillips static void talitos_error(unsigned long data) 4679c4a7965SKim Phillips { 4689c4a7965SKim Phillips struct device *dev = (struct device *)data; 4699c4a7965SKim Phillips struct talitos_private *priv = dev_get_drvdata(dev); 4709c4a7965SKim Phillips unsigned int timeout = TALITOS_TIMEOUT; 4719c4a7965SKim Phillips int ch, error, reset_dev = 0, reset_ch = 0; 4729c4a7965SKim Phillips u32 isr, isr_lo, v, v_lo; 4739c4a7965SKim Phillips 4749c4a7965SKim Phillips isr = in_be32(priv->reg + TALITOS_ISR); 4759c4a7965SKim Phillips isr_lo = in_be32(priv->reg + TALITOS_ISR_LO); 4769c4a7965SKim Phillips 4779c4a7965SKim Phillips for (ch = 0; ch < priv->num_channels; ch++) { 4789c4a7965SKim Phillips /* skip channels without errors */ 4799c4a7965SKim Phillips if (!(isr & (1 << (ch * 2 + 1)))) 4809c4a7965SKim Phillips continue; 4819c4a7965SKim Phillips 4829c4a7965SKim Phillips error = -EINVAL; 4839c4a7965SKim Phillips 4849c4a7965SKim Phillips v = in_be32(priv->reg + TALITOS_CCPSR(ch)); 4859c4a7965SKim Phillips v_lo = in_be32(priv->reg + TALITOS_CCPSR_LO(ch)); 4869c4a7965SKim Phillips 4879c4a7965SKim Phillips if (v_lo & TALITOS_CCPSR_LO_DOF) { 4889c4a7965SKim Phillips dev_err(dev, "double fetch fifo overflow error\n"); 4899c4a7965SKim Phillips error = -EAGAIN; 4909c4a7965SKim Phillips reset_ch = 1; 4919c4a7965SKim Phillips } 4929c4a7965SKim Phillips if (v_lo & TALITOS_CCPSR_LO_SOF) { 4939c4a7965SKim Phillips /* h/w dropped descriptor */ 4949c4a7965SKim Phillips dev_err(dev, "single fetch fifo overflow error\n"); 4959c4a7965SKim Phillips error = -EAGAIN; 4969c4a7965SKim Phillips } 4979c4a7965SKim Phillips if (v_lo & TALITOS_CCPSR_LO_MDTE) 4989c4a7965SKim Phillips dev_err(dev, "master data transfer error\n"); 4999c4a7965SKim Phillips if (v_lo & TALITOS_CCPSR_LO_SGDLZ) 5009c4a7965SKim Phillips dev_err(dev, "s/g data length zero error\n"); 5019c4a7965SKim Phillips if (v_lo & TALITOS_CCPSR_LO_FPZ) 5029c4a7965SKim Phillips dev_err(dev, "fetch pointer zero error\n"); 5039c4a7965SKim Phillips if (v_lo & TALITOS_CCPSR_LO_IDH) 5049c4a7965SKim Phillips dev_err(dev, "illegal descriptor header error\n"); 5059c4a7965SKim Phillips if (v_lo & TALITOS_CCPSR_LO_IEU) 5069c4a7965SKim Phillips dev_err(dev, "invalid execution unit error\n"); 5079c4a7965SKim Phillips if (v_lo & TALITOS_CCPSR_LO_EU) 5089c4a7965SKim Phillips report_eu_error(dev, ch, current_desc(dev, ch)); 5099c4a7965SKim Phillips if (v_lo & TALITOS_CCPSR_LO_GB) 5109c4a7965SKim Phillips dev_err(dev, "gather boundary error\n"); 5119c4a7965SKim Phillips if (v_lo & TALITOS_CCPSR_LO_GRL) 5129c4a7965SKim Phillips dev_err(dev, "gather return/length error\n"); 5139c4a7965SKim Phillips if (v_lo & TALITOS_CCPSR_LO_SB) 5149c4a7965SKim Phillips dev_err(dev, "scatter boundary error\n"); 5159c4a7965SKim Phillips if (v_lo & TALITOS_CCPSR_LO_SRL) 5169c4a7965SKim Phillips dev_err(dev, "scatter return/length error\n"); 5179c4a7965SKim Phillips 5189c4a7965SKim Phillips flush_channel(dev, ch, error, reset_ch); 5199c4a7965SKim Phillips 5209c4a7965SKim Phillips if (reset_ch) { 5219c4a7965SKim Phillips reset_channel(dev, ch); 5229c4a7965SKim Phillips } else { 5239c4a7965SKim Phillips setbits32(priv->reg + TALITOS_CCCR(ch), 5249c4a7965SKim Phillips TALITOS_CCCR_CONT); 5259c4a7965SKim Phillips setbits32(priv->reg + TALITOS_CCCR_LO(ch), 0); 5269c4a7965SKim Phillips while ((in_be32(priv->reg + TALITOS_CCCR(ch)) & 5279c4a7965SKim Phillips TALITOS_CCCR_CONT) && --timeout) 5289c4a7965SKim Phillips cpu_relax(); 5299c4a7965SKim Phillips if (timeout == 0) { 5309c4a7965SKim Phillips dev_err(dev, "failed to restart channel %d\n", 5319c4a7965SKim Phillips ch); 5329c4a7965SKim Phillips reset_dev = 1; 5339c4a7965SKim Phillips } 5349c4a7965SKim Phillips } 5359c4a7965SKim Phillips } 5369c4a7965SKim Phillips if (reset_dev || isr & ~TALITOS_ISR_CHERR || isr_lo) { 5379c4a7965SKim Phillips dev_err(dev, "done overflow, internal time out, or rngu error: " 5389c4a7965SKim Phillips "ISR 0x%08x_%08x\n", isr, isr_lo); 5399c4a7965SKim Phillips 5409c4a7965SKim Phillips /* purge request queues */ 5419c4a7965SKim Phillips for (ch = 0; ch < priv->num_channels; ch++) 5429c4a7965SKim Phillips flush_channel(dev, ch, -EIO, 1); 5439c4a7965SKim Phillips 5449c4a7965SKim Phillips /* reset and reinitialize the device */ 5459c4a7965SKim Phillips init_device(dev); 5469c4a7965SKim Phillips } 5479c4a7965SKim Phillips } 5489c4a7965SKim Phillips 5499c4a7965SKim Phillips static irqreturn_t talitos_interrupt(int irq, void *data) 5509c4a7965SKim Phillips { 5519c4a7965SKim Phillips struct device *dev = data; 5529c4a7965SKim Phillips struct talitos_private *priv = dev_get_drvdata(dev); 5539c4a7965SKim Phillips u32 isr, isr_lo; 5549c4a7965SKim Phillips 5559c4a7965SKim Phillips isr = in_be32(priv->reg + TALITOS_ISR); 5569c4a7965SKim Phillips isr_lo = in_be32(priv->reg + TALITOS_ISR_LO); 5579c4a7965SKim Phillips 5589c4a7965SKim Phillips /* ack */ 5599c4a7965SKim Phillips out_be32(priv->reg + TALITOS_ICR, isr); 5609c4a7965SKim Phillips out_be32(priv->reg + TALITOS_ICR_LO, isr_lo); 5619c4a7965SKim Phillips 5629c4a7965SKim Phillips if (unlikely((isr & ~TALITOS_ISR_CHDONE) || isr_lo)) 5639c4a7965SKim Phillips talitos_error((unsigned long)data); 5649c4a7965SKim Phillips else 5659c4a7965SKim Phillips if (likely(isr & TALITOS_ISR_CHDONE)) 5669c4a7965SKim Phillips tasklet_schedule(&priv->done_task); 5679c4a7965SKim Phillips 5689c4a7965SKim Phillips return (isr || isr_lo) ? IRQ_HANDLED : IRQ_NONE; 5699c4a7965SKim Phillips } 5709c4a7965SKim Phillips 5719c4a7965SKim Phillips /* 5729c4a7965SKim Phillips * hwrng 5739c4a7965SKim Phillips */ 5749c4a7965SKim Phillips static int talitos_rng_data_present(struct hwrng *rng, int wait) 5759c4a7965SKim Phillips { 5769c4a7965SKim Phillips struct device *dev = (struct device *)rng->priv; 5779c4a7965SKim Phillips struct talitos_private *priv = dev_get_drvdata(dev); 5789c4a7965SKim Phillips u32 ofl; 5799c4a7965SKim Phillips int i; 5809c4a7965SKim Phillips 5819c4a7965SKim Phillips for (i = 0; i < 20; i++) { 5829c4a7965SKim Phillips ofl = in_be32(priv->reg + TALITOS_RNGUSR_LO) & 5839c4a7965SKim Phillips TALITOS_RNGUSR_LO_OFL; 5849c4a7965SKim Phillips if (ofl || !wait) 5859c4a7965SKim Phillips break; 5869c4a7965SKim Phillips udelay(10); 5879c4a7965SKim Phillips } 5889c4a7965SKim Phillips 5899c4a7965SKim Phillips return !!ofl; 5909c4a7965SKim Phillips } 5919c4a7965SKim Phillips 5929c4a7965SKim Phillips static int talitos_rng_data_read(struct hwrng *rng, u32 *data) 5939c4a7965SKim Phillips { 5949c4a7965SKim Phillips struct device *dev = (struct device *)rng->priv; 5959c4a7965SKim Phillips struct talitos_private *priv = dev_get_drvdata(dev); 5969c4a7965SKim Phillips 5979c4a7965SKim Phillips /* rng fifo requires 64-bit accesses */ 5989c4a7965SKim Phillips *data = in_be32(priv->reg + TALITOS_RNGU_FIFO); 5999c4a7965SKim Phillips *data = in_be32(priv->reg + TALITOS_RNGU_FIFO_LO); 6009c4a7965SKim Phillips 6019c4a7965SKim Phillips return sizeof(u32); 6029c4a7965SKim Phillips } 6039c4a7965SKim Phillips 6049c4a7965SKim Phillips static int talitos_rng_init(struct hwrng *rng) 6059c4a7965SKim Phillips { 6069c4a7965SKim Phillips struct device *dev = (struct device *)rng->priv; 6079c4a7965SKim Phillips struct talitos_private *priv = dev_get_drvdata(dev); 6089c4a7965SKim Phillips unsigned int timeout = TALITOS_TIMEOUT; 6099c4a7965SKim Phillips 6109c4a7965SKim Phillips setbits32(priv->reg + TALITOS_RNGURCR_LO, TALITOS_RNGURCR_LO_SR); 6119c4a7965SKim Phillips while (!(in_be32(priv->reg + TALITOS_RNGUSR_LO) & TALITOS_RNGUSR_LO_RD) 6129c4a7965SKim Phillips && --timeout) 6139c4a7965SKim Phillips cpu_relax(); 6149c4a7965SKim Phillips if (timeout == 0) { 6159c4a7965SKim Phillips dev_err(dev, "failed to reset rng hw\n"); 6169c4a7965SKim Phillips return -ENODEV; 6179c4a7965SKim Phillips } 6189c4a7965SKim Phillips 6199c4a7965SKim Phillips /* start generating */ 6209c4a7965SKim Phillips setbits32(priv->reg + TALITOS_RNGUDSR_LO, 0); 6219c4a7965SKim Phillips 6229c4a7965SKim Phillips return 0; 6239c4a7965SKim Phillips } 6249c4a7965SKim Phillips 6259c4a7965SKim Phillips static int talitos_register_rng(struct device *dev) 6269c4a7965SKim Phillips { 6279c4a7965SKim Phillips struct talitos_private *priv = dev_get_drvdata(dev); 6289c4a7965SKim Phillips 6299c4a7965SKim Phillips priv->rng.name = dev_driver_string(dev), 6309c4a7965SKim Phillips priv->rng.init = talitos_rng_init, 6319c4a7965SKim Phillips priv->rng.data_present = talitos_rng_data_present, 6329c4a7965SKim Phillips priv->rng.data_read = talitos_rng_data_read, 6339c4a7965SKim Phillips priv->rng.priv = (unsigned long)dev; 6349c4a7965SKim Phillips 6359c4a7965SKim Phillips return hwrng_register(&priv->rng); 6369c4a7965SKim Phillips } 6379c4a7965SKim Phillips 6389c4a7965SKim Phillips static void talitos_unregister_rng(struct device *dev) 6399c4a7965SKim Phillips { 6409c4a7965SKim Phillips struct talitos_private *priv = dev_get_drvdata(dev); 6419c4a7965SKim Phillips 6429c4a7965SKim Phillips hwrng_unregister(&priv->rng); 6439c4a7965SKim Phillips } 6449c4a7965SKim Phillips 6459c4a7965SKim Phillips /* 6469c4a7965SKim Phillips * crypto alg 6479c4a7965SKim Phillips */ 6489c4a7965SKim Phillips #define TALITOS_CRA_PRIORITY 3000 6499c4a7965SKim Phillips #define TALITOS_MAX_KEY_SIZE 64 6503952f17eSLee Nipper #define TALITOS_MAX_IV_LENGTH 16 /* max of AES_BLOCK_SIZE, DES3_EDE_BLOCK_SIZE */ 65170bcaca7SLee Nipper 6523952f17eSLee Nipper #define MD5_DIGEST_SIZE 16 6539c4a7965SKim Phillips 6549c4a7965SKim Phillips struct talitos_ctx { 6559c4a7965SKim Phillips struct device *dev; 6569c4a7965SKim Phillips __be32 desc_hdr_template; 6579c4a7965SKim Phillips u8 key[TALITOS_MAX_KEY_SIZE]; 65870bcaca7SLee Nipper u8 iv[TALITOS_MAX_IV_LENGTH]; 6599c4a7965SKim Phillips unsigned int keylen; 6609c4a7965SKim Phillips unsigned int enckeylen; 6619c4a7965SKim Phillips unsigned int authkeylen; 6629c4a7965SKim Phillips unsigned int authsize; 6639c4a7965SKim Phillips }; 6649c4a7965SKim Phillips 66570bcaca7SLee Nipper static int aead_authenc_setauthsize(struct crypto_aead *authenc, 6669c4a7965SKim Phillips unsigned int authsize) 6679c4a7965SKim Phillips { 6689c4a7965SKim Phillips struct talitos_ctx *ctx = crypto_aead_ctx(authenc); 6699c4a7965SKim Phillips 6709c4a7965SKim Phillips ctx->authsize = authsize; 6719c4a7965SKim Phillips 6729c4a7965SKim Phillips return 0; 6739c4a7965SKim Phillips } 6749c4a7965SKim Phillips 67570bcaca7SLee Nipper static int aead_authenc_setkey(struct crypto_aead *authenc, 6769c4a7965SKim Phillips const u8 *key, unsigned int keylen) 6779c4a7965SKim Phillips { 6789c4a7965SKim Phillips struct talitos_ctx *ctx = crypto_aead_ctx(authenc); 6799c4a7965SKim Phillips struct rtattr *rta = (void *)key; 6809c4a7965SKim Phillips struct crypto_authenc_key_param *param; 6819c4a7965SKim Phillips unsigned int authkeylen; 6829c4a7965SKim Phillips unsigned int enckeylen; 6839c4a7965SKim Phillips 6849c4a7965SKim Phillips if (!RTA_OK(rta, keylen)) 6859c4a7965SKim Phillips goto badkey; 6869c4a7965SKim Phillips 6879c4a7965SKim Phillips if (rta->rta_type != CRYPTO_AUTHENC_KEYA_PARAM) 6889c4a7965SKim Phillips goto badkey; 6899c4a7965SKim Phillips 6909c4a7965SKim Phillips if (RTA_PAYLOAD(rta) < sizeof(*param)) 6919c4a7965SKim Phillips goto badkey; 6929c4a7965SKim Phillips 6939c4a7965SKim Phillips param = RTA_DATA(rta); 6949c4a7965SKim Phillips enckeylen = be32_to_cpu(param->enckeylen); 6959c4a7965SKim Phillips 6969c4a7965SKim Phillips key += RTA_ALIGN(rta->rta_len); 6979c4a7965SKim Phillips keylen -= RTA_ALIGN(rta->rta_len); 6989c4a7965SKim Phillips 6999c4a7965SKim Phillips if (keylen < enckeylen) 7009c4a7965SKim Phillips goto badkey; 7019c4a7965SKim Phillips 7029c4a7965SKim Phillips authkeylen = keylen - enckeylen; 7039c4a7965SKim Phillips 7049c4a7965SKim Phillips if (keylen > TALITOS_MAX_KEY_SIZE) 7059c4a7965SKim Phillips goto badkey; 7069c4a7965SKim Phillips 7079c4a7965SKim Phillips memcpy(&ctx->key, key, keylen); 7089c4a7965SKim Phillips 7099c4a7965SKim Phillips ctx->keylen = keylen; 7109c4a7965SKim Phillips ctx->enckeylen = enckeylen; 7119c4a7965SKim Phillips ctx->authkeylen = authkeylen; 7129c4a7965SKim Phillips 7139c4a7965SKim Phillips return 0; 7149c4a7965SKim Phillips 7159c4a7965SKim Phillips badkey: 7169c4a7965SKim Phillips crypto_aead_set_flags(authenc, CRYPTO_TFM_RES_BAD_KEY_LEN); 7179c4a7965SKim Phillips return -EINVAL; 7189c4a7965SKim Phillips } 7199c4a7965SKim Phillips 7209c4a7965SKim Phillips /* 7219c4a7965SKim Phillips * ipsec_esp_edesc - s/w-extended ipsec_esp descriptor 7229c4a7965SKim Phillips * @src_nents: number of segments in input scatterlist 7239c4a7965SKim Phillips * @dst_nents: number of segments in output scatterlist 7249c4a7965SKim Phillips * @dma_len: length of dma mapped link_tbl space 7259c4a7965SKim Phillips * @dma_link_tbl: bus physical address of link_tbl 7269c4a7965SKim Phillips * @desc: h/w descriptor 7279c4a7965SKim Phillips * @link_tbl: input and output h/w link tables (if {src,dst}_nents > 1) 7289c4a7965SKim Phillips * 7299c4a7965SKim Phillips * if decrypting (with authcheck), or either one of src_nents or dst_nents 7309c4a7965SKim Phillips * is greater than 1, an integrity check value is concatenated to the end 7319c4a7965SKim Phillips * of link_tbl data 7329c4a7965SKim Phillips */ 7339c4a7965SKim Phillips struct ipsec_esp_edesc { 7349c4a7965SKim Phillips int src_nents; 7359c4a7965SKim Phillips int dst_nents; 7369c4a7965SKim Phillips int dma_len; 7379c4a7965SKim Phillips dma_addr_t dma_link_tbl; 7389c4a7965SKim Phillips struct talitos_desc desc; 7399c4a7965SKim Phillips struct talitos_ptr link_tbl[0]; 7409c4a7965SKim Phillips }; 7419c4a7965SKim Phillips 7429c4a7965SKim Phillips static void ipsec_esp_unmap(struct device *dev, 7439c4a7965SKim Phillips struct ipsec_esp_edesc *edesc, 7449c4a7965SKim Phillips struct aead_request *areq) 7459c4a7965SKim Phillips { 7469c4a7965SKim Phillips unmap_single_talitos_ptr(dev, &edesc->desc.ptr[6], DMA_FROM_DEVICE); 7479c4a7965SKim Phillips unmap_single_talitos_ptr(dev, &edesc->desc.ptr[3], DMA_TO_DEVICE); 7489c4a7965SKim Phillips unmap_single_talitos_ptr(dev, &edesc->desc.ptr[2], DMA_TO_DEVICE); 7499c4a7965SKim Phillips unmap_single_talitos_ptr(dev, &edesc->desc.ptr[0], DMA_TO_DEVICE); 7509c4a7965SKim Phillips 7519c4a7965SKim Phillips dma_unmap_sg(dev, areq->assoc, 1, DMA_TO_DEVICE); 7529c4a7965SKim Phillips 7539c4a7965SKim Phillips if (areq->src != areq->dst) { 7549c4a7965SKim Phillips dma_unmap_sg(dev, areq->src, edesc->src_nents ? : 1, 7559c4a7965SKim Phillips DMA_TO_DEVICE); 7569c4a7965SKim Phillips dma_unmap_sg(dev, areq->dst, edesc->dst_nents ? : 1, 7579c4a7965SKim Phillips DMA_FROM_DEVICE); 7589c4a7965SKim Phillips } else { 7599c4a7965SKim Phillips dma_unmap_sg(dev, areq->src, edesc->src_nents ? : 1, 7609c4a7965SKim Phillips DMA_BIDIRECTIONAL); 7619c4a7965SKim Phillips } 7629c4a7965SKim Phillips 7639c4a7965SKim Phillips if (edesc->dma_len) 7649c4a7965SKim Phillips dma_unmap_single(dev, edesc->dma_link_tbl, edesc->dma_len, 7659c4a7965SKim Phillips DMA_BIDIRECTIONAL); 7669c4a7965SKim Phillips } 7679c4a7965SKim Phillips 7689c4a7965SKim Phillips /* 7699c4a7965SKim Phillips * ipsec_esp descriptor callbacks 7709c4a7965SKim Phillips */ 7719c4a7965SKim Phillips static void ipsec_esp_encrypt_done(struct device *dev, 7729c4a7965SKim Phillips struct talitos_desc *desc, void *context, 7739c4a7965SKim Phillips int err) 7749c4a7965SKim Phillips { 7759c4a7965SKim Phillips struct aead_request *areq = context; 7769c4a7965SKim Phillips struct ipsec_esp_edesc *edesc = 7779c4a7965SKim Phillips container_of(desc, struct ipsec_esp_edesc, desc); 7789c4a7965SKim Phillips struct crypto_aead *authenc = crypto_aead_reqtfm(areq); 7799c4a7965SKim Phillips struct talitos_ctx *ctx = crypto_aead_ctx(authenc); 7809c4a7965SKim Phillips struct scatterlist *sg; 7819c4a7965SKim Phillips void *icvdata; 7829c4a7965SKim Phillips 7839c4a7965SKim Phillips ipsec_esp_unmap(dev, edesc, areq); 7849c4a7965SKim Phillips 7859c4a7965SKim Phillips /* copy the generated ICV to dst */ 7869c4a7965SKim Phillips if (edesc->dma_len) { 7879c4a7965SKim Phillips icvdata = &edesc->link_tbl[edesc->src_nents + 7889c4a7965SKim Phillips edesc->dst_nents + 1]; 7899c4a7965SKim Phillips sg = sg_last(areq->dst, edesc->dst_nents); 7909c4a7965SKim Phillips memcpy((char *)sg_virt(sg) + sg->length - ctx->authsize, 7919c4a7965SKim Phillips icvdata, ctx->authsize); 7929c4a7965SKim Phillips } 7939c4a7965SKim Phillips 7949c4a7965SKim Phillips kfree(edesc); 7959c4a7965SKim Phillips 7969c4a7965SKim Phillips aead_request_complete(areq, err); 7979c4a7965SKim Phillips } 7989c4a7965SKim Phillips 7999c4a7965SKim Phillips static void ipsec_esp_decrypt_done(struct device *dev, 8009c4a7965SKim Phillips struct talitos_desc *desc, void *context, 8019c4a7965SKim Phillips int err) 8029c4a7965SKim Phillips { 8039c4a7965SKim Phillips struct aead_request *req = context; 8049c4a7965SKim Phillips struct ipsec_esp_edesc *edesc = 8059c4a7965SKim Phillips container_of(desc, struct ipsec_esp_edesc, desc); 8069c4a7965SKim Phillips struct crypto_aead *authenc = crypto_aead_reqtfm(req); 8079c4a7965SKim Phillips struct talitos_ctx *ctx = crypto_aead_ctx(authenc); 8089c4a7965SKim Phillips struct scatterlist *sg; 8099c4a7965SKim Phillips void *icvdata; 8109c4a7965SKim Phillips 8119c4a7965SKim Phillips ipsec_esp_unmap(dev, edesc, req); 8129c4a7965SKim Phillips 8139c4a7965SKim Phillips if (!err) { 8149c4a7965SKim Phillips /* auth check */ 8159c4a7965SKim Phillips if (edesc->dma_len) 8169c4a7965SKim Phillips icvdata = &edesc->link_tbl[edesc->src_nents + 8179c4a7965SKim Phillips edesc->dst_nents + 1]; 8189c4a7965SKim Phillips else 8199c4a7965SKim Phillips icvdata = &edesc->link_tbl[0]; 8209c4a7965SKim Phillips 8219c4a7965SKim Phillips sg = sg_last(req->dst, edesc->dst_nents ? : 1); 8229c4a7965SKim Phillips err = memcmp(icvdata, (char *)sg_virt(sg) + sg->length - 8239c4a7965SKim Phillips ctx->authsize, ctx->authsize) ? -EBADMSG : 0; 8249c4a7965SKim Phillips } 8259c4a7965SKim Phillips 8269c4a7965SKim Phillips kfree(edesc); 8279c4a7965SKim Phillips 8289c4a7965SKim Phillips aead_request_complete(req, err); 8299c4a7965SKim Phillips } 8309c4a7965SKim Phillips 8319c4a7965SKim Phillips /* 8329c4a7965SKim Phillips * convert scatterlist to SEC h/w link table format 8339c4a7965SKim Phillips * stop at cryptlen bytes 8349c4a7965SKim Phillips */ 83570bcaca7SLee Nipper static int sg_to_link_tbl(struct scatterlist *sg, int sg_count, 8369c4a7965SKim Phillips int cryptlen, struct talitos_ptr *link_tbl_ptr) 8379c4a7965SKim Phillips { 83870bcaca7SLee Nipper int n_sg = sg_count; 83970bcaca7SLee Nipper 84070bcaca7SLee Nipper while (n_sg--) { 8419c4a7965SKim Phillips link_tbl_ptr->ptr = cpu_to_be32(sg_dma_address(sg)); 8429c4a7965SKim Phillips link_tbl_ptr->len = cpu_to_be16(sg_dma_len(sg)); 8439c4a7965SKim Phillips link_tbl_ptr->j_extent = 0; 8449c4a7965SKim Phillips link_tbl_ptr++; 8459c4a7965SKim Phillips cryptlen -= sg_dma_len(sg); 8469c4a7965SKim Phillips sg = sg_next(sg); 8479c4a7965SKim Phillips } 8489c4a7965SKim Phillips 84970bcaca7SLee Nipper /* adjust (decrease) last one (or two) entry's len to cryptlen */ 8509c4a7965SKim Phillips link_tbl_ptr--; 85170bcaca7SLee Nipper while (link_tbl_ptr->len <= (-cryptlen)) { 85270bcaca7SLee Nipper /* Empty this entry, and move to previous one */ 85370bcaca7SLee Nipper cryptlen += be16_to_cpu(link_tbl_ptr->len); 85470bcaca7SLee Nipper link_tbl_ptr->len = 0; 85570bcaca7SLee Nipper sg_count--; 85670bcaca7SLee Nipper link_tbl_ptr--; 85770bcaca7SLee Nipper } 8589c4a7965SKim Phillips link_tbl_ptr->len = cpu_to_be16(be16_to_cpu(link_tbl_ptr->len) 8599c4a7965SKim Phillips + cryptlen); 8609c4a7965SKim Phillips 8619c4a7965SKim Phillips /* tag end of link table */ 8629c4a7965SKim Phillips link_tbl_ptr->j_extent = DESC_PTR_LNKTBL_RETURN; 86370bcaca7SLee Nipper 86470bcaca7SLee Nipper return sg_count; 8659c4a7965SKim Phillips } 8669c4a7965SKim Phillips 8679c4a7965SKim Phillips /* 8689c4a7965SKim Phillips * fill in and submit ipsec_esp descriptor 8699c4a7965SKim Phillips */ 8709c4a7965SKim Phillips static int ipsec_esp(struct ipsec_esp_edesc *edesc, struct aead_request *areq, 8719c4a7965SKim Phillips u8 *giv, u64 seq, 8729c4a7965SKim Phillips void (*callback) (struct device *dev, 8739c4a7965SKim Phillips struct talitos_desc *desc, 8749c4a7965SKim Phillips void *context, int error)) 8759c4a7965SKim Phillips { 8769c4a7965SKim Phillips struct crypto_aead *aead = crypto_aead_reqtfm(areq); 8779c4a7965SKim Phillips struct talitos_ctx *ctx = crypto_aead_ctx(aead); 8789c4a7965SKim Phillips struct device *dev = ctx->dev; 8799c4a7965SKim Phillips struct talitos_desc *desc = &edesc->desc; 8809c4a7965SKim Phillips unsigned int cryptlen = areq->cryptlen; 8819c4a7965SKim Phillips unsigned int authsize = ctx->authsize; 8829c4a7965SKim Phillips unsigned int ivsize; 8839c4a7965SKim Phillips int sg_count; 8849c4a7965SKim Phillips 8859c4a7965SKim Phillips /* hmac key */ 8869c4a7965SKim Phillips map_single_talitos_ptr(dev, &desc->ptr[0], ctx->authkeylen, &ctx->key, 8879c4a7965SKim Phillips 0, DMA_TO_DEVICE); 8889c4a7965SKim Phillips /* hmac data */ 8899c4a7965SKim Phillips map_single_talitos_ptr(dev, &desc->ptr[1], sg_virt(areq->src) - 8909c4a7965SKim Phillips sg_virt(areq->assoc), sg_virt(areq->assoc), 0, 8919c4a7965SKim Phillips DMA_TO_DEVICE); 8929c4a7965SKim Phillips /* cipher iv */ 8939c4a7965SKim Phillips ivsize = crypto_aead_ivsize(aead); 8949c4a7965SKim Phillips map_single_talitos_ptr(dev, &desc->ptr[2], ivsize, giv ?: areq->iv, 0, 8959c4a7965SKim Phillips DMA_TO_DEVICE); 8969c4a7965SKim Phillips 8979c4a7965SKim Phillips /* cipher key */ 8989c4a7965SKim Phillips map_single_talitos_ptr(dev, &desc->ptr[3], ctx->enckeylen, 8999c4a7965SKim Phillips (char *)&ctx->key + ctx->authkeylen, 0, 9009c4a7965SKim Phillips DMA_TO_DEVICE); 9019c4a7965SKim Phillips 9029c4a7965SKim Phillips /* 9039c4a7965SKim Phillips * cipher in 9049c4a7965SKim Phillips * map and adjust cipher len to aead request cryptlen. 9059c4a7965SKim Phillips * extent is bytes of HMAC postpended to ciphertext, 9069c4a7965SKim Phillips * typically 12 for ipsec 9079c4a7965SKim Phillips */ 9089c4a7965SKim Phillips desc->ptr[4].len = cpu_to_be16(cryptlen); 9099c4a7965SKim Phillips desc->ptr[4].j_extent = authsize; 9109c4a7965SKim Phillips 9119c4a7965SKim Phillips if (areq->src == areq->dst) 9129c4a7965SKim Phillips sg_count = dma_map_sg(dev, areq->src, edesc->src_nents ? : 1, 9139c4a7965SKim Phillips DMA_BIDIRECTIONAL); 9149c4a7965SKim Phillips else 9159c4a7965SKim Phillips sg_count = dma_map_sg(dev, areq->src, edesc->src_nents ? : 1, 9169c4a7965SKim Phillips DMA_TO_DEVICE); 9179c4a7965SKim Phillips 9189c4a7965SKim Phillips if (sg_count == 1) { 9199c4a7965SKim Phillips desc->ptr[4].ptr = cpu_to_be32(sg_dma_address(areq->src)); 9209c4a7965SKim Phillips } else { 92170bcaca7SLee Nipper sg_count = sg_to_link_tbl(areq->src, sg_count, cryptlen, 9229c4a7965SKim Phillips &edesc->link_tbl[0]); 92370bcaca7SLee Nipper if (sg_count > 1) { 9249c4a7965SKim Phillips desc->ptr[4].j_extent |= DESC_PTR_LNKTBL_JUMP; 9259c4a7965SKim Phillips desc->ptr[4].ptr = cpu_to_be32(edesc->dma_link_tbl); 9269c4a7965SKim Phillips dma_sync_single_for_device(ctx->dev, edesc->dma_link_tbl, 9279c4a7965SKim Phillips edesc->dma_len, DMA_BIDIRECTIONAL); 92870bcaca7SLee Nipper } else { 92970bcaca7SLee Nipper /* Only one segment now, so no link tbl needed */ 93070bcaca7SLee Nipper desc->ptr[4].ptr = cpu_to_be32(sg_dma_address(areq->src)); 93170bcaca7SLee Nipper } 9329c4a7965SKim Phillips } 9339c4a7965SKim Phillips 9349c4a7965SKim Phillips /* cipher out */ 9359c4a7965SKim Phillips desc->ptr[5].len = cpu_to_be16(cryptlen); 9369c4a7965SKim Phillips desc->ptr[5].j_extent = authsize; 9379c4a7965SKim Phillips 9389c4a7965SKim Phillips if (areq->src != areq->dst) { 9399c4a7965SKim Phillips sg_count = dma_map_sg(dev, areq->dst, edesc->dst_nents ? : 1, 9409c4a7965SKim Phillips DMA_FROM_DEVICE); 9419c4a7965SKim Phillips } 9429c4a7965SKim Phillips 9439c4a7965SKim Phillips if (sg_count == 1) { 9449c4a7965SKim Phillips desc->ptr[5].ptr = cpu_to_be32(sg_dma_address(areq->dst)); 9459c4a7965SKim Phillips } else { 9469c4a7965SKim Phillips struct talitos_ptr *link_tbl_ptr = 9479c4a7965SKim Phillips &edesc->link_tbl[edesc->src_nents]; 9489c4a7965SKim Phillips struct scatterlist *sg; 9499c4a7965SKim Phillips 9509c4a7965SKim Phillips desc->ptr[5].ptr = cpu_to_be32((struct talitos_ptr *) 9519c4a7965SKim Phillips edesc->dma_link_tbl + 9529c4a7965SKim Phillips edesc->src_nents); 9539c4a7965SKim Phillips if (areq->src == areq->dst) { 9549c4a7965SKim Phillips memcpy(link_tbl_ptr, &edesc->link_tbl[0], 9559c4a7965SKim Phillips edesc->src_nents * sizeof(struct talitos_ptr)); 9569c4a7965SKim Phillips } else { 95770bcaca7SLee Nipper sg_count = sg_to_link_tbl(areq->dst, sg_count, cryptlen, 9589c4a7965SKim Phillips link_tbl_ptr); 9599c4a7965SKim Phillips } 9609c4a7965SKim Phillips link_tbl_ptr += sg_count - 1; 9619c4a7965SKim Phillips 9629c4a7965SKim Phillips /* handle case where sg_last contains the ICV exclusively */ 9639c4a7965SKim Phillips sg = sg_last(areq->dst, edesc->dst_nents); 9649c4a7965SKim Phillips if (sg->length == ctx->authsize) 9659c4a7965SKim Phillips link_tbl_ptr--; 9669c4a7965SKim Phillips 9679c4a7965SKim Phillips link_tbl_ptr->j_extent = 0; 9689c4a7965SKim Phillips link_tbl_ptr++; 9699c4a7965SKim Phillips link_tbl_ptr->j_extent = DESC_PTR_LNKTBL_RETURN; 9709c4a7965SKim Phillips link_tbl_ptr->len = cpu_to_be16(authsize); 9719c4a7965SKim Phillips 9729c4a7965SKim Phillips /* icv data follows link tables */ 9739c4a7965SKim Phillips link_tbl_ptr->ptr = cpu_to_be32((struct talitos_ptr *) 9749c4a7965SKim Phillips edesc->dma_link_tbl + 9759c4a7965SKim Phillips edesc->src_nents + 9769c4a7965SKim Phillips edesc->dst_nents + 1); 9779c4a7965SKim Phillips 9789c4a7965SKim Phillips desc->ptr[5].j_extent |= DESC_PTR_LNKTBL_JUMP; 9799c4a7965SKim Phillips dma_sync_single_for_device(ctx->dev, edesc->dma_link_tbl, 9809c4a7965SKim Phillips edesc->dma_len, DMA_BIDIRECTIONAL); 9819c4a7965SKim Phillips } 9829c4a7965SKim Phillips 9839c4a7965SKim Phillips /* iv out */ 9849c4a7965SKim Phillips map_single_talitos_ptr(dev, &desc->ptr[6], ivsize, ctx->iv, 0, 9859c4a7965SKim Phillips DMA_FROM_DEVICE); 9869c4a7965SKim Phillips 9879c4a7965SKim Phillips return talitos_submit(dev, desc, callback, areq); 9889c4a7965SKim Phillips } 9899c4a7965SKim Phillips 9909c4a7965SKim Phillips 9919c4a7965SKim Phillips /* 9929c4a7965SKim Phillips * derive number of elements in scatterlist 9939c4a7965SKim Phillips */ 9949c4a7965SKim Phillips static int sg_count(struct scatterlist *sg_list, int nbytes) 9959c4a7965SKim Phillips { 9969c4a7965SKim Phillips struct scatterlist *sg = sg_list; 9979c4a7965SKim Phillips int sg_nents = 0; 9989c4a7965SKim Phillips 9999c4a7965SKim Phillips while (nbytes) { 10009c4a7965SKim Phillips sg_nents++; 10019c4a7965SKim Phillips nbytes -= sg->length; 10029c4a7965SKim Phillips sg = sg_next(sg); 10039c4a7965SKim Phillips } 10049c4a7965SKim Phillips 10059c4a7965SKim Phillips return sg_nents; 10069c4a7965SKim Phillips } 10079c4a7965SKim Phillips 10089c4a7965SKim Phillips /* 10099c4a7965SKim Phillips * allocate and map the ipsec_esp extended descriptor 10109c4a7965SKim Phillips */ 10119c4a7965SKim Phillips static struct ipsec_esp_edesc *ipsec_esp_edesc_alloc(struct aead_request *areq, 10129c4a7965SKim Phillips int icv_stashing) 10139c4a7965SKim Phillips { 10149c4a7965SKim Phillips struct crypto_aead *authenc = crypto_aead_reqtfm(areq); 10159c4a7965SKim Phillips struct talitos_ctx *ctx = crypto_aead_ctx(authenc); 10169c4a7965SKim Phillips struct ipsec_esp_edesc *edesc; 10179c4a7965SKim Phillips int src_nents, dst_nents, alloc_len, dma_len; 1018*586725f8SKim Phillips gfp_t flags = areq->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP ? GFP_KERNEL : 1019*586725f8SKim Phillips GFP_ATOMIC; 10209c4a7965SKim Phillips 10219c4a7965SKim Phillips if (areq->cryptlen + ctx->authsize > TALITOS_MAX_DATA_LEN) { 10229c4a7965SKim Phillips dev_err(ctx->dev, "cryptlen exceeds h/w max limit\n"); 10239c4a7965SKim Phillips return ERR_PTR(-EINVAL); 10249c4a7965SKim Phillips } 10259c4a7965SKim Phillips 10269c4a7965SKim Phillips src_nents = sg_count(areq->src, areq->cryptlen + ctx->authsize); 10279c4a7965SKim Phillips src_nents = (src_nents == 1) ? 0 : src_nents; 10289c4a7965SKim Phillips 10299c4a7965SKim Phillips if (areq->dst == areq->src) { 10309c4a7965SKim Phillips dst_nents = src_nents; 10319c4a7965SKim Phillips } else { 10329c4a7965SKim Phillips dst_nents = sg_count(areq->dst, areq->cryptlen + ctx->authsize); 1033695ad589SLee Nipper dst_nents = (dst_nents == 1) ? 0 : dst_nents; 10349c4a7965SKim Phillips } 10359c4a7965SKim Phillips 10369c4a7965SKim Phillips /* 10379c4a7965SKim Phillips * allocate space for base edesc plus the link tables, 10389c4a7965SKim Phillips * allowing for a separate entry for the generated ICV (+ 1), 10399c4a7965SKim Phillips * and the ICV data itself 10409c4a7965SKim Phillips */ 10419c4a7965SKim Phillips alloc_len = sizeof(struct ipsec_esp_edesc); 10429c4a7965SKim Phillips if (src_nents || dst_nents) { 10439c4a7965SKim Phillips dma_len = (src_nents + dst_nents + 1) * 10449c4a7965SKim Phillips sizeof(struct talitos_ptr) + ctx->authsize; 10459c4a7965SKim Phillips alloc_len += dma_len; 10469c4a7965SKim Phillips } else { 10479c4a7965SKim Phillips dma_len = 0; 10489c4a7965SKim Phillips alloc_len += icv_stashing ? ctx->authsize : 0; 10499c4a7965SKim Phillips } 10509c4a7965SKim Phillips 1051*586725f8SKim Phillips edesc = kmalloc(alloc_len, GFP_DMA | flags); 10529c4a7965SKim Phillips if (!edesc) { 10539c4a7965SKim Phillips dev_err(ctx->dev, "could not allocate edescriptor\n"); 10549c4a7965SKim Phillips return ERR_PTR(-ENOMEM); 10559c4a7965SKim Phillips } 10569c4a7965SKim Phillips 10579c4a7965SKim Phillips edesc->src_nents = src_nents; 10589c4a7965SKim Phillips edesc->dst_nents = dst_nents; 10599c4a7965SKim Phillips edesc->dma_len = dma_len; 10609c4a7965SKim Phillips edesc->dma_link_tbl = dma_map_single(ctx->dev, &edesc->link_tbl[0], 10619c4a7965SKim Phillips edesc->dma_len, DMA_BIDIRECTIONAL); 10629c4a7965SKim Phillips 10639c4a7965SKim Phillips return edesc; 10649c4a7965SKim Phillips } 10659c4a7965SKim Phillips 106670bcaca7SLee Nipper static int aead_authenc_encrypt(struct aead_request *req) 10679c4a7965SKim Phillips { 10689c4a7965SKim Phillips struct crypto_aead *authenc = crypto_aead_reqtfm(req); 10699c4a7965SKim Phillips struct talitos_ctx *ctx = crypto_aead_ctx(authenc); 10709c4a7965SKim Phillips struct ipsec_esp_edesc *edesc; 10719c4a7965SKim Phillips 10729c4a7965SKim Phillips /* allocate extended descriptor */ 10739c4a7965SKim Phillips edesc = ipsec_esp_edesc_alloc(req, 0); 10749c4a7965SKim Phillips if (IS_ERR(edesc)) 10759c4a7965SKim Phillips return PTR_ERR(edesc); 10769c4a7965SKim Phillips 10779c4a7965SKim Phillips /* set encrypt */ 107870bcaca7SLee Nipper edesc->desc.hdr = ctx->desc_hdr_template | DESC_HDR_MODE0_ENCRYPT; 10799c4a7965SKim Phillips 10809c4a7965SKim Phillips return ipsec_esp(edesc, req, NULL, 0, ipsec_esp_encrypt_done); 10819c4a7965SKim Phillips } 10829c4a7965SKim Phillips 108370bcaca7SLee Nipper static int aead_authenc_decrypt(struct aead_request *req) 10849c4a7965SKim Phillips { 10859c4a7965SKim Phillips struct crypto_aead *authenc = crypto_aead_reqtfm(req); 10869c4a7965SKim Phillips struct talitos_ctx *ctx = crypto_aead_ctx(authenc); 10879c4a7965SKim Phillips unsigned int authsize = ctx->authsize; 10889c4a7965SKim Phillips struct ipsec_esp_edesc *edesc; 10899c4a7965SKim Phillips struct scatterlist *sg; 10909c4a7965SKim Phillips void *icvdata; 10919c4a7965SKim Phillips 10929c4a7965SKim Phillips req->cryptlen -= authsize; 10939c4a7965SKim Phillips 10949c4a7965SKim Phillips /* allocate extended descriptor */ 10959c4a7965SKim Phillips edesc = ipsec_esp_edesc_alloc(req, 1); 10969c4a7965SKim Phillips if (IS_ERR(edesc)) 10979c4a7965SKim Phillips return PTR_ERR(edesc); 10989c4a7965SKim Phillips 10999c4a7965SKim Phillips /* stash incoming ICV for later cmp with ICV generated by the h/w */ 11009c4a7965SKim Phillips if (edesc->dma_len) 11019c4a7965SKim Phillips icvdata = &edesc->link_tbl[edesc->src_nents + 11029c4a7965SKim Phillips edesc->dst_nents + 1]; 11039c4a7965SKim Phillips else 11049c4a7965SKim Phillips icvdata = &edesc->link_tbl[0]; 11059c4a7965SKim Phillips 11069c4a7965SKim Phillips sg = sg_last(req->src, edesc->src_nents ? : 1); 11079c4a7965SKim Phillips 11089c4a7965SKim Phillips memcpy(icvdata, (char *)sg_virt(sg) + sg->length - ctx->authsize, 11099c4a7965SKim Phillips ctx->authsize); 11109c4a7965SKim Phillips 11119c4a7965SKim Phillips /* decrypt */ 11129c4a7965SKim Phillips edesc->desc.hdr = ctx->desc_hdr_template | DESC_HDR_DIR_INBOUND; 11139c4a7965SKim Phillips 11149c4a7965SKim Phillips return ipsec_esp(edesc, req, NULL, 0, ipsec_esp_decrypt_done); 11159c4a7965SKim Phillips } 11169c4a7965SKim Phillips 111770bcaca7SLee Nipper static int aead_authenc_givencrypt( 11189c4a7965SKim Phillips struct aead_givcrypt_request *req) 11199c4a7965SKim Phillips { 11209c4a7965SKim Phillips struct aead_request *areq = &req->areq; 11219c4a7965SKim Phillips struct crypto_aead *authenc = crypto_aead_reqtfm(areq); 11229c4a7965SKim Phillips struct talitos_ctx *ctx = crypto_aead_ctx(authenc); 11239c4a7965SKim Phillips struct ipsec_esp_edesc *edesc; 11249c4a7965SKim Phillips 11259c4a7965SKim Phillips /* allocate extended descriptor */ 11269c4a7965SKim Phillips edesc = ipsec_esp_edesc_alloc(areq, 0); 11279c4a7965SKim Phillips if (IS_ERR(edesc)) 11289c4a7965SKim Phillips return PTR_ERR(edesc); 11299c4a7965SKim Phillips 11309c4a7965SKim Phillips /* set encrypt */ 113170bcaca7SLee Nipper edesc->desc.hdr = ctx->desc_hdr_template | DESC_HDR_MODE0_ENCRYPT; 11329c4a7965SKim Phillips 11339c4a7965SKim Phillips memcpy(req->giv, ctx->iv, crypto_aead_ivsize(authenc)); 11349c4a7965SKim Phillips 11359c4a7965SKim Phillips return ipsec_esp(edesc, areq, req->giv, req->seq, 11369c4a7965SKim Phillips ipsec_esp_encrypt_done); 11379c4a7965SKim Phillips } 11389c4a7965SKim Phillips 11399c4a7965SKim Phillips struct talitos_alg_template { 11409c4a7965SKim Phillips char name[CRYPTO_MAX_ALG_NAME]; 11419c4a7965SKim Phillips char driver_name[CRYPTO_MAX_ALG_NAME]; 11429c4a7965SKim Phillips unsigned int blocksize; 11439c4a7965SKim Phillips struct aead_alg aead; 11449c4a7965SKim Phillips struct device *dev; 11459c4a7965SKim Phillips __be32 desc_hdr_template; 11469c4a7965SKim Phillips }; 11479c4a7965SKim Phillips 11489c4a7965SKim Phillips static struct talitos_alg_template driver_algs[] = { 11499c4a7965SKim Phillips /* single-pass ipsec_esp descriptor */ 11509c4a7965SKim Phillips { 11519c4a7965SKim Phillips .name = "authenc(hmac(sha1),cbc(aes))", 1152ebbcf336SHerbert Xu .driver_name = "authenc-hmac-sha1-cbc-aes-talitos", 11533952f17eSLee Nipper .blocksize = AES_BLOCK_SIZE, 11549c4a7965SKim Phillips .aead = { 115570bcaca7SLee Nipper .setkey = aead_authenc_setkey, 115670bcaca7SLee Nipper .setauthsize = aead_authenc_setauthsize, 115770bcaca7SLee Nipper .encrypt = aead_authenc_encrypt, 115870bcaca7SLee Nipper .decrypt = aead_authenc_decrypt, 115970bcaca7SLee Nipper .givencrypt = aead_authenc_givencrypt, 11609c4a7965SKim Phillips .geniv = "<built-in>", 11613952f17eSLee Nipper .ivsize = AES_BLOCK_SIZE, 11623952f17eSLee Nipper .maxauthsize = SHA1_DIGEST_SIZE, 11639c4a7965SKim Phillips }, 11649c4a7965SKim Phillips .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP | 11659c4a7965SKim Phillips DESC_HDR_SEL0_AESU | 11669c4a7965SKim Phillips DESC_HDR_MODE0_AESU_CBC | 11679c4a7965SKim Phillips DESC_HDR_SEL1_MDEUA | 11689c4a7965SKim Phillips DESC_HDR_MODE1_MDEU_INIT | 11699c4a7965SKim Phillips DESC_HDR_MODE1_MDEU_PAD | 11709c4a7965SKim Phillips DESC_HDR_MODE1_MDEU_SHA1_HMAC, 117170bcaca7SLee Nipper }, 117270bcaca7SLee Nipper { 117370bcaca7SLee Nipper .name = "authenc(hmac(sha1),cbc(des3_ede))", 1174ebbcf336SHerbert Xu .driver_name = "authenc-hmac-sha1-cbc-3des-talitos", 11753952f17eSLee Nipper .blocksize = DES3_EDE_BLOCK_SIZE, 117670bcaca7SLee Nipper .aead = { 117770bcaca7SLee Nipper .setkey = aead_authenc_setkey, 117870bcaca7SLee Nipper .setauthsize = aead_authenc_setauthsize, 117970bcaca7SLee Nipper .encrypt = aead_authenc_encrypt, 118070bcaca7SLee Nipper .decrypt = aead_authenc_decrypt, 118170bcaca7SLee Nipper .givencrypt = aead_authenc_givencrypt, 118270bcaca7SLee Nipper .geniv = "<built-in>", 11833952f17eSLee Nipper .ivsize = DES3_EDE_BLOCK_SIZE, 11843952f17eSLee Nipper .maxauthsize = SHA1_DIGEST_SIZE, 118570bcaca7SLee Nipper }, 118670bcaca7SLee Nipper .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP | 118770bcaca7SLee Nipper DESC_HDR_SEL0_DEU | 118870bcaca7SLee Nipper DESC_HDR_MODE0_DEU_CBC | 118970bcaca7SLee Nipper DESC_HDR_MODE0_DEU_3DES | 119070bcaca7SLee Nipper DESC_HDR_SEL1_MDEUA | 119170bcaca7SLee Nipper DESC_HDR_MODE1_MDEU_INIT | 119270bcaca7SLee Nipper DESC_HDR_MODE1_MDEU_PAD | 119370bcaca7SLee Nipper DESC_HDR_MODE1_MDEU_SHA1_HMAC, 11943952f17eSLee Nipper }, 11953952f17eSLee Nipper { 11963952f17eSLee Nipper .name = "authenc(hmac(sha256),cbc(aes))", 11973952f17eSLee Nipper .driver_name = "authenc-hmac-sha256-cbc-aes-talitos", 11983952f17eSLee Nipper .blocksize = AES_BLOCK_SIZE, 11993952f17eSLee Nipper .aead = { 12003952f17eSLee Nipper .setkey = aead_authenc_setkey, 12013952f17eSLee Nipper .setauthsize = aead_authenc_setauthsize, 12023952f17eSLee Nipper .encrypt = aead_authenc_encrypt, 12033952f17eSLee Nipper .decrypt = aead_authenc_decrypt, 12043952f17eSLee Nipper .givencrypt = aead_authenc_givencrypt, 12053952f17eSLee Nipper .geniv = "<built-in>", 12063952f17eSLee Nipper .ivsize = AES_BLOCK_SIZE, 12073952f17eSLee Nipper .maxauthsize = SHA256_DIGEST_SIZE, 12083952f17eSLee Nipper }, 12093952f17eSLee Nipper .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP | 12103952f17eSLee Nipper DESC_HDR_SEL0_AESU | 12113952f17eSLee Nipper DESC_HDR_MODE0_AESU_CBC | 12123952f17eSLee Nipper DESC_HDR_SEL1_MDEUA | 12133952f17eSLee Nipper DESC_HDR_MODE1_MDEU_INIT | 12143952f17eSLee Nipper DESC_HDR_MODE1_MDEU_PAD | 12153952f17eSLee Nipper DESC_HDR_MODE1_MDEU_SHA256_HMAC, 12163952f17eSLee Nipper }, 12173952f17eSLee Nipper { 12183952f17eSLee Nipper .name = "authenc(hmac(sha256),cbc(des3_ede))", 12193952f17eSLee Nipper .driver_name = "authenc-hmac-sha256-cbc-3des-talitos", 12203952f17eSLee Nipper .blocksize = DES3_EDE_BLOCK_SIZE, 12213952f17eSLee Nipper .aead = { 12223952f17eSLee Nipper .setkey = aead_authenc_setkey, 12233952f17eSLee Nipper .setauthsize = aead_authenc_setauthsize, 12243952f17eSLee Nipper .encrypt = aead_authenc_encrypt, 12253952f17eSLee Nipper .decrypt = aead_authenc_decrypt, 12263952f17eSLee Nipper .givencrypt = aead_authenc_givencrypt, 12273952f17eSLee Nipper .geniv = "<built-in>", 12283952f17eSLee Nipper .ivsize = DES3_EDE_BLOCK_SIZE, 12293952f17eSLee Nipper .maxauthsize = SHA256_DIGEST_SIZE, 12303952f17eSLee Nipper }, 12313952f17eSLee Nipper .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP | 12323952f17eSLee Nipper DESC_HDR_SEL0_DEU | 12333952f17eSLee Nipper DESC_HDR_MODE0_DEU_CBC | 12343952f17eSLee Nipper DESC_HDR_MODE0_DEU_3DES | 12353952f17eSLee Nipper DESC_HDR_SEL1_MDEUA | 12363952f17eSLee Nipper DESC_HDR_MODE1_MDEU_INIT | 12373952f17eSLee Nipper DESC_HDR_MODE1_MDEU_PAD | 12383952f17eSLee Nipper DESC_HDR_MODE1_MDEU_SHA256_HMAC, 12393952f17eSLee Nipper }, 12403952f17eSLee Nipper { 12413952f17eSLee Nipper .name = "authenc(hmac(md5),cbc(aes))", 12423952f17eSLee Nipper .driver_name = "authenc-hmac-md5-cbc-aes-talitos", 12433952f17eSLee Nipper .blocksize = AES_BLOCK_SIZE, 12443952f17eSLee Nipper .aead = { 12453952f17eSLee Nipper .setkey = aead_authenc_setkey, 12463952f17eSLee Nipper .setauthsize = aead_authenc_setauthsize, 12473952f17eSLee Nipper .encrypt = aead_authenc_encrypt, 12483952f17eSLee Nipper .decrypt = aead_authenc_decrypt, 12493952f17eSLee Nipper .givencrypt = aead_authenc_givencrypt, 12503952f17eSLee Nipper .geniv = "<built-in>", 12513952f17eSLee Nipper .ivsize = AES_BLOCK_SIZE, 12523952f17eSLee Nipper .maxauthsize = MD5_DIGEST_SIZE, 12533952f17eSLee Nipper }, 12543952f17eSLee Nipper .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP | 12553952f17eSLee Nipper DESC_HDR_SEL0_AESU | 12563952f17eSLee Nipper DESC_HDR_MODE0_AESU_CBC | 12573952f17eSLee Nipper DESC_HDR_SEL1_MDEUA | 12583952f17eSLee Nipper DESC_HDR_MODE1_MDEU_INIT | 12593952f17eSLee Nipper DESC_HDR_MODE1_MDEU_PAD | 12603952f17eSLee Nipper DESC_HDR_MODE1_MDEU_MD5_HMAC, 12613952f17eSLee Nipper }, 12623952f17eSLee Nipper { 12633952f17eSLee Nipper .name = "authenc(hmac(md5),cbc(des3_ede))", 12643952f17eSLee Nipper .driver_name = "authenc-hmac-md5-cbc-3des-talitos", 12653952f17eSLee Nipper .blocksize = DES3_EDE_BLOCK_SIZE, 12663952f17eSLee Nipper .aead = { 12673952f17eSLee Nipper .setkey = aead_authenc_setkey, 12683952f17eSLee Nipper .setauthsize = aead_authenc_setauthsize, 12693952f17eSLee Nipper .encrypt = aead_authenc_encrypt, 12703952f17eSLee Nipper .decrypt = aead_authenc_decrypt, 12713952f17eSLee Nipper .givencrypt = aead_authenc_givencrypt, 12723952f17eSLee Nipper .geniv = "<built-in>", 12733952f17eSLee Nipper .ivsize = DES3_EDE_BLOCK_SIZE, 12743952f17eSLee Nipper .maxauthsize = MD5_DIGEST_SIZE, 12753952f17eSLee Nipper }, 12763952f17eSLee Nipper .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP | 12773952f17eSLee Nipper DESC_HDR_SEL0_DEU | 12783952f17eSLee Nipper DESC_HDR_MODE0_DEU_CBC | 12793952f17eSLee Nipper DESC_HDR_MODE0_DEU_3DES | 12803952f17eSLee Nipper DESC_HDR_SEL1_MDEUA | 12813952f17eSLee Nipper DESC_HDR_MODE1_MDEU_INIT | 12823952f17eSLee Nipper DESC_HDR_MODE1_MDEU_PAD | 12833952f17eSLee Nipper DESC_HDR_MODE1_MDEU_MD5_HMAC, 12849c4a7965SKim Phillips } 12859c4a7965SKim Phillips }; 12869c4a7965SKim Phillips 12879c4a7965SKim Phillips struct talitos_crypto_alg { 12889c4a7965SKim Phillips struct list_head entry; 12899c4a7965SKim Phillips struct device *dev; 12909c4a7965SKim Phillips __be32 desc_hdr_template; 12919c4a7965SKim Phillips struct crypto_alg crypto_alg; 12929c4a7965SKim Phillips }; 12939c4a7965SKim Phillips 12949c4a7965SKim Phillips static int talitos_cra_init(struct crypto_tfm *tfm) 12959c4a7965SKim Phillips { 12969c4a7965SKim Phillips struct crypto_alg *alg = tfm->__crt_alg; 12979c4a7965SKim Phillips struct talitos_crypto_alg *talitos_alg = 12989c4a7965SKim Phillips container_of(alg, struct talitos_crypto_alg, crypto_alg); 12999c4a7965SKim Phillips struct talitos_ctx *ctx = crypto_tfm_ctx(tfm); 13009c4a7965SKim Phillips 13019c4a7965SKim Phillips /* update context with ptr to dev */ 13029c4a7965SKim Phillips ctx->dev = talitos_alg->dev; 13039c4a7965SKim Phillips /* copy descriptor header template value */ 13049c4a7965SKim Phillips ctx->desc_hdr_template = talitos_alg->desc_hdr_template; 13059c4a7965SKim Phillips 13069c4a7965SKim Phillips /* random first IV */ 130770bcaca7SLee Nipper get_random_bytes(ctx->iv, TALITOS_MAX_IV_LENGTH); 13089c4a7965SKim Phillips 13099c4a7965SKim Phillips return 0; 13109c4a7965SKim Phillips } 13119c4a7965SKim Phillips 13129c4a7965SKim Phillips /* 13139c4a7965SKim Phillips * given the alg's descriptor header template, determine whether descriptor 13149c4a7965SKim Phillips * type and primary/secondary execution units required match the hw 13159c4a7965SKim Phillips * capabilities description provided in the device tree node. 13169c4a7965SKim Phillips */ 13179c4a7965SKim Phillips static int hw_supports(struct device *dev, __be32 desc_hdr_template) 13189c4a7965SKim Phillips { 13199c4a7965SKim Phillips struct talitos_private *priv = dev_get_drvdata(dev); 13209c4a7965SKim Phillips int ret; 13219c4a7965SKim Phillips 13229c4a7965SKim Phillips ret = (1 << DESC_TYPE(desc_hdr_template) & priv->desc_types) && 13239c4a7965SKim Phillips (1 << PRIMARY_EU(desc_hdr_template) & priv->exec_units); 13249c4a7965SKim Phillips 13259c4a7965SKim Phillips if (SECONDARY_EU(desc_hdr_template)) 13269c4a7965SKim Phillips ret = ret && (1 << SECONDARY_EU(desc_hdr_template) 13279c4a7965SKim Phillips & priv->exec_units); 13289c4a7965SKim Phillips 13299c4a7965SKim Phillips return ret; 13309c4a7965SKim Phillips } 13319c4a7965SKim Phillips 13329c4a7965SKim Phillips static int __devexit talitos_remove(struct of_device *ofdev) 13339c4a7965SKim Phillips { 13349c4a7965SKim Phillips struct device *dev = &ofdev->dev; 13359c4a7965SKim Phillips struct talitos_private *priv = dev_get_drvdata(dev); 13369c4a7965SKim Phillips struct talitos_crypto_alg *t_alg, *n; 13379c4a7965SKim Phillips int i; 13389c4a7965SKim Phillips 13399c4a7965SKim Phillips list_for_each_entry_safe(t_alg, n, &priv->alg_list, entry) { 13409c4a7965SKim Phillips crypto_unregister_alg(&t_alg->crypto_alg); 13419c4a7965SKim Phillips list_del(&t_alg->entry); 13429c4a7965SKim Phillips kfree(t_alg); 13439c4a7965SKim Phillips } 13449c4a7965SKim Phillips 13459c4a7965SKim Phillips if (hw_supports(dev, DESC_HDR_SEL0_RNG)) 13469c4a7965SKim Phillips talitos_unregister_rng(dev); 13479c4a7965SKim Phillips 1348ec6644d6SKim Phillips kfree(priv->submit_count); 13499c4a7965SKim Phillips kfree(priv->tail); 13509c4a7965SKim Phillips kfree(priv->head); 13519c4a7965SKim Phillips 13529c4a7965SKim Phillips if (priv->fifo) 13539c4a7965SKim Phillips for (i = 0; i < priv->num_channels; i++) 13549c4a7965SKim Phillips kfree(priv->fifo[i]); 13559c4a7965SKim Phillips 13569c4a7965SKim Phillips kfree(priv->fifo); 13579c4a7965SKim Phillips kfree(priv->head_lock); 13589c4a7965SKim Phillips kfree(priv->tail_lock); 13599c4a7965SKim Phillips 13609c4a7965SKim Phillips if (priv->irq != NO_IRQ) { 13619c4a7965SKim Phillips free_irq(priv->irq, dev); 13629c4a7965SKim Phillips irq_dispose_mapping(priv->irq); 13639c4a7965SKim Phillips } 13649c4a7965SKim Phillips 13659c4a7965SKim Phillips tasklet_kill(&priv->done_task); 13669c4a7965SKim Phillips tasklet_kill(&priv->error_task); 13679c4a7965SKim Phillips 13689c4a7965SKim Phillips iounmap(priv->reg); 13699c4a7965SKim Phillips 13709c4a7965SKim Phillips dev_set_drvdata(dev, NULL); 13719c4a7965SKim Phillips 13729c4a7965SKim Phillips kfree(priv); 13739c4a7965SKim Phillips 13749c4a7965SKim Phillips return 0; 13759c4a7965SKim Phillips } 13769c4a7965SKim Phillips 13779c4a7965SKim Phillips static struct talitos_crypto_alg *talitos_alg_alloc(struct device *dev, 13789c4a7965SKim Phillips struct talitos_alg_template 13799c4a7965SKim Phillips *template) 13809c4a7965SKim Phillips { 13819c4a7965SKim Phillips struct talitos_crypto_alg *t_alg; 13829c4a7965SKim Phillips struct crypto_alg *alg; 13839c4a7965SKim Phillips 13849c4a7965SKim Phillips t_alg = kzalloc(sizeof(struct talitos_crypto_alg), GFP_KERNEL); 13859c4a7965SKim Phillips if (!t_alg) 13869c4a7965SKim Phillips return ERR_PTR(-ENOMEM); 13879c4a7965SKim Phillips 13889c4a7965SKim Phillips alg = &t_alg->crypto_alg; 13899c4a7965SKim Phillips 13909c4a7965SKim Phillips snprintf(alg->cra_name, CRYPTO_MAX_ALG_NAME, "%s", template->name); 13919c4a7965SKim Phillips snprintf(alg->cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s", 13929c4a7965SKim Phillips template->driver_name); 13939c4a7965SKim Phillips alg->cra_module = THIS_MODULE; 13949c4a7965SKim Phillips alg->cra_init = talitos_cra_init; 13959c4a7965SKim Phillips alg->cra_priority = TALITOS_CRA_PRIORITY; 13969c4a7965SKim Phillips alg->cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC; 13979c4a7965SKim Phillips alg->cra_blocksize = template->blocksize; 13989c4a7965SKim Phillips alg->cra_alignmask = 0; 13999c4a7965SKim Phillips alg->cra_type = &crypto_aead_type; 14009c4a7965SKim Phillips alg->cra_ctxsize = sizeof(struct talitos_ctx); 14019c4a7965SKim Phillips alg->cra_u.aead = template->aead; 14029c4a7965SKim Phillips 14039c4a7965SKim Phillips t_alg->desc_hdr_template = template->desc_hdr_template; 14049c4a7965SKim Phillips t_alg->dev = dev; 14059c4a7965SKim Phillips 14069c4a7965SKim Phillips return t_alg; 14079c4a7965SKim Phillips } 14089c4a7965SKim Phillips 14099c4a7965SKim Phillips static int talitos_probe(struct of_device *ofdev, 14109c4a7965SKim Phillips const struct of_device_id *match) 14119c4a7965SKim Phillips { 14129c4a7965SKim Phillips struct device *dev = &ofdev->dev; 14139c4a7965SKim Phillips struct device_node *np = ofdev->node; 14149c4a7965SKim Phillips struct talitos_private *priv; 14159c4a7965SKim Phillips const unsigned int *prop; 14169c4a7965SKim Phillips int i, err; 14179c4a7965SKim Phillips 14189c4a7965SKim Phillips priv = kzalloc(sizeof(struct talitos_private), GFP_KERNEL); 14199c4a7965SKim Phillips if (!priv) 14209c4a7965SKim Phillips return -ENOMEM; 14219c4a7965SKim Phillips 14229c4a7965SKim Phillips dev_set_drvdata(dev, priv); 14239c4a7965SKim Phillips 14249c4a7965SKim Phillips priv->ofdev = ofdev; 14259c4a7965SKim Phillips 14269c4a7965SKim Phillips tasklet_init(&priv->done_task, talitos_done, (unsigned long)dev); 14279c4a7965SKim Phillips tasklet_init(&priv->error_task, talitos_error, (unsigned long)dev); 14289c4a7965SKim Phillips 14299c4a7965SKim Phillips priv->irq = irq_of_parse_and_map(np, 0); 14309c4a7965SKim Phillips 14319c4a7965SKim Phillips if (priv->irq == NO_IRQ) { 14329c4a7965SKim Phillips dev_err(dev, "failed to map irq\n"); 14339c4a7965SKim Phillips err = -EINVAL; 14349c4a7965SKim Phillips goto err_out; 14359c4a7965SKim Phillips } 14369c4a7965SKim Phillips 14379c4a7965SKim Phillips /* get the irq line */ 14389c4a7965SKim Phillips err = request_irq(priv->irq, talitos_interrupt, 0, 14399c4a7965SKim Phillips dev_driver_string(dev), dev); 14409c4a7965SKim Phillips if (err) { 14419c4a7965SKim Phillips dev_err(dev, "failed to request irq %d\n", priv->irq); 14429c4a7965SKim Phillips irq_dispose_mapping(priv->irq); 14439c4a7965SKim Phillips priv->irq = NO_IRQ; 14449c4a7965SKim Phillips goto err_out; 14459c4a7965SKim Phillips } 14469c4a7965SKim Phillips 14479c4a7965SKim Phillips priv->reg = of_iomap(np, 0); 14489c4a7965SKim Phillips if (!priv->reg) { 14499c4a7965SKim Phillips dev_err(dev, "failed to of_iomap\n"); 14509c4a7965SKim Phillips err = -ENOMEM; 14519c4a7965SKim Phillips goto err_out; 14529c4a7965SKim Phillips } 14539c4a7965SKim Phillips 14549c4a7965SKim Phillips /* get SEC version capabilities from device tree */ 14559c4a7965SKim Phillips prop = of_get_property(np, "fsl,num-channels", NULL); 14569c4a7965SKim Phillips if (prop) 14579c4a7965SKim Phillips priv->num_channels = *prop; 14589c4a7965SKim Phillips 14599c4a7965SKim Phillips prop = of_get_property(np, "fsl,channel-fifo-len", NULL); 14609c4a7965SKim Phillips if (prop) 14619c4a7965SKim Phillips priv->chfifo_len = *prop; 14629c4a7965SKim Phillips 14639c4a7965SKim Phillips prop = of_get_property(np, "fsl,exec-units-mask", NULL); 14649c4a7965SKim Phillips if (prop) 14659c4a7965SKim Phillips priv->exec_units = *prop; 14669c4a7965SKim Phillips 14679c4a7965SKim Phillips prop = of_get_property(np, "fsl,descriptor-types-mask", NULL); 14689c4a7965SKim Phillips if (prop) 14699c4a7965SKim Phillips priv->desc_types = *prop; 14709c4a7965SKim Phillips 14719c4a7965SKim Phillips if (!is_power_of_2(priv->num_channels) || !priv->chfifo_len || 14729c4a7965SKim Phillips !priv->exec_units || !priv->desc_types) { 14739c4a7965SKim Phillips dev_err(dev, "invalid property data in device tree node\n"); 14749c4a7965SKim Phillips err = -EINVAL; 14759c4a7965SKim Phillips goto err_out; 14769c4a7965SKim Phillips } 14779c4a7965SKim Phillips 14789c4a7965SKim Phillips priv->head_lock = kmalloc(sizeof(spinlock_t) * priv->num_channels, 14799c4a7965SKim Phillips GFP_KERNEL); 14809c4a7965SKim Phillips priv->tail_lock = kmalloc(sizeof(spinlock_t) * priv->num_channels, 14819c4a7965SKim Phillips GFP_KERNEL); 14829c4a7965SKim Phillips if (!priv->head_lock || !priv->tail_lock) { 14839c4a7965SKim Phillips dev_err(dev, "failed to allocate fifo locks\n"); 14849c4a7965SKim Phillips err = -ENOMEM; 14859c4a7965SKim Phillips goto err_out; 14869c4a7965SKim Phillips } 14879c4a7965SKim Phillips 14889c4a7965SKim Phillips for (i = 0; i < priv->num_channels; i++) { 14899c4a7965SKim Phillips spin_lock_init(&priv->head_lock[i]); 14909c4a7965SKim Phillips spin_lock_init(&priv->tail_lock[i]); 14919c4a7965SKim Phillips } 14929c4a7965SKim Phillips 14939c4a7965SKim Phillips priv->fifo = kmalloc(sizeof(struct talitos_request *) * 14949c4a7965SKim Phillips priv->num_channels, GFP_KERNEL); 14959c4a7965SKim Phillips if (!priv->fifo) { 14969c4a7965SKim Phillips dev_err(dev, "failed to allocate request fifo\n"); 14979c4a7965SKim Phillips err = -ENOMEM; 14989c4a7965SKim Phillips goto err_out; 14999c4a7965SKim Phillips } 15009c4a7965SKim Phillips 15019c4a7965SKim Phillips priv->fifo_len = roundup_pow_of_two(priv->chfifo_len); 15029c4a7965SKim Phillips 15039c4a7965SKim Phillips for (i = 0; i < priv->num_channels; i++) { 15049c4a7965SKim Phillips priv->fifo[i] = kzalloc(sizeof(struct talitos_request) * 15059c4a7965SKim Phillips priv->fifo_len, GFP_KERNEL); 15069c4a7965SKim Phillips if (!priv->fifo[i]) { 15079c4a7965SKim Phillips dev_err(dev, "failed to allocate request fifo %d\n", i); 15089c4a7965SKim Phillips err = -ENOMEM; 15099c4a7965SKim Phillips goto err_out; 15109c4a7965SKim Phillips } 15119c4a7965SKim Phillips } 15129c4a7965SKim Phillips 1513*586725f8SKim Phillips priv->submit_count = kmalloc(sizeof(atomic_t) * priv->num_channels, 1514ec6644d6SKim Phillips GFP_KERNEL); 1515ec6644d6SKim Phillips if (!priv->submit_count) { 1516ec6644d6SKim Phillips dev_err(dev, "failed to allocate fifo submit count space\n"); 1517ec6644d6SKim Phillips err = -ENOMEM; 1518ec6644d6SKim Phillips goto err_out; 1519ec6644d6SKim Phillips } 1520ec6644d6SKim Phillips for (i = 0; i < priv->num_channels; i++) 1521ec6644d6SKim Phillips atomic_set(&priv->submit_count[i], -priv->chfifo_len); 1522ec6644d6SKim Phillips 15239c4a7965SKim Phillips priv->head = kzalloc(sizeof(int) * priv->num_channels, GFP_KERNEL); 15249c4a7965SKim Phillips priv->tail = kzalloc(sizeof(int) * priv->num_channels, GFP_KERNEL); 15259c4a7965SKim Phillips if (!priv->head || !priv->tail) { 15269c4a7965SKim Phillips dev_err(dev, "failed to allocate request index space\n"); 15279c4a7965SKim Phillips err = -ENOMEM; 15289c4a7965SKim Phillips goto err_out; 15299c4a7965SKim Phillips } 15309c4a7965SKim Phillips 15319c4a7965SKim Phillips /* reset and initialize the h/w */ 15329c4a7965SKim Phillips err = init_device(dev); 15339c4a7965SKim Phillips if (err) { 15349c4a7965SKim Phillips dev_err(dev, "failed to initialize device\n"); 15359c4a7965SKim Phillips goto err_out; 15369c4a7965SKim Phillips } 15379c4a7965SKim Phillips 15389c4a7965SKim Phillips /* register the RNG, if available */ 15399c4a7965SKim Phillips if (hw_supports(dev, DESC_HDR_SEL0_RNG)) { 15409c4a7965SKim Phillips err = talitos_register_rng(dev); 15419c4a7965SKim Phillips if (err) { 15429c4a7965SKim Phillips dev_err(dev, "failed to register hwrng: %d\n", err); 15439c4a7965SKim Phillips goto err_out; 15449c4a7965SKim Phillips } else 15459c4a7965SKim Phillips dev_info(dev, "hwrng\n"); 15469c4a7965SKim Phillips } 15479c4a7965SKim Phillips 15489c4a7965SKim Phillips /* register crypto algorithms the device supports */ 15499c4a7965SKim Phillips INIT_LIST_HEAD(&priv->alg_list); 15509c4a7965SKim Phillips 15519c4a7965SKim Phillips for (i = 0; i < ARRAY_SIZE(driver_algs); i++) { 15529c4a7965SKim Phillips if (hw_supports(dev, driver_algs[i].desc_hdr_template)) { 15539c4a7965SKim Phillips struct talitos_crypto_alg *t_alg; 15549c4a7965SKim Phillips 15559c4a7965SKim Phillips t_alg = talitos_alg_alloc(dev, &driver_algs[i]); 15569c4a7965SKim Phillips if (IS_ERR(t_alg)) { 15579c4a7965SKim Phillips err = PTR_ERR(t_alg); 15589c4a7965SKim Phillips goto err_out; 15599c4a7965SKim Phillips } 15609c4a7965SKim Phillips 15619c4a7965SKim Phillips err = crypto_register_alg(&t_alg->crypto_alg); 15629c4a7965SKim Phillips if (err) { 15639c4a7965SKim Phillips dev_err(dev, "%s alg registration failed\n", 15649c4a7965SKim Phillips t_alg->crypto_alg.cra_driver_name); 15659c4a7965SKim Phillips kfree(t_alg); 15669c4a7965SKim Phillips } else { 15679c4a7965SKim Phillips list_add_tail(&t_alg->entry, &priv->alg_list); 15689c4a7965SKim Phillips dev_info(dev, "%s\n", 15699c4a7965SKim Phillips t_alg->crypto_alg.cra_driver_name); 15709c4a7965SKim Phillips } 15719c4a7965SKim Phillips } 15729c4a7965SKim Phillips } 15739c4a7965SKim Phillips 15749c4a7965SKim Phillips return 0; 15759c4a7965SKim Phillips 15769c4a7965SKim Phillips err_out: 15779c4a7965SKim Phillips talitos_remove(ofdev); 15789c4a7965SKim Phillips 15799c4a7965SKim Phillips return err; 15809c4a7965SKim Phillips } 15819c4a7965SKim Phillips 15829c4a7965SKim Phillips static struct of_device_id talitos_match[] = { 15839c4a7965SKim Phillips { 15849c4a7965SKim Phillips .compatible = "fsl,sec2.0", 15859c4a7965SKim Phillips }, 15869c4a7965SKim Phillips {}, 15879c4a7965SKim Phillips }; 15889c4a7965SKim Phillips MODULE_DEVICE_TABLE(of, talitos_match); 15899c4a7965SKim Phillips 15909c4a7965SKim Phillips static struct of_platform_driver talitos_driver = { 15919c4a7965SKim Phillips .name = "talitos", 15929c4a7965SKim Phillips .match_table = talitos_match, 15939c4a7965SKim Phillips .probe = talitos_probe, 15949c4a7965SKim Phillips .remove = __devexit_p(talitos_remove), 15959c4a7965SKim Phillips }; 15969c4a7965SKim Phillips 15979c4a7965SKim Phillips static int __init talitos_init(void) 15989c4a7965SKim Phillips { 15999c4a7965SKim Phillips return of_register_platform_driver(&talitos_driver); 16009c4a7965SKim Phillips } 16019c4a7965SKim Phillips module_init(talitos_init); 16029c4a7965SKim Phillips 16039c4a7965SKim Phillips static void __exit talitos_exit(void) 16049c4a7965SKim Phillips { 16059c4a7965SKim Phillips of_unregister_platform_driver(&talitos_driver); 16069c4a7965SKim Phillips } 16079c4a7965SKim Phillips module_exit(talitos_exit); 16089c4a7965SKim Phillips 16099c4a7965SKim Phillips MODULE_LICENSE("GPL"); 16109c4a7965SKim Phillips MODULE_AUTHOR("Kim Phillips <kim.phillips@freescale.com>"); 16119c4a7965SKim Phillips MODULE_DESCRIPTION("Freescale integrated security engine (SEC) driver"); 1612