xref: /freebsd/sys/dev/qat/qat_api/common/ctrl/sal_crypto.c (revision 59c8e88e72633afbc47a4ace0d2170d00d51f7dc)
1 /* SPDX-License-Identifier: BSD-3-Clause */
2 /* Copyright(c) 2007-2022 Intel Corporation */
3 
4 /**
5  ***************************************************************************
6  * @file sal_crypto.c     Instance handling functions for crypto
7  *
8  * @ingroup SalCtrl
9  *
10  ***************************************************************************/
11 
12 /*
13 *******************************************************************************
14 * Include public/global header files
15 *******************************************************************************
16 */
17 
18 /* QAT-API includes */
19 #include "cpa.h"
20 #include "cpa_types.h"
21 #include "cpa_cy_common.h"
22 #include "cpa_cy_im.h"
23 #include "cpa_cy_key.h"
24 #include "cpa_cy_sym.h"
25 
26 #include "qat_utils.h"
27 
28 /* ADF includes */
29 #include "icp_adf_init.h"
30 #include "icp_adf_transport.h"
31 #include "icp_accel_devices.h"
32 #include "icp_adf_cfg.h"
33 #include "icp_adf_accel_mgr.h"
34 #include "icp_adf_poll.h"
35 #include "icp_adf_debug.h"
36 
37 /* SAL includes */
38 #include "lac_log.h"
39 #include "lac_mem.h"
40 #include "lac_mem_pools.h"
41 #include "sal_statistics.h"
42 #include "lac_common.h"
43 #include "lac_list.h"
44 #include "lac_hooks.h"
45 #include "lac_sym_qat_hash_defs_lookup.h"
46 #include "lac_sym.h"
47 #include "lac_sym_key.h"
48 #include "lac_sym_hash.h"
49 #include "lac_sym_cb.h"
50 #include "lac_sym_stats.h"
51 #include "lac_sal_types_crypto.h"
52 #include "lac_sal.h"
53 #include "lac_sal_ctrl.h"
54 #include "sal_string_parse.h"
55 #include "sal_service_state.h"
56 #include "icp_sal_poll.h"
57 #include "lac_sync.h"
58 #include "lac_sym_qat.h"
59 #include "icp_sal_versions.h"
60 #include "icp_sal_user.h"
61 #include "sal_hw_gen.h"
62 
63 #define HMAC_MODE_1 1
64 #define HMAC_MODE_2 2
65 #define TH_CY_RX_0 0
66 #define TH_CY_RX_1 1
67 #define MAX_CY_RX_RINGS 2
68 
69 #define DOUBLE_INCR 2
70 
71 #define TH_SINGLE_RX 0
72 #define NUM_CRYPTO_SYM_RX_RINGS 1
73 #define NUM_CRYPTO_ASYM_RX_RINGS 1
74 #define NUM_CRYPTO_NRBG_RX_RINGS 1
75 
76 static CpaInstanceHandle
77 Lac_CryptoGetFirstHandle(void)
78 {
79 	CpaInstanceHandle instHandle;
80 	instHandle = Lac_GetFirstHandle(SAL_SERVICE_TYPE_CRYPTO);
81 	if (!instHandle) {
82 		instHandle = Lac_GetFirstHandle(SAL_SERVICE_TYPE_CRYPTO_SYM);
83 		if (!instHandle) {
84 			instHandle =
85 			    Lac_GetFirstHandle(SAL_SERVICE_TYPE_CRYPTO_ASYM);
86 		}
87 	}
88 	return instHandle;
89 }
90 
91 
92 /* Function to release the sym handles. */
93 static CpaStatus
94 SalCtrl_SymReleaseTransHandle(sal_service_t *service)
95 {
96 
97 	CpaStatus status = CPA_STATUS_SUCCESS;
98 	CpaStatus ret_status = CPA_STATUS_SUCCESS;
99 	sal_crypto_service_t *pCryptoService = (sal_crypto_service_t *)service;
100 
101 	if (NULL != pCryptoService->trans_handle_sym_tx) {
102 		status = icp_adf_transReleaseHandle(
103 		    pCryptoService->trans_handle_sym_tx);
104 		if (CPA_STATUS_SUCCESS != status) {
105 			ret_status = status;
106 		}
107 	}
108 	if (NULL != pCryptoService->trans_handle_sym_rx) {
109 		status = icp_adf_transReleaseHandle(
110 		    pCryptoService->trans_handle_sym_rx);
111 		if (CPA_STATUS_SUCCESS != status) {
112 			ret_status = status;
113 		}
114 	}
115 
116 	return ret_status;
117 }
118 
119 
120 /*
121  * @ingroup sal_crypto
122  *     Frees resources (memory and transhandles) if allocated
123  *
124  * @param[in]  pCryptoService       Pointer to sym service instance
125  * @retval                          SUCCESS if transhandles released
126  *                                  successfully.
127 */
128 static CpaStatus
129 SalCtrl_SymFreeResources(sal_crypto_service_t *pCryptoService)
130 {
131 
132 	CpaStatus status = CPA_STATUS_SUCCESS;
133 
134 	/* Free memory pools if not NULL */
135 	Lac_MemPoolDestroy(pCryptoService->lac_sym_cookie_pool);
136 
137 	/* Free misc memory if allocated */
138 	/* Frees memory allocated for Hmac precomputes */
139 	LacSymHash_HmacPrecompShutdown(pCryptoService);
140 	/* Free memory allocated for key labels
141 	   Also clears key stats  */
142 	LacSymKey_Shutdown(pCryptoService);
143 	/* Free hash lookup table if allocated */
144 	if (NULL != pCryptoService->pLacHashLookupDefs) {
145 		LAC_OS_FREE(pCryptoService->pLacHashLookupDefs);
146 	}
147 
148 	/* Free statistics */
149 	LacSym_StatsFree(pCryptoService);
150 
151 	/* Free transport handles */
152 	status = SalCtrl_SymReleaseTransHandle((sal_service_t *)pCryptoService);
153 	return status;
154 }
155 
156 
157 /**
158  ***********************************************************************
159  * @ingroup SalCtrl
160  *   This macro verifies that the status is _SUCCESS
161  *   If status is not _SUCCESS then Sym Instance resources are
162  *   freed before the function returns the error
163  *
164  * @param[in] status    status we are checking
165  *
166  * @return void         status is ok (CPA_STATUS_SUCCESS)
167  * @return status       The value in the status parameter is an error one
168  *
169  ****************************************************************************/
170 #define LAC_CHECK_STATUS_SYM_INIT(status)                                      \
171 	do {                                                                   \
172 		if (CPA_STATUS_SUCCESS != status) {                            \
173 			SalCtrl_SymFreeResources(pCryptoService);              \
174 			return status;                                         \
175 		}                                                              \
176 	} while (0)
177 
178 
179 /* Function that creates the Sym Handles. */
180 static CpaStatus
181 SalCtrl_SymCreateTransHandle(icp_accel_dev_t *device,
182 			     sal_service_t *service,
183 			     Cpa32U numSymRequests,
184 			     char *section)
185 {
186 	CpaStatus status = CPA_STATUS_SUCCESS;
187 	char temp_string[SAL_CFG_MAX_VAL_LEN_IN_BYTES] = { 0 };
188 	sal_crypto_service_t *pCryptoService = (sal_crypto_service_t *)service;
189 	icp_resp_deliv_method rx_resp_type = ICP_RESP_TYPE_IRQ;
190 	Cpa32U msgSize = 0;
191 
192 	if (SAL_RESP_POLL_CFG_FILE == pCryptoService->isPolled) {
193 		rx_resp_type = ICP_RESP_TYPE_POLL;
194 	}
195 
196 	if (CPA_FALSE == pCryptoService->generic_service_info.is_dyn) {
197 		section = icpGetProcessName();
198 	}
199 
200 	/* Parse Sym ring details */
201 	status =
202 	    Sal_StringParsing("Cy",
203 			      pCryptoService->generic_service_info.instance,
204 			      "RingSymTx",
205 			      temp_string);
206 
207 	/* Need to free resources in case not _SUCCESS from here */
208 	LAC_CHECK_STATUS_SYM_INIT(status);
209 
210 	msgSize = LAC_QAT_SYM_REQ_SZ_LW * LAC_LONG_WORD_IN_BYTES;
211 	status =
212 	    icp_adf_transCreateHandle(device,
213 				      ICP_TRANS_TYPE_ETR,
214 				      section,
215 				      pCryptoService->acceleratorNum,
216 				      pCryptoService->bankNumSym,
217 				      temp_string,
218 				      lac_getRingType(SAL_RING_TYPE_A_SYM_HI),
219 				      NULL,
220 				      ICP_RESP_TYPE_NONE,
221 				      numSymRequests,
222 				      msgSize,
223 				      (icp_comms_trans_handle *)&(
224 					  pCryptoService->trans_handle_sym_tx));
225 	LAC_CHECK_STATUS_SYM_INIT(status);
226 
227 	status =
228 	    Sal_StringParsing("Cy",
229 			      pCryptoService->generic_service_info.instance,
230 			      "RingSymRx",
231 			      temp_string);
232 	LAC_CHECK_STATUS_SYM_INIT(status);
233 
234 	msgSize = LAC_QAT_SYM_RESP_SZ_LW * LAC_LONG_WORD_IN_BYTES;
235 	status = icp_adf_transCreateHandle(
236 	    device,
237 	    ICP_TRANS_TYPE_ETR,
238 	    section,
239 	    pCryptoService->acceleratorNum,
240 	    pCryptoService->bankNumSym,
241 	    temp_string,
242 	    lac_getRingType(SAL_RING_TYPE_NONE),
243 	    (icp_trans_callback)LacSymQat_SymRespHandler,
244 	    rx_resp_type,
245 	    numSymRequests,
246 	    msgSize,
247 	    (icp_comms_trans_handle *)&(pCryptoService->trans_handle_sym_rx));
248 	LAC_CHECK_STATUS_SYM_INIT(status);
249 
250 	return status;
251 }
252 
253 static int
254 SalCtrl_CryptoDebug(void *private_data, char *data, int size, int offset)
255 {
256 	CpaStatus status = CPA_STATUS_SUCCESS;
257 	Cpa32U len = 0;
258 	sal_crypto_service_t *pCryptoService =
259 	    (sal_crypto_service_t *)private_data;
260 
261 	switch (offset) {
262 	case SAL_STATS_SYM: {
263 		CpaCySymStats64 symStats = { 0 };
264 		if (CPA_TRUE !=
265 		    pCryptoService->generic_service_info.stats
266 			->bSymStatsEnabled) {
267 			break;
268 		}
269 		status = cpaCySymQueryStats64(pCryptoService, &symStats);
270 		if (status != CPA_STATUS_SUCCESS) {
271 			LAC_LOG_ERROR("cpaCySymQueryStats64 returned error\n");
272 			return 0;
273 		}
274 
275 		/* Engine Info */
276 		len += snprintf(
277 		    data + len,
278 		    size - len,
279 		    SEPARATOR BORDER
280 		    " Statistics for Instance %24s |\n" BORDER
281 		    " Symmetric Stats                                  " BORDER
282 		    "\n" SEPARATOR,
283 		    pCryptoService->debug_file->name);
284 
285 		/* Session Info */
286 		len += snprintf(
287 		    data + len,
288 		    size - len,
289 		    BORDER " Sessions Initialized:           %16llu " BORDER
290 			   "\n" BORDER
291 			   " Sessions Removed:               %16llu " BORDER
292 			   "\n" BORDER
293 			   " Session Errors:                 %16llu " BORDER
294 			   "\n" SEPARATOR,
295 		    (long long unsigned int)symStats.numSessionsInitialized,
296 		    (long long unsigned int)symStats.numSessionsRemoved,
297 		    (long long unsigned int)symStats.numSessionErrors);
298 
299 		/* Session info */
300 		len += snprintf(
301 		    data + len,
302 		    size - len,
303 		    BORDER " Symmetric Requests:             %16llu " BORDER
304 			   "\n" BORDER
305 			   " Symmetric Request Errors:       %16llu " BORDER
306 			   "\n" BORDER
307 			   " Symmetric Completed:            %16llu " BORDER
308 			   "\n" BORDER
309 			   " Symmetric Completed Errors:     %16llu " BORDER
310 			   "\n" BORDER
311 			   " Symmetric Verify Failures:      %16llu " BORDER
312 			   "\n",
313 		    (long long unsigned int)symStats.numSymOpRequests,
314 		    (long long unsigned int)symStats.numSymOpRequestErrors,
315 		    (long long unsigned int)symStats.numSymOpCompleted,
316 		    (long long unsigned int)symStats.numSymOpCompletedErrors,
317 		    (long long unsigned int)symStats.numSymOpVerifyFailures);
318 		break;
319 	}
320 	default: {
321 		len += snprintf(data + len, size - len, SEPARATOR);
322 		return 0;
323 	}
324 	}
325 	return ++offset;
326 }
327 
328 
329 static CpaStatus
330 SalCtrl_SymInit(icp_accel_dev_t *device, sal_service_t *service)
331 {
332 	CpaStatus status = CPA_STATUS_SUCCESS;
333 	Cpa32U qatHmacMode = 0;
334 	Cpa32U numSymConcurrentReq = 0;
335 	char adfGetParam[ADF_CFG_MAX_VAL_LEN_IN_BYTES] = { 0 };
336 	char temp_string[SAL_CFG_MAX_VAL_LEN_IN_BYTES] = { 0 };
337 	sal_crypto_service_t *pCryptoService = (sal_crypto_service_t *)service;
338 	char *section = DYN_SEC;
339 
340 	/*Instance may not in the DYN section*/
341 	if (CPA_FALSE == pCryptoService->generic_service_info.is_dyn) {
342 		section = icpGetProcessName();
343 	}
344 
345 
346 	/* Register callbacks for the symmetric services
347 	* (Hash, Cipher, Algorithm-Chaining) (returns void)*/
348 	LacSymCb_CallbacksRegister();
349 
350 	qatHmacMode = (Cpa32U)Sal_Strtoul(adfGetParam, NULL, SAL_CFG_BASE_DEC);
351 	switch (qatHmacMode) {
352 	case HMAC_MODE_1:
353 		pCryptoService->qatHmacMode = ICP_QAT_HW_AUTH_MODE1;
354 		break;
355 	case HMAC_MODE_2:
356 		pCryptoService->qatHmacMode = ICP_QAT_HW_AUTH_MODE2;
357 		break;
358 	default:
359 		pCryptoService->qatHmacMode = ICP_QAT_HW_AUTH_MODE1;
360 		break;
361 	}
362 
363 	/* Get num concurrent requests from config file */
364 	status =
365 	    Sal_StringParsing("Cy",
366 			      pCryptoService->generic_service_info.instance,
367 			      "NumConcurrentSymRequests",
368 			      temp_string);
369 	LAC_CHECK_STATUS(status);
370 	status =
371 	    icp_adf_cfgGetParamValue(device, section, temp_string, adfGetParam);
372 	if (CPA_STATUS_SUCCESS != status) {
373 		QAT_UTILS_LOG("Failed to get %s from configuration file\n",
374 			      temp_string);
375 		return status;
376 	}
377 
378 	numSymConcurrentReq =
379 	    (Cpa32U)Sal_Strtoul(adfGetParam, NULL, SAL_CFG_BASE_DEC);
380 	if (CPA_STATUS_FAIL == validateConcurrRequest(numSymConcurrentReq)) {
381 		LAC_LOG_ERROR("Invalid NumConcurrentSymRequests, valid "
382 			      "values {64, 128, 256, ... 32768, 65536}");
383 		return CPA_STATUS_FAIL;
384 	}
385 
386 	/* ADF does not allow us to completely fill the ring for batch requests
387 	 */
388 	pCryptoService->maxNumSymReqBatch =
389 	    (numSymConcurrentReq - SAL_BATCH_SUBMIT_FREE_SPACE);
390 
391 	/* Create transport handles */
392 	status = SalCtrl_SymCreateTransHandle(device,
393 					      service,
394 					      numSymConcurrentReq,
395 					      section);
396 	LAC_CHECK_STATUS(status);
397 
398 	/* Allocates memory pools */
399 
400 	/* Create and initialise symmetric cookie memory pool */
401 	pCryptoService->lac_sym_cookie_pool = LAC_MEM_POOL_INIT_POOL_ID;
402 	status =
403 	    Sal_StringParsing("Cy",
404 			      pCryptoService->generic_service_info.instance,
405 			      "SymPool",
406 			      temp_string);
407 	LAC_CHECK_STATUS_SYM_INIT(status);
408 	/* Note we need twice (i.e. <<1) the number of sym cookies to
409 	   support sym ring pairs (and some, for partials) */
410 	status =
411 	    Lac_MemPoolCreate(&pCryptoService->lac_sym_cookie_pool,
412 			      temp_string,
413 			      ((numSymConcurrentReq + numSymConcurrentReq + 1)
414 			       << 1),
415 			      sizeof(lac_sym_cookie_t),
416 			      LAC_64BYTE_ALIGNMENT,
417 			      CPA_FALSE,
418 			      pCryptoService->nodeAffinity);
419 	LAC_CHECK_STATUS_SYM_INIT(status);
420 	/* For all sym cookies fill out the physical address of data that
421 	   will be set to QAT */
422 	Lac_MemPoolInitSymCookiesPhyAddr(pCryptoService->lac_sym_cookie_pool);
423 
424 	/* Clear stats */
425 	/* Clears Key stats and allocate memory of SSL and TLS labels
426 	    These labels are initialised to standard values */
427 	status = LacSymKey_Init(pCryptoService);
428 	LAC_CHECK_STATUS_SYM_INIT(status);
429 
430 	/* Initialises the hash lookup table*/
431 	status = LacSymQat_Init(pCryptoService);
432 	LAC_CHECK_STATUS_SYM_INIT(status);
433 
434 	/* Fills out content descriptor for precomputes and registers the
435 	   hash precompute callback */
436 	status = LacSymHash_HmacPrecompInit(pCryptoService);
437 	LAC_CHECK_STATUS_SYM_INIT(status);
438 
439 	/* Init the Sym stats */
440 	status = LacSym_StatsInit(pCryptoService);
441 	LAC_CHECK_STATUS_SYM_INIT(status);
442 
443 	return status;
444 }
445 
446 static void
447 SalCtrl_DebugShutdown(icp_accel_dev_t *device, sal_service_t *service)
448 {
449 	sal_crypto_service_t *pCryptoService = (sal_crypto_service_t *)service;
450 	sal_statistics_collection_t *pStatsCollection =
451 	    (sal_statistics_collection_t *)device->pQatStats;
452 
453 	if (CPA_TRUE == pStatsCollection->bStatsEnabled) {
454 		/* Clean stats */
455 		if (NULL != pCryptoService->debug_file) {
456 			icp_adf_debugRemoveFile(pCryptoService->debug_file);
457 			LAC_OS_FREE(pCryptoService->debug_file->name);
458 			LAC_OS_FREE(pCryptoService->debug_file);
459 			pCryptoService->debug_file = NULL;
460 		}
461 	}
462 	pCryptoService->generic_service_info.stats = NULL;
463 }
464 
465 static CpaStatus
466 SalCtrl_DebugInit(icp_accel_dev_t *device, sal_service_t *service)
467 {
468 	char adfGetParam[ADF_CFG_MAX_VAL_LEN_IN_BYTES] = { 0 };
469 	char temp_string[SAL_CFG_MAX_VAL_LEN_IN_BYTES] = { 0 };
470 	char *instance_name = NULL;
471 	sal_crypto_service_t *pCryptoService = (sal_crypto_service_t *)service;
472 	sal_statistics_collection_t *pStatsCollection =
473 	    (sal_statistics_collection_t *)device->pQatStats;
474 	CpaStatus status = CPA_STATUS_SUCCESS;
475 	char *section = DYN_SEC;
476 
477 	/*Instance may not in the DYN section*/
478 	if (CPA_FALSE == pCryptoService->generic_service_info.is_dyn) {
479 		section = icpGetProcessName();
480 	}
481 
482 	if (CPA_TRUE == pStatsCollection->bStatsEnabled) {
483 		/* Get instance name for stats */
484 		instance_name = LAC_OS_MALLOC(ADF_CFG_MAX_VAL_LEN_IN_BYTES);
485 		if (NULL == instance_name) {
486 			return CPA_STATUS_RESOURCE;
487 		}
488 
489 		status = Sal_StringParsing(
490 		    "Cy",
491 		    pCryptoService->generic_service_info.instance,
492 		    "Name",
493 		    temp_string);
494 		if (CPA_STATUS_SUCCESS != status) {
495 			LAC_OS_FREE(instance_name);
496 			return status;
497 		}
498 		status = icp_adf_cfgGetParamValue(device,
499 						  section,
500 						  temp_string,
501 						  adfGetParam);
502 		if (CPA_STATUS_SUCCESS != status) {
503 			QAT_UTILS_LOG(
504 			    "Failed to get %s from configuration file\n",
505 			    temp_string);
506 			LAC_OS_FREE(instance_name);
507 			return status;
508 		}
509 		snprintf(instance_name,
510 			 ADF_CFG_MAX_VAL_LEN_IN_BYTES,
511 			 "%s",
512 			 adfGetParam);
513 
514 		pCryptoService->debug_file =
515 		    LAC_OS_MALLOC(sizeof(debug_file_info_t));
516 		if (NULL == pCryptoService->debug_file) {
517 			LAC_OS_FREE(instance_name);
518 			return CPA_STATUS_RESOURCE;
519 		}
520 
521 		memset(pCryptoService->debug_file,
522 		       0,
523 		       sizeof(debug_file_info_t));
524 		pCryptoService->debug_file->name = instance_name;
525 		pCryptoService->debug_file->seq_read = SalCtrl_CryptoDebug;
526 		pCryptoService->debug_file->private_data = pCryptoService;
527 		pCryptoService->debug_file->parent =
528 		    pCryptoService->generic_service_info.debug_parent_dir;
529 
530 		status =
531 		    icp_adf_debugAddFile(device, pCryptoService->debug_file);
532 		if (CPA_STATUS_SUCCESS != status) {
533 			LAC_OS_FREE(instance_name);
534 			LAC_OS_FREE(pCryptoService->debug_file);
535 			return status;
536 		}
537 	}
538 	pCryptoService->generic_service_info.stats = pStatsCollection;
539 
540 	return status;
541 }
542 
543 static CpaStatus
544 SalCtrl_GetBankNum(icp_accel_dev_t *device,
545 		   Cpa32U inst,
546 		   char *section,
547 		   char *bank_name,
548 		   Cpa16U *bank)
549 {
550 	char adfParamValue[ADF_CFG_MAX_VAL_LEN_IN_BYTES] = { 0 };
551 	char adfParamName[SAL_CFG_MAX_VAL_LEN_IN_BYTES] = { 0 };
552 	CpaStatus status = CPA_STATUS_SUCCESS;
553 
554 	status = Sal_StringParsing("Cy", inst, bank_name, adfParamName);
555 	LAC_CHECK_STATUS(status);
556 	status = icp_adf_cfgGetParamValue(device,
557 					  section,
558 					  adfParamName,
559 					  adfParamValue);
560 	if (CPA_STATUS_SUCCESS != status) {
561 		QAT_UTILS_LOG("Failed to get %s from configuration file\n",
562 			      adfParamName);
563 		return status;
564 	}
565 	*bank = (Cpa16U)Sal_Strtoul(adfParamValue, NULL, SAL_CFG_BASE_DEC);
566 	return status;
567 }
568 
569 static CpaStatus
570 SalCtr_InstInit(icp_accel_dev_t *device, sal_service_t *service)
571 {
572 	char adfGetParam[ADF_CFG_MAX_VAL_LEN_IN_BYTES] = { 0 };
573 	char temp_string[SAL_CFG_MAX_VAL_LEN_IN_BYTES] = { 0 };
574 	char temp_string2[SAL_CFG_MAX_VAL_LEN_IN_BYTES] = { 0 };
575 	sal_crypto_service_t *pCryptoService = (sal_crypto_service_t *)service;
576 	CpaStatus status = CPA_STATUS_SUCCESS;
577 	char *section = DYN_SEC;
578 
579 	/*Instance may not in the DYN section*/
580 	if (CPA_FALSE == pCryptoService->generic_service_info.is_dyn) {
581 		section = icpGetProcessName();
582 	}
583 
584 
585 	/* Get Config Info: Accel Num, bank Num, packageID,
586 				coreAffinity, nodeAffinity and response mode */
587 
588 	pCryptoService->acceleratorNum = 0;
589 
590 	/* Gen4, a bank only has 2 rings (1 ring pair), only one type of service
591 	   can be assigned one time. asym and sym will be in different bank*/
592 	if (isCyGen4x(pCryptoService)) {
593 		switch (service->type) {
594 		case SAL_SERVICE_TYPE_CRYPTO_ASYM:
595 			status = SalCtrl_GetBankNum(
596 			    device,
597 			    pCryptoService->generic_service_info.instance,
598 			    section,
599 			    "BankNumberAsym",
600 			    &pCryptoService->bankNumAsym);
601 			if (CPA_STATUS_SUCCESS != status)
602 				return status;
603 			break;
604 		case SAL_SERVICE_TYPE_CRYPTO_SYM:
605 			status = SalCtrl_GetBankNum(
606 			    device,
607 			    pCryptoService->generic_service_info.instance,
608 			    section,
609 			    "BankNumberSym",
610 			    &pCryptoService->bankNumSym);
611 			if (CPA_STATUS_SUCCESS != status)
612 				return status;
613 			break;
614 		case SAL_SERVICE_TYPE_CRYPTO:
615 			status = SalCtrl_GetBankNum(
616 			    device,
617 			    pCryptoService->generic_service_info.instance,
618 			    section,
619 			    "BankNumberAsym",
620 			    &pCryptoService->bankNumAsym);
621 			if (CPA_STATUS_SUCCESS != status)
622 				return status;
623 			status = SalCtrl_GetBankNum(
624 			    device,
625 			    pCryptoService->generic_service_info.instance,
626 			    section,
627 			    "BankNumberSym",
628 			    &pCryptoService->bankNumSym);
629 			if (CPA_STATUS_SUCCESS != status)
630 				return status;
631 			break;
632 		default:
633 			return CPA_STATUS_FAIL;
634 		}
635 	} else {
636 		status = SalCtrl_GetBankNum(
637 		    device,
638 		    pCryptoService->generic_service_info.instance,
639 		    section,
640 		    "BankNumber",
641 		    &pCryptoService->bankNumSym);
642 		if (CPA_STATUS_SUCCESS != status)
643 			return status;
644 		pCryptoService->bankNumAsym = pCryptoService->bankNumSym;
645 	}
646 
647 	status =
648 	    Sal_StringParsing("Cy",
649 			      pCryptoService->generic_service_info.instance,
650 			      "IsPolled",
651 			      temp_string);
652 	LAC_CHECK_STATUS(status);
653 	status =
654 	    icp_adf_cfgGetParamValue(device, section, temp_string, adfGetParam);
655 	if (CPA_STATUS_SUCCESS != status) {
656 		QAT_UTILS_LOG("Failed to get %s from configuration file\n",
657 			      temp_string);
658 		return status;
659 	}
660 	pCryptoService->isPolled =
661 	    (Cpa8U)Sal_Strtoul(adfGetParam, NULL, SAL_CFG_BASE_DEC);
662 
663 	/* Kernel instances do not support epoll mode */
664 	if (SAL_RESP_EPOLL_CFG_FILE == pCryptoService->isPolled) {
665 		QAT_UTILS_LOG(
666 		    "IsPolled %u is not supported for kernel instance %s",
667 		    pCryptoService->isPolled,
668 		    temp_string);
669 		return CPA_STATUS_FAIL;
670 	}
671 
672 	status = icp_adf_cfgGetParamValue(device,
673 					  LAC_CFG_SECTION_GENERAL,
674 					  ADF_DEV_PKG_ID,
675 					  adfGetParam);
676 	if (CPA_STATUS_SUCCESS != status) {
677 		QAT_UTILS_LOG("Failed to get %s from configuration file\n",
678 			      ADF_DEV_PKG_ID);
679 		return status;
680 	}
681 	pCryptoService->pkgID =
682 	    (Cpa16U)Sal_Strtoul(adfGetParam, NULL, SAL_CFG_BASE_DEC);
683 
684 	status = icp_adf_cfgGetParamValue(device,
685 					  LAC_CFG_SECTION_GENERAL,
686 					  ADF_DEV_NODE_ID,
687 					  adfGetParam);
688 	if (CPA_STATUS_SUCCESS != status) {
689 		QAT_UTILS_LOG("Failed to get %s from configuration file\n",
690 			      ADF_DEV_NODE_ID);
691 		return status;
692 	}
693 	pCryptoService->nodeAffinity =
694 	    (Cpa32U)Sal_Strtoul(adfGetParam, NULL, SAL_CFG_BASE_DEC);
695 	/* In case of interrupt instance, use the bank affinity set by adf_ctl
696 	 * Otherwise, use the instance affinity for backwards compatibility */
697 	if (SAL_RESP_POLL_CFG_FILE != pCryptoService->isPolled) {
698 		/* Next need to read the [AcceleratorX] section of the config
699 		 * file */
700 		status = Sal_StringParsing("Accelerator",
701 					   pCryptoService->acceleratorNum,
702 					   "",
703 					   temp_string2);
704 		LAC_CHECK_STATUS(status);
705 		if (service->type == SAL_SERVICE_TYPE_CRYPTO_ASYM)
706 			status = Sal_StringParsing("Bank",
707 						   pCryptoService->bankNumAsym,
708 						   "CoreAffinity",
709 						   temp_string);
710 		else
711 			/* For cy service, asym bank and sym bank will set the
712 			   same core affinity. So Just read one*/
713 			status = Sal_StringParsing("Bank",
714 						   pCryptoService->bankNumSym,
715 						   "CoreAffinity",
716 						   temp_string);
717 		LAC_CHECK_STATUS(status);
718 	} else {
719 		strncpy(temp_string2, section, (strlen(section) + 1));
720 		status = Sal_StringParsing(
721 		    "Cy",
722 		    pCryptoService->generic_service_info.instance,
723 		    "CoreAffinity",
724 		    temp_string);
725 		LAC_CHECK_STATUS(status);
726 	}
727 
728 	status = icp_adf_cfgGetParamValue(device,
729 					  temp_string2,
730 					  temp_string,
731 					  adfGetParam);
732 	if (CPA_STATUS_SUCCESS != status) {
733 		QAT_UTILS_LOG("Failed to get %s from configuration file\n",
734 			      temp_string);
735 		return status;
736 	}
737 	pCryptoService->coreAffinity =
738 	    (Cpa32U)Sal_Strtoul(adfGetParam, NULL, SAL_CFG_BASE_DEC);
739 
740 	/*No Execution Engine in DH895xcc, so make sure it is zero*/
741 	pCryptoService->executionEngine = 0;
742 
743 	return status;
744 }
745 
746 /* This function:
747  * 1. Creates sym and asym transport handles
748  * 2. Allocates memory pools required by sym and asym services
749 .* 3. Clears the sym and asym stats counters
750  * 4. In case service asym or sym is enabled then this function
751  *    only allocates resources for these services. i.e if the
752  *    service asym is enabled then only asym transport handles
753  *    are created and vice versa.
754  */
755 CpaStatus
756 SalCtrl_CryptoInit(icp_accel_dev_t *device, sal_service_t *service)
757 {
758 	CpaStatus status = CPA_STATUS_SUCCESS;
759 	sal_crypto_service_t *pCryptoService = (sal_crypto_service_t *)service;
760 	sal_service_type_t svc_type = service->type;
761 
762 	SAL_SERVICE_GOOD_FOR_INIT(pCryptoService);
763 	pCryptoService->generic_service_info.state =
764 	    SAL_SERVICE_STATE_INITIALIZING;
765 
766 	/* Set up the instance parameters such as bank number,
767 	 * coreAffinity, pkgId and node affinity etc
768 	 */
769 	status = SalCtr_InstInit(device, service);
770 	LAC_CHECK_STATUS(status);
771 	/* Create debug directory for service */
772 	status = SalCtrl_DebugInit(device, service);
773 	LAC_CHECK_STATUS(status);
774 
775 	switch (svc_type) {
776 	case SAL_SERVICE_TYPE_CRYPTO_ASYM:
777 		break;
778 	case SAL_SERVICE_TYPE_CRYPTO_SYM:
779 		status = SalCtrl_SymInit(device, service);
780 		if (CPA_STATUS_SUCCESS != status) {
781 			SalCtrl_DebugShutdown(device, service);
782 			return status;
783 		}
784 		break;
785 	case SAL_SERVICE_TYPE_CRYPTO:
786 		status = SalCtrl_SymInit(device, service);
787 		if (CPA_STATUS_SUCCESS != status) {
788 			SalCtrl_DebugShutdown(device, service);
789 			return status;
790 		}
791 		break;
792 	default:
793 		LAC_LOG_ERROR("Invalid service type\n");
794 		status = CPA_STATUS_FAIL;
795 		break;
796 	}
797 
798 	pCryptoService->generic_service_info.state =
799 	    SAL_SERVICE_STATE_INITIALIZED;
800 
801 	return status;
802 }
803 
804 CpaStatus
805 SalCtrl_CryptoStart(icp_accel_dev_t *device, sal_service_t *service)
806 {
807 	sal_crypto_service_t *pCryptoService = (sal_crypto_service_t *)service;
808 	CpaStatus status = CPA_STATUS_SUCCESS;
809 
810 	if (pCryptoService->generic_service_info.state !=
811 	    SAL_SERVICE_STATE_INITIALIZED) {
812 		LAC_LOG_ERROR("Not in the correct state to call start\n");
813 		return CPA_STATUS_FAIL;
814 	}
815 
816 	pCryptoService->generic_service_info.state = SAL_SERVICE_STATE_RUNNING;
817 	return status;
818 }
819 
820 CpaStatus
821 SalCtrl_CryptoStop(icp_accel_dev_t *device, sal_service_t *service)
822 {
823 	sal_crypto_service_t *pCryptoService = (sal_crypto_service_t *)service;
824 
825 	if (SAL_SERVICE_STATE_RUNNING !=
826 	    pCryptoService->generic_service_info.state) {
827 		LAC_LOG_ERROR("Not in the correct state to call stop");
828 	}
829 
830 	pCryptoService->generic_service_info.state =
831 	    SAL_SERVICE_STATE_SHUTTING_DOWN;
832 	return CPA_STATUS_SUCCESS;
833 }
834 
835 CpaStatus
836 SalCtrl_CryptoShutdown(icp_accel_dev_t *device, sal_service_t *service)
837 {
838 	sal_crypto_service_t *pCryptoService = (sal_crypto_service_t *)service;
839 	CpaStatus status = CPA_STATUS_SUCCESS;
840 	sal_service_type_t svc_type = service->type;
841 
842 	if ((SAL_SERVICE_STATE_INITIALIZED !=
843 	     pCryptoService->generic_service_info.state) &&
844 	    (SAL_SERVICE_STATE_SHUTTING_DOWN !=
845 	     pCryptoService->generic_service_info.state)) {
846 		LAC_LOG_ERROR("Not in the correct state to call shutdown \n");
847 		return CPA_STATUS_FAIL;
848 	}
849 
850 
851 	/* Free memory and transhandles */
852 	switch (svc_type) {
853 	case SAL_SERVICE_TYPE_CRYPTO_ASYM:
854 		break;
855 	case SAL_SERVICE_TYPE_CRYPTO_SYM:
856 		if (SalCtrl_SymFreeResources(pCryptoService)) {
857 			status = CPA_STATUS_FAIL;
858 		}
859 		break;
860 	case SAL_SERVICE_TYPE_CRYPTO:
861 		if (SalCtrl_SymFreeResources(pCryptoService)) {
862 			status = CPA_STATUS_FAIL;
863 		}
864 		break;
865 	default:
866 		LAC_LOG_ERROR("Invalid service type\n");
867 		status = CPA_STATUS_FAIL;
868 		break;
869 	}
870 
871 	SalCtrl_DebugShutdown(device, service);
872 
873 	pCryptoService->generic_service_info.state = SAL_SERVICE_STATE_SHUTDOWN;
874 
875 	return status;
876 }
877 
878 /**
879  ******************************************************************************
880  * @ingroup cpaCyCommon
881  *****************************************************************************/
882 CpaStatus
883 cpaCyGetStatusText(const CpaInstanceHandle instanceHandle,
884 		   CpaStatus errStatus,
885 		   Cpa8S *pStatusText)
886 {
887 	CpaStatus status = CPA_STATUS_SUCCESS;
888 
889 
890 	LAC_CHECK_NULL_PARAM(pStatusText);
891 
892 	switch (errStatus) {
893 	case CPA_STATUS_SUCCESS:
894 		LAC_COPY_STRING(pStatusText, CPA_STATUS_STR_SUCCESS);
895 		break;
896 	case CPA_STATUS_FAIL:
897 		LAC_COPY_STRING(pStatusText, CPA_STATUS_STR_FAIL);
898 		break;
899 	case CPA_STATUS_RETRY:
900 		LAC_COPY_STRING(pStatusText, CPA_STATUS_STR_RETRY);
901 		break;
902 	case CPA_STATUS_RESOURCE:
903 		LAC_COPY_STRING(pStatusText, CPA_STATUS_STR_RESOURCE);
904 		break;
905 	case CPA_STATUS_INVALID_PARAM:
906 		LAC_COPY_STRING(pStatusText, CPA_STATUS_STR_INVALID_PARAM);
907 		break;
908 	case CPA_STATUS_FATAL:
909 		LAC_COPY_STRING(pStatusText, CPA_STATUS_STR_FATAL);
910 		break;
911 	case CPA_STATUS_UNSUPPORTED:
912 		LAC_COPY_STRING(pStatusText, CPA_STATUS_STR_UNSUPPORTED);
913 		break;
914 	default:
915 		status = CPA_STATUS_INVALID_PARAM;
916 		break;
917 	}
918 	return status;
919 }
920 
921 void
922 SalCtrl_CyQueryCapabilities(sal_service_t *pGenericService,
923 			    CpaCyCapabilitiesInfo *pCapInfo)
924 {
925 	memset(pCapInfo, 0, sizeof(CpaCyCapabilitiesInfo));
926 
927 	if (SAL_SERVICE_TYPE_CRYPTO == pGenericService->type ||
928 	    SAL_SERVICE_TYPE_CRYPTO_SYM == pGenericService->type) {
929 		pCapInfo->symSupported = CPA_TRUE;
930 		if (pGenericService->capabilitiesMask &
931 		    ICP_ACCEL_CAPABILITIES_EXT_ALGCHAIN) {
932 			pCapInfo->extAlgchainSupported = CPA_TRUE;
933 		}
934 
935 		if (pGenericService->capabilitiesMask &
936 		    ICP_ACCEL_CAPABILITIES_HKDF) {
937 			pCapInfo->hkdfSupported = CPA_TRUE;
938 		}
939 	}
940 
941 	if (pGenericService->capabilitiesMask &
942 	    ICP_ACCEL_CAPABILITIES_ECEDMONT) {
943 		pCapInfo->ecEdMontSupported = CPA_TRUE;
944 	}
945 
946 	if (pGenericService->capabilitiesMask &
947 	    ICP_ACCEL_CAPABILITIES_RANDOM_NUMBER) {
948 		pCapInfo->nrbgSupported = CPA_TRUE;
949 	}
950 
951 	pCapInfo->drbgSupported = CPA_FALSE;
952 	pCapInfo->randSupported = CPA_FALSE;
953 	pCapInfo->nrbgSupported = CPA_FALSE;
954 }
955 
956 /**
957  ******************************************************************************
958  * @ingroup cpaCyCommon
959  *****************************************************************************/
960 CpaStatus
961 cpaCyStartInstance(CpaInstanceHandle instanceHandle_in)
962 {
963 	CpaInstanceHandle instanceHandle = NULL;
964 /* Structure initializer is supported by C99, but it is
965  * not supported by some former Intel compilers.
966  */
967 	CpaInstanceInfo2 info = { 0 };
968 	icp_accel_dev_t *dev = NULL;
969 	CpaStatus status = CPA_STATUS_SUCCESS;
970 	sal_crypto_service_t *pService = NULL;
971 
972 
973 	if (CPA_INSTANCE_HANDLE_SINGLE == instanceHandle_in) {
974 		instanceHandle = Lac_GetFirstHandle(SAL_SERVICE_TYPE_CRYPTO);
975 		if (!instanceHandle) {
976 			instanceHandle =
977 			    Lac_GetFirstHandle(SAL_SERVICE_TYPE_CRYPTO_SYM);
978 		}
979 	} else {
980 		instanceHandle = instanceHandle_in;
981 	}
982 	LAC_CHECK_NULL_PARAM(instanceHandle);
983 	SAL_CHECK_INSTANCE_TYPE(instanceHandle,
984 				(SAL_SERVICE_TYPE_CRYPTO |
985 				 SAL_SERVICE_TYPE_CRYPTO_ASYM |
986 				 SAL_SERVICE_TYPE_CRYPTO_SYM));
987 
988 	pService = (sal_crypto_service_t *)instanceHandle;
989 
990 	status = cpaCyInstanceGetInfo2(instanceHandle, &info);
991 	if (CPA_STATUS_SUCCESS != status) {
992 		LAC_LOG_ERROR("Can not get instance info\n");
993 		return status;
994 	}
995 	dev = icp_adf_getAccelDevByAccelId(info.physInstId.packageId);
996 	if (NULL == dev) {
997 		LAC_LOG_ERROR("Can not find device for the instance\n");
998 		return CPA_STATUS_FAIL;
999 	}
1000 
1001 	pService->generic_service_info.isInstanceStarted = CPA_TRUE;
1002 
1003 	/* Increment dev ref counter */
1004 	icp_qa_dev_get(dev);
1005 	return CPA_STATUS_SUCCESS;
1006 }
1007 
1008 /**
1009  ******************************************************************************
1010  * @ingroup cpaCyCommon
1011  *****************************************************************************/
1012 CpaStatus
1013 cpaCyStopInstance(CpaInstanceHandle instanceHandle_in)
1014 {
1015 	CpaInstanceHandle instanceHandle = NULL;
1016 /* Structure initializer is supported by C99, but it is
1017  * not supported by some former Intel compilers.
1018  */
1019 	CpaInstanceInfo2 info = { 0 };
1020 	icp_accel_dev_t *dev = NULL;
1021 	CpaStatus status = CPA_STATUS_SUCCESS;
1022 	sal_crypto_service_t *pService = NULL;
1023 
1024 
1025 	if (CPA_INSTANCE_HANDLE_SINGLE == instanceHandle_in) {
1026 		instanceHandle = Lac_CryptoGetFirstHandle();
1027 	} else {
1028 		instanceHandle = instanceHandle_in;
1029 	}
1030 	LAC_CHECK_NULL_PARAM(instanceHandle);
1031 	SAL_CHECK_INSTANCE_TYPE(instanceHandle,
1032 				(SAL_SERVICE_TYPE_CRYPTO |
1033 				 SAL_SERVICE_TYPE_CRYPTO_ASYM |
1034 				 SAL_SERVICE_TYPE_CRYPTO_SYM));
1035 
1036 	status = cpaCyInstanceGetInfo2(instanceHandle, &info);
1037 	if (CPA_STATUS_SUCCESS != status) {
1038 		LAC_LOG_ERROR("Can not get instance info\n");
1039 		return status;
1040 	}
1041 	dev = icp_adf_getAccelDevByAccelId(info.physInstId.packageId);
1042 	if (NULL == dev) {
1043 		LAC_LOG_ERROR("Can not find device for the instance\n");
1044 		return CPA_STATUS_FAIL;
1045 	}
1046 
1047 	pService = (sal_crypto_service_t *)instanceHandle;
1048 
1049 	pService->generic_service_info.isInstanceStarted = CPA_FALSE;
1050 
1051 	/* Decrement dev ref counter */
1052 	icp_qa_dev_put(dev);
1053 	return CPA_STATUS_SUCCESS;
1054 }
1055 
1056 /**
1057  ******************************************************************************
1058  * @ingroup cpaCyCommon
1059  *****************************************************************************/
1060 CpaStatus
1061 cpaCyInstanceSetNotificationCb(
1062     const CpaInstanceHandle instanceHandle,
1063     const CpaCyInstanceNotificationCbFunc pInstanceNotificationCb,
1064     void *pCallbackTag)
1065 {
1066 	CpaStatus status = CPA_STATUS_SUCCESS;
1067 	sal_service_t *gen_handle = instanceHandle;
1068 
1069 
1070 	LAC_CHECK_NULL_PARAM(gen_handle);
1071 	gen_handle->notification_cb = pInstanceNotificationCb;
1072 	gen_handle->cb_tag = pCallbackTag;
1073 	return status;
1074 }
1075 
1076 /**
1077  ******************************************************************************
1078  * @ingroup cpaCyCommon
1079  *****************************************************************************/
1080 CpaStatus
1081 cpaCyGetNumInstances(Cpa16U *pNumInstances)
1082 {
1083 	CpaStatus status = CPA_STATUS_SUCCESS;
1084 	CpaInstanceHandle cyInstanceHandle;
1085 	CpaInstanceInfo2 info;
1086 	icp_accel_dev_t **pAdfInsts = NULL;
1087 	icp_accel_dev_t *dev_addr = NULL;
1088 	sal_t *base_addr = NULL;
1089 	sal_list_t *list_temp = NULL;
1090 	Cpa16U num_accel_dev = 0;
1091 	Cpa16U num_inst = 0;
1092 	Cpa16U i = 0;
1093 
1094 	LAC_CHECK_NULL_PARAM(pNumInstances);
1095 
1096 	/* Get the number of accel_dev in the system */
1097 	status = icp_amgr_getNumInstances(&num_accel_dev);
1098 	LAC_CHECK_STATUS(status);
1099 
1100 	/* Allocate memory to store addr of accel_devs */
1101 	pAdfInsts =
1102 	    malloc(num_accel_dev * sizeof(icp_accel_dev_t *), M_QAT, M_WAITOK);
1103 	num_accel_dev = 0;
1104 	/* Get ADF to return all accel_devs that support either
1105 	 * symmetric or asymmetric crypto */
1106 	status = icp_amgr_getAllAccelDevByCapabilities(
1107 	    (ICP_ACCEL_CAPABILITIES_CRYPTO_ASYMMETRIC |
1108 	     ICP_ACCEL_CAPABILITIES_CRYPTO_SYMMETRIC),
1109 	    pAdfInsts,
1110 	    &num_accel_dev);
1111 	if (CPA_STATUS_SUCCESS != status) {
1112 		LAC_LOG_ERROR("No support for crypto\n");
1113 		*pNumInstances = 0;
1114 		free(pAdfInsts, M_QAT);
1115 		return status;
1116 	}
1117 
1118 	for (i = 0; i < num_accel_dev; i++) {
1119 		dev_addr = (icp_accel_dev_t *)pAdfInsts[i];
1120 		if (NULL == dev_addr || NULL == dev_addr->pSalHandle) {
1121 			continue;
1122 		}
1123 
1124 		base_addr = dev_addr->pSalHandle;
1125 		list_temp = base_addr->crypto_services;
1126 		while (NULL != list_temp) {
1127 			cyInstanceHandle = SalList_getObject(list_temp);
1128 			status = cpaCyInstanceGetInfo2(cyInstanceHandle, &info);
1129 			if (CPA_STATUS_SUCCESS == status &&
1130 			    CPA_TRUE == info.isPolled) {
1131 				num_inst++;
1132 			}
1133 			list_temp = SalList_next(list_temp);
1134 		}
1135 		list_temp = base_addr->asym_services;
1136 		while (NULL != list_temp) {
1137 			cyInstanceHandle = SalList_getObject(list_temp);
1138 			status = cpaCyInstanceGetInfo2(cyInstanceHandle, &info);
1139 			if (CPA_STATUS_SUCCESS == status &&
1140 			    CPA_TRUE == info.isPolled) {
1141 				num_inst++;
1142 			}
1143 			list_temp = SalList_next(list_temp);
1144 		}
1145 		list_temp = base_addr->sym_services;
1146 		while (NULL != list_temp) {
1147 			cyInstanceHandle = SalList_getObject(list_temp);
1148 			status = cpaCyInstanceGetInfo2(cyInstanceHandle, &info);
1149 			if (CPA_STATUS_SUCCESS == status &&
1150 			    CPA_TRUE == info.isPolled) {
1151 				num_inst++;
1152 			}
1153 			list_temp = SalList_next(list_temp);
1154 		}
1155 	}
1156 	*pNumInstances = num_inst;
1157 	free(pAdfInsts, M_QAT);
1158 
1159 
1160 	return status;
1161 }
1162 
1163 /**
1164  ******************************************************************************
1165  * @ingroup cpaCyCommon
1166  *****************************************************************************/
1167 CpaStatus
1168 cpaCyGetInstances(Cpa16U numInstances, CpaInstanceHandle *pCyInstances)
1169 {
1170 	CpaStatus status = CPA_STATUS_SUCCESS;
1171 	CpaInstanceHandle cyInstanceHandle;
1172 	CpaInstanceInfo2 info;
1173 	icp_accel_dev_t **pAdfInsts = NULL;
1174 	icp_accel_dev_t *dev_addr = NULL;
1175 	sal_t *base_addr = NULL;
1176 	sal_list_t *list_temp = NULL;
1177 	Cpa16U num_accel_dev = 0;
1178 	Cpa16U num_allocated_instances = 0;
1179 	Cpa16U index = 0;
1180 	Cpa16U i = 0;
1181 
1182 
1183 	LAC_CHECK_NULL_PARAM(pCyInstances);
1184 	if (0 == numInstances) {
1185 		LAC_INVALID_PARAM_LOG("NumInstances is 0");
1186 		return CPA_STATUS_INVALID_PARAM;
1187 	}
1188 
1189 	/* Get the number of crypto instances */
1190 	status = cpaCyGetNumInstances(&num_allocated_instances);
1191 	if (CPA_STATUS_SUCCESS != status) {
1192 		return status;
1193 	}
1194 
1195 	if (numInstances > num_allocated_instances) {
1196 		QAT_UTILS_LOG("Only %d crypto instances available\n",
1197 			      num_allocated_instances);
1198 		return CPA_STATUS_RESOURCE;
1199 	}
1200 
1201 	/* Get the number of accel devices in the system */
1202 	status = icp_amgr_getNumInstances(&num_accel_dev);
1203 	LAC_CHECK_STATUS(status);
1204 
1205 	/* Allocate memory to store addr of accel_devs */
1206 	pAdfInsts =
1207 	    malloc(num_accel_dev * sizeof(icp_accel_dev_t *), M_QAT, M_WAITOK);
1208 
1209 	num_accel_dev = 0;
1210 	/* Get ADF to return all accel_devs that support either
1211 	 * symmetric or asymmetric crypto */
1212 	status = icp_amgr_getAllAccelDevByCapabilities(
1213 	    (ICP_ACCEL_CAPABILITIES_CRYPTO_ASYMMETRIC |
1214 	     ICP_ACCEL_CAPABILITIES_CRYPTO_SYMMETRIC),
1215 	    pAdfInsts,
1216 	    &num_accel_dev);
1217 	if (CPA_STATUS_SUCCESS != status) {
1218 		LAC_LOG_ERROR("No support for crypto\n");
1219 		free(pAdfInsts, M_QAT);
1220 		return status;
1221 	}
1222 
1223 	for (i = 0; i < num_accel_dev; i++) {
1224 		dev_addr = (icp_accel_dev_t *)pAdfInsts[i];
1225 		/* Note dev_addr cannot be NULL here as numInstances = 0
1226 		 * is not valid and if dev_addr = NULL then index = 0 (which
1227 		 * is less than numInstances and status is set to _RESOURCE
1228 		 * above
1229 		 */
1230 		base_addr = dev_addr->pSalHandle;
1231 		if (NULL == base_addr) {
1232 			continue;
1233 		}
1234 		list_temp = base_addr->crypto_services;
1235 		while (NULL != list_temp) {
1236 			if (index > (numInstances - 1)) {
1237 				break;
1238 			}
1239 			cyInstanceHandle = SalList_getObject(list_temp);
1240 			status = cpaCyInstanceGetInfo2(cyInstanceHandle, &info);
1241 			list_temp = SalList_next(list_temp);
1242 			if (CPA_STATUS_SUCCESS != status ||
1243 			    CPA_TRUE != info.isPolled) {
1244 				continue;
1245 			}
1246 			pCyInstances[index] = cyInstanceHandle;
1247 			index++;
1248 		}
1249 		list_temp = base_addr->asym_services;
1250 		while (NULL != list_temp) {
1251 			if (index > (numInstances - 1)) {
1252 				break;
1253 			}
1254 			cyInstanceHandle = SalList_getObject(list_temp);
1255 			status = cpaCyInstanceGetInfo2(cyInstanceHandle, &info);
1256 			list_temp = SalList_next(list_temp);
1257 			if (CPA_STATUS_SUCCESS != status ||
1258 			    CPA_TRUE != info.isPolled) {
1259 				continue;
1260 			}
1261 			pCyInstances[index] = cyInstanceHandle;
1262 			index++;
1263 		}
1264 		list_temp = base_addr->sym_services;
1265 		while (NULL != list_temp) {
1266 			if (index > (numInstances - 1)) {
1267 				break;
1268 			}
1269 			cyInstanceHandle = SalList_getObject(list_temp);
1270 			status = cpaCyInstanceGetInfo2(cyInstanceHandle, &info);
1271 			list_temp = SalList_next(list_temp);
1272 			if (CPA_STATUS_SUCCESS != status ||
1273 			    CPA_TRUE != info.isPolled) {
1274 				continue;
1275 			}
1276 			pCyInstances[index] = cyInstanceHandle;
1277 			index++;
1278 		}
1279 	}
1280 	free(pAdfInsts, M_QAT);
1281 
1282 	return status;
1283 }
1284 
1285 /**
1286  ******************************************************************************
1287  * @ingroup cpaCyCommon
1288  *****************************************************************************/
1289 CpaStatus
1290 cpaCyInstanceGetInfo(const CpaInstanceHandle instanceHandle_in,
1291 		     struct _CpaInstanceInfo *pInstanceInfo)
1292 {
1293 	CpaInstanceHandle instanceHandle = NULL;
1294 	sal_crypto_service_t *pCryptoService = NULL;
1295 	sal_service_t *pGenericService = NULL;
1296 
1297 	Cpa8U name[CPA_INST_NAME_SIZE] =
1298 	    "Intel(R) DH89XXCC instance number: %02x, type: Crypto";
1299 
1300 	if (CPA_INSTANCE_HANDLE_SINGLE == instanceHandle_in) {
1301 		instanceHandle = Lac_CryptoGetFirstHandle();
1302 	} else {
1303 		instanceHandle = instanceHandle_in;
1304 	}
1305 
1306 	LAC_CHECK_NULL_PARAM(instanceHandle);
1307 	LAC_CHECK_NULL_PARAM(pInstanceInfo);
1308 	SAL_CHECK_INSTANCE_TYPE(instanceHandle,
1309 				(SAL_SERVICE_TYPE_CRYPTO |
1310 				 SAL_SERVICE_TYPE_CRYPTO_ASYM |
1311 				 SAL_SERVICE_TYPE_CRYPTO_SYM));
1312 
1313 	pCryptoService = (sal_crypto_service_t *)instanceHandle;
1314 
1315 	pInstanceInfo->type = CPA_INSTANCE_TYPE_CRYPTO;
1316 
1317 	/* According to cpa.h instance state is initialized and ready for use
1318 	 * or shutdown. Therefore need to map our running state to initialised
1319 	 * or shutdown */
1320 	if (SAL_SERVICE_STATE_RUNNING ==
1321 	    pCryptoService->generic_service_info.state) {
1322 		pInstanceInfo->state = CPA_INSTANCE_STATE_INITIALISED;
1323 	} else {
1324 		pInstanceInfo->state = CPA_INSTANCE_STATE_SHUTDOWN;
1325 	}
1326 
1327 	pGenericService = (sal_service_t *)instanceHandle;
1328 	snprintf((char *)pInstanceInfo->name,
1329 		 CPA_INST_NAME_SIZE,
1330 		 (char *)name,
1331 		 pGenericService->instance);
1332 
1333 	pInstanceInfo->name[CPA_INST_NAME_SIZE - 1] = '\0';
1334 
1335 	snprintf((char *)pInstanceInfo->version,
1336 		 CPA_INSTANCE_MAX_NAME_SIZE_IN_BYTES,
1337 		 "%d.%d",
1338 		 CPA_CY_API_VERSION_NUM_MAJOR,
1339 		 CPA_CY_API_VERSION_NUM_MINOR);
1340 
1341 	pInstanceInfo->version[CPA_INSTANCE_MAX_VERSION_SIZE_IN_BYTES - 1] =
1342 	    '\0';
1343 	return CPA_STATUS_SUCCESS;
1344 }
1345 
1346 /**
1347  ******************************************************************************
1348  * @ingroup cpaCyCommon
1349  *****************************************************************************/
1350 CpaStatus
1351 cpaCyInstanceGetInfo2(const CpaInstanceHandle instanceHandle_in,
1352 		      CpaInstanceInfo2 *pInstanceInfo2)
1353 {
1354 	CpaInstanceHandle instanceHandle = NULL;
1355 	sal_crypto_service_t *pCryptoService = NULL;
1356 	icp_accel_dev_t *dev = NULL;
1357 	CpaStatus status = CPA_STATUS_SUCCESS;
1358 	char keyStr[ADF_CFG_MAX_KEY_LEN_IN_BYTES] = { 0 };
1359 	char valStr[ADF_CFG_MAX_VAL_LEN_IN_BYTES] = { 0 };
1360 	char *section = DYN_SEC;
1361 
1362 
1363 	if (CPA_INSTANCE_HANDLE_SINGLE == instanceHandle_in) {
1364 		instanceHandle = Lac_CryptoGetFirstHandle();
1365 	} else {
1366 		instanceHandle = instanceHandle_in;
1367 	}
1368 
1369 	LAC_CHECK_NULL_PARAM(instanceHandle);
1370 	LAC_CHECK_NULL_PARAM(pInstanceInfo2);
1371 	SAL_CHECK_INSTANCE_TYPE(instanceHandle,
1372 				(SAL_SERVICE_TYPE_CRYPTO |
1373 				 SAL_SERVICE_TYPE_CRYPTO_ASYM |
1374 				 SAL_SERVICE_TYPE_CRYPTO_SYM));
1375 
1376 	LAC_OS_BZERO(pInstanceInfo2, sizeof(CpaInstanceInfo2));
1377 	pInstanceInfo2->accelerationServiceType = CPA_ACC_SVC_TYPE_CRYPTO;
1378 	snprintf((char *)pInstanceInfo2->vendorName,
1379 		 CPA_INST_VENDOR_NAME_SIZE,
1380 		 "%s",
1381 		 SAL_INFO2_VENDOR_NAME);
1382 	pInstanceInfo2->vendorName[CPA_INST_VENDOR_NAME_SIZE - 1] = '\0';
1383 
1384 	snprintf((char *)pInstanceInfo2->swVersion,
1385 		 CPA_INST_SW_VERSION_SIZE,
1386 		 "Version %d.%d",
1387 		 SAL_INFO2_DRIVER_SW_VERSION_MAJ_NUMBER,
1388 		 SAL_INFO2_DRIVER_SW_VERSION_MIN_NUMBER);
1389 	pInstanceInfo2->swVersion[CPA_INST_SW_VERSION_SIZE - 1] = '\0';
1390 
1391 	/* Note we can safely read the contents of the crypto service instance
1392 	   here because icp_amgr_getAllAccelDevByCapabilities() only returns
1393 	   devs
1394 	   that have started */
1395 	pCryptoService = (sal_crypto_service_t *)instanceHandle;
1396 	pInstanceInfo2->physInstId.packageId = pCryptoService->pkgID;
1397 	pInstanceInfo2->physInstId.acceleratorId =
1398 	    pCryptoService->acceleratorNum;
1399 	pInstanceInfo2->physInstId.executionEngineId =
1400 	    pCryptoService->executionEngine;
1401 	pInstanceInfo2->physInstId.busAddress =
1402 	    icp_adf_get_busAddress(pInstanceInfo2->physInstId.packageId);
1403 
1404 	/*set coreAffinity to zero before use */
1405 	LAC_OS_BZERO(pInstanceInfo2->coreAffinity,
1406 		     sizeof(pInstanceInfo2->coreAffinity));
1407 	CPA_BITMAP_BIT_SET(pInstanceInfo2->coreAffinity,
1408 			   pCryptoService->coreAffinity);
1409 	pInstanceInfo2->nodeAffinity = pCryptoService->nodeAffinity;
1410 
1411 	if (SAL_SERVICE_STATE_RUNNING ==
1412 	    pCryptoService->generic_service_info.state) {
1413 		pInstanceInfo2->operState = CPA_OPER_STATE_UP;
1414 	} else {
1415 		pInstanceInfo2->operState = CPA_OPER_STATE_DOWN;
1416 	}
1417 
1418 	pInstanceInfo2->requiresPhysicallyContiguousMemory = CPA_TRUE;
1419 	if (SAL_RESP_POLL_CFG_FILE == pCryptoService->isPolled) {
1420 		pInstanceInfo2->isPolled = CPA_TRUE;
1421 	} else {
1422 		pInstanceInfo2->isPolled = CPA_FALSE;
1423 	}
1424 	pInstanceInfo2->isOffloaded = CPA_TRUE;
1425 
1426 	/* Get the instance name and part name*/
1427 	dev = icp_adf_getAccelDevByAccelId(pCryptoService->pkgID);
1428 	if (NULL == dev) {
1429 		LAC_LOG_ERROR("Can not find device for the instance\n");
1430 		LAC_OS_BZERO(pInstanceInfo2, sizeof(CpaInstanceInfo2));
1431 		return CPA_STATUS_FAIL;
1432 	}
1433 	snprintf((char *)pInstanceInfo2->partName,
1434 		 CPA_INST_PART_NAME_SIZE,
1435 		 SAL_INFO2_PART_NAME,
1436 		 dev->deviceName);
1437 	pInstanceInfo2->partName[CPA_INST_PART_NAME_SIZE - 1] = '\0';
1438 
1439 	status =
1440 	    Sal_StringParsing("Cy",
1441 			      pCryptoService->generic_service_info.instance,
1442 			      "Name",
1443 			      keyStr);
1444 	LAC_CHECK_STATUS(status);
1445 
1446 	if (CPA_FALSE == pCryptoService->generic_service_info.is_dyn) {
1447 		section = icpGetProcessName();
1448 	}
1449 
1450 	status = icp_adf_cfgGetParamValue(dev, section, keyStr, valStr);
1451 	LAC_CHECK_STATUS(status);
1452 
1453 	snprintf((char *)pInstanceInfo2->instName,
1454 		 CPA_INST_NAME_SIZE,
1455 		 "%s",
1456 		 valStr);
1457 	snprintf((char *)pInstanceInfo2->instID,
1458 		 CPA_INST_ID_SIZE,
1459 		 "%s_%s",
1460 		 section,
1461 		 valStr);
1462 	return CPA_STATUS_SUCCESS;
1463 }
1464 
1465 /**
1466  ******************************************************************************
1467  * @ingroup cpaCyCommon
1468  *****************************************************************************/
1469 
1470 CpaStatus
1471 cpaCyQueryCapabilities(const CpaInstanceHandle instanceHandle_in,
1472 		       CpaCyCapabilitiesInfo *pCapInfo)
1473 {
1474 	/* Verify Instance exists */
1475 	CpaInstanceHandle instanceHandle = NULL;
1476 
1477 
1478 	if (CPA_INSTANCE_HANDLE_SINGLE == instanceHandle_in) {
1479 		instanceHandle = Lac_CryptoGetFirstHandle();
1480 	} else {
1481 		instanceHandle = instanceHandle_in;
1482 	}
1483 
1484 	LAC_CHECK_NULL_PARAM(instanceHandle);
1485 	SAL_CHECK_INSTANCE_TYPE(instanceHandle,
1486 				(SAL_SERVICE_TYPE_CRYPTO |
1487 				 SAL_SERVICE_TYPE_CRYPTO_ASYM |
1488 				 SAL_SERVICE_TYPE_CRYPTO_SYM));
1489 	LAC_CHECK_NULL_PARAM(pCapInfo);
1490 
1491 	SalCtrl_CyQueryCapabilities((sal_service_t *)instanceHandle, pCapInfo);
1492 
1493 	return CPA_STATUS_SUCCESS;
1494 }
1495 
1496 /**
1497  ******************************************************************************
1498  * @ingroup cpaCySym
1499  *****************************************************************************/
1500 CpaStatus
1501 cpaCySymQueryCapabilities(const CpaInstanceHandle instanceHandle_in,
1502 			  CpaCySymCapabilitiesInfo *pCapInfo)
1503 {
1504 	sal_crypto_service_t *pCryptoService = NULL;
1505 	sal_service_t *pGenericService = NULL;
1506 	CpaInstanceHandle instanceHandle = NULL;
1507 
1508 	/* Verify Instance exists */
1509 	if (CPA_INSTANCE_HANDLE_SINGLE == instanceHandle_in) {
1510 		instanceHandle = Lac_GetFirstHandle(SAL_SERVICE_TYPE_CRYPTO);
1511 		if (!instanceHandle) {
1512 			instanceHandle =
1513 			    Lac_GetFirstHandle(SAL_SERVICE_TYPE_CRYPTO_SYM);
1514 		}
1515 	} else {
1516 		instanceHandle = instanceHandle_in;
1517 	}
1518 
1519 	LAC_CHECK_NULL_PARAM(instanceHandle);
1520 	SAL_CHECK_INSTANCE_TYPE(instanceHandle,
1521 				(SAL_SERVICE_TYPE_CRYPTO |
1522 				 SAL_SERVICE_TYPE_CRYPTO_ASYM |
1523 				 SAL_SERVICE_TYPE_CRYPTO_SYM));
1524 	LAC_CHECK_NULL_PARAM(pCapInfo);
1525 
1526 	pCryptoService = (sal_crypto_service_t *)instanceHandle;
1527 	pGenericService = &(pCryptoService->generic_service_info);
1528 
1529 	memset(pCapInfo, '\0', sizeof(CpaCySymCapabilitiesInfo));
1530 	/* An asym crypto instance does not support sym service */
1531 	if (SAL_SERVICE_TYPE_CRYPTO_ASYM == pGenericService->type) {
1532 		return CPA_STATUS_SUCCESS;
1533 	}
1534 
1535 	CPA_BITMAP_BIT_SET(pCapInfo->ciphers, CPA_CY_SYM_CIPHER_NULL);
1536 	CPA_BITMAP_BIT_SET(pCapInfo->ciphers, CPA_CY_SYM_CIPHER_AES_ECB);
1537 	CPA_BITMAP_BIT_SET(pCapInfo->ciphers, CPA_CY_SYM_CIPHER_AES_CBC);
1538 	CPA_BITMAP_BIT_SET(pCapInfo->ciphers, CPA_CY_SYM_CIPHER_AES_CTR);
1539 	CPA_BITMAP_BIT_SET(pCapInfo->ciphers, CPA_CY_SYM_CIPHER_AES_CCM);
1540 	CPA_BITMAP_BIT_SET(pCapInfo->ciphers, CPA_CY_SYM_CIPHER_AES_GCM);
1541 	CPA_BITMAP_BIT_SET(pCapInfo->ciphers, CPA_CY_SYM_CIPHER_AES_XTS);
1542 	if (isCyGen2x(pCryptoService)) {
1543 		CPA_BITMAP_BIT_SET(pCapInfo->ciphers, CPA_CY_SYM_CIPHER_ARC4);
1544 		CPA_BITMAP_BIT_SET(pCapInfo->ciphers,
1545 				   CPA_CY_SYM_CIPHER_DES_ECB);
1546 		CPA_BITMAP_BIT_SET(pCapInfo->ciphers,
1547 				   CPA_CY_SYM_CIPHER_DES_CBC);
1548 		CPA_BITMAP_BIT_SET(pCapInfo->ciphers,
1549 				   CPA_CY_SYM_CIPHER_3DES_ECB);
1550 		CPA_BITMAP_BIT_SET(pCapInfo->ciphers,
1551 				   CPA_CY_SYM_CIPHER_3DES_CBC);
1552 		CPA_BITMAP_BIT_SET(pCapInfo->ciphers,
1553 				   CPA_CY_SYM_CIPHER_3DES_CTR);
1554 		CPA_BITMAP_BIT_SET(pCapInfo->ciphers,
1555 				   CPA_CY_SYM_CIPHER_KASUMI_F8);
1556 		CPA_BITMAP_BIT_SET(pCapInfo->ciphers,
1557 				   CPA_CY_SYM_CIPHER_SNOW3G_UEA2);
1558 		CPA_BITMAP_BIT_SET(pCapInfo->ciphers, CPA_CY_SYM_CIPHER_AES_F8);
1559 	}
1560 
1561 	CPA_BITMAP_BIT_SET(pCapInfo->hashes, CPA_CY_SYM_HASH_SHA1);
1562 	CPA_BITMAP_BIT_SET(pCapInfo->hashes, CPA_CY_SYM_HASH_SHA224);
1563 	CPA_BITMAP_BIT_SET(pCapInfo->hashes, CPA_CY_SYM_HASH_SHA256);
1564 	CPA_BITMAP_BIT_SET(pCapInfo->hashes, CPA_CY_SYM_HASH_SHA384);
1565 	CPA_BITMAP_BIT_SET(pCapInfo->hashes, CPA_CY_SYM_HASH_SHA512);
1566 	CPA_BITMAP_BIT_SET(pCapInfo->hashes, CPA_CY_SYM_HASH_AES_XCBC);
1567 	CPA_BITMAP_BIT_SET(pCapInfo->hashes, CPA_CY_SYM_HASH_AES_CCM);
1568 	CPA_BITMAP_BIT_SET(pCapInfo->hashes, CPA_CY_SYM_HASH_AES_GCM);
1569 	CPA_BITMAP_BIT_SET(pCapInfo->hashes, CPA_CY_SYM_HASH_AES_CMAC);
1570 	CPA_BITMAP_BIT_SET(pCapInfo->hashes, CPA_CY_SYM_HASH_AES_GMAC);
1571 	CPA_BITMAP_BIT_SET(pCapInfo->hashes, CPA_CY_SYM_HASH_AES_CBC_MAC);
1572 	if (isCyGen2x(pCryptoService)) {
1573 		CPA_BITMAP_BIT_SET(pCapInfo->hashes, CPA_CY_SYM_HASH_MD5);
1574 		CPA_BITMAP_BIT_SET(pCapInfo->hashes, CPA_CY_SYM_HASH_KASUMI_F9);
1575 		CPA_BITMAP_BIT_SET(pCapInfo->hashes,
1576 				   CPA_CY_SYM_HASH_SNOW3G_UIA2);
1577 	}
1578 
1579 	if (pGenericService->capabilitiesMask &
1580 	    ICP_ACCEL_CAPABILITIES_CRYPTO_ZUC) {
1581 		CPA_BITMAP_BIT_SET(pCapInfo->ciphers,
1582 				   CPA_CY_SYM_CIPHER_ZUC_EEA3);
1583 		CPA_BITMAP_BIT_SET(pCapInfo->hashes, CPA_CY_SYM_HASH_ZUC_EIA3);
1584 	}
1585 
1586 	if (pGenericService->capabilitiesMask &
1587 	    ICP_ACCEL_CAPABILITIES_CHACHA_POLY) {
1588 		CPA_BITMAP_BIT_SET(pCapInfo->hashes, CPA_CY_SYM_HASH_POLY);
1589 		CPA_BITMAP_BIT_SET(pCapInfo->ciphers, CPA_CY_SYM_CIPHER_CHACHA);
1590 	}
1591 
1592 	if (pGenericService->capabilitiesMask & ICP_ACCEL_CAPABILITIES_SM3) {
1593 		CPA_BITMAP_BIT_SET(pCapInfo->hashes, CPA_CY_SYM_HASH_SM3);
1594 	}
1595 
1596 	pCapInfo->partialPacketSupported = CPA_TRUE;
1597 
1598 	if (pGenericService->capabilitiesMask & ICP_ACCEL_CAPABILITIES_SHA3) {
1599 		CPA_BITMAP_BIT_SET(pCapInfo->hashes, CPA_CY_SYM_HASH_SHA3_256);
1600 		pCapInfo->partialPacketSupported = CPA_FALSE;
1601 	}
1602 
1603 	if (pGenericService->capabilitiesMask &
1604 	    ICP_ACCEL_CAPABILITIES_SHA3_EXT) {
1605 		CPA_BITMAP_BIT_SET(pCapInfo->hashes, CPA_CY_SYM_HASH_SHA3_224);
1606 		CPA_BITMAP_BIT_SET(pCapInfo->hashes, CPA_CY_SYM_HASH_SHA3_256);
1607 		CPA_BITMAP_BIT_SET(pCapInfo->hashes, CPA_CY_SYM_HASH_SHA3_384);
1608 		CPA_BITMAP_BIT_SET(pCapInfo->hashes, CPA_CY_SYM_HASH_SHA3_512);
1609 		pCapInfo->partialPacketSupported = CPA_FALSE;
1610 	}
1611 
1612 	if (pGenericService->capabilitiesMask & ICP_ACCEL_CAPABILITIES_SM4) {
1613 		CPA_BITMAP_BIT_SET(pCapInfo->ciphers,
1614 				   CPA_CY_SYM_CIPHER_SM4_ECB);
1615 		CPA_BITMAP_BIT_SET(pCapInfo->ciphers,
1616 				   CPA_CY_SYM_CIPHER_SM4_CBC);
1617 		CPA_BITMAP_BIT_SET(pCapInfo->ciphers,
1618 				   CPA_CY_SYM_CIPHER_SM4_CTR);
1619 		pCapInfo->partialPacketSupported = CPA_FALSE;
1620 	}
1621 
1622 	return CPA_STATUS_SUCCESS;
1623 }
1624 
1625 /**
1626  ******************************************************************************
1627  * @ingroup cpaCyCommon
1628  *****************************************************************************/
1629 CpaStatus
1630 cpaCySetAddressTranslation(const CpaInstanceHandle instanceHandle_in,
1631 			   CpaVirtualToPhysical virtual2physical)
1632 {
1633 
1634 	CpaInstanceHandle instanceHandle = NULL;
1635 	sal_service_t *pService = NULL;
1636 
1637 
1638 	if (CPA_INSTANCE_HANDLE_SINGLE == instanceHandle_in) {
1639 		instanceHandle = Lac_CryptoGetFirstHandle();
1640 	} else {
1641 		instanceHandle = instanceHandle_in;
1642 	}
1643 
1644 	LAC_CHECK_NULL_PARAM(instanceHandle);
1645 	SAL_CHECK_INSTANCE_TYPE(instanceHandle,
1646 				(SAL_SERVICE_TYPE_CRYPTO |
1647 				 SAL_SERVICE_TYPE_CRYPTO_ASYM |
1648 				 SAL_SERVICE_TYPE_CRYPTO_SYM));
1649 	LAC_CHECK_NULL_PARAM(virtual2physical);
1650 
1651 	pService = (sal_service_t *)instanceHandle;
1652 
1653 	pService->virt2PhysClient = virtual2physical;
1654 
1655 	return CPA_STATUS_SUCCESS;
1656 }
1657 
1658 /**
1659  ******************************************************************************
1660  * @ingroup cpaCyCommon
1661  * Crypto specific polling function which polls a crypto instance.
1662  *****************************************************************************/
1663 CpaStatus
1664 icp_sal_CyPollInstance(CpaInstanceHandle instanceHandle_in,
1665 		       Cpa32U response_quota)
1666 {
1667 	CpaStatus status = CPA_STATUS_SUCCESS;
1668 	sal_crypto_service_t *crypto_handle = NULL;
1669 	sal_service_t *gen_handle = NULL;
1670 	icp_comms_trans_handle trans_hndTable[MAX_CY_RX_RINGS] = { 0 };
1671 	Cpa32U num_rx_rings = 0;
1672 
1673 	if (CPA_INSTANCE_HANDLE_SINGLE == instanceHandle_in) {
1674 		crypto_handle =
1675 		    (sal_crypto_service_t *)Lac_CryptoGetFirstHandle();
1676 	} else {
1677 		crypto_handle = (sal_crypto_service_t *)instanceHandle_in;
1678 	}
1679 	LAC_CHECK_NULL_PARAM(crypto_handle);
1680 	SAL_RUNNING_CHECK(crypto_handle);
1681 	SAL_CHECK_INSTANCE_TYPE(crypto_handle,
1682 				(SAL_SERVICE_TYPE_CRYPTO |
1683 				 SAL_SERVICE_TYPE_CRYPTO_ASYM |
1684 				 SAL_SERVICE_TYPE_CRYPTO_SYM));
1685 
1686 	gen_handle = &(crypto_handle->generic_service_info);
1687 
1688 	/*
1689 	 * From the instanceHandle we must get the trans_handle and send
1690 	 * down to adf for polling.
1691 	 * Populate our trans handle table with the appropriate handles.
1692 	 */
1693 
1694 	switch (gen_handle->type) {
1695 	case SAL_SERVICE_TYPE_CRYPTO_ASYM:
1696 		trans_hndTable[TH_CY_RX_0] =
1697 		    crypto_handle->trans_handle_asym_rx;
1698 		num_rx_rings = 1;
1699 		break;
1700 	case SAL_SERVICE_TYPE_CRYPTO_SYM:
1701 		trans_hndTable[TH_CY_RX_0] = crypto_handle->trans_handle_sym_rx;
1702 		num_rx_rings = 1;
1703 		break;
1704 	case SAL_SERVICE_TYPE_CRYPTO:
1705 		trans_hndTable[TH_CY_RX_0] = crypto_handle->trans_handle_sym_rx;
1706 		trans_hndTable[TH_CY_RX_1] =
1707 		    crypto_handle->trans_handle_asym_rx;
1708 		num_rx_rings = MAX_CY_RX_RINGS;
1709 		break;
1710 	default:
1711 		break;
1712 	}
1713 
1714 	/* Call adf to do the polling. */
1715 	status =
1716 	    icp_adf_pollInstance(trans_hndTable, num_rx_rings, response_quota);
1717 
1718 	return status;
1719 }
1720 
1721 /**
1722  ******************************************************************************
1723  * @ingroup cpaCyCommon
1724  * Crypto specific polling function which polls sym crypto ring.
1725  *****************************************************************************/
1726 CpaStatus
1727 icp_sal_CyPollSymRing(CpaInstanceHandle instanceHandle_in,
1728 		      Cpa32U response_quota)
1729 {
1730 	CpaStatus status = CPA_STATUS_SUCCESS;
1731 	sal_crypto_service_t *crypto_handle = NULL;
1732 	icp_comms_trans_handle trans_hndTable[NUM_CRYPTO_SYM_RX_RINGS] = { 0 };
1733 
1734 	if (CPA_INSTANCE_HANDLE_SINGLE == instanceHandle_in) {
1735 		crypto_handle = (sal_crypto_service_t *)Lac_GetFirstHandle(
1736 		    SAL_SERVICE_TYPE_CRYPTO_SYM);
1737 	} else {
1738 		crypto_handle = (sal_crypto_service_t *)instanceHandle_in;
1739 	}
1740 	LAC_CHECK_NULL_PARAM(crypto_handle);
1741 	SAL_CHECK_INSTANCE_TYPE(crypto_handle,
1742 				(SAL_SERVICE_TYPE_CRYPTO |
1743 				 SAL_SERVICE_TYPE_CRYPTO_SYM));
1744 	SAL_RUNNING_CHECK(crypto_handle);
1745 
1746 	/*
1747 	 * From the instanceHandle we must get the trans_handle and send
1748 	 * down to adf for polling.
1749 	 * Populate our trans handle table with the appropriate handles.
1750 	 */
1751 	trans_hndTable[TH_SINGLE_RX] = crypto_handle->trans_handle_sym_rx;
1752 	/* Call adf to do the polling. */
1753 	status = icp_adf_pollInstance(trans_hndTable,
1754 				      NUM_CRYPTO_SYM_RX_RINGS,
1755 				      response_quota);
1756 	return status;
1757 }
1758 
1759 
1760 /**
1761  ******************************************************************************
1762  * @ingroup cpaCyCommon
1763  * Crypto specific polling function which polls an nrbg crypto ring.
1764  *****************************************************************************/
1765 CpaStatus
1766 icp_sal_CyPollNRBGRing(CpaInstanceHandle instanceHandle_in,
1767 		       Cpa32U response_quota)
1768 {
1769 	return CPA_STATUS_UNSUPPORTED;
1770 }
1771 
1772 /* Returns the handle to the first asym crypto instance */
1773 static CpaInstanceHandle
1774 Lac_GetFirstAsymHandle(icp_accel_dev_t *adfInsts[ADF_MAX_DEVICES],
1775 		       Cpa16U num_dev)
1776 {
1777 	CpaStatus status = CPA_STATUS_SUCCESS;
1778 	icp_accel_dev_t *dev_addr = NULL;
1779 	sal_t *base_addr = NULL;
1780 	sal_list_t *list_temp = NULL;
1781 	CpaInstanceHandle cyInst = NULL;
1782 	CpaInstanceInfo2 info;
1783 	Cpa16U i = 0;
1784 
1785 	for (i = 0; i < num_dev; i++) {
1786 		dev_addr = (icp_accel_dev_t *)adfInsts[i];
1787 		base_addr = dev_addr->pSalHandle;
1788 		if (NULL == base_addr) {
1789 			continue;
1790 		}
1791 		list_temp = base_addr->asym_services;
1792 		while (NULL != list_temp) {
1793 			cyInst = SalList_getObject(list_temp);
1794 			status = cpaCyInstanceGetInfo2(cyInst, &info);
1795 			list_temp = SalList_next(list_temp);
1796 			if (CPA_STATUS_SUCCESS != status ||
1797 			    CPA_TRUE != info.isPolled) {
1798 				cyInst = NULL;
1799 				continue;
1800 			}
1801 			break;
1802 		}
1803 		if (cyInst) {
1804 			break;
1805 		}
1806 	}
1807 
1808 	return cyInst;
1809 }
1810 
1811 /* Returns the handle to the first sym crypto instance */
1812 static CpaInstanceHandle
1813 Lac_GetFirstSymHandle(icp_accel_dev_t *adfInsts[ADF_MAX_DEVICES],
1814 		      Cpa16U num_dev)
1815 {
1816 	CpaStatus status = CPA_STATUS_SUCCESS;
1817 	icp_accel_dev_t *dev_addr = NULL;
1818 	sal_t *base_addr = NULL;
1819 	sal_list_t *list_temp = NULL;
1820 	CpaInstanceHandle cyInst = NULL;
1821 	CpaInstanceInfo2 info;
1822 	Cpa16U i = 0;
1823 
1824 	for (i = 0; i < num_dev; i++) {
1825 		dev_addr = (icp_accel_dev_t *)adfInsts[i];
1826 		base_addr = dev_addr->pSalHandle;
1827 		if (NULL == base_addr) {
1828 			continue;
1829 		}
1830 		list_temp = base_addr->sym_services;
1831 		while (NULL != list_temp) {
1832 			cyInst = SalList_getObject(list_temp);
1833 			status = cpaCyInstanceGetInfo2(cyInst, &info);
1834 			list_temp = SalList_next(list_temp);
1835 			if (CPA_STATUS_SUCCESS != status ||
1836 			    CPA_TRUE != info.isPolled) {
1837 				cyInst = NULL;
1838 				continue;
1839 			}
1840 			break;
1841 		}
1842 		if (cyInst) {
1843 			break;
1844 		}
1845 	}
1846 
1847 	return cyInst;
1848 }
1849 
1850 /* Returns the handle to the first crypto instance
1851  * Note that the crypto instance in this case supports
1852  * both asym and sym services */
1853 static CpaInstanceHandle
1854 Lac_GetFirstCyHandle(icp_accel_dev_t *adfInsts[ADF_MAX_DEVICES], Cpa16U num_dev)
1855 {
1856 	CpaStatus status = CPA_STATUS_SUCCESS;
1857 	icp_accel_dev_t *dev_addr = NULL;
1858 	sal_t *base_addr = NULL;
1859 	sal_list_t *list_temp = NULL;
1860 	CpaInstanceHandle cyInst = NULL;
1861 	CpaInstanceInfo2 info;
1862 	Cpa16U i = 0;
1863 
1864 	for (i = 0; i < num_dev; i++) {
1865 		dev_addr = (icp_accel_dev_t *)adfInsts[i];
1866 		base_addr = dev_addr->pSalHandle;
1867 		if (NULL == base_addr) {
1868 			continue;
1869 		}
1870 		list_temp = base_addr->crypto_services;
1871 		while (NULL != list_temp) {
1872 			cyInst = SalList_getObject(list_temp);
1873 			status = cpaCyInstanceGetInfo2(cyInst, &info);
1874 			list_temp = SalList_next(list_temp);
1875 			if (CPA_STATUS_SUCCESS != status ||
1876 			    CPA_TRUE != info.isPolled) {
1877 				cyInst = NULL;
1878 				continue;
1879 			}
1880 			break;
1881 		}
1882 		if (cyInst) {
1883 			break;
1884 		}
1885 	}
1886 
1887 	return cyInst;
1888 }
1889 
1890 CpaInstanceHandle
1891 Lac_GetFirstHandle(sal_service_type_t svc_type)
1892 {
1893 	CpaStatus status = CPA_STATUS_SUCCESS;
1894 	static icp_accel_dev_t *adfInsts[ADF_MAX_DEVICES] = { 0 };
1895 	CpaInstanceHandle cyInst = NULL;
1896 	Cpa16U num_cy_dev = 0;
1897 	Cpa32U capabilities = 0;
1898 
1899 	switch (svc_type) {
1900 	case SAL_SERVICE_TYPE_CRYPTO_ASYM:
1901 		capabilities = ICP_ACCEL_CAPABILITIES_CRYPTO_ASYMMETRIC;
1902 		break;
1903 	case SAL_SERVICE_TYPE_CRYPTO_SYM:
1904 		capabilities = ICP_ACCEL_CAPABILITIES_CRYPTO_SYMMETRIC;
1905 		break;
1906 	case SAL_SERVICE_TYPE_CRYPTO:
1907 		capabilities = ICP_ACCEL_CAPABILITIES_CRYPTO_ASYMMETRIC;
1908 		capabilities |= ICP_ACCEL_CAPABILITIES_CRYPTO_SYMMETRIC;
1909 		break;
1910 	default:
1911 		LAC_LOG_ERROR("Invalid service type\n");
1912 		return NULL;
1913 		break;
1914 	}
1915 	/* Only need 1 dev with crypto enabled - so check all devices*/
1916 	status = icp_amgr_getAllAccelDevByEachCapability(capabilities,
1917 							 adfInsts,
1918 							 &num_cy_dev);
1919 	if ((0 == num_cy_dev) || (CPA_STATUS_SUCCESS != status)) {
1920 		LAC_LOG_ERROR("No crypto devices enabled in the system\n");
1921 		return NULL;
1922 	}
1923 
1924 	switch (svc_type) {
1925 	case SAL_SERVICE_TYPE_CRYPTO_ASYM:
1926 		/* Try to find an asym only instance first */
1927 		cyInst = Lac_GetFirstAsymHandle(adfInsts, num_cy_dev);
1928 		/* Try to find a cy instance since it also supports asym */
1929 		if (NULL == cyInst) {
1930 			cyInst = Lac_GetFirstCyHandle(adfInsts, num_cy_dev);
1931 		}
1932 		break;
1933 	case SAL_SERVICE_TYPE_CRYPTO_SYM:
1934 		/* Try to find a sym only instance first */
1935 		cyInst = Lac_GetFirstSymHandle(adfInsts, num_cy_dev);
1936 		/* Try to find a cy instance since it also supports sym */
1937 		if (NULL == cyInst) {
1938 			cyInst = Lac_GetFirstCyHandle(adfInsts, num_cy_dev);
1939 		}
1940 		break;
1941 	case SAL_SERVICE_TYPE_CRYPTO:
1942 		/* Try to find a cy instance */
1943 		cyInst = Lac_GetFirstCyHandle(adfInsts, num_cy_dev);
1944 		break;
1945 	default:
1946 		break;
1947 	}
1948 	if (NULL == cyInst) {
1949 		LAC_LOG_ERROR("No remaining crypto instances available\n");
1950 	}
1951 	return cyInst;
1952 }
1953 
1954 CpaStatus
1955 icp_sal_NrbgGetInflightRequests(CpaInstanceHandle instanceHandle_in,
1956 				Cpa32U *maxInflightRequests,
1957 				Cpa32U *numInflightRequests)
1958 {
1959 	return CPA_STATUS_UNSUPPORTED;
1960 }
1961 
1962 CpaStatus
1963 icp_sal_SymGetInflightRequests(CpaInstanceHandle instanceHandle,
1964 			       Cpa32U *maxInflightRequests,
1965 			       Cpa32U *numInflightRequests)
1966 {
1967 	sal_crypto_service_t *crypto_handle = NULL;
1968 
1969 	crypto_handle = (sal_crypto_service_t *)instanceHandle;
1970 
1971 	LAC_CHECK_NULL_PARAM(crypto_handle);
1972 	LAC_CHECK_NULL_PARAM(maxInflightRequests);
1973 	LAC_CHECK_NULL_PARAM(numInflightRequests);
1974 	SAL_RUNNING_CHECK(crypto_handle);
1975 
1976 	return icp_adf_getInflightRequests(crypto_handle->trans_handle_sym_tx,
1977 					   maxInflightRequests,
1978 					   numInflightRequests);
1979 }
1980 
1981 
1982 CpaStatus
1983 icp_sal_dp_SymGetInflightRequests(CpaInstanceHandle instanceHandle,
1984 				  Cpa32U *maxInflightRequests,
1985 				  Cpa32U *numInflightRequests)
1986 {
1987 	sal_crypto_service_t *crypto_handle = NULL;
1988 
1989 	crypto_handle = (sal_crypto_service_t *)instanceHandle;
1990 
1991 	return icp_adf_dp_getInflightRequests(
1992 	    crypto_handle->trans_handle_sym_tx,
1993 	    maxInflightRequests,
1994 	    numInflightRequests);
1995 }
1996 
1997 
1998 CpaStatus
1999 icp_sal_setForceAEADMACVerify(CpaInstanceHandle instanceHandle,
2000 			      CpaBoolean forceAEADMacVerify)
2001 {
2002 	sal_crypto_service_t *crypto_handle = NULL;
2003 
2004 	crypto_handle = (sal_crypto_service_t *)instanceHandle;
2005 	LAC_CHECK_NULL_PARAM(crypto_handle);
2006 	crypto_handle->forceAEADMacVerify = forceAEADMacVerify;
2007 
2008 	return CPA_STATUS_SUCCESS;
2009 }
2010