Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Cryptographic API. |
| 3 | * |
| 4 | * Support for OMAP SHA1/MD5 HW acceleration. |
| 5 | * |
| 6 | * Copyright (c) 2010 Nokia Corporation |
| 7 | * Author: Dmitry Kasatkin <dmitry.kasatkin@nokia.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 old omap-sha1-md5.c driver. |
| 14 | */ |
| 15 | |
| 16 | #define pr_fmt(fmt) "%s: " fmt, __func__ |
| 17 | |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 18 | #include <linux/err.h> |
| 19 | #include <linux/device.h> |
| 20 | #include <linux/module.h> |
| 21 | #include <linux/init.h> |
| 22 | #include <linux/errno.h> |
| 23 | #include <linux/interrupt.h> |
| 24 | #include <linux/kernel.h> |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 25 | #include <linux/irq.h> |
| 26 | #include <linux/io.h> |
| 27 | #include <linux/platform_device.h> |
| 28 | #include <linux/scatterlist.h> |
| 29 | #include <linux/dma-mapping.h> |
Mark A. Greer | b359f03 | 2012-12-21 10:04:02 -0700 | [diff] [blame] | 30 | #include <linux/pm_runtime.h> |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 31 | #include <linux/delay.h> |
| 32 | #include <linux/crypto.h> |
| 33 | #include <linux/cryptohash.h> |
| 34 | #include <crypto/scatterwalk.h> |
| 35 | #include <crypto/algapi.h> |
| 36 | #include <crypto/sha.h> |
| 37 | #include <crypto/hash.h> |
| 38 | #include <crypto/internal/hash.h> |
| 39 | |
Tony Lindgren | 45c3eb7 | 2012-11-30 08:41:50 -0800 | [diff] [blame] | 40 | #include <linux/omap-dma.h> |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 41 | #include <mach/irqs.h> |
| 42 | |
| 43 | #define SHA_REG_DIGEST(x) (0x00 + ((x) * 0x04)) |
| 44 | #define SHA_REG_DIN(x) (0x1C + ((x) * 0x04)) |
| 45 | |
| 46 | #define SHA1_MD5_BLOCK_SIZE SHA1_BLOCK_SIZE |
| 47 | #define MD5_DIGEST_SIZE 16 |
| 48 | |
| 49 | #define SHA_REG_DIGCNT 0x14 |
| 50 | |
| 51 | #define SHA_REG_CTRL 0x18 |
| 52 | #define SHA_REG_CTRL_LENGTH (0xFFFFFFFF << 5) |
| 53 | #define SHA_REG_CTRL_CLOSE_HASH (1 << 4) |
| 54 | #define SHA_REG_CTRL_ALGO_CONST (1 << 3) |
| 55 | #define SHA_REG_CTRL_ALGO (1 << 2) |
| 56 | #define SHA_REG_CTRL_INPUT_READY (1 << 1) |
| 57 | #define SHA_REG_CTRL_OUTPUT_READY (1 << 0) |
| 58 | |
| 59 | #define SHA_REG_REV 0x5C |
| 60 | #define SHA_REG_REV_MAJOR 0xF0 |
| 61 | #define SHA_REG_REV_MINOR 0x0F |
| 62 | |
| 63 | #define SHA_REG_MASK 0x60 |
| 64 | #define SHA_REG_MASK_DMA_EN (1 << 3) |
| 65 | #define SHA_REG_MASK_IT_EN (1 << 2) |
| 66 | #define SHA_REG_MASK_SOFTRESET (1 << 1) |
| 67 | #define SHA_REG_AUTOIDLE (1 << 0) |
| 68 | |
| 69 | #define SHA_REG_SYSSTATUS 0x64 |
| 70 | #define SHA_REG_SYSSTATUS_RESETDONE (1 << 0) |
| 71 | |
| 72 | #define DEFAULT_TIMEOUT_INTERVAL HZ |
| 73 | |
Dmitry Kasatkin | ea1fd22 | 2011-06-02 21:10:05 +0300 | [diff] [blame] | 74 | /* mostly device flags */ |
| 75 | #define FLAGS_BUSY 0 |
| 76 | #define FLAGS_FINAL 1 |
| 77 | #define FLAGS_DMA_ACTIVE 2 |
| 78 | #define FLAGS_OUTPUT_READY 3 |
| 79 | #define FLAGS_INIT 4 |
| 80 | #define FLAGS_CPU 5 |
Dmitry Kasatkin | 6c63db8 | 2011-06-02 21:10:10 +0300 | [diff] [blame] | 81 | #define FLAGS_DMA_READY 6 |
Dmitry Kasatkin | ea1fd22 | 2011-06-02 21:10:05 +0300 | [diff] [blame] | 82 | /* context flags */ |
| 83 | #define FLAGS_FINUP 16 |
| 84 | #define FLAGS_SG 17 |
| 85 | #define FLAGS_SHA1 18 |
| 86 | #define FLAGS_HMAC 19 |
| 87 | #define FLAGS_ERROR 20 |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 88 | |
| 89 | #define OP_UPDATE 1 |
| 90 | #define OP_FINAL 2 |
| 91 | |
Dmitry Kasatkin | 798eed5d | 2010-11-19 16:04:26 +0200 | [diff] [blame] | 92 | #define OMAP_ALIGN_MASK (sizeof(u32)-1) |
| 93 | #define OMAP_ALIGNED __attribute__((aligned(sizeof(u32)))) |
| 94 | |
| 95 | #define BUFLEN PAGE_SIZE |
| 96 | |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 97 | struct omap_sham_dev; |
| 98 | |
| 99 | struct omap_sham_reqctx { |
| 100 | struct omap_sham_dev *dd; |
| 101 | unsigned long flags; |
| 102 | unsigned long op; |
| 103 | |
Dmitry Kasatkin | 798eed5d | 2010-11-19 16:04:26 +0200 | [diff] [blame] | 104 | u8 digest[SHA1_DIGEST_SIZE] OMAP_ALIGNED; |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 105 | size_t digcnt; |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 106 | size_t bufcnt; |
| 107 | size_t buflen; |
| 108 | dma_addr_t dma_addr; |
| 109 | |
| 110 | /* walk state */ |
| 111 | struct scatterlist *sg; |
| 112 | unsigned int offset; /* offset in current sg */ |
| 113 | unsigned int total; /* total request */ |
Dmitry Kasatkin | 798eed5d | 2010-11-19 16:04:26 +0200 | [diff] [blame] | 114 | |
| 115 | u8 buffer[0] OMAP_ALIGNED; |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 116 | }; |
| 117 | |
| 118 | struct omap_sham_hmac_ctx { |
| 119 | struct crypto_shash *shash; |
| 120 | u8 ipad[SHA1_MD5_BLOCK_SIZE]; |
| 121 | u8 opad[SHA1_MD5_BLOCK_SIZE]; |
| 122 | }; |
| 123 | |
| 124 | struct omap_sham_ctx { |
| 125 | struct omap_sham_dev *dd; |
| 126 | |
| 127 | unsigned long flags; |
| 128 | |
| 129 | /* fallback stuff */ |
| 130 | struct crypto_shash *fallback; |
| 131 | |
| 132 | struct omap_sham_hmac_ctx base[0]; |
| 133 | }; |
| 134 | |
| 135 | #define OMAP_SHAM_QUEUE_LENGTH 1 |
| 136 | |
| 137 | struct omap_sham_dev { |
| 138 | struct list_head list; |
| 139 | unsigned long phys_base; |
| 140 | struct device *dev; |
| 141 | void __iomem *io_base; |
| 142 | int irq; |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 143 | spinlock_t lock; |
Dmitry Kasatkin | 3e133c8 | 2010-11-19 16:04:24 +0200 | [diff] [blame] | 144 | int err; |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 145 | int dma; |
| 146 | int dma_lch; |
| 147 | struct tasklet_struct done_task; |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 148 | |
| 149 | unsigned long flags; |
| 150 | struct crypto_queue queue; |
| 151 | struct ahash_request *req; |
| 152 | }; |
| 153 | |
| 154 | struct omap_sham_drv { |
| 155 | struct list_head dev_list; |
| 156 | spinlock_t lock; |
| 157 | unsigned long flags; |
| 158 | }; |
| 159 | |
| 160 | static struct omap_sham_drv sham = { |
| 161 | .dev_list = LIST_HEAD_INIT(sham.dev_list), |
| 162 | .lock = __SPIN_LOCK_UNLOCKED(sham.lock), |
| 163 | }; |
| 164 | |
| 165 | static inline u32 omap_sham_read(struct omap_sham_dev *dd, u32 offset) |
| 166 | { |
| 167 | return __raw_readl(dd->io_base + offset); |
| 168 | } |
| 169 | |
| 170 | static inline void omap_sham_write(struct omap_sham_dev *dd, |
| 171 | u32 offset, u32 value) |
| 172 | { |
| 173 | __raw_writel(value, dd->io_base + offset); |
| 174 | } |
| 175 | |
| 176 | static inline void omap_sham_write_mask(struct omap_sham_dev *dd, u32 address, |
| 177 | u32 value, u32 mask) |
| 178 | { |
| 179 | u32 val; |
| 180 | |
| 181 | val = omap_sham_read(dd, address); |
| 182 | val &= ~mask; |
| 183 | val |= value; |
| 184 | omap_sham_write(dd, address, val); |
| 185 | } |
| 186 | |
| 187 | static inline int omap_sham_wait(struct omap_sham_dev *dd, u32 offset, u32 bit) |
| 188 | { |
| 189 | unsigned long timeout = jiffies + DEFAULT_TIMEOUT_INTERVAL; |
| 190 | |
| 191 | while (!(omap_sham_read(dd, offset) & bit)) { |
| 192 | if (time_is_before_jiffies(timeout)) |
| 193 | return -ETIMEDOUT; |
| 194 | } |
| 195 | |
| 196 | return 0; |
| 197 | } |
| 198 | |
| 199 | static void omap_sham_copy_hash(struct ahash_request *req, int out) |
| 200 | { |
| 201 | struct omap_sham_reqctx *ctx = ahash_request_ctx(req); |
Dmitry Kasatkin | 0c3cf4c | 2010-11-19 16:04:22 +0200 | [diff] [blame] | 202 | u32 *hash = (u32 *)ctx->digest; |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 203 | int i; |
| 204 | |
Dmitry Kasatkin | 3c8d758 | 2010-11-19 16:04:27 +0200 | [diff] [blame] | 205 | /* MD5 is almost unused. So copy sha1 size to reduce code */ |
| 206 | for (i = 0; i < SHA1_DIGEST_SIZE / sizeof(u32); i++) { |
| 207 | if (out) |
| 208 | hash[i] = omap_sham_read(ctx->dd, |
| 209 | SHA_REG_DIGEST(i)); |
| 210 | else |
| 211 | omap_sham_write(ctx->dd, |
| 212 | SHA_REG_DIGEST(i), hash[i]); |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | static void omap_sham_copy_ready_hash(struct ahash_request *req) |
| 217 | { |
| 218 | struct omap_sham_reqctx *ctx = ahash_request_ctx(req); |
| 219 | u32 *in = (u32 *)ctx->digest; |
| 220 | u32 *hash = (u32 *)req->result; |
| 221 | int i; |
| 222 | |
| 223 | if (!hash) |
| 224 | return; |
| 225 | |
Dmitry Kasatkin | ea1fd22 | 2011-06-02 21:10:05 +0300 | [diff] [blame] | 226 | if (likely(ctx->flags & BIT(FLAGS_SHA1))) { |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 227 | /* SHA1 results are in big endian */ |
| 228 | for (i = 0; i < SHA1_DIGEST_SIZE / sizeof(u32); i++) |
Dmitry Kasatkin | 3c8d758 | 2010-11-19 16:04:27 +0200 | [diff] [blame] | 229 | hash[i] = be32_to_cpu(in[i]); |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 230 | } else { |
| 231 | /* MD5 results are in little endian */ |
| 232 | for (i = 0; i < MD5_DIGEST_SIZE / sizeof(u32); i++) |
Dmitry Kasatkin | 3c8d758 | 2010-11-19 16:04:27 +0200 | [diff] [blame] | 233 | hash[i] = le32_to_cpu(in[i]); |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 234 | } |
| 235 | } |
| 236 | |
Dmitry Kasatkin | 798eed5d | 2010-11-19 16:04:26 +0200 | [diff] [blame] | 237 | static int omap_sham_hw_init(struct omap_sham_dev *dd) |
| 238 | { |
Mark A. Greer | b359f03 | 2012-12-21 10:04:02 -0700 | [diff] [blame] | 239 | pm_runtime_get_sync(dd->dev); |
Dmitry Kasatkin | 798eed5d | 2010-11-19 16:04:26 +0200 | [diff] [blame] | 240 | |
Dmitry Kasatkin | a929cbe | 2011-06-02 21:10:06 +0300 | [diff] [blame] | 241 | if (!test_bit(FLAGS_INIT, &dd->flags)) { |
Dmitry Kasatkin | 798eed5d | 2010-11-19 16:04:26 +0200 | [diff] [blame] | 242 | omap_sham_write_mask(dd, SHA_REG_MASK, |
| 243 | SHA_REG_MASK_SOFTRESET, SHA_REG_MASK_SOFTRESET); |
| 244 | |
| 245 | if (omap_sham_wait(dd, SHA_REG_SYSSTATUS, |
| 246 | SHA_REG_SYSSTATUS_RESETDONE)) |
| 247 | return -ETIMEDOUT; |
| 248 | |
Dmitry Kasatkin | a929cbe | 2011-06-02 21:10:06 +0300 | [diff] [blame] | 249 | set_bit(FLAGS_INIT, &dd->flags); |
Dmitry Kasatkin | 798eed5d | 2010-11-19 16:04:26 +0200 | [diff] [blame] | 250 | dd->err = 0; |
| 251 | } |
| 252 | |
| 253 | return 0; |
| 254 | } |
| 255 | |
| 256 | static void omap_sham_write_ctrl(struct omap_sham_dev *dd, size_t length, |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 257 | int final, int dma) |
| 258 | { |
| 259 | struct omap_sham_reqctx *ctx = ahash_request_ctx(dd->req); |
| 260 | u32 val = length << 5, mask; |
| 261 | |
Dmitry Kasatkin | 798eed5d | 2010-11-19 16:04:26 +0200 | [diff] [blame] | 262 | if (likely(ctx->digcnt)) |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 263 | omap_sham_write(dd, SHA_REG_DIGCNT, ctx->digcnt); |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 264 | |
| 265 | omap_sham_write_mask(dd, SHA_REG_MASK, |
| 266 | SHA_REG_MASK_IT_EN | (dma ? SHA_REG_MASK_DMA_EN : 0), |
| 267 | SHA_REG_MASK_IT_EN | SHA_REG_MASK_DMA_EN); |
| 268 | /* |
| 269 | * Setting ALGO_CONST only for the first iteration |
| 270 | * and CLOSE_HASH only for the last one. |
| 271 | */ |
Dmitry Kasatkin | ea1fd22 | 2011-06-02 21:10:05 +0300 | [diff] [blame] | 272 | if (ctx->flags & BIT(FLAGS_SHA1)) |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 273 | val |= SHA_REG_CTRL_ALGO; |
| 274 | if (!ctx->digcnt) |
| 275 | val |= SHA_REG_CTRL_ALGO_CONST; |
| 276 | if (final) |
| 277 | val |= SHA_REG_CTRL_CLOSE_HASH; |
| 278 | |
| 279 | mask = SHA_REG_CTRL_ALGO_CONST | SHA_REG_CTRL_CLOSE_HASH | |
| 280 | SHA_REG_CTRL_ALGO | SHA_REG_CTRL_LENGTH; |
| 281 | |
| 282 | omap_sham_write_mask(dd, SHA_REG_CTRL, val, mask); |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | static int omap_sham_xmit_cpu(struct omap_sham_dev *dd, const u8 *buf, |
| 286 | size_t length, int final) |
| 287 | { |
| 288 | struct omap_sham_reqctx *ctx = ahash_request_ctx(dd->req); |
Dmitry Kasatkin | 798eed5d | 2010-11-19 16:04:26 +0200 | [diff] [blame] | 289 | int count, len32; |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 290 | const u32 *buffer = (const u32 *)buf; |
| 291 | |
| 292 | dev_dbg(dd->dev, "xmit_cpu: digcnt: %d, length: %d, final: %d\n", |
| 293 | ctx->digcnt, length, final); |
| 294 | |
Dmitry Kasatkin | 798eed5d | 2010-11-19 16:04:26 +0200 | [diff] [blame] | 295 | omap_sham_write_ctrl(dd, length, final, 0); |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 296 | |
Dmitry Kasatkin | 3e133c8 | 2010-11-19 16:04:24 +0200 | [diff] [blame] | 297 | /* should be non-zero before next lines to disable clocks later */ |
| 298 | ctx->digcnt += length; |
| 299 | |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 300 | if (omap_sham_wait(dd, SHA_REG_CTRL, SHA_REG_CTRL_INPUT_READY)) |
| 301 | return -ETIMEDOUT; |
| 302 | |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 303 | if (final) |
Dmitry Kasatkin | ed3ea9a8 | 2011-06-02 21:10:07 +0300 | [diff] [blame] | 304 | set_bit(FLAGS_FINAL, &dd->flags); /* catch last interrupt */ |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 305 | |
Dmitry Kasatkin | 6c63db8 | 2011-06-02 21:10:10 +0300 | [diff] [blame] | 306 | set_bit(FLAGS_CPU, &dd->flags); |
| 307 | |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 308 | len32 = DIV_ROUND_UP(length, sizeof(u32)); |
| 309 | |
| 310 | for (count = 0; count < len32; count++) |
| 311 | omap_sham_write(dd, SHA_REG_DIN(count), buffer[count]); |
| 312 | |
| 313 | return -EINPROGRESS; |
| 314 | } |
| 315 | |
| 316 | static int omap_sham_xmit_dma(struct omap_sham_dev *dd, dma_addr_t dma_addr, |
| 317 | size_t length, int final) |
| 318 | { |
| 319 | struct omap_sham_reqctx *ctx = ahash_request_ctx(dd->req); |
Dmitry Kasatkin | 798eed5d | 2010-11-19 16:04:26 +0200 | [diff] [blame] | 320 | int len32; |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 321 | |
| 322 | dev_dbg(dd->dev, "xmit_dma: digcnt: %d, length: %d, final: %d\n", |
| 323 | ctx->digcnt, length, final); |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 324 | |
| 325 | len32 = DIV_ROUND_UP(length, sizeof(u32)); |
| 326 | |
| 327 | omap_set_dma_transfer_params(dd->dma_lch, OMAP_DMA_DATA_TYPE_S32, len32, |
Samu Onkalo | 584db6a | 2010-09-03 19:20:19 +0800 | [diff] [blame] | 328 | 1, OMAP_DMA_SYNC_PACKET, dd->dma, |
| 329 | OMAP_DMA_DST_SYNC_PREFETCH); |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 330 | |
| 331 | omap_set_dma_src_params(dd->dma_lch, 0, OMAP_DMA_AMODE_POST_INC, |
| 332 | dma_addr, 0, 0); |
| 333 | |
Dmitry Kasatkin | 798eed5d | 2010-11-19 16:04:26 +0200 | [diff] [blame] | 334 | omap_sham_write_ctrl(dd, length, final, 1); |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 335 | |
| 336 | ctx->digcnt += length; |
| 337 | |
| 338 | if (final) |
Dmitry Kasatkin | ed3ea9a8 | 2011-06-02 21:10:07 +0300 | [diff] [blame] | 339 | set_bit(FLAGS_FINAL, &dd->flags); /* catch last interrupt */ |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 340 | |
Dmitry Kasatkin | a929cbe | 2011-06-02 21:10:06 +0300 | [diff] [blame] | 341 | set_bit(FLAGS_DMA_ACTIVE, &dd->flags); |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 342 | |
| 343 | omap_start_dma(dd->dma_lch); |
| 344 | |
| 345 | return -EINPROGRESS; |
| 346 | } |
| 347 | |
| 348 | static size_t omap_sham_append_buffer(struct omap_sham_reqctx *ctx, |
| 349 | const u8 *data, size_t length) |
| 350 | { |
| 351 | size_t count = min(length, ctx->buflen - ctx->bufcnt); |
| 352 | |
| 353 | count = min(count, ctx->total); |
| 354 | if (count <= 0) |
| 355 | return 0; |
| 356 | memcpy(ctx->buffer + ctx->bufcnt, data, count); |
| 357 | ctx->bufcnt += count; |
| 358 | |
| 359 | return count; |
| 360 | } |
| 361 | |
| 362 | static size_t omap_sham_append_sg(struct omap_sham_reqctx *ctx) |
| 363 | { |
| 364 | size_t count; |
| 365 | |
| 366 | while (ctx->sg) { |
| 367 | count = omap_sham_append_buffer(ctx, |
| 368 | sg_virt(ctx->sg) + ctx->offset, |
| 369 | ctx->sg->length - ctx->offset); |
| 370 | if (!count) |
| 371 | break; |
| 372 | ctx->offset += count; |
| 373 | ctx->total -= count; |
| 374 | if (ctx->offset == ctx->sg->length) { |
| 375 | ctx->sg = sg_next(ctx->sg); |
| 376 | if (ctx->sg) |
| 377 | ctx->offset = 0; |
| 378 | else |
| 379 | ctx->total = 0; |
| 380 | } |
| 381 | } |
| 382 | |
| 383 | return 0; |
| 384 | } |
| 385 | |
Dmitry Kasatkin | 798eed5d | 2010-11-19 16:04:26 +0200 | [diff] [blame] | 386 | static int omap_sham_xmit_dma_map(struct omap_sham_dev *dd, |
| 387 | struct omap_sham_reqctx *ctx, |
| 388 | size_t length, int final) |
| 389 | { |
| 390 | ctx->dma_addr = dma_map_single(dd->dev, ctx->buffer, ctx->buflen, |
| 391 | DMA_TO_DEVICE); |
| 392 | if (dma_mapping_error(dd->dev, ctx->dma_addr)) { |
| 393 | dev_err(dd->dev, "dma %u bytes error\n", ctx->buflen); |
| 394 | return -EINVAL; |
| 395 | } |
| 396 | |
Dmitry Kasatkin | ea1fd22 | 2011-06-02 21:10:05 +0300 | [diff] [blame] | 397 | ctx->flags &= ~BIT(FLAGS_SG); |
Dmitry Kasatkin | 887c883 | 2010-11-19 16:04:29 +0200 | [diff] [blame] | 398 | |
Dmitry Kasatkin | 798eed5d | 2010-11-19 16:04:26 +0200 | [diff] [blame] | 399 | /* next call does not fail... so no unmap in the case of error */ |
| 400 | return omap_sham_xmit_dma(dd, ctx->dma_addr, length, final); |
| 401 | } |
| 402 | |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 403 | static int omap_sham_update_dma_slow(struct omap_sham_dev *dd) |
| 404 | { |
| 405 | struct omap_sham_reqctx *ctx = ahash_request_ctx(dd->req); |
| 406 | unsigned int final; |
| 407 | size_t count; |
| 408 | |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 409 | omap_sham_append_sg(ctx); |
| 410 | |
Dmitry Kasatkin | ea1fd22 | 2011-06-02 21:10:05 +0300 | [diff] [blame] | 411 | final = (ctx->flags & BIT(FLAGS_FINUP)) && !ctx->total; |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 412 | |
| 413 | dev_dbg(dd->dev, "slow: bufcnt: %u, digcnt: %d, final: %d\n", |
| 414 | ctx->bufcnt, ctx->digcnt, final); |
| 415 | |
| 416 | if (final || (ctx->bufcnt == ctx->buflen && ctx->total)) { |
| 417 | count = ctx->bufcnt; |
| 418 | ctx->bufcnt = 0; |
Dmitry Kasatkin | 798eed5d | 2010-11-19 16:04:26 +0200 | [diff] [blame] | 419 | return omap_sham_xmit_dma_map(dd, ctx, count, final); |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 420 | } |
| 421 | |
| 422 | return 0; |
| 423 | } |
| 424 | |
Dmitry Kasatkin | 887c883 | 2010-11-19 16:04:29 +0200 | [diff] [blame] | 425 | /* Start address alignment */ |
| 426 | #define SG_AA(sg) (IS_ALIGNED(sg->offset, sizeof(u32))) |
| 427 | /* SHA1 block size alignment */ |
| 428 | #define SG_SA(sg) (IS_ALIGNED(sg->length, SHA1_MD5_BLOCK_SIZE)) |
| 429 | |
| 430 | static int omap_sham_update_dma_start(struct omap_sham_dev *dd) |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 431 | { |
| 432 | struct omap_sham_reqctx *ctx = ahash_request_ctx(dd->req); |
Dmitry Kasatkin | 887c883 | 2010-11-19 16:04:29 +0200 | [diff] [blame] | 433 | unsigned int length, final, tail; |
| 434 | struct scatterlist *sg; |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 435 | |
Dmitry Kasatkin | 887c883 | 2010-11-19 16:04:29 +0200 | [diff] [blame] | 436 | if (!ctx->total) |
| 437 | return 0; |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 438 | |
Dmitry Kasatkin | 887c883 | 2010-11-19 16:04:29 +0200 | [diff] [blame] | 439 | if (ctx->bufcnt || ctx->offset) |
| 440 | return omap_sham_update_dma_slow(dd); |
| 441 | |
| 442 | dev_dbg(dd->dev, "fast: digcnt: %d, bufcnt: %u, total: %u\n", |
| 443 | ctx->digcnt, ctx->bufcnt, ctx->total); |
| 444 | |
| 445 | sg = ctx->sg; |
| 446 | |
| 447 | if (!SG_AA(sg)) |
| 448 | return omap_sham_update_dma_slow(dd); |
| 449 | |
| 450 | if (!sg_is_last(sg) && !SG_SA(sg)) |
| 451 | /* size is not SHA1_BLOCK_SIZE aligned */ |
| 452 | return omap_sham_update_dma_slow(dd); |
| 453 | |
| 454 | length = min(ctx->total, sg->length); |
| 455 | |
| 456 | if (sg_is_last(sg)) { |
Dmitry Kasatkin | ea1fd22 | 2011-06-02 21:10:05 +0300 | [diff] [blame] | 457 | if (!(ctx->flags & BIT(FLAGS_FINUP))) { |
Dmitry Kasatkin | 887c883 | 2010-11-19 16:04:29 +0200 | [diff] [blame] | 458 | /* not last sg must be SHA1_MD5_BLOCK_SIZE aligned */ |
| 459 | tail = length & (SHA1_MD5_BLOCK_SIZE - 1); |
| 460 | /* without finup() we need one block to close hash */ |
| 461 | if (!tail) |
| 462 | tail = SHA1_MD5_BLOCK_SIZE; |
| 463 | length -= tail; |
| 464 | } |
| 465 | } |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 466 | |
| 467 | if (!dma_map_sg(dd->dev, ctx->sg, 1, DMA_TO_DEVICE)) { |
| 468 | dev_err(dd->dev, "dma_map_sg error\n"); |
| 469 | return -EINVAL; |
| 470 | } |
| 471 | |
Dmitry Kasatkin | ea1fd22 | 2011-06-02 21:10:05 +0300 | [diff] [blame] | 472 | ctx->flags |= BIT(FLAGS_SG); |
Dmitry Kasatkin | 887c883 | 2010-11-19 16:04:29 +0200 | [diff] [blame] | 473 | |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 474 | ctx->total -= length; |
Dmitry Kasatkin | 887c883 | 2010-11-19 16:04:29 +0200 | [diff] [blame] | 475 | ctx->offset = length; /* offset where to start slow */ |
| 476 | |
Dmitry Kasatkin | ea1fd22 | 2011-06-02 21:10:05 +0300 | [diff] [blame] | 477 | final = (ctx->flags & BIT(FLAGS_FINUP)) && !ctx->total; |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 478 | |
Dmitry Kasatkin | 798eed5d | 2010-11-19 16:04:26 +0200 | [diff] [blame] | 479 | /* next call does not fail... so no unmap in the case of error */ |
Dmitry Kasatkin | 887c883 | 2010-11-19 16:04:29 +0200 | [diff] [blame] | 480 | return omap_sham_xmit_dma(dd, sg_dma_address(ctx->sg), length, final); |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 481 | } |
| 482 | |
| 483 | static int omap_sham_update_cpu(struct omap_sham_dev *dd) |
| 484 | { |
| 485 | struct omap_sham_reqctx *ctx = ahash_request_ctx(dd->req); |
| 486 | int bufcnt; |
| 487 | |
| 488 | omap_sham_append_sg(ctx); |
| 489 | bufcnt = ctx->bufcnt; |
| 490 | ctx->bufcnt = 0; |
| 491 | |
| 492 | return omap_sham_xmit_cpu(dd, ctx->buffer, bufcnt, 1); |
| 493 | } |
| 494 | |
| 495 | static int omap_sham_update_dma_stop(struct omap_sham_dev *dd) |
| 496 | { |
| 497 | struct omap_sham_reqctx *ctx = ahash_request_ctx(dd->req); |
| 498 | |
| 499 | omap_stop_dma(dd->dma_lch); |
Dmitry Kasatkin | ea1fd22 | 2011-06-02 21:10:05 +0300 | [diff] [blame] | 500 | if (ctx->flags & BIT(FLAGS_SG)) { |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 501 | dma_unmap_sg(dd->dev, ctx->sg, 1, DMA_TO_DEVICE); |
Dmitry Kasatkin | 887c883 | 2010-11-19 16:04:29 +0200 | [diff] [blame] | 502 | if (ctx->sg->length == ctx->offset) { |
| 503 | ctx->sg = sg_next(ctx->sg); |
| 504 | if (ctx->sg) |
| 505 | ctx->offset = 0; |
| 506 | } |
| 507 | } else { |
Dmitry Kasatkin | 798eed5d | 2010-11-19 16:04:26 +0200 | [diff] [blame] | 508 | dma_unmap_single(dd->dev, ctx->dma_addr, ctx->buflen, |
| 509 | DMA_TO_DEVICE); |
Dmitry Kasatkin | 887c883 | 2010-11-19 16:04:29 +0200 | [diff] [blame] | 510 | } |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 511 | |
| 512 | return 0; |
| 513 | } |
| 514 | |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 515 | static int omap_sham_init(struct ahash_request *req) |
| 516 | { |
| 517 | struct crypto_ahash *tfm = crypto_ahash_reqtfm(req); |
| 518 | struct omap_sham_ctx *tctx = crypto_ahash_ctx(tfm); |
| 519 | struct omap_sham_reqctx *ctx = ahash_request_ctx(req); |
| 520 | struct omap_sham_dev *dd = NULL, *tmp; |
| 521 | |
| 522 | spin_lock_bh(&sham.lock); |
| 523 | if (!tctx->dd) { |
| 524 | list_for_each_entry(tmp, &sham.dev_list, list) { |
| 525 | dd = tmp; |
| 526 | break; |
| 527 | } |
| 528 | tctx->dd = dd; |
| 529 | } else { |
| 530 | dd = tctx->dd; |
| 531 | } |
| 532 | spin_unlock_bh(&sham.lock); |
| 533 | |
| 534 | ctx->dd = dd; |
| 535 | |
| 536 | ctx->flags = 0; |
| 537 | |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 538 | dev_dbg(dd->dev, "init: digest size: %d\n", |
| 539 | crypto_ahash_digestsize(tfm)); |
| 540 | |
| 541 | if (crypto_ahash_digestsize(tfm) == SHA1_DIGEST_SIZE) |
Dmitry Kasatkin | ea1fd22 | 2011-06-02 21:10:05 +0300 | [diff] [blame] | 542 | ctx->flags |= BIT(FLAGS_SHA1); |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 543 | |
| 544 | ctx->bufcnt = 0; |
| 545 | ctx->digcnt = 0; |
Dmitry Kasatkin | 798eed5d | 2010-11-19 16:04:26 +0200 | [diff] [blame] | 546 | ctx->buflen = BUFLEN; |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 547 | |
Dmitry Kasatkin | ea1fd22 | 2011-06-02 21:10:05 +0300 | [diff] [blame] | 548 | if (tctx->flags & BIT(FLAGS_HMAC)) { |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 549 | struct omap_sham_hmac_ctx *bctx = tctx->base; |
| 550 | |
| 551 | memcpy(ctx->buffer, bctx->ipad, SHA1_MD5_BLOCK_SIZE); |
| 552 | ctx->bufcnt = SHA1_MD5_BLOCK_SIZE; |
Dmitry Kasatkin | ea1fd22 | 2011-06-02 21:10:05 +0300 | [diff] [blame] | 553 | ctx->flags |= BIT(FLAGS_HMAC); |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 554 | } |
| 555 | |
| 556 | return 0; |
| 557 | |
| 558 | } |
| 559 | |
| 560 | static int omap_sham_update_req(struct omap_sham_dev *dd) |
| 561 | { |
| 562 | struct ahash_request *req = dd->req; |
| 563 | struct omap_sham_reqctx *ctx = ahash_request_ctx(req); |
| 564 | int err; |
| 565 | |
| 566 | dev_dbg(dd->dev, "update_req: total: %u, digcnt: %d, finup: %d\n", |
Dmitry Kasatkin | ea1fd22 | 2011-06-02 21:10:05 +0300 | [diff] [blame] | 567 | ctx->total, ctx->digcnt, (ctx->flags & BIT(FLAGS_FINUP)) != 0); |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 568 | |
Dmitry Kasatkin | ea1fd22 | 2011-06-02 21:10:05 +0300 | [diff] [blame] | 569 | if (ctx->flags & BIT(FLAGS_CPU)) |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 570 | err = omap_sham_update_cpu(dd); |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 571 | else |
Dmitry Kasatkin | 887c883 | 2010-11-19 16:04:29 +0200 | [diff] [blame] | 572 | err = omap_sham_update_dma_start(dd); |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 573 | |
| 574 | /* wait for dma completion before can take more data */ |
| 575 | dev_dbg(dd->dev, "update: err: %d, digcnt: %d\n", err, ctx->digcnt); |
| 576 | |
| 577 | return err; |
| 578 | } |
| 579 | |
| 580 | static int omap_sham_final_req(struct omap_sham_dev *dd) |
| 581 | { |
| 582 | struct ahash_request *req = dd->req; |
| 583 | struct omap_sham_reqctx *ctx = ahash_request_ctx(req); |
| 584 | int err = 0, use_dma = 1; |
| 585 | |
| 586 | if (ctx->bufcnt <= 64) |
| 587 | /* faster to handle last block with cpu */ |
| 588 | use_dma = 0; |
| 589 | |
| 590 | if (use_dma) |
Dmitry Kasatkin | 798eed5d | 2010-11-19 16:04:26 +0200 | [diff] [blame] | 591 | err = omap_sham_xmit_dma_map(dd, ctx, ctx->bufcnt, 1); |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 592 | else |
| 593 | err = omap_sham_xmit_cpu(dd, ctx->buffer, ctx->bufcnt, 1); |
| 594 | |
| 595 | ctx->bufcnt = 0; |
| 596 | |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 597 | dev_dbg(dd->dev, "final_req: err: %d\n", err); |
| 598 | |
| 599 | return err; |
| 600 | } |
| 601 | |
Dmitry Kasatkin | bf36275 | 2011-04-20 13:34:58 +0300 | [diff] [blame] | 602 | static int omap_sham_finish_hmac(struct ahash_request *req) |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 603 | { |
| 604 | struct omap_sham_ctx *tctx = crypto_tfm_ctx(req->base.tfm); |
| 605 | struct omap_sham_hmac_ctx *bctx = tctx->base; |
| 606 | int bs = crypto_shash_blocksize(bctx->shash); |
| 607 | int ds = crypto_shash_digestsize(bctx->shash); |
| 608 | struct { |
| 609 | struct shash_desc shash; |
| 610 | char ctx[crypto_shash_descsize(bctx->shash)]; |
| 611 | } desc; |
| 612 | |
| 613 | desc.shash.tfm = bctx->shash; |
| 614 | desc.shash.flags = 0; /* not CRYPTO_TFM_REQ_MAY_SLEEP */ |
| 615 | |
| 616 | return crypto_shash_init(&desc.shash) ?: |
| 617 | crypto_shash_update(&desc.shash, bctx->opad, bs) ?: |
Dmitry Kasatkin | bf36275 | 2011-04-20 13:34:58 +0300 | [diff] [blame] | 618 | crypto_shash_finup(&desc.shash, req->result, ds, req->result); |
| 619 | } |
| 620 | |
| 621 | static int omap_sham_finish(struct ahash_request *req) |
| 622 | { |
| 623 | struct omap_sham_reqctx *ctx = ahash_request_ctx(req); |
| 624 | struct omap_sham_dev *dd = ctx->dd; |
| 625 | int err = 0; |
| 626 | |
| 627 | if (ctx->digcnt) { |
| 628 | omap_sham_copy_ready_hash(req); |
Dmitry Kasatkin | ea1fd22 | 2011-06-02 21:10:05 +0300 | [diff] [blame] | 629 | if (ctx->flags & BIT(FLAGS_HMAC)) |
Dmitry Kasatkin | bf36275 | 2011-04-20 13:34:58 +0300 | [diff] [blame] | 630 | err = omap_sham_finish_hmac(req); |
| 631 | } |
| 632 | |
| 633 | dev_dbg(dd->dev, "digcnt: %d, bufcnt: %d\n", ctx->digcnt, ctx->bufcnt); |
| 634 | |
| 635 | return err; |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 636 | } |
| 637 | |
| 638 | static void omap_sham_finish_req(struct ahash_request *req, int err) |
| 639 | { |
| 640 | struct omap_sham_reqctx *ctx = ahash_request_ctx(req); |
Dmitry Kasatkin | 798eed5d | 2010-11-19 16:04:26 +0200 | [diff] [blame] | 641 | struct omap_sham_dev *dd = ctx->dd; |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 642 | |
| 643 | if (!err) { |
Dmitry Kasatkin | 0e87b15 | 2011-06-02 21:10:03 +0300 | [diff] [blame] | 644 | omap_sham_copy_hash(req, 1); |
Dmitry Kasatkin | ed3ea9a8 | 2011-06-02 21:10:07 +0300 | [diff] [blame] | 645 | if (test_bit(FLAGS_FINAL, &dd->flags)) |
Dmitry Kasatkin | bf36275 | 2011-04-20 13:34:58 +0300 | [diff] [blame] | 646 | err = omap_sham_finish(req); |
Dmitry Kasatkin | 3e133c8 | 2010-11-19 16:04:24 +0200 | [diff] [blame] | 647 | } else { |
Dmitry Kasatkin | ea1fd22 | 2011-06-02 21:10:05 +0300 | [diff] [blame] | 648 | ctx->flags |= BIT(FLAGS_ERROR); |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 649 | } |
| 650 | |
Dmitry Kasatkin | 0efd4d8 | 2011-06-02 21:10:12 +0300 | [diff] [blame] | 651 | /* atomic operation is not needed here */ |
| 652 | dd->flags &= ~(BIT(FLAGS_BUSY) | BIT(FLAGS_FINAL) | BIT(FLAGS_CPU) | |
| 653 | BIT(FLAGS_DMA_READY) | BIT(FLAGS_OUTPUT_READY)); |
Mark A. Greer | b359f03 | 2012-12-21 10:04:02 -0700 | [diff] [blame] | 654 | |
| 655 | pm_runtime_put_sync(dd->dev); |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 656 | |
| 657 | if (req->base.complete) |
| 658 | req->base.complete(&req->base, err); |
Dmitry Kasatkin | 6cb3ffe | 2011-06-02 21:10:09 +0300 | [diff] [blame] | 659 | |
| 660 | /* handle new request */ |
| 661 | tasklet_schedule(&dd->done_task); |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 662 | } |
| 663 | |
Dmitry Kasatkin | a5d8723 | 2010-11-19 16:04:25 +0200 | [diff] [blame] | 664 | static int omap_sham_handle_queue(struct omap_sham_dev *dd, |
| 665 | struct ahash_request *req) |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 666 | { |
Dmitry Kasatkin | 6c39d11 | 2010-12-29 21:52:04 +1100 | [diff] [blame] | 667 | struct crypto_async_request *async_req, *backlog; |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 668 | struct omap_sham_reqctx *ctx; |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 669 | unsigned long flags; |
Dmitry Kasatkin | a5d8723 | 2010-11-19 16:04:25 +0200 | [diff] [blame] | 670 | int err = 0, ret = 0; |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 671 | |
| 672 | spin_lock_irqsave(&dd->lock, flags); |
Dmitry Kasatkin | a5d8723 | 2010-11-19 16:04:25 +0200 | [diff] [blame] | 673 | if (req) |
| 674 | ret = ahash_enqueue_request(&dd->queue, req); |
Dmitry Kasatkin | a929cbe | 2011-06-02 21:10:06 +0300 | [diff] [blame] | 675 | if (test_bit(FLAGS_BUSY, &dd->flags)) { |
Dmitry Kasatkin | a5d8723 | 2010-11-19 16:04:25 +0200 | [diff] [blame] | 676 | spin_unlock_irqrestore(&dd->lock, flags); |
| 677 | return ret; |
| 678 | } |
Dmitry Kasatkin | 6c39d11 | 2010-12-29 21:52:04 +1100 | [diff] [blame] | 679 | backlog = crypto_get_backlog(&dd->queue); |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 680 | async_req = crypto_dequeue_request(&dd->queue); |
Dmitry Kasatkin | 6c39d11 | 2010-12-29 21:52:04 +1100 | [diff] [blame] | 681 | if (async_req) |
Dmitry Kasatkin | a929cbe | 2011-06-02 21:10:06 +0300 | [diff] [blame] | 682 | set_bit(FLAGS_BUSY, &dd->flags); |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 683 | spin_unlock_irqrestore(&dd->lock, flags); |
| 684 | |
| 685 | if (!async_req) |
Dmitry Kasatkin | a5d8723 | 2010-11-19 16:04:25 +0200 | [diff] [blame] | 686 | return ret; |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 687 | |
| 688 | if (backlog) |
| 689 | backlog->complete(backlog, -EINPROGRESS); |
| 690 | |
| 691 | req = ahash_request_cast(async_req); |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 692 | dd->req = req; |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 693 | ctx = ahash_request_ctx(req); |
| 694 | |
| 695 | dev_dbg(dd->dev, "handling new req, op: %lu, nbytes: %d\n", |
| 696 | ctx->op, req->nbytes); |
| 697 | |
Dmitry Kasatkin | 798eed5d | 2010-11-19 16:04:26 +0200 | [diff] [blame] | 698 | err = omap_sham_hw_init(dd); |
| 699 | if (err) |
| 700 | goto err1; |
| 701 | |
| 702 | omap_set_dma_dest_params(dd->dma_lch, 0, |
| 703 | OMAP_DMA_AMODE_CONSTANT, |
| 704 | dd->phys_base + SHA_REG_DIN(0), 0, 16); |
| 705 | |
| 706 | omap_set_dma_dest_burst_mode(dd->dma_lch, |
| 707 | OMAP_DMA_DATA_BURST_16); |
| 708 | |
| 709 | omap_set_dma_src_burst_mode(dd->dma_lch, |
| 710 | OMAP_DMA_DATA_BURST_4); |
| 711 | |
| 712 | if (ctx->digcnt) |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 713 | /* request has changed - restore hash */ |
| 714 | omap_sham_copy_hash(req, 0); |
| 715 | |
| 716 | if (ctx->op == OP_UPDATE) { |
| 717 | err = omap_sham_update_req(dd); |
Dmitry Kasatkin | ea1fd22 | 2011-06-02 21:10:05 +0300 | [diff] [blame] | 718 | if (err != -EINPROGRESS && (ctx->flags & BIT(FLAGS_FINUP))) |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 719 | /* no final() after finup() */ |
| 720 | err = omap_sham_final_req(dd); |
| 721 | } else if (ctx->op == OP_FINAL) { |
| 722 | err = omap_sham_final_req(dd); |
| 723 | } |
Dmitry Kasatkin | 798eed5d | 2010-11-19 16:04:26 +0200 | [diff] [blame] | 724 | err1: |
Dmitry Kasatkin | 6cb3ffe | 2011-06-02 21:10:09 +0300 | [diff] [blame] | 725 | if (err != -EINPROGRESS) |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 726 | /* done_task will not finish it, so do it here */ |
| 727 | omap_sham_finish_req(req, err); |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 728 | |
| 729 | dev_dbg(dd->dev, "exit, err: %d\n", err); |
| 730 | |
Dmitry Kasatkin | a5d8723 | 2010-11-19 16:04:25 +0200 | [diff] [blame] | 731 | return ret; |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 732 | } |
| 733 | |
| 734 | static int omap_sham_enqueue(struct ahash_request *req, unsigned int op) |
| 735 | { |
| 736 | struct omap_sham_reqctx *ctx = ahash_request_ctx(req); |
| 737 | struct omap_sham_ctx *tctx = crypto_tfm_ctx(req->base.tfm); |
| 738 | struct omap_sham_dev *dd = tctx->dd; |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 739 | |
| 740 | ctx->op = op; |
| 741 | |
Dmitry Kasatkin | a5d8723 | 2010-11-19 16:04:25 +0200 | [diff] [blame] | 742 | return omap_sham_handle_queue(dd, req); |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 743 | } |
| 744 | |
| 745 | static int omap_sham_update(struct ahash_request *req) |
| 746 | { |
| 747 | struct omap_sham_reqctx *ctx = ahash_request_ctx(req); |
| 748 | |
| 749 | if (!req->nbytes) |
| 750 | return 0; |
| 751 | |
| 752 | ctx->total = req->nbytes; |
| 753 | ctx->sg = req->src; |
| 754 | ctx->offset = 0; |
| 755 | |
Dmitry Kasatkin | ea1fd22 | 2011-06-02 21:10:05 +0300 | [diff] [blame] | 756 | if (ctx->flags & BIT(FLAGS_FINUP)) { |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 757 | if ((ctx->digcnt + ctx->bufcnt + ctx->total) < 9) { |
| 758 | /* |
| 759 | * OMAP HW accel works only with buffers >= 9 |
| 760 | * will switch to bypass in final() |
| 761 | * final has the same request and data |
| 762 | */ |
| 763 | omap_sham_append_sg(ctx); |
| 764 | return 0; |
Dmitry Kasatkin | 887c883 | 2010-11-19 16:04:29 +0200 | [diff] [blame] | 765 | } else if (ctx->bufcnt + ctx->total <= SHA1_MD5_BLOCK_SIZE) { |
| 766 | /* |
| 767 | * faster to use CPU for short transfers |
| 768 | */ |
Dmitry Kasatkin | ea1fd22 | 2011-06-02 21:10:05 +0300 | [diff] [blame] | 769 | ctx->flags |= BIT(FLAGS_CPU); |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 770 | } |
Dmitry Kasatkin | 887c883 | 2010-11-19 16:04:29 +0200 | [diff] [blame] | 771 | } else if (ctx->bufcnt + ctx->total < ctx->buflen) { |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 772 | omap_sham_append_sg(ctx); |
| 773 | return 0; |
| 774 | } |
| 775 | |
| 776 | return omap_sham_enqueue(req, OP_UPDATE); |
| 777 | } |
| 778 | |
| 779 | static int omap_sham_shash_digest(struct crypto_shash *shash, u32 flags, |
| 780 | const u8 *data, unsigned int len, u8 *out) |
| 781 | { |
| 782 | struct { |
| 783 | struct shash_desc shash; |
| 784 | char ctx[crypto_shash_descsize(shash)]; |
| 785 | } desc; |
| 786 | |
| 787 | desc.shash.tfm = shash; |
| 788 | desc.shash.flags = flags & CRYPTO_TFM_REQ_MAY_SLEEP; |
| 789 | |
| 790 | return crypto_shash_digest(&desc.shash, data, len, out); |
| 791 | } |
| 792 | |
| 793 | static int omap_sham_final_shash(struct ahash_request *req) |
| 794 | { |
| 795 | struct omap_sham_ctx *tctx = crypto_tfm_ctx(req->base.tfm); |
| 796 | struct omap_sham_reqctx *ctx = ahash_request_ctx(req); |
| 797 | |
| 798 | return omap_sham_shash_digest(tctx->fallback, req->base.flags, |
| 799 | ctx->buffer, ctx->bufcnt, req->result); |
| 800 | } |
| 801 | |
| 802 | static int omap_sham_final(struct ahash_request *req) |
| 803 | { |
| 804 | struct omap_sham_reqctx *ctx = ahash_request_ctx(req); |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 805 | |
Dmitry Kasatkin | ea1fd22 | 2011-06-02 21:10:05 +0300 | [diff] [blame] | 806 | ctx->flags |= BIT(FLAGS_FINUP); |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 807 | |
Dmitry Kasatkin | ea1fd22 | 2011-06-02 21:10:05 +0300 | [diff] [blame] | 808 | if (ctx->flags & BIT(FLAGS_ERROR)) |
Dmitry Kasatkin | bf36275 | 2011-04-20 13:34:58 +0300 | [diff] [blame] | 809 | return 0; /* uncompleted hash is not needed */ |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 810 | |
Dmitry Kasatkin | bf36275 | 2011-04-20 13:34:58 +0300 | [diff] [blame] | 811 | /* OMAP HW accel works only with buffers >= 9 */ |
| 812 | /* HMAC is always >= 9 because ipad == block size */ |
| 813 | if ((ctx->digcnt + ctx->bufcnt) < 9) |
| 814 | return omap_sham_final_shash(req); |
| 815 | else if (ctx->bufcnt) |
| 816 | return omap_sham_enqueue(req, OP_FINAL); |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 817 | |
Dmitry Kasatkin | bf36275 | 2011-04-20 13:34:58 +0300 | [diff] [blame] | 818 | /* copy ready hash (+ finalize hmac) */ |
| 819 | return omap_sham_finish(req); |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 820 | } |
| 821 | |
| 822 | static int omap_sham_finup(struct ahash_request *req) |
| 823 | { |
| 824 | struct omap_sham_reqctx *ctx = ahash_request_ctx(req); |
| 825 | int err1, err2; |
| 826 | |
Dmitry Kasatkin | ea1fd22 | 2011-06-02 21:10:05 +0300 | [diff] [blame] | 827 | ctx->flags |= BIT(FLAGS_FINUP); |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 828 | |
| 829 | err1 = omap_sham_update(req); |
Markku Kylanpaa | 455e338 | 2011-04-20 13:34:55 +0300 | [diff] [blame] | 830 | if (err1 == -EINPROGRESS || err1 == -EBUSY) |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 831 | return err1; |
| 832 | /* |
| 833 | * final() has to be always called to cleanup resources |
| 834 | * even if udpate() failed, except EINPROGRESS |
| 835 | */ |
| 836 | err2 = omap_sham_final(req); |
| 837 | |
| 838 | return err1 ?: err2; |
| 839 | } |
| 840 | |
| 841 | static int omap_sham_digest(struct ahash_request *req) |
| 842 | { |
| 843 | return omap_sham_init(req) ?: omap_sham_finup(req); |
| 844 | } |
| 845 | |
| 846 | static int omap_sham_setkey(struct crypto_ahash *tfm, const u8 *key, |
| 847 | unsigned int keylen) |
| 848 | { |
| 849 | struct omap_sham_ctx *tctx = crypto_ahash_ctx(tfm); |
| 850 | struct omap_sham_hmac_ctx *bctx = tctx->base; |
| 851 | int bs = crypto_shash_blocksize(bctx->shash); |
| 852 | int ds = crypto_shash_digestsize(bctx->shash); |
| 853 | int err, i; |
| 854 | err = crypto_shash_setkey(tctx->fallback, key, keylen); |
| 855 | if (err) |
| 856 | return err; |
| 857 | |
| 858 | if (keylen > bs) { |
| 859 | err = omap_sham_shash_digest(bctx->shash, |
| 860 | crypto_shash_get_flags(bctx->shash), |
| 861 | key, keylen, bctx->ipad); |
| 862 | if (err) |
| 863 | return err; |
| 864 | keylen = ds; |
| 865 | } else { |
| 866 | memcpy(bctx->ipad, key, keylen); |
| 867 | } |
| 868 | |
| 869 | memset(bctx->ipad + keylen, 0, bs - keylen); |
| 870 | memcpy(bctx->opad, bctx->ipad, bs); |
| 871 | |
| 872 | for (i = 0; i < bs; i++) { |
| 873 | bctx->ipad[i] ^= 0x36; |
| 874 | bctx->opad[i] ^= 0x5c; |
| 875 | } |
| 876 | |
| 877 | return err; |
| 878 | } |
| 879 | |
| 880 | static int omap_sham_cra_init_alg(struct crypto_tfm *tfm, const char *alg_base) |
| 881 | { |
| 882 | struct omap_sham_ctx *tctx = crypto_tfm_ctx(tfm); |
| 883 | const char *alg_name = crypto_tfm_alg_name(tfm); |
| 884 | |
| 885 | /* Allocate a fallback and abort if it failed. */ |
| 886 | tctx->fallback = crypto_alloc_shash(alg_name, 0, |
| 887 | CRYPTO_ALG_NEED_FALLBACK); |
| 888 | if (IS_ERR(tctx->fallback)) { |
| 889 | pr_err("omap-sham: fallback driver '%s' " |
| 890 | "could not be loaded.\n", alg_name); |
| 891 | return PTR_ERR(tctx->fallback); |
| 892 | } |
| 893 | |
| 894 | crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm), |
Dmitry Kasatkin | 798eed5d | 2010-11-19 16:04:26 +0200 | [diff] [blame] | 895 | sizeof(struct omap_sham_reqctx) + BUFLEN); |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 896 | |
| 897 | if (alg_base) { |
| 898 | struct omap_sham_hmac_ctx *bctx = tctx->base; |
Dmitry Kasatkin | ea1fd22 | 2011-06-02 21:10:05 +0300 | [diff] [blame] | 899 | tctx->flags |= BIT(FLAGS_HMAC); |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 900 | bctx->shash = crypto_alloc_shash(alg_base, 0, |
| 901 | CRYPTO_ALG_NEED_FALLBACK); |
| 902 | if (IS_ERR(bctx->shash)) { |
| 903 | pr_err("omap-sham: base driver '%s' " |
| 904 | "could not be loaded.\n", alg_base); |
| 905 | crypto_free_shash(tctx->fallback); |
| 906 | return PTR_ERR(bctx->shash); |
| 907 | } |
| 908 | |
| 909 | } |
| 910 | |
| 911 | return 0; |
| 912 | } |
| 913 | |
| 914 | static int omap_sham_cra_init(struct crypto_tfm *tfm) |
| 915 | { |
| 916 | return omap_sham_cra_init_alg(tfm, NULL); |
| 917 | } |
| 918 | |
| 919 | static int omap_sham_cra_sha1_init(struct crypto_tfm *tfm) |
| 920 | { |
| 921 | return omap_sham_cra_init_alg(tfm, "sha1"); |
| 922 | } |
| 923 | |
| 924 | static int omap_sham_cra_md5_init(struct crypto_tfm *tfm) |
| 925 | { |
| 926 | return omap_sham_cra_init_alg(tfm, "md5"); |
| 927 | } |
| 928 | |
| 929 | static void omap_sham_cra_exit(struct crypto_tfm *tfm) |
| 930 | { |
| 931 | struct omap_sham_ctx *tctx = crypto_tfm_ctx(tfm); |
| 932 | |
| 933 | crypto_free_shash(tctx->fallback); |
| 934 | tctx->fallback = NULL; |
| 935 | |
Dmitry Kasatkin | ea1fd22 | 2011-06-02 21:10:05 +0300 | [diff] [blame] | 936 | if (tctx->flags & BIT(FLAGS_HMAC)) { |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 937 | struct omap_sham_hmac_ctx *bctx = tctx->base; |
| 938 | crypto_free_shash(bctx->shash); |
| 939 | } |
| 940 | } |
| 941 | |
| 942 | static struct ahash_alg algs[] = { |
| 943 | { |
| 944 | .init = omap_sham_init, |
| 945 | .update = omap_sham_update, |
| 946 | .final = omap_sham_final, |
| 947 | .finup = omap_sham_finup, |
| 948 | .digest = omap_sham_digest, |
| 949 | .halg.digestsize = SHA1_DIGEST_SIZE, |
| 950 | .halg.base = { |
| 951 | .cra_name = "sha1", |
| 952 | .cra_driver_name = "omap-sha1", |
| 953 | .cra_priority = 100, |
| 954 | .cra_flags = CRYPTO_ALG_TYPE_AHASH | |
Nikos Mavrogiannopoulos | d912bb7 | 2011-11-01 13:39:56 +0100 | [diff] [blame] | 955 | CRYPTO_ALG_KERN_DRIVER_ONLY | |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 956 | CRYPTO_ALG_ASYNC | |
| 957 | CRYPTO_ALG_NEED_FALLBACK, |
| 958 | .cra_blocksize = SHA1_BLOCK_SIZE, |
| 959 | .cra_ctxsize = sizeof(struct omap_sham_ctx), |
| 960 | .cra_alignmask = 0, |
| 961 | .cra_module = THIS_MODULE, |
| 962 | .cra_init = omap_sham_cra_init, |
| 963 | .cra_exit = omap_sham_cra_exit, |
| 964 | } |
| 965 | }, |
| 966 | { |
| 967 | .init = omap_sham_init, |
| 968 | .update = omap_sham_update, |
| 969 | .final = omap_sham_final, |
| 970 | .finup = omap_sham_finup, |
| 971 | .digest = omap_sham_digest, |
| 972 | .halg.digestsize = MD5_DIGEST_SIZE, |
| 973 | .halg.base = { |
| 974 | .cra_name = "md5", |
| 975 | .cra_driver_name = "omap-md5", |
| 976 | .cra_priority = 100, |
| 977 | .cra_flags = CRYPTO_ALG_TYPE_AHASH | |
Nikos Mavrogiannopoulos | d912bb7 | 2011-11-01 13:39:56 +0100 | [diff] [blame] | 978 | CRYPTO_ALG_KERN_DRIVER_ONLY | |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 979 | CRYPTO_ALG_ASYNC | |
| 980 | CRYPTO_ALG_NEED_FALLBACK, |
| 981 | .cra_blocksize = SHA1_BLOCK_SIZE, |
| 982 | .cra_ctxsize = sizeof(struct omap_sham_ctx), |
Dmitry Kasatkin | 798eed5d | 2010-11-19 16:04:26 +0200 | [diff] [blame] | 983 | .cra_alignmask = OMAP_ALIGN_MASK, |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 984 | .cra_module = THIS_MODULE, |
| 985 | .cra_init = omap_sham_cra_init, |
| 986 | .cra_exit = omap_sham_cra_exit, |
| 987 | } |
| 988 | }, |
| 989 | { |
| 990 | .init = omap_sham_init, |
| 991 | .update = omap_sham_update, |
| 992 | .final = omap_sham_final, |
| 993 | .finup = omap_sham_finup, |
| 994 | .digest = omap_sham_digest, |
| 995 | .setkey = omap_sham_setkey, |
| 996 | .halg.digestsize = SHA1_DIGEST_SIZE, |
| 997 | .halg.base = { |
| 998 | .cra_name = "hmac(sha1)", |
| 999 | .cra_driver_name = "omap-hmac-sha1", |
| 1000 | .cra_priority = 100, |
| 1001 | .cra_flags = CRYPTO_ALG_TYPE_AHASH | |
Nikos Mavrogiannopoulos | d912bb7 | 2011-11-01 13:39:56 +0100 | [diff] [blame] | 1002 | CRYPTO_ALG_KERN_DRIVER_ONLY | |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 1003 | CRYPTO_ALG_ASYNC | |
| 1004 | CRYPTO_ALG_NEED_FALLBACK, |
| 1005 | .cra_blocksize = SHA1_BLOCK_SIZE, |
| 1006 | .cra_ctxsize = sizeof(struct omap_sham_ctx) + |
| 1007 | sizeof(struct omap_sham_hmac_ctx), |
Dmitry Kasatkin | 798eed5d | 2010-11-19 16:04:26 +0200 | [diff] [blame] | 1008 | .cra_alignmask = OMAP_ALIGN_MASK, |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 1009 | .cra_module = THIS_MODULE, |
| 1010 | .cra_init = omap_sham_cra_sha1_init, |
| 1011 | .cra_exit = omap_sham_cra_exit, |
| 1012 | } |
| 1013 | }, |
| 1014 | { |
| 1015 | .init = omap_sham_init, |
| 1016 | .update = omap_sham_update, |
| 1017 | .final = omap_sham_final, |
| 1018 | .finup = omap_sham_finup, |
| 1019 | .digest = omap_sham_digest, |
| 1020 | .setkey = omap_sham_setkey, |
| 1021 | .halg.digestsize = MD5_DIGEST_SIZE, |
| 1022 | .halg.base = { |
| 1023 | .cra_name = "hmac(md5)", |
| 1024 | .cra_driver_name = "omap-hmac-md5", |
| 1025 | .cra_priority = 100, |
| 1026 | .cra_flags = CRYPTO_ALG_TYPE_AHASH | |
Nikos Mavrogiannopoulos | d912bb7 | 2011-11-01 13:39:56 +0100 | [diff] [blame] | 1027 | CRYPTO_ALG_KERN_DRIVER_ONLY | |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 1028 | CRYPTO_ALG_ASYNC | |
| 1029 | CRYPTO_ALG_NEED_FALLBACK, |
| 1030 | .cra_blocksize = SHA1_BLOCK_SIZE, |
| 1031 | .cra_ctxsize = sizeof(struct omap_sham_ctx) + |
| 1032 | sizeof(struct omap_sham_hmac_ctx), |
Dmitry Kasatkin | 798eed5d | 2010-11-19 16:04:26 +0200 | [diff] [blame] | 1033 | .cra_alignmask = OMAP_ALIGN_MASK, |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 1034 | .cra_module = THIS_MODULE, |
| 1035 | .cra_init = omap_sham_cra_md5_init, |
| 1036 | .cra_exit = omap_sham_cra_exit, |
| 1037 | } |
| 1038 | } |
| 1039 | }; |
| 1040 | |
| 1041 | static void omap_sham_done_task(unsigned long data) |
| 1042 | { |
| 1043 | struct omap_sham_dev *dd = (struct omap_sham_dev *)data; |
Dmitry Kasatkin | 6c63db8 | 2011-06-02 21:10:10 +0300 | [diff] [blame] | 1044 | int err = 0; |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 1045 | |
Dmitry Kasatkin | 6cb3ffe | 2011-06-02 21:10:09 +0300 | [diff] [blame] | 1046 | if (!test_bit(FLAGS_BUSY, &dd->flags)) { |
| 1047 | omap_sham_handle_queue(dd, NULL); |
| 1048 | return; |
| 1049 | } |
| 1050 | |
Dmitry Kasatkin | 6c63db8 | 2011-06-02 21:10:10 +0300 | [diff] [blame] | 1051 | if (test_bit(FLAGS_CPU, &dd->flags)) { |
| 1052 | if (test_and_clear_bit(FLAGS_OUTPUT_READY, &dd->flags)) |
| 1053 | goto finish; |
| 1054 | } else if (test_bit(FLAGS_DMA_READY, &dd->flags)) { |
| 1055 | if (test_and_clear_bit(FLAGS_DMA_ACTIVE, &dd->flags)) { |
| 1056 | omap_sham_update_dma_stop(dd); |
| 1057 | if (dd->err) { |
| 1058 | err = dd->err; |
| 1059 | goto finish; |
| 1060 | } |
| 1061 | } |
| 1062 | if (test_and_clear_bit(FLAGS_OUTPUT_READY, &dd->flags)) { |
| 1063 | /* hash or semi-hash ready */ |
| 1064 | clear_bit(FLAGS_DMA_READY, &dd->flags); |
Dmitry Kasatkin | 887c883 | 2010-11-19 16:04:29 +0200 | [diff] [blame] | 1065 | err = omap_sham_update_dma_start(dd); |
Dmitry Kasatkin | 6c63db8 | 2011-06-02 21:10:10 +0300 | [diff] [blame] | 1066 | if (err != -EINPROGRESS) |
| 1067 | goto finish; |
| 1068 | } |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 1069 | } |
| 1070 | |
Dmitry Kasatkin | 6c63db8 | 2011-06-02 21:10:10 +0300 | [diff] [blame] | 1071 | return; |
Dmitry Kasatkin | 3e133c8 | 2010-11-19 16:04:24 +0200 | [diff] [blame] | 1072 | |
Dmitry Kasatkin | 6c63db8 | 2011-06-02 21:10:10 +0300 | [diff] [blame] | 1073 | finish: |
| 1074 | dev_dbg(dd->dev, "update done: err: %d\n", err); |
| 1075 | /* finish curent request */ |
| 1076 | omap_sham_finish_req(dd->req, err); |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 1077 | } |
| 1078 | |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 1079 | static irqreturn_t omap_sham_irq(int irq, void *dev_id) |
| 1080 | { |
| 1081 | struct omap_sham_dev *dd = dev_id; |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 1082 | |
Dmitry Kasatkin | ed3ea9a8 | 2011-06-02 21:10:07 +0300 | [diff] [blame] | 1083 | if (unlikely(test_bit(FLAGS_FINAL, &dd->flags))) |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 1084 | /* final -> allow device to go to power-saving mode */ |
| 1085 | omap_sham_write_mask(dd, SHA_REG_CTRL, 0, SHA_REG_CTRL_LENGTH); |
| 1086 | |
| 1087 | omap_sham_write_mask(dd, SHA_REG_CTRL, SHA_REG_CTRL_OUTPUT_READY, |
| 1088 | SHA_REG_CTRL_OUTPUT_READY); |
| 1089 | omap_sham_read(dd, SHA_REG_CTRL); |
| 1090 | |
Dmitry Kasatkin | cd3f1d5 | 2011-06-02 21:10:13 +0300 | [diff] [blame] | 1091 | if (!test_bit(FLAGS_BUSY, &dd->flags)) { |
| 1092 | dev_warn(dd->dev, "Interrupt when no active requests.\n"); |
| 1093 | return IRQ_HANDLED; |
| 1094 | } |
| 1095 | |
Dmitry Kasatkin | ed3ea9a8 | 2011-06-02 21:10:07 +0300 | [diff] [blame] | 1096 | set_bit(FLAGS_OUTPUT_READY, &dd->flags); |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 1097 | tasklet_schedule(&dd->done_task); |
| 1098 | |
| 1099 | return IRQ_HANDLED; |
| 1100 | } |
| 1101 | |
| 1102 | static void omap_sham_dma_callback(int lch, u16 ch_status, void *data) |
| 1103 | { |
| 1104 | struct omap_sham_dev *dd = data; |
| 1105 | |
Dmitry Kasatkin | 3e133c8 | 2010-11-19 16:04:24 +0200 | [diff] [blame] | 1106 | if (ch_status != OMAP_DMA_BLOCK_IRQ) { |
| 1107 | pr_err("omap-sham DMA error status: 0x%hx\n", ch_status); |
| 1108 | dd->err = -EIO; |
Dmitry Kasatkin | a929cbe | 2011-06-02 21:10:06 +0300 | [diff] [blame] | 1109 | clear_bit(FLAGS_INIT, &dd->flags);/* request to re-initialize */ |
Dmitry Kasatkin | 3e133c8 | 2010-11-19 16:04:24 +0200 | [diff] [blame] | 1110 | } |
| 1111 | |
Dmitry Kasatkin | 6c63db8 | 2011-06-02 21:10:10 +0300 | [diff] [blame] | 1112 | set_bit(FLAGS_DMA_READY, &dd->flags); |
Dmitry Kasatkin | 3e133c8 | 2010-11-19 16:04:24 +0200 | [diff] [blame] | 1113 | tasklet_schedule(&dd->done_task); |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 1114 | } |
| 1115 | |
| 1116 | static int omap_sham_dma_init(struct omap_sham_dev *dd) |
| 1117 | { |
| 1118 | int err; |
| 1119 | |
| 1120 | dd->dma_lch = -1; |
| 1121 | |
| 1122 | err = omap_request_dma(dd->dma, dev_name(dd->dev), |
| 1123 | omap_sham_dma_callback, dd, &dd->dma_lch); |
| 1124 | if (err) { |
| 1125 | dev_err(dd->dev, "Unable to request DMA channel\n"); |
| 1126 | return err; |
| 1127 | } |
Samu Onkalo | 584db6a | 2010-09-03 19:20:19 +0800 | [diff] [blame] | 1128 | |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 1129 | return 0; |
| 1130 | } |
| 1131 | |
| 1132 | static void omap_sham_dma_cleanup(struct omap_sham_dev *dd) |
| 1133 | { |
| 1134 | if (dd->dma_lch >= 0) { |
| 1135 | omap_free_dma(dd->dma_lch); |
| 1136 | dd->dma_lch = -1; |
| 1137 | } |
| 1138 | } |
| 1139 | |
| 1140 | static int __devinit omap_sham_probe(struct platform_device *pdev) |
| 1141 | { |
| 1142 | struct omap_sham_dev *dd; |
| 1143 | struct device *dev = &pdev->dev; |
| 1144 | struct resource *res; |
| 1145 | int err, i, j; |
| 1146 | |
| 1147 | dd = kzalloc(sizeof(struct omap_sham_dev), GFP_KERNEL); |
| 1148 | if (dd == NULL) { |
| 1149 | dev_err(dev, "unable to alloc data struct.\n"); |
| 1150 | err = -ENOMEM; |
| 1151 | goto data_err; |
| 1152 | } |
| 1153 | dd->dev = dev; |
| 1154 | platform_set_drvdata(pdev, dd); |
| 1155 | |
| 1156 | INIT_LIST_HEAD(&dd->list); |
| 1157 | spin_lock_init(&dd->lock); |
| 1158 | tasklet_init(&dd->done_task, omap_sham_done_task, (unsigned long)dd); |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 1159 | crypto_init_queue(&dd->queue, OMAP_SHAM_QUEUE_LENGTH); |
| 1160 | |
| 1161 | dd->irq = -1; |
| 1162 | |
| 1163 | /* Get the base address */ |
| 1164 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 1165 | if (!res) { |
| 1166 | dev_err(dev, "no MEM resource info\n"); |
| 1167 | err = -ENODEV; |
| 1168 | goto res_err; |
| 1169 | } |
| 1170 | dd->phys_base = res->start; |
| 1171 | |
| 1172 | /* Get the DMA */ |
| 1173 | res = platform_get_resource(pdev, IORESOURCE_DMA, 0); |
| 1174 | if (!res) { |
| 1175 | dev_err(dev, "no DMA resource info\n"); |
| 1176 | err = -ENODEV; |
| 1177 | goto res_err; |
| 1178 | } |
| 1179 | dd->dma = res->start; |
| 1180 | |
| 1181 | /* Get the IRQ */ |
| 1182 | dd->irq = platform_get_irq(pdev, 0); |
| 1183 | if (dd->irq < 0) { |
| 1184 | dev_err(dev, "no IRQ resource info\n"); |
| 1185 | err = dd->irq; |
| 1186 | goto res_err; |
| 1187 | } |
| 1188 | |
| 1189 | err = request_irq(dd->irq, omap_sham_irq, |
| 1190 | IRQF_TRIGGER_LOW, dev_name(dev), dd); |
| 1191 | if (err) { |
| 1192 | dev_err(dev, "unable to request irq.\n"); |
| 1193 | goto res_err; |
| 1194 | } |
| 1195 | |
| 1196 | err = omap_sham_dma_init(dd); |
| 1197 | if (err) |
| 1198 | goto dma_err; |
| 1199 | |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 1200 | dd->io_base = ioremap(dd->phys_base, SZ_4K); |
| 1201 | if (!dd->io_base) { |
| 1202 | dev_err(dev, "can't ioremap\n"); |
| 1203 | err = -ENOMEM; |
| 1204 | goto io_err; |
| 1205 | } |
| 1206 | |
Mark A. Greer | b359f03 | 2012-12-21 10:04:02 -0700 | [diff] [blame] | 1207 | pm_runtime_enable(dev); |
| 1208 | pm_runtime_get_sync(dev); |
| 1209 | |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 1210 | dev_info(dev, "hw accel on OMAP rev %u.%u\n", |
| 1211 | (omap_sham_read(dd, SHA_REG_REV) & SHA_REG_REV_MAJOR) >> 4, |
| 1212 | omap_sham_read(dd, SHA_REG_REV) & SHA_REG_REV_MINOR); |
Mark A. Greer | b359f03 | 2012-12-21 10:04:02 -0700 | [diff] [blame] | 1213 | |
| 1214 | pm_runtime_put_sync(&pdev->dev); |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 1215 | |
| 1216 | spin_lock(&sham.lock); |
| 1217 | list_add_tail(&dd->list, &sham.dev_list); |
| 1218 | spin_unlock(&sham.lock); |
| 1219 | |
| 1220 | for (i = 0; i < ARRAY_SIZE(algs); i++) { |
| 1221 | err = crypto_register_ahash(&algs[i]); |
| 1222 | if (err) |
| 1223 | goto err_algs; |
| 1224 | } |
| 1225 | |
| 1226 | return 0; |
| 1227 | |
| 1228 | err_algs: |
| 1229 | for (j = 0; j < i; j++) |
| 1230 | crypto_unregister_ahash(&algs[j]); |
| 1231 | iounmap(dd->io_base); |
Mark A. Greer | b359f03 | 2012-12-21 10:04:02 -0700 | [diff] [blame] | 1232 | pm_runtime_disable(dev); |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 1233 | io_err: |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 1234 | omap_sham_dma_cleanup(dd); |
| 1235 | dma_err: |
| 1236 | if (dd->irq >= 0) |
| 1237 | free_irq(dd->irq, dd); |
| 1238 | res_err: |
| 1239 | kfree(dd); |
| 1240 | dd = NULL; |
| 1241 | data_err: |
| 1242 | dev_err(dev, "initialization failed.\n"); |
| 1243 | |
| 1244 | return err; |
| 1245 | } |
| 1246 | |
| 1247 | static int __devexit omap_sham_remove(struct platform_device *pdev) |
| 1248 | { |
| 1249 | static struct omap_sham_dev *dd; |
| 1250 | int i; |
| 1251 | |
| 1252 | dd = platform_get_drvdata(pdev); |
| 1253 | if (!dd) |
| 1254 | return -ENODEV; |
| 1255 | spin_lock(&sham.lock); |
| 1256 | list_del(&dd->list); |
| 1257 | spin_unlock(&sham.lock); |
| 1258 | for (i = 0; i < ARRAY_SIZE(algs); i++) |
| 1259 | crypto_unregister_ahash(&algs[i]); |
| 1260 | tasklet_kill(&dd->done_task); |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 1261 | iounmap(dd->io_base); |
Mark A. Greer | b359f03 | 2012-12-21 10:04:02 -0700 | [diff] [blame] | 1262 | pm_runtime_disable(&pdev->dev); |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 1263 | omap_sham_dma_cleanup(dd); |
| 1264 | if (dd->irq >= 0) |
| 1265 | free_irq(dd->irq, dd); |
| 1266 | kfree(dd); |
| 1267 | dd = NULL; |
| 1268 | |
| 1269 | return 0; |
| 1270 | } |
| 1271 | |
Mark A. Greer | 3b3f440 | 2012-12-21 10:04:03 -0700 | [diff] [blame^] | 1272 | #ifdef CONFIG_PM_SLEEP |
| 1273 | static int omap_sham_suspend(struct device *dev) |
| 1274 | { |
| 1275 | pm_runtime_put_sync(dev); |
| 1276 | return 0; |
| 1277 | } |
| 1278 | |
| 1279 | static int omap_sham_resume(struct device *dev) |
| 1280 | { |
| 1281 | pm_runtime_get_sync(dev); |
| 1282 | return 0; |
| 1283 | } |
| 1284 | #endif |
| 1285 | |
| 1286 | static const struct dev_pm_ops omap_sham_pm_ops = { |
| 1287 | SET_SYSTEM_SLEEP_PM_OPS(omap_sham_suspend, omap_sham_resume) |
| 1288 | }; |
| 1289 | |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 1290 | static struct platform_driver omap_sham_driver = { |
| 1291 | .probe = omap_sham_probe, |
| 1292 | .remove = omap_sham_remove, |
| 1293 | .driver = { |
| 1294 | .name = "omap-sham", |
| 1295 | .owner = THIS_MODULE, |
Mark A. Greer | 3b3f440 | 2012-12-21 10:04:03 -0700 | [diff] [blame^] | 1296 | .pm = &omap_sham_pm_ops, |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 1297 | }, |
| 1298 | }; |
| 1299 | |
| 1300 | static int __init omap_sham_mod_init(void) |
| 1301 | { |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 1302 | return platform_driver_register(&omap_sham_driver); |
| 1303 | } |
| 1304 | |
| 1305 | static void __exit omap_sham_mod_exit(void) |
| 1306 | { |
| 1307 | platform_driver_unregister(&omap_sham_driver); |
| 1308 | } |
| 1309 | |
| 1310 | module_init(omap_sham_mod_init); |
| 1311 | module_exit(omap_sham_mod_exit); |
| 1312 | |
| 1313 | MODULE_DESCRIPTION("OMAP SHA1/MD5 hw acceleration support."); |
| 1314 | MODULE_LICENSE("GPL v2"); |
| 1315 | MODULE_AUTHOR("Dmitry Kasatkin"); |