blob: 855898977d38539b2443043e45952bb9a198e403 [file] [log] [blame]
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001/*
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. Greer0d373d62012-12-21 10:04:08 -07008 * Copyright (c) 2011 Texas Instruments Incorporated
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08009 *
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 Kasatkin8628e7c2010-05-03 11:10:59 +080019#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 Kasatkin8628e7c2010-05-03 11:10:59 +080026#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. Greerdfd061d2012-12-21 10:04:04 -070031#include <linux/dmaengine.h>
Mark A. Greerb359f032012-12-21 10:04:02 -070032#include <linux/pm_runtime.h>
Mark A. Greer03feec92012-12-21 10:04:06 -070033#include <linux/of.h>
34#include <linux/of_device.h>
35#include <linux/of_address.h>
36#include <linux/of_irq.h>
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +080037#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 Kasatkin8628e7c2010-05-03 11:10:59 +080046#define MD5_DIGEST_SIZE 16
47
Mark A. Greer0d373d62012-12-21 10:04:08 -070048#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 Vutlaeaef7e32013-07-26 12:29:14 +053052#define SHA_REG_ODIGEST(dd, x) ((dd)->pdata->odigest_ofs + (x * 0x04))
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +080053
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. Greer0d373d62012-12-21 10:04:08 -070062#define SHA_REG_REV(dd) ((dd)->pdata->rev_ofs)
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +080063
Mark A. Greer0d373d62012-12-21 10:04:08 -070064#define SHA_REG_MASK(dd) ((dd)->pdata->mask_ofs)
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +080065#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. Greer0d373d62012-12-21 10:04:08 -070070#define SHA_REG_SYSSTATUS(dd) ((dd)->pdata->sysstatus_ofs)
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +080071#define SHA_REG_SYSSTATUS_RESETDONE (1 << 0)
72
Lokesh Vutlaeaef7e32013-07-26 12:29:14 +053073#define SHA_REG_MODE(dd) ((dd)->pdata->mode_ofs)
Mark A. Greer0d373d62012-12-21 10:04:08 -070074#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. Greer0d373d62012-12-21 10:04:08 -070078
Lokesh Vutlaeaef7e32013-07-26 12:29:14 +053079#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. Greer0d373d62012-12-21 10:04:08 -070088
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 Kasatkin8628e7c2010-05-03 11:10:59 +0800101#define DEFAULT_TIMEOUT_INTERVAL HZ
102
Tero Kristoe93f7672016-06-22 16:23:34 +0300103#define DEFAULT_AUTOSUSPEND_DELAY 1000
104
Dmitry Kasatkinea1fd222011-06-02 21:10:05 +0300105/* 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 Kasatkin6c63db82011-06-02 21:10:10 +0300112#define FLAGS_DMA_READY 6
Mark A. Greer0d373d62012-12-21 10:04:08 -0700113#define FLAGS_AUTO_XOR 7
114#define FLAGS_BE32_SHA1 8
Tero Kristof19de1b2016-09-19 18:22:15 +0300115#define FLAGS_SGS_COPIED 9
116#define FLAGS_SGS_ALLOCED 10
Dmitry Kasatkinea1fd222011-06-02 21:10:05 +0300117/* context flags */
118#define FLAGS_FINUP 16
119#define FLAGS_SG 17
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800120
Mark A. Greer0d373d62012-12-21 10:04:08 -0700121#define FLAGS_MODE_SHIFT 18
Lokesh Vutlaeaef7e32013-07-26 12:29:14 +0530122#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. Greer0d373d62012-12-21 10:04:08 -0700132
133#define OP_UPDATE 1
134#define OP_FINAL 2
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800135
Dmitry Kasatkin798eed5d2010-11-19 16:04:26 +0200136#define OMAP_ALIGN_MASK (sizeof(u32)-1)
137#define OMAP_ALIGNED __attribute__((aligned(sizeof(u32))))
138
Mark A. Greer0d373d62012-12-21 10:04:08 -0700139#define BUFLEN PAGE_SIZE
Dmitry Kasatkin798eed5d2010-11-19 16:04:26 +0200140
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800141struct omap_sham_dev;
142
143struct omap_sham_reqctx {
144 struct omap_sham_dev *dd;
145 unsigned long flags;
146 unsigned long op;
147
Lokesh Vutlaeaef7e32013-07-26 12:29:14 +0530148 u8 digest[SHA512_DIGEST_SIZE] OMAP_ALIGNED;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800149 size_t digcnt;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800150 size_t bufcnt;
151 size_t buflen;
152 dma_addr_t dma_addr;
153
154 /* walk state */
155 struct scatterlist *sg;
Tero Kristof19de1b2016-09-19 18:22:15 +0300156 struct scatterlist sgl[2];
Tero Kristo8addf572016-09-19 18:22:14 +0300157 struct scatterlist sgl_tmp;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800158 unsigned int offset; /* offset in current sg */
Tero Kristof19de1b2016-09-19 18:22:15 +0300159 int sg_len;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800160 unsigned int total; /* total request */
Dmitry Kasatkin798eed5d2010-11-19 16:04:26 +0200161
162 u8 buffer[0] OMAP_ALIGNED;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800163};
164
165struct omap_sham_hmac_ctx {
166 struct crypto_shash *shash;
Lokesh Vutlaeaef7e32013-07-26 12:29:14 +0530167 u8 ipad[SHA512_BLOCK_SIZE] OMAP_ALIGNED;
168 u8 opad[SHA512_BLOCK_SIZE] OMAP_ALIGNED;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800169};
170
171struct 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 Kristo65e7a542016-06-22 16:23:35 +0300182#define OMAP_SHAM_QUEUE_LENGTH 10
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800183
Mark A. Greerd20fb182012-12-21 10:04:09 -0700184struct omap_sham_algs_info {
185 struct ahash_alg *algs_list;
186 unsigned int size;
187 unsigned int registered;
188};
189
Mark A. Greer0d373d62012-12-21 10:04:08 -0700190struct omap_sham_pdata {
Mark A. Greerd20fb182012-12-21 10:04:09 -0700191 struct omap_sham_algs_info *algs_info;
192 unsigned int algs_info_size;
Mark A. Greer0d373d62012-12-21 10:04:08 -0700193 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 Vutlaeaef7e32013-07-26 12:29:14 +0530210 u32 mode_ofs;
211 u32 length_ofs;
Mark A. Greer0d373d62012-12-21 10:04:08 -0700212
213 u32 major_mask;
214 u32 major_shift;
215 u32 minor_mask;
216 u32 minor_shift;
217};
218
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800219struct 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 Kasatkin8628e7c2010-05-03 11:10:59 +0800225 spinlock_t lock;
Dmitry Kasatkin3e133c82010-11-19 16:04:24 +0200226 int err;
Mark A. Greerdfd061d2012-12-21 10:04:04 -0700227 struct dma_chan *dma_lch;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800228 struct tasklet_struct done_task;
Lokesh Vutlab8411cc2013-08-20 20:32:34 +0530229 u8 polling_mode;
Tero Kristof19de1b2016-09-19 18:22:15 +0300230 u8 xmit_buf[BUFLEN];
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800231
232 unsigned long flags;
233 struct crypto_queue queue;
234 struct ahash_request *req;
Mark A. Greer0d373d62012-12-21 10:04:08 -0700235
236 const struct omap_sham_pdata *pdata;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800237};
238
239struct omap_sham_drv {
240 struct list_head dev_list;
241 spinlock_t lock;
242 unsigned long flags;
243};
244
245static struct omap_sham_drv sham = {
246 .dev_list = LIST_HEAD_INIT(sham.dev_list),
247 .lock = __SPIN_LOCK_UNLOCKED(sham.lock),
248};
249
250static inline u32 omap_sham_read(struct omap_sham_dev *dd, u32 offset)
251{
252 return __raw_readl(dd->io_base + offset);
253}
254
255static 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
261static 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
272static 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. Greer0d373d62012-12-21 10:04:08 -0700284static void omap_sham_copy_hash_omap2(struct ahash_request *req, int out)
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800285{
286 struct omap_sham_reqctx *ctx = ahash_request_ctx(req);
Mark A. Greer0d373d62012-12-21 10:04:08 -0700287 struct omap_sham_dev *dd = ctx->dd;
Dmitry Kasatkin0c3cf4c2010-11-19 16:04:22 +0200288 u32 *hash = (u32 *)ctx->digest;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800289 int i;
290
Mark A. Greer0d373d62012-12-21 10:04:08 -0700291 for (i = 0; i < dd->pdata->digest_size / sizeof(u32); i++) {
Dmitry Kasatkin3c8d7582010-11-19 16:04:27 +0200292 if (out)
Mark A. Greer0d373d62012-12-21 10:04:08 -0700293 hash[i] = omap_sham_read(dd, SHA_REG_IDIGEST(dd, i));
Dmitry Kasatkin3c8d7582010-11-19 16:04:27 +0200294 else
Mark A. Greer0d373d62012-12-21 10:04:08 -0700295 omap_sham_write(dd, SHA_REG_IDIGEST(dd, i), hash[i]);
Dmitry Kasatkin3c8d7582010-11-19 16:04:27 +0200296 }
297}
298
Mark A. Greer0d373d62012-12-21 10:04:08 -0700299static 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 Vutlaeaef7e32013-07-26 12:29:14 +0530314 SHA_REG_ODIGEST(dd, i));
Mark A. Greer0d373d62012-12-21 10:04:08 -0700315 else
Lokesh Vutlaeaef7e32013-07-26 12:29:14 +0530316 omap_sham_write(dd, SHA_REG_ODIGEST(dd, i),
Mark A. Greer0d373d62012-12-21 10:04:08 -0700317 opad[i]);
318 }
319 }
320
321 omap_sham_copy_hash_omap2(req, out);
322}
323
Dmitry Kasatkin3c8d7582010-11-19 16:04:27 +0200324static 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. Greer0d373d62012-12-21 10:04:08 -0700329 int i, d, big_endian = 0;
Dmitry Kasatkin3c8d7582010-11-19 16:04:27 +0200330
331 if (!hash)
332 return;
333
Mark A. Greer0d373d62012-12-21 10:04:08 -0700334 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. Greerd20fb182012-12-21 10:04:09 -0700344 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 Vutlaeaef7e32013-07-26 12:29:14 +0530350 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. Greer0d373d62012-12-21 10:04:08 -0700356 default:
357 d = 0;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800358 }
Mark A. Greer0d373d62012-12-21 10:04:08 -0700359
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 Kasatkin8628e7c2010-05-03 11:10:59 +0800366}
367
Dmitry Kasatkin798eed5d2010-11-19 16:04:26 +0200368static int omap_sham_hw_init(struct omap_sham_dev *dd)
369{
Pali Rohár604c3102015-03-08 11:01:01 +0100370 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 Kasatkin798eed5d2010-11-19 16:04:26 +0200377
Dmitry Kasatkina929cbe2011-06-02 21:10:06 +0300378 if (!test_bit(FLAGS_INIT, &dd->flags)) {
Dmitry Kasatkina929cbe2011-06-02 21:10:06 +0300379 set_bit(FLAGS_INIT, &dd->flags);
Dmitry Kasatkin798eed5d2010-11-19 16:04:26 +0200380 dd->err = 0;
381 }
382
383 return 0;
384}
385
Mark A. Greer0d373d62012-12-21 10:04:08 -0700386static void omap_sham_write_ctrl_omap2(struct omap_sham_dev *dd, size_t length,
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800387 int final, int dma)
388{
389 struct omap_sham_reqctx *ctx = ahash_request_ctx(dd->req);
390 u32 val = length << 5, mask;
391
Dmitry Kasatkin798eed5d2010-11-19 16:04:26 +0200392 if (likely(ctx->digcnt))
Mark A. Greer0d373d62012-12-21 10:04:08 -0700393 omap_sham_write(dd, SHA_REG_DIGCNT(dd), ctx->digcnt);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800394
Mark A. Greer0d373d62012-12-21 10:04:08 -0700395 omap_sham_write_mask(dd, SHA_REG_MASK(dd),
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800396 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. Greer0d373d62012-12-21 10:04:08 -0700402 if ((ctx->flags & FLAGS_MODE_MASK) == FLAGS_MODE_SHA1)
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800403 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 Kasatkin8628e7c2010-05-03 11:10:59 +0800413}
414
Mark A. Greer0d373d62012-12-21 10:04:08 -0700415static void omap_sham_trigger_omap2(struct omap_sham_dev *dd, size_t length)
416{
417}
418
419static 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 Vutlaeaef7e32013-07-26 12:29:14 +0530424static 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. Greer0d373d62012-12-21 10:04:08 -0700448static 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
455static 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 Vutlaeaef7e32013-07-26 12:29:14 +0530466 val = (ctx->flags & FLAGS_MODE_MASK) >> (FLAGS_MODE_SHIFT);
Mark A. Greer0d373d62012-12-21 10:04:08 -0700467 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 Vutlaeaef7e32013-07-26 12:29:14 +0530471 int bs, nr_dr;
Mark A. Greer0d373d62012-12-21 10:04:08 -0700472
473 val |= SHA_REG_MODE_ALGO_CONSTANT;
474
475 if (ctx->flags & BIT(FLAGS_HMAC)) {
Lokesh Vutlaeaef7e32013-07-26 12:29:14 +0530476 bs = get_block_size(ctx);
477 nr_dr = bs / (2 * sizeof(u32));
Mark A. Greer0d373d62012-12-21 10:04:08 -0700478 val |= SHA_REG_MODE_HMAC_KEY_PROC;
Lokesh Vutlaeaef7e32013-07-26 12:29:14 +0530479 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. Greer0d373d62012-12-21 10:04:08 -0700484 }
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 Vutlaeaef7e32013-07-26 12:29:14 +0530499 omap_sham_write_mask(dd, SHA_REG_MODE(dd), val, mask);
Mark A. Greer0d373d62012-12-21 10:04:08 -0700500 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
507static void omap_sham_trigger_omap4(struct omap_sham_dev *dd, size_t length)
508{
Lokesh Vutlaeaef7e32013-07-26 12:29:14 +0530509 omap_sham_write(dd, SHA_REG_LENGTH(dd), length);
Mark A. Greer0d373d62012-12-21 10:04:08 -0700510}
511
512static 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 Kasatkin8628e7c2010-05-03 11:10:59 +0800518static 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 Vutlab8411cc2013-08-20 20:32:34 +0530522 int count, len32, bs32, offset = 0;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800523 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. Greer0d373d62012-12-21 10:04:08 -0700528 dd->pdata->write_ctrl(dd, length, final, 0);
529 dd->pdata->trigger(dd, length);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800530
Dmitry Kasatkin3e133c82010-11-19 16:04:24 +0200531 /* should be non-zero before next lines to disable clocks later */
532 ctx->digcnt += length;
533
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800534 if (final)
Dmitry Kasatkined3ea9a82011-06-02 21:10:07 +0300535 set_bit(FLAGS_FINAL, &dd->flags); /* catch last interrupt */
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800536
Dmitry Kasatkin6c63db82011-06-02 21:10:10 +0300537 set_bit(FLAGS_CPU, &dd->flags);
538
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800539 len32 = DIV_ROUND_UP(length, sizeof(u32));
Lokesh Vutlab8411cc2013-08-20 20:32:34 +0530540 bs32 = get_block_size(ctx) / sizeof(u32);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800541
Lokesh Vutlab8411cc2013-08-20 20:32:34 +0530542 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 Kasatkin8628e7c2010-05-03 11:10:59 +0800551
552 return -EINPROGRESS;
553}
554
Mark A. Greerdfd061d2012-12-21 10:04:04 -0700555static 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. Greerdfd061d2012-12-21 10:04:04 -0700562
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800563static int omap_sham_xmit_dma(struct omap_sham_dev *dd, dma_addr_t dma_addr,
Mark A. Greerdfd061d2012-12-21 10:04:04 -0700564 size_t length, int final, int is_sg)
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800565{
566 struct omap_sham_reqctx *ctx = ahash_request_ctx(dd->req);
Mark A. Greerdfd061d2012-12-21 10:04:04 -0700567 struct dma_async_tx_descriptor *tx;
568 struct dma_slave_config cfg;
Lokesh Vutlaf5e46262013-08-20 20:32:35 +0530569 int len32, ret, dma_min = get_block_size(ctx);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800570
571 dev_dbg(dd->dev, "xmit_dma: digcnt: %d, length: %d, final: %d\n",
572 ctx->digcnt, length, final);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800573
Mark A. Greerdfd061d2012-12-21 10:04:04 -0700574 memset(&cfg, 0, sizeof(cfg));
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800575
Mark A. Greer0d373d62012-12-21 10:04:08 -0700576 cfg.dst_addr = dd->phys_base + SHA_REG_DIN(dd, 0);
Mark A. Greerdfd061d2012-12-21 10:04:04 -0700577 cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
Lokesh Vutlaf5e46262013-08-20 20:32:35 +0530578 cfg.dst_maxburst = dma_min / DMA_SLAVE_BUSWIDTH_4_BYTES;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800579
Mark A. Greerdfd061d2012-12-21 10:04:04 -0700580 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 Kasatkin8628e7c2010-05-03 11:10:59 +0800585
Lokesh Vutlaf5e46262013-08-20 20:32:35 +0530586 len32 = DIV_ROUND_UP(length, dma_min) * dma_min;
Mark A. Greerdfd061d2012-12-21 10:04:04 -0700587
588 if (is_sg) {
589 /*
590 * The SG entry passed in may not have the 'length' member
Tero Kristo8addf572016-09-19 18:22:14 +0300591 * set correctly so use a local SG entry (sgl_tmp) with the
Mark A. Greerdfd061d2012-12-21 10:04:04 -0700592 * proper value for 'length' instead. If this is not done,
593 * the dmaengine may try to DMA the incorrect amount of data.
594 */
Tero Kristo8addf572016-09-19 18:22:14 +0300595 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. Greerdfd061d2012-12-21 10:04:04 -0700600
Tero Kristo8addf572016-09-19 18:22:14 +0300601 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. Greerdfd061d2012-12-21 10:04:04 -0700604 } 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. Greerdfd061d2012-12-21 10:04:04 -0700616
Mark A. Greer0d373d62012-12-21 10:04:08 -0700617 dd->pdata->write_ctrl(dd, length, final, 1);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800618
619 ctx->digcnt += length;
620
621 if (final)
Dmitry Kasatkined3ea9a82011-06-02 21:10:07 +0300622 set_bit(FLAGS_FINAL, &dd->flags); /* catch last interrupt */
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800623
Dmitry Kasatkina929cbe2011-06-02 21:10:06 +0300624 set_bit(FLAGS_DMA_ACTIVE, &dd->flags);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800625
Mark A. Greerdfd061d2012-12-21 10:04:04 -0700626 dmaengine_submit(tx);
627 dma_async_issue_pending(dd->dma_lch);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800628
Mark A. Greer0d373d62012-12-21 10:04:08 -0700629 dd->pdata->trigger(dd, length);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800630
631 return -EINPROGRESS;
632}
633
Tero Kristof19de1b2016-09-19 18:22:15 +0300634static 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
691static 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
724static 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
792static 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 Kasatkin8628e7c2010-05-03 11:10:59 +0800888static 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
902static size_t omap_sham_append_sg(struct omap_sham_reqctx *ctx)
903{
904 size_t count;
Joel Fernandes26a05482014-03-07 10:28:46 -0600905 const u8 *vaddr;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800906
907 while (ctx->sg) {
Joel Fernandes26a05482014-03-07 10:28:46 -0600908 vaddr = kmap_atomic(sg_page(ctx->sg));
Vutla, Lokesh13cf3942015-04-02 15:32:45 +0530909 vaddr += ctx->sg->offset;
Joel Fernandes26a05482014-03-07 10:28:46 -0600910
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800911 count = omap_sham_append_buffer(ctx,
Joel Fernandes26a05482014-03-07 10:28:46 -0600912 vaddr + ctx->offset,
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800913 ctx->sg->length - ctx->offset);
Joel Fernandes26a05482014-03-07 10:28:46 -0600914
915 kunmap_atomic((void *)vaddr);
916
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800917 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 Kasatkin798eed5d2010-11-19 16:04:26 +0200933static 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. Greerdfd061d2012-12-21 10:04:04 -0700937 int ret;
938
Dmitry Kasatkin798eed5d2010-11-19 16:04:26 +0200939 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 Kasatkinea1fd222011-06-02 21:10:05 +0300946 ctx->flags &= ~BIT(FLAGS_SG);
Dmitry Kasatkin887c8832010-11-19 16:04:29 +0200947
Mark A. Greerdfd061d2012-12-21 10:04:04 -0700948 ret = omap_sham_xmit_dma(dd, ctx->dma_addr, length, final, 0);
Mark A. Greer0d373d62012-12-21 10:04:08 -0700949 if (ret != -EINPROGRESS)
Mark A. Greerdfd061d2012-12-21 10:04:04 -0700950 dma_unmap_single(dd->dev, ctx->dma_addr, ctx->buflen,
951 DMA_TO_DEVICE);
952
953 return ret;
Dmitry Kasatkin798eed5d2010-11-19 16:04:26 +0200954}
955
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800956static 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 Kasatkin8628e7c2010-05-03 11:10:59 +0800962 omap_sham_append_sg(ctx);
963
Dmitry Kasatkinea1fd222011-06-02 21:10:05 +0300964 final = (ctx->flags & BIT(FLAGS_FINUP)) && !ctx->total;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800965
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 Kasatkin798eed5d2010-11-19 16:04:26 +0200972 return omap_sham_xmit_dma_map(dd, ctx, count, final);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800973 }
974
975 return 0;
976}
977
Dmitry Kasatkin887c8832010-11-19 16:04:29 +0200978/* Start address alignment */
979#define SG_AA(sg) (IS_ALIGNED(sg->offset, sizeof(u32)))
980/* SHA1 block size alignment */
Lokesh Vutlaeaef7e32013-07-26 12:29:14 +0530981#define SG_SA(sg, bs) (IS_ALIGNED(sg->length, bs))
Dmitry Kasatkin887c8832010-11-19 16:04:29 +0200982
983static int omap_sham_update_dma_start(struct omap_sham_dev *dd)
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800984{
985 struct omap_sham_reqctx *ctx = ahash_request_ctx(dd->req);
Dmitry Kasatkin887c8832010-11-19 16:04:29 +0200986 unsigned int length, final, tail;
987 struct scatterlist *sg;
Lokesh Vutlaeaef7e32013-07-26 12:29:14 +0530988 int ret, bs;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800989
Dmitry Kasatkin887c8832010-11-19 16:04:29 +0200990 if (!ctx->total)
991 return 0;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800992
Dmitry Kasatkin887c8832010-11-19 16:04:29 +0200993 if (ctx->bufcnt || ctx->offset)
994 return omap_sham_update_dma_slow(dd);
995
Mark A. Greerdfd061d2012-12-21 10:04:04 -0700996 /*
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 Vutlaf5e46262013-08-20 20:32:35 +05301002 if (ctx->total < get_block_size(ctx))
Mark A. Greerdfd061d2012-12-21 10:04:04 -07001003 return omap_sham_update_dma_slow(dd);
Mark A. Greerdfd061d2012-12-21 10:04:04 -07001004
Dmitry Kasatkin887c8832010-11-19 16:04:29 +02001005 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 Vutlaeaef7e32013-07-26 12:29:14 +05301009 bs = get_block_size(ctx);
Dmitry Kasatkin887c8832010-11-19 16:04:29 +02001010
1011 if (!SG_AA(sg))
1012 return omap_sham_update_dma_slow(dd);
1013
Lokesh Vutlaeaef7e32013-07-26 12:29:14 +05301014 if (!sg_is_last(sg) && !SG_SA(sg, bs))
1015 /* size is not BLOCK_SIZE aligned */
Dmitry Kasatkin887c8832010-11-19 16:04:29 +02001016 return omap_sham_update_dma_slow(dd);
1017
1018 length = min(ctx->total, sg->length);
1019
1020 if (sg_is_last(sg)) {
Dmitry Kasatkinea1fd222011-06-02 21:10:05 +03001021 if (!(ctx->flags & BIT(FLAGS_FINUP))) {
Lokesh Vutlaeaef7e32013-07-26 12:29:14 +05301022 /* not last sg must be BLOCK_SIZE aligned */
1023 tail = length & (bs - 1);
Dmitry Kasatkin887c8832010-11-19 16:04:29 +02001024 /* without finup() we need one block to close hash */
1025 if (!tail)
Lokesh Vutlaeaef7e32013-07-26 12:29:14 +05301026 tail = bs;
Dmitry Kasatkin887c8832010-11-19 16:04:29 +02001027 length -= tail;
1028 }
1029 }
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001030
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 Kasatkinea1fd222011-06-02 21:10:05 +03001036 ctx->flags |= BIT(FLAGS_SG);
Dmitry Kasatkin887c8832010-11-19 16:04:29 +02001037
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001038 ctx->total -= length;
Dmitry Kasatkin887c8832010-11-19 16:04:29 +02001039 ctx->offset = length; /* offset where to start slow */
1040
Dmitry Kasatkinea1fd222011-06-02 21:10:05 +03001041 final = (ctx->flags & BIT(FLAGS_FINUP)) && !ctx->total;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001042
Mark A. Greerdfd061d2012-12-21 10:04:04 -07001043 ret = omap_sham_xmit_dma(dd, sg_dma_address(ctx->sg), length, final, 1);
Mark A. Greer0d373d62012-12-21 10:04:08 -07001044 if (ret != -EINPROGRESS)
Mark A. Greerdfd061d2012-12-21 10:04:04 -07001045 dma_unmap_sg(dd->dev, ctx->sg, 1, DMA_TO_DEVICE);
1046
1047 return ret;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001048}
1049
1050static int omap_sham_update_cpu(struct omap_sham_dev *dd)
1051{
1052 struct omap_sham_reqctx *ctx = ahash_request_ctx(dd->req);
Lokesh Vutlab8411cc2013-08-20 20:32:34 +05301053 int bufcnt, final;
1054
1055 if (!ctx->total)
1056 return 0;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001057
1058 omap_sham_append_sg(ctx);
Lokesh Vutlab8411cc2013-08-20 20:32:34 +05301059
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 Vutlaacef7b02013-12-18 19:03:33 +05301065 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 Kasatkin8628e7c2010-05-03 11:10:59 +08001070
Lokesh Vutlaacef7b02013-12-18 19:03:33 +05301071 return 0;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001072}
1073
1074static 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. Greerdfd061d2012-12-21 10:04:04 -07001078
Dmitry Kasatkinea1fd222011-06-02 21:10:05 +03001079 if (ctx->flags & BIT(FLAGS_SG)) {
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001080 dma_unmap_sg(dd->dev, ctx->sg, 1, DMA_TO_DEVICE);
Dmitry Kasatkin887c8832010-11-19 16:04:29 +02001081 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 Kasatkin798eed5d2010-11-19 16:04:26 +02001087 dma_unmap_single(dd->dev, ctx->dma_addr, ctx->buflen,
1088 DMA_TO_DEVICE);
Dmitry Kasatkin887c8832010-11-19 16:04:29 +02001089 }
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001090
1091 return 0;
1092}
1093
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001094static 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 Vutlaeaef7e32013-07-26 12:29:14 +05301100 int bs = 0;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001101
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 Kasatkin8628e7c2010-05-03 11:10:59 +08001118 dev_dbg(dd->dev, "init: digest size: %d\n",
1119 crypto_ahash_digestsize(tfm));
1120
Mark A. Greer0d373d62012-12-21 10:04:08 -07001121 switch (crypto_ahash_digestsize(tfm)) {
1122 case MD5_DIGEST_SIZE:
1123 ctx->flags |= FLAGS_MODE_MD5;
Lokesh Vutlaeaef7e32013-07-26 12:29:14 +05301124 bs = SHA1_BLOCK_SIZE;
Mark A. Greer0d373d62012-12-21 10:04:08 -07001125 break;
1126 case SHA1_DIGEST_SIZE:
1127 ctx->flags |= FLAGS_MODE_SHA1;
Lokesh Vutlaeaef7e32013-07-26 12:29:14 +05301128 bs = SHA1_BLOCK_SIZE;
Mark A. Greer0d373d62012-12-21 10:04:08 -07001129 break;
Mark A. Greerd20fb182012-12-21 10:04:09 -07001130 case SHA224_DIGEST_SIZE:
1131 ctx->flags |= FLAGS_MODE_SHA224;
Lokesh Vutlaeaef7e32013-07-26 12:29:14 +05301132 bs = SHA224_BLOCK_SIZE;
Mark A. Greerd20fb182012-12-21 10:04:09 -07001133 break;
1134 case SHA256_DIGEST_SIZE:
1135 ctx->flags |= FLAGS_MODE_SHA256;
Lokesh Vutlaeaef7e32013-07-26 12:29:14 +05301136 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. Greerd20fb182012-12-21 10:04:09 -07001145 break;
Mark A. Greer0d373d62012-12-21 10:04:08 -07001146 }
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001147
1148 ctx->bufcnt = 0;
1149 ctx->digcnt = 0;
Dmitry Kasatkin798eed5d2010-11-19 16:04:26 +02001150 ctx->buflen = BUFLEN;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001151
Dmitry Kasatkinea1fd222011-06-02 21:10:05 +03001152 if (tctx->flags & BIT(FLAGS_HMAC)) {
Mark A. Greer0d373d62012-12-21 10:04:08 -07001153 if (!test_bit(FLAGS_AUTO_XOR, &dd->flags)) {
1154 struct omap_sham_hmac_ctx *bctx = tctx->base;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001155
Lokesh Vutlaeaef7e32013-07-26 12:29:14 +05301156 memcpy(ctx->buffer, bctx->ipad, bs);
1157 ctx->bufcnt = bs;
Mark A. Greer0d373d62012-12-21 10:04:08 -07001158 }
1159
Dmitry Kasatkinea1fd222011-06-02 21:10:05 +03001160 ctx->flags |= BIT(FLAGS_HMAC);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001161 }
1162
1163 return 0;
1164
1165}
1166
1167static 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 Kasatkinea1fd222011-06-02 21:10:05 +03001174 ctx->total, ctx->digcnt, (ctx->flags & BIT(FLAGS_FINUP)) != 0);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001175
Dmitry Kasatkinea1fd222011-06-02 21:10:05 +03001176 if (ctx->flags & BIT(FLAGS_CPU))
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001177 err = omap_sham_update_cpu(dd);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001178 else
Dmitry Kasatkin887c8832010-11-19 16:04:29 +02001179 err = omap_sham_update_dma_start(dd);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001180
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
1187static 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 Vutlab8411cc2013-08-20 20:32:34 +05301193 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 Kasatkin8628e7c2010-05-03 11:10:59 +08001198 use_dma = 0;
1199
1200 if (use_dma)
Dmitry Kasatkin798eed5d2010-11-19 16:04:26 +02001201 err = omap_sham_xmit_dma_map(dd, ctx, ctx->bufcnt, 1);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001202 else
1203 err = omap_sham_xmit_cpu(dd, ctx->buffer, ctx->bufcnt, 1);
1204
1205 ctx->bufcnt = 0;
1206
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001207 dev_dbg(dd->dev, "final_req: err: %d\n", err);
1208
1209 return err;
1210}
1211
Dmitry Kasatkinbf362752011-04-20 13:34:58 +03001212static int omap_sham_finish_hmac(struct ahash_request *req)
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001213{
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 Webster7bc53c32014-04-04 18:18:00 -03001218 SHASH_DESC_ON_STACK(shash, bctx->shash);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001219
Behan Webster7bc53c32014-04-04 18:18:00 -03001220 shash->tfm = bctx->shash;
1221 shash->flags = 0; /* not CRYPTO_TFM_REQ_MAY_SLEEP */
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001222
Behan Webster7bc53c32014-04-04 18:18:00 -03001223 return crypto_shash_init(shash) ?:
1224 crypto_shash_update(shash, bctx->opad, bs) ?:
1225 crypto_shash_finup(shash, req->result, ds, req->result);
Dmitry Kasatkinbf362752011-04-20 13:34:58 +03001226}
1227
1228static 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. Greer0d373d62012-12-21 10:04:08 -07001236 if ((ctx->flags & BIT(FLAGS_HMAC)) &&
1237 !test_bit(FLAGS_AUTO_XOR, &dd->flags))
Dmitry Kasatkinbf362752011-04-20 13:34:58 +03001238 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 Kasatkin8628e7c2010-05-03 11:10:59 +08001244}
1245
1246static void omap_sham_finish_req(struct ahash_request *req, int err)
1247{
1248 struct omap_sham_reqctx *ctx = ahash_request_ctx(req);
Dmitry Kasatkin798eed5d2010-11-19 16:04:26 +02001249 struct omap_sham_dev *dd = ctx->dd;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001250
1251 if (!err) {
Mark A. Greer0d373d62012-12-21 10:04:08 -07001252 dd->pdata->copy_hash(req, 1);
Dmitry Kasatkined3ea9a82011-06-02 21:10:07 +03001253 if (test_bit(FLAGS_FINAL, &dd->flags))
Dmitry Kasatkinbf362752011-04-20 13:34:58 +03001254 err = omap_sham_finish(req);
Dmitry Kasatkin3e133c82010-11-19 16:04:24 +02001255 } else {
Dmitry Kasatkinea1fd222011-06-02 21:10:05 +03001256 ctx->flags |= BIT(FLAGS_ERROR);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001257 }
1258
Dmitry Kasatkin0efd4d82011-06-02 21:10:12 +03001259 /* 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. Greerb359f032012-12-21 10:04:02 -07001262
Tero Kristoe93f7672016-06-22 16:23:34 +03001263 pm_runtime_mark_last_busy(dd->dev);
1264 pm_runtime_put_autosuspend(dd->dev);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001265
1266 if (req->base.complete)
1267 req->base.complete(&req->base, err);
1268}
1269
Dmitry Kasatkina5d87232010-11-19 16:04:25 +02001270static int omap_sham_handle_queue(struct omap_sham_dev *dd,
1271 struct ahash_request *req)
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001272{
Dmitry Kasatkin6c39d112010-12-29 21:52:04 +11001273 struct crypto_async_request *async_req, *backlog;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001274 struct omap_sham_reqctx *ctx;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001275 unsigned long flags;
Dmitry Kasatkina5d87232010-11-19 16:04:25 +02001276 int err = 0, ret = 0;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001277
Tero Kristo4e7813a2016-08-04 13:28:36 +03001278retry:
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001279 spin_lock_irqsave(&dd->lock, flags);
Dmitry Kasatkina5d87232010-11-19 16:04:25 +02001280 if (req)
1281 ret = ahash_enqueue_request(&dd->queue, req);
Dmitry Kasatkina929cbe2011-06-02 21:10:06 +03001282 if (test_bit(FLAGS_BUSY, &dd->flags)) {
Dmitry Kasatkina5d87232010-11-19 16:04:25 +02001283 spin_unlock_irqrestore(&dd->lock, flags);
1284 return ret;
1285 }
Dmitry Kasatkin6c39d112010-12-29 21:52:04 +11001286 backlog = crypto_get_backlog(&dd->queue);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001287 async_req = crypto_dequeue_request(&dd->queue);
Dmitry Kasatkin6c39d112010-12-29 21:52:04 +11001288 if (async_req)
Dmitry Kasatkina929cbe2011-06-02 21:10:06 +03001289 set_bit(FLAGS_BUSY, &dd->flags);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001290 spin_unlock_irqrestore(&dd->lock, flags);
1291
1292 if (!async_req)
Dmitry Kasatkina5d87232010-11-19 16:04:25 +02001293 return ret;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001294
1295 if (backlog)
1296 backlog->complete(backlog, -EINPROGRESS);
1297
1298 req = ahash_request_cast(async_req);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001299 dd->req = req;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001300 ctx = ahash_request_ctx(req);
1301
Tero Kristof19de1b2016-09-19 18:22:15 +03001302 err = omap_sham_prepare_request(NULL, ctx->op == OP_UPDATE);
1303 if (err)
1304 goto err1;
1305
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001306 dev_dbg(dd->dev, "handling new req, op: %lu, nbytes: %d\n",
1307 ctx->op, req->nbytes);
1308
Dmitry Kasatkin798eed5d2010-11-19 16:04:26 +02001309 err = omap_sham_hw_init(dd);
1310 if (err)
1311 goto err1;
1312
Dmitry Kasatkin798eed5d2010-11-19 16:04:26 +02001313 if (ctx->digcnt)
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001314 /* request has changed - restore hash */
Mark A. Greer0d373d62012-12-21 10:04:08 -07001315 dd->pdata->copy_hash(req, 0);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001316
1317 if (ctx->op == OP_UPDATE) {
1318 err = omap_sham_update_req(dd);
Dmitry Kasatkinea1fd222011-06-02 21:10:05 +03001319 if (err != -EINPROGRESS && (ctx->flags & BIT(FLAGS_FINUP)))
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001320 /* 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 Kasatkin798eed5d2010-11-19 16:04:26 +02001325err1:
Tero Kristo4e7813a2016-08-04 13:28:36 +03001326 dev_dbg(dd->dev, "exit, err: %d\n", err);
1327
1328 if (err != -EINPROGRESS) {
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001329 /* done_task will not finish it, so do it here */
1330 omap_sham_finish_req(req, err);
Tero Kristo4e7813a2016-08-04 13:28:36 +03001331 req = NULL;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001332
Tero Kristo4e7813a2016-08-04 13:28:36 +03001333 /*
1334 * Execute next request immediately if there is anything
1335 * in queue.
1336 */
1337 goto retry;
1338 }
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001339
Dmitry Kasatkina5d87232010-11-19 16:04:25 +02001340 return ret;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001341}
1342
1343static 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 Kasatkin8628e7c2010-05-03 11:10:59 +08001348
1349 ctx->op = op;
1350
Dmitry Kasatkina5d87232010-11-19 16:04:25 +02001351 return omap_sham_handle_queue(dd, req);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001352}
1353
1354static int omap_sham_update(struct ahash_request *req)
1355{
1356 struct omap_sham_reqctx *ctx = ahash_request_ctx(req);
Lokesh Vutlab8411cc2013-08-20 20:32:34 +05301357 struct omap_sham_dev *dd = ctx->dd;
Lokesh Vutlaeaef7e32013-07-26 12:29:14 +05301358 int bs = get_block_size(ctx);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001359
1360 if (!req->nbytes)
1361 return 0;
1362
1363 ctx->total = req->nbytes;
1364 ctx->sg = req->src;
1365 ctx->offset = 0;
1366
Dmitry Kasatkinea1fd222011-06-02 21:10:05 +03001367 if (ctx->flags & BIT(FLAGS_FINUP)) {
Bin Liu85e06872016-06-22 16:23:37 +03001368 if ((ctx->digcnt + ctx->bufcnt + ctx->total) < 240) {
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001369 /*
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 Vutlab8411cc2013-08-20 20:32:34 +05301376 } else if ((ctx->bufcnt + ctx->total <= bs) ||
1377 dd->polling_mode) {
Dmitry Kasatkin887c8832010-11-19 16:04:29 +02001378 /*
Lokesh Vutlab8411cc2013-08-20 20:32:34 +05301379 * faster to use CPU for short transfers or
1380 * use cpu when dma is not present.
1381 */
Dmitry Kasatkinea1fd222011-06-02 21:10:05 +03001382 ctx->flags |= BIT(FLAGS_CPU);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001383 }
Dmitry Kasatkin887c8832010-11-19 16:04:29 +02001384 } else if (ctx->bufcnt + ctx->total < ctx->buflen) {
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001385 omap_sham_append_sg(ctx);
1386 return 0;
1387 }
1388
Lokesh Vutlaacef7b02013-12-18 19:03:33 +05301389 if (dd->polling_mode)
1390 ctx->flags |= BIT(FLAGS_CPU);
1391
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001392 return omap_sham_enqueue(req, OP_UPDATE);
1393}
1394
Behan Webster7bc53c32014-04-04 18:18:00 -03001395static int omap_sham_shash_digest(struct crypto_shash *tfm, u32 flags,
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001396 const u8 *data, unsigned int len, u8 *out)
1397{
Behan Webster7bc53c32014-04-04 18:18:00 -03001398 SHASH_DESC_ON_STACK(shash, tfm);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001399
Behan Webster7bc53c32014-04-04 18:18:00 -03001400 shash->tfm = tfm;
1401 shash->flags = flags & CRYPTO_TFM_REQ_MAY_SLEEP;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001402
Behan Webster7bc53c32014-04-04 18:18:00 -03001403 return crypto_shash_digest(shash, data, len, out);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001404}
1405
1406static 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 Kristocb8d5c82016-08-04 13:28:40 +03001410 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 Kasatkin8628e7c2010-05-03 11:10:59 +08001420
1421 return omap_sham_shash_digest(tctx->fallback, req->base.flags,
Tero Kristocb8d5c82016-08-04 13:28:40 +03001422 ctx->buffer + offset,
1423 ctx->bufcnt - offset, req->result);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001424}
1425
1426static int omap_sham_final(struct ahash_request *req)
1427{
1428 struct omap_sham_reqctx *ctx = ahash_request_ctx(req);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001429
Dmitry Kasatkinea1fd222011-06-02 21:10:05 +03001430 ctx->flags |= BIT(FLAGS_FINUP);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001431
Dmitry Kasatkinea1fd222011-06-02 21:10:05 +03001432 if (ctx->flags & BIT(FLAGS_ERROR))
Dmitry Kasatkinbf362752011-04-20 13:34:58 +03001433 return 0; /* uncompleted hash is not needed */
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001434
Bin Liu85e06872016-06-22 16:23:37 +03001435 /*
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 Kristo5a793bc2016-08-04 13:28:39 +03001441 if (!ctx->digcnt && ctx->bufcnt < 240)
Dmitry Kasatkinbf362752011-04-20 13:34:58 +03001442 return omap_sham_final_shash(req);
1443 else if (ctx->bufcnt)
1444 return omap_sham_enqueue(req, OP_FINAL);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001445
Dmitry Kasatkinbf362752011-04-20 13:34:58 +03001446 /* copy ready hash (+ finalize hmac) */
1447 return omap_sham_finish(req);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001448}
1449
1450static 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 Kasatkinea1fd222011-06-02 21:10:05 +03001455 ctx->flags |= BIT(FLAGS_FINUP);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001456
1457 err1 = omap_sham_update(req);
Markku Kylanpaa455e3382011-04-20 13:34:55 +03001458 if (err1 == -EINPROGRESS || err1 == -EBUSY)
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001459 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
1469static int omap_sham_digest(struct ahash_request *req)
1470{
1471 return omap_sham_init(req) ?: omap_sham_finup(req);
1472}
1473
1474static 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. Greer0d373d62012-12-21 10:04:08 -07001481 struct omap_sham_dev *dd = NULL, *tmp;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001482 int err, i;
Mark A. Greer0d373d62012-12-21 10:04:08 -07001483
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 Kasatkin8628e7c2010-05-03 11:10:59 +08001496 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 Kasatkin8628e7c2010-05-03 11:10:59 +08001512
Mark A. Greer0d373d62012-12-21 10:04:08 -07001513 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 Kasatkin8628e7c2010-05-03 11:10:59 +08001520 }
1521
1522 return err;
1523}
1524
1525static 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 Kasatkin798eed5d2010-11-19 16:04:26 +02001540 sizeof(struct omap_sham_reqctx) + BUFLEN);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001541
1542 if (alg_base) {
1543 struct omap_sham_hmac_ctx *bctx = tctx->base;
Dmitry Kasatkinea1fd222011-06-02 21:10:05 +03001544 tctx->flags |= BIT(FLAGS_HMAC);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001545 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
1559static int omap_sham_cra_init(struct crypto_tfm *tfm)
1560{
1561 return omap_sham_cra_init_alg(tfm, NULL);
1562}
1563
1564static int omap_sham_cra_sha1_init(struct crypto_tfm *tfm)
1565{
1566 return omap_sham_cra_init_alg(tfm, "sha1");
1567}
1568
Mark A. Greerd20fb182012-12-21 10:04:09 -07001569static int omap_sham_cra_sha224_init(struct crypto_tfm *tfm)
1570{
1571 return omap_sham_cra_init_alg(tfm, "sha224");
1572}
1573
1574static int omap_sham_cra_sha256_init(struct crypto_tfm *tfm)
1575{
1576 return omap_sham_cra_init_alg(tfm, "sha256");
1577}
1578
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001579static int omap_sham_cra_md5_init(struct crypto_tfm *tfm)
1580{
1581 return omap_sham_cra_init_alg(tfm, "md5");
1582}
1583
Lokesh Vutlaeaef7e32013-07-26 12:29:14 +05301584static int omap_sham_cra_sha384_init(struct crypto_tfm *tfm)
1585{
1586 return omap_sham_cra_init_alg(tfm, "sha384");
1587}
1588
1589static int omap_sham_cra_sha512_init(struct crypto_tfm *tfm)
1590{
1591 return omap_sham_cra_init_alg(tfm, "sha512");
1592}
1593
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001594static 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 Kasatkinea1fd222011-06-02 21:10:05 +03001601 if (tctx->flags & BIT(FLAGS_HMAC)) {
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001602 struct omap_sham_hmac_ctx *bctx = tctx->base;
1603 crypto_free_shash(bctx->shash);
1604 }
1605}
1606
Tero Kristo99a7fff2016-09-19 18:22:12 +03001607static int omap_sham_export(struct ahash_request *req, void *out)
1608{
1609 return -ENOTSUPP;
1610}
1611
1612static int omap_sham_import(struct ahash_request *req, const void *in)
1613{
1614 return -ENOTSUPP;
1615}
1616
Mark A. Greerd20fb182012-12-21 10:04:09 -07001617static struct ahash_alg algs_sha1_md5[] = {
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001618{
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 Liueb354782016-06-30 14:04:11 -05001628 .cra_priority = 400,
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001629 .cra_flags = CRYPTO_ALG_TYPE_AHASH |
Nikos Mavrogiannopoulosd912bb72011-11-01 13:39:56 +01001630 CRYPTO_ALG_KERN_DRIVER_ONLY |
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001631 CRYPTO_ALG_ASYNC |
1632 CRYPTO_ALG_NEED_FALLBACK,
1633 .cra_blocksize = SHA1_BLOCK_SIZE,
1634 .cra_ctxsize = sizeof(struct omap_sham_ctx),
Tero Kristo744e6862016-09-19 18:22:13 +03001635 .cra_alignmask = OMAP_ALIGN_MASK,
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001636 .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 Liueb354782016-06-30 14:04:11 -05001651 .cra_priority = 400,
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001652 .cra_flags = CRYPTO_ALG_TYPE_AHASH |
Nikos Mavrogiannopoulosd912bb72011-11-01 13:39:56 +01001653 CRYPTO_ALG_KERN_DRIVER_ONLY |
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001654 CRYPTO_ALG_ASYNC |
1655 CRYPTO_ALG_NEED_FALLBACK,
1656 .cra_blocksize = SHA1_BLOCK_SIZE,
1657 .cra_ctxsize = sizeof(struct omap_sham_ctx),
Dmitry Kasatkin798eed5d2010-11-19 16:04:26 +02001658 .cra_alignmask = OMAP_ALIGN_MASK,
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001659 .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 Liueb354782016-06-30 14:04:11 -05001675 .cra_priority = 400,
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001676 .cra_flags = CRYPTO_ALG_TYPE_AHASH |
Nikos Mavrogiannopoulosd912bb72011-11-01 13:39:56 +01001677 CRYPTO_ALG_KERN_DRIVER_ONLY |
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001678 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 Kasatkin798eed5d2010-11-19 16:04:26 +02001683 .cra_alignmask = OMAP_ALIGN_MASK,
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001684 .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 Liueb354782016-06-30 14:04:11 -05001700 .cra_priority = 400,
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001701 .cra_flags = CRYPTO_ALG_TYPE_AHASH |
Nikos Mavrogiannopoulosd912bb72011-11-01 13:39:56 +01001702 CRYPTO_ALG_KERN_DRIVER_ONLY |
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001703 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 Kasatkin798eed5d2010-11-19 16:04:26 +02001708 .cra_alignmask = OMAP_ALIGN_MASK,
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001709 .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. Greerd20fb182012-12-21 10:04:09 -07001716/* OMAP4 has some algs in addition to what OMAP2 has */
1717static 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 Liueb354782016-06-30 14:04:11 -05001728 .cra_priority = 400,
Mark A. Greerd20fb182012-12-21 10:04:09 -07001729 .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 Kristo744e6862016-09-19 18:22:13 +03001734 .cra_alignmask = OMAP_ALIGN_MASK,
Mark A. Greerd20fb182012-12-21 10:04:09 -07001735 .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 Liueb354782016-06-30 14:04:11 -05001750 .cra_priority = 400,
Mark A. Greerd20fb182012-12-21 10:04:09 -07001751 .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 Kristo744e6862016-09-19 18:22:13 +03001756 .cra_alignmask = OMAP_ALIGN_MASK,
Mark A. Greerd20fb182012-12-21 10:04:09 -07001757 .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 Liueb354782016-06-30 14:04:11 -05001773 .cra_priority = 400,
Mark A. Greerd20fb182012-12-21 10:04:09 -07001774 .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 Liueb354782016-06-30 14:04:11 -05001797 .cra_priority = 400,
Mark A. Greerd20fb182012-12-21 10:04:09 -07001798 .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 Vutlaeaef7e32013-07-26 12:29:14 +05301812static 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 Liueb354782016-06-30 14:04:11 -05001823 .cra_priority = 400,
Lokesh Vutlaeaef7e32013-07-26 12:29:14 +05301824 .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 Kristo744e6862016-09-19 18:22:13 +03001829 .cra_alignmask = OMAP_ALIGN_MASK,
Lokesh Vutlaeaef7e32013-07-26 12:29:14 +05301830 .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 Liueb354782016-06-30 14:04:11 -05001845 .cra_priority = 400,
Lokesh Vutlaeaef7e32013-07-26 12:29:14 +05301846 .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 Kristo744e6862016-09-19 18:22:13 +03001851 .cra_alignmask = OMAP_ALIGN_MASK,
Lokesh Vutlaeaef7e32013-07-26 12:29:14 +05301852 .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 Liueb354782016-06-30 14:04:11 -05001868 .cra_priority = 400,
Lokesh Vutlaeaef7e32013-07-26 12:29:14 +05301869 .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 Liueb354782016-06-30 14:04:11 -05001892 .cra_priority = 400,
Lokesh Vutlaeaef7e32013-07-26 12:29:14 +05301893 .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 Kasatkin8628e7c2010-05-03 11:10:59 +08001907static void omap_sham_done_task(unsigned long data)
1908{
1909 struct omap_sham_dev *dd = (struct omap_sham_dev *)data;
Dmitry Kasatkin6c63db82011-06-02 21:10:10 +03001910 int err = 0;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001911
Dmitry Kasatkin6cb3ffe2011-06-02 21:10:09 +03001912 if (!test_bit(FLAGS_BUSY, &dd->flags)) {
1913 omap_sham_handle_queue(dd, NULL);
1914 return;
1915 }
1916
Dmitry Kasatkin6c63db82011-06-02 21:10:10 +03001917 if (test_bit(FLAGS_CPU, &dd->flags)) {
Lokesh Vutlab8411cc2013-08-20 20:32:34 +05301918 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 Kasatkin6c63db82011-06-02 21:10:10 +03001924 } 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 Kasatkin887c8832010-11-19 16:04:29 +02001935 err = omap_sham_update_dma_start(dd);
Dmitry Kasatkin6c63db82011-06-02 21:10:10 +03001936 if (err != -EINPROGRESS)
1937 goto finish;
1938 }
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001939 }
1940
Dmitry Kasatkin6c63db82011-06-02 21:10:10 +03001941 return;
Dmitry Kasatkin3e133c82010-11-19 16:04:24 +02001942
Dmitry Kasatkin6c63db82011-06-02 21:10:10 +03001943finish:
1944 dev_dbg(dd->dev, "update done: err: %d\n", err);
1945 /* finish curent request */
1946 omap_sham_finish_req(dd->req, err);
Tero Kristo4e7813a2016-08-04 13:28:36 +03001947
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 Kasatkin8628e7c2010-05-03 11:10:59 +08001951}
1952
Mark A. Greer0d373d62012-12-21 10:04:08 -07001953static 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
1965static irqreturn_t omap_sham_irq_omap2(int irq, void *dev_id)
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001966{
1967 struct omap_sham_dev *dd = dev_id;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001968
Dmitry Kasatkined3ea9a82011-06-02 21:10:07 +03001969 if (unlikely(test_bit(FLAGS_FINAL, &dd->flags)))
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001970 /* 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. Greer0d373d62012-12-21 10:04:08 -07001977 return omap_sham_irq_common(dd);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001978}
1979
Mark A. Greer0d373d62012-12-21 10:04:08 -07001980static irqreturn_t omap_sham_irq_omap4(int irq, void *dev_id)
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001981{
Mark A. Greer0d373d62012-12-21 10:04:08 -07001982 struct omap_sham_dev *dd = dev_id;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001983
Mark A. Greer0d373d62012-12-21 10:04:08 -07001984 omap_sham_write_mask(dd, SHA_REG_MASK(dd), 0, SHA_REG_MASK_IT_EN);
Dmitry Kasatkin3e133c82010-11-19 16:04:24 +02001985
Mark A. Greer0d373d62012-12-21 10:04:08 -07001986 return omap_sham_irq_common(dd);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001987}
1988
Mark A. Greerd20fb182012-12-21 10:04:09 -07001989static 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. Greer0d373d62012-12-21 10:04:08 -07001996static const struct omap_sham_pdata omap_sham_pdata_omap2 = {
Mark A. Greerd20fb182012-12-21 10:04:09 -07001997 .algs_info = omap_sham_algs_info_omap2,
1998 .algs_info_size = ARRAY_SIZE(omap_sham_algs_info_omap2),
Mark A. Greer0d373d62012-12-21 10:04:08 -07001999 .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. Greer03feec92012-12-21 10:04:06 -07002018#ifdef CONFIG_OF
Mark A. Greerd20fb182012-12-21 10:04:09 -07002019static 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. Greer0d373d62012-12-21 10:04:08 -07002030static const struct omap_sham_pdata omap_sham_pdata_omap4 = {
Mark A. Greerd20fb182012-12-21 10:04:09 -07002031 .algs_info = omap_sham_algs_info_omap4,
2032 .algs_info_size = ARRAY_SIZE(omap_sham_algs_info_omap4),
Mark A. Greer0d373d62012-12-21 10:04:08 -07002033 .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 Vutlaeaef7e32013-07-26 12:29:14 +05302041 .odigest_ofs = 0x0,
Mark A. Greer0d373d62012-12-21 10:04:08 -07002042 .din_ofs = 0x080,
2043 .digcnt_ofs = 0x040,
2044 .rev_ofs = 0x100,
2045 .mask_ofs = 0x110,
2046 .sysstatus_ofs = 0x114,
Lokesh Vutlaeaef7e32013-07-26 12:29:14 +05302047 .mode_ofs = 0x44,
2048 .length_ofs = 0x48,
Mark A. Greer0d373d62012-12-21 10:04:08 -07002049 .major_mask = 0x0700,
2050 .major_shift = 8,
2051 .minor_mask = 0x003f,
2052 .minor_shift = 0,
2053};
2054
Lokesh Vutla7d7c7042013-07-26 12:29:15 +05302055static 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
2070static 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. Greer03feec92012-12-21 10:04:06 -07002095static const struct of_device_id omap_sham_of_match[] = {
2096 {
2097 .compatible = "ti,omap2-sham",
Mark A. Greer0d373d62012-12-21 10:04:08 -07002098 .data = &omap_sham_pdata_omap2,
2099 },
2100 {
Pali Roháreddca852015-02-26 14:49:53 +01002101 .compatible = "ti,omap3-sham",
2102 .data = &omap_sham_pdata_omap2,
2103 },
2104 {
Mark A. Greer0d373d62012-12-21 10:04:08 -07002105 .compatible = "ti,omap4-sham",
2106 .data = &omap_sham_pdata_omap4,
Mark A. Greer03feec92012-12-21 10:04:06 -07002107 },
Lokesh Vutla7d7c7042013-07-26 12:29:15 +05302108 {
2109 .compatible = "ti,omap5-sham",
2110 .data = &omap_sham_pdata_omap5,
2111 },
Mark A. Greer03feec92012-12-21 10:04:06 -07002112 {},
2113};
2114MODULE_DEVICE_TABLE(of, omap_sham_of_match);
2115
2116static int omap_sham_get_res_of(struct omap_sham_dev *dd,
2117 struct device *dev, struct resource *res)
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08002118{
Mark A. Greer03feec92012-12-21 10:04:06 -07002119 struct device_node *node = dev->of_node;
2120 const struct of_device_id *match;
2121 int err = 0;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08002122
Mark A. Greer03feec92012-12-21 10:04:06 -07002123 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 Kasatkin8628e7c2010-05-03 11:10:59 +08002128 }
Samu Onkalo584db6a2010-09-03 19:20:19 +08002129
Mark A. Greer03feec92012-12-21 10:04:06 -07002130 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 Redingf7578492013-09-18 15:24:44 +02002137 dd->irq = irq_of_parse_and_map(node, 0);
Mark A. Greer03feec92012-12-21 10:04:06 -07002138 if (!dd->irq) {
2139 dev_err(dev, "can't translate OF irq value\n");
2140 err = -EINVAL;
2141 goto err;
2142 }
2143
Mark A. Greer0d373d62012-12-21 10:04:08 -07002144 dd->pdata = match->data;
Mark A. Greer03feec92012-12-21 10:04:06 -07002145
2146err:
2147 return err;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08002148}
Mark A. Greer03feec92012-12-21 10:04:06 -07002149#else
Mark A. Greerc3c3b322013-01-15 13:53:02 -07002150static const struct of_device_id omap_sham_of_match[] = {
2151 {},
2152};
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08002153
Mark A. Greerc3c3b322013-01-15 13:53:02 -07002154static int omap_sham_get_res_of(struct omap_sham_dev *dd,
Mark A. Greer03feec92012-12-21 10:04:06 -07002155 struct device *dev, struct resource *res)
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08002156{
Mark A. Greer03feec92012-12-21 10:04:06 -07002157 return -EINVAL;
2158}
2159#endif
2160
2161static 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 Kasatkin8628e7c2010-05-03 11:10:59 +08002174 }
Mark A. Greer03feec92012-12-21 10:04:06 -07002175 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. Greer0d373d62012-12-21 10:04:08 -07002185 /* Only OMAP2/3 can be non-DT */
2186 dd->pdata = &omap_sham_pdata_omap2;
2187
Mark A. Greer03feec92012-12-21 10:04:06 -07002188err:
2189 return err;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08002190}
2191
Greg Kroah-Hartman49cfe4d2012-12-21 13:14:09 -08002192static int omap_sham_probe(struct platform_device *pdev)
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08002193{
2194 struct omap_sham_dev *dd;
2195 struct device *dev = &pdev->dev;
Mark A. Greer03feec92012-12-21 10:04:06 -07002196 struct resource res;
Mark A. Greerdfd061d2012-12-21 10:04:04 -07002197 dma_cap_mask_t mask;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08002198 int err, i, j;
Mark A. Greer0d373d62012-12-21 10:04:08 -07002199 u32 rev;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08002200
Lokesh Vutla7a7e4b72013-07-26 12:29:17 +05302201 dd = devm_kzalloc(dev, sizeof(struct omap_sham_dev), GFP_KERNEL);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08002202 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 Kasatkin8628e7c2010-05-03 11:10:59 +08002213 crypto_init_queue(&dd->queue, OMAP_SHAM_QUEUE_LENGTH);
2214
Mark A. Greer03feec92012-12-21 10:04:06 -07002215 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 Vutla7a7e4b72013-07-26 12:29:17 +05302218 goto data_err;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08002219
Laurent Navet30862282013-05-02 14:00:38 +02002220 dd->io_base = devm_ioremap_resource(dev, &res);
2221 if (IS_ERR(dd->io_base)) {
2222 err = PTR_ERR(dd->io_base);
Lokesh Vutla7a7e4b72013-07-26 12:29:17 +05302223 goto data_err;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08002224 }
Mark A. Greer03feec92012-12-21 10:04:06 -07002225 dd->phys_base = res.start;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08002226
Lokesh Vutla0de9c382013-07-26 12:29:16 +05302227 err = devm_request_irq(dev, dd->irq, dd->pdata->intr_hdlr,
2228 IRQF_TRIGGER_NONE, dev_name(dev), dd);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08002229 if (err) {
Lokesh Vutla0de9c382013-07-26 12:29:16 +05302230 dev_err(dev, "unable to request irq %d, err = %d\n",
2231 dd->irq, err);
Lokesh Vutla7a7e4b72013-07-26 12:29:17 +05302232 goto data_err;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08002233 }
2234
Mark A. Greerdfd061d2012-12-21 10:04:04 -07002235 dma_cap_zero(mask);
2236 dma_cap_set(DMA_SLAVE, mask);
2237
Peter Ujfalusidbe24622016-04-29 16:03:41 +03002238 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 Vutlab8411cc2013-08-20 20:32:34 +05302244 dd->polling_mode = 1;
2245 dev_dbg(dev, "using polling mode instead of dma\n");
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08002246 }
2247
Mark A. Greer0d373d62012-12-21 10:04:08 -07002248 dd->flags |= dd->pdata->flags;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08002249
Tero Kristoe93f7672016-06-22 16:23:34 +03002250 pm_runtime_use_autosuspend(dev);
2251 pm_runtime_set_autosuspend_delay(dev, DEFAULT_AUTOSUSPEND_DELAY);
2252
Mark A. Greerb359f032012-12-21 10:04:02 -07002253 pm_runtime_enable(dev);
Vutla, Lokeshb0a3d892015-03-31 09:52:24 +05302254 pm_runtime_irq_safe(dev);
Pali Rohár604c3102015-03-08 11:01:01 +01002255
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. Greer0d373d62012-12-21 10:04:08 -07002262 rev = omap_sham_read(dd, SHA_REG_REV(dd));
2263 pm_runtime_put_sync(&pdev->dev);
Mark A. Greerb359f032012-12-21 10:04:02 -07002264
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08002265 dev_info(dev, "hw accel on OMAP rev %u.%u\n",
Mark A. Greer0d373d62012-12-21 10:04:08 -07002266 (rev & dd->pdata->major_mask) >> dd->pdata->major_shift,
2267 (rev & dd->pdata->minor_mask) >> dd->pdata->minor_shift);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08002268
2269 spin_lock(&sham.lock);
2270 list_add_tail(&dd->list, &sham.dev_list);
2271 spin_unlock(&sham.lock);
2272
Mark A. Greerd20fb182012-12-21 10:04:09 -07002273 for (i = 0; i < dd->pdata->algs_info_size; i++) {
2274 for (j = 0; j < dd->pdata->algs_info[i].size; j++) {
Tero Kristo99a7fff2016-09-19 18:22:12 +03002275 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. Greerd20fb182012-12-21 10:04:09 -07002282 if (err)
2283 goto err_algs;
2284
2285 dd->pdata->algs_info[i].registered++;
2286 }
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08002287 }
2288
2289 return 0;
2290
2291err_algs:
Mark A. Greerd20fb182012-12-21 10:04:09 -07002292 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ár604c3102015-03-08 11:01:01 +01002296err_pm:
Mark A. Greerb359f032012-12-21 10:04:02 -07002297 pm_runtime_disable(dev);
Dan Carpenterd462e322016-05-18 13:39:05 +03002298 if (!dd->polling_mode)
Mark A. Greerf13ab862013-11-12 13:12:27 -07002299 dma_release_channel(dd->dma_lch);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08002300data_err:
2301 dev_err(dev, "initialization failed.\n");
2302
2303 return err;
2304}
2305
Greg Kroah-Hartman49cfe4d2012-12-21 13:14:09 -08002306static int omap_sham_remove(struct platform_device *pdev)
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08002307{
2308 static struct omap_sham_dev *dd;
Mark A. Greerd20fb182012-12-21 10:04:09 -07002309 int i, j;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08002310
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. Greerd20fb182012-12-21 10:04:09 -07002317 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 Kasatkin8628e7c2010-05-03 11:10:59 +08002321 tasklet_kill(&dd->done_task);
Mark A. Greerb359f032012-12-21 10:04:02 -07002322 pm_runtime_disable(&pdev->dev);
Mark A. Greerf13ab862013-11-12 13:12:27 -07002323
Peter Ujfalusidbe24622016-04-29 16:03:41 +03002324 if (!dd->polling_mode)
Mark A. Greerf13ab862013-11-12 13:12:27 -07002325 dma_release_channel(dd->dma_lch);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08002326
2327 return 0;
2328}
2329
Mark A. Greer3b3f4402012-12-21 10:04:03 -07002330#ifdef CONFIG_PM_SLEEP
2331static int omap_sham_suspend(struct device *dev)
2332{
2333 pm_runtime_put_sync(dev);
2334 return 0;
2335}
2336
2337static int omap_sham_resume(struct device *dev)
2338{
Pali Rohár604c3102015-03-08 11:01:01 +01002339 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. Greer3b3f4402012-12-21 10:04:03 -07002344 return 0;
2345}
2346#endif
2347
Jingoo Hanae12fe22014-02-27 20:33:32 +09002348static SIMPLE_DEV_PM_OPS(omap_sham_pm_ops, omap_sham_suspend, omap_sham_resume);
Mark A. Greer3b3f4402012-12-21 10:04:03 -07002349
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08002350static struct platform_driver omap_sham_driver = {
2351 .probe = omap_sham_probe,
2352 .remove = omap_sham_remove,
2353 .driver = {
2354 .name = "omap-sham",
Mark A. Greer3b3f4402012-12-21 10:04:03 -07002355 .pm = &omap_sham_pm_ops,
Mark A. Greer03feec92012-12-21 10:04:06 -07002356 .of_match_table = omap_sham_of_match,
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08002357 },
2358};
2359
Sachin Kamat02613702013-03-04 15:09:43 +05302360module_platform_driver(omap_sham_driver);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08002361
2362MODULE_DESCRIPTION("OMAP SHA1/MD5 hw acceleration support.");
2363MODULE_LICENSE("GPL v2");
2364MODULE_AUTHOR("Dmitry Kasatkin");
Joni Lapilainen718249d2013-10-26 23:00:41 +02002365MODULE_ALIAS("platform:omap-sham");