blob: 9c3b096e15e8c238b9bbf7f5e29c695c74702e06 [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>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as published
11 * by the Free Software Foundation.
12 *
13 * Some ideas are from old omap-sha1-md5.c driver.
14 */
15
16#define pr_fmt(fmt) "%s: " fmt, __func__
17
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +080018#include <linux/err.h>
19#include <linux/device.h>
20#include <linux/module.h>
21#include <linux/init.h>
22#include <linux/errno.h>
23#include <linux/interrupt.h>
24#include <linux/kernel.h>
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +080025#include <linux/irq.h>
26#include <linux/io.h>
27#include <linux/platform_device.h>
28#include <linux/scatterlist.h>
29#include <linux/dma-mapping.h>
Mark A. Greerb359f032012-12-21 10:04:02 -070030#include <linux/pm_runtime.h>
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +080031#include <linux/delay.h>
32#include <linux/crypto.h>
33#include <linux/cryptohash.h>
34#include <crypto/scatterwalk.h>
35#include <crypto/algapi.h>
36#include <crypto/sha.h>
37#include <crypto/hash.h>
38#include <crypto/internal/hash.h>
39
Tony Lindgren45c3eb72012-11-30 08:41:50 -080040#include <linux/omap-dma.h>
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +080041#include <mach/irqs.h>
42
43#define SHA_REG_DIGEST(x) (0x00 + ((x) * 0x04))
44#define SHA_REG_DIN(x) (0x1C + ((x) * 0x04))
45
46#define SHA1_MD5_BLOCK_SIZE SHA1_BLOCK_SIZE
47#define MD5_DIGEST_SIZE 16
48
49#define SHA_REG_DIGCNT 0x14
50
51#define SHA_REG_CTRL 0x18
52#define SHA_REG_CTRL_LENGTH (0xFFFFFFFF << 5)
53#define SHA_REG_CTRL_CLOSE_HASH (1 << 4)
54#define SHA_REG_CTRL_ALGO_CONST (1 << 3)
55#define SHA_REG_CTRL_ALGO (1 << 2)
56#define SHA_REG_CTRL_INPUT_READY (1 << 1)
57#define SHA_REG_CTRL_OUTPUT_READY (1 << 0)
58
59#define SHA_REG_REV 0x5C
60#define SHA_REG_REV_MAJOR 0xF0
61#define SHA_REG_REV_MINOR 0x0F
62
63#define SHA_REG_MASK 0x60
64#define SHA_REG_MASK_DMA_EN (1 << 3)
65#define SHA_REG_MASK_IT_EN (1 << 2)
66#define SHA_REG_MASK_SOFTRESET (1 << 1)
67#define SHA_REG_AUTOIDLE (1 << 0)
68
69#define SHA_REG_SYSSTATUS 0x64
70#define SHA_REG_SYSSTATUS_RESETDONE (1 << 0)
71
72#define DEFAULT_TIMEOUT_INTERVAL HZ
73
Dmitry Kasatkinea1fd222011-06-02 21:10:05 +030074/* mostly device flags */
75#define FLAGS_BUSY 0
76#define FLAGS_FINAL 1
77#define FLAGS_DMA_ACTIVE 2
78#define FLAGS_OUTPUT_READY 3
79#define FLAGS_INIT 4
80#define FLAGS_CPU 5
Dmitry Kasatkin6c63db82011-06-02 21:10:10 +030081#define FLAGS_DMA_READY 6
Dmitry Kasatkinea1fd222011-06-02 21:10:05 +030082/* context flags */
83#define FLAGS_FINUP 16
84#define FLAGS_SG 17
85#define FLAGS_SHA1 18
86#define FLAGS_HMAC 19
87#define FLAGS_ERROR 20
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +080088
89#define OP_UPDATE 1
90#define OP_FINAL 2
91
Dmitry Kasatkin798eed52010-11-19 16:04:26 +020092#define OMAP_ALIGN_MASK (sizeof(u32)-1)
93#define OMAP_ALIGNED __attribute__((aligned(sizeof(u32))))
94
95#define BUFLEN PAGE_SIZE
96
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +080097struct omap_sham_dev;
98
99struct omap_sham_reqctx {
100 struct omap_sham_dev *dd;
101 unsigned long flags;
102 unsigned long op;
103
Dmitry Kasatkin798eed52010-11-19 16:04:26 +0200104 u8 digest[SHA1_DIGEST_SIZE] OMAP_ALIGNED;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800105 size_t digcnt;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800106 size_t bufcnt;
107 size_t buflen;
108 dma_addr_t dma_addr;
109
110 /* walk state */
111 struct scatterlist *sg;
112 unsigned int offset; /* offset in current sg */
113 unsigned int total; /* total request */
Dmitry Kasatkin798eed52010-11-19 16:04:26 +0200114
115 u8 buffer[0] OMAP_ALIGNED;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800116};
117
118struct omap_sham_hmac_ctx {
119 struct crypto_shash *shash;
120 u8 ipad[SHA1_MD5_BLOCK_SIZE];
121 u8 opad[SHA1_MD5_BLOCK_SIZE];
122};
123
124struct omap_sham_ctx {
125 struct omap_sham_dev *dd;
126
127 unsigned long flags;
128
129 /* fallback stuff */
130 struct crypto_shash *fallback;
131
132 struct omap_sham_hmac_ctx base[0];
133};
134
135#define OMAP_SHAM_QUEUE_LENGTH 1
136
137struct omap_sham_dev {
138 struct list_head list;
139 unsigned long phys_base;
140 struct device *dev;
141 void __iomem *io_base;
142 int irq;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800143 spinlock_t lock;
Dmitry Kasatkin3e133c82010-11-19 16:04:24 +0200144 int err;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800145 int dma;
146 int dma_lch;
147 struct tasklet_struct done_task;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800148
149 unsigned long flags;
150 struct crypto_queue queue;
151 struct ahash_request *req;
152};
153
154struct omap_sham_drv {
155 struct list_head dev_list;
156 spinlock_t lock;
157 unsigned long flags;
158};
159
160static struct omap_sham_drv sham = {
161 .dev_list = LIST_HEAD_INIT(sham.dev_list),
162 .lock = __SPIN_LOCK_UNLOCKED(sham.lock),
163};
164
165static inline u32 omap_sham_read(struct omap_sham_dev *dd, u32 offset)
166{
167 return __raw_readl(dd->io_base + offset);
168}
169
170static inline void omap_sham_write(struct omap_sham_dev *dd,
171 u32 offset, u32 value)
172{
173 __raw_writel(value, dd->io_base + offset);
174}
175
176static inline void omap_sham_write_mask(struct omap_sham_dev *dd, u32 address,
177 u32 value, u32 mask)
178{
179 u32 val;
180
181 val = omap_sham_read(dd, address);
182 val &= ~mask;
183 val |= value;
184 omap_sham_write(dd, address, val);
185}
186
187static inline int omap_sham_wait(struct omap_sham_dev *dd, u32 offset, u32 bit)
188{
189 unsigned long timeout = jiffies + DEFAULT_TIMEOUT_INTERVAL;
190
191 while (!(omap_sham_read(dd, offset) & bit)) {
192 if (time_is_before_jiffies(timeout))
193 return -ETIMEDOUT;
194 }
195
196 return 0;
197}
198
199static void omap_sham_copy_hash(struct ahash_request *req, int out)
200{
201 struct omap_sham_reqctx *ctx = ahash_request_ctx(req);
Dmitry Kasatkin0c3cf4c2010-11-19 16:04:22 +0200202 u32 *hash = (u32 *)ctx->digest;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800203 int i;
204
Dmitry Kasatkin3c8d7582010-11-19 16:04:27 +0200205 /* MD5 is almost unused. So copy sha1 size to reduce code */
206 for (i = 0; i < SHA1_DIGEST_SIZE / sizeof(u32); i++) {
207 if (out)
208 hash[i] = omap_sham_read(ctx->dd,
209 SHA_REG_DIGEST(i));
210 else
211 omap_sham_write(ctx->dd,
212 SHA_REG_DIGEST(i), hash[i]);
213 }
214}
215
216static void omap_sham_copy_ready_hash(struct ahash_request *req)
217{
218 struct omap_sham_reqctx *ctx = ahash_request_ctx(req);
219 u32 *in = (u32 *)ctx->digest;
220 u32 *hash = (u32 *)req->result;
221 int i;
222
223 if (!hash)
224 return;
225
Dmitry Kasatkinea1fd222011-06-02 21:10:05 +0300226 if (likely(ctx->flags & BIT(FLAGS_SHA1))) {
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800227 /* SHA1 results are in big endian */
228 for (i = 0; i < SHA1_DIGEST_SIZE / sizeof(u32); i++)
Dmitry Kasatkin3c8d7582010-11-19 16:04:27 +0200229 hash[i] = be32_to_cpu(in[i]);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800230 } else {
231 /* MD5 results are in little endian */
232 for (i = 0; i < MD5_DIGEST_SIZE / sizeof(u32); i++)
Dmitry Kasatkin3c8d7582010-11-19 16:04:27 +0200233 hash[i] = le32_to_cpu(in[i]);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800234 }
235}
236
Dmitry Kasatkin798eed52010-11-19 16:04:26 +0200237static int omap_sham_hw_init(struct omap_sham_dev *dd)
238{
Mark A. Greerb359f032012-12-21 10:04:02 -0700239 pm_runtime_get_sync(dd->dev);
Dmitry Kasatkin798eed52010-11-19 16:04:26 +0200240
Dmitry Kasatkina929cbe2011-06-02 21:10:06 +0300241 if (!test_bit(FLAGS_INIT, &dd->flags)) {
Dmitry Kasatkin798eed52010-11-19 16:04:26 +0200242 omap_sham_write_mask(dd, SHA_REG_MASK,
243 SHA_REG_MASK_SOFTRESET, SHA_REG_MASK_SOFTRESET);
244
245 if (omap_sham_wait(dd, SHA_REG_SYSSTATUS,
246 SHA_REG_SYSSTATUS_RESETDONE))
247 return -ETIMEDOUT;
248
Dmitry Kasatkina929cbe2011-06-02 21:10:06 +0300249 set_bit(FLAGS_INIT, &dd->flags);
Dmitry Kasatkin798eed52010-11-19 16:04:26 +0200250 dd->err = 0;
251 }
252
253 return 0;
254}
255
256static void omap_sham_write_ctrl(struct omap_sham_dev *dd, size_t length,
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800257 int final, int dma)
258{
259 struct omap_sham_reqctx *ctx = ahash_request_ctx(dd->req);
260 u32 val = length << 5, mask;
261
Dmitry Kasatkin798eed52010-11-19 16:04:26 +0200262 if (likely(ctx->digcnt))
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800263 omap_sham_write(dd, SHA_REG_DIGCNT, ctx->digcnt);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800264
265 omap_sham_write_mask(dd, SHA_REG_MASK,
266 SHA_REG_MASK_IT_EN | (dma ? SHA_REG_MASK_DMA_EN : 0),
267 SHA_REG_MASK_IT_EN | SHA_REG_MASK_DMA_EN);
268 /*
269 * Setting ALGO_CONST only for the first iteration
270 * and CLOSE_HASH only for the last one.
271 */
Dmitry Kasatkinea1fd222011-06-02 21:10:05 +0300272 if (ctx->flags & BIT(FLAGS_SHA1))
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800273 val |= SHA_REG_CTRL_ALGO;
274 if (!ctx->digcnt)
275 val |= SHA_REG_CTRL_ALGO_CONST;
276 if (final)
277 val |= SHA_REG_CTRL_CLOSE_HASH;
278
279 mask = SHA_REG_CTRL_ALGO_CONST | SHA_REG_CTRL_CLOSE_HASH |
280 SHA_REG_CTRL_ALGO | SHA_REG_CTRL_LENGTH;
281
282 omap_sham_write_mask(dd, SHA_REG_CTRL, val, mask);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800283}
284
285static int omap_sham_xmit_cpu(struct omap_sham_dev *dd, const u8 *buf,
286 size_t length, int final)
287{
288 struct omap_sham_reqctx *ctx = ahash_request_ctx(dd->req);
Dmitry Kasatkin798eed52010-11-19 16:04:26 +0200289 int count, len32;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800290 const u32 *buffer = (const u32 *)buf;
291
292 dev_dbg(dd->dev, "xmit_cpu: digcnt: %d, length: %d, final: %d\n",
293 ctx->digcnt, length, final);
294
Dmitry Kasatkin798eed52010-11-19 16:04:26 +0200295 omap_sham_write_ctrl(dd, length, final, 0);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800296
Dmitry Kasatkin3e133c82010-11-19 16:04:24 +0200297 /* should be non-zero before next lines to disable clocks later */
298 ctx->digcnt += length;
299
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800300 if (omap_sham_wait(dd, SHA_REG_CTRL, SHA_REG_CTRL_INPUT_READY))
301 return -ETIMEDOUT;
302
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800303 if (final)
Dmitry Kasatkined3ea9a82011-06-02 21:10:07 +0300304 set_bit(FLAGS_FINAL, &dd->flags); /* catch last interrupt */
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800305
Dmitry Kasatkin6c63db82011-06-02 21:10:10 +0300306 set_bit(FLAGS_CPU, &dd->flags);
307
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800308 len32 = DIV_ROUND_UP(length, sizeof(u32));
309
310 for (count = 0; count < len32; count++)
311 omap_sham_write(dd, SHA_REG_DIN(count), buffer[count]);
312
313 return -EINPROGRESS;
314}
315
316static int omap_sham_xmit_dma(struct omap_sham_dev *dd, dma_addr_t dma_addr,
317 size_t length, int final)
318{
319 struct omap_sham_reqctx *ctx = ahash_request_ctx(dd->req);
Dmitry Kasatkin798eed52010-11-19 16:04:26 +0200320 int len32;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800321
322 dev_dbg(dd->dev, "xmit_dma: digcnt: %d, length: %d, final: %d\n",
323 ctx->digcnt, length, final);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800324
325 len32 = DIV_ROUND_UP(length, sizeof(u32));
326
327 omap_set_dma_transfer_params(dd->dma_lch, OMAP_DMA_DATA_TYPE_S32, len32,
Samu Onkalo584db6a2010-09-03 19:20:19 +0800328 1, OMAP_DMA_SYNC_PACKET, dd->dma,
329 OMAP_DMA_DST_SYNC_PREFETCH);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800330
331 omap_set_dma_src_params(dd->dma_lch, 0, OMAP_DMA_AMODE_POST_INC,
332 dma_addr, 0, 0);
333
Dmitry Kasatkin798eed52010-11-19 16:04:26 +0200334 omap_sham_write_ctrl(dd, length, final, 1);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800335
336 ctx->digcnt += length;
337
338 if (final)
Dmitry Kasatkined3ea9a82011-06-02 21:10:07 +0300339 set_bit(FLAGS_FINAL, &dd->flags); /* catch last interrupt */
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800340
Dmitry Kasatkina929cbe2011-06-02 21:10:06 +0300341 set_bit(FLAGS_DMA_ACTIVE, &dd->flags);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800342
343 omap_start_dma(dd->dma_lch);
344
345 return -EINPROGRESS;
346}
347
348static size_t omap_sham_append_buffer(struct omap_sham_reqctx *ctx,
349 const u8 *data, size_t length)
350{
351 size_t count = min(length, ctx->buflen - ctx->bufcnt);
352
353 count = min(count, ctx->total);
354 if (count <= 0)
355 return 0;
356 memcpy(ctx->buffer + ctx->bufcnt, data, count);
357 ctx->bufcnt += count;
358
359 return count;
360}
361
362static size_t omap_sham_append_sg(struct omap_sham_reqctx *ctx)
363{
364 size_t count;
365
366 while (ctx->sg) {
367 count = omap_sham_append_buffer(ctx,
368 sg_virt(ctx->sg) + ctx->offset,
369 ctx->sg->length - ctx->offset);
370 if (!count)
371 break;
372 ctx->offset += count;
373 ctx->total -= count;
374 if (ctx->offset == ctx->sg->length) {
375 ctx->sg = sg_next(ctx->sg);
376 if (ctx->sg)
377 ctx->offset = 0;
378 else
379 ctx->total = 0;
380 }
381 }
382
383 return 0;
384}
385
Dmitry Kasatkin798eed52010-11-19 16:04:26 +0200386static int omap_sham_xmit_dma_map(struct omap_sham_dev *dd,
387 struct omap_sham_reqctx *ctx,
388 size_t length, int final)
389{
390 ctx->dma_addr = dma_map_single(dd->dev, ctx->buffer, ctx->buflen,
391 DMA_TO_DEVICE);
392 if (dma_mapping_error(dd->dev, ctx->dma_addr)) {
393 dev_err(dd->dev, "dma %u bytes error\n", ctx->buflen);
394 return -EINVAL;
395 }
396
Dmitry Kasatkinea1fd222011-06-02 21:10:05 +0300397 ctx->flags &= ~BIT(FLAGS_SG);
Dmitry Kasatkin887c8832010-11-19 16:04:29 +0200398
Dmitry Kasatkin798eed52010-11-19 16:04:26 +0200399 /* next call does not fail... so no unmap in the case of error */
400 return omap_sham_xmit_dma(dd, ctx->dma_addr, length, final);
401}
402
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800403static int omap_sham_update_dma_slow(struct omap_sham_dev *dd)
404{
405 struct omap_sham_reqctx *ctx = ahash_request_ctx(dd->req);
406 unsigned int final;
407 size_t count;
408
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800409 omap_sham_append_sg(ctx);
410
Dmitry Kasatkinea1fd222011-06-02 21:10:05 +0300411 final = (ctx->flags & BIT(FLAGS_FINUP)) && !ctx->total;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800412
413 dev_dbg(dd->dev, "slow: bufcnt: %u, digcnt: %d, final: %d\n",
414 ctx->bufcnt, ctx->digcnt, final);
415
416 if (final || (ctx->bufcnt == ctx->buflen && ctx->total)) {
417 count = ctx->bufcnt;
418 ctx->bufcnt = 0;
Dmitry Kasatkin798eed52010-11-19 16:04:26 +0200419 return omap_sham_xmit_dma_map(dd, ctx, count, final);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800420 }
421
422 return 0;
423}
424
Dmitry Kasatkin887c8832010-11-19 16:04:29 +0200425/* Start address alignment */
426#define SG_AA(sg) (IS_ALIGNED(sg->offset, sizeof(u32)))
427/* SHA1 block size alignment */
428#define SG_SA(sg) (IS_ALIGNED(sg->length, SHA1_MD5_BLOCK_SIZE))
429
430static int omap_sham_update_dma_start(struct omap_sham_dev *dd)
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800431{
432 struct omap_sham_reqctx *ctx = ahash_request_ctx(dd->req);
Dmitry Kasatkin887c8832010-11-19 16:04:29 +0200433 unsigned int length, final, tail;
434 struct scatterlist *sg;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800435
Dmitry Kasatkin887c8832010-11-19 16:04:29 +0200436 if (!ctx->total)
437 return 0;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800438
Dmitry Kasatkin887c8832010-11-19 16:04:29 +0200439 if (ctx->bufcnt || ctx->offset)
440 return omap_sham_update_dma_slow(dd);
441
442 dev_dbg(dd->dev, "fast: digcnt: %d, bufcnt: %u, total: %u\n",
443 ctx->digcnt, ctx->bufcnt, ctx->total);
444
445 sg = ctx->sg;
446
447 if (!SG_AA(sg))
448 return omap_sham_update_dma_slow(dd);
449
450 if (!sg_is_last(sg) && !SG_SA(sg))
451 /* size is not SHA1_BLOCK_SIZE aligned */
452 return omap_sham_update_dma_slow(dd);
453
454 length = min(ctx->total, sg->length);
455
456 if (sg_is_last(sg)) {
Dmitry Kasatkinea1fd222011-06-02 21:10:05 +0300457 if (!(ctx->flags & BIT(FLAGS_FINUP))) {
Dmitry Kasatkin887c8832010-11-19 16:04:29 +0200458 /* not last sg must be SHA1_MD5_BLOCK_SIZE aligned */
459 tail = length & (SHA1_MD5_BLOCK_SIZE - 1);
460 /* without finup() we need one block to close hash */
461 if (!tail)
462 tail = SHA1_MD5_BLOCK_SIZE;
463 length -= tail;
464 }
465 }
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800466
467 if (!dma_map_sg(dd->dev, ctx->sg, 1, DMA_TO_DEVICE)) {
468 dev_err(dd->dev, "dma_map_sg error\n");
469 return -EINVAL;
470 }
471
Dmitry Kasatkinea1fd222011-06-02 21:10:05 +0300472 ctx->flags |= BIT(FLAGS_SG);
Dmitry Kasatkin887c8832010-11-19 16:04:29 +0200473
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800474 ctx->total -= length;
Dmitry Kasatkin887c8832010-11-19 16:04:29 +0200475 ctx->offset = length; /* offset where to start slow */
476
Dmitry Kasatkinea1fd222011-06-02 21:10:05 +0300477 final = (ctx->flags & BIT(FLAGS_FINUP)) && !ctx->total;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800478
Dmitry Kasatkin798eed52010-11-19 16:04:26 +0200479 /* next call does not fail... so no unmap in the case of error */
Dmitry Kasatkin887c8832010-11-19 16:04:29 +0200480 return omap_sham_xmit_dma(dd, sg_dma_address(ctx->sg), length, final);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800481}
482
483static int omap_sham_update_cpu(struct omap_sham_dev *dd)
484{
485 struct omap_sham_reqctx *ctx = ahash_request_ctx(dd->req);
486 int bufcnt;
487
488 omap_sham_append_sg(ctx);
489 bufcnt = ctx->bufcnt;
490 ctx->bufcnt = 0;
491
492 return omap_sham_xmit_cpu(dd, ctx->buffer, bufcnt, 1);
493}
494
495static int omap_sham_update_dma_stop(struct omap_sham_dev *dd)
496{
497 struct omap_sham_reqctx *ctx = ahash_request_ctx(dd->req);
498
499 omap_stop_dma(dd->dma_lch);
Dmitry Kasatkinea1fd222011-06-02 21:10:05 +0300500 if (ctx->flags & BIT(FLAGS_SG)) {
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800501 dma_unmap_sg(dd->dev, ctx->sg, 1, DMA_TO_DEVICE);
Dmitry Kasatkin887c8832010-11-19 16:04:29 +0200502 if (ctx->sg->length == ctx->offset) {
503 ctx->sg = sg_next(ctx->sg);
504 if (ctx->sg)
505 ctx->offset = 0;
506 }
507 } else {
Dmitry Kasatkin798eed52010-11-19 16:04:26 +0200508 dma_unmap_single(dd->dev, ctx->dma_addr, ctx->buflen,
509 DMA_TO_DEVICE);
Dmitry Kasatkin887c8832010-11-19 16:04:29 +0200510 }
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800511
512 return 0;
513}
514
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800515static int omap_sham_init(struct ahash_request *req)
516{
517 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
518 struct omap_sham_ctx *tctx = crypto_ahash_ctx(tfm);
519 struct omap_sham_reqctx *ctx = ahash_request_ctx(req);
520 struct omap_sham_dev *dd = NULL, *tmp;
521
522 spin_lock_bh(&sham.lock);
523 if (!tctx->dd) {
524 list_for_each_entry(tmp, &sham.dev_list, list) {
525 dd = tmp;
526 break;
527 }
528 tctx->dd = dd;
529 } else {
530 dd = tctx->dd;
531 }
532 spin_unlock_bh(&sham.lock);
533
534 ctx->dd = dd;
535
536 ctx->flags = 0;
537
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800538 dev_dbg(dd->dev, "init: digest size: %d\n",
539 crypto_ahash_digestsize(tfm));
540
541 if (crypto_ahash_digestsize(tfm) == SHA1_DIGEST_SIZE)
Dmitry Kasatkinea1fd222011-06-02 21:10:05 +0300542 ctx->flags |= BIT(FLAGS_SHA1);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800543
544 ctx->bufcnt = 0;
545 ctx->digcnt = 0;
Dmitry Kasatkin798eed52010-11-19 16:04:26 +0200546 ctx->buflen = BUFLEN;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800547
Dmitry Kasatkinea1fd222011-06-02 21:10:05 +0300548 if (tctx->flags & BIT(FLAGS_HMAC)) {
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800549 struct omap_sham_hmac_ctx *bctx = tctx->base;
550
551 memcpy(ctx->buffer, bctx->ipad, SHA1_MD5_BLOCK_SIZE);
552 ctx->bufcnt = SHA1_MD5_BLOCK_SIZE;
Dmitry Kasatkinea1fd222011-06-02 21:10:05 +0300553 ctx->flags |= BIT(FLAGS_HMAC);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800554 }
555
556 return 0;
557
558}
559
560static int omap_sham_update_req(struct omap_sham_dev *dd)
561{
562 struct ahash_request *req = dd->req;
563 struct omap_sham_reqctx *ctx = ahash_request_ctx(req);
564 int err;
565
566 dev_dbg(dd->dev, "update_req: total: %u, digcnt: %d, finup: %d\n",
Dmitry Kasatkinea1fd222011-06-02 21:10:05 +0300567 ctx->total, ctx->digcnt, (ctx->flags & BIT(FLAGS_FINUP)) != 0);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800568
Dmitry Kasatkinea1fd222011-06-02 21:10:05 +0300569 if (ctx->flags & BIT(FLAGS_CPU))
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800570 err = omap_sham_update_cpu(dd);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800571 else
Dmitry Kasatkin887c8832010-11-19 16:04:29 +0200572 err = omap_sham_update_dma_start(dd);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800573
574 /* wait for dma completion before can take more data */
575 dev_dbg(dd->dev, "update: err: %d, digcnt: %d\n", err, ctx->digcnt);
576
577 return err;
578}
579
580static int omap_sham_final_req(struct omap_sham_dev *dd)
581{
582 struct ahash_request *req = dd->req;
583 struct omap_sham_reqctx *ctx = ahash_request_ctx(req);
584 int err = 0, use_dma = 1;
585
586 if (ctx->bufcnt <= 64)
587 /* faster to handle last block with cpu */
588 use_dma = 0;
589
590 if (use_dma)
Dmitry Kasatkin798eed52010-11-19 16:04:26 +0200591 err = omap_sham_xmit_dma_map(dd, ctx, ctx->bufcnt, 1);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800592 else
593 err = omap_sham_xmit_cpu(dd, ctx->buffer, ctx->bufcnt, 1);
594
595 ctx->bufcnt = 0;
596
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800597 dev_dbg(dd->dev, "final_req: err: %d\n", err);
598
599 return err;
600}
601
Dmitry Kasatkinbf362752011-04-20 13:34:58 +0300602static int omap_sham_finish_hmac(struct ahash_request *req)
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800603{
604 struct omap_sham_ctx *tctx = crypto_tfm_ctx(req->base.tfm);
605 struct omap_sham_hmac_ctx *bctx = tctx->base;
606 int bs = crypto_shash_blocksize(bctx->shash);
607 int ds = crypto_shash_digestsize(bctx->shash);
608 struct {
609 struct shash_desc shash;
610 char ctx[crypto_shash_descsize(bctx->shash)];
611 } desc;
612
613 desc.shash.tfm = bctx->shash;
614 desc.shash.flags = 0; /* not CRYPTO_TFM_REQ_MAY_SLEEP */
615
616 return crypto_shash_init(&desc.shash) ?:
617 crypto_shash_update(&desc.shash, bctx->opad, bs) ?:
Dmitry Kasatkinbf362752011-04-20 13:34:58 +0300618 crypto_shash_finup(&desc.shash, req->result, ds, req->result);
619}
620
621static int omap_sham_finish(struct ahash_request *req)
622{
623 struct omap_sham_reqctx *ctx = ahash_request_ctx(req);
624 struct omap_sham_dev *dd = ctx->dd;
625 int err = 0;
626
627 if (ctx->digcnt) {
628 omap_sham_copy_ready_hash(req);
Dmitry Kasatkinea1fd222011-06-02 21:10:05 +0300629 if (ctx->flags & BIT(FLAGS_HMAC))
Dmitry Kasatkinbf362752011-04-20 13:34:58 +0300630 err = omap_sham_finish_hmac(req);
631 }
632
633 dev_dbg(dd->dev, "digcnt: %d, bufcnt: %d\n", ctx->digcnt, ctx->bufcnt);
634
635 return err;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800636}
637
638static void omap_sham_finish_req(struct ahash_request *req, int err)
639{
640 struct omap_sham_reqctx *ctx = ahash_request_ctx(req);
Dmitry Kasatkin798eed52010-11-19 16:04:26 +0200641 struct omap_sham_dev *dd = ctx->dd;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800642
643 if (!err) {
Dmitry Kasatkin0e87b152011-06-02 21:10:03 +0300644 omap_sham_copy_hash(req, 1);
Dmitry Kasatkined3ea9a82011-06-02 21:10:07 +0300645 if (test_bit(FLAGS_FINAL, &dd->flags))
Dmitry Kasatkinbf362752011-04-20 13:34:58 +0300646 err = omap_sham_finish(req);
Dmitry Kasatkin3e133c82010-11-19 16:04:24 +0200647 } else {
Dmitry Kasatkinea1fd222011-06-02 21:10:05 +0300648 ctx->flags |= BIT(FLAGS_ERROR);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800649 }
650
Dmitry Kasatkin0efd4d82011-06-02 21:10:12 +0300651 /* atomic operation is not needed here */
652 dd->flags &= ~(BIT(FLAGS_BUSY) | BIT(FLAGS_FINAL) | BIT(FLAGS_CPU) |
653 BIT(FLAGS_DMA_READY) | BIT(FLAGS_OUTPUT_READY));
Mark A. Greerb359f032012-12-21 10:04:02 -0700654
655 pm_runtime_put_sync(dd->dev);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800656
657 if (req->base.complete)
658 req->base.complete(&req->base, err);
Dmitry Kasatkin6cb3ffe2011-06-02 21:10:09 +0300659
660 /* handle new request */
661 tasklet_schedule(&dd->done_task);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800662}
663
Dmitry Kasatkina5d87232010-11-19 16:04:25 +0200664static int omap_sham_handle_queue(struct omap_sham_dev *dd,
665 struct ahash_request *req)
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800666{
Dmitry Kasatkin6c39d112010-12-29 21:52:04 +1100667 struct crypto_async_request *async_req, *backlog;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800668 struct omap_sham_reqctx *ctx;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800669 unsigned long flags;
Dmitry Kasatkina5d87232010-11-19 16:04:25 +0200670 int err = 0, ret = 0;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800671
672 spin_lock_irqsave(&dd->lock, flags);
Dmitry Kasatkina5d87232010-11-19 16:04:25 +0200673 if (req)
674 ret = ahash_enqueue_request(&dd->queue, req);
Dmitry Kasatkina929cbe2011-06-02 21:10:06 +0300675 if (test_bit(FLAGS_BUSY, &dd->flags)) {
Dmitry Kasatkina5d87232010-11-19 16:04:25 +0200676 spin_unlock_irqrestore(&dd->lock, flags);
677 return ret;
678 }
Dmitry Kasatkin6c39d112010-12-29 21:52:04 +1100679 backlog = crypto_get_backlog(&dd->queue);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800680 async_req = crypto_dequeue_request(&dd->queue);
Dmitry Kasatkin6c39d112010-12-29 21:52:04 +1100681 if (async_req)
Dmitry Kasatkina929cbe2011-06-02 21:10:06 +0300682 set_bit(FLAGS_BUSY, &dd->flags);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800683 spin_unlock_irqrestore(&dd->lock, flags);
684
685 if (!async_req)
Dmitry Kasatkina5d87232010-11-19 16:04:25 +0200686 return ret;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800687
688 if (backlog)
689 backlog->complete(backlog, -EINPROGRESS);
690
691 req = ahash_request_cast(async_req);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800692 dd->req = req;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800693 ctx = ahash_request_ctx(req);
694
695 dev_dbg(dd->dev, "handling new req, op: %lu, nbytes: %d\n",
696 ctx->op, req->nbytes);
697
Dmitry Kasatkin798eed52010-11-19 16:04:26 +0200698 err = omap_sham_hw_init(dd);
699 if (err)
700 goto err1;
701
702 omap_set_dma_dest_params(dd->dma_lch, 0,
703 OMAP_DMA_AMODE_CONSTANT,
704 dd->phys_base + SHA_REG_DIN(0), 0, 16);
705
706 omap_set_dma_dest_burst_mode(dd->dma_lch,
707 OMAP_DMA_DATA_BURST_16);
708
709 omap_set_dma_src_burst_mode(dd->dma_lch,
710 OMAP_DMA_DATA_BURST_4);
711
712 if (ctx->digcnt)
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800713 /* request has changed - restore hash */
714 omap_sham_copy_hash(req, 0);
715
716 if (ctx->op == OP_UPDATE) {
717 err = omap_sham_update_req(dd);
Dmitry Kasatkinea1fd222011-06-02 21:10:05 +0300718 if (err != -EINPROGRESS && (ctx->flags & BIT(FLAGS_FINUP)))
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800719 /* no final() after finup() */
720 err = omap_sham_final_req(dd);
721 } else if (ctx->op == OP_FINAL) {
722 err = omap_sham_final_req(dd);
723 }
Dmitry Kasatkin798eed52010-11-19 16:04:26 +0200724err1:
Dmitry Kasatkin6cb3ffe2011-06-02 21:10:09 +0300725 if (err != -EINPROGRESS)
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800726 /* done_task will not finish it, so do it here */
727 omap_sham_finish_req(req, err);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800728
729 dev_dbg(dd->dev, "exit, err: %d\n", err);
730
Dmitry Kasatkina5d87232010-11-19 16:04:25 +0200731 return ret;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800732}
733
734static int omap_sham_enqueue(struct ahash_request *req, unsigned int op)
735{
736 struct omap_sham_reqctx *ctx = ahash_request_ctx(req);
737 struct omap_sham_ctx *tctx = crypto_tfm_ctx(req->base.tfm);
738 struct omap_sham_dev *dd = tctx->dd;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800739
740 ctx->op = op;
741
Dmitry Kasatkina5d87232010-11-19 16:04:25 +0200742 return omap_sham_handle_queue(dd, req);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800743}
744
745static int omap_sham_update(struct ahash_request *req)
746{
747 struct omap_sham_reqctx *ctx = ahash_request_ctx(req);
748
749 if (!req->nbytes)
750 return 0;
751
752 ctx->total = req->nbytes;
753 ctx->sg = req->src;
754 ctx->offset = 0;
755
Dmitry Kasatkinea1fd222011-06-02 21:10:05 +0300756 if (ctx->flags & BIT(FLAGS_FINUP)) {
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800757 if ((ctx->digcnt + ctx->bufcnt + ctx->total) < 9) {
758 /*
759 * OMAP HW accel works only with buffers >= 9
760 * will switch to bypass in final()
761 * final has the same request and data
762 */
763 omap_sham_append_sg(ctx);
764 return 0;
Dmitry Kasatkin887c8832010-11-19 16:04:29 +0200765 } else if (ctx->bufcnt + ctx->total <= SHA1_MD5_BLOCK_SIZE) {
766 /*
767 * faster to use CPU for short transfers
768 */
Dmitry Kasatkinea1fd222011-06-02 21:10:05 +0300769 ctx->flags |= BIT(FLAGS_CPU);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800770 }
Dmitry Kasatkin887c8832010-11-19 16:04:29 +0200771 } else if (ctx->bufcnt + ctx->total < ctx->buflen) {
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800772 omap_sham_append_sg(ctx);
773 return 0;
774 }
775
776 return omap_sham_enqueue(req, OP_UPDATE);
777}
778
779static int omap_sham_shash_digest(struct crypto_shash *shash, u32 flags,
780 const u8 *data, unsigned int len, u8 *out)
781{
782 struct {
783 struct shash_desc shash;
784 char ctx[crypto_shash_descsize(shash)];
785 } desc;
786
787 desc.shash.tfm = shash;
788 desc.shash.flags = flags & CRYPTO_TFM_REQ_MAY_SLEEP;
789
790 return crypto_shash_digest(&desc.shash, data, len, out);
791}
792
793static int omap_sham_final_shash(struct ahash_request *req)
794{
795 struct omap_sham_ctx *tctx = crypto_tfm_ctx(req->base.tfm);
796 struct omap_sham_reqctx *ctx = ahash_request_ctx(req);
797
798 return omap_sham_shash_digest(tctx->fallback, req->base.flags,
799 ctx->buffer, ctx->bufcnt, req->result);
800}
801
802static int omap_sham_final(struct ahash_request *req)
803{
804 struct omap_sham_reqctx *ctx = ahash_request_ctx(req);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800805
Dmitry Kasatkinea1fd222011-06-02 21:10:05 +0300806 ctx->flags |= BIT(FLAGS_FINUP);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800807
Dmitry Kasatkinea1fd222011-06-02 21:10:05 +0300808 if (ctx->flags & BIT(FLAGS_ERROR))
Dmitry Kasatkinbf362752011-04-20 13:34:58 +0300809 return 0; /* uncompleted hash is not needed */
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800810
Dmitry Kasatkinbf362752011-04-20 13:34:58 +0300811 /* OMAP HW accel works only with buffers >= 9 */
812 /* HMAC is always >= 9 because ipad == block size */
813 if ((ctx->digcnt + ctx->bufcnt) < 9)
814 return omap_sham_final_shash(req);
815 else if (ctx->bufcnt)
816 return omap_sham_enqueue(req, OP_FINAL);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800817
Dmitry Kasatkinbf362752011-04-20 13:34:58 +0300818 /* copy ready hash (+ finalize hmac) */
819 return omap_sham_finish(req);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800820}
821
822static int omap_sham_finup(struct ahash_request *req)
823{
824 struct omap_sham_reqctx *ctx = ahash_request_ctx(req);
825 int err1, err2;
826
Dmitry Kasatkinea1fd222011-06-02 21:10:05 +0300827 ctx->flags |= BIT(FLAGS_FINUP);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800828
829 err1 = omap_sham_update(req);
Markku Kylanpaa455e3382011-04-20 13:34:55 +0300830 if (err1 == -EINPROGRESS || err1 == -EBUSY)
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800831 return err1;
832 /*
833 * final() has to be always called to cleanup resources
834 * even if udpate() failed, except EINPROGRESS
835 */
836 err2 = omap_sham_final(req);
837
838 return err1 ?: err2;
839}
840
841static int omap_sham_digest(struct ahash_request *req)
842{
843 return omap_sham_init(req) ?: omap_sham_finup(req);
844}
845
846static int omap_sham_setkey(struct crypto_ahash *tfm, const u8 *key,
847 unsigned int keylen)
848{
849 struct omap_sham_ctx *tctx = crypto_ahash_ctx(tfm);
850 struct omap_sham_hmac_ctx *bctx = tctx->base;
851 int bs = crypto_shash_blocksize(bctx->shash);
852 int ds = crypto_shash_digestsize(bctx->shash);
853 int err, i;
854 err = crypto_shash_setkey(tctx->fallback, key, keylen);
855 if (err)
856 return err;
857
858 if (keylen > bs) {
859 err = omap_sham_shash_digest(bctx->shash,
860 crypto_shash_get_flags(bctx->shash),
861 key, keylen, bctx->ipad);
862 if (err)
863 return err;
864 keylen = ds;
865 } else {
866 memcpy(bctx->ipad, key, keylen);
867 }
868
869 memset(bctx->ipad + keylen, 0, bs - keylen);
870 memcpy(bctx->opad, bctx->ipad, bs);
871
872 for (i = 0; i < bs; i++) {
873 bctx->ipad[i] ^= 0x36;
874 bctx->opad[i] ^= 0x5c;
875 }
876
877 return err;
878}
879
880static int omap_sham_cra_init_alg(struct crypto_tfm *tfm, const char *alg_base)
881{
882 struct omap_sham_ctx *tctx = crypto_tfm_ctx(tfm);
883 const char *alg_name = crypto_tfm_alg_name(tfm);
884
885 /* Allocate a fallback and abort if it failed. */
886 tctx->fallback = crypto_alloc_shash(alg_name, 0,
887 CRYPTO_ALG_NEED_FALLBACK);
888 if (IS_ERR(tctx->fallback)) {
889 pr_err("omap-sham: fallback driver '%s' "
890 "could not be loaded.\n", alg_name);
891 return PTR_ERR(tctx->fallback);
892 }
893
894 crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
Dmitry Kasatkin798eed52010-11-19 16:04:26 +0200895 sizeof(struct omap_sham_reqctx) + BUFLEN);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800896
897 if (alg_base) {
898 struct omap_sham_hmac_ctx *bctx = tctx->base;
Dmitry Kasatkinea1fd222011-06-02 21:10:05 +0300899 tctx->flags |= BIT(FLAGS_HMAC);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800900 bctx->shash = crypto_alloc_shash(alg_base, 0,
901 CRYPTO_ALG_NEED_FALLBACK);
902 if (IS_ERR(bctx->shash)) {
903 pr_err("omap-sham: base driver '%s' "
904 "could not be loaded.\n", alg_base);
905 crypto_free_shash(tctx->fallback);
906 return PTR_ERR(bctx->shash);
907 }
908
909 }
910
911 return 0;
912}
913
914static int omap_sham_cra_init(struct crypto_tfm *tfm)
915{
916 return omap_sham_cra_init_alg(tfm, NULL);
917}
918
919static int omap_sham_cra_sha1_init(struct crypto_tfm *tfm)
920{
921 return omap_sham_cra_init_alg(tfm, "sha1");
922}
923
924static int omap_sham_cra_md5_init(struct crypto_tfm *tfm)
925{
926 return omap_sham_cra_init_alg(tfm, "md5");
927}
928
929static void omap_sham_cra_exit(struct crypto_tfm *tfm)
930{
931 struct omap_sham_ctx *tctx = crypto_tfm_ctx(tfm);
932
933 crypto_free_shash(tctx->fallback);
934 tctx->fallback = NULL;
935
Dmitry Kasatkinea1fd222011-06-02 21:10:05 +0300936 if (tctx->flags & BIT(FLAGS_HMAC)) {
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800937 struct omap_sham_hmac_ctx *bctx = tctx->base;
938 crypto_free_shash(bctx->shash);
939 }
940}
941
942static struct ahash_alg algs[] = {
943{
944 .init = omap_sham_init,
945 .update = omap_sham_update,
946 .final = omap_sham_final,
947 .finup = omap_sham_finup,
948 .digest = omap_sham_digest,
949 .halg.digestsize = SHA1_DIGEST_SIZE,
950 .halg.base = {
951 .cra_name = "sha1",
952 .cra_driver_name = "omap-sha1",
953 .cra_priority = 100,
954 .cra_flags = CRYPTO_ALG_TYPE_AHASH |
Nikos Mavrogiannopoulosd912bb72011-11-01 13:39:56 +0100955 CRYPTO_ALG_KERN_DRIVER_ONLY |
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800956 CRYPTO_ALG_ASYNC |
957 CRYPTO_ALG_NEED_FALLBACK,
958 .cra_blocksize = SHA1_BLOCK_SIZE,
959 .cra_ctxsize = sizeof(struct omap_sham_ctx),
960 .cra_alignmask = 0,
961 .cra_module = THIS_MODULE,
962 .cra_init = omap_sham_cra_init,
963 .cra_exit = omap_sham_cra_exit,
964 }
965},
966{
967 .init = omap_sham_init,
968 .update = omap_sham_update,
969 .final = omap_sham_final,
970 .finup = omap_sham_finup,
971 .digest = omap_sham_digest,
972 .halg.digestsize = MD5_DIGEST_SIZE,
973 .halg.base = {
974 .cra_name = "md5",
975 .cra_driver_name = "omap-md5",
976 .cra_priority = 100,
977 .cra_flags = CRYPTO_ALG_TYPE_AHASH |
Nikos Mavrogiannopoulosd912bb72011-11-01 13:39:56 +0100978 CRYPTO_ALG_KERN_DRIVER_ONLY |
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800979 CRYPTO_ALG_ASYNC |
980 CRYPTO_ALG_NEED_FALLBACK,
981 .cra_blocksize = SHA1_BLOCK_SIZE,
982 .cra_ctxsize = sizeof(struct omap_sham_ctx),
Dmitry Kasatkin798eed52010-11-19 16:04:26 +0200983 .cra_alignmask = OMAP_ALIGN_MASK,
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +0800984 .cra_module = THIS_MODULE,
985 .cra_init = omap_sham_cra_init,
986 .cra_exit = omap_sham_cra_exit,
987 }
988},
989{
990 .init = omap_sham_init,
991 .update = omap_sham_update,
992 .final = omap_sham_final,
993 .finup = omap_sham_finup,
994 .digest = omap_sham_digest,
995 .setkey = omap_sham_setkey,
996 .halg.digestsize = SHA1_DIGEST_SIZE,
997 .halg.base = {
998 .cra_name = "hmac(sha1)",
999 .cra_driver_name = "omap-hmac-sha1",
1000 .cra_priority = 100,
1001 .cra_flags = CRYPTO_ALG_TYPE_AHASH |
Nikos Mavrogiannopoulosd912bb72011-11-01 13:39:56 +01001002 CRYPTO_ALG_KERN_DRIVER_ONLY |
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001003 CRYPTO_ALG_ASYNC |
1004 CRYPTO_ALG_NEED_FALLBACK,
1005 .cra_blocksize = SHA1_BLOCK_SIZE,
1006 .cra_ctxsize = sizeof(struct omap_sham_ctx) +
1007 sizeof(struct omap_sham_hmac_ctx),
Dmitry Kasatkin798eed52010-11-19 16:04:26 +02001008 .cra_alignmask = OMAP_ALIGN_MASK,
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001009 .cra_module = THIS_MODULE,
1010 .cra_init = omap_sham_cra_sha1_init,
1011 .cra_exit = omap_sham_cra_exit,
1012 }
1013},
1014{
1015 .init = omap_sham_init,
1016 .update = omap_sham_update,
1017 .final = omap_sham_final,
1018 .finup = omap_sham_finup,
1019 .digest = omap_sham_digest,
1020 .setkey = omap_sham_setkey,
1021 .halg.digestsize = MD5_DIGEST_SIZE,
1022 .halg.base = {
1023 .cra_name = "hmac(md5)",
1024 .cra_driver_name = "omap-hmac-md5",
1025 .cra_priority = 100,
1026 .cra_flags = CRYPTO_ALG_TYPE_AHASH |
Nikos Mavrogiannopoulosd912bb72011-11-01 13:39:56 +01001027 CRYPTO_ALG_KERN_DRIVER_ONLY |
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001028 CRYPTO_ALG_ASYNC |
1029 CRYPTO_ALG_NEED_FALLBACK,
1030 .cra_blocksize = SHA1_BLOCK_SIZE,
1031 .cra_ctxsize = sizeof(struct omap_sham_ctx) +
1032 sizeof(struct omap_sham_hmac_ctx),
Dmitry Kasatkin798eed52010-11-19 16:04:26 +02001033 .cra_alignmask = OMAP_ALIGN_MASK,
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001034 .cra_module = THIS_MODULE,
1035 .cra_init = omap_sham_cra_md5_init,
1036 .cra_exit = omap_sham_cra_exit,
1037 }
1038}
1039};
1040
1041static void omap_sham_done_task(unsigned long data)
1042{
1043 struct omap_sham_dev *dd = (struct omap_sham_dev *)data;
Dmitry Kasatkin6c63db82011-06-02 21:10:10 +03001044 int err = 0;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001045
Dmitry Kasatkin6cb3ffe2011-06-02 21:10:09 +03001046 if (!test_bit(FLAGS_BUSY, &dd->flags)) {
1047 omap_sham_handle_queue(dd, NULL);
1048 return;
1049 }
1050
Dmitry Kasatkin6c63db82011-06-02 21:10:10 +03001051 if (test_bit(FLAGS_CPU, &dd->flags)) {
1052 if (test_and_clear_bit(FLAGS_OUTPUT_READY, &dd->flags))
1053 goto finish;
1054 } else if (test_bit(FLAGS_DMA_READY, &dd->flags)) {
1055 if (test_and_clear_bit(FLAGS_DMA_ACTIVE, &dd->flags)) {
1056 omap_sham_update_dma_stop(dd);
1057 if (dd->err) {
1058 err = dd->err;
1059 goto finish;
1060 }
1061 }
1062 if (test_and_clear_bit(FLAGS_OUTPUT_READY, &dd->flags)) {
1063 /* hash or semi-hash ready */
1064 clear_bit(FLAGS_DMA_READY, &dd->flags);
Dmitry Kasatkin887c8832010-11-19 16:04:29 +02001065 err = omap_sham_update_dma_start(dd);
Dmitry Kasatkin6c63db82011-06-02 21:10:10 +03001066 if (err != -EINPROGRESS)
1067 goto finish;
1068 }
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001069 }
1070
Dmitry Kasatkin6c63db82011-06-02 21:10:10 +03001071 return;
Dmitry Kasatkin3e133c82010-11-19 16:04:24 +02001072
Dmitry Kasatkin6c63db82011-06-02 21:10:10 +03001073finish:
1074 dev_dbg(dd->dev, "update done: err: %d\n", err);
1075 /* finish curent request */
1076 omap_sham_finish_req(dd->req, err);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001077}
1078
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001079static irqreturn_t omap_sham_irq(int irq, void *dev_id)
1080{
1081 struct omap_sham_dev *dd = dev_id;
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001082
Dmitry Kasatkined3ea9a82011-06-02 21:10:07 +03001083 if (unlikely(test_bit(FLAGS_FINAL, &dd->flags)))
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001084 /* final -> allow device to go to power-saving mode */
1085 omap_sham_write_mask(dd, SHA_REG_CTRL, 0, SHA_REG_CTRL_LENGTH);
1086
1087 omap_sham_write_mask(dd, SHA_REG_CTRL, SHA_REG_CTRL_OUTPUT_READY,
1088 SHA_REG_CTRL_OUTPUT_READY);
1089 omap_sham_read(dd, SHA_REG_CTRL);
1090
Dmitry Kasatkincd3f1d52011-06-02 21:10:13 +03001091 if (!test_bit(FLAGS_BUSY, &dd->flags)) {
1092 dev_warn(dd->dev, "Interrupt when no active requests.\n");
1093 return IRQ_HANDLED;
1094 }
1095
Dmitry Kasatkined3ea9a82011-06-02 21:10:07 +03001096 set_bit(FLAGS_OUTPUT_READY, &dd->flags);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001097 tasklet_schedule(&dd->done_task);
1098
1099 return IRQ_HANDLED;
1100}
1101
1102static void omap_sham_dma_callback(int lch, u16 ch_status, void *data)
1103{
1104 struct omap_sham_dev *dd = data;
1105
Dmitry Kasatkin3e133c82010-11-19 16:04:24 +02001106 if (ch_status != OMAP_DMA_BLOCK_IRQ) {
1107 pr_err("omap-sham DMA error status: 0x%hx\n", ch_status);
1108 dd->err = -EIO;
Dmitry Kasatkina929cbe2011-06-02 21:10:06 +03001109 clear_bit(FLAGS_INIT, &dd->flags);/* request to re-initialize */
Dmitry Kasatkin3e133c82010-11-19 16:04:24 +02001110 }
1111
Dmitry Kasatkin6c63db82011-06-02 21:10:10 +03001112 set_bit(FLAGS_DMA_READY, &dd->flags);
Dmitry Kasatkin3e133c82010-11-19 16:04:24 +02001113 tasklet_schedule(&dd->done_task);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001114}
1115
1116static int omap_sham_dma_init(struct omap_sham_dev *dd)
1117{
1118 int err;
1119
1120 dd->dma_lch = -1;
1121
1122 err = omap_request_dma(dd->dma, dev_name(dd->dev),
1123 omap_sham_dma_callback, dd, &dd->dma_lch);
1124 if (err) {
1125 dev_err(dd->dev, "Unable to request DMA channel\n");
1126 return err;
1127 }
Samu Onkalo584db6a2010-09-03 19:20:19 +08001128
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001129 return 0;
1130}
1131
1132static void omap_sham_dma_cleanup(struct omap_sham_dev *dd)
1133{
1134 if (dd->dma_lch >= 0) {
1135 omap_free_dma(dd->dma_lch);
1136 dd->dma_lch = -1;
1137 }
1138}
1139
1140static int __devinit omap_sham_probe(struct platform_device *pdev)
1141{
1142 struct omap_sham_dev *dd;
1143 struct device *dev = &pdev->dev;
1144 struct resource *res;
1145 int err, i, j;
1146
1147 dd = kzalloc(sizeof(struct omap_sham_dev), GFP_KERNEL);
1148 if (dd == NULL) {
1149 dev_err(dev, "unable to alloc data struct.\n");
1150 err = -ENOMEM;
1151 goto data_err;
1152 }
1153 dd->dev = dev;
1154 platform_set_drvdata(pdev, dd);
1155
1156 INIT_LIST_HEAD(&dd->list);
1157 spin_lock_init(&dd->lock);
1158 tasklet_init(&dd->done_task, omap_sham_done_task, (unsigned long)dd);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001159 crypto_init_queue(&dd->queue, OMAP_SHAM_QUEUE_LENGTH);
1160
1161 dd->irq = -1;
1162
1163 /* Get the base address */
1164 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1165 if (!res) {
1166 dev_err(dev, "no MEM resource info\n");
1167 err = -ENODEV;
1168 goto res_err;
1169 }
1170 dd->phys_base = res->start;
1171
1172 /* Get the DMA */
1173 res = platform_get_resource(pdev, IORESOURCE_DMA, 0);
1174 if (!res) {
1175 dev_err(dev, "no DMA resource info\n");
1176 err = -ENODEV;
1177 goto res_err;
1178 }
1179 dd->dma = res->start;
1180
1181 /* Get the IRQ */
1182 dd->irq = platform_get_irq(pdev, 0);
1183 if (dd->irq < 0) {
1184 dev_err(dev, "no IRQ resource info\n");
1185 err = dd->irq;
1186 goto res_err;
1187 }
1188
1189 err = request_irq(dd->irq, omap_sham_irq,
1190 IRQF_TRIGGER_LOW, dev_name(dev), dd);
1191 if (err) {
1192 dev_err(dev, "unable to request irq.\n");
1193 goto res_err;
1194 }
1195
1196 err = omap_sham_dma_init(dd);
1197 if (err)
1198 goto dma_err;
1199
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001200 dd->io_base = ioremap(dd->phys_base, SZ_4K);
1201 if (!dd->io_base) {
1202 dev_err(dev, "can't ioremap\n");
1203 err = -ENOMEM;
1204 goto io_err;
1205 }
1206
Mark A. Greerb359f032012-12-21 10:04:02 -07001207 pm_runtime_enable(dev);
1208 pm_runtime_get_sync(dev);
1209
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001210 dev_info(dev, "hw accel on OMAP rev %u.%u\n",
1211 (omap_sham_read(dd, SHA_REG_REV) & SHA_REG_REV_MAJOR) >> 4,
1212 omap_sham_read(dd, SHA_REG_REV) & SHA_REG_REV_MINOR);
Mark A. Greerb359f032012-12-21 10:04:02 -07001213
1214 pm_runtime_put_sync(&pdev->dev);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001215
1216 spin_lock(&sham.lock);
1217 list_add_tail(&dd->list, &sham.dev_list);
1218 spin_unlock(&sham.lock);
1219
1220 for (i = 0; i < ARRAY_SIZE(algs); i++) {
1221 err = crypto_register_ahash(&algs[i]);
1222 if (err)
1223 goto err_algs;
1224 }
1225
1226 return 0;
1227
1228err_algs:
1229 for (j = 0; j < i; j++)
1230 crypto_unregister_ahash(&algs[j]);
1231 iounmap(dd->io_base);
Mark A. Greerb359f032012-12-21 10:04:02 -07001232 pm_runtime_disable(dev);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001233io_err:
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001234 omap_sham_dma_cleanup(dd);
1235dma_err:
1236 if (dd->irq >= 0)
1237 free_irq(dd->irq, dd);
1238res_err:
1239 kfree(dd);
1240 dd = NULL;
1241data_err:
1242 dev_err(dev, "initialization failed.\n");
1243
1244 return err;
1245}
1246
1247static int __devexit omap_sham_remove(struct platform_device *pdev)
1248{
1249 static struct omap_sham_dev *dd;
1250 int i;
1251
1252 dd = platform_get_drvdata(pdev);
1253 if (!dd)
1254 return -ENODEV;
1255 spin_lock(&sham.lock);
1256 list_del(&dd->list);
1257 spin_unlock(&sham.lock);
1258 for (i = 0; i < ARRAY_SIZE(algs); i++)
1259 crypto_unregister_ahash(&algs[i]);
1260 tasklet_kill(&dd->done_task);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001261 iounmap(dd->io_base);
Mark A. Greerb359f032012-12-21 10:04:02 -07001262 pm_runtime_disable(&pdev->dev);
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001263 omap_sham_dma_cleanup(dd);
1264 if (dd->irq >= 0)
1265 free_irq(dd->irq, dd);
1266 kfree(dd);
1267 dd = NULL;
1268
1269 return 0;
1270}
1271
Mark A. Greer3b3f4402012-12-21 10:04:03 -07001272#ifdef CONFIG_PM_SLEEP
1273static int omap_sham_suspend(struct device *dev)
1274{
1275 pm_runtime_put_sync(dev);
1276 return 0;
1277}
1278
1279static int omap_sham_resume(struct device *dev)
1280{
1281 pm_runtime_get_sync(dev);
1282 return 0;
1283}
1284#endif
1285
1286static const struct dev_pm_ops omap_sham_pm_ops = {
1287 SET_SYSTEM_SLEEP_PM_OPS(omap_sham_suspend, omap_sham_resume)
1288};
1289
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001290static struct platform_driver omap_sham_driver = {
1291 .probe = omap_sham_probe,
1292 .remove = omap_sham_remove,
1293 .driver = {
1294 .name = "omap-sham",
1295 .owner = THIS_MODULE,
Mark A. Greer3b3f4402012-12-21 10:04:03 -07001296 .pm = &omap_sham_pm_ops,
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001297 },
1298};
1299
1300static int __init omap_sham_mod_init(void)
1301{
Dmitry Kasatkin8628e7c2010-05-03 11:10:59 +08001302 return platform_driver_register(&omap_sham_driver);
1303}
1304
1305static void __exit omap_sham_mod_exit(void)
1306{
1307 platform_driver_unregister(&omap_sham_driver);
1308}
1309
1310module_init(omap_sham_mod_init);
1311module_exit(omap_sham_mod_exit);
1312
1313MODULE_DESCRIPTION("OMAP SHA1/MD5 hw acceleration support.");
1314MODULE_LICENSE("GPL v2");
1315MODULE_AUTHOR("Dmitry Kasatkin");