blob: 51b20abac46477db981027160398cc6948a23e13 [file] [log] [blame]
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001/*
2 * Cryptographic API.
3 *
4 * Support for OMAP SHA1/MD5 HW acceleration.
5 *
6 * Copyright (c) 2010 Nokia Corporation
7 * Author: Dmitry Kasatkin <dmitry.kasatkin@nokia.com>
Mark A. Greer0d373d62012-12-21 10:04:08 -07008 * Copyright (c) 2011 Texas Instruments Incorporated
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08009 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as published
12 * by the Free Software Foundation.
13 *
14 * Some ideas are from old omap-sha1-md5.c driver.
15 */
16
17#define pr_fmt(fmt) "%s: " fmt, __func__
18
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +080019#include <linux/err.h>
20#include <linux/device.h>
21#include <linux/module.h>
22#include <linux/init.h>
23#include <linux/errno.h>
24#include <linux/interrupt.h>
25#include <linux/kernel.h>
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +080026#include <linux/irq.h>
27#include <linux/io.h>
28#include <linux/platform_device.h>
29#include <linux/scatterlist.h>
30#include <linux/dma-mapping.h>
Mark A. Greerdfd061d2012-12-21 10:04:04 -070031#include <linux/dmaengine.h>
Mark A. Greerb359f032012-12-21 10:04:02 -070032#include <linux/pm_runtime.h>
Mark A. Greer03feec92012-12-21 10:04:06 -070033#include <linux/of.h>
34#include <linux/of_device.h>
35#include <linux/of_address.h>
36#include <linux/of_irq.h>
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +080037#include <linux/delay.h>
38#include <linux/crypto.h>
39#include <linux/cryptohash.h>
40#include <crypto/scatterwalk.h>
41#include <crypto/algapi.h>
42#include <crypto/sha.h>
43#include <crypto/hash.h>
Corentin LABBEebd401e2017-05-19 08:53:28 +020044#include <crypto/hmac.h>
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +080045#include <crypto/internal/hash.h>
46
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +080047#define MD5_DIGEST_SIZE 16
48
Mark A. Greer0d373d62012-12-21 10:04:08 -070049#define SHA_REG_IDIGEST(dd, x) ((dd)->pdata->idigest_ofs + ((x)*0x04))
50#define SHA_REG_DIN(dd, x) ((dd)->pdata->din_ofs + ((x) * 0x04))
51#define SHA_REG_DIGCNT(dd) ((dd)->pdata->digcnt_ofs)
52
Lokesh Vutlaeaef7e32013-07-26 12:29:14 +053053#define SHA_REG_ODIGEST(dd, x) ((dd)->pdata->odigest_ofs + (x * 0x04))
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +080054
55#define SHA_REG_CTRL 0x18
56#define SHA_REG_CTRL_LENGTH (0xFFFFFFFF << 5)
57#define SHA_REG_CTRL_CLOSE_HASH (1 << 4)
58#define SHA_REG_CTRL_ALGO_CONST (1 << 3)
59#define SHA_REG_CTRL_ALGO (1 << 2)
60#define SHA_REG_CTRL_INPUT_READY (1 << 1)
61#define SHA_REG_CTRL_OUTPUT_READY (1 << 0)
62
Mark A. Greer0d373d62012-12-21 10:04:08 -070063#define SHA_REG_REV(dd) ((dd)->pdata->rev_ofs)
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +080064
Mark A. Greer0d373d62012-12-21 10:04:08 -070065#define SHA_REG_MASK(dd) ((dd)->pdata->mask_ofs)
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +080066#define SHA_REG_MASK_DMA_EN (1 << 3)
67#define SHA_REG_MASK_IT_EN (1 << 2)
68#define SHA_REG_MASK_SOFTRESET (1 << 1)
69#define SHA_REG_AUTOIDLE (1 << 0)
70
Mark A. Greer0d373d62012-12-21 10:04:08 -070071#define SHA_REG_SYSSTATUS(dd) ((dd)->pdata->sysstatus_ofs)
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +080072#define SHA_REG_SYSSTATUS_RESETDONE (1 << 0)
73
Lokesh Vutlaeaef7e32013-07-26 12:29:14 +053074#define SHA_REG_MODE(dd) ((dd)->pdata->mode_ofs)
Mark A. Greer0d373d62012-12-21 10:04:08 -070075#define SHA_REG_MODE_HMAC_OUTER_HASH (1 << 7)
76#define SHA_REG_MODE_HMAC_KEY_PROC (1 << 5)
77#define SHA_REG_MODE_CLOSE_HASH (1 << 4)
78#define SHA_REG_MODE_ALGO_CONSTANT (1 << 3)
Mark A. Greer0d373d62012-12-21 10:04:08 -070079
Lokesh Vutlaeaef7e32013-07-26 12:29:14 +053080#define SHA_REG_MODE_ALGO_MASK (7 << 0)
81#define SHA_REG_MODE_ALGO_MD5_128 (0 << 1)
82#define SHA_REG_MODE_ALGO_SHA1_160 (1 << 1)
83#define SHA_REG_MODE_ALGO_SHA2_224 (2 << 1)
84#define SHA_REG_MODE_ALGO_SHA2_256 (3 << 1)
85#define SHA_REG_MODE_ALGO_SHA2_384 (1 << 0)
86#define SHA_REG_MODE_ALGO_SHA2_512 (3 << 0)
87
88#define SHA_REG_LENGTH(dd) ((dd)->pdata->length_ofs)
Mark A. Greer0d373d62012-12-21 10:04:08 -070089
90#define SHA_REG_IRQSTATUS 0x118
91#define SHA_REG_IRQSTATUS_CTX_RDY (1 << 3)
92#define SHA_REG_IRQSTATUS_PARTHASH_RDY (1 << 2)
93#define SHA_REG_IRQSTATUS_INPUT_RDY (1 << 1)
94#define SHA_REG_IRQSTATUS_OUTPUT_RDY (1 << 0)
95
96#define SHA_REG_IRQENA 0x11C
97#define SHA_REG_IRQENA_CTX_RDY (1 << 3)
98#define SHA_REG_IRQENA_PARTHASH_RDY (1 << 2)
99#define SHA_REG_IRQENA_INPUT_RDY (1 << 1)
100#define SHA_REG_IRQENA_OUTPUT_RDY (1 << 0)
101
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800102#define DEFAULT_TIMEOUT_INTERVAL HZ
103
Tero Kristoe93f7672016-06-22 16:23:34 +0300104#define DEFAULT_AUTOSUSPEND_DELAY 1000
105
Dmitry Kasatkinea1fd222011-06-02 21:10:05 +0300106/* mostly device flags */
107#define FLAGS_BUSY 0
108#define FLAGS_FINAL 1
109#define FLAGS_DMA_ACTIVE 2
110#define FLAGS_OUTPUT_READY 3
111#define FLAGS_INIT 4
112#define FLAGS_CPU 5
Dmitry Kasatkin6c63db82011-06-02 21:10:10 +0300113#define FLAGS_DMA_READY 6
Mark A. Greer0d373d62012-12-21 10:04:08 -0700114#define FLAGS_AUTO_XOR 7
115#define FLAGS_BE32_SHA1 8
Tero Kristof19de1b2016-09-19 18:22:15 +0300116#define FLAGS_SGS_COPIED 9
117#define FLAGS_SGS_ALLOCED 10
Dmitry Kasatkinea1fd222011-06-02 21:10:05 +0300118/* context flags */
119#define FLAGS_FINUP 16
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800120
Mark A. Greer0d373d62012-12-21 10:04:08 -0700121#define FLAGS_MODE_SHIFT 18
Lokesh Vutlaeaef7e32013-07-26 12:29:14 +0530122#define FLAGS_MODE_MASK (SHA_REG_MODE_ALGO_MASK << FLAGS_MODE_SHIFT)
123#define FLAGS_MODE_MD5 (SHA_REG_MODE_ALGO_MD5_128 << FLAGS_MODE_SHIFT)
124#define FLAGS_MODE_SHA1 (SHA_REG_MODE_ALGO_SHA1_160 << FLAGS_MODE_SHIFT)
125#define FLAGS_MODE_SHA224 (SHA_REG_MODE_ALGO_SHA2_224 << FLAGS_MODE_SHIFT)
126#define FLAGS_MODE_SHA256 (SHA_REG_MODE_ALGO_SHA2_256 << FLAGS_MODE_SHIFT)
127#define FLAGS_MODE_SHA384 (SHA_REG_MODE_ALGO_SHA2_384 << FLAGS_MODE_SHIFT)
128#define FLAGS_MODE_SHA512 (SHA_REG_MODE_ALGO_SHA2_512 << FLAGS_MODE_SHIFT)
129
130#define FLAGS_HMAC 21
131#define FLAGS_ERROR 22
Mark A. Greer0d373d62012-12-21 10:04:08 -0700132
133#define OP_UPDATE 1
134#define OP_FINAL 2
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800135
Dmitry Kasatkin798eed5d2010-11-19 16:04:26 +0200136#define OMAP_ALIGN_MASK (sizeof(u32)-1)
137#define OMAP_ALIGNED __attribute__((aligned(sizeof(u32))))
138
Tero Kristo182e2832016-09-19 18:22:19 +0300139#define BUFLEN SHA512_BLOCK_SIZE
Tero Kristo2c5bd1e2016-09-19 18:22:16 +0300140#define OMAP_SHA_DMA_THRESHOLD 256
Dmitry Kasatkin798eed5d2010-11-19 16:04:26 +0200141
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800142struct omap_sham_dev;
143
144struct omap_sham_reqctx {
145 struct omap_sham_dev *dd;
146 unsigned long flags;
147 unsigned long op;
148
Lokesh Vutlaeaef7e32013-07-26 12:29:14 +0530149 u8 digest[SHA512_DIGEST_SIZE] OMAP_ALIGNED;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800150 size_t digcnt;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800151 size_t bufcnt;
152 size_t buflen;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800153
154 /* walk state */
155 struct scatterlist *sg;
Tero Kristof19de1b2016-09-19 18:22:15 +0300156 struct scatterlist sgl[2];
Tero Kristo8043bb12016-09-19 18:22:17 +0300157 int offset; /* offset in current sg */
Tero Kristof19de1b2016-09-19 18:22:15 +0300158 int sg_len;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800159 unsigned int total; /* total request */
Dmitry Kasatkin798eed5d2010-11-19 16:04:26 +0200160
161 u8 buffer[0] OMAP_ALIGNED;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800162};
163
164struct omap_sham_hmac_ctx {
165 struct crypto_shash *shash;
Lokesh Vutlaeaef7e32013-07-26 12:29:14 +0530166 u8 ipad[SHA512_BLOCK_SIZE] OMAP_ALIGNED;
167 u8 opad[SHA512_BLOCK_SIZE] OMAP_ALIGNED;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800168};
169
170struct omap_sham_ctx {
171 struct omap_sham_dev *dd;
172
173 unsigned long flags;
174
175 /* fallback stuff */
176 struct crypto_shash *fallback;
177
178 struct omap_sham_hmac_ctx base[0];
179};
180
Tero Kristo65e7a542016-06-22 16:23:35 +0300181#define OMAP_SHAM_QUEUE_LENGTH 10
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800182
Mark A. Greerd20fb182012-12-21 10:04:09 -0700183struct omap_sham_algs_info {
184 struct ahash_alg *algs_list;
185 unsigned int size;
186 unsigned int registered;
187};
188
Mark A. Greer0d373d62012-12-21 10:04:08 -0700189struct omap_sham_pdata {
Mark A. Greerd20fb182012-12-21 10:04:09 -0700190 struct omap_sham_algs_info *algs_info;
191 unsigned int algs_info_size;
Mark A. Greer0d373d62012-12-21 10:04:08 -0700192 unsigned long flags;
193 int digest_size;
194
195 void (*copy_hash)(struct ahash_request *req, int out);
196 void (*write_ctrl)(struct omap_sham_dev *dd, size_t length,
197 int final, int dma);
198 void (*trigger)(struct omap_sham_dev *dd, size_t length);
199 int (*poll_irq)(struct omap_sham_dev *dd);
200 irqreturn_t (*intr_hdlr)(int irq, void *dev_id);
201
202 u32 odigest_ofs;
203 u32 idigest_ofs;
204 u32 din_ofs;
205 u32 digcnt_ofs;
206 u32 rev_ofs;
207 u32 mask_ofs;
208 u32 sysstatus_ofs;
Lokesh Vutlaeaef7e32013-07-26 12:29:14 +0530209 u32 mode_ofs;
210 u32 length_ofs;
Mark A. Greer0d373d62012-12-21 10:04:08 -0700211
212 u32 major_mask;
213 u32 major_shift;
214 u32 minor_mask;
215 u32 minor_shift;
216};
217
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800218struct omap_sham_dev {
219 struct list_head list;
220 unsigned long phys_base;
221 struct device *dev;
222 void __iomem *io_base;
223 int irq;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800224 spinlock_t lock;
Dmitry Kasatkin3e133c82010-11-19 16:04:24 +0200225 int err;
Mark A. Greerdfd061d2012-12-21 10:04:04 -0700226 struct dma_chan *dma_lch;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800227 struct tasklet_struct done_task;
Lokesh Vutlab8411cc2013-08-20 20:32:34 +0530228 u8 polling_mode;
Tero Kristoc28e8f22017-05-24 10:35:34 +0300229 u8 xmit_buf[BUFLEN] OMAP_ALIGNED;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800230
231 unsigned long flags;
Tero Kristoc9af5992018-02-27 15:30:36 +0200232 int fallback_sz;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800233 struct crypto_queue queue;
234 struct ahash_request *req;
Mark A. Greer0d373d62012-12-21 10:04:08 -0700235
236 const struct omap_sham_pdata *pdata;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800237};
238
239struct omap_sham_drv {
240 struct list_head dev_list;
241 spinlock_t lock;
242 unsigned long flags;
243};
244
245static struct omap_sham_drv sham = {
246 .dev_list = LIST_HEAD_INIT(sham.dev_list),
247 .lock = __SPIN_LOCK_UNLOCKED(sham.lock),
248};
249
250static inline u32 omap_sham_read(struct omap_sham_dev *dd, u32 offset)
251{
252 return __raw_readl(dd->io_base + offset);
253}
254
255static inline void omap_sham_write(struct omap_sham_dev *dd,
256 u32 offset, u32 value)
257{
258 __raw_writel(value, dd->io_base + offset);
259}
260
261static inline void omap_sham_write_mask(struct omap_sham_dev *dd, u32 address,
262 u32 value, u32 mask)
263{
264 u32 val;
265
266 val = omap_sham_read(dd, address);
267 val &= ~mask;
268 val |= value;
269 omap_sham_write(dd, address, val);
270}
271
272static inline int omap_sham_wait(struct omap_sham_dev *dd, u32 offset, u32 bit)
273{
274 unsigned long timeout = jiffies + DEFAULT_TIMEOUT_INTERVAL;
275
276 while (!(omap_sham_read(dd, offset) & bit)) {
277 if (time_is_before_jiffies(timeout))
278 return -ETIMEDOUT;
279 }
280
281 return 0;
282}
283
Mark A. Greer0d373d62012-12-21 10:04:08 -0700284static void omap_sham_copy_hash_omap2(struct ahash_request *req, int out)
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800285{
286 struct omap_sham_reqctx *ctx = ahash_request_ctx(req);
Mark A. Greer0d373d62012-12-21 10:04:08 -0700287 struct omap_sham_dev *dd = ctx->dd;
Dmitry Kasatkin0c3cf4c2010-11-19 16:04:22 +0200288 u32 *hash = (u32 *)ctx->digest;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800289 int i;
290
Mark A. Greer0d373d62012-12-21 10:04:08 -0700291 for (i = 0; i < dd->pdata->digest_size / sizeof(u32); i++) {
Dmitry Kasatkin3c8d7582010-11-19 16:04:27 +0200292 if (out)
Mark A. Greer0d373d62012-12-21 10:04:08 -0700293 hash[i] = omap_sham_read(dd, SHA_REG_IDIGEST(dd, i));
Dmitry Kasatkin3c8d7582010-11-19 16:04:27 +0200294 else
Mark A. Greer0d373d62012-12-21 10:04:08 -0700295 omap_sham_write(dd, SHA_REG_IDIGEST(dd, i), hash[i]);
Dmitry Kasatkin3c8d7582010-11-19 16:04:27 +0200296 }
297}
298
Mark A. Greer0d373d62012-12-21 10:04:08 -0700299static void omap_sham_copy_hash_omap4(struct ahash_request *req, int out)
300{
301 struct omap_sham_reqctx *ctx = ahash_request_ctx(req);
302 struct omap_sham_dev *dd = ctx->dd;
303 int i;
304
305 if (ctx->flags & BIT(FLAGS_HMAC)) {
306 struct crypto_ahash *tfm = crypto_ahash_reqtfm(dd->req);
307 struct omap_sham_ctx *tctx = crypto_ahash_ctx(tfm);
308 struct omap_sham_hmac_ctx *bctx = tctx->base;
309 u32 *opad = (u32 *)bctx->opad;
310
311 for (i = 0; i < dd->pdata->digest_size / sizeof(u32); i++) {
312 if (out)
313 opad[i] = omap_sham_read(dd,
Lokesh Vutlaeaef7e32013-07-26 12:29:14 +0530314 SHA_REG_ODIGEST(dd, i));
Mark A. Greer0d373d62012-12-21 10:04:08 -0700315 else
Lokesh Vutlaeaef7e32013-07-26 12:29:14 +0530316 omap_sham_write(dd, SHA_REG_ODIGEST(dd, i),
Mark A. Greer0d373d62012-12-21 10:04:08 -0700317 opad[i]);
318 }
319 }
320
321 omap_sham_copy_hash_omap2(req, out);
322}
323
Dmitry Kasatkin3c8d7582010-11-19 16:04:27 +0200324static void omap_sham_copy_ready_hash(struct ahash_request *req)
325{
326 struct omap_sham_reqctx *ctx = ahash_request_ctx(req);
327 u32 *in = (u32 *)ctx->digest;
328 u32 *hash = (u32 *)req->result;
Mark A. Greer0d373d62012-12-21 10:04:08 -0700329 int i, d, big_endian = 0;
Dmitry Kasatkin3c8d7582010-11-19 16:04:27 +0200330
331 if (!hash)
332 return;
333
Mark A. Greer0d373d62012-12-21 10:04:08 -0700334 switch (ctx->flags & FLAGS_MODE_MASK) {
335 case FLAGS_MODE_MD5:
336 d = MD5_DIGEST_SIZE / sizeof(u32);
337 break;
338 case FLAGS_MODE_SHA1:
339 /* OMAP2 SHA1 is big endian */
340 if (test_bit(FLAGS_BE32_SHA1, &ctx->dd->flags))
341 big_endian = 1;
342 d = SHA1_DIGEST_SIZE / sizeof(u32);
343 break;
Mark A. Greerd20fb182012-12-21 10:04:09 -0700344 case FLAGS_MODE_SHA224:
345 d = SHA224_DIGEST_SIZE / sizeof(u32);
346 break;
347 case FLAGS_MODE_SHA256:
348 d = SHA256_DIGEST_SIZE / sizeof(u32);
349 break;
Lokesh Vutlaeaef7e32013-07-26 12:29:14 +0530350 case FLAGS_MODE_SHA384:
351 d = SHA384_DIGEST_SIZE / sizeof(u32);
352 break;
353 case FLAGS_MODE_SHA512:
354 d = SHA512_DIGEST_SIZE / sizeof(u32);
355 break;
Mark A. Greer0d373d62012-12-21 10:04:08 -0700356 default:
357 d = 0;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800358 }
Mark A. Greer0d373d62012-12-21 10:04:08 -0700359
360 if (big_endian)
361 for (i = 0; i < d; i++)
362 hash[i] = be32_to_cpu(in[i]);
363 else
364 for (i = 0; i < d; i++)
365 hash[i] = le32_to_cpu(in[i]);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800366}
367
Dmitry Kasatkin798eed5d2010-11-19 16:04:26 +0200368static int omap_sham_hw_init(struct omap_sham_dev *dd)
369{
Pali Rohár604c3102015-03-08 11:01:01 +0100370 int err;
371
372 err = pm_runtime_get_sync(dd->dev);
373 if (err < 0) {
374 dev_err(dd->dev, "failed to get sync: %d\n", err);
375 return err;
376 }
Dmitry Kasatkin798eed5d2010-11-19 16:04:26 +0200377
Dmitry Kasatkina929cbe2011-06-02 21:10:06 +0300378 if (!test_bit(FLAGS_INIT, &dd->flags)) {
Dmitry Kasatkina929cbe2011-06-02 21:10:06 +0300379 set_bit(FLAGS_INIT, &dd->flags);
Dmitry Kasatkin798eed5d2010-11-19 16:04:26 +0200380 dd->err = 0;
381 }
382
383 return 0;
384}
385
Mark A. Greer0d373d62012-12-21 10:04:08 -0700386static void omap_sham_write_ctrl_omap2(struct omap_sham_dev *dd, size_t length,
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800387 int final, int dma)
388{
389 struct omap_sham_reqctx *ctx = ahash_request_ctx(dd->req);
390 u32 val = length << 5, mask;
391
Dmitry Kasatkin798eed5d2010-11-19 16:04:26 +0200392 if (likely(ctx->digcnt))
Mark A. Greer0d373d62012-12-21 10:04:08 -0700393 omap_sham_write(dd, SHA_REG_DIGCNT(dd), ctx->digcnt);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800394
Mark A. Greer0d373d62012-12-21 10:04:08 -0700395 omap_sham_write_mask(dd, SHA_REG_MASK(dd),
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800396 SHA_REG_MASK_IT_EN | (dma ? SHA_REG_MASK_DMA_EN : 0),
397 SHA_REG_MASK_IT_EN | SHA_REG_MASK_DMA_EN);
398 /*
399 * Setting ALGO_CONST only for the first iteration
400 * and CLOSE_HASH only for the last one.
401 */
Mark A. Greer0d373d62012-12-21 10:04:08 -0700402 if ((ctx->flags & FLAGS_MODE_MASK) == FLAGS_MODE_SHA1)
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800403 val |= SHA_REG_CTRL_ALGO;
404 if (!ctx->digcnt)
405 val |= SHA_REG_CTRL_ALGO_CONST;
406 if (final)
407 val |= SHA_REG_CTRL_CLOSE_HASH;
408
409 mask = SHA_REG_CTRL_ALGO_CONST | SHA_REG_CTRL_CLOSE_HASH |
410 SHA_REG_CTRL_ALGO | SHA_REG_CTRL_LENGTH;
411
412 omap_sham_write_mask(dd, SHA_REG_CTRL, val, mask);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800413}
414
Mark A. Greer0d373d62012-12-21 10:04:08 -0700415static void omap_sham_trigger_omap2(struct omap_sham_dev *dd, size_t length)
416{
417}
418
419static int omap_sham_poll_irq_omap2(struct omap_sham_dev *dd)
420{
421 return omap_sham_wait(dd, SHA_REG_CTRL, SHA_REG_CTRL_INPUT_READY);
422}
423
Lokesh Vutlaeaef7e32013-07-26 12:29:14 +0530424static int get_block_size(struct omap_sham_reqctx *ctx)
425{
426 int d;
427
428 switch (ctx->flags & FLAGS_MODE_MASK) {
429 case FLAGS_MODE_MD5:
430 case FLAGS_MODE_SHA1:
431 d = SHA1_BLOCK_SIZE;
432 break;
433 case FLAGS_MODE_SHA224:
434 case FLAGS_MODE_SHA256:
435 d = SHA256_BLOCK_SIZE;
436 break;
437 case FLAGS_MODE_SHA384:
438 case FLAGS_MODE_SHA512:
439 d = SHA512_BLOCK_SIZE;
440 break;
441 default:
442 d = 0;
443 }
444
445 return d;
446}
447
Mark A. Greer0d373d62012-12-21 10:04:08 -0700448static void omap_sham_write_n(struct omap_sham_dev *dd, u32 offset,
449 u32 *value, int count)
450{
451 for (; count--; value++, offset += 4)
452 omap_sham_write(dd, offset, *value);
453}
454
455static void omap_sham_write_ctrl_omap4(struct omap_sham_dev *dd, size_t length,
456 int final, int dma)
457{
458 struct omap_sham_reqctx *ctx = ahash_request_ctx(dd->req);
459 u32 val, mask;
460
461 /*
462 * Setting ALGO_CONST only for the first iteration and
463 * CLOSE_HASH only for the last one. Note that flags mode bits
464 * correspond to algorithm encoding in mode register.
465 */
Lokesh Vutlaeaef7e32013-07-26 12:29:14 +0530466 val = (ctx->flags & FLAGS_MODE_MASK) >> (FLAGS_MODE_SHIFT);
Mark A. Greer0d373d62012-12-21 10:04:08 -0700467 if (!ctx->digcnt) {
468 struct crypto_ahash *tfm = crypto_ahash_reqtfm(dd->req);
469 struct omap_sham_ctx *tctx = crypto_ahash_ctx(tfm);
470 struct omap_sham_hmac_ctx *bctx = tctx->base;
Lokesh Vutlaeaef7e32013-07-26 12:29:14 +0530471 int bs, nr_dr;
Mark A. Greer0d373d62012-12-21 10:04:08 -0700472
473 val |= SHA_REG_MODE_ALGO_CONSTANT;
474
475 if (ctx->flags & BIT(FLAGS_HMAC)) {
Lokesh Vutlaeaef7e32013-07-26 12:29:14 +0530476 bs = get_block_size(ctx);
477 nr_dr = bs / (2 * sizeof(u32));
Mark A. Greer0d373d62012-12-21 10:04:08 -0700478 val |= SHA_REG_MODE_HMAC_KEY_PROC;
Lokesh Vutlaeaef7e32013-07-26 12:29:14 +0530479 omap_sham_write_n(dd, SHA_REG_ODIGEST(dd, 0),
480 (u32 *)bctx->ipad, nr_dr);
481 omap_sham_write_n(dd, SHA_REG_IDIGEST(dd, 0),
482 (u32 *)bctx->ipad + nr_dr, nr_dr);
483 ctx->digcnt += bs;
Mark A. Greer0d373d62012-12-21 10:04:08 -0700484 }
485 }
486
487 if (final) {
488 val |= SHA_REG_MODE_CLOSE_HASH;
489
490 if (ctx->flags & BIT(FLAGS_HMAC))
491 val |= SHA_REG_MODE_HMAC_OUTER_HASH;
492 }
493
494 mask = SHA_REG_MODE_ALGO_CONSTANT | SHA_REG_MODE_CLOSE_HASH |
495 SHA_REG_MODE_ALGO_MASK | SHA_REG_MODE_HMAC_OUTER_HASH |
496 SHA_REG_MODE_HMAC_KEY_PROC;
497
498 dev_dbg(dd->dev, "ctrl: %08x, flags: %08lx\n", val, ctx->flags);
Lokesh Vutlaeaef7e32013-07-26 12:29:14 +0530499 omap_sham_write_mask(dd, SHA_REG_MODE(dd), val, mask);
Mark A. Greer0d373d62012-12-21 10:04:08 -0700500 omap_sham_write(dd, SHA_REG_IRQENA, SHA_REG_IRQENA_OUTPUT_RDY);
501 omap_sham_write_mask(dd, SHA_REG_MASK(dd),
502 SHA_REG_MASK_IT_EN |
503 (dma ? SHA_REG_MASK_DMA_EN : 0),
504 SHA_REG_MASK_IT_EN | SHA_REG_MASK_DMA_EN);
505}
506
507static void omap_sham_trigger_omap4(struct omap_sham_dev *dd, size_t length)
508{
Lokesh Vutlaeaef7e32013-07-26 12:29:14 +0530509 omap_sham_write(dd, SHA_REG_LENGTH(dd), length);
Mark A. Greer0d373d62012-12-21 10:04:08 -0700510}
511
512static int omap_sham_poll_irq_omap4(struct omap_sham_dev *dd)
513{
514 return omap_sham_wait(dd, SHA_REG_IRQSTATUS,
515 SHA_REG_IRQSTATUS_INPUT_RDY);
516}
517
Tero Kristo8043bb12016-09-19 18:22:17 +0300518static int omap_sham_xmit_cpu(struct omap_sham_dev *dd, size_t length,
519 int final)
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800520{
521 struct omap_sham_reqctx *ctx = ahash_request_ctx(dd->req);
Lokesh Vutlab8411cc2013-08-20 20:32:34 +0530522 int count, len32, bs32, offset = 0;
Tero Kristo8043bb12016-09-19 18:22:17 +0300523 const u32 *buffer;
524 int mlen;
525 struct sg_mapping_iter mi;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800526
527 dev_dbg(dd->dev, "xmit_cpu: digcnt: %d, length: %d, final: %d\n",
528 ctx->digcnt, length, final);
529
Mark A. Greer0d373d62012-12-21 10:04:08 -0700530 dd->pdata->write_ctrl(dd, length, final, 0);
531 dd->pdata->trigger(dd, length);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800532
Dmitry Kasatkin3e133c82010-11-19 16:04:24 +0200533 /* should be non-zero before next lines to disable clocks later */
534 ctx->digcnt += length;
Tero Kristo8043bb12016-09-19 18:22:17 +0300535 ctx->total -= length;
Dmitry Kasatkin3e133c82010-11-19 16:04:24 +0200536
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800537 if (final)
Dmitry Kasatkined3ea9a82011-06-02 21:10:07 +0300538 set_bit(FLAGS_FINAL, &dd->flags); /* catch last interrupt */
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800539
Dmitry Kasatkin6c63db82011-06-02 21:10:10 +0300540 set_bit(FLAGS_CPU, &dd->flags);
541
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800542 len32 = DIV_ROUND_UP(length, sizeof(u32));
Lokesh Vutlab8411cc2013-08-20 20:32:34 +0530543 bs32 = get_block_size(ctx) / sizeof(u32);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800544
Tero Kristo8043bb12016-09-19 18:22:17 +0300545 sg_miter_start(&mi, ctx->sg, ctx->sg_len,
546 SG_MITER_FROM_SG | SG_MITER_ATOMIC);
547
548 mlen = 0;
549
Lokesh Vutlab8411cc2013-08-20 20:32:34 +0530550 while (len32) {
551 if (dd->pdata->poll_irq(dd))
552 return -ETIMEDOUT;
553
Tero Kristo8043bb12016-09-19 18:22:17 +0300554 for (count = 0; count < min(len32, bs32); count++, offset++) {
555 if (!mlen) {
556 sg_miter_next(&mi);
557 mlen = mi.length;
558 if (!mlen) {
559 pr_err("sg miter failure.\n");
560 return -EINVAL;
561 }
562 offset = 0;
563 buffer = mi.addr;
564 }
Lokesh Vutlab8411cc2013-08-20 20:32:34 +0530565 omap_sham_write(dd, SHA_REG_DIN(dd, count),
566 buffer[offset]);
Tero Kristo8043bb12016-09-19 18:22:17 +0300567 mlen -= 4;
568 }
Lokesh Vutlab8411cc2013-08-20 20:32:34 +0530569 len32 -= min(len32, bs32);
570 }
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800571
Tero Kristo8043bb12016-09-19 18:22:17 +0300572 sg_miter_stop(&mi);
573
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800574 return -EINPROGRESS;
575}
576
Mark A. Greerdfd061d2012-12-21 10:04:04 -0700577static void omap_sham_dma_callback(void *param)
578{
579 struct omap_sham_dev *dd = param;
580
581 set_bit(FLAGS_DMA_READY, &dd->flags);
582 tasklet_schedule(&dd->done_task);
583}
Mark A. Greerdfd061d2012-12-21 10:04:04 -0700584
Tero Kristo8043bb12016-09-19 18:22:17 +0300585static int omap_sham_xmit_dma(struct omap_sham_dev *dd, size_t length,
586 int final)
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800587{
588 struct omap_sham_reqctx *ctx = ahash_request_ctx(dd->req);
Mark A. Greerdfd061d2012-12-21 10:04:04 -0700589 struct dma_async_tx_descriptor *tx;
590 struct dma_slave_config cfg;
Tero Kristo8043bb12016-09-19 18:22:17 +0300591 int ret;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800592
593 dev_dbg(dd->dev, "xmit_dma: digcnt: %d, length: %d, final: %d\n",
594 ctx->digcnt, length, final);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800595
Tero Kristo8043bb12016-09-19 18:22:17 +0300596 if (!dma_map_sg(dd->dev, ctx->sg, ctx->sg_len, DMA_TO_DEVICE)) {
597 dev_err(dd->dev, "dma_map_sg error\n");
598 return -EINVAL;
599 }
600
Mark A. Greerdfd061d2012-12-21 10:04:04 -0700601 memset(&cfg, 0, sizeof(cfg));
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800602
Mark A. Greer0d373d62012-12-21 10:04:08 -0700603 cfg.dst_addr = dd->phys_base + SHA_REG_DIN(dd, 0);
Mark A. Greerdfd061d2012-12-21 10:04:04 -0700604 cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
Tero Kristo8043bb12016-09-19 18:22:17 +0300605 cfg.dst_maxburst = get_block_size(ctx) / DMA_SLAVE_BUSWIDTH_4_BYTES;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800606
Mark A. Greerdfd061d2012-12-21 10:04:04 -0700607 ret = dmaengine_slave_config(dd->dma_lch, &cfg);
608 if (ret) {
609 pr_err("omap-sham: can't configure dmaengine slave: %d\n", ret);
610 return ret;
611 }
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800612
Tero Kristo8043bb12016-09-19 18:22:17 +0300613 tx = dmaengine_prep_slave_sg(dd->dma_lch, ctx->sg, ctx->sg_len,
614 DMA_MEM_TO_DEV,
615 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
Mark A. Greerdfd061d2012-12-21 10:04:04 -0700616
617 if (!tx) {
Tero Kristo8043bb12016-09-19 18:22:17 +0300618 dev_err(dd->dev, "prep_slave_sg failed\n");
Mark A. Greerdfd061d2012-12-21 10:04:04 -0700619 return -EINVAL;
620 }
621
622 tx->callback = omap_sham_dma_callback;
623 tx->callback_param = dd;
Mark A. Greerdfd061d2012-12-21 10:04:04 -0700624
Mark A. Greer0d373d62012-12-21 10:04:08 -0700625 dd->pdata->write_ctrl(dd, length, final, 1);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800626
627 ctx->digcnt += length;
Tero Kristo8043bb12016-09-19 18:22:17 +0300628 ctx->total -= length;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800629
630 if (final)
Dmitry Kasatkined3ea9a82011-06-02 21:10:07 +0300631 set_bit(FLAGS_FINAL, &dd->flags); /* catch last interrupt */
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800632
Dmitry Kasatkina929cbe2011-06-02 21:10:06 +0300633 set_bit(FLAGS_DMA_ACTIVE, &dd->flags);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800634
Mark A. Greerdfd061d2012-12-21 10:04:04 -0700635 dmaengine_submit(tx);
636 dma_async_issue_pending(dd->dma_lch);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800637
Mark A. Greer0d373d62012-12-21 10:04:08 -0700638 dd->pdata->trigger(dd, length);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800639
640 return -EINPROGRESS;
641}
642
Tero Kristof19de1b2016-09-19 18:22:15 +0300643static int omap_sham_copy_sg_lists(struct omap_sham_reqctx *ctx,
644 struct scatterlist *sg, int bs, int new_len)
645{
646 int n = sg_nents(sg);
647 struct scatterlist *tmp;
648 int offset = ctx->offset;
649
650 if (ctx->bufcnt)
651 n++;
652
653 ctx->sg = kmalloc_array(n, sizeof(*sg), GFP_KERNEL);
654 if (!ctx->sg)
655 return -ENOMEM;
656
657 sg_init_table(ctx->sg, n);
658
659 tmp = ctx->sg;
660
661 ctx->sg_len = 0;
662
663 if (ctx->bufcnt) {
664 sg_set_buf(tmp, ctx->dd->xmit_buf, ctx->bufcnt);
665 tmp = sg_next(tmp);
666 ctx->sg_len++;
667 }
668
669 while (sg && new_len) {
670 int len = sg->length - offset;
671
672 if (offset) {
673 offset -= sg->length;
674 if (offset < 0)
675 offset = 0;
676 }
677
678 if (new_len < len)
679 len = new_len;
680
681 if (len > 0) {
682 new_len -= len;
683 sg_set_page(tmp, sg_page(sg), len, sg->offset);
684 if (new_len <= 0)
685 sg_mark_end(tmp);
686 tmp = sg_next(tmp);
687 ctx->sg_len++;
688 }
689
690 sg = sg_next(sg);
691 }
692
693 set_bit(FLAGS_SGS_ALLOCED, &ctx->dd->flags);
694
695 ctx->bufcnt = 0;
696
697 return 0;
698}
699
700static int omap_sham_copy_sgs(struct omap_sham_reqctx *ctx,
701 struct scatterlist *sg, int bs, int new_len)
702{
703 int pages;
704 void *buf;
705 int len;
706
707 len = new_len + ctx->bufcnt;
708
709 pages = get_order(ctx->total);
710
711 buf = (void *)__get_free_pages(GFP_ATOMIC, pages);
712 if (!buf) {
713 pr_err("Couldn't allocate pages for unaligned cases.\n");
714 return -ENOMEM;
715 }
716
717 if (ctx->bufcnt)
718 memcpy(buf, ctx->dd->xmit_buf, ctx->bufcnt);
719
720 scatterwalk_map_and_copy(buf + ctx->bufcnt, sg, ctx->offset,
721 ctx->total - ctx->bufcnt, 0);
722 sg_init_table(ctx->sgl, 1);
723 sg_set_buf(ctx->sgl, buf, len);
724 ctx->sg = ctx->sgl;
725 set_bit(FLAGS_SGS_COPIED, &ctx->dd->flags);
726 ctx->sg_len = 1;
727 ctx->bufcnt = 0;
728 ctx->offset = 0;
729
730 return 0;
731}
732
733static int omap_sham_align_sgs(struct scatterlist *sg,
734 int nbytes, int bs, bool final,
735 struct omap_sham_reqctx *rctx)
736{
737 int n = 0;
738 bool aligned = true;
739 bool list_ok = true;
740 struct scatterlist *sg_tmp = sg;
741 int new_len;
742 int offset = rctx->offset;
743
744 if (!sg || !sg->length || !nbytes)
745 return 0;
746
747 new_len = nbytes;
748
749 if (offset)
750 list_ok = false;
751
752 if (final)
753 new_len = DIV_ROUND_UP(new_len, bs) * bs;
754 else
Tero Kristo898d86a2017-05-24 10:35:33 +0300755 new_len = (new_len - 1) / bs * bs;
756
757 if (nbytes != new_len)
758 list_ok = false;
Tero Kristof19de1b2016-09-19 18:22:15 +0300759
760 while (nbytes > 0 && sg_tmp) {
761 n++;
762
Tero Kristo4c219852018-02-27 15:30:34 +0200763#ifdef CONFIG_ZONE_DMA
764 if (page_zonenum(sg_page(sg_tmp)) != ZONE_DMA) {
765 aligned = false;
766 break;
767 }
768#endif
769
Tero Kristof19de1b2016-09-19 18:22:15 +0300770 if (offset < sg_tmp->length) {
771 if (!IS_ALIGNED(offset + sg_tmp->offset, 4)) {
772 aligned = false;
773 break;
774 }
775
776 if (!IS_ALIGNED(sg_tmp->length - offset, bs)) {
777 aligned = false;
778 break;
779 }
780 }
781
782 if (offset) {
783 offset -= sg_tmp->length;
784 if (offset < 0) {
785 nbytes += offset;
786 offset = 0;
787 }
788 } else {
789 nbytes -= sg_tmp->length;
790 }
791
792 sg_tmp = sg_next(sg_tmp);
793
794 if (nbytes < 0) {
795 list_ok = false;
796 break;
797 }
798 }
799
800 if (!aligned)
801 return omap_sham_copy_sgs(rctx, sg, bs, new_len);
802 else if (!list_ok)
803 return omap_sham_copy_sg_lists(rctx, sg, bs, new_len);
804
805 rctx->sg_len = n;
806 rctx->sg = sg;
807
808 return 0;
809}
810
811static int omap_sham_prepare_request(struct ahash_request *req, bool update)
812{
813 struct omap_sham_reqctx *rctx = ahash_request_ctx(req);
814 int bs;
815 int ret;
816 int nbytes;
817 bool final = rctx->flags & BIT(FLAGS_FINUP);
818 int xmit_len, hash_later;
819
Tero Kristof19de1b2016-09-19 18:22:15 +0300820 bs = get_block_size(rctx);
821
822 if (update)
823 nbytes = req->nbytes;
824 else
825 nbytes = 0;
826
827 rctx->total = nbytes + rctx->bufcnt;
828
829 if (!rctx->total)
830 return 0;
831
832 if (nbytes && (!IS_ALIGNED(rctx->bufcnt, bs))) {
833 int len = bs - rctx->bufcnt % bs;
834
835 if (len > nbytes)
836 len = nbytes;
837 scatterwalk_map_and_copy(rctx->buffer + rctx->bufcnt, req->src,
838 0, len, 0);
839 rctx->bufcnt += len;
840 nbytes -= len;
841 rctx->offset = len;
842 }
843
844 if (rctx->bufcnt)
845 memcpy(rctx->dd->xmit_buf, rctx->buffer, rctx->bufcnt);
846
847 ret = omap_sham_align_sgs(req->src, nbytes, bs, final, rctx);
848 if (ret)
849 return ret;
850
851 xmit_len = rctx->total;
852
853 if (!IS_ALIGNED(xmit_len, bs)) {
854 if (final)
855 xmit_len = DIV_ROUND_UP(xmit_len, bs) * bs;
856 else
857 xmit_len = xmit_len / bs * bs;
Tero Kristo898d86a2017-05-24 10:35:33 +0300858 } else if (!final) {
859 xmit_len -= bs;
Tero Kristof19de1b2016-09-19 18:22:15 +0300860 }
861
862 hash_later = rctx->total - xmit_len;
863 if (hash_later < 0)
864 hash_later = 0;
865
866 if (rctx->bufcnt && nbytes) {
867 /* have data from previous operation and current */
868 sg_init_table(rctx->sgl, 2);
869 sg_set_buf(rctx->sgl, rctx->dd->xmit_buf, rctx->bufcnt);
870
871 sg_chain(rctx->sgl, 2, req->src);
872
873 rctx->sg = rctx->sgl;
874
875 rctx->sg_len++;
876 } else if (rctx->bufcnt) {
877 /* have buffered data only */
878 sg_init_table(rctx->sgl, 1);
879 sg_set_buf(rctx->sgl, rctx->dd->xmit_buf, xmit_len);
880
881 rctx->sg = rctx->sgl;
882
883 rctx->sg_len = 1;
884 }
885
886 if (hash_later) {
Tero Kristo5d78d572017-05-24 10:35:32 +0300887 int offset = 0;
888
889 if (hash_later > req->nbytes) {
Tero Kristof19de1b2016-09-19 18:22:15 +0300890 memcpy(rctx->buffer, rctx->buffer + xmit_len,
Tero Kristo5d78d572017-05-24 10:35:32 +0300891 hash_later - req->nbytes);
892 offset = hash_later - req->nbytes;
Tero Kristof19de1b2016-09-19 18:22:15 +0300893 }
Tero Kristo5d78d572017-05-24 10:35:32 +0300894
895 if (req->nbytes) {
896 scatterwalk_map_and_copy(rctx->buffer + offset,
897 req->src,
898 offset + req->nbytes -
899 hash_later, hash_later, 0);
900 }
901
Tero Kristof19de1b2016-09-19 18:22:15 +0300902 rctx->bufcnt = hash_later;
903 } else {
904 rctx->bufcnt = 0;
905 }
906
907 if (!final)
908 rctx->total = xmit_len;
909
910 return 0;
911}
912
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800913static int omap_sham_update_dma_stop(struct omap_sham_dev *dd)
914{
915 struct omap_sham_reqctx *ctx = ahash_request_ctx(dd->req);
916
Tero Kristo8043bb12016-09-19 18:22:17 +0300917 dma_unmap_sg(dd->dev, ctx->sg, ctx->sg_len, DMA_TO_DEVICE);
Mark A. Greerdfd061d2012-12-21 10:04:04 -0700918
Tero Kristo8043bb12016-09-19 18:22:17 +0300919 clear_bit(FLAGS_DMA_ACTIVE, &dd->flags);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800920
921 return 0;
922}
923
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800924static int omap_sham_init(struct ahash_request *req)
925{
926 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
927 struct omap_sham_ctx *tctx = crypto_ahash_ctx(tfm);
928 struct omap_sham_reqctx *ctx = ahash_request_ctx(req);
929 struct omap_sham_dev *dd = NULL, *tmp;
Lokesh Vutlaeaef7e32013-07-26 12:29:14 +0530930 int bs = 0;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800931
932 spin_lock_bh(&sham.lock);
933 if (!tctx->dd) {
934 list_for_each_entry(tmp, &sham.dev_list, list) {
935 dd = tmp;
936 break;
937 }
938 tctx->dd = dd;
939 } else {
940 dd = tctx->dd;
941 }
942 spin_unlock_bh(&sham.lock);
943
944 ctx->dd = dd;
945
946 ctx->flags = 0;
947
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800948 dev_dbg(dd->dev, "init: digest size: %d\n",
949 crypto_ahash_digestsize(tfm));
950
Mark A. Greer0d373d62012-12-21 10:04:08 -0700951 switch (crypto_ahash_digestsize(tfm)) {
952 case MD5_DIGEST_SIZE:
953 ctx->flags |= FLAGS_MODE_MD5;
Lokesh Vutlaeaef7e32013-07-26 12:29:14 +0530954 bs = SHA1_BLOCK_SIZE;
Mark A. Greer0d373d62012-12-21 10:04:08 -0700955 break;
956 case SHA1_DIGEST_SIZE:
957 ctx->flags |= FLAGS_MODE_SHA1;
Lokesh Vutlaeaef7e32013-07-26 12:29:14 +0530958 bs = SHA1_BLOCK_SIZE;
Mark A. Greer0d373d62012-12-21 10:04:08 -0700959 break;
Mark A. Greerd20fb182012-12-21 10:04:09 -0700960 case SHA224_DIGEST_SIZE:
961 ctx->flags |= FLAGS_MODE_SHA224;
Lokesh Vutlaeaef7e32013-07-26 12:29:14 +0530962 bs = SHA224_BLOCK_SIZE;
Mark A. Greerd20fb182012-12-21 10:04:09 -0700963 break;
964 case SHA256_DIGEST_SIZE:
965 ctx->flags |= FLAGS_MODE_SHA256;
Lokesh Vutlaeaef7e32013-07-26 12:29:14 +0530966 bs = SHA256_BLOCK_SIZE;
967 break;
968 case SHA384_DIGEST_SIZE:
969 ctx->flags |= FLAGS_MODE_SHA384;
970 bs = SHA384_BLOCK_SIZE;
971 break;
972 case SHA512_DIGEST_SIZE:
973 ctx->flags |= FLAGS_MODE_SHA512;
974 bs = SHA512_BLOCK_SIZE;
Mark A. Greerd20fb182012-12-21 10:04:09 -0700975 break;
Mark A. Greer0d373d62012-12-21 10:04:08 -0700976 }
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800977
978 ctx->bufcnt = 0;
979 ctx->digcnt = 0;
Tero Kristo8043bb12016-09-19 18:22:17 +0300980 ctx->total = 0;
981 ctx->offset = 0;
Dmitry Kasatkin798eed5d2010-11-19 16:04:26 +0200982 ctx->buflen = BUFLEN;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800983
Dmitry Kasatkinea1fd222011-06-02 21:10:05 +0300984 if (tctx->flags & BIT(FLAGS_HMAC)) {
Mark A. Greer0d373d62012-12-21 10:04:08 -0700985 if (!test_bit(FLAGS_AUTO_XOR, &dd->flags)) {
986 struct omap_sham_hmac_ctx *bctx = tctx->base;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800987
Lokesh Vutlaeaef7e32013-07-26 12:29:14 +0530988 memcpy(ctx->buffer, bctx->ipad, bs);
989 ctx->bufcnt = bs;
Mark A. Greer0d373d62012-12-21 10:04:08 -0700990 }
991
Dmitry Kasatkinea1fd222011-06-02 21:10:05 +0300992 ctx->flags |= BIT(FLAGS_HMAC);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800993 }
994
995 return 0;
996
997}
998
999static int omap_sham_update_req(struct omap_sham_dev *dd)
1000{
1001 struct ahash_request *req = dd->req;
1002 struct omap_sham_reqctx *ctx = ahash_request_ctx(req);
1003 int err;
Tero Kristo8043bb12016-09-19 18:22:17 +03001004 bool final = ctx->flags & BIT(FLAGS_FINUP);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001005
1006 dev_dbg(dd->dev, "update_req: total: %u, digcnt: %d, finup: %d\n",
Dmitry Kasatkinea1fd222011-06-02 21:10:05 +03001007 ctx->total, ctx->digcnt, (ctx->flags & BIT(FLAGS_FINUP)) != 0);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001008
Tero Kristo8043bb12016-09-19 18:22:17 +03001009 if (ctx->total < get_block_size(ctx) ||
Tero Kristoc9af5992018-02-27 15:30:36 +02001010 ctx->total < dd->fallback_sz)
Tero Kristo8043bb12016-09-19 18:22:17 +03001011 ctx->flags |= BIT(FLAGS_CPU);
1012
Dmitry Kasatkinea1fd222011-06-02 21:10:05 +03001013 if (ctx->flags & BIT(FLAGS_CPU))
Tero Kristo8043bb12016-09-19 18:22:17 +03001014 err = omap_sham_xmit_cpu(dd, ctx->total, final);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001015 else
Tero Kristo8043bb12016-09-19 18:22:17 +03001016 err = omap_sham_xmit_dma(dd, ctx->total, final);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001017
1018 /* wait for dma completion before can take more data */
1019 dev_dbg(dd->dev, "update: err: %d, digcnt: %d\n", err, ctx->digcnt);
1020
1021 return err;
1022}
1023
1024static int omap_sham_final_req(struct omap_sham_dev *dd)
1025{
1026 struct ahash_request *req = dd->req;
1027 struct omap_sham_reqctx *ctx = ahash_request_ctx(req);
1028 int err = 0, use_dma = 1;
1029
Tero Kristo8043bb12016-09-19 18:22:17 +03001030 if ((ctx->total <= get_block_size(ctx)) || dd->polling_mode)
Lokesh Vutlab8411cc2013-08-20 20:32:34 +05301031 /*
1032 * faster to handle last block with cpu or
1033 * use cpu when dma is not present.
1034 */
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001035 use_dma = 0;
1036
1037 if (use_dma)
Tero Kristo8043bb12016-09-19 18:22:17 +03001038 err = omap_sham_xmit_dma(dd, ctx->total, 1);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001039 else
Tero Kristo8043bb12016-09-19 18:22:17 +03001040 err = omap_sham_xmit_cpu(dd, ctx->total, 1);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001041
1042 ctx->bufcnt = 0;
1043
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001044 dev_dbg(dd->dev, "final_req: err: %d\n", err);
1045
1046 return err;
1047}
1048
Dmitry Kasatkinbf362752011-04-20 13:34:58 +03001049static int omap_sham_finish_hmac(struct ahash_request *req)
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001050{
1051 struct omap_sham_ctx *tctx = crypto_tfm_ctx(req->base.tfm);
1052 struct omap_sham_hmac_ctx *bctx = tctx->base;
1053 int bs = crypto_shash_blocksize(bctx->shash);
1054 int ds = crypto_shash_digestsize(bctx->shash);
Behan Webster7bc53c32014-04-04 18:18:00 -03001055 SHASH_DESC_ON_STACK(shash, bctx->shash);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001056
Behan Webster7bc53c32014-04-04 18:18:00 -03001057 shash->tfm = bctx->shash;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001058
Behan Webster7bc53c32014-04-04 18:18:00 -03001059 return crypto_shash_init(shash) ?:
1060 crypto_shash_update(shash, bctx->opad, bs) ?:
1061 crypto_shash_finup(shash, req->result, ds, req->result);
Dmitry Kasatkinbf362752011-04-20 13:34:58 +03001062}
1063
1064static int omap_sham_finish(struct ahash_request *req)
1065{
1066 struct omap_sham_reqctx *ctx = ahash_request_ctx(req);
1067 struct omap_sham_dev *dd = ctx->dd;
1068 int err = 0;
1069
1070 if (ctx->digcnt) {
1071 omap_sham_copy_ready_hash(req);
Mark A. Greer0d373d62012-12-21 10:04:08 -07001072 if ((ctx->flags & BIT(FLAGS_HMAC)) &&
1073 !test_bit(FLAGS_AUTO_XOR, &dd->flags))
Dmitry Kasatkinbf362752011-04-20 13:34:58 +03001074 err = omap_sham_finish_hmac(req);
1075 }
1076
1077 dev_dbg(dd->dev, "digcnt: %d, bufcnt: %d\n", ctx->digcnt, ctx->bufcnt);
1078
1079 return err;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001080}
1081
1082static void omap_sham_finish_req(struct ahash_request *req, int err)
1083{
1084 struct omap_sham_reqctx *ctx = ahash_request_ctx(req);
Dmitry Kasatkin798eed5d2010-11-19 16:04:26 +02001085 struct omap_sham_dev *dd = ctx->dd;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001086
Tero Kristo8043bb12016-09-19 18:22:17 +03001087 if (test_bit(FLAGS_SGS_COPIED, &dd->flags))
1088 free_pages((unsigned long)sg_virt(ctx->sg),
Bin Liu9dbc8a02018-04-17 14:53:13 -05001089 get_order(ctx->sg->length + ctx->bufcnt));
Tero Kristo8043bb12016-09-19 18:22:17 +03001090
1091 if (test_bit(FLAGS_SGS_ALLOCED, &dd->flags))
1092 kfree(ctx->sg);
1093
1094 ctx->sg = NULL;
1095
1096 dd->flags &= ~(BIT(FLAGS_SGS_ALLOCED) | BIT(FLAGS_SGS_COPIED));
1097
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001098 if (!err) {
Mark A. Greer0d373d62012-12-21 10:04:08 -07001099 dd->pdata->copy_hash(req, 1);
Dmitry Kasatkined3ea9a82011-06-02 21:10:07 +03001100 if (test_bit(FLAGS_FINAL, &dd->flags))
Dmitry Kasatkinbf362752011-04-20 13:34:58 +03001101 err = omap_sham_finish(req);
Dmitry Kasatkin3e133c82010-11-19 16:04:24 +02001102 } else {
Dmitry Kasatkinea1fd222011-06-02 21:10:05 +03001103 ctx->flags |= BIT(FLAGS_ERROR);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001104 }
1105
Dmitry Kasatkin0efd4d82011-06-02 21:10:12 +03001106 /* atomic operation is not needed here */
1107 dd->flags &= ~(BIT(FLAGS_BUSY) | BIT(FLAGS_FINAL) | BIT(FLAGS_CPU) |
1108 BIT(FLAGS_DMA_READY) | BIT(FLAGS_OUTPUT_READY));
Mark A. Greerb359f032012-12-21 10:04:02 -07001109
Tero Kristoe93f7672016-06-22 16:23:34 +03001110 pm_runtime_mark_last_busy(dd->dev);
1111 pm_runtime_put_autosuspend(dd->dev);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001112
1113 if (req->base.complete)
1114 req->base.complete(&req->base, err);
1115}
1116
Dmitry Kasatkina5d87232010-11-19 16:04:25 +02001117static int omap_sham_handle_queue(struct omap_sham_dev *dd,
1118 struct ahash_request *req)
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001119{
Dmitry Kasatkin6c39d112010-12-29 21:52:04 +11001120 struct crypto_async_request *async_req, *backlog;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001121 struct omap_sham_reqctx *ctx;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001122 unsigned long flags;
Dmitry Kasatkina5d87232010-11-19 16:04:25 +02001123 int err = 0, ret = 0;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001124
Tero Kristo4e7813a2016-08-04 13:28:36 +03001125retry:
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001126 spin_lock_irqsave(&dd->lock, flags);
Dmitry Kasatkina5d87232010-11-19 16:04:25 +02001127 if (req)
1128 ret = ahash_enqueue_request(&dd->queue, req);
Dmitry Kasatkina929cbe2011-06-02 21:10:06 +03001129 if (test_bit(FLAGS_BUSY, &dd->flags)) {
Dmitry Kasatkina5d87232010-11-19 16:04:25 +02001130 spin_unlock_irqrestore(&dd->lock, flags);
1131 return ret;
1132 }
Dmitry Kasatkin6c39d112010-12-29 21:52:04 +11001133 backlog = crypto_get_backlog(&dd->queue);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001134 async_req = crypto_dequeue_request(&dd->queue);
Dmitry Kasatkin6c39d112010-12-29 21:52:04 +11001135 if (async_req)
Dmitry Kasatkina929cbe2011-06-02 21:10:06 +03001136 set_bit(FLAGS_BUSY, &dd->flags);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001137 spin_unlock_irqrestore(&dd->lock, flags);
1138
1139 if (!async_req)
Dmitry Kasatkina5d87232010-11-19 16:04:25 +02001140 return ret;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001141
1142 if (backlog)
1143 backlog->complete(backlog, -EINPROGRESS);
1144
1145 req = ahash_request_cast(async_req);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001146 dd->req = req;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001147 ctx = ahash_request_ctx(req);
1148
Tero Kristo8043bb12016-09-19 18:22:17 +03001149 err = omap_sham_prepare_request(req, ctx->op == OP_UPDATE);
Tero Kristo898d86a2017-05-24 10:35:33 +03001150 if (err || !ctx->total)
Tero Kristof19de1b2016-09-19 18:22:15 +03001151 goto err1;
1152
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001153 dev_dbg(dd->dev, "handling new req, op: %lu, nbytes: %d\n",
1154 ctx->op, req->nbytes);
1155
Dmitry Kasatkin798eed5d2010-11-19 16:04:26 +02001156 err = omap_sham_hw_init(dd);
1157 if (err)
1158 goto err1;
1159
Dmitry Kasatkin798eed5d2010-11-19 16:04:26 +02001160 if (ctx->digcnt)
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001161 /* request has changed - restore hash */
Mark A. Greer0d373d62012-12-21 10:04:08 -07001162 dd->pdata->copy_hash(req, 0);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001163
1164 if (ctx->op == OP_UPDATE) {
1165 err = omap_sham_update_req(dd);
Dmitry Kasatkinea1fd222011-06-02 21:10:05 +03001166 if (err != -EINPROGRESS && (ctx->flags & BIT(FLAGS_FINUP)))
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001167 /* no final() after finup() */
1168 err = omap_sham_final_req(dd);
1169 } else if (ctx->op == OP_FINAL) {
1170 err = omap_sham_final_req(dd);
1171 }
Dmitry Kasatkin798eed5d2010-11-19 16:04:26 +02001172err1:
Tero Kristo4e7813a2016-08-04 13:28:36 +03001173 dev_dbg(dd->dev, "exit, err: %d\n", err);
1174
1175 if (err != -EINPROGRESS) {
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001176 /* done_task will not finish it, so do it here */
1177 omap_sham_finish_req(req, err);
Tero Kristo4e7813a2016-08-04 13:28:36 +03001178 req = NULL;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001179
Tero Kristo4e7813a2016-08-04 13:28:36 +03001180 /*
1181 * Execute next request immediately if there is anything
1182 * in queue.
1183 */
1184 goto retry;
1185 }
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001186
Dmitry Kasatkina5d87232010-11-19 16:04:25 +02001187 return ret;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001188}
1189
1190static int omap_sham_enqueue(struct ahash_request *req, unsigned int op)
1191{
1192 struct omap_sham_reqctx *ctx = ahash_request_ctx(req);
1193 struct omap_sham_ctx *tctx = crypto_tfm_ctx(req->base.tfm);
1194 struct omap_sham_dev *dd = tctx->dd;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001195
1196 ctx->op = op;
1197
Dmitry Kasatkina5d87232010-11-19 16:04:25 +02001198 return omap_sham_handle_queue(dd, req);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001199}
1200
1201static int omap_sham_update(struct ahash_request *req)
1202{
1203 struct omap_sham_reqctx *ctx = ahash_request_ctx(req);
Lokesh Vutlab8411cc2013-08-20 20:32:34 +05301204 struct omap_sham_dev *dd = ctx->dd;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001205
1206 if (!req->nbytes)
1207 return 0;
1208
Tero Kristo5d78d572017-05-24 10:35:32 +03001209 if (ctx->bufcnt + req->nbytes <= ctx->buflen) {
Tero Kristo8043bb12016-09-19 18:22:17 +03001210 scatterwalk_map_and_copy(ctx->buffer + ctx->bufcnt, req->src,
1211 0, req->nbytes, 0);
1212 ctx->bufcnt += req->nbytes;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001213 return 0;
1214 }
1215
Lokesh Vutlaacef7b02013-12-18 19:03:33 +05301216 if (dd->polling_mode)
1217 ctx->flags |= BIT(FLAGS_CPU);
1218
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001219 return omap_sham_enqueue(req, OP_UPDATE);
1220}
1221
Behan Webster7bc53c32014-04-04 18:18:00 -03001222static int omap_sham_shash_digest(struct crypto_shash *tfm, u32 flags,
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001223 const u8 *data, unsigned int len, u8 *out)
1224{
Behan Webster7bc53c32014-04-04 18:18:00 -03001225 SHASH_DESC_ON_STACK(shash, tfm);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001226
Behan Webster7bc53c32014-04-04 18:18:00 -03001227 shash->tfm = tfm;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001228
Behan Webster7bc53c32014-04-04 18:18:00 -03001229 return crypto_shash_digest(shash, data, len, out);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001230}
1231
1232static int omap_sham_final_shash(struct ahash_request *req)
1233{
1234 struct omap_sham_ctx *tctx = crypto_tfm_ctx(req->base.tfm);
1235 struct omap_sham_reqctx *ctx = ahash_request_ctx(req);
Tero Kristocb8d5c82016-08-04 13:28:40 +03001236 int offset = 0;
1237
1238 /*
1239 * If we are running HMAC on limited hardware support, skip
1240 * the ipad in the beginning of the buffer if we are going for
1241 * software fallback algorithm.
1242 */
1243 if (test_bit(FLAGS_HMAC, &ctx->flags) &&
1244 !test_bit(FLAGS_AUTO_XOR, &ctx->dd->flags))
1245 offset = get_block_size(ctx);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001246
1247 return omap_sham_shash_digest(tctx->fallback, req->base.flags,
Tero Kristocb8d5c82016-08-04 13:28:40 +03001248 ctx->buffer + offset,
1249 ctx->bufcnt - offset, req->result);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001250}
1251
1252static int omap_sham_final(struct ahash_request *req)
1253{
1254 struct omap_sham_reqctx *ctx = ahash_request_ctx(req);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001255
Dmitry Kasatkinea1fd222011-06-02 21:10:05 +03001256 ctx->flags |= BIT(FLAGS_FINUP);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001257
Dmitry Kasatkinea1fd222011-06-02 21:10:05 +03001258 if (ctx->flags & BIT(FLAGS_ERROR))
Dmitry Kasatkinbf362752011-04-20 13:34:58 +03001259 return 0; /* uncompleted hash is not needed */
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001260
Bin Liu85e06872016-06-22 16:23:37 +03001261 /*
1262 * OMAP HW accel works only with buffers >= 9.
1263 * HMAC is always >= 9 because ipad == block size.
Tero Kristoc9af5992018-02-27 15:30:36 +02001264 * If buffersize is less than fallback_sz, we use fallback
Tero Kristo2c5bd1e2016-09-19 18:22:16 +03001265 * SW encoding, as using DMA + HW in this case doesn't provide
1266 * any benefit.
Bin Liu85e06872016-06-22 16:23:37 +03001267 */
Tero Kristoc9af5992018-02-27 15:30:36 +02001268 if (!ctx->digcnt && ctx->bufcnt < ctx->dd->fallback_sz)
Dmitry Kasatkinbf362752011-04-20 13:34:58 +03001269 return omap_sham_final_shash(req);
1270 else if (ctx->bufcnt)
1271 return omap_sham_enqueue(req, OP_FINAL);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001272
Dmitry Kasatkinbf362752011-04-20 13:34:58 +03001273 /* copy ready hash (+ finalize hmac) */
1274 return omap_sham_finish(req);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001275}
1276
1277static int omap_sham_finup(struct ahash_request *req)
1278{
1279 struct omap_sham_reqctx *ctx = ahash_request_ctx(req);
1280 int err1, err2;
1281
Dmitry Kasatkinea1fd222011-06-02 21:10:05 +03001282 ctx->flags |= BIT(FLAGS_FINUP);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001283
1284 err1 = omap_sham_update(req);
Markku Kylanpaa455e3382011-04-20 13:34:55 +03001285 if (err1 == -EINPROGRESS || err1 == -EBUSY)
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001286 return err1;
1287 /*
1288 * final() has to be always called to cleanup resources
1289 * even if udpate() failed, except EINPROGRESS
1290 */
1291 err2 = omap_sham_final(req);
1292
1293 return err1 ?: err2;
1294}
1295
1296static int omap_sham_digest(struct ahash_request *req)
1297{
1298 return omap_sham_init(req) ?: omap_sham_finup(req);
1299}
1300
1301static int omap_sham_setkey(struct crypto_ahash *tfm, const u8 *key,
1302 unsigned int keylen)
1303{
1304 struct omap_sham_ctx *tctx = crypto_ahash_ctx(tfm);
1305 struct omap_sham_hmac_ctx *bctx = tctx->base;
1306 int bs = crypto_shash_blocksize(bctx->shash);
1307 int ds = crypto_shash_digestsize(bctx->shash);
Mark A. Greer0d373d62012-12-21 10:04:08 -07001308 struct omap_sham_dev *dd = NULL, *tmp;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001309 int err, i;
Mark A. Greer0d373d62012-12-21 10:04:08 -07001310
1311 spin_lock_bh(&sham.lock);
1312 if (!tctx->dd) {
1313 list_for_each_entry(tmp, &sham.dev_list, list) {
1314 dd = tmp;
1315 break;
1316 }
1317 tctx->dd = dd;
1318 } else {
1319 dd = tctx->dd;
1320 }
1321 spin_unlock_bh(&sham.lock);
1322
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001323 err = crypto_shash_setkey(tctx->fallback, key, keylen);
1324 if (err)
1325 return err;
1326
1327 if (keylen > bs) {
1328 err = omap_sham_shash_digest(bctx->shash,
1329 crypto_shash_get_flags(bctx->shash),
1330 key, keylen, bctx->ipad);
1331 if (err)
1332 return err;
1333 keylen = ds;
1334 } else {
1335 memcpy(bctx->ipad, key, keylen);
1336 }
1337
1338 memset(bctx->ipad + keylen, 0, bs - keylen);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001339
Mark A. Greer0d373d62012-12-21 10:04:08 -07001340 if (!test_bit(FLAGS_AUTO_XOR, &dd->flags)) {
1341 memcpy(bctx->opad, bctx->ipad, bs);
1342
1343 for (i = 0; i < bs; i++) {
Corentin LABBEebd401e2017-05-19 08:53:28 +02001344 bctx->ipad[i] ^= HMAC_IPAD_VALUE;
1345 bctx->opad[i] ^= HMAC_OPAD_VALUE;
Mark A. Greer0d373d62012-12-21 10:04:08 -07001346 }
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001347 }
1348
1349 return err;
1350}
1351
1352static int omap_sham_cra_init_alg(struct crypto_tfm *tfm, const char *alg_base)
1353{
1354 struct omap_sham_ctx *tctx = crypto_tfm_ctx(tfm);
1355 const char *alg_name = crypto_tfm_alg_name(tfm);
1356
1357 /* Allocate a fallback and abort if it failed. */
1358 tctx->fallback = crypto_alloc_shash(alg_name, 0,
1359 CRYPTO_ALG_NEED_FALLBACK);
1360 if (IS_ERR(tctx->fallback)) {
1361 pr_err("omap-sham: fallback driver '%s' "
1362 "could not be loaded.\n", alg_name);
1363 return PTR_ERR(tctx->fallback);
1364 }
1365
1366 crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
Dmitry Kasatkin798eed5d2010-11-19 16:04:26 +02001367 sizeof(struct omap_sham_reqctx) + BUFLEN);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001368
1369 if (alg_base) {
1370 struct omap_sham_hmac_ctx *bctx = tctx->base;
Dmitry Kasatkinea1fd222011-06-02 21:10:05 +03001371 tctx->flags |= BIT(FLAGS_HMAC);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001372 bctx->shash = crypto_alloc_shash(alg_base, 0,
1373 CRYPTO_ALG_NEED_FALLBACK);
1374 if (IS_ERR(bctx->shash)) {
1375 pr_err("omap-sham: base driver '%s' "
1376 "could not be loaded.\n", alg_base);
1377 crypto_free_shash(tctx->fallback);
1378 return PTR_ERR(bctx->shash);
1379 }
1380
1381 }
1382
1383 return 0;
1384}
1385
1386static int omap_sham_cra_init(struct crypto_tfm *tfm)
1387{
1388 return omap_sham_cra_init_alg(tfm, NULL);
1389}
1390
1391static int omap_sham_cra_sha1_init(struct crypto_tfm *tfm)
1392{
1393 return omap_sham_cra_init_alg(tfm, "sha1");
1394}
1395
Mark A. Greerd20fb182012-12-21 10:04:09 -07001396static int omap_sham_cra_sha224_init(struct crypto_tfm *tfm)
1397{
1398 return omap_sham_cra_init_alg(tfm, "sha224");
1399}
1400
1401static int omap_sham_cra_sha256_init(struct crypto_tfm *tfm)
1402{
1403 return omap_sham_cra_init_alg(tfm, "sha256");
1404}
1405
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001406static int omap_sham_cra_md5_init(struct crypto_tfm *tfm)
1407{
1408 return omap_sham_cra_init_alg(tfm, "md5");
1409}
1410
Lokesh Vutlaeaef7e32013-07-26 12:29:14 +05301411static int omap_sham_cra_sha384_init(struct crypto_tfm *tfm)
1412{
1413 return omap_sham_cra_init_alg(tfm, "sha384");
1414}
1415
1416static int omap_sham_cra_sha512_init(struct crypto_tfm *tfm)
1417{
1418 return omap_sham_cra_init_alg(tfm, "sha512");
1419}
1420
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001421static void omap_sham_cra_exit(struct crypto_tfm *tfm)
1422{
1423 struct omap_sham_ctx *tctx = crypto_tfm_ctx(tfm);
1424
1425 crypto_free_shash(tctx->fallback);
1426 tctx->fallback = NULL;
1427
Dmitry Kasatkinea1fd222011-06-02 21:10:05 +03001428 if (tctx->flags & BIT(FLAGS_HMAC)) {
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001429 struct omap_sham_hmac_ctx *bctx = tctx->base;
1430 crypto_free_shash(bctx->shash);
1431 }
1432}
1433
Tero Kristo99a7fff2016-09-19 18:22:12 +03001434static int omap_sham_export(struct ahash_request *req, void *out)
1435{
Tero Kristoa84d3512016-09-19 18:22:18 +03001436 struct omap_sham_reqctx *rctx = ahash_request_ctx(req);
1437
1438 memcpy(out, rctx, sizeof(*rctx) + rctx->bufcnt);
1439
1440 return 0;
Tero Kristo99a7fff2016-09-19 18:22:12 +03001441}
1442
1443static int omap_sham_import(struct ahash_request *req, const void *in)
1444{
Tero Kristoa84d3512016-09-19 18:22:18 +03001445 struct omap_sham_reqctx *rctx = ahash_request_ctx(req);
1446 const struct omap_sham_reqctx *ctx_in = in;
1447
1448 memcpy(rctx, in, sizeof(*rctx) + ctx_in->bufcnt);
1449
1450 return 0;
Tero Kristo99a7fff2016-09-19 18:22:12 +03001451}
1452
Mark A. Greerd20fb182012-12-21 10:04:09 -07001453static struct ahash_alg algs_sha1_md5[] = {
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001454{
1455 .init = omap_sham_init,
1456 .update = omap_sham_update,
1457 .final = omap_sham_final,
1458 .finup = omap_sham_finup,
1459 .digest = omap_sham_digest,
1460 .halg.digestsize = SHA1_DIGEST_SIZE,
1461 .halg.base = {
1462 .cra_name = "sha1",
1463 .cra_driver_name = "omap-sha1",
Bin Liueb354782016-06-30 14:04:11 -05001464 .cra_priority = 400,
Eric Biggers6a38f622018-06-30 15:16:12 -07001465 .cra_flags = CRYPTO_ALG_KERN_DRIVER_ONLY |
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001466 CRYPTO_ALG_ASYNC |
1467 CRYPTO_ALG_NEED_FALLBACK,
1468 .cra_blocksize = SHA1_BLOCK_SIZE,
1469 .cra_ctxsize = sizeof(struct omap_sham_ctx),
Tero Kristo744e6862016-09-19 18:22:13 +03001470 .cra_alignmask = OMAP_ALIGN_MASK,
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001471 .cra_module = THIS_MODULE,
1472 .cra_init = omap_sham_cra_init,
1473 .cra_exit = omap_sham_cra_exit,
1474 }
1475},
1476{
1477 .init = omap_sham_init,
1478 .update = omap_sham_update,
1479 .final = omap_sham_final,
1480 .finup = omap_sham_finup,
1481 .digest = omap_sham_digest,
1482 .halg.digestsize = MD5_DIGEST_SIZE,
1483 .halg.base = {
1484 .cra_name = "md5",
1485 .cra_driver_name = "omap-md5",
Bin Liueb354782016-06-30 14:04:11 -05001486 .cra_priority = 400,
Eric Biggers6a38f622018-06-30 15:16:12 -07001487 .cra_flags = CRYPTO_ALG_KERN_DRIVER_ONLY |
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001488 CRYPTO_ALG_ASYNC |
1489 CRYPTO_ALG_NEED_FALLBACK,
1490 .cra_blocksize = SHA1_BLOCK_SIZE,
1491 .cra_ctxsize = sizeof(struct omap_sham_ctx),
Dmitry Kasatkin798eed5d2010-11-19 16:04:26 +02001492 .cra_alignmask = OMAP_ALIGN_MASK,
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001493 .cra_module = THIS_MODULE,
1494 .cra_init = omap_sham_cra_init,
1495 .cra_exit = omap_sham_cra_exit,
1496 }
1497},
1498{
1499 .init = omap_sham_init,
1500 .update = omap_sham_update,
1501 .final = omap_sham_final,
1502 .finup = omap_sham_finup,
1503 .digest = omap_sham_digest,
1504 .setkey = omap_sham_setkey,
1505 .halg.digestsize = SHA1_DIGEST_SIZE,
1506 .halg.base = {
1507 .cra_name = "hmac(sha1)",
1508 .cra_driver_name = "omap-hmac-sha1",
Bin Liueb354782016-06-30 14:04:11 -05001509 .cra_priority = 400,
Eric Biggers6a38f622018-06-30 15:16:12 -07001510 .cra_flags = CRYPTO_ALG_KERN_DRIVER_ONLY |
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001511 CRYPTO_ALG_ASYNC |
1512 CRYPTO_ALG_NEED_FALLBACK,
1513 .cra_blocksize = SHA1_BLOCK_SIZE,
1514 .cra_ctxsize = sizeof(struct omap_sham_ctx) +
1515 sizeof(struct omap_sham_hmac_ctx),
Dmitry Kasatkin798eed5d2010-11-19 16:04:26 +02001516 .cra_alignmask = OMAP_ALIGN_MASK,
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001517 .cra_module = THIS_MODULE,
1518 .cra_init = omap_sham_cra_sha1_init,
1519 .cra_exit = omap_sham_cra_exit,
1520 }
1521},
1522{
1523 .init = omap_sham_init,
1524 .update = omap_sham_update,
1525 .final = omap_sham_final,
1526 .finup = omap_sham_finup,
1527 .digest = omap_sham_digest,
1528 .setkey = omap_sham_setkey,
1529 .halg.digestsize = MD5_DIGEST_SIZE,
1530 .halg.base = {
1531 .cra_name = "hmac(md5)",
1532 .cra_driver_name = "omap-hmac-md5",
Bin Liueb354782016-06-30 14:04:11 -05001533 .cra_priority = 400,
Eric Biggers6a38f622018-06-30 15:16:12 -07001534 .cra_flags = CRYPTO_ALG_KERN_DRIVER_ONLY |
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001535 CRYPTO_ALG_ASYNC |
1536 CRYPTO_ALG_NEED_FALLBACK,
1537 .cra_blocksize = SHA1_BLOCK_SIZE,
1538 .cra_ctxsize = sizeof(struct omap_sham_ctx) +
1539 sizeof(struct omap_sham_hmac_ctx),
Dmitry Kasatkin798eed5d2010-11-19 16:04:26 +02001540 .cra_alignmask = OMAP_ALIGN_MASK,
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001541 .cra_module = THIS_MODULE,
1542 .cra_init = omap_sham_cra_md5_init,
1543 .cra_exit = omap_sham_cra_exit,
1544 }
1545}
1546};
1547
Mark A. Greerd20fb182012-12-21 10:04:09 -07001548/* OMAP4 has some algs in addition to what OMAP2 has */
1549static struct ahash_alg algs_sha224_sha256[] = {
1550{
1551 .init = omap_sham_init,
1552 .update = omap_sham_update,
1553 .final = omap_sham_final,
1554 .finup = omap_sham_finup,
1555 .digest = omap_sham_digest,
1556 .halg.digestsize = SHA224_DIGEST_SIZE,
1557 .halg.base = {
1558 .cra_name = "sha224",
1559 .cra_driver_name = "omap-sha224",
Bin Liueb354782016-06-30 14:04:11 -05001560 .cra_priority = 400,
Eric Biggers6a38f622018-06-30 15:16:12 -07001561 .cra_flags = CRYPTO_ALG_ASYNC |
Mark A. Greerd20fb182012-12-21 10:04:09 -07001562 CRYPTO_ALG_NEED_FALLBACK,
1563 .cra_blocksize = SHA224_BLOCK_SIZE,
1564 .cra_ctxsize = sizeof(struct omap_sham_ctx),
Tero Kristo744e6862016-09-19 18:22:13 +03001565 .cra_alignmask = OMAP_ALIGN_MASK,
Mark A. Greerd20fb182012-12-21 10:04:09 -07001566 .cra_module = THIS_MODULE,
1567 .cra_init = omap_sham_cra_init,
1568 .cra_exit = omap_sham_cra_exit,
1569 }
1570},
1571{
1572 .init = omap_sham_init,
1573 .update = omap_sham_update,
1574 .final = omap_sham_final,
1575 .finup = omap_sham_finup,
1576 .digest = omap_sham_digest,
1577 .halg.digestsize = SHA256_DIGEST_SIZE,
1578 .halg.base = {
1579 .cra_name = "sha256",
1580 .cra_driver_name = "omap-sha256",
Bin Liueb354782016-06-30 14:04:11 -05001581 .cra_priority = 400,
Eric Biggers6a38f622018-06-30 15:16:12 -07001582 .cra_flags = CRYPTO_ALG_ASYNC |
Mark A. Greerd20fb182012-12-21 10:04:09 -07001583 CRYPTO_ALG_NEED_FALLBACK,
1584 .cra_blocksize = SHA256_BLOCK_SIZE,
1585 .cra_ctxsize = sizeof(struct omap_sham_ctx),
Tero Kristo744e6862016-09-19 18:22:13 +03001586 .cra_alignmask = OMAP_ALIGN_MASK,
Mark A. Greerd20fb182012-12-21 10:04:09 -07001587 .cra_module = THIS_MODULE,
1588 .cra_init = omap_sham_cra_init,
1589 .cra_exit = omap_sham_cra_exit,
1590 }
1591},
1592{
1593 .init = omap_sham_init,
1594 .update = omap_sham_update,
1595 .final = omap_sham_final,
1596 .finup = omap_sham_finup,
1597 .digest = omap_sham_digest,
1598 .setkey = omap_sham_setkey,
1599 .halg.digestsize = SHA224_DIGEST_SIZE,
1600 .halg.base = {
1601 .cra_name = "hmac(sha224)",
1602 .cra_driver_name = "omap-hmac-sha224",
Bin Liueb354782016-06-30 14:04:11 -05001603 .cra_priority = 400,
Eric Biggers6a38f622018-06-30 15:16:12 -07001604 .cra_flags = CRYPTO_ALG_ASYNC |
Mark A. Greerd20fb182012-12-21 10:04:09 -07001605 CRYPTO_ALG_NEED_FALLBACK,
1606 .cra_blocksize = SHA224_BLOCK_SIZE,
1607 .cra_ctxsize = sizeof(struct omap_sham_ctx) +
1608 sizeof(struct omap_sham_hmac_ctx),
1609 .cra_alignmask = OMAP_ALIGN_MASK,
1610 .cra_module = THIS_MODULE,
1611 .cra_init = omap_sham_cra_sha224_init,
1612 .cra_exit = omap_sham_cra_exit,
1613 }
1614},
1615{
1616 .init = omap_sham_init,
1617 .update = omap_sham_update,
1618 .final = omap_sham_final,
1619 .finup = omap_sham_finup,
1620 .digest = omap_sham_digest,
1621 .setkey = omap_sham_setkey,
1622 .halg.digestsize = SHA256_DIGEST_SIZE,
1623 .halg.base = {
1624 .cra_name = "hmac(sha256)",
1625 .cra_driver_name = "omap-hmac-sha256",
Bin Liueb354782016-06-30 14:04:11 -05001626 .cra_priority = 400,
Eric Biggers6a38f622018-06-30 15:16:12 -07001627 .cra_flags = CRYPTO_ALG_ASYNC |
Mark A. Greerd20fb182012-12-21 10:04:09 -07001628 CRYPTO_ALG_NEED_FALLBACK,
1629 .cra_blocksize = SHA256_BLOCK_SIZE,
1630 .cra_ctxsize = sizeof(struct omap_sham_ctx) +
1631 sizeof(struct omap_sham_hmac_ctx),
1632 .cra_alignmask = OMAP_ALIGN_MASK,
1633 .cra_module = THIS_MODULE,
1634 .cra_init = omap_sham_cra_sha256_init,
1635 .cra_exit = omap_sham_cra_exit,
1636 }
1637},
1638};
1639
Lokesh Vutlaeaef7e32013-07-26 12:29:14 +05301640static struct ahash_alg algs_sha384_sha512[] = {
1641{
1642 .init = omap_sham_init,
1643 .update = omap_sham_update,
1644 .final = omap_sham_final,
1645 .finup = omap_sham_finup,
1646 .digest = omap_sham_digest,
1647 .halg.digestsize = SHA384_DIGEST_SIZE,
1648 .halg.base = {
1649 .cra_name = "sha384",
1650 .cra_driver_name = "omap-sha384",
Bin Liueb354782016-06-30 14:04:11 -05001651 .cra_priority = 400,
Eric Biggers6a38f622018-06-30 15:16:12 -07001652 .cra_flags = CRYPTO_ALG_ASYNC |
Lokesh Vutlaeaef7e32013-07-26 12:29:14 +05301653 CRYPTO_ALG_NEED_FALLBACK,
1654 .cra_blocksize = SHA384_BLOCK_SIZE,
1655 .cra_ctxsize = sizeof(struct omap_sham_ctx),
Tero Kristo744e6862016-09-19 18:22:13 +03001656 .cra_alignmask = OMAP_ALIGN_MASK,
Lokesh Vutlaeaef7e32013-07-26 12:29:14 +05301657 .cra_module = THIS_MODULE,
1658 .cra_init = omap_sham_cra_init,
1659 .cra_exit = omap_sham_cra_exit,
1660 }
1661},
1662{
1663 .init = omap_sham_init,
1664 .update = omap_sham_update,
1665 .final = omap_sham_final,
1666 .finup = omap_sham_finup,
1667 .digest = omap_sham_digest,
1668 .halg.digestsize = SHA512_DIGEST_SIZE,
1669 .halg.base = {
1670 .cra_name = "sha512",
1671 .cra_driver_name = "omap-sha512",
Bin Liueb354782016-06-30 14:04:11 -05001672 .cra_priority = 400,
Eric Biggers6a38f622018-06-30 15:16:12 -07001673 .cra_flags = CRYPTO_ALG_ASYNC |
Lokesh Vutlaeaef7e32013-07-26 12:29:14 +05301674 CRYPTO_ALG_NEED_FALLBACK,
1675 .cra_blocksize = SHA512_BLOCK_SIZE,
1676 .cra_ctxsize = sizeof(struct omap_sham_ctx),
Tero Kristo744e6862016-09-19 18:22:13 +03001677 .cra_alignmask = OMAP_ALIGN_MASK,
Lokesh Vutlaeaef7e32013-07-26 12:29:14 +05301678 .cra_module = THIS_MODULE,
1679 .cra_init = omap_sham_cra_init,
1680 .cra_exit = omap_sham_cra_exit,
1681 }
1682},
1683{
1684 .init = omap_sham_init,
1685 .update = omap_sham_update,
1686 .final = omap_sham_final,
1687 .finup = omap_sham_finup,
1688 .digest = omap_sham_digest,
1689 .setkey = omap_sham_setkey,
1690 .halg.digestsize = SHA384_DIGEST_SIZE,
1691 .halg.base = {
1692 .cra_name = "hmac(sha384)",
1693 .cra_driver_name = "omap-hmac-sha384",
Bin Liueb354782016-06-30 14:04:11 -05001694 .cra_priority = 400,
Eric Biggers6a38f622018-06-30 15:16:12 -07001695 .cra_flags = CRYPTO_ALG_ASYNC |
Lokesh Vutlaeaef7e32013-07-26 12:29:14 +05301696 CRYPTO_ALG_NEED_FALLBACK,
1697 .cra_blocksize = SHA384_BLOCK_SIZE,
1698 .cra_ctxsize = sizeof(struct omap_sham_ctx) +
1699 sizeof(struct omap_sham_hmac_ctx),
1700 .cra_alignmask = OMAP_ALIGN_MASK,
1701 .cra_module = THIS_MODULE,
1702 .cra_init = omap_sham_cra_sha384_init,
1703 .cra_exit = omap_sham_cra_exit,
1704 }
1705},
1706{
1707 .init = omap_sham_init,
1708 .update = omap_sham_update,
1709 .final = omap_sham_final,
1710 .finup = omap_sham_finup,
1711 .digest = omap_sham_digest,
1712 .setkey = omap_sham_setkey,
1713 .halg.digestsize = SHA512_DIGEST_SIZE,
1714 .halg.base = {
1715 .cra_name = "hmac(sha512)",
1716 .cra_driver_name = "omap-hmac-sha512",
Bin Liueb354782016-06-30 14:04:11 -05001717 .cra_priority = 400,
Eric Biggers6a38f622018-06-30 15:16:12 -07001718 .cra_flags = CRYPTO_ALG_ASYNC |
Lokesh Vutlaeaef7e32013-07-26 12:29:14 +05301719 CRYPTO_ALG_NEED_FALLBACK,
1720 .cra_blocksize = SHA512_BLOCK_SIZE,
1721 .cra_ctxsize = sizeof(struct omap_sham_ctx) +
1722 sizeof(struct omap_sham_hmac_ctx),
1723 .cra_alignmask = OMAP_ALIGN_MASK,
1724 .cra_module = THIS_MODULE,
1725 .cra_init = omap_sham_cra_sha512_init,
1726 .cra_exit = omap_sham_cra_exit,
1727 }
1728},
1729};
1730
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001731static void omap_sham_done_task(unsigned long data)
1732{
1733 struct omap_sham_dev *dd = (struct omap_sham_dev *)data;
Dmitry Kasatkin6c63db82011-06-02 21:10:10 +03001734 int err = 0;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001735
Dmitry Kasatkin6cb3ffe2011-06-02 21:10:09 +03001736 if (!test_bit(FLAGS_BUSY, &dd->flags)) {
1737 omap_sham_handle_queue(dd, NULL);
1738 return;
1739 }
1740
Dmitry Kasatkin6c63db82011-06-02 21:10:10 +03001741 if (test_bit(FLAGS_CPU, &dd->flags)) {
Tero Kristo8043bb12016-09-19 18:22:17 +03001742 if (test_and_clear_bit(FLAGS_OUTPUT_READY, &dd->flags))
1743 goto finish;
Dmitry Kasatkin6c63db82011-06-02 21:10:10 +03001744 } else if (test_bit(FLAGS_DMA_READY, &dd->flags)) {
1745 if (test_and_clear_bit(FLAGS_DMA_ACTIVE, &dd->flags)) {
1746 omap_sham_update_dma_stop(dd);
1747 if (dd->err) {
1748 err = dd->err;
1749 goto finish;
1750 }
1751 }
1752 if (test_and_clear_bit(FLAGS_OUTPUT_READY, &dd->flags)) {
1753 /* hash or semi-hash ready */
1754 clear_bit(FLAGS_DMA_READY, &dd->flags);
Krzysztof Kozlowski17f5b192018-03-01 21:50:11 +01001755 goto finish;
Dmitry Kasatkin6c63db82011-06-02 21:10:10 +03001756 }
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001757 }
1758
Dmitry Kasatkin6c63db82011-06-02 21:10:10 +03001759 return;
Dmitry Kasatkin3e133c82010-11-19 16:04:24 +02001760
Dmitry Kasatkin6c63db82011-06-02 21:10:10 +03001761finish:
1762 dev_dbg(dd->dev, "update done: err: %d\n", err);
1763 /* finish curent request */
1764 omap_sham_finish_req(dd->req, err);
Tero Kristo4e7813a2016-08-04 13:28:36 +03001765
1766 /* If we are not busy, process next req */
1767 if (!test_bit(FLAGS_BUSY, &dd->flags))
1768 omap_sham_handle_queue(dd, NULL);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001769}
1770
Mark A. Greer0d373d62012-12-21 10:04:08 -07001771static irqreturn_t omap_sham_irq_common(struct omap_sham_dev *dd)
1772{
1773 if (!test_bit(FLAGS_BUSY, &dd->flags)) {
1774 dev_warn(dd->dev, "Interrupt when no active requests.\n");
1775 } else {
1776 set_bit(FLAGS_OUTPUT_READY, &dd->flags);
1777 tasklet_schedule(&dd->done_task);
1778 }
1779
1780 return IRQ_HANDLED;
1781}
1782
1783static irqreturn_t omap_sham_irq_omap2(int irq, void *dev_id)
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001784{
1785 struct omap_sham_dev *dd = dev_id;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001786
Dmitry Kasatkined3ea9a82011-06-02 21:10:07 +03001787 if (unlikely(test_bit(FLAGS_FINAL, &dd->flags)))
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001788 /* final -> allow device to go to power-saving mode */
1789 omap_sham_write_mask(dd, SHA_REG_CTRL, 0, SHA_REG_CTRL_LENGTH);
1790
1791 omap_sham_write_mask(dd, SHA_REG_CTRL, SHA_REG_CTRL_OUTPUT_READY,
1792 SHA_REG_CTRL_OUTPUT_READY);
1793 omap_sham_read(dd, SHA_REG_CTRL);
1794
Mark A. Greer0d373d62012-12-21 10:04:08 -07001795 return omap_sham_irq_common(dd);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001796}
1797
Mark A. Greer0d373d62012-12-21 10:04:08 -07001798static irqreturn_t omap_sham_irq_omap4(int irq, void *dev_id)
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001799{
Mark A. Greer0d373d62012-12-21 10:04:08 -07001800 struct omap_sham_dev *dd = dev_id;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001801
Mark A. Greer0d373d62012-12-21 10:04:08 -07001802 omap_sham_write_mask(dd, SHA_REG_MASK(dd), 0, SHA_REG_MASK_IT_EN);
Dmitry Kasatkin3e133c82010-11-19 16:04:24 +02001803
Mark A. Greer0d373d62012-12-21 10:04:08 -07001804 return omap_sham_irq_common(dd);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001805}
1806
Mark A. Greerd20fb182012-12-21 10:04:09 -07001807static struct omap_sham_algs_info omap_sham_algs_info_omap2[] = {
1808 {
1809 .algs_list = algs_sha1_md5,
1810 .size = ARRAY_SIZE(algs_sha1_md5),
1811 },
1812};
1813
Mark A. Greer0d373d62012-12-21 10:04:08 -07001814static const struct omap_sham_pdata omap_sham_pdata_omap2 = {
Mark A. Greerd20fb182012-12-21 10:04:09 -07001815 .algs_info = omap_sham_algs_info_omap2,
1816 .algs_info_size = ARRAY_SIZE(omap_sham_algs_info_omap2),
Mark A. Greer0d373d62012-12-21 10:04:08 -07001817 .flags = BIT(FLAGS_BE32_SHA1),
1818 .digest_size = SHA1_DIGEST_SIZE,
1819 .copy_hash = omap_sham_copy_hash_omap2,
1820 .write_ctrl = omap_sham_write_ctrl_omap2,
1821 .trigger = omap_sham_trigger_omap2,
1822 .poll_irq = omap_sham_poll_irq_omap2,
1823 .intr_hdlr = omap_sham_irq_omap2,
1824 .idigest_ofs = 0x00,
1825 .din_ofs = 0x1c,
1826 .digcnt_ofs = 0x14,
1827 .rev_ofs = 0x5c,
1828 .mask_ofs = 0x60,
1829 .sysstatus_ofs = 0x64,
1830 .major_mask = 0xf0,
1831 .major_shift = 4,
1832 .minor_mask = 0x0f,
1833 .minor_shift = 0,
1834};
1835
Mark A. Greer03feec92012-12-21 10:04:06 -07001836#ifdef CONFIG_OF
Mark A. Greerd20fb182012-12-21 10:04:09 -07001837static struct omap_sham_algs_info omap_sham_algs_info_omap4[] = {
1838 {
1839 .algs_list = algs_sha1_md5,
1840 .size = ARRAY_SIZE(algs_sha1_md5),
1841 },
1842 {
1843 .algs_list = algs_sha224_sha256,
1844 .size = ARRAY_SIZE(algs_sha224_sha256),
1845 },
1846};
1847
Mark A. Greer0d373d62012-12-21 10:04:08 -07001848static const struct omap_sham_pdata omap_sham_pdata_omap4 = {
Mark A. Greerd20fb182012-12-21 10:04:09 -07001849 .algs_info = omap_sham_algs_info_omap4,
1850 .algs_info_size = ARRAY_SIZE(omap_sham_algs_info_omap4),
Mark A. Greer0d373d62012-12-21 10:04:08 -07001851 .flags = BIT(FLAGS_AUTO_XOR),
1852 .digest_size = SHA256_DIGEST_SIZE,
1853 .copy_hash = omap_sham_copy_hash_omap4,
1854 .write_ctrl = omap_sham_write_ctrl_omap4,
1855 .trigger = omap_sham_trigger_omap4,
1856 .poll_irq = omap_sham_poll_irq_omap4,
1857 .intr_hdlr = omap_sham_irq_omap4,
1858 .idigest_ofs = 0x020,
Lokesh Vutlaeaef7e32013-07-26 12:29:14 +05301859 .odigest_ofs = 0x0,
Mark A. Greer0d373d62012-12-21 10:04:08 -07001860 .din_ofs = 0x080,
1861 .digcnt_ofs = 0x040,
1862 .rev_ofs = 0x100,
1863 .mask_ofs = 0x110,
1864 .sysstatus_ofs = 0x114,
Lokesh Vutlaeaef7e32013-07-26 12:29:14 +05301865 .mode_ofs = 0x44,
1866 .length_ofs = 0x48,
Mark A. Greer0d373d62012-12-21 10:04:08 -07001867 .major_mask = 0x0700,
1868 .major_shift = 8,
1869 .minor_mask = 0x003f,
1870 .minor_shift = 0,
1871};
1872
Lokesh Vutla7d7c7042013-07-26 12:29:15 +05301873static struct omap_sham_algs_info omap_sham_algs_info_omap5[] = {
1874 {
1875 .algs_list = algs_sha1_md5,
1876 .size = ARRAY_SIZE(algs_sha1_md5),
1877 },
1878 {
1879 .algs_list = algs_sha224_sha256,
1880 .size = ARRAY_SIZE(algs_sha224_sha256),
1881 },
1882 {
1883 .algs_list = algs_sha384_sha512,
1884 .size = ARRAY_SIZE(algs_sha384_sha512),
1885 },
1886};
1887
1888static const struct omap_sham_pdata omap_sham_pdata_omap5 = {
1889 .algs_info = omap_sham_algs_info_omap5,
1890 .algs_info_size = ARRAY_SIZE(omap_sham_algs_info_omap5),
1891 .flags = BIT(FLAGS_AUTO_XOR),
1892 .digest_size = SHA512_DIGEST_SIZE,
1893 .copy_hash = omap_sham_copy_hash_omap4,
1894 .write_ctrl = omap_sham_write_ctrl_omap4,
1895 .trigger = omap_sham_trigger_omap4,
1896 .poll_irq = omap_sham_poll_irq_omap4,
1897 .intr_hdlr = omap_sham_irq_omap4,
1898 .idigest_ofs = 0x240,
1899 .odigest_ofs = 0x200,
1900 .din_ofs = 0x080,
1901 .digcnt_ofs = 0x280,
1902 .rev_ofs = 0x100,
1903 .mask_ofs = 0x110,
1904 .sysstatus_ofs = 0x114,
1905 .mode_ofs = 0x284,
1906 .length_ofs = 0x288,
1907 .major_mask = 0x0700,
1908 .major_shift = 8,
1909 .minor_mask = 0x003f,
1910 .minor_shift = 0,
1911};
1912
Mark A. Greer03feec92012-12-21 10:04:06 -07001913static const struct of_device_id omap_sham_of_match[] = {
1914 {
1915 .compatible = "ti,omap2-sham",
Mark A. Greer0d373d62012-12-21 10:04:08 -07001916 .data = &omap_sham_pdata_omap2,
1917 },
1918 {
Pali Roháreddca852015-02-26 14:49:53 +01001919 .compatible = "ti,omap3-sham",
1920 .data = &omap_sham_pdata_omap2,
1921 },
1922 {
Mark A. Greer0d373d62012-12-21 10:04:08 -07001923 .compatible = "ti,omap4-sham",
1924 .data = &omap_sham_pdata_omap4,
Mark A. Greer03feec92012-12-21 10:04:06 -07001925 },
Lokesh Vutla7d7c7042013-07-26 12:29:15 +05301926 {
1927 .compatible = "ti,omap5-sham",
1928 .data = &omap_sham_pdata_omap5,
1929 },
Mark A. Greer03feec92012-12-21 10:04:06 -07001930 {},
1931};
1932MODULE_DEVICE_TABLE(of, omap_sham_of_match);
1933
1934static int omap_sham_get_res_of(struct omap_sham_dev *dd,
1935 struct device *dev, struct resource *res)
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001936{
Mark A. Greer03feec92012-12-21 10:04:06 -07001937 struct device_node *node = dev->of_node;
Mark A. Greer03feec92012-12-21 10:04:06 -07001938 int err = 0;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001939
Corentin LABBE7d5569312017-09-20 20:42:48 +02001940 dd->pdata = of_device_get_match_data(dev);
1941 if (!dd->pdata) {
Mark A. Greer03feec92012-12-21 10:04:06 -07001942 dev_err(dev, "no compatible OF match\n");
1943 err = -EINVAL;
1944 goto err;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001945 }
Samu Onkalo584db6a2010-09-03 19:20:19 +08001946
Mark A. Greer03feec92012-12-21 10:04:06 -07001947 err = of_address_to_resource(node, 0, res);
1948 if (err < 0) {
1949 dev_err(dev, "can't translate OF node address\n");
1950 err = -EINVAL;
1951 goto err;
1952 }
1953
Thierry Redingf7578492013-09-18 15:24:44 +02001954 dd->irq = irq_of_parse_and_map(node, 0);
Mark A. Greer03feec92012-12-21 10:04:06 -07001955 if (!dd->irq) {
1956 dev_err(dev, "can't translate OF irq value\n");
1957 err = -EINVAL;
1958 goto err;
1959 }
1960
Mark A. Greer03feec92012-12-21 10:04:06 -07001961err:
1962 return err;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001963}
Mark A. Greer03feec92012-12-21 10:04:06 -07001964#else
Mark A. Greerc3c3b322013-01-15 13:53:02 -07001965static const struct of_device_id omap_sham_of_match[] = {
1966 {},
1967};
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001968
Mark A. Greerc3c3b322013-01-15 13:53:02 -07001969static int omap_sham_get_res_of(struct omap_sham_dev *dd,
Mark A. Greer03feec92012-12-21 10:04:06 -07001970 struct device *dev, struct resource *res)
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001971{
Mark A. Greer03feec92012-12-21 10:04:06 -07001972 return -EINVAL;
1973}
1974#endif
1975
1976static int omap_sham_get_res_pdev(struct omap_sham_dev *dd,
1977 struct platform_device *pdev, struct resource *res)
1978{
1979 struct device *dev = &pdev->dev;
1980 struct resource *r;
1981 int err = 0;
1982
1983 /* Get the base address */
1984 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1985 if (!r) {
1986 dev_err(dev, "no MEM resource info\n");
1987 err = -ENODEV;
1988 goto err;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001989 }
Mark A. Greer03feec92012-12-21 10:04:06 -07001990 memcpy(res, r, sizeof(*res));
1991
1992 /* Get the IRQ */
1993 dd->irq = platform_get_irq(pdev, 0);
1994 if (dd->irq < 0) {
1995 dev_err(dev, "no IRQ resource info\n");
1996 err = dd->irq;
1997 goto err;
1998 }
1999
Mark A. Greer0d373d62012-12-21 10:04:08 -07002000 /* Only OMAP2/3 can be non-DT */
2001 dd->pdata = &omap_sham_pdata_omap2;
2002
Mark A. Greer03feec92012-12-21 10:04:06 -07002003err:
2004 return err;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08002005}
2006
Tero Kristoc9af5992018-02-27 15:30:36 +02002007static ssize_t fallback_show(struct device *dev, struct device_attribute *attr,
2008 char *buf)
2009{
2010 struct omap_sham_dev *dd = dev_get_drvdata(dev);
2011
2012 return sprintf(buf, "%d\n", dd->fallback_sz);
2013}
2014
2015static ssize_t fallback_store(struct device *dev, struct device_attribute *attr,
2016 const char *buf, size_t size)
2017{
2018 struct omap_sham_dev *dd = dev_get_drvdata(dev);
2019 ssize_t status;
2020 long value;
2021
2022 status = kstrtol(buf, 0, &value);
2023 if (status)
2024 return status;
2025
2026 /* HW accelerator only works with buffers > 9 */
2027 if (value < 9) {
2028 dev_err(dev, "minimum fallback size 9\n");
2029 return -EINVAL;
2030 }
2031
2032 dd->fallback_sz = value;
2033
2034 return size;
2035}
2036
Tero Kristo62f7c702018-02-27 15:30:37 +02002037static ssize_t queue_len_show(struct device *dev, struct device_attribute *attr,
2038 char *buf)
2039{
2040 struct omap_sham_dev *dd = dev_get_drvdata(dev);
2041
2042 return sprintf(buf, "%d\n", dd->queue.max_qlen);
2043}
2044
2045static ssize_t queue_len_store(struct device *dev,
2046 struct device_attribute *attr, const char *buf,
2047 size_t size)
2048{
2049 struct omap_sham_dev *dd = dev_get_drvdata(dev);
2050 ssize_t status;
2051 long value;
2052 unsigned long flags;
2053
2054 status = kstrtol(buf, 0, &value);
2055 if (status)
2056 return status;
2057
2058 if (value < 1)
2059 return -EINVAL;
2060
2061 /*
2062 * Changing the queue size in fly is safe, if size becomes smaller
2063 * than current size, it will just not accept new entries until
2064 * it has shrank enough.
2065 */
2066 spin_lock_irqsave(&dd->lock, flags);
2067 dd->queue.max_qlen = value;
2068 spin_unlock_irqrestore(&dd->lock, flags);
2069
2070 return size;
2071}
2072
2073static DEVICE_ATTR_RW(queue_len);
Tero Kristoc9af5992018-02-27 15:30:36 +02002074static DEVICE_ATTR_RW(fallback);
2075
2076static struct attribute *omap_sham_attrs[] = {
Tero Kristo62f7c702018-02-27 15:30:37 +02002077 &dev_attr_queue_len.attr,
Tero Kristoc9af5992018-02-27 15:30:36 +02002078 &dev_attr_fallback.attr,
2079 NULL,
2080};
2081
2082static struct attribute_group omap_sham_attr_group = {
2083 .attrs = omap_sham_attrs,
2084};
2085
Greg Kroah-Hartman49cfe4d2012-12-21 13:14:09 -08002086static int omap_sham_probe(struct platform_device *pdev)
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08002087{
2088 struct omap_sham_dev *dd;
2089 struct device *dev = &pdev->dev;
Mark A. Greer03feec92012-12-21 10:04:06 -07002090 struct resource res;
Mark A. Greerdfd061d2012-12-21 10:04:04 -07002091 dma_cap_mask_t mask;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08002092 int err, i, j;
Mark A. Greer0d373d62012-12-21 10:04:08 -07002093 u32 rev;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08002094
Lokesh Vutla7a7e4b72013-07-26 12:29:17 +05302095 dd = devm_kzalloc(dev, sizeof(struct omap_sham_dev), GFP_KERNEL);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08002096 if (dd == NULL) {
2097 dev_err(dev, "unable to alloc data struct.\n");
2098 err = -ENOMEM;
2099 goto data_err;
2100 }
2101 dd->dev = dev;
2102 platform_set_drvdata(pdev, dd);
2103
2104 INIT_LIST_HEAD(&dd->list);
2105 spin_lock_init(&dd->lock);
2106 tasklet_init(&dd->done_task, omap_sham_done_task, (unsigned long)dd);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08002107 crypto_init_queue(&dd->queue, OMAP_SHAM_QUEUE_LENGTH);
2108
Mark A. Greer03feec92012-12-21 10:04:06 -07002109 err = (dev->of_node) ? omap_sham_get_res_of(dd, dev, &res) :
2110 omap_sham_get_res_pdev(dd, pdev, &res);
2111 if (err)
Lokesh Vutla7a7e4b72013-07-26 12:29:17 +05302112 goto data_err;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08002113
Laurent Navet30862282013-05-02 14:00:38 +02002114 dd->io_base = devm_ioremap_resource(dev, &res);
2115 if (IS_ERR(dd->io_base)) {
2116 err = PTR_ERR(dd->io_base);
Lokesh Vutla7a7e4b72013-07-26 12:29:17 +05302117 goto data_err;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08002118 }
Mark A. Greer03feec92012-12-21 10:04:06 -07002119 dd->phys_base = res.start;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08002120
Lokesh Vutla0de9c382013-07-26 12:29:16 +05302121 err = devm_request_irq(dev, dd->irq, dd->pdata->intr_hdlr,
2122 IRQF_TRIGGER_NONE, dev_name(dev), dd);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08002123 if (err) {
Lokesh Vutla0de9c382013-07-26 12:29:16 +05302124 dev_err(dev, "unable to request irq %d, err = %d\n",
2125 dd->irq, err);
Lokesh Vutla7a7e4b72013-07-26 12:29:17 +05302126 goto data_err;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08002127 }
2128
Mark A. Greerdfd061d2012-12-21 10:04:04 -07002129 dma_cap_zero(mask);
2130 dma_cap_set(DMA_SLAVE, mask);
2131
Peter Ujfalusidbe24622016-04-29 16:03:41 +03002132 dd->dma_lch = dma_request_chan(dev, "rx");
2133 if (IS_ERR(dd->dma_lch)) {
2134 err = PTR_ERR(dd->dma_lch);
2135 if (err == -EPROBE_DEFER)
2136 goto data_err;
2137
Lokesh Vutlab8411cc2013-08-20 20:32:34 +05302138 dd->polling_mode = 1;
2139 dev_dbg(dev, "using polling mode instead of dma\n");
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08002140 }
2141
Mark A. Greer0d373d62012-12-21 10:04:08 -07002142 dd->flags |= dd->pdata->flags;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08002143
Tero Kristoe93f7672016-06-22 16:23:34 +03002144 pm_runtime_use_autosuspend(dev);
2145 pm_runtime_set_autosuspend_delay(dev, DEFAULT_AUTOSUSPEND_DELAY);
2146
Tero Kristoc9af5992018-02-27 15:30:36 +02002147 dd->fallback_sz = OMAP_SHA_DMA_THRESHOLD;
2148
Mark A. Greerb359f032012-12-21 10:04:02 -07002149 pm_runtime_enable(dev);
Vutla, Lokeshb0a3d892015-03-31 09:52:24 +05302150 pm_runtime_irq_safe(dev);
Pali Rohár604c3102015-03-08 11:01:01 +01002151
2152 err = pm_runtime_get_sync(dev);
2153 if (err < 0) {
2154 dev_err(dev, "failed to get sync: %d\n", err);
2155 goto err_pm;
2156 }
2157
Mark A. Greer0d373d62012-12-21 10:04:08 -07002158 rev = omap_sham_read(dd, SHA_REG_REV(dd));
2159 pm_runtime_put_sync(&pdev->dev);
Mark A. Greerb359f032012-12-21 10:04:02 -07002160
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08002161 dev_info(dev, "hw accel on OMAP rev %u.%u\n",
Mark A. Greer0d373d62012-12-21 10:04:08 -07002162 (rev & dd->pdata->major_mask) >> dd->pdata->major_shift,
2163 (rev & dd->pdata->minor_mask) >> dd->pdata->minor_shift);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08002164
2165 spin_lock(&sham.lock);
2166 list_add_tail(&dd->list, &sham.dev_list);
2167 spin_unlock(&sham.lock);
2168
Mark A. Greerd20fb182012-12-21 10:04:09 -07002169 for (i = 0; i < dd->pdata->algs_info_size; i++) {
2170 for (j = 0; j < dd->pdata->algs_info[i].size; j++) {
Tero Kristo99a7fff2016-09-19 18:22:12 +03002171 struct ahash_alg *alg;
2172
2173 alg = &dd->pdata->algs_info[i].algs_list[j];
2174 alg->export = omap_sham_export;
2175 alg->import = omap_sham_import;
Tero Kristoa84d3512016-09-19 18:22:18 +03002176 alg->halg.statesize = sizeof(struct omap_sham_reqctx) +
2177 BUFLEN;
Tero Kristo99a7fff2016-09-19 18:22:12 +03002178 err = crypto_register_ahash(alg);
Mark A. Greerd20fb182012-12-21 10:04:09 -07002179 if (err)
2180 goto err_algs;
2181
2182 dd->pdata->algs_info[i].registered++;
2183 }
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08002184 }
2185
Tero Kristoc9af5992018-02-27 15:30:36 +02002186 err = sysfs_create_group(&dev->kobj, &omap_sham_attr_group);
2187 if (err) {
2188 dev_err(dev, "could not create sysfs device attrs\n");
2189 goto err_algs;
2190 }
2191
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08002192 return 0;
2193
2194err_algs:
Mark A. Greerd20fb182012-12-21 10:04:09 -07002195 for (i = dd->pdata->algs_info_size - 1; i >= 0; i--)
2196 for (j = dd->pdata->algs_info[i].registered - 1; j >= 0; j--)
2197 crypto_unregister_ahash(
2198 &dd->pdata->algs_info[i].algs_list[j]);
Pali Rohár604c3102015-03-08 11:01:01 +01002199err_pm:
Mark A. Greerb359f032012-12-21 10:04:02 -07002200 pm_runtime_disable(dev);
Dan Carpenterd462e322016-05-18 13:39:05 +03002201 if (!dd->polling_mode)
Mark A. Greerf13ab862013-11-12 13:12:27 -07002202 dma_release_channel(dd->dma_lch);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08002203data_err:
2204 dev_err(dev, "initialization failed.\n");
2205
2206 return err;
2207}
2208
Greg Kroah-Hartman49cfe4d2012-12-21 13:14:09 -08002209static int omap_sham_remove(struct platform_device *pdev)
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08002210{
Gustavo A. R. Silva0588d852017-07-18 18:03:11 -05002211 struct omap_sham_dev *dd;
Mark A. Greerd20fb182012-12-21 10:04:09 -07002212 int i, j;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08002213
2214 dd = platform_get_drvdata(pdev);
2215 if (!dd)
2216 return -ENODEV;
2217 spin_lock(&sham.lock);
2218 list_del(&dd->list);
2219 spin_unlock(&sham.lock);
Mark A. Greerd20fb182012-12-21 10:04:09 -07002220 for (i = dd->pdata->algs_info_size - 1; i >= 0; i--)
2221 for (j = dd->pdata->algs_info[i].registered - 1; j >= 0; j--)
2222 crypto_unregister_ahash(
2223 &dd->pdata->algs_info[i].algs_list[j]);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08002224 tasklet_kill(&dd->done_task);
Mark A. Greerb359f032012-12-21 10:04:02 -07002225 pm_runtime_disable(&pdev->dev);
Mark A. Greerf13ab862013-11-12 13:12:27 -07002226
Peter Ujfalusidbe24622016-04-29 16:03:41 +03002227 if (!dd->polling_mode)
Mark A. Greerf13ab862013-11-12 13:12:27 -07002228 dma_release_channel(dd->dma_lch);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08002229
2230 return 0;
2231}
2232
Mark A. Greer3b3f4402012-12-21 10:04:03 -07002233#ifdef CONFIG_PM_SLEEP
2234static int omap_sham_suspend(struct device *dev)
2235{
2236 pm_runtime_put_sync(dev);
2237 return 0;
2238}
2239
2240static int omap_sham_resume(struct device *dev)
2241{
Pali Rohár604c3102015-03-08 11:01:01 +01002242 int err = pm_runtime_get_sync(dev);
2243 if (err < 0) {
2244 dev_err(dev, "failed to get sync: %d\n", err);
2245 return err;
2246 }
Mark A. Greer3b3f4402012-12-21 10:04:03 -07002247 return 0;
2248}
2249#endif
2250
Jingoo Hanae12fe22014-02-27 20:33:32 +09002251static SIMPLE_DEV_PM_OPS(omap_sham_pm_ops, omap_sham_suspend, omap_sham_resume);
Mark A. Greer3b3f4402012-12-21 10:04:03 -07002252
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08002253static struct platform_driver omap_sham_driver = {
2254 .probe = omap_sham_probe,
2255 .remove = omap_sham_remove,
2256 .driver = {
2257 .name = "omap-sham",
Mark A. Greer3b3f4402012-12-21 10:04:03 -07002258 .pm = &omap_sham_pm_ops,
Mark A. Greer03feec92012-12-21 10:04:06 -07002259 .of_match_table = omap_sham_of_match,
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08002260 },
2261};
2262
Sachin Kamat02613702013-03-04 15:09:43 +05302263module_platform_driver(omap_sham_driver);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08002264
2265MODULE_DESCRIPTION("OMAP SHA1/MD5 hw acceleration support.");
2266MODULE_LICENSE("GPL v2");
2267MODULE_AUTHOR("Dmitry Kasatkin");
Joni Lapilainen718249d2013-10-26 23:00:41 +02002268MODULE_ALIAS("platform:omap-sham");