Skip to content

Commit d31e712

Browse files
XinIZengherbertx
authored andcommitted
crypto: virtio - Refacotor virtio_crypto driver for new virito crypto services
In current virtio crypto device driver, some common data structures and implementations that should be used by other virtio crypto algorithms (e.g. asymmetric crypto algorithms) introduce symmetric crypto algorithms specific implementations. This patch refactors these pieces of code so that they can be reused by other virtio crypto algorithms. Acked-by: Gonglei <arei.gonglei@huawei.com> Signed-off-by: Xin Zeng <xin.zeng@intel.com> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
1 parent 41cdf7a commit d31e712

3 files changed

Lines changed: 98 additions & 70 deletions

File tree

‎drivers/crypto/virtio/virtio_crypto_algs.c‎

Lines changed: 87 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,68 @@
2727
#include <uapi/linux/virtio_crypto.h>
2828
#include "virtio_crypto_common.h"
2929

30+
31+
struct virtio_crypto_ablkcipher_ctx {
32+
struct virtio_crypto *vcrypto;
33+
struct crypto_tfm *tfm;
34+
35+
struct virtio_crypto_sym_session_info enc_sess_info;
36+
struct virtio_crypto_sym_session_info dec_sess_info;
37+
};
38+
39+
struct virtio_crypto_sym_request {
40+
struct virtio_crypto_request base;
41+
42+
/* Cipher or aead */
43+
uint32_t type;
44+
struct virtio_crypto_ablkcipher_ctx *ablkcipher_ctx;
45+
struct ablkcipher_request *ablkcipher_req;
46+
uint8_t *iv;
47+
/* Encryption? */
48+
bool encrypt;
49+
};
50+
3051
/*
3152
* The algs_lock protects the below global virtio_crypto_active_devs
3253
* and crypto algorithms registion.
3354
*/
3455
static DEFINE_MUTEX(algs_lock);
3556
static unsigned int virtio_crypto_active_devs;
57+
static void virtio_crypto_ablkcipher_finalize_req(
58+
struct virtio_crypto_sym_request *vc_sym_req,
59+
struct ablkcipher_request *req,
60+
int err);
61+
62+
static void virtio_crypto_dataq_sym_callback
63+
(struct virtio_crypto_request *vc_req, int len)
64+
{
65+
struct virtio_crypto_sym_request *vc_sym_req =
66+
container_of(vc_req, struct virtio_crypto_sym_request, base);
67+
struct ablkcipher_request *ablk_req;
68+
int error;
69+
70+
/* Finish the encrypt or decrypt process */
71+
if (vc_sym_req->type == VIRTIO_CRYPTO_SYM_OP_CIPHER) {
72+
switch (vc_req->status) {
73+
case VIRTIO_CRYPTO_OK:
74+
error = 0;
75+
break;
76+
case VIRTIO_CRYPTO_INVSESS:
77+
case VIRTIO_CRYPTO_ERR:
78+
error = -EINVAL;
79+
break;
80+
case VIRTIO_CRYPTO_BADMSG:
81+
error = -EBADMSG;
82+
break;
83+
default:
84+
error = -EIO;
85+
break;
86+
}
87+
ablk_req = vc_sym_req->ablkcipher_req;
88+
virtio_crypto_ablkcipher_finalize_req(vc_sym_req,
89+
ablk_req, error);
90+
}
91+
}
3692

3793
static u64 virtio_crypto_alg_sg_nents_length(struct scatterlist *sg)
3894
{
@@ -286,13 +342,14 @@ static int virtio_crypto_ablkcipher_setkey(struct crypto_ablkcipher *tfm,
286342
}
287343

288344
static int
289-
__virtio_crypto_ablkcipher_do_req(struct virtio_crypto_request *vc_req,
345+
__virtio_crypto_ablkcipher_do_req(struct virtio_crypto_sym_request *vc_sym_req,
290346
struct ablkcipher_request *req,
291347
struct data_queue *data_vq)
292348
{
293349
struct crypto_ablkcipher *tfm = crypto_ablkcipher_reqtfm(req);
350+
struct virtio_crypto_ablkcipher_ctx *ctx = vc_sym_req->ablkcipher_ctx;
351+
struct virtio_crypto_request *vc_req = &vc_sym_req->base;
294352
unsigned int ivsize = crypto_ablkcipher_ivsize(tfm);
295-
struct virtio_crypto_ablkcipher_ctx *ctx = vc_req->ablkcipher_ctx;
296353
struct virtio_crypto *vcrypto = ctx->vcrypto;
297354
struct virtio_crypto_op_data_req *req_data;
298355
int src_nents, dst_nents;
@@ -326,9 +383,9 @@ __virtio_crypto_ablkcipher_do_req(struct virtio_crypto_request *vc_req,
326383
}
327384

328385
vc_req->req_data = req_data;
329-
vc_req->type = VIRTIO_CRYPTO_SYM_OP_CIPHER;
386+
vc_sym_req->type = VIRTIO_CRYPTO_SYM_OP_CIPHER;
330387
/* Head of operation */
331-
if (vc_req->encrypt) {
388+
if (vc_sym_req->encrypt) {
332389
req_data->header.session_id =
333390
cpu_to_le64(ctx->enc_sess_info.session_id);
334391
req_data->header.opcode =
@@ -383,7 +440,7 @@ __virtio_crypto_ablkcipher_do_req(struct virtio_crypto_request *vc_req,
383440
memcpy(iv, req->info, ivsize);
384441
sg_init_one(&iv_sg, iv, ivsize);
385442
sgs[num_out++] = &iv_sg;
386-
vc_req->iv = iv;
443+
vc_sym_req->iv = iv;
387444

388445
/* Source data */
389446
for (i = 0; i < src_nents; i++)
@@ -421,15 +478,18 @@ static int virtio_crypto_ablkcipher_encrypt(struct ablkcipher_request *req)
421478
{
422479
struct crypto_ablkcipher *atfm = crypto_ablkcipher_reqtfm(req);
423480
struct virtio_crypto_ablkcipher_ctx *ctx = crypto_ablkcipher_ctx(atfm);
424-
struct virtio_crypto_request *vc_req = ablkcipher_request_ctx(req);
481+
struct virtio_crypto_sym_request *vc_sym_req =
482+
ablkcipher_request_ctx(req);
483+
struct virtio_crypto_request *vc_req = &vc_sym_req->base;
425484
struct virtio_crypto *vcrypto = ctx->vcrypto;
426485
/* Use the first data virtqueue as default */
427486
struct data_queue *data_vq = &vcrypto->data_vq[0];
428487

429-
vc_req->ablkcipher_ctx = ctx;
430-
vc_req->ablkcipher_req = req;
431-
vc_req->encrypt = true;
432488
vc_req->dataq = data_vq;
489+
vc_req->alg_cb = virtio_crypto_dataq_sym_callback;
490+
vc_sym_req->ablkcipher_ctx = ctx;
491+
vc_sym_req->ablkcipher_req = req;
492+
vc_sym_req->encrypt = true;
433493

434494
return crypto_transfer_cipher_request_to_engine(data_vq->engine, req);
435495
}
@@ -438,16 +498,18 @@ static int virtio_crypto_ablkcipher_decrypt(struct ablkcipher_request *req)
438498
{
439499
struct crypto_ablkcipher *atfm = crypto_ablkcipher_reqtfm(req);
440500
struct virtio_crypto_ablkcipher_ctx *ctx = crypto_ablkcipher_ctx(atfm);
441-
struct virtio_crypto_request *vc_req = ablkcipher_request_ctx(req);
501+
struct virtio_crypto_sym_request *vc_sym_req =
502+
ablkcipher_request_ctx(req);
503+
struct virtio_crypto_request *vc_req = &vc_sym_req->base;
442504
struct virtio_crypto *vcrypto = ctx->vcrypto;
443505
/* Use the first data virtqueue as default */
444506
struct data_queue *data_vq = &vcrypto->data_vq[0];
445507

446-
vc_req->ablkcipher_ctx = ctx;
447-
vc_req->ablkcipher_req = req;
448-
449-
vc_req->encrypt = false;
450508
vc_req->dataq = data_vq;
509+
vc_req->alg_cb = virtio_crypto_dataq_sym_callback;
510+
vc_sym_req->ablkcipher_ctx = ctx;
511+
vc_sym_req->ablkcipher_req = req;
512+
vc_sym_req->encrypt = false;
451513

452514
return crypto_transfer_cipher_request_to_engine(data_vq->engine, req);
453515
}
@@ -456,7 +518,7 @@ static int virtio_crypto_ablkcipher_init(struct crypto_tfm *tfm)
456518
{
457519
struct virtio_crypto_ablkcipher_ctx *ctx = crypto_tfm_ctx(tfm);
458520

459-
tfm->crt_ablkcipher.reqsize = sizeof(struct virtio_crypto_request);
521+
tfm->crt_ablkcipher.reqsize = sizeof(struct virtio_crypto_sym_request);
460522
ctx->tfm = tfm;
461523

462524
return 0;
@@ -479,11 +541,13 @@ int virtio_crypto_ablkcipher_crypt_req(
479541
struct crypto_engine *engine,
480542
struct ablkcipher_request *req)
481543
{
482-
struct virtio_crypto_request *vc_req = ablkcipher_request_ctx(req);
544+
struct virtio_crypto_sym_request *vc_sym_req =
545+
ablkcipher_request_ctx(req);
546+
struct virtio_crypto_request *vc_req = &vc_sym_req->base;
483547
struct data_queue *data_vq = vc_req->dataq;
484548
int ret;
485549

486-
ret = __virtio_crypto_ablkcipher_do_req(vc_req, req, data_vq);
550+
ret = __virtio_crypto_ablkcipher_do_req(vc_sym_req, req, data_vq);
487551
if (ret < 0)
488552
return ret;
489553

@@ -492,14 +556,15 @@ int virtio_crypto_ablkcipher_crypt_req(
492556
return 0;
493557
}
494558

495-
void virtio_crypto_ablkcipher_finalize_req(
496-
struct virtio_crypto_request *vc_req,
559+
static void virtio_crypto_ablkcipher_finalize_req(
560+
struct virtio_crypto_sym_request *vc_sym_req,
497561
struct ablkcipher_request *req,
498562
int err)
499563
{
500-
crypto_finalize_cipher_request(vc_req->dataq->engine, req, err);
501-
502-
virtcrypto_clear_request(vc_req);
564+
crypto_finalize_cipher_request(vc_sym_req->base.dataq->engine,
565+
req, err);
566+
kzfree(vc_sym_req->iv);
567+
virtcrypto_clear_request(&vc_sym_req->base);
503568
}
504569

505570
static struct crypto_alg virtio_crypto_algs[] = { {

‎drivers/crypto/virtio/virtio_crypto_common.h‎

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -83,26 +83,16 @@ struct virtio_crypto_sym_session_info {
8383
__u64 session_id;
8484
};
8585

86-
struct virtio_crypto_ablkcipher_ctx {
87-
struct virtio_crypto *vcrypto;
88-
struct crypto_tfm *tfm;
89-
90-
struct virtio_crypto_sym_session_info enc_sess_info;
91-
struct virtio_crypto_sym_session_info dec_sess_info;
92-
};
86+
struct virtio_crypto_request;
87+
typedef void (*virtio_crypto_data_callback)
88+
(struct virtio_crypto_request *vc_req, int len);
9389

9490
struct virtio_crypto_request {
95-
/* Cipher or aead */
96-
uint32_t type;
9791
uint8_t status;
98-
struct virtio_crypto_ablkcipher_ctx *ablkcipher_ctx;
99-
struct ablkcipher_request *ablkcipher_req;
10092
struct virtio_crypto_op_data_req *req_data;
10193
struct scatterlist **sgs;
102-
uint8_t *iv;
103-
/* Encryption? */
104-
bool encrypt;
10594
struct data_queue *dataq;
95+
virtio_crypto_data_callback alg_cb;
10696
};
10797

10898
int virtcrypto_devmgr_add_dev(struct virtio_crypto *vcrypto_dev);
@@ -119,10 +109,6 @@ void virtcrypto_dev_stop(struct virtio_crypto *vcrypto);
119109
int virtio_crypto_ablkcipher_crypt_req(
120110
struct crypto_engine *engine,
121111
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);
126112

127113
void
128114
virtcrypto_clear_request(struct virtio_crypto_request *vc_req);

‎drivers/crypto/virtio/virtio_crypto_core.c‎

Lines changed: 7 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ void
2929
virtcrypto_clear_request(struct virtio_crypto_request *vc_req)
3030
{
3131
if (vc_req) {
32-
kzfree(vc_req->iv);
3332
kzfree(vc_req->req_data);
3433
kfree(vc_req->sgs);
3534
}
@@ -41,40 +40,18 @@ static void virtcrypto_dataq_callback(struct virtqueue *vq)
4140
struct virtio_crypto_request *vc_req;
4241
unsigned long flags;
4342
unsigned int len;
44-
struct ablkcipher_request *ablk_req;
45-
int error;
4643
unsigned int qid = vq->index;
4744

4845
spin_lock_irqsave(&vcrypto->data_vq[qid].lock, flags);
4946
do {
5047
virtqueue_disable_cb(vq);
5148
while ((vc_req = virtqueue_get_buf(vq, &len)) != NULL) {
52-
if (vc_req->type == VIRTIO_CRYPTO_SYM_OP_CIPHER) {
53-
switch (vc_req->status) {
54-
case VIRTIO_CRYPTO_OK:
55-
error = 0;
56-
break;
57-
case VIRTIO_CRYPTO_INVSESS:
58-
case VIRTIO_CRYPTO_ERR:
59-
error = -EINVAL;
60-
break;
61-
case VIRTIO_CRYPTO_BADMSG:
62-
error = -EBADMSG;
63-
break;
64-
default:
65-
error = -EIO;
66-
break;
67-
}
68-
ablk_req = vc_req->ablkcipher_req;
69-
70-
spin_unlock_irqrestore(
71-
&vcrypto->data_vq[qid].lock, flags);
72-
/* Finish the encrypt or decrypt process */
73-
virtio_crypto_ablkcipher_finalize_req(vc_req,
74-
ablk_req, error);
75-
spin_lock_irqsave(
76-
&vcrypto->data_vq[qid].lock, flags);
77-
}
49+
spin_unlock_irqrestore(
50+
&vcrypto->data_vq[qid].lock, flags);
51+
if (vc_req->alg_cb)
52+
vc_req->alg_cb(vc_req, len);
53+
spin_lock_irqsave(
54+
&vcrypto->data_vq[qid].lock, flags);
7855
}
7956
} while (!virtqueue_enable_cb(vq));
8057
spin_unlock_irqrestore(&vcrypto->data_vq[qid].lock, flags);
@@ -270,7 +247,7 @@ static int virtcrypto_update_status(struct virtio_crypto *vcrypto)
270247

271248
return -EPERM;
272249
}
273-
dev_info(&vcrypto->vdev->dev, "Accelerator is ready\n");
250+
dev_info(&vcrypto->vdev->dev, "Accelerator device is ready\n");
274251
} else {
275252
virtcrypto_dev_stop(vcrypto);
276253
dev_info(&vcrypto->vdev->dev, "Accelerator is not ready\n");

0 commit comments

Comments
 (0)