19c4a7965SKim Phillips /* 29c4a7965SKim Phillips * talitos - Freescale Integrated Security Engine (SEC) device driver 39c4a7965SKim Phillips * 45228f0f7SKim Phillips * Copyright (c) 2008-2011 Freescale Semiconductor, Inc. 59c4a7965SKim Phillips * 69c4a7965SKim Phillips * Scatterlist Crypto API glue code copied from files with the following: 79c4a7965SKim Phillips * Copyright (c) 2006-2007 Herbert Xu <herbert@gondor.apana.org.au> 89c4a7965SKim Phillips * 99c4a7965SKim Phillips * Crypto algorithm registration code copied from hifn driver: 109c4a7965SKim Phillips * 2007+ Copyright (c) Evgeniy Polyakov <johnpol@2ka.mipt.ru> 119c4a7965SKim Phillips * All rights reserved. 129c4a7965SKim Phillips * 139c4a7965SKim Phillips * This program is free software; you can redistribute it and/or modify 149c4a7965SKim Phillips * it under the terms of the GNU General Public License as published by 159c4a7965SKim Phillips * the Free Software Foundation; either version 2 of the License, or 169c4a7965SKim Phillips * (at your option) any later version. 179c4a7965SKim Phillips * 189c4a7965SKim Phillips * This program is distributed in the hope that it will be useful, 199c4a7965SKim Phillips * but WITHOUT ANY WARRANTY; without even the implied warranty of 209c4a7965SKim Phillips * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 219c4a7965SKim Phillips * GNU General Public License for more details. 229c4a7965SKim Phillips * 239c4a7965SKim Phillips * You should have received a copy of the GNU General Public License 249c4a7965SKim Phillips * along with this program; if not, write to the Free Software 259c4a7965SKim Phillips * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 269c4a7965SKim Phillips */ 279c4a7965SKim Phillips 289c4a7965SKim Phillips #include <linux/kernel.h> 299c4a7965SKim Phillips #include <linux/module.h> 309c4a7965SKim Phillips #include <linux/mod_devicetable.h> 319c4a7965SKim Phillips #include <linux/device.h> 329c4a7965SKim Phillips #include <linux/interrupt.h> 339c4a7965SKim Phillips #include <linux/crypto.h> 349c4a7965SKim Phillips #include <linux/hw_random.h> 359c4a7965SKim Phillips #include <linux/of_platform.h> 369c4a7965SKim Phillips #include <linux/dma-mapping.h> 379c4a7965SKim Phillips #include <linux/io.h> 389c4a7965SKim Phillips #include <linux/spinlock.h> 399c4a7965SKim Phillips #include <linux/rtnetlink.h> 405a0e3ad6STejun Heo #include <linux/slab.h> 419c4a7965SKim Phillips 429c4a7965SKim Phillips #include <crypto/algapi.h> 439c4a7965SKim Phillips #include <crypto/aes.h> 443952f17eSLee Nipper #include <crypto/des.h> 459c4a7965SKim Phillips #include <crypto/sha.h> 46497f2e6bSLee Nipper #include <crypto/md5.h> 479c4a7965SKim Phillips #include <crypto/aead.h> 489c4a7965SKim Phillips #include <crypto/authenc.h> 494de9d0b5SLee Nipper #include <crypto/skcipher.h> 50acbf7c62SLee Nipper #include <crypto/hash.h> 51acbf7c62SLee Nipper #include <crypto/internal/hash.h> 524de9d0b5SLee Nipper #include <crypto/scatterwalk.h> 539c4a7965SKim Phillips 549c4a7965SKim Phillips #include "talitos.h" 559c4a7965SKim Phillips 569c4a7965SKim Phillips #define TALITOS_TIMEOUT 100000 579c4a7965SKim Phillips #define TALITOS_MAX_DATA_LEN 65535 589c4a7965SKim Phillips 599c4a7965SKim Phillips #define DESC_TYPE(desc_hdr) ((be32_to_cpu(desc_hdr) >> 3) & 0x1f) 609c4a7965SKim Phillips #define PRIMARY_EU(desc_hdr) ((be32_to_cpu(desc_hdr) >> 28) & 0xf) 619c4a7965SKim Phillips #define SECONDARY_EU(desc_hdr) ((be32_to_cpu(desc_hdr) >> 16) & 0xf) 629c4a7965SKim Phillips 639c4a7965SKim Phillips /* descriptor pointer entry */ 649c4a7965SKim Phillips struct talitos_ptr { 659c4a7965SKim Phillips __be16 len; /* length */ 669c4a7965SKim Phillips u8 j_extent; /* jump to sg link table and/or extent */ 679c4a7965SKim Phillips u8 eptr; /* extended address */ 689c4a7965SKim Phillips __be32 ptr; /* address */ 699c4a7965SKim Phillips }; 709c4a7965SKim Phillips 71497f2e6bSLee Nipper static const struct talitos_ptr zero_entry = { 72497f2e6bSLee Nipper .len = 0, 73497f2e6bSLee Nipper .j_extent = 0, 74497f2e6bSLee Nipper .eptr = 0, 75497f2e6bSLee Nipper .ptr = 0 76497f2e6bSLee Nipper }; 77497f2e6bSLee Nipper 789c4a7965SKim Phillips /* descriptor */ 799c4a7965SKim Phillips struct talitos_desc { 809c4a7965SKim Phillips __be32 hdr; /* header high bits */ 819c4a7965SKim Phillips __be32 hdr_lo; /* header low bits */ 829c4a7965SKim Phillips struct talitos_ptr ptr[7]; /* ptr/len pair array */ 839c4a7965SKim Phillips }; 849c4a7965SKim Phillips 859c4a7965SKim Phillips /** 869c4a7965SKim Phillips * talitos_request - descriptor submission request 879c4a7965SKim Phillips * @desc: descriptor pointer (kernel virtual) 889c4a7965SKim Phillips * @dma_desc: descriptor's physical bus address 899c4a7965SKim Phillips * @callback: whom to call when descriptor processing is done 909c4a7965SKim Phillips * @context: caller context (optional) 919c4a7965SKim Phillips */ 929c4a7965SKim Phillips struct talitos_request { 939c4a7965SKim Phillips struct talitos_desc *desc; 949c4a7965SKim Phillips dma_addr_t dma_desc; 959c4a7965SKim Phillips void (*callback) (struct device *dev, struct talitos_desc *desc, 969c4a7965SKim Phillips void *context, int error); 979c4a7965SKim Phillips void *context; 989c4a7965SKim Phillips }; 999c4a7965SKim Phillips 1004b992628SKim Phillips /* per-channel fifo management */ 1014b992628SKim Phillips struct talitos_channel { 1024b992628SKim Phillips /* request fifo */ 1034b992628SKim Phillips struct talitos_request *fifo; 1044b992628SKim Phillips 1054b992628SKim Phillips /* number of requests pending in channel h/w fifo */ 1064b992628SKim Phillips atomic_t submit_count ____cacheline_aligned; 1074b992628SKim Phillips 1084b992628SKim Phillips /* request submission (head) lock */ 1094b992628SKim Phillips spinlock_t head_lock ____cacheline_aligned; 1104b992628SKim Phillips /* index to next free descriptor request */ 1114b992628SKim Phillips int head; 1124b992628SKim Phillips 1134b992628SKim Phillips /* request release (tail) lock */ 1144b992628SKim Phillips spinlock_t tail_lock ____cacheline_aligned; 1154b992628SKim Phillips /* index to next in-progress/done descriptor request */ 1164b992628SKim Phillips int tail; 1174b992628SKim Phillips }; 1184b992628SKim Phillips 1199c4a7965SKim Phillips struct talitos_private { 1209c4a7965SKim Phillips struct device *dev; 1212dc11581SGrant Likely struct platform_device *ofdev; 1229c4a7965SKim Phillips void __iomem *reg; 1239c4a7965SKim Phillips int irq; 1249c4a7965SKim Phillips 1259c4a7965SKim Phillips /* SEC version geometry (from device tree node) */ 1269c4a7965SKim Phillips unsigned int num_channels; 1279c4a7965SKim Phillips unsigned int chfifo_len; 1289c4a7965SKim Phillips unsigned int exec_units; 1299c4a7965SKim Phillips unsigned int desc_types; 1309c4a7965SKim Phillips 131f3c85bc1SLee Nipper /* SEC Compatibility info */ 132f3c85bc1SLee Nipper unsigned long features; 133f3c85bc1SLee Nipper 1349c4a7965SKim Phillips /* 1359c4a7965SKim Phillips * length of the request fifo 1369c4a7965SKim Phillips * fifo_len is chfifo_len rounded up to next power of 2 1379c4a7965SKim Phillips * so we can use bitwise ops to wrap 1389c4a7965SKim Phillips */ 1399c4a7965SKim Phillips unsigned int fifo_len; 1409c4a7965SKim Phillips 1414b992628SKim Phillips struct talitos_channel *chan; 1429c4a7965SKim Phillips 1434b992628SKim Phillips /* next channel to be assigned next incoming descriptor */ 1444b992628SKim Phillips atomic_t last_chan ____cacheline_aligned; 1459c4a7965SKim Phillips 1469c4a7965SKim Phillips /* request callback tasklet */ 1479c4a7965SKim Phillips struct tasklet_struct done_task; 1489c4a7965SKim Phillips 1499c4a7965SKim Phillips /* list of registered algorithms */ 1509c4a7965SKim Phillips struct list_head alg_list; 1519c4a7965SKim Phillips 1529c4a7965SKim Phillips /* hwrng device */ 1539c4a7965SKim Phillips struct hwrng rng; 1549c4a7965SKim Phillips }; 1559c4a7965SKim Phillips 156f3c85bc1SLee Nipper /* .features flag */ 157f3c85bc1SLee Nipper #define TALITOS_FTR_SRC_LINK_TBL_LEN_INCLUDES_EXTENT 0x00000001 158fe5720e2SKim Phillips #define TALITOS_FTR_HW_AUTH_CHECK 0x00000002 15960f208d7SKim Phillips #define TALITOS_FTR_SHA224_HWINIT 0x00000004 16079b3a418SLee Nipper #define TALITOS_FTR_HMAC_OK 0x00000008 161f3c85bc1SLee Nipper 16281eb024cSKim Phillips static void to_talitos_ptr(struct talitos_ptr *talitos_ptr, dma_addr_t dma_addr) 16381eb024cSKim Phillips { 16481eb024cSKim Phillips talitos_ptr->ptr = cpu_to_be32(lower_32_bits(dma_addr)); 165a752447aSKim Phillips talitos_ptr->eptr = upper_32_bits(dma_addr); 16681eb024cSKim Phillips } 16781eb024cSKim Phillips 1689c4a7965SKim Phillips /* 1699c4a7965SKim Phillips * map virtual single (contiguous) pointer to h/w descriptor pointer 1709c4a7965SKim Phillips */ 1719c4a7965SKim Phillips static void map_single_talitos_ptr(struct device *dev, 1729c4a7965SKim Phillips struct talitos_ptr *talitos_ptr, 1739c4a7965SKim Phillips unsigned short len, void *data, 1749c4a7965SKim Phillips unsigned char extent, 1759c4a7965SKim Phillips enum dma_data_direction dir) 1769c4a7965SKim Phillips { 17781eb024cSKim Phillips dma_addr_t dma_addr = dma_map_single(dev, data, len, dir); 17881eb024cSKim Phillips 1799c4a7965SKim Phillips talitos_ptr->len = cpu_to_be16(len); 18081eb024cSKim Phillips to_talitos_ptr(talitos_ptr, dma_addr); 1819c4a7965SKim Phillips talitos_ptr->j_extent = extent; 1829c4a7965SKim Phillips } 1839c4a7965SKim Phillips 1849c4a7965SKim Phillips /* 1859c4a7965SKim Phillips * unmap bus single (contiguous) h/w descriptor pointer 1869c4a7965SKim Phillips */ 1879c4a7965SKim Phillips static void unmap_single_talitos_ptr(struct device *dev, 1889c4a7965SKim Phillips struct talitos_ptr *talitos_ptr, 1899c4a7965SKim Phillips enum dma_data_direction dir) 1909c4a7965SKim Phillips { 1919c4a7965SKim Phillips dma_unmap_single(dev, be32_to_cpu(talitos_ptr->ptr), 1929c4a7965SKim Phillips be16_to_cpu(talitos_ptr->len), dir); 1939c4a7965SKim Phillips } 1949c4a7965SKim Phillips 1959c4a7965SKim Phillips static int reset_channel(struct device *dev, int ch) 1969c4a7965SKim Phillips { 1979c4a7965SKim Phillips struct talitos_private *priv = dev_get_drvdata(dev); 1989c4a7965SKim Phillips unsigned int timeout = TALITOS_TIMEOUT; 1999c4a7965SKim Phillips 2009c4a7965SKim Phillips setbits32(priv->reg + TALITOS_CCCR(ch), TALITOS_CCCR_RESET); 2019c4a7965SKim Phillips 2029c4a7965SKim Phillips while ((in_be32(priv->reg + TALITOS_CCCR(ch)) & TALITOS_CCCR_RESET) 2039c4a7965SKim Phillips && --timeout) 2049c4a7965SKim Phillips cpu_relax(); 2059c4a7965SKim Phillips 2069c4a7965SKim Phillips if (timeout == 0) { 2079c4a7965SKim Phillips dev_err(dev, "failed to reset channel %d\n", ch); 2089c4a7965SKim Phillips return -EIO; 2099c4a7965SKim Phillips } 2109c4a7965SKim Phillips 21181eb024cSKim Phillips /* set 36-bit addressing, done writeback enable and done IRQ enable */ 21281eb024cSKim Phillips setbits32(priv->reg + TALITOS_CCCR_LO(ch), TALITOS_CCCR_LO_EAE | 21381eb024cSKim Phillips TALITOS_CCCR_LO_CDWE | TALITOS_CCCR_LO_CDIE); 2149c4a7965SKim Phillips 215fe5720e2SKim Phillips /* and ICCR writeback, if available */ 216fe5720e2SKim Phillips if (priv->features & TALITOS_FTR_HW_AUTH_CHECK) 217fe5720e2SKim Phillips setbits32(priv->reg + TALITOS_CCCR_LO(ch), 218fe5720e2SKim Phillips TALITOS_CCCR_LO_IWSE); 219fe5720e2SKim Phillips 2209c4a7965SKim Phillips return 0; 2219c4a7965SKim Phillips } 2229c4a7965SKim Phillips 2239c4a7965SKim Phillips static int reset_device(struct device *dev) 2249c4a7965SKim Phillips { 2259c4a7965SKim Phillips struct talitos_private *priv = dev_get_drvdata(dev); 2269c4a7965SKim Phillips unsigned int timeout = TALITOS_TIMEOUT; 2279c4a7965SKim Phillips 2289c4a7965SKim Phillips setbits32(priv->reg + TALITOS_MCR, TALITOS_MCR_SWR); 2299c4a7965SKim Phillips 2309c4a7965SKim Phillips while ((in_be32(priv->reg + TALITOS_MCR) & TALITOS_MCR_SWR) 2319c4a7965SKim Phillips && --timeout) 2329c4a7965SKim Phillips cpu_relax(); 2339c4a7965SKim Phillips 2349c4a7965SKim Phillips if (timeout == 0) { 2359c4a7965SKim Phillips dev_err(dev, "failed to reset device\n"); 2369c4a7965SKim Phillips return -EIO; 2379c4a7965SKim Phillips } 2389c4a7965SKim Phillips 2399c4a7965SKim Phillips return 0; 2409c4a7965SKim Phillips } 2419c4a7965SKim Phillips 2429c4a7965SKim Phillips /* 2439c4a7965SKim Phillips * Reset and initialize the device 2449c4a7965SKim Phillips */ 2459c4a7965SKim Phillips static int init_device(struct device *dev) 2469c4a7965SKim Phillips { 2479c4a7965SKim Phillips struct talitos_private *priv = dev_get_drvdata(dev); 2489c4a7965SKim Phillips int ch, err; 2499c4a7965SKim Phillips 2509c4a7965SKim Phillips /* 2519c4a7965SKim Phillips * Master reset 2529c4a7965SKim Phillips * errata documentation: warning: certain SEC interrupts 2539c4a7965SKim Phillips * are not fully cleared by writing the MCR:SWR bit, 2549c4a7965SKim Phillips * set bit twice to completely reset 2559c4a7965SKim Phillips */ 2569c4a7965SKim Phillips err = reset_device(dev); 2579c4a7965SKim Phillips if (err) 2589c4a7965SKim Phillips return err; 2599c4a7965SKim Phillips 2609c4a7965SKim Phillips err = reset_device(dev); 2619c4a7965SKim Phillips if (err) 2629c4a7965SKim Phillips return err; 2639c4a7965SKim Phillips 2649c4a7965SKim Phillips /* reset channels */ 2659c4a7965SKim Phillips for (ch = 0; ch < priv->num_channels; ch++) { 2669c4a7965SKim Phillips err = reset_channel(dev, ch); 2679c4a7965SKim Phillips if (err) 2689c4a7965SKim Phillips return err; 2699c4a7965SKim Phillips } 2709c4a7965SKim Phillips 2719c4a7965SKim Phillips /* enable channel done and error interrupts */ 2729c4a7965SKim Phillips setbits32(priv->reg + TALITOS_IMR, TALITOS_IMR_INIT); 2739c4a7965SKim Phillips setbits32(priv->reg + TALITOS_IMR_LO, TALITOS_IMR_LO_INIT); 2749c4a7965SKim Phillips 275fe5720e2SKim Phillips /* disable integrity check error interrupts (use writeback instead) */ 276fe5720e2SKim Phillips if (priv->features & TALITOS_FTR_HW_AUTH_CHECK) 277fe5720e2SKim Phillips setbits32(priv->reg + TALITOS_MDEUICR_LO, 278fe5720e2SKim Phillips TALITOS_MDEUICR_LO_ICE); 279fe5720e2SKim Phillips 2809c4a7965SKim Phillips return 0; 2819c4a7965SKim Phillips } 2829c4a7965SKim Phillips 2839c4a7965SKim Phillips /** 2849c4a7965SKim Phillips * talitos_submit - submits a descriptor to the device for processing 2859c4a7965SKim Phillips * @dev: the SEC device to be used 2865228f0f7SKim Phillips * @ch: the SEC device channel to be used 2879c4a7965SKim Phillips * @desc: the descriptor to be processed by the device 2889c4a7965SKim Phillips * @callback: whom to call when processing is complete 2899c4a7965SKim Phillips * @context: a handle for use by caller (optional) 2909c4a7965SKim Phillips * 2919c4a7965SKim Phillips * desc must contain valid dma-mapped (bus physical) address pointers. 2929c4a7965SKim Phillips * callback must check err and feedback in descriptor header 2939c4a7965SKim Phillips * for device processing status. 2949c4a7965SKim Phillips */ 2955228f0f7SKim Phillips static int talitos_submit(struct device *dev, int ch, struct talitos_desc *desc, 2969c4a7965SKim Phillips void (*callback)(struct device *dev, 2979c4a7965SKim Phillips struct talitos_desc *desc, 2989c4a7965SKim Phillips void *context, int error), 2999c4a7965SKim Phillips void *context) 3009c4a7965SKim Phillips { 3019c4a7965SKim Phillips struct talitos_private *priv = dev_get_drvdata(dev); 3029c4a7965SKim Phillips struct talitos_request *request; 3035228f0f7SKim Phillips unsigned long flags; 3049c4a7965SKim Phillips int head; 3059c4a7965SKim Phillips 3064b992628SKim Phillips spin_lock_irqsave(&priv->chan[ch].head_lock, flags); 3079c4a7965SKim Phillips 3084b992628SKim Phillips if (!atomic_inc_not_zero(&priv->chan[ch].submit_count)) { 309ec6644d6SKim Phillips /* h/w fifo is full */ 3104b992628SKim Phillips spin_unlock_irqrestore(&priv->chan[ch].head_lock, flags); 3119c4a7965SKim Phillips return -EAGAIN; 3129c4a7965SKim Phillips } 3139c4a7965SKim Phillips 3144b992628SKim Phillips head = priv->chan[ch].head; 3154b992628SKim Phillips request = &priv->chan[ch].fifo[head]; 316ec6644d6SKim Phillips 3179c4a7965SKim Phillips /* map descriptor and save caller data */ 3189c4a7965SKim Phillips request->dma_desc = dma_map_single(dev, desc, sizeof(*desc), 3199c4a7965SKim Phillips DMA_BIDIRECTIONAL); 3209c4a7965SKim Phillips request->callback = callback; 3219c4a7965SKim Phillips request->context = context; 3229c4a7965SKim Phillips 3239c4a7965SKim Phillips /* increment fifo head */ 3244b992628SKim Phillips priv->chan[ch].head = (priv->chan[ch].head + 1) & (priv->fifo_len - 1); 3259c4a7965SKim Phillips 3269c4a7965SKim Phillips smp_wmb(); 3279c4a7965SKim Phillips request->desc = desc; 3289c4a7965SKim Phillips 3299c4a7965SKim Phillips /* GO! */ 3309c4a7965SKim Phillips wmb(); 331a752447aSKim Phillips out_be32(priv->reg + TALITOS_FF(ch), upper_32_bits(request->dma_desc)); 33281eb024cSKim Phillips out_be32(priv->reg + TALITOS_FF_LO(ch), 333a752447aSKim Phillips lower_32_bits(request->dma_desc)); 3349c4a7965SKim Phillips 3354b992628SKim Phillips spin_unlock_irqrestore(&priv->chan[ch].head_lock, flags); 3369c4a7965SKim Phillips 3379c4a7965SKim Phillips return -EINPROGRESS; 3389c4a7965SKim Phillips } 3399c4a7965SKim Phillips 3409c4a7965SKim Phillips /* 3419c4a7965SKim Phillips * process what was done, notify callback of error if not 3429c4a7965SKim Phillips */ 3439c4a7965SKim Phillips static void flush_channel(struct device *dev, int ch, int error, int reset_ch) 3449c4a7965SKim Phillips { 3459c4a7965SKim Phillips struct talitos_private *priv = dev_get_drvdata(dev); 3469c4a7965SKim Phillips struct talitos_request *request, saved_req; 3479c4a7965SKim Phillips unsigned long flags; 3489c4a7965SKim Phillips int tail, status; 3499c4a7965SKim Phillips 3504b992628SKim Phillips spin_lock_irqsave(&priv->chan[ch].tail_lock, flags); 3519c4a7965SKim Phillips 3524b992628SKim Phillips tail = priv->chan[ch].tail; 3534b992628SKim Phillips while (priv->chan[ch].fifo[tail].desc) { 3544b992628SKim Phillips request = &priv->chan[ch].fifo[tail]; 3559c4a7965SKim Phillips 3569c4a7965SKim Phillips /* descriptors with their done bits set don't get the error */ 3579c4a7965SKim Phillips rmb(); 358ca38a814SLee Nipper if ((request->desc->hdr & DESC_HDR_DONE) == DESC_HDR_DONE) 3599c4a7965SKim Phillips status = 0; 360ca38a814SLee Nipper else 3619c4a7965SKim Phillips if (!error) 3629c4a7965SKim Phillips break; 3639c4a7965SKim Phillips else 3649c4a7965SKim Phillips status = error; 3659c4a7965SKim Phillips 3669c4a7965SKim Phillips dma_unmap_single(dev, request->dma_desc, 367e938e465SKim Phillips sizeof(struct talitos_desc), 368e938e465SKim Phillips DMA_BIDIRECTIONAL); 3699c4a7965SKim Phillips 3709c4a7965SKim Phillips /* copy entries so we can call callback outside lock */ 3719c4a7965SKim Phillips saved_req.desc = request->desc; 3729c4a7965SKim Phillips saved_req.callback = request->callback; 3739c4a7965SKim Phillips saved_req.context = request->context; 3749c4a7965SKim Phillips 3759c4a7965SKim Phillips /* release request entry in fifo */ 3769c4a7965SKim Phillips smp_wmb(); 3779c4a7965SKim Phillips request->desc = NULL; 3789c4a7965SKim Phillips 3799c4a7965SKim Phillips /* increment fifo tail */ 3804b992628SKim Phillips priv->chan[ch].tail = (tail + 1) & (priv->fifo_len - 1); 3819c4a7965SKim Phillips 3824b992628SKim Phillips spin_unlock_irqrestore(&priv->chan[ch].tail_lock, flags); 383ec6644d6SKim Phillips 3844b992628SKim Phillips atomic_dec(&priv->chan[ch].submit_count); 385ec6644d6SKim Phillips 3869c4a7965SKim Phillips saved_req.callback(dev, saved_req.desc, saved_req.context, 3879c4a7965SKim Phillips status); 3889c4a7965SKim Phillips /* channel may resume processing in single desc error case */ 3899c4a7965SKim Phillips if (error && !reset_ch && status == error) 3909c4a7965SKim Phillips return; 3914b992628SKim Phillips spin_lock_irqsave(&priv->chan[ch].tail_lock, flags); 3924b992628SKim Phillips tail = priv->chan[ch].tail; 3939c4a7965SKim Phillips } 3949c4a7965SKim Phillips 3954b992628SKim Phillips spin_unlock_irqrestore(&priv->chan[ch].tail_lock, flags); 3969c4a7965SKim Phillips } 3979c4a7965SKim Phillips 3989c4a7965SKim Phillips /* 3999c4a7965SKim Phillips * process completed requests for channels that have done status 4009c4a7965SKim Phillips */ 4019c4a7965SKim Phillips static void talitos_done(unsigned long data) 4029c4a7965SKim Phillips { 4039c4a7965SKim Phillips struct device *dev = (struct device *)data; 4049c4a7965SKim Phillips struct talitos_private *priv = dev_get_drvdata(dev); 4059c4a7965SKim Phillips int ch; 4069c4a7965SKim Phillips 4079c4a7965SKim Phillips for (ch = 0; ch < priv->num_channels; ch++) 4089c4a7965SKim Phillips flush_channel(dev, ch, 0, 0); 4091c2e8811SLee Nipper 4101c2e8811SLee Nipper /* At this point, all completed channels have been processed. 4111c2e8811SLee Nipper * Unmask done interrupts for channels completed later on. 4121c2e8811SLee Nipper */ 413fe5720e2SKim Phillips setbits32(priv->reg + TALITOS_IMR, TALITOS_IMR_INIT); 414fe5720e2SKim Phillips setbits32(priv->reg + TALITOS_IMR_LO, TALITOS_IMR_LO_INIT); 4159c4a7965SKim Phillips } 4169c4a7965SKim Phillips 4179c4a7965SKim Phillips /* 4189c4a7965SKim Phillips * locate current (offending) descriptor 4199c4a7965SKim Phillips */ 4203e721aebSKim Phillips static u32 current_desc_hdr(struct device *dev, int ch) 4219c4a7965SKim Phillips { 4229c4a7965SKim Phillips struct talitos_private *priv = dev_get_drvdata(dev); 4234b992628SKim Phillips int tail = priv->chan[ch].tail; 4249c4a7965SKim Phillips dma_addr_t cur_desc; 4259c4a7965SKim Phillips 4269c4a7965SKim Phillips cur_desc = in_be32(priv->reg + TALITOS_CDPR_LO(ch)); 4279c4a7965SKim Phillips 4284b992628SKim Phillips while (priv->chan[ch].fifo[tail].dma_desc != cur_desc) { 4299c4a7965SKim Phillips tail = (tail + 1) & (priv->fifo_len - 1); 4304b992628SKim Phillips if (tail == priv->chan[ch].tail) { 4319c4a7965SKim Phillips dev_err(dev, "couldn't locate current descriptor\n"); 4323e721aebSKim Phillips return 0; 4339c4a7965SKim Phillips } 4349c4a7965SKim Phillips } 4359c4a7965SKim Phillips 4363e721aebSKim Phillips return priv->chan[ch].fifo[tail].desc->hdr; 4379c4a7965SKim Phillips } 4389c4a7965SKim Phillips 4399c4a7965SKim Phillips /* 4409c4a7965SKim Phillips * user diagnostics; report root cause of error based on execution unit status 4419c4a7965SKim Phillips */ 4423e721aebSKim Phillips static void report_eu_error(struct device *dev, int ch, u32 desc_hdr) 4439c4a7965SKim Phillips { 4449c4a7965SKim Phillips struct talitos_private *priv = dev_get_drvdata(dev); 4459c4a7965SKim Phillips int i; 4469c4a7965SKim Phillips 4473e721aebSKim Phillips if (!desc_hdr) 4483e721aebSKim Phillips desc_hdr = in_be32(priv->reg + TALITOS_DESCBUF(ch)); 4493e721aebSKim Phillips 4503e721aebSKim Phillips switch (desc_hdr & DESC_HDR_SEL0_MASK) { 4519c4a7965SKim Phillips case DESC_HDR_SEL0_AFEU: 4529c4a7965SKim Phillips dev_err(dev, "AFEUISR 0x%08x_%08x\n", 4539c4a7965SKim Phillips in_be32(priv->reg + TALITOS_AFEUISR), 4549c4a7965SKim Phillips in_be32(priv->reg + TALITOS_AFEUISR_LO)); 4559c4a7965SKim Phillips break; 4569c4a7965SKim Phillips case DESC_HDR_SEL0_DEU: 4579c4a7965SKim Phillips dev_err(dev, "DEUISR 0x%08x_%08x\n", 4589c4a7965SKim Phillips in_be32(priv->reg + TALITOS_DEUISR), 4599c4a7965SKim Phillips in_be32(priv->reg + TALITOS_DEUISR_LO)); 4609c4a7965SKim Phillips break; 4619c4a7965SKim Phillips case DESC_HDR_SEL0_MDEUA: 4629c4a7965SKim Phillips case DESC_HDR_SEL0_MDEUB: 4639c4a7965SKim Phillips dev_err(dev, "MDEUISR 0x%08x_%08x\n", 4649c4a7965SKim Phillips in_be32(priv->reg + TALITOS_MDEUISR), 4659c4a7965SKim Phillips in_be32(priv->reg + TALITOS_MDEUISR_LO)); 4669c4a7965SKim Phillips break; 4679c4a7965SKim Phillips case DESC_HDR_SEL0_RNG: 4689c4a7965SKim Phillips dev_err(dev, "RNGUISR 0x%08x_%08x\n", 4699c4a7965SKim Phillips in_be32(priv->reg + TALITOS_RNGUISR), 4709c4a7965SKim Phillips in_be32(priv->reg + TALITOS_RNGUISR_LO)); 4719c4a7965SKim Phillips break; 4729c4a7965SKim Phillips case DESC_HDR_SEL0_PKEU: 4739c4a7965SKim Phillips dev_err(dev, "PKEUISR 0x%08x_%08x\n", 4749c4a7965SKim Phillips in_be32(priv->reg + TALITOS_PKEUISR), 4759c4a7965SKim Phillips in_be32(priv->reg + TALITOS_PKEUISR_LO)); 4769c4a7965SKim Phillips break; 4779c4a7965SKim Phillips case DESC_HDR_SEL0_AESU: 4789c4a7965SKim Phillips dev_err(dev, "AESUISR 0x%08x_%08x\n", 4799c4a7965SKim Phillips in_be32(priv->reg + TALITOS_AESUISR), 4809c4a7965SKim Phillips in_be32(priv->reg + TALITOS_AESUISR_LO)); 4819c4a7965SKim Phillips break; 4829c4a7965SKim Phillips case DESC_HDR_SEL0_CRCU: 4839c4a7965SKim Phillips dev_err(dev, "CRCUISR 0x%08x_%08x\n", 4849c4a7965SKim Phillips in_be32(priv->reg + TALITOS_CRCUISR), 4859c4a7965SKim Phillips in_be32(priv->reg + TALITOS_CRCUISR_LO)); 4869c4a7965SKim Phillips break; 4879c4a7965SKim Phillips case DESC_HDR_SEL0_KEU: 4889c4a7965SKim Phillips dev_err(dev, "KEUISR 0x%08x_%08x\n", 4899c4a7965SKim Phillips in_be32(priv->reg + TALITOS_KEUISR), 4909c4a7965SKim Phillips in_be32(priv->reg + TALITOS_KEUISR_LO)); 4919c4a7965SKim Phillips break; 4929c4a7965SKim Phillips } 4939c4a7965SKim Phillips 4943e721aebSKim Phillips switch (desc_hdr & DESC_HDR_SEL1_MASK) { 4959c4a7965SKim Phillips case DESC_HDR_SEL1_MDEUA: 4969c4a7965SKim Phillips case DESC_HDR_SEL1_MDEUB: 4979c4a7965SKim Phillips dev_err(dev, "MDEUISR 0x%08x_%08x\n", 4989c4a7965SKim Phillips in_be32(priv->reg + TALITOS_MDEUISR), 4999c4a7965SKim Phillips in_be32(priv->reg + TALITOS_MDEUISR_LO)); 5009c4a7965SKim Phillips break; 5019c4a7965SKim Phillips case DESC_HDR_SEL1_CRCU: 5029c4a7965SKim Phillips dev_err(dev, "CRCUISR 0x%08x_%08x\n", 5039c4a7965SKim Phillips in_be32(priv->reg + TALITOS_CRCUISR), 5049c4a7965SKim Phillips in_be32(priv->reg + TALITOS_CRCUISR_LO)); 5059c4a7965SKim Phillips break; 5069c4a7965SKim Phillips } 5079c4a7965SKim Phillips 5089c4a7965SKim Phillips for (i = 0; i < 8; i++) 5099c4a7965SKim Phillips dev_err(dev, "DESCBUF 0x%08x_%08x\n", 5109c4a7965SKim Phillips in_be32(priv->reg + TALITOS_DESCBUF(ch) + 8*i), 5119c4a7965SKim Phillips in_be32(priv->reg + TALITOS_DESCBUF_LO(ch) + 8*i)); 5129c4a7965SKim Phillips } 5139c4a7965SKim Phillips 5149c4a7965SKim Phillips /* 5159c4a7965SKim Phillips * recover from error interrupts 5169c4a7965SKim Phillips */ 51740405f10SKim Phillips static void talitos_error(unsigned long data, u32 isr, u32 isr_lo) 5189c4a7965SKim Phillips { 5199c4a7965SKim Phillips struct device *dev = (struct device *)data; 5209c4a7965SKim Phillips struct talitos_private *priv = dev_get_drvdata(dev); 5219c4a7965SKim Phillips unsigned int timeout = TALITOS_TIMEOUT; 5229c4a7965SKim Phillips int ch, error, reset_dev = 0, reset_ch = 0; 52340405f10SKim Phillips u32 v, v_lo; 5249c4a7965SKim Phillips 5259c4a7965SKim Phillips for (ch = 0; ch < priv->num_channels; ch++) { 5269c4a7965SKim Phillips /* skip channels without errors */ 5279c4a7965SKim Phillips if (!(isr & (1 << (ch * 2 + 1)))) 5289c4a7965SKim Phillips continue; 5299c4a7965SKim Phillips 5309c4a7965SKim Phillips error = -EINVAL; 5319c4a7965SKim Phillips 5329c4a7965SKim Phillips v = in_be32(priv->reg + TALITOS_CCPSR(ch)); 5339c4a7965SKim Phillips v_lo = in_be32(priv->reg + TALITOS_CCPSR_LO(ch)); 5349c4a7965SKim Phillips 5359c4a7965SKim Phillips if (v_lo & TALITOS_CCPSR_LO_DOF) { 5369c4a7965SKim Phillips dev_err(dev, "double fetch fifo overflow error\n"); 5379c4a7965SKim Phillips error = -EAGAIN; 5389c4a7965SKim Phillips reset_ch = 1; 5399c4a7965SKim Phillips } 5409c4a7965SKim Phillips if (v_lo & TALITOS_CCPSR_LO_SOF) { 5419c4a7965SKim Phillips /* h/w dropped descriptor */ 5429c4a7965SKim Phillips dev_err(dev, "single fetch fifo overflow error\n"); 5439c4a7965SKim Phillips error = -EAGAIN; 5449c4a7965SKim Phillips } 5459c4a7965SKim Phillips if (v_lo & TALITOS_CCPSR_LO_MDTE) 5469c4a7965SKim Phillips dev_err(dev, "master data transfer error\n"); 5479c4a7965SKim Phillips if (v_lo & TALITOS_CCPSR_LO_SGDLZ) 5489c4a7965SKim Phillips dev_err(dev, "s/g data length zero error\n"); 5499c4a7965SKim Phillips if (v_lo & TALITOS_CCPSR_LO_FPZ) 5509c4a7965SKim Phillips dev_err(dev, "fetch pointer zero error\n"); 5519c4a7965SKim Phillips if (v_lo & TALITOS_CCPSR_LO_IDH) 5529c4a7965SKim Phillips dev_err(dev, "illegal descriptor header error\n"); 5539c4a7965SKim Phillips if (v_lo & TALITOS_CCPSR_LO_IEU) 5549c4a7965SKim Phillips dev_err(dev, "invalid execution unit error\n"); 5559c4a7965SKim Phillips if (v_lo & TALITOS_CCPSR_LO_EU) 5563e721aebSKim Phillips report_eu_error(dev, ch, current_desc_hdr(dev, ch)); 5579c4a7965SKim Phillips if (v_lo & TALITOS_CCPSR_LO_GB) 5589c4a7965SKim Phillips dev_err(dev, "gather boundary error\n"); 5599c4a7965SKim Phillips if (v_lo & TALITOS_CCPSR_LO_GRL) 5609c4a7965SKim Phillips dev_err(dev, "gather return/length error\n"); 5619c4a7965SKim Phillips if (v_lo & TALITOS_CCPSR_LO_SB) 5629c4a7965SKim Phillips dev_err(dev, "scatter boundary error\n"); 5639c4a7965SKim Phillips if (v_lo & TALITOS_CCPSR_LO_SRL) 5649c4a7965SKim Phillips dev_err(dev, "scatter return/length error\n"); 5659c4a7965SKim Phillips 5669c4a7965SKim Phillips flush_channel(dev, ch, error, reset_ch); 5679c4a7965SKim Phillips 5689c4a7965SKim Phillips if (reset_ch) { 5699c4a7965SKim Phillips reset_channel(dev, ch); 5709c4a7965SKim Phillips } else { 5719c4a7965SKim Phillips setbits32(priv->reg + TALITOS_CCCR(ch), 5729c4a7965SKim Phillips TALITOS_CCCR_CONT); 5739c4a7965SKim Phillips setbits32(priv->reg + TALITOS_CCCR_LO(ch), 0); 5749c4a7965SKim Phillips while ((in_be32(priv->reg + TALITOS_CCCR(ch)) & 5759c4a7965SKim Phillips TALITOS_CCCR_CONT) && --timeout) 5769c4a7965SKim Phillips cpu_relax(); 5779c4a7965SKim Phillips if (timeout == 0) { 5789c4a7965SKim Phillips dev_err(dev, "failed to restart channel %d\n", 5799c4a7965SKim Phillips ch); 5809c4a7965SKim Phillips reset_dev = 1; 5819c4a7965SKim Phillips } 5829c4a7965SKim Phillips } 5839c4a7965SKim Phillips } 5849c4a7965SKim Phillips if (reset_dev || isr & ~TALITOS_ISR_CHERR || isr_lo) { 5859c4a7965SKim Phillips dev_err(dev, "done overflow, internal time out, or rngu error: " 5869c4a7965SKim Phillips "ISR 0x%08x_%08x\n", isr, isr_lo); 5879c4a7965SKim Phillips 5889c4a7965SKim Phillips /* purge request queues */ 5899c4a7965SKim Phillips for (ch = 0; ch < priv->num_channels; ch++) 5909c4a7965SKim Phillips flush_channel(dev, ch, -EIO, 1); 5919c4a7965SKim Phillips 5929c4a7965SKim Phillips /* reset and reinitialize the device */ 5939c4a7965SKim Phillips init_device(dev); 5949c4a7965SKim Phillips } 5959c4a7965SKim Phillips } 5969c4a7965SKim Phillips 5979c4a7965SKim Phillips static irqreturn_t talitos_interrupt(int irq, void *data) 5989c4a7965SKim Phillips { 5999c4a7965SKim Phillips struct device *dev = data; 6009c4a7965SKim Phillips struct talitos_private *priv = dev_get_drvdata(dev); 6019c4a7965SKim Phillips u32 isr, isr_lo; 6029c4a7965SKim Phillips 6039c4a7965SKim Phillips isr = in_be32(priv->reg + TALITOS_ISR); 6049c4a7965SKim Phillips isr_lo = in_be32(priv->reg + TALITOS_ISR_LO); 605ca38a814SLee Nipper /* Acknowledge interrupt */ 6069c4a7965SKim Phillips out_be32(priv->reg + TALITOS_ICR, isr); 6079c4a7965SKim Phillips out_be32(priv->reg + TALITOS_ICR_LO, isr_lo); 6089c4a7965SKim Phillips 609ca38a814SLee Nipper if (unlikely((isr & ~TALITOS_ISR_CHDONE) || isr_lo)) 61040405f10SKim Phillips talitos_error((unsigned long)data, isr, isr_lo); 611ca38a814SLee Nipper else 6121c2e8811SLee Nipper if (likely(isr & TALITOS_ISR_CHDONE)) { 6131c2e8811SLee Nipper /* mask further done interrupts. */ 6141c2e8811SLee Nipper clrbits32(priv->reg + TALITOS_IMR, TALITOS_IMR_DONE); 6151c2e8811SLee Nipper /* done_task will unmask done interrupts at exit */ 6169c4a7965SKim Phillips tasklet_schedule(&priv->done_task); 6171c2e8811SLee Nipper } 6189c4a7965SKim Phillips 6199c4a7965SKim Phillips return (isr || isr_lo) ? IRQ_HANDLED : IRQ_NONE; 6209c4a7965SKim Phillips } 6219c4a7965SKim Phillips 6229c4a7965SKim Phillips /* 6239c4a7965SKim Phillips * hwrng 6249c4a7965SKim Phillips */ 6259c4a7965SKim Phillips static int talitos_rng_data_present(struct hwrng *rng, int wait) 6269c4a7965SKim Phillips { 6279c4a7965SKim Phillips struct device *dev = (struct device *)rng->priv; 6289c4a7965SKim Phillips struct talitos_private *priv = dev_get_drvdata(dev); 6299c4a7965SKim Phillips u32 ofl; 6309c4a7965SKim Phillips int i; 6319c4a7965SKim Phillips 6329c4a7965SKim Phillips for (i = 0; i < 20; i++) { 6339c4a7965SKim Phillips ofl = in_be32(priv->reg + TALITOS_RNGUSR_LO) & 6349c4a7965SKim Phillips TALITOS_RNGUSR_LO_OFL; 6359c4a7965SKim Phillips if (ofl || !wait) 6369c4a7965SKim Phillips break; 6379c4a7965SKim Phillips udelay(10); 6389c4a7965SKim Phillips } 6399c4a7965SKim Phillips 6409c4a7965SKim Phillips return !!ofl; 6419c4a7965SKim Phillips } 6429c4a7965SKim Phillips 6439c4a7965SKim Phillips static int talitos_rng_data_read(struct hwrng *rng, u32 *data) 6449c4a7965SKim Phillips { 6459c4a7965SKim Phillips struct device *dev = (struct device *)rng->priv; 6469c4a7965SKim Phillips struct talitos_private *priv = dev_get_drvdata(dev); 6479c4a7965SKim Phillips 6489c4a7965SKim Phillips /* rng fifo requires 64-bit accesses */ 6499c4a7965SKim Phillips *data = in_be32(priv->reg + TALITOS_RNGU_FIFO); 6509c4a7965SKim Phillips *data = in_be32(priv->reg + TALITOS_RNGU_FIFO_LO); 6519c4a7965SKim Phillips 6529c4a7965SKim Phillips return sizeof(u32); 6539c4a7965SKim Phillips } 6549c4a7965SKim Phillips 6559c4a7965SKim Phillips static int talitos_rng_init(struct hwrng *rng) 6569c4a7965SKim Phillips { 6579c4a7965SKim Phillips struct device *dev = (struct device *)rng->priv; 6589c4a7965SKim Phillips struct talitos_private *priv = dev_get_drvdata(dev); 6599c4a7965SKim Phillips unsigned int timeout = TALITOS_TIMEOUT; 6609c4a7965SKim Phillips 6619c4a7965SKim Phillips setbits32(priv->reg + TALITOS_RNGURCR_LO, TALITOS_RNGURCR_LO_SR); 6629c4a7965SKim Phillips while (!(in_be32(priv->reg + TALITOS_RNGUSR_LO) & TALITOS_RNGUSR_LO_RD) 6639c4a7965SKim Phillips && --timeout) 6649c4a7965SKim Phillips cpu_relax(); 6659c4a7965SKim Phillips if (timeout == 0) { 6669c4a7965SKim Phillips dev_err(dev, "failed to reset rng hw\n"); 6679c4a7965SKim Phillips return -ENODEV; 6689c4a7965SKim Phillips } 6699c4a7965SKim Phillips 6709c4a7965SKim Phillips /* start generating */ 6719c4a7965SKim Phillips setbits32(priv->reg + TALITOS_RNGUDSR_LO, 0); 6729c4a7965SKim Phillips 6739c4a7965SKim Phillips return 0; 6749c4a7965SKim Phillips } 6759c4a7965SKim Phillips 6769c4a7965SKim Phillips static int talitos_register_rng(struct device *dev) 6779c4a7965SKim Phillips { 6789c4a7965SKim Phillips struct talitos_private *priv = dev_get_drvdata(dev); 6799c4a7965SKim Phillips 6809c4a7965SKim Phillips priv->rng.name = dev_driver_string(dev), 6819c4a7965SKim Phillips priv->rng.init = talitos_rng_init, 6829c4a7965SKim Phillips priv->rng.data_present = talitos_rng_data_present, 6839c4a7965SKim Phillips priv->rng.data_read = talitos_rng_data_read, 6849c4a7965SKim Phillips priv->rng.priv = (unsigned long)dev; 6859c4a7965SKim Phillips 6869c4a7965SKim Phillips return hwrng_register(&priv->rng); 6879c4a7965SKim Phillips } 6889c4a7965SKim Phillips 6899c4a7965SKim Phillips static void talitos_unregister_rng(struct device *dev) 6909c4a7965SKim Phillips { 6919c4a7965SKim Phillips struct talitos_private *priv = dev_get_drvdata(dev); 6929c4a7965SKim Phillips 6939c4a7965SKim Phillips hwrng_unregister(&priv->rng); 6949c4a7965SKim Phillips } 6959c4a7965SKim Phillips 6969c4a7965SKim Phillips /* 6979c4a7965SKim Phillips * crypto alg 6989c4a7965SKim Phillips */ 6999c4a7965SKim Phillips #define TALITOS_CRA_PRIORITY 3000 7009c4a7965SKim Phillips #define TALITOS_MAX_KEY_SIZE 64 7013952f17eSLee Nipper #define TALITOS_MAX_IV_LENGTH 16 /* max of AES_BLOCK_SIZE, DES3_EDE_BLOCK_SIZE */ 70270bcaca7SLee Nipper 703497f2e6bSLee Nipper #define MD5_BLOCK_SIZE 64 7049c4a7965SKim Phillips 7059c4a7965SKim Phillips struct talitos_ctx { 7069c4a7965SKim Phillips struct device *dev; 7075228f0f7SKim Phillips int ch; 7089c4a7965SKim Phillips __be32 desc_hdr_template; 7099c4a7965SKim Phillips u8 key[TALITOS_MAX_KEY_SIZE]; 71070bcaca7SLee Nipper u8 iv[TALITOS_MAX_IV_LENGTH]; 7119c4a7965SKim Phillips unsigned int keylen; 7129c4a7965SKim Phillips unsigned int enckeylen; 7139c4a7965SKim Phillips unsigned int authkeylen; 7149c4a7965SKim Phillips unsigned int authsize; 7159c4a7965SKim Phillips }; 7169c4a7965SKim Phillips 717497f2e6bSLee Nipper #define HASH_MAX_BLOCK_SIZE SHA512_BLOCK_SIZE 718497f2e6bSLee Nipper #define TALITOS_MDEU_MAX_CONTEXT_SIZE TALITOS_MDEU_CONTEXT_SIZE_SHA384_SHA512 719497f2e6bSLee Nipper 720497f2e6bSLee Nipper struct talitos_ahash_req_ctx { 72160f208d7SKim Phillips u32 hw_context[TALITOS_MDEU_MAX_CONTEXT_SIZE / sizeof(u32)]; 722497f2e6bSLee Nipper unsigned int hw_context_size; 723497f2e6bSLee Nipper u8 buf[HASH_MAX_BLOCK_SIZE]; 724497f2e6bSLee Nipper u8 bufnext[HASH_MAX_BLOCK_SIZE]; 72560f208d7SKim Phillips unsigned int swinit; 726497f2e6bSLee Nipper unsigned int first; 727497f2e6bSLee Nipper unsigned int last; 728497f2e6bSLee Nipper unsigned int to_hash_later; 7295e833bc4SLee Nipper u64 nbuf; 730497f2e6bSLee Nipper struct scatterlist bufsl[2]; 731497f2e6bSLee Nipper struct scatterlist *psrc; 732497f2e6bSLee Nipper }; 733497f2e6bSLee Nipper 73456af8cd4SLee Nipper static int aead_setauthsize(struct crypto_aead *authenc, 7359c4a7965SKim Phillips unsigned int authsize) 7369c4a7965SKim Phillips { 7379c4a7965SKim Phillips struct talitos_ctx *ctx = crypto_aead_ctx(authenc); 7389c4a7965SKim Phillips 7399c4a7965SKim Phillips ctx->authsize = authsize; 7409c4a7965SKim Phillips 7419c4a7965SKim Phillips return 0; 7429c4a7965SKim Phillips } 7439c4a7965SKim Phillips 74456af8cd4SLee Nipper static int aead_setkey(struct crypto_aead *authenc, 7459c4a7965SKim Phillips const u8 *key, unsigned int keylen) 7469c4a7965SKim Phillips { 7479c4a7965SKim Phillips struct talitos_ctx *ctx = crypto_aead_ctx(authenc); 7489c4a7965SKim Phillips struct rtattr *rta = (void *)key; 7499c4a7965SKim Phillips struct crypto_authenc_key_param *param; 7509c4a7965SKim Phillips unsigned int authkeylen; 7519c4a7965SKim Phillips unsigned int enckeylen; 7529c4a7965SKim Phillips 7539c4a7965SKim Phillips if (!RTA_OK(rta, keylen)) 7549c4a7965SKim Phillips goto badkey; 7559c4a7965SKim Phillips 7569c4a7965SKim Phillips if (rta->rta_type != CRYPTO_AUTHENC_KEYA_PARAM) 7579c4a7965SKim Phillips goto badkey; 7589c4a7965SKim Phillips 7599c4a7965SKim Phillips if (RTA_PAYLOAD(rta) < sizeof(*param)) 7609c4a7965SKim Phillips goto badkey; 7619c4a7965SKim Phillips 7629c4a7965SKim Phillips param = RTA_DATA(rta); 7639c4a7965SKim Phillips enckeylen = be32_to_cpu(param->enckeylen); 7649c4a7965SKim Phillips 7659c4a7965SKim Phillips key += RTA_ALIGN(rta->rta_len); 7669c4a7965SKim Phillips keylen -= RTA_ALIGN(rta->rta_len); 7679c4a7965SKim Phillips 7689c4a7965SKim Phillips if (keylen < enckeylen) 7699c4a7965SKim Phillips goto badkey; 7709c4a7965SKim Phillips 7719c4a7965SKim Phillips authkeylen = keylen - enckeylen; 7729c4a7965SKim Phillips 7739c4a7965SKim Phillips if (keylen > TALITOS_MAX_KEY_SIZE) 7749c4a7965SKim Phillips goto badkey; 7759c4a7965SKim Phillips 7769c4a7965SKim Phillips memcpy(&ctx->key, key, keylen); 7779c4a7965SKim Phillips 7789c4a7965SKim Phillips ctx->keylen = keylen; 7799c4a7965SKim Phillips ctx->enckeylen = enckeylen; 7809c4a7965SKim Phillips ctx->authkeylen = authkeylen; 7819c4a7965SKim Phillips 7829c4a7965SKim Phillips return 0; 7839c4a7965SKim Phillips 7849c4a7965SKim Phillips badkey: 7859c4a7965SKim Phillips crypto_aead_set_flags(authenc, CRYPTO_TFM_RES_BAD_KEY_LEN); 7869c4a7965SKim Phillips return -EINVAL; 7879c4a7965SKim Phillips } 7889c4a7965SKim Phillips 7899c4a7965SKim Phillips /* 79056af8cd4SLee Nipper * talitos_edesc - s/w-extended descriptor 7919c4a7965SKim Phillips * @src_nents: number of segments in input scatterlist 7929c4a7965SKim Phillips * @dst_nents: number of segments in output scatterlist 7939c4a7965SKim Phillips * @dma_len: length of dma mapped link_tbl space 7949c4a7965SKim Phillips * @dma_link_tbl: bus physical address of link_tbl 7959c4a7965SKim Phillips * @desc: h/w descriptor 7969c4a7965SKim Phillips * @link_tbl: input and output h/w link tables (if {src,dst}_nents > 1) 7979c4a7965SKim Phillips * 7989c4a7965SKim Phillips * if decrypting (with authcheck), or either one of src_nents or dst_nents 7999c4a7965SKim Phillips * is greater than 1, an integrity check value is concatenated to the end 8009c4a7965SKim Phillips * of link_tbl data 8019c4a7965SKim Phillips */ 80256af8cd4SLee Nipper struct talitos_edesc { 8039c4a7965SKim Phillips int src_nents; 8049c4a7965SKim Phillips int dst_nents; 8054de9d0b5SLee Nipper int src_is_chained; 8064de9d0b5SLee Nipper int dst_is_chained; 8079c4a7965SKim Phillips int dma_len; 8089c4a7965SKim Phillips dma_addr_t dma_link_tbl; 8099c4a7965SKim Phillips struct talitos_desc desc; 8109c4a7965SKim Phillips struct talitos_ptr link_tbl[0]; 8119c4a7965SKim Phillips }; 8129c4a7965SKim Phillips 8134de9d0b5SLee Nipper static int talitos_map_sg(struct device *dev, struct scatterlist *sg, 8144de9d0b5SLee Nipper unsigned int nents, enum dma_data_direction dir, 8154de9d0b5SLee Nipper int chained) 8164de9d0b5SLee Nipper { 8174de9d0b5SLee Nipper if (unlikely(chained)) 8184de9d0b5SLee Nipper while (sg) { 8194de9d0b5SLee Nipper dma_map_sg(dev, sg, 1, dir); 8204de9d0b5SLee Nipper sg = scatterwalk_sg_next(sg); 8214de9d0b5SLee Nipper } 8224de9d0b5SLee Nipper else 8234de9d0b5SLee Nipper dma_map_sg(dev, sg, nents, dir); 8244de9d0b5SLee Nipper return nents; 8254de9d0b5SLee Nipper } 8264de9d0b5SLee Nipper 8274de9d0b5SLee Nipper static void talitos_unmap_sg_chain(struct device *dev, struct scatterlist *sg, 8284de9d0b5SLee Nipper enum dma_data_direction dir) 8294de9d0b5SLee Nipper { 8304de9d0b5SLee Nipper while (sg) { 8314de9d0b5SLee Nipper dma_unmap_sg(dev, sg, 1, dir); 8324de9d0b5SLee Nipper sg = scatterwalk_sg_next(sg); 8334de9d0b5SLee Nipper } 8344de9d0b5SLee Nipper } 8354de9d0b5SLee Nipper 8364de9d0b5SLee Nipper static void talitos_sg_unmap(struct device *dev, 8374de9d0b5SLee Nipper struct talitos_edesc *edesc, 8384de9d0b5SLee Nipper struct scatterlist *src, 8394de9d0b5SLee Nipper struct scatterlist *dst) 8404de9d0b5SLee Nipper { 8414de9d0b5SLee Nipper unsigned int src_nents = edesc->src_nents ? : 1; 8424de9d0b5SLee Nipper unsigned int dst_nents = edesc->dst_nents ? : 1; 8434de9d0b5SLee Nipper 8444de9d0b5SLee Nipper if (src != dst) { 8454de9d0b5SLee Nipper if (edesc->src_is_chained) 8464de9d0b5SLee Nipper talitos_unmap_sg_chain(dev, src, DMA_TO_DEVICE); 8474de9d0b5SLee Nipper else 8484de9d0b5SLee Nipper dma_unmap_sg(dev, src, src_nents, DMA_TO_DEVICE); 8494de9d0b5SLee Nipper 850497f2e6bSLee Nipper if (dst) { 8514de9d0b5SLee Nipper if (edesc->dst_is_chained) 852497f2e6bSLee Nipper talitos_unmap_sg_chain(dev, dst, 853497f2e6bSLee Nipper DMA_FROM_DEVICE); 8544de9d0b5SLee Nipper else 855497f2e6bSLee Nipper dma_unmap_sg(dev, dst, dst_nents, 856497f2e6bSLee Nipper DMA_FROM_DEVICE); 857497f2e6bSLee Nipper } 8584de9d0b5SLee Nipper } else 8594de9d0b5SLee Nipper if (edesc->src_is_chained) 8604de9d0b5SLee Nipper talitos_unmap_sg_chain(dev, src, DMA_BIDIRECTIONAL); 8614de9d0b5SLee Nipper else 8624de9d0b5SLee Nipper dma_unmap_sg(dev, src, src_nents, DMA_BIDIRECTIONAL); 8634de9d0b5SLee Nipper } 8644de9d0b5SLee Nipper 8659c4a7965SKim Phillips static void ipsec_esp_unmap(struct device *dev, 86656af8cd4SLee Nipper struct talitos_edesc *edesc, 8679c4a7965SKim Phillips struct aead_request *areq) 8689c4a7965SKim Phillips { 8699c4a7965SKim Phillips unmap_single_talitos_ptr(dev, &edesc->desc.ptr[6], DMA_FROM_DEVICE); 8709c4a7965SKim Phillips unmap_single_talitos_ptr(dev, &edesc->desc.ptr[3], DMA_TO_DEVICE); 8719c4a7965SKim Phillips unmap_single_talitos_ptr(dev, &edesc->desc.ptr[2], DMA_TO_DEVICE); 8729c4a7965SKim Phillips unmap_single_talitos_ptr(dev, &edesc->desc.ptr[0], DMA_TO_DEVICE); 8739c4a7965SKim Phillips 8749c4a7965SKim Phillips dma_unmap_sg(dev, areq->assoc, 1, DMA_TO_DEVICE); 8759c4a7965SKim Phillips 8764de9d0b5SLee Nipper talitos_sg_unmap(dev, edesc, areq->src, areq->dst); 8779c4a7965SKim Phillips 8789c4a7965SKim Phillips if (edesc->dma_len) 8799c4a7965SKim Phillips dma_unmap_single(dev, edesc->dma_link_tbl, edesc->dma_len, 8809c4a7965SKim Phillips DMA_BIDIRECTIONAL); 8819c4a7965SKim Phillips } 8829c4a7965SKim Phillips 8839c4a7965SKim Phillips /* 8849c4a7965SKim Phillips * ipsec_esp descriptor callbacks 8859c4a7965SKim Phillips */ 8869c4a7965SKim Phillips static void ipsec_esp_encrypt_done(struct device *dev, 8879c4a7965SKim Phillips struct talitos_desc *desc, void *context, 8889c4a7965SKim Phillips int err) 8899c4a7965SKim Phillips { 8909c4a7965SKim Phillips struct aead_request *areq = context; 8919c4a7965SKim Phillips struct crypto_aead *authenc = crypto_aead_reqtfm(areq); 8929c4a7965SKim Phillips struct talitos_ctx *ctx = crypto_aead_ctx(authenc); 89319bbbc63SKim Phillips struct talitos_edesc *edesc; 8949c4a7965SKim Phillips struct scatterlist *sg; 8959c4a7965SKim Phillips void *icvdata; 8969c4a7965SKim Phillips 89719bbbc63SKim Phillips edesc = container_of(desc, struct talitos_edesc, desc); 89819bbbc63SKim Phillips 8999c4a7965SKim Phillips ipsec_esp_unmap(dev, edesc, areq); 9009c4a7965SKim Phillips 9019c4a7965SKim Phillips /* copy the generated ICV to dst */ 9029c4a7965SKim Phillips if (edesc->dma_len) { 9039c4a7965SKim Phillips icvdata = &edesc->link_tbl[edesc->src_nents + 904f3c85bc1SLee Nipper edesc->dst_nents + 2]; 9059c4a7965SKim Phillips sg = sg_last(areq->dst, edesc->dst_nents); 9069c4a7965SKim Phillips memcpy((char *)sg_virt(sg) + sg->length - ctx->authsize, 9079c4a7965SKim Phillips icvdata, ctx->authsize); 9089c4a7965SKim Phillips } 9099c4a7965SKim Phillips 9109c4a7965SKim Phillips kfree(edesc); 9119c4a7965SKim Phillips 9129c4a7965SKim Phillips aead_request_complete(areq, err); 9139c4a7965SKim Phillips } 9149c4a7965SKim Phillips 915fe5720e2SKim Phillips static void ipsec_esp_decrypt_swauth_done(struct device *dev, 916e938e465SKim Phillips struct talitos_desc *desc, 917e938e465SKim Phillips void *context, int err) 9189c4a7965SKim Phillips { 9199c4a7965SKim Phillips struct aead_request *req = context; 9209c4a7965SKim Phillips struct crypto_aead *authenc = crypto_aead_reqtfm(req); 9219c4a7965SKim Phillips struct talitos_ctx *ctx = crypto_aead_ctx(authenc); 92219bbbc63SKim Phillips struct talitos_edesc *edesc; 9239c4a7965SKim Phillips struct scatterlist *sg; 9249c4a7965SKim Phillips void *icvdata; 9259c4a7965SKim Phillips 92619bbbc63SKim Phillips edesc = container_of(desc, struct talitos_edesc, desc); 92719bbbc63SKim Phillips 9289c4a7965SKim Phillips ipsec_esp_unmap(dev, edesc, req); 9299c4a7965SKim Phillips 9309c4a7965SKim Phillips if (!err) { 9319c4a7965SKim Phillips /* auth check */ 9329c4a7965SKim Phillips if (edesc->dma_len) 9339c4a7965SKim Phillips icvdata = &edesc->link_tbl[edesc->src_nents + 934f3c85bc1SLee Nipper edesc->dst_nents + 2]; 9359c4a7965SKim Phillips else 9369c4a7965SKim Phillips icvdata = &edesc->link_tbl[0]; 9379c4a7965SKim Phillips 9389c4a7965SKim Phillips sg = sg_last(req->dst, edesc->dst_nents ? : 1); 9399c4a7965SKim Phillips err = memcmp(icvdata, (char *)sg_virt(sg) + sg->length - 9409c4a7965SKim Phillips ctx->authsize, ctx->authsize) ? -EBADMSG : 0; 9419c4a7965SKim Phillips } 9429c4a7965SKim Phillips 9439c4a7965SKim Phillips kfree(edesc); 9449c4a7965SKim Phillips 9459c4a7965SKim Phillips aead_request_complete(req, err); 9469c4a7965SKim Phillips } 9479c4a7965SKim Phillips 948fe5720e2SKim Phillips static void ipsec_esp_decrypt_hwauth_done(struct device *dev, 949e938e465SKim Phillips struct talitos_desc *desc, 950e938e465SKim Phillips void *context, int err) 951fe5720e2SKim Phillips { 952fe5720e2SKim Phillips struct aead_request *req = context; 95319bbbc63SKim Phillips struct talitos_edesc *edesc; 95419bbbc63SKim Phillips 95519bbbc63SKim Phillips edesc = container_of(desc, struct talitos_edesc, desc); 956fe5720e2SKim Phillips 957fe5720e2SKim Phillips ipsec_esp_unmap(dev, edesc, req); 958fe5720e2SKim Phillips 959fe5720e2SKim Phillips /* check ICV auth status */ 960e938e465SKim Phillips if (!err && ((desc->hdr_lo & DESC_HDR_LO_ICCR1_MASK) != 961e938e465SKim Phillips DESC_HDR_LO_ICCR1_PASS)) 962fe5720e2SKim Phillips err = -EBADMSG; 963fe5720e2SKim Phillips 964fe5720e2SKim Phillips kfree(edesc); 965fe5720e2SKim Phillips 966fe5720e2SKim Phillips aead_request_complete(req, err); 967fe5720e2SKim Phillips } 968fe5720e2SKim Phillips 9699c4a7965SKim Phillips /* 9709c4a7965SKim Phillips * convert scatterlist to SEC h/w link table format 9719c4a7965SKim Phillips * stop at cryptlen bytes 9729c4a7965SKim Phillips */ 97370bcaca7SLee Nipper static int sg_to_link_tbl(struct scatterlist *sg, int sg_count, 9749c4a7965SKim Phillips int cryptlen, struct talitos_ptr *link_tbl_ptr) 9759c4a7965SKim Phillips { 97670bcaca7SLee Nipper int n_sg = sg_count; 97770bcaca7SLee Nipper 97870bcaca7SLee Nipper while (n_sg--) { 97981eb024cSKim Phillips to_talitos_ptr(link_tbl_ptr, sg_dma_address(sg)); 9809c4a7965SKim Phillips link_tbl_ptr->len = cpu_to_be16(sg_dma_len(sg)); 9819c4a7965SKim Phillips link_tbl_ptr->j_extent = 0; 9829c4a7965SKim Phillips link_tbl_ptr++; 9839c4a7965SKim Phillips cryptlen -= sg_dma_len(sg); 9844de9d0b5SLee Nipper sg = scatterwalk_sg_next(sg); 9859c4a7965SKim Phillips } 9869c4a7965SKim Phillips 98770bcaca7SLee Nipper /* adjust (decrease) last one (or two) entry's len to cryptlen */ 9889c4a7965SKim Phillips link_tbl_ptr--; 989c0e741d4SKim Phillips while (be16_to_cpu(link_tbl_ptr->len) <= (-cryptlen)) { 99070bcaca7SLee Nipper /* Empty this entry, and move to previous one */ 99170bcaca7SLee Nipper cryptlen += be16_to_cpu(link_tbl_ptr->len); 99270bcaca7SLee Nipper link_tbl_ptr->len = 0; 99370bcaca7SLee Nipper sg_count--; 99470bcaca7SLee Nipper link_tbl_ptr--; 99570bcaca7SLee Nipper } 9969c4a7965SKim Phillips link_tbl_ptr->len = cpu_to_be16(be16_to_cpu(link_tbl_ptr->len) 9979c4a7965SKim Phillips + cryptlen); 9989c4a7965SKim Phillips 9999c4a7965SKim Phillips /* tag end of link table */ 10009c4a7965SKim Phillips link_tbl_ptr->j_extent = DESC_PTR_LNKTBL_RETURN; 100170bcaca7SLee Nipper 100270bcaca7SLee Nipper return sg_count; 10039c4a7965SKim Phillips } 10049c4a7965SKim Phillips 10059c4a7965SKim Phillips /* 10069c4a7965SKim Phillips * fill in and submit ipsec_esp descriptor 10079c4a7965SKim Phillips */ 100856af8cd4SLee Nipper static int ipsec_esp(struct talitos_edesc *edesc, struct aead_request *areq, 10099c4a7965SKim Phillips u8 *giv, u64 seq, 10109c4a7965SKim Phillips void (*callback) (struct device *dev, 10119c4a7965SKim Phillips struct talitos_desc *desc, 10129c4a7965SKim Phillips void *context, int error)) 10139c4a7965SKim Phillips { 10149c4a7965SKim Phillips struct crypto_aead *aead = crypto_aead_reqtfm(areq); 10159c4a7965SKim Phillips struct talitos_ctx *ctx = crypto_aead_ctx(aead); 10169c4a7965SKim Phillips struct device *dev = ctx->dev; 10179c4a7965SKim Phillips struct talitos_desc *desc = &edesc->desc; 10189c4a7965SKim Phillips unsigned int cryptlen = areq->cryptlen; 10199c4a7965SKim Phillips unsigned int authsize = ctx->authsize; 1020e41256f1SKim Phillips unsigned int ivsize = crypto_aead_ivsize(aead); 1021fa86a267SKim Phillips int sg_count, ret; 1022fe5720e2SKim Phillips int sg_link_tbl_len; 10239c4a7965SKim Phillips 10249c4a7965SKim Phillips /* hmac key */ 10259c4a7965SKim Phillips map_single_talitos_ptr(dev, &desc->ptr[0], ctx->authkeylen, &ctx->key, 10269c4a7965SKim Phillips 0, DMA_TO_DEVICE); 10279c4a7965SKim Phillips /* hmac data */ 1028e41256f1SKim Phillips map_single_talitos_ptr(dev, &desc->ptr[1], areq->assoclen + ivsize, 1029e41256f1SKim Phillips sg_virt(areq->assoc), 0, DMA_TO_DEVICE); 10309c4a7965SKim Phillips /* cipher iv */ 10319c4a7965SKim Phillips map_single_talitos_ptr(dev, &desc->ptr[2], ivsize, giv ?: areq->iv, 0, 10329c4a7965SKim Phillips DMA_TO_DEVICE); 10339c4a7965SKim Phillips 10349c4a7965SKim Phillips /* cipher key */ 10359c4a7965SKim Phillips map_single_talitos_ptr(dev, &desc->ptr[3], ctx->enckeylen, 10369c4a7965SKim Phillips (char *)&ctx->key + ctx->authkeylen, 0, 10379c4a7965SKim Phillips DMA_TO_DEVICE); 10389c4a7965SKim Phillips 10399c4a7965SKim Phillips /* 10409c4a7965SKim Phillips * cipher in 10419c4a7965SKim Phillips * map and adjust cipher len to aead request cryptlen. 10429c4a7965SKim Phillips * extent is bytes of HMAC postpended to ciphertext, 10439c4a7965SKim Phillips * typically 12 for ipsec 10449c4a7965SKim Phillips */ 10459c4a7965SKim Phillips desc->ptr[4].len = cpu_to_be16(cryptlen); 10469c4a7965SKim Phillips desc->ptr[4].j_extent = authsize; 10479c4a7965SKim Phillips 1048e938e465SKim Phillips sg_count = talitos_map_sg(dev, areq->src, edesc->src_nents ? : 1, 1049e938e465SKim Phillips (areq->src == areq->dst) ? DMA_BIDIRECTIONAL 1050e938e465SKim Phillips : DMA_TO_DEVICE, 10514de9d0b5SLee Nipper edesc->src_is_chained); 10529c4a7965SKim Phillips 10539c4a7965SKim Phillips if (sg_count == 1) { 105481eb024cSKim Phillips to_talitos_ptr(&desc->ptr[4], sg_dma_address(areq->src)); 10559c4a7965SKim Phillips } else { 1056fe5720e2SKim Phillips sg_link_tbl_len = cryptlen; 1057fe5720e2SKim Phillips 1058962a9c99SKim Phillips if (edesc->desc.hdr & DESC_HDR_MODE1_MDEU_CICV) 1059fe5720e2SKim Phillips sg_link_tbl_len = cryptlen + authsize; 1060e938e465SKim Phillips 1061fe5720e2SKim Phillips sg_count = sg_to_link_tbl(areq->src, sg_count, sg_link_tbl_len, 10629c4a7965SKim Phillips &edesc->link_tbl[0]); 106370bcaca7SLee Nipper if (sg_count > 1) { 10649c4a7965SKim Phillips desc->ptr[4].j_extent |= DESC_PTR_LNKTBL_JUMP; 106581eb024cSKim Phillips to_talitos_ptr(&desc->ptr[4], edesc->dma_link_tbl); 1066e938e465SKim Phillips dma_sync_single_for_device(dev, edesc->dma_link_tbl, 1067e938e465SKim Phillips edesc->dma_len, 1068e938e465SKim Phillips DMA_BIDIRECTIONAL); 106970bcaca7SLee Nipper } else { 107070bcaca7SLee Nipper /* Only one segment now, so no link tbl needed */ 107181eb024cSKim Phillips to_talitos_ptr(&desc->ptr[4], 107281eb024cSKim Phillips sg_dma_address(areq->src)); 107370bcaca7SLee Nipper } 10749c4a7965SKim Phillips } 10759c4a7965SKim Phillips 10769c4a7965SKim Phillips /* cipher out */ 10779c4a7965SKim Phillips desc->ptr[5].len = cpu_to_be16(cryptlen); 10789c4a7965SKim Phillips desc->ptr[5].j_extent = authsize; 10799c4a7965SKim Phillips 1080e938e465SKim Phillips if (areq->src != areq->dst) 10814de9d0b5SLee Nipper sg_count = talitos_map_sg(dev, areq->dst, 10824de9d0b5SLee Nipper edesc->dst_nents ? : 1, 10834de9d0b5SLee Nipper DMA_FROM_DEVICE, 10844de9d0b5SLee Nipper edesc->dst_is_chained); 10859c4a7965SKim Phillips 10869c4a7965SKim Phillips if (sg_count == 1) { 108781eb024cSKim Phillips to_talitos_ptr(&desc->ptr[5], sg_dma_address(areq->dst)); 10889c4a7965SKim Phillips } else { 10899c4a7965SKim Phillips struct talitos_ptr *link_tbl_ptr = 1090f3c85bc1SLee Nipper &edesc->link_tbl[edesc->src_nents + 1]; 10919c4a7965SKim Phillips 109281eb024cSKim Phillips to_talitos_ptr(&desc->ptr[5], edesc->dma_link_tbl + 109381eb024cSKim Phillips (edesc->src_nents + 1) * 109481eb024cSKim Phillips sizeof(struct talitos_ptr)); 109570bcaca7SLee Nipper sg_count = sg_to_link_tbl(areq->dst, sg_count, cryptlen, 10969c4a7965SKim Phillips link_tbl_ptr); 1097fe5720e2SKim Phillips 1098f3c85bc1SLee Nipper /* Add an entry to the link table for ICV data */ 10999c4a7965SKim Phillips link_tbl_ptr += sg_count - 1; 11009c4a7965SKim Phillips link_tbl_ptr->j_extent = 0; 1101f3c85bc1SLee Nipper sg_count++; 11029c4a7965SKim Phillips link_tbl_ptr++; 11039c4a7965SKim Phillips link_tbl_ptr->j_extent = DESC_PTR_LNKTBL_RETURN; 11049c4a7965SKim Phillips link_tbl_ptr->len = cpu_to_be16(authsize); 11059c4a7965SKim Phillips 11069c4a7965SKim Phillips /* icv data follows link tables */ 110781eb024cSKim Phillips to_talitos_ptr(link_tbl_ptr, edesc->dma_link_tbl + 110881eb024cSKim Phillips (edesc->src_nents + edesc->dst_nents + 2) * 110981eb024cSKim Phillips sizeof(struct talitos_ptr)); 11109c4a7965SKim Phillips desc->ptr[5].j_extent |= DESC_PTR_LNKTBL_JUMP; 11119c4a7965SKim Phillips dma_sync_single_for_device(ctx->dev, edesc->dma_link_tbl, 11129c4a7965SKim Phillips edesc->dma_len, DMA_BIDIRECTIONAL); 11139c4a7965SKim Phillips } 11149c4a7965SKim Phillips 11159c4a7965SKim Phillips /* iv out */ 11169c4a7965SKim Phillips map_single_talitos_ptr(dev, &desc->ptr[6], ivsize, ctx->iv, 0, 11179c4a7965SKim Phillips DMA_FROM_DEVICE); 11189c4a7965SKim Phillips 11195228f0f7SKim Phillips ret = talitos_submit(dev, ctx->ch, desc, callback, areq); 1120fa86a267SKim Phillips if (ret != -EINPROGRESS) { 1121fa86a267SKim Phillips ipsec_esp_unmap(dev, edesc, areq); 1122fa86a267SKim Phillips kfree(edesc); 1123fa86a267SKim Phillips } 1124fa86a267SKim Phillips return ret; 11259c4a7965SKim Phillips } 11269c4a7965SKim Phillips 11279c4a7965SKim Phillips /* 11289c4a7965SKim Phillips * derive number of elements in scatterlist 11299c4a7965SKim Phillips */ 11304de9d0b5SLee Nipper static int sg_count(struct scatterlist *sg_list, int nbytes, int *chained) 11319c4a7965SKim Phillips { 11329c4a7965SKim Phillips struct scatterlist *sg = sg_list; 11339c4a7965SKim Phillips int sg_nents = 0; 11349c4a7965SKim Phillips 11354de9d0b5SLee Nipper *chained = 0; 11364de9d0b5SLee Nipper while (nbytes > 0) { 11379c4a7965SKim Phillips sg_nents++; 11389c4a7965SKim Phillips nbytes -= sg->length; 11394de9d0b5SLee Nipper if (!sg_is_last(sg) && (sg + 1)->length == 0) 11404de9d0b5SLee Nipper *chained = 1; 11414de9d0b5SLee Nipper sg = scatterwalk_sg_next(sg); 11429c4a7965SKim Phillips } 11439c4a7965SKim Phillips 11449c4a7965SKim Phillips return sg_nents; 11459c4a7965SKim Phillips } 11469c4a7965SKim Phillips 1147497f2e6bSLee Nipper /** 1148497f2e6bSLee Nipper * sg_copy_end_to_buffer - Copy end data from SG list to a linear buffer 1149497f2e6bSLee Nipper * @sgl: The SG list 1150497f2e6bSLee Nipper * @nents: Number of SG entries 1151497f2e6bSLee Nipper * @buf: Where to copy to 1152497f2e6bSLee Nipper * @buflen: The number of bytes to copy 1153497f2e6bSLee Nipper * @skip: The number of bytes to skip before copying. 1154497f2e6bSLee Nipper * Note: skip + buflen should equal SG total size. 1155497f2e6bSLee Nipper * 1156497f2e6bSLee Nipper * Returns the number of copied bytes. 1157497f2e6bSLee Nipper * 1158497f2e6bSLee Nipper **/ 1159497f2e6bSLee Nipper static size_t sg_copy_end_to_buffer(struct scatterlist *sgl, unsigned int nents, 1160497f2e6bSLee Nipper void *buf, size_t buflen, unsigned int skip) 1161497f2e6bSLee Nipper { 1162497f2e6bSLee Nipper unsigned int offset = 0; 1163497f2e6bSLee Nipper unsigned int boffset = 0; 1164497f2e6bSLee Nipper struct sg_mapping_iter miter; 1165497f2e6bSLee Nipper unsigned long flags; 1166497f2e6bSLee Nipper unsigned int sg_flags = SG_MITER_ATOMIC; 1167497f2e6bSLee Nipper size_t total_buffer = buflen + skip; 1168497f2e6bSLee Nipper 1169497f2e6bSLee Nipper sg_flags |= SG_MITER_FROM_SG; 1170497f2e6bSLee Nipper 1171497f2e6bSLee Nipper sg_miter_start(&miter, sgl, nents, sg_flags); 1172497f2e6bSLee Nipper 1173497f2e6bSLee Nipper local_irq_save(flags); 1174497f2e6bSLee Nipper 1175497f2e6bSLee Nipper while (sg_miter_next(&miter) && offset < total_buffer) { 1176497f2e6bSLee Nipper unsigned int len; 1177497f2e6bSLee Nipper unsigned int ignore; 1178497f2e6bSLee Nipper 1179497f2e6bSLee Nipper if ((offset + miter.length) > skip) { 1180497f2e6bSLee Nipper if (offset < skip) { 1181497f2e6bSLee Nipper /* Copy part of this segment */ 1182497f2e6bSLee Nipper ignore = skip - offset; 1183497f2e6bSLee Nipper len = miter.length - ignore; 11847260042bSLee Nipper if (boffset + len > buflen) 11857260042bSLee Nipper len = buflen - boffset; 1186497f2e6bSLee Nipper memcpy(buf + boffset, miter.addr + ignore, len); 1187497f2e6bSLee Nipper } else { 11887260042bSLee Nipper /* Copy all of this segment (up to buflen) */ 1189497f2e6bSLee Nipper len = miter.length; 11907260042bSLee Nipper if (boffset + len > buflen) 11917260042bSLee Nipper len = buflen - boffset; 1192497f2e6bSLee Nipper memcpy(buf + boffset, miter.addr, len); 1193497f2e6bSLee Nipper } 1194497f2e6bSLee Nipper boffset += len; 1195497f2e6bSLee Nipper } 1196497f2e6bSLee Nipper offset += miter.length; 1197497f2e6bSLee Nipper } 1198497f2e6bSLee Nipper 1199497f2e6bSLee Nipper sg_miter_stop(&miter); 1200497f2e6bSLee Nipper 1201497f2e6bSLee Nipper local_irq_restore(flags); 1202497f2e6bSLee Nipper return boffset; 1203497f2e6bSLee Nipper } 1204497f2e6bSLee Nipper 12059c4a7965SKim Phillips /* 120656af8cd4SLee Nipper * allocate and map the extended descriptor 12079c4a7965SKim Phillips */ 12084de9d0b5SLee Nipper static struct talitos_edesc *talitos_edesc_alloc(struct device *dev, 12094de9d0b5SLee Nipper struct scatterlist *src, 12104de9d0b5SLee Nipper struct scatterlist *dst, 1211497f2e6bSLee Nipper int hash_result, 12124de9d0b5SLee Nipper unsigned int cryptlen, 12134de9d0b5SLee Nipper unsigned int authsize, 12144de9d0b5SLee Nipper int icv_stashing, 12154de9d0b5SLee Nipper u32 cryptoflags) 12169c4a7965SKim Phillips { 121756af8cd4SLee Nipper struct talitos_edesc *edesc; 12189c4a7965SKim Phillips int src_nents, dst_nents, alloc_len, dma_len; 12194de9d0b5SLee Nipper int src_chained, dst_chained = 0; 12204de9d0b5SLee Nipper gfp_t flags = cryptoflags & CRYPTO_TFM_REQ_MAY_SLEEP ? GFP_KERNEL : 1221586725f8SKim Phillips GFP_ATOMIC; 12229c4a7965SKim Phillips 12234de9d0b5SLee Nipper if (cryptlen + authsize > TALITOS_MAX_DATA_LEN) { 12244de9d0b5SLee Nipper dev_err(dev, "length exceeds h/w max limit\n"); 12259c4a7965SKim Phillips return ERR_PTR(-EINVAL); 12269c4a7965SKim Phillips } 12279c4a7965SKim Phillips 12284de9d0b5SLee Nipper src_nents = sg_count(src, cryptlen + authsize, &src_chained); 12299c4a7965SKim Phillips src_nents = (src_nents == 1) ? 0 : src_nents; 12309c4a7965SKim Phillips 1231497f2e6bSLee Nipper if (hash_result) { 1232497f2e6bSLee Nipper dst_nents = 0; 1233497f2e6bSLee Nipper } else { 12344de9d0b5SLee Nipper if (dst == src) { 12359c4a7965SKim Phillips dst_nents = src_nents; 12369c4a7965SKim Phillips } else { 1237497f2e6bSLee Nipper dst_nents = sg_count(dst, cryptlen + authsize, 1238497f2e6bSLee Nipper &dst_chained); 1239695ad589SLee Nipper dst_nents = (dst_nents == 1) ? 0 : dst_nents; 12409c4a7965SKim Phillips } 1241497f2e6bSLee Nipper } 12429c4a7965SKim Phillips 12439c4a7965SKim Phillips /* 12449c4a7965SKim Phillips * allocate space for base edesc plus the link tables, 1245f3c85bc1SLee Nipper * allowing for two separate entries for ICV and generated ICV (+ 2), 12469c4a7965SKim Phillips * and the ICV data itself 12479c4a7965SKim Phillips */ 124856af8cd4SLee Nipper alloc_len = sizeof(struct talitos_edesc); 12499c4a7965SKim Phillips if (src_nents || dst_nents) { 1250f3c85bc1SLee Nipper dma_len = (src_nents + dst_nents + 2) * 12514de9d0b5SLee Nipper sizeof(struct talitos_ptr) + authsize; 12529c4a7965SKim Phillips alloc_len += dma_len; 12539c4a7965SKim Phillips } else { 12549c4a7965SKim Phillips dma_len = 0; 12554de9d0b5SLee Nipper alloc_len += icv_stashing ? authsize : 0; 12569c4a7965SKim Phillips } 12579c4a7965SKim Phillips 1258586725f8SKim Phillips edesc = kmalloc(alloc_len, GFP_DMA | flags); 12599c4a7965SKim Phillips if (!edesc) { 12604de9d0b5SLee Nipper dev_err(dev, "could not allocate edescriptor\n"); 12619c4a7965SKim Phillips return ERR_PTR(-ENOMEM); 12629c4a7965SKim Phillips } 12639c4a7965SKim Phillips 12649c4a7965SKim Phillips edesc->src_nents = src_nents; 12659c4a7965SKim Phillips edesc->dst_nents = dst_nents; 12664de9d0b5SLee Nipper edesc->src_is_chained = src_chained; 12674de9d0b5SLee Nipper edesc->dst_is_chained = dst_chained; 12689c4a7965SKim Phillips edesc->dma_len = dma_len; 1269497f2e6bSLee Nipper if (dma_len) 12704de9d0b5SLee Nipper edesc->dma_link_tbl = dma_map_single(dev, &edesc->link_tbl[0], 1271497f2e6bSLee Nipper edesc->dma_len, 1272497f2e6bSLee Nipper DMA_BIDIRECTIONAL); 12739c4a7965SKim Phillips 12749c4a7965SKim Phillips return edesc; 12759c4a7965SKim Phillips } 12769c4a7965SKim Phillips 12774de9d0b5SLee Nipper static struct talitos_edesc *aead_edesc_alloc(struct aead_request *areq, 12784de9d0b5SLee Nipper int icv_stashing) 12794de9d0b5SLee Nipper { 12804de9d0b5SLee Nipper struct crypto_aead *authenc = crypto_aead_reqtfm(areq); 12814de9d0b5SLee Nipper struct talitos_ctx *ctx = crypto_aead_ctx(authenc); 12824de9d0b5SLee Nipper 1283497f2e6bSLee Nipper return talitos_edesc_alloc(ctx->dev, areq->src, areq->dst, 0, 12844de9d0b5SLee Nipper areq->cryptlen, ctx->authsize, icv_stashing, 12854de9d0b5SLee Nipper areq->base.flags); 12864de9d0b5SLee Nipper } 12874de9d0b5SLee Nipper 128856af8cd4SLee Nipper static int aead_encrypt(struct aead_request *req) 12899c4a7965SKim Phillips { 12909c4a7965SKim Phillips struct crypto_aead *authenc = crypto_aead_reqtfm(req); 12919c4a7965SKim Phillips struct talitos_ctx *ctx = crypto_aead_ctx(authenc); 129256af8cd4SLee Nipper struct talitos_edesc *edesc; 12939c4a7965SKim Phillips 12949c4a7965SKim Phillips /* allocate extended descriptor */ 12954de9d0b5SLee Nipper edesc = aead_edesc_alloc(req, 0); 12969c4a7965SKim Phillips if (IS_ERR(edesc)) 12979c4a7965SKim Phillips return PTR_ERR(edesc); 12989c4a7965SKim Phillips 12999c4a7965SKim Phillips /* set encrypt */ 130070bcaca7SLee Nipper edesc->desc.hdr = ctx->desc_hdr_template | DESC_HDR_MODE0_ENCRYPT; 13019c4a7965SKim Phillips 13029c4a7965SKim Phillips return ipsec_esp(edesc, req, NULL, 0, ipsec_esp_encrypt_done); 13039c4a7965SKim Phillips } 13049c4a7965SKim Phillips 130556af8cd4SLee Nipper static int aead_decrypt(struct aead_request *req) 13069c4a7965SKim Phillips { 13079c4a7965SKim Phillips struct crypto_aead *authenc = crypto_aead_reqtfm(req); 13089c4a7965SKim Phillips struct talitos_ctx *ctx = crypto_aead_ctx(authenc); 13099c4a7965SKim Phillips unsigned int authsize = ctx->authsize; 1310fe5720e2SKim Phillips struct talitos_private *priv = dev_get_drvdata(ctx->dev); 131156af8cd4SLee Nipper struct talitos_edesc *edesc; 13129c4a7965SKim Phillips struct scatterlist *sg; 13139c4a7965SKim Phillips void *icvdata; 13149c4a7965SKim Phillips 13159c4a7965SKim Phillips req->cryptlen -= authsize; 13169c4a7965SKim Phillips 13179c4a7965SKim Phillips /* allocate extended descriptor */ 13184de9d0b5SLee Nipper edesc = aead_edesc_alloc(req, 1); 13199c4a7965SKim Phillips if (IS_ERR(edesc)) 13209c4a7965SKim Phillips return PTR_ERR(edesc); 13219c4a7965SKim Phillips 1322fe5720e2SKim Phillips if ((priv->features & TALITOS_FTR_HW_AUTH_CHECK) && 1323e938e465SKim Phillips ((!edesc->src_nents && !edesc->dst_nents) || 1324e938e465SKim Phillips priv->features & TALITOS_FTR_SRC_LINK_TBL_LEN_INCLUDES_EXTENT)) { 1325fe5720e2SKim Phillips 1326fe5720e2SKim Phillips /* decrypt and check the ICV */ 1327e938e465SKim Phillips edesc->desc.hdr = ctx->desc_hdr_template | 1328e938e465SKim Phillips DESC_HDR_DIR_INBOUND | 1329fe5720e2SKim Phillips DESC_HDR_MODE1_MDEU_CICV; 1330fe5720e2SKim Phillips 1331fe5720e2SKim Phillips /* reset integrity check result bits */ 1332fe5720e2SKim Phillips edesc->desc.hdr_lo = 0; 1333fe5720e2SKim Phillips 1334e938e465SKim Phillips return ipsec_esp(edesc, req, NULL, 0, 1335e938e465SKim Phillips ipsec_esp_decrypt_hwauth_done); 1336fe5720e2SKim Phillips 1337e938e465SKim Phillips } 1338fe5720e2SKim Phillips 1339fe5720e2SKim Phillips /* Have to check the ICV with software */ 1340fe5720e2SKim Phillips edesc->desc.hdr = ctx->desc_hdr_template | DESC_HDR_DIR_INBOUND; 1341fe5720e2SKim Phillips 13429c4a7965SKim Phillips /* stash incoming ICV for later cmp with ICV generated by the h/w */ 13439c4a7965SKim Phillips if (edesc->dma_len) 13449c4a7965SKim Phillips icvdata = &edesc->link_tbl[edesc->src_nents + 1345f3c85bc1SLee Nipper edesc->dst_nents + 2]; 13469c4a7965SKim Phillips else 13479c4a7965SKim Phillips icvdata = &edesc->link_tbl[0]; 13489c4a7965SKim Phillips 13499c4a7965SKim Phillips sg = sg_last(req->src, edesc->src_nents ? : 1); 13509c4a7965SKim Phillips 13519c4a7965SKim Phillips memcpy(icvdata, (char *)sg_virt(sg) + sg->length - ctx->authsize, 13529c4a7965SKim Phillips ctx->authsize); 13539c4a7965SKim Phillips 1354fe5720e2SKim Phillips return ipsec_esp(edesc, req, NULL, 0, ipsec_esp_decrypt_swauth_done); 1355fe5720e2SKim Phillips } 13569c4a7965SKim Phillips 135756af8cd4SLee Nipper static int aead_givencrypt(struct aead_givcrypt_request *req) 13589c4a7965SKim Phillips { 13599c4a7965SKim Phillips struct aead_request *areq = &req->areq; 13609c4a7965SKim Phillips struct crypto_aead *authenc = crypto_aead_reqtfm(areq); 13619c4a7965SKim Phillips struct talitos_ctx *ctx = crypto_aead_ctx(authenc); 136256af8cd4SLee Nipper struct talitos_edesc *edesc; 13639c4a7965SKim Phillips 13649c4a7965SKim Phillips /* allocate extended descriptor */ 13654de9d0b5SLee Nipper edesc = aead_edesc_alloc(areq, 0); 13669c4a7965SKim Phillips if (IS_ERR(edesc)) 13679c4a7965SKim Phillips return PTR_ERR(edesc); 13689c4a7965SKim Phillips 13699c4a7965SKim Phillips /* set encrypt */ 137070bcaca7SLee Nipper edesc->desc.hdr = ctx->desc_hdr_template | DESC_HDR_MODE0_ENCRYPT; 13719c4a7965SKim Phillips 13729c4a7965SKim Phillips memcpy(req->giv, ctx->iv, crypto_aead_ivsize(authenc)); 1373ba95487dSKim Phillips /* avoid consecutive packets going out with same IV */ 1374ba95487dSKim Phillips *(__be64 *)req->giv ^= cpu_to_be64(req->seq); 13759c4a7965SKim Phillips 13769c4a7965SKim Phillips return ipsec_esp(edesc, areq, req->giv, req->seq, 13779c4a7965SKim Phillips ipsec_esp_encrypt_done); 13789c4a7965SKim Phillips } 13799c4a7965SKim Phillips 13804de9d0b5SLee Nipper static int ablkcipher_setkey(struct crypto_ablkcipher *cipher, 13814de9d0b5SLee Nipper const u8 *key, unsigned int keylen) 13824de9d0b5SLee Nipper { 13834de9d0b5SLee Nipper struct talitos_ctx *ctx = crypto_ablkcipher_ctx(cipher); 13844de9d0b5SLee Nipper 13854de9d0b5SLee Nipper memcpy(&ctx->key, key, keylen); 13864de9d0b5SLee Nipper ctx->keylen = keylen; 13874de9d0b5SLee Nipper 13884de9d0b5SLee Nipper return 0; 13894de9d0b5SLee Nipper } 13904de9d0b5SLee Nipper 13914de9d0b5SLee Nipper static void common_nonsnoop_unmap(struct device *dev, 13924de9d0b5SLee Nipper struct talitos_edesc *edesc, 13934de9d0b5SLee Nipper struct ablkcipher_request *areq) 13944de9d0b5SLee Nipper { 13954de9d0b5SLee Nipper unmap_single_talitos_ptr(dev, &edesc->desc.ptr[5], DMA_FROM_DEVICE); 13964de9d0b5SLee Nipper unmap_single_talitos_ptr(dev, &edesc->desc.ptr[2], DMA_TO_DEVICE); 13974de9d0b5SLee Nipper unmap_single_talitos_ptr(dev, &edesc->desc.ptr[1], DMA_TO_DEVICE); 13984de9d0b5SLee Nipper 13994de9d0b5SLee Nipper talitos_sg_unmap(dev, edesc, areq->src, areq->dst); 14004de9d0b5SLee Nipper 14014de9d0b5SLee Nipper if (edesc->dma_len) 14024de9d0b5SLee Nipper dma_unmap_single(dev, edesc->dma_link_tbl, edesc->dma_len, 14034de9d0b5SLee Nipper DMA_BIDIRECTIONAL); 14044de9d0b5SLee Nipper } 14054de9d0b5SLee Nipper 14064de9d0b5SLee Nipper static void ablkcipher_done(struct device *dev, 14074de9d0b5SLee Nipper struct talitos_desc *desc, void *context, 14084de9d0b5SLee Nipper int err) 14094de9d0b5SLee Nipper { 14104de9d0b5SLee Nipper struct ablkcipher_request *areq = context; 141119bbbc63SKim Phillips struct talitos_edesc *edesc; 141219bbbc63SKim Phillips 141319bbbc63SKim Phillips edesc = container_of(desc, struct talitos_edesc, desc); 14144de9d0b5SLee Nipper 14154de9d0b5SLee Nipper common_nonsnoop_unmap(dev, edesc, areq); 14164de9d0b5SLee Nipper 14174de9d0b5SLee Nipper kfree(edesc); 14184de9d0b5SLee Nipper 14194de9d0b5SLee Nipper areq->base.complete(&areq->base, err); 14204de9d0b5SLee Nipper } 14214de9d0b5SLee Nipper 14224de9d0b5SLee Nipper static int common_nonsnoop(struct talitos_edesc *edesc, 14234de9d0b5SLee Nipper struct ablkcipher_request *areq, 14244de9d0b5SLee Nipper void (*callback) (struct device *dev, 14254de9d0b5SLee Nipper struct talitos_desc *desc, 14264de9d0b5SLee Nipper void *context, int error)) 14274de9d0b5SLee Nipper { 14284de9d0b5SLee Nipper struct crypto_ablkcipher *cipher = crypto_ablkcipher_reqtfm(areq); 14294de9d0b5SLee Nipper struct talitos_ctx *ctx = crypto_ablkcipher_ctx(cipher); 14304de9d0b5SLee Nipper struct device *dev = ctx->dev; 14314de9d0b5SLee Nipper struct talitos_desc *desc = &edesc->desc; 14324de9d0b5SLee Nipper unsigned int cryptlen = areq->nbytes; 14334de9d0b5SLee Nipper unsigned int ivsize; 14344de9d0b5SLee Nipper int sg_count, ret; 14354de9d0b5SLee Nipper 14364de9d0b5SLee Nipper /* first DWORD empty */ 14374de9d0b5SLee Nipper desc->ptr[0].len = 0; 143881eb024cSKim Phillips to_talitos_ptr(&desc->ptr[0], 0); 14394de9d0b5SLee Nipper desc->ptr[0].j_extent = 0; 14404de9d0b5SLee Nipper 14414de9d0b5SLee Nipper /* cipher iv */ 14424de9d0b5SLee Nipper ivsize = crypto_ablkcipher_ivsize(cipher); 1443febec542SKim Phillips map_single_talitos_ptr(dev, &desc->ptr[1], ivsize, areq->info, 0, 14444de9d0b5SLee Nipper DMA_TO_DEVICE); 14454de9d0b5SLee Nipper 14464de9d0b5SLee Nipper /* cipher key */ 14474de9d0b5SLee Nipper map_single_talitos_ptr(dev, &desc->ptr[2], ctx->keylen, 14484de9d0b5SLee Nipper (char *)&ctx->key, 0, DMA_TO_DEVICE); 14494de9d0b5SLee Nipper 14504de9d0b5SLee Nipper /* 14514de9d0b5SLee Nipper * cipher in 14524de9d0b5SLee Nipper */ 14534de9d0b5SLee Nipper desc->ptr[3].len = cpu_to_be16(cryptlen); 14544de9d0b5SLee Nipper desc->ptr[3].j_extent = 0; 14554de9d0b5SLee Nipper 14564de9d0b5SLee Nipper sg_count = talitos_map_sg(dev, areq->src, edesc->src_nents ? : 1, 14574de9d0b5SLee Nipper (areq->src == areq->dst) ? DMA_BIDIRECTIONAL 14584de9d0b5SLee Nipper : DMA_TO_DEVICE, 14594de9d0b5SLee Nipper edesc->src_is_chained); 14604de9d0b5SLee Nipper 14614de9d0b5SLee Nipper if (sg_count == 1) { 146281eb024cSKim Phillips to_talitos_ptr(&desc->ptr[3], sg_dma_address(areq->src)); 14634de9d0b5SLee Nipper } else { 14644de9d0b5SLee Nipper sg_count = sg_to_link_tbl(areq->src, sg_count, cryptlen, 14654de9d0b5SLee Nipper &edesc->link_tbl[0]); 14664de9d0b5SLee Nipper if (sg_count > 1) { 146781eb024cSKim Phillips to_talitos_ptr(&desc->ptr[3], edesc->dma_link_tbl); 14684de9d0b5SLee Nipper desc->ptr[3].j_extent |= DESC_PTR_LNKTBL_JUMP; 1469e938e465SKim Phillips dma_sync_single_for_device(dev, edesc->dma_link_tbl, 1470e938e465SKim Phillips edesc->dma_len, 1471e938e465SKim Phillips DMA_BIDIRECTIONAL); 14724de9d0b5SLee Nipper } else { 14734de9d0b5SLee Nipper /* Only one segment now, so no link tbl needed */ 147481eb024cSKim Phillips to_talitos_ptr(&desc->ptr[3], 147581eb024cSKim Phillips sg_dma_address(areq->src)); 14764de9d0b5SLee Nipper } 14774de9d0b5SLee Nipper } 14784de9d0b5SLee Nipper 14794de9d0b5SLee Nipper /* cipher out */ 14804de9d0b5SLee Nipper desc->ptr[4].len = cpu_to_be16(cryptlen); 14814de9d0b5SLee Nipper desc->ptr[4].j_extent = 0; 14824de9d0b5SLee Nipper 14834de9d0b5SLee Nipper if (areq->src != areq->dst) 14844de9d0b5SLee Nipper sg_count = talitos_map_sg(dev, areq->dst, 14854de9d0b5SLee Nipper edesc->dst_nents ? : 1, 14864de9d0b5SLee Nipper DMA_FROM_DEVICE, 14874de9d0b5SLee Nipper edesc->dst_is_chained); 14884de9d0b5SLee Nipper 14894de9d0b5SLee Nipper if (sg_count == 1) { 149081eb024cSKim Phillips to_talitos_ptr(&desc->ptr[4], sg_dma_address(areq->dst)); 14914de9d0b5SLee Nipper } else { 14924de9d0b5SLee Nipper struct talitos_ptr *link_tbl_ptr = 14934de9d0b5SLee Nipper &edesc->link_tbl[edesc->src_nents + 1]; 14944de9d0b5SLee Nipper 149581eb024cSKim Phillips to_talitos_ptr(&desc->ptr[4], edesc->dma_link_tbl + 149681eb024cSKim Phillips (edesc->src_nents + 1) * 149781eb024cSKim Phillips sizeof(struct talitos_ptr)); 14984de9d0b5SLee Nipper desc->ptr[4].j_extent |= DESC_PTR_LNKTBL_JUMP; 14994de9d0b5SLee Nipper sg_count = sg_to_link_tbl(areq->dst, sg_count, cryptlen, 15004de9d0b5SLee Nipper link_tbl_ptr); 15014de9d0b5SLee Nipper dma_sync_single_for_device(ctx->dev, edesc->dma_link_tbl, 15024de9d0b5SLee Nipper edesc->dma_len, DMA_BIDIRECTIONAL); 15034de9d0b5SLee Nipper } 15044de9d0b5SLee Nipper 15054de9d0b5SLee Nipper /* iv out */ 15064de9d0b5SLee Nipper map_single_talitos_ptr(dev, &desc->ptr[5], ivsize, ctx->iv, 0, 15074de9d0b5SLee Nipper DMA_FROM_DEVICE); 15084de9d0b5SLee Nipper 15094de9d0b5SLee Nipper /* last DWORD empty */ 15104de9d0b5SLee Nipper desc->ptr[6].len = 0; 151181eb024cSKim Phillips to_talitos_ptr(&desc->ptr[6], 0); 15124de9d0b5SLee Nipper desc->ptr[6].j_extent = 0; 15134de9d0b5SLee Nipper 15145228f0f7SKim Phillips ret = talitos_submit(dev, ctx->ch, desc, callback, areq); 15154de9d0b5SLee Nipper if (ret != -EINPROGRESS) { 15164de9d0b5SLee Nipper common_nonsnoop_unmap(dev, edesc, areq); 15174de9d0b5SLee Nipper kfree(edesc); 15184de9d0b5SLee Nipper } 15194de9d0b5SLee Nipper return ret; 15204de9d0b5SLee Nipper } 15214de9d0b5SLee Nipper 1522e938e465SKim Phillips static struct talitos_edesc *ablkcipher_edesc_alloc(struct ablkcipher_request * 1523e938e465SKim Phillips areq) 15244de9d0b5SLee Nipper { 15254de9d0b5SLee Nipper struct crypto_ablkcipher *cipher = crypto_ablkcipher_reqtfm(areq); 15264de9d0b5SLee Nipper struct talitos_ctx *ctx = crypto_ablkcipher_ctx(cipher); 15274de9d0b5SLee Nipper 1528497f2e6bSLee Nipper return talitos_edesc_alloc(ctx->dev, areq->src, areq->dst, 0, 1529497f2e6bSLee Nipper areq->nbytes, 0, 0, areq->base.flags); 15304de9d0b5SLee Nipper } 15314de9d0b5SLee Nipper 15324de9d0b5SLee Nipper static int ablkcipher_encrypt(struct ablkcipher_request *areq) 15334de9d0b5SLee Nipper { 15344de9d0b5SLee Nipper struct crypto_ablkcipher *cipher = crypto_ablkcipher_reqtfm(areq); 15354de9d0b5SLee Nipper struct talitos_ctx *ctx = crypto_ablkcipher_ctx(cipher); 15364de9d0b5SLee Nipper struct talitos_edesc *edesc; 15374de9d0b5SLee Nipper 15384de9d0b5SLee Nipper /* allocate extended descriptor */ 15394de9d0b5SLee Nipper edesc = ablkcipher_edesc_alloc(areq); 15404de9d0b5SLee Nipper if (IS_ERR(edesc)) 15414de9d0b5SLee Nipper return PTR_ERR(edesc); 15424de9d0b5SLee Nipper 15434de9d0b5SLee Nipper /* set encrypt */ 15444de9d0b5SLee Nipper edesc->desc.hdr = ctx->desc_hdr_template | DESC_HDR_MODE0_ENCRYPT; 15454de9d0b5SLee Nipper 1546febec542SKim Phillips return common_nonsnoop(edesc, areq, ablkcipher_done); 15474de9d0b5SLee Nipper } 15484de9d0b5SLee Nipper 15494de9d0b5SLee Nipper static int ablkcipher_decrypt(struct ablkcipher_request *areq) 15504de9d0b5SLee Nipper { 15514de9d0b5SLee Nipper struct crypto_ablkcipher *cipher = crypto_ablkcipher_reqtfm(areq); 15524de9d0b5SLee Nipper struct talitos_ctx *ctx = crypto_ablkcipher_ctx(cipher); 15534de9d0b5SLee Nipper struct talitos_edesc *edesc; 15544de9d0b5SLee Nipper 15554de9d0b5SLee Nipper /* allocate extended descriptor */ 15564de9d0b5SLee Nipper edesc = ablkcipher_edesc_alloc(areq); 15574de9d0b5SLee Nipper if (IS_ERR(edesc)) 15584de9d0b5SLee Nipper return PTR_ERR(edesc); 15594de9d0b5SLee Nipper 15604de9d0b5SLee Nipper edesc->desc.hdr = ctx->desc_hdr_template | DESC_HDR_DIR_INBOUND; 15614de9d0b5SLee Nipper 1562febec542SKim Phillips return common_nonsnoop(edesc, areq, ablkcipher_done); 15634de9d0b5SLee Nipper } 15644de9d0b5SLee Nipper 1565497f2e6bSLee Nipper static void common_nonsnoop_hash_unmap(struct device *dev, 1566497f2e6bSLee Nipper struct talitos_edesc *edesc, 1567497f2e6bSLee Nipper struct ahash_request *areq) 1568497f2e6bSLee Nipper { 1569497f2e6bSLee Nipper struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq); 1570497f2e6bSLee Nipper 1571497f2e6bSLee Nipper unmap_single_talitos_ptr(dev, &edesc->desc.ptr[5], DMA_FROM_DEVICE); 1572497f2e6bSLee Nipper 1573497f2e6bSLee Nipper /* When using hashctx-in, must unmap it. */ 1574497f2e6bSLee Nipper if (edesc->desc.ptr[1].len) 1575497f2e6bSLee Nipper unmap_single_talitos_ptr(dev, &edesc->desc.ptr[1], 1576497f2e6bSLee Nipper DMA_TO_DEVICE); 1577497f2e6bSLee Nipper 1578497f2e6bSLee Nipper if (edesc->desc.ptr[2].len) 1579497f2e6bSLee Nipper unmap_single_talitos_ptr(dev, &edesc->desc.ptr[2], 1580497f2e6bSLee Nipper DMA_TO_DEVICE); 1581497f2e6bSLee Nipper 1582497f2e6bSLee Nipper talitos_sg_unmap(dev, edesc, req_ctx->psrc, NULL); 1583497f2e6bSLee Nipper 1584497f2e6bSLee Nipper if (edesc->dma_len) 1585497f2e6bSLee Nipper dma_unmap_single(dev, edesc->dma_link_tbl, edesc->dma_len, 1586497f2e6bSLee Nipper DMA_BIDIRECTIONAL); 1587497f2e6bSLee Nipper 1588497f2e6bSLee Nipper } 1589497f2e6bSLee Nipper 1590497f2e6bSLee Nipper static void ahash_done(struct device *dev, 1591497f2e6bSLee Nipper struct talitos_desc *desc, void *context, 1592497f2e6bSLee Nipper int err) 1593497f2e6bSLee Nipper { 1594497f2e6bSLee Nipper struct ahash_request *areq = context; 1595497f2e6bSLee Nipper struct talitos_edesc *edesc = 1596497f2e6bSLee Nipper container_of(desc, struct talitos_edesc, desc); 1597497f2e6bSLee Nipper struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq); 1598497f2e6bSLee Nipper 1599497f2e6bSLee Nipper if (!req_ctx->last && req_ctx->to_hash_later) { 1600497f2e6bSLee Nipper /* Position any partial block for next update/final/finup */ 1601497f2e6bSLee Nipper memcpy(req_ctx->buf, req_ctx->bufnext, req_ctx->to_hash_later); 16025e833bc4SLee Nipper req_ctx->nbuf = req_ctx->to_hash_later; 1603497f2e6bSLee Nipper } 1604497f2e6bSLee Nipper common_nonsnoop_hash_unmap(dev, edesc, areq); 1605497f2e6bSLee Nipper 1606497f2e6bSLee Nipper kfree(edesc); 1607497f2e6bSLee Nipper 1608497f2e6bSLee Nipper areq->base.complete(&areq->base, err); 1609497f2e6bSLee Nipper } 1610497f2e6bSLee Nipper 1611497f2e6bSLee Nipper static int common_nonsnoop_hash(struct talitos_edesc *edesc, 1612497f2e6bSLee Nipper struct ahash_request *areq, unsigned int length, 1613497f2e6bSLee Nipper void (*callback) (struct device *dev, 1614497f2e6bSLee Nipper struct talitos_desc *desc, 1615497f2e6bSLee Nipper void *context, int error)) 1616497f2e6bSLee Nipper { 1617497f2e6bSLee Nipper struct crypto_ahash *tfm = crypto_ahash_reqtfm(areq); 1618497f2e6bSLee Nipper struct talitos_ctx *ctx = crypto_ahash_ctx(tfm); 1619497f2e6bSLee Nipper struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq); 1620497f2e6bSLee Nipper struct device *dev = ctx->dev; 1621497f2e6bSLee Nipper struct talitos_desc *desc = &edesc->desc; 1622497f2e6bSLee Nipper int sg_count, ret; 1623497f2e6bSLee Nipper 1624497f2e6bSLee Nipper /* first DWORD empty */ 1625497f2e6bSLee Nipper desc->ptr[0] = zero_entry; 1626497f2e6bSLee Nipper 162760f208d7SKim Phillips /* hash context in */ 162860f208d7SKim Phillips if (!req_ctx->first || req_ctx->swinit) { 1629497f2e6bSLee Nipper map_single_talitos_ptr(dev, &desc->ptr[1], 1630497f2e6bSLee Nipper req_ctx->hw_context_size, 1631497f2e6bSLee Nipper (char *)req_ctx->hw_context, 0, 1632497f2e6bSLee Nipper DMA_TO_DEVICE); 163360f208d7SKim Phillips req_ctx->swinit = 0; 1634497f2e6bSLee Nipper } else { 1635497f2e6bSLee Nipper desc->ptr[1] = zero_entry; 1636497f2e6bSLee Nipper /* Indicate next op is not the first. */ 1637497f2e6bSLee Nipper req_ctx->first = 0; 1638497f2e6bSLee Nipper } 1639497f2e6bSLee Nipper 1640497f2e6bSLee Nipper /* HMAC key */ 1641497f2e6bSLee Nipper if (ctx->keylen) 1642497f2e6bSLee Nipper map_single_talitos_ptr(dev, &desc->ptr[2], ctx->keylen, 1643497f2e6bSLee Nipper (char *)&ctx->key, 0, DMA_TO_DEVICE); 1644497f2e6bSLee Nipper else 1645497f2e6bSLee Nipper desc->ptr[2] = zero_entry; 1646497f2e6bSLee Nipper 1647497f2e6bSLee Nipper /* 1648497f2e6bSLee Nipper * data in 1649497f2e6bSLee Nipper */ 1650497f2e6bSLee Nipper desc->ptr[3].len = cpu_to_be16(length); 1651497f2e6bSLee Nipper desc->ptr[3].j_extent = 0; 1652497f2e6bSLee Nipper 1653497f2e6bSLee Nipper sg_count = talitos_map_sg(dev, req_ctx->psrc, 1654497f2e6bSLee Nipper edesc->src_nents ? : 1, 1655497f2e6bSLee Nipper DMA_TO_DEVICE, 1656497f2e6bSLee Nipper edesc->src_is_chained); 1657497f2e6bSLee Nipper 1658497f2e6bSLee Nipper if (sg_count == 1) { 1659497f2e6bSLee Nipper to_talitos_ptr(&desc->ptr[3], sg_dma_address(req_ctx->psrc)); 1660497f2e6bSLee Nipper } else { 1661497f2e6bSLee Nipper sg_count = sg_to_link_tbl(req_ctx->psrc, sg_count, length, 1662497f2e6bSLee Nipper &edesc->link_tbl[0]); 1663497f2e6bSLee Nipper if (sg_count > 1) { 1664497f2e6bSLee Nipper desc->ptr[3].j_extent |= DESC_PTR_LNKTBL_JUMP; 1665497f2e6bSLee Nipper to_talitos_ptr(&desc->ptr[3], edesc->dma_link_tbl); 1666497f2e6bSLee Nipper dma_sync_single_for_device(ctx->dev, 1667497f2e6bSLee Nipper edesc->dma_link_tbl, 1668497f2e6bSLee Nipper edesc->dma_len, 1669497f2e6bSLee Nipper DMA_BIDIRECTIONAL); 1670497f2e6bSLee Nipper } else { 1671497f2e6bSLee Nipper /* Only one segment now, so no link tbl needed */ 1672497f2e6bSLee Nipper to_talitos_ptr(&desc->ptr[3], 1673497f2e6bSLee Nipper sg_dma_address(req_ctx->psrc)); 1674497f2e6bSLee Nipper } 1675497f2e6bSLee Nipper } 1676497f2e6bSLee Nipper 1677497f2e6bSLee Nipper /* fifth DWORD empty */ 1678497f2e6bSLee Nipper desc->ptr[4] = zero_entry; 1679497f2e6bSLee Nipper 1680497f2e6bSLee Nipper /* hash/HMAC out -or- hash context out */ 1681497f2e6bSLee Nipper if (req_ctx->last) 1682497f2e6bSLee Nipper map_single_talitos_ptr(dev, &desc->ptr[5], 1683497f2e6bSLee Nipper crypto_ahash_digestsize(tfm), 1684497f2e6bSLee Nipper areq->result, 0, DMA_FROM_DEVICE); 1685497f2e6bSLee Nipper else 1686497f2e6bSLee Nipper map_single_talitos_ptr(dev, &desc->ptr[5], 1687497f2e6bSLee Nipper req_ctx->hw_context_size, 1688497f2e6bSLee Nipper req_ctx->hw_context, 0, DMA_FROM_DEVICE); 1689497f2e6bSLee Nipper 1690497f2e6bSLee Nipper /* last DWORD empty */ 1691497f2e6bSLee Nipper desc->ptr[6] = zero_entry; 1692497f2e6bSLee Nipper 16935228f0f7SKim Phillips ret = talitos_submit(dev, ctx->ch, desc, callback, areq); 1694497f2e6bSLee Nipper if (ret != -EINPROGRESS) { 1695497f2e6bSLee Nipper common_nonsnoop_hash_unmap(dev, edesc, areq); 1696497f2e6bSLee Nipper kfree(edesc); 1697497f2e6bSLee Nipper } 1698497f2e6bSLee Nipper return ret; 1699497f2e6bSLee Nipper } 1700497f2e6bSLee Nipper 1701497f2e6bSLee Nipper static struct talitos_edesc *ahash_edesc_alloc(struct ahash_request *areq, 1702497f2e6bSLee Nipper unsigned int nbytes) 1703497f2e6bSLee Nipper { 1704497f2e6bSLee Nipper struct crypto_ahash *tfm = crypto_ahash_reqtfm(areq); 1705497f2e6bSLee Nipper struct talitos_ctx *ctx = crypto_ahash_ctx(tfm); 1706497f2e6bSLee Nipper struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq); 1707497f2e6bSLee Nipper 1708497f2e6bSLee Nipper return talitos_edesc_alloc(ctx->dev, req_ctx->psrc, NULL, 1, 1709497f2e6bSLee Nipper nbytes, 0, 0, areq->base.flags); 1710497f2e6bSLee Nipper } 1711497f2e6bSLee Nipper 1712497f2e6bSLee Nipper static int ahash_init(struct ahash_request *areq) 1713497f2e6bSLee Nipper { 1714497f2e6bSLee Nipper struct crypto_ahash *tfm = crypto_ahash_reqtfm(areq); 1715497f2e6bSLee Nipper struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq); 1716497f2e6bSLee Nipper 1717497f2e6bSLee Nipper /* Initialize the context */ 17185e833bc4SLee Nipper req_ctx->nbuf = 0; 171960f208d7SKim Phillips req_ctx->first = 1; /* first indicates h/w must init its context */ 172060f208d7SKim Phillips req_ctx->swinit = 0; /* assume h/w init of context */ 1721497f2e6bSLee Nipper req_ctx->hw_context_size = 1722497f2e6bSLee Nipper (crypto_ahash_digestsize(tfm) <= SHA256_DIGEST_SIZE) 1723497f2e6bSLee Nipper ? TALITOS_MDEU_CONTEXT_SIZE_MD5_SHA1_SHA256 1724497f2e6bSLee Nipper : TALITOS_MDEU_CONTEXT_SIZE_SHA384_SHA512; 1725497f2e6bSLee Nipper 1726497f2e6bSLee Nipper return 0; 1727497f2e6bSLee Nipper } 1728497f2e6bSLee Nipper 172960f208d7SKim Phillips /* 173060f208d7SKim Phillips * on h/w without explicit sha224 support, we initialize h/w context 173160f208d7SKim Phillips * manually with sha224 constants, and tell it to run sha256. 173260f208d7SKim Phillips */ 173360f208d7SKim Phillips static int ahash_init_sha224_swinit(struct ahash_request *areq) 173460f208d7SKim Phillips { 173560f208d7SKim Phillips struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq); 173660f208d7SKim Phillips 173760f208d7SKim Phillips ahash_init(areq); 173860f208d7SKim Phillips req_ctx->swinit = 1;/* prevent h/w initting context with sha256 values*/ 173960f208d7SKim Phillips 1740a752447aSKim Phillips req_ctx->hw_context[0] = SHA224_H0; 1741a752447aSKim Phillips req_ctx->hw_context[1] = SHA224_H1; 1742a752447aSKim Phillips req_ctx->hw_context[2] = SHA224_H2; 1743a752447aSKim Phillips req_ctx->hw_context[3] = SHA224_H3; 1744a752447aSKim Phillips req_ctx->hw_context[4] = SHA224_H4; 1745a752447aSKim Phillips req_ctx->hw_context[5] = SHA224_H5; 1746a752447aSKim Phillips req_ctx->hw_context[6] = SHA224_H6; 1747a752447aSKim Phillips req_ctx->hw_context[7] = SHA224_H7; 174860f208d7SKim Phillips 174960f208d7SKim Phillips /* init 64-bit count */ 175060f208d7SKim Phillips req_ctx->hw_context[8] = 0; 175160f208d7SKim Phillips req_ctx->hw_context[9] = 0; 175260f208d7SKim Phillips 175360f208d7SKim Phillips return 0; 175460f208d7SKim Phillips } 175560f208d7SKim Phillips 1756497f2e6bSLee Nipper static int ahash_process_req(struct ahash_request *areq, unsigned int nbytes) 1757497f2e6bSLee Nipper { 1758497f2e6bSLee Nipper struct crypto_ahash *tfm = crypto_ahash_reqtfm(areq); 1759497f2e6bSLee Nipper struct talitos_ctx *ctx = crypto_ahash_ctx(tfm); 1760497f2e6bSLee Nipper struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq); 1761497f2e6bSLee Nipper struct talitos_edesc *edesc; 1762497f2e6bSLee Nipper unsigned int blocksize = 1763497f2e6bSLee Nipper crypto_tfm_alg_blocksize(crypto_ahash_tfm(tfm)); 1764497f2e6bSLee Nipper unsigned int nbytes_to_hash; 1765497f2e6bSLee Nipper unsigned int to_hash_later; 17665e833bc4SLee Nipper unsigned int nsg; 1767497f2e6bSLee Nipper int chained; 1768497f2e6bSLee Nipper 17695e833bc4SLee Nipper if (!req_ctx->last && (nbytes + req_ctx->nbuf <= blocksize)) { 17705e833bc4SLee Nipper /* Buffer up to one whole block */ 1771497f2e6bSLee Nipper sg_copy_to_buffer(areq->src, 1772497f2e6bSLee Nipper sg_count(areq->src, nbytes, &chained), 17735e833bc4SLee Nipper req_ctx->buf + req_ctx->nbuf, nbytes); 17745e833bc4SLee Nipper req_ctx->nbuf += nbytes; 1775497f2e6bSLee Nipper return 0; 1776497f2e6bSLee Nipper } 1777497f2e6bSLee Nipper 17785e833bc4SLee Nipper /* At least (blocksize + 1) bytes are available to hash */ 17795e833bc4SLee Nipper nbytes_to_hash = nbytes + req_ctx->nbuf; 17805e833bc4SLee Nipper to_hash_later = nbytes_to_hash & (blocksize - 1); 17815e833bc4SLee Nipper 17825e833bc4SLee Nipper if (req_ctx->last) 17835e833bc4SLee Nipper to_hash_later = 0; 17845e833bc4SLee Nipper else if (to_hash_later) 17855e833bc4SLee Nipper /* There is a partial block. Hash the full block(s) now */ 17865e833bc4SLee Nipper nbytes_to_hash -= to_hash_later; 17875e833bc4SLee Nipper else { 17885e833bc4SLee Nipper /* Keep one block buffered */ 17895e833bc4SLee Nipper nbytes_to_hash -= blocksize; 17905e833bc4SLee Nipper to_hash_later = blocksize; 1791497f2e6bSLee Nipper } 17925e833bc4SLee Nipper 17935e833bc4SLee Nipper /* Chain in any previously buffered data */ 17945e833bc4SLee Nipper if (req_ctx->nbuf) { 17955e833bc4SLee Nipper nsg = (req_ctx->nbuf < nbytes_to_hash) ? 2 : 1; 17965e833bc4SLee Nipper sg_init_table(req_ctx->bufsl, nsg); 17975e833bc4SLee Nipper sg_set_buf(req_ctx->bufsl, req_ctx->buf, req_ctx->nbuf); 17985e833bc4SLee Nipper if (nsg > 1) 17995e833bc4SLee Nipper scatterwalk_sg_chain(req_ctx->bufsl, 2, areq->src); 18005e833bc4SLee Nipper req_ctx->psrc = req_ctx->bufsl; 18015e833bc4SLee Nipper } else 18025e833bc4SLee Nipper req_ctx->psrc = areq->src; 18035e833bc4SLee Nipper 1804497f2e6bSLee Nipper if (to_hash_later) { 18055e833bc4SLee Nipper int nents = sg_count(areq->src, nbytes, &chained); 1806497f2e6bSLee Nipper sg_copy_end_to_buffer(areq->src, nents, 1807497f2e6bSLee Nipper req_ctx->bufnext, 1808497f2e6bSLee Nipper to_hash_later, 1809497f2e6bSLee Nipper nbytes - to_hash_later); 1810497f2e6bSLee Nipper } 1811497f2e6bSLee Nipper req_ctx->to_hash_later = to_hash_later; 1812497f2e6bSLee Nipper 18135e833bc4SLee Nipper /* Allocate extended descriptor */ 1814497f2e6bSLee Nipper edesc = ahash_edesc_alloc(areq, nbytes_to_hash); 1815497f2e6bSLee Nipper if (IS_ERR(edesc)) 1816497f2e6bSLee Nipper return PTR_ERR(edesc); 1817497f2e6bSLee Nipper 1818497f2e6bSLee Nipper edesc->desc.hdr = ctx->desc_hdr_template; 1819497f2e6bSLee Nipper 1820497f2e6bSLee Nipper /* On last one, request SEC to pad; otherwise continue */ 1821497f2e6bSLee Nipper if (req_ctx->last) 1822497f2e6bSLee Nipper edesc->desc.hdr |= DESC_HDR_MODE0_MDEU_PAD; 1823497f2e6bSLee Nipper else 1824497f2e6bSLee Nipper edesc->desc.hdr |= DESC_HDR_MODE0_MDEU_CONT; 1825497f2e6bSLee Nipper 182660f208d7SKim Phillips /* request SEC to INIT hash. */ 182760f208d7SKim Phillips if (req_ctx->first && !req_ctx->swinit) 1828497f2e6bSLee Nipper edesc->desc.hdr |= DESC_HDR_MODE0_MDEU_INIT; 1829497f2e6bSLee Nipper 1830497f2e6bSLee Nipper /* When the tfm context has a keylen, it's an HMAC. 1831497f2e6bSLee Nipper * A first or last (ie. not middle) descriptor must request HMAC. 1832497f2e6bSLee Nipper */ 1833497f2e6bSLee Nipper if (ctx->keylen && (req_ctx->first || req_ctx->last)) 1834497f2e6bSLee Nipper edesc->desc.hdr |= DESC_HDR_MODE0_MDEU_HMAC; 1835497f2e6bSLee Nipper 1836497f2e6bSLee Nipper return common_nonsnoop_hash(edesc, areq, nbytes_to_hash, 1837497f2e6bSLee Nipper ahash_done); 1838497f2e6bSLee Nipper } 1839497f2e6bSLee Nipper 1840497f2e6bSLee Nipper static int ahash_update(struct ahash_request *areq) 1841497f2e6bSLee Nipper { 1842497f2e6bSLee Nipper struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq); 1843497f2e6bSLee Nipper 1844497f2e6bSLee Nipper req_ctx->last = 0; 1845497f2e6bSLee Nipper 1846497f2e6bSLee Nipper return ahash_process_req(areq, areq->nbytes); 1847497f2e6bSLee Nipper } 1848497f2e6bSLee Nipper 1849497f2e6bSLee Nipper static int ahash_final(struct ahash_request *areq) 1850497f2e6bSLee Nipper { 1851497f2e6bSLee Nipper struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq); 1852497f2e6bSLee Nipper 1853497f2e6bSLee Nipper req_ctx->last = 1; 1854497f2e6bSLee Nipper 1855497f2e6bSLee Nipper return ahash_process_req(areq, 0); 1856497f2e6bSLee Nipper } 1857497f2e6bSLee Nipper 1858497f2e6bSLee Nipper static int ahash_finup(struct ahash_request *areq) 1859497f2e6bSLee Nipper { 1860497f2e6bSLee Nipper struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq); 1861497f2e6bSLee Nipper 1862497f2e6bSLee Nipper req_ctx->last = 1; 1863497f2e6bSLee Nipper 1864497f2e6bSLee Nipper return ahash_process_req(areq, areq->nbytes); 1865497f2e6bSLee Nipper } 1866497f2e6bSLee Nipper 1867497f2e6bSLee Nipper static int ahash_digest(struct ahash_request *areq) 1868497f2e6bSLee Nipper { 1869497f2e6bSLee Nipper struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq); 187060f208d7SKim Phillips struct crypto_ahash *ahash = crypto_ahash_reqtfm(areq); 1871497f2e6bSLee Nipper 187260f208d7SKim Phillips ahash->init(areq); 1873497f2e6bSLee Nipper req_ctx->last = 1; 1874497f2e6bSLee Nipper 1875497f2e6bSLee Nipper return ahash_process_req(areq, areq->nbytes); 1876497f2e6bSLee Nipper } 1877497f2e6bSLee Nipper 187879b3a418SLee Nipper struct keyhash_result { 187979b3a418SLee Nipper struct completion completion; 188079b3a418SLee Nipper int err; 188179b3a418SLee Nipper }; 188279b3a418SLee Nipper 188379b3a418SLee Nipper static void keyhash_complete(struct crypto_async_request *req, int err) 188479b3a418SLee Nipper { 188579b3a418SLee Nipper struct keyhash_result *res = req->data; 188679b3a418SLee Nipper 188779b3a418SLee Nipper if (err == -EINPROGRESS) 188879b3a418SLee Nipper return; 188979b3a418SLee Nipper 189079b3a418SLee Nipper res->err = err; 189179b3a418SLee Nipper complete(&res->completion); 189279b3a418SLee Nipper } 189379b3a418SLee Nipper 189479b3a418SLee Nipper static int keyhash(struct crypto_ahash *tfm, const u8 *key, unsigned int keylen, 189579b3a418SLee Nipper u8 *hash) 189679b3a418SLee Nipper { 189779b3a418SLee Nipper struct talitos_ctx *ctx = crypto_tfm_ctx(crypto_ahash_tfm(tfm)); 189879b3a418SLee Nipper 189979b3a418SLee Nipper struct scatterlist sg[1]; 190079b3a418SLee Nipper struct ahash_request *req; 190179b3a418SLee Nipper struct keyhash_result hresult; 190279b3a418SLee Nipper int ret; 190379b3a418SLee Nipper 190479b3a418SLee Nipper init_completion(&hresult.completion); 190579b3a418SLee Nipper 190679b3a418SLee Nipper req = ahash_request_alloc(tfm, GFP_KERNEL); 190779b3a418SLee Nipper if (!req) 190879b3a418SLee Nipper return -ENOMEM; 190979b3a418SLee Nipper 191079b3a418SLee Nipper /* Keep tfm keylen == 0 during hash of the long key */ 191179b3a418SLee Nipper ctx->keylen = 0; 191279b3a418SLee Nipper ahash_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG, 191379b3a418SLee Nipper keyhash_complete, &hresult); 191479b3a418SLee Nipper 191579b3a418SLee Nipper sg_init_one(&sg[0], key, keylen); 191679b3a418SLee Nipper 191779b3a418SLee Nipper ahash_request_set_crypt(req, sg, hash, keylen); 191879b3a418SLee Nipper ret = crypto_ahash_digest(req); 191979b3a418SLee Nipper switch (ret) { 192079b3a418SLee Nipper case 0: 192179b3a418SLee Nipper break; 192279b3a418SLee Nipper case -EINPROGRESS: 192379b3a418SLee Nipper case -EBUSY: 192479b3a418SLee Nipper ret = wait_for_completion_interruptible( 192579b3a418SLee Nipper &hresult.completion); 192679b3a418SLee Nipper if (!ret) 192779b3a418SLee Nipper ret = hresult.err; 192879b3a418SLee Nipper break; 192979b3a418SLee Nipper default: 193079b3a418SLee Nipper break; 193179b3a418SLee Nipper } 193279b3a418SLee Nipper ahash_request_free(req); 193379b3a418SLee Nipper 193479b3a418SLee Nipper return ret; 193579b3a418SLee Nipper } 193679b3a418SLee Nipper 193779b3a418SLee Nipper static int ahash_setkey(struct crypto_ahash *tfm, const u8 *key, 193879b3a418SLee Nipper unsigned int keylen) 193979b3a418SLee Nipper { 194079b3a418SLee Nipper struct talitos_ctx *ctx = crypto_tfm_ctx(crypto_ahash_tfm(tfm)); 194179b3a418SLee Nipper unsigned int blocksize = 194279b3a418SLee Nipper crypto_tfm_alg_blocksize(crypto_ahash_tfm(tfm)); 194379b3a418SLee Nipper unsigned int digestsize = crypto_ahash_digestsize(tfm); 194479b3a418SLee Nipper unsigned int keysize = keylen; 194579b3a418SLee Nipper u8 hash[SHA512_DIGEST_SIZE]; 194679b3a418SLee Nipper int ret; 194779b3a418SLee Nipper 194879b3a418SLee Nipper if (keylen <= blocksize) 194979b3a418SLee Nipper memcpy(ctx->key, key, keysize); 195079b3a418SLee Nipper else { 195179b3a418SLee Nipper /* Must get the hash of the long key */ 195279b3a418SLee Nipper ret = keyhash(tfm, key, keylen, hash); 195379b3a418SLee Nipper 195479b3a418SLee Nipper if (ret) { 195579b3a418SLee Nipper crypto_ahash_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN); 195679b3a418SLee Nipper return -EINVAL; 195779b3a418SLee Nipper } 195879b3a418SLee Nipper 195979b3a418SLee Nipper keysize = digestsize; 196079b3a418SLee Nipper memcpy(ctx->key, hash, digestsize); 196179b3a418SLee Nipper } 196279b3a418SLee Nipper 196379b3a418SLee Nipper ctx->keylen = keysize; 196479b3a418SLee Nipper 196579b3a418SLee Nipper return 0; 196679b3a418SLee Nipper } 196779b3a418SLee Nipper 196879b3a418SLee Nipper 19699c4a7965SKim Phillips struct talitos_alg_template { 1970d5e4aaefSLee Nipper u32 type; 1971d5e4aaefSLee Nipper union { 1972d5e4aaefSLee Nipper struct crypto_alg crypto; 1973acbf7c62SLee Nipper struct ahash_alg hash; 1974d5e4aaefSLee Nipper } alg; 19759c4a7965SKim Phillips __be32 desc_hdr_template; 19769c4a7965SKim Phillips }; 19779c4a7965SKim Phillips 19789c4a7965SKim Phillips static struct talitos_alg_template driver_algs[] = { 197956af8cd4SLee Nipper /* AEAD algorithms. These use a single-pass ipsec_esp descriptor */ 1980d5e4aaefSLee Nipper { .type = CRYPTO_ALG_TYPE_AEAD, 1981d5e4aaefSLee Nipper .alg.crypto = { 198256af8cd4SLee Nipper .cra_name = "authenc(hmac(sha1),cbc(aes))", 198356af8cd4SLee Nipper .cra_driver_name = "authenc-hmac-sha1-cbc-aes-talitos", 198456af8cd4SLee Nipper .cra_blocksize = AES_BLOCK_SIZE, 198556af8cd4SLee Nipper .cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC, 198656af8cd4SLee Nipper .cra_type = &crypto_aead_type, 198756af8cd4SLee Nipper .cra_aead = { 198856af8cd4SLee Nipper .setkey = aead_setkey, 198956af8cd4SLee Nipper .setauthsize = aead_setauthsize, 199056af8cd4SLee Nipper .encrypt = aead_encrypt, 199156af8cd4SLee Nipper .decrypt = aead_decrypt, 199256af8cd4SLee Nipper .givencrypt = aead_givencrypt, 19939c4a7965SKim Phillips .geniv = "<built-in>", 19943952f17eSLee Nipper .ivsize = AES_BLOCK_SIZE, 19953952f17eSLee Nipper .maxauthsize = SHA1_DIGEST_SIZE, 199656af8cd4SLee Nipper } 19979c4a7965SKim Phillips }, 19989c4a7965SKim Phillips .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP | 19999c4a7965SKim Phillips DESC_HDR_SEL0_AESU | 20009c4a7965SKim Phillips DESC_HDR_MODE0_AESU_CBC | 20019c4a7965SKim Phillips DESC_HDR_SEL1_MDEUA | 20029c4a7965SKim Phillips DESC_HDR_MODE1_MDEU_INIT | 20039c4a7965SKim Phillips DESC_HDR_MODE1_MDEU_PAD | 20049c4a7965SKim Phillips DESC_HDR_MODE1_MDEU_SHA1_HMAC, 200570bcaca7SLee Nipper }, 2006d5e4aaefSLee Nipper { .type = CRYPTO_ALG_TYPE_AEAD, 2007d5e4aaefSLee Nipper .alg.crypto = { 200856af8cd4SLee Nipper .cra_name = "authenc(hmac(sha1),cbc(des3_ede))", 200956af8cd4SLee Nipper .cra_driver_name = "authenc-hmac-sha1-cbc-3des-talitos", 201056af8cd4SLee Nipper .cra_blocksize = DES3_EDE_BLOCK_SIZE, 201156af8cd4SLee Nipper .cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC, 201256af8cd4SLee Nipper .cra_type = &crypto_aead_type, 201356af8cd4SLee Nipper .cra_aead = { 201456af8cd4SLee Nipper .setkey = aead_setkey, 201556af8cd4SLee Nipper .setauthsize = aead_setauthsize, 201656af8cd4SLee Nipper .encrypt = aead_encrypt, 201756af8cd4SLee Nipper .decrypt = aead_decrypt, 201856af8cd4SLee Nipper .givencrypt = aead_givencrypt, 201970bcaca7SLee Nipper .geniv = "<built-in>", 20203952f17eSLee Nipper .ivsize = DES3_EDE_BLOCK_SIZE, 20213952f17eSLee Nipper .maxauthsize = SHA1_DIGEST_SIZE, 202256af8cd4SLee Nipper } 202370bcaca7SLee Nipper }, 202470bcaca7SLee Nipper .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP | 202570bcaca7SLee Nipper DESC_HDR_SEL0_DEU | 202670bcaca7SLee Nipper DESC_HDR_MODE0_DEU_CBC | 202770bcaca7SLee Nipper DESC_HDR_MODE0_DEU_3DES | 202870bcaca7SLee Nipper DESC_HDR_SEL1_MDEUA | 202970bcaca7SLee Nipper DESC_HDR_MODE1_MDEU_INIT | 203070bcaca7SLee Nipper DESC_HDR_MODE1_MDEU_PAD | 203170bcaca7SLee Nipper DESC_HDR_MODE1_MDEU_SHA1_HMAC, 20323952f17eSLee Nipper }, 2033d5e4aaefSLee Nipper { .type = CRYPTO_ALG_TYPE_AEAD, 2034d5e4aaefSLee Nipper .alg.crypto = { 203556af8cd4SLee Nipper .cra_name = "authenc(hmac(sha256),cbc(aes))", 203656af8cd4SLee Nipper .cra_driver_name = "authenc-hmac-sha256-cbc-aes-talitos", 203756af8cd4SLee Nipper .cra_blocksize = AES_BLOCK_SIZE, 203856af8cd4SLee Nipper .cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC, 203956af8cd4SLee Nipper .cra_type = &crypto_aead_type, 204056af8cd4SLee Nipper .cra_aead = { 204156af8cd4SLee Nipper .setkey = aead_setkey, 204256af8cd4SLee Nipper .setauthsize = aead_setauthsize, 204356af8cd4SLee Nipper .encrypt = aead_encrypt, 204456af8cd4SLee Nipper .decrypt = aead_decrypt, 204556af8cd4SLee Nipper .givencrypt = aead_givencrypt, 20463952f17eSLee Nipper .geniv = "<built-in>", 20473952f17eSLee Nipper .ivsize = AES_BLOCK_SIZE, 20483952f17eSLee Nipper .maxauthsize = SHA256_DIGEST_SIZE, 204956af8cd4SLee Nipper } 20503952f17eSLee Nipper }, 20513952f17eSLee Nipper .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP | 20523952f17eSLee Nipper DESC_HDR_SEL0_AESU | 20533952f17eSLee Nipper DESC_HDR_MODE0_AESU_CBC | 20543952f17eSLee Nipper DESC_HDR_SEL1_MDEUA | 20553952f17eSLee Nipper DESC_HDR_MODE1_MDEU_INIT | 20563952f17eSLee Nipper DESC_HDR_MODE1_MDEU_PAD | 20573952f17eSLee Nipper DESC_HDR_MODE1_MDEU_SHA256_HMAC, 20583952f17eSLee Nipper }, 2059d5e4aaefSLee Nipper { .type = CRYPTO_ALG_TYPE_AEAD, 2060d5e4aaefSLee Nipper .alg.crypto = { 206156af8cd4SLee Nipper .cra_name = "authenc(hmac(sha256),cbc(des3_ede))", 206256af8cd4SLee Nipper .cra_driver_name = "authenc-hmac-sha256-cbc-3des-talitos", 206356af8cd4SLee Nipper .cra_blocksize = DES3_EDE_BLOCK_SIZE, 206456af8cd4SLee Nipper .cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC, 206556af8cd4SLee Nipper .cra_type = &crypto_aead_type, 206656af8cd4SLee Nipper .cra_aead = { 206756af8cd4SLee Nipper .setkey = aead_setkey, 206856af8cd4SLee Nipper .setauthsize = aead_setauthsize, 206956af8cd4SLee Nipper .encrypt = aead_encrypt, 207056af8cd4SLee Nipper .decrypt = aead_decrypt, 207156af8cd4SLee Nipper .givencrypt = aead_givencrypt, 20723952f17eSLee Nipper .geniv = "<built-in>", 20733952f17eSLee Nipper .ivsize = DES3_EDE_BLOCK_SIZE, 20743952f17eSLee Nipper .maxauthsize = SHA256_DIGEST_SIZE, 207556af8cd4SLee Nipper } 20763952f17eSLee Nipper }, 20773952f17eSLee Nipper .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP | 20783952f17eSLee Nipper DESC_HDR_SEL0_DEU | 20793952f17eSLee Nipper DESC_HDR_MODE0_DEU_CBC | 20803952f17eSLee Nipper DESC_HDR_MODE0_DEU_3DES | 20813952f17eSLee Nipper DESC_HDR_SEL1_MDEUA | 20823952f17eSLee Nipper DESC_HDR_MODE1_MDEU_INIT | 20833952f17eSLee Nipper DESC_HDR_MODE1_MDEU_PAD | 20843952f17eSLee Nipper DESC_HDR_MODE1_MDEU_SHA256_HMAC, 20853952f17eSLee Nipper }, 2086d5e4aaefSLee Nipper { .type = CRYPTO_ALG_TYPE_AEAD, 2087d5e4aaefSLee Nipper .alg.crypto = { 208856af8cd4SLee Nipper .cra_name = "authenc(hmac(md5),cbc(aes))", 208956af8cd4SLee Nipper .cra_driver_name = "authenc-hmac-md5-cbc-aes-talitos", 209056af8cd4SLee Nipper .cra_blocksize = AES_BLOCK_SIZE, 209156af8cd4SLee Nipper .cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC, 209256af8cd4SLee Nipper .cra_type = &crypto_aead_type, 209356af8cd4SLee Nipper .cra_aead = { 209456af8cd4SLee Nipper .setkey = aead_setkey, 209556af8cd4SLee Nipper .setauthsize = aead_setauthsize, 209656af8cd4SLee Nipper .encrypt = aead_encrypt, 209756af8cd4SLee Nipper .decrypt = aead_decrypt, 209856af8cd4SLee Nipper .givencrypt = aead_givencrypt, 20993952f17eSLee Nipper .geniv = "<built-in>", 21003952f17eSLee Nipper .ivsize = AES_BLOCK_SIZE, 21013952f17eSLee Nipper .maxauthsize = MD5_DIGEST_SIZE, 210256af8cd4SLee Nipper } 21033952f17eSLee Nipper }, 21043952f17eSLee Nipper .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP | 21053952f17eSLee Nipper DESC_HDR_SEL0_AESU | 21063952f17eSLee Nipper DESC_HDR_MODE0_AESU_CBC | 21073952f17eSLee Nipper DESC_HDR_SEL1_MDEUA | 21083952f17eSLee Nipper DESC_HDR_MODE1_MDEU_INIT | 21093952f17eSLee Nipper DESC_HDR_MODE1_MDEU_PAD | 21103952f17eSLee Nipper DESC_HDR_MODE1_MDEU_MD5_HMAC, 21113952f17eSLee Nipper }, 2112d5e4aaefSLee Nipper { .type = CRYPTO_ALG_TYPE_AEAD, 2113d5e4aaefSLee Nipper .alg.crypto = { 211456af8cd4SLee Nipper .cra_name = "authenc(hmac(md5),cbc(des3_ede))", 211556af8cd4SLee Nipper .cra_driver_name = "authenc-hmac-md5-cbc-3des-talitos", 211656af8cd4SLee Nipper .cra_blocksize = DES3_EDE_BLOCK_SIZE, 211756af8cd4SLee Nipper .cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC, 211856af8cd4SLee Nipper .cra_type = &crypto_aead_type, 211956af8cd4SLee Nipper .cra_aead = { 212056af8cd4SLee Nipper .setkey = aead_setkey, 212156af8cd4SLee Nipper .setauthsize = aead_setauthsize, 212256af8cd4SLee Nipper .encrypt = aead_encrypt, 212356af8cd4SLee Nipper .decrypt = aead_decrypt, 212456af8cd4SLee Nipper .givencrypt = aead_givencrypt, 21253952f17eSLee Nipper .geniv = "<built-in>", 21263952f17eSLee Nipper .ivsize = DES3_EDE_BLOCK_SIZE, 21273952f17eSLee Nipper .maxauthsize = MD5_DIGEST_SIZE, 212856af8cd4SLee Nipper } 21293952f17eSLee Nipper }, 21303952f17eSLee Nipper .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP | 21313952f17eSLee Nipper DESC_HDR_SEL0_DEU | 21323952f17eSLee Nipper DESC_HDR_MODE0_DEU_CBC | 21333952f17eSLee Nipper DESC_HDR_MODE0_DEU_3DES | 21343952f17eSLee Nipper DESC_HDR_SEL1_MDEUA | 21353952f17eSLee Nipper DESC_HDR_MODE1_MDEU_INIT | 21363952f17eSLee Nipper DESC_HDR_MODE1_MDEU_PAD | 21373952f17eSLee Nipper DESC_HDR_MODE1_MDEU_MD5_HMAC, 21384de9d0b5SLee Nipper }, 21394de9d0b5SLee Nipper /* ABLKCIPHER algorithms. */ 2140d5e4aaefSLee Nipper { .type = CRYPTO_ALG_TYPE_ABLKCIPHER, 2141d5e4aaefSLee Nipper .alg.crypto = { 21424de9d0b5SLee Nipper .cra_name = "cbc(aes)", 21434de9d0b5SLee Nipper .cra_driver_name = "cbc-aes-talitos", 21444de9d0b5SLee Nipper .cra_blocksize = AES_BLOCK_SIZE, 21454de9d0b5SLee Nipper .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | 21464de9d0b5SLee Nipper CRYPTO_ALG_ASYNC, 21474de9d0b5SLee Nipper .cra_type = &crypto_ablkcipher_type, 21484de9d0b5SLee Nipper .cra_ablkcipher = { 21494de9d0b5SLee Nipper .setkey = ablkcipher_setkey, 21504de9d0b5SLee Nipper .encrypt = ablkcipher_encrypt, 21514de9d0b5SLee Nipper .decrypt = ablkcipher_decrypt, 21524de9d0b5SLee Nipper .geniv = "eseqiv", 21534de9d0b5SLee Nipper .min_keysize = AES_MIN_KEY_SIZE, 21544de9d0b5SLee Nipper .max_keysize = AES_MAX_KEY_SIZE, 21554de9d0b5SLee Nipper .ivsize = AES_BLOCK_SIZE, 21564de9d0b5SLee Nipper } 21574de9d0b5SLee Nipper }, 21584de9d0b5SLee Nipper .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU | 21594de9d0b5SLee Nipper DESC_HDR_SEL0_AESU | 21604de9d0b5SLee Nipper DESC_HDR_MODE0_AESU_CBC, 21614de9d0b5SLee Nipper }, 2162d5e4aaefSLee Nipper { .type = CRYPTO_ALG_TYPE_ABLKCIPHER, 2163d5e4aaefSLee Nipper .alg.crypto = { 21644de9d0b5SLee Nipper .cra_name = "cbc(des3_ede)", 21654de9d0b5SLee Nipper .cra_driver_name = "cbc-3des-talitos", 21664de9d0b5SLee Nipper .cra_blocksize = DES3_EDE_BLOCK_SIZE, 21674de9d0b5SLee Nipper .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | 21684de9d0b5SLee Nipper CRYPTO_ALG_ASYNC, 21694de9d0b5SLee Nipper .cra_type = &crypto_ablkcipher_type, 21704de9d0b5SLee Nipper .cra_ablkcipher = { 21714de9d0b5SLee Nipper .setkey = ablkcipher_setkey, 21724de9d0b5SLee Nipper .encrypt = ablkcipher_encrypt, 21734de9d0b5SLee Nipper .decrypt = ablkcipher_decrypt, 21744de9d0b5SLee Nipper .geniv = "eseqiv", 21754de9d0b5SLee Nipper .min_keysize = DES3_EDE_KEY_SIZE, 21764de9d0b5SLee Nipper .max_keysize = DES3_EDE_KEY_SIZE, 21774de9d0b5SLee Nipper .ivsize = DES3_EDE_BLOCK_SIZE, 21784de9d0b5SLee Nipper } 21794de9d0b5SLee Nipper }, 21804de9d0b5SLee Nipper .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU | 21814de9d0b5SLee Nipper DESC_HDR_SEL0_DEU | 21824de9d0b5SLee Nipper DESC_HDR_MODE0_DEU_CBC | 21834de9d0b5SLee Nipper DESC_HDR_MODE0_DEU_3DES, 2184497f2e6bSLee Nipper }, 2185497f2e6bSLee Nipper /* AHASH algorithms. */ 2186497f2e6bSLee Nipper { .type = CRYPTO_ALG_TYPE_AHASH, 2187497f2e6bSLee Nipper .alg.hash = { 2188497f2e6bSLee Nipper .init = ahash_init, 2189497f2e6bSLee Nipper .update = ahash_update, 2190497f2e6bSLee Nipper .final = ahash_final, 2191497f2e6bSLee Nipper .finup = ahash_finup, 2192497f2e6bSLee Nipper .digest = ahash_digest, 2193497f2e6bSLee Nipper .halg.digestsize = MD5_DIGEST_SIZE, 2194497f2e6bSLee Nipper .halg.base = { 2195497f2e6bSLee Nipper .cra_name = "md5", 2196497f2e6bSLee Nipper .cra_driver_name = "md5-talitos", 2197497f2e6bSLee Nipper .cra_blocksize = MD5_BLOCK_SIZE, 2198497f2e6bSLee Nipper .cra_flags = CRYPTO_ALG_TYPE_AHASH | 2199497f2e6bSLee Nipper CRYPTO_ALG_ASYNC, 2200497f2e6bSLee Nipper .cra_type = &crypto_ahash_type 22019c4a7965SKim Phillips } 2202497f2e6bSLee Nipper }, 2203497f2e6bSLee Nipper .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU | 2204497f2e6bSLee Nipper DESC_HDR_SEL0_MDEUA | 2205497f2e6bSLee Nipper DESC_HDR_MODE0_MDEU_MD5, 2206497f2e6bSLee Nipper }, 2207497f2e6bSLee Nipper { .type = CRYPTO_ALG_TYPE_AHASH, 2208497f2e6bSLee Nipper .alg.hash = { 2209497f2e6bSLee Nipper .init = ahash_init, 2210497f2e6bSLee Nipper .update = ahash_update, 2211497f2e6bSLee Nipper .final = ahash_final, 2212497f2e6bSLee Nipper .finup = ahash_finup, 2213497f2e6bSLee Nipper .digest = ahash_digest, 2214497f2e6bSLee Nipper .halg.digestsize = SHA1_DIGEST_SIZE, 2215497f2e6bSLee Nipper .halg.base = { 2216497f2e6bSLee Nipper .cra_name = "sha1", 2217497f2e6bSLee Nipper .cra_driver_name = "sha1-talitos", 2218497f2e6bSLee Nipper .cra_blocksize = SHA1_BLOCK_SIZE, 2219497f2e6bSLee Nipper .cra_flags = CRYPTO_ALG_TYPE_AHASH | 2220497f2e6bSLee Nipper CRYPTO_ALG_ASYNC, 2221497f2e6bSLee Nipper .cra_type = &crypto_ahash_type 2222497f2e6bSLee Nipper } 2223497f2e6bSLee Nipper }, 2224497f2e6bSLee Nipper .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU | 2225497f2e6bSLee Nipper DESC_HDR_SEL0_MDEUA | 2226497f2e6bSLee Nipper DESC_HDR_MODE0_MDEU_SHA1, 2227497f2e6bSLee Nipper }, 2228497f2e6bSLee Nipper { .type = CRYPTO_ALG_TYPE_AHASH, 2229497f2e6bSLee Nipper .alg.hash = { 2230497f2e6bSLee Nipper .init = ahash_init, 2231497f2e6bSLee Nipper .update = ahash_update, 2232497f2e6bSLee Nipper .final = ahash_final, 2233497f2e6bSLee Nipper .finup = ahash_finup, 2234497f2e6bSLee Nipper .digest = ahash_digest, 223560f208d7SKim Phillips .halg.digestsize = SHA224_DIGEST_SIZE, 223660f208d7SKim Phillips .halg.base = { 223760f208d7SKim Phillips .cra_name = "sha224", 223860f208d7SKim Phillips .cra_driver_name = "sha224-talitos", 223960f208d7SKim Phillips .cra_blocksize = SHA224_BLOCK_SIZE, 224060f208d7SKim Phillips .cra_flags = CRYPTO_ALG_TYPE_AHASH | 224160f208d7SKim Phillips CRYPTO_ALG_ASYNC, 224260f208d7SKim Phillips .cra_type = &crypto_ahash_type 224360f208d7SKim Phillips } 224460f208d7SKim Phillips }, 224560f208d7SKim Phillips .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU | 224660f208d7SKim Phillips DESC_HDR_SEL0_MDEUA | 224760f208d7SKim Phillips DESC_HDR_MODE0_MDEU_SHA224, 224860f208d7SKim Phillips }, 224960f208d7SKim Phillips { .type = CRYPTO_ALG_TYPE_AHASH, 225060f208d7SKim Phillips .alg.hash = { 225160f208d7SKim Phillips .init = ahash_init, 225260f208d7SKim Phillips .update = ahash_update, 225360f208d7SKim Phillips .final = ahash_final, 225460f208d7SKim Phillips .finup = ahash_finup, 225560f208d7SKim Phillips .digest = ahash_digest, 2256497f2e6bSLee Nipper .halg.digestsize = SHA256_DIGEST_SIZE, 2257497f2e6bSLee Nipper .halg.base = { 2258497f2e6bSLee Nipper .cra_name = "sha256", 2259497f2e6bSLee Nipper .cra_driver_name = "sha256-talitos", 2260497f2e6bSLee Nipper .cra_blocksize = SHA256_BLOCK_SIZE, 2261497f2e6bSLee Nipper .cra_flags = CRYPTO_ALG_TYPE_AHASH | 2262497f2e6bSLee Nipper CRYPTO_ALG_ASYNC, 2263497f2e6bSLee Nipper .cra_type = &crypto_ahash_type 2264497f2e6bSLee Nipper } 2265497f2e6bSLee Nipper }, 2266497f2e6bSLee Nipper .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU | 2267497f2e6bSLee Nipper DESC_HDR_SEL0_MDEUA | 2268497f2e6bSLee Nipper DESC_HDR_MODE0_MDEU_SHA256, 2269497f2e6bSLee Nipper }, 2270497f2e6bSLee Nipper { .type = CRYPTO_ALG_TYPE_AHASH, 2271497f2e6bSLee Nipper .alg.hash = { 2272497f2e6bSLee Nipper .init = ahash_init, 2273497f2e6bSLee Nipper .update = ahash_update, 2274497f2e6bSLee Nipper .final = ahash_final, 2275497f2e6bSLee Nipper .finup = ahash_finup, 2276497f2e6bSLee Nipper .digest = ahash_digest, 2277497f2e6bSLee Nipper .halg.digestsize = SHA384_DIGEST_SIZE, 2278497f2e6bSLee Nipper .halg.base = { 2279497f2e6bSLee Nipper .cra_name = "sha384", 2280497f2e6bSLee Nipper .cra_driver_name = "sha384-talitos", 2281497f2e6bSLee Nipper .cra_blocksize = SHA384_BLOCK_SIZE, 2282497f2e6bSLee Nipper .cra_flags = CRYPTO_ALG_TYPE_AHASH | 2283497f2e6bSLee Nipper CRYPTO_ALG_ASYNC, 2284497f2e6bSLee Nipper .cra_type = &crypto_ahash_type 2285497f2e6bSLee Nipper } 2286497f2e6bSLee Nipper }, 2287497f2e6bSLee Nipper .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU | 2288497f2e6bSLee Nipper DESC_HDR_SEL0_MDEUB | 2289497f2e6bSLee Nipper DESC_HDR_MODE0_MDEUB_SHA384, 2290497f2e6bSLee Nipper }, 2291497f2e6bSLee Nipper { .type = CRYPTO_ALG_TYPE_AHASH, 2292497f2e6bSLee Nipper .alg.hash = { 2293497f2e6bSLee Nipper .init = ahash_init, 2294497f2e6bSLee Nipper .update = ahash_update, 2295497f2e6bSLee Nipper .final = ahash_final, 2296497f2e6bSLee Nipper .finup = ahash_finup, 2297497f2e6bSLee Nipper .digest = ahash_digest, 2298497f2e6bSLee Nipper .halg.digestsize = SHA512_DIGEST_SIZE, 2299497f2e6bSLee Nipper .halg.base = { 2300497f2e6bSLee Nipper .cra_name = "sha512", 2301497f2e6bSLee Nipper .cra_driver_name = "sha512-talitos", 2302497f2e6bSLee Nipper .cra_blocksize = SHA512_BLOCK_SIZE, 2303497f2e6bSLee Nipper .cra_flags = CRYPTO_ALG_TYPE_AHASH | 2304497f2e6bSLee Nipper CRYPTO_ALG_ASYNC, 2305497f2e6bSLee Nipper .cra_type = &crypto_ahash_type 2306497f2e6bSLee Nipper } 2307497f2e6bSLee Nipper }, 2308497f2e6bSLee Nipper .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU | 2309497f2e6bSLee Nipper DESC_HDR_SEL0_MDEUB | 2310497f2e6bSLee Nipper DESC_HDR_MODE0_MDEUB_SHA512, 2311497f2e6bSLee Nipper }, 231279b3a418SLee Nipper { .type = CRYPTO_ALG_TYPE_AHASH, 231379b3a418SLee Nipper .alg.hash = { 231479b3a418SLee Nipper .init = ahash_init, 231579b3a418SLee Nipper .update = ahash_update, 231679b3a418SLee Nipper .final = ahash_final, 231779b3a418SLee Nipper .finup = ahash_finup, 231879b3a418SLee Nipper .digest = ahash_digest, 231979b3a418SLee Nipper .setkey = ahash_setkey, 232079b3a418SLee Nipper .halg.digestsize = MD5_DIGEST_SIZE, 232179b3a418SLee Nipper .halg.base = { 232279b3a418SLee Nipper .cra_name = "hmac(md5)", 232379b3a418SLee Nipper .cra_driver_name = "hmac-md5-talitos", 232479b3a418SLee Nipper .cra_blocksize = MD5_BLOCK_SIZE, 232579b3a418SLee Nipper .cra_flags = CRYPTO_ALG_TYPE_AHASH | 232679b3a418SLee Nipper CRYPTO_ALG_ASYNC, 232779b3a418SLee Nipper .cra_type = &crypto_ahash_type 232879b3a418SLee Nipper } 232979b3a418SLee Nipper }, 233079b3a418SLee Nipper .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU | 233179b3a418SLee Nipper DESC_HDR_SEL0_MDEUA | 233279b3a418SLee Nipper DESC_HDR_MODE0_MDEU_MD5, 233379b3a418SLee Nipper }, 233479b3a418SLee Nipper { .type = CRYPTO_ALG_TYPE_AHASH, 233579b3a418SLee Nipper .alg.hash = { 233679b3a418SLee Nipper .init = ahash_init, 233779b3a418SLee Nipper .update = ahash_update, 233879b3a418SLee Nipper .final = ahash_final, 233979b3a418SLee Nipper .finup = ahash_finup, 234079b3a418SLee Nipper .digest = ahash_digest, 234179b3a418SLee Nipper .setkey = ahash_setkey, 234279b3a418SLee Nipper .halg.digestsize = SHA1_DIGEST_SIZE, 234379b3a418SLee Nipper .halg.base = { 234479b3a418SLee Nipper .cra_name = "hmac(sha1)", 234579b3a418SLee Nipper .cra_driver_name = "hmac-sha1-talitos", 234679b3a418SLee Nipper .cra_blocksize = SHA1_BLOCK_SIZE, 234779b3a418SLee Nipper .cra_flags = CRYPTO_ALG_TYPE_AHASH | 234879b3a418SLee Nipper CRYPTO_ALG_ASYNC, 234979b3a418SLee Nipper .cra_type = &crypto_ahash_type 235079b3a418SLee Nipper } 235179b3a418SLee Nipper }, 235279b3a418SLee Nipper .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU | 235379b3a418SLee Nipper DESC_HDR_SEL0_MDEUA | 235479b3a418SLee Nipper DESC_HDR_MODE0_MDEU_SHA1, 235579b3a418SLee Nipper }, 235679b3a418SLee Nipper { .type = CRYPTO_ALG_TYPE_AHASH, 235779b3a418SLee Nipper .alg.hash = { 235879b3a418SLee Nipper .init = ahash_init, 235979b3a418SLee Nipper .update = ahash_update, 236079b3a418SLee Nipper .final = ahash_final, 236179b3a418SLee Nipper .finup = ahash_finup, 236279b3a418SLee Nipper .digest = ahash_digest, 236379b3a418SLee Nipper .setkey = ahash_setkey, 236479b3a418SLee Nipper .halg.digestsize = SHA224_DIGEST_SIZE, 236579b3a418SLee Nipper .halg.base = { 236679b3a418SLee Nipper .cra_name = "hmac(sha224)", 236779b3a418SLee Nipper .cra_driver_name = "hmac-sha224-talitos", 236879b3a418SLee Nipper .cra_blocksize = SHA224_BLOCK_SIZE, 236979b3a418SLee Nipper .cra_flags = CRYPTO_ALG_TYPE_AHASH | 237079b3a418SLee Nipper CRYPTO_ALG_ASYNC, 237179b3a418SLee Nipper .cra_type = &crypto_ahash_type 237279b3a418SLee Nipper } 237379b3a418SLee Nipper }, 237479b3a418SLee Nipper .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU | 237579b3a418SLee Nipper DESC_HDR_SEL0_MDEUA | 237679b3a418SLee Nipper DESC_HDR_MODE0_MDEU_SHA224, 237779b3a418SLee Nipper }, 237879b3a418SLee Nipper { .type = CRYPTO_ALG_TYPE_AHASH, 237979b3a418SLee Nipper .alg.hash = { 238079b3a418SLee Nipper .init = ahash_init, 238179b3a418SLee Nipper .update = ahash_update, 238279b3a418SLee Nipper .final = ahash_final, 238379b3a418SLee Nipper .finup = ahash_finup, 238479b3a418SLee Nipper .digest = ahash_digest, 238579b3a418SLee Nipper .setkey = ahash_setkey, 238679b3a418SLee Nipper .halg.digestsize = SHA256_DIGEST_SIZE, 238779b3a418SLee Nipper .halg.base = { 238879b3a418SLee Nipper .cra_name = "hmac(sha256)", 238979b3a418SLee Nipper .cra_driver_name = "hmac-sha256-talitos", 239079b3a418SLee Nipper .cra_blocksize = SHA256_BLOCK_SIZE, 239179b3a418SLee Nipper .cra_flags = CRYPTO_ALG_TYPE_AHASH | 239279b3a418SLee Nipper CRYPTO_ALG_ASYNC, 239379b3a418SLee Nipper .cra_type = &crypto_ahash_type 239479b3a418SLee Nipper } 239579b3a418SLee Nipper }, 239679b3a418SLee Nipper .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU | 239779b3a418SLee Nipper DESC_HDR_SEL0_MDEUA | 239879b3a418SLee Nipper DESC_HDR_MODE0_MDEU_SHA256, 239979b3a418SLee Nipper }, 240079b3a418SLee Nipper { .type = CRYPTO_ALG_TYPE_AHASH, 240179b3a418SLee Nipper .alg.hash = { 240279b3a418SLee Nipper .init = ahash_init, 240379b3a418SLee Nipper .update = ahash_update, 240479b3a418SLee Nipper .final = ahash_final, 240579b3a418SLee Nipper .finup = ahash_finup, 240679b3a418SLee Nipper .digest = ahash_digest, 240779b3a418SLee Nipper .setkey = ahash_setkey, 240879b3a418SLee Nipper .halg.digestsize = SHA384_DIGEST_SIZE, 240979b3a418SLee Nipper .halg.base = { 241079b3a418SLee Nipper .cra_name = "hmac(sha384)", 241179b3a418SLee Nipper .cra_driver_name = "hmac-sha384-talitos", 241279b3a418SLee Nipper .cra_blocksize = SHA384_BLOCK_SIZE, 241379b3a418SLee Nipper .cra_flags = CRYPTO_ALG_TYPE_AHASH | 241479b3a418SLee Nipper CRYPTO_ALG_ASYNC, 241579b3a418SLee Nipper .cra_type = &crypto_ahash_type 241679b3a418SLee Nipper } 241779b3a418SLee Nipper }, 241879b3a418SLee Nipper .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU | 241979b3a418SLee Nipper DESC_HDR_SEL0_MDEUB | 242079b3a418SLee Nipper DESC_HDR_MODE0_MDEUB_SHA384, 242179b3a418SLee Nipper }, 242279b3a418SLee Nipper { .type = CRYPTO_ALG_TYPE_AHASH, 242379b3a418SLee Nipper .alg.hash = { 242479b3a418SLee Nipper .init = ahash_init, 242579b3a418SLee Nipper .update = ahash_update, 242679b3a418SLee Nipper .final = ahash_final, 242779b3a418SLee Nipper .finup = ahash_finup, 242879b3a418SLee Nipper .digest = ahash_digest, 242979b3a418SLee Nipper .setkey = ahash_setkey, 243079b3a418SLee Nipper .halg.digestsize = SHA512_DIGEST_SIZE, 243179b3a418SLee Nipper .halg.base = { 243279b3a418SLee Nipper .cra_name = "hmac(sha512)", 243379b3a418SLee Nipper .cra_driver_name = "hmac-sha512-talitos", 243479b3a418SLee Nipper .cra_blocksize = SHA512_BLOCK_SIZE, 243579b3a418SLee Nipper .cra_flags = CRYPTO_ALG_TYPE_AHASH | 243679b3a418SLee Nipper CRYPTO_ALG_ASYNC, 243779b3a418SLee Nipper .cra_type = &crypto_ahash_type 243879b3a418SLee Nipper } 243979b3a418SLee Nipper }, 244079b3a418SLee Nipper .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU | 244179b3a418SLee Nipper DESC_HDR_SEL0_MDEUB | 244279b3a418SLee Nipper DESC_HDR_MODE0_MDEUB_SHA512, 244379b3a418SLee Nipper } 24449c4a7965SKim Phillips }; 24459c4a7965SKim Phillips 24469c4a7965SKim Phillips struct talitos_crypto_alg { 24479c4a7965SKim Phillips struct list_head entry; 24489c4a7965SKim Phillips struct device *dev; 2449acbf7c62SLee Nipper struct talitos_alg_template algt; 24509c4a7965SKim Phillips }; 24519c4a7965SKim Phillips 24529c4a7965SKim Phillips static int talitos_cra_init(struct crypto_tfm *tfm) 24539c4a7965SKim Phillips { 24549c4a7965SKim Phillips struct crypto_alg *alg = tfm->__crt_alg; 245519bbbc63SKim Phillips struct talitos_crypto_alg *talitos_alg; 24569c4a7965SKim Phillips struct talitos_ctx *ctx = crypto_tfm_ctx(tfm); 24575228f0f7SKim Phillips struct talitos_private *priv; 24589c4a7965SKim Phillips 2459497f2e6bSLee Nipper if ((alg->cra_flags & CRYPTO_ALG_TYPE_MASK) == CRYPTO_ALG_TYPE_AHASH) 2460497f2e6bSLee Nipper talitos_alg = container_of(__crypto_ahash_alg(alg), 2461497f2e6bSLee Nipper struct talitos_crypto_alg, 2462497f2e6bSLee Nipper algt.alg.hash); 2463497f2e6bSLee Nipper else 2464acbf7c62SLee Nipper talitos_alg = container_of(alg, struct talitos_crypto_alg, 2465acbf7c62SLee Nipper algt.alg.crypto); 246619bbbc63SKim Phillips 24679c4a7965SKim Phillips /* update context with ptr to dev */ 24689c4a7965SKim Phillips ctx->dev = talitos_alg->dev; 246919bbbc63SKim Phillips 24705228f0f7SKim Phillips /* assign SEC channel to tfm in round-robin fashion */ 24715228f0f7SKim Phillips priv = dev_get_drvdata(ctx->dev); 24725228f0f7SKim Phillips ctx->ch = atomic_inc_return(&priv->last_chan) & 24735228f0f7SKim Phillips (priv->num_channels - 1); 24745228f0f7SKim Phillips 24759c4a7965SKim Phillips /* copy descriptor header template value */ 2476acbf7c62SLee Nipper ctx->desc_hdr_template = talitos_alg->algt.desc_hdr_template; 24779c4a7965SKim Phillips 2478602dba5aSKim Phillips /* select done notification */ 2479602dba5aSKim Phillips ctx->desc_hdr_template |= DESC_HDR_DONE_NOTIFY; 2480602dba5aSKim Phillips 2481497f2e6bSLee Nipper return 0; 2482497f2e6bSLee Nipper } 2483497f2e6bSLee Nipper 2484497f2e6bSLee Nipper static int talitos_cra_init_aead(struct crypto_tfm *tfm) 2485497f2e6bSLee Nipper { 2486497f2e6bSLee Nipper struct talitos_ctx *ctx = crypto_tfm_ctx(tfm); 2487497f2e6bSLee Nipper 2488497f2e6bSLee Nipper talitos_cra_init(tfm); 24899c4a7965SKim Phillips 24909c4a7965SKim Phillips /* random first IV */ 249170bcaca7SLee Nipper get_random_bytes(ctx->iv, TALITOS_MAX_IV_LENGTH); 24929c4a7965SKim Phillips 24939c4a7965SKim Phillips return 0; 24949c4a7965SKim Phillips } 24959c4a7965SKim Phillips 2496497f2e6bSLee Nipper static int talitos_cra_init_ahash(struct crypto_tfm *tfm) 2497497f2e6bSLee Nipper { 2498497f2e6bSLee Nipper struct talitos_ctx *ctx = crypto_tfm_ctx(tfm); 2499497f2e6bSLee Nipper 2500497f2e6bSLee Nipper talitos_cra_init(tfm); 2501497f2e6bSLee Nipper 2502497f2e6bSLee Nipper ctx->keylen = 0; 2503497f2e6bSLee Nipper crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm), 2504497f2e6bSLee Nipper sizeof(struct talitos_ahash_req_ctx)); 2505497f2e6bSLee Nipper 2506497f2e6bSLee Nipper return 0; 2507497f2e6bSLee Nipper } 2508497f2e6bSLee Nipper 25099c4a7965SKim Phillips /* 25109c4a7965SKim Phillips * given the alg's descriptor header template, determine whether descriptor 25119c4a7965SKim Phillips * type and primary/secondary execution units required match the hw 25129c4a7965SKim Phillips * capabilities description provided in the device tree node. 25139c4a7965SKim Phillips */ 25149c4a7965SKim Phillips static int hw_supports(struct device *dev, __be32 desc_hdr_template) 25159c4a7965SKim Phillips { 25169c4a7965SKim Phillips struct talitos_private *priv = dev_get_drvdata(dev); 25179c4a7965SKim Phillips int ret; 25189c4a7965SKim Phillips 25199c4a7965SKim Phillips ret = (1 << DESC_TYPE(desc_hdr_template) & priv->desc_types) && 25209c4a7965SKim Phillips (1 << PRIMARY_EU(desc_hdr_template) & priv->exec_units); 25219c4a7965SKim Phillips 25229c4a7965SKim Phillips if (SECONDARY_EU(desc_hdr_template)) 25239c4a7965SKim Phillips ret = ret && (1 << SECONDARY_EU(desc_hdr_template) 25249c4a7965SKim Phillips & priv->exec_units); 25259c4a7965SKim Phillips 25269c4a7965SKim Phillips return ret; 25279c4a7965SKim Phillips } 25289c4a7965SKim Phillips 25292dc11581SGrant Likely static int talitos_remove(struct platform_device *ofdev) 25309c4a7965SKim Phillips { 25319c4a7965SKim Phillips struct device *dev = &ofdev->dev; 25329c4a7965SKim Phillips struct talitos_private *priv = dev_get_drvdata(dev); 25339c4a7965SKim Phillips struct talitos_crypto_alg *t_alg, *n; 25349c4a7965SKim Phillips int i; 25359c4a7965SKim Phillips 25369c4a7965SKim Phillips list_for_each_entry_safe(t_alg, n, &priv->alg_list, entry) { 2537acbf7c62SLee Nipper switch (t_alg->algt.type) { 2538acbf7c62SLee Nipper case CRYPTO_ALG_TYPE_ABLKCIPHER: 2539acbf7c62SLee Nipper case CRYPTO_ALG_TYPE_AEAD: 2540acbf7c62SLee Nipper crypto_unregister_alg(&t_alg->algt.alg.crypto); 2541acbf7c62SLee Nipper break; 2542acbf7c62SLee Nipper case CRYPTO_ALG_TYPE_AHASH: 2543acbf7c62SLee Nipper crypto_unregister_ahash(&t_alg->algt.alg.hash); 2544acbf7c62SLee Nipper break; 2545acbf7c62SLee Nipper } 25469c4a7965SKim Phillips list_del(&t_alg->entry); 25479c4a7965SKim Phillips kfree(t_alg); 25489c4a7965SKim Phillips } 25499c4a7965SKim Phillips 25509c4a7965SKim Phillips if (hw_supports(dev, DESC_HDR_SEL0_RNG)) 25519c4a7965SKim Phillips talitos_unregister_rng(dev); 25529c4a7965SKim Phillips 25539c4a7965SKim Phillips for (i = 0; i < priv->num_channels; i++) 25544b992628SKim Phillips kfree(priv->chan[i].fifo); 25559c4a7965SKim Phillips 25564b992628SKim Phillips kfree(priv->chan); 25579c4a7965SKim Phillips 25589c4a7965SKim Phillips if (priv->irq != NO_IRQ) { 25599c4a7965SKim Phillips free_irq(priv->irq, dev); 25609c4a7965SKim Phillips irq_dispose_mapping(priv->irq); 25619c4a7965SKim Phillips } 25629c4a7965SKim Phillips 25639c4a7965SKim Phillips tasklet_kill(&priv->done_task); 25649c4a7965SKim Phillips 25659c4a7965SKim Phillips iounmap(priv->reg); 25669c4a7965SKim Phillips 25679c4a7965SKim Phillips dev_set_drvdata(dev, NULL); 25689c4a7965SKim Phillips 25699c4a7965SKim Phillips kfree(priv); 25709c4a7965SKim Phillips 25719c4a7965SKim Phillips return 0; 25729c4a7965SKim Phillips } 25739c4a7965SKim Phillips 25749c4a7965SKim Phillips static struct talitos_crypto_alg *talitos_alg_alloc(struct device *dev, 25759c4a7965SKim Phillips struct talitos_alg_template 25769c4a7965SKim Phillips *template) 25779c4a7965SKim Phillips { 257860f208d7SKim Phillips struct talitos_private *priv = dev_get_drvdata(dev); 25799c4a7965SKim Phillips struct talitos_crypto_alg *t_alg; 25809c4a7965SKim Phillips struct crypto_alg *alg; 25819c4a7965SKim Phillips 25829c4a7965SKim Phillips t_alg = kzalloc(sizeof(struct talitos_crypto_alg), GFP_KERNEL); 25839c4a7965SKim Phillips if (!t_alg) 25849c4a7965SKim Phillips return ERR_PTR(-ENOMEM); 25859c4a7965SKim Phillips 2586acbf7c62SLee Nipper t_alg->algt = *template; 2587acbf7c62SLee Nipper 2588acbf7c62SLee Nipper switch (t_alg->algt.type) { 2589acbf7c62SLee Nipper case CRYPTO_ALG_TYPE_ABLKCIPHER: 2590497f2e6bSLee Nipper alg = &t_alg->algt.alg.crypto; 2591497f2e6bSLee Nipper alg->cra_init = talitos_cra_init; 2592497f2e6bSLee Nipper break; 2593acbf7c62SLee Nipper case CRYPTO_ALG_TYPE_AEAD: 2594acbf7c62SLee Nipper alg = &t_alg->algt.alg.crypto; 2595497f2e6bSLee Nipper alg->cra_init = talitos_cra_init_aead; 2596acbf7c62SLee Nipper break; 2597acbf7c62SLee Nipper case CRYPTO_ALG_TYPE_AHASH: 2598acbf7c62SLee Nipper alg = &t_alg->algt.alg.hash.halg.base; 2599497f2e6bSLee Nipper alg->cra_init = talitos_cra_init_ahash; 260079b3a418SLee Nipper if (!(priv->features & TALITOS_FTR_HMAC_OK) && 260179b3a418SLee Nipper !strncmp(alg->cra_name, "hmac", 4)) 260279b3a418SLee Nipper return ERR_PTR(-ENOTSUPP); 260360f208d7SKim Phillips if (!(priv->features & TALITOS_FTR_SHA224_HWINIT) && 260479b3a418SLee Nipper (!strcmp(alg->cra_name, "sha224") || 260579b3a418SLee Nipper !strcmp(alg->cra_name, "hmac(sha224)"))) { 260660f208d7SKim Phillips t_alg->algt.alg.hash.init = ahash_init_sha224_swinit; 260760f208d7SKim Phillips t_alg->algt.desc_hdr_template = 260860f208d7SKim Phillips DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU | 260960f208d7SKim Phillips DESC_HDR_SEL0_MDEUA | 261060f208d7SKim Phillips DESC_HDR_MODE0_MDEU_SHA256; 261160f208d7SKim Phillips } 2612497f2e6bSLee Nipper break; 26131d11911aSKim Phillips default: 26141d11911aSKim Phillips dev_err(dev, "unknown algorithm type %d\n", t_alg->algt.type); 26151d11911aSKim Phillips return ERR_PTR(-EINVAL); 2616acbf7c62SLee Nipper } 26179c4a7965SKim Phillips 26189c4a7965SKim Phillips alg->cra_module = THIS_MODULE; 26199c4a7965SKim Phillips alg->cra_priority = TALITOS_CRA_PRIORITY; 26209c4a7965SKim Phillips alg->cra_alignmask = 0; 26219c4a7965SKim Phillips alg->cra_ctxsize = sizeof(struct talitos_ctx); 26229c4a7965SKim Phillips 26239c4a7965SKim Phillips t_alg->dev = dev; 26249c4a7965SKim Phillips 26259c4a7965SKim Phillips return t_alg; 26269c4a7965SKim Phillips } 26279c4a7965SKim Phillips 26281c48a5c9SGrant Likely static int talitos_probe(struct platform_device *ofdev) 26299c4a7965SKim Phillips { 26309c4a7965SKim Phillips struct device *dev = &ofdev->dev; 263161c7a080SGrant Likely struct device_node *np = ofdev->dev.of_node; 26329c4a7965SKim Phillips struct talitos_private *priv; 26339c4a7965SKim Phillips const unsigned int *prop; 26349c4a7965SKim Phillips int i, err; 26359c4a7965SKim Phillips 26369c4a7965SKim Phillips priv = kzalloc(sizeof(struct talitos_private), GFP_KERNEL); 26379c4a7965SKim Phillips if (!priv) 26389c4a7965SKim Phillips return -ENOMEM; 26399c4a7965SKim Phillips 26409c4a7965SKim Phillips dev_set_drvdata(dev, priv); 26419c4a7965SKim Phillips 26429c4a7965SKim Phillips priv->ofdev = ofdev; 26439c4a7965SKim Phillips 26449c4a7965SKim Phillips tasklet_init(&priv->done_task, talitos_done, (unsigned long)dev); 26459c4a7965SKim Phillips 2646fe5720e2SKim Phillips INIT_LIST_HEAD(&priv->alg_list); 2647fe5720e2SKim Phillips 26489c4a7965SKim Phillips priv->irq = irq_of_parse_and_map(np, 0); 26499c4a7965SKim Phillips 26509c4a7965SKim Phillips if (priv->irq == NO_IRQ) { 26519c4a7965SKim Phillips dev_err(dev, "failed to map irq\n"); 26529c4a7965SKim Phillips err = -EINVAL; 26539c4a7965SKim Phillips goto err_out; 26549c4a7965SKim Phillips } 26559c4a7965SKim Phillips 26569c4a7965SKim Phillips /* get the irq line */ 26579c4a7965SKim Phillips err = request_irq(priv->irq, talitos_interrupt, 0, 26589c4a7965SKim Phillips dev_driver_string(dev), dev); 26599c4a7965SKim Phillips if (err) { 26609c4a7965SKim Phillips dev_err(dev, "failed to request irq %d\n", priv->irq); 26619c4a7965SKim Phillips irq_dispose_mapping(priv->irq); 26629c4a7965SKim Phillips priv->irq = NO_IRQ; 26639c4a7965SKim Phillips goto err_out; 26649c4a7965SKim Phillips } 26659c4a7965SKim Phillips 26669c4a7965SKim Phillips priv->reg = of_iomap(np, 0); 26679c4a7965SKim Phillips if (!priv->reg) { 26689c4a7965SKim Phillips dev_err(dev, "failed to of_iomap\n"); 26699c4a7965SKim Phillips err = -ENOMEM; 26709c4a7965SKim Phillips goto err_out; 26719c4a7965SKim Phillips } 26729c4a7965SKim Phillips 26739c4a7965SKim Phillips /* get SEC version capabilities from device tree */ 26749c4a7965SKim Phillips prop = of_get_property(np, "fsl,num-channels", NULL); 26759c4a7965SKim Phillips if (prop) 26769c4a7965SKim Phillips priv->num_channels = *prop; 26779c4a7965SKim Phillips 26789c4a7965SKim Phillips prop = of_get_property(np, "fsl,channel-fifo-len", NULL); 26799c4a7965SKim Phillips if (prop) 26809c4a7965SKim Phillips priv->chfifo_len = *prop; 26819c4a7965SKim Phillips 26829c4a7965SKim Phillips prop = of_get_property(np, "fsl,exec-units-mask", NULL); 26839c4a7965SKim Phillips if (prop) 26849c4a7965SKim Phillips priv->exec_units = *prop; 26859c4a7965SKim Phillips 26869c4a7965SKim Phillips prop = of_get_property(np, "fsl,descriptor-types-mask", NULL); 26879c4a7965SKim Phillips if (prop) 26889c4a7965SKim Phillips priv->desc_types = *prop; 26899c4a7965SKim Phillips 26909c4a7965SKim Phillips if (!is_power_of_2(priv->num_channels) || !priv->chfifo_len || 26919c4a7965SKim Phillips !priv->exec_units || !priv->desc_types) { 26929c4a7965SKim Phillips dev_err(dev, "invalid property data in device tree node\n"); 26939c4a7965SKim Phillips err = -EINVAL; 26949c4a7965SKim Phillips goto err_out; 26959c4a7965SKim Phillips } 26969c4a7965SKim Phillips 2697f3c85bc1SLee Nipper if (of_device_is_compatible(np, "fsl,sec3.0")) 2698f3c85bc1SLee Nipper priv->features |= TALITOS_FTR_SRC_LINK_TBL_LEN_INCLUDES_EXTENT; 2699f3c85bc1SLee Nipper 2700fe5720e2SKim Phillips if (of_device_is_compatible(np, "fsl,sec2.1")) 270160f208d7SKim Phillips priv->features |= TALITOS_FTR_HW_AUTH_CHECK | 270279b3a418SLee Nipper TALITOS_FTR_SHA224_HWINIT | 270379b3a418SLee Nipper TALITOS_FTR_HMAC_OK; 2704fe5720e2SKim Phillips 27054b992628SKim Phillips priv->chan = kzalloc(sizeof(struct talitos_channel) * 27064b992628SKim Phillips priv->num_channels, GFP_KERNEL); 27074b992628SKim Phillips if (!priv->chan) { 27084b992628SKim Phillips dev_err(dev, "failed to allocate channel management space\n"); 27099c4a7965SKim Phillips err = -ENOMEM; 27109c4a7965SKim Phillips goto err_out; 27119c4a7965SKim Phillips } 27129c4a7965SKim Phillips 27139c4a7965SKim Phillips for (i = 0; i < priv->num_channels; i++) { 27144b992628SKim Phillips spin_lock_init(&priv->chan[i].head_lock); 27154b992628SKim Phillips spin_lock_init(&priv->chan[i].tail_lock); 27169c4a7965SKim Phillips } 27179c4a7965SKim Phillips 27189c4a7965SKim Phillips priv->fifo_len = roundup_pow_of_two(priv->chfifo_len); 27199c4a7965SKim Phillips 27209c4a7965SKim Phillips for (i = 0; i < priv->num_channels; i++) { 27214b992628SKim Phillips priv->chan[i].fifo = kzalloc(sizeof(struct talitos_request) * 27229c4a7965SKim Phillips priv->fifo_len, GFP_KERNEL); 27234b992628SKim Phillips if (!priv->chan[i].fifo) { 27249c4a7965SKim Phillips dev_err(dev, "failed to allocate request fifo %d\n", i); 27259c4a7965SKim Phillips err = -ENOMEM; 27269c4a7965SKim Phillips goto err_out; 27279c4a7965SKim Phillips } 27289c4a7965SKim Phillips } 27299c4a7965SKim Phillips 2730ec6644d6SKim Phillips for (i = 0; i < priv->num_channels; i++) 27314b992628SKim Phillips atomic_set(&priv->chan[i].submit_count, 27324b992628SKim Phillips -(priv->chfifo_len - 1)); 27339c4a7965SKim Phillips 273481eb024cSKim Phillips dma_set_mask(dev, DMA_BIT_MASK(36)); 273581eb024cSKim Phillips 27369c4a7965SKim Phillips /* reset and initialize the h/w */ 27379c4a7965SKim Phillips err = init_device(dev); 27389c4a7965SKim Phillips if (err) { 27399c4a7965SKim Phillips dev_err(dev, "failed to initialize device\n"); 27409c4a7965SKim Phillips goto err_out; 27419c4a7965SKim Phillips } 27429c4a7965SKim Phillips 27439c4a7965SKim Phillips /* register the RNG, if available */ 27449c4a7965SKim Phillips if (hw_supports(dev, DESC_HDR_SEL0_RNG)) { 27459c4a7965SKim Phillips err = talitos_register_rng(dev); 27469c4a7965SKim Phillips if (err) { 27479c4a7965SKim Phillips dev_err(dev, "failed to register hwrng: %d\n", err); 27489c4a7965SKim Phillips goto err_out; 27499c4a7965SKim Phillips } else 27509c4a7965SKim Phillips dev_info(dev, "hwrng\n"); 27519c4a7965SKim Phillips } 27529c4a7965SKim Phillips 27539c4a7965SKim Phillips /* register crypto algorithms the device supports */ 27549c4a7965SKim Phillips for (i = 0; i < ARRAY_SIZE(driver_algs); i++) { 27559c4a7965SKim Phillips if (hw_supports(dev, driver_algs[i].desc_hdr_template)) { 27569c4a7965SKim Phillips struct talitos_crypto_alg *t_alg; 2757acbf7c62SLee Nipper char *name = NULL; 27589c4a7965SKim Phillips 27599c4a7965SKim Phillips t_alg = talitos_alg_alloc(dev, &driver_algs[i]); 27609c4a7965SKim Phillips if (IS_ERR(t_alg)) { 27619c4a7965SKim Phillips err = PTR_ERR(t_alg); 276279b3a418SLee Nipper if (err == -ENOTSUPP) { 276379b3a418SLee Nipper kfree(t_alg); 276479b3a418SLee Nipper continue; 276579b3a418SLee Nipper } 27669c4a7965SKim Phillips goto err_out; 27679c4a7965SKim Phillips } 27689c4a7965SKim Phillips 2769acbf7c62SLee Nipper switch (t_alg->algt.type) { 2770acbf7c62SLee Nipper case CRYPTO_ALG_TYPE_ABLKCIPHER: 2771acbf7c62SLee Nipper case CRYPTO_ALG_TYPE_AEAD: 2772acbf7c62SLee Nipper err = crypto_register_alg( 2773acbf7c62SLee Nipper &t_alg->algt.alg.crypto); 2774acbf7c62SLee Nipper name = t_alg->algt.alg.crypto.cra_driver_name; 2775acbf7c62SLee Nipper break; 2776acbf7c62SLee Nipper case CRYPTO_ALG_TYPE_AHASH: 2777acbf7c62SLee Nipper err = crypto_register_ahash( 2778acbf7c62SLee Nipper &t_alg->algt.alg.hash); 2779acbf7c62SLee Nipper name = 2780acbf7c62SLee Nipper t_alg->algt.alg.hash.halg.base.cra_driver_name; 2781acbf7c62SLee Nipper break; 2782acbf7c62SLee Nipper } 27839c4a7965SKim Phillips if (err) { 27849c4a7965SKim Phillips dev_err(dev, "%s alg registration failed\n", 2785acbf7c62SLee Nipper name); 27869c4a7965SKim Phillips kfree(t_alg); 2787*5b859b6eSKim Phillips } else 27889c4a7965SKim Phillips list_add_tail(&t_alg->entry, &priv->alg_list); 27899c4a7965SKim Phillips } 27909c4a7965SKim Phillips } 2791*5b859b6eSKim Phillips if (!list_empty(&priv->alg_list)) 2792*5b859b6eSKim Phillips dev_info(dev, "%s algorithms registered in /proc/crypto\n", 2793*5b859b6eSKim Phillips (char *)of_get_property(np, "compatible", NULL)); 27949c4a7965SKim Phillips 27959c4a7965SKim Phillips return 0; 27969c4a7965SKim Phillips 27979c4a7965SKim Phillips err_out: 27989c4a7965SKim Phillips talitos_remove(ofdev); 27999c4a7965SKim Phillips 28009c4a7965SKim Phillips return err; 28019c4a7965SKim Phillips } 28029c4a7965SKim Phillips 28036c3f975aSMárton Németh static const struct of_device_id talitos_match[] = { 28049c4a7965SKim Phillips { 28059c4a7965SKim Phillips .compatible = "fsl,sec2.0", 28069c4a7965SKim Phillips }, 28079c4a7965SKim Phillips {}, 28089c4a7965SKim Phillips }; 28099c4a7965SKim Phillips MODULE_DEVICE_TABLE(of, talitos_match); 28109c4a7965SKim Phillips 28111c48a5c9SGrant Likely static struct platform_driver talitos_driver = { 28124018294bSGrant Likely .driver = { 28139c4a7965SKim Phillips .name = "talitos", 28144018294bSGrant Likely .owner = THIS_MODULE, 28154018294bSGrant Likely .of_match_table = talitos_match, 28164018294bSGrant Likely }, 28179c4a7965SKim Phillips .probe = talitos_probe, 2818596f1034SAl Viro .remove = talitos_remove, 28199c4a7965SKim Phillips }; 28209c4a7965SKim Phillips 28219c4a7965SKim Phillips static int __init talitos_init(void) 28229c4a7965SKim Phillips { 28231c48a5c9SGrant Likely return platform_driver_register(&talitos_driver); 28249c4a7965SKim Phillips } 28259c4a7965SKim Phillips module_init(talitos_init); 28269c4a7965SKim Phillips 28279c4a7965SKim Phillips static void __exit talitos_exit(void) 28289c4a7965SKim Phillips { 28291c48a5c9SGrant Likely platform_driver_unregister(&talitos_driver); 28309c4a7965SKim Phillips } 28319c4a7965SKim Phillips module_exit(talitos_exit); 28329c4a7965SKim Phillips 28339c4a7965SKim Phillips MODULE_LICENSE("GPL"); 28349c4a7965SKim Phillips MODULE_AUTHOR("Kim Phillips <kim.phillips@freescale.com>"); 28359c4a7965SKim Phillips MODULE_DESCRIPTION("Freescale integrated security engine (SEC) driver"); 2836