1 /* SPDX-License-Identifier: BSD-3-Clause */
2 /* Copyright(c) 2007-2022 Intel Corporation */
3 /* $FreeBSD$ */
4 /**
5  *****************************************************************************
6  * @file lac_sal.h
7  *
8  * @defgroup SalCtrl Service Access Layer Controller
9  *
10  * @ingroup SalCtrl
11  *
12  * @description
13  *      These functions are the functions to be executed for each state
14  *      of the state machine for each service.
15  *
16  *****************************************************************************/
17 
18 #ifndef LAC_SAL_H
19 #define LAC_SAL_H
20 
21 #include "cpa_cy_im.h"
22 
23 /**
24 *******************************************************************************
25  * @ingroup SalCtrl
26  * @description
27  *      This function allocates memory for a specific instance type.
28  *      Zeros this memory and sets the generic service section of
29  *      the instance memory.
30  *
31  * @context
32  *      This function is called from the generic services init.
33  *
34  * @assumptions
35  *      None
36  * @sideEffects
37  *      None
38  * @reentrant
39  *      No
40  * @threadSafe
41  *      Yes
42  *
43  * @param[in]  service         The type of the service to be created
44  *                             (e.g. CRYPTO)
45  * @param[in]  instance_num    The logical instance number which will
46  *                             run the service
47  * @param[out] pObj            Pointer to specific service instance memory
48  * @retVal CPA_STATUS_SUCCESS  Instance memory successfully allocated
49  * @retVal CPA_STATUS_RESOURCE Instance memory not successfully allocated
50  * @retVal CPA_STATUS_FAIL     Unsupported service type
51  *
52  *****************************************************************************/
53 CpaStatus SalCtrl_ServiceCreate(sal_service_type_t service,
54 				Cpa32U instance_num,
55 				sal_service_t **pObj);
56 
57 /******************************************************************************
58  * @ingroup SalCtl
59  * @description
60  *      This macro goes through the 'list' passed in as a parameter. For each
61  *      element found in the list, it peforms a cast to the type of the element
62  *      given by the 'type' parameter. Finally, it calls the function given by
63  *      the 'function' parameter, passing itself and the device as parameters.
64  *
65  *      In case of error (i.e. 'function' does not return _SUCCESS or _RETRY)
66  *      processing of the 'list' elements will stop and the status_ret will be
67  *      updated.
68  *
69  *      In case of _RETRY status_ret will be updated but the 'list'
70  *      will continue to be processed. _RETRY is only expected when
71  *      'function' is stop.
72  *
73  * @context
74  *      This macro is used by both the service and qat event handlers.
75  *
76  * @assumptions
77  *      None
78  * @sideEffects
79  *      None
80  *
81  * @param[in]  list             The list of services or qats as a type of list_t
82  * @param[in]  type             It identifies the type of the object inside the
83  *                              list: service or qat
84  * @param[in]  device           The ADF accelerator handle for the device
85  * @param[in]  function         The function pointer to call
86  * @param[in/out] status_ret    If an error occurred (i.e. status returned from
87  *                              function is not _SUCCESS) then status_ret is
88  *                              overwritten with status returned from function.
89  *
90  *****************************************************************************/
91 #define SAL_FOR_EACH(list, type, device, function, status_ret)                 \
92 	do {                                                                   \
93 		sal_list_t *curr_element = list;                               \
94 		CpaStatus status_temp = CPA_STATUS_SUCCESS;                    \
95 		typeof(type) *process = NULL;                                  \
96 		while (NULL != curr_element) {                                 \
97 			process =                                              \
98 			    (typeof(type) *)SalList_getObject(curr_element);   \
99 			status_temp = process->function(device, process);      \
100 			if ((CPA_STATUS_SUCCESS != status_temp) &&             \
101 			    (CPA_STATUS_RETRY != status_temp)) {               \
102 				status_ret = status_temp;                      \
103 				break;                                         \
104 			} else {                                               \
105 				if (CPA_STATUS_RETRY == status_temp) {         \
106 					status_ret = status_temp;              \
107 				}                                              \
108 			}                                                      \
109 			curr_element = SalList_next(curr_element);             \
110 		}                                                              \
111 	} while (0)
112 
113 /**
114 *******************************************************************************
115  * @ingroup SalCtl
116  * @description
117  *      This macro goes through the 'list' passed in as a parameter. For each
118  *      element found in the list, it peforms a cast to the type of the element
119  *      given by the 'type' parameter. Finally, it checks the state of the
120  *      element and if it is in state 'state_check' then it calls the
121  *      function given by the 'function' parameter, passing itself
122  *      and the device as parameters.
123  *      If the element is not in 'state_check' it returns from the macro.
124  *
125  *      In case of error (i.e. 'function' does not return _SUCCESS)
126  *      processing of the 'list' elements will continue.
127  *
128  * @context
129  *      This macro is used by both the service and qat event handlers.
130  *
131  * @assumptions
132  *      None
133  * @sideEffects
134  *      None
135  *
136  * @param[in]  list             The list of services or qats as a type of list_t
137  * @param[in]  type             It identifies the type of the object
138  *                              inside the list: service or qat
139  * @param[in]  device           The ADF accelerator handle for the device
140  * @param[in]  function         The function pointer to call
141  * @param[in]  state_check      The state to check for
142  *
143  *****************************************************************************/
144 #define SAL_FOR_EACH_STATE(list, type, device, function, state_check)          \
145 	do {                                                                   \
146 		sal_list_t *curr_element = list;                               \
147 		typeof(type) *process = NULL;                                  \
148 		while (NULL != curr_element) {                                 \
149 			process =                                              \
150 			    (typeof(type) *)SalList_getObject(curr_element);   \
151 			if (process->state == state_check) {                   \
152 				process->function(device, process);            \
153 			} else {                                               \
154 				break;                                         \
155 			}                                                      \
156 			curr_element = SalList_next(curr_element);             \
157 		}                                                              \
158 	} while (0)
159 
160 /*************************************************************************
161  * @ingroup SalCtrl
162  * @description
163  *      This function is used to initialize an instance of crypto service.
164  *   It creates a crypto instance's memory pools. It calls ADF to create
165  *   its required transport handles. It calls the sub crypto service init
166  *   functions. Resets the stats.
167  *
168  * @context
169  *    This function is called from the SalCtrl_ServiceEventInit function.
170  *
171  * @assumptions
172  *      None
173  * @sideEffects
174  *      None
175  * @reentrant
176  *      No
177  * @threadSafe
178  *      No (ADF ensures that this function doesn't need to be thread safe)
179  *
180  * @param[in] device    An icp_accel_dev_t* type
181  * @param[in] service   A crypto instance
182  *
183  *************************************************************************/
184 CpaStatus SalCtrl_CryptoInit(icp_accel_dev_t *device, sal_service_t *service);
185 
186 /*************************************************************************
187  * @ingroup SalCtrl
188  * @description
189  *      This function is used to start an instance of crypto service.
190  *  It sends the first messages to FW on its crypto instance transport
191  *  handles. For asymmetric crypto it verifies the header on the downloaded
192  *  MMP library.
193  *
194  * @context
195  *    This function is called from the SalCtrl_ServiceEventStart function.
196  *
197  * @assumptions
198  *      None
199  * @sideEffects
200  *      None
201  * @reentrant
202  *      No
203  * @threadSafe
204  *      No (ADF ensures that this function doesn't need to be thread safe)
205  *
206  * @param[in] device    An icp_accel_dev_t* type
207  * @param[in] service   A crypto instance
208  *
209  *************************************************************************/
210 CpaStatus SalCtrl_CryptoStart(icp_accel_dev_t *device, sal_service_t *service);
211 
212 /*************************************************************************
213  * @ingroup SalCtrl
214  * @description
215  *      This function is used to stop an instance of crypto service.
216  *  It checks for inflight messages to the FW. If no messages are pending
217  * it returns success. If messages are pending it returns retry.
218  *
219  * @context
220  *    This function is called from the SalCtrl_ServiceEventStop function.
221  *
222  * @assumptions
223  *      None
224  * @sideEffects
225  *      None
226  * @reentrant
227  *      No
228  * @threadSafe
229  *      No (ADF ensures that this function doesn't need to be thread safe)
230  *
231  * @param[in] device    An icp_accel_dev_t* type
232  * @param[in] service   A crypto instance
233  *
234  *************************************************************************/
235 CpaStatus SalCtrl_CryptoStop(icp_accel_dev_t *device, sal_service_t *service);
236 
237 /*************************************************************************
238  * @ingroup SalCtrl
239  * @description
240  *      This function is used to shutdown an instance of crypto service.
241  *  It frees resources allocated at initialisation - e.g. frees the
242  *  memory pools and ADF transport handles.
243  *
244  * @context
245  *    This function is called from the SalCtrl_ServiceEventShutdown function.
246  *
247  * @assumptions
248  *      None
249  * @sideEffects
250  *      None
251  * @reentrant
252  *      No
253  * @threadSafe
254  *      No (ADF ensures that this function doesn't need to be thread safe)
255  *
256  * @param[in] device    An icp_accel_dev_t* type
257  * @param[in] service   A crypto instance
258  *
259  *************************************************************************/
260 CpaStatus SalCtrl_CryptoShutdown(icp_accel_dev_t *device,
261 				 sal_service_t *service);
262 
263 /*************************************************************************
264  * @ingroup SalCtrl
265  * @description
266  *      This function sets the capability info of crypto instances.
267  *
268  * @context
269  *    This function is called from the cpaCyQueryCapabilities and
270  *    LacSymSession_ParamCheck function.
271  *
272  * @assumptions
273  *      None
274  * @sideEffects
275  *      None
276  * @reentrant
277  *      No
278  * @threadSafe
279  *      No (ADF ensures that this function doesn't need to be thread safe)
280  *
281  * @param[in] service            A sal_service_t* type
282  * @param[in] cyCapabilityInfo   A CpaCyCapabilitiesInfo* type
283  *
284  *************************************************************************/
285 void SalCtrl_CyQueryCapabilities(sal_service_t *pGenericService,
286 				 CpaCyCapabilitiesInfo *pCapInfo);
287 
288 /*************************************************************************
289  * @ingroup SalCtrl
290  * @description
291  *      This function is used to initialize an instance of compression service.
292  *   It creates a compression instance's memory pools. It calls ADF to create
293  *   its required transport handles. It zeros an instances stats.
294  *
295  * @context
296  *    This function is called from the SalCtrl_ServiceEventInit function.
297  *
298  * @assumptions
299  *      None
300  * @sideEffects
301  *      None
302  * @reentrant
303  *      No
304  * @threadSafe
305  *      No (ADF ensures that this function doesn't need to be thread safe)
306  *
307  * @param[in] device    An icp_accel_dev_t* type
308  * @param[in] service   A compression instance
309  *
310  *************************************************************************/
311 
312 CpaStatus SalCtrl_CompressionInit(icp_accel_dev_t *device,
313 				  sal_service_t *service);
314 
315 /*************************************************************************
316  * @ingroup SalCtrl
317  * @description
318  *      This function is used to start an instance of compression service.
319  *
320  * @context
321  *    This function is called from the SalCtrl_ServiceEventStart function.
322  *
323  * @assumptions
324  *      None
325  * @sideEffects
326  *      None
327  * @reentrant
328  *      No
329  * @threadSafe
330  *      No (ADF ensures that this function doesn't need to be thread safe)
331  *
332  * @param[in] device    An icp_accel_dev_t* type
333  * @param[in] service   A compression instance
334  *
335  *************************************************************************/
336 
337 CpaStatus SalCtrl_CompressionStart(icp_accel_dev_t *device,
338 				   sal_service_t *service);
339 
340 /*************************************************************************
341  * @ingroup SalCtrl
342  * @description
343  *      This function is used to stop an instance of compression service.
344  *  It checks for inflight messages to the FW. If no messages are pending
345  * it returns success. If messages are pending it returns retry.
346  *
347  * @context
348  *    This function is called from the SalCtrl_ServiceEventStop function.
349  *
350  * @assumptions
351  *      None
352  * @sideEffects
353  *      None
354  * @reentrant
355  *      No
356  * @threadSafe
357  *      No (ADF ensures that this function doesn't need to be thread safe)
358  *
359  * @param[in] device    An icp_accel_dev_t* type
360  * @param[in] service   A compression instance
361  *
362  *************************************************************************/
363 
364 CpaStatus SalCtrl_CompressionStop(icp_accel_dev_t *device,
365 				  sal_service_t *service);
366 
367 /*************************************************************************
368  * @ingroup SalCtrl
369  * @description
370  *      This function is used to shutdown an instance of compression service.
371  *  It frees resources allocated at initialisation - e.g. frees the
372  *  memory pools and ADF transport handles.
373  *
374  * @context
375  *    This function is called from the SalCtrl_ServiceEventShutdown function.
376  *
377  * @assumptions
378  *      None
379  * @sideEffects
380  *      None
381  * @reentrant
382  *      No
383  * @threadSafe
384  *      No (ADF ensures that this function doesn't need to be thread safe)
385  *
386  * @param[in] device    An icp_accel_dev_t* type
387  * @param[in] service   A compression instance
388  *
389  *************************************************************************/
390 
391 CpaStatus SalCtrl_CompressionShutdown(icp_accel_dev_t *device,
392 				      sal_service_t *service);
393 
394 /*************************************************************************
395  * @ingroup SalCtrl
396  * @description
397  *    This function is used to get the number of services enabled
398  *    from the config table.
399  *
400  * @context
401  *    This function is called from the SalCtrl_QatInit
402  *
403  * @assumptions
404  *      None
405  * @sideEffects
406  *      None
407  * @reentrant
408  *      No
409  * @threadSafe
410  *      No
411  *
412  * param[in] device            An icp_accel_dev_t* type
413  * param[in] pEnabledServices  pointer to a variable used to store
414  *                             the enabled services
415  *
416  *************************************************************************/
417 
418 CpaStatus SalCtrl_GetEnabledServices(icp_accel_dev_t *device,
419 				     Cpa32U *pEnabledServices);
420 
421 /*************************************************************************
422  * @ingroup SalCtrl
423  * @description
424  *    This function is used to check if a service is enabled
425  *
426  * @context
427  *    This function is called from the SalCtrl_QatInit
428  *
429  * @assumptions
430  *      None
431  * @sideEffects
432  *      None
433  * @reentrant
434  *      No
435  * @threadSafe
436  *      Yes
437  *
438  * param[in] enabled_services
439  * param[in] service
440  *
441  *************************************************************************/
442 
443 CpaBoolean SalCtrl_IsServiceEnabled(Cpa32U enabled_services,
444 				    sal_service_type_t service);
445 
446 /*************************************************************************
447  * @ingroup SalCtrl
448  * @description
449  *    This function is used to check if a service is supported on the device
450  *    The key difference between this and SalCtrl_GetSupportedServices() is
451  *    that the latter treats it as an error if the service is unsupported.
452  *
453  * @context
454  *      This can be called anywhere.
455  *
456  * @assumptions
457  *      None
458  * @sideEffects
459  *      None
460  * @reentrant
461  *      No
462  * @threadSafe
463  *      Yes
464  *
465  * param[in] device
466  * param[in] service    service or services to check
467  *
468  *************************************************************************/
469 CpaBoolean SalCtrl_IsServiceSupported(icp_accel_dev_t *device,
470 				      sal_service_type_t service);
471 
472 /*************************************************************************
473  * @ingroup SalCtrl
474  * @description
475  *    This function is used to check whether enabled services has associated
476  *    hardware capability support
477  *
478  * @context
479  *      This functions is called from the SalCtrl_ServiceEventInit function.
480  *
481  * @assumptions
482  *      None
483  * @sideEffects
484  *      None
485  * @reentrant
486  *      No
487  * @threadSafe
488  *      Yes
489  *
490  * param[in] device              A pointer to an icp_accel_dev_t
491  * param[in] enabled_services    It is the bitmask for the enabled services
492  *************************************************************************/
493 
494 CpaStatus SalCtrl_GetSupportedServices(icp_accel_dev_t *device,
495 				       Cpa32U enabled_services);
496 
497 #endif
498