sal_get_instances.c (71625ec9) sal_get_instances.c (266b0663)
1/* SPDX-License-Identifier: BSD-3-Clause */
2/* Copyright(c) 2007-2022 Intel Corporation */
1/* SPDX-License-Identifier: BSD-3-Clause */
2/* Copyright(c) 2007-2022 Intel Corporation */
3/* $FreeBSD$ */
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 the main function to get SAL instances.
14 *
15 *****************************************************************************/
16
17/*
18*******************************************************************************
19* Include public/global header files
20*******************************************************************************
21*/
22
23/* QAT-API includes */
24#include "cpa.h"
25#include "cpa_cy_common.h"
26#include "cpa_cy_im.h"
27#include "cpa_dc.h"
28
29/* ADF includes */
30#include "icp_accel_devices.h"
31#include "icp_adf_accel_mgr.h"
32
33/* SAL includes */
34#include "lac_mem.h"
35#include "lac_list.h"
36#include "lac_sal_types.h"
37
38/**
39 ******************************************************************************
40 * @ingroup SalCtrl
41 * @description
42 * Get either sym or asym instance number
43 *****************************************************************************/
44static CpaStatus
45Lac_GetSingleCyNumInstances(
46 const CpaAccelerationServiceType accelerationServiceType,
47 Cpa16U *pNumInstances)
48{
49 CpaStatus status = CPA_STATUS_SUCCESS;
50 icp_accel_dev_t **pAdfInsts = NULL;
51 icp_accel_dev_t *dev_addr = NULL;
52 sal_t *base_addr = NULL;
53 sal_list_t *list_temp = NULL;
54 Cpa16U num_accel_dev = 0;
55 Cpa16U num_inst = 0;
56 Cpa16U i = 0;
57 Cpa32U accel_capability = 0;
58 char *service = NULL;
59
60 LAC_CHECK_NULL_PARAM(pNumInstances);
61 *pNumInstances = 0;
62
63 switch (accelerationServiceType) {
64 case CPA_ACC_SVC_TYPE_CRYPTO_ASYM:
65 accel_capability = ICP_ACCEL_CAPABILITIES_CRYPTO_ASYMMETRIC;
66 service = "asym";
67 break;
68
69 case CPA_ACC_SVC_TYPE_CRYPTO_SYM:
70 accel_capability = ICP_ACCEL_CAPABILITIES_CRYPTO_SYMMETRIC;
71 service = "sym";
72 break;
73
74 default:
75 QAT_UTILS_LOG("Invalid service type\n");
76 return CPA_STATUS_INVALID_PARAM;
77 }
78
79 /* Get the number of accel_dev in the system */
80 status = icp_amgr_getNumInstances(&num_accel_dev);
81 LAC_CHECK_STATUS(status);
82
83 /* Allocate memory to store addr of accel_devs */
84 pAdfInsts = malloc(num_accel_dev * sizeof(icp_accel_dev_t *),
85 M_QAT,
86 M_WAITOK | M_ZERO);
87 if (NULL == pAdfInsts) {
88 QAT_UTILS_LOG("Failed to allocate dev instance memory\n");
89 return CPA_STATUS_RESOURCE;
90 }
91
92 num_accel_dev = 0;
93 status = icp_amgr_getAllAccelDevByCapabilities(accel_capability,
94 pAdfInsts,
95 &num_accel_dev);
96 if (CPA_STATUS_SUCCESS != status) {
97 QAT_UTILS_LOG("No support for service %s\n", service);
98 free(pAdfInsts, M_QAT);
99 return status;
100 }
101
102 for (i = 0; i < num_accel_dev; i++) {
103 dev_addr = pAdfInsts[i];
104 if (NULL == dev_addr || NULL == dev_addr->pSalHandle) {
105 continue;
106 }
107 base_addr = dev_addr->pSalHandle;
108
109 if (CPA_ACC_SVC_TYPE_CRYPTO_ASYM == accelerationServiceType) {
110 list_temp = base_addr->asym_services;
111 } else {
112 list_temp = base_addr->sym_services;
113 }
114 while (NULL != list_temp) {
115 num_inst++;
116 list_temp = SalList_next(list_temp);
117 }
118 }
119
120 *pNumInstances = num_inst;
121 free(pAdfInsts, M_QAT);
122
123 return status;
124}
125
126/**
127 ******************************************************************************
128 * @ingroup SalCtrl
129 * @description
130 * Get either sym or asym instance
131 *****************************************************************************/
132static CpaStatus
133Lac_GetSingleCyInstances(
134 const CpaAccelerationServiceType accelerationServiceType,
135 Cpa16U numInstances,
136 CpaInstanceHandle *pInstances)
137{
138 CpaStatus status = CPA_STATUS_SUCCESS;
139 icp_accel_dev_t **pAdfInsts = NULL;
140 icp_accel_dev_t *dev_addr = NULL;
141 sal_t *base_addr = NULL;
142 sal_list_t *list_temp = NULL;
143 Cpa16U num_accel_dev = 0;
144 Cpa16U num_allocated_instances = 0;
145 Cpa16U index = 0;
146 Cpa16U i = 0;
147 Cpa32U accel_capability = 0;
148 char *service = NULL;
149
150 LAC_CHECK_NULL_PARAM(pInstances);
151 if (0 == numInstances) {
152 QAT_UTILS_LOG("NumInstances is 0\n");
153 return CPA_STATUS_INVALID_PARAM;
154 }
155
156 switch (accelerationServiceType) {
157 case CPA_ACC_SVC_TYPE_CRYPTO_ASYM:
158 accel_capability = ICP_ACCEL_CAPABILITIES_CRYPTO_ASYMMETRIC;
159 service = "asym";
160 break;
161
162 case CPA_ACC_SVC_TYPE_CRYPTO_SYM:
163 accel_capability = ICP_ACCEL_CAPABILITIES_CRYPTO_SYMMETRIC;
164 service = "sym";
165 break;
166 default:
167 QAT_UTILS_LOG("Invalid service type\n");
168 return CPA_STATUS_INVALID_PARAM;
169 }
170
171 /* Get the number of instances */
172 status = cpaGetNumInstances(accelerationServiceType,
173 &num_allocated_instances);
174 if (CPA_STATUS_SUCCESS != status) {
175 return status;
176 }
177
178 if (numInstances > num_allocated_instances) {
179 QAT_UTILS_LOG("Only %d instances available\n",
180 num_allocated_instances);
181 return CPA_STATUS_RESOURCE;
182 }
183
184 /* Get the number of accel devices in the system */
185 status = icp_amgr_getNumInstances(&num_accel_dev);
186 LAC_CHECK_STATUS(status);
187
188 /* Allocate memory to store addr of accel_devs */
189 pAdfInsts = malloc(num_accel_dev * sizeof(icp_accel_dev_t *),
190 M_QAT,
191 M_WAITOK | M_ZERO);
192 if (NULL == pAdfInsts) {
193 QAT_UTILS_LOG("Failed to allocate dev instance memory\n");
194 return CPA_STATUS_RESOURCE;
195 }
196
197 num_accel_dev = 0;
198 status = icp_amgr_getAllAccelDevByCapabilities(accel_capability,
199 pAdfInsts,
200 &num_accel_dev);
201 if (CPA_STATUS_SUCCESS != status) {
202 QAT_UTILS_LOG("No support for service %s\n", service);
203 free(pAdfInsts, M_QAT);
204 return status;
205 }
206
207 for (i = 0; i < num_accel_dev; i++) {
208 dev_addr = pAdfInsts[i];
209 /* Note dev_addr cannot be NULL here as numInstances = 0
210 * is not valid and if dev_addr = NULL then index = 0 (which
211 * is less than numInstances and status is set to _RESOURCE
212 * above)
213 */
214 base_addr = dev_addr->pSalHandle;
215 if (NULL == base_addr) {
216 continue;
217 }
218
219 if (CPA_ACC_SVC_TYPE_CRYPTO_ASYM == accelerationServiceType)
220 list_temp = base_addr->asym_services;
221 else
222 list_temp = base_addr->sym_services;
223 while (NULL != list_temp) {
224 if (index > (numInstances - 1))
225 break;
226
227 pInstances[index] = SalList_getObject(list_temp);
228 list_temp = SalList_next(list_temp);
229 index++;
230 }
231 }
232 free(pAdfInsts, M_QAT);
233
234 return status;
235}
236
237/**
238 ******************************************************************************
239 * @ingroup SalCtrl
240 *****************************************************************************/
241CpaStatus
242cpaGetNumInstances(const CpaAccelerationServiceType accelerationServiceType,
243 Cpa16U *pNumInstances)
244{
245 switch (accelerationServiceType) {
246 case CPA_ACC_SVC_TYPE_CRYPTO_ASYM:
247 case CPA_ACC_SVC_TYPE_CRYPTO_SYM:
248 return Lac_GetSingleCyNumInstances(accelerationServiceType,
249 pNumInstances);
250 case CPA_ACC_SVC_TYPE_CRYPTO:
251 return cpaCyGetNumInstances(pNumInstances);
252 case CPA_ACC_SVC_TYPE_DATA_COMPRESSION:
253 return cpaDcGetNumInstances(pNumInstances);
254
255 default:
256 QAT_UTILS_LOG("Invalid service type\n");
257 *pNumInstances = 0;
258 return CPA_STATUS_INVALID_PARAM;
259 }
260}
261
262/**
263 ******************************************************************************
264 * @ingroup SalCtrl
265 *****************************************************************************/
266CpaStatus
267cpaGetInstances(const CpaAccelerationServiceType accelerationServiceType,
268 Cpa16U numInstances,
269 CpaInstanceHandle *pInstances)
270{
271 switch (accelerationServiceType) {
272 case CPA_ACC_SVC_TYPE_CRYPTO_ASYM:
273 case CPA_ACC_SVC_TYPE_CRYPTO_SYM:
274 return Lac_GetSingleCyInstances(accelerationServiceType,
275 numInstances,
276 pInstances);
277
278 case CPA_ACC_SVC_TYPE_CRYPTO:
279 return cpaCyGetInstances(numInstances, pInstances);
280 case CPA_ACC_SVC_TYPE_DATA_COMPRESSION:
281 return cpaDcGetInstances(numInstances, pInstances);
282
283 default:
284 QAT_UTILS_LOG("Invalid service type\n");
285 return CPA_STATUS_INVALID_PARAM;
286 }
287}
4
5/**
6 *****************************************************************************
7 * @file sal_get_instances.c
8 *
9 * @defgroup SalCtrl Service Access Layer Controller
10 *
11 * @ingroup SalCtrl
12 *
13 * @description
14 * This file contains the main function to get SAL instances.
15 *
16 *****************************************************************************/
17
18/*
19*******************************************************************************
20* Include public/global header files
21*******************************************************************************
22*/
23
24/* QAT-API includes */
25#include "cpa.h"
26#include "cpa_cy_common.h"
27#include "cpa_cy_im.h"
28#include "cpa_dc.h"
29
30/* ADF includes */
31#include "icp_accel_devices.h"
32#include "icp_adf_accel_mgr.h"
33
34/* SAL includes */
35#include "lac_mem.h"
36#include "lac_list.h"
37#include "lac_sal_types.h"
38
39/**
40 ******************************************************************************
41 * @ingroup SalCtrl
42 * @description
43 * Get either sym or asym instance number
44 *****************************************************************************/
45static CpaStatus
46Lac_GetSingleCyNumInstances(
47 const CpaAccelerationServiceType accelerationServiceType,
48 Cpa16U *pNumInstances)
49{
50 CpaStatus status = CPA_STATUS_SUCCESS;
51 icp_accel_dev_t **pAdfInsts = NULL;
52 icp_accel_dev_t *dev_addr = NULL;
53 sal_t *base_addr = NULL;
54 sal_list_t *list_temp = NULL;
55 Cpa16U num_accel_dev = 0;
56 Cpa16U num_inst = 0;
57 Cpa16U i = 0;
58 Cpa32U accel_capability = 0;
59 char *service = NULL;
60
61 LAC_CHECK_NULL_PARAM(pNumInstances);
62 *pNumInstances = 0;
63
64 switch (accelerationServiceType) {
65 case CPA_ACC_SVC_TYPE_CRYPTO_ASYM:
66 accel_capability = ICP_ACCEL_CAPABILITIES_CRYPTO_ASYMMETRIC;
67 service = "asym";
68 break;
69
70 case CPA_ACC_SVC_TYPE_CRYPTO_SYM:
71 accel_capability = ICP_ACCEL_CAPABILITIES_CRYPTO_SYMMETRIC;
72 service = "sym";
73 break;
74
75 default:
76 QAT_UTILS_LOG("Invalid service type\n");
77 return CPA_STATUS_INVALID_PARAM;
78 }
79
80 /* Get the number of accel_dev in the system */
81 status = icp_amgr_getNumInstances(&num_accel_dev);
82 LAC_CHECK_STATUS(status);
83
84 /* Allocate memory to store addr of accel_devs */
85 pAdfInsts = malloc(num_accel_dev * sizeof(icp_accel_dev_t *),
86 M_QAT,
87 M_WAITOK | M_ZERO);
88 if (NULL == pAdfInsts) {
89 QAT_UTILS_LOG("Failed to allocate dev instance memory\n");
90 return CPA_STATUS_RESOURCE;
91 }
92
93 num_accel_dev = 0;
94 status = icp_amgr_getAllAccelDevByCapabilities(accel_capability,
95 pAdfInsts,
96 &num_accel_dev);
97 if (CPA_STATUS_SUCCESS != status) {
98 QAT_UTILS_LOG("No support for service %s\n", service);
99 free(pAdfInsts, M_QAT);
100 return status;
101 }
102
103 for (i = 0; i < num_accel_dev; i++) {
104 dev_addr = pAdfInsts[i];
105 if (NULL == dev_addr || NULL == dev_addr->pSalHandle) {
106 continue;
107 }
108 base_addr = dev_addr->pSalHandle;
109
110 if (CPA_ACC_SVC_TYPE_CRYPTO_ASYM == accelerationServiceType) {
111 list_temp = base_addr->asym_services;
112 } else {
113 list_temp = base_addr->sym_services;
114 }
115 while (NULL != list_temp) {
116 num_inst++;
117 list_temp = SalList_next(list_temp);
118 }
119 }
120
121 *pNumInstances = num_inst;
122 free(pAdfInsts, M_QAT);
123
124 return status;
125}
126
127/**
128 ******************************************************************************
129 * @ingroup SalCtrl
130 * @description
131 * Get either sym or asym instance
132 *****************************************************************************/
133static CpaStatus
134Lac_GetSingleCyInstances(
135 const CpaAccelerationServiceType accelerationServiceType,
136 Cpa16U numInstances,
137 CpaInstanceHandle *pInstances)
138{
139 CpaStatus status = CPA_STATUS_SUCCESS;
140 icp_accel_dev_t **pAdfInsts = NULL;
141 icp_accel_dev_t *dev_addr = NULL;
142 sal_t *base_addr = NULL;
143 sal_list_t *list_temp = NULL;
144 Cpa16U num_accel_dev = 0;
145 Cpa16U num_allocated_instances = 0;
146 Cpa16U index = 0;
147 Cpa16U i = 0;
148 Cpa32U accel_capability = 0;
149 char *service = NULL;
150
151 LAC_CHECK_NULL_PARAM(pInstances);
152 if (0 == numInstances) {
153 QAT_UTILS_LOG("NumInstances is 0\n");
154 return CPA_STATUS_INVALID_PARAM;
155 }
156
157 switch (accelerationServiceType) {
158 case CPA_ACC_SVC_TYPE_CRYPTO_ASYM:
159 accel_capability = ICP_ACCEL_CAPABILITIES_CRYPTO_ASYMMETRIC;
160 service = "asym";
161 break;
162
163 case CPA_ACC_SVC_TYPE_CRYPTO_SYM:
164 accel_capability = ICP_ACCEL_CAPABILITIES_CRYPTO_SYMMETRIC;
165 service = "sym";
166 break;
167 default:
168 QAT_UTILS_LOG("Invalid service type\n");
169 return CPA_STATUS_INVALID_PARAM;
170 }
171
172 /* Get the number of instances */
173 status = cpaGetNumInstances(accelerationServiceType,
174 &num_allocated_instances);
175 if (CPA_STATUS_SUCCESS != status) {
176 return status;
177 }
178
179 if (numInstances > num_allocated_instances) {
180 QAT_UTILS_LOG("Only %d instances available\n",
181 num_allocated_instances);
182 return CPA_STATUS_RESOURCE;
183 }
184
185 /* Get the number of accel devices in the system */
186 status = icp_amgr_getNumInstances(&num_accel_dev);
187 LAC_CHECK_STATUS(status);
188
189 /* Allocate memory to store addr of accel_devs */
190 pAdfInsts = malloc(num_accel_dev * sizeof(icp_accel_dev_t *),
191 M_QAT,
192 M_WAITOK | M_ZERO);
193 if (NULL == pAdfInsts) {
194 QAT_UTILS_LOG("Failed to allocate dev instance memory\n");
195 return CPA_STATUS_RESOURCE;
196 }
197
198 num_accel_dev = 0;
199 status = icp_amgr_getAllAccelDevByCapabilities(accel_capability,
200 pAdfInsts,
201 &num_accel_dev);
202 if (CPA_STATUS_SUCCESS != status) {
203 QAT_UTILS_LOG("No support for service %s\n", service);
204 free(pAdfInsts, M_QAT);
205 return status;
206 }
207
208 for (i = 0; i < num_accel_dev; i++) {
209 dev_addr = pAdfInsts[i];
210 /* Note dev_addr cannot be NULL here as numInstances = 0
211 * is not valid and if dev_addr = NULL then index = 0 (which
212 * is less than numInstances and status is set to _RESOURCE
213 * above)
214 */
215 base_addr = dev_addr->pSalHandle;
216 if (NULL == base_addr) {
217 continue;
218 }
219
220 if (CPA_ACC_SVC_TYPE_CRYPTO_ASYM == accelerationServiceType)
221 list_temp = base_addr->asym_services;
222 else
223 list_temp = base_addr->sym_services;
224 while (NULL != list_temp) {
225 if (index > (numInstances - 1))
226 break;
227
228 pInstances[index] = SalList_getObject(list_temp);
229 list_temp = SalList_next(list_temp);
230 index++;
231 }
232 }
233 free(pAdfInsts, M_QAT);
234
235 return status;
236}
237
238/**
239 ******************************************************************************
240 * @ingroup SalCtrl
241 *****************************************************************************/
242CpaStatus
243cpaGetNumInstances(const CpaAccelerationServiceType accelerationServiceType,
244 Cpa16U *pNumInstances)
245{
246 switch (accelerationServiceType) {
247 case CPA_ACC_SVC_TYPE_CRYPTO_ASYM:
248 case CPA_ACC_SVC_TYPE_CRYPTO_SYM:
249 return Lac_GetSingleCyNumInstances(accelerationServiceType,
250 pNumInstances);
251 case CPA_ACC_SVC_TYPE_CRYPTO:
252 return cpaCyGetNumInstances(pNumInstances);
253 case CPA_ACC_SVC_TYPE_DATA_COMPRESSION:
254 return cpaDcGetNumInstances(pNumInstances);
255
256 default:
257 QAT_UTILS_LOG("Invalid service type\n");
258 *pNumInstances = 0;
259 return CPA_STATUS_INVALID_PARAM;
260 }
261}
262
263/**
264 ******************************************************************************
265 * @ingroup SalCtrl
266 *****************************************************************************/
267CpaStatus
268cpaGetInstances(const CpaAccelerationServiceType accelerationServiceType,
269 Cpa16U numInstances,
270 CpaInstanceHandle *pInstances)
271{
272 switch (accelerationServiceType) {
273 case CPA_ACC_SVC_TYPE_CRYPTO_ASYM:
274 case CPA_ACC_SVC_TYPE_CRYPTO_SYM:
275 return Lac_GetSingleCyInstances(accelerationServiceType,
276 numInstances,
277 pInstances);
278
279 case CPA_ACC_SVC_TYPE_CRYPTO:
280 return cpaCyGetInstances(numInstances, pInstances);
281 case CPA_ACC_SVC_TYPE_DATA_COMPRESSION:
282 return cpaDcGetInstances(numInstances, pInstances);
283
284 default:
285 QAT_UTILS_LOG("Invalid service type\n");
286 return CPA_STATUS_INVALID_PARAM;
287 }
288}