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