blob: 59f9849c36622845a711e420286f3c92e044a7e4 [file] [log] [blame]
Hook, Gary499df962019-05-14 21:53:16 +00001// SPDX-License-Identifier: GPL-2.0
Tom Lendacky63b94502013-11-12 11:46:16 -06002/*
3 * AMD Cryptographic Coprocessor (CCP) driver
4 *
Hook, Gary499df962019-05-14 21:53:16 +00005 * Copyright (C) 2013-2019 Advanced Micro Devices, Inc.
Tom Lendacky63b94502013-11-12 11:46:16 -06006 *
7 * Author: Tom Lendacky <thomas.lendacky@amd.com>
Gary R Hooka43eb982016-07-26 19:09:31 -05008 * Author: Gary R Hook <gary.hook@amd.com>
Tom Lendacky63b94502013-11-12 11:46:16 -06009 */
10
11#include <linux/module.h>
12#include <linux/kernel.h>
13#include <linux/pci.h>
Tom Lendacky63b94502013-11-12 11:46:16 -060014#include <linux/interrupt.h>
Tom Lendacky63b94502013-11-12 11:46:16 -060015#include <crypto/scatterwalk.h>
Gary R Hook990672d2017-03-15 13:20:52 -050016#include <crypto/des.h>
Gary R Hookea0375a2016-03-01 13:49:25 -060017#include <linux/ccp.h>
Tom Lendacky63b94502013-11-12 11:46:16 -060018
19#include "ccp-dev.h"
20
Tom Lendackyc11baa02014-01-24 16:18:02 -060021/* SHA initial context values */
Gary R Hook4b394a22016-07-26 19:10:21 -050022static const __be32 ccp_sha1_init[SHA1_DIGEST_SIZE / sizeof(__be32)] = {
Tom Lendackyc11baa02014-01-24 16:18:02 -060023 cpu_to_be32(SHA1_H0), cpu_to_be32(SHA1_H1),
24 cpu_to_be32(SHA1_H2), cpu_to_be32(SHA1_H3),
Gary R Hook4b394a22016-07-26 19:10:21 -050025 cpu_to_be32(SHA1_H4),
Tom Lendackyc11baa02014-01-24 16:18:02 -060026};
27
Gary R Hook4b394a22016-07-26 19:10:21 -050028static const __be32 ccp_sha224_init[SHA256_DIGEST_SIZE / sizeof(__be32)] = {
Tom Lendackyc11baa02014-01-24 16:18:02 -060029 cpu_to_be32(SHA224_H0), cpu_to_be32(SHA224_H1),
30 cpu_to_be32(SHA224_H2), cpu_to_be32(SHA224_H3),
31 cpu_to_be32(SHA224_H4), cpu_to_be32(SHA224_H5),
32 cpu_to_be32(SHA224_H6), cpu_to_be32(SHA224_H7),
33};
34
Gary R Hook4b394a22016-07-26 19:10:21 -050035static const __be32 ccp_sha256_init[SHA256_DIGEST_SIZE / sizeof(__be32)] = {
Tom Lendackyc11baa02014-01-24 16:18:02 -060036 cpu_to_be32(SHA256_H0), cpu_to_be32(SHA256_H1),
37 cpu_to_be32(SHA256_H2), cpu_to_be32(SHA256_H3),
38 cpu_to_be32(SHA256_H4), cpu_to_be32(SHA256_H5),
39 cpu_to_be32(SHA256_H6), cpu_to_be32(SHA256_H7),
40};
41
Gary R Hookccebcf32017-03-15 13:20:43 -050042static const __be64 ccp_sha384_init[SHA512_DIGEST_SIZE / sizeof(__be64)] = {
43 cpu_to_be64(SHA384_H0), cpu_to_be64(SHA384_H1),
44 cpu_to_be64(SHA384_H2), cpu_to_be64(SHA384_H3),
45 cpu_to_be64(SHA384_H4), cpu_to_be64(SHA384_H5),
46 cpu_to_be64(SHA384_H6), cpu_to_be64(SHA384_H7),
47};
48
49static const __be64 ccp_sha512_init[SHA512_DIGEST_SIZE / sizeof(__be64)] = {
50 cpu_to_be64(SHA512_H0), cpu_to_be64(SHA512_H1),
51 cpu_to_be64(SHA512_H2), cpu_to_be64(SHA512_H3),
52 cpu_to_be64(SHA512_H4), cpu_to_be64(SHA512_H5),
53 cpu_to_be64(SHA512_H6), cpu_to_be64(SHA512_H7),
54};
55
Gary R Hook4b394a22016-07-26 19:10:21 -050056#define CCP_NEW_JOBID(ccp) ((ccp->vdata->version == CCP_VERSION(3, 0)) ? \
57 ccp_gen_jobid(ccp) : 0)
58
Tom Lendacky63b94502013-11-12 11:46:16 -060059static u32 ccp_gen_jobid(struct ccp_device *ccp)
60{
61 return atomic_inc_return(&ccp->current_id) & CCP_JOBID_MASK;
62}
63
64static void ccp_sg_free(struct ccp_sg_workarea *wa)
65{
66 if (wa->dma_count)
67 dma_unmap_sg(wa->dma_dev, wa->dma_sg, wa->nents, wa->dma_dir);
68
69 wa->dma_count = 0;
70}
71
72static int ccp_init_sg_workarea(struct ccp_sg_workarea *wa, struct device *dev,
Tom Lendacky81a59f02014-01-06 13:34:17 -060073 struct scatterlist *sg, u64 len,
Tom Lendacky63b94502013-11-12 11:46:16 -060074 enum dma_data_direction dma_dir)
75{
76 memset(wa, 0, sizeof(*wa));
77
78 wa->sg = sg;
79 if (!sg)
80 return 0;
81
Tom Lendackyfb43f692015-06-01 11:15:53 -050082 wa->nents = sg_nents_for_len(sg, len);
83 if (wa->nents < 0)
84 return wa->nents;
85
Tom Lendacky63b94502013-11-12 11:46:16 -060086 wa->bytes_left = len;
87 wa->sg_used = 0;
88
89 if (len == 0)
90 return 0;
91
92 if (dma_dir == DMA_NONE)
93 return 0;
94
95 wa->dma_sg = sg;
96 wa->dma_dev = dev;
97 wa->dma_dir = dma_dir;
98 wa->dma_count = dma_map_sg(dev, sg, wa->nents, dma_dir);
99 if (!wa->dma_count)
100 return -ENOMEM;
101
Tom Lendacky63b94502013-11-12 11:46:16 -0600102 return 0;
103}
104
105static void ccp_update_sg_workarea(struct ccp_sg_workarea *wa, unsigned int len)
106{
Tom Lendacky81a59f02014-01-06 13:34:17 -0600107 unsigned int nbytes = min_t(u64, len, wa->bytes_left);
Tom Lendacky63b94502013-11-12 11:46:16 -0600108
109 if (!wa->sg)
110 return;
111
112 wa->sg_used += nbytes;
113 wa->bytes_left -= nbytes;
114 if (wa->sg_used == wa->sg->length) {
115 wa->sg = sg_next(wa->sg);
116 wa->sg_used = 0;
117 }
118}
119
120static void ccp_dm_free(struct ccp_dm_workarea *wa)
121{
122 if (wa->length <= CCP_DMAPOOL_MAX_SIZE) {
123 if (wa->address)
124 dma_pool_free(wa->dma_pool, wa->address,
125 wa->dma.address);
126 } else {
127 if (wa->dma.address)
128 dma_unmap_single(wa->dev, wa->dma.address, wa->length,
129 wa->dma.dir);
130 kfree(wa->address);
131 }
132
133 wa->address = NULL;
134 wa->dma.address = 0;
135}
136
137static int ccp_init_dm_workarea(struct ccp_dm_workarea *wa,
138 struct ccp_cmd_queue *cmd_q,
139 unsigned int len,
140 enum dma_data_direction dir)
141{
142 memset(wa, 0, sizeof(*wa));
143
144 if (!len)
145 return 0;
146
147 wa->dev = cmd_q->ccp->dev;
148 wa->length = len;
149
150 if (len <= CCP_DMAPOOL_MAX_SIZE) {
151 wa->dma_pool = cmd_q->dma_pool;
152
153 wa->address = dma_pool_alloc(wa->dma_pool, GFP_KERNEL,
154 &wa->dma.address);
155 if (!wa->address)
156 return -ENOMEM;
157
158 wa->dma.length = CCP_DMAPOOL_MAX_SIZE;
159
160 memset(wa->address, 0, CCP_DMAPOOL_MAX_SIZE);
161 } else {
162 wa->address = kzalloc(len, GFP_KERNEL);
163 if (!wa->address)
164 return -ENOMEM;
165
166 wa->dma.address = dma_map_single(wa->dev, wa->address, len,
167 dir);
Pan Bianef4064b2017-08-08 21:42:47 +0800168 if (dma_mapping_error(wa->dev, wa->dma.address))
Tom Lendacky63b94502013-11-12 11:46:16 -0600169 return -ENOMEM;
170
171 wa->dma.length = len;
172 }
173 wa->dma.dir = dir;
174
175 return 0;
176}
177
Gary R Hookb698a9f2018-03-07 11:31:14 -0600178static int ccp_set_dm_area(struct ccp_dm_workarea *wa, unsigned int wa_offset,
179 struct scatterlist *sg, unsigned int sg_offset,
180 unsigned int len)
Tom Lendacky63b94502013-11-12 11:46:16 -0600181{
182 WARN_ON(!wa->address);
183
Gary R Hookb698a9f2018-03-07 11:31:14 -0600184 if (len > (wa->length - wa_offset))
185 return -EINVAL;
186
Tom Lendacky63b94502013-11-12 11:46:16 -0600187 scatterwalk_map_and_copy(wa->address + wa_offset, sg, sg_offset, len,
188 0);
Gary R Hookb698a9f2018-03-07 11:31:14 -0600189 return 0;
Tom Lendacky63b94502013-11-12 11:46:16 -0600190}
191
192static void ccp_get_dm_area(struct ccp_dm_workarea *wa, unsigned int wa_offset,
193 struct scatterlist *sg, unsigned int sg_offset,
194 unsigned int len)
195{
196 WARN_ON(!wa->address);
197
198 scatterwalk_map_and_copy(wa->address + wa_offset, sg, sg_offset, len,
199 1);
200}
201
Tom Lendacky355eba52015-10-01 16:32:31 -0500202static int ccp_reverse_set_dm_area(struct ccp_dm_workarea *wa,
Gary R Hook83d650a2017-02-09 15:50:08 -0600203 unsigned int wa_offset,
Tom Lendacky355eba52015-10-01 16:32:31 -0500204 struct scatterlist *sg,
Gary R Hook83d650a2017-02-09 15:50:08 -0600205 unsigned int sg_offset,
206 unsigned int len)
Tom Lendacky63b94502013-11-12 11:46:16 -0600207{
Gary R Hook83d650a2017-02-09 15:50:08 -0600208 u8 *p, *q;
Gary R Hookb698a9f2018-03-07 11:31:14 -0600209 int rc;
Tom Lendacky63b94502013-11-12 11:46:16 -0600210
Gary R Hookb698a9f2018-03-07 11:31:14 -0600211 rc = ccp_set_dm_area(wa, wa_offset, sg, sg_offset, len);
212 if (rc)
213 return rc;
Tom Lendacky63b94502013-11-12 11:46:16 -0600214
Gary R Hook83d650a2017-02-09 15:50:08 -0600215 p = wa->address + wa_offset;
216 q = p + len - 1;
217 while (p < q) {
218 *p = *p ^ *q;
219 *q = *p ^ *q;
220 *p = *p ^ *q;
221 p++;
222 q--;
Tom Lendacky63b94502013-11-12 11:46:16 -0600223 }
Tom Lendacky355eba52015-10-01 16:32:31 -0500224 return 0;
Tom Lendacky63b94502013-11-12 11:46:16 -0600225}
226
227static void ccp_reverse_get_dm_area(struct ccp_dm_workarea *wa,
Gary R Hook83d650a2017-02-09 15:50:08 -0600228 unsigned int wa_offset,
Tom Lendacky63b94502013-11-12 11:46:16 -0600229 struct scatterlist *sg,
Gary R Hook83d650a2017-02-09 15:50:08 -0600230 unsigned int sg_offset,
Tom Lendacky63b94502013-11-12 11:46:16 -0600231 unsigned int len)
232{
Gary R Hook83d650a2017-02-09 15:50:08 -0600233 u8 *p, *q;
Tom Lendacky63b94502013-11-12 11:46:16 -0600234
Gary R Hook83d650a2017-02-09 15:50:08 -0600235 p = wa->address + wa_offset;
236 q = p + len - 1;
237 while (p < q) {
238 *p = *p ^ *q;
239 *q = *p ^ *q;
240 *p = *p ^ *q;
241 p++;
242 q--;
Tom Lendacky63b94502013-11-12 11:46:16 -0600243 }
Gary R Hook83d650a2017-02-09 15:50:08 -0600244
245 ccp_get_dm_area(wa, wa_offset, sg, sg_offset, len);
Tom Lendacky63b94502013-11-12 11:46:16 -0600246}
247
248static void ccp_free_data(struct ccp_data *data, struct ccp_cmd_queue *cmd_q)
249{
250 ccp_dm_free(&data->dm_wa);
251 ccp_sg_free(&data->sg_wa);
252}
253
254static int ccp_init_data(struct ccp_data *data, struct ccp_cmd_queue *cmd_q,
Tom Lendacky81a59f02014-01-06 13:34:17 -0600255 struct scatterlist *sg, u64 sg_len,
Tom Lendacky63b94502013-11-12 11:46:16 -0600256 unsigned int dm_len,
257 enum dma_data_direction dir)
258{
259 int ret;
260
261 memset(data, 0, sizeof(*data));
262
263 ret = ccp_init_sg_workarea(&data->sg_wa, cmd_q->ccp->dev, sg, sg_len,
264 dir);
265 if (ret)
266 goto e_err;
267
268 ret = ccp_init_dm_workarea(&data->dm_wa, cmd_q, dm_len, dir);
269 if (ret)
270 goto e_err;
271
272 return 0;
273
274e_err:
275 ccp_free_data(data, cmd_q);
276
277 return ret;
278}
279
280static unsigned int ccp_queue_buf(struct ccp_data *data, unsigned int from)
281{
282 struct ccp_sg_workarea *sg_wa = &data->sg_wa;
283 struct ccp_dm_workarea *dm_wa = &data->dm_wa;
284 unsigned int buf_count, nbytes;
285
286 /* Clear the buffer if setting it */
287 if (!from)
288 memset(dm_wa->address, 0, dm_wa->length);
289
290 if (!sg_wa->sg)
291 return 0;
292
Tom Lendacky81a59f02014-01-06 13:34:17 -0600293 /* Perform the copy operation
294 * nbytes will always be <= UINT_MAX because dm_wa->length is
295 * an unsigned int
296 */
297 nbytes = min_t(u64, sg_wa->bytes_left, dm_wa->length);
Tom Lendacky63b94502013-11-12 11:46:16 -0600298 scatterwalk_map_and_copy(dm_wa->address, sg_wa->sg, sg_wa->sg_used,
299 nbytes, from);
300
301 /* Update the structures and generate the count */
302 buf_count = 0;
303 while (sg_wa->bytes_left && (buf_count < dm_wa->length)) {
Tom Lendacky81a59f02014-01-06 13:34:17 -0600304 nbytes = min(sg_wa->sg->length - sg_wa->sg_used,
305 dm_wa->length - buf_count);
306 nbytes = min_t(u64, sg_wa->bytes_left, nbytes);
Tom Lendacky63b94502013-11-12 11:46:16 -0600307
308 buf_count += nbytes;
309 ccp_update_sg_workarea(sg_wa, nbytes);
310 }
311
312 return buf_count;
313}
314
315static unsigned int ccp_fill_queue_buf(struct ccp_data *data)
316{
317 return ccp_queue_buf(data, 0);
318}
319
320static unsigned int ccp_empty_queue_buf(struct ccp_data *data)
321{
322 return ccp_queue_buf(data, 1);
323}
324
325static void ccp_prepare_data(struct ccp_data *src, struct ccp_data *dst,
326 struct ccp_op *op, unsigned int block_size,
327 bool blocksize_op)
328{
329 unsigned int sg_src_len, sg_dst_len, op_len;
330
331 /* The CCP can only DMA from/to one address each per operation. This
332 * requires that we find the smallest DMA area between the source
Tom Lendacky81a59f02014-01-06 13:34:17 -0600333 * and destination. The resulting len values will always be <= UINT_MAX
334 * because the dma length is an unsigned int.
Tom Lendacky63b94502013-11-12 11:46:16 -0600335 */
Tom Lendacky81a59f02014-01-06 13:34:17 -0600336 sg_src_len = sg_dma_len(src->sg_wa.sg) - src->sg_wa.sg_used;
337 sg_src_len = min_t(u64, src->sg_wa.bytes_left, sg_src_len);
Tom Lendacky63b94502013-11-12 11:46:16 -0600338
339 if (dst) {
Tom Lendacky81a59f02014-01-06 13:34:17 -0600340 sg_dst_len = sg_dma_len(dst->sg_wa.sg) - dst->sg_wa.sg_used;
341 sg_dst_len = min_t(u64, src->sg_wa.bytes_left, sg_dst_len);
Tom Lendacky63b94502013-11-12 11:46:16 -0600342 op_len = min(sg_src_len, sg_dst_len);
Tom Lendacky8db88462015-02-03 13:07:05 -0600343 } else {
Tom Lendacky63b94502013-11-12 11:46:16 -0600344 op_len = sg_src_len;
Tom Lendacky8db88462015-02-03 13:07:05 -0600345 }
Tom Lendacky63b94502013-11-12 11:46:16 -0600346
347 /* The data operation length will be at least block_size in length
348 * or the smaller of available sg room remaining for the source or
349 * the destination
350 */
351 op_len = max(op_len, block_size);
352
353 /* Unless we have to buffer data, there's no reason to wait */
354 op->soc = 0;
355
356 if (sg_src_len < block_size) {
357 /* Not enough data in the sg element, so it
358 * needs to be buffered into a blocksize chunk
359 */
360 int cp_len = ccp_fill_queue_buf(src);
361
362 op->soc = 1;
363 op->src.u.dma.address = src->dm_wa.dma.address;
364 op->src.u.dma.offset = 0;
365 op->src.u.dma.length = (blocksize_op) ? block_size : cp_len;
366 } else {
367 /* Enough data in the sg element, but we need to
368 * adjust for any previously copied data
369 */
370 op->src.u.dma.address = sg_dma_address(src->sg_wa.sg);
371 op->src.u.dma.offset = src->sg_wa.sg_used;
372 op->src.u.dma.length = op_len & ~(block_size - 1);
373
374 ccp_update_sg_workarea(&src->sg_wa, op->src.u.dma.length);
375 }
376
377 if (dst) {
378 if (sg_dst_len < block_size) {
379 /* Not enough room in the sg element or we're on the
380 * last piece of data (when using padding), so the
381 * output needs to be buffered into a blocksize chunk
382 */
383 op->soc = 1;
384 op->dst.u.dma.address = dst->dm_wa.dma.address;
385 op->dst.u.dma.offset = 0;
386 op->dst.u.dma.length = op->src.u.dma.length;
387 } else {
388 /* Enough room in the sg element, but we need to
389 * adjust for any previously used area
390 */
391 op->dst.u.dma.address = sg_dma_address(dst->sg_wa.sg);
392 op->dst.u.dma.offset = dst->sg_wa.sg_used;
393 op->dst.u.dma.length = op->src.u.dma.length;
394 }
395 }
396}
397
398static void ccp_process_data(struct ccp_data *src, struct ccp_data *dst,
399 struct ccp_op *op)
400{
401 op->init = 0;
402
403 if (dst) {
404 if (op->dst.u.dma.address == dst->dm_wa.dma.address)
405 ccp_empty_queue_buf(dst);
406 else
407 ccp_update_sg_workarea(&dst->sg_wa,
408 op->dst.u.dma.length);
409 }
410}
411
Gary R Hook956ee212016-07-26 19:09:40 -0500412static int ccp_copy_to_from_sb(struct ccp_cmd_queue *cmd_q,
413 struct ccp_dm_workarea *wa, u32 jobid, u32 sb,
414 u32 byte_swap, bool from)
Tom Lendacky63b94502013-11-12 11:46:16 -0600415{
416 struct ccp_op op;
417
418 memset(&op, 0, sizeof(op));
419
420 op.cmd_q = cmd_q;
421 op.jobid = jobid;
422 op.eom = 1;
423
424 if (from) {
425 op.soc = 1;
Gary R Hook956ee212016-07-26 19:09:40 -0500426 op.src.type = CCP_MEMTYPE_SB;
427 op.src.u.sb = sb;
Tom Lendacky63b94502013-11-12 11:46:16 -0600428 op.dst.type = CCP_MEMTYPE_SYSTEM;
429 op.dst.u.dma.address = wa->dma.address;
430 op.dst.u.dma.length = wa->length;
431 } else {
432 op.src.type = CCP_MEMTYPE_SYSTEM;
433 op.src.u.dma.address = wa->dma.address;
434 op.src.u.dma.length = wa->length;
Gary R Hook956ee212016-07-26 19:09:40 -0500435 op.dst.type = CCP_MEMTYPE_SB;
436 op.dst.u.sb = sb;
Tom Lendacky63b94502013-11-12 11:46:16 -0600437 }
438
439 op.u.passthru.byte_swap = byte_swap;
440
Gary R Hooka43eb982016-07-26 19:09:31 -0500441 return cmd_q->ccp->vdata->perform->passthru(&op);
Tom Lendacky63b94502013-11-12 11:46:16 -0600442}
443
Gary R Hook956ee212016-07-26 19:09:40 -0500444static int ccp_copy_to_sb(struct ccp_cmd_queue *cmd_q,
445 struct ccp_dm_workarea *wa, u32 jobid, u32 sb,
446 u32 byte_swap)
Tom Lendacky63b94502013-11-12 11:46:16 -0600447{
Gary R Hook956ee212016-07-26 19:09:40 -0500448 return ccp_copy_to_from_sb(cmd_q, wa, jobid, sb, byte_swap, false);
Tom Lendacky63b94502013-11-12 11:46:16 -0600449}
450
Gary R Hook956ee212016-07-26 19:09:40 -0500451static int ccp_copy_from_sb(struct ccp_cmd_queue *cmd_q,
452 struct ccp_dm_workarea *wa, u32 jobid, u32 sb,
453 u32 byte_swap)
Tom Lendacky63b94502013-11-12 11:46:16 -0600454{
Gary R Hook956ee212016-07-26 19:09:40 -0500455 return ccp_copy_to_from_sb(cmd_q, wa, jobid, sb, byte_swap, true);
Tom Lendacky63b94502013-11-12 11:46:16 -0600456}
457
458static int ccp_run_aes_cmac_cmd(struct ccp_cmd_queue *cmd_q,
459 struct ccp_cmd *cmd)
460{
461 struct ccp_aes_engine *aes = &cmd->u.aes;
462 struct ccp_dm_workarea key, ctx;
463 struct ccp_data src;
464 struct ccp_op op;
465 unsigned int dm_offset;
466 int ret;
467
468 if (!((aes->key_len == AES_KEYSIZE_128) ||
469 (aes->key_len == AES_KEYSIZE_192) ||
470 (aes->key_len == AES_KEYSIZE_256)))
471 return -EINVAL;
472
473 if (aes->src_len & (AES_BLOCK_SIZE - 1))
474 return -EINVAL;
475
476 if (aes->iv_len != AES_BLOCK_SIZE)
477 return -EINVAL;
478
479 if (!aes->key || !aes->iv || !aes->src)
480 return -EINVAL;
481
482 if (aes->cmac_final) {
483 if (aes->cmac_key_len != AES_BLOCK_SIZE)
484 return -EINVAL;
485
486 if (!aes->cmac_key)
487 return -EINVAL;
488 }
489
Gary R Hook956ee212016-07-26 19:09:40 -0500490 BUILD_BUG_ON(CCP_AES_KEY_SB_COUNT != 1);
491 BUILD_BUG_ON(CCP_AES_CTX_SB_COUNT != 1);
Tom Lendacky63b94502013-11-12 11:46:16 -0600492
493 ret = -EIO;
494 memset(&op, 0, sizeof(op));
495 op.cmd_q = cmd_q;
Gary R Hook4b394a22016-07-26 19:10:21 -0500496 op.jobid = CCP_NEW_JOBID(cmd_q->ccp);
Gary R Hook956ee212016-07-26 19:09:40 -0500497 op.sb_key = cmd_q->sb_key;
498 op.sb_ctx = cmd_q->sb_ctx;
Tom Lendacky63b94502013-11-12 11:46:16 -0600499 op.init = 1;
500 op.u.aes.type = aes->type;
501 op.u.aes.mode = aes->mode;
502 op.u.aes.action = aes->action;
503
Gary R Hook956ee212016-07-26 19:09:40 -0500504 /* All supported key sizes fit in a single (32-byte) SB entry
Tom Lendacky63b94502013-11-12 11:46:16 -0600505 * and must be in little endian format. Use the 256-bit byte
506 * swap passthru option to convert from big endian to little
507 * endian.
508 */
509 ret = ccp_init_dm_workarea(&key, cmd_q,
Gary R Hook956ee212016-07-26 19:09:40 -0500510 CCP_AES_KEY_SB_COUNT * CCP_SB_BYTES,
Tom Lendacky63b94502013-11-12 11:46:16 -0600511 DMA_TO_DEVICE);
512 if (ret)
513 return ret;
514
Gary R Hook956ee212016-07-26 19:09:40 -0500515 dm_offset = CCP_SB_BYTES - aes->key_len;
Gary R Hookb698a9f2018-03-07 11:31:14 -0600516 ret = ccp_set_dm_area(&key, dm_offset, aes->key, 0, aes->key_len);
517 if (ret)
518 goto e_key;
Gary R Hook956ee212016-07-26 19:09:40 -0500519 ret = ccp_copy_to_sb(cmd_q, &key, op.jobid, op.sb_key,
520 CCP_PASSTHRU_BYTESWAP_256BIT);
Tom Lendacky63b94502013-11-12 11:46:16 -0600521 if (ret) {
522 cmd->engine_error = cmd_q->cmd_error;
523 goto e_key;
524 }
525
Gary R Hook956ee212016-07-26 19:09:40 -0500526 /* The AES context fits in a single (32-byte) SB entry and
Tom Lendacky63b94502013-11-12 11:46:16 -0600527 * must be in little endian format. Use the 256-bit byte swap
528 * passthru option to convert from big endian to little endian.
529 */
530 ret = ccp_init_dm_workarea(&ctx, cmd_q,
Gary R Hook956ee212016-07-26 19:09:40 -0500531 CCP_AES_CTX_SB_COUNT * CCP_SB_BYTES,
Tom Lendacky63b94502013-11-12 11:46:16 -0600532 DMA_BIDIRECTIONAL);
533 if (ret)
534 goto e_key;
535
Gary R Hook956ee212016-07-26 19:09:40 -0500536 dm_offset = CCP_SB_BYTES - AES_BLOCK_SIZE;
Gary R Hookb698a9f2018-03-07 11:31:14 -0600537 ret = ccp_set_dm_area(&ctx, dm_offset, aes->iv, 0, aes->iv_len);
538 if (ret)
539 goto e_ctx;
Gary R Hook956ee212016-07-26 19:09:40 -0500540 ret = ccp_copy_to_sb(cmd_q, &ctx, op.jobid, op.sb_ctx,
541 CCP_PASSTHRU_BYTESWAP_256BIT);
Tom Lendacky63b94502013-11-12 11:46:16 -0600542 if (ret) {
543 cmd->engine_error = cmd_q->cmd_error;
544 goto e_ctx;
545 }
546
547 /* Send data to the CCP AES engine */
548 ret = ccp_init_data(&src, cmd_q, aes->src, aes->src_len,
549 AES_BLOCK_SIZE, DMA_TO_DEVICE);
550 if (ret)
551 goto e_ctx;
552
553 while (src.sg_wa.bytes_left) {
554 ccp_prepare_data(&src, NULL, &op, AES_BLOCK_SIZE, true);
555 if (aes->cmac_final && !src.sg_wa.bytes_left) {
556 op.eom = 1;
557
558 /* Push the K1/K2 key to the CCP now */
Gary R Hook956ee212016-07-26 19:09:40 -0500559 ret = ccp_copy_from_sb(cmd_q, &ctx, op.jobid,
560 op.sb_ctx,
561 CCP_PASSTHRU_BYTESWAP_256BIT);
Tom Lendacky63b94502013-11-12 11:46:16 -0600562 if (ret) {
563 cmd->engine_error = cmd_q->cmd_error;
564 goto e_src;
565 }
566
Gary R Hookb698a9f2018-03-07 11:31:14 -0600567 ret = ccp_set_dm_area(&ctx, 0, aes->cmac_key, 0,
568 aes->cmac_key_len);
569 if (ret)
570 goto e_src;
Gary R Hook956ee212016-07-26 19:09:40 -0500571 ret = ccp_copy_to_sb(cmd_q, &ctx, op.jobid, op.sb_ctx,
572 CCP_PASSTHRU_BYTESWAP_256BIT);
Tom Lendacky63b94502013-11-12 11:46:16 -0600573 if (ret) {
574 cmd->engine_error = cmd_q->cmd_error;
575 goto e_src;
576 }
577 }
578
Gary R Hooka43eb982016-07-26 19:09:31 -0500579 ret = cmd_q->ccp->vdata->perform->aes(&op);
Tom Lendacky63b94502013-11-12 11:46:16 -0600580 if (ret) {
581 cmd->engine_error = cmd_q->cmd_error;
582 goto e_src;
583 }
584
585 ccp_process_data(&src, NULL, &op);
586 }
587
588 /* Retrieve the AES context - convert from LE to BE using
589 * 32-byte (256-bit) byteswapping
590 */
Gary R Hook956ee212016-07-26 19:09:40 -0500591 ret = ccp_copy_from_sb(cmd_q, &ctx, op.jobid, op.sb_ctx,
592 CCP_PASSTHRU_BYTESWAP_256BIT);
Tom Lendacky63b94502013-11-12 11:46:16 -0600593 if (ret) {
594 cmd->engine_error = cmd_q->cmd_error;
595 goto e_src;
596 }
597
598 /* ...but we only need AES_BLOCK_SIZE bytes */
Gary R Hook956ee212016-07-26 19:09:40 -0500599 dm_offset = CCP_SB_BYTES - AES_BLOCK_SIZE;
Tom Lendacky63b94502013-11-12 11:46:16 -0600600 ccp_get_dm_area(&ctx, dm_offset, aes->iv, 0, aes->iv_len);
601
602e_src:
603 ccp_free_data(&src, cmd_q);
604
605e_ctx:
606 ccp_dm_free(&ctx);
607
608e_key:
609 ccp_dm_free(&key);
610
611 return ret;
612}
613
Gary R Hook36cf5152017-03-15 13:21:01 -0500614static int ccp_run_aes_gcm_cmd(struct ccp_cmd_queue *cmd_q,
615 struct ccp_cmd *cmd)
616{
617 struct ccp_aes_engine *aes = &cmd->u.aes;
618 struct ccp_dm_workarea key, ctx, final_wa, tag;
619 struct ccp_data src, dst;
620 struct ccp_data aad;
621 struct ccp_op op;
622
623 unsigned long long *final;
624 unsigned int dm_offset;
Hook, Gary20e833d2019-07-10 00:09:22 +0000625 unsigned int jobid;
Gary R Hook36cf5152017-03-15 13:21:01 -0500626 unsigned int ilen;
627 bool in_place = true; /* Default value */
628 int ret;
629
630 struct scatterlist *p_inp, sg_inp[2];
631 struct scatterlist *p_tag, sg_tag[2];
632 struct scatterlist *p_outp, sg_outp[2];
633 struct scatterlist *p_aad;
634
635 if (!aes->iv)
636 return -EINVAL;
637
638 if (!((aes->key_len == AES_KEYSIZE_128) ||
639 (aes->key_len == AES_KEYSIZE_192) ||
640 (aes->key_len == AES_KEYSIZE_256)))
641 return -EINVAL;
642
643 if (!aes->key) /* Gotta have a key SGL */
644 return -EINVAL;
645
646 /* First, decompose the source buffer into AAD & PT,
647 * and the destination buffer into AAD, CT & tag, or
648 * the input into CT & tag.
649 * It is expected that the input and output SGs will
650 * be valid, even if the AAD and input lengths are 0.
651 */
652 p_aad = aes->src;
653 p_inp = scatterwalk_ffwd(sg_inp, aes->src, aes->aad_len);
654 p_outp = scatterwalk_ffwd(sg_outp, aes->dst, aes->aad_len);
655 if (aes->action == CCP_AES_ACTION_ENCRYPT) {
656 ilen = aes->src_len;
657 p_tag = scatterwalk_ffwd(sg_tag, p_outp, ilen);
658 } else {
659 /* Input length for decryption includes tag */
660 ilen = aes->src_len - AES_BLOCK_SIZE;
661 p_tag = scatterwalk_ffwd(sg_tag, p_inp, ilen);
662 }
663
Hook, Gary20e833d2019-07-10 00:09:22 +0000664 jobid = CCP_NEW_JOBID(cmd_q->ccp);
665
Gary R Hook36cf5152017-03-15 13:21:01 -0500666 memset(&op, 0, sizeof(op));
667 op.cmd_q = cmd_q;
Hook, Gary20e833d2019-07-10 00:09:22 +0000668 op.jobid = jobid;
Gary R Hook36cf5152017-03-15 13:21:01 -0500669 op.sb_key = cmd_q->sb_key; /* Pre-allocated */
670 op.sb_ctx = cmd_q->sb_ctx; /* Pre-allocated */
671 op.init = 1;
672 op.u.aes.type = aes->type;
673
674 /* Copy the key to the LSB */
675 ret = ccp_init_dm_workarea(&key, cmd_q,
676 CCP_AES_CTX_SB_COUNT * CCP_SB_BYTES,
677 DMA_TO_DEVICE);
678 if (ret)
679 return ret;
680
681 dm_offset = CCP_SB_BYTES - aes->key_len;
Gary R Hookb698a9f2018-03-07 11:31:14 -0600682 ret = ccp_set_dm_area(&key, dm_offset, aes->key, 0, aes->key_len);
683 if (ret)
684 goto e_key;
Gary R Hook36cf5152017-03-15 13:21:01 -0500685 ret = ccp_copy_to_sb(cmd_q, &key, op.jobid, op.sb_key,
686 CCP_PASSTHRU_BYTESWAP_256BIT);
687 if (ret) {
688 cmd->engine_error = cmd_q->cmd_error;
689 goto e_key;
690 }
691
692 /* Copy the context (IV) to the LSB.
693 * There is an assumption here that the IV is 96 bits in length, plus
694 * a nonce of 32 bits. If no IV is present, use a zeroed buffer.
695 */
696 ret = ccp_init_dm_workarea(&ctx, cmd_q,
697 CCP_AES_CTX_SB_COUNT * CCP_SB_BYTES,
698 DMA_BIDIRECTIONAL);
699 if (ret)
700 goto e_key;
701
702 dm_offset = CCP_AES_CTX_SB_COUNT * CCP_SB_BYTES - aes->iv_len;
Gary R Hookb698a9f2018-03-07 11:31:14 -0600703 ret = ccp_set_dm_area(&ctx, dm_offset, aes->iv, 0, aes->iv_len);
704 if (ret)
705 goto e_ctx;
Gary R Hook36cf5152017-03-15 13:21:01 -0500706
707 ret = ccp_copy_to_sb(cmd_q, &ctx, op.jobid, op.sb_ctx,
708 CCP_PASSTHRU_BYTESWAP_256BIT);
709 if (ret) {
710 cmd->engine_error = cmd_q->cmd_error;
711 goto e_ctx;
712 }
713
714 op.init = 1;
715 if (aes->aad_len > 0) {
716 /* Step 1: Run a GHASH over the Additional Authenticated Data */
717 ret = ccp_init_data(&aad, cmd_q, p_aad, aes->aad_len,
718 AES_BLOCK_SIZE,
719 DMA_TO_DEVICE);
720 if (ret)
721 goto e_ctx;
722
723 op.u.aes.mode = CCP_AES_MODE_GHASH;
724 op.u.aes.action = CCP_AES_GHASHAAD;
725
726 while (aad.sg_wa.bytes_left) {
727 ccp_prepare_data(&aad, NULL, &op, AES_BLOCK_SIZE, true);
728
729 ret = cmd_q->ccp->vdata->perform->aes(&op);
730 if (ret) {
731 cmd->engine_error = cmd_q->cmd_error;
732 goto e_aad;
733 }
734
735 ccp_process_data(&aad, NULL, &op);
736 op.init = 0;
737 }
738 }
739
740 op.u.aes.mode = CCP_AES_MODE_GCTR;
741 op.u.aes.action = aes->action;
742
743 if (ilen > 0) {
744 /* Step 2: Run a GCTR over the plaintext */
745 in_place = (sg_virt(p_inp) == sg_virt(p_outp)) ? true : false;
746
747 ret = ccp_init_data(&src, cmd_q, p_inp, ilen,
748 AES_BLOCK_SIZE,
749 in_place ? DMA_BIDIRECTIONAL
750 : DMA_TO_DEVICE);
751 if (ret)
752 goto e_ctx;
753
754 if (in_place) {
755 dst = src;
756 } else {
757 ret = ccp_init_data(&dst, cmd_q, p_outp, ilen,
758 AES_BLOCK_SIZE, DMA_FROM_DEVICE);
759 if (ret)
760 goto e_src;
761 }
762
763 op.soc = 0;
764 op.eom = 0;
765 op.init = 1;
766 while (src.sg_wa.bytes_left) {
767 ccp_prepare_data(&src, &dst, &op, AES_BLOCK_SIZE, true);
768 if (!src.sg_wa.bytes_left) {
769 unsigned int nbytes = aes->src_len
770 % AES_BLOCK_SIZE;
771
772 if (nbytes) {
773 op.eom = 1;
774 op.u.aes.size = (nbytes * 8) - 1;
775 }
776 }
777
778 ret = cmd_q->ccp->vdata->perform->aes(&op);
779 if (ret) {
780 cmd->engine_error = cmd_q->cmd_error;
781 goto e_dst;
782 }
783
784 ccp_process_data(&src, &dst, &op);
785 op.init = 0;
786 }
787 }
788
789 /* Step 3: Update the IV portion of the context with the original IV */
790 ret = ccp_copy_from_sb(cmd_q, &ctx, op.jobid, op.sb_ctx,
791 CCP_PASSTHRU_BYTESWAP_256BIT);
792 if (ret) {
793 cmd->engine_error = cmd_q->cmd_error;
794 goto e_dst;
795 }
796
Gary R Hookb698a9f2018-03-07 11:31:14 -0600797 ret = ccp_set_dm_area(&ctx, dm_offset, aes->iv, 0, aes->iv_len);
798 if (ret)
799 goto e_dst;
Gary R Hook36cf5152017-03-15 13:21:01 -0500800
801 ret = ccp_copy_to_sb(cmd_q, &ctx, op.jobid, op.sb_ctx,
802 CCP_PASSTHRU_BYTESWAP_256BIT);
803 if (ret) {
804 cmd->engine_error = cmd_q->cmd_error;
805 goto e_dst;
806 }
807
808 /* Step 4: Concatenate the lengths of the AAD and source, and
809 * hash that 16 byte buffer.
810 */
811 ret = ccp_init_dm_workarea(&final_wa, cmd_q, AES_BLOCK_SIZE,
812 DMA_BIDIRECTIONAL);
813 if (ret)
814 goto e_dst;
815 final = (unsigned long long *) final_wa.address;
816 final[0] = cpu_to_be64(aes->aad_len * 8);
817 final[1] = cpu_to_be64(ilen * 8);
818
Hook, Gary20e833d2019-07-10 00:09:22 +0000819 memset(&op, 0, sizeof(op));
820 op.cmd_q = cmd_q;
821 op.jobid = jobid;
822 op.sb_key = cmd_q->sb_key; /* Pre-allocated */
823 op.sb_ctx = cmd_q->sb_ctx; /* Pre-allocated */
824 op.init = 1;
825 op.u.aes.type = aes->type;
Gary R Hook36cf5152017-03-15 13:21:01 -0500826 op.u.aes.mode = CCP_AES_MODE_GHASH;
827 op.u.aes.action = CCP_AES_GHASHFINAL;
828 op.src.type = CCP_MEMTYPE_SYSTEM;
829 op.src.u.dma.address = final_wa.dma.address;
830 op.src.u.dma.length = AES_BLOCK_SIZE;
831 op.dst.type = CCP_MEMTYPE_SYSTEM;
832 op.dst.u.dma.address = final_wa.dma.address;
833 op.dst.u.dma.length = AES_BLOCK_SIZE;
834 op.eom = 1;
835 op.u.aes.size = 0;
836 ret = cmd_q->ccp->vdata->perform->aes(&op);
837 if (ret)
838 goto e_dst;
839
840 if (aes->action == CCP_AES_ACTION_ENCRYPT) {
841 /* Put the ciphered tag after the ciphertext. */
842 ccp_get_dm_area(&final_wa, 0, p_tag, 0, AES_BLOCK_SIZE);
843 } else {
844 /* Does this ciphered tag match the input? */
845 ret = ccp_init_dm_workarea(&tag, cmd_q, AES_BLOCK_SIZE,
846 DMA_BIDIRECTIONAL);
847 if (ret)
848 goto e_tag;
Gary R Hookb698a9f2018-03-07 11:31:14 -0600849 ret = ccp_set_dm_area(&tag, 0, p_tag, 0, AES_BLOCK_SIZE);
850 if (ret)
851 goto e_tag;
Gary R Hook36cf5152017-03-15 13:21:01 -0500852
Cfir Cohen538a5a072019-07-02 10:32:56 -0700853 ret = crypto_memneq(tag.address, final_wa.address,
854 AES_BLOCK_SIZE) ? -EBADMSG : 0;
Gary R Hook36cf5152017-03-15 13:21:01 -0500855 ccp_dm_free(&tag);
856 }
857
858e_tag:
859 ccp_dm_free(&final_wa);
860
861e_dst:
Gary R Hook25e443382019-07-30 16:05:22 +0000862 if (ilen > 0 && !in_place)
Gary R Hook36cf5152017-03-15 13:21:01 -0500863 ccp_free_data(&dst, cmd_q);
864
865e_src:
Gary R Hook25e443382019-07-30 16:05:22 +0000866 if (ilen > 0)
Gary R Hook36cf5152017-03-15 13:21:01 -0500867 ccp_free_data(&src, cmd_q);
868
869e_aad:
870 if (aes->aad_len)
871 ccp_free_data(&aad, cmd_q);
872
873e_ctx:
874 ccp_dm_free(&ctx);
875
876e_key:
877 ccp_dm_free(&key);
878
879 return ret;
880}
881
Tom Lendacky63b94502013-11-12 11:46:16 -0600882static int ccp_run_aes_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd)
883{
884 struct ccp_aes_engine *aes = &cmd->u.aes;
885 struct ccp_dm_workarea key, ctx;
886 struct ccp_data src, dst;
887 struct ccp_op op;
888 unsigned int dm_offset;
889 bool in_place = false;
890 int ret;
891
892 if (aes->mode == CCP_AES_MODE_CMAC)
893 return ccp_run_aes_cmac_cmd(cmd_q, cmd);
894
Gary R Hook36cf5152017-03-15 13:21:01 -0500895 if (aes->mode == CCP_AES_MODE_GCM)
896 return ccp_run_aes_gcm_cmd(cmd_q, cmd);
897
Tom Lendacky63b94502013-11-12 11:46:16 -0600898 if (!((aes->key_len == AES_KEYSIZE_128) ||
899 (aes->key_len == AES_KEYSIZE_192) ||
900 (aes->key_len == AES_KEYSIZE_256)))
901 return -EINVAL;
902
903 if (((aes->mode == CCP_AES_MODE_ECB) ||
Hook, Gary499df962019-05-14 21:53:16 +0000904 (aes->mode == CCP_AES_MODE_CBC)) &&
Tom Lendacky63b94502013-11-12 11:46:16 -0600905 (aes->src_len & (AES_BLOCK_SIZE - 1)))
906 return -EINVAL;
907
908 if (!aes->key || !aes->src || !aes->dst)
909 return -EINVAL;
910
911 if (aes->mode != CCP_AES_MODE_ECB) {
912 if (aes->iv_len != AES_BLOCK_SIZE)
913 return -EINVAL;
914
915 if (!aes->iv)
916 return -EINVAL;
917 }
918
Gary R Hook956ee212016-07-26 19:09:40 -0500919 BUILD_BUG_ON(CCP_AES_KEY_SB_COUNT != 1);
920 BUILD_BUG_ON(CCP_AES_CTX_SB_COUNT != 1);
Tom Lendacky63b94502013-11-12 11:46:16 -0600921
922 ret = -EIO;
923 memset(&op, 0, sizeof(op));
924 op.cmd_q = cmd_q;
Gary R Hook4b394a22016-07-26 19:10:21 -0500925 op.jobid = CCP_NEW_JOBID(cmd_q->ccp);
Gary R Hook956ee212016-07-26 19:09:40 -0500926 op.sb_key = cmd_q->sb_key;
927 op.sb_ctx = cmd_q->sb_ctx;
Tom Lendacky63b94502013-11-12 11:46:16 -0600928 op.init = (aes->mode == CCP_AES_MODE_ECB) ? 0 : 1;
929 op.u.aes.type = aes->type;
930 op.u.aes.mode = aes->mode;
931 op.u.aes.action = aes->action;
932
Gary R Hook956ee212016-07-26 19:09:40 -0500933 /* All supported key sizes fit in a single (32-byte) SB entry
Tom Lendacky63b94502013-11-12 11:46:16 -0600934 * and must be in little endian format. Use the 256-bit byte
935 * swap passthru option to convert from big endian to little
936 * endian.
937 */
938 ret = ccp_init_dm_workarea(&key, cmd_q,
Gary R Hook956ee212016-07-26 19:09:40 -0500939 CCP_AES_KEY_SB_COUNT * CCP_SB_BYTES,
Tom Lendacky63b94502013-11-12 11:46:16 -0600940 DMA_TO_DEVICE);
941 if (ret)
942 return ret;
943
Gary R Hook956ee212016-07-26 19:09:40 -0500944 dm_offset = CCP_SB_BYTES - aes->key_len;
Gary R Hookb698a9f2018-03-07 11:31:14 -0600945 ret = ccp_set_dm_area(&key, dm_offset, aes->key, 0, aes->key_len);
946 if (ret)
947 goto e_key;
Gary R Hook956ee212016-07-26 19:09:40 -0500948 ret = ccp_copy_to_sb(cmd_q, &key, op.jobid, op.sb_key,
949 CCP_PASSTHRU_BYTESWAP_256BIT);
Tom Lendacky63b94502013-11-12 11:46:16 -0600950 if (ret) {
951 cmd->engine_error = cmd_q->cmd_error;
952 goto e_key;
953 }
954
Gary R Hook956ee212016-07-26 19:09:40 -0500955 /* The AES context fits in a single (32-byte) SB entry and
Tom Lendacky63b94502013-11-12 11:46:16 -0600956 * must be in little endian format. Use the 256-bit byte swap
957 * passthru option to convert from big endian to little endian.
958 */
959 ret = ccp_init_dm_workarea(&ctx, cmd_q,
Gary R Hook956ee212016-07-26 19:09:40 -0500960 CCP_AES_CTX_SB_COUNT * CCP_SB_BYTES,
Tom Lendacky63b94502013-11-12 11:46:16 -0600961 DMA_BIDIRECTIONAL);
962 if (ret)
963 goto e_key;
964
965 if (aes->mode != CCP_AES_MODE_ECB) {
Gary R Hook4b394a22016-07-26 19:10:21 -0500966 /* Load the AES context - convert to LE */
Gary R Hook956ee212016-07-26 19:09:40 -0500967 dm_offset = CCP_SB_BYTES - AES_BLOCK_SIZE;
Gary R Hookb698a9f2018-03-07 11:31:14 -0600968 ret = ccp_set_dm_area(&ctx, dm_offset, aes->iv, 0, aes->iv_len);
969 if (ret)
970 goto e_ctx;
Gary R Hook956ee212016-07-26 19:09:40 -0500971 ret = ccp_copy_to_sb(cmd_q, &ctx, op.jobid, op.sb_ctx,
972 CCP_PASSTHRU_BYTESWAP_256BIT);
Tom Lendacky63b94502013-11-12 11:46:16 -0600973 if (ret) {
974 cmd->engine_error = cmd_q->cmd_error;
975 goto e_ctx;
976 }
977 }
Gary R Hookf7cc02b32017-02-08 13:07:06 -0600978 switch (aes->mode) {
979 case CCP_AES_MODE_CFB: /* CFB128 only */
980 case CCP_AES_MODE_CTR:
981 op.u.aes.size = AES_BLOCK_SIZE * BITS_PER_BYTE - 1;
982 break;
983 default:
984 op.u.aes.size = 0;
985 }
Tom Lendacky63b94502013-11-12 11:46:16 -0600986
987 /* Prepare the input and output data workareas. For in-place
988 * operations we need to set the dma direction to BIDIRECTIONAL
989 * and copy the src workarea to the dst workarea.
990 */
991 if (sg_virt(aes->src) == sg_virt(aes->dst))
992 in_place = true;
993
994 ret = ccp_init_data(&src, cmd_q, aes->src, aes->src_len,
995 AES_BLOCK_SIZE,
996 in_place ? DMA_BIDIRECTIONAL : DMA_TO_DEVICE);
997 if (ret)
998 goto e_ctx;
999
Tom Lendacky8db88462015-02-03 13:07:05 -06001000 if (in_place) {
Tom Lendacky63b94502013-11-12 11:46:16 -06001001 dst = src;
Tom Lendacky8db88462015-02-03 13:07:05 -06001002 } else {
Tom Lendacky63b94502013-11-12 11:46:16 -06001003 ret = ccp_init_data(&dst, cmd_q, aes->dst, aes->src_len,
1004 AES_BLOCK_SIZE, DMA_FROM_DEVICE);
1005 if (ret)
1006 goto e_src;
1007 }
1008
1009 /* Send data to the CCP AES engine */
1010 while (src.sg_wa.bytes_left) {
1011 ccp_prepare_data(&src, &dst, &op, AES_BLOCK_SIZE, true);
1012 if (!src.sg_wa.bytes_left) {
1013 op.eom = 1;
1014
1015 /* Since we don't retrieve the AES context in ECB
1016 * mode we have to wait for the operation to complete
1017 * on the last piece of data
1018 */
1019 if (aes->mode == CCP_AES_MODE_ECB)
1020 op.soc = 1;
1021 }
1022
Gary R Hooka43eb982016-07-26 19:09:31 -05001023 ret = cmd_q->ccp->vdata->perform->aes(&op);
Tom Lendacky63b94502013-11-12 11:46:16 -06001024 if (ret) {
1025 cmd->engine_error = cmd_q->cmd_error;
1026 goto e_dst;
1027 }
1028
1029 ccp_process_data(&src, &dst, &op);
1030 }
1031
1032 if (aes->mode != CCP_AES_MODE_ECB) {
1033 /* Retrieve the AES context - convert from LE to BE using
1034 * 32-byte (256-bit) byteswapping
1035 */
Gary R Hook956ee212016-07-26 19:09:40 -05001036 ret = ccp_copy_from_sb(cmd_q, &ctx, op.jobid, op.sb_ctx,
1037 CCP_PASSTHRU_BYTESWAP_256BIT);
Tom Lendacky63b94502013-11-12 11:46:16 -06001038 if (ret) {
1039 cmd->engine_error = cmd_q->cmd_error;
1040 goto e_dst;
1041 }
1042
1043 /* ...but we only need AES_BLOCK_SIZE bytes */
Gary R Hook956ee212016-07-26 19:09:40 -05001044 dm_offset = CCP_SB_BYTES - AES_BLOCK_SIZE;
Tom Lendacky63b94502013-11-12 11:46:16 -06001045 ccp_get_dm_area(&ctx, dm_offset, aes->iv, 0, aes->iv_len);
1046 }
1047
1048e_dst:
1049 if (!in_place)
1050 ccp_free_data(&dst, cmd_q);
1051
1052e_src:
1053 ccp_free_data(&src, cmd_q);
1054
1055e_ctx:
1056 ccp_dm_free(&ctx);
1057
1058e_key:
1059 ccp_dm_free(&key);
1060
1061 return ret;
1062}
1063
1064static int ccp_run_xts_aes_cmd(struct ccp_cmd_queue *cmd_q,
1065 struct ccp_cmd *cmd)
1066{
1067 struct ccp_xts_aes_engine *xts = &cmd->u.xts;
1068 struct ccp_dm_workarea key, ctx;
1069 struct ccp_data src, dst;
1070 struct ccp_op op;
1071 unsigned int unit_size, dm_offset;
1072 bool in_place = false;
Gary R Hooke6523992017-07-25 14:12:11 -05001073 unsigned int sb_count;
1074 enum ccp_aes_type aestype;
Tom Lendacky63b94502013-11-12 11:46:16 -06001075 int ret;
1076
1077 switch (xts->unit_size) {
1078 case CCP_XTS_AES_UNIT_SIZE_16:
1079 unit_size = 16;
1080 break;
1081 case CCP_XTS_AES_UNIT_SIZE_512:
1082 unit_size = 512;
1083 break;
1084 case CCP_XTS_AES_UNIT_SIZE_1024:
1085 unit_size = 1024;
1086 break;
1087 case CCP_XTS_AES_UNIT_SIZE_2048:
1088 unit_size = 2048;
1089 break;
1090 case CCP_XTS_AES_UNIT_SIZE_4096:
1091 unit_size = 4096;
1092 break;
1093
1094 default:
1095 return -EINVAL;
1096 }
1097
Gary R Hooke6523992017-07-25 14:12:11 -05001098 if (xts->key_len == AES_KEYSIZE_128)
1099 aestype = CCP_AES_TYPE_128;
Gary R Hook5060ffc2017-07-25 14:21:43 -05001100 else if (xts->key_len == AES_KEYSIZE_256)
1101 aestype = CCP_AES_TYPE_256;
Gary R Hooke6523992017-07-25 14:12:11 -05001102 else
Tom Lendacky63b94502013-11-12 11:46:16 -06001103 return -EINVAL;
1104
1105 if (!xts->final && (xts->src_len & (AES_BLOCK_SIZE - 1)))
1106 return -EINVAL;
1107
1108 if (xts->iv_len != AES_BLOCK_SIZE)
1109 return -EINVAL;
1110
1111 if (!xts->key || !xts->iv || !xts->src || !xts->dst)
1112 return -EINVAL;
1113
Gary R Hook956ee212016-07-26 19:09:40 -05001114 BUILD_BUG_ON(CCP_XTS_AES_KEY_SB_COUNT != 1);
1115 BUILD_BUG_ON(CCP_XTS_AES_CTX_SB_COUNT != 1);
Tom Lendacky63b94502013-11-12 11:46:16 -06001116
1117 ret = -EIO;
1118 memset(&op, 0, sizeof(op));
1119 op.cmd_q = cmd_q;
Gary R Hook4b394a22016-07-26 19:10:21 -05001120 op.jobid = CCP_NEW_JOBID(cmd_q->ccp);
Gary R Hook956ee212016-07-26 19:09:40 -05001121 op.sb_key = cmd_q->sb_key;
1122 op.sb_ctx = cmd_q->sb_ctx;
Tom Lendacky63b94502013-11-12 11:46:16 -06001123 op.init = 1;
Gary R Hooke6523992017-07-25 14:12:11 -05001124 op.u.xts.type = aestype;
Tom Lendacky63b94502013-11-12 11:46:16 -06001125 op.u.xts.action = xts->action;
1126 op.u.xts.unit_size = xts->unit_size;
1127
Gary R Hooke6523992017-07-25 14:12:11 -05001128 /* A version 3 device only supports 128-bit keys, which fits into a
1129 * single SB entry. A version 5 device uses a 512-bit vector, so two
1130 * SB entries.
Tom Lendacky63b94502013-11-12 11:46:16 -06001131 */
Gary R Hooke6523992017-07-25 14:12:11 -05001132 if (cmd_q->ccp->vdata->version == CCP_VERSION(3, 0))
1133 sb_count = CCP_XTS_AES_KEY_SB_COUNT;
1134 else
1135 sb_count = CCP5_XTS_AES_KEY_SB_COUNT;
Tom Lendacky63b94502013-11-12 11:46:16 -06001136 ret = ccp_init_dm_workarea(&key, cmd_q,
Gary R Hooke6523992017-07-25 14:12:11 -05001137 sb_count * CCP_SB_BYTES,
Tom Lendacky63b94502013-11-12 11:46:16 -06001138 DMA_TO_DEVICE);
1139 if (ret)
1140 return ret;
1141
Gary R Hooke6523992017-07-25 14:12:11 -05001142 if (cmd_q->ccp->vdata->version == CCP_VERSION(3, 0)) {
1143 /* All supported key sizes must be in little endian format.
1144 * Use the 256-bit byte swap passthru option to convert from
1145 * big endian to little endian.
1146 */
1147 dm_offset = CCP_SB_BYTES - AES_KEYSIZE_128;
Gary R Hookb698a9f2018-03-07 11:31:14 -06001148 ret = ccp_set_dm_area(&key, dm_offset, xts->key, 0, xts->key_len);
1149 if (ret)
1150 goto e_key;
1151 ret = ccp_set_dm_area(&key, 0, xts->key, xts->key_len, xts->key_len);
1152 if (ret)
1153 goto e_key;
Gary R Hooke6523992017-07-25 14:12:11 -05001154 } else {
1155 /* Version 5 CCPs use a 512-bit space for the key: each portion
1156 * occupies 256 bits, or one entire slot, and is zero-padded.
1157 */
1158 unsigned int pad;
1159
1160 dm_offset = CCP_SB_BYTES;
1161 pad = dm_offset - xts->key_len;
Gary R Hookb698a9f2018-03-07 11:31:14 -06001162 ret = ccp_set_dm_area(&key, pad, xts->key, 0, xts->key_len);
1163 if (ret)
1164 goto e_key;
1165 ret = ccp_set_dm_area(&key, dm_offset + pad, xts->key,
1166 xts->key_len, xts->key_len);
1167 if (ret)
1168 goto e_key;
Gary R Hooke6523992017-07-25 14:12:11 -05001169 }
Gary R Hook956ee212016-07-26 19:09:40 -05001170 ret = ccp_copy_to_sb(cmd_q, &key, op.jobid, op.sb_key,
1171 CCP_PASSTHRU_BYTESWAP_256BIT);
Tom Lendacky63b94502013-11-12 11:46:16 -06001172 if (ret) {
1173 cmd->engine_error = cmd_q->cmd_error;
1174 goto e_key;
1175 }
1176
Gary R Hook956ee212016-07-26 19:09:40 -05001177 /* The AES context fits in a single (32-byte) SB entry and
Tom Lendacky63b94502013-11-12 11:46:16 -06001178 * for XTS is already in little endian format so no byte swapping
1179 * is needed.
1180 */
1181 ret = ccp_init_dm_workarea(&ctx, cmd_q,
Gary R Hook956ee212016-07-26 19:09:40 -05001182 CCP_XTS_AES_CTX_SB_COUNT * CCP_SB_BYTES,
Tom Lendacky63b94502013-11-12 11:46:16 -06001183 DMA_BIDIRECTIONAL);
1184 if (ret)
1185 goto e_key;
1186
Gary R Hookb698a9f2018-03-07 11:31:14 -06001187 ret = ccp_set_dm_area(&ctx, 0, xts->iv, 0, xts->iv_len);
1188 if (ret)
1189 goto e_ctx;
Gary R Hook956ee212016-07-26 19:09:40 -05001190 ret = ccp_copy_to_sb(cmd_q, &ctx, op.jobid, op.sb_ctx,
1191 CCP_PASSTHRU_BYTESWAP_NOOP);
Tom Lendacky63b94502013-11-12 11:46:16 -06001192 if (ret) {
1193 cmd->engine_error = cmd_q->cmd_error;
1194 goto e_ctx;
1195 }
1196
1197 /* Prepare the input and output data workareas. For in-place
1198 * operations we need to set the dma direction to BIDIRECTIONAL
1199 * and copy the src workarea to the dst workarea.
1200 */
1201 if (sg_virt(xts->src) == sg_virt(xts->dst))
1202 in_place = true;
1203
1204 ret = ccp_init_data(&src, cmd_q, xts->src, xts->src_len,
1205 unit_size,
1206 in_place ? DMA_BIDIRECTIONAL : DMA_TO_DEVICE);
1207 if (ret)
1208 goto e_ctx;
1209
Tom Lendacky8db88462015-02-03 13:07:05 -06001210 if (in_place) {
Tom Lendacky63b94502013-11-12 11:46:16 -06001211 dst = src;
Tom Lendacky8db88462015-02-03 13:07:05 -06001212 } else {
Tom Lendacky63b94502013-11-12 11:46:16 -06001213 ret = ccp_init_data(&dst, cmd_q, xts->dst, xts->src_len,
1214 unit_size, DMA_FROM_DEVICE);
1215 if (ret)
1216 goto e_src;
1217 }
1218
1219 /* Send data to the CCP AES engine */
1220 while (src.sg_wa.bytes_left) {
1221 ccp_prepare_data(&src, &dst, &op, unit_size, true);
1222 if (!src.sg_wa.bytes_left)
1223 op.eom = 1;
1224
Gary R Hooka43eb982016-07-26 19:09:31 -05001225 ret = cmd_q->ccp->vdata->perform->xts_aes(&op);
Tom Lendacky63b94502013-11-12 11:46:16 -06001226 if (ret) {
1227 cmd->engine_error = cmd_q->cmd_error;
1228 goto e_dst;
1229 }
1230
1231 ccp_process_data(&src, &dst, &op);
1232 }
1233
1234 /* Retrieve the AES context - convert from LE to BE using
1235 * 32-byte (256-bit) byteswapping
1236 */
Gary R Hook956ee212016-07-26 19:09:40 -05001237 ret = ccp_copy_from_sb(cmd_q, &ctx, op.jobid, op.sb_ctx,
1238 CCP_PASSTHRU_BYTESWAP_256BIT);
Tom Lendacky63b94502013-11-12 11:46:16 -06001239 if (ret) {
1240 cmd->engine_error = cmd_q->cmd_error;
1241 goto e_dst;
1242 }
1243
1244 /* ...but we only need AES_BLOCK_SIZE bytes */
Gary R Hook956ee212016-07-26 19:09:40 -05001245 dm_offset = CCP_SB_BYTES - AES_BLOCK_SIZE;
Tom Lendacky63b94502013-11-12 11:46:16 -06001246 ccp_get_dm_area(&ctx, dm_offset, xts->iv, 0, xts->iv_len);
1247
1248e_dst:
1249 if (!in_place)
1250 ccp_free_data(&dst, cmd_q);
1251
1252e_src:
1253 ccp_free_data(&src, cmd_q);
1254
1255e_ctx:
1256 ccp_dm_free(&ctx);
1257
1258e_key:
1259 ccp_dm_free(&key);
1260
1261 return ret;
1262}
1263
Gary R Hook990672d2017-03-15 13:20:52 -05001264static int ccp_run_des3_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd)
1265{
1266 struct ccp_des3_engine *des3 = &cmd->u.des3;
1267
1268 struct ccp_dm_workarea key, ctx;
1269 struct ccp_data src, dst;
1270 struct ccp_op op;
1271 unsigned int dm_offset;
1272 unsigned int len_singlekey;
1273 bool in_place = false;
1274 int ret;
1275
1276 /* Error checks */
Hook, Gary89646fd2019-05-14 21:53:30 +00001277 if (cmd_q->ccp->vdata->version < CCP_VERSION(5, 0))
1278 return -EINVAL;
1279
Gary R Hook990672d2017-03-15 13:20:52 -05001280 if (!cmd_q->ccp->vdata->perform->des3)
1281 return -EINVAL;
1282
1283 if (des3->key_len != DES3_EDE_KEY_SIZE)
1284 return -EINVAL;
1285
1286 if (((des3->mode == CCP_DES3_MODE_ECB) ||
1287 (des3->mode == CCP_DES3_MODE_CBC)) &&
1288 (des3->src_len & (DES3_EDE_BLOCK_SIZE - 1)))
1289 return -EINVAL;
1290
1291 if (!des3->key || !des3->src || !des3->dst)
1292 return -EINVAL;
1293
1294 if (des3->mode != CCP_DES3_MODE_ECB) {
1295 if (des3->iv_len != DES3_EDE_BLOCK_SIZE)
1296 return -EINVAL;
1297
1298 if (!des3->iv)
1299 return -EINVAL;
1300 }
1301
1302 ret = -EIO;
1303 /* Zero out all the fields of the command desc */
1304 memset(&op, 0, sizeof(op));
1305
1306 /* Set up the Function field */
1307 op.cmd_q = cmd_q;
1308 op.jobid = CCP_NEW_JOBID(cmd_q->ccp);
1309 op.sb_key = cmd_q->sb_key;
1310
1311 op.init = (des3->mode == CCP_DES3_MODE_ECB) ? 0 : 1;
1312 op.u.des3.type = des3->type;
1313 op.u.des3.mode = des3->mode;
1314 op.u.des3.action = des3->action;
1315
1316 /*
1317 * All supported key sizes fit in a single (32-byte) KSB entry and
1318 * (like AES) must be in little endian format. Use the 256-bit byte
1319 * swap passthru option to convert from big endian to little endian.
1320 */
1321 ret = ccp_init_dm_workarea(&key, cmd_q,
1322 CCP_DES3_KEY_SB_COUNT * CCP_SB_BYTES,
1323 DMA_TO_DEVICE);
1324 if (ret)
1325 return ret;
1326
1327 /*
1328 * The contents of the key triplet are in the reverse order of what
1329 * is required by the engine. Copy the 3 pieces individually to put
1330 * them where they belong.
1331 */
1332 dm_offset = CCP_SB_BYTES - des3->key_len; /* Basic offset */
1333
1334 len_singlekey = des3->key_len / 3;
Gary R Hookb698a9f2018-03-07 11:31:14 -06001335 ret = ccp_set_dm_area(&key, dm_offset + 2 * len_singlekey,
1336 des3->key, 0, len_singlekey);
1337 if (ret)
1338 goto e_key;
1339 ret = ccp_set_dm_area(&key, dm_offset + len_singlekey,
1340 des3->key, len_singlekey, len_singlekey);
1341 if (ret)
1342 goto e_key;
1343 ret = ccp_set_dm_area(&key, dm_offset,
1344 des3->key, 2 * len_singlekey, len_singlekey);
1345 if (ret)
1346 goto e_key;
Gary R Hook990672d2017-03-15 13:20:52 -05001347
1348 /* Copy the key to the SB */
1349 ret = ccp_copy_to_sb(cmd_q, &key, op.jobid, op.sb_key,
1350 CCP_PASSTHRU_BYTESWAP_256BIT);
1351 if (ret) {
1352 cmd->engine_error = cmd_q->cmd_error;
1353 goto e_key;
1354 }
1355
1356 /*
1357 * The DES3 context fits in a single (32-byte) KSB entry and
1358 * must be in little endian format. Use the 256-bit byte swap
1359 * passthru option to convert from big endian to little endian.
1360 */
1361 if (des3->mode != CCP_DES3_MODE_ECB) {
Gary R Hook990672d2017-03-15 13:20:52 -05001362 op.sb_ctx = cmd_q->sb_ctx;
1363
1364 ret = ccp_init_dm_workarea(&ctx, cmd_q,
1365 CCP_DES3_CTX_SB_COUNT * CCP_SB_BYTES,
1366 DMA_BIDIRECTIONAL);
1367 if (ret)
1368 goto e_key;
1369
1370 /* Load the context into the LSB */
1371 dm_offset = CCP_SB_BYTES - des3->iv_len;
Gary R Hookb698a9f2018-03-07 11:31:14 -06001372 ret = ccp_set_dm_area(&ctx, dm_offset, des3->iv, 0,
1373 des3->iv_len);
1374 if (ret)
1375 goto e_ctx;
Gary R Hook990672d2017-03-15 13:20:52 -05001376
Gary R Hook990672d2017-03-15 13:20:52 -05001377 ret = ccp_copy_to_sb(cmd_q, &ctx, op.jobid, op.sb_ctx,
Hook, Gary89646fd2019-05-14 21:53:30 +00001378 CCP_PASSTHRU_BYTESWAP_256BIT);
Gary R Hook990672d2017-03-15 13:20:52 -05001379 if (ret) {
1380 cmd->engine_error = cmd_q->cmd_error;
1381 goto e_ctx;
1382 }
1383 }
1384
1385 /*
1386 * Prepare the input and output data workareas. For in-place
1387 * operations we need to set the dma direction to BIDIRECTIONAL
1388 * and copy the src workarea to the dst workarea.
1389 */
1390 if (sg_virt(des3->src) == sg_virt(des3->dst))
1391 in_place = true;
1392
1393 ret = ccp_init_data(&src, cmd_q, des3->src, des3->src_len,
1394 DES3_EDE_BLOCK_SIZE,
1395 in_place ? DMA_BIDIRECTIONAL : DMA_TO_DEVICE);
1396 if (ret)
1397 goto e_ctx;
1398
1399 if (in_place)
1400 dst = src;
1401 else {
1402 ret = ccp_init_data(&dst, cmd_q, des3->dst, des3->src_len,
1403 DES3_EDE_BLOCK_SIZE, DMA_FROM_DEVICE);
1404 if (ret)
1405 goto e_src;
1406 }
1407
1408 /* Send data to the CCP DES3 engine */
1409 while (src.sg_wa.bytes_left) {
1410 ccp_prepare_data(&src, &dst, &op, DES3_EDE_BLOCK_SIZE, true);
1411 if (!src.sg_wa.bytes_left) {
1412 op.eom = 1;
1413
1414 /* Since we don't retrieve the context in ECB mode
1415 * we have to wait for the operation to complete
1416 * on the last piece of data
1417 */
1418 op.soc = 0;
1419 }
1420
1421 ret = cmd_q->ccp->vdata->perform->des3(&op);
1422 if (ret) {
1423 cmd->engine_error = cmd_q->cmd_error;
1424 goto e_dst;
1425 }
1426
1427 ccp_process_data(&src, &dst, &op);
1428 }
1429
1430 if (des3->mode != CCP_DES3_MODE_ECB) {
1431 /* Retrieve the context and make BE */
1432 ret = ccp_copy_from_sb(cmd_q, &ctx, op.jobid, op.sb_ctx,
1433 CCP_PASSTHRU_BYTESWAP_256BIT);
1434 if (ret) {
1435 cmd->engine_error = cmd_q->cmd_error;
1436 goto e_dst;
1437 }
1438
1439 /* ...but we only need the last DES3_EDE_BLOCK_SIZE bytes */
Gary R Hook990672d2017-03-15 13:20:52 -05001440 ccp_get_dm_area(&ctx, dm_offset, des3->iv, 0,
1441 DES3_EDE_BLOCK_SIZE);
1442 }
1443e_dst:
1444 if (!in_place)
1445 ccp_free_data(&dst, cmd_q);
1446
1447e_src:
1448 ccp_free_data(&src, cmd_q);
1449
1450e_ctx:
1451 if (des3->mode != CCP_DES3_MODE_ECB)
1452 ccp_dm_free(&ctx);
1453
1454e_key:
1455 ccp_dm_free(&key);
1456
1457 return ret;
1458}
1459
Tom Lendacky63b94502013-11-12 11:46:16 -06001460static int ccp_run_sha_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd)
1461{
1462 struct ccp_sha_engine *sha = &cmd->u.sha;
1463 struct ccp_dm_workarea ctx;
1464 struct ccp_data src;
1465 struct ccp_op op;
Gary R Hook4b394a22016-07-26 19:10:21 -05001466 unsigned int ioffset, ooffset;
1467 unsigned int digest_size;
1468 int sb_count;
1469 const void *init;
1470 u64 block_size;
1471 int ctx_size;
Tom Lendacky63b94502013-11-12 11:46:16 -06001472 int ret;
1473
Gary R Hook4b394a22016-07-26 19:10:21 -05001474 switch (sha->type) {
1475 case CCP_SHA_TYPE_1:
1476 if (sha->ctx_len < SHA1_DIGEST_SIZE)
1477 return -EINVAL;
1478 block_size = SHA1_BLOCK_SIZE;
1479 break;
1480 case CCP_SHA_TYPE_224:
1481 if (sha->ctx_len < SHA224_DIGEST_SIZE)
1482 return -EINVAL;
1483 block_size = SHA224_BLOCK_SIZE;
1484 break;
1485 case CCP_SHA_TYPE_256:
1486 if (sha->ctx_len < SHA256_DIGEST_SIZE)
1487 return -EINVAL;
1488 block_size = SHA256_BLOCK_SIZE;
1489 break;
Gary R Hookccebcf32017-03-15 13:20:43 -05001490 case CCP_SHA_TYPE_384:
1491 if (cmd_q->ccp->vdata->version < CCP_VERSION(4, 0)
1492 || sha->ctx_len < SHA384_DIGEST_SIZE)
1493 return -EINVAL;
1494 block_size = SHA384_BLOCK_SIZE;
1495 break;
1496 case CCP_SHA_TYPE_512:
1497 if (cmd_q->ccp->vdata->version < CCP_VERSION(4, 0)
1498 || sha->ctx_len < SHA512_DIGEST_SIZE)
1499 return -EINVAL;
1500 block_size = SHA512_BLOCK_SIZE;
1501 break;
Gary R Hook4b394a22016-07-26 19:10:21 -05001502 default:
Tom Lendacky63b94502013-11-12 11:46:16 -06001503 return -EINVAL;
Gary R Hook4b394a22016-07-26 19:10:21 -05001504 }
Tom Lendacky63b94502013-11-12 11:46:16 -06001505
1506 if (!sha->ctx)
1507 return -EINVAL;
1508
Gary R Hook4b394a22016-07-26 19:10:21 -05001509 if (!sha->final && (sha->src_len & (block_size - 1)))
Tom Lendacky63b94502013-11-12 11:46:16 -06001510 return -EINVAL;
1511
Gary R Hook4b394a22016-07-26 19:10:21 -05001512 /* The version 3 device can't handle zero-length input */
1513 if (cmd_q->ccp->vdata->version == CCP_VERSION(3, 0)) {
Tom Lendacky63b94502013-11-12 11:46:16 -06001514
Gary R Hook4b394a22016-07-26 19:10:21 -05001515 if (!sha->src_len) {
1516 unsigned int digest_len;
1517 const u8 *sha_zero;
1518
1519 /* Not final, just return */
1520 if (!sha->final)
1521 return 0;
1522
1523 /* CCP can't do a zero length sha operation so the
1524 * caller must buffer the data.
1525 */
1526 if (sha->msg_bits)
1527 return -EINVAL;
1528
1529 /* The CCP cannot perform zero-length sha operations
1530 * so the caller is required to buffer data for the
1531 * final operation. However, a sha operation for a
1532 * message with a total length of zero is valid so
1533 * known values are required to supply the result.
1534 */
1535 switch (sha->type) {
1536 case CCP_SHA_TYPE_1:
1537 sha_zero = sha1_zero_message_hash;
1538 digest_len = SHA1_DIGEST_SIZE;
1539 break;
1540 case CCP_SHA_TYPE_224:
1541 sha_zero = sha224_zero_message_hash;
1542 digest_len = SHA224_DIGEST_SIZE;
1543 break;
1544 case CCP_SHA_TYPE_256:
1545 sha_zero = sha256_zero_message_hash;
1546 digest_len = SHA256_DIGEST_SIZE;
1547 break;
1548 default:
1549 return -EINVAL;
1550 }
1551
1552 scatterwalk_map_and_copy((void *)sha_zero, sha->ctx, 0,
1553 digest_len, 1);
1554
Tom Lendacky63b94502013-11-12 11:46:16 -06001555 return 0;
Tom Lendacky63b94502013-11-12 11:46:16 -06001556 }
Tom Lendacky63b94502013-11-12 11:46:16 -06001557 }
1558
Gary R Hook4b394a22016-07-26 19:10:21 -05001559 /* Set variables used throughout */
1560 switch (sha->type) {
1561 case CCP_SHA_TYPE_1:
1562 digest_size = SHA1_DIGEST_SIZE;
1563 init = (void *) ccp_sha1_init;
1564 ctx_size = SHA1_DIGEST_SIZE;
1565 sb_count = 1;
1566 if (cmd_q->ccp->vdata->version != CCP_VERSION(3, 0))
1567 ooffset = ioffset = CCP_SB_BYTES - SHA1_DIGEST_SIZE;
1568 else
1569 ooffset = ioffset = 0;
1570 break;
1571 case CCP_SHA_TYPE_224:
1572 digest_size = SHA224_DIGEST_SIZE;
1573 init = (void *) ccp_sha224_init;
1574 ctx_size = SHA256_DIGEST_SIZE;
1575 sb_count = 1;
1576 ioffset = 0;
1577 if (cmd_q->ccp->vdata->version != CCP_VERSION(3, 0))
1578 ooffset = CCP_SB_BYTES - SHA224_DIGEST_SIZE;
1579 else
1580 ooffset = 0;
1581 break;
1582 case CCP_SHA_TYPE_256:
1583 digest_size = SHA256_DIGEST_SIZE;
1584 init = (void *) ccp_sha256_init;
1585 ctx_size = SHA256_DIGEST_SIZE;
1586 sb_count = 1;
1587 ooffset = ioffset = 0;
1588 break;
Gary R Hookccebcf32017-03-15 13:20:43 -05001589 case CCP_SHA_TYPE_384:
1590 digest_size = SHA384_DIGEST_SIZE;
1591 init = (void *) ccp_sha384_init;
1592 ctx_size = SHA512_DIGEST_SIZE;
1593 sb_count = 2;
1594 ioffset = 0;
1595 ooffset = 2 * CCP_SB_BYTES - SHA384_DIGEST_SIZE;
1596 break;
1597 case CCP_SHA_TYPE_512:
1598 digest_size = SHA512_DIGEST_SIZE;
1599 init = (void *) ccp_sha512_init;
1600 ctx_size = SHA512_DIGEST_SIZE;
1601 sb_count = 2;
1602 ooffset = ioffset = 0;
1603 break;
Gary R Hook4b394a22016-07-26 19:10:21 -05001604 default:
1605 ret = -EINVAL;
1606 goto e_data;
1607 }
Tom Lendacky63b94502013-11-12 11:46:16 -06001608
Gary R Hook4b394a22016-07-26 19:10:21 -05001609 /* For zero-length plaintext the src pointer is ignored;
1610 * otherwise both parts must be valid
1611 */
1612 if (sha->src_len && !sha->src)
1613 return -EINVAL;
Tom Lendacky63b94502013-11-12 11:46:16 -06001614
1615 memset(&op, 0, sizeof(op));
1616 op.cmd_q = cmd_q;
Gary R Hook4b394a22016-07-26 19:10:21 -05001617 op.jobid = CCP_NEW_JOBID(cmd_q->ccp);
1618 op.sb_ctx = cmd_q->sb_ctx; /* Pre-allocated */
Tom Lendacky63b94502013-11-12 11:46:16 -06001619 op.u.sha.type = sha->type;
1620 op.u.sha.msg_bits = sha->msg_bits;
1621
Gary R Hookccebcf32017-03-15 13:20:43 -05001622 /* For SHA1/224/256 the context fits in a single (32-byte) SB entry;
1623 * SHA384/512 require 2 adjacent SB slots, with the right half in the
1624 * first slot, and the left half in the second. Each portion must then
1625 * be in little endian format: use the 256-bit byte swap option.
1626 */
Gary R Hook4b394a22016-07-26 19:10:21 -05001627 ret = ccp_init_dm_workarea(&ctx, cmd_q, sb_count * CCP_SB_BYTES,
Tom Lendacky63b94502013-11-12 11:46:16 -06001628 DMA_BIDIRECTIONAL);
1629 if (ret)
1630 return ret;
Tom Lendackyc11baa02014-01-24 16:18:02 -06001631 if (sha->first) {
Tom Lendackyc11baa02014-01-24 16:18:02 -06001632 switch (sha->type) {
1633 case CCP_SHA_TYPE_1:
Tom Lendackyc11baa02014-01-24 16:18:02 -06001634 case CCP_SHA_TYPE_224:
Tom Lendackyc11baa02014-01-24 16:18:02 -06001635 case CCP_SHA_TYPE_256:
Gary R Hook4b394a22016-07-26 19:10:21 -05001636 memcpy(ctx.address + ioffset, init, ctx_size);
Tom Lendackyc11baa02014-01-24 16:18:02 -06001637 break;
Gary R Hookccebcf32017-03-15 13:20:43 -05001638 case CCP_SHA_TYPE_384:
1639 case CCP_SHA_TYPE_512:
1640 memcpy(ctx.address + ctx_size / 2, init,
1641 ctx_size / 2);
1642 memcpy(ctx.address, init + ctx_size / 2,
1643 ctx_size / 2);
1644 break;
Tom Lendackyc11baa02014-01-24 16:18:02 -06001645 default:
1646 ret = -EINVAL;
1647 goto e_ctx;
1648 }
Tom Lendacky8db88462015-02-03 13:07:05 -06001649 } else {
Gary R Hook4b394a22016-07-26 19:10:21 -05001650 /* Restore the context */
Gary R Hookb698a9f2018-03-07 11:31:14 -06001651 ret = ccp_set_dm_area(&ctx, 0, sha->ctx, 0,
1652 sb_count * CCP_SB_BYTES);
1653 if (ret)
1654 goto e_ctx;
Tom Lendacky8db88462015-02-03 13:07:05 -06001655 }
Tom Lendackyc11baa02014-01-24 16:18:02 -06001656
Gary R Hook956ee212016-07-26 19:09:40 -05001657 ret = ccp_copy_to_sb(cmd_q, &ctx, op.jobid, op.sb_ctx,
1658 CCP_PASSTHRU_BYTESWAP_256BIT);
Tom Lendacky63b94502013-11-12 11:46:16 -06001659 if (ret) {
1660 cmd->engine_error = cmd_q->cmd_error;
1661 goto e_ctx;
1662 }
1663
Gary R Hook4b394a22016-07-26 19:10:21 -05001664 if (sha->src) {
1665 /* Send data to the CCP SHA engine; block_size is set above */
1666 ret = ccp_init_data(&src, cmd_q, sha->src, sha->src_len,
1667 block_size, DMA_TO_DEVICE);
1668 if (ret)
1669 goto e_ctx;
Tom Lendacky63b94502013-11-12 11:46:16 -06001670
Gary R Hook4b394a22016-07-26 19:10:21 -05001671 while (src.sg_wa.bytes_left) {
1672 ccp_prepare_data(&src, NULL, &op, block_size, false);
1673 if (sha->final && !src.sg_wa.bytes_left)
1674 op.eom = 1;
Tom Lendacky63b94502013-11-12 11:46:16 -06001675
Gary R Hook4b394a22016-07-26 19:10:21 -05001676 ret = cmd_q->ccp->vdata->perform->sha(&op);
1677 if (ret) {
1678 cmd->engine_error = cmd_q->cmd_error;
1679 goto e_data;
1680 }
1681
1682 ccp_process_data(&src, NULL, &op);
1683 }
1684 } else {
1685 op.eom = 1;
Gary R Hooka43eb982016-07-26 19:09:31 -05001686 ret = cmd_q->ccp->vdata->perform->sha(&op);
Tom Lendacky63b94502013-11-12 11:46:16 -06001687 if (ret) {
1688 cmd->engine_error = cmd_q->cmd_error;
1689 goto e_data;
1690 }
Tom Lendacky63b94502013-11-12 11:46:16 -06001691 }
1692
1693 /* Retrieve the SHA context - convert from LE to BE using
1694 * 32-byte (256-bit) byteswapping to BE
1695 */
Gary R Hook956ee212016-07-26 19:09:40 -05001696 ret = ccp_copy_from_sb(cmd_q, &ctx, op.jobid, op.sb_ctx,
1697 CCP_PASSTHRU_BYTESWAP_256BIT);
Tom Lendacky63b94502013-11-12 11:46:16 -06001698 if (ret) {
1699 cmd->engine_error = cmd_q->cmd_error;
1700 goto e_data;
1701 }
1702
Gary R Hook4b394a22016-07-26 19:10:21 -05001703 if (sha->final) {
1704 /* Finishing up, so get the digest */
1705 switch (sha->type) {
1706 case CCP_SHA_TYPE_1:
1707 case CCP_SHA_TYPE_224:
1708 case CCP_SHA_TYPE_256:
1709 ccp_get_dm_area(&ctx, ooffset,
1710 sha->ctx, 0,
1711 digest_size);
1712 break;
Gary R Hookccebcf32017-03-15 13:20:43 -05001713 case CCP_SHA_TYPE_384:
1714 case CCP_SHA_TYPE_512:
1715 ccp_get_dm_area(&ctx, 0,
1716 sha->ctx, LSB_ITEM_SIZE - ooffset,
1717 LSB_ITEM_SIZE);
1718 ccp_get_dm_area(&ctx, LSB_ITEM_SIZE + ooffset,
1719 sha->ctx, 0,
1720 LSB_ITEM_SIZE - ooffset);
1721 break;
Gary R Hook4b394a22016-07-26 19:10:21 -05001722 default:
1723 ret = -EINVAL;
1724 goto e_ctx;
1725 }
1726 } else {
1727 /* Stash the context */
1728 ccp_get_dm_area(&ctx, 0, sha->ctx, 0,
1729 sb_count * CCP_SB_BYTES);
1730 }
Tom Lendacky63b94502013-11-12 11:46:16 -06001731
Tom Lendackyc11baa02014-01-24 16:18:02 -06001732 if (sha->final && sha->opad) {
1733 /* HMAC operation, recursively perform final SHA */
1734 struct ccp_cmd hmac_cmd;
1735 struct scatterlist sg;
Tom Lendackyc11baa02014-01-24 16:18:02 -06001736 u8 *hmac_buf;
1737
Tom Lendackyc11baa02014-01-24 16:18:02 -06001738 if (sha->opad_len != block_size) {
1739 ret = -EINVAL;
1740 goto e_data;
1741 }
1742
1743 hmac_buf = kmalloc(block_size + digest_size, GFP_KERNEL);
1744 if (!hmac_buf) {
1745 ret = -ENOMEM;
1746 goto e_data;
1747 }
1748 sg_init_one(&sg, hmac_buf, block_size + digest_size);
1749
1750 scatterwalk_map_and_copy(hmac_buf, sha->opad, 0, block_size, 0);
Gary R Hook4b394a22016-07-26 19:10:21 -05001751 switch (sha->type) {
1752 case CCP_SHA_TYPE_1:
1753 case CCP_SHA_TYPE_224:
1754 case CCP_SHA_TYPE_256:
1755 memcpy(hmac_buf + block_size,
1756 ctx.address + ooffset,
1757 digest_size);
1758 break;
Gary R Hookccebcf32017-03-15 13:20:43 -05001759 case CCP_SHA_TYPE_384:
1760 case CCP_SHA_TYPE_512:
1761 memcpy(hmac_buf + block_size,
1762 ctx.address + LSB_ITEM_SIZE + ooffset,
1763 LSB_ITEM_SIZE);
1764 memcpy(hmac_buf + block_size +
1765 (LSB_ITEM_SIZE - ooffset),
1766 ctx.address,
1767 LSB_ITEM_SIZE);
1768 break;
Gary R Hook4b394a22016-07-26 19:10:21 -05001769 default:
1770 ret = -EINVAL;
1771 goto e_ctx;
1772 }
Tom Lendackyc11baa02014-01-24 16:18:02 -06001773
1774 memset(&hmac_cmd, 0, sizeof(hmac_cmd));
1775 hmac_cmd.engine = CCP_ENGINE_SHA;
1776 hmac_cmd.u.sha.type = sha->type;
1777 hmac_cmd.u.sha.ctx = sha->ctx;
1778 hmac_cmd.u.sha.ctx_len = sha->ctx_len;
1779 hmac_cmd.u.sha.src = &sg;
1780 hmac_cmd.u.sha.src_len = block_size + digest_size;
1781 hmac_cmd.u.sha.opad = NULL;
1782 hmac_cmd.u.sha.opad_len = 0;
1783 hmac_cmd.u.sha.first = 1;
1784 hmac_cmd.u.sha.final = 1;
1785 hmac_cmd.u.sha.msg_bits = (block_size + digest_size) << 3;
1786
1787 ret = ccp_run_sha_cmd(cmd_q, &hmac_cmd);
1788 if (ret)
1789 cmd->engine_error = hmac_cmd.engine_error;
1790
1791 kfree(hmac_buf);
1792 }
1793
Tom Lendacky63b94502013-11-12 11:46:16 -06001794e_data:
Gary R Hook4b394a22016-07-26 19:10:21 -05001795 if (sha->src)
1796 ccp_free_data(&src, cmd_q);
Tom Lendacky63b94502013-11-12 11:46:16 -06001797
1798e_ctx:
1799 ccp_dm_free(&ctx);
1800
1801 return ret;
1802}
1803
1804static int ccp_run_rsa_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd)
1805{
1806 struct ccp_rsa_engine *rsa = &cmd->u.rsa;
Gary R Hook6ba46c72017-07-17 15:16:13 -05001807 struct ccp_dm_workarea exp, src, dst;
Tom Lendacky63b94502013-11-12 11:46:16 -06001808 struct ccp_op op;
Gary R Hook956ee212016-07-26 19:09:40 -05001809 unsigned int sb_count, i_len, o_len;
Tom Lendacky63b94502013-11-12 11:46:16 -06001810 int ret;
1811
Gary R Hooke28c1902017-07-17 15:16:42 -05001812 /* Check against the maximum allowable size, in bits */
1813 if (rsa->key_size > cmd_q->ccp->vdata->rsamax)
Tom Lendacky63b94502013-11-12 11:46:16 -06001814 return -EINVAL;
1815
1816 if (!rsa->exp || !rsa->mod || !rsa->src || !rsa->dst)
1817 return -EINVAL;
1818
Gary R Hook6ba46c72017-07-17 15:16:13 -05001819 memset(&op, 0, sizeof(op));
1820 op.cmd_q = cmd_q;
1821 op.jobid = CCP_NEW_JOBID(cmd_q->ccp);
1822
Tom Lendacky63b94502013-11-12 11:46:16 -06001823 /* The RSA modulus must precede the message being acted upon, so
1824 * it must be copied to a DMA area where the message and the
1825 * modulus can be concatenated. Therefore the input buffer
1826 * length required is twice the output buffer length (which
Gary R Hook6ba46c72017-07-17 15:16:13 -05001827 * must be a multiple of 256-bits). Compute o_len, i_len in bytes.
1828 * Buffer sizes must be a multiple of 32 bytes; rounding up may be
1829 * required.
Tom Lendacky63b94502013-11-12 11:46:16 -06001830 */
Gary R Hook6ba46c72017-07-17 15:16:13 -05001831 o_len = 32 * ((rsa->key_size + 255) / 256);
Tom Lendacky63b94502013-11-12 11:46:16 -06001832 i_len = o_len * 2;
1833
Arnd Bergmannd634bae2017-07-31 22:49:21 +02001834 sb_count = 0;
Gary R Hook6ba46c72017-07-17 15:16:13 -05001835 if (cmd_q->ccp->vdata->version < CCP_VERSION(5, 0)) {
1836 /* sb_count is the number of storage block slots required
1837 * for the modulus.
1838 */
1839 sb_count = o_len / CCP_SB_BYTES;
1840 op.sb_key = cmd_q->ccp->vdata->perform->sballoc(cmd_q,
1841 sb_count);
1842 if (!op.sb_key)
1843 return -EIO;
1844 } else {
1845 /* A version 5 device allows a modulus size that will not fit
1846 * in the LSB, so the command will transfer it from memory.
1847 * Set the sb key to the default, even though it's not used.
1848 */
1849 op.sb_key = cmd_q->sb_key;
1850 }
Tom Lendacky63b94502013-11-12 11:46:16 -06001851
Gary R Hook6ba46c72017-07-17 15:16:13 -05001852 /* The RSA exponent must be in little endian format. Reverse its
1853 * byte order.
Tom Lendacky63b94502013-11-12 11:46:16 -06001854 */
1855 ret = ccp_init_dm_workarea(&exp, cmd_q, o_len, DMA_TO_DEVICE);
1856 if (ret)
Gary R Hook956ee212016-07-26 19:09:40 -05001857 goto e_sb;
Tom Lendacky63b94502013-11-12 11:46:16 -06001858
Gary R Hook83d650a2017-02-09 15:50:08 -06001859 ret = ccp_reverse_set_dm_area(&exp, 0, rsa->exp, 0, rsa->exp_len);
Tom Lendacky355eba52015-10-01 16:32:31 -05001860 if (ret)
1861 goto e_exp;
Gary R Hook6ba46c72017-07-17 15:16:13 -05001862
1863 if (cmd_q->ccp->vdata->version < CCP_VERSION(5, 0)) {
1864 /* Copy the exponent to the local storage block, using
1865 * as many 32-byte blocks as were allocated above. It's
1866 * already little endian, so no further change is required.
1867 */
1868 ret = ccp_copy_to_sb(cmd_q, &exp, op.jobid, op.sb_key,
1869 CCP_PASSTHRU_BYTESWAP_NOOP);
1870 if (ret) {
1871 cmd->engine_error = cmd_q->cmd_error;
1872 goto e_exp;
1873 }
1874 } else {
1875 /* The exponent can be retrieved from memory via DMA. */
1876 op.exp.u.dma.address = exp.dma.address;
1877 op.exp.u.dma.offset = 0;
Tom Lendacky63b94502013-11-12 11:46:16 -06001878 }
1879
1880 /* Concatenate the modulus and the message. Both the modulus and
1881 * the operands must be in little endian format. Since the input
1882 * is in big endian format it must be converted.
1883 */
1884 ret = ccp_init_dm_workarea(&src, cmd_q, i_len, DMA_TO_DEVICE);
1885 if (ret)
1886 goto e_exp;
1887
Gary R Hook83d650a2017-02-09 15:50:08 -06001888 ret = ccp_reverse_set_dm_area(&src, 0, rsa->mod, 0, rsa->mod_len);
Tom Lendacky355eba52015-10-01 16:32:31 -05001889 if (ret)
1890 goto e_src;
Gary R Hook83d650a2017-02-09 15:50:08 -06001891 ret = ccp_reverse_set_dm_area(&src, o_len, rsa->src, 0, rsa->src_len);
Tom Lendacky355eba52015-10-01 16:32:31 -05001892 if (ret)
1893 goto e_src;
Tom Lendacky63b94502013-11-12 11:46:16 -06001894
1895 /* Prepare the output area for the operation */
Gary R Hook6ba46c72017-07-17 15:16:13 -05001896 ret = ccp_init_dm_workarea(&dst, cmd_q, o_len, DMA_FROM_DEVICE);
Tom Lendacky63b94502013-11-12 11:46:16 -06001897 if (ret)
1898 goto e_src;
1899
1900 op.soc = 1;
1901 op.src.u.dma.address = src.dma.address;
1902 op.src.u.dma.offset = 0;
1903 op.src.u.dma.length = i_len;
Gary R Hook6ba46c72017-07-17 15:16:13 -05001904 op.dst.u.dma.address = dst.dma.address;
Tom Lendacky63b94502013-11-12 11:46:16 -06001905 op.dst.u.dma.offset = 0;
1906 op.dst.u.dma.length = o_len;
1907
1908 op.u.rsa.mod_size = rsa->key_size;
1909 op.u.rsa.input_len = i_len;
1910
Gary R Hooka43eb982016-07-26 19:09:31 -05001911 ret = cmd_q->ccp->vdata->perform->rsa(&op);
Tom Lendacky63b94502013-11-12 11:46:16 -06001912 if (ret) {
1913 cmd->engine_error = cmd_q->cmd_error;
1914 goto e_dst;
1915 }
1916
Gary R Hook6ba46c72017-07-17 15:16:13 -05001917 ccp_reverse_get_dm_area(&dst, 0, rsa->dst, 0, rsa->mod_len);
Tom Lendacky63b94502013-11-12 11:46:16 -06001918
1919e_dst:
Gary R Hook6ba46c72017-07-17 15:16:13 -05001920 ccp_dm_free(&dst);
Tom Lendacky63b94502013-11-12 11:46:16 -06001921
1922e_src:
1923 ccp_dm_free(&src);
1924
1925e_exp:
1926 ccp_dm_free(&exp);
1927
Gary R Hook956ee212016-07-26 19:09:40 -05001928e_sb:
Arnd Bergmannd634bae2017-07-31 22:49:21 +02001929 if (sb_count)
Gary R Hook6ba46c72017-07-17 15:16:13 -05001930 cmd_q->ccp->vdata->perform->sbfree(cmd_q, op.sb_key, sb_count);
Tom Lendacky63b94502013-11-12 11:46:16 -06001931
1932 return ret;
1933}
1934
1935static int ccp_run_passthru_cmd(struct ccp_cmd_queue *cmd_q,
1936 struct ccp_cmd *cmd)
1937{
1938 struct ccp_passthru_engine *pt = &cmd->u.passthru;
1939 struct ccp_dm_workarea mask;
1940 struct ccp_data src, dst;
1941 struct ccp_op op;
1942 bool in_place = false;
1943 unsigned int i;
Gary R Hook4b394a22016-07-26 19:10:21 -05001944 int ret = 0;
Tom Lendacky63b94502013-11-12 11:46:16 -06001945
1946 if (!pt->final && (pt->src_len & (CCP_PASSTHRU_BLOCKSIZE - 1)))
1947 return -EINVAL;
1948
1949 if (!pt->src || !pt->dst)
1950 return -EINVAL;
1951
1952 if (pt->bit_mod != CCP_PASSTHRU_BITWISE_NOOP) {
1953 if (pt->mask_len != CCP_PASSTHRU_MASKSIZE)
1954 return -EINVAL;
1955 if (!pt->mask)
1956 return -EINVAL;
1957 }
1958
Gary R Hook956ee212016-07-26 19:09:40 -05001959 BUILD_BUG_ON(CCP_PASSTHRU_SB_COUNT != 1);
Tom Lendacky63b94502013-11-12 11:46:16 -06001960
1961 memset(&op, 0, sizeof(op));
1962 op.cmd_q = cmd_q;
Gary R Hook4b394a22016-07-26 19:10:21 -05001963 op.jobid = CCP_NEW_JOBID(cmd_q->ccp);
Tom Lendacky63b94502013-11-12 11:46:16 -06001964
1965 if (pt->bit_mod != CCP_PASSTHRU_BITWISE_NOOP) {
1966 /* Load the mask */
Gary R Hook956ee212016-07-26 19:09:40 -05001967 op.sb_key = cmd_q->sb_key;
Tom Lendacky63b94502013-11-12 11:46:16 -06001968
1969 ret = ccp_init_dm_workarea(&mask, cmd_q,
Gary R Hook956ee212016-07-26 19:09:40 -05001970 CCP_PASSTHRU_SB_COUNT *
1971 CCP_SB_BYTES,
Tom Lendacky63b94502013-11-12 11:46:16 -06001972 DMA_TO_DEVICE);
1973 if (ret)
1974 return ret;
1975
Gary R Hookb698a9f2018-03-07 11:31:14 -06001976 ret = ccp_set_dm_area(&mask, 0, pt->mask, 0, pt->mask_len);
1977 if (ret)
1978 goto e_mask;
Gary R Hook956ee212016-07-26 19:09:40 -05001979 ret = ccp_copy_to_sb(cmd_q, &mask, op.jobid, op.sb_key,
1980 CCP_PASSTHRU_BYTESWAP_NOOP);
Tom Lendacky63b94502013-11-12 11:46:16 -06001981 if (ret) {
1982 cmd->engine_error = cmd_q->cmd_error;
1983 goto e_mask;
1984 }
1985 }
1986
1987 /* Prepare the input and output data workareas. For in-place
1988 * operations we need to set the dma direction to BIDIRECTIONAL
1989 * and copy the src workarea to the dst workarea.
1990 */
1991 if (sg_virt(pt->src) == sg_virt(pt->dst))
1992 in_place = true;
1993
1994 ret = ccp_init_data(&src, cmd_q, pt->src, pt->src_len,
1995 CCP_PASSTHRU_MASKSIZE,
1996 in_place ? DMA_BIDIRECTIONAL : DMA_TO_DEVICE);
1997 if (ret)
1998 goto e_mask;
1999
Tom Lendacky8db88462015-02-03 13:07:05 -06002000 if (in_place) {
Tom Lendacky63b94502013-11-12 11:46:16 -06002001 dst = src;
Tom Lendacky8db88462015-02-03 13:07:05 -06002002 } else {
Tom Lendacky63b94502013-11-12 11:46:16 -06002003 ret = ccp_init_data(&dst, cmd_q, pt->dst, pt->src_len,
2004 CCP_PASSTHRU_MASKSIZE, DMA_FROM_DEVICE);
2005 if (ret)
2006 goto e_src;
2007 }
2008
2009 /* Send data to the CCP Passthru engine
2010 * Because the CCP engine works on a single source and destination
2011 * dma address at a time, each entry in the source scatterlist
2012 * (after the dma_map_sg call) must be less than or equal to the
2013 * (remaining) length in the destination scatterlist entry and the
2014 * length must be a multiple of CCP_PASSTHRU_BLOCKSIZE
2015 */
2016 dst.sg_wa.sg_used = 0;
2017 for (i = 1; i <= src.sg_wa.dma_count; i++) {
2018 if (!dst.sg_wa.sg ||
2019 (dst.sg_wa.sg->length < src.sg_wa.sg->length)) {
2020 ret = -EINVAL;
2021 goto e_dst;
2022 }
2023
2024 if (i == src.sg_wa.dma_count) {
2025 op.eom = 1;
2026 op.soc = 1;
2027 }
2028
2029 op.src.type = CCP_MEMTYPE_SYSTEM;
2030 op.src.u.dma.address = sg_dma_address(src.sg_wa.sg);
2031 op.src.u.dma.offset = 0;
2032 op.src.u.dma.length = sg_dma_len(src.sg_wa.sg);
2033
2034 op.dst.type = CCP_MEMTYPE_SYSTEM;
2035 op.dst.u.dma.address = sg_dma_address(dst.sg_wa.sg);
Dave Jones80e84c12014-02-09 09:59:14 +08002036 op.dst.u.dma.offset = dst.sg_wa.sg_used;
2037 op.dst.u.dma.length = op.src.u.dma.length;
Tom Lendacky63b94502013-11-12 11:46:16 -06002038
Gary R Hooka43eb982016-07-26 19:09:31 -05002039 ret = cmd_q->ccp->vdata->perform->passthru(&op);
Tom Lendacky63b94502013-11-12 11:46:16 -06002040 if (ret) {
2041 cmd->engine_error = cmd_q->cmd_error;
2042 goto e_dst;
2043 }
2044
2045 dst.sg_wa.sg_used += src.sg_wa.sg->length;
2046 if (dst.sg_wa.sg_used == dst.sg_wa.sg->length) {
2047 dst.sg_wa.sg = sg_next(dst.sg_wa.sg);
2048 dst.sg_wa.sg_used = 0;
2049 }
2050 src.sg_wa.sg = sg_next(src.sg_wa.sg);
2051 }
2052
2053e_dst:
2054 if (!in_place)
2055 ccp_free_data(&dst, cmd_q);
2056
2057e_src:
2058 ccp_free_data(&src, cmd_q);
2059
2060e_mask:
2061 if (pt->bit_mod != CCP_PASSTHRU_BITWISE_NOOP)
2062 ccp_dm_free(&mask);
2063
2064 return ret;
2065}
2066
Gary R Hook58ea8ab2016-04-18 09:21:44 -05002067static int ccp_run_passthru_nomap_cmd(struct ccp_cmd_queue *cmd_q,
2068 struct ccp_cmd *cmd)
2069{
2070 struct ccp_passthru_nomap_engine *pt = &cmd->u.passthru_nomap;
2071 struct ccp_dm_workarea mask;
2072 struct ccp_op op;
2073 int ret;
2074
2075 if (!pt->final && (pt->src_len & (CCP_PASSTHRU_BLOCKSIZE - 1)))
2076 return -EINVAL;
2077
2078 if (!pt->src_dma || !pt->dst_dma)
2079 return -EINVAL;
2080
2081 if (pt->bit_mod != CCP_PASSTHRU_BITWISE_NOOP) {
2082 if (pt->mask_len != CCP_PASSTHRU_MASKSIZE)
2083 return -EINVAL;
2084 if (!pt->mask)
2085 return -EINVAL;
2086 }
2087
Gary R Hook956ee212016-07-26 19:09:40 -05002088 BUILD_BUG_ON(CCP_PASSTHRU_SB_COUNT != 1);
Gary R Hook58ea8ab2016-04-18 09:21:44 -05002089
2090 memset(&op, 0, sizeof(op));
2091 op.cmd_q = cmd_q;
Gary R Hookbce386a2017-06-27 08:58:16 -05002092 op.jobid = CCP_NEW_JOBID(cmd_q->ccp);
Gary R Hook58ea8ab2016-04-18 09:21:44 -05002093
2094 if (pt->bit_mod != CCP_PASSTHRU_BITWISE_NOOP) {
2095 /* Load the mask */
Gary R Hook956ee212016-07-26 19:09:40 -05002096 op.sb_key = cmd_q->sb_key;
Gary R Hook58ea8ab2016-04-18 09:21:44 -05002097
2098 mask.length = pt->mask_len;
2099 mask.dma.address = pt->mask;
2100 mask.dma.length = pt->mask_len;
2101
Gary R Hook956ee212016-07-26 19:09:40 -05002102 ret = ccp_copy_to_sb(cmd_q, &mask, op.jobid, op.sb_key,
Gary R Hook58ea8ab2016-04-18 09:21:44 -05002103 CCP_PASSTHRU_BYTESWAP_NOOP);
2104 if (ret) {
2105 cmd->engine_error = cmd_q->cmd_error;
2106 return ret;
2107 }
2108 }
2109
2110 /* Send data to the CCP Passthru engine */
2111 op.eom = 1;
2112 op.soc = 1;
2113
2114 op.src.type = CCP_MEMTYPE_SYSTEM;
2115 op.src.u.dma.address = pt->src_dma;
2116 op.src.u.dma.offset = 0;
2117 op.src.u.dma.length = pt->src_len;
2118
2119 op.dst.type = CCP_MEMTYPE_SYSTEM;
2120 op.dst.u.dma.address = pt->dst_dma;
2121 op.dst.u.dma.offset = 0;
2122 op.dst.u.dma.length = pt->src_len;
2123
Gary R Hooka43eb982016-07-26 19:09:31 -05002124 ret = cmd_q->ccp->vdata->perform->passthru(&op);
Gary R Hook58ea8ab2016-04-18 09:21:44 -05002125 if (ret)
2126 cmd->engine_error = cmd_q->cmd_error;
2127
2128 return ret;
2129}
2130
Tom Lendacky63b94502013-11-12 11:46:16 -06002131static int ccp_run_ecc_mm_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd)
2132{
2133 struct ccp_ecc_engine *ecc = &cmd->u.ecc;
2134 struct ccp_dm_workarea src, dst;
2135 struct ccp_op op;
2136 int ret;
2137 u8 *save;
2138
2139 if (!ecc->u.mm.operand_1 ||
2140 (ecc->u.mm.operand_1_len > CCP_ECC_MODULUS_BYTES))
2141 return -EINVAL;
2142
2143 if (ecc->function != CCP_ECC_FUNCTION_MINV_384BIT)
2144 if (!ecc->u.mm.operand_2 ||
2145 (ecc->u.mm.operand_2_len > CCP_ECC_MODULUS_BYTES))
2146 return -EINVAL;
2147
2148 if (!ecc->u.mm.result ||
2149 (ecc->u.mm.result_len < CCP_ECC_MODULUS_BYTES))
2150 return -EINVAL;
2151
2152 memset(&op, 0, sizeof(op));
2153 op.cmd_q = cmd_q;
Gary R Hook4b394a22016-07-26 19:10:21 -05002154 op.jobid = CCP_NEW_JOBID(cmd_q->ccp);
Tom Lendacky63b94502013-11-12 11:46:16 -06002155
2156 /* Concatenate the modulus and the operands. Both the modulus and
2157 * the operands must be in little endian format. Since the input
2158 * is in big endian format it must be converted and placed in a
2159 * fixed length buffer.
2160 */
2161 ret = ccp_init_dm_workarea(&src, cmd_q, CCP_ECC_SRC_BUF_SIZE,
2162 DMA_TO_DEVICE);
2163 if (ret)
2164 return ret;
2165
2166 /* Save the workarea address since it is updated in order to perform
2167 * the concatenation
2168 */
2169 save = src.address;
2170
2171 /* Copy the ECC modulus */
Gary R Hook83d650a2017-02-09 15:50:08 -06002172 ret = ccp_reverse_set_dm_area(&src, 0, ecc->mod, 0, ecc->mod_len);
Tom Lendacky355eba52015-10-01 16:32:31 -05002173 if (ret)
2174 goto e_src;
Tom Lendacky63b94502013-11-12 11:46:16 -06002175 src.address += CCP_ECC_OPERAND_SIZE;
2176
2177 /* Copy the first operand */
Gary R Hook83d650a2017-02-09 15:50:08 -06002178 ret = ccp_reverse_set_dm_area(&src, 0, ecc->u.mm.operand_1, 0,
2179 ecc->u.mm.operand_1_len);
Tom Lendacky355eba52015-10-01 16:32:31 -05002180 if (ret)
2181 goto e_src;
Tom Lendacky63b94502013-11-12 11:46:16 -06002182 src.address += CCP_ECC_OPERAND_SIZE;
2183
2184 if (ecc->function != CCP_ECC_FUNCTION_MINV_384BIT) {
2185 /* Copy the second operand */
Gary R Hook83d650a2017-02-09 15:50:08 -06002186 ret = ccp_reverse_set_dm_area(&src, 0, ecc->u.mm.operand_2, 0,
2187 ecc->u.mm.operand_2_len);
Tom Lendacky355eba52015-10-01 16:32:31 -05002188 if (ret)
2189 goto e_src;
Tom Lendacky63b94502013-11-12 11:46:16 -06002190 src.address += CCP_ECC_OPERAND_SIZE;
2191 }
2192
2193 /* Restore the workarea address */
2194 src.address = save;
2195
2196 /* Prepare the output area for the operation */
2197 ret = ccp_init_dm_workarea(&dst, cmd_q, CCP_ECC_DST_BUF_SIZE,
2198 DMA_FROM_DEVICE);
2199 if (ret)
2200 goto e_src;
2201
2202 op.soc = 1;
2203 op.src.u.dma.address = src.dma.address;
2204 op.src.u.dma.offset = 0;
2205 op.src.u.dma.length = src.length;
2206 op.dst.u.dma.address = dst.dma.address;
2207 op.dst.u.dma.offset = 0;
2208 op.dst.u.dma.length = dst.length;
2209
2210 op.u.ecc.function = cmd->u.ecc.function;
2211
Gary R Hooka43eb982016-07-26 19:09:31 -05002212 ret = cmd_q->ccp->vdata->perform->ecc(&op);
Tom Lendacky63b94502013-11-12 11:46:16 -06002213 if (ret) {
2214 cmd->engine_error = cmd_q->cmd_error;
2215 goto e_dst;
2216 }
2217
2218 ecc->ecc_result = le16_to_cpup(
2219 (const __le16 *)(dst.address + CCP_ECC_RESULT_OFFSET));
2220 if (!(ecc->ecc_result & CCP_ECC_RESULT_SUCCESS)) {
2221 ret = -EIO;
2222 goto e_dst;
2223 }
2224
2225 /* Save the ECC result */
Gary R Hook83d650a2017-02-09 15:50:08 -06002226 ccp_reverse_get_dm_area(&dst, 0, ecc->u.mm.result, 0,
2227 CCP_ECC_MODULUS_BYTES);
Tom Lendacky63b94502013-11-12 11:46:16 -06002228
2229e_dst:
2230 ccp_dm_free(&dst);
2231
2232e_src:
2233 ccp_dm_free(&src);
2234
2235 return ret;
2236}
2237
2238static int ccp_run_ecc_pm_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd)
2239{
2240 struct ccp_ecc_engine *ecc = &cmd->u.ecc;
2241 struct ccp_dm_workarea src, dst;
2242 struct ccp_op op;
2243 int ret;
2244 u8 *save;
2245
2246 if (!ecc->u.pm.point_1.x ||
2247 (ecc->u.pm.point_1.x_len > CCP_ECC_MODULUS_BYTES) ||
2248 !ecc->u.pm.point_1.y ||
2249 (ecc->u.pm.point_1.y_len > CCP_ECC_MODULUS_BYTES))
2250 return -EINVAL;
2251
2252 if (ecc->function == CCP_ECC_FUNCTION_PADD_384BIT) {
2253 if (!ecc->u.pm.point_2.x ||
2254 (ecc->u.pm.point_2.x_len > CCP_ECC_MODULUS_BYTES) ||
2255 !ecc->u.pm.point_2.y ||
2256 (ecc->u.pm.point_2.y_len > CCP_ECC_MODULUS_BYTES))
2257 return -EINVAL;
2258 } else {
2259 if (!ecc->u.pm.domain_a ||
2260 (ecc->u.pm.domain_a_len > CCP_ECC_MODULUS_BYTES))
2261 return -EINVAL;
2262
2263 if (ecc->function == CCP_ECC_FUNCTION_PMUL_384BIT)
2264 if (!ecc->u.pm.scalar ||
2265 (ecc->u.pm.scalar_len > CCP_ECC_MODULUS_BYTES))
2266 return -EINVAL;
2267 }
2268
2269 if (!ecc->u.pm.result.x ||
2270 (ecc->u.pm.result.x_len < CCP_ECC_MODULUS_BYTES) ||
2271 !ecc->u.pm.result.y ||
2272 (ecc->u.pm.result.y_len < CCP_ECC_MODULUS_BYTES))
2273 return -EINVAL;
2274
2275 memset(&op, 0, sizeof(op));
2276 op.cmd_q = cmd_q;
Gary R Hook4b394a22016-07-26 19:10:21 -05002277 op.jobid = CCP_NEW_JOBID(cmd_q->ccp);
Tom Lendacky63b94502013-11-12 11:46:16 -06002278
2279 /* Concatenate the modulus and the operands. Both the modulus and
2280 * the operands must be in little endian format. Since the input
2281 * is in big endian format it must be converted and placed in a
2282 * fixed length buffer.
2283 */
2284 ret = ccp_init_dm_workarea(&src, cmd_q, CCP_ECC_SRC_BUF_SIZE,
2285 DMA_TO_DEVICE);
2286 if (ret)
2287 return ret;
2288
2289 /* Save the workarea address since it is updated in order to perform
2290 * the concatenation
2291 */
2292 save = src.address;
2293
2294 /* Copy the ECC modulus */
Gary R Hook83d650a2017-02-09 15:50:08 -06002295 ret = ccp_reverse_set_dm_area(&src, 0, ecc->mod, 0, ecc->mod_len);
Tom Lendacky355eba52015-10-01 16:32:31 -05002296 if (ret)
2297 goto e_src;
Tom Lendacky63b94502013-11-12 11:46:16 -06002298 src.address += CCP_ECC_OPERAND_SIZE;
2299
2300 /* Copy the first point X and Y coordinate */
Gary R Hook83d650a2017-02-09 15:50:08 -06002301 ret = ccp_reverse_set_dm_area(&src, 0, ecc->u.pm.point_1.x, 0,
2302 ecc->u.pm.point_1.x_len);
Tom Lendacky355eba52015-10-01 16:32:31 -05002303 if (ret)
2304 goto e_src;
Tom Lendacky63b94502013-11-12 11:46:16 -06002305 src.address += CCP_ECC_OPERAND_SIZE;
Gary R Hook83d650a2017-02-09 15:50:08 -06002306 ret = ccp_reverse_set_dm_area(&src, 0, ecc->u.pm.point_1.y, 0,
2307 ecc->u.pm.point_1.y_len);
Tom Lendacky355eba52015-10-01 16:32:31 -05002308 if (ret)
2309 goto e_src;
Tom Lendacky63b94502013-11-12 11:46:16 -06002310 src.address += CCP_ECC_OPERAND_SIZE;
2311
Gary R Hook4b394a22016-07-26 19:10:21 -05002312 /* Set the first point Z coordinate to 1 */
Tom Lendacky8db88462015-02-03 13:07:05 -06002313 *src.address = 0x01;
Tom Lendacky63b94502013-11-12 11:46:16 -06002314 src.address += CCP_ECC_OPERAND_SIZE;
2315
2316 if (ecc->function == CCP_ECC_FUNCTION_PADD_384BIT) {
2317 /* Copy the second point X and Y coordinate */
Gary R Hook83d650a2017-02-09 15:50:08 -06002318 ret = ccp_reverse_set_dm_area(&src, 0, ecc->u.pm.point_2.x, 0,
2319 ecc->u.pm.point_2.x_len);
Tom Lendacky355eba52015-10-01 16:32:31 -05002320 if (ret)
2321 goto e_src;
Tom Lendacky63b94502013-11-12 11:46:16 -06002322 src.address += CCP_ECC_OPERAND_SIZE;
Gary R Hook83d650a2017-02-09 15:50:08 -06002323 ret = ccp_reverse_set_dm_area(&src, 0, ecc->u.pm.point_2.y, 0,
2324 ecc->u.pm.point_2.y_len);
Tom Lendacky355eba52015-10-01 16:32:31 -05002325 if (ret)
2326 goto e_src;
Tom Lendacky63b94502013-11-12 11:46:16 -06002327 src.address += CCP_ECC_OPERAND_SIZE;
2328
Gary R Hook4b394a22016-07-26 19:10:21 -05002329 /* Set the second point Z coordinate to 1 */
Tom Lendacky8db88462015-02-03 13:07:05 -06002330 *src.address = 0x01;
Tom Lendacky63b94502013-11-12 11:46:16 -06002331 src.address += CCP_ECC_OPERAND_SIZE;
2332 } else {
2333 /* Copy the Domain "a" parameter */
Gary R Hook83d650a2017-02-09 15:50:08 -06002334 ret = ccp_reverse_set_dm_area(&src, 0, ecc->u.pm.domain_a, 0,
2335 ecc->u.pm.domain_a_len);
Tom Lendacky355eba52015-10-01 16:32:31 -05002336 if (ret)
2337 goto e_src;
Tom Lendacky63b94502013-11-12 11:46:16 -06002338 src.address += CCP_ECC_OPERAND_SIZE;
2339
2340 if (ecc->function == CCP_ECC_FUNCTION_PMUL_384BIT) {
2341 /* Copy the scalar value */
Gary R Hook83d650a2017-02-09 15:50:08 -06002342 ret = ccp_reverse_set_dm_area(&src, 0,
2343 ecc->u.pm.scalar, 0,
2344 ecc->u.pm.scalar_len);
Tom Lendacky355eba52015-10-01 16:32:31 -05002345 if (ret)
2346 goto e_src;
Tom Lendacky63b94502013-11-12 11:46:16 -06002347 src.address += CCP_ECC_OPERAND_SIZE;
2348 }
2349 }
2350
2351 /* Restore the workarea address */
2352 src.address = save;
2353
2354 /* Prepare the output area for the operation */
2355 ret = ccp_init_dm_workarea(&dst, cmd_q, CCP_ECC_DST_BUF_SIZE,
2356 DMA_FROM_DEVICE);
2357 if (ret)
2358 goto e_src;
2359
2360 op.soc = 1;
2361 op.src.u.dma.address = src.dma.address;
2362 op.src.u.dma.offset = 0;
2363 op.src.u.dma.length = src.length;
2364 op.dst.u.dma.address = dst.dma.address;
2365 op.dst.u.dma.offset = 0;
2366 op.dst.u.dma.length = dst.length;
2367
2368 op.u.ecc.function = cmd->u.ecc.function;
2369
Gary R Hooka43eb982016-07-26 19:09:31 -05002370 ret = cmd_q->ccp->vdata->perform->ecc(&op);
Tom Lendacky63b94502013-11-12 11:46:16 -06002371 if (ret) {
2372 cmd->engine_error = cmd_q->cmd_error;
2373 goto e_dst;
2374 }
2375
2376 ecc->ecc_result = le16_to_cpup(
2377 (const __le16 *)(dst.address + CCP_ECC_RESULT_OFFSET));
2378 if (!(ecc->ecc_result & CCP_ECC_RESULT_SUCCESS)) {
2379 ret = -EIO;
2380 goto e_dst;
2381 }
2382
2383 /* Save the workarea address since it is updated as we walk through
2384 * to copy the point math result
2385 */
2386 save = dst.address;
2387
2388 /* Save the ECC result X and Y coordinates */
Gary R Hook83d650a2017-02-09 15:50:08 -06002389 ccp_reverse_get_dm_area(&dst, 0, ecc->u.pm.result.x, 0,
Tom Lendacky63b94502013-11-12 11:46:16 -06002390 CCP_ECC_MODULUS_BYTES);
2391 dst.address += CCP_ECC_OUTPUT_SIZE;
Gary R Hook83d650a2017-02-09 15:50:08 -06002392 ccp_reverse_get_dm_area(&dst, 0, ecc->u.pm.result.y, 0,
Tom Lendacky63b94502013-11-12 11:46:16 -06002393 CCP_ECC_MODULUS_BYTES);
2394 dst.address += CCP_ECC_OUTPUT_SIZE;
2395
2396 /* Restore the workarea address */
2397 dst.address = save;
2398
2399e_dst:
2400 ccp_dm_free(&dst);
2401
2402e_src:
2403 ccp_dm_free(&src);
2404
2405 return ret;
2406}
2407
2408static int ccp_run_ecc_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd)
2409{
2410 struct ccp_ecc_engine *ecc = &cmd->u.ecc;
2411
2412 ecc->ecc_result = 0;
2413
2414 if (!ecc->mod ||
2415 (ecc->mod_len > CCP_ECC_MODULUS_BYTES))
2416 return -EINVAL;
2417
2418 switch (ecc->function) {
2419 case CCP_ECC_FUNCTION_MMUL_384BIT:
2420 case CCP_ECC_FUNCTION_MADD_384BIT:
2421 case CCP_ECC_FUNCTION_MINV_384BIT:
2422 return ccp_run_ecc_mm_cmd(cmd_q, cmd);
2423
2424 case CCP_ECC_FUNCTION_PADD_384BIT:
2425 case CCP_ECC_FUNCTION_PMUL_384BIT:
2426 case CCP_ECC_FUNCTION_PDBL_384BIT:
2427 return ccp_run_ecc_pm_cmd(cmd_q, cmd);
2428
2429 default:
2430 return -EINVAL;
2431 }
2432}
2433
2434int ccp_run_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd)
2435{
2436 int ret;
2437
2438 cmd->engine_error = 0;
2439 cmd_q->cmd_error = 0;
2440 cmd_q->int_rcvd = 0;
Gary R Hookbb4e89b2016-07-26 19:10:13 -05002441 cmd_q->free_slots = cmd_q->ccp->vdata->perform->get_free_slots(cmd_q);
Tom Lendacky63b94502013-11-12 11:46:16 -06002442
2443 switch (cmd->engine) {
2444 case CCP_ENGINE_AES:
2445 ret = ccp_run_aes_cmd(cmd_q, cmd);
2446 break;
2447 case CCP_ENGINE_XTS_AES_128:
2448 ret = ccp_run_xts_aes_cmd(cmd_q, cmd);
2449 break;
Gary R Hook990672d2017-03-15 13:20:52 -05002450 case CCP_ENGINE_DES3:
2451 ret = ccp_run_des3_cmd(cmd_q, cmd);
2452 break;
Tom Lendacky63b94502013-11-12 11:46:16 -06002453 case CCP_ENGINE_SHA:
2454 ret = ccp_run_sha_cmd(cmd_q, cmd);
2455 break;
2456 case CCP_ENGINE_RSA:
2457 ret = ccp_run_rsa_cmd(cmd_q, cmd);
2458 break;
2459 case CCP_ENGINE_PASSTHRU:
Gary R Hook58ea8ab2016-04-18 09:21:44 -05002460 if (cmd->flags & CCP_CMD_PASSTHRU_NO_DMA_MAP)
2461 ret = ccp_run_passthru_nomap_cmd(cmd_q, cmd);
2462 else
2463 ret = ccp_run_passthru_cmd(cmd_q, cmd);
Tom Lendacky63b94502013-11-12 11:46:16 -06002464 break;
2465 case CCP_ENGINE_ECC:
2466 ret = ccp_run_ecc_cmd(cmd_q, cmd);
2467 break;
2468 default:
2469 ret = -EINVAL;
2470 }
2471
2472 return ret;
2473}