10
10
#include <linux/dma-mapping.h>
11
11
#include <linux/fips.h>
12
12
#include <linux/module.h>
13
+ #include <linux/time.h>
13
14
#include "hpre.h"
14
15
15
16
struct hpre_ctx ;
@@ -32,6 +33,9 @@ struct hpre_ctx;
32
33
#define HPRE_SQE_DONE_SHIFT 30
33
34
#define HPRE_DH_MAX_P_SZ 512
34
35
36
+ #define HPRE_DFX_SEC_TO_US 1000000
37
+ #define HPRE_DFX_US_TO_NS 1000
38
+
35
39
typedef void (* hpre_cb )(struct hpre_ctx * ctx , void * sqe );
36
40
37
41
struct hpre_rsa_ctx {
@@ -68,6 +72,7 @@ struct hpre_dh_ctx {
68
72
struct hpre_ctx {
69
73
struct hisi_qp * qp ;
70
74
struct hpre_asym_request * * req_list ;
75
+ struct hpre * hpre ;
71
76
spinlock_t req_lock ;
72
77
unsigned int key_sz ;
73
78
bool crt_g2_mode ;
@@ -90,6 +95,7 @@ struct hpre_asym_request {
90
95
int err ;
91
96
int req_id ;
92
97
hpre_cb cb ;
98
+ struct timespec64 req_time ;
93
99
};
94
100
95
101
static DEFINE_MUTEX (hpre_alg_lock );
@@ -119,6 +125,7 @@ static void hpre_free_req_id(struct hpre_ctx *ctx, int req_id)
119
125
static int hpre_add_req_to_ctx (struct hpre_asym_request * hpre_req )
120
126
{
121
127
struct hpre_ctx * ctx ;
128
+ struct hpre_dfx * dfx ;
122
129
int id ;
123
130
124
131
ctx = hpre_req -> ctx ;
@@ -129,6 +136,10 @@ static int hpre_add_req_to_ctx(struct hpre_asym_request *hpre_req)
129
136
ctx -> req_list [id ] = hpre_req ;
130
137
hpre_req -> req_id = id ;
131
138
139
+ dfx = ctx -> hpre -> debug .dfx ;
140
+ if (atomic64_read (& dfx [HPRE_OVERTIME_THRHLD ].value ))
141
+ ktime_get_ts64 (& hpre_req -> req_time );
142
+
132
143
return id ;
133
144
}
134
145
@@ -309,12 +320,16 @@ static int hpre_alg_res_post_hf(struct hpre_ctx *ctx, struct hpre_sqe *sqe,
309
320
310
321
static int hpre_ctx_set (struct hpre_ctx * ctx , struct hisi_qp * qp , int qlen )
311
322
{
323
+ struct hpre * hpre ;
324
+
312
325
if (!ctx || !qp || qlen < 0 )
313
326
return - EINVAL ;
314
327
315
328
spin_lock_init (& ctx -> req_lock );
316
329
ctx -> qp = qp ;
317
330
331
+ hpre = container_of (ctx -> qp -> qm , struct hpre , qm );
332
+ ctx -> hpre = hpre ;
318
333
ctx -> req_list = kcalloc (qlen , sizeof (void * ), GFP_KERNEL );
319
334
if (!ctx -> req_list )
320
335
return - ENOMEM ;
@@ -337,38 +352,80 @@ static void hpre_ctx_clear(struct hpre_ctx *ctx, bool is_clear_all)
337
352
ctx -> key_sz = 0 ;
338
353
}
339
354
355
+ static bool hpre_is_bd_timeout (struct hpre_asym_request * req ,
356
+ u64 overtime_thrhld )
357
+ {
358
+ struct timespec64 reply_time ;
359
+ u64 time_use_us ;
360
+
361
+ ktime_get_ts64 (& reply_time );
362
+ time_use_us = (reply_time .tv_sec - req -> req_time .tv_sec ) *
363
+ HPRE_DFX_SEC_TO_US +
364
+ (reply_time .tv_nsec - req -> req_time .tv_nsec ) /
365
+ HPRE_DFX_US_TO_NS ;
366
+
367
+ if (time_use_us <= overtime_thrhld )
368
+ return false;
369
+
370
+ return true;
371
+ }
372
+
340
373
static void hpre_dh_cb (struct hpre_ctx * ctx , void * resp )
341
374
{
375
+ struct hpre_dfx * dfx = ctx -> hpre -> debug .dfx ;
342
376
struct hpre_asym_request * req ;
343
377
struct kpp_request * areq ;
378
+ u64 overtime_thrhld ;
344
379
int ret ;
345
380
346
381
ret = hpre_alg_res_post_hf (ctx , resp , (void * * )& req );
347
382
areq = req -> areq .dh ;
348
383
areq -> dst_len = ctx -> key_sz ;
384
+
385
+ overtime_thrhld = atomic64_read (& dfx [HPRE_OVERTIME_THRHLD ].value );
386
+ if (overtime_thrhld && hpre_is_bd_timeout (req , overtime_thrhld ))
387
+ atomic64_inc (& dfx [HPRE_OVER_THRHLD_CNT ].value );
388
+
349
389
hpre_hw_data_clr_all (ctx , req , areq -> dst , areq -> src );
350
390
kpp_request_complete (areq , ret );
391
+ atomic64_inc (& dfx [HPRE_RECV_CNT ].value );
351
392
}
352
393
353
394
static void hpre_rsa_cb (struct hpre_ctx * ctx , void * resp )
354
395
{
396
+ struct hpre_dfx * dfx = ctx -> hpre -> debug .dfx ;
355
397
struct hpre_asym_request * req ;
356
398
struct akcipher_request * areq ;
399
+ u64 overtime_thrhld ;
357
400
int ret ;
358
401
359
402
ret = hpre_alg_res_post_hf (ctx , resp , (void * * )& req );
403
+
404
+ overtime_thrhld = atomic64_read (& dfx [HPRE_OVERTIME_THRHLD ].value );
405
+ if (overtime_thrhld && hpre_is_bd_timeout (req , overtime_thrhld ))
406
+ atomic64_inc (& dfx [HPRE_OVER_THRHLD_CNT ].value );
407
+
360
408
areq = req -> areq .rsa ;
361
409
areq -> dst_len = ctx -> key_sz ;
362
410
hpre_hw_data_clr_all (ctx , req , areq -> dst , areq -> src );
363
411
akcipher_request_complete (areq , ret );
412
+ atomic64_inc (& dfx [HPRE_RECV_CNT ].value );
364
413
}
365
414
366
415
static void hpre_alg_cb (struct hisi_qp * qp , void * resp )
367
416
{
368
417
struct hpre_ctx * ctx = qp -> qp_ctx ;
418
+ struct hpre_dfx * dfx = ctx -> hpre -> debug .dfx ;
369
419
struct hpre_sqe * sqe = resp ;
420
+ struct hpre_asym_request * req = ctx -> req_list [le16_to_cpu (sqe -> tag )];
370
421
371
- ctx -> req_list [le16_to_cpu (sqe -> tag )]-> cb (ctx , resp );
422
+
423
+ if (unlikely (!req )) {
424
+ atomic64_inc (& dfx [HPRE_INVALID_REQ_CNT ].value );
425
+ return ;
426
+ }
427
+
428
+ req -> cb (ctx , resp );
372
429
}
373
430
374
431
static int hpre_ctx_init (struct hpre_ctx * ctx )
@@ -436,6 +493,29 @@ static int hpre_msg_request_set(struct hpre_ctx *ctx, void *req, bool is_rsa)
436
493
return 0 ;
437
494
}
438
495
496
+ static int hpre_send (struct hpre_ctx * ctx , struct hpre_sqe * msg )
497
+ {
498
+ struct hpre_dfx * dfx = ctx -> hpre -> debug .dfx ;
499
+ int ctr = 0 ;
500
+ int ret ;
501
+
502
+ do {
503
+ atomic64_inc (& dfx [HPRE_SEND_CNT ].value );
504
+ ret = hisi_qp_send (ctx -> qp , msg );
505
+ if (ret != - EBUSY )
506
+ break ;
507
+ atomic64_inc (& dfx [HPRE_SEND_BUSY_CNT ].value );
508
+ } while (ctr ++ < HPRE_TRY_SEND_TIMES );
509
+
510
+ if (likely (!ret ))
511
+ return ret ;
512
+
513
+ if (ret != - EBUSY )
514
+ atomic64_inc (& dfx [HPRE_SEND_FAIL_CNT ].value );
515
+
516
+ return ret ;
517
+ }
518
+
439
519
#ifdef CONFIG_CRYPTO_DH
440
520
static int hpre_dh_compute_value (struct kpp_request * req )
441
521
{
@@ -444,7 +524,6 @@ static int hpre_dh_compute_value(struct kpp_request *req)
444
524
void * tmp = kpp_request_ctx (req );
445
525
struct hpre_asym_request * hpre_req = PTR_ALIGN (tmp , HPRE_ALIGN_SZ );
446
526
struct hpre_sqe * msg = & hpre_req -> req ;
447
- int ctr = 0 ;
448
527
int ret ;
449
528
450
529
ret = hpre_msg_request_set (ctx , req , false);
@@ -465,11 +544,9 @@ static int hpre_dh_compute_value(struct kpp_request *req)
465
544
msg -> dw0 = cpu_to_le32 (le32_to_cpu (msg -> dw0 ) | HPRE_ALG_DH_G2 );
466
545
else
467
546
msg -> dw0 = cpu_to_le32 (le32_to_cpu (msg -> dw0 ) | HPRE_ALG_DH );
468
- do {
469
- ret = hisi_qp_send (ctx -> qp , msg );
470
- } while (ret == - EBUSY && ctr ++ < HPRE_TRY_SEND_TIMES );
471
547
472
548
/* success */
549
+ ret = hpre_send (ctx , msg );
473
550
if (likely (!ret ))
474
551
return - EINPROGRESS ;
475
552
@@ -647,7 +724,6 @@ static int hpre_rsa_enc(struct akcipher_request *req)
647
724
void * tmp = akcipher_request_ctx (req );
648
725
struct hpre_asym_request * hpre_req = PTR_ALIGN (tmp , HPRE_ALIGN_SZ );
649
726
struct hpre_sqe * msg = & hpre_req -> req ;
650
- int ctr = 0 ;
651
727
int ret ;
652
728
653
729
/* For 512 and 1536 bits key size, use soft tfm instead */
@@ -677,11 +753,8 @@ static int hpre_rsa_enc(struct akcipher_request *req)
677
753
if (unlikely (ret ))
678
754
goto clear_all ;
679
755
680
- do {
681
- ret = hisi_qp_send (ctx -> qp , msg );
682
- } while (ret == - EBUSY && ctr ++ < HPRE_TRY_SEND_TIMES );
683
-
684
756
/* success */
757
+ ret = hpre_send (ctx , msg );
685
758
if (likely (!ret ))
686
759
return - EINPROGRESS ;
687
760
@@ -699,7 +772,6 @@ static int hpre_rsa_dec(struct akcipher_request *req)
699
772
void * tmp = akcipher_request_ctx (req );
700
773
struct hpre_asym_request * hpre_req = PTR_ALIGN (tmp , HPRE_ALIGN_SZ );
701
774
struct hpre_sqe * msg = & hpre_req -> req ;
702
- int ctr = 0 ;
703
775
int ret ;
704
776
705
777
/* For 512 and 1536 bits key size, use soft tfm instead */
@@ -736,11 +808,8 @@ static int hpre_rsa_dec(struct akcipher_request *req)
736
808
if (unlikely (ret ))
737
809
goto clear_all ;
738
810
739
- do {
740
- ret = hisi_qp_send (ctx -> qp , msg );
741
- } while (ret == - EBUSY && ctr ++ < HPRE_TRY_SEND_TIMES );
742
-
743
811
/* success */
812
+ ret = hpre_send (ctx , msg );
744
813
if (likely (!ret ))
745
814
return - EINPROGRESS ;
746
815
0 commit comments