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> |
Cyrille Pitchen | d52db51 | 2016-10-03 14:33:16 +0200 | [diff] [blame] | 39 | #include <crypto/xts.h> |
Cyrille Pitchen | d441954 | 2015-12-17 18:13:07 +0100 | [diff] [blame] | 40 | #include <crypto/internal/aead.h> |
Nicolas Royer | cadc4ab | 2013-02-20 17:10:24 +0100 | [diff] [blame] | 41 | #include <linux/platform_data/crypto-atmel.h> |
Nicolas Ferre | be943c7 | 2013-10-14 17:52:38 +0200 | [diff] [blame] | 42 | #include <dt-bindings/dma/at91.h> |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 43 | #include "atmel-aes-regs.h" |
| 44 | |
Cyrille Pitchen | 88efd9a | 2015-12-17 17:48:34 +0100 | [diff] [blame] | 45 | #define ATMEL_AES_PRIORITY 300 |
| 46 | |
Cyrille Pitchen | bbe628e | 2015-12-17 18:13:00 +0100 | [diff] [blame] | 47 | #define ATMEL_AES_BUFFER_ORDER 2 |
| 48 | #define ATMEL_AES_BUFFER_SIZE (PAGE_SIZE << ATMEL_AES_BUFFER_ORDER) |
| 49 | |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 50 | #define CFB8_BLOCK_SIZE 1 |
| 51 | #define CFB16_BLOCK_SIZE 2 |
| 52 | #define CFB32_BLOCK_SIZE 4 |
| 53 | #define CFB64_BLOCK_SIZE 8 |
| 54 | |
Cyrille Pitchen | bbe628e | 2015-12-17 18:13:00 +0100 | [diff] [blame] | 55 | #define SIZE_IN_WORDS(x) ((x) >> 2) |
| 56 | |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 57 | /* AES flags */ |
Cyrille Pitchen | d441954 | 2015-12-17 18:13:07 +0100 | [diff] [blame] | 58 | /* Reserve bits [18:16] [14:12] [1:0] for mode (same as for AES_MR) */ |
Cyrille Pitchen | 77dacf5 | 2015-12-17 17:48:41 +0100 | [diff] [blame] | 59 | #define AES_FLAGS_ENCRYPT AES_MR_CYPHER_ENC |
Cyrille Pitchen | d441954 | 2015-12-17 18:13:07 +0100 | [diff] [blame] | 60 | #define AES_FLAGS_GTAGEN AES_MR_GTAGEN |
Cyrille Pitchen | 77dacf5 | 2015-12-17 17:48:41 +0100 | [diff] [blame] | 61 | #define AES_FLAGS_OPMODE_MASK (AES_MR_OPMOD_MASK | AES_MR_CFBS_MASK) |
| 62 | #define AES_FLAGS_ECB AES_MR_OPMOD_ECB |
| 63 | #define AES_FLAGS_CBC AES_MR_OPMOD_CBC |
| 64 | #define AES_FLAGS_OFB AES_MR_OPMOD_OFB |
| 65 | #define AES_FLAGS_CFB128 (AES_MR_OPMOD_CFB | AES_MR_CFBS_128b) |
| 66 | #define AES_FLAGS_CFB64 (AES_MR_OPMOD_CFB | AES_MR_CFBS_64b) |
| 67 | #define AES_FLAGS_CFB32 (AES_MR_OPMOD_CFB | AES_MR_CFBS_32b) |
| 68 | #define AES_FLAGS_CFB16 (AES_MR_OPMOD_CFB | AES_MR_CFBS_16b) |
| 69 | #define AES_FLAGS_CFB8 (AES_MR_OPMOD_CFB | AES_MR_CFBS_8b) |
| 70 | #define AES_FLAGS_CTR AES_MR_OPMOD_CTR |
Cyrille Pitchen | d441954 | 2015-12-17 18:13:07 +0100 | [diff] [blame] | 71 | #define AES_FLAGS_GCM AES_MR_OPMOD_GCM |
Cyrille Pitchen | d52db51 | 2016-10-03 14:33:16 +0200 | [diff] [blame] | 72 | #define AES_FLAGS_XTS AES_MR_OPMOD_XTS |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 73 | |
Cyrille Pitchen | 77dacf5 | 2015-12-17 17:48:41 +0100 | [diff] [blame] | 74 | #define AES_FLAGS_MODE_MASK (AES_FLAGS_OPMODE_MASK | \ |
Cyrille Pitchen | d441954 | 2015-12-17 18:13:07 +0100 | [diff] [blame] | 75 | AES_FLAGS_ENCRYPT | \ |
| 76 | AES_FLAGS_GTAGEN) |
Cyrille Pitchen | 77dacf5 | 2015-12-17 17:48:41 +0100 | [diff] [blame] | 77 | |
| 78 | #define AES_FLAGS_INIT BIT(2) |
| 79 | #define AES_FLAGS_BUSY BIT(3) |
Cyrille Pitchen | 4537992 | 2015-12-17 18:13:08 +0100 | [diff] [blame] | 80 | #define AES_FLAGS_DUMP_REG BIT(4) |
Cyrille Pitchen | 77dacf5 | 2015-12-17 17:48:41 +0100 | [diff] [blame] | 81 | |
| 82 | #define AES_FLAGS_PERSISTENT (AES_FLAGS_INIT | AES_FLAGS_BUSY) |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 83 | |
Nicolas Royer | cadc4ab | 2013-02-20 17:10:24 +0100 | [diff] [blame] | 84 | #define ATMEL_AES_QUEUE_LENGTH 50 |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 85 | |
Cyrille Pitchen | 129f8bb | 2015-12-17 18:13:06 +0100 | [diff] [blame] | 86 | #define ATMEL_AES_DMA_THRESHOLD 256 |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 87 | |
| 88 | |
Nicolas Royer | cadc4ab | 2013-02-20 17:10:24 +0100 | [diff] [blame] | 89 | struct atmel_aes_caps { |
Cyrille Pitchen | afbac17 | 2015-12-17 18:13:02 +0100 | [diff] [blame] | 90 | bool has_dualbuff; |
| 91 | bool has_cfb64; |
Cyrille Pitchen | fcac836 | 2015-12-17 18:13:05 +0100 | [diff] [blame] | 92 | bool has_ctr32; |
Cyrille Pitchen | d441954 | 2015-12-17 18:13:07 +0100 | [diff] [blame] | 93 | bool has_gcm; |
Cyrille Pitchen | d52db51 | 2016-10-03 14:33:16 +0200 | [diff] [blame] | 94 | bool has_xts; |
Cyrille Pitchen | afbac17 | 2015-12-17 18:13:02 +0100 | [diff] [blame] | 95 | u32 max_burst_size; |
Nicolas Royer | cadc4ab | 2013-02-20 17:10:24 +0100 | [diff] [blame] | 96 | }; |
| 97 | |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 98 | struct atmel_aes_dev; |
| 99 | |
Cyrille Pitchen | ccbf729 | 2015-12-17 17:48:39 +0100 | [diff] [blame] | 100 | |
| 101 | typedef int (*atmel_aes_fn_t)(struct atmel_aes_dev *); |
| 102 | |
| 103 | |
| 104 | struct atmel_aes_base_ctx { |
Cyrille Pitchen | afbac17 | 2015-12-17 18:13:02 +0100 | [diff] [blame] | 105 | struct atmel_aes_dev *dd; |
| 106 | atmel_aes_fn_t start; |
| 107 | int keylen; |
| 108 | u32 key[AES_KEYSIZE_256 / sizeof(u32)]; |
| 109 | u16 block_size; |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 110 | }; |
| 111 | |
Cyrille Pitchen | ccbf729 | 2015-12-17 17:48:39 +0100 | [diff] [blame] | 112 | struct atmel_aes_ctx { |
| 113 | struct atmel_aes_base_ctx base; |
| 114 | }; |
| 115 | |
Cyrille Pitchen | fcac836 | 2015-12-17 18:13:05 +0100 | [diff] [blame] | 116 | struct atmel_aes_ctr_ctx { |
| 117 | struct atmel_aes_base_ctx base; |
| 118 | |
| 119 | u32 iv[AES_BLOCK_SIZE / sizeof(u32)]; |
| 120 | size_t offset; |
| 121 | struct scatterlist src[2]; |
| 122 | struct scatterlist dst[2]; |
| 123 | }; |
| 124 | |
Cyrille Pitchen | d441954 | 2015-12-17 18:13:07 +0100 | [diff] [blame] | 125 | struct atmel_aes_gcm_ctx { |
| 126 | struct atmel_aes_base_ctx base; |
| 127 | |
| 128 | struct scatterlist src[2]; |
| 129 | struct scatterlist dst[2]; |
| 130 | |
| 131 | u32 j0[AES_BLOCK_SIZE / sizeof(u32)]; |
| 132 | u32 tag[AES_BLOCK_SIZE / sizeof(u32)]; |
| 133 | u32 ghash[AES_BLOCK_SIZE / sizeof(u32)]; |
| 134 | size_t textlen; |
| 135 | |
| 136 | const u32 *ghash_in; |
| 137 | u32 *ghash_out; |
| 138 | atmel_aes_fn_t ghash_resume; |
| 139 | }; |
| 140 | |
Cyrille Pitchen | d52db51 | 2016-10-03 14:33:16 +0200 | [diff] [blame] | 141 | struct atmel_aes_xts_ctx { |
| 142 | struct atmel_aes_base_ctx base; |
| 143 | |
| 144 | u32 key2[AES_KEYSIZE_256 / sizeof(u32)]; |
| 145 | }; |
| 146 | |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 147 | struct atmel_aes_reqctx { |
Cyrille Pitchen | afbac17 | 2015-12-17 18:13:02 +0100 | [diff] [blame] | 148 | unsigned long mode; |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 149 | }; |
| 150 | |
| 151 | struct atmel_aes_dma { |
Cyrille Pitchen | bbe628e | 2015-12-17 18:13:00 +0100 | [diff] [blame] | 152 | struct dma_chan *chan; |
| 153 | struct scatterlist *sg; |
| 154 | int nents; |
| 155 | unsigned int remainder; |
| 156 | unsigned int sg_len; |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 157 | }; |
| 158 | |
| 159 | struct atmel_aes_dev { |
| 160 | struct list_head list; |
| 161 | unsigned long phys_base; |
| 162 | void __iomem *io_base; |
| 163 | |
Cyrille Pitchen | ccbf729 | 2015-12-17 17:48:39 +0100 | [diff] [blame] | 164 | struct crypto_async_request *areq; |
| 165 | struct atmel_aes_base_ctx *ctx; |
| 166 | |
Cyrille Pitchen | 10f12c1 | 2015-12-17 17:48:42 +0100 | [diff] [blame] | 167 | bool is_async; |
| 168 | atmel_aes_fn_t resume; |
Cyrille Pitchen | bbe628e | 2015-12-17 18:13:00 +0100 | [diff] [blame] | 169 | atmel_aes_fn_t cpu_transfer_complete; |
Cyrille Pitchen | 10f12c1 | 2015-12-17 17:48:42 +0100 | [diff] [blame] | 170 | |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 171 | struct device *dev; |
| 172 | struct clk *iclk; |
Cyrille Pitchen | afbac17 | 2015-12-17 18:13:02 +0100 | [diff] [blame] | 173 | int irq; |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 174 | |
| 175 | unsigned long flags; |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 176 | |
| 177 | spinlock_t lock; |
| 178 | struct crypto_queue queue; |
| 179 | |
| 180 | struct tasklet_struct done_task; |
| 181 | struct tasklet_struct queue_task; |
| 182 | |
Cyrille Pitchen | bbe628e | 2015-12-17 18:13:00 +0100 | [diff] [blame] | 183 | size_t total; |
| 184 | size_t datalen; |
| 185 | u32 *data; |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 186 | |
Cyrille Pitchen | bbe628e | 2015-12-17 18:13:00 +0100 | [diff] [blame] | 187 | struct atmel_aes_dma src; |
| 188 | struct atmel_aes_dma dst; |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 189 | |
Cyrille Pitchen | bbe628e | 2015-12-17 18:13:00 +0100 | [diff] [blame] | 190 | size_t buflen; |
| 191 | void *buf; |
| 192 | struct scatterlist aligned_sg; |
| 193 | struct scatterlist *real_dst; |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 194 | |
Nicolas Royer | cadc4ab | 2013-02-20 17:10:24 +0100 | [diff] [blame] | 195 | struct atmel_aes_caps caps; |
| 196 | |
Cyrille Pitchen | afbac17 | 2015-12-17 18:13:02 +0100 | [diff] [blame] | 197 | u32 hw_version; |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 198 | }; |
| 199 | |
| 200 | struct atmel_aes_drv { |
| 201 | struct list_head dev_list; |
| 202 | spinlock_t lock; |
| 203 | }; |
| 204 | |
| 205 | static struct atmel_aes_drv atmel_aes = { |
| 206 | .dev_list = LIST_HEAD_INIT(atmel_aes.dev_list), |
| 207 | .lock = __SPIN_LOCK_UNLOCKED(atmel_aes.lock), |
| 208 | }; |
| 209 | |
Cyrille Pitchen | 4537992 | 2015-12-17 18:13:08 +0100 | [diff] [blame] | 210 | #ifdef VERBOSE_DEBUG |
| 211 | static const char *atmel_aes_reg_name(u32 offset, char *tmp, size_t sz) |
| 212 | { |
| 213 | switch (offset) { |
| 214 | case AES_CR: |
| 215 | return "CR"; |
| 216 | |
| 217 | case AES_MR: |
| 218 | return "MR"; |
| 219 | |
| 220 | case AES_ISR: |
| 221 | return "ISR"; |
| 222 | |
| 223 | case AES_IMR: |
| 224 | return "IMR"; |
| 225 | |
| 226 | case AES_IER: |
| 227 | return "IER"; |
| 228 | |
| 229 | case AES_IDR: |
| 230 | return "IDR"; |
| 231 | |
| 232 | case AES_KEYWR(0): |
| 233 | case AES_KEYWR(1): |
| 234 | case AES_KEYWR(2): |
| 235 | case AES_KEYWR(3): |
| 236 | case AES_KEYWR(4): |
| 237 | case AES_KEYWR(5): |
| 238 | case AES_KEYWR(6): |
| 239 | case AES_KEYWR(7): |
| 240 | snprintf(tmp, sz, "KEYWR[%u]", (offset - AES_KEYWR(0)) >> 2); |
| 241 | break; |
| 242 | |
| 243 | case AES_IDATAR(0): |
| 244 | case AES_IDATAR(1): |
| 245 | case AES_IDATAR(2): |
| 246 | case AES_IDATAR(3): |
| 247 | snprintf(tmp, sz, "IDATAR[%u]", (offset - AES_IDATAR(0)) >> 2); |
| 248 | break; |
| 249 | |
| 250 | case AES_ODATAR(0): |
| 251 | case AES_ODATAR(1): |
| 252 | case AES_ODATAR(2): |
| 253 | case AES_ODATAR(3): |
| 254 | snprintf(tmp, sz, "ODATAR[%u]", (offset - AES_ODATAR(0)) >> 2); |
| 255 | break; |
| 256 | |
| 257 | case AES_IVR(0): |
| 258 | case AES_IVR(1): |
| 259 | case AES_IVR(2): |
| 260 | case AES_IVR(3): |
| 261 | snprintf(tmp, sz, "IVR[%u]", (offset - AES_IVR(0)) >> 2); |
| 262 | break; |
| 263 | |
| 264 | case AES_AADLENR: |
| 265 | return "AADLENR"; |
| 266 | |
| 267 | case AES_CLENR: |
| 268 | return "CLENR"; |
| 269 | |
| 270 | case AES_GHASHR(0): |
| 271 | case AES_GHASHR(1): |
| 272 | case AES_GHASHR(2): |
| 273 | case AES_GHASHR(3): |
| 274 | snprintf(tmp, sz, "GHASHR[%u]", (offset - AES_GHASHR(0)) >> 2); |
| 275 | break; |
| 276 | |
| 277 | case AES_TAGR(0): |
| 278 | case AES_TAGR(1): |
| 279 | case AES_TAGR(2): |
| 280 | case AES_TAGR(3): |
| 281 | snprintf(tmp, sz, "TAGR[%u]", (offset - AES_TAGR(0)) >> 2); |
| 282 | break; |
| 283 | |
| 284 | case AES_CTRR: |
| 285 | return "CTRR"; |
| 286 | |
| 287 | case AES_GCMHR(0): |
| 288 | case AES_GCMHR(1): |
| 289 | case AES_GCMHR(2): |
| 290 | case AES_GCMHR(3): |
| 291 | snprintf(tmp, sz, "GCMHR[%u]", (offset - AES_GCMHR(0)) >> 2); |
Herbert Xu | e31835a | 2016-01-19 09:05:43 +0800 | [diff] [blame] | 292 | break; |
Cyrille Pitchen | 4537992 | 2015-12-17 18:13:08 +0100 | [diff] [blame] | 293 | |
Cyrille Pitchen | d52db51 | 2016-10-03 14:33:16 +0200 | [diff] [blame] | 294 | case AES_TWR(0): |
| 295 | case AES_TWR(1): |
| 296 | case AES_TWR(2): |
| 297 | case AES_TWR(3): |
| 298 | snprintf(tmp, sz, "TWR[%u]", (offset - AES_TWR(0)) >> 2); |
| 299 | break; |
| 300 | |
| 301 | case AES_ALPHAR(0): |
| 302 | case AES_ALPHAR(1): |
| 303 | case AES_ALPHAR(2): |
| 304 | case AES_ALPHAR(3): |
| 305 | snprintf(tmp, sz, "ALPHAR[%u]", (offset - AES_ALPHAR(0)) >> 2); |
| 306 | break; |
| 307 | |
Cyrille Pitchen | 4537992 | 2015-12-17 18:13:08 +0100 | [diff] [blame] | 308 | default: |
| 309 | snprintf(tmp, sz, "0x%02x", offset); |
| 310 | break; |
| 311 | } |
| 312 | |
| 313 | return tmp; |
| 314 | } |
| 315 | #endif /* VERBOSE_DEBUG */ |
| 316 | |
Cyrille Pitchen | e37a7e5 | 2015-12-17 18:13:03 +0100 | [diff] [blame] | 317 | /* Shared functions */ |
Nicolas Royer | cadc4ab | 2013-02-20 17:10:24 +0100 | [diff] [blame] | 318 | |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 319 | static inline u32 atmel_aes_read(struct atmel_aes_dev *dd, u32 offset) |
| 320 | { |
Cyrille Pitchen | 4537992 | 2015-12-17 18:13:08 +0100 | [diff] [blame] | 321 | u32 value = readl_relaxed(dd->io_base + offset); |
| 322 | |
| 323 | #ifdef VERBOSE_DEBUG |
| 324 | if (dd->flags & AES_FLAGS_DUMP_REG) { |
| 325 | char tmp[16]; |
| 326 | |
| 327 | dev_vdbg(dd->dev, "read 0x%08x from %s\n", value, |
| 328 | atmel_aes_reg_name(offset, tmp, sizeof(tmp))); |
| 329 | } |
| 330 | #endif /* VERBOSE_DEBUG */ |
| 331 | |
| 332 | return value; |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | static inline void atmel_aes_write(struct atmel_aes_dev *dd, |
| 336 | u32 offset, u32 value) |
| 337 | { |
Cyrille Pitchen | 4537992 | 2015-12-17 18:13:08 +0100 | [diff] [blame] | 338 | #ifdef VERBOSE_DEBUG |
| 339 | if (dd->flags & AES_FLAGS_DUMP_REG) { |
| 340 | char tmp[16]; |
| 341 | |
| 342 | dev_vdbg(dd->dev, "write 0x%08x into %s\n", value, |
Cyrille Pitchen | f709dc8 | 2016-09-29 18:46:57 +0200 | [diff] [blame] | 343 | atmel_aes_reg_name(offset, tmp, sizeof(tmp))); |
Cyrille Pitchen | 4537992 | 2015-12-17 18:13:08 +0100 | [diff] [blame] | 344 | } |
| 345 | #endif /* VERBOSE_DEBUG */ |
| 346 | |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 347 | writel_relaxed(value, dd->io_base + offset); |
| 348 | } |
| 349 | |
| 350 | static void atmel_aes_read_n(struct atmel_aes_dev *dd, u32 offset, |
| 351 | u32 *value, int count) |
| 352 | { |
| 353 | for (; count--; value++, offset += 4) |
| 354 | *value = atmel_aes_read(dd, offset); |
| 355 | } |
| 356 | |
| 357 | 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] | 358 | const u32 *value, int count) |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 359 | { |
| 360 | for (; count--; value++, offset += 4) |
| 361 | atmel_aes_write(dd, offset, *value); |
| 362 | } |
| 363 | |
Cyrille Pitchen | bbe628e | 2015-12-17 18:13:00 +0100 | [diff] [blame] | 364 | static inline void atmel_aes_read_block(struct atmel_aes_dev *dd, u32 offset, |
| 365 | u32 *value) |
| 366 | { |
| 367 | atmel_aes_read_n(dd, offset, value, SIZE_IN_WORDS(AES_BLOCK_SIZE)); |
| 368 | } |
| 369 | |
| 370 | static inline void atmel_aes_write_block(struct atmel_aes_dev *dd, u32 offset, |
| 371 | const u32 *value) |
| 372 | { |
| 373 | atmel_aes_write_n(dd, offset, value, SIZE_IN_WORDS(AES_BLOCK_SIZE)); |
| 374 | } |
| 375 | |
| 376 | static inline int atmel_aes_wait_for_data_ready(struct atmel_aes_dev *dd, |
| 377 | atmel_aes_fn_t resume) |
| 378 | { |
| 379 | u32 isr = atmel_aes_read(dd, AES_ISR); |
| 380 | |
| 381 | if (unlikely(isr & AES_INT_DATARDY)) |
| 382 | return resume(dd); |
| 383 | |
| 384 | dd->resume = resume; |
| 385 | atmel_aes_write(dd, AES_IER, AES_INT_DATARDY); |
| 386 | return -EINPROGRESS; |
| 387 | } |
| 388 | |
| 389 | static inline size_t atmel_aes_padlen(size_t len, size_t block_size) |
| 390 | { |
| 391 | len &= block_size - 1; |
| 392 | return len ? block_size - len : 0; |
| 393 | } |
| 394 | |
Cyrille Pitchen | ccbf729 | 2015-12-17 17:48:39 +0100 | [diff] [blame] | 395 | 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] | 396 | { |
| 397 | struct atmel_aes_dev *aes_dd = NULL; |
| 398 | struct atmel_aes_dev *tmp; |
| 399 | |
| 400 | spin_lock_bh(&atmel_aes.lock); |
| 401 | if (!ctx->dd) { |
| 402 | list_for_each_entry(tmp, &atmel_aes.dev_list, list) { |
| 403 | aes_dd = tmp; |
| 404 | break; |
| 405 | } |
| 406 | ctx->dd = aes_dd; |
| 407 | } else { |
| 408 | aes_dd = ctx->dd; |
| 409 | } |
| 410 | |
| 411 | spin_unlock_bh(&atmel_aes.lock); |
| 412 | |
| 413 | return aes_dd; |
| 414 | } |
| 415 | |
| 416 | static int atmel_aes_hw_init(struct atmel_aes_dev *dd) |
| 417 | { |
LABBE Corentin | 9d83d29 | 2015-10-02 14:12:58 +0200 | [diff] [blame] | 418 | int err; |
| 419 | |
Cyrille Pitchen | 49a2045 | 2016-01-29 17:53:33 +0100 | [diff] [blame] | 420 | err = clk_enable(dd->iclk); |
LABBE Corentin | 9d83d29 | 2015-10-02 14:12:58 +0200 | [diff] [blame] | 421 | if (err) |
| 422 | return err; |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 423 | |
| 424 | if (!(dd->flags & AES_FLAGS_INIT)) { |
| 425 | atmel_aes_write(dd, AES_CR, AES_CR_SWRST); |
Nicolas Royer | cadc4ab | 2013-02-20 17:10:24 +0100 | [diff] [blame] | 426 | atmel_aes_write(dd, AES_MR, 0xE << AES_MR_CKEY_OFFSET); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 427 | dd->flags |= AES_FLAGS_INIT; |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 428 | } |
| 429 | |
| 430 | return 0; |
| 431 | } |
| 432 | |
Nicolas Royer | cadc4ab | 2013-02-20 17:10:24 +0100 | [diff] [blame] | 433 | static inline unsigned int atmel_aes_get_version(struct atmel_aes_dev *dd) |
| 434 | { |
| 435 | return atmel_aes_read(dd, AES_HW_VERSION) & 0x00000fff; |
| 436 | } |
| 437 | |
Cyrille Pitchen | aab0a39 | 2015-12-17 17:48:37 +0100 | [diff] [blame] | 438 | static int atmel_aes_hw_version_init(struct atmel_aes_dev *dd) |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 439 | { |
Cyrille Pitchen | aab0a39 | 2015-12-17 17:48:37 +0100 | [diff] [blame] | 440 | int err; |
| 441 | |
| 442 | err = atmel_aes_hw_init(dd); |
| 443 | if (err) |
| 444 | return err; |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 445 | |
Nicolas Royer | cadc4ab | 2013-02-20 17:10:24 +0100 | [diff] [blame] | 446 | dd->hw_version = atmel_aes_get_version(dd); |
| 447 | |
Cyrille Pitchen | aab0a39 | 2015-12-17 17:48:37 +0100 | [diff] [blame] | 448 | dev_info(dd->dev, "version: 0x%x\n", dd->hw_version); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 449 | |
Cyrille Pitchen | 49a2045 | 2016-01-29 17:53:33 +0100 | [diff] [blame] | 450 | clk_disable(dd->iclk); |
Cyrille Pitchen | aab0a39 | 2015-12-17 17:48:37 +0100 | [diff] [blame] | 451 | return 0; |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 452 | } |
| 453 | |
Cyrille Pitchen | 77dacf5 | 2015-12-17 17:48:41 +0100 | [diff] [blame] | 454 | static inline void atmel_aes_set_mode(struct atmel_aes_dev *dd, |
| 455 | const struct atmel_aes_reqctx *rctx) |
| 456 | { |
| 457 | /* Clear all but persistent flags and set request flags. */ |
| 458 | dd->flags = (dd->flags & AES_FLAGS_PERSISTENT) | rctx->mode; |
| 459 | } |
| 460 | |
Cyrille Pitchen | d441954 | 2015-12-17 18:13:07 +0100 | [diff] [blame] | 461 | static inline bool atmel_aes_is_encrypt(const struct atmel_aes_dev *dd) |
| 462 | { |
| 463 | return (dd->flags & AES_FLAGS_ENCRYPT); |
| 464 | } |
| 465 | |
Cyrille Pitchen | 10f12c1 | 2015-12-17 17:48:42 +0100 | [diff] [blame] | 466 | 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] | 467 | { |
Cyrille Pitchen | 49a2045 | 2016-01-29 17:53:33 +0100 | [diff] [blame] | 468 | clk_disable(dd->iclk); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 469 | dd->flags &= ~AES_FLAGS_BUSY; |
| 470 | |
Cyrille Pitchen | 10f12c1 | 2015-12-17 17:48:42 +0100 | [diff] [blame] | 471 | if (dd->is_async) |
| 472 | dd->areq->complete(dd->areq, err); |
| 473 | |
| 474 | tasklet_schedule(&dd->queue_task); |
| 475 | |
| 476 | return err; |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 477 | } |
| 478 | |
Cyrille Pitchen | d52db51 | 2016-10-03 14:33:16 +0200 | [diff] [blame] | 479 | static void atmel_aes_write_ctrl_key(struct atmel_aes_dev *dd, bool use_dma, |
| 480 | const u32 *iv, const u32 *key, int keylen) |
Cyrille Pitchen | e37a7e5 | 2015-12-17 18:13:03 +0100 | [diff] [blame] | 481 | { |
| 482 | u32 valmr = 0; |
| 483 | |
| 484 | /* MR register must be set before IV registers */ |
Cyrille Pitchen | d52db51 | 2016-10-03 14:33:16 +0200 | [diff] [blame] | 485 | if (keylen == AES_KEYSIZE_128) |
Cyrille Pitchen | e37a7e5 | 2015-12-17 18:13:03 +0100 | [diff] [blame] | 486 | valmr |= AES_MR_KEYSIZE_128; |
Cyrille Pitchen | d52db51 | 2016-10-03 14:33:16 +0200 | [diff] [blame] | 487 | else if (keylen == AES_KEYSIZE_192) |
Cyrille Pitchen | e37a7e5 | 2015-12-17 18:13:03 +0100 | [diff] [blame] | 488 | valmr |= AES_MR_KEYSIZE_192; |
| 489 | else |
| 490 | valmr |= AES_MR_KEYSIZE_256; |
| 491 | |
| 492 | valmr |= dd->flags & AES_FLAGS_MODE_MASK; |
| 493 | |
| 494 | if (use_dma) { |
| 495 | valmr |= AES_MR_SMOD_IDATAR0; |
| 496 | if (dd->caps.has_dualbuff) |
| 497 | valmr |= AES_MR_DUALBUFF; |
| 498 | } else { |
| 499 | valmr |= AES_MR_SMOD_AUTO; |
| 500 | } |
| 501 | |
| 502 | atmel_aes_write(dd, AES_MR, valmr); |
| 503 | |
Cyrille Pitchen | d52db51 | 2016-10-03 14:33:16 +0200 | [diff] [blame] | 504 | atmel_aes_write_n(dd, AES_KEYWR(0), key, SIZE_IN_WORDS(keylen)); |
Cyrille Pitchen | e37a7e5 | 2015-12-17 18:13:03 +0100 | [diff] [blame] | 505 | |
| 506 | if (iv && (valmr & AES_MR_OPMOD_MASK) != AES_MR_OPMOD_ECB) |
| 507 | atmel_aes_write_block(dd, AES_IVR(0), iv); |
| 508 | } |
| 509 | |
Cyrille Pitchen | d52db51 | 2016-10-03 14:33:16 +0200 | [diff] [blame] | 510 | static inline void atmel_aes_write_ctrl(struct atmel_aes_dev *dd, bool use_dma, |
| 511 | const u32 *iv) |
| 512 | |
| 513 | { |
| 514 | atmel_aes_write_ctrl_key(dd, use_dma, iv, |
| 515 | dd->ctx->key, dd->ctx->keylen); |
| 516 | } |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 517 | |
Cyrille Pitchen | bbe628e | 2015-12-17 18:13:00 +0100 | [diff] [blame] | 518 | /* CPU transfer */ |
| 519 | |
| 520 | static int atmel_aes_cpu_transfer(struct atmel_aes_dev *dd) |
| 521 | { |
| 522 | int err = 0; |
| 523 | u32 isr; |
| 524 | |
| 525 | for (;;) { |
| 526 | atmel_aes_read_block(dd, AES_ODATAR(0), dd->data); |
| 527 | dd->data += 4; |
| 528 | dd->datalen -= AES_BLOCK_SIZE; |
| 529 | |
| 530 | if (dd->datalen < AES_BLOCK_SIZE) |
| 531 | break; |
| 532 | |
| 533 | atmel_aes_write_block(dd, AES_IDATAR(0), dd->data); |
| 534 | |
| 535 | isr = atmel_aes_read(dd, AES_ISR); |
| 536 | if (!(isr & AES_INT_DATARDY)) { |
| 537 | dd->resume = atmel_aes_cpu_transfer; |
| 538 | atmel_aes_write(dd, AES_IER, AES_INT_DATARDY); |
| 539 | return -EINPROGRESS; |
| 540 | } |
| 541 | } |
| 542 | |
| 543 | if (!sg_copy_from_buffer(dd->real_dst, sg_nents(dd->real_dst), |
| 544 | dd->buf, dd->total)) |
| 545 | err = -EINVAL; |
| 546 | |
| 547 | if (err) |
| 548 | return atmel_aes_complete(dd, err); |
| 549 | |
| 550 | return dd->cpu_transfer_complete(dd); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 551 | } |
| 552 | |
Cyrille Pitchen | bbe628e | 2015-12-17 18:13:00 +0100 | [diff] [blame] | 553 | static int atmel_aes_cpu_start(struct atmel_aes_dev *dd, |
| 554 | struct scatterlist *src, |
| 555 | struct scatterlist *dst, |
| 556 | size_t len, |
| 557 | atmel_aes_fn_t resume) |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 558 | { |
Cyrille Pitchen | bbe628e | 2015-12-17 18:13:00 +0100 | [diff] [blame] | 559 | size_t padlen = atmel_aes_padlen(len, AES_BLOCK_SIZE); |
| 560 | |
| 561 | if (unlikely(len == 0)) |
| 562 | return -EINVAL; |
| 563 | |
| 564 | sg_copy_to_buffer(src, sg_nents(src), dd->buf, len); |
| 565 | |
| 566 | dd->total = len; |
| 567 | dd->real_dst = dst; |
| 568 | dd->cpu_transfer_complete = resume; |
| 569 | dd->datalen = len + padlen; |
| 570 | dd->data = (u32 *)dd->buf; |
| 571 | atmel_aes_write_block(dd, AES_IDATAR(0), dd->data); |
| 572 | return atmel_aes_wait_for_data_ready(dd, atmel_aes_cpu_transfer); |
| 573 | } |
| 574 | |
| 575 | |
| 576 | /* DMA transfer */ |
| 577 | |
| 578 | static void atmel_aes_dma_callback(void *data); |
| 579 | |
| 580 | static bool atmel_aes_check_aligned(struct atmel_aes_dev *dd, |
| 581 | struct scatterlist *sg, |
| 582 | size_t len, |
| 583 | struct atmel_aes_dma *dma) |
| 584 | { |
| 585 | int nents; |
| 586 | |
| 587 | if (!IS_ALIGNED(len, dd->ctx->block_size)) |
| 588 | return false; |
| 589 | |
| 590 | for (nents = 0; sg; sg = sg_next(sg), ++nents) { |
| 591 | if (!IS_ALIGNED(sg->offset, sizeof(u32))) |
| 592 | return false; |
| 593 | |
| 594 | if (len <= sg->length) { |
| 595 | if (!IS_ALIGNED(len, dd->ctx->block_size)) |
| 596 | return false; |
| 597 | |
| 598 | dma->nents = nents+1; |
| 599 | dma->remainder = sg->length - len; |
| 600 | sg->length = len; |
| 601 | return true; |
| 602 | } |
| 603 | |
| 604 | if (!IS_ALIGNED(sg->length, dd->ctx->block_size)) |
| 605 | return false; |
| 606 | |
| 607 | len -= sg->length; |
| 608 | } |
| 609 | |
| 610 | return false; |
| 611 | } |
| 612 | |
| 613 | static inline void atmel_aes_restore_sg(const struct atmel_aes_dma *dma) |
| 614 | { |
| 615 | struct scatterlist *sg = dma->sg; |
| 616 | int nents = dma->nents; |
| 617 | |
| 618 | if (!dma->remainder) |
| 619 | return; |
| 620 | |
| 621 | while (--nents > 0 && sg) |
| 622 | sg = sg_next(sg); |
| 623 | |
| 624 | if (!sg) |
| 625 | return; |
| 626 | |
| 627 | sg->length += dma->remainder; |
| 628 | } |
| 629 | |
| 630 | static int atmel_aes_map(struct atmel_aes_dev *dd, |
| 631 | struct scatterlist *src, |
| 632 | struct scatterlist *dst, |
| 633 | size_t len) |
| 634 | { |
| 635 | bool src_aligned, dst_aligned; |
| 636 | size_t padlen; |
| 637 | |
| 638 | dd->total = len; |
| 639 | dd->src.sg = src; |
| 640 | dd->dst.sg = dst; |
| 641 | dd->real_dst = dst; |
| 642 | |
| 643 | src_aligned = atmel_aes_check_aligned(dd, src, len, &dd->src); |
| 644 | if (src == dst) |
| 645 | dst_aligned = src_aligned; |
| 646 | else |
| 647 | dst_aligned = atmel_aes_check_aligned(dd, dst, len, &dd->dst); |
| 648 | if (!src_aligned || !dst_aligned) { |
| 649 | padlen = atmel_aes_padlen(len, dd->ctx->block_size); |
| 650 | |
| 651 | if (dd->buflen < len + padlen) |
| 652 | return -ENOMEM; |
| 653 | |
| 654 | if (!src_aligned) { |
| 655 | sg_copy_to_buffer(src, sg_nents(src), dd->buf, len); |
| 656 | dd->src.sg = &dd->aligned_sg; |
| 657 | dd->src.nents = 1; |
| 658 | dd->src.remainder = 0; |
| 659 | } |
| 660 | |
| 661 | if (!dst_aligned) { |
| 662 | dd->dst.sg = &dd->aligned_sg; |
| 663 | dd->dst.nents = 1; |
| 664 | dd->dst.remainder = 0; |
| 665 | } |
| 666 | |
| 667 | sg_init_table(&dd->aligned_sg, 1); |
| 668 | sg_set_buf(&dd->aligned_sg, dd->buf, len + padlen); |
| 669 | } |
| 670 | |
| 671 | if (dd->src.sg == dd->dst.sg) { |
| 672 | dd->src.sg_len = dma_map_sg(dd->dev, dd->src.sg, dd->src.nents, |
| 673 | DMA_BIDIRECTIONAL); |
| 674 | dd->dst.sg_len = dd->src.sg_len; |
| 675 | if (!dd->src.sg_len) |
| 676 | return -EFAULT; |
| 677 | } else { |
| 678 | dd->src.sg_len = dma_map_sg(dd->dev, dd->src.sg, dd->src.nents, |
| 679 | DMA_TO_DEVICE); |
| 680 | if (!dd->src.sg_len) |
| 681 | return -EFAULT; |
| 682 | |
| 683 | dd->dst.sg_len = dma_map_sg(dd->dev, dd->dst.sg, dd->dst.nents, |
| 684 | DMA_FROM_DEVICE); |
| 685 | if (!dd->dst.sg_len) { |
| 686 | dma_unmap_sg(dd->dev, dd->src.sg, dd->src.nents, |
| 687 | DMA_TO_DEVICE); |
| 688 | return -EFAULT; |
| 689 | } |
| 690 | } |
| 691 | |
| 692 | return 0; |
| 693 | } |
| 694 | |
| 695 | static void atmel_aes_unmap(struct atmel_aes_dev *dd) |
| 696 | { |
| 697 | if (dd->src.sg == dd->dst.sg) { |
| 698 | dma_unmap_sg(dd->dev, dd->src.sg, dd->src.nents, |
| 699 | DMA_BIDIRECTIONAL); |
| 700 | |
| 701 | if (dd->src.sg != &dd->aligned_sg) |
| 702 | atmel_aes_restore_sg(&dd->src); |
| 703 | } else { |
| 704 | dma_unmap_sg(dd->dev, dd->dst.sg, dd->dst.nents, |
| 705 | DMA_FROM_DEVICE); |
| 706 | |
| 707 | if (dd->dst.sg != &dd->aligned_sg) |
| 708 | atmel_aes_restore_sg(&dd->dst); |
| 709 | |
| 710 | dma_unmap_sg(dd->dev, dd->src.sg, dd->src.nents, |
| 711 | DMA_TO_DEVICE); |
| 712 | |
| 713 | if (dd->src.sg != &dd->aligned_sg) |
| 714 | atmel_aes_restore_sg(&dd->src); |
| 715 | } |
| 716 | |
| 717 | if (dd->dst.sg == &dd->aligned_sg) |
| 718 | sg_copy_from_buffer(dd->real_dst, sg_nents(dd->real_dst), |
| 719 | dd->buf, dd->total); |
| 720 | } |
| 721 | |
| 722 | static int atmel_aes_dma_transfer_start(struct atmel_aes_dev *dd, |
| 723 | enum dma_slave_buswidth addr_width, |
| 724 | enum dma_transfer_direction dir, |
| 725 | u32 maxburst) |
| 726 | { |
| 727 | struct dma_async_tx_descriptor *desc; |
| 728 | struct dma_slave_config config; |
| 729 | dma_async_tx_callback callback; |
| 730 | struct atmel_aes_dma *dma; |
| 731 | int err; |
| 732 | |
| 733 | memset(&config, 0, sizeof(config)); |
| 734 | config.direction = dir; |
| 735 | config.src_addr_width = addr_width; |
| 736 | config.dst_addr_width = addr_width; |
| 737 | config.src_maxburst = maxburst; |
| 738 | config.dst_maxburst = maxburst; |
| 739 | |
| 740 | switch (dir) { |
| 741 | case DMA_MEM_TO_DEV: |
| 742 | dma = &dd->src; |
| 743 | callback = NULL; |
| 744 | config.dst_addr = dd->phys_base + AES_IDATAR(0); |
| 745 | break; |
| 746 | |
| 747 | case DMA_DEV_TO_MEM: |
| 748 | dma = &dd->dst; |
| 749 | callback = atmel_aes_dma_callback; |
| 750 | config.src_addr = dd->phys_base + AES_ODATAR(0); |
| 751 | break; |
| 752 | |
| 753 | default: |
| 754 | return -EINVAL; |
| 755 | } |
| 756 | |
| 757 | err = dmaengine_slave_config(dma->chan, &config); |
| 758 | if (err) |
| 759 | return err; |
| 760 | |
| 761 | desc = dmaengine_prep_slave_sg(dma->chan, dma->sg, dma->sg_len, dir, |
| 762 | DMA_PREP_INTERRUPT | DMA_CTRL_ACK); |
| 763 | if (!desc) |
| 764 | return -ENOMEM; |
| 765 | |
| 766 | desc->callback = callback; |
| 767 | desc->callback_param = dd; |
| 768 | dmaengine_submit(desc); |
| 769 | dma_async_issue_pending(dma->chan); |
| 770 | |
| 771 | return 0; |
| 772 | } |
| 773 | |
| 774 | static void atmel_aes_dma_transfer_stop(struct atmel_aes_dev *dd, |
| 775 | enum dma_transfer_direction dir) |
| 776 | { |
| 777 | struct atmel_aes_dma *dma; |
| 778 | |
| 779 | switch (dir) { |
| 780 | case DMA_MEM_TO_DEV: |
| 781 | dma = &dd->src; |
| 782 | break; |
| 783 | |
| 784 | case DMA_DEV_TO_MEM: |
| 785 | dma = &dd->dst; |
| 786 | break; |
| 787 | |
| 788 | default: |
| 789 | return; |
| 790 | } |
| 791 | |
| 792 | dmaengine_terminate_all(dma->chan); |
| 793 | } |
| 794 | |
| 795 | static int atmel_aes_dma_start(struct atmel_aes_dev *dd, |
| 796 | struct scatterlist *src, |
| 797 | struct scatterlist *dst, |
| 798 | size_t len, |
| 799 | atmel_aes_fn_t resume) |
| 800 | { |
Cyrille Pitchen | 77dacf5 | 2015-12-17 17:48:41 +0100 | [diff] [blame] | 801 | enum dma_slave_buswidth addr_width; |
| 802 | u32 maxburst; |
Cyrille Pitchen | bbe628e | 2015-12-17 18:13:00 +0100 | [diff] [blame] | 803 | int err; |
Cyrille Pitchen | 77dacf5 | 2015-12-17 17:48:41 +0100 | [diff] [blame] | 804 | |
| 805 | switch (dd->ctx->block_size) { |
| 806 | case CFB8_BLOCK_SIZE: |
| 807 | addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE; |
| 808 | maxburst = 1; |
| 809 | break; |
| 810 | |
| 811 | case CFB16_BLOCK_SIZE: |
| 812 | addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES; |
| 813 | maxburst = 1; |
| 814 | break; |
| 815 | |
| 816 | case CFB32_BLOCK_SIZE: |
| 817 | case CFB64_BLOCK_SIZE: |
| 818 | addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES; |
| 819 | maxburst = 1; |
| 820 | break; |
| 821 | |
| 822 | case AES_BLOCK_SIZE: |
| 823 | addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES; |
| 824 | maxburst = dd->caps.max_burst_size; |
| 825 | break; |
| 826 | |
| 827 | default: |
Cyrille Pitchen | bbe628e | 2015-12-17 18:13:00 +0100 | [diff] [blame] | 828 | err = -EINVAL; |
| 829 | goto exit; |
Cyrille Pitchen | 77dacf5 | 2015-12-17 17:48:41 +0100 | [diff] [blame] | 830 | } |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 831 | |
Cyrille Pitchen | bbe628e | 2015-12-17 18:13:00 +0100 | [diff] [blame] | 832 | err = atmel_aes_map(dd, src, dst, len); |
| 833 | if (err) |
| 834 | goto exit; |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 835 | |
Cyrille Pitchen | bbe628e | 2015-12-17 18:13:00 +0100 | [diff] [blame] | 836 | dd->resume = resume; |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 837 | |
Cyrille Pitchen | bbe628e | 2015-12-17 18:13:00 +0100 | [diff] [blame] | 838 | /* Set output DMA transfer first */ |
| 839 | err = atmel_aes_dma_transfer_start(dd, addr_width, DMA_DEV_TO_MEM, |
| 840 | maxburst); |
| 841 | if (err) |
| 842 | goto unmap; |
Nicolas Royer | cadc4ab | 2013-02-20 17:10:24 +0100 | [diff] [blame] | 843 | |
Cyrille Pitchen | bbe628e | 2015-12-17 18:13:00 +0100 | [diff] [blame] | 844 | /* Then set input DMA transfer */ |
| 845 | err = atmel_aes_dma_transfer_start(dd, addr_width, DMA_MEM_TO_DEV, |
| 846 | maxburst); |
| 847 | if (err) |
| 848 | goto output_transfer_stop; |
Nicolas Royer | cadc4ab | 2013-02-20 17:10:24 +0100 | [diff] [blame] | 849 | |
Cyrille Pitchen | 10f12c1 | 2015-12-17 17:48:42 +0100 | [diff] [blame] | 850 | return -EINPROGRESS; |
Cyrille Pitchen | bbe628e | 2015-12-17 18:13:00 +0100 | [diff] [blame] | 851 | |
| 852 | output_transfer_stop: |
| 853 | atmel_aes_dma_transfer_stop(dd, DMA_DEV_TO_MEM); |
| 854 | unmap: |
| 855 | atmel_aes_unmap(dd); |
| 856 | exit: |
| 857 | return atmel_aes_complete(dd, err); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 858 | } |
| 859 | |
Cyrille Pitchen | bbe628e | 2015-12-17 18:13:00 +0100 | [diff] [blame] | 860 | static void atmel_aes_dma_stop(struct atmel_aes_dev *dd) |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 861 | { |
Cyrille Pitchen | bbe628e | 2015-12-17 18:13:00 +0100 | [diff] [blame] | 862 | atmel_aes_dma_transfer_stop(dd, DMA_MEM_TO_DEV); |
| 863 | atmel_aes_dma_transfer_stop(dd, DMA_DEV_TO_MEM); |
| 864 | atmel_aes_unmap(dd); |
| 865 | } |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 866 | |
Cyrille Pitchen | bbe628e | 2015-12-17 18:13:00 +0100 | [diff] [blame] | 867 | static void atmel_aes_dma_callback(void *data) |
| 868 | { |
| 869 | struct atmel_aes_dev *dd = data; |
Nicolas Royer | cadc4ab | 2013-02-20 17:10:24 +0100 | [diff] [blame] | 870 | |
Cyrille Pitchen | bbe628e | 2015-12-17 18:13:00 +0100 | [diff] [blame] | 871 | atmel_aes_dma_stop(dd); |
| 872 | dd->is_async = true; |
| 873 | (void)dd->resume(dd); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 874 | } |
| 875 | |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 876 | static int atmel_aes_handle_queue(struct atmel_aes_dev *dd, |
Cyrille Pitchen | ccbf729 | 2015-12-17 17:48:39 +0100 | [diff] [blame] | 877 | struct crypto_async_request *new_areq) |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 878 | { |
Cyrille Pitchen | ccbf729 | 2015-12-17 17:48:39 +0100 | [diff] [blame] | 879 | struct crypto_async_request *areq, *backlog; |
| 880 | struct atmel_aes_base_ctx *ctx; |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 881 | unsigned long flags; |
Cyrille Pitchen | a1f613f | 2017-01-26 17:07:55 +0100 | [diff] [blame^] | 882 | bool start_async; |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 883 | int err, ret = 0; |
| 884 | |
| 885 | spin_lock_irqsave(&dd->lock, flags); |
Cyrille Pitchen | ccbf729 | 2015-12-17 17:48:39 +0100 | [diff] [blame] | 886 | if (new_areq) |
| 887 | ret = crypto_enqueue_request(&dd->queue, new_areq); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 888 | if (dd->flags & AES_FLAGS_BUSY) { |
| 889 | spin_unlock_irqrestore(&dd->lock, flags); |
| 890 | return ret; |
| 891 | } |
| 892 | backlog = crypto_get_backlog(&dd->queue); |
Cyrille Pitchen | ccbf729 | 2015-12-17 17:48:39 +0100 | [diff] [blame] | 893 | areq = crypto_dequeue_request(&dd->queue); |
| 894 | if (areq) |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 895 | dd->flags |= AES_FLAGS_BUSY; |
| 896 | spin_unlock_irqrestore(&dd->lock, flags); |
| 897 | |
Cyrille Pitchen | ccbf729 | 2015-12-17 17:48:39 +0100 | [diff] [blame] | 898 | if (!areq) |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 899 | return ret; |
| 900 | |
| 901 | if (backlog) |
| 902 | backlog->complete(backlog, -EINPROGRESS); |
| 903 | |
Cyrille Pitchen | ccbf729 | 2015-12-17 17:48:39 +0100 | [diff] [blame] | 904 | ctx = crypto_tfm_ctx(areq->tfm); |
| 905 | |
| 906 | dd->areq = areq; |
| 907 | dd->ctx = ctx; |
Cyrille Pitchen | a1f613f | 2017-01-26 17:07:55 +0100 | [diff] [blame^] | 908 | start_async = (areq != new_areq); |
| 909 | dd->is_async = start_async; |
Cyrille Pitchen | ccbf729 | 2015-12-17 17:48:39 +0100 | [diff] [blame] | 910 | |
Cyrille Pitchen | a1f613f | 2017-01-26 17:07:55 +0100 | [diff] [blame^] | 911 | /* WARNING: ctx->start() MAY change dd->is_async. */ |
Cyrille Pitchen | ccbf729 | 2015-12-17 17:48:39 +0100 | [diff] [blame] | 912 | err = ctx->start(dd); |
Cyrille Pitchen | a1f613f | 2017-01-26 17:07:55 +0100 | [diff] [blame^] | 913 | return (start_async) ? ret : err; |
Cyrille Pitchen | ccbf729 | 2015-12-17 17:48:39 +0100 | [diff] [blame] | 914 | } |
| 915 | |
Cyrille Pitchen | e37a7e5 | 2015-12-17 18:13:03 +0100 | [diff] [blame] | 916 | |
| 917 | /* AES async block ciphers */ |
| 918 | |
Cyrille Pitchen | bbe628e | 2015-12-17 18:13:00 +0100 | [diff] [blame] | 919 | static int atmel_aes_transfer_complete(struct atmel_aes_dev *dd) |
| 920 | { |
| 921 | return atmel_aes_complete(dd, 0); |
| 922 | } |
| 923 | |
Cyrille Pitchen | ccbf729 | 2015-12-17 17:48:39 +0100 | [diff] [blame] | 924 | static int atmel_aes_start(struct atmel_aes_dev *dd) |
| 925 | { |
| 926 | struct ablkcipher_request *req = ablkcipher_request_cast(dd->areq); |
Cyrille Pitchen | bbe628e | 2015-12-17 18:13:00 +0100 | [diff] [blame] | 927 | struct atmel_aes_reqctx *rctx = ablkcipher_request_ctx(req); |
| 928 | bool use_dma = (req->nbytes >= ATMEL_AES_DMA_THRESHOLD || |
| 929 | dd->ctx->block_size != AES_BLOCK_SIZE); |
Cyrille Pitchen | ccbf729 | 2015-12-17 17:48:39 +0100 | [diff] [blame] | 930 | int err; |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 931 | |
Cyrille Pitchen | 77dacf5 | 2015-12-17 17:48:41 +0100 | [diff] [blame] | 932 | atmel_aes_set_mode(dd, rctx); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 933 | |
Cyrille Pitchen | cdfab4a | 2015-12-17 17:48:38 +0100 | [diff] [blame] | 934 | err = atmel_aes_hw_init(dd); |
Cyrille Pitchen | bbe628e | 2015-12-17 18:13:00 +0100 | [diff] [blame] | 935 | if (err) |
Cyrille Pitchen | 10f12c1 | 2015-12-17 17:48:42 +0100 | [diff] [blame] | 936 | return atmel_aes_complete(dd, err); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 937 | |
Cyrille Pitchen | bbe628e | 2015-12-17 18:13:00 +0100 | [diff] [blame] | 938 | atmel_aes_write_ctrl(dd, use_dma, req->info); |
| 939 | if (use_dma) |
| 940 | return atmel_aes_dma_start(dd, req->src, req->dst, req->nbytes, |
| 941 | atmel_aes_transfer_complete); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 942 | |
Cyrille Pitchen | bbe628e | 2015-12-17 18:13:00 +0100 | [diff] [blame] | 943 | return atmel_aes_cpu_start(dd, req->src, req->dst, req->nbytes, |
| 944 | atmel_aes_transfer_complete); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 945 | } |
| 946 | |
Cyrille Pitchen | fcac836 | 2015-12-17 18:13:05 +0100 | [diff] [blame] | 947 | static inline struct atmel_aes_ctr_ctx * |
| 948 | atmel_aes_ctr_ctx_cast(struct atmel_aes_base_ctx *ctx) |
| 949 | { |
| 950 | return container_of(ctx, struct atmel_aes_ctr_ctx, base); |
| 951 | } |
| 952 | |
| 953 | static int atmel_aes_ctr_transfer(struct atmel_aes_dev *dd) |
| 954 | { |
| 955 | struct atmel_aes_ctr_ctx *ctx = atmel_aes_ctr_ctx_cast(dd->ctx); |
| 956 | struct ablkcipher_request *req = ablkcipher_request_cast(dd->areq); |
| 957 | struct scatterlist *src, *dst; |
| 958 | u32 ctr, blocks; |
| 959 | size_t datalen; |
| 960 | bool use_dma, fragmented = false; |
| 961 | |
| 962 | /* Check for transfer completion. */ |
| 963 | ctx->offset += dd->total; |
| 964 | if (ctx->offset >= req->nbytes) |
| 965 | return atmel_aes_transfer_complete(dd); |
| 966 | |
| 967 | /* Compute data length. */ |
| 968 | datalen = req->nbytes - ctx->offset; |
| 969 | blocks = DIV_ROUND_UP(datalen, AES_BLOCK_SIZE); |
| 970 | ctr = be32_to_cpu(ctx->iv[3]); |
| 971 | if (dd->caps.has_ctr32) { |
| 972 | /* Check 32bit counter overflow. */ |
| 973 | u32 start = ctr; |
| 974 | u32 end = start + blocks - 1; |
| 975 | |
| 976 | if (end < start) { |
| 977 | ctr |= 0xffffffff; |
| 978 | datalen = AES_BLOCK_SIZE * -start; |
| 979 | fragmented = true; |
| 980 | } |
| 981 | } else { |
| 982 | /* Check 16bit counter overflow. */ |
| 983 | u16 start = ctr & 0xffff; |
| 984 | u16 end = start + (u16)blocks - 1; |
| 985 | |
| 986 | if (blocks >> 16 || end < start) { |
| 987 | ctr |= 0xffff; |
| 988 | datalen = AES_BLOCK_SIZE * (0x10000-start); |
| 989 | fragmented = true; |
| 990 | } |
| 991 | } |
| 992 | use_dma = (datalen >= ATMEL_AES_DMA_THRESHOLD); |
| 993 | |
| 994 | /* Jump to offset. */ |
| 995 | src = scatterwalk_ffwd(ctx->src, req->src, ctx->offset); |
| 996 | dst = ((req->src == req->dst) ? src : |
| 997 | scatterwalk_ffwd(ctx->dst, req->dst, ctx->offset)); |
| 998 | |
| 999 | /* Configure hardware. */ |
| 1000 | atmel_aes_write_ctrl(dd, use_dma, ctx->iv); |
| 1001 | if (unlikely(fragmented)) { |
| 1002 | /* |
| 1003 | * Increment the counter manually to cope with the hardware |
| 1004 | * counter overflow. |
| 1005 | */ |
| 1006 | ctx->iv[3] = cpu_to_be32(ctr); |
| 1007 | crypto_inc((u8 *)ctx->iv, AES_BLOCK_SIZE); |
| 1008 | } |
| 1009 | |
| 1010 | if (use_dma) |
| 1011 | return atmel_aes_dma_start(dd, src, dst, datalen, |
| 1012 | atmel_aes_ctr_transfer); |
| 1013 | |
| 1014 | return atmel_aes_cpu_start(dd, src, dst, datalen, |
| 1015 | atmel_aes_ctr_transfer); |
| 1016 | } |
| 1017 | |
| 1018 | static int atmel_aes_ctr_start(struct atmel_aes_dev *dd) |
| 1019 | { |
| 1020 | struct atmel_aes_ctr_ctx *ctx = atmel_aes_ctr_ctx_cast(dd->ctx); |
| 1021 | struct ablkcipher_request *req = ablkcipher_request_cast(dd->areq); |
| 1022 | struct atmel_aes_reqctx *rctx = ablkcipher_request_ctx(req); |
| 1023 | int err; |
| 1024 | |
| 1025 | atmel_aes_set_mode(dd, rctx); |
| 1026 | |
| 1027 | err = atmel_aes_hw_init(dd); |
| 1028 | if (err) |
| 1029 | return atmel_aes_complete(dd, err); |
| 1030 | |
| 1031 | memcpy(ctx->iv, req->info, AES_BLOCK_SIZE); |
| 1032 | ctx->offset = 0; |
| 1033 | dd->total = 0; |
| 1034 | return atmel_aes_ctr_transfer(dd); |
| 1035 | } |
| 1036 | |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1037 | static int atmel_aes_crypt(struct ablkcipher_request *req, unsigned long mode) |
| 1038 | { |
Cyrille Pitchen | afbac17 | 2015-12-17 18:13:02 +0100 | [diff] [blame] | 1039 | struct atmel_aes_base_ctx *ctx; |
| 1040 | struct atmel_aes_reqctx *rctx; |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1041 | struct atmel_aes_dev *dd; |
| 1042 | |
Cyrille Pitchen | afbac17 | 2015-12-17 18:13:02 +0100 | [diff] [blame] | 1043 | ctx = crypto_ablkcipher_ctx(crypto_ablkcipher_reqtfm(req)); |
Cyrille Pitchen | 77dacf5 | 2015-12-17 17:48:41 +0100 | [diff] [blame] | 1044 | switch (mode & AES_FLAGS_OPMODE_MASK) { |
| 1045 | case AES_FLAGS_CFB8: |
Nicolas Royer | cadc4ab | 2013-02-20 17:10:24 +0100 | [diff] [blame] | 1046 | ctx->block_size = CFB8_BLOCK_SIZE; |
Cyrille Pitchen | 77dacf5 | 2015-12-17 17:48:41 +0100 | [diff] [blame] | 1047 | break; |
| 1048 | |
| 1049 | case AES_FLAGS_CFB16: |
Nicolas Royer | cadc4ab | 2013-02-20 17:10:24 +0100 | [diff] [blame] | 1050 | ctx->block_size = CFB16_BLOCK_SIZE; |
Cyrille Pitchen | 77dacf5 | 2015-12-17 17:48:41 +0100 | [diff] [blame] | 1051 | break; |
| 1052 | |
| 1053 | case AES_FLAGS_CFB32: |
Nicolas Royer | cadc4ab | 2013-02-20 17:10:24 +0100 | [diff] [blame] | 1054 | ctx->block_size = CFB32_BLOCK_SIZE; |
Cyrille Pitchen | 77dacf5 | 2015-12-17 17:48:41 +0100 | [diff] [blame] | 1055 | break; |
| 1056 | |
| 1057 | case AES_FLAGS_CFB64: |
Leilei Zhao | 9f84951 | 2014-04-22 15:23:24 +0800 | [diff] [blame] | 1058 | ctx->block_size = CFB64_BLOCK_SIZE; |
Cyrille Pitchen | 77dacf5 | 2015-12-17 17:48:41 +0100 | [diff] [blame] | 1059 | break; |
| 1060 | |
| 1061 | default: |
Nicolas Royer | cadc4ab | 2013-02-20 17:10:24 +0100 | [diff] [blame] | 1062 | ctx->block_size = AES_BLOCK_SIZE; |
Cyrille Pitchen | 77dacf5 | 2015-12-17 17:48:41 +0100 | [diff] [blame] | 1063 | break; |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1064 | } |
| 1065 | |
| 1066 | dd = atmel_aes_find_dev(ctx); |
| 1067 | if (!dd) |
| 1068 | return -ENODEV; |
| 1069 | |
Cyrille Pitchen | afbac17 | 2015-12-17 18:13:02 +0100 | [diff] [blame] | 1070 | rctx = ablkcipher_request_ctx(req); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1071 | rctx->mode = mode; |
| 1072 | |
Cyrille Pitchen | ccbf729 | 2015-12-17 17:48:39 +0100 | [diff] [blame] | 1073 | return atmel_aes_handle_queue(dd, &req->base); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1074 | } |
| 1075 | |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1076 | static int atmel_aes_setkey(struct crypto_ablkcipher *tfm, const u8 *key, |
| 1077 | unsigned int keylen) |
| 1078 | { |
Cyrille Pitchen | ccbf729 | 2015-12-17 17:48:39 +0100 | [diff] [blame] | 1079 | struct atmel_aes_base_ctx *ctx = crypto_ablkcipher_ctx(tfm); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1080 | |
Cyrille Pitchen | afbac17 | 2015-12-17 18:13:02 +0100 | [diff] [blame] | 1081 | if (keylen != AES_KEYSIZE_128 && |
| 1082 | keylen != AES_KEYSIZE_192 && |
| 1083 | keylen != AES_KEYSIZE_256) { |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1084 | crypto_ablkcipher_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN); |
| 1085 | return -EINVAL; |
| 1086 | } |
| 1087 | |
| 1088 | memcpy(ctx->key, key, keylen); |
| 1089 | ctx->keylen = keylen; |
| 1090 | |
| 1091 | return 0; |
| 1092 | } |
| 1093 | |
| 1094 | static int atmel_aes_ecb_encrypt(struct ablkcipher_request *req) |
| 1095 | { |
Cyrille Pitchen | 77dacf5 | 2015-12-17 17:48:41 +0100 | [diff] [blame] | 1096 | return atmel_aes_crypt(req, AES_FLAGS_ECB | AES_FLAGS_ENCRYPT); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1097 | } |
| 1098 | |
| 1099 | static int atmel_aes_ecb_decrypt(struct ablkcipher_request *req) |
| 1100 | { |
Cyrille Pitchen | 77dacf5 | 2015-12-17 17:48:41 +0100 | [diff] [blame] | 1101 | return atmel_aes_crypt(req, AES_FLAGS_ECB); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1102 | } |
| 1103 | |
| 1104 | static int atmel_aes_cbc_encrypt(struct ablkcipher_request *req) |
| 1105 | { |
Cyrille Pitchen | afbac17 | 2015-12-17 18:13:02 +0100 | [diff] [blame] | 1106 | return atmel_aes_crypt(req, AES_FLAGS_CBC | AES_FLAGS_ENCRYPT); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1107 | } |
| 1108 | |
| 1109 | static int atmel_aes_cbc_decrypt(struct ablkcipher_request *req) |
| 1110 | { |
Cyrille Pitchen | afbac17 | 2015-12-17 18:13:02 +0100 | [diff] [blame] | 1111 | return atmel_aes_crypt(req, AES_FLAGS_CBC); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1112 | } |
| 1113 | |
| 1114 | static int atmel_aes_ofb_encrypt(struct ablkcipher_request *req) |
| 1115 | { |
Cyrille Pitchen | afbac17 | 2015-12-17 18:13:02 +0100 | [diff] [blame] | 1116 | return atmel_aes_crypt(req, AES_FLAGS_OFB | AES_FLAGS_ENCRYPT); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1117 | } |
| 1118 | |
| 1119 | static int atmel_aes_ofb_decrypt(struct ablkcipher_request *req) |
| 1120 | { |
Cyrille Pitchen | afbac17 | 2015-12-17 18:13:02 +0100 | [diff] [blame] | 1121 | return atmel_aes_crypt(req, AES_FLAGS_OFB); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1122 | } |
| 1123 | |
| 1124 | static int atmel_aes_cfb_encrypt(struct ablkcipher_request *req) |
| 1125 | { |
Cyrille Pitchen | 77dacf5 | 2015-12-17 17:48:41 +0100 | [diff] [blame] | 1126 | return atmel_aes_crypt(req, AES_FLAGS_CFB128 | AES_FLAGS_ENCRYPT); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1127 | } |
| 1128 | |
| 1129 | static int atmel_aes_cfb_decrypt(struct ablkcipher_request *req) |
| 1130 | { |
Cyrille Pitchen | 77dacf5 | 2015-12-17 17:48:41 +0100 | [diff] [blame] | 1131 | return atmel_aes_crypt(req, AES_FLAGS_CFB128); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1132 | } |
| 1133 | |
| 1134 | static int atmel_aes_cfb64_encrypt(struct ablkcipher_request *req) |
| 1135 | { |
Cyrille Pitchen | 77dacf5 | 2015-12-17 17:48:41 +0100 | [diff] [blame] | 1136 | return atmel_aes_crypt(req, AES_FLAGS_CFB64 | AES_FLAGS_ENCRYPT); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1137 | } |
| 1138 | |
| 1139 | static int atmel_aes_cfb64_decrypt(struct ablkcipher_request *req) |
| 1140 | { |
Cyrille Pitchen | 77dacf5 | 2015-12-17 17:48:41 +0100 | [diff] [blame] | 1141 | return atmel_aes_crypt(req, AES_FLAGS_CFB64); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1142 | } |
| 1143 | |
| 1144 | static int atmel_aes_cfb32_encrypt(struct ablkcipher_request *req) |
| 1145 | { |
Cyrille Pitchen | 77dacf5 | 2015-12-17 17:48:41 +0100 | [diff] [blame] | 1146 | return atmel_aes_crypt(req, AES_FLAGS_CFB32 | AES_FLAGS_ENCRYPT); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1147 | } |
| 1148 | |
| 1149 | static int atmel_aes_cfb32_decrypt(struct ablkcipher_request *req) |
| 1150 | { |
Cyrille Pitchen | 77dacf5 | 2015-12-17 17:48:41 +0100 | [diff] [blame] | 1151 | return atmel_aes_crypt(req, AES_FLAGS_CFB32); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1152 | } |
| 1153 | |
| 1154 | static int atmel_aes_cfb16_encrypt(struct ablkcipher_request *req) |
| 1155 | { |
Cyrille Pitchen | 77dacf5 | 2015-12-17 17:48:41 +0100 | [diff] [blame] | 1156 | return atmel_aes_crypt(req, AES_FLAGS_CFB16 | AES_FLAGS_ENCRYPT); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1157 | } |
| 1158 | |
| 1159 | static int atmel_aes_cfb16_decrypt(struct ablkcipher_request *req) |
| 1160 | { |
Cyrille Pitchen | 77dacf5 | 2015-12-17 17:48:41 +0100 | [diff] [blame] | 1161 | return atmel_aes_crypt(req, AES_FLAGS_CFB16); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1162 | } |
| 1163 | |
| 1164 | static int atmel_aes_cfb8_encrypt(struct ablkcipher_request *req) |
| 1165 | { |
Cyrille Pitchen | 77dacf5 | 2015-12-17 17:48:41 +0100 | [diff] [blame] | 1166 | return atmel_aes_crypt(req, AES_FLAGS_CFB8 | AES_FLAGS_ENCRYPT); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1167 | } |
| 1168 | |
| 1169 | static int atmel_aes_cfb8_decrypt(struct ablkcipher_request *req) |
| 1170 | { |
Cyrille Pitchen | 77dacf5 | 2015-12-17 17:48:41 +0100 | [diff] [blame] | 1171 | return atmel_aes_crypt(req, AES_FLAGS_CFB8); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1172 | } |
| 1173 | |
| 1174 | static int atmel_aes_ctr_encrypt(struct ablkcipher_request *req) |
| 1175 | { |
Cyrille Pitchen | afbac17 | 2015-12-17 18:13:02 +0100 | [diff] [blame] | 1176 | return atmel_aes_crypt(req, AES_FLAGS_CTR | AES_FLAGS_ENCRYPT); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1177 | } |
| 1178 | |
| 1179 | static int atmel_aes_ctr_decrypt(struct ablkcipher_request *req) |
| 1180 | { |
Cyrille Pitchen | afbac17 | 2015-12-17 18:13:02 +0100 | [diff] [blame] | 1181 | return atmel_aes_crypt(req, AES_FLAGS_CTR); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1182 | } |
| 1183 | |
| 1184 | static int atmel_aes_cra_init(struct crypto_tfm *tfm) |
| 1185 | { |
Cyrille Pitchen | ccbf729 | 2015-12-17 17:48:39 +0100 | [diff] [blame] | 1186 | struct atmel_aes_ctx *ctx = crypto_tfm_ctx(tfm); |
| 1187 | |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1188 | tfm->crt_ablkcipher.reqsize = sizeof(struct atmel_aes_reqctx); |
Cyrille Pitchen | ccbf729 | 2015-12-17 17:48:39 +0100 | [diff] [blame] | 1189 | ctx->base.start = atmel_aes_start; |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1190 | |
| 1191 | return 0; |
| 1192 | } |
| 1193 | |
Cyrille Pitchen | fcac836 | 2015-12-17 18:13:05 +0100 | [diff] [blame] | 1194 | static int atmel_aes_ctr_cra_init(struct crypto_tfm *tfm) |
| 1195 | { |
| 1196 | struct atmel_aes_ctx *ctx = crypto_tfm_ctx(tfm); |
| 1197 | |
| 1198 | tfm->crt_ablkcipher.reqsize = sizeof(struct atmel_aes_reqctx); |
| 1199 | ctx->base.start = atmel_aes_ctr_start; |
| 1200 | |
| 1201 | return 0; |
| 1202 | } |
| 1203 | |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1204 | static void atmel_aes_cra_exit(struct crypto_tfm *tfm) |
| 1205 | { |
| 1206 | } |
| 1207 | |
| 1208 | static struct crypto_alg aes_algs[] = { |
| 1209 | { |
| 1210 | .cra_name = "ecb(aes)", |
| 1211 | .cra_driver_name = "atmel-ecb-aes", |
Cyrille Pitchen | 88efd9a | 2015-12-17 17:48:34 +0100 | [diff] [blame] | 1212 | .cra_priority = ATMEL_AES_PRIORITY, |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1213 | .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC, |
| 1214 | .cra_blocksize = AES_BLOCK_SIZE, |
| 1215 | .cra_ctxsize = sizeof(struct atmel_aes_ctx), |
Nicolas Royer | cadc4ab | 2013-02-20 17:10:24 +0100 | [diff] [blame] | 1216 | .cra_alignmask = 0xf, |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1217 | .cra_type = &crypto_ablkcipher_type, |
| 1218 | .cra_module = THIS_MODULE, |
| 1219 | .cra_init = atmel_aes_cra_init, |
| 1220 | .cra_exit = atmel_aes_cra_exit, |
| 1221 | .cra_u.ablkcipher = { |
| 1222 | .min_keysize = AES_MIN_KEY_SIZE, |
| 1223 | .max_keysize = AES_MAX_KEY_SIZE, |
| 1224 | .setkey = atmel_aes_setkey, |
| 1225 | .encrypt = atmel_aes_ecb_encrypt, |
| 1226 | .decrypt = atmel_aes_ecb_decrypt, |
| 1227 | } |
| 1228 | }, |
| 1229 | { |
| 1230 | .cra_name = "cbc(aes)", |
| 1231 | .cra_driver_name = "atmel-cbc-aes", |
Cyrille Pitchen | 88efd9a | 2015-12-17 17:48:34 +0100 | [diff] [blame] | 1232 | .cra_priority = ATMEL_AES_PRIORITY, |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1233 | .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC, |
| 1234 | .cra_blocksize = AES_BLOCK_SIZE, |
| 1235 | .cra_ctxsize = sizeof(struct atmel_aes_ctx), |
Nicolas Royer | cadc4ab | 2013-02-20 17:10:24 +0100 | [diff] [blame] | 1236 | .cra_alignmask = 0xf, |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1237 | .cra_type = &crypto_ablkcipher_type, |
| 1238 | .cra_module = THIS_MODULE, |
| 1239 | .cra_init = atmel_aes_cra_init, |
| 1240 | .cra_exit = atmel_aes_cra_exit, |
| 1241 | .cra_u.ablkcipher = { |
| 1242 | .min_keysize = AES_MIN_KEY_SIZE, |
| 1243 | .max_keysize = AES_MAX_KEY_SIZE, |
| 1244 | .ivsize = AES_BLOCK_SIZE, |
| 1245 | .setkey = atmel_aes_setkey, |
| 1246 | .encrypt = atmel_aes_cbc_encrypt, |
| 1247 | .decrypt = atmel_aes_cbc_decrypt, |
| 1248 | } |
| 1249 | }, |
| 1250 | { |
| 1251 | .cra_name = "ofb(aes)", |
| 1252 | .cra_driver_name = "atmel-ofb-aes", |
Cyrille Pitchen | 88efd9a | 2015-12-17 17:48:34 +0100 | [diff] [blame] | 1253 | .cra_priority = ATMEL_AES_PRIORITY, |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1254 | .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC, |
| 1255 | .cra_blocksize = AES_BLOCK_SIZE, |
| 1256 | .cra_ctxsize = sizeof(struct atmel_aes_ctx), |
Nicolas Royer | cadc4ab | 2013-02-20 17:10:24 +0100 | [diff] [blame] | 1257 | .cra_alignmask = 0xf, |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1258 | .cra_type = &crypto_ablkcipher_type, |
| 1259 | .cra_module = THIS_MODULE, |
| 1260 | .cra_init = atmel_aes_cra_init, |
| 1261 | .cra_exit = atmel_aes_cra_exit, |
| 1262 | .cra_u.ablkcipher = { |
| 1263 | .min_keysize = AES_MIN_KEY_SIZE, |
| 1264 | .max_keysize = AES_MAX_KEY_SIZE, |
| 1265 | .ivsize = AES_BLOCK_SIZE, |
| 1266 | .setkey = atmel_aes_setkey, |
| 1267 | .encrypt = atmel_aes_ofb_encrypt, |
| 1268 | .decrypt = atmel_aes_ofb_decrypt, |
| 1269 | } |
| 1270 | }, |
| 1271 | { |
| 1272 | .cra_name = "cfb(aes)", |
| 1273 | .cra_driver_name = "atmel-cfb-aes", |
Cyrille Pitchen | 88efd9a | 2015-12-17 17:48:34 +0100 | [diff] [blame] | 1274 | .cra_priority = ATMEL_AES_PRIORITY, |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1275 | .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC, |
| 1276 | .cra_blocksize = AES_BLOCK_SIZE, |
| 1277 | .cra_ctxsize = sizeof(struct atmel_aes_ctx), |
Nicolas Royer | cadc4ab | 2013-02-20 17:10:24 +0100 | [diff] [blame] | 1278 | .cra_alignmask = 0xf, |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1279 | .cra_type = &crypto_ablkcipher_type, |
| 1280 | .cra_module = THIS_MODULE, |
| 1281 | .cra_init = atmel_aes_cra_init, |
| 1282 | .cra_exit = atmel_aes_cra_exit, |
| 1283 | .cra_u.ablkcipher = { |
| 1284 | .min_keysize = AES_MIN_KEY_SIZE, |
| 1285 | .max_keysize = AES_MAX_KEY_SIZE, |
| 1286 | .ivsize = AES_BLOCK_SIZE, |
| 1287 | .setkey = atmel_aes_setkey, |
| 1288 | .encrypt = atmel_aes_cfb_encrypt, |
| 1289 | .decrypt = atmel_aes_cfb_decrypt, |
| 1290 | } |
| 1291 | }, |
| 1292 | { |
| 1293 | .cra_name = "cfb32(aes)", |
| 1294 | .cra_driver_name = "atmel-cfb32-aes", |
Cyrille Pitchen | 88efd9a | 2015-12-17 17:48:34 +0100 | [diff] [blame] | 1295 | .cra_priority = ATMEL_AES_PRIORITY, |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1296 | .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC, |
| 1297 | .cra_blocksize = CFB32_BLOCK_SIZE, |
| 1298 | .cra_ctxsize = sizeof(struct atmel_aes_ctx), |
Nicolas Royer | cadc4ab | 2013-02-20 17:10:24 +0100 | [diff] [blame] | 1299 | .cra_alignmask = 0x3, |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1300 | .cra_type = &crypto_ablkcipher_type, |
| 1301 | .cra_module = THIS_MODULE, |
| 1302 | .cra_init = atmel_aes_cra_init, |
| 1303 | .cra_exit = atmel_aes_cra_exit, |
| 1304 | .cra_u.ablkcipher = { |
| 1305 | .min_keysize = AES_MIN_KEY_SIZE, |
| 1306 | .max_keysize = AES_MAX_KEY_SIZE, |
| 1307 | .ivsize = AES_BLOCK_SIZE, |
| 1308 | .setkey = atmel_aes_setkey, |
| 1309 | .encrypt = atmel_aes_cfb32_encrypt, |
| 1310 | .decrypt = atmel_aes_cfb32_decrypt, |
| 1311 | } |
| 1312 | }, |
| 1313 | { |
| 1314 | .cra_name = "cfb16(aes)", |
| 1315 | .cra_driver_name = "atmel-cfb16-aes", |
Cyrille Pitchen | 88efd9a | 2015-12-17 17:48:34 +0100 | [diff] [blame] | 1316 | .cra_priority = ATMEL_AES_PRIORITY, |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1317 | .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC, |
| 1318 | .cra_blocksize = CFB16_BLOCK_SIZE, |
| 1319 | .cra_ctxsize = sizeof(struct atmel_aes_ctx), |
Nicolas Royer | cadc4ab | 2013-02-20 17:10:24 +0100 | [diff] [blame] | 1320 | .cra_alignmask = 0x1, |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1321 | .cra_type = &crypto_ablkcipher_type, |
| 1322 | .cra_module = THIS_MODULE, |
| 1323 | .cra_init = atmel_aes_cra_init, |
| 1324 | .cra_exit = atmel_aes_cra_exit, |
| 1325 | .cra_u.ablkcipher = { |
| 1326 | .min_keysize = AES_MIN_KEY_SIZE, |
| 1327 | .max_keysize = AES_MAX_KEY_SIZE, |
| 1328 | .ivsize = AES_BLOCK_SIZE, |
| 1329 | .setkey = atmel_aes_setkey, |
| 1330 | .encrypt = atmel_aes_cfb16_encrypt, |
| 1331 | .decrypt = atmel_aes_cfb16_decrypt, |
| 1332 | } |
| 1333 | }, |
| 1334 | { |
| 1335 | .cra_name = "cfb8(aes)", |
| 1336 | .cra_driver_name = "atmel-cfb8-aes", |
Cyrille Pitchen | 88efd9a | 2015-12-17 17:48:34 +0100 | [diff] [blame] | 1337 | .cra_priority = ATMEL_AES_PRIORITY, |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1338 | .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC, |
Leilei Zhao | e5d8c96 | 2014-04-22 15:23:23 +0800 | [diff] [blame] | 1339 | .cra_blocksize = CFB8_BLOCK_SIZE, |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1340 | .cra_ctxsize = sizeof(struct atmel_aes_ctx), |
| 1341 | .cra_alignmask = 0x0, |
| 1342 | .cra_type = &crypto_ablkcipher_type, |
| 1343 | .cra_module = THIS_MODULE, |
| 1344 | .cra_init = atmel_aes_cra_init, |
| 1345 | .cra_exit = atmel_aes_cra_exit, |
| 1346 | .cra_u.ablkcipher = { |
| 1347 | .min_keysize = AES_MIN_KEY_SIZE, |
| 1348 | .max_keysize = AES_MAX_KEY_SIZE, |
| 1349 | .ivsize = AES_BLOCK_SIZE, |
| 1350 | .setkey = atmel_aes_setkey, |
| 1351 | .encrypt = atmel_aes_cfb8_encrypt, |
| 1352 | .decrypt = atmel_aes_cfb8_decrypt, |
| 1353 | } |
| 1354 | }, |
| 1355 | { |
| 1356 | .cra_name = "ctr(aes)", |
| 1357 | .cra_driver_name = "atmel-ctr-aes", |
Cyrille Pitchen | 88efd9a | 2015-12-17 17:48:34 +0100 | [diff] [blame] | 1358 | .cra_priority = ATMEL_AES_PRIORITY, |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1359 | .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC, |
Cyrille Pitchen | da7b850 | 2015-12-17 18:13:04 +0100 | [diff] [blame] | 1360 | .cra_blocksize = 1, |
Cyrille Pitchen | fcac836 | 2015-12-17 18:13:05 +0100 | [diff] [blame] | 1361 | .cra_ctxsize = sizeof(struct atmel_aes_ctr_ctx), |
Nicolas Royer | cadc4ab | 2013-02-20 17:10:24 +0100 | [diff] [blame] | 1362 | .cra_alignmask = 0xf, |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1363 | .cra_type = &crypto_ablkcipher_type, |
| 1364 | .cra_module = THIS_MODULE, |
Cyrille Pitchen | fcac836 | 2015-12-17 18:13:05 +0100 | [diff] [blame] | 1365 | .cra_init = atmel_aes_ctr_cra_init, |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1366 | .cra_exit = atmel_aes_cra_exit, |
| 1367 | .cra_u.ablkcipher = { |
| 1368 | .min_keysize = AES_MIN_KEY_SIZE, |
| 1369 | .max_keysize = AES_MAX_KEY_SIZE, |
| 1370 | .ivsize = AES_BLOCK_SIZE, |
| 1371 | .setkey = atmel_aes_setkey, |
| 1372 | .encrypt = atmel_aes_ctr_encrypt, |
| 1373 | .decrypt = atmel_aes_ctr_decrypt, |
| 1374 | } |
| 1375 | }, |
| 1376 | }; |
| 1377 | |
Nicolas Royer | cadc4ab | 2013-02-20 17:10:24 +0100 | [diff] [blame] | 1378 | static struct crypto_alg aes_cfb64_alg = { |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1379 | .cra_name = "cfb64(aes)", |
| 1380 | .cra_driver_name = "atmel-cfb64-aes", |
Cyrille Pitchen | 88efd9a | 2015-12-17 17:48:34 +0100 | [diff] [blame] | 1381 | .cra_priority = ATMEL_AES_PRIORITY, |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1382 | .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC, |
| 1383 | .cra_blocksize = CFB64_BLOCK_SIZE, |
| 1384 | .cra_ctxsize = sizeof(struct atmel_aes_ctx), |
Nicolas Royer | cadc4ab | 2013-02-20 17:10:24 +0100 | [diff] [blame] | 1385 | .cra_alignmask = 0x7, |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1386 | .cra_type = &crypto_ablkcipher_type, |
| 1387 | .cra_module = THIS_MODULE, |
| 1388 | .cra_init = atmel_aes_cra_init, |
| 1389 | .cra_exit = atmel_aes_cra_exit, |
| 1390 | .cra_u.ablkcipher = { |
| 1391 | .min_keysize = AES_MIN_KEY_SIZE, |
| 1392 | .max_keysize = AES_MAX_KEY_SIZE, |
| 1393 | .ivsize = AES_BLOCK_SIZE, |
| 1394 | .setkey = atmel_aes_setkey, |
| 1395 | .encrypt = atmel_aes_cfb64_encrypt, |
| 1396 | .decrypt = atmel_aes_cfb64_decrypt, |
| 1397 | } |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 1398 | }; |
| 1399 | |
Cyrille Pitchen | e37a7e5 | 2015-12-17 18:13:03 +0100 | [diff] [blame] | 1400 | |
Cyrille Pitchen | d441954 | 2015-12-17 18:13:07 +0100 | [diff] [blame] | 1401 | /* gcm aead functions */ |
| 1402 | |
| 1403 | static int atmel_aes_gcm_ghash(struct atmel_aes_dev *dd, |
| 1404 | const u32 *data, size_t datalen, |
| 1405 | const u32 *ghash_in, u32 *ghash_out, |
| 1406 | atmel_aes_fn_t resume); |
| 1407 | static int atmel_aes_gcm_ghash_init(struct atmel_aes_dev *dd); |
| 1408 | static int atmel_aes_gcm_ghash_finalize(struct atmel_aes_dev *dd); |
| 1409 | |
| 1410 | static int atmel_aes_gcm_start(struct atmel_aes_dev *dd); |
| 1411 | static int atmel_aes_gcm_process(struct atmel_aes_dev *dd); |
| 1412 | static int atmel_aes_gcm_length(struct atmel_aes_dev *dd); |
| 1413 | static int atmel_aes_gcm_data(struct atmel_aes_dev *dd); |
| 1414 | static int atmel_aes_gcm_tag_init(struct atmel_aes_dev *dd); |
| 1415 | static int atmel_aes_gcm_tag(struct atmel_aes_dev *dd); |
| 1416 | static int atmel_aes_gcm_finalize(struct atmel_aes_dev *dd); |
| 1417 | |
| 1418 | static inline struct atmel_aes_gcm_ctx * |
| 1419 | atmel_aes_gcm_ctx_cast(struct atmel_aes_base_ctx *ctx) |
| 1420 | { |
| 1421 | return container_of(ctx, struct atmel_aes_gcm_ctx, base); |
| 1422 | } |
| 1423 | |
| 1424 | static int atmel_aes_gcm_ghash(struct atmel_aes_dev *dd, |
| 1425 | const u32 *data, size_t datalen, |
| 1426 | const u32 *ghash_in, u32 *ghash_out, |
| 1427 | atmel_aes_fn_t resume) |
| 1428 | { |
| 1429 | struct atmel_aes_gcm_ctx *ctx = atmel_aes_gcm_ctx_cast(dd->ctx); |
| 1430 | |
| 1431 | dd->data = (u32 *)data; |
| 1432 | dd->datalen = datalen; |
| 1433 | ctx->ghash_in = ghash_in; |
| 1434 | ctx->ghash_out = ghash_out; |
| 1435 | ctx->ghash_resume = resume; |
| 1436 | |
| 1437 | atmel_aes_write_ctrl(dd, false, NULL); |
| 1438 | return atmel_aes_wait_for_data_ready(dd, atmel_aes_gcm_ghash_init); |
| 1439 | } |
| 1440 | |
| 1441 | static int atmel_aes_gcm_ghash_init(struct atmel_aes_dev *dd) |
| 1442 | { |
| 1443 | struct atmel_aes_gcm_ctx *ctx = atmel_aes_gcm_ctx_cast(dd->ctx); |
| 1444 | |
| 1445 | /* Set the data length. */ |
| 1446 | atmel_aes_write(dd, AES_AADLENR, dd->total); |
| 1447 | atmel_aes_write(dd, AES_CLENR, 0); |
| 1448 | |
| 1449 | /* If needed, overwrite the GCM Intermediate Hash Word Registers */ |
| 1450 | if (ctx->ghash_in) |
| 1451 | atmel_aes_write_block(dd, AES_GHASHR(0), ctx->ghash_in); |
| 1452 | |
| 1453 | return atmel_aes_gcm_ghash_finalize(dd); |
| 1454 | } |
| 1455 | |
| 1456 | static int atmel_aes_gcm_ghash_finalize(struct atmel_aes_dev *dd) |
| 1457 | { |
| 1458 | struct atmel_aes_gcm_ctx *ctx = atmel_aes_gcm_ctx_cast(dd->ctx); |
| 1459 | u32 isr; |
| 1460 | |
| 1461 | /* Write data into the Input Data Registers. */ |
| 1462 | while (dd->datalen > 0) { |
| 1463 | atmel_aes_write_block(dd, AES_IDATAR(0), dd->data); |
| 1464 | dd->data += 4; |
| 1465 | dd->datalen -= AES_BLOCK_SIZE; |
| 1466 | |
| 1467 | isr = atmel_aes_read(dd, AES_ISR); |
| 1468 | if (!(isr & AES_INT_DATARDY)) { |
| 1469 | dd->resume = atmel_aes_gcm_ghash_finalize; |
| 1470 | atmel_aes_write(dd, AES_IER, AES_INT_DATARDY); |
| 1471 | return -EINPROGRESS; |
| 1472 | } |
| 1473 | } |
| 1474 | |
| 1475 | /* Read the computed hash from GHASHRx. */ |
| 1476 | atmel_aes_read_block(dd, AES_GHASHR(0), ctx->ghash_out); |
| 1477 | |
| 1478 | return ctx->ghash_resume(dd); |
| 1479 | } |
| 1480 | |
| 1481 | |
| 1482 | static int atmel_aes_gcm_start(struct atmel_aes_dev *dd) |
| 1483 | { |
| 1484 | struct atmel_aes_gcm_ctx *ctx = atmel_aes_gcm_ctx_cast(dd->ctx); |
| 1485 | struct aead_request *req = aead_request_cast(dd->areq); |
| 1486 | struct crypto_aead *tfm = crypto_aead_reqtfm(req); |
| 1487 | struct atmel_aes_reqctx *rctx = aead_request_ctx(req); |
| 1488 | size_t ivsize = crypto_aead_ivsize(tfm); |
| 1489 | size_t datalen, padlen; |
| 1490 | const void *iv = req->iv; |
| 1491 | u8 *data = dd->buf; |
| 1492 | int err; |
| 1493 | |
| 1494 | atmel_aes_set_mode(dd, rctx); |
| 1495 | |
| 1496 | err = atmel_aes_hw_init(dd); |
| 1497 | if (err) |
| 1498 | return atmel_aes_complete(dd, err); |
| 1499 | |
| 1500 | if (likely(ivsize == 12)) { |
| 1501 | memcpy(ctx->j0, iv, ivsize); |
| 1502 | ctx->j0[3] = cpu_to_be32(1); |
| 1503 | return atmel_aes_gcm_process(dd); |
| 1504 | } |
| 1505 | |
| 1506 | padlen = atmel_aes_padlen(ivsize, AES_BLOCK_SIZE); |
| 1507 | datalen = ivsize + padlen + AES_BLOCK_SIZE; |
| 1508 | if (datalen > dd->buflen) |
| 1509 | return atmel_aes_complete(dd, -EINVAL); |
| 1510 | |
| 1511 | memcpy(data, iv, ivsize); |
| 1512 | memset(data + ivsize, 0, padlen + sizeof(u64)); |
| 1513 | ((u64 *)(data + datalen))[-1] = cpu_to_be64(ivsize * 8); |
| 1514 | |
| 1515 | return atmel_aes_gcm_ghash(dd, (const u32 *)data, datalen, |
| 1516 | NULL, ctx->j0, atmel_aes_gcm_process); |
| 1517 | } |
| 1518 | |
| 1519 | static int atmel_aes_gcm_process(struct atmel_aes_dev *dd) |
| 1520 | { |
| 1521 | struct atmel_aes_gcm_ctx *ctx = atmel_aes_gcm_ctx_cast(dd->ctx); |
| 1522 | struct aead_request *req = aead_request_cast(dd->areq); |
| 1523 | struct crypto_aead *tfm = crypto_aead_reqtfm(req); |
| 1524 | bool enc = atmel_aes_is_encrypt(dd); |
| 1525 | u32 authsize; |
| 1526 | |
| 1527 | /* Compute text length. */ |
| 1528 | authsize = crypto_aead_authsize(tfm); |
| 1529 | ctx->textlen = req->cryptlen - (enc ? 0 : authsize); |
| 1530 | |
| 1531 | /* |
| 1532 | * According to tcrypt test suite, the GCM Automatic Tag Generation |
| 1533 | * fails when both the message and its associated data are empty. |
| 1534 | */ |
| 1535 | if (likely(req->assoclen != 0 || ctx->textlen != 0)) |
| 1536 | dd->flags |= AES_FLAGS_GTAGEN; |
| 1537 | |
| 1538 | atmel_aes_write_ctrl(dd, false, NULL); |
| 1539 | return atmel_aes_wait_for_data_ready(dd, atmel_aes_gcm_length); |
| 1540 | } |
| 1541 | |
| 1542 | static int atmel_aes_gcm_length(struct atmel_aes_dev *dd) |
| 1543 | { |
| 1544 | struct atmel_aes_gcm_ctx *ctx = atmel_aes_gcm_ctx_cast(dd->ctx); |
| 1545 | struct aead_request *req = aead_request_cast(dd->areq); |
| 1546 | u32 j0_lsw, *j0 = ctx->j0; |
| 1547 | size_t padlen; |
| 1548 | |
| 1549 | /* Write incr32(J0) into IV. */ |
| 1550 | j0_lsw = j0[3]; |
| 1551 | j0[3] = cpu_to_be32(be32_to_cpu(j0[3]) + 1); |
| 1552 | atmel_aes_write_block(dd, AES_IVR(0), j0); |
| 1553 | j0[3] = j0_lsw; |
| 1554 | |
| 1555 | /* Set aad and text lengths. */ |
| 1556 | atmel_aes_write(dd, AES_AADLENR, req->assoclen); |
| 1557 | atmel_aes_write(dd, AES_CLENR, ctx->textlen); |
| 1558 | |
| 1559 | /* Check whether AAD are present. */ |
| 1560 | if (unlikely(req->assoclen == 0)) { |
| 1561 | dd->datalen = 0; |
| 1562 | return atmel_aes_gcm_data(dd); |
| 1563 | } |
| 1564 | |
| 1565 | /* Copy assoc data and add padding. */ |
| 1566 | padlen = atmel_aes_padlen(req->assoclen, AES_BLOCK_SIZE); |
| 1567 | if (unlikely(req->assoclen + padlen > dd->buflen)) |
| 1568 | return atmel_aes_complete(dd, -EINVAL); |
| 1569 | sg_copy_to_buffer(req->src, sg_nents(req->src), dd->buf, req->assoclen); |
| 1570 | |
| 1571 | /* Write assoc data into the Input Data register. */ |
| 1572 | dd->data = (u32 *)dd->buf; |
| 1573 | dd->datalen = req->assoclen + padlen; |
| 1574 | return atmel_aes_gcm_data(dd); |
| 1575 | } |
| 1576 | |
| 1577 | static int atmel_aes_gcm_data(struct atmel_aes_dev *dd) |
| 1578 | { |
| 1579 | struct atmel_aes_gcm_ctx *ctx = atmel_aes_gcm_ctx_cast(dd->ctx); |
| 1580 | struct aead_request *req = aead_request_cast(dd->areq); |
| 1581 | bool use_dma = (ctx->textlen >= ATMEL_AES_DMA_THRESHOLD); |
| 1582 | struct scatterlist *src, *dst; |
| 1583 | u32 isr, mr; |
| 1584 | |
| 1585 | /* Write AAD first. */ |
| 1586 | while (dd->datalen > 0) { |
| 1587 | atmel_aes_write_block(dd, AES_IDATAR(0), dd->data); |
| 1588 | dd->data += 4; |
| 1589 | dd->datalen -= AES_BLOCK_SIZE; |
| 1590 | |
| 1591 | isr = atmel_aes_read(dd, AES_ISR); |
| 1592 | if (!(isr & AES_INT_DATARDY)) { |
| 1593 | dd->resume = atmel_aes_gcm_data; |
| 1594 | atmel_aes_write(dd, AES_IER, AES_INT_DATARDY); |
| 1595 | return -EINPROGRESS; |
| 1596 | } |
| 1597 | } |
| 1598 | |
| 1599 | /* GMAC only. */ |
| 1600 | if (unlikely(ctx->textlen == 0)) |
| 1601 | return atmel_aes_gcm_tag_init(dd); |
| 1602 | |
| 1603 | /* Prepare src and dst scatter lists to transfer cipher/plain texts */ |
| 1604 | src = scatterwalk_ffwd(ctx->src, req->src, req->assoclen); |
| 1605 | dst = ((req->src == req->dst) ? src : |
| 1606 | scatterwalk_ffwd(ctx->dst, req->dst, req->assoclen)); |
| 1607 | |
| 1608 | if (use_dma) { |
| 1609 | /* Update the Mode Register for DMA transfers. */ |
| 1610 | mr = atmel_aes_read(dd, AES_MR); |
| 1611 | mr &= ~(AES_MR_SMOD_MASK | AES_MR_DUALBUFF); |
| 1612 | mr |= AES_MR_SMOD_IDATAR0; |
| 1613 | if (dd->caps.has_dualbuff) |
| 1614 | mr |= AES_MR_DUALBUFF; |
| 1615 | atmel_aes_write(dd, AES_MR, mr); |
| 1616 | |
| 1617 | return atmel_aes_dma_start(dd, src, dst, ctx->textlen, |
| 1618 | atmel_aes_gcm_tag_init); |
| 1619 | } |
| 1620 | |
| 1621 | return atmel_aes_cpu_start(dd, src, dst, ctx->textlen, |
| 1622 | atmel_aes_gcm_tag_init); |
| 1623 | } |
| 1624 | |
| 1625 | static int atmel_aes_gcm_tag_init(struct atmel_aes_dev *dd) |
| 1626 | { |
| 1627 | struct atmel_aes_gcm_ctx *ctx = atmel_aes_gcm_ctx_cast(dd->ctx); |
| 1628 | struct aead_request *req = aead_request_cast(dd->areq); |
| 1629 | u64 *data = dd->buf; |
| 1630 | |
| 1631 | if (likely(dd->flags & AES_FLAGS_GTAGEN)) { |
| 1632 | if (!(atmel_aes_read(dd, AES_ISR) & AES_INT_TAGRDY)) { |
| 1633 | dd->resume = atmel_aes_gcm_tag_init; |
| 1634 | atmel_aes_write(dd, AES_IER, AES_INT_TAGRDY); |
| 1635 | return -EINPROGRESS; |
| 1636 | } |
| 1637 | |
| 1638 | return atmel_aes_gcm_finalize(dd); |
| 1639 | } |
| 1640 | |
| 1641 | /* Read the GCM Intermediate Hash Word Registers. */ |
| 1642 | atmel_aes_read_block(dd, AES_GHASHR(0), ctx->ghash); |
| 1643 | |
| 1644 | data[0] = cpu_to_be64(req->assoclen * 8); |
| 1645 | data[1] = cpu_to_be64(ctx->textlen * 8); |
| 1646 | |
| 1647 | return atmel_aes_gcm_ghash(dd, (const u32 *)data, AES_BLOCK_SIZE, |
| 1648 | ctx->ghash, ctx->ghash, atmel_aes_gcm_tag); |
| 1649 | } |
| 1650 | |
| 1651 | static int atmel_aes_gcm_tag(struct atmel_aes_dev *dd) |
| 1652 | { |
| 1653 | struct atmel_aes_gcm_ctx *ctx = atmel_aes_gcm_ctx_cast(dd->ctx); |
| 1654 | unsigned long flags; |
| 1655 | |
| 1656 | /* |
| 1657 | * Change mode to CTR to complete the tag generation. |
| 1658 | * Use J0 as Initialization Vector. |
| 1659 | */ |
| 1660 | flags = dd->flags; |
| 1661 | dd->flags &= ~(AES_FLAGS_OPMODE_MASK | AES_FLAGS_GTAGEN); |
| 1662 | dd->flags |= AES_FLAGS_CTR; |
| 1663 | atmel_aes_write_ctrl(dd, false, ctx->j0); |
| 1664 | dd->flags = flags; |
| 1665 | |
| 1666 | atmel_aes_write_block(dd, AES_IDATAR(0), ctx->ghash); |
| 1667 | return atmel_aes_wait_for_data_ready(dd, atmel_aes_gcm_finalize); |
| 1668 | } |
| 1669 | |
| 1670 | static int atmel_aes_gcm_finalize(struct atmel_aes_dev *dd) |
| 1671 | { |
| 1672 | struct atmel_aes_gcm_ctx *ctx = atmel_aes_gcm_ctx_cast(dd->ctx); |
| 1673 | struct aead_request *req = aead_request_cast(dd->areq); |
| 1674 | struct crypto_aead *tfm = crypto_aead_reqtfm(req); |
| 1675 | bool enc = atmel_aes_is_encrypt(dd); |
| 1676 | u32 offset, authsize, itag[4], *otag = ctx->tag; |
| 1677 | int err; |
| 1678 | |
| 1679 | /* Read the computed tag. */ |
| 1680 | if (likely(dd->flags & AES_FLAGS_GTAGEN)) |
| 1681 | atmel_aes_read_block(dd, AES_TAGR(0), ctx->tag); |
| 1682 | else |
| 1683 | atmel_aes_read_block(dd, AES_ODATAR(0), ctx->tag); |
| 1684 | |
| 1685 | offset = req->assoclen + ctx->textlen; |
| 1686 | authsize = crypto_aead_authsize(tfm); |
| 1687 | if (enc) { |
| 1688 | scatterwalk_map_and_copy(otag, req->dst, offset, authsize, 1); |
| 1689 | err = 0; |
| 1690 | } else { |
| 1691 | scatterwalk_map_and_copy(itag, req->src, offset, authsize, 0); |
| 1692 | err = crypto_memneq(itag, otag, authsize) ? -EBADMSG : 0; |
| 1693 | } |
| 1694 | |
| 1695 | return atmel_aes_complete(dd, err); |
| 1696 | } |
| 1697 | |
| 1698 | static int atmel_aes_gcm_crypt(struct aead_request *req, |
| 1699 | unsigned long mode) |
| 1700 | { |
| 1701 | struct atmel_aes_base_ctx *ctx; |
| 1702 | struct atmel_aes_reqctx *rctx; |
| 1703 | struct atmel_aes_dev *dd; |
| 1704 | |
| 1705 | ctx = crypto_aead_ctx(crypto_aead_reqtfm(req)); |
| 1706 | ctx->block_size = AES_BLOCK_SIZE; |
| 1707 | |
| 1708 | dd = atmel_aes_find_dev(ctx); |
| 1709 | if (!dd) |
| 1710 | return -ENODEV; |
| 1711 | |
| 1712 | rctx = aead_request_ctx(req); |
| 1713 | rctx->mode = AES_FLAGS_GCM | mode; |
| 1714 | |
| 1715 | return atmel_aes_handle_queue(dd, &req->base); |
| 1716 | } |
| 1717 | |
| 1718 | static int atmel_aes_gcm_setkey(struct crypto_aead *tfm, const u8 *key, |
| 1719 | unsigned int keylen) |
| 1720 | { |
| 1721 | struct atmel_aes_base_ctx *ctx = crypto_aead_ctx(tfm); |
| 1722 | |
| 1723 | if (keylen != AES_KEYSIZE_256 && |
| 1724 | keylen != AES_KEYSIZE_192 && |
| 1725 | keylen != AES_KEYSIZE_128) { |
| 1726 | crypto_aead_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN); |
| 1727 | return -EINVAL; |
| 1728 | } |
| 1729 | |
| 1730 | memcpy(ctx->key, key, keylen); |
| 1731 | ctx->keylen = keylen; |
| 1732 | |
| 1733 | return 0; |
| 1734 | } |
| 1735 | |
| 1736 | static int atmel_aes_gcm_setauthsize(struct crypto_aead *tfm, |
| 1737 | unsigned int authsize) |
| 1738 | { |
| 1739 | /* Same as crypto_gcm_authsize() from crypto/gcm.c */ |
| 1740 | switch (authsize) { |
| 1741 | case 4: |
| 1742 | case 8: |
| 1743 | case 12: |
| 1744 | case 13: |
| 1745 | case 14: |
| 1746 | case 15: |
| 1747 | case 16: |
| 1748 | break; |
| 1749 | default: |
| 1750 | return -EINVAL; |
| 1751 | } |
| 1752 | |
| 1753 | return 0; |
| 1754 | } |
| 1755 | |
| 1756 | static int atmel_aes_gcm_encrypt(struct aead_request *req) |
| 1757 | { |
| 1758 | return atmel_aes_gcm_crypt(req, AES_FLAGS_ENCRYPT); |
| 1759 | } |
| 1760 | |
| 1761 | static int atmel_aes_gcm_decrypt(struct aead_request *req) |
| 1762 | { |
| 1763 | return atmel_aes_gcm_crypt(req, 0); |
| 1764 | } |
| 1765 | |
| 1766 | static int atmel_aes_gcm_init(struct crypto_aead *tfm) |
| 1767 | { |
| 1768 | struct atmel_aes_gcm_ctx *ctx = crypto_aead_ctx(tfm); |
| 1769 | |
| 1770 | crypto_aead_set_reqsize(tfm, sizeof(struct atmel_aes_reqctx)); |
| 1771 | ctx->base.start = atmel_aes_gcm_start; |
| 1772 | |
| 1773 | return 0; |
| 1774 | } |
| 1775 | |
| 1776 | static void atmel_aes_gcm_exit(struct crypto_aead *tfm) |
| 1777 | { |
| 1778 | |
| 1779 | } |
| 1780 | |
| 1781 | static struct aead_alg aes_gcm_alg = { |
| 1782 | .setkey = atmel_aes_gcm_setkey, |
| 1783 | .setauthsize = atmel_aes_gcm_setauthsize, |
| 1784 | .encrypt = atmel_aes_gcm_encrypt, |
| 1785 | .decrypt = atmel_aes_gcm_decrypt, |
| 1786 | .init = atmel_aes_gcm_init, |
| 1787 | .exit = atmel_aes_gcm_exit, |
| 1788 | .ivsize = 12, |
| 1789 | .maxauthsize = AES_BLOCK_SIZE, |
| 1790 | |
| 1791 | .base = { |
| 1792 | .cra_name = "gcm(aes)", |
| 1793 | .cra_driver_name = "atmel-gcm-aes", |
| 1794 | .cra_priority = ATMEL_AES_PRIORITY, |
| 1795 | .cra_flags = CRYPTO_ALG_ASYNC, |
| 1796 | .cra_blocksize = 1, |
| 1797 | .cra_ctxsize = sizeof(struct atmel_aes_gcm_ctx), |
| 1798 | .cra_alignmask = 0xf, |
| 1799 | .cra_module = THIS_MODULE, |
| 1800 | }, |
| 1801 | }; |
| 1802 | |
| 1803 | |
Cyrille Pitchen | d52db51 | 2016-10-03 14:33:16 +0200 | [diff] [blame] | 1804 | /* xts functions */ |
| 1805 | |
| 1806 | static inline struct atmel_aes_xts_ctx * |
| 1807 | atmel_aes_xts_ctx_cast(struct atmel_aes_base_ctx *ctx) |
| 1808 | { |
| 1809 | return container_of(ctx, struct atmel_aes_xts_ctx, base); |
| 1810 | } |
| 1811 | |
| 1812 | static int atmel_aes_xts_process_data(struct atmel_aes_dev *dd); |
| 1813 | |
| 1814 | static int atmel_aes_xts_start(struct atmel_aes_dev *dd) |
| 1815 | { |
| 1816 | struct atmel_aes_xts_ctx *ctx = atmel_aes_xts_ctx_cast(dd->ctx); |
| 1817 | struct ablkcipher_request *req = ablkcipher_request_cast(dd->areq); |
| 1818 | struct atmel_aes_reqctx *rctx = ablkcipher_request_ctx(req); |
| 1819 | unsigned long flags; |
| 1820 | int err; |
| 1821 | |
| 1822 | atmel_aes_set_mode(dd, rctx); |
| 1823 | |
| 1824 | err = atmel_aes_hw_init(dd); |
| 1825 | if (err) |
| 1826 | return atmel_aes_complete(dd, err); |
| 1827 | |
| 1828 | /* Compute the tweak value from req->info with ecb(aes). */ |
| 1829 | flags = dd->flags; |
| 1830 | dd->flags &= ~AES_FLAGS_MODE_MASK; |
| 1831 | dd->flags |= (AES_FLAGS_ECB | AES_FLAGS_ENCRYPT); |
| 1832 | atmel_aes_write_ctrl_key(dd, false, NULL, |
| 1833 | ctx->key2, ctx->base.keylen); |
| 1834 | dd->flags = flags; |
| 1835 | |
| 1836 | atmel_aes_write_block(dd, AES_IDATAR(0), req->info); |
| 1837 | return atmel_aes_wait_for_data_ready(dd, atmel_aes_xts_process_data); |
| 1838 | } |
| 1839 | |
| 1840 | static int atmel_aes_xts_process_data(struct atmel_aes_dev *dd) |
| 1841 | { |
| 1842 | struct ablkcipher_request *req = ablkcipher_request_cast(dd->areq); |
| 1843 | bool use_dma = (req->nbytes >= ATMEL_AES_DMA_THRESHOLD); |
| 1844 | u32 tweak[AES_BLOCK_SIZE / sizeof(u32)]; |
| 1845 | static const u32 one[AES_BLOCK_SIZE / sizeof(u32)] = {cpu_to_le32(1), }; |
| 1846 | u8 *tweak_bytes = (u8 *)tweak; |
| 1847 | int i; |
| 1848 | |
| 1849 | /* Read the computed ciphered tweak value. */ |
| 1850 | atmel_aes_read_block(dd, AES_ODATAR(0), tweak); |
| 1851 | /* |
| 1852 | * Hardware quirk: |
| 1853 | * the order of the ciphered tweak bytes need to be reversed before |
| 1854 | * writing them into the ODATARx registers. |
| 1855 | */ |
| 1856 | for (i = 0; i < AES_BLOCK_SIZE/2; ++i) { |
| 1857 | u8 tmp = tweak_bytes[AES_BLOCK_SIZE - 1 - i]; |
| 1858 | |
| 1859 | tweak_bytes[AES_BLOCK_SIZE - 1 - i] = tweak_bytes[i]; |
| 1860 | tweak_bytes[i] = tmp; |
| 1861 | } |
| 1862 | |
| 1863 | /* Process the data. */ |
| 1864 | atmel_aes_write_ctrl(dd, use_dma, NULL); |
| 1865 | atmel_aes_write_block(dd, AES_TWR(0), tweak); |
| 1866 | atmel_aes_write_block(dd, AES_ALPHAR(0), one); |
| 1867 | if (use_dma) |
| 1868 | return atmel_aes_dma_start(dd, req->src, req->dst, req->nbytes, |
| 1869 | atmel_aes_transfer_complete); |
| 1870 | |
| 1871 | return atmel_aes_cpu_start(dd, req->src, req->dst, req->nbytes, |
| 1872 | atmel_aes_transfer_complete); |
| 1873 | } |
| 1874 | |
| 1875 | static int atmel_aes_xts_setkey(struct crypto_ablkcipher *tfm, const u8 *key, |
| 1876 | unsigned int keylen) |
| 1877 | { |
| 1878 | struct atmel_aes_xts_ctx *ctx = crypto_ablkcipher_ctx(tfm); |
| 1879 | int err; |
| 1880 | |
| 1881 | err = xts_check_key(crypto_ablkcipher_tfm(tfm), key, keylen); |
| 1882 | if (err) |
| 1883 | return err; |
| 1884 | |
| 1885 | memcpy(ctx->base.key, key, keylen/2); |
| 1886 | memcpy(ctx->key2, key + keylen/2, keylen/2); |
| 1887 | ctx->base.keylen = keylen/2; |
| 1888 | |
| 1889 | return 0; |
| 1890 | } |
| 1891 | |
| 1892 | static int atmel_aes_xts_encrypt(struct ablkcipher_request *req) |
| 1893 | { |
| 1894 | return atmel_aes_crypt(req, AES_FLAGS_XTS | AES_FLAGS_ENCRYPT); |
| 1895 | } |
| 1896 | |
| 1897 | static int atmel_aes_xts_decrypt(struct ablkcipher_request *req) |
| 1898 | { |
| 1899 | return atmel_aes_crypt(req, AES_FLAGS_XTS); |
| 1900 | } |
| 1901 | |
| 1902 | static int atmel_aes_xts_cra_init(struct crypto_tfm *tfm) |
| 1903 | { |
| 1904 | struct atmel_aes_xts_ctx *ctx = crypto_tfm_ctx(tfm); |
| 1905 | |
| 1906 | tfm->crt_ablkcipher.reqsize = sizeof(struct atmel_aes_reqctx); |
| 1907 | ctx->base.start = atmel_aes_xts_start; |
| 1908 | |
| 1909 | return 0; |
| 1910 | } |
| 1911 | |
| 1912 | static struct crypto_alg aes_xts_alg = { |
| 1913 | .cra_name = "xts(aes)", |
| 1914 | .cra_driver_name = "atmel-xts-aes", |
| 1915 | .cra_priority = ATMEL_AES_PRIORITY, |
| 1916 | .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC, |
| 1917 | .cra_blocksize = AES_BLOCK_SIZE, |
| 1918 | .cra_ctxsize = sizeof(struct atmel_aes_xts_ctx), |
| 1919 | .cra_alignmask = 0xf, |
| 1920 | .cra_type = &crypto_ablkcipher_type, |
| 1921 | .cra_module = THIS_MODULE, |
| 1922 | .cra_init = atmel_aes_xts_cra_init, |
| 1923 | .cra_exit = atmel_aes_cra_exit, |
| 1924 | .cra_u.ablkcipher = { |
| 1925 | .min_keysize = 2 * AES_MIN_KEY_SIZE, |
| 1926 | .max_keysize = 2 * AES_MAX_KEY_SIZE, |
| 1927 | .ivsize = AES_BLOCK_SIZE, |
| 1928 | .setkey = atmel_aes_xts_setkey, |
| 1929 | .encrypt = atmel_aes_xts_encrypt, |
| 1930 | .decrypt = atmel_aes_xts_decrypt, |
| 1931 | } |
| 1932 | }; |
| 1933 | |
| 1934 | |
Cyrille Pitchen | e37a7e5 | 2015-12-17 18:13:03 +0100 | [diff] [blame] | 1935 | /* Probe functions */ |
| 1936 | |
| 1937 | static int atmel_aes_buff_init(struct atmel_aes_dev *dd) |
| 1938 | { |
| 1939 | dd->buf = (void *)__get_free_pages(GFP_KERNEL, ATMEL_AES_BUFFER_ORDER); |
| 1940 | dd->buflen = ATMEL_AES_BUFFER_SIZE; |
| 1941 | dd->buflen &= ~(AES_BLOCK_SIZE - 1); |
| 1942 | |
| 1943 | if (!dd->buf) { |
| 1944 | dev_err(dd->dev, "unable to alloc pages.\n"); |
| 1945 | return -ENOMEM; |
| 1946 | } |
| 1947 | |
| 1948 | return 0; |
| 1949 | } |
| 1950 | |
| 1951 | static void atmel_aes_buff_cleanup(struct atmel_aes_dev *dd) |
| 1952 | { |
| 1953 | free_page((unsigned long)dd->buf); |
| 1954 | } |
| 1955 | |
| 1956 | static bool atmel_aes_filter(struct dma_chan *chan, void *slave) |
| 1957 | { |
| 1958 | struct at_dma_slave *sl = slave; |
| 1959 | |
| 1960 | if (sl && sl->dma_dev == chan->device->dev) { |
| 1961 | chan->private = sl; |
| 1962 | return true; |
| 1963 | } else { |
| 1964 | return false; |
| 1965 | } |
| 1966 | } |
| 1967 | |
| 1968 | static int atmel_aes_dma_init(struct atmel_aes_dev *dd, |
| 1969 | struct crypto_platform_data *pdata) |
| 1970 | { |
| 1971 | struct at_dma_slave *slave; |
| 1972 | int err = -ENOMEM; |
| 1973 | dma_cap_mask_t mask; |
| 1974 | |
| 1975 | dma_cap_zero(mask); |
| 1976 | dma_cap_set(DMA_SLAVE, mask); |
| 1977 | |
| 1978 | /* Try to grab 2 DMA channels */ |
| 1979 | slave = &pdata->dma_slave->rxdata; |
| 1980 | dd->src.chan = dma_request_slave_channel_compat(mask, atmel_aes_filter, |
| 1981 | slave, dd->dev, "tx"); |
| 1982 | if (!dd->src.chan) |
| 1983 | goto err_dma_in; |
| 1984 | |
| 1985 | slave = &pdata->dma_slave->txdata; |
| 1986 | dd->dst.chan = dma_request_slave_channel_compat(mask, atmel_aes_filter, |
| 1987 | slave, dd->dev, "rx"); |
| 1988 | if (!dd->dst.chan) |
| 1989 | goto err_dma_out; |
| 1990 | |
| 1991 | return 0; |
| 1992 | |
| 1993 | err_dma_out: |
| 1994 | dma_release_channel(dd->src.chan); |
| 1995 | err_dma_in: |
| 1996 | dev_warn(dd->dev, "no DMA channel available\n"); |
| 1997 | return err; |
| 1998 | } |
| 1999 | |
| 2000 | static void atmel_aes_dma_cleanup(struct atmel_aes_dev *dd) |
| 2001 | { |
| 2002 | dma_release_channel(dd->dst.chan); |
| 2003 | dma_release_channel(dd->src.chan); |
| 2004 | } |
| 2005 | |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 2006 | static void atmel_aes_queue_task(unsigned long data) |
| 2007 | { |
| 2008 | struct atmel_aes_dev *dd = (struct atmel_aes_dev *)data; |
| 2009 | |
| 2010 | atmel_aes_handle_queue(dd, NULL); |
| 2011 | } |
| 2012 | |
| 2013 | static void atmel_aes_done_task(unsigned long data) |
| 2014 | { |
Cyrille Pitchen | afbac17 | 2015-12-17 18:13:02 +0100 | [diff] [blame] | 2015 | struct atmel_aes_dev *dd = (struct atmel_aes_dev *)data; |
Cyrille Pitchen | 10f12c1 | 2015-12-17 17:48:42 +0100 | [diff] [blame] | 2016 | |
| 2017 | dd->is_async = true; |
| 2018 | (void)dd->resume(dd); |
| 2019 | } |
| 2020 | |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 2021 | static irqreturn_t atmel_aes_irq(int irq, void *dev_id) |
| 2022 | { |
| 2023 | struct atmel_aes_dev *aes_dd = dev_id; |
| 2024 | u32 reg; |
| 2025 | |
| 2026 | reg = atmel_aes_read(aes_dd, AES_ISR); |
| 2027 | if (reg & atmel_aes_read(aes_dd, AES_IMR)) { |
| 2028 | atmel_aes_write(aes_dd, AES_IDR, reg); |
| 2029 | if (AES_FLAGS_BUSY & aes_dd->flags) |
| 2030 | tasklet_schedule(&aes_dd->done_task); |
| 2031 | else |
| 2032 | dev_warn(aes_dd->dev, "AES interrupt when no active requests.\n"); |
| 2033 | return IRQ_HANDLED; |
| 2034 | } |
| 2035 | |
| 2036 | return IRQ_NONE; |
| 2037 | } |
| 2038 | |
| 2039 | static void atmel_aes_unregister_algs(struct atmel_aes_dev *dd) |
| 2040 | { |
| 2041 | int i; |
| 2042 | |
Cyrille Pitchen | d52db51 | 2016-10-03 14:33:16 +0200 | [diff] [blame] | 2043 | if (dd->caps.has_xts) |
| 2044 | crypto_unregister_alg(&aes_xts_alg); |
| 2045 | |
Cyrille Pitchen | d441954 | 2015-12-17 18:13:07 +0100 | [diff] [blame] | 2046 | if (dd->caps.has_gcm) |
| 2047 | crypto_unregister_aead(&aes_gcm_alg); |
| 2048 | |
Nicolas Royer | cadc4ab | 2013-02-20 17:10:24 +0100 | [diff] [blame] | 2049 | if (dd->caps.has_cfb64) |
| 2050 | crypto_unregister_alg(&aes_cfb64_alg); |
Cyrille Pitchen | 924a8bc | 2015-12-17 17:48:35 +0100 | [diff] [blame] | 2051 | |
| 2052 | for (i = 0; i < ARRAY_SIZE(aes_algs); i++) |
| 2053 | crypto_unregister_alg(&aes_algs[i]); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 2054 | } |
| 2055 | |
| 2056 | static int atmel_aes_register_algs(struct atmel_aes_dev *dd) |
| 2057 | { |
| 2058 | int err, i, j; |
| 2059 | |
| 2060 | for (i = 0; i < ARRAY_SIZE(aes_algs); i++) { |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 2061 | err = crypto_register_alg(&aes_algs[i]); |
| 2062 | if (err) |
| 2063 | goto err_aes_algs; |
| 2064 | } |
| 2065 | |
Nicolas Royer | cadc4ab | 2013-02-20 17:10:24 +0100 | [diff] [blame] | 2066 | if (dd->caps.has_cfb64) { |
| 2067 | err = crypto_register_alg(&aes_cfb64_alg); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 2068 | if (err) |
| 2069 | goto err_aes_cfb64_alg; |
| 2070 | } |
| 2071 | |
Cyrille Pitchen | d441954 | 2015-12-17 18:13:07 +0100 | [diff] [blame] | 2072 | if (dd->caps.has_gcm) { |
| 2073 | err = crypto_register_aead(&aes_gcm_alg); |
| 2074 | if (err) |
| 2075 | goto err_aes_gcm_alg; |
| 2076 | } |
| 2077 | |
Cyrille Pitchen | d52db51 | 2016-10-03 14:33:16 +0200 | [diff] [blame] | 2078 | if (dd->caps.has_xts) { |
| 2079 | err = crypto_register_alg(&aes_xts_alg); |
| 2080 | if (err) |
| 2081 | goto err_aes_xts_alg; |
| 2082 | } |
| 2083 | |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 2084 | return 0; |
| 2085 | |
Cyrille Pitchen | d52db51 | 2016-10-03 14:33:16 +0200 | [diff] [blame] | 2086 | err_aes_xts_alg: |
| 2087 | crypto_unregister_aead(&aes_gcm_alg); |
Cyrille Pitchen | d441954 | 2015-12-17 18:13:07 +0100 | [diff] [blame] | 2088 | err_aes_gcm_alg: |
| 2089 | crypto_unregister_alg(&aes_cfb64_alg); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 2090 | err_aes_cfb64_alg: |
| 2091 | i = ARRAY_SIZE(aes_algs); |
| 2092 | err_aes_algs: |
| 2093 | for (j = 0; j < i; j++) |
| 2094 | crypto_unregister_alg(&aes_algs[j]); |
| 2095 | |
| 2096 | return err; |
| 2097 | } |
| 2098 | |
Nicolas Royer | cadc4ab | 2013-02-20 17:10:24 +0100 | [diff] [blame] | 2099 | static void atmel_aes_get_cap(struct atmel_aes_dev *dd) |
| 2100 | { |
| 2101 | dd->caps.has_dualbuff = 0; |
| 2102 | dd->caps.has_cfb64 = 0; |
Cyrille Pitchen | fcac836 | 2015-12-17 18:13:05 +0100 | [diff] [blame] | 2103 | dd->caps.has_ctr32 = 0; |
Cyrille Pitchen | d441954 | 2015-12-17 18:13:07 +0100 | [diff] [blame] | 2104 | dd->caps.has_gcm = 0; |
Cyrille Pitchen | d52db51 | 2016-10-03 14:33:16 +0200 | [diff] [blame] | 2105 | dd->caps.has_xts = 0; |
Nicolas Royer | cadc4ab | 2013-02-20 17:10:24 +0100 | [diff] [blame] | 2106 | dd->caps.max_burst_size = 1; |
| 2107 | |
| 2108 | /* keep only major version number */ |
| 2109 | switch (dd->hw_version & 0xff0) { |
Leilei Zhao | 973e209 | 2015-12-17 17:48:32 +0100 | [diff] [blame] | 2110 | case 0x500: |
| 2111 | dd->caps.has_dualbuff = 1; |
| 2112 | dd->caps.has_cfb64 = 1; |
Cyrille Pitchen | fcac836 | 2015-12-17 18:13:05 +0100 | [diff] [blame] | 2113 | dd->caps.has_ctr32 = 1; |
Cyrille Pitchen | d441954 | 2015-12-17 18:13:07 +0100 | [diff] [blame] | 2114 | dd->caps.has_gcm = 1; |
Cyrille Pitchen | d52db51 | 2016-10-03 14:33:16 +0200 | [diff] [blame] | 2115 | dd->caps.has_xts = 1; |
Leilei Zhao | 973e209 | 2015-12-17 17:48:32 +0100 | [diff] [blame] | 2116 | dd->caps.max_burst_size = 4; |
| 2117 | break; |
Leilei Zhao | cf1f0d1 | 2015-04-07 17:45:02 +0800 | [diff] [blame] | 2118 | case 0x200: |
| 2119 | dd->caps.has_dualbuff = 1; |
| 2120 | dd->caps.has_cfb64 = 1; |
Cyrille Pitchen | fcac836 | 2015-12-17 18:13:05 +0100 | [diff] [blame] | 2121 | dd->caps.has_ctr32 = 1; |
Cyrille Pitchen | d441954 | 2015-12-17 18:13:07 +0100 | [diff] [blame] | 2122 | dd->caps.has_gcm = 1; |
Leilei Zhao | cf1f0d1 | 2015-04-07 17:45:02 +0800 | [diff] [blame] | 2123 | dd->caps.max_burst_size = 4; |
| 2124 | break; |
Nicolas Royer | cadc4ab | 2013-02-20 17:10:24 +0100 | [diff] [blame] | 2125 | case 0x130: |
| 2126 | dd->caps.has_dualbuff = 1; |
| 2127 | dd->caps.has_cfb64 = 1; |
| 2128 | dd->caps.max_burst_size = 4; |
| 2129 | break; |
| 2130 | case 0x120: |
| 2131 | break; |
| 2132 | default: |
| 2133 | dev_warn(dd->dev, |
| 2134 | "Unmanaged aes version, set minimum capabilities\n"); |
| 2135 | break; |
| 2136 | } |
| 2137 | } |
| 2138 | |
Nicolas Ferre | be943c7 | 2013-10-14 17:52:38 +0200 | [diff] [blame] | 2139 | #if defined(CONFIG_OF) |
| 2140 | static const struct of_device_id atmel_aes_dt_ids[] = { |
| 2141 | { .compatible = "atmel,at91sam9g46-aes" }, |
| 2142 | { /* sentinel */ } |
| 2143 | }; |
| 2144 | MODULE_DEVICE_TABLE(of, atmel_aes_dt_ids); |
| 2145 | |
| 2146 | static struct crypto_platform_data *atmel_aes_of_init(struct platform_device *pdev) |
| 2147 | { |
| 2148 | struct device_node *np = pdev->dev.of_node; |
| 2149 | struct crypto_platform_data *pdata; |
| 2150 | |
| 2151 | if (!np) { |
| 2152 | dev_err(&pdev->dev, "device node not found\n"); |
| 2153 | return ERR_PTR(-EINVAL); |
| 2154 | } |
| 2155 | |
| 2156 | pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL); |
| 2157 | if (!pdata) { |
| 2158 | dev_err(&pdev->dev, "could not allocate memory for pdata\n"); |
| 2159 | return ERR_PTR(-ENOMEM); |
| 2160 | } |
| 2161 | |
| 2162 | pdata->dma_slave = devm_kzalloc(&pdev->dev, |
| 2163 | sizeof(*(pdata->dma_slave)), |
| 2164 | GFP_KERNEL); |
| 2165 | if (!pdata->dma_slave) { |
| 2166 | dev_err(&pdev->dev, "could not allocate memory for dma_slave\n"); |
| 2167 | devm_kfree(&pdev->dev, pdata); |
| 2168 | return ERR_PTR(-ENOMEM); |
| 2169 | } |
| 2170 | |
| 2171 | return pdata; |
| 2172 | } |
| 2173 | #else |
| 2174 | static inline struct crypto_platform_data *atmel_aes_of_init(struct platform_device *pdev) |
| 2175 | { |
| 2176 | return ERR_PTR(-EINVAL); |
| 2177 | } |
| 2178 | #endif |
| 2179 | |
Greg Kroah-Hartman | 49cfe4d | 2012-12-21 13:14:09 -0800 | [diff] [blame] | 2180 | static int atmel_aes_probe(struct platform_device *pdev) |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 2181 | { |
| 2182 | struct atmel_aes_dev *aes_dd; |
Nicolas Royer | cadc4ab | 2013-02-20 17:10:24 +0100 | [diff] [blame] | 2183 | struct crypto_platform_data *pdata; |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 2184 | struct device *dev = &pdev->dev; |
| 2185 | struct resource *aes_res; |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 2186 | int err; |
| 2187 | |
| 2188 | pdata = pdev->dev.platform_data; |
| 2189 | if (!pdata) { |
Nicolas Ferre | be943c7 | 2013-10-14 17:52:38 +0200 | [diff] [blame] | 2190 | pdata = atmel_aes_of_init(pdev); |
| 2191 | if (IS_ERR(pdata)) { |
| 2192 | err = PTR_ERR(pdata); |
| 2193 | goto aes_dd_err; |
| 2194 | } |
| 2195 | } |
| 2196 | |
| 2197 | if (!pdata->dma_slave) { |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 2198 | err = -ENXIO; |
| 2199 | goto aes_dd_err; |
| 2200 | } |
| 2201 | |
LABBE Corentin | b0e8b34 | 2015-10-12 19:47:03 +0200 | [diff] [blame] | 2202 | aes_dd = devm_kzalloc(&pdev->dev, sizeof(*aes_dd), GFP_KERNEL); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 2203 | if (aes_dd == NULL) { |
| 2204 | dev_err(dev, "unable to alloc data struct.\n"); |
| 2205 | err = -ENOMEM; |
| 2206 | goto aes_dd_err; |
| 2207 | } |
| 2208 | |
| 2209 | aes_dd->dev = dev; |
| 2210 | |
| 2211 | platform_set_drvdata(pdev, aes_dd); |
| 2212 | |
| 2213 | INIT_LIST_HEAD(&aes_dd->list); |
Leilei Zhao | 8a10eb8 | 2015-04-07 17:45:09 +0800 | [diff] [blame] | 2214 | spin_lock_init(&aes_dd->lock); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 2215 | |
| 2216 | tasklet_init(&aes_dd->done_task, atmel_aes_done_task, |
| 2217 | (unsigned long)aes_dd); |
| 2218 | tasklet_init(&aes_dd->queue_task, atmel_aes_queue_task, |
| 2219 | (unsigned long)aes_dd); |
| 2220 | |
| 2221 | crypto_init_queue(&aes_dd->queue, ATMEL_AES_QUEUE_LENGTH); |
| 2222 | |
| 2223 | aes_dd->irq = -1; |
| 2224 | |
| 2225 | /* Get the base address */ |
| 2226 | aes_res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 2227 | if (!aes_res) { |
| 2228 | dev_err(dev, "no MEM resource info\n"); |
| 2229 | err = -ENODEV; |
| 2230 | goto res_err; |
| 2231 | } |
| 2232 | aes_dd->phys_base = aes_res->start; |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 2233 | |
| 2234 | /* Get the IRQ */ |
| 2235 | aes_dd->irq = platform_get_irq(pdev, 0); |
| 2236 | if (aes_dd->irq < 0) { |
| 2237 | dev_err(dev, "no IRQ resource info\n"); |
| 2238 | err = aes_dd->irq; |
LABBE Corentin | b0e8b34 | 2015-10-12 19:47:03 +0200 | [diff] [blame] | 2239 | goto res_err; |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 2240 | } |
| 2241 | |
LABBE Corentin | b0e8b34 | 2015-10-12 19:47:03 +0200 | [diff] [blame] | 2242 | err = devm_request_irq(&pdev->dev, aes_dd->irq, atmel_aes_irq, |
| 2243 | IRQF_SHARED, "atmel-aes", aes_dd); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 2244 | if (err) { |
| 2245 | dev_err(dev, "unable to request aes irq.\n"); |
LABBE Corentin | b0e8b34 | 2015-10-12 19:47:03 +0200 | [diff] [blame] | 2246 | goto res_err; |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 2247 | } |
| 2248 | |
| 2249 | /* Initializing the clock */ |
LABBE Corentin | b0e8b34 | 2015-10-12 19:47:03 +0200 | [diff] [blame] | 2250 | aes_dd->iclk = devm_clk_get(&pdev->dev, "aes_clk"); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 2251 | if (IS_ERR(aes_dd->iclk)) { |
Colin Ian King | be20835 | 2015-02-28 20:40:10 +0000 | [diff] [blame] | 2252 | dev_err(dev, "clock initialization failed.\n"); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 2253 | err = PTR_ERR(aes_dd->iclk); |
LABBE Corentin | b0e8b34 | 2015-10-12 19:47:03 +0200 | [diff] [blame] | 2254 | goto res_err; |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 2255 | } |
| 2256 | |
LABBE Corentin | b0e8b34 | 2015-10-12 19:47:03 +0200 | [diff] [blame] | 2257 | aes_dd->io_base = devm_ioremap_resource(&pdev->dev, aes_res); |
Vladimir Zapolskiy | 9b52d55 | 2016-03-06 03:21:52 +0200 | [diff] [blame] | 2258 | if (IS_ERR(aes_dd->io_base)) { |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 2259 | dev_err(dev, "can't ioremap\n"); |
Vladimir Zapolskiy | 9b52d55 | 2016-03-06 03:21:52 +0200 | [diff] [blame] | 2260 | err = PTR_ERR(aes_dd->io_base); |
LABBE Corentin | b0e8b34 | 2015-10-12 19:47:03 +0200 | [diff] [blame] | 2261 | goto res_err; |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 2262 | } |
| 2263 | |
Cyrille Pitchen | 49a2045 | 2016-01-29 17:53:33 +0100 | [diff] [blame] | 2264 | err = clk_prepare(aes_dd->iclk); |
Cyrille Pitchen | aab0a39 | 2015-12-17 17:48:37 +0100 | [diff] [blame] | 2265 | if (err) |
| 2266 | goto res_err; |
Nicolas Royer | cadc4ab | 2013-02-20 17:10:24 +0100 | [diff] [blame] | 2267 | |
Cyrille Pitchen | 49a2045 | 2016-01-29 17:53:33 +0100 | [diff] [blame] | 2268 | err = atmel_aes_hw_version_init(aes_dd); |
| 2269 | if (err) |
| 2270 | goto iclk_unprepare; |
| 2271 | |
Nicolas Royer | cadc4ab | 2013-02-20 17:10:24 +0100 | [diff] [blame] | 2272 | atmel_aes_get_cap(aes_dd); |
| 2273 | |
| 2274 | err = atmel_aes_buff_init(aes_dd); |
| 2275 | if (err) |
| 2276 | goto err_aes_buff; |
| 2277 | |
| 2278 | err = atmel_aes_dma_init(aes_dd, pdata); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 2279 | if (err) |
| 2280 | goto err_aes_dma; |
| 2281 | |
| 2282 | spin_lock(&atmel_aes.lock); |
| 2283 | list_add_tail(&aes_dd->list, &atmel_aes.dev_list); |
| 2284 | spin_unlock(&atmel_aes.lock); |
| 2285 | |
| 2286 | err = atmel_aes_register_algs(aes_dd); |
| 2287 | if (err) |
| 2288 | goto err_algs; |
| 2289 | |
Nicolas Ferre | be943c7 | 2013-10-14 17:52:38 +0200 | [diff] [blame] | 2290 | dev_info(dev, "Atmel AES - Using %s, %s for DMA transfers\n", |
Cyrille Pitchen | bbe628e | 2015-12-17 18:13:00 +0100 | [diff] [blame] | 2291 | dma_chan_name(aes_dd->src.chan), |
| 2292 | dma_chan_name(aes_dd->dst.chan)); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 2293 | |
| 2294 | return 0; |
| 2295 | |
| 2296 | err_algs: |
| 2297 | spin_lock(&atmel_aes.lock); |
| 2298 | list_del(&aes_dd->list); |
| 2299 | spin_unlock(&atmel_aes.lock); |
| 2300 | atmel_aes_dma_cleanup(aes_dd); |
| 2301 | err_aes_dma: |
Nicolas Royer | cadc4ab | 2013-02-20 17:10:24 +0100 | [diff] [blame] | 2302 | atmel_aes_buff_cleanup(aes_dd); |
| 2303 | err_aes_buff: |
Cyrille Pitchen | 49a2045 | 2016-01-29 17:53:33 +0100 | [diff] [blame] | 2304 | iclk_unprepare: |
| 2305 | clk_unprepare(aes_dd->iclk); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 2306 | res_err: |
| 2307 | tasklet_kill(&aes_dd->done_task); |
| 2308 | tasklet_kill(&aes_dd->queue_task); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 2309 | aes_dd_err: |
| 2310 | dev_err(dev, "initialization failed.\n"); |
| 2311 | |
| 2312 | return err; |
| 2313 | } |
| 2314 | |
Greg Kroah-Hartman | 49cfe4d | 2012-12-21 13:14:09 -0800 | [diff] [blame] | 2315 | static int atmel_aes_remove(struct platform_device *pdev) |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 2316 | { |
Wei Yongjun | fc78334 | 2016-10-24 14:51:22 +0000 | [diff] [blame] | 2317 | struct atmel_aes_dev *aes_dd; |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 2318 | |
| 2319 | aes_dd = platform_get_drvdata(pdev); |
| 2320 | if (!aes_dd) |
| 2321 | return -ENODEV; |
| 2322 | spin_lock(&atmel_aes.lock); |
| 2323 | list_del(&aes_dd->list); |
| 2324 | spin_unlock(&atmel_aes.lock); |
| 2325 | |
| 2326 | atmel_aes_unregister_algs(aes_dd); |
| 2327 | |
| 2328 | tasklet_kill(&aes_dd->done_task); |
| 2329 | tasklet_kill(&aes_dd->queue_task); |
| 2330 | |
| 2331 | atmel_aes_dma_cleanup(aes_dd); |
Cyrille Pitchen | 2a37782 | 2015-12-17 17:48:46 +0100 | [diff] [blame] | 2332 | atmel_aes_buff_cleanup(aes_dd); |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 2333 | |
Cyrille Pitchen | 49a2045 | 2016-01-29 17:53:33 +0100 | [diff] [blame] | 2334 | clk_unprepare(aes_dd->iclk); |
| 2335 | |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 2336 | return 0; |
| 2337 | } |
| 2338 | |
| 2339 | static struct platform_driver atmel_aes_driver = { |
| 2340 | .probe = atmel_aes_probe, |
Greg Kroah-Hartman | 49cfe4d | 2012-12-21 13:14:09 -0800 | [diff] [blame] | 2341 | .remove = atmel_aes_remove, |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 2342 | .driver = { |
| 2343 | .name = "atmel_aes", |
Nicolas Ferre | be943c7 | 2013-10-14 17:52:38 +0200 | [diff] [blame] | 2344 | .of_match_table = of_match_ptr(atmel_aes_dt_ids), |
Nicolas Royer | bd3c7b5 | 2012-07-01 19:19:44 +0200 | [diff] [blame] | 2345 | }, |
| 2346 | }; |
| 2347 | |
| 2348 | module_platform_driver(atmel_aes_driver); |
| 2349 | |
| 2350 | MODULE_DESCRIPTION("Atmel AES hw acceleration support."); |
| 2351 | MODULE_LICENSE("GPL v2"); |
| 2352 | MODULE_AUTHOR("Nicolas Royer - Eukréa Electromatique"); |