Commit | Line | Data |
---|---|---|
8628e7c8 DK |
1 | /* |
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> | |
0d373d60 | 8 | * Copyright (c) 2011 Texas Instruments Incorporated |
8628e7c8 DK |
9 | * |
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 | ||
8628e7c8 DK |
19 | #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> | |
8628e7c8 DK |
26 | #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> | |
dfd061d5 MG |
31 | #include <linux/dmaengine.h> |
32 | #include <linux/omap-dma.h> | |
b359f034 | 33 | #include <linux/pm_runtime.h> |
03feec9c MG |
34 | #include <linux/of.h> |
35 | #include <linux/of_device.h> | |
36 | #include <linux/of_address.h> | |
37 | #include <linux/of_irq.h> | |
8628e7c8 DK |
38 | #include <linux/delay.h> |
39 | #include <linux/crypto.h> | |
40 | #include <linux/cryptohash.h> | |
41 | #include <crypto/scatterwalk.h> | |
42 | #include <crypto/algapi.h> | |
43 | #include <crypto/sha.h> | |
44 | #include <crypto/hash.h> | |
45 | #include <crypto/internal/hash.h> | |
46 | ||
8628e7c8 DK |
47 | #define MD5_DIGEST_SIZE 16 |
48 | ||
dfd061d5 MG |
49 | #define DST_MAXBURST 16 |
50 | #define DMA_MIN (DST_MAXBURST * sizeof(u32)) | |
51 | ||
0d373d60 MG |
52 | #define SHA_REG_IDIGEST(dd, x) ((dd)->pdata->idigest_ofs + ((x)*0x04)) |
53 | #define SHA_REG_DIN(dd, x) ((dd)->pdata->din_ofs + ((x) * 0x04)) | |
54 | #define SHA_REG_DIGCNT(dd) ((dd)->pdata->digcnt_ofs) | |
55 | ||
eaef7e3f | 56 | #define SHA_REG_ODIGEST(dd, x) ((dd)->pdata->odigest_ofs + (x * 0x04)) |
8628e7c8 DK |
57 | |
58 | #define SHA_REG_CTRL 0x18 | |
59 | #define SHA_REG_CTRL_LENGTH (0xFFFFFFFF << 5) | |
60 | #define SHA_REG_CTRL_CLOSE_HASH (1 << 4) | |
61 | #define SHA_REG_CTRL_ALGO_CONST (1 << 3) | |
62 | #define SHA_REG_CTRL_ALGO (1 << 2) | |
63 | #define SHA_REG_CTRL_INPUT_READY (1 << 1) | |
64 | #define SHA_REG_CTRL_OUTPUT_READY (1 << 0) | |
65 | ||
0d373d60 | 66 | #define SHA_REG_REV(dd) ((dd)->pdata->rev_ofs) |
8628e7c8 | 67 | |
0d373d60 | 68 | #define SHA_REG_MASK(dd) ((dd)->pdata->mask_ofs) |
8628e7c8 DK |
69 | #define SHA_REG_MASK_DMA_EN (1 << 3) |
70 | #define SHA_REG_MASK_IT_EN (1 << 2) | |
71 | #define SHA_REG_MASK_SOFTRESET (1 << 1) | |
72 | #define SHA_REG_AUTOIDLE (1 << 0) | |
73 | ||
0d373d60 | 74 | #define SHA_REG_SYSSTATUS(dd) ((dd)->pdata->sysstatus_ofs) |
8628e7c8 DK |
75 | #define SHA_REG_SYSSTATUS_RESETDONE (1 << 0) |
76 | ||
eaef7e3f | 77 | #define SHA_REG_MODE(dd) ((dd)->pdata->mode_ofs) |
0d373d60 MG |
78 | #define SHA_REG_MODE_HMAC_OUTER_HASH (1 << 7) |
79 | #define SHA_REG_MODE_HMAC_KEY_PROC (1 << 5) | |
80 | #define SHA_REG_MODE_CLOSE_HASH (1 << 4) | |
81 | #define SHA_REG_MODE_ALGO_CONSTANT (1 << 3) | |
0d373d60 | 82 | |
eaef7e3f LV |
83 | #define SHA_REG_MODE_ALGO_MASK (7 << 0) |
84 | #define SHA_REG_MODE_ALGO_MD5_128 (0 << 1) | |
85 | #define SHA_REG_MODE_ALGO_SHA1_160 (1 << 1) | |
86 | #define SHA_REG_MODE_ALGO_SHA2_224 (2 << 1) | |
87 | #define SHA_REG_MODE_ALGO_SHA2_256 (3 << 1) | |
88 | #define SHA_REG_MODE_ALGO_SHA2_384 (1 << 0) | |
89 | #define SHA_REG_MODE_ALGO_SHA2_512 (3 << 0) | |
90 | ||
91 | #define SHA_REG_LENGTH(dd) ((dd)->pdata->length_ofs) | |
0d373d60 MG |
92 | |
93 | #define SHA_REG_IRQSTATUS 0x118 | |
94 | #define SHA_REG_IRQSTATUS_CTX_RDY (1 << 3) | |
95 | #define SHA_REG_IRQSTATUS_PARTHASH_RDY (1 << 2) | |
96 | #define SHA_REG_IRQSTATUS_INPUT_RDY (1 << 1) | |
97 | #define SHA_REG_IRQSTATUS_OUTPUT_RDY (1 << 0) | |
98 | ||
99 | #define SHA_REG_IRQENA 0x11C | |
100 | #define SHA_REG_IRQENA_CTX_RDY (1 << 3) | |
101 | #define SHA_REG_IRQENA_PARTHASH_RDY (1 << 2) | |
102 | #define SHA_REG_IRQENA_INPUT_RDY (1 << 1) | |
103 | #define SHA_REG_IRQENA_OUTPUT_RDY (1 << 0) | |
104 | ||
8628e7c8 DK |
105 | #define DEFAULT_TIMEOUT_INTERVAL HZ |
106 | ||
ea1fd224 DK |
107 | /* mostly device flags */ |
108 | #define FLAGS_BUSY 0 | |
109 | #define FLAGS_FINAL 1 | |
110 | #define FLAGS_DMA_ACTIVE 2 | |
111 | #define FLAGS_OUTPUT_READY 3 | |
112 | #define FLAGS_INIT 4 | |
113 | #define FLAGS_CPU 5 | |
6c63db82 | 114 | #define FLAGS_DMA_READY 6 |
0d373d60 MG |
115 | #define FLAGS_AUTO_XOR 7 |
116 | #define FLAGS_BE32_SHA1 8 | |
ea1fd224 DK |
117 | /* context flags */ |
118 | #define FLAGS_FINUP 16 | |
119 | #define FLAGS_SG 17 | |
8628e7c8 | 120 | |
0d373d60 | 121 | #define FLAGS_MODE_SHIFT 18 |
eaef7e3f LV |
122 | #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 | |
0d373d60 MG |
132 | |
133 | #define OP_UPDATE 1 | |
134 | #define OP_FINAL 2 | |
8628e7c8 | 135 | |
798eed5d DK |
136 | #define OMAP_ALIGN_MASK (sizeof(u32)-1) |
137 | #define OMAP_ALIGNED __attribute__((aligned(sizeof(u32)))) | |
138 | ||
0d373d60 | 139 | #define BUFLEN PAGE_SIZE |
798eed5d | 140 | |
8628e7c8 DK |
141 | struct omap_sham_dev; |
142 | ||
143 | struct omap_sham_reqctx { | |
144 | struct omap_sham_dev *dd; | |
145 | unsigned long flags; | |
146 | unsigned long op; | |
147 | ||
eaef7e3f | 148 | u8 digest[SHA512_DIGEST_SIZE] OMAP_ALIGNED; |
8628e7c8 | 149 | size_t digcnt; |
8628e7c8 DK |
150 | size_t bufcnt; |
151 | size_t buflen; | |
152 | dma_addr_t dma_addr; | |
153 | ||
154 | /* walk state */ | |
155 | struct scatterlist *sg; | |
dfd061d5 | 156 | struct scatterlist sgl; |
8628e7c8 DK |
157 | unsigned int offset; /* offset in current sg */ |
158 | unsigned int total; /* total request */ | |
798eed5d DK |
159 | |
160 | u8 buffer[0] OMAP_ALIGNED; | |
8628e7c8 DK |
161 | }; |
162 | ||
163 | struct omap_sham_hmac_ctx { | |
164 | struct crypto_shash *shash; | |
eaef7e3f LV |
165 | u8 ipad[SHA512_BLOCK_SIZE] OMAP_ALIGNED; |
166 | u8 opad[SHA512_BLOCK_SIZE] OMAP_ALIGNED; | |
8628e7c8 DK |
167 | }; |
168 | ||
169 | struct omap_sham_ctx { | |
170 | struct omap_sham_dev *dd; | |
171 | ||
172 | unsigned long flags; | |
173 | ||
174 | /* fallback stuff */ | |
175 | struct crypto_shash *fallback; | |
176 | ||
177 | struct omap_sham_hmac_ctx base[0]; | |
178 | }; | |
179 | ||
180 | #define OMAP_SHAM_QUEUE_LENGTH 1 | |
181 | ||
d20fb18b MG |
182 | struct omap_sham_algs_info { |
183 | struct ahash_alg *algs_list; | |
184 | unsigned int size; | |
185 | unsigned int registered; | |
186 | }; | |
187 | ||
0d373d60 | 188 | struct omap_sham_pdata { |
d20fb18b MG |
189 | struct omap_sham_algs_info *algs_info; |
190 | unsigned int algs_info_size; | |
0d373d60 MG |
191 | unsigned long flags; |
192 | int digest_size; | |
193 | ||
194 | void (*copy_hash)(struct ahash_request *req, int out); | |
195 | void (*write_ctrl)(struct omap_sham_dev *dd, size_t length, | |
196 | int final, int dma); | |
197 | void (*trigger)(struct omap_sham_dev *dd, size_t length); | |
198 | int (*poll_irq)(struct omap_sham_dev *dd); | |
199 | irqreturn_t (*intr_hdlr)(int irq, void *dev_id); | |
200 | ||
201 | u32 odigest_ofs; | |
202 | u32 idigest_ofs; | |
203 | u32 din_ofs; | |
204 | u32 digcnt_ofs; | |
205 | u32 rev_ofs; | |
206 | u32 mask_ofs; | |
207 | u32 sysstatus_ofs; | |
eaef7e3f LV |
208 | u32 mode_ofs; |
209 | u32 length_ofs; | |
0d373d60 MG |
210 | |
211 | u32 major_mask; | |
212 | u32 major_shift; | |
213 | u32 minor_mask; | |
214 | u32 minor_shift; | |
215 | }; | |
216 | ||
8628e7c8 DK |
217 | struct omap_sham_dev { |
218 | struct list_head list; | |
219 | unsigned long phys_base; | |
220 | struct device *dev; | |
221 | void __iomem *io_base; | |
222 | int irq; | |
8628e7c8 | 223 | spinlock_t lock; |
3e133c8b | 224 | int err; |
03feec9c | 225 | unsigned int dma; |
dfd061d5 | 226 | struct dma_chan *dma_lch; |
8628e7c8 | 227 | struct tasklet_struct done_task; |
b8411ccd | 228 | u8 polling_mode; |
8628e7c8 DK |
229 | |
230 | unsigned long flags; | |
231 | struct crypto_queue queue; | |
232 | struct ahash_request *req; | |
0d373d60 MG |
233 | |
234 | const struct omap_sham_pdata *pdata; | |
8628e7c8 DK |
235 | }; |
236 | ||
237 | struct omap_sham_drv { | |
238 | struct list_head dev_list; | |
239 | spinlock_t lock; | |
240 | unsigned long flags; | |
241 | }; | |
242 | ||
243 | static struct omap_sham_drv sham = { | |
244 | .dev_list = LIST_HEAD_INIT(sham.dev_list), | |
245 | .lock = __SPIN_LOCK_UNLOCKED(sham.lock), | |
246 | }; | |
247 | ||
248 | static inline u32 omap_sham_read(struct omap_sham_dev *dd, u32 offset) | |
249 | { | |
250 | return __raw_readl(dd->io_base + offset); | |
251 | } | |
252 | ||
253 | static inline void omap_sham_write(struct omap_sham_dev *dd, | |
254 | u32 offset, u32 value) | |
255 | { | |
256 | __raw_writel(value, dd->io_base + offset); | |
257 | } | |
258 | ||
259 | static inline void omap_sham_write_mask(struct omap_sham_dev *dd, u32 address, | |
260 | u32 value, u32 mask) | |
261 | { | |
262 | u32 val; | |
263 | ||
264 | val = omap_sham_read(dd, address); | |
265 | val &= ~mask; | |
266 | val |= value; | |
267 | omap_sham_write(dd, address, val); | |
268 | } | |
269 | ||
270 | static inline int omap_sham_wait(struct omap_sham_dev *dd, u32 offset, u32 bit) | |
271 | { | |
272 | unsigned long timeout = jiffies + DEFAULT_TIMEOUT_INTERVAL; | |
273 | ||
274 | while (!(omap_sham_read(dd, offset) & bit)) { | |
275 | if (time_is_before_jiffies(timeout)) | |
276 | return -ETIMEDOUT; | |
277 | } | |
278 | ||
279 | return 0; | |
280 | } | |
281 | ||
0d373d60 | 282 | static void omap_sham_copy_hash_omap2(struct ahash_request *req, int out) |
8628e7c8 DK |
283 | { |
284 | struct omap_sham_reqctx *ctx = ahash_request_ctx(req); | |
0d373d60 | 285 | struct omap_sham_dev *dd = ctx->dd; |
0c3cf4cc | 286 | u32 *hash = (u32 *)ctx->digest; |
8628e7c8 DK |
287 | int i; |
288 | ||
0d373d60 | 289 | for (i = 0; i < dd->pdata->digest_size / sizeof(u32); i++) { |
3c8d758a | 290 | if (out) |
0d373d60 | 291 | hash[i] = omap_sham_read(dd, SHA_REG_IDIGEST(dd, i)); |
3c8d758a | 292 | else |
0d373d60 MG |
293 | omap_sham_write(dd, SHA_REG_IDIGEST(dd, i), hash[i]); |
294 | } | |
295 | } | |
296 | ||
297 | static void omap_sham_copy_hash_omap4(struct ahash_request *req, int out) | |
298 | { | |
299 | struct omap_sham_reqctx *ctx = ahash_request_ctx(req); | |
300 | struct omap_sham_dev *dd = ctx->dd; | |
301 | int i; | |
302 | ||
303 | if (ctx->flags & BIT(FLAGS_HMAC)) { | |
304 | struct crypto_ahash *tfm = crypto_ahash_reqtfm(dd->req); | |
305 | struct omap_sham_ctx *tctx = crypto_ahash_ctx(tfm); | |
306 | struct omap_sham_hmac_ctx *bctx = tctx->base; | |
307 | u32 *opad = (u32 *)bctx->opad; | |
308 | ||
309 | for (i = 0; i < dd->pdata->digest_size / sizeof(u32); i++) { | |
310 | if (out) | |
311 | opad[i] = omap_sham_read(dd, | |
eaef7e3f | 312 | SHA_REG_ODIGEST(dd, i)); |
0d373d60 | 313 | else |
eaef7e3f | 314 | omap_sham_write(dd, SHA_REG_ODIGEST(dd, i), |
0d373d60 MG |
315 | opad[i]); |
316 | } | |
3c8d758a | 317 | } |
0d373d60 MG |
318 | |
319 | omap_sham_copy_hash_omap2(req, out); | |
3c8d758a DK |
320 | } |
321 | ||
322 | static void omap_sham_copy_ready_hash(struct ahash_request *req) | |
323 | { | |
324 | struct omap_sham_reqctx *ctx = ahash_request_ctx(req); | |
325 | u32 *in = (u32 *)ctx->digest; | |
326 | u32 *hash = (u32 *)req->result; | |
0d373d60 | 327 | int i, d, big_endian = 0; |
3c8d758a DK |
328 | |
329 | if (!hash) | |
330 | return; | |
331 | ||
0d373d60 MG |
332 | switch (ctx->flags & FLAGS_MODE_MASK) { |
333 | case FLAGS_MODE_MD5: | |
334 | d = MD5_DIGEST_SIZE / sizeof(u32); | |
335 | break; | |
336 | case FLAGS_MODE_SHA1: | |
337 | /* OMAP2 SHA1 is big endian */ | |
338 | if (test_bit(FLAGS_BE32_SHA1, &ctx->dd->flags)) | |
339 | big_endian = 1; | |
340 | d = SHA1_DIGEST_SIZE / sizeof(u32); | |
341 | break; | |
d20fb18b MG |
342 | case FLAGS_MODE_SHA224: |
343 | d = SHA224_DIGEST_SIZE / sizeof(u32); | |
344 | break; | |
345 | case FLAGS_MODE_SHA256: | |
346 | d = SHA256_DIGEST_SIZE / sizeof(u32); | |
347 | break; | |
eaef7e3f LV |
348 | case FLAGS_MODE_SHA384: |
349 | d = SHA384_DIGEST_SIZE / sizeof(u32); | |
350 | break; | |
351 | case FLAGS_MODE_SHA512: | |
352 | d = SHA512_DIGEST_SIZE / sizeof(u32); | |
353 | break; | |
0d373d60 MG |
354 | default: |
355 | d = 0; | |
356 | } | |
357 | ||
358 | if (big_endian) | |
359 | for (i = 0; i < d; i++) | |
3c8d758a | 360 | hash[i] = be32_to_cpu(in[i]); |
0d373d60 MG |
361 | else |
362 | for (i = 0; i < d; i++) | |
3c8d758a | 363 | hash[i] = le32_to_cpu(in[i]); |
8628e7c8 DK |
364 | } |
365 | ||
798eed5d | 366 | static int omap_sham_hw_init(struct omap_sham_dev *dd) |
8628e7c8 | 367 | { |
b359f034 | 368 | pm_runtime_get_sync(dd->dev); |
8628e7c8 | 369 | |
a929cbee | 370 | if (!test_bit(FLAGS_INIT, &dd->flags)) { |
a929cbee | 371 | set_bit(FLAGS_INIT, &dd->flags); |
798eed5d DK |
372 | dd->err = 0; |
373 | } | |
8628e7c8 | 374 | |
798eed5d DK |
375 | return 0; |
376 | } | |
377 | ||
0d373d60 | 378 | static void omap_sham_write_ctrl_omap2(struct omap_sham_dev *dd, size_t length, |
798eed5d DK |
379 | int final, int dma) |
380 | { | |
381 | struct omap_sham_reqctx *ctx = ahash_request_ctx(dd->req); | |
382 | u32 val = length << 5, mask; | |
383 | ||
384 | if (likely(ctx->digcnt)) | |
0d373d60 | 385 | omap_sham_write(dd, SHA_REG_DIGCNT(dd), ctx->digcnt); |
8628e7c8 | 386 | |
0d373d60 | 387 | omap_sham_write_mask(dd, SHA_REG_MASK(dd), |
8628e7c8 DK |
388 | SHA_REG_MASK_IT_EN | (dma ? SHA_REG_MASK_DMA_EN : 0), |
389 | SHA_REG_MASK_IT_EN | SHA_REG_MASK_DMA_EN); | |
390 | /* | |
391 | * Setting ALGO_CONST only for the first iteration | |
392 | * and CLOSE_HASH only for the last one. | |
393 | */ | |
0d373d60 | 394 | if ((ctx->flags & FLAGS_MODE_MASK) == FLAGS_MODE_SHA1) |
8628e7c8 DK |
395 | val |= SHA_REG_CTRL_ALGO; |
396 | if (!ctx->digcnt) | |
397 | val |= SHA_REG_CTRL_ALGO_CONST; | |
398 | if (final) | |
399 | val |= SHA_REG_CTRL_CLOSE_HASH; | |
400 | ||
401 | mask = SHA_REG_CTRL_ALGO_CONST | SHA_REG_CTRL_CLOSE_HASH | | |
402 | SHA_REG_CTRL_ALGO | SHA_REG_CTRL_LENGTH; | |
403 | ||
404 | omap_sham_write_mask(dd, SHA_REG_CTRL, val, mask); | |
8628e7c8 DK |
405 | } |
406 | ||
0d373d60 MG |
407 | static void omap_sham_trigger_omap2(struct omap_sham_dev *dd, size_t length) |
408 | { | |
409 | } | |
410 | ||
411 | static int omap_sham_poll_irq_omap2(struct omap_sham_dev *dd) | |
412 | { | |
413 | return omap_sham_wait(dd, SHA_REG_CTRL, SHA_REG_CTRL_INPUT_READY); | |
414 | } | |
415 | ||
eaef7e3f LV |
416 | static int get_block_size(struct omap_sham_reqctx *ctx) |
417 | { | |
418 | int d; | |
419 | ||
420 | switch (ctx->flags & FLAGS_MODE_MASK) { | |
421 | case FLAGS_MODE_MD5: | |
422 | case FLAGS_MODE_SHA1: | |
423 | d = SHA1_BLOCK_SIZE; | |
424 | break; | |
425 | case FLAGS_MODE_SHA224: | |
426 | case FLAGS_MODE_SHA256: | |
427 | d = SHA256_BLOCK_SIZE; | |
428 | break; | |
429 | case FLAGS_MODE_SHA384: | |
430 | case FLAGS_MODE_SHA512: | |
431 | d = SHA512_BLOCK_SIZE; | |
432 | break; | |
433 | default: | |
434 | d = 0; | |
435 | } | |
436 | ||
437 | return d; | |
438 | } | |
439 | ||
0d373d60 MG |
440 | static void omap_sham_write_n(struct omap_sham_dev *dd, u32 offset, |
441 | u32 *value, int count) | |
442 | { | |
443 | for (; count--; value++, offset += 4) | |
444 | omap_sham_write(dd, offset, *value); | |
445 | } | |
446 | ||
447 | static void omap_sham_write_ctrl_omap4(struct omap_sham_dev *dd, size_t length, | |
448 | int final, int dma) | |
449 | { | |
450 | struct omap_sham_reqctx *ctx = ahash_request_ctx(dd->req); | |
451 | u32 val, mask; | |
452 | ||
453 | /* | |
454 | * Setting ALGO_CONST only for the first iteration and | |
455 | * CLOSE_HASH only for the last one. Note that flags mode bits | |
456 | * correspond to algorithm encoding in mode register. | |
457 | */ | |
eaef7e3f | 458 | val = (ctx->flags & FLAGS_MODE_MASK) >> (FLAGS_MODE_SHIFT); |
0d373d60 MG |
459 | if (!ctx->digcnt) { |
460 | struct crypto_ahash *tfm = crypto_ahash_reqtfm(dd->req); | |
461 | struct omap_sham_ctx *tctx = crypto_ahash_ctx(tfm); | |
462 | struct omap_sham_hmac_ctx *bctx = tctx->base; | |
eaef7e3f | 463 | int bs, nr_dr; |
0d373d60 MG |
464 | |
465 | val |= SHA_REG_MODE_ALGO_CONSTANT; | |
466 | ||
467 | if (ctx->flags & BIT(FLAGS_HMAC)) { | |
eaef7e3f LV |
468 | bs = get_block_size(ctx); |
469 | nr_dr = bs / (2 * sizeof(u32)); | |
0d373d60 | 470 | val |= SHA_REG_MODE_HMAC_KEY_PROC; |
eaef7e3f LV |
471 | omap_sham_write_n(dd, SHA_REG_ODIGEST(dd, 0), |
472 | (u32 *)bctx->ipad, nr_dr); | |
473 | omap_sham_write_n(dd, SHA_REG_IDIGEST(dd, 0), | |
474 | (u32 *)bctx->ipad + nr_dr, nr_dr); | |
475 | ctx->digcnt += bs; | |
0d373d60 MG |
476 | } |
477 | } | |
478 | ||
479 | if (final) { | |
480 | val |= SHA_REG_MODE_CLOSE_HASH; | |
481 | ||
482 | if (ctx->flags & BIT(FLAGS_HMAC)) | |
483 | val |= SHA_REG_MODE_HMAC_OUTER_HASH; | |
484 | } | |
485 | ||
486 | mask = SHA_REG_MODE_ALGO_CONSTANT | SHA_REG_MODE_CLOSE_HASH | | |
487 | SHA_REG_MODE_ALGO_MASK | SHA_REG_MODE_HMAC_OUTER_HASH | | |
488 | SHA_REG_MODE_HMAC_KEY_PROC; | |
489 | ||
490 | dev_dbg(dd->dev, "ctrl: %08x, flags: %08lx\n", val, ctx->flags); | |
eaef7e3f | 491 | omap_sham_write_mask(dd, SHA_REG_MODE(dd), val, mask); |
0d373d60 MG |
492 | omap_sham_write(dd, SHA_REG_IRQENA, SHA_REG_IRQENA_OUTPUT_RDY); |
493 | omap_sham_write_mask(dd, SHA_REG_MASK(dd), | |
494 | SHA_REG_MASK_IT_EN | | |
495 | (dma ? SHA_REG_MASK_DMA_EN : 0), | |
496 | SHA_REG_MASK_IT_EN | SHA_REG_MASK_DMA_EN); | |
497 | } | |
498 | ||
499 | static void omap_sham_trigger_omap4(struct omap_sham_dev *dd, size_t length) | |
500 | { | |
eaef7e3f | 501 | omap_sham_write(dd, SHA_REG_LENGTH(dd), length); |
0d373d60 MG |
502 | } |
503 | ||
504 | static int omap_sham_poll_irq_omap4(struct omap_sham_dev *dd) | |
505 | { | |
506 | return omap_sham_wait(dd, SHA_REG_IRQSTATUS, | |
507 | SHA_REG_IRQSTATUS_INPUT_RDY); | |
508 | } | |
509 | ||
8628e7c8 DK |
510 | static int omap_sham_xmit_cpu(struct omap_sham_dev *dd, const u8 *buf, |
511 | size_t length, int final) | |
512 | { | |
513 | struct omap_sham_reqctx *ctx = ahash_request_ctx(dd->req); | |
b8411ccd | 514 | int count, len32, bs32, offset = 0; |
8628e7c8 DK |
515 | const u32 *buffer = (const u32 *)buf; |
516 | ||
517 | dev_dbg(dd->dev, "xmit_cpu: digcnt: %d, length: %d, final: %d\n", | |
518 | ctx->digcnt, length, final); | |
519 | ||
0d373d60 MG |
520 | dd->pdata->write_ctrl(dd, length, final, 0); |
521 | dd->pdata->trigger(dd, length); | |
8628e7c8 | 522 | |
3e133c8b DK |
523 | /* should be non-zero before next lines to disable clocks later */ |
524 | ctx->digcnt += length; | |
525 | ||
8628e7c8 | 526 | if (final) |
ed3ea9a8 | 527 | set_bit(FLAGS_FINAL, &dd->flags); /* catch last interrupt */ |
8628e7c8 | 528 | |
6c63db82 DK |
529 | set_bit(FLAGS_CPU, &dd->flags); |
530 | ||
8628e7c8 | 531 | len32 = DIV_ROUND_UP(length, sizeof(u32)); |
b8411ccd LV |
532 | bs32 = get_block_size(ctx) / sizeof(u32); |
533 | ||
534 | while (len32) { | |
535 | if (dd->pdata->poll_irq(dd)) | |
536 | return -ETIMEDOUT; | |
8628e7c8 | 537 | |
b8411ccd LV |
538 | for (count = 0; count < min(len32, bs32); count++, offset++) |
539 | omap_sham_write(dd, SHA_REG_DIN(dd, count), | |
540 | buffer[offset]); | |
541 | len32 -= min(len32, bs32); | |
542 | } | |
8628e7c8 DK |
543 | |
544 | return -EINPROGRESS; | |
545 | } | |
546 | ||
dfd061d5 MG |
547 | static void omap_sham_dma_callback(void *param) |
548 | { | |
549 | struct omap_sham_dev *dd = param; | |
550 | ||
551 | set_bit(FLAGS_DMA_READY, &dd->flags); | |
552 | tasklet_schedule(&dd->done_task); | |
553 | } | |
dfd061d5 | 554 | |
8628e7c8 | 555 | static int omap_sham_xmit_dma(struct omap_sham_dev *dd, dma_addr_t dma_addr, |
dfd061d5 | 556 | size_t length, int final, int is_sg) |
8628e7c8 DK |
557 | { |
558 | struct omap_sham_reqctx *ctx = ahash_request_ctx(dd->req); | |
dfd061d5 MG |
559 | struct dma_async_tx_descriptor *tx; |
560 | struct dma_slave_config cfg; | |
561 | int len32, ret; | |
8628e7c8 DK |
562 | |
563 | dev_dbg(dd->dev, "xmit_dma: digcnt: %d, length: %d, final: %d\n", | |
564 | ctx->digcnt, length, final); | |
8628e7c8 | 565 | |
dfd061d5 MG |
566 | memset(&cfg, 0, sizeof(cfg)); |
567 | ||
0d373d60 | 568 | cfg.dst_addr = dd->phys_base + SHA_REG_DIN(dd, 0); |
dfd061d5 MG |
569 | cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES; |
570 | cfg.dst_maxburst = DST_MAXBURST; | |
571 | ||
572 | ret = dmaengine_slave_config(dd->dma_lch, &cfg); | |
573 | if (ret) { | |
574 | pr_err("omap-sham: can't configure dmaengine slave: %d\n", ret); | |
575 | return ret; | |
576 | } | |
577 | ||
578 | len32 = DIV_ROUND_UP(length, DMA_MIN) * DMA_MIN; | |
579 | ||
580 | if (is_sg) { | |
581 | /* | |
582 | * The SG entry passed in may not have the 'length' member | |
583 | * set correctly so use a local SG entry (sgl) with the | |
584 | * proper value for 'length' instead. If this is not done, | |
585 | * the dmaengine may try to DMA the incorrect amount of data. | |
586 | */ | |
587 | sg_init_table(&ctx->sgl, 1); | |
588 | ctx->sgl.page_link = ctx->sg->page_link; | |
589 | ctx->sgl.offset = ctx->sg->offset; | |
590 | sg_dma_len(&ctx->sgl) = len32; | |
591 | sg_dma_address(&ctx->sgl) = sg_dma_address(ctx->sg); | |
592 | ||
593 | tx = dmaengine_prep_slave_sg(dd->dma_lch, &ctx->sgl, 1, | |
594 | DMA_MEM_TO_DEV, DMA_PREP_INTERRUPT | DMA_CTRL_ACK); | |
595 | } else { | |
596 | tx = dmaengine_prep_slave_single(dd->dma_lch, dma_addr, len32, | |
597 | DMA_MEM_TO_DEV, DMA_PREP_INTERRUPT | DMA_CTRL_ACK); | |
598 | } | |
8628e7c8 | 599 | |
dfd061d5 MG |
600 | if (!tx) { |
601 | dev_err(dd->dev, "prep_slave_sg/single() failed\n"); | |
602 | return -EINVAL; | |
603 | } | |
8628e7c8 | 604 | |
dfd061d5 MG |
605 | tx->callback = omap_sham_dma_callback; |
606 | tx->callback_param = dd; | |
8628e7c8 | 607 | |
0d373d60 | 608 | dd->pdata->write_ctrl(dd, length, final, 1); |
8628e7c8 DK |
609 | |
610 | ctx->digcnt += length; | |
611 | ||
612 | if (final) | |
ed3ea9a8 | 613 | set_bit(FLAGS_FINAL, &dd->flags); /* catch last interrupt */ |
8628e7c8 | 614 | |
a929cbee | 615 | set_bit(FLAGS_DMA_ACTIVE, &dd->flags); |
8628e7c8 | 616 | |
dfd061d5 MG |
617 | dmaengine_submit(tx); |
618 | dma_async_issue_pending(dd->dma_lch); | |
8628e7c8 | 619 | |
0d373d60 | 620 | dd->pdata->trigger(dd, length); |
8628e7c8 DK |
621 | |
622 | return -EINPROGRESS; | |
623 | } | |
624 | ||
625 | static size_t omap_sham_append_buffer(struct omap_sham_reqctx *ctx, | |
626 | const u8 *data, size_t length) | |
627 | { | |
628 | size_t count = min(length, ctx->buflen - ctx->bufcnt); | |
629 | ||
630 | count = min(count, ctx->total); | |
631 | if (count <= 0) | |
632 | return 0; | |
633 | memcpy(ctx->buffer + ctx->bufcnt, data, count); | |
634 | ctx->bufcnt += count; | |
635 | ||
636 | return count; | |
637 | } | |
638 | ||
639 | static size_t omap_sham_append_sg(struct omap_sham_reqctx *ctx) | |
640 | { | |
641 | size_t count; | |
642 | ||
643 | while (ctx->sg) { | |
644 | count = omap_sham_append_buffer(ctx, | |
645 | sg_virt(ctx->sg) + ctx->offset, | |
646 | ctx->sg->length - ctx->offset); | |
647 | if (!count) | |
648 | break; | |
649 | ctx->offset += count; | |
650 | ctx->total -= count; | |
651 | if (ctx->offset == ctx->sg->length) { | |
652 | ctx->sg = sg_next(ctx->sg); | |
653 | if (ctx->sg) | |
654 | ctx->offset = 0; | |
655 | else | |
656 | ctx->total = 0; | |
657 | } | |
658 | } | |
659 | ||
660 | return 0; | |
661 | } | |
662 | ||
798eed5d DK |
663 | static int omap_sham_xmit_dma_map(struct omap_sham_dev *dd, |
664 | struct omap_sham_reqctx *ctx, | |
665 | size_t length, int final) | |
666 | { | |
dfd061d5 MG |
667 | int ret; |
668 | ||
798eed5d DK |
669 | ctx->dma_addr = dma_map_single(dd->dev, ctx->buffer, ctx->buflen, |
670 | DMA_TO_DEVICE); | |
671 | if (dma_mapping_error(dd->dev, ctx->dma_addr)) { | |
672 | dev_err(dd->dev, "dma %u bytes error\n", ctx->buflen); | |
673 | return -EINVAL; | |
674 | } | |
675 | ||
ea1fd224 | 676 | ctx->flags &= ~BIT(FLAGS_SG); |
887c883e | 677 | |
dfd061d5 | 678 | ret = omap_sham_xmit_dma(dd, ctx->dma_addr, length, final, 0); |
0d373d60 | 679 | if (ret != -EINPROGRESS) |
dfd061d5 MG |
680 | dma_unmap_single(dd->dev, ctx->dma_addr, ctx->buflen, |
681 | DMA_TO_DEVICE); | |
682 | ||
683 | return ret; | |
798eed5d DK |
684 | } |
685 | ||
8628e7c8 DK |
686 | static int omap_sham_update_dma_slow(struct omap_sham_dev *dd) |
687 | { | |
688 | struct omap_sham_reqctx *ctx = ahash_request_ctx(dd->req); | |
689 | unsigned int final; | |
690 | size_t count; | |
691 | ||
8628e7c8 DK |
692 | omap_sham_append_sg(ctx); |
693 | ||
ea1fd224 | 694 | final = (ctx->flags & BIT(FLAGS_FINUP)) && !ctx->total; |
8628e7c8 DK |
695 | |
696 | dev_dbg(dd->dev, "slow: bufcnt: %u, digcnt: %d, final: %d\n", | |
697 | ctx->bufcnt, ctx->digcnt, final); | |
698 | ||
699 | if (final || (ctx->bufcnt == ctx->buflen && ctx->total)) { | |
700 | count = ctx->bufcnt; | |
701 | ctx->bufcnt = 0; | |
798eed5d | 702 | return omap_sham_xmit_dma_map(dd, ctx, count, final); |
8628e7c8 DK |
703 | } |
704 | ||
705 | return 0; | |
706 | } | |
707 | ||
887c883e DK |
708 | /* Start address alignment */ |
709 | #define SG_AA(sg) (IS_ALIGNED(sg->offset, sizeof(u32))) | |
710 | /* SHA1 block size alignment */ | |
eaef7e3f | 711 | #define SG_SA(sg, bs) (IS_ALIGNED(sg->length, bs)) |
887c883e DK |
712 | |
713 | static int omap_sham_update_dma_start(struct omap_sham_dev *dd) | |
8628e7c8 DK |
714 | { |
715 | struct omap_sham_reqctx *ctx = ahash_request_ctx(dd->req); | |
887c883e DK |
716 | unsigned int length, final, tail; |
717 | struct scatterlist *sg; | |
eaef7e3f | 718 | int ret, bs; |
887c883e DK |
719 | |
720 | if (!ctx->total) | |
721 | return 0; | |
722 | ||
723 | if (ctx->bufcnt || ctx->offset) | |
724 | return omap_sham_update_dma_slow(dd); | |
725 | ||
dfd061d5 MG |
726 | /* |
727 | * Don't use the sg interface when the transfer size is less | |
728 | * than the number of elements in a DMA frame. Otherwise, | |
729 | * the dmaengine infrastructure will calculate that it needs | |
730 | * to transfer 0 frames which ultimately fails. | |
731 | */ | |
732 | if (ctx->total < (DST_MAXBURST * sizeof(u32))) | |
733 | return omap_sham_update_dma_slow(dd); | |
dfd061d5 | 734 | |
887c883e DK |
735 | dev_dbg(dd->dev, "fast: digcnt: %d, bufcnt: %u, total: %u\n", |
736 | ctx->digcnt, ctx->bufcnt, ctx->total); | |
737 | ||
738 | sg = ctx->sg; | |
eaef7e3f | 739 | bs = get_block_size(ctx); |
8628e7c8 | 740 | |
887c883e DK |
741 | if (!SG_AA(sg)) |
742 | return omap_sham_update_dma_slow(dd); | |
8628e7c8 | 743 | |
eaef7e3f LV |
744 | if (!sg_is_last(sg) && !SG_SA(sg, bs)) |
745 | /* size is not BLOCK_SIZE aligned */ | |
887c883e DK |
746 | return omap_sham_update_dma_slow(dd); |
747 | ||
748 | length = min(ctx->total, sg->length); | |
749 | ||
750 | if (sg_is_last(sg)) { | |
ea1fd224 | 751 | if (!(ctx->flags & BIT(FLAGS_FINUP))) { |
eaef7e3f LV |
752 | /* not last sg must be BLOCK_SIZE aligned */ |
753 | tail = length & (bs - 1); | |
887c883e DK |
754 | /* without finup() we need one block to close hash */ |
755 | if (!tail) | |
eaef7e3f | 756 | tail = bs; |
887c883e DK |
757 | length -= tail; |
758 | } | |
759 | } | |
8628e7c8 DK |
760 | |
761 | if (!dma_map_sg(dd->dev, ctx->sg, 1, DMA_TO_DEVICE)) { | |
762 | dev_err(dd->dev, "dma_map_sg error\n"); | |
763 | return -EINVAL; | |
764 | } | |
765 | ||
ea1fd224 | 766 | ctx->flags |= BIT(FLAGS_SG); |
887c883e | 767 | |
8628e7c8 | 768 | ctx->total -= length; |
887c883e DK |
769 | ctx->offset = length; /* offset where to start slow */ |
770 | ||
ea1fd224 | 771 | final = (ctx->flags & BIT(FLAGS_FINUP)) && !ctx->total; |
8628e7c8 | 772 | |
dfd061d5 | 773 | ret = omap_sham_xmit_dma(dd, sg_dma_address(ctx->sg), length, final, 1); |
0d373d60 | 774 | if (ret != -EINPROGRESS) |
dfd061d5 MG |
775 | dma_unmap_sg(dd->dev, ctx->sg, 1, DMA_TO_DEVICE); |
776 | ||
777 | return ret; | |
8628e7c8 DK |
778 | } |
779 | ||
780 | static int omap_sham_update_cpu(struct omap_sham_dev *dd) | |
781 | { | |
782 | struct omap_sham_reqctx *ctx = ahash_request_ctx(dd->req); | |
b8411ccd LV |
783 | int bufcnt, final; |
784 | ||
785 | if (!ctx->total) | |
786 | return 0; | |
8628e7c8 DK |
787 | |
788 | omap_sham_append_sg(ctx); | |
b8411ccd LV |
789 | |
790 | final = (ctx->flags & BIT(FLAGS_FINUP)) && !ctx->total; | |
791 | ||
792 | dev_dbg(dd->dev, "cpu: bufcnt: %u, digcnt: %d, final: %d\n", | |
793 | ctx->bufcnt, ctx->digcnt, final); | |
794 | ||
8628e7c8 DK |
795 | bufcnt = ctx->bufcnt; |
796 | ctx->bufcnt = 0; | |
797 | ||
b8411ccd | 798 | return omap_sham_xmit_cpu(dd, ctx->buffer, bufcnt, final); |
8628e7c8 DK |
799 | } |
800 | ||
801 | static int omap_sham_update_dma_stop(struct omap_sham_dev *dd) | |
802 | { | |
803 | struct omap_sham_reqctx *ctx = ahash_request_ctx(dd->req); | |
804 | ||
dfd061d5 | 805 | dmaengine_terminate_all(dd->dma_lch); |
dfd061d5 | 806 | |
ea1fd224 | 807 | if (ctx->flags & BIT(FLAGS_SG)) { |
8628e7c8 | 808 | dma_unmap_sg(dd->dev, ctx->sg, 1, DMA_TO_DEVICE); |
887c883e DK |
809 | if (ctx->sg->length == ctx->offset) { |
810 | ctx->sg = sg_next(ctx->sg); | |
811 | if (ctx->sg) | |
812 | ctx->offset = 0; | |
813 | } | |
814 | } else { | |
798eed5d DK |
815 | dma_unmap_single(dd->dev, ctx->dma_addr, ctx->buflen, |
816 | DMA_TO_DEVICE); | |
887c883e | 817 | } |
8628e7c8 DK |
818 | |
819 | return 0; | |
820 | } | |
821 | ||
8628e7c8 DK |
822 | static int omap_sham_init(struct ahash_request *req) |
823 | { | |
824 | struct crypto_ahash *tfm = crypto_ahash_reqtfm(req); | |
825 | struct omap_sham_ctx *tctx = crypto_ahash_ctx(tfm); | |
826 | struct omap_sham_reqctx *ctx = ahash_request_ctx(req); | |
827 | struct omap_sham_dev *dd = NULL, *tmp; | |
eaef7e3f | 828 | int bs = 0; |
8628e7c8 DK |
829 | |
830 | spin_lock_bh(&sham.lock); | |
831 | if (!tctx->dd) { | |
832 | list_for_each_entry(tmp, &sham.dev_list, list) { | |
833 | dd = tmp; | |
834 | break; | |
835 | } | |
836 | tctx->dd = dd; | |
837 | } else { | |
838 | dd = tctx->dd; | |
839 | } | |
840 | spin_unlock_bh(&sham.lock); | |
841 | ||
842 | ctx->dd = dd; | |
843 | ||
844 | ctx->flags = 0; | |
845 | ||
8628e7c8 DK |
846 | dev_dbg(dd->dev, "init: digest size: %d\n", |
847 | crypto_ahash_digestsize(tfm)); | |
848 | ||
0d373d60 MG |
849 | switch (crypto_ahash_digestsize(tfm)) { |
850 | case MD5_DIGEST_SIZE: | |
851 | ctx->flags |= FLAGS_MODE_MD5; | |
eaef7e3f | 852 | bs = SHA1_BLOCK_SIZE; |
0d373d60 MG |
853 | break; |
854 | case SHA1_DIGEST_SIZE: | |
855 | ctx->flags |= FLAGS_MODE_SHA1; | |
eaef7e3f | 856 | bs = SHA1_BLOCK_SIZE; |
0d373d60 | 857 | break; |
d20fb18b MG |
858 | case SHA224_DIGEST_SIZE: |
859 | ctx->flags |= FLAGS_MODE_SHA224; | |
eaef7e3f | 860 | bs = SHA224_BLOCK_SIZE; |
d20fb18b MG |
861 | break; |
862 | case SHA256_DIGEST_SIZE: | |
863 | ctx->flags |= FLAGS_MODE_SHA256; | |
eaef7e3f LV |
864 | bs = SHA256_BLOCK_SIZE; |
865 | break; | |
866 | case SHA384_DIGEST_SIZE: | |
867 | ctx->flags |= FLAGS_MODE_SHA384; | |
868 | bs = SHA384_BLOCK_SIZE; | |
869 | break; | |
870 | case SHA512_DIGEST_SIZE: | |
871 | ctx->flags |= FLAGS_MODE_SHA512; | |
872 | bs = SHA512_BLOCK_SIZE; | |
d20fb18b | 873 | break; |
0d373d60 | 874 | } |
8628e7c8 DK |
875 | |
876 | ctx->bufcnt = 0; | |
877 | ctx->digcnt = 0; | |
798eed5d | 878 | ctx->buflen = BUFLEN; |
8628e7c8 | 879 | |
ea1fd224 | 880 | if (tctx->flags & BIT(FLAGS_HMAC)) { |
0d373d60 MG |
881 | if (!test_bit(FLAGS_AUTO_XOR, &dd->flags)) { |
882 | struct omap_sham_hmac_ctx *bctx = tctx->base; | |
883 | ||
eaef7e3f LV |
884 | memcpy(ctx->buffer, bctx->ipad, bs); |
885 | ctx->bufcnt = bs; | |
0d373d60 | 886 | } |
8628e7c8 | 887 | |
ea1fd224 | 888 | ctx->flags |= BIT(FLAGS_HMAC); |
8628e7c8 DK |
889 | } |
890 | ||
891 | return 0; | |
892 | ||
893 | } | |
894 | ||
895 | static int omap_sham_update_req(struct omap_sham_dev *dd) | |
896 | { | |
897 | struct ahash_request *req = dd->req; | |
898 | struct omap_sham_reqctx *ctx = ahash_request_ctx(req); | |
899 | int err; | |
900 | ||
901 | dev_dbg(dd->dev, "update_req: total: %u, digcnt: %d, finup: %d\n", | |
ea1fd224 | 902 | ctx->total, ctx->digcnt, (ctx->flags & BIT(FLAGS_FINUP)) != 0); |
8628e7c8 | 903 | |
ea1fd224 | 904 | if (ctx->flags & BIT(FLAGS_CPU)) |
8628e7c8 | 905 | err = omap_sham_update_cpu(dd); |
8628e7c8 | 906 | else |
887c883e | 907 | err = omap_sham_update_dma_start(dd); |
8628e7c8 DK |
908 | |
909 | /* wait for dma completion before can take more data */ | |
910 | dev_dbg(dd->dev, "update: err: %d, digcnt: %d\n", err, ctx->digcnt); | |
911 | ||
912 | return err; | |
913 | } | |
914 | ||
915 | static int omap_sham_final_req(struct omap_sham_dev *dd) | |
916 | { | |
917 | struct ahash_request *req = dd->req; | |
918 | struct omap_sham_reqctx *ctx = ahash_request_ctx(req); | |
919 | int err = 0, use_dma = 1; | |
920 | ||
b8411ccd LV |
921 | if ((ctx->bufcnt <= get_block_size(ctx)) || dd->polling_mode) |
922 | /* | |
923 | * faster to handle last block with cpu or | |
924 | * use cpu when dma is not present. | |
925 | */ | |
8628e7c8 DK |
926 | use_dma = 0; |
927 | ||
928 | if (use_dma) | |
798eed5d | 929 | err = omap_sham_xmit_dma_map(dd, ctx, ctx->bufcnt, 1); |
8628e7c8 DK |
930 | else |
931 | err = omap_sham_xmit_cpu(dd, ctx->buffer, ctx->bufcnt, 1); | |
932 | ||
933 | ctx->bufcnt = 0; | |
934 | ||
8628e7c8 DK |
935 | dev_dbg(dd->dev, "final_req: err: %d\n", err); |
936 | ||
937 | return err; | |
938 | } | |
939 | ||
bf362759 | 940 | static int omap_sham_finish_hmac(struct ahash_request *req) |
8628e7c8 DK |
941 | { |
942 | struct omap_sham_ctx *tctx = crypto_tfm_ctx(req->base.tfm); | |
943 | struct omap_sham_hmac_ctx *bctx = tctx->base; | |
944 | int bs = crypto_shash_blocksize(bctx->shash); | |
945 | int ds = crypto_shash_digestsize(bctx->shash); | |
946 | struct { | |
947 | struct shash_desc shash; | |
948 | char ctx[crypto_shash_descsize(bctx->shash)]; | |
949 | } desc; | |
950 | ||
951 | desc.shash.tfm = bctx->shash; | |
952 | desc.shash.flags = 0; /* not CRYPTO_TFM_REQ_MAY_SLEEP */ | |
953 | ||
954 | return crypto_shash_init(&desc.shash) ?: | |
955 | crypto_shash_update(&desc.shash, bctx->opad, bs) ?: | |
bf362759 DK |
956 | crypto_shash_finup(&desc.shash, req->result, ds, req->result); |
957 | } | |
958 | ||
959 | static int omap_sham_finish(struct ahash_request *req) | |
960 | { | |
961 | struct omap_sham_reqctx *ctx = ahash_request_ctx(req); | |
962 | struct omap_sham_dev *dd = ctx->dd; | |
963 | int err = 0; | |
964 | ||
965 | if (ctx->digcnt) { | |
966 | omap_sham_copy_ready_hash(req); | |
0d373d60 MG |
967 | if ((ctx->flags & BIT(FLAGS_HMAC)) && |
968 | !test_bit(FLAGS_AUTO_XOR, &dd->flags)) | |
bf362759 DK |
969 | err = omap_sham_finish_hmac(req); |
970 | } | |
971 | ||
972 | dev_dbg(dd->dev, "digcnt: %d, bufcnt: %d\n", ctx->digcnt, ctx->bufcnt); | |
973 | ||
974 | return err; | |
8628e7c8 DK |
975 | } |
976 | ||
977 | static void omap_sham_finish_req(struct ahash_request *req, int err) | |
978 | { | |
979 | struct omap_sham_reqctx *ctx = ahash_request_ctx(req); | |
798eed5d | 980 | struct omap_sham_dev *dd = ctx->dd; |
8628e7c8 DK |
981 | |
982 | if (!err) { | |
0d373d60 | 983 | dd->pdata->copy_hash(req, 1); |
ed3ea9a8 | 984 | if (test_bit(FLAGS_FINAL, &dd->flags)) |
bf362759 | 985 | err = omap_sham_finish(req); |
3e133c8b | 986 | } else { |
ea1fd224 | 987 | ctx->flags |= BIT(FLAGS_ERROR); |
8628e7c8 DK |
988 | } |
989 | ||
0efd4d8a DK |
990 | /* atomic operation is not needed here */ |
991 | dd->flags &= ~(BIT(FLAGS_BUSY) | BIT(FLAGS_FINAL) | BIT(FLAGS_CPU) | | |
992 | BIT(FLAGS_DMA_READY) | BIT(FLAGS_OUTPUT_READY)); | |
b359f034 | 993 | |
e68af482 | 994 | pm_runtime_put(dd->dev); |
8628e7c8 DK |
995 | |
996 | if (req->base.complete) | |
997 | req->base.complete(&req->base, err); | |
6cb3ffe1 DK |
998 | |
999 | /* handle new request */ | |
1000 | tasklet_schedule(&dd->done_task); | |
8628e7c8 DK |
1001 | } |
1002 | ||
a5d87237 DK |
1003 | static int omap_sham_handle_queue(struct omap_sham_dev *dd, |
1004 | struct ahash_request *req) | |
8628e7c8 | 1005 | { |
6c39d116 | 1006 | struct crypto_async_request *async_req, *backlog; |
8628e7c8 | 1007 | struct omap_sham_reqctx *ctx; |
8628e7c8 | 1008 | unsigned long flags; |
a5d87237 | 1009 | int err = 0, ret = 0; |
8628e7c8 DK |
1010 | |
1011 | spin_lock_irqsave(&dd->lock, flags); | |
a5d87237 DK |
1012 | if (req) |
1013 | ret = ahash_enqueue_request(&dd->queue, req); | |
a929cbee | 1014 | if (test_bit(FLAGS_BUSY, &dd->flags)) { |
a5d87237 DK |
1015 | spin_unlock_irqrestore(&dd->lock, flags); |
1016 | return ret; | |
1017 | } | |
6c39d116 | 1018 | backlog = crypto_get_backlog(&dd->queue); |
8628e7c8 | 1019 | async_req = crypto_dequeue_request(&dd->queue); |
6c39d116 | 1020 | if (async_req) |
a929cbee | 1021 | set_bit(FLAGS_BUSY, &dd->flags); |
8628e7c8 DK |
1022 | spin_unlock_irqrestore(&dd->lock, flags); |
1023 | ||
1024 | if (!async_req) | |
a5d87237 | 1025 | return ret; |
8628e7c8 DK |
1026 | |
1027 | if (backlog) | |
1028 | backlog->complete(backlog, -EINPROGRESS); | |
1029 | ||
1030 | req = ahash_request_cast(async_req); | |
8628e7c8 | 1031 | dd->req = req; |
8628e7c8 DK |
1032 | ctx = ahash_request_ctx(req); |
1033 | ||
1034 | dev_dbg(dd->dev, "handling new req, op: %lu, nbytes: %d\n", | |
1035 | ctx->op, req->nbytes); | |
1036 | ||
798eed5d DK |
1037 | err = omap_sham_hw_init(dd); |
1038 | if (err) | |
1039 | goto err1; | |
1040 | ||
798eed5d | 1041 | if (ctx->digcnt) |
8628e7c8 | 1042 | /* request has changed - restore hash */ |
0d373d60 | 1043 | dd->pdata->copy_hash(req, 0); |
8628e7c8 DK |
1044 | |
1045 | if (ctx->op == OP_UPDATE) { | |
1046 | err = omap_sham_update_req(dd); | |
ea1fd224 | 1047 | if (err != -EINPROGRESS && (ctx->flags & BIT(FLAGS_FINUP))) |
8628e7c8 DK |
1048 | /* no final() after finup() */ |
1049 | err = omap_sham_final_req(dd); | |
1050 | } else if (ctx->op == OP_FINAL) { | |
1051 | err = omap_sham_final_req(dd); | |
1052 | } | |
798eed5d | 1053 | err1: |
6cb3ffe1 | 1054 | if (err != -EINPROGRESS) |
8628e7c8 DK |
1055 | /* done_task will not finish it, so do it here */ |
1056 | omap_sham_finish_req(req, err); | |
8628e7c8 DK |
1057 | |
1058 | dev_dbg(dd->dev, "exit, err: %d\n", err); | |
1059 | ||
a5d87237 | 1060 | return ret; |
8628e7c8 DK |
1061 | } |
1062 | ||
1063 | static int omap_sham_enqueue(struct ahash_request *req, unsigned int op) | |
1064 | { | |
1065 | struct omap_sham_reqctx *ctx = ahash_request_ctx(req); | |
1066 | struct omap_sham_ctx *tctx = crypto_tfm_ctx(req->base.tfm); | |
1067 | struct omap_sham_dev *dd = tctx->dd; | |
8628e7c8 DK |
1068 | |
1069 | ctx->op = op; | |
1070 | ||
a5d87237 | 1071 | return omap_sham_handle_queue(dd, req); |
8628e7c8 DK |
1072 | } |
1073 | ||
1074 | static int omap_sham_update(struct ahash_request *req) | |
1075 | { | |
1076 | struct omap_sham_reqctx *ctx = ahash_request_ctx(req); | |
b8411ccd | 1077 | struct omap_sham_dev *dd = ctx->dd; |
eaef7e3f | 1078 | int bs = get_block_size(ctx); |
8628e7c8 DK |
1079 | |
1080 | if (!req->nbytes) | |
1081 | return 0; | |
1082 | ||
1083 | ctx->total = req->nbytes; | |
1084 | ctx->sg = req->src; | |
1085 | ctx->offset = 0; | |
1086 | ||
ea1fd224 | 1087 | if (ctx->flags & BIT(FLAGS_FINUP)) { |
8628e7c8 DK |
1088 | if ((ctx->digcnt + ctx->bufcnt + ctx->total) < 9) { |
1089 | /* | |
1090 | * OMAP HW accel works only with buffers >= 9 | |
1091 | * will switch to bypass in final() | |
1092 | * final has the same request and data | |
1093 | */ | |
1094 | omap_sham_append_sg(ctx); | |
1095 | return 0; | |
b8411ccd LV |
1096 | } else if ((ctx->bufcnt + ctx->total <= bs) || |
1097 | dd->polling_mode) { | |
887c883e | 1098 | /* |
b8411ccd LV |
1099 | * faster to use CPU for short transfers or |
1100 | * use cpu when dma is not present. | |
1101 | */ | |
ea1fd224 | 1102 | ctx->flags |= BIT(FLAGS_CPU); |
8628e7c8 | 1103 | } |
887c883e | 1104 | } else if (ctx->bufcnt + ctx->total < ctx->buflen) { |
8628e7c8 DK |
1105 | omap_sham_append_sg(ctx); |
1106 | return 0; | |
1107 | } | |
1108 | ||
1109 | return omap_sham_enqueue(req, OP_UPDATE); | |
1110 | } | |
1111 | ||
1112 | static int omap_sham_shash_digest(struct crypto_shash *shash, u32 flags, | |
1113 | const u8 *data, unsigned int len, u8 *out) | |
1114 | { | |
1115 | struct { | |
1116 | struct shash_desc shash; | |
1117 | char ctx[crypto_shash_descsize(shash)]; | |
1118 | } desc; | |
1119 | ||
1120 | desc.shash.tfm = shash; | |
1121 | desc.shash.flags = flags & CRYPTO_TFM_REQ_MAY_SLEEP; | |
1122 | ||
1123 | return crypto_shash_digest(&desc.shash, data, len, out); | |
1124 | } | |
1125 | ||
1126 | static int omap_sham_final_shash(struct ahash_request *req) | |
1127 | { | |
1128 | struct omap_sham_ctx *tctx = crypto_tfm_ctx(req->base.tfm); | |
1129 | struct omap_sham_reqctx *ctx = ahash_request_ctx(req); | |
1130 | ||
1131 | return omap_sham_shash_digest(tctx->fallback, req->base.flags, | |
1132 | ctx->buffer, ctx->bufcnt, req->result); | |
1133 | } | |
1134 | ||
1135 | static int omap_sham_final(struct ahash_request *req) | |
1136 | { | |
1137 | struct omap_sham_reqctx *ctx = ahash_request_ctx(req); | |
8628e7c8 | 1138 | |
ea1fd224 | 1139 | ctx->flags |= BIT(FLAGS_FINUP); |
8628e7c8 | 1140 | |
ea1fd224 | 1141 | if (ctx->flags & BIT(FLAGS_ERROR)) |
bf362759 | 1142 | return 0; /* uncompleted hash is not needed */ |
8628e7c8 | 1143 | |
bf362759 DK |
1144 | /* OMAP HW accel works only with buffers >= 9 */ |
1145 | /* HMAC is always >= 9 because ipad == block size */ | |
1146 | if ((ctx->digcnt + ctx->bufcnt) < 9) | |
1147 | return omap_sham_final_shash(req); | |
1148 | else if (ctx->bufcnt) | |
1149 | return omap_sham_enqueue(req, OP_FINAL); | |
8628e7c8 | 1150 | |
bf362759 DK |
1151 | /* copy ready hash (+ finalize hmac) */ |
1152 | return omap_sham_finish(req); | |
8628e7c8 DK |
1153 | } |
1154 | ||
1155 | static int omap_sham_finup(struct ahash_request *req) | |
1156 | { | |
1157 | struct omap_sham_reqctx *ctx = ahash_request_ctx(req); | |
1158 | int err1, err2; | |
1159 | ||
ea1fd224 | 1160 | ctx->flags |= BIT(FLAGS_FINUP); |
8628e7c8 DK |
1161 | |
1162 | err1 = omap_sham_update(req); | |
455e3389 | 1163 | if (err1 == -EINPROGRESS || err1 == -EBUSY) |
8628e7c8 DK |
1164 | return err1; |
1165 | /* | |
1166 | * final() has to be always called to cleanup resources | |
1167 | * even if udpate() failed, except EINPROGRESS | |
1168 | */ | |
1169 | err2 = omap_sham_final(req); | |
1170 | ||
1171 | return err1 ?: err2; | |
1172 | } | |
1173 | ||
1174 | static int omap_sham_digest(struct ahash_request *req) | |
1175 | { | |
1176 | return omap_sham_init(req) ?: omap_sham_finup(req); | |
1177 | } | |
1178 | ||
1179 | static int omap_sham_setkey(struct crypto_ahash *tfm, const u8 *key, | |
1180 | unsigned int keylen) | |
1181 | { | |
1182 | struct omap_sham_ctx *tctx = crypto_ahash_ctx(tfm); | |
1183 | struct omap_sham_hmac_ctx *bctx = tctx->base; | |
1184 | int bs = crypto_shash_blocksize(bctx->shash); | |
1185 | int ds = crypto_shash_digestsize(bctx->shash); | |
0d373d60 | 1186 | struct omap_sham_dev *dd = NULL, *tmp; |
8628e7c8 | 1187 | int err, i; |
0d373d60 MG |
1188 | |
1189 | spin_lock_bh(&sham.lock); | |
1190 | if (!tctx->dd) { | |
1191 | list_for_each_entry(tmp, &sham.dev_list, list) { | |
1192 | dd = tmp; | |
1193 | break; | |
1194 | } | |
1195 | tctx->dd = dd; | |
1196 | } else { | |
1197 | dd = tctx->dd; | |
1198 | } | |
1199 | spin_unlock_bh(&sham.lock); | |
1200 | ||
8628e7c8 DK |
1201 | err = crypto_shash_setkey(tctx->fallback, key, keylen); |
1202 | if (err) | |
1203 | return err; | |
1204 | ||
1205 | if (keylen > bs) { | |
1206 | err = omap_sham_shash_digest(bctx->shash, | |
1207 | crypto_shash_get_flags(bctx->shash), | |
1208 | key, keylen, bctx->ipad); | |
1209 | if (err) | |
1210 | return err; | |
1211 | keylen = ds; | |
1212 | } else { | |
1213 | memcpy(bctx->ipad, key, keylen); | |
1214 | } | |
1215 | ||
1216 | memset(bctx->ipad + keylen, 0, bs - keylen); | |
8628e7c8 | 1217 | |
0d373d60 MG |
1218 | if (!test_bit(FLAGS_AUTO_XOR, &dd->flags)) { |
1219 | memcpy(bctx->opad, bctx->ipad, bs); | |
1220 | ||
1221 | for (i = 0; i < bs; i++) { | |
1222 | bctx->ipad[i] ^= 0x36; | |
1223 | bctx->opad[i] ^= 0x5c; | |
1224 | } | |
8628e7c8 DK |
1225 | } |
1226 | ||
1227 | return err; | |
1228 | } | |
1229 | ||
1230 | static int omap_sham_cra_init_alg(struct crypto_tfm *tfm, const char *alg_base) | |
1231 | { | |
1232 | struct omap_sham_ctx *tctx = crypto_tfm_ctx(tfm); | |
1233 | const char *alg_name = crypto_tfm_alg_name(tfm); | |
1234 | ||
1235 | /* Allocate a fallback and abort if it failed. */ | |
1236 | tctx->fallback = crypto_alloc_shash(alg_name, 0, | |
1237 | CRYPTO_ALG_NEED_FALLBACK); | |
1238 | if (IS_ERR(tctx->fallback)) { | |
1239 | pr_err("omap-sham: fallback driver '%s' " | |
1240 | "could not be loaded.\n", alg_name); | |
1241 | return PTR_ERR(tctx->fallback); | |
1242 | } | |
1243 | ||
1244 | crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm), | |
798eed5d | 1245 | sizeof(struct omap_sham_reqctx) + BUFLEN); |
8628e7c8 DK |
1246 | |
1247 | if (alg_base) { | |
1248 | struct omap_sham_hmac_ctx *bctx = tctx->base; | |
ea1fd224 | 1249 | tctx->flags |= BIT(FLAGS_HMAC); |
8628e7c8 DK |
1250 | bctx->shash = crypto_alloc_shash(alg_base, 0, |
1251 | CRYPTO_ALG_NEED_FALLBACK); | |
1252 | if (IS_ERR(bctx->shash)) { | |
1253 | pr_err("omap-sham: base driver '%s' " | |
1254 | "could not be loaded.\n", alg_base); | |
1255 | crypto_free_shash(tctx->fallback); | |
1256 | return PTR_ERR(bctx->shash); | |
1257 | } | |
1258 | ||
1259 | } | |
1260 | ||
1261 | return 0; | |
1262 | } | |
1263 | ||
1264 | static int omap_sham_cra_init(struct crypto_tfm *tfm) | |
1265 | { | |
1266 | return omap_sham_cra_init_alg(tfm, NULL); | |
1267 | } | |
1268 | ||
1269 | static int omap_sham_cra_sha1_init(struct crypto_tfm *tfm) | |
1270 | { | |
1271 | return omap_sham_cra_init_alg(tfm, "sha1"); | |
1272 | } | |
1273 | ||
d20fb18b MG |
1274 | static int omap_sham_cra_sha224_init(struct crypto_tfm *tfm) |
1275 | { | |
1276 | return omap_sham_cra_init_alg(tfm, "sha224"); | |
1277 | } | |
1278 | ||
1279 | static int omap_sham_cra_sha256_init(struct crypto_tfm *tfm) | |
1280 | { | |
1281 | return omap_sham_cra_init_alg(tfm, "sha256"); | |
1282 | } | |
1283 | ||
8628e7c8 DK |
1284 | static int omap_sham_cra_md5_init(struct crypto_tfm *tfm) |
1285 | { | |
1286 | return omap_sham_cra_init_alg(tfm, "md5"); | |
1287 | } | |
1288 | ||
eaef7e3f LV |
1289 | static int omap_sham_cra_sha384_init(struct crypto_tfm *tfm) |
1290 | { | |
1291 | return omap_sham_cra_init_alg(tfm, "sha384"); | |
1292 | } | |
1293 | ||
1294 | static int omap_sham_cra_sha512_init(struct crypto_tfm *tfm) | |
1295 | { | |
1296 | return omap_sham_cra_init_alg(tfm, "sha512"); | |
1297 | } | |
1298 | ||
8628e7c8 DK |
1299 | static void omap_sham_cra_exit(struct crypto_tfm *tfm) |
1300 | { | |
1301 | struct omap_sham_ctx *tctx = crypto_tfm_ctx(tfm); | |
1302 | ||
1303 | crypto_free_shash(tctx->fallback); | |
1304 | tctx->fallback = NULL; | |
1305 | ||
ea1fd224 | 1306 | if (tctx->flags & BIT(FLAGS_HMAC)) { |
8628e7c8 DK |
1307 | struct omap_sham_hmac_ctx *bctx = tctx->base; |
1308 | crypto_free_shash(bctx->shash); | |
1309 | } | |
1310 | } | |
1311 | ||
d20fb18b | 1312 | static struct ahash_alg algs_sha1_md5[] = { |
8628e7c8 DK |
1313 | { |
1314 | .init = omap_sham_init, | |
1315 | .update = omap_sham_update, | |
1316 | .final = omap_sham_final, | |
1317 | .finup = omap_sham_finup, | |
1318 | .digest = omap_sham_digest, | |
1319 | .halg.digestsize = SHA1_DIGEST_SIZE, | |
1320 | .halg.base = { | |
1321 | .cra_name = "sha1", | |
1322 | .cra_driver_name = "omap-sha1", | |
1323 | .cra_priority = 100, | |
1324 | .cra_flags = CRYPTO_ALG_TYPE_AHASH | | |
d912bb76 | 1325 | CRYPTO_ALG_KERN_DRIVER_ONLY | |
8628e7c8 DK |
1326 | CRYPTO_ALG_ASYNC | |
1327 | CRYPTO_ALG_NEED_FALLBACK, | |
1328 | .cra_blocksize = SHA1_BLOCK_SIZE, | |
1329 | .cra_ctxsize = sizeof(struct omap_sham_ctx), | |
1330 | .cra_alignmask = 0, | |
1331 | .cra_module = THIS_MODULE, | |
1332 | .cra_init = omap_sham_cra_init, | |
1333 | .cra_exit = omap_sham_cra_exit, | |
1334 | } | |
1335 | }, | |
1336 | { | |
1337 | .init = omap_sham_init, | |
1338 | .update = omap_sham_update, | |
1339 | .final = omap_sham_final, | |
1340 | .finup = omap_sham_finup, | |
1341 | .digest = omap_sham_digest, | |
1342 | .halg.digestsize = MD5_DIGEST_SIZE, | |
1343 | .halg.base = { | |
1344 | .cra_name = "md5", | |
1345 | .cra_driver_name = "omap-md5", | |
1346 | .cra_priority = 100, | |
1347 | .cra_flags = CRYPTO_ALG_TYPE_AHASH | | |
d912bb76 | 1348 | CRYPTO_ALG_KERN_DRIVER_ONLY | |
8628e7c8 DK |
1349 | CRYPTO_ALG_ASYNC | |
1350 | CRYPTO_ALG_NEED_FALLBACK, | |
1351 | .cra_blocksize = SHA1_BLOCK_SIZE, | |
1352 | .cra_ctxsize = sizeof(struct omap_sham_ctx), | |
798eed5d | 1353 | .cra_alignmask = OMAP_ALIGN_MASK, |
8628e7c8 DK |
1354 | .cra_module = THIS_MODULE, |
1355 | .cra_init = omap_sham_cra_init, | |
1356 | .cra_exit = omap_sham_cra_exit, | |
1357 | } | |
1358 | }, | |
1359 | { | |
1360 | .init = omap_sham_init, | |
1361 | .update = omap_sham_update, | |
1362 | .final = omap_sham_final, | |
1363 | .finup = omap_sham_finup, | |
1364 | .digest = omap_sham_digest, | |
1365 | .setkey = omap_sham_setkey, | |
1366 | .halg.digestsize = SHA1_DIGEST_SIZE, | |
1367 | .halg.base = { | |
1368 | .cra_name = "hmac(sha1)", | |
1369 | .cra_driver_name = "omap-hmac-sha1", | |
1370 | .cra_priority = 100, | |
1371 | .cra_flags = CRYPTO_ALG_TYPE_AHASH | | |
d912bb76 | 1372 | CRYPTO_ALG_KERN_DRIVER_ONLY | |
8628e7c8 DK |
1373 | CRYPTO_ALG_ASYNC | |
1374 | CRYPTO_ALG_NEED_FALLBACK, | |
1375 | .cra_blocksize = SHA1_BLOCK_SIZE, | |
1376 | .cra_ctxsize = sizeof(struct omap_sham_ctx) + | |
1377 | sizeof(struct omap_sham_hmac_ctx), | |
798eed5d | 1378 | .cra_alignmask = OMAP_ALIGN_MASK, |
8628e7c8 DK |
1379 | .cra_module = THIS_MODULE, |
1380 | .cra_init = omap_sham_cra_sha1_init, | |
1381 | .cra_exit = omap_sham_cra_exit, | |
1382 | } | |
1383 | }, | |
1384 | { | |
1385 | .init = omap_sham_init, | |
1386 | .update = omap_sham_update, | |
1387 | .final = omap_sham_final, | |
1388 | .finup = omap_sham_finup, | |
1389 | .digest = omap_sham_digest, | |
1390 | .setkey = omap_sham_setkey, | |
1391 | .halg.digestsize = MD5_DIGEST_SIZE, | |
1392 | .halg.base = { | |
1393 | .cra_name = "hmac(md5)", | |
1394 | .cra_driver_name = "omap-hmac-md5", | |
1395 | .cra_priority = 100, | |
1396 | .cra_flags = CRYPTO_ALG_TYPE_AHASH | | |
d912bb76 | 1397 | CRYPTO_ALG_KERN_DRIVER_ONLY | |
8628e7c8 DK |
1398 | CRYPTO_ALG_ASYNC | |
1399 | CRYPTO_ALG_NEED_FALLBACK, | |
1400 | .cra_blocksize = SHA1_BLOCK_SIZE, | |
1401 | .cra_ctxsize = sizeof(struct omap_sham_ctx) + | |
1402 | sizeof(struct omap_sham_hmac_ctx), | |
798eed5d | 1403 | .cra_alignmask = OMAP_ALIGN_MASK, |
8628e7c8 DK |
1404 | .cra_module = THIS_MODULE, |
1405 | .cra_init = omap_sham_cra_md5_init, | |
1406 | .cra_exit = omap_sham_cra_exit, | |
1407 | } | |
1408 | } | |
1409 | }; | |
1410 | ||
d20fb18b MG |
1411 | /* OMAP4 has some algs in addition to what OMAP2 has */ |
1412 | static struct ahash_alg algs_sha224_sha256[] = { | |
1413 | { | |
1414 | .init = omap_sham_init, | |
1415 | .update = omap_sham_update, | |
1416 | .final = omap_sham_final, | |
1417 | .finup = omap_sham_finup, | |
1418 | .digest = omap_sham_digest, | |
1419 | .halg.digestsize = SHA224_DIGEST_SIZE, | |
1420 | .halg.base = { | |
1421 | .cra_name = "sha224", | |
1422 | .cra_driver_name = "omap-sha224", | |
1423 | .cra_priority = 100, | |
1424 | .cra_flags = CRYPTO_ALG_TYPE_AHASH | | |
1425 | CRYPTO_ALG_ASYNC | | |
1426 | CRYPTO_ALG_NEED_FALLBACK, | |
1427 | .cra_blocksize = SHA224_BLOCK_SIZE, | |
1428 | .cra_ctxsize = sizeof(struct omap_sham_ctx), | |
1429 | .cra_alignmask = 0, | |
1430 | .cra_module = THIS_MODULE, | |
1431 | .cra_init = omap_sham_cra_init, | |
1432 | .cra_exit = omap_sham_cra_exit, | |
1433 | } | |
1434 | }, | |
1435 | { | |
1436 | .init = omap_sham_init, | |
1437 | .update = omap_sham_update, | |
1438 | .final = omap_sham_final, | |
1439 | .finup = omap_sham_finup, | |
1440 | .digest = omap_sham_digest, | |
1441 | .halg.digestsize = SHA256_DIGEST_SIZE, | |
1442 | .halg.base = { | |
1443 | .cra_name = "sha256", | |
1444 | .cra_driver_name = "omap-sha256", | |
1445 | .cra_priority = 100, | |
1446 | .cra_flags = CRYPTO_ALG_TYPE_AHASH | | |
1447 | CRYPTO_ALG_ASYNC | | |
1448 | CRYPTO_ALG_NEED_FALLBACK, | |
1449 | .cra_blocksize = SHA256_BLOCK_SIZE, | |
1450 | .cra_ctxsize = sizeof(struct omap_sham_ctx), | |
1451 | .cra_alignmask = 0, | |
1452 | .cra_module = THIS_MODULE, | |
1453 | .cra_init = omap_sham_cra_init, | |
1454 | .cra_exit = omap_sham_cra_exit, | |
1455 | } | |
1456 | }, | |
1457 | { | |
1458 | .init = omap_sham_init, | |
1459 | .update = omap_sham_update, | |
1460 | .final = omap_sham_final, | |
1461 | .finup = omap_sham_finup, | |
1462 | .digest = omap_sham_digest, | |
1463 | .setkey = omap_sham_setkey, | |
1464 | .halg.digestsize = SHA224_DIGEST_SIZE, | |
1465 | .halg.base = { | |
1466 | .cra_name = "hmac(sha224)", | |
1467 | .cra_driver_name = "omap-hmac-sha224", | |
1468 | .cra_priority = 100, | |
1469 | .cra_flags = CRYPTO_ALG_TYPE_AHASH | | |
1470 | CRYPTO_ALG_ASYNC | | |
1471 | CRYPTO_ALG_NEED_FALLBACK, | |
1472 | .cra_blocksize = SHA224_BLOCK_SIZE, | |
1473 | .cra_ctxsize = sizeof(struct omap_sham_ctx) + | |
1474 | sizeof(struct omap_sham_hmac_ctx), | |
1475 | .cra_alignmask = OMAP_ALIGN_MASK, | |
1476 | .cra_module = THIS_MODULE, | |
1477 | .cra_init = omap_sham_cra_sha224_init, | |
1478 | .cra_exit = omap_sham_cra_exit, | |
1479 | } | |
1480 | }, | |
1481 | { | |
1482 | .init = omap_sham_init, | |
1483 | .update = omap_sham_update, | |
1484 | .final = omap_sham_final, | |
1485 | .finup = omap_sham_finup, | |
1486 | .digest = omap_sham_digest, | |
1487 | .setkey = omap_sham_setkey, | |
1488 | .halg.digestsize = SHA256_DIGEST_SIZE, | |
1489 | .halg.base = { | |
1490 | .cra_name = "hmac(sha256)", | |
1491 | .cra_driver_name = "omap-hmac-sha256", | |
1492 | .cra_priority = 100, | |
1493 | .cra_flags = CRYPTO_ALG_TYPE_AHASH | | |
1494 | CRYPTO_ALG_ASYNC | | |
1495 | CRYPTO_ALG_NEED_FALLBACK, | |
1496 | .cra_blocksize = SHA256_BLOCK_SIZE, | |
1497 | .cra_ctxsize = sizeof(struct omap_sham_ctx) + | |
1498 | sizeof(struct omap_sham_hmac_ctx), | |
1499 | .cra_alignmask = OMAP_ALIGN_MASK, | |
1500 | .cra_module = THIS_MODULE, | |
1501 | .cra_init = omap_sham_cra_sha256_init, | |
1502 | .cra_exit = omap_sham_cra_exit, | |
1503 | } | |
1504 | }, | |
1505 | }; | |
1506 | ||
eaef7e3f LV |
1507 | static struct ahash_alg algs_sha384_sha512[] = { |
1508 | { | |
1509 | .init = omap_sham_init, | |
1510 | .update = omap_sham_update, | |
1511 | .final = omap_sham_final, | |
1512 | .finup = omap_sham_finup, | |
1513 | .digest = omap_sham_digest, | |
1514 | .halg.digestsize = SHA384_DIGEST_SIZE, | |
1515 | .halg.base = { | |
1516 | .cra_name = "sha384", | |
1517 | .cra_driver_name = "omap-sha384", | |
1518 | .cra_priority = 100, | |
1519 | .cra_flags = CRYPTO_ALG_TYPE_AHASH | | |
1520 | CRYPTO_ALG_ASYNC | | |
1521 | CRYPTO_ALG_NEED_FALLBACK, | |
1522 | .cra_blocksize = SHA384_BLOCK_SIZE, | |
1523 | .cra_ctxsize = sizeof(struct omap_sham_ctx), | |
1524 | .cra_alignmask = 0, | |
1525 | .cra_module = THIS_MODULE, | |
1526 | .cra_init = omap_sham_cra_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 | .halg.digestsize = SHA512_DIGEST_SIZE, | |
1537 | .halg.base = { | |
1538 | .cra_name = "sha512", | |
1539 | .cra_driver_name = "omap-sha512", | |
1540 | .cra_priority = 100, | |
1541 | .cra_flags = CRYPTO_ALG_TYPE_AHASH | | |
1542 | CRYPTO_ALG_ASYNC | | |
1543 | CRYPTO_ALG_NEED_FALLBACK, | |
1544 | .cra_blocksize = SHA512_BLOCK_SIZE, | |
1545 | .cra_ctxsize = sizeof(struct omap_sham_ctx), | |
1546 | .cra_alignmask = 0, | |
1547 | .cra_module = THIS_MODULE, | |
1548 | .cra_init = omap_sham_cra_init, | |
1549 | .cra_exit = omap_sham_cra_exit, | |
1550 | } | |
1551 | }, | |
1552 | { | |
1553 | .init = omap_sham_init, | |
1554 | .update = omap_sham_update, | |
1555 | .final = omap_sham_final, | |
1556 | .finup = omap_sham_finup, | |
1557 | .digest = omap_sham_digest, | |
1558 | .setkey = omap_sham_setkey, | |
1559 | .halg.digestsize = SHA384_DIGEST_SIZE, | |
1560 | .halg.base = { | |
1561 | .cra_name = "hmac(sha384)", | |
1562 | .cra_driver_name = "omap-hmac-sha384", | |
1563 | .cra_priority = 100, | |
1564 | .cra_flags = CRYPTO_ALG_TYPE_AHASH | | |
1565 | CRYPTO_ALG_ASYNC | | |
1566 | CRYPTO_ALG_NEED_FALLBACK, | |
1567 | .cra_blocksize = SHA384_BLOCK_SIZE, | |
1568 | .cra_ctxsize = sizeof(struct omap_sham_ctx) + | |
1569 | sizeof(struct omap_sham_hmac_ctx), | |
1570 | .cra_alignmask = OMAP_ALIGN_MASK, | |
1571 | .cra_module = THIS_MODULE, | |
1572 | .cra_init = omap_sham_cra_sha384_init, | |
1573 | .cra_exit = omap_sham_cra_exit, | |
1574 | } | |
1575 | }, | |
1576 | { | |
1577 | .init = omap_sham_init, | |
1578 | .update = omap_sham_update, | |
1579 | .final = omap_sham_final, | |
1580 | .finup = omap_sham_finup, | |
1581 | .digest = omap_sham_digest, | |
1582 | .setkey = omap_sham_setkey, | |
1583 | .halg.digestsize = SHA512_DIGEST_SIZE, | |
1584 | .halg.base = { | |
1585 | .cra_name = "hmac(sha512)", | |
1586 | .cra_driver_name = "omap-hmac-sha512", | |
1587 | .cra_priority = 100, | |
1588 | .cra_flags = CRYPTO_ALG_TYPE_AHASH | | |
1589 | CRYPTO_ALG_ASYNC | | |
1590 | CRYPTO_ALG_NEED_FALLBACK, | |
1591 | .cra_blocksize = SHA512_BLOCK_SIZE, | |
1592 | .cra_ctxsize = sizeof(struct omap_sham_ctx) + | |
1593 | sizeof(struct omap_sham_hmac_ctx), | |
1594 | .cra_alignmask = OMAP_ALIGN_MASK, | |
1595 | .cra_module = THIS_MODULE, | |
1596 | .cra_init = omap_sham_cra_sha512_init, | |
1597 | .cra_exit = omap_sham_cra_exit, | |
1598 | } | |
1599 | }, | |
1600 | }; | |
1601 | ||
8628e7c8 DK |
1602 | static void omap_sham_done_task(unsigned long data) |
1603 | { | |
1604 | struct omap_sham_dev *dd = (struct omap_sham_dev *)data; | |
6c63db82 | 1605 | int err = 0; |
8628e7c8 | 1606 | |
6cb3ffe1 DK |
1607 | if (!test_bit(FLAGS_BUSY, &dd->flags)) { |
1608 | omap_sham_handle_queue(dd, NULL); | |
1609 | return; | |
1610 | } | |
1611 | ||
6c63db82 | 1612 | if (test_bit(FLAGS_CPU, &dd->flags)) { |
b8411ccd LV |
1613 | if (test_and_clear_bit(FLAGS_OUTPUT_READY, &dd->flags)) { |
1614 | /* hash or semi-hash ready */ | |
1615 | err = omap_sham_update_cpu(dd); | |
1616 | if (err != -EINPROGRESS) | |
1617 | goto finish; | |
1618 | } | |
6c63db82 DK |
1619 | } else if (test_bit(FLAGS_DMA_READY, &dd->flags)) { |
1620 | if (test_and_clear_bit(FLAGS_DMA_ACTIVE, &dd->flags)) { | |
1621 | omap_sham_update_dma_stop(dd); | |
1622 | if (dd->err) { | |
1623 | err = dd->err; | |
1624 | goto finish; | |
1625 | } | |
1626 | } | |
1627 | if (test_and_clear_bit(FLAGS_OUTPUT_READY, &dd->flags)) { | |
1628 | /* hash or semi-hash ready */ | |
1629 | clear_bit(FLAGS_DMA_READY, &dd->flags); | |
887c883e | 1630 | err = omap_sham_update_dma_start(dd); |
6c63db82 DK |
1631 | if (err != -EINPROGRESS) |
1632 | goto finish; | |
1633 | } | |
8628e7c8 DK |
1634 | } |
1635 | ||
6c63db82 | 1636 | return; |
3e133c8b | 1637 | |
6c63db82 DK |
1638 | finish: |
1639 | dev_dbg(dd->dev, "update done: err: %d\n", err); | |
1640 | /* finish curent request */ | |
1641 | omap_sham_finish_req(dd->req, err); | |
8628e7c8 DK |
1642 | } |
1643 | ||
0d373d60 MG |
1644 | static irqreturn_t omap_sham_irq_common(struct omap_sham_dev *dd) |
1645 | { | |
1646 | if (!test_bit(FLAGS_BUSY, &dd->flags)) { | |
1647 | dev_warn(dd->dev, "Interrupt when no active requests.\n"); | |
1648 | } else { | |
1649 | set_bit(FLAGS_OUTPUT_READY, &dd->flags); | |
1650 | tasklet_schedule(&dd->done_task); | |
1651 | } | |
1652 | ||
1653 | return IRQ_HANDLED; | |
1654 | } | |
1655 | ||
1656 | static irqreturn_t omap_sham_irq_omap2(int irq, void *dev_id) | |
8628e7c8 DK |
1657 | { |
1658 | struct omap_sham_dev *dd = dev_id; | |
8628e7c8 | 1659 | |
ed3ea9a8 | 1660 | if (unlikely(test_bit(FLAGS_FINAL, &dd->flags))) |
8628e7c8 DK |
1661 | /* final -> allow device to go to power-saving mode */ |
1662 | omap_sham_write_mask(dd, SHA_REG_CTRL, 0, SHA_REG_CTRL_LENGTH); | |
1663 | ||
1664 | omap_sham_write_mask(dd, SHA_REG_CTRL, SHA_REG_CTRL_OUTPUT_READY, | |
1665 | SHA_REG_CTRL_OUTPUT_READY); | |
1666 | omap_sham_read(dd, SHA_REG_CTRL); | |
1667 | ||
0d373d60 MG |
1668 | return omap_sham_irq_common(dd); |
1669 | } | |
cd3f1d54 | 1670 | |
0d373d60 MG |
1671 | static irqreturn_t omap_sham_irq_omap4(int irq, void *dev_id) |
1672 | { | |
1673 | struct omap_sham_dev *dd = dev_id; | |
8628e7c8 | 1674 | |
0d373d60 MG |
1675 | omap_sham_write_mask(dd, SHA_REG_MASK(dd), 0, SHA_REG_MASK_IT_EN); |
1676 | ||
1677 | return omap_sham_irq_common(dd); | |
8628e7c8 DK |
1678 | } |
1679 | ||
d20fb18b MG |
1680 | static struct omap_sham_algs_info omap_sham_algs_info_omap2[] = { |
1681 | { | |
1682 | .algs_list = algs_sha1_md5, | |
1683 | .size = ARRAY_SIZE(algs_sha1_md5), | |
1684 | }, | |
1685 | }; | |
1686 | ||
0d373d60 | 1687 | static const struct omap_sham_pdata omap_sham_pdata_omap2 = { |
d20fb18b MG |
1688 | .algs_info = omap_sham_algs_info_omap2, |
1689 | .algs_info_size = ARRAY_SIZE(omap_sham_algs_info_omap2), | |
0d373d60 MG |
1690 | .flags = BIT(FLAGS_BE32_SHA1), |
1691 | .digest_size = SHA1_DIGEST_SIZE, | |
1692 | .copy_hash = omap_sham_copy_hash_omap2, | |
1693 | .write_ctrl = omap_sham_write_ctrl_omap2, | |
1694 | .trigger = omap_sham_trigger_omap2, | |
1695 | .poll_irq = omap_sham_poll_irq_omap2, | |
1696 | .intr_hdlr = omap_sham_irq_omap2, | |
1697 | .idigest_ofs = 0x00, | |
1698 | .din_ofs = 0x1c, | |
1699 | .digcnt_ofs = 0x14, | |
1700 | .rev_ofs = 0x5c, | |
1701 | .mask_ofs = 0x60, | |
1702 | .sysstatus_ofs = 0x64, | |
1703 | .major_mask = 0xf0, | |
1704 | .major_shift = 4, | |
1705 | .minor_mask = 0x0f, | |
1706 | .minor_shift = 0, | |
1707 | }; | |
1708 | ||
03feec9c | 1709 | #ifdef CONFIG_OF |
d20fb18b MG |
1710 | static struct omap_sham_algs_info omap_sham_algs_info_omap4[] = { |
1711 | { | |
1712 | .algs_list = algs_sha1_md5, | |
1713 | .size = ARRAY_SIZE(algs_sha1_md5), | |
1714 | }, | |
1715 | { | |
1716 | .algs_list = algs_sha224_sha256, | |
1717 | .size = ARRAY_SIZE(algs_sha224_sha256), | |
1718 | }, | |
1719 | }; | |
1720 | ||
0d373d60 | 1721 | static const struct omap_sham_pdata omap_sham_pdata_omap4 = { |
d20fb18b MG |
1722 | .algs_info = omap_sham_algs_info_omap4, |
1723 | .algs_info_size = ARRAY_SIZE(omap_sham_algs_info_omap4), | |
0d373d60 MG |
1724 | .flags = BIT(FLAGS_AUTO_XOR), |
1725 | .digest_size = SHA256_DIGEST_SIZE, | |
1726 | .copy_hash = omap_sham_copy_hash_omap4, | |
1727 | .write_ctrl = omap_sham_write_ctrl_omap4, | |
1728 | .trigger = omap_sham_trigger_omap4, | |
1729 | .poll_irq = omap_sham_poll_irq_omap4, | |
1730 | .intr_hdlr = omap_sham_irq_omap4, | |
1731 | .idigest_ofs = 0x020, | |
eaef7e3f | 1732 | .odigest_ofs = 0x0, |
0d373d60 MG |
1733 | .din_ofs = 0x080, |
1734 | .digcnt_ofs = 0x040, | |
1735 | .rev_ofs = 0x100, | |
1736 | .mask_ofs = 0x110, | |
1737 | .sysstatus_ofs = 0x114, | |
eaef7e3f LV |
1738 | .mode_ofs = 0x44, |
1739 | .length_ofs = 0x48, | |
0d373d60 MG |
1740 | .major_mask = 0x0700, |
1741 | .major_shift = 8, | |
1742 | .minor_mask = 0x003f, | |
1743 | .minor_shift = 0, | |
1744 | }; | |
1745 | ||
7d7c704d LV |
1746 | static struct omap_sham_algs_info omap_sham_algs_info_omap5[] = { |
1747 | { | |
1748 | .algs_list = algs_sha1_md5, | |
1749 | .size = ARRAY_SIZE(algs_sha1_md5), | |
1750 | }, | |
1751 | { | |
1752 | .algs_list = algs_sha224_sha256, | |
1753 | .size = ARRAY_SIZE(algs_sha224_sha256), | |
1754 | }, | |
1755 | { | |
1756 | .algs_list = algs_sha384_sha512, | |
1757 | .size = ARRAY_SIZE(algs_sha384_sha512), | |
1758 | }, | |
1759 | }; | |
1760 | ||
1761 | static const struct omap_sham_pdata omap_sham_pdata_omap5 = { | |
1762 | .algs_info = omap_sham_algs_info_omap5, | |
1763 | .algs_info_size = ARRAY_SIZE(omap_sham_algs_info_omap5), | |
1764 | .flags = BIT(FLAGS_AUTO_XOR), | |
1765 | .digest_size = SHA512_DIGEST_SIZE, | |
1766 | .copy_hash = omap_sham_copy_hash_omap4, | |
1767 | .write_ctrl = omap_sham_write_ctrl_omap4, | |
1768 | .trigger = omap_sham_trigger_omap4, | |
1769 | .poll_irq = omap_sham_poll_irq_omap4, | |
1770 | .intr_hdlr = omap_sham_irq_omap4, | |
1771 | .idigest_ofs = 0x240, | |
1772 | .odigest_ofs = 0x200, | |
1773 | .din_ofs = 0x080, | |
1774 | .digcnt_ofs = 0x280, | |
1775 | .rev_ofs = 0x100, | |
1776 | .mask_ofs = 0x110, | |
1777 | .sysstatus_ofs = 0x114, | |
1778 | .mode_ofs = 0x284, | |
1779 | .length_ofs = 0x288, | |
1780 | .major_mask = 0x0700, | |
1781 | .major_shift = 8, | |
1782 | .minor_mask = 0x003f, | |
1783 | .minor_shift = 0, | |
1784 | }; | |
1785 | ||
03feec9c MG |
1786 | static const struct of_device_id omap_sham_of_match[] = { |
1787 | { | |
1788 | .compatible = "ti,omap2-sham", | |
0d373d60 MG |
1789 | .data = &omap_sham_pdata_omap2, |
1790 | }, | |
1791 | { | |
1792 | .compatible = "ti,omap4-sham", | |
1793 | .data = &omap_sham_pdata_omap4, | |
03feec9c | 1794 | }, |
7d7c704d LV |
1795 | { |
1796 | .compatible = "ti,omap5-sham", | |
1797 | .data = &omap_sham_pdata_omap5, | |
1798 | }, | |
03feec9c MG |
1799 | {}, |
1800 | }; | |
1801 | MODULE_DEVICE_TABLE(of, omap_sham_of_match); | |
1802 | ||
1803 | static int omap_sham_get_res_of(struct omap_sham_dev *dd, | |
1804 | struct device *dev, struct resource *res) | |
8628e7c8 | 1805 | { |
03feec9c MG |
1806 | struct device_node *node = dev->of_node; |
1807 | const struct of_device_id *match; | |
1808 | int err = 0; | |
8628e7c8 | 1809 | |
03feec9c MG |
1810 | match = of_match_device(of_match_ptr(omap_sham_of_match), dev); |
1811 | if (!match) { | |
1812 | dev_err(dev, "no compatible OF match\n"); | |
1813 | err = -EINVAL; | |
1814 | goto err; | |
3e133c8b DK |
1815 | } |
1816 | ||
03feec9c MG |
1817 | err = of_address_to_resource(node, 0, res); |
1818 | if (err < 0) { | |
1819 | dev_err(dev, "can't translate OF node address\n"); | |
1820 | err = -EINVAL; | |
1821 | goto err; | |
1822 | } | |
1823 | ||
1824 | dd->irq = of_irq_to_resource(node, 0, NULL); | |
1825 | if (!dd->irq) { | |
1826 | dev_err(dev, "can't translate OF irq value\n"); | |
1827 | err = -EINVAL; | |
1828 | goto err; | |
1829 | } | |
1830 | ||
1831 | dd->dma = -1; /* Dummy value that's unused */ | |
0d373d60 | 1832 | dd->pdata = match->data; |
03feec9c MG |
1833 | |
1834 | err: | |
1835 | return err; | |
8628e7c8 | 1836 | } |
03feec9c | 1837 | #else |
c3c3b329 MG |
1838 | static const struct of_device_id omap_sham_of_match[] = { |
1839 | {}, | |
1840 | }; | |
8628e7c8 | 1841 | |
c3c3b329 | 1842 | static int omap_sham_get_res_of(struct omap_sham_dev *dd, |
03feec9c | 1843 | struct device *dev, struct resource *res) |
8628e7c8 | 1844 | { |
03feec9c MG |
1845 | return -EINVAL; |
1846 | } | |
1847 | #endif | |
8628e7c8 | 1848 | |
03feec9c MG |
1849 | static int omap_sham_get_res_pdev(struct omap_sham_dev *dd, |
1850 | struct platform_device *pdev, struct resource *res) | |
1851 | { | |
1852 | struct device *dev = &pdev->dev; | |
1853 | struct resource *r; | |
1854 | int err = 0; | |
8628e7c8 | 1855 | |
03feec9c MG |
1856 | /* Get the base address */ |
1857 | r = platform_get_resource(pdev, IORESOURCE_MEM, 0); | |
1858 | if (!r) { | |
1859 | dev_err(dev, "no MEM resource info\n"); | |
1860 | err = -ENODEV; | |
1861 | goto err; | |
8628e7c8 | 1862 | } |
03feec9c | 1863 | memcpy(res, r, sizeof(*res)); |
584db6a1 | 1864 | |
03feec9c MG |
1865 | /* Get the IRQ */ |
1866 | dd->irq = platform_get_irq(pdev, 0); | |
1867 | if (dd->irq < 0) { | |
1868 | dev_err(dev, "no IRQ resource info\n"); | |
1869 | err = dd->irq; | |
1870 | goto err; | |
1871 | } | |
8628e7c8 | 1872 | |
03feec9c MG |
1873 | /* Get the DMA */ |
1874 | r = platform_get_resource(pdev, IORESOURCE_DMA, 0); | |
1875 | if (!r) { | |
1876 | dev_err(dev, "no DMA resource info\n"); | |
1877 | err = -ENODEV; | |
1878 | goto err; | |
8628e7c8 | 1879 | } |
03feec9c MG |
1880 | dd->dma = r->start; |
1881 | ||
0d373d60 MG |
1882 | /* Only OMAP2/3 can be non-DT */ |
1883 | dd->pdata = &omap_sham_pdata_omap2; | |
1884 | ||
03feec9c MG |
1885 | err: |
1886 | return err; | |
8628e7c8 DK |
1887 | } |
1888 | ||
49cfe4db | 1889 | static int omap_sham_probe(struct platform_device *pdev) |
8628e7c8 DK |
1890 | { |
1891 | struct omap_sham_dev *dd; | |
1892 | struct device *dev = &pdev->dev; | |
03feec9c | 1893 | struct resource res; |
dfd061d5 | 1894 | dma_cap_mask_t mask; |
8628e7c8 | 1895 | int err, i, j; |
0d373d60 | 1896 | u32 rev; |
8628e7c8 | 1897 | |
7a7e4b73 | 1898 | dd = devm_kzalloc(dev, sizeof(struct omap_sham_dev), GFP_KERNEL); |
8628e7c8 DK |
1899 | if (dd == NULL) { |
1900 | dev_err(dev, "unable to alloc data struct.\n"); | |
1901 | err = -ENOMEM; | |
1902 | goto data_err; | |
1903 | } | |
1904 | dd->dev = dev; | |
1905 | platform_set_drvdata(pdev, dd); | |
1906 | ||
1907 | INIT_LIST_HEAD(&dd->list); | |
1908 | spin_lock_init(&dd->lock); | |
1909 | tasklet_init(&dd->done_task, omap_sham_done_task, (unsigned long)dd); | |
8628e7c8 DK |
1910 | crypto_init_queue(&dd->queue, OMAP_SHAM_QUEUE_LENGTH); |
1911 | ||
03feec9c MG |
1912 | err = (dev->of_node) ? omap_sham_get_res_of(dd, dev, &res) : |
1913 | omap_sham_get_res_pdev(dd, pdev, &res); | |
1914 | if (err) | |
7a7e4b73 | 1915 | goto data_err; |
8628e7c8 | 1916 | |
30862281 LN |
1917 | dd->io_base = devm_ioremap_resource(dev, &res); |
1918 | if (IS_ERR(dd->io_base)) { | |
1919 | err = PTR_ERR(dd->io_base); | |
7a7e4b73 | 1920 | goto data_err; |
8628e7c8 | 1921 | } |
03feec9c | 1922 | dd->phys_base = res.start; |
8628e7c8 | 1923 | |
0de9c387 LV |
1924 | err = devm_request_irq(dev, dd->irq, dd->pdata->intr_hdlr, |
1925 | IRQF_TRIGGER_NONE, dev_name(dev), dd); | |
8628e7c8 | 1926 | if (err) { |
0de9c387 LV |
1927 | dev_err(dev, "unable to request irq %d, err = %d\n", |
1928 | dd->irq, err); | |
7a7e4b73 | 1929 | goto data_err; |
8628e7c8 DK |
1930 | } |
1931 | ||
dfd061d5 MG |
1932 | dma_cap_zero(mask); |
1933 | dma_cap_set(DMA_SLAVE, mask); | |
8628e7c8 | 1934 | |
0e87e73f MG |
1935 | dd->dma_lch = dma_request_slave_channel_compat(mask, omap_dma_filter_fn, |
1936 | &dd->dma, dev, "rx"); | |
dfd061d5 | 1937 | if (!dd->dma_lch) { |
b8411ccd LV |
1938 | dd->polling_mode = 1; |
1939 | dev_dbg(dev, "using polling mode instead of dma\n"); | |
8628e7c8 DK |
1940 | } |
1941 | ||
0d373d60 | 1942 | dd->flags |= dd->pdata->flags; |
8628e7c8 | 1943 | |
b359f034 MG |
1944 | pm_runtime_enable(dev); |
1945 | pm_runtime_get_sync(dev); | |
0d373d60 MG |
1946 | rev = omap_sham_read(dd, SHA_REG_REV(dd)); |
1947 | pm_runtime_put_sync(&pdev->dev); | |
8628e7c8 | 1948 | |
8628e7c8 | 1949 | dev_info(dev, "hw accel on OMAP rev %u.%u\n", |
0d373d60 MG |
1950 | (rev & dd->pdata->major_mask) >> dd->pdata->major_shift, |
1951 | (rev & dd->pdata->minor_mask) >> dd->pdata->minor_shift); | |
8628e7c8 DK |
1952 | |
1953 | spin_lock(&sham.lock); | |
1954 | list_add_tail(&dd->list, &sham.dev_list); | |
1955 | spin_unlock(&sham.lock); | |
1956 | ||
d20fb18b MG |
1957 | for (i = 0; i < dd->pdata->algs_info_size; i++) { |
1958 | for (j = 0; j < dd->pdata->algs_info[i].size; j++) { | |
1959 | err = crypto_register_ahash( | |
1960 | &dd->pdata->algs_info[i].algs_list[j]); | |
1961 | if (err) | |
1962 | goto err_algs; | |
1963 | ||
1964 | dd->pdata->algs_info[i].registered++; | |
1965 | } | |
8628e7c8 DK |
1966 | } |
1967 | ||
1968 | return 0; | |
1969 | ||
1970 | err_algs: | |
d20fb18b MG |
1971 | for (i = dd->pdata->algs_info_size - 1; i >= 0; i--) |
1972 | for (j = dd->pdata->algs_info[i].registered - 1; j >= 0; j--) | |
1973 | crypto_unregister_ahash( | |
1974 | &dd->pdata->algs_info[i].algs_list[j]); | |
b359f034 | 1975 | pm_runtime_disable(dev); |
dfd061d5 | 1976 | dma_release_channel(dd->dma_lch); |
8628e7c8 DK |
1977 | data_err: |
1978 | dev_err(dev, "initialization failed.\n"); | |
1979 | ||
1980 | return err; | |
1981 | } | |
1982 | ||
49cfe4db | 1983 | static int omap_sham_remove(struct platform_device *pdev) |
8628e7c8 DK |
1984 | { |
1985 | static struct omap_sham_dev *dd; | |
d20fb18b | 1986 | int i, j; |
8628e7c8 DK |
1987 | |
1988 | dd = platform_get_drvdata(pdev); | |
1989 | if (!dd) | |
1990 | return -ENODEV; | |
1991 | spin_lock(&sham.lock); | |
1992 | list_del(&dd->list); | |
1993 | spin_unlock(&sham.lock); | |
d20fb18b MG |
1994 | for (i = dd->pdata->algs_info_size - 1; i >= 0; i--) |
1995 | for (j = dd->pdata->algs_info[i].registered - 1; j >= 0; j--) | |
1996 | crypto_unregister_ahash( | |
1997 | &dd->pdata->algs_info[i].algs_list[j]); | |
8628e7c8 | 1998 | tasklet_kill(&dd->done_task); |
b359f034 | 1999 | pm_runtime_disable(&pdev->dev); |
dfd061d5 | 2000 | dma_release_channel(dd->dma_lch); |
8628e7c8 DK |
2001 | |
2002 | return 0; | |
2003 | } | |
2004 | ||
3b3f4400 MG |
2005 | #ifdef CONFIG_PM_SLEEP |
2006 | static int omap_sham_suspend(struct device *dev) | |
2007 | { | |
2008 | pm_runtime_put_sync(dev); | |
2009 | return 0; | |
2010 | } | |
2011 | ||
2012 | static int omap_sham_resume(struct device *dev) | |
2013 | { | |
2014 | pm_runtime_get_sync(dev); | |
2015 | return 0; | |
2016 | } | |
2017 | #endif | |
2018 | ||
2019 | static const struct dev_pm_ops omap_sham_pm_ops = { | |
2020 | SET_SYSTEM_SLEEP_PM_OPS(omap_sham_suspend, omap_sham_resume) | |
2021 | }; | |
2022 | ||
8628e7c8 DK |
2023 | static struct platform_driver omap_sham_driver = { |
2024 | .probe = omap_sham_probe, | |
2025 | .remove = omap_sham_remove, | |
2026 | .driver = { | |
2027 | .name = "omap-sham", | |
2028 | .owner = THIS_MODULE, | |
3b3f4400 | 2029 | .pm = &omap_sham_pm_ops, |
03feec9c | 2030 | .of_match_table = omap_sham_of_match, |
8628e7c8 DK |
2031 | }, |
2032 | }; | |
2033 | ||
02613702 | 2034 | module_platform_driver(omap_sham_driver); |
8628e7c8 DK |
2035 | |
2036 | MODULE_DESCRIPTION("OMAP SHA1/MD5 hw acceleration support."); | |
2037 | MODULE_LICENSE("GPL v2"); | |
2038 | MODULE_AUTHOR("Dmitry Kasatkin"); |