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