Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Cryptographic API. |
| 3 | * |
| 4 | * Support for OMAP AES HW acceleration. |
| 5 | * |
| 6 | * Copyright (c) 2010 Nokia Corporation |
| 7 | * Author: Dmitry Kasatkin <dmitry.kasatkin@nokia.com> |
Mark A. Greer | 0d35583 | 2013-01-08 11:57:46 -0700 | [diff] [blame] | 8 | * Copyright (c) 2011 Texas Instruments Incorporated |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 9 | * |
| 10 | * This program is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License version 2 as published |
| 12 | * by the Free Software Foundation. |
| 13 | * |
| 14 | */ |
| 15 | |
Joel Fernandes | 016af9b | 2013-08-18 00:56:11 -0500 | [diff] [blame] | 16 | #define pr_fmt(fmt) "%20s: " fmt, __func__ |
| 17 | #define prn(num) pr_debug(#num "=%d\n", num) |
| 18 | #define prx(num) pr_debug(#num "=%x\n", num) |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 19 | |
| 20 | #include <linux/err.h> |
| 21 | #include <linux/module.h> |
| 22 | #include <linux/init.h> |
| 23 | #include <linux/errno.h> |
| 24 | #include <linux/kernel.h> |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 25 | #include <linux/platform_device.h> |
| 26 | #include <linux/scatterlist.h> |
| 27 | #include <linux/dma-mapping.h> |
Mark A. Greer | ebedbf7 | 2013-01-08 11:57:42 -0700 | [diff] [blame] | 28 | #include <linux/dmaengine.h> |
Mark A. Greer | 5946c4a | 2013-01-08 11:57:40 -0700 | [diff] [blame] | 29 | #include <linux/pm_runtime.h> |
Mark A. Greer | bc69d12 | 2013-01-08 11:57:44 -0700 | [diff] [blame] | 30 | #include <linux/of.h> |
| 31 | #include <linux/of_device.h> |
| 32 | #include <linux/of_address.h> |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 33 | #include <linux/io.h> |
| 34 | #include <linux/crypto.h> |
| 35 | #include <linux/interrupt.h> |
| 36 | #include <crypto/scatterwalk.h> |
| 37 | #include <crypto/aes.h> |
Corentin LABBE | 2589ad8 | 2016-08-31 14:02:57 +0200 | [diff] [blame] | 38 | #include <crypto/engine.h> |
Lokesh Vutla | 9fcb191 | 2016-08-04 13:28:44 +0300 | [diff] [blame] | 39 | #include <crypto/internal/skcipher.h> |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 40 | |
Mark A. Greer | ebedbf7 | 2013-01-08 11:57:42 -0700 | [diff] [blame] | 41 | #define DST_MAXBURST 4 |
| 42 | #define DMA_MIN (DST_MAXBURST * sizeof(u32)) |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 43 | |
Joel Fernandes | 1bf95cc | 2013-08-17 21:42:29 -0500 | [diff] [blame] | 44 | #define _calc_walked(inout) (dd->inout##_walk.offset - dd->inout##_sg->offset) |
| 45 | |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 46 | /* OMAP TRM gives bitfields as start:end, where start is the higher bit |
| 47 | number. For example 7:0 */ |
| 48 | #define FLD_MASK(start, end) (((1 << ((start) - (end) + 1)) - 1) << (end)) |
| 49 | #define FLD_VAL(val, start, end) (((val) << (end)) & FLD_MASK(start, end)) |
| 50 | |
Mark A. Greer | 0d35583 | 2013-01-08 11:57:46 -0700 | [diff] [blame] | 51 | #define AES_REG_KEY(dd, x) ((dd)->pdata->key_ofs - \ |
| 52 | ((x ^ 0x01) * 0x04)) |
| 53 | #define AES_REG_IV(dd, x) ((dd)->pdata->iv_ofs + ((x) * 0x04)) |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 54 | |
Mark A. Greer | 0d35583 | 2013-01-08 11:57:46 -0700 | [diff] [blame] | 55 | #define AES_REG_CTRL(dd) ((dd)->pdata->ctrl_ofs) |
Vutla, Lokesh | 340d9d3 | 2015-07-07 21:01:46 +0530 | [diff] [blame] | 56 | #define AES_REG_CTRL_CTR_WIDTH_MASK GENMASK(8, 7) |
| 57 | #define AES_REG_CTRL_CTR_WIDTH_32 0 |
| 58 | #define AES_REG_CTRL_CTR_WIDTH_64 BIT(7) |
| 59 | #define AES_REG_CTRL_CTR_WIDTH_96 BIT(8) |
| 60 | #define AES_REG_CTRL_CTR_WIDTH_128 GENMASK(8, 7) |
| 61 | #define AES_REG_CTRL_CTR BIT(6) |
| 62 | #define AES_REG_CTRL_CBC BIT(5) |
| 63 | #define AES_REG_CTRL_KEY_SIZE GENMASK(4, 3) |
| 64 | #define AES_REG_CTRL_DIRECTION BIT(2) |
| 65 | #define AES_REG_CTRL_INPUT_READY BIT(1) |
| 66 | #define AES_REG_CTRL_OUTPUT_READY BIT(0) |
Vutla, Lokesh | 5396c6c | 2015-07-07 21:01:45 +0530 | [diff] [blame] | 67 | #define AES_REG_CTRL_MASK GENMASK(24, 2) |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 68 | |
Mark A. Greer | 0d35583 | 2013-01-08 11:57:46 -0700 | [diff] [blame] | 69 | #define AES_REG_DATA_N(dd, x) ((dd)->pdata->data_ofs + ((x) * 0x04)) |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 70 | |
Mark A. Greer | 0d35583 | 2013-01-08 11:57:46 -0700 | [diff] [blame] | 71 | #define AES_REG_REV(dd) ((dd)->pdata->rev_ofs) |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 72 | |
Mark A. Greer | 0d35583 | 2013-01-08 11:57:46 -0700 | [diff] [blame] | 73 | #define AES_REG_MASK(dd) ((dd)->pdata->mask_ofs) |
Vutla, Lokesh | 340d9d3 | 2015-07-07 21:01:46 +0530 | [diff] [blame] | 74 | #define AES_REG_MASK_SIDLE BIT(6) |
| 75 | #define AES_REG_MASK_START BIT(5) |
| 76 | #define AES_REG_MASK_DMA_OUT_EN BIT(3) |
| 77 | #define AES_REG_MASK_DMA_IN_EN BIT(2) |
| 78 | #define AES_REG_MASK_SOFTRESET BIT(1) |
| 79 | #define AES_REG_AUTOIDLE BIT(0) |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 80 | |
Mark A. Greer | 0d35583 | 2013-01-08 11:57:46 -0700 | [diff] [blame] | 81 | #define AES_REG_LENGTH_N(x) (0x54 + ((x) * 0x04)) |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 82 | |
Joel Fernandes | 6721675 | 2013-08-17 21:42:28 -0500 | [diff] [blame] | 83 | #define AES_REG_IRQ_STATUS(dd) ((dd)->pdata->irq_status_ofs) |
| 84 | #define AES_REG_IRQ_ENABLE(dd) ((dd)->pdata->irq_enable_ofs) |
| 85 | #define AES_REG_IRQ_DATA_IN BIT(1) |
| 86 | #define AES_REG_IRQ_DATA_OUT BIT(2) |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 87 | #define DEFAULT_TIMEOUT (5*HZ) |
| 88 | |
Tero Kristo | f303b45 | 2016-08-04 13:28:42 +0300 | [diff] [blame] | 89 | #define DEFAULT_AUTOSUSPEND_DELAY 1000 |
| 90 | |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 91 | #define FLAGS_MODE_MASK 0x000f |
| 92 | #define FLAGS_ENCRYPT BIT(0) |
| 93 | #define FLAGS_CBC BIT(1) |
| 94 | #define FLAGS_GIV BIT(2) |
Mark A. Greer | f9fb69e | 2013-01-08 11:57:47 -0700 | [diff] [blame] | 95 | #define FLAGS_CTR BIT(3) |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 96 | |
Dmitry Kasatkin | 67a730c | 2010-11-30 10:13:30 +0200 | [diff] [blame] | 97 | #define FLAGS_INIT BIT(4) |
| 98 | #define FLAGS_FAST BIT(5) |
| 99 | #define FLAGS_BUSY BIT(6) |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 100 | |
Joel Fernandes | 1bf95cc | 2013-08-17 21:42:29 -0500 | [diff] [blame] | 101 | #define AES_BLOCK_WORDS (AES_BLOCK_SIZE >> 2) |
| 102 | |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 103 | struct omap_aes_ctx { |
| 104 | struct omap_aes_dev *dd; |
| 105 | |
| 106 | int keylen; |
| 107 | u32 key[AES_KEYSIZE_256 / sizeof(u32)]; |
| 108 | unsigned long flags; |
Lokesh Vutla | 9fcb191 | 2016-08-04 13:28:44 +0300 | [diff] [blame] | 109 | struct crypto_skcipher *fallback; |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 110 | }; |
| 111 | |
| 112 | struct omap_aes_reqctx { |
| 113 | unsigned long mode; |
| 114 | }; |
| 115 | |
| 116 | #define OMAP_AES_QUEUE_LENGTH 1 |
| 117 | #define OMAP_AES_CACHE_SIZE 0 |
| 118 | |
Mark A. Greer | f9fb69e | 2013-01-08 11:57:47 -0700 | [diff] [blame] | 119 | struct omap_aes_algs_info { |
| 120 | struct crypto_alg *algs_list; |
| 121 | unsigned int size; |
| 122 | unsigned int registered; |
| 123 | }; |
| 124 | |
Mark A. Greer | 0d35583 | 2013-01-08 11:57:46 -0700 | [diff] [blame] | 125 | struct omap_aes_pdata { |
Mark A. Greer | f9fb69e | 2013-01-08 11:57:47 -0700 | [diff] [blame] | 126 | struct omap_aes_algs_info *algs_info; |
| 127 | unsigned int algs_info_size; |
| 128 | |
Mark A. Greer | 0d35583 | 2013-01-08 11:57:46 -0700 | [diff] [blame] | 129 | void (*trigger)(struct omap_aes_dev *dd, int length); |
| 130 | |
| 131 | u32 key_ofs; |
| 132 | u32 iv_ofs; |
| 133 | u32 ctrl_ofs; |
| 134 | u32 data_ofs; |
| 135 | u32 rev_ofs; |
| 136 | u32 mask_ofs; |
Joel Fernandes | 6721675 | 2013-08-17 21:42:28 -0500 | [diff] [blame] | 137 | u32 irq_enable_ofs; |
| 138 | u32 irq_status_ofs; |
Mark A. Greer | 0d35583 | 2013-01-08 11:57:46 -0700 | [diff] [blame] | 139 | |
| 140 | u32 dma_enable_in; |
| 141 | u32 dma_enable_out; |
| 142 | u32 dma_start; |
| 143 | |
| 144 | u32 major_mask; |
| 145 | u32 major_shift; |
| 146 | u32 minor_mask; |
| 147 | u32 minor_shift; |
| 148 | }; |
| 149 | |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 150 | struct omap_aes_dev { |
| 151 | struct list_head list; |
| 152 | unsigned long phys_base; |
Dmitry Kasatkin | efce41b | 2010-11-30 10:13:32 +0200 | [diff] [blame] | 153 | void __iomem *io_base; |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 154 | struct omap_aes_ctx *ctx; |
| 155 | struct device *dev; |
| 156 | unsigned long flags; |
Dmitry Kasatkin | 21fe976 | 2010-11-30 10:13:29 +0200 | [diff] [blame] | 157 | int err; |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 158 | |
Dmitry Kasatkin | 21fe976 | 2010-11-30 10:13:29 +0200 | [diff] [blame] | 159 | struct tasklet_struct done_task; |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 160 | |
| 161 | struct ablkcipher_request *req; |
Baolin Wang | 0529900 | 2016-01-26 20:25:40 +0800 | [diff] [blame] | 162 | struct crypto_engine *engine; |
Joel Fernandes | 6242332 | 2013-08-17 21:42:32 -0500 | [diff] [blame] | 163 | |
| 164 | /* |
| 165 | * total is used by PIO mode for book keeping so introduce |
| 166 | * variable total_save as need it to calc page_order |
| 167 | */ |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 168 | size_t total; |
Joel Fernandes | 6242332 | 2013-08-17 21:42:32 -0500 | [diff] [blame] | 169 | size_t total_save; |
| 170 | |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 171 | struct scatterlist *in_sg; |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 172 | struct scatterlist *out_sg; |
Joel Fernandes | 6242332 | 2013-08-17 21:42:32 -0500 | [diff] [blame] | 173 | |
| 174 | /* Buffers for copying for unaligned cases */ |
| 175 | struct scatterlist in_sgl; |
| 176 | struct scatterlist out_sgl; |
| 177 | struct scatterlist *orig_out; |
| 178 | int sgs_copied; |
| 179 | |
Joel Fernandes | 1bf95cc | 2013-08-17 21:42:29 -0500 | [diff] [blame] | 180 | struct scatter_walk in_walk; |
| 181 | struct scatter_walk out_walk; |
Mark A. Greer | ebedbf7 | 2013-01-08 11:57:42 -0700 | [diff] [blame] | 182 | struct dma_chan *dma_lch_in; |
Mark A. Greer | ebedbf7 | 2013-01-08 11:57:42 -0700 | [diff] [blame] | 183 | struct dma_chan *dma_lch_out; |
Joel Fernandes | e77c756 | 2013-08-17 21:42:24 -0500 | [diff] [blame] | 184 | int in_sg_len; |
| 185 | int out_sg_len; |
Joel Fernandes | 98837ab | 2013-08-17 21:42:30 -0500 | [diff] [blame] | 186 | int pio_only; |
Mark A. Greer | 0d35583 | 2013-01-08 11:57:46 -0700 | [diff] [blame] | 187 | const struct omap_aes_pdata *pdata; |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 188 | }; |
| 189 | |
| 190 | /* keep registered devices data here */ |
| 191 | static LIST_HEAD(dev_list); |
| 192 | static DEFINE_SPINLOCK(list_lock); |
| 193 | |
Joel Fernandes | 016af9b | 2013-08-18 00:56:11 -0500 | [diff] [blame] | 194 | #ifdef DEBUG |
| 195 | #define omap_aes_read(dd, offset) \ |
| 196 | ({ \ |
| 197 | int _read_ret; \ |
| 198 | _read_ret = __raw_readl(dd->io_base + offset); \ |
| 199 | pr_debug("omap_aes_read(" #offset "=%#x)= %#x\n", \ |
| 200 | offset, _read_ret); \ |
| 201 | _read_ret; \ |
| 202 | }) |
| 203 | #else |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 204 | static inline u32 omap_aes_read(struct omap_aes_dev *dd, u32 offset) |
| 205 | { |
| 206 | return __raw_readl(dd->io_base + offset); |
| 207 | } |
Joel Fernandes | 016af9b | 2013-08-18 00:56:11 -0500 | [diff] [blame] | 208 | #endif |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 209 | |
Joel Fernandes | 016af9b | 2013-08-18 00:56:11 -0500 | [diff] [blame] | 210 | #ifdef DEBUG |
| 211 | #define omap_aes_write(dd, offset, value) \ |
| 212 | do { \ |
| 213 | pr_debug("omap_aes_write(" #offset "=%#x) value=%#x\n", \ |
| 214 | offset, value); \ |
| 215 | __raw_writel(value, dd->io_base + offset); \ |
| 216 | } while (0) |
| 217 | #else |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 218 | static inline void omap_aes_write(struct omap_aes_dev *dd, u32 offset, |
| 219 | u32 value) |
| 220 | { |
| 221 | __raw_writel(value, dd->io_base + offset); |
| 222 | } |
Joel Fernandes | 016af9b | 2013-08-18 00:56:11 -0500 | [diff] [blame] | 223 | #endif |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 224 | |
| 225 | static inline void omap_aes_write_mask(struct omap_aes_dev *dd, u32 offset, |
| 226 | u32 value, u32 mask) |
| 227 | { |
| 228 | u32 val; |
| 229 | |
| 230 | val = omap_aes_read(dd, offset); |
| 231 | val &= ~mask; |
| 232 | val |= value; |
| 233 | omap_aes_write(dd, offset, val); |
| 234 | } |
| 235 | |
| 236 | static void omap_aes_write_n(struct omap_aes_dev *dd, u32 offset, |
| 237 | u32 *value, int count) |
| 238 | { |
| 239 | for (; count--; value++, offset += 4) |
| 240 | omap_aes_write(dd, offset, *value); |
| 241 | } |
| 242 | |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 243 | static int omap_aes_hw_init(struct omap_aes_dev *dd) |
| 244 | { |
Tero Kristo | f303b45 | 2016-08-04 13:28:42 +0300 | [diff] [blame] | 245 | int err; |
| 246 | |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 247 | if (!(dd->flags & FLAGS_INIT)) { |
Dmitry Kasatkin | eeb2b20 | 2010-11-30 10:13:28 +0200 | [diff] [blame] | 248 | dd->flags |= FLAGS_INIT; |
Dmitry Kasatkin | 21fe976 | 2010-11-30 10:13:29 +0200 | [diff] [blame] | 249 | dd->err = 0; |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 250 | } |
| 251 | |
Tero Kristo | f303b45 | 2016-08-04 13:28:42 +0300 | [diff] [blame] | 252 | err = pm_runtime_get_sync(dd->dev); |
| 253 | if (err < 0) { |
| 254 | dev_err(dd->dev, "failed to get sync: %d\n", err); |
| 255 | return err; |
| 256 | } |
| 257 | |
Dmitry Kasatkin | eeb2b20 | 2010-11-30 10:13:28 +0200 | [diff] [blame] | 258 | return 0; |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 259 | } |
| 260 | |
Dmitry Kasatkin | 21fe976 | 2010-11-30 10:13:29 +0200 | [diff] [blame] | 261 | static int omap_aes_write_ctrl(struct omap_aes_dev *dd) |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 262 | { |
| 263 | unsigned int key32; |
Dmitry Kasatkin | 67a730c | 2010-11-30 10:13:30 +0200 | [diff] [blame] | 264 | int i, err; |
Vutla, Lokesh | 5396c6c | 2015-07-07 21:01:45 +0530 | [diff] [blame] | 265 | u32 val; |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 266 | |
Dmitry Kasatkin | 21fe976 | 2010-11-30 10:13:29 +0200 | [diff] [blame] | 267 | err = omap_aes_hw_init(dd); |
| 268 | if (err) |
| 269 | return err; |
| 270 | |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 271 | key32 = dd->ctx->keylen / sizeof(u32); |
Dmitry Kasatkin | 67a730c | 2010-11-30 10:13:30 +0200 | [diff] [blame] | 272 | |
| 273 | /* it seems a key should always be set even if it has not changed */ |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 274 | for (i = 0; i < key32; i++) { |
Mark A. Greer | 0d35583 | 2013-01-08 11:57:46 -0700 | [diff] [blame] | 275 | omap_aes_write(dd, AES_REG_KEY(dd, i), |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 276 | __le32_to_cpu(dd->ctx->key[i])); |
| 277 | } |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 278 | |
Mark A. Greer | f9fb69e | 2013-01-08 11:57:47 -0700 | [diff] [blame] | 279 | if ((dd->flags & (FLAGS_CBC | FLAGS_CTR)) && dd->req->info) |
Mark A. Greer | 0d35583 | 2013-01-08 11:57:46 -0700 | [diff] [blame] | 280 | omap_aes_write_n(dd, AES_REG_IV(dd, 0), dd->req->info, 4); |
Dmitry Kasatkin | 67a730c | 2010-11-30 10:13:30 +0200 | [diff] [blame] | 281 | |
| 282 | val = FLD_VAL(((dd->ctx->keylen >> 3) - 1), 4, 3); |
| 283 | if (dd->flags & FLAGS_CBC) |
| 284 | val |= AES_REG_CTRL_CBC; |
Vutla, Lokesh | 5396c6c | 2015-07-07 21:01:45 +0530 | [diff] [blame] | 285 | if (dd->flags & FLAGS_CTR) |
Joel Fernandes | 8ed49c7 | 2013-10-29 17:37:38 -0500 | [diff] [blame] | 286 | val |= AES_REG_CTRL_CTR | AES_REG_CTRL_CTR_WIDTH_128; |
Vutla, Lokesh | 5396c6c | 2015-07-07 21:01:45 +0530 | [diff] [blame] | 287 | |
Dmitry Kasatkin | 67a730c | 2010-11-30 10:13:30 +0200 | [diff] [blame] | 288 | if (dd->flags & FLAGS_ENCRYPT) |
| 289 | val |= AES_REG_CTRL_DIRECTION; |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 290 | |
Vutla, Lokesh | 5396c6c | 2015-07-07 21:01:45 +0530 | [diff] [blame] | 291 | omap_aes_write_mask(dd, AES_REG_CTRL(dd), val, AES_REG_CTRL_MASK); |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 292 | |
Dmitry Kasatkin | 21fe976 | 2010-11-30 10:13:29 +0200 | [diff] [blame] | 293 | return 0; |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 294 | } |
| 295 | |
Mark A. Greer | 0d35583 | 2013-01-08 11:57:46 -0700 | [diff] [blame] | 296 | static void omap_aes_dma_trigger_omap2(struct omap_aes_dev *dd, int length) |
| 297 | { |
| 298 | u32 mask, val; |
| 299 | |
| 300 | val = dd->pdata->dma_start; |
| 301 | |
| 302 | if (dd->dma_lch_out != NULL) |
| 303 | val |= dd->pdata->dma_enable_out; |
| 304 | if (dd->dma_lch_in != NULL) |
| 305 | val |= dd->pdata->dma_enable_in; |
| 306 | |
| 307 | mask = dd->pdata->dma_enable_out | dd->pdata->dma_enable_in | |
| 308 | dd->pdata->dma_start; |
| 309 | |
| 310 | omap_aes_write_mask(dd, AES_REG_MASK(dd), val, mask); |
| 311 | |
| 312 | } |
| 313 | |
| 314 | static void omap_aes_dma_trigger_omap4(struct omap_aes_dev *dd, int length) |
| 315 | { |
| 316 | omap_aes_write(dd, AES_REG_LENGTH_N(0), length); |
| 317 | omap_aes_write(dd, AES_REG_LENGTH_N(1), 0); |
| 318 | |
| 319 | omap_aes_dma_trigger_omap2(dd, length); |
| 320 | } |
| 321 | |
| 322 | static void omap_aes_dma_stop(struct omap_aes_dev *dd) |
| 323 | { |
| 324 | u32 mask; |
| 325 | |
| 326 | mask = dd->pdata->dma_enable_out | dd->pdata->dma_enable_in | |
| 327 | dd->pdata->dma_start; |
| 328 | |
| 329 | omap_aes_write_mask(dd, AES_REG_MASK(dd), 0, mask); |
| 330 | } |
| 331 | |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 332 | static struct omap_aes_dev *omap_aes_find_dev(struct omap_aes_ctx *ctx) |
| 333 | { |
Lokesh Vutla | 164f3ef | 2016-08-04 13:28:43 +0300 | [diff] [blame] | 334 | struct omap_aes_dev *dd; |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 335 | |
| 336 | spin_lock_bh(&list_lock); |
Lokesh Vutla | 164f3ef | 2016-08-04 13:28:43 +0300 | [diff] [blame] | 337 | dd = list_first_entry(&dev_list, struct omap_aes_dev, list); |
| 338 | list_move_tail(&dd->list, &dev_list); |
| 339 | ctx->dd = dd; |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 340 | spin_unlock_bh(&list_lock); |
| 341 | |
| 342 | return dd; |
| 343 | } |
| 344 | |
Mark A. Greer | ebedbf7 | 2013-01-08 11:57:42 -0700 | [diff] [blame] | 345 | static void omap_aes_dma_out_callback(void *data) |
| 346 | { |
| 347 | struct omap_aes_dev *dd = data; |
| 348 | |
| 349 | /* dma_lch_out - completed */ |
| 350 | tasklet_schedule(&dd->done_task); |
| 351 | } |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 352 | |
| 353 | static int omap_aes_dma_init(struct omap_aes_dev *dd) |
| 354 | { |
Peter Ujfalusi | da8b29a | 2016-04-29 16:02:18 +0300 | [diff] [blame] | 355 | int err; |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 356 | |
Mark A. Greer | ebedbf7 | 2013-01-08 11:57:42 -0700 | [diff] [blame] | 357 | dd->dma_lch_out = NULL; |
| 358 | dd->dma_lch_in = NULL; |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 359 | |
Peter Ujfalusi | da8b29a | 2016-04-29 16:02:18 +0300 | [diff] [blame] | 360 | dd->dma_lch_in = dma_request_chan(dd->dev, "rx"); |
| 361 | if (IS_ERR(dd->dma_lch_in)) { |
Mark A. Greer | ebedbf7 | 2013-01-08 11:57:42 -0700 | [diff] [blame] | 362 | dev_err(dd->dev, "Unable to request in DMA channel\n"); |
Peter Ujfalusi | da8b29a | 2016-04-29 16:02:18 +0300 | [diff] [blame] | 363 | return PTR_ERR(dd->dma_lch_in); |
Mark A. Greer | ebedbf7 | 2013-01-08 11:57:42 -0700 | [diff] [blame] | 364 | } |
| 365 | |
Peter Ujfalusi | da8b29a | 2016-04-29 16:02:18 +0300 | [diff] [blame] | 366 | dd->dma_lch_out = dma_request_chan(dd->dev, "tx"); |
| 367 | if (IS_ERR(dd->dma_lch_out)) { |
Mark A. Greer | ebedbf7 | 2013-01-08 11:57:42 -0700 | [diff] [blame] | 368 | dev_err(dd->dev, "Unable to request out DMA channel\n"); |
Peter Ujfalusi | da8b29a | 2016-04-29 16:02:18 +0300 | [diff] [blame] | 369 | err = PTR_ERR(dd->dma_lch_out); |
Mark A. Greer | ebedbf7 | 2013-01-08 11:57:42 -0700 | [diff] [blame] | 370 | goto err_dma_out; |
| 371 | } |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 372 | |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 373 | return 0; |
| 374 | |
| 375 | err_dma_out: |
Mark A. Greer | ebedbf7 | 2013-01-08 11:57:42 -0700 | [diff] [blame] | 376 | dma_release_channel(dd->dma_lch_in); |
Peter Ujfalusi | da8b29a | 2016-04-29 16:02:18 +0300 | [diff] [blame] | 377 | |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 378 | return err; |
| 379 | } |
| 380 | |
| 381 | static void omap_aes_dma_cleanup(struct omap_aes_dev *dd) |
| 382 | { |
Peter Ujfalusi | da8b29a | 2016-04-29 16:02:18 +0300 | [diff] [blame] | 383 | if (dd->pio_only) |
| 384 | return; |
| 385 | |
Mark A. Greer | ebedbf7 | 2013-01-08 11:57:42 -0700 | [diff] [blame] | 386 | dma_release_channel(dd->dma_lch_out); |
| 387 | dma_release_channel(dd->dma_lch_in); |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | static void sg_copy_buf(void *buf, struct scatterlist *sg, |
| 391 | unsigned int start, unsigned int nbytes, int out) |
| 392 | { |
| 393 | struct scatter_walk walk; |
| 394 | |
| 395 | if (!nbytes) |
| 396 | return; |
| 397 | |
| 398 | scatterwalk_start(&walk, sg); |
| 399 | scatterwalk_advance(&walk, start); |
| 400 | scatterwalk_copychunks(buf, &walk, nbytes, out); |
| 401 | scatterwalk_done(&walk, out, 0); |
| 402 | } |
| 403 | |
Mark A. Greer | ebedbf7 | 2013-01-08 11:57:42 -0700 | [diff] [blame] | 404 | static int omap_aes_crypt_dma(struct crypto_tfm *tfm, |
Joel Fernandes | 4b645c9 | 2013-08-17 21:42:25 -0500 | [diff] [blame] | 405 | struct scatterlist *in_sg, struct scatterlist *out_sg, |
| 406 | int in_sg_len, int out_sg_len) |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 407 | { |
| 408 | struct omap_aes_ctx *ctx = crypto_tfm_ctx(tfm); |
| 409 | struct omap_aes_dev *dd = ctx->dd; |
Mark A. Greer | ebedbf7 | 2013-01-08 11:57:42 -0700 | [diff] [blame] | 410 | struct dma_async_tx_descriptor *tx_in, *tx_out; |
| 411 | struct dma_slave_config cfg; |
Joel Fernandes | 4b645c9 | 2013-08-17 21:42:25 -0500 | [diff] [blame] | 412 | int ret; |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 413 | |
Joel Fernandes | 98837ab | 2013-08-17 21:42:30 -0500 | [diff] [blame] | 414 | if (dd->pio_only) { |
| 415 | scatterwalk_start(&dd->in_walk, dd->in_sg); |
| 416 | scatterwalk_start(&dd->out_walk, dd->out_sg); |
| 417 | |
| 418 | /* Enable DATAIN interrupt and let it take |
| 419 | care of the rest */ |
| 420 | omap_aes_write(dd, AES_REG_IRQ_ENABLE(dd), 0x2); |
| 421 | return 0; |
| 422 | } |
| 423 | |
Joel Fernandes | 0a64171 | 2013-08-17 21:42:26 -0500 | [diff] [blame] | 424 | dma_sync_sg_for_device(dd->dev, dd->in_sg, in_sg_len, DMA_TO_DEVICE); |
| 425 | |
Mark A. Greer | ebedbf7 | 2013-01-08 11:57:42 -0700 | [diff] [blame] | 426 | memset(&cfg, 0, sizeof(cfg)); |
| 427 | |
Mark A. Greer | 0d35583 | 2013-01-08 11:57:46 -0700 | [diff] [blame] | 428 | cfg.src_addr = dd->phys_base + AES_REG_DATA_N(dd, 0); |
| 429 | cfg.dst_addr = dd->phys_base + AES_REG_DATA_N(dd, 0); |
Mark A. Greer | ebedbf7 | 2013-01-08 11:57:42 -0700 | [diff] [blame] | 430 | cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES; |
| 431 | cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES; |
| 432 | cfg.src_maxburst = DST_MAXBURST; |
| 433 | cfg.dst_maxburst = DST_MAXBURST; |
| 434 | |
| 435 | /* IN */ |
| 436 | ret = dmaengine_slave_config(dd->dma_lch_in, &cfg); |
| 437 | if (ret) { |
| 438 | dev_err(dd->dev, "can't configure IN dmaengine slave: %d\n", |
| 439 | ret); |
| 440 | return ret; |
| 441 | } |
| 442 | |
Joel Fernandes | 4b645c9 | 2013-08-17 21:42:25 -0500 | [diff] [blame] | 443 | tx_in = dmaengine_prep_slave_sg(dd->dma_lch_in, in_sg, in_sg_len, |
Mark A. Greer | ebedbf7 | 2013-01-08 11:57:42 -0700 | [diff] [blame] | 444 | DMA_MEM_TO_DEV, |
| 445 | DMA_PREP_INTERRUPT | DMA_CTRL_ACK); |
| 446 | if (!tx_in) { |
| 447 | dev_err(dd->dev, "IN prep_slave_sg() failed\n"); |
| 448 | return -EINVAL; |
| 449 | } |
| 450 | |
| 451 | /* No callback necessary */ |
| 452 | tx_in->callback_param = dd; |
| 453 | |
| 454 | /* OUT */ |
| 455 | ret = dmaengine_slave_config(dd->dma_lch_out, &cfg); |
| 456 | if (ret) { |
| 457 | dev_err(dd->dev, "can't configure OUT dmaengine slave: %d\n", |
| 458 | ret); |
| 459 | return ret; |
| 460 | } |
| 461 | |
Joel Fernandes | 4b645c9 | 2013-08-17 21:42:25 -0500 | [diff] [blame] | 462 | tx_out = dmaengine_prep_slave_sg(dd->dma_lch_out, out_sg, out_sg_len, |
Mark A. Greer | ebedbf7 | 2013-01-08 11:57:42 -0700 | [diff] [blame] | 463 | DMA_DEV_TO_MEM, |
| 464 | DMA_PREP_INTERRUPT | DMA_CTRL_ACK); |
| 465 | if (!tx_out) { |
| 466 | dev_err(dd->dev, "OUT prep_slave_sg() failed\n"); |
| 467 | return -EINVAL; |
| 468 | } |
| 469 | |
| 470 | tx_out->callback = omap_aes_dma_out_callback; |
| 471 | tx_out->callback_param = dd; |
| 472 | |
| 473 | dmaengine_submit(tx_in); |
| 474 | dmaengine_submit(tx_out); |
| 475 | |
| 476 | dma_async_issue_pending(dd->dma_lch_in); |
| 477 | dma_async_issue_pending(dd->dma_lch_out); |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 478 | |
Mark A. Greer | 0d35583 | 2013-01-08 11:57:46 -0700 | [diff] [blame] | 479 | /* start DMA */ |
Joel Fernandes | 4b645c9 | 2013-08-17 21:42:25 -0500 | [diff] [blame] | 480 | dd->pdata->trigger(dd, dd->total); |
Dmitry Kasatkin | 83ea7e0 | 2010-11-30 10:13:31 +0200 | [diff] [blame] | 481 | |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 482 | return 0; |
| 483 | } |
| 484 | |
| 485 | static int omap_aes_crypt_dma_start(struct omap_aes_dev *dd) |
| 486 | { |
| 487 | struct crypto_tfm *tfm = crypto_ablkcipher_tfm( |
| 488 | crypto_ablkcipher_reqtfm(dd->req)); |
Joel Fernandes | 4b645c9 | 2013-08-17 21:42:25 -0500 | [diff] [blame] | 489 | int err; |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 490 | |
| 491 | pr_debug("total: %d\n", dd->total); |
| 492 | |
Joel Fernandes | 98837ab | 2013-08-17 21:42:30 -0500 | [diff] [blame] | 493 | if (!dd->pio_only) { |
| 494 | err = dma_map_sg(dd->dev, dd->in_sg, dd->in_sg_len, |
| 495 | DMA_TO_DEVICE); |
| 496 | if (!err) { |
| 497 | dev_err(dd->dev, "dma_map_sg() error\n"); |
| 498 | return -EINVAL; |
| 499 | } |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 500 | |
Joel Fernandes | 98837ab | 2013-08-17 21:42:30 -0500 | [diff] [blame] | 501 | err = dma_map_sg(dd->dev, dd->out_sg, dd->out_sg_len, |
| 502 | DMA_FROM_DEVICE); |
| 503 | if (!err) { |
| 504 | dev_err(dd->dev, "dma_map_sg() error\n"); |
| 505 | return -EINVAL; |
| 506 | } |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 507 | } |
| 508 | |
Joel Fernandes | 4b645c9 | 2013-08-17 21:42:25 -0500 | [diff] [blame] | 509 | err = omap_aes_crypt_dma(tfm, dd->in_sg, dd->out_sg, dd->in_sg_len, |
| 510 | dd->out_sg_len); |
Joel Fernandes | 98837ab | 2013-08-17 21:42:30 -0500 | [diff] [blame] | 511 | if (err && !dd->pio_only) { |
Joel Fernandes | 4b645c9 | 2013-08-17 21:42:25 -0500 | [diff] [blame] | 512 | dma_unmap_sg(dd->dev, dd->in_sg, dd->in_sg_len, DMA_TO_DEVICE); |
| 513 | dma_unmap_sg(dd->dev, dd->out_sg, dd->out_sg_len, |
| 514 | DMA_FROM_DEVICE); |
Dmitry Kasatkin | 21fe976 | 2010-11-30 10:13:29 +0200 | [diff] [blame] | 515 | } |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 516 | |
| 517 | return err; |
| 518 | } |
| 519 | |
| 520 | static void omap_aes_finish_req(struct omap_aes_dev *dd, int err) |
| 521 | { |
Dmitry Kasatkin | 21fe976 | 2010-11-30 10:13:29 +0200 | [diff] [blame] | 522 | struct ablkcipher_request *req = dd->req; |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 523 | |
| 524 | pr_debug("err: %d\n", err); |
| 525 | |
Corentin LABBE | 4cba7cf | 2016-08-31 14:02:58 +0200 | [diff] [blame] | 526 | crypto_finalize_cipher_request(dd->engine, req, err); |
Tero Kristo | f303b45 | 2016-08-04 13:28:42 +0300 | [diff] [blame] | 527 | |
| 528 | pm_runtime_mark_last_busy(dd->dev); |
| 529 | pm_runtime_put_autosuspend(dd->dev); |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 530 | } |
| 531 | |
| 532 | static int omap_aes_crypt_dma_stop(struct omap_aes_dev *dd) |
| 533 | { |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 534 | pr_debug("total: %d\n", dd->total); |
| 535 | |
Mark A. Greer | 0d35583 | 2013-01-08 11:57:46 -0700 | [diff] [blame] | 536 | omap_aes_dma_stop(dd); |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 537 | |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 538 | |
Rahul Pathak | 16f080a | 2015-12-14 08:45:23 +0000 | [diff] [blame] | 539 | return 0; |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 540 | } |
| 541 | |
Vutla, Lokesh | 6d7e7e0 | 2015-03-31 09:52:25 +0530 | [diff] [blame] | 542 | static int omap_aes_check_aligned(struct scatterlist *sg, int total) |
Joel Fernandes | 6242332 | 2013-08-17 21:42:32 -0500 | [diff] [blame] | 543 | { |
Vutla, Lokesh | 6d7e7e0 | 2015-03-31 09:52:25 +0530 | [diff] [blame] | 544 | int len = 0; |
| 545 | |
Vutla, Lokesh | 310b0d5 | 2015-07-07 21:01:43 +0530 | [diff] [blame] | 546 | if (!IS_ALIGNED(total, AES_BLOCK_SIZE)) |
| 547 | return -EINVAL; |
| 548 | |
Joel Fernandes | 6242332 | 2013-08-17 21:42:32 -0500 | [diff] [blame] | 549 | while (sg) { |
| 550 | if (!IS_ALIGNED(sg->offset, 4)) |
| 551 | return -1; |
| 552 | if (!IS_ALIGNED(sg->length, AES_BLOCK_SIZE)) |
| 553 | return -1; |
Vutla, Lokesh | 6d7e7e0 | 2015-03-31 09:52:25 +0530 | [diff] [blame] | 554 | |
| 555 | len += sg->length; |
Joel Fernandes | 6242332 | 2013-08-17 21:42:32 -0500 | [diff] [blame] | 556 | sg = sg_next(sg); |
| 557 | } |
Vutla, Lokesh | 6d7e7e0 | 2015-03-31 09:52:25 +0530 | [diff] [blame] | 558 | |
| 559 | if (len != total) |
| 560 | return -1; |
| 561 | |
Joel Fernandes | 6242332 | 2013-08-17 21:42:32 -0500 | [diff] [blame] | 562 | return 0; |
| 563 | } |
| 564 | |
Sachin Kamat | 034568e | 2013-09-13 17:06:46 +0530 | [diff] [blame] | 565 | static int omap_aes_copy_sgs(struct omap_aes_dev *dd) |
Joel Fernandes | 6242332 | 2013-08-17 21:42:32 -0500 | [diff] [blame] | 566 | { |
| 567 | void *buf_in, *buf_out; |
Vutla, Lokesh | 310b0d5 | 2015-07-07 21:01:43 +0530 | [diff] [blame] | 568 | int pages, total; |
Joel Fernandes | 6242332 | 2013-08-17 21:42:32 -0500 | [diff] [blame] | 569 | |
Vutla, Lokesh | 310b0d5 | 2015-07-07 21:01:43 +0530 | [diff] [blame] | 570 | total = ALIGN(dd->total, AES_BLOCK_SIZE); |
| 571 | pages = get_order(total); |
Joel Fernandes | 6242332 | 2013-08-17 21:42:32 -0500 | [diff] [blame] | 572 | |
| 573 | buf_in = (void *)__get_free_pages(GFP_ATOMIC, pages); |
| 574 | buf_out = (void *)__get_free_pages(GFP_ATOMIC, pages); |
| 575 | |
| 576 | if (!buf_in || !buf_out) { |
| 577 | pr_err("Couldn't allocated pages for unaligned cases.\n"); |
| 578 | return -1; |
| 579 | } |
| 580 | |
| 581 | dd->orig_out = dd->out_sg; |
| 582 | |
| 583 | sg_copy_buf(buf_in, dd->in_sg, 0, dd->total, 0); |
| 584 | |
| 585 | sg_init_table(&dd->in_sgl, 1); |
Vutla, Lokesh | 310b0d5 | 2015-07-07 21:01:43 +0530 | [diff] [blame] | 586 | sg_set_buf(&dd->in_sgl, buf_in, total); |
Joel Fernandes | 6242332 | 2013-08-17 21:42:32 -0500 | [diff] [blame] | 587 | dd->in_sg = &dd->in_sgl; |
Herbert Xu | 7c001a8 | 2016-07-12 13:17:52 +0800 | [diff] [blame] | 588 | dd->in_sg_len = 1; |
Joel Fernandes | 6242332 | 2013-08-17 21:42:32 -0500 | [diff] [blame] | 589 | |
| 590 | sg_init_table(&dd->out_sgl, 1); |
Vutla, Lokesh | 310b0d5 | 2015-07-07 21:01:43 +0530 | [diff] [blame] | 591 | sg_set_buf(&dd->out_sgl, buf_out, total); |
Joel Fernandes | 6242332 | 2013-08-17 21:42:32 -0500 | [diff] [blame] | 592 | dd->out_sg = &dd->out_sgl; |
Herbert Xu | 7c001a8 | 2016-07-12 13:17:52 +0800 | [diff] [blame] | 593 | dd->out_sg_len = 1; |
Joel Fernandes | 6242332 | 2013-08-17 21:42:32 -0500 | [diff] [blame] | 594 | |
| 595 | return 0; |
| 596 | } |
| 597 | |
Dmitry Kasatkin | 21fe976 | 2010-11-30 10:13:29 +0200 | [diff] [blame] | 598 | static int omap_aes_handle_queue(struct omap_aes_dev *dd, |
Baolin Wang | 0529900 | 2016-01-26 20:25:40 +0800 | [diff] [blame] | 599 | struct ablkcipher_request *req) |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 600 | { |
Dmitry Kasatkin | eeb2b20 | 2010-11-30 10:13:28 +0200 | [diff] [blame] | 601 | if (req) |
Corentin LABBE | 4cba7cf | 2016-08-31 14:02:58 +0200 | [diff] [blame] | 602 | return crypto_transfer_cipher_request_to_engine(dd->engine, req); |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 603 | |
Baolin Wang | 0529900 | 2016-01-26 20:25:40 +0800 | [diff] [blame] | 604 | return 0; |
| 605 | } |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 606 | |
Baolin Wang | 0529900 | 2016-01-26 20:25:40 +0800 | [diff] [blame] | 607 | static int omap_aes_prepare_req(struct crypto_engine *engine, |
| 608 | struct ablkcipher_request *req) |
| 609 | { |
| 610 | struct omap_aes_ctx *ctx = crypto_ablkcipher_ctx( |
| 611 | crypto_ablkcipher_reqtfm(req)); |
Lokesh Vutla | 164f3ef | 2016-08-04 13:28:43 +0300 | [diff] [blame] | 612 | struct omap_aes_dev *dd = ctx->dd; |
Baolin Wang | 0529900 | 2016-01-26 20:25:40 +0800 | [diff] [blame] | 613 | struct omap_aes_reqctx *rctx; |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 614 | |
Baolin Wang | 0529900 | 2016-01-26 20:25:40 +0800 | [diff] [blame] | 615 | if (!dd) |
| 616 | return -ENODEV; |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 617 | |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 618 | /* assign new request to device */ |
| 619 | dd->req = req; |
| 620 | dd->total = req->nbytes; |
Joel Fernandes | 6242332 | 2013-08-17 21:42:32 -0500 | [diff] [blame] | 621 | dd->total_save = req->nbytes; |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 622 | dd->in_sg = req->src; |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 623 | dd->out_sg = req->dst; |
| 624 | |
Herbert Xu | 7c001a8 | 2016-07-12 13:17:52 +0800 | [diff] [blame] | 625 | dd->in_sg_len = sg_nents_for_len(dd->in_sg, dd->total); |
| 626 | if (dd->in_sg_len < 0) |
| 627 | return dd->in_sg_len; |
| 628 | |
| 629 | dd->out_sg_len = sg_nents_for_len(dd->out_sg, dd->total); |
| 630 | if (dd->out_sg_len < 0) |
| 631 | return dd->out_sg_len; |
| 632 | |
Vutla, Lokesh | 6d7e7e0 | 2015-03-31 09:52:25 +0530 | [diff] [blame] | 633 | if (omap_aes_check_aligned(dd->in_sg, dd->total) || |
| 634 | omap_aes_check_aligned(dd->out_sg, dd->total)) { |
Joel Fernandes | 6242332 | 2013-08-17 21:42:32 -0500 | [diff] [blame] | 635 | if (omap_aes_copy_sgs(dd)) |
| 636 | pr_err("Failed to copy SGs for unaligned cases\n"); |
| 637 | dd->sgs_copied = 1; |
| 638 | } else { |
| 639 | dd->sgs_copied = 0; |
| 640 | } |
| 641 | |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 642 | rctx = ablkcipher_request_ctx(req); |
| 643 | ctx = crypto_ablkcipher_ctx(crypto_ablkcipher_reqtfm(req)); |
| 644 | rctx->mode &= FLAGS_MODE_MASK; |
| 645 | dd->flags = (dd->flags & ~FLAGS_MODE_MASK) | rctx->mode; |
| 646 | |
Dmitry Kasatkin | 67a730c | 2010-11-30 10:13:30 +0200 | [diff] [blame] | 647 | dd->ctx = ctx; |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 648 | ctx->dd = dd; |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 649 | |
Baolin Wang | 0529900 | 2016-01-26 20:25:40 +0800 | [diff] [blame] | 650 | return omap_aes_write_ctrl(dd); |
| 651 | } |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 652 | |
Baolin Wang | 0529900 | 2016-01-26 20:25:40 +0800 | [diff] [blame] | 653 | static int omap_aes_crypt_req(struct crypto_engine *engine, |
| 654 | struct ablkcipher_request *req) |
| 655 | { |
| 656 | struct omap_aes_ctx *ctx = crypto_ablkcipher_ctx( |
| 657 | crypto_ablkcipher_reqtfm(req)); |
Lokesh Vutla | 164f3ef | 2016-08-04 13:28:43 +0300 | [diff] [blame] | 658 | struct omap_aes_dev *dd = ctx->dd; |
Baolin Wang | 0529900 | 2016-01-26 20:25:40 +0800 | [diff] [blame] | 659 | |
| 660 | if (!dd) |
| 661 | return -ENODEV; |
| 662 | |
| 663 | return omap_aes_crypt_dma_start(dd); |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 664 | } |
| 665 | |
Dmitry Kasatkin | 21fe976 | 2010-11-30 10:13:29 +0200 | [diff] [blame] | 666 | static void omap_aes_done_task(unsigned long data) |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 667 | { |
| 668 | struct omap_aes_dev *dd = (struct omap_aes_dev *)data; |
Joel Fernandes | 6242332 | 2013-08-17 21:42:32 -0500 | [diff] [blame] | 669 | void *buf_in, *buf_out; |
Vutla, Lokesh | 310b0d5 | 2015-07-07 21:01:43 +0530 | [diff] [blame] | 670 | int pages, len; |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 671 | |
Joel Fernandes | 4b645c9 | 2013-08-17 21:42:25 -0500 | [diff] [blame] | 672 | pr_debug("enter done_task\n"); |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 673 | |
Joel Fernandes | 98837ab | 2013-08-17 21:42:30 -0500 | [diff] [blame] | 674 | if (!dd->pio_only) { |
| 675 | dma_sync_sg_for_device(dd->dev, dd->out_sg, dd->out_sg_len, |
| 676 | DMA_FROM_DEVICE); |
Joel Fernandes | 6242332 | 2013-08-17 21:42:32 -0500 | [diff] [blame] | 677 | dma_unmap_sg(dd->dev, dd->in_sg, dd->in_sg_len, DMA_TO_DEVICE); |
| 678 | dma_unmap_sg(dd->dev, dd->out_sg, dd->out_sg_len, |
| 679 | DMA_FROM_DEVICE); |
Joel Fernandes | 98837ab | 2013-08-17 21:42:30 -0500 | [diff] [blame] | 680 | omap_aes_crypt_dma_stop(dd); |
| 681 | } |
Joel Fernandes | 6242332 | 2013-08-17 21:42:32 -0500 | [diff] [blame] | 682 | |
| 683 | if (dd->sgs_copied) { |
| 684 | buf_in = sg_virt(&dd->in_sgl); |
| 685 | buf_out = sg_virt(&dd->out_sgl); |
| 686 | |
| 687 | sg_copy_buf(buf_out, dd->orig_out, 0, dd->total_save, 1); |
| 688 | |
Vutla, Lokesh | 310b0d5 | 2015-07-07 21:01:43 +0530 | [diff] [blame] | 689 | len = ALIGN(dd->total_save, AES_BLOCK_SIZE); |
| 690 | pages = get_order(len); |
Joel Fernandes | 6242332 | 2013-08-17 21:42:32 -0500 | [diff] [blame] | 691 | free_pages((unsigned long)buf_in, pages); |
| 692 | free_pages((unsigned long)buf_out, pages); |
| 693 | } |
| 694 | |
Joel Fernandes | 4b645c9 | 2013-08-17 21:42:25 -0500 | [diff] [blame] | 695 | omap_aes_finish_req(dd, 0); |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 696 | |
| 697 | pr_debug("exit\n"); |
| 698 | } |
| 699 | |
| 700 | static int omap_aes_crypt(struct ablkcipher_request *req, unsigned long mode) |
| 701 | { |
| 702 | struct omap_aes_ctx *ctx = crypto_ablkcipher_ctx( |
| 703 | crypto_ablkcipher_reqtfm(req)); |
| 704 | struct omap_aes_reqctx *rctx = ablkcipher_request_ctx(req); |
| 705 | struct omap_aes_dev *dd; |
Lokesh Vutla | 9fcb191 | 2016-08-04 13:28:44 +0300 | [diff] [blame] | 706 | int ret; |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 707 | |
| 708 | pr_debug("nbytes: %d, enc: %d, cbc: %d\n", req->nbytes, |
| 709 | !!(mode & FLAGS_ENCRYPT), |
| 710 | !!(mode & FLAGS_CBC)); |
| 711 | |
Lokesh Vutla | 9fcb191 | 2016-08-04 13:28:44 +0300 | [diff] [blame] | 712 | if (req->nbytes < 200) { |
| 713 | SKCIPHER_REQUEST_ON_STACK(subreq, ctx->fallback); |
| 714 | |
| 715 | skcipher_request_set_tfm(subreq, ctx->fallback); |
| 716 | skcipher_request_set_callback(subreq, req->base.flags, NULL, |
| 717 | NULL); |
| 718 | skcipher_request_set_crypt(subreq, req->src, req->dst, |
| 719 | req->nbytes, req->info); |
| 720 | |
| 721 | if (mode & FLAGS_ENCRYPT) |
| 722 | ret = crypto_skcipher_encrypt(subreq); |
| 723 | else |
| 724 | ret = crypto_skcipher_decrypt(subreq); |
| 725 | |
| 726 | skcipher_request_zero(subreq); |
| 727 | return ret; |
| 728 | } |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 729 | dd = omap_aes_find_dev(ctx); |
| 730 | if (!dd) |
| 731 | return -ENODEV; |
| 732 | |
| 733 | rctx->mode = mode; |
| 734 | |
Dmitry Kasatkin | 21fe976 | 2010-11-30 10:13:29 +0200 | [diff] [blame] | 735 | return omap_aes_handle_queue(dd, req); |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 736 | } |
| 737 | |
| 738 | /* ********************** ALG API ************************************ */ |
| 739 | |
| 740 | static int omap_aes_setkey(struct crypto_ablkcipher *tfm, const u8 *key, |
| 741 | unsigned int keylen) |
| 742 | { |
| 743 | struct omap_aes_ctx *ctx = crypto_ablkcipher_ctx(tfm); |
Lokesh Vutla | 9fcb191 | 2016-08-04 13:28:44 +0300 | [diff] [blame] | 744 | int ret; |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 745 | |
| 746 | if (keylen != AES_KEYSIZE_128 && keylen != AES_KEYSIZE_192 && |
| 747 | keylen != AES_KEYSIZE_256) |
| 748 | return -EINVAL; |
| 749 | |
| 750 | pr_debug("enter, keylen: %d\n", keylen); |
| 751 | |
| 752 | memcpy(ctx->key, key, keylen); |
| 753 | ctx->keylen = keylen; |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 754 | |
Lokesh Vutla | 9fcb191 | 2016-08-04 13:28:44 +0300 | [diff] [blame] | 755 | crypto_skcipher_clear_flags(ctx->fallback, CRYPTO_TFM_REQ_MASK); |
| 756 | crypto_skcipher_set_flags(ctx->fallback, tfm->base.crt_flags & |
| 757 | CRYPTO_TFM_REQ_MASK); |
| 758 | |
| 759 | ret = crypto_skcipher_setkey(ctx->fallback, key, keylen); |
| 760 | if (!ret) |
| 761 | return 0; |
| 762 | |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 763 | return 0; |
| 764 | } |
| 765 | |
| 766 | static int omap_aes_ecb_encrypt(struct ablkcipher_request *req) |
| 767 | { |
| 768 | return omap_aes_crypt(req, FLAGS_ENCRYPT); |
| 769 | } |
| 770 | |
| 771 | static int omap_aes_ecb_decrypt(struct ablkcipher_request *req) |
| 772 | { |
| 773 | return omap_aes_crypt(req, 0); |
| 774 | } |
| 775 | |
| 776 | static int omap_aes_cbc_encrypt(struct ablkcipher_request *req) |
| 777 | { |
| 778 | return omap_aes_crypt(req, FLAGS_ENCRYPT | FLAGS_CBC); |
| 779 | } |
| 780 | |
| 781 | static int omap_aes_cbc_decrypt(struct ablkcipher_request *req) |
| 782 | { |
| 783 | return omap_aes_crypt(req, FLAGS_CBC); |
| 784 | } |
| 785 | |
Mark A. Greer | f9fb69e | 2013-01-08 11:57:47 -0700 | [diff] [blame] | 786 | static int omap_aes_ctr_encrypt(struct ablkcipher_request *req) |
| 787 | { |
| 788 | return omap_aes_crypt(req, FLAGS_ENCRYPT | FLAGS_CTR); |
| 789 | } |
| 790 | |
| 791 | static int omap_aes_ctr_decrypt(struct ablkcipher_request *req) |
| 792 | { |
| 793 | return omap_aes_crypt(req, FLAGS_CTR); |
| 794 | } |
| 795 | |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 796 | static int omap_aes_cra_init(struct crypto_tfm *tfm) |
| 797 | { |
Lokesh Vutla | 9fcb191 | 2016-08-04 13:28:44 +0300 | [diff] [blame] | 798 | const char *name = crypto_tfm_alg_name(tfm); |
| 799 | const u32 flags = CRYPTO_ALG_ASYNC | CRYPTO_ALG_NEED_FALLBACK; |
| 800 | struct omap_aes_ctx *ctx = crypto_tfm_ctx(tfm); |
| 801 | struct crypto_skcipher *blk; |
| 802 | |
| 803 | blk = crypto_alloc_skcipher(name, 0, flags); |
| 804 | if (IS_ERR(blk)) |
| 805 | return PTR_ERR(blk); |
| 806 | |
| 807 | ctx->fallback = blk; |
| 808 | |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 809 | tfm->crt_ablkcipher.reqsize = sizeof(struct omap_aes_reqctx); |
| 810 | |
| 811 | return 0; |
| 812 | } |
| 813 | |
| 814 | static void omap_aes_cra_exit(struct crypto_tfm *tfm) |
| 815 | { |
Lokesh Vutla | 9fcb191 | 2016-08-04 13:28:44 +0300 | [diff] [blame] | 816 | struct omap_aes_ctx *ctx = crypto_tfm_ctx(tfm); |
| 817 | |
| 818 | if (ctx->fallback) |
| 819 | crypto_free_skcipher(ctx->fallback); |
| 820 | |
| 821 | ctx->fallback = NULL; |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 822 | } |
| 823 | |
| 824 | /* ********************** ALGS ************************************ */ |
| 825 | |
Mark A. Greer | f9fb69e | 2013-01-08 11:57:47 -0700 | [diff] [blame] | 826 | static struct crypto_alg algs_ecb_cbc[] = { |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 827 | { |
| 828 | .cra_name = "ecb(aes)", |
| 829 | .cra_driver_name = "ecb-aes-omap", |
Vutla, Lokesh | 6e2e3d1 | 2015-07-07 21:01:44 +0530 | [diff] [blame] | 830 | .cra_priority = 300, |
Nikos Mavrogiannopoulos | d912bb7 | 2011-11-01 13:39:56 +0100 | [diff] [blame] | 831 | .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | |
| 832 | CRYPTO_ALG_KERN_DRIVER_ONLY | |
Lokesh Vutla | 9fcb191 | 2016-08-04 13:28:44 +0300 | [diff] [blame] | 833 | CRYPTO_ALG_ASYNC | CRYPTO_ALG_NEED_FALLBACK, |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 834 | .cra_blocksize = AES_BLOCK_SIZE, |
| 835 | .cra_ctxsize = sizeof(struct omap_aes_ctx), |
Dmitry Kasatkin | efce41b | 2010-11-30 10:13:32 +0200 | [diff] [blame] | 836 | .cra_alignmask = 0, |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 837 | .cra_type = &crypto_ablkcipher_type, |
| 838 | .cra_module = THIS_MODULE, |
| 839 | .cra_init = omap_aes_cra_init, |
| 840 | .cra_exit = omap_aes_cra_exit, |
| 841 | .cra_u.ablkcipher = { |
| 842 | .min_keysize = AES_MIN_KEY_SIZE, |
| 843 | .max_keysize = AES_MAX_KEY_SIZE, |
| 844 | .setkey = omap_aes_setkey, |
| 845 | .encrypt = omap_aes_ecb_encrypt, |
| 846 | .decrypt = omap_aes_ecb_decrypt, |
| 847 | } |
| 848 | }, |
| 849 | { |
| 850 | .cra_name = "cbc(aes)", |
| 851 | .cra_driver_name = "cbc-aes-omap", |
Vutla, Lokesh | 6e2e3d1 | 2015-07-07 21:01:44 +0530 | [diff] [blame] | 852 | .cra_priority = 300, |
Nikos Mavrogiannopoulos | d912bb7 | 2011-11-01 13:39:56 +0100 | [diff] [blame] | 853 | .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | |
| 854 | CRYPTO_ALG_KERN_DRIVER_ONLY | |
Lokesh Vutla | 9fcb191 | 2016-08-04 13:28:44 +0300 | [diff] [blame] | 855 | CRYPTO_ALG_ASYNC | CRYPTO_ALG_NEED_FALLBACK, |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 856 | .cra_blocksize = AES_BLOCK_SIZE, |
| 857 | .cra_ctxsize = sizeof(struct omap_aes_ctx), |
Dmitry Kasatkin | efce41b | 2010-11-30 10:13:32 +0200 | [diff] [blame] | 858 | .cra_alignmask = 0, |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 859 | .cra_type = &crypto_ablkcipher_type, |
| 860 | .cra_module = THIS_MODULE, |
| 861 | .cra_init = omap_aes_cra_init, |
| 862 | .cra_exit = omap_aes_cra_exit, |
| 863 | .cra_u.ablkcipher = { |
| 864 | .min_keysize = AES_MIN_KEY_SIZE, |
| 865 | .max_keysize = AES_MAX_KEY_SIZE, |
| 866 | .ivsize = AES_BLOCK_SIZE, |
| 867 | .setkey = omap_aes_setkey, |
| 868 | .encrypt = omap_aes_cbc_encrypt, |
| 869 | .decrypt = omap_aes_cbc_decrypt, |
| 870 | } |
| 871 | } |
| 872 | }; |
| 873 | |
Mark A. Greer | f9fb69e | 2013-01-08 11:57:47 -0700 | [diff] [blame] | 874 | static struct crypto_alg algs_ctr[] = { |
| 875 | { |
| 876 | .cra_name = "ctr(aes)", |
| 877 | .cra_driver_name = "ctr-aes-omap", |
Vutla, Lokesh | 6e2e3d1 | 2015-07-07 21:01:44 +0530 | [diff] [blame] | 878 | .cra_priority = 300, |
Mark A. Greer | f9fb69e | 2013-01-08 11:57:47 -0700 | [diff] [blame] | 879 | .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | |
| 880 | CRYPTO_ALG_KERN_DRIVER_ONLY | |
Lokesh Vutla | 9fcb191 | 2016-08-04 13:28:44 +0300 | [diff] [blame] | 881 | CRYPTO_ALG_ASYNC | CRYPTO_ALG_NEED_FALLBACK, |
Mark A. Greer | f9fb69e | 2013-01-08 11:57:47 -0700 | [diff] [blame] | 882 | .cra_blocksize = AES_BLOCK_SIZE, |
| 883 | .cra_ctxsize = sizeof(struct omap_aes_ctx), |
| 884 | .cra_alignmask = 0, |
| 885 | .cra_type = &crypto_ablkcipher_type, |
| 886 | .cra_module = THIS_MODULE, |
| 887 | .cra_init = omap_aes_cra_init, |
| 888 | .cra_exit = omap_aes_cra_exit, |
| 889 | .cra_u.ablkcipher = { |
| 890 | .min_keysize = AES_MIN_KEY_SIZE, |
| 891 | .max_keysize = AES_MAX_KEY_SIZE, |
| 892 | .geniv = "eseqiv", |
| 893 | .ivsize = AES_BLOCK_SIZE, |
| 894 | .setkey = omap_aes_setkey, |
| 895 | .encrypt = omap_aes_ctr_encrypt, |
| 896 | .decrypt = omap_aes_ctr_decrypt, |
| 897 | } |
| 898 | } , |
| 899 | }; |
| 900 | |
| 901 | static struct omap_aes_algs_info omap_aes_algs_info_ecb_cbc[] = { |
| 902 | { |
| 903 | .algs_list = algs_ecb_cbc, |
| 904 | .size = ARRAY_SIZE(algs_ecb_cbc), |
| 905 | }, |
| 906 | }; |
| 907 | |
Mark A. Greer | 0d35583 | 2013-01-08 11:57:46 -0700 | [diff] [blame] | 908 | static const struct omap_aes_pdata omap_aes_pdata_omap2 = { |
Mark A. Greer | f9fb69e | 2013-01-08 11:57:47 -0700 | [diff] [blame] | 909 | .algs_info = omap_aes_algs_info_ecb_cbc, |
| 910 | .algs_info_size = ARRAY_SIZE(omap_aes_algs_info_ecb_cbc), |
Mark A. Greer | 0d35583 | 2013-01-08 11:57:46 -0700 | [diff] [blame] | 911 | .trigger = omap_aes_dma_trigger_omap2, |
| 912 | .key_ofs = 0x1c, |
| 913 | .iv_ofs = 0x20, |
| 914 | .ctrl_ofs = 0x30, |
| 915 | .data_ofs = 0x34, |
| 916 | .rev_ofs = 0x44, |
| 917 | .mask_ofs = 0x48, |
| 918 | .dma_enable_in = BIT(2), |
| 919 | .dma_enable_out = BIT(3), |
| 920 | .dma_start = BIT(5), |
| 921 | .major_mask = 0xf0, |
| 922 | .major_shift = 4, |
| 923 | .minor_mask = 0x0f, |
| 924 | .minor_shift = 0, |
| 925 | }; |
| 926 | |
Mark A. Greer | bc69d12 | 2013-01-08 11:57:44 -0700 | [diff] [blame] | 927 | #ifdef CONFIG_OF |
Mark A. Greer | f9fb69e | 2013-01-08 11:57:47 -0700 | [diff] [blame] | 928 | static struct omap_aes_algs_info omap_aes_algs_info_ecb_cbc_ctr[] = { |
| 929 | { |
| 930 | .algs_list = algs_ecb_cbc, |
| 931 | .size = ARRAY_SIZE(algs_ecb_cbc), |
| 932 | }, |
| 933 | { |
| 934 | .algs_list = algs_ctr, |
| 935 | .size = ARRAY_SIZE(algs_ctr), |
| 936 | }, |
| 937 | }; |
| 938 | |
| 939 | static const struct omap_aes_pdata omap_aes_pdata_omap3 = { |
| 940 | .algs_info = omap_aes_algs_info_ecb_cbc_ctr, |
| 941 | .algs_info_size = ARRAY_SIZE(omap_aes_algs_info_ecb_cbc_ctr), |
| 942 | .trigger = omap_aes_dma_trigger_omap2, |
| 943 | .key_ofs = 0x1c, |
| 944 | .iv_ofs = 0x20, |
| 945 | .ctrl_ofs = 0x30, |
| 946 | .data_ofs = 0x34, |
| 947 | .rev_ofs = 0x44, |
| 948 | .mask_ofs = 0x48, |
| 949 | .dma_enable_in = BIT(2), |
| 950 | .dma_enable_out = BIT(3), |
| 951 | .dma_start = BIT(5), |
| 952 | .major_mask = 0xf0, |
| 953 | .major_shift = 4, |
| 954 | .minor_mask = 0x0f, |
| 955 | .minor_shift = 0, |
| 956 | }; |
| 957 | |
Mark A. Greer | 0d35583 | 2013-01-08 11:57:46 -0700 | [diff] [blame] | 958 | static const struct omap_aes_pdata omap_aes_pdata_omap4 = { |
Mark A. Greer | f9fb69e | 2013-01-08 11:57:47 -0700 | [diff] [blame] | 959 | .algs_info = omap_aes_algs_info_ecb_cbc_ctr, |
| 960 | .algs_info_size = ARRAY_SIZE(omap_aes_algs_info_ecb_cbc_ctr), |
Mark A. Greer | 0d35583 | 2013-01-08 11:57:46 -0700 | [diff] [blame] | 961 | .trigger = omap_aes_dma_trigger_omap4, |
| 962 | .key_ofs = 0x3c, |
| 963 | .iv_ofs = 0x40, |
| 964 | .ctrl_ofs = 0x50, |
| 965 | .data_ofs = 0x60, |
| 966 | .rev_ofs = 0x80, |
| 967 | .mask_ofs = 0x84, |
Joel Fernandes | 6721675 | 2013-08-17 21:42:28 -0500 | [diff] [blame] | 968 | .irq_status_ofs = 0x8c, |
| 969 | .irq_enable_ofs = 0x90, |
Mark A. Greer | 0d35583 | 2013-01-08 11:57:46 -0700 | [diff] [blame] | 970 | .dma_enable_in = BIT(5), |
| 971 | .dma_enable_out = BIT(6), |
| 972 | .major_mask = 0x0700, |
| 973 | .major_shift = 8, |
| 974 | .minor_mask = 0x003f, |
| 975 | .minor_shift = 0, |
| 976 | }; |
| 977 | |
Joel Fernandes | 1bf95cc | 2013-08-17 21:42:29 -0500 | [diff] [blame] | 978 | static irqreturn_t omap_aes_irq(int irq, void *dev_id) |
| 979 | { |
| 980 | struct omap_aes_dev *dd = dev_id; |
| 981 | u32 status, i; |
| 982 | u32 *src, *dst; |
| 983 | |
| 984 | status = omap_aes_read(dd, AES_REG_IRQ_STATUS(dd)); |
| 985 | if (status & AES_REG_IRQ_DATA_IN) { |
| 986 | omap_aes_write(dd, AES_REG_IRQ_ENABLE(dd), 0x0); |
| 987 | |
| 988 | BUG_ON(!dd->in_sg); |
| 989 | |
| 990 | BUG_ON(_calc_walked(in) > dd->in_sg->length); |
| 991 | |
| 992 | src = sg_virt(dd->in_sg) + _calc_walked(in); |
| 993 | |
| 994 | for (i = 0; i < AES_BLOCK_WORDS; i++) { |
| 995 | omap_aes_write(dd, AES_REG_DATA_N(dd, i), *src); |
| 996 | |
| 997 | scatterwalk_advance(&dd->in_walk, 4); |
| 998 | if (dd->in_sg->length == _calc_walked(in)) { |
Cristian Stoica | 5be4d4c | 2015-01-20 10:06:16 +0200 | [diff] [blame] | 999 | dd->in_sg = sg_next(dd->in_sg); |
Joel Fernandes | 1bf95cc | 2013-08-17 21:42:29 -0500 | [diff] [blame] | 1000 | if (dd->in_sg) { |
| 1001 | scatterwalk_start(&dd->in_walk, |
| 1002 | dd->in_sg); |
| 1003 | src = sg_virt(dd->in_sg) + |
| 1004 | _calc_walked(in); |
| 1005 | } |
| 1006 | } else { |
| 1007 | src++; |
| 1008 | } |
| 1009 | } |
| 1010 | |
| 1011 | /* Clear IRQ status */ |
| 1012 | status &= ~AES_REG_IRQ_DATA_IN; |
| 1013 | omap_aes_write(dd, AES_REG_IRQ_STATUS(dd), status); |
| 1014 | |
| 1015 | /* Enable DATA_OUT interrupt */ |
| 1016 | omap_aes_write(dd, AES_REG_IRQ_ENABLE(dd), 0x4); |
| 1017 | |
| 1018 | } else if (status & AES_REG_IRQ_DATA_OUT) { |
| 1019 | omap_aes_write(dd, AES_REG_IRQ_ENABLE(dd), 0x0); |
| 1020 | |
| 1021 | BUG_ON(!dd->out_sg); |
| 1022 | |
| 1023 | BUG_ON(_calc_walked(out) > dd->out_sg->length); |
| 1024 | |
| 1025 | dst = sg_virt(dd->out_sg) + _calc_walked(out); |
| 1026 | |
| 1027 | for (i = 0; i < AES_BLOCK_WORDS; i++) { |
| 1028 | *dst = omap_aes_read(dd, AES_REG_DATA_N(dd, i)); |
| 1029 | scatterwalk_advance(&dd->out_walk, 4); |
| 1030 | if (dd->out_sg->length == _calc_walked(out)) { |
Cristian Stoica | 5be4d4c | 2015-01-20 10:06:16 +0200 | [diff] [blame] | 1031 | dd->out_sg = sg_next(dd->out_sg); |
Joel Fernandes | 1bf95cc | 2013-08-17 21:42:29 -0500 | [diff] [blame] | 1032 | if (dd->out_sg) { |
| 1033 | scatterwalk_start(&dd->out_walk, |
| 1034 | dd->out_sg); |
| 1035 | dst = sg_virt(dd->out_sg) + |
| 1036 | _calc_walked(out); |
| 1037 | } |
| 1038 | } else { |
| 1039 | dst++; |
| 1040 | } |
| 1041 | } |
| 1042 | |
Vutla, Lokesh | 310b0d5 | 2015-07-07 21:01:43 +0530 | [diff] [blame] | 1043 | dd->total -= min_t(size_t, AES_BLOCK_SIZE, dd->total); |
Joel Fernandes | 1bf95cc | 2013-08-17 21:42:29 -0500 | [diff] [blame] | 1044 | |
| 1045 | /* Clear IRQ status */ |
| 1046 | status &= ~AES_REG_IRQ_DATA_OUT; |
| 1047 | omap_aes_write(dd, AES_REG_IRQ_STATUS(dd), status); |
| 1048 | |
| 1049 | if (!dd->total) |
| 1050 | /* All bytes read! */ |
| 1051 | tasklet_schedule(&dd->done_task); |
| 1052 | else |
| 1053 | /* Enable DATA_IN interrupt for next block */ |
| 1054 | omap_aes_write(dd, AES_REG_IRQ_ENABLE(dd), 0x2); |
| 1055 | } |
| 1056 | |
| 1057 | return IRQ_HANDLED; |
| 1058 | } |
| 1059 | |
Mark A. Greer | bc69d12 | 2013-01-08 11:57:44 -0700 | [diff] [blame] | 1060 | static const struct of_device_id omap_aes_of_match[] = { |
| 1061 | { |
| 1062 | .compatible = "ti,omap2-aes", |
Mark A. Greer | 0d35583 | 2013-01-08 11:57:46 -0700 | [diff] [blame] | 1063 | .data = &omap_aes_pdata_omap2, |
| 1064 | }, |
| 1065 | { |
Mark A. Greer | f9fb69e | 2013-01-08 11:57:47 -0700 | [diff] [blame] | 1066 | .compatible = "ti,omap3-aes", |
| 1067 | .data = &omap_aes_pdata_omap3, |
| 1068 | }, |
| 1069 | { |
Mark A. Greer | 0d35583 | 2013-01-08 11:57:46 -0700 | [diff] [blame] | 1070 | .compatible = "ti,omap4-aes", |
| 1071 | .data = &omap_aes_pdata_omap4, |
Mark A. Greer | bc69d12 | 2013-01-08 11:57:44 -0700 | [diff] [blame] | 1072 | }, |
| 1073 | {}, |
| 1074 | }; |
| 1075 | MODULE_DEVICE_TABLE(of, omap_aes_of_match); |
| 1076 | |
| 1077 | static int omap_aes_get_res_of(struct omap_aes_dev *dd, |
| 1078 | struct device *dev, struct resource *res) |
| 1079 | { |
| 1080 | struct device_node *node = dev->of_node; |
| 1081 | const struct of_device_id *match; |
| 1082 | int err = 0; |
| 1083 | |
| 1084 | match = of_match_device(of_match_ptr(omap_aes_of_match), dev); |
| 1085 | if (!match) { |
| 1086 | dev_err(dev, "no compatible OF match\n"); |
| 1087 | err = -EINVAL; |
| 1088 | goto err; |
| 1089 | } |
| 1090 | |
| 1091 | err = of_address_to_resource(node, 0, res); |
| 1092 | if (err < 0) { |
| 1093 | dev_err(dev, "can't translate OF node address\n"); |
| 1094 | err = -EINVAL; |
| 1095 | goto err; |
| 1096 | } |
| 1097 | |
Mark A. Greer | 0d35583 | 2013-01-08 11:57:46 -0700 | [diff] [blame] | 1098 | dd->pdata = match->data; |
| 1099 | |
Mark A. Greer | bc69d12 | 2013-01-08 11:57:44 -0700 | [diff] [blame] | 1100 | err: |
| 1101 | return err; |
| 1102 | } |
| 1103 | #else |
| 1104 | static const struct of_device_id omap_aes_of_match[] = { |
| 1105 | {}, |
| 1106 | }; |
| 1107 | |
| 1108 | static int omap_aes_get_res_of(struct omap_aes_dev *dd, |
| 1109 | struct device *dev, struct resource *res) |
| 1110 | { |
| 1111 | return -EINVAL; |
| 1112 | } |
| 1113 | #endif |
| 1114 | |
| 1115 | static int omap_aes_get_res_pdev(struct omap_aes_dev *dd, |
| 1116 | struct platform_device *pdev, struct resource *res) |
| 1117 | { |
| 1118 | struct device *dev = &pdev->dev; |
| 1119 | struct resource *r; |
| 1120 | int err = 0; |
| 1121 | |
| 1122 | /* Get the base address */ |
| 1123 | r = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 1124 | if (!r) { |
| 1125 | dev_err(dev, "no MEM resource info\n"); |
| 1126 | err = -ENODEV; |
| 1127 | goto err; |
| 1128 | } |
| 1129 | memcpy(res, r, sizeof(*res)); |
| 1130 | |
Mark A. Greer | 0d35583 | 2013-01-08 11:57:46 -0700 | [diff] [blame] | 1131 | /* Only OMAP2/3 can be non-DT */ |
| 1132 | dd->pdata = &omap_aes_pdata_omap2; |
| 1133 | |
Mark A. Greer | bc69d12 | 2013-01-08 11:57:44 -0700 | [diff] [blame] | 1134 | err: |
| 1135 | return err; |
| 1136 | } |
| 1137 | |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 1138 | static int omap_aes_probe(struct platform_device *pdev) |
| 1139 | { |
| 1140 | struct device *dev = &pdev->dev; |
| 1141 | struct omap_aes_dev *dd; |
Mark A. Greer | f9fb69e | 2013-01-08 11:57:47 -0700 | [diff] [blame] | 1142 | struct crypto_alg *algp; |
Mark A. Greer | bc69d12 | 2013-01-08 11:57:44 -0700 | [diff] [blame] | 1143 | struct resource res; |
Joel Fernandes | 1801ad9 | 2013-08-17 21:42:31 -0500 | [diff] [blame] | 1144 | int err = -ENOMEM, i, j, irq = -1; |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 1145 | u32 reg; |
| 1146 | |
Joel Fernandes | 05007c1 | 2013-08-17 21:42:33 -0500 | [diff] [blame] | 1147 | dd = devm_kzalloc(dev, sizeof(struct omap_aes_dev), GFP_KERNEL); |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 1148 | if (dd == NULL) { |
| 1149 | dev_err(dev, "unable to alloc data struct.\n"); |
| 1150 | goto err_data; |
| 1151 | } |
| 1152 | dd->dev = dev; |
| 1153 | platform_set_drvdata(pdev, dd); |
| 1154 | |
Mark A. Greer | bc69d12 | 2013-01-08 11:57:44 -0700 | [diff] [blame] | 1155 | err = (dev->of_node) ? omap_aes_get_res_of(dd, dev, &res) : |
| 1156 | omap_aes_get_res_pdev(dd, pdev, &res); |
| 1157 | if (err) |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 1158 | goto err_res; |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 1159 | |
Laurent Navet | 3086228 | 2013-05-02 14:00:38 +0200 | [diff] [blame] | 1160 | dd->io_base = devm_ioremap_resource(dev, &res); |
| 1161 | if (IS_ERR(dd->io_base)) { |
| 1162 | err = PTR_ERR(dd->io_base); |
Mark A. Greer | 5946c4a | 2013-01-08 11:57:40 -0700 | [diff] [blame] | 1163 | goto err_res; |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 1164 | } |
Mark A. Greer | bc69d12 | 2013-01-08 11:57:44 -0700 | [diff] [blame] | 1165 | dd->phys_base = res.start; |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 1166 | |
Tero Kristo | f303b45 | 2016-08-04 13:28:42 +0300 | [diff] [blame] | 1167 | pm_runtime_use_autosuspend(dev); |
| 1168 | pm_runtime_set_autosuspend_delay(dev, DEFAULT_AUTOSUSPEND_DELAY); |
| 1169 | |
Mark A. Greer | 5946c4a | 2013-01-08 11:57:40 -0700 | [diff] [blame] | 1170 | pm_runtime_enable(dev); |
Nishanth Menon | f7b2b5d | 2013-12-03 19:43:13 -0600 | [diff] [blame] | 1171 | err = pm_runtime_get_sync(dev); |
| 1172 | if (err < 0) { |
| 1173 | dev_err(dev, "%s: failed to get_sync(%d)\n", |
| 1174 | __func__, err); |
| 1175 | goto err_res; |
| 1176 | } |
Mark A. Greer | 5946c4a | 2013-01-08 11:57:40 -0700 | [diff] [blame] | 1177 | |
Mark A. Greer | 0d35583 | 2013-01-08 11:57:46 -0700 | [diff] [blame] | 1178 | omap_aes_dma_stop(dd); |
| 1179 | |
| 1180 | reg = omap_aes_read(dd, AES_REG_REV(dd)); |
Mark A. Greer | 5946c4a | 2013-01-08 11:57:40 -0700 | [diff] [blame] | 1181 | |
| 1182 | pm_runtime_put_sync(dev); |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 1183 | |
Mark A. Greer | 0d35583 | 2013-01-08 11:57:46 -0700 | [diff] [blame] | 1184 | dev_info(dev, "OMAP AES hw accel rev: %u.%u\n", |
| 1185 | (reg & dd->pdata->major_mask) >> dd->pdata->major_shift, |
| 1186 | (reg & dd->pdata->minor_mask) >> dd->pdata->minor_shift); |
| 1187 | |
Dmitry Kasatkin | 21fe976 | 2010-11-30 10:13:29 +0200 | [diff] [blame] | 1188 | tasklet_init(&dd->done_task, omap_aes_done_task, (unsigned long)dd); |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 1189 | |
| 1190 | err = omap_aes_dma_init(dd); |
Peter Ujfalusi | da8b29a | 2016-04-29 16:02:18 +0300 | [diff] [blame] | 1191 | if (err == -EPROBE_DEFER) { |
| 1192 | goto err_irq; |
| 1193 | } else if (err && AES_REG_IRQ_STATUS(dd) && AES_REG_IRQ_ENABLE(dd)) { |
Joel Fernandes | 1801ad9 | 2013-08-17 21:42:31 -0500 | [diff] [blame] | 1194 | dd->pio_only = 1; |
| 1195 | |
| 1196 | irq = platform_get_irq(pdev, 0); |
| 1197 | if (irq < 0) { |
| 1198 | dev_err(dev, "can't get IRQ resource\n"); |
| 1199 | goto err_irq; |
| 1200 | } |
| 1201 | |
Joel Fernandes | bce2a22 | 2013-08-17 21:42:34 -0500 | [diff] [blame] | 1202 | err = devm_request_irq(dev, irq, omap_aes_irq, 0, |
Joel Fernandes | 1801ad9 | 2013-08-17 21:42:31 -0500 | [diff] [blame] | 1203 | dev_name(dev), dd); |
| 1204 | if (err) { |
| 1205 | dev_err(dev, "Unable to grab omap-aes IRQ\n"); |
| 1206 | goto err_irq; |
| 1207 | } |
| 1208 | } |
| 1209 | |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 1210 | |
| 1211 | INIT_LIST_HEAD(&dd->list); |
| 1212 | spin_lock(&list_lock); |
| 1213 | list_add_tail(&dd->list, &dev_list); |
| 1214 | spin_unlock(&list_lock); |
| 1215 | |
Tero Kristo | 0d0cda93 | 2016-08-04 13:28:45 +0300 | [diff] [blame] | 1216 | /* Initialize crypto engine */ |
| 1217 | dd->engine = crypto_engine_alloc_init(dev, 1); |
Wei Yongjun | c98ef8d | 2016-09-15 03:27:32 +0000 | [diff] [blame] | 1218 | if (!dd->engine) { |
| 1219 | err = -ENOMEM; |
Tero Kristo | 0d0cda93 | 2016-08-04 13:28:45 +0300 | [diff] [blame] | 1220 | goto err_engine; |
Wei Yongjun | c98ef8d | 2016-09-15 03:27:32 +0000 | [diff] [blame] | 1221 | } |
Tero Kristo | 0d0cda93 | 2016-08-04 13:28:45 +0300 | [diff] [blame] | 1222 | |
| 1223 | dd->engine->prepare_cipher_request = omap_aes_prepare_req; |
| 1224 | dd->engine->cipher_one_request = omap_aes_crypt_req; |
| 1225 | err = crypto_engine_start(dd->engine); |
| 1226 | if (err) |
| 1227 | goto err_engine; |
| 1228 | |
Mark A. Greer | f9fb69e | 2013-01-08 11:57:47 -0700 | [diff] [blame] | 1229 | for (i = 0; i < dd->pdata->algs_info_size; i++) { |
Lokesh Vutla | 3741bbb | 2016-06-01 11:56:02 +0300 | [diff] [blame] | 1230 | if (!dd->pdata->algs_info[i].registered) { |
| 1231 | for (j = 0; j < dd->pdata->algs_info[i].size; j++) { |
| 1232 | algp = &dd->pdata->algs_info[i].algs_list[j]; |
Mark A. Greer | f9fb69e | 2013-01-08 11:57:47 -0700 | [diff] [blame] | 1233 | |
Lokesh Vutla | 3741bbb | 2016-06-01 11:56:02 +0300 | [diff] [blame] | 1234 | pr_debug("reg alg: %s\n", algp->cra_name); |
| 1235 | INIT_LIST_HEAD(&algp->cra_list); |
Mark A. Greer | f9fb69e | 2013-01-08 11:57:47 -0700 | [diff] [blame] | 1236 | |
Lokesh Vutla | 3741bbb | 2016-06-01 11:56:02 +0300 | [diff] [blame] | 1237 | err = crypto_register_alg(algp); |
| 1238 | if (err) |
| 1239 | goto err_algs; |
Mark A. Greer | f9fb69e | 2013-01-08 11:57:47 -0700 | [diff] [blame] | 1240 | |
Lokesh Vutla | 3741bbb | 2016-06-01 11:56:02 +0300 | [diff] [blame] | 1241 | dd->pdata->algs_info[i].registered++; |
| 1242 | } |
Mark A. Greer | f9fb69e | 2013-01-08 11:57:47 -0700 | [diff] [blame] | 1243 | } |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 1244 | } |
| 1245 | |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 1246 | return 0; |
| 1247 | err_algs: |
Mark A. Greer | f9fb69e | 2013-01-08 11:57:47 -0700 | [diff] [blame] | 1248 | for (i = dd->pdata->algs_info_size - 1; i >= 0; i--) |
| 1249 | for (j = dd->pdata->algs_info[i].registered - 1; j >= 0; j--) |
| 1250 | crypto_unregister_alg( |
| 1251 | &dd->pdata->algs_info[i].algs_list[j]); |
Peter Ujfalusi | da8b29a | 2016-04-29 16:02:18 +0300 | [diff] [blame] | 1252 | |
Tero Kristo | 0d0cda93 | 2016-08-04 13:28:45 +0300 | [diff] [blame] | 1253 | err_engine: |
| 1254 | if (dd->engine) |
| 1255 | crypto_engine_exit(dd->engine); |
| 1256 | |
Peter Ujfalusi | da8b29a | 2016-04-29 16:02:18 +0300 | [diff] [blame] | 1257 | omap_aes_dma_cleanup(dd); |
Joel Fernandes | 1801ad9 | 2013-08-17 21:42:31 -0500 | [diff] [blame] | 1258 | err_irq: |
Dmitry Kasatkin | 21fe976 | 2010-11-30 10:13:29 +0200 | [diff] [blame] | 1259 | tasklet_kill(&dd->done_task); |
Mark A. Greer | 5946c4a | 2013-01-08 11:57:40 -0700 | [diff] [blame] | 1260 | pm_runtime_disable(dev); |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 1261 | err_res: |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 1262 | dd = NULL; |
| 1263 | err_data: |
| 1264 | dev_err(dev, "initialization failed.\n"); |
| 1265 | return err; |
| 1266 | } |
| 1267 | |
| 1268 | static int omap_aes_remove(struct platform_device *pdev) |
| 1269 | { |
| 1270 | struct omap_aes_dev *dd = platform_get_drvdata(pdev); |
Mark A. Greer | f9fb69e | 2013-01-08 11:57:47 -0700 | [diff] [blame] | 1271 | int i, j; |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 1272 | |
| 1273 | if (!dd) |
| 1274 | return -ENODEV; |
| 1275 | |
| 1276 | spin_lock(&list_lock); |
| 1277 | list_del(&dd->list); |
| 1278 | spin_unlock(&list_lock); |
| 1279 | |
Mark A. Greer | f9fb69e | 2013-01-08 11:57:47 -0700 | [diff] [blame] | 1280 | for (i = dd->pdata->algs_info_size - 1; i >= 0; i--) |
| 1281 | for (j = dd->pdata->algs_info[i].registered - 1; j >= 0; j--) |
| 1282 | crypto_unregister_alg( |
| 1283 | &dd->pdata->algs_info[i].algs_list[j]); |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 1284 | |
Baolin Wang | 0529900 | 2016-01-26 20:25:40 +0800 | [diff] [blame] | 1285 | crypto_engine_exit(dd->engine); |
Dmitry Kasatkin | 21fe976 | 2010-11-30 10:13:29 +0200 | [diff] [blame] | 1286 | tasklet_kill(&dd->done_task); |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 1287 | omap_aes_dma_cleanup(dd); |
Mark A. Greer | 5946c4a | 2013-01-08 11:57:40 -0700 | [diff] [blame] | 1288 | pm_runtime_disable(dd->dev); |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 1289 | dd = NULL; |
| 1290 | |
| 1291 | return 0; |
| 1292 | } |
| 1293 | |
Mark A. Greer | 0635fb3 | 2013-01-08 11:57:41 -0700 | [diff] [blame] | 1294 | #ifdef CONFIG_PM_SLEEP |
| 1295 | static int omap_aes_suspend(struct device *dev) |
| 1296 | { |
| 1297 | pm_runtime_put_sync(dev); |
| 1298 | return 0; |
| 1299 | } |
| 1300 | |
| 1301 | static int omap_aes_resume(struct device *dev) |
| 1302 | { |
| 1303 | pm_runtime_get_sync(dev); |
| 1304 | return 0; |
| 1305 | } |
| 1306 | #endif |
| 1307 | |
Jingoo Han | ea7b284 | 2014-02-27 20:31:38 +0900 | [diff] [blame] | 1308 | static SIMPLE_DEV_PM_OPS(omap_aes_pm_ops, omap_aes_suspend, omap_aes_resume); |
Mark A. Greer | 0635fb3 | 2013-01-08 11:57:41 -0700 | [diff] [blame] | 1309 | |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 1310 | static struct platform_driver omap_aes_driver = { |
| 1311 | .probe = omap_aes_probe, |
| 1312 | .remove = omap_aes_remove, |
| 1313 | .driver = { |
| 1314 | .name = "omap-aes", |
Mark A. Greer | 0635fb3 | 2013-01-08 11:57:41 -0700 | [diff] [blame] | 1315 | .pm = &omap_aes_pm_ops, |
Mark A. Greer | bc69d12 | 2013-01-08 11:57:44 -0700 | [diff] [blame] | 1316 | .of_match_table = omap_aes_of_match, |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 1317 | }, |
| 1318 | }; |
| 1319 | |
Sachin Kamat | 94e51df | 2013-03-04 15:09:42 +0530 | [diff] [blame] | 1320 | module_platform_driver(omap_aes_driver); |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 1321 | |
| 1322 | MODULE_DESCRIPTION("OMAP AES hw acceleration support."); |
| 1323 | MODULE_LICENSE("GPL v2"); |
| 1324 | MODULE_AUTHOR("Dmitry Kasatkin"); |
| 1325 | |