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> |
Mark A. Greer | 0d373d6 | 2012-12-21 10:04:08 -0700 | [diff] [blame] | 8 | * Copyright (c) 2011 Texas Instruments Incorporated |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 9 | * |
| 10 | * This program is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License version 2 as published |
| 12 | * by the Free Software Foundation. |
| 13 | * |
| 14 | * Some ideas are from old omap-sha1-md5.c driver. |
| 15 | */ |
| 16 | |
| 17 | #define pr_fmt(fmt) "%s: " fmt, __func__ |
| 18 | |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 19 | #include <linux/err.h> |
| 20 | #include <linux/device.h> |
| 21 | #include <linux/module.h> |
| 22 | #include <linux/init.h> |
| 23 | #include <linux/errno.h> |
| 24 | #include <linux/interrupt.h> |
| 25 | #include <linux/kernel.h> |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 26 | #include <linux/irq.h> |
| 27 | #include <linux/io.h> |
| 28 | #include <linux/platform_device.h> |
| 29 | #include <linux/scatterlist.h> |
| 30 | #include <linux/dma-mapping.h> |
Mark A. Greer | dfd061d | 2012-12-21 10:04:04 -0700 | [diff] [blame] | 31 | #include <linux/dmaengine.h> |
Mark A. Greer | b359f03 | 2012-12-21 10:04:02 -0700 | [diff] [blame] | 32 | #include <linux/pm_runtime.h> |
Mark A. Greer | 03feec9 | 2012-12-21 10:04:06 -0700 | [diff] [blame] | 33 | #include <linux/of.h> |
| 34 | #include <linux/of_device.h> |
| 35 | #include <linux/of_address.h> |
| 36 | #include <linux/of_irq.h> |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 37 | #include <linux/delay.h> |
| 38 | #include <linux/crypto.h> |
| 39 | #include <linux/cryptohash.h> |
| 40 | #include <crypto/scatterwalk.h> |
| 41 | #include <crypto/algapi.h> |
| 42 | #include <crypto/sha.h> |
| 43 | #include <crypto/hash.h> |
| 44 | #include <crypto/internal/hash.h> |
| 45 | |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 46 | #define MD5_DIGEST_SIZE 16 |
| 47 | |
Mark A. Greer | 0d373d6 | 2012-12-21 10:04:08 -0700 | [diff] [blame] | 48 | #define SHA_REG_IDIGEST(dd, x) ((dd)->pdata->idigest_ofs + ((x)*0x04)) |
| 49 | #define SHA_REG_DIN(dd, x) ((dd)->pdata->din_ofs + ((x) * 0x04)) |
| 50 | #define SHA_REG_DIGCNT(dd) ((dd)->pdata->digcnt_ofs) |
| 51 | |
Lokesh Vutla | eaef7e3 | 2013-07-26 12:29:14 +0530 | [diff] [blame] | 52 | #define SHA_REG_ODIGEST(dd, x) ((dd)->pdata->odigest_ofs + (x * 0x04)) |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 53 | |
| 54 | #define SHA_REG_CTRL 0x18 |
| 55 | #define SHA_REG_CTRL_LENGTH (0xFFFFFFFF << 5) |
| 56 | #define SHA_REG_CTRL_CLOSE_HASH (1 << 4) |
| 57 | #define SHA_REG_CTRL_ALGO_CONST (1 << 3) |
| 58 | #define SHA_REG_CTRL_ALGO (1 << 2) |
| 59 | #define SHA_REG_CTRL_INPUT_READY (1 << 1) |
| 60 | #define SHA_REG_CTRL_OUTPUT_READY (1 << 0) |
| 61 | |
Mark A. Greer | 0d373d6 | 2012-12-21 10:04:08 -0700 | [diff] [blame] | 62 | #define SHA_REG_REV(dd) ((dd)->pdata->rev_ofs) |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 63 | |
Mark A. Greer | 0d373d6 | 2012-12-21 10:04:08 -0700 | [diff] [blame] | 64 | #define SHA_REG_MASK(dd) ((dd)->pdata->mask_ofs) |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 65 | #define SHA_REG_MASK_DMA_EN (1 << 3) |
| 66 | #define SHA_REG_MASK_IT_EN (1 << 2) |
| 67 | #define SHA_REG_MASK_SOFTRESET (1 << 1) |
| 68 | #define SHA_REG_AUTOIDLE (1 << 0) |
| 69 | |
Mark A. Greer | 0d373d6 | 2012-12-21 10:04:08 -0700 | [diff] [blame] | 70 | #define SHA_REG_SYSSTATUS(dd) ((dd)->pdata->sysstatus_ofs) |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 71 | #define SHA_REG_SYSSTATUS_RESETDONE (1 << 0) |
| 72 | |
Lokesh Vutla | eaef7e3 | 2013-07-26 12:29:14 +0530 | [diff] [blame] | 73 | #define SHA_REG_MODE(dd) ((dd)->pdata->mode_ofs) |
Mark A. Greer | 0d373d6 | 2012-12-21 10:04:08 -0700 | [diff] [blame] | 74 | #define SHA_REG_MODE_HMAC_OUTER_HASH (1 << 7) |
| 75 | #define SHA_REG_MODE_HMAC_KEY_PROC (1 << 5) |
| 76 | #define SHA_REG_MODE_CLOSE_HASH (1 << 4) |
| 77 | #define SHA_REG_MODE_ALGO_CONSTANT (1 << 3) |
Mark A. Greer | 0d373d6 | 2012-12-21 10:04:08 -0700 | [diff] [blame] | 78 | |
Lokesh Vutla | eaef7e3 | 2013-07-26 12:29:14 +0530 | [diff] [blame] | 79 | #define SHA_REG_MODE_ALGO_MASK (7 << 0) |
| 80 | #define SHA_REG_MODE_ALGO_MD5_128 (0 << 1) |
| 81 | #define SHA_REG_MODE_ALGO_SHA1_160 (1 << 1) |
| 82 | #define SHA_REG_MODE_ALGO_SHA2_224 (2 << 1) |
| 83 | #define SHA_REG_MODE_ALGO_SHA2_256 (3 << 1) |
| 84 | #define SHA_REG_MODE_ALGO_SHA2_384 (1 << 0) |
| 85 | #define SHA_REG_MODE_ALGO_SHA2_512 (3 << 0) |
| 86 | |
| 87 | #define SHA_REG_LENGTH(dd) ((dd)->pdata->length_ofs) |
Mark A. Greer | 0d373d6 | 2012-12-21 10:04:08 -0700 | [diff] [blame] | 88 | |
| 89 | #define SHA_REG_IRQSTATUS 0x118 |
| 90 | #define SHA_REG_IRQSTATUS_CTX_RDY (1 << 3) |
| 91 | #define SHA_REG_IRQSTATUS_PARTHASH_RDY (1 << 2) |
| 92 | #define SHA_REG_IRQSTATUS_INPUT_RDY (1 << 1) |
| 93 | #define SHA_REG_IRQSTATUS_OUTPUT_RDY (1 << 0) |
| 94 | |
| 95 | #define SHA_REG_IRQENA 0x11C |
| 96 | #define SHA_REG_IRQENA_CTX_RDY (1 << 3) |
| 97 | #define SHA_REG_IRQENA_PARTHASH_RDY (1 << 2) |
| 98 | #define SHA_REG_IRQENA_INPUT_RDY (1 << 1) |
| 99 | #define SHA_REG_IRQENA_OUTPUT_RDY (1 << 0) |
| 100 | |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 101 | #define DEFAULT_TIMEOUT_INTERVAL HZ |
| 102 | |
Dmitry Kasatkin | ea1fd22 | 2011-06-02 21:10:05 +0300 | [diff] [blame] | 103 | /* mostly device flags */ |
| 104 | #define FLAGS_BUSY 0 |
| 105 | #define FLAGS_FINAL 1 |
| 106 | #define FLAGS_DMA_ACTIVE 2 |
| 107 | #define FLAGS_OUTPUT_READY 3 |
| 108 | #define FLAGS_INIT 4 |
| 109 | #define FLAGS_CPU 5 |
Dmitry Kasatkin | 6c63db8 | 2011-06-02 21:10:10 +0300 | [diff] [blame] | 110 | #define FLAGS_DMA_READY 6 |
Mark A. Greer | 0d373d6 | 2012-12-21 10:04:08 -0700 | [diff] [blame] | 111 | #define FLAGS_AUTO_XOR 7 |
| 112 | #define FLAGS_BE32_SHA1 8 |
Dmitry Kasatkin | ea1fd22 | 2011-06-02 21:10:05 +0300 | [diff] [blame] | 113 | /* context flags */ |
| 114 | #define FLAGS_FINUP 16 |
| 115 | #define FLAGS_SG 17 |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 116 | |
Mark A. Greer | 0d373d6 | 2012-12-21 10:04:08 -0700 | [diff] [blame] | 117 | #define FLAGS_MODE_SHIFT 18 |
Lokesh Vutla | eaef7e3 | 2013-07-26 12:29:14 +0530 | [diff] [blame] | 118 | #define FLAGS_MODE_MASK (SHA_REG_MODE_ALGO_MASK << FLAGS_MODE_SHIFT) |
| 119 | #define FLAGS_MODE_MD5 (SHA_REG_MODE_ALGO_MD5_128 << FLAGS_MODE_SHIFT) |
| 120 | #define FLAGS_MODE_SHA1 (SHA_REG_MODE_ALGO_SHA1_160 << FLAGS_MODE_SHIFT) |
| 121 | #define FLAGS_MODE_SHA224 (SHA_REG_MODE_ALGO_SHA2_224 << FLAGS_MODE_SHIFT) |
| 122 | #define FLAGS_MODE_SHA256 (SHA_REG_MODE_ALGO_SHA2_256 << FLAGS_MODE_SHIFT) |
| 123 | #define FLAGS_MODE_SHA384 (SHA_REG_MODE_ALGO_SHA2_384 << FLAGS_MODE_SHIFT) |
| 124 | #define FLAGS_MODE_SHA512 (SHA_REG_MODE_ALGO_SHA2_512 << FLAGS_MODE_SHIFT) |
| 125 | |
| 126 | #define FLAGS_HMAC 21 |
| 127 | #define FLAGS_ERROR 22 |
Mark A. Greer | 0d373d6 | 2012-12-21 10:04:08 -0700 | [diff] [blame] | 128 | |
| 129 | #define OP_UPDATE 1 |
| 130 | #define OP_FINAL 2 |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 131 | |
Dmitry Kasatkin | 798eed5d | 2010-11-19 16:04:26 +0200 | [diff] [blame] | 132 | #define OMAP_ALIGN_MASK (sizeof(u32)-1) |
| 133 | #define OMAP_ALIGNED __attribute__((aligned(sizeof(u32)))) |
| 134 | |
Mark A. Greer | 0d373d6 | 2012-12-21 10:04:08 -0700 | [diff] [blame] | 135 | #define BUFLEN PAGE_SIZE |
Dmitry Kasatkin | 798eed5d | 2010-11-19 16:04:26 +0200 | [diff] [blame] | 136 | |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 137 | struct omap_sham_dev; |
| 138 | |
| 139 | struct omap_sham_reqctx { |
| 140 | struct omap_sham_dev *dd; |
| 141 | unsigned long flags; |
| 142 | unsigned long op; |
| 143 | |
Lokesh Vutla | eaef7e3 | 2013-07-26 12:29:14 +0530 | [diff] [blame] | 144 | u8 digest[SHA512_DIGEST_SIZE] OMAP_ALIGNED; |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 145 | size_t digcnt; |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 146 | size_t bufcnt; |
| 147 | size_t buflen; |
| 148 | dma_addr_t dma_addr; |
| 149 | |
| 150 | /* walk state */ |
| 151 | struct scatterlist *sg; |
Mark A. Greer | dfd061d | 2012-12-21 10:04:04 -0700 | [diff] [blame] | 152 | struct scatterlist sgl; |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 153 | unsigned int offset; /* offset in current sg */ |
| 154 | unsigned int total; /* total request */ |
Dmitry Kasatkin | 798eed5d | 2010-11-19 16:04:26 +0200 | [diff] [blame] | 155 | |
| 156 | u8 buffer[0] OMAP_ALIGNED; |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 157 | }; |
| 158 | |
| 159 | struct omap_sham_hmac_ctx { |
| 160 | struct crypto_shash *shash; |
Lokesh Vutla | eaef7e3 | 2013-07-26 12:29:14 +0530 | [diff] [blame] | 161 | u8 ipad[SHA512_BLOCK_SIZE] OMAP_ALIGNED; |
| 162 | u8 opad[SHA512_BLOCK_SIZE] OMAP_ALIGNED; |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 163 | }; |
| 164 | |
| 165 | struct omap_sham_ctx { |
| 166 | struct omap_sham_dev *dd; |
| 167 | |
| 168 | unsigned long flags; |
| 169 | |
| 170 | /* fallback stuff */ |
| 171 | struct crypto_shash *fallback; |
| 172 | |
| 173 | struct omap_sham_hmac_ctx base[0]; |
| 174 | }; |
| 175 | |
| 176 | #define OMAP_SHAM_QUEUE_LENGTH 1 |
| 177 | |
Mark A. Greer | d20fb18 | 2012-12-21 10:04:09 -0700 | [diff] [blame] | 178 | struct omap_sham_algs_info { |
| 179 | struct ahash_alg *algs_list; |
| 180 | unsigned int size; |
| 181 | unsigned int registered; |
| 182 | }; |
| 183 | |
Mark A. Greer | 0d373d6 | 2012-12-21 10:04:08 -0700 | [diff] [blame] | 184 | struct omap_sham_pdata { |
Mark A. Greer | d20fb18 | 2012-12-21 10:04:09 -0700 | [diff] [blame] | 185 | struct omap_sham_algs_info *algs_info; |
| 186 | unsigned int algs_info_size; |
Mark A. Greer | 0d373d6 | 2012-12-21 10:04:08 -0700 | [diff] [blame] | 187 | unsigned long flags; |
| 188 | int digest_size; |
| 189 | |
| 190 | void (*copy_hash)(struct ahash_request *req, int out); |
| 191 | void (*write_ctrl)(struct omap_sham_dev *dd, size_t length, |
| 192 | int final, int dma); |
| 193 | void (*trigger)(struct omap_sham_dev *dd, size_t length); |
| 194 | int (*poll_irq)(struct omap_sham_dev *dd); |
| 195 | irqreturn_t (*intr_hdlr)(int irq, void *dev_id); |
| 196 | |
| 197 | u32 odigest_ofs; |
| 198 | u32 idigest_ofs; |
| 199 | u32 din_ofs; |
| 200 | u32 digcnt_ofs; |
| 201 | u32 rev_ofs; |
| 202 | u32 mask_ofs; |
| 203 | u32 sysstatus_ofs; |
Lokesh Vutla | eaef7e3 | 2013-07-26 12:29:14 +0530 | [diff] [blame] | 204 | u32 mode_ofs; |
| 205 | u32 length_ofs; |
Mark A. Greer | 0d373d6 | 2012-12-21 10:04:08 -0700 | [diff] [blame] | 206 | |
| 207 | u32 major_mask; |
| 208 | u32 major_shift; |
| 209 | u32 minor_mask; |
| 210 | u32 minor_shift; |
| 211 | }; |
| 212 | |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 213 | struct omap_sham_dev { |
| 214 | struct list_head list; |
| 215 | unsigned long phys_base; |
| 216 | struct device *dev; |
| 217 | void __iomem *io_base; |
| 218 | int irq; |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 219 | spinlock_t lock; |
Dmitry Kasatkin | 3e133c8 | 2010-11-19 16:04:24 +0200 | [diff] [blame] | 220 | int err; |
Mark A. Greer | dfd061d | 2012-12-21 10:04:04 -0700 | [diff] [blame] | 221 | struct dma_chan *dma_lch; |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 222 | struct tasklet_struct done_task; |
Lokesh Vutla | b8411cc | 2013-08-20 20:32:34 +0530 | [diff] [blame] | 223 | u8 polling_mode; |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 224 | |
| 225 | unsigned long flags; |
| 226 | struct crypto_queue queue; |
| 227 | struct ahash_request *req; |
Mark A. Greer | 0d373d6 | 2012-12-21 10:04:08 -0700 | [diff] [blame] | 228 | |
| 229 | const struct omap_sham_pdata *pdata; |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 230 | }; |
| 231 | |
| 232 | struct omap_sham_drv { |
| 233 | struct list_head dev_list; |
| 234 | spinlock_t lock; |
| 235 | unsigned long flags; |
| 236 | }; |
| 237 | |
| 238 | static struct omap_sham_drv sham = { |
| 239 | .dev_list = LIST_HEAD_INIT(sham.dev_list), |
| 240 | .lock = __SPIN_LOCK_UNLOCKED(sham.lock), |
| 241 | }; |
| 242 | |
| 243 | static inline u32 omap_sham_read(struct omap_sham_dev *dd, u32 offset) |
| 244 | { |
| 245 | return __raw_readl(dd->io_base + offset); |
| 246 | } |
| 247 | |
| 248 | static inline void omap_sham_write(struct omap_sham_dev *dd, |
| 249 | u32 offset, u32 value) |
| 250 | { |
| 251 | __raw_writel(value, dd->io_base + offset); |
| 252 | } |
| 253 | |
| 254 | static inline void omap_sham_write_mask(struct omap_sham_dev *dd, u32 address, |
| 255 | u32 value, u32 mask) |
| 256 | { |
| 257 | u32 val; |
| 258 | |
| 259 | val = omap_sham_read(dd, address); |
| 260 | val &= ~mask; |
| 261 | val |= value; |
| 262 | omap_sham_write(dd, address, val); |
| 263 | } |
| 264 | |
| 265 | static inline int omap_sham_wait(struct omap_sham_dev *dd, u32 offset, u32 bit) |
| 266 | { |
| 267 | unsigned long timeout = jiffies + DEFAULT_TIMEOUT_INTERVAL; |
| 268 | |
| 269 | while (!(omap_sham_read(dd, offset) & bit)) { |
| 270 | if (time_is_before_jiffies(timeout)) |
| 271 | return -ETIMEDOUT; |
| 272 | } |
| 273 | |
| 274 | return 0; |
| 275 | } |
| 276 | |
Mark A. Greer | 0d373d6 | 2012-12-21 10:04:08 -0700 | [diff] [blame] | 277 | static void omap_sham_copy_hash_omap2(struct ahash_request *req, int out) |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 278 | { |
| 279 | struct omap_sham_reqctx *ctx = ahash_request_ctx(req); |
Mark A. Greer | 0d373d6 | 2012-12-21 10:04:08 -0700 | [diff] [blame] | 280 | struct omap_sham_dev *dd = ctx->dd; |
Dmitry Kasatkin | 0c3cf4c | 2010-11-19 16:04:22 +0200 | [diff] [blame] | 281 | u32 *hash = (u32 *)ctx->digest; |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 282 | int i; |
| 283 | |
Mark A. Greer | 0d373d6 | 2012-12-21 10:04:08 -0700 | [diff] [blame] | 284 | for (i = 0; i < dd->pdata->digest_size / sizeof(u32); i++) { |
Dmitry Kasatkin | 3c8d758 | 2010-11-19 16:04:27 +0200 | [diff] [blame] | 285 | if (out) |
Mark A. Greer | 0d373d6 | 2012-12-21 10:04:08 -0700 | [diff] [blame] | 286 | hash[i] = omap_sham_read(dd, SHA_REG_IDIGEST(dd, i)); |
Dmitry Kasatkin | 3c8d758 | 2010-11-19 16:04:27 +0200 | [diff] [blame] | 287 | else |
Mark A. Greer | 0d373d6 | 2012-12-21 10:04:08 -0700 | [diff] [blame] | 288 | omap_sham_write(dd, SHA_REG_IDIGEST(dd, i), hash[i]); |
Dmitry Kasatkin | 3c8d758 | 2010-11-19 16:04:27 +0200 | [diff] [blame] | 289 | } |
| 290 | } |
| 291 | |
Mark A. Greer | 0d373d6 | 2012-12-21 10:04:08 -0700 | [diff] [blame] | 292 | static void omap_sham_copy_hash_omap4(struct ahash_request *req, int out) |
| 293 | { |
| 294 | struct omap_sham_reqctx *ctx = ahash_request_ctx(req); |
| 295 | struct omap_sham_dev *dd = ctx->dd; |
| 296 | int i; |
| 297 | |
| 298 | if (ctx->flags & BIT(FLAGS_HMAC)) { |
| 299 | struct crypto_ahash *tfm = crypto_ahash_reqtfm(dd->req); |
| 300 | struct omap_sham_ctx *tctx = crypto_ahash_ctx(tfm); |
| 301 | struct omap_sham_hmac_ctx *bctx = tctx->base; |
| 302 | u32 *opad = (u32 *)bctx->opad; |
| 303 | |
| 304 | for (i = 0; i < dd->pdata->digest_size / sizeof(u32); i++) { |
| 305 | if (out) |
| 306 | opad[i] = omap_sham_read(dd, |
Lokesh Vutla | eaef7e3 | 2013-07-26 12:29:14 +0530 | [diff] [blame] | 307 | SHA_REG_ODIGEST(dd, i)); |
Mark A. Greer | 0d373d6 | 2012-12-21 10:04:08 -0700 | [diff] [blame] | 308 | else |
Lokesh Vutla | eaef7e3 | 2013-07-26 12:29:14 +0530 | [diff] [blame] | 309 | omap_sham_write(dd, SHA_REG_ODIGEST(dd, i), |
Mark A. Greer | 0d373d6 | 2012-12-21 10:04:08 -0700 | [diff] [blame] | 310 | opad[i]); |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | omap_sham_copy_hash_omap2(req, out); |
| 315 | } |
| 316 | |
Dmitry Kasatkin | 3c8d758 | 2010-11-19 16:04:27 +0200 | [diff] [blame] | 317 | static void omap_sham_copy_ready_hash(struct ahash_request *req) |
| 318 | { |
| 319 | struct omap_sham_reqctx *ctx = ahash_request_ctx(req); |
| 320 | u32 *in = (u32 *)ctx->digest; |
| 321 | u32 *hash = (u32 *)req->result; |
Mark A. Greer | 0d373d6 | 2012-12-21 10:04:08 -0700 | [diff] [blame] | 322 | int i, d, big_endian = 0; |
Dmitry Kasatkin | 3c8d758 | 2010-11-19 16:04:27 +0200 | [diff] [blame] | 323 | |
| 324 | if (!hash) |
| 325 | return; |
| 326 | |
Mark A. Greer | 0d373d6 | 2012-12-21 10:04:08 -0700 | [diff] [blame] | 327 | switch (ctx->flags & FLAGS_MODE_MASK) { |
| 328 | case FLAGS_MODE_MD5: |
| 329 | d = MD5_DIGEST_SIZE / sizeof(u32); |
| 330 | break; |
| 331 | case FLAGS_MODE_SHA1: |
| 332 | /* OMAP2 SHA1 is big endian */ |
| 333 | if (test_bit(FLAGS_BE32_SHA1, &ctx->dd->flags)) |
| 334 | big_endian = 1; |
| 335 | d = SHA1_DIGEST_SIZE / sizeof(u32); |
| 336 | break; |
Mark A. Greer | d20fb18 | 2012-12-21 10:04:09 -0700 | [diff] [blame] | 337 | case FLAGS_MODE_SHA224: |
| 338 | d = SHA224_DIGEST_SIZE / sizeof(u32); |
| 339 | break; |
| 340 | case FLAGS_MODE_SHA256: |
| 341 | d = SHA256_DIGEST_SIZE / sizeof(u32); |
| 342 | break; |
Lokesh Vutla | eaef7e3 | 2013-07-26 12:29:14 +0530 | [diff] [blame] | 343 | case FLAGS_MODE_SHA384: |
| 344 | d = SHA384_DIGEST_SIZE / sizeof(u32); |
| 345 | break; |
| 346 | case FLAGS_MODE_SHA512: |
| 347 | d = SHA512_DIGEST_SIZE / sizeof(u32); |
| 348 | break; |
Mark A. Greer | 0d373d6 | 2012-12-21 10:04:08 -0700 | [diff] [blame] | 349 | default: |
| 350 | d = 0; |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 351 | } |
Mark A. Greer | 0d373d6 | 2012-12-21 10:04:08 -0700 | [diff] [blame] | 352 | |
| 353 | if (big_endian) |
| 354 | for (i = 0; i < d; i++) |
| 355 | hash[i] = be32_to_cpu(in[i]); |
| 356 | else |
| 357 | for (i = 0; i < d; i++) |
| 358 | hash[i] = le32_to_cpu(in[i]); |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 359 | } |
| 360 | |
Dmitry Kasatkin | 798eed5d | 2010-11-19 16:04:26 +0200 | [diff] [blame] | 361 | static int omap_sham_hw_init(struct omap_sham_dev *dd) |
| 362 | { |
Pali Rohár | 604c310 | 2015-03-08 11:01:01 +0100 | [diff] [blame] | 363 | int err; |
| 364 | |
| 365 | err = pm_runtime_get_sync(dd->dev); |
| 366 | if (err < 0) { |
| 367 | dev_err(dd->dev, "failed to get sync: %d\n", err); |
| 368 | return err; |
| 369 | } |
Dmitry Kasatkin | 798eed5d | 2010-11-19 16:04:26 +0200 | [diff] [blame] | 370 | |
Dmitry Kasatkin | a929cbe | 2011-06-02 21:10:06 +0300 | [diff] [blame] | 371 | if (!test_bit(FLAGS_INIT, &dd->flags)) { |
Dmitry Kasatkin | a929cbe | 2011-06-02 21:10:06 +0300 | [diff] [blame] | 372 | set_bit(FLAGS_INIT, &dd->flags); |
Dmitry Kasatkin | 798eed5d | 2010-11-19 16:04:26 +0200 | [diff] [blame] | 373 | dd->err = 0; |
| 374 | } |
| 375 | |
| 376 | return 0; |
| 377 | } |
| 378 | |
Mark A. Greer | 0d373d6 | 2012-12-21 10:04:08 -0700 | [diff] [blame] | 379 | static void omap_sham_write_ctrl_omap2(struct omap_sham_dev *dd, size_t length, |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 380 | int final, int dma) |
| 381 | { |
| 382 | struct omap_sham_reqctx *ctx = ahash_request_ctx(dd->req); |
| 383 | u32 val = length << 5, mask; |
| 384 | |
Dmitry Kasatkin | 798eed5d | 2010-11-19 16:04:26 +0200 | [diff] [blame] | 385 | if (likely(ctx->digcnt)) |
Mark A. Greer | 0d373d6 | 2012-12-21 10:04:08 -0700 | [diff] [blame] | 386 | omap_sham_write(dd, SHA_REG_DIGCNT(dd), ctx->digcnt); |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 387 | |
Mark A. Greer | 0d373d6 | 2012-12-21 10:04:08 -0700 | [diff] [blame] | 388 | omap_sham_write_mask(dd, SHA_REG_MASK(dd), |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 389 | SHA_REG_MASK_IT_EN | (dma ? SHA_REG_MASK_DMA_EN : 0), |
| 390 | SHA_REG_MASK_IT_EN | SHA_REG_MASK_DMA_EN); |
| 391 | /* |
| 392 | * Setting ALGO_CONST only for the first iteration |
| 393 | * and CLOSE_HASH only for the last one. |
| 394 | */ |
Mark A. Greer | 0d373d6 | 2012-12-21 10:04:08 -0700 | [diff] [blame] | 395 | if ((ctx->flags & FLAGS_MODE_MASK) == FLAGS_MODE_SHA1) |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 396 | val |= SHA_REG_CTRL_ALGO; |
| 397 | if (!ctx->digcnt) |
| 398 | val |= SHA_REG_CTRL_ALGO_CONST; |
| 399 | if (final) |
| 400 | val |= SHA_REG_CTRL_CLOSE_HASH; |
| 401 | |
| 402 | mask = SHA_REG_CTRL_ALGO_CONST | SHA_REG_CTRL_CLOSE_HASH | |
| 403 | SHA_REG_CTRL_ALGO | SHA_REG_CTRL_LENGTH; |
| 404 | |
| 405 | omap_sham_write_mask(dd, SHA_REG_CTRL, val, mask); |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 406 | } |
| 407 | |
Mark A. Greer | 0d373d6 | 2012-12-21 10:04:08 -0700 | [diff] [blame] | 408 | static void omap_sham_trigger_omap2(struct omap_sham_dev *dd, size_t length) |
| 409 | { |
| 410 | } |
| 411 | |
| 412 | static int omap_sham_poll_irq_omap2(struct omap_sham_dev *dd) |
| 413 | { |
| 414 | return omap_sham_wait(dd, SHA_REG_CTRL, SHA_REG_CTRL_INPUT_READY); |
| 415 | } |
| 416 | |
Lokesh Vutla | eaef7e3 | 2013-07-26 12:29:14 +0530 | [diff] [blame] | 417 | static int get_block_size(struct omap_sham_reqctx *ctx) |
| 418 | { |
| 419 | int d; |
| 420 | |
| 421 | switch (ctx->flags & FLAGS_MODE_MASK) { |
| 422 | case FLAGS_MODE_MD5: |
| 423 | case FLAGS_MODE_SHA1: |
| 424 | d = SHA1_BLOCK_SIZE; |
| 425 | break; |
| 426 | case FLAGS_MODE_SHA224: |
| 427 | case FLAGS_MODE_SHA256: |
| 428 | d = SHA256_BLOCK_SIZE; |
| 429 | break; |
| 430 | case FLAGS_MODE_SHA384: |
| 431 | case FLAGS_MODE_SHA512: |
| 432 | d = SHA512_BLOCK_SIZE; |
| 433 | break; |
| 434 | default: |
| 435 | d = 0; |
| 436 | } |
| 437 | |
| 438 | return d; |
| 439 | } |
| 440 | |
Mark A. Greer | 0d373d6 | 2012-12-21 10:04:08 -0700 | [diff] [blame] | 441 | static void omap_sham_write_n(struct omap_sham_dev *dd, u32 offset, |
| 442 | u32 *value, int count) |
| 443 | { |
| 444 | for (; count--; value++, offset += 4) |
| 445 | omap_sham_write(dd, offset, *value); |
| 446 | } |
| 447 | |
| 448 | static void omap_sham_write_ctrl_omap4(struct omap_sham_dev *dd, size_t length, |
| 449 | int final, int dma) |
| 450 | { |
| 451 | struct omap_sham_reqctx *ctx = ahash_request_ctx(dd->req); |
| 452 | u32 val, mask; |
| 453 | |
| 454 | /* |
| 455 | * Setting ALGO_CONST only for the first iteration and |
| 456 | * CLOSE_HASH only for the last one. Note that flags mode bits |
| 457 | * correspond to algorithm encoding in mode register. |
| 458 | */ |
Lokesh Vutla | eaef7e3 | 2013-07-26 12:29:14 +0530 | [diff] [blame] | 459 | val = (ctx->flags & FLAGS_MODE_MASK) >> (FLAGS_MODE_SHIFT); |
Mark A. Greer | 0d373d6 | 2012-12-21 10:04:08 -0700 | [diff] [blame] | 460 | if (!ctx->digcnt) { |
| 461 | struct crypto_ahash *tfm = crypto_ahash_reqtfm(dd->req); |
| 462 | struct omap_sham_ctx *tctx = crypto_ahash_ctx(tfm); |
| 463 | struct omap_sham_hmac_ctx *bctx = tctx->base; |
Lokesh Vutla | eaef7e3 | 2013-07-26 12:29:14 +0530 | [diff] [blame] | 464 | int bs, nr_dr; |
Mark A. Greer | 0d373d6 | 2012-12-21 10:04:08 -0700 | [diff] [blame] | 465 | |
| 466 | val |= SHA_REG_MODE_ALGO_CONSTANT; |
| 467 | |
| 468 | if (ctx->flags & BIT(FLAGS_HMAC)) { |
Lokesh Vutla | eaef7e3 | 2013-07-26 12:29:14 +0530 | [diff] [blame] | 469 | bs = get_block_size(ctx); |
| 470 | nr_dr = bs / (2 * sizeof(u32)); |
Mark A. Greer | 0d373d6 | 2012-12-21 10:04:08 -0700 | [diff] [blame] | 471 | val |= SHA_REG_MODE_HMAC_KEY_PROC; |
Lokesh Vutla | eaef7e3 | 2013-07-26 12:29:14 +0530 | [diff] [blame] | 472 | omap_sham_write_n(dd, SHA_REG_ODIGEST(dd, 0), |
| 473 | (u32 *)bctx->ipad, nr_dr); |
| 474 | omap_sham_write_n(dd, SHA_REG_IDIGEST(dd, 0), |
| 475 | (u32 *)bctx->ipad + nr_dr, nr_dr); |
| 476 | ctx->digcnt += bs; |
Mark A. Greer | 0d373d6 | 2012-12-21 10:04:08 -0700 | [diff] [blame] | 477 | } |
| 478 | } |
| 479 | |
| 480 | if (final) { |
| 481 | val |= SHA_REG_MODE_CLOSE_HASH; |
| 482 | |
| 483 | if (ctx->flags & BIT(FLAGS_HMAC)) |
| 484 | val |= SHA_REG_MODE_HMAC_OUTER_HASH; |
| 485 | } |
| 486 | |
| 487 | mask = SHA_REG_MODE_ALGO_CONSTANT | SHA_REG_MODE_CLOSE_HASH | |
| 488 | SHA_REG_MODE_ALGO_MASK | SHA_REG_MODE_HMAC_OUTER_HASH | |
| 489 | SHA_REG_MODE_HMAC_KEY_PROC; |
| 490 | |
| 491 | dev_dbg(dd->dev, "ctrl: %08x, flags: %08lx\n", val, ctx->flags); |
Lokesh Vutla | eaef7e3 | 2013-07-26 12:29:14 +0530 | [diff] [blame] | 492 | omap_sham_write_mask(dd, SHA_REG_MODE(dd), val, mask); |
Mark A. Greer | 0d373d6 | 2012-12-21 10:04:08 -0700 | [diff] [blame] | 493 | omap_sham_write(dd, SHA_REG_IRQENA, SHA_REG_IRQENA_OUTPUT_RDY); |
| 494 | omap_sham_write_mask(dd, SHA_REG_MASK(dd), |
| 495 | SHA_REG_MASK_IT_EN | |
| 496 | (dma ? SHA_REG_MASK_DMA_EN : 0), |
| 497 | SHA_REG_MASK_IT_EN | SHA_REG_MASK_DMA_EN); |
| 498 | } |
| 499 | |
| 500 | static void omap_sham_trigger_omap4(struct omap_sham_dev *dd, size_t length) |
| 501 | { |
Lokesh Vutla | eaef7e3 | 2013-07-26 12:29:14 +0530 | [diff] [blame] | 502 | omap_sham_write(dd, SHA_REG_LENGTH(dd), length); |
Mark A. Greer | 0d373d6 | 2012-12-21 10:04:08 -0700 | [diff] [blame] | 503 | } |
| 504 | |
| 505 | static int omap_sham_poll_irq_omap4(struct omap_sham_dev *dd) |
| 506 | { |
| 507 | return omap_sham_wait(dd, SHA_REG_IRQSTATUS, |
| 508 | SHA_REG_IRQSTATUS_INPUT_RDY); |
| 509 | } |
| 510 | |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 511 | static int omap_sham_xmit_cpu(struct omap_sham_dev *dd, const u8 *buf, |
| 512 | size_t length, int final) |
| 513 | { |
| 514 | struct omap_sham_reqctx *ctx = ahash_request_ctx(dd->req); |
Lokesh Vutla | b8411cc | 2013-08-20 20:32:34 +0530 | [diff] [blame] | 515 | int count, len32, bs32, offset = 0; |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 516 | const u32 *buffer = (const u32 *)buf; |
| 517 | |
| 518 | dev_dbg(dd->dev, "xmit_cpu: digcnt: %d, length: %d, final: %d\n", |
| 519 | ctx->digcnt, length, final); |
| 520 | |
Mark A. Greer | 0d373d6 | 2012-12-21 10:04:08 -0700 | [diff] [blame] | 521 | dd->pdata->write_ctrl(dd, length, final, 0); |
| 522 | dd->pdata->trigger(dd, length); |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 523 | |
Dmitry Kasatkin | 3e133c8 | 2010-11-19 16:04:24 +0200 | [diff] [blame] | 524 | /* should be non-zero before next lines to disable clocks later */ |
| 525 | ctx->digcnt += length; |
| 526 | |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 527 | if (final) |
Dmitry Kasatkin | ed3ea9a8 | 2011-06-02 21:10:07 +0300 | [diff] [blame] | 528 | set_bit(FLAGS_FINAL, &dd->flags); /* catch last interrupt */ |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 529 | |
Dmitry Kasatkin | 6c63db8 | 2011-06-02 21:10:10 +0300 | [diff] [blame] | 530 | set_bit(FLAGS_CPU, &dd->flags); |
| 531 | |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 532 | len32 = DIV_ROUND_UP(length, sizeof(u32)); |
Lokesh Vutla | b8411cc | 2013-08-20 20:32:34 +0530 | [diff] [blame] | 533 | bs32 = get_block_size(ctx) / sizeof(u32); |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 534 | |
Lokesh Vutla | b8411cc | 2013-08-20 20:32:34 +0530 | [diff] [blame] | 535 | while (len32) { |
| 536 | if (dd->pdata->poll_irq(dd)) |
| 537 | return -ETIMEDOUT; |
| 538 | |
| 539 | for (count = 0; count < min(len32, bs32); count++, offset++) |
| 540 | omap_sham_write(dd, SHA_REG_DIN(dd, count), |
| 541 | buffer[offset]); |
| 542 | len32 -= min(len32, bs32); |
| 543 | } |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 544 | |
| 545 | return -EINPROGRESS; |
| 546 | } |
| 547 | |
Mark A. Greer | dfd061d | 2012-12-21 10:04:04 -0700 | [diff] [blame] | 548 | static void omap_sham_dma_callback(void *param) |
| 549 | { |
| 550 | struct omap_sham_dev *dd = param; |
| 551 | |
| 552 | set_bit(FLAGS_DMA_READY, &dd->flags); |
| 553 | tasklet_schedule(&dd->done_task); |
| 554 | } |
Mark A. Greer | dfd061d | 2012-12-21 10:04:04 -0700 | [diff] [blame] | 555 | |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 556 | static int omap_sham_xmit_dma(struct omap_sham_dev *dd, dma_addr_t dma_addr, |
Mark A. Greer | dfd061d | 2012-12-21 10:04:04 -0700 | [diff] [blame] | 557 | size_t length, int final, int is_sg) |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 558 | { |
| 559 | struct omap_sham_reqctx *ctx = ahash_request_ctx(dd->req); |
Mark A. Greer | dfd061d | 2012-12-21 10:04:04 -0700 | [diff] [blame] | 560 | struct dma_async_tx_descriptor *tx; |
| 561 | struct dma_slave_config cfg; |
Lokesh Vutla | f5e4626 | 2013-08-20 20:32:35 +0530 | [diff] [blame] | 562 | int len32, ret, dma_min = get_block_size(ctx); |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 563 | |
| 564 | dev_dbg(dd->dev, "xmit_dma: digcnt: %d, length: %d, final: %d\n", |
| 565 | ctx->digcnt, length, final); |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 566 | |
Mark A. Greer | dfd061d | 2012-12-21 10:04:04 -0700 | [diff] [blame] | 567 | memset(&cfg, 0, sizeof(cfg)); |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 568 | |
Mark A. Greer | 0d373d6 | 2012-12-21 10:04:08 -0700 | [diff] [blame] | 569 | cfg.dst_addr = dd->phys_base + SHA_REG_DIN(dd, 0); |
Mark A. Greer | dfd061d | 2012-12-21 10:04:04 -0700 | [diff] [blame] | 570 | cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES; |
Lokesh Vutla | f5e4626 | 2013-08-20 20:32:35 +0530 | [diff] [blame] | 571 | cfg.dst_maxburst = dma_min / DMA_SLAVE_BUSWIDTH_4_BYTES; |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 572 | |
Mark A. Greer | dfd061d | 2012-12-21 10:04:04 -0700 | [diff] [blame] | 573 | ret = dmaengine_slave_config(dd->dma_lch, &cfg); |
| 574 | if (ret) { |
| 575 | pr_err("omap-sham: can't configure dmaengine slave: %d\n", ret); |
| 576 | return ret; |
| 577 | } |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 578 | |
Lokesh Vutla | f5e4626 | 2013-08-20 20:32:35 +0530 | [diff] [blame] | 579 | len32 = DIV_ROUND_UP(length, dma_min) * dma_min; |
Mark A. Greer | dfd061d | 2012-12-21 10:04:04 -0700 | [diff] [blame] | 580 | |
| 581 | if (is_sg) { |
| 582 | /* |
| 583 | * The SG entry passed in may not have the 'length' member |
| 584 | * set correctly so use a local SG entry (sgl) with the |
| 585 | * proper value for 'length' instead. If this is not done, |
| 586 | * the dmaengine may try to DMA the incorrect amount of data. |
| 587 | */ |
| 588 | sg_init_table(&ctx->sgl, 1); |
Christoph Hellwig | 89e2a84 | 2015-08-07 18:15:15 +0200 | [diff] [blame] | 589 | sg_assign_page(&ctx->sgl, sg_page(ctx->sg)); |
Mark A. Greer | dfd061d | 2012-12-21 10:04:04 -0700 | [diff] [blame] | 590 | ctx->sgl.offset = ctx->sg->offset; |
| 591 | sg_dma_len(&ctx->sgl) = len32; |
| 592 | sg_dma_address(&ctx->sgl) = sg_dma_address(ctx->sg); |
| 593 | |
| 594 | tx = dmaengine_prep_slave_sg(dd->dma_lch, &ctx->sgl, 1, |
| 595 | DMA_MEM_TO_DEV, DMA_PREP_INTERRUPT | DMA_CTRL_ACK); |
| 596 | } else { |
| 597 | tx = dmaengine_prep_slave_single(dd->dma_lch, dma_addr, len32, |
| 598 | DMA_MEM_TO_DEV, DMA_PREP_INTERRUPT | DMA_CTRL_ACK); |
| 599 | } |
| 600 | |
| 601 | if (!tx) { |
| 602 | dev_err(dd->dev, "prep_slave_sg/single() failed\n"); |
| 603 | return -EINVAL; |
| 604 | } |
| 605 | |
| 606 | tx->callback = omap_sham_dma_callback; |
| 607 | tx->callback_param = dd; |
Mark A. Greer | dfd061d | 2012-12-21 10:04:04 -0700 | [diff] [blame] | 608 | |
Mark A. Greer | 0d373d6 | 2012-12-21 10:04:08 -0700 | [diff] [blame] | 609 | dd->pdata->write_ctrl(dd, length, final, 1); |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 610 | |
| 611 | ctx->digcnt += length; |
| 612 | |
| 613 | if (final) |
Dmitry Kasatkin | ed3ea9a8 | 2011-06-02 21:10:07 +0300 | [diff] [blame] | 614 | set_bit(FLAGS_FINAL, &dd->flags); /* catch last interrupt */ |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 615 | |
Dmitry Kasatkin | a929cbe | 2011-06-02 21:10:06 +0300 | [diff] [blame] | 616 | set_bit(FLAGS_DMA_ACTIVE, &dd->flags); |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 617 | |
Mark A. Greer | dfd061d | 2012-12-21 10:04:04 -0700 | [diff] [blame] | 618 | dmaengine_submit(tx); |
| 619 | dma_async_issue_pending(dd->dma_lch); |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 620 | |
Mark A. Greer | 0d373d6 | 2012-12-21 10:04:08 -0700 | [diff] [blame] | 621 | dd->pdata->trigger(dd, length); |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 622 | |
| 623 | return -EINPROGRESS; |
| 624 | } |
| 625 | |
| 626 | static size_t omap_sham_append_buffer(struct omap_sham_reqctx *ctx, |
| 627 | const u8 *data, size_t length) |
| 628 | { |
| 629 | size_t count = min(length, ctx->buflen - ctx->bufcnt); |
| 630 | |
| 631 | count = min(count, ctx->total); |
| 632 | if (count <= 0) |
| 633 | return 0; |
| 634 | memcpy(ctx->buffer + ctx->bufcnt, data, count); |
| 635 | ctx->bufcnt += count; |
| 636 | |
| 637 | return count; |
| 638 | } |
| 639 | |
| 640 | static size_t omap_sham_append_sg(struct omap_sham_reqctx *ctx) |
| 641 | { |
| 642 | size_t count; |
Joel Fernandes | 26a0548 | 2014-03-07 10:28:46 -0600 | [diff] [blame] | 643 | const u8 *vaddr; |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 644 | |
| 645 | while (ctx->sg) { |
Joel Fernandes | 26a0548 | 2014-03-07 10:28:46 -0600 | [diff] [blame] | 646 | vaddr = kmap_atomic(sg_page(ctx->sg)); |
Vutla, Lokesh | 13cf394 | 2015-04-02 15:32:45 +0530 | [diff] [blame] | 647 | vaddr += ctx->sg->offset; |
Joel Fernandes | 26a0548 | 2014-03-07 10:28:46 -0600 | [diff] [blame] | 648 | |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 649 | count = omap_sham_append_buffer(ctx, |
Joel Fernandes | 26a0548 | 2014-03-07 10:28:46 -0600 | [diff] [blame] | 650 | vaddr + ctx->offset, |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 651 | ctx->sg->length - ctx->offset); |
Joel Fernandes | 26a0548 | 2014-03-07 10:28:46 -0600 | [diff] [blame] | 652 | |
| 653 | kunmap_atomic((void *)vaddr); |
| 654 | |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 655 | if (!count) |
| 656 | break; |
| 657 | ctx->offset += count; |
| 658 | ctx->total -= count; |
| 659 | if (ctx->offset == ctx->sg->length) { |
| 660 | ctx->sg = sg_next(ctx->sg); |
| 661 | if (ctx->sg) |
| 662 | ctx->offset = 0; |
| 663 | else |
| 664 | ctx->total = 0; |
| 665 | } |
| 666 | } |
| 667 | |
| 668 | return 0; |
| 669 | } |
| 670 | |
Dmitry Kasatkin | 798eed5d | 2010-11-19 16:04:26 +0200 | [diff] [blame] | 671 | static int omap_sham_xmit_dma_map(struct omap_sham_dev *dd, |
| 672 | struct omap_sham_reqctx *ctx, |
| 673 | size_t length, int final) |
| 674 | { |
Mark A. Greer | dfd061d | 2012-12-21 10:04:04 -0700 | [diff] [blame] | 675 | int ret; |
| 676 | |
Dmitry Kasatkin | 798eed5d | 2010-11-19 16:04:26 +0200 | [diff] [blame] | 677 | ctx->dma_addr = dma_map_single(dd->dev, ctx->buffer, ctx->buflen, |
| 678 | DMA_TO_DEVICE); |
| 679 | if (dma_mapping_error(dd->dev, ctx->dma_addr)) { |
| 680 | dev_err(dd->dev, "dma %u bytes error\n", ctx->buflen); |
| 681 | return -EINVAL; |
| 682 | } |
| 683 | |
Dmitry Kasatkin | ea1fd22 | 2011-06-02 21:10:05 +0300 | [diff] [blame] | 684 | ctx->flags &= ~BIT(FLAGS_SG); |
Dmitry Kasatkin | 887c883 | 2010-11-19 16:04:29 +0200 | [diff] [blame] | 685 | |
Mark A. Greer | dfd061d | 2012-12-21 10:04:04 -0700 | [diff] [blame] | 686 | ret = omap_sham_xmit_dma(dd, ctx->dma_addr, length, final, 0); |
Mark A. Greer | 0d373d6 | 2012-12-21 10:04:08 -0700 | [diff] [blame] | 687 | if (ret != -EINPROGRESS) |
Mark A. Greer | dfd061d | 2012-12-21 10:04:04 -0700 | [diff] [blame] | 688 | dma_unmap_single(dd->dev, ctx->dma_addr, ctx->buflen, |
| 689 | DMA_TO_DEVICE); |
| 690 | |
| 691 | return ret; |
Dmitry Kasatkin | 798eed5d | 2010-11-19 16:04:26 +0200 | [diff] [blame] | 692 | } |
| 693 | |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 694 | static int omap_sham_update_dma_slow(struct omap_sham_dev *dd) |
| 695 | { |
| 696 | struct omap_sham_reqctx *ctx = ahash_request_ctx(dd->req); |
| 697 | unsigned int final; |
| 698 | size_t count; |
| 699 | |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 700 | omap_sham_append_sg(ctx); |
| 701 | |
Dmitry Kasatkin | ea1fd22 | 2011-06-02 21:10:05 +0300 | [diff] [blame] | 702 | final = (ctx->flags & BIT(FLAGS_FINUP)) && !ctx->total; |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 703 | |
| 704 | dev_dbg(dd->dev, "slow: bufcnt: %u, digcnt: %d, final: %d\n", |
| 705 | ctx->bufcnt, ctx->digcnt, final); |
| 706 | |
| 707 | if (final || (ctx->bufcnt == ctx->buflen && ctx->total)) { |
| 708 | count = ctx->bufcnt; |
| 709 | ctx->bufcnt = 0; |
Dmitry Kasatkin | 798eed5d | 2010-11-19 16:04:26 +0200 | [diff] [blame] | 710 | return omap_sham_xmit_dma_map(dd, ctx, count, final); |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 711 | } |
| 712 | |
| 713 | return 0; |
| 714 | } |
| 715 | |
Dmitry Kasatkin | 887c883 | 2010-11-19 16:04:29 +0200 | [diff] [blame] | 716 | /* Start address alignment */ |
| 717 | #define SG_AA(sg) (IS_ALIGNED(sg->offset, sizeof(u32))) |
| 718 | /* SHA1 block size alignment */ |
Lokesh Vutla | eaef7e3 | 2013-07-26 12:29:14 +0530 | [diff] [blame] | 719 | #define SG_SA(sg, bs) (IS_ALIGNED(sg->length, bs)) |
Dmitry Kasatkin | 887c883 | 2010-11-19 16:04:29 +0200 | [diff] [blame] | 720 | |
| 721 | static int omap_sham_update_dma_start(struct omap_sham_dev *dd) |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 722 | { |
| 723 | struct omap_sham_reqctx *ctx = ahash_request_ctx(dd->req); |
Dmitry Kasatkin | 887c883 | 2010-11-19 16:04:29 +0200 | [diff] [blame] | 724 | unsigned int length, final, tail; |
| 725 | struct scatterlist *sg; |
Lokesh Vutla | eaef7e3 | 2013-07-26 12:29:14 +0530 | [diff] [blame] | 726 | int ret, bs; |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 727 | |
Dmitry Kasatkin | 887c883 | 2010-11-19 16:04:29 +0200 | [diff] [blame] | 728 | if (!ctx->total) |
| 729 | return 0; |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 730 | |
Dmitry Kasatkin | 887c883 | 2010-11-19 16:04:29 +0200 | [diff] [blame] | 731 | if (ctx->bufcnt || ctx->offset) |
| 732 | return omap_sham_update_dma_slow(dd); |
| 733 | |
Mark A. Greer | dfd061d | 2012-12-21 10:04:04 -0700 | [diff] [blame] | 734 | /* |
| 735 | * Don't use the sg interface when the transfer size is less |
| 736 | * than the number of elements in a DMA frame. Otherwise, |
| 737 | * the dmaengine infrastructure will calculate that it needs |
| 738 | * to transfer 0 frames which ultimately fails. |
| 739 | */ |
Lokesh Vutla | f5e4626 | 2013-08-20 20:32:35 +0530 | [diff] [blame] | 740 | if (ctx->total < get_block_size(ctx)) |
Mark A. Greer | dfd061d | 2012-12-21 10:04:04 -0700 | [diff] [blame] | 741 | return omap_sham_update_dma_slow(dd); |
Mark A. Greer | dfd061d | 2012-12-21 10:04:04 -0700 | [diff] [blame] | 742 | |
Dmitry Kasatkin | 887c883 | 2010-11-19 16:04:29 +0200 | [diff] [blame] | 743 | dev_dbg(dd->dev, "fast: digcnt: %d, bufcnt: %u, total: %u\n", |
| 744 | ctx->digcnt, ctx->bufcnt, ctx->total); |
| 745 | |
| 746 | sg = ctx->sg; |
Lokesh Vutla | eaef7e3 | 2013-07-26 12:29:14 +0530 | [diff] [blame] | 747 | bs = get_block_size(ctx); |
Dmitry Kasatkin | 887c883 | 2010-11-19 16:04:29 +0200 | [diff] [blame] | 748 | |
| 749 | if (!SG_AA(sg)) |
| 750 | return omap_sham_update_dma_slow(dd); |
| 751 | |
Lokesh Vutla | eaef7e3 | 2013-07-26 12:29:14 +0530 | [diff] [blame] | 752 | if (!sg_is_last(sg) && !SG_SA(sg, bs)) |
| 753 | /* size is not BLOCK_SIZE aligned */ |
Dmitry Kasatkin | 887c883 | 2010-11-19 16:04:29 +0200 | [diff] [blame] | 754 | return omap_sham_update_dma_slow(dd); |
| 755 | |
| 756 | length = min(ctx->total, sg->length); |
| 757 | |
| 758 | if (sg_is_last(sg)) { |
Dmitry Kasatkin | ea1fd22 | 2011-06-02 21:10:05 +0300 | [diff] [blame] | 759 | if (!(ctx->flags & BIT(FLAGS_FINUP))) { |
Lokesh Vutla | eaef7e3 | 2013-07-26 12:29:14 +0530 | [diff] [blame] | 760 | /* not last sg must be BLOCK_SIZE aligned */ |
| 761 | tail = length & (bs - 1); |
Dmitry Kasatkin | 887c883 | 2010-11-19 16:04:29 +0200 | [diff] [blame] | 762 | /* without finup() we need one block to close hash */ |
| 763 | if (!tail) |
Lokesh Vutla | eaef7e3 | 2013-07-26 12:29:14 +0530 | [diff] [blame] | 764 | tail = bs; |
Dmitry Kasatkin | 887c883 | 2010-11-19 16:04:29 +0200 | [diff] [blame] | 765 | length -= tail; |
| 766 | } |
| 767 | } |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 768 | |
| 769 | if (!dma_map_sg(dd->dev, ctx->sg, 1, DMA_TO_DEVICE)) { |
| 770 | dev_err(dd->dev, "dma_map_sg error\n"); |
| 771 | return -EINVAL; |
| 772 | } |
| 773 | |
Dmitry Kasatkin | ea1fd22 | 2011-06-02 21:10:05 +0300 | [diff] [blame] | 774 | ctx->flags |= BIT(FLAGS_SG); |
Dmitry Kasatkin | 887c883 | 2010-11-19 16:04:29 +0200 | [diff] [blame] | 775 | |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 776 | ctx->total -= length; |
Dmitry Kasatkin | 887c883 | 2010-11-19 16:04:29 +0200 | [diff] [blame] | 777 | ctx->offset = length; /* offset where to start slow */ |
| 778 | |
Dmitry Kasatkin | ea1fd22 | 2011-06-02 21:10:05 +0300 | [diff] [blame] | 779 | final = (ctx->flags & BIT(FLAGS_FINUP)) && !ctx->total; |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 780 | |
Mark A. Greer | dfd061d | 2012-12-21 10:04:04 -0700 | [diff] [blame] | 781 | ret = omap_sham_xmit_dma(dd, sg_dma_address(ctx->sg), length, final, 1); |
Mark A. Greer | 0d373d6 | 2012-12-21 10:04:08 -0700 | [diff] [blame] | 782 | if (ret != -EINPROGRESS) |
Mark A. Greer | dfd061d | 2012-12-21 10:04:04 -0700 | [diff] [blame] | 783 | dma_unmap_sg(dd->dev, ctx->sg, 1, DMA_TO_DEVICE); |
| 784 | |
| 785 | return ret; |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 786 | } |
| 787 | |
| 788 | static int omap_sham_update_cpu(struct omap_sham_dev *dd) |
| 789 | { |
| 790 | struct omap_sham_reqctx *ctx = ahash_request_ctx(dd->req); |
Lokesh Vutla | b8411cc | 2013-08-20 20:32:34 +0530 | [diff] [blame] | 791 | int bufcnt, final; |
| 792 | |
| 793 | if (!ctx->total) |
| 794 | return 0; |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 795 | |
| 796 | omap_sham_append_sg(ctx); |
Lokesh Vutla | b8411cc | 2013-08-20 20:32:34 +0530 | [diff] [blame] | 797 | |
| 798 | final = (ctx->flags & BIT(FLAGS_FINUP)) && !ctx->total; |
| 799 | |
| 800 | dev_dbg(dd->dev, "cpu: bufcnt: %u, digcnt: %d, final: %d\n", |
| 801 | ctx->bufcnt, ctx->digcnt, final); |
| 802 | |
Lokesh Vutla | acef7b0 | 2013-12-18 19:03:33 +0530 | [diff] [blame] | 803 | if (final || (ctx->bufcnt == ctx->buflen && ctx->total)) { |
| 804 | bufcnt = ctx->bufcnt; |
| 805 | ctx->bufcnt = 0; |
| 806 | return omap_sham_xmit_cpu(dd, ctx->buffer, bufcnt, final); |
| 807 | } |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 808 | |
Lokesh Vutla | acef7b0 | 2013-12-18 19:03:33 +0530 | [diff] [blame] | 809 | return 0; |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 810 | } |
| 811 | |
| 812 | static int omap_sham_update_dma_stop(struct omap_sham_dev *dd) |
| 813 | { |
| 814 | struct omap_sham_reqctx *ctx = ahash_request_ctx(dd->req); |
| 815 | |
Mark A. Greer | dfd061d | 2012-12-21 10:04:04 -0700 | [diff] [blame] | 816 | dmaengine_terminate_all(dd->dma_lch); |
Mark A. Greer | dfd061d | 2012-12-21 10:04:04 -0700 | [diff] [blame] | 817 | |
Dmitry Kasatkin | ea1fd22 | 2011-06-02 21:10:05 +0300 | [diff] [blame] | 818 | if (ctx->flags & BIT(FLAGS_SG)) { |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 819 | dma_unmap_sg(dd->dev, ctx->sg, 1, DMA_TO_DEVICE); |
Dmitry Kasatkin | 887c883 | 2010-11-19 16:04:29 +0200 | [diff] [blame] | 820 | if (ctx->sg->length == ctx->offset) { |
| 821 | ctx->sg = sg_next(ctx->sg); |
| 822 | if (ctx->sg) |
| 823 | ctx->offset = 0; |
| 824 | } |
| 825 | } else { |
Dmitry Kasatkin | 798eed5d | 2010-11-19 16:04:26 +0200 | [diff] [blame] | 826 | dma_unmap_single(dd->dev, ctx->dma_addr, ctx->buflen, |
| 827 | DMA_TO_DEVICE); |
Dmitry Kasatkin | 887c883 | 2010-11-19 16:04:29 +0200 | [diff] [blame] | 828 | } |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 829 | |
| 830 | return 0; |
| 831 | } |
| 832 | |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 833 | static int omap_sham_init(struct ahash_request *req) |
| 834 | { |
| 835 | struct crypto_ahash *tfm = crypto_ahash_reqtfm(req); |
| 836 | struct omap_sham_ctx *tctx = crypto_ahash_ctx(tfm); |
| 837 | struct omap_sham_reqctx *ctx = ahash_request_ctx(req); |
| 838 | struct omap_sham_dev *dd = NULL, *tmp; |
Lokesh Vutla | eaef7e3 | 2013-07-26 12:29:14 +0530 | [diff] [blame] | 839 | int bs = 0; |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 840 | |
| 841 | spin_lock_bh(&sham.lock); |
| 842 | if (!tctx->dd) { |
| 843 | list_for_each_entry(tmp, &sham.dev_list, list) { |
| 844 | dd = tmp; |
| 845 | break; |
| 846 | } |
| 847 | tctx->dd = dd; |
| 848 | } else { |
| 849 | dd = tctx->dd; |
| 850 | } |
| 851 | spin_unlock_bh(&sham.lock); |
| 852 | |
| 853 | ctx->dd = dd; |
| 854 | |
| 855 | ctx->flags = 0; |
| 856 | |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 857 | dev_dbg(dd->dev, "init: digest size: %d\n", |
| 858 | crypto_ahash_digestsize(tfm)); |
| 859 | |
Mark A. Greer | 0d373d6 | 2012-12-21 10:04:08 -0700 | [diff] [blame] | 860 | switch (crypto_ahash_digestsize(tfm)) { |
| 861 | case MD5_DIGEST_SIZE: |
| 862 | ctx->flags |= FLAGS_MODE_MD5; |
Lokesh Vutla | eaef7e3 | 2013-07-26 12:29:14 +0530 | [diff] [blame] | 863 | bs = SHA1_BLOCK_SIZE; |
Mark A. Greer | 0d373d6 | 2012-12-21 10:04:08 -0700 | [diff] [blame] | 864 | break; |
| 865 | case SHA1_DIGEST_SIZE: |
| 866 | ctx->flags |= FLAGS_MODE_SHA1; |
Lokesh Vutla | eaef7e3 | 2013-07-26 12:29:14 +0530 | [diff] [blame] | 867 | bs = SHA1_BLOCK_SIZE; |
Mark A. Greer | 0d373d6 | 2012-12-21 10:04:08 -0700 | [diff] [blame] | 868 | break; |
Mark A. Greer | d20fb18 | 2012-12-21 10:04:09 -0700 | [diff] [blame] | 869 | case SHA224_DIGEST_SIZE: |
| 870 | ctx->flags |= FLAGS_MODE_SHA224; |
Lokesh Vutla | eaef7e3 | 2013-07-26 12:29:14 +0530 | [diff] [blame] | 871 | bs = SHA224_BLOCK_SIZE; |
Mark A. Greer | d20fb18 | 2012-12-21 10:04:09 -0700 | [diff] [blame] | 872 | break; |
| 873 | case SHA256_DIGEST_SIZE: |
| 874 | ctx->flags |= FLAGS_MODE_SHA256; |
Lokesh Vutla | eaef7e3 | 2013-07-26 12:29:14 +0530 | [diff] [blame] | 875 | bs = SHA256_BLOCK_SIZE; |
| 876 | break; |
| 877 | case SHA384_DIGEST_SIZE: |
| 878 | ctx->flags |= FLAGS_MODE_SHA384; |
| 879 | bs = SHA384_BLOCK_SIZE; |
| 880 | break; |
| 881 | case SHA512_DIGEST_SIZE: |
| 882 | ctx->flags |= FLAGS_MODE_SHA512; |
| 883 | bs = SHA512_BLOCK_SIZE; |
Mark A. Greer | d20fb18 | 2012-12-21 10:04:09 -0700 | [diff] [blame] | 884 | break; |
Mark A. Greer | 0d373d6 | 2012-12-21 10:04:08 -0700 | [diff] [blame] | 885 | } |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 886 | |
| 887 | ctx->bufcnt = 0; |
| 888 | ctx->digcnt = 0; |
Dmitry Kasatkin | 798eed5d | 2010-11-19 16:04:26 +0200 | [diff] [blame] | 889 | ctx->buflen = BUFLEN; |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 890 | |
Dmitry Kasatkin | ea1fd22 | 2011-06-02 21:10:05 +0300 | [diff] [blame] | 891 | if (tctx->flags & BIT(FLAGS_HMAC)) { |
Mark A. Greer | 0d373d6 | 2012-12-21 10:04:08 -0700 | [diff] [blame] | 892 | if (!test_bit(FLAGS_AUTO_XOR, &dd->flags)) { |
| 893 | struct omap_sham_hmac_ctx *bctx = tctx->base; |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 894 | |
Lokesh Vutla | eaef7e3 | 2013-07-26 12:29:14 +0530 | [diff] [blame] | 895 | memcpy(ctx->buffer, bctx->ipad, bs); |
| 896 | ctx->bufcnt = bs; |
Mark A. Greer | 0d373d6 | 2012-12-21 10:04:08 -0700 | [diff] [blame] | 897 | } |
| 898 | |
Dmitry Kasatkin | ea1fd22 | 2011-06-02 21:10:05 +0300 | [diff] [blame] | 899 | ctx->flags |= BIT(FLAGS_HMAC); |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 900 | } |
| 901 | |
| 902 | return 0; |
| 903 | |
| 904 | } |
| 905 | |
| 906 | static int omap_sham_update_req(struct omap_sham_dev *dd) |
| 907 | { |
| 908 | struct ahash_request *req = dd->req; |
| 909 | struct omap_sham_reqctx *ctx = ahash_request_ctx(req); |
| 910 | int err; |
| 911 | |
| 912 | 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] | 913 | ctx->total, ctx->digcnt, (ctx->flags & BIT(FLAGS_FINUP)) != 0); |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 914 | |
Dmitry Kasatkin | ea1fd22 | 2011-06-02 21:10:05 +0300 | [diff] [blame] | 915 | if (ctx->flags & BIT(FLAGS_CPU)) |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 916 | err = omap_sham_update_cpu(dd); |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 917 | else |
Dmitry Kasatkin | 887c883 | 2010-11-19 16:04:29 +0200 | [diff] [blame] | 918 | err = omap_sham_update_dma_start(dd); |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 919 | |
| 920 | /* wait for dma completion before can take more data */ |
| 921 | dev_dbg(dd->dev, "update: err: %d, digcnt: %d\n", err, ctx->digcnt); |
| 922 | |
| 923 | return err; |
| 924 | } |
| 925 | |
| 926 | static int omap_sham_final_req(struct omap_sham_dev *dd) |
| 927 | { |
| 928 | struct ahash_request *req = dd->req; |
| 929 | struct omap_sham_reqctx *ctx = ahash_request_ctx(req); |
| 930 | int err = 0, use_dma = 1; |
| 931 | |
Lokesh Vutla | b8411cc | 2013-08-20 20:32:34 +0530 | [diff] [blame] | 932 | if ((ctx->bufcnt <= get_block_size(ctx)) || dd->polling_mode) |
| 933 | /* |
| 934 | * faster to handle last block with cpu or |
| 935 | * use cpu when dma is not present. |
| 936 | */ |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 937 | use_dma = 0; |
| 938 | |
| 939 | if (use_dma) |
Dmitry Kasatkin | 798eed5d | 2010-11-19 16:04:26 +0200 | [diff] [blame] | 940 | err = omap_sham_xmit_dma_map(dd, ctx, ctx->bufcnt, 1); |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 941 | else |
| 942 | err = omap_sham_xmit_cpu(dd, ctx->buffer, ctx->bufcnt, 1); |
| 943 | |
| 944 | ctx->bufcnt = 0; |
| 945 | |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 946 | dev_dbg(dd->dev, "final_req: err: %d\n", err); |
| 947 | |
| 948 | return err; |
| 949 | } |
| 950 | |
Dmitry Kasatkin | bf36275 | 2011-04-20 13:34:58 +0300 | [diff] [blame] | 951 | static int omap_sham_finish_hmac(struct ahash_request *req) |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 952 | { |
| 953 | struct omap_sham_ctx *tctx = crypto_tfm_ctx(req->base.tfm); |
| 954 | struct omap_sham_hmac_ctx *bctx = tctx->base; |
| 955 | int bs = crypto_shash_blocksize(bctx->shash); |
| 956 | int ds = crypto_shash_digestsize(bctx->shash); |
Behan Webster | 7bc53c3 | 2014-04-04 18:18:00 -0300 | [diff] [blame] | 957 | SHASH_DESC_ON_STACK(shash, bctx->shash); |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 958 | |
Behan Webster | 7bc53c3 | 2014-04-04 18:18:00 -0300 | [diff] [blame] | 959 | shash->tfm = bctx->shash; |
| 960 | shash->flags = 0; /* not CRYPTO_TFM_REQ_MAY_SLEEP */ |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 961 | |
Behan Webster | 7bc53c3 | 2014-04-04 18:18:00 -0300 | [diff] [blame] | 962 | return crypto_shash_init(shash) ?: |
| 963 | crypto_shash_update(shash, bctx->opad, bs) ?: |
| 964 | crypto_shash_finup(shash, req->result, ds, req->result); |
Dmitry Kasatkin | bf36275 | 2011-04-20 13:34:58 +0300 | [diff] [blame] | 965 | } |
| 966 | |
| 967 | static int omap_sham_finish(struct ahash_request *req) |
| 968 | { |
| 969 | struct omap_sham_reqctx *ctx = ahash_request_ctx(req); |
| 970 | struct omap_sham_dev *dd = ctx->dd; |
| 971 | int err = 0; |
| 972 | |
| 973 | if (ctx->digcnt) { |
| 974 | omap_sham_copy_ready_hash(req); |
Mark A. Greer | 0d373d6 | 2012-12-21 10:04:08 -0700 | [diff] [blame] | 975 | if ((ctx->flags & BIT(FLAGS_HMAC)) && |
| 976 | !test_bit(FLAGS_AUTO_XOR, &dd->flags)) |
Dmitry Kasatkin | bf36275 | 2011-04-20 13:34:58 +0300 | [diff] [blame] | 977 | err = omap_sham_finish_hmac(req); |
| 978 | } |
| 979 | |
| 980 | dev_dbg(dd->dev, "digcnt: %d, bufcnt: %d\n", ctx->digcnt, ctx->bufcnt); |
| 981 | |
| 982 | return err; |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 983 | } |
| 984 | |
| 985 | static void omap_sham_finish_req(struct ahash_request *req, int err) |
| 986 | { |
| 987 | struct omap_sham_reqctx *ctx = ahash_request_ctx(req); |
Dmitry Kasatkin | 798eed5d | 2010-11-19 16:04:26 +0200 | [diff] [blame] | 988 | struct omap_sham_dev *dd = ctx->dd; |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 989 | |
| 990 | if (!err) { |
Mark A. Greer | 0d373d6 | 2012-12-21 10:04:08 -0700 | [diff] [blame] | 991 | dd->pdata->copy_hash(req, 1); |
Dmitry Kasatkin | ed3ea9a8 | 2011-06-02 21:10:07 +0300 | [diff] [blame] | 992 | if (test_bit(FLAGS_FINAL, &dd->flags)) |
Dmitry Kasatkin | bf36275 | 2011-04-20 13:34:58 +0300 | [diff] [blame] | 993 | err = omap_sham_finish(req); |
Dmitry Kasatkin | 3e133c8 | 2010-11-19 16:04:24 +0200 | [diff] [blame] | 994 | } else { |
Dmitry Kasatkin | ea1fd22 | 2011-06-02 21:10:05 +0300 | [diff] [blame] | 995 | ctx->flags |= BIT(FLAGS_ERROR); |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 996 | } |
| 997 | |
Dmitry Kasatkin | 0efd4d8 | 2011-06-02 21:10:12 +0300 | [diff] [blame] | 998 | /* atomic operation is not needed here */ |
| 999 | dd->flags &= ~(BIT(FLAGS_BUSY) | BIT(FLAGS_FINAL) | BIT(FLAGS_CPU) | |
| 1000 | BIT(FLAGS_DMA_READY) | BIT(FLAGS_OUTPUT_READY)); |
Mark A. Greer | b359f03 | 2012-12-21 10:04:02 -0700 | [diff] [blame] | 1001 | |
Joel A Fernandes | e68af48 | 2013-02-26 10:04:31 -0600 | [diff] [blame] | 1002 | pm_runtime_put(dd->dev); |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 1003 | |
| 1004 | if (req->base.complete) |
| 1005 | req->base.complete(&req->base, err); |
Dmitry Kasatkin | 6cb3ffe | 2011-06-02 21:10:09 +0300 | [diff] [blame] | 1006 | |
| 1007 | /* handle new request */ |
| 1008 | tasklet_schedule(&dd->done_task); |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 1009 | } |
| 1010 | |
Dmitry Kasatkin | a5d8723 | 2010-11-19 16:04:25 +0200 | [diff] [blame] | 1011 | static int omap_sham_handle_queue(struct omap_sham_dev *dd, |
| 1012 | struct ahash_request *req) |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 1013 | { |
Dmitry Kasatkin | 6c39d11 | 2010-12-29 21:52:04 +1100 | [diff] [blame] | 1014 | struct crypto_async_request *async_req, *backlog; |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 1015 | struct omap_sham_reqctx *ctx; |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 1016 | unsigned long flags; |
Dmitry Kasatkin | a5d8723 | 2010-11-19 16:04:25 +0200 | [diff] [blame] | 1017 | int err = 0, ret = 0; |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 1018 | |
| 1019 | spin_lock_irqsave(&dd->lock, flags); |
Dmitry Kasatkin | a5d8723 | 2010-11-19 16:04:25 +0200 | [diff] [blame] | 1020 | if (req) |
| 1021 | ret = ahash_enqueue_request(&dd->queue, req); |
Dmitry Kasatkin | a929cbe | 2011-06-02 21:10:06 +0300 | [diff] [blame] | 1022 | if (test_bit(FLAGS_BUSY, &dd->flags)) { |
Dmitry Kasatkin | a5d8723 | 2010-11-19 16:04:25 +0200 | [diff] [blame] | 1023 | spin_unlock_irqrestore(&dd->lock, flags); |
| 1024 | return ret; |
| 1025 | } |
Dmitry Kasatkin | 6c39d11 | 2010-12-29 21:52:04 +1100 | [diff] [blame] | 1026 | backlog = crypto_get_backlog(&dd->queue); |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 1027 | async_req = crypto_dequeue_request(&dd->queue); |
Dmitry Kasatkin | 6c39d11 | 2010-12-29 21:52:04 +1100 | [diff] [blame] | 1028 | if (async_req) |
Dmitry Kasatkin | a929cbe | 2011-06-02 21:10:06 +0300 | [diff] [blame] | 1029 | set_bit(FLAGS_BUSY, &dd->flags); |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 1030 | spin_unlock_irqrestore(&dd->lock, flags); |
| 1031 | |
| 1032 | if (!async_req) |
Dmitry Kasatkin | a5d8723 | 2010-11-19 16:04:25 +0200 | [diff] [blame] | 1033 | return ret; |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 1034 | |
| 1035 | if (backlog) |
| 1036 | backlog->complete(backlog, -EINPROGRESS); |
| 1037 | |
| 1038 | req = ahash_request_cast(async_req); |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 1039 | dd->req = req; |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 1040 | ctx = ahash_request_ctx(req); |
| 1041 | |
| 1042 | dev_dbg(dd->dev, "handling new req, op: %lu, nbytes: %d\n", |
| 1043 | ctx->op, req->nbytes); |
| 1044 | |
Dmitry Kasatkin | 798eed5d | 2010-11-19 16:04:26 +0200 | [diff] [blame] | 1045 | err = omap_sham_hw_init(dd); |
| 1046 | if (err) |
| 1047 | goto err1; |
| 1048 | |
Dmitry Kasatkin | 798eed5d | 2010-11-19 16:04:26 +0200 | [diff] [blame] | 1049 | if (ctx->digcnt) |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 1050 | /* request has changed - restore hash */ |
Mark A. Greer | 0d373d6 | 2012-12-21 10:04:08 -0700 | [diff] [blame] | 1051 | dd->pdata->copy_hash(req, 0); |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 1052 | |
| 1053 | if (ctx->op == OP_UPDATE) { |
| 1054 | err = omap_sham_update_req(dd); |
Dmitry Kasatkin | ea1fd22 | 2011-06-02 21:10:05 +0300 | [diff] [blame] | 1055 | if (err != -EINPROGRESS && (ctx->flags & BIT(FLAGS_FINUP))) |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 1056 | /* no final() after finup() */ |
| 1057 | err = omap_sham_final_req(dd); |
| 1058 | } else if (ctx->op == OP_FINAL) { |
| 1059 | err = omap_sham_final_req(dd); |
| 1060 | } |
Dmitry Kasatkin | 798eed5d | 2010-11-19 16:04:26 +0200 | [diff] [blame] | 1061 | err1: |
Dmitry Kasatkin | 6cb3ffe | 2011-06-02 21:10:09 +0300 | [diff] [blame] | 1062 | if (err != -EINPROGRESS) |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 1063 | /* done_task will not finish it, so do it here */ |
| 1064 | omap_sham_finish_req(req, err); |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 1065 | |
| 1066 | dev_dbg(dd->dev, "exit, err: %d\n", err); |
| 1067 | |
Dmitry Kasatkin | a5d8723 | 2010-11-19 16:04:25 +0200 | [diff] [blame] | 1068 | return ret; |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 1069 | } |
| 1070 | |
| 1071 | static int omap_sham_enqueue(struct ahash_request *req, unsigned int op) |
| 1072 | { |
| 1073 | struct omap_sham_reqctx *ctx = ahash_request_ctx(req); |
| 1074 | struct omap_sham_ctx *tctx = crypto_tfm_ctx(req->base.tfm); |
| 1075 | struct omap_sham_dev *dd = tctx->dd; |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 1076 | |
| 1077 | ctx->op = op; |
| 1078 | |
Dmitry Kasatkin | a5d8723 | 2010-11-19 16:04:25 +0200 | [diff] [blame] | 1079 | return omap_sham_handle_queue(dd, req); |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 1080 | } |
| 1081 | |
| 1082 | static int omap_sham_update(struct ahash_request *req) |
| 1083 | { |
| 1084 | struct omap_sham_reqctx *ctx = ahash_request_ctx(req); |
Lokesh Vutla | b8411cc | 2013-08-20 20:32:34 +0530 | [diff] [blame] | 1085 | struct omap_sham_dev *dd = ctx->dd; |
Lokesh Vutla | eaef7e3 | 2013-07-26 12:29:14 +0530 | [diff] [blame] | 1086 | int bs = get_block_size(ctx); |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 1087 | |
| 1088 | if (!req->nbytes) |
| 1089 | return 0; |
| 1090 | |
| 1091 | ctx->total = req->nbytes; |
| 1092 | ctx->sg = req->src; |
| 1093 | ctx->offset = 0; |
| 1094 | |
Dmitry Kasatkin | ea1fd22 | 2011-06-02 21:10:05 +0300 | [diff] [blame] | 1095 | if (ctx->flags & BIT(FLAGS_FINUP)) { |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 1096 | if ((ctx->digcnt + ctx->bufcnt + ctx->total) < 9) { |
| 1097 | /* |
| 1098 | * OMAP HW accel works only with buffers >= 9 |
| 1099 | * will switch to bypass in final() |
| 1100 | * final has the same request and data |
| 1101 | */ |
| 1102 | omap_sham_append_sg(ctx); |
| 1103 | return 0; |
Lokesh Vutla | b8411cc | 2013-08-20 20:32:34 +0530 | [diff] [blame] | 1104 | } else if ((ctx->bufcnt + ctx->total <= bs) || |
| 1105 | dd->polling_mode) { |
Dmitry Kasatkin | 887c883 | 2010-11-19 16:04:29 +0200 | [diff] [blame] | 1106 | /* |
Lokesh Vutla | b8411cc | 2013-08-20 20:32:34 +0530 | [diff] [blame] | 1107 | * faster to use CPU for short transfers or |
| 1108 | * use cpu when dma is not present. |
| 1109 | */ |
Dmitry Kasatkin | ea1fd22 | 2011-06-02 21:10:05 +0300 | [diff] [blame] | 1110 | ctx->flags |= BIT(FLAGS_CPU); |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 1111 | } |
Dmitry Kasatkin | 887c883 | 2010-11-19 16:04:29 +0200 | [diff] [blame] | 1112 | } else if (ctx->bufcnt + ctx->total < ctx->buflen) { |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 1113 | omap_sham_append_sg(ctx); |
| 1114 | return 0; |
| 1115 | } |
| 1116 | |
Lokesh Vutla | acef7b0 | 2013-12-18 19:03:33 +0530 | [diff] [blame] | 1117 | if (dd->polling_mode) |
| 1118 | ctx->flags |= BIT(FLAGS_CPU); |
| 1119 | |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 1120 | return omap_sham_enqueue(req, OP_UPDATE); |
| 1121 | } |
| 1122 | |
Behan Webster | 7bc53c3 | 2014-04-04 18:18:00 -0300 | [diff] [blame] | 1123 | static int omap_sham_shash_digest(struct crypto_shash *tfm, u32 flags, |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 1124 | const u8 *data, unsigned int len, u8 *out) |
| 1125 | { |
Behan Webster | 7bc53c3 | 2014-04-04 18:18:00 -0300 | [diff] [blame] | 1126 | SHASH_DESC_ON_STACK(shash, tfm); |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 1127 | |
Behan Webster | 7bc53c3 | 2014-04-04 18:18:00 -0300 | [diff] [blame] | 1128 | shash->tfm = tfm; |
| 1129 | shash->flags = flags & CRYPTO_TFM_REQ_MAY_SLEEP; |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 1130 | |
Behan Webster | 7bc53c3 | 2014-04-04 18:18:00 -0300 | [diff] [blame] | 1131 | return crypto_shash_digest(shash, data, len, out); |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 1132 | } |
| 1133 | |
| 1134 | static int omap_sham_final_shash(struct ahash_request *req) |
| 1135 | { |
| 1136 | struct omap_sham_ctx *tctx = crypto_tfm_ctx(req->base.tfm); |
| 1137 | struct omap_sham_reqctx *ctx = ahash_request_ctx(req); |
| 1138 | |
| 1139 | return omap_sham_shash_digest(tctx->fallback, req->base.flags, |
| 1140 | ctx->buffer, ctx->bufcnt, req->result); |
| 1141 | } |
| 1142 | |
| 1143 | static int omap_sham_final(struct ahash_request *req) |
| 1144 | { |
| 1145 | struct omap_sham_reqctx *ctx = ahash_request_ctx(req); |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 1146 | |
Dmitry Kasatkin | ea1fd22 | 2011-06-02 21:10:05 +0300 | [diff] [blame] | 1147 | ctx->flags |= BIT(FLAGS_FINUP); |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 1148 | |
Dmitry Kasatkin | ea1fd22 | 2011-06-02 21:10:05 +0300 | [diff] [blame] | 1149 | if (ctx->flags & BIT(FLAGS_ERROR)) |
Dmitry Kasatkin | bf36275 | 2011-04-20 13:34:58 +0300 | [diff] [blame] | 1150 | return 0; /* uncompleted hash is not needed */ |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 1151 | |
Dmitry Kasatkin | bf36275 | 2011-04-20 13:34:58 +0300 | [diff] [blame] | 1152 | /* OMAP HW accel works only with buffers >= 9 */ |
| 1153 | /* HMAC is always >= 9 because ipad == block size */ |
| 1154 | if ((ctx->digcnt + ctx->bufcnt) < 9) |
| 1155 | return omap_sham_final_shash(req); |
| 1156 | else if (ctx->bufcnt) |
| 1157 | return omap_sham_enqueue(req, OP_FINAL); |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 1158 | |
Dmitry Kasatkin | bf36275 | 2011-04-20 13:34:58 +0300 | [diff] [blame] | 1159 | /* copy ready hash (+ finalize hmac) */ |
| 1160 | return omap_sham_finish(req); |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 1161 | } |
| 1162 | |
| 1163 | static int omap_sham_finup(struct ahash_request *req) |
| 1164 | { |
| 1165 | struct omap_sham_reqctx *ctx = ahash_request_ctx(req); |
| 1166 | int err1, err2; |
| 1167 | |
Dmitry Kasatkin | ea1fd22 | 2011-06-02 21:10:05 +0300 | [diff] [blame] | 1168 | ctx->flags |= BIT(FLAGS_FINUP); |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 1169 | |
| 1170 | err1 = omap_sham_update(req); |
Markku Kylanpaa | 455e338 | 2011-04-20 13:34:55 +0300 | [diff] [blame] | 1171 | if (err1 == -EINPROGRESS || err1 == -EBUSY) |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 1172 | return err1; |
| 1173 | /* |
| 1174 | * final() has to be always called to cleanup resources |
| 1175 | * even if udpate() failed, except EINPROGRESS |
| 1176 | */ |
| 1177 | err2 = omap_sham_final(req); |
| 1178 | |
| 1179 | return err1 ?: err2; |
| 1180 | } |
| 1181 | |
| 1182 | static int omap_sham_digest(struct ahash_request *req) |
| 1183 | { |
| 1184 | return omap_sham_init(req) ?: omap_sham_finup(req); |
| 1185 | } |
| 1186 | |
| 1187 | static int omap_sham_setkey(struct crypto_ahash *tfm, const u8 *key, |
| 1188 | unsigned int keylen) |
| 1189 | { |
| 1190 | struct omap_sham_ctx *tctx = crypto_ahash_ctx(tfm); |
| 1191 | struct omap_sham_hmac_ctx *bctx = tctx->base; |
| 1192 | int bs = crypto_shash_blocksize(bctx->shash); |
| 1193 | int ds = crypto_shash_digestsize(bctx->shash); |
Mark A. Greer | 0d373d6 | 2012-12-21 10:04:08 -0700 | [diff] [blame] | 1194 | struct omap_sham_dev *dd = NULL, *tmp; |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 1195 | int err, i; |
Mark A. Greer | 0d373d6 | 2012-12-21 10:04:08 -0700 | [diff] [blame] | 1196 | |
| 1197 | spin_lock_bh(&sham.lock); |
| 1198 | if (!tctx->dd) { |
| 1199 | list_for_each_entry(tmp, &sham.dev_list, list) { |
| 1200 | dd = tmp; |
| 1201 | break; |
| 1202 | } |
| 1203 | tctx->dd = dd; |
| 1204 | } else { |
| 1205 | dd = tctx->dd; |
| 1206 | } |
| 1207 | spin_unlock_bh(&sham.lock); |
| 1208 | |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 1209 | err = crypto_shash_setkey(tctx->fallback, key, keylen); |
| 1210 | if (err) |
| 1211 | return err; |
| 1212 | |
| 1213 | if (keylen > bs) { |
| 1214 | err = omap_sham_shash_digest(bctx->shash, |
| 1215 | crypto_shash_get_flags(bctx->shash), |
| 1216 | key, keylen, bctx->ipad); |
| 1217 | if (err) |
| 1218 | return err; |
| 1219 | keylen = ds; |
| 1220 | } else { |
| 1221 | memcpy(bctx->ipad, key, keylen); |
| 1222 | } |
| 1223 | |
| 1224 | memset(bctx->ipad + keylen, 0, bs - keylen); |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 1225 | |
Mark A. Greer | 0d373d6 | 2012-12-21 10:04:08 -0700 | [diff] [blame] | 1226 | if (!test_bit(FLAGS_AUTO_XOR, &dd->flags)) { |
| 1227 | memcpy(bctx->opad, bctx->ipad, bs); |
| 1228 | |
| 1229 | for (i = 0; i < bs; i++) { |
| 1230 | bctx->ipad[i] ^= 0x36; |
| 1231 | bctx->opad[i] ^= 0x5c; |
| 1232 | } |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 1233 | } |
| 1234 | |
| 1235 | return err; |
| 1236 | } |
| 1237 | |
| 1238 | static int omap_sham_cra_init_alg(struct crypto_tfm *tfm, const char *alg_base) |
| 1239 | { |
| 1240 | struct omap_sham_ctx *tctx = crypto_tfm_ctx(tfm); |
| 1241 | const char *alg_name = crypto_tfm_alg_name(tfm); |
| 1242 | |
| 1243 | /* Allocate a fallback and abort if it failed. */ |
| 1244 | tctx->fallback = crypto_alloc_shash(alg_name, 0, |
| 1245 | CRYPTO_ALG_NEED_FALLBACK); |
| 1246 | if (IS_ERR(tctx->fallback)) { |
| 1247 | pr_err("omap-sham: fallback driver '%s' " |
| 1248 | "could not be loaded.\n", alg_name); |
| 1249 | return PTR_ERR(tctx->fallback); |
| 1250 | } |
| 1251 | |
| 1252 | crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm), |
Dmitry Kasatkin | 798eed5d | 2010-11-19 16:04:26 +0200 | [diff] [blame] | 1253 | sizeof(struct omap_sham_reqctx) + BUFLEN); |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 1254 | |
| 1255 | if (alg_base) { |
| 1256 | struct omap_sham_hmac_ctx *bctx = tctx->base; |
Dmitry Kasatkin | ea1fd22 | 2011-06-02 21:10:05 +0300 | [diff] [blame] | 1257 | tctx->flags |= BIT(FLAGS_HMAC); |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 1258 | bctx->shash = crypto_alloc_shash(alg_base, 0, |
| 1259 | CRYPTO_ALG_NEED_FALLBACK); |
| 1260 | if (IS_ERR(bctx->shash)) { |
| 1261 | pr_err("omap-sham: base driver '%s' " |
| 1262 | "could not be loaded.\n", alg_base); |
| 1263 | crypto_free_shash(tctx->fallback); |
| 1264 | return PTR_ERR(bctx->shash); |
| 1265 | } |
| 1266 | |
| 1267 | } |
| 1268 | |
| 1269 | return 0; |
| 1270 | } |
| 1271 | |
| 1272 | static int omap_sham_cra_init(struct crypto_tfm *tfm) |
| 1273 | { |
| 1274 | return omap_sham_cra_init_alg(tfm, NULL); |
| 1275 | } |
| 1276 | |
| 1277 | static int omap_sham_cra_sha1_init(struct crypto_tfm *tfm) |
| 1278 | { |
| 1279 | return omap_sham_cra_init_alg(tfm, "sha1"); |
| 1280 | } |
| 1281 | |
Mark A. Greer | d20fb18 | 2012-12-21 10:04:09 -0700 | [diff] [blame] | 1282 | static int omap_sham_cra_sha224_init(struct crypto_tfm *tfm) |
| 1283 | { |
| 1284 | return omap_sham_cra_init_alg(tfm, "sha224"); |
| 1285 | } |
| 1286 | |
| 1287 | static int omap_sham_cra_sha256_init(struct crypto_tfm *tfm) |
| 1288 | { |
| 1289 | return omap_sham_cra_init_alg(tfm, "sha256"); |
| 1290 | } |
| 1291 | |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 1292 | static int omap_sham_cra_md5_init(struct crypto_tfm *tfm) |
| 1293 | { |
| 1294 | return omap_sham_cra_init_alg(tfm, "md5"); |
| 1295 | } |
| 1296 | |
Lokesh Vutla | eaef7e3 | 2013-07-26 12:29:14 +0530 | [diff] [blame] | 1297 | static int omap_sham_cra_sha384_init(struct crypto_tfm *tfm) |
| 1298 | { |
| 1299 | return omap_sham_cra_init_alg(tfm, "sha384"); |
| 1300 | } |
| 1301 | |
| 1302 | static int omap_sham_cra_sha512_init(struct crypto_tfm *tfm) |
| 1303 | { |
| 1304 | return omap_sham_cra_init_alg(tfm, "sha512"); |
| 1305 | } |
| 1306 | |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 1307 | static void omap_sham_cra_exit(struct crypto_tfm *tfm) |
| 1308 | { |
| 1309 | struct omap_sham_ctx *tctx = crypto_tfm_ctx(tfm); |
| 1310 | |
| 1311 | crypto_free_shash(tctx->fallback); |
| 1312 | tctx->fallback = NULL; |
| 1313 | |
Dmitry Kasatkin | ea1fd22 | 2011-06-02 21:10:05 +0300 | [diff] [blame] | 1314 | if (tctx->flags & BIT(FLAGS_HMAC)) { |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 1315 | struct omap_sham_hmac_ctx *bctx = tctx->base; |
| 1316 | crypto_free_shash(bctx->shash); |
| 1317 | } |
| 1318 | } |
| 1319 | |
Mark A. Greer | d20fb18 | 2012-12-21 10:04:09 -0700 | [diff] [blame] | 1320 | static struct ahash_alg algs_sha1_md5[] = { |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 1321 | { |
| 1322 | .init = omap_sham_init, |
| 1323 | .update = omap_sham_update, |
| 1324 | .final = omap_sham_final, |
| 1325 | .finup = omap_sham_finup, |
| 1326 | .digest = omap_sham_digest, |
| 1327 | .halg.digestsize = SHA1_DIGEST_SIZE, |
| 1328 | .halg.base = { |
| 1329 | .cra_name = "sha1", |
| 1330 | .cra_driver_name = "omap-sha1", |
| 1331 | .cra_priority = 100, |
| 1332 | .cra_flags = CRYPTO_ALG_TYPE_AHASH | |
Nikos Mavrogiannopoulos | d912bb7 | 2011-11-01 13:39:56 +0100 | [diff] [blame] | 1333 | CRYPTO_ALG_KERN_DRIVER_ONLY | |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 1334 | CRYPTO_ALG_ASYNC | |
| 1335 | CRYPTO_ALG_NEED_FALLBACK, |
| 1336 | .cra_blocksize = SHA1_BLOCK_SIZE, |
| 1337 | .cra_ctxsize = sizeof(struct omap_sham_ctx), |
| 1338 | .cra_alignmask = 0, |
| 1339 | .cra_module = THIS_MODULE, |
| 1340 | .cra_init = omap_sham_cra_init, |
| 1341 | .cra_exit = omap_sham_cra_exit, |
| 1342 | } |
| 1343 | }, |
| 1344 | { |
| 1345 | .init = omap_sham_init, |
| 1346 | .update = omap_sham_update, |
| 1347 | .final = omap_sham_final, |
| 1348 | .finup = omap_sham_finup, |
| 1349 | .digest = omap_sham_digest, |
| 1350 | .halg.digestsize = MD5_DIGEST_SIZE, |
| 1351 | .halg.base = { |
| 1352 | .cra_name = "md5", |
| 1353 | .cra_driver_name = "omap-md5", |
| 1354 | .cra_priority = 100, |
| 1355 | .cra_flags = CRYPTO_ALG_TYPE_AHASH | |
Nikos Mavrogiannopoulos | d912bb7 | 2011-11-01 13:39:56 +0100 | [diff] [blame] | 1356 | CRYPTO_ALG_KERN_DRIVER_ONLY | |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 1357 | CRYPTO_ALG_ASYNC | |
| 1358 | CRYPTO_ALG_NEED_FALLBACK, |
| 1359 | .cra_blocksize = SHA1_BLOCK_SIZE, |
| 1360 | .cra_ctxsize = sizeof(struct omap_sham_ctx), |
Dmitry Kasatkin | 798eed5d | 2010-11-19 16:04:26 +0200 | [diff] [blame] | 1361 | .cra_alignmask = OMAP_ALIGN_MASK, |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 1362 | .cra_module = THIS_MODULE, |
| 1363 | .cra_init = omap_sham_cra_init, |
| 1364 | .cra_exit = omap_sham_cra_exit, |
| 1365 | } |
| 1366 | }, |
| 1367 | { |
| 1368 | .init = omap_sham_init, |
| 1369 | .update = omap_sham_update, |
| 1370 | .final = omap_sham_final, |
| 1371 | .finup = omap_sham_finup, |
| 1372 | .digest = omap_sham_digest, |
| 1373 | .setkey = omap_sham_setkey, |
| 1374 | .halg.digestsize = SHA1_DIGEST_SIZE, |
| 1375 | .halg.base = { |
| 1376 | .cra_name = "hmac(sha1)", |
| 1377 | .cra_driver_name = "omap-hmac-sha1", |
| 1378 | .cra_priority = 100, |
| 1379 | .cra_flags = CRYPTO_ALG_TYPE_AHASH | |
Nikos Mavrogiannopoulos | d912bb7 | 2011-11-01 13:39:56 +0100 | [diff] [blame] | 1380 | CRYPTO_ALG_KERN_DRIVER_ONLY | |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 1381 | CRYPTO_ALG_ASYNC | |
| 1382 | CRYPTO_ALG_NEED_FALLBACK, |
| 1383 | .cra_blocksize = SHA1_BLOCK_SIZE, |
| 1384 | .cra_ctxsize = sizeof(struct omap_sham_ctx) + |
| 1385 | sizeof(struct omap_sham_hmac_ctx), |
Dmitry Kasatkin | 798eed5d | 2010-11-19 16:04:26 +0200 | [diff] [blame] | 1386 | .cra_alignmask = OMAP_ALIGN_MASK, |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 1387 | .cra_module = THIS_MODULE, |
| 1388 | .cra_init = omap_sham_cra_sha1_init, |
| 1389 | .cra_exit = omap_sham_cra_exit, |
| 1390 | } |
| 1391 | }, |
| 1392 | { |
| 1393 | .init = omap_sham_init, |
| 1394 | .update = omap_sham_update, |
| 1395 | .final = omap_sham_final, |
| 1396 | .finup = omap_sham_finup, |
| 1397 | .digest = omap_sham_digest, |
| 1398 | .setkey = omap_sham_setkey, |
| 1399 | .halg.digestsize = MD5_DIGEST_SIZE, |
| 1400 | .halg.base = { |
| 1401 | .cra_name = "hmac(md5)", |
| 1402 | .cra_driver_name = "omap-hmac-md5", |
| 1403 | .cra_priority = 100, |
| 1404 | .cra_flags = CRYPTO_ALG_TYPE_AHASH | |
Nikos Mavrogiannopoulos | d912bb7 | 2011-11-01 13:39:56 +0100 | [diff] [blame] | 1405 | CRYPTO_ALG_KERN_DRIVER_ONLY | |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 1406 | CRYPTO_ALG_ASYNC | |
| 1407 | CRYPTO_ALG_NEED_FALLBACK, |
| 1408 | .cra_blocksize = SHA1_BLOCK_SIZE, |
| 1409 | .cra_ctxsize = sizeof(struct omap_sham_ctx) + |
| 1410 | sizeof(struct omap_sham_hmac_ctx), |
Dmitry Kasatkin | 798eed5d | 2010-11-19 16:04:26 +0200 | [diff] [blame] | 1411 | .cra_alignmask = OMAP_ALIGN_MASK, |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 1412 | .cra_module = THIS_MODULE, |
| 1413 | .cra_init = omap_sham_cra_md5_init, |
| 1414 | .cra_exit = omap_sham_cra_exit, |
| 1415 | } |
| 1416 | } |
| 1417 | }; |
| 1418 | |
Mark A. Greer | d20fb18 | 2012-12-21 10:04:09 -0700 | [diff] [blame] | 1419 | /* OMAP4 has some algs in addition to what OMAP2 has */ |
| 1420 | static struct ahash_alg algs_sha224_sha256[] = { |
| 1421 | { |
| 1422 | .init = omap_sham_init, |
| 1423 | .update = omap_sham_update, |
| 1424 | .final = omap_sham_final, |
| 1425 | .finup = omap_sham_finup, |
| 1426 | .digest = omap_sham_digest, |
| 1427 | .halg.digestsize = SHA224_DIGEST_SIZE, |
| 1428 | .halg.base = { |
| 1429 | .cra_name = "sha224", |
| 1430 | .cra_driver_name = "omap-sha224", |
| 1431 | .cra_priority = 100, |
| 1432 | .cra_flags = CRYPTO_ALG_TYPE_AHASH | |
| 1433 | CRYPTO_ALG_ASYNC | |
| 1434 | CRYPTO_ALG_NEED_FALLBACK, |
| 1435 | .cra_blocksize = SHA224_BLOCK_SIZE, |
| 1436 | .cra_ctxsize = sizeof(struct omap_sham_ctx), |
| 1437 | .cra_alignmask = 0, |
| 1438 | .cra_module = THIS_MODULE, |
| 1439 | .cra_init = omap_sham_cra_init, |
| 1440 | .cra_exit = omap_sham_cra_exit, |
| 1441 | } |
| 1442 | }, |
| 1443 | { |
| 1444 | .init = omap_sham_init, |
| 1445 | .update = omap_sham_update, |
| 1446 | .final = omap_sham_final, |
| 1447 | .finup = omap_sham_finup, |
| 1448 | .digest = omap_sham_digest, |
| 1449 | .halg.digestsize = SHA256_DIGEST_SIZE, |
| 1450 | .halg.base = { |
| 1451 | .cra_name = "sha256", |
| 1452 | .cra_driver_name = "omap-sha256", |
| 1453 | .cra_priority = 100, |
| 1454 | .cra_flags = CRYPTO_ALG_TYPE_AHASH | |
| 1455 | CRYPTO_ALG_ASYNC | |
| 1456 | CRYPTO_ALG_NEED_FALLBACK, |
| 1457 | .cra_blocksize = SHA256_BLOCK_SIZE, |
| 1458 | .cra_ctxsize = sizeof(struct omap_sham_ctx), |
| 1459 | .cra_alignmask = 0, |
| 1460 | .cra_module = THIS_MODULE, |
| 1461 | .cra_init = omap_sham_cra_init, |
| 1462 | .cra_exit = omap_sham_cra_exit, |
| 1463 | } |
| 1464 | }, |
| 1465 | { |
| 1466 | .init = omap_sham_init, |
| 1467 | .update = omap_sham_update, |
| 1468 | .final = omap_sham_final, |
| 1469 | .finup = omap_sham_finup, |
| 1470 | .digest = omap_sham_digest, |
| 1471 | .setkey = omap_sham_setkey, |
| 1472 | .halg.digestsize = SHA224_DIGEST_SIZE, |
| 1473 | .halg.base = { |
| 1474 | .cra_name = "hmac(sha224)", |
| 1475 | .cra_driver_name = "omap-hmac-sha224", |
| 1476 | .cra_priority = 100, |
| 1477 | .cra_flags = CRYPTO_ALG_TYPE_AHASH | |
| 1478 | CRYPTO_ALG_ASYNC | |
| 1479 | CRYPTO_ALG_NEED_FALLBACK, |
| 1480 | .cra_blocksize = SHA224_BLOCK_SIZE, |
| 1481 | .cra_ctxsize = sizeof(struct omap_sham_ctx) + |
| 1482 | sizeof(struct omap_sham_hmac_ctx), |
| 1483 | .cra_alignmask = OMAP_ALIGN_MASK, |
| 1484 | .cra_module = THIS_MODULE, |
| 1485 | .cra_init = omap_sham_cra_sha224_init, |
| 1486 | .cra_exit = omap_sham_cra_exit, |
| 1487 | } |
| 1488 | }, |
| 1489 | { |
| 1490 | .init = omap_sham_init, |
| 1491 | .update = omap_sham_update, |
| 1492 | .final = omap_sham_final, |
| 1493 | .finup = omap_sham_finup, |
| 1494 | .digest = omap_sham_digest, |
| 1495 | .setkey = omap_sham_setkey, |
| 1496 | .halg.digestsize = SHA256_DIGEST_SIZE, |
| 1497 | .halg.base = { |
| 1498 | .cra_name = "hmac(sha256)", |
| 1499 | .cra_driver_name = "omap-hmac-sha256", |
| 1500 | .cra_priority = 100, |
| 1501 | .cra_flags = CRYPTO_ALG_TYPE_AHASH | |
| 1502 | CRYPTO_ALG_ASYNC | |
| 1503 | CRYPTO_ALG_NEED_FALLBACK, |
| 1504 | .cra_blocksize = SHA256_BLOCK_SIZE, |
| 1505 | .cra_ctxsize = sizeof(struct omap_sham_ctx) + |
| 1506 | sizeof(struct omap_sham_hmac_ctx), |
| 1507 | .cra_alignmask = OMAP_ALIGN_MASK, |
| 1508 | .cra_module = THIS_MODULE, |
| 1509 | .cra_init = omap_sham_cra_sha256_init, |
| 1510 | .cra_exit = omap_sham_cra_exit, |
| 1511 | } |
| 1512 | }, |
| 1513 | }; |
| 1514 | |
Lokesh Vutla | eaef7e3 | 2013-07-26 12:29:14 +0530 | [diff] [blame] | 1515 | static struct ahash_alg algs_sha384_sha512[] = { |
| 1516 | { |
| 1517 | .init = omap_sham_init, |
| 1518 | .update = omap_sham_update, |
| 1519 | .final = omap_sham_final, |
| 1520 | .finup = omap_sham_finup, |
| 1521 | .digest = omap_sham_digest, |
| 1522 | .halg.digestsize = SHA384_DIGEST_SIZE, |
| 1523 | .halg.base = { |
| 1524 | .cra_name = "sha384", |
| 1525 | .cra_driver_name = "omap-sha384", |
| 1526 | .cra_priority = 100, |
| 1527 | .cra_flags = CRYPTO_ALG_TYPE_AHASH | |
| 1528 | CRYPTO_ALG_ASYNC | |
| 1529 | CRYPTO_ALG_NEED_FALLBACK, |
| 1530 | .cra_blocksize = SHA384_BLOCK_SIZE, |
| 1531 | .cra_ctxsize = sizeof(struct omap_sham_ctx), |
| 1532 | .cra_alignmask = 0, |
| 1533 | .cra_module = THIS_MODULE, |
| 1534 | .cra_init = omap_sham_cra_init, |
| 1535 | .cra_exit = omap_sham_cra_exit, |
| 1536 | } |
| 1537 | }, |
| 1538 | { |
| 1539 | .init = omap_sham_init, |
| 1540 | .update = omap_sham_update, |
| 1541 | .final = omap_sham_final, |
| 1542 | .finup = omap_sham_finup, |
| 1543 | .digest = omap_sham_digest, |
| 1544 | .halg.digestsize = SHA512_DIGEST_SIZE, |
| 1545 | .halg.base = { |
| 1546 | .cra_name = "sha512", |
| 1547 | .cra_driver_name = "omap-sha512", |
| 1548 | .cra_priority = 100, |
| 1549 | .cra_flags = CRYPTO_ALG_TYPE_AHASH | |
| 1550 | CRYPTO_ALG_ASYNC | |
| 1551 | CRYPTO_ALG_NEED_FALLBACK, |
| 1552 | .cra_blocksize = SHA512_BLOCK_SIZE, |
| 1553 | .cra_ctxsize = sizeof(struct omap_sham_ctx), |
| 1554 | .cra_alignmask = 0, |
| 1555 | .cra_module = THIS_MODULE, |
| 1556 | .cra_init = omap_sham_cra_init, |
| 1557 | .cra_exit = omap_sham_cra_exit, |
| 1558 | } |
| 1559 | }, |
| 1560 | { |
| 1561 | .init = omap_sham_init, |
| 1562 | .update = omap_sham_update, |
| 1563 | .final = omap_sham_final, |
| 1564 | .finup = omap_sham_finup, |
| 1565 | .digest = omap_sham_digest, |
| 1566 | .setkey = omap_sham_setkey, |
| 1567 | .halg.digestsize = SHA384_DIGEST_SIZE, |
| 1568 | .halg.base = { |
| 1569 | .cra_name = "hmac(sha384)", |
| 1570 | .cra_driver_name = "omap-hmac-sha384", |
| 1571 | .cra_priority = 100, |
| 1572 | .cra_flags = CRYPTO_ALG_TYPE_AHASH | |
| 1573 | CRYPTO_ALG_ASYNC | |
| 1574 | CRYPTO_ALG_NEED_FALLBACK, |
| 1575 | .cra_blocksize = SHA384_BLOCK_SIZE, |
| 1576 | .cra_ctxsize = sizeof(struct omap_sham_ctx) + |
| 1577 | sizeof(struct omap_sham_hmac_ctx), |
| 1578 | .cra_alignmask = OMAP_ALIGN_MASK, |
| 1579 | .cra_module = THIS_MODULE, |
| 1580 | .cra_init = omap_sham_cra_sha384_init, |
| 1581 | .cra_exit = omap_sham_cra_exit, |
| 1582 | } |
| 1583 | }, |
| 1584 | { |
| 1585 | .init = omap_sham_init, |
| 1586 | .update = omap_sham_update, |
| 1587 | .final = omap_sham_final, |
| 1588 | .finup = omap_sham_finup, |
| 1589 | .digest = omap_sham_digest, |
| 1590 | .setkey = omap_sham_setkey, |
| 1591 | .halg.digestsize = SHA512_DIGEST_SIZE, |
| 1592 | .halg.base = { |
| 1593 | .cra_name = "hmac(sha512)", |
| 1594 | .cra_driver_name = "omap-hmac-sha512", |
| 1595 | .cra_priority = 100, |
| 1596 | .cra_flags = CRYPTO_ALG_TYPE_AHASH | |
| 1597 | CRYPTO_ALG_ASYNC | |
| 1598 | CRYPTO_ALG_NEED_FALLBACK, |
| 1599 | .cra_blocksize = SHA512_BLOCK_SIZE, |
| 1600 | .cra_ctxsize = sizeof(struct omap_sham_ctx) + |
| 1601 | sizeof(struct omap_sham_hmac_ctx), |
| 1602 | .cra_alignmask = OMAP_ALIGN_MASK, |
| 1603 | .cra_module = THIS_MODULE, |
| 1604 | .cra_init = omap_sham_cra_sha512_init, |
| 1605 | .cra_exit = omap_sham_cra_exit, |
| 1606 | } |
| 1607 | }, |
| 1608 | }; |
| 1609 | |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 1610 | static void omap_sham_done_task(unsigned long data) |
| 1611 | { |
| 1612 | struct omap_sham_dev *dd = (struct omap_sham_dev *)data; |
Dmitry Kasatkin | 6c63db8 | 2011-06-02 21:10:10 +0300 | [diff] [blame] | 1613 | int err = 0; |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 1614 | |
Dmitry Kasatkin | 6cb3ffe | 2011-06-02 21:10:09 +0300 | [diff] [blame] | 1615 | if (!test_bit(FLAGS_BUSY, &dd->flags)) { |
| 1616 | omap_sham_handle_queue(dd, NULL); |
| 1617 | return; |
| 1618 | } |
| 1619 | |
Dmitry Kasatkin | 6c63db8 | 2011-06-02 21:10:10 +0300 | [diff] [blame] | 1620 | if (test_bit(FLAGS_CPU, &dd->flags)) { |
Lokesh Vutla | b8411cc | 2013-08-20 20:32:34 +0530 | [diff] [blame] | 1621 | if (test_and_clear_bit(FLAGS_OUTPUT_READY, &dd->flags)) { |
| 1622 | /* hash or semi-hash ready */ |
| 1623 | err = omap_sham_update_cpu(dd); |
| 1624 | if (err != -EINPROGRESS) |
| 1625 | goto finish; |
| 1626 | } |
Dmitry Kasatkin | 6c63db8 | 2011-06-02 21:10:10 +0300 | [diff] [blame] | 1627 | } else if (test_bit(FLAGS_DMA_READY, &dd->flags)) { |
| 1628 | if (test_and_clear_bit(FLAGS_DMA_ACTIVE, &dd->flags)) { |
| 1629 | omap_sham_update_dma_stop(dd); |
| 1630 | if (dd->err) { |
| 1631 | err = dd->err; |
| 1632 | goto finish; |
| 1633 | } |
| 1634 | } |
| 1635 | if (test_and_clear_bit(FLAGS_OUTPUT_READY, &dd->flags)) { |
| 1636 | /* hash or semi-hash ready */ |
| 1637 | clear_bit(FLAGS_DMA_READY, &dd->flags); |
Dmitry Kasatkin | 887c883 | 2010-11-19 16:04:29 +0200 | [diff] [blame] | 1638 | err = omap_sham_update_dma_start(dd); |
Dmitry Kasatkin | 6c63db8 | 2011-06-02 21:10:10 +0300 | [diff] [blame] | 1639 | if (err != -EINPROGRESS) |
| 1640 | goto finish; |
| 1641 | } |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 1642 | } |
| 1643 | |
Dmitry Kasatkin | 6c63db8 | 2011-06-02 21:10:10 +0300 | [diff] [blame] | 1644 | return; |
Dmitry Kasatkin | 3e133c8 | 2010-11-19 16:04:24 +0200 | [diff] [blame] | 1645 | |
Dmitry Kasatkin | 6c63db8 | 2011-06-02 21:10:10 +0300 | [diff] [blame] | 1646 | finish: |
| 1647 | dev_dbg(dd->dev, "update done: err: %d\n", err); |
| 1648 | /* finish curent request */ |
| 1649 | omap_sham_finish_req(dd->req, err); |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 1650 | } |
| 1651 | |
Mark A. Greer | 0d373d6 | 2012-12-21 10:04:08 -0700 | [diff] [blame] | 1652 | static irqreturn_t omap_sham_irq_common(struct omap_sham_dev *dd) |
| 1653 | { |
| 1654 | if (!test_bit(FLAGS_BUSY, &dd->flags)) { |
| 1655 | dev_warn(dd->dev, "Interrupt when no active requests.\n"); |
| 1656 | } else { |
| 1657 | set_bit(FLAGS_OUTPUT_READY, &dd->flags); |
| 1658 | tasklet_schedule(&dd->done_task); |
| 1659 | } |
| 1660 | |
| 1661 | return IRQ_HANDLED; |
| 1662 | } |
| 1663 | |
| 1664 | static irqreturn_t omap_sham_irq_omap2(int irq, void *dev_id) |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 1665 | { |
| 1666 | struct omap_sham_dev *dd = dev_id; |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 1667 | |
Dmitry Kasatkin | ed3ea9a8 | 2011-06-02 21:10:07 +0300 | [diff] [blame] | 1668 | if (unlikely(test_bit(FLAGS_FINAL, &dd->flags))) |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 1669 | /* final -> allow device to go to power-saving mode */ |
| 1670 | omap_sham_write_mask(dd, SHA_REG_CTRL, 0, SHA_REG_CTRL_LENGTH); |
| 1671 | |
| 1672 | omap_sham_write_mask(dd, SHA_REG_CTRL, SHA_REG_CTRL_OUTPUT_READY, |
| 1673 | SHA_REG_CTRL_OUTPUT_READY); |
| 1674 | omap_sham_read(dd, SHA_REG_CTRL); |
| 1675 | |
Mark A. Greer | 0d373d6 | 2012-12-21 10:04:08 -0700 | [diff] [blame] | 1676 | return omap_sham_irq_common(dd); |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 1677 | } |
| 1678 | |
Mark A. Greer | 0d373d6 | 2012-12-21 10:04:08 -0700 | [diff] [blame] | 1679 | static irqreturn_t omap_sham_irq_omap4(int irq, void *dev_id) |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 1680 | { |
Mark A. Greer | 0d373d6 | 2012-12-21 10:04:08 -0700 | [diff] [blame] | 1681 | struct omap_sham_dev *dd = dev_id; |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 1682 | |
Mark A. Greer | 0d373d6 | 2012-12-21 10:04:08 -0700 | [diff] [blame] | 1683 | omap_sham_write_mask(dd, SHA_REG_MASK(dd), 0, SHA_REG_MASK_IT_EN); |
Dmitry Kasatkin | 3e133c8 | 2010-11-19 16:04:24 +0200 | [diff] [blame] | 1684 | |
Mark A. Greer | 0d373d6 | 2012-12-21 10:04:08 -0700 | [diff] [blame] | 1685 | return omap_sham_irq_common(dd); |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 1686 | } |
| 1687 | |
Mark A. Greer | d20fb18 | 2012-12-21 10:04:09 -0700 | [diff] [blame] | 1688 | static struct omap_sham_algs_info omap_sham_algs_info_omap2[] = { |
| 1689 | { |
| 1690 | .algs_list = algs_sha1_md5, |
| 1691 | .size = ARRAY_SIZE(algs_sha1_md5), |
| 1692 | }, |
| 1693 | }; |
| 1694 | |
Mark A. Greer | 0d373d6 | 2012-12-21 10:04:08 -0700 | [diff] [blame] | 1695 | static const struct omap_sham_pdata omap_sham_pdata_omap2 = { |
Mark A. Greer | d20fb18 | 2012-12-21 10:04:09 -0700 | [diff] [blame] | 1696 | .algs_info = omap_sham_algs_info_omap2, |
| 1697 | .algs_info_size = ARRAY_SIZE(omap_sham_algs_info_omap2), |
Mark A. Greer | 0d373d6 | 2012-12-21 10:04:08 -0700 | [diff] [blame] | 1698 | .flags = BIT(FLAGS_BE32_SHA1), |
| 1699 | .digest_size = SHA1_DIGEST_SIZE, |
| 1700 | .copy_hash = omap_sham_copy_hash_omap2, |
| 1701 | .write_ctrl = omap_sham_write_ctrl_omap2, |
| 1702 | .trigger = omap_sham_trigger_omap2, |
| 1703 | .poll_irq = omap_sham_poll_irq_omap2, |
| 1704 | .intr_hdlr = omap_sham_irq_omap2, |
| 1705 | .idigest_ofs = 0x00, |
| 1706 | .din_ofs = 0x1c, |
| 1707 | .digcnt_ofs = 0x14, |
| 1708 | .rev_ofs = 0x5c, |
| 1709 | .mask_ofs = 0x60, |
| 1710 | .sysstatus_ofs = 0x64, |
| 1711 | .major_mask = 0xf0, |
| 1712 | .major_shift = 4, |
| 1713 | .minor_mask = 0x0f, |
| 1714 | .minor_shift = 0, |
| 1715 | }; |
| 1716 | |
Mark A. Greer | 03feec9 | 2012-12-21 10:04:06 -0700 | [diff] [blame] | 1717 | #ifdef CONFIG_OF |
Mark A. Greer | d20fb18 | 2012-12-21 10:04:09 -0700 | [diff] [blame] | 1718 | static struct omap_sham_algs_info omap_sham_algs_info_omap4[] = { |
| 1719 | { |
| 1720 | .algs_list = algs_sha1_md5, |
| 1721 | .size = ARRAY_SIZE(algs_sha1_md5), |
| 1722 | }, |
| 1723 | { |
| 1724 | .algs_list = algs_sha224_sha256, |
| 1725 | .size = ARRAY_SIZE(algs_sha224_sha256), |
| 1726 | }, |
| 1727 | }; |
| 1728 | |
Mark A. Greer | 0d373d6 | 2012-12-21 10:04:08 -0700 | [diff] [blame] | 1729 | static const struct omap_sham_pdata omap_sham_pdata_omap4 = { |
Mark A. Greer | d20fb18 | 2012-12-21 10:04:09 -0700 | [diff] [blame] | 1730 | .algs_info = omap_sham_algs_info_omap4, |
| 1731 | .algs_info_size = ARRAY_SIZE(omap_sham_algs_info_omap4), |
Mark A. Greer | 0d373d6 | 2012-12-21 10:04:08 -0700 | [diff] [blame] | 1732 | .flags = BIT(FLAGS_AUTO_XOR), |
| 1733 | .digest_size = SHA256_DIGEST_SIZE, |
| 1734 | .copy_hash = omap_sham_copy_hash_omap4, |
| 1735 | .write_ctrl = omap_sham_write_ctrl_omap4, |
| 1736 | .trigger = omap_sham_trigger_omap4, |
| 1737 | .poll_irq = omap_sham_poll_irq_omap4, |
| 1738 | .intr_hdlr = omap_sham_irq_omap4, |
| 1739 | .idigest_ofs = 0x020, |
Lokesh Vutla | eaef7e3 | 2013-07-26 12:29:14 +0530 | [diff] [blame] | 1740 | .odigest_ofs = 0x0, |
Mark A. Greer | 0d373d6 | 2012-12-21 10:04:08 -0700 | [diff] [blame] | 1741 | .din_ofs = 0x080, |
| 1742 | .digcnt_ofs = 0x040, |
| 1743 | .rev_ofs = 0x100, |
| 1744 | .mask_ofs = 0x110, |
| 1745 | .sysstatus_ofs = 0x114, |
Lokesh Vutla | eaef7e3 | 2013-07-26 12:29:14 +0530 | [diff] [blame] | 1746 | .mode_ofs = 0x44, |
| 1747 | .length_ofs = 0x48, |
Mark A. Greer | 0d373d6 | 2012-12-21 10:04:08 -0700 | [diff] [blame] | 1748 | .major_mask = 0x0700, |
| 1749 | .major_shift = 8, |
| 1750 | .minor_mask = 0x003f, |
| 1751 | .minor_shift = 0, |
| 1752 | }; |
| 1753 | |
Lokesh Vutla | 7d7c704 | 2013-07-26 12:29:15 +0530 | [diff] [blame] | 1754 | static struct omap_sham_algs_info omap_sham_algs_info_omap5[] = { |
| 1755 | { |
| 1756 | .algs_list = algs_sha1_md5, |
| 1757 | .size = ARRAY_SIZE(algs_sha1_md5), |
| 1758 | }, |
| 1759 | { |
| 1760 | .algs_list = algs_sha224_sha256, |
| 1761 | .size = ARRAY_SIZE(algs_sha224_sha256), |
| 1762 | }, |
| 1763 | { |
| 1764 | .algs_list = algs_sha384_sha512, |
| 1765 | .size = ARRAY_SIZE(algs_sha384_sha512), |
| 1766 | }, |
| 1767 | }; |
| 1768 | |
| 1769 | static const struct omap_sham_pdata omap_sham_pdata_omap5 = { |
| 1770 | .algs_info = omap_sham_algs_info_omap5, |
| 1771 | .algs_info_size = ARRAY_SIZE(omap_sham_algs_info_omap5), |
| 1772 | .flags = BIT(FLAGS_AUTO_XOR), |
| 1773 | .digest_size = SHA512_DIGEST_SIZE, |
| 1774 | .copy_hash = omap_sham_copy_hash_omap4, |
| 1775 | .write_ctrl = omap_sham_write_ctrl_omap4, |
| 1776 | .trigger = omap_sham_trigger_omap4, |
| 1777 | .poll_irq = omap_sham_poll_irq_omap4, |
| 1778 | .intr_hdlr = omap_sham_irq_omap4, |
| 1779 | .idigest_ofs = 0x240, |
| 1780 | .odigest_ofs = 0x200, |
| 1781 | .din_ofs = 0x080, |
| 1782 | .digcnt_ofs = 0x280, |
| 1783 | .rev_ofs = 0x100, |
| 1784 | .mask_ofs = 0x110, |
| 1785 | .sysstatus_ofs = 0x114, |
| 1786 | .mode_ofs = 0x284, |
| 1787 | .length_ofs = 0x288, |
| 1788 | .major_mask = 0x0700, |
| 1789 | .major_shift = 8, |
| 1790 | .minor_mask = 0x003f, |
| 1791 | .minor_shift = 0, |
| 1792 | }; |
| 1793 | |
Mark A. Greer | 03feec9 | 2012-12-21 10:04:06 -0700 | [diff] [blame] | 1794 | static const struct of_device_id omap_sham_of_match[] = { |
| 1795 | { |
| 1796 | .compatible = "ti,omap2-sham", |
Mark A. Greer | 0d373d6 | 2012-12-21 10:04:08 -0700 | [diff] [blame] | 1797 | .data = &omap_sham_pdata_omap2, |
| 1798 | }, |
| 1799 | { |
Pali Rohár | eddca85 | 2015-02-26 14:49:53 +0100 | [diff] [blame] | 1800 | .compatible = "ti,omap3-sham", |
| 1801 | .data = &omap_sham_pdata_omap2, |
| 1802 | }, |
| 1803 | { |
Mark A. Greer | 0d373d6 | 2012-12-21 10:04:08 -0700 | [diff] [blame] | 1804 | .compatible = "ti,omap4-sham", |
| 1805 | .data = &omap_sham_pdata_omap4, |
Mark A. Greer | 03feec9 | 2012-12-21 10:04:06 -0700 | [diff] [blame] | 1806 | }, |
Lokesh Vutla | 7d7c704 | 2013-07-26 12:29:15 +0530 | [diff] [blame] | 1807 | { |
| 1808 | .compatible = "ti,omap5-sham", |
| 1809 | .data = &omap_sham_pdata_omap5, |
| 1810 | }, |
Mark A. Greer | 03feec9 | 2012-12-21 10:04:06 -0700 | [diff] [blame] | 1811 | {}, |
| 1812 | }; |
| 1813 | MODULE_DEVICE_TABLE(of, omap_sham_of_match); |
| 1814 | |
| 1815 | static int omap_sham_get_res_of(struct omap_sham_dev *dd, |
| 1816 | struct device *dev, struct resource *res) |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 1817 | { |
Mark A. Greer | 03feec9 | 2012-12-21 10:04:06 -0700 | [diff] [blame] | 1818 | struct device_node *node = dev->of_node; |
| 1819 | const struct of_device_id *match; |
| 1820 | int err = 0; |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 1821 | |
Mark A. Greer | 03feec9 | 2012-12-21 10:04:06 -0700 | [diff] [blame] | 1822 | match = of_match_device(of_match_ptr(omap_sham_of_match), dev); |
| 1823 | if (!match) { |
| 1824 | dev_err(dev, "no compatible OF match\n"); |
| 1825 | err = -EINVAL; |
| 1826 | goto err; |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 1827 | } |
Samu Onkalo | 584db6a | 2010-09-03 19:20:19 +0800 | [diff] [blame] | 1828 | |
Mark A. Greer | 03feec9 | 2012-12-21 10:04:06 -0700 | [diff] [blame] | 1829 | err = of_address_to_resource(node, 0, res); |
| 1830 | if (err < 0) { |
| 1831 | dev_err(dev, "can't translate OF node address\n"); |
| 1832 | err = -EINVAL; |
| 1833 | goto err; |
| 1834 | } |
| 1835 | |
Thierry Reding | f757849 | 2013-09-18 15:24:44 +0200 | [diff] [blame] | 1836 | dd->irq = irq_of_parse_and_map(node, 0); |
Mark A. Greer | 03feec9 | 2012-12-21 10:04:06 -0700 | [diff] [blame] | 1837 | if (!dd->irq) { |
| 1838 | dev_err(dev, "can't translate OF irq value\n"); |
| 1839 | err = -EINVAL; |
| 1840 | goto err; |
| 1841 | } |
| 1842 | |
Mark A. Greer | 0d373d6 | 2012-12-21 10:04:08 -0700 | [diff] [blame] | 1843 | dd->pdata = match->data; |
Mark A. Greer | 03feec9 | 2012-12-21 10:04:06 -0700 | [diff] [blame] | 1844 | |
| 1845 | err: |
| 1846 | return err; |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 1847 | } |
Mark A. Greer | 03feec9 | 2012-12-21 10:04:06 -0700 | [diff] [blame] | 1848 | #else |
Mark A. Greer | c3c3b32 | 2013-01-15 13:53:02 -0700 | [diff] [blame] | 1849 | static const struct of_device_id omap_sham_of_match[] = { |
| 1850 | {}, |
| 1851 | }; |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 1852 | |
Mark A. Greer | c3c3b32 | 2013-01-15 13:53:02 -0700 | [diff] [blame] | 1853 | static int omap_sham_get_res_of(struct omap_sham_dev *dd, |
Mark A. Greer | 03feec9 | 2012-12-21 10:04:06 -0700 | [diff] [blame] | 1854 | struct device *dev, struct resource *res) |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 1855 | { |
Mark A. Greer | 03feec9 | 2012-12-21 10:04:06 -0700 | [diff] [blame] | 1856 | return -EINVAL; |
| 1857 | } |
| 1858 | #endif |
| 1859 | |
| 1860 | static int omap_sham_get_res_pdev(struct omap_sham_dev *dd, |
| 1861 | struct platform_device *pdev, struct resource *res) |
| 1862 | { |
| 1863 | struct device *dev = &pdev->dev; |
| 1864 | struct resource *r; |
| 1865 | int err = 0; |
| 1866 | |
| 1867 | /* Get the base address */ |
| 1868 | r = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 1869 | if (!r) { |
| 1870 | dev_err(dev, "no MEM resource info\n"); |
| 1871 | err = -ENODEV; |
| 1872 | goto err; |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 1873 | } |
Mark A. Greer | 03feec9 | 2012-12-21 10:04:06 -0700 | [diff] [blame] | 1874 | memcpy(res, r, sizeof(*res)); |
| 1875 | |
| 1876 | /* Get the IRQ */ |
| 1877 | dd->irq = platform_get_irq(pdev, 0); |
| 1878 | if (dd->irq < 0) { |
| 1879 | dev_err(dev, "no IRQ resource info\n"); |
| 1880 | err = dd->irq; |
| 1881 | goto err; |
| 1882 | } |
| 1883 | |
Mark A. Greer | 0d373d6 | 2012-12-21 10:04:08 -0700 | [diff] [blame] | 1884 | /* Only OMAP2/3 can be non-DT */ |
| 1885 | dd->pdata = &omap_sham_pdata_omap2; |
| 1886 | |
Mark A. Greer | 03feec9 | 2012-12-21 10:04:06 -0700 | [diff] [blame] | 1887 | err: |
| 1888 | return err; |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 1889 | } |
| 1890 | |
Greg Kroah-Hartman | 49cfe4d | 2012-12-21 13:14:09 -0800 | [diff] [blame] | 1891 | static int omap_sham_probe(struct platform_device *pdev) |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 1892 | { |
| 1893 | struct omap_sham_dev *dd; |
| 1894 | struct device *dev = &pdev->dev; |
Mark A. Greer | 03feec9 | 2012-12-21 10:04:06 -0700 | [diff] [blame] | 1895 | struct resource res; |
Mark A. Greer | dfd061d | 2012-12-21 10:04:04 -0700 | [diff] [blame] | 1896 | dma_cap_mask_t mask; |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 1897 | int err, i, j; |
Mark A. Greer | 0d373d6 | 2012-12-21 10:04:08 -0700 | [diff] [blame] | 1898 | u32 rev; |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 1899 | |
Lokesh Vutla | 7a7e4b7 | 2013-07-26 12:29:17 +0530 | [diff] [blame] | 1900 | dd = devm_kzalloc(dev, sizeof(struct omap_sham_dev), GFP_KERNEL); |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 1901 | if (dd == NULL) { |
| 1902 | dev_err(dev, "unable to alloc data struct.\n"); |
| 1903 | err = -ENOMEM; |
| 1904 | goto data_err; |
| 1905 | } |
| 1906 | dd->dev = dev; |
| 1907 | platform_set_drvdata(pdev, dd); |
| 1908 | |
| 1909 | INIT_LIST_HEAD(&dd->list); |
| 1910 | spin_lock_init(&dd->lock); |
| 1911 | tasklet_init(&dd->done_task, omap_sham_done_task, (unsigned long)dd); |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 1912 | crypto_init_queue(&dd->queue, OMAP_SHAM_QUEUE_LENGTH); |
| 1913 | |
Mark A. Greer | 03feec9 | 2012-12-21 10:04:06 -0700 | [diff] [blame] | 1914 | err = (dev->of_node) ? omap_sham_get_res_of(dd, dev, &res) : |
| 1915 | omap_sham_get_res_pdev(dd, pdev, &res); |
| 1916 | if (err) |
Lokesh Vutla | 7a7e4b7 | 2013-07-26 12:29:17 +0530 | [diff] [blame] | 1917 | goto data_err; |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 1918 | |
Laurent Navet | 3086228 | 2013-05-02 14:00:38 +0200 | [diff] [blame] | 1919 | dd->io_base = devm_ioremap_resource(dev, &res); |
| 1920 | if (IS_ERR(dd->io_base)) { |
| 1921 | err = PTR_ERR(dd->io_base); |
Lokesh Vutla | 7a7e4b7 | 2013-07-26 12:29:17 +0530 | [diff] [blame] | 1922 | goto data_err; |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 1923 | } |
Mark A. Greer | 03feec9 | 2012-12-21 10:04:06 -0700 | [diff] [blame] | 1924 | dd->phys_base = res.start; |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 1925 | |
Lokesh Vutla | 0de9c38 | 2013-07-26 12:29:16 +0530 | [diff] [blame] | 1926 | err = devm_request_irq(dev, dd->irq, dd->pdata->intr_hdlr, |
| 1927 | IRQF_TRIGGER_NONE, dev_name(dev), dd); |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 1928 | if (err) { |
Lokesh Vutla | 0de9c38 | 2013-07-26 12:29:16 +0530 | [diff] [blame] | 1929 | dev_err(dev, "unable to request irq %d, err = %d\n", |
| 1930 | dd->irq, err); |
Lokesh Vutla | 7a7e4b7 | 2013-07-26 12:29:17 +0530 | [diff] [blame] | 1931 | goto data_err; |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 1932 | } |
| 1933 | |
Mark A. Greer | dfd061d | 2012-12-21 10:04:04 -0700 | [diff] [blame] | 1934 | dma_cap_zero(mask); |
| 1935 | dma_cap_set(DMA_SLAVE, mask); |
| 1936 | |
Peter Ujfalusi | dbe2462 | 2016-04-29 16:03:41 +0300 | [diff] [blame] | 1937 | dd->dma_lch = dma_request_chan(dev, "rx"); |
| 1938 | if (IS_ERR(dd->dma_lch)) { |
| 1939 | err = PTR_ERR(dd->dma_lch); |
| 1940 | if (err == -EPROBE_DEFER) |
| 1941 | goto data_err; |
| 1942 | |
Lokesh Vutla | b8411cc | 2013-08-20 20:32:34 +0530 | [diff] [blame] | 1943 | dd->polling_mode = 1; |
| 1944 | dev_dbg(dev, "using polling mode instead of dma\n"); |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 1945 | } |
| 1946 | |
Mark A. Greer | 0d373d6 | 2012-12-21 10:04:08 -0700 | [diff] [blame] | 1947 | dd->flags |= dd->pdata->flags; |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 1948 | |
Mark A. Greer | b359f03 | 2012-12-21 10:04:02 -0700 | [diff] [blame] | 1949 | pm_runtime_enable(dev); |
Vutla, Lokesh | b0a3d89 | 2015-03-31 09:52:24 +0530 | [diff] [blame] | 1950 | pm_runtime_irq_safe(dev); |
Pali Rohár | 604c310 | 2015-03-08 11:01:01 +0100 | [diff] [blame] | 1951 | |
| 1952 | err = pm_runtime_get_sync(dev); |
| 1953 | if (err < 0) { |
| 1954 | dev_err(dev, "failed to get sync: %d\n", err); |
| 1955 | goto err_pm; |
| 1956 | } |
| 1957 | |
Mark A. Greer | 0d373d6 | 2012-12-21 10:04:08 -0700 | [diff] [blame] | 1958 | rev = omap_sham_read(dd, SHA_REG_REV(dd)); |
| 1959 | pm_runtime_put_sync(&pdev->dev); |
Mark A. Greer | b359f03 | 2012-12-21 10:04:02 -0700 | [diff] [blame] | 1960 | |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 1961 | dev_info(dev, "hw accel on OMAP rev %u.%u\n", |
Mark A. Greer | 0d373d6 | 2012-12-21 10:04:08 -0700 | [diff] [blame] | 1962 | (rev & dd->pdata->major_mask) >> dd->pdata->major_shift, |
| 1963 | (rev & dd->pdata->minor_mask) >> dd->pdata->minor_shift); |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 1964 | |
| 1965 | spin_lock(&sham.lock); |
| 1966 | list_add_tail(&dd->list, &sham.dev_list); |
| 1967 | spin_unlock(&sham.lock); |
| 1968 | |
Mark A. Greer | d20fb18 | 2012-12-21 10:04:09 -0700 | [diff] [blame] | 1969 | for (i = 0; i < dd->pdata->algs_info_size; i++) { |
| 1970 | for (j = 0; j < dd->pdata->algs_info[i].size; j++) { |
| 1971 | err = crypto_register_ahash( |
| 1972 | &dd->pdata->algs_info[i].algs_list[j]); |
| 1973 | if (err) |
| 1974 | goto err_algs; |
| 1975 | |
| 1976 | dd->pdata->algs_info[i].registered++; |
| 1977 | } |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 1978 | } |
| 1979 | |
| 1980 | return 0; |
| 1981 | |
| 1982 | err_algs: |
Mark A. Greer | d20fb18 | 2012-12-21 10:04:09 -0700 | [diff] [blame] | 1983 | for (i = dd->pdata->algs_info_size - 1; i >= 0; i--) |
| 1984 | for (j = dd->pdata->algs_info[i].registered - 1; j >= 0; j--) |
| 1985 | crypto_unregister_ahash( |
| 1986 | &dd->pdata->algs_info[i].algs_list[j]); |
Pali Rohár | 604c310 | 2015-03-08 11:01:01 +0100 | [diff] [blame] | 1987 | err_pm: |
Mark A. Greer | b359f03 | 2012-12-21 10:04:02 -0700 | [diff] [blame] | 1988 | pm_runtime_disable(dev); |
Dan Carpenter | d462e32 | 2016-05-18 13:39:05 +0300 | [diff] [blame^] | 1989 | if (!dd->polling_mode) |
Mark A. Greer | f13ab86 | 2013-11-12 13:12:27 -0700 | [diff] [blame] | 1990 | dma_release_channel(dd->dma_lch); |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 1991 | data_err: |
| 1992 | dev_err(dev, "initialization failed.\n"); |
| 1993 | |
| 1994 | return err; |
| 1995 | } |
| 1996 | |
Greg Kroah-Hartman | 49cfe4d | 2012-12-21 13:14:09 -0800 | [diff] [blame] | 1997 | static int omap_sham_remove(struct platform_device *pdev) |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 1998 | { |
| 1999 | static struct omap_sham_dev *dd; |
Mark A. Greer | d20fb18 | 2012-12-21 10:04:09 -0700 | [diff] [blame] | 2000 | int i, j; |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 2001 | |
| 2002 | dd = platform_get_drvdata(pdev); |
| 2003 | if (!dd) |
| 2004 | return -ENODEV; |
| 2005 | spin_lock(&sham.lock); |
| 2006 | list_del(&dd->list); |
| 2007 | spin_unlock(&sham.lock); |
Mark A. Greer | d20fb18 | 2012-12-21 10:04:09 -0700 | [diff] [blame] | 2008 | for (i = dd->pdata->algs_info_size - 1; i >= 0; i--) |
| 2009 | for (j = dd->pdata->algs_info[i].registered - 1; j >= 0; j--) |
| 2010 | crypto_unregister_ahash( |
| 2011 | &dd->pdata->algs_info[i].algs_list[j]); |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 2012 | tasklet_kill(&dd->done_task); |
Mark A. Greer | b359f03 | 2012-12-21 10:04:02 -0700 | [diff] [blame] | 2013 | pm_runtime_disable(&pdev->dev); |
Mark A. Greer | f13ab86 | 2013-11-12 13:12:27 -0700 | [diff] [blame] | 2014 | |
Peter Ujfalusi | dbe2462 | 2016-04-29 16:03:41 +0300 | [diff] [blame] | 2015 | if (!dd->polling_mode) |
Mark A. Greer | f13ab86 | 2013-11-12 13:12:27 -0700 | [diff] [blame] | 2016 | dma_release_channel(dd->dma_lch); |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 2017 | |
| 2018 | return 0; |
| 2019 | } |
| 2020 | |
Mark A. Greer | 3b3f440 | 2012-12-21 10:04:03 -0700 | [diff] [blame] | 2021 | #ifdef CONFIG_PM_SLEEP |
| 2022 | static int omap_sham_suspend(struct device *dev) |
| 2023 | { |
| 2024 | pm_runtime_put_sync(dev); |
| 2025 | return 0; |
| 2026 | } |
| 2027 | |
| 2028 | static int omap_sham_resume(struct device *dev) |
| 2029 | { |
Pali Rohár | 604c310 | 2015-03-08 11:01:01 +0100 | [diff] [blame] | 2030 | int err = pm_runtime_get_sync(dev); |
| 2031 | if (err < 0) { |
| 2032 | dev_err(dev, "failed to get sync: %d\n", err); |
| 2033 | return err; |
| 2034 | } |
Mark A. Greer | 3b3f440 | 2012-12-21 10:04:03 -0700 | [diff] [blame] | 2035 | return 0; |
| 2036 | } |
| 2037 | #endif |
| 2038 | |
Jingoo Han | ae12fe2 | 2014-02-27 20:33:32 +0900 | [diff] [blame] | 2039 | static SIMPLE_DEV_PM_OPS(omap_sham_pm_ops, omap_sham_suspend, omap_sham_resume); |
Mark A. Greer | 3b3f440 | 2012-12-21 10:04:03 -0700 | [diff] [blame] | 2040 | |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 2041 | static struct platform_driver omap_sham_driver = { |
| 2042 | .probe = omap_sham_probe, |
| 2043 | .remove = omap_sham_remove, |
| 2044 | .driver = { |
| 2045 | .name = "omap-sham", |
Mark A. Greer | 3b3f440 | 2012-12-21 10:04:03 -0700 | [diff] [blame] | 2046 | .pm = &omap_sham_pm_ops, |
Mark A. Greer | 03feec9 | 2012-12-21 10:04:06 -0700 | [diff] [blame] | 2047 | .of_match_table = omap_sham_of_match, |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 2048 | }, |
| 2049 | }; |
| 2050 | |
Sachin Kamat | 0261370 | 2013-03-04 15:09:43 +0530 | [diff] [blame] | 2051 | module_platform_driver(omap_sham_driver); |
Dmitry Kasatkin | 8628e7c | 2010-05-03 11:10:59 +0800 | [diff] [blame] | 2052 | |
| 2053 | MODULE_DESCRIPTION("OMAP SHA1/MD5 hw acceleration support."); |
| 2054 | MODULE_LICENSE("GPL v2"); |
| 2055 | MODULE_AUTHOR("Dmitry Kasatkin"); |
Joni Lapilainen | 718249d | 2013-10-26 23:00:41 +0200 | [diff] [blame] | 2056 | MODULE_ALIAS("platform:omap-sham"); |