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