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