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 | 81d8750 | 2017-01-26 17:07:54 +0100 | [diff] [blame^] | 54 | /* bits[11:8] are reserved. */ |
Cyrille Pitchen | f07ceba | 2017-01-26 17:07:49 +0100 | [diff] [blame] | 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 |
Cyrille Pitchen | 81d8750 | 2017-01-26 17:07:54 +0100 | [diff] [blame^] | 61 | #define SHA_FLAGS_HMAC SHA_MR_HMAC |
| 62 | #define SHA_FLAGS_HMAC_SHA1 (SHA_FLAGS_HMAC | SHA_FLAGS_SHA1) |
| 63 | #define SHA_FLAGS_HMAC_SHA256 (SHA_FLAGS_HMAC | SHA_FLAGS_SHA256) |
| 64 | #define SHA_FLAGS_HMAC_SHA384 (SHA_FLAGS_HMAC | SHA_FLAGS_SHA384) |
| 65 | #define SHA_FLAGS_HMAC_SHA512 (SHA_FLAGS_HMAC | SHA_FLAGS_SHA512) |
| 66 | #define SHA_FLAGS_HMAC_SHA224 (SHA_FLAGS_HMAC | SHA_FLAGS_SHA224) |
| 67 | #define SHA_FLAGS_MODE_MASK (SHA_FLAGS_HMAC | SHA_FLAGS_ALGO_MASK) |
Cyrille Pitchen | f07ceba | 2017-01-26 17:07:49 +0100 | [diff] [blame] | 68 | |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 69 | #define SHA_FLAGS_FINUP BIT(16) |
| 70 | #define SHA_FLAGS_SG BIT(17) |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 71 | #define SHA_FLAGS_ERROR BIT(23) |
| 72 | #define SHA_FLAGS_PAD BIT(24) |
Cyrille Pitchen | 7cee350 | 2016-01-15 15:49:34 +0100 | [diff] [blame] | 73 | #define SHA_FLAGS_RESTORE BIT(25) |
Cyrille Pitchen | eec12f6 | 2017-01-26 17:07:52 +0100 | [diff] [blame] | 74 | #define SHA_FLAGS_IDATAR0 BIT(26) |
| 75 | #define SHA_FLAGS_WAIT_DATARDY BIT(27) |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 76 | |
Cyrille Pitchen | 81d8750 | 2017-01-26 17:07:54 +0100 | [diff] [blame^] | 77 | #define SHA_OP_INIT 0 |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 78 | #define SHA_OP_UPDATE 1 |
| 79 | #define SHA_OP_FINAL 2 |
Cyrille Pitchen | 81d8750 | 2017-01-26 17:07:54 +0100 | [diff] [blame^] | 80 | #define SHA_OP_DIGEST 3 |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 81 | |
Cyrille Pitchen | cc831d3 | 2016-01-29 17:04:02 +0100 | [diff] [blame] | 82 | #define SHA_BUFFER_LEN (PAGE_SIZE / 16) |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 83 | |
| 84 | #define ATMEL_SHA_DMA_THRESHOLD 56 |
| 85 | |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 86 | struct atmel_sha_caps { |
| 87 | bool has_dma; |
| 88 | bool has_dualbuff; |
| 89 | bool has_sha224; |
| 90 | bool has_sha_384_512; |
Cyrille Pitchen | 7cee350 | 2016-01-15 15:49:34 +0100 | [diff] [blame] | 91 | bool has_uihv; |
Cyrille Pitchen | 81d8750 | 2017-01-26 17:07:54 +0100 | [diff] [blame^] | 92 | bool has_hmac; |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 93 | }; |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 94 | |
| 95 | struct atmel_sha_dev; |
| 96 | |
Cyrille Pitchen | cc831d3 | 2016-01-29 17:04:02 +0100 | [diff] [blame] | 97 | /* |
Cyrille Pitchen | 9c4274d | 2016-02-08 16:26:48 +0100 | [diff] [blame] | 98 | * .statesize = sizeof(struct atmel_sha_reqctx) must be <= PAGE_SIZE / 8 as |
Cyrille Pitchen | cc831d3 | 2016-01-29 17:04:02 +0100 | [diff] [blame] | 99 | * tested by the ahash_prepare_alg() function. |
| 100 | */ |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 101 | struct atmel_sha_reqctx { |
| 102 | struct atmel_sha_dev *dd; |
| 103 | unsigned long flags; |
| 104 | unsigned long op; |
| 105 | |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 106 | u8 digest[SHA512_DIGEST_SIZE] __aligned(sizeof(u32)); |
| 107 | u64 digcnt[2]; |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 108 | size_t bufcnt; |
| 109 | size_t buflen; |
| 110 | dma_addr_t dma_addr; |
| 111 | |
| 112 | /* walk state */ |
| 113 | struct scatterlist *sg; |
| 114 | unsigned int offset; /* offset in current sg */ |
| 115 | unsigned int total; /* total request */ |
| 116 | |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 117 | size_t block_size; |
Cyrille Pitchen | 81d8750 | 2017-01-26 17:07:54 +0100 | [diff] [blame^] | 118 | size_t hash_size; |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 119 | |
Cyrille Pitchen | 9c4274d | 2016-02-08 16:26:48 +0100 | [diff] [blame] | 120 | u8 buffer[SHA_BUFFER_LEN + SHA512_BLOCK_SIZE] __aligned(sizeof(u32)); |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 121 | }; |
| 122 | |
Cyrille Pitchen | a29af93 | 2017-01-26 17:07:47 +0100 | [diff] [blame] | 123 | typedef int (*atmel_sha_fn_t)(struct atmel_sha_dev *); |
| 124 | |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 125 | struct atmel_sha_ctx { |
| 126 | struct atmel_sha_dev *dd; |
Cyrille Pitchen | a29af93 | 2017-01-26 17:07:47 +0100 | [diff] [blame] | 127 | atmel_sha_fn_t start; |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 128 | |
| 129 | unsigned long flags; |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 130 | }; |
| 131 | |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 132 | #define ATMEL_SHA_QUEUE_LENGTH 50 |
| 133 | |
| 134 | struct atmel_sha_dma { |
| 135 | struct dma_chan *chan; |
| 136 | struct dma_slave_config dma_conf; |
Cyrille Pitchen | 69303cf | 2017-01-26 17:07:53 +0100 | [diff] [blame] | 137 | struct scatterlist *sg; |
| 138 | int nents; |
| 139 | unsigned int last_sg_length; |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 140 | }; |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 141 | |
| 142 | struct atmel_sha_dev { |
| 143 | struct list_head list; |
| 144 | unsigned long phys_base; |
| 145 | struct device *dev; |
| 146 | struct clk *iclk; |
| 147 | int irq; |
| 148 | void __iomem *io_base; |
| 149 | |
| 150 | spinlock_t lock; |
| 151 | int err; |
| 152 | struct tasklet_struct done_task; |
Cyrille Pitchen | f56809c | 2016-01-15 15:49:32 +0100 | [diff] [blame] | 153 | struct tasklet_struct queue_task; |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 154 | |
| 155 | unsigned long flags; |
| 156 | struct crypto_queue queue; |
| 157 | struct ahash_request *req; |
Cyrille Pitchen | a29af93 | 2017-01-26 17:07:47 +0100 | [diff] [blame] | 158 | bool is_async; |
Cyrille Pitchen | b5ce82a | 2017-01-26 17:07:48 +0100 | [diff] [blame] | 159 | atmel_sha_fn_t resume; |
Cyrille Pitchen | eec12f6 | 2017-01-26 17:07:52 +0100 | [diff] [blame] | 160 | atmel_sha_fn_t cpu_transfer_complete; |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 161 | |
| 162 | struct atmel_sha_dma dma_lch_in; |
| 163 | |
| 164 | struct atmel_sha_caps caps; |
| 165 | |
Cyrille Pitchen | 81d8750 | 2017-01-26 17:07:54 +0100 | [diff] [blame^] | 166 | struct scatterlist tmp; |
| 167 | |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 168 | u32 hw_version; |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 169 | }; |
| 170 | |
| 171 | struct atmel_sha_drv { |
| 172 | struct list_head dev_list; |
| 173 | spinlock_t lock; |
| 174 | }; |
| 175 | |
| 176 | static struct atmel_sha_drv atmel_sha = { |
| 177 | .dev_list = LIST_HEAD_INIT(atmel_sha.dev_list), |
| 178 | .lock = __SPIN_LOCK_UNLOCKED(atmel_sha.lock), |
| 179 | }; |
| 180 | |
| 181 | static inline u32 atmel_sha_read(struct atmel_sha_dev *dd, u32 offset) |
| 182 | { |
| 183 | return readl_relaxed(dd->io_base + offset); |
| 184 | } |
| 185 | |
| 186 | static inline void atmel_sha_write(struct atmel_sha_dev *dd, |
| 187 | u32 offset, u32 value) |
| 188 | { |
| 189 | writel_relaxed(value, dd->io_base + offset); |
| 190 | } |
| 191 | |
Cyrille Pitchen | a29af93 | 2017-01-26 17:07:47 +0100 | [diff] [blame] | 192 | static inline int atmel_sha_complete(struct atmel_sha_dev *dd, int err) |
| 193 | { |
| 194 | struct ahash_request *req = dd->req; |
| 195 | |
| 196 | dd->flags &= ~(SHA_FLAGS_BUSY | SHA_FLAGS_FINAL | SHA_FLAGS_CPU | |
| 197 | SHA_FLAGS_DMA_READY | SHA_FLAGS_OUTPUT_READY); |
| 198 | |
| 199 | clk_disable(dd->iclk); |
| 200 | |
| 201 | if (dd->is_async && req->base.complete) |
| 202 | req->base.complete(&req->base, err); |
| 203 | |
| 204 | /* handle new request */ |
| 205 | tasklet_schedule(&dd->queue_task); |
| 206 | |
| 207 | return err; |
| 208 | } |
| 209 | |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 210 | static size_t atmel_sha_append_sg(struct atmel_sha_reqctx *ctx) |
| 211 | { |
| 212 | size_t count; |
| 213 | |
| 214 | while ((ctx->bufcnt < ctx->buflen) && ctx->total) { |
| 215 | count = min(ctx->sg->length - ctx->offset, ctx->total); |
| 216 | count = min(count, ctx->buflen - ctx->bufcnt); |
| 217 | |
Leilei Zhao | 803eeae | 2015-04-07 17:45:05 +0800 | [diff] [blame] | 218 | if (count <= 0) { |
| 219 | /* |
| 220 | * Check if count <= 0 because the buffer is full or |
| 221 | * because the sg length is 0. In the latest case, |
| 222 | * check if there is another sg in the list, a 0 length |
| 223 | * sg doesn't necessarily mean the end of the sg list. |
| 224 | */ |
| 225 | if ((ctx->sg->length == 0) && !sg_is_last(ctx->sg)) { |
| 226 | ctx->sg = sg_next(ctx->sg); |
| 227 | continue; |
| 228 | } else { |
| 229 | break; |
| 230 | } |
| 231 | } |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 232 | |
| 233 | scatterwalk_map_and_copy(ctx->buffer + ctx->bufcnt, ctx->sg, |
| 234 | ctx->offset, count, 0); |
| 235 | |
| 236 | ctx->bufcnt += count; |
| 237 | ctx->offset += count; |
| 238 | ctx->total -= count; |
| 239 | |
| 240 | if (ctx->offset == ctx->sg->length) { |
| 241 | ctx->sg = sg_next(ctx->sg); |
| 242 | if (ctx->sg) |
| 243 | ctx->offset = 0; |
| 244 | else |
| 245 | ctx->total = 0; |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | return 0; |
| 250 | } |
| 251 | |
| 252 | /* |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 253 | * The purpose of this padding is to ensure that the padded message is a |
| 254 | * multiple of 512 bits (SHA1/SHA224/SHA256) or 1024 bits (SHA384/SHA512). |
| 255 | * The bit "1" is appended at the end of the message followed by |
| 256 | * "padlen-1" zero bits. Then a 64 bits block (SHA1/SHA224/SHA256) or |
| 257 | * 128 bits block (SHA384/SHA512) equals to the message length in bits |
| 258 | * is appended. |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 259 | * |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 260 | * For SHA1/SHA224/SHA256, padlen is calculated as followed: |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 261 | * - if message length < 56 bytes then padlen = 56 - message length |
| 262 | * - else padlen = 64 + 56 - message length |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 263 | * |
| 264 | * For SHA384/SHA512, padlen is calculated as followed: |
| 265 | * - if message length < 112 bytes then padlen = 112 - message length |
| 266 | * - else padlen = 128 + 112 - message length |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 267 | */ |
| 268 | static void atmel_sha_fill_padding(struct atmel_sha_reqctx *ctx, int length) |
| 269 | { |
| 270 | unsigned int index, padlen; |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 271 | u64 bits[2]; |
| 272 | u64 size[2]; |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 273 | |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 274 | size[0] = ctx->digcnt[0]; |
| 275 | size[1] = ctx->digcnt[1]; |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 276 | |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 277 | size[0] += ctx->bufcnt; |
| 278 | if (size[0] < ctx->bufcnt) |
| 279 | size[1]++; |
| 280 | |
| 281 | size[0] += length; |
| 282 | if (size[0] < length) |
| 283 | size[1]++; |
| 284 | |
| 285 | bits[1] = cpu_to_be64(size[0] << 3); |
| 286 | bits[0] = cpu_to_be64(size[1] << 3 | size[0] >> 61); |
| 287 | |
Cyrille Pitchen | f07ceba | 2017-01-26 17:07:49 +0100 | [diff] [blame] | 288 | switch (ctx->flags & SHA_FLAGS_ALGO_MASK) { |
| 289 | case SHA_FLAGS_SHA384: |
| 290 | case SHA_FLAGS_SHA512: |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 291 | index = ctx->bufcnt & 0x7f; |
| 292 | padlen = (index < 112) ? (112 - index) : ((128+112) - index); |
| 293 | *(ctx->buffer + ctx->bufcnt) = 0x80; |
| 294 | memset(ctx->buffer + ctx->bufcnt + 1, 0, padlen-1); |
| 295 | memcpy(ctx->buffer + ctx->bufcnt + padlen, bits, 16); |
| 296 | ctx->bufcnt += padlen + 16; |
| 297 | ctx->flags |= SHA_FLAGS_PAD; |
Cyrille Pitchen | f07ceba | 2017-01-26 17:07:49 +0100 | [diff] [blame] | 298 | break; |
| 299 | |
| 300 | default: |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 301 | index = ctx->bufcnt & 0x3f; |
| 302 | padlen = (index < 56) ? (56 - index) : ((64+56) - index); |
| 303 | *(ctx->buffer + ctx->bufcnt) = 0x80; |
| 304 | memset(ctx->buffer + ctx->bufcnt + 1, 0, padlen-1); |
| 305 | memcpy(ctx->buffer + ctx->bufcnt + padlen, &bits[1], 8); |
| 306 | ctx->bufcnt += padlen + 8; |
| 307 | ctx->flags |= SHA_FLAGS_PAD; |
Cyrille Pitchen | f07ceba | 2017-01-26 17:07:49 +0100 | [diff] [blame] | 308 | break; |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 309 | } |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 310 | } |
| 311 | |
Cyrille Pitchen | 8340c7f | 2017-01-26 17:07:46 +0100 | [diff] [blame] | 312 | 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] | 313 | { |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 314 | struct atmel_sha_dev *dd = NULL; |
| 315 | struct atmel_sha_dev *tmp; |
| 316 | |
| 317 | spin_lock_bh(&atmel_sha.lock); |
| 318 | if (!tctx->dd) { |
| 319 | list_for_each_entry(tmp, &atmel_sha.dev_list, list) { |
| 320 | dd = tmp; |
| 321 | break; |
| 322 | } |
| 323 | tctx->dd = dd; |
| 324 | } else { |
| 325 | dd = tctx->dd; |
| 326 | } |
| 327 | |
| 328 | spin_unlock_bh(&atmel_sha.lock); |
| 329 | |
Cyrille Pitchen | 8340c7f | 2017-01-26 17:07:46 +0100 | [diff] [blame] | 330 | return dd; |
| 331 | } |
| 332 | |
| 333 | static int atmel_sha_init(struct ahash_request *req) |
| 334 | { |
| 335 | struct crypto_ahash *tfm = crypto_ahash_reqtfm(req); |
| 336 | struct atmel_sha_ctx *tctx = crypto_ahash_ctx(tfm); |
| 337 | struct atmel_sha_reqctx *ctx = ahash_request_ctx(req); |
| 338 | struct atmel_sha_dev *dd = atmel_sha_find_dev(tctx); |
| 339 | |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 340 | ctx->dd = dd; |
| 341 | |
| 342 | ctx->flags = 0; |
| 343 | |
| 344 | dev_dbg(dd->dev, "init: digest size: %d\n", |
| 345 | crypto_ahash_digestsize(tfm)); |
| 346 | |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 347 | switch (crypto_ahash_digestsize(tfm)) { |
| 348 | case SHA1_DIGEST_SIZE: |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 349 | ctx->flags |= SHA_FLAGS_SHA1; |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 350 | ctx->block_size = SHA1_BLOCK_SIZE; |
| 351 | break; |
| 352 | case SHA224_DIGEST_SIZE: |
| 353 | ctx->flags |= SHA_FLAGS_SHA224; |
| 354 | ctx->block_size = SHA224_BLOCK_SIZE; |
| 355 | break; |
| 356 | case SHA256_DIGEST_SIZE: |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 357 | ctx->flags |= SHA_FLAGS_SHA256; |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 358 | ctx->block_size = SHA256_BLOCK_SIZE; |
| 359 | break; |
| 360 | case SHA384_DIGEST_SIZE: |
| 361 | ctx->flags |= SHA_FLAGS_SHA384; |
| 362 | ctx->block_size = SHA384_BLOCK_SIZE; |
| 363 | break; |
| 364 | case SHA512_DIGEST_SIZE: |
| 365 | ctx->flags |= SHA_FLAGS_SHA512; |
| 366 | ctx->block_size = SHA512_BLOCK_SIZE; |
| 367 | break; |
| 368 | default: |
| 369 | return -EINVAL; |
| 370 | break; |
| 371 | } |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 372 | |
| 373 | ctx->bufcnt = 0; |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 374 | ctx->digcnt[0] = 0; |
| 375 | ctx->digcnt[1] = 0; |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 376 | ctx->buflen = SHA_BUFFER_LEN; |
| 377 | |
| 378 | return 0; |
| 379 | } |
| 380 | |
| 381 | static void atmel_sha_write_ctrl(struct atmel_sha_dev *dd, int dma) |
| 382 | { |
| 383 | struct atmel_sha_reqctx *ctx = ahash_request_ctx(dd->req); |
Cyrille Pitchen | 7cee350 | 2016-01-15 15:49:34 +0100 | [diff] [blame] | 384 | u32 valmr = SHA_MR_MODE_AUTO; |
| 385 | unsigned int i, hashsize = 0; |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 386 | |
| 387 | if (likely(dma)) { |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 388 | if (!dd->caps.has_dma) |
| 389 | atmel_sha_write(dd, SHA_IER, SHA_INT_TXBUFE); |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 390 | valmr = SHA_MR_MODE_PDC; |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 391 | if (dd->caps.has_dualbuff) |
| 392 | valmr |= SHA_MR_DUALBUFF; |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 393 | } else { |
| 394 | atmel_sha_write(dd, SHA_IER, SHA_INT_DATARDY); |
| 395 | } |
| 396 | |
Cyrille Pitchen | 7cee350 | 2016-01-15 15:49:34 +0100 | [diff] [blame] | 397 | switch (ctx->flags & SHA_FLAGS_ALGO_MASK) { |
| 398 | case SHA_FLAGS_SHA1: |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 399 | valmr |= SHA_MR_ALGO_SHA1; |
Cyrille Pitchen | 7cee350 | 2016-01-15 15:49:34 +0100 | [diff] [blame] | 400 | hashsize = SHA1_DIGEST_SIZE; |
| 401 | break; |
| 402 | |
| 403 | case SHA_FLAGS_SHA224: |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 404 | valmr |= SHA_MR_ALGO_SHA224; |
Cyrille Pitchen | 7cee350 | 2016-01-15 15:49:34 +0100 | [diff] [blame] | 405 | hashsize = SHA256_DIGEST_SIZE; |
| 406 | break; |
| 407 | |
| 408 | case SHA_FLAGS_SHA256: |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 409 | valmr |= SHA_MR_ALGO_SHA256; |
Cyrille Pitchen | 7cee350 | 2016-01-15 15:49:34 +0100 | [diff] [blame] | 410 | hashsize = SHA256_DIGEST_SIZE; |
| 411 | break; |
| 412 | |
| 413 | case SHA_FLAGS_SHA384: |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 414 | valmr |= SHA_MR_ALGO_SHA384; |
Cyrille Pitchen | 7cee350 | 2016-01-15 15:49:34 +0100 | [diff] [blame] | 415 | hashsize = SHA512_DIGEST_SIZE; |
| 416 | break; |
| 417 | |
| 418 | case SHA_FLAGS_SHA512: |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 419 | valmr |= SHA_MR_ALGO_SHA512; |
Cyrille Pitchen | 7cee350 | 2016-01-15 15:49:34 +0100 | [diff] [blame] | 420 | hashsize = SHA512_DIGEST_SIZE; |
| 421 | break; |
| 422 | |
| 423 | default: |
| 424 | break; |
| 425 | } |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 426 | |
| 427 | /* Setting CR_FIRST only for the first iteration */ |
Cyrille Pitchen | 7cee350 | 2016-01-15 15:49:34 +0100 | [diff] [blame] | 428 | if (!(ctx->digcnt[0] || ctx->digcnt[1])) { |
| 429 | atmel_sha_write(dd, SHA_CR, SHA_CR_FIRST); |
| 430 | } else if (dd->caps.has_uihv && (ctx->flags & SHA_FLAGS_RESTORE)) { |
| 431 | const u32 *hash = (const u32 *)ctx->digest; |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 432 | |
Cyrille Pitchen | 7cee350 | 2016-01-15 15:49:34 +0100 | [diff] [blame] | 433 | /* |
| 434 | * Restore the hardware context: update the User Initialize |
| 435 | * Hash Value (UIHV) with the value saved when the latest |
| 436 | * 'update' operation completed on this very same crypto |
| 437 | * request. |
| 438 | */ |
| 439 | ctx->flags &= ~SHA_FLAGS_RESTORE; |
| 440 | atmel_sha_write(dd, SHA_CR, SHA_CR_WUIHV); |
| 441 | for (i = 0; i < hashsize / sizeof(u32); ++i) |
| 442 | atmel_sha_write(dd, SHA_REG_DIN(i), hash[i]); |
| 443 | atmel_sha_write(dd, SHA_CR, SHA_CR_FIRST); |
| 444 | valmr |= SHA_MR_UIHV; |
| 445 | } |
| 446 | /* |
| 447 | * WARNING: If the UIHV feature is not available, the hardware CANNOT |
| 448 | * process concurrent requests: the internal registers used to store |
| 449 | * the hash/digest are still set to the partial digest output values |
| 450 | * computed during the latest round. |
| 451 | */ |
| 452 | |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 453 | atmel_sha_write(dd, SHA_MR, valmr); |
| 454 | } |
| 455 | |
Cyrille Pitchen | 9064ed9 | 2017-01-26 17:07:50 +0100 | [diff] [blame] | 456 | static inline int atmel_sha_wait_for_data_ready(struct atmel_sha_dev *dd, |
| 457 | atmel_sha_fn_t resume) |
| 458 | { |
| 459 | u32 isr = atmel_sha_read(dd, SHA_ISR); |
| 460 | |
| 461 | if (unlikely(isr & SHA_INT_DATARDY)) |
| 462 | return resume(dd); |
| 463 | |
| 464 | dd->resume = resume; |
| 465 | atmel_sha_write(dd, SHA_IER, SHA_INT_DATARDY); |
| 466 | return -EINPROGRESS; |
| 467 | } |
| 468 | |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 469 | static int atmel_sha_xmit_cpu(struct atmel_sha_dev *dd, const u8 *buf, |
| 470 | size_t length, int final) |
| 471 | { |
| 472 | struct atmel_sha_reqctx *ctx = ahash_request_ctx(dd->req); |
| 473 | int count, len32; |
| 474 | const u32 *buffer = (const u32 *)buf; |
| 475 | |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 476 | dev_dbg(dd->dev, "xmit_cpu: digcnt: 0x%llx 0x%llx, length: %d, final: %d\n", |
| 477 | ctx->digcnt[1], ctx->digcnt[0], length, final); |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 478 | |
| 479 | atmel_sha_write_ctrl(dd, 0); |
| 480 | |
| 481 | /* should be non-zero before next lines to disable clocks later */ |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 482 | ctx->digcnt[0] += length; |
| 483 | if (ctx->digcnt[0] < length) |
| 484 | ctx->digcnt[1]++; |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 485 | |
| 486 | if (final) |
| 487 | dd->flags |= SHA_FLAGS_FINAL; /* catch last interrupt */ |
| 488 | |
| 489 | len32 = DIV_ROUND_UP(length, sizeof(u32)); |
| 490 | |
| 491 | dd->flags |= SHA_FLAGS_CPU; |
| 492 | |
| 493 | for (count = 0; count < len32; count++) |
| 494 | atmel_sha_write(dd, SHA_REG_DIN(count), buffer[count]); |
| 495 | |
| 496 | return -EINPROGRESS; |
| 497 | } |
| 498 | |
| 499 | static int atmel_sha_xmit_pdc(struct atmel_sha_dev *dd, dma_addr_t dma_addr1, |
| 500 | size_t length1, dma_addr_t dma_addr2, size_t length2, int final) |
| 501 | { |
| 502 | struct atmel_sha_reqctx *ctx = ahash_request_ctx(dd->req); |
| 503 | int len32; |
| 504 | |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 505 | dev_dbg(dd->dev, "xmit_pdc: digcnt: 0x%llx 0x%llx, length: %d, final: %d\n", |
| 506 | ctx->digcnt[1], ctx->digcnt[0], length1, final); |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 507 | |
| 508 | len32 = DIV_ROUND_UP(length1, sizeof(u32)); |
| 509 | atmel_sha_write(dd, SHA_PTCR, SHA_PTCR_TXTDIS); |
| 510 | atmel_sha_write(dd, SHA_TPR, dma_addr1); |
| 511 | atmel_sha_write(dd, SHA_TCR, len32); |
| 512 | |
| 513 | len32 = DIV_ROUND_UP(length2, sizeof(u32)); |
| 514 | atmel_sha_write(dd, SHA_TNPR, dma_addr2); |
| 515 | atmel_sha_write(dd, SHA_TNCR, len32); |
| 516 | |
| 517 | atmel_sha_write_ctrl(dd, 1); |
| 518 | |
| 519 | /* should be non-zero before next lines to disable clocks later */ |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 520 | ctx->digcnt[0] += length1; |
| 521 | if (ctx->digcnt[0] < length1) |
| 522 | ctx->digcnt[1]++; |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 523 | |
| 524 | if (final) |
| 525 | dd->flags |= SHA_FLAGS_FINAL; /* catch last interrupt */ |
| 526 | |
| 527 | dd->flags |= SHA_FLAGS_DMA_ACTIVE; |
| 528 | |
| 529 | /* Start DMA transfer */ |
| 530 | atmel_sha_write(dd, SHA_PTCR, SHA_PTCR_TXTEN); |
| 531 | |
| 532 | return -EINPROGRESS; |
| 533 | } |
| 534 | |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 535 | static void atmel_sha_dma_callback(void *data) |
| 536 | { |
| 537 | struct atmel_sha_dev *dd = data; |
| 538 | |
Cyrille Pitchen | a29af93 | 2017-01-26 17:07:47 +0100 | [diff] [blame] | 539 | dd->is_async = true; |
| 540 | |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 541 | /* dma_lch_in - completed - wait DATRDY */ |
| 542 | atmel_sha_write(dd, SHA_IER, SHA_INT_DATARDY); |
| 543 | } |
| 544 | |
| 545 | static int atmel_sha_xmit_dma(struct atmel_sha_dev *dd, dma_addr_t dma_addr1, |
| 546 | size_t length1, dma_addr_t dma_addr2, size_t length2, int final) |
| 547 | { |
| 548 | struct atmel_sha_reqctx *ctx = ahash_request_ctx(dd->req); |
| 549 | struct dma_async_tx_descriptor *in_desc; |
| 550 | struct scatterlist sg[2]; |
| 551 | |
| 552 | dev_dbg(dd->dev, "xmit_dma: digcnt: 0x%llx 0x%llx, length: %d, final: %d\n", |
| 553 | ctx->digcnt[1], ctx->digcnt[0], length1, final); |
| 554 | |
Leilei Zhao | 3f1992c | 2015-04-07 17:45:07 +0800 | [diff] [blame] | 555 | dd->dma_lch_in.dma_conf.src_maxburst = 16; |
| 556 | dd->dma_lch_in.dma_conf.dst_maxburst = 16; |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 557 | |
| 558 | dmaengine_slave_config(dd->dma_lch_in.chan, &dd->dma_lch_in.dma_conf); |
| 559 | |
| 560 | if (length2) { |
| 561 | sg_init_table(sg, 2); |
| 562 | sg_dma_address(&sg[0]) = dma_addr1; |
| 563 | sg_dma_len(&sg[0]) = length1; |
| 564 | sg_dma_address(&sg[1]) = dma_addr2; |
| 565 | sg_dma_len(&sg[1]) = length2; |
| 566 | in_desc = dmaengine_prep_slave_sg(dd->dma_lch_in.chan, sg, 2, |
| 567 | DMA_MEM_TO_DEV, DMA_PREP_INTERRUPT | DMA_CTRL_ACK); |
| 568 | } else { |
| 569 | sg_init_table(sg, 1); |
| 570 | sg_dma_address(&sg[0]) = dma_addr1; |
| 571 | sg_dma_len(&sg[0]) = length1; |
| 572 | in_desc = dmaengine_prep_slave_sg(dd->dma_lch_in.chan, sg, 1, |
| 573 | DMA_MEM_TO_DEV, DMA_PREP_INTERRUPT | DMA_CTRL_ACK); |
| 574 | } |
| 575 | if (!in_desc) |
Cyrille Pitchen | a29af93 | 2017-01-26 17:07:47 +0100 | [diff] [blame] | 576 | atmel_sha_complete(dd, -EINVAL); |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 577 | |
| 578 | in_desc->callback = atmel_sha_dma_callback; |
| 579 | in_desc->callback_param = dd; |
| 580 | |
| 581 | atmel_sha_write_ctrl(dd, 1); |
| 582 | |
| 583 | /* should be non-zero before next lines to disable clocks later */ |
| 584 | ctx->digcnt[0] += length1; |
| 585 | if (ctx->digcnt[0] < length1) |
| 586 | ctx->digcnt[1]++; |
| 587 | |
| 588 | if (final) |
| 589 | dd->flags |= SHA_FLAGS_FINAL; /* catch last interrupt */ |
| 590 | |
| 591 | dd->flags |= SHA_FLAGS_DMA_ACTIVE; |
| 592 | |
| 593 | /* Start DMA transfer */ |
| 594 | dmaengine_submit(in_desc); |
| 595 | dma_async_issue_pending(dd->dma_lch_in.chan); |
| 596 | |
| 597 | return -EINPROGRESS; |
| 598 | } |
| 599 | |
| 600 | static int atmel_sha_xmit_start(struct atmel_sha_dev *dd, dma_addr_t dma_addr1, |
| 601 | size_t length1, dma_addr_t dma_addr2, size_t length2, int final) |
| 602 | { |
| 603 | if (dd->caps.has_dma) |
| 604 | return atmel_sha_xmit_dma(dd, dma_addr1, length1, |
| 605 | dma_addr2, length2, final); |
| 606 | else |
| 607 | return atmel_sha_xmit_pdc(dd, dma_addr1, length1, |
| 608 | dma_addr2, length2, final); |
| 609 | } |
| 610 | |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 611 | static int atmel_sha_update_cpu(struct atmel_sha_dev *dd) |
| 612 | { |
| 613 | struct atmel_sha_reqctx *ctx = ahash_request_ctx(dd->req); |
| 614 | int bufcnt; |
| 615 | |
| 616 | atmel_sha_append_sg(ctx); |
| 617 | atmel_sha_fill_padding(ctx, 0); |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 618 | bufcnt = ctx->bufcnt; |
| 619 | ctx->bufcnt = 0; |
| 620 | |
| 621 | return atmel_sha_xmit_cpu(dd, ctx->buffer, bufcnt, 1); |
| 622 | } |
| 623 | |
| 624 | static int atmel_sha_xmit_dma_map(struct atmel_sha_dev *dd, |
| 625 | struct atmel_sha_reqctx *ctx, |
| 626 | size_t length, int final) |
| 627 | { |
| 628 | ctx->dma_addr = dma_map_single(dd->dev, ctx->buffer, |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 629 | ctx->buflen + ctx->block_size, DMA_TO_DEVICE); |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 630 | if (dma_mapping_error(dd->dev, ctx->dma_addr)) { |
| 631 | dev_err(dd->dev, "dma %u bytes error\n", ctx->buflen + |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 632 | ctx->block_size); |
Cyrille Pitchen | a29af93 | 2017-01-26 17:07:47 +0100 | [diff] [blame] | 633 | atmel_sha_complete(dd, -EINVAL); |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 634 | } |
| 635 | |
| 636 | ctx->flags &= ~SHA_FLAGS_SG; |
| 637 | |
| 638 | /* 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] | 639 | 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] | 640 | } |
| 641 | |
| 642 | static int atmel_sha_update_dma_slow(struct atmel_sha_dev *dd) |
| 643 | { |
| 644 | struct atmel_sha_reqctx *ctx = ahash_request_ctx(dd->req); |
| 645 | unsigned int final; |
| 646 | size_t count; |
| 647 | |
| 648 | atmel_sha_append_sg(ctx); |
| 649 | |
| 650 | final = (ctx->flags & SHA_FLAGS_FINUP) && !ctx->total; |
| 651 | |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 652 | dev_dbg(dd->dev, "slow: bufcnt: %u, digcnt: 0x%llx 0x%llx, final: %d\n", |
| 653 | ctx->bufcnt, ctx->digcnt[1], ctx->digcnt[0], final); |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 654 | |
| 655 | if (final) |
| 656 | atmel_sha_fill_padding(ctx, 0); |
| 657 | |
Ludovic Desroches | 0099286 | 2015-04-07 17:45:04 +0800 | [diff] [blame] | 658 | if (final || (ctx->bufcnt == ctx->buflen)) { |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 659 | count = ctx->bufcnt; |
| 660 | ctx->bufcnt = 0; |
| 661 | return atmel_sha_xmit_dma_map(dd, ctx, count, final); |
| 662 | } |
| 663 | |
| 664 | return 0; |
| 665 | } |
| 666 | |
| 667 | static int atmel_sha_update_dma_start(struct atmel_sha_dev *dd) |
| 668 | { |
| 669 | struct atmel_sha_reqctx *ctx = ahash_request_ctx(dd->req); |
| 670 | unsigned int length, final, tail; |
| 671 | struct scatterlist *sg; |
| 672 | unsigned int count; |
| 673 | |
| 674 | if (!ctx->total) |
| 675 | return 0; |
| 676 | |
| 677 | if (ctx->bufcnt || ctx->offset) |
| 678 | return atmel_sha_update_dma_slow(dd); |
| 679 | |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 680 | dev_dbg(dd->dev, "fast: digcnt: 0x%llx 0x%llx, bufcnt: %u, total: %u\n", |
| 681 | ctx->digcnt[1], ctx->digcnt[0], ctx->bufcnt, ctx->total); |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 682 | |
| 683 | sg = ctx->sg; |
| 684 | |
| 685 | if (!IS_ALIGNED(sg->offset, sizeof(u32))) |
| 686 | return atmel_sha_update_dma_slow(dd); |
| 687 | |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 688 | if (!sg_is_last(sg) && !IS_ALIGNED(sg->length, ctx->block_size)) |
| 689 | /* size is not ctx->block_size aligned */ |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 690 | return atmel_sha_update_dma_slow(dd); |
| 691 | |
| 692 | length = min(ctx->total, sg->length); |
| 693 | |
| 694 | if (sg_is_last(sg)) { |
| 695 | if (!(ctx->flags & SHA_FLAGS_FINUP)) { |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 696 | /* not last sg must be ctx->block_size aligned */ |
| 697 | tail = length & (ctx->block_size - 1); |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 698 | length -= tail; |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 699 | } |
| 700 | } |
| 701 | |
| 702 | ctx->total -= length; |
| 703 | ctx->offset = length; /* offset where to start slow */ |
| 704 | |
| 705 | final = (ctx->flags & SHA_FLAGS_FINUP) && !ctx->total; |
| 706 | |
| 707 | /* Add padding */ |
| 708 | if (final) { |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 709 | tail = length & (ctx->block_size - 1); |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 710 | length -= tail; |
| 711 | ctx->total += tail; |
| 712 | ctx->offset = length; /* offset where to start slow */ |
| 713 | |
| 714 | sg = ctx->sg; |
| 715 | atmel_sha_append_sg(ctx); |
| 716 | |
| 717 | atmel_sha_fill_padding(ctx, length); |
| 718 | |
| 719 | ctx->dma_addr = dma_map_single(dd->dev, ctx->buffer, |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 720 | ctx->buflen + ctx->block_size, DMA_TO_DEVICE); |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 721 | if (dma_mapping_error(dd->dev, ctx->dma_addr)) { |
| 722 | dev_err(dd->dev, "dma %u bytes error\n", |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 723 | ctx->buflen + ctx->block_size); |
Cyrille Pitchen | a29af93 | 2017-01-26 17:07:47 +0100 | [diff] [blame] | 724 | atmel_sha_complete(dd, -EINVAL); |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 725 | } |
| 726 | |
| 727 | if (length == 0) { |
| 728 | ctx->flags &= ~SHA_FLAGS_SG; |
| 729 | count = ctx->bufcnt; |
| 730 | ctx->bufcnt = 0; |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 731 | return atmel_sha_xmit_start(dd, ctx->dma_addr, count, 0, |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 732 | 0, final); |
| 733 | } else { |
| 734 | ctx->sg = sg; |
| 735 | if (!dma_map_sg(dd->dev, ctx->sg, 1, |
| 736 | DMA_TO_DEVICE)) { |
| 737 | dev_err(dd->dev, "dma_map_sg error\n"); |
Cyrille Pitchen | a29af93 | 2017-01-26 17:07:47 +0100 | [diff] [blame] | 738 | atmel_sha_complete(dd, -EINVAL); |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 739 | } |
| 740 | |
| 741 | ctx->flags |= SHA_FLAGS_SG; |
| 742 | |
| 743 | count = ctx->bufcnt; |
| 744 | ctx->bufcnt = 0; |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 745 | return atmel_sha_xmit_start(dd, sg_dma_address(ctx->sg), |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 746 | length, ctx->dma_addr, count, final); |
| 747 | } |
| 748 | } |
| 749 | |
| 750 | if (!dma_map_sg(dd->dev, ctx->sg, 1, DMA_TO_DEVICE)) { |
| 751 | dev_err(dd->dev, "dma_map_sg error\n"); |
Cyrille Pitchen | a29af93 | 2017-01-26 17:07:47 +0100 | [diff] [blame] | 752 | atmel_sha_complete(dd, -EINVAL); |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 753 | } |
| 754 | |
| 755 | ctx->flags |= SHA_FLAGS_SG; |
| 756 | |
| 757 | /* 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] | 758 | 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] | 759 | 0, final); |
| 760 | } |
| 761 | |
| 762 | static int atmel_sha_update_dma_stop(struct atmel_sha_dev *dd) |
| 763 | { |
| 764 | struct atmel_sha_reqctx *ctx = ahash_request_ctx(dd->req); |
| 765 | |
| 766 | if (ctx->flags & SHA_FLAGS_SG) { |
| 767 | dma_unmap_sg(dd->dev, ctx->sg, 1, DMA_TO_DEVICE); |
| 768 | if (ctx->sg->length == ctx->offset) { |
| 769 | ctx->sg = sg_next(ctx->sg); |
| 770 | if (ctx->sg) |
| 771 | ctx->offset = 0; |
| 772 | } |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 773 | if (ctx->flags & SHA_FLAGS_PAD) { |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 774 | dma_unmap_single(dd->dev, ctx->dma_addr, |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 775 | ctx->buflen + ctx->block_size, DMA_TO_DEVICE); |
| 776 | } |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 777 | } else { |
| 778 | dma_unmap_single(dd->dev, ctx->dma_addr, ctx->buflen + |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 779 | ctx->block_size, DMA_TO_DEVICE); |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 780 | } |
| 781 | |
| 782 | return 0; |
| 783 | } |
| 784 | |
| 785 | static int atmel_sha_update_req(struct atmel_sha_dev *dd) |
| 786 | { |
| 787 | struct ahash_request *req = dd->req; |
| 788 | struct atmel_sha_reqctx *ctx = ahash_request_ctx(req); |
| 789 | int err; |
| 790 | |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 791 | dev_dbg(dd->dev, "update_req: total: %u, digcnt: 0x%llx 0x%llx\n", |
| 792 | ctx->total, ctx->digcnt[1], ctx->digcnt[0]); |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 793 | |
| 794 | if (ctx->flags & SHA_FLAGS_CPU) |
| 795 | err = atmel_sha_update_cpu(dd); |
| 796 | else |
| 797 | err = atmel_sha_update_dma_start(dd); |
| 798 | |
| 799 | /* wait for dma completion before can take more data */ |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 800 | dev_dbg(dd->dev, "update: err: %d, digcnt: 0x%llx 0%llx\n", |
| 801 | err, ctx->digcnt[1], ctx->digcnt[0]); |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 802 | |
| 803 | return err; |
| 804 | } |
| 805 | |
| 806 | static int atmel_sha_final_req(struct atmel_sha_dev *dd) |
| 807 | { |
| 808 | struct ahash_request *req = dd->req; |
| 809 | struct atmel_sha_reqctx *ctx = ahash_request_ctx(req); |
| 810 | int err = 0; |
| 811 | int count; |
| 812 | |
| 813 | if (ctx->bufcnt >= ATMEL_SHA_DMA_THRESHOLD) { |
| 814 | atmel_sha_fill_padding(ctx, 0); |
| 815 | count = ctx->bufcnt; |
| 816 | ctx->bufcnt = 0; |
| 817 | err = atmel_sha_xmit_dma_map(dd, ctx, count, 1); |
| 818 | } |
| 819 | /* faster to handle last block with cpu */ |
| 820 | else { |
| 821 | atmel_sha_fill_padding(ctx, 0); |
| 822 | count = ctx->bufcnt; |
| 823 | ctx->bufcnt = 0; |
| 824 | err = atmel_sha_xmit_cpu(dd, ctx->buffer, count, 1); |
| 825 | } |
| 826 | |
| 827 | dev_dbg(dd->dev, "final_req: err: %d\n", err); |
| 828 | |
| 829 | return err; |
| 830 | } |
| 831 | |
| 832 | static void atmel_sha_copy_hash(struct ahash_request *req) |
| 833 | { |
| 834 | struct atmel_sha_reqctx *ctx = ahash_request_ctx(req); |
| 835 | u32 *hash = (u32 *)ctx->digest; |
Cyrille Pitchen | 7cee350 | 2016-01-15 15:49:34 +0100 | [diff] [blame] | 836 | unsigned int i, hashsize; |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 837 | |
Cyrille Pitchen | 7cee350 | 2016-01-15 15:49:34 +0100 | [diff] [blame] | 838 | switch (ctx->flags & SHA_FLAGS_ALGO_MASK) { |
| 839 | case SHA_FLAGS_SHA1: |
| 840 | hashsize = SHA1_DIGEST_SIZE; |
| 841 | break; |
| 842 | |
| 843 | case SHA_FLAGS_SHA224: |
| 844 | case SHA_FLAGS_SHA256: |
| 845 | hashsize = SHA256_DIGEST_SIZE; |
| 846 | break; |
| 847 | |
| 848 | case SHA_FLAGS_SHA384: |
| 849 | case SHA_FLAGS_SHA512: |
| 850 | hashsize = SHA512_DIGEST_SIZE; |
| 851 | break; |
| 852 | |
| 853 | default: |
| 854 | /* Should not happen... */ |
| 855 | return; |
| 856 | } |
| 857 | |
| 858 | for (i = 0; i < hashsize / sizeof(u32); ++i) |
| 859 | hash[i] = atmel_sha_read(ctx->dd, SHA_REG_DIGEST(i)); |
| 860 | ctx->flags |= SHA_FLAGS_RESTORE; |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 861 | } |
| 862 | |
| 863 | static void atmel_sha_copy_ready_hash(struct ahash_request *req) |
| 864 | { |
| 865 | struct atmel_sha_reqctx *ctx = ahash_request_ctx(req); |
| 866 | |
| 867 | if (!req->result) |
| 868 | return; |
| 869 | |
Cyrille Pitchen | f07ceba | 2017-01-26 17:07:49 +0100 | [diff] [blame] | 870 | switch (ctx->flags & SHA_FLAGS_ALGO_MASK) { |
| 871 | default: |
| 872 | case SHA_FLAGS_SHA1: |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 873 | memcpy(req->result, ctx->digest, SHA1_DIGEST_SIZE); |
Cyrille Pitchen | f07ceba | 2017-01-26 17:07:49 +0100 | [diff] [blame] | 874 | break; |
| 875 | |
| 876 | case SHA_FLAGS_SHA224: |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 877 | memcpy(req->result, ctx->digest, SHA224_DIGEST_SIZE); |
Cyrille Pitchen | f07ceba | 2017-01-26 17:07:49 +0100 | [diff] [blame] | 878 | break; |
| 879 | |
| 880 | case SHA_FLAGS_SHA256: |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 881 | memcpy(req->result, ctx->digest, SHA256_DIGEST_SIZE); |
Cyrille Pitchen | f07ceba | 2017-01-26 17:07:49 +0100 | [diff] [blame] | 882 | break; |
| 883 | |
| 884 | case SHA_FLAGS_SHA384: |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 885 | memcpy(req->result, ctx->digest, SHA384_DIGEST_SIZE); |
Cyrille Pitchen | f07ceba | 2017-01-26 17:07:49 +0100 | [diff] [blame] | 886 | break; |
| 887 | |
| 888 | case SHA_FLAGS_SHA512: |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 889 | memcpy(req->result, ctx->digest, SHA512_DIGEST_SIZE); |
Cyrille Pitchen | f07ceba | 2017-01-26 17:07:49 +0100 | [diff] [blame] | 890 | break; |
| 891 | } |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 892 | } |
| 893 | |
| 894 | static int atmel_sha_finish(struct ahash_request *req) |
| 895 | { |
| 896 | struct atmel_sha_reqctx *ctx = ahash_request_ctx(req); |
| 897 | struct atmel_sha_dev *dd = ctx->dd; |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 898 | |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 899 | if (ctx->digcnt[0] || ctx->digcnt[1]) |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 900 | atmel_sha_copy_ready_hash(req); |
| 901 | |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 902 | dev_dbg(dd->dev, "digcnt: 0x%llx 0x%llx, bufcnt: %d\n", ctx->digcnt[1], |
| 903 | ctx->digcnt[0], ctx->bufcnt); |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 904 | |
Rahul Pathak | 871b88a | 2015-12-14 08:44:19 +0000 | [diff] [blame] | 905 | return 0; |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 906 | } |
| 907 | |
| 908 | static void atmel_sha_finish_req(struct ahash_request *req, int err) |
| 909 | { |
| 910 | struct atmel_sha_reqctx *ctx = ahash_request_ctx(req); |
| 911 | struct atmel_sha_dev *dd = ctx->dd; |
| 912 | |
| 913 | if (!err) { |
| 914 | atmel_sha_copy_hash(req); |
| 915 | if (SHA_FLAGS_FINAL & dd->flags) |
| 916 | err = atmel_sha_finish(req); |
| 917 | } else { |
| 918 | ctx->flags |= SHA_FLAGS_ERROR; |
| 919 | } |
| 920 | |
| 921 | /* atomic operation is not needed here */ |
Cyrille Pitchen | a29af93 | 2017-01-26 17:07:47 +0100 | [diff] [blame] | 922 | (void)atmel_sha_complete(dd, err); |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 923 | } |
| 924 | |
| 925 | static int atmel_sha_hw_init(struct atmel_sha_dev *dd) |
| 926 | { |
LABBE Corentin | 9d83d29 | 2015-10-02 14:12:58 +0200 | [diff] [blame] | 927 | int err; |
| 928 | |
Cyrille Pitchen | c033042 | 2016-02-05 13:45:13 +0100 | [diff] [blame] | 929 | err = clk_enable(dd->iclk); |
LABBE Corentin | 9d83d29 | 2015-10-02 14:12:58 +0200 | [diff] [blame] | 930 | if (err) |
| 931 | return err; |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 932 | |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 933 | if (!(SHA_FLAGS_INIT & dd->flags)) { |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 934 | atmel_sha_write(dd, SHA_CR, SHA_CR_SWRST); |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 935 | dd->flags |= SHA_FLAGS_INIT; |
| 936 | dd->err = 0; |
| 937 | } |
| 938 | |
| 939 | return 0; |
| 940 | } |
| 941 | |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 942 | static inline unsigned int atmel_sha_get_version(struct atmel_sha_dev *dd) |
| 943 | { |
| 944 | return atmel_sha_read(dd, SHA_HW_VERSION) & 0x00000fff; |
| 945 | } |
| 946 | |
| 947 | static void atmel_sha_hw_version_init(struct atmel_sha_dev *dd) |
| 948 | { |
| 949 | atmel_sha_hw_init(dd); |
| 950 | |
| 951 | dd->hw_version = atmel_sha_get_version(dd); |
| 952 | |
| 953 | dev_info(dd->dev, |
| 954 | "version: 0x%x\n", dd->hw_version); |
| 955 | |
Cyrille Pitchen | c033042 | 2016-02-05 13:45:13 +0100 | [diff] [blame] | 956 | clk_disable(dd->iclk); |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 957 | } |
| 958 | |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 959 | static int atmel_sha_handle_queue(struct atmel_sha_dev *dd, |
| 960 | struct ahash_request *req) |
| 961 | { |
| 962 | struct crypto_async_request *async_req, *backlog; |
Cyrille Pitchen | a29af93 | 2017-01-26 17:07:47 +0100 | [diff] [blame] | 963 | struct atmel_sha_ctx *ctx; |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 964 | unsigned long flags; |
Cyrille Pitchen | a29af93 | 2017-01-26 17:07:47 +0100 | [diff] [blame] | 965 | bool start_async; |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 966 | int err = 0, ret = 0; |
| 967 | |
| 968 | spin_lock_irqsave(&dd->lock, flags); |
| 969 | if (req) |
| 970 | ret = ahash_enqueue_request(&dd->queue, req); |
| 971 | |
| 972 | if (SHA_FLAGS_BUSY & dd->flags) { |
| 973 | spin_unlock_irqrestore(&dd->lock, flags); |
| 974 | return ret; |
| 975 | } |
| 976 | |
| 977 | backlog = crypto_get_backlog(&dd->queue); |
| 978 | async_req = crypto_dequeue_request(&dd->queue); |
| 979 | if (async_req) |
| 980 | dd->flags |= SHA_FLAGS_BUSY; |
| 981 | |
| 982 | spin_unlock_irqrestore(&dd->lock, flags); |
| 983 | |
| 984 | if (!async_req) |
| 985 | return ret; |
| 986 | |
| 987 | if (backlog) |
| 988 | backlog->complete(backlog, -EINPROGRESS); |
| 989 | |
Cyrille Pitchen | a29af93 | 2017-01-26 17:07:47 +0100 | [diff] [blame] | 990 | ctx = crypto_tfm_ctx(async_req->tfm); |
| 991 | |
| 992 | dd->req = ahash_request_cast(async_req); |
| 993 | start_async = (dd->req != req); |
| 994 | dd->is_async = start_async; |
| 995 | |
| 996 | /* WARNING: ctx->start() MAY change dd->is_async. */ |
| 997 | err = ctx->start(dd); |
| 998 | return (start_async) ? ret : err; |
| 999 | } |
| 1000 | |
Cyrille Pitchen | b5ce82a | 2017-01-26 17:07:48 +0100 | [diff] [blame] | 1001 | static int atmel_sha_done(struct atmel_sha_dev *dd); |
| 1002 | |
Cyrille Pitchen | a29af93 | 2017-01-26 17:07:47 +0100 | [diff] [blame] | 1003 | static int atmel_sha_start(struct atmel_sha_dev *dd) |
| 1004 | { |
| 1005 | struct ahash_request *req = dd->req; |
| 1006 | struct atmel_sha_reqctx *ctx = ahash_request_ctx(req); |
| 1007 | int err; |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 1008 | |
| 1009 | dev_dbg(dd->dev, "handling new req, op: %lu, nbytes: %d\n", |
| 1010 | ctx->op, req->nbytes); |
| 1011 | |
| 1012 | err = atmel_sha_hw_init(dd); |
| 1013 | |
| 1014 | if (err) |
| 1015 | goto err1; |
| 1016 | |
Cyrille Pitchen | b5ce82a | 2017-01-26 17:07:48 +0100 | [diff] [blame] | 1017 | dd->resume = atmel_sha_done; |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 1018 | if (ctx->op == SHA_OP_UPDATE) { |
| 1019 | err = atmel_sha_update_req(dd); |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 1020 | if (err != -EINPROGRESS && (ctx->flags & SHA_FLAGS_FINUP)) |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 1021 | /* no final() after finup() */ |
| 1022 | err = atmel_sha_final_req(dd); |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 1023 | } else if (ctx->op == SHA_OP_FINAL) { |
| 1024 | err = atmel_sha_final_req(dd); |
| 1025 | } |
| 1026 | |
| 1027 | err1: |
| 1028 | if (err != -EINPROGRESS) |
| 1029 | /* done_task will not finish it, so do it here */ |
| 1030 | atmel_sha_finish_req(req, err); |
| 1031 | |
| 1032 | dev_dbg(dd->dev, "exit, err: %d\n", err); |
| 1033 | |
Cyrille Pitchen | a29af93 | 2017-01-26 17:07:47 +0100 | [diff] [blame] | 1034 | return err; |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 1035 | } |
| 1036 | |
| 1037 | static int atmel_sha_enqueue(struct ahash_request *req, unsigned int op) |
| 1038 | { |
| 1039 | struct atmel_sha_reqctx *ctx = ahash_request_ctx(req); |
| 1040 | struct atmel_sha_ctx *tctx = crypto_tfm_ctx(req->base.tfm); |
| 1041 | struct atmel_sha_dev *dd = tctx->dd; |
| 1042 | |
| 1043 | ctx->op = op; |
| 1044 | |
| 1045 | return atmel_sha_handle_queue(dd, req); |
| 1046 | } |
| 1047 | |
| 1048 | static int atmel_sha_update(struct ahash_request *req) |
| 1049 | { |
| 1050 | struct atmel_sha_reqctx *ctx = ahash_request_ctx(req); |
| 1051 | |
| 1052 | if (!req->nbytes) |
| 1053 | return 0; |
| 1054 | |
| 1055 | ctx->total = req->nbytes; |
| 1056 | ctx->sg = req->src; |
| 1057 | ctx->offset = 0; |
| 1058 | |
| 1059 | if (ctx->flags & SHA_FLAGS_FINUP) { |
| 1060 | if (ctx->bufcnt + ctx->total < ATMEL_SHA_DMA_THRESHOLD) |
| 1061 | /* faster to use CPU for short transfers */ |
| 1062 | ctx->flags |= SHA_FLAGS_CPU; |
| 1063 | } else if (ctx->bufcnt + ctx->total < ctx->buflen) { |
| 1064 | atmel_sha_append_sg(ctx); |
| 1065 | return 0; |
| 1066 | } |
| 1067 | return atmel_sha_enqueue(req, SHA_OP_UPDATE); |
| 1068 | } |
| 1069 | |
| 1070 | static int atmel_sha_final(struct ahash_request *req) |
| 1071 | { |
| 1072 | struct atmel_sha_reqctx *ctx = ahash_request_ctx(req); |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 1073 | |
| 1074 | ctx->flags |= SHA_FLAGS_FINUP; |
| 1075 | |
| 1076 | if (ctx->flags & SHA_FLAGS_ERROR) |
| 1077 | return 0; /* uncompleted hash is not needed */ |
| 1078 | |
Cyrille Pitchen | ad84112 | 2016-02-08 16:26:49 +0100 | [diff] [blame] | 1079 | if (ctx->flags & SHA_FLAGS_PAD) |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 1080 | /* copy ready hash (+ finalize hmac) */ |
| 1081 | return atmel_sha_finish(req); |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 1082 | |
Cyrille Pitchen | ad84112 | 2016-02-08 16:26:49 +0100 | [diff] [blame] | 1083 | return atmel_sha_enqueue(req, SHA_OP_FINAL); |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 1084 | } |
| 1085 | |
| 1086 | static int atmel_sha_finup(struct ahash_request *req) |
| 1087 | { |
| 1088 | struct atmel_sha_reqctx *ctx = ahash_request_ctx(req); |
| 1089 | int err1, err2; |
| 1090 | |
| 1091 | ctx->flags |= SHA_FLAGS_FINUP; |
| 1092 | |
| 1093 | err1 = atmel_sha_update(req); |
| 1094 | if (err1 == -EINPROGRESS || err1 == -EBUSY) |
| 1095 | return err1; |
| 1096 | |
| 1097 | /* |
| 1098 | * final() has to be always called to cleanup resources |
| 1099 | * even if udpate() failed, except EINPROGRESS |
| 1100 | */ |
| 1101 | err2 = atmel_sha_final(req); |
| 1102 | |
| 1103 | return err1 ?: err2; |
| 1104 | } |
| 1105 | |
| 1106 | static int atmel_sha_digest(struct ahash_request *req) |
| 1107 | { |
| 1108 | return atmel_sha_init(req) ?: atmel_sha_finup(req); |
| 1109 | } |
| 1110 | |
Cyrille Pitchen | cc831d3 | 2016-01-29 17:04:02 +0100 | [diff] [blame] | 1111 | |
| 1112 | static int atmel_sha_export(struct ahash_request *req, void *out) |
| 1113 | { |
| 1114 | const struct atmel_sha_reqctx *ctx = ahash_request_ctx(req); |
Cyrille Pitchen | cc831d3 | 2016-01-29 17:04:02 +0100 | [diff] [blame] | 1115 | |
Cyrille Pitchen | 9c4274d | 2016-02-08 16:26:48 +0100 | [diff] [blame] | 1116 | memcpy(out, ctx, sizeof(*ctx)); |
Cyrille Pitchen | cc831d3 | 2016-01-29 17:04:02 +0100 | [diff] [blame] | 1117 | return 0; |
| 1118 | } |
| 1119 | |
| 1120 | static int atmel_sha_import(struct ahash_request *req, const void *in) |
| 1121 | { |
| 1122 | struct atmel_sha_reqctx *ctx = ahash_request_ctx(req); |
Cyrille Pitchen | cc831d3 | 2016-01-29 17:04:02 +0100 | [diff] [blame] | 1123 | |
Cyrille Pitchen | 9c4274d | 2016-02-08 16:26:48 +0100 | [diff] [blame] | 1124 | memcpy(ctx, in, sizeof(*ctx)); |
Cyrille Pitchen | cc831d3 | 2016-01-29 17:04:02 +0100 | [diff] [blame] | 1125 | return 0; |
| 1126 | } |
| 1127 | |
Svenning Sørensen | be95f0f | 2014-12-05 01:18:57 +0100 | [diff] [blame] | 1128 | static int atmel_sha_cra_init(struct crypto_tfm *tfm) |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 1129 | { |
Cyrille Pitchen | a29af93 | 2017-01-26 17:07:47 +0100 | [diff] [blame] | 1130 | struct atmel_sha_ctx *ctx = crypto_tfm_ctx(tfm); |
| 1131 | |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 1132 | crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm), |
Cyrille Pitchen | 9c4274d | 2016-02-08 16:26:48 +0100 | [diff] [blame] | 1133 | sizeof(struct atmel_sha_reqctx)); |
Cyrille Pitchen | a29af93 | 2017-01-26 17:07:47 +0100 | [diff] [blame] | 1134 | ctx->start = atmel_sha_start; |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 1135 | |
| 1136 | return 0; |
| 1137 | } |
| 1138 | |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 1139 | static struct ahash_alg sha_1_256_algs[] = { |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 1140 | { |
| 1141 | .init = atmel_sha_init, |
| 1142 | .update = atmel_sha_update, |
| 1143 | .final = atmel_sha_final, |
| 1144 | .finup = atmel_sha_finup, |
| 1145 | .digest = atmel_sha_digest, |
Cyrille Pitchen | cc831d3 | 2016-01-29 17:04:02 +0100 | [diff] [blame] | 1146 | .export = atmel_sha_export, |
| 1147 | .import = atmel_sha_import, |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 1148 | .halg = { |
| 1149 | .digestsize = SHA1_DIGEST_SIZE, |
Cyrille Pitchen | 9c4274d | 2016-02-08 16:26:48 +0100 | [diff] [blame] | 1150 | .statesize = sizeof(struct atmel_sha_reqctx), |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 1151 | .base = { |
| 1152 | .cra_name = "sha1", |
| 1153 | .cra_driver_name = "atmel-sha1", |
| 1154 | .cra_priority = 100, |
Svenning Sørensen | be95f0f | 2014-12-05 01:18:57 +0100 | [diff] [blame] | 1155 | .cra_flags = CRYPTO_ALG_ASYNC, |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 1156 | .cra_blocksize = SHA1_BLOCK_SIZE, |
| 1157 | .cra_ctxsize = sizeof(struct atmel_sha_ctx), |
| 1158 | .cra_alignmask = 0, |
| 1159 | .cra_module = THIS_MODULE, |
| 1160 | .cra_init = atmel_sha_cra_init, |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 1161 | } |
| 1162 | } |
| 1163 | }, |
| 1164 | { |
| 1165 | .init = atmel_sha_init, |
| 1166 | .update = atmel_sha_update, |
| 1167 | .final = atmel_sha_final, |
| 1168 | .finup = atmel_sha_finup, |
| 1169 | .digest = atmel_sha_digest, |
Cyrille Pitchen | cc831d3 | 2016-01-29 17:04:02 +0100 | [diff] [blame] | 1170 | .export = atmel_sha_export, |
| 1171 | .import = atmel_sha_import, |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 1172 | .halg = { |
| 1173 | .digestsize = SHA256_DIGEST_SIZE, |
Cyrille Pitchen | 9c4274d | 2016-02-08 16:26:48 +0100 | [diff] [blame] | 1174 | .statesize = sizeof(struct atmel_sha_reqctx), |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 1175 | .base = { |
| 1176 | .cra_name = "sha256", |
| 1177 | .cra_driver_name = "atmel-sha256", |
| 1178 | .cra_priority = 100, |
Svenning Sørensen | be95f0f | 2014-12-05 01:18:57 +0100 | [diff] [blame] | 1179 | .cra_flags = CRYPTO_ALG_ASYNC, |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 1180 | .cra_blocksize = SHA256_BLOCK_SIZE, |
| 1181 | .cra_ctxsize = sizeof(struct atmel_sha_ctx), |
| 1182 | .cra_alignmask = 0, |
| 1183 | .cra_module = THIS_MODULE, |
| 1184 | .cra_init = atmel_sha_cra_init, |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 1185 | } |
| 1186 | } |
| 1187 | }, |
| 1188 | }; |
| 1189 | |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 1190 | static struct ahash_alg sha_224_alg = { |
| 1191 | .init = atmel_sha_init, |
| 1192 | .update = atmel_sha_update, |
| 1193 | .final = atmel_sha_final, |
| 1194 | .finup = atmel_sha_finup, |
| 1195 | .digest = atmel_sha_digest, |
Cyrille Pitchen | cc831d3 | 2016-01-29 17:04:02 +0100 | [diff] [blame] | 1196 | .export = atmel_sha_export, |
| 1197 | .import = atmel_sha_import, |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 1198 | .halg = { |
| 1199 | .digestsize = SHA224_DIGEST_SIZE, |
Cyrille Pitchen | 9c4274d | 2016-02-08 16:26:48 +0100 | [diff] [blame] | 1200 | .statesize = sizeof(struct atmel_sha_reqctx), |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 1201 | .base = { |
| 1202 | .cra_name = "sha224", |
| 1203 | .cra_driver_name = "atmel-sha224", |
| 1204 | .cra_priority = 100, |
Svenning Sørensen | be95f0f | 2014-12-05 01:18:57 +0100 | [diff] [blame] | 1205 | .cra_flags = CRYPTO_ALG_ASYNC, |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 1206 | .cra_blocksize = SHA224_BLOCK_SIZE, |
| 1207 | .cra_ctxsize = sizeof(struct atmel_sha_ctx), |
| 1208 | .cra_alignmask = 0, |
| 1209 | .cra_module = THIS_MODULE, |
| 1210 | .cra_init = atmel_sha_cra_init, |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 1211 | } |
| 1212 | } |
| 1213 | }; |
| 1214 | |
| 1215 | static struct ahash_alg sha_384_512_algs[] = { |
| 1216 | { |
| 1217 | .init = atmel_sha_init, |
| 1218 | .update = atmel_sha_update, |
| 1219 | .final = atmel_sha_final, |
| 1220 | .finup = atmel_sha_finup, |
| 1221 | .digest = atmel_sha_digest, |
Cyrille Pitchen | cc831d3 | 2016-01-29 17:04:02 +0100 | [diff] [blame] | 1222 | .export = atmel_sha_export, |
| 1223 | .import = atmel_sha_import, |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 1224 | .halg = { |
| 1225 | .digestsize = SHA384_DIGEST_SIZE, |
Cyrille Pitchen | 9c4274d | 2016-02-08 16:26:48 +0100 | [diff] [blame] | 1226 | .statesize = sizeof(struct atmel_sha_reqctx), |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 1227 | .base = { |
| 1228 | .cra_name = "sha384", |
| 1229 | .cra_driver_name = "atmel-sha384", |
| 1230 | .cra_priority = 100, |
Svenning Sørensen | be95f0f | 2014-12-05 01:18:57 +0100 | [diff] [blame] | 1231 | .cra_flags = CRYPTO_ALG_ASYNC, |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 1232 | .cra_blocksize = SHA384_BLOCK_SIZE, |
| 1233 | .cra_ctxsize = sizeof(struct atmel_sha_ctx), |
| 1234 | .cra_alignmask = 0x3, |
| 1235 | .cra_module = THIS_MODULE, |
| 1236 | .cra_init = atmel_sha_cra_init, |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 1237 | } |
| 1238 | } |
| 1239 | }, |
| 1240 | { |
| 1241 | .init = atmel_sha_init, |
| 1242 | .update = atmel_sha_update, |
| 1243 | .final = atmel_sha_final, |
| 1244 | .finup = atmel_sha_finup, |
| 1245 | .digest = atmel_sha_digest, |
Cyrille Pitchen | cc831d3 | 2016-01-29 17:04:02 +0100 | [diff] [blame] | 1246 | .export = atmel_sha_export, |
| 1247 | .import = atmel_sha_import, |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 1248 | .halg = { |
| 1249 | .digestsize = SHA512_DIGEST_SIZE, |
Cyrille Pitchen | 9c4274d | 2016-02-08 16:26:48 +0100 | [diff] [blame] | 1250 | .statesize = sizeof(struct atmel_sha_reqctx), |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 1251 | .base = { |
| 1252 | .cra_name = "sha512", |
| 1253 | .cra_driver_name = "atmel-sha512", |
| 1254 | .cra_priority = 100, |
Svenning Sørensen | be95f0f | 2014-12-05 01:18:57 +0100 | [diff] [blame] | 1255 | .cra_flags = CRYPTO_ALG_ASYNC, |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 1256 | .cra_blocksize = SHA512_BLOCK_SIZE, |
| 1257 | .cra_ctxsize = sizeof(struct atmel_sha_ctx), |
| 1258 | .cra_alignmask = 0x3, |
| 1259 | .cra_module = THIS_MODULE, |
| 1260 | .cra_init = atmel_sha_cra_init, |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 1261 | } |
| 1262 | } |
| 1263 | }, |
| 1264 | }; |
| 1265 | |
Cyrille Pitchen | f56809c | 2016-01-15 15:49:32 +0100 | [diff] [blame] | 1266 | static void atmel_sha_queue_task(unsigned long data) |
| 1267 | { |
| 1268 | struct atmel_sha_dev *dd = (struct atmel_sha_dev *)data; |
| 1269 | |
| 1270 | atmel_sha_handle_queue(dd, NULL); |
| 1271 | } |
| 1272 | |
Cyrille Pitchen | b5ce82a | 2017-01-26 17:07:48 +0100 | [diff] [blame] | 1273 | static int atmel_sha_done(struct atmel_sha_dev *dd) |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 1274 | { |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 1275 | int err = 0; |
| 1276 | |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 1277 | if (SHA_FLAGS_CPU & dd->flags) { |
| 1278 | if (SHA_FLAGS_OUTPUT_READY & dd->flags) { |
| 1279 | dd->flags &= ~SHA_FLAGS_OUTPUT_READY; |
| 1280 | goto finish; |
| 1281 | } |
| 1282 | } else if (SHA_FLAGS_DMA_READY & dd->flags) { |
| 1283 | if (SHA_FLAGS_DMA_ACTIVE & dd->flags) { |
| 1284 | dd->flags &= ~SHA_FLAGS_DMA_ACTIVE; |
| 1285 | atmel_sha_update_dma_stop(dd); |
| 1286 | if (dd->err) { |
| 1287 | err = dd->err; |
| 1288 | goto finish; |
| 1289 | } |
| 1290 | } |
| 1291 | if (SHA_FLAGS_OUTPUT_READY & dd->flags) { |
| 1292 | /* hash or semi-hash ready */ |
| 1293 | dd->flags &= ~(SHA_FLAGS_DMA_READY | |
| 1294 | SHA_FLAGS_OUTPUT_READY); |
| 1295 | err = atmel_sha_update_dma_start(dd); |
| 1296 | if (err != -EINPROGRESS) |
| 1297 | goto finish; |
| 1298 | } |
| 1299 | } |
Cyrille Pitchen | b5ce82a | 2017-01-26 17:07:48 +0100 | [diff] [blame] | 1300 | return err; |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 1301 | |
| 1302 | finish: |
| 1303 | /* finish curent request */ |
| 1304 | atmel_sha_finish_req(dd->req, err); |
Cyrille Pitchen | b5ce82a | 2017-01-26 17:07:48 +0100 | [diff] [blame] | 1305 | |
| 1306 | return err; |
| 1307 | } |
| 1308 | |
| 1309 | static void atmel_sha_done_task(unsigned long data) |
| 1310 | { |
| 1311 | struct atmel_sha_dev *dd = (struct atmel_sha_dev *)data; |
| 1312 | |
| 1313 | dd->is_async = true; |
| 1314 | (void)dd->resume(dd); |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 1315 | } |
| 1316 | |
| 1317 | static irqreturn_t atmel_sha_irq(int irq, void *dev_id) |
| 1318 | { |
| 1319 | struct atmel_sha_dev *sha_dd = dev_id; |
| 1320 | u32 reg; |
| 1321 | |
| 1322 | reg = atmel_sha_read(sha_dd, SHA_ISR); |
| 1323 | if (reg & atmel_sha_read(sha_dd, SHA_IMR)) { |
| 1324 | atmel_sha_write(sha_dd, SHA_IDR, reg); |
| 1325 | if (SHA_FLAGS_BUSY & sha_dd->flags) { |
| 1326 | sha_dd->flags |= SHA_FLAGS_OUTPUT_READY; |
| 1327 | if (!(SHA_FLAGS_CPU & sha_dd->flags)) |
| 1328 | sha_dd->flags |= SHA_FLAGS_DMA_READY; |
| 1329 | tasklet_schedule(&sha_dd->done_task); |
| 1330 | } else { |
| 1331 | dev_warn(sha_dd->dev, "SHA interrupt when no active requests.\n"); |
| 1332 | } |
| 1333 | return IRQ_HANDLED; |
| 1334 | } |
| 1335 | |
| 1336 | return IRQ_NONE; |
| 1337 | } |
| 1338 | |
Cyrille Pitchen | eec12f6 | 2017-01-26 17:07:52 +0100 | [diff] [blame] | 1339 | |
Cyrille Pitchen | 69303cf | 2017-01-26 17:07:53 +0100 | [diff] [blame] | 1340 | /* DMA transfer functions */ |
| 1341 | |
| 1342 | static bool atmel_sha_dma_check_aligned(struct atmel_sha_dev *dd, |
| 1343 | struct scatterlist *sg, |
| 1344 | size_t len) |
| 1345 | { |
| 1346 | struct atmel_sha_dma *dma = &dd->dma_lch_in; |
| 1347 | struct ahash_request *req = dd->req; |
| 1348 | struct atmel_sha_reqctx *ctx = ahash_request_ctx(req); |
| 1349 | size_t bs = ctx->block_size; |
| 1350 | int nents; |
| 1351 | |
| 1352 | for (nents = 0; sg; sg = sg_next(sg), ++nents) { |
| 1353 | if (!IS_ALIGNED(sg->offset, sizeof(u32))) |
| 1354 | return false; |
| 1355 | |
| 1356 | /* |
| 1357 | * This is the last sg, the only one that is allowed to |
| 1358 | * have an unaligned length. |
| 1359 | */ |
| 1360 | if (len <= sg->length) { |
| 1361 | dma->nents = nents + 1; |
| 1362 | dma->last_sg_length = sg->length; |
| 1363 | sg->length = ALIGN(len, sizeof(u32)); |
| 1364 | return true; |
| 1365 | } |
| 1366 | |
| 1367 | /* All other sg lengths MUST be aligned to the block size. */ |
| 1368 | if (!IS_ALIGNED(sg->length, bs)) |
| 1369 | return false; |
| 1370 | |
| 1371 | len -= sg->length; |
| 1372 | } |
| 1373 | |
| 1374 | return false; |
| 1375 | } |
| 1376 | |
| 1377 | static void atmel_sha_dma_callback2(void *data) |
| 1378 | { |
| 1379 | struct atmel_sha_dev *dd = data; |
| 1380 | struct atmel_sha_dma *dma = &dd->dma_lch_in; |
| 1381 | struct scatterlist *sg; |
| 1382 | int nents; |
| 1383 | |
| 1384 | dmaengine_terminate_all(dma->chan); |
| 1385 | dma_unmap_sg(dd->dev, dma->sg, dma->nents, DMA_TO_DEVICE); |
| 1386 | |
| 1387 | sg = dma->sg; |
| 1388 | for (nents = 0; nents < dma->nents - 1; ++nents) |
| 1389 | sg = sg_next(sg); |
| 1390 | sg->length = dma->last_sg_length; |
| 1391 | |
| 1392 | dd->is_async = true; |
| 1393 | (void)atmel_sha_wait_for_data_ready(dd, dd->resume); |
| 1394 | } |
| 1395 | |
| 1396 | static int atmel_sha_dma_start(struct atmel_sha_dev *dd, |
| 1397 | struct scatterlist *src, |
| 1398 | size_t len, |
| 1399 | atmel_sha_fn_t resume) |
| 1400 | { |
| 1401 | struct atmel_sha_dma *dma = &dd->dma_lch_in; |
| 1402 | struct dma_slave_config *config = &dma->dma_conf; |
| 1403 | struct dma_chan *chan = dma->chan; |
| 1404 | struct dma_async_tx_descriptor *desc; |
| 1405 | dma_cookie_t cookie; |
| 1406 | unsigned int sg_len; |
| 1407 | int err; |
| 1408 | |
| 1409 | dd->resume = resume; |
| 1410 | |
| 1411 | /* |
| 1412 | * dma->nents has already been initialized by |
| 1413 | * atmel_sha_dma_check_aligned(). |
| 1414 | */ |
| 1415 | dma->sg = src; |
| 1416 | sg_len = dma_map_sg(dd->dev, dma->sg, dma->nents, DMA_TO_DEVICE); |
| 1417 | if (!sg_len) { |
| 1418 | err = -ENOMEM; |
| 1419 | goto exit; |
| 1420 | } |
| 1421 | |
| 1422 | config->src_maxburst = 16; |
| 1423 | config->dst_maxburst = 16; |
| 1424 | err = dmaengine_slave_config(chan, config); |
| 1425 | if (err) |
| 1426 | goto unmap_sg; |
| 1427 | |
| 1428 | desc = dmaengine_prep_slave_sg(chan, dma->sg, sg_len, DMA_MEM_TO_DEV, |
| 1429 | DMA_PREP_INTERRUPT | DMA_CTRL_ACK); |
| 1430 | if (!desc) { |
| 1431 | err = -ENOMEM; |
| 1432 | goto unmap_sg; |
| 1433 | } |
| 1434 | |
| 1435 | desc->callback = atmel_sha_dma_callback2; |
| 1436 | desc->callback_param = dd; |
| 1437 | cookie = dmaengine_submit(desc); |
| 1438 | err = dma_submit_error(cookie); |
| 1439 | if (err) |
| 1440 | goto unmap_sg; |
| 1441 | |
| 1442 | dma_async_issue_pending(chan); |
| 1443 | |
| 1444 | return -EINPROGRESS; |
| 1445 | |
| 1446 | unmap_sg: |
| 1447 | dma_unmap_sg(dd->dev, dma->sg, dma->nents, DMA_TO_DEVICE); |
| 1448 | exit: |
| 1449 | return atmel_sha_complete(dd, err); |
| 1450 | } |
| 1451 | |
| 1452 | |
Cyrille Pitchen | eec12f6 | 2017-01-26 17:07:52 +0100 | [diff] [blame] | 1453 | /* CPU transfer functions */ |
| 1454 | |
| 1455 | static int atmel_sha_cpu_transfer(struct atmel_sha_dev *dd) |
| 1456 | { |
| 1457 | struct ahash_request *req = dd->req; |
| 1458 | struct atmel_sha_reqctx *ctx = ahash_request_ctx(req); |
| 1459 | const u32 *words = (const u32 *)ctx->buffer; |
| 1460 | size_t i, num_words; |
| 1461 | u32 isr, din, din_inc; |
| 1462 | |
| 1463 | din_inc = (ctx->flags & SHA_FLAGS_IDATAR0) ? 0 : 1; |
| 1464 | for (;;) { |
| 1465 | /* Write data into the Input Data Registers. */ |
| 1466 | num_words = DIV_ROUND_UP(ctx->bufcnt, sizeof(u32)); |
| 1467 | for (i = 0, din = 0; i < num_words; ++i, din += din_inc) |
| 1468 | atmel_sha_write(dd, SHA_REG_DIN(din), words[i]); |
| 1469 | |
| 1470 | ctx->offset += ctx->bufcnt; |
| 1471 | ctx->total -= ctx->bufcnt; |
| 1472 | |
| 1473 | if (!ctx->total) |
| 1474 | break; |
| 1475 | |
| 1476 | /* |
| 1477 | * Prepare next block: |
| 1478 | * Fill ctx->buffer now with the next data to be written into |
| 1479 | * IDATARx: it gives time for the SHA hardware to process |
| 1480 | * the current data so the SHA_INT_DATARDY flag might be set |
| 1481 | * in SHA_ISR when polling this register at the beginning of |
| 1482 | * the next loop. |
| 1483 | */ |
| 1484 | ctx->bufcnt = min_t(size_t, ctx->block_size, ctx->total); |
| 1485 | scatterwalk_map_and_copy(ctx->buffer, ctx->sg, |
| 1486 | ctx->offset, ctx->bufcnt, 0); |
| 1487 | |
| 1488 | /* Wait for hardware to be ready again. */ |
| 1489 | isr = atmel_sha_read(dd, SHA_ISR); |
| 1490 | if (!(isr & SHA_INT_DATARDY)) { |
| 1491 | /* Not ready yet. */ |
| 1492 | dd->resume = atmel_sha_cpu_transfer; |
| 1493 | atmel_sha_write(dd, SHA_IER, SHA_INT_DATARDY); |
| 1494 | return -EINPROGRESS; |
| 1495 | } |
| 1496 | } |
| 1497 | |
| 1498 | if (unlikely(!(ctx->flags & SHA_FLAGS_WAIT_DATARDY))) |
| 1499 | return dd->cpu_transfer_complete(dd); |
| 1500 | |
| 1501 | return atmel_sha_wait_for_data_ready(dd, dd->cpu_transfer_complete); |
| 1502 | } |
| 1503 | |
| 1504 | static int atmel_sha_cpu_start(struct atmel_sha_dev *dd, |
| 1505 | struct scatterlist *sg, |
| 1506 | unsigned int len, |
| 1507 | bool idatar0_only, |
| 1508 | bool wait_data_ready, |
| 1509 | atmel_sha_fn_t resume) |
| 1510 | { |
| 1511 | struct ahash_request *req = dd->req; |
| 1512 | struct atmel_sha_reqctx *ctx = ahash_request_ctx(req); |
| 1513 | |
| 1514 | if (!len) |
| 1515 | return resume(dd); |
| 1516 | |
| 1517 | ctx->flags &= ~(SHA_FLAGS_IDATAR0 | SHA_FLAGS_WAIT_DATARDY); |
| 1518 | |
| 1519 | if (idatar0_only) |
| 1520 | ctx->flags |= SHA_FLAGS_IDATAR0; |
| 1521 | |
| 1522 | if (wait_data_ready) |
| 1523 | ctx->flags |= SHA_FLAGS_WAIT_DATARDY; |
| 1524 | |
| 1525 | ctx->sg = sg; |
| 1526 | ctx->total = len; |
| 1527 | ctx->offset = 0; |
| 1528 | |
| 1529 | /* Prepare the first block to be written. */ |
| 1530 | ctx->bufcnt = min_t(size_t, ctx->block_size, ctx->total); |
| 1531 | scatterwalk_map_and_copy(ctx->buffer, ctx->sg, |
| 1532 | ctx->offset, ctx->bufcnt, 0); |
| 1533 | |
| 1534 | dd->cpu_transfer_complete = resume; |
| 1535 | return atmel_sha_cpu_transfer(dd); |
| 1536 | } |
| 1537 | |
Cyrille Pitchen | 81d8750 | 2017-01-26 17:07:54 +0100 | [diff] [blame^] | 1538 | static int atmel_sha_cpu_hash(struct atmel_sha_dev *dd, |
| 1539 | const void *data, unsigned int datalen, |
| 1540 | bool auto_padding, |
| 1541 | atmel_sha_fn_t resume) |
| 1542 | { |
| 1543 | struct ahash_request *req = dd->req; |
| 1544 | struct atmel_sha_reqctx *ctx = ahash_request_ctx(req); |
| 1545 | u32 msglen = (auto_padding) ? datalen : 0; |
| 1546 | u32 mr = SHA_MR_MODE_AUTO; |
| 1547 | |
| 1548 | if (!(IS_ALIGNED(datalen, ctx->block_size) || auto_padding)) |
| 1549 | return atmel_sha_complete(dd, -EINVAL); |
| 1550 | |
| 1551 | mr |= (ctx->flags & SHA_FLAGS_ALGO_MASK); |
| 1552 | atmel_sha_write(dd, SHA_MR, mr); |
| 1553 | atmel_sha_write(dd, SHA_MSR, msglen); |
| 1554 | atmel_sha_write(dd, SHA_BCR, msglen); |
| 1555 | atmel_sha_write(dd, SHA_CR, SHA_CR_FIRST); |
| 1556 | |
| 1557 | sg_init_one(&dd->tmp, data, datalen); |
| 1558 | return atmel_sha_cpu_start(dd, &dd->tmp, datalen, false, true, resume); |
| 1559 | } |
| 1560 | |
| 1561 | |
| 1562 | /* hmac functions */ |
| 1563 | |
| 1564 | struct atmel_sha_hmac_key { |
| 1565 | bool valid; |
| 1566 | unsigned int keylen; |
| 1567 | u8 buffer[SHA512_BLOCK_SIZE]; |
| 1568 | u8 *keydup; |
| 1569 | }; |
| 1570 | |
| 1571 | static inline void atmel_sha_hmac_key_init(struct atmel_sha_hmac_key *hkey) |
| 1572 | { |
| 1573 | memset(hkey, 0, sizeof(*hkey)); |
| 1574 | } |
| 1575 | |
| 1576 | static inline void atmel_sha_hmac_key_release(struct atmel_sha_hmac_key *hkey) |
| 1577 | { |
| 1578 | kfree(hkey->keydup); |
| 1579 | memset(hkey, 0, sizeof(*hkey)); |
| 1580 | } |
| 1581 | |
| 1582 | static inline int atmel_sha_hmac_key_set(struct atmel_sha_hmac_key *hkey, |
| 1583 | const u8 *key, |
| 1584 | unsigned int keylen) |
| 1585 | { |
| 1586 | atmel_sha_hmac_key_release(hkey); |
| 1587 | |
| 1588 | if (keylen > sizeof(hkey->buffer)) { |
| 1589 | hkey->keydup = kmemdup(key, keylen, GFP_KERNEL); |
| 1590 | if (!hkey->keydup) |
| 1591 | return -ENOMEM; |
| 1592 | |
| 1593 | } else { |
| 1594 | memcpy(hkey->buffer, key, keylen); |
| 1595 | } |
| 1596 | |
| 1597 | hkey->valid = true; |
| 1598 | hkey->keylen = keylen; |
| 1599 | return 0; |
| 1600 | } |
| 1601 | |
| 1602 | static inline bool atmel_sha_hmac_key_get(const struct atmel_sha_hmac_key *hkey, |
| 1603 | const u8 **key, |
| 1604 | unsigned int *keylen) |
| 1605 | { |
| 1606 | if (!hkey->valid) |
| 1607 | return false; |
| 1608 | |
| 1609 | *keylen = hkey->keylen; |
| 1610 | *key = (hkey->keydup) ? hkey->keydup : hkey->buffer; |
| 1611 | return true; |
| 1612 | } |
| 1613 | |
| 1614 | |
| 1615 | struct atmel_sha_hmac_ctx { |
| 1616 | struct atmel_sha_ctx base; |
| 1617 | |
| 1618 | struct atmel_sha_hmac_key hkey; |
| 1619 | u32 ipad[SHA512_BLOCK_SIZE / sizeof(u32)]; |
| 1620 | u32 opad[SHA512_BLOCK_SIZE / sizeof(u32)]; |
| 1621 | atmel_sha_fn_t resume; |
| 1622 | }; |
| 1623 | |
| 1624 | static int atmel_sha_hmac_setup(struct atmel_sha_dev *dd, |
| 1625 | atmel_sha_fn_t resume); |
| 1626 | static int atmel_sha_hmac_prehash_key(struct atmel_sha_dev *dd, |
| 1627 | const u8 *key, unsigned int keylen); |
| 1628 | static int atmel_sha_hmac_prehash_key_done(struct atmel_sha_dev *dd); |
| 1629 | static int atmel_sha_hmac_compute_ipad_hash(struct atmel_sha_dev *dd); |
| 1630 | static int atmel_sha_hmac_compute_opad_hash(struct atmel_sha_dev *dd); |
| 1631 | static int atmel_sha_hmac_setup_done(struct atmel_sha_dev *dd); |
| 1632 | |
| 1633 | static int atmel_sha_hmac_init_done(struct atmel_sha_dev *dd); |
| 1634 | static int atmel_sha_hmac_final(struct atmel_sha_dev *dd); |
| 1635 | static int atmel_sha_hmac_final_done(struct atmel_sha_dev *dd); |
| 1636 | static int atmel_sha_hmac_digest2(struct atmel_sha_dev *dd); |
| 1637 | |
| 1638 | static int atmel_sha_hmac_setup(struct atmel_sha_dev *dd, |
| 1639 | atmel_sha_fn_t resume) |
| 1640 | { |
| 1641 | struct ahash_request *req = dd->req; |
| 1642 | struct atmel_sha_reqctx *ctx = ahash_request_ctx(req); |
| 1643 | struct crypto_ahash *tfm = crypto_ahash_reqtfm(req); |
| 1644 | struct atmel_sha_hmac_ctx *hmac = crypto_ahash_ctx(tfm); |
| 1645 | unsigned int keylen; |
| 1646 | const u8 *key; |
| 1647 | size_t bs; |
| 1648 | |
| 1649 | hmac->resume = resume; |
| 1650 | switch (ctx->flags & SHA_FLAGS_ALGO_MASK) { |
| 1651 | case SHA_FLAGS_SHA1: |
| 1652 | ctx->block_size = SHA1_BLOCK_SIZE; |
| 1653 | ctx->hash_size = SHA1_DIGEST_SIZE; |
| 1654 | break; |
| 1655 | |
| 1656 | case SHA_FLAGS_SHA224: |
| 1657 | ctx->block_size = SHA224_BLOCK_SIZE; |
| 1658 | ctx->hash_size = SHA256_DIGEST_SIZE; |
| 1659 | break; |
| 1660 | |
| 1661 | case SHA_FLAGS_SHA256: |
| 1662 | ctx->block_size = SHA256_BLOCK_SIZE; |
| 1663 | ctx->hash_size = SHA256_DIGEST_SIZE; |
| 1664 | break; |
| 1665 | |
| 1666 | case SHA_FLAGS_SHA384: |
| 1667 | ctx->block_size = SHA384_BLOCK_SIZE; |
| 1668 | ctx->hash_size = SHA512_DIGEST_SIZE; |
| 1669 | break; |
| 1670 | |
| 1671 | case SHA_FLAGS_SHA512: |
| 1672 | ctx->block_size = SHA512_BLOCK_SIZE; |
| 1673 | ctx->hash_size = SHA512_DIGEST_SIZE; |
| 1674 | break; |
| 1675 | |
| 1676 | default: |
| 1677 | return atmel_sha_complete(dd, -EINVAL); |
| 1678 | } |
| 1679 | bs = ctx->block_size; |
| 1680 | |
| 1681 | if (likely(!atmel_sha_hmac_key_get(&hmac->hkey, &key, &keylen))) |
| 1682 | return resume(dd); |
| 1683 | |
| 1684 | /* Compute K' from K. */ |
| 1685 | if (unlikely(keylen > bs)) |
| 1686 | return atmel_sha_hmac_prehash_key(dd, key, keylen); |
| 1687 | |
| 1688 | /* Prepare ipad. */ |
| 1689 | memcpy((u8 *)hmac->ipad, key, keylen); |
| 1690 | memset((u8 *)hmac->ipad + keylen, 0, bs - keylen); |
| 1691 | return atmel_sha_hmac_compute_ipad_hash(dd); |
| 1692 | } |
| 1693 | |
| 1694 | static int atmel_sha_hmac_prehash_key(struct atmel_sha_dev *dd, |
| 1695 | const u8 *key, unsigned int keylen) |
| 1696 | { |
| 1697 | return atmel_sha_cpu_hash(dd, key, keylen, true, |
| 1698 | atmel_sha_hmac_prehash_key_done); |
| 1699 | } |
| 1700 | |
| 1701 | static int atmel_sha_hmac_prehash_key_done(struct atmel_sha_dev *dd) |
| 1702 | { |
| 1703 | struct ahash_request *req = dd->req; |
| 1704 | struct crypto_ahash *tfm = crypto_ahash_reqtfm(req); |
| 1705 | struct atmel_sha_hmac_ctx *hmac = crypto_ahash_ctx(tfm); |
| 1706 | struct atmel_sha_reqctx *ctx = ahash_request_ctx(req); |
| 1707 | size_t ds = crypto_ahash_digestsize(tfm); |
| 1708 | size_t bs = ctx->block_size; |
| 1709 | size_t i, num_words = ds / sizeof(u32); |
| 1710 | |
| 1711 | /* Prepare ipad. */ |
| 1712 | for (i = 0; i < num_words; ++i) |
| 1713 | hmac->ipad[i] = atmel_sha_read(dd, SHA_REG_DIGEST(i)); |
| 1714 | memset((u8 *)hmac->ipad + ds, 0, bs - ds); |
| 1715 | return atmel_sha_hmac_compute_ipad_hash(dd); |
| 1716 | } |
| 1717 | |
| 1718 | static int atmel_sha_hmac_compute_ipad_hash(struct atmel_sha_dev *dd) |
| 1719 | { |
| 1720 | struct ahash_request *req = dd->req; |
| 1721 | struct crypto_ahash *tfm = crypto_ahash_reqtfm(req); |
| 1722 | struct atmel_sha_hmac_ctx *hmac = crypto_ahash_ctx(tfm); |
| 1723 | struct atmel_sha_reqctx *ctx = ahash_request_ctx(req); |
| 1724 | size_t bs = ctx->block_size; |
| 1725 | size_t i, num_words = bs / sizeof(u32); |
| 1726 | |
| 1727 | memcpy(hmac->opad, hmac->ipad, bs); |
| 1728 | for (i = 0; i < num_words; ++i) { |
| 1729 | hmac->ipad[i] ^= 0x36363636; |
| 1730 | hmac->opad[i] ^= 0x5c5c5c5c; |
| 1731 | } |
| 1732 | |
| 1733 | return atmel_sha_cpu_hash(dd, hmac->ipad, bs, false, |
| 1734 | atmel_sha_hmac_compute_opad_hash); |
| 1735 | } |
| 1736 | |
| 1737 | static int atmel_sha_hmac_compute_opad_hash(struct atmel_sha_dev *dd) |
| 1738 | { |
| 1739 | struct ahash_request *req = dd->req; |
| 1740 | struct crypto_ahash *tfm = crypto_ahash_reqtfm(req); |
| 1741 | struct atmel_sha_hmac_ctx *hmac = crypto_ahash_ctx(tfm); |
| 1742 | struct atmel_sha_reqctx *ctx = ahash_request_ctx(req); |
| 1743 | size_t bs = ctx->block_size; |
| 1744 | size_t hs = ctx->hash_size; |
| 1745 | size_t i, num_words = hs / sizeof(u32); |
| 1746 | |
| 1747 | for (i = 0; i < num_words; ++i) |
| 1748 | hmac->ipad[i] = atmel_sha_read(dd, SHA_REG_DIGEST(i)); |
| 1749 | return atmel_sha_cpu_hash(dd, hmac->opad, bs, false, |
| 1750 | atmel_sha_hmac_setup_done); |
| 1751 | } |
| 1752 | |
| 1753 | static int atmel_sha_hmac_setup_done(struct atmel_sha_dev *dd) |
| 1754 | { |
| 1755 | struct ahash_request *req = dd->req; |
| 1756 | struct crypto_ahash *tfm = crypto_ahash_reqtfm(req); |
| 1757 | struct atmel_sha_hmac_ctx *hmac = crypto_ahash_ctx(tfm); |
| 1758 | struct atmel_sha_reqctx *ctx = ahash_request_ctx(req); |
| 1759 | size_t hs = ctx->hash_size; |
| 1760 | size_t i, num_words = hs / sizeof(u32); |
| 1761 | |
| 1762 | for (i = 0; i < num_words; ++i) |
| 1763 | hmac->opad[i] = atmel_sha_read(dd, SHA_REG_DIGEST(i)); |
| 1764 | atmel_sha_hmac_key_release(&hmac->hkey); |
| 1765 | return hmac->resume(dd); |
| 1766 | } |
| 1767 | |
| 1768 | static int atmel_sha_hmac_start(struct atmel_sha_dev *dd) |
| 1769 | { |
| 1770 | struct ahash_request *req = dd->req; |
| 1771 | struct atmel_sha_reqctx *ctx = ahash_request_ctx(req); |
| 1772 | int err; |
| 1773 | |
| 1774 | err = atmel_sha_hw_init(dd); |
| 1775 | if (err) |
| 1776 | return atmel_sha_complete(dd, err); |
| 1777 | |
| 1778 | switch (ctx->op) { |
| 1779 | case SHA_OP_INIT: |
| 1780 | err = atmel_sha_hmac_setup(dd, atmel_sha_hmac_init_done); |
| 1781 | break; |
| 1782 | |
| 1783 | case SHA_OP_UPDATE: |
| 1784 | dd->resume = atmel_sha_done; |
| 1785 | err = atmel_sha_update_req(dd); |
| 1786 | break; |
| 1787 | |
| 1788 | case SHA_OP_FINAL: |
| 1789 | dd->resume = atmel_sha_hmac_final; |
| 1790 | err = atmel_sha_final_req(dd); |
| 1791 | break; |
| 1792 | |
| 1793 | case SHA_OP_DIGEST: |
| 1794 | err = atmel_sha_hmac_setup(dd, atmel_sha_hmac_digest2); |
| 1795 | break; |
| 1796 | |
| 1797 | default: |
| 1798 | return atmel_sha_complete(dd, -EINVAL); |
| 1799 | } |
| 1800 | |
| 1801 | return err; |
| 1802 | } |
| 1803 | |
| 1804 | static int atmel_sha_hmac_setkey(struct crypto_ahash *tfm, const u8 *key, |
| 1805 | unsigned int keylen) |
| 1806 | { |
| 1807 | struct atmel_sha_hmac_ctx *hmac = crypto_ahash_ctx(tfm); |
| 1808 | |
| 1809 | if (atmel_sha_hmac_key_set(&hmac->hkey, key, keylen)) { |
| 1810 | crypto_ahash_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN); |
| 1811 | return -EINVAL; |
| 1812 | } |
| 1813 | |
| 1814 | return 0; |
| 1815 | } |
| 1816 | |
| 1817 | static int atmel_sha_hmac_init(struct ahash_request *req) |
| 1818 | { |
| 1819 | int err; |
| 1820 | |
| 1821 | err = atmel_sha_init(req); |
| 1822 | if (err) |
| 1823 | return err; |
| 1824 | |
| 1825 | return atmel_sha_enqueue(req, SHA_OP_INIT); |
| 1826 | } |
| 1827 | |
| 1828 | static int atmel_sha_hmac_init_done(struct atmel_sha_dev *dd) |
| 1829 | { |
| 1830 | struct ahash_request *req = dd->req; |
| 1831 | struct atmel_sha_reqctx *ctx = ahash_request_ctx(req); |
| 1832 | struct crypto_ahash *tfm = crypto_ahash_reqtfm(req); |
| 1833 | struct atmel_sha_hmac_ctx *hmac = crypto_ahash_ctx(tfm); |
| 1834 | size_t bs = ctx->block_size; |
| 1835 | size_t hs = ctx->hash_size; |
| 1836 | |
| 1837 | ctx->bufcnt = 0; |
| 1838 | ctx->digcnt[0] = bs; |
| 1839 | ctx->digcnt[1] = 0; |
| 1840 | ctx->flags |= SHA_FLAGS_RESTORE; |
| 1841 | memcpy(ctx->digest, hmac->ipad, hs); |
| 1842 | return atmel_sha_complete(dd, 0); |
| 1843 | } |
| 1844 | |
| 1845 | static int atmel_sha_hmac_final(struct atmel_sha_dev *dd) |
| 1846 | { |
| 1847 | struct ahash_request *req = dd->req; |
| 1848 | struct atmel_sha_reqctx *ctx = ahash_request_ctx(req); |
| 1849 | struct crypto_ahash *tfm = crypto_ahash_reqtfm(req); |
| 1850 | struct atmel_sha_hmac_ctx *hmac = crypto_ahash_ctx(tfm); |
| 1851 | u32 *digest = (u32 *)ctx->digest; |
| 1852 | size_t ds = crypto_ahash_digestsize(tfm); |
| 1853 | size_t bs = ctx->block_size; |
| 1854 | size_t hs = ctx->hash_size; |
| 1855 | size_t i, num_words; |
| 1856 | u32 mr; |
| 1857 | |
| 1858 | /* Save d = SHA((K' + ipad) | msg). */ |
| 1859 | num_words = ds / sizeof(u32); |
| 1860 | for (i = 0; i < num_words; ++i) |
| 1861 | digest[i] = atmel_sha_read(dd, SHA_REG_DIGEST(i)); |
| 1862 | |
| 1863 | /* Restore context to finish computing SHA((K' + opad) | d). */ |
| 1864 | atmel_sha_write(dd, SHA_CR, SHA_CR_WUIHV); |
| 1865 | num_words = hs / sizeof(u32); |
| 1866 | for (i = 0; i < num_words; ++i) |
| 1867 | atmel_sha_write(dd, SHA_REG_DIN(i), hmac->opad[i]); |
| 1868 | |
| 1869 | mr = SHA_MR_MODE_AUTO | SHA_MR_UIHV; |
| 1870 | mr |= (ctx->flags & SHA_FLAGS_ALGO_MASK); |
| 1871 | atmel_sha_write(dd, SHA_MR, mr); |
| 1872 | atmel_sha_write(dd, SHA_MSR, bs + ds); |
| 1873 | atmel_sha_write(dd, SHA_BCR, ds); |
| 1874 | atmel_sha_write(dd, SHA_CR, SHA_CR_FIRST); |
| 1875 | |
| 1876 | sg_init_one(&dd->tmp, digest, ds); |
| 1877 | return atmel_sha_cpu_start(dd, &dd->tmp, ds, false, true, |
| 1878 | atmel_sha_hmac_final_done); |
| 1879 | } |
| 1880 | |
| 1881 | static int atmel_sha_hmac_final_done(struct atmel_sha_dev *dd) |
| 1882 | { |
| 1883 | /* |
| 1884 | * req->result might not be sizeof(u32) aligned, so copy the |
| 1885 | * digest into ctx->digest[] before memcpy() the data into |
| 1886 | * req->result. |
| 1887 | */ |
| 1888 | atmel_sha_copy_hash(dd->req); |
| 1889 | atmel_sha_copy_ready_hash(dd->req); |
| 1890 | return atmel_sha_complete(dd, 0); |
| 1891 | } |
| 1892 | |
| 1893 | static int atmel_sha_hmac_digest(struct ahash_request *req) |
| 1894 | { |
| 1895 | int err; |
| 1896 | |
| 1897 | err = atmel_sha_init(req); |
| 1898 | if (err) |
| 1899 | return err; |
| 1900 | |
| 1901 | return atmel_sha_enqueue(req, SHA_OP_DIGEST); |
| 1902 | } |
| 1903 | |
| 1904 | static int atmel_sha_hmac_digest2(struct atmel_sha_dev *dd) |
| 1905 | { |
| 1906 | struct ahash_request *req = dd->req; |
| 1907 | struct atmel_sha_reqctx *ctx = ahash_request_ctx(req); |
| 1908 | struct crypto_ahash *tfm = crypto_ahash_reqtfm(req); |
| 1909 | struct atmel_sha_hmac_ctx *hmac = crypto_ahash_ctx(tfm); |
| 1910 | size_t hs = ctx->hash_size; |
| 1911 | size_t i, num_words = hs / sizeof(u32); |
| 1912 | bool use_dma = false; |
| 1913 | u32 mr; |
| 1914 | |
| 1915 | /* Special case for empty message. */ |
| 1916 | if (!req->nbytes) |
| 1917 | return atmel_sha_complete(dd, -EINVAL); // TODO: |
| 1918 | |
| 1919 | /* Check DMA threshold and alignment. */ |
| 1920 | if (req->nbytes > ATMEL_SHA_DMA_THRESHOLD && |
| 1921 | atmel_sha_dma_check_aligned(dd, req->src, req->nbytes)) |
| 1922 | use_dma = true; |
| 1923 | |
| 1924 | /* Write both initial hash values to compute a HMAC. */ |
| 1925 | atmel_sha_write(dd, SHA_CR, SHA_CR_WUIHV); |
| 1926 | for (i = 0; i < num_words; ++i) |
| 1927 | atmel_sha_write(dd, SHA_REG_DIN(i), hmac->ipad[i]); |
| 1928 | |
| 1929 | atmel_sha_write(dd, SHA_CR, SHA_CR_WUIEHV); |
| 1930 | for (i = 0; i < num_words; ++i) |
| 1931 | atmel_sha_write(dd, SHA_REG_DIN(i), hmac->opad[i]); |
| 1932 | |
| 1933 | /* Write the Mode, Message Size, Bytes Count then Control Registers. */ |
| 1934 | mr = (SHA_MR_HMAC | SHA_MR_DUALBUFF); |
| 1935 | mr |= ctx->flags & SHA_FLAGS_ALGO_MASK; |
| 1936 | if (use_dma) |
| 1937 | mr |= SHA_MR_MODE_IDATAR0; |
| 1938 | else |
| 1939 | mr |= SHA_MR_MODE_AUTO; |
| 1940 | atmel_sha_write(dd, SHA_MR, mr); |
| 1941 | |
| 1942 | atmel_sha_write(dd, SHA_MSR, req->nbytes); |
| 1943 | atmel_sha_write(dd, SHA_BCR, req->nbytes); |
| 1944 | |
| 1945 | atmel_sha_write(dd, SHA_CR, SHA_CR_FIRST); |
| 1946 | |
| 1947 | /* Process data. */ |
| 1948 | if (use_dma) |
| 1949 | return atmel_sha_dma_start(dd, req->src, req->nbytes, |
| 1950 | atmel_sha_hmac_final_done); |
| 1951 | |
| 1952 | return atmel_sha_cpu_start(dd, req->src, req->nbytes, false, true, |
| 1953 | atmel_sha_hmac_final_done); |
| 1954 | } |
| 1955 | |
| 1956 | static int atmel_sha_hmac_cra_init(struct crypto_tfm *tfm) |
| 1957 | { |
| 1958 | struct atmel_sha_hmac_ctx *hmac = crypto_tfm_ctx(tfm); |
| 1959 | |
| 1960 | crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm), |
| 1961 | sizeof(struct atmel_sha_reqctx)); |
| 1962 | hmac->base.start = atmel_sha_hmac_start; |
| 1963 | atmel_sha_hmac_key_init(&hmac->hkey); |
| 1964 | |
| 1965 | return 0; |
| 1966 | } |
| 1967 | |
| 1968 | static void atmel_sha_hmac_cra_exit(struct crypto_tfm *tfm) |
| 1969 | { |
| 1970 | struct atmel_sha_hmac_ctx *hmac = crypto_tfm_ctx(tfm); |
| 1971 | |
| 1972 | atmel_sha_hmac_key_release(&hmac->hkey); |
| 1973 | } |
| 1974 | |
| 1975 | static struct ahash_alg sha_hmac_algs[] = { |
| 1976 | { |
| 1977 | .init = atmel_sha_hmac_init, |
| 1978 | .update = atmel_sha_update, |
| 1979 | .final = atmel_sha_final, |
| 1980 | .digest = atmel_sha_hmac_digest, |
| 1981 | .setkey = atmel_sha_hmac_setkey, |
| 1982 | .export = atmel_sha_export, |
| 1983 | .import = atmel_sha_import, |
| 1984 | .halg = { |
| 1985 | .digestsize = SHA1_DIGEST_SIZE, |
| 1986 | .statesize = sizeof(struct atmel_sha_reqctx), |
| 1987 | .base = { |
| 1988 | .cra_name = "hmac(sha1)", |
| 1989 | .cra_driver_name = "atmel-hmac-sha1", |
| 1990 | .cra_priority = 100, |
| 1991 | .cra_flags = CRYPTO_ALG_ASYNC, |
| 1992 | .cra_blocksize = SHA1_BLOCK_SIZE, |
| 1993 | .cra_ctxsize = sizeof(struct atmel_sha_hmac_ctx), |
| 1994 | .cra_alignmask = 0, |
| 1995 | .cra_module = THIS_MODULE, |
| 1996 | .cra_init = atmel_sha_hmac_cra_init, |
| 1997 | .cra_exit = atmel_sha_hmac_cra_exit, |
| 1998 | } |
| 1999 | } |
| 2000 | }, |
| 2001 | { |
| 2002 | .init = atmel_sha_hmac_init, |
| 2003 | .update = atmel_sha_update, |
| 2004 | .final = atmel_sha_final, |
| 2005 | .digest = atmel_sha_hmac_digest, |
| 2006 | .setkey = atmel_sha_hmac_setkey, |
| 2007 | .export = atmel_sha_export, |
| 2008 | .import = atmel_sha_import, |
| 2009 | .halg = { |
| 2010 | .digestsize = SHA224_DIGEST_SIZE, |
| 2011 | .statesize = sizeof(struct atmel_sha_reqctx), |
| 2012 | .base = { |
| 2013 | .cra_name = "hmac(sha224)", |
| 2014 | .cra_driver_name = "atmel-hmac-sha224", |
| 2015 | .cra_priority = 100, |
| 2016 | .cra_flags = CRYPTO_ALG_ASYNC, |
| 2017 | .cra_blocksize = SHA224_BLOCK_SIZE, |
| 2018 | .cra_ctxsize = sizeof(struct atmel_sha_hmac_ctx), |
| 2019 | .cra_alignmask = 0, |
| 2020 | .cra_module = THIS_MODULE, |
| 2021 | .cra_init = atmel_sha_hmac_cra_init, |
| 2022 | .cra_exit = atmel_sha_hmac_cra_exit, |
| 2023 | } |
| 2024 | } |
| 2025 | }, |
| 2026 | { |
| 2027 | .init = atmel_sha_hmac_init, |
| 2028 | .update = atmel_sha_update, |
| 2029 | .final = atmel_sha_final, |
| 2030 | .digest = atmel_sha_hmac_digest, |
| 2031 | .setkey = atmel_sha_hmac_setkey, |
| 2032 | .export = atmel_sha_export, |
| 2033 | .import = atmel_sha_import, |
| 2034 | .halg = { |
| 2035 | .digestsize = SHA256_DIGEST_SIZE, |
| 2036 | .statesize = sizeof(struct atmel_sha_reqctx), |
| 2037 | .base = { |
| 2038 | .cra_name = "hmac(sha256)", |
| 2039 | .cra_driver_name = "atmel-hmac-sha256", |
| 2040 | .cra_priority = 100, |
| 2041 | .cra_flags = CRYPTO_ALG_ASYNC, |
| 2042 | .cra_blocksize = SHA256_BLOCK_SIZE, |
| 2043 | .cra_ctxsize = sizeof(struct atmel_sha_hmac_ctx), |
| 2044 | .cra_alignmask = 0, |
| 2045 | .cra_module = THIS_MODULE, |
| 2046 | .cra_init = atmel_sha_hmac_cra_init, |
| 2047 | .cra_exit = atmel_sha_hmac_cra_exit, |
| 2048 | } |
| 2049 | } |
| 2050 | }, |
| 2051 | { |
| 2052 | .init = atmel_sha_hmac_init, |
| 2053 | .update = atmel_sha_update, |
| 2054 | .final = atmel_sha_final, |
| 2055 | .digest = atmel_sha_hmac_digest, |
| 2056 | .setkey = atmel_sha_hmac_setkey, |
| 2057 | .export = atmel_sha_export, |
| 2058 | .import = atmel_sha_import, |
| 2059 | .halg = { |
| 2060 | .digestsize = SHA384_DIGEST_SIZE, |
| 2061 | .statesize = sizeof(struct atmel_sha_reqctx), |
| 2062 | .base = { |
| 2063 | .cra_name = "hmac(sha384)", |
| 2064 | .cra_driver_name = "atmel-hmac-sha384", |
| 2065 | .cra_priority = 100, |
| 2066 | .cra_flags = CRYPTO_ALG_ASYNC, |
| 2067 | .cra_blocksize = SHA384_BLOCK_SIZE, |
| 2068 | .cra_ctxsize = sizeof(struct atmel_sha_hmac_ctx), |
| 2069 | .cra_alignmask = 0, |
| 2070 | .cra_module = THIS_MODULE, |
| 2071 | .cra_init = atmel_sha_hmac_cra_init, |
| 2072 | .cra_exit = atmel_sha_hmac_cra_exit, |
| 2073 | } |
| 2074 | } |
| 2075 | }, |
| 2076 | { |
| 2077 | .init = atmel_sha_hmac_init, |
| 2078 | .update = atmel_sha_update, |
| 2079 | .final = atmel_sha_final, |
| 2080 | .digest = atmel_sha_hmac_digest, |
| 2081 | .setkey = atmel_sha_hmac_setkey, |
| 2082 | .export = atmel_sha_export, |
| 2083 | .import = atmel_sha_import, |
| 2084 | .halg = { |
| 2085 | .digestsize = SHA512_DIGEST_SIZE, |
| 2086 | .statesize = sizeof(struct atmel_sha_reqctx), |
| 2087 | .base = { |
| 2088 | .cra_name = "hmac(sha512)", |
| 2089 | .cra_driver_name = "atmel-hmac-sha512", |
| 2090 | .cra_priority = 100, |
| 2091 | .cra_flags = CRYPTO_ALG_ASYNC, |
| 2092 | .cra_blocksize = SHA512_BLOCK_SIZE, |
| 2093 | .cra_ctxsize = sizeof(struct atmel_sha_hmac_ctx), |
| 2094 | .cra_alignmask = 0, |
| 2095 | .cra_module = THIS_MODULE, |
| 2096 | .cra_init = atmel_sha_hmac_cra_init, |
| 2097 | .cra_exit = atmel_sha_hmac_cra_exit, |
| 2098 | } |
| 2099 | } |
| 2100 | }, |
| 2101 | }; |
Cyrille Pitchen | eec12f6 | 2017-01-26 17:07:52 +0100 | [diff] [blame] | 2102 | |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 2103 | static void atmel_sha_unregister_algs(struct atmel_sha_dev *dd) |
| 2104 | { |
| 2105 | int i; |
| 2106 | |
Cyrille Pitchen | 81d8750 | 2017-01-26 17:07:54 +0100 | [diff] [blame^] | 2107 | if (dd->caps.has_hmac) |
| 2108 | for (i = 0; i < ARRAY_SIZE(sha_hmac_algs); i++) |
| 2109 | crypto_unregister_ahash(&sha_hmac_algs[i]); |
| 2110 | |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 2111 | for (i = 0; i < ARRAY_SIZE(sha_1_256_algs); i++) |
| 2112 | crypto_unregister_ahash(&sha_1_256_algs[i]); |
| 2113 | |
| 2114 | if (dd->caps.has_sha224) |
| 2115 | crypto_unregister_ahash(&sha_224_alg); |
| 2116 | |
| 2117 | if (dd->caps.has_sha_384_512) { |
| 2118 | for (i = 0; i < ARRAY_SIZE(sha_384_512_algs); i++) |
| 2119 | crypto_unregister_ahash(&sha_384_512_algs[i]); |
| 2120 | } |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 2121 | } |
| 2122 | |
| 2123 | static int atmel_sha_register_algs(struct atmel_sha_dev *dd) |
| 2124 | { |
| 2125 | int err, i, j; |
| 2126 | |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 2127 | for (i = 0; i < ARRAY_SIZE(sha_1_256_algs); i++) { |
| 2128 | err = crypto_register_ahash(&sha_1_256_algs[i]); |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 2129 | if (err) |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 2130 | goto err_sha_1_256_algs; |
| 2131 | } |
| 2132 | |
| 2133 | if (dd->caps.has_sha224) { |
| 2134 | err = crypto_register_ahash(&sha_224_alg); |
| 2135 | if (err) |
| 2136 | goto err_sha_224_algs; |
| 2137 | } |
| 2138 | |
| 2139 | if (dd->caps.has_sha_384_512) { |
| 2140 | for (i = 0; i < ARRAY_SIZE(sha_384_512_algs); i++) { |
| 2141 | err = crypto_register_ahash(&sha_384_512_algs[i]); |
| 2142 | if (err) |
| 2143 | goto err_sha_384_512_algs; |
| 2144 | } |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 2145 | } |
| 2146 | |
Cyrille Pitchen | 81d8750 | 2017-01-26 17:07:54 +0100 | [diff] [blame^] | 2147 | if (dd->caps.has_hmac) { |
| 2148 | for (i = 0; i < ARRAY_SIZE(sha_hmac_algs); i++) { |
| 2149 | err = crypto_register_ahash(&sha_hmac_algs[i]); |
| 2150 | if (err) |
| 2151 | goto err_sha_hmac_algs; |
| 2152 | } |
| 2153 | } |
| 2154 | |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 2155 | return 0; |
| 2156 | |
Cyrille Pitchen | 81d8750 | 2017-01-26 17:07:54 +0100 | [diff] [blame^] | 2157 | /*i = ARRAY_SIZE(sha_hmac_algs);*/ |
| 2158 | err_sha_hmac_algs: |
| 2159 | for (j = 0; j < i; j++) |
| 2160 | crypto_unregister_ahash(&sha_hmac_algs[j]); |
| 2161 | i = ARRAY_SIZE(sha_384_512_algs); |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 2162 | err_sha_384_512_algs: |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 2163 | for (j = 0; j < i; j++) |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 2164 | crypto_unregister_ahash(&sha_384_512_algs[j]); |
| 2165 | crypto_unregister_ahash(&sha_224_alg); |
| 2166 | err_sha_224_algs: |
| 2167 | i = ARRAY_SIZE(sha_1_256_algs); |
| 2168 | err_sha_1_256_algs: |
| 2169 | for (j = 0; j < i; j++) |
| 2170 | crypto_unregister_ahash(&sha_1_256_algs[j]); |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 2171 | |
| 2172 | return err; |
| 2173 | } |
| 2174 | |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 2175 | static bool atmel_sha_filter(struct dma_chan *chan, void *slave) |
| 2176 | { |
| 2177 | struct at_dma_slave *sl = slave; |
| 2178 | |
| 2179 | if (sl && sl->dma_dev == chan->device->dev) { |
| 2180 | chan->private = sl; |
| 2181 | return true; |
| 2182 | } else { |
| 2183 | return false; |
| 2184 | } |
| 2185 | } |
| 2186 | |
| 2187 | static int atmel_sha_dma_init(struct atmel_sha_dev *dd, |
| 2188 | struct crypto_platform_data *pdata) |
| 2189 | { |
| 2190 | int err = -ENOMEM; |
| 2191 | dma_cap_mask_t mask_in; |
| 2192 | |
Nicolas Ferre | abfe7ae | 2013-10-15 15:36:34 +0200 | [diff] [blame] | 2193 | /* Try to grab DMA channel */ |
| 2194 | dma_cap_zero(mask_in); |
| 2195 | dma_cap_set(DMA_SLAVE, mask_in); |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 2196 | |
Nicolas Ferre | abfe7ae | 2013-10-15 15:36:34 +0200 | [diff] [blame] | 2197 | dd->dma_lch_in.chan = dma_request_slave_channel_compat(mask_in, |
| 2198 | atmel_sha_filter, &pdata->dma_slave->rxdata, dd->dev, "tx"); |
| 2199 | if (!dd->dma_lch_in.chan) { |
| 2200 | dev_warn(dd->dev, "no DMA channel available\n"); |
| 2201 | return err; |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 2202 | } |
| 2203 | |
Nicolas Ferre | abfe7ae | 2013-10-15 15:36:34 +0200 | [diff] [blame] | 2204 | dd->dma_lch_in.dma_conf.direction = DMA_MEM_TO_DEV; |
| 2205 | dd->dma_lch_in.dma_conf.dst_addr = dd->phys_base + |
| 2206 | SHA_REG_DIN(0); |
| 2207 | dd->dma_lch_in.dma_conf.src_maxburst = 1; |
| 2208 | dd->dma_lch_in.dma_conf.src_addr_width = |
| 2209 | DMA_SLAVE_BUSWIDTH_4_BYTES; |
| 2210 | dd->dma_lch_in.dma_conf.dst_maxburst = 1; |
| 2211 | dd->dma_lch_in.dma_conf.dst_addr_width = |
| 2212 | DMA_SLAVE_BUSWIDTH_4_BYTES; |
| 2213 | dd->dma_lch_in.dma_conf.device_fc = false; |
| 2214 | |
| 2215 | return 0; |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 2216 | } |
| 2217 | |
| 2218 | static void atmel_sha_dma_cleanup(struct atmel_sha_dev *dd) |
| 2219 | { |
| 2220 | dma_release_channel(dd->dma_lch_in.chan); |
| 2221 | } |
| 2222 | |
| 2223 | static void atmel_sha_get_cap(struct atmel_sha_dev *dd) |
| 2224 | { |
| 2225 | |
| 2226 | dd->caps.has_dma = 0; |
| 2227 | dd->caps.has_dualbuff = 0; |
| 2228 | dd->caps.has_sha224 = 0; |
| 2229 | dd->caps.has_sha_384_512 = 0; |
Cyrille Pitchen | 7cee350 | 2016-01-15 15:49:34 +0100 | [diff] [blame] | 2230 | dd->caps.has_uihv = 0; |
Cyrille Pitchen | 81d8750 | 2017-01-26 17:07:54 +0100 | [diff] [blame^] | 2231 | dd->caps.has_hmac = 0; |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 2232 | |
| 2233 | /* keep only major version number */ |
| 2234 | switch (dd->hw_version & 0xff0) { |
Cyrille Pitchen | 507c5cc | 2016-01-15 15:49:33 +0100 | [diff] [blame] | 2235 | case 0x510: |
| 2236 | dd->caps.has_dma = 1; |
| 2237 | dd->caps.has_dualbuff = 1; |
| 2238 | dd->caps.has_sha224 = 1; |
| 2239 | dd->caps.has_sha_384_512 = 1; |
Cyrille Pitchen | 7cee350 | 2016-01-15 15:49:34 +0100 | [diff] [blame] | 2240 | dd->caps.has_uihv = 1; |
Cyrille Pitchen | 81d8750 | 2017-01-26 17:07:54 +0100 | [diff] [blame^] | 2241 | dd->caps.has_hmac = 1; |
Cyrille Pitchen | 507c5cc | 2016-01-15 15:49:33 +0100 | [diff] [blame] | 2242 | break; |
Leilei Zhao | 141824d | 2015-04-07 17:45:03 +0800 | [diff] [blame] | 2243 | case 0x420: |
| 2244 | dd->caps.has_dma = 1; |
| 2245 | dd->caps.has_dualbuff = 1; |
| 2246 | dd->caps.has_sha224 = 1; |
| 2247 | dd->caps.has_sha_384_512 = 1; |
Cyrille Pitchen | 7cee350 | 2016-01-15 15:49:34 +0100 | [diff] [blame] | 2248 | dd->caps.has_uihv = 1; |
Leilei Zhao | 141824d | 2015-04-07 17:45:03 +0800 | [diff] [blame] | 2249 | break; |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 2250 | case 0x410: |
| 2251 | dd->caps.has_dma = 1; |
| 2252 | dd->caps.has_dualbuff = 1; |
| 2253 | dd->caps.has_sha224 = 1; |
| 2254 | dd->caps.has_sha_384_512 = 1; |
| 2255 | break; |
| 2256 | case 0x400: |
| 2257 | dd->caps.has_dma = 1; |
| 2258 | dd->caps.has_dualbuff = 1; |
| 2259 | dd->caps.has_sha224 = 1; |
| 2260 | break; |
| 2261 | case 0x320: |
| 2262 | break; |
| 2263 | default: |
| 2264 | dev_warn(dd->dev, |
| 2265 | "Unmanaged sha version, set minimum capabilities\n"); |
| 2266 | break; |
| 2267 | } |
| 2268 | } |
| 2269 | |
Nicolas Ferre | abfe7ae | 2013-10-15 15:36:34 +0200 | [diff] [blame] | 2270 | #if defined(CONFIG_OF) |
| 2271 | static const struct of_device_id atmel_sha_dt_ids[] = { |
| 2272 | { .compatible = "atmel,at91sam9g46-sha" }, |
| 2273 | { /* sentinel */ } |
| 2274 | }; |
| 2275 | |
| 2276 | MODULE_DEVICE_TABLE(of, atmel_sha_dt_ids); |
| 2277 | |
| 2278 | static struct crypto_platform_data *atmel_sha_of_init(struct platform_device *pdev) |
| 2279 | { |
| 2280 | struct device_node *np = pdev->dev.of_node; |
| 2281 | struct crypto_platform_data *pdata; |
| 2282 | |
| 2283 | if (!np) { |
| 2284 | dev_err(&pdev->dev, "device node not found\n"); |
| 2285 | return ERR_PTR(-EINVAL); |
| 2286 | } |
| 2287 | |
| 2288 | pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL); |
| 2289 | if (!pdata) { |
| 2290 | dev_err(&pdev->dev, "could not allocate memory for pdata\n"); |
| 2291 | return ERR_PTR(-ENOMEM); |
| 2292 | } |
| 2293 | |
| 2294 | pdata->dma_slave = devm_kzalloc(&pdev->dev, |
| 2295 | sizeof(*(pdata->dma_slave)), |
| 2296 | GFP_KERNEL); |
| 2297 | if (!pdata->dma_slave) { |
| 2298 | dev_err(&pdev->dev, "could not allocate memory for dma_slave\n"); |
Nicolas Ferre | abfe7ae | 2013-10-15 15:36:34 +0200 | [diff] [blame] | 2299 | return ERR_PTR(-ENOMEM); |
| 2300 | } |
| 2301 | |
| 2302 | return pdata; |
| 2303 | } |
| 2304 | #else /* CONFIG_OF */ |
| 2305 | static inline struct crypto_platform_data *atmel_sha_of_init(struct platform_device *dev) |
| 2306 | { |
| 2307 | return ERR_PTR(-EINVAL); |
| 2308 | } |
| 2309 | #endif |
| 2310 | |
Greg Kroah-Hartman | 49cfe4d | 2012-12-21 13:14:09 -0800 | [diff] [blame] | 2311 | static int atmel_sha_probe(struct platform_device *pdev) |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 2312 | { |
| 2313 | struct atmel_sha_dev *sha_dd; |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 2314 | struct crypto_platform_data *pdata; |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 2315 | struct device *dev = &pdev->dev; |
| 2316 | struct resource *sha_res; |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 2317 | int err; |
| 2318 | |
LABBE Corentin | b0e8b34 | 2015-10-12 19:47:03 +0200 | [diff] [blame] | 2319 | sha_dd = devm_kzalloc(&pdev->dev, sizeof(*sha_dd), GFP_KERNEL); |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 2320 | if (sha_dd == NULL) { |
| 2321 | dev_err(dev, "unable to alloc data struct.\n"); |
| 2322 | err = -ENOMEM; |
| 2323 | goto sha_dd_err; |
| 2324 | } |
| 2325 | |
| 2326 | sha_dd->dev = dev; |
| 2327 | |
| 2328 | platform_set_drvdata(pdev, sha_dd); |
| 2329 | |
| 2330 | INIT_LIST_HEAD(&sha_dd->list); |
Leilei Zhao | 62728e8 | 2015-04-07 17:45:06 +0800 | [diff] [blame] | 2331 | spin_lock_init(&sha_dd->lock); |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 2332 | |
| 2333 | tasklet_init(&sha_dd->done_task, atmel_sha_done_task, |
| 2334 | (unsigned long)sha_dd); |
Cyrille Pitchen | f56809c | 2016-01-15 15:49:32 +0100 | [diff] [blame] | 2335 | tasklet_init(&sha_dd->queue_task, atmel_sha_queue_task, |
| 2336 | (unsigned long)sha_dd); |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 2337 | |
| 2338 | crypto_init_queue(&sha_dd->queue, ATMEL_SHA_QUEUE_LENGTH); |
| 2339 | |
| 2340 | sha_dd->irq = -1; |
| 2341 | |
| 2342 | /* Get the base address */ |
| 2343 | sha_res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 2344 | if (!sha_res) { |
| 2345 | dev_err(dev, "no MEM resource info\n"); |
| 2346 | err = -ENODEV; |
| 2347 | goto res_err; |
| 2348 | } |
| 2349 | sha_dd->phys_base = sha_res->start; |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 2350 | |
| 2351 | /* Get the IRQ */ |
| 2352 | sha_dd->irq = platform_get_irq(pdev, 0); |
| 2353 | if (sha_dd->irq < 0) { |
| 2354 | dev_err(dev, "no IRQ resource info\n"); |
| 2355 | err = sha_dd->irq; |
| 2356 | goto res_err; |
| 2357 | } |
| 2358 | |
LABBE Corentin | b0e8b34 | 2015-10-12 19:47:03 +0200 | [diff] [blame] | 2359 | err = devm_request_irq(&pdev->dev, sha_dd->irq, atmel_sha_irq, |
| 2360 | IRQF_SHARED, "atmel-sha", sha_dd); |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 2361 | if (err) { |
| 2362 | dev_err(dev, "unable to request sha irq.\n"); |
| 2363 | goto res_err; |
| 2364 | } |
| 2365 | |
| 2366 | /* Initializing the clock */ |
LABBE Corentin | b0e8b34 | 2015-10-12 19:47:03 +0200 | [diff] [blame] | 2367 | sha_dd->iclk = devm_clk_get(&pdev->dev, "sha_clk"); |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 2368 | if (IS_ERR(sha_dd->iclk)) { |
Colin Ian King | be20835 | 2015-02-28 20:40:10 +0000 | [diff] [blame] | 2369 | dev_err(dev, "clock initialization failed.\n"); |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 2370 | err = PTR_ERR(sha_dd->iclk); |
LABBE Corentin | b0e8b34 | 2015-10-12 19:47:03 +0200 | [diff] [blame] | 2371 | goto res_err; |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 2372 | } |
| 2373 | |
LABBE Corentin | b0e8b34 | 2015-10-12 19:47:03 +0200 | [diff] [blame] | 2374 | sha_dd->io_base = devm_ioremap_resource(&pdev->dev, sha_res); |
Vladimir Zapolskiy | 9b52d55 | 2016-03-06 03:21:52 +0200 | [diff] [blame] | 2375 | if (IS_ERR(sha_dd->io_base)) { |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 2376 | dev_err(dev, "can't ioremap\n"); |
Vladimir Zapolskiy | 9b52d55 | 2016-03-06 03:21:52 +0200 | [diff] [blame] | 2377 | err = PTR_ERR(sha_dd->io_base); |
LABBE Corentin | b0e8b34 | 2015-10-12 19:47:03 +0200 | [diff] [blame] | 2378 | goto res_err; |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 2379 | } |
| 2380 | |
Cyrille Pitchen | c033042 | 2016-02-05 13:45:13 +0100 | [diff] [blame] | 2381 | err = clk_prepare(sha_dd->iclk); |
| 2382 | if (err) |
| 2383 | goto res_err; |
| 2384 | |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 2385 | atmel_sha_hw_version_init(sha_dd); |
| 2386 | |
| 2387 | atmel_sha_get_cap(sha_dd); |
| 2388 | |
| 2389 | if (sha_dd->caps.has_dma) { |
| 2390 | pdata = pdev->dev.platform_data; |
| 2391 | if (!pdata) { |
Nicolas Ferre | abfe7ae | 2013-10-15 15:36:34 +0200 | [diff] [blame] | 2392 | pdata = atmel_sha_of_init(pdev); |
| 2393 | if (IS_ERR(pdata)) { |
| 2394 | dev_err(&pdev->dev, "platform data not available\n"); |
| 2395 | err = PTR_ERR(pdata); |
Cyrille Pitchen | c033042 | 2016-02-05 13:45:13 +0100 | [diff] [blame] | 2396 | goto iclk_unprepare; |
Nicolas Ferre | abfe7ae | 2013-10-15 15:36:34 +0200 | [diff] [blame] | 2397 | } |
| 2398 | } |
| 2399 | if (!pdata->dma_slave) { |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 2400 | err = -ENXIO; |
Cyrille Pitchen | c033042 | 2016-02-05 13:45:13 +0100 | [diff] [blame] | 2401 | goto iclk_unprepare; |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 2402 | } |
| 2403 | err = atmel_sha_dma_init(sha_dd, pdata); |
| 2404 | if (err) |
| 2405 | goto err_sha_dma; |
Nicolas Ferre | abfe7ae | 2013-10-15 15:36:34 +0200 | [diff] [blame] | 2406 | |
| 2407 | dev_info(dev, "using %s for DMA transfers\n", |
| 2408 | dma_chan_name(sha_dd->dma_lch_in.chan)); |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 2409 | } |
| 2410 | |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 2411 | spin_lock(&atmel_sha.lock); |
| 2412 | list_add_tail(&sha_dd->list, &atmel_sha.dev_list); |
| 2413 | spin_unlock(&atmel_sha.lock); |
| 2414 | |
| 2415 | err = atmel_sha_register_algs(sha_dd); |
| 2416 | if (err) |
| 2417 | goto err_algs; |
| 2418 | |
Nicolas Ferre | 1ca5b7d | 2013-10-15 16:37:44 +0200 | [diff] [blame] | 2419 | dev_info(dev, "Atmel SHA1/SHA256%s%s\n", |
| 2420 | sha_dd->caps.has_sha224 ? "/SHA224" : "", |
| 2421 | sha_dd->caps.has_sha_384_512 ? "/SHA384/SHA512" : ""); |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 2422 | |
| 2423 | return 0; |
| 2424 | |
| 2425 | err_algs: |
| 2426 | spin_lock(&atmel_sha.lock); |
| 2427 | list_del(&sha_dd->list); |
| 2428 | spin_unlock(&atmel_sha.lock); |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 2429 | if (sha_dd->caps.has_dma) |
| 2430 | atmel_sha_dma_cleanup(sha_dd); |
| 2431 | err_sha_dma: |
Cyrille Pitchen | c033042 | 2016-02-05 13:45:13 +0100 | [diff] [blame] | 2432 | iclk_unprepare: |
| 2433 | clk_unprepare(sha_dd->iclk); |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 2434 | res_err: |
Cyrille Pitchen | f56809c | 2016-01-15 15:49:32 +0100 | [diff] [blame] | 2435 | tasklet_kill(&sha_dd->queue_task); |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 2436 | tasklet_kill(&sha_dd->done_task); |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 2437 | sha_dd_err: |
| 2438 | dev_err(dev, "initialization failed.\n"); |
| 2439 | |
| 2440 | return err; |
| 2441 | } |
| 2442 | |
Greg Kroah-Hartman | 49cfe4d | 2012-12-21 13:14:09 -0800 | [diff] [blame] | 2443 | static int atmel_sha_remove(struct platform_device *pdev) |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 2444 | { |
| 2445 | static struct atmel_sha_dev *sha_dd; |
| 2446 | |
| 2447 | sha_dd = platform_get_drvdata(pdev); |
| 2448 | if (!sha_dd) |
| 2449 | return -ENODEV; |
| 2450 | spin_lock(&atmel_sha.lock); |
| 2451 | list_del(&sha_dd->list); |
| 2452 | spin_unlock(&atmel_sha.lock); |
| 2453 | |
| 2454 | atmel_sha_unregister_algs(sha_dd); |
| 2455 | |
Cyrille Pitchen | f56809c | 2016-01-15 15:49:32 +0100 | [diff] [blame] | 2456 | tasklet_kill(&sha_dd->queue_task); |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 2457 | tasklet_kill(&sha_dd->done_task); |
| 2458 | |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 2459 | if (sha_dd->caps.has_dma) |
| 2460 | atmel_sha_dma_cleanup(sha_dd); |
| 2461 | |
Cyrille Pitchen | c033042 | 2016-02-05 13:45:13 +0100 | [diff] [blame] | 2462 | clk_unprepare(sha_dd->iclk); |
| 2463 | |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 2464 | return 0; |
| 2465 | } |
| 2466 | |
| 2467 | static struct platform_driver atmel_sha_driver = { |
| 2468 | .probe = atmel_sha_probe, |
Greg Kroah-Hartman | 49cfe4d | 2012-12-21 13:14:09 -0800 | [diff] [blame] | 2469 | .remove = atmel_sha_remove, |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 2470 | .driver = { |
| 2471 | .name = "atmel_sha", |
Nicolas Ferre | abfe7ae | 2013-10-15 15:36:34 +0200 | [diff] [blame] | 2472 | .of_match_table = of_match_ptr(atmel_sha_dt_ids), |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 2473 | }, |
| 2474 | }; |
| 2475 | |
| 2476 | module_platform_driver(atmel_sha_driver); |
| 2477 | |
Nicolas Royer | d4905b3 | 2013-02-20 17:10:26 +0100 | [diff] [blame] | 2478 | MODULE_DESCRIPTION("Atmel SHA (1/256/224/384/512) hw acceleration support."); |
Nicolas Royer | ebc82ef | 2012-07-01 19:19:46 +0200 | [diff] [blame] | 2479 | MODULE_LICENSE("GPL v2"); |
| 2480 | MODULE_AUTHOR("Nicolas Royer - Eukréa Electromatique"); |