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> |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License version 2 as published |
| 11 | * by the Free Software Foundation. |
| 12 | * |
| 13 | */ |
| 14 | |
| 15 | #define pr_fmt(fmt) "%s: " fmt, __func__ |
| 16 | |
| 17 | #include <linux/err.h> |
| 18 | #include <linux/module.h> |
| 19 | #include <linux/init.h> |
| 20 | #include <linux/errno.h> |
| 21 | #include <linux/kernel.h> |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 22 | #include <linux/platform_device.h> |
| 23 | #include <linux/scatterlist.h> |
| 24 | #include <linux/dma-mapping.h> |
Mark A. Greer | ebedbf7 | 2013-01-08 11:57:42 -0700 | [diff] [blame] | 25 | #include <linux/dmaengine.h> |
| 26 | #include <linux/omap-dma.h> |
Mark A. Greer | 5946c4a | 2013-01-08 11:57:40 -0700 | [diff] [blame] | 27 | #include <linux/pm_runtime.h> |
Mark A. Greer | bc69d12 | 2013-01-08 11:57:44 -0700 | [diff] [blame^] | 28 | #include <linux/of.h> |
| 29 | #include <linux/of_device.h> |
| 30 | #include <linux/of_address.h> |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 31 | #include <linux/io.h> |
| 32 | #include <linux/crypto.h> |
| 33 | #include <linux/interrupt.h> |
| 34 | #include <crypto/scatterwalk.h> |
| 35 | #include <crypto/aes.h> |
| 36 | |
Mark A. Greer | ebedbf7 | 2013-01-08 11:57:42 -0700 | [diff] [blame] | 37 | #define DST_MAXBURST 4 |
| 38 | #define DMA_MIN (DST_MAXBURST * sizeof(u32)) |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 39 | |
| 40 | /* OMAP TRM gives bitfields as start:end, where start is the higher bit |
| 41 | number. For example 7:0 */ |
| 42 | #define FLD_MASK(start, end) (((1 << ((start) - (end) + 1)) - 1) << (end)) |
| 43 | #define FLD_VAL(val, start, end) (((val) << (end)) & FLD_MASK(start, end)) |
| 44 | |
| 45 | #define AES_REG_KEY(x) (0x1C - ((x ^ 0x01) * 0x04)) |
| 46 | #define AES_REG_IV(x) (0x20 + ((x) * 0x04)) |
| 47 | |
| 48 | #define AES_REG_CTRL 0x30 |
| 49 | #define AES_REG_CTRL_CTR_WIDTH (1 << 7) |
| 50 | #define AES_REG_CTRL_CTR (1 << 6) |
| 51 | #define AES_REG_CTRL_CBC (1 << 5) |
| 52 | #define AES_REG_CTRL_KEY_SIZE (3 << 3) |
| 53 | #define AES_REG_CTRL_DIRECTION (1 << 2) |
| 54 | #define AES_REG_CTRL_INPUT_READY (1 << 1) |
| 55 | #define AES_REG_CTRL_OUTPUT_READY (1 << 0) |
| 56 | |
| 57 | #define AES_REG_DATA 0x34 |
| 58 | #define AES_REG_DATA_N(x) (0x34 + ((x) * 0x04)) |
| 59 | |
| 60 | #define AES_REG_REV 0x44 |
| 61 | #define AES_REG_REV_MAJOR 0xF0 |
| 62 | #define AES_REG_REV_MINOR 0x0F |
| 63 | |
| 64 | #define AES_REG_MASK 0x48 |
| 65 | #define AES_REG_MASK_SIDLE (1 << 6) |
| 66 | #define AES_REG_MASK_START (1 << 5) |
| 67 | #define AES_REG_MASK_DMA_OUT_EN (1 << 3) |
| 68 | #define AES_REG_MASK_DMA_IN_EN (1 << 2) |
| 69 | #define AES_REG_MASK_SOFTRESET (1 << 1) |
| 70 | #define AES_REG_AUTOIDLE (1 << 0) |
| 71 | |
| 72 | #define AES_REG_SYSSTATUS 0x4C |
| 73 | #define AES_REG_SYSSTATUS_RESETDONE (1 << 0) |
| 74 | |
| 75 | #define DEFAULT_TIMEOUT (5*HZ) |
| 76 | |
| 77 | #define FLAGS_MODE_MASK 0x000f |
| 78 | #define FLAGS_ENCRYPT BIT(0) |
| 79 | #define FLAGS_CBC BIT(1) |
| 80 | #define FLAGS_GIV BIT(2) |
| 81 | |
Dmitry Kasatkin | 67a730c | 2010-11-30 10:13:30 +0200 | [diff] [blame] | 82 | #define FLAGS_INIT BIT(4) |
| 83 | #define FLAGS_FAST BIT(5) |
| 84 | #define FLAGS_BUSY BIT(6) |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 85 | |
| 86 | struct omap_aes_ctx { |
| 87 | struct omap_aes_dev *dd; |
| 88 | |
| 89 | int keylen; |
| 90 | u32 key[AES_KEYSIZE_256 / sizeof(u32)]; |
| 91 | unsigned long flags; |
| 92 | }; |
| 93 | |
| 94 | struct omap_aes_reqctx { |
| 95 | unsigned long mode; |
| 96 | }; |
| 97 | |
| 98 | #define OMAP_AES_QUEUE_LENGTH 1 |
| 99 | #define OMAP_AES_CACHE_SIZE 0 |
| 100 | |
| 101 | struct omap_aes_dev { |
| 102 | struct list_head list; |
| 103 | unsigned long phys_base; |
Dmitry Kasatkin | efce41b | 2010-11-30 10:13:32 +0200 | [diff] [blame] | 104 | void __iomem *io_base; |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 105 | struct omap_aes_ctx *ctx; |
| 106 | struct device *dev; |
| 107 | unsigned long flags; |
Dmitry Kasatkin | 21fe976 | 2010-11-30 10:13:29 +0200 | [diff] [blame] | 108 | int err; |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 109 | |
Dmitry Kasatkin | 21fe976 | 2010-11-30 10:13:29 +0200 | [diff] [blame] | 110 | spinlock_t lock; |
| 111 | struct crypto_queue queue; |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 112 | |
Dmitry Kasatkin | 21fe976 | 2010-11-30 10:13:29 +0200 | [diff] [blame] | 113 | struct tasklet_struct done_task; |
| 114 | struct tasklet_struct queue_task; |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 115 | |
| 116 | struct ablkcipher_request *req; |
| 117 | size_t total; |
| 118 | struct scatterlist *in_sg; |
Mark A. Greer | ebedbf7 | 2013-01-08 11:57:42 -0700 | [diff] [blame] | 119 | struct scatterlist in_sgl; |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 120 | size_t in_offset; |
| 121 | struct scatterlist *out_sg; |
Mark A. Greer | ebedbf7 | 2013-01-08 11:57:42 -0700 | [diff] [blame] | 122 | struct scatterlist out_sgl; |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 123 | size_t out_offset; |
| 124 | |
| 125 | size_t buflen; |
| 126 | void *buf_in; |
| 127 | size_t dma_size; |
| 128 | int dma_in; |
Mark A. Greer | ebedbf7 | 2013-01-08 11:57:42 -0700 | [diff] [blame] | 129 | struct dma_chan *dma_lch_in; |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 130 | dma_addr_t dma_addr_in; |
| 131 | void *buf_out; |
| 132 | int dma_out; |
Mark A. Greer | ebedbf7 | 2013-01-08 11:57:42 -0700 | [diff] [blame] | 133 | struct dma_chan *dma_lch_out; |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 134 | dma_addr_t dma_addr_out; |
| 135 | }; |
| 136 | |
| 137 | /* keep registered devices data here */ |
| 138 | static LIST_HEAD(dev_list); |
| 139 | static DEFINE_SPINLOCK(list_lock); |
| 140 | |
| 141 | static inline u32 omap_aes_read(struct omap_aes_dev *dd, u32 offset) |
| 142 | { |
| 143 | return __raw_readl(dd->io_base + offset); |
| 144 | } |
| 145 | |
| 146 | static inline void omap_aes_write(struct omap_aes_dev *dd, u32 offset, |
| 147 | u32 value) |
| 148 | { |
| 149 | __raw_writel(value, dd->io_base + offset); |
| 150 | } |
| 151 | |
| 152 | static inline void omap_aes_write_mask(struct omap_aes_dev *dd, u32 offset, |
| 153 | u32 value, u32 mask) |
| 154 | { |
| 155 | u32 val; |
| 156 | |
| 157 | val = omap_aes_read(dd, offset); |
| 158 | val &= ~mask; |
| 159 | val |= value; |
| 160 | omap_aes_write(dd, offset, val); |
| 161 | } |
| 162 | |
| 163 | static void omap_aes_write_n(struct omap_aes_dev *dd, u32 offset, |
| 164 | u32 *value, int count) |
| 165 | { |
| 166 | for (; count--; value++, offset += 4) |
| 167 | omap_aes_write(dd, offset, *value); |
| 168 | } |
| 169 | |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 170 | static int omap_aes_hw_init(struct omap_aes_dev *dd) |
| 171 | { |
Dmitry Kasatkin | 83ea7e0 | 2010-11-30 10:13:31 +0200 | [diff] [blame] | 172 | /* |
| 173 | * clocks are enabled when request starts and disabled when finished. |
| 174 | * It may be long delays between requests. |
| 175 | * Device might go to off mode to save power. |
| 176 | */ |
Mark A. Greer | 5946c4a | 2013-01-08 11:57:40 -0700 | [diff] [blame] | 177 | pm_runtime_get_sync(dd->dev); |
Dmitry Kasatkin | eeb2b20 | 2010-11-30 10:13:28 +0200 | [diff] [blame] | 178 | |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 179 | if (!(dd->flags & FLAGS_INIT)) { |
Dmitry Kasatkin | eeb2b20 | 2010-11-30 10:13:28 +0200 | [diff] [blame] | 180 | dd->flags |= FLAGS_INIT; |
Dmitry Kasatkin | 21fe976 | 2010-11-30 10:13:29 +0200 | [diff] [blame] | 181 | dd->err = 0; |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 182 | } |
| 183 | |
Dmitry Kasatkin | eeb2b20 | 2010-11-30 10:13:28 +0200 | [diff] [blame] | 184 | return 0; |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 185 | } |
| 186 | |
Dmitry Kasatkin | 21fe976 | 2010-11-30 10:13:29 +0200 | [diff] [blame] | 187 | static int omap_aes_write_ctrl(struct omap_aes_dev *dd) |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 188 | { |
| 189 | unsigned int key32; |
Dmitry Kasatkin | 67a730c | 2010-11-30 10:13:30 +0200 | [diff] [blame] | 190 | int i, err; |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 191 | u32 val, mask; |
| 192 | |
Dmitry Kasatkin | 21fe976 | 2010-11-30 10:13:29 +0200 | [diff] [blame] | 193 | err = omap_aes_hw_init(dd); |
| 194 | if (err) |
| 195 | return err; |
| 196 | |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 197 | val = 0; |
Mark A. Greer | ebedbf7 | 2013-01-08 11:57:42 -0700 | [diff] [blame] | 198 | if (dd->dma_lch_out != NULL) |
| 199 | val |= AES_REG_MASK_DMA_OUT_EN; |
| 200 | if (dd->dma_lch_in != NULL) |
| 201 | val |= AES_REG_MASK_DMA_IN_EN; |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 202 | |
| 203 | mask = AES_REG_MASK_DMA_IN_EN | AES_REG_MASK_DMA_OUT_EN; |
| 204 | |
| 205 | omap_aes_write_mask(dd, AES_REG_MASK, val, mask); |
| 206 | |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 207 | key32 = dd->ctx->keylen / sizeof(u32); |
Dmitry Kasatkin | 67a730c | 2010-11-30 10:13:30 +0200 | [diff] [blame] | 208 | |
| 209 | /* 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] | 210 | for (i = 0; i < key32; i++) { |
| 211 | omap_aes_write(dd, AES_REG_KEY(i), |
| 212 | __le32_to_cpu(dd->ctx->key[i])); |
| 213 | } |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 214 | |
Dmitry Kasatkin | 67a730c | 2010-11-30 10:13:30 +0200 | [diff] [blame] | 215 | if ((dd->flags & FLAGS_CBC) && dd->req->info) |
| 216 | omap_aes_write_n(dd, AES_REG_IV(0), dd->req->info, 4); |
| 217 | |
| 218 | val = FLD_VAL(((dd->ctx->keylen >> 3) - 1), 4, 3); |
| 219 | if (dd->flags & FLAGS_CBC) |
| 220 | val |= AES_REG_CTRL_CBC; |
| 221 | if (dd->flags & FLAGS_ENCRYPT) |
| 222 | val |= AES_REG_CTRL_DIRECTION; |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 223 | |
| 224 | mask = AES_REG_CTRL_CBC | AES_REG_CTRL_DIRECTION | |
| 225 | AES_REG_CTRL_KEY_SIZE; |
| 226 | |
Dmitry Kasatkin | 67a730c | 2010-11-30 10:13:30 +0200 | [diff] [blame] | 227 | omap_aes_write_mask(dd, AES_REG_CTRL, val, mask); |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 228 | |
Dmitry Kasatkin | 21fe976 | 2010-11-30 10:13:29 +0200 | [diff] [blame] | 229 | return 0; |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | static struct omap_aes_dev *omap_aes_find_dev(struct omap_aes_ctx *ctx) |
| 233 | { |
| 234 | struct omap_aes_dev *dd = NULL, *tmp; |
| 235 | |
| 236 | spin_lock_bh(&list_lock); |
| 237 | if (!ctx->dd) { |
| 238 | list_for_each_entry(tmp, &dev_list, list) { |
| 239 | /* FIXME: take fist available aes core */ |
| 240 | dd = tmp; |
| 241 | break; |
| 242 | } |
| 243 | ctx->dd = dd; |
| 244 | } else { |
| 245 | /* already found before */ |
| 246 | dd = ctx->dd; |
| 247 | } |
| 248 | spin_unlock_bh(&list_lock); |
| 249 | |
| 250 | return dd; |
| 251 | } |
| 252 | |
Mark A. Greer | ebedbf7 | 2013-01-08 11:57:42 -0700 | [diff] [blame] | 253 | static void omap_aes_dma_out_callback(void *data) |
| 254 | { |
| 255 | struct omap_aes_dev *dd = data; |
| 256 | |
| 257 | /* dma_lch_out - completed */ |
| 258 | tasklet_schedule(&dd->done_task); |
| 259 | } |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 260 | |
| 261 | static int omap_aes_dma_init(struct omap_aes_dev *dd) |
| 262 | { |
| 263 | int err = -ENOMEM; |
Mark A. Greer | ebedbf7 | 2013-01-08 11:57:42 -0700 | [diff] [blame] | 264 | dma_cap_mask_t mask; |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 265 | |
Mark A. Greer | ebedbf7 | 2013-01-08 11:57:42 -0700 | [diff] [blame] | 266 | dd->dma_lch_out = NULL; |
| 267 | dd->dma_lch_in = NULL; |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 268 | |
| 269 | dd->buf_in = (void *)__get_free_pages(GFP_KERNEL, OMAP_AES_CACHE_SIZE); |
| 270 | dd->buf_out = (void *)__get_free_pages(GFP_KERNEL, OMAP_AES_CACHE_SIZE); |
| 271 | dd->buflen = PAGE_SIZE << OMAP_AES_CACHE_SIZE; |
| 272 | dd->buflen &= ~(AES_BLOCK_SIZE - 1); |
| 273 | |
| 274 | if (!dd->buf_in || !dd->buf_out) { |
| 275 | dev_err(dd->dev, "unable to alloc pages.\n"); |
| 276 | goto err_alloc; |
| 277 | } |
| 278 | |
| 279 | /* MAP here */ |
| 280 | dd->dma_addr_in = dma_map_single(dd->dev, dd->buf_in, dd->buflen, |
| 281 | DMA_TO_DEVICE); |
| 282 | if (dma_mapping_error(dd->dev, dd->dma_addr_in)) { |
| 283 | dev_err(dd->dev, "dma %d bytes error\n", dd->buflen); |
| 284 | err = -EINVAL; |
| 285 | goto err_map_in; |
| 286 | } |
| 287 | |
| 288 | dd->dma_addr_out = dma_map_single(dd->dev, dd->buf_out, dd->buflen, |
| 289 | DMA_FROM_DEVICE); |
| 290 | if (dma_mapping_error(dd->dev, dd->dma_addr_out)) { |
| 291 | dev_err(dd->dev, "dma %d bytes error\n", dd->buflen); |
| 292 | err = -EINVAL; |
| 293 | goto err_map_out; |
| 294 | } |
| 295 | |
Mark A. Greer | ebedbf7 | 2013-01-08 11:57:42 -0700 | [diff] [blame] | 296 | dma_cap_zero(mask); |
| 297 | dma_cap_set(DMA_SLAVE, mask); |
| 298 | |
| 299 | dd->dma_lch_in = dma_request_channel(mask, omap_dma_filter_fn, |
| 300 | &dd->dma_in); |
| 301 | if (!dd->dma_lch_in) { |
| 302 | dev_err(dd->dev, "Unable to request in DMA channel\n"); |
| 303 | goto err_dma_in; |
| 304 | } |
| 305 | |
| 306 | dd->dma_lch_out = dma_request_channel(mask, omap_dma_filter_fn, |
| 307 | &dd->dma_out); |
| 308 | if (!dd->dma_lch_out) { |
| 309 | dev_err(dd->dev, "Unable to request out DMA channel\n"); |
| 310 | goto err_dma_out; |
| 311 | } |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 312 | |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 313 | return 0; |
| 314 | |
| 315 | err_dma_out: |
Mark A. Greer | ebedbf7 | 2013-01-08 11:57:42 -0700 | [diff] [blame] | 316 | dma_release_channel(dd->dma_lch_in); |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 317 | err_dma_in: |
| 318 | dma_unmap_single(dd->dev, dd->dma_addr_out, dd->buflen, |
| 319 | DMA_FROM_DEVICE); |
| 320 | err_map_out: |
| 321 | dma_unmap_single(dd->dev, dd->dma_addr_in, dd->buflen, DMA_TO_DEVICE); |
| 322 | err_map_in: |
| 323 | free_pages((unsigned long)dd->buf_out, OMAP_AES_CACHE_SIZE); |
| 324 | free_pages((unsigned long)dd->buf_in, OMAP_AES_CACHE_SIZE); |
| 325 | err_alloc: |
| 326 | if (err) |
| 327 | pr_err("error: %d\n", err); |
| 328 | return err; |
| 329 | } |
| 330 | |
| 331 | static void omap_aes_dma_cleanup(struct omap_aes_dev *dd) |
| 332 | { |
Mark A. Greer | ebedbf7 | 2013-01-08 11:57:42 -0700 | [diff] [blame] | 333 | dma_release_channel(dd->dma_lch_out); |
| 334 | dma_release_channel(dd->dma_lch_in); |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 335 | dma_unmap_single(dd->dev, dd->dma_addr_out, dd->buflen, |
| 336 | DMA_FROM_DEVICE); |
| 337 | dma_unmap_single(dd->dev, dd->dma_addr_in, dd->buflen, DMA_TO_DEVICE); |
| 338 | free_pages((unsigned long)dd->buf_out, OMAP_AES_CACHE_SIZE); |
| 339 | free_pages((unsigned long)dd->buf_in, OMAP_AES_CACHE_SIZE); |
| 340 | } |
| 341 | |
| 342 | static void sg_copy_buf(void *buf, struct scatterlist *sg, |
| 343 | unsigned int start, unsigned int nbytes, int out) |
| 344 | { |
| 345 | struct scatter_walk walk; |
| 346 | |
| 347 | if (!nbytes) |
| 348 | return; |
| 349 | |
| 350 | scatterwalk_start(&walk, sg); |
| 351 | scatterwalk_advance(&walk, start); |
| 352 | scatterwalk_copychunks(buf, &walk, nbytes, out); |
| 353 | scatterwalk_done(&walk, out, 0); |
| 354 | } |
| 355 | |
| 356 | static int sg_copy(struct scatterlist **sg, size_t *offset, void *buf, |
| 357 | size_t buflen, size_t total, int out) |
| 358 | { |
| 359 | unsigned int count, off = 0; |
| 360 | |
| 361 | while (buflen && total) { |
| 362 | count = min((*sg)->length - *offset, total); |
| 363 | count = min(count, buflen); |
| 364 | |
| 365 | if (!count) |
| 366 | return off; |
| 367 | |
Dmitry Kasatkin | 21fe976 | 2010-11-30 10:13:29 +0200 | [diff] [blame] | 368 | /* |
| 369 | * buflen and total are AES_BLOCK_SIZE size aligned, |
| 370 | * so count should be also aligned |
| 371 | */ |
| 372 | |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 373 | sg_copy_buf(buf + off, *sg, *offset, count, out); |
| 374 | |
| 375 | off += count; |
| 376 | buflen -= count; |
| 377 | *offset += count; |
| 378 | total -= count; |
| 379 | |
| 380 | if (*offset == (*sg)->length) { |
| 381 | *sg = sg_next(*sg); |
| 382 | if (*sg) |
| 383 | *offset = 0; |
| 384 | else |
| 385 | total = 0; |
| 386 | } |
| 387 | } |
| 388 | |
| 389 | return off; |
| 390 | } |
| 391 | |
Mark A. Greer | ebedbf7 | 2013-01-08 11:57:42 -0700 | [diff] [blame] | 392 | static int omap_aes_crypt_dma(struct crypto_tfm *tfm, |
| 393 | struct scatterlist *in_sg, struct scatterlist *out_sg) |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 394 | { |
| 395 | struct omap_aes_ctx *ctx = crypto_tfm_ctx(tfm); |
| 396 | struct omap_aes_dev *dd = ctx->dd; |
Mark A. Greer | ebedbf7 | 2013-01-08 11:57:42 -0700 | [diff] [blame] | 397 | struct dma_async_tx_descriptor *tx_in, *tx_out; |
| 398 | struct dma_slave_config cfg; |
| 399 | dma_addr_t dma_addr_in = sg_dma_address(in_sg); |
| 400 | int ret, length = sg_dma_len(in_sg); |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 401 | |
| 402 | pr_debug("len: %d\n", length); |
| 403 | |
| 404 | dd->dma_size = length; |
| 405 | |
| 406 | if (!(dd->flags & FLAGS_FAST)) |
| 407 | dma_sync_single_for_device(dd->dev, dma_addr_in, length, |
| 408 | DMA_TO_DEVICE); |
| 409 | |
Mark A. Greer | ebedbf7 | 2013-01-08 11:57:42 -0700 | [diff] [blame] | 410 | memset(&cfg, 0, sizeof(cfg)); |
| 411 | |
| 412 | cfg.src_addr = dd->phys_base + AES_REG_DATA; |
| 413 | cfg.dst_addr = dd->phys_base + AES_REG_DATA; |
| 414 | cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES; |
| 415 | cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES; |
| 416 | cfg.src_maxburst = DST_MAXBURST; |
| 417 | cfg.dst_maxburst = DST_MAXBURST; |
| 418 | |
| 419 | /* IN */ |
| 420 | ret = dmaengine_slave_config(dd->dma_lch_in, &cfg); |
| 421 | if (ret) { |
| 422 | dev_err(dd->dev, "can't configure IN dmaengine slave: %d\n", |
| 423 | ret); |
| 424 | return ret; |
| 425 | } |
| 426 | |
| 427 | tx_in = dmaengine_prep_slave_sg(dd->dma_lch_in, in_sg, 1, |
| 428 | DMA_MEM_TO_DEV, |
| 429 | DMA_PREP_INTERRUPT | DMA_CTRL_ACK); |
| 430 | if (!tx_in) { |
| 431 | dev_err(dd->dev, "IN prep_slave_sg() failed\n"); |
| 432 | return -EINVAL; |
| 433 | } |
| 434 | |
| 435 | /* No callback necessary */ |
| 436 | tx_in->callback_param = dd; |
| 437 | |
| 438 | /* OUT */ |
| 439 | ret = dmaengine_slave_config(dd->dma_lch_out, &cfg); |
| 440 | if (ret) { |
| 441 | dev_err(dd->dev, "can't configure OUT dmaengine slave: %d\n", |
| 442 | ret); |
| 443 | return ret; |
| 444 | } |
| 445 | |
| 446 | tx_out = dmaengine_prep_slave_sg(dd->dma_lch_out, out_sg, 1, |
| 447 | DMA_DEV_TO_MEM, |
| 448 | DMA_PREP_INTERRUPT | DMA_CTRL_ACK); |
| 449 | if (!tx_out) { |
| 450 | dev_err(dd->dev, "OUT prep_slave_sg() failed\n"); |
| 451 | return -EINVAL; |
| 452 | } |
| 453 | |
| 454 | tx_out->callback = omap_aes_dma_out_callback; |
| 455 | tx_out->callback_param = dd; |
| 456 | |
| 457 | dmaengine_submit(tx_in); |
| 458 | dmaengine_submit(tx_out); |
| 459 | |
| 460 | dma_async_issue_pending(dd->dma_lch_in); |
| 461 | dma_async_issue_pending(dd->dma_lch_out); |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 462 | |
Dmitry Kasatkin | 83ea7e0 | 2010-11-30 10:13:31 +0200 | [diff] [blame] | 463 | /* start DMA or disable idle mode */ |
| 464 | omap_aes_write_mask(dd, AES_REG_MASK, AES_REG_MASK_START, |
| 465 | AES_REG_MASK_START); |
| 466 | |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 467 | return 0; |
| 468 | } |
| 469 | |
| 470 | static int omap_aes_crypt_dma_start(struct omap_aes_dev *dd) |
| 471 | { |
| 472 | struct crypto_tfm *tfm = crypto_ablkcipher_tfm( |
| 473 | crypto_ablkcipher_reqtfm(dd->req)); |
| 474 | int err, fast = 0, in, out; |
| 475 | size_t count; |
| 476 | dma_addr_t addr_in, addr_out; |
Mark A. Greer | ebedbf7 | 2013-01-08 11:57:42 -0700 | [diff] [blame] | 477 | struct scatterlist *in_sg, *out_sg; |
| 478 | int len32; |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 479 | |
| 480 | pr_debug("total: %d\n", dd->total); |
| 481 | |
| 482 | if (sg_is_last(dd->in_sg) && sg_is_last(dd->out_sg)) { |
| 483 | /* check for alignment */ |
| 484 | in = IS_ALIGNED((u32)dd->in_sg->offset, sizeof(u32)); |
| 485 | out = IS_ALIGNED((u32)dd->out_sg->offset, sizeof(u32)); |
| 486 | |
| 487 | fast = in && out; |
| 488 | } |
| 489 | |
| 490 | if (fast) { |
| 491 | count = min(dd->total, sg_dma_len(dd->in_sg)); |
| 492 | count = min(count, sg_dma_len(dd->out_sg)); |
| 493 | |
Dmitry Kasatkin | 21fe976 | 2010-11-30 10:13:29 +0200 | [diff] [blame] | 494 | if (count != dd->total) { |
| 495 | pr_err("request length != buffer length\n"); |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 496 | return -EINVAL; |
Dmitry Kasatkin | 21fe976 | 2010-11-30 10:13:29 +0200 | [diff] [blame] | 497 | } |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 498 | |
| 499 | pr_debug("fast\n"); |
| 500 | |
| 501 | err = dma_map_sg(dd->dev, dd->in_sg, 1, DMA_TO_DEVICE); |
| 502 | if (!err) { |
| 503 | dev_err(dd->dev, "dma_map_sg() error\n"); |
| 504 | return -EINVAL; |
| 505 | } |
| 506 | |
| 507 | err = dma_map_sg(dd->dev, dd->out_sg, 1, DMA_FROM_DEVICE); |
| 508 | if (!err) { |
| 509 | dev_err(dd->dev, "dma_map_sg() error\n"); |
| 510 | dma_unmap_sg(dd->dev, dd->in_sg, 1, DMA_TO_DEVICE); |
| 511 | return -EINVAL; |
| 512 | } |
| 513 | |
| 514 | addr_in = sg_dma_address(dd->in_sg); |
| 515 | addr_out = sg_dma_address(dd->out_sg); |
| 516 | |
Mark A. Greer | ebedbf7 | 2013-01-08 11:57:42 -0700 | [diff] [blame] | 517 | in_sg = dd->in_sg; |
| 518 | out_sg = dd->out_sg; |
Mark A. Greer | ebedbf7 | 2013-01-08 11:57:42 -0700 | [diff] [blame] | 519 | |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 520 | dd->flags |= FLAGS_FAST; |
| 521 | |
| 522 | } else { |
| 523 | /* use cache buffers */ |
| 524 | count = sg_copy(&dd->in_sg, &dd->in_offset, dd->buf_in, |
| 525 | dd->buflen, dd->total, 0); |
| 526 | |
Mark A. Greer | ebedbf7 | 2013-01-08 11:57:42 -0700 | [diff] [blame] | 527 | len32 = DIV_ROUND_UP(count, DMA_MIN) * DMA_MIN; |
| 528 | |
| 529 | /* |
| 530 | * The data going into the AES module has been copied |
| 531 | * to a local buffer and the data coming out will go |
| 532 | * into a local buffer so set up local SG entries for |
| 533 | * both. |
| 534 | */ |
| 535 | sg_init_table(&dd->in_sgl, 1); |
| 536 | dd->in_sgl.offset = dd->in_offset; |
| 537 | sg_dma_len(&dd->in_sgl) = len32; |
| 538 | sg_dma_address(&dd->in_sgl) = dd->dma_addr_in; |
| 539 | |
| 540 | sg_init_table(&dd->out_sgl, 1); |
| 541 | dd->out_sgl.offset = dd->out_offset; |
| 542 | sg_dma_len(&dd->out_sgl) = len32; |
| 543 | sg_dma_address(&dd->out_sgl) = dd->dma_addr_out; |
| 544 | |
| 545 | in_sg = &dd->in_sgl; |
| 546 | out_sg = &dd->out_sgl; |
Mark A. Greer | ebedbf7 | 2013-01-08 11:57:42 -0700 | [diff] [blame] | 547 | |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 548 | addr_in = dd->dma_addr_in; |
| 549 | addr_out = dd->dma_addr_out; |
| 550 | |
| 551 | dd->flags &= ~FLAGS_FAST; |
| 552 | |
| 553 | } |
| 554 | |
| 555 | dd->total -= count; |
| 556 | |
Mark A. Greer | ebedbf7 | 2013-01-08 11:57:42 -0700 | [diff] [blame] | 557 | err = omap_aes_crypt_dma(tfm, in_sg, out_sg); |
Dmitry Kasatkin | 21fe976 | 2010-11-30 10:13:29 +0200 | [diff] [blame] | 558 | if (err) { |
| 559 | dma_unmap_sg(dd->dev, dd->in_sg, 1, DMA_TO_DEVICE); |
| 560 | dma_unmap_sg(dd->dev, dd->out_sg, 1, DMA_TO_DEVICE); |
| 561 | } |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 562 | |
| 563 | return err; |
| 564 | } |
| 565 | |
| 566 | static void omap_aes_finish_req(struct omap_aes_dev *dd, int err) |
| 567 | { |
Dmitry Kasatkin | 21fe976 | 2010-11-30 10:13:29 +0200 | [diff] [blame] | 568 | struct ablkcipher_request *req = dd->req; |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 569 | |
| 570 | pr_debug("err: %d\n", err); |
| 571 | |
Mark A. Greer | 5946c4a | 2013-01-08 11:57:40 -0700 | [diff] [blame] | 572 | pm_runtime_put_sync(dd->dev); |
Dmitry Kasatkin | eeb2b20 | 2010-11-30 10:13:28 +0200 | [diff] [blame] | 573 | dd->flags &= ~FLAGS_BUSY; |
| 574 | |
Dmitry Kasatkin | 67a730c | 2010-11-30 10:13:30 +0200 | [diff] [blame] | 575 | req->base.complete(&req->base, err); |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 576 | } |
| 577 | |
| 578 | static int omap_aes_crypt_dma_stop(struct omap_aes_dev *dd) |
| 579 | { |
| 580 | int err = 0; |
| 581 | size_t count; |
| 582 | |
| 583 | pr_debug("total: %d\n", dd->total); |
| 584 | |
| 585 | omap_aes_write_mask(dd, AES_REG_MASK, 0, AES_REG_MASK_START); |
| 586 | |
Mark A. Greer | ebedbf7 | 2013-01-08 11:57:42 -0700 | [diff] [blame] | 587 | dmaengine_terminate_all(dd->dma_lch_in); |
| 588 | dmaengine_terminate_all(dd->dma_lch_out); |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 589 | |
| 590 | if (dd->flags & FLAGS_FAST) { |
| 591 | dma_unmap_sg(dd->dev, dd->out_sg, 1, DMA_FROM_DEVICE); |
| 592 | dma_unmap_sg(dd->dev, dd->in_sg, 1, DMA_TO_DEVICE); |
| 593 | } else { |
| 594 | dma_sync_single_for_device(dd->dev, dd->dma_addr_out, |
| 595 | dd->dma_size, DMA_FROM_DEVICE); |
| 596 | |
| 597 | /* copy data */ |
| 598 | count = sg_copy(&dd->out_sg, &dd->out_offset, dd->buf_out, |
| 599 | dd->buflen, dd->dma_size, 1); |
| 600 | if (count != dd->dma_size) { |
| 601 | err = -EINVAL; |
| 602 | pr_err("not all data converted: %u\n", count); |
| 603 | } |
| 604 | } |
| 605 | |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 606 | return err; |
| 607 | } |
| 608 | |
Dmitry Kasatkin | 21fe976 | 2010-11-30 10:13:29 +0200 | [diff] [blame] | 609 | static int omap_aes_handle_queue(struct omap_aes_dev *dd, |
Dmitry Kasatkin | eeb2b20 | 2010-11-30 10:13:28 +0200 | [diff] [blame] | 610 | struct ablkcipher_request *req) |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 611 | { |
| 612 | struct crypto_async_request *async_req, *backlog; |
| 613 | struct omap_aes_ctx *ctx; |
| 614 | struct omap_aes_reqctx *rctx; |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 615 | unsigned long flags; |
Dmitry Kasatkin | 21fe976 | 2010-11-30 10:13:29 +0200 | [diff] [blame] | 616 | int err, ret = 0; |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 617 | |
| 618 | spin_lock_irqsave(&dd->lock, flags); |
Dmitry Kasatkin | eeb2b20 | 2010-11-30 10:13:28 +0200 | [diff] [blame] | 619 | if (req) |
Dmitry Kasatkin | 21fe976 | 2010-11-30 10:13:29 +0200 | [diff] [blame] | 620 | ret = ablkcipher_enqueue_request(&dd->queue, req); |
Dmitry Kasatkin | eeb2b20 | 2010-11-30 10:13:28 +0200 | [diff] [blame] | 621 | if (dd->flags & FLAGS_BUSY) { |
| 622 | spin_unlock_irqrestore(&dd->lock, flags); |
Dmitry Kasatkin | 21fe976 | 2010-11-30 10:13:29 +0200 | [diff] [blame] | 623 | return ret; |
Dmitry Kasatkin | eeb2b20 | 2010-11-30 10:13:28 +0200 | [diff] [blame] | 624 | } |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 625 | backlog = crypto_get_backlog(&dd->queue); |
| 626 | async_req = crypto_dequeue_request(&dd->queue); |
Dmitry Kasatkin | eeb2b20 | 2010-11-30 10:13:28 +0200 | [diff] [blame] | 627 | if (async_req) |
| 628 | dd->flags |= FLAGS_BUSY; |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 629 | spin_unlock_irqrestore(&dd->lock, flags); |
| 630 | |
| 631 | if (!async_req) |
Dmitry Kasatkin | 21fe976 | 2010-11-30 10:13:29 +0200 | [diff] [blame] | 632 | return ret; |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 633 | |
| 634 | if (backlog) |
| 635 | backlog->complete(backlog, -EINPROGRESS); |
| 636 | |
| 637 | req = ablkcipher_request_cast(async_req); |
| 638 | |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 639 | /* assign new request to device */ |
| 640 | dd->req = req; |
| 641 | dd->total = req->nbytes; |
| 642 | dd->in_offset = 0; |
| 643 | dd->in_sg = req->src; |
| 644 | dd->out_offset = 0; |
| 645 | dd->out_sg = req->dst; |
| 646 | |
| 647 | rctx = ablkcipher_request_ctx(req); |
| 648 | ctx = crypto_ablkcipher_ctx(crypto_ablkcipher_reqtfm(req)); |
| 649 | rctx->mode &= FLAGS_MODE_MASK; |
| 650 | dd->flags = (dd->flags & ~FLAGS_MODE_MASK) | rctx->mode; |
| 651 | |
Dmitry Kasatkin | 67a730c | 2010-11-30 10:13:30 +0200 | [diff] [blame] | 652 | dd->ctx = ctx; |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 653 | ctx->dd = dd; |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 654 | |
Dmitry Kasatkin | 83ea7e0 | 2010-11-30 10:13:31 +0200 | [diff] [blame] | 655 | err = omap_aes_write_ctrl(dd); |
| 656 | if (!err) |
| 657 | err = omap_aes_crypt_dma_start(dd); |
Dmitry Kasatkin | 21fe976 | 2010-11-30 10:13:29 +0200 | [diff] [blame] | 658 | if (err) { |
| 659 | /* aes_task will not finish it, so do it here */ |
| 660 | omap_aes_finish_req(dd, err); |
| 661 | tasklet_schedule(&dd->queue_task); |
| 662 | } |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 663 | |
Dmitry Kasatkin | 21fe976 | 2010-11-30 10:13:29 +0200 | [diff] [blame] | 664 | return ret; /* return ret, which is enqueue return value */ |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 665 | } |
| 666 | |
Dmitry Kasatkin | 21fe976 | 2010-11-30 10:13:29 +0200 | [diff] [blame] | 667 | static void omap_aes_done_task(unsigned long data) |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 668 | { |
| 669 | struct omap_aes_dev *dd = (struct omap_aes_dev *)data; |
Dmitry Kasatkin | 21fe976 | 2010-11-30 10:13:29 +0200 | [diff] [blame] | 670 | int err; |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 671 | |
| 672 | pr_debug("enter\n"); |
| 673 | |
Dmitry Kasatkin | 21fe976 | 2010-11-30 10:13:29 +0200 | [diff] [blame] | 674 | err = omap_aes_crypt_dma_stop(dd); |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 675 | |
Dmitry Kasatkin | 21fe976 | 2010-11-30 10:13:29 +0200 | [diff] [blame] | 676 | err = dd->err ? : err; |
| 677 | |
| 678 | if (dd->total && !err) { |
| 679 | err = omap_aes_crypt_dma_start(dd); |
| 680 | if (!err) |
| 681 | return; /* DMA started. Not fininishing. */ |
| 682 | } |
| 683 | |
| 684 | omap_aes_finish_req(dd, err); |
| 685 | omap_aes_handle_queue(dd, NULL); |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 686 | |
| 687 | pr_debug("exit\n"); |
| 688 | } |
| 689 | |
Dmitry Kasatkin | 21fe976 | 2010-11-30 10:13:29 +0200 | [diff] [blame] | 690 | static void omap_aes_queue_task(unsigned long data) |
| 691 | { |
| 692 | struct omap_aes_dev *dd = (struct omap_aes_dev *)data; |
| 693 | |
| 694 | omap_aes_handle_queue(dd, NULL); |
| 695 | } |
| 696 | |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 697 | static int omap_aes_crypt(struct ablkcipher_request *req, unsigned long mode) |
| 698 | { |
| 699 | struct omap_aes_ctx *ctx = crypto_ablkcipher_ctx( |
| 700 | crypto_ablkcipher_reqtfm(req)); |
| 701 | struct omap_aes_reqctx *rctx = ablkcipher_request_ctx(req); |
| 702 | struct omap_aes_dev *dd; |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 703 | |
| 704 | pr_debug("nbytes: %d, enc: %d, cbc: %d\n", req->nbytes, |
| 705 | !!(mode & FLAGS_ENCRYPT), |
| 706 | !!(mode & FLAGS_CBC)); |
| 707 | |
Dmitry Kasatkin | 21fe976 | 2010-11-30 10:13:29 +0200 | [diff] [blame] | 708 | if (!IS_ALIGNED(req->nbytes, AES_BLOCK_SIZE)) { |
| 709 | pr_err("request size is not exact amount of AES blocks\n"); |
| 710 | return -EINVAL; |
| 711 | } |
| 712 | |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 713 | dd = omap_aes_find_dev(ctx); |
| 714 | if (!dd) |
| 715 | return -ENODEV; |
| 716 | |
| 717 | rctx->mode = mode; |
| 718 | |
Dmitry Kasatkin | 21fe976 | 2010-11-30 10:13:29 +0200 | [diff] [blame] | 719 | return omap_aes_handle_queue(dd, req); |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 720 | } |
| 721 | |
| 722 | /* ********************** ALG API ************************************ */ |
| 723 | |
| 724 | static int omap_aes_setkey(struct crypto_ablkcipher *tfm, const u8 *key, |
| 725 | unsigned int keylen) |
| 726 | { |
| 727 | struct omap_aes_ctx *ctx = crypto_ablkcipher_ctx(tfm); |
| 728 | |
| 729 | if (keylen != AES_KEYSIZE_128 && keylen != AES_KEYSIZE_192 && |
| 730 | keylen != AES_KEYSIZE_256) |
| 731 | return -EINVAL; |
| 732 | |
| 733 | pr_debug("enter, keylen: %d\n", keylen); |
| 734 | |
| 735 | memcpy(ctx->key, key, keylen); |
| 736 | ctx->keylen = keylen; |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 737 | |
| 738 | return 0; |
| 739 | } |
| 740 | |
| 741 | static int omap_aes_ecb_encrypt(struct ablkcipher_request *req) |
| 742 | { |
| 743 | return omap_aes_crypt(req, FLAGS_ENCRYPT); |
| 744 | } |
| 745 | |
| 746 | static int omap_aes_ecb_decrypt(struct ablkcipher_request *req) |
| 747 | { |
| 748 | return omap_aes_crypt(req, 0); |
| 749 | } |
| 750 | |
| 751 | static int omap_aes_cbc_encrypt(struct ablkcipher_request *req) |
| 752 | { |
| 753 | return omap_aes_crypt(req, FLAGS_ENCRYPT | FLAGS_CBC); |
| 754 | } |
| 755 | |
| 756 | static int omap_aes_cbc_decrypt(struct ablkcipher_request *req) |
| 757 | { |
| 758 | return omap_aes_crypt(req, FLAGS_CBC); |
| 759 | } |
| 760 | |
| 761 | static int omap_aes_cra_init(struct crypto_tfm *tfm) |
| 762 | { |
| 763 | pr_debug("enter\n"); |
| 764 | |
| 765 | tfm->crt_ablkcipher.reqsize = sizeof(struct omap_aes_reqctx); |
| 766 | |
| 767 | return 0; |
| 768 | } |
| 769 | |
| 770 | static void omap_aes_cra_exit(struct crypto_tfm *tfm) |
| 771 | { |
| 772 | pr_debug("enter\n"); |
| 773 | } |
| 774 | |
| 775 | /* ********************** ALGS ************************************ */ |
| 776 | |
| 777 | static struct crypto_alg algs[] = { |
| 778 | { |
| 779 | .cra_name = "ecb(aes)", |
| 780 | .cra_driver_name = "ecb-aes-omap", |
| 781 | .cra_priority = 100, |
Nikos Mavrogiannopoulos | d912bb7 | 2011-11-01 13:39:56 +0100 | [diff] [blame] | 782 | .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | |
| 783 | CRYPTO_ALG_KERN_DRIVER_ONLY | |
| 784 | CRYPTO_ALG_ASYNC, |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 785 | .cra_blocksize = AES_BLOCK_SIZE, |
| 786 | .cra_ctxsize = sizeof(struct omap_aes_ctx), |
Dmitry Kasatkin | efce41b | 2010-11-30 10:13:32 +0200 | [diff] [blame] | 787 | .cra_alignmask = 0, |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 788 | .cra_type = &crypto_ablkcipher_type, |
| 789 | .cra_module = THIS_MODULE, |
| 790 | .cra_init = omap_aes_cra_init, |
| 791 | .cra_exit = omap_aes_cra_exit, |
| 792 | .cra_u.ablkcipher = { |
| 793 | .min_keysize = AES_MIN_KEY_SIZE, |
| 794 | .max_keysize = AES_MAX_KEY_SIZE, |
| 795 | .setkey = omap_aes_setkey, |
| 796 | .encrypt = omap_aes_ecb_encrypt, |
| 797 | .decrypt = omap_aes_ecb_decrypt, |
| 798 | } |
| 799 | }, |
| 800 | { |
| 801 | .cra_name = "cbc(aes)", |
| 802 | .cra_driver_name = "cbc-aes-omap", |
| 803 | .cra_priority = 100, |
Nikos Mavrogiannopoulos | d912bb7 | 2011-11-01 13:39:56 +0100 | [diff] [blame] | 804 | .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | |
| 805 | CRYPTO_ALG_KERN_DRIVER_ONLY | |
| 806 | CRYPTO_ALG_ASYNC, |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 807 | .cra_blocksize = AES_BLOCK_SIZE, |
| 808 | .cra_ctxsize = sizeof(struct omap_aes_ctx), |
Dmitry Kasatkin | efce41b | 2010-11-30 10:13:32 +0200 | [diff] [blame] | 809 | .cra_alignmask = 0, |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 810 | .cra_type = &crypto_ablkcipher_type, |
| 811 | .cra_module = THIS_MODULE, |
| 812 | .cra_init = omap_aes_cra_init, |
| 813 | .cra_exit = omap_aes_cra_exit, |
| 814 | .cra_u.ablkcipher = { |
| 815 | .min_keysize = AES_MIN_KEY_SIZE, |
| 816 | .max_keysize = AES_MAX_KEY_SIZE, |
| 817 | .ivsize = AES_BLOCK_SIZE, |
| 818 | .setkey = omap_aes_setkey, |
| 819 | .encrypt = omap_aes_cbc_encrypt, |
| 820 | .decrypt = omap_aes_cbc_decrypt, |
| 821 | } |
| 822 | } |
| 823 | }; |
| 824 | |
Mark A. Greer | bc69d12 | 2013-01-08 11:57:44 -0700 | [diff] [blame^] | 825 | #ifdef CONFIG_OF |
| 826 | static const struct of_device_id omap_aes_of_match[] = { |
| 827 | { |
| 828 | .compatible = "ti,omap2-aes", |
| 829 | }, |
| 830 | {}, |
| 831 | }; |
| 832 | MODULE_DEVICE_TABLE(of, omap_aes_of_match); |
| 833 | |
| 834 | static int omap_aes_get_res_of(struct omap_aes_dev *dd, |
| 835 | struct device *dev, struct resource *res) |
| 836 | { |
| 837 | struct device_node *node = dev->of_node; |
| 838 | const struct of_device_id *match; |
| 839 | int err = 0; |
| 840 | |
| 841 | match = of_match_device(of_match_ptr(omap_aes_of_match), dev); |
| 842 | if (!match) { |
| 843 | dev_err(dev, "no compatible OF match\n"); |
| 844 | err = -EINVAL; |
| 845 | goto err; |
| 846 | } |
| 847 | |
| 848 | err = of_address_to_resource(node, 0, res); |
| 849 | if (err < 0) { |
| 850 | dev_err(dev, "can't translate OF node address\n"); |
| 851 | err = -EINVAL; |
| 852 | goto err; |
| 853 | } |
| 854 | |
| 855 | dd->dma_out = -1; /* Dummy value that's unused */ |
| 856 | dd->dma_in = -1; /* Dummy value that's unused */ |
| 857 | |
| 858 | err: |
| 859 | return err; |
| 860 | } |
| 861 | #else |
| 862 | static const struct of_device_id omap_aes_of_match[] = { |
| 863 | {}, |
| 864 | }; |
| 865 | |
| 866 | static int omap_aes_get_res_of(struct omap_aes_dev *dd, |
| 867 | struct device *dev, struct resource *res) |
| 868 | { |
| 869 | return -EINVAL; |
| 870 | } |
| 871 | #endif |
| 872 | |
| 873 | static int omap_aes_get_res_pdev(struct omap_aes_dev *dd, |
| 874 | struct platform_device *pdev, struct resource *res) |
| 875 | { |
| 876 | struct device *dev = &pdev->dev; |
| 877 | struct resource *r; |
| 878 | int err = 0; |
| 879 | |
| 880 | /* Get the base address */ |
| 881 | r = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 882 | if (!r) { |
| 883 | dev_err(dev, "no MEM resource info\n"); |
| 884 | err = -ENODEV; |
| 885 | goto err; |
| 886 | } |
| 887 | memcpy(res, r, sizeof(*res)); |
| 888 | |
| 889 | /* Get the DMA out channel */ |
| 890 | r = platform_get_resource(pdev, IORESOURCE_DMA, 0); |
| 891 | if (!r) { |
| 892 | dev_err(dev, "no DMA out resource info\n"); |
| 893 | err = -ENODEV; |
| 894 | goto err; |
| 895 | } |
| 896 | dd->dma_out = r->start; |
| 897 | |
| 898 | /* Get the DMA in channel */ |
| 899 | r = platform_get_resource(pdev, IORESOURCE_DMA, 1); |
| 900 | if (!r) { |
| 901 | dev_err(dev, "no DMA in resource info\n"); |
| 902 | err = -ENODEV; |
| 903 | goto err; |
| 904 | } |
| 905 | dd->dma_in = r->start; |
| 906 | |
| 907 | err: |
| 908 | return err; |
| 909 | } |
| 910 | |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 911 | static int omap_aes_probe(struct platform_device *pdev) |
| 912 | { |
| 913 | struct device *dev = &pdev->dev; |
| 914 | struct omap_aes_dev *dd; |
Mark A. Greer | bc69d12 | 2013-01-08 11:57:44 -0700 | [diff] [blame^] | 915 | struct resource res; |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 916 | int err = -ENOMEM, i, j; |
| 917 | u32 reg; |
| 918 | |
| 919 | dd = kzalloc(sizeof(struct omap_aes_dev), GFP_KERNEL); |
| 920 | if (dd == NULL) { |
| 921 | dev_err(dev, "unable to alloc data struct.\n"); |
| 922 | goto err_data; |
| 923 | } |
| 924 | dd->dev = dev; |
| 925 | platform_set_drvdata(pdev, dd); |
| 926 | |
| 927 | spin_lock_init(&dd->lock); |
| 928 | crypto_init_queue(&dd->queue, OMAP_AES_QUEUE_LENGTH); |
| 929 | |
Mark A. Greer | bc69d12 | 2013-01-08 11:57:44 -0700 | [diff] [blame^] | 930 | err = (dev->of_node) ? omap_aes_get_res_of(dd, dev, &res) : |
| 931 | omap_aes_get_res_pdev(dd, pdev, &res); |
| 932 | if (err) |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 933 | goto err_res; |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 934 | |
Mark A. Greer | bc69d12 | 2013-01-08 11:57:44 -0700 | [diff] [blame^] | 935 | dd->io_base = devm_request_and_ioremap(dev, &res); |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 936 | if (!dd->io_base) { |
| 937 | dev_err(dev, "can't ioremap\n"); |
| 938 | err = -ENOMEM; |
Mark A. Greer | 5946c4a | 2013-01-08 11:57:40 -0700 | [diff] [blame] | 939 | goto err_res; |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 940 | } |
Mark A. Greer | bc69d12 | 2013-01-08 11:57:44 -0700 | [diff] [blame^] | 941 | dd->phys_base = res.start; |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 942 | |
Mark A. Greer | 5946c4a | 2013-01-08 11:57:40 -0700 | [diff] [blame] | 943 | pm_runtime_enable(dev); |
| 944 | pm_runtime_get_sync(dev); |
| 945 | |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 946 | reg = omap_aes_read(dd, AES_REG_REV); |
| 947 | dev_info(dev, "OMAP AES hw accel rev: %u.%u\n", |
| 948 | (reg & AES_REG_REV_MAJOR) >> 4, reg & AES_REG_REV_MINOR); |
Mark A. Greer | 5946c4a | 2013-01-08 11:57:40 -0700 | [diff] [blame] | 949 | |
| 950 | pm_runtime_put_sync(dev); |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 951 | |
Dmitry Kasatkin | 21fe976 | 2010-11-30 10:13:29 +0200 | [diff] [blame] | 952 | tasklet_init(&dd->done_task, omap_aes_done_task, (unsigned long)dd); |
| 953 | tasklet_init(&dd->queue_task, omap_aes_queue_task, (unsigned long)dd); |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 954 | |
| 955 | err = omap_aes_dma_init(dd); |
| 956 | if (err) |
| 957 | goto err_dma; |
| 958 | |
| 959 | INIT_LIST_HEAD(&dd->list); |
| 960 | spin_lock(&list_lock); |
| 961 | list_add_tail(&dd->list, &dev_list); |
| 962 | spin_unlock(&list_lock); |
| 963 | |
| 964 | for (i = 0; i < ARRAY_SIZE(algs); i++) { |
| 965 | pr_debug("i: %d\n", i); |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 966 | err = crypto_register_alg(&algs[i]); |
| 967 | if (err) |
| 968 | goto err_algs; |
| 969 | } |
| 970 | |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 971 | return 0; |
| 972 | err_algs: |
| 973 | for (j = 0; j < i; j++) |
| 974 | crypto_unregister_alg(&algs[j]); |
| 975 | omap_aes_dma_cleanup(dd); |
| 976 | err_dma: |
Dmitry Kasatkin | 21fe976 | 2010-11-30 10:13:29 +0200 | [diff] [blame] | 977 | tasklet_kill(&dd->done_task); |
| 978 | tasklet_kill(&dd->queue_task); |
Mark A. Greer | 5946c4a | 2013-01-08 11:57:40 -0700 | [diff] [blame] | 979 | pm_runtime_disable(dev); |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 980 | err_res: |
| 981 | kfree(dd); |
| 982 | dd = NULL; |
| 983 | err_data: |
| 984 | dev_err(dev, "initialization failed.\n"); |
| 985 | return err; |
| 986 | } |
| 987 | |
| 988 | static int omap_aes_remove(struct platform_device *pdev) |
| 989 | { |
| 990 | struct omap_aes_dev *dd = platform_get_drvdata(pdev); |
| 991 | int i; |
| 992 | |
| 993 | if (!dd) |
| 994 | return -ENODEV; |
| 995 | |
| 996 | spin_lock(&list_lock); |
| 997 | list_del(&dd->list); |
| 998 | spin_unlock(&list_lock); |
| 999 | |
| 1000 | for (i = 0; i < ARRAY_SIZE(algs); i++) |
| 1001 | crypto_unregister_alg(&algs[i]); |
| 1002 | |
Dmitry Kasatkin | 21fe976 | 2010-11-30 10:13:29 +0200 | [diff] [blame] | 1003 | tasklet_kill(&dd->done_task); |
| 1004 | tasklet_kill(&dd->queue_task); |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 1005 | omap_aes_dma_cleanup(dd); |
Mark A. Greer | 5946c4a | 2013-01-08 11:57:40 -0700 | [diff] [blame] | 1006 | pm_runtime_disable(dd->dev); |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 1007 | kfree(dd); |
| 1008 | dd = NULL; |
| 1009 | |
| 1010 | return 0; |
| 1011 | } |
| 1012 | |
Mark A. Greer | 0635fb3 | 2013-01-08 11:57:41 -0700 | [diff] [blame] | 1013 | #ifdef CONFIG_PM_SLEEP |
| 1014 | static int omap_aes_suspend(struct device *dev) |
| 1015 | { |
| 1016 | pm_runtime_put_sync(dev); |
| 1017 | return 0; |
| 1018 | } |
| 1019 | |
| 1020 | static int omap_aes_resume(struct device *dev) |
| 1021 | { |
| 1022 | pm_runtime_get_sync(dev); |
| 1023 | return 0; |
| 1024 | } |
| 1025 | #endif |
| 1026 | |
| 1027 | static const struct dev_pm_ops omap_aes_pm_ops = { |
| 1028 | SET_SYSTEM_SLEEP_PM_OPS(omap_aes_suspend, omap_aes_resume) |
| 1029 | }; |
| 1030 | |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 1031 | static struct platform_driver omap_aes_driver = { |
| 1032 | .probe = omap_aes_probe, |
| 1033 | .remove = omap_aes_remove, |
| 1034 | .driver = { |
| 1035 | .name = "omap-aes", |
| 1036 | .owner = THIS_MODULE, |
Mark A. Greer | 0635fb3 | 2013-01-08 11:57:41 -0700 | [diff] [blame] | 1037 | .pm = &omap_aes_pm_ops, |
Mark A. Greer | bc69d12 | 2013-01-08 11:57:44 -0700 | [diff] [blame^] | 1038 | .of_match_table = omap_aes_of_match, |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 1039 | }, |
| 1040 | }; |
| 1041 | |
| 1042 | static int __init omap_aes_mod_init(void) |
| 1043 | { |
Dmitry Kasatkin | 537559a | 2010-09-03 19:16:02 +0800 | [diff] [blame] | 1044 | return platform_driver_register(&omap_aes_driver); |
| 1045 | } |
| 1046 | |
| 1047 | static void __exit omap_aes_mod_exit(void) |
| 1048 | { |
| 1049 | platform_driver_unregister(&omap_aes_driver); |
| 1050 | } |
| 1051 | |
| 1052 | module_init(omap_aes_mod_init); |
| 1053 | module_exit(omap_aes_mod_exit); |
| 1054 | |
| 1055 | MODULE_DESCRIPTION("OMAP AES hw acceleration support."); |
| 1056 | MODULE_LICENSE("GPL v2"); |
| 1057 | MODULE_AUTHOR("Dmitry Kasatkin"); |
| 1058 | |