blob: 542453c9ba1c87c2885090a3ec280b8df30ccb60 [file] [log] [blame]
Tom Lendacky63b94502013-11-12 11:46:16 -06001/*
2 * AMD Cryptographic Coprocessor (CCP) driver
3 *
4 * Copyright (C) 2013 Advanced Micro Devices, Inc.
5 *
6 * Author: Tom Lendacky <thomas.lendacky@amd.com>
7 *
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.
11 */
12
13#include <linux/module.h>
14#include <linux/kernel.h>
15#include <linux/pci.h>
16#include <linux/pci_ids.h>
17#include <linux/kthread.h>
18#include <linux/sched.h>
19#include <linux/interrupt.h>
20#include <linux/spinlock.h>
21#include <linux/mutex.h>
22#include <linux/delay.h>
23#include <linux/ccp.h>
24#include <linux/scatterlist.h>
25#include <crypto/scatterwalk.h>
Tom Lendackyc11baa02014-01-24 16:18:02 -060026#include <crypto/sha.h>
Tom Lendacky63b94502013-11-12 11:46:16 -060027
28#include "ccp-dev.h"
29
Tom Lendacky63b94502013-11-12 11:46:16 -060030enum ccp_memtype {
31 CCP_MEMTYPE_SYSTEM = 0,
32 CCP_MEMTYPE_KSB,
33 CCP_MEMTYPE_LOCAL,
34 CCP_MEMTYPE__LAST,
35};
36
37struct ccp_dma_info {
38 dma_addr_t address;
39 unsigned int offset;
40 unsigned int length;
41 enum dma_data_direction dir;
42};
43
44struct ccp_dm_workarea {
45 struct device *dev;
46 struct dma_pool *dma_pool;
47 unsigned int length;
48
49 u8 *address;
50 struct ccp_dma_info dma;
51};
52
53struct ccp_sg_workarea {
54 struct scatterlist *sg;
55 unsigned int nents;
Tom Lendacky63b94502013-11-12 11:46:16 -060056
57 struct scatterlist *dma_sg;
58 struct device *dma_dev;
59 unsigned int dma_count;
60 enum dma_data_direction dma_dir;
61
Tom Lendacky81a59f02014-01-06 13:34:17 -060062 unsigned int sg_used;
Tom Lendacky63b94502013-11-12 11:46:16 -060063
Tom Lendacky81a59f02014-01-06 13:34:17 -060064 u64 bytes_left;
Tom Lendacky63b94502013-11-12 11:46:16 -060065};
66
67struct ccp_data {
68 struct ccp_sg_workarea sg_wa;
69 struct ccp_dm_workarea dm_wa;
70};
71
72struct ccp_mem {
73 enum ccp_memtype type;
74 union {
75 struct ccp_dma_info dma;
76 u32 ksb;
77 } u;
78};
79
80struct ccp_aes_op {
81 enum ccp_aes_type type;
82 enum ccp_aes_mode mode;
83 enum ccp_aes_action action;
84};
85
86struct ccp_xts_aes_op {
87 enum ccp_aes_action action;
88 enum ccp_xts_aes_unit_size unit_size;
89};
90
91struct ccp_sha_op {
92 enum ccp_sha_type type;
93 u64 msg_bits;
94};
95
96struct ccp_rsa_op {
97 u32 mod_size;
98 u32 input_len;
99};
100
101struct ccp_passthru_op {
102 enum ccp_passthru_bitwise bit_mod;
103 enum ccp_passthru_byteswap byte_swap;
104};
105
106struct ccp_ecc_op {
107 enum ccp_ecc_function function;
108};
109
110struct ccp_op {
111 struct ccp_cmd_queue *cmd_q;
112
113 u32 jobid;
114 u32 ioc;
115 u32 soc;
116 u32 ksb_key;
117 u32 ksb_ctx;
118 u32 init;
119 u32 eom;
120
121 struct ccp_mem src;
122 struct ccp_mem dst;
123
124 union {
125 struct ccp_aes_op aes;
126 struct ccp_xts_aes_op xts;
127 struct ccp_sha_op sha;
128 struct ccp_rsa_op rsa;
129 struct ccp_passthru_op passthru;
130 struct ccp_ecc_op ecc;
131 } u;
132};
133
Tom Lendackyc11baa02014-01-24 16:18:02 -0600134/* SHA initial context values */
135static const __be32 ccp_sha1_init[CCP_SHA_CTXSIZE / sizeof(__be32)] = {
136 cpu_to_be32(SHA1_H0), cpu_to_be32(SHA1_H1),
137 cpu_to_be32(SHA1_H2), cpu_to_be32(SHA1_H3),
138 cpu_to_be32(SHA1_H4), 0, 0, 0,
139};
140
141static const __be32 ccp_sha224_init[CCP_SHA_CTXSIZE / sizeof(__be32)] = {
142 cpu_to_be32(SHA224_H0), cpu_to_be32(SHA224_H1),
143 cpu_to_be32(SHA224_H2), cpu_to_be32(SHA224_H3),
144 cpu_to_be32(SHA224_H4), cpu_to_be32(SHA224_H5),
145 cpu_to_be32(SHA224_H6), cpu_to_be32(SHA224_H7),
146};
147
148static const __be32 ccp_sha256_init[CCP_SHA_CTXSIZE / sizeof(__be32)] = {
149 cpu_to_be32(SHA256_H0), cpu_to_be32(SHA256_H1),
150 cpu_to_be32(SHA256_H2), cpu_to_be32(SHA256_H3),
151 cpu_to_be32(SHA256_H4), cpu_to_be32(SHA256_H5),
152 cpu_to_be32(SHA256_H6), cpu_to_be32(SHA256_H7),
153};
154
Tom Lendacky63b94502013-11-12 11:46:16 -0600155/* The CCP cannot perform zero-length sha operations so the caller
156 * is required to buffer data for the final operation. However, a
157 * sha operation for a message with a total length of zero is valid
158 * so known values are required to supply the result.
159 */
160static const u8 ccp_sha1_zero[CCP_SHA_CTXSIZE] = {
161 0xda, 0x39, 0xa3, 0xee, 0x5e, 0x6b, 0x4b, 0x0d,
162 0x32, 0x55, 0xbf, 0xef, 0x95, 0x60, 0x18, 0x90,
163 0xaf, 0xd8, 0x07, 0x09, 0x00, 0x00, 0x00, 0x00,
164 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
165};
166
167static const u8 ccp_sha224_zero[CCP_SHA_CTXSIZE] = {
168 0xd1, 0x4a, 0x02, 0x8c, 0x2a, 0x3a, 0x2b, 0xc9,
169 0x47, 0x61, 0x02, 0xbb, 0x28, 0x82, 0x34, 0xc4,
170 0x15, 0xa2, 0xb0, 0x1f, 0x82, 0x8e, 0xa6, 0x2a,
171 0xc5, 0xb3, 0xe4, 0x2f, 0x00, 0x00, 0x00, 0x00,
172};
173
174static const u8 ccp_sha256_zero[CCP_SHA_CTXSIZE] = {
175 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14,
176 0x9a, 0xfb, 0xf4, 0xc8, 0x99, 0x6f, 0xb9, 0x24,
177 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
178 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55,
179};
180
181static u32 ccp_addr_lo(struct ccp_dma_info *info)
182{
183 return lower_32_bits(info->address + info->offset);
184}
185
186static u32 ccp_addr_hi(struct ccp_dma_info *info)
187{
188 return upper_32_bits(info->address + info->offset) & 0x0000ffff;
189}
190
191static int ccp_do_cmd(struct ccp_op *op, u32 *cr, unsigned int cr_count)
192{
193 struct ccp_cmd_queue *cmd_q = op->cmd_q;
194 struct ccp_device *ccp = cmd_q->ccp;
195 void __iomem *cr_addr;
196 u32 cr0, cmd;
197 unsigned int i;
198 int ret = 0;
199
200 /* We could read a status register to see how many free slots
201 * are actually available, but reading that register resets it
202 * and you could lose some error information.
203 */
204 cmd_q->free_slots--;
205
206 cr0 = (cmd_q->id << REQ0_CMD_Q_SHIFT)
207 | (op->jobid << REQ0_JOBID_SHIFT)
208 | REQ0_WAIT_FOR_WRITE;
209
210 if (op->soc)
211 cr0 |= REQ0_STOP_ON_COMPLETE
212 | REQ0_INT_ON_COMPLETE;
213
214 if (op->ioc || !cmd_q->free_slots)
215 cr0 |= REQ0_INT_ON_COMPLETE;
216
217 /* Start at CMD_REQ1 */
218 cr_addr = ccp->io_regs + CMD_REQ0 + CMD_REQ_INCR;
219
220 mutex_lock(&ccp->req_mutex);
221
222 /* Write CMD_REQ1 through CMD_REQx first */
223 for (i = 0; i < cr_count; i++, cr_addr += CMD_REQ_INCR)
224 iowrite32(*(cr + i), cr_addr);
225
226 /* Tell the CCP to start */
227 wmb();
228 iowrite32(cr0, ccp->io_regs + CMD_REQ0);
229
230 mutex_unlock(&ccp->req_mutex);
231
232 if (cr0 & REQ0_INT_ON_COMPLETE) {
233 /* Wait for the job to complete */
234 ret = wait_event_interruptible(cmd_q->int_queue,
235 cmd_q->int_rcvd);
236 if (ret || cmd_q->cmd_error) {
237 /* On error delete all related jobs from the queue */
238 cmd = (cmd_q->id << DEL_Q_ID_SHIFT)
239 | op->jobid;
240
241 iowrite32(cmd, ccp->io_regs + DEL_CMD_Q_JOB);
242
243 if (!ret)
244 ret = -EIO;
245 } else if (op->soc) {
246 /* Delete just head job from the queue on SoC */
247 cmd = DEL_Q_ACTIVE
248 | (cmd_q->id << DEL_Q_ID_SHIFT)
249 | op->jobid;
250
251 iowrite32(cmd, ccp->io_regs + DEL_CMD_Q_JOB);
252 }
253
254 cmd_q->free_slots = CMD_Q_DEPTH(cmd_q->q_status);
255
256 cmd_q->int_rcvd = 0;
257 }
258
259 return ret;
260}
261
262static int ccp_perform_aes(struct ccp_op *op)
263{
264 u32 cr[6];
265
266 /* Fill out the register contents for REQ1 through REQ6 */
267 cr[0] = (CCP_ENGINE_AES << REQ1_ENGINE_SHIFT)
268 | (op->u.aes.type << REQ1_AES_TYPE_SHIFT)
269 | (op->u.aes.mode << REQ1_AES_MODE_SHIFT)
270 | (op->u.aes.action << REQ1_AES_ACTION_SHIFT)
271 | (op->ksb_key << REQ1_KEY_KSB_SHIFT);
272 cr[1] = op->src.u.dma.length - 1;
273 cr[2] = ccp_addr_lo(&op->src.u.dma);
274 cr[3] = (op->ksb_ctx << REQ4_KSB_SHIFT)
275 | (CCP_MEMTYPE_SYSTEM << REQ4_MEMTYPE_SHIFT)
276 | ccp_addr_hi(&op->src.u.dma);
277 cr[4] = ccp_addr_lo(&op->dst.u.dma);
278 cr[5] = (CCP_MEMTYPE_SYSTEM << REQ6_MEMTYPE_SHIFT)
279 | ccp_addr_hi(&op->dst.u.dma);
280
281 if (op->u.aes.mode == CCP_AES_MODE_CFB)
282 cr[0] |= ((0x7f) << REQ1_AES_CFB_SIZE_SHIFT);
283
284 if (op->eom)
285 cr[0] |= REQ1_EOM;
286
287 if (op->init)
288 cr[0] |= REQ1_INIT;
289
290 return ccp_do_cmd(op, cr, ARRAY_SIZE(cr));
291}
292
293static int ccp_perform_xts_aes(struct ccp_op *op)
294{
295 u32 cr[6];
296
297 /* Fill out the register contents for REQ1 through REQ6 */
298 cr[0] = (CCP_ENGINE_XTS_AES_128 << REQ1_ENGINE_SHIFT)
299 | (op->u.xts.action << REQ1_AES_ACTION_SHIFT)
300 | (op->u.xts.unit_size << REQ1_XTS_AES_SIZE_SHIFT)
301 | (op->ksb_key << REQ1_KEY_KSB_SHIFT);
302 cr[1] = op->src.u.dma.length - 1;
303 cr[2] = ccp_addr_lo(&op->src.u.dma);
304 cr[3] = (op->ksb_ctx << REQ4_KSB_SHIFT)
305 | (CCP_MEMTYPE_SYSTEM << REQ4_MEMTYPE_SHIFT)
306 | ccp_addr_hi(&op->src.u.dma);
307 cr[4] = ccp_addr_lo(&op->dst.u.dma);
308 cr[5] = (CCP_MEMTYPE_SYSTEM << REQ6_MEMTYPE_SHIFT)
309 | ccp_addr_hi(&op->dst.u.dma);
310
311 if (op->eom)
312 cr[0] |= REQ1_EOM;
313
314 if (op->init)
315 cr[0] |= REQ1_INIT;
316
317 return ccp_do_cmd(op, cr, ARRAY_SIZE(cr));
318}
319
320static int ccp_perform_sha(struct ccp_op *op)
321{
322 u32 cr[6];
323
324 /* Fill out the register contents for REQ1 through REQ6 */
325 cr[0] = (CCP_ENGINE_SHA << REQ1_ENGINE_SHIFT)
326 | (op->u.sha.type << REQ1_SHA_TYPE_SHIFT)
327 | REQ1_INIT;
328 cr[1] = op->src.u.dma.length - 1;
329 cr[2] = ccp_addr_lo(&op->src.u.dma);
330 cr[3] = (op->ksb_ctx << REQ4_KSB_SHIFT)
331 | (CCP_MEMTYPE_SYSTEM << REQ4_MEMTYPE_SHIFT)
332 | ccp_addr_hi(&op->src.u.dma);
333
334 if (op->eom) {
335 cr[0] |= REQ1_EOM;
336 cr[4] = lower_32_bits(op->u.sha.msg_bits);
337 cr[5] = upper_32_bits(op->u.sha.msg_bits);
338 } else {
339 cr[4] = 0;
340 cr[5] = 0;
341 }
342
343 return ccp_do_cmd(op, cr, ARRAY_SIZE(cr));
344}
345
346static int ccp_perform_rsa(struct ccp_op *op)
347{
348 u32 cr[6];
349
350 /* Fill out the register contents for REQ1 through REQ6 */
351 cr[0] = (CCP_ENGINE_RSA << REQ1_ENGINE_SHIFT)
352 | (op->u.rsa.mod_size << REQ1_RSA_MOD_SIZE_SHIFT)
353 | (op->ksb_key << REQ1_KEY_KSB_SHIFT)
354 | REQ1_EOM;
355 cr[1] = op->u.rsa.input_len - 1;
356 cr[2] = ccp_addr_lo(&op->src.u.dma);
357 cr[3] = (op->ksb_ctx << REQ4_KSB_SHIFT)
358 | (CCP_MEMTYPE_SYSTEM << REQ4_MEMTYPE_SHIFT)
359 | ccp_addr_hi(&op->src.u.dma);
360 cr[4] = ccp_addr_lo(&op->dst.u.dma);
361 cr[5] = (CCP_MEMTYPE_SYSTEM << REQ6_MEMTYPE_SHIFT)
362 | ccp_addr_hi(&op->dst.u.dma);
363
364 return ccp_do_cmd(op, cr, ARRAY_SIZE(cr));
365}
366
367static int ccp_perform_passthru(struct ccp_op *op)
368{
369 u32 cr[6];
370
371 /* Fill out the register contents for REQ1 through REQ6 */
372 cr[0] = (CCP_ENGINE_PASSTHRU << REQ1_ENGINE_SHIFT)
373 | (op->u.passthru.bit_mod << REQ1_PT_BW_SHIFT)
374 | (op->u.passthru.byte_swap << REQ1_PT_BS_SHIFT);
375
376 if (op->src.type == CCP_MEMTYPE_SYSTEM)
377 cr[1] = op->src.u.dma.length - 1;
378 else
379 cr[1] = op->dst.u.dma.length - 1;
380
381 if (op->src.type == CCP_MEMTYPE_SYSTEM) {
382 cr[2] = ccp_addr_lo(&op->src.u.dma);
383 cr[3] = (CCP_MEMTYPE_SYSTEM << REQ4_MEMTYPE_SHIFT)
384 | ccp_addr_hi(&op->src.u.dma);
385
386 if (op->u.passthru.bit_mod != CCP_PASSTHRU_BITWISE_NOOP)
387 cr[3] |= (op->ksb_key << REQ4_KSB_SHIFT);
388 } else {
389 cr[2] = op->src.u.ksb * CCP_KSB_BYTES;
390 cr[3] = (CCP_MEMTYPE_KSB << REQ4_MEMTYPE_SHIFT);
391 }
392
393 if (op->dst.type == CCP_MEMTYPE_SYSTEM) {
394 cr[4] = ccp_addr_lo(&op->dst.u.dma);
395 cr[5] = (CCP_MEMTYPE_SYSTEM << REQ6_MEMTYPE_SHIFT)
396 | ccp_addr_hi(&op->dst.u.dma);
397 } else {
398 cr[4] = op->dst.u.ksb * CCP_KSB_BYTES;
399 cr[5] = (CCP_MEMTYPE_KSB << REQ6_MEMTYPE_SHIFT);
400 }
401
402 if (op->eom)
403 cr[0] |= REQ1_EOM;
404
405 return ccp_do_cmd(op, cr, ARRAY_SIZE(cr));
406}
407
408static int ccp_perform_ecc(struct ccp_op *op)
409{
410 u32 cr[6];
411
412 /* Fill out the register contents for REQ1 through REQ6 */
413 cr[0] = REQ1_ECC_AFFINE_CONVERT
414 | (CCP_ENGINE_ECC << REQ1_ENGINE_SHIFT)
415 | (op->u.ecc.function << REQ1_ECC_FUNCTION_SHIFT)
416 | REQ1_EOM;
417 cr[1] = op->src.u.dma.length - 1;
418 cr[2] = ccp_addr_lo(&op->src.u.dma);
419 cr[3] = (CCP_MEMTYPE_SYSTEM << REQ4_MEMTYPE_SHIFT)
420 | ccp_addr_hi(&op->src.u.dma);
421 cr[4] = ccp_addr_lo(&op->dst.u.dma);
422 cr[5] = (CCP_MEMTYPE_SYSTEM << REQ6_MEMTYPE_SHIFT)
423 | ccp_addr_hi(&op->dst.u.dma);
424
425 return ccp_do_cmd(op, cr, ARRAY_SIZE(cr));
426}
427
428static u32 ccp_alloc_ksb(struct ccp_device *ccp, unsigned int count)
429{
430 int start;
431
432 for (;;) {
433 mutex_lock(&ccp->ksb_mutex);
434
435 start = (u32)bitmap_find_next_zero_area(ccp->ksb,
436 ccp->ksb_count,
437 ccp->ksb_start,
438 count, 0);
439 if (start <= ccp->ksb_count) {
440 bitmap_set(ccp->ksb, start, count);
441
442 mutex_unlock(&ccp->ksb_mutex);
443 break;
444 }
445
446 ccp->ksb_avail = 0;
447
448 mutex_unlock(&ccp->ksb_mutex);
449
450 /* Wait for KSB entries to become available */
451 if (wait_event_interruptible(ccp->ksb_queue, ccp->ksb_avail))
452 return 0;
453 }
454
455 return KSB_START + start;
456}
457
458static void ccp_free_ksb(struct ccp_device *ccp, unsigned int start,
459 unsigned int count)
460{
461 if (!start)
462 return;
463
464 mutex_lock(&ccp->ksb_mutex);
465
466 bitmap_clear(ccp->ksb, start - KSB_START, count);
467
468 ccp->ksb_avail = 1;
469
470 mutex_unlock(&ccp->ksb_mutex);
471
472 wake_up_interruptible_all(&ccp->ksb_queue);
473}
474
475static u32 ccp_gen_jobid(struct ccp_device *ccp)
476{
477 return atomic_inc_return(&ccp->current_id) & CCP_JOBID_MASK;
478}
479
480static void ccp_sg_free(struct ccp_sg_workarea *wa)
481{
482 if (wa->dma_count)
483 dma_unmap_sg(wa->dma_dev, wa->dma_sg, wa->nents, wa->dma_dir);
484
485 wa->dma_count = 0;
486}
487
488static int ccp_init_sg_workarea(struct ccp_sg_workarea *wa, struct device *dev,
Tom Lendacky81a59f02014-01-06 13:34:17 -0600489 struct scatterlist *sg, u64 len,
Tom Lendacky63b94502013-11-12 11:46:16 -0600490 enum dma_data_direction dma_dir)
491{
492 memset(wa, 0, sizeof(*wa));
493
494 wa->sg = sg;
495 if (!sg)
496 return 0;
497
498 wa->nents = sg_nents(sg);
Tom Lendacky63b94502013-11-12 11:46:16 -0600499 wa->bytes_left = len;
500 wa->sg_used = 0;
501
502 if (len == 0)
503 return 0;
504
505 if (dma_dir == DMA_NONE)
506 return 0;
507
508 wa->dma_sg = sg;
509 wa->dma_dev = dev;
510 wa->dma_dir = dma_dir;
511 wa->dma_count = dma_map_sg(dev, sg, wa->nents, dma_dir);
512 if (!wa->dma_count)
513 return -ENOMEM;
514
Tom Lendacky63b94502013-11-12 11:46:16 -0600515 return 0;
516}
517
518static void ccp_update_sg_workarea(struct ccp_sg_workarea *wa, unsigned int len)
519{
Tom Lendacky81a59f02014-01-06 13:34:17 -0600520 unsigned int nbytes = min_t(u64, len, wa->bytes_left);
Tom Lendacky63b94502013-11-12 11:46:16 -0600521
522 if (!wa->sg)
523 return;
524
525 wa->sg_used += nbytes;
526 wa->bytes_left -= nbytes;
527 if (wa->sg_used == wa->sg->length) {
528 wa->sg = sg_next(wa->sg);
529 wa->sg_used = 0;
530 }
531}
532
533static void ccp_dm_free(struct ccp_dm_workarea *wa)
534{
535 if (wa->length <= CCP_DMAPOOL_MAX_SIZE) {
536 if (wa->address)
537 dma_pool_free(wa->dma_pool, wa->address,
538 wa->dma.address);
539 } else {
540 if (wa->dma.address)
541 dma_unmap_single(wa->dev, wa->dma.address, wa->length,
542 wa->dma.dir);
543 kfree(wa->address);
544 }
545
546 wa->address = NULL;
547 wa->dma.address = 0;
548}
549
550static int ccp_init_dm_workarea(struct ccp_dm_workarea *wa,
551 struct ccp_cmd_queue *cmd_q,
552 unsigned int len,
553 enum dma_data_direction dir)
554{
555 memset(wa, 0, sizeof(*wa));
556
557 if (!len)
558 return 0;
559
560 wa->dev = cmd_q->ccp->dev;
561 wa->length = len;
562
563 if (len <= CCP_DMAPOOL_MAX_SIZE) {
564 wa->dma_pool = cmd_q->dma_pool;
565
566 wa->address = dma_pool_alloc(wa->dma_pool, GFP_KERNEL,
567 &wa->dma.address);
568 if (!wa->address)
569 return -ENOMEM;
570
571 wa->dma.length = CCP_DMAPOOL_MAX_SIZE;
572
573 memset(wa->address, 0, CCP_DMAPOOL_MAX_SIZE);
574 } else {
575 wa->address = kzalloc(len, GFP_KERNEL);
576 if (!wa->address)
577 return -ENOMEM;
578
579 wa->dma.address = dma_map_single(wa->dev, wa->address, len,
580 dir);
581 if (!wa->dma.address)
582 return -ENOMEM;
583
584 wa->dma.length = len;
585 }
586 wa->dma.dir = dir;
587
588 return 0;
589}
590
591static void ccp_set_dm_area(struct ccp_dm_workarea *wa, unsigned int wa_offset,
592 struct scatterlist *sg, unsigned int sg_offset,
593 unsigned int len)
594{
595 WARN_ON(!wa->address);
596
597 scatterwalk_map_and_copy(wa->address + wa_offset, sg, sg_offset, len,
598 0);
599}
600
601static void ccp_get_dm_area(struct ccp_dm_workarea *wa, unsigned int wa_offset,
602 struct scatterlist *sg, unsigned int sg_offset,
603 unsigned int len)
604{
605 WARN_ON(!wa->address);
606
607 scatterwalk_map_and_copy(wa->address + wa_offset, sg, sg_offset, len,
608 1);
609}
610
611static void ccp_reverse_set_dm_area(struct ccp_dm_workarea *wa,
612 struct scatterlist *sg,
613 unsigned int len, unsigned int se_len,
614 bool sign_extend)
615{
616 unsigned int nbytes, sg_offset, dm_offset, ksb_len, i;
617 u8 buffer[CCP_REVERSE_BUF_SIZE];
618
619 BUG_ON(se_len > sizeof(buffer));
620
621 sg_offset = len;
622 dm_offset = 0;
623 nbytes = len;
624 while (nbytes) {
625 ksb_len = min_t(unsigned int, nbytes, se_len);
626 sg_offset -= ksb_len;
627
628 scatterwalk_map_and_copy(buffer, sg, sg_offset, ksb_len, 0);
629 for (i = 0; i < ksb_len; i++)
630 wa->address[dm_offset + i] = buffer[ksb_len - i - 1];
631
632 dm_offset += ksb_len;
633 nbytes -= ksb_len;
634
635 if ((ksb_len != se_len) && sign_extend) {
636 /* Must sign-extend to nearest sign-extend length */
637 if (wa->address[dm_offset - 1] & 0x80)
638 memset(wa->address + dm_offset, 0xff,
639 se_len - ksb_len);
640 }
641 }
642}
643
644static void ccp_reverse_get_dm_area(struct ccp_dm_workarea *wa,
645 struct scatterlist *sg,
646 unsigned int len)
647{
648 unsigned int nbytes, sg_offset, dm_offset, ksb_len, i;
649 u8 buffer[CCP_REVERSE_BUF_SIZE];
650
651 sg_offset = 0;
652 dm_offset = len;
653 nbytes = len;
654 while (nbytes) {
655 ksb_len = min_t(unsigned int, nbytes, sizeof(buffer));
656 dm_offset -= ksb_len;
657
658 for (i = 0; i < ksb_len; i++)
659 buffer[ksb_len - i - 1] = wa->address[dm_offset + i];
660 scatterwalk_map_and_copy(buffer, sg, sg_offset, ksb_len, 1);
661
662 sg_offset += ksb_len;
663 nbytes -= ksb_len;
664 }
665}
666
667static void ccp_free_data(struct ccp_data *data, struct ccp_cmd_queue *cmd_q)
668{
669 ccp_dm_free(&data->dm_wa);
670 ccp_sg_free(&data->sg_wa);
671}
672
673static int ccp_init_data(struct ccp_data *data, struct ccp_cmd_queue *cmd_q,
Tom Lendacky81a59f02014-01-06 13:34:17 -0600674 struct scatterlist *sg, u64 sg_len,
Tom Lendacky63b94502013-11-12 11:46:16 -0600675 unsigned int dm_len,
676 enum dma_data_direction dir)
677{
678 int ret;
679
680 memset(data, 0, sizeof(*data));
681
682 ret = ccp_init_sg_workarea(&data->sg_wa, cmd_q->ccp->dev, sg, sg_len,
683 dir);
684 if (ret)
685 goto e_err;
686
687 ret = ccp_init_dm_workarea(&data->dm_wa, cmd_q, dm_len, dir);
688 if (ret)
689 goto e_err;
690
691 return 0;
692
693e_err:
694 ccp_free_data(data, cmd_q);
695
696 return ret;
697}
698
699static unsigned int ccp_queue_buf(struct ccp_data *data, unsigned int from)
700{
701 struct ccp_sg_workarea *sg_wa = &data->sg_wa;
702 struct ccp_dm_workarea *dm_wa = &data->dm_wa;
703 unsigned int buf_count, nbytes;
704
705 /* Clear the buffer if setting it */
706 if (!from)
707 memset(dm_wa->address, 0, dm_wa->length);
708
709 if (!sg_wa->sg)
710 return 0;
711
Tom Lendacky81a59f02014-01-06 13:34:17 -0600712 /* Perform the copy operation
713 * nbytes will always be <= UINT_MAX because dm_wa->length is
714 * an unsigned int
715 */
716 nbytes = min_t(u64, sg_wa->bytes_left, dm_wa->length);
Tom Lendacky63b94502013-11-12 11:46:16 -0600717 scatterwalk_map_and_copy(dm_wa->address, sg_wa->sg, sg_wa->sg_used,
718 nbytes, from);
719
720 /* Update the structures and generate the count */
721 buf_count = 0;
722 while (sg_wa->bytes_left && (buf_count < dm_wa->length)) {
Tom Lendacky81a59f02014-01-06 13:34:17 -0600723 nbytes = min(sg_wa->sg->length - sg_wa->sg_used,
724 dm_wa->length - buf_count);
725 nbytes = min_t(u64, sg_wa->bytes_left, nbytes);
Tom Lendacky63b94502013-11-12 11:46:16 -0600726
727 buf_count += nbytes;
728 ccp_update_sg_workarea(sg_wa, nbytes);
729 }
730
731 return buf_count;
732}
733
734static unsigned int ccp_fill_queue_buf(struct ccp_data *data)
735{
736 return ccp_queue_buf(data, 0);
737}
738
739static unsigned int ccp_empty_queue_buf(struct ccp_data *data)
740{
741 return ccp_queue_buf(data, 1);
742}
743
744static void ccp_prepare_data(struct ccp_data *src, struct ccp_data *dst,
745 struct ccp_op *op, unsigned int block_size,
746 bool blocksize_op)
747{
748 unsigned int sg_src_len, sg_dst_len, op_len;
749
750 /* The CCP can only DMA from/to one address each per operation. This
751 * requires that we find the smallest DMA area between the source
Tom Lendacky81a59f02014-01-06 13:34:17 -0600752 * and destination. The resulting len values will always be <= UINT_MAX
753 * because the dma length is an unsigned int.
Tom Lendacky63b94502013-11-12 11:46:16 -0600754 */
Tom Lendacky81a59f02014-01-06 13:34:17 -0600755 sg_src_len = sg_dma_len(src->sg_wa.sg) - src->sg_wa.sg_used;
756 sg_src_len = min_t(u64, src->sg_wa.bytes_left, sg_src_len);
Tom Lendacky63b94502013-11-12 11:46:16 -0600757
758 if (dst) {
Tom Lendacky81a59f02014-01-06 13:34:17 -0600759 sg_dst_len = sg_dma_len(dst->sg_wa.sg) - dst->sg_wa.sg_used;
760 sg_dst_len = min_t(u64, src->sg_wa.bytes_left, sg_dst_len);
Tom Lendacky63b94502013-11-12 11:46:16 -0600761 op_len = min(sg_src_len, sg_dst_len);
Tom Lendacky8db88462015-02-03 13:07:05 -0600762 } else {
Tom Lendacky63b94502013-11-12 11:46:16 -0600763 op_len = sg_src_len;
Tom Lendacky8db88462015-02-03 13:07:05 -0600764 }
Tom Lendacky63b94502013-11-12 11:46:16 -0600765
766 /* The data operation length will be at least block_size in length
767 * or the smaller of available sg room remaining for the source or
768 * the destination
769 */
770 op_len = max(op_len, block_size);
771
772 /* Unless we have to buffer data, there's no reason to wait */
773 op->soc = 0;
774
775 if (sg_src_len < block_size) {
776 /* Not enough data in the sg element, so it
777 * needs to be buffered into a blocksize chunk
778 */
779 int cp_len = ccp_fill_queue_buf(src);
780
781 op->soc = 1;
782 op->src.u.dma.address = src->dm_wa.dma.address;
783 op->src.u.dma.offset = 0;
784 op->src.u.dma.length = (blocksize_op) ? block_size : cp_len;
785 } else {
786 /* Enough data in the sg element, but we need to
787 * adjust for any previously copied data
788 */
789 op->src.u.dma.address = sg_dma_address(src->sg_wa.sg);
790 op->src.u.dma.offset = src->sg_wa.sg_used;
791 op->src.u.dma.length = op_len & ~(block_size - 1);
792
793 ccp_update_sg_workarea(&src->sg_wa, op->src.u.dma.length);
794 }
795
796 if (dst) {
797 if (sg_dst_len < block_size) {
798 /* Not enough room in the sg element or we're on the
799 * last piece of data (when using padding), so the
800 * output needs to be buffered into a blocksize chunk
801 */
802 op->soc = 1;
803 op->dst.u.dma.address = dst->dm_wa.dma.address;
804 op->dst.u.dma.offset = 0;
805 op->dst.u.dma.length = op->src.u.dma.length;
806 } else {
807 /* Enough room in the sg element, but we need to
808 * adjust for any previously used area
809 */
810 op->dst.u.dma.address = sg_dma_address(dst->sg_wa.sg);
811 op->dst.u.dma.offset = dst->sg_wa.sg_used;
812 op->dst.u.dma.length = op->src.u.dma.length;
813 }
814 }
815}
816
817static void ccp_process_data(struct ccp_data *src, struct ccp_data *dst,
818 struct ccp_op *op)
819{
820 op->init = 0;
821
822 if (dst) {
823 if (op->dst.u.dma.address == dst->dm_wa.dma.address)
824 ccp_empty_queue_buf(dst);
825 else
826 ccp_update_sg_workarea(&dst->sg_wa,
827 op->dst.u.dma.length);
828 }
829}
830
831static int ccp_copy_to_from_ksb(struct ccp_cmd_queue *cmd_q,
832 struct ccp_dm_workarea *wa, u32 jobid, u32 ksb,
833 u32 byte_swap, bool from)
834{
835 struct ccp_op op;
836
837 memset(&op, 0, sizeof(op));
838
839 op.cmd_q = cmd_q;
840 op.jobid = jobid;
841 op.eom = 1;
842
843 if (from) {
844 op.soc = 1;
845 op.src.type = CCP_MEMTYPE_KSB;
846 op.src.u.ksb = ksb;
847 op.dst.type = CCP_MEMTYPE_SYSTEM;
848 op.dst.u.dma.address = wa->dma.address;
849 op.dst.u.dma.length = wa->length;
850 } else {
851 op.src.type = CCP_MEMTYPE_SYSTEM;
852 op.src.u.dma.address = wa->dma.address;
853 op.src.u.dma.length = wa->length;
854 op.dst.type = CCP_MEMTYPE_KSB;
855 op.dst.u.ksb = ksb;
856 }
857
858 op.u.passthru.byte_swap = byte_swap;
859
860 return ccp_perform_passthru(&op);
861}
862
863static int ccp_copy_to_ksb(struct ccp_cmd_queue *cmd_q,
864 struct ccp_dm_workarea *wa, u32 jobid, u32 ksb,
865 u32 byte_swap)
866{
867 return ccp_copy_to_from_ksb(cmd_q, wa, jobid, ksb, byte_swap, false);
868}
869
870static int ccp_copy_from_ksb(struct ccp_cmd_queue *cmd_q,
871 struct ccp_dm_workarea *wa, u32 jobid, u32 ksb,
872 u32 byte_swap)
873{
874 return ccp_copy_to_from_ksb(cmd_q, wa, jobid, ksb, byte_swap, true);
875}
876
877static int ccp_run_aes_cmac_cmd(struct ccp_cmd_queue *cmd_q,
878 struct ccp_cmd *cmd)
879{
880 struct ccp_aes_engine *aes = &cmd->u.aes;
881 struct ccp_dm_workarea key, ctx;
882 struct ccp_data src;
883 struct ccp_op op;
884 unsigned int dm_offset;
885 int ret;
886
887 if (!((aes->key_len == AES_KEYSIZE_128) ||
888 (aes->key_len == AES_KEYSIZE_192) ||
889 (aes->key_len == AES_KEYSIZE_256)))
890 return -EINVAL;
891
892 if (aes->src_len & (AES_BLOCK_SIZE - 1))
893 return -EINVAL;
894
895 if (aes->iv_len != AES_BLOCK_SIZE)
896 return -EINVAL;
897
898 if (!aes->key || !aes->iv || !aes->src)
899 return -EINVAL;
900
901 if (aes->cmac_final) {
902 if (aes->cmac_key_len != AES_BLOCK_SIZE)
903 return -EINVAL;
904
905 if (!aes->cmac_key)
906 return -EINVAL;
907 }
908
909 BUILD_BUG_ON(CCP_AES_KEY_KSB_COUNT != 1);
910 BUILD_BUG_ON(CCP_AES_CTX_KSB_COUNT != 1);
911
912 ret = -EIO;
913 memset(&op, 0, sizeof(op));
914 op.cmd_q = cmd_q;
915 op.jobid = ccp_gen_jobid(cmd_q->ccp);
916 op.ksb_key = cmd_q->ksb_key;
917 op.ksb_ctx = cmd_q->ksb_ctx;
918 op.init = 1;
919 op.u.aes.type = aes->type;
920 op.u.aes.mode = aes->mode;
921 op.u.aes.action = aes->action;
922
923 /* All supported key sizes fit in a single (32-byte) KSB entry
924 * and must be in little endian format. Use the 256-bit byte
925 * swap passthru option to convert from big endian to little
926 * endian.
927 */
928 ret = ccp_init_dm_workarea(&key, cmd_q,
929 CCP_AES_KEY_KSB_COUNT * CCP_KSB_BYTES,
930 DMA_TO_DEVICE);
931 if (ret)
932 return ret;
933
934 dm_offset = CCP_KSB_BYTES - aes->key_len;
935 ccp_set_dm_area(&key, dm_offset, aes->key, 0, aes->key_len);
936 ret = ccp_copy_to_ksb(cmd_q, &key, op.jobid, op.ksb_key,
937 CCP_PASSTHRU_BYTESWAP_256BIT);
938 if (ret) {
939 cmd->engine_error = cmd_q->cmd_error;
940 goto e_key;
941 }
942
943 /* The AES context fits in a single (32-byte) KSB entry and
944 * must be in little endian format. Use the 256-bit byte swap
945 * passthru option to convert from big endian to little endian.
946 */
947 ret = ccp_init_dm_workarea(&ctx, cmd_q,
948 CCP_AES_CTX_KSB_COUNT * CCP_KSB_BYTES,
949 DMA_BIDIRECTIONAL);
950 if (ret)
951 goto e_key;
952
953 dm_offset = CCP_KSB_BYTES - AES_BLOCK_SIZE;
954 ccp_set_dm_area(&ctx, dm_offset, aes->iv, 0, aes->iv_len);
955 ret = ccp_copy_to_ksb(cmd_q, &ctx, op.jobid, op.ksb_ctx,
956 CCP_PASSTHRU_BYTESWAP_256BIT);
957 if (ret) {
958 cmd->engine_error = cmd_q->cmd_error;
959 goto e_ctx;
960 }
961
962 /* Send data to the CCP AES engine */
963 ret = ccp_init_data(&src, cmd_q, aes->src, aes->src_len,
964 AES_BLOCK_SIZE, DMA_TO_DEVICE);
965 if (ret)
966 goto e_ctx;
967
968 while (src.sg_wa.bytes_left) {
969 ccp_prepare_data(&src, NULL, &op, AES_BLOCK_SIZE, true);
970 if (aes->cmac_final && !src.sg_wa.bytes_left) {
971 op.eom = 1;
972
973 /* Push the K1/K2 key to the CCP now */
974 ret = ccp_copy_from_ksb(cmd_q, &ctx, op.jobid,
975 op.ksb_ctx,
976 CCP_PASSTHRU_BYTESWAP_256BIT);
977 if (ret) {
978 cmd->engine_error = cmd_q->cmd_error;
979 goto e_src;
980 }
981
982 ccp_set_dm_area(&ctx, 0, aes->cmac_key, 0,
983 aes->cmac_key_len);
984 ret = ccp_copy_to_ksb(cmd_q, &ctx, op.jobid, op.ksb_ctx,
985 CCP_PASSTHRU_BYTESWAP_256BIT);
986 if (ret) {
987 cmd->engine_error = cmd_q->cmd_error;
988 goto e_src;
989 }
990 }
991
992 ret = ccp_perform_aes(&op);
993 if (ret) {
994 cmd->engine_error = cmd_q->cmd_error;
995 goto e_src;
996 }
997
998 ccp_process_data(&src, NULL, &op);
999 }
1000
1001 /* Retrieve the AES context - convert from LE to BE using
1002 * 32-byte (256-bit) byteswapping
1003 */
1004 ret = ccp_copy_from_ksb(cmd_q, &ctx, op.jobid, op.ksb_ctx,
1005 CCP_PASSTHRU_BYTESWAP_256BIT);
1006 if (ret) {
1007 cmd->engine_error = cmd_q->cmd_error;
1008 goto e_src;
1009 }
1010
1011 /* ...but we only need AES_BLOCK_SIZE bytes */
1012 dm_offset = CCP_KSB_BYTES - AES_BLOCK_SIZE;
1013 ccp_get_dm_area(&ctx, dm_offset, aes->iv, 0, aes->iv_len);
1014
1015e_src:
1016 ccp_free_data(&src, cmd_q);
1017
1018e_ctx:
1019 ccp_dm_free(&ctx);
1020
1021e_key:
1022 ccp_dm_free(&key);
1023
1024 return ret;
1025}
1026
1027static int ccp_run_aes_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd)
1028{
1029 struct ccp_aes_engine *aes = &cmd->u.aes;
1030 struct ccp_dm_workarea key, ctx;
1031 struct ccp_data src, dst;
1032 struct ccp_op op;
1033 unsigned int dm_offset;
1034 bool in_place = false;
1035 int ret;
1036
1037 if (aes->mode == CCP_AES_MODE_CMAC)
1038 return ccp_run_aes_cmac_cmd(cmd_q, cmd);
1039
1040 if (!((aes->key_len == AES_KEYSIZE_128) ||
1041 (aes->key_len == AES_KEYSIZE_192) ||
1042 (aes->key_len == AES_KEYSIZE_256)))
1043 return -EINVAL;
1044
1045 if (((aes->mode == CCP_AES_MODE_ECB) ||
1046 (aes->mode == CCP_AES_MODE_CBC) ||
1047 (aes->mode == CCP_AES_MODE_CFB)) &&
1048 (aes->src_len & (AES_BLOCK_SIZE - 1)))
1049 return -EINVAL;
1050
1051 if (!aes->key || !aes->src || !aes->dst)
1052 return -EINVAL;
1053
1054 if (aes->mode != CCP_AES_MODE_ECB) {
1055 if (aes->iv_len != AES_BLOCK_SIZE)
1056 return -EINVAL;
1057
1058 if (!aes->iv)
1059 return -EINVAL;
1060 }
1061
1062 BUILD_BUG_ON(CCP_AES_KEY_KSB_COUNT != 1);
1063 BUILD_BUG_ON(CCP_AES_CTX_KSB_COUNT != 1);
1064
1065 ret = -EIO;
1066 memset(&op, 0, sizeof(op));
1067 op.cmd_q = cmd_q;
1068 op.jobid = ccp_gen_jobid(cmd_q->ccp);
1069 op.ksb_key = cmd_q->ksb_key;
1070 op.ksb_ctx = cmd_q->ksb_ctx;
1071 op.init = (aes->mode == CCP_AES_MODE_ECB) ? 0 : 1;
1072 op.u.aes.type = aes->type;
1073 op.u.aes.mode = aes->mode;
1074 op.u.aes.action = aes->action;
1075
1076 /* All supported key sizes fit in a single (32-byte) KSB entry
1077 * and must be in little endian format. Use the 256-bit byte
1078 * swap passthru option to convert from big endian to little
1079 * endian.
1080 */
1081 ret = ccp_init_dm_workarea(&key, cmd_q,
1082 CCP_AES_KEY_KSB_COUNT * CCP_KSB_BYTES,
1083 DMA_TO_DEVICE);
1084 if (ret)
1085 return ret;
1086
1087 dm_offset = CCP_KSB_BYTES - aes->key_len;
1088 ccp_set_dm_area(&key, dm_offset, aes->key, 0, aes->key_len);
1089 ret = ccp_copy_to_ksb(cmd_q, &key, op.jobid, op.ksb_key,
1090 CCP_PASSTHRU_BYTESWAP_256BIT);
1091 if (ret) {
1092 cmd->engine_error = cmd_q->cmd_error;
1093 goto e_key;
1094 }
1095
1096 /* The AES context fits in a single (32-byte) KSB entry and
1097 * must be in little endian format. Use the 256-bit byte swap
1098 * passthru option to convert from big endian to little endian.
1099 */
1100 ret = ccp_init_dm_workarea(&ctx, cmd_q,
1101 CCP_AES_CTX_KSB_COUNT * CCP_KSB_BYTES,
1102 DMA_BIDIRECTIONAL);
1103 if (ret)
1104 goto e_key;
1105
1106 if (aes->mode != CCP_AES_MODE_ECB) {
1107 /* Load the AES context - conver to LE */
1108 dm_offset = CCP_KSB_BYTES - AES_BLOCK_SIZE;
1109 ccp_set_dm_area(&ctx, dm_offset, aes->iv, 0, aes->iv_len);
1110 ret = ccp_copy_to_ksb(cmd_q, &ctx, op.jobid, op.ksb_ctx,
1111 CCP_PASSTHRU_BYTESWAP_256BIT);
1112 if (ret) {
1113 cmd->engine_error = cmd_q->cmd_error;
1114 goto e_ctx;
1115 }
1116 }
1117
1118 /* Prepare the input and output data workareas. For in-place
1119 * operations we need to set the dma direction to BIDIRECTIONAL
1120 * and copy the src workarea to the dst workarea.
1121 */
1122 if (sg_virt(aes->src) == sg_virt(aes->dst))
1123 in_place = true;
1124
1125 ret = ccp_init_data(&src, cmd_q, aes->src, aes->src_len,
1126 AES_BLOCK_SIZE,
1127 in_place ? DMA_BIDIRECTIONAL : DMA_TO_DEVICE);
1128 if (ret)
1129 goto e_ctx;
1130
Tom Lendacky8db88462015-02-03 13:07:05 -06001131 if (in_place) {
Tom Lendacky63b94502013-11-12 11:46:16 -06001132 dst = src;
Tom Lendacky8db88462015-02-03 13:07:05 -06001133 } else {
Tom Lendacky63b94502013-11-12 11:46:16 -06001134 ret = ccp_init_data(&dst, cmd_q, aes->dst, aes->src_len,
1135 AES_BLOCK_SIZE, DMA_FROM_DEVICE);
1136 if (ret)
1137 goto e_src;
1138 }
1139
1140 /* Send data to the CCP AES engine */
1141 while (src.sg_wa.bytes_left) {
1142 ccp_prepare_data(&src, &dst, &op, AES_BLOCK_SIZE, true);
1143 if (!src.sg_wa.bytes_left) {
1144 op.eom = 1;
1145
1146 /* Since we don't retrieve the AES context in ECB
1147 * mode we have to wait for the operation to complete
1148 * on the last piece of data
1149 */
1150 if (aes->mode == CCP_AES_MODE_ECB)
1151 op.soc = 1;
1152 }
1153
1154 ret = ccp_perform_aes(&op);
1155 if (ret) {
1156 cmd->engine_error = cmd_q->cmd_error;
1157 goto e_dst;
1158 }
1159
1160 ccp_process_data(&src, &dst, &op);
1161 }
1162
1163 if (aes->mode != CCP_AES_MODE_ECB) {
1164 /* Retrieve the AES context - convert from LE to BE using
1165 * 32-byte (256-bit) byteswapping
1166 */
1167 ret = ccp_copy_from_ksb(cmd_q, &ctx, op.jobid, op.ksb_ctx,
1168 CCP_PASSTHRU_BYTESWAP_256BIT);
1169 if (ret) {
1170 cmd->engine_error = cmd_q->cmd_error;
1171 goto e_dst;
1172 }
1173
1174 /* ...but we only need AES_BLOCK_SIZE bytes */
1175 dm_offset = CCP_KSB_BYTES - AES_BLOCK_SIZE;
1176 ccp_get_dm_area(&ctx, dm_offset, aes->iv, 0, aes->iv_len);
1177 }
1178
1179e_dst:
1180 if (!in_place)
1181 ccp_free_data(&dst, cmd_q);
1182
1183e_src:
1184 ccp_free_data(&src, cmd_q);
1185
1186e_ctx:
1187 ccp_dm_free(&ctx);
1188
1189e_key:
1190 ccp_dm_free(&key);
1191
1192 return ret;
1193}
1194
1195static int ccp_run_xts_aes_cmd(struct ccp_cmd_queue *cmd_q,
1196 struct ccp_cmd *cmd)
1197{
1198 struct ccp_xts_aes_engine *xts = &cmd->u.xts;
1199 struct ccp_dm_workarea key, ctx;
1200 struct ccp_data src, dst;
1201 struct ccp_op op;
1202 unsigned int unit_size, dm_offset;
1203 bool in_place = false;
1204 int ret;
1205
1206 switch (xts->unit_size) {
1207 case CCP_XTS_AES_UNIT_SIZE_16:
1208 unit_size = 16;
1209 break;
1210 case CCP_XTS_AES_UNIT_SIZE_512:
1211 unit_size = 512;
1212 break;
1213 case CCP_XTS_AES_UNIT_SIZE_1024:
1214 unit_size = 1024;
1215 break;
1216 case CCP_XTS_AES_UNIT_SIZE_2048:
1217 unit_size = 2048;
1218 break;
1219 case CCP_XTS_AES_UNIT_SIZE_4096:
1220 unit_size = 4096;
1221 break;
1222
1223 default:
1224 return -EINVAL;
1225 }
1226
1227 if (xts->key_len != AES_KEYSIZE_128)
1228 return -EINVAL;
1229
1230 if (!xts->final && (xts->src_len & (AES_BLOCK_SIZE - 1)))
1231 return -EINVAL;
1232
1233 if (xts->iv_len != AES_BLOCK_SIZE)
1234 return -EINVAL;
1235
1236 if (!xts->key || !xts->iv || !xts->src || !xts->dst)
1237 return -EINVAL;
1238
1239 BUILD_BUG_ON(CCP_XTS_AES_KEY_KSB_COUNT != 1);
1240 BUILD_BUG_ON(CCP_XTS_AES_CTX_KSB_COUNT != 1);
1241
1242 ret = -EIO;
1243 memset(&op, 0, sizeof(op));
1244 op.cmd_q = cmd_q;
1245 op.jobid = ccp_gen_jobid(cmd_q->ccp);
1246 op.ksb_key = cmd_q->ksb_key;
1247 op.ksb_ctx = cmd_q->ksb_ctx;
1248 op.init = 1;
1249 op.u.xts.action = xts->action;
1250 op.u.xts.unit_size = xts->unit_size;
1251
1252 /* All supported key sizes fit in a single (32-byte) KSB entry
1253 * and must be in little endian format. Use the 256-bit byte
1254 * swap passthru option to convert from big endian to little
1255 * endian.
1256 */
1257 ret = ccp_init_dm_workarea(&key, cmd_q,
1258 CCP_XTS_AES_KEY_KSB_COUNT * CCP_KSB_BYTES,
1259 DMA_TO_DEVICE);
1260 if (ret)
1261 return ret;
1262
1263 dm_offset = CCP_KSB_BYTES - AES_KEYSIZE_128;
1264 ccp_set_dm_area(&key, dm_offset, xts->key, 0, xts->key_len);
1265 ccp_set_dm_area(&key, 0, xts->key, dm_offset, xts->key_len);
1266 ret = ccp_copy_to_ksb(cmd_q, &key, op.jobid, op.ksb_key,
1267 CCP_PASSTHRU_BYTESWAP_256BIT);
1268 if (ret) {
1269 cmd->engine_error = cmd_q->cmd_error;
1270 goto e_key;
1271 }
1272
1273 /* The AES context fits in a single (32-byte) KSB entry and
1274 * for XTS is already in little endian format so no byte swapping
1275 * is needed.
1276 */
1277 ret = ccp_init_dm_workarea(&ctx, cmd_q,
1278 CCP_XTS_AES_CTX_KSB_COUNT * CCP_KSB_BYTES,
1279 DMA_BIDIRECTIONAL);
1280 if (ret)
1281 goto e_key;
1282
1283 ccp_set_dm_area(&ctx, 0, xts->iv, 0, xts->iv_len);
1284 ret = ccp_copy_to_ksb(cmd_q, &ctx, op.jobid, op.ksb_ctx,
1285 CCP_PASSTHRU_BYTESWAP_NOOP);
1286 if (ret) {
1287 cmd->engine_error = cmd_q->cmd_error;
1288 goto e_ctx;
1289 }
1290
1291 /* Prepare the input and output data workareas. For in-place
1292 * operations we need to set the dma direction to BIDIRECTIONAL
1293 * and copy the src workarea to the dst workarea.
1294 */
1295 if (sg_virt(xts->src) == sg_virt(xts->dst))
1296 in_place = true;
1297
1298 ret = ccp_init_data(&src, cmd_q, xts->src, xts->src_len,
1299 unit_size,
1300 in_place ? DMA_BIDIRECTIONAL : DMA_TO_DEVICE);
1301 if (ret)
1302 goto e_ctx;
1303
Tom Lendacky8db88462015-02-03 13:07:05 -06001304 if (in_place) {
Tom Lendacky63b94502013-11-12 11:46:16 -06001305 dst = src;
Tom Lendacky8db88462015-02-03 13:07:05 -06001306 } else {
Tom Lendacky63b94502013-11-12 11:46:16 -06001307 ret = ccp_init_data(&dst, cmd_q, xts->dst, xts->src_len,
1308 unit_size, DMA_FROM_DEVICE);
1309 if (ret)
1310 goto e_src;
1311 }
1312
1313 /* Send data to the CCP AES engine */
1314 while (src.sg_wa.bytes_left) {
1315 ccp_prepare_data(&src, &dst, &op, unit_size, true);
1316 if (!src.sg_wa.bytes_left)
1317 op.eom = 1;
1318
1319 ret = ccp_perform_xts_aes(&op);
1320 if (ret) {
1321 cmd->engine_error = cmd_q->cmd_error;
1322 goto e_dst;
1323 }
1324
1325 ccp_process_data(&src, &dst, &op);
1326 }
1327
1328 /* Retrieve the AES context - convert from LE to BE using
1329 * 32-byte (256-bit) byteswapping
1330 */
1331 ret = ccp_copy_from_ksb(cmd_q, &ctx, op.jobid, op.ksb_ctx,
1332 CCP_PASSTHRU_BYTESWAP_256BIT);
1333 if (ret) {
1334 cmd->engine_error = cmd_q->cmd_error;
1335 goto e_dst;
1336 }
1337
1338 /* ...but we only need AES_BLOCK_SIZE bytes */
1339 dm_offset = CCP_KSB_BYTES - AES_BLOCK_SIZE;
1340 ccp_get_dm_area(&ctx, dm_offset, xts->iv, 0, xts->iv_len);
1341
1342e_dst:
1343 if (!in_place)
1344 ccp_free_data(&dst, cmd_q);
1345
1346e_src:
1347 ccp_free_data(&src, cmd_q);
1348
1349e_ctx:
1350 ccp_dm_free(&ctx);
1351
1352e_key:
1353 ccp_dm_free(&key);
1354
1355 return ret;
1356}
1357
1358static int ccp_run_sha_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd)
1359{
1360 struct ccp_sha_engine *sha = &cmd->u.sha;
1361 struct ccp_dm_workarea ctx;
1362 struct ccp_data src;
1363 struct ccp_op op;
1364 int ret;
1365
1366 if (sha->ctx_len != CCP_SHA_CTXSIZE)
1367 return -EINVAL;
1368
1369 if (!sha->ctx)
1370 return -EINVAL;
1371
1372 if (!sha->final && (sha->src_len & (CCP_SHA_BLOCKSIZE - 1)))
1373 return -EINVAL;
1374
1375 if (!sha->src_len) {
1376 const u8 *sha_zero;
1377
1378 /* Not final, just return */
1379 if (!sha->final)
1380 return 0;
1381
1382 /* CCP can't do a zero length sha operation so the caller
1383 * must buffer the data.
1384 */
1385 if (sha->msg_bits)
1386 return -EINVAL;
1387
1388 /* A sha operation for a message with a total length of zero,
1389 * return known result.
1390 */
1391 switch (sha->type) {
1392 case CCP_SHA_TYPE_1:
1393 sha_zero = ccp_sha1_zero;
1394 break;
1395 case CCP_SHA_TYPE_224:
1396 sha_zero = ccp_sha224_zero;
1397 break;
1398 case CCP_SHA_TYPE_256:
1399 sha_zero = ccp_sha256_zero;
1400 break;
1401 default:
1402 return -EINVAL;
1403 }
1404
1405 scatterwalk_map_and_copy((void *)sha_zero, sha->ctx, 0,
1406 sha->ctx_len, 1);
1407
1408 return 0;
1409 }
1410
1411 if (!sha->src)
1412 return -EINVAL;
1413
1414 BUILD_BUG_ON(CCP_SHA_KSB_COUNT != 1);
1415
1416 memset(&op, 0, sizeof(op));
1417 op.cmd_q = cmd_q;
1418 op.jobid = ccp_gen_jobid(cmd_q->ccp);
1419 op.ksb_ctx = cmd_q->ksb_ctx;
1420 op.u.sha.type = sha->type;
1421 op.u.sha.msg_bits = sha->msg_bits;
1422
1423 /* The SHA context fits in a single (32-byte) KSB entry and
1424 * must be in little endian format. Use the 256-bit byte swap
1425 * passthru option to convert from big endian to little endian.
1426 */
1427 ret = ccp_init_dm_workarea(&ctx, cmd_q,
1428 CCP_SHA_KSB_COUNT * CCP_KSB_BYTES,
1429 DMA_BIDIRECTIONAL);
1430 if (ret)
1431 return ret;
1432
Tom Lendackyc11baa02014-01-24 16:18:02 -06001433 if (sha->first) {
1434 const __be32 *init;
1435
1436 switch (sha->type) {
1437 case CCP_SHA_TYPE_1:
1438 init = ccp_sha1_init;
1439 break;
1440 case CCP_SHA_TYPE_224:
1441 init = ccp_sha224_init;
1442 break;
1443 case CCP_SHA_TYPE_256:
1444 init = ccp_sha256_init;
1445 break;
1446 default:
1447 ret = -EINVAL;
1448 goto e_ctx;
1449 }
1450 memcpy(ctx.address, init, CCP_SHA_CTXSIZE);
Tom Lendacky8db88462015-02-03 13:07:05 -06001451 } else {
Tom Lendackyc11baa02014-01-24 16:18:02 -06001452 ccp_set_dm_area(&ctx, 0, sha->ctx, 0, sha->ctx_len);
Tom Lendacky8db88462015-02-03 13:07:05 -06001453 }
Tom Lendackyc11baa02014-01-24 16:18:02 -06001454
Tom Lendacky63b94502013-11-12 11:46:16 -06001455 ret = ccp_copy_to_ksb(cmd_q, &ctx, op.jobid, op.ksb_ctx,
1456 CCP_PASSTHRU_BYTESWAP_256BIT);
1457 if (ret) {
1458 cmd->engine_error = cmd_q->cmd_error;
1459 goto e_ctx;
1460 }
1461
1462 /* Send data to the CCP SHA engine */
1463 ret = ccp_init_data(&src, cmd_q, sha->src, sha->src_len,
1464 CCP_SHA_BLOCKSIZE, DMA_TO_DEVICE);
1465 if (ret)
1466 goto e_ctx;
1467
1468 while (src.sg_wa.bytes_left) {
1469 ccp_prepare_data(&src, NULL, &op, CCP_SHA_BLOCKSIZE, false);
1470 if (sha->final && !src.sg_wa.bytes_left)
1471 op.eom = 1;
1472
1473 ret = ccp_perform_sha(&op);
1474 if (ret) {
1475 cmd->engine_error = cmd_q->cmd_error;
1476 goto e_data;
1477 }
1478
1479 ccp_process_data(&src, NULL, &op);
1480 }
1481
1482 /* Retrieve the SHA context - convert from LE to BE using
1483 * 32-byte (256-bit) byteswapping to BE
1484 */
1485 ret = ccp_copy_from_ksb(cmd_q, &ctx, op.jobid, op.ksb_ctx,
1486 CCP_PASSTHRU_BYTESWAP_256BIT);
1487 if (ret) {
1488 cmd->engine_error = cmd_q->cmd_error;
1489 goto e_data;
1490 }
1491
1492 ccp_get_dm_area(&ctx, 0, sha->ctx, 0, sha->ctx_len);
1493
Tom Lendackyc11baa02014-01-24 16:18:02 -06001494 if (sha->final && sha->opad) {
1495 /* HMAC operation, recursively perform final SHA */
1496 struct ccp_cmd hmac_cmd;
1497 struct scatterlist sg;
1498 u64 block_size, digest_size;
1499 u8 *hmac_buf;
1500
1501 switch (sha->type) {
1502 case CCP_SHA_TYPE_1:
1503 block_size = SHA1_BLOCK_SIZE;
1504 digest_size = SHA1_DIGEST_SIZE;
1505 break;
1506 case CCP_SHA_TYPE_224:
1507 block_size = SHA224_BLOCK_SIZE;
1508 digest_size = SHA224_DIGEST_SIZE;
1509 break;
1510 case CCP_SHA_TYPE_256:
1511 block_size = SHA256_BLOCK_SIZE;
1512 digest_size = SHA256_DIGEST_SIZE;
1513 break;
1514 default:
1515 ret = -EINVAL;
1516 goto e_data;
1517 }
1518
1519 if (sha->opad_len != block_size) {
1520 ret = -EINVAL;
1521 goto e_data;
1522 }
1523
1524 hmac_buf = kmalloc(block_size + digest_size, GFP_KERNEL);
1525 if (!hmac_buf) {
1526 ret = -ENOMEM;
1527 goto e_data;
1528 }
1529 sg_init_one(&sg, hmac_buf, block_size + digest_size);
1530
1531 scatterwalk_map_and_copy(hmac_buf, sha->opad, 0, block_size, 0);
1532 memcpy(hmac_buf + block_size, ctx.address, digest_size);
1533
1534 memset(&hmac_cmd, 0, sizeof(hmac_cmd));
1535 hmac_cmd.engine = CCP_ENGINE_SHA;
1536 hmac_cmd.u.sha.type = sha->type;
1537 hmac_cmd.u.sha.ctx = sha->ctx;
1538 hmac_cmd.u.sha.ctx_len = sha->ctx_len;
1539 hmac_cmd.u.sha.src = &sg;
1540 hmac_cmd.u.sha.src_len = block_size + digest_size;
1541 hmac_cmd.u.sha.opad = NULL;
1542 hmac_cmd.u.sha.opad_len = 0;
1543 hmac_cmd.u.sha.first = 1;
1544 hmac_cmd.u.sha.final = 1;
1545 hmac_cmd.u.sha.msg_bits = (block_size + digest_size) << 3;
1546
1547 ret = ccp_run_sha_cmd(cmd_q, &hmac_cmd);
1548 if (ret)
1549 cmd->engine_error = hmac_cmd.engine_error;
1550
1551 kfree(hmac_buf);
1552 }
1553
Tom Lendacky63b94502013-11-12 11:46:16 -06001554e_data:
1555 ccp_free_data(&src, cmd_q);
1556
1557e_ctx:
1558 ccp_dm_free(&ctx);
1559
1560 return ret;
1561}
1562
1563static int ccp_run_rsa_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd)
1564{
1565 struct ccp_rsa_engine *rsa = &cmd->u.rsa;
1566 struct ccp_dm_workarea exp, src;
1567 struct ccp_data dst;
1568 struct ccp_op op;
1569 unsigned int ksb_count, i_len, o_len;
1570 int ret;
1571
1572 if (rsa->key_size > CCP_RSA_MAX_WIDTH)
1573 return -EINVAL;
1574
1575 if (!rsa->exp || !rsa->mod || !rsa->src || !rsa->dst)
1576 return -EINVAL;
1577
1578 /* The RSA modulus must precede the message being acted upon, so
1579 * it must be copied to a DMA area where the message and the
1580 * modulus can be concatenated. Therefore the input buffer
1581 * length required is twice the output buffer length (which
1582 * must be a multiple of 256-bits).
1583 */
1584 o_len = ((rsa->key_size + 255) / 256) * 32;
1585 i_len = o_len * 2;
1586
1587 ksb_count = o_len / CCP_KSB_BYTES;
1588
1589 memset(&op, 0, sizeof(op));
1590 op.cmd_q = cmd_q;
1591 op.jobid = ccp_gen_jobid(cmd_q->ccp);
1592 op.ksb_key = ccp_alloc_ksb(cmd_q->ccp, ksb_count);
1593 if (!op.ksb_key)
1594 return -EIO;
1595
1596 /* The RSA exponent may span multiple (32-byte) KSB entries and must
1597 * be in little endian format. Reverse copy each 32-byte chunk
1598 * of the exponent (En chunk to E0 chunk, E(n-1) chunk to E1 chunk)
1599 * and each byte within that chunk and do not perform any byte swap
1600 * operations on the passthru operation.
1601 */
1602 ret = ccp_init_dm_workarea(&exp, cmd_q, o_len, DMA_TO_DEVICE);
1603 if (ret)
1604 goto e_ksb;
1605
1606 ccp_reverse_set_dm_area(&exp, rsa->exp, rsa->exp_len, CCP_KSB_BYTES,
Tom Lendacky63917232014-07-30 15:41:32 -05001607 false);
Tom Lendacky63b94502013-11-12 11:46:16 -06001608 ret = ccp_copy_to_ksb(cmd_q, &exp, op.jobid, op.ksb_key,
1609 CCP_PASSTHRU_BYTESWAP_NOOP);
1610 if (ret) {
1611 cmd->engine_error = cmd_q->cmd_error;
1612 goto e_exp;
1613 }
1614
1615 /* Concatenate the modulus and the message. Both the modulus and
1616 * the operands must be in little endian format. Since the input
1617 * is in big endian format it must be converted.
1618 */
1619 ret = ccp_init_dm_workarea(&src, cmd_q, i_len, DMA_TO_DEVICE);
1620 if (ret)
1621 goto e_exp;
1622
1623 ccp_reverse_set_dm_area(&src, rsa->mod, rsa->mod_len, CCP_KSB_BYTES,
Tom Lendacky63917232014-07-30 15:41:32 -05001624 false);
Tom Lendacky63b94502013-11-12 11:46:16 -06001625 src.address += o_len; /* Adjust the address for the copy operation */
1626 ccp_reverse_set_dm_area(&src, rsa->src, rsa->src_len, CCP_KSB_BYTES,
Tom Lendacky63917232014-07-30 15:41:32 -05001627 false);
Tom Lendacky63b94502013-11-12 11:46:16 -06001628 src.address -= o_len; /* Reset the address to original value */
1629
1630 /* Prepare the output area for the operation */
1631 ret = ccp_init_data(&dst, cmd_q, rsa->dst, rsa->mod_len,
1632 o_len, DMA_FROM_DEVICE);
1633 if (ret)
1634 goto e_src;
1635
1636 op.soc = 1;
1637 op.src.u.dma.address = src.dma.address;
1638 op.src.u.dma.offset = 0;
1639 op.src.u.dma.length = i_len;
1640 op.dst.u.dma.address = dst.dm_wa.dma.address;
1641 op.dst.u.dma.offset = 0;
1642 op.dst.u.dma.length = o_len;
1643
1644 op.u.rsa.mod_size = rsa->key_size;
1645 op.u.rsa.input_len = i_len;
1646
1647 ret = ccp_perform_rsa(&op);
1648 if (ret) {
1649 cmd->engine_error = cmd_q->cmd_error;
1650 goto e_dst;
1651 }
1652
1653 ccp_reverse_get_dm_area(&dst.dm_wa, rsa->dst, rsa->mod_len);
1654
1655e_dst:
1656 ccp_free_data(&dst, cmd_q);
1657
1658e_src:
1659 ccp_dm_free(&src);
1660
1661e_exp:
1662 ccp_dm_free(&exp);
1663
1664e_ksb:
1665 ccp_free_ksb(cmd_q->ccp, op.ksb_key, ksb_count);
1666
1667 return ret;
1668}
1669
1670static int ccp_run_passthru_cmd(struct ccp_cmd_queue *cmd_q,
1671 struct ccp_cmd *cmd)
1672{
1673 struct ccp_passthru_engine *pt = &cmd->u.passthru;
1674 struct ccp_dm_workarea mask;
1675 struct ccp_data src, dst;
1676 struct ccp_op op;
1677 bool in_place = false;
1678 unsigned int i;
1679 int ret;
1680
1681 if (!pt->final && (pt->src_len & (CCP_PASSTHRU_BLOCKSIZE - 1)))
1682 return -EINVAL;
1683
1684 if (!pt->src || !pt->dst)
1685 return -EINVAL;
1686
1687 if (pt->bit_mod != CCP_PASSTHRU_BITWISE_NOOP) {
1688 if (pt->mask_len != CCP_PASSTHRU_MASKSIZE)
1689 return -EINVAL;
1690 if (!pt->mask)
1691 return -EINVAL;
1692 }
1693
1694 BUILD_BUG_ON(CCP_PASSTHRU_KSB_COUNT != 1);
1695
1696 memset(&op, 0, sizeof(op));
1697 op.cmd_q = cmd_q;
1698 op.jobid = ccp_gen_jobid(cmd_q->ccp);
1699
1700 if (pt->bit_mod != CCP_PASSTHRU_BITWISE_NOOP) {
1701 /* Load the mask */
1702 op.ksb_key = cmd_q->ksb_key;
1703
1704 ret = ccp_init_dm_workarea(&mask, cmd_q,
1705 CCP_PASSTHRU_KSB_COUNT *
1706 CCP_KSB_BYTES,
1707 DMA_TO_DEVICE);
1708 if (ret)
1709 return ret;
1710
1711 ccp_set_dm_area(&mask, 0, pt->mask, 0, pt->mask_len);
1712 ret = ccp_copy_to_ksb(cmd_q, &mask, op.jobid, op.ksb_key,
1713 CCP_PASSTHRU_BYTESWAP_NOOP);
1714 if (ret) {
1715 cmd->engine_error = cmd_q->cmd_error;
1716 goto e_mask;
1717 }
1718 }
1719
1720 /* Prepare the input and output data workareas. For in-place
1721 * operations we need to set the dma direction to BIDIRECTIONAL
1722 * and copy the src workarea to the dst workarea.
1723 */
1724 if (sg_virt(pt->src) == sg_virt(pt->dst))
1725 in_place = true;
1726
1727 ret = ccp_init_data(&src, cmd_q, pt->src, pt->src_len,
1728 CCP_PASSTHRU_MASKSIZE,
1729 in_place ? DMA_BIDIRECTIONAL : DMA_TO_DEVICE);
1730 if (ret)
1731 goto e_mask;
1732
Tom Lendacky8db88462015-02-03 13:07:05 -06001733 if (in_place) {
Tom Lendacky63b94502013-11-12 11:46:16 -06001734 dst = src;
Tom Lendacky8db88462015-02-03 13:07:05 -06001735 } else {
Tom Lendacky63b94502013-11-12 11:46:16 -06001736 ret = ccp_init_data(&dst, cmd_q, pt->dst, pt->src_len,
1737 CCP_PASSTHRU_MASKSIZE, DMA_FROM_DEVICE);
1738 if (ret)
1739 goto e_src;
1740 }
1741
1742 /* Send data to the CCP Passthru engine
1743 * Because the CCP engine works on a single source and destination
1744 * dma address at a time, each entry in the source scatterlist
1745 * (after the dma_map_sg call) must be less than or equal to the
1746 * (remaining) length in the destination scatterlist entry and the
1747 * length must be a multiple of CCP_PASSTHRU_BLOCKSIZE
1748 */
1749 dst.sg_wa.sg_used = 0;
1750 for (i = 1; i <= src.sg_wa.dma_count; i++) {
1751 if (!dst.sg_wa.sg ||
1752 (dst.sg_wa.sg->length < src.sg_wa.sg->length)) {
1753 ret = -EINVAL;
1754 goto e_dst;
1755 }
1756
1757 if (i == src.sg_wa.dma_count) {
1758 op.eom = 1;
1759 op.soc = 1;
1760 }
1761
1762 op.src.type = CCP_MEMTYPE_SYSTEM;
1763 op.src.u.dma.address = sg_dma_address(src.sg_wa.sg);
1764 op.src.u.dma.offset = 0;
1765 op.src.u.dma.length = sg_dma_len(src.sg_wa.sg);
1766
1767 op.dst.type = CCP_MEMTYPE_SYSTEM;
1768 op.dst.u.dma.address = sg_dma_address(dst.sg_wa.sg);
Dave Jones80e84c12014-02-09 09:59:14 +08001769 op.dst.u.dma.offset = dst.sg_wa.sg_used;
1770 op.dst.u.dma.length = op.src.u.dma.length;
Tom Lendacky63b94502013-11-12 11:46:16 -06001771
1772 ret = ccp_perform_passthru(&op);
1773 if (ret) {
1774 cmd->engine_error = cmd_q->cmd_error;
1775 goto e_dst;
1776 }
1777
1778 dst.sg_wa.sg_used += src.sg_wa.sg->length;
1779 if (dst.sg_wa.sg_used == dst.sg_wa.sg->length) {
1780 dst.sg_wa.sg = sg_next(dst.sg_wa.sg);
1781 dst.sg_wa.sg_used = 0;
1782 }
1783 src.sg_wa.sg = sg_next(src.sg_wa.sg);
1784 }
1785
1786e_dst:
1787 if (!in_place)
1788 ccp_free_data(&dst, cmd_q);
1789
1790e_src:
1791 ccp_free_data(&src, cmd_q);
1792
1793e_mask:
1794 if (pt->bit_mod != CCP_PASSTHRU_BITWISE_NOOP)
1795 ccp_dm_free(&mask);
1796
1797 return ret;
1798}
1799
1800static int ccp_run_ecc_mm_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd)
1801{
1802 struct ccp_ecc_engine *ecc = &cmd->u.ecc;
1803 struct ccp_dm_workarea src, dst;
1804 struct ccp_op op;
1805 int ret;
1806 u8 *save;
1807
1808 if (!ecc->u.mm.operand_1 ||
1809 (ecc->u.mm.operand_1_len > CCP_ECC_MODULUS_BYTES))
1810 return -EINVAL;
1811
1812 if (ecc->function != CCP_ECC_FUNCTION_MINV_384BIT)
1813 if (!ecc->u.mm.operand_2 ||
1814 (ecc->u.mm.operand_2_len > CCP_ECC_MODULUS_BYTES))
1815 return -EINVAL;
1816
1817 if (!ecc->u.mm.result ||
1818 (ecc->u.mm.result_len < CCP_ECC_MODULUS_BYTES))
1819 return -EINVAL;
1820
1821 memset(&op, 0, sizeof(op));
1822 op.cmd_q = cmd_q;
1823 op.jobid = ccp_gen_jobid(cmd_q->ccp);
1824
1825 /* Concatenate the modulus and the operands. Both the modulus and
1826 * the operands must be in little endian format. Since the input
1827 * is in big endian format it must be converted and placed in a
1828 * fixed length buffer.
1829 */
1830 ret = ccp_init_dm_workarea(&src, cmd_q, CCP_ECC_SRC_BUF_SIZE,
1831 DMA_TO_DEVICE);
1832 if (ret)
1833 return ret;
1834
1835 /* Save the workarea address since it is updated in order to perform
1836 * the concatenation
1837 */
1838 save = src.address;
1839
1840 /* Copy the ECC modulus */
1841 ccp_reverse_set_dm_area(&src, ecc->mod, ecc->mod_len,
Tom Lendacky63917232014-07-30 15:41:32 -05001842 CCP_ECC_OPERAND_SIZE, false);
Tom Lendacky63b94502013-11-12 11:46:16 -06001843 src.address += CCP_ECC_OPERAND_SIZE;
1844
1845 /* Copy the first operand */
1846 ccp_reverse_set_dm_area(&src, ecc->u.mm.operand_1,
1847 ecc->u.mm.operand_1_len,
Tom Lendacky63917232014-07-30 15:41:32 -05001848 CCP_ECC_OPERAND_SIZE, false);
Tom Lendacky63b94502013-11-12 11:46:16 -06001849 src.address += CCP_ECC_OPERAND_SIZE;
1850
1851 if (ecc->function != CCP_ECC_FUNCTION_MINV_384BIT) {
1852 /* Copy the second operand */
1853 ccp_reverse_set_dm_area(&src, ecc->u.mm.operand_2,
1854 ecc->u.mm.operand_2_len,
Tom Lendacky63917232014-07-30 15:41:32 -05001855 CCP_ECC_OPERAND_SIZE, false);
Tom Lendacky63b94502013-11-12 11:46:16 -06001856 src.address += CCP_ECC_OPERAND_SIZE;
1857 }
1858
1859 /* Restore the workarea address */
1860 src.address = save;
1861
1862 /* Prepare the output area for the operation */
1863 ret = ccp_init_dm_workarea(&dst, cmd_q, CCP_ECC_DST_BUF_SIZE,
1864 DMA_FROM_DEVICE);
1865 if (ret)
1866 goto e_src;
1867
1868 op.soc = 1;
1869 op.src.u.dma.address = src.dma.address;
1870 op.src.u.dma.offset = 0;
1871 op.src.u.dma.length = src.length;
1872 op.dst.u.dma.address = dst.dma.address;
1873 op.dst.u.dma.offset = 0;
1874 op.dst.u.dma.length = dst.length;
1875
1876 op.u.ecc.function = cmd->u.ecc.function;
1877
1878 ret = ccp_perform_ecc(&op);
1879 if (ret) {
1880 cmd->engine_error = cmd_q->cmd_error;
1881 goto e_dst;
1882 }
1883
1884 ecc->ecc_result = le16_to_cpup(
1885 (const __le16 *)(dst.address + CCP_ECC_RESULT_OFFSET));
1886 if (!(ecc->ecc_result & CCP_ECC_RESULT_SUCCESS)) {
1887 ret = -EIO;
1888 goto e_dst;
1889 }
1890
1891 /* Save the ECC result */
1892 ccp_reverse_get_dm_area(&dst, ecc->u.mm.result, CCP_ECC_MODULUS_BYTES);
1893
1894e_dst:
1895 ccp_dm_free(&dst);
1896
1897e_src:
1898 ccp_dm_free(&src);
1899
1900 return ret;
1901}
1902
1903static int ccp_run_ecc_pm_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd)
1904{
1905 struct ccp_ecc_engine *ecc = &cmd->u.ecc;
1906 struct ccp_dm_workarea src, dst;
1907 struct ccp_op op;
1908 int ret;
1909 u8 *save;
1910
1911 if (!ecc->u.pm.point_1.x ||
1912 (ecc->u.pm.point_1.x_len > CCP_ECC_MODULUS_BYTES) ||
1913 !ecc->u.pm.point_1.y ||
1914 (ecc->u.pm.point_1.y_len > CCP_ECC_MODULUS_BYTES))
1915 return -EINVAL;
1916
1917 if (ecc->function == CCP_ECC_FUNCTION_PADD_384BIT) {
1918 if (!ecc->u.pm.point_2.x ||
1919 (ecc->u.pm.point_2.x_len > CCP_ECC_MODULUS_BYTES) ||
1920 !ecc->u.pm.point_2.y ||
1921 (ecc->u.pm.point_2.y_len > CCP_ECC_MODULUS_BYTES))
1922 return -EINVAL;
1923 } else {
1924 if (!ecc->u.pm.domain_a ||
1925 (ecc->u.pm.domain_a_len > CCP_ECC_MODULUS_BYTES))
1926 return -EINVAL;
1927
1928 if (ecc->function == CCP_ECC_FUNCTION_PMUL_384BIT)
1929 if (!ecc->u.pm.scalar ||
1930 (ecc->u.pm.scalar_len > CCP_ECC_MODULUS_BYTES))
1931 return -EINVAL;
1932 }
1933
1934 if (!ecc->u.pm.result.x ||
1935 (ecc->u.pm.result.x_len < CCP_ECC_MODULUS_BYTES) ||
1936 !ecc->u.pm.result.y ||
1937 (ecc->u.pm.result.y_len < CCP_ECC_MODULUS_BYTES))
1938 return -EINVAL;
1939
1940 memset(&op, 0, sizeof(op));
1941 op.cmd_q = cmd_q;
1942 op.jobid = ccp_gen_jobid(cmd_q->ccp);
1943
1944 /* Concatenate the modulus and the operands. Both the modulus and
1945 * the operands must be in little endian format. Since the input
1946 * is in big endian format it must be converted and placed in a
1947 * fixed length buffer.
1948 */
1949 ret = ccp_init_dm_workarea(&src, cmd_q, CCP_ECC_SRC_BUF_SIZE,
1950 DMA_TO_DEVICE);
1951 if (ret)
1952 return ret;
1953
1954 /* Save the workarea address since it is updated in order to perform
1955 * the concatenation
1956 */
1957 save = src.address;
1958
1959 /* Copy the ECC modulus */
1960 ccp_reverse_set_dm_area(&src, ecc->mod, ecc->mod_len,
Tom Lendacky63917232014-07-30 15:41:32 -05001961 CCP_ECC_OPERAND_SIZE, false);
Tom Lendacky63b94502013-11-12 11:46:16 -06001962 src.address += CCP_ECC_OPERAND_SIZE;
1963
1964 /* Copy the first point X and Y coordinate */
1965 ccp_reverse_set_dm_area(&src, ecc->u.pm.point_1.x,
1966 ecc->u.pm.point_1.x_len,
Tom Lendacky63917232014-07-30 15:41:32 -05001967 CCP_ECC_OPERAND_SIZE, false);
Tom Lendacky63b94502013-11-12 11:46:16 -06001968 src.address += CCP_ECC_OPERAND_SIZE;
1969 ccp_reverse_set_dm_area(&src, ecc->u.pm.point_1.y,
1970 ecc->u.pm.point_1.y_len,
Tom Lendacky63917232014-07-30 15:41:32 -05001971 CCP_ECC_OPERAND_SIZE, false);
Tom Lendacky63b94502013-11-12 11:46:16 -06001972 src.address += CCP_ECC_OPERAND_SIZE;
1973
1974 /* Set the first point Z coordianate to 1 */
Tom Lendacky8db88462015-02-03 13:07:05 -06001975 *src.address = 0x01;
Tom Lendacky63b94502013-11-12 11:46:16 -06001976 src.address += CCP_ECC_OPERAND_SIZE;
1977
1978 if (ecc->function == CCP_ECC_FUNCTION_PADD_384BIT) {
1979 /* Copy the second point X and Y coordinate */
1980 ccp_reverse_set_dm_area(&src, ecc->u.pm.point_2.x,
1981 ecc->u.pm.point_2.x_len,
Tom Lendacky63917232014-07-30 15:41:32 -05001982 CCP_ECC_OPERAND_SIZE, false);
Tom Lendacky63b94502013-11-12 11:46:16 -06001983 src.address += CCP_ECC_OPERAND_SIZE;
1984 ccp_reverse_set_dm_area(&src, ecc->u.pm.point_2.y,
1985 ecc->u.pm.point_2.y_len,
Tom Lendacky63917232014-07-30 15:41:32 -05001986 CCP_ECC_OPERAND_SIZE, false);
Tom Lendacky63b94502013-11-12 11:46:16 -06001987 src.address += CCP_ECC_OPERAND_SIZE;
1988
1989 /* Set the second point Z coordianate to 1 */
Tom Lendacky8db88462015-02-03 13:07:05 -06001990 *src.address = 0x01;
Tom Lendacky63b94502013-11-12 11:46:16 -06001991 src.address += CCP_ECC_OPERAND_SIZE;
1992 } else {
1993 /* Copy the Domain "a" parameter */
1994 ccp_reverse_set_dm_area(&src, ecc->u.pm.domain_a,
1995 ecc->u.pm.domain_a_len,
Tom Lendacky63917232014-07-30 15:41:32 -05001996 CCP_ECC_OPERAND_SIZE, false);
Tom Lendacky63b94502013-11-12 11:46:16 -06001997 src.address += CCP_ECC_OPERAND_SIZE;
1998
1999 if (ecc->function == CCP_ECC_FUNCTION_PMUL_384BIT) {
2000 /* Copy the scalar value */
2001 ccp_reverse_set_dm_area(&src, ecc->u.pm.scalar,
2002 ecc->u.pm.scalar_len,
Tom Lendacky63917232014-07-30 15:41:32 -05002003 CCP_ECC_OPERAND_SIZE, false);
Tom Lendacky63b94502013-11-12 11:46:16 -06002004 src.address += CCP_ECC_OPERAND_SIZE;
2005 }
2006 }
2007
2008 /* Restore the workarea address */
2009 src.address = save;
2010
2011 /* Prepare the output area for the operation */
2012 ret = ccp_init_dm_workarea(&dst, cmd_q, CCP_ECC_DST_BUF_SIZE,
2013 DMA_FROM_DEVICE);
2014 if (ret)
2015 goto e_src;
2016
2017 op.soc = 1;
2018 op.src.u.dma.address = src.dma.address;
2019 op.src.u.dma.offset = 0;
2020 op.src.u.dma.length = src.length;
2021 op.dst.u.dma.address = dst.dma.address;
2022 op.dst.u.dma.offset = 0;
2023 op.dst.u.dma.length = dst.length;
2024
2025 op.u.ecc.function = cmd->u.ecc.function;
2026
2027 ret = ccp_perform_ecc(&op);
2028 if (ret) {
2029 cmd->engine_error = cmd_q->cmd_error;
2030 goto e_dst;
2031 }
2032
2033 ecc->ecc_result = le16_to_cpup(
2034 (const __le16 *)(dst.address + CCP_ECC_RESULT_OFFSET));
2035 if (!(ecc->ecc_result & CCP_ECC_RESULT_SUCCESS)) {
2036 ret = -EIO;
2037 goto e_dst;
2038 }
2039
2040 /* Save the workarea address since it is updated as we walk through
2041 * to copy the point math result
2042 */
2043 save = dst.address;
2044
2045 /* Save the ECC result X and Y coordinates */
2046 ccp_reverse_get_dm_area(&dst, ecc->u.pm.result.x,
2047 CCP_ECC_MODULUS_BYTES);
2048 dst.address += CCP_ECC_OUTPUT_SIZE;
2049 ccp_reverse_get_dm_area(&dst, ecc->u.pm.result.y,
2050 CCP_ECC_MODULUS_BYTES);
2051 dst.address += CCP_ECC_OUTPUT_SIZE;
2052
2053 /* Restore the workarea address */
2054 dst.address = save;
2055
2056e_dst:
2057 ccp_dm_free(&dst);
2058
2059e_src:
2060 ccp_dm_free(&src);
2061
2062 return ret;
2063}
2064
2065static int ccp_run_ecc_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd)
2066{
2067 struct ccp_ecc_engine *ecc = &cmd->u.ecc;
2068
2069 ecc->ecc_result = 0;
2070
2071 if (!ecc->mod ||
2072 (ecc->mod_len > CCP_ECC_MODULUS_BYTES))
2073 return -EINVAL;
2074
2075 switch (ecc->function) {
2076 case CCP_ECC_FUNCTION_MMUL_384BIT:
2077 case CCP_ECC_FUNCTION_MADD_384BIT:
2078 case CCP_ECC_FUNCTION_MINV_384BIT:
2079 return ccp_run_ecc_mm_cmd(cmd_q, cmd);
2080
2081 case CCP_ECC_FUNCTION_PADD_384BIT:
2082 case CCP_ECC_FUNCTION_PMUL_384BIT:
2083 case CCP_ECC_FUNCTION_PDBL_384BIT:
2084 return ccp_run_ecc_pm_cmd(cmd_q, cmd);
2085
2086 default:
2087 return -EINVAL;
2088 }
2089}
2090
2091int ccp_run_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd)
2092{
2093 int ret;
2094
2095 cmd->engine_error = 0;
2096 cmd_q->cmd_error = 0;
2097 cmd_q->int_rcvd = 0;
2098 cmd_q->free_slots = CMD_Q_DEPTH(ioread32(cmd_q->reg_status));
2099
2100 switch (cmd->engine) {
2101 case CCP_ENGINE_AES:
2102 ret = ccp_run_aes_cmd(cmd_q, cmd);
2103 break;
2104 case CCP_ENGINE_XTS_AES_128:
2105 ret = ccp_run_xts_aes_cmd(cmd_q, cmd);
2106 break;
2107 case CCP_ENGINE_SHA:
2108 ret = ccp_run_sha_cmd(cmd_q, cmd);
2109 break;
2110 case CCP_ENGINE_RSA:
2111 ret = ccp_run_rsa_cmd(cmd_q, cmd);
2112 break;
2113 case CCP_ENGINE_PASSTHRU:
2114 ret = ccp_run_passthru_cmd(cmd_q, cmd);
2115 break;
2116 case CCP_ENGINE_ECC:
2117 ret = ccp_run_ecc_cmd(cmd_q, cmd);
2118 break;
2119 default:
2120 ret = -EINVAL;
2121 }
2122
2123 return ret;
2124}