aboutsummaryrefslogtreecommitdiff
path: root/sys/dev/qat/qat_api/common/ctrl/sal_get_instances.c
blob: c09d75608bb44c6afff24a8a972e6192878492b8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
/* SPDX-License-Identifier: BSD-3-Clause */
/* Copyright(c) 2007-2026 Intel Corporation */

/**
 *****************************************************************************
 * @file sal_get_instances.c
 *
 * @defgroup SalCtrl Service Access Layer Controller
 *
 * @ingroup SalCtrl
 *
 * @description
 *      This file contains generic functions to get instances of a specified
 *      service type. Note these are complementary to the already existing
 *      service-specific functions.
 *
 *****************************************************************************/

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

/* QAT-API includes */
#include "cpa.h"
#include "cpa_cy_common.h"
#include "cpa_cy_im.h"
#include "cpa_dc.h"

/* ADF includes */
#include "icp_accel_devices.h"
#include "icp_adf_accel_mgr.h"

/* SAL includes */
#include "lac_mem.h"
#include "lac_list.h"
#include "lac_sal_types.h"
#include "lac_sal_types_crypto.h"

/**
 ******************************************************************************
 * @ingroup SalCtrl
 * @description
 *   Get the total number of either sym, asym or cy instances
 *****************************************************************************/
CpaStatus
Lac_GetCyNumInstancesByType(
    const CpaAccelerationServiceType accelerationServiceType,
    Cpa16U *pNumInstances)
{
	CpaStatus status = CPA_STATUS_SUCCESS;
	CpaInstanceHandle instanceHandle;
	CpaInstanceInfo2 info;
	icp_accel_dev_t **pAdfInsts = NULL;
	icp_accel_dev_t *dev_addr = NULL;
	sal_t *base_addr = NULL;
	sal_list_t *list_temp = NULL;
	Cpa16U num_accel_dev = 0;
	Cpa16U num_inst = 0;
	Cpa16U i = 0;
	Cpa32U accel_capability = 0;
	char *service = NULL;

	LAC_CHECK_NULL_PARAM(pNumInstances);
	*pNumInstances = 0;

	switch (accelerationServiceType) {
	case CPA_ACC_SVC_TYPE_CRYPTO_ASYM:
		accel_capability = ICP_ACCEL_CAPABILITIES_CRYPTO_ASYMMETRIC;
		service = "asym";
		break;

	case CPA_ACC_SVC_TYPE_CRYPTO_SYM:
		accel_capability = ICP_ACCEL_CAPABILITIES_CRYPTO_SYMMETRIC;
		service = "sym";
		break;

	case CPA_ACC_SVC_TYPE_CRYPTO:
		accel_capability = ICP_ACCEL_CAPABILITIES_CRYPTO_SYMMETRIC |
		    ICP_ACCEL_CAPABILITIES_CRYPTO_ASYMMETRIC;
		service = "cy";
		break;

	default:
		QAT_UTILS_LOG("Invalid service type\n");
		return CPA_STATUS_INVALID_PARAM;
	}

	/* Get the number of accel_dev in the system */
	status = icp_amgr_getNumInstances(&num_accel_dev);
	LAC_CHECK_STATUS(status);

	/* Allocate memory to store addr of accel_devs */
	pAdfInsts = malloc(num_accel_dev * sizeof(icp_accel_dev_t *),
			   M_QAT,
			   M_WAITOK | M_ZERO);
	if (NULL == pAdfInsts) {
		QAT_UTILS_LOG("Failed to allocate dev instance memory\n");
		return CPA_STATUS_RESOURCE;
	}

	num_accel_dev = 0;
	status = icp_amgr_getAllAccelDevByCapabilities(accel_capability,
						       pAdfInsts,
						       &num_accel_dev);
	if (CPA_STATUS_SUCCESS != status) {
		QAT_UTILS_LOG("No support for service %s\n", service);
		free(pAdfInsts, M_QAT);
		return status;
	}

	for (i = 0; i < num_accel_dev; i++) {
		dev_addr = pAdfInsts[i];
		if (NULL == dev_addr || NULL == dev_addr->pSalHandle) {
			continue;
		}
		base_addr = dev_addr->pSalHandle;

		if (CPA_ACC_SVC_TYPE_CRYPTO == accelerationServiceType) {
			list_temp = base_addr->crypto_services;
			while (NULL != list_temp) {
				instanceHandle = SalList_getObject(list_temp);
				status = cpaCyInstanceGetInfo2(instanceHandle,
							       &info);
				if (CPA_STATUS_SUCCESS == status &&
				    CPA_TRUE == info.isPolled) {
					num_inst++;
				}
				list_temp = SalList_next(list_temp);
			}
		}

		if (CPA_ACC_SVC_TYPE_CRYPTO_ASYM == accelerationServiceType ||
		    CPA_ACC_SVC_TYPE_CRYPTO == accelerationServiceType) {
			list_temp = base_addr->asym_services;
			if ((NULL == list_temp) &&
			    (CPA_ACC_SVC_TYPE_CRYPTO !=
			     accelerationServiceType)) {
				list_temp = base_addr->crypto_services;
			}
			while (NULL != list_temp) {
				instanceHandle = SalList_getObject(list_temp);
				status = cpaCyInstanceGetInfo2(instanceHandle,
							       &info);
				if (CPA_STATUS_SUCCESS == status &&
				    CPA_TRUE == info.isPolled) {
					num_inst++;
				}
				list_temp = SalList_next(list_temp);
			}
		}

		if (CPA_ACC_SVC_TYPE_CRYPTO_SYM == accelerationServiceType ||
		    CPA_ACC_SVC_TYPE_CRYPTO == accelerationServiceType) {
			list_temp = base_addr->sym_services;
			if ((NULL == list_temp) &&
			    (CPA_ACC_SVC_TYPE_CRYPTO !=
			     accelerationServiceType)) {
				list_temp = base_addr->crypto_services;
			}
			while (NULL != list_temp) {
				instanceHandle = SalList_getObject(list_temp);
				status = cpaCyInstanceGetInfo2(instanceHandle,
							       &info);
				if (CPA_STATUS_SUCCESS == status &&
				    CPA_TRUE == info.isPolled) {
					num_inst++;
				}
				list_temp = SalList_next(list_temp);
			}
		}
	}

	*pNumInstances = num_inst;
	free(pAdfInsts, M_QAT);

	return status;
}

/**
 ******************************************************************************
 * @ingroup SalCtrl
 * @description
 *   Get either sym, asym or cy instance
 *****************************************************************************/
CpaStatus
Lac_GetCyInstancesByType(
    const CpaAccelerationServiceType accelerationServiceType,
    Cpa16U numInstances,
    CpaInstanceHandle *pInstances)
{
	CpaStatus status = CPA_STATUS_SUCCESS;
	CpaInstanceHandle instanceHandle = NULL;
	CpaInstanceInfo2 info;
	icp_accel_dev_t **pAdfInsts = NULL;
	icp_accel_dev_t *dev_addr = NULL;
	sal_t *base_addr = NULL;
	sal_list_t *list_temp = NULL;
	Cpa16U num_accel_dev = 0;
	Cpa16U num_allocated_instances = 0;
	Cpa16U index = 0;
	Cpa16U i = 0;
	Cpa32U accel_capability = 0;
	char *service = NULL;

	LAC_CHECK_NULL_PARAM(pInstances);
	if (0 == numInstances) {
		QAT_UTILS_LOG("NumInstances is 0\n");
		return CPA_STATUS_INVALID_PARAM;
	}

	switch (accelerationServiceType) {
	case CPA_ACC_SVC_TYPE_CRYPTO_ASYM:
		accel_capability = ICP_ACCEL_CAPABILITIES_CRYPTO_ASYMMETRIC;
		service = "asym";
		break;

	case CPA_ACC_SVC_TYPE_CRYPTO_SYM:
		accel_capability = ICP_ACCEL_CAPABILITIES_CRYPTO_SYMMETRIC;
		service = "sym";
		break;

	case CPA_ACC_SVC_TYPE_CRYPTO:
		accel_capability = ICP_ACCEL_CAPABILITIES_CRYPTO_SYMMETRIC |
		    ICP_ACCEL_CAPABILITIES_CRYPTO_ASYMMETRIC;
		service = "cy";
		break;

	default:
		QAT_UTILS_LOG("Invalid service type\n");
		return CPA_STATUS_INVALID_PARAM;
	}

	/* Get the number of instances */
	status = Lac_GetCyNumInstancesByType(accelerationServiceType,
					     &num_allocated_instances);
	if (CPA_STATUS_SUCCESS != status) {
		return status;
	}

	if (numInstances > num_allocated_instances) {
		QAT_UTILS_LOG("Only %d instances available\n",
			      num_allocated_instances);
		return CPA_STATUS_RESOURCE;
	}

	/* Get the number of accel devices in the system */
	status = icp_amgr_getNumInstances(&num_accel_dev);
	LAC_CHECK_STATUS(status);

	/* Allocate memory to store addr of accel_devs */
	pAdfInsts = malloc(num_accel_dev * sizeof(icp_accel_dev_t *),
			   M_QAT,
			   M_WAITOK | M_ZERO);
	if (NULL == pAdfInsts) {
		QAT_UTILS_LOG("Failed to allocate dev instance memory\n");
		return CPA_STATUS_RESOURCE;
	}

	num_accel_dev = 0;
	status = icp_amgr_getAllAccelDevByCapabilities(accel_capability,
						       pAdfInsts,
						       &num_accel_dev);
	if (CPA_STATUS_SUCCESS != status) {
		QAT_UTILS_LOG("No support for service %s\n", service);
		free(pAdfInsts, M_QAT);
		return status;
	}

	for (i = 0; i < num_accel_dev; i++) {
		dev_addr = pAdfInsts[i];
		/* Note dev_addr cannot be NULL here as numInstances = 0
		 * is not valid and if dev_addr = NULL then index = 0 (which
		 * is less than numInstances and status is set to _RESOURCE
		 * above)
		 */
		base_addr = dev_addr->pSalHandle;
		if (NULL == base_addr) {
			continue;
		}

		if (CPA_ACC_SVC_TYPE_CRYPTO == accelerationServiceType) {
			list_temp = base_addr->crypto_services;
			while (NULL != list_temp) {
				if (index > (numInstances - 1))
					break;

				instanceHandle = SalList_getObject(list_temp);
				status = cpaCyInstanceGetInfo2(instanceHandle,
							       &info);
				list_temp = SalList_next(list_temp);
				if (CPA_STATUS_SUCCESS != status ||
				    CPA_TRUE != info.isPolled) {
					continue;
				}
				pInstances[index] = instanceHandle;
				index++;
			}
		}

		if (CPA_ACC_SVC_TYPE_CRYPTO_ASYM == accelerationServiceType ||
		    CPA_ACC_SVC_TYPE_CRYPTO == accelerationServiceType) {
			list_temp = base_addr->asym_services;
			if ((NULL == list_temp) &&
			    (CPA_ACC_SVC_TYPE_CRYPTO !=
			     accelerationServiceType)) {
				list_temp = base_addr->crypto_services;
			}
			while (NULL != list_temp) {
				if (index > (numInstances - 1))
					break;

				instanceHandle = SalList_getObject(list_temp);
				status = cpaCyInstanceGetInfo2(instanceHandle,
							       &info);
				list_temp = SalList_next(list_temp);
				if (CPA_STATUS_SUCCESS != status ||
				    CPA_TRUE != info.isPolled) {
					continue;
				}
				pInstances[index] = instanceHandle;
				index++;
			}
		}

		if (CPA_ACC_SVC_TYPE_CRYPTO_SYM == accelerationServiceType ||
		    CPA_ACC_SVC_TYPE_CRYPTO == accelerationServiceType) {
			list_temp = base_addr->sym_services;
			if ((NULL == list_temp) &&
			    (CPA_ACC_SVC_TYPE_CRYPTO !=
			     accelerationServiceType)) {
				list_temp = base_addr->crypto_services;
			}
			while (NULL != list_temp) {
				if (index > (numInstances - 1))
					break;

				instanceHandle = SalList_getObject(list_temp);
				status = cpaCyInstanceGetInfo2(instanceHandle,
							       &info);
				list_temp = SalList_next(list_temp);
				if (CPA_STATUS_SUCCESS != status ||
				    CPA_TRUE != info.isPolled) {
					continue;
				}
				pInstances[index] = instanceHandle;
				index++;
			}
		}
	}
	free(pAdfInsts, M_QAT);

	return status;
}

/**
 ******************************************************************************
 * @ingroup SalCtrl
 *****************************************************************************/
CpaStatus
cpaGetNumInstances(const CpaAccelerationServiceType accelerationServiceType,
		   Cpa16U *pNumInstances)
{
	LAC_CHECK_NULL_PARAM(pNumInstances);

	switch (accelerationServiceType) {
	case CPA_ACC_SVC_TYPE_CRYPTO_ASYM:
	case CPA_ACC_SVC_TYPE_CRYPTO_SYM:
	case CPA_ACC_SVC_TYPE_CRYPTO:
		return Lac_GetCyNumInstancesByType(accelerationServiceType,
						   pNumInstances);

	case CPA_ACC_SVC_TYPE_DATA_COMPRESSION:
		return cpaDcGetNumInstances(pNumInstances);

	case CPA_ACC_SVC_TYPE_PATTERN_MATCH:
	case CPA_ACC_SVC_TYPE_RAID:
	case CPA_ACC_SVC_TYPE_XML:
		QAT_UTILS_LOG("Unsupported service type\n");
		return CPA_STATUS_UNSUPPORTED;

	default:
		QAT_UTILS_LOG("Invalid service type\n");
		*pNumInstances = 0;
		return CPA_STATUS_INVALID_PARAM;
	}
}

/**
 ******************************************************************************
 * @ingroup SalCtrl
 *****************************************************************************/
CpaStatus
cpaGetInstances(const CpaAccelerationServiceType accelerationServiceType,
		Cpa16U numInstances,
		CpaInstanceHandle *pInstances)
{
	LAC_CHECK_NULL_PARAM(pInstances);

	switch (accelerationServiceType) {
	case CPA_ACC_SVC_TYPE_CRYPTO_ASYM:
	case CPA_ACC_SVC_TYPE_CRYPTO_SYM:
	case CPA_ACC_SVC_TYPE_CRYPTO:
		return Lac_GetCyInstancesByType(accelerationServiceType,
						numInstances,
						pInstances);

	case CPA_ACC_SVC_TYPE_DATA_COMPRESSION:
		return cpaDcGetInstances(numInstances, pInstances);

	case CPA_ACC_SVC_TYPE_PATTERN_MATCH:
	case CPA_ACC_SVC_TYPE_RAID:
	case CPA_ACC_SVC_TYPE_XML:
		QAT_UTILS_LOG("Unsupported service type\n");
		return CPA_STATUS_UNSUPPORTED;

	default:
		QAT_UTILS_LOG("Invalid service type\n");
		return CPA_STATUS_INVALID_PARAM;
	}
}