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