blob: e4072cd385857cffae2bf41ecdcc82282c079e73 [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08002/*
3 * Cryptographic API.
4 *
5 * Support for OMAP SHA1/MD5 HW acceleration.
6 *
7 * Copyright (c) 2010 Nokia Corporation
8 * Author: Dmitry Kasatkin <dmitry.kasatkin@nokia.com>
Mark A. Greer0d373d62012-12-21 10:04:08 -07009 * Copyright (c) 2011 Texas Instruments Incorporated
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +080010 *
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +080011 * Some ideas are from old omap-sha1-md5.c driver.
12 */
13
14#define pr_fmt(fmt) "%s: " fmt, __func__
15
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +080016#include <linux/err.h>
17#include <linux/device.h>
18#include <linux/module.h>
19#include <linux/init.h>
20#include <linux/errno.h>
21#include <linux/interrupt.h>
22#include <linux/kernel.h>
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +080023#include <linux/irq.h>
24#include <linux/io.h>
25#include <linux/platform_device.h>
26#include <linux/scatterlist.h>
27#include <linux/dma-mapping.h>
Mark A. Greerdfd061d2012-12-21 10:04:04 -070028#include <linux/dmaengine.h>
Mark A. Greerb359f032012-12-21 10:04:02 -070029#include <linux/pm_runtime.h>
Mark A. Greer03feec92012-12-21 10:04:06 -070030#include <linux/of.h>
31#include <linux/of_device.h>
32#include <linux/of_address.h>
33#include <linux/of_irq.h>
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +080034#include <linux/delay.h>
35#include <linux/crypto.h>
36#include <linux/cryptohash.h>
37#include <crypto/scatterwalk.h>
38#include <crypto/algapi.h>
39#include <crypto/sha.h>
40#include <crypto/hash.h>
Corentin LABBEebd401e2017-05-19 08:53:28 +020041#include <crypto/hmac.h>
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +080042#include <crypto/internal/hash.h>
43
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +080044#define MD5_DIGEST_SIZE 16
45
Mark A. Greer0d373d62012-12-21 10:04:08 -070046#define SHA_REG_IDIGEST(dd, x) ((dd)->pdata->idigest_ofs + ((x)*0x04))
47#define SHA_REG_DIN(dd, x) ((dd)->pdata->din_ofs + ((x) * 0x04))
48#define SHA_REG_DIGCNT(dd) ((dd)->pdata->digcnt_ofs)
49
Lokesh Vutlaeaef7e32013-07-26 12:29:14 +053050#define SHA_REG_ODIGEST(dd, x) ((dd)->pdata->odigest_ofs + (x * 0x04))
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +080051
52#define SHA_REG_CTRL 0x18
53#define SHA_REG_CTRL_LENGTH (0xFFFFFFFF << 5)
54#define SHA_REG_CTRL_CLOSE_HASH (1 << 4)
55#define SHA_REG_CTRL_ALGO_CONST (1 << 3)
56#define SHA_REG_CTRL_ALGO (1 << 2)
57#define SHA_REG_CTRL_INPUT_READY (1 << 1)
58#define SHA_REG_CTRL_OUTPUT_READY (1 << 0)
59
Mark A. Greer0d373d62012-12-21 10:04:08 -070060#define SHA_REG_REV(dd) ((dd)->pdata->rev_ofs)
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +080061
Mark A. Greer0d373d62012-12-21 10:04:08 -070062#define SHA_REG_MASK(dd) ((dd)->pdata->mask_ofs)
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +080063#define SHA_REG_MASK_DMA_EN (1 << 3)
64#define SHA_REG_MASK_IT_EN (1 << 2)
65#define SHA_REG_MASK_SOFTRESET (1 << 1)
66#define SHA_REG_AUTOIDLE (1 << 0)
67
Mark A. Greer0d373d62012-12-21 10:04:08 -070068#define SHA_REG_SYSSTATUS(dd) ((dd)->pdata->sysstatus_ofs)
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +080069#define SHA_REG_SYSSTATUS_RESETDONE (1 << 0)
70
Lokesh Vutlaeaef7e32013-07-26 12:29:14 +053071#define SHA_REG_MODE(dd) ((dd)->pdata->mode_ofs)
Mark A. Greer0d373d62012-12-21 10:04:08 -070072#define SHA_REG_MODE_HMAC_OUTER_HASH (1 << 7)
73#define SHA_REG_MODE_HMAC_KEY_PROC (1 << 5)
74#define SHA_REG_MODE_CLOSE_HASH (1 << 4)
75#define SHA_REG_MODE_ALGO_CONSTANT (1 << 3)
Mark A. Greer0d373d62012-12-21 10:04:08 -070076
Lokesh Vutlaeaef7e32013-07-26 12:29:14 +053077#define SHA_REG_MODE_ALGO_MASK (7 << 0)
78#define SHA_REG_MODE_ALGO_MD5_128 (0 << 1)
79#define SHA_REG_MODE_ALGO_SHA1_160 (1 << 1)
80#define SHA_REG_MODE_ALGO_SHA2_224 (2 << 1)
81#define SHA_REG_MODE_ALGO_SHA2_256 (3 << 1)
82#define SHA_REG_MODE_ALGO_SHA2_384 (1 << 0)
83#define SHA_REG_MODE_ALGO_SHA2_512 (3 << 0)
84
85#define SHA_REG_LENGTH(dd) ((dd)->pdata->length_ofs)
Mark A. Greer0d373d62012-12-21 10:04:08 -070086
87#define SHA_REG_IRQSTATUS 0x118
88#define SHA_REG_IRQSTATUS_CTX_RDY (1 << 3)
89#define SHA_REG_IRQSTATUS_PARTHASH_RDY (1 << 2)
90#define SHA_REG_IRQSTATUS_INPUT_RDY (1 << 1)
91#define SHA_REG_IRQSTATUS_OUTPUT_RDY (1 << 0)
92
93#define SHA_REG_IRQENA 0x11C
94#define SHA_REG_IRQENA_CTX_RDY (1 << 3)
95#define SHA_REG_IRQENA_PARTHASH_RDY (1 << 2)
96#define SHA_REG_IRQENA_INPUT_RDY (1 << 1)
97#define SHA_REG_IRQENA_OUTPUT_RDY (1 << 0)
98
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +080099#define DEFAULT_TIMEOUT_INTERVAL HZ
100
Tero Kristoe93f7672016-06-22 16:23:34 +0300101#define DEFAULT_AUTOSUSPEND_DELAY 1000
102
Dmitry Kasatkinea1fd222011-06-02 21:10:05 +0300103/* mostly device flags */
104#define FLAGS_BUSY 0
105#define FLAGS_FINAL 1
106#define FLAGS_DMA_ACTIVE 2
107#define FLAGS_OUTPUT_READY 3
108#define FLAGS_INIT 4
109#define FLAGS_CPU 5
Dmitry Kasatkin6c63db82011-06-02 21:10:10 +0300110#define FLAGS_DMA_READY 6
Mark A. Greer0d373d62012-12-21 10:04:08 -0700111#define FLAGS_AUTO_XOR 7
112#define FLAGS_BE32_SHA1 8
Tero Kristof19de1b2016-09-19 18:22:15 +0300113#define FLAGS_SGS_COPIED 9
114#define FLAGS_SGS_ALLOCED 10
Tero Kristo462519f2019-11-05 16:00:50 +0200115#define FLAGS_HUGE 11
116
Dmitry Kasatkinea1fd222011-06-02 21:10:05 +0300117/* context flags */
118#define FLAGS_FINUP 16
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800119
Mark A. Greer0d373d62012-12-21 10:04:08 -0700120#define FLAGS_MODE_SHIFT 18
Lokesh Vutlaeaef7e32013-07-26 12:29:14 +0530121#define FLAGS_MODE_MASK (SHA_REG_MODE_ALGO_MASK << FLAGS_MODE_SHIFT)
122#define FLAGS_MODE_MD5 (SHA_REG_MODE_ALGO_MD5_128 << FLAGS_MODE_SHIFT)
123#define FLAGS_MODE_SHA1 (SHA_REG_MODE_ALGO_SHA1_160 << FLAGS_MODE_SHIFT)
124#define FLAGS_MODE_SHA224 (SHA_REG_MODE_ALGO_SHA2_224 << FLAGS_MODE_SHIFT)
125#define FLAGS_MODE_SHA256 (SHA_REG_MODE_ALGO_SHA2_256 << FLAGS_MODE_SHIFT)
126#define FLAGS_MODE_SHA384 (SHA_REG_MODE_ALGO_SHA2_384 << FLAGS_MODE_SHIFT)
127#define FLAGS_MODE_SHA512 (SHA_REG_MODE_ALGO_SHA2_512 << FLAGS_MODE_SHIFT)
128
129#define FLAGS_HMAC 21
130#define FLAGS_ERROR 22
Mark A. Greer0d373d62012-12-21 10:04:08 -0700131
132#define OP_UPDATE 1
133#define OP_FINAL 2
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800134
Dmitry Kasatkin798eed5d2010-11-19 16:04:26 +0200135#define OMAP_ALIGN_MASK (sizeof(u32)-1)
136#define OMAP_ALIGNED __attribute__((aligned(sizeof(u32))))
137
Tero Kristo182e2832016-09-19 18:22:19 +0300138#define BUFLEN SHA512_BLOCK_SIZE
Tero Kristo2c5bd1e2016-09-19 18:22:16 +0300139#define OMAP_SHA_DMA_THRESHOLD 256
Dmitry Kasatkin798eed5d2010-11-19 16:04:26 +0200140
Tero Kristo462519f2019-11-05 16:00:50 +0200141#define OMAP_SHA_MAX_DMA_LEN (1024 * 2048)
142
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800143struct omap_sham_dev;
144
145struct omap_sham_reqctx {
146 struct omap_sham_dev *dd;
147 unsigned long flags;
148 unsigned long op;
149
Lokesh Vutlaeaef7e32013-07-26 12:29:14 +0530150 u8 digest[SHA512_DIGEST_SIZE] OMAP_ALIGNED;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800151 size_t digcnt;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800152 size_t bufcnt;
153 size_t buflen;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800154
155 /* walk state */
156 struct scatterlist *sg;
Tero Kristof19de1b2016-09-19 18:22:15 +0300157 struct scatterlist sgl[2];
Tero Kristo8043bb12016-09-19 18:22:17 +0300158 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
Gustavo A. R. Silva5a8a0762020-02-24 10:21:00 -0600162 u8 buffer[] 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
Gustavo A. R. Silva5a8a0762020-02-24 10:21:00 -0600179 struct omap_sham_hmac_ctx base[];
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800180};
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 Kristoc28e8f22017-05-24 10:35:34 +0300230 u8 xmit_buf[BUFLEN] OMAP_ALIGNED;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800231
232 unsigned long flags;
Tero Kristoc9af5992018-02-27 15:30:36 +0200233 int fallback_sz;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800234 struct crypto_queue queue;
235 struct ahash_request *req;
Mark A. Greer0d373d62012-12-21 10:04:08 -0700236
237 const struct omap_sham_pdata *pdata;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800238};
239
240struct omap_sham_drv {
241 struct list_head dev_list;
242 spinlock_t lock;
243 unsigned long flags;
244};
245
246static struct omap_sham_drv sham = {
247 .dev_list = LIST_HEAD_INIT(sham.dev_list),
248 .lock = __SPIN_LOCK_UNLOCKED(sham.lock),
249};
250
251static inline u32 omap_sham_read(struct omap_sham_dev *dd, u32 offset)
252{
253 return __raw_readl(dd->io_base + offset);
254}
255
256static inline void omap_sham_write(struct omap_sham_dev *dd,
257 u32 offset, u32 value)
258{
259 __raw_writel(value, dd->io_base + offset);
260}
261
262static inline void omap_sham_write_mask(struct omap_sham_dev *dd, u32 address,
263 u32 value, u32 mask)
264{
265 u32 val;
266
267 val = omap_sham_read(dd, address);
268 val &= ~mask;
269 val |= value;
270 omap_sham_write(dd, address, val);
271}
272
273static inline int omap_sham_wait(struct omap_sham_dev *dd, u32 offset, u32 bit)
274{
275 unsigned long timeout = jiffies + DEFAULT_TIMEOUT_INTERVAL;
276
277 while (!(omap_sham_read(dd, offset) & bit)) {
278 if (time_is_before_jiffies(timeout))
279 return -ETIMEDOUT;
280 }
281
282 return 0;
283}
284
Mark A. Greer0d373d62012-12-21 10:04:08 -0700285static void omap_sham_copy_hash_omap2(struct ahash_request *req, int out)
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800286{
287 struct omap_sham_reqctx *ctx = ahash_request_ctx(req);
Mark A. Greer0d373d62012-12-21 10:04:08 -0700288 struct omap_sham_dev *dd = ctx->dd;
Dmitry Kasatkin0c3cf4c2010-11-19 16:04:22 +0200289 u32 *hash = (u32 *)ctx->digest;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800290 int i;
291
Mark A. Greer0d373d62012-12-21 10:04:08 -0700292 for (i = 0; i < dd->pdata->digest_size / sizeof(u32); i++) {
Dmitry Kasatkin3c8d7582010-11-19 16:04:27 +0200293 if (out)
Mark A. Greer0d373d62012-12-21 10:04:08 -0700294 hash[i] = omap_sham_read(dd, SHA_REG_IDIGEST(dd, i));
Dmitry Kasatkin3c8d7582010-11-19 16:04:27 +0200295 else
Mark A. Greer0d373d62012-12-21 10:04:08 -0700296 omap_sham_write(dd, SHA_REG_IDIGEST(dd, i), hash[i]);
Dmitry Kasatkin3c8d7582010-11-19 16:04:27 +0200297 }
298}
299
Mark A. Greer0d373d62012-12-21 10:04:08 -0700300static void omap_sham_copy_hash_omap4(struct ahash_request *req, int out)
301{
302 struct omap_sham_reqctx *ctx = ahash_request_ctx(req);
303 struct omap_sham_dev *dd = ctx->dd;
304 int i;
305
306 if (ctx->flags & BIT(FLAGS_HMAC)) {
307 struct crypto_ahash *tfm = crypto_ahash_reqtfm(dd->req);
308 struct omap_sham_ctx *tctx = crypto_ahash_ctx(tfm);
309 struct omap_sham_hmac_ctx *bctx = tctx->base;
310 u32 *opad = (u32 *)bctx->opad;
311
312 for (i = 0; i < dd->pdata->digest_size / sizeof(u32); i++) {
313 if (out)
314 opad[i] = omap_sham_read(dd,
Lokesh Vutlaeaef7e32013-07-26 12:29:14 +0530315 SHA_REG_ODIGEST(dd, i));
Mark A. Greer0d373d62012-12-21 10:04:08 -0700316 else
Lokesh Vutlaeaef7e32013-07-26 12:29:14 +0530317 omap_sham_write(dd, SHA_REG_ODIGEST(dd, i),
Mark A. Greer0d373d62012-12-21 10:04:08 -0700318 opad[i]);
319 }
320 }
321
322 omap_sham_copy_hash_omap2(req, out);
323}
324
Dmitry Kasatkin3c8d7582010-11-19 16:04:27 +0200325static void omap_sham_copy_ready_hash(struct ahash_request *req)
326{
327 struct omap_sham_reqctx *ctx = ahash_request_ctx(req);
328 u32 *in = (u32 *)ctx->digest;
329 u32 *hash = (u32 *)req->result;
Mark A. Greer0d373d62012-12-21 10:04:08 -0700330 int i, d, big_endian = 0;
Dmitry Kasatkin3c8d7582010-11-19 16:04:27 +0200331
332 if (!hash)
333 return;
334
Mark A. Greer0d373d62012-12-21 10:04:08 -0700335 switch (ctx->flags & FLAGS_MODE_MASK) {
336 case FLAGS_MODE_MD5:
337 d = MD5_DIGEST_SIZE / sizeof(u32);
338 break;
339 case FLAGS_MODE_SHA1:
340 /* OMAP2 SHA1 is big endian */
341 if (test_bit(FLAGS_BE32_SHA1, &ctx->dd->flags))
342 big_endian = 1;
343 d = SHA1_DIGEST_SIZE / sizeof(u32);
344 break;
Mark A. Greerd20fb182012-12-21 10:04:09 -0700345 case FLAGS_MODE_SHA224:
346 d = SHA224_DIGEST_SIZE / sizeof(u32);
347 break;
348 case FLAGS_MODE_SHA256:
349 d = SHA256_DIGEST_SIZE / sizeof(u32);
350 break;
Lokesh Vutlaeaef7e32013-07-26 12:29:14 +0530351 case FLAGS_MODE_SHA384:
352 d = SHA384_DIGEST_SIZE / sizeof(u32);
353 break;
354 case FLAGS_MODE_SHA512:
355 d = SHA512_DIGEST_SIZE / sizeof(u32);
356 break;
Mark A. Greer0d373d62012-12-21 10:04:08 -0700357 default:
358 d = 0;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800359 }
Mark A. Greer0d373d62012-12-21 10:04:08 -0700360
361 if (big_endian)
362 for (i = 0; i < d; i++)
363 hash[i] = be32_to_cpu(in[i]);
364 else
365 for (i = 0; i < d; i++)
366 hash[i] = le32_to_cpu(in[i]);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800367}
368
Dmitry Kasatkin798eed5d2010-11-19 16:04:26 +0200369static int omap_sham_hw_init(struct omap_sham_dev *dd)
370{
Pali Rohár604c3102015-03-08 11:01:01 +0100371 int err;
372
373 err = pm_runtime_get_sync(dd->dev);
374 if (err < 0) {
375 dev_err(dd->dev, "failed to get sync: %d\n", err);
376 return err;
377 }
Dmitry Kasatkin798eed5d2010-11-19 16:04:26 +0200378
Dmitry Kasatkina929cbe2011-06-02 21:10:06 +0300379 if (!test_bit(FLAGS_INIT, &dd->flags)) {
Dmitry Kasatkina929cbe2011-06-02 21:10:06 +0300380 set_bit(FLAGS_INIT, &dd->flags);
Dmitry Kasatkin798eed5d2010-11-19 16:04:26 +0200381 dd->err = 0;
382 }
383
384 return 0;
385}
386
Mark A. Greer0d373d62012-12-21 10:04:08 -0700387static void omap_sham_write_ctrl_omap2(struct omap_sham_dev *dd, size_t length,
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800388 int final, int dma)
389{
390 struct omap_sham_reqctx *ctx = ahash_request_ctx(dd->req);
391 u32 val = length << 5, mask;
392
Dmitry Kasatkin798eed5d2010-11-19 16:04:26 +0200393 if (likely(ctx->digcnt))
Mark A. Greer0d373d62012-12-21 10:04:08 -0700394 omap_sham_write(dd, SHA_REG_DIGCNT(dd), ctx->digcnt);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800395
Mark A. Greer0d373d62012-12-21 10:04:08 -0700396 omap_sham_write_mask(dd, SHA_REG_MASK(dd),
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800397 SHA_REG_MASK_IT_EN | (dma ? SHA_REG_MASK_DMA_EN : 0),
398 SHA_REG_MASK_IT_EN | SHA_REG_MASK_DMA_EN);
399 /*
400 * Setting ALGO_CONST only for the first iteration
401 * and CLOSE_HASH only for the last one.
402 */
Mark A. Greer0d373d62012-12-21 10:04:08 -0700403 if ((ctx->flags & FLAGS_MODE_MASK) == FLAGS_MODE_SHA1)
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800404 val |= SHA_REG_CTRL_ALGO;
405 if (!ctx->digcnt)
406 val |= SHA_REG_CTRL_ALGO_CONST;
407 if (final)
408 val |= SHA_REG_CTRL_CLOSE_HASH;
409
410 mask = SHA_REG_CTRL_ALGO_CONST | SHA_REG_CTRL_CLOSE_HASH |
411 SHA_REG_CTRL_ALGO | SHA_REG_CTRL_LENGTH;
412
413 omap_sham_write_mask(dd, SHA_REG_CTRL, val, mask);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800414}
415
Mark A. Greer0d373d62012-12-21 10:04:08 -0700416static void omap_sham_trigger_omap2(struct omap_sham_dev *dd, size_t length)
417{
418}
419
420static int omap_sham_poll_irq_omap2(struct omap_sham_dev *dd)
421{
422 return omap_sham_wait(dd, SHA_REG_CTRL, SHA_REG_CTRL_INPUT_READY);
423}
424
Lokesh Vutlaeaef7e32013-07-26 12:29:14 +0530425static int get_block_size(struct omap_sham_reqctx *ctx)
426{
427 int d;
428
429 switch (ctx->flags & FLAGS_MODE_MASK) {
430 case FLAGS_MODE_MD5:
431 case FLAGS_MODE_SHA1:
432 d = SHA1_BLOCK_SIZE;
433 break;
434 case FLAGS_MODE_SHA224:
435 case FLAGS_MODE_SHA256:
436 d = SHA256_BLOCK_SIZE;
437 break;
438 case FLAGS_MODE_SHA384:
439 case FLAGS_MODE_SHA512:
440 d = SHA512_BLOCK_SIZE;
441 break;
442 default:
443 d = 0;
444 }
445
446 return d;
447}
448
Mark A. Greer0d373d62012-12-21 10:04:08 -0700449static void omap_sham_write_n(struct omap_sham_dev *dd, u32 offset,
450 u32 *value, int count)
451{
452 for (; count--; value++, offset += 4)
453 omap_sham_write(dd, offset, *value);
454}
455
456static void omap_sham_write_ctrl_omap4(struct omap_sham_dev *dd, size_t length,
457 int final, int dma)
458{
459 struct omap_sham_reqctx *ctx = ahash_request_ctx(dd->req);
460 u32 val, mask;
461
462 /*
463 * Setting ALGO_CONST only for the first iteration and
464 * CLOSE_HASH only for the last one. Note that flags mode bits
465 * correspond to algorithm encoding in mode register.
466 */
Lokesh Vutlaeaef7e32013-07-26 12:29:14 +0530467 val = (ctx->flags & FLAGS_MODE_MASK) >> (FLAGS_MODE_SHIFT);
Mark A. Greer0d373d62012-12-21 10:04:08 -0700468 if (!ctx->digcnt) {
469 struct crypto_ahash *tfm = crypto_ahash_reqtfm(dd->req);
470 struct omap_sham_ctx *tctx = crypto_ahash_ctx(tfm);
471 struct omap_sham_hmac_ctx *bctx = tctx->base;
Lokesh Vutlaeaef7e32013-07-26 12:29:14 +0530472 int bs, nr_dr;
Mark A. Greer0d373d62012-12-21 10:04:08 -0700473
474 val |= SHA_REG_MODE_ALGO_CONSTANT;
475
476 if (ctx->flags & BIT(FLAGS_HMAC)) {
Lokesh Vutlaeaef7e32013-07-26 12:29:14 +0530477 bs = get_block_size(ctx);
478 nr_dr = bs / (2 * sizeof(u32));
Mark A. Greer0d373d62012-12-21 10:04:08 -0700479 val |= SHA_REG_MODE_HMAC_KEY_PROC;
Lokesh Vutlaeaef7e32013-07-26 12:29:14 +0530480 omap_sham_write_n(dd, SHA_REG_ODIGEST(dd, 0),
481 (u32 *)bctx->ipad, nr_dr);
482 omap_sham_write_n(dd, SHA_REG_IDIGEST(dd, 0),
483 (u32 *)bctx->ipad + nr_dr, nr_dr);
484 ctx->digcnt += bs;
Mark A. Greer0d373d62012-12-21 10:04:08 -0700485 }
486 }
487
488 if (final) {
489 val |= SHA_REG_MODE_CLOSE_HASH;
490
491 if (ctx->flags & BIT(FLAGS_HMAC))
492 val |= SHA_REG_MODE_HMAC_OUTER_HASH;
493 }
494
495 mask = SHA_REG_MODE_ALGO_CONSTANT | SHA_REG_MODE_CLOSE_HASH |
496 SHA_REG_MODE_ALGO_MASK | SHA_REG_MODE_HMAC_OUTER_HASH |
497 SHA_REG_MODE_HMAC_KEY_PROC;
498
499 dev_dbg(dd->dev, "ctrl: %08x, flags: %08lx\n", val, ctx->flags);
Lokesh Vutlaeaef7e32013-07-26 12:29:14 +0530500 omap_sham_write_mask(dd, SHA_REG_MODE(dd), val, mask);
Mark A. Greer0d373d62012-12-21 10:04:08 -0700501 omap_sham_write(dd, SHA_REG_IRQENA, SHA_REG_IRQENA_OUTPUT_RDY);
502 omap_sham_write_mask(dd, SHA_REG_MASK(dd),
503 SHA_REG_MASK_IT_EN |
504 (dma ? SHA_REG_MASK_DMA_EN : 0),
505 SHA_REG_MASK_IT_EN | SHA_REG_MASK_DMA_EN);
506}
507
508static void omap_sham_trigger_omap4(struct omap_sham_dev *dd, size_t length)
509{
Lokesh Vutlaeaef7e32013-07-26 12:29:14 +0530510 omap_sham_write(dd, SHA_REG_LENGTH(dd), length);
Mark A. Greer0d373d62012-12-21 10:04:08 -0700511}
512
513static int omap_sham_poll_irq_omap4(struct omap_sham_dev *dd)
514{
515 return omap_sham_wait(dd, SHA_REG_IRQSTATUS,
516 SHA_REG_IRQSTATUS_INPUT_RDY);
517}
518
Tero Kristo8043bb12016-09-19 18:22:17 +0300519static int omap_sham_xmit_cpu(struct omap_sham_dev *dd, size_t length,
520 int final)
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800521{
522 struct omap_sham_reqctx *ctx = ahash_request_ctx(dd->req);
Lokesh Vutlab8411cc2013-08-20 20:32:34 +0530523 int count, len32, bs32, offset = 0;
Tero Kristo8043bb12016-09-19 18:22:17 +0300524 const u32 *buffer;
525 int mlen;
526 struct sg_mapping_iter mi;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800527
528 dev_dbg(dd->dev, "xmit_cpu: digcnt: %d, length: %d, final: %d\n",
529 ctx->digcnt, length, final);
530
Mark A. Greer0d373d62012-12-21 10:04:08 -0700531 dd->pdata->write_ctrl(dd, length, final, 0);
532 dd->pdata->trigger(dd, length);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800533
Dmitry Kasatkin3e133c82010-11-19 16:04:24 +0200534 /* should be non-zero before next lines to disable clocks later */
535 ctx->digcnt += length;
Tero Kristo8043bb12016-09-19 18:22:17 +0300536 ctx->total -= length;
Dmitry Kasatkin3e133c82010-11-19 16:04:24 +0200537
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800538 if (final)
Dmitry Kasatkined3ea9a82011-06-02 21:10:07 +0300539 set_bit(FLAGS_FINAL, &dd->flags); /* catch last interrupt */
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800540
Dmitry Kasatkin6c63db82011-06-02 21:10:10 +0300541 set_bit(FLAGS_CPU, &dd->flags);
542
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800543 len32 = DIV_ROUND_UP(length, sizeof(u32));
Lokesh Vutlab8411cc2013-08-20 20:32:34 +0530544 bs32 = get_block_size(ctx) / sizeof(u32);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800545
Tero Kristo8043bb12016-09-19 18:22:17 +0300546 sg_miter_start(&mi, ctx->sg, ctx->sg_len,
547 SG_MITER_FROM_SG | SG_MITER_ATOMIC);
548
549 mlen = 0;
550
Lokesh Vutlab8411cc2013-08-20 20:32:34 +0530551 while (len32) {
552 if (dd->pdata->poll_irq(dd))
553 return -ETIMEDOUT;
554
Tero Kristo8043bb12016-09-19 18:22:17 +0300555 for (count = 0; count < min(len32, bs32); count++, offset++) {
556 if (!mlen) {
557 sg_miter_next(&mi);
558 mlen = mi.length;
559 if (!mlen) {
560 pr_err("sg miter failure.\n");
561 return -EINVAL;
562 }
563 offset = 0;
564 buffer = mi.addr;
565 }
Lokesh Vutlab8411cc2013-08-20 20:32:34 +0530566 omap_sham_write(dd, SHA_REG_DIN(dd, count),
567 buffer[offset]);
Tero Kristo8043bb12016-09-19 18:22:17 +0300568 mlen -= 4;
569 }
Lokesh Vutlab8411cc2013-08-20 20:32:34 +0530570 len32 -= min(len32, bs32);
571 }
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800572
Tero Kristo8043bb12016-09-19 18:22:17 +0300573 sg_miter_stop(&mi);
574
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800575 return -EINPROGRESS;
576}
577
Mark A. Greerdfd061d2012-12-21 10:04:04 -0700578static void omap_sham_dma_callback(void *param)
579{
580 struct omap_sham_dev *dd = param;
581
582 set_bit(FLAGS_DMA_READY, &dd->flags);
583 tasklet_schedule(&dd->done_task);
584}
Mark A. Greerdfd061d2012-12-21 10:04:04 -0700585
Tero Kristo8043bb12016-09-19 18:22:17 +0300586static int omap_sham_xmit_dma(struct omap_sham_dev *dd, size_t length,
587 int final)
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800588{
589 struct omap_sham_reqctx *ctx = ahash_request_ctx(dd->req);
Mark A. Greerdfd061d2012-12-21 10:04:04 -0700590 struct dma_async_tx_descriptor *tx;
591 struct dma_slave_config cfg;
Tero Kristo8043bb12016-09-19 18:22:17 +0300592 int ret;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800593
594 dev_dbg(dd->dev, "xmit_dma: digcnt: %d, length: %d, final: %d\n",
595 ctx->digcnt, length, final);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800596
Tero Kristo8043bb12016-09-19 18:22:17 +0300597 if (!dma_map_sg(dd->dev, ctx->sg, ctx->sg_len, DMA_TO_DEVICE)) {
598 dev_err(dd->dev, "dma_map_sg error\n");
599 return -EINVAL;
600 }
601
Mark A. Greerdfd061d2012-12-21 10:04:04 -0700602 memset(&cfg, 0, sizeof(cfg));
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800603
Mark A. Greer0d373d62012-12-21 10:04:08 -0700604 cfg.dst_addr = dd->phys_base + SHA_REG_DIN(dd, 0);
Mark A. Greerdfd061d2012-12-21 10:04:04 -0700605 cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
Tero Kristo8043bb12016-09-19 18:22:17 +0300606 cfg.dst_maxburst = get_block_size(ctx) / DMA_SLAVE_BUSWIDTH_4_BYTES;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800607
Mark A. Greerdfd061d2012-12-21 10:04:04 -0700608 ret = dmaengine_slave_config(dd->dma_lch, &cfg);
609 if (ret) {
610 pr_err("omap-sham: can't configure dmaengine slave: %d\n", ret);
611 return ret;
612 }
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800613
Tero Kristo8043bb12016-09-19 18:22:17 +0300614 tx = dmaengine_prep_slave_sg(dd->dma_lch, ctx->sg, ctx->sg_len,
615 DMA_MEM_TO_DEV,
616 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
Mark A. Greerdfd061d2012-12-21 10:04:04 -0700617
618 if (!tx) {
Tero Kristo8043bb12016-09-19 18:22:17 +0300619 dev_err(dd->dev, "prep_slave_sg failed\n");
Mark A. Greerdfd061d2012-12-21 10:04:04 -0700620 return -EINVAL;
621 }
622
623 tx->callback = omap_sham_dma_callback;
624 tx->callback_param = dd;
Mark A. Greerdfd061d2012-12-21 10:04:04 -0700625
Mark A. Greer0d373d62012-12-21 10:04:08 -0700626 dd->pdata->write_ctrl(dd, length, final, 1);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800627
628 ctx->digcnt += length;
Tero Kristo8043bb12016-09-19 18:22:17 +0300629 ctx->total -= length;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800630
631 if (final)
Dmitry Kasatkined3ea9a82011-06-02 21:10:07 +0300632 set_bit(FLAGS_FINAL, &dd->flags); /* catch last interrupt */
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800633
Dmitry Kasatkina929cbe2011-06-02 21:10:06 +0300634 set_bit(FLAGS_DMA_ACTIVE, &dd->flags);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800635
Mark A. Greerdfd061d2012-12-21 10:04:04 -0700636 dmaengine_submit(tx);
637 dma_async_issue_pending(dd->dma_lch);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800638
Mark A. Greer0d373d62012-12-21 10:04:08 -0700639 dd->pdata->trigger(dd, length);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800640
641 return -EINPROGRESS;
642}
643
Tero Kristof19de1b2016-09-19 18:22:15 +0300644static int omap_sham_copy_sg_lists(struct omap_sham_reqctx *ctx,
645 struct scatterlist *sg, int bs, int new_len)
646{
647 int n = sg_nents(sg);
648 struct scatterlist *tmp;
649 int offset = ctx->offset;
650
Tero Kristo60a08942019-11-05 16:00:57 +0200651 ctx->total = new_len;
652
Tero Kristof19de1b2016-09-19 18:22:15 +0300653 if (ctx->bufcnt)
654 n++;
655
656 ctx->sg = kmalloc_array(n, sizeof(*sg), GFP_KERNEL);
657 if (!ctx->sg)
658 return -ENOMEM;
659
660 sg_init_table(ctx->sg, n);
661
662 tmp = ctx->sg;
663
664 ctx->sg_len = 0;
665
666 if (ctx->bufcnt) {
667 sg_set_buf(tmp, ctx->dd->xmit_buf, ctx->bufcnt);
668 tmp = sg_next(tmp);
669 ctx->sg_len++;
Tero Kristo60a08942019-11-05 16:00:57 +0200670 new_len -= ctx->bufcnt;
Tero Kristof19de1b2016-09-19 18:22:15 +0300671 }
672
673 while (sg && new_len) {
674 int len = sg->length - offset;
675
Tero Kristo1cfd9f32019-11-05 16:01:07 +0200676 if (len <= 0) {
Tero Kristof19de1b2016-09-19 18:22:15 +0300677 offset -= sg->length;
Tero Kristo1cfd9f32019-11-05 16:01:07 +0200678 sg = sg_next(sg);
679 continue;
Tero Kristof19de1b2016-09-19 18:22:15 +0300680 }
681
682 if (new_len < len)
683 len = new_len;
684
685 if (len > 0) {
686 new_len -= len;
Tero Kristo1cfd9f32019-11-05 16:01:07 +0200687 sg_set_page(tmp, sg_page(sg), len, sg->offset + offset);
688 offset = 0;
689 ctx->offset = 0;
Tero Kristof19de1b2016-09-19 18:22:15 +0300690 ctx->sg_len++;
Tero Kristo60a08942019-11-05 16:00:57 +0200691 if (new_len <= 0)
692 break;
693 tmp = sg_next(tmp);
Tero Kristof19de1b2016-09-19 18:22:15 +0300694 }
695
696 sg = sg_next(sg);
697 }
698
Tero Kristo60a08942019-11-05 16:00:57 +0200699 if (tmp)
700 sg_mark_end(tmp);
701
Tero Kristof19de1b2016-09-19 18:22:15 +0300702 set_bit(FLAGS_SGS_ALLOCED, &ctx->dd->flags);
703
Tero Kristo462519f2019-11-05 16:00:50 +0200704 ctx->offset += new_len - ctx->bufcnt;
Tero Kristof19de1b2016-09-19 18:22:15 +0300705 ctx->bufcnt = 0;
706
707 return 0;
708}
709
710static int omap_sham_copy_sgs(struct omap_sham_reqctx *ctx,
Tero Kristo462519f2019-11-05 16:00:50 +0200711 struct scatterlist *sg, int bs,
712 unsigned int new_len)
Tero Kristof19de1b2016-09-19 18:22:15 +0300713{
714 int pages;
715 void *buf;
Tero Kristof19de1b2016-09-19 18:22:15 +0300716
Tero Kristo462519f2019-11-05 16:00:50 +0200717 pages = get_order(new_len);
Tero Kristof19de1b2016-09-19 18:22:15 +0300718
719 buf = (void *)__get_free_pages(GFP_ATOMIC, pages);
720 if (!buf) {
721 pr_err("Couldn't allocate pages for unaligned cases.\n");
722 return -ENOMEM;
723 }
724
725 if (ctx->bufcnt)
726 memcpy(buf, ctx->dd->xmit_buf, ctx->bufcnt);
727
728 scatterwalk_map_and_copy(buf + ctx->bufcnt, sg, ctx->offset,
Tero Kristo462519f2019-11-05 16:00:50 +0200729 min(new_len, ctx->total) - ctx->bufcnt, 0);
Tero Kristof19de1b2016-09-19 18:22:15 +0300730 sg_init_table(ctx->sgl, 1);
Tero Kristo462519f2019-11-05 16:00:50 +0200731 sg_set_buf(ctx->sgl, buf, new_len);
Tero Kristof19de1b2016-09-19 18:22:15 +0300732 ctx->sg = ctx->sgl;
733 set_bit(FLAGS_SGS_COPIED, &ctx->dd->flags);
734 ctx->sg_len = 1;
Tero Kristo462519f2019-11-05 16:00:50 +0200735 ctx->offset += new_len - ctx->bufcnt;
Tero Kristof19de1b2016-09-19 18:22:15 +0300736 ctx->bufcnt = 0;
Tero Kristo60a08942019-11-05 16:00:57 +0200737 ctx->total = new_len;
Tero Kristof19de1b2016-09-19 18:22:15 +0300738
739 return 0;
740}
741
742static int omap_sham_align_sgs(struct scatterlist *sg,
743 int nbytes, int bs, bool final,
744 struct omap_sham_reqctx *rctx)
745{
746 int n = 0;
747 bool aligned = true;
748 bool list_ok = true;
749 struct scatterlist *sg_tmp = sg;
750 int new_len;
751 int offset = rctx->offset;
Tero Kristo2b352482019-11-05 16:00:55 +0200752 int bufcnt = rctx->bufcnt;
Tero Kristof19de1b2016-09-19 18:22:15 +0300753
754 if (!sg || !sg->length || !nbytes)
755 return 0;
756
Tero Kristo2b352482019-11-05 16:00:55 +0200757 new_len = nbytes;
Tero Kristof19de1b2016-09-19 18:22:15 +0300758
759 if (offset)
760 list_ok = false;
761
762 if (final)
763 new_len = DIV_ROUND_UP(new_len, bs) * bs;
764 else
Tero Kristo898d86a2017-05-24 10:35:33 +0300765 new_len = (new_len - 1) / bs * bs;
766
Tero Kristo462519f2019-11-05 16:00:50 +0200767 if (!new_len)
768 return 0;
769
Tero Kristo898d86a2017-05-24 10:35:33 +0300770 if (nbytes != new_len)
771 list_ok = false;
Tero Kristof19de1b2016-09-19 18:22:15 +0300772
773 while (nbytes > 0 && sg_tmp) {
774 n++;
775
Tero Kristo2b352482019-11-05 16:00:55 +0200776 if (bufcnt) {
777 if (!IS_ALIGNED(bufcnt, bs)) {
778 aligned = false;
779 break;
780 }
781 nbytes -= bufcnt;
782 bufcnt = 0;
Tero Kristo60a08942019-11-05 16:00:57 +0200783 if (!nbytes)
784 list_ok = false;
785
Tero Kristo2b352482019-11-05 16:00:55 +0200786 continue;
787 }
788
Tero Kristo4c219852018-02-27 15:30:34 +0200789#ifdef CONFIG_ZONE_DMA
790 if (page_zonenum(sg_page(sg_tmp)) != ZONE_DMA) {
791 aligned = false;
792 break;
793 }
794#endif
795
Tero Kristof19de1b2016-09-19 18:22:15 +0300796 if (offset < sg_tmp->length) {
797 if (!IS_ALIGNED(offset + sg_tmp->offset, 4)) {
798 aligned = false;
799 break;
800 }
801
802 if (!IS_ALIGNED(sg_tmp->length - offset, bs)) {
803 aligned = false;
804 break;
805 }
806 }
807
808 if (offset) {
809 offset -= sg_tmp->length;
810 if (offset < 0) {
811 nbytes += offset;
812 offset = 0;
813 }
814 } else {
815 nbytes -= sg_tmp->length;
816 }
817
818 sg_tmp = sg_next(sg_tmp);
819
820 if (nbytes < 0) {
821 list_ok = false;
822 break;
823 }
824 }
825
Tero Kristo462519f2019-11-05 16:00:50 +0200826 if (new_len > OMAP_SHA_MAX_DMA_LEN) {
827 new_len = OMAP_SHA_MAX_DMA_LEN;
828 aligned = false;
829 }
830
Tero Kristof19de1b2016-09-19 18:22:15 +0300831 if (!aligned)
832 return omap_sham_copy_sgs(rctx, sg, bs, new_len);
833 else if (!list_ok)
834 return omap_sham_copy_sg_lists(rctx, sg, bs, new_len);
835
Tero Kristo60a08942019-11-05 16:00:57 +0200836 rctx->total = new_len;
837 rctx->offset += new_len;
Tero Kristof19de1b2016-09-19 18:22:15 +0300838 rctx->sg_len = n;
Tero Kristo1cfd9f32019-11-05 16:01:07 +0200839 if (rctx->bufcnt) {
840 sg_init_table(rctx->sgl, 2);
841 sg_set_buf(rctx->sgl, rctx->dd->xmit_buf, rctx->bufcnt);
842 sg_chain(rctx->sgl, 2, sg);
843 rctx->sg = rctx->sgl;
844 } else {
845 rctx->sg = sg;
846 }
Tero Kristof19de1b2016-09-19 18:22:15 +0300847
848 return 0;
849}
850
851static int omap_sham_prepare_request(struct ahash_request *req, bool update)
852{
853 struct omap_sham_reqctx *rctx = ahash_request_ctx(req);
854 int bs;
855 int ret;
Tero Kristo60a08942019-11-05 16:00:57 +0200856 unsigned int nbytes;
Tero Kristof19de1b2016-09-19 18:22:15 +0300857 bool final = rctx->flags & BIT(FLAGS_FINUP);
Tero Kristo60a08942019-11-05 16:00:57 +0200858 int hash_later;
Tero Kristof19de1b2016-09-19 18:22:15 +0300859
Tero Kristof19de1b2016-09-19 18:22:15 +0300860 bs = get_block_size(rctx);
861
Tero Kristo60a08942019-11-05 16:00:57 +0200862 nbytes = rctx->bufcnt;
Tero Kristof19de1b2016-09-19 18:22:15 +0300863
Tero Kristo60a08942019-11-05 16:00:57 +0200864 if (update)
865 nbytes += req->nbytes - rctx->offset;
Tero Kristo462519f2019-11-05 16:00:50 +0200866
867 dev_dbg(rctx->dd->dev,
868 "%s: nbytes=%d, bs=%d, total=%d, offset=%d, bufcnt=%d\n",
869 __func__, nbytes, bs, rctx->total, rctx->offset,
870 rctx->bufcnt);
Tero Kristof19de1b2016-09-19 18:22:15 +0300871
Tero Kristo60a08942019-11-05 16:00:57 +0200872 if (!nbytes)
Tero Kristof19de1b2016-09-19 18:22:15 +0300873 return 0;
874
Tero Kristo60a08942019-11-05 16:00:57 +0200875 rctx->total = nbytes;
876
877 if (update && req->nbytes && (!IS_ALIGNED(rctx->bufcnt, bs))) {
Tero Kristof19de1b2016-09-19 18:22:15 +0300878 int len = bs - rctx->bufcnt % bs;
879
Tero Kristo60a08942019-11-05 16:00:57 +0200880 if (len > req->nbytes)
881 len = req->nbytes;
Tero Kristof19de1b2016-09-19 18:22:15 +0300882 scatterwalk_map_and_copy(rctx->buffer + rctx->bufcnt, req->src,
883 0, len, 0);
884 rctx->bufcnt += len;
Tero Kristof19de1b2016-09-19 18:22:15 +0300885 rctx->offset = len;
886 }
887
888 if (rctx->bufcnt)
889 memcpy(rctx->dd->xmit_buf, rctx->buffer, rctx->bufcnt);
890
Tero Kristo60a08942019-11-05 16:00:57 +0200891 ret = omap_sham_align_sgs(req->src, nbytes, bs, final, rctx);
Tero Kristof19de1b2016-09-19 18:22:15 +0300892 if (ret)
893 return ret;
894
Tero Kristo60a08942019-11-05 16:00:57 +0200895 hash_later = nbytes - rctx->total;
Tero Kristof19de1b2016-09-19 18:22:15 +0300896 if (hash_later < 0)
897 hash_later = 0;
898
Tero Kristo60a08942019-11-05 16:00:57 +0200899 if (hash_later) {
900 scatterwalk_map_and_copy(rctx->buffer,
901 req->src,
902 req->nbytes - hash_later,
903 hash_later, 0);
Tero Kristo5d78d572017-05-24 10:35:32 +0300904
Tero Kristof19de1b2016-09-19 18:22:15 +0300905 rctx->bufcnt = hash_later;
906 } else {
907 rctx->bufcnt = 0;
908 }
909
Tero Kristo462519f2019-11-05 16:00:50 +0200910 if (hash_later > rctx->buflen)
911 set_bit(FLAGS_HUGE, &rctx->dd->flags);
912
Tero Kristo60a08942019-11-05 16:00:57 +0200913 rctx->total = min(nbytes, rctx->total);
Tero Kristof19de1b2016-09-19 18:22:15 +0300914
915 return 0;
916}
917
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800918static int omap_sham_update_dma_stop(struct omap_sham_dev *dd)
919{
920 struct omap_sham_reqctx *ctx = ahash_request_ctx(dd->req);
921
Tero Kristo8043bb12016-09-19 18:22:17 +0300922 dma_unmap_sg(dd->dev, ctx->sg, ctx->sg_len, DMA_TO_DEVICE);
Mark A. Greerdfd061d2012-12-21 10:04:04 -0700923
Tero Kristo8043bb12016-09-19 18:22:17 +0300924 clear_bit(FLAGS_DMA_ACTIVE, &dd->flags);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800925
926 return 0;
927}
928
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800929static int omap_sham_init(struct ahash_request *req)
930{
931 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
932 struct omap_sham_ctx *tctx = crypto_ahash_ctx(tfm);
933 struct omap_sham_reqctx *ctx = ahash_request_ctx(req);
934 struct omap_sham_dev *dd = NULL, *tmp;
Lokesh Vutlaeaef7e32013-07-26 12:29:14 +0530935 int bs = 0;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800936
937 spin_lock_bh(&sham.lock);
938 if (!tctx->dd) {
939 list_for_each_entry(tmp, &sham.dev_list, list) {
940 dd = tmp;
941 break;
942 }
943 tctx->dd = dd;
944 } else {
945 dd = tctx->dd;
946 }
947 spin_unlock_bh(&sham.lock);
948
949 ctx->dd = dd;
950
951 ctx->flags = 0;
952
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800953 dev_dbg(dd->dev, "init: digest size: %d\n",
954 crypto_ahash_digestsize(tfm));
955
Mark A. Greer0d373d62012-12-21 10:04:08 -0700956 switch (crypto_ahash_digestsize(tfm)) {
957 case MD5_DIGEST_SIZE:
958 ctx->flags |= FLAGS_MODE_MD5;
Lokesh Vutlaeaef7e32013-07-26 12:29:14 +0530959 bs = SHA1_BLOCK_SIZE;
Mark A. Greer0d373d62012-12-21 10:04:08 -0700960 break;
961 case SHA1_DIGEST_SIZE:
962 ctx->flags |= FLAGS_MODE_SHA1;
Lokesh Vutlaeaef7e32013-07-26 12:29:14 +0530963 bs = SHA1_BLOCK_SIZE;
Mark A. Greer0d373d62012-12-21 10:04:08 -0700964 break;
Mark A. Greerd20fb182012-12-21 10:04:09 -0700965 case SHA224_DIGEST_SIZE:
966 ctx->flags |= FLAGS_MODE_SHA224;
Lokesh Vutlaeaef7e32013-07-26 12:29:14 +0530967 bs = SHA224_BLOCK_SIZE;
Mark A. Greerd20fb182012-12-21 10:04:09 -0700968 break;
969 case SHA256_DIGEST_SIZE:
970 ctx->flags |= FLAGS_MODE_SHA256;
Lokesh Vutlaeaef7e32013-07-26 12:29:14 +0530971 bs = SHA256_BLOCK_SIZE;
972 break;
973 case SHA384_DIGEST_SIZE:
974 ctx->flags |= FLAGS_MODE_SHA384;
975 bs = SHA384_BLOCK_SIZE;
976 break;
977 case SHA512_DIGEST_SIZE:
978 ctx->flags |= FLAGS_MODE_SHA512;
979 bs = SHA512_BLOCK_SIZE;
Mark A. Greerd20fb182012-12-21 10:04:09 -0700980 break;
Mark A. Greer0d373d62012-12-21 10:04:08 -0700981 }
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800982
983 ctx->bufcnt = 0;
984 ctx->digcnt = 0;
Tero Kristo8043bb12016-09-19 18:22:17 +0300985 ctx->total = 0;
986 ctx->offset = 0;
Dmitry Kasatkin798eed5d2010-11-19 16:04:26 +0200987 ctx->buflen = BUFLEN;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800988
Dmitry Kasatkinea1fd222011-06-02 21:10:05 +0300989 if (tctx->flags & BIT(FLAGS_HMAC)) {
Mark A. Greer0d373d62012-12-21 10:04:08 -0700990 if (!test_bit(FLAGS_AUTO_XOR, &dd->flags)) {
991 struct omap_sham_hmac_ctx *bctx = tctx->base;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800992
Lokesh Vutlaeaef7e32013-07-26 12:29:14 +0530993 memcpy(ctx->buffer, bctx->ipad, bs);
994 ctx->bufcnt = bs;
Mark A. Greer0d373d62012-12-21 10:04:08 -0700995 }
996
Dmitry Kasatkinea1fd222011-06-02 21:10:05 +0300997 ctx->flags |= BIT(FLAGS_HMAC);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800998 }
999
1000 return 0;
1001
1002}
1003
1004static int omap_sham_update_req(struct omap_sham_dev *dd)
1005{
1006 struct ahash_request *req = dd->req;
1007 struct omap_sham_reqctx *ctx = ahash_request_ctx(req);
1008 int err;
Tero Kristo462519f2019-11-05 16:00:50 +02001009 bool final = (ctx->flags & BIT(FLAGS_FINUP)) &&
1010 !(dd->flags & BIT(FLAGS_HUGE));
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001011
Tero Kristo462519f2019-11-05 16:00:50 +02001012 dev_dbg(dd->dev, "update_req: total: %u, digcnt: %d, final: %d",
1013 ctx->total, ctx->digcnt, final);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001014
Tero Kristo8043bb12016-09-19 18:22:17 +03001015 if (ctx->total < get_block_size(ctx) ||
Tero Kristoc9af5992018-02-27 15:30:36 +02001016 ctx->total < dd->fallback_sz)
Tero Kristo8043bb12016-09-19 18:22:17 +03001017 ctx->flags |= BIT(FLAGS_CPU);
1018
Dmitry Kasatkinea1fd222011-06-02 21:10:05 +03001019 if (ctx->flags & BIT(FLAGS_CPU))
Tero Kristo8043bb12016-09-19 18:22:17 +03001020 err = omap_sham_xmit_cpu(dd, ctx->total, final);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001021 else
Tero Kristo8043bb12016-09-19 18:22:17 +03001022 err = omap_sham_xmit_dma(dd, ctx->total, final);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001023
1024 /* wait for dma completion before can take more data */
1025 dev_dbg(dd->dev, "update: err: %d, digcnt: %d\n", err, ctx->digcnt);
1026
1027 return err;
1028}
1029
1030static int omap_sham_final_req(struct omap_sham_dev *dd)
1031{
1032 struct ahash_request *req = dd->req;
1033 struct omap_sham_reqctx *ctx = ahash_request_ctx(req);
1034 int err = 0, use_dma = 1;
1035
Tero Kristo462519f2019-11-05 16:00:50 +02001036 if (dd->flags & BIT(FLAGS_HUGE))
1037 return 0;
1038
Tero Kristo8043bb12016-09-19 18:22:17 +03001039 if ((ctx->total <= get_block_size(ctx)) || dd->polling_mode)
Lokesh Vutlab8411cc2013-08-20 20:32:34 +05301040 /*
1041 * faster to handle last block with cpu or
1042 * use cpu when dma is not present.
1043 */
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001044 use_dma = 0;
1045
1046 if (use_dma)
Tero Kristo8043bb12016-09-19 18:22:17 +03001047 err = omap_sham_xmit_dma(dd, ctx->total, 1);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001048 else
Tero Kristo8043bb12016-09-19 18:22:17 +03001049 err = omap_sham_xmit_cpu(dd, ctx->total, 1);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001050
1051 ctx->bufcnt = 0;
1052
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001053 dev_dbg(dd->dev, "final_req: err: %d\n", err);
1054
1055 return err;
1056}
1057
Dmitry Kasatkinbf362752011-04-20 13:34:58 +03001058static int omap_sham_finish_hmac(struct ahash_request *req)
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001059{
1060 struct omap_sham_ctx *tctx = crypto_tfm_ctx(req->base.tfm);
1061 struct omap_sham_hmac_ctx *bctx = tctx->base;
1062 int bs = crypto_shash_blocksize(bctx->shash);
1063 int ds = crypto_shash_digestsize(bctx->shash);
Behan Webster7bc53c32014-04-04 18:18:00 -03001064 SHASH_DESC_ON_STACK(shash, bctx->shash);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001065
Behan Webster7bc53c32014-04-04 18:18:00 -03001066 shash->tfm = bctx->shash;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001067
Behan Webster7bc53c32014-04-04 18:18:00 -03001068 return crypto_shash_init(shash) ?:
1069 crypto_shash_update(shash, bctx->opad, bs) ?:
1070 crypto_shash_finup(shash, req->result, ds, req->result);
Dmitry Kasatkinbf362752011-04-20 13:34:58 +03001071}
1072
1073static int omap_sham_finish(struct ahash_request *req)
1074{
1075 struct omap_sham_reqctx *ctx = ahash_request_ctx(req);
1076 struct omap_sham_dev *dd = ctx->dd;
1077 int err = 0;
1078
1079 if (ctx->digcnt) {
1080 omap_sham_copy_ready_hash(req);
Mark A. Greer0d373d62012-12-21 10:04:08 -07001081 if ((ctx->flags & BIT(FLAGS_HMAC)) &&
1082 !test_bit(FLAGS_AUTO_XOR, &dd->flags))
Dmitry Kasatkinbf362752011-04-20 13:34:58 +03001083 err = omap_sham_finish_hmac(req);
1084 }
1085
1086 dev_dbg(dd->dev, "digcnt: %d, bufcnt: %d\n", ctx->digcnt, ctx->bufcnt);
1087
1088 return err;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001089}
1090
1091static void omap_sham_finish_req(struct ahash_request *req, int err)
1092{
1093 struct omap_sham_reqctx *ctx = ahash_request_ctx(req);
Dmitry Kasatkin798eed5d2010-11-19 16:04:26 +02001094 struct omap_sham_dev *dd = ctx->dd;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001095
Tero Kristo8043bb12016-09-19 18:22:17 +03001096 if (test_bit(FLAGS_SGS_COPIED, &dd->flags))
1097 free_pages((unsigned long)sg_virt(ctx->sg),
Tero Kristo462519f2019-11-05 16:00:50 +02001098 get_order(ctx->sg->length));
Tero Kristo8043bb12016-09-19 18:22:17 +03001099
1100 if (test_bit(FLAGS_SGS_ALLOCED, &dd->flags))
1101 kfree(ctx->sg);
1102
1103 ctx->sg = NULL;
1104
1105 dd->flags &= ~(BIT(FLAGS_SGS_ALLOCED) | BIT(FLAGS_SGS_COPIED));
1106
Tero Kristo462519f2019-11-05 16:00:50 +02001107 if (dd->flags & BIT(FLAGS_HUGE)) {
1108 dd->flags &= ~(BIT(FLAGS_CPU) | BIT(FLAGS_DMA_READY) |
1109 BIT(FLAGS_OUTPUT_READY) | BIT(FLAGS_HUGE));
1110 omap_sham_prepare_request(req, ctx->op == OP_UPDATE);
1111 if (ctx->op == OP_UPDATE || (dd->flags & BIT(FLAGS_HUGE))) {
1112 err = omap_sham_update_req(dd);
1113 if (err != -EINPROGRESS &&
1114 (ctx->flags & BIT(FLAGS_FINUP)))
1115 err = omap_sham_final_req(dd);
1116 } else if (ctx->op == OP_FINAL) {
1117 omap_sham_final_req(dd);
1118 }
1119 return;
1120 }
1121
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001122 if (!err) {
Mark A. Greer0d373d62012-12-21 10:04:08 -07001123 dd->pdata->copy_hash(req, 1);
Dmitry Kasatkined3ea9a82011-06-02 21:10:07 +03001124 if (test_bit(FLAGS_FINAL, &dd->flags))
Dmitry Kasatkinbf362752011-04-20 13:34:58 +03001125 err = omap_sham_finish(req);
Dmitry Kasatkin3e133c82010-11-19 16:04:24 +02001126 } else {
Dmitry Kasatkinea1fd222011-06-02 21:10:05 +03001127 ctx->flags |= BIT(FLAGS_ERROR);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001128 }
1129
Dmitry Kasatkin0efd4d82011-06-02 21:10:12 +03001130 /* atomic operation is not needed here */
1131 dd->flags &= ~(BIT(FLAGS_BUSY) | BIT(FLAGS_FINAL) | BIT(FLAGS_CPU) |
1132 BIT(FLAGS_DMA_READY) | BIT(FLAGS_OUTPUT_READY));
Mark A. Greerb359f032012-12-21 10:04:02 -07001133
Tero Kristoe93f7672016-06-22 16:23:34 +03001134 pm_runtime_mark_last_busy(dd->dev);
1135 pm_runtime_put_autosuspend(dd->dev);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001136
Tero Kristo462519f2019-11-05 16:00:50 +02001137 ctx->offset = 0;
1138
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001139 if (req->base.complete)
1140 req->base.complete(&req->base, err);
1141}
1142
Dmitry Kasatkina5d87232010-11-19 16:04:25 +02001143static int omap_sham_handle_queue(struct omap_sham_dev *dd,
1144 struct ahash_request *req)
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001145{
Dmitry Kasatkin6c39d112010-12-29 21:52:04 +11001146 struct crypto_async_request *async_req, *backlog;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001147 struct omap_sham_reqctx *ctx;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001148 unsigned long flags;
Dmitry Kasatkina5d87232010-11-19 16:04:25 +02001149 int err = 0, ret = 0;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001150
Tero Kristo4e7813a2016-08-04 13:28:36 +03001151retry:
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001152 spin_lock_irqsave(&dd->lock, flags);
Dmitry Kasatkina5d87232010-11-19 16:04:25 +02001153 if (req)
1154 ret = ahash_enqueue_request(&dd->queue, req);
Dmitry Kasatkina929cbe2011-06-02 21:10:06 +03001155 if (test_bit(FLAGS_BUSY, &dd->flags)) {
Dmitry Kasatkina5d87232010-11-19 16:04:25 +02001156 spin_unlock_irqrestore(&dd->lock, flags);
1157 return ret;
1158 }
Dmitry Kasatkin6c39d112010-12-29 21:52:04 +11001159 backlog = crypto_get_backlog(&dd->queue);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001160 async_req = crypto_dequeue_request(&dd->queue);
Dmitry Kasatkin6c39d112010-12-29 21:52:04 +11001161 if (async_req)
Dmitry Kasatkina929cbe2011-06-02 21:10:06 +03001162 set_bit(FLAGS_BUSY, &dd->flags);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001163 spin_unlock_irqrestore(&dd->lock, flags);
1164
1165 if (!async_req)
Dmitry Kasatkina5d87232010-11-19 16:04:25 +02001166 return ret;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001167
1168 if (backlog)
1169 backlog->complete(backlog, -EINPROGRESS);
1170
1171 req = ahash_request_cast(async_req);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001172 dd->req = req;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001173 ctx = ahash_request_ctx(req);
1174
Tero Kristo8043bb12016-09-19 18:22:17 +03001175 err = omap_sham_prepare_request(req, ctx->op == OP_UPDATE);
Tero Kristo898d86a2017-05-24 10:35:33 +03001176 if (err || !ctx->total)
Tero Kristof19de1b2016-09-19 18:22:15 +03001177 goto err1;
1178
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001179 dev_dbg(dd->dev, "handling new req, op: %lu, nbytes: %d\n",
1180 ctx->op, req->nbytes);
1181
Dmitry Kasatkin798eed5d2010-11-19 16:04:26 +02001182 err = omap_sham_hw_init(dd);
1183 if (err)
1184 goto err1;
1185
Dmitry Kasatkin798eed5d2010-11-19 16:04:26 +02001186 if (ctx->digcnt)
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001187 /* request has changed - restore hash */
Mark A. Greer0d373d62012-12-21 10:04:08 -07001188 dd->pdata->copy_hash(req, 0);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001189
Tero Kristo462519f2019-11-05 16:00:50 +02001190 if (ctx->op == OP_UPDATE || (dd->flags & BIT(FLAGS_HUGE))) {
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001191 err = omap_sham_update_req(dd);
Dmitry Kasatkinea1fd222011-06-02 21:10:05 +03001192 if (err != -EINPROGRESS && (ctx->flags & BIT(FLAGS_FINUP)))
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001193 /* no final() after finup() */
1194 err = omap_sham_final_req(dd);
1195 } else if (ctx->op == OP_FINAL) {
1196 err = omap_sham_final_req(dd);
1197 }
Dmitry Kasatkin798eed5d2010-11-19 16:04:26 +02001198err1:
Tero Kristo4e7813a2016-08-04 13:28:36 +03001199 dev_dbg(dd->dev, "exit, err: %d\n", err);
1200
1201 if (err != -EINPROGRESS) {
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001202 /* done_task will not finish it, so do it here */
1203 omap_sham_finish_req(req, err);
Tero Kristo4e7813a2016-08-04 13:28:36 +03001204 req = NULL;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001205
Tero Kristo4e7813a2016-08-04 13:28:36 +03001206 /*
1207 * Execute next request immediately if there is anything
1208 * in queue.
1209 */
1210 goto retry;
1211 }
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001212
Dmitry Kasatkina5d87232010-11-19 16:04:25 +02001213 return ret;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001214}
1215
1216static int omap_sham_enqueue(struct ahash_request *req, unsigned int op)
1217{
1218 struct omap_sham_reqctx *ctx = ahash_request_ctx(req);
1219 struct omap_sham_ctx *tctx = crypto_tfm_ctx(req->base.tfm);
1220 struct omap_sham_dev *dd = tctx->dd;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001221
1222 ctx->op = op;
1223
Dmitry Kasatkina5d87232010-11-19 16:04:25 +02001224 return omap_sham_handle_queue(dd, req);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001225}
1226
1227static int omap_sham_update(struct ahash_request *req)
1228{
1229 struct omap_sham_reqctx *ctx = ahash_request_ctx(req);
Lokesh Vutlab8411cc2013-08-20 20:32:34 +05301230 struct omap_sham_dev *dd = ctx->dd;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001231
1232 if (!req->nbytes)
1233 return 0;
1234
Tero Kristo5d78d572017-05-24 10:35:32 +03001235 if (ctx->bufcnt + req->nbytes <= ctx->buflen) {
Tero Kristo8043bb12016-09-19 18:22:17 +03001236 scatterwalk_map_and_copy(ctx->buffer + ctx->bufcnt, req->src,
1237 0, req->nbytes, 0);
1238 ctx->bufcnt += req->nbytes;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001239 return 0;
1240 }
1241
Lokesh Vutlaacef7b02013-12-18 19:03:33 +05301242 if (dd->polling_mode)
1243 ctx->flags |= BIT(FLAGS_CPU);
1244
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001245 return omap_sham_enqueue(req, OP_UPDATE);
1246}
1247
Behan Webster7bc53c32014-04-04 18:18:00 -03001248static int omap_sham_shash_digest(struct crypto_shash *tfm, u32 flags,
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001249 const u8 *data, unsigned int len, u8 *out)
1250{
Behan Webster7bc53c32014-04-04 18:18:00 -03001251 SHASH_DESC_ON_STACK(shash, tfm);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001252
Behan Webster7bc53c32014-04-04 18:18:00 -03001253 shash->tfm = tfm;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001254
Behan Webster7bc53c32014-04-04 18:18:00 -03001255 return crypto_shash_digest(shash, data, len, out);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001256}
1257
1258static int omap_sham_final_shash(struct ahash_request *req)
1259{
1260 struct omap_sham_ctx *tctx = crypto_tfm_ctx(req->base.tfm);
1261 struct omap_sham_reqctx *ctx = ahash_request_ctx(req);
Tero Kristocb8d5c82016-08-04 13:28:40 +03001262 int offset = 0;
1263
1264 /*
1265 * If we are running HMAC on limited hardware support, skip
1266 * the ipad in the beginning of the buffer if we are going for
1267 * software fallback algorithm.
1268 */
1269 if (test_bit(FLAGS_HMAC, &ctx->flags) &&
1270 !test_bit(FLAGS_AUTO_XOR, &ctx->dd->flags))
1271 offset = get_block_size(ctx);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001272
1273 return omap_sham_shash_digest(tctx->fallback, req->base.flags,
Tero Kristocb8d5c82016-08-04 13:28:40 +03001274 ctx->buffer + offset,
1275 ctx->bufcnt - offset, req->result);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001276}
1277
1278static int omap_sham_final(struct ahash_request *req)
1279{
1280 struct omap_sham_reqctx *ctx = ahash_request_ctx(req);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001281
Dmitry Kasatkinea1fd222011-06-02 21:10:05 +03001282 ctx->flags |= BIT(FLAGS_FINUP);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001283
Dmitry Kasatkinea1fd222011-06-02 21:10:05 +03001284 if (ctx->flags & BIT(FLAGS_ERROR))
Dmitry Kasatkinbf362752011-04-20 13:34:58 +03001285 return 0; /* uncompleted hash is not needed */
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001286
Bin Liu85e06872016-06-22 16:23:37 +03001287 /*
1288 * OMAP HW accel works only with buffers >= 9.
1289 * HMAC is always >= 9 because ipad == block size.
Tero Kristoc9af5992018-02-27 15:30:36 +02001290 * If buffersize is less than fallback_sz, we use fallback
Tero Kristo2c5bd1e2016-09-19 18:22:16 +03001291 * SW encoding, as using DMA + HW in this case doesn't provide
1292 * any benefit.
Bin Liu85e06872016-06-22 16:23:37 +03001293 */
Tero Kristoc9af5992018-02-27 15:30:36 +02001294 if (!ctx->digcnt && ctx->bufcnt < ctx->dd->fallback_sz)
Dmitry Kasatkinbf362752011-04-20 13:34:58 +03001295 return omap_sham_final_shash(req);
1296 else if (ctx->bufcnt)
1297 return omap_sham_enqueue(req, OP_FINAL);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001298
Dmitry Kasatkinbf362752011-04-20 13:34:58 +03001299 /* copy ready hash (+ finalize hmac) */
1300 return omap_sham_finish(req);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001301}
1302
1303static int omap_sham_finup(struct ahash_request *req)
1304{
1305 struct omap_sham_reqctx *ctx = ahash_request_ctx(req);
1306 int err1, err2;
1307
Dmitry Kasatkinea1fd222011-06-02 21:10:05 +03001308 ctx->flags |= BIT(FLAGS_FINUP);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001309
1310 err1 = omap_sham_update(req);
Markku Kylanpaa455e3382011-04-20 13:34:55 +03001311 if (err1 == -EINPROGRESS || err1 == -EBUSY)
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001312 return err1;
1313 /*
1314 * final() has to be always called to cleanup resources
1315 * even if udpate() failed, except EINPROGRESS
1316 */
1317 err2 = omap_sham_final(req);
1318
1319 return err1 ?: err2;
1320}
1321
1322static int omap_sham_digest(struct ahash_request *req)
1323{
1324 return omap_sham_init(req) ?: omap_sham_finup(req);
1325}
1326
1327static int omap_sham_setkey(struct crypto_ahash *tfm, const u8 *key,
1328 unsigned int keylen)
1329{
1330 struct omap_sham_ctx *tctx = crypto_ahash_ctx(tfm);
1331 struct omap_sham_hmac_ctx *bctx = tctx->base;
1332 int bs = crypto_shash_blocksize(bctx->shash);
1333 int ds = crypto_shash_digestsize(bctx->shash);
Mark A. Greer0d373d62012-12-21 10:04:08 -07001334 struct omap_sham_dev *dd = NULL, *tmp;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001335 int err, i;
Mark A. Greer0d373d62012-12-21 10:04:08 -07001336
1337 spin_lock_bh(&sham.lock);
1338 if (!tctx->dd) {
1339 list_for_each_entry(tmp, &sham.dev_list, list) {
1340 dd = tmp;
1341 break;
1342 }
1343 tctx->dd = dd;
1344 } else {
1345 dd = tctx->dd;
1346 }
1347 spin_unlock_bh(&sham.lock);
1348
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001349 err = crypto_shash_setkey(tctx->fallback, key, keylen);
1350 if (err)
1351 return err;
1352
1353 if (keylen > bs) {
1354 err = omap_sham_shash_digest(bctx->shash,
1355 crypto_shash_get_flags(bctx->shash),
1356 key, keylen, bctx->ipad);
1357 if (err)
1358 return err;
1359 keylen = ds;
1360 } else {
1361 memcpy(bctx->ipad, key, keylen);
1362 }
1363
1364 memset(bctx->ipad + keylen, 0, bs - keylen);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001365
Mark A. Greer0d373d62012-12-21 10:04:08 -07001366 if (!test_bit(FLAGS_AUTO_XOR, &dd->flags)) {
1367 memcpy(bctx->opad, bctx->ipad, bs);
1368
1369 for (i = 0; i < bs; i++) {
Corentin LABBEebd401e2017-05-19 08:53:28 +02001370 bctx->ipad[i] ^= HMAC_IPAD_VALUE;
1371 bctx->opad[i] ^= HMAC_OPAD_VALUE;
Mark A. Greer0d373d62012-12-21 10:04:08 -07001372 }
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001373 }
1374
1375 return err;
1376}
1377
1378static int omap_sham_cra_init_alg(struct crypto_tfm *tfm, const char *alg_base)
1379{
1380 struct omap_sham_ctx *tctx = crypto_tfm_ctx(tfm);
1381 const char *alg_name = crypto_tfm_alg_name(tfm);
1382
1383 /* Allocate a fallback and abort if it failed. */
1384 tctx->fallback = crypto_alloc_shash(alg_name, 0,
1385 CRYPTO_ALG_NEED_FALLBACK);
1386 if (IS_ERR(tctx->fallback)) {
1387 pr_err("omap-sham: fallback driver '%s' "
1388 "could not be loaded.\n", alg_name);
1389 return PTR_ERR(tctx->fallback);
1390 }
1391
1392 crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
Dmitry Kasatkin798eed5d2010-11-19 16:04:26 +02001393 sizeof(struct omap_sham_reqctx) + BUFLEN);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001394
1395 if (alg_base) {
1396 struct omap_sham_hmac_ctx *bctx = tctx->base;
Dmitry Kasatkinea1fd222011-06-02 21:10:05 +03001397 tctx->flags |= BIT(FLAGS_HMAC);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001398 bctx->shash = crypto_alloc_shash(alg_base, 0,
1399 CRYPTO_ALG_NEED_FALLBACK);
1400 if (IS_ERR(bctx->shash)) {
1401 pr_err("omap-sham: base driver '%s' "
1402 "could not be loaded.\n", alg_base);
1403 crypto_free_shash(tctx->fallback);
1404 return PTR_ERR(bctx->shash);
1405 }
1406
1407 }
1408
1409 return 0;
1410}
1411
1412static int omap_sham_cra_init(struct crypto_tfm *tfm)
1413{
1414 return omap_sham_cra_init_alg(tfm, NULL);
1415}
1416
1417static int omap_sham_cra_sha1_init(struct crypto_tfm *tfm)
1418{
1419 return omap_sham_cra_init_alg(tfm, "sha1");
1420}
1421
Mark A. Greerd20fb182012-12-21 10:04:09 -07001422static int omap_sham_cra_sha224_init(struct crypto_tfm *tfm)
1423{
1424 return omap_sham_cra_init_alg(tfm, "sha224");
1425}
1426
1427static int omap_sham_cra_sha256_init(struct crypto_tfm *tfm)
1428{
1429 return omap_sham_cra_init_alg(tfm, "sha256");
1430}
1431
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001432static int omap_sham_cra_md5_init(struct crypto_tfm *tfm)
1433{
1434 return omap_sham_cra_init_alg(tfm, "md5");
1435}
1436
Lokesh Vutlaeaef7e32013-07-26 12:29:14 +05301437static int omap_sham_cra_sha384_init(struct crypto_tfm *tfm)
1438{
1439 return omap_sham_cra_init_alg(tfm, "sha384");
1440}
1441
1442static int omap_sham_cra_sha512_init(struct crypto_tfm *tfm)
1443{
1444 return omap_sham_cra_init_alg(tfm, "sha512");
1445}
1446
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001447static void omap_sham_cra_exit(struct crypto_tfm *tfm)
1448{
1449 struct omap_sham_ctx *tctx = crypto_tfm_ctx(tfm);
1450
1451 crypto_free_shash(tctx->fallback);
1452 tctx->fallback = NULL;
1453
Dmitry Kasatkinea1fd222011-06-02 21:10:05 +03001454 if (tctx->flags & BIT(FLAGS_HMAC)) {
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001455 struct omap_sham_hmac_ctx *bctx = tctx->base;
1456 crypto_free_shash(bctx->shash);
1457 }
1458}
1459
Tero Kristo99a7fff2016-09-19 18:22:12 +03001460static int omap_sham_export(struct ahash_request *req, void *out)
1461{
Tero Kristoa84d3512016-09-19 18:22:18 +03001462 struct omap_sham_reqctx *rctx = ahash_request_ctx(req);
1463
1464 memcpy(out, rctx, sizeof(*rctx) + rctx->bufcnt);
1465
1466 return 0;
Tero Kristo99a7fff2016-09-19 18:22:12 +03001467}
1468
1469static int omap_sham_import(struct ahash_request *req, const void *in)
1470{
Tero Kristoa84d3512016-09-19 18:22:18 +03001471 struct omap_sham_reqctx *rctx = ahash_request_ctx(req);
1472 const struct omap_sham_reqctx *ctx_in = in;
1473
1474 memcpy(rctx, in, sizeof(*rctx) + ctx_in->bufcnt);
1475
1476 return 0;
Tero Kristo99a7fff2016-09-19 18:22:12 +03001477}
1478
Mark A. Greerd20fb182012-12-21 10:04:09 -07001479static struct ahash_alg algs_sha1_md5[] = {
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001480{
1481 .init = omap_sham_init,
1482 .update = omap_sham_update,
1483 .final = omap_sham_final,
1484 .finup = omap_sham_finup,
1485 .digest = omap_sham_digest,
1486 .halg.digestsize = SHA1_DIGEST_SIZE,
1487 .halg.base = {
1488 .cra_name = "sha1",
1489 .cra_driver_name = "omap-sha1",
Bin Liueb354782016-06-30 14:04:11 -05001490 .cra_priority = 400,
Eric Biggers6a38f622018-06-30 15:16:12 -07001491 .cra_flags = CRYPTO_ALG_KERN_DRIVER_ONLY |
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001492 CRYPTO_ALG_ASYNC |
1493 CRYPTO_ALG_NEED_FALLBACK,
1494 .cra_blocksize = SHA1_BLOCK_SIZE,
1495 .cra_ctxsize = sizeof(struct omap_sham_ctx),
Tero Kristo744e6862016-09-19 18:22:13 +03001496 .cra_alignmask = OMAP_ALIGN_MASK,
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001497 .cra_module = THIS_MODULE,
1498 .cra_init = omap_sham_cra_init,
1499 .cra_exit = omap_sham_cra_exit,
1500 }
1501},
1502{
1503 .init = omap_sham_init,
1504 .update = omap_sham_update,
1505 .final = omap_sham_final,
1506 .finup = omap_sham_finup,
1507 .digest = omap_sham_digest,
1508 .halg.digestsize = MD5_DIGEST_SIZE,
1509 .halg.base = {
1510 .cra_name = "md5",
1511 .cra_driver_name = "omap-md5",
Bin Liueb354782016-06-30 14:04:11 -05001512 .cra_priority = 400,
Eric Biggers6a38f622018-06-30 15:16:12 -07001513 .cra_flags = CRYPTO_ALG_KERN_DRIVER_ONLY |
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001514 CRYPTO_ALG_ASYNC |
1515 CRYPTO_ALG_NEED_FALLBACK,
1516 .cra_blocksize = SHA1_BLOCK_SIZE,
1517 .cra_ctxsize = sizeof(struct omap_sham_ctx),
Dmitry Kasatkin798eed5d2010-11-19 16:04:26 +02001518 .cra_alignmask = OMAP_ALIGN_MASK,
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001519 .cra_module = THIS_MODULE,
1520 .cra_init = omap_sham_cra_init,
1521 .cra_exit = omap_sham_cra_exit,
1522 }
1523},
1524{
1525 .init = omap_sham_init,
1526 .update = omap_sham_update,
1527 .final = omap_sham_final,
1528 .finup = omap_sham_finup,
1529 .digest = omap_sham_digest,
1530 .setkey = omap_sham_setkey,
1531 .halg.digestsize = SHA1_DIGEST_SIZE,
1532 .halg.base = {
1533 .cra_name = "hmac(sha1)",
1534 .cra_driver_name = "omap-hmac-sha1",
Bin Liueb354782016-06-30 14:04:11 -05001535 .cra_priority = 400,
Eric Biggers6a38f622018-06-30 15:16:12 -07001536 .cra_flags = CRYPTO_ALG_KERN_DRIVER_ONLY |
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001537 CRYPTO_ALG_ASYNC |
1538 CRYPTO_ALG_NEED_FALLBACK,
1539 .cra_blocksize = SHA1_BLOCK_SIZE,
1540 .cra_ctxsize = sizeof(struct omap_sham_ctx) +
1541 sizeof(struct omap_sham_hmac_ctx),
Dmitry Kasatkin798eed5d2010-11-19 16:04:26 +02001542 .cra_alignmask = OMAP_ALIGN_MASK,
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001543 .cra_module = THIS_MODULE,
1544 .cra_init = omap_sham_cra_sha1_init,
1545 .cra_exit = omap_sham_cra_exit,
1546 }
1547},
1548{
1549 .init = omap_sham_init,
1550 .update = omap_sham_update,
1551 .final = omap_sham_final,
1552 .finup = omap_sham_finup,
1553 .digest = omap_sham_digest,
1554 .setkey = omap_sham_setkey,
1555 .halg.digestsize = MD5_DIGEST_SIZE,
1556 .halg.base = {
1557 .cra_name = "hmac(md5)",
1558 .cra_driver_name = "omap-hmac-md5",
Bin Liueb354782016-06-30 14:04:11 -05001559 .cra_priority = 400,
Eric Biggers6a38f622018-06-30 15:16:12 -07001560 .cra_flags = CRYPTO_ALG_KERN_DRIVER_ONLY |
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001561 CRYPTO_ALG_ASYNC |
1562 CRYPTO_ALG_NEED_FALLBACK,
1563 .cra_blocksize = SHA1_BLOCK_SIZE,
1564 .cra_ctxsize = sizeof(struct omap_sham_ctx) +
1565 sizeof(struct omap_sham_hmac_ctx),
Dmitry Kasatkin798eed5d2010-11-19 16:04:26 +02001566 .cra_alignmask = OMAP_ALIGN_MASK,
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001567 .cra_module = THIS_MODULE,
1568 .cra_init = omap_sham_cra_md5_init,
1569 .cra_exit = omap_sham_cra_exit,
1570 }
1571}
1572};
1573
Mark A. Greerd20fb182012-12-21 10:04:09 -07001574/* OMAP4 has some algs in addition to what OMAP2 has */
1575static struct ahash_alg algs_sha224_sha256[] = {
1576{
1577 .init = omap_sham_init,
1578 .update = omap_sham_update,
1579 .final = omap_sham_final,
1580 .finup = omap_sham_finup,
1581 .digest = omap_sham_digest,
1582 .halg.digestsize = SHA224_DIGEST_SIZE,
1583 .halg.base = {
1584 .cra_name = "sha224",
1585 .cra_driver_name = "omap-sha224",
Bin Liueb354782016-06-30 14:04:11 -05001586 .cra_priority = 400,
Eric Biggers6a38f622018-06-30 15:16:12 -07001587 .cra_flags = CRYPTO_ALG_ASYNC |
Mark A. Greerd20fb182012-12-21 10:04:09 -07001588 CRYPTO_ALG_NEED_FALLBACK,
1589 .cra_blocksize = SHA224_BLOCK_SIZE,
1590 .cra_ctxsize = sizeof(struct omap_sham_ctx),
Tero Kristo744e6862016-09-19 18:22:13 +03001591 .cra_alignmask = OMAP_ALIGN_MASK,
Mark A. Greerd20fb182012-12-21 10:04:09 -07001592 .cra_module = THIS_MODULE,
1593 .cra_init = omap_sham_cra_init,
1594 .cra_exit = omap_sham_cra_exit,
1595 }
1596},
1597{
1598 .init = omap_sham_init,
1599 .update = omap_sham_update,
1600 .final = omap_sham_final,
1601 .finup = omap_sham_finup,
1602 .digest = omap_sham_digest,
1603 .halg.digestsize = SHA256_DIGEST_SIZE,
1604 .halg.base = {
1605 .cra_name = "sha256",
1606 .cra_driver_name = "omap-sha256",
Bin Liueb354782016-06-30 14:04:11 -05001607 .cra_priority = 400,
Eric Biggers6a38f622018-06-30 15:16:12 -07001608 .cra_flags = CRYPTO_ALG_ASYNC |
Mark A. Greerd20fb182012-12-21 10:04:09 -07001609 CRYPTO_ALG_NEED_FALLBACK,
1610 .cra_blocksize = SHA256_BLOCK_SIZE,
1611 .cra_ctxsize = sizeof(struct omap_sham_ctx),
Tero Kristo744e6862016-09-19 18:22:13 +03001612 .cra_alignmask = OMAP_ALIGN_MASK,
Mark A. Greerd20fb182012-12-21 10:04:09 -07001613 .cra_module = THIS_MODULE,
1614 .cra_init = omap_sham_cra_init,
1615 .cra_exit = omap_sham_cra_exit,
1616 }
1617},
1618{
1619 .init = omap_sham_init,
1620 .update = omap_sham_update,
1621 .final = omap_sham_final,
1622 .finup = omap_sham_finup,
1623 .digest = omap_sham_digest,
1624 .setkey = omap_sham_setkey,
1625 .halg.digestsize = SHA224_DIGEST_SIZE,
1626 .halg.base = {
1627 .cra_name = "hmac(sha224)",
1628 .cra_driver_name = "omap-hmac-sha224",
Bin Liueb354782016-06-30 14:04:11 -05001629 .cra_priority = 400,
Eric Biggers6a38f622018-06-30 15:16:12 -07001630 .cra_flags = CRYPTO_ALG_ASYNC |
Mark A. Greerd20fb182012-12-21 10:04:09 -07001631 CRYPTO_ALG_NEED_FALLBACK,
1632 .cra_blocksize = SHA224_BLOCK_SIZE,
1633 .cra_ctxsize = sizeof(struct omap_sham_ctx) +
1634 sizeof(struct omap_sham_hmac_ctx),
1635 .cra_alignmask = OMAP_ALIGN_MASK,
1636 .cra_module = THIS_MODULE,
1637 .cra_init = omap_sham_cra_sha224_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 .setkey = omap_sham_setkey,
1648 .halg.digestsize = SHA256_DIGEST_SIZE,
1649 .halg.base = {
1650 .cra_name = "hmac(sha256)",
1651 .cra_driver_name = "omap-hmac-sha256",
Bin Liueb354782016-06-30 14:04:11 -05001652 .cra_priority = 400,
Eric Biggers6a38f622018-06-30 15:16:12 -07001653 .cra_flags = CRYPTO_ALG_ASYNC |
Mark A. Greerd20fb182012-12-21 10:04:09 -07001654 CRYPTO_ALG_NEED_FALLBACK,
1655 .cra_blocksize = SHA256_BLOCK_SIZE,
1656 .cra_ctxsize = sizeof(struct omap_sham_ctx) +
1657 sizeof(struct omap_sham_hmac_ctx),
1658 .cra_alignmask = OMAP_ALIGN_MASK,
1659 .cra_module = THIS_MODULE,
1660 .cra_init = omap_sham_cra_sha256_init,
1661 .cra_exit = omap_sham_cra_exit,
1662 }
1663},
1664};
1665
Lokesh Vutlaeaef7e32013-07-26 12:29:14 +05301666static struct ahash_alg algs_sha384_sha512[] = {
1667{
1668 .init = omap_sham_init,
1669 .update = omap_sham_update,
1670 .final = omap_sham_final,
1671 .finup = omap_sham_finup,
1672 .digest = omap_sham_digest,
1673 .halg.digestsize = SHA384_DIGEST_SIZE,
1674 .halg.base = {
1675 .cra_name = "sha384",
1676 .cra_driver_name = "omap-sha384",
Bin Liueb354782016-06-30 14:04:11 -05001677 .cra_priority = 400,
Eric Biggers6a38f622018-06-30 15:16:12 -07001678 .cra_flags = CRYPTO_ALG_ASYNC |
Lokesh Vutlaeaef7e32013-07-26 12:29:14 +05301679 CRYPTO_ALG_NEED_FALLBACK,
1680 .cra_blocksize = SHA384_BLOCK_SIZE,
1681 .cra_ctxsize = sizeof(struct omap_sham_ctx),
Tero Kristo744e6862016-09-19 18:22:13 +03001682 .cra_alignmask = OMAP_ALIGN_MASK,
Lokesh Vutlaeaef7e32013-07-26 12:29:14 +05301683 .cra_module = THIS_MODULE,
1684 .cra_init = omap_sham_cra_init,
1685 .cra_exit = omap_sham_cra_exit,
1686 }
1687},
1688{
1689 .init = omap_sham_init,
1690 .update = omap_sham_update,
1691 .final = omap_sham_final,
1692 .finup = omap_sham_finup,
1693 .digest = omap_sham_digest,
1694 .halg.digestsize = SHA512_DIGEST_SIZE,
1695 .halg.base = {
1696 .cra_name = "sha512",
1697 .cra_driver_name = "omap-sha512",
Bin Liueb354782016-06-30 14:04:11 -05001698 .cra_priority = 400,
Eric Biggers6a38f622018-06-30 15:16:12 -07001699 .cra_flags = CRYPTO_ALG_ASYNC |
Lokesh Vutlaeaef7e32013-07-26 12:29:14 +05301700 CRYPTO_ALG_NEED_FALLBACK,
1701 .cra_blocksize = SHA512_BLOCK_SIZE,
1702 .cra_ctxsize = sizeof(struct omap_sham_ctx),
Tero Kristo744e6862016-09-19 18:22:13 +03001703 .cra_alignmask = OMAP_ALIGN_MASK,
Lokesh Vutlaeaef7e32013-07-26 12:29:14 +05301704 .cra_module = THIS_MODULE,
1705 .cra_init = omap_sham_cra_init,
1706 .cra_exit = omap_sham_cra_exit,
1707 }
1708},
1709{
1710 .init = omap_sham_init,
1711 .update = omap_sham_update,
1712 .final = omap_sham_final,
1713 .finup = omap_sham_finup,
1714 .digest = omap_sham_digest,
1715 .setkey = omap_sham_setkey,
1716 .halg.digestsize = SHA384_DIGEST_SIZE,
1717 .halg.base = {
1718 .cra_name = "hmac(sha384)",
1719 .cra_driver_name = "omap-hmac-sha384",
Bin Liueb354782016-06-30 14:04:11 -05001720 .cra_priority = 400,
Eric Biggers6a38f622018-06-30 15:16:12 -07001721 .cra_flags = CRYPTO_ALG_ASYNC |
Lokesh Vutlaeaef7e32013-07-26 12:29:14 +05301722 CRYPTO_ALG_NEED_FALLBACK,
1723 .cra_blocksize = SHA384_BLOCK_SIZE,
1724 .cra_ctxsize = sizeof(struct omap_sham_ctx) +
1725 sizeof(struct omap_sham_hmac_ctx),
1726 .cra_alignmask = OMAP_ALIGN_MASK,
1727 .cra_module = THIS_MODULE,
1728 .cra_init = omap_sham_cra_sha384_init,
1729 .cra_exit = omap_sham_cra_exit,
1730 }
1731},
1732{
1733 .init = omap_sham_init,
1734 .update = omap_sham_update,
1735 .final = omap_sham_final,
1736 .finup = omap_sham_finup,
1737 .digest = omap_sham_digest,
1738 .setkey = omap_sham_setkey,
1739 .halg.digestsize = SHA512_DIGEST_SIZE,
1740 .halg.base = {
1741 .cra_name = "hmac(sha512)",
1742 .cra_driver_name = "omap-hmac-sha512",
Bin Liueb354782016-06-30 14:04:11 -05001743 .cra_priority = 400,
Eric Biggers6a38f622018-06-30 15:16:12 -07001744 .cra_flags = CRYPTO_ALG_ASYNC |
Lokesh Vutlaeaef7e32013-07-26 12:29:14 +05301745 CRYPTO_ALG_NEED_FALLBACK,
1746 .cra_blocksize = SHA512_BLOCK_SIZE,
1747 .cra_ctxsize = sizeof(struct omap_sham_ctx) +
1748 sizeof(struct omap_sham_hmac_ctx),
1749 .cra_alignmask = OMAP_ALIGN_MASK,
1750 .cra_module = THIS_MODULE,
1751 .cra_init = omap_sham_cra_sha512_init,
1752 .cra_exit = omap_sham_cra_exit,
1753 }
1754},
1755};
1756
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001757static void omap_sham_done_task(unsigned long data)
1758{
1759 struct omap_sham_dev *dd = (struct omap_sham_dev *)data;
Dmitry Kasatkin6c63db82011-06-02 21:10:10 +03001760 int err = 0;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001761
Tero Kristo462519f2019-11-05 16:00:50 +02001762 dev_dbg(dd->dev, "%s: flags=%lx\n", __func__, dd->flags);
1763
Dmitry Kasatkin6cb3ffe2011-06-02 21:10:09 +03001764 if (!test_bit(FLAGS_BUSY, &dd->flags)) {
1765 omap_sham_handle_queue(dd, NULL);
1766 return;
1767 }
1768
Dmitry Kasatkin6c63db82011-06-02 21:10:10 +03001769 if (test_bit(FLAGS_CPU, &dd->flags)) {
Tero Kristo8043bb12016-09-19 18:22:17 +03001770 if (test_and_clear_bit(FLAGS_OUTPUT_READY, &dd->flags))
1771 goto finish;
Dmitry Kasatkin6c63db82011-06-02 21:10:10 +03001772 } else if (test_bit(FLAGS_DMA_READY, &dd->flags)) {
1773 if (test_and_clear_bit(FLAGS_DMA_ACTIVE, &dd->flags)) {
1774 omap_sham_update_dma_stop(dd);
1775 if (dd->err) {
1776 err = dd->err;
1777 goto finish;
1778 }
1779 }
1780 if (test_and_clear_bit(FLAGS_OUTPUT_READY, &dd->flags)) {
1781 /* hash or semi-hash ready */
1782 clear_bit(FLAGS_DMA_READY, &dd->flags);
Krzysztof Kozlowski17f5b192018-03-01 21:50:11 +01001783 goto finish;
Dmitry Kasatkin6c63db82011-06-02 21:10:10 +03001784 }
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001785 }
1786
Dmitry Kasatkin6c63db82011-06-02 21:10:10 +03001787 return;
Dmitry Kasatkin3e133c82010-11-19 16:04:24 +02001788
Dmitry Kasatkin6c63db82011-06-02 21:10:10 +03001789finish:
1790 dev_dbg(dd->dev, "update done: err: %d\n", err);
1791 /* finish curent request */
1792 omap_sham_finish_req(dd->req, err);
Tero Kristo4e7813a2016-08-04 13:28:36 +03001793
1794 /* If we are not busy, process next req */
1795 if (!test_bit(FLAGS_BUSY, &dd->flags))
1796 omap_sham_handle_queue(dd, NULL);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001797}
1798
Mark A. Greer0d373d62012-12-21 10:04:08 -07001799static irqreturn_t omap_sham_irq_common(struct omap_sham_dev *dd)
1800{
1801 if (!test_bit(FLAGS_BUSY, &dd->flags)) {
1802 dev_warn(dd->dev, "Interrupt when no active requests.\n");
1803 } else {
1804 set_bit(FLAGS_OUTPUT_READY, &dd->flags);
1805 tasklet_schedule(&dd->done_task);
1806 }
1807
1808 return IRQ_HANDLED;
1809}
1810
1811static irqreturn_t omap_sham_irq_omap2(int irq, void *dev_id)
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001812{
1813 struct omap_sham_dev *dd = dev_id;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001814
Dmitry Kasatkined3ea9a82011-06-02 21:10:07 +03001815 if (unlikely(test_bit(FLAGS_FINAL, &dd->flags)))
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001816 /* final -> allow device to go to power-saving mode */
1817 omap_sham_write_mask(dd, SHA_REG_CTRL, 0, SHA_REG_CTRL_LENGTH);
1818
1819 omap_sham_write_mask(dd, SHA_REG_CTRL, SHA_REG_CTRL_OUTPUT_READY,
1820 SHA_REG_CTRL_OUTPUT_READY);
1821 omap_sham_read(dd, SHA_REG_CTRL);
1822
Mark A. Greer0d373d62012-12-21 10:04:08 -07001823 return omap_sham_irq_common(dd);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001824}
1825
Mark A. Greer0d373d62012-12-21 10:04:08 -07001826static irqreturn_t omap_sham_irq_omap4(int irq, void *dev_id)
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001827{
Mark A. Greer0d373d62012-12-21 10:04:08 -07001828 struct omap_sham_dev *dd = dev_id;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001829
Mark A. Greer0d373d62012-12-21 10:04:08 -07001830 omap_sham_write_mask(dd, SHA_REG_MASK(dd), 0, SHA_REG_MASK_IT_EN);
Dmitry Kasatkin3e133c82010-11-19 16:04:24 +02001831
Mark A. Greer0d373d62012-12-21 10:04:08 -07001832 return omap_sham_irq_common(dd);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001833}
1834
Mark A. Greerd20fb182012-12-21 10:04:09 -07001835static struct omap_sham_algs_info omap_sham_algs_info_omap2[] = {
1836 {
1837 .algs_list = algs_sha1_md5,
1838 .size = ARRAY_SIZE(algs_sha1_md5),
1839 },
1840};
1841
Mark A. Greer0d373d62012-12-21 10:04:08 -07001842static const struct omap_sham_pdata omap_sham_pdata_omap2 = {
Mark A. Greerd20fb182012-12-21 10:04:09 -07001843 .algs_info = omap_sham_algs_info_omap2,
1844 .algs_info_size = ARRAY_SIZE(omap_sham_algs_info_omap2),
Mark A. Greer0d373d62012-12-21 10:04:08 -07001845 .flags = BIT(FLAGS_BE32_SHA1),
1846 .digest_size = SHA1_DIGEST_SIZE,
1847 .copy_hash = omap_sham_copy_hash_omap2,
1848 .write_ctrl = omap_sham_write_ctrl_omap2,
1849 .trigger = omap_sham_trigger_omap2,
1850 .poll_irq = omap_sham_poll_irq_omap2,
1851 .intr_hdlr = omap_sham_irq_omap2,
1852 .idigest_ofs = 0x00,
1853 .din_ofs = 0x1c,
1854 .digcnt_ofs = 0x14,
1855 .rev_ofs = 0x5c,
1856 .mask_ofs = 0x60,
1857 .sysstatus_ofs = 0x64,
1858 .major_mask = 0xf0,
1859 .major_shift = 4,
1860 .minor_mask = 0x0f,
1861 .minor_shift = 0,
1862};
1863
Mark A. Greer03feec92012-12-21 10:04:06 -07001864#ifdef CONFIG_OF
Mark A. Greerd20fb182012-12-21 10:04:09 -07001865static struct omap_sham_algs_info omap_sham_algs_info_omap4[] = {
1866 {
1867 .algs_list = algs_sha1_md5,
1868 .size = ARRAY_SIZE(algs_sha1_md5),
1869 },
1870 {
1871 .algs_list = algs_sha224_sha256,
1872 .size = ARRAY_SIZE(algs_sha224_sha256),
1873 },
1874};
1875
Mark A. Greer0d373d62012-12-21 10:04:08 -07001876static const struct omap_sham_pdata omap_sham_pdata_omap4 = {
Mark A. Greerd20fb182012-12-21 10:04:09 -07001877 .algs_info = omap_sham_algs_info_omap4,
1878 .algs_info_size = ARRAY_SIZE(omap_sham_algs_info_omap4),
Mark A. Greer0d373d62012-12-21 10:04:08 -07001879 .flags = BIT(FLAGS_AUTO_XOR),
1880 .digest_size = SHA256_DIGEST_SIZE,
1881 .copy_hash = omap_sham_copy_hash_omap4,
1882 .write_ctrl = omap_sham_write_ctrl_omap4,
1883 .trigger = omap_sham_trigger_omap4,
1884 .poll_irq = omap_sham_poll_irq_omap4,
1885 .intr_hdlr = omap_sham_irq_omap4,
1886 .idigest_ofs = 0x020,
Lokesh Vutlaeaef7e32013-07-26 12:29:14 +05301887 .odigest_ofs = 0x0,
Mark A. Greer0d373d62012-12-21 10:04:08 -07001888 .din_ofs = 0x080,
1889 .digcnt_ofs = 0x040,
1890 .rev_ofs = 0x100,
1891 .mask_ofs = 0x110,
1892 .sysstatus_ofs = 0x114,
Lokesh Vutlaeaef7e32013-07-26 12:29:14 +05301893 .mode_ofs = 0x44,
1894 .length_ofs = 0x48,
Mark A. Greer0d373d62012-12-21 10:04:08 -07001895 .major_mask = 0x0700,
1896 .major_shift = 8,
1897 .minor_mask = 0x003f,
1898 .minor_shift = 0,
1899};
1900
Lokesh Vutla7d7c7042013-07-26 12:29:15 +05301901static struct omap_sham_algs_info omap_sham_algs_info_omap5[] = {
1902 {
1903 .algs_list = algs_sha1_md5,
1904 .size = ARRAY_SIZE(algs_sha1_md5),
1905 },
1906 {
1907 .algs_list = algs_sha224_sha256,
1908 .size = ARRAY_SIZE(algs_sha224_sha256),
1909 },
1910 {
1911 .algs_list = algs_sha384_sha512,
1912 .size = ARRAY_SIZE(algs_sha384_sha512),
1913 },
1914};
1915
1916static const struct omap_sham_pdata omap_sham_pdata_omap5 = {
1917 .algs_info = omap_sham_algs_info_omap5,
1918 .algs_info_size = ARRAY_SIZE(omap_sham_algs_info_omap5),
1919 .flags = BIT(FLAGS_AUTO_XOR),
1920 .digest_size = SHA512_DIGEST_SIZE,
1921 .copy_hash = omap_sham_copy_hash_omap4,
1922 .write_ctrl = omap_sham_write_ctrl_omap4,
1923 .trigger = omap_sham_trigger_omap4,
1924 .poll_irq = omap_sham_poll_irq_omap4,
1925 .intr_hdlr = omap_sham_irq_omap4,
1926 .idigest_ofs = 0x240,
1927 .odigest_ofs = 0x200,
1928 .din_ofs = 0x080,
1929 .digcnt_ofs = 0x280,
1930 .rev_ofs = 0x100,
1931 .mask_ofs = 0x110,
1932 .sysstatus_ofs = 0x114,
1933 .mode_ofs = 0x284,
1934 .length_ofs = 0x288,
1935 .major_mask = 0x0700,
1936 .major_shift = 8,
1937 .minor_mask = 0x003f,
1938 .minor_shift = 0,
1939};
1940
Mark A. Greer03feec92012-12-21 10:04:06 -07001941static const struct of_device_id omap_sham_of_match[] = {
1942 {
1943 .compatible = "ti,omap2-sham",
Mark A. Greer0d373d62012-12-21 10:04:08 -07001944 .data = &omap_sham_pdata_omap2,
1945 },
1946 {
Pali Roháreddca852015-02-26 14:49:53 +01001947 .compatible = "ti,omap3-sham",
1948 .data = &omap_sham_pdata_omap2,
1949 },
1950 {
Mark A. Greer0d373d62012-12-21 10:04:08 -07001951 .compatible = "ti,omap4-sham",
1952 .data = &omap_sham_pdata_omap4,
Mark A. Greer03feec92012-12-21 10:04:06 -07001953 },
Lokesh Vutla7d7c7042013-07-26 12:29:15 +05301954 {
1955 .compatible = "ti,omap5-sham",
1956 .data = &omap_sham_pdata_omap5,
1957 },
Mark A. Greer03feec92012-12-21 10:04:06 -07001958 {},
1959};
1960MODULE_DEVICE_TABLE(of, omap_sham_of_match);
1961
1962static int omap_sham_get_res_of(struct omap_sham_dev *dd,
1963 struct device *dev, struct resource *res)
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001964{
Mark A. Greer03feec92012-12-21 10:04:06 -07001965 struct device_node *node = dev->of_node;
Mark A. Greer03feec92012-12-21 10:04:06 -07001966 int err = 0;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001967
Corentin LABBE7d5569312017-09-20 20:42:48 +02001968 dd->pdata = of_device_get_match_data(dev);
1969 if (!dd->pdata) {
Mark A. Greer03feec92012-12-21 10:04:06 -07001970 dev_err(dev, "no compatible OF match\n");
1971 err = -EINVAL;
1972 goto err;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001973 }
Samu Onkalo584db6a2010-09-03 19:20:19 +08001974
Mark A. Greer03feec92012-12-21 10:04:06 -07001975 err = of_address_to_resource(node, 0, res);
1976 if (err < 0) {
1977 dev_err(dev, "can't translate OF node address\n");
1978 err = -EINVAL;
1979 goto err;
1980 }
1981
Thierry Redingf7578492013-09-18 15:24:44 +02001982 dd->irq = irq_of_parse_and_map(node, 0);
Mark A. Greer03feec92012-12-21 10:04:06 -07001983 if (!dd->irq) {
1984 dev_err(dev, "can't translate OF irq value\n");
1985 err = -EINVAL;
1986 goto err;
1987 }
1988
Mark A. Greer03feec92012-12-21 10:04:06 -07001989err:
1990 return err;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001991}
Mark A. Greer03feec92012-12-21 10:04:06 -07001992#else
Mark A. Greerc3c3b322013-01-15 13:53:02 -07001993static const struct of_device_id omap_sham_of_match[] = {
1994 {},
1995};
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001996
Mark A. Greerc3c3b322013-01-15 13:53:02 -07001997static int omap_sham_get_res_of(struct omap_sham_dev *dd,
Mark A. Greer03feec92012-12-21 10:04:06 -07001998 struct device *dev, struct resource *res)
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001999{
Mark A. Greer03feec92012-12-21 10:04:06 -07002000 return -EINVAL;
2001}
2002#endif
2003
2004static int omap_sham_get_res_pdev(struct omap_sham_dev *dd,
2005 struct platform_device *pdev, struct resource *res)
2006{
2007 struct device *dev = &pdev->dev;
2008 struct resource *r;
2009 int err = 0;
2010
2011 /* Get the base address */
2012 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2013 if (!r) {
2014 dev_err(dev, "no MEM resource info\n");
2015 err = -ENODEV;
2016 goto err;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08002017 }
Mark A. Greer03feec92012-12-21 10:04:06 -07002018 memcpy(res, r, sizeof(*res));
2019
2020 /* Get the IRQ */
2021 dd->irq = platform_get_irq(pdev, 0);
2022 if (dd->irq < 0) {
Mark A. Greer03feec92012-12-21 10:04:06 -07002023 err = dd->irq;
2024 goto err;
2025 }
2026
Mark A. Greer0d373d62012-12-21 10:04:08 -07002027 /* Only OMAP2/3 can be non-DT */
2028 dd->pdata = &omap_sham_pdata_omap2;
2029
Mark A. Greer03feec92012-12-21 10:04:06 -07002030err:
2031 return err;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08002032}
2033
Tero Kristoc9af5992018-02-27 15:30:36 +02002034static ssize_t fallback_show(struct device *dev, struct device_attribute *attr,
2035 char *buf)
2036{
2037 struct omap_sham_dev *dd = dev_get_drvdata(dev);
2038
2039 return sprintf(buf, "%d\n", dd->fallback_sz);
2040}
2041
2042static ssize_t fallback_store(struct device *dev, struct device_attribute *attr,
2043 const char *buf, size_t size)
2044{
2045 struct omap_sham_dev *dd = dev_get_drvdata(dev);
2046 ssize_t status;
2047 long value;
2048
2049 status = kstrtol(buf, 0, &value);
2050 if (status)
2051 return status;
2052
2053 /* HW accelerator only works with buffers > 9 */
2054 if (value < 9) {
2055 dev_err(dev, "minimum fallback size 9\n");
2056 return -EINVAL;
2057 }
2058
2059 dd->fallback_sz = value;
2060
2061 return size;
2062}
2063
Tero Kristo62f7c702018-02-27 15:30:37 +02002064static ssize_t queue_len_show(struct device *dev, struct device_attribute *attr,
2065 char *buf)
2066{
2067 struct omap_sham_dev *dd = dev_get_drvdata(dev);
2068
2069 return sprintf(buf, "%d\n", dd->queue.max_qlen);
2070}
2071
2072static ssize_t queue_len_store(struct device *dev,
2073 struct device_attribute *attr, const char *buf,
2074 size_t size)
2075{
2076 struct omap_sham_dev *dd = dev_get_drvdata(dev);
2077 ssize_t status;
2078 long value;
2079 unsigned long flags;
2080
2081 status = kstrtol(buf, 0, &value);
2082 if (status)
2083 return status;
2084
2085 if (value < 1)
2086 return -EINVAL;
2087
2088 /*
2089 * Changing the queue size in fly is safe, if size becomes smaller
2090 * than current size, it will just not accept new entries until
2091 * it has shrank enough.
2092 */
2093 spin_lock_irqsave(&dd->lock, flags);
2094 dd->queue.max_qlen = value;
2095 spin_unlock_irqrestore(&dd->lock, flags);
2096
2097 return size;
2098}
2099
2100static DEVICE_ATTR_RW(queue_len);
Tero Kristoc9af5992018-02-27 15:30:36 +02002101static DEVICE_ATTR_RW(fallback);
2102
2103static struct attribute *omap_sham_attrs[] = {
Tero Kristo62f7c702018-02-27 15:30:37 +02002104 &dev_attr_queue_len.attr,
Tero Kristoc9af5992018-02-27 15:30:36 +02002105 &dev_attr_fallback.attr,
2106 NULL,
2107};
2108
2109static struct attribute_group omap_sham_attr_group = {
2110 .attrs = omap_sham_attrs,
2111};
2112
Greg Kroah-Hartman49cfe4d2012-12-21 13:14:09 -08002113static int omap_sham_probe(struct platform_device *pdev)
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08002114{
2115 struct omap_sham_dev *dd;
2116 struct device *dev = &pdev->dev;
Mark A. Greer03feec92012-12-21 10:04:06 -07002117 struct resource res;
Mark A. Greerdfd061d2012-12-21 10:04:04 -07002118 dma_cap_mask_t mask;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08002119 int err, i, j;
Mark A. Greer0d373d62012-12-21 10:04:08 -07002120 u32 rev;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08002121
Lokesh Vutla7a7e4b72013-07-26 12:29:17 +05302122 dd = devm_kzalloc(dev, sizeof(struct omap_sham_dev), GFP_KERNEL);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08002123 if (dd == NULL) {
2124 dev_err(dev, "unable to alloc data struct.\n");
2125 err = -ENOMEM;
2126 goto data_err;
2127 }
2128 dd->dev = dev;
2129 platform_set_drvdata(pdev, dd);
2130
2131 INIT_LIST_HEAD(&dd->list);
2132 spin_lock_init(&dd->lock);
2133 tasklet_init(&dd->done_task, omap_sham_done_task, (unsigned long)dd);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08002134 crypto_init_queue(&dd->queue, OMAP_SHAM_QUEUE_LENGTH);
2135
Mark A. Greer03feec92012-12-21 10:04:06 -07002136 err = (dev->of_node) ? omap_sham_get_res_of(dd, dev, &res) :
2137 omap_sham_get_res_pdev(dd, pdev, &res);
2138 if (err)
Lokesh Vutla7a7e4b72013-07-26 12:29:17 +05302139 goto data_err;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08002140
Laurent Navet30862282013-05-02 14:00:38 +02002141 dd->io_base = devm_ioremap_resource(dev, &res);
2142 if (IS_ERR(dd->io_base)) {
2143 err = PTR_ERR(dd->io_base);
Lokesh Vutla7a7e4b72013-07-26 12:29:17 +05302144 goto data_err;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08002145 }
Mark A. Greer03feec92012-12-21 10:04:06 -07002146 dd->phys_base = res.start;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08002147
Lokesh Vutla0de9c382013-07-26 12:29:16 +05302148 err = devm_request_irq(dev, dd->irq, dd->pdata->intr_hdlr,
2149 IRQF_TRIGGER_NONE, dev_name(dev), dd);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08002150 if (err) {
Lokesh Vutla0de9c382013-07-26 12:29:16 +05302151 dev_err(dev, "unable to request irq %d, err = %d\n",
2152 dd->irq, err);
Lokesh Vutla7a7e4b72013-07-26 12:29:17 +05302153 goto data_err;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08002154 }
2155
Mark A. Greerdfd061d2012-12-21 10:04:04 -07002156 dma_cap_zero(mask);
2157 dma_cap_set(DMA_SLAVE, mask);
2158
Peter Ujfalusidbe24622016-04-29 16:03:41 +03002159 dd->dma_lch = dma_request_chan(dev, "rx");
2160 if (IS_ERR(dd->dma_lch)) {
2161 err = PTR_ERR(dd->dma_lch);
2162 if (err == -EPROBE_DEFER)
2163 goto data_err;
2164
Lokesh Vutlab8411cc2013-08-20 20:32:34 +05302165 dd->polling_mode = 1;
2166 dev_dbg(dev, "using polling mode instead of dma\n");
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08002167 }
2168
Mark A. Greer0d373d62012-12-21 10:04:08 -07002169 dd->flags |= dd->pdata->flags;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08002170
Tero Kristoe93f7672016-06-22 16:23:34 +03002171 pm_runtime_use_autosuspend(dev);
2172 pm_runtime_set_autosuspend_delay(dev, DEFAULT_AUTOSUSPEND_DELAY);
2173
Tero Kristoc9af5992018-02-27 15:30:36 +02002174 dd->fallback_sz = OMAP_SHA_DMA_THRESHOLD;
2175
Mark A. Greerb359f032012-12-21 10:04:02 -07002176 pm_runtime_enable(dev);
Vutla, Lokeshb0a3d892015-03-31 09:52:24 +05302177 pm_runtime_irq_safe(dev);
Pali Rohár604c3102015-03-08 11:01:01 +01002178
2179 err = pm_runtime_get_sync(dev);
2180 if (err < 0) {
2181 dev_err(dev, "failed to get sync: %d\n", err);
2182 goto err_pm;
2183 }
2184
Mark A. Greer0d373d62012-12-21 10:04:08 -07002185 rev = omap_sham_read(dd, SHA_REG_REV(dd));
2186 pm_runtime_put_sync(&pdev->dev);
Mark A. Greerb359f032012-12-21 10:04:02 -07002187
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08002188 dev_info(dev, "hw accel on OMAP rev %u.%u\n",
Mark A. Greer0d373d62012-12-21 10:04:08 -07002189 (rev & dd->pdata->major_mask) >> dd->pdata->major_shift,
2190 (rev & dd->pdata->minor_mask) >> dd->pdata->minor_shift);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08002191
2192 spin_lock(&sham.lock);
2193 list_add_tail(&dd->list, &sham.dev_list);
2194 spin_unlock(&sham.lock);
2195
Mark A. Greerd20fb182012-12-21 10:04:09 -07002196 for (i = 0; i < dd->pdata->algs_info_size; i++) {
2197 for (j = 0; j < dd->pdata->algs_info[i].size; j++) {
Tero Kristo99a7fff2016-09-19 18:22:12 +03002198 struct ahash_alg *alg;
2199
2200 alg = &dd->pdata->algs_info[i].algs_list[j];
2201 alg->export = omap_sham_export;
2202 alg->import = omap_sham_import;
Tero Kristoa84d3512016-09-19 18:22:18 +03002203 alg->halg.statesize = sizeof(struct omap_sham_reqctx) +
2204 BUFLEN;
Tero Kristo99a7fff2016-09-19 18:22:12 +03002205 err = crypto_register_ahash(alg);
Mark A. Greerd20fb182012-12-21 10:04:09 -07002206 if (err)
2207 goto err_algs;
2208
2209 dd->pdata->algs_info[i].registered++;
2210 }
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08002211 }
2212
Tero Kristoc9af5992018-02-27 15:30:36 +02002213 err = sysfs_create_group(&dev->kobj, &omap_sham_attr_group);
2214 if (err) {
2215 dev_err(dev, "could not create sysfs device attrs\n");
2216 goto err_algs;
2217 }
2218
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08002219 return 0;
2220
2221err_algs:
Mark A. Greerd20fb182012-12-21 10:04:09 -07002222 for (i = dd->pdata->algs_info_size - 1; i >= 0; i--)
2223 for (j = dd->pdata->algs_info[i].registered - 1; j >= 0; j--)
2224 crypto_unregister_ahash(
2225 &dd->pdata->algs_info[i].algs_list[j]);
Pali Rohár604c3102015-03-08 11:01:01 +01002226err_pm:
Mark A. Greerb359f032012-12-21 10:04:02 -07002227 pm_runtime_disable(dev);
Dan Carpenterd462e322016-05-18 13:39:05 +03002228 if (!dd->polling_mode)
Mark A. Greerf13ab862013-11-12 13:12:27 -07002229 dma_release_channel(dd->dma_lch);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08002230data_err:
2231 dev_err(dev, "initialization failed.\n");
2232
2233 return err;
2234}
2235
Greg Kroah-Hartman49cfe4d2012-12-21 13:14:09 -08002236static int omap_sham_remove(struct platform_device *pdev)
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08002237{
Gustavo A. R. Silva0588d852017-07-18 18:03:11 -05002238 struct omap_sham_dev *dd;
Mark A. Greerd20fb182012-12-21 10:04:09 -07002239 int i, j;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08002240
2241 dd = platform_get_drvdata(pdev);
2242 if (!dd)
2243 return -ENODEV;
2244 spin_lock(&sham.lock);
2245 list_del(&dd->list);
2246 spin_unlock(&sham.lock);
Mark A. Greerd20fb182012-12-21 10:04:09 -07002247 for (i = dd->pdata->algs_info_size - 1; i >= 0; i--)
2248 for (j = dd->pdata->algs_info[i].registered - 1; j >= 0; j--)
2249 crypto_unregister_ahash(
2250 &dd->pdata->algs_info[i].algs_list[j]);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08002251 tasklet_kill(&dd->done_task);
Mark A. Greerb359f032012-12-21 10:04:02 -07002252 pm_runtime_disable(&pdev->dev);
Mark A. Greerf13ab862013-11-12 13:12:27 -07002253
Peter Ujfalusidbe24622016-04-29 16:03:41 +03002254 if (!dd->polling_mode)
Mark A. Greerf13ab862013-11-12 13:12:27 -07002255 dma_release_channel(dd->dma_lch);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08002256
Tero Kristob82fc912019-11-05 16:00:51 +02002257 sysfs_remove_group(&dd->dev->kobj, &omap_sham_attr_group);
2258
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08002259 return 0;
2260}
2261
Mark A. Greer3b3f4402012-12-21 10:04:03 -07002262#ifdef CONFIG_PM_SLEEP
2263static int omap_sham_suspend(struct device *dev)
2264{
2265 pm_runtime_put_sync(dev);
2266 return 0;
2267}
2268
2269static int omap_sham_resume(struct device *dev)
2270{
Pali Rohár604c3102015-03-08 11:01:01 +01002271 int err = pm_runtime_get_sync(dev);
2272 if (err < 0) {
2273 dev_err(dev, "failed to get sync: %d\n", err);
2274 return err;
2275 }
Mark A. Greer3b3f4402012-12-21 10:04:03 -07002276 return 0;
2277}
2278#endif
2279
Jingoo Hanae12fe22014-02-27 20:33:32 +09002280static SIMPLE_DEV_PM_OPS(omap_sham_pm_ops, omap_sham_suspend, omap_sham_resume);
Mark A. Greer3b3f4402012-12-21 10:04:03 -07002281
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08002282static struct platform_driver omap_sham_driver = {
2283 .probe = omap_sham_probe,
2284 .remove = omap_sham_remove,
2285 .driver = {
2286 .name = "omap-sham",
Mark A. Greer3b3f4402012-12-21 10:04:03 -07002287 .pm = &omap_sham_pm_ops,
Mark A. Greer03feec92012-12-21 10:04:06 -07002288 .of_match_table = omap_sham_of_match,
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08002289 },
2290};
2291
Sachin Kamat02613702013-03-04 15:09:43 +05302292module_platform_driver(omap_sham_driver);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08002293
2294MODULE_DESCRIPTION("OMAP SHA1/MD5 hw acceleration support.");
2295MODULE_LICENSE("GPL v2");
2296MODULE_AUTHOR("Dmitry Kasatkin");
Joni Lapilainen718249d2013-10-26 23:00:41 +02002297MODULE_ALIAS("platform:omap-sham");