Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Cryptographic API. |
| 3 | * |
| 4 | * Support for ATMEL SHA1/SHA256 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-sham.c drivers. |
| 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 | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 27 | #include <linux/init.h> |
| 28 | #include <linux/errno.h> |
| 29 | #include <linux/interrupt.h> |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 30 | #include <linux/irq.h> |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 31 | #include <linux/scatterlist.h> |
| 32 | #include <linux/dma-mapping.h> |
Nicolas Ferre | abfe7ae | 2013-10-15 15:36:34 +0200 | [diff] [blame] | 33 | #include <linux/of_device.h> |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 34 | #include <linux/delay.h> |
| 35 | #include <linux/crypto.h> |
| 36 | #include <linux/cryptohash.h> |
| 37 | #include <crypto/scatterwalk.h> |
| 38 | #include <crypto/algapi.h> |
| 39 | #include <crypto/sha.h> |
| 40 | #include <crypto/hash.h> |
| 41 | #include <crypto/internal/hash.h> |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 42 | #include <linux/platform_data/crypto-atmel.h> |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 43 | #include "atmel-sha-regs.h" |
| 44 | |
| 45 | /* SHA flags */ |
| 46 | #define SHA_FLAGS_BUSY BIT(0) |
| 47 | #define SHA_FLAGS_FINAL BIT(1) |
| 48 | #define SHA_FLAGS_DMA_ACTIVE BIT(2) |
| 49 | #define SHA_FLAGS_OUTPUT_READY BIT(3) |
| 50 | #define SHA_FLAGS_INIT BIT(4) |
| 51 | #define SHA_FLAGS_CPU BIT(5) |
| 52 | #define SHA_FLAGS_DMA_READY BIT(6) |
| 53 | |
Cyrille Pitchen | f07ceba | 2017-01-26 17:07:49 +0100 | [diff] [blame] | 54 | /* bits[10:8] are reserved. */ |
| 55 | #define SHA_FLAGS_ALGO_MASK SHA_MR_ALGO_MASK |
| 56 | #define SHA_FLAGS_SHA1 SHA_MR_ALGO_SHA1 |
| 57 | #define SHA_FLAGS_SHA256 SHA_MR_ALGO_SHA256 |
| 58 | #define SHA_FLAGS_SHA384 SHA_MR_ALGO_SHA384 |
| 59 | #define SHA_FLAGS_SHA512 SHA_MR_ALGO_SHA512 |
| 60 | #define SHA_FLAGS_SHA224 SHA_MR_ALGO_SHA224 |
| 61 | |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 62 | #define SHA_FLAGS_FINUP BIT(16) |
| 63 | #define SHA_FLAGS_SG BIT(17) |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 64 | #define SHA_FLAGS_ERROR BIT(23) |
| 65 | #define SHA_FLAGS_PAD BIT(24) |
Cyrille Pitchen | 7cee350 | 2016-01-15 15:49:34 +0100 | [diff] [blame] | 66 | #define SHA_FLAGS_RESTORE BIT(25) |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 67 | |
| 68 | #define SHA_OP_UPDATE 1 |
| 69 | #define SHA_OP_FINAL 2 |
| 70 | |
Cyrille Pitchen | cc831d3 | 2016-01-29 17:04:02 +0100 | [diff] [blame] | 71 | #define SHA_BUFFER_LEN (PAGE_SIZE / 16) |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 72 | |
| 73 | #define ATMEL_SHA_DMA_THRESHOLD 56 |
| 74 | |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 75 | struct atmel_sha_caps { |
| 76 | bool has_dma; |
| 77 | bool has_dualbuff; |
| 78 | bool has_sha224; |
| 79 | bool has_sha_384_512; |
Cyrille Pitchen | 7cee350 | 2016-01-15 15:49:34 +0100 | [diff] [blame] | 80 | bool has_uihv; |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 81 | }; |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 82 | |
| 83 | struct atmel_sha_dev; |
| 84 | |
Cyrille Pitchen | cc831d3 | 2016-01-29 17:04:02 +0100 | [diff] [blame] | 85 | /* |
Cyrille Pitchen | 9c4274d | 2016-02-08 16:26:48 +0100 | [diff] [blame] | 86 | * .statesize = sizeof(struct atmel_sha_reqctx) must be <= PAGE_SIZE / 8 as |
Cyrille Pitchen | cc831d3 | 2016-01-29 17:04:02 +0100 | [diff] [blame] | 87 | * tested by the ahash_prepare_alg() function. |
| 88 | */ |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 89 | struct atmel_sha_reqctx { |
| 90 | struct atmel_sha_dev *dd; |
| 91 | unsigned long flags; |
| 92 | unsigned long op; |
| 93 | |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 94 | u8 digest[SHA512_DIGEST_SIZE] __aligned(sizeof(u32)); |
| 95 | u64 digcnt[2]; |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 96 | size_t bufcnt; |
| 97 | size_t buflen; |
| 98 | dma_addr_t dma_addr; |
| 99 | |
| 100 | /* walk state */ |
| 101 | struct scatterlist *sg; |
| 102 | unsigned int offset; /* offset in current sg */ |
| 103 | unsigned int total; /* total request */ |
| 104 | |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 105 | size_t block_size; |
| 106 | |
Cyrille Pitchen | 9c4274d | 2016-02-08 16:26:48 +0100 | [diff] [blame] | 107 | u8 buffer[SHA_BUFFER_LEN + SHA512_BLOCK_SIZE] __aligned(sizeof(u32)); |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 108 | }; |
| 109 | |
Cyrille Pitchen | a29af93 | 2017-01-26 17:07:47 +0100 | [diff] [blame] | 110 | typedef int (*atmel_sha_fn_t)(struct atmel_sha_dev *); |
| 111 | |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 112 | struct atmel_sha_ctx { |
| 113 | struct atmel_sha_dev *dd; |
Cyrille Pitchen | a29af93 | 2017-01-26 17:07:47 +0100 | [diff] [blame] | 114 | atmel_sha_fn_t start; |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 115 | |
| 116 | unsigned long flags; |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 117 | }; |
| 118 | |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 119 | #define ATMEL_SHA_QUEUE_LENGTH 50 |
| 120 | |
| 121 | struct atmel_sha_dma { |
| 122 | struct dma_chan *chan; |
| 123 | struct dma_slave_config dma_conf; |
| 124 | }; |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 125 | |
| 126 | struct atmel_sha_dev { |
| 127 | struct list_head list; |
| 128 | unsigned long phys_base; |
| 129 | struct device *dev; |
| 130 | struct clk *iclk; |
| 131 | int irq; |
| 132 | void __iomem *io_base; |
| 133 | |
| 134 | spinlock_t lock; |
| 135 | int err; |
| 136 | struct tasklet_struct done_task; |
Cyrille Pitchen | f56809c | 2016-01-15 15:49:32 +0100 | [diff] [blame] | 137 | struct tasklet_struct queue_task; |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 138 | |
| 139 | unsigned long flags; |
| 140 | struct crypto_queue queue; |
| 141 | struct ahash_request *req; |
Cyrille Pitchen | a29af93 | 2017-01-26 17:07:47 +0100 | [diff] [blame] | 142 | bool is_async; |
Cyrille Pitchen | b5ce82a | 2017-01-26 17:07:48 +0100 | [diff] [blame] | 143 | atmel_sha_fn_t resume; |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 144 | |
| 145 | struct atmel_sha_dma dma_lch_in; |
| 146 | |
| 147 | struct atmel_sha_caps caps; |
| 148 | |
| 149 | u32 hw_version; |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 150 | }; |
| 151 | |
| 152 | struct atmel_sha_drv { |
| 153 | struct list_head dev_list; |
| 154 | spinlock_t lock; |
| 155 | }; |
| 156 | |
| 157 | static struct atmel_sha_drv atmel_sha = { |
| 158 | .dev_list = LIST_HEAD_INIT(atmel_sha.dev_list), |
| 159 | .lock = __SPIN_LOCK_UNLOCKED(atmel_sha.lock), |
| 160 | }; |
| 161 | |
| 162 | static inline u32 atmel_sha_read(struct atmel_sha_dev *dd, u32 offset) |
| 163 | { |
| 164 | return readl_relaxed(dd->io_base + offset); |
| 165 | } |
| 166 | |
| 167 | static inline void atmel_sha_write(struct atmel_sha_dev *dd, |
| 168 | u32 offset, u32 value) |
| 169 | { |
| 170 | writel_relaxed(value, dd->io_base + offset); |
| 171 | } |
| 172 | |
Cyrille Pitchen | a29af93 | 2017-01-26 17:07:47 +0100 | [diff] [blame] | 173 | static inline int atmel_sha_complete(struct atmel_sha_dev *dd, int err) |
| 174 | { |
| 175 | struct ahash_request *req = dd->req; |
| 176 | |
| 177 | dd->flags &= ~(SHA_FLAGS_BUSY | SHA_FLAGS_FINAL | SHA_FLAGS_CPU | |
| 178 | SHA_FLAGS_DMA_READY | SHA_FLAGS_OUTPUT_READY); |
| 179 | |
| 180 | clk_disable(dd->iclk); |
| 181 | |
| 182 | if (dd->is_async && req->base.complete) |
| 183 | req->base.complete(&req->base, err); |
| 184 | |
| 185 | /* handle new request */ |
| 186 | tasklet_schedule(&dd->queue_task); |
| 187 | |
| 188 | return err; |
| 189 | } |
| 190 | |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 191 | static size_t atmel_sha_append_sg(struct atmel_sha_reqctx *ctx) |
| 192 | { |
| 193 | size_t count; |
| 194 | |
| 195 | while ((ctx->bufcnt < ctx->buflen) && ctx->total) { |
| 196 | count = min(ctx->sg->length - ctx->offset, ctx->total); |
| 197 | count = min(count, ctx->buflen - ctx->bufcnt); |
| 198 | |
Leilei Zhao | 803eeae | 2015-04-07 17:45:05 +0800 | [diff] [blame] | 199 | if (count <= 0) { |
| 200 | /* |
| 201 | * Check if count <= 0 because the buffer is full or |
| 202 | * because the sg length is 0. In the latest case, |
| 203 | * check if there is another sg in the list, a 0 length |
| 204 | * sg doesn't necessarily mean the end of the sg list. |
| 205 | */ |
| 206 | if ((ctx->sg->length == 0) && !sg_is_last(ctx->sg)) { |
| 207 | ctx->sg = sg_next(ctx->sg); |
| 208 | continue; |
| 209 | } else { |
| 210 | break; |
| 211 | } |
| 212 | } |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 213 | |
| 214 | scatterwalk_map_and_copy(ctx->buffer + ctx->bufcnt, ctx->sg, |
| 215 | ctx->offset, count, 0); |
| 216 | |
| 217 | ctx->bufcnt += count; |
| 218 | ctx->offset += count; |
| 219 | ctx->total -= count; |
| 220 | |
| 221 | if (ctx->offset == ctx->sg->length) { |
| 222 | ctx->sg = sg_next(ctx->sg); |
| 223 | if (ctx->sg) |
| 224 | ctx->offset = 0; |
| 225 | else |
| 226 | ctx->total = 0; |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | return 0; |
| 231 | } |
| 232 | |
| 233 | /* |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 234 | * The purpose of this padding is to ensure that the padded message is a |
| 235 | * multiple of 512 bits (SHA1/SHA224/SHA256) or 1024 bits (SHA384/SHA512). |
| 236 | * The bit "1" is appended at the end of the message followed by |
| 237 | * "padlen-1" zero bits. Then a 64 bits block (SHA1/SHA224/SHA256) or |
| 238 | * 128 bits block (SHA384/SHA512) equals to the message length in bits |
| 239 | * is appended. |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 240 | * |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 241 | * For SHA1/SHA224/SHA256, padlen is calculated as followed: |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 242 | * - if message length < 56 bytes then padlen = 56 - message length |
| 243 | * - else padlen = 64 + 56 - message length |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 244 | * |
| 245 | * For SHA384/SHA512, padlen is calculated as followed: |
| 246 | * - if message length < 112 bytes then padlen = 112 - message length |
| 247 | * - else padlen = 128 + 112 - message length |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 248 | */ |
| 249 | static void atmel_sha_fill_padding(struct atmel_sha_reqctx *ctx, int length) |
| 250 | { |
| 251 | unsigned int index, padlen; |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 252 | u64 bits[2]; |
| 253 | u64 size[2]; |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 254 | |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 255 | size[0] = ctx->digcnt[0]; |
| 256 | size[1] = ctx->digcnt[1]; |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 257 | |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 258 | size[0] += ctx->bufcnt; |
| 259 | if (size[0] < ctx->bufcnt) |
| 260 | size[1]++; |
| 261 | |
| 262 | size[0] += length; |
| 263 | if (size[0] < length) |
| 264 | size[1]++; |
| 265 | |
| 266 | bits[1] = cpu_to_be64(size[0] << 3); |
| 267 | bits[0] = cpu_to_be64(size[1] << 3 | size[0] >> 61); |
| 268 | |
Cyrille Pitchen | f07ceba | 2017-01-26 17:07:49 +0100 | [diff] [blame] | 269 | switch (ctx->flags & SHA_FLAGS_ALGO_MASK) { |
| 270 | case SHA_FLAGS_SHA384: |
| 271 | case SHA_FLAGS_SHA512: |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 272 | index = ctx->bufcnt & 0x7f; |
| 273 | padlen = (index < 112) ? (112 - index) : ((128+112) - index); |
| 274 | *(ctx->buffer + ctx->bufcnt) = 0x80; |
| 275 | memset(ctx->buffer + ctx->bufcnt + 1, 0, padlen-1); |
| 276 | memcpy(ctx->buffer + ctx->bufcnt + padlen, bits, 16); |
| 277 | ctx->bufcnt += padlen + 16; |
| 278 | ctx->flags |= SHA_FLAGS_PAD; |
Cyrille Pitchen | f07ceba | 2017-01-26 17:07:49 +0100 | [diff] [blame] | 279 | break; |
| 280 | |
| 281 | default: |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 282 | index = ctx->bufcnt & 0x3f; |
| 283 | padlen = (index < 56) ? (56 - index) : ((64+56) - index); |
| 284 | *(ctx->buffer + ctx->bufcnt) = 0x80; |
| 285 | memset(ctx->buffer + ctx->bufcnt + 1, 0, padlen-1); |
| 286 | memcpy(ctx->buffer + ctx->bufcnt + padlen, &bits[1], 8); |
| 287 | ctx->bufcnt += padlen + 8; |
| 288 | ctx->flags |= SHA_FLAGS_PAD; |
Cyrille Pitchen | f07ceba | 2017-01-26 17:07:49 +0100 | [diff] [blame] | 289 | break; |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 290 | } |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 291 | } |
| 292 | |
Cyrille Pitchen | 8340c7f | 2017-01-26 17:07:46 +0100 | [diff] [blame] | 293 | static struct atmel_sha_dev *atmel_sha_find_dev(struct atmel_sha_ctx *tctx) |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 294 | { |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 295 | struct atmel_sha_dev *dd = NULL; |
| 296 | struct atmel_sha_dev *tmp; |
| 297 | |
| 298 | spin_lock_bh(&atmel_sha.lock); |
| 299 | if (!tctx->dd) { |
| 300 | list_for_each_entry(tmp, &atmel_sha.dev_list, list) { |
| 301 | dd = tmp; |
| 302 | break; |
| 303 | } |
| 304 | tctx->dd = dd; |
| 305 | } else { |
| 306 | dd = tctx->dd; |
| 307 | } |
| 308 | |
| 309 | spin_unlock_bh(&atmel_sha.lock); |
| 310 | |
Cyrille Pitchen | 8340c7f | 2017-01-26 17:07:46 +0100 | [diff] [blame] | 311 | return dd; |
| 312 | } |
| 313 | |
| 314 | static int atmel_sha_init(struct ahash_request *req) |
| 315 | { |
| 316 | struct crypto_ahash *tfm = crypto_ahash_reqtfm(req); |
| 317 | struct atmel_sha_ctx *tctx = crypto_ahash_ctx(tfm); |
| 318 | struct atmel_sha_reqctx *ctx = ahash_request_ctx(req); |
| 319 | struct atmel_sha_dev *dd = atmel_sha_find_dev(tctx); |
| 320 | |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 321 | ctx->dd = dd; |
| 322 | |
| 323 | ctx->flags = 0; |
| 324 | |
| 325 | dev_dbg(dd->dev, "init: digest size: %d\n", |
| 326 | crypto_ahash_digestsize(tfm)); |
| 327 | |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 328 | switch (crypto_ahash_digestsize(tfm)) { |
| 329 | case SHA1_DIGEST_SIZE: |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 330 | ctx->flags |= SHA_FLAGS_SHA1; |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 331 | ctx->block_size = SHA1_BLOCK_SIZE; |
| 332 | break; |
| 333 | case SHA224_DIGEST_SIZE: |
| 334 | ctx->flags |= SHA_FLAGS_SHA224; |
| 335 | ctx->block_size = SHA224_BLOCK_SIZE; |
| 336 | break; |
| 337 | case SHA256_DIGEST_SIZE: |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 338 | ctx->flags |= SHA_FLAGS_SHA256; |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 339 | ctx->block_size = SHA256_BLOCK_SIZE; |
| 340 | break; |
| 341 | case SHA384_DIGEST_SIZE: |
| 342 | ctx->flags |= SHA_FLAGS_SHA384; |
| 343 | ctx->block_size = SHA384_BLOCK_SIZE; |
| 344 | break; |
| 345 | case SHA512_DIGEST_SIZE: |
| 346 | ctx->flags |= SHA_FLAGS_SHA512; |
| 347 | ctx->block_size = SHA512_BLOCK_SIZE; |
| 348 | break; |
| 349 | default: |
| 350 | return -EINVAL; |
| 351 | break; |
| 352 | } |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 353 | |
| 354 | ctx->bufcnt = 0; |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 355 | ctx->digcnt[0] = 0; |
| 356 | ctx->digcnt[1] = 0; |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 357 | ctx->buflen = SHA_BUFFER_LEN; |
| 358 | |
| 359 | return 0; |
| 360 | } |
| 361 | |
| 362 | static void atmel_sha_write_ctrl(struct atmel_sha_dev *dd, int dma) |
| 363 | { |
| 364 | struct atmel_sha_reqctx *ctx = ahash_request_ctx(dd->req); |
Cyrille Pitchen | 7cee350 | 2016-01-15 15:49:34 +0100 | [diff] [blame] | 365 | u32 valmr = SHA_MR_MODE_AUTO; |
| 366 | unsigned int i, hashsize = 0; |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 367 | |
| 368 | if (likely(dma)) { |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 369 | if (!dd->caps.has_dma) |
| 370 | atmel_sha_write(dd, SHA_IER, SHA_INT_TXBUFE); |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 371 | valmr = SHA_MR_MODE_PDC; |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 372 | if (dd->caps.has_dualbuff) |
| 373 | valmr |= SHA_MR_DUALBUFF; |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 374 | } else { |
| 375 | atmel_sha_write(dd, SHA_IER, SHA_INT_DATARDY); |
| 376 | } |
| 377 | |
Cyrille Pitchen | 7cee350 | 2016-01-15 15:49:34 +0100 | [diff] [blame] | 378 | switch (ctx->flags & SHA_FLAGS_ALGO_MASK) { |
| 379 | case SHA_FLAGS_SHA1: |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 380 | valmr |= SHA_MR_ALGO_SHA1; |
Cyrille Pitchen | 7cee350 | 2016-01-15 15:49:34 +0100 | [diff] [blame] | 381 | hashsize = SHA1_DIGEST_SIZE; |
| 382 | break; |
| 383 | |
| 384 | case SHA_FLAGS_SHA224: |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 385 | valmr |= SHA_MR_ALGO_SHA224; |
Cyrille Pitchen | 7cee350 | 2016-01-15 15:49:34 +0100 | [diff] [blame] | 386 | hashsize = SHA256_DIGEST_SIZE; |
| 387 | break; |
| 388 | |
| 389 | case SHA_FLAGS_SHA256: |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 390 | valmr |= SHA_MR_ALGO_SHA256; |
Cyrille Pitchen | 7cee350 | 2016-01-15 15:49:34 +0100 | [diff] [blame] | 391 | hashsize = SHA256_DIGEST_SIZE; |
| 392 | break; |
| 393 | |
| 394 | case SHA_FLAGS_SHA384: |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 395 | valmr |= SHA_MR_ALGO_SHA384; |
Cyrille Pitchen | 7cee350 | 2016-01-15 15:49:34 +0100 | [diff] [blame] | 396 | hashsize = SHA512_DIGEST_SIZE; |
| 397 | break; |
| 398 | |
| 399 | case SHA_FLAGS_SHA512: |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 400 | valmr |= SHA_MR_ALGO_SHA512; |
Cyrille Pitchen | 7cee350 | 2016-01-15 15:49:34 +0100 | [diff] [blame] | 401 | hashsize = SHA512_DIGEST_SIZE; |
| 402 | break; |
| 403 | |
| 404 | default: |
| 405 | break; |
| 406 | } |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 407 | |
| 408 | /* Setting CR_FIRST only for the first iteration */ |
Cyrille Pitchen | 7cee350 | 2016-01-15 15:49:34 +0100 | [diff] [blame] | 409 | if (!(ctx->digcnt[0] || ctx->digcnt[1])) { |
| 410 | atmel_sha_write(dd, SHA_CR, SHA_CR_FIRST); |
| 411 | } else if (dd->caps.has_uihv && (ctx->flags & SHA_FLAGS_RESTORE)) { |
| 412 | const u32 *hash = (const u32 *)ctx->digest; |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 413 | |
Cyrille Pitchen | 7cee350 | 2016-01-15 15:49:34 +0100 | [diff] [blame] | 414 | /* |
| 415 | * Restore the hardware context: update the User Initialize |
| 416 | * Hash Value (UIHV) with the value saved when the latest |
| 417 | * 'update' operation completed on this very same crypto |
| 418 | * request. |
| 419 | */ |
| 420 | ctx->flags &= ~SHA_FLAGS_RESTORE; |
| 421 | atmel_sha_write(dd, SHA_CR, SHA_CR_WUIHV); |
| 422 | for (i = 0; i < hashsize / sizeof(u32); ++i) |
| 423 | atmel_sha_write(dd, SHA_REG_DIN(i), hash[i]); |
| 424 | atmel_sha_write(dd, SHA_CR, SHA_CR_FIRST); |
| 425 | valmr |= SHA_MR_UIHV; |
| 426 | } |
| 427 | /* |
| 428 | * WARNING: If the UIHV feature is not available, the hardware CANNOT |
| 429 | * process concurrent requests: the internal registers used to store |
| 430 | * the hash/digest are still set to the partial digest output values |
| 431 | * computed during the latest round. |
| 432 | */ |
| 433 | |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 434 | atmel_sha_write(dd, SHA_MR, valmr); |
| 435 | } |
| 436 | |
Cyrille Pitchen | 9064ed9 | 2017-01-26 17:07:50 +0100 | [diff] [blame^] | 437 | static inline int atmel_sha_wait_for_data_ready(struct atmel_sha_dev *dd, |
| 438 | atmel_sha_fn_t resume) |
| 439 | { |
| 440 | u32 isr = atmel_sha_read(dd, SHA_ISR); |
| 441 | |
| 442 | if (unlikely(isr & SHA_INT_DATARDY)) |
| 443 | return resume(dd); |
| 444 | |
| 445 | dd->resume = resume; |
| 446 | atmel_sha_write(dd, SHA_IER, SHA_INT_DATARDY); |
| 447 | return -EINPROGRESS; |
| 448 | } |
| 449 | |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 450 | static int atmel_sha_xmit_cpu(struct atmel_sha_dev *dd, const u8 *buf, |
| 451 | size_t length, int final) |
| 452 | { |
| 453 | struct atmel_sha_reqctx *ctx = ahash_request_ctx(dd->req); |
| 454 | int count, len32; |
| 455 | const u32 *buffer = (const u32 *)buf; |
| 456 | |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 457 | dev_dbg(dd->dev, "xmit_cpu: digcnt: 0x%llx 0x%llx, length: %d, final: %d\n", |
| 458 | ctx->digcnt[1], ctx->digcnt[0], length, final); |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 459 | |
| 460 | atmel_sha_write_ctrl(dd, 0); |
| 461 | |
| 462 | /* should be non-zero before next lines to disable clocks later */ |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 463 | ctx->digcnt[0] += length; |
| 464 | if (ctx->digcnt[0] < length) |
| 465 | ctx->digcnt[1]++; |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 466 | |
| 467 | if (final) |
| 468 | dd->flags |= SHA_FLAGS_FINAL; /* catch last interrupt */ |
| 469 | |
| 470 | len32 = DIV_ROUND_UP(length, sizeof(u32)); |
| 471 | |
| 472 | dd->flags |= SHA_FLAGS_CPU; |
| 473 | |
| 474 | for (count = 0; count < len32; count++) |
| 475 | atmel_sha_write(dd, SHA_REG_DIN(count), buffer[count]); |
| 476 | |
| 477 | return -EINPROGRESS; |
| 478 | } |
| 479 | |
| 480 | static int atmel_sha_xmit_pdc(struct atmel_sha_dev *dd, dma_addr_t dma_addr1, |
| 481 | size_t length1, dma_addr_t dma_addr2, size_t length2, int final) |
| 482 | { |
| 483 | struct atmel_sha_reqctx *ctx = ahash_request_ctx(dd->req); |
| 484 | int len32; |
| 485 | |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 486 | dev_dbg(dd->dev, "xmit_pdc: digcnt: 0x%llx 0x%llx, length: %d, final: %d\n", |
| 487 | ctx->digcnt[1], ctx->digcnt[0], length1, final); |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 488 | |
| 489 | len32 = DIV_ROUND_UP(length1, sizeof(u32)); |
| 490 | atmel_sha_write(dd, SHA_PTCR, SHA_PTCR_TXTDIS); |
| 491 | atmel_sha_write(dd, SHA_TPR, dma_addr1); |
| 492 | atmel_sha_write(dd, SHA_TCR, len32); |
| 493 | |
| 494 | len32 = DIV_ROUND_UP(length2, sizeof(u32)); |
| 495 | atmel_sha_write(dd, SHA_TNPR, dma_addr2); |
| 496 | atmel_sha_write(dd, SHA_TNCR, len32); |
| 497 | |
| 498 | atmel_sha_write_ctrl(dd, 1); |
| 499 | |
| 500 | /* should be non-zero before next lines to disable clocks later */ |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 501 | ctx->digcnt[0] += length1; |
| 502 | if (ctx->digcnt[0] < length1) |
| 503 | ctx->digcnt[1]++; |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 504 | |
| 505 | if (final) |
| 506 | dd->flags |= SHA_FLAGS_FINAL; /* catch last interrupt */ |
| 507 | |
| 508 | dd->flags |= SHA_FLAGS_DMA_ACTIVE; |
| 509 | |
| 510 | /* Start DMA transfer */ |
| 511 | atmel_sha_write(dd, SHA_PTCR, SHA_PTCR_TXTEN); |
| 512 | |
| 513 | return -EINPROGRESS; |
| 514 | } |
| 515 | |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 516 | static void atmel_sha_dma_callback(void *data) |
| 517 | { |
| 518 | struct atmel_sha_dev *dd = data; |
| 519 | |
Cyrille Pitchen | a29af93 | 2017-01-26 17:07:47 +0100 | [diff] [blame] | 520 | dd->is_async = true; |
| 521 | |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 522 | /* dma_lch_in - completed - wait DATRDY */ |
| 523 | atmel_sha_write(dd, SHA_IER, SHA_INT_DATARDY); |
| 524 | } |
| 525 | |
| 526 | static int atmel_sha_xmit_dma(struct atmel_sha_dev *dd, dma_addr_t dma_addr1, |
| 527 | size_t length1, dma_addr_t dma_addr2, size_t length2, int final) |
| 528 | { |
| 529 | struct atmel_sha_reqctx *ctx = ahash_request_ctx(dd->req); |
| 530 | struct dma_async_tx_descriptor *in_desc; |
| 531 | struct scatterlist sg[2]; |
| 532 | |
| 533 | dev_dbg(dd->dev, "xmit_dma: digcnt: 0x%llx 0x%llx, length: %d, final: %d\n", |
| 534 | ctx->digcnt[1], ctx->digcnt[0], length1, final); |
| 535 | |
Leilei Zhao | 3f1992c | 2015-04-07 17:45:07 +0800 | [diff] [blame] | 536 | dd->dma_lch_in.dma_conf.src_maxburst = 16; |
| 537 | dd->dma_lch_in.dma_conf.dst_maxburst = 16; |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 538 | |
| 539 | dmaengine_slave_config(dd->dma_lch_in.chan, &dd->dma_lch_in.dma_conf); |
| 540 | |
| 541 | if (length2) { |
| 542 | sg_init_table(sg, 2); |
| 543 | sg_dma_address(&sg[0]) = dma_addr1; |
| 544 | sg_dma_len(&sg[0]) = length1; |
| 545 | sg_dma_address(&sg[1]) = dma_addr2; |
| 546 | sg_dma_len(&sg[1]) = length2; |
| 547 | in_desc = dmaengine_prep_slave_sg(dd->dma_lch_in.chan, sg, 2, |
| 548 | DMA_MEM_TO_DEV, DMA_PREP_INTERRUPT | DMA_CTRL_ACK); |
| 549 | } else { |
| 550 | sg_init_table(sg, 1); |
| 551 | sg_dma_address(&sg[0]) = dma_addr1; |
| 552 | sg_dma_len(&sg[0]) = length1; |
| 553 | in_desc = dmaengine_prep_slave_sg(dd->dma_lch_in.chan, sg, 1, |
| 554 | DMA_MEM_TO_DEV, DMA_PREP_INTERRUPT | DMA_CTRL_ACK); |
| 555 | } |
| 556 | if (!in_desc) |
Cyrille Pitchen | a29af93 | 2017-01-26 17:07:47 +0100 | [diff] [blame] | 557 | atmel_sha_complete(dd, -EINVAL); |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 558 | |
| 559 | in_desc->callback = atmel_sha_dma_callback; |
| 560 | in_desc->callback_param = dd; |
| 561 | |
| 562 | atmel_sha_write_ctrl(dd, 1); |
| 563 | |
| 564 | /* should be non-zero before next lines to disable clocks later */ |
| 565 | ctx->digcnt[0] += length1; |
| 566 | if (ctx->digcnt[0] < length1) |
| 567 | ctx->digcnt[1]++; |
| 568 | |
| 569 | if (final) |
| 570 | dd->flags |= SHA_FLAGS_FINAL; /* catch last interrupt */ |
| 571 | |
| 572 | dd->flags |= SHA_FLAGS_DMA_ACTIVE; |
| 573 | |
| 574 | /* Start DMA transfer */ |
| 575 | dmaengine_submit(in_desc); |
| 576 | dma_async_issue_pending(dd->dma_lch_in.chan); |
| 577 | |
| 578 | return -EINPROGRESS; |
| 579 | } |
| 580 | |
| 581 | static int atmel_sha_xmit_start(struct atmel_sha_dev *dd, dma_addr_t dma_addr1, |
| 582 | size_t length1, dma_addr_t dma_addr2, size_t length2, int final) |
| 583 | { |
| 584 | if (dd->caps.has_dma) |
| 585 | return atmel_sha_xmit_dma(dd, dma_addr1, length1, |
| 586 | dma_addr2, length2, final); |
| 587 | else |
| 588 | return atmel_sha_xmit_pdc(dd, dma_addr1, length1, |
| 589 | dma_addr2, length2, final); |
| 590 | } |
| 591 | |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 592 | static int atmel_sha_update_cpu(struct atmel_sha_dev *dd) |
| 593 | { |
| 594 | struct atmel_sha_reqctx *ctx = ahash_request_ctx(dd->req); |
| 595 | int bufcnt; |
| 596 | |
| 597 | atmel_sha_append_sg(ctx); |
| 598 | atmel_sha_fill_padding(ctx, 0); |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 599 | bufcnt = ctx->bufcnt; |
| 600 | ctx->bufcnt = 0; |
| 601 | |
| 602 | return atmel_sha_xmit_cpu(dd, ctx->buffer, bufcnt, 1); |
| 603 | } |
| 604 | |
| 605 | static int atmel_sha_xmit_dma_map(struct atmel_sha_dev *dd, |
| 606 | struct atmel_sha_reqctx *ctx, |
| 607 | size_t length, int final) |
| 608 | { |
| 609 | ctx->dma_addr = dma_map_single(dd->dev, ctx->buffer, |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 610 | ctx->buflen + ctx->block_size, DMA_TO_DEVICE); |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 611 | if (dma_mapping_error(dd->dev, ctx->dma_addr)) { |
| 612 | dev_err(dd->dev, "dma %u bytes error\n", ctx->buflen + |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 613 | ctx->block_size); |
Cyrille Pitchen | a29af93 | 2017-01-26 17:07:47 +0100 | [diff] [blame] | 614 | atmel_sha_complete(dd, -EINVAL); |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 615 | } |
| 616 | |
| 617 | ctx->flags &= ~SHA_FLAGS_SG; |
| 618 | |
| 619 | /* next call does not fail... so no unmap in the case of error */ |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 620 | return atmel_sha_xmit_start(dd, ctx->dma_addr, length, 0, 0, final); |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 621 | } |
| 622 | |
| 623 | static int atmel_sha_update_dma_slow(struct atmel_sha_dev *dd) |
| 624 | { |
| 625 | struct atmel_sha_reqctx *ctx = ahash_request_ctx(dd->req); |
| 626 | unsigned int final; |
| 627 | size_t count; |
| 628 | |
| 629 | atmel_sha_append_sg(ctx); |
| 630 | |
| 631 | final = (ctx->flags & SHA_FLAGS_FINUP) && !ctx->total; |
| 632 | |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 633 | dev_dbg(dd->dev, "slow: bufcnt: %u, digcnt: 0x%llx 0x%llx, final: %d\n", |
| 634 | ctx->bufcnt, ctx->digcnt[1], ctx->digcnt[0], final); |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 635 | |
| 636 | if (final) |
| 637 | atmel_sha_fill_padding(ctx, 0); |
| 638 | |
Ludovic Desroches | 0099286 | 2015-04-07 17:45:04 +0800 | [diff] [blame] | 639 | if (final || (ctx->bufcnt == ctx->buflen)) { |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 640 | count = ctx->bufcnt; |
| 641 | ctx->bufcnt = 0; |
| 642 | return atmel_sha_xmit_dma_map(dd, ctx, count, final); |
| 643 | } |
| 644 | |
| 645 | return 0; |
| 646 | } |
| 647 | |
| 648 | static int atmel_sha_update_dma_start(struct atmel_sha_dev *dd) |
| 649 | { |
| 650 | struct atmel_sha_reqctx *ctx = ahash_request_ctx(dd->req); |
| 651 | unsigned int length, final, tail; |
| 652 | struct scatterlist *sg; |
| 653 | unsigned int count; |
| 654 | |
| 655 | if (!ctx->total) |
| 656 | return 0; |
| 657 | |
| 658 | if (ctx->bufcnt || ctx->offset) |
| 659 | return atmel_sha_update_dma_slow(dd); |
| 660 | |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 661 | dev_dbg(dd->dev, "fast: digcnt: 0x%llx 0x%llx, bufcnt: %u, total: %u\n", |
| 662 | ctx->digcnt[1], ctx->digcnt[0], ctx->bufcnt, ctx->total); |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 663 | |
| 664 | sg = ctx->sg; |
| 665 | |
| 666 | if (!IS_ALIGNED(sg->offset, sizeof(u32))) |
| 667 | return atmel_sha_update_dma_slow(dd); |
| 668 | |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 669 | if (!sg_is_last(sg) && !IS_ALIGNED(sg->length, ctx->block_size)) |
| 670 | /* size is not ctx->block_size aligned */ |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 671 | return atmel_sha_update_dma_slow(dd); |
| 672 | |
| 673 | length = min(ctx->total, sg->length); |
| 674 | |
| 675 | if (sg_is_last(sg)) { |
| 676 | if (!(ctx->flags & SHA_FLAGS_FINUP)) { |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 677 | /* not last sg must be ctx->block_size aligned */ |
| 678 | tail = length & (ctx->block_size - 1); |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 679 | length -= tail; |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 680 | } |
| 681 | } |
| 682 | |
| 683 | ctx->total -= length; |
| 684 | ctx->offset = length; /* offset where to start slow */ |
| 685 | |
| 686 | final = (ctx->flags & SHA_FLAGS_FINUP) && !ctx->total; |
| 687 | |
| 688 | /* Add padding */ |
| 689 | if (final) { |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 690 | tail = length & (ctx->block_size - 1); |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 691 | length -= tail; |
| 692 | ctx->total += tail; |
| 693 | ctx->offset = length; /* offset where to start slow */ |
| 694 | |
| 695 | sg = ctx->sg; |
| 696 | atmel_sha_append_sg(ctx); |
| 697 | |
| 698 | atmel_sha_fill_padding(ctx, length); |
| 699 | |
| 700 | ctx->dma_addr = dma_map_single(dd->dev, ctx->buffer, |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 701 | ctx->buflen + ctx->block_size, DMA_TO_DEVICE); |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 702 | if (dma_mapping_error(dd->dev, ctx->dma_addr)) { |
| 703 | dev_err(dd->dev, "dma %u bytes error\n", |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 704 | ctx->buflen + ctx->block_size); |
Cyrille Pitchen | a29af93 | 2017-01-26 17:07:47 +0100 | [diff] [blame] | 705 | atmel_sha_complete(dd, -EINVAL); |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 706 | } |
| 707 | |
| 708 | if (length == 0) { |
| 709 | ctx->flags &= ~SHA_FLAGS_SG; |
| 710 | count = ctx->bufcnt; |
| 711 | ctx->bufcnt = 0; |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 712 | return atmel_sha_xmit_start(dd, ctx->dma_addr, count, 0, |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 713 | 0, final); |
| 714 | } else { |
| 715 | ctx->sg = sg; |
| 716 | if (!dma_map_sg(dd->dev, ctx->sg, 1, |
| 717 | DMA_TO_DEVICE)) { |
| 718 | dev_err(dd->dev, "dma_map_sg error\n"); |
Cyrille Pitchen | a29af93 | 2017-01-26 17:07:47 +0100 | [diff] [blame] | 719 | atmel_sha_complete(dd, -EINVAL); |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 720 | } |
| 721 | |
| 722 | ctx->flags |= SHA_FLAGS_SG; |
| 723 | |
| 724 | count = ctx->bufcnt; |
| 725 | ctx->bufcnt = 0; |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 726 | return atmel_sha_xmit_start(dd, sg_dma_address(ctx->sg), |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 727 | length, ctx->dma_addr, count, final); |
| 728 | } |
| 729 | } |
| 730 | |
| 731 | if (!dma_map_sg(dd->dev, ctx->sg, 1, DMA_TO_DEVICE)) { |
| 732 | dev_err(dd->dev, "dma_map_sg error\n"); |
Cyrille Pitchen | a29af93 | 2017-01-26 17:07:47 +0100 | [diff] [blame] | 733 | atmel_sha_complete(dd, -EINVAL); |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 734 | } |
| 735 | |
| 736 | ctx->flags |= SHA_FLAGS_SG; |
| 737 | |
| 738 | /* next call does not fail... so no unmap in the case of error */ |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 739 | return atmel_sha_xmit_start(dd, sg_dma_address(ctx->sg), length, 0, |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 740 | 0, final); |
| 741 | } |
| 742 | |
| 743 | static int atmel_sha_update_dma_stop(struct atmel_sha_dev *dd) |
| 744 | { |
| 745 | struct atmel_sha_reqctx *ctx = ahash_request_ctx(dd->req); |
| 746 | |
| 747 | if (ctx->flags & SHA_FLAGS_SG) { |
| 748 | dma_unmap_sg(dd->dev, ctx->sg, 1, DMA_TO_DEVICE); |
| 749 | if (ctx->sg->length == ctx->offset) { |
| 750 | ctx->sg = sg_next(ctx->sg); |
| 751 | if (ctx->sg) |
| 752 | ctx->offset = 0; |
| 753 | } |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 754 | if (ctx->flags & SHA_FLAGS_PAD) { |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 755 | dma_unmap_single(dd->dev, ctx->dma_addr, |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 756 | ctx->buflen + ctx->block_size, DMA_TO_DEVICE); |
| 757 | } |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 758 | } else { |
| 759 | dma_unmap_single(dd->dev, ctx->dma_addr, ctx->buflen + |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 760 | ctx->block_size, DMA_TO_DEVICE); |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 761 | } |
| 762 | |
| 763 | return 0; |
| 764 | } |
| 765 | |
| 766 | static int atmel_sha_update_req(struct atmel_sha_dev *dd) |
| 767 | { |
| 768 | struct ahash_request *req = dd->req; |
| 769 | struct atmel_sha_reqctx *ctx = ahash_request_ctx(req); |
| 770 | int err; |
| 771 | |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 772 | dev_dbg(dd->dev, "update_req: total: %u, digcnt: 0x%llx 0x%llx\n", |
| 773 | ctx->total, ctx->digcnt[1], ctx->digcnt[0]); |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 774 | |
| 775 | if (ctx->flags & SHA_FLAGS_CPU) |
| 776 | err = atmel_sha_update_cpu(dd); |
| 777 | else |
| 778 | err = atmel_sha_update_dma_start(dd); |
| 779 | |
| 780 | /* wait for dma completion before can take more data */ |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 781 | dev_dbg(dd->dev, "update: err: %d, digcnt: 0x%llx 0%llx\n", |
| 782 | err, ctx->digcnt[1], ctx->digcnt[0]); |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 783 | |
| 784 | return err; |
| 785 | } |
| 786 | |
| 787 | static int atmel_sha_final_req(struct atmel_sha_dev *dd) |
| 788 | { |
| 789 | struct ahash_request *req = dd->req; |
| 790 | struct atmel_sha_reqctx *ctx = ahash_request_ctx(req); |
| 791 | int err = 0; |
| 792 | int count; |
| 793 | |
| 794 | if (ctx->bufcnt >= ATMEL_SHA_DMA_THRESHOLD) { |
| 795 | atmel_sha_fill_padding(ctx, 0); |
| 796 | count = ctx->bufcnt; |
| 797 | ctx->bufcnt = 0; |
| 798 | err = atmel_sha_xmit_dma_map(dd, ctx, count, 1); |
| 799 | } |
| 800 | /* faster to handle last block with cpu */ |
| 801 | else { |
| 802 | atmel_sha_fill_padding(ctx, 0); |
| 803 | count = ctx->bufcnt; |
| 804 | ctx->bufcnt = 0; |
| 805 | err = atmel_sha_xmit_cpu(dd, ctx->buffer, count, 1); |
| 806 | } |
| 807 | |
| 808 | dev_dbg(dd->dev, "final_req: err: %d\n", err); |
| 809 | |
| 810 | return err; |
| 811 | } |
| 812 | |
| 813 | static void atmel_sha_copy_hash(struct ahash_request *req) |
| 814 | { |
| 815 | struct atmel_sha_reqctx *ctx = ahash_request_ctx(req); |
| 816 | u32 *hash = (u32 *)ctx->digest; |
Cyrille Pitchen | 7cee350 | 2016-01-15 15:49:34 +0100 | [diff] [blame] | 817 | unsigned int i, hashsize; |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 818 | |
Cyrille Pitchen | 7cee350 | 2016-01-15 15:49:34 +0100 | [diff] [blame] | 819 | switch (ctx->flags & SHA_FLAGS_ALGO_MASK) { |
| 820 | case SHA_FLAGS_SHA1: |
| 821 | hashsize = SHA1_DIGEST_SIZE; |
| 822 | break; |
| 823 | |
| 824 | case SHA_FLAGS_SHA224: |
| 825 | case SHA_FLAGS_SHA256: |
| 826 | hashsize = SHA256_DIGEST_SIZE; |
| 827 | break; |
| 828 | |
| 829 | case SHA_FLAGS_SHA384: |
| 830 | case SHA_FLAGS_SHA512: |
| 831 | hashsize = SHA512_DIGEST_SIZE; |
| 832 | break; |
| 833 | |
| 834 | default: |
| 835 | /* Should not happen... */ |
| 836 | return; |
| 837 | } |
| 838 | |
| 839 | for (i = 0; i < hashsize / sizeof(u32); ++i) |
| 840 | hash[i] = atmel_sha_read(ctx->dd, SHA_REG_DIGEST(i)); |
| 841 | ctx->flags |= SHA_FLAGS_RESTORE; |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 842 | } |
| 843 | |
| 844 | static void atmel_sha_copy_ready_hash(struct ahash_request *req) |
| 845 | { |
| 846 | struct atmel_sha_reqctx *ctx = ahash_request_ctx(req); |
| 847 | |
| 848 | if (!req->result) |
| 849 | return; |
| 850 | |
Cyrille Pitchen | f07ceba | 2017-01-26 17:07:49 +0100 | [diff] [blame] | 851 | switch (ctx->flags & SHA_FLAGS_ALGO_MASK) { |
| 852 | default: |
| 853 | case SHA_FLAGS_SHA1: |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 854 | memcpy(req->result, ctx->digest, SHA1_DIGEST_SIZE); |
Cyrille Pitchen | f07ceba | 2017-01-26 17:07:49 +0100 | [diff] [blame] | 855 | break; |
| 856 | |
| 857 | case SHA_FLAGS_SHA224: |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 858 | memcpy(req->result, ctx->digest, SHA224_DIGEST_SIZE); |
Cyrille Pitchen | f07ceba | 2017-01-26 17:07:49 +0100 | [diff] [blame] | 859 | break; |
| 860 | |
| 861 | case SHA_FLAGS_SHA256: |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 862 | memcpy(req->result, ctx->digest, SHA256_DIGEST_SIZE); |
Cyrille Pitchen | f07ceba | 2017-01-26 17:07:49 +0100 | [diff] [blame] | 863 | break; |
| 864 | |
| 865 | case SHA_FLAGS_SHA384: |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 866 | memcpy(req->result, ctx->digest, SHA384_DIGEST_SIZE); |
Cyrille Pitchen | f07ceba | 2017-01-26 17:07:49 +0100 | [diff] [blame] | 867 | break; |
| 868 | |
| 869 | case SHA_FLAGS_SHA512: |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 870 | memcpy(req->result, ctx->digest, SHA512_DIGEST_SIZE); |
Cyrille Pitchen | f07ceba | 2017-01-26 17:07:49 +0100 | [diff] [blame] | 871 | break; |
| 872 | } |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 873 | } |
| 874 | |
| 875 | static int atmel_sha_finish(struct ahash_request *req) |
| 876 | { |
| 877 | struct atmel_sha_reqctx *ctx = ahash_request_ctx(req); |
| 878 | struct atmel_sha_dev *dd = ctx->dd; |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 879 | |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 880 | if (ctx->digcnt[0] || ctx->digcnt[1]) |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 881 | atmel_sha_copy_ready_hash(req); |
| 882 | |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 883 | dev_dbg(dd->dev, "digcnt: 0x%llx 0x%llx, bufcnt: %d\n", ctx->digcnt[1], |
| 884 | ctx->digcnt[0], ctx->bufcnt); |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 885 | |
Rahul Pathak | 871b88a | 2015-12-14 08:44:19 +0000 | [diff] [blame] | 886 | return 0; |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 887 | } |
| 888 | |
| 889 | static void atmel_sha_finish_req(struct ahash_request *req, int err) |
| 890 | { |
| 891 | struct atmel_sha_reqctx *ctx = ahash_request_ctx(req); |
| 892 | struct atmel_sha_dev *dd = ctx->dd; |
| 893 | |
| 894 | if (!err) { |
| 895 | atmel_sha_copy_hash(req); |
| 896 | if (SHA_FLAGS_FINAL & dd->flags) |
| 897 | err = atmel_sha_finish(req); |
| 898 | } else { |
| 899 | ctx->flags |= SHA_FLAGS_ERROR; |
| 900 | } |
| 901 | |
| 902 | /* atomic operation is not needed here */ |
Cyrille Pitchen | a29af93 | 2017-01-26 17:07:47 +0100 | [diff] [blame] | 903 | (void)atmel_sha_complete(dd, err); |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 904 | } |
| 905 | |
| 906 | static int atmel_sha_hw_init(struct atmel_sha_dev *dd) |
| 907 | { |
LABBE Corentin | 9d83d29 | 2015-10-02 14:12:58 +0200 | [diff] [blame] | 908 | int err; |
| 909 | |
Cyrille Pitchen | c033042 | 2016-02-05 13:45:13 +0100 | [diff] [blame] | 910 | err = clk_enable(dd->iclk); |
LABBE Corentin | 9d83d29 | 2015-10-02 14:12:58 +0200 | [diff] [blame] | 911 | if (err) |
| 912 | return err; |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 913 | |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 914 | if (!(SHA_FLAGS_INIT & dd->flags)) { |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 915 | atmel_sha_write(dd, SHA_CR, SHA_CR_SWRST); |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 916 | dd->flags |= SHA_FLAGS_INIT; |
| 917 | dd->err = 0; |
| 918 | } |
| 919 | |
| 920 | return 0; |
| 921 | } |
| 922 | |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 923 | static inline unsigned int atmel_sha_get_version(struct atmel_sha_dev *dd) |
| 924 | { |
| 925 | return atmel_sha_read(dd, SHA_HW_VERSION) & 0x00000fff; |
| 926 | } |
| 927 | |
| 928 | static void atmel_sha_hw_version_init(struct atmel_sha_dev *dd) |
| 929 | { |
| 930 | atmel_sha_hw_init(dd); |
| 931 | |
| 932 | dd->hw_version = atmel_sha_get_version(dd); |
| 933 | |
| 934 | dev_info(dd->dev, |
| 935 | "version: 0x%x\n", dd->hw_version); |
| 936 | |
Cyrille Pitchen | c033042 | 2016-02-05 13:45:13 +0100 | [diff] [blame] | 937 | clk_disable(dd->iclk); |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 938 | } |
| 939 | |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 940 | static int atmel_sha_handle_queue(struct atmel_sha_dev *dd, |
| 941 | struct ahash_request *req) |
| 942 | { |
| 943 | struct crypto_async_request *async_req, *backlog; |
Cyrille Pitchen | a29af93 | 2017-01-26 17:07:47 +0100 | [diff] [blame] | 944 | struct atmel_sha_ctx *ctx; |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 945 | unsigned long flags; |
Cyrille Pitchen | a29af93 | 2017-01-26 17:07:47 +0100 | [diff] [blame] | 946 | bool start_async; |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 947 | int err = 0, ret = 0; |
| 948 | |
| 949 | spin_lock_irqsave(&dd->lock, flags); |
| 950 | if (req) |
| 951 | ret = ahash_enqueue_request(&dd->queue, req); |
| 952 | |
| 953 | if (SHA_FLAGS_BUSY & dd->flags) { |
| 954 | spin_unlock_irqrestore(&dd->lock, flags); |
| 955 | return ret; |
| 956 | } |
| 957 | |
| 958 | backlog = crypto_get_backlog(&dd->queue); |
| 959 | async_req = crypto_dequeue_request(&dd->queue); |
| 960 | if (async_req) |
| 961 | dd->flags |= SHA_FLAGS_BUSY; |
| 962 | |
| 963 | spin_unlock_irqrestore(&dd->lock, flags); |
| 964 | |
| 965 | if (!async_req) |
| 966 | return ret; |
| 967 | |
| 968 | if (backlog) |
| 969 | backlog->complete(backlog, -EINPROGRESS); |
| 970 | |
Cyrille Pitchen | a29af93 | 2017-01-26 17:07:47 +0100 | [diff] [blame] | 971 | ctx = crypto_tfm_ctx(async_req->tfm); |
| 972 | |
| 973 | dd->req = ahash_request_cast(async_req); |
| 974 | start_async = (dd->req != req); |
| 975 | dd->is_async = start_async; |
| 976 | |
| 977 | /* WARNING: ctx->start() MAY change dd->is_async. */ |
| 978 | err = ctx->start(dd); |
| 979 | return (start_async) ? ret : err; |
| 980 | } |
| 981 | |
Cyrille Pitchen | b5ce82a | 2017-01-26 17:07:48 +0100 | [diff] [blame] | 982 | static int atmel_sha_done(struct atmel_sha_dev *dd); |
| 983 | |
Cyrille Pitchen | a29af93 | 2017-01-26 17:07:47 +0100 | [diff] [blame] | 984 | static int atmel_sha_start(struct atmel_sha_dev *dd) |
| 985 | { |
| 986 | struct ahash_request *req = dd->req; |
| 987 | struct atmel_sha_reqctx *ctx = ahash_request_ctx(req); |
| 988 | int err; |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 989 | |
| 990 | dev_dbg(dd->dev, "handling new req, op: %lu, nbytes: %d\n", |
| 991 | ctx->op, req->nbytes); |
| 992 | |
| 993 | err = atmel_sha_hw_init(dd); |
| 994 | |
| 995 | if (err) |
| 996 | goto err1; |
| 997 | |
Cyrille Pitchen | b5ce82a | 2017-01-26 17:07:48 +0100 | [diff] [blame] | 998 | dd->resume = atmel_sha_done; |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 999 | if (ctx->op == SHA_OP_UPDATE) { |
| 1000 | err = atmel_sha_update_req(dd); |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 1001 | if (err != -EINPROGRESS && (ctx->flags & SHA_FLAGS_FINUP)) |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 1002 | /* no final() after finup() */ |
| 1003 | err = atmel_sha_final_req(dd); |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 1004 | } else if (ctx->op == SHA_OP_FINAL) { |
| 1005 | err = atmel_sha_final_req(dd); |
| 1006 | } |
| 1007 | |
| 1008 | err1: |
| 1009 | if (err != -EINPROGRESS) |
| 1010 | /* done_task will not finish it, so do it here */ |
| 1011 | atmel_sha_finish_req(req, err); |
| 1012 | |
| 1013 | dev_dbg(dd->dev, "exit, err: %d\n", err); |
| 1014 | |
Cyrille Pitchen | a29af93 | 2017-01-26 17:07:47 +0100 | [diff] [blame] | 1015 | return err; |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 1016 | } |
| 1017 | |
| 1018 | static int atmel_sha_enqueue(struct ahash_request *req, unsigned int op) |
| 1019 | { |
| 1020 | struct atmel_sha_reqctx *ctx = ahash_request_ctx(req); |
| 1021 | struct atmel_sha_ctx *tctx = crypto_tfm_ctx(req->base.tfm); |
| 1022 | struct atmel_sha_dev *dd = tctx->dd; |
| 1023 | |
| 1024 | ctx->op = op; |
| 1025 | |
| 1026 | return atmel_sha_handle_queue(dd, req); |
| 1027 | } |
| 1028 | |
| 1029 | static int atmel_sha_update(struct ahash_request *req) |
| 1030 | { |
| 1031 | struct atmel_sha_reqctx *ctx = ahash_request_ctx(req); |
| 1032 | |
| 1033 | if (!req->nbytes) |
| 1034 | return 0; |
| 1035 | |
| 1036 | ctx->total = req->nbytes; |
| 1037 | ctx->sg = req->src; |
| 1038 | ctx->offset = 0; |
| 1039 | |
| 1040 | if (ctx->flags & SHA_FLAGS_FINUP) { |
| 1041 | if (ctx->bufcnt + ctx->total < ATMEL_SHA_DMA_THRESHOLD) |
| 1042 | /* faster to use CPU for short transfers */ |
| 1043 | ctx->flags |= SHA_FLAGS_CPU; |
| 1044 | } else if (ctx->bufcnt + ctx->total < ctx->buflen) { |
| 1045 | atmel_sha_append_sg(ctx); |
| 1046 | return 0; |
| 1047 | } |
| 1048 | return atmel_sha_enqueue(req, SHA_OP_UPDATE); |
| 1049 | } |
| 1050 | |
| 1051 | static int atmel_sha_final(struct ahash_request *req) |
| 1052 | { |
| 1053 | struct atmel_sha_reqctx *ctx = ahash_request_ctx(req); |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 1054 | |
| 1055 | ctx->flags |= SHA_FLAGS_FINUP; |
| 1056 | |
| 1057 | if (ctx->flags & SHA_FLAGS_ERROR) |
| 1058 | return 0; /* uncompleted hash is not needed */ |
| 1059 | |
Cyrille Pitchen | ad84112 | 2016-02-08 16:26:49 +0100 | [diff] [blame] | 1060 | if (ctx->flags & SHA_FLAGS_PAD) |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 1061 | /* copy ready hash (+ finalize hmac) */ |
| 1062 | return atmel_sha_finish(req); |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 1063 | |
Cyrille Pitchen | ad84112 | 2016-02-08 16:26:49 +0100 | [diff] [blame] | 1064 | return atmel_sha_enqueue(req, SHA_OP_FINAL); |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 1065 | } |
| 1066 | |
| 1067 | static int atmel_sha_finup(struct ahash_request *req) |
| 1068 | { |
| 1069 | struct atmel_sha_reqctx *ctx = ahash_request_ctx(req); |
| 1070 | int err1, err2; |
| 1071 | |
| 1072 | ctx->flags |= SHA_FLAGS_FINUP; |
| 1073 | |
| 1074 | err1 = atmel_sha_update(req); |
| 1075 | if (err1 == -EINPROGRESS || err1 == -EBUSY) |
| 1076 | return err1; |
| 1077 | |
| 1078 | /* |
| 1079 | * final() has to be always called to cleanup resources |
| 1080 | * even if udpate() failed, except EINPROGRESS |
| 1081 | */ |
| 1082 | err2 = atmel_sha_final(req); |
| 1083 | |
| 1084 | return err1 ?: err2; |
| 1085 | } |
| 1086 | |
| 1087 | static int atmel_sha_digest(struct ahash_request *req) |
| 1088 | { |
| 1089 | return atmel_sha_init(req) ?: atmel_sha_finup(req); |
| 1090 | } |
| 1091 | |
Cyrille Pitchen | cc831d3 | 2016-01-29 17:04:02 +0100 | [diff] [blame] | 1092 | |
| 1093 | static int atmel_sha_export(struct ahash_request *req, void *out) |
| 1094 | { |
| 1095 | const struct atmel_sha_reqctx *ctx = ahash_request_ctx(req); |
Cyrille Pitchen | cc831d3 | 2016-01-29 17:04:02 +0100 | [diff] [blame] | 1096 | |
Cyrille Pitchen | 9c4274d | 2016-02-08 16:26:48 +0100 | [diff] [blame] | 1097 | memcpy(out, ctx, sizeof(*ctx)); |
Cyrille Pitchen | cc831d3 | 2016-01-29 17:04:02 +0100 | [diff] [blame] | 1098 | return 0; |
| 1099 | } |
| 1100 | |
| 1101 | static int atmel_sha_import(struct ahash_request *req, const void *in) |
| 1102 | { |
| 1103 | struct atmel_sha_reqctx *ctx = ahash_request_ctx(req); |
Cyrille Pitchen | cc831d3 | 2016-01-29 17:04:02 +0100 | [diff] [blame] | 1104 | |
Cyrille Pitchen | 9c4274d | 2016-02-08 16:26:48 +0100 | [diff] [blame] | 1105 | memcpy(ctx, in, sizeof(*ctx)); |
Cyrille Pitchen | cc831d3 | 2016-01-29 17:04:02 +0100 | [diff] [blame] | 1106 | return 0; |
| 1107 | } |
| 1108 | |
Svenning Sørensen | be95f0f | 2014-12-05 01:18:57 +0100 | [diff] [blame] | 1109 | static int atmel_sha_cra_init(struct crypto_tfm *tfm) |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 1110 | { |
Cyrille Pitchen | a29af93 | 2017-01-26 17:07:47 +0100 | [diff] [blame] | 1111 | struct atmel_sha_ctx *ctx = crypto_tfm_ctx(tfm); |
| 1112 | |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 1113 | crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm), |
Cyrille Pitchen | 9c4274d | 2016-02-08 16:26:48 +0100 | [diff] [blame] | 1114 | sizeof(struct atmel_sha_reqctx)); |
Cyrille Pitchen | a29af93 | 2017-01-26 17:07:47 +0100 | [diff] [blame] | 1115 | ctx->start = atmel_sha_start; |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 1116 | |
| 1117 | return 0; |
| 1118 | } |
| 1119 | |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 1120 | static struct ahash_alg sha_1_256_algs[] = { |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 1121 | { |
| 1122 | .init = atmel_sha_init, |
| 1123 | .update = atmel_sha_update, |
| 1124 | .final = atmel_sha_final, |
| 1125 | .finup = atmel_sha_finup, |
| 1126 | .digest = atmel_sha_digest, |
Cyrille Pitchen | cc831d3 | 2016-01-29 17:04:02 +0100 | [diff] [blame] | 1127 | .export = atmel_sha_export, |
| 1128 | .import = atmel_sha_import, |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 1129 | .halg = { |
| 1130 | .digestsize = SHA1_DIGEST_SIZE, |
Cyrille Pitchen | 9c4274d | 2016-02-08 16:26:48 +0100 | [diff] [blame] | 1131 | .statesize = sizeof(struct atmel_sha_reqctx), |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 1132 | .base = { |
| 1133 | .cra_name = "sha1", |
| 1134 | .cra_driver_name = "atmel-sha1", |
| 1135 | .cra_priority = 100, |
Svenning Sørensen | be95f0f | 2014-12-05 01:18:57 +0100 | [diff] [blame] | 1136 | .cra_flags = CRYPTO_ALG_ASYNC, |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 1137 | .cra_blocksize = SHA1_BLOCK_SIZE, |
| 1138 | .cra_ctxsize = sizeof(struct atmel_sha_ctx), |
| 1139 | .cra_alignmask = 0, |
| 1140 | .cra_module = THIS_MODULE, |
| 1141 | .cra_init = atmel_sha_cra_init, |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 1142 | } |
| 1143 | } |
| 1144 | }, |
| 1145 | { |
| 1146 | .init = atmel_sha_init, |
| 1147 | .update = atmel_sha_update, |
| 1148 | .final = atmel_sha_final, |
| 1149 | .finup = atmel_sha_finup, |
| 1150 | .digest = atmel_sha_digest, |
Cyrille Pitchen | cc831d3 | 2016-01-29 17:04:02 +0100 | [diff] [blame] | 1151 | .export = atmel_sha_export, |
| 1152 | .import = atmel_sha_import, |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 1153 | .halg = { |
| 1154 | .digestsize = SHA256_DIGEST_SIZE, |
Cyrille Pitchen | 9c4274d | 2016-02-08 16:26:48 +0100 | [diff] [blame] | 1155 | .statesize = sizeof(struct atmel_sha_reqctx), |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 1156 | .base = { |
| 1157 | .cra_name = "sha256", |
| 1158 | .cra_driver_name = "atmel-sha256", |
| 1159 | .cra_priority = 100, |
Svenning Sørensen | be95f0f | 2014-12-05 01:18:57 +0100 | [diff] [blame] | 1160 | .cra_flags = CRYPTO_ALG_ASYNC, |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 1161 | .cra_blocksize = SHA256_BLOCK_SIZE, |
| 1162 | .cra_ctxsize = sizeof(struct atmel_sha_ctx), |
| 1163 | .cra_alignmask = 0, |
| 1164 | .cra_module = THIS_MODULE, |
| 1165 | .cra_init = atmel_sha_cra_init, |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 1166 | } |
| 1167 | } |
| 1168 | }, |
| 1169 | }; |
| 1170 | |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 1171 | static struct ahash_alg sha_224_alg = { |
| 1172 | .init = atmel_sha_init, |
| 1173 | .update = atmel_sha_update, |
| 1174 | .final = atmel_sha_final, |
| 1175 | .finup = atmel_sha_finup, |
| 1176 | .digest = atmel_sha_digest, |
Cyrille Pitchen | cc831d3 | 2016-01-29 17:04:02 +0100 | [diff] [blame] | 1177 | .export = atmel_sha_export, |
| 1178 | .import = atmel_sha_import, |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 1179 | .halg = { |
| 1180 | .digestsize = SHA224_DIGEST_SIZE, |
Cyrille Pitchen | 9c4274d | 2016-02-08 16:26:48 +0100 | [diff] [blame] | 1181 | .statesize = sizeof(struct atmel_sha_reqctx), |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 1182 | .base = { |
| 1183 | .cra_name = "sha224", |
| 1184 | .cra_driver_name = "atmel-sha224", |
| 1185 | .cra_priority = 100, |
Svenning Sørensen | be95f0f | 2014-12-05 01:18:57 +0100 | [diff] [blame] | 1186 | .cra_flags = CRYPTO_ALG_ASYNC, |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 1187 | .cra_blocksize = SHA224_BLOCK_SIZE, |
| 1188 | .cra_ctxsize = sizeof(struct atmel_sha_ctx), |
| 1189 | .cra_alignmask = 0, |
| 1190 | .cra_module = THIS_MODULE, |
| 1191 | .cra_init = atmel_sha_cra_init, |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 1192 | } |
| 1193 | } |
| 1194 | }; |
| 1195 | |
| 1196 | static struct ahash_alg sha_384_512_algs[] = { |
| 1197 | { |
| 1198 | .init = atmel_sha_init, |
| 1199 | .update = atmel_sha_update, |
| 1200 | .final = atmel_sha_final, |
| 1201 | .finup = atmel_sha_finup, |
| 1202 | .digest = atmel_sha_digest, |
Cyrille Pitchen | cc831d3 | 2016-01-29 17:04:02 +0100 | [diff] [blame] | 1203 | .export = atmel_sha_export, |
| 1204 | .import = atmel_sha_import, |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 1205 | .halg = { |
| 1206 | .digestsize = SHA384_DIGEST_SIZE, |
Cyrille Pitchen | 9c4274d | 2016-02-08 16:26:48 +0100 | [diff] [blame] | 1207 | .statesize = sizeof(struct atmel_sha_reqctx), |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 1208 | .base = { |
| 1209 | .cra_name = "sha384", |
| 1210 | .cra_driver_name = "atmel-sha384", |
| 1211 | .cra_priority = 100, |
Svenning Sørensen | be95f0f | 2014-12-05 01:18:57 +0100 | [diff] [blame] | 1212 | .cra_flags = CRYPTO_ALG_ASYNC, |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 1213 | .cra_blocksize = SHA384_BLOCK_SIZE, |
| 1214 | .cra_ctxsize = sizeof(struct atmel_sha_ctx), |
| 1215 | .cra_alignmask = 0x3, |
| 1216 | .cra_module = THIS_MODULE, |
| 1217 | .cra_init = atmel_sha_cra_init, |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 1218 | } |
| 1219 | } |
| 1220 | }, |
| 1221 | { |
| 1222 | .init = atmel_sha_init, |
| 1223 | .update = atmel_sha_update, |
| 1224 | .final = atmel_sha_final, |
| 1225 | .finup = atmel_sha_finup, |
| 1226 | .digest = atmel_sha_digest, |
Cyrille Pitchen | cc831d3 | 2016-01-29 17:04:02 +0100 | [diff] [blame] | 1227 | .export = atmel_sha_export, |
| 1228 | .import = atmel_sha_import, |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 1229 | .halg = { |
| 1230 | .digestsize = SHA512_DIGEST_SIZE, |
Cyrille Pitchen | 9c4274d | 2016-02-08 16:26:48 +0100 | [diff] [blame] | 1231 | .statesize = sizeof(struct atmel_sha_reqctx), |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 1232 | .base = { |
| 1233 | .cra_name = "sha512", |
| 1234 | .cra_driver_name = "atmel-sha512", |
| 1235 | .cra_priority = 100, |
Svenning Sørensen | be95f0f | 2014-12-05 01:18:57 +0100 | [diff] [blame] | 1236 | .cra_flags = CRYPTO_ALG_ASYNC, |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 1237 | .cra_blocksize = SHA512_BLOCK_SIZE, |
| 1238 | .cra_ctxsize = sizeof(struct atmel_sha_ctx), |
| 1239 | .cra_alignmask = 0x3, |
| 1240 | .cra_module = THIS_MODULE, |
| 1241 | .cra_init = atmel_sha_cra_init, |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 1242 | } |
| 1243 | } |
| 1244 | }, |
| 1245 | }; |
| 1246 | |
Cyrille Pitchen | f56809c | 2016-01-15 15:49:32 +0100 | [diff] [blame] | 1247 | static void atmel_sha_queue_task(unsigned long data) |
| 1248 | { |
| 1249 | struct atmel_sha_dev *dd = (struct atmel_sha_dev *)data; |
| 1250 | |
| 1251 | atmel_sha_handle_queue(dd, NULL); |
| 1252 | } |
| 1253 | |
Cyrille Pitchen | b5ce82a | 2017-01-26 17:07:48 +0100 | [diff] [blame] | 1254 | static int atmel_sha_done(struct atmel_sha_dev *dd) |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 1255 | { |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 1256 | int err = 0; |
| 1257 | |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 1258 | if (SHA_FLAGS_CPU & dd->flags) { |
| 1259 | if (SHA_FLAGS_OUTPUT_READY & dd->flags) { |
| 1260 | dd->flags &= ~SHA_FLAGS_OUTPUT_READY; |
| 1261 | goto finish; |
| 1262 | } |
| 1263 | } else if (SHA_FLAGS_DMA_READY & dd->flags) { |
| 1264 | if (SHA_FLAGS_DMA_ACTIVE & dd->flags) { |
| 1265 | dd->flags &= ~SHA_FLAGS_DMA_ACTIVE; |
| 1266 | atmel_sha_update_dma_stop(dd); |
| 1267 | if (dd->err) { |
| 1268 | err = dd->err; |
| 1269 | goto finish; |
| 1270 | } |
| 1271 | } |
| 1272 | if (SHA_FLAGS_OUTPUT_READY & dd->flags) { |
| 1273 | /* hash or semi-hash ready */ |
| 1274 | dd->flags &= ~(SHA_FLAGS_DMA_READY | |
| 1275 | SHA_FLAGS_OUTPUT_READY); |
| 1276 | err = atmel_sha_update_dma_start(dd); |
| 1277 | if (err != -EINPROGRESS) |
| 1278 | goto finish; |
| 1279 | } |
| 1280 | } |
Cyrille Pitchen | b5ce82a | 2017-01-26 17:07:48 +0100 | [diff] [blame] | 1281 | return err; |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 1282 | |
| 1283 | finish: |
| 1284 | /* finish curent request */ |
| 1285 | atmel_sha_finish_req(dd->req, err); |
Cyrille Pitchen | b5ce82a | 2017-01-26 17:07:48 +0100 | [diff] [blame] | 1286 | |
| 1287 | return err; |
| 1288 | } |
| 1289 | |
| 1290 | static void atmel_sha_done_task(unsigned long data) |
| 1291 | { |
| 1292 | struct atmel_sha_dev *dd = (struct atmel_sha_dev *)data; |
| 1293 | |
| 1294 | dd->is_async = true; |
| 1295 | (void)dd->resume(dd); |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 1296 | } |
| 1297 | |
| 1298 | static irqreturn_t atmel_sha_irq(int irq, void *dev_id) |
| 1299 | { |
| 1300 | struct atmel_sha_dev *sha_dd = dev_id; |
| 1301 | u32 reg; |
| 1302 | |
| 1303 | reg = atmel_sha_read(sha_dd, SHA_ISR); |
| 1304 | if (reg & atmel_sha_read(sha_dd, SHA_IMR)) { |
| 1305 | atmel_sha_write(sha_dd, SHA_IDR, reg); |
| 1306 | if (SHA_FLAGS_BUSY & sha_dd->flags) { |
| 1307 | sha_dd->flags |= SHA_FLAGS_OUTPUT_READY; |
| 1308 | if (!(SHA_FLAGS_CPU & sha_dd->flags)) |
| 1309 | sha_dd->flags |= SHA_FLAGS_DMA_READY; |
| 1310 | tasklet_schedule(&sha_dd->done_task); |
| 1311 | } else { |
| 1312 | dev_warn(sha_dd->dev, "SHA interrupt when no active requests.\n"); |
| 1313 | } |
| 1314 | return IRQ_HANDLED; |
| 1315 | } |
| 1316 | |
| 1317 | return IRQ_NONE; |
| 1318 | } |
| 1319 | |
| 1320 | static void atmel_sha_unregister_algs(struct atmel_sha_dev *dd) |
| 1321 | { |
| 1322 | int i; |
| 1323 | |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 1324 | for (i = 0; i < ARRAY_SIZE(sha_1_256_algs); i++) |
| 1325 | crypto_unregister_ahash(&sha_1_256_algs[i]); |
| 1326 | |
| 1327 | if (dd->caps.has_sha224) |
| 1328 | crypto_unregister_ahash(&sha_224_alg); |
| 1329 | |
| 1330 | if (dd->caps.has_sha_384_512) { |
| 1331 | for (i = 0; i < ARRAY_SIZE(sha_384_512_algs); i++) |
| 1332 | crypto_unregister_ahash(&sha_384_512_algs[i]); |
| 1333 | } |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 1334 | } |
| 1335 | |
| 1336 | static int atmel_sha_register_algs(struct atmel_sha_dev *dd) |
| 1337 | { |
| 1338 | int err, i, j; |
| 1339 | |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 1340 | for (i = 0; i < ARRAY_SIZE(sha_1_256_algs); i++) { |
| 1341 | err = crypto_register_ahash(&sha_1_256_algs[i]); |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 1342 | if (err) |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 1343 | goto err_sha_1_256_algs; |
| 1344 | } |
| 1345 | |
| 1346 | if (dd->caps.has_sha224) { |
| 1347 | err = crypto_register_ahash(&sha_224_alg); |
| 1348 | if (err) |
| 1349 | goto err_sha_224_algs; |
| 1350 | } |
| 1351 | |
| 1352 | if (dd->caps.has_sha_384_512) { |
| 1353 | for (i = 0; i < ARRAY_SIZE(sha_384_512_algs); i++) { |
| 1354 | err = crypto_register_ahash(&sha_384_512_algs[i]); |
| 1355 | if (err) |
| 1356 | goto err_sha_384_512_algs; |
| 1357 | } |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 1358 | } |
| 1359 | |
| 1360 | return 0; |
| 1361 | |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 1362 | err_sha_384_512_algs: |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 1363 | for (j = 0; j < i; j++) |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 1364 | crypto_unregister_ahash(&sha_384_512_algs[j]); |
| 1365 | crypto_unregister_ahash(&sha_224_alg); |
| 1366 | err_sha_224_algs: |
| 1367 | i = ARRAY_SIZE(sha_1_256_algs); |
| 1368 | err_sha_1_256_algs: |
| 1369 | for (j = 0; j < i; j++) |
| 1370 | crypto_unregister_ahash(&sha_1_256_algs[j]); |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 1371 | |
| 1372 | return err; |
| 1373 | } |
| 1374 | |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 1375 | static bool atmel_sha_filter(struct dma_chan *chan, void *slave) |
| 1376 | { |
| 1377 | struct at_dma_slave *sl = slave; |
| 1378 | |
| 1379 | if (sl && sl->dma_dev == chan->device->dev) { |
| 1380 | chan->private = sl; |
| 1381 | return true; |
| 1382 | } else { |
| 1383 | return false; |
| 1384 | } |
| 1385 | } |
| 1386 | |
| 1387 | static int atmel_sha_dma_init(struct atmel_sha_dev *dd, |
| 1388 | struct crypto_platform_data *pdata) |
| 1389 | { |
| 1390 | int err = -ENOMEM; |
| 1391 | dma_cap_mask_t mask_in; |
| 1392 | |
Nicolas Ferre | abfe7ae | 2013-10-15 15:36:34 +0200 | [diff] [blame] | 1393 | /* Try to grab DMA channel */ |
| 1394 | dma_cap_zero(mask_in); |
| 1395 | dma_cap_set(DMA_SLAVE, mask_in); |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 1396 | |
Nicolas Ferre | abfe7ae | 2013-10-15 15:36:34 +0200 | [diff] [blame] | 1397 | dd->dma_lch_in.chan = dma_request_slave_channel_compat(mask_in, |
| 1398 | atmel_sha_filter, &pdata->dma_slave->rxdata, dd->dev, "tx"); |
| 1399 | if (!dd->dma_lch_in.chan) { |
| 1400 | dev_warn(dd->dev, "no DMA channel available\n"); |
| 1401 | return err; |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 1402 | } |
| 1403 | |
Nicolas Ferre | abfe7ae | 2013-10-15 15:36:34 +0200 | [diff] [blame] | 1404 | dd->dma_lch_in.dma_conf.direction = DMA_MEM_TO_DEV; |
| 1405 | dd->dma_lch_in.dma_conf.dst_addr = dd->phys_base + |
| 1406 | SHA_REG_DIN(0); |
| 1407 | dd->dma_lch_in.dma_conf.src_maxburst = 1; |
| 1408 | dd->dma_lch_in.dma_conf.src_addr_width = |
| 1409 | DMA_SLAVE_BUSWIDTH_4_BYTES; |
| 1410 | dd->dma_lch_in.dma_conf.dst_maxburst = 1; |
| 1411 | dd->dma_lch_in.dma_conf.dst_addr_width = |
| 1412 | DMA_SLAVE_BUSWIDTH_4_BYTES; |
| 1413 | dd->dma_lch_in.dma_conf.device_fc = false; |
| 1414 | |
| 1415 | return 0; |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 1416 | } |
| 1417 | |
| 1418 | static void atmel_sha_dma_cleanup(struct atmel_sha_dev *dd) |
| 1419 | { |
| 1420 | dma_release_channel(dd->dma_lch_in.chan); |
| 1421 | } |
| 1422 | |
| 1423 | static void atmel_sha_get_cap(struct atmel_sha_dev *dd) |
| 1424 | { |
| 1425 | |
| 1426 | dd->caps.has_dma = 0; |
| 1427 | dd->caps.has_dualbuff = 0; |
| 1428 | dd->caps.has_sha224 = 0; |
| 1429 | dd->caps.has_sha_384_512 = 0; |
Cyrille Pitchen | 7cee350 | 2016-01-15 15:49:34 +0100 | [diff] [blame] | 1430 | dd->caps.has_uihv = 0; |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 1431 | |
| 1432 | /* keep only major version number */ |
| 1433 | switch (dd->hw_version & 0xff0) { |
Cyrille Pitchen | 507c5cc | 2016-01-15 15:49:33 +0100 | [diff] [blame] | 1434 | case 0x510: |
| 1435 | dd->caps.has_dma = 1; |
| 1436 | dd->caps.has_dualbuff = 1; |
| 1437 | dd->caps.has_sha224 = 1; |
| 1438 | dd->caps.has_sha_384_512 = 1; |
Cyrille Pitchen | 7cee350 | 2016-01-15 15:49:34 +0100 | [diff] [blame] | 1439 | dd->caps.has_uihv = 1; |
Cyrille Pitchen | 507c5cc | 2016-01-15 15:49:33 +0100 | [diff] [blame] | 1440 | break; |
Leilei Zhao | 141824d | 2015-04-07 17:45:03 +0800 | [diff] [blame] | 1441 | case 0x420: |
| 1442 | dd->caps.has_dma = 1; |
| 1443 | dd->caps.has_dualbuff = 1; |
| 1444 | dd->caps.has_sha224 = 1; |
| 1445 | dd->caps.has_sha_384_512 = 1; |
Cyrille Pitchen | 7cee350 | 2016-01-15 15:49:34 +0100 | [diff] [blame] | 1446 | dd->caps.has_uihv = 1; |
Leilei Zhao | 141824d | 2015-04-07 17:45:03 +0800 | [diff] [blame] | 1447 | break; |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 1448 | case 0x410: |
| 1449 | dd->caps.has_dma = 1; |
| 1450 | dd->caps.has_dualbuff = 1; |
| 1451 | dd->caps.has_sha224 = 1; |
| 1452 | dd->caps.has_sha_384_512 = 1; |
| 1453 | break; |
| 1454 | case 0x400: |
| 1455 | dd->caps.has_dma = 1; |
| 1456 | dd->caps.has_dualbuff = 1; |
| 1457 | dd->caps.has_sha224 = 1; |
| 1458 | break; |
| 1459 | case 0x320: |
| 1460 | break; |
| 1461 | default: |
| 1462 | dev_warn(dd->dev, |
| 1463 | "Unmanaged sha version, set minimum capabilities\n"); |
| 1464 | break; |
| 1465 | } |
| 1466 | } |
| 1467 | |
Nicolas Ferre | abfe7ae | 2013-10-15 15:36:34 +0200 | [diff] [blame] | 1468 | #if defined(CONFIG_OF) |
| 1469 | static const struct of_device_id atmel_sha_dt_ids[] = { |
| 1470 | { .compatible = "atmel,at91sam9g46-sha" }, |
| 1471 | { /* sentinel */ } |
| 1472 | }; |
| 1473 | |
| 1474 | MODULE_DEVICE_TABLE(of, atmel_sha_dt_ids); |
| 1475 | |
| 1476 | static struct crypto_platform_data *atmel_sha_of_init(struct platform_device *pdev) |
| 1477 | { |
| 1478 | struct device_node *np = pdev->dev.of_node; |
| 1479 | struct crypto_platform_data *pdata; |
| 1480 | |
| 1481 | if (!np) { |
| 1482 | dev_err(&pdev->dev, "device node not found\n"); |
| 1483 | return ERR_PTR(-EINVAL); |
| 1484 | } |
| 1485 | |
| 1486 | pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL); |
| 1487 | if (!pdata) { |
| 1488 | dev_err(&pdev->dev, "could not allocate memory for pdata\n"); |
| 1489 | return ERR_PTR(-ENOMEM); |
| 1490 | } |
| 1491 | |
| 1492 | pdata->dma_slave = devm_kzalloc(&pdev->dev, |
| 1493 | sizeof(*(pdata->dma_slave)), |
| 1494 | GFP_KERNEL); |
| 1495 | if (!pdata->dma_slave) { |
| 1496 | dev_err(&pdev->dev, "could not allocate memory for dma_slave\n"); |
Nicolas Ferre | abfe7ae | 2013-10-15 15:36:34 +0200 | [diff] [blame] | 1497 | return ERR_PTR(-ENOMEM); |
| 1498 | } |
| 1499 | |
| 1500 | return pdata; |
| 1501 | } |
| 1502 | #else /* CONFIG_OF */ |
| 1503 | static inline struct crypto_platform_data *atmel_sha_of_init(struct platform_device *dev) |
| 1504 | { |
| 1505 | return ERR_PTR(-EINVAL); |
| 1506 | } |
| 1507 | #endif |
| 1508 | |
Greg Kroah-Hartman | 49cfe4d | 2012-12-21 13:14:09 -0800 | [diff] [blame] | 1509 | static int atmel_sha_probe(struct platform_device *pdev) |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 1510 | { |
| 1511 | struct atmel_sha_dev *sha_dd; |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 1512 | struct crypto_platform_data *pdata; |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 1513 | struct device *dev = &pdev->dev; |
| 1514 | struct resource *sha_res; |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 1515 | int err; |
| 1516 | |
LABBE Corentin | b0e8b34 | 2015-10-12 19:47:03 +0200 | [diff] [blame] | 1517 | sha_dd = devm_kzalloc(&pdev->dev, sizeof(*sha_dd), GFP_KERNEL); |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 1518 | if (sha_dd == NULL) { |
| 1519 | dev_err(dev, "unable to alloc data struct.\n"); |
| 1520 | err = -ENOMEM; |
| 1521 | goto sha_dd_err; |
| 1522 | } |
| 1523 | |
| 1524 | sha_dd->dev = dev; |
| 1525 | |
| 1526 | platform_set_drvdata(pdev, sha_dd); |
| 1527 | |
| 1528 | INIT_LIST_HEAD(&sha_dd->list); |
Leilei Zhao | 62728e8 | 2015-04-07 17:45:06 +0800 | [diff] [blame] | 1529 | spin_lock_init(&sha_dd->lock); |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 1530 | |
| 1531 | tasklet_init(&sha_dd->done_task, atmel_sha_done_task, |
| 1532 | (unsigned long)sha_dd); |
Cyrille Pitchen | f56809c | 2016-01-15 15:49:32 +0100 | [diff] [blame] | 1533 | tasklet_init(&sha_dd->queue_task, atmel_sha_queue_task, |
| 1534 | (unsigned long)sha_dd); |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 1535 | |
| 1536 | crypto_init_queue(&sha_dd->queue, ATMEL_SHA_QUEUE_LENGTH); |
| 1537 | |
| 1538 | sha_dd->irq = -1; |
| 1539 | |
| 1540 | /* Get the base address */ |
| 1541 | sha_res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 1542 | if (!sha_res) { |
| 1543 | dev_err(dev, "no MEM resource info\n"); |
| 1544 | err = -ENODEV; |
| 1545 | goto res_err; |
| 1546 | } |
| 1547 | sha_dd->phys_base = sha_res->start; |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 1548 | |
| 1549 | /* Get the IRQ */ |
| 1550 | sha_dd->irq = platform_get_irq(pdev, 0); |
| 1551 | if (sha_dd->irq < 0) { |
| 1552 | dev_err(dev, "no IRQ resource info\n"); |
| 1553 | err = sha_dd->irq; |
| 1554 | goto res_err; |
| 1555 | } |
| 1556 | |
LABBE Corentin | b0e8b34 | 2015-10-12 19:47:03 +0200 | [diff] [blame] | 1557 | err = devm_request_irq(&pdev->dev, sha_dd->irq, atmel_sha_irq, |
| 1558 | IRQF_SHARED, "atmel-sha", sha_dd); |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 1559 | if (err) { |
| 1560 | dev_err(dev, "unable to request sha irq.\n"); |
| 1561 | goto res_err; |
| 1562 | } |
| 1563 | |
| 1564 | /* Initializing the clock */ |
LABBE Corentin | b0e8b34 | 2015-10-12 19:47:03 +0200 | [diff] [blame] | 1565 | sha_dd->iclk = devm_clk_get(&pdev->dev, "sha_clk"); |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 1566 | if (IS_ERR(sha_dd->iclk)) { |
Colin Ian King | be20835 | 2015-02-28 20:40:10 +0000 | [diff] [blame] | 1567 | dev_err(dev, "clock initialization failed.\n"); |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 1568 | err = PTR_ERR(sha_dd->iclk); |
LABBE Corentin | b0e8b34 | 2015-10-12 19:47:03 +0200 | [diff] [blame] | 1569 | goto res_err; |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 1570 | } |
| 1571 | |
LABBE Corentin | b0e8b34 | 2015-10-12 19:47:03 +0200 | [diff] [blame] | 1572 | sha_dd->io_base = devm_ioremap_resource(&pdev->dev, sha_res); |
Vladimir Zapolskiy | 9b52d55 | 2016-03-06 03:21:52 +0200 | [diff] [blame] | 1573 | if (IS_ERR(sha_dd->io_base)) { |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 1574 | dev_err(dev, "can't ioremap\n"); |
Vladimir Zapolskiy | 9b52d55 | 2016-03-06 03:21:52 +0200 | [diff] [blame] | 1575 | err = PTR_ERR(sha_dd->io_base); |
LABBE Corentin | b0e8b34 | 2015-10-12 19:47:03 +0200 | [diff] [blame] | 1576 | goto res_err; |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 1577 | } |
| 1578 | |
Cyrille Pitchen | c033042 | 2016-02-05 13:45:13 +0100 | [diff] [blame] | 1579 | err = clk_prepare(sha_dd->iclk); |
| 1580 | if (err) |
| 1581 | goto res_err; |
| 1582 | |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 1583 | atmel_sha_hw_version_init(sha_dd); |
| 1584 | |
| 1585 | atmel_sha_get_cap(sha_dd); |
| 1586 | |
| 1587 | if (sha_dd->caps.has_dma) { |
| 1588 | pdata = pdev->dev.platform_data; |
| 1589 | if (!pdata) { |
Nicolas Ferre | abfe7ae | 2013-10-15 15:36:34 +0200 | [diff] [blame] | 1590 | pdata = atmel_sha_of_init(pdev); |
| 1591 | if (IS_ERR(pdata)) { |
| 1592 | dev_err(&pdev->dev, "platform data not available\n"); |
| 1593 | err = PTR_ERR(pdata); |
Cyrille Pitchen | c033042 | 2016-02-05 13:45:13 +0100 | [diff] [blame] | 1594 | goto iclk_unprepare; |
Nicolas Ferre | abfe7ae | 2013-10-15 15:36:34 +0200 | [diff] [blame] | 1595 | } |
| 1596 | } |
| 1597 | if (!pdata->dma_slave) { |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 1598 | err = -ENXIO; |
Cyrille Pitchen | c033042 | 2016-02-05 13:45:13 +0100 | [diff] [blame] | 1599 | goto iclk_unprepare; |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 1600 | } |
| 1601 | err = atmel_sha_dma_init(sha_dd, pdata); |
| 1602 | if (err) |
| 1603 | goto err_sha_dma; |
Nicolas Ferre | abfe7ae | 2013-10-15 15:36:34 +0200 | [diff] [blame] | 1604 | |
| 1605 | dev_info(dev, "using %s for DMA transfers\n", |
| 1606 | dma_chan_name(sha_dd->dma_lch_in.chan)); |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 1607 | } |
| 1608 | |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 1609 | spin_lock(&atmel_sha.lock); |
| 1610 | list_add_tail(&sha_dd->list, &atmel_sha.dev_list); |
| 1611 | spin_unlock(&atmel_sha.lock); |
| 1612 | |
| 1613 | err = atmel_sha_register_algs(sha_dd); |
| 1614 | if (err) |
| 1615 | goto err_algs; |
| 1616 | |
Nicolas Ferre | 1ca5b7d | 2013-10-15 16:37:44 +0200 | [diff] [blame] | 1617 | dev_info(dev, "Atmel SHA1/SHA256%s%s\n", |
| 1618 | sha_dd->caps.has_sha224 ? "/SHA224" : "", |
| 1619 | sha_dd->caps.has_sha_384_512 ? "/SHA384/SHA512" : ""); |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 1620 | |
| 1621 | return 0; |
| 1622 | |
| 1623 | err_algs: |
| 1624 | spin_lock(&atmel_sha.lock); |
| 1625 | list_del(&sha_dd->list); |
| 1626 | spin_unlock(&atmel_sha.lock); |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 1627 | if (sha_dd->caps.has_dma) |
| 1628 | atmel_sha_dma_cleanup(sha_dd); |
| 1629 | err_sha_dma: |
Cyrille Pitchen | c033042 | 2016-02-05 13:45:13 +0100 | [diff] [blame] | 1630 | iclk_unprepare: |
| 1631 | clk_unprepare(sha_dd->iclk); |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 1632 | res_err: |
Cyrille Pitchen | f56809c | 2016-01-15 15:49:32 +0100 | [diff] [blame] | 1633 | tasklet_kill(&sha_dd->queue_task); |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 1634 | tasklet_kill(&sha_dd->done_task); |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 1635 | sha_dd_err: |
| 1636 | dev_err(dev, "initialization failed.\n"); |
| 1637 | |
| 1638 | return err; |
| 1639 | } |
| 1640 | |
Greg Kroah-Hartman | 49cfe4d | 2012-12-21 13:14:09 -0800 | [diff] [blame] | 1641 | static int atmel_sha_remove(struct platform_device *pdev) |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 1642 | { |
| 1643 | static struct atmel_sha_dev *sha_dd; |
| 1644 | |
| 1645 | sha_dd = platform_get_drvdata(pdev); |
| 1646 | if (!sha_dd) |
| 1647 | return -ENODEV; |
| 1648 | spin_lock(&atmel_sha.lock); |
| 1649 | list_del(&sha_dd->list); |
| 1650 | spin_unlock(&atmel_sha.lock); |
| 1651 | |
| 1652 | atmel_sha_unregister_algs(sha_dd); |
| 1653 | |
Cyrille Pitchen | f56809c | 2016-01-15 15:49:32 +0100 | [diff] [blame] | 1654 | tasklet_kill(&sha_dd->queue_task); |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 1655 | tasklet_kill(&sha_dd->done_task); |
| 1656 | |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 1657 | if (sha_dd->caps.has_dma) |
| 1658 | atmel_sha_dma_cleanup(sha_dd); |
| 1659 | |
Cyrille Pitchen | c033042 | 2016-02-05 13:45:13 +0100 | [diff] [blame] | 1660 | clk_unprepare(sha_dd->iclk); |
| 1661 | |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 1662 | return 0; |
| 1663 | } |
| 1664 | |
| 1665 | static struct platform_driver atmel_sha_driver = { |
| 1666 | .probe = atmel_sha_probe, |
Greg Kroah-Hartman | 49cfe4d | 2012-12-21 13:14:09 -0800 | [diff] [blame] | 1667 | .remove = atmel_sha_remove, |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 1668 | .driver = { |
| 1669 | .name = "atmel_sha", |
Nicolas Ferre | abfe7ae | 2013-10-15 15:36:34 +0200 | [diff] [blame] | 1670 | .of_match_table = of_match_ptr(atmel_sha_dt_ids), |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 1671 | }, |
| 1672 | }; |
| 1673 | |
| 1674 | module_platform_driver(atmel_sha_driver); |
| 1675 | |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 1676 | MODULE_DESCRIPTION("Atmel SHA (1/256/224/384/512) hw acceleration support."); |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 1677 | MODULE_LICENSE("GPL v2"); |
| 1678 | MODULE_AUTHOR("Nicolas Royer - Eukréa Electromatique"); |