forked from torvalds/linux
-
Notifications
You must be signed in to change notification settings - Fork 449
/
Copy pathstm32-hash.c
2547 lines (2166 loc) · 62 KB
/
stm32-hash.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-License-Identifier: GPL-2.0-only
/*
* This file is part of STM32 Crypto driver for Linux.
*
* Copyright (C) 2017, STMicroelectronics - All Rights Reserved
* Author(s): Lionel DEBIEVE <[email protected]> for STMicroelectronics.
*/
#include <crypto/engine.h>
#include <crypto/internal/hash.h>
#include <crypto/md5.h>
#include <crypto/scatterwalk.h>
#include <crypto/sha1.h>
#include <crypto/sha2.h>
#include <crypto/sha3.h>
#include <linux/clk.h>
#include <linux/delay.h>
#include <linux/dma-mapping.h>
#include <linux/dmaengine.h>
#include <linux/interrupt.h>
#include <linux/iopoll.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/platform_device.h>
#include <linux/pm_runtime.h>
#include <linux/reset.h>
#include <linux/string.h>
#define HASH_CR 0x00
#define HASH_DIN 0x04
#define HASH_STR 0x08
#define HASH_UX500_HREG(x) (0x0c + ((x) * 0x04))
#define HASH_IMR 0x20
#define HASH_SR 0x24
#define HASH_CSR(x) (0x0F8 + ((x) * 0x04))
#define HASH_HREG(x) (0x310 + ((x) * 0x04))
#define HASH_HWCFGR 0x3F0
#define HASH_VER 0x3F4
#define HASH_ID 0x3F8
/* Control Register */
#define HASH_CR_INIT BIT(2)
#define HASH_CR_DMAE BIT(3)
#define HASH_CR_DATATYPE_POS 4
#define HASH_CR_MODE BIT(6)
#define HASH_CR_ALGO_POS 7
#define HASH_CR_MDMAT BIT(13)
#define HASH_CR_DMAA BIT(14)
#define HASH_CR_LKEY BIT(16)
/* Interrupt */
#define HASH_DINIE BIT(0)
#define HASH_DCIE BIT(1)
/* Interrupt Mask */
#define HASH_MASK_CALC_COMPLETION BIT(0)
#define HASH_MASK_DATA_INPUT BIT(1)
/* Status Flags */
#define HASH_SR_DATA_INPUT_READY BIT(0)
#define HASH_SR_OUTPUT_READY BIT(1)
#define HASH_SR_DMA_ACTIVE BIT(2)
#define HASH_SR_BUSY BIT(3)
/* STR Register */
#define HASH_STR_NBLW_MASK GENMASK(4, 0)
#define HASH_STR_DCAL BIT(8)
/* HWCFGR Register */
#define HASH_HWCFG_DMA_MASK GENMASK(3, 0)
/* Context swap register */
#define HASH_CSR_NB_SHA256_HMAC 54
#define HASH_CSR_NB_SHA256 38
#define HASH_CSR_NB_SHA512_HMAC 103
#define HASH_CSR_NB_SHA512 91
#define HASH_CSR_NB_SHA3_HMAC 88
#define HASH_CSR_NB_SHA3 72
#define HASH_CSR_NB_MAX HASH_CSR_NB_SHA512_HMAC
#define HASH_FLAGS_INIT BIT(0)
#define HASH_FLAGS_OUTPUT_READY BIT(1)
#define HASH_FLAGS_CPU BIT(2)
#define HASH_FLAGS_DMA_ACTIVE BIT(3)
#define HASH_FLAGS_HMAC_INIT BIT(4)
#define HASH_FLAGS_HMAC_FINAL BIT(5)
#define HASH_FLAGS_HMAC_KEY BIT(6)
#define HASH_FLAGS_SHA3_MODE BIT(7)
#define HASH_FLAGS_FINAL BIT(15)
#define HASH_FLAGS_FINUP BIT(16)
#define HASH_FLAGS_ALGO_MASK GENMASK(20, 17)
#define HASH_FLAGS_ALGO_SHIFT 17
#define HASH_FLAGS_ERRORS BIT(21)
#define HASH_FLAGS_EMPTY BIT(22)
#define HASH_FLAGS_HMAC BIT(23)
#define HASH_FLAGS_SGS_COPIED BIT(24)
#define HASH_OP_UPDATE 1
#define HASH_OP_FINAL 2
#define HASH_BURST_LEVEL 4
enum stm32_hash_data_format {
HASH_DATA_32_BITS = 0x0,
HASH_DATA_16_BITS = 0x1,
HASH_DATA_8_BITS = 0x2,
HASH_DATA_1_BIT = 0x3
};
#define HASH_BUFLEN (SHA3_224_BLOCK_SIZE + 4)
#define HASH_MAX_KEY_SIZE (SHA512_BLOCK_SIZE * 8)
enum stm32_hash_algo {
HASH_SHA1 = 0,
HASH_MD5 = 1,
HASH_SHA224 = 2,
HASH_SHA256 = 3,
HASH_SHA3_224 = 4,
HASH_SHA3_256 = 5,
HASH_SHA3_384 = 6,
HASH_SHA3_512 = 7,
HASH_SHA384 = 12,
HASH_SHA512 = 15,
};
enum ux500_hash_algo {
HASH_SHA256_UX500 = 0,
HASH_SHA1_UX500 = 1,
};
#define HASH_AUTOSUSPEND_DELAY 50
struct stm32_hash_ctx {
struct stm32_hash_dev *hdev;
struct crypto_shash *xtfm;
unsigned long flags;
u8 key[HASH_MAX_KEY_SIZE];
int keylen;
};
struct stm32_hash_state {
u32 flags;
u16 bufcnt;
u16 blocklen;
u8 buffer[HASH_BUFLEN] __aligned(sizeof(u32));
/* hash state */
u32 hw_context[3 + HASH_CSR_NB_MAX];
};
struct stm32_hash_request_ctx {
struct stm32_hash_dev *hdev;
unsigned long op;
u8 digest[SHA512_DIGEST_SIZE] __aligned(sizeof(u32));
size_t digcnt;
struct scatterlist *sg;
struct scatterlist sgl[2]; /* scatterlist used to realize alignment */
unsigned int offset;
unsigned int total;
struct scatterlist sg_key;
dma_addr_t dma_addr;
size_t dma_ct;
int nents;
u8 data_type;
struct stm32_hash_state state;
};
struct stm32_hash_algs_info {
struct ahash_engine_alg *algs_list;
size_t size;
};
struct stm32_hash_pdata {
const int alg_shift;
const struct stm32_hash_algs_info *algs_info;
size_t algs_info_size;
bool has_sr;
bool has_mdmat;
bool context_secured;
bool broken_emptymsg;
bool ux500;
};
struct stm32_hash_dev {
struct list_head list;
struct device *dev;
struct clk *clk;
struct reset_control *rst;
void __iomem *io_base;
phys_addr_t phys_base;
u8 xmit_buf[HASH_BUFLEN] __aligned(sizeof(u32));
u32 dma_mode;
bool polled;
struct ahash_request *req;
struct crypto_engine *engine;
unsigned long flags;
struct dma_chan *dma_lch;
struct completion dma_completion;
const struct stm32_hash_pdata *pdata;
};
struct stm32_hash_drv {
struct list_head dev_list;
spinlock_t lock; /* List protection access */
};
static struct stm32_hash_drv stm32_hash = {
.dev_list = LIST_HEAD_INIT(stm32_hash.dev_list),
.lock = __SPIN_LOCK_UNLOCKED(stm32_hash.lock),
};
static void stm32_hash_dma_callback(void *param);
static int stm32_hash_prepare_request(struct ahash_request *req);
static void stm32_hash_unprepare_request(struct ahash_request *req);
static inline u32 stm32_hash_read(struct stm32_hash_dev *hdev, u32 offset)
{
return readl_relaxed(hdev->io_base + offset);
}
static inline void stm32_hash_write(struct stm32_hash_dev *hdev,
u32 offset, u32 value)
{
writel_relaxed(value, hdev->io_base + offset);
}
/**
* stm32_hash_wait_busy - wait until hash processor is available. It return an
* error if the hash core is processing a block of data for more than 10 ms.
* @hdev: the stm32_hash_dev device.
*/
static inline int stm32_hash_wait_busy(struct stm32_hash_dev *hdev)
{
u32 status;
/* The Ux500 lacks the special status register, we poll the DCAL bit instead */
if (!hdev->pdata->has_sr)
return readl_relaxed_poll_timeout(hdev->io_base + HASH_STR, status,
!(status & HASH_STR_DCAL), 10, 10000);
return readl_relaxed_poll_timeout(hdev->io_base + HASH_SR, status,
!(status & HASH_SR_BUSY), 10, 10000);
}
/**
* stm32_hash_set_nblw - set the number of valid bytes in the last word.
* @hdev: the stm32_hash_dev device.
* @length: the length of the final word.
*/
static void stm32_hash_set_nblw(struct stm32_hash_dev *hdev, int length)
{
u32 reg;
reg = stm32_hash_read(hdev, HASH_STR);
reg &= ~(HASH_STR_NBLW_MASK);
reg |= (8U * ((length) % 4U));
stm32_hash_write(hdev, HASH_STR, reg);
}
static int stm32_hash_write_key(struct stm32_hash_dev *hdev)
{
struct crypto_ahash *tfm = crypto_ahash_reqtfm(hdev->req);
struct stm32_hash_ctx *ctx = crypto_ahash_ctx(tfm);
u32 reg;
int keylen = ctx->keylen;
void *key = ctx->key;
if (keylen) {
stm32_hash_set_nblw(hdev, keylen);
while (keylen > 0) {
stm32_hash_write(hdev, HASH_DIN, *(u32 *)key);
keylen -= 4;
key += 4;
}
reg = stm32_hash_read(hdev, HASH_STR);
reg |= HASH_STR_DCAL;
stm32_hash_write(hdev, HASH_STR, reg);
return -EINPROGRESS;
}
return 0;
}
/**
* stm32_hash_write_ctrl - Initialize the hash processor, only if
* HASH_FLAGS_INIT is set.
* @hdev: the stm32_hash_dev device
*/
static void stm32_hash_write_ctrl(struct stm32_hash_dev *hdev)
{
struct stm32_hash_request_ctx *rctx = ahash_request_ctx(hdev->req);
struct crypto_ahash *tfm = crypto_ahash_reqtfm(hdev->req);
struct stm32_hash_ctx *ctx = crypto_ahash_ctx(tfm);
struct stm32_hash_state *state = &rctx->state;
u32 alg = (state->flags & HASH_FLAGS_ALGO_MASK) >> HASH_FLAGS_ALGO_SHIFT;
u32 reg = HASH_CR_INIT;
if (!(hdev->flags & HASH_FLAGS_INIT)) {
if (hdev->pdata->ux500) {
reg |= ((alg & BIT(0)) << HASH_CR_ALGO_POS);
} else {
if (hdev->pdata->alg_shift == HASH_CR_ALGO_POS)
reg |= ((alg & BIT(1)) << 17) |
((alg & BIT(0)) << HASH_CR_ALGO_POS);
else
reg |= alg << hdev->pdata->alg_shift;
}
reg |= (rctx->data_type << HASH_CR_DATATYPE_POS);
if (state->flags & HASH_FLAGS_HMAC) {
hdev->flags |= HASH_FLAGS_HMAC;
reg |= HASH_CR_MODE;
if (ctx->keylen > crypto_ahash_blocksize(tfm))
reg |= HASH_CR_LKEY;
}
if (!hdev->polled)
stm32_hash_write(hdev, HASH_IMR, HASH_DCIE);
stm32_hash_write(hdev, HASH_CR, reg);
hdev->flags |= HASH_FLAGS_INIT;
/*
* After first block + 1 words are fill up,
* we only need to fill 1 block to start partial computation
*/
rctx->state.blocklen -= sizeof(u32);
dev_dbg(hdev->dev, "Write Control %x\n", reg);
}
}
static void stm32_hash_append_sg(struct stm32_hash_request_ctx *rctx)
{
struct stm32_hash_state *state = &rctx->state;
size_t count;
while ((state->bufcnt < state->blocklen) && rctx->total) {
count = min(rctx->sg->length - rctx->offset, rctx->total);
count = min_t(size_t, count, state->blocklen - state->bufcnt);
if (count <= 0) {
if ((rctx->sg->length == 0) && !sg_is_last(rctx->sg)) {
rctx->sg = sg_next(rctx->sg);
continue;
} else {
break;
}
}
scatterwalk_map_and_copy(state->buffer + state->bufcnt,
rctx->sg, rctx->offset, count, 0);
state->bufcnt += count;
rctx->offset += count;
rctx->total -= count;
if (rctx->offset == rctx->sg->length) {
rctx->sg = sg_next(rctx->sg);
if (rctx->sg)
rctx->offset = 0;
else
rctx->total = 0;
}
}
}
static int stm32_hash_xmit_cpu(struct stm32_hash_dev *hdev,
const u8 *buf, size_t length, int final)
{
struct stm32_hash_request_ctx *rctx = ahash_request_ctx(hdev->req);
struct stm32_hash_state *state = &rctx->state;
unsigned int count, len32;
const u32 *buffer = (const u32 *)buf;
u32 reg;
if (final) {
hdev->flags |= HASH_FLAGS_FINAL;
/* Do not process empty messages if hw is buggy. */
if (!(hdev->flags & HASH_FLAGS_INIT) && !length &&
hdev->pdata->broken_emptymsg) {
state->flags |= HASH_FLAGS_EMPTY;
return 0;
}
}
len32 = DIV_ROUND_UP(length, sizeof(u32));
dev_dbg(hdev->dev, "%s: length: %zd, final: %x len32 %i\n",
__func__, length, final, len32);
hdev->flags |= HASH_FLAGS_CPU;
stm32_hash_write_ctrl(hdev);
if (stm32_hash_wait_busy(hdev))
return -ETIMEDOUT;
if ((hdev->flags & HASH_FLAGS_HMAC) &&
(!(hdev->flags & HASH_FLAGS_HMAC_KEY))) {
hdev->flags |= HASH_FLAGS_HMAC_KEY;
stm32_hash_write_key(hdev);
if (stm32_hash_wait_busy(hdev))
return -ETIMEDOUT;
}
for (count = 0; count < len32; count++)
stm32_hash_write(hdev, HASH_DIN, buffer[count]);
if (final) {
if (stm32_hash_wait_busy(hdev))
return -ETIMEDOUT;
stm32_hash_set_nblw(hdev, length);
reg = stm32_hash_read(hdev, HASH_STR);
reg |= HASH_STR_DCAL;
stm32_hash_write(hdev, HASH_STR, reg);
if (hdev->flags & HASH_FLAGS_HMAC) {
if (stm32_hash_wait_busy(hdev))
return -ETIMEDOUT;
stm32_hash_write_key(hdev);
}
return -EINPROGRESS;
}
return 0;
}
static int hash_swap_reg(struct stm32_hash_request_ctx *rctx)
{
struct stm32_hash_state *state = &rctx->state;
switch ((state->flags & HASH_FLAGS_ALGO_MASK) >>
HASH_FLAGS_ALGO_SHIFT) {
case HASH_MD5:
case HASH_SHA1:
case HASH_SHA224:
case HASH_SHA256:
if (state->flags & HASH_FLAGS_HMAC)
return HASH_CSR_NB_SHA256_HMAC;
else
return HASH_CSR_NB_SHA256;
break;
case HASH_SHA384:
case HASH_SHA512:
if (state->flags & HASH_FLAGS_HMAC)
return HASH_CSR_NB_SHA512_HMAC;
else
return HASH_CSR_NB_SHA512;
break;
case HASH_SHA3_224:
case HASH_SHA3_256:
case HASH_SHA3_384:
case HASH_SHA3_512:
if (state->flags & HASH_FLAGS_HMAC)
return HASH_CSR_NB_SHA3_HMAC;
else
return HASH_CSR_NB_SHA3;
break;
default:
return -EINVAL;
}
}
static int stm32_hash_update_cpu(struct stm32_hash_dev *hdev)
{
struct stm32_hash_request_ctx *rctx = ahash_request_ctx(hdev->req);
struct stm32_hash_state *state = &rctx->state;
int bufcnt, err = 0, final;
dev_dbg(hdev->dev, "%s flags %x\n", __func__, state->flags);
final = state->flags & HASH_FLAGS_FINAL;
while ((rctx->total >= state->blocklen) ||
(state->bufcnt + rctx->total >= state->blocklen)) {
stm32_hash_append_sg(rctx);
bufcnt = state->bufcnt;
state->bufcnt = 0;
err = stm32_hash_xmit_cpu(hdev, state->buffer, bufcnt, 0);
if (err)
return err;
}
stm32_hash_append_sg(rctx);
if (final) {
bufcnt = state->bufcnt;
state->bufcnt = 0;
return stm32_hash_xmit_cpu(hdev, state->buffer, bufcnt, 1);
}
return err;
}
static int stm32_hash_xmit_dma(struct stm32_hash_dev *hdev,
struct scatterlist *sg, int length, int mdmat)
{
struct dma_async_tx_descriptor *in_desc;
dma_cookie_t cookie;
u32 reg;
int err;
dev_dbg(hdev->dev, "%s mdmat: %x length: %d\n", __func__, mdmat, length);
/* do not use dma if there is no data to send */
if (length <= 0)
return 0;
in_desc = dmaengine_prep_slave_sg(hdev->dma_lch, sg, 1,
DMA_MEM_TO_DEV, DMA_PREP_INTERRUPT |
DMA_CTRL_ACK);
if (!in_desc) {
dev_err(hdev->dev, "dmaengine_prep_slave error\n");
return -ENOMEM;
}
reinit_completion(&hdev->dma_completion);
in_desc->callback = stm32_hash_dma_callback;
in_desc->callback_param = hdev;
hdev->flags |= HASH_FLAGS_DMA_ACTIVE;
reg = stm32_hash_read(hdev, HASH_CR);
if (hdev->pdata->has_mdmat) {
if (mdmat)
reg |= HASH_CR_MDMAT;
else
reg &= ~HASH_CR_MDMAT;
}
reg |= HASH_CR_DMAE;
stm32_hash_write(hdev, HASH_CR, reg);
cookie = dmaengine_submit(in_desc);
err = dma_submit_error(cookie);
if (err)
return -ENOMEM;
dma_async_issue_pending(hdev->dma_lch);
if (!wait_for_completion_timeout(&hdev->dma_completion,
msecs_to_jiffies(100)))
err = -ETIMEDOUT;
if (dma_async_is_tx_complete(hdev->dma_lch, cookie,
NULL, NULL) != DMA_COMPLETE)
err = -ETIMEDOUT;
if (err) {
dev_err(hdev->dev, "DMA Error %i\n", err);
dmaengine_terminate_all(hdev->dma_lch);
return err;
}
return -EINPROGRESS;
}
static void stm32_hash_dma_callback(void *param)
{
struct stm32_hash_dev *hdev = param;
complete(&hdev->dma_completion);
}
static int stm32_hash_hmac_dma_send(struct stm32_hash_dev *hdev)
{
struct stm32_hash_request_ctx *rctx = ahash_request_ctx(hdev->req);
struct crypto_ahash *tfm = crypto_ahash_reqtfm(hdev->req);
struct stm32_hash_ctx *ctx = crypto_ahash_ctx(tfm);
int err;
if (ctx->keylen < rctx->state.blocklen || hdev->dma_mode > 0) {
err = stm32_hash_write_key(hdev);
if (stm32_hash_wait_busy(hdev))
return -ETIMEDOUT;
} else {
if (!(hdev->flags & HASH_FLAGS_HMAC_KEY))
sg_init_one(&rctx->sg_key, ctx->key,
ALIGN(ctx->keylen, sizeof(u32)));
rctx->dma_ct = dma_map_sg(hdev->dev, &rctx->sg_key, 1,
DMA_TO_DEVICE);
if (rctx->dma_ct == 0) {
dev_err(hdev->dev, "dma_map_sg error\n");
return -ENOMEM;
}
err = stm32_hash_xmit_dma(hdev, &rctx->sg_key, ctx->keylen, 0);
dma_unmap_sg(hdev->dev, &rctx->sg_key, 1, DMA_TO_DEVICE);
}
return err;
}
static int stm32_hash_dma_init(struct stm32_hash_dev *hdev)
{
struct dma_slave_config dma_conf;
struct dma_chan *chan;
int err;
memset(&dma_conf, 0, sizeof(dma_conf));
dma_conf.direction = DMA_MEM_TO_DEV;
dma_conf.dst_addr = hdev->phys_base + HASH_DIN;
dma_conf.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
dma_conf.src_maxburst = HASH_BURST_LEVEL;
dma_conf.dst_maxburst = HASH_BURST_LEVEL;
dma_conf.device_fc = false;
chan = dma_request_chan(hdev->dev, "in");
if (IS_ERR(chan))
return PTR_ERR(chan);
hdev->dma_lch = chan;
err = dmaengine_slave_config(hdev->dma_lch, &dma_conf);
if (err) {
dma_release_channel(hdev->dma_lch);
hdev->dma_lch = NULL;
dev_err(hdev->dev, "Couldn't configure DMA slave.\n");
return err;
}
init_completion(&hdev->dma_completion);
return 0;
}
static int stm32_hash_dma_send(struct stm32_hash_dev *hdev)
{
struct stm32_hash_request_ctx *rctx = ahash_request_ctx(hdev->req);
u32 *buffer = (void *)rctx->state.buffer;
struct scatterlist sg[1], *tsg;
int err = 0, reg, ncp = 0;
unsigned int i, len = 0, bufcnt = 0;
bool final = hdev->flags & HASH_FLAGS_FINAL;
bool is_last = false;
u32 last_word;
dev_dbg(hdev->dev, "%s total: %d bufcnt: %d final: %d\n",
__func__, rctx->total, rctx->state.bufcnt, final);
if (rctx->nents < 0)
return -EINVAL;
stm32_hash_write_ctrl(hdev);
if (hdev->flags & HASH_FLAGS_HMAC && (!(hdev->flags & HASH_FLAGS_HMAC_KEY))) {
hdev->flags |= HASH_FLAGS_HMAC_KEY;
err = stm32_hash_hmac_dma_send(hdev);
if (err != -EINPROGRESS)
return err;
}
for_each_sg(rctx->sg, tsg, rctx->nents, i) {
sg[0] = *tsg;
len = sg->length;
if (sg_is_last(sg) || (bufcnt + sg[0].length) >= rctx->total) {
if (!final) {
/* Always manually put the last word of a non-final transfer. */
len -= sizeof(u32);
sg_pcopy_to_buffer(rctx->sg, rctx->nents, &last_word, 4, len);
sg->length -= sizeof(u32);
} else {
/*
* In Multiple DMA mode, DMA must be aborted before the final
* transfer.
*/
sg->length = rctx->total - bufcnt;
if (hdev->dma_mode > 0) {
len = (ALIGN(sg->length, 16) - 16);
ncp = sg_pcopy_to_buffer(rctx->sg, rctx->nents,
rctx->state.buffer,
sg->length - len,
rctx->total - sg->length + len);
if (!len)
break;
sg->length = len;
} else {
is_last = true;
if (!(IS_ALIGNED(sg->length, sizeof(u32)))) {
len = sg->length;
sg->length = ALIGN(sg->length,
sizeof(u32));
}
}
}
}
rctx->dma_ct = dma_map_sg(hdev->dev, sg, 1,
DMA_TO_DEVICE);
if (rctx->dma_ct == 0) {
dev_err(hdev->dev, "dma_map_sg error\n");
return -ENOMEM;
}
err = stm32_hash_xmit_dma(hdev, sg, len, !is_last);
/* The last word of a non final transfer is sent manually. */
if (!final) {
stm32_hash_write(hdev, HASH_DIN, last_word);
len += sizeof(u32);
}
rctx->total -= len;
bufcnt += sg[0].length;
dma_unmap_sg(hdev->dev, sg, 1, DMA_TO_DEVICE);
if (err == -ENOMEM || err == -ETIMEDOUT)
return err;
if (is_last)
break;
}
/*
* When the second last block transfer of 4 words is performed by the DMA,
* the software must set the DMA Abort bit (DMAA) to 1 before completing the
* last transfer of 4 words or less.
*/
if (final) {
if (hdev->dma_mode > 0) {
if (stm32_hash_wait_busy(hdev))
return -ETIMEDOUT;
reg = stm32_hash_read(hdev, HASH_CR);
reg &= ~HASH_CR_DMAE;
reg |= HASH_CR_DMAA;
stm32_hash_write(hdev, HASH_CR, reg);
if (ncp) {
memset(buffer + ncp, 0, 4 - DIV_ROUND_UP(ncp, sizeof(u32)));
writesl(hdev->io_base + HASH_DIN, buffer,
DIV_ROUND_UP(ncp, sizeof(u32)));
}
stm32_hash_set_nblw(hdev, ncp);
reg = stm32_hash_read(hdev, HASH_STR);
reg |= HASH_STR_DCAL;
stm32_hash_write(hdev, HASH_STR, reg);
err = -EINPROGRESS;
}
/*
* The hash processor needs the key to be loaded a second time in order
* to process the HMAC.
*/
if (hdev->flags & HASH_FLAGS_HMAC) {
if (stm32_hash_wait_busy(hdev))
return -ETIMEDOUT;
err = stm32_hash_hmac_dma_send(hdev);
}
return err;
}
if (err != -EINPROGRESS)
return err;
return 0;
}
static struct stm32_hash_dev *stm32_hash_find_dev(struct stm32_hash_ctx *ctx)
{
struct stm32_hash_dev *hdev = NULL, *tmp;
spin_lock_bh(&stm32_hash.lock);
if (!ctx->hdev) {
list_for_each_entry(tmp, &stm32_hash.dev_list, list) {
hdev = tmp;
break;
}
ctx->hdev = hdev;
} else {
hdev = ctx->hdev;
}
spin_unlock_bh(&stm32_hash.lock);
return hdev;
}
static int stm32_hash_init(struct ahash_request *req)
{
struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
struct stm32_hash_ctx *ctx = crypto_ahash_ctx(tfm);
struct stm32_hash_request_ctx *rctx = ahash_request_ctx(req);
struct stm32_hash_dev *hdev = stm32_hash_find_dev(ctx);
struct stm32_hash_state *state = &rctx->state;
bool sha3_mode = ctx->flags & HASH_FLAGS_SHA3_MODE;
rctx->hdev = hdev;
state->flags = 0;
if (!(hdev->dma_lch && hdev->pdata->has_mdmat))
state->flags |= HASH_FLAGS_CPU;
if (sha3_mode)
state->flags |= HASH_FLAGS_SHA3_MODE;
rctx->digcnt = crypto_ahash_digestsize(tfm);
switch (rctx->digcnt) {
case MD5_DIGEST_SIZE:
state->flags |= HASH_MD5 << HASH_FLAGS_ALGO_SHIFT;
break;
case SHA1_DIGEST_SIZE:
if (hdev->pdata->ux500)
state->flags |= HASH_SHA1_UX500 << HASH_FLAGS_ALGO_SHIFT;
else
state->flags |= HASH_SHA1 << HASH_FLAGS_ALGO_SHIFT;
break;
case SHA224_DIGEST_SIZE:
if (sha3_mode)
state->flags |= HASH_SHA3_224 << HASH_FLAGS_ALGO_SHIFT;
else
state->flags |= HASH_SHA224 << HASH_FLAGS_ALGO_SHIFT;
break;
case SHA256_DIGEST_SIZE:
if (sha3_mode) {
state->flags |= HASH_SHA3_256 << HASH_FLAGS_ALGO_SHIFT;
} else {
if (hdev->pdata->ux500)
state->flags |= HASH_SHA256_UX500 << HASH_FLAGS_ALGO_SHIFT;
else
state->flags |= HASH_SHA256 << HASH_FLAGS_ALGO_SHIFT;
}
break;
case SHA384_DIGEST_SIZE:
if (sha3_mode)
state->flags |= HASH_SHA3_384 << HASH_FLAGS_ALGO_SHIFT;
else
state->flags |= HASH_SHA384 << HASH_FLAGS_ALGO_SHIFT;
break;
case SHA512_DIGEST_SIZE:
if (sha3_mode)
state->flags |= HASH_SHA3_512 << HASH_FLAGS_ALGO_SHIFT;
else
state->flags |= HASH_SHA512 << HASH_FLAGS_ALGO_SHIFT;
break;
default:
return -EINVAL;
}
rctx->state.bufcnt = 0;
rctx->state.blocklen = crypto_ahash_blocksize(tfm) + sizeof(u32);
if (rctx->state.blocklen > HASH_BUFLEN) {
dev_err(hdev->dev, "Error, block too large");
return -EINVAL;
}
rctx->nents = 0;
rctx->total = 0;
rctx->offset = 0;
rctx->data_type = HASH_DATA_8_BITS;
if (ctx->flags & HASH_FLAGS_HMAC)
state->flags |= HASH_FLAGS_HMAC;
dev_dbg(hdev->dev, "%s Flags %x\n", __func__, state->flags);
return 0;
}
static int stm32_hash_update_req(struct stm32_hash_dev *hdev)
{
struct stm32_hash_request_ctx *rctx = ahash_request_ctx(hdev->req);
struct stm32_hash_state *state = &rctx->state;
dev_dbg(hdev->dev, "update_req: total: %u, digcnt: %zd, final: 0",
rctx->total, rctx->digcnt);
if (!(state->flags & HASH_FLAGS_CPU))
return stm32_hash_dma_send(hdev);
return stm32_hash_update_cpu(hdev);
}
static int stm32_hash_final_req(struct stm32_hash_dev *hdev)
{
struct ahash_request *req = hdev->req;
struct stm32_hash_request_ctx *rctx = ahash_request_ctx(req);
struct stm32_hash_state *state = &rctx->state;
int buflen = state->bufcnt;
if (!(state->flags & HASH_FLAGS_CPU)) {
hdev->flags |= HASH_FLAGS_FINAL;
return stm32_hash_dma_send(hdev);
}
if (state->flags & HASH_FLAGS_FINUP)
return stm32_hash_update_req(hdev);
state->bufcnt = 0;
return stm32_hash_xmit_cpu(hdev, state->buffer, buflen, 1);
}
static void stm32_hash_emptymsg_fallback(struct ahash_request *req)
{
struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
struct stm32_hash_ctx *ctx = crypto_ahash_ctx(ahash);
struct stm32_hash_request_ctx *rctx = ahash_request_ctx(req);
struct stm32_hash_dev *hdev = rctx->hdev;
int ret;
dev_dbg(hdev->dev, "use fallback message size 0 key size %d\n",
ctx->keylen);
if (!ctx->xtfm) {
dev_err(hdev->dev, "no fallback engine\n");
return;
}
if (ctx->keylen) {
ret = crypto_shash_setkey(ctx->xtfm, ctx->key, ctx->keylen);
if (ret) {
dev_err(hdev->dev, "failed to set key ret=%d\n", ret);
return;
}
}
ret = crypto_shash_tfm_digest(ctx->xtfm, NULL, 0, rctx->digest);
if (ret)
dev_err(hdev->dev, "shash digest error\n");
}
static void stm32_hash_copy_hash(struct ahash_request *req)
{
struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
struct stm32_hash_request_ctx *rctx = ahash_request_ctx(req);
struct stm32_hash_state *state = &rctx->state;
struct stm32_hash_dev *hdev = rctx->hdev;
__be32 *hash = (void *)rctx->digest;
unsigned int i, hashsize;
if (hdev->pdata->broken_emptymsg && (state->flags & HASH_FLAGS_EMPTY))
return stm32_hash_emptymsg_fallback(req);
hashsize = crypto_ahash_digestsize(tfm);
for (i = 0; i < hashsize / sizeof(u32); i++) {
if (hdev->pdata->ux500)
hash[i] = cpu_to_be32(stm32_hash_read(hdev,
HASH_UX500_HREG(i)));
else
hash[i] = cpu_to_be32(stm32_hash_read(hdev,
HASH_HREG(i)));
}
}
static int stm32_hash_finish(struct ahash_request *req)
{
struct stm32_hash_request_ctx *rctx = ahash_request_ctx(req);
u32 reg;
reg = stm32_hash_read(rctx->hdev, HASH_SR);
reg &= ~HASH_SR_OUTPUT_READY;
stm32_hash_write(rctx->hdev, HASH_SR, reg);
if (!req->result)
return -EINVAL;
memcpy(req->result, rctx->digest, rctx->digcnt);
return 0;
}
static void stm32_hash_finish_req(struct ahash_request *req, int err)
{
struct stm32_hash_request_ctx *rctx = ahash_request_ctx(req);
struct stm32_hash_state *state = &rctx->state;