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