1 /* SPDX-License-Identifier: BSD-3-Clause */
2 /* Copyright(c) 2007-2022 Intel Corporation */
3 /**
4  *****************************************************************************
5  * @file lac_common.h Common macros
6  *
7  * @defgroup  Lac   Look Aside Crypto LLD Doc
8  *
9  *****************************************************************************/
10 
11 /**
12  *****************************************************************************
13  * @defgroup  LacCommon   LAC Common
14  * Common code for Lac which includes init/shutdown, memory, logging and
15  * hooks.
16  *
17  * @ingroup Lac
18  *
19  *****************************************************************************/
20 
21 /***************************************************************************/
22 
23 #ifndef LAC_COMMON_H
24 #define LAC_COMMON_H
25 
26 /*
27 ******************************************************************************
28 * Include public/global header files
29 ******************************************************************************
30 */
31 
32 #include "cpa.h"
33 #include "qat_utils.h"
34 #include "cpa_cy_common.h"
35 #include "icp_adf_init.h"
36 
37 #define LAC_ARCH_UINT uintptr_t
38 #define LAC_ARCH_INT intptr_t
39 
40 /*
41 *****************************************************************************
42 *  Max range values for some primitive param checking
43 *****************************************************************************
44 */
45 
46 /**< Maximum number of instances */
47 #define SAL_MAX_NUM_INSTANCES_PER_DEV 512
48 
49 #define SAL_DEFAULT_RING_SIZE 256
50 /**<  Default ring size */
51 
52 #define SAL_64_CONCURR_REQUESTS 64
53 #define SAL_128_CONCURR_REQUESTS 128
54 #define SAL_256_CONCURR_REQUESTS 256
55 #define SAL_512_CONCURR_REQUESTS 512
56 #define SAL_1024_CONCURR_REQUESTS 1024
57 #define SAL_2048_CONCURR_REQUESTS 2048
58 #define SAL_4096_CONCURR_REQUESTS 4096
59 #define SAL_MAX_CONCURR_REQUESTS 65536
60 /**< Valid options for the num of concurrent requests per ring pair read
61      from the config file. These values are used to size the rings */
62 
63 #define SAL_BATCH_SUBMIT_FREE_SPACE 2
64 /**< For data plane batch submissions ADF leaves 2 spaces free on the ring */
65 
66 /*
67 ******************************************************************************
68 * Some common settings for QA API queries
69 ******************************************************************************
70 */
71 
72 #define SAL_INFO2_VENDOR_NAME "Intel(R)"
73 /**< @ingroup LacCommon
74  * Name of vendor of this driver  */
75 #define SAL_INFO2_PART_NAME "%s with Intel(R) QuickAssist Technology"
76 /**< @ingroup LacCommon
77  */
78 
79 /*
80 ********************************************************************************
81 * User process name defines and functions
82 ********************************************************************************
83 */
84 
85 #define LAC_USER_PROCESS_NAME_MAX_LEN 32
86 /**< @ingroup LacCommon
87  * Max length of user process name */
88 
89 #define LAC_KERNEL_PROCESS_NAME "KERNEL_QAT"
90 /**< @ingroup LacCommon
91  * Default name for kernel process */
92 
93 /*
94 ********************************************************************************
95 * response mode indicator from Config file
96 ********************************************************************************
97 */
98 
99 #define SAL_RESP_POLL_CFG_FILE 1
100 #define SAL_RESP_EPOLL_CFG_FILE 2
101 
102 /*
103  * @ingroup LacCommon
104  * @description
105  *      This function sets the process name
106  *
107  * @context
108  *      This functions is called from module_init or from user space process
109  *      initialization function
110  *
111  * @assumptions
112  *      None
113  * @sideEffects
114  *      None
115  * @reentrant
116  *      No
117  * @threadSafe
118  *      No
119  *
120  * param[in]  processName    Process name to be set
121 */
122 CpaStatus icpSetProcessName(const char *processName);
123 
124 /*
125  * @ingroup LacCommon
126  * @description
127  *      This function gets the process name
128  *
129  * @context
130  *      This functions is called from LAC context
131  *
132  * @assumptions
133  *      None
134  * @sideEffects
135  *      None
136  * @reentrant
137  *      Yes
138  * @threadSafe
139  *      Yes
140  *
141 */
142 char *icpGetProcessName(void);
143 
144 /* Sections of the config file */
145 #define LAC_CFG_SECTION_GENERAL "GENERAL"
146 #define LAC_CFG_SECTION_INTERNAL "INTERNAL"
147 
148 /*
149 ********************************************************************************
150 * Debug Macros and settings
151 ********************************************************************************
152 */
153 
154 #define SEPARATOR "+--------------------------------------------------+\n"
155 /**< @ingroup LacCommon
156  * separator used for printing stats to standard output*/
157 
158 #define BORDER "|"
159 /**< @ingroup LacCommon
160  * separator used for printing stats to standard output*/
161 
162 /**
163 *****************************************************************************
164  * @ingroup LacCommon
165  *      Component state
166  *
167  * @description
168  *      This enum is used to indicate the state that the component is in. Its
169  *      purpose is to prevent components from being initialised or shutdown
170  *      incorrectly.
171  *
172  *****************************************************************************/
173 typedef enum {
174 	LAC_COMP_SHUT_DOWN = 0,
175 	/**< Component in the Shut Down state */
176 	LAC_COMP_SHUTTING_DOWN,
177 	/**< Component in the Process of Shutting down */
178 	LAC_COMP_INITIALISING,
179 	/**< Component in the Process of being initialised */
180 	LAC_COMP_INITIALISED,
181 	/**< Component in the initialised state */
182 } lac_comp_state_t;
183 
184 /**
185  *******************************************************************************
186  * @ingroup LacCommon
187  *      This macro checks if a parameter is NULL
188  *
189  * @param[in] param                 Parameter
190  *
191  * @return CPA_STATUS_INVALID_PARAM Parameter is NULL
192  * @return void                     Parameter is not NULL
193  ******************************************************************************/
194 #define LAC_CHECK_NULL_PARAM(param)                                            \
195 	do {                                                                   \
196 		if (NULL == (param)) {                                         \
197 			return CPA_STATUS_INVALID_PARAM;                       \
198 		}                                                              \
199 	} while (0)
200 
201 /**
202  *******************************************************************************
203  * @ingroup LacCommon
204  *      This macro checks if a parameter is within a specified range
205  *
206  * @param[in] param                 Parameter
207  * @param[in] min                   Parameter must be greater than OR equal to
208  *min
209  * @param[in] max                   Parameter must be less than max
210  *
211  * @return CPA_STATUS_INVALID_PARAM Parameter is outside range
212  * @return void                     Parameter is within range
213  ******************************************************************************/
214 #define LAC_CHECK_PARAM_RANGE(param, min, max)                                 \
215 	do {                                                                   \
216 		if (((param) < (min)) || ((param) >= (max))) {                 \
217 			return CPA_STATUS_INVALID_PARAM;                       \
218 		}                                                              \
219 	} while (0)
220 
221 /**
222  *******************************************************************************
223  * @ingroup LacCommon
224  *      This checks if a param is 8 byte aligned.
225  *
226  ******************************************************************************/
227 #define LAC_CHECK_8_BYTE_ALIGNMENT(param)                                      \
228 	do {                                                                   \
229 		if ((Cpa64U)param % 8 != 0) {                                  \
230 			return CPA_STATUS_INVALID_PARAM;                       \
231 		}                                                              \
232 	} while (0)
233 
234 /**
235  *******************************************************************************
236  * @ingroup LacCommon
237  *      This checks if a param is 64 byte aligned.
238  *
239  ******************************************************************************/
240 #define LAC_CHECK_64_BYTE_ALIGNMENT(param)                                     \
241 	do {                                                                   \
242 		if ((LAC_ARCH_UINT)param % 64 != 0) {                          \
243 			return CPA_STATUS_INVALID_PARAM;                       \
244 		}                                                              \
245 	} while (0)
246 
247 /**
248  *******************************************************************************
249  * @ingroup LacCommon
250  *      This macro returns the size of the buffer list structure given the
251  *      number of elements in the buffer list - note: only the sizeof the
252  *      buffer list structure is returned.
253  *
254  * @param[in] numBuffers    The number of flatbuffers in a buffer list
255  *
256  * @return size of the buffer list structure
257  ******************************************************************************/
258 #define LAC_BUFFER_LIST_SIZE_GET(numBuffers)                                   \
259 	(sizeof(CpaBufferList) + (numBuffers * sizeof(CpaFlatBuffer)))
260 
261 /**
262  *******************************************************************************
263  * @ingroup LacCommon
264  *      This macro checks that a flatbuffer is valid i.e. that it is not
265  *      null and the data it points to is not null
266  *
267  * @param[in] pFlatBuffer           Pointer to flatbuffer
268  *
269  * @return CPA_STATUS_INVALID_PARAM Invalid flatbuffer pointer
270  * @return void                     flatbuffer is ok
271  ******************************************************************************/
272 #define LAC_CHECK_FLAT_BUFFER(pFlatBuffer)                                     \
273 	do {                                                                   \
274 		LAC_CHECK_NULL_PARAM((pFlatBuffer));                           \
275 		LAC_CHECK_NULL_PARAM((pFlatBuffer)->pData);                    \
276 	} while (0)
277 
278 /**
279  *******************************************************************************
280  * @ingroup LacCommon
281  *   This macro verifies that the status is ok i.e. equal to CPA_STATUS_SUCCESS
282  *
283  * @param[in] status    status we are checking
284  *
285  * @return void         status is ok (CPA_STATUS_SUCCESS)
286  * @return status       The value in the status parameter is an error one
287  *
288  ******************************************************************************/
289 #define LAC_CHECK_STATUS(status)                                               \
290 	do {                                                                   \
291 		if (CPA_STATUS_SUCCESS != (status)) {                          \
292 			return status;                                         \
293 		}                                                              \
294 	} while (0)
295 
296 /**
297  *******************************************************************************
298  * @ingroup LacCommon
299  *      This macro verifies that the Instance Handle is valid.
300  *
301  * @param[in] instanceHandle    Instance Handle
302  *
303  * @return CPA_STATUS_INVALID_PARAM Parameter is NULL
304  * @return void                     Parameter is not NULL
305  *
306  ******************************************************************************/
307 #define LAC_CHECK_INSTANCE_HANDLE(instanceHandle)                              \
308 	do {                                                                   \
309 		if (NULL == (instanceHandle)) {                                \
310 			return CPA_STATUS_INVALID_PARAM;                       \
311 		}                                                              \
312 	} while (0)
313 
314 /**
315  *******************************************************************************
316  * @ingroup LacCommon
317  *      This macro copies a string from one location to another
318  *
319  * @param[out] pDestinationBuffer   Pointer to destination buffer
320  * @param[in] pSource               Pointer to source buffer
321  *
322  ******************************************************************************/
323 #define LAC_COPY_STRING(pDestinationBuffer, pSource)                           \
324 	do {                                                                   \
325 		memcpy(pDestinationBuffer, pSource, (sizeof(pSource) - 1));    \
326 		pDestinationBuffer[(sizeof(pSource) - 1)] = '\0';              \
327 	} while (0)
328 
329 /**
330  *******************************************************************************
331  * @ingroup LacCommon
332  *      This macro fills a memory zone with ZEROES
333  *
334  * @param[in] pBuffer               Pointer to buffer
335  * @param[in] count                 Buffer length
336  *
337  * @return void
338  *
339  ******************************************************************************/
340 #define LAC_OS_BZERO(pBuffer, count) memset(pBuffer, 0, count);
341 
342 /**
343  *******************************************************************************
344  * @ingroup LacCommon
345  *      This macro calculates the position of the given member in a struct
346  *      Only for use on a struct where all members are of equal size to map
347  *      the struct member position to an array index
348  *
349  * @param[in] structType        the struct
350  * @param[in] member            the member of the given struct
351  *
352  ******************************************************************************/
353 #define LAC_IDX_OF(structType, member)                                         \
354 	(offsetof(structType, member) / sizeof(((structType *)0)->member))
355 
356 /*
357 ********************************************************************************
358 * Alignment, Bid define and Bit Operation Macros
359 ********************************************************************************
360 */
361 
362 #define LAC_BIT31_SET 0x80000000 /**< bit 31 == 1 */
363 #define LAC_BIT7_SET 0x80	/**< bit 7 == 1  */
364 #define LAC_BIT6_SET 0x40	/**< bit 6 == 1  */
365 #define LAC_BIT5_SET 0x20	/**< bit 5 == 1  */
366 #define LAC_BIT4_SET 0x10	/**< bit 4 == 1  */
367 #define LAC_BIT3_SET 0x08	/**< bit 3 == 1  */
368 #define LAC_BIT2_SET 0x04	/**< bit 2 == 1  */
369 #define LAC_BIT1_SET 0x02	/**< bit 1 == 1  */
370 #define LAC_BIT0_SET 0x01	/**< bit 0 == 1  */
371 
372 #define LAC_NUM_BITS_IN_BYTE (8)
373 /**< @ingroup LacCommon
374  * Number of bits in a byte */
375 
376 #define LAC_LONG_WORD_IN_BYTES (4)
377 /**< @ingroup LacCommon
378  * Number of bytes in an IA word */
379 
380 #define LAC_QUAD_WORD_IN_BYTES (8)
381 /**< @ingroup LacCommon
382  * Number of bytes in a QUAD word */
383 
384 #define LAC_QAT_MAX_MSG_SZ_LW (32)
385 /**< @ingroup LacCommon
386  * Maximum size in Long Words for a QAT message */
387 
388 /**
389 *****************************************************************************
390  * @ingroup LacCommon
391  *      Alignment shift requirements of a buffer.
392  *
393  * @description
394  *      This enum is used to indicate the alignment shift of a buffer.
395  *      All alignments are to power of 2
396  *
397  *****************************************************************************/
398 typedef enum lac_aligment_shift_s {
399 	LAC_NO_ALIGNMENT_SHIFT = 0,
400 	/**< No alignment shift (to a power of 2)*/
401 	LAC_8BYTE_ALIGNMENT_SHIFT = 3,
402 	/**< 8 byte alignment shift (to a power of 2)*/
403 	LAC_16BYTE_ALIGNMENT_SHIFT = 4,
404 	/**< 16 byte alignment shift (to a power of 2)*/
405 	LAC_64BYTE_ALIGNMENT_SHIFT = 6,
406 	/**< 64 byte alignment shift (to a power of 2)*/
407 	LAC_4KBYTE_ALIGNMENT_SHIFT = 12,
408 	/**< 4k byte alignment shift (to a power of 2)*/
409 } lac_aligment_shift_t;
410 
411 /**
412 *****************************************************************************
413  * @ingroup LacCommon
414  *      Alignment of a buffer.
415  *
416  * @description
417  *      This enum is used to indicate the alignment requirements of a buffer.
418  *
419  *****************************************************************************/
420 typedef enum lac_aligment_s {
421 	LAC_NO_ALIGNMENT = 0,
422 	/**< No alignment */
423 	LAC_1BYTE_ALIGNMENT = 1,
424 	/**< 1 byte alignment */
425 	LAC_8BYTE_ALIGNMENT = 8,
426 	/**< 8 byte alignment*/
427 	LAC_64BYTE_ALIGNMENT = 64,
428 	/**< 64 byte alignment*/
429 	LAC_4KBYTE_ALIGNMENT = 4096,
430 	/**< 4k byte alignment */
431 } lac_aligment_t;
432 
433 /**
434 *****************************************************************************
435  * @ingroup LacCommon
436  *      Size of a buffer.
437  *
438  * @description
439  *      This enum is used to indicate the required size.
440  *      The buffer must be a multiple of the required size.
441  *
442  *****************************************************************************/
443 typedef enum lac_expected_size_s {
444 	LAC_NO_LENGTH_REQUIREMENTS = 0,
445 	/**< No requirement for size */
446 	LAC_4KBYTE_MULTIPLE_REQUIRED = 4096,
447 	/**< 4k multiple requirement for size */
448 } lac_expected_size_t;
449 
450 #define LAC_OPTIMAL_ALIGNMENT_SHIFT LAC_64BYTE_ALIGNMENT_SHIFT
451 /**< @ingroup LacCommon
452  * optimal alignment to a power of 2 */
453 
454 #define LAC_SHIFT_8 (1 << LAC_8BYTE_ALIGNMENT_SHIFT)
455 /**< shift by 8 bits  */
456 #define LAC_SHIFT_24                                                           \
457 	((1 << LAC_8BYTE_ALIGNMENT_SHIFT) + (1 << LAC_16BYTE_ALIGNMENT_SHIFT))
458 /**< shift by 24 bits */
459 
460 #define LAC_MAX_16_BIT_VALUE ((1 << 16) - 1)
461 /**< @ingroup LacCommon
462  * maximum value a 16 bit type can hold */
463 
464 /**
465  *******************************************************************************
466  * @ingroup LacCommon
467  *      This macro can be used to avoid an unused variable warning from the
468  *      compiler
469  *
470  * @param[in] variable  unused variable
471  *
472  ******************************************************************************/
473 #define LAC_UNUSED_VARIABLE(x) (void)(x)
474 
475 /**
476  *******************************************************************************
477  * @ingroup LacCommon
478  *      This macro checks if an address is aligned to the specified power of 2
479  *      Returns 0 if alignment is ok, or non-zero otherwise
480  *
481  * @param[in] address   the address we are checking
482  *
483  * @param[in] alignment the byte alignment to check (specified as power of 2)
484  *
485  ******************************************************************************/
486 #define LAC_ADDRESS_ALIGNED(address, alignment)                                \
487 	(!((LAC_ARCH_UINT)(address) & ((1 << (alignment)) - 1)))
488 
489 /**
490  *******************************************************************************
491  * @ingroup LacCommon
492  *      This macro rounds up a number to a be a multiple of the alignment when
493  *      the alignment is a power of 2.
494  *
495  * @param[in] num   Number
496  * @param[in] align Alignment (must be a power of 2)
497  *
498  ******************************************************************************/
499 #define LAC_ALIGN_POW2_ROUNDUP(num, align) (((num) + (align)-1) & ~((align)-1))
500 
501 /**
502  *******************************************************************************
503  * @ingroup LacCommon
504  *      This macro generates a bit mask to select a particular bit
505  *
506  * @param[in] bitPos    Bit position to select
507  *
508  ******************************************************************************/
509 #define LAC_BIT(bitPos) (0x1 << (bitPos))
510 
511 /**
512  *******************************************************************************
513  * @ingroup LacCommon
514  *      This macro converts a size in bits to the equivalent size in bytes,
515  *      using a bit shift to divide by 8
516  *
517  * @param[in] x     size in bits
518  *
519  ******************************************************************************/
520 #define LAC_BITS_TO_BYTES(x) ((x) >> 3)
521 
522 /**
523  *******************************************************************************
524  * @ingroup LacCommon
525  *      This macro converts a size in bytes to the equivalent size in bits,
526  *      using a bit shift to multiply by 8
527  *
528  * @param[in] x     size in bytes
529  *
530  ******************************************************************************/
531 #define LAC_BYTES_TO_BITS(x) ((x) << 3)
532 
533 /**
534  *******************************************************************************
535  * @ingroup LacCommon
536  *      This macro converts a size in bytes to the equivalent size in longwords,
537  *      using a bit shift to divide by 4
538  *
539  * @param[in] x     size in bytes
540  *
541  ******************************************************************************/
542 #define LAC_BYTES_TO_LONGWORDS(x) ((x) >> 2)
543 
544 /**
545  *******************************************************************************
546  * @ingroup LacCommon
547  *      This macro converts a size in longwords to the equivalent size in bytes,
548  *      using a bit shift to multiply by 4
549  *
550  * @param[in] x     size in long words
551  *
552  ******************************************************************************/
553 #define LAC_LONGWORDS_TO_BYTES(x) ((x) << 2)
554 
555 /**
556  *******************************************************************************
557  * @ingroup LacCommon
558  *      This macro converts a size in bytes to the equivalent size in quadwords,
559  *      using a bit shift to divide by 8
560  *
561  * @param[in] x     size in bytes
562  *
563  ******************************************************************************/
564 #define LAC_BYTES_TO_QUADWORDS(x) (((x) >> 3) + (((x) % 8) ? 1 : 0))
565 
566 /**
567  *******************************************************************************
568  * @ingroup LacCommon
569  *      This macro converts a size in quadwords to the equivalent size in bytes,
570  *      using a bit shift to multiply by 8
571  *
572  * @param[in] x     size in quad words
573  *
574  ******************************************************************************/
575 #define LAC_QUADWORDS_TO_BYTES(x) ((x) << 3)
576 
577 
578 /******************************************************************************/
579 
580 /*
581 *******************************************************************************
582 * Mutex Macros
583 *******************************************************************************
584 */
585 
586 /**
587  *******************************************************************************
588  * @ingroup LacCommon
589  *      This macro tries to acquire a mutex and returns the status
590  *
591  * @param[in] pLock             Pointer to Lock
592  * @param[in] timeout           Timeout
593  *
594  * @retval CPA_STATUS_SUCCESS   Function executed successfully.
595  * @retval CPA_STATUS_RESOURCE  Error with Mutex
596  ******************************************************************************/
597 #define LAC_LOCK_MUTEX(pLock, timeout)                                         \
598 	((CPA_STATUS_SUCCESS != qatUtilsMutexLock((pLock), (timeout))) ?       \
599 	     CPA_STATUS_RESOURCE :                                             \
600 	     CPA_STATUS_SUCCESS)
601 
602 /**
603  *******************************************************************************
604  * @ingroup LacCommon
605  *      This macro unlocks a mutex and returns the status
606  *
607  * @param[in] pLock             Pointer to Lock
608  *
609  * @retval CPA_STATUS_SUCCESS   Function executed successfully.
610  * @retval CPA_STATUS_RESOURCE  Error with Mutex
611  ******************************************************************************/
612 #define LAC_UNLOCK_MUTEX(pLock)                                                \
613 	((CPA_STATUS_SUCCESS != qatUtilsMutexUnlock((pLock))) ?                \
614 	     CPA_STATUS_RESOURCE :                                             \
615 	     CPA_STATUS_SUCCESS)
616 
617 /**
618  *******************************************************************************
619  * @ingroup LacCommon
620  *      This macro initialises a mutex and returns the status
621  *
622  * @param[in] pLock             Pointer to Lock
623  *
624  * @retval CPA_STATUS_SUCCESS   Function executed successfully.
625  * @retval CPA_STATUS_RESOURCE  Error with Mutex
626  ******************************************************************************/
627 #define LAC_INIT_MUTEX(pLock)                                                  \
628 	((CPA_STATUS_SUCCESS != qatUtilsMutexInit((pLock))) ?                  \
629 	     CPA_STATUS_RESOURCE :                                             \
630 	     CPA_STATUS_SUCCESS)
631 
632 /**
633  *******************************************************************************
634  * @ingroup LacCommon
635  *      This macro destroys a mutex and returns the status
636  *
637  * @param[in] pLock             Pointer to Lock
638  *
639  * @retval CPA_STATUS_SUCCESS   Function executed successfully.
640  * @retval CPA_STATUS_RESOURCE  Error with Mutex
641  ******************************************************************************/
642 #define LAC_DESTROY_MUTEX(pLock)                                               \
643 	((CPA_STATUS_SUCCESS != qatUtilsMutexDestroy((pLock))) ?               \
644 	     CPA_STATUS_RESOURCE :                                             \
645 	     CPA_STATUS_SUCCESS)
646 
647 /**
648  *******************************************************************************
649  * @ingroup LacCommon
650  *      This macro calls a trylock on a mutex
651  *
652  * @param[in] pLock             Pointer to Lock
653  *
654  * @retval CPA_STATUS_SUCCESS   Function executed successfully.
655  * @retval CPA_STATUS_RESOURCE  Error with Mutex
656  ******************************************************************************/
657 #define LAC_TRYLOCK_MUTEX(pLock)                                               \
658 	((CPA_STATUS_SUCCESS !=                                                \
659 	  qatUtilsMutexTryLock((pLock), QAT_UTILS_WAIT_NONE)) ?                \
660 	     CPA_STATUS_RESOURCE :                                             \
661 	     CPA_STATUS_SUCCESS)
662 
663 /*
664 *******************************************************************************
665 * Semaphore Macros
666 *******************************************************************************
667 */
668 
669 /**
670  *******************************************************************************
671  * @ingroup LacCommon
672  *      This macro waits on a semaphore and returns the status
673  *
674  * @param[in] sid               The semaphore
675  * @param[in] timeout           Timeout
676  *
677  * @retval CPA_STATUS_SUCCESS   Function executed successfully.
678  * @retval CPA_STATUS_RESOURCE  Error with semaphore
679  ******************************************************************************/
680 #define LAC_WAIT_SEMAPHORE(sid, timeout)                                       \
681 	((CPA_STATUS_SUCCESS != qatUtilsSemaphoreWait(&sid, (timeout))) ?      \
682 	     CPA_STATUS_RESOURCE :                                             \
683 	     CPA_STATUS_SUCCESS)
684 
685 /**
686  *******************************************************************************
687  * @ingroup LacCommon
688  *      This macro checks a semaphore and returns the status
689  *
690  * @param[in] sid               The semaphore
691  *
692  * @retval CPA_STATUS_SUCCESS   Function executed successfully.
693  * @retval CPA_STATUS_RESOURCE  Error with semaphore
694  ******************************************************************************/
695 #define LAC_CHECK_SEMAPHORE(sid)                                               \
696 	((CPA_STATUS_SUCCESS != qatUtilsSemaphoreTryWait(&sid)) ?              \
697 	     CPA_STATUS_RETRY :                                                \
698 	     CPA_STATUS_SUCCESS)
699 
700 /**
701  *******************************************************************************
702  * @ingroup LacCommon
703  *      This macro post a semaphore and returns the status
704  *
705  * @param[in] sid               The semaphore
706  *
707  * @retval CPA_STATUS_SUCCESS   Function executed successfully.
708  * @retval CPA_STATUS_RESOURCE  Error with semaphore
709  ******************************************************************************/
710 #define LAC_POST_SEMAPHORE(sid)                                                \
711 	((CPA_STATUS_SUCCESS != qatUtilsSemaphorePost(&sid)) ?                 \
712 	     CPA_STATUS_RESOURCE :                                             \
713 	     CPA_STATUS_SUCCESS)
714 /**
715  *******************************************************************************
716  * @ingroup LacCommon
717  *      This macro initialises a semaphore and returns the status
718  *
719  * @param[in] sid               The semaphore
720  * @param[in] semValue          Initial semaphore value
721  *
722  * @retval CPA_STATUS_SUCCESS   Function executed successfully.
723  * @retval CPA_STATUS_RESOURCE  Error with semaphore
724  ******************************************************************************/
725 #define LAC_INIT_SEMAPHORE(sid, semValue)                                      \
726 	((CPA_STATUS_SUCCESS != qatUtilsSemaphoreInit(&sid, semValue)) ?       \
727 	     CPA_STATUS_RESOURCE :                                             \
728 	     CPA_STATUS_SUCCESS)
729 
730 /**
731  *******************************************************************************
732  * @ingroup LacCommon
733  *      This macro destroys a semaphore and returns the status
734  *
735  * @param[in] sid               The semaphore
736  *
737  * @retval CPA_STATUS_SUCCESS   Function executed successfully.
738  * @retval CPA_STATUS_RESOURCE  Error with semaphore
739  ******************************************************************************/
740 #define LAC_DESTROY_SEMAPHORE(sid)                                             \
741 	((CPA_STATUS_SUCCESS != qatUtilsSemaphoreDestroy(&sid)) ?              \
742 	     CPA_STATUS_RESOURCE :                                             \
743 	     CPA_STATUS_SUCCESS)
744 
745 /*
746 *******************************************************************************
747 * Spinlock Macros
748 *******************************************************************************
749 */
750 typedef struct mtx *lac_lock_t;
751 #define LAC_SPINLOCK_INIT(lock)                                                \
752 	((CPA_STATUS_SUCCESS != qatUtilsLockInit(lock)) ?                      \
753 	     CPA_STATUS_RESOURCE :                                             \
754 	     CPA_STATUS_SUCCESS)
755 #define LAC_SPINLOCK(lock)                                                     \
756 	({                                                                     \
757 		(void)qatUtilsLock(lock);                                      \
758 	})
759 #define LAC_SPINUNLOCK(lock)                                                   \
760 	({                                                                     \
761 		(void)qatUtilsUnlock(lock);                                    \
762 	})
763 #define LAC_SPINLOCK_DESTROY(lock)                                             \
764 	({                                                                     \
765 		(void)qatUtilsLockDestroy(lock);                               \
766 	})
767 
768 #define LAC_CONST_PTR_CAST(castee) ((void *)(LAC_ARCH_UINT)(castee))
769 #define LAC_CONST_VOLATILE_PTR_CAST(castee) ((void *)(LAC_ARCH_UINT)(castee))
770 
771 /* Type of ring */
772 #define SAL_RING_TYPE_NONE 0
773 #define SAL_RING_TYPE_A_SYM_HI 1
774 #define SAL_RING_TYPE_A_SYM_LO 2
775 #define SAL_RING_TYPE_A_ASYM 3
776 #define SAL_RING_TYPE_B_SYM_HI 4
777 #define SAL_RING_TYPE_B_SYM_LO 5
778 #define SAL_RING_TYPE_B_ASYM 6
779 #define SAL_RING_TYPE_DC 7
780 #define SAL_RING_TYPE_ADMIN 8
781 #define SAL_RING_TYPE_TRNG 9
782 
783 /* Maps Ring Service to generic service type */
784 static inline icp_adf_ringInfoService_t
lac_getRingType(int type)785 lac_getRingType(int type)
786 {
787 	switch (type) {
788 	case SAL_RING_TYPE_NONE:
789 		return ICP_ADF_RING_SERVICE_0;
790 	case SAL_RING_TYPE_A_SYM_HI:
791 		return ICP_ADF_RING_SERVICE_1;
792 	case SAL_RING_TYPE_A_SYM_LO:
793 		return ICP_ADF_RING_SERVICE_2;
794 	case SAL_RING_TYPE_A_ASYM:
795 		return ICP_ADF_RING_SERVICE_3;
796 	case SAL_RING_TYPE_B_SYM_HI:
797 		return ICP_ADF_RING_SERVICE_4;
798 	case SAL_RING_TYPE_B_SYM_LO:
799 		return ICP_ADF_RING_SERVICE_5;
800 	case SAL_RING_TYPE_B_ASYM:
801 		return ICP_ADF_RING_SERVICE_6;
802 	case SAL_RING_TYPE_DC:
803 		return ICP_ADF_RING_SERVICE_7;
804 	case SAL_RING_TYPE_ADMIN:
805 		return ICP_ADF_RING_SERVICE_8;
806 	case SAL_RING_TYPE_TRNG:
807 		return ICP_ADF_RING_SERVICE_9;
808 	default:
809 		return ICP_ADF_RING_SERVICE_0;
810 	}
811 	return ICP_ADF_RING_SERVICE_0;
812 }
813 
814 /* Maps generic service type to Ring Service type  */
815 static inline int
lac_getServiceType(icp_adf_ringInfoService_t type)816 lac_getServiceType(icp_adf_ringInfoService_t type)
817 {
818 	switch (type) {
819 	case ICP_ADF_RING_SERVICE_0:
820 		return SAL_RING_TYPE_NONE;
821 	case ICP_ADF_RING_SERVICE_1:
822 		return SAL_RING_TYPE_A_SYM_HI;
823 	case ICP_ADF_RING_SERVICE_2:
824 		return SAL_RING_TYPE_A_SYM_LO;
825 	case ICP_ADF_RING_SERVICE_3:
826 		return SAL_RING_TYPE_A_ASYM;
827 	case ICP_ADF_RING_SERVICE_4:
828 		return SAL_RING_TYPE_B_SYM_HI;
829 	case ICP_ADF_RING_SERVICE_5:
830 		return SAL_RING_TYPE_B_SYM_LO;
831 	case ICP_ADF_RING_SERVICE_6:
832 		return SAL_RING_TYPE_B_ASYM;
833 	case ICP_ADF_RING_SERVICE_7:
834 		return SAL_RING_TYPE_DC;
835 	case ICP_ADF_RING_SERVICE_8:
836 		return SAL_RING_TYPE_ADMIN;
837 	default:
838 		return SAL_RING_TYPE_NONE;
839 	}
840 	return SAL_RING_TYPE_NONE;
841 }
842 
843 #endif /* LAC_COMMON_H */
844