2525#include "virtio_crypto_common.h"
2626
2727
28- static void
28+ void
2929virtcrypto_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 :
139150err_find :
140151 kfree (names );
141152err_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+
272315static 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 );
366416free_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