19c4a7965SKim Phillips /* 29c4a7965SKim Phillips * talitos - Freescale Integrated Security Engine (SEC) device driver 39c4a7965SKim Phillips * 49c4a7965SKim Phillips * Copyright (c) 2008 Freescale Semiconductor, Inc. 59c4a7965SKim Phillips * 69c4a7965SKim Phillips * Scatterlist Crypto API glue code copied from files with the following: 79c4a7965SKim Phillips * Copyright (c) 2006-2007 Herbert Xu <herbert@gondor.apana.org.au> 89c4a7965SKim Phillips * 99c4a7965SKim Phillips * Crypto algorithm registration code copied from hifn driver: 109c4a7965SKim Phillips * 2007+ Copyright (c) Evgeniy Polyakov <johnpol@2ka.mipt.ru> 119c4a7965SKim Phillips * All rights reserved. 129c4a7965SKim Phillips * 139c4a7965SKim Phillips * This program is free software; you can redistribute it and/or modify 149c4a7965SKim Phillips * it under the terms of the GNU General Public License as published by 159c4a7965SKim Phillips * the Free Software Foundation; either version 2 of the License, or 169c4a7965SKim Phillips * (at your option) any later version. 179c4a7965SKim Phillips * 189c4a7965SKim Phillips * This program is distributed in the hope that it will be useful, 199c4a7965SKim Phillips * but WITHOUT ANY WARRANTY; without even the implied warranty of 209c4a7965SKim Phillips * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 219c4a7965SKim Phillips * GNU General Public License for more details. 229c4a7965SKim Phillips * 239c4a7965SKim Phillips * You should have received a copy of the GNU General Public License 249c4a7965SKim Phillips * along with this program; if not, write to the Free Software 259c4a7965SKim Phillips * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 269c4a7965SKim Phillips */ 279c4a7965SKim Phillips 289c4a7965SKim Phillips #include <linux/kernel.h> 299c4a7965SKim Phillips #include <linux/module.h> 309c4a7965SKim Phillips #include <linux/mod_devicetable.h> 319c4a7965SKim Phillips #include <linux/device.h> 329c4a7965SKim Phillips #include <linux/interrupt.h> 339c4a7965SKim Phillips #include <linux/crypto.h> 349c4a7965SKim Phillips #include <linux/hw_random.h> 359c4a7965SKim Phillips #include <linux/of_platform.h> 369c4a7965SKim Phillips #include <linux/dma-mapping.h> 379c4a7965SKim Phillips #include <linux/io.h> 389c4a7965SKim Phillips #include <linux/spinlock.h> 399c4a7965SKim Phillips #include <linux/rtnetlink.h> 409c4a7965SKim Phillips 419c4a7965SKim Phillips #include <crypto/algapi.h> 429c4a7965SKim Phillips #include <crypto/aes.h> 433952f17eSLee Nipper #include <crypto/des.h> 449c4a7965SKim Phillips #include <crypto/sha.h> 459c4a7965SKim Phillips #include <crypto/aead.h> 469c4a7965SKim Phillips #include <crypto/authenc.h> 474de9d0b5SLee Nipper #include <crypto/skcipher.h> 484de9d0b5SLee Nipper #include <crypto/scatterwalk.h> 499c4a7965SKim Phillips 509c4a7965SKim Phillips #include "talitos.h" 519c4a7965SKim Phillips 529c4a7965SKim Phillips #define TALITOS_TIMEOUT 100000 539c4a7965SKim Phillips #define TALITOS_MAX_DATA_LEN 65535 549c4a7965SKim Phillips 559c4a7965SKim Phillips #define DESC_TYPE(desc_hdr) ((be32_to_cpu(desc_hdr) >> 3) & 0x1f) 569c4a7965SKim Phillips #define PRIMARY_EU(desc_hdr) ((be32_to_cpu(desc_hdr) >> 28) & 0xf) 579c4a7965SKim Phillips #define SECONDARY_EU(desc_hdr) ((be32_to_cpu(desc_hdr) >> 16) & 0xf) 589c4a7965SKim Phillips 599c4a7965SKim Phillips /* descriptor pointer entry */ 609c4a7965SKim Phillips struct talitos_ptr { 619c4a7965SKim Phillips __be16 len; /* length */ 629c4a7965SKim Phillips u8 j_extent; /* jump to sg link table and/or extent */ 639c4a7965SKim Phillips u8 eptr; /* extended address */ 649c4a7965SKim Phillips __be32 ptr; /* address */ 659c4a7965SKim Phillips }; 669c4a7965SKim Phillips 679c4a7965SKim Phillips /* descriptor */ 689c4a7965SKim Phillips struct talitos_desc { 699c4a7965SKim Phillips __be32 hdr; /* header high bits */ 709c4a7965SKim Phillips __be32 hdr_lo; /* header low bits */ 719c4a7965SKim Phillips struct talitos_ptr ptr[7]; /* ptr/len pair array */ 729c4a7965SKim Phillips }; 739c4a7965SKim Phillips 749c4a7965SKim Phillips /** 759c4a7965SKim Phillips * talitos_request - descriptor submission request 769c4a7965SKim Phillips * @desc: descriptor pointer (kernel virtual) 779c4a7965SKim Phillips * @dma_desc: descriptor's physical bus address 789c4a7965SKim Phillips * @callback: whom to call when descriptor processing is done 799c4a7965SKim Phillips * @context: caller context (optional) 809c4a7965SKim Phillips */ 819c4a7965SKim Phillips struct talitos_request { 829c4a7965SKim Phillips struct talitos_desc *desc; 839c4a7965SKim Phillips dma_addr_t dma_desc; 849c4a7965SKim Phillips void (*callback) (struct device *dev, struct talitos_desc *desc, 859c4a7965SKim Phillips void *context, int error); 869c4a7965SKim Phillips void *context; 879c4a7965SKim Phillips }; 889c4a7965SKim Phillips 899c4a7965SKim Phillips struct talitos_private { 909c4a7965SKim Phillips struct device *dev; 919c4a7965SKim Phillips struct of_device *ofdev; 929c4a7965SKim Phillips void __iomem *reg; 939c4a7965SKim Phillips int irq; 949c4a7965SKim Phillips 959c4a7965SKim Phillips /* SEC version geometry (from device tree node) */ 969c4a7965SKim Phillips unsigned int num_channels; 979c4a7965SKim Phillips unsigned int chfifo_len; 989c4a7965SKim Phillips unsigned int exec_units; 999c4a7965SKim Phillips unsigned int desc_types; 1009c4a7965SKim Phillips 101f3c85bc1SLee Nipper /* SEC Compatibility info */ 102f3c85bc1SLee Nipper unsigned long features; 103f3c85bc1SLee Nipper 1049c4a7965SKim Phillips /* next channel to be assigned next incoming descriptor */ 1059c4a7965SKim Phillips atomic_t last_chan; 1069c4a7965SKim Phillips 107ec6644d6SKim Phillips /* per-channel number of requests pending in channel h/w fifo */ 108ec6644d6SKim Phillips atomic_t *submit_count; 109ec6644d6SKim Phillips 1109c4a7965SKim Phillips /* per-channel request fifo */ 1119c4a7965SKim Phillips struct talitos_request **fifo; 1129c4a7965SKim Phillips 1139c4a7965SKim Phillips /* 1149c4a7965SKim Phillips * length of the request fifo 1159c4a7965SKim Phillips * fifo_len is chfifo_len rounded up to next power of 2 1169c4a7965SKim Phillips * so we can use bitwise ops to wrap 1179c4a7965SKim Phillips */ 1189c4a7965SKim Phillips unsigned int fifo_len; 1199c4a7965SKim Phillips 1209c4a7965SKim Phillips /* per-channel index to next free descriptor request */ 1219c4a7965SKim Phillips int *head; 1229c4a7965SKim Phillips 1239c4a7965SKim Phillips /* per-channel index to next in-progress/done descriptor request */ 1249c4a7965SKim Phillips int *tail; 1259c4a7965SKim Phillips 1269c4a7965SKim Phillips /* per-channel request submission (head) and release (tail) locks */ 1279c4a7965SKim Phillips spinlock_t *head_lock; 1289c4a7965SKim Phillips spinlock_t *tail_lock; 1299c4a7965SKim Phillips 1309c4a7965SKim Phillips /* request callback tasklet */ 1319c4a7965SKim Phillips struct tasklet_struct done_task; 1329c4a7965SKim Phillips 1339c4a7965SKim Phillips /* list of registered algorithms */ 1349c4a7965SKim Phillips struct list_head alg_list; 1359c4a7965SKim Phillips 1369c4a7965SKim Phillips /* hwrng device */ 1379c4a7965SKim Phillips struct hwrng rng; 1389c4a7965SKim Phillips }; 1399c4a7965SKim Phillips 140f3c85bc1SLee Nipper /* .features flag */ 141f3c85bc1SLee Nipper #define TALITOS_FTR_SRC_LINK_TBL_LEN_INCLUDES_EXTENT 0x00000001 142fe5720e2SKim Phillips #define TALITOS_FTR_HW_AUTH_CHECK 0x00000002 143f3c85bc1SLee Nipper 1449c4a7965SKim Phillips /* 1459c4a7965SKim Phillips * map virtual single (contiguous) pointer to h/w descriptor pointer 1469c4a7965SKim Phillips */ 1479c4a7965SKim Phillips static void map_single_talitos_ptr(struct device *dev, 1489c4a7965SKim Phillips struct talitos_ptr *talitos_ptr, 1499c4a7965SKim Phillips unsigned short len, void *data, 1509c4a7965SKim Phillips unsigned char extent, 1519c4a7965SKim Phillips enum dma_data_direction dir) 1529c4a7965SKim Phillips { 1539c4a7965SKim Phillips talitos_ptr->len = cpu_to_be16(len); 1549c4a7965SKim Phillips talitos_ptr->ptr = cpu_to_be32(dma_map_single(dev, data, len, dir)); 1559c4a7965SKim Phillips talitos_ptr->j_extent = extent; 1569c4a7965SKim Phillips } 1579c4a7965SKim Phillips 1589c4a7965SKim Phillips /* 1599c4a7965SKim Phillips * unmap bus single (contiguous) h/w descriptor pointer 1609c4a7965SKim Phillips */ 1619c4a7965SKim Phillips static void unmap_single_talitos_ptr(struct device *dev, 1629c4a7965SKim Phillips struct talitos_ptr *talitos_ptr, 1639c4a7965SKim Phillips enum dma_data_direction dir) 1649c4a7965SKim Phillips { 1659c4a7965SKim Phillips dma_unmap_single(dev, be32_to_cpu(talitos_ptr->ptr), 1669c4a7965SKim Phillips be16_to_cpu(talitos_ptr->len), dir); 1679c4a7965SKim Phillips } 1689c4a7965SKim Phillips 1699c4a7965SKim Phillips static int reset_channel(struct device *dev, int ch) 1709c4a7965SKim Phillips { 1719c4a7965SKim Phillips struct talitos_private *priv = dev_get_drvdata(dev); 1729c4a7965SKim Phillips unsigned int timeout = TALITOS_TIMEOUT; 1739c4a7965SKim Phillips 1749c4a7965SKim Phillips setbits32(priv->reg + TALITOS_CCCR(ch), TALITOS_CCCR_RESET); 1759c4a7965SKim Phillips 1769c4a7965SKim Phillips while ((in_be32(priv->reg + TALITOS_CCCR(ch)) & TALITOS_CCCR_RESET) 1779c4a7965SKim Phillips && --timeout) 1789c4a7965SKim Phillips cpu_relax(); 1799c4a7965SKim Phillips 1809c4a7965SKim Phillips if (timeout == 0) { 1819c4a7965SKim Phillips dev_err(dev, "failed to reset channel %d\n", ch); 1829c4a7965SKim Phillips return -EIO; 1839c4a7965SKim Phillips } 1849c4a7965SKim Phillips 1859c4a7965SKim Phillips /* set done writeback and IRQ */ 1869c4a7965SKim Phillips setbits32(priv->reg + TALITOS_CCCR_LO(ch), TALITOS_CCCR_LO_CDWE | 1879c4a7965SKim Phillips TALITOS_CCCR_LO_CDIE); 1889c4a7965SKim Phillips 189fe5720e2SKim Phillips /* and ICCR writeback, if available */ 190fe5720e2SKim Phillips if (priv->features & TALITOS_FTR_HW_AUTH_CHECK) 191fe5720e2SKim Phillips setbits32(priv->reg + TALITOS_CCCR_LO(ch), 192fe5720e2SKim Phillips TALITOS_CCCR_LO_IWSE); 193fe5720e2SKim Phillips 1949c4a7965SKim Phillips return 0; 1959c4a7965SKim Phillips } 1969c4a7965SKim Phillips 1979c4a7965SKim Phillips static int reset_device(struct device *dev) 1989c4a7965SKim Phillips { 1999c4a7965SKim Phillips struct talitos_private *priv = dev_get_drvdata(dev); 2009c4a7965SKim Phillips unsigned int timeout = TALITOS_TIMEOUT; 2019c4a7965SKim Phillips 2029c4a7965SKim Phillips setbits32(priv->reg + TALITOS_MCR, TALITOS_MCR_SWR); 2039c4a7965SKim Phillips 2049c4a7965SKim Phillips while ((in_be32(priv->reg + TALITOS_MCR) & TALITOS_MCR_SWR) 2059c4a7965SKim Phillips && --timeout) 2069c4a7965SKim Phillips cpu_relax(); 2079c4a7965SKim Phillips 2089c4a7965SKim Phillips if (timeout == 0) { 2099c4a7965SKim Phillips dev_err(dev, "failed to reset device\n"); 2109c4a7965SKim Phillips return -EIO; 2119c4a7965SKim Phillips } 2129c4a7965SKim Phillips 2139c4a7965SKim Phillips return 0; 2149c4a7965SKim Phillips } 2159c4a7965SKim Phillips 2169c4a7965SKim Phillips /* 2179c4a7965SKim Phillips * Reset and initialize the device 2189c4a7965SKim Phillips */ 2199c4a7965SKim Phillips static int init_device(struct device *dev) 2209c4a7965SKim Phillips { 2219c4a7965SKim Phillips struct talitos_private *priv = dev_get_drvdata(dev); 2229c4a7965SKim Phillips int ch, err; 2239c4a7965SKim Phillips 2249c4a7965SKim Phillips /* 2259c4a7965SKim Phillips * Master reset 2269c4a7965SKim Phillips * errata documentation: warning: certain SEC interrupts 2279c4a7965SKim Phillips * are not fully cleared by writing the MCR:SWR bit, 2289c4a7965SKim Phillips * set bit twice to completely reset 2299c4a7965SKim Phillips */ 2309c4a7965SKim Phillips err = reset_device(dev); 2319c4a7965SKim Phillips if (err) 2329c4a7965SKim Phillips return err; 2339c4a7965SKim Phillips 2349c4a7965SKim Phillips err = reset_device(dev); 2359c4a7965SKim Phillips if (err) 2369c4a7965SKim Phillips return err; 2379c4a7965SKim Phillips 2389c4a7965SKim Phillips /* reset channels */ 2399c4a7965SKim Phillips for (ch = 0; ch < priv->num_channels; ch++) { 2409c4a7965SKim Phillips err = reset_channel(dev, ch); 2419c4a7965SKim Phillips if (err) 2429c4a7965SKim Phillips return err; 2439c4a7965SKim Phillips } 2449c4a7965SKim Phillips 2459c4a7965SKim Phillips /* enable channel done and error interrupts */ 2469c4a7965SKim Phillips setbits32(priv->reg + TALITOS_IMR, TALITOS_IMR_INIT); 2479c4a7965SKim Phillips setbits32(priv->reg + TALITOS_IMR_LO, TALITOS_IMR_LO_INIT); 2489c4a7965SKim Phillips 249fe5720e2SKim Phillips /* disable integrity check error interrupts (use writeback instead) */ 250fe5720e2SKim Phillips if (priv->features & TALITOS_FTR_HW_AUTH_CHECK) 251fe5720e2SKim Phillips setbits32(priv->reg + TALITOS_MDEUICR_LO, 252fe5720e2SKim Phillips TALITOS_MDEUICR_LO_ICE); 253fe5720e2SKim Phillips 2549c4a7965SKim Phillips return 0; 2559c4a7965SKim Phillips } 2569c4a7965SKim Phillips 2579c4a7965SKim Phillips /** 2589c4a7965SKim Phillips * talitos_submit - submits a descriptor to the device for processing 2599c4a7965SKim Phillips * @dev: the SEC device to be used 2609c4a7965SKim Phillips * @desc: the descriptor to be processed by the device 2619c4a7965SKim Phillips * @callback: whom to call when processing is complete 2629c4a7965SKim Phillips * @context: a handle for use by caller (optional) 2639c4a7965SKim Phillips * 2649c4a7965SKim Phillips * desc must contain valid dma-mapped (bus physical) address pointers. 2659c4a7965SKim Phillips * callback must check err and feedback in descriptor header 2669c4a7965SKim Phillips * for device processing status. 2679c4a7965SKim Phillips */ 2689c4a7965SKim Phillips static int talitos_submit(struct device *dev, struct talitos_desc *desc, 2699c4a7965SKim Phillips void (*callback)(struct device *dev, 2709c4a7965SKim Phillips struct talitos_desc *desc, 2719c4a7965SKim Phillips void *context, int error), 2729c4a7965SKim Phillips void *context) 2739c4a7965SKim Phillips { 2749c4a7965SKim Phillips struct talitos_private *priv = dev_get_drvdata(dev); 2759c4a7965SKim Phillips struct talitos_request *request; 2769c4a7965SKim Phillips unsigned long flags, ch; 2779c4a7965SKim Phillips int head; 2789c4a7965SKim Phillips 2799c4a7965SKim Phillips /* select done notification */ 2809c4a7965SKim Phillips desc->hdr |= DESC_HDR_DONE_NOTIFY; 2819c4a7965SKim Phillips 2829c4a7965SKim Phillips /* emulate SEC's round-robin channel fifo polling scheme */ 2839c4a7965SKim Phillips ch = atomic_inc_return(&priv->last_chan) & (priv->num_channels - 1); 2849c4a7965SKim Phillips 2859c4a7965SKim Phillips spin_lock_irqsave(&priv->head_lock[ch], flags); 2869c4a7965SKim Phillips 287ec6644d6SKim Phillips if (!atomic_inc_not_zero(&priv->submit_count[ch])) { 288ec6644d6SKim Phillips /* h/w fifo is full */ 2899c4a7965SKim Phillips spin_unlock_irqrestore(&priv->head_lock[ch], flags); 2909c4a7965SKim Phillips return -EAGAIN; 2919c4a7965SKim Phillips } 2929c4a7965SKim Phillips 293ec6644d6SKim Phillips head = priv->head[ch]; 294ec6644d6SKim Phillips request = &priv->fifo[ch][head]; 295ec6644d6SKim Phillips 2969c4a7965SKim Phillips /* map descriptor and save caller data */ 2979c4a7965SKim Phillips request->dma_desc = dma_map_single(dev, desc, sizeof(*desc), 2989c4a7965SKim Phillips DMA_BIDIRECTIONAL); 2999c4a7965SKim Phillips request->callback = callback; 3009c4a7965SKim Phillips request->context = context; 3019c4a7965SKim Phillips 3029c4a7965SKim Phillips /* increment fifo head */ 3039c4a7965SKim Phillips priv->head[ch] = (priv->head[ch] + 1) & (priv->fifo_len - 1); 3049c4a7965SKim Phillips 3059c4a7965SKim Phillips smp_wmb(); 3069c4a7965SKim Phillips request->desc = desc; 3079c4a7965SKim Phillips 3089c4a7965SKim Phillips /* GO! */ 3099c4a7965SKim Phillips wmb(); 3109c4a7965SKim Phillips out_be32(priv->reg + TALITOS_FF_LO(ch), request->dma_desc); 3119c4a7965SKim Phillips 3129c4a7965SKim Phillips spin_unlock_irqrestore(&priv->head_lock[ch], flags); 3139c4a7965SKim Phillips 3149c4a7965SKim Phillips return -EINPROGRESS; 3159c4a7965SKim Phillips } 3169c4a7965SKim Phillips 3179c4a7965SKim Phillips /* 3189c4a7965SKim Phillips * process what was done, notify callback of error if not 3199c4a7965SKim Phillips */ 3209c4a7965SKim Phillips static void flush_channel(struct device *dev, int ch, int error, int reset_ch) 3219c4a7965SKim Phillips { 3229c4a7965SKim Phillips struct talitos_private *priv = dev_get_drvdata(dev); 3239c4a7965SKim Phillips struct talitos_request *request, saved_req; 3249c4a7965SKim Phillips unsigned long flags; 3259c4a7965SKim Phillips int tail, status; 3269c4a7965SKim Phillips 3279c4a7965SKim Phillips spin_lock_irqsave(&priv->tail_lock[ch], flags); 3289c4a7965SKim Phillips 3299c4a7965SKim Phillips tail = priv->tail[ch]; 3309c4a7965SKim Phillips while (priv->fifo[ch][tail].desc) { 3319c4a7965SKim Phillips request = &priv->fifo[ch][tail]; 3329c4a7965SKim Phillips 3339c4a7965SKim Phillips /* descriptors with their done bits set don't get the error */ 3349c4a7965SKim Phillips rmb(); 335ca38a814SLee Nipper if ((request->desc->hdr & DESC_HDR_DONE) == DESC_HDR_DONE) 3369c4a7965SKim Phillips status = 0; 337ca38a814SLee Nipper else 3389c4a7965SKim Phillips if (!error) 3399c4a7965SKim Phillips break; 3409c4a7965SKim Phillips else 3419c4a7965SKim Phillips status = error; 3429c4a7965SKim Phillips 3439c4a7965SKim Phillips dma_unmap_single(dev, request->dma_desc, 344*e938e465SKim Phillips sizeof(struct talitos_desc), 345*e938e465SKim Phillips DMA_BIDIRECTIONAL); 3469c4a7965SKim Phillips 3479c4a7965SKim Phillips /* copy entries so we can call callback outside lock */ 3489c4a7965SKim Phillips saved_req.desc = request->desc; 3499c4a7965SKim Phillips saved_req.callback = request->callback; 3509c4a7965SKim Phillips saved_req.context = request->context; 3519c4a7965SKim Phillips 3529c4a7965SKim Phillips /* release request entry in fifo */ 3539c4a7965SKim Phillips smp_wmb(); 3549c4a7965SKim Phillips request->desc = NULL; 3559c4a7965SKim Phillips 3569c4a7965SKim Phillips /* increment fifo tail */ 3579c4a7965SKim Phillips priv->tail[ch] = (tail + 1) & (priv->fifo_len - 1); 3589c4a7965SKim Phillips 3599c4a7965SKim Phillips spin_unlock_irqrestore(&priv->tail_lock[ch], flags); 360ec6644d6SKim Phillips 361ec6644d6SKim Phillips atomic_dec(&priv->submit_count[ch]); 362ec6644d6SKim Phillips 3639c4a7965SKim Phillips saved_req.callback(dev, saved_req.desc, saved_req.context, 3649c4a7965SKim Phillips status); 3659c4a7965SKim Phillips /* channel may resume processing in single desc error case */ 3669c4a7965SKim Phillips if (error && !reset_ch && status == error) 3679c4a7965SKim Phillips return; 3689c4a7965SKim Phillips spin_lock_irqsave(&priv->tail_lock[ch], flags); 3699c4a7965SKim Phillips tail = priv->tail[ch]; 3709c4a7965SKim Phillips } 3719c4a7965SKim Phillips 3729c4a7965SKim Phillips spin_unlock_irqrestore(&priv->tail_lock[ch], flags); 3739c4a7965SKim Phillips } 3749c4a7965SKim Phillips 3759c4a7965SKim Phillips /* 3769c4a7965SKim Phillips * process completed requests for channels that have done status 3779c4a7965SKim Phillips */ 3789c4a7965SKim Phillips static void talitos_done(unsigned long data) 3799c4a7965SKim Phillips { 3809c4a7965SKim Phillips struct device *dev = (struct device *)data; 3819c4a7965SKim Phillips struct talitos_private *priv = dev_get_drvdata(dev); 3829c4a7965SKim Phillips int ch; 3839c4a7965SKim Phillips 3849c4a7965SKim Phillips for (ch = 0; ch < priv->num_channels; ch++) 3859c4a7965SKim Phillips flush_channel(dev, ch, 0, 0); 3861c2e8811SLee Nipper 3871c2e8811SLee Nipper /* At this point, all completed channels have been processed. 3881c2e8811SLee Nipper * Unmask done interrupts for channels completed later on. 3891c2e8811SLee Nipper */ 390fe5720e2SKim Phillips setbits32(priv->reg + TALITOS_IMR, TALITOS_IMR_INIT); 391fe5720e2SKim Phillips setbits32(priv->reg + TALITOS_IMR_LO, TALITOS_IMR_LO_INIT); 3929c4a7965SKim Phillips } 3939c4a7965SKim Phillips 3949c4a7965SKim Phillips /* 3959c4a7965SKim Phillips * locate current (offending) descriptor 3969c4a7965SKim Phillips */ 3979c4a7965SKim Phillips static struct talitos_desc *current_desc(struct device *dev, int ch) 3989c4a7965SKim Phillips { 3999c4a7965SKim Phillips struct talitos_private *priv = dev_get_drvdata(dev); 4009c4a7965SKim Phillips int tail = priv->tail[ch]; 4019c4a7965SKim Phillips dma_addr_t cur_desc; 4029c4a7965SKim Phillips 4039c4a7965SKim Phillips cur_desc = in_be32(priv->reg + TALITOS_CDPR_LO(ch)); 4049c4a7965SKim Phillips 4059c4a7965SKim Phillips while (priv->fifo[ch][tail].dma_desc != cur_desc) { 4069c4a7965SKim Phillips tail = (tail + 1) & (priv->fifo_len - 1); 4079c4a7965SKim Phillips if (tail == priv->tail[ch]) { 4089c4a7965SKim Phillips dev_err(dev, "couldn't locate current descriptor\n"); 4099c4a7965SKim Phillips return NULL; 4109c4a7965SKim Phillips } 4119c4a7965SKim Phillips } 4129c4a7965SKim Phillips 4139c4a7965SKim Phillips return priv->fifo[ch][tail].desc; 4149c4a7965SKim Phillips } 4159c4a7965SKim Phillips 4169c4a7965SKim Phillips /* 4179c4a7965SKim Phillips * user diagnostics; report root cause of error based on execution unit status 4189c4a7965SKim Phillips */ 419*e938e465SKim Phillips static void report_eu_error(struct device *dev, int ch, 420*e938e465SKim Phillips struct talitos_desc *desc) 4219c4a7965SKim Phillips { 4229c4a7965SKim Phillips struct talitos_private *priv = dev_get_drvdata(dev); 4239c4a7965SKim Phillips int i; 4249c4a7965SKim Phillips 4259c4a7965SKim Phillips switch (desc->hdr & DESC_HDR_SEL0_MASK) { 4269c4a7965SKim Phillips case DESC_HDR_SEL0_AFEU: 4279c4a7965SKim Phillips dev_err(dev, "AFEUISR 0x%08x_%08x\n", 4289c4a7965SKim Phillips in_be32(priv->reg + TALITOS_AFEUISR), 4299c4a7965SKim Phillips in_be32(priv->reg + TALITOS_AFEUISR_LO)); 4309c4a7965SKim Phillips break; 4319c4a7965SKim Phillips case DESC_HDR_SEL0_DEU: 4329c4a7965SKim Phillips dev_err(dev, "DEUISR 0x%08x_%08x\n", 4339c4a7965SKim Phillips in_be32(priv->reg + TALITOS_DEUISR), 4349c4a7965SKim Phillips in_be32(priv->reg + TALITOS_DEUISR_LO)); 4359c4a7965SKim Phillips break; 4369c4a7965SKim Phillips case DESC_HDR_SEL0_MDEUA: 4379c4a7965SKim Phillips case DESC_HDR_SEL0_MDEUB: 4389c4a7965SKim Phillips dev_err(dev, "MDEUISR 0x%08x_%08x\n", 4399c4a7965SKim Phillips in_be32(priv->reg + TALITOS_MDEUISR), 4409c4a7965SKim Phillips in_be32(priv->reg + TALITOS_MDEUISR_LO)); 4419c4a7965SKim Phillips break; 4429c4a7965SKim Phillips case DESC_HDR_SEL0_RNG: 4439c4a7965SKim Phillips dev_err(dev, "RNGUISR 0x%08x_%08x\n", 4449c4a7965SKim Phillips in_be32(priv->reg + TALITOS_RNGUISR), 4459c4a7965SKim Phillips in_be32(priv->reg + TALITOS_RNGUISR_LO)); 4469c4a7965SKim Phillips break; 4479c4a7965SKim Phillips case DESC_HDR_SEL0_PKEU: 4489c4a7965SKim Phillips dev_err(dev, "PKEUISR 0x%08x_%08x\n", 4499c4a7965SKim Phillips in_be32(priv->reg + TALITOS_PKEUISR), 4509c4a7965SKim Phillips in_be32(priv->reg + TALITOS_PKEUISR_LO)); 4519c4a7965SKim Phillips break; 4529c4a7965SKim Phillips case DESC_HDR_SEL0_AESU: 4539c4a7965SKim Phillips dev_err(dev, "AESUISR 0x%08x_%08x\n", 4549c4a7965SKim Phillips in_be32(priv->reg + TALITOS_AESUISR), 4559c4a7965SKim Phillips in_be32(priv->reg + TALITOS_AESUISR_LO)); 4569c4a7965SKim Phillips break; 4579c4a7965SKim Phillips case DESC_HDR_SEL0_CRCU: 4589c4a7965SKim Phillips dev_err(dev, "CRCUISR 0x%08x_%08x\n", 4599c4a7965SKim Phillips in_be32(priv->reg + TALITOS_CRCUISR), 4609c4a7965SKim Phillips in_be32(priv->reg + TALITOS_CRCUISR_LO)); 4619c4a7965SKim Phillips break; 4629c4a7965SKim Phillips case DESC_HDR_SEL0_KEU: 4639c4a7965SKim Phillips dev_err(dev, "KEUISR 0x%08x_%08x\n", 4649c4a7965SKim Phillips in_be32(priv->reg + TALITOS_KEUISR), 4659c4a7965SKim Phillips in_be32(priv->reg + TALITOS_KEUISR_LO)); 4669c4a7965SKim Phillips break; 4679c4a7965SKim Phillips } 4689c4a7965SKim Phillips 4699c4a7965SKim Phillips switch (desc->hdr & DESC_HDR_SEL1_MASK) { 4709c4a7965SKim Phillips case DESC_HDR_SEL1_MDEUA: 4719c4a7965SKim Phillips case DESC_HDR_SEL1_MDEUB: 4729c4a7965SKim Phillips dev_err(dev, "MDEUISR 0x%08x_%08x\n", 4739c4a7965SKim Phillips in_be32(priv->reg + TALITOS_MDEUISR), 4749c4a7965SKim Phillips in_be32(priv->reg + TALITOS_MDEUISR_LO)); 4759c4a7965SKim Phillips break; 4769c4a7965SKim Phillips case DESC_HDR_SEL1_CRCU: 4779c4a7965SKim Phillips dev_err(dev, "CRCUISR 0x%08x_%08x\n", 4789c4a7965SKim Phillips in_be32(priv->reg + TALITOS_CRCUISR), 4799c4a7965SKim Phillips in_be32(priv->reg + TALITOS_CRCUISR_LO)); 4809c4a7965SKim Phillips break; 4819c4a7965SKim Phillips } 4829c4a7965SKim Phillips 4839c4a7965SKim Phillips for (i = 0; i < 8; i++) 4849c4a7965SKim Phillips dev_err(dev, "DESCBUF 0x%08x_%08x\n", 4859c4a7965SKim Phillips in_be32(priv->reg + TALITOS_DESCBUF(ch) + 8*i), 4869c4a7965SKim Phillips in_be32(priv->reg + TALITOS_DESCBUF_LO(ch) + 8*i)); 4879c4a7965SKim Phillips } 4889c4a7965SKim Phillips 4899c4a7965SKim Phillips /* 4909c4a7965SKim Phillips * recover from error interrupts 4919c4a7965SKim Phillips */ 49240405f10SKim Phillips static void talitos_error(unsigned long data, u32 isr, u32 isr_lo) 4939c4a7965SKim Phillips { 4949c4a7965SKim Phillips struct device *dev = (struct device *)data; 4959c4a7965SKim Phillips struct talitos_private *priv = dev_get_drvdata(dev); 4969c4a7965SKim Phillips unsigned int timeout = TALITOS_TIMEOUT; 4979c4a7965SKim Phillips int ch, error, reset_dev = 0, reset_ch = 0; 49840405f10SKim Phillips u32 v, v_lo; 4999c4a7965SKim Phillips 5009c4a7965SKim Phillips for (ch = 0; ch < priv->num_channels; ch++) { 5019c4a7965SKim Phillips /* skip channels without errors */ 5029c4a7965SKim Phillips if (!(isr & (1 << (ch * 2 + 1)))) 5039c4a7965SKim Phillips continue; 5049c4a7965SKim Phillips 5059c4a7965SKim Phillips error = -EINVAL; 5069c4a7965SKim Phillips 5079c4a7965SKim Phillips v = in_be32(priv->reg + TALITOS_CCPSR(ch)); 5089c4a7965SKim Phillips v_lo = in_be32(priv->reg + TALITOS_CCPSR_LO(ch)); 5099c4a7965SKim Phillips 5109c4a7965SKim Phillips if (v_lo & TALITOS_CCPSR_LO_DOF) { 5119c4a7965SKim Phillips dev_err(dev, "double fetch fifo overflow error\n"); 5129c4a7965SKim Phillips error = -EAGAIN; 5139c4a7965SKim Phillips reset_ch = 1; 5149c4a7965SKim Phillips } 5159c4a7965SKim Phillips if (v_lo & TALITOS_CCPSR_LO_SOF) { 5169c4a7965SKim Phillips /* h/w dropped descriptor */ 5179c4a7965SKim Phillips dev_err(dev, "single fetch fifo overflow error\n"); 5189c4a7965SKim Phillips error = -EAGAIN; 5199c4a7965SKim Phillips } 5209c4a7965SKim Phillips if (v_lo & TALITOS_CCPSR_LO_MDTE) 5219c4a7965SKim Phillips dev_err(dev, "master data transfer error\n"); 5229c4a7965SKim Phillips if (v_lo & TALITOS_CCPSR_LO_SGDLZ) 5239c4a7965SKim Phillips dev_err(dev, "s/g data length zero error\n"); 5249c4a7965SKim Phillips if (v_lo & TALITOS_CCPSR_LO_FPZ) 5259c4a7965SKim Phillips dev_err(dev, "fetch pointer zero error\n"); 5269c4a7965SKim Phillips if (v_lo & TALITOS_CCPSR_LO_IDH) 5279c4a7965SKim Phillips dev_err(dev, "illegal descriptor header error\n"); 5289c4a7965SKim Phillips if (v_lo & TALITOS_CCPSR_LO_IEU) 5299c4a7965SKim Phillips dev_err(dev, "invalid execution unit error\n"); 5309c4a7965SKim Phillips if (v_lo & TALITOS_CCPSR_LO_EU) 5319c4a7965SKim Phillips report_eu_error(dev, ch, current_desc(dev, ch)); 5329c4a7965SKim Phillips if (v_lo & TALITOS_CCPSR_LO_GB) 5339c4a7965SKim Phillips dev_err(dev, "gather boundary error\n"); 5349c4a7965SKim Phillips if (v_lo & TALITOS_CCPSR_LO_GRL) 5359c4a7965SKim Phillips dev_err(dev, "gather return/length error\n"); 5369c4a7965SKim Phillips if (v_lo & TALITOS_CCPSR_LO_SB) 5379c4a7965SKim Phillips dev_err(dev, "scatter boundary error\n"); 5389c4a7965SKim Phillips if (v_lo & TALITOS_CCPSR_LO_SRL) 5399c4a7965SKim Phillips dev_err(dev, "scatter return/length error\n"); 5409c4a7965SKim Phillips 5419c4a7965SKim Phillips flush_channel(dev, ch, error, reset_ch); 5429c4a7965SKim Phillips 5439c4a7965SKim Phillips if (reset_ch) { 5449c4a7965SKim Phillips reset_channel(dev, ch); 5459c4a7965SKim Phillips } else { 5469c4a7965SKim Phillips setbits32(priv->reg + TALITOS_CCCR(ch), 5479c4a7965SKim Phillips TALITOS_CCCR_CONT); 5489c4a7965SKim Phillips setbits32(priv->reg + TALITOS_CCCR_LO(ch), 0); 5499c4a7965SKim Phillips while ((in_be32(priv->reg + TALITOS_CCCR(ch)) & 5509c4a7965SKim Phillips TALITOS_CCCR_CONT) && --timeout) 5519c4a7965SKim Phillips cpu_relax(); 5529c4a7965SKim Phillips if (timeout == 0) { 5539c4a7965SKim Phillips dev_err(dev, "failed to restart channel %d\n", 5549c4a7965SKim Phillips ch); 5559c4a7965SKim Phillips reset_dev = 1; 5569c4a7965SKim Phillips } 5579c4a7965SKim Phillips } 5589c4a7965SKim Phillips } 5599c4a7965SKim Phillips if (reset_dev || isr & ~TALITOS_ISR_CHERR || isr_lo) { 5609c4a7965SKim Phillips dev_err(dev, "done overflow, internal time out, or rngu error: " 5619c4a7965SKim Phillips "ISR 0x%08x_%08x\n", isr, isr_lo); 5629c4a7965SKim Phillips 5639c4a7965SKim Phillips /* purge request queues */ 5649c4a7965SKim Phillips for (ch = 0; ch < priv->num_channels; ch++) 5659c4a7965SKim Phillips flush_channel(dev, ch, -EIO, 1); 5669c4a7965SKim Phillips 5679c4a7965SKim Phillips /* reset and reinitialize the device */ 5689c4a7965SKim Phillips init_device(dev); 5699c4a7965SKim Phillips } 5709c4a7965SKim Phillips } 5719c4a7965SKim Phillips 5729c4a7965SKim Phillips static irqreturn_t talitos_interrupt(int irq, void *data) 5739c4a7965SKim Phillips { 5749c4a7965SKim Phillips struct device *dev = data; 5759c4a7965SKim Phillips struct talitos_private *priv = dev_get_drvdata(dev); 5769c4a7965SKim Phillips u32 isr, isr_lo; 5779c4a7965SKim Phillips 5789c4a7965SKim Phillips isr = in_be32(priv->reg + TALITOS_ISR); 5799c4a7965SKim Phillips isr_lo = in_be32(priv->reg + TALITOS_ISR_LO); 580ca38a814SLee Nipper /* Acknowledge interrupt */ 5819c4a7965SKim Phillips out_be32(priv->reg + TALITOS_ICR, isr); 5829c4a7965SKim Phillips out_be32(priv->reg + TALITOS_ICR_LO, isr_lo); 5839c4a7965SKim Phillips 584ca38a814SLee Nipper if (unlikely((isr & ~TALITOS_ISR_CHDONE) || isr_lo)) 58540405f10SKim Phillips talitos_error((unsigned long)data, isr, isr_lo); 586ca38a814SLee Nipper else 5871c2e8811SLee Nipper if (likely(isr & TALITOS_ISR_CHDONE)) { 5881c2e8811SLee Nipper /* mask further done interrupts. */ 5891c2e8811SLee Nipper clrbits32(priv->reg + TALITOS_IMR, TALITOS_IMR_DONE); 5901c2e8811SLee Nipper /* done_task will unmask done interrupts at exit */ 5919c4a7965SKim Phillips tasklet_schedule(&priv->done_task); 5921c2e8811SLee Nipper } 5939c4a7965SKim Phillips 5949c4a7965SKim Phillips return (isr || isr_lo) ? IRQ_HANDLED : IRQ_NONE; 5959c4a7965SKim Phillips } 5969c4a7965SKim Phillips 5979c4a7965SKim Phillips /* 5989c4a7965SKim Phillips * hwrng 5999c4a7965SKim Phillips */ 6009c4a7965SKim Phillips static int talitos_rng_data_present(struct hwrng *rng, int wait) 6019c4a7965SKim Phillips { 6029c4a7965SKim Phillips struct device *dev = (struct device *)rng->priv; 6039c4a7965SKim Phillips struct talitos_private *priv = dev_get_drvdata(dev); 6049c4a7965SKim Phillips u32 ofl; 6059c4a7965SKim Phillips int i; 6069c4a7965SKim Phillips 6079c4a7965SKim Phillips for (i = 0; i < 20; i++) { 6089c4a7965SKim Phillips ofl = in_be32(priv->reg + TALITOS_RNGUSR_LO) & 6099c4a7965SKim Phillips TALITOS_RNGUSR_LO_OFL; 6109c4a7965SKim Phillips if (ofl || !wait) 6119c4a7965SKim Phillips break; 6129c4a7965SKim Phillips udelay(10); 6139c4a7965SKim Phillips } 6149c4a7965SKim Phillips 6159c4a7965SKim Phillips return !!ofl; 6169c4a7965SKim Phillips } 6179c4a7965SKim Phillips 6189c4a7965SKim Phillips static int talitos_rng_data_read(struct hwrng *rng, u32 *data) 6199c4a7965SKim Phillips { 6209c4a7965SKim Phillips struct device *dev = (struct device *)rng->priv; 6219c4a7965SKim Phillips struct talitos_private *priv = dev_get_drvdata(dev); 6229c4a7965SKim Phillips 6239c4a7965SKim Phillips /* rng fifo requires 64-bit accesses */ 6249c4a7965SKim Phillips *data = in_be32(priv->reg + TALITOS_RNGU_FIFO); 6259c4a7965SKim Phillips *data = in_be32(priv->reg + TALITOS_RNGU_FIFO_LO); 6269c4a7965SKim Phillips 6279c4a7965SKim Phillips return sizeof(u32); 6289c4a7965SKim Phillips } 6299c4a7965SKim Phillips 6309c4a7965SKim Phillips static int talitos_rng_init(struct hwrng *rng) 6319c4a7965SKim Phillips { 6329c4a7965SKim Phillips struct device *dev = (struct device *)rng->priv; 6339c4a7965SKim Phillips struct talitos_private *priv = dev_get_drvdata(dev); 6349c4a7965SKim Phillips unsigned int timeout = TALITOS_TIMEOUT; 6359c4a7965SKim Phillips 6369c4a7965SKim Phillips setbits32(priv->reg + TALITOS_RNGURCR_LO, TALITOS_RNGURCR_LO_SR); 6379c4a7965SKim Phillips while (!(in_be32(priv->reg + TALITOS_RNGUSR_LO) & TALITOS_RNGUSR_LO_RD) 6389c4a7965SKim Phillips && --timeout) 6399c4a7965SKim Phillips cpu_relax(); 6409c4a7965SKim Phillips if (timeout == 0) { 6419c4a7965SKim Phillips dev_err(dev, "failed to reset rng hw\n"); 6429c4a7965SKim Phillips return -ENODEV; 6439c4a7965SKim Phillips } 6449c4a7965SKim Phillips 6459c4a7965SKim Phillips /* start generating */ 6469c4a7965SKim Phillips setbits32(priv->reg + TALITOS_RNGUDSR_LO, 0); 6479c4a7965SKim Phillips 6489c4a7965SKim Phillips return 0; 6499c4a7965SKim Phillips } 6509c4a7965SKim Phillips 6519c4a7965SKim Phillips static int talitos_register_rng(struct device *dev) 6529c4a7965SKim Phillips { 6539c4a7965SKim Phillips struct talitos_private *priv = dev_get_drvdata(dev); 6549c4a7965SKim Phillips 6559c4a7965SKim Phillips priv->rng.name = dev_driver_string(dev), 6569c4a7965SKim Phillips priv->rng.init = talitos_rng_init, 6579c4a7965SKim Phillips priv->rng.data_present = talitos_rng_data_present, 6589c4a7965SKim Phillips priv->rng.data_read = talitos_rng_data_read, 6599c4a7965SKim Phillips priv->rng.priv = (unsigned long)dev; 6609c4a7965SKim Phillips 6619c4a7965SKim Phillips return hwrng_register(&priv->rng); 6629c4a7965SKim Phillips } 6639c4a7965SKim Phillips 6649c4a7965SKim Phillips static void talitos_unregister_rng(struct device *dev) 6659c4a7965SKim Phillips { 6669c4a7965SKim Phillips struct talitos_private *priv = dev_get_drvdata(dev); 6679c4a7965SKim Phillips 6689c4a7965SKim Phillips hwrng_unregister(&priv->rng); 6699c4a7965SKim Phillips } 6709c4a7965SKim Phillips 6719c4a7965SKim Phillips /* 6729c4a7965SKim Phillips * crypto alg 6739c4a7965SKim Phillips */ 6749c4a7965SKim Phillips #define TALITOS_CRA_PRIORITY 3000 6759c4a7965SKim Phillips #define TALITOS_MAX_KEY_SIZE 64 6763952f17eSLee Nipper #define TALITOS_MAX_IV_LENGTH 16 /* max of AES_BLOCK_SIZE, DES3_EDE_BLOCK_SIZE */ 67770bcaca7SLee Nipper 6783952f17eSLee Nipper #define MD5_DIGEST_SIZE 16 6799c4a7965SKim Phillips 6809c4a7965SKim Phillips struct talitos_ctx { 6819c4a7965SKim Phillips struct device *dev; 6829c4a7965SKim Phillips __be32 desc_hdr_template; 6839c4a7965SKim Phillips u8 key[TALITOS_MAX_KEY_SIZE]; 68470bcaca7SLee Nipper u8 iv[TALITOS_MAX_IV_LENGTH]; 6859c4a7965SKim Phillips unsigned int keylen; 6869c4a7965SKim Phillips unsigned int enckeylen; 6879c4a7965SKim Phillips unsigned int authkeylen; 6889c4a7965SKim Phillips unsigned int authsize; 6899c4a7965SKim Phillips }; 6909c4a7965SKim Phillips 69156af8cd4SLee Nipper static int aead_setauthsize(struct crypto_aead *authenc, 6929c4a7965SKim Phillips unsigned int authsize) 6939c4a7965SKim Phillips { 6949c4a7965SKim Phillips struct talitos_ctx *ctx = crypto_aead_ctx(authenc); 6959c4a7965SKim Phillips 6969c4a7965SKim Phillips ctx->authsize = authsize; 6979c4a7965SKim Phillips 6989c4a7965SKim Phillips return 0; 6999c4a7965SKim Phillips } 7009c4a7965SKim Phillips 70156af8cd4SLee Nipper static int aead_setkey(struct crypto_aead *authenc, 7029c4a7965SKim Phillips const u8 *key, unsigned int keylen) 7039c4a7965SKim Phillips { 7049c4a7965SKim Phillips struct talitos_ctx *ctx = crypto_aead_ctx(authenc); 7059c4a7965SKim Phillips struct rtattr *rta = (void *)key; 7069c4a7965SKim Phillips struct crypto_authenc_key_param *param; 7079c4a7965SKim Phillips unsigned int authkeylen; 7089c4a7965SKim Phillips unsigned int enckeylen; 7099c4a7965SKim Phillips 7109c4a7965SKim Phillips if (!RTA_OK(rta, keylen)) 7119c4a7965SKim Phillips goto badkey; 7129c4a7965SKim Phillips 7139c4a7965SKim Phillips if (rta->rta_type != CRYPTO_AUTHENC_KEYA_PARAM) 7149c4a7965SKim Phillips goto badkey; 7159c4a7965SKim Phillips 7169c4a7965SKim Phillips if (RTA_PAYLOAD(rta) < sizeof(*param)) 7179c4a7965SKim Phillips goto badkey; 7189c4a7965SKim Phillips 7199c4a7965SKim Phillips param = RTA_DATA(rta); 7209c4a7965SKim Phillips enckeylen = be32_to_cpu(param->enckeylen); 7219c4a7965SKim Phillips 7229c4a7965SKim Phillips key += RTA_ALIGN(rta->rta_len); 7239c4a7965SKim Phillips keylen -= RTA_ALIGN(rta->rta_len); 7249c4a7965SKim Phillips 7259c4a7965SKim Phillips if (keylen < enckeylen) 7269c4a7965SKim Phillips goto badkey; 7279c4a7965SKim Phillips 7289c4a7965SKim Phillips authkeylen = keylen - enckeylen; 7299c4a7965SKim Phillips 7309c4a7965SKim Phillips if (keylen > TALITOS_MAX_KEY_SIZE) 7319c4a7965SKim Phillips goto badkey; 7329c4a7965SKim Phillips 7339c4a7965SKim Phillips memcpy(&ctx->key, key, keylen); 7349c4a7965SKim Phillips 7359c4a7965SKim Phillips ctx->keylen = keylen; 7369c4a7965SKim Phillips ctx->enckeylen = enckeylen; 7379c4a7965SKim Phillips ctx->authkeylen = authkeylen; 7389c4a7965SKim Phillips 7399c4a7965SKim Phillips return 0; 7409c4a7965SKim Phillips 7419c4a7965SKim Phillips badkey: 7429c4a7965SKim Phillips crypto_aead_set_flags(authenc, CRYPTO_TFM_RES_BAD_KEY_LEN); 7439c4a7965SKim Phillips return -EINVAL; 7449c4a7965SKim Phillips } 7459c4a7965SKim Phillips 7469c4a7965SKim Phillips /* 74756af8cd4SLee Nipper * talitos_edesc - s/w-extended descriptor 7489c4a7965SKim Phillips * @src_nents: number of segments in input scatterlist 7499c4a7965SKim Phillips * @dst_nents: number of segments in output scatterlist 7509c4a7965SKim Phillips * @dma_len: length of dma mapped link_tbl space 7519c4a7965SKim Phillips * @dma_link_tbl: bus physical address of link_tbl 7529c4a7965SKim Phillips * @desc: h/w descriptor 7539c4a7965SKim Phillips * @link_tbl: input and output h/w link tables (if {src,dst}_nents > 1) 7549c4a7965SKim Phillips * 7559c4a7965SKim Phillips * if decrypting (with authcheck), or either one of src_nents or dst_nents 7569c4a7965SKim Phillips * is greater than 1, an integrity check value is concatenated to the end 7579c4a7965SKim Phillips * of link_tbl data 7589c4a7965SKim Phillips */ 75956af8cd4SLee Nipper struct talitos_edesc { 7609c4a7965SKim Phillips int src_nents; 7619c4a7965SKim Phillips int dst_nents; 7624de9d0b5SLee Nipper int src_is_chained; 7634de9d0b5SLee Nipper int dst_is_chained; 7649c4a7965SKim Phillips int dma_len; 7659c4a7965SKim Phillips dma_addr_t dma_link_tbl; 7669c4a7965SKim Phillips struct talitos_desc desc; 7679c4a7965SKim Phillips struct talitos_ptr link_tbl[0]; 7689c4a7965SKim Phillips }; 7699c4a7965SKim Phillips 7704de9d0b5SLee Nipper static int talitos_map_sg(struct device *dev, struct scatterlist *sg, 7714de9d0b5SLee Nipper unsigned int nents, enum dma_data_direction dir, 7724de9d0b5SLee Nipper int chained) 7734de9d0b5SLee Nipper { 7744de9d0b5SLee Nipper if (unlikely(chained)) 7754de9d0b5SLee Nipper while (sg) { 7764de9d0b5SLee Nipper dma_map_sg(dev, sg, 1, dir); 7774de9d0b5SLee Nipper sg = scatterwalk_sg_next(sg); 7784de9d0b5SLee Nipper } 7794de9d0b5SLee Nipper else 7804de9d0b5SLee Nipper dma_map_sg(dev, sg, nents, dir); 7814de9d0b5SLee Nipper return nents; 7824de9d0b5SLee Nipper } 7834de9d0b5SLee Nipper 7844de9d0b5SLee Nipper static void talitos_unmap_sg_chain(struct device *dev, struct scatterlist *sg, 7854de9d0b5SLee Nipper enum dma_data_direction dir) 7864de9d0b5SLee Nipper { 7874de9d0b5SLee Nipper while (sg) { 7884de9d0b5SLee Nipper dma_unmap_sg(dev, sg, 1, dir); 7894de9d0b5SLee Nipper sg = scatterwalk_sg_next(sg); 7904de9d0b5SLee Nipper } 7914de9d0b5SLee Nipper } 7924de9d0b5SLee Nipper 7934de9d0b5SLee Nipper static void talitos_sg_unmap(struct device *dev, 7944de9d0b5SLee Nipper struct talitos_edesc *edesc, 7954de9d0b5SLee Nipper struct scatterlist *src, 7964de9d0b5SLee Nipper struct scatterlist *dst) 7974de9d0b5SLee Nipper { 7984de9d0b5SLee Nipper unsigned int src_nents = edesc->src_nents ? : 1; 7994de9d0b5SLee Nipper unsigned int dst_nents = edesc->dst_nents ? : 1; 8004de9d0b5SLee Nipper 8014de9d0b5SLee Nipper if (src != dst) { 8024de9d0b5SLee Nipper if (edesc->src_is_chained) 8034de9d0b5SLee Nipper talitos_unmap_sg_chain(dev, src, DMA_TO_DEVICE); 8044de9d0b5SLee Nipper else 8054de9d0b5SLee Nipper dma_unmap_sg(dev, src, src_nents, DMA_TO_DEVICE); 8064de9d0b5SLee Nipper 8074de9d0b5SLee Nipper if (edesc->dst_is_chained) 8084de9d0b5SLee Nipper talitos_unmap_sg_chain(dev, dst, DMA_FROM_DEVICE); 8094de9d0b5SLee Nipper else 8104de9d0b5SLee Nipper dma_unmap_sg(dev, dst, dst_nents, DMA_FROM_DEVICE); 8114de9d0b5SLee Nipper } else 8124de9d0b5SLee Nipper if (edesc->src_is_chained) 8134de9d0b5SLee Nipper talitos_unmap_sg_chain(dev, src, DMA_BIDIRECTIONAL); 8144de9d0b5SLee Nipper else 8154de9d0b5SLee Nipper dma_unmap_sg(dev, src, src_nents, DMA_BIDIRECTIONAL); 8164de9d0b5SLee Nipper } 8174de9d0b5SLee Nipper 8189c4a7965SKim Phillips static void ipsec_esp_unmap(struct device *dev, 81956af8cd4SLee Nipper struct talitos_edesc *edesc, 8209c4a7965SKim Phillips struct aead_request *areq) 8219c4a7965SKim Phillips { 8229c4a7965SKim Phillips unmap_single_talitos_ptr(dev, &edesc->desc.ptr[6], DMA_FROM_DEVICE); 8239c4a7965SKim Phillips unmap_single_talitos_ptr(dev, &edesc->desc.ptr[3], DMA_TO_DEVICE); 8249c4a7965SKim Phillips unmap_single_talitos_ptr(dev, &edesc->desc.ptr[2], DMA_TO_DEVICE); 8259c4a7965SKim Phillips unmap_single_talitos_ptr(dev, &edesc->desc.ptr[0], DMA_TO_DEVICE); 8269c4a7965SKim Phillips 8279c4a7965SKim Phillips dma_unmap_sg(dev, areq->assoc, 1, DMA_TO_DEVICE); 8289c4a7965SKim Phillips 8294de9d0b5SLee Nipper talitos_sg_unmap(dev, edesc, areq->src, areq->dst); 8309c4a7965SKim Phillips 8319c4a7965SKim Phillips if (edesc->dma_len) 8329c4a7965SKim Phillips dma_unmap_single(dev, edesc->dma_link_tbl, edesc->dma_len, 8339c4a7965SKim Phillips DMA_BIDIRECTIONAL); 8349c4a7965SKim Phillips } 8359c4a7965SKim Phillips 8369c4a7965SKim Phillips /* 8379c4a7965SKim Phillips * ipsec_esp descriptor callbacks 8389c4a7965SKim Phillips */ 8399c4a7965SKim Phillips static void ipsec_esp_encrypt_done(struct device *dev, 8409c4a7965SKim Phillips struct talitos_desc *desc, void *context, 8419c4a7965SKim Phillips int err) 8429c4a7965SKim Phillips { 8439c4a7965SKim Phillips struct aead_request *areq = context; 84456af8cd4SLee Nipper struct talitos_edesc *edesc = 84556af8cd4SLee Nipper container_of(desc, struct talitos_edesc, desc); 8469c4a7965SKim Phillips struct crypto_aead *authenc = crypto_aead_reqtfm(areq); 8479c4a7965SKim Phillips struct talitos_ctx *ctx = crypto_aead_ctx(authenc); 8489c4a7965SKim Phillips struct scatterlist *sg; 8499c4a7965SKim Phillips void *icvdata; 8509c4a7965SKim Phillips 8519c4a7965SKim Phillips ipsec_esp_unmap(dev, edesc, areq); 8529c4a7965SKim Phillips 8539c4a7965SKim Phillips /* copy the generated ICV to dst */ 8549c4a7965SKim Phillips if (edesc->dma_len) { 8559c4a7965SKim Phillips icvdata = &edesc->link_tbl[edesc->src_nents + 856f3c85bc1SLee Nipper edesc->dst_nents + 2]; 8579c4a7965SKim Phillips sg = sg_last(areq->dst, edesc->dst_nents); 8589c4a7965SKim Phillips memcpy((char *)sg_virt(sg) + sg->length - ctx->authsize, 8599c4a7965SKim Phillips icvdata, ctx->authsize); 8609c4a7965SKim Phillips } 8619c4a7965SKim Phillips 8629c4a7965SKim Phillips kfree(edesc); 8639c4a7965SKim Phillips 8649c4a7965SKim Phillips aead_request_complete(areq, err); 8659c4a7965SKim Phillips } 8669c4a7965SKim Phillips 867fe5720e2SKim Phillips static void ipsec_esp_decrypt_swauth_done(struct device *dev, 868*e938e465SKim Phillips struct talitos_desc *desc, 869*e938e465SKim Phillips void *context, int err) 8709c4a7965SKim Phillips { 8719c4a7965SKim Phillips struct aead_request *req = context; 87256af8cd4SLee Nipper struct talitos_edesc *edesc = 87356af8cd4SLee Nipper container_of(desc, struct talitos_edesc, desc); 8749c4a7965SKim Phillips struct crypto_aead *authenc = crypto_aead_reqtfm(req); 8759c4a7965SKim Phillips struct talitos_ctx *ctx = crypto_aead_ctx(authenc); 8769c4a7965SKim Phillips struct scatterlist *sg; 8779c4a7965SKim Phillips void *icvdata; 8789c4a7965SKim Phillips 8799c4a7965SKim Phillips ipsec_esp_unmap(dev, edesc, req); 8809c4a7965SKim Phillips 8819c4a7965SKim Phillips if (!err) { 8829c4a7965SKim Phillips /* auth check */ 8839c4a7965SKim Phillips if (edesc->dma_len) 8849c4a7965SKim Phillips icvdata = &edesc->link_tbl[edesc->src_nents + 885f3c85bc1SLee Nipper edesc->dst_nents + 2]; 8869c4a7965SKim Phillips else 8879c4a7965SKim Phillips icvdata = &edesc->link_tbl[0]; 8889c4a7965SKim Phillips 8899c4a7965SKim Phillips sg = sg_last(req->dst, edesc->dst_nents ? : 1); 8909c4a7965SKim Phillips err = memcmp(icvdata, (char *)sg_virt(sg) + sg->length - 8919c4a7965SKim Phillips ctx->authsize, ctx->authsize) ? -EBADMSG : 0; 8929c4a7965SKim Phillips } 8939c4a7965SKim Phillips 8949c4a7965SKim Phillips kfree(edesc); 8959c4a7965SKim Phillips 8969c4a7965SKim Phillips aead_request_complete(req, err); 8979c4a7965SKim Phillips } 8989c4a7965SKim Phillips 899fe5720e2SKim Phillips static void ipsec_esp_decrypt_hwauth_done(struct device *dev, 900*e938e465SKim Phillips struct talitos_desc *desc, 901*e938e465SKim Phillips void *context, int err) 902fe5720e2SKim Phillips { 903fe5720e2SKim Phillips struct aead_request *req = context; 90456af8cd4SLee Nipper struct talitos_edesc *edesc = 90556af8cd4SLee Nipper container_of(desc, struct talitos_edesc, desc); 906fe5720e2SKim Phillips 907fe5720e2SKim Phillips ipsec_esp_unmap(dev, edesc, req); 908fe5720e2SKim Phillips 909fe5720e2SKim Phillips /* check ICV auth status */ 910*e938e465SKim Phillips if (!err && ((desc->hdr_lo & DESC_HDR_LO_ICCR1_MASK) != 911*e938e465SKim Phillips DESC_HDR_LO_ICCR1_PASS)) 912fe5720e2SKim Phillips err = -EBADMSG; 913fe5720e2SKim Phillips 914fe5720e2SKim Phillips kfree(edesc); 915fe5720e2SKim Phillips 916fe5720e2SKim Phillips aead_request_complete(req, err); 917fe5720e2SKim Phillips } 918fe5720e2SKim Phillips 9199c4a7965SKim Phillips /* 9209c4a7965SKim Phillips * convert scatterlist to SEC h/w link table format 9219c4a7965SKim Phillips * stop at cryptlen bytes 9229c4a7965SKim Phillips */ 92370bcaca7SLee Nipper static int sg_to_link_tbl(struct scatterlist *sg, int sg_count, 9249c4a7965SKim Phillips int cryptlen, struct talitos_ptr *link_tbl_ptr) 9259c4a7965SKim Phillips { 92670bcaca7SLee Nipper int n_sg = sg_count; 92770bcaca7SLee Nipper 92870bcaca7SLee Nipper while (n_sg--) { 9299c4a7965SKim Phillips link_tbl_ptr->ptr = cpu_to_be32(sg_dma_address(sg)); 9309c4a7965SKim Phillips link_tbl_ptr->len = cpu_to_be16(sg_dma_len(sg)); 9319c4a7965SKim Phillips link_tbl_ptr->j_extent = 0; 9329c4a7965SKim Phillips link_tbl_ptr++; 9339c4a7965SKim Phillips cryptlen -= sg_dma_len(sg); 9344de9d0b5SLee Nipper sg = scatterwalk_sg_next(sg); 9359c4a7965SKim Phillips } 9369c4a7965SKim Phillips 93770bcaca7SLee Nipper /* adjust (decrease) last one (or two) entry's len to cryptlen */ 9389c4a7965SKim Phillips link_tbl_ptr--; 939c0e741d4SKim Phillips while (be16_to_cpu(link_tbl_ptr->len) <= (-cryptlen)) { 94070bcaca7SLee Nipper /* Empty this entry, and move to previous one */ 94170bcaca7SLee Nipper cryptlen += be16_to_cpu(link_tbl_ptr->len); 94270bcaca7SLee Nipper link_tbl_ptr->len = 0; 94370bcaca7SLee Nipper sg_count--; 94470bcaca7SLee Nipper link_tbl_ptr--; 94570bcaca7SLee Nipper } 9469c4a7965SKim Phillips link_tbl_ptr->len = cpu_to_be16(be16_to_cpu(link_tbl_ptr->len) 9479c4a7965SKim Phillips + cryptlen); 9489c4a7965SKim Phillips 9499c4a7965SKim Phillips /* tag end of link table */ 9509c4a7965SKim Phillips link_tbl_ptr->j_extent = DESC_PTR_LNKTBL_RETURN; 95170bcaca7SLee Nipper 95270bcaca7SLee Nipper return sg_count; 9539c4a7965SKim Phillips } 9549c4a7965SKim Phillips 9559c4a7965SKim Phillips /* 9569c4a7965SKim Phillips * fill in and submit ipsec_esp descriptor 9579c4a7965SKim Phillips */ 95856af8cd4SLee Nipper static int ipsec_esp(struct talitos_edesc *edesc, struct aead_request *areq, 9599c4a7965SKim Phillips u8 *giv, u64 seq, 9609c4a7965SKim Phillips void (*callback) (struct device *dev, 9619c4a7965SKim Phillips struct talitos_desc *desc, 9629c4a7965SKim Phillips void *context, int error)) 9639c4a7965SKim Phillips { 9649c4a7965SKim Phillips struct crypto_aead *aead = crypto_aead_reqtfm(areq); 9659c4a7965SKim Phillips struct talitos_ctx *ctx = crypto_aead_ctx(aead); 9669c4a7965SKim Phillips struct device *dev = ctx->dev; 9679c4a7965SKim Phillips struct talitos_desc *desc = &edesc->desc; 9689c4a7965SKim Phillips unsigned int cryptlen = areq->cryptlen; 9699c4a7965SKim Phillips unsigned int authsize = ctx->authsize; 9709c4a7965SKim Phillips unsigned int ivsize; 971fa86a267SKim Phillips int sg_count, ret; 972fe5720e2SKim Phillips int sg_link_tbl_len; 9739c4a7965SKim Phillips 9749c4a7965SKim Phillips /* hmac key */ 9759c4a7965SKim Phillips map_single_talitos_ptr(dev, &desc->ptr[0], ctx->authkeylen, &ctx->key, 9769c4a7965SKim Phillips 0, DMA_TO_DEVICE); 9779c4a7965SKim Phillips /* hmac data */ 9789c4a7965SKim Phillips map_single_talitos_ptr(dev, &desc->ptr[1], sg_virt(areq->src) - 9799c4a7965SKim Phillips sg_virt(areq->assoc), sg_virt(areq->assoc), 0, 9809c4a7965SKim Phillips DMA_TO_DEVICE); 9819c4a7965SKim Phillips /* cipher iv */ 9829c4a7965SKim Phillips ivsize = crypto_aead_ivsize(aead); 9839c4a7965SKim Phillips map_single_talitos_ptr(dev, &desc->ptr[2], ivsize, giv ?: areq->iv, 0, 9849c4a7965SKim Phillips DMA_TO_DEVICE); 9859c4a7965SKim Phillips 9869c4a7965SKim Phillips /* cipher key */ 9879c4a7965SKim Phillips map_single_talitos_ptr(dev, &desc->ptr[3], ctx->enckeylen, 9889c4a7965SKim Phillips (char *)&ctx->key + ctx->authkeylen, 0, 9899c4a7965SKim Phillips DMA_TO_DEVICE); 9909c4a7965SKim Phillips 9919c4a7965SKim Phillips /* 9929c4a7965SKim Phillips * cipher in 9939c4a7965SKim Phillips * map and adjust cipher len to aead request cryptlen. 9949c4a7965SKim Phillips * extent is bytes of HMAC postpended to ciphertext, 9959c4a7965SKim Phillips * typically 12 for ipsec 9969c4a7965SKim Phillips */ 9979c4a7965SKim Phillips desc->ptr[4].len = cpu_to_be16(cryptlen); 9989c4a7965SKim Phillips desc->ptr[4].j_extent = authsize; 9999c4a7965SKim Phillips 1000*e938e465SKim Phillips sg_count = talitos_map_sg(dev, areq->src, edesc->src_nents ? : 1, 1001*e938e465SKim Phillips (areq->src == areq->dst) ? DMA_BIDIRECTIONAL 1002*e938e465SKim Phillips : DMA_TO_DEVICE, 10034de9d0b5SLee Nipper edesc->src_is_chained); 10049c4a7965SKim Phillips 10059c4a7965SKim Phillips if (sg_count == 1) { 10069c4a7965SKim Phillips desc->ptr[4].ptr = cpu_to_be32(sg_dma_address(areq->src)); 10079c4a7965SKim Phillips } else { 1008fe5720e2SKim Phillips sg_link_tbl_len = cryptlen; 1009fe5720e2SKim Phillips 1010fe5720e2SKim Phillips if ((edesc->desc.hdr & DESC_HDR_MODE1_MDEU_CICV) && 1011*e938e465SKim Phillips (edesc->desc.hdr & DESC_HDR_MODE0_ENCRYPT) == 0) 1012fe5720e2SKim Phillips sg_link_tbl_len = cryptlen + authsize; 1013*e938e465SKim Phillips 1014fe5720e2SKim Phillips sg_count = sg_to_link_tbl(areq->src, sg_count, sg_link_tbl_len, 10159c4a7965SKim Phillips &edesc->link_tbl[0]); 101670bcaca7SLee Nipper if (sg_count > 1) { 10179c4a7965SKim Phillips desc->ptr[4].j_extent |= DESC_PTR_LNKTBL_JUMP; 10189c4a7965SKim Phillips desc->ptr[4].ptr = cpu_to_be32(edesc->dma_link_tbl); 1019*e938e465SKim Phillips dma_sync_single_for_device(dev, edesc->dma_link_tbl, 1020*e938e465SKim Phillips edesc->dma_len, 1021*e938e465SKim Phillips DMA_BIDIRECTIONAL); 102270bcaca7SLee Nipper } else { 102370bcaca7SLee Nipper /* Only one segment now, so no link tbl needed */ 1024*e938e465SKim Phillips desc->ptr[4].ptr = cpu_to_be32(sg_dma_address(areq-> 1025*e938e465SKim Phillips src)); 102670bcaca7SLee Nipper } 10279c4a7965SKim Phillips } 10289c4a7965SKim Phillips 10299c4a7965SKim Phillips /* cipher out */ 10309c4a7965SKim Phillips desc->ptr[5].len = cpu_to_be16(cryptlen); 10319c4a7965SKim Phillips desc->ptr[5].j_extent = authsize; 10329c4a7965SKim Phillips 1033*e938e465SKim Phillips if (areq->src != areq->dst) 10344de9d0b5SLee Nipper sg_count = talitos_map_sg(dev, areq->dst, 10354de9d0b5SLee Nipper edesc->dst_nents ? : 1, 10364de9d0b5SLee Nipper DMA_FROM_DEVICE, 10374de9d0b5SLee Nipper edesc->dst_is_chained); 10389c4a7965SKim Phillips 10399c4a7965SKim Phillips if (sg_count == 1) { 10409c4a7965SKim Phillips desc->ptr[5].ptr = cpu_to_be32(sg_dma_address(areq->dst)); 10419c4a7965SKim Phillips } else { 10429c4a7965SKim Phillips struct talitos_ptr *link_tbl_ptr = 1043f3c85bc1SLee Nipper &edesc->link_tbl[edesc->src_nents + 1]; 10449c4a7965SKim Phillips 10459c4a7965SKim Phillips desc->ptr[5].ptr = cpu_to_be32((struct talitos_ptr *) 10469c4a7965SKim Phillips edesc->dma_link_tbl + 1047f3c85bc1SLee Nipper edesc->src_nents + 1); 104870bcaca7SLee Nipper sg_count = sg_to_link_tbl(areq->dst, sg_count, cryptlen, 10499c4a7965SKim Phillips link_tbl_ptr); 1050fe5720e2SKim Phillips 1051f3c85bc1SLee Nipper /* Add an entry to the link table for ICV data */ 10529c4a7965SKim Phillips link_tbl_ptr += sg_count - 1; 10539c4a7965SKim Phillips link_tbl_ptr->j_extent = 0; 1054f3c85bc1SLee Nipper sg_count++; 10559c4a7965SKim Phillips link_tbl_ptr++; 10569c4a7965SKim Phillips link_tbl_ptr->j_extent = DESC_PTR_LNKTBL_RETURN; 10579c4a7965SKim Phillips link_tbl_ptr->len = cpu_to_be16(authsize); 10589c4a7965SKim Phillips 10599c4a7965SKim Phillips /* icv data follows link tables */ 10609c4a7965SKim Phillips link_tbl_ptr->ptr = cpu_to_be32((struct talitos_ptr *) 10619c4a7965SKim Phillips edesc->dma_link_tbl + 10629c4a7965SKim Phillips edesc->src_nents + 1063f3c85bc1SLee Nipper edesc->dst_nents + 2); 10649c4a7965SKim Phillips 10659c4a7965SKim Phillips desc->ptr[5].j_extent |= DESC_PTR_LNKTBL_JUMP; 10669c4a7965SKim Phillips dma_sync_single_for_device(ctx->dev, edesc->dma_link_tbl, 10679c4a7965SKim Phillips edesc->dma_len, DMA_BIDIRECTIONAL); 10689c4a7965SKim Phillips } 10699c4a7965SKim Phillips 10709c4a7965SKim Phillips /* iv out */ 10719c4a7965SKim Phillips map_single_talitos_ptr(dev, &desc->ptr[6], ivsize, ctx->iv, 0, 10729c4a7965SKim Phillips DMA_FROM_DEVICE); 10739c4a7965SKim Phillips 1074fa86a267SKim Phillips ret = talitos_submit(dev, desc, callback, areq); 1075fa86a267SKim Phillips if (ret != -EINPROGRESS) { 1076fa86a267SKim Phillips ipsec_esp_unmap(dev, edesc, areq); 1077fa86a267SKim Phillips kfree(edesc); 1078fa86a267SKim Phillips } 1079fa86a267SKim Phillips return ret; 10809c4a7965SKim Phillips } 10819c4a7965SKim Phillips 10829c4a7965SKim Phillips /* 10839c4a7965SKim Phillips * derive number of elements in scatterlist 10849c4a7965SKim Phillips */ 10854de9d0b5SLee Nipper static int sg_count(struct scatterlist *sg_list, int nbytes, int *chained) 10869c4a7965SKim Phillips { 10879c4a7965SKim Phillips struct scatterlist *sg = sg_list; 10889c4a7965SKim Phillips int sg_nents = 0; 10899c4a7965SKim Phillips 10904de9d0b5SLee Nipper *chained = 0; 10914de9d0b5SLee Nipper while (nbytes > 0) { 10929c4a7965SKim Phillips sg_nents++; 10939c4a7965SKim Phillips nbytes -= sg->length; 10944de9d0b5SLee Nipper if (!sg_is_last(sg) && (sg + 1)->length == 0) 10954de9d0b5SLee Nipper *chained = 1; 10964de9d0b5SLee Nipper sg = scatterwalk_sg_next(sg); 10979c4a7965SKim Phillips } 10989c4a7965SKim Phillips 10999c4a7965SKim Phillips return sg_nents; 11009c4a7965SKim Phillips } 11019c4a7965SKim Phillips 11029c4a7965SKim Phillips /* 110356af8cd4SLee Nipper * allocate and map the extended descriptor 11049c4a7965SKim Phillips */ 11054de9d0b5SLee Nipper static struct talitos_edesc *talitos_edesc_alloc(struct device *dev, 11064de9d0b5SLee Nipper struct scatterlist *src, 11074de9d0b5SLee Nipper struct scatterlist *dst, 11084de9d0b5SLee Nipper unsigned int cryptlen, 11094de9d0b5SLee Nipper unsigned int authsize, 11104de9d0b5SLee Nipper int icv_stashing, 11114de9d0b5SLee Nipper u32 cryptoflags) 11129c4a7965SKim Phillips { 111356af8cd4SLee Nipper struct talitos_edesc *edesc; 11149c4a7965SKim Phillips int src_nents, dst_nents, alloc_len, dma_len; 11154de9d0b5SLee Nipper int src_chained, dst_chained = 0; 11164de9d0b5SLee Nipper gfp_t flags = cryptoflags & CRYPTO_TFM_REQ_MAY_SLEEP ? GFP_KERNEL : 1117586725f8SKim Phillips GFP_ATOMIC; 11189c4a7965SKim Phillips 11194de9d0b5SLee Nipper if (cryptlen + authsize > TALITOS_MAX_DATA_LEN) { 11204de9d0b5SLee Nipper dev_err(dev, "length exceeds h/w max limit\n"); 11219c4a7965SKim Phillips return ERR_PTR(-EINVAL); 11229c4a7965SKim Phillips } 11239c4a7965SKim Phillips 11244de9d0b5SLee Nipper src_nents = sg_count(src, cryptlen + authsize, &src_chained); 11259c4a7965SKim Phillips src_nents = (src_nents == 1) ? 0 : src_nents; 11269c4a7965SKim Phillips 11274de9d0b5SLee Nipper if (dst == src) { 11289c4a7965SKim Phillips dst_nents = src_nents; 11299c4a7965SKim Phillips } else { 11304de9d0b5SLee Nipper dst_nents = sg_count(dst, cryptlen + authsize, &dst_chained); 1131695ad589SLee Nipper dst_nents = (dst_nents == 1) ? 0 : dst_nents; 11329c4a7965SKim Phillips } 11339c4a7965SKim Phillips 11349c4a7965SKim Phillips /* 11359c4a7965SKim Phillips * allocate space for base edesc plus the link tables, 1136f3c85bc1SLee Nipper * allowing for two separate entries for ICV and generated ICV (+ 2), 11379c4a7965SKim Phillips * and the ICV data itself 11389c4a7965SKim Phillips */ 113956af8cd4SLee Nipper alloc_len = sizeof(struct talitos_edesc); 11409c4a7965SKim Phillips if (src_nents || dst_nents) { 1141f3c85bc1SLee Nipper dma_len = (src_nents + dst_nents + 2) * 11424de9d0b5SLee Nipper sizeof(struct talitos_ptr) + authsize; 11439c4a7965SKim Phillips alloc_len += dma_len; 11449c4a7965SKim Phillips } else { 11459c4a7965SKim Phillips dma_len = 0; 11464de9d0b5SLee Nipper alloc_len += icv_stashing ? authsize : 0; 11479c4a7965SKim Phillips } 11489c4a7965SKim Phillips 1149586725f8SKim Phillips edesc = kmalloc(alloc_len, GFP_DMA | flags); 11509c4a7965SKim Phillips if (!edesc) { 11514de9d0b5SLee Nipper dev_err(dev, "could not allocate edescriptor\n"); 11529c4a7965SKim Phillips return ERR_PTR(-ENOMEM); 11539c4a7965SKim Phillips } 11549c4a7965SKim Phillips 11559c4a7965SKim Phillips edesc->src_nents = src_nents; 11569c4a7965SKim Phillips edesc->dst_nents = dst_nents; 11574de9d0b5SLee Nipper edesc->src_is_chained = src_chained; 11584de9d0b5SLee Nipper edesc->dst_is_chained = dst_chained; 11599c4a7965SKim Phillips edesc->dma_len = dma_len; 11604de9d0b5SLee Nipper edesc->dma_link_tbl = dma_map_single(dev, &edesc->link_tbl[0], 11619c4a7965SKim Phillips edesc->dma_len, DMA_BIDIRECTIONAL); 11629c4a7965SKim Phillips 11639c4a7965SKim Phillips return edesc; 11649c4a7965SKim Phillips } 11659c4a7965SKim Phillips 11664de9d0b5SLee Nipper static struct talitos_edesc *aead_edesc_alloc(struct aead_request *areq, 11674de9d0b5SLee Nipper int icv_stashing) 11684de9d0b5SLee Nipper { 11694de9d0b5SLee Nipper struct crypto_aead *authenc = crypto_aead_reqtfm(areq); 11704de9d0b5SLee Nipper struct talitos_ctx *ctx = crypto_aead_ctx(authenc); 11714de9d0b5SLee Nipper 11724de9d0b5SLee Nipper return talitos_edesc_alloc(ctx->dev, areq->src, areq->dst, 11734de9d0b5SLee Nipper areq->cryptlen, ctx->authsize, icv_stashing, 11744de9d0b5SLee Nipper areq->base.flags); 11754de9d0b5SLee Nipper } 11764de9d0b5SLee Nipper 117756af8cd4SLee Nipper static int aead_encrypt(struct aead_request *req) 11789c4a7965SKim Phillips { 11799c4a7965SKim Phillips struct crypto_aead *authenc = crypto_aead_reqtfm(req); 11809c4a7965SKim Phillips struct talitos_ctx *ctx = crypto_aead_ctx(authenc); 118156af8cd4SLee Nipper struct talitos_edesc *edesc; 11829c4a7965SKim Phillips 11839c4a7965SKim Phillips /* allocate extended descriptor */ 11844de9d0b5SLee Nipper edesc = aead_edesc_alloc(req, 0); 11859c4a7965SKim Phillips if (IS_ERR(edesc)) 11869c4a7965SKim Phillips return PTR_ERR(edesc); 11879c4a7965SKim Phillips 11889c4a7965SKim Phillips /* set encrypt */ 118970bcaca7SLee Nipper edesc->desc.hdr = ctx->desc_hdr_template | DESC_HDR_MODE0_ENCRYPT; 11909c4a7965SKim Phillips 11919c4a7965SKim Phillips return ipsec_esp(edesc, req, NULL, 0, ipsec_esp_encrypt_done); 11929c4a7965SKim Phillips } 11939c4a7965SKim Phillips 119456af8cd4SLee Nipper static int aead_decrypt(struct aead_request *req) 11959c4a7965SKim Phillips { 11969c4a7965SKim Phillips struct crypto_aead *authenc = crypto_aead_reqtfm(req); 11979c4a7965SKim Phillips struct talitos_ctx *ctx = crypto_aead_ctx(authenc); 11989c4a7965SKim Phillips unsigned int authsize = ctx->authsize; 1199fe5720e2SKim Phillips struct talitos_private *priv = dev_get_drvdata(ctx->dev); 120056af8cd4SLee Nipper struct talitos_edesc *edesc; 12019c4a7965SKim Phillips struct scatterlist *sg; 12029c4a7965SKim Phillips void *icvdata; 12039c4a7965SKim Phillips 12049c4a7965SKim Phillips req->cryptlen -= authsize; 12059c4a7965SKim Phillips 12069c4a7965SKim Phillips /* allocate extended descriptor */ 12074de9d0b5SLee Nipper edesc = aead_edesc_alloc(req, 1); 12089c4a7965SKim Phillips if (IS_ERR(edesc)) 12099c4a7965SKim Phillips return PTR_ERR(edesc); 12109c4a7965SKim Phillips 1211fe5720e2SKim Phillips if ((priv->features & TALITOS_FTR_HW_AUTH_CHECK) && 1212*e938e465SKim Phillips ((!edesc->src_nents && !edesc->dst_nents) || 1213*e938e465SKim Phillips priv->features & TALITOS_FTR_SRC_LINK_TBL_LEN_INCLUDES_EXTENT)) { 1214fe5720e2SKim Phillips 1215fe5720e2SKim Phillips /* decrypt and check the ICV */ 1216*e938e465SKim Phillips edesc->desc.hdr = ctx->desc_hdr_template | 1217*e938e465SKim Phillips DESC_HDR_DIR_INBOUND | 1218fe5720e2SKim Phillips DESC_HDR_MODE1_MDEU_CICV; 1219fe5720e2SKim Phillips 1220fe5720e2SKim Phillips /* reset integrity check result bits */ 1221fe5720e2SKim Phillips edesc->desc.hdr_lo = 0; 1222fe5720e2SKim Phillips 1223*e938e465SKim Phillips return ipsec_esp(edesc, req, NULL, 0, 1224*e938e465SKim Phillips ipsec_esp_decrypt_hwauth_done); 1225fe5720e2SKim Phillips 1226*e938e465SKim Phillips } 1227fe5720e2SKim Phillips 1228fe5720e2SKim Phillips /* Have to check the ICV with software */ 1229fe5720e2SKim Phillips edesc->desc.hdr = ctx->desc_hdr_template | DESC_HDR_DIR_INBOUND; 1230fe5720e2SKim Phillips 12319c4a7965SKim Phillips /* stash incoming ICV for later cmp with ICV generated by the h/w */ 12329c4a7965SKim Phillips if (edesc->dma_len) 12339c4a7965SKim Phillips icvdata = &edesc->link_tbl[edesc->src_nents + 1234f3c85bc1SLee Nipper edesc->dst_nents + 2]; 12359c4a7965SKim Phillips else 12369c4a7965SKim Phillips icvdata = &edesc->link_tbl[0]; 12379c4a7965SKim Phillips 12389c4a7965SKim Phillips sg = sg_last(req->src, edesc->src_nents ? : 1); 12399c4a7965SKim Phillips 12409c4a7965SKim Phillips memcpy(icvdata, (char *)sg_virt(sg) + sg->length - ctx->authsize, 12419c4a7965SKim Phillips ctx->authsize); 12429c4a7965SKim Phillips 1243fe5720e2SKim Phillips return ipsec_esp(edesc, req, NULL, 0, ipsec_esp_decrypt_swauth_done); 1244fe5720e2SKim Phillips } 12459c4a7965SKim Phillips 124656af8cd4SLee Nipper static int aead_givencrypt(struct aead_givcrypt_request *req) 12479c4a7965SKim Phillips { 12489c4a7965SKim Phillips struct aead_request *areq = &req->areq; 12499c4a7965SKim Phillips struct crypto_aead *authenc = crypto_aead_reqtfm(areq); 12509c4a7965SKim Phillips struct talitos_ctx *ctx = crypto_aead_ctx(authenc); 125156af8cd4SLee Nipper struct talitos_edesc *edesc; 12529c4a7965SKim Phillips 12539c4a7965SKim Phillips /* allocate extended descriptor */ 12544de9d0b5SLee Nipper edesc = aead_edesc_alloc(areq, 0); 12559c4a7965SKim Phillips if (IS_ERR(edesc)) 12569c4a7965SKim Phillips return PTR_ERR(edesc); 12579c4a7965SKim Phillips 12589c4a7965SKim Phillips /* set encrypt */ 125970bcaca7SLee Nipper edesc->desc.hdr = ctx->desc_hdr_template | DESC_HDR_MODE0_ENCRYPT; 12609c4a7965SKim Phillips 12619c4a7965SKim Phillips memcpy(req->giv, ctx->iv, crypto_aead_ivsize(authenc)); 1262ba95487dSKim Phillips /* avoid consecutive packets going out with same IV */ 1263ba95487dSKim Phillips *(__be64 *)req->giv ^= cpu_to_be64(req->seq); 12649c4a7965SKim Phillips 12659c4a7965SKim Phillips return ipsec_esp(edesc, areq, req->giv, req->seq, 12669c4a7965SKim Phillips ipsec_esp_encrypt_done); 12679c4a7965SKim Phillips } 12689c4a7965SKim Phillips 12694de9d0b5SLee Nipper static int ablkcipher_setkey(struct crypto_ablkcipher *cipher, 12704de9d0b5SLee Nipper const u8 *key, unsigned int keylen) 12714de9d0b5SLee Nipper { 12724de9d0b5SLee Nipper struct talitos_ctx *ctx = crypto_ablkcipher_ctx(cipher); 12734de9d0b5SLee Nipper struct ablkcipher_alg *alg = crypto_ablkcipher_alg(cipher); 12744de9d0b5SLee Nipper 12754de9d0b5SLee Nipper if (keylen > TALITOS_MAX_KEY_SIZE) 12764de9d0b5SLee Nipper goto badkey; 12774de9d0b5SLee Nipper 12784de9d0b5SLee Nipper if (keylen < alg->min_keysize || keylen > alg->max_keysize) 12794de9d0b5SLee Nipper goto badkey; 12804de9d0b5SLee Nipper 12814de9d0b5SLee Nipper memcpy(&ctx->key, key, keylen); 12824de9d0b5SLee Nipper ctx->keylen = keylen; 12834de9d0b5SLee Nipper 12844de9d0b5SLee Nipper return 0; 12854de9d0b5SLee Nipper 12864de9d0b5SLee Nipper badkey: 12874de9d0b5SLee Nipper crypto_ablkcipher_set_flags(cipher, CRYPTO_TFM_RES_BAD_KEY_LEN); 12884de9d0b5SLee Nipper return -EINVAL; 12894de9d0b5SLee Nipper } 12904de9d0b5SLee Nipper 12914de9d0b5SLee Nipper static void common_nonsnoop_unmap(struct device *dev, 12924de9d0b5SLee Nipper struct talitos_edesc *edesc, 12934de9d0b5SLee Nipper struct ablkcipher_request *areq) 12944de9d0b5SLee Nipper { 12954de9d0b5SLee Nipper unmap_single_talitos_ptr(dev, &edesc->desc.ptr[5], DMA_FROM_DEVICE); 12964de9d0b5SLee Nipper unmap_single_talitos_ptr(dev, &edesc->desc.ptr[2], DMA_TO_DEVICE); 12974de9d0b5SLee Nipper unmap_single_talitos_ptr(dev, &edesc->desc.ptr[1], DMA_TO_DEVICE); 12984de9d0b5SLee Nipper 12994de9d0b5SLee Nipper talitos_sg_unmap(dev, edesc, areq->src, areq->dst); 13004de9d0b5SLee Nipper 13014de9d0b5SLee Nipper if (edesc->dma_len) 13024de9d0b5SLee Nipper dma_unmap_single(dev, edesc->dma_link_tbl, edesc->dma_len, 13034de9d0b5SLee Nipper DMA_BIDIRECTIONAL); 13044de9d0b5SLee Nipper } 13054de9d0b5SLee Nipper 13064de9d0b5SLee Nipper static void ablkcipher_done(struct device *dev, 13074de9d0b5SLee Nipper struct talitos_desc *desc, void *context, 13084de9d0b5SLee Nipper int err) 13094de9d0b5SLee Nipper { 13104de9d0b5SLee Nipper struct ablkcipher_request *areq = context; 13114de9d0b5SLee Nipper struct talitos_edesc *edesc = 13124de9d0b5SLee Nipper container_of(desc, struct talitos_edesc, desc); 13134de9d0b5SLee Nipper 13144de9d0b5SLee Nipper common_nonsnoop_unmap(dev, edesc, areq); 13154de9d0b5SLee Nipper 13164de9d0b5SLee Nipper kfree(edesc); 13174de9d0b5SLee Nipper 13184de9d0b5SLee Nipper areq->base.complete(&areq->base, err); 13194de9d0b5SLee Nipper } 13204de9d0b5SLee Nipper 13214de9d0b5SLee Nipper static int common_nonsnoop(struct talitos_edesc *edesc, 13224de9d0b5SLee Nipper struct ablkcipher_request *areq, 13234de9d0b5SLee Nipper u8 *giv, 13244de9d0b5SLee Nipper void (*callback) (struct device *dev, 13254de9d0b5SLee Nipper struct talitos_desc *desc, 13264de9d0b5SLee Nipper void *context, int error)) 13274de9d0b5SLee Nipper { 13284de9d0b5SLee Nipper struct crypto_ablkcipher *cipher = crypto_ablkcipher_reqtfm(areq); 13294de9d0b5SLee Nipper struct talitos_ctx *ctx = crypto_ablkcipher_ctx(cipher); 13304de9d0b5SLee Nipper struct device *dev = ctx->dev; 13314de9d0b5SLee Nipper struct talitos_desc *desc = &edesc->desc; 13324de9d0b5SLee Nipper unsigned int cryptlen = areq->nbytes; 13334de9d0b5SLee Nipper unsigned int ivsize; 13344de9d0b5SLee Nipper int sg_count, ret; 13354de9d0b5SLee Nipper 13364de9d0b5SLee Nipper /* first DWORD empty */ 13374de9d0b5SLee Nipper desc->ptr[0].len = 0; 13384de9d0b5SLee Nipper desc->ptr[0].ptr = 0; 13394de9d0b5SLee Nipper desc->ptr[0].j_extent = 0; 13404de9d0b5SLee Nipper 13414de9d0b5SLee Nipper /* cipher iv */ 13424de9d0b5SLee Nipper ivsize = crypto_ablkcipher_ivsize(cipher); 13434de9d0b5SLee Nipper map_single_talitos_ptr(dev, &desc->ptr[1], ivsize, giv ?: areq->info, 0, 13444de9d0b5SLee Nipper DMA_TO_DEVICE); 13454de9d0b5SLee Nipper 13464de9d0b5SLee Nipper /* cipher key */ 13474de9d0b5SLee Nipper map_single_talitos_ptr(dev, &desc->ptr[2], ctx->keylen, 13484de9d0b5SLee Nipper (char *)&ctx->key, 0, DMA_TO_DEVICE); 13494de9d0b5SLee Nipper 13504de9d0b5SLee Nipper /* 13514de9d0b5SLee Nipper * cipher in 13524de9d0b5SLee Nipper */ 13534de9d0b5SLee Nipper desc->ptr[3].len = cpu_to_be16(cryptlen); 13544de9d0b5SLee Nipper desc->ptr[3].j_extent = 0; 13554de9d0b5SLee Nipper 13564de9d0b5SLee Nipper sg_count = talitos_map_sg(dev, areq->src, edesc->src_nents ? : 1, 13574de9d0b5SLee Nipper (areq->src == areq->dst) ? DMA_BIDIRECTIONAL 13584de9d0b5SLee Nipper : DMA_TO_DEVICE, 13594de9d0b5SLee Nipper edesc->src_is_chained); 13604de9d0b5SLee Nipper 13614de9d0b5SLee Nipper if (sg_count == 1) { 13624de9d0b5SLee Nipper desc->ptr[3].ptr = cpu_to_be32(sg_dma_address(areq->src)); 13634de9d0b5SLee Nipper } else { 13644de9d0b5SLee Nipper sg_count = sg_to_link_tbl(areq->src, sg_count, cryptlen, 13654de9d0b5SLee Nipper &edesc->link_tbl[0]); 13664de9d0b5SLee Nipper if (sg_count > 1) { 13674de9d0b5SLee Nipper desc->ptr[3].j_extent |= DESC_PTR_LNKTBL_JUMP; 13684de9d0b5SLee Nipper desc->ptr[3].ptr = cpu_to_be32(edesc->dma_link_tbl); 1369*e938e465SKim Phillips dma_sync_single_for_device(dev, edesc->dma_link_tbl, 1370*e938e465SKim Phillips edesc->dma_len, 1371*e938e465SKim Phillips DMA_BIDIRECTIONAL); 13724de9d0b5SLee Nipper } else { 13734de9d0b5SLee Nipper /* Only one segment now, so no link tbl needed */ 1374*e938e465SKim Phillips desc->ptr[3].ptr = cpu_to_be32(sg_dma_address(areq-> 1375*e938e465SKim Phillips src)); 13764de9d0b5SLee Nipper } 13774de9d0b5SLee Nipper } 13784de9d0b5SLee Nipper 13794de9d0b5SLee Nipper /* cipher out */ 13804de9d0b5SLee Nipper desc->ptr[4].len = cpu_to_be16(cryptlen); 13814de9d0b5SLee Nipper desc->ptr[4].j_extent = 0; 13824de9d0b5SLee Nipper 13834de9d0b5SLee Nipper if (areq->src != areq->dst) 13844de9d0b5SLee Nipper sg_count = talitos_map_sg(dev, areq->dst, 13854de9d0b5SLee Nipper edesc->dst_nents ? : 1, 13864de9d0b5SLee Nipper DMA_FROM_DEVICE, 13874de9d0b5SLee Nipper edesc->dst_is_chained); 13884de9d0b5SLee Nipper 13894de9d0b5SLee Nipper if (sg_count == 1) { 13904de9d0b5SLee Nipper desc->ptr[4].ptr = cpu_to_be32(sg_dma_address(areq->dst)); 13914de9d0b5SLee Nipper } else { 13924de9d0b5SLee Nipper struct talitos_ptr *link_tbl_ptr = 13934de9d0b5SLee Nipper &edesc->link_tbl[edesc->src_nents + 1]; 13944de9d0b5SLee Nipper 13954de9d0b5SLee Nipper desc->ptr[4].j_extent |= DESC_PTR_LNKTBL_JUMP; 13964de9d0b5SLee Nipper desc->ptr[4].ptr = cpu_to_be32((struct talitos_ptr *) 13974de9d0b5SLee Nipper edesc->dma_link_tbl + 13984de9d0b5SLee Nipper edesc->src_nents + 1); 13994de9d0b5SLee Nipper sg_count = sg_to_link_tbl(areq->dst, sg_count, cryptlen, 14004de9d0b5SLee Nipper link_tbl_ptr); 14014de9d0b5SLee Nipper dma_sync_single_for_device(ctx->dev, edesc->dma_link_tbl, 14024de9d0b5SLee Nipper edesc->dma_len, DMA_BIDIRECTIONAL); 14034de9d0b5SLee Nipper } 14044de9d0b5SLee Nipper 14054de9d0b5SLee Nipper /* iv out */ 14064de9d0b5SLee Nipper map_single_talitos_ptr(dev, &desc->ptr[5], ivsize, ctx->iv, 0, 14074de9d0b5SLee Nipper DMA_FROM_DEVICE); 14084de9d0b5SLee Nipper 14094de9d0b5SLee Nipper /* last DWORD empty */ 14104de9d0b5SLee Nipper desc->ptr[6].len = 0; 14114de9d0b5SLee Nipper desc->ptr[6].ptr = 0; 14124de9d0b5SLee Nipper desc->ptr[6].j_extent = 0; 14134de9d0b5SLee Nipper 14144de9d0b5SLee Nipper ret = talitos_submit(dev, desc, callback, areq); 14154de9d0b5SLee Nipper if (ret != -EINPROGRESS) { 14164de9d0b5SLee Nipper common_nonsnoop_unmap(dev, edesc, areq); 14174de9d0b5SLee Nipper kfree(edesc); 14184de9d0b5SLee Nipper } 14194de9d0b5SLee Nipper return ret; 14204de9d0b5SLee Nipper } 14214de9d0b5SLee Nipper 1422*e938e465SKim Phillips static struct talitos_edesc *ablkcipher_edesc_alloc(struct ablkcipher_request * 1423*e938e465SKim Phillips areq) 14244de9d0b5SLee Nipper { 14254de9d0b5SLee Nipper struct crypto_ablkcipher *cipher = crypto_ablkcipher_reqtfm(areq); 14264de9d0b5SLee Nipper struct talitos_ctx *ctx = crypto_ablkcipher_ctx(cipher); 14274de9d0b5SLee Nipper 14284de9d0b5SLee Nipper return talitos_edesc_alloc(ctx->dev, areq->src, areq->dst, areq->nbytes, 14294de9d0b5SLee Nipper 0, 0, areq->base.flags); 14304de9d0b5SLee Nipper } 14314de9d0b5SLee Nipper 14324de9d0b5SLee Nipper static int ablkcipher_encrypt(struct ablkcipher_request *areq) 14334de9d0b5SLee Nipper { 14344de9d0b5SLee Nipper struct crypto_ablkcipher *cipher = crypto_ablkcipher_reqtfm(areq); 14354de9d0b5SLee Nipper struct talitos_ctx *ctx = crypto_ablkcipher_ctx(cipher); 14364de9d0b5SLee Nipper struct talitos_edesc *edesc; 14374de9d0b5SLee Nipper 14384de9d0b5SLee Nipper /* allocate extended descriptor */ 14394de9d0b5SLee Nipper edesc = ablkcipher_edesc_alloc(areq); 14404de9d0b5SLee Nipper if (IS_ERR(edesc)) 14414de9d0b5SLee Nipper return PTR_ERR(edesc); 14424de9d0b5SLee Nipper 14434de9d0b5SLee Nipper /* set encrypt */ 14444de9d0b5SLee Nipper edesc->desc.hdr = ctx->desc_hdr_template | DESC_HDR_MODE0_ENCRYPT; 14454de9d0b5SLee Nipper 14464de9d0b5SLee Nipper return common_nonsnoop(edesc, areq, NULL, ablkcipher_done); 14474de9d0b5SLee Nipper } 14484de9d0b5SLee Nipper 14494de9d0b5SLee Nipper static int ablkcipher_decrypt(struct ablkcipher_request *areq) 14504de9d0b5SLee Nipper { 14514de9d0b5SLee Nipper struct crypto_ablkcipher *cipher = crypto_ablkcipher_reqtfm(areq); 14524de9d0b5SLee Nipper struct talitos_ctx *ctx = crypto_ablkcipher_ctx(cipher); 14534de9d0b5SLee Nipper struct talitos_edesc *edesc; 14544de9d0b5SLee Nipper 14554de9d0b5SLee Nipper /* allocate extended descriptor */ 14564de9d0b5SLee Nipper edesc = ablkcipher_edesc_alloc(areq); 14574de9d0b5SLee Nipper if (IS_ERR(edesc)) 14584de9d0b5SLee Nipper return PTR_ERR(edesc); 14594de9d0b5SLee Nipper 14604de9d0b5SLee Nipper edesc->desc.hdr = ctx->desc_hdr_template | DESC_HDR_DIR_INBOUND; 14614de9d0b5SLee Nipper 14624de9d0b5SLee Nipper return common_nonsnoop(edesc, areq, NULL, ablkcipher_done); 14634de9d0b5SLee Nipper } 14644de9d0b5SLee Nipper 14659c4a7965SKim Phillips struct talitos_alg_template { 146656af8cd4SLee Nipper struct crypto_alg alg; 14679c4a7965SKim Phillips __be32 desc_hdr_template; 14689c4a7965SKim Phillips }; 14699c4a7965SKim Phillips 14709c4a7965SKim Phillips static struct talitos_alg_template driver_algs[] = { 147156af8cd4SLee Nipper /* AEAD algorithms. These use a single-pass ipsec_esp descriptor */ 14729c4a7965SKim Phillips { 147356af8cd4SLee Nipper .alg = { 147456af8cd4SLee Nipper .cra_name = "authenc(hmac(sha1),cbc(aes))", 147556af8cd4SLee Nipper .cra_driver_name = "authenc-hmac-sha1-cbc-aes-talitos", 147656af8cd4SLee Nipper .cra_blocksize = AES_BLOCK_SIZE, 147756af8cd4SLee Nipper .cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC, 147856af8cd4SLee Nipper .cra_type = &crypto_aead_type, 147956af8cd4SLee Nipper .cra_aead = { 148056af8cd4SLee Nipper .setkey = aead_setkey, 148156af8cd4SLee Nipper .setauthsize = aead_setauthsize, 148256af8cd4SLee Nipper .encrypt = aead_encrypt, 148356af8cd4SLee Nipper .decrypt = aead_decrypt, 148456af8cd4SLee Nipper .givencrypt = aead_givencrypt, 14859c4a7965SKim Phillips .geniv = "<built-in>", 14863952f17eSLee Nipper .ivsize = AES_BLOCK_SIZE, 14873952f17eSLee Nipper .maxauthsize = SHA1_DIGEST_SIZE, 148856af8cd4SLee Nipper } 14899c4a7965SKim Phillips }, 14909c4a7965SKim Phillips .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP | 14919c4a7965SKim Phillips DESC_HDR_SEL0_AESU | 14929c4a7965SKim Phillips DESC_HDR_MODE0_AESU_CBC | 14939c4a7965SKim Phillips DESC_HDR_SEL1_MDEUA | 14949c4a7965SKim Phillips DESC_HDR_MODE1_MDEU_INIT | 14959c4a7965SKim Phillips DESC_HDR_MODE1_MDEU_PAD | 14969c4a7965SKim Phillips DESC_HDR_MODE1_MDEU_SHA1_HMAC, 149770bcaca7SLee Nipper }, 149870bcaca7SLee Nipper { 149956af8cd4SLee Nipper .alg = { 150056af8cd4SLee Nipper .cra_name = "authenc(hmac(sha1),cbc(des3_ede))", 150156af8cd4SLee Nipper .cra_driver_name = "authenc-hmac-sha1-cbc-3des-talitos", 150256af8cd4SLee Nipper .cra_blocksize = DES3_EDE_BLOCK_SIZE, 150356af8cd4SLee Nipper .cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC, 150456af8cd4SLee Nipper .cra_type = &crypto_aead_type, 150556af8cd4SLee Nipper .cra_aead = { 150656af8cd4SLee Nipper .setkey = aead_setkey, 150756af8cd4SLee Nipper .setauthsize = aead_setauthsize, 150856af8cd4SLee Nipper .encrypt = aead_encrypt, 150956af8cd4SLee Nipper .decrypt = aead_decrypt, 151056af8cd4SLee Nipper .givencrypt = aead_givencrypt, 151170bcaca7SLee Nipper .geniv = "<built-in>", 15123952f17eSLee Nipper .ivsize = DES3_EDE_BLOCK_SIZE, 15133952f17eSLee Nipper .maxauthsize = SHA1_DIGEST_SIZE, 151456af8cd4SLee Nipper } 151570bcaca7SLee Nipper }, 151670bcaca7SLee Nipper .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP | 151770bcaca7SLee Nipper DESC_HDR_SEL0_DEU | 151870bcaca7SLee Nipper DESC_HDR_MODE0_DEU_CBC | 151970bcaca7SLee Nipper DESC_HDR_MODE0_DEU_3DES | 152070bcaca7SLee Nipper DESC_HDR_SEL1_MDEUA | 152170bcaca7SLee Nipper DESC_HDR_MODE1_MDEU_INIT | 152270bcaca7SLee Nipper DESC_HDR_MODE1_MDEU_PAD | 152370bcaca7SLee Nipper DESC_HDR_MODE1_MDEU_SHA1_HMAC, 15243952f17eSLee Nipper }, 15253952f17eSLee Nipper { 152656af8cd4SLee Nipper .alg = { 152756af8cd4SLee Nipper .cra_name = "authenc(hmac(sha256),cbc(aes))", 152856af8cd4SLee Nipper .cra_driver_name = "authenc-hmac-sha256-cbc-aes-talitos", 152956af8cd4SLee Nipper .cra_blocksize = AES_BLOCK_SIZE, 153056af8cd4SLee Nipper .cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC, 153156af8cd4SLee Nipper .cra_type = &crypto_aead_type, 153256af8cd4SLee Nipper .cra_aead = { 153356af8cd4SLee Nipper .setkey = aead_setkey, 153456af8cd4SLee Nipper .setauthsize = aead_setauthsize, 153556af8cd4SLee Nipper .encrypt = aead_encrypt, 153656af8cd4SLee Nipper .decrypt = aead_decrypt, 153756af8cd4SLee Nipper .givencrypt = aead_givencrypt, 15383952f17eSLee Nipper .geniv = "<built-in>", 15393952f17eSLee Nipper .ivsize = AES_BLOCK_SIZE, 15403952f17eSLee Nipper .maxauthsize = SHA256_DIGEST_SIZE, 154156af8cd4SLee Nipper } 15423952f17eSLee Nipper }, 15433952f17eSLee Nipper .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP | 15443952f17eSLee Nipper DESC_HDR_SEL0_AESU | 15453952f17eSLee Nipper DESC_HDR_MODE0_AESU_CBC | 15463952f17eSLee Nipper DESC_HDR_SEL1_MDEUA | 15473952f17eSLee Nipper DESC_HDR_MODE1_MDEU_INIT | 15483952f17eSLee Nipper DESC_HDR_MODE1_MDEU_PAD | 15493952f17eSLee Nipper DESC_HDR_MODE1_MDEU_SHA256_HMAC, 15503952f17eSLee Nipper }, 15513952f17eSLee Nipper { 155256af8cd4SLee Nipper .alg = { 155356af8cd4SLee Nipper .cra_name = "authenc(hmac(sha256),cbc(des3_ede))", 155456af8cd4SLee Nipper .cra_driver_name = "authenc-hmac-sha256-cbc-3des-talitos", 155556af8cd4SLee Nipper .cra_blocksize = DES3_EDE_BLOCK_SIZE, 155656af8cd4SLee Nipper .cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC, 155756af8cd4SLee Nipper .cra_type = &crypto_aead_type, 155856af8cd4SLee Nipper .cra_aead = { 155956af8cd4SLee Nipper .setkey = aead_setkey, 156056af8cd4SLee Nipper .setauthsize = aead_setauthsize, 156156af8cd4SLee Nipper .encrypt = aead_encrypt, 156256af8cd4SLee Nipper .decrypt = aead_decrypt, 156356af8cd4SLee Nipper .givencrypt = aead_givencrypt, 15643952f17eSLee Nipper .geniv = "<built-in>", 15653952f17eSLee Nipper .ivsize = DES3_EDE_BLOCK_SIZE, 15663952f17eSLee Nipper .maxauthsize = SHA256_DIGEST_SIZE, 156756af8cd4SLee Nipper } 15683952f17eSLee Nipper }, 15693952f17eSLee Nipper .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP | 15703952f17eSLee Nipper DESC_HDR_SEL0_DEU | 15713952f17eSLee Nipper DESC_HDR_MODE0_DEU_CBC | 15723952f17eSLee Nipper DESC_HDR_MODE0_DEU_3DES | 15733952f17eSLee Nipper DESC_HDR_SEL1_MDEUA | 15743952f17eSLee Nipper DESC_HDR_MODE1_MDEU_INIT | 15753952f17eSLee Nipper DESC_HDR_MODE1_MDEU_PAD | 15763952f17eSLee Nipper DESC_HDR_MODE1_MDEU_SHA256_HMAC, 15773952f17eSLee Nipper }, 15783952f17eSLee Nipper { 157956af8cd4SLee Nipper .alg = { 158056af8cd4SLee Nipper .cra_name = "authenc(hmac(md5),cbc(aes))", 158156af8cd4SLee Nipper .cra_driver_name = "authenc-hmac-md5-cbc-aes-talitos", 158256af8cd4SLee Nipper .cra_blocksize = AES_BLOCK_SIZE, 158356af8cd4SLee Nipper .cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC, 158456af8cd4SLee Nipper .cra_type = &crypto_aead_type, 158556af8cd4SLee Nipper .cra_aead = { 158656af8cd4SLee Nipper .setkey = aead_setkey, 158756af8cd4SLee Nipper .setauthsize = aead_setauthsize, 158856af8cd4SLee Nipper .encrypt = aead_encrypt, 158956af8cd4SLee Nipper .decrypt = aead_decrypt, 159056af8cd4SLee Nipper .givencrypt = aead_givencrypt, 15913952f17eSLee Nipper .geniv = "<built-in>", 15923952f17eSLee Nipper .ivsize = AES_BLOCK_SIZE, 15933952f17eSLee Nipper .maxauthsize = MD5_DIGEST_SIZE, 159456af8cd4SLee Nipper } 15953952f17eSLee Nipper }, 15963952f17eSLee Nipper .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP | 15973952f17eSLee Nipper DESC_HDR_SEL0_AESU | 15983952f17eSLee Nipper DESC_HDR_MODE0_AESU_CBC | 15993952f17eSLee Nipper DESC_HDR_SEL1_MDEUA | 16003952f17eSLee Nipper DESC_HDR_MODE1_MDEU_INIT | 16013952f17eSLee Nipper DESC_HDR_MODE1_MDEU_PAD | 16023952f17eSLee Nipper DESC_HDR_MODE1_MDEU_MD5_HMAC, 16033952f17eSLee Nipper }, 16043952f17eSLee Nipper { 160556af8cd4SLee Nipper .alg = { 160656af8cd4SLee Nipper .cra_name = "authenc(hmac(md5),cbc(des3_ede))", 160756af8cd4SLee Nipper .cra_driver_name = "authenc-hmac-md5-cbc-3des-talitos", 160856af8cd4SLee Nipper .cra_blocksize = DES3_EDE_BLOCK_SIZE, 160956af8cd4SLee Nipper .cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC, 161056af8cd4SLee Nipper .cra_type = &crypto_aead_type, 161156af8cd4SLee Nipper .cra_aead = { 161256af8cd4SLee Nipper .setkey = aead_setkey, 161356af8cd4SLee Nipper .setauthsize = aead_setauthsize, 161456af8cd4SLee Nipper .encrypt = aead_encrypt, 161556af8cd4SLee Nipper .decrypt = aead_decrypt, 161656af8cd4SLee Nipper .givencrypt = aead_givencrypt, 16173952f17eSLee Nipper .geniv = "<built-in>", 16183952f17eSLee Nipper .ivsize = DES3_EDE_BLOCK_SIZE, 16193952f17eSLee Nipper .maxauthsize = MD5_DIGEST_SIZE, 162056af8cd4SLee Nipper } 16213952f17eSLee Nipper }, 16223952f17eSLee Nipper .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP | 16233952f17eSLee Nipper DESC_HDR_SEL0_DEU | 16243952f17eSLee Nipper DESC_HDR_MODE0_DEU_CBC | 16253952f17eSLee Nipper DESC_HDR_MODE0_DEU_3DES | 16263952f17eSLee Nipper DESC_HDR_SEL1_MDEUA | 16273952f17eSLee Nipper DESC_HDR_MODE1_MDEU_INIT | 16283952f17eSLee Nipper DESC_HDR_MODE1_MDEU_PAD | 16293952f17eSLee Nipper DESC_HDR_MODE1_MDEU_MD5_HMAC, 16304de9d0b5SLee Nipper }, 16314de9d0b5SLee Nipper /* ABLKCIPHER algorithms. */ 16324de9d0b5SLee Nipper { 16334de9d0b5SLee Nipper .alg = { 16344de9d0b5SLee Nipper .cra_name = "cbc(aes)", 16354de9d0b5SLee Nipper .cra_driver_name = "cbc-aes-talitos", 16364de9d0b5SLee Nipper .cra_blocksize = AES_BLOCK_SIZE, 16374de9d0b5SLee Nipper .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | 16384de9d0b5SLee Nipper CRYPTO_ALG_ASYNC, 16394de9d0b5SLee Nipper .cra_type = &crypto_ablkcipher_type, 16404de9d0b5SLee Nipper .cra_ablkcipher = { 16414de9d0b5SLee Nipper .setkey = ablkcipher_setkey, 16424de9d0b5SLee Nipper .encrypt = ablkcipher_encrypt, 16434de9d0b5SLee Nipper .decrypt = ablkcipher_decrypt, 16444de9d0b5SLee Nipper .geniv = "eseqiv", 16454de9d0b5SLee Nipper .min_keysize = AES_MIN_KEY_SIZE, 16464de9d0b5SLee Nipper .max_keysize = AES_MAX_KEY_SIZE, 16474de9d0b5SLee Nipper .ivsize = AES_BLOCK_SIZE, 16484de9d0b5SLee Nipper } 16494de9d0b5SLee Nipper }, 16504de9d0b5SLee Nipper .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU | 16514de9d0b5SLee Nipper DESC_HDR_SEL0_AESU | 16524de9d0b5SLee Nipper DESC_HDR_MODE0_AESU_CBC, 16534de9d0b5SLee Nipper }, 16544de9d0b5SLee Nipper { 16554de9d0b5SLee Nipper .alg = { 16564de9d0b5SLee Nipper .cra_name = "cbc(des3_ede)", 16574de9d0b5SLee Nipper .cra_driver_name = "cbc-3des-talitos", 16584de9d0b5SLee Nipper .cra_blocksize = DES3_EDE_BLOCK_SIZE, 16594de9d0b5SLee Nipper .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | 16604de9d0b5SLee Nipper CRYPTO_ALG_ASYNC, 16614de9d0b5SLee Nipper .cra_type = &crypto_ablkcipher_type, 16624de9d0b5SLee Nipper .cra_ablkcipher = { 16634de9d0b5SLee Nipper .setkey = ablkcipher_setkey, 16644de9d0b5SLee Nipper .encrypt = ablkcipher_encrypt, 16654de9d0b5SLee Nipper .decrypt = ablkcipher_decrypt, 16664de9d0b5SLee Nipper .geniv = "eseqiv", 16674de9d0b5SLee Nipper .min_keysize = DES3_EDE_KEY_SIZE, 16684de9d0b5SLee Nipper .max_keysize = DES3_EDE_KEY_SIZE, 16694de9d0b5SLee Nipper .ivsize = DES3_EDE_BLOCK_SIZE, 16704de9d0b5SLee Nipper } 16714de9d0b5SLee Nipper }, 16724de9d0b5SLee Nipper .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU | 16734de9d0b5SLee Nipper DESC_HDR_SEL0_DEU | 16744de9d0b5SLee Nipper DESC_HDR_MODE0_DEU_CBC | 16754de9d0b5SLee Nipper DESC_HDR_MODE0_DEU_3DES, 16769c4a7965SKim Phillips } 16779c4a7965SKim Phillips }; 16789c4a7965SKim Phillips 16799c4a7965SKim Phillips struct talitos_crypto_alg { 16809c4a7965SKim Phillips struct list_head entry; 16819c4a7965SKim Phillips struct device *dev; 16829c4a7965SKim Phillips __be32 desc_hdr_template; 16839c4a7965SKim Phillips struct crypto_alg crypto_alg; 16849c4a7965SKim Phillips }; 16859c4a7965SKim Phillips 16869c4a7965SKim Phillips static int talitos_cra_init(struct crypto_tfm *tfm) 16879c4a7965SKim Phillips { 16889c4a7965SKim Phillips struct crypto_alg *alg = tfm->__crt_alg; 16899c4a7965SKim Phillips struct talitos_crypto_alg *talitos_alg = 16909c4a7965SKim Phillips container_of(alg, struct talitos_crypto_alg, crypto_alg); 16919c4a7965SKim Phillips struct talitos_ctx *ctx = crypto_tfm_ctx(tfm); 16929c4a7965SKim Phillips 16939c4a7965SKim Phillips /* update context with ptr to dev */ 16949c4a7965SKim Phillips ctx->dev = talitos_alg->dev; 16959c4a7965SKim Phillips /* copy descriptor header template value */ 16969c4a7965SKim Phillips ctx->desc_hdr_template = talitos_alg->desc_hdr_template; 16979c4a7965SKim Phillips 16989c4a7965SKim Phillips /* random first IV */ 169970bcaca7SLee Nipper get_random_bytes(ctx->iv, TALITOS_MAX_IV_LENGTH); 17009c4a7965SKim Phillips 17019c4a7965SKim Phillips return 0; 17029c4a7965SKim Phillips } 17039c4a7965SKim Phillips 17049c4a7965SKim Phillips /* 17059c4a7965SKim Phillips * given the alg's descriptor header template, determine whether descriptor 17069c4a7965SKim Phillips * type and primary/secondary execution units required match the hw 17079c4a7965SKim Phillips * capabilities description provided in the device tree node. 17089c4a7965SKim Phillips */ 17099c4a7965SKim Phillips static int hw_supports(struct device *dev, __be32 desc_hdr_template) 17109c4a7965SKim Phillips { 17119c4a7965SKim Phillips struct talitos_private *priv = dev_get_drvdata(dev); 17129c4a7965SKim Phillips int ret; 17139c4a7965SKim Phillips 17149c4a7965SKim Phillips ret = (1 << DESC_TYPE(desc_hdr_template) & priv->desc_types) && 17159c4a7965SKim Phillips (1 << PRIMARY_EU(desc_hdr_template) & priv->exec_units); 17169c4a7965SKim Phillips 17179c4a7965SKim Phillips if (SECONDARY_EU(desc_hdr_template)) 17189c4a7965SKim Phillips ret = ret && (1 << SECONDARY_EU(desc_hdr_template) 17199c4a7965SKim Phillips & priv->exec_units); 17209c4a7965SKim Phillips 17219c4a7965SKim Phillips return ret; 17229c4a7965SKim Phillips } 17239c4a7965SKim Phillips 1724596f1034SAl Viro static int talitos_remove(struct of_device *ofdev) 17259c4a7965SKim Phillips { 17269c4a7965SKim Phillips struct device *dev = &ofdev->dev; 17279c4a7965SKim Phillips struct talitos_private *priv = dev_get_drvdata(dev); 17289c4a7965SKim Phillips struct talitos_crypto_alg *t_alg, *n; 17299c4a7965SKim Phillips int i; 17309c4a7965SKim Phillips 17319c4a7965SKim Phillips list_for_each_entry_safe(t_alg, n, &priv->alg_list, entry) { 17329c4a7965SKim Phillips crypto_unregister_alg(&t_alg->crypto_alg); 17339c4a7965SKim Phillips list_del(&t_alg->entry); 17349c4a7965SKim Phillips kfree(t_alg); 17359c4a7965SKim Phillips } 17369c4a7965SKim Phillips 17379c4a7965SKim Phillips if (hw_supports(dev, DESC_HDR_SEL0_RNG)) 17389c4a7965SKim Phillips talitos_unregister_rng(dev); 17399c4a7965SKim Phillips 1740ec6644d6SKim Phillips kfree(priv->submit_count); 17419c4a7965SKim Phillips kfree(priv->tail); 17429c4a7965SKim Phillips kfree(priv->head); 17439c4a7965SKim Phillips 17449c4a7965SKim Phillips if (priv->fifo) 17459c4a7965SKim Phillips for (i = 0; i < priv->num_channels; i++) 17469c4a7965SKim Phillips kfree(priv->fifo[i]); 17479c4a7965SKim Phillips 17489c4a7965SKim Phillips kfree(priv->fifo); 17499c4a7965SKim Phillips kfree(priv->head_lock); 17509c4a7965SKim Phillips kfree(priv->tail_lock); 17519c4a7965SKim Phillips 17529c4a7965SKim Phillips if (priv->irq != NO_IRQ) { 17539c4a7965SKim Phillips free_irq(priv->irq, dev); 17549c4a7965SKim Phillips irq_dispose_mapping(priv->irq); 17559c4a7965SKim Phillips } 17569c4a7965SKim Phillips 17579c4a7965SKim Phillips tasklet_kill(&priv->done_task); 17589c4a7965SKim Phillips 17599c4a7965SKim Phillips iounmap(priv->reg); 17609c4a7965SKim Phillips 17619c4a7965SKim Phillips dev_set_drvdata(dev, NULL); 17629c4a7965SKim Phillips 17639c4a7965SKim Phillips kfree(priv); 17649c4a7965SKim Phillips 17659c4a7965SKim Phillips return 0; 17669c4a7965SKim Phillips } 17679c4a7965SKim Phillips 17689c4a7965SKim Phillips static struct talitos_crypto_alg *talitos_alg_alloc(struct device *dev, 17699c4a7965SKim Phillips struct talitos_alg_template 17709c4a7965SKim Phillips *template) 17719c4a7965SKim Phillips { 17729c4a7965SKim Phillips struct talitos_crypto_alg *t_alg; 17739c4a7965SKim Phillips struct crypto_alg *alg; 17749c4a7965SKim Phillips 17759c4a7965SKim Phillips t_alg = kzalloc(sizeof(struct talitos_crypto_alg), GFP_KERNEL); 17769c4a7965SKim Phillips if (!t_alg) 17779c4a7965SKim Phillips return ERR_PTR(-ENOMEM); 17789c4a7965SKim Phillips 17799c4a7965SKim Phillips alg = &t_alg->crypto_alg; 178056af8cd4SLee Nipper *alg = template->alg; 17819c4a7965SKim Phillips 17829c4a7965SKim Phillips alg->cra_module = THIS_MODULE; 17839c4a7965SKim Phillips alg->cra_init = talitos_cra_init; 17849c4a7965SKim Phillips alg->cra_priority = TALITOS_CRA_PRIORITY; 17859c4a7965SKim Phillips alg->cra_alignmask = 0; 17869c4a7965SKim Phillips alg->cra_ctxsize = sizeof(struct talitos_ctx); 17879c4a7965SKim Phillips 17889c4a7965SKim Phillips t_alg->desc_hdr_template = template->desc_hdr_template; 17899c4a7965SKim Phillips t_alg->dev = dev; 17909c4a7965SKim Phillips 17919c4a7965SKim Phillips return t_alg; 17929c4a7965SKim Phillips } 17939c4a7965SKim Phillips 17949c4a7965SKim Phillips static int talitos_probe(struct of_device *ofdev, 17959c4a7965SKim Phillips const struct of_device_id *match) 17969c4a7965SKim Phillips { 17979c4a7965SKim Phillips struct device *dev = &ofdev->dev; 17989c4a7965SKim Phillips struct device_node *np = ofdev->node; 17999c4a7965SKim Phillips struct talitos_private *priv; 18009c4a7965SKim Phillips const unsigned int *prop; 18019c4a7965SKim Phillips int i, err; 18029c4a7965SKim Phillips 18039c4a7965SKim Phillips priv = kzalloc(sizeof(struct talitos_private), GFP_KERNEL); 18049c4a7965SKim Phillips if (!priv) 18059c4a7965SKim Phillips return -ENOMEM; 18069c4a7965SKim Phillips 18079c4a7965SKim Phillips dev_set_drvdata(dev, priv); 18089c4a7965SKim Phillips 18099c4a7965SKim Phillips priv->ofdev = ofdev; 18109c4a7965SKim Phillips 18119c4a7965SKim Phillips tasklet_init(&priv->done_task, talitos_done, (unsigned long)dev); 18129c4a7965SKim Phillips 1813fe5720e2SKim Phillips INIT_LIST_HEAD(&priv->alg_list); 1814fe5720e2SKim Phillips 18159c4a7965SKim Phillips priv->irq = irq_of_parse_and_map(np, 0); 18169c4a7965SKim Phillips 18179c4a7965SKim Phillips if (priv->irq == NO_IRQ) { 18189c4a7965SKim Phillips dev_err(dev, "failed to map irq\n"); 18199c4a7965SKim Phillips err = -EINVAL; 18209c4a7965SKim Phillips goto err_out; 18219c4a7965SKim Phillips } 18229c4a7965SKim Phillips 18239c4a7965SKim Phillips /* get the irq line */ 18249c4a7965SKim Phillips err = request_irq(priv->irq, talitos_interrupt, 0, 18259c4a7965SKim Phillips dev_driver_string(dev), dev); 18269c4a7965SKim Phillips if (err) { 18279c4a7965SKim Phillips dev_err(dev, "failed to request irq %d\n", priv->irq); 18289c4a7965SKim Phillips irq_dispose_mapping(priv->irq); 18299c4a7965SKim Phillips priv->irq = NO_IRQ; 18309c4a7965SKim Phillips goto err_out; 18319c4a7965SKim Phillips } 18329c4a7965SKim Phillips 18339c4a7965SKim Phillips priv->reg = of_iomap(np, 0); 18349c4a7965SKim Phillips if (!priv->reg) { 18359c4a7965SKim Phillips dev_err(dev, "failed to of_iomap\n"); 18369c4a7965SKim Phillips err = -ENOMEM; 18379c4a7965SKim Phillips goto err_out; 18389c4a7965SKim Phillips } 18399c4a7965SKim Phillips 18409c4a7965SKim Phillips /* get SEC version capabilities from device tree */ 18419c4a7965SKim Phillips prop = of_get_property(np, "fsl,num-channels", NULL); 18429c4a7965SKim Phillips if (prop) 18439c4a7965SKim Phillips priv->num_channels = *prop; 18449c4a7965SKim Phillips 18459c4a7965SKim Phillips prop = of_get_property(np, "fsl,channel-fifo-len", NULL); 18469c4a7965SKim Phillips if (prop) 18479c4a7965SKim Phillips priv->chfifo_len = *prop; 18489c4a7965SKim Phillips 18499c4a7965SKim Phillips prop = of_get_property(np, "fsl,exec-units-mask", NULL); 18509c4a7965SKim Phillips if (prop) 18519c4a7965SKim Phillips priv->exec_units = *prop; 18529c4a7965SKim Phillips 18539c4a7965SKim Phillips prop = of_get_property(np, "fsl,descriptor-types-mask", NULL); 18549c4a7965SKim Phillips if (prop) 18559c4a7965SKim Phillips priv->desc_types = *prop; 18569c4a7965SKim Phillips 18579c4a7965SKim Phillips if (!is_power_of_2(priv->num_channels) || !priv->chfifo_len || 18589c4a7965SKim Phillips !priv->exec_units || !priv->desc_types) { 18599c4a7965SKim Phillips dev_err(dev, "invalid property data in device tree node\n"); 18609c4a7965SKim Phillips err = -EINVAL; 18619c4a7965SKim Phillips goto err_out; 18629c4a7965SKim Phillips } 18639c4a7965SKim Phillips 1864f3c85bc1SLee Nipper if (of_device_is_compatible(np, "fsl,sec3.0")) 1865f3c85bc1SLee Nipper priv->features |= TALITOS_FTR_SRC_LINK_TBL_LEN_INCLUDES_EXTENT; 1866f3c85bc1SLee Nipper 1867fe5720e2SKim Phillips if (of_device_is_compatible(np, "fsl,sec2.1")) 1868fe5720e2SKim Phillips priv->features |= TALITOS_FTR_HW_AUTH_CHECK; 1869fe5720e2SKim Phillips 18709c4a7965SKim Phillips priv->head_lock = kmalloc(sizeof(spinlock_t) * priv->num_channels, 18719c4a7965SKim Phillips GFP_KERNEL); 18729c4a7965SKim Phillips priv->tail_lock = kmalloc(sizeof(spinlock_t) * priv->num_channels, 18739c4a7965SKim Phillips GFP_KERNEL); 18749c4a7965SKim Phillips if (!priv->head_lock || !priv->tail_lock) { 18759c4a7965SKim Phillips dev_err(dev, "failed to allocate fifo locks\n"); 18769c4a7965SKim Phillips err = -ENOMEM; 18779c4a7965SKim Phillips goto err_out; 18789c4a7965SKim Phillips } 18799c4a7965SKim Phillips 18809c4a7965SKim Phillips for (i = 0; i < priv->num_channels; i++) { 18819c4a7965SKim Phillips spin_lock_init(&priv->head_lock[i]); 18829c4a7965SKim Phillips spin_lock_init(&priv->tail_lock[i]); 18839c4a7965SKim Phillips } 18849c4a7965SKim Phillips 18859c4a7965SKim Phillips priv->fifo = kmalloc(sizeof(struct talitos_request *) * 18869c4a7965SKim Phillips priv->num_channels, GFP_KERNEL); 18879c4a7965SKim Phillips if (!priv->fifo) { 18889c4a7965SKim Phillips dev_err(dev, "failed to allocate request fifo\n"); 18899c4a7965SKim Phillips err = -ENOMEM; 18909c4a7965SKim Phillips goto err_out; 18919c4a7965SKim Phillips } 18929c4a7965SKim Phillips 18939c4a7965SKim Phillips priv->fifo_len = roundup_pow_of_two(priv->chfifo_len); 18949c4a7965SKim Phillips 18959c4a7965SKim Phillips for (i = 0; i < priv->num_channels; i++) { 18969c4a7965SKim Phillips priv->fifo[i] = kzalloc(sizeof(struct talitos_request) * 18979c4a7965SKim Phillips priv->fifo_len, GFP_KERNEL); 18989c4a7965SKim Phillips if (!priv->fifo[i]) { 18999c4a7965SKim Phillips dev_err(dev, "failed to allocate request fifo %d\n", i); 19009c4a7965SKim Phillips err = -ENOMEM; 19019c4a7965SKim Phillips goto err_out; 19029c4a7965SKim Phillips } 19039c4a7965SKim Phillips } 19049c4a7965SKim Phillips 1905586725f8SKim Phillips priv->submit_count = kmalloc(sizeof(atomic_t) * priv->num_channels, 1906ec6644d6SKim Phillips GFP_KERNEL); 1907ec6644d6SKim Phillips if (!priv->submit_count) { 1908ec6644d6SKim Phillips dev_err(dev, "failed to allocate fifo submit count space\n"); 1909ec6644d6SKim Phillips err = -ENOMEM; 1910ec6644d6SKim Phillips goto err_out; 1911ec6644d6SKim Phillips } 1912ec6644d6SKim Phillips for (i = 0; i < priv->num_channels; i++) 19134b24ea97SVishnu Suresh atomic_set(&priv->submit_count[i], -(priv->chfifo_len - 1)); 1914ec6644d6SKim Phillips 19159c4a7965SKim Phillips priv->head = kzalloc(sizeof(int) * priv->num_channels, GFP_KERNEL); 19169c4a7965SKim Phillips priv->tail = kzalloc(sizeof(int) * priv->num_channels, GFP_KERNEL); 19179c4a7965SKim Phillips if (!priv->head || !priv->tail) { 19189c4a7965SKim Phillips dev_err(dev, "failed to allocate request index space\n"); 19199c4a7965SKim Phillips err = -ENOMEM; 19209c4a7965SKim Phillips goto err_out; 19219c4a7965SKim Phillips } 19229c4a7965SKim Phillips 19239c4a7965SKim Phillips /* reset and initialize the h/w */ 19249c4a7965SKim Phillips err = init_device(dev); 19259c4a7965SKim Phillips if (err) { 19269c4a7965SKim Phillips dev_err(dev, "failed to initialize device\n"); 19279c4a7965SKim Phillips goto err_out; 19289c4a7965SKim Phillips } 19299c4a7965SKim Phillips 19309c4a7965SKim Phillips /* register the RNG, if available */ 19319c4a7965SKim Phillips if (hw_supports(dev, DESC_HDR_SEL0_RNG)) { 19329c4a7965SKim Phillips err = talitos_register_rng(dev); 19339c4a7965SKim Phillips if (err) { 19349c4a7965SKim Phillips dev_err(dev, "failed to register hwrng: %d\n", err); 19359c4a7965SKim Phillips goto err_out; 19369c4a7965SKim Phillips } else 19379c4a7965SKim Phillips dev_info(dev, "hwrng\n"); 19389c4a7965SKim Phillips } 19399c4a7965SKim Phillips 19409c4a7965SKim Phillips /* register crypto algorithms the device supports */ 19419c4a7965SKim Phillips for (i = 0; i < ARRAY_SIZE(driver_algs); i++) { 19429c4a7965SKim Phillips if (hw_supports(dev, driver_algs[i].desc_hdr_template)) { 19439c4a7965SKim Phillips struct talitos_crypto_alg *t_alg; 19449c4a7965SKim Phillips 19459c4a7965SKim Phillips t_alg = talitos_alg_alloc(dev, &driver_algs[i]); 19469c4a7965SKim Phillips if (IS_ERR(t_alg)) { 19479c4a7965SKim Phillips err = PTR_ERR(t_alg); 19489c4a7965SKim Phillips goto err_out; 19499c4a7965SKim Phillips } 19509c4a7965SKim Phillips 19519c4a7965SKim Phillips err = crypto_register_alg(&t_alg->crypto_alg); 19529c4a7965SKim Phillips if (err) { 19539c4a7965SKim Phillips dev_err(dev, "%s alg registration failed\n", 19549c4a7965SKim Phillips t_alg->crypto_alg.cra_driver_name); 19559c4a7965SKim Phillips kfree(t_alg); 19569c4a7965SKim Phillips } else { 19579c4a7965SKim Phillips list_add_tail(&t_alg->entry, &priv->alg_list); 19589c4a7965SKim Phillips dev_info(dev, "%s\n", 19599c4a7965SKim Phillips t_alg->crypto_alg.cra_driver_name); 19609c4a7965SKim Phillips } 19619c4a7965SKim Phillips } 19629c4a7965SKim Phillips } 19639c4a7965SKim Phillips 19649c4a7965SKim Phillips return 0; 19659c4a7965SKim Phillips 19669c4a7965SKim Phillips err_out: 19679c4a7965SKim Phillips talitos_remove(ofdev); 19689c4a7965SKim Phillips 19699c4a7965SKim Phillips return err; 19709c4a7965SKim Phillips } 19719c4a7965SKim Phillips 19729c4a7965SKim Phillips static struct of_device_id talitos_match[] = { 19739c4a7965SKim Phillips { 19749c4a7965SKim Phillips .compatible = "fsl,sec2.0", 19759c4a7965SKim Phillips }, 19769c4a7965SKim Phillips {}, 19779c4a7965SKim Phillips }; 19789c4a7965SKim Phillips MODULE_DEVICE_TABLE(of, talitos_match); 19799c4a7965SKim Phillips 19809c4a7965SKim Phillips static struct of_platform_driver talitos_driver = { 19819c4a7965SKim Phillips .name = "talitos", 19829c4a7965SKim Phillips .match_table = talitos_match, 19839c4a7965SKim Phillips .probe = talitos_probe, 1984596f1034SAl Viro .remove = talitos_remove, 19859c4a7965SKim Phillips }; 19869c4a7965SKim Phillips 19879c4a7965SKim Phillips static int __init talitos_init(void) 19889c4a7965SKim Phillips { 19899c4a7965SKim Phillips return of_register_platform_driver(&talitos_driver); 19909c4a7965SKim Phillips } 19919c4a7965SKim Phillips module_init(talitos_init); 19929c4a7965SKim Phillips 19939c4a7965SKim Phillips static void __exit talitos_exit(void) 19949c4a7965SKim Phillips { 19959c4a7965SKim Phillips of_unregister_platform_driver(&talitos_driver); 19969c4a7965SKim Phillips } 19979c4a7965SKim Phillips module_exit(talitos_exit); 19989c4a7965SKim Phillips 19999c4a7965SKim Phillips MODULE_LICENSE("GPL"); 20009c4a7965SKim Phillips MODULE_AUTHOR("Kim Phillips <kim.phillips@freescale.com>"); 20019c4a7965SKim Phillips MODULE_DESCRIPTION("Freescale integrated security engine (SEC) driver"); 2002