forked from intel/QAT_Engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathe_qat.c
1390 lines (1230 loc) · 47.5 KB
/
e_qat.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* ====================================================================
*
*
* BSD LICENSE
*
* Copyright(c) 2016-2019 Intel Corporation.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Intel Corporation nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*
* ====================================================================
*/
/*****************************************************************************
* @file e_qat.c
*
* This file provides a OpenSSL engine for the quick assist API
*
*****************************************************************************/
/* macros defined to allow use of the cpu get and set affinity functions */
#ifndef _GNU_SOURCE
# define _GNU_SOURCE
#endif
#ifndef __USE_GNU
# define __USE_GNU
#endif
/* Defines */
#if defined(USE_QAT_CONTIG_MEM) && !defined(USE_QAE_MEM)
# define QAT_DEV "/dev/qat_contig_mem"
#elif defined(USE_QAE_MEM) && !defined(USE_QAT_CONTIG_MEM)
# define QAT_DEV "/dev/usdm_drv"
#elif defined(USE_QAE_MEM) && defined(USE_QAT_CONFIG_MEM)
# error "USE_QAT_CONTIG_MEM and USE_QAE_MEM both defined"
#else
# error "No memory driver type defined"
#endif
#define NANOSECONDS_TO_MICROSECONDS 1000
/* Standard Includes */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <unistd.h>
#include <ctype.h>
#include <fcntl.h>
#include <sys/epoll.h>
#include <sys/types.h>
#include <sys/eventfd.h>
#include <unistd.h>
#include <signal.h>
#include <time.h>
/* Local Includes */
#include "e_qat.h"
#include "qat_fork.h"
#include "qat_events.h"
#include "qat_callback.h"
#include "qat_polling.h"
#include "qat_ciphers.h"
#include "qat_rsa.h"
#include "qat_dsa.h"
#include "qat_dh.h"
#include "qat_ec.h"
#include "qat_utils.h"
#include "e_qat_err.h"
#include "qat_prf.h"
/* OpenSSL Includes */
#include <openssl/err.h>
#if OPENSSL_VERSION_NUMBER >= 0x10100000L
#include <openssl/async.h>
#endif
#include <openssl/objects.h>
#include <openssl/crypto.h>
/* QAT includes */
#ifdef USE_QAT_CONTIG_MEM
# include "qae_mem_utils.h"
#endif
#ifdef USE_QAE_MEM
# include "cmn_mem_drv_inf.h"
#endif
#include "cpa.h"
#include "cpa_cy_im.h"
#include "cpa_cy_common.h"
#include "cpa_types.h"
#include "icp_sal_user.h"
#include "icp_sal_poll.h"
#include "qat_parseconf.h"
#define QAT_MAX_INPUT_STRING_LENGTH 1024
/* Qat engine id declaration */
const char *engine_qat_id = "qat";
const char *engine_qat_name =
"Reference implementation of QAT crypto engine";
#define QAT_CONFIG_SECTION_NAME_SIZE 64
char qat_config_section_name[QAT_CONFIG_SECTION_NAME_SIZE] = "SHIM";
char *ICPConfigSectionName_libcrypto = qat_config_section_name;
CpaInstanceHandle *qat_instance_handles = NULL;
Cpa16U qat_num_instances = 0;
Cpa32U qat_num_devices = 0;
pthread_key_t thread_local_variables;
pthread_t polling_thread;
int keep_polling = 1;
int enable_external_polling = 0;
int enable_inline_polling = 0;
int enable_event_driven_polling = 0;
int enable_heuristic_polling = 0;
int enable_instance_for_thread = 0;
int enable_sw_fallback = 0;
int disable_qat_offload = 0;
pthread_mutex_t qat_instance_mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t qat_engine_mutex = PTHREAD_MUTEX_INITIALIZER;
unsigned int engine_inited = 0;
qat_instance_details_t qat_instance_details[QAT_MAX_CRYPTO_INSTANCES] = {{{0}}};
qat_accel_details_t qat_accel_details[QAT_MAX_CRYPTO_ACCELERATORS] = {{0}};
useconds_t qat_poll_interval = QAT_POLL_PERIOD_IN_NS;
int qat_epoll_timeout = QAT_EPOLL_TIMEOUT_IN_MS;
int qat_max_retry_count = QAT_CRYPTO_NUM_POLLING_RETRIES;
int num_requests_in_flight = 0;
int num_asym_requests_in_flight = 0;
int num_prf_requests_in_flight = 0;
int num_cipher_pipeline_requests_in_flight = 0;
sigset_t set = {{0}};
pthread_t timer_poll_func_thread = 0;
int cleared_to_start = 0;
int qat_get_qat_offload_disabled(void)
{
if (disable_qat_offload ||
(qat_get_sw_fallback_enabled() && !is_any_device_available()))
return 1;
else
return 0;
}
int qat_get_sw_fallback_enabled(void)
{
return enable_sw_fallback;
}
/******************************************************************************
* function:
* qat_engine_finish(ENGINE *e)
*
* @param e [IN] - OpenSSL engine pointer
*
* description:
* Qat engine finish function with standard signature.
* This is a wrapper for qat_engine_finish_int that always resets all the
* global variables used to store the engine configuration.
******************************************************************************/
static int qat_engine_finish(ENGINE *e);
static inline int qat_use_signals_no_engine_start(void)
{
return (int)timer_poll_func_thread;
}
int qat_use_signals(void)
{
/* We check engine_inited outside of a mutex here because it is more
efficient and we are only interested in the state if it hasn't been
initialised. The usual case is that the engine will have been
initialised and we can carry on without locking. If the engine hasn't
been initialised then there will be a further check within
qat_engine_init inside a mutex to prevent a race condition. */
if (unlikely(!engine_inited)) {
ENGINE* e = ENGINE_by_id(engine_qat_id);
if (e == NULL) {
WARN("Function ENGINE_by_id returned NULL\n");
return 0;
}
if (!qat_engine_init(e)) {
WARN("Failure in qat_engine_init function\n");
ENGINE_free(e);
return 0;
}
ENGINE_free(e);
}
return qat_use_signals_no_engine_start();
}
static int validate_configuration_section_name(const char *name)
{
int len;
if (name == NULL) {
WARN("Section name is NULL\n");
return 0;
}
len = strlen(name);
if (len == 0 || len >= QAT_CONFIG_SECTION_NAME_SIZE) {
WARN("Invalid section name length %d\n", len);
return 0;
}
return 1;
}
int is_instance_available(int inst_num)
{
if (inst_num > qat_num_instances)
return 0;
if (!qat_instance_details[inst_num].qat_instance_started)
return 0;
return !qat_accel_details[qat_instance_details[inst_num].
qat_instance_info.
physInstId.packageId].qat_accel_reset_status;
}
int is_any_device_available(void)
{
int device_num = 0;
if (qat_num_devices == 0)
return 0;
for (device_num = 0; device_num < qat_num_devices; device_num++) {
if (qat_accel_details[device_num].qat_accel_reset_status == 0) {
return 1;
}
}
return 0;
}
int get_next_inst_num(void)
{
int inst_num = QAT_INVALID_INSTANCE;
unsigned int inst_count = 0;
thread_local_variables_t * tlv = NULL;
/* See qat_use_signals() above for more info on why it is safe to
check engine_inited outside of a mutex in this case. */
if (unlikely(!engine_inited)) {
ENGINE* e = ENGINE_by_id(engine_qat_id);
if (e == NULL) {
WARN("Function ENGINE_by_id returned NULL\n");
return inst_num;
}
if (!qat_engine_init(e)) {
WARN("Failure in qat_engine_init function\n");
ENGINE_free(e);
return inst_num;
}
ENGINE_free(e);
}
tlv = qat_check_create_local_variables();
if (unlikely(NULL == tlv)) {
WARN("No local variables are available\n");
return inst_num;
}
if (0 == enable_instance_for_thread) {
if (likely(qat_instance_handles && qat_num_instances)) {
do {
inst_count++;
tlv->qatInstanceNumForThread = (tlv->qatInstanceNumForThread + 1) %
qat_num_instances;
} while (!is_instance_available(tlv->qatInstanceNumForThread) &&
inst_count <= qat_num_instances);
if (likely(inst_count <= qat_num_instances)) {
inst_num = tlv->qatInstanceNumForThread;
}
}
} else {
if (tlv->qatInstanceNumForThread != QAT_INVALID_INSTANCE) {
if (is_instance_available(tlv->qatInstanceNumForThread)) {
inst_num = tlv->qatInstanceNumForThread;
}
}
}
/* If no working instance could be found then flag a warning */
if (unlikely(inst_num == QAT_INVALID_INSTANCE)) {
WARN("No working instance is available\n");
}
return inst_num;
}
/******************************************************************************
* function:
* virtualToPhysical(void *virtualAddr)
*
* @param virtualAddr [IN] - Virtual address.
*
* description:
* Translates virtual address to hardware physical address. See the qae_mem
* module for more details. The virtual to physical translator is required
* by the QAT hardware to map from virtual addresses to physical locations
* in pinned memory.
*
* This function is designed to work with the allocator defined in
* qae_mem_utils.c and qat_contig_mem/qat_contig_mem.c
*
******************************************************************************/
static CpaPhysicalAddr virtualToPhysical(void *virtualAddr)
{
return qaeCryptoMemV2P(virtualAddr);
}
thread_local_variables_t * qat_check_create_local_variables(void)
{
thread_local_variables_t * tlv =
(thread_local_variables_t *)pthread_getspecific(thread_local_variables);
if (tlv != NULL)
return tlv;
tlv = OPENSSL_zalloc(sizeof(thread_local_variables_t));
if (tlv != NULL) {
tlv->qatInstanceNumForThread = QAT_INVALID_INSTANCE;
pthread_setspecific(thread_local_variables, (void *)tlv);
}
return tlv;
}
/******************************************************************************
* function:
* qat_local_variable_destructor(void *tlv)
*
* description:
* This is a cleanup callback function registered when pthread_key_create()
* is called. It will get called when the thread is destroyed and will
* cleanup the thread local variables.
*
*****************************************************************************/
static void qat_local_variable_destructor(void *tlv)
{
if (tlv)
OPENSSL_free(tlv);
pthread_setspecific(thread_local_variables, NULL);
}
#ifdef OPENSSL_ENABLE_QAT_UPSTREAM_DRIVER
void qat_instance_notification_callbackFn(const CpaInstanceHandle ih, void *callbackTag,
const CpaInstanceEvent inst_ev)
{
Cpa32U packageId;
struct timespec ts = { 0 };
switch (inst_ev) {
case CPA_INSTANCE_EVENT_FATAL_ERROR:
WARN("Received Callback that instance %ld is unavailable\n",
(intptr_t)callbackTag);
packageId =
qat_instance_details[(intptr_t)callbackTag].qat_instance_info.physInstId.packageId;
qat_accel_details[packageId].qat_accel_reset_status = 1;
clock_gettime(CLOCK_MONOTONIC_RAW, &ts);
CRYPTO_QAT_LOG("[%lld.%06ld] Instance: %ld Handle %p Device %d RESTARTING \n",
(long long)ts.tv_sec, ts.tv_nsec / NANOSECONDS_TO_MICROSECONDS,
(intptr_t)callbackTag, ih, packageId);
break;
case CPA_INSTANCE_EVENT_RESTARTING:
WARN("Received Callback that instance %ld is restarting\n",
(intptr_t)callbackTag);
break;
case CPA_INSTANCE_EVENT_RESTARTED:
WARN("Received Callback that instance %ld is available\n",
(intptr_t)callbackTag);
packageId =
qat_instance_details[(intptr_t)callbackTag].qat_instance_info.physInstId.packageId;
qat_accel_details[packageId].qat_accel_reset_status = 0;
clock_gettime(CLOCK_MONOTONIC_RAW, &ts);
CRYPTO_QAT_LOG("[%lld.%06ld] Instance: %ld Handle %p Device %d RESTARTED \n",
(long long)ts.tv_sec, ts.tv_nsec / NANOSECONDS_TO_MICROSECONDS,
(intptr_t)callbackTag, ih, packageId);
break;
default:
WARN("Fatal Error detected for instance: %ld\n", (intptr_t)callbackTag);
break;
}
}
#endif
int qat_engine_init(ENGINE *e)
{
int instNum, err;
CpaStatus status = CPA_STATUS_SUCCESS;
CpaBoolean limitDevAccess = CPA_FALSE;
int ret_pthread_sigmask;
Cpa32U package_id = 0;
pthread_mutex_lock(&qat_engine_mutex);
if (engine_inited) {
pthread_mutex_unlock(&qat_engine_mutex);
return 1;
}
DEBUG("QAT Engine initialization:\n");
DEBUG("- External polling: %s\n", enable_external_polling ? "ON": "OFF");
DEBUG("- SW Fallback: %s\n", enable_sw_fallback ? "ON": "OFF");
DEBUG("- Inline polling: %s\n", enable_inline_polling ? "ON": "OFF");
DEBUG("- Internal poll interval: %dns\n", qat_poll_interval);
DEBUG("- Epoll timeout: %dms\n", qat_epoll_timeout);
DEBUG("- Event driven polling mode: %s\n", enable_event_driven_polling ? "ON": "OFF");
DEBUG("- Instance for thread: %s\n", enable_instance_for_thread ? "ON": "OFF");
DEBUG("- Max retry count: %d\n", qat_max_retry_count);
CRYPTO_INIT_QAT_LOG();
polling_thread = pthread_self();
if ((err = pthread_key_create(&thread_local_variables, qat_local_variable_destructor)) != 0) {
WARN("pthread_key_create failed: %s\n", strerror(err));
QATerr(QAT_F_QAT_ENGINE_INIT, QAT_R_PTHREAD_CREATE_FAILURE);
pthread_mutex_unlock(&qat_engine_mutex);
return 0;
}
#ifndef OPENSSL_ENABLE_QAT_UPSTREAM_DRIVER
/* limitDevAccess is passed as an input to icp_sal_userStartMultiProcess().
* However, in upstream driver the value is ignored and read directly from
* the configuration file -> No need to parse the file here.
*/
if (!checkLimitDevAccessValue((int *)&limitDevAccess,
ICPConfigSectionName_libcrypto)) {
WARN("Could not load driver config file. Assuming LimitDevAccess = 0\n");
}
#endif
/* Initialise the QAT hardware */
if (CPA_STATUS_SUCCESS !=
icp_sal_userStartMultiProcess(ICPConfigSectionName_libcrypto,
limitDevAccess)) {
WARN("icp_sal_userStart failed\n");
QATerr(QAT_F_QAT_ENGINE_INIT, QAT_R_ICP_SAL_USERSTART_FAIL);
pthread_mutex_unlock(&qat_engine_mutex);
return 0;
}
/* Get the number of available instances */
status = cpaCyGetNumInstances(&qat_num_instances);
if (CPA_STATUS_SUCCESS != status) {
WARN("cpaCyGetNumInstances failed, status=%d\n", status);
QATerr(QAT_F_QAT_ENGINE_INIT, QAT_R_GET_NUM_INSTANCE_FAILURE);
pthread_mutex_unlock(&qat_engine_mutex);
qat_engine_finish(e);
return 0;
}
if (!qat_num_instances) {
WARN("No crypto instances found\n");
QATerr(QAT_F_QAT_ENGINE_INIT, QAT_R_INSTANCE_UNAVAILABLE);
pthread_mutex_unlock(&qat_engine_mutex);
qat_engine_finish(e);
return 0;
}
DEBUG("Found %d Cy instances\n", qat_num_instances);
/* Allocate memory for the instance handle array */
qat_instance_handles =
(CpaInstanceHandle *) OPENSSL_zalloc(((int)qat_num_instances) *
sizeof(CpaInstanceHandle));
if (NULL == qat_instance_handles) {
WARN("OPENSSL_zalloc() failed for instance handles.\n");
QATerr(QAT_F_QAT_ENGINE_INIT, QAT_R_INSTANCE_HANDLE_MALLOC_FAILURE);
pthread_mutex_unlock(&qat_engine_mutex);
qat_engine_finish(e);
return 0;
}
/* Get the Cy instances */
status = cpaCyGetInstances(qat_num_instances, qat_instance_handles);
if (CPA_STATUS_SUCCESS != status) {
WARN("cpaCyGetInstances failed, status=%d\n", status);
QATerr(QAT_F_QAT_ENGINE_INIT, QAT_R_GET_INSTANCE_FAILURE);
pthread_mutex_unlock(&qat_engine_mutex);
qat_engine_finish(e);
return 0;
}
if (!enable_external_polling && !enable_inline_polling) {
if (qat_is_event_driven()) {
CpaStatus status;
int flags;
int engine_fd;
/* Add the file descriptor to an epoll event list */
internal_efd = epoll_create1(0);
if (-1 == internal_efd) {
WARN("Error creating epoll fd\n");
QATerr(QAT_F_QAT_ENGINE_INIT, QAT_R_EPOLL_CREATE_FAILURE);
pthread_mutex_unlock(&qat_engine_mutex);
qat_engine_finish(e);
return 0;
}
for (instNum = 0; instNum < qat_num_instances; instNum++) {
/* Get the file descriptor for the instance */
status =
icp_sal_CyGetFileDescriptor(qat_instance_handles[instNum],
&engine_fd);
if (CPA_STATUS_FAIL == status) {
WARN("Error getting file descriptor for instance\n");
QATerr(QAT_F_QAT_ENGINE_INIT, QAT_R_GET_FILE_DESCRIPTOR_FAILURE);
pthread_mutex_unlock(&qat_engine_mutex);
qat_engine_finish(e);
return 0;
}
/* Make the file descriptor non-blocking */
eng_poll_st[instNum].eng_fd = engine_fd;
eng_poll_st[instNum].inst_index = instNum;
flags = fcntl(engine_fd, F_GETFL, 0);
fcntl(engine_fd, F_SETFL, flags | O_NONBLOCK);
eng_epoll_events[instNum].data.ptr = &eng_poll_st[instNum];
eng_epoll_events[instNum].events = EPOLLIN | EPOLLET;
if (-1 ==
epoll_ctl(internal_efd, EPOLL_CTL_ADD, engine_fd,
&eng_epoll_events[instNum])) {
WARN("Error adding fd to epoll\n");
QATerr(QAT_F_QAT_ENGINE_INIT, QAT_R_EPOLL_CTL_FAILURE);
pthread_mutex_unlock(&qat_engine_mutex);
qat_engine_finish(e);
return 0;
}
}
}
}
/* Set translation function and start each instance */
for (instNum = 0; instNum < qat_num_instances; instNum++) {
/* Retrieve CpaInstanceInfo2 structure for that instance */
status = cpaCyInstanceGetInfo2(qat_instance_handles[instNum],
&qat_instance_details[instNum].qat_instance_info);
if (CPA_STATUS_SUCCESS != status ) {
WARN("cpaCyInstanceGetInfo2 failed. status = %d\n", status);
QATerr(QAT_F_QAT_ENGINE_INIT, QAT_R_GET_INSTANCE_INFO_FAILURE);
pthread_mutex_unlock(&qat_engine_mutex);
qat_engine_finish(e);
return 0;
}
package_id = qat_instance_details[instNum].qat_instance_info.physInstId.packageId;
qat_accel_details[package_id].qat_accel_present = 1;
if (package_id >= qat_num_devices) {
qat_num_devices = package_id + 1;
}
/* Set the address translation function */
status = cpaCySetAddressTranslation(qat_instance_handles[instNum],
virtualToPhysical);
if (CPA_STATUS_SUCCESS != status) {
WARN("cpaCySetAddressTranslation failed, status=%d\n", status);
QATerr(QAT_F_QAT_ENGINE_INIT, QAT_R_SET_ADDRESS_TRANSLATION_FAILURE);
pthread_mutex_unlock(&qat_engine_mutex);
qat_engine_finish(e);
return 0;
}
/* Start the instances */
status = cpaCyStartInstance(qat_instance_handles[instNum]);
if (CPA_STATUS_SUCCESS != status) {
WARN("cpaCyStartInstance failed, status=%d\n", status);
QATerr(QAT_F_QAT_ENGINE_INIT, QAT_R_START_INSTANCE_FAILURE);
pthread_mutex_unlock(&qat_engine_mutex);
qat_engine_finish(e);
return 0;
}
qat_instance_details[instNum].qat_instance_started = 1;
DEBUG("Started Instance No: %d Located on Device: %d\n", instNum, package_id);
#ifdef OPENSSL_ENABLE_QAT_UPSTREAM_DRIVER
if (enable_sw_fallback) {
DEBUG("cpaCyInstanceSetNotificationCb instNum = %d\n", instNum);
status = cpaCyInstanceSetNotificationCb(qat_instance_handles[instNum],
qat_instance_notification_callbackFn,
(void *)(intptr_t)instNum);
if (CPA_STATUS_SUCCESS != status) {
WARN("cpaCyInstanceSetNotificationCb failed, status=%d\n", status);
QATerr(QAT_F_QAT_ENGINE_INIT, QAT_R_SET_NOTIFICATION_CALLBACK_FAILURE);
pthread_mutex_unlock(&qat_engine_mutex);
qat_engine_finish(e);
return 0;
}
}
#endif
}
if (!enable_external_polling && !enable_inline_polling) {
if (!qat_is_event_driven()) {
sigemptyset(&set);
sigaddset(&set, SIGUSR1);
ret_pthread_sigmask = pthread_sigmask(SIG_BLOCK, &set, NULL);
if (ret_pthread_sigmask != 0) {
WARN("pthread_sigmask error\n");
QATerr(QAT_F_QAT_ENGINE_INIT, QAT_R_POLLING_THREAD_SIGMASK_FAILURE);
pthread_mutex_unlock(&qat_engine_mutex);
qat_engine_finish(e);
return 0;
}
}
if (qat_create_thread(&polling_thread, NULL,
qat_is_event_driven() ? event_poll_func : timer_poll_func, NULL)) {
WARN("Creation of polling thread failed\n");
QATerr(QAT_F_QAT_ENGINE_INIT, QAT_R_POLLING_THREAD_CREATE_FAILURE);
polling_thread = pthread_self();
pthread_mutex_unlock(&qat_engine_mutex);
qat_engine_finish(e);
return 0;
}
if (qat_adjust_thread_affinity(polling_thread) == 0) {
WARN("Setting polling thread affinity failed\n");
QATerr(QAT_F_QAT_ENGINE_INIT, QAT_R_SET_POLLING_THREAD_AFFINITY_FAILURE);
pthread_mutex_unlock(&qat_engine_mutex);
qat_engine_finish(e);
return 0;
}
if (!qat_is_event_driven()) {
while (!cleared_to_start)
sleep(1);
}
}
engine_inited = 1;
pthread_mutex_unlock(&qat_engine_mutex);
return 1;
}
#define QAT_CMD_ENABLE_EXTERNAL_POLLING ENGINE_CMD_BASE
#define QAT_CMD_POLL (ENGINE_CMD_BASE + 1)
#define QAT_CMD_SET_INSTANCE_FOR_THREAD (ENGINE_CMD_BASE + 2)
#define QAT_CMD_GET_NUM_OP_RETRIES (ENGINE_CMD_BASE + 3)
#define QAT_CMD_SET_MAX_RETRY_COUNT (ENGINE_CMD_BASE + 4)
#define QAT_CMD_SET_INTERNAL_POLL_INTERVAL (ENGINE_CMD_BASE + 5)
#define QAT_CMD_GET_EXTERNAL_POLLING_FD (ENGINE_CMD_BASE + 6)
#define QAT_CMD_ENABLE_EVENT_DRIVEN_POLLING_MODE (ENGINE_CMD_BASE + 7)
#define QAT_CMD_GET_NUM_CRYPTO_INSTANCES (ENGINE_CMD_BASE + 8)
#define QAT_CMD_DISABLE_EVENT_DRIVEN_POLLING_MODE (ENGINE_CMD_BASE + 9)
#define QAT_CMD_SET_EPOLL_TIMEOUT (ENGINE_CMD_BASE + 10)
#define QAT_CMD_SET_CRYPTO_SMALL_PACKET_OFFLOAD_THRESHOLD (ENGINE_CMD_BASE + 11)
#define QAT_CMD_ENABLE_INLINE_POLLING (ENGINE_CMD_BASE + 12)
#define QAT_CMD_ENABLE_HEURISTIC_POLLING (ENGINE_CMD_BASE + 13)
#define QAT_CMD_GET_NUM_REQUESTS_IN_FLIGHT (ENGINE_CMD_BASE + 14)
#define QAT_CMD_INIT_ENGINE (ENGINE_CMD_BASE + 15)
#define QAT_CMD_SET_CONFIGURATION_SECTION_NAME (ENGINE_CMD_BASE + 16)
#define QAT_CMD_ENABLE_SW_FALLBACK (ENGINE_CMD_BASE + 17)
#define QAT_CMD_HEARTBEAT_POLL (ENGINE_CMD_BASE + 18)
#define QAT_CMD_DISABLE_QAT_OFFLOAD (ENGINE_CMD_BASE + 19)
static const ENGINE_CMD_DEFN qat_cmd_defns[] = {
{
QAT_CMD_ENABLE_EXTERNAL_POLLING,
"ENABLE_EXTERNAL_POLLING",
"Enables the external polling interface to the engine.",
ENGINE_CMD_FLAG_NO_INPUT},
{
QAT_CMD_POLL,
"POLL",
"Polls the engine for any completed requests",
ENGINE_CMD_FLAG_NO_INPUT},
{
QAT_CMD_SET_INSTANCE_FOR_THREAD,
"SET_INSTANCE_FOR_THREAD",
"Set instance to be used by this thread",
ENGINE_CMD_FLAG_NUMERIC},
{
QAT_CMD_GET_NUM_OP_RETRIES,
"GET_NUM_OP_RETRIES",
"Get number of retries",
ENGINE_CMD_FLAG_NO_INPUT},
{
QAT_CMD_SET_MAX_RETRY_COUNT,
"SET_MAX_RETRY_COUNT",
"Set maximum retry count",
ENGINE_CMD_FLAG_NUMERIC},
{
QAT_CMD_SET_INTERNAL_POLL_INTERVAL,
"SET_INTERNAL_POLL_INTERVAL",
"Set internal polling interval",
ENGINE_CMD_FLAG_NUMERIC},
{
QAT_CMD_GET_EXTERNAL_POLLING_FD,
"GET_EXTERNAL_POLLING_FD",
"Returns non blocking fd for crypto engine",
ENGINE_CMD_FLAG_NO_INPUT},
{
QAT_CMD_ENABLE_EVENT_DRIVEN_POLLING_MODE,
"ENABLE_EVENT_DRIVEN_POLLING_MODE",
"Set event driven polling mode",
ENGINE_CMD_FLAG_NO_INPUT},
{
QAT_CMD_GET_NUM_CRYPTO_INSTANCES,
"GET_NUM_CRYPTO_INSTANCES",
"Get the number of crypto instances",
ENGINE_CMD_FLAG_NO_INPUT},
{
QAT_CMD_DISABLE_EVENT_DRIVEN_POLLING_MODE,
"DISABLE_EVENT_DRIVEN_POLLING_MODE",
"Unset event driven polling mode",
ENGINE_CMD_FLAG_NO_INPUT},
{
QAT_CMD_SET_EPOLL_TIMEOUT,
"SET_EPOLL_TIMEOUT",
"Set epoll_wait timeout",
ENGINE_CMD_FLAG_NUMERIC},
{
QAT_CMD_SET_CRYPTO_SMALL_PACKET_OFFLOAD_THRESHOLD,
"SET_CRYPTO_SMALL_PACKET_OFFLOAD_THRESHOLD",
"Set QAT small packet threshold",
ENGINE_CMD_FLAG_STRING},
{
QAT_CMD_ENABLE_INLINE_POLLING,
"ENABLE_INLINE_POLLING",
"Enables the inline polling mode.",
ENGINE_CMD_FLAG_NO_INPUT},
{
QAT_CMD_ENABLE_HEURISTIC_POLLING,
"ENABLE_HEURISTIC_POLLING",
"Enable the heuristic polling mode",
ENGINE_CMD_FLAG_NO_INPUT},
{
QAT_CMD_GET_NUM_REQUESTS_IN_FLIGHT,
"GET_NUM_REQUESTS_IN_FLIGHT",
"Get the number of in-flight requests",
ENGINE_CMD_FLAG_NUMERIC},
{
QAT_CMD_INIT_ENGINE,
"INIT_ENGINE",
"Initializes the engine if not already initialized",
ENGINE_CMD_FLAG_NO_INPUT},
{
QAT_CMD_SET_CONFIGURATION_SECTION_NAME,
"SET_CONFIGURATION_SECTION_NAME",
"Set the configuration section to use in QAT driver configuration file",
ENGINE_CMD_FLAG_STRING},
{
QAT_CMD_ENABLE_SW_FALLBACK,
"ENABLE_SW_FALLBACK",
"Enables the fallback to SW if the acceleration devices go offline",
ENGINE_CMD_FLAG_NO_INPUT},
{
QAT_CMD_HEARTBEAT_POLL,
"HEARTBEAT_POLL",
"Check the acceleration devices are still functioning",
ENGINE_CMD_FLAG_NO_INPUT},
{
QAT_CMD_DISABLE_QAT_OFFLOAD,
"DISABLE_QAT_OFFLOAD",
"Perform crypto operations on core",
ENGINE_CMD_FLAG_NO_INPUT},
{0, NULL, NULL, 0}
};
/******************************************************************************
* function:
* qat_engine_ctrl(ENGINE *e, int cmd, long i,
* void *p, void (*f)(void))
*
* @param e [IN] - OpenSSL engine pointer
* @param cmd [IN] - Control Command
* @param i [IN] - Unused
* @param p [IN] - Parameters for the command
* @param f [IN] - Callback function
*
* description:
* Qat engine control functions.
* Note: QAT_CMD_ENABLE_EXTERNAL_POLLING should be called at the following
* point during startup:
* ENGINE_load_qat
* ENGINE_by_id
* ---> ENGINE_ctrl_cmd(QAT_CMD_ENABLE_EXTERNAL_POLLING)
* ENGINE_init
******************************************************************************/
static int
qat_engine_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f) (void))
{
unsigned int retVal = 1;
CpaStatus status = CPA_STATUS_SUCCESS;
int flags = 0;
int fd = 0;
switch (cmd) {
case QAT_CMD_POLL:
BREAK_IF(!engine_inited, "POLL failed as engine is not initialized\n");
BREAK_IF(qat_instance_handles == NULL, "POLL failed as no instances are available\n");
BREAK_IF(!enable_external_polling, "POLL failed as external polling is not enabled\n");
BREAK_IF(p == NULL, "POLL failed as the input parameter was NULL\n");
*(int *)p = (int)poll_instances();
break;
case QAT_CMD_ENABLE_EXTERNAL_POLLING:
BREAK_IF(engine_inited, \
"ENABLE_EXTERNAL_POLLING failed as the engine is already initialized\n");
DEBUG("Enabled external polling\n");
enable_external_polling = 1;
enable_inline_polling = 0;
break;
case QAT_CMD_ENABLE_INLINE_POLLING:
BREAK_IF(engine_inited, \
"ENABLE_INLINE_POLLING failed as the engine is already initialized\n");
DEBUG("Enabled inline polling\n");
enable_inline_polling = 1;
enable_external_polling = 0;
break;
case QAT_CMD_GET_EXTERNAL_POLLING_FD:
BREAK_IF(!enable_event_driven_polling || !enable_external_polling, \
"GET_EXTERNAL_POLLING_FD failed as this engine message is only supported \
when running in Event Driven Mode with External Polling enabled\n");
BREAK_IF(!engine_inited, \
"GET_EXTERNAL_POLLING_FD failed as the engine is not initialized\n");
BREAK_IF(qat_instance_handles == NULL, \
"GET_EXTERNAL_POLLING_FD failed as no instances are available\n");
BREAK_IF(p == NULL, "GET_EXTERNAL_POLLING_FD failed as the input parameter was NULL\n");
BREAK_IF(i >= qat_num_instances, \
"GET_EXTERNAL_POLLING_FD failed as the instance does not exist\n");
/* Get the file descriptor for the instance */
status = icp_sal_CyGetFileDescriptor(qat_instance_handles[i], &fd);
BREAK_IF(CPA_STATUS_FAIL == status, \
"GET_EXTERNAL_POLLING_FD failed as there was an error retrieving the fd\n");
/* Make the file descriptor non-blocking */
flags = fcntl(fd, F_GETFL, 0);
fcntl(fd, F_SETFL, flags | O_NONBLOCK);
DEBUG("External polling FD for instance[%ld] = %d\n", i, fd);
*(int *)p = fd;
break;
case QAT_CMD_ENABLE_EVENT_DRIVEN_POLLING_MODE:
DEBUG("Enabled event driven polling mode\n");
BREAK_IF(engine_inited, \
"ENABLE_EVENT_DRIVEN_POLLING_MODE failed as the engine is already initialized\n");
enable_event_driven_polling = 1;
break;
case QAT_CMD_DISABLE_EVENT_DRIVEN_POLLING_MODE:
DEBUG("Disabled event driven polling mode\n");
BREAK_IF(engine_inited, \
"DISABLE_EVENT_DRIVEN_POLLING_MODE failed as the engine is already initialized\n");
enable_event_driven_polling = 0;
break;
case QAT_CMD_SET_INSTANCE_FOR_THREAD:
BREAK_IF(!engine_inited, \
"SET_INSTANCE_FOR_THREAD failed as the engine is not initialized\n");
BREAK_IF(qat_instance_handles == NULL, \
"SET_INSTANCE_FOR_THREAD failed as no instances are available\n");
DEBUG("Set instance for thread = %ld\n", i);
retVal = qat_set_instance_for_thread(i);
break;
case QAT_CMD_GET_NUM_OP_RETRIES:
BREAK_IF(p == NULL, "GET_NUM_OP_RETRIES failed as the input parameter was NULL\n");
BREAK_IF(!engine_inited, "GET_NUM_OP_RETRIES failed as the engine is not initialized\n");
*(int *)p = qatPerformOpRetries;
break;
case QAT_CMD_SET_MAX_RETRY_COUNT:
BREAK_IF(i < -1 || i > 100000,
"The Message retry count value is out of range, using default value\n");
DEBUG("Set max retry counter = %ld\n", i);
qat_max_retry_count = (int)i;
break;
case QAT_CMD_SET_INTERNAL_POLL_INTERVAL:
BREAK_IF(i < 1 || i > 1000000,
"The polling interval value is out of range, using default value\n");
DEBUG("Set internal poll interval = %ld ns\n", i);
qat_poll_interval = (useconds_t) i;
break;
case QAT_CMD_SET_EPOLL_TIMEOUT:
BREAK_IF(i < 1 || i > 10000,
"The epoll timeout value is out of range, using default value\n")
DEBUG("Set epoll_wait timeout = %ld ms\n", i);
qat_epoll_timeout = (int) i;
break;
case QAT_CMD_GET_NUM_CRYPTO_INSTANCES:
BREAK_IF(p == NULL, \
"GET_NUM_CRYPTO_INSTANCES failed as the input parameter was NULL\n");
BREAK_IF(!engine_inited, \
"GET_NUM_CRYPTO_INSTANCES failed as the engine is not initialized\n");
BREAK_IF(qat_instance_handles == NULL, \
"GET_NUM_CRYPTO_INSTANCES failed as no instances are available\n");
DEBUG("Get number of crypto instances = %d\n", qat_num_instances);
*(int *)p = qat_num_instances;
break;
case QAT_CMD_SET_CRYPTO_SMALL_PACKET_OFFLOAD_THRESHOLD:
#ifndef OPENSSL_ENABLE_QAT_SMALL_PACKET_CIPHER_OFFLOADS
if (p != NULL) {
char *token;
char str_p[QAT_MAX_INPUT_STRING_LENGTH];
char *itr = str_p;
strncpy(str_p, (const char *)p, QAT_MAX_INPUT_STRING_LENGTH - 1);
str_p[QAT_MAX_INPUT_STRING_LENGTH - 1] = '\0';
while ((token = strsep(&itr, ","))) {
char *name_token = strsep(&token,":");
char *value_token = strsep(&token,":");
if (name_token && value_token) {
retVal = qat_pkt_threshold_table_set_threshold(
name_token, atoi(value_token));
} else {
WARN("Invalid name_token or value_token\n");
retVal = 0;
}
}
} else {
WARN("Invalid p parameter\n");
retVal = 0;
}
#else
WARN("QAT_CMD_SET_CRYPTO_SMALL_PACKET_OFFLOAD_THRESHOLD is not supported\n");
retVal = 0;
#endif
break;
case QAT_CMD_ENABLE_HEURISTIC_POLLING:
BREAK_IF(engine_inited,
"ENABLE_HEURISTIC_POLLING failed as the engine is already initialized\n");
BREAK_IF(!enable_external_polling,
"ENABLE_HEURISTIC_POLLING failed as external polling is not enabled\n");
DEBUG("Enabled heuristic polling\n");
enable_heuristic_polling = 1;
break;
case QAT_CMD_GET_NUM_REQUESTS_IN_FLIGHT:
BREAK_IF(p == NULL,
"GET_NUM_REQUESTS_IN_FLIGHT failed as the input parameter was NULL\n");
if (i == GET_NUM_ASYM_REQUESTS_IN_FLIGHT) {
*(int **)p = &num_asym_requests_in_flight;
} else if (i == GET_NUM_PRF_REQUESTS_IN_FLIGHT) {
*(int **)p = &num_prf_requests_in_flight;
} else if (i == GET_NUM_CIPHER_PIPELINE_REQUESTS_IN_FLIGHT) {
*(int **)p = &num_cipher_pipeline_requests_in_flight;
} else {
WARN("Invalid i parameter\n");
retVal = 0;
}
break;
case QAT_CMD_INIT_ENGINE:
DEBUG("Init engine\n");
if ((retVal = qat_engine_init(e)) == 0) {
WARN("Failure initializing engine\n");
}
break;
case QAT_CMD_SET_CONFIGURATION_SECTION_NAME:
BREAK_IF(engine_inited, \