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> 405a0e3ad6STejun Heo #include <linux/slab.h> 419c4a7965SKim Phillips 429c4a7965SKim Phillips #include <crypto/algapi.h> 439c4a7965SKim Phillips #include <crypto/aes.h> 443952f17eSLee Nipper #include <crypto/des.h> 459c4a7965SKim Phillips #include <crypto/sha.h> 469c4a7965SKim Phillips #include <crypto/aead.h> 479c4a7965SKim Phillips #include <crypto/authenc.h> 484de9d0b5SLee Nipper #include <crypto/skcipher.h> 494de9d0b5SLee Nipper #include <crypto/scatterwalk.h> 509c4a7965SKim Phillips 519c4a7965SKim Phillips #include "talitos.h" 529c4a7965SKim Phillips 539c4a7965SKim Phillips #define TALITOS_TIMEOUT 100000 549c4a7965SKim Phillips #define TALITOS_MAX_DATA_LEN 65535 559c4a7965SKim Phillips 569c4a7965SKim Phillips #define DESC_TYPE(desc_hdr) ((be32_to_cpu(desc_hdr) >> 3) & 0x1f) 579c4a7965SKim Phillips #define PRIMARY_EU(desc_hdr) ((be32_to_cpu(desc_hdr) >> 28) & 0xf) 589c4a7965SKim Phillips #define SECONDARY_EU(desc_hdr) ((be32_to_cpu(desc_hdr) >> 16) & 0xf) 599c4a7965SKim Phillips 609c4a7965SKim Phillips /* descriptor pointer entry */ 619c4a7965SKim Phillips struct talitos_ptr { 629c4a7965SKim Phillips __be16 len; /* length */ 639c4a7965SKim Phillips u8 j_extent; /* jump to sg link table and/or extent */ 649c4a7965SKim Phillips u8 eptr; /* extended address */ 659c4a7965SKim Phillips __be32 ptr; /* address */ 669c4a7965SKim Phillips }; 679c4a7965SKim Phillips 689c4a7965SKim Phillips /* descriptor */ 699c4a7965SKim Phillips struct talitos_desc { 709c4a7965SKim Phillips __be32 hdr; /* header high bits */ 719c4a7965SKim Phillips __be32 hdr_lo; /* header low bits */ 729c4a7965SKim Phillips struct talitos_ptr ptr[7]; /* ptr/len pair array */ 739c4a7965SKim Phillips }; 749c4a7965SKim Phillips 759c4a7965SKim Phillips /** 769c4a7965SKim Phillips * talitos_request - descriptor submission request 779c4a7965SKim Phillips * @desc: descriptor pointer (kernel virtual) 789c4a7965SKim Phillips * @dma_desc: descriptor's physical bus address 799c4a7965SKim Phillips * @callback: whom to call when descriptor processing is done 809c4a7965SKim Phillips * @context: caller context (optional) 819c4a7965SKim Phillips */ 829c4a7965SKim Phillips struct talitos_request { 839c4a7965SKim Phillips struct talitos_desc *desc; 849c4a7965SKim Phillips dma_addr_t dma_desc; 859c4a7965SKim Phillips void (*callback) (struct device *dev, struct talitos_desc *desc, 869c4a7965SKim Phillips void *context, int error); 879c4a7965SKim Phillips void *context; 889c4a7965SKim Phillips }; 899c4a7965SKim Phillips 904b992628SKim Phillips /* per-channel fifo management */ 914b992628SKim Phillips struct talitos_channel { 924b992628SKim Phillips /* request fifo */ 934b992628SKim Phillips struct talitos_request *fifo; 944b992628SKim Phillips 954b992628SKim Phillips /* number of requests pending in channel h/w fifo */ 964b992628SKim Phillips atomic_t submit_count ____cacheline_aligned; 974b992628SKim Phillips 984b992628SKim Phillips /* request submission (head) lock */ 994b992628SKim Phillips spinlock_t head_lock ____cacheline_aligned; 1004b992628SKim Phillips /* index to next free descriptor request */ 1014b992628SKim Phillips int head; 1024b992628SKim Phillips 1034b992628SKim Phillips /* request release (tail) lock */ 1044b992628SKim Phillips spinlock_t tail_lock ____cacheline_aligned; 1054b992628SKim Phillips /* index to next in-progress/done descriptor request */ 1064b992628SKim Phillips int tail; 1074b992628SKim Phillips }; 1084b992628SKim Phillips 1099c4a7965SKim Phillips struct talitos_private { 1109c4a7965SKim Phillips struct device *dev; 1119c4a7965SKim Phillips struct of_device *ofdev; 1129c4a7965SKim Phillips void __iomem *reg; 1139c4a7965SKim Phillips int irq; 1149c4a7965SKim Phillips 1159c4a7965SKim Phillips /* SEC version geometry (from device tree node) */ 1169c4a7965SKim Phillips unsigned int num_channels; 1179c4a7965SKim Phillips unsigned int chfifo_len; 1189c4a7965SKim Phillips unsigned int exec_units; 1199c4a7965SKim Phillips unsigned int desc_types; 1209c4a7965SKim Phillips 121f3c85bc1SLee Nipper /* SEC Compatibility info */ 122f3c85bc1SLee Nipper unsigned long features; 123f3c85bc1SLee Nipper 1249c4a7965SKim Phillips /* 1259c4a7965SKim Phillips * length of the request fifo 1269c4a7965SKim Phillips * fifo_len is chfifo_len rounded up to next power of 2 1279c4a7965SKim Phillips * so we can use bitwise ops to wrap 1289c4a7965SKim Phillips */ 1299c4a7965SKim Phillips unsigned int fifo_len; 1309c4a7965SKim Phillips 1314b992628SKim Phillips struct talitos_channel *chan; 1329c4a7965SKim Phillips 1334b992628SKim Phillips /* next channel to be assigned next incoming descriptor */ 1344b992628SKim Phillips atomic_t last_chan ____cacheline_aligned; 1359c4a7965SKim Phillips 1369c4a7965SKim Phillips /* request callback tasklet */ 1379c4a7965SKim Phillips struct tasklet_struct done_task; 1389c4a7965SKim Phillips 1399c4a7965SKim Phillips /* list of registered algorithms */ 1409c4a7965SKim Phillips struct list_head alg_list; 1419c4a7965SKim Phillips 1429c4a7965SKim Phillips /* hwrng device */ 1439c4a7965SKim Phillips struct hwrng rng; 1449c4a7965SKim Phillips }; 1459c4a7965SKim Phillips 146f3c85bc1SLee Nipper /* .features flag */ 147f3c85bc1SLee Nipper #define TALITOS_FTR_SRC_LINK_TBL_LEN_INCLUDES_EXTENT 0x00000001 148fe5720e2SKim Phillips #define TALITOS_FTR_HW_AUTH_CHECK 0x00000002 149f3c85bc1SLee Nipper 15081eb024cSKim Phillips static void to_talitos_ptr(struct talitos_ptr *talitos_ptr, dma_addr_t dma_addr) 15181eb024cSKim Phillips { 15281eb024cSKim Phillips talitos_ptr->ptr = cpu_to_be32(lower_32_bits(dma_addr)); 15381eb024cSKim Phillips talitos_ptr->eptr = cpu_to_be32(upper_32_bits(dma_addr)); 15481eb024cSKim Phillips } 15581eb024cSKim Phillips 1569c4a7965SKim Phillips /* 1579c4a7965SKim Phillips * map virtual single (contiguous) pointer to h/w descriptor pointer 1589c4a7965SKim Phillips */ 1599c4a7965SKim Phillips static void map_single_talitos_ptr(struct device *dev, 1609c4a7965SKim Phillips struct talitos_ptr *talitos_ptr, 1619c4a7965SKim Phillips unsigned short len, void *data, 1629c4a7965SKim Phillips unsigned char extent, 1639c4a7965SKim Phillips enum dma_data_direction dir) 1649c4a7965SKim Phillips { 16581eb024cSKim Phillips dma_addr_t dma_addr = dma_map_single(dev, data, len, dir); 16681eb024cSKim Phillips 1679c4a7965SKim Phillips talitos_ptr->len = cpu_to_be16(len); 16881eb024cSKim Phillips to_talitos_ptr(talitos_ptr, dma_addr); 1699c4a7965SKim Phillips talitos_ptr->j_extent = extent; 1709c4a7965SKim Phillips } 1719c4a7965SKim Phillips 1729c4a7965SKim Phillips /* 1739c4a7965SKim Phillips * unmap bus single (contiguous) h/w descriptor pointer 1749c4a7965SKim Phillips */ 1759c4a7965SKim Phillips static void unmap_single_talitos_ptr(struct device *dev, 1769c4a7965SKim Phillips struct talitos_ptr *talitos_ptr, 1779c4a7965SKim Phillips enum dma_data_direction dir) 1789c4a7965SKim Phillips { 1799c4a7965SKim Phillips dma_unmap_single(dev, be32_to_cpu(talitos_ptr->ptr), 1809c4a7965SKim Phillips be16_to_cpu(talitos_ptr->len), dir); 1819c4a7965SKim Phillips } 1829c4a7965SKim Phillips 1839c4a7965SKim Phillips static int reset_channel(struct device *dev, int ch) 1849c4a7965SKim Phillips { 1859c4a7965SKim Phillips struct talitos_private *priv = dev_get_drvdata(dev); 1869c4a7965SKim Phillips unsigned int timeout = TALITOS_TIMEOUT; 1879c4a7965SKim Phillips 1889c4a7965SKim Phillips setbits32(priv->reg + TALITOS_CCCR(ch), TALITOS_CCCR_RESET); 1899c4a7965SKim Phillips 1909c4a7965SKim Phillips while ((in_be32(priv->reg + TALITOS_CCCR(ch)) & TALITOS_CCCR_RESET) 1919c4a7965SKim Phillips && --timeout) 1929c4a7965SKim Phillips cpu_relax(); 1939c4a7965SKim Phillips 1949c4a7965SKim Phillips if (timeout == 0) { 1959c4a7965SKim Phillips dev_err(dev, "failed to reset channel %d\n", ch); 1969c4a7965SKim Phillips return -EIO; 1979c4a7965SKim Phillips } 1989c4a7965SKim Phillips 19981eb024cSKim Phillips /* set 36-bit addressing, done writeback enable and done IRQ enable */ 20081eb024cSKim Phillips setbits32(priv->reg + TALITOS_CCCR_LO(ch), TALITOS_CCCR_LO_EAE | 20181eb024cSKim Phillips TALITOS_CCCR_LO_CDWE | TALITOS_CCCR_LO_CDIE); 2029c4a7965SKim Phillips 203fe5720e2SKim Phillips /* and ICCR writeback, if available */ 204fe5720e2SKim Phillips if (priv->features & TALITOS_FTR_HW_AUTH_CHECK) 205fe5720e2SKim Phillips setbits32(priv->reg + TALITOS_CCCR_LO(ch), 206fe5720e2SKim Phillips TALITOS_CCCR_LO_IWSE); 207fe5720e2SKim Phillips 2089c4a7965SKim Phillips return 0; 2099c4a7965SKim Phillips } 2109c4a7965SKim Phillips 2119c4a7965SKim Phillips static int reset_device(struct device *dev) 2129c4a7965SKim Phillips { 2139c4a7965SKim Phillips struct talitos_private *priv = dev_get_drvdata(dev); 2149c4a7965SKim Phillips unsigned int timeout = TALITOS_TIMEOUT; 2159c4a7965SKim Phillips 2169c4a7965SKim Phillips setbits32(priv->reg + TALITOS_MCR, TALITOS_MCR_SWR); 2179c4a7965SKim Phillips 2189c4a7965SKim Phillips while ((in_be32(priv->reg + TALITOS_MCR) & TALITOS_MCR_SWR) 2199c4a7965SKim Phillips && --timeout) 2209c4a7965SKim Phillips cpu_relax(); 2219c4a7965SKim Phillips 2229c4a7965SKim Phillips if (timeout == 0) { 2239c4a7965SKim Phillips dev_err(dev, "failed to reset device\n"); 2249c4a7965SKim Phillips return -EIO; 2259c4a7965SKim Phillips } 2269c4a7965SKim Phillips 2279c4a7965SKim Phillips return 0; 2289c4a7965SKim Phillips } 2299c4a7965SKim Phillips 2309c4a7965SKim Phillips /* 2319c4a7965SKim Phillips * Reset and initialize the device 2329c4a7965SKim Phillips */ 2339c4a7965SKim Phillips static int init_device(struct device *dev) 2349c4a7965SKim Phillips { 2359c4a7965SKim Phillips struct talitos_private *priv = dev_get_drvdata(dev); 2369c4a7965SKim Phillips int ch, err; 2379c4a7965SKim Phillips 2389c4a7965SKim Phillips /* 2399c4a7965SKim Phillips * Master reset 2409c4a7965SKim Phillips * errata documentation: warning: certain SEC interrupts 2419c4a7965SKim Phillips * are not fully cleared by writing the MCR:SWR bit, 2429c4a7965SKim Phillips * set bit twice to completely reset 2439c4a7965SKim Phillips */ 2449c4a7965SKim Phillips err = reset_device(dev); 2459c4a7965SKim Phillips if (err) 2469c4a7965SKim Phillips return err; 2479c4a7965SKim Phillips 2489c4a7965SKim Phillips err = reset_device(dev); 2499c4a7965SKim Phillips if (err) 2509c4a7965SKim Phillips return err; 2519c4a7965SKim Phillips 2529c4a7965SKim Phillips /* reset channels */ 2539c4a7965SKim Phillips for (ch = 0; ch < priv->num_channels; ch++) { 2549c4a7965SKim Phillips err = reset_channel(dev, ch); 2559c4a7965SKim Phillips if (err) 2569c4a7965SKim Phillips return err; 2579c4a7965SKim Phillips } 2589c4a7965SKim Phillips 2599c4a7965SKim Phillips /* enable channel done and error interrupts */ 2609c4a7965SKim Phillips setbits32(priv->reg + TALITOS_IMR, TALITOS_IMR_INIT); 2619c4a7965SKim Phillips setbits32(priv->reg + TALITOS_IMR_LO, TALITOS_IMR_LO_INIT); 2629c4a7965SKim Phillips 263fe5720e2SKim Phillips /* disable integrity check error interrupts (use writeback instead) */ 264fe5720e2SKim Phillips if (priv->features & TALITOS_FTR_HW_AUTH_CHECK) 265fe5720e2SKim Phillips setbits32(priv->reg + TALITOS_MDEUICR_LO, 266fe5720e2SKim Phillips TALITOS_MDEUICR_LO_ICE); 267fe5720e2SKim Phillips 2689c4a7965SKim Phillips return 0; 2699c4a7965SKim Phillips } 2709c4a7965SKim Phillips 2719c4a7965SKim Phillips /** 2729c4a7965SKim Phillips * talitos_submit - submits a descriptor to the device for processing 2739c4a7965SKim Phillips * @dev: the SEC device to be used 2749c4a7965SKim Phillips * @desc: the descriptor to be processed by the device 2759c4a7965SKim Phillips * @callback: whom to call when processing is complete 2769c4a7965SKim Phillips * @context: a handle for use by caller (optional) 2779c4a7965SKim Phillips * 2789c4a7965SKim Phillips * desc must contain valid dma-mapped (bus physical) address pointers. 2799c4a7965SKim Phillips * callback must check err and feedback in descriptor header 2809c4a7965SKim Phillips * for device processing status. 2819c4a7965SKim Phillips */ 2829c4a7965SKim Phillips static int talitos_submit(struct device *dev, struct talitos_desc *desc, 2839c4a7965SKim Phillips void (*callback)(struct device *dev, 2849c4a7965SKim Phillips struct talitos_desc *desc, 2859c4a7965SKim Phillips void *context, int error), 2869c4a7965SKim Phillips void *context) 2879c4a7965SKim Phillips { 2889c4a7965SKim Phillips struct talitos_private *priv = dev_get_drvdata(dev); 2899c4a7965SKim Phillips struct talitos_request *request; 2909c4a7965SKim Phillips unsigned long flags, ch; 2919c4a7965SKim Phillips int head; 2929c4a7965SKim Phillips 2939c4a7965SKim Phillips /* select done notification */ 2949c4a7965SKim Phillips desc->hdr |= DESC_HDR_DONE_NOTIFY; 2959c4a7965SKim Phillips 2969c4a7965SKim Phillips /* emulate SEC's round-robin channel fifo polling scheme */ 2979c4a7965SKim Phillips ch = atomic_inc_return(&priv->last_chan) & (priv->num_channels - 1); 2989c4a7965SKim Phillips 2994b992628SKim Phillips spin_lock_irqsave(&priv->chan[ch].head_lock, flags); 3009c4a7965SKim Phillips 3014b992628SKim Phillips if (!atomic_inc_not_zero(&priv->chan[ch].submit_count)) { 302ec6644d6SKim Phillips /* h/w fifo is full */ 3034b992628SKim Phillips spin_unlock_irqrestore(&priv->chan[ch].head_lock, flags); 3049c4a7965SKim Phillips return -EAGAIN; 3059c4a7965SKim Phillips } 3069c4a7965SKim Phillips 3074b992628SKim Phillips head = priv->chan[ch].head; 3084b992628SKim Phillips request = &priv->chan[ch].fifo[head]; 309ec6644d6SKim Phillips 3109c4a7965SKim Phillips /* map descriptor and save caller data */ 3119c4a7965SKim Phillips request->dma_desc = dma_map_single(dev, desc, sizeof(*desc), 3129c4a7965SKim Phillips DMA_BIDIRECTIONAL); 3139c4a7965SKim Phillips request->callback = callback; 3149c4a7965SKim Phillips request->context = context; 3159c4a7965SKim Phillips 3169c4a7965SKim Phillips /* increment fifo head */ 3174b992628SKim Phillips priv->chan[ch].head = (priv->chan[ch].head + 1) & (priv->fifo_len - 1); 3189c4a7965SKim Phillips 3199c4a7965SKim Phillips smp_wmb(); 3209c4a7965SKim Phillips request->desc = desc; 3219c4a7965SKim Phillips 3229c4a7965SKim Phillips /* GO! */ 3239c4a7965SKim Phillips wmb(); 32481eb024cSKim Phillips out_be32(priv->reg + TALITOS_FF(ch), 32581eb024cSKim Phillips cpu_to_be32(upper_32_bits(request->dma_desc))); 32681eb024cSKim Phillips out_be32(priv->reg + TALITOS_FF_LO(ch), 32781eb024cSKim Phillips cpu_to_be32(lower_32_bits(request->dma_desc))); 3289c4a7965SKim Phillips 3294b992628SKim Phillips spin_unlock_irqrestore(&priv->chan[ch].head_lock, flags); 3309c4a7965SKim Phillips 3319c4a7965SKim Phillips return -EINPROGRESS; 3329c4a7965SKim Phillips } 3339c4a7965SKim Phillips 3349c4a7965SKim Phillips /* 3359c4a7965SKim Phillips * process what was done, notify callback of error if not 3369c4a7965SKim Phillips */ 3379c4a7965SKim Phillips static void flush_channel(struct device *dev, int ch, int error, int reset_ch) 3389c4a7965SKim Phillips { 3399c4a7965SKim Phillips struct talitos_private *priv = dev_get_drvdata(dev); 3409c4a7965SKim Phillips struct talitos_request *request, saved_req; 3419c4a7965SKim Phillips unsigned long flags; 3429c4a7965SKim Phillips int tail, status; 3439c4a7965SKim Phillips 3444b992628SKim Phillips spin_lock_irqsave(&priv->chan[ch].tail_lock, flags); 3459c4a7965SKim Phillips 3464b992628SKim Phillips tail = priv->chan[ch].tail; 3474b992628SKim Phillips while (priv->chan[ch].fifo[tail].desc) { 3484b992628SKim Phillips request = &priv->chan[ch].fifo[tail]; 3499c4a7965SKim Phillips 3509c4a7965SKim Phillips /* descriptors with their done bits set don't get the error */ 3519c4a7965SKim Phillips rmb(); 352ca38a814SLee Nipper if ((request->desc->hdr & DESC_HDR_DONE) == DESC_HDR_DONE) 3539c4a7965SKim Phillips status = 0; 354ca38a814SLee Nipper else 3559c4a7965SKim Phillips if (!error) 3569c4a7965SKim Phillips break; 3579c4a7965SKim Phillips else 3589c4a7965SKim Phillips status = error; 3599c4a7965SKim Phillips 3609c4a7965SKim Phillips dma_unmap_single(dev, request->dma_desc, 361e938e465SKim Phillips sizeof(struct talitos_desc), 362e938e465SKim Phillips DMA_BIDIRECTIONAL); 3639c4a7965SKim Phillips 3649c4a7965SKim Phillips /* copy entries so we can call callback outside lock */ 3659c4a7965SKim Phillips saved_req.desc = request->desc; 3669c4a7965SKim Phillips saved_req.callback = request->callback; 3679c4a7965SKim Phillips saved_req.context = request->context; 3689c4a7965SKim Phillips 3699c4a7965SKim Phillips /* release request entry in fifo */ 3709c4a7965SKim Phillips smp_wmb(); 3719c4a7965SKim Phillips request->desc = NULL; 3729c4a7965SKim Phillips 3739c4a7965SKim Phillips /* increment fifo tail */ 3744b992628SKim Phillips priv->chan[ch].tail = (tail + 1) & (priv->fifo_len - 1); 3759c4a7965SKim Phillips 3764b992628SKim Phillips spin_unlock_irqrestore(&priv->chan[ch].tail_lock, flags); 377ec6644d6SKim Phillips 3784b992628SKim Phillips atomic_dec(&priv->chan[ch].submit_count); 379ec6644d6SKim Phillips 3809c4a7965SKim Phillips saved_req.callback(dev, saved_req.desc, saved_req.context, 3819c4a7965SKim Phillips status); 3829c4a7965SKim Phillips /* channel may resume processing in single desc error case */ 3839c4a7965SKim Phillips if (error && !reset_ch && status == error) 3849c4a7965SKim Phillips return; 3854b992628SKim Phillips spin_lock_irqsave(&priv->chan[ch].tail_lock, flags); 3864b992628SKim Phillips tail = priv->chan[ch].tail; 3879c4a7965SKim Phillips } 3889c4a7965SKim Phillips 3894b992628SKim Phillips spin_unlock_irqrestore(&priv->chan[ch].tail_lock, flags); 3909c4a7965SKim Phillips } 3919c4a7965SKim Phillips 3929c4a7965SKim Phillips /* 3939c4a7965SKim Phillips * process completed requests for channels that have done status 3949c4a7965SKim Phillips */ 3959c4a7965SKim Phillips static void talitos_done(unsigned long data) 3969c4a7965SKim Phillips { 3979c4a7965SKim Phillips struct device *dev = (struct device *)data; 3989c4a7965SKim Phillips struct talitos_private *priv = dev_get_drvdata(dev); 3999c4a7965SKim Phillips int ch; 4009c4a7965SKim Phillips 4019c4a7965SKim Phillips for (ch = 0; ch < priv->num_channels; ch++) 4029c4a7965SKim Phillips flush_channel(dev, ch, 0, 0); 4031c2e8811SLee Nipper 4041c2e8811SLee Nipper /* At this point, all completed channels have been processed. 4051c2e8811SLee Nipper * Unmask done interrupts for channels completed later on. 4061c2e8811SLee Nipper */ 407fe5720e2SKim Phillips setbits32(priv->reg + TALITOS_IMR, TALITOS_IMR_INIT); 408fe5720e2SKim Phillips setbits32(priv->reg + TALITOS_IMR_LO, TALITOS_IMR_LO_INIT); 4099c4a7965SKim Phillips } 4109c4a7965SKim Phillips 4119c4a7965SKim Phillips /* 4129c4a7965SKim Phillips * locate current (offending) descriptor 4139c4a7965SKim Phillips */ 4149c4a7965SKim Phillips static struct talitos_desc *current_desc(struct device *dev, int ch) 4159c4a7965SKim Phillips { 4169c4a7965SKim Phillips struct talitos_private *priv = dev_get_drvdata(dev); 4174b992628SKim Phillips int tail = priv->chan[ch].tail; 4189c4a7965SKim Phillips dma_addr_t cur_desc; 4199c4a7965SKim Phillips 4209c4a7965SKim Phillips cur_desc = in_be32(priv->reg + TALITOS_CDPR_LO(ch)); 4219c4a7965SKim Phillips 4224b992628SKim Phillips while (priv->chan[ch].fifo[tail].dma_desc != cur_desc) { 4239c4a7965SKim Phillips tail = (tail + 1) & (priv->fifo_len - 1); 4244b992628SKim Phillips if (tail == priv->chan[ch].tail) { 4259c4a7965SKim Phillips dev_err(dev, "couldn't locate current descriptor\n"); 4269c4a7965SKim Phillips return NULL; 4279c4a7965SKim Phillips } 4289c4a7965SKim Phillips } 4299c4a7965SKim Phillips 4304b992628SKim Phillips return priv->chan[ch].fifo[tail].desc; 4319c4a7965SKim Phillips } 4329c4a7965SKim Phillips 4339c4a7965SKim Phillips /* 4349c4a7965SKim Phillips * user diagnostics; report root cause of error based on execution unit status 4359c4a7965SKim Phillips */ 436e938e465SKim Phillips static void report_eu_error(struct device *dev, int ch, 437e938e465SKim Phillips struct talitos_desc *desc) 4389c4a7965SKim Phillips { 4399c4a7965SKim Phillips struct talitos_private *priv = dev_get_drvdata(dev); 4409c4a7965SKim Phillips int i; 4419c4a7965SKim Phillips 4429c4a7965SKim Phillips switch (desc->hdr & DESC_HDR_SEL0_MASK) { 4439c4a7965SKim Phillips case DESC_HDR_SEL0_AFEU: 4449c4a7965SKim Phillips dev_err(dev, "AFEUISR 0x%08x_%08x\n", 4459c4a7965SKim Phillips in_be32(priv->reg + TALITOS_AFEUISR), 4469c4a7965SKim Phillips in_be32(priv->reg + TALITOS_AFEUISR_LO)); 4479c4a7965SKim Phillips break; 4489c4a7965SKim Phillips case DESC_HDR_SEL0_DEU: 4499c4a7965SKim Phillips dev_err(dev, "DEUISR 0x%08x_%08x\n", 4509c4a7965SKim Phillips in_be32(priv->reg + TALITOS_DEUISR), 4519c4a7965SKim Phillips in_be32(priv->reg + TALITOS_DEUISR_LO)); 4529c4a7965SKim Phillips break; 4539c4a7965SKim Phillips case DESC_HDR_SEL0_MDEUA: 4549c4a7965SKim Phillips case DESC_HDR_SEL0_MDEUB: 4559c4a7965SKim Phillips dev_err(dev, "MDEUISR 0x%08x_%08x\n", 4569c4a7965SKim Phillips in_be32(priv->reg + TALITOS_MDEUISR), 4579c4a7965SKim Phillips in_be32(priv->reg + TALITOS_MDEUISR_LO)); 4589c4a7965SKim Phillips break; 4599c4a7965SKim Phillips case DESC_HDR_SEL0_RNG: 4609c4a7965SKim Phillips dev_err(dev, "RNGUISR 0x%08x_%08x\n", 4619c4a7965SKim Phillips in_be32(priv->reg + TALITOS_RNGUISR), 4629c4a7965SKim Phillips in_be32(priv->reg + TALITOS_RNGUISR_LO)); 4639c4a7965SKim Phillips break; 4649c4a7965SKim Phillips case DESC_HDR_SEL0_PKEU: 4659c4a7965SKim Phillips dev_err(dev, "PKEUISR 0x%08x_%08x\n", 4669c4a7965SKim Phillips in_be32(priv->reg + TALITOS_PKEUISR), 4679c4a7965SKim Phillips in_be32(priv->reg + TALITOS_PKEUISR_LO)); 4689c4a7965SKim Phillips break; 4699c4a7965SKim Phillips case DESC_HDR_SEL0_AESU: 4709c4a7965SKim Phillips dev_err(dev, "AESUISR 0x%08x_%08x\n", 4719c4a7965SKim Phillips in_be32(priv->reg + TALITOS_AESUISR), 4729c4a7965SKim Phillips in_be32(priv->reg + TALITOS_AESUISR_LO)); 4739c4a7965SKim Phillips break; 4749c4a7965SKim Phillips case DESC_HDR_SEL0_CRCU: 4759c4a7965SKim Phillips dev_err(dev, "CRCUISR 0x%08x_%08x\n", 4769c4a7965SKim Phillips in_be32(priv->reg + TALITOS_CRCUISR), 4779c4a7965SKim Phillips in_be32(priv->reg + TALITOS_CRCUISR_LO)); 4789c4a7965SKim Phillips break; 4799c4a7965SKim Phillips case DESC_HDR_SEL0_KEU: 4809c4a7965SKim Phillips dev_err(dev, "KEUISR 0x%08x_%08x\n", 4819c4a7965SKim Phillips in_be32(priv->reg + TALITOS_KEUISR), 4829c4a7965SKim Phillips in_be32(priv->reg + TALITOS_KEUISR_LO)); 4839c4a7965SKim Phillips break; 4849c4a7965SKim Phillips } 4859c4a7965SKim Phillips 4869c4a7965SKim Phillips switch (desc->hdr & DESC_HDR_SEL1_MASK) { 4879c4a7965SKim Phillips case DESC_HDR_SEL1_MDEUA: 4889c4a7965SKim Phillips case DESC_HDR_SEL1_MDEUB: 4899c4a7965SKim Phillips dev_err(dev, "MDEUISR 0x%08x_%08x\n", 4909c4a7965SKim Phillips in_be32(priv->reg + TALITOS_MDEUISR), 4919c4a7965SKim Phillips in_be32(priv->reg + TALITOS_MDEUISR_LO)); 4929c4a7965SKim Phillips break; 4939c4a7965SKim Phillips case DESC_HDR_SEL1_CRCU: 4949c4a7965SKim Phillips dev_err(dev, "CRCUISR 0x%08x_%08x\n", 4959c4a7965SKim Phillips in_be32(priv->reg + TALITOS_CRCUISR), 4969c4a7965SKim Phillips in_be32(priv->reg + TALITOS_CRCUISR_LO)); 4979c4a7965SKim Phillips break; 4989c4a7965SKim Phillips } 4999c4a7965SKim Phillips 5009c4a7965SKim Phillips for (i = 0; i < 8; i++) 5019c4a7965SKim Phillips dev_err(dev, "DESCBUF 0x%08x_%08x\n", 5029c4a7965SKim Phillips in_be32(priv->reg + TALITOS_DESCBUF(ch) + 8*i), 5039c4a7965SKim Phillips in_be32(priv->reg + TALITOS_DESCBUF_LO(ch) + 8*i)); 5049c4a7965SKim Phillips } 5059c4a7965SKim Phillips 5069c4a7965SKim Phillips /* 5079c4a7965SKim Phillips * recover from error interrupts 5089c4a7965SKim Phillips */ 50940405f10SKim Phillips static void talitos_error(unsigned long data, u32 isr, u32 isr_lo) 5109c4a7965SKim Phillips { 5119c4a7965SKim Phillips struct device *dev = (struct device *)data; 5129c4a7965SKim Phillips struct talitos_private *priv = dev_get_drvdata(dev); 5139c4a7965SKim Phillips unsigned int timeout = TALITOS_TIMEOUT; 5149c4a7965SKim Phillips int ch, error, reset_dev = 0, reset_ch = 0; 51540405f10SKim Phillips u32 v, v_lo; 5169c4a7965SKim Phillips 5179c4a7965SKim Phillips for (ch = 0; ch < priv->num_channels; ch++) { 5189c4a7965SKim Phillips /* skip channels without errors */ 5199c4a7965SKim Phillips if (!(isr & (1 << (ch * 2 + 1)))) 5209c4a7965SKim Phillips continue; 5219c4a7965SKim Phillips 5229c4a7965SKim Phillips error = -EINVAL; 5239c4a7965SKim Phillips 5249c4a7965SKim Phillips v = in_be32(priv->reg + TALITOS_CCPSR(ch)); 5259c4a7965SKim Phillips v_lo = in_be32(priv->reg + TALITOS_CCPSR_LO(ch)); 5269c4a7965SKim Phillips 5279c4a7965SKim Phillips if (v_lo & TALITOS_CCPSR_LO_DOF) { 5289c4a7965SKim Phillips dev_err(dev, "double fetch fifo overflow error\n"); 5299c4a7965SKim Phillips error = -EAGAIN; 5309c4a7965SKim Phillips reset_ch = 1; 5319c4a7965SKim Phillips } 5329c4a7965SKim Phillips if (v_lo & TALITOS_CCPSR_LO_SOF) { 5339c4a7965SKim Phillips /* h/w dropped descriptor */ 5349c4a7965SKim Phillips dev_err(dev, "single fetch fifo overflow error\n"); 5359c4a7965SKim Phillips error = -EAGAIN; 5369c4a7965SKim Phillips } 5379c4a7965SKim Phillips if (v_lo & TALITOS_CCPSR_LO_MDTE) 5389c4a7965SKim Phillips dev_err(dev, "master data transfer error\n"); 5399c4a7965SKim Phillips if (v_lo & TALITOS_CCPSR_LO_SGDLZ) 5409c4a7965SKim Phillips dev_err(dev, "s/g data length zero error\n"); 5419c4a7965SKim Phillips if (v_lo & TALITOS_CCPSR_LO_FPZ) 5429c4a7965SKim Phillips dev_err(dev, "fetch pointer zero error\n"); 5439c4a7965SKim Phillips if (v_lo & TALITOS_CCPSR_LO_IDH) 5449c4a7965SKim Phillips dev_err(dev, "illegal descriptor header error\n"); 5459c4a7965SKim Phillips if (v_lo & TALITOS_CCPSR_LO_IEU) 5469c4a7965SKim Phillips dev_err(dev, "invalid execution unit error\n"); 5479c4a7965SKim Phillips if (v_lo & TALITOS_CCPSR_LO_EU) 5489c4a7965SKim Phillips report_eu_error(dev, ch, current_desc(dev, ch)); 5499c4a7965SKim Phillips if (v_lo & TALITOS_CCPSR_LO_GB) 5509c4a7965SKim Phillips dev_err(dev, "gather boundary error\n"); 5519c4a7965SKim Phillips if (v_lo & TALITOS_CCPSR_LO_GRL) 5529c4a7965SKim Phillips dev_err(dev, "gather return/length error\n"); 5539c4a7965SKim Phillips if (v_lo & TALITOS_CCPSR_LO_SB) 5549c4a7965SKim Phillips dev_err(dev, "scatter boundary error\n"); 5559c4a7965SKim Phillips if (v_lo & TALITOS_CCPSR_LO_SRL) 5569c4a7965SKim Phillips dev_err(dev, "scatter return/length error\n"); 5579c4a7965SKim Phillips 5589c4a7965SKim Phillips flush_channel(dev, ch, error, reset_ch); 5599c4a7965SKim Phillips 5609c4a7965SKim Phillips if (reset_ch) { 5619c4a7965SKim Phillips reset_channel(dev, ch); 5629c4a7965SKim Phillips } else { 5639c4a7965SKim Phillips setbits32(priv->reg + TALITOS_CCCR(ch), 5649c4a7965SKim Phillips TALITOS_CCCR_CONT); 5659c4a7965SKim Phillips setbits32(priv->reg + TALITOS_CCCR_LO(ch), 0); 5669c4a7965SKim Phillips while ((in_be32(priv->reg + TALITOS_CCCR(ch)) & 5679c4a7965SKim Phillips TALITOS_CCCR_CONT) && --timeout) 5689c4a7965SKim Phillips cpu_relax(); 5699c4a7965SKim Phillips if (timeout == 0) { 5709c4a7965SKim Phillips dev_err(dev, "failed to restart channel %d\n", 5719c4a7965SKim Phillips ch); 5729c4a7965SKim Phillips reset_dev = 1; 5739c4a7965SKim Phillips } 5749c4a7965SKim Phillips } 5759c4a7965SKim Phillips } 5769c4a7965SKim Phillips if (reset_dev || isr & ~TALITOS_ISR_CHERR || isr_lo) { 5779c4a7965SKim Phillips dev_err(dev, "done overflow, internal time out, or rngu error: " 5789c4a7965SKim Phillips "ISR 0x%08x_%08x\n", isr, isr_lo); 5799c4a7965SKim Phillips 5809c4a7965SKim Phillips /* purge request queues */ 5819c4a7965SKim Phillips for (ch = 0; ch < priv->num_channels; ch++) 5829c4a7965SKim Phillips flush_channel(dev, ch, -EIO, 1); 5839c4a7965SKim Phillips 5849c4a7965SKim Phillips /* reset and reinitialize the device */ 5859c4a7965SKim Phillips init_device(dev); 5869c4a7965SKim Phillips } 5879c4a7965SKim Phillips } 5889c4a7965SKim Phillips 5899c4a7965SKim Phillips static irqreturn_t talitos_interrupt(int irq, void *data) 5909c4a7965SKim Phillips { 5919c4a7965SKim Phillips struct device *dev = data; 5929c4a7965SKim Phillips struct talitos_private *priv = dev_get_drvdata(dev); 5939c4a7965SKim Phillips u32 isr, isr_lo; 5949c4a7965SKim Phillips 5959c4a7965SKim Phillips isr = in_be32(priv->reg + TALITOS_ISR); 5969c4a7965SKim Phillips isr_lo = in_be32(priv->reg + TALITOS_ISR_LO); 597ca38a814SLee Nipper /* Acknowledge interrupt */ 5989c4a7965SKim Phillips out_be32(priv->reg + TALITOS_ICR, isr); 5999c4a7965SKim Phillips out_be32(priv->reg + TALITOS_ICR_LO, isr_lo); 6009c4a7965SKim Phillips 601ca38a814SLee Nipper if (unlikely((isr & ~TALITOS_ISR_CHDONE) || isr_lo)) 60240405f10SKim Phillips talitos_error((unsigned long)data, isr, isr_lo); 603ca38a814SLee Nipper else 6041c2e8811SLee Nipper if (likely(isr & TALITOS_ISR_CHDONE)) { 6051c2e8811SLee Nipper /* mask further done interrupts. */ 6061c2e8811SLee Nipper clrbits32(priv->reg + TALITOS_IMR, TALITOS_IMR_DONE); 6071c2e8811SLee Nipper /* done_task will unmask done interrupts at exit */ 6089c4a7965SKim Phillips tasklet_schedule(&priv->done_task); 6091c2e8811SLee Nipper } 6109c4a7965SKim Phillips 6119c4a7965SKim Phillips return (isr || isr_lo) ? IRQ_HANDLED : IRQ_NONE; 6129c4a7965SKim Phillips } 6139c4a7965SKim Phillips 6149c4a7965SKim Phillips /* 6159c4a7965SKim Phillips * hwrng 6169c4a7965SKim Phillips */ 6179c4a7965SKim Phillips static int talitos_rng_data_present(struct hwrng *rng, int wait) 6189c4a7965SKim Phillips { 6199c4a7965SKim Phillips struct device *dev = (struct device *)rng->priv; 6209c4a7965SKim Phillips struct talitos_private *priv = dev_get_drvdata(dev); 6219c4a7965SKim Phillips u32 ofl; 6229c4a7965SKim Phillips int i; 6239c4a7965SKim Phillips 6249c4a7965SKim Phillips for (i = 0; i < 20; i++) { 6259c4a7965SKim Phillips ofl = in_be32(priv->reg + TALITOS_RNGUSR_LO) & 6269c4a7965SKim Phillips TALITOS_RNGUSR_LO_OFL; 6279c4a7965SKim Phillips if (ofl || !wait) 6289c4a7965SKim Phillips break; 6299c4a7965SKim Phillips udelay(10); 6309c4a7965SKim Phillips } 6319c4a7965SKim Phillips 6329c4a7965SKim Phillips return !!ofl; 6339c4a7965SKim Phillips } 6349c4a7965SKim Phillips 6359c4a7965SKim Phillips static int talitos_rng_data_read(struct hwrng *rng, u32 *data) 6369c4a7965SKim Phillips { 6379c4a7965SKim Phillips struct device *dev = (struct device *)rng->priv; 6389c4a7965SKim Phillips struct talitos_private *priv = dev_get_drvdata(dev); 6399c4a7965SKim Phillips 6409c4a7965SKim Phillips /* rng fifo requires 64-bit accesses */ 6419c4a7965SKim Phillips *data = in_be32(priv->reg + TALITOS_RNGU_FIFO); 6429c4a7965SKim Phillips *data = in_be32(priv->reg + TALITOS_RNGU_FIFO_LO); 6439c4a7965SKim Phillips 6449c4a7965SKim Phillips return sizeof(u32); 6459c4a7965SKim Phillips } 6469c4a7965SKim Phillips 6479c4a7965SKim Phillips static int talitos_rng_init(struct hwrng *rng) 6489c4a7965SKim Phillips { 6499c4a7965SKim Phillips struct device *dev = (struct device *)rng->priv; 6509c4a7965SKim Phillips struct talitos_private *priv = dev_get_drvdata(dev); 6519c4a7965SKim Phillips unsigned int timeout = TALITOS_TIMEOUT; 6529c4a7965SKim Phillips 6539c4a7965SKim Phillips setbits32(priv->reg + TALITOS_RNGURCR_LO, TALITOS_RNGURCR_LO_SR); 6549c4a7965SKim Phillips while (!(in_be32(priv->reg + TALITOS_RNGUSR_LO) & TALITOS_RNGUSR_LO_RD) 6559c4a7965SKim Phillips && --timeout) 6569c4a7965SKim Phillips cpu_relax(); 6579c4a7965SKim Phillips if (timeout == 0) { 6589c4a7965SKim Phillips dev_err(dev, "failed to reset rng hw\n"); 6599c4a7965SKim Phillips return -ENODEV; 6609c4a7965SKim Phillips } 6619c4a7965SKim Phillips 6629c4a7965SKim Phillips /* start generating */ 6639c4a7965SKim Phillips setbits32(priv->reg + TALITOS_RNGUDSR_LO, 0); 6649c4a7965SKim Phillips 6659c4a7965SKim Phillips return 0; 6669c4a7965SKim Phillips } 6679c4a7965SKim Phillips 6689c4a7965SKim Phillips static int talitos_register_rng(struct device *dev) 6699c4a7965SKim Phillips { 6709c4a7965SKim Phillips struct talitos_private *priv = dev_get_drvdata(dev); 6719c4a7965SKim Phillips 6729c4a7965SKim Phillips priv->rng.name = dev_driver_string(dev), 6739c4a7965SKim Phillips priv->rng.init = talitos_rng_init, 6749c4a7965SKim Phillips priv->rng.data_present = talitos_rng_data_present, 6759c4a7965SKim Phillips priv->rng.data_read = talitos_rng_data_read, 6769c4a7965SKim Phillips priv->rng.priv = (unsigned long)dev; 6779c4a7965SKim Phillips 6789c4a7965SKim Phillips return hwrng_register(&priv->rng); 6799c4a7965SKim Phillips } 6809c4a7965SKim Phillips 6819c4a7965SKim Phillips static void talitos_unregister_rng(struct device *dev) 6829c4a7965SKim Phillips { 6839c4a7965SKim Phillips struct talitos_private *priv = dev_get_drvdata(dev); 6849c4a7965SKim Phillips 6859c4a7965SKim Phillips hwrng_unregister(&priv->rng); 6869c4a7965SKim Phillips } 6879c4a7965SKim Phillips 6889c4a7965SKim Phillips /* 6899c4a7965SKim Phillips * crypto alg 6909c4a7965SKim Phillips */ 6919c4a7965SKim Phillips #define TALITOS_CRA_PRIORITY 3000 6929c4a7965SKim Phillips #define TALITOS_MAX_KEY_SIZE 64 6933952f17eSLee Nipper #define TALITOS_MAX_IV_LENGTH 16 /* max of AES_BLOCK_SIZE, DES3_EDE_BLOCK_SIZE */ 69470bcaca7SLee Nipper 6953952f17eSLee Nipper #define MD5_DIGEST_SIZE 16 6969c4a7965SKim Phillips 6979c4a7965SKim Phillips struct talitos_ctx { 6989c4a7965SKim Phillips struct device *dev; 6999c4a7965SKim Phillips __be32 desc_hdr_template; 7009c4a7965SKim Phillips u8 key[TALITOS_MAX_KEY_SIZE]; 70170bcaca7SLee Nipper u8 iv[TALITOS_MAX_IV_LENGTH]; 7029c4a7965SKim Phillips unsigned int keylen; 7039c4a7965SKim Phillips unsigned int enckeylen; 7049c4a7965SKim Phillips unsigned int authkeylen; 7059c4a7965SKim Phillips unsigned int authsize; 7069c4a7965SKim Phillips }; 7079c4a7965SKim Phillips 70856af8cd4SLee Nipper static int aead_setauthsize(struct crypto_aead *authenc, 7099c4a7965SKim Phillips unsigned int authsize) 7109c4a7965SKim Phillips { 7119c4a7965SKim Phillips struct talitos_ctx *ctx = crypto_aead_ctx(authenc); 7129c4a7965SKim Phillips 7139c4a7965SKim Phillips ctx->authsize = authsize; 7149c4a7965SKim Phillips 7159c4a7965SKim Phillips return 0; 7169c4a7965SKim Phillips } 7179c4a7965SKim Phillips 71856af8cd4SLee Nipper static int aead_setkey(struct crypto_aead *authenc, 7199c4a7965SKim Phillips const u8 *key, unsigned int keylen) 7209c4a7965SKim Phillips { 7219c4a7965SKim Phillips struct talitos_ctx *ctx = crypto_aead_ctx(authenc); 7229c4a7965SKim Phillips struct rtattr *rta = (void *)key; 7239c4a7965SKim Phillips struct crypto_authenc_key_param *param; 7249c4a7965SKim Phillips unsigned int authkeylen; 7259c4a7965SKim Phillips unsigned int enckeylen; 7269c4a7965SKim Phillips 7279c4a7965SKim Phillips if (!RTA_OK(rta, keylen)) 7289c4a7965SKim Phillips goto badkey; 7299c4a7965SKim Phillips 7309c4a7965SKim Phillips if (rta->rta_type != CRYPTO_AUTHENC_KEYA_PARAM) 7319c4a7965SKim Phillips goto badkey; 7329c4a7965SKim Phillips 7339c4a7965SKim Phillips if (RTA_PAYLOAD(rta) < sizeof(*param)) 7349c4a7965SKim Phillips goto badkey; 7359c4a7965SKim Phillips 7369c4a7965SKim Phillips param = RTA_DATA(rta); 7379c4a7965SKim Phillips enckeylen = be32_to_cpu(param->enckeylen); 7389c4a7965SKim Phillips 7399c4a7965SKim Phillips key += RTA_ALIGN(rta->rta_len); 7409c4a7965SKim Phillips keylen -= RTA_ALIGN(rta->rta_len); 7419c4a7965SKim Phillips 7429c4a7965SKim Phillips if (keylen < enckeylen) 7439c4a7965SKim Phillips goto badkey; 7449c4a7965SKim Phillips 7459c4a7965SKim Phillips authkeylen = keylen - enckeylen; 7469c4a7965SKim Phillips 7479c4a7965SKim Phillips if (keylen > TALITOS_MAX_KEY_SIZE) 7489c4a7965SKim Phillips goto badkey; 7499c4a7965SKim Phillips 7509c4a7965SKim Phillips memcpy(&ctx->key, key, keylen); 7519c4a7965SKim Phillips 7529c4a7965SKim Phillips ctx->keylen = keylen; 7539c4a7965SKim Phillips ctx->enckeylen = enckeylen; 7549c4a7965SKim Phillips ctx->authkeylen = authkeylen; 7559c4a7965SKim Phillips 7569c4a7965SKim Phillips return 0; 7579c4a7965SKim Phillips 7589c4a7965SKim Phillips badkey: 7599c4a7965SKim Phillips crypto_aead_set_flags(authenc, CRYPTO_TFM_RES_BAD_KEY_LEN); 7609c4a7965SKim Phillips return -EINVAL; 7619c4a7965SKim Phillips } 7629c4a7965SKim Phillips 7639c4a7965SKim Phillips /* 76456af8cd4SLee Nipper * talitos_edesc - s/w-extended descriptor 7659c4a7965SKim Phillips * @src_nents: number of segments in input scatterlist 7669c4a7965SKim Phillips * @dst_nents: number of segments in output scatterlist 7679c4a7965SKim Phillips * @dma_len: length of dma mapped link_tbl space 7689c4a7965SKim Phillips * @dma_link_tbl: bus physical address of link_tbl 7699c4a7965SKim Phillips * @desc: h/w descriptor 7709c4a7965SKim Phillips * @link_tbl: input and output h/w link tables (if {src,dst}_nents > 1) 7719c4a7965SKim Phillips * 7729c4a7965SKim Phillips * if decrypting (with authcheck), or either one of src_nents or dst_nents 7739c4a7965SKim Phillips * is greater than 1, an integrity check value is concatenated to the end 7749c4a7965SKim Phillips * of link_tbl data 7759c4a7965SKim Phillips */ 77656af8cd4SLee Nipper struct talitos_edesc { 7779c4a7965SKim Phillips int src_nents; 7789c4a7965SKim Phillips int dst_nents; 7794de9d0b5SLee Nipper int src_is_chained; 7804de9d0b5SLee Nipper int dst_is_chained; 7819c4a7965SKim Phillips int dma_len; 7829c4a7965SKim Phillips dma_addr_t dma_link_tbl; 7839c4a7965SKim Phillips struct talitos_desc desc; 7849c4a7965SKim Phillips struct talitos_ptr link_tbl[0]; 7859c4a7965SKim Phillips }; 7869c4a7965SKim Phillips 7874de9d0b5SLee Nipper static int talitos_map_sg(struct device *dev, struct scatterlist *sg, 7884de9d0b5SLee Nipper unsigned int nents, enum dma_data_direction dir, 7894de9d0b5SLee Nipper int chained) 7904de9d0b5SLee Nipper { 7914de9d0b5SLee Nipper if (unlikely(chained)) 7924de9d0b5SLee Nipper while (sg) { 7934de9d0b5SLee Nipper dma_map_sg(dev, sg, 1, dir); 7944de9d0b5SLee Nipper sg = scatterwalk_sg_next(sg); 7954de9d0b5SLee Nipper } 7964de9d0b5SLee Nipper else 7974de9d0b5SLee Nipper dma_map_sg(dev, sg, nents, dir); 7984de9d0b5SLee Nipper return nents; 7994de9d0b5SLee Nipper } 8004de9d0b5SLee Nipper 8014de9d0b5SLee Nipper static void talitos_unmap_sg_chain(struct device *dev, struct scatterlist *sg, 8024de9d0b5SLee Nipper enum dma_data_direction dir) 8034de9d0b5SLee Nipper { 8044de9d0b5SLee Nipper while (sg) { 8054de9d0b5SLee Nipper dma_unmap_sg(dev, sg, 1, dir); 8064de9d0b5SLee Nipper sg = scatterwalk_sg_next(sg); 8074de9d0b5SLee Nipper } 8084de9d0b5SLee Nipper } 8094de9d0b5SLee Nipper 8104de9d0b5SLee Nipper static void talitos_sg_unmap(struct device *dev, 8114de9d0b5SLee Nipper struct talitos_edesc *edesc, 8124de9d0b5SLee Nipper struct scatterlist *src, 8134de9d0b5SLee Nipper struct scatterlist *dst) 8144de9d0b5SLee Nipper { 8154de9d0b5SLee Nipper unsigned int src_nents = edesc->src_nents ? : 1; 8164de9d0b5SLee Nipper unsigned int dst_nents = edesc->dst_nents ? : 1; 8174de9d0b5SLee Nipper 8184de9d0b5SLee Nipper if (src != dst) { 8194de9d0b5SLee Nipper if (edesc->src_is_chained) 8204de9d0b5SLee Nipper talitos_unmap_sg_chain(dev, src, DMA_TO_DEVICE); 8214de9d0b5SLee Nipper else 8224de9d0b5SLee Nipper dma_unmap_sg(dev, src, src_nents, DMA_TO_DEVICE); 8234de9d0b5SLee Nipper 8244de9d0b5SLee Nipper if (edesc->dst_is_chained) 8254de9d0b5SLee Nipper talitos_unmap_sg_chain(dev, dst, DMA_FROM_DEVICE); 8264de9d0b5SLee Nipper else 8274de9d0b5SLee Nipper dma_unmap_sg(dev, dst, dst_nents, DMA_FROM_DEVICE); 8284de9d0b5SLee Nipper } else 8294de9d0b5SLee Nipper if (edesc->src_is_chained) 8304de9d0b5SLee Nipper talitos_unmap_sg_chain(dev, src, DMA_BIDIRECTIONAL); 8314de9d0b5SLee Nipper else 8324de9d0b5SLee Nipper dma_unmap_sg(dev, src, src_nents, DMA_BIDIRECTIONAL); 8334de9d0b5SLee Nipper } 8344de9d0b5SLee Nipper 8359c4a7965SKim Phillips static void ipsec_esp_unmap(struct device *dev, 83656af8cd4SLee Nipper struct talitos_edesc *edesc, 8379c4a7965SKim Phillips struct aead_request *areq) 8389c4a7965SKim Phillips { 8399c4a7965SKim Phillips unmap_single_talitos_ptr(dev, &edesc->desc.ptr[6], DMA_FROM_DEVICE); 8409c4a7965SKim Phillips unmap_single_talitos_ptr(dev, &edesc->desc.ptr[3], DMA_TO_DEVICE); 8419c4a7965SKim Phillips unmap_single_talitos_ptr(dev, &edesc->desc.ptr[2], DMA_TO_DEVICE); 8429c4a7965SKim Phillips unmap_single_talitos_ptr(dev, &edesc->desc.ptr[0], DMA_TO_DEVICE); 8439c4a7965SKim Phillips 8449c4a7965SKim Phillips dma_unmap_sg(dev, areq->assoc, 1, DMA_TO_DEVICE); 8459c4a7965SKim Phillips 8464de9d0b5SLee Nipper talitos_sg_unmap(dev, edesc, areq->src, areq->dst); 8479c4a7965SKim Phillips 8489c4a7965SKim Phillips if (edesc->dma_len) 8499c4a7965SKim Phillips dma_unmap_single(dev, edesc->dma_link_tbl, edesc->dma_len, 8509c4a7965SKim Phillips DMA_BIDIRECTIONAL); 8519c4a7965SKim Phillips } 8529c4a7965SKim Phillips 8539c4a7965SKim Phillips /* 8549c4a7965SKim Phillips * ipsec_esp descriptor callbacks 8559c4a7965SKim Phillips */ 8569c4a7965SKim Phillips static void ipsec_esp_encrypt_done(struct device *dev, 8579c4a7965SKim Phillips struct talitos_desc *desc, void *context, 8589c4a7965SKim Phillips int err) 8599c4a7965SKim Phillips { 8609c4a7965SKim Phillips struct aead_request *areq = context; 8619c4a7965SKim Phillips struct crypto_aead *authenc = crypto_aead_reqtfm(areq); 8629c4a7965SKim Phillips struct talitos_ctx *ctx = crypto_aead_ctx(authenc); 86319bbbc63SKim Phillips struct talitos_edesc *edesc; 8649c4a7965SKim Phillips struct scatterlist *sg; 8659c4a7965SKim Phillips void *icvdata; 8669c4a7965SKim Phillips 86719bbbc63SKim Phillips edesc = container_of(desc, struct talitos_edesc, desc); 86819bbbc63SKim Phillips 8699c4a7965SKim Phillips ipsec_esp_unmap(dev, edesc, areq); 8709c4a7965SKim Phillips 8719c4a7965SKim Phillips /* copy the generated ICV to dst */ 8729c4a7965SKim Phillips if (edesc->dma_len) { 8739c4a7965SKim Phillips icvdata = &edesc->link_tbl[edesc->src_nents + 874f3c85bc1SLee Nipper edesc->dst_nents + 2]; 8759c4a7965SKim Phillips sg = sg_last(areq->dst, edesc->dst_nents); 8769c4a7965SKim Phillips memcpy((char *)sg_virt(sg) + sg->length - ctx->authsize, 8779c4a7965SKim Phillips icvdata, ctx->authsize); 8789c4a7965SKim Phillips } 8799c4a7965SKim Phillips 8809c4a7965SKim Phillips kfree(edesc); 8819c4a7965SKim Phillips 8829c4a7965SKim Phillips aead_request_complete(areq, err); 8839c4a7965SKim Phillips } 8849c4a7965SKim Phillips 885fe5720e2SKim Phillips static void ipsec_esp_decrypt_swauth_done(struct device *dev, 886e938e465SKim Phillips struct talitos_desc *desc, 887e938e465SKim Phillips void *context, int err) 8889c4a7965SKim Phillips { 8899c4a7965SKim Phillips struct aead_request *req = context; 8909c4a7965SKim Phillips struct crypto_aead *authenc = crypto_aead_reqtfm(req); 8919c4a7965SKim Phillips struct talitos_ctx *ctx = crypto_aead_ctx(authenc); 89219bbbc63SKim Phillips struct talitos_edesc *edesc; 8939c4a7965SKim Phillips struct scatterlist *sg; 8949c4a7965SKim Phillips void *icvdata; 8959c4a7965SKim Phillips 89619bbbc63SKim Phillips edesc = container_of(desc, struct talitos_edesc, desc); 89719bbbc63SKim Phillips 8989c4a7965SKim Phillips ipsec_esp_unmap(dev, edesc, req); 8999c4a7965SKim Phillips 9009c4a7965SKim Phillips if (!err) { 9019c4a7965SKim Phillips /* auth check */ 9029c4a7965SKim Phillips if (edesc->dma_len) 9039c4a7965SKim Phillips icvdata = &edesc->link_tbl[edesc->src_nents + 904f3c85bc1SLee Nipper edesc->dst_nents + 2]; 9059c4a7965SKim Phillips else 9069c4a7965SKim Phillips icvdata = &edesc->link_tbl[0]; 9079c4a7965SKim Phillips 9089c4a7965SKim Phillips sg = sg_last(req->dst, edesc->dst_nents ? : 1); 9099c4a7965SKim Phillips err = memcmp(icvdata, (char *)sg_virt(sg) + sg->length - 9109c4a7965SKim Phillips ctx->authsize, ctx->authsize) ? -EBADMSG : 0; 9119c4a7965SKim Phillips } 9129c4a7965SKim Phillips 9139c4a7965SKim Phillips kfree(edesc); 9149c4a7965SKim Phillips 9159c4a7965SKim Phillips aead_request_complete(req, err); 9169c4a7965SKim Phillips } 9179c4a7965SKim Phillips 918fe5720e2SKim Phillips static void ipsec_esp_decrypt_hwauth_done(struct device *dev, 919e938e465SKim Phillips struct talitos_desc *desc, 920e938e465SKim Phillips void *context, int err) 921fe5720e2SKim Phillips { 922fe5720e2SKim Phillips struct aead_request *req = context; 92319bbbc63SKim Phillips struct talitos_edesc *edesc; 92419bbbc63SKim Phillips 92519bbbc63SKim Phillips edesc = container_of(desc, struct talitos_edesc, desc); 926fe5720e2SKim Phillips 927fe5720e2SKim Phillips ipsec_esp_unmap(dev, edesc, req); 928fe5720e2SKim Phillips 929fe5720e2SKim Phillips /* check ICV auth status */ 930e938e465SKim Phillips if (!err && ((desc->hdr_lo & DESC_HDR_LO_ICCR1_MASK) != 931e938e465SKim Phillips DESC_HDR_LO_ICCR1_PASS)) 932fe5720e2SKim Phillips err = -EBADMSG; 933fe5720e2SKim Phillips 934fe5720e2SKim Phillips kfree(edesc); 935fe5720e2SKim Phillips 936fe5720e2SKim Phillips aead_request_complete(req, err); 937fe5720e2SKim Phillips } 938fe5720e2SKim Phillips 9399c4a7965SKim Phillips /* 9409c4a7965SKim Phillips * convert scatterlist to SEC h/w link table format 9419c4a7965SKim Phillips * stop at cryptlen bytes 9429c4a7965SKim Phillips */ 94370bcaca7SLee Nipper static int sg_to_link_tbl(struct scatterlist *sg, int sg_count, 9449c4a7965SKim Phillips int cryptlen, struct talitos_ptr *link_tbl_ptr) 9459c4a7965SKim Phillips { 94670bcaca7SLee Nipper int n_sg = sg_count; 94770bcaca7SLee Nipper 94870bcaca7SLee Nipper while (n_sg--) { 94981eb024cSKim Phillips to_talitos_ptr(link_tbl_ptr, sg_dma_address(sg)); 9509c4a7965SKim Phillips link_tbl_ptr->len = cpu_to_be16(sg_dma_len(sg)); 9519c4a7965SKim Phillips link_tbl_ptr->j_extent = 0; 9529c4a7965SKim Phillips link_tbl_ptr++; 9539c4a7965SKim Phillips cryptlen -= sg_dma_len(sg); 9544de9d0b5SLee Nipper sg = scatterwalk_sg_next(sg); 9559c4a7965SKim Phillips } 9569c4a7965SKim Phillips 95770bcaca7SLee Nipper /* adjust (decrease) last one (or two) entry's len to cryptlen */ 9589c4a7965SKim Phillips link_tbl_ptr--; 959c0e741d4SKim Phillips while (be16_to_cpu(link_tbl_ptr->len) <= (-cryptlen)) { 96070bcaca7SLee Nipper /* Empty this entry, and move to previous one */ 96170bcaca7SLee Nipper cryptlen += be16_to_cpu(link_tbl_ptr->len); 96270bcaca7SLee Nipper link_tbl_ptr->len = 0; 96370bcaca7SLee Nipper sg_count--; 96470bcaca7SLee Nipper link_tbl_ptr--; 96570bcaca7SLee Nipper } 9669c4a7965SKim Phillips link_tbl_ptr->len = cpu_to_be16(be16_to_cpu(link_tbl_ptr->len) 9679c4a7965SKim Phillips + cryptlen); 9689c4a7965SKim Phillips 9699c4a7965SKim Phillips /* tag end of link table */ 9709c4a7965SKim Phillips link_tbl_ptr->j_extent = DESC_PTR_LNKTBL_RETURN; 97170bcaca7SLee Nipper 97270bcaca7SLee Nipper return sg_count; 9739c4a7965SKim Phillips } 9749c4a7965SKim Phillips 9759c4a7965SKim Phillips /* 9769c4a7965SKim Phillips * fill in and submit ipsec_esp descriptor 9779c4a7965SKim Phillips */ 97856af8cd4SLee Nipper static int ipsec_esp(struct talitos_edesc *edesc, struct aead_request *areq, 9799c4a7965SKim Phillips u8 *giv, u64 seq, 9809c4a7965SKim Phillips void (*callback) (struct device *dev, 9819c4a7965SKim Phillips struct talitos_desc *desc, 9829c4a7965SKim Phillips void *context, int error)) 9839c4a7965SKim Phillips { 9849c4a7965SKim Phillips struct crypto_aead *aead = crypto_aead_reqtfm(areq); 9859c4a7965SKim Phillips struct talitos_ctx *ctx = crypto_aead_ctx(aead); 9869c4a7965SKim Phillips struct device *dev = ctx->dev; 9879c4a7965SKim Phillips struct talitos_desc *desc = &edesc->desc; 9889c4a7965SKim Phillips unsigned int cryptlen = areq->cryptlen; 9899c4a7965SKim Phillips unsigned int authsize = ctx->authsize; 990e41256f1SKim Phillips unsigned int ivsize = crypto_aead_ivsize(aead); 991fa86a267SKim Phillips int sg_count, ret; 992fe5720e2SKim Phillips int sg_link_tbl_len; 9939c4a7965SKim Phillips 9949c4a7965SKim Phillips /* hmac key */ 9959c4a7965SKim Phillips map_single_talitos_ptr(dev, &desc->ptr[0], ctx->authkeylen, &ctx->key, 9969c4a7965SKim Phillips 0, DMA_TO_DEVICE); 9979c4a7965SKim Phillips /* hmac data */ 998e41256f1SKim Phillips map_single_talitos_ptr(dev, &desc->ptr[1], areq->assoclen + ivsize, 999e41256f1SKim Phillips sg_virt(areq->assoc), 0, DMA_TO_DEVICE); 10009c4a7965SKim Phillips /* cipher iv */ 10019c4a7965SKim Phillips map_single_talitos_ptr(dev, &desc->ptr[2], ivsize, giv ?: areq->iv, 0, 10029c4a7965SKim Phillips DMA_TO_DEVICE); 10039c4a7965SKim Phillips 10049c4a7965SKim Phillips /* cipher key */ 10059c4a7965SKim Phillips map_single_talitos_ptr(dev, &desc->ptr[3], ctx->enckeylen, 10069c4a7965SKim Phillips (char *)&ctx->key + ctx->authkeylen, 0, 10079c4a7965SKim Phillips DMA_TO_DEVICE); 10089c4a7965SKim Phillips 10099c4a7965SKim Phillips /* 10109c4a7965SKim Phillips * cipher in 10119c4a7965SKim Phillips * map and adjust cipher len to aead request cryptlen. 10129c4a7965SKim Phillips * extent is bytes of HMAC postpended to ciphertext, 10139c4a7965SKim Phillips * typically 12 for ipsec 10149c4a7965SKim Phillips */ 10159c4a7965SKim Phillips desc->ptr[4].len = cpu_to_be16(cryptlen); 10169c4a7965SKim Phillips desc->ptr[4].j_extent = authsize; 10179c4a7965SKim Phillips 1018e938e465SKim Phillips sg_count = talitos_map_sg(dev, areq->src, edesc->src_nents ? : 1, 1019e938e465SKim Phillips (areq->src == areq->dst) ? DMA_BIDIRECTIONAL 1020e938e465SKim Phillips : DMA_TO_DEVICE, 10214de9d0b5SLee Nipper edesc->src_is_chained); 10229c4a7965SKim Phillips 10239c4a7965SKim Phillips if (sg_count == 1) { 102481eb024cSKim Phillips to_talitos_ptr(&desc->ptr[4], sg_dma_address(areq->src)); 10259c4a7965SKim Phillips } else { 1026fe5720e2SKim Phillips sg_link_tbl_len = cryptlen; 1027fe5720e2SKim Phillips 1028962a9c99SKim Phillips if (edesc->desc.hdr & DESC_HDR_MODE1_MDEU_CICV) 1029fe5720e2SKim Phillips sg_link_tbl_len = cryptlen + authsize; 1030e938e465SKim Phillips 1031fe5720e2SKim Phillips sg_count = sg_to_link_tbl(areq->src, sg_count, sg_link_tbl_len, 10329c4a7965SKim Phillips &edesc->link_tbl[0]); 103370bcaca7SLee Nipper if (sg_count > 1) { 10349c4a7965SKim Phillips desc->ptr[4].j_extent |= DESC_PTR_LNKTBL_JUMP; 103581eb024cSKim Phillips to_talitos_ptr(&desc->ptr[4], edesc->dma_link_tbl); 1036e938e465SKim Phillips dma_sync_single_for_device(dev, edesc->dma_link_tbl, 1037e938e465SKim Phillips edesc->dma_len, 1038e938e465SKim Phillips DMA_BIDIRECTIONAL); 103970bcaca7SLee Nipper } else { 104070bcaca7SLee Nipper /* Only one segment now, so no link tbl needed */ 104181eb024cSKim Phillips to_talitos_ptr(&desc->ptr[4], 104281eb024cSKim Phillips sg_dma_address(areq->src)); 104370bcaca7SLee Nipper } 10449c4a7965SKim Phillips } 10459c4a7965SKim Phillips 10469c4a7965SKim Phillips /* cipher out */ 10479c4a7965SKim Phillips desc->ptr[5].len = cpu_to_be16(cryptlen); 10489c4a7965SKim Phillips desc->ptr[5].j_extent = authsize; 10499c4a7965SKim Phillips 1050e938e465SKim Phillips if (areq->src != areq->dst) 10514de9d0b5SLee Nipper sg_count = talitos_map_sg(dev, areq->dst, 10524de9d0b5SLee Nipper edesc->dst_nents ? : 1, 10534de9d0b5SLee Nipper DMA_FROM_DEVICE, 10544de9d0b5SLee Nipper edesc->dst_is_chained); 10559c4a7965SKim Phillips 10569c4a7965SKim Phillips if (sg_count == 1) { 105781eb024cSKim Phillips to_talitos_ptr(&desc->ptr[5], sg_dma_address(areq->dst)); 10589c4a7965SKim Phillips } else { 10599c4a7965SKim Phillips struct talitos_ptr *link_tbl_ptr = 1060f3c85bc1SLee Nipper &edesc->link_tbl[edesc->src_nents + 1]; 10619c4a7965SKim Phillips 106281eb024cSKim Phillips to_talitos_ptr(&desc->ptr[5], edesc->dma_link_tbl + 106381eb024cSKim Phillips (edesc->src_nents + 1) * 106481eb024cSKim Phillips sizeof(struct talitos_ptr)); 106570bcaca7SLee Nipper sg_count = sg_to_link_tbl(areq->dst, sg_count, cryptlen, 10669c4a7965SKim Phillips link_tbl_ptr); 1067fe5720e2SKim Phillips 1068f3c85bc1SLee Nipper /* Add an entry to the link table for ICV data */ 10699c4a7965SKim Phillips link_tbl_ptr += sg_count - 1; 10709c4a7965SKim Phillips link_tbl_ptr->j_extent = 0; 1071f3c85bc1SLee Nipper sg_count++; 10729c4a7965SKim Phillips link_tbl_ptr++; 10739c4a7965SKim Phillips link_tbl_ptr->j_extent = DESC_PTR_LNKTBL_RETURN; 10749c4a7965SKim Phillips link_tbl_ptr->len = cpu_to_be16(authsize); 10759c4a7965SKim Phillips 10769c4a7965SKim Phillips /* icv data follows link tables */ 107781eb024cSKim Phillips to_talitos_ptr(link_tbl_ptr, edesc->dma_link_tbl + 107881eb024cSKim Phillips (edesc->src_nents + edesc->dst_nents + 2) * 107981eb024cSKim Phillips sizeof(struct talitos_ptr)); 10809c4a7965SKim Phillips desc->ptr[5].j_extent |= DESC_PTR_LNKTBL_JUMP; 10819c4a7965SKim Phillips dma_sync_single_for_device(ctx->dev, edesc->dma_link_tbl, 10829c4a7965SKim Phillips edesc->dma_len, DMA_BIDIRECTIONAL); 10839c4a7965SKim Phillips } 10849c4a7965SKim Phillips 10859c4a7965SKim Phillips /* iv out */ 10869c4a7965SKim Phillips map_single_talitos_ptr(dev, &desc->ptr[6], ivsize, ctx->iv, 0, 10879c4a7965SKim Phillips DMA_FROM_DEVICE); 10889c4a7965SKim Phillips 1089fa86a267SKim Phillips ret = talitos_submit(dev, desc, callback, areq); 1090fa86a267SKim Phillips if (ret != -EINPROGRESS) { 1091fa86a267SKim Phillips ipsec_esp_unmap(dev, edesc, areq); 1092fa86a267SKim Phillips kfree(edesc); 1093fa86a267SKim Phillips } 1094fa86a267SKim Phillips return ret; 10959c4a7965SKim Phillips } 10969c4a7965SKim Phillips 10979c4a7965SKim Phillips /* 10989c4a7965SKim Phillips * derive number of elements in scatterlist 10999c4a7965SKim Phillips */ 11004de9d0b5SLee Nipper static int sg_count(struct scatterlist *sg_list, int nbytes, int *chained) 11019c4a7965SKim Phillips { 11029c4a7965SKim Phillips struct scatterlist *sg = sg_list; 11039c4a7965SKim Phillips int sg_nents = 0; 11049c4a7965SKim Phillips 11054de9d0b5SLee Nipper *chained = 0; 11064de9d0b5SLee Nipper while (nbytes > 0) { 11079c4a7965SKim Phillips sg_nents++; 11089c4a7965SKim Phillips nbytes -= sg->length; 11094de9d0b5SLee Nipper if (!sg_is_last(sg) && (sg + 1)->length == 0) 11104de9d0b5SLee Nipper *chained = 1; 11114de9d0b5SLee Nipper sg = scatterwalk_sg_next(sg); 11129c4a7965SKim Phillips } 11139c4a7965SKim Phillips 11149c4a7965SKim Phillips return sg_nents; 11159c4a7965SKim Phillips } 11169c4a7965SKim Phillips 11179c4a7965SKim Phillips /* 111856af8cd4SLee Nipper * allocate and map the extended descriptor 11199c4a7965SKim Phillips */ 11204de9d0b5SLee Nipper static struct talitos_edesc *talitos_edesc_alloc(struct device *dev, 11214de9d0b5SLee Nipper struct scatterlist *src, 11224de9d0b5SLee Nipper struct scatterlist *dst, 11234de9d0b5SLee Nipper unsigned int cryptlen, 11244de9d0b5SLee Nipper unsigned int authsize, 11254de9d0b5SLee Nipper int icv_stashing, 11264de9d0b5SLee Nipper u32 cryptoflags) 11279c4a7965SKim Phillips { 112856af8cd4SLee Nipper struct talitos_edesc *edesc; 11299c4a7965SKim Phillips int src_nents, dst_nents, alloc_len, dma_len; 11304de9d0b5SLee Nipper int src_chained, dst_chained = 0; 11314de9d0b5SLee Nipper gfp_t flags = cryptoflags & CRYPTO_TFM_REQ_MAY_SLEEP ? GFP_KERNEL : 1132586725f8SKim Phillips GFP_ATOMIC; 11339c4a7965SKim Phillips 11344de9d0b5SLee Nipper if (cryptlen + authsize > TALITOS_MAX_DATA_LEN) { 11354de9d0b5SLee Nipper dev_err(dev, "length exceeds h/w max limit\n"); 11369c4a7965SKim Phillips return ERR_PTR(-EINVAL); 11379c4a7965SKim Phillips } 11389c4a7965SKim Phillips 11394de9d0b5SLee Nipper src_nents = sg_count(src, cryptlen + authsize, &src_chained); 11409c4a7965SKim Phillips src_nents = (src_nents == 1) ? 0 : src_nents; 11419c4a7965SKim Phillips 11424de9d0b5SLee Nipper if (dst == src) { 11439c4a7965SKim Phillips dst_nents = src_nents; 11449c4a7965SKim Phillips } else { 11454de9d0b5SLee Nipper dst_nents = sg_count(dst, cryptlen + authsize, &dst_chained); 1146695ad589SLee Nipper dst_nents = (dst_nents == 1) ? 0 : dst_nents; 11479c4a7965SKim Phillips } 11489c4a7965SKim Phillips 11499c4a7965SKim Phillips /* 11509c4a7965SKim Phillips * allocate space for base edesc plus the link tables, 1151f3c85bc1SLee Nipper * allowing for two separate entries for ICV and generated ICV (+ 2), 11529c4a7965SKim Phillips * and the ICV data itself 11539c4a7965SKim Phillips */ 115456af8cd4SLee Nipper alloc_len = sizeof(struct talitos_edesc); 11559c4a7965SKim Phillips if (src_nents || dst_nents) { 1156f3c85bc1SLee Nipper dma_len = (src_nents + dst_nents + 2) * 11574de9d0b5SLee Nipper sizeof(struct talitos_ptr) + authsize; 11589c4a7965SKim Phillips alloc_len += dma_len; 11599c4a7965SKim Phillips } else { 11609c4a7965SKim Phillips dma_len = 0; 11614de9d0b5SLee Nipper alloc_len += icv_stashing ? authsize : 0; 11629c4a7965SKim Phillips } 11639c4a7965SKim Phillips 1164586725f8SKim Phillips edesc = kmalloc(alloc_len, GFP_DMA | flags); 11659c4a7965SKim Phillips if (!edesc) { 11664de9d0b5SLee Nipper dev_err(dev, "could not allocate edescriptor\n"); 11679c4a7965SKim Phillips return ERR_PTR(-ENOMEM); 11689c4a7965SKim Phillips } 11699c4a7965SKim Phillips 11709c4a7965SKim Phillips edesc->src_nents = src_nents; 11719c4a7965SKim Phillips edesc->dst_nents = dst_nents; 11724de9d0b5SLee Nipper edesc->src_is_chained = src_chained; 11734de9d0b5SLee Nipper edesc->dst_is_chained = dst_chained; 11749c4a7965SKim Phillips edesc->dma_len = dma_len; 11754de9d0b5SLee Nipper edesc->dma_link_tbl = dma_map_single(dev, &edesc->link_tbl[0], 11769c4a7965SKim Phillips edesc->dma_len, DMA_BIDIRECTIONAL); 11779c4a7965SKim Phillips 11789c4a7965SKim Phillips return edesc; 11799c4a7965SKim Phillips } 11809c4a7965SKim Phillips 11814de9d0b5SLee Nipper static struct talitos_edesc *aead_edesc_alloc(struct aead_request *areq, 11824de9d0b5SLee Nipper int icv_stashing) 11834de9d0b5SLee Nipper { 11844de9d0b5SLee Nipper struct crypto_aead *authenc = crypto_aead_reqtfm(areq); 11854de9d0b5SLee Nipper struct talitos_ctx *ctx = crypto_aead_ctx(authenc); 11864de9d0b5SLee Nipper 11874de9d0b5SLee Nipper return talitos_edesc_alloc(ctx->dev, areq->src, areq->dst, 11884de9d0b5SLee Nipper areq->cryptlen, ctx->authsize, icv_stashing, 11894de9d0b5SLee Nipper areq->base.flags); 11904de9d0b5SLee Nipper } 11914de9d0b5SLee Nipper 119256af8cd4SLee Nipper static int aead_encrypt(struct aead_request *req) 11939c4a7965SKim Phillips { 11949c4a7965SKim Phillips struct crypto_aead *authenc = crypto_aead_reqtfm(req); 11959c4a7965SKim Phillips struct talitos_ctx *ctx = crypto_aead_ctx(authenc); 119656af8cd4SLee Nipper struct talitos_edesc *edesc; 11979c4a7965SKim Phillips 11989c4a7965SKim Phillips /* allocate extended descriptor */ 11994de9d0b5SLee Nipper edesc = aead_edesc_alloc(req, 0); 12009c4a7965SKim Phillips if (IS_ERR(edesc)) 12019c4a7965SKim Phillips return PTR_ERR(edesc); 12029c4a7965SKim Phillips 12039c4a7965SKim Phillips /* set encrypt */ 120470bcaca7SLee Nipper edesc->desc.hdr = ctx->desc_hdr_template | DESC_HDR_MODE0_ENCRYPT; 12059c4a7965SKim Phillips 12069c4a7965SKim Phillips return ipsec_esp(edesc, req, NULL, 0, ipsec_esp_encrypt_done); 12079c4a7965SKim Phillips } 12089c4a7965SKim Phillips 120956af8cd4SLee Nipper static int aead_decrypt(struct aead_request *req) 12109c4a7965SKim Phillips { 12119c4a7965SKim Phillips struct crypto_aead *authenc = crypto_aead_reqtfm(req); 12129c4a7965SKim Phillips struct talitos_ctx *ctx = crypto_aead_ctx(authenc); 12139c4a7965SKim Phillips unsigned int authsize = ctx->authsize; 1214fe5720e2SKim Phillips struct talitos_private *priv = dev_get_drvdata(ctx->dev); 121556af8cd4SLee Nipper struct talitos_edesc *edesc; 12169c4a7965SKim Phillips struct scatterlist *sg; 12179c4a7965SKim Phillips void *icvdata; 12189c4a7965SKim Phillips 12199c4a7965SKim Phillips req->cryptlen -= authsize; 12209c4a7965SKim Phillips 12219c4a7965SKim Phillips /* allocate extended descriptor */ 12224de9d0b5SLee Nipper edesc = aead_edesc_alloc(req, 1); 12239c4a7965SKim Phillips if (IS_ERR(edesc)) 12249c4a7965SKim Phillips return PTR_ERR(edesc); 12259c4a7965SKim Phillips 1226fe5720e2SKim Phillips if ((priv->features & TALITOS_FTR_HW_AUTH_CHECK) && 1227e938e465SKim Phillips ((!edesc->src_nents && !edesc->dst_nents) || 1228e938e465SKim Phillips priv->features & TALITOS_FTR_SRC_LINK_TBL_LEN_INCLUDES_EXTENT)) { 1229fe5720e2SKim Phillips 1230fe5720e2SKim Phillips /* decrypt and check the ICV */ 1231e938e465SKim Phillips edesc->desc.hdr = ctx->desc_hdr_template | 1232e938e465SKim Phillips DESC_HDR_DIR_INBOUND | 1233fe5720e2SKim Phillips DESC_HDR_MODE1_MDEU_CICV; 1234fe5720e2SKim Phillips 1235fe5720e2SKim Phillips /* reset integrity check result bits */ 1236fe5720e2SKim Phillips edesc->desc.hdr_lo = 0; 1237fe5720e2SKim Phillips 1238e938e465SKim Phillips return ipsec_esp(edesc, req, NULL, 0, 1239e938e465SKim Phillips ipsec_esp_decrypt_hwauth_done); 1240fe5720e2SKim Phillips 1241e938e465SKim Phillips } 1242fe5720e2SKim Phillips 1243fe5720e2SKim Phillips /* Have to check the ICV with software */ 1244fe5720e2SKim Phillips edesc->desc.hdr = ctx->desc_hdr_template | DESC_HDR_DIR_INBOUND; 1245fe5720e2SKim Phillips 12469c4a7965SKim Phillips /* stash incoming ICV for later cmp with ICV generated by the h/w */ 12479c4a7965SKim Phillips if (edesc->dma_len) 12489c4a7965SKim Phillips icvdata = &edesc->link_tbl[edesc->src_nents + 1249f3c85bc1SLee Nipper edesc->dst_nents + 2]; 12509c4a7965SKim Phillips else 12519c4a7965SKim Phillips icvdata = &edesc->link_tbl[0]; 12529c4a7965SKim Phillips 12539c4a7965SKim Phillips sg = sg_last(req->src, edesc->src_nents ? : 1); 12549c4a7965SKim Phillips 12559c4a7965SKim Phillips memcpy(icvdata, (char *)sg_virt(sg) + sg->length - ctx->authsize, 12569c4a7965SKim Phillips ctx->authsize); 12579c4a7965SKim Phillips 1258fe5720e2SKim Phillips return ipsec_esp(edesc, req, NULL, 0, ipsec_esp_decrypt_swauth_done); 1259fe5720e2SKim Phillips } 12609c4a7965SKim Phillips 126156af8cd4SLee Nipper static int aead_givencrypt(struct aead_givcrypt_request *req) 12629c4a7965SKim Phillips { 12639c4a7965SKim Phillips struct aead_request *areq = &req->areq; 12649c4a7965SKim Phillips struct crypto_aead *authenc = crypto_aead_reqtfm(areq); 12659c4a7965SKim Phillips struct talitos_ctx *ctx = crypto_aead_ctx(authenc); 126656af8cd4SLee Nipper struct talitos_edesc *edesc; 12679c4a7965SKim Phillips 12689c4a7965SKim Phillips /* allocate extended descriptor */ 12694de9d0b5SLee Nipper edesc = aead_edesc_alloc(areq, 0); 12709c4a7965SKim Phillips if (IS_ERR(edesc)) 12719c4a7965SKim Phillips return PTR_ERR(edesc); 12729c4a7965SKim Phillips 12739c4a7965SKim Phillips /* set encrypt */ 127470bcaca7SLee Nipper edesc->desc.hdr = ctx->desc_hdr_template | DESC_HDR_MODE0_ENCRYPT; 12759c4a7965SKim Phillips 12769c4a7965SKim Phillips memcpy(req->giv, ctx->iv, crypto_aead_ivsize(authenc)); 1277ba95487dSKim Phillips /* avoid consecutive packets going out with same IV */ 1278ba95487dSKim Phillips *(__be64 *)req->giv ^= cpu_to_be64(req->seq); 12799c4a7965SKim Phillips 12809c4a7965SKim Phillips return ipsec_esp(edesc, areq, req->giv, req->seq, 12819c4a7965SKim Phillips ipsec_esp_encrypt_done); 12829c4a7965SKim Phillips } 12839c4a7965SKim Phillips 12844de9d0b5SLee Nipper static int ablkcipher_setkey(struct crypto_ablkcipher *cipher, 12854de9d0b5SLee Nipper const u8 *key, unsigned int keylen) 12864de9d0b5SLee Nipper { 12874de9d0b5SLee Nipper struct talitos_ctx *ctx = crypto_ablkcipher_ctx(cipher); 12884de9d0b5SLee Nipper struct ablkcipher_alg *alg = crypto_ablkcipher_alg(cipher); 12894de9d0b5SLee Nipper 12904de9d0b5SLee Nipper if (keylen > TALITOS_MAX_KEY_SIZE) 12914de9d0b5SLee Nipper goto badkey; 12924de9d0b5SLee Nipper 12934de9d0b5SLee Nipper if (keylen < alg->min_keysize || keylen > alg->max_keysize) 12944de9d0b5SLee Nipper goto badkey; 12954de9d0b5SLee Nipper 12964de9d0b5SLee Nipper memcpy(&ctx->key, key, keylen); 12974de9d0b5SLee Nipper ctx->keylen = keylen; 12984de9d0b5SLee Nipper 12994de9d0b5SLee Nipper return 0; 13004de9d0b5SLee Nipper 13014de9d0b5SLee Nipper badkey: 13024de9d0b5SLee Nipper crypto_ablkcipher_set_flags(cipher, CRYPTO_TFM_RES_BAD_KEY_LEN); 13034de9d0b5SLee Nipper return -EINVAL; 13044de9d0b5SLee Nipper } 13054de9d0b5SLee Nipper 13064de9d0b5SLee Nipper static void common_nonsnoop_unmap(struct device *dev, 13074de9d0b5SLee Nipper struct talitos_edesc *edesc, 13084de9d0b5SLee Nipper struct ablkcipher_request *areq) 13094de9d0b5SLee Nipper { 13104de9d0b5SLee Nipper unmap_single_talitos_ptr(dev, &edesc->desc.ptr[5], DMA_FROM_DEVICE); 13114de9d0b5SLee Nipper unmap_single_talitos_ptr(dev, &edesc->desc.ptr[2], DMA_TO_DEVICE); 13124de9d0b5SLee Nipper unmap_single_talitos_ptr(dev, &edesc->desc.ptr[1], DMA_TO_DEVICE); 13134de9d0b5SLee Nipper 13144de9d0b5SLee Nipper talitos_sg_unmap(dev, edesc, areq->src, areq->dst); 13154de9d0b5SLee Nipper 13164de9d0b5SLee Nipper if (edesc->dma_len) 13174de9d0b5SLee Nipper dma_unmap_single(dev, edesc->dma_link_tbl, edesc->dma_len, 13184de9d0b5SLee Nipper DMA_BIDIRECTIONAL); 13194de9d0b5SLee Nipper } 13204de9d0b5SLee Nipper 13214de9d0b5SLee Nipper static void ablkcipher_done(struct device *dev, 13224de9d0b5SLee Nipper struct talitos_desc *desc, void *context, 13234de9d0b5SLee Nipper int err) 13244de9d0b5SLee Nipper { 13254de9d0b5SLee Nipper struct ablkcipher_request *areq = context; 132619bbbc63SKim Phillips struct talitos_edesc *edesc; 132719bbbc63SKim Phillips 132819bbbc63SKim Phillips edesc = container_of(desc, struct talitos_edesc, desc); 13294de9d0b5SLee Nipper 13304de9d0b5SLee Nipper common_nonsnoop_unmap(dev, edesc, areq); 13314de9d0b5SLee Nipper 13324de9d0b5SLee Nipper kfree(edesc); 13334de9d0b5SLee Nipper 13344de9d0b5SLee Nipper areq->base.complete(&areq->base, err); 13354de9d0b5SLee Nipper } 13364de9d0b5SLee Nipper 13374de9d0b5SLee Nipper static int common_nonsnoop(struct talitos_edesc *edesc, 13384de9d0b5SLee Nipper struct ablkcipher_request *areq, 13394de9d0b5SLee Nipper u8 *giv, 13404de9d0b5SLee Nipper void (*callback) (struct device *dev, 13414de9d0b5SLee Nipper struct talitos_desc *desc, 13424de9d0b5SLee Nipper void *context, int error)) 13434de9d0b5SLee Nipper { 13444de9d0b5SLee Nipper struct crypto_ablkcipher *cipher = crypto_ablkcipher_reqtfm(areq); 13454de9d0b5SLee Nipper struct talitos_ctx *ctx = crypto_ablkcipher_ctx(cipher); 13464de9d0b5SLee Nipper struct device *dev = ctx->dev; 13474de9d0b5SLee Nipper struct talitos_desc *desc = &edesc->desc; 13484de9d0b5SLee Nipper unsigned int cryptlen = areq->nbytes; 13494de9d0b5SLee Nipper unsigned int ivsize; 13504de9d0b5SLee Nipper int sg_count, ret; 13514de9d0b5SLee Nipper 13524de9d0b5SLee Nipper /* first DWORD empty */ 13534de9d0b5SLee Nipper desc->ptr[0].len = 0; 135481eb024cSKim Phillips to_talitos_ptr(&desc->ptr[0], 0); 13554de9d0b5SLee Nipper desc->ptr[0].j_extent = 0; 13564de9d0b5SLee Nipper 13574de9d0b5SLee Nipper /* cipher iv */ 13584de9d0b5SLee Nipper ivsize = crypto_ablkcipher_ivsize(cipher); 13594de9d0b5SLee Nipper map_single_talitos_ptr(dev, &desc->ptr[1], ivsize, giv ?: areq->info, 0, 13604de9d0b5SLee Nipper DMA_TO_DEVICE); 13614de9d0b5SLee Nipper 13624de9d0b5SLee Nipper /* cipher key */ 13634de9d0b5SLee Nipper map_single_talitos_ptr(dev, &desc->ptr[2], ctx->keylen, 13644de9d0b5SLee Nipper (char *)&ctx->key, 0, DMA_TO_DEVICE); 13654de9d0b5SLee Nipper 13664de9d0b5SLee Nipper /* 13674de9d0b5SLee Nipper * cipher in 13684de9d0b5SLee Nipper */ 13694de9d0b5SLee Nipper desc->ptr[3].len = cpu_to_be16(cryptlen); 13704de9d0b5SLee Nipper desc->ptr[3].j_extent = 0; 13714de9d0b5SLee Nipper 13724de9d0b5SLee Nipper sg_count = talitos_map_sg(dev, areq->src, edesc->src_nents ? : 1, 13734de9d0b5SLee Nipper (areq->src == areq->dst) ? DMA_BIDIRECTIONAL 13744de9d0b5SLee Nipper : DMA_TO_DEVICE, 13754de9d0b5SLee Nipper edesc->src_is_chained); 13764de9d0b5SLee Nipper 13774de9d0b5SLee Nipper if (sg_count == 1) { 137881eb024cSKim Phillips to_talitos_ptr(&desc->ptr[3], sg_dma_address(areq->src)); 13794de9d0b5SLee Nipper } else { 13804de9d0b5SLee Nipper sg_count = sg_to_link_tbl(areq->src, sg_count, cryptlen, 13814de9d0b5SLee Nipper &edesc->link_tbl[0]); 13824de9d0b5SLee Nipper if (sg_count > 1) { 138381eb024cSKim Phillips to_talitos_ptr(&desc->ptr[3], edesc->dma_link_tbl); 13844de9d0b5SLee Nipper desc->ptr[3].j_extent |= DESC_PTR_LNKTBL_JUMP; 1385e938e465SKim Phillips dma_sync_single_for_device(dev, edesc->dma_link_tbl, 1386e938e465SKim Phillips edesc->dma_len, 1387e938e465SKim Phillips DMA_BIDIRECTIONAL); 13884de9d0b5SLee Nipper } else { 13894de9d0b5SLee Nipper /* Only one segment now, so no link tbl needed */ 139081eb024cSKim Phillips to_talitos_ptr(&desc->ptr[3], 139181eb024cSKim Phillips sg_dma_address(areq->src)); 13924de9d0b5SLee Nipper } 13934de9d0b5SLee Nipper } 13944de9d0b5SLee Nipper 13954de9d0b5SLee Nipper /* cipher out */ 13964de9d0b5SLee Nipper desc->ptr[4].len = cpu_to_be16(cryptlen); 13974de9d0b5SLee Nipper desc->ptr[4].j_extent = 0; 13984de9d0b5SLee Nipper 13994de9d0b5SLee Nipper if (areq->src != areq->dst) 14004de9d0b5SLee Nipper sg_count = talitos_map_sg(dev, areq->dst, 14014de9d0b5SLee Nipper edesc->dst_nents ? : 1, 14024de9d0b5SLee Nipper DMA_FROM_DEVICE, 14034de9d0b5SLee Nipper edesc->dst_is_chained); 14044de9d0b5SLee Nipper 14054de9d0b5SLee Nipper if (sg_count == 1) { 140681eb024cSKim Phillips to_talitos_ptr(&desc->ptr[4], sg_dma_address(areq->dst)); 14074de9d0b5SLee Nipper } else { 14084de9d0b5SLee Nipper struct talitos_ptr *link_tbl_ptr = 14094de9d0b5SLee Nipper &edesc->link_tbl[edesc->src_nents + 1]; 14104de9d0b5SLee Nipper 141181eb024cSKim Phillips to_talitos_ptr(&desc->ptr[4], edesc->dma_link_tbl + 141281eb024cSKim Phillips (edesc->src_nents + 1) * 141381eb024cSKim Phillips sizeof(struct talitos_ptr)); 14144de9d0b5SLee Nipper desc->ptr[4].j_extent |= DESC_PTR_LNKTBL_JUMP; 14154de9d0b5SLee Nipper sg_count = sg_to_link_tbl(areq->dst, sg_count, cryptlen, 14164de9d0b5SLee Nipper link_tbl_ptr); 14174de9d0b5SLee Nipper dma_sync_single_for_device(ctx->dev, edesc->dma_link_tbl, 14184de9d0b5SLee Nipper edesc->dma_len, DMA_BIDIRECTIONAL); 14194de9d0b5SLee Nipper } 14204de9d0b5SLee Nipper 14214de9d0b5SLee Nipper /* iv out */ 14224de9d0b5SLee Nipper map_single_talitos_ptr(dev, &desc->ptr[5], ivsize, ctx->iv, 0, 14234de9d0b5SLee Nipper DMA_FROM_DEVICE); 14244de9d0b5SLee Nipper 14254de9d0b5SLee Nipper /* last DWORD empty */ 14264de9d0b5SLee Nipper desc->ptr[6].len = 0; 142781eb024cSKim Phillips to_talitos_ptr(&desc->ptr[6], 0); 14284de9d0b5SLee Nipper desc->ptr[6].j_extent = 0; 14294de9d0b5SLee Nipper 14304de9d0b5SLee Nipper ret = talitos_submit(dev, desc, callback, areq); 14314de9d0b5SLee Nipper if (ret != -EINPROGRESS) { 14324de9d0b5SLee Nipper common_nonsnoop_unmap(dev, edesc, areq); 14334de9d0b5SLee Nipper kfree(edesc); 14344de9d0b5SLee Nipper } 14354de9d0b5SLee Nipper return ret; 14364de9d0b5SLee Nipper } 14374de9d0b5SLee Nipper 1438e938e465SKim Phillips static struct talitos_edesc *ablkcipher_edesc_alloc(struct ablkcipher_request * 1439e938e465SKim Phillips areq) 14404de9d0b5SLee Nipper { 14414de9d0b5SLee Nipper struct crypto_ablkcipher *cipher = crypto_ablkcipher_reqtfm(areq); 14424de9d0b5SLee Nipper struct talitos_ctx *ctx = crypto_ablkcipher_ctx(cipher); 14434de9d0b5SLee Nipper 14444de9d0b5SLee Nipper return talitos_edesc_alloc(ctx->dev, areq->src, areq->dst, areq->nbytes, 14454de9d0b5SLee Nipper 0, 0, areq->base.flags); 14464de9d0b5SLee Nipper } 14474de9d0b5SLee Nipper 14484de9d0b5SLee Nipper static int ablkcipher_encrypt(struct ablkcipher_request *areq) 14494de9d0b5SLee Nipper { 14504de9d0b5SLee Nipper struct crypto_ablkcipher *cipher = crypto_ablkcipher_reqtfm(areq); 14514de9d0b5SLee Nipper struct talitos_ctx *ctx = crypto_ablkcipher_ctx(cipher); 14524de9d0b5SLee Nipper struct talitos_edesc *edesc; 14534de9d0b5SLee Nipper 14544de9d0b5SLee Nipper /* allocate extended descriptor */ 14554de9d0b5SLee Nipper edesc = ablkcipher_edesc_alloc(areq); 14564de9d0b5SLee Nipper if (IS_ERR(edesc)) 14574de9d0b5SLee Nipper return PTR_ERR(edesc); 14584de9d0b5SLee Nipper 14594de9d0b5SLee Nipper /* set encrypt */ 14604de9d0b5SLee Nipper edesc->desc.hdr = ctx->desc_hdr_template | DESC_HDR_MODE0_ENCRYPT; 14614de9d0b5SLee Nipper 14624de9d0b5SLee Nipper return common_nonsnoop(edesc, areq, NULL, ablkcipher_done); 14634de9d0b5SLee Nipper } 14644de9d0b5SLee Nipper 14654de9d0b5SLee Nipper static int ablkcipher_decrypt(struct ablkcipher_request *areq) 14664de9d0b5SLee Nipper { 14674de9d0b5SLee Nipper struct crypto_ablkcipher *cipher = crypto_ablkcipher_reqtfm(areq); 14684de9d0b5SLee Nipper struct talitos_ctx *ctx = crypto_ablkcipher_ctx(cipher); 14694de9d0b5SLee Nipper struct talitos_edesc *edesc; 14704de9d0b5SLee Nipper 14714de9d0b5SLee Nipper /* allocate extended descriptor */ 14724de9d0b5SLee Nipper edesc = ablkcipher_edesc_alloc(areq); 14734de9d0b5SLee Nipper if (IS_ERR(edesc)) 14744de9d0b5SLee Nipper return PTR_ERR(edesc); 14754de9d0b5SLee Nipper 14764de9d0b5SLee Nipper edesc->desc.hdr = ctx->desc_hdr_template | DESC_HDR_DIR_INBOUND; 14774de9d0b5SLee Nipper 14784de9d0b5SLee Nipper return common_nonsnoop(edesc, areq, NULL, ablkcipher_done); 14794de9d0b5SLee Nipper } 14804de9d0b5SLee Nipper 14819c4a7965SKim Phillips struct talitos_alg_template { 148256af8cd4SLee Nipper struct crypto_alg alg; 14839c4a7965SKim Phillips __be32 desc_hdr_template; 14849c4a7965SKim Phillips }; 14859c4a7965SKim Phillips 14869c4a7965SKim Phillips static struct talitos_alg_template driver_algs[] = { 148756af8cd4SLee Nipper /* AEAD algorithms. These use a single-pass ipsec_esp descriptor */ 14889c4a7965SKim Phillips { 148956af8cd4SLee Nipper .alg = { 149056af8cd4SLee Nipper .cra_name = "authenc(hmac(sha1),cbc(aes))", 149156af8cd4SLee Nipper .cra_driver_name = "authenc-hmac-sha1-cbc-aes-talitos", 149256af8cd4SLee Nipper .cra_blocksize = AES_BLOCK_SIZE, 149356af8cd4SLee Nipper .cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC, 149456af8cd4SLee Nipper .cra_type = &crypto_aead_type, 149556af8cd4SLee Nipper .cra_aead = { 149656af8cd4SLee Nipper .setkey = aead_setkey, 149756af8cd4SLee Nipper .setauthsize = aead_setauthsize, 149856af8cd4SLee Nipper .encrypt = aead_encrypt, 149956af8cd4SLee Nipper .decrypt = aead_decrypt, 150056af8cd4SLee Nipper .givencrypt = aead_givencrypt, 15019c4a7965SKim Phillips .geniv = "<built-in>", 15023952f17eSLee Nipper .ivsize = AES_BLOCK_SIZE, 15033952f17eSLee Nipper .maxauthsize = SHA1_DIGEST_SIZE, 150456af8cd4SLee Nipper } 15059c4a7965SKim Phillips }, 15069c4a7965SKim Phillips .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP | 15079c4a7965SKim Phillips DESC_HDR_SEL0_AESU | 15089c4a7965SKim Phillips DESC_HDR_MODE0_AESU_CBC | 15099c4a7965SKim Phillips DESC_HDR_SEL1_MDEUA | 15109c4a7965SKim Phillips DESC_HDR_MODE1_MDEU_INIT | 15119c4a7965SKim Phillips DESC_HDR_MODE1_MDEU_PAD | 15129c4a7965SKim Phillips DESC_HDR_MODE1_MDEU_SHA1_HMAC, 151370bcaca7SLee Nipper }, 151470bcaca7SLee Nipper { 151556af8cd4SLee Nipper .alg = { 151656af8cd4SLee Nipper .cra_name = "authenc(hmac(sha1),cbc(des3_ede))", 151756af8cd4SLee Nipper .cra_driver_name = "authenc-hmac-sha1-cbc-3des-talitos", 151856af8cd4SLee Nipper .cra_blocksize = DES3_EDE_BLOCK_SIZE, 151956af8cd4SLee Nipper .cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC, 152056af8cd4SLee Nipper .cra_type = &crypto_aead_type, 152156af8cd4SLee Nipper .cra_aead = { 152256af8cd4SLee Nipper .setkey = aead_setkey, 152356af8cd4SLee Nipper .setauthsize = aead_setauthsize, 152456af8cd4SLee Nipper .encrypt = aead_encrypt, 152556af8cd4SLee Nipper .decrypt = aead_decrypt, 152656af8cd4SLee Nipper .givencrypt = aead_givencrypt, 152770bcaca7SLee Nipper .geniv = "<built-in>", 15283952f17eSLee Nipper .ivsize = DES3_EDE_BLOCK_SIZE, 15293952f17eSLee Nipper .maxauthsize = SHA1_DIGEST_SIZE, 153056af8cd4SLee Nipper } 153170bcaca7SLee Nipper }, 153270bcaca7SLee Nipper .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP | 153370bcaca7SLee Nipper DESC_HDR_SEL0_DEU | 153470bcaca7SLee Nipper DESC_HDR_MODE0_DEU_CBC | 153570bcaca7SLee Nipper DESC_HDR_MODE0_DEU_3DES | 153670bcaca7SLee Nipper DESC_HDR_SEL1_MDEUA | 153770bcaca7SLee Nipper DESC_HDR_MODE1_MDEU_INIT | 153870bcaca7SLee Nipper DESC_HDR_MODE1_MDEU_PAD | 153970bcaca7SLee Nipper DESC_HDR_MODE1_MDEU_SHA1_HMAC, 15403952f17eSLee Nipper }, 15413952f17eSLee Nipper { 154256af8cd4SLee Nipper .alg = { 154356af8cd4SLee Nipper .cra_name = "authenc(hmac(sha256),cbc(aes))", 154456af8cd4SLee Nipper .cra_driver_name = "authenc-hmac-sha256-cbc-aes-talitos", 154556af8cd4SLee Nipper .cra_blocksize = AES_BLOCK_SIZE, 154656af8cd4SLee Nipper .cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC, 154756af8cd4SLee Nipper .cra_type = &crypto_aead_type, 154856af8cd4SLee Nipper .cra_aead = { 154956af8cd4SLee Nipper .setkey = aead_setkey, 155056af8cd4SLee Nipper .setauthsize = aead_setauthsize, 155156af8cd4SLee Nipper .encrypt = aead_encrypt, 155256af8cd4SLee Nipper .decrypt = aead_decrypt, 155356af8cd4SLee Nipper .givencrypt = aead_givencrypt, 15543952f17eSLee Nipper .geniv = "<built-in>", 15553952f17eSLee Nipper .ivsize = AES_BLOCK_SIZE, 15563952f17eSLee Nipper .maxauthsize = SHA256_DIGEST_SIZE, 155756af8cd4SLee Nipper } 15583952f17eSLee Nipper }, 15593952f17eSLee Nipper .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP | 15603952f17eSLee Nipper DESC_HDR_SEL0_AESU | 15613952f17eSLee Nipper DESC_HDR_MODE0_AESU_CBC | 15623952f17eSLee Nipper DESC_HDR_SEL1_MDEUA | 15633952f17eSLee Nipper DESC_HDR_MODE1_MDEU_INIT | 15643952f17eSLee Nipper DESC_HDR_MODE1_MDEU_PAD | 15653952f17eSLee Nipper DESC_HDR_MODE1_MDEU_SHA256_HMAC, 15663952f17eSLee Nipper }, 15673952f17eSLee Nipper { 156856af8cd4SLee Nipper .alg = { 156956af8cd4SLee Nipper .cra_name = "authenc(hmac(sha256),cbc(des3_ede))", 157056af8cd4SLee Nipper .cra_driver_name = "authenc-hmac-sha256-cbc-3des-talitos", 157156af8cd4SLee Nipper .cra_blocksize = DES3_EDE_BLOCK_SIZE, 157256af8cd4SLee Nipper .cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC, 157356af8cd4SLee Nipper .cra_type = &crypto_aead_type, 157456af8cd4SLee Nipper .cra_aead = { 157556af8cd4SLee Nipper .setkey = aead_setkey, 157656af8cd4SLee Nipper .setauthsize = aead_setauthsize, 157756af8cd4SLee Nipper .encrypt = aead_encrypt, 157856af8cd4SLee Nipper .decrypt = aead_decrypt, 157956af8cd4SLee Nipper .givencrypt = aead_givencrypt, 15803952f17eSLee Nipper .geniv = "<built-in>", 15813952f17eSLee Nipper .ivsize = DES3_EDE_BLOCK_SIZE, 15823952f17eSLee Nipper .maxauthsize = SHA256_DIGEST_SIZE, 158356af8cd4SLee Nipper } 15843952f17eSLee Nipper }, 15853952f17eSLee Nipper .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP | 15863952f17eSLee Nipper DESC_HDR_SEL0_DEU | 15873952f17eSLee Nipper DESC_HDR_MODE0_DEU_CBC | 15883952f17eSLee Nipper DESC_HDR_MODE0_DEU_3DES | 15893952f17eSLee Nipper DESC_HDR_SEL1_MDEUA | 15903952f17eSLee Nipper DESC_HDR_MODE1_MDEU_INIT | 15913952f17eSLee Nipper DESC_HDR_MODE1_MDEU_PAD | 15923952f17eSLee Nipper DESC_HDR_MODE1_MDEU_SHA256_HMAC, 15933952f17eSLee Nipper }, 15943952f17eSLee Nipper { 159556af8cd4SLee Nipper .alg = { 159656af8cd4SLee Nipper .cra_name = "authenc(hmac(md5),cbc(aes))", 159756af8cd4SLee Nipper .cra_driver_name = "authenc-hmac-md5-cbc-aes-talitos", 159856af8cd4SLee Nipper .cra_blocksize = AES_BLOCK_SIZE, 159956af8cd4SLee Nipper .cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC, 160056af8cd4SLee Nipper .cra_type = &crypto_aead_type, 160156af8cd4SLee Nipper .cra_aead = { 160256af8cd4SLee Nipper .setkey = aead_setkey, 160356af8cd4SLee Nipper .setauthsize = aead_setauthsize, 160456af8cd4SLee Nipper .encrypt = aead_encrypt, 160556af8cd4SLee Nipper .decrypt = aead_decrypt, 160656af8cd4SLee Nipper .givencrypt = aead_givencrypt, 16073952f17eSLee Nipper .geniv = "<built-in>", 16083952f17eSLee Nipper .ivsize = AES_BLOCK_SIZE, 16093952f17eSLee Nipper .maxauthsize = MD5_DIGEST_SIZE, 161056af8cd4SLee Nipper } 16113952f17eSLee Nipper }, 16123952f17eSLee Nipper .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP | 16133952f17eSLee Nipper DESC_HDR_SEL0_AESU | 16143952f17eSLee Nipper DESC_HDR_MODE0_AESU_CBC | 16153952f17eSLee Nipper DESC_HDR_SEL1_MDEUA | 16163952f17eSLee Nipper DESC_HDR_MODE1_MDEU_INIT | 16173952f17eSLee Nipper DESC_HDR_MODE1_MDEU_PAD | 16183952f17eSLee Nipper DESC_HDR_MODE1_MDEU_MD5_HMAC, 16193952f17eSLee Nipper }, 16203952f17eSLee Nipper { 162156af8cd4SLee Nipper .alg = { 162256af8cd4SLee Nipper .cra_name = "authenc(hmac(md5),cbc(des3_ede))", 162356af8cd4SLee Nipper .cra_driver_name = "authenc-hmac-md5-cbc-3des-talitos", 162456af8cd4SLee Nipper .cra_blocksize = DES3_EDE_BLOCK_SIZE, 162556af8cd4SLee Nipper .cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC, 162656af8cd4SLee Nipper .cra_type = &crypto_aead_type, 162756af8cd4SLee Nipper .cra_aead = { 162856af8cd4SLee Nipper .setkey = aead_setkey, 162956af8cd4SLee Nipper .setauthsize = aead_setauthsize, 163056af8cd4SLee Nipper .encrypt = aead_encrypt, 163156af8cd4SLee Nipper .decrypt = aead_decrypt, 163256af8cd4SLee Nipper .givencrypt = aead_givencrypt, 16333952f17eSLee Nipper .geniv = "<built-in>", 16343952f17eSLee Nipper .ivsize = DES3_EDE_BLOCK_SIZE, 16353952f17eSLee Nipper .maxauthsize = MD5_DIGEST_SIZE, 163656af8cd4SLee Nipper } 16373952f17eSLee Nipper }, 16383952f17eSLee Nipper .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP | 16393952f17eSLee Nipper DESC_HDR_SEL0_DEU | 16403952f17eSLee Nipper DESC_HDR_MODE0_DEU_CBC | 16413952f17eSLee Nipper DESC_HDR_MODE0_DEU_3DES | 16423952f17eSLee Nipper DESC_HDR_SEL1_MDEUA | 16433952f17eSLee Nipper DESC_HDR_MODE1_MDEU_INIT | 16443952f17eSLee Nipper DESC_HDR_MODE1_MDEU_PAD | 16453952f17eSLee Nipper DESC_HDR_MODE1_MDEU_MD5_HMAC, 16464de9d0b5SLee Nipper }, 16474de9d0b5SLee Nipper /* ABLKCIPHER algorithms. */ 16484de9d0b5SLee Nipper { 16494de9d0b5SLee Nipper .alg = { 16504de9d0b5SLee Nipper .cra_name = "cbc(aes)", 16514de9d0b5SLee Nipper .cra_driver_name = "cbc-aes-talitos", 16524de9d0b5SLee Nipper .cra_blocksize = AES_BLOCK_SIZE, 16534de9d0b5SLee Nipper .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | 16544de9d0b5SLee Nipper CRYPTO_ALG_ASYNC, 16554de9d0b5SLee Nipper .cra_type = &crypto_ablkcipher_type, 16564de9d0b5SLee Nipper .cra_ablkcipher = { 16574de9d0b5SLee Nipper .setkey = ablkcipher_setkey, 16584de9d0b5SLee Nipper .encrypt = ablkcipher_encrypt, 16594de9d0b5SLee Nipper .decrypt = ablkcipher_decrypt, 16604de9d0b5SLee Nipper .geniv = "eseqiv", 16614de9d0b5SLee Nipper .min_keysize = AES_MIN_KEY_SIZE, 16624de9d0b5SLee Nipper .max_keysize = AES_MAX_KEY_SIZE, 16634de9d0b5SLee Nipper .ivsize = AES_BLOCK_SIZE, 16644de9d0b5SLee Nipper } 16654de9d0b5SLee Nipper }, 16664de9d0b5SLee Nipper .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU | 16674de9d0b5SLee Nipper DESC_HDR_SEL0_AESU | 16684de9d0b5SLee Nipper DESC_HDR_MODE0_AESU_CBC, 16694de9d0b5SLee Nipper }, 16704de9d0b5SLee Nipper { 16714de9d0b5SLee Nipper .alg = { 16724de9d0b5SLee Nipper .cra_name = "cbc(des3_ede)", 16734de9d0b5SLee Nipper .cra_driver_name = "cbc-3des-talitos", 16744de9d0b5SLee Nipper .cra_blocksize = DES3_EDE_BLOCK_SIZE, 16754de9d0b5SLee Nipper .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | 16764de9d0b5SLee Nipper CRYPTO_ALG_ASYNC, 16774de9d0b5SLee Nipper .cra_type = &crypto_ablkcipher_type, 16784de9d0b5SLee Nipper .cra_ablkcipher = { 16794de9d0b5SLee Nipper .setkey = ablkcipher_setkey, 16804de9d0b5SLee Nipper .encrypt = ablkcipher_encrypt, 16814de9d0b5SLee Nipper .decrypt = ablkcipher_decrypt, 16824de9d0b5SLee Nipper .geniv = "eseqiv", 16834de9d0b5SLee Nipper .min_keysize = DES3_EDE_KEY_SIZE, 16844de9d0b5SLee Nipper .max_keysize = DES3_EDE_KEY_SIZE, 16854de9d0b5SLee Nipper .ivsize = DES3_EDE_BLOCK_SIZE, 16864de9d0b5SLee Nipper } 16874de9d0b5SLee Nipper }, 16884de9d0b5SLee Nipper .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU | 16894de9d0b5SLee Nipper DESC_HDR_SEL0_DEU | 16904de9d0b5SLee Nipper DESC_HDR_MODE0_DEU_CBC | 16914de9d0b5SLee Nipper DESC_HDR_MODE0_DEU_3DES, 16929c4a7965SKim Phillips } 16939c4a7965SKim Phillips }; 16949c4a7965SKim Phillips 16959c4a7965SKim Phillips struct talitos_crypto_alg { 16969c4a7965SKim Phillips struct list_head entry; 16979c4a7965SKim Phillips struct device *dev; 16989c4a7965SKim Phillips __be32 desc_hdr_template; 16999c4a7965SKim Phillips struct crypto_alg crypto_alg; 17009c4a7965SKim Phillips }; 17019c4a7965SKim Phillips 17029c4a7965SKim Phillips static int talitos_cra_init(struct crypto_tfm *tfm) 17039c4a7965SKim Phillips { 17049c4a7965SKim Phillips struct crypto_alg *alg = tfm->__crt_alg; 170519bbbc63SKim Phillips struct talitos_crypto_alg *talitos_alg; 17069c4a7965SKim Phillips struct talitos_ctx *ctx = crypto_tfm_ctx(tfm); 17079c4a7965SKim Phillips 170819bbbc63SKim Phillips talitos_alg = container_of(alg, struct talitos_crypto_alg, crypto_alg); 170919bbbc63SKim Phillips 17109c4a7965SKim Phillips /* update context with ptr to dev */ 17119c4a7965SKim Phillips ctx->dev = talitos_alg->dev; 171219bbbc63SKim Phillips 17139c4a7965SKim Phillips /* copy descriptor header template value */ 17149c4a7965SKim Phillips ctx->desc_hdr_template = talitos_alg->desc_hdr_template; 17159c4a7965SKim Phillips 17169c4a7965SKim Phillips /* random first IV */ 171770bcaca7SLee Nipper get_random_bytes(ctx->iv, TALITOS_MAX_IV_LENGTH); 17189c4a7965SKim Phillips 17199c4a7965SKim Phillips return 0; 17209c4a7965SKim Phillips } 17219c4a7965SKim Phillips 17229c4a7965SKim Phillips /* 17239c4a7965SKim Phillips * given the alg's descriptor header template, determine whether descriptor 17249c4a7965SKim Phillips * type and primary/secondary execution units required match the hw 17259c4a7965SKim Phillips * capabilities description provided in the device tree node. 17269c4a7965SKim Phillips */ 17279c4a7965SKim Phillips static int hw_supports(struct device *dev, __be32 desc_hdr_template) 17289c4a7965SKim Phillips { 17299c4a7965SKim Phillips struct talitos_private *priv = dev_get_drvdata(dev); 17309c4a7965SKim Phillips int ret; 17319c4a7965SKim Phillips 17329c4a7965SKim Phillips ret = (1 << DESC_TYPE(desc_hdr_template) & priv->desc_types) && 17339c4a7965SKim Phillips (1 << PRIMARY_EU(desc_hdr_template) & priv->exec_units); 17349c4a7965SKim Phillips 17359c4a7965SKim Phillips if (SECONDARY_EU(desc_hdr_template)) 17369c4a7965SKim Phillips ret = ret && (1 << SECONDARY_EU(desc_hdr_template) 17379c4a7965SKim Phillips & priv->exec_units); 17389c4a7965SKim Phillips 17399c4a7965SKim Phillips return ret; 17409c4a7965SKim Phillips } 17419c4a7965SKim Phillips 1742596f1034SAl Viro static int talitos_remove(struct of_device *ofdev) 17439c4a7965SKim Phillips { 17449c4a7965SKim Phillips struct device *dev = &ofdev->dev; 17459c4a7965SKim Phillips struct talitos_private *priv = dev_get_drvdata(dev); 17469c4a7965SKim Phillips struct talitos_crypto_alg *t_alg, *n; 17479c4a7965SKim Phillips int i; 17489c4a7965SKim Phillips 17499c4a7965SKim Phillips list_for_each_entry_safe(t_alg, n, &priv->alg_list, entry) { 17509c4a7965SKim Phillips crypto_unregister_alg(&t_alg->crypto_alg); 17519c4a7965SKim Phillips list_del(&t_alg->entry); 17529c4a7965SKim Phillips kfree(t_alg); 17539c4a7965SKim Phillips } 17549c4a7965SKim Phillips 17559c4a7965SKim Phillips if (hw_supports(dev, DESC_HDR_SEL0_RNG)) 17569c4a7965SKim Phillips talitos_unregister_rng(dev); 17579c4a7965SKim Phillips 17589c4a7965SKim Phillips for (i = 0; i < priv->num_channels; i++) 17594b992628SKim Phillips if (priv->chan[i].fifo) 17604b992628SKim Phillips kfree(priv->chan[i].fifo); 17619c4a7965SKim Phillips 17624b992628SKim Phillips kfree(priv->chan); 17639c4a7965SKim Phillips 17649c4a7965SKim Phillips if (priv->irq != NO_IRQ) { 17659c4a7965SKim Phillips free_irq(priv->irq, dev); 17669c4a7965SKim Phillips irq_dispose_mapping(priv->irq); 17679c4a7965SKim Phillips } 17689c4a7965SKim Phillips 17699c4a7965SKim Phillips tasklet_kill(&priv->done_task); 17709c4a7965SKim Phillips 17719c4a7965SKim Phillips iounmap(priv->reg); 17729c4a7965SKim Phillips 17739c4a7965SKim Phillips dev_set_drvdata(dev, NULL); 17749c4a7965SKim Phillips 17759c4a7965SKim Phillips kfree(priv); 17769c4a7965SKim Phillips 17779c4a7965SKim Phillips return 0; 17789c4a7965SKim Phillips } 17799c4a7965SKim Phillips 17809c4a7965SKim Phillips static struct talitos_crypto_alg *talitos_alg_alloc(struct device *dev, 17819c4a7965SKim Phillips struct talitos_alg_template 17829c4a7965SKim Phillips *template) 17839c4a7965SKim Phillips { 17849c4a7965SKim Phillips struct talitos_crypto_alg *t_alg; 17859c4a7965SKim Phillips struct crypto_alg *alg; 17869c4a7965SKim Phillips 17879c4a7965SKim Phillips t_alg = kzalloc(sizeof(struct talitos_crypto_alg), GFP_KERNEL); 17889c4a7965SKim Phillips if (!t_alg) 17899c4a7965SKim Phillips return ERR_PTR(-ENOMEM); 17909c4a7965SKim Phillips 17919c4a7965SKim Phillips alg = &t_alg->crypto_alg; 179256af8cd4SLee Nipper *alg = template->alg; 17939c4a7965SKim Phillips 17949c4a7965SKim Phillips alg->cra_module = THIS_MODULE; 17959c4a7965SKim Phillips alg->cra_init = talitos_cra_init; 17969c4a7965SKim Phillips alg->cra_priority = TALITOS_CRA_PRIORITY; 17979c4a7965SKim Phillips alg->cra_alignmask = 0; 17989c4a7965SKim Phillips alg->cra_ctxsize = sizeof(struct talitos_ctx); 17999c4a7965SKim Phillips 18009c4a7965SKim Phillips t_alg->desc_hdr_template = template->desc_hdr_template; 18019c4a7965SKim Phillips t_alg->dev = dev; 18029c4a7965SKim Phillips 18039c4a7965SKim Phillips return t_alg; 18049c4a7965SKim Phillips } 18059c4a7965SKim Phillips 18069c4a7965SKim Phillips static int talitos_probe(struct of_device *ofdev, 18079c4a7965SKim Phillips const struct of_device_id *match) 18089c4a7965SKim Phillips { 18099c4a7965SKim Phillips struct device *dev = &ofdev->dev; 1810*61c7a080SGrant Likely struct device_node *np = ofdev->dev.of_node; 18119c4a7965SKim Phillips struct talitos_private *priv; 18129c4a7965SKim Phillips const unsigned int *prop; 18139c4a7965SKim Phillips int i, err; 18149c4a7965SKim Phillips 18159c4a7965SKim Phillips priv = kzalloc(sizeof(struct talitos_private), GFP_KERNEL); 18169c4a7965SKim Phillips if (!priv) 18179c4a7965SKim Phillips return -ENOMEM; 18189c4a7965SKim Phillips 18199c4a7965SKim Phillips dev_set_drvdata(dev, priv); 18209c4a7965SKim Phillips 18219c4a7965SKim Phillips priv->ofdev = ofdev; 18229c4a7965SKim Phillips 18239c4a7965SKim Phillips tasklet_init(&priv->done_task, talitos_done, (unsigned long)dev); 18249c4a7965SKim Phillips 1825fe5720e2SKim Phillips INIT_LIST_HEAD(&priv->alg_list); 1826fe5720e2SKim Phillips 18279c4a7965SKim Phillips priv->irq = irq_of_parse_and_map(np, 0); 18289c4a7965SKim Phillips 18299c4a7965SKim Phillips if (priv->irq == NO_IRQ) { 18309c4a7965SKim Phillips dev_err(dev, "failed to map irq\n"); 18319c4a7965SKim Phillips err = -EINVAL; 18329c4a7965SKim Phillips goto err_out; 18339c4a7965SKim Phillips } 18349c4a7965SKim Phillips 18359c4a7965SKim Phillips /* get the irq line */ 18369c4a7965SKim Phillips err = request_irq(priv->irq, talitos_interrupt, 0, 18379c4a7965SKim Phillips dev_driver_string(dev), dev); 18389c4a7965SKim Phillips if (err) { 18399c4a7965SKim Phillips dev_err(dev, "failed to request irq %d\n", priv->irq); 18409c4a7965SKim Phillips irq_dispose_mapping(priv->irq); 18419c4a7965SKim Phillips priv->irq = NO_IRQ; 18429c4a7965SKim Phillips goto err_out; 18439c4a7965SKim Phillips } 18449c4a7965SKim Phillips 18459c4a7965SKim Phillips priv->reg = of_iomap(np, 0); 18469c4a7965SKim Phillips if (!priv->reg) { 18479c4a7965SKim Phillips dev_err(dev, "failed to of_iomap\n"); 18489c4a7965SKim Phillips err = -ENOMEM; 18499c4a7965SKim Phillips goto err_out; 18509c4a7965SKim Phillips } 18519c4a7965SKim Phillips 18529c4a7965SKim Phillips /* get SEC version capabilities from device tree */ 18539c4a7965SKim Phillips prop = of_get_property(np, "fsl,num-channels", NULL); 18549c4a7965SKim Phillips if (prop) 18559c4a7965SKim Phillips priv->num_channels = *prop; 18569c4a7965SKim Phillips 18579c4a7965SKim Phillips prop = of_get_property(np, "fsl,channel-fifo-len", NULL); 18589c4a7965SKim Phillips if (prop) 18599c4a7965SKim Phillips priv->chfifo_len = *prop; 18609c4a7965SKim Phillips 18619c4a7965SKim Phillips prop = of_get_property(np, "fsl,exec-units-mask", NULL); 18629c4a7965SKim Phillips if (prop) 18639c4a7965SKim Phillips priv->exec_units = *prop; 18649c4a7965SKim Phillips 18659c4a7965SKim Phillips prop = of_get_property(np, "fsl,descriptor-types-mask", NULL); 18669c4a7965SKim Phillips if (prop) 18679c4a7965SKim Phillips priv->desc_types = *prop; 18689c4a7965SKim Phillips 18699c4a7965SKim Phillips if (!is_power_of_2(priv->num_channels) || !priv->chfifo_len || 18709c4a7965SKim Phillips !priv->exec_units || !priv->desc_types) { 18719c4a7965SKim Phillips dev_err(dev, "invalid property data in device tree node\n"); 18729c4a7965SKim Phillips err = -EINVAL; 18739c4a7965SKim Phillips goto err_out; 18749c4a7965SKim Phillips } 18759c4a7965SKim Phillips 1876f3c85bc1SLee Nipper if (of_device_is_compatible(np, "fsl,sec3.0")) 1877f3c85bc1SLee Nipper priv->features |= TALITOS_FTR_SRC_LINK_TBL_LEN_INCLUDES_EXTENT; 1878f3c85bc1SLee Nipper 1879fe5720e2SKim Phillips if (of_device_is_compatible(np, "fsl,sec2.1")) 1880fe5720e2SKim Phillips priv->features |= TALITOS_FTR_HW_AUTH_CHECK; 1881fe5720e2SKim Phillips 18824b992628SKim Phillips priv->chan = kzalloc(sizeof(struct talitos_channel) * 18834b992628SKim Phillips priv->num_channels, GFP_KERNEL); 18844b992628SKim Phillips if (!priv->chan) { 18854b992628SKim Phillips dev_err(dev, "failed to allocate channel management space\n"); 18869c4a7965SKim Phillips err = -ENOMEM; 18879c4a7965SKim Phillips goto err_out; 18889c4a7965SKim Phillips } 18899c4a7965SKim Phillips 18909c4a7965SKim Phillips for (i = 0; i < priv->num_channels; i++) { 18914b992628SKim Phillips spin_lock_init(&priv->chan[i].head_lock); 18924b992628SKim Phillips spin_lock_init(&priv->chan[i].tail_lock); 18939c4a7965SKim Phillips } 18949c4a7965SKim Phillips 18959c4a7965SKim Phillips priv->fifo_len = roundup_pow_of_two(priv->chfifo_len); 18969c4a7965SKim Phillips 18979c4a7965SKim Phillips for (i = 0; i < priv->num_channels; i++) { 18984b992628SKim Phillips priv->chan[i].fifo = kzalloc(sizeof(struct talitos_request) * 18999c4a7965SKim Phillips priv->fifo_len, GFP_KERNEL); 19004b992628SKim Phillips if (!priv->chan[i].fifo) { 19019c4a7965SKim Phillips dev_err(dev, "failed to allocate request fifo %d\n", i); 19029c4a7965SKim Phillips err = -ENOMEM; 19039c4a7965SKim Phillips goto err_out; 19049c4a7965SKim Phillips } 19059c4a7965SKim Phillips } 19069c4a7965SKim Phillips 1907ec6644d6SKim Phillips for (i = 0; i < priv->num_channels; i++) 19084b992628SKim Phillips atomic_set(&priv->chan[i].submit_count, 19094b992628SKim Phillips -(priv->chfifo_len - 1)); 19109c4a7965SKim Phillips 191181eb024cSKim Phillips dma_set_mask(dev, DMA_BIT_MASK(36)); 191281eb024cSKim Phillips 19139c4a7965SKim Phillips /* reset and initialize the h/w */ 19149c4a7965SKim Phillips err = init_device(dev); 19159c4a7965SKim Phillips if (err) { 19169c4a7965SKim Phillips dev_err(dev, "failed to initialize device\n"); 19179c4a7965SKim Phillips goto err_out; 19189c4a7965SKim Phillips } 19199c4a7965SKim Phillips 19209c4a7965SKim Phillips /* register the RNG, if available */ 19219c4a7965SKim Phillips if (hw_supports(dev, DESC_HDR_SEL0_RNG)) { 19229c4a7965SKim Phillips err = talitos_register_rng(dev); 19239c4a7965SKim Phillips if (err) { 19249c4a7965SKim Phillips dev_err(dev, "failed to register hwrng: %d\n", err); 19259c4a7965SKim Phillips goto err_out; 19269c4a7965SKim Phillips } else 19279c4a7965SKim Phillips dev_info(dev, "hwrng\n"); 19289c4a7965SKim Phillips } 19299c4a7965SKim Phillips 19309c4a7965SKim Phillips /* register crypto algorithms the device supports */ 19319c4a7965SKim Phillips for (i = 0; i < ARRAY_SIZE(driver_algs); i++) { 19329c4a7965SKim Phillips if (hw_supports(dev, driver_algs[i].desc_hdr_template)) { 19339c4a7965SKim Phillips struct talitos_crypto_alg *t_alg; 19349c4a7965SKim Phillips 19359c4a7965SKim Phillips t_alg = talitos_alg_alloc(dev, &driver_algs[i]); 19369c4a7965SKim Phillips if (IS_ERR(t_alg)) { 19379c4a7965SKim Phillips err = PTR_ERR(t_alg); 19389c4a7965SKim Phillips goto err_out; 19399c4a7965SKim Phillips } 19409c4a7965SKim Phillips 19419c4a7965SKim Phillips err = crypto_register_alg(&t_alg->crypto_alg); 19429c4a7965SKim Phillips if (err) { 19439c4a7965SKim Phillips dev_err(dev, "%s alg registration failed\n", 19449c4a7965SKim Phillips t_alg->crypto_alg.cra_driver_name); 19459c4a7965SKim Phillips kfree(t_alg); 19469c4a7965SKim Phillips } else { 19479c4a7965SKim Phillips list_add_tail(&t_alg->entry, &priv->alg_list); 19489c4a7965SKim Phillips dev_info(dev, "%s\n", 19499c4a7965SKim Phillips t_alg->crypto_alg.cra_driver_name); 19509c4a7965SKim Phillips } 19519c4a7965SKim Phillips } 19529c4a7965SKim Phillips } 19539c4a7965SKim Phillips 19549c4a7965SKim Phillips return 0; 19559c4a7965SKim Phillips 19569c4a7965SKim Phillips err_out: 19579c4a7965SKim Phillips talitos_remove(ofdev); 19589c4a7965SKim Phillips 19599c4a7965SKim Phillips return err; 19609c4a7965SKim Phillips } 19619c4a7965SKim Phillips 19626c3f975aSMárton Németh static const struct of_device_id talitos_match[] = { 19639c4a7965SKim Phillips { 19649c4a7965SKim Phillips .compatible = "fsl,sec2.0", 19659c4a7965SKim Phillips }, 19669c4a7965SKim Phillips {}, 19679c4a7965SKim Phillips }; 19689c4a7965SKim Phillips MODULE_DEVICE_TABLE(of, talitos_match); 19699c4a7965SKim Phillips 19709c4a7965SKim Phillips static struct of_platform_driver talitos_driver = { 19719c4a7965SKim Phillips .name = "talitos", 19729c4a7965SKim Phillips .match_table = talitos_match, 19739c4a7965SKim Phillips .probe = talitos_probe, 1974596f1034SAl Viro .remove = talitos_remove, 19759c4a7965SKim Phillips }; 19769c4a7965SKim Phillips 19779c4a7965SKim Phillips static int __init talitos_init(void) 19789c4a7965SKim Phillips { 19799c4a7965SKim Phillips return of_register_platform_driver(&talitos_driver); 19809c4a7965SKim Phillips } 19819c4a7965SKim Phillips module_init(talitos_init); 19829c4a7965SKim Phillips 19839c4a7965SKim Phillips static void __exit talitos_exit(void) 19849c4a7965SKim Phillips { 19859c4a7965SKim Phillips of_unregister_platform_driver(&talitos_driver); 19869c4a7965SKim Phillips } 19879c4a7965SKim Phillips module_exit(talitos_exit); 19889c4a7965SKim Phillips 19899c4a7965SKim Phillips MODULE_LICENSE("GPL"); 19909c4a7965SKim Phillips MODULE_AUTHOR("Kim Phillips <kim.phillips@freescale.com>"); 19919c4a7965SKim Phillips MODULE_DESCRIPTION("Freescale integrated security engine (SEC) driver"); 1992