2 * AMD Cryptographic Coprocessor (CCP) driver
4 * Copyright (C) 2013,2016 Advanced Micro Devices, Inc.
6 * Author: Tom Lendacky <thomas.lendacky@amd.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
13 #include <linux/module.h>
14 #include <linux/kernel.h>
15 #include <linux/pci.h>
16 #include <linux/interrupt.h>
17 #include <crypto/scatterwalk.h>
18 #include <linux/ccp.h>
22 /* SHA initial context values */
23 static const __be32 ccp_sha1_init
[CCP_SHA_CTXSIZE
/ sizeof(__be32
)] = {
24 cpu_to_be32(SHA1_H0
), cpu_to_be32(SHA1_H1
),
25 cpu_to_be32(SHA1_H2
), cpu_to_be32(SHA1_H3
),
26 cpu_to_be32(SHA1_H4
), 0, 0, 0,
29 static const __be32 ccp_sha224_init
[CCP_SHA_CTXSIZE
/ sizeof(__be32
)] = {
30 cpu_to_be32(SHA224_H0
), cpu_to_be32(SHA224_H1
),
31 cpu_to_be32(SHA224_H2
), cpu_to_be32(SHA224_H3
),
32 cpu_to_be32(SHA224_H4
), cpu_to_be32(SHA224_H5
),
33 cpu_to_be32(SHA224_H6
), cpu_to_be32(SHA224_H7
),
36 static const __be32 ccp_sha256_init
[CCP_SHA_CTXSIZE
/ sizeof(__be32
)] = {
37 cpu_to_be32(SHA256_H0
), cpu_to_be32(SHA256_H1
),
38 cpu_to_be32(SHA256_H2
), cpu_to_be32(SHA256_H3
),
39 cpu_to_be32(SHA256_H4
), cpu_to_be32(SHA256_H5
),
40 cpu_to_be32(SHA256_H6
), cpu_to_be32(SHA256_H7
),
43 static u32
ccp_alloc_ksb(struct ccp_device
*ccp
, unsigned int count
)
48 mutex_lock(&ccp
->ksb_mutex
);
50 start
= (u32
)bitmap_find_next_zero_area(ccp
->ksb
,
54 if (start
<= ccp
->ksb_count
) {
55 bitmap_set(ccp
->ksb
, start
, count
);
57 mutex_unlock(&ccp
->ksb_mutex
);
63 mutex_unlock(&ccp
->ksb_mutex
);
65 /* Wait for KSB entries to become available */
66 if (wait_event_interruptible(ccp
->ksb_queue
, ccp
->ksb_avail
))
70 return KSB_START
+ start
;
73 static void ccp_free_ksb(struct ccp_device
*ccp
, unsigned int start
,
79 mutex_lock(&ccp
->ksb_mutex
);
81 bitmap_clear(ccp
->ksb
, start
- KSB_START
, count
);
85 mutex_unlock(&ccp
->ksb_mutex
);
87 wake_up_interruptible_all(&ccp
->ksb_queue
);
90 static u32
ccp_gen_jobid(struct ccp_device
*ccp
)
92 return atomic_inc_return(&ccp
->current_id
) & CCP_JOBID_MASK
;
95 static void ccp_sg_free(struct ccp_sg_workarea
*wa
)
98 dma_unmap_sg(wa
->dma_dev
, wa
->dma_sg
, wa
->nents
, wa
->dma_dir
);
103 static int ccp_init_sg_workarea(struct ccp_sg_workarea
*wa
, struct device
*dev
,
104 struct scatterlist
*sg
, u64 len
,
105 enum dma_data_direction dma_dir
)
107 memset(wa
, 0, sizeof(*wa
));
113 wa
->nents
= sg_nents_for_len(sg
, len
);
117 wa
->bytes_left
= len
;
123 if (dma_dir
== DMA_NONE
)
128 wa
->dma_dir
= dma_dir
;
129 wa
->dma_count
= dma_map_sg(dev
, sg
, wa
->nents
, dma_dir
);
136 static void ccp_update_sg_workarea(struct ccp_sg_workarea
*wa
, unsigned int len
)
138 unsigned int nbytes
= min_t(u64
, len
, wa
->bytes_left
);
143 wa
->sg_used
+= nbytes
;
144 wa
->bytes_left
-= nbytes
;
145 if (wa
->sg_used
== wa
->sg
->length
) {
146 wa
->sg
= sg_next(wa
->sg
);
151 static void ccp_dm_free(struct ccp_dm_workarea
*wa
)
153 if (wa
->length
<= CCP_DMAPOOL_MAX_SIZE
) {
155 dma_pool_free(wa
->dma_pool
, wa
->address
,
159 dma_unmap_single(wa
->dev
, wa
->dma
.address
, wa
->length
,
168 static int ccp_init_dm_workarea(struct ccp_dm_workarea
*wa
,
169 struct ccp_cmd_queue
*cmd_q
,
171 enum dma_data_direction dir
)
173 memset(wa
, 0, sizeof(*wa
));
178 wa
->dev
= cmd_q
->ccp
->dev
;
181 if (len
<= CCP_DMAPOOL_MAX_SIZE
) {
182 wa
->dma_pool
= cmd_q
->dma_pool
;
184 wa
->address
= dma_pool_alloc(wa
->dma_pool
, GFP_KERNEL
,
189 wa
->dma
.length
= CCP_DMAPOOL_MAX_SIZE
;
191 memset(wa
->address
, 0, CCP_DMAPOOL_MAX_SIZE
);
193 wa
->address
= kzalloc(len
, GFP_KERNEL
);
197 wa
->dma
.address
= dma_map_single(wa
->dev
, wa
->address
, len
,
199 if (!wa
->dma
.address
)
202 wa
->dma
.length
= len
;
209 static void ccp_set_dm_area(struct ccp_dm_workarea
*wa
, unsigned int wa_offset
,
210 struct scatterlist
*sg
, unsigned int sg_offset
,
213 WARN_ON(!wa
->address
);
215 scatterwalk_map_and_copy(wa
->address
+ wa_offset
, sg
, sg_offset
, len
,
219 static void ccp_get_dm_area(struct ccp_dm_workarea
*wa
, unsigned int wa_offset
,
220 struct scatterlist
*sg
, unsigned int sg_offset
,
223 WARN_ON(!wa
->address
);
225 scatterwalk_map_and_copy(wa
->address
+ wa_offset
, sg
, sg_offset
, len
,
229 static int ccp_reverse_set_dm_area(struct ccp_dm_workarea
*wa
,
230 struct scatterlist
*sg
,
231 unsigned int len
, unsigned int se_len
,
234 unsigned int nbytes
, sg_offset
, dm_offset
, ksb_len
, i
;
235 u8 buffer
[CCP_REVERSE_BUF_SIZE
];
237 if (WARN_ON(se_len
> sizeof(buffer
)))
244 ksb_len
= min_t(unsigned int, nbytes
, se_len
);
245 sg_offset
-= ksb_len
;
247 scatterwalk_map_and_copy(buffer
, sg
, sg_offset
, ksb_len
, 0);
248 for (i
= 0; i
< ksb_len
; i
++)
249 wa
->address
[dm_offset
+ i
] = buffer
[ksb_len
- i
- 1];
251 dm_offset
+= ksb_len
;
254 if ((ksb_len
!= se_len
) && sign_extend
) {
255 /* Must sign-extend to nearest sign-extend length */
256 if (wa
->address
[dm_offset
- 1] & 0x80)
257 memset(wa
->address
+ dm_offset
, 0xff,
265 static void ccp_reverse_get_dm_area(struct ccp_dm_workarea
*wa
,
266 struct scatterlist
*sg
,
269 unsigned int nbytes
, sg_offset
, dm_offset
, ksb_len
, i
;
270 u8 buffer
[CCP_REVERSE_BUF_SIZE
];
276 ksb_len
= min_t(unsigned int, nbytes
, sizeof(buffer
));
277 dm_offset
-= ksb_len
;
279 for (i
= 0; i
< ksb_len
; i
++)
280 buffer
[ksb_len
- i
- 1] = wa
->address
[dm_offset
+ i
];
281 scatterwalk_map_and_copy(buffer
, sg
, sg_offset
, ksb_len
, 1);
283 sg_offset
+= ksb_len
;
288 static void ccp_free_data(struct ccp_data
*data
, struct ccp_cmd_queue
*cmd_q
)
290 ccp_dm_free(&data
->dm_wa
);
291 ccp_sg_free(&data
->sg_wa
);
294 static int ccp_init_data(struct ccp_data
*data
, struct ccp_cmd_queue
*cmd_q
,
295 struct scatterlist
*sg
, u64 sg_len
,
297 enum dma_data_direction dir
)
301 memset(data
, 0, sizeof(*data
));
303 ret
= ccp_init_sg_workarea(&data
->sg_wa
, cmd_q
->ccp
->dev
, sg
, sg_len
,
308 ret
= ccp_init_dm_workarea(&data
->dm_wa
, cmd_q
, dm_len
, dir
);
315 ccp_free_data(data
, cmd_q
);
320 static unsigned int ccp_queue_buf(struct ccp_data
*data
, unsigned int from
)
322 struct ccp_sg_workarea
*sg_wa
= &data
->sg_wa
;
323 struct ccp_dm_workarea
*dm_wa
= &data
->dm_wa
;
324 unsigned int buf_count
, nbytes
;
326 /* Clear the buffer if setting it */
328 memset(dm_wa
->address
, 0, dm_wa
->length
);
333 /* Perform the copy operation
334 * nbytes will always be <= UINT_MAX because dm_wa->length is
337 nbytes
= min_t(u64
, sg_wa
->bytes_left
, dm_wa
->length
);
338 scatterwalk_map_and_copy(dm_wa
->address
, sg_wa
->sg
, sg_wa
->sg_used
,
341 /* Update the structures and generate the count */
343 while (sg_wa
->bytes_left
&& (buf_count
< dm_wa
->length
)) {
344 nbytes
= min(sg_wa
->sg
->length
- sg_wa
->sg_used
,
345 dm_wa
->length
- buf_count
);
346 nbytes
= min_t(u64
, sg_wa
->bytes_left
, nbytes
);
349 ccp_update_sg_workarea(sg_wa
, nbytes
);
355 static unsigned int ccp_fill_queue_buf(struct ccp_data
*data
)
357 return ccp_queue_buf(data
, 0);
360 static unsigned int ccp_empty_queue_buf(struct ccp_data
*data
)
362 return ccp_queue_buf(data
, 1);
365 static void ccp_prepare_data(struct ccp_data
*src
, struct ccp_data
*dst
,
366 struct ccp_op
*op
, unsigned int block_size
,
369 unsigned int sg_src_len
, sg_dst_len
, op_len
;
371 /* The CCP can only DMA from/to one address each per operation. This
372 * requires that we find the smallest DMA area between the source
373 * and destination. The resulting len values will always be <= UINT_MAX
374 * because the dma length is an unsigned int.
376 sg_src_len
= sg_dma_len(src
->sg_wa
.sg
) - src
->sg_wa
.sg_used
;
377 sg_src_len
= min_t(u64
, src
->sg_wa
.bytes_left
, sg_src_len
);
380 sg_dst_len
= sg_dma_len(dst
->sg_wa
.sg
) - dst
->sg_wa
.sg_used
;
381 sg_dst_len
= min_t(u64
, src
->sg_wa
.bytes_left
, sg_dst_len
);
382 op_len
= min(sg_src_len
, sg_dst_len
);
387 /* The data operation length will be at least block_size in length
388 * or the smaller of available sg room remaining for the source or
391 op_len
= max(op_len
, block_size
);
393 /* Unless we have to buffer data, there's no reason to wait */
396 if (sg_src_len
< block_size
) {
397 /* Not enough data in the sg element, so it
398 * needs to be buffered into a blocksize chunk
400 int cp_len
= ccp_fill_queue_buf(src
);
403 op
->src
.u
.dma
.address
= src
->dm_wa
.dma
.address
;
404 op
->src
.u
.dma
.offset
= 0;
405 op
->src
.u
.dma
.length
= (blocksize_op
) ? block_size
: cp_len
;
407 /* Enough data in the sg element, but we need to
408 * adjust for any previously copied data
410 op
->src
.u
.dma
.address
= sg_dma_address(src
->sg_wa
.sg
);
411 op
->src
.u
.dma
.offset
= src
->sg_wa
.sg_used
;
412 op
->src
.u
.dma
.length
= op_len
& ~(block_size
- 1);
414 ccp_update_sg_workarea(&src
->sg_wa
, op
->src
.u
.dma
.length
);
418 if (sg_dst_len
< block_size
) {
419 /* Not enough room in the sg element or we're on the
420 * last piece of data (when using padding), so the
421 * output needs to be buffered into a blocksize chunk
424 op
->dst
.u
.dma
.address
= dst
->dm_wa
.dma
.address
;
425 op
->dst
.u
.dma
.offset
= 0;
426 op
->dst
.u
.dma
.length
= op
->src
.u
.dma
.length
;
428 /* Enough room in the sg element, but we need to
429 * adjust for any previously used area
431 op
->dst
.u
.dma
.address
= sg_dma_address(dst
->sg_wa
.sg
);
432 op
->dst
.u
.dma
.offset
= dst
->sg_wa
.sg_used
;
433 op
->dst
.u
.dma
.length
= op
->src
.u
.dma
.length
;
438 static void ccp_process_data(struct ccp_data
*src
, struct ccp_data
*dst
,
444 if (op
->dst
.u
.dma
.address
== dst
->dm_wa
.dma
.address
)
445 ccp_empty_queue_buf(dst
);
447 ccp_update_sg_workarea(&dst
->sg_wa
,
448 op
->dst
.u
.dma
.length
);
452 static int ccp_copy_to_from_ksb(struct ccp_cmd_queue
*cmd_q
,
453 struct ccp_dm_workarea
*wa
, u32 jobid
, u32 ksb
,
454 u32 byte_swap
, bool from
)
458 memset(&op
, 0, sizeof(op
));
466 op
.src
.type
= CCP_MEMTYPE_KSB
;
468 op
.dst
.type
= CCP_MEMTYPE_SYSTEM
;
469 op
.dst
.u
.dma
.address
= wa
->dma
.address
;
470 op
.dst
.u
.dma
.length
= wa
->length
;
472 op
.src
.type
= CCP_MEMTYPE_SYSTEM
;
473 op
.src
.u
.dma
.address
= wa
->dma
.address
;
474 op
.src
.u
.dma
.length
= wa
->length
;
475 op
.dst
.type
= CCP_MEMTYPE_KSB
;
479 op
.u
.passthru
.byte_swap
= byte_swap
;
481 return cmd_q
->ccp
->vdata
->perform
->perform_passthru(&op
);
484 static int ccp_copy_to_ksb(struct ccp_cmd_queue
*cmd_q
,
485 struct ccp_dm_workarea
*wa
, u32 jobid
, u32 ksb
,
488 return ccp_copy_to_from_ksb(cmd_q
, wa
, jobid
, ksb
, byte_swap
, false);
491 static int ccp_copy_from_ksb(struct ccp_cmd_queue
*cmd_q
,
492 struct ccp_dm_workarea
*wa
, u32 jobid
, u32 ksb
,
495 return ccp_copy_to_from_ksb(cmd_q
, wa
, jobid
, ksb
, byte_swap
, true);
498 static int ccp_run_aes_cmac_cmd(struct ccp_cmd_queue
*cmd_q
,
501 struct ccp_aes_engine
*aes
= &cmd
->u
.aes
;
502 struct ccp_dm_workarea key
, ctx
;
505 unsigned int dm_offset
;
508 if (!((aes
->key_len
== AES_KEYSIZE_128
) ||
509 (aes
->key_len
== AES_KEYSIZE_192
) ||
510 (aes
->key_len
== AES_KEYSIZE_256
)))
513 if (aes
->src_len
& (AES_BLOCK_SIZE
- 1))
516 if (aes
->iv_len
!= AES_BLOCK_SIZE
)
519 if (!aes
->key
|| !aes
->iv
|| !aes
->src
)
522 if (aes
->cmac_final
) {
523 if (aes
->cmac_key_len
!= AES_BLOCK_SIZE
)
530 BUILD_BUG_ON(CCP_AES_KEY_KSB_COUNT
!= 1);
531 BUILD_BUG_ON(CCP_AES_CTX_KSB_COUNT
!= 1);
534 memset(&op
, 0, sizeof(op
));
536 op
.jobid
= ccp_gen_jobid(cmd_q
->ccp
);
537 op
.ksb_key
= cmd_q
->ksb_key
;
538 op
.ksb_ctx
= cmd_q
->ksb_ctx
;
540 op
.u
.aes
.type
= aes
->type
;
541 op
.u
.aes
.mode
= aes
->mode
;
542 op
.u
.aes
.action
= aes
->action
;
544 /* All supported key sizes fit in a single (32-byte) KSB entry
545 * and must be in little endian format. Use the 256-bit byte
546 * swap passthru option to convert from big endian to little
549 ret
= ccp_init_dm_workarea(&key
, cmd_q
,
550 CCP_AES_KEY_KSB_COUNT
* CCP_KSB_BYTES
,
555 dm_offset
= CCP_KSB_BYTES
- aes
->key_len
;
556 ccp_set_dm_area(&key
, dm_offset
, aes
->key
, 0, aes
->key_len
);
557 ret
= ccp_copy_to_ksb(cmd_q
, &key
, op
.jobid
, op
.ksb_key
,
558 CCP_PASSTHRU_BYTESWAP_256BIT
);
560 cmd
->engine_error
= cmd_q
->cmd_error
;
564 /* The AES context fits in a single (32-byte) KSB entry and
565 * must be in little endian format. Use the 256-bit byte swap
566 * passthru option to convert from big endian to little endian.
568 ret
= ccp_init_dm_workarea(&ctx
, cmd_q
,
569 CCP_AES_CTX_KSB_COUNT
* CCP_KSB_BYTES
,
574 dm_offset
= CCP_KSB_BYTES
- AES_BLOCK_SIZE
;
575 ccp_set_dm_area(&ctx
, dm_offset
, aes
->iv
, 0, aes
->iv_len
);
576 ret
= ccp_copy_to_ksb(cmd_q
, &ctx
, op
.jobid
, op
.ksb_ctx
,
577 CCP_PASSTHRU_BYTESWAP_256BIT
);
579 cmd
->engine_error
= cmd_q
->cmd_error
;
583 /* Send data to the CCP AES engine */
584 ret
= ccp_init_data(&src
, cmd_q
, aes
->src
, aes
->src_len
,
585 AES_BLOCK_SIZE
, DMA_TO_DEVICE
);
589 while (src
.sg_wa
.bytes_left
) {
590 ccp_prepare_data(&src
, NULL
, &op
, AES_BLOCK_SIZE
, true);
591 if (aes
->cmac_final
&& !src
.sg_wa
.bytes_left
) {
594 /* Push the K1/K2 key to the CCP now */
595 ret
= ccp_copy_from_ksb(cmd_q
, &ctx
, op
.jobid
,
597 CCP_PASSTHRU_BYTESWAP_256BIT
);
599 cmd
->engine_error
= cmd_q
->cmd_error
;
603 ccp_set_dm_area(&ctx
, 0, aes
->cmac_key
, 0,
605 ret
= ccp_copy_to_ksb(cmd_q
, &ctx
, op
.jobid
, op
.ksb_ctx
,
606 CCP_PASSTHRU_BYTESWAP_256BIT
);
608 cmd
->engine_error
= cmd_q
->cmd_error
;
613 ret
= cmd_q
->ccp
->vdata
->perform
->perform_aes(&op
);
615 cmd
->engine_error
= cmd_q
->cmd_error
;
619 ccp_process_data(&src
, NULL
, &op
);
622 /* Retrieve the AES context - convert from LE to BE using
623 * 32-byte (256-bit) byteswapping
625 ret
= ccp_copy_from_ksb(cmd_q
, &ctx
, op
.jobid
, op
.ksb_ctx
,
626 CCP_PASSTHRU_BYTESWAP_256BIT
);
628 cmd
->engine_error
= cmd_q
->cmd_error
;
632 /* ...but we only need AES_BLOCK_SIZE bytes */
633 dm_offset
= CCP_KSB_BYTES
- AES_BLOCK_SIZE
;
634 ccp_get_dm_area(&ctx
, dm_offset
, aes
->iv
, 0, aes
->iv_len
);
637 ccp_free_data(&src
, cmd_q
);
648 static int ccp_run_aes_cmd(struct ccp_cmd_queue
*cmd_q
, struct ccp_cmd
*cmd
)
650 struct ccp_aes_engine
*aes
= &cmd
->u
.aes
;
651 struct ccp_dm_workarea key
, ctx
;
652 struct ccp_data src
, dst
;
654 unsigned int dm_offset
;
655 bool in_place
= false;
658 if (aes
->mode
== CCP_AES_MODE_CMAC
)
659 return ccp_run_aes_cmac_cmd(cmd_q
, cmd
);
661 if (!((aes
->key_len
== AES_KEYSIZE_128
) ||
662 (aes
->key_len
== AES_KEYSIZE_192
) ||
663 (aes
->key_len
== AES_KEYSIZE_256
)))
666 if (((aes
->mode
== CCP_AES_MODE_ECB
) ||
667 (aes
->mode
== CCP_AES_MODE_CBC
) ||
668 (aes
->mode
== CCP_AES_MODE_CFB
)) &&
669 (aes
->src_len
& (AES_BLOCK_SIZE
- 1)))
672 if (!aes
->key
|| !aes
->src
|| !aes
->dst
)
675 if (aes
->mode
!= CCP_AES_MODE_ECB
) {
676 if (aes
->iv_len
!= AES_BLOCK_SIZE
)
683 BUILD_BUG_ON(CCP_AES_KEY_KSB_COUNT
!= 1);
684 BUILD_BUG_ON(CCP_AES_CTX_KSB_COUNT
!= 1);
687 memset(&op
, 0, sizeof(op
));
689 op
.jobid
= ccp_gen_jobid(cmd_q
->ccp
);
690 op
.ksb_key
= cmd_q
->ksb_key
;
691 op
.ksb_ctx
= cmd_q
->ksb_ctx
;
692 op
.init
= (aes
->mode
== CCP_AES_MODE_ECB
) ? 0 : 1;
693 op
.u
.aes
.type
= aes
->type
;
694 op
.u
.aes
.mode
= aes
->mode
;
695 op
.u
.aes
.action
= aes
->action
;
697 /* All supported key sizes fit in a single (32-byte) KSB entry
698 * and must be in little endian format. Use the 256-bit byte
699 * swap passthru option to convert from big endian to little
702 ret
= ccp_init_dm_workarea(&key
, cmd_q
,
703 CCP_AES_KEY_KSB_COUNT
* CCP_KSB_BYTES
,
708 dm_offset
= CCP_KSB_BYTES
- aes
->key_len
;
709 ccp_set_dm_area(&key
, dm_offset
, aes
->key
, 0, aes
->key_len
);
710 ret
= ccp_copy_to_ksb(cmd_q
, &key
, op
.jobid
, op
.ksb_key
,
711 CCP_PASSTHRU_BYTESWAP_256BIT
);
713 cmd
->engine_error
= cmd_q
->cmd_error
;
717 /* The AES context fits in a single (32-byte) KSB entry and
718 * must be in little endian format. Use the 256-bit byte swap
719 * passthru option to convert from big endian to little endian.
721 ret
= ccp_init_dm_workarea(&ctx
, cmd_q
,
722 CCP_AES_CTX_KSB_COUNT
* CCP_KSB_BYTES
,
727 if (aes
->mode
!= CCP_AES_MODE_ECB
) {
728 /* Load the AES context - conver to LE */
729 dm_offset
= CCP_KSB_BYTES
- AES_BLOCK_SIZE
;
730 ccp_set_dm_area(&ctx
, dm_offset
, aes
->iv
, 0, aes
->iv_len
);
731 ret
= ccp_copy_to_ksb(cmd_q
, &ctx
, op
.jobid
, op
.ksb_ctx
,
732 CCP_PASSTHRU_BYTESWAP_256BIT
);
734 cmd
->engine_error
= cmd_q
->cmd_error
;
739 /* Prepare the input and output data workareas. For in-place
740 * operations we need to set the dma direction to BIDIRECTIONAL
741 * and copy the src workarea to the dst workarea.
743 if (sg_virt(aes
->src
) == sg_virt(aes
->dst
))
746 ret
= ccp_init_data(&src
, cmd_q
, aes
->src
, aes
->src_len
,
748 in_place
? DMA_BIDIRECTIONAL
: DMA_TO_DEVICE
);
755 ret
= ccp_init_data(&dst
, cmd_q
, aes
->dst
, aes
->src_len
,
756 AES_BLOCK_SIZE
, DMA_FROM_DEVICE
);
761 /* Send data to the CCP AES engine */
762 while (src
.sg_wa
.bytes_left
) {
763 ccp_prepare_data(&src
, &dst
, &op
, AES_BLOCK_SIZE
, true);
764 if (!src
.sg_wa
.bytes_left
) {
767 /* Since we don't retrieve the AES context in ECB
768 * mode we have to wait for the operation to complete
769 * on the last piece of data
771 if (aes
->mode
== CCP_AES_MODE_ECB
)
775 ret
= cmd_q
->ccp
->vdata
->perform
->perform_aes(&op
);
777 cmd
->engine_error
= cmd_q
->cmd_error
;
781 ccp_process_data(&src
, &dst
, &op
);
784 if (aes
->mode
!= CCP_AES_MODE_ECB
) {
785 /* Retrieve the AES context - convert from LE to BE using
786 * 32-byte (256-bit) byteswapping
788 ret
= ccp_copy_from_ksb(cmd_q
, &ctx
, op
.jobid
, op
.ksb_ctx
,
789 CCP_PASSTHRU_BYTESWAP_256BIT
);
791 cmd
->engine_error
= cmd_q
->cmd_error
;
795 /* ...but we only need AES_BLOCK_SIZE bytes */
796 dm_offset
= CCP_KSB_BYTES
- AES_BLOCK_SIZE
;
797 ccp_get_dm_area(&ctx
, dm_offset
, aes
->iv
, 0, aes
->iv_len
);
802 ccp_free_data(&dst
, cmd_q
);
805 ccp_free_data(&src
, cmd_q
);
816 static int ccp_run_xts_aes_cmd(struct ccp_cmd_queue
*cmd_q
,
819 struct ccp_xts_aes_engine
*xts
= &cmd
->u
.xts
;
820 struct ccp_dm_workarea key
, ctx
;
821 struct ccp_data src
, dst
;
823 unsigned int unit_size
, dm_offset
;
824 bool in_place
= false;
827 switch (xts
->unit_size
) {
828 case CCP_XTS_AES_UNIT_SIZE_16
:
831 case CCP_XTS_AES_UNIT_SIZE_512
:
834 case CCP_XTS_AES_UNIT_SIZE_1024
:
837 case CCP_XTS_AES_UNIT_SIZE_2048
:
840 case CCP_XTS_AES_UNIT_SIZE_4096
:
848 if (xts
->key_len
!= AES_KEYSIZE_128
)
851 if (!xts
->final
&& (xts
->src_len
& (AES_BLOCK_SIZE
- 1)))
854 if (xts
->iv_len
!= AES_BLOCK_SIZE
)
857 if (!xts
->key
|| !xts
->iv
|| !xts
->src
|| !xts
->dst
)
860 BUILD_BUG_ON(CCP_XTS_AES_KEY_KSB_COUNT
!= 1);
861 BUILD_BUG_ON(CCP_XTS_AES_CTX_KSB_COUNT
!= 1);
864 memset(&op
, 0, sizeof(op
));
866 op
.jobid
= ccp_gen_jobid(cmd_q
->ccp
);
867 op
.ksb_key
= cmd_q
->ksb_key
;
868 op
.ksb_ctx
= cmd_q
->ksb_ctx
;
870 op
.u
.xts
.action
= xts
->action
;
871 op
.u
.xts
.unit_size
= xts
->unit_size
;
873 /* All supported key sizes fit in a single (32-byte) KSB entry
874 * and must be in little endian format. Use the 256-bit byte
875 * swap passthru option to convert from big endian to little
878 ret
= ccp_init_dm_workarea(&key
, cmd_q
,
879 CCP_XTS_AES_KEY_KSB_COUNT
* CCP_KSB_BYTES
,
884 dm_offset
= CCP_KSB_BYTES
- AES_KEYSIZE_128
;
885 ccp_set_dm_area(&key
, dm_offset
, xts
->key
, 0, xts
->key_len
);
886 ccp_set_dm_area(&key
, 0, xts
->key
, dm_offset
, xts
->key_len
);
887 ret
= ccp_copy_to_ksb(cmd_q
, &key
, op
.jobid
, op
.ksb_key
,
888 CCP_PASSTHRU_BYTESWAP_256BIT
);
890 cmd
->engine_error
= cmd_q
->cmd_error
;
894 /* The AES context fits in a single (32-byte) KSB entry and
895 * for XTS is already in little endian format so no byte swapping
898 ret
= ccp_init_dm_workarea(&ctx
, cmd_q
,
899 CCP_XTS_AES_CTX_KSB_COUNT
* CCP_KSB_BYTES
,
904 ccp_set_dm_area(&ctx
, 0, xts
->iv
, 0, xts
->iv_len
);
905 ret
= ccp_copy_to_ksb(cmd_q
, &ctx
, op
.jobid
, op
.ksb_ctx
,
906 CCP_PASSTHRU_BYTESWAP_NOOP
);
908 cmd
->engine_error
= cmd_q
->cmd_error
;
912 /* Prepare the input and output data workareas. For in-place
913 * operations we need to set the dma direction to BIDIRECTIONAL
914 * and copy the src workarea to the dst workarea.
916 if (sg_virt(xts
->src
) == sg_virt(xts
->dst
))
919 ret
= ccp_init_data(&src
, cmd_q
, xts
->src
, xts
->src_len
,
921 in_place
? DMA_BIDIRECTIONAL
: DMA_TO_DEVICE
);
928 ret
= ccp_init_data(&dst
, cmd_q
, xts
->dst
, xts
->src_len
,
929 unit_size
, DMA_FROM_DEVICE
);
934 /* Send data to the CCP AES engine */
935 while (src
.sg_wa
.bytes_left
) {
936 ccp_prepare_data(&src
, &dst
, &op
, unit_size
, true);
937 if (!src
.sg_wa
.bytes_left
)
940 ret
= cmd_q
->ccp
->vdata
->perform
->perform_xts_aes(&op
);
942 cmd
->engine_error
= cmd_q
->cmd_error
;
946 ccp_process_data(&src
, &dst
, &op
);
949 /* Retrieve the AES context - convert from LE to BE using
950 * 32-byte (256-bit) byteswapping
952 ret
= ccp_copy_from_ksb(cmd_q
, &ctx
, op
.jobid
, op
.ksb_ctx
,
953 CCP_PASSTHRU_BYTESWAP_256BIT
);
955 cmd
->engine_error
= cmd_q
->cmd_error
;
959 /* ...but we only need AES_BLOCK_SIZE bytes */
960 dm_offset
= CCP_KSB_BYTES
- AES_BLOCK_SIZE
;
961 ccp_get_dm_area(&ctx
, dm_offset
, xts
->iv
, 0, xts
->iv_len
);
965 ccp_free_data(&dst
, cmd_q
);
968 ccp_free_data(&src
, cmd_q
);
979 static int ccp_run_sha_cmd(struct ccp_cmd_queue
*cmd_q
, struct ccp_cmd
*cmd
)
981 struct ccp_sha_engine
*sha
= &cmd
->u
.sha
;
982 struct ccp_dm_workarea ctx
;
987 if (sha
->ctx_len
!= CCP_SHA_CTXSIZE
)
993 if (!sha
->final
&& (sha
->src_len
& (CCP_SHA_BLOCKSIZE
- 1)))
999 /* Not final, just return */
1003 /* CCP can't do a zero length sha operation so the caller
1004 * must buffer the data.
1009 /* The CCP cannot perform zero-length sha operations so the
1010 * caller is required to buffer data for the final operation.
1011 * However, a sha operation for a message with a total length
1012 * of zero is valid so known values are required to supply
1015 switch (sha
->type
) {
1016 case CCP_SHA_TYPE_1
:
1017 sha_zero
= sha1_zero_message_hash
;
1019 case CCP_SHA_TYPE_224
:
1020 sha_zero
= sha224_zero_message_hash
;
1022 case CCP_SHA_TYPE_256
:
1023 sha_zero
= sha256_zero_message_hash
;
1029 scatterwalk_map_and_copy((void *)sha_zero
, sha
->ctx
, 0,
1038 BUILD_BUG_ON(CCP_SHA_KSB_COUNT
!= 1);
1040 memset(&op
, 0, sizeof(op
));
1042 op
.jobid
= ccp_gen_jobid(cmd_q
->ccp
);
1043 op
.ksb_ctx
= cmd_q
->ksb_ctx
;
1044 op
.u
.sha
.type
= sha
->type
;
1045 op
.u
.sha
.msg_bits
= sha
->msg_bits
;
1047 /* The SHA context fits in a single (32-byte) KSB entry and
1048 * must be in little endian format. Use the 256-bit byte swap
1049 * passthru option to convert from big endian to little endian.
1051 ret
= ccp_init_dm_workarea(&ctx
, cmd_q
,
1052 CCP_SHA_KSB_COUNT
* CCP_KSB_BYTES
,
1060 switch (sha
->type
) {
1061 case CCP_SHA_TYPE_1
:
1062 init
= ccp_sha1_init
;
1064 case CCP_SHA_TYPE_224
:
1065 init
= ccp_sha224_init
;
1067 case CCP_SHA_TYPE_256
:
1068 init
= ccp_sha256_init
;
1074 memcpy(ctx
.address
, init
, CCP_SHA_CTXSIZE
);
1076 ccp_set_dm_area(&ctx
, 0, sha
->ctx
, 0, sha
->ctx_len
);
1079 ret
= ccp_copy_to_ksb(cmd_q
, &ctx
, op
.jobid
, op
.ksb_ctx
,
1080 CCP_PASSTHRU_BYTESWAP_256BIT
);
1082 cmd
->engine_error
= cmd_q
->cmd_error
;
1086 /* Send data to the CCP SHA engine */
1087 ret
= ccp_init_data(&src
, cmd_q
, sha
->src
, sha
->src_len
,
1088 CCP_SHA_BLOCKSIZE
, DMA_TO_DEVICE
);
1092 while (src
.sg_wa
.bytes_left
) {
1093 ccp_prepare_data(&src
, NULL
, &op
, CCP_SHA_BLOCKSIZE
, false);
1094 if (sha
->final
&& !src
.sg_wa
.bytes_left
)
1097 ret
= cmd_q
->ccp
->vdata
->perform
->perform_sha(&op
);
1099 cmd
->engine_error
= cmd_q
->cmd_error
;
1103 ccp_process_data(&src
, NULL
, &op
);
1106 /* Retrieve the SHA context - convert from LE to BE using
1107 * 32-byte (256-bit) byteswapping to BE
1109 ret
= ccp_copy_from_ksb(cmd_q
, &ctx
, op
.jobid
, op
.ksb_ctx
,
1110 CCP_PASSTHRU_BYTESWAP_256BIT
);
1112 cmd
->engine_error
= cmd_q
->cmd_error
;
1116 ccp_get_dm_area(&ctx
, 0, sha
->ctx
, 0, sha
->ctx_len
);
1118 if (sha
->final
&& sha
->opad
) {
1119 /* HMAC operation, recursively perform final SHA */
1120 struct ccp_cmd hmac_cmd
;
1121 struct scatterlist sg
;
1122 u64 block_size
, digest_size
;
1125 switch (sha
->type
) {
1126 case CCP_SHA_TYPE_1
:
1127 block_size
= SHA1_BLOCK_SIZE
;
1128 digest_size
= SHA1_DIGEST_SIZE
;
1130 case CCP_SHA_TYPE_224
:
1131 block_size
= SHA224_BLOCK_SIZE
;
1132 digest_size
= SHA224_DIGEST_SIZE
;
1134 case CCP_SHA_TYPE_256
:
1135 block_size
= SHA256_BLOCK_SIZE
;
1136 digest_size
= SHA256_DIGEST_SIZE
;
1143 if (sha
->opad_len
!= block_size
) {
1148 hmac_buf
= kmalloc(block_size
+ digest_size
, GFP_KERNEL
);
1153 sg_init_one(&sg
, hmac_buf
, block_size
+ digest_size
);
1155 scatterwalk_map_and_copy(hmac_buf
, sha
->opad
, 0, block_size
, 0);
1156 memcpy(hmac_buf
+ block_size
, ctx
.address
, digest_size
);
1158 memset(&hmac_cmd
, 0, sizeof(hmac_cmd
));
1159 hmac_cmd
.engine
= CCP_ENGINE_SHA
;
1160 hmac_cmd
.u
.sha
.type
= sha
->type
;
1161 hmac_cmd
.u
.sha
.ctx
= sha
->ctx
;
1162 hmac_cmd
.u
.sha
.ctx_len
= sha
->ctx_len
;
1163 hmac_cmd
.u
.sha
.src
= &sg
;
1164 hmac_cmd
.u
.sha
.src_len
= block_size
+ digest_size
;
1165 hmac_cmd
.u
.sha
.opad
= NULL
;
1166 hmac_cmd
.u
.sha
.opad_len
= 0;
1167 hmac_cmd
.u
.sha
.first
= 1;
1168 hmac_cmd
.u
.sha
.final
= 1;
1169 hmac_cmd
.u
.sha
.msg_bits
= (block_size
+ digest_size
) << 3;
1171 ret
= ccp_run_sha_cmd(cmd_q
, &hmac_cmd
);
1173 cmd
->engine_error
= hmac_cmd
.engine_error
;
1179 ccp_free_data(&src
, cmd_q
);
1187 static int ccp_run_rsa_cmd(struct ccp_cmd_queue
*cmd_q
, struct ccp_cmd
*cmd
)
1189 struct ccp_rsa_engine
*rsa
= &cmd
->u
.rsa
;
1190 struct ccp_dm_workarea exp
, src
;
1191 struct ccp_data dst
;
1193 unsigned int ksb_count
, i_len
, o_len
;
1196 if (rsa
->key_size
> CCP_RSA_MAX_WIDTH
)
1199 if (!rsa
->exp
|| !rsa
->mod
|| !rsa
->src
|| !rsa
->dst
)
1202 /* The RSA modulus must precede the message being acted upon, so
1203 * it must be copied to a DMA area where the message and the
1204 * modulus can be concatenated. Therefore the input buffer
1205 * length required is twice the output buffer length (which
1206 * must be a multiple of 256-bits).
1208 o_len
= ((rsa
->key_size
+ 255) / 256) * 32;
1211 ksb_count
= o_len
/ CCP_KSB_BYTES
;
1213 memset(&op
, 0, sizeof(op
));
1215 op
.jobid
= ccp_gen_jobid(cmd_q
->ccp
);
1216 op
.ksb_key
= ccp_alloc_ksb(cmd_q
->ccp
, ksb_count
);
1220 /* The RSA exponent may span multiple (32-byte) KSB entries and must
1221 * be in little endian format. Reverse copy each 32-byte chunk
1222 * of the exponent (En chunk to E0 chunk, E(n-1) chunk to E1 chunk)
1223 * and each byte within that chunk and do not perform any byte swap
1224 * operations on the passthru operation.
1226 ret
= ccp_init_dm_workarea(&exp
, cmd_q
, o_len
, DMA_TO_DEVICE
);
1230 ret
= ccp_reverse_set_dm_area(&exp
, rsa
->exp
, rsa
->exp_len
,
1231 CCP_KSB_BYTES
, false);
1234 ret
= ccp_copy_to_ksb(cmd_q
, &exp
, op
.jobid
, op
.ksb_key
,
1235 CCP_PASSTHRU_BYTESWAP_NOOP
);
1237 cmd
->engine_error
= cmd_q
->cmd_error
;
1241 /* Concatenate the modulus and the message. Both the modulus and
1242 * the operands must be in little endian format. Since the input
1243 * is in big endian format it must be converted.
1245 ret
= ccp_init_dm_workarea(&src
, cmd_q
, i_len
, DMA_TO_DEVICE
);
1249 ret
= ccp_reverse_set_dm_area(&src
, rsa
->mod
, rsa
->mod_len
,
1250 CCP_KSB_BYTES
, false);
1253 src
.address
+= o_len
; /* Adjust the address for the copy operation */
1254 ret
= ccp_reverse_set_dm_area(&src
, rsa
->src
, rsa
->src_len
,
1255 CCP_KSB_BYTES
, false);
1258 src
.address
-= o_len
; /* Reset the address to original value */
1260 /* Prepare the output area for the operation */
1261 ret
= ccp_init_data(&dst
, cmd_q
, rsa
->dst
, rsa
->mod_len
,
1262 o_len
, DMA_FROM_DEVICE
);
1267 op
.src
.u
.dma
.address
= src
.dma
.address
;
1268 op
.src
.u
.dma
.offset
= 0;
1269 op
.src
.u
.dma
.length
= i_len
;
1270 op
.dst
.u
.dma
.address
= dst
.dm_wa
.dma
.address
;
1271 op
.dst
.u
.dma
.offset
= 0;
1272 op
.dst
.u
.dma
.length
= o_len
;
1274 op
.u
.rsa
.mod_size
= rsa
->key_size
;
1275 op
.u
.rsa
.input_len
= i_len
;
1277 ret
= cmd_q
->ccp
->vdata
->perform
->perform_rsa(&op
);
1279 cmd
->engine_error
= cmd_q
->cmd_error
;
1283 ccp_reverse_get_dm_area(&dst
.dm_wa
, rsa
->dst
, rsa
->mod_len
);
1286 ccp_free_data(&dst
, cmd_q
);
1295 ccp_free_ksb(cmd_q
->ccp
, op
.ksb_key
, ksb_count
);
1300 static int ccp_run_passthru_cmd(struct ccp_cmd_queue
*cmd_q
,
1301 struct ccp_cmd
*cmd
)
1303 struct ccp_passthru_engine
*pt
= &cmd
->u
.passthru
;
1304 struct ccp_dm_workarea mask
;
1305 struct ccp_data src
, dst
;
1307 bool in_place
= false;
1311 if (!pt
->final
&& (pt
->src_len
& (CCP_PASSTHRU_BLOCKSIZE
- 1)))
1314 if (!pt
->src
|| !pt
->dst
)
1317 if (pt
->bit_mod
!= CCP_PASSTHRU_BITWISE_NOOP
) {
1318 if (pt
->mask_len
!= CCP_PASSTHRU_MASKSIZE
)
1324 BUILD_BUG_ON(CCP_PASSTHRU_KSB_COUNT
!= 1);
1326 memset(&op
, 0, sizeof(op
));
1328 op
.jobid
= ccp_gen_jobid(cmd_q
->ccp
);
1330 if (pt
->bit_mod
!= CCP_PASSTHRU_BITWISE_NOOP
) {
1332 op
.ksb_key
= cmd_q
->ksb_key
;
1334 ret
= ccp_init_dm_workarea(&mask
, cmd_q
,
1335 CCP_PASSTHRU_KSB_COUNT
*
1341 ccp_set_dm_area(&mask
, 0, pt
->mask
, 0, pt
->mask_len
);
1342 ret
= ccp_copy_to_ksb(cmd_q
, &mask
, op
.jobid
, op
.ksb_key
,
1343 CCP_PASSTHRU_BYTESWAP_NOOP
);
1345 cmd
->engine_error
= cmd_q
->cmd_error
;
1350 /* Prepare the input and output data workareas. For in-place
1351 * operations we need to set the dma direction to BIDIRECTIONAL
1352 * and copy the src workarea to the dst workarea.
1354 if (sg_virt(pt
->src
) == sg_virt(pt
->dst
))
1357 ret
= ccp_init_data(&src
, cmd_q
, pt
->src
, pt
->src_len
,
1358 CCP_PASSTHRU_MASKSIZE
,
1359 in_place
? DMA_BIDIRECTIONAL
: DMA_TO_DEVICE
);
1366 ret
= ccp_init_data(&dst
, cmd_q
, pt
->dst
, pt
->src_len
,
1367 CCP_PASSTHRU_MASKSIZE
, DMA_FROM_DEVICE
);
1372 /* Send data to the CCP Passthru engine
1373 * Because the CCP engine works on a single source and destination
1374 * dma address at a time, each entry in the source scatterlist
1375 * (after the dma_map_sg call) must be less than or equal to the
1376 * (remaining) length in the destination scatterlist entry and the
1377 * length must be a multiple of CCP_PASSTHRU_BLOCKSIZE
1379 dst
.sg_wa
.sg_used
= 0;
1380 for (i
= 1; i
<= src
.sg_wa
.dma_count
; i
++) {
1381 if (!dst
.sg_wa
.sg
||
1382 (dst
.sg_wa
.sg
->length
< src
.sg_wa
.sg
->length
)) {
1387 if (i
== src
.sg_wa
.dma_count
) {
1392 op
.src
.type
= CCP_MEMTYPE_SYSTEM
;
1393 op
.src
.u
.dma
.address
= sg_dma_address(src
.sg_wa
.sg
);
1394 op
.src
.u
.dma
.offset
= 0;
1395 op
.src
.u
.dma
.length
= sg_dma_len(src
.sg_wa
.sg
);
1397 op
.dst
.type
= CCP_MEMTYPE_SYSTEM
;
1398 op
.dst
.u
.dma
.address
= sg_dma_address(dst
.sg_wa
.sg
);
1399 op
.dst
.u
.dma
.offset
= dst
.sg_wa
.sg_used
;
1400 op
.dst
.u
.dma
.length
= op
.src
.u
.dma
.length
;
1402 ret
= cmd_q
->ccp
->vdata
->perform
->perform_passthru(&op
);
1404 cmd
->engine_error
= cmd_q
->cmd_error
;
1408 dst
.sg_wa
.sg_used
+= src
.sg_wa
.sg
->length
;
1409 if (dst
.sg_wa
.sg_used
== dst
.sg_wa
.sg
->length
) {
1410 dst
.sg_wa
.sg
= sg_next(dst
.sg_wa
.sg
);
1411 dst
.sg_wa
.sg_used
= 0;
1413 src
.sg_wa
.sg
= sg_next(src
.sg_wa
.sg
);
1418 ccp_free_data(&dst
, cmd_q
);
1421 ccp_free_data(&src
, cmd_q
);
1424 if (pt
->bit_mod
!= CCP_PASSTHRU_BITWISE_NOOP
)
1430 static int ccp_run_passthru_nomap_cmd(struct ccp_cmd_queue
*cmd_q
,
1431 struct ccp_cmd
*cmd
)
1433 struct ccp_passthru_nomap_engine
*pt
= &cmd
->u
.passthru_nomap
;
1434 struct ccp_dm_workarea mask
;
1438 if (!pt
->final
&& (pt
->src_len
& (CCP_PASSTHRU_BLOCKSIZE
- 1)))
1441 if (!pt
->src_dma
|| !pt
->dst_dma
)
1444 if (pt
->bit_mod
!= CCP_PASSTHRU_BITWISE_NOOP
) {
1445 if (pt
->mask_len
!= CCP_PASSTHRU_MASKSIZE
)
1451 BUILD_BUG_ON(CCP_PASSTHRU_KSB_COUNT
!= 1);
1453 memset(&op
, 0, sizeof(op
));
1455 op
.jobid
= ccp_gen_jobid(cmd_q
->ccp
);
1457 if (pt
->bit_mod
!= CCP_PASSTHRU_BITWISE_NOOP
) {
1459 op
.ksb_key
= cmd_q
->ksb_key
;
1461 mask
.length
= pt
->mask_len
;
1462 mask
.dma
.address
= pt
->mask
;
1463 mask
.dma
.length
= pt
->mask_len
;
1465 ret
= ccp_copy_to_ksb(cmd_q
, &mask
, op
.jobid
, op
.ksb_key
,
1466 CCP_PASSTHRU_BYTESWAP_NOOP
);
1468 cmd
->engine_error
= cmd_q
->cmd_error
;
1473 /* Send data to the CCP Passthru engine */
1477 op
.src
.type
= CCP_MEMTYPE_SYSTEM
;
1478 op
.src
.u
.dma
.address
= pt
->src_dma
;
1479 op
.src
.u
.dma
.offset
= 0;
1480 op
.src
.u
.dma
.length
= pt
->src_len
;
1482 op
.dst
.type
= CCP_MEMTYPE_SYSTEM
;
1483 op
.dst
.u
.dma
.address
= pt
->dst_dma
;
1484 op
.dst
.u
.dma
.offset
= 0;
1485 op
.dst
.u
.dma
.length
= pt
->src_len
;
1487 ret
= cmd_q
->ccp
->vdata
->perform
->perform_passthru(&op
);
1489 cmd
->engine_error
= cmd_q
->cmd_error
;
1494 static int ccp_run_ecc_mm_cmd(struct ccp_cmd_queue
*cmd_q
, struct ccp_cmd
*cmd
)
1496 struct ccp_ecc_engine
*ecc
= &cmd
->u
.ecc
;
1497 struct ccp_dm_workarea src
, dst
;
1502 if (!ecc
->u
.mm
.operand_1
||
1503 (ecc
->u
.mm
.operand_1_len
> CCP_ECC_MODULUS_BYTES
))
1506 if (ecc
->function
!= CCP_ECC_FUNCTION_MINV_384BIT
)
1507 if (!ecc
->u
.mm
.operand_2
||
1508 (ecc
->u
.mm
.operand_2_len
> CCP_ECC_MODULUS_BYTES
))
1511 if (!ecc
->u
.mm
.result
||
1512 (ecc
->u
.mm
.result_len
< CCP_ECC_MODULUS_BYTES
))
1515 memset(&op
, 0, sizeof(op
));
1517 op
.jobid
= ccp_gen_jobid(cmd_q
->ccp
);
1519 /* Concatenate the modulus and the operands. Both the modulus and
1520 * the operands must be in little endian format. Since the input
1521 * is in big endian format it must be converted and placed in a
1522 * fixed length buffer.
1524 ret
= ccp_init_dm_workarea(&src
, cmd_q
, CCP_ECC_SRC_BUF_SIZE
,
1529 /* Save the workarea address since it is updated in order to perform
1534 /* Copy the ECC modulus */
1535 ret
= ccp_reverse_set_dm_area(&src
, ecc
->mod
, ecc
->mod_len
,
1536 CCP_ECC_OPERAND_SIZE
, false);
1539 src
.address
+= CCP_ECC_OPERAND_SIZE
;
1541 /* Copy the first operand */
1542 ret
= ccp_reverse_set_dm_area(&src
, ecc
->u
.mm
.operand_1
,
1543 ecc
->u
.mm
.operand_1_len
,
1544 CCP_ECC_OPERAND_SIZE
, false);
1547 src
.address
+= CCP_ECC_OPERAND_SIZE
;
1549 if (ecc
->function
!= CCP_ECC_FUNCTION_MINV_384BIT
) {
1550 /* Copy the second operand */
1551 ret
= ccp_reverse_set_dm_area(&src
, ecc
->u
.mm
.operand_2
,
1552 ecc
->u
.mm
.operand_2_len
,
1553 CCP_ECC_OPERAND_SIZE
, false);
1556 src
.address
+= CCP_ECC_OPERAND_SIZE
;
1559 /* Restore the workarea address */
1562 /* Prepare the output area for the operation */
1563 ret
= ccp_init_dm_workarea(&dst
, cmd_q
, CCP_ECC_DST_BUF_SIZE
,
1569 op
.src
.u
.dma
.address
= src
.dma
.address
;
1570 op
.src
.u
.dma
.offset
= 0;
1571 op
.src
.u
.dma
.length
= src
.length
;
1572 op
.dst
.u
.dma
.address
= dst
.dma
.address
;
1573 op
.dst
.u
.dma
.offset
= 0;
1574 op
.dst
.u
.dma
.length
= dst
.length
;
1576 op
.u
.ecc
.function
= cmd
->u
.ecc
.function
;
1578 ret
= cmd_q
->ccp
->vdata
->perform
->perform_ecc(&op
);
1580 cmd
->engine_error
= cmd_q
->cmd_error
;
1584 ecc
->ecc_result
= le16_to_cpup(
1585 (const __le16
*)(dst
.address
+ CCP_ECC_RESULT_OFFSET
));
1586 if (!(ecc
->ecc_result
& CCP_ECC_RESULT_SUCCESS
)) {
1591 /* Save the ECC result */
1592 ccp_reverse_get_dm_area(&dst
, ecc
->u
.mm
.result
, CCP_ECC_MODULUS_BYTES
);
1603 static int ccp_run_ecc_pm_cmd(struct ccp_cmd_queue
*cmd_q
, struct ccp_cmd
*cmd
)
1605 struct ccp_ecc_engine
*ecc
= &cmd
->u
.ecc
;
1606 struct ccp_dm_workarea src
, dst
;
1611 if (!ecc
->u
.pm
.point_1
.x
||
1612 (ecc
->u
.pm
.point_1
.x_len
> CCP_ECC_MODULUS_BYTES
) ||
1613 !ecc
->u
.pm
.point_1
.y
||
1614 (ecc
->u
.pm
.point_1
.y_len
> CCP_ECC_MODULUS_BYTES
))
1617 if (ecc
->function
== CCP_ECC_FUNCTION_PADD_384BIT
) {
1618 if (!ecc
->u
.pm
.point_2
.x
||
1619 (ecc
->u
.pm
.point_2
.x_len
> CCP_ECC_MODULUS_BYTES
) ||
1620 !ecc
->u
.pm
.point_2
.y
||
1621 (ecc
->u
.pm
.point_2
.y_len
> CCP_ECC_MODULUS_BYTES
))
1624 if (!ecc
->u
.pm
.domain_a
||
1625 (ecc
->u
.pm
.domain_a_len
> CCP_ECC_MODULUS_BYTES
))
1628 if (ecc
->function
== CCP_ECC_FUNCTION_PMUL_384BIT
)
1629 if (!ecc
->u
.pm
.scalar
||
1630 (ecc
->u
.pm
.scalar_len
> CCP_ECC_MODULUS_BYTES
))
1634 if (!ecc
->u
.pm
.result
.x
||
1635 (ecc
->u
.pm
.result
.x_len
< CCP_ECC_MODULUS_BYTES
) ||
1636 !ecc
->u
.pm
.result
.y
||
1637 (ecc
->u
.pm
.result
.y_len
< CCP_ECC_MODULUS_BYTES
))
1640 memset(&op
, 0, sizeof(op
));
1642 op
.jobid
= ccp_gen_jobid(cmd_q
->ccp
);
1644 /* Concatenate the modulus and the operands. Both the modulus and
1645 * the operands must be in little endian format. Since the input
1646 * is in big endian format it must be converted and placed in a
1647 * fixed length buffer.
1649 ret
= ccp_init_dm_workarea(&src
, cmd_q
, CCP_ECC_SRC_BUF_SIZE
,
1654 /* Save the workarea address since it is updated in order to perform
1659 /* Copy the ECC modulus */
1660 ret
= ccp_reverse_set_dm_area(&src
, ecc
->mod
, ecc
->mod_len
,
1661 CCP_ECC_OPERAND_SIZE
, false);
1664 src
.address
+= CCP_ECC_OPERAND_SIZE
;
1666 /* Copy the first point X and Y coordinate */
1667 ret
= ccp_reverse_set_dm_area(&src
, ecc
->u
.pm
.point_1
.x
,
1668 ecc
->u
.pm
.point_1
.x_len
,
1669 CCP_ECC_OPERAND_SIZE
, false);
1672 src
.address
+= CCP_ECC_OPERAND_SIZE
;
1673 ret
= ccp_reverse_set_dm_area(&src
, ecc
->u
.pm
.point_1
.y
,
1674 ecc
->u
.pm
.point_1
.y_len
,
1675 CCP_ECC_OPERAND_SIZE
, false);
1678 src
.address
+= CCP_ECC_OPERAND_SIZE
;
1680 /* Set the first point Z coordianate to 1 */
1681 *src
.address
= 0x01;
1682 src
.address
+= CCP_ECC_OPERAND_SIZE
;
1684 if (ecc
->function
== CCP_ECC_FUNCTION_PADD_384BIT
) {
1685 /* Copy the second point X and Y coordinate */
1686 ret
= ccp_reverse_set_dm_area(&src
, ecc
->u
.pm
.point_2
.x
,
1687 ecc
->u
.pm
.point_2
.x_len
,
1688 CCP_ECC_OPERAND_SIZE
, false);
1691 src
.address
+= CCP_ECC_OPERAND_SIZE
;
1692 ret
= ccp_reverse_set_dm_area(&src
, ecc
->u
.pm
.point_2
.y
,
1693 ecc
->u
.pm
.point_2
.y_len
,
1694 CCP_ECC_OPERAND_SIZE
, false);
1697 src
.address
+= CCP_ECC_OPERAND_SIZE
;
1699 /* Set the second point Z coordianate to 1 */
1700 *src
.address
= 0x01;
1701 src
.address
+= CCP_ECC_OPERAND_SIZE
;
1703 /* Copy the Domain "a" parameter */
1704 ret
= ccp_reverse_set_dm_area(&src
, ecc
->u
.pm
.domain_a
,
1705 ecc
->u
.pm
.domain_a_len
,
1706 CCP_ECC_OPERAND_SIZE
, false);
1709 src
.address
+= CCP_ECC_OPERAND_SIZE
;
1711 if (ecc
->function
== CCP_ECC_FUNCTION_PMUL_384BIT
) {
1712 /* Copy the scalar value */
1713 ret
= ccp_reverse_set_dm_area(&src
, ecc
->u
.pm
.scalar
,
1714 ecc
->u
.pm
.scalar_len
,
1715 CCP_ECC_OPERAND_SIZE
,
1719 src
.address
+= CCP_ECC_OPERAND_SIZE
;
1723 /* Restore the workarea address */
1726 /* Prepare the output area for the operation */
1727 ret
= ccp_init_dm_workarea(&dst
, cmd_q
, CCP_ECC_DST_BUF_SIZE
,
1733 op
.src
.u
.dma
.address
= src
.dma
.address
;
1734 op
.src
.u
.dma
.offset
= 0;
1735 op
.src
.u
.dma
.length
= src
.length
;
1736 op
.dst
.u
.dma
.address
= dst
.dma
.address
;
1737 op
.dst
.u
.dma
.offset
= 0;
1738 op
.dst
.u
.dma
.length
= dst
.length
;
1740 op
.u
.ecc
.function
= cmd
->u
.ecc
.function
;
1742 ret
= cmd_q
->ccp
->vdata
->perform
->perform_ecc(&op
);
1744 cmd
->engine_error
= cmd_q
->cmd_error
;
1748 ecc
->ecc_result
= le16_to_cpup(
1749 (const __le16
*)(dst
.address
+ CCP_ECC_RESULT_OFFSET
));
1750 if (!(ecc
->ecc_result
& CCP_ECC_RESULT_SUCCESS
)) {
1755 /* Save the workarea address since it is updated as we walk through
1756 * to copy the point math result
1760 /* Save the ECC result X and Y coordinates */
1761 ccp_reverse_get_dm_area(&dst
, ecc
->u
.pm
.result
.x
,
1762 CCP_ECC_MODULUS_BYTES
);
1763 dst
.address
+= CCP_ECC_OUTPUT_SIZE
;
1764 ccp_reverse_get_dm_area(&dst
, ecc
->u
.pm
.result
.y
,
1765 CCP_ECC_MODULUS_BYTES
);
1766 dst
.address
+= CCP_ECC_OUTPUT_SIZE
;
1768 /* Restore the workarea address */
1780 static int ccp_run_ecc_cmd(struct ccp_cmd_queue
*cmd_q
, struct ccp_cmd
*cmd
)
1782 struct ccp_ecc_engine
*ecc
= &cmd
->u
.ecc
;
1784 ecc
->ecc_result
= 0;
1787 (ecc
->mod_len
> CCP_ECC_MODULUS_BYTES
))
1790 switch (ecc
->function
) {
1791 case CCP_ECC_FUNCTION_MMUL_384BIT
:
1792 case CCP_ECC_FUNCTION_MADD_384BIT
:
1793 case CCP_ECC_FUNCTION_MINV_384BIT
:
1794 return ccp_run_ecc_mm_cmd(cmd_q
, cmd
);
1796 case CCP_ECC_FUNCTION_PADD_384BIT
:
1797 case CCP_ECC_FUNCTION_PMUL_384BIT
:
1798 case CCP_ECC_FUNCTION_PDBL_384BIT
:
1799 return ccp_run_ecc_pm_cmd(cmd_q
, cmd
);
1806 int ccp_run_cmd(struct ccp_cmd_queue
*cmd_q
, struct ccp_cmd
*cmd
)
1810 cmd
->engine_error
= 0;
1811 cmd_q
->cmd_error
= 0;
1812 cmd_q
->int_rcvd
= 0;
1813 cmd_q
->free_slots
= CMD_Q_DEPTH(ioread32(cmd_q
->reg_status
));
1815 switch (cmd
->engine
) {
1816 case CCP_ENGINE_AES
:
1817 ret
= ccp_run_aes_cmd(cmd_q
, cmd
);
1819 case CCP_ENGINE_XTS_AES_128
:
1820 ret
= ccp_run_xts_aes_cmd(cmd_q
, cmd
);
1822 case CCP_ENGINE_SHA
:
1823 ret
= ccp_run_sha_cmd(cmd_q
, cmd
);
1825 case CCP_ENGINE_RSA
:
1826 ret
= ccp_run_rsa_cmd(cmd_q
, cmd
);
1828 case CCP_ENGINE_PASSTHRU
:
1829 if (cmd
->flags
& CCP_CMD_PASSTHRU_NO_DMA_MAP
)
1830 ret
= ccp_run_passthru_nomap_cmd(cmd_q
, cmd
);
1832 ret
= ccp_run_passthru_cmd(cmd_q
, cmd
);
1834 case CCP_ENGINE_ECC
:
1835 ret
= ccp_run_ecc_cmd(cmd_q
, cmd
);