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