blob: 8e7e1582ef695eac7e3184490b092235f69251aa [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
820 if (!req)
821 return 0;
822
823 bs = get_block_size(rctx);
824
825 if (update)
826 nbytes = req->nbytes;
827 else
828 nbytes = 0;
829
830 rctx->total = nbytes + rctx->bufcnt;
831
832 if (!rctx->total)
833 return 0;
834
835 if (nbytes && (!IS_ALIGNED(rctx->bufcnt, bs))) {
836 int len = bs - rctx->bufcnt % bs;
837
838 if (len > nbytes)
839 len = nbytes;
840 scatterwalk_map_and_copy(rctx->buffer + rctx->bufcnt, req->src,
841 0, len, 0);
842 rctx->bufcnt += len;
843 nbytes -= len;
844 rctx->offset = len;
845 }
846
847 if (rctx->bufcnt)
848 memcpy(rctx->dd->xmit_buf, rctx->buffer, rctx->bufcnt);
849
850 ret = omap_sham_align_sgs(req->src, nbytes, bs, final, rctx);
851 if (ret)
852 return ret;
853
854 xmit_len = rctx->total;
855
856 if (!IS_ALIGNED(xmit_len, bs)) {
857 if (final)
858 xmit_len = DIV_ROUND_UP(xmit_len, bs) * bs;
859 else
860 xmit_len = xmit_len / bs * bs;
Tero Kristo898d86a2017-05-24 10:35:33 +0300861 } else if (!final) {
862 xmit_len -= bs;
Tero Kristof19de1b2016-09-19 18:22:15 +0300863 }
864
865 hash_later = rctx->total - xmit_len;
866 if (hash_later < 0)
867 hash_later = 0;
868
869 if (rctx->bufcnt && nbytes) {
870 /* have data from previous operation and current */
871 sg_init_table(rctx->sgl, 2);
872 sg_set_buf(rctx->sgl, rctx->dd->xmit_buf, rctx->bufcnt);
873
874 sg_chain(rctx->sgl, 2, req->src);
875
876 rctx->sg = rctx->sgl;
877
878 rctx->sg_len++;
879 } else if (rctx->bufcnt) {
880 /* have buffered data only */
881 sg_init_table(rctx->sgl, 1);
882 sg_set_buf(rctx->sgl, rctx->dd->xmit_buf, xmit_len);
883
884 rctx->sg = rctx->sgl;
885
886 rctx->sg_len = 1;
887 }
888
889 if (hash_later) {
Tero Kristo5d78d572017-05-24 10:35:32 +0300890 int offset = 0;
891
892 if (hash_later > req->nbytes) {
Tero Kristof19de1b2016-09-19 18:22:15 +0300893 memcpy(rctx->buffer, rctx->buffer + xmit_len,
Tero Kristo5d78d572017-05-24 10:35:32 +0300894 hash_later - req->nbytes);
895 offset = hash_later - req->nbytes;
Tero Kristof19de1b2016-09-19 18:22:15 +0300896 }
Tero Kristo5d78d572017-05-24 10:35:32 +0300897
898 if (req->nbytes) {
899 scatterwalk_map_and_copy(rctx->buffer + offset,
900 req->src,
901 offset + req->nbytes -
902 hash_later, hash_later, 0);
903 }
904
Tero Kristof19de1b2016-09-19 18:22:15 +0300905 rctx->bufcnt = hash_later;
906 } else {
907 rctx->bufcnt = 0;
908 }
909
910 if (!final)
911 rctx->total = xmit_len;
912
913 return 0;
914}
915
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800916static int omap_sham_update_dma_stop(struct omap_sham_dev *dd)
917{
918 struct omap_sham_reqctx *ctx = ahash_request_ctx(dd->req);
919
Tero Kristo8043bb12016-09-19 18:22:17 +0300920 dma_unmap_sg(dd->dev, ctx->sg, ctx->sg_len, DMA_TO_DEVICE);
Mark A. Greerdfd061d2012-12-21 10:04:04 -0700921
Tero Kristo8043bb12016-09-19 18:22:17 +0300922 clear_bit(FLAGS_DMA_ACTIVE, &dd->flags);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800923
924 return 0;
925}
926
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800927static int omap_sham_init(struct ahash_request *req)
928{
929 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
930 struct omap_sham_ctx *tctx = crypto_ahash_ctx(tfm);
931 struct omap_sham_reqctx *ctx = ahash_request_ctx(req);
932 struct omap_sham_dev *dd = NULL, *tmp;
Lokesh Vutlaeaef7e32013-07-26 12:29:14 +0530933 int bs = 0;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800934
935 spin_lock_bh(&sham.lock);
936 if (!tctx->dd) {
937 list_for_each_entry(tmp, &sham.dev_list, list) {
938 dd = tmp;
939 break;
940 }
941 tctx->dd = dd;
942 } else {
943 dd = tctx->dd;
944 }
945 spin_unlock_bh(&sham.lock);
946
947 ctx->dd = dd;
948
949 ctx->flags = 0;
950
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800951 dev_dbg(dd->dev, "init: digest size: %d\n",
952 crypto_ahash_digestsize(tfm));
953
Mark A. Greer0d373d62012-12-21 10:04:08 -0700954 switch (crypto_ahash_digestsize(tfm)) {
955 case MD5_DIGEST_SIZE:
956 ctx->flags |= FLAGS_MODE_MD5;
Lokesh Vutlaeaef7e32013-07-26 12:29:14 +0530957 bs = SHA1_BLOCK_SIZE;
Mark A. Greer0d373d62012-12-21 10:04:08 -0700958 break;
959 case SHA1_DIGEST_SIZE:
960 ctx->flags |= FLAGS_MODE_SHA1;
Lokesh Vutlaeaef7e32013-07-26 12:29:14 +0530961 bs = SHA1_BLOCK_SIZE;
Mark A. Greer0d373d62012-12-21 10:04:08 -0700962 break;
Mark A. Greerd20fb182012-12-21 10:04:09 -0700963 case SHA224_DIGEST_SIZE:
964 ctx->flags |= FLAGS_MODE_SHA224;
Lokesh Vutlaeaef7e32013-07-26 12:29:14 +0530965 bs = SHA224_BLOCK_SIZE;
Mark A. Greerd20fb182012-12-21 10:04:09 -0700966 break;
967 case SHA256_DIGEST_SIZE:
968 ctx->flags |= FLAGS_MODE_SHA256;
Lokesh Vutlaeaef7e32013-07-26 12:29:14 +0530969 bs = SHA256_BLOCK_SIZE;
970 break;
971 case SHA384_DIGEST_SIZE:
972 ctx->flags |= FLAGS_MODE_SHA384;
973 bs = SHA384_BLOCK_SIZE;
974 break;
975 case SHA512_DIGEST_SIZE:
976 ctx->flags |= FLAGS_MODE_SHA512;
977 bs = SHA512_BLOCK_SIZE;
Mark A. Greerd20fb182012-12-21 10:04:09 -0700978 break;
Mark A. Greer0d373d62012-12-21 10:04:08 -0700979 }
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800980
981 ctx->bufcnt = 0;
982 ctx->digcnt = 0;
Tero Kristo8043bb12016-09-19 18:22:17 +0300983 ctx->total = 0;
984 ctx->offset = 0;
Dmitry Kasatkin798eed5d2010-11-19 16:04:26 +0200985 ctx->buflen = BUFLEN;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800986
Dmitry Kasatkinea1fd222011-06-02 21:10:05 +0300987 if (tctx->flags & BIT(FLAGS_HMAC)) {
Mark A. Greer0d373d62012-12-21 10:04:08 -0700988 if (!test_bit(FLAGS_AUTO_XOR, &dd->flags)) {
989 struct omap_sham_hmac_ctx *bctx = tctx->base;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800990
Lokesh Vutlaeaef7e32013-07-26 12:29:14 +0530991 memcpy(ctx->buffer, bctx->ipad, bs);
992 ctx->bufcnt = bs;
Mark A. Greer0d373d62012-12-21 10:04:08 -0700993 }
994
Dmitry Kasatkinea1fd222011-06-02 21:10:05 +0300995 ctx->flags |= BIT(FLAGS_HMAC);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800996 }
997
998 return 0;
999
1000}
1001
1002static int omap_sham_update_req(struct omap_sham_dev *dd)
1003{
1004 struct ahash_request *req = dd->req;
1005 struct omap_sham_reqctx *ctx = ahash_request_ctx(req);
1006 int err;
Tero Kristo8043bb12016-09-19 18:22:17 +03001007 bool final = ctx->flags & BIT(FLAGS_FINUP);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001008
1009 dev_dbg(dd->dev, "update_req: total: %u, digcnt: %d, finup: %d\n",
Dmitry Kasatkinea1fd222011-06-02 21:10:05 +03001010 ctx->total, ctx->digcnt, (ctx->flags & BIT(FLAGS_FINUP)) != 0);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001011
Tero Kristo8043bb12016-09-19 18:22:17 +03001012 if (ctx->total < get_block_size(ctx) ||
Tero Kristoc9af5992018-02-27 15:30:36 +02001013 ctx->total < dd->fallback_sz)
Tero Kristo8043bb12016-09-19 18:22:17 +03001014 ctx->flags |= BIT(FLAGS_CPU);
1015
Dmitry Kasatkinea1fd222011-06-02 21:10:05 +03001016 if (ctx->flags & BIT(FLAGS_CPU))
Tero Kristo8043bb12016-09-19 18:22:17 +03001017 err = omap_sham_xmit_cpu(dd, ctx->total, final);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001018 else
Tero Kristo8043bb12016-09-19 18:22:17 +03001019 err = omap_sham_xmit_dma(dd, ctx->total, final);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001020
1021 /* wait for dma completion before can take more data */
1022 dev_dbg(dd->dev, "update: err: %d, digcnt: %d\n", err, ctx->digcnt);
1023
1024 return err;
1025}
1026
1027static int omap_sham_final_req(struct omap_sham_dev *dd)
1028{
1029 struct ahash_request *req = dd->req;
1030 struct omap_sham_reqctx *ctx = ahash_request_ctx(req);
1031 int err = 0, use_dma = 1;
1032
Tero Kristo8043bb12016-09-19 18:22:17 +03001033 if ((ctx->total <= get_block_size(ctx)) || dd->polling_mode)
Lokesh Vutlab8411cc2013-08-20 20:32:34 +05301034 /*
1035 * faster to handle last block with cpu or
1036 * use cpu when dma is not present.
1037 */
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001038 use_dma = 0;
1039
1040 if (use_dma)
Tero Kristo8043bb12016-09-19 18:22:17 +03001041 err = omap_sham_xmit_dma(dd, ctx->total, 1);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001042 else
Tero Kristo8043bb12016-09-19 18:22:17 +03001043 err = omap_sham_xmit_cpu(dd, ctx->total, 1);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001044
1045 ctx->bufcnt = 0;
1046
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001047 dev_dbg(dd->dev, "final_req: err: %d\n", err);
1048
1049 return err;
1050}
1051
Dmitry Kasatkinbf362752011-04-20 13:34:58 +03001052static int omap_sham_finish_hmac(struct ahash_request *req)
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001053{
1054 struct omap_sham_ctx *tctx = crypto_tfm_ctx(req->base.tfm);
1055 struct omap_sham_hmac_ctx *bctx = tctx->base;
1056 int bs = crypto_shash_blocksize(bctx->shash);
1057 int ds = crypto_shash_digestsize(bctx->shash);
Behan Webster7bc53c32014-04-04 18:18:00 -03001058 SHASH_DESC_ON_STACK(shash, bctx->shash);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001059
Behan Webster7bc53c32014-04-04 18:18:00 -03001060 shash->tfm = bctx->shash;
1061 shash->flags = 0; /* not CRYPTO_TFM_REQ_MAY_SLEEP */
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001062
Behan Webster7bc53c32014-04-04 18:18:00 -03001063 return crypto_shash_init(shash) ?:
1064 crypto_shash_update(shash, bctx->opad, bs) ?:
1065 crypto_shash_finup(shash, req->result, ds, req->result);
Dmitry Kasatkinbf362752011-04-20 13:34:58 +03001066}
1067
1068static int omap_sham_finish(struct ahash_request *req)
1069{
1070 struct omap_sham_reqctx *ctx = ahash_request_ctx(req);
1071 struct omap_sham_dev *dd = ctx->dd;
1072 int err = 0;
1073
1074 if (ctx->digcnt) {
1075 omap_sham_copy_ready_hash(req);
Mark A. Greer0d373d62012-12-21 10:04:08 -07001076 if ((ctx->flags & BIT(FLAGS_HMAC)) &&
1077 !test_bit(FLAGS_AUTO_XOR, &dd->flags))
Dmitry Kasatkinbf362752011-04-20 13:34:58 +03001078 err = omap_sham_finish_hmac(req);
1079 }
1080
1081 dev_dbg(dd->dev, "digcnt: %d, bufcnt: %d\n", ctx->digcnt, ctx->bufcnt);
1082
1083 return err;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001084}
1085
1086static void omap_sham_finish_req(struct ahash_request *req, int err)
1087{
1088 struct omap_sham_reqctx *ctx = ahash_request_ctx(req);
Dmitry Kasatkin798eed5d2010-11-19 16:04:26 +02001089 struct omap_sham_dev *dd = ctx->dd;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001090
Tero Kristo8043bb12016-09-19 18:22:17 +03001091 if (test_bit(FLAGS_SGS_COPIED, &dd->flags))
1092 free_pages((unsigned long)sg_virt(ctx->sg),
1093 get_order(ctx->sg->length));
1094
1095 if (test_bit(FLAGS_SGS_ALLOCED, &dd->flags))
1096 kfree(ctx->sg);
1097
1098 ctx->sg = NULL;
1099
1100 dd->flags &= ~(BIT(FLAGS_SGS_ALLOCED) | BIT(FLAGS_SGS_COPIED));
1101
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001102 if (!err) {
Mark A. Greer0d373d62012-12-21 10:04:08 -07001103 dd->pdata->copy_hash(req, 1);
Dmitry Kasatkined3ea9a82011-06-02 21:10:07 +03001104 if (test_bit(FLAGS_FINAL, &dd->flags))
Dmitry Kasatkinbf362752011-04-20 13:34:58 +03001105 err = omap_sham_finish(req);
Dmitry Kasatkin3e133c82010-11-19 16:04:24 +02001106 } else {
Dmitry Kasatkinea1fd222011-06-02 21:10:05 +03001107 ctx->flags |= BIT(FLAGS_ERROR);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001108 }
1109
Dmitry Kasatkin0efd4d82011-06-02 21:10:12 +03001110 /* atomic operation is not needed here */
1111 dd->flags &= ~(BIT(FLAGS_BUSY) | BIT(FLAGS_FINAL) | BIT(FLAGS_CPU) |
1112 BIT(FLAGS_DMA_READY) | BIT(FLAGS_OUTPUT_READY));
Mark A. Greerb359f032012-12-21 10:04:02 -07001113
Tero Kristoe93f7672016-06-22 16:23:34 +03001114 pm_runtime_mark_last_busy(dd->dev);
1115 pm_runtime_put_autosuspend(dd->dev);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001116
1117 if (req->base.complete)
1118 req->base.complete(&req->base, err);
1119}
1120
Dmitry Kasatkina5d87232010-11-19 16:04:25 +02001121static int omap_sham_handle_queue(struct omap_sham_dev *dd,
1122 struct ahash_request *req)
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001123{
Dmitry Kasatkin6c39d112010-12-29 21:52:04 +11001124 struct crypto_async_request *async_req, *backlog;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001125 struct omap_sham_reqctx *ctx;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001126 unsigned long flags;
Dmitry Kasatkina5d87232010-11-19 16:04:25 +02001127 int err = 0, ret = 0;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001128
Tero Kristo4e7813a2016-08-04 13:28:36 +03001129retry:
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001130 spin_lock_irqsave(&dd->lock, flags);
Dmitry Kasatkina5d87232010-11-19 16:04:25 +02001131 if (req)
1132 ret = ahash_enqueue_request(&dd->queue, req);
Dmitry Kasatkina929cbe2011-06-02 21:10:06 +03001133 if (test_bit(FLAGS_BUSY, &dd->flags)) {
Dmitry Kasatkina5d87232010-11-19 16:04:25 +02001134 spin_unlock_irqrestore(&dd->lock, flags);
1135 return ret;
1136 }
Dmitry Kasatkin6c39d112010-12-29 21:52:04 +11001137 backlog = crypto_get_backlog(&dd->queue);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001138 async_req = crypto_dequeue_request(&dd->queue);
Dmitry Kasatkin6c39d112010-12-29 21:52:04 +11001139 if (async_req)
Dmitry Kasatkina929cbe2011-06-02 21:10:06 +03001140 set_bit(FLAGS_BUSY, &dd->flags);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001141 spin_unlock_irqrestore(&dd->lock, flags);
1142
1143 if (!async_req)
Dmitry Kasatkina5d87232010-11-19 16:04:25 +02001144 return ret;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001145
1146 if (backlog)
1147 backlog->complete(backlog, -EINPROGRESS);
1148
1149 req = ahash_request_cast(async_req);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001150 dd->req = req;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001151 ctx = ahash_request_ctx(req);
1152
Tero Kristo8043bb12016-09-19 18:22:17 +03001153 err = omap_sham_prepare_request(req, ctx->op == OP_UPDATE);
Tero Kristo898d86a2017-05-24 10:35:33 +03001154 if (err || !ctx->total)
Tero Kristof19de1b2016-09-19 18:22:15 +03001155 goto err1;
1156
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001157 dev_dbg(dd->dev, "handling new req, op: %lu, nbytes: %d\n",
1158 ctx->op, req->nbytes);
1159
Dmitry Kasatkin798eed5d2010-11-19 16:04:26 +02001160 err = omap_sham_hw_init(dd);
1161 if (err)
1162 goto err1;
1163
Dmitry Kasatkin798eed5d2010-11-19 16:04:26 +02001164 if (ctx->digcnt)
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001165 /* request has changed - restore hash */
Mark A. Greer0d373d62012-12-21 10:04:08 -07001166 dd->pdata->copy_hash(req, 0);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001167
1168 if (ctx->op == OP_UPDATE) {
1169 err = omap_sham_update_req(dd);
Dmitry Kasatkinea1fd222011-06-02 21:10:05 +03001170 if (err != -EINPROGRESS && (ctx->flags & BIT(FLAGS_FINUP)))
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001171 /* no final() after finup() */
1172 err = omap_sham_final_req(dd);
1173 } else if (ctx->op == OP_FINAL) {
1174 err = omap_sham_final_req(dd);
1175 }
Dmitry Kasatkin798eed5d2010-11-19 16:04:26 +02001176err1:
Tero Kristo4e7813a2016-08-04 13:28:36 +03001177 dev_dbg(dd->dev, "exit, err: %d\n", err);
1178
1179 if (err != -EINPROGRESS) {
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001180 /* done_task will not finish it, so do it here */
1181 omap_sham_finish_req(req, err);
Tero Kristo4e7813a2016-08-04 13:28:36 +03001182 req = NULL;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001183
Tero Kristo4e7813a2016-08-04 13:28:36 +03001184 /*
1185 * Execute next request immediately if there is anything
1186 * in queue.
1187 */
1188 goto retry;
1189 }
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001190
Dmitry Kasatkina5d87232010-11-19 16:04:25 +02001191 return ret;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001192}
1193
1194static int omap_sham_enqueue(struct ahash_request *req, unsigned int op)
1195{
1196 struct omap_sham_reqctx *ctx = ahash_request_ctx(req);
1197 struct omap_sham_ctx *tctx = crypto_tfm_ctx(req->base.tfm);
1198 struct omap_sham_dev *dd = tctx->dd;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001199
1200 ctx->op = op;
1201
Dmitry Kasatkina5d87232010-11-19 16:04:25 +02001202 return omap_sham_handle_queue(dd, req);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001203}
1204
1205static int omap_sham_update(struct ahash_request *req)
1206{
1207 struct omap_sham_reqctx *ctx = ahash_request_ctx(req);
Lokesh Vutlab8411cc2013-08-20 20:32:34 +05301208 struct omap_sham_dev *dd = ctx->dd;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001209
1210 if (!req->nbytes)
1211 return 0;
1212
Tero Kristo5d78d572017-05-24 10:35:32 +03001213 if (ctx->bufcnt + req->nbytes <= ctx->buflen) {
Tero Kristo8043bb12016-09-19 18:22:17 +03001214 scatterwalk_map_and_copy(ctx->buffer + ctx->bufcnt, req->src,
1215 0, req->nbytes, 0);
1216 ctx->bufcnt += req->nbytes;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001217 return 0;
1218 }
1219
Lokesh Vutlaacef7b02013-12-18 19:03:33 +05301220 if (dd->polling_mode)
1221 ctx->flags |= BIT(FLAGS_CPU);
1222
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001223 return omap_sham_enqueue(req, OP_UPDATE);
1224}
1225
Behan Webster7bc53c32014-04-04 18:18:00 -03001226static int omap_sham_shash_digest(struct crypto_shash *tfm, u32 flags,
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001227 const u8 *data, unsigned int len, u8 *out)
1228{
Behan Webster7bc53c32014-04-04 18:18:00 -03001229 SHASH_DESC_ON_STACK(shash, tfm);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001230
Behan Webster7bc53c32014-04-04 18:18:00 -03001231 shash->tfm = tfm;
1232 shash->flags = flags & CRYPTO_TFM_REQ_MAY_SLEEP;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001233
Behan Webster7bc53c32014-04-04 18:18:00 -03001234 return crypto_shash_digest(shash, data, len, out);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001235}
1236
1237static int omap_sham_final_shash(struct ahash_request *req)
1238{
1239 struct omap_sham_ctx *tctx = crypto_tfm_ctx(req->base.tfm);
1240 struct omap_sham_reqctx *ctx = ahash_request_ctx(req);
Tero Kristocb8d5c82016-08-04 13:28:40 +03001241 int offset = 0;
1242
1243 /*
1244 * If we are running HMAC on limited hardware support, skip
1245 * the ipad in the beginning of the buffer if we are going for
1246 * software fallback algorithm.
1247 */
1248 if (test_bit(FLAGS_HMAC, &ctx->flags) &&
1249 !test_bit(FLAGS_AUTO_XOR, &ctx->dd->flags))
1250 offset = get_block_size(ctx);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001251
1252 return omap_sham_shash_digest(tctx->fallback, req->base.flags,
Tero Kristocb8d5c82016-08-04 13:28:40 +03001253 ctx->buffer + offset,
1254 ctx->bufcnt - offset, req->result);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001255}
1256
1257static int omap_sham_final(struct ahash_request *req)
1258{
1259 struct omap_sham_reqctx *ctx = ahash_request_ctx(req);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001260
Dmitry Kasatkinea1fd222011-06-02 21:10:05 +03001261 ctx->flags |= BIT(FLAGS_FINUP);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001262
Dmitry Kasatkinea1fd222011-06-02 21:10:05 +03001263 if (ctx->flags & BIT(FLAGS_ERROR))
Dmitry Kasatkinbf362752011-04-20 13:34:58 +03001264 return 0; /* uncompleted hash is not needed */
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001265
Bin Liu85e06872016-06-22 16:23:37 +03001266 /*
1267 * OMAP HW accel works only with buffers >= 9.
1268 * HMAC is always >= 9 because ipad == block size.
Tero Kristoc9af5992018-02-27 15:30:36 +02001269 * If buffersize is less than fallback_sz, we use fallback
Tero Kristo2c5bd1e2016-09-19 18:22:16 +03001270 * SW encoding, as using DMA + HW in this case doesn't provide
1271 * any benefit.
Bin Liu85e06872016-06-22 16:23:37 +03001272 */
Tero Kristoc9af5992018-02-27 15:30:36 +02001273 if (!ctx->digcnt && ctx->bufcnt < ctx->dd->fallback_sz)
Dmitry Kasatkinbf362752011-04-20 13:34:58 +03001274 return omap_sham_final_shash(req);
1275 else if (ctx->bufcnt)
1276 return omap_sham_enqueue(req, OP_FINAL);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001277
Dmitry Kasatkinbf362752011-04-20 13:34:58 +03001278 /* copy ready hash (+ finalize hmac) */
1279 return omap_sham_finish(req);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001280}
1281
1282static int omap_sham_finup(struct ahash_request *req)
1283{
1284 struct omap_sham_reqctx *ctx = ahash_request_ctx(req);
1285 int err1, err2;
1286
Dmitry Kasatkinea1fd222011-06-02 21:10:05 +03001287 ctx->flags |= BIT(FLAGS_FINUP);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001288
1289 err1 = omap_sham_update(req);
Markku Kylanpaa455e3382011-04-20 13:34:55 +03001290 if (err1 == -EINPROGRESS || err1 == -EBUSY)
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001291 return err1;
1292 /*
1293 * final() has to be always called to cleanup resources
1294 * even if udpate() failed, except EINPROGRESS
1295 */
1296 err2 = omap_sham_final(req);
1297
1298 return err1 ?: err2;
1299}
1300
1301static int omap_sham_digest(struct ahash_request *req)
1302{
1303 return omap_sham_init(req) ?: omap_sham_finup(req);
1304}
1305
1306static int omap_sham_setkey(struct crypto_ahash *tfm, const u8 *key,
1307 unsigned int keylen)
1308{
1309 struct omap_sham_ctx *tctx = crypto_ahash_ctx(tfm);
1310 struct omap_sham_hmac_ctx *bctx = tctx->base;
1311 int bs = crypto_shash_blocksize(bctx->shash);
1312 int ds = crypto_shash_digestsize(bctx->shash);
Mark A. Greer0d373d62012-12-21 10:04:08 -07001313 struct omap_sham_dev *dd = NULL, *tmp;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001314 int err, i;
Mark A. Greer0d373d62012-12-21 10:04:08 -07001315
1316 spin_lock_bh(&sham.lock);
1317 if (!tctx->dd) {
1318 list_for_each_entry(tmp, &sham.dev_list, list) {
1319 dd = tmp;
1320 break;
1321 }
1322 tctx->dd = dd;
1323 } else {
1324 dd = tctx->dd;
1325 }
1326 spin_unlock_bh(&sham.lock);
1327
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001328 err = crypto_shash_setkey(tctx->fallback, key, keylen);
1329 if (err)
1330 return err;
1331
1332 if (keylen > bs) {
1333 err = omap_sham_shash_digest(bctx->shash,
1334 crypto_shash_get_flags(bctx->shash),
1335 key, keylen, bctx->ipad);
1336 if (err)
1337 return err;
1338 keylen = ds;
1339 } else {
1340 memcpy(bctx->ipad, key, keylen);
1341 }
1342
1343 memset(bctx->ipad + keylen, 0, bs - keylen);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001344
Mark A. Greer0d373d62012-12-21 10:04:08 -07001345 if (!test_bit(FLAGS_AUTO_XOR, &dd->flags)) {
1346 memcpy(bctx->opad, bctx->ipad, bs);
1347
1348 for (i = 0; i < bs; i++) {
Corentin LABBEebd401e2017-05-19 08:53:28 +02001349 bctx->ipad[i] ^= HMAC_IPAD_VALUE;
1350 bctx->opad[i] ^= HMAC_OPAD_VALUE;
Mark A. Greer0d373d62012-12-21 10:04:08 -07001351 }
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001352 }
1353
1354 return err;
1355}
1356
1357static int omap_sham_cra_init_alg(struct crypto_tfm *tfm, const char *alg_base)
1358{
1359 struct omap_sham_ctx *tctx = crypto_tfm_ctx(tfm);
1360 const char *alg_name = crypto_tfm_alg_name(tfm);
1361
1362 /* Allocate a fallback and abort if it failed. */
1363 tctx->fallback = crypto_alloc_shash(alg_name, 0,
1364 CRYPTO_ALG_NEED_FALLBACK);
1365 if (IS_ERR(tctx->fallback)) {
1366 pr_err("omap-sham: fallback driver '%s' "
1367 "could not be loaded.\n", alg_name);
1368 return PTR_ERR(tctx->fallback);
1369 }
1370
1371 crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
Dmitry Kasatkin798eed5d2010-11-19 16:04:26 +02001372 sizeof(struct omap_sham_reqctx) + BUFLEN);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001373
1374 if (alg_base) {
1375 struct omap_sham_hmac_ctx *bctx = tctx->base;
Dmitry Kasatkinea1fd222011-06-02 21:10:05 +03001376 tctx->flags |= BIT(FLAGS_HMAC);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001377 bctx->shash = crypto_alloc_shash(alg_base, 0,
1378 CRYPTO_ALG_NEED_FALLBACK);
1379 if (IS_ERR(bctx->shash)) {
1380 pr_err("omap-sham: base driver '%s' "
1381 "could not be loaded.\n", alg_base);
1382 crypto_free_shash(tctx->fallback);
1383 return PTR_ERR(bctx->shash);
1384 }
1385
1386 }
1387
1388 return 0;
1389}
1390
1391static int omap_sham_cra_init(struct crypto_tfm *tfm)
1392{
1393 return omap_sham_cra_init_alg(tfm, NULL);
1394}
1395
1396static int omap_sham_cra_sha1_init(struct crypto_tfm *tfm)
1397{
1398 return omap_sham_cra_init_alg(tfm, "sha1");
1399}
1400
Mark A. Greerd20fb182012-12-21 10:04:09 -07001401static int omap_sham_cra_sha224_init(struct crypto_tfm *tfm)
1402{
1403 return omap_sham_cra_init_alg(tfm, "sha224");
1404}
1405
1406static int omap_sham_cra_sha256_init(struct crypto_tfm *tfm)
1407{
1408 return omap_sham_cra_init_alg(tfm, "sha256");
1409}
1410
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001411static int omap_sham_cra_md5_init(struct crypto_tfm *tfm)
1412{
1413 return omap_sham_cra_init_alg(tfm, "md5");
1414}
1415
Lokesh Vutlaeaef7e32013-07-26 12:29:14 +05301416static int omap_sham_cra_sha384_init(struct crypto_tfm *tfm)
1417{
1418 return omap_sham_cra_init_alg(tfm, "sha384");
1419}
1420
1421static int omap_sham_cra_sha512_init(struct crypto_tfm *tfm)
1422{
1423 return omap_sham_cra_init_alg(tfm, "sha512");
1424}
1425
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001426static void omap_sham_cra_exit(struct crypto_tfm *tfm)
1427{
1428 struct omap_sham_ctx *tctx = crypto_tfm_ctx(tfm);
1429
1430 crypto_free_shash(tctx->fallback);
1431 tctx->fallback = NULL;
1432
Dmitry Kasatkinea1fd222011-06-02 21:10:05 +03001433 if (tctx->flags & BIT(FLAGS_HMAC)) {
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001434 struct omap_sham_hmac_ctx *bctx = tctx->base;
1435 crypto_free_shash(bctx->shash);
1436 }
1437}
1438
Tero Kristo99a7fff2016-09-19 18:22:12 +03001439static int omap_sham_export(struct ahash_request *req, void *out)
1440{
Tero Kristoa84d3512016-09-19 18:22:18 +03001441 struct omap_sham_reqctx *rctx = ahash_request_ctx(req);
1442
1443 memcpy(out, rctx, sizeof(*rctx) + rctx->bufcnt);
1444
1445 return 0;
Tero Kristo99a7fff2016-09-19 18:22:12 +03001446}
1447
1448static int omap_sham_import(struct ahash_request *req, const void *in)
1449{
Tero Kristoa84d3512016-09-19 18:22:18 +03001450 struct omap_sham_reqctx *rctx = ahash_request_ctx(req);
1451 const struct omap_sham_reqctx *ctx_in = in;
1452
1453 memcpy(rctx, in, sizeof(*rctx) + ctx_in->bufcnt);
1454
1455 return 0;
Tero Kristo99a7fff2016-09-19 18:22:12 +03001456}
1457
Mark A. Greerd20fb182012-12-21 10:04:09 -07001458static struct ahash_alg algs_sha1_md5[] = {
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001459{
1460 .init = omap_sham_init,
1461 .update = omap_sham_update,
1462 .final = omap_sham_final,
1463 .finup = omap_sham_finup,
1464 .digest = omap_sham_digest,
1465 .halg.digestsize = SHA1_DIGEST_SIZE,
1466 .halg.base = {
1467 .cra_name = "sha1",
1468 .cra_driver_name = "omap-sha1",
Bin Liueb354782016-06-30 14:04:11 -05001469 .cra_priority = 400,
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001470 .cra_flags = CRYPTO_ALG_TYPE_AHASH |
Nikos Mavrogiannopoulosd912bb72011-11-01 13:39:56 +01001471 CRYPTO_ALG_KERN_DRIVER_ONLY |
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001472 CRYPTO_ALG_ASYNC |
1473 CRYPTO_ALG_NEED_FALLBACK,
1474 .cra_blocksize = SHA1_BLOCK_SIZE,
1475 .cra_ctxsize = sizeof(struct omap_sham_ctx),
Tero Kristo744e6862016-09-19 18:22:13 +03001476 .cra_alignmask = OMAP_ALIGN_MASK,
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001477 .cra_module = THIS_MODULE,
1478 .cra_init = omap_sham_cra_init,
1479 .cra_exit = omap_sham_cra_exit,
1480 }
1481},
1482{
1483 .init = omap_sham_init,
1484 .update = omap_sham_update,
1485 .final = omap_sham_final,
1486 .finup = omap_sham_finup,
1487 .digest = omap_sham_digest,
1488 .halg.digestsize = MD5_DIGEST_SIZE,
1489 .halg.base = {
1490 .cra_name = "md5",
1491 .cra_driver_name = "omap-md5",
Bin Liueb354782016-06-30 14:04:11 -05001492 .cra_priority = 400,
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001493 .cra_flags = CRYPTO_ALG_TYPE_AHASH |
Nikos Mavrogiannopoulosd912bb72011-11-01 13:39:56 +01001494 CRYPTO_ALG_KERN_DRIVER_ONLY |
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001495 CRYPTO_ALG_ASYNC |
1496 CRYPTO_ALG_NEED_FALLBACK,
1497 .cra_blocksize = SHA1_BLOCK_SIZE,
1498 .cra_ctxsize = sizeof(struct omap_sham_ctx),
Dmitry Kasatkin798eed5d2010-11-19 16:04:26 +02001499 .cra_alignmask = OMAP_ALIGN_MASK,
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001500 .cra_module = THIS_MODULE,
1501 .cra_init = omap_sham_cra_init,
1502 .cra_exit = omap_sham_cra_exit,
1503 }
1504},
1505{
1506 .init = omap_sham_init,
1507 .update = omap_sham_update,
1508 .final = omap_sham_final,
1509 .finup = omap_sham_finup,
1510 .digest = omap_sham_digest,
1511 .setkey = omap_sham_setkey,
1512 .halg.digestsize = SHA1_DIGEST_SIZE,
1513 .halg.base = {
1514 .cra_name = "hmac(sha1)",
1515 .cra_driver_name = "omap-hmac-sha1",
Bin Liueb354782016-06-30 14:04:11 -05001516 .cra_priority = 400,
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001517 .cra_flags = CRYPTO_ALG_TYPE_AHASH |
Nikos Mavrogiannopoulosd912bb72011-11-01 13:39:56 +01001518 CRYPTO_ALG_KERN_DRIVER_ONLY |
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001519 CRYPTO_ALG_ASYNC |
1520 CRYPTO_ALG_NEED_FALLBACK,
1521 .cra_blocksize = SHA1_BLOCK_SIZE,
1522 .cra_ctxsize = sizeof(struct omap_sham_ctx) +
1523 sizeof(struct omap_sham_hmac_ctx),
Dmitry Kasatkin798eed5d2010-11-19 16:04:26 +02001524 .cra_alignmask = OMAP_ALIGN_MASK,
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001525 .cra_module = THIS_MODULE,
1526 .cra_init = omap_sham_cra_sha1_init,
1527 .cra_exit = omap_sham_cra_exit,
1528 }
1529},
1530{
1531 .init = omap_sham_init,
1532 .update = omap_sham_update,
1533 .final = omap_sham_final,
1534 .finup = omap_sham_finup,
1535 .digest = omap_sham_digest,
1536 .setkey = omap_sham_setkey,
1537 .halg.digestsize = MD5_DIGEST_SIZE,
1538 .halg.base = {
1539 .cra_name = "hmac(md5)",
1540 .cra_driver_name = "omap-hmac-md5",
Bin Liueb354782016-06-30 14:04:11 -05001541 .cra_priority = 400,
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001542 .cra_flags = CRYPTO_ALG_TYPE_AHASH |
Nikos Mavrogiannopoulosd912bb72011-11-01 13:39:56 +01001543 CRYPTO_ALG_KERN_DRIVER_ONLY |
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001544 CRYPTO_ALG_ASYNC |
1545 CRYPTO_ALG_NEED_FALLBACK,
1546 .cra_blocksize = SHA1_BLOCK_SIZE,
1547 .cra_ctxsize = sizeof(struct omap_sham_ctx) +
1548 sizeof(struct omap_sham_hmac_ctx),
Dmitry Kasatkin798eed5d2010-11-19 16:04:26 +02001549 .cra_alignmask = OMAP_ALIGN_MASK,
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001550 .cra_module = THIS_MODULE,
1551 .cra_init = omap_sham_cra_md5_init,
1552 .cra_exit = omap_sham_cra_exit,
1553 }
1554}
1555};
1556
Mark A. Greerd20fb182012-12-21 10:04:09 -07001557/* OMAP4 has some algs in addition to what OMAP2 has */
1558static struct ahash_alg algs_sha224_sha256[] = {
1559{
1560 .init = omap_sham_init,
1561 .update = omap_sham_update,
1562 .final = omap_sham_final,
1563 .finup = omap_sham_finup,
1564 .digest = omap_sham_digest,
1565 .halg.digestsize = SHA224_DIGEST_SIZE,
1566 .halg.base = {
1567 .cra_name = "sha224",
1568 .cra_driver_name = "omap-sha224",
Bin Liueb354782016-06-30 14:04:11 -05001569 .cra_priority = 400,
Mark A. Greerd20fb182012-12-21 10:04:09 -07001570 .cra_flags = CRYPTO_ALG_TYPE_AHASH |
1571 CRYPTO_ALG_ASYNC |
1572 CRYPTO_ALG_NEED_FALLBACK,
1573 .cra_blocksize = SHA224_BLOCK_SIZE,
1574 .cra_ctxsize = sizeof(struct omap_sham_ctx),
Tero Kristo744e6862016-09-19 18:22:13 +03001575 .cra_alignmask = OMAP_ALIGN_MASK,
Mark A. Greerd20fb182012-12-21 10:04:09 -07001576 .cra_module = THIS_MODULE,
1577 .cra_init = omap_sham_cra_init,
1578 .cra_exit = omap_sham_cra_exit,
1579 }
1580},
1581{
1582 .init = omap_sham_init,
1583 .update = omap_sham_update,
1584 .final = omap_sham_final,
1585 .finup = omap_sham_finup,
1586 .digest = omap_sham_digest,
1587 .halg.digestsize = SHA256_DIGEST_SIZE,
1588 .halg.base = {
1589 .cra_name = "sha256",
1590 .cra_driver_name = "omap-sha256",
Bin Liueb354782016-06-30 14:04:11 -05001591 .cra_priority = 400,
Mark A. Greerd20fb182012-12-21 10:04:09 -07001592 .cra_flags = CRYPTO_ALG_TYPE_AHASH |
1593 CRYPTO_ALG_ASYNC |
1594 CRYPTO_ALG_NEED_FALLBACK,
1595 .cra_blocksize = SHA256_BLOCK_SIZE,
1596 .cra_ctxsize = sizeof(struct omap_sham_ctx),
Tero Kristo744e6862016-09-19 18:22:13 +03001597 .cra_alignmask = OMAP_ALIGN_MASK,
Mark A. Greerd20fb182012-12-21 10:04:09 -07001598 .cra_module = THIS_MODULE,
1599 .cra_init = omap_sham_cra_init,
1600 .cra_exit = omap_sham_cra_exit,
1601 }
1602},
1603{
1604 .init = omap_sham_init,
1605 .update = omap_sham_update,
1606 .final = omap_sham_final,
1607 .finup = omap_sham_finup,
1608 .digest = omap_sham_digest,
1609 .setkey = omap_sham_setkey,
1610 .halg.digestsize = SHA224_DIGEST_SIZE,
1611 .halg.base = {
1612 .cra_name = "hmac(sha224)",
1613 .cra_driver_name = "omap-hmac-sha224",
Bin Liueb354782016-06-30 14:04:11 -05001614 .cra_priority = 400,
Mark A. Greerd20fb182012-12-21 10:04:09 -07001615 .cra_flags = CRYPTO_ALG_TYPE_AHASH |
1616 CRYPTO_ALG_ASYNC |
1617 CRYPTO_ALG_NEED_FALLBACK,
1618 .cra_blocksize = SHA224_BLOCK_SIZE,
1619 .cra_ctxsize = sizeof(struct omap_sham_ctx) +
1620 sizeof(struct omap_sham_hmac_ctx),
1621 .cra_alignmask = OMAP_ALIGN_MASK,
1622 .cra_module = THIS_MODULE,
1623 .cra_init = omap_sham_cra_sha224_init,
1624 .cra_exit = omap_sham_cra_exit,
1625 }
1626},
1627{
1628 .init = omap_sham_init,
1629 .update = omap_sham_update,
1630 .final = omap_sham_final,
1631 .finup = omap_sham_finup,
1632 .digest = omap_sham_digest,
1633 .setkey = omap_sham_setkey,
1634 .halg.digestsize = SHA256_DIGEST_SIZE,
1635 .halg.base = {
1636 .cra_name = "hmac(sha256)",
1637 .cra_driver_name = "omap-hmac-sha256",
Bin Liueb354782016-06-30 14:04:11 -05001638 .cra_priority = 400,
Mark A. Greerd20fb182012-12-21 10:04:09 -07001639 .cra_flags = CRYPTO_ALG_TYPE_AHASH |
1640 CRYPTO_ALG_ASYNC |
1641 CRYPTO_ALG_NEED_FALLBACK,
1642 .cra_blocksize = SHA256_BLOCK_SIZE,
1643 .cra_ctxsize = sizeof(struct omap_sham_ctx) +
1644 sizeof(struct omap_sham_hmac_ctx),
1645 .cra_alignmask = OMAP_ALIGN_MASK,
1646 .cra_module = THIS_MODULE,
1647 .cra_init = omap_sham_cra_sha256_init,
1648 .cra_exit = omap_sham_cra_exit,
1649 }
1650},
1651};
1652
Lokesh Vutlaeaef7e32013-07-26 12:29:14 +05301653static struct ahash_alg algs_sha384_sha512[] = {
1654{
1655 .init = omap_sham_init,
1656 .update = omap_sham_update,
1657 .final = omap_sham_final,
1658 .finup = omap_sham_finup,
1659 .digest = omap_sham_digest,
1660 .halg.digestsize = SHA384_DIGEST_SIZE,
1661 .halg.base = {
1662 .cra_name = "sha384",
1663 .cra_driver_name = "omap-sha384",
Bin Liueb354782016-06-30 14:04:11 -05001664 .cra_priority = 400,
Lokesh Vutlaeaef7e32013-07-26 12:29:14 +05301665 .cra_flags = CRYPTO_ALG_TYPE_AHASH |
1666 CRYPTO_ALG_ASYNC |
1667 CRYPTO_ALG_NEED_FALLBACK,
1668 .cra_blocksize = SHA384_BLOCK_SIZE,
1669 .cra_ctxsize = sizeof(struct omap_sham_ctx),
Tero Kristo744e6862016-09-19 18:22:13 +03001670 .cra_alignmask = OMAP_ALIGN_MASK,
Lokesh Vutlaeaef7e32013-07-26 12:29:14 +05301671 .cra_module = THIS_MODULE,
1672 .cra_init = omap_sham_cra_init,
1673 .cra_exit = omap_sham_cra_exit,
1674 }
1675},
1676{
1677 .init = omap_sham_init,
1678 .update = omap_sham_update,
1679 .final = omap_sham_final,
1680 .finup = omap_sham_finup,
1681 .digest = omap_sham_digest,
1682 .halg.digestsize = SHA512_DIGEST_SIZE,
1683 .halg.base = {
1684 .cra_name = "sha512",
1685 .cra_driver_name = "omap-sha512",
Bin Liueb354782016-06-30 14:04:11 -05001686 .cra_priority = 400,
Lokesh Vutlaeaef7e32013-07-26 12:29:14 +05301687 .cra_flags = CRYPTO_ALG_TYPE_AHASH |
1688 CRYPTO_ALG_ASYNC |
1689 CRYPTO_ALG_NEED_FALLBACK,
1690 .cra_blocksize = SHA512_BLOCK_SIZE,
1691 .cra_ctxsize = sizeof(struct omap_sham_ctx),
Tero Kristo744e6862016-09-19 18:22:13 +03001692 .cra_alignmask = OMAP_ALIGN_MASK,
Lokesh Vutlaeaef7e32013-07-26 12:29:14 +05301693 .cra_module = THIS_MODULE,
1694 .cra_init = omap_sham_cra_init,
1695 .cra_exit = omap_sham_cra_exit,
1696 }
1697},
1698{
1699 .init = omap_sham_init,
1700 .update = omap_sham_update,
1701 .final = omap_sham_final,
1702 .finup = omap_sham_finup,
1703 .digest = omap_sham_digest,
1704 .setkey = omap_sham_setkey,
1705 .halg.digestsize = SHA384_DIGEST_SIZE,
1706 .halg.base = {
1707 .cra_name = "hmac(sha384)",
1708 .cra_driver_name = "omap-hmac-sha384",
Bin Liueb354782016-06-30 14:04:11 -05001709 .cra_priority = 400,
Lokesh Vutlaeaef7e32013-07-26 12:29:14 +05301710 .cra_flags = CRYPTO_ALG_TYPE_AHASH |
1711 CRYPTO_ALG_ASYNC |
1712 CRYPTO_ALG_NEED_FALLBACK,
1713 .cra_blocksize = SHA384_BLOCK_SIZE,
1714 .cra_ctxsize = sizeof(struct omap_sham_ctx) +
1715 sizeof(struct omap_sham_hmac_ctx),
1716 .cra_alignmask = OMAP_ALIGN_MASK,
1717 .cra_module = THIS_MODULE,
1718 .cra_init = omap_sham_cra_sha384_init,
1719 .cra_exit = omap_sham_cra_exit,
1720 }
1721},
1722{
1723 .init = omap_sham_init,
1724 .update = omap_sham_update,
1725 .final = omap_sham_final,
1726 .finup = omap_sham_finup,
1727 .digest = omap_sham_digest,
1728 .setkey = omap_sham_setkey,
1729 .halg.digestsize = SHA512_DIGEST_SIZE,
1730 .halg.base = {
1731 .cra_name = "hmac(sha512)",
1732 .cra_driver_name = "omap-hmac-sha512",
Bin Liueb354782016-06-30 14:04:11 -05001733 .cra_priority = 400,
Lokesh Vutlaeaef7e32013-07-26 12:29:14 +05301734 .cra_flags = CRYPTO_ALG_TYPE_AHASH |
1735 CRYPTO_ALG_ASYNC |
1736 CRYPTO_ALG_NEED_FALLBACK,
1737 .cra_blocksize = SHA512_BLOCK_SIZE,
1738 .cra_ctxsize = sizeof(struct omap_sham_ctx) +
1739 sizeof(struct omap_sham_hmac_ctx),
1740 .cra_alignmask = OMAP_ALIGN_MASK,
1741 .cra_module = THIS_MODULE,
1742 .cra_init = omap_sham_cra_sha512_init,
1743 .cra_exit = omap_sham_cra_exit,
1744 }
1745},
1746};
1747
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001748static void omap_sham_done_task(unsigned long data)
1749{
1750 struct omap_sham_dev *dd = (struct omap_sham_dev *)data;
Dmitry Kasatkin6c63db82011-06-02 21:10:10 +03001751 int err = 0;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001752
Dmitry Kasatkin6cb3ffe2011-06-02 21:10:09 +03001753 if (!test_bit(FLAGS_BUSY, &dd->flags)) {
1754 omap_sham_handle_queue(dd, NULL);
1755 return;
1756 }
1757
Dmitry Kasatkin6c63db82011-06-02 21:10:10 +03001758 if (test_bit(FLAGS_CPU, &dd->flags)) {
Tero Kristo8043bb12016-09-19 18:22:17 +03001759 if (test_and_clear_bit(FLAGS_OUTPUT_READY, &dd->flags))
1760 goto finish;
Dmitry Kasatkin6c63db82011-06-02 21:10:10 +03001761 } else if (test_bit(FLAGS_DMA_READY, &dd->flags)) {
1762 if (test_and_clear_bit(FLAGS_DMA_ACTIVE, &dd->flags)) {
1763 omap_sham_update_dma_stop(dd);
1764 if (dd->err) {
1765 err = dd->err;
1766 goto finish;
1767 }
1768 }
1769 if (test_and_clear_bit(FLAGS_OUTPUT_READY, &dd->flags)) {
1770 /* hash or semi-hash ready */
1771 clear_bit(FLAGS_DMA_READY, &dd->flags);
Dmitry Kasatkin6c63db82011-06-02 21:10:10 +03001772 goto finish;
1773 }
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001774 }
1775
Dmitry Kasatkin6c63db82011-06-02 21:10:10 +03001776 return;
Dmitry Kasatkin3e133c82010-11-19 16:04:24 +02001777
Dmitry Kasatkin6c63db82011-06-02 21:10:10 +03001778finish:
1779 dev_dbg(dd->dev, "update done: err: %d\n", err);
1780 /* finish curent request */
1781 omap_sham_finish_req(dd->req, err);
Tero Kristo4e7813a2016-08-04 13:28:36 +03001782
1783 /* If we are not busy, process next req */
1784 if (!test_bit(FLAGS_BUSY, &dd->flags))
1785 omap_sham_handle_queue(dd, NULL);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001786}
1787
Mark A. Greer0d373d62012-12-21 10:04:08 -07001788static irqreturn_t omap_sham_irq_common(struct omap_sham_dev *dd)
1789{
1790 if (!test_bit(FLAGS_BUSY, &dd->flags)) {
1791 dev_warn(dd->dev, "Interrupt when no active requests.\n");
1792 } else {
1793 set_bit(FLAGS_OUTPUT_READY, &dd->flags);
1794 tasklet_schedule(&dd->done_task);
1795 }
1796
1797 return IRQ_HANDLED;
1798}
1799
1800static irqreturn_t omap_sham_irq_omap2(int irq, void *dev_id)
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001801{
1802 struct omap_sham_dev *dd = dev_id;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001803
Dmitry Kasatkined3ea9a82011-06-02 21:10:07 +03001804 if (unlikely(test_bit(FLAGS_FINAL, &dd->flags)))
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001805 /* final -> allow device to go to power-saving mode */
1806 omap_sham_write_mask(dd, SHA_REG_CTRL, 0, SHA_REG_CTRL_LENGTH);
1807
1808 omap_sham_write_mask(dd, SHA_REG_CTRL, SHA_REG_CTRL_OUTPUT_READY,
1809 SHA_REG_CTRL_OUTPUT_READY);
1810 omap_sham_read(dd, SHA_REG_CTRL);
1811
Mark A. Greer0d373d62012-12-21 10:04:08 -07001812 return omap_sham_irq_common(dd);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001813}
1814
Mark A. Greer0d373d62012-12-21 10:04:08 -07001815static irqreturn_t omap_sham_irq_omap4(int irq, void *dev_id)
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001816{
Mark A. Greer0d373d62012-12-21 10:04:08 -07001817 struct omap_sham_dev *dd = dev_id;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001818
Mark A. Greer0d373d62012-12-21 10:04:08 -07001819 omap_sham_write_mask(dd, SHA_REG_MASK(dd), 0, SHA_REG_MASK_IT_EN);
Dmitry Kasatkin3e133c82010-11-19 16:04:24 +02001820
Mark A. Greer0d373d62012-12-21 10:04:08 -07001821 return omap_sham_irq_common(dd);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001822}
1823
Mark A. Greerd20fb182012-12-21 10:04:09 -07001824static struct omap_sham_algs_info omap_sham_algs_info_omap2[] = {
1825 {
1826 .algs_list = algs_sha1_md5,
1827 .size = ARRAY_SIZE(algs_sha1_md5),
1828 },
1829};
1830
Mark A. Greer0d373d62012-12-21 10:04:08 -07001831static const struct omap_sham_pdata omap_sham_pdata_omap2 = {
Mark A. Greerd20fb182012-12-21 10:04:09 -07001832 .algs_info = omap_sham_algs_info_omap2,
1833 .algs_info_size = ARRAY_SIZE(omap_sham_algs_info_omap2),
Mark A. Greer0d373d62012-12-21 10:04:08 -07001834 .flags = BIT(FLAGS_BE32_SHA1),
1835 .digest_size = SHA1_DIGEST_SIZE,
1836 .copy_hash = omap_sham_copy_hash_omap2,
1837 .write_ctrl = omap_sham_write_ctrl_omap2,
1838 .trigger = omap_sham_trigger_omap2,
1839 .poll_irq = omap_sham_poll_irq_omap2,
1840 .intr_hdlr = omap_sham_irq_omap2,
1841 .idigest_ofs = 0x00,
1842 .din_ofs = 0x1c,
1843 .digcnt_ofs = 0x14,
1844 .rev_ofs = 0x5c,
1845 .mask_ofs = 0x60,
1846 .sysstatus_ofs = 0x64,
1847 .major_mask = 0xf0,
1848 .major_shift = 4,
1849 .minor_mask = 0x0f,
1850 .minor_shift = 0,
1851};
1852
Mark A. Greer03feec92012-12-21 10:04:06 -07001853#ifdef CONFIG_OF
Mark A. Greerd20fb182012-12-21 10:04:09 -07001854static struct omap_sham_algs_info omap_sham_algs_info_omap4[] = {
1855 {
1856 .algs_list = algs_sha1_md5,
1857 .size = ARRAY_SIZE(algs_sha1_md5),
1858 },
1859 {
1860 .algs_list = algs_sha224_sha256,
1861 .size = ARRAY_SIZE(algs_sha224_sha256),
1862 },
1863};
1864
Mark A. Greer0d373d62012-12-21 10:04:08 -07001865static const struct omap_sham_pdata omap_sham_pdata_omap4 = {
Mark A. Greerd20fb182012-12-21 10:04:09 -07001866 .algs_info = omap_sham_algs_info_omap4,
1867 .algs_info_size = ARRAY_SIZE(omap_sham_algs_info_omap4),
Mark A. Greer0d373d62012-12-21 10:04:08 -07001868 .flags = BIT(FLAGS_AUTO_XOR),
1869 .digest_size = SHA256_DIGEST_SIZE,
1870 .copy_hash = omap_sham_copy_hash_omap4,
1871 .write_ctrl = omap_sham_write_ctrl_omap4,
1872 .trigger = omap_sham_trigger_omap4,
1873 .poll_irq = omap_sham_poll_irq_omap4,
1874 .intr_hdlr = omap_sham_irq_omap4,
1875 .idigest_ofs = 0x020,
Lokesh Vutlaeaef7e32013-07-26 12:29:14 +05301876 .odigest_ofs = 0x0,
Mark A. Greer0d373d62012-12-21 10:04:08 -07001877 .din_ofs = 0x080,
1878 .digcnt_ofs = 0x040,
1879 .rev_ofs = 0x100,
1880 .mask_ofs = 0x110,
1881 .sysstatus_ofs = 0x114,
Lokesh Vutlaeaef7e32013-07-26 12:29:14 +05301882 .mode_ofs = 0x44,
1883 .length_ofs = 0x48,
Mark A. Greer0d373d62012-12-21 10:04:08 -07001884 .major_mask = 0x0700,
1885 .major_shift = 8,
1886 .minor_mask = 0x003f,
1887 .minor_shift = 0,
1888};
1889
Lokesh Vutla7d7c7042013-07-26 12:29:15 +05301890static struct omap_sham_algs_info omap_sham_algs_info_omap5[] = {
1891 {
1892 .algs_list = algs_sha1_md5,
1893 .size = ARRAY_SIZE(algs_sha1_md5),
1894 },
1895 {
1896 .algs_list = algs_sha224_sha256,
1897 .size = ARRAY_SIZE(algs_sha224_sha256),
1898 },
1899 {
1900 .algs_list = algs_sha384_sha512,
1901 .size = ARRAY_SIZE(algs_sha384_sha512),
1902 },
1903};
1904
1905static const struct omap_sham_pdata omap_sham_pdata_omap5 = {
1906 .algs_info = omap_sham_algs_info_omap5,
1907 .algs_info_size = ARRAY_SIZE(omap_sham_algs_info_omap5),
1908 .flags = BIT(FLAGS_AUTO_XOR),
1909 .digest_size = SHA512_DIGEST_SIZE,
1910 .copy_hash = omap_sham_copy_hash_omap4,
1911 .write_ctrl = omap_sham_write_ctrl_omap4,
1912 .trigger = omap_sham_trigger_omap4,
1913 .poll_irq = omap_sham_poll_irq_omap4,
1914 .intr_hdlr = omap_sham_irq_omap4,
1915 .idigest_ofs = 0x240,
1916 .odigest_ofs = 0x200,
1917 .din_ofs = 0x080,
1918 .digcnt_ofs = 0x280,
1919 .rev_ofs = 0x100,
1920 .mask_ofs = 0x110,
1921 .sysstatus_ofs = 0x114,
1922 .mode_ofs = 0x284,
1923 .length_ofs = 0x288,
1924 .major_mask = 0x0700,
1925 .major_shift = 8,
1926 .minor_mask = 0x003f,
1927 .minor_shift = 0,
1928};
1929
Mark A. Greer03feec92012-12-21 10:04:06 -07001930static const struct of_device_id omap_sham_of_match[] = {
1931 {
1932 .compatible = "ti,omap2-sham",
Mark A. Greer0d373d62012-12-21 10:04:08 -07001933 .data = &omap_sham_pdata_omap2,
1934 },
1935 {
Pali Roháreddca852015-02-26 14:49:53 +01001936 .compatible = "ti,omap3-sham",
1937 .data = &omap_sham_pdata_omap2,
1938 },
1939 {
Mark A. Greer0d373d62012-12-21 10:04:08 -07001940 .compatible = "ti,omap4-sham",
1941 .data = &omap_sham_pdata_omap4,
Mark A. Greer03feec92012-12-21 10:04:06 -07001942 },
Lokesh Vutla7d7c7042013-07-26 12:29:15 +05301943 {
1944 .compatible = "ti,omap5-sham",
1945 .data = &omap_sham_pdata_omap5,
1946 },
Mark A. Greer03feec92012-12-21 10:04:06 -07001947 {},
1948};
1949MODULE_DEVICE_TABLE(of, omap_sham_of_match);
1950
1951static int omap_sham_get_res_of(struct omap_sham_dev *dd,
1952 struct device *dev, struct resource *res)
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001953{
Mark A. Greer03feec92012-12-21 10:04:06 -07001954 struct device_node *node = dev->of_node;
Mark A. Greer03feec92012-12-21 10:04:06 -07001955 int err = 0;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001956
Corentin LABBE7d5569312017-09-20 20:42:48 +02001957 dd->pdata = of_device_get_match_data(dev);
1958 if (!dd->pdata) {
Mark A. Greer03feec92012-12-21 10:04:06 -07001959 dev_err(dev, "no compatible OF match\n");
1960 err = -EINVAL;
1961 goto err;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001962 }
Samu Onkalo584db6a2010-09-03 19:20:19 +08001963
Mark A. Greer03feec92012-12-21 10:04:06 -07001964 err = of_address_to_resource(node, 0, res);
1965 if (err < 0) {
1966 dev_err(dev, "can't translate OF node address\n");
1967 err = -EINVAL;
1968 goto err;
1969 }
1970
Thierry Redingf7578492013-09-18 15:24:44 +02001971 dd->irq = irq_of_parse_and_map(node, 0);
Mark A. Greer03feec92012-12-21 10:04:06 -07001972 if (!dd->irq) {
1973 dev_err(dev, "can't translate OF irq value\n");
1974 err = -EINVAL;
1975 goto err;
1976 }
1977
Mark A. Greer03feec92012-12-21 10:04:06 -07001978err:
1979 return err;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001980}
Mark A. Greer03feec92012-12-21 10:04:06 -07001981#else
Mark A. Greerc3c3b322013-01-15 13:53:02 -07001982static const struct of_device_id omap_sham_of_match[] = {
1983 {},
1984};
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001985
Mark A. Greerc3c3b322013-01-15 13:53:02 -07001986static int omap_sham_get_res_of(struct omap_sham_dev *dd,
Mark A. Greer03feec92012-12-21 10:04:06 -07001987 struct device *dev, struct resource *res)
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001988{
Mark A. Greer03feec92012-12-21 10:04:06 -07001989 return -EINVAL;
1990}
1991#endif
1992
1993static int omap_sham_get_res_pdev(struct omap_sham_dev *dd,
1994 struct platform_device *pdev, struct resource *res)
1995{
1996 struct device *dev = &pdev->dev;
1997 struct resource *r;
1998 int err = 0;
1999
2000 /* Get the base address */
2001 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2002 if (!r) {
2003 dev_err(dev, "no MEM resource info\n");
2004 err = -ENODEV;
2005 goto err;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08002006 }
Mark A. Greer03feec92012-12-21 10:04:06 -07002007 memcpy(res, r, sizeof(*res));
2008
2009 /* Get the IRQ */
2010 dd->irq = platform_get_irq(pdev, 0);
2011 if (dd->irq < 0) {
2012 dev_err(dev, "no IRQ resource info\n");
2013 err = dd->irq;
2014 goto err;
2015 }
2016
Mark A. Greer0d373d62012-12-21 10:04:08 -07002017 /* Only OMAP2/3 can be non-DT */
2018 dd->pdata = &omap_sham_pdata_omap2;
2019
Mark A. Greer03feec92012-12-21 10:04:06 -07002020err:
2021 return err;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08002022}
2023
Tero Kristoc9af5992018-02-27 15:30:36 +02002024static ssize_t fallback_show(struct device *dev, struct device_attribute *attr,
2025 char *buf)
2026{
2027 struct omap_sham_dev *dd = dev_get_drvdata(dev);
2028
2029 return sprintf(buf, "%d\n", dd->fallback_sz);
2030}
2031
2032static ssize_t fallback_store(struct device *dev, struct device_attribute *attr,
2033 const char *buf, size_t size)
2034{
2035 struct omap_sham_dev *dd = dev_get_drvdata(dev);
2036 ssize_t status;
2037 long value;
2038
2039 status = kstrtol(buf, 0, &value);
2040 if (status)
2041 return status;
2042
2043 /* HW accelerator only works with buffers > 9 */
2044 if (value < 9) {
2045 dev_err(dev, "minimum fallback size 9\n");
2046 return -EINVAL;
2047 }
2048
2049 dd->fallback_sz = value;
2050
2051 return size;
2052}
2053
Tero Kristo62f7c702018-02-27 15:30:37 +02002054static ssize_t queue_len_show(struct device *dev, struct device_attribute *attr,
2055 char *buf)
2056{
2057 struct omap_sham_dev *dd = dev_get_drvdata(dev);
2058
2059 return sprintf(buf, "%d\n", dd->queue.max_qlen);
2060}
2061
2062static ssize_t queue_len_store(struct device *dev,
2063 struct device_attribute *attr, const char *buf,
2064 size_t size)
2065{
2066 struct omap_sham_dev *dd = dev_get_drvdata(dev);
2067 ssize_t status;
2068 long value;
2069 unsigned long flags;
2070
2071 status = kstrtol(buf, 0, &value);
2072 if (status)
2073 return status;
2074
2075 if (value < 1)
2076 return -EINVAL;
2077
2078 /*
2079 * Changing the queue size in fly is safe, if size becomes smaller
2080 * than current size, it will just not accept new entries until
2081 * it has shrank enough.
2082 */
2083 spin_lock_irqsave(&dd->lock, flags);
2084 dd->queue.max_qlen = value;
2085 spin_unlock_irqrestore(&dd->lock, flags);
2086
2087 return size;
2088}
2089
2090static DEVICE_ATTR_RW(queue_len);
Tero Kristoc9af5992018-02-27 15:30:36 +02002091static DEVICE_ATTR_RW(fallback);
2092
2093static struct attribute *omap_sham_attrs[] = {
Tero Kristo62f7c702018-02-27 15:30:37 +02002094 &dev_attr_queue_len.attr,
Tero Kristoc9af5992018-02-27 15:30:36 +02002095 &dev_attr_fallback.attr,
2096 NULL,
2097};
2098
2099static struct attribute_group omap_sham_attr_group = {
2100 .attrs = omap_sham_attrs,
2101};
2102
Greg Kroah-Hartman49cfe4d2012-12-21 13:14:09 -08002103static int omap_sham_probe(struct platform_device *pdev)
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08002104{
2105 struct omap_sham_dev *dd;
2106 struct device *dev = &pdev->dev;
Mark A. Greer03feec92012-12-21 10:04:06 -07002107 struct resource res;
Mark A. Greerdfd061d2012-12-21 10:04:04 -07002108 dma_cap_mask_t mask;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08002109 int err, i, j;
Mark A. Greer0d373d62012-12-21 10:04:08 -07002110 u32 rev;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08002111
Lokesh Vutla7a7e4b72013-07-26 12:29:17 +05302112 dd = devm_kzalloc(dev, sizeof(struct omap_sham_dev), GFP_KERNEL);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08002113 if (dd == NULL) {
2114 dev_err(dev, "unable to alloc data struct.\n");
2115 err = -ENOMEM;
2116 goto data_err;
2117 }
2118 dd->dev = dev;
2119 platform_set_drvdata(pdev, dd);
2120
2121 INIT_LIST_HEAD(&dd->list);
2122 spin_lock_init(&dd->lock);
2123 tasklet_init(&dd->done_task, omap_sham_done_task, (unsigned long)dd);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08002124 crypto_init_queue(&dd->queue, OMAP_SHAM_QUEUE_LENGTH);
2125
Mark A. Greer03feec92012-12-21 10:04:06 -07002126 err = (dev->of_node) ? omap_sham_get_res_of(dd, dev, &res) :
2127 omap_sham_get_res_pdev(dd, pdev, &res);
2128 if (err)
Lokesh Vutla7a7e4b72013-07-26 12:29:17 +05302129 goto data_err;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08002130
Laurent Navet30862282013-05-02 14:00:38 +02002131 dd->io_base = devm_ioremap_resource(dev, &res);
2132 if (IS_ERR(dd->io_base)) {
2133 err = PTR_ERR(dd->io_base);
Lokesh Vutla7a7e4b72013-07-26 12:29:17 +05302134 goto data_err;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08002135 }
Mark A. Greer03feec92012-12-21 10:04:06 -07002136 dd->phys_base = res.start;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08002137
Lokesh Vutla0de9c382013-07-26 12:29:16 +05302138 err = devm_request_irq(dev, dd->irq, dd->pdata->intr_hdlr,
2139 IRQF_TRIGGER_NONE, dev_name(dev), dd);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08002140 if (err) {
Lokesh Vutla0de9c382013-07-26 12:29:16 +05302141 dev_err(dev, "unable to request irq %d, err = %d\n",
2142 dd->irq, err);
Lokesh Vutla7a7e4b72013-07-26 12:29:17 +05302143 goto data_err;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08002144 }
2145
Mark A. Greerdfd061d2012-12-21 10:04:04 -07002146 dma_cap_zero(mask);
2147 dma_cap_set(DMA_SLAVE, mask);
2148
Peter Ujfalusidbe24622016-04-29 16:03:41 +03002149 dd->dma_lch = dma_request_chan(dev, "rx");
2150 if (IS_ERR(dd->dma_lch)) {
2151 err = PTR_ERR(dd->dma_lch);
2152 if (err == -EPROBE_DEFER)
2153 goto data_err;
2154
Lokesh Vutlab8411cc2013-08-20 20:32:34 +05302155 dd->polling_mode = 1;
2156 dev_dbg(dev, "using polling mode instead of dma\n");
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08002157 }
2158
Mark A. Greer0d373d62012-12-21 10:04:08 -07002159 dd->flags |= dd->pdata->flags;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08002160
Tero Kristoe93f7672016-06-22 16:23:34 +03002161 pm_runtime_use_autosuspend(dev);
2162 pm_runtime_set_autosuspend_delay(dev, DEFAULT_AUTOSUSPEND_DELAY);
2163
Tero Kristoc9af5992018-02-27 15:30:36 +02002164 dd->fallback_sz = OMAP_SHA_DMA_THRESHOLD;
2165
Mark A. Greerb359f032012-12-21 10:04:02 -07002166 pm_runtime_enable(dev);
Vutla, Lokeshb0a3d892015-03-31 09:52:24 +05302167 pm_runtime_irq_safe(dev);
Pali Rohár604c3102015-03-08 11:01:01 +01002168
2169 err = pm_runtime_get_sync(dev);
2170 if (err < 0) {
2171 dev_err(dev, "failed to get sync: %d\n", err);
2172 goto err_pm;
2173 }
2174
Mark A. Greer0d373d62012-12-21 10:04:08 -07002175 rev = omap_sham_read(dd, SHA_REG_REV(dd));
2176 pm_runtime_put_sync(&pdev->dev);
Mark A. Greerb359f032012-12-21 10:04:02 -07002177
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08002178 dev_info(dev, "hw accel on OMAP rev %u.%u\n",
Mark A. Greer0d373d62012-12-21 10:04:08 -07002179 (rev & dd->pdata->major_mask) >> dd->pdata->major_shift,
2180 (rev & dd->pdata->minor_mask) >> dd->pdata->minor_shift);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08002181
2182 spin_lock(&sham.lock);
2183 list_add_tail(&dd->list, &sham.dev_list);
2184 spin_unlock(&sham.lock);
2185
Mark A. Greerd20fb182012-12-21 10:04:09 -07002186 for (i = 0; i < dd->pdata->algs_info_size; i++) {
2187 for (j = 0; j < dd->pdata->algs_info[i].size; j++) {
Tero Kristo99a7fff2016-09-19 18:22:12 +03002188 struct ahash_alg *alg;
2189
2190 alg = &dd->pdata->algs_info[i].algs_list[j];
2191 alg->export = omap_sham_export;
2192 alg->import = omap_sham_import;
Tero Kristoa84d3512016-09-19 18:22:18 +03002193 alg->halg.statesize = sizeof(struct omap_sham_reqctx) +
2194 BUFLEN;
Tero Kristo99a7fff2016-09-19 18:22:12 +03002195 err = crypto_register_ahash(alg);
Mark A. Greerd20fb182012-12-21 10:04:09 -07002196 if (err)
2197 goto err_algs;
2198
2199 dd->pdata->algs_info[i].registered++;
2200 }
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08002201 }
2202
Tero Kristoc9af5992018-02-27 15:30:36 +02002203 err = sysfs_create_group(&dev->kobj, &omap_sham_attr_group);
2204 if (err) {
2205 dev_err(dev, "could not create sysfs device attrs\n");
2206 goto err_algs;
2207 }
2208
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08002209 return 0;
2210
2211err_algs:
Mark A. Greerd20fb182012-12-21 10:04:09 -07002212 for (i = dd->pdata->algs_info_size - 1; i >= 0; i--)
2213 for (j = dd->pdata->algs_info[i].registered - 1; j >= 0; j--)
2214 crypto_unregister_ahash(
2215 &dd->pdata->algs_info[i].algs_list[j]);
Pali Rohár604c3102015-03-08 11:01:01 +01002216err_pm:
Mark A. Greerb359f032012-12-21 10:04:02 -07002217 pm_runtime_disable(dev);
Dan Carpenterd462e322016-05-18 13:39:05 +03002218 if (!dd->polling_mode)
Mark A. Greerf13ab862013-11-12 13:12:27 -07002219 dma_release_channel(dd->dma_lch);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08002220data_err:
2221 dev_err(dev, "initialization failed.\n");
2222
2223 return err;
2224}
2225
Greg Kroah-Hartman49cfe4d2012-12-21 13:14:09 -08002226static int omap_sham_remove(struct platform_device *pdev)
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08002227{
Gustavo A. R. Silva0588d852017-07-18 18:03:11 -05002228 struct omap_sham_dev *dd;
Mark A. Greerd20fb182012-12-21 10:04:09 -07002229 int i, j;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08002230
2231 dd = platform_get_drvdata(pdev);
2232 if (!dd)
2233 return -ENODEV;
2234 spin_lock(&sham.lock);
2235 list_del(&dd->list);
2236 spin_unlock(&sham.lock);
Mark A. Greerd20fb182012-12-21 10:04:09 -07002237 for (i = dd->pdata->algs_info_size - 1; i >= 0; i--)
2238 for (j = dd->pdata->algs_info[i].registered - 1; j >= 0; j--)
2239 crypto_unregister_ahash(
2240 &dd->pdata->algs_info[i].algs_list[j]);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08002241 tasklet_kill(&dd->done_task);
Mark A. Greerb359f032012-12-21 10:04:02 -07002242 pm_runtime_disable(&pdev->dev);
Mark A. Greerf13ab862013-11-12 13:12:27 -07002243
Peter Ujfalusidbe24622016-04-29 16:03:41 +03002244 if (!dd->polling_mode)
Mark A. Greerf13ab862013-11-12 13:12:27 -07002245 dma_release_channel(dd->dma_lch);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08002246
2247 return 0;
2248}
2249
Mark A. Greer3b3f4402012-12-21 10:04:03 -07002250#ifdef CONFIG_PM_SLEEP
2251static int omap_sham_suspend(struct device *dev)
2252{
2253 pm_runtime_put_sync(dev);
2254 return 0;
2255}
2256
2257static int omap_sham_resume(struct device *dev)
2258{
Pali Rohár604c3102015-03-08 11:01:01 +01002259 int err = pm_runtime_get_sync(dev);
2260 if (err < 0) {
2261 dev_err(dev, "failed to get sync: %d\n", err);
2262 return err;
2263 }
Mark A. Greer3b3f4402012-12-21 10:04:03 -07002264 return 0;
2265}
2266#endif
2267
Jingoo Hanae12fe22014-02-27 20:33:32 +09002268static SIMPLE_DEV_PM_OPS(omap_sham_pm_ops, omap_sham_suspend, omap_sham_resume);
Mark A. Greer3b3f4402012-12-21 10:04:03 -07002269
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08002270static struct platform_driver omap_sham_driver = {
2271 .probe = omap_sham_probe,
2272 .remove = omap_sham_remove,
2273 .driver = {
2274 .name = "omap-sham",
Mark A. Greer3b3f4402012-12-21 10:04:03 -07002275 .pm = &omap_sham_pm_ops,
Mark A. Greer03feec92012-12-21 10:04:06 -07002276 .of_match_table = omap_sham_of_match,
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08002277 },
2278};
2279
Sachin Kamat02613702013-03-04 15:09:43 +05302280module_platform_driver(omap_sham_driver);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08002281
2282MODULE_DESCRIPTION("OMAP SHA1/MD5 hw acceleration support.");
2283MODULE_LICENSE("GPL v2");
2284MODULE_AUTHOR("Dmitry Kasatkin");
Joni Lapilainen718249d2013-10-26 23:00:41 +02002285MODULE_ALIAS("platform:omap-sham");