Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Cryptographic API. |
| 3 | * |
| 4 | * Support for ATMEL AES HW acceleration. |
| 5 | * |
| 6 | * Copyright (c) 2012 Eukréa Electromatique - ATMEL |
| 7 | * Author: Nicolas Royer <nicolas@eukrea.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 | * Some ideas are from omap-aes.c driver. |
| 14 | */ |
| 15 | |
| 16 | |
| 17 | #include <linux/kernel.h> |
| 18 | #include <linux/module.h> |
| 19 | #include <linux/slab.h> |
| 20 | #include <linux/err.h> |
| 21 | #include <linux/clk.h> |
| 22 | #include <linux/io.h> |
| 23 | #include <linux/hw_random.h> |
| 24 | #include <linux/platform_device.h> |
| 25 | |
| 26 | #include <linux/device.h> |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 27 | #include <linux/init.h> |
| 28 | #include <linux/errno.h> |
| 29 | #include <linux/interrupt.h> |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 30 | #include <linux/irq.h> |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 31 | #include <linux/scatterlist.h> |
| 32 | #include <linux/dma-mapping.h> |
Nicolas Ferre | be943c7 | 2013-10-14 17:52:38 +0200 | [diff] [blame] | 33 | #include <linux/of_device.h> |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 34 | #include <linux/delay.h> |
| 35 | #include <linux/crypto.h> |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 36 | #include <crypto/scatterwalk.h> |
| 37 | #include <crypto/algapi.h> |
| 38 | #include <crypto/aes.h> |
Nicolas Royer | cadc4ab | 2013-02-20 17:10:24 +0100 | [diff] [blame] | 39 | #include <linux/platform_data/crypto-atmel.h> |
Nicolas Ferre | be943c7 | 2013-10-14 17:52:38 +0200 | [diff] [blame] | 40 | #include <dt-bindings/dma/at91.h> |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 41 | #include "atmel-aes-regs.h" |
| 42 | |
Cyrille Pitchen | 88efd9a | 2015-12-17 17:48:34 +0100 | [diff] [blame] | 43 | #define ATMEL_AES_PRIORITY 300 |
| 44 | |
Cyrille Pitchen | bbe628e | 2015-12-17 18:13:00 +0100 | [diff] [blame] | 45 | #define ATMEL_AES_BUFFER_ORDER 2 |
| 46 | #define ATMEL_AES_BUFFER_SIZE (PAGE_SIZE << ATMEL_AES_BUFFER_ORDER) |
| 47 | |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 48 | #define CFB8_BLOCK_SIZE 1 |
| 49 | #define CFB16_BLOCK_SIZE 2 |
| 50 | #define CFB32_BLOCK_SIZE 4 |
| 51 | #define CFB64_BLOCK_SIZE 8 |
| 52 | |
Cyrille Pitchen | bbe628e | 2015-12-17 18:13:00 +0100 | [diff] [blame] | 53 | #define SIZE_IN_WORDS(x) ((x) >> 2) |
| 54 | |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 55 | /* AES flags */ |
Cyrille Pitchen | 77dacf5 | 2015-12-17 17:48:41 +0100 | [diff] [blame] | 56 | /* Reserve bits [18:16] [14:12] [0] for mode (same as for AES_MR) */ |
| 57 | #define AES_FLAGS_ENCRYPT AES_MR_CYPHER_ENC |
| 58 | #define AES_FLAGS_OPMODE_MASK (AES_MR_OPMOD_MASK | AES_MR_CFBS_MASK) |
| 59 | #define AES_FLAGS_ECB AES_MR_OPMOD_ECB |
| 60 | #define AES_FLAGS_CBC AES_MR_OPMOD_CBC |
| 61 | #define AES_FLAGS_OFB AES_MR_OPMOD_OFB |
| 62 | #define AES_FLAGS_CFB128 (AES_MR_OPMOD_CFB | AES_MR_CFBS_128b) |
| 63 | #define AES_FLAGS_CFB64 (AES_MR_OPMOD_CFB | AES_MR_CFBS_64b) |
| 64 | #define AES_FLAGS_CFB32 (AES_MR_OPMOD_CFB | AES_MR_CFBS_32b) |
| 65 | #define AES_FLAGS_CFB16 (AES_MR_OPMOD_CFB | AES_MR_CFBS_16b) |
| 66 | #define AES_FLAGS_CFB8 (AES_MR_OPMOD_CFB | AES_MR_CFBS_8b) |
| 67 | #define AES_FLAGS_CTR AES_MR_OPMOD_CTR |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 68 | |
Cyrille Pitchen | 77dacf5 | 2015-12-17 17:48:41 +0100 | [diff] [blame] | 69 | #define AES_FLAGS_MODE_MASK (AES_FLAGS_OPMODE_MASK | \ |
| 70 | AES_FLAGS_ENCRYPT) |
| 71 | |
| 72 | #define AES_FLAGS_INIT BIT(2) |
| 73 | #define AES_FLAGS_BUSY BIT(3) |
Cyrille Pitchen | 77dacf5 | 2015-12-17 17:48:41 +0100 | [diff] [blame] | 74 | |
| 75 | #define AES_FLAGS_PERSISTENT (AES_FLAGS_INIT | AES_FLAGS_BUSY) |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 76 | |
Nicolas Royer | cadc4ab | 2013-02-20 17:10:24 +0100 | [diff] [blame] | 77 | #define ATMEL_AES_QUEUE_LENGTH 50 |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 78 | |
| 79 | #define ATMEL_AES_DMA_THRESHOLD 16 |
| 80 | |
| 81 | |
Nicolas Royer | cadc4ab | 2013-02-20 17:10:24 +0100 | [diff] [blame] | 82 | struct atmel_aes_caps { |
Cyrille Pitchen | afbac17 | 2015-12-17 18:13:02 +0100 | [diff] [blame] | 83 | bool has_dualbuff; |
| 84 | bool has_cfb64; |
| 85 | u32 max_burst_size; |
Nicolas Royer | cadc4ab | 2013-02-20 17:10:24 +0100 | [diff] [blame] | 86 | }; |
| 87 | |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 88 | struct atmel_aes_dev; |
| 89 | |
Cyrille Pitchen | ccbf729 | 2015-12-17 17:48:39 +0100 | [diff] [blame] | 90 | |
| 91 | typedef int (*atmel_aes_fn_t)(struct atmel_aes_dev *); |
| 92 | |
| 93 | |
| 94 | struct atmel_aes_base_ctx { |
Cyrille Pitchen | afbac17 | 2015-12-17 18:13:02 +0100 | [diff] [blame] | 95 | struct atmel_aes_dev *dd; |
| 96 | atmel_aes_fn_t start; |
| 97 | int keylen; |
| 98 | u32 key[AES_KEYSIZE_256 / sizeof(u32)]; |
| 99 | u16 block_size; |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 100 | }; |
| 101 | |
Cyrille Pitchen | ccbf729 | 2015-12-17 17:48:39 +0100 | [diff] [blame] | 102 | struct atmel_aes_ctx { |
| 103 | struct atmel_aes_base_ctx base; |
| 104 | }; |
| 105 | |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 106 | struct atmel_aes_reqctx { |
Cyrille Pitchen | afbac17 | 2015-12-17 18:13:02 +0100 | [diff] [blame] | 107 | unsigned long mode; |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 108 | }; |
| 109 | |
| 110 | struct atmel_aes_dma { |
Cyrille Pitchen | bbe628e | 2015-12-17 18:13:00 +0100 | [diff] [blame] | 111 | struct dma_chan *chan; |
| 112 | struct scatterlist *sg; |
| 113 | int nents; |
| 114 | unsigned int remainder; |
| 115 | unsigned int sg_len; |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 116 | }; |
| 117 | |
| 118 | struct atmel_aes_dev { |
| 119 | struct list_head list; |
| 120 | unsigned long phys_base; |
| 121 | void __iomem *io_base; |
| 122 | |
Cyrille Pitchen | ccbf729 | 2015-12-17 17:48:39 +0100 | [diff] [blame] | 123 | struct crypto_async_request *areq; |
| 124 | struct atmel_aes_base_ctx *ctx; |
| 125 | |
Cyrille Pitchen | 10f12c1 | 2015-12-17 17:48:42 +0100 | [diff] [blame] | 126 | bool is_async; |
| 127 | atmel_aes_fn_t resume; |
Cyrille Pitchen | bbe628e | 2015-12-17 18:13:00 +0100 | [diff] [blame] | 128 | atmel_aes_fn_t cpu_transfer_complete; |
Cyrille Pitchen | 10f12c1 | 2015-12-17 17:48:42 +0100 | [diff] [blame] | 129 | |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 130 | struct device *dev; |
| 131 | struct clk *iclk; |
Cyrille Pitchen | afbac17 | 2015-12-17 18:13:02 +0100 | [diff] [blame] | 132 | int irq; |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 133 | |
| 134 | unsigned long flags; |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 135 | |
| 136 | spinlock_t lock; |
| 137 | struct crypto_queue queue; |
| 138 | |
| 139 | struct tasklet_struct done_task; |
| 140 | struct tasklet_struct queue_task; |
| 141 | |
Cyrille Pitchen | bbe628e | 2015-12-17 18:13:00 +0100 | [diff] [blame] | 142 | size_t total; |
| 143 | size_t datalen; |
| 144 | u32 *data; |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 145 | |
Cyrille Pitchen | bbe628e | 2015-12-17 18:13:00 +0100 | [diff] [blame] | 146 | struct atmel_aes_dma src; |
| 147 | struct atmel_aes_dma dst; |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 148 | |
Cyrille Pitchen | bbe628e | 2015-12-17 18:13:00 +0100 | [diff] [blame] | 149 | size_t buflen; |
| 150 | void *buf; |
| 151 | struct scatterlist aligned_sg; |
| 152 | struct scatterlist *real_dst; |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 153 | |
Nicolas Royer | cadc4ab | 2013-02-20 17:10:24 +0100 | [diff] [blame] | 154 | struct atmel_aes_caps caps; |
| 155 | |
Cyrille Pitchen | afbac17 | 2015-12-17 18:13:02 +0100 | [diff] [blame] | 156 | u32 hw_version; |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 157 | }; |
| 158 | |
| 159 | struct atmel_aes_drv { |
| 160 | struct list_head dev_list; |
| 161 | spinlock_t lock; |
| 162 | }; |
| 163 | |
| 164 | static struct atmel_aes_drv atmel_aes = { |
| 165 | .dev_list = LIST_HEAD_INIT(atmel_aes.dev_list), |
| 166 | .lock = __SPIN_LOCK_UNLOCKED(atmel_aes.lock), |
| 167 | }; |
| 168 | |
Cyrille Pitchen | e37a7e5 | 2015-12-17 18:13:03 +0100 | [diff] [blame^] | 169 | /* Shared functions */ |
Nicolas Royer | cadc4ab | 2013-02-20 17:10:24 +0100 | [diff] [blame] | 170 | |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 171 | static inline u32 atmel_aes_read(struct atmel_aes_dev *dd, u32 offset) |
| 172 | { |
| 173 | return readl_relaxed(dd->io_base + offset); |
| 174 | } |
| 175 | |
| 176 | static inline void atmel_aes_write(struct atmel_aes_dev *dd, |
| 177 | u32 offset, u32 value) |
| 178 | { |
| 179 | writel_relaxed(value, dd->io_base + offset); |
| 180 | } |
| 181 | |
| 182 | static void atmel_aes_read_n(struct atmel_aes_dev *dd, u32 offset, |
| 183 | u32 *value, int count) |
| 184 | { |
| 185 | for (; count--; value++, offset += 4) |
| 186 | *value = atmel_aes_read(dd, offset); |
| 187 | } |
| 188 | |
| 189 | static void atmel_aes_write_n(struct atmel_aes_dev *dd, u32 offset, |
Cyrille Pitchen | c0b28d8 | 2015-12-17 17:48:33 +0100 | [diff] [blame] | 190 | const u32 *value, int count) |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 191 | { |
| 192 | for (; count--; value++, offset += 4) |
| 193 | atmel_aes_write(dd, offset, *value); |
| 194 | } |
| 195 | |
Cyrille Pitchen | bbe628e | 2015-12-17 18:13:00 +0100 | [diff] [blame] | 196 | static inline void atmel_aes_read_block(struct atmel_aes_dev *dd, u32 offset, |
| 197 | u32 *value) |
| 198 | { |
| 199 | atmel_aes_read_n(dd, offset, value, SIZE_IN_WORDS(AES_BLOCK_SIZE)); |
| 200 | } |
| 201 | |
| 202 | static inline void atmel_aes_write_block(struct atmel_aes_dev *dd, u32 offset, |
| 203 | const u32 *value) |
| 204 | { |
| 205 | atmel_aes_write_n(dd, offset, value, SIZE_IN_WORDS(AES_BLOCK_SIZE)); |
| 206 | } |
| 207 | |
| 208 | static inline int atmel_aes_wait_for_data_ready(struct atmel_aes_dev *dd, |
| 209 | atmel_aes_fn_t resume) |
| 210 | { |
| 211 | u32 isr = atmel_aes_read(dd, AES_ISR); |
| 212 | |
| 213 | if (unlikely(isr & AES_INT_DATARDY)) |
| 214 | return resume(dd); |
| 215 | |
| 216 | dd->resume = resume; |
| 217 | atmel_aes_write(dd, AES_IER, AES_INT_DATARDY); |
| 218 | return -EINPROGRESS; |
| 219 | } |
| 220 | |
| 221 | static inline size_t atmel_aes_padlen(size_t len, size_t block_size) |
| 222 | { |
| 223 | len &= block_size - 1; |
| 224 | return len ? block_size - len : 0; |
| 225 | } |
| 226 | |
Cyrille Pitchen | ccbf729 | 2015-12-17 17:48:39 +0100 | [diff] [blame] | 227 | static struct atmel_aes_dev *atmel_aes_find_dev(struct atmel_aes_base_ctx *ctx) |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 228 | { |
| 229 | struct atmel_aes_dev *aes_dd = NULL; |
| 230 | struct atmel_aes_dev *tmp; |
| 231 | |
| 232 | spin_lock_bh(&atmel_aes.lock); |
| 233 | if (!ctx->dd) { |
| 234 | list_for_each_entry(tmp, &atmel_aes.dev_list, list) { |
| 235 | aes_dd = tmp; |
| 236 | break; |
| 237 | } |
| 238 | ctx->dd = aes_dd; |
| 239 | } else { |
| 240 | aes_dd = ctx->dd; |
| 241 | } |
| 242 | |
| 243 | spin_unlock_bh(&atmel_aes.lock); |
| 244 | |
| 245 | return aes_dd; |
| 246 | } |
| 247 | |
| 248 | static int atmel_aes_hw_init(struct atmel_aes_dev *dd) |
| 249 | { |
LABBE Corentin | 9d83d29 | 2015-10-02 14:12:58 +0200 | [diff] [blame] | 250 | int err; |
| 251 | |
| 252 | err = clk_prepare_enable(dd->iclk); |
| 253 | if (err) |
| 254 | return err; |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 255 | |
| 256 | if (!(dd->flags & AES_FLAGS_INIT)) { |
| 257 | atmel_aes_write(dd, AES_CR, AES_CR_SWRST); |
Nicolas Royer | cadc4ab | 2013-02-20 17:10:24 +0100 | [diff] [blame] | 258 | atmel_aes_write(dd, AES_MR, 0xE << AES_MR_CKEY_OFFSET); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 259 | dd->flags |= AES_FLAGS_INIT; |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | return 0; |
| 263 | } |
| 264 | |
Nicolas Royer | cadc4ab | 2013-02-20 17:10:24 +0100 | [diff] [blame] | 265 | static inline unsigned int atmel_aes_get_version(struct atmel_aes_dev *dd) |
| 266 | { |
| 267 | return atmel_aes_read(dd, AES_HW_VERSION) & 0x00000fff; |
| 268 | } |
| 269 | |
Cyrille Pitchen | aab0a39 | 2015-12-17 17:48:37 +0100 | [diff] [blame] | 270 | static int atmel_aes_hw_version_init(struct atmel_aes_dev *dd) |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 271 | { |
Cyrille Pitchen | aab0a39 | 2015-12-17 17:48:37 +0100 | [diff] [blame] | 272 | int err; |
| 273 | |
| 274 | err = atmel_aes_hw_init(dd); |
| 275 | if (err) |
| 276 | return err; |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 277 | |
Nicolas Royer | cadc4ab | 2013-02-20 17:10:24 +0100 | [diff] [blame] | 278 | dd->hw_version = atmel_aes_get_version(dd); |
| 279 | |
Cyrille Pitchen | aab0a39 | 2015-12-17 17:48:37 +0100 | [diff] [blame] | 280 | dev_info(dd->dev, "version: 0x%x\n", dd->hw_version); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 281 | |
| 282 | clk_disable_unprepare(dd->iclk); |
Cyrille Pitchen | aab0a39 | 2015-12-17 17:48:37 +0100 | [diff] [blame] | 283 | return 0; |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 284 | } |
| 285 | |
Cyrille Pitchen | 77dacf5 | 2015-12-17 17:48:41 +0100 | [diff] [blame] | 286 | static inline void atmel_aes_set_mode(struct atmel_aes_dev *dd, |
| 287 | const struct atmel_aes_reqctx *rctx) |
| 288 | { |
| 289 | /* Clear all but persistent flags and set request flags. */ |
| 290 | dd->flags = (dd->flags & AES_FLAGS_PERSISTENT) | rctx->mode; |
| 291 | } |
| 292 | |
Cyrille Pitchen | 10f12c1 | 2015-12-17 17:48:42 +0100 | [diff] [blame] | 293 | static inline int atmel_aes_complete(struct atmel_aes_dev *dd, int err) |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 294 | { |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 295 | clk_disable_unprepare(dd->iclk); |
| 296 | dd->flags &= ~AES_FLAGS_BUSY; |
| 297 | |
Cyrille Pitchen | 10f12c1 | 2015-12-17 17:48:42 +0100 | [diff] [blame] | 298 | if (dd->is_async) |
| 299 | dd->areq->complete(dd->areq, err); |
| 300 | |
| 301 | tasklet_schedule(&dd->queue_task); |
| 302 | |
| 303 | return err; |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 304 | } |
| 305 | |
Cyrille Pitchen | e37a7e5 | 2015-12-17 18:13:03 +0100 | [diff] [blame^] | 306 | static void atmel_aes_write_ctrl(struct atmel_aes_dev *dd, bool use_dma, |
| 307 | const u32 *iv) |
| 308 | { |
| 309 | u32 valmr = 0; |
| 310 | |
| 311 | /* MR register must be set before IV registers */ |
| 312 | if (dd->ctx->keylen == AES_KEYSIZE_128) |
| 313 | valmr |= AES_MR_KEYSIZE_128; |
| 314 | else if (dd->ctx->keylen == AES_KEYSIZE_192) |
| 315 | valmr |= AES_MR_KEYSIZE_192; |
| 316 | else |
| 317 | valmr |= AES_MR_KEYSIZE_256; |
| 318 | |
| 319 | valmr |= dd->flags & AES_FLAGS_MODE_MASK; |
| 320 | |
| 321 | if (use_dma) { |
| 322 | valmr |= AES_MR_SMOD_IDATAR0; |
| 323 | if (dd->caps.has_dualbuff) |
| 324 | valmr |= AES_MR_DUALBUFF; |
| 325 | } else { |
| 326 | valmr |= AES_MR_SMOD_AUTO; |
| 327 | } |
| 328 | |
| 329 | atmel_aes_write(dd, AES_MR, valmr); |
| 330 | |
| 331 | atmel_aes_write_n(dd, AES_KEYWR(0), dd->ctx->key, |
| 332 | SIZE_IN_WORDS(dd->ctx->keylen)); |
| 333 | |
| 334 | if (iv && (valmr & AES_MR_OPMOD_MASK) != AES_MR_OPMOD_ECB) |
| 335 | atmel_aes_write_block(dd, AES_IVR(0), iv); |
| 336 | } |
| 337 | |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 338 | |
Cyrille Pitchen | bbe628e | 2015-12-17 18:13:00 +0100 | [diff] [blame] | 339 | /* CPU transfer */ |
| 340 | |
| 341 | static int atmel_aes_cpu_transfer(struct atmel_aes_dev *dd) |
| 342 | { |
| 343 | int err = 0; |
| 344 | u32 isr; |
| 345 | |
| 346 | for (;;) { |
| 347 | atmel_aes_read_block(dd, AES_ODATAR(0), dd->data); |
| 348 | dd->data += 4; |
| 349 | dd->datalen -= AES_BLOCK_SIZE; |
| 350 | |
| 351 | if (dd->datalen < AES_BLOCK_SIZE) |
| 352 | break; |
| 353 | |
| 354 | atmel_aes_write_block(dd, AES_IDATAR(0), dd->data); |
| 355 | |
| 356 | isr = atmel_aes_read(dd, AES_ISR); |
| 357 | if (!(isr & AES_INT_DATARDY)) { |
| 358 | dd->resume = atmel_aes_cpu_transfer; |
| 359 | atmel_aes_write(dd, AES_IER, AES_INT_DATARDY); |
| 360 | return -EINPROGRESS; |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | if (!sg_copy_from_buffer(dd->real_dst, sg_nents(dd->real_dst), |
| 365 | dd->buf, dd->total)) |
| 366 | err = -EINVAL; |
| 367 | |
| 368 | if (err) |
| 369 | return atmel_aes_complete(dd, err); |
| 370 | |
| 371 | return dd->cpu_transfer_complete(dd); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 372 | } |
| 373 | |
Cyrille Pitchen | bbe628e | 2015-12-17 18:13:00 +0100 | [diff] [blame] | 374 | static int atmel_aes_cpu_start(struct atmel_aes_dev *dd, |
| 375 | struct scatterlist *src, |
| 376 | struct scatterlist *dst, |
| 377 | size_t len, |
| 378 | atmel_aes_fn_t resume) |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 379 | { |
Cyrille Pitchen | bbe628e | 2015-12-17 18:13:00 +0100 | [diff] [blame] | 380 | size_t padlen = atmel_aes_padlen(len, AES_BLOCK_SIZE); |
| 381 | |
| 382 | if (unlikely(len == 0)) |
| 383 | return -EINVAL; |
| 384 | |
| 385 | sg_copy_to_buffer(src, sg_nents(src), dd->buf, len); |
| 386 | |
| 387 | dd->total = len; |
| 388 | dd->real_dst = dst; |
| 389 | dd->cpu_transfer_complete = resume; |
| 390 | dd->datalen = len + padlen; |
| 391 | dd->data = (u32 *)dd->buf; |
| 392 | atmel_aes_write_block(dd, AES_IDATAR(0), dd->data); |
| 393 | return atmel_aes_wait_for_data_ready(dd, atmel_aes_cpu_transfer); |
| 394 | } |
| 395 | |
| 396 | |
| 397 | /* DMA transfer */ |
| 398 | |
| 399 | static void atmel_aes_dma_callback(void *data); |
| 400 | |
| 401 | static bool atmel_aes_check_aligned(struct atmel_aes_dev *dd, |
| 402 | struct scatterlist *sg, |
| 403 | size_t len, |
| 404 | struct atmel_aes_dma *dma) |
| 405 | { |
| 406 | int nents; |
| 407 | |
| 408 | if (!IS_ALIGNED(len, dd->ctx->block_size)) |
| 409 | return false; |
| 410 | |
| 411 | for (nents = 0; sg; sg = sg_next(sg), ++nents) { |
| 412 | if (!IS_ALIGNED(sg->offset, sizeof(u32))) |
| 413 | return false; |
| 414 | |
| 415 | if (len <= sg->length) { |
| 416 | if (!IS_ALIGNED(len, dd->ctx->block_size)) |
| 417 | return false; |
| 418 | |
| 419 | dma->nents = nents+1; |
| 420 | dma->remainder = sg->length - len; |
| 421 | sg->length = len; |
| 422 | return true; |
| 423 | } |
| 424 | |
| 425 | if (!IS_ALIGNED(sg->length, dd->ctx->block_size)) |
| 426 | return false; |
| 427 | |
| 428 | len -= sg->length; |
| 429 | } |
| 430 | |
| 431 | return false; |
| 432 | } |
| 433 | |
| 434 | static inline void atmel_aes_restore_sg(const struct atmel_aes_dma *dma) |
| 435 | { |
| 436 | struct scatterlist *sg = dma->sg; |
| 437 | int nents = dma->nents; |
| 438 | |
| 439 | if (!dma->remainder) |
| 440 | return; |
| 441 | |
| 442 | while (--nents > 0 && sg) |
| 443 | sg = sg_next(sg); |
| 444 | |
| 445 | if (!sg) |
| 446 | return; |
| 447 | |
| 448 | sg->length += dma->remainder; |
| 449 | } |
| 450 | |
| 451 | static int atmel_aes_map(struct atmel_aes_dev *dd, |
| 452 | struct scatterlist *src, |
| 453 | struct scatterlist *dst, |
| 454 | size_t len) |
| 455 | { |
| 456 | bool src_aligned, dst_aligned; |
| 457 | size_t padlen; |
| 458 | |
| 459 | dd->total = len; |
| 460 | dd->src.sg = src; |
| 461 | dd->dst.sg = dst; |
| 462 | dd->real_dst = dst; |
| 463 | |
| 464 | src_aligned = atmel_aes_check_aligned(dd, src, len, &dd->src); |
| 465 | if (src == dst) |
| 466 | dst_aligned = src_aligned; |
| 467 | else |
| 468 | dst_aligned = atmel_aes_check_aligned(dd, dst, len, &dd->dst); |
| 469 | if (!src_aligned || !dst_aligned) { |
| 470 | padlen = atmel_aes_padlen(len, dd->ctx->block_size); |
| 471 | |
| 472 | if (dd->buflen < len + padlen) |
| 473 | return -ENOMEM; |
| 474 | |
| 475 | if (!src_aligned) { |
| 476 | sg_copy_to_buffer(src, sg_nents(src), dd->buf, len); |
| 477 | dd->src.sg = &dd->aligned_sg; |
| 478 | dd->src.nents = 1; |
| 479 | dd->src.remainder = 0; |
| 480 | } |
| 481 | |
| 482 | if (!dst_aligned) { |
| 483 | dd->dst.sg = &dd->aligned_sg; |
| 484 | dd->dst.nents = 1; |
| 485 | dd->dst.remainder = 0; |
| 486 | } |
| 487 | |
| 488 | sg_init_table(&dd->aligned_sg, 1); |
| 489 | sg_set_buf(&dd->aligned_sg, dd->buf, len + padlen); |
| 490 | } |
| 491 | |
| 492 | if (dd->src.sg == dd->dst.sg) { |
| 493 | dd->src.sg_len = dma_map_sg(dd->dev, dd->src.sg, dd->src.nents, |
| 494 | DMA_BIDIRECTIONAL); |
| 495 | dd->dst.sg_len = dd->src.sg_len; |
| 496 | if (!dd->src.sg_len) |
| 497 | return -EFAULT; |
| 498 | } else { |
| 499 | dd->src.sg_len = dma_map_sg(dd->dev, dd->src.sg, dd->src.nents, |
| 500 | DMA_TO_DEVICE); |
| 501 | if (!dd->src.sg_len) |
| 502 | return -EFAULT; |
| 503 | |
| 504 | dd->dst.sg_len = dma_map_sg(dd->dev, dd->dst.sg, dd->dst.nents, |
| 505 | DMA_FROM_DEVICE); |
| 506 | if (!dd->dst.sg_len) { |
| 507 | dma_unmap_sg(dd->dev, dd->src.sg, dd->src.nents, |
| 508 | DMA_TO_DEVICE); |
| 509 | return -EFAULT; |
| 510 | } |
| 511 | } |
| 512 | |
| 513 | return 0; |
| 514 | } |
| 515 | |
| 516 | static void atmel_aes_unmap(struct atmel_aes_dev *dd) |
| 517 | { |
| 518 | if (dd->src.sg == dd->dst.sg) { |
| 519 | dma_unmap_sg(dd->dev, dd->src.sg, dd->src.nents, |
| 520 | DMA_BIDIRECTIONAL); |
| 521 | |
| 522 | if (dd->src.sg != &dd->aligned_sg) |
| 523 | atmel_aes_restore_sg(&dd->src); |
| 524 | } else { |
| 525 | dma_unmap_sg(dd->dev, dd->dst.sg, dd->dst.nents, |
| 526 | DMA_FROM_DEVICE); |
| 527 | |
| 528 | if (dd->dst.sg != &dd->aligned_sg) |
| 529 | atmel_aes_restore_sg(&dd->dst); |
| 530 | |
| 531 | dma_unmap_sg(dd->dev, dd->src.sg, dd->src.nents, |
| 532 | DMA_TO_DEVICE); |
| 533 | |
| 534 | if (dd->src.sg != &dd->aligned_sg) |
| 535 | atmel_aes_restore_sg(&dd->src); |
| 536 | } |
| 537 | |
| 538 | if (dd->dst.sg == &dd->aligned_sg) |
| 539 | sg_copy_from_buffer(dd->real_dst, sg_nents(dd->real_dst), |
| 540 | dd->buf, dd->total); |
| 541 | } |
| 542 | |
| 543 | static int atmel_aes_dma_transfer_start(struct atmel_aes_dev *dd, |
| 544 | enum dma_slave_buswidth addr_width, |
| 545 | enum dma_transfer_direction dir, |
| 546 | u32 maxburst) |
| 547 | { |
| 548 | struct dma_async_tx_descriptor *desc; |
| 549 | struct dma_slave_config config; |
| 550 | dma_async_tx_callback callback; |
| 551 | struct atmel_aes_dma *dma; |
| 552 | int err; |
| 553 | |
| 554 | memset(&config, 0, sizeof(config)); |
| 555 | config.direction = dir; |
| 556 | config.src_addr_width = addr_width; |
| 557 | config.dst_addr_width = addr_width; |
| 558 | config.src_maxburst = maxburst; |
| 559 | config.dst_maxburst = maxburst; |
| 560 | |
| 561 | switch (dir) { |
| 562 | case DMA_MEM_TO_DEV: |
| 563 | dma = &dd->src; |
| 564 | callback = NULL; |
| 565 | config.dst_addr = dd->phys_base + AES_IDATAR(0); |
| 566 | break; |
| 567 | |
| 568 | case DMA_DEV_TO_MEM: |
| 569 | dma = &dd->dst; |
| 570 | callback = atmel_aes_dma_callback; |
| 571 | config.src_addr = dd->phys_base + AES_ODATAR(0); |
| 572 | break; |
| 573 | |
| 574 | default: |
| 575 | return -EINVAL; |
| 576 | } |
| 577 | |
| 578 | err = dmaengine_slave_config(dma->chan, &config); |
| 579 | if (err) |
| 580 | return err; |
| 581 | |
| 582 | desc = dmaengine_prep_slave_sg(dma->chan, dma->sg, dma->sg_len, dir, |
| 583 | DMA_PREP_INTERRUPT | DMA_CTRL_ACK); |
| 584 | if (!desc) |
| 585 | return -ENOMEM; |
| 586 | |
| 587 | desc->callback = callback; |
| 588 | desc->callback_param = dd; |
| 589 | dmaengine_submit(desc); |
| 590 | dma_async_issue_pending(dma->chan); |
| 591 | |
| 592 | return 0; |
| 593 | } |
| 594 | |
| 595 | static void atmel_aes_dma_transfer_stop(struct atmel_aes_dev *dd, |
| 596 | enum dma_transfer_direction dir) |
| 597 | { |
| 598 | struct atmel_aes_dma *dma; |
| 599 | |
| 600 | switch (dir) { |
| 601 | case DMA_MEM_TO_DEV: |
| 602 | dma = &dd->src; |
| 603 | break; |
| 604 | |
| 605 | case DMA_DEV_TO_MEM: |
| 606 | dma = &dd->dst; |
| 607 | break; |
| 608 | |
| 609 | default: |
| 610 | return; |
| 611 | } |
| 612 | |
| 613 | dmaengine_terminate_all(dma->chan); |
| 614 | } |
| 615 | |
| 616 | static int atmel_aes_dma_start(struct atmel_aes_dev *dd, |
| 617 | struct scatterlist *src, |
| 618 | struct scatterlist *dst, |
| 619 | size_t len, |
| 620 | atmel_aes_fn_t resume) |
| 621 | { |
Cyrille Pitchen | 77dacf5 | 2015-12-17 17:48:41 +0100 | [diff] [blame] | 622 | enum dma_slave_buswidth addr_width; |
| 623 | u32 maxburst; |
Cyrille Pitchen | bbe628e | 2015-12-17 18:13:00 +0100 | [diff] [blame] | 624 | int err; |
Cyrille Pitchen | 77dacf5 | 2015-12-17 17:48:41 +0100 | [diff] [blame] | 625 | |
| 626 | switch (dd->ctx->block_size) { |
| 627 | case CFB8_BLOCK_SIZE: |
| 628 | addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE; |
| 629 | maxburst = 1; |
| 630 | break; |
| 631 | |
| 632 | case CFB16_BLOCK_SIZE: |
| 633 | addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES; |
| 634 | maxburst = 1; |
| 635 | break; |
| 636 | |
| 637 | case CFB32_BLOCK_SIZE: |
| 638 | case CFB64_BLOCK_SIZE: |
| 639 | addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES; |
| 640 | maxburst = 1; |
| 641 | break; |
| 642 | |
| 643 | case AES_BLOCK_SIZE: |
| 644 | addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES; |
| 645 | maxburst = dd->caps.max_burst_size; |
| 646 | break; |
| 647 | |
| 648 | default: |
Cyrille Pitchen | bbe628e | 2015-12-17 18:13:00 +0100 | [diff] [blame] | 649 | err = -EINVAL; |
| 650 | goto exit; |
Cyrille Pitchen | 77dacf5 | 2015-12-17 17:48:41 +0100 | [diff] [blame] | 651 | } |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 652 | |
Cyrille Pitchen | bbe628e | 2015-12-17 18:13:00 +0100 | [diff] [blame] | 653 | err = atmel_aes_map(dd, src, dst, len); |
| 654 | if (err) |
| 655 | goto exit; |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 656 | |
Cyrille Pitchen | bbe628e | 2015-12-17 18:13:00 +0100 | [diff] [blame] | 657 | dd->resume = resume; |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 658 | |
Cyrille Pitchen | bbe628e | 2015-12-17 18:13:00 +0100 | [diff] [blame] | 659 | /* Set output DMA transfer first */ |
| 660 | err = atmel_aes_dma_transfer_start(dd, addr_width, DMA_DEV_TO_MEM, |
| 661 | maxburst); |
| 662 | if (err) |
| 663 | goto unmap; |
Nicolas Royer | cadc4ab | 2013-02-20 17:10:24 +0100 | [diff] [blame] | 664 | |
Cyrille Pitchen | bbe628e | 2015-12-17 18:13:00 +0100 | [diff] [blame] | 665 | /* Then set input DMA transfer */ |
| 666 | err = atmel_aes_dma_transfer_start(dd, addr_width, DMA_MEM_TO_DEV, |
| 667 | maxburst); |
| 668 | if (err) |
| 669 | goto output_transfer_stop; |
Nicolas Royer | cadc4ab | 2013-02-20 17:10:24 +0100 | [diff] [blame] | 670 | |
Cyrille Pitchen | 10f12c1 | 2015-12-17 17:48:42 +0100 | [diff] [blame] | 671 | return -EINPROGRESS; |
Cyrille Pitchen | bbe628e | 2015-12-17 18:13:00 +0100 | [diff] [blame] | 672 | |
| 673 | output_transfer_stop: |
| 674 | atmel_aes_dma_transfer_stop(dd, DMA_DEV_TO_MEM); |
| 675 | unmap: |
| 676 | atmel_aes_unmap(dd); |
| 677 | exit: |
| 678 | return atmel_aes_complete(dd, err); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 679 | } |
| 680 | |
Cyrille Pitchen | bbe628e | 2015-12-17 18:13:00 +0100 | [diff] [blame] | 681 | static void atmel_aes_dma_stop(struct atmel_aes_dev *dd) |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 682 | { |
Cyrille Pitchen | bbe628e | 2015-12-17 18:13:00 +0100 | [diff] [blame] | 683 | atmel_aes_dma_transfer_stop(dd, DMA_MEM_TO_DEV); |
| 684 | atmel_aes_dma_transfer_stop(dd, DMA_DEV_TO_MEM); |
| 685 | atmel_aes_unmap(dd); |
| 686 | } |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 687 | |
Cyrille Pitchen | bbe628e | 2015-12-17 18:13:00 +0100 | [diff] [blame] | 688 | static void atmel_aes_dma_callback(void *data) |
| 689 | { |
| 690 | struct atmel_aes_dev *dd = data; |
Nicolas Royer | cadc4ab | 2013-02-20 17:10:24 +0100 | [diff] [blame] | 691 | |
Cyrille Pitchen | bbe628e | 2015-12-17 18:13:00 +0100 | [diff] [blame] | 692 | atmel_aes_dma_stop(dd); |
| 693 | dd->is_async = true; |
| 694 | (void)dd->resume(dd); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 695 | } |
| 696 | |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 697 | static int atmel_aes_handle_queue(struct atmel_aes_dev *dd, |
Cyrille Pitchen | ccbf729 | 2015-12-17 17:48:39 +0100 | [diff] [blame] | 698 | struct crypto_async_request *new_areq) |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 699 | { |
Cyrille Pitchen | ccbf729 | 2015-12-17 17:48:39 +0100 | [diff] [blame] | 700 | struct crypto_async_request *areq, *backlog; |
| 701 | struct atmel_aes_base_ctx *ctx; |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 702 | unsigned long flags; |
| 703 | int err, ret = 0; |
| 704 | |
| 705 | spin_lock_irqsave(&dd->lock, flags); |
Cyrille Pitchen | ccbf729 | 2015-12-17 17:48:39 +0100 | [diff] [blame] | 706 | if (new_areq) |
| 707 | ret = crypto_enqueue_request(&dd->queue, new_areq); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 708 | if (dd->flags & AES_FLAGS_BUSY) { |
| 709 | spin_unlock_irqrestore(&dd->lock, flags); |
| 710 | return ret; |
| 711 | } |
| 712 | backlog = crypto_get_backlog(&dd->queue); |
Cyrille Pitchen | ccbf729 | 2015-12-17 17:48:39 +0100 | [diff] [blame] | 713 | areq = crypto_dequeue_request(&dd->queue); |
| 714 | if (areq) |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 715 | dd->flags |= AES_FLAGS_BUSY; |
| 716 | spin_unlock_irqrestore(&dd->lock, flags); |
| 717 | |
Cyrille Pitchen | ccbf729 | 2015-12-17 17:48:39 +0100 | [diff] [blame] | 718 | if (!areq) |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 719 | return ret; |
| 720 | |
| 721 | if (backlog) |
| 722 | backlog->complete(backlog, -EINPROGRESS); |
| 723 | |
Cyrille Pitchen | ccbf729 | 2015-12-17 17:48:39 +0100 | [diff] [blame] | 724 | ctx = crypto_tfm_ctx(areq->tfm); |
| 725 | |
| 726 | dd->areq = areq; |
| 727 | dd->ctx = ctx; |
Cyrille Pitchen | 10f12c1 | 2015-12-17 17:48:42 +0100 | [diff] [blame] | 728 | dd->is_async = (areq != new_areq); |
Cyrille Pitchen | ccbf729 | 2015-12-17 17:48:39 +0100 | [diff] [blame] | 729 | |
| 730 | err = ctx->start(dd); |
Cyrille Pitchen | 10f12c1 | 2015-12-17 17:48:42 +0100 | [diff] [blame] | 731 | return (dd->is_async) ? ret : err; |
Cyrille Pitchen | ccbf729 | 2015-12-17 17:48:39 +0100 | [diff] [blame] | 732 | } |
| 733 | |
Cyrille Pitchen | e37a7e5 | 2015-12-17 18:13:03 +0100 | [diff] [blame^] | 734 | |
| 735 | /* AES async block ciphers */ |
| 736 | |
Cyrille Pitchen | bbe628e | 2015-12-17 18:13:00 +0100 | [diff] [blame] | 737 | static int atmel_aes_transfer_complete(struct atmel_aes_dev *dd) |
| 738 | { |
| 739 | return atmel_aes_complete(dd, 0); |
| 740 | } |
| 741 | |
Cyrille Pitchen | ccbf729 | 2015-12-17 17:48:39 +0100 | [diff] [blame] | 742 | static int atmel_aes_start(struct atmel_aes_dev *dd) |
| 743 | { |
| 744 | struct ablkcipher_request *req = ablkcipher_request_cast(dd->areq); |
Cyrille Pitchen | bbe628e | 2015-12-17 18:13:00 +0100 | [diff] [blame] | 745 | struct atmel_aes_reqctx *rctx = ablkcipher_request_ctx(req); |
| 746 | bool use_dma = (req->nbytes >= ATMEL_AES_DMA_THRESHOLD || |
| 747 | dd->ctx->block_size != AES_BLOCK_SIZE); |
Cyrille Pitchen | ccbf729 | 2015-12-17 17:48:39 +0100 | [diff] [blame] | 748 | int err; |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 749 | |
Cyrille Pitchen | 77dacf5 | 2015-12-17 17:48:41 +0100 | [diff] [blame] | 750 | atmel_aes_set_mode(dd, rctx); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 751 | |
Cyrille Pitchen | cdfab4a | 2015-12-17 17:48:38 +0100 | [diff] [blame] | 752 | err = atmel_aes_hw_init(dd); |
Cyrille Pitchen | bbe628e | 2015-12-17 18:13:00 +0100 | [diff] [blame] | 753 | if (err) |
Cyrille Pitchen | 10f12c1 | 2015-12-17 17:48:42 +0100 | [diff] [blame] | 754 | return atmel_aes_complete(dd, err); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 755 | |
Cyrille Pitchen | bbe628e | 2015-12-17 18:13:00 +0100 | [diff] [blame] | 756 | atmel_aes_write_ctrl(dd, use_dma, req->info); |
| 757 | if (use_dma) |
| 758 | return atmel_aes_dma_start(dd, req->src, req->dst, req->nbytes, |
| 759 | atmel_aes_transfer_complete); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 760 | |
Cyrille Pitchen | bbe628e | 2015-12-17 18:13:00 +0100 | [diff] [blame] | 761 | return atmel_aes_cpu_start(dd, req->src, req->dst, req->nbytes, |
| 762 | atmel_aes_transfer_complete); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 763 | } |
| 764 | |
| 765 | static int atmel_aes_crypt(struct ablkcipher_request *req, unsigned long mode) |
| 766 | { |
Cyrille Pitchen | afbac17 | 2015-12-17 18:13:02 +0100 | [diff] [blame] | 767 | struct atmel_aes_base_ctx *ctx; |
| 768 | struct atmel_aes_reqctx *rctx; |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 769 | struct atmel_aes_dev *dd; |
| 770 | |
Cyrille Pitchen | afbac17 | 2015-12-17 18:13:02 +0100 | [diff] [blame] | 771 | ctx = crypto_ablkcipher_ctx(crypto_ablkcipher_reqtfm(req)); |
Cyrille Pitchen | 77dacf5 | 2015-12-17 17:48:41 +0100 | [diff] [blame] | 772 | switch (mode & AES_FLAGS_OPMODE_MASK) { |
| 773 | case AES_FLAGS_CFB8: |
Nicolas Royer | cadc4ab | 2013-02-20 17:10:24 +0100 | [diff] [blame] | 774 | ctx->block_size = CFB8_BLOCK_SIZE; |
Cyrille Pitchen | 77dacf5 | 2015-12-17 17:48:41 +0100 | [diff] [blame] | 775 | break; |
| 776 | |
| 777 | case AES_FLAGS_CFB16: |
Nicolas Royer | cadc4ab | 2013-02-20 17:10:24 +0100 | [diff] [blame] | 778 | ctx->block_size = CFB16_BLOCK_SIZE; |
Cyrille Pitchen | 77dacf5 | 2015-12-17 17:48:41 +0100 | [diff] [blame] | 779 | break; |
| 780 | |
| 781 | case AES_FLAGS_CFB32: |
Nicolas Royer | cadc4ab | 2013-02-20 17:10:24 +0100 | [diff] [blame] | 782 | ctx->block_size = CFB32_BLOCK_SIZE; |
Cyrille Pitchen | 77dacf5 | 2015-12-17 17:48:41 +0100 | [diff] [blame] | 783 | break; |
| 784 | |
| 785 | case AES_FLAGS_CFB64: |
Leilei Zhao | 9f84951 | 2014-04-22 15:23:24 +0800 | [diff] [blame] | 786 | ctx->block_size = CFB64_BLOCK_SIZE; |
Cyrille Pitchen | 77dacf5 | 2015-12-17 17:48:41 +0100 | [diff] [blame] | 787 | break; |
| 788 | |
| 789 | default: |
Nicolas Royer | cadc4ab | 2013-02-20 17:10:24 +0100 | [diff] [blame] | 790 | ctx->block_size = AES_BLOCK_SIZE; |
Cyrille Pitchen | 77dacf5 | 2015-12-17 17:48:41 +0100 | [diff] [blame] | 791 | break; |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 792 | } |
| 793 | |
| 794 | dd = atmel_aes_find_dev(ctx); |
| 795 | if (!dd) |
| 796 | return -ENODEV; |
| 797 | |
Cyrille Pitchen | afbac17 | 2015-12-17 18:13:02 +0100 | [diff] [blame] | 798 | rctx = ablkcipher_request_ctx(req); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 799 | rctx->mode = mode; |
| 800 | |
Cyrille Pitchen | ccbf729 | 2015-12-17 17:48:39 +0100 | [diff] [blame] | 801 | return atmel_aes_handle_queue(dd, &req->base); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 802 | } |
| 803 | |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 804 | static int atmel_aes_setkey(struct crypto_ablkcipher *tfm, const u8 *key, |
| 805 | unsigned int keylen) |
| 806 | { |
Cyrille Pitchen | ccbf729 | 2015-12-17 17:48:39 +0100 | [diff] [blame] | 807 | struct atmel_aes_base_ctx *ctx = crypto_ablkcipher_ctx(tfm); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 808 | |
Cyrille Pitchen | afbac17 | 2015-12-17 18:13:02 +0100 | [diff] [blame] | 809 | if (keylen != AES_KEYSIZE_128 && |
| 810 | keylen != AES_KEYSIZE_192 && |
| 811 | keylen != AES_KEYSIZE_256) { |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 812 | crypto_ablkcipher_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN); |
| 813 | return -EINVAL; |
| 814 | } |
| 815 | |
| 816 | memcpy(ctx->key, key, keylen); |
| 817 | ctx->keylen = keylen; |
| 818 | |
| 819 | return 0; |
| 820 | } |
| 821 | |
| 822 | static int atmel_aes_ecb_encrypt(struct ablkcipher_request *req) |
| 823 | { |
Cyrille Pitchen | 77dacf5 | 2015-12-17 17:48:41 +0100 | [diff] [blame] | 824 | return atmel_aes_crypt(req, AES_FLAGS_ECB | AES_FLAGS_ENCRYPT); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 825 | } |
| 826 | |
| 827 | static int atmel_aes_ecb_decrypt(struct ablkcipher_request *req) |
| 828 | { |
Cyrille Pitchen | 77dacf5 | 2015-12-17 17:48:41 +0100 | [diff] [blame] | 829 | return atmel_aes_crypt(req, AES_FLAGS_ECB); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 830 | } |
| 831 | |
| 832 | static int atmel_aes_cbc_encrypt(struct ablkcipher_request *req) |
| 833 | { |
Cyrille Pitchen | afbac17 | 2015-12-17 18:13:02 +0100 | [diff] [blame] | 834 | return atmel_aes_crypt(req, AES_FLAGS_CBC | AES_FLAGS_ENCRYPT); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 835 | } |
| 836 | |
| 837 | static int atmel_aes_cbc_decrypt(struct ablkcipher_request *req) |
| 838 | { |
Cyrille Pitchen | afbac17 | 2015-12-17 18:13:02 +0100 | [diff] [blame] | 839 | return atmel_aes_crypt(req, AES_FLAGS_CBC); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 840 | } |
| 841 | |
| 842 | static int atmel_aes_ofb_encrypt(struct ablkcipher_request *req) |
| 843 | { |
Cyrille Pitchen | afbac17 | 2015-12-17 18:13:02 +0100 | [diff] [blame] | 844 | return atmel_aes_crypt(req, AES_FLAGS_OFB | AES_FLAGS_ENCRYPT); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 845 | } |
| 846 | |
| 847 | static int atmel_aes_ofb_decrypt(struct ablkcipher_request *req) |
| 848 | { |
Cyrille Pitchen | afbac17 | 2015-12-17 18:13:02 +0100 | [diff] [blame] | 849 | return atmel_aes_crypt(req, AES_FLAGS_OFB); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 850 | } |
| 851 | |
| 852 | static int atmel_aes_cfb_encrypt(struct ablkcipher_request *req) |
| 853 | { |
Cyrille Pitchen | 77dacf5 | 2015-12-17 17:48:41 +0100 | [diff] [blame] | 854 | return atmel_aes_crypt(req, AES_FLAGS_CFB128 | AES_FLAGS_ENCRYPT); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 855 | } |
| 856 | |
| 857 | static int atmel_aes_cfb_decrypt(struct ablkcipher_request *req) |
| 858 | { |
Cyrille Pitchen | 77dacf5 | 2015-12-17 17:48:41 +0100 | [diff] [blame] | 859 | return atmel_aes_crypt(req, AES_FLAGS_CFB128); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 860 | } |
| 861 | |
| 862 | static int atmel_aes_cfb64_encrypt(struct ablkcipher_request *req) |
| 863 | { |
Cyrille Pitchen | 77dacf5 | 2015-12-17 17:48:41 +0100 | [diff] [blame] | 864 | return atmel_aes_crypt(req, AES_FLAGS_CFB64 | AES_FLAGS_ENCRYPT); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 865 | } |
| 866 | |
| 867 | static int atmel_aes_cfb64_decrypt(struct ablkcipher_request *req) |
| 868 | { |
Cyrille Pitchen | 77dacf5 | 2015-12-17 17:48:41 +0100 | [diff] [blame] | 869 | return atmel_aes_crypt(req, AES_FLAGS_CFB64); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 870 | } |
| 871 | |
| 872 | static int atmel_aes_cfb32_encrypt(struct ablkcipher_request *req) |
| 873 | { |
Cyrille Pitchen | 77dacf5 | 2015-12-17 17:48:41 +0100 | [diff] [blame] | 874 | return atmel_aes_crypt(req, AES_FLAGS_CFB32 | AES_FLAGS_ENCRYPT); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 875 | } |
| 876 | |
| 877 | static int atmel_aes_cfb32_decrypt(struct ablkcipher_request *req) |
| 878 | { |
Cyrille Pitchen | 77dacf5 | 2015-12-17 17:48:41 +0100 | [diff] [blame] | 879 | return atmel_aes_crypt(req, AES_FLAGS_CFB32); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 880 | } |
| 881 | |
| 882 | static int atmel_aes_cfb16_encrypt(struct ablkcipher_request *req) |
| 883 | { |
Cyrille Pitchen | 77dacf5 | 2015-12-17 17:48:41 +0100 | [diff] [blame] | 884 | return atmel_aes_crypt(req, AES_FLAGS_CFB16 | AES_FLAGS_ENCRYPT); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 885 | } |
| 886 | |
| 887 | static int atmel_aes_cfb16_decrypt(struct ablkcipher_request *req) |
| 888 | { |
Cyrille Pitchen | 77dacf5 | 2015-12-17 17:48:41 +0100 | [diff] [blame] | 889 | return atmel_aes_crypt(req, AES_FLAGS_CFB16); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 890 | } |
| 891 | |
| 892 | static int atmel_aes_cfb8_encrypt(struct ablkcipher_request *req) |
| 893 | { |
Cyrille Pitchen | 77dacf5 | 2015-12-17 17:48:41 +0100 | [diff] [blame] | 894 | return atmel_aes_crypt(req, AES_FLAGS_CFB8 | AES_FLAGS_ENCRYPT); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 895 | } |
| 896 | |
| 897 | static int atmel_aes_cfb8_decrypt(struct ablkcipher_request *req) |
| 898 | { |
Cyrille Pitchen | 77dacf5 | 2015-12-17 17:48:41 +0100 | [diff] [blame] | 899 | return atmel_aes_crypt(req, AES_FLAGS_CFB8); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 900 | } |
| 901 | |
| 902 | static int atmel_aes_ctr_encrypt(struct ablkcipher_request *req) |
| 903 | { |
Cyrille Pitchen | afbac17 | 2015-12-17 18:13:02 +0100 | [diff] [blame] | 904 | return atmel_aes_crypt(req, AES_FLAGS_CTR | AES_FLAGS_ENCRYPT); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 905 | } |
| 906 | |
| 907 | static int atmel_aes_ctr_decrypt(struct ablkcipher_request *req) |
| 908 | { |
Cyrille Pitchen | afbac17 | 2015-12-17 18:13:02 +0100 | [diff] [blame] | 909 | return atmel_aes_crypt(req, AES_FLAGS_CTR); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 910 | } |
| 911 | |
| 912 | static int atmel_aes_cra_init(struct crypto_tfm *tfm) |
| 913 | { |
Cyrille Pitchen | ccbf729 | 2015-12-17 17:48:39 +0100 | [diff] [blame] | 914 | struct atmel_aes_ctx *ctx = crypto_tfm_ctx(tfm); |
| 915 | |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 916 | tfm->crt_ablkcipher.reqsize = sizeof(struct atmel_aes_reqctx); |
Cyrille Pitchen | ccbf729 | 2015-12-17 17:48:39 +0100 | [diff] [blame] | 917 | ctx->base.start = atmel_aes_start; |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 918 | |
| 919 | return 0; |
| 920 | } |
| 921 | |
| 922 | static void atmel_aes_cra_exit(struct crypto_tfm *tfm) |
| 923 | { |
| 924 | } |
| 925 | |
| 926 | static struct crypto_alg aes_algs[] = { |
| 927 | { |
| 928 | .cra_name = "ecb(aes)", |
| 929 | .cra_driver_name = "atmel-ecb-aes", |
Cyrille Pitchen | 88efd9a | 2015-12-17 17:48:34 +0100 | [diff] [blame] | 930 | .cra_priority = ATMEL_AES_PRIORITY, |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 931 | .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC, |
| 932 | .cra_blocksize = AES_BLOCK_SIZE, |
| 933 | .cra_ctxsize = sizeof(struct atmel_aes_ctx), |
Nicolas Royer | cadc4ab | 2013-02-20 17:10:24 +0100 | [diff] [blame] | 934 | .cra_alignmask = 0xf, |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 935 | .cra_type = &crypto_ablkcipher_type, |
| 936 | .cra_module = THIS_MODULE, |
| 937 | .cra_init = atmel_aes_cra_init, |
| 938 | .cra_exit = atmel_aes_cra_exit, |
| 939 | .cra_u.ablkcipher = { |
| 940 | .min_keysize = AES_MIN_KEY_SIZE, |
| 941 | .max_keysize = AES_MAX_KEY_SIZE, |
| 942 | .setkey = atmel_aes_setkey, |
| 943 | .encrypt = atmel_aes_ecb_encrypt, |
| 944 | .decrypt = atmel_aes_ecb_decrypt, |
| 945 | } |
| 946 | }, |
| 947 | { |
| 948 | .cra_name = "cbc(aes)", |
| 949 | .cra_driver_name = "atmel-cbc-aes", |
Cyrille Pitchen | 88efd9a | 2015-12-17 17:48:34 +0100 | [diff] [blame] | 950 | .cra_priority = ATMEL_AES_PRIORITY, |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 951 | .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC, |
| 952 | .cra_blocksize = AES_BLOCK_SIZE, |
| 953 | .cra_ctxsize = sizeof(struct atmel_aes_ctx), |
Nicolas Royer | cadc4ab | 2013-02-20 17:10:24 +0100 | [diff] [blame] | 954 | .cra_alignmask = 0xf, |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 955 | .cra_type = &crypto_ablkcipher_type, |
| 956 | .cra_module = THIS_MODULE, |
| 957 | .cra_init = atmel_aes_cra_init, |
| 958 | .cra_exit = atmel_aes_cra_exit, |
| 959 | .cra_u.ablkcipher = { |
| 960 | .min_keysize = AES_MIN_KEY_SIZE, |
| 961 | .max_keysize = AES_MAX_KEY_SIZE, |
| 962 | .ivsize = AES_BLOCK_SIZE, |
| 963 | .setkey = atmel_aes_setkey, |
| 964 | .encrypt = atmel_aes_cbc_encrypt, |
| 965 | .decrypt = atmel_aes_cbc_decrypt, |
| 966 | } |
| 967 | }, |
| 968 | { |
| 969 | .cra_name = "ofb(aes)", |
| 970 | .cra_driver_name = "atmel-ofb-aes", |
Cyrille Pitchen | 88efd9a | 2015-12-17 17:48:34 +0100 | [diff] [blame] | 971 | .cra_priority = ATMEL_AES_PRIORITY, |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 972 | .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC, |
| 973 | .cra_blocksize = AES_BLOCK_SIZE, |
| 974 | .cra_ctxsize = sizeof(struct atmel_aes_ctx), |
Nicolas Royer | cadc4ab | 2013-02-20 17:10:24 +0100 | [diff] [blame] | 975 | .cra_alignmask = 0xf, |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 976 | .cra_type = &crypto_ablkcipher_type, |
| 977 | .cra_module = THIS_MODULE, |
| 978 | .cra_init = atmel_aes_cra_init, |
| 979 | .cra_exit = atmel_aes_cra_exit, |
| 980 | .cra_u.ablkcipher = { |
| 981 | .min_keysize = AES_MIN_KEY_SIZE, |
| 982 | .max_keysize = AES_MAX_KEY_SIZE, |
| 983 | .ivsize = AES_BLOCK_SIZE, |
| 984 | .setkey = atmel_aes_setkey, |
| 985 | .encrypt = atmel_aes_ofb_encrypt, |
| 986 | .decrypt = atmel_aes_ofb_decrypt, |
| 987 | } |
| 988 | }, |
| 989 | { |
| 990 | .cra_name = "cfb(aes)", |
| 991 | .cra_driver_name = "atmel-cfb-aes", |
Cyrille Pitchen | 88efd9a | 2015-12-17 17:48:34 +0100 | [diff] [blame] | 992 | .cra_priority = ATMEL_AES_PRIORITY, |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 993 | .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC, |
| 994 | .cra_blocksize = AES_BLOCK_SIZE, |
| 995 | .cra_ctxsize = sizeof(struct atmel_aes_ctx), |
Nicolas Royer | cadc4ab | 2013-02-20 17:10:24 +0100 | [diff] [blame] | 996 | .cra_alignmask = 0xf, |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 997 | .cra_type = &crypto_ablkcipher_type, |
| 998 | .cra_module = THIS_MODULE, |
| 999 | .cra_init = atmel_aes_cra_init, |
| 1000 | .cra_exit = atmel_aes_cra_exit, |
| 1001 | .cra_u.ablkcipher = { |
| 1002 | .min_keysize = AES_MIN_KEY_SIZE, |
| 1003 | .max_keysize = AES_MAX_KEY_SIZE, |
| 1004 | .ivsize = AES_BLOCK_SIZE, |
| 1005 | .setkey = atmel_aes_setkey, |
| 1006 | .encrypt = atmel_aes_cfb_encrypt, |
| 1007 | .decrypt = atmel_aes_cfb_decrypt, |
| 1008 | } |
| 1009 | }, |
| 1010 | { |
| 1011 | .cra_name = "cfb32(aes)", |
| 1012 | .cra_driver_name = "atmel-cfb32-aes", |
Cyrille Pitchen | 88efd9a | 2015-12-17 17:48:34 +0100 | [diff] [blame] | 1013 | .cra_priority = ATMEL_AES_PRIORITY, |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1014 | .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC, |
| 1015 | .cra_blocksize = CFB32_BLOCK_SIZE, |
| 1016 | .cra_ctxsize = sizeof(struct atmel_aes_ctx), |
Nicolas Royer | cadc4ab | 2013-02-20 17:10:24 +0100 | [diff] [blame] | 1017 | .cra_alignmask = 0x3, |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1018 | .cra_type = &crypto_ablkcipher_type, |
| 1019 | .cra_module = THIS_MODULE, |
| 1020 | .cra_init = atmel_aes_cra_init, |
| 1021 | .cra_exit = atmel_aes_cra_exit, |
| 1022 | .cra_u.ablkcipher = { |
| 1023 | .min_keysize = AES_MIN_KEY_SIZE, |
| 1024 | .max_keysize = AES_MAX_KEY_SIZE, |
| 1025 | .ivsize = AES_BLOCK_SIZE, |
| 1026 | .setkey = atmel_aes_setkey, |
| 1027 | .encrypt = atmel_aes_cfb32_encrypt, |
| 1028 | .decrypt = atmel_aes_cfb32_decrypt, |
| 1029 | } |
| 1030 | }, |
| 1031 | { |
| 1032 | .cra_name = "cfb16(aes)", |
| 1033 | .cra_driver_name = "atmel-cfb16-aes", |
Cyrille Pitchen | 88efd9a | 2015-12-17 17:48:34 +0100 | [diff] [blame] | 1034 | .cra_priority = ATMEL_AES_PRIORITY, |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1035 | .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC, |
| 1036 | .cra_blocksize = CFB16_BLOCK_SIZE, |
| 1037 | .cra_ctxsize = sizeof(struct atmel_aes_ctx), |
Nicolas Royer | cadc4ab | 2013-02-20 17:10:24 +0100 | [diff] [blame] | 1038 | .cra_alignmask = 0x1, |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1039 | .cra_type = &crypto_ablkcipher_type, |
| 1040 | .cra_module = THIS_MODULE, |
| 1041 | .cra_init = atmel_aes_cra_init, |
| 1042 | .cra_exit = atmel_aes_cra_exit, |
| 1043 | .cra_u.ablkcipher = { |
| 1044 | .min_keysize = AES_MIN_KEY_SIZE, |
| 1045 | .max_keysize = AES_MAX_KEY_SIZE, |
| 1046 | .ivsize = AES_BLOCK_SIZE, |
| 1047 | .setkey = atmel_aes_setkey, |
| 1048 | .encrypt = atmel_aes_cfb16_encrypt, |
| 1049 | .decrypt = atmel_aes_cfb16_decrypt, |
| 1050 | } |
| 1051 | }, |
| 1052 | { |
| 1053 | .cra_name = "cfb8(aes)", |
| 1054 | .cra_driver_name = "atmel-cfb8-aes", |
Cyrille Pitchen | 88efd9a | 2015-12-17 17:48:34 +0100 | [diff] [blame] | 1055 | .cra_priority = ATMEL_AES_PRIORITY, |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1056 | .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC, |
Leilei Zhao | e5d8c96 | 2014-04-22 15:23:23 +0800 | [diff] [blame] | 1057 | .cra_blocksize = CFB8_BLOCK_SIZE, |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1058 | .cra_ctxsize = sizeof(struct atmel_aes_ctx), |
| 1059 | .cra_alignmask = 0x0, |
| 1060 | .cra_type = &crypto_ablkcipher_type, |
| 1061 | .cra_module = THIS_MODULE, |
| 1062 | .cra_init = atmel_aes_cra_init, |
| 1063 | .cra_exit = atmel_aes_cra_exit, |
| 1064 | .cra_u.ablkcipher = { |
| 1065 | .min_keysize = AES_MIN_KEY_SIZE, |
| 1066 | .max_keysize = AES_MAX_KEY_SIZE, |
| 1067 | .ivsize = AES_BLOCK_SIZE, |
| 1068 | .setkey = atmel_aes_setkey, |
| 1069 | .encrypt = atmel_aes_cfb8_encrypt, |
| 1070 | .decrypt = atmel_aes_cfb8_decrypt, |
| 1071 | } |
| 1072 | }, |
| 1073 | { |
| 1074 | .cra_name = "ctr(aes)", |
| 1075 | .cra_driver_name = "atmel-ctr-aes", |
Cyrille Pitchen | 88efd9a | 2015-12-17 17:48:34 +0100 | [diff] [blame] | 1076 | .cra_priority = ATMEL_AES_PRIORITY, |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1077 | .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC, |
| 1078 | .cra_blocksize = AES_BLOCK_SIZE, |
| 1079 | .cra_ctxsize = sizeof(struct atmel_aes_ctx), |
Nicolas Royer | cadc4ab | 2013-02-20 17:10:24 +0100 | [diff] [blame] | 1080 | .cra_alignmask = 0xf, |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1081 | .cra_type = &crypto_ablkcipher_type, |
| 1082 | .cra_module = THIS_MODULE, |
| 1083 | .cra_init = atmel_aes_cra_init, |
| 1084 | .cra_exit = atmel_aes_cra_exit, |
| 1085 | .cra_u.ablkcipher = { |
| 1086 | .min_keysize = AES_MIN_KEY_SIZE, |
| 1087 | .max_keysize = AES_MAX_KEY_SIZE, |
| 1088 | .ivsize = AES_BLOCK_SIZE, |
| 1089 | .setkey = atmel_aes_setkey, |
| 1090 | .encrypt = atmel_aes_ctr_encrypt, |
| 1091 | .decrypt = atmel_aes_ctr_decrypt, |
| 1092 | } |
| 1093 | }, |
| 1094 | }; |
| 1095 | |
Nicolas Royer | cadc4ab | 2013-02-20 17:10:24 +0100 | [diff] [blame] | 1096 | static struct crypto_alg aes_cfb64_alg = { |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1097 | .cra_name = "cfb64(aes)", |
| 1098 | .cra_driver_name = "atmel-cfb64-aes", |
Cyrille Pitchen | 88efd9a | 2015-12-17 17:48:34 +0100 | [diff] [blame] | 1099 | .cra_priority = ATMEL_AES_PRIORITY, |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1100 | .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC, |
| 1101 | .cra_blocksize = CFB64_BLOCK_SIZE, |
| 1102 | .cra_ctxsize = sizeof(struct atmel_aes_ctx), |
Nicolas Royer | cadc4ab | 2013-02-20 17:10:24 +0100 | [diff] [blame] | 1103 | .cra_alignmask = 0x7, |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1104 | .cra_type = &crypto_ablkcipher_type, |
| 1105 | .cra_module = THIS_MODULE, |
| 1106 | .cra_init = atmel_aes_cra_init, |
| 1107 | .cra_exit = atmel_aes_cra_exit, |
| 1108 | .cra_u.ablkcipher = { |
| 1109 | .min_keysize = AES_MIN_KEY_SIZE, |
| 1110 | .max_keysize = AES_MAX_KEY_SIZE, |
| 1111 | .ivsize = AES_BLOCK_SIZE, |
| 1112 | .setkey = atmel_aes_setkey, |
| 1113 | .encrypt = atmel_aes_cfb64_encrypt, |
| 1114 | .decrypt = atmel_aes_cfb64_decrypt, |
| 1115 | } |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1116 | }; |
| 1117 | |
Cyrille Pitchen | e37a7e5 | 2015-12-17 18:13:03 +0100 | [diff] [blame^] | 1118 | |
| 1119 | /* Probe functions */ |
| 1120 | |
| 1121 | static int atmel_aes_buff_init(struct atmel_aes_dev *dd) |
| 1122 | { |
| 1123 | dd->buf = (void *)__get_free_pages(GFP_KERNEL, ATMEL_AES_BUFFER_ORDER); |
| 1124 | dd->buflen = ATMEL_AES_BUFFER_SIZE; |
| 1125 | dd->buflen &= ~(AES_BLOCK_SIZE - 1); |
| 1126 | |
| 1127 | if (!dd->buf) { |
| 1128 | dev_err(dd->dev, "unable to alloc pages.\n"); |
| 1129 | return -ENOMEM; |
| 1130 | } |
| 1131 | |
| 1132 | return 0; |
| 1133 | } |
| 1134 | |
| 1135 | static void atmel_aes_buff_cleanup(struct atmel_aes_dev *dd) |
| 1136 | { |
| 1137 | free_page((unsigned long)dd->buf); |
| 1138 | } |
| 1139 | |
| 1140 | static bool atmel_aes_filter(struct dma_chan *chan, void *slave) |
| 1141 | { |
| 1142 | struct at_dma_slave *sl = slave; |
| 1143 | |
| 1144 | if (sl && sl->dma_dev == chan->device->dev) { |
| 1145 | chan->private = sl; |
| 1146 | return true; |
| 1147 | } else { |
| 1148 | return false; |
| 1149 | } |
| 1150 | } |
| 1151 | |
| 1152 | static int atmel_aes_dma_init(struct atmel_aes_dev *dd, |
| 1153 | struct crypto_platform_data *pdata) |
| 1154 | { |
| 1155 | struct at_dma_slave *slave; |
| 1156 | int err = -ENOMEM; |
| 1157 | dma_cap_mask_t mask; |
| 1158 | |
| 1159 | dma_cap_zero(mask); |
| 1160 | dma_cap_set(DMA_SLAVE, mask); |
| 1161 | |
| 1162 | /* Try to grab 2 DMA channels */ |
| 1163 | slave = &pdata->dma_slave->rxdata; |
| 1164 | dd->src.chan = dma_request_slave_channel_compat(mask, atmel_aes_filter, |
| 1165 | slave, dd->dev, "tx"); |
| 1166 | if (!dd->src.chan) |
| 1167 | goto err_dma_in; |
| 1168 | |
| 1169 | slave = &pdata->dma_slave->txdata; |
| 1170 | dd->dst.chan = dma_request_slave_channel_compat(mask, atmel_aes_filter, |
| 1171 | slave, dd->dev, "rx"); |
| 1172 | if (!dd->dst.chan) |
| 1173 | goto err_dma_out; |
| 1174 | |
| 1175 | return 0; |
| 1176 | |
| 1177 | err_dma_out: |
| 1178 | dma_release_channel(dd->src.chan); |
| 1179 | err_dma_in: |
| 1180 | dev_warn(dd->dev, "no DMA channel available\n"); |
| 1181 | return err; |
| 1182 | } |
| 1183 | |
| 1184 | static void atmel_aes_dma_cleanup(struct atmel_aes_dev *dd) |
| 1185 | { |
| 1186 | dma_release_channel(dd->dst.chan); |
| 1187 | dma_release_channel(dd->src.chan); |
| 1188 | } |
| 1189 | |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1190 | static void atmel_aes_queue_task(unsigned long data) |
| 1191 | { |
| 1192 | struct atmel_aes_dev *dd = (struct atmel_aes_dev *)data; |
| 1193 | |
| 1194 | atmel_aes_handle_queue(dd, NULL); |
| 1195 | } |
| 1196 | |
| 1197 | static void atmel_aes_done_task(unsigned long data) |
| 1198 | { |
Cyrille Pitchen | afbac17 | 2015-12-17 18:13:02 +0100 | [diff] [blame] | 1199 | struct atmel_aes_dev *dd = (struct atmel_aes_dev *)data; |
Cyrille Pitchen | 10f12c1 | 2015-12-17 17:48:42 +0100 | [diff] [blame] | 1200 | |
| 1201 | dd->is_async = true; |
| 1202 | (void)dd->resume(dd); |
| 1203 | } |
| 1204 | |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1205 | static irqreturn_t atmel_aes_irq(int irq, void *dev_id) |
| 1206 | { |
| 1207 | struct atmel_aes_dev *aes_dd = dev_id; |
| 1208 | u32 reg; |
| 1209 | |
| 1210 | reg = atmel_aes_read(aes_dd, AES_ISR); |
| 1211 | if (reg & atmel_aes_read(aes_dd, AES_IMR)) { |
| 1212 | atmel_aes_write(aes_dd, AES_IDR, reg); |
| 1213 | if (AES_FLAGS_BUSY & aes_dd->flags) |
| 1214 | tasklet_schedule(&aes_dd->done_task); |
| 1215 | else |
| 1216 | dev_warn(aes_dd->dev, "AES interrupt when no active requests.\n"); |
| 1217 | return IRQ_HANDLED; |
| 1218 | } |
| 1219 | |
| 1220 | return IRQ_NONE; |
| 1221 | } |
| 1222 | |
| 1223 | static void atmel_aes_unregister_algs(struct atmel_aes_dev *dd) |
| 1224 | { |
| 1225 | int i; |
| 1226 | |
Nicolas Royer | cadc4ab | 2013-02-20 17:10:24 +0100 | [diff] [blame] | 1227 | if (dd->caps.has_cfb64) |
| 1228 | crypto_unregister_alg(&aes_cfb64_alg); |
Cyrille Pitchen | 924a8bc | 2015-12-17 17:48:35 +0100 | [diff] [blame] | 1229 | |
| 1230 | for (i = 0; i < ARRAY_SIZE(aes_algs); i++) |
| 1231 | crypto_unregister_alg(&aes_algs[i]); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1232 | } |
| 1233 | |
| 1234 | static int atmel_aes_register_algs(struct atmel_aes_dev *dd) |
| 1235 | { |
| 1236 | int err, i, j; |
| 1237 | |
| 1238 | for (i = 0; i < ARRAY_SIZE(aes_algs); i++) { |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1239 | err = crypto_register_alg(&aes_algs[i]); |
| 1240 | if (err) |
| 1241 | goto err_aes_algs; |
| 1242 | } |
| 1243 | |
Nicolas Royer | cadc4ab | 2013-02-20 17:10:24 +0100 | [diff] [blame] | 1244 | if (dd->caps.has_cfb64) { |
| 1245 | err = crypto_register_alg(&aes_cfb64_alg); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1246 | if (err) |
| 1247 | goto err_aes_cfb64_alg; |
| 1248 | } |
| 1249 | |
| 1250 | return 0; |
| 1251 | |
| 1252 | err_aes_cfb64_alg: |
| 1253 | i = ARRAY_SIZE(aes_algs); |
| 1254 | err_aes_algs: |
| 1255 | for (j = 0; j < i; j++) |
| 1256 | crypto_unregister_alg(&aes_algs[j]); |
| 1257 | |
| 1258 | return err; |
| 1259 | } |
| 1260 | |
Nicolas Royer | cadc4ab | 2013-02-20 17:10:24 +0100 | [diff] [blame] | 1261 | static void atmel_aes_get_cap(struct atmel_aes_dev *dd) |
| 1262 | { |
| 1263 | dd->caps.has_dualbuff = 0; |
| 1264 | dd->caps.has_cfb64 = 0; |
| 1265 | dd->caps.max_burst_size = 1; |
| 1266 | |
| 1267 | /* keep only major version number */ |
| 1268 | switch (dd->hw_version & 0xff0) { |
Leilei Zhao | 973e209 | 2015-12-17 17:48:32 +0100 | [diff] [blame] | 1269 | case 0x500: |
| 1270 | dd->caps.has_dualbuff = 1; |
| 1271 | dd->caps.has_cfb64 = 1; |
| 1272 | dd->caps.max_burst_size = 4; |
| 1273 | break; |
Leilei Zhao | cf1f0d1 | 2015-04-07 17:45:02 +0800 | [diff] [blame] | 1274 | case 0x200: |
| 1275 | dd->caps.has_dualbuff = 1; |
| 1276 | dd->caps.has_cfb64 = 1; |
| 1277 | dd->caps.max_burst_size = 4; |
| 1278 | break; |
Nicolas Royer | cadc4ab | 2013-02-20 17:10:24 +0100 | [diff] [blame] | 1279 | case 0x130: |
| 1280 | dd->caps.has_dualbuff = 1; |
| 1281 | dd->caps.has_cfb64 = 1; |
| 1282 | dd->caps.max_burst_size = 4; |
| 1283 | break; |
| 1284 | case 0x120: |
| 1285 | break; |
| 1286 | default: |
| 1287 | dev_warn(dd->dev, |
| 1288 | "Unmanaged aes version, set minimum capabilities\n"); |
| 1289 | break; |
| 1290 | } |
| 1291 | } |
| 1292 | |
Nicolas Ferre | be943c7 | 2013-10-14 17:52:38 +0200 | [diff] [blame] | 1293 | #if defined(CONFIG_OF) |
| 1294 | static const struct of_device_id atmel_aes_dt_ids[] = { |
| 1295 | { .compatible = "atmel,at91sam9g46-aes" }, |
| 1296 | { /* sentinel */ } |
| 1297 | }; |
| 1298 | MODULE_DEVICE_TABLE(of, atmel_aes_dt_ids); |
| 1299 | |
| 1300 | static struct crypto_platform_data *atmel_aes_of_init(struct platform_device *pdev) |
| 1301 | { |
| 1302 | struct device_node *np = pdev->dev.of_node; |
| 1303 | struct crypto_platform_data *pdata; |
| 1304 | |
| 1305 | if (!np) { |
| 1306 | dev_err(&pdev->dev, "device node not found\n"); |
| 1307 | return ERR_PTR(-EINVAL); |
| 1308 | } |
| 1309 | |
| 1310 | pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL); |
| 1311 | if (!pdata) { |
| 1312 | dev_err(&pdev->dev, "could not allocate memory for pdata\n"); |
| 1313 | return ERR_PTR(-ENOMEM); |
| 1314 | } |
| 1315 | |
| 1316 | pdata->dma_slave = devm_kzalloc(&pdev->dev, |
| 1317 | sizeof(*(pdata->dma_slave)), |
| 1318 | GFP_KERNEL); |
| 1319 | if (!pdata->dma_slave) { |
| 1320 | dev_err(&pdev->dev, "could not allocate memory for dma_slave\n"); |
| 1321 | devm_kfree(&pdev->dev, pdata); |
| 1322 | return ERR_PTR(-ENOMEM); |
| 1323 | } |
| 1324 | |
| 1325 | return pdata; |
| 1326 | } |
| 1327 | #else |
| 1328 | static inline struct crypto_platform_data *atmel_aes_of_init(struct platform_device *pdev) |
| 1329 | { |
| 1330 | return ERR_PTR(-EINVAL); |
| 1331 | } |
| 1332 | #endif |
| 1333 | |
Greg Kroah-Hartman | 49cfe4d | 2012-12-21 13:14:09 -0800 | [diff] [blame] | 1334 | static int atmel_aes_probe(struct platform_device *pdev) |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1335 | { |
| 1336 | struct atmel_aes_dev *aes_dd; |
Nicolas Royer | cadc4ab | 2013-02-20 17:10:24 +0100 | [diff] [blame] | 1337 | struct crypto_platform_data *pdata; |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1338 | struct device *dev = &pdev->dev; |
| 1339 | struct resource *aes_res; |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1340 | int err; |
| 1341 | |
| 1342 | pdata = pdev->dev.platform_data; |
| 1343 | if (!pdata) { |
Nicolas Ferre | be943c7 | 2013-10-14 17:52:38 +0200 | [diff] [blame] | 1344 | pdata = atmel_aes_of_init(pdev); |
| 1345 | if (IS_ERR(pdata)) { |
| 1346 | err = PTR_ERR(pdata); |
| 1347 | goto aes_dd_err; |
| 1348 | } |
| 1349 | } |
| 1350 | |
| 1351 | if (!pdata->dma_slave) { |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1352 | err = -ENXIO; |
| 1353 | goto aes_dd_err; |
| 1354 | } |
| 1355 | |
LABBE Corentin | b0e8b34 | 2015-10-12 19:47:03 +0200 | [diff] [blame] | 1356 | aes_dd = devm_kzalloc(&pdev->dev, sizeof(*aes_dd), GFP_KERNEL); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1357 | if (aes_dd == NULL) { |
| 1358 | dev_err(dev, "unable to alloc data struct.\n"); |
| 1359 | err = -ENOMEM; |
| 1360 | goto aes_dd_err; |
| 1361 | } |
| 1362 | |
| 1363 | aes_dd->dev = dev; |
| 1364 | |
| 1365 | platform_set_drvdata(pdev, aes_dd); |
| 1366 | |
| 1367 | INIT_LIST_HEAD(&aes_dd->list); |
Leilei Zhao | 8a10eb8 | 2015-04-07 17:45:09 +0800 | [diff] [blame] | 1368 | spin_lock_init(&aes_dd->lock); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1369 | |
| 1370 | tasklet_init(&aes_dd->done_task, atmel_aes_done_task, |
| 1371 | (unsigned long)aes_dd); |
| 1372 | tasklet_init(&aes_dd->queue_task, atmel_aes_queue_task, |
| 1373 | (unsigned long)aes_dd); |
| 1374 | |
| 1375 | crypto_init_queue(&aes_dd->queue, ATMEL_AES_QUEUE_LENGTH); |
| 1376 | |
| 1377 | aes_dd->irq = -1; |
| 1378 | |
| 1379 | /* Get the base address */ |
| 1380 | aes_res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 1381 | if (!aes_res) { |
| 1382 | dev_err(dev, "no MEM resource info\n"); |
| 1383 | err = -ENODEV; |
| 1384 | goto res_err; |
| 1385 | } |
| 1386 | aes_dd->phys_base = aes_res->start; |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1387 | |
| 1388 | /* Get the IRQ */ |
| 1389 | aes_dd->irq = platform_get_irq(pdev, 0); |
| 1390 | if (aes_dd->irq < 0) { |
| 1391 | dev_err(dev, "no IRQ resource info\n"); |
| 1392 | err = aes_dd->irq; |
LABBE Corentin | b0e8b34 | 2015-10-12 19:47:03 +0200 | [diff] [blame] | 1393 | goto res_err; |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1394 | } |
| 1395 | |
LABBE Corentin | b0e8b34 | 2015-10-12 19:47:03 +0200 | [diff] [blame] | 1396 | err = devm_request_irq(&pdev->dev, aes_dd->irq, atmel_aes_irq, |
| 1397 | IRQF_SHARED, "atmel-aes", aes_dd); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1398 | if (err) { |
| 1399 | dev_err(dev, "unable to request aes irq.\n"); |
LABBE Corentin | b0e8b34 | 2015-10-12 19:47:03 +0200 | [diff] [blame] | 1400 | goto res_err; |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1401 | } |
| 1402 | |
| 1403 | /* Initializing the clock */ |
LABBE Corentin | b0e8b34 | 2015-10-12 19:47:03 +0200 | [diff] [blame] | 1404 | aes_dd->iclk = devm_clk_get(&pdev->dev, "aes_clk"); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1405 | if (IS_ERR(aes_dd->iclk)) { |
Colin Ian King | be20835 | 2015-02-28 20:40:10 +0000 | [diff] [blame] | 1406 | dev_err(dev, "clock initialization failed.\n"); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1407 | err = PTR_ERR(aes_dd->iclk); |
LABBE Corentin | b0e8b34 | 2015-10-12 19:47:03 +0200 | [diff] [blame] | 1408 | goto res_err; |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1409 | } |
| 1410 | |
LABBE Corentin | b0e8b34 | 2015-10-12 19:47:03 +0200 | [diff] [blame] | 1411 | aes_dd->io_base = devm_ioremap_resource(&pdev->dev, aes_res); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1412 | if (!aes_dd->io_base) { |
| 1413 | dev_err(dev, "can't ioremap\n"); |
| 1414 | err = -ENOMEM; |
LABBE Corentin | b0e8b34 | 2015-10-12 19:47:03 +0200 | [diff] [blame] | 1415 | goto res_err; |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1416 | } |
| 1417 | |
Cyrille Pitchen | aab0a39 | 2015-12-17 17:48:37 +0100 | [diff] [blame] | 1418 | err = atmel_aes_hw_version_init(aes_dd); |
| 1419 | if (err) |
| 1420 | goto res_err; |
Nicolas Royer | cadc4ab | 2013-02-20 17:10:24 +0100 | [diff] [blame] | 1421 | |
| 1422 | atmel_aes_get_cap(aes_dd); |
| 1423 | |
| 1424 | err = atmel_aes_buff_init(aes_dd); |
| 1425 | if (err) |
| 1426 | goto err_aes_buff; |
| 1427 | |
| 1428 | err = atmel_aes_dma_init(aes_dd, pdata); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1429 | if (err) |
| 1430 | goto err_aes_dma; |
| 1431 | |
| 1432 | spin_lock(&atmel_aes.lock); |
| 1433 | list_add_tail(&aes_dd->list, &atmel_aes.dev_list); |
| 1434 | spin_unlock(&atmel_aes.lock); |
| 1435 | |
| 1436 | err = atmel_aes_register_algs(aes_dd); |
| 1437 | if (err) |
| 1438 | goto err_algs; |
| 1439 | |
Nicolas Ferre | be943c7 | 2013-10-14 17:52:38 +0200 | [diff] [blame] | 1440 | dev_info(dev, "Atmel AES - Using %s, %s for DMA transfers\n", |
Cyrille Pitchen | bbe628e | 2015-12-17 18:13:00 +0100 | [diff] [blame] | 1441 | dma_chan_name(aes_dd->src.chan), |
| 1442 | dma_chan_name(aes_dd->dst.chan)); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1443 | |
| 1444 | return 0; |
| 1445 | |
| 1446 | err_algs: |
| 1447 | spin_lock(&atmel_aes.lock); |
| 1448 | list_del(&aes_dd->list); |
| 1449 | spin_unlock(&atmel_aes.lock); |
| 1450 | atmel_aes_dma_cleanup(aes_dd); |
| 1451 | err_aes_dma: |
Nicolas Royer | cadc4ab | 2013-02-20 17:10:24 +0100 | [diff] [blame] | 1452 | atmel_aes_buff_cleanup(aes_dd); |
| 1453 | err_aes_buff: |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1454 | res_err: |
| 1455 | tasklet_kill(&aes_dd->done_task); |
| 1456 | tasklet_kill(&aes_dd->queue_task); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1457 | aes_dd_err: |
| 1458 | dev_err(dev, "initialization failed.\n"); |
| 1459 | |
| 1460 | return err; |
| 1461 | } |
| 1462 | |
Greg Kroah-Hartman | 49cfe4d | 2012-12-21 13:14:09 -0800 | [diff] [blame] | 1463 | static int atmel_aes_remove(struct platform_device *pdev) |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1464 | { |
| 1465 | static struct atmel_aes_dev *aes_dd; |
| 1466 | |
| 1467 | aes_dd = platform_get_drvdata(pdev); |
| 1468 | if (!aes_dd) |
| 1469 | return -ENODEV; |
| 1470 | spin_lock(&atmel_aes.lock); |
| 1471 | list_del(&aes_dd->list); |
| 1472 | spin_unlock(&atmel_aes.lock); |
| 1473 | |
| 1474 | atmel_aes_unregister_algs(aes_dd); |
| 1475 | |
| 1476 | tasklet_kill(&aes_dd->done_task); |
| 1477 | tasklet_kill(&aes_dd->queue_task); |
| 1478 | |
| 1479 | atmel_aes_dma_cleanup(aes_dd); |
Cyrille Pitchen | 2a37782 | 2015-12-17 17:48:46 +0100 | [diff] [blame] | 1480 | atmel_aes_buff_cleanup(aes_dd); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1481 | |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1482 | return 0; |
| 1483 | } |
| 1484 | |
| 1485 | static struct platform_driver atmel_aes_driver = { |
| 1486 | .probe = atmel_aes_probe, |
Greg Kroah-Hartman | 49cfe4d | 2012-12-21 13:14:09 -0800 | [diff] [blame] | 1487 | .remove = atmel_aes_remove, |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1488 | .driver = { |
| 1489 | .name = "atmel_aes", |
Nicolas Ferre | be943c7 | 2013-10-14 17:52:38 +0200 | [diff] [blame] | 1490 | .of_match_table = of_match_ptr(atmel_aes_dt_ids), |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1491 | }, |
| 1492 | }; |
| 1493 | |
| 1494 | module_platform_driver(atmel_aes_driver); |
| 1495 | |
| 1496 | MODULE_DESCRIPTION("Atmel AES hw acceleration support."); |
| 1497 | MODULE_LICENSE("GPL v2"); |
| 1498 | MODULE_AUTHOR("Nicolas Royer - Eukréa Electromatique"); |