hc
2024-05-10 37f49e37ab4cb5d0bc4c60eb5c6d4dd57db767bb
kernel/drivers/crypto/caam/desc_constr.h
....@@ -3,6 +3,7 @@
33 * caam descriptor construction helper functions
44 *
55 * Copyright 2008-2012 Freescale Semiconductor, Inc.
6
+ * Copyright 2019 NXP
67 */
78
89 #ifndef DESC_CONSTR_H
....@@ -13,9 +14,41 @@
1314
1415 #define IMMEDIATE (1 << 23)
1516 #define CAAM_CMD_SZ sizeof(u32)
16
-#define CAAM_PTR_SZ sizeof(dma_addr_t)
17
+#define CAAM_PTR_SZ caam_ptr_sz
18
+#define CAAM_PTR_SZ_MAX sizeof(dma_addr_t)
19
+#define CAAM_PTR_SZ_MIN sizeof(u32)
1720 #define CAAM_DESC_BYTES_MAX (CAAM_CMD_SZ * MAX_CAAM_DESCSIZE)
18
-#define DESC_JOB_IO_LEN (CAAM_CMD_SZ * 5 + CAAM_PTR_SZ * 3)
21
+#define __DESC_JOB_IO_LEN(n) (CAAM_CMD_SZ * 5 + (n) * 3)
22
+#define DESC_JOB_IO_LEN __DESC_JOB_IO_LEN(CAAM_PTR_SZ)
23
+#define DESC_JOB_IO_LEN_MAX __DESC_JOB_IO_LEN(CAAM_PTR_SZ_MAX)
24
+#define DESC_JOB_IO_LEN_MIN __DESC_JOB_IO_LEN(CAAM_PTR_SZ_MIN)
25
+
26
+/*
27
+ * The CAAM QI hardware constructs a job descriptor which points
28
+ * to shared descriptor (as pointed by context_a of FQ to CAAM).
29
+ * When the job descriptor is executed by deco, the whole job
30
+ * descriptor together with shared descriptor gets loaded in
31
+ * deco buffer which is 64 words long (each 32-bit).
32
+ *
33
+ * The job descriptor constructed by QI hardware has layout:
34
+ *
35
+ * HEADER (1 word)
36
+ * Shdesc ptr (1 or 2 words)
37
+ * SEQ_OUT_PTR (1 word)
38
+ * Out ptr (1 or 2 words)
39
+ * Out length (1 word)
40
+ * SEQ_IN_PTR (1 word)
41
+ * In ptr (1 or 2 words)
42
+ * In length (1 word)
43
+ *
44
+ * The shdesc ptr is used to fetch shared descriptor contents
45
+ * into deco buffer.
46
+ *
47
+ * Apart from shdesc contents, the total number of words that
48
+ * get loaded in deco buffer are '8' or '11'. The remaining words
49
+ * in deco buffer can be used for storing shared descriptor.
50
+ */
51
+#define MAX_SDLEN ((CAAM_DESC_BYTES_MAX - DESC_JOB_IO_LEN_MIN) / CAAM_CMD_SZ)
1952
2053 #ifdef DEBUG
2154 #define PRINT_POS do { printk(KERN_DEBUG "%02d: %s\n", desc_len(desc),\
....@@ -36,6 +69,17 @@
3669 (LDOFF_ENABLE_AUTO_NFIFO << LDST_OFFSET_SHIFT))
3770
3871 extern bool caam_little_end;
72
+extern size_t caam_ptr_sz;
73
+
74
+/*
75
+ * HW fetches 4 S/G table entries at a time, irrespective of how many entries
76
+ * are in the table. It's SW's responsibility to make sure these accesses
77
+ * do not have side effects.
78
+ */
79
+static inline int pad_sg_nents(int sg_nents)
80
+{
81
+ return ALIGN(sg_nents, 4);
82
+}
3983
4084 static inline int desc_len(u32 * const desc)
4185 {
....@@ -92,9 +136,15 @@
92136
93137 static inline void append_ptr(u32 * const desc, dma_addr_t ptr)
94138 {
95
- dma_addr_t *offset = (dma_addr_t *)desc_end(desc);
139
+ if (caam_ptr_sz == sizeof(dma_addr_t)) {
140
+ dma_addr_t *offset = (dma_addr_t *)desc_end(desc);
96141
97
- *offset = cpu_to_caam_dma(ptr);
142
+ *offset = cpu_to_caam_dma(ptr);
143
+ } else {
144
+ u32 *offset = (u32 *)desc_end(desc);
145
+
146
+ *offset = cpu_to_caam_dma(ptr);
147
+ }
98148
99149 (*desc) = cpu_to_caam32(caam32_to_cpu(*desc) +
100150 CAAM_PTR_SZ / CAAM_CMD_SZ);
....@@ -189,6 +239,7 @@
189239 }
190240 APPEND_CMD_RET(jump, JUMP)
191241 APPEND_CMD_RET(move, MOVE)
242
+APPEND_CMD_RET(move_len, MOVE_LEN)
192243
193244 static inline void set_jump_tgt_here(u32 * const desc, u32 *jump_cmd)
194245 {
....@@ -327,7 +378,11 @@
327378 u32 options) \
328379 { \
329380 PRINT_POS; \
330
- append_cmd(desc, CMD_##op | IMMEDIATE | options | sizeof(type)); \
381
+ if (options & LDST_LEN_MASK) \
382
+ append_cmd(desc, CMD_##op | IMMEDIATE | options); \
383
+ else \
384
+ append_cmd(desc, CMD_##op | IMMEDIATE | options | \
385
+ sizeof(type)); \
331386 append_cmd(desc, immediate); \
332387 }
333388 APPEND_CMD_RAW_IMM(load, LOAD, u32);
....@@ -441,8 +496,8 @@
441496 * functions where it is used.
442497 * @keylen: length of the provided algorithm key, in bytes
443498 * @keylen_pad: padded length of the provided algorithm key, in bytes
444
- * @key: address where algorithm key resides; virtual address if key_inline
445
- * is true, dma (bus) address if key_inline is false.
499
+ * @key_dma: dma (bus) address where algorithm key resides
500
+ * @key_virt: virtual address where algorithm key resides
446501 * @key_inline: true - key can be inlined in the descriptor; false - key is
447502 * referenced by the descriptor
448503 */
....@@ -450,10 +505,8 @@
450505 u32 algtype;
451506 unsigned int keylen;
452507 unsigned int keylen_pad;
453
- union {
454
- dma_addr_t key_dma;
455
- const void *key_virt;
456
- };
508
+ dma_addr_t key_dma;
509
+ const void *key_virt;
457510 bool key_inline;
458511 };
459512
....@@ -519,14 +572,26 @@
519572 if (adata->key_inline) {
520573 int words;
521574
522
- append_operation(desc, OP_TYPE_UNI_PROTOCOL | protid |
523
- OP_PCL_DKP_SRC_IMM | OP_PCL_DKP_DST_IMM |
524
- adata->keylen);
525
- append_data(desc, adata->key_virt, adata->keylen);
575
+ if (adata->keylen > adata->keylen_pad) {
576
+ append_operation(desc, OP_TYPE_UNI_PROTOCOL | protid |
577
+ OP_PCL_DKP_SRC_PTR |
578
+ OP_PCL_DKP_DST_IMM | adata->keylen);
579
+ append_ptr(desc, adata->key_dma);
580
+
581
+ words = (ALIGN(adata->keylen_pad, CAAM_CMD_SZ) -
582
+ CAAM_PTR_SZ) / CAAM_CMD_SZ;
583
+ } else {
584
+ append_operation(desc, OP_TYPE_UNI_PROTOCOL | protid |
585
+ OP_PCL_DKP_SRC_IMM |
586
+ OP_PCL_DKP_DST_IMM | adata->keylen);
587
+ append_data(desc, adata->key_virt, adata->keylen);
588
+
589
+ words = (ALIGN(adata->keylen_pad, CAAM_CMD_SZ) -
590
+ ALIGN(adata->keylen, CAAM_CMD_SZ)) /
591
+ CAAM_CMD_SZ;
592
+ }
526593
527594 /* Reserve space in descriptor buffer for the derived key */
528
- words = (ALIGN(adata->keylen_pad, CAAM_CMD_SZ) -
529
- ALIGN(adata->keylen, CAAM_CMD_SZ)) / CAAM_CMD_SZ;
530595 if (words)
531596 (*desc) = cpu_to_caam32(caam32_to_cpu(*desc) + words);
532597 } else {