@@ -273,25 +273,17 @@ static void sec_release_qp_ctx(struct sec_ctx *ctx,
273273 hisi_qm_release_qp (qp_ctx -> qp );
274274}
275275
276- static int sec_skcipher_init (struct crypto_skcipher * tfm )
276+ static int sec_ctx_base_init (struct sec_ctx * ctx )
277277{
278- struct sec_ctx * ctx = crypto_skcipher_ctx (tfm );
279- struct sec_cipher_ctx * c_ctx ;
280278 struct sec_dev * sec ;
281- struct device * dev ;
282- struct hisi_qm * qm ;
283279 int i , ret ;
284280
285- crypto_skcipher_set_reqsize (tfm , sizeof (struct sec_req ));
286-
287281 sec = sec_find_device (cpu_to_node (smp_processor_id ()));
288282 if (!sec ) {
289283 pr_err ("Can not find proper Hisilicon SEC device!\n" );
290284 return - ENODEV ;
291285 }
292286 ctx -> sec = sec ;
293- qm = & sec -> qm ;
294- dev = & qm -> pdev -> dev ;
295287 ctx -> hlf_q_num = sec -> ctx_q_num >> 1 ;
296288
297289 /* Half of queue depth is taken as fake requests limit in the queue. */
@@ -302,27 +294,12 @@ static int sec_skcipher_init(struct crypto_skcipher *tfm)
302294 return - ENOMEM ;
303295
304296 for (i = 0 ; i < sec -> ctx_q_num ; i ++ ) {
305- ret = sec_create_qp_ctx (qm , ctx , i , 0 );
297+ ret = sec_create_qp_ctx (& sec -> qm , ctx , i , 0 );
306298 if (ret )
307299 goto err_sec_release_qp_ctx ;
308300 }
309301
310- c_ctx = & ctx -> c_ctx ;
311- c_ctx -> ivsize = crypto_skcipher_ivsize (tfm );
312- if (c_ctx -> ivsize > SEC_IV_SIZE ) {
313- dev_err (dev , "get error iv size!\n" );
314- ret = - EINVAL ;
315- goto err_sec_release_qp_ctx ;
316- }
317- c_ctx -> c_key = dma_alloc_coherent (dev , SEC_MAX_KEY_SIZE ,
318- & c_ctx -> c_key_dma , GFP_KERNEL );
319- if (!c_ctx -> c_key ) {
320- ret = - ENOMEM ;
321- goto err_sec_release_qp_ctx ;
322- }
323-
324302 return 0 ;
325-
326303err_sec_release_qp_ctx :
327304 for (i = i - 1 ; i >= 0 ; i -- )
328305 sec_release_qp_ctx (ctx , & ctx -> qp_ctx [i ]);
@@ -331,24 +308,73 @@ static int sec_skcipher_init(struct crypto_skcipher *tfm)
331308 return ret ;
332309}
333310
334- static void sec_skcipher_uninit (struct crypto_skcipher * tfm )
311+ static void sec_ctx_base_uninit (struct sec_ctx * ctx )
335312{
336- struct sec_ctx * ctx = crypto_skcipher_ctx (tfm );
337- struct sec_cipher_ctx * c_ctx = & ctx -> c_ctx ;
338- int i = 0 ;
339-
340- if (c_ctx -> c_key ) {
341- dma_free_coherent (SEC_CTX_DEV (ctx ), SEC_MAX_KEY_SIZE ,
342- c_ctx -> c_key , c_ctx -> c_key_dma );
343- c_ctx -> c_key = NULL ;
344- }
313+ int i ;
345314
346315 for (i = 0 ; i < ctx -> sec -> ctx_q_num ; i ++ )
347316 sec_release_qp_ctx (ctx , & ctx -> qp_ctx [i ]);
348317
349318 kfree (ctx -> qp_ctx );
350319}
351320
321+ static int sec_cipher_init (struct sec_ctx * ctx )
322+ {
323+ struct sec_cipher_ctx * c_ctx = & ctx -> c_ctx ;
324+
325+ c_ctx -> c_key = dma_alloc_coherent (SEC_CTX_DEV (ctx ), SEC_MAX_KEY_SIZE ,
326+ & c_ctx -> c_key_dma , GFP_KERNEL );
327+ if (!c_ctx -> c_key )
328+ return - ENOMEM ;
329+
330+ return 0 ;
331+ }
332+
333+ static void sec_cipher_uninit (struct sec_ctx * ctx )
334+ {
335+ struct sec_cipher_ctx * c_ctx = & ctx -> c_ctx ;
336+
337+ memzero_explicit (c_ctx -> c_key , SEC_MAX_KEY_SIZE );
338+ dma_free_coherent (SEC_CTX_DEV (ctx ), SEC_MAX_KEY_SIZE ,
339+ c_ctx -> c_key , c_ctx -> c_key_dma );
340+ }
341+
342+ static int sec_skcipher_init (struct crypto_skcipher * tfm )
343+ {
344+ struct sec_ctx * ctx = crypto_skcipher_ctx (tfm );
345+ int ret ;
346+
347+ ctx = crypto_skcipher_ctx (tfm );
348+ crypto_skcipher_set_reqsize (tfm , sizeof (struct sec_req ));
349+ ctx -> c_ctx .ivsize = crypto_skcipher_ivsize (tfm );
350+ if (ctx -> c_ctx .ivsize > SEC_IV_SIZE ) {
351+ dev_err (SEC_CTX_DEV (ctx ), "get error skcipher iv size!\n" );
352+ return - EINVAL ;
353+ }
354+
355+ ret = sec_ctx_base_init (ctx );
356+ if (ret )
357+ return ret ;
358+
359+ ret = sec_cipher_init (ctx );
360+ if (ret )
361+ goto err_cipher_init ;
362+
363+ return 0 ;
364+ err_cipher_init :
365+ sec_ctx_base_uninit (ctx );
366+
367+ return ret ;
368+ }
369+
370+ static void sec_skcipher_uninit (struct crypto_skcipher * tfm )
371+ {
372+ struct sec_ctx * ctx = crypto_skcipher_ctx (tfm );
373+
374+ sec_cipher_uninit (ctx );
375+ sec_ctx_base_uninit (ctx );
376+ }
377+
352378static int sec_skcipher_3des_setkey (struct sec_cipher_ctx * c_ctx ,
353379 const u32 keylen ,
354380 const enum sec_cmode c_mode )
0 commit comments