Skip to content

Commit d79b5d0

Browse files
gongleiareiherbertx
authored andcommitted
crypto: virtio - support crypto engine framework
crypto engine was introduced since 'commit 735d37b ("crypto: engine - Introduce the block request crypto engine framework")' which uses work queue to realize the asynchronous processing for ablk_cipher and ahash. For virtio-crypto device, I register an engine for each data virtqueue so that we can use the capability of multiple data queues in future. Cc: Baolin Wang <baolin.wang@linaro.org> Cc: Herbert Xu <herbert@gondor.apana.org.au> Cc: Michael S. Tsirkin <mst@redhat.com> Signed-off-by: Gonglei <arei.gonglei@huawei.com> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
1 parent 1043c51 commit d79b5d0

4 files changed

Lines changed: 121 additions & 22 deletions

File tree

‎drivers/crypto/virtio/Kconfig‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ config CRYPTO_DEV_VIRTIO
44
select CRYPTO_AEAD
55
select CRYPTO_AUTHENC
66
select CRYPTO_BLKCIPHER
7+
select CRYPTO_ENGINE
78
default m
89
help
910
This driver provides support for virtio crypto device. If you

‎drivers/crypto/virtio/virtio_crypto_algs.c‎

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -288,8 +288,7 @@ static int virtio_crypto_ablkcipher_setkey(struct crypto_ablkcipher *tfm,
288288
static int
289289
__virtio_crypto_ablkcipher_do_req(struct virtio_crypto_request *vc_req,
290290
struct ablkcipher_request *req,
291-
struct data_queue *data_vq,
292-
__u8 op)
291+
struct data_queue *data_vq)
293292
{
294293
struct crypto_ablkcipher *tfm = crypto_ablkcipher_reqtfm(req);
295294
unsigned int ivsize = crypto_ablkcipher_ivsize(tfm);
@@ -329,7 +328,7 @@ __virtio_crypto_ablkcipher_do_req(struct virtio_crypto_request *vc_req,
329328
vc_req->req_data = req_data;
330329
vc_req->type = VIRTIO_CRYPTO_SYM_OP_CIPHER;
331330
/* Head of operation */
332-
if (op) {
331+
if (vc_req->encrypt) {
333332
req_data->header.session_id =
334333
cpu_to_le64(ctx->enc_sess_info.session_id);
335334
req_data->header.opcode =
@@ -424,19 +423,15 @@ static int virtio_crypto_ablkcipher_encrypt(struct ablkcipher_request *req)
424423
struct virtio_crypto_ablkcipher_ctx *ctx = crypto_ablkcipher_ctx(atfm);
425424
struct virtio_crypto_request *vc_req = ablkcipher_request_ctx(req);
426425
struct virtio_crypto *vcrypto = ctx->vcrypto;
427-
int ret;
428426
/* Use the first data virtqueue as default */
429427
struct data_queue *data_vq = &vcrypto->data_vq[0];
430428

431429
vc_req->ablkcipher_ctx = ctx;
432430
vc_req->ablkcipher_req = req;
433-
ret = __virtio_crypto_ablkcipher_do_req(vc_req, req, data_vq, 1);
434-
if (ret < 0) {
435-
pr_err("virtio_crypto: Encryption failed!\n");
436-
return ret;
437-
}
431+
vc_req->encrypt = true;
432+
vc_req->dataq = data_vq;
438433

439-
return -EINPROGRESS;
434+
return crypto_transfer_cipher_request_to_engine(data_vq->engine, req);
440435
}
441436

442437
static int virtio_crypto_ablkcipher_decrypt(struct ablkcipher_request *req)
@@ -445,20 +440,16 @@ static int virtio_crypto_ablkcipher_decrypt(struct ablkcipher_request *req)
445440
struct virtio_crypto_ablkcipher_ctx *ctx = crypto_ablkcipher_ctx(atfm);
446441
struct virtio_crypto_request *vc_req = ablkcipher_request_ctx(req);
447442
struct virtio_crypto *vcrypto = ctx->vcrypto;
448-
int ret;
449443
/* Use the first data virtqueue as default */
450444
struct data_queue *data_vq = &vcrypto->data_vq[0];
451445

452446
vc_req->ablkcipher_ctx = ctx;
453447
vc_req->ablkcipher_req = req;
454448

455-
ret = __virtio_crypto_ablkcipher_do_req(vc_req, req, data_vq, 0);
456-
if (ret < 0) {
457-
pr_err("virtio_crypto: Decryption failed!\n");
458-
return ret;
459-
}
449+
vc_req->encrypt = false;
450+
vc_req->dataq = data_vq;
460451

461-
return -EINPROGRESS;
452+
return crypto_transfer_cipher_request_to_engine(data_vq->engine, req);
462453
}
463454

464455
static int virtio_crypto_ablkcipher_init(struct crypto_tfm *tfm)
@@ -484,6 +475,33 @@ static void virtio_crypto_ablkcipher_exit(struct crypto_tfm *tfm)
484475
ctx->vcrypto = NULL;
485476
}
486477

478+
int virtio_crypto_ablkcipher_crypt_req(
479+
struct crypto_engine *engine,
480+
struct ablkcipher_request *req)
481+
{
482+
struct virtio_crypto_request *vc_req = ablkcipher_request_ctx(req);
483+
struct data_queue *data_vq = vc_req->dataq;
484+
int ret;
485+
486+
ret = __virtio_crypto_ablkcipher_do_req(vc_req, req, data_vq);
487+
if (ret < 0)
488+
return ret;
489+
490+
virtqueue_kick(data_vq->vq);
491+
492+
return 0;
493+
}
494+
495+
void virtio_crypto_ablkcipher_finalize_req(
496+
struct virtio_crypto_request *vc_req,
497+
struct ablkcipher_request *req,
498+
int err)
499+
{
500+
crypto_finalize_cipher_request(vc_req->dataq->engine, req, err);
501+
502+
virtcrypto_clear_request(vc_req);
503+
}
504+
487505
static struct crypto_alg virtio_crypto_algs[] = { {
488506
.cra_name = "cbc(aes)",
489507
.cra_driver_name = "virtio_crypto_aes_cbc",

‎drivers/crypto/virtio/virtio_crypto_common.h‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include <crypto/aead.h>
2626
#include <crypto/aes.h>
2727
#include <crypto/authenc.h>
28+
#include <crypto/engine.h>
2829

2930

3031
/* Internal representation of a data virtqueue */
@@ -37,6 +38,8 @@ struct data_queue {
3738

3839
/* Name of the tx queue: dataq.$index */
3940
char name[32];
41+
42+
struct crypto_engine *engine;
4043
};
4144

4245
struct virtio_crypto {
@@ -97,6 +100,9 @@ struct virtio_crypto_request {
97100
struct virtio_crypto_op_data_req *req_data;
98101
struct scatterlist **sgs;
99102
uint8_t *iv;
103+
/* Encryption? */
104+
bool encrypt;
105+
struct data_queue *dataq;
100106
};
101107

102108
int virtcrypto_devmgr_add_dev(struct virtio_crypto *vcrypto_dev);
@@ -110,6 +116,16 @@ int virtcrypto_dev_started(struct virtio_crypto *vcrypto_dev);
110116
struct virtio_crypto *virtcrypto_get_dev_node(int node);
111117
int virtcrypto_dev_start(struct virtio_crypto *vcrypto);
112118
void virtcrypto_dev_stop(struct virtio_crypto *vcrypto);
119+
int virtio_crypto_ablkcipher_crypt_req(
120+
struct crypto_engine *engine,
121+
struct ablkcipher_request *req);
122+
void virtio_crypto_ablkcipher_finalize_req(
123+
struct virtio_crypto_request *vc_req,
124+
struct ablkcipher_request *req,
125+
int err);
126+
127+
void
128+
virtcrypto_clear_request(struct virtio_crypto_request *vc_req);
113129

114130
static inline int virtio_crypto_get_current_node(void)
115131
{

‎drivers/crypto/virtio/virtio_crypto_core.c‎

Lines changed: 69 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
#include "virtio_crypto_common.h"
2626

2727

28-
static void
28+
void
2929
virtcrypto_clear_request(struct virtio_crypto_request *vc_req)
3030
{
3131
if (vc_req) {
@@ -66,12 +66,12 @@ static void virtcrypto_dataq_callback(struct virtqueue *vq)
6666
break;
6767
}
6868
ablk_req = vc_req->ablkcipher_req;
69-
virtcrypto_clear_request(vc_req);
7069

7170
spin_unlock_irqrestore(
7271
&vcrypto->data_vq[qid].lock, flags);
7372
/* Finish the encrypt or decrypt process */
74-
ablk_req->base.complete(&ablk_req->base, error);
73+
virtio_crypto_ablkcipher_finalize_req(vc_req,
74+
ablk_req, error);
7575
spin_lock_irqsave(
7676
&vcrypto->data_vq[qid].lock, flags);
7777
}
@@ -87,6 +87,7 @@ static int virtcrypto_find_vqs(struct virtio_crypto *vi)
8787
int ret = -ENOMEM;
8888
int i, total_vqs;
8989
const char **names;
90+
struct device *dev = &vi->vdev->dev;
9091

9192
/*
9293
* We expect 1 data virtqueue, followed by
@@ -128,6 +129,15 @@ static int virtcrypto_find_vqs(struct virtio_crypto *vi)
128129
for (i = 0; i < vi->max_data_queues; i++) {
129130
spin_lock_init(&vi->data_vq[i].lock);
130131
vi->data_vq[i].vq = vqs[i];
132+
/* Initialize crypto engine */
133+
vi->data_vq[i].engine = crypto_engine_alloc_init(dev, 1);
134+
if (!vi->data_vq[i].engine) {
135+
ret = -ENOMEM;
136+
goto err_engine;
137+
}
138+
139+
vi->data_vq[i].engine->cipher_one_request =
140+
virtio_crypto_ablkcipher_crypt_req;
131141
}
132142

133143
kfree(names);
@@ -136,6 +146,7 @@ static int virtcrypto_find_vqs(struct virtio_crypto *vi)
136146

137147
return 0;
138148

149+
err_engine:
139150
err_find:
140151
kfree(names);
141152
err_names:
@@ -269,6 +280,38 @@ static int virtcrypto_update_status(struct virtio_crypto *vcrypto)
269280
return 0;
270281
}
271282

283+
static int virtcrypto_start_crypto_engines(struct virtio_crypto *vcrypto)
284+
{
285+
int32_t i;
286+
int ret;
287+
288+
for (i = 0; i < vcrypto->max_data_queues; i++) {
289+
if (vcrypto->data_vq[i].engine) {
290+
ret = crypto_engine_start(vcrypto->data_vq[i].engine);
291+
if (ret)
292+
goto err;
293+
}
294+
}
295+
296+
return 0;
297+
298+
err:
299+
while (--i >= 0)
300+
if (vcrypto->data_vq[i].engine)
301+
crypto_engine_exit(vcrypto->data_vq[i].engine);
302+
303+
return ret;
304+
}
305+
306+
static void virtcrypto_clear_crypto_engines(struct virtio_crypto *vcrypto)
307+
{
308+
u32 i;
309+
310+
for (i = 0; i < vcrypto->max_data_queues; i++)
311+
if (vcrypto->data_vq[i].engine)
312+
crypto_engine_exit(vcrypto->data_vq[i].engine);
313+
}
314+
272315
static void virtcrypto_del_vqs(struct virtio_crypto *vcrypto)
273316
{
274317
struct virtio_device *vdev = vcrypto->vdev;
@@ -355,14 +398,21 @@ static int virtcrypto_probe(struct virtio_device *vdev)
355398
dev_err(&vdev->dev, "Failed to initialize vqs.\n");
356399
goto free_dev;
357400
}
401+
402+
err = virtcrypto_start_crypto_engines(vcrypto);
403+
if (err)
404+
goto free_vqs;
405+
358406
virtio_device_ready(vdev);
359407

360408
err = virtcrypto_update_status(vcrypto);
361409
if (err)
362-
goto free_vqs;
410+
goto free_engines;
363411

364412
return 0;
365413

414+
free_engines:
415+
virtcrypto_clear_crypto_engines(vcrypto);
366416
free_vqs:
367417
vcrypto->vdev->config->reset(vdev);
368418
virtcrypto_del_vqs(vcrypto);
@@ -398,6 +448,7 @@ static void virtcrypto_remove(struct virtio_device *vdev)
398448
virtcrypto_dev_stop(vcrypto);
399449
vdev->config->reset(vdev);
400450
virtcrypto_free_unused_reqs(vcrypto);
451+
virtcrypto_clear_crypto_engines(vcrypto);
401452
virtcrypto_del_vqs(vcrypto);
402453
virtcrypto_devmgr_rm_dev(vcrypto);
403454
kfree(vcrypto);
@@ -420,6 +471,7 @@ static int virtcrypto_freeze(struct virtio_device *vdev)
420471
if (virtcrypto_dev_started(vcrypto))
421472
virtcrypto_dev_stop(vcrypto);
422473

474+
virtcrypto_clear_crypto_engines(vcrypto);
423475
virtcrypto_del_vqs(vcrypto);
424476
return 0;
425477
}
@@ -433,14 +485,26 @@ static int virtcrypto_restore(struct virtio_device *vdev)
433485
if (err)
434486
return err;
435487

488+
err = virtcrypto_start_crypto_engines(vcrypto);
489+
if (err)
490+
goto free_vqs;
491+
436492
virtio_device_ready(vdev);
493+
437494
err = virtcrypto_dev_start(vcrypto);
438495
if (err) {
439496
dev_err(&vdev->dev, "Failed to start virtio crypto device.\n");
440-
return -EFAULT;
497+
goto free_engines;
441498
}
442499

443500
return 0;
501+
502+
free_engines:
503+
virtcrypto_clear_crypto_engines(vcrypto);
504+
free_vqs:
505+
vcrypto->vdev->config->reset(vdev);
506+
virtcrypto_del_vqs(vcrypto);
507+
return err;
444508
}
445509
#endif
446510

0 commit comments

Comments
 (0)