aboutsummaryrefslogtreecommitdiff
path: root/sys/dev/qat/qat_api/common/include/lac_common.h
blob: 6962e6f43a6d00cbc06916ccb1d3937caf1d702b (plain) (blame)
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
/* SPDX-License-Identifier: BSD-3-Clause */
/* Copyright(c) 2007-2025 Intel Corporation */
/**
 *****************************************************************************
 * @file lac_common.h Common macros
 *
 * @defgroup  Lac   Look Aside Crypto LLD Doc
 *
 *****************************************************************************/

/**
 *****************************************************************************
 * @defgroup  LacCommon   LAC Common
 * Common code for Lac which includes init/shutdown, memory, logging and
 * hooks.
 *
 * @ingroup Lac
 *
 *****************************************************************************/

/***************************************************************************/

#ifndef LAC_COMMON_H
#define LAC_COMMON_H

/*
******************************************************************************
* Include public/global header files
******************************************************************************
*/

#include "cpa.h"
#include "qat_utils.h"
#include "cpa_cy_common.h"
#include "icp_adf_init.h"

#define LAC_ARCH_UINT uintptr_t
#define LAC_ARCH_INT intptr_t

/*
*****************************************************************************
*  Max range values for some primitive param checking
*****************************************************************************
*/

/**< Maximum number of instances */
#define SAL_MAX_NUM_INSTANCES_PER_DEV 512

#define SAL_DEFAULT_RING_SIZE 256
/**<  Default ring size */

#define SAL_64_CONCURR_REQUESTS 64
#define SAL_128_CONCURR_REQUESTS 128
#define SAL_256_CONCURR_REQUESTS 256
#define SAL_512_CONCURR_REQUESTS 512
#define SAL_1024_CONCURR_REQUESTS 1024
#define SAL_2048_CONCURR_REQUESTS 2048
#define SAL_4096_CONCURR_REQUESTS 4096
#define SAL_MAX_CONCURR_REQUESTS 65536
/**< Valid options for the num of concurrent requests per ring pair read
     from the config file. These values are used to size the rings */

#define SAL_BATCH_SUBMIT_FREE_SPACE 2
/**< For data plane batch submissions ADF leaves 2 spaces free on the ring */

/*
******************************************************************************
* Some common settings for QA API queries
******************************************************************************
*/

#define SAL_INFO2_VENDOR_NAME "Intel(R)"
/**< @ingroup LacCommon
 * Name of vendor of this driver  */
#define SAL_INFO2_PART_NAME "%s with Intel(R) QuickAssist Technology"
/**< @ingroup LacCommon
 */

/*
********************************************************************************
* User process name defines and functions
********************************************************************************
*/

#define LAC_USER_PROCESS_NAME_MAX_LEN 32
/**< @ingroup LacCommon
 * Max length of user process name */

#define LAC_KERNEL_PROCESS_NAME "KERNEL_QAT"
/**< @ingroup LacCommon
 * Default name for kernel process */

/*
********************************************************************************
* response mode indicator from Config file
********************************************************************************
*/

#define SAL_RESP_POLL_CFG_FILE 1
#define SAL_RESP_EPOLL_CFG_FILE 2

/*
 * @ingroup LacCommon
 * @description
 *      This function sets the process name
 *
 * @context
 *      This functions is called from module_init or from user space process
 *      initialization function
 *
 * @assumptions
 *      None
 * @sideEffects
 *      None
 * @reentrant
 *      No
 * @threadSafe
 *      No
 *
 * param[in]  processName    Process name to be set
*/
CpaStatus icpSetProcessName(const char *processName);

/*
 * @ingroup LacCommon
 * @description
 *      This function gets the process name
 *
 * @context
 *      This functions is called from LAC context
 *
 * @assumptions
 *      None
 * @sideEffects
 *      None
 * @reentrant
 *      Yes
 * @threadSafe
 *      Yes
 *
*/
char *icpGetProcessName(void);

/* Sections of the config file */
#define LAC_CFG_SECTION_GENERAL "GENERAL"
#define LAC_CFG_SECTION_INTERNAL "INTERNAL"

/*
********************************************************************************
* Debug Macros and settings
********************************************************************************
*/

#define SEPARATOR "+--------------------------------------------------+\n"
/**< @ingroup LacCommon
 * separator used for printing stats to standard output*/

#define BORDER "|"
/**< @ingroup LacCommon
 * separator used for printing stats to standard output*/

/**
*****************************************************************************
 * @ingroup LacCommon
 *      Component state
 *
 * @description
 *      This enum is used to indicate the state that the component is in. Its
 *      purpose is to prevent components from being initialised or shutdown
 *      incorrectly.
 *
 *****************************************************************************/
typedef enum {
	LAC_COMP_SHUT_DOWN = 0,
	/**< Component in the Shut Down state */
	LAC_COMP_SHUTTING_DOWN,
	/**< Component in the Process of Shutting down */
	LAC_COMP_INITIALISING,
	/**< Component in the Process of being initialised */
	LAC_COMP_INITIALISED,
	/**< Component in the initialised state */
} lac_comp_state_t;

/**
 *******************************************************************************
 * @ingroup LacCommon
 *      This macro checks if a parameter is NULL
 *
 * @param[in] param                 Parameter
 *
 * @return CPA_STATUS_INVALID_PARAM Parameter is NULL
 * @return void                     Parameter is not NULL
 ******************************************************************************/
#define LAC_CHECK_NULL_PARAM(param)                                            \
	do {                                                                   \
		if (NULL == (param)) {                                         \
			return CPA_STATUS_INVALID_PARAM;                       \
		}                                                              \
	} while (0)

/**
 *******************************************************************************
 * @ingroup LacCommon
 *      This macro checks if a parameter is within a specified range
 *
 * @param[in] param                 Parameter
 * @param[in] min                   Parameter must be greater than OR equal to
 *min
 * @param[in] max                   Parameter must be less than max
 *
 * @return CPA_STATUS_INVALID_PARAM Parameter is outside range
 * @return void                     Parameter is within range
 ******************************************************************************/
#define LAC_CHECK_PARAM_RANGE(param, min, max)                                 \
	do {                                                                   \
		if (((param) < (min)) || ((param) >= (max))) {                 \
			return CPA_STATUS_INVALID_PARAM;                       \
		}                                                              \
	} while (0)

/**
 *******************************************************************************
 * @ingroup LacCommon
 *      This checks if a param is 8 byte aligned.
 *
 ******************************************************************************/
#define LAC_CHECK_8_BYTE_ALIGNMENT(param)                                      \
	do {                                                                   \
		if ((Cpa64U)param % 8 != 0) {                                  \
			return CPA_STATUS_INVALID_PARAM;                       \
		}                                                              \
	} while (0)

/**
 *******************************************************************************
 * @ingroup LacCommon
 *      This checks if a param is 64 byte aligned.
 *
 ******************************************************************************/
#define LAC_CHECK_64_BYTE_ALIGNMENT(param)                                     \
	do {                                                                   \
		if ((LAC_ARCH_UINT)param % 64 != 0) {                          \
			return CPA_STATUS_INVALID_PARAM;                       \
		}                                                              \
	} while (0)

/**
 *******************************************************************************
 * @ingroup LacCommon
 *      This macro returns the size of the buffer list structure given the
 *      number of elements in the buffer list - note: only the sizeof the
 *      buffer list structure is returned.
 *
 * @param[in] numBuffers    The number of flatbuffers in a buffer list
 *
 * @return size of the buffer list structure
 ******************************************************************************/
#define LAC_BUFFER_LIST_SIZE_GET(numBuffers)                                   \
	(sizeof(CpaBufferList) + (numBuffers * sizeof(CpaFlatBuffer)))

/**
 *******************************************************************************
 * @ingroup LacCommon
 *      This macro checks that a flatbuffer is valid i.e. that it is not
 *      null and the data it points to is not null
 *
 * @param[in] pFlatBuffer           Pointer to flatbuffer
 *
 * @return CPA_STATUS_INVALID_PARAM Invalid flatbuffer pointer
 * @return void                     flatbuffer is ok
 ******************************************************************************/
#define LAC_CHECK_FLAT_BUFFER(pFlatBuffer)                                     \
	do {                                                                   \
		LAC_CHECK_NULL_PARAM((pFlatBuffer));                           \
		LAC_CHECK_NULL_PARAM((pFlatBuffer)->pData);                    \
	} while (0)

/**
 *******************************************************************************
 * @ingroup LacCommon
 *   This macro verifies that the status is ok i.e. equal to CPA_STATUS_SUCCESS
 *
 * @param[in] status    status we are checking
 *
 * @return void         status is ok (CPA_STATUS_SUCCESS)
 * @return status       The value in the status parameter is an error one
 *
 ******************************************************************************/
#define LAC_CHECK_STATUS(status)                                               \
	do {                                                                   \
		if (CPA_STATUS_SUCCESS != (status)) {                          \
			return status;                                         \
		}                                                              \
	} while (0)

/**
 *******************************************************************************
 * @ingroup LacCommon
 *      This macro verifies that the Instance Handle is valid.
 *
 * @param[in] instanceHandle    Instance Handle
 *
 * @return CPA_STATUS_INVALID_PARAM Parameter is NULL
 * @return void                     Parameter is not NULL
 *
 ******************************************************************************/
#define LAC_CHECK_INSTANCE_HANDLE(instanceHandle)                              \
	do {                                                                   \
		if (NULL == (instanceHandle)) {                                \
			return CPA_STATUS_INVALID_PARAM;                       \
		}                                                              \
	} while (0)

/**
 *******************************************************************************
 * @ingroup LacCommon
 *      This macro copies a string from one location to another
 *
 * @param[out] pDestinationBuffer   Pointer to destination buffer
 * @param[in] pSource               Pointer to source buffer
 *
 ******************************************************************************/
#define LAC_COPY_STRING(pDestinationBuffer, pSource)                           \
	do {                                                                   \
		memcpy(pDestinationBuffer, pSource, (sizeof(pSource) - 1));    \
		pDestinationBuffer[(sizeof(pSource) - 1)] = '\0';              \
	} while (0)

/**
 *******************************************************************************
 * @ingroup LacCommon
 *      This macro fills a memory zone with ZEROES
 *
 * @param[in] pBuffer               Pointer to buffer
 * @param[in] count                 Buffer length
 *
 * @return void
 *
 ******************************************************************************/
#define LAC_OS_BZERO(pBuffer, count) memset(pBuffer, 0, count);

/**
 *******************************************************************************
 * @ingroup LacCommon
 *      This macro calculates the position of the given member in a struct
 *      Only for use on a struct where all members are of equal size to map
 *      the struct member position to an array index
 *
 * @param[in] structType        the struct
 * @param[in] member            the member of the given struct
 *
 ******************************************************************************/
#define LAC_IDX_OF(structType, member)                                         \
	(offsetof(structType, member) / sizeof(((structType *)0)->member))

/*
********************************************************************************
* Alignment, Bid define and Bit Operation Macros
********************************************************************************
*/

#define LAC_BIT31_SET 0x80000000 /**< bit 31 == 1 */
#define LAC_BIT7_SET 0x80	/**< bit 7 == 1  */
#define LAC_BIT6_SET 0x40	/**< bit 6 == 1  */
#define LAC_BIT5_SET 0x20	/**< bit 5 == 1  */
#define LAC_BIT4_SET 0x10	/**< bit 4 == 1  */
#define LAC_BIT3_SET 0x08	/**< bit 3 == 1  */
#define LAC_BIT2_SET 0x04	/**< bit 2 == 1  */
#define LAC_BIT1_SET 0x02	/**< bit 1 == 1  */
#define LAC_BIT0_SET 0x01	/**< bit 0 == 1  */

#define LAC_NUM_BITS_IN_BYTE (8)
/**< @ingroup LacCommon
 * Number of bits in a byte */

#define LAC_LONG_WORD_IN_BYTES (4)
/**< @ingroup LacCommon
 * Number of bytes in an IA word */

#define LAC_QUAD_WORD_IN_BYTES (8)
/**< @ingroup LacCommon
 * Number of bytes in a QUAD word */

#define LAC_QAT_MAX_MSG_SZ_LW (32)
/**< @ingroup LacCommon
 * Maximum size in Long Words for a QAT message */

/**
*****************************************************************************
 * @ingroup LacCommon
 *      Alignment shift requirements of a buffer.
 *
 * @description
 *      This enum is used to indicate the alignment shift of a buffer.
 *      All alignments are to power of 2
 *
 *****************************************************************************/
typedef enum lac_aligment_shift_s {
	LAC_NO_ALIGNMENT_SHIFT = 0,
	/**< No alignment shift (to a power of 2)*/
	LAC_8BYTE_ALIGNMENT_SHIFT = 3,
	/**< 8 byte alignment shift (to a power of 2)*/
	LAC_16BYTE_ALIGNMENT_SHIFT = 4,
	/**< 16 byte alignment shift (to a power of 2)*/
	LAC_64BYTE_ALIGNMENT_SHIFT = 6,
	/**< 64 byte alignment shift (to a power of 2)*/
	LAC_4KBYTE_ALIGNMENT_SHIFT = 12,
	/**< 4k byte alignment shift (to a power of 2)*/
} lac_aligment_shift_t;

/**
*****************************************************************************
 * @ingroup LacCommon
 *      Alignment of a buffer.
 *
 * @description
 *      This enum is used to indicate the alignment requirements of a buffer.
 *
 *****************************************************************************/
typedef enum lac_aligment_s {
	LAC_NO_ALIGNMENT = 0,
	/**< No alignment */
	LAC_1BYTE_ALIGNMENT = 1,
	/**< 1 byte alignment */
	LAC_8BYTE_ALIGNMENT = 8,
	/**< 8 byte alignment*/
	LAC_64BYTE_ALIGNMENT = 64,
	/**< 64 byte alignment*/
	LAC_4KBYTE_ALIGNMENT = 4096,
	/**< 4k byte alignment */
} lac_aligment_t;

/**
*****************************************************************************
 * @ingroup LacCommon
 *      Size of a buffer.
 *
 * @description
 *      This enum is used to indicate the required size.
 *      The buffer must be a multiple of the required size.
 *
 *****************************************************************************/
typedef enum lac_expected_size_s {
	LAC_NO_LENGTH_REQUIREMENTS = 0,
	/**< No requirement for size */
	LAC_4KBYTE_MULTIPLE_REQUIRED = 4096,
	/**< 4k multiple requirement for size */
} lac_expected_size_t;

#define LAC_OPTIMAL_ALIGNMENT_SHIFT LAC_64BYTE_ALIGNMENT_SHIFT
/**< @ingroup LacCommon
 * optimal alignment to a power of 2 */

#define LAC_SHIFT_8 (1 << LAC_8BYTE_ALIGNMENT_SHIFT)
/**< shift by 8 bits  */
#define LAC_SHIFT_24                                                           \
	((1 << LAC_8BYTE_ALIGNMENT_SHIFT) + (1 << LAC_16BYTE_ALIGNMENT_SHIFT))
/**< shift by 24 bits */

#define LAC_MAX_16_BIT_VALUE ((1 << 16) - 1)
/**< @ingroup LacCommon
 * maximum value a 16 bit type can hold */

/**
 *******************************************************************************
 * @ingroup LacCommon
 *      This macro can be used to avoid an unused variable warning from the
 *      compiler
 *
 * @param[in] variable  unused variable
 *
 ******************************************************************************/
#define LAC_UNUSED_VARIABLE(x) (void)(x)

/**
 *******************************************************************************
 * @ingroup LacCommon
 *      This macro checks if an address is aligned to the specified power of 2
 *      Returns 0 if alignment is ok, or non-zero otherwise
 *
 * @param[in] address   the address we are checking
 *
 * @param[in] alignment the byte alignment to check (specified as power of 2)
 *
 ******************************************************************************/
#define LAC_ADDRESS_ALIGNED(address, alignment)                                \
	(!((LAC_ARCH_UINT)(address) & ((1 << (alignment)) - 1)))

/**
 *******************************************************************************
 * @ingroup LacCommon
 *      This macro rounds up a number to a be a multiple of the alignment when
 *      the alignment is a power of 2.
 *
 * @param[in] num   Number
 * @param[in] align Alignment (must be a power of 2)
 *
 ******************************************************************************/
#define LAC_ALIGN_POW2_ROUNDUP(num, align) (((num) + (align)-1) & ~((align)-1))

/**
 *******************************************************************************
 * @ingroup LacCommon
 *      This macro generates a bit mask to select a particular bit
 *
 * @param[in] bitPos    Bit position to select
 *
 ******************************************************************************/
#define LAC_BIT(bitPos) (0x1 << (bitPos))

/**
 *******************************************************************************
 * @ingroup LacCommon
 *      This macro converts a size in bits to the equivalent size in bytes,
 *      using a bit shift to divide by 8
 *
 * @param[in] x     size in bits
 *
 ******************************************************************************/
#define LAC_BITS_TO_BYTES(x) ((x) >> 3)

/**
 *******************************************************************************
 * @ingroup LacCommon
 *      This macro converts a size in bytes to the equivalent size in bits,
 *      using a bit shift to multiply by 8
 *
 * @param[in] x     size in bytes
 *
 ******************************************************************************/
#define LAC_BYTES_TO_BITS(x) ((x) << 3)

/**
 *******************************************************************************
 * @ingroup LacCommon
 *      This macro converts a size in bytes to the equivalent size in longwords,
 *      using a bit shift to divide by 4
 *
 * @param[in] x     size in bytes
 *
 ******************************************************************************/
#define LAC_BYTES_TO_LONGWORDS(x) ((x) >> 2)

/**
 *******************************************************************************
 * @ingroup LacCommon
 *      This macro converts a size in longwords to the equivalent size in bytes,
 *      using a bit shift to multiply by 4
 *
 * @param[in] x     size in long words
 *
 ******************************************************************************/
#define LAC_LONGWORDS_TO_BYTES(x) ((x) << 2)

/**
 *******************************************************************************
 * @ingroup LacCommon
 *      This macro converts a size in bytes to the equivalent size in quadwords,
 *      using a bit shift to divide by 8
 *
 * @param[in] x     size in bytes
 *
 ******************************************************************************/
#define LAC_BYTES_TO_QUADWORDS(x) (((x) >> 3) + (((x) % 8) ? 1 : 0))

/**
 *******************************************************************************
 * @ingroup LacCommon
 *      This macro converts a size in quadwords to the equivalent size in bytes,
 *      using a bit shift to multiply by 8
 *
 * @param[in] x     size in quad words
 *
 ******************************************************************************/
#define LAC_QUADWORDS_TO_BYTES(x) ((x) << 3)

/******************************************************************************/

/*
*******************************************************************************
* Mutex Macros
*******************************************************************************
*/

/**
 *******************************************************************************
 * @ingroup LacCommon
 *      This macro tries to acquire a mutex and returns the status
 *
 * @param[in] pLock             Pointer to Lock
 * @param[in] timeout           Timeout
 *
 * @retval CPA_STATUS_SUCCESS   Function executed successfully.
 * @retval CPA_STATUS_RESOURCE  Error with Mutex
 ******************************************************************************/
#define LAC_LOCK_MUTEX(pLock, timeout)                                         \
	((CPA_STATUS_SUCCESS != qatUtilsMutexLock((pLock), (timeout))) ?       \
	     CPA_STATUS_RESOURCE :                                             \
	     CPA_STATUS_SUCCESS)

/**
 *******************************************************************************
 * @ingroup LacCommon
 *      This macro unlocks a mutex and returns the status
 *
 * @param[in] pLock             Pointer to Lock
 *
 * @retval CPA_STATUS_SUCCESS   Function executed successfully.
 * @retval CPA_STATUS_RESOURCE  Error with Mutex
 ******************************************************************************/
#define LAC_UNLOCK_MUTEX(pLock)                                                \
	((CPA_STATUS_SUCCESS != qatUtilsMutexUnlock((pLock))) ?                \
	     CPA_STATUS_RESOURCE :                                             \
	     CPA_STATUS_SUCCESS)

/**
 *******************************************************************************
 * @ingroup LacCommon
 *      This macro initialises a mutex and returns the status
 *
 * @param[in] pLock             Pointer to Lock
 *
 * @retval CPA_STATUS_SUCCESS   Function executed successfully.
 * @retval CPA_STATUS_RESOURCE  Error with Mutex
 ******************************************************************************/
#define LAC_INIT_MUTEX(pLock)                                                  \
	((CPA_STATUS_SUCCESS != qatUtilsMutexInit((pLock))) ?                  \
	     CPA_STATUS_RESOURCE :                                             \
	     CPA_STATUS_SUCCESS)

/**
 *******************************************************************************
 * @ingroup LacCommon
 *      This macro destroys a mutex and returns the status
 *
 * @param[in] pLock             Pointer to Lock
 *
 * @retval CPA_STATUS_SUCCESS   Function executed successfully.
 * @retval CPA_STATUS_RESOURCE  Error with Mutex
 ******************************************************************************/
#define LAC_DESTROY_MUTEX(pLock)                                               \
	((CPA_STATUS_SUCCESS != qatUtilsMutexDestroy((pLock))) ?               \
	     CPA_STATUS_RESOURCE :                                             \
	     CPA_STATUS_SUCCESS)

/**
 *******************************************************************************
 * @ingroup LacCommon
 *      This macro calls a trylock on a mutex
 *
 * @param[in] pLock             Pointer to Lock
 *
 * @retval CPA_STATUS_SUCCESS   Function executed successfully.
 * @retval CPA_STATUS_RESOURCE  Error with Mutex
 ******************************************************************************/
#define LAC_TRYLOCK_MUTEX(pLock)                                               \
	((CPA_STATUS_SUCCESS !=                                                \
	  qatUtilsMutexTryLock((pLock), QAT_UTILS_WAIT_NONE)) ?                \
	     CPA_STATUS_RESOURCE :                                             \
	     CPA_STATUS_SUCCESS)

/*
*******************************************************************************
* Semaphore Macros
*******************************************************************************
*/

/**
 *******************************************************************************
 * @ingroup LacCommon
 *      This macro waits on a semaphore and returns the status
 *
 * @param[in] sid               The semaphore
 * @param[in] timeout           Timeout
 *
 * @retval CPA_STATUS_SUCCESS   Function executed successfully.
 * @retval CPA_STATUS_RESOURCE  Error with semaphore
 ******************************************************************************/
#define LAC_WAIT_SEMAPHORE(sid, timeout)                                       \
	((CPA_STATUS_SUCCESS != qatUtilsSemaphoreWait(&sid, (timeout))) ?      \
	     CPA_STATUS_RESOURCE :                                             \
	     CPA_STATUS_SUCCESS)

/**
 *******************************************************************************
 * @ingroup LacCommon
 *      This macro checks a semaphore and returns the status
 *
 * @param[in] sid               The semaphore
 *
 * @retval CPA_STATUS_SUCCESS   Function executed successfully.
 * @retval CPA_STATUS_RESOURCE  Error with semaphore
 ******************************************************************************/
#define LAC_CHECK_SEMAPHORE(sid)                                               \
	((CPA_STATUS_SUCCESS != qatUtilsSemaphoreTryWait(&sid)) ?              \
	     CPA_STATUS_RETRY :                                                \
	     CPA_STATUS_SUCCESS)

/**
 *******************************************************************************
 * @ingroup LacCommon
 *      This macro post a semaphore and returns the status
 *
 * @param[in] sid               The semaphore
 *
 * @retval CPA_STATUS_SUCCESS   Function executed successfully.
 * @retval CPA_STATUS_RESOURCE  Error with semaphore
 ******************************************************************************/
#define LAC_POST_SEMAPHORE(sid)                                                \
	((CPA_STATUS_SUCCESS != qatUtilsSemaphorePost(&sid)) ?                 \
	     CPA_STATUS_RESOURCE :                                             \
	     CPA_STATUS_SUCCESS)
/**
 *******************************************************************************
 * @ingroup LacCommon
 *      This macro initialises a semaphore and returns the status
 *
 * @param[in] sid               The semaphore
 * @param[in] semValue          Initial semaphore value
 *
 * @retval CPA_STATUS_SUCCESS   Function executed successfully.
 * @retval CPA_STATUS_RESOURCE  Error with semaphore
 ******************************************************************************/
#define LAC_INIT_SEMAPHORE(sid, semValue)                                      \
	((CPA_STATUS_SUCCESS != qatUtilsSemaphoreInit(&sid, semValue)) ?       \
	     CPA_STATUS_RESOURCE :                                             \
	     CPA_STATUS_SUCCESS)

/**
 *******************************************************************************
 * @ingroup LacCommon
 *      This macro destroys a semaphore and returns the status
 *
 * @param[in] sid               The semaphore
 *
 * @retval CPA_STATUS_SUCCESS   Function executed successfully.
 * @retval CPA_STATUS_RESOURCE  Error with semaphore
 ******************************************************************************/
#define LAC_DESTROY_SEMAPHORE(sid)                                             \
	((CPA_STATUS_SUCCESS != qatUtilsSemaphoreDestroy(&sid)) ?              \
	     CPA_STATUS_RESOURCE :                                             \
	     CPA_STATUS_SUCCESS)

/*
*******************************************************************************
* Spinlock Macros
*******************************************************************************
*/
typedef struct mtx *lac_lock_t;
#define LAC_SPINLOCK_INIT(lock)                                                \
	((CPA_STATUS_SUCCESS != qatUtilsLockInit(lock)) ?                      \
	     CPA_STATUS_RESOURCE :                                             \
	     CPA_STATUS_SUCCESS)
#define LAC_SPINLOCK(lock)                                                     \
	({                                                                     \
		(void)qatUtilsLock(lock);                                      \
	})
#define LAC_SPINUNLOCK(lock)                                                   \
	({                                                                     \
		(void)qatUtilsUnlock(lock);                                    \
	})
#define LAC_SPINLOCK_DESTROY(lock)                                             \
	({                                                                     \
		(void)qatUtilsLockDestroy(lock);                               \
	})

#define LAC_CONST_PTR_CAST(castee) ((void *)(LAC_ARCH_UINT)(castee))
#define LAC_CONST_VOLATILE_PTR_CAST(castee) ((void *)(LAC_ARCH_UINT)(castee))

/* Type of ring */
#define SAL_RING_TYPE_NONE 0
#define SAL_RING_TYPE_A_SYM_HI 1
#define SAL_RING_TYPE_A_SYM_LO 2
#define SAL_RING_TYPE_A_ASYM 3
#define SAL_RING_TYPE_B_SYM_HI 4
#define SAL_RING_TYPE_B_SYM_LO 5
#define SAL_RING_TYPE_B_ASYM 6
#define SAL_RING_TYPE_DC 7
#define SAL_RING_TYPE_ADMIN 8
#define SAL_RING_TYPE_TRNG 9

/* Maps Ring Service to generic service type */
static inline icp_adf_ringInfoService_t
lac_getRingType(int type)
{
	switch (type) {
	case SAL_RING_TYPE_NONE:
		return ICP_ADF_RING_SERVICE_0;
	case SAL_RING_TYPE_A_SYM_HI:
		return ICP_ADF_RING_SERVICE_1;
	case SAL_RING_TYPE_A_SYM_LO:
		return ICP_ADF_RING_SERVICE_2;
	case SAL_RING_TYPE_A_ASYM:
		return ICP_ADF_RING_SERVICE_3;
	case SAL_RING_TYPE_B_SYM_HI:
		return ICP_ADF_RING_SERVICE_4;
	case SAL_RING_TYPE_B_SYM_LO:
		return ICP_ADF_RING_SERVICE_5;
	case SAL_RING_TYPE_B_ASYM:
		return ICP_ADF_RING_SERVICE_6;
	case SAL_RING_TYPE_DC:
		return ICP_ADF_RING_SERVICE_7;
	case SAL_RING_TYPE_ADMIN:
		return ICP_ADF_RING_SERVICE_8;
	case SAL_RING_TYPE_TRNG:
		return ICP_ADF_RING_SERVICE_9;
	default:
		return ICP_ADF_RING_SERVICE_0;
	}
	return ICP_ADF_RING_SERVICE_0;
}

/* Maps generic service type to Ring Service type  */
static inline int
lac_getServiceType(icp_adf_ringInfoService_t type)
{
	switch (type) {
	case ICP_ADF_RING_SERVICE_0:
		return SAL_RING_TYPE_NONE;
	case ICP_ADF_RING_SERVICE_1:
		return SAL_RING_TYPE_A_SYM_HI;
	case ICP_ADF_RING_SERVICE_2:
		return SAL_RING_TYPE_A_SYM_LO;
	case ICP_ADF_RING_SERVICE_3:
		return SAL_RING_TYPE_A_ASYM;
	case ICP_ADF_RING_SERVICE_4:
		return SAL_RING_TYPE_B_SYM_HI;
	case ICP_ADF_RING_SERVICE_5:
		return SAL_RING_TYPE_B_SYM_LO;
	case ICP_ADF_RING_SERVICE_6:
		return SAL_RING_TYPE_B_ASYM;
	case ICP_ADF_RING_SERVICE_7:
		return SAL_RING_TYPE_DC;
	case ICP_ADF_RING_SERVICE_8:
		return SAL_RING_TYPE_ADMIN;
	default:
		return SAL_RING_TYPE_NONE;
	}
	return SAL_RING_TYPE_NONE;
}

#endif /* LAC_COMMON_H */