1 /* SPDX-License-Identifier: BSD-3-Clause */
2 /* Copyright(c) 2007-2025 Intel Corporation */
3
4 /**
5 *****************************************************************************
6 * @file sal_get_instances.c
7 *
8 * @defgroup SalCtrl Service Access Layer Controller
9 *
10 * @ingroup SalCtrl
11 *
12 * @description
13 * This file contains generic functions to get instances of a specified
14 * service type. Note these are complementary to the already existing
15 * service-specific functions.
16 *
17 *****************************************************************************/
18
19 /*
20 *******************************************************************************
21 * Include public/global header files
22 *******************************************************************************
23 */
24
25 /* QAT-API includes */
26 #include "cpa.h"
27 #include "cpa_cy_common.h"
28 #include "cpa_cy_im.h"
29 #include "cpa_dc.h"
30
31 /* ADF includes */
32 #include "icp_accel_devices.h"
33 #include "icp_adf_accel_mgr.h"
34
35 /* SAL includes */
36 #include "lac_mem.h"
37 #include "lac_list.h"
38 #include "lac_sal_types.h"
39 #include "lac_sal_types_crypto.h"
40
41 /**
42 ******************************************************************************
43 * @ingroup SalCtrl
44 * @description
45 * Get the total number of either sym, asym or cy instances
46 *****************************************************************************/
47 CpaStatus
Lac_GetCyNumInstancesByType(const CpaAccelerationServiceType accelerationServiceType,Cpa16U * pNumInstances)48 Lac_GetCyNumInstancesByType(
49 const CpaAccelerationServiceType accelerationServiceType,
50 Cpa16U *pNumInstances)
51 {
52 CpaStatus status = CPA_STATUS_SUCCESS;
53 CpaInstanceHandle instanceHandle;
54 CpaInstanceInfo2 info;
55 icp_accel_dev_t **pAdfInsts = NULL;
56 icp_accel_dev_t *dev_addr = NULL;
57 sal_t *base_addr = NULL;
58 sal_list_t *list_temp = NULL;
59 Cpa16U num_accel_dev = 0;
60 Cpa16U num_inst = 0;
61 Cpa16U i = 0;
62 Cpa32U accel_capability = 0;
63 char *service = NULL;
64
65 LAC_CHECK_NULL_PARAM(pNumInstances);
66 *pNumInstances = 0;
67
68 switch (accelerationServiceType) {
69 case CPA_ACC_SVC_TYPE_CRYPTO_ASYM:
70 accel_capability = ICP_ACCEL_CAPABILITIES_CRYPTO_ASYMMETRIC;
71 service = "asym";
72 break;
73
74 case CPA_ACC_SVC_TYPE_CRYPTO_SYM:
75 accel_capability = ICP_ACCEL_CAPABILITIES_CRYPTO_SYMMETRIC;
76 service = "sym";
77 break;
78
79 case CPA_ACC_SVC_TYPE_CRYPTO:
80 accel_capability = ICP_ACCEL_CAPABILITIES_CRYPTO_SYMMETRIC |
81 ICP_ACCEL_CAPABILITIES_CRYPTO_ASYMMETRIC;
82 service = "cy";
83 break;
84
85 default:
86 QAT_UTILS_LOG("Invalid service type\n");
87 return CPA_STATUS_INVALID_PARAM;
88 }
89
90 /* Get the number of accel_dev in the system */
91 status = icp_amgr_getNumInstances(&num_accel_dev);
92 LAC_CHECK_STATUS(status);
93
94 /* Allocate memory to store addr of accel_devs */
95 pAdfInsts = malloc(num_accel_dev * sizeof(icp_accel_dev_t *),
96 M_QAT,
97 M_WAITOK | M_ZERO);
98 if (NULL == pAdfInsts) {
99 QAT_UTILS_LOG("Failed to allocate dev instance memory\n");
100 return CPA_STATUS_RESOURCE;
101 }
102
103 num_accel_dev = 0;
104 status = icp_amgr_getAllAccelDevByCapabilities(accel_capability,
105 pAdfInsts,
106 &num_accel_dev);
107 if (CPA_STATUS_SUCCESS != status) {
108 QAT_UTILS_LOG("No support for service %s\n", service);
109 free(pAdfInsts, M_QAT);
110 return status;
111 }
112
113 for (i = 0; i < num_accel_dev; i++) {
114 dev_addr = pAdfInsts[i];
115 if (NULL == dev_addr || NULL == dev_addr->pSalHandle) {
116 continue;
117 }
118 base_addr = dev_addr->pSalHandle;
119
120 if (CPA_ACC_SVC_TYPE_CRYPTO == accelerationServiceType) {
121 list_temp = base_addr->crypto_services;
122 while (NULL != list_temp) {
123 instanceHandle = SalList_getObject(list_temp);
124 status = cpaCyInstanceGetInfo2(instanceHandle,
125 &info);
126 if (CPA_STATUS_SUCCESS == status &&
127 CPA_TRUE == info.isPolled) {
128 num_inst++;
129 }
130 list_temp = SalList_next(list_temp);
131 }
132 }
133
134 if (CPA_ACC_SVC_TYPE_CRYPTO_ASYM == accelerationServiceType ||
135 CPA_ACC_SVC_TYPE_CRYPTO == accelerationServiceType) {
136 list_temp = base_addr->asym_services;
137 while (NULL != list_temp) {
138 instanceHandle = SalList_getObject(list_temp);
139 status = cpaCyInstanceGetInfo2(instanceHandle,
140 &info);
141 if (CPA_STATUS_SUCCESS == status &&
142 CPA_TRUE == info.isPolled) {
143 num_inst++;
144 }
145 list_temp = SalList_next(list_temp);
146 }
147 }
148
149 if (CPA_ACC_SVC_TYPE_CRYPTO_SYM == accelerationServiceType ||
150 CPA_ACC_SVC_TYPE_CRYPTO == accelerationServiceType) {
151 list_temp = base_addr->sym_services;
152 while (NULL != list_temp) {
153 instanceHandle = SalList_getObject(list_temp);
154 status = cpaCyInstanceGetInfo2(instanceHandle,
155 &info);
156 if (CPA_STATUS_SUCCESS == status &&
157 CPA_TRUE == info.isPolled) {
158 num_inst++;
159 }
160 list_temp = SalList_next(list_temp);
161 }
162 }
163 }
164
165 *pNumInstances = num_inst;
166 free(pAdfInsts, M_QAT);
167
168 return status;
169 }
170
171 /**
172 ******************************************************************************
173 * @ingroup SalCtrl
174 * @description
175 * Get either sym, asym or cy instance
176 *****************************************************************************/
177 CpaStatus
Lac_GetCyInstancesByType(const CpaAccelerationServiceType accelerationServiceType,Cpa16U numInstances,CpaInstanceHandle * pInstances)178 Lac_GetCyInstancesByType(
179 const CpaAccelerationServiceType accelerationServiceType,
180 Cpa16U numInstances,
181 CpaInstanceHandle *pInstances)
182 {
183 CpaStatus status = CPA_STATUS_SUCCESS;
184 CpaInstanceHandle instanceHandle = NULL;
185 CpaInstanceInfo2 info;
186 icp_accel_dev_t **pAdfInsts = NULL;
187 icp_accel_dev_t *dev_addr = NULL;
188 sal_t *base_addr = NULL;
189 sal_list_t *list_temp = NULL;
190 Cpa16U num_accel_dev = 0;
191 Cpa16U num_allocated_instances = 0;
192 Cpa16U index = 0;
193 Cpa16U i = 0;
194 Cpa32U accel_capability = 0;
195 char *service = NULL;
196
197 LAC_CHECK_NULL_PARAM(pInstances);
198 if (0 == numInstances) {
199 QAT_UTILS_LOG("NumInstances is 0\n");
200 return CPA_STATUS_INVALID_PARAM;
201 }
202
203 switch (accelerationServiceType) {
204 case CPA_ACC_SVC_TYPE_CRYPTO_ASYM:
205 accel_capability = ICP_ACCEL_CAPABILITIES_CRYPTO_ASYMMETRIC;
206 service = "asym";
207 break;
208
209 case CPA_ACC_SVC_TYPE_CRYPTO_SYM:
210 accel_capability = ICP_ACCEL_CAPABILITIES_CRYPTO_SYMMETRIC;
211 service = "sym";
212 break;
213
214 case CPA_ACC_SVC_TYPE_CRYPTO:
215 accel_capability = ICP_ACCEL_CAPABILITIES_CRYPTO_SYMMETRIC |
216 ICP_ACCEL_CAPABILITIES_CRYPTO_ASYMMETRIC;
217 service = "cy";
218 break;
219
220 default:
221 QAT_UTILS_LOG("Invalid service type\n");
222 return CPA_STATUS_INVALID_PARAM;
223 }
224
225 /* Get the number of instances */
226 status = Lac_GetCyNumInstancesByType(accelerationServiceType,
227 &num_allocated_instances);
228 if (CPA_STATUS_SUCCESS != status) {
229 return status;
230 }
231
232 if (numInstances > num_allocated_instances) {
233 QAT_UTILS_LOG("Only %d instances available\n",
234 num_allocated_instances);
235 return CPA_STATUS_RESOURCE;
236 }
237
238 /* Get the number of accel devices in the system */
239 status = icp_amgr_getNumInstances(&num_accel_dev);
240 LAC_CHECK_STATUS(status);
241
242 /* Allocate memory to store addr of accel_devs */
243 pAdfInsts = malloc(num_accel_dev * sizeof(icp_accel_dev_t *),
244 M_QAT,
245 M_WAITOK | M_ZERO);
246 if (NULL == pAdfInsts) {
247 QAT_UTILS_LOG("Failed to allocate dev instance memory\n");
248 return CPA_STATUS_RESOURCE;
249 }
250
251 num_accel_dev = 0;
252 status = icp_amgr_getAllAccelDevByCapabilities(accel_capability,
253 pAdfInsts,
254 &num_accel_dev);
255 if (CPA_STATUS_SUCCESS != status) {
256 QAT_UTILS_LOG("No support for service %s\n", service);
257 free(pAdfInsts, M_QAT);
258 return status;
259 }
260
261 for (i = 0; i < num_accel_dev; i++) {
262 dev_addr = pAdfInsts[i];
263 /* Note dev_addr cannot be NULL here as numInstances = 0
264 * is not valid and if dev_addr = NULL then index = 0 (which
265 * is less than numInstances and status is set to _RESOURCE
266 * above)
267 */
268 base_addr = dev_addr->pSalHandle;
269 if (NULL == base_addr) {
270 continue;
271 }
272
273 if (CPA_ACC_SVC_TYPE_CRYPTO == accelerationServiceType) {
274 list_temp = base_addr->crypto_services;
275 while (NULL != list_temp) {
276 if (index > (numInstances - 1))
277 break;
278
279 instanceHandle = SalList_getObject(list_temp);
280 status = cpaCyInstanceGetInfo2(instanceHandle,
281 &info);
282 list_temp = SalList_next(list_temp);
283 if (CPA_STATUS_SUCCESS != status ||
284 CPA_TRUE != info.isPolled) {
285 continue;
286 }
287 pInstances[index] = instanceHandle;
288 index++;
289 }
290 }
291
292 if (CPA_ACC_SVC_TYPE_CRYPTO_ASYM == accelerationServiceType ||
293 CPA_ACC_SVC_TYPE_CRYPTO == accelerationServiceType) {
294 list_temp = base_addr->asym_services;
295 while (NULL != list_temp) {
296 if (index > (numInstances - 1))
297 break;
298
299 instanceHandle = SalList_getObject(list_temp);
300 status = cpaCyInstanceGetInfo2(instanceHandle,
301 &info);
302 list_temp = SalList_next(list_temp);
303 if (CPA_STATUS_SUCCESS != status ||
304 CPA_TRUE != info.isPolled) {
305 continue;
306 }
307 pInstances[index] = instanceHandle;
308 index++;
309 }
310 }
311
312 if (CPA_ACC_SVC_TYPE_CRYPTO_SYM == accelerationServiceType ||
313 CPA_ACC_SVC_TYPE_CRYPTO == accelerationServiceType) {
314 list_temp = base_addr->sym_services;
315 while (NULL != list_temp) {
316 if (index > (numInstances - 1))
317 break;
318
319 instanceHandle = SalList_getObject(list_temp);
320 status = cpaCyInstanceGetInfo2(instanceHandle,
321 &info);
322 list_temp = SalList_next(list_temp);
323 if (CPA_STATUS_SUCCESS != status ||
324 CPA_TRUE != info.isPolled) {
325 continue;
326 }
327 pInstances[index] = instanceHandle;
328 index++;
329 }
330 }
331 }
332 free(pAdfInsts, M_QAT);
333
334 return status;
335 }
336
337 /**
338 ******************************************************************************
339 * @ingroup SalCtrl
340 *****************************************************************************/
341 CpaStatus
cpaGetNumInstances(const CpaAccelerationServiceType accelerationServiceType,Cpa16U * pNumInstances)342 cpaGetNumInstances(const CpaAccelerationServiceType accelerationServiceType,
343 Cpa16U *pNumInstances)
344 {
345 LAC_CHECK_NULL_PARAM(pNumInstances);
346
347 switch (accelerationServiceType) {
348 case CPA_ACC_SVC_TYPE_CRYPTO_ASYM:
349 case CPA_ACC_SVC_TYPE_CRYPTO_SYM:
350 case CPA_ACC_SVC_TYPE_CRYPTO:
351 return Lac_GetCyNumInstancesByType(accelerationServiceType,
352 pNumInstances);
353
354 case CPA_ACC_SVC_TYPE_DATA_COMPRESSION:
355 return cpaDcGetNumInstances(pNumInstances);
356
357 case CPA_ACC_SVC_TYPE_PATTERN_MATCH:
358 case CPA_ACC_SVC_TYPE_RAID:
359 case CPA_ACC_SVC_TYPE_XML:
360 QAT_UTILS_LOG("Unsupported service type\n");
361 return CPA_STATUS_UNSUPPORTED;
362
363 default:
364 QAT_UTILS_LOG("Invalid service type\n");
365 *pNumInstances = 0;
366 return CPA_STATUS_INVALID_PARAM;
367 }
368 }
369
370 /**
371 ******************************************************************************
372 * @ingroup SalCtrl
373 *****************************************************************************/
374 CpaStatus
cpaGetInstances(const CpaAccelerationServiceType accelerationServiceType,Cpa16U numInstances,CpaInstanceHandle * pInstances)375 cpaGetInstances(const CpaAccelerationServiceType accelerationServiceType,
376 Cpa16U numInstances,
377 CpaInstanceHandle *pInstances)
378 {
379 LAC_CHECK_NULL_PARAM(pInstances);
380
381 switch (accelerationServiceType) {
382 case CPA_ACC_SVC_TYPE_CRYPTO_ASYM:
383 case CPA_ACC_SVC_TYPE_CRYPTO_SYM:
384 case CPA_ACC_SVC_TYPE_CRYPTO:
385 return Lac_GetCyInstancesByType(accelerationServiceType,
386 numInstances,
387 pInstances);
388
389 case CPA_ACC_SVC_TYPE_DATA_COMPRESSION:
390 return cpaDcGetInstances(numInstances, pInstances);
391
392 case CPA_ACC_SVC_TYPE_PATTERN_MATCH:
393 case CPA_ACC_SVC_TYPE_RAID:
394 case CPA_ACC_SVC_TYPE_XML:
395 QAT_UTILS_LOG("Unsupported service type\n");
396 return CPA_STATUS_UNSUPPORTED;
397
398 default:
399 QAT_UTILS_LOG("Invalid service type\n");
400 return CPA_STATUS_INVALID_PARAM;
401 }
402 }
403