xref: /freebsd/sys/dev/qat/qat_common/adf_cfg_section.c (revision 5956d97f4b3204318ceb6aa9c77bd0bc6ea87a41)
1 /* SPDX-License-Identifier: BSD-3-Clause */
2 /* Copyright(c) 2007-2022 Intel Corporation */
3 /* $FreeBSD$ */
4 #include "adf_cfg_instance.h"
5 #include "adf_cfg_device.h"
6 #include "adf_cfg_section.h"
7 
8 static bool
9 adf_cfg_is_svc_enabled(struct adf_accel_dev *accel_dev, const u8 svc)
10 {
11 	int ring_pair_index = 0;
12 	u8 serv_type = NA;
13 	struct adf_hw_device_data *hw_data = GET_HW_DATA(accel_dev);
14 
15 	for (ring_pair_index = 0; ring_pair_index < ADF_CFG_NUM_SERVICES;
16 	     ring_pair_index++) {
17 		serv_type =
18 		    GET_SRV_TYPE(hw_data->ring_to_svc_map, ring_pair_index);
19 		if (serv_type == svc)
20 			return true;
21 	}
22 	return false;
23 }
24 
25 static int
26 adf_cfg_set_core_number_for_instance(struct adf_accel_dev *accel_dev,
27 				     const char *sec_name,
28 				     const char *inst_name,
29 				     int process_num,
30 				     unsigned long *core_number)
31 {
32 	char *core_val = NULL;
33 	char *pos = NULL;
34 	char **tokens = NULL;
35 	int token_index = 0;
36 	int core_arr_index = 0;
37 	int i = 0;
38 	int ret = EFAULT;
39 	unsigned long *core_num_arr = NULL;
40 	unsigned long core_num;
41 	unsigned long start, end;
42 
43 	/* do memory allocation */
44 	core_val =
45 	    malloc(ADF_CFG_MAX_VAL_LEN_IN_BYTES, M_QAT, M_WAITOK | M_ZERO);
46 
47 	tokens = malloc(sizeof(char *) * ADF_CFG_MAX_TOKENS,
48 			M_QAT,
49 			M_WAITOK | M_ZERO);
50 
51 	for (i = 0; i < ADF_CFG_MAX_TOKENS; i++) {
52 		tokens[i] =
53 		    malloc(ADF_CFG_MAX_TOKEN_LEN, M_QAT, M_WAITOK | M_ZERO);
54 	}
55 
56 	core_num_arr = malloc(sizeof(unsigned long) * ADF_CFG_MAX_CORE_NUM,
57 			      M_QAT,
58 			      M_WAITOK | M_ZERO);
59 
60 	/* parse the core_val */
61 	ret = EFAULT;
62 	if (adf_cfg_get_param_value(accel_dev, sec_name, inst_name, core_val))
63 		goto failed;
64 
65 	pos = strchr(core_val, ',');
66 	while (pos) {
67 		pos[0] = '\0';
68 		strlcpy(tokens[token_index++], core_val, ADF_CFG_MAX_TOKEN_LEN);
69 		strlcpy(core_val, pos + 1, ADF_CFG_MAX_VAL_LEN_IN_BYTES);
70 		pos = strchr(core_val, ',');
71 		if (!pos)
72 			strlcpy(tokens[token_index++],
73 				core_val,
74 				ADF_CFG_MAX_VAL_LEN_IN_BYTES);
75 	}
76 
77 	/* in case there is only N-M */
78 	if (token_index == 0)
79 		strlcpy(tokens[token_index++],
80 			core_val,
81 			ADF_CFG_MAX_VAL_LEN_IN_BYTES);
82 
83 	/* parse the tokens such as N-M */
84 	for (i = 0; i < token_index; i++) {
85 		pos = strchr(tokens[i], '-');
86 		if (pos) {
87 			pos[0] = '\0';
88 			ret = compat_strtoul(tokens[i], 10, &start);
89 			if (ret)
90 				goto failed;
91 			ret = compat_strtoul(pos + 1, 10, &end);
92 			if (ret)
93 				goto failed;
94 			if (start > end) {
95 				ret = EFAULT;
96 				goto failed;
97 			}
98 			for (core_num = start; core_num < end + 1; core_num++)
99 				core_num_arr[core_arr_index++] = core_num;
100 		} else {
101 			ret = compat_strtoul(tokens[i], 10, &core_num);
102 			if (ret)
103 				goto failed;
104 			core_num_arr[core_arr_index++] = core_num;
105 		}
106 	}
107 
108 	if (core_arr_index == 0) {
109 		ret = compat_strtoul(core_val, 10, &core_num);
110 		if (ret)
111 			goto failed;
112 		else
113 			core_num_arr[core_arr_index++] = core_num;
114 	}
115 
116 	*core_number = core_num_arr[process_num % core_arr_index];
117 	ret = 0;
118 failed:
119 	free(core_val, M_QAT);
120 	if (tokens) {
121 		for (i = 0; i < ADF_CFG_MAX_TOKENS; i++)
122 			free(tokens[i], M_QAT);
123 		free(tokens, M_QAT);
124 	}
125 	free(core_num_arr, M_QAT);
126 
127 	if (ret)
128 		device_printf(GET_DEV(accel_dev),
129 			      "Get core number failed with error %d\n",
130 			      ret);
131 	return ret;
132 }
133 
134 static int
135 adf_cfg_set_value(struct adf_accel_dev *accel_dev,
136 		  const char *sec,
137 		  const char *key,
138 		  unsigned long *value)
139 {
140 	char *val = NULL;
141 	int ret = EFAULT;
142 
143 	val = malloc(ADF_CFG_MAX_VAL_LEN_IN_BYTES, M_QAT, M_WAITOK | M_ZERO);
144 
145 	if (adf_cfg_get_param_value(accel_dev, sec, key, val))
146 		goto out;
147 
148 	/* as the key type can be either ADF_DEC or ADF_HEX */
149 	if (compat_strtoul(val, 10, value) && compat_strtoul(val, 16, value))
150 		goto out;
151 
152 	ret = 0;
153 out:
154 	free(val, M_QAT);
155 	return ret;
156 }
157 
158 static void
159 adf_cfg_add_cy_inst_info(struct adf_accel_dev *accel_dev,
160 			 struct adf_cfg_instance *crypto_inst,
161 			 const char *derived_sec,
162 			 int inst_index)
163 {
164 	char *key = NULL;
165 	unsigned long bank_number = 0;
166 	unsigned long ring_number = 0;
167 	unsigned long asym_req = 0;
168 	unsigned long sym_req = 0;
169 
170 	key = malloc(ADF_CFG_MAX_KEY_LEN_IN_BYTES, M_QAT, M_WAITOK | M_ZERO);
171 
172 	snprintf(key,
173 		 ADF_CFG_MAX_KEY_LEN_IN_BYTES,
174 		 ADF_CY_BANK_NUM_FORMAT,
175 		 inst_index);
176 	bank_number = crypto_inst->bundle;
177 	adf_cfg_add_key_value_param(
178 	    accel_dev, derived_sec, key, (void *)&bank_number, ADF_DEC);
179 
180 	snprintf(key,
181 		 ADF_CFG_MAX_KEY_LEN_IN_BYTES,
182 		 ADF_CY_ASYM_TX_FORMAT,
183 		 inst_index);
184 	ring_number = crypto_inst->asym_tx;
185 	adf_cfg_add_key_value_param(
186 	    accel_dev, derived_sec, key, (void *)&ring_number, ADF_DEC);
187 
188 	snprintf(key,
189 		 ADF_CFG_MAX_KEY_LEN_IN_BYTES,
190 		 ADF_CY_SYM_TX_FORMAT,
191 		 inst_index);
192 	ring_number = crypto_inst->sym_tx;
193 	adf_cfg_add_key_value_param(
194 	    accel_dev, derived_sec, key, (void *)&ring_number, ADF_DEC);
195 
196 	snprintf(key,
197 		 ADF_CFG_MAX_KEY_LEN_IN_BYTES,
198 		 ADF_CY_ASYM_RX_FORMAT,
199 		 inst_index);
200 	ring_number = crypto_inst->asym_rx;
201 	adf_cfg_add_key_value_param(
202 	    accel_dev, derived_sec, key, (void *)&ring_number, ADF_DEC);
203 
204 	snprintf(key,
205 		 ADF_CFG_MAX_KEY_LEN_IN_BYTES,
206 		 ADF_CY_SYM_RX_FORMAT,
207 		 inst_index);
208 	ring_number = crypto_inst->sym_rx;
209 	adf_cfg_add_key_value_param(
210 	    accel_dev, derived_sec, key, (void *)&ring_number, ADF_DEC);
211 
212 	strlcpy(key, ADF_CY_RING_ASYM_SIZE, ADF_CFG_MAX_KEY_LEN_IN_BYTES);
213 	if (adf_cfg_set_value(accel_dev, ADF_GENERAL_SEC, key, &asym_req))
214 		asym_req = ADF_CFG_DEF_CY_RING_ASYM_SIZE;
215 
216 	snprintf(key,
217 		 ADF_CFG_MAX_KEY_LEN_IN_BYTES,
218 		 ADF_CY_RING_ASYM_SIZE_FORMAT,
219 		 inst_index);
220 	adf_cfg_add_key_value_param(
221 	    accel_dev, derived_sec, key, (void *)&asym_req, ADF_DEC);
222 
223 	strlcpy(key, ADF_CY_RING_SYM_SIZE, ADF_CFG_MAX_KEY_LEN_IN_BYTES);
224 	if (adf_cfg_set_value(accel_dev, ADF_GENERAL_SEC, key, &sym_req))
225 		sym_req = ADF_CFG_DEF_CY_RING_SYM_SIZE;
226 
227 	snprintf(key,
228 		 ADF_CFG_MAX_KEY_LEN_IN_BYTES,
229 		 ADF_CY_RING_SYM_SIZE_FORMAT,
230 		 inst_index);
231 	adf_cfg_add_key_value_param(
232 	    accel_dev, derived_sec, key, (void *)&sym_req, ADF_DEC);
233 
234 	free(key, M_QAT);
235 }
236 
237 static void
238 adf_cfg_add_dc_inst_info(struct adf_accel_dev *accel_dev,
239 			 struct adf_cfg_instance *dc_inst,
240 			 const char *derived_sec,
241 			 int inst_index)
242 {
243 	char *key = NULL;
244 	unsigned long bank_number = 0;
245 	unsigned long ring_number = 0;
246 	unsigned long dc_req = 0;
247 
248 	key = malloc(ADF_CFG_MAX_KEY_LEN_IN_BYTES, M_QAT, M_WAITOK | M_ZERO);
249 
250 	snprintf(key, ADF_CFG_MAX_STR_LEN, ADF_DC_BANK_NUM_FORMAT, inst_index);
251 	bank_number = dc_inst->bundle;
252 	adf_cfg_add_key_value_param(
253 	    accel_dev, derived_sec, key, (void *)&bank_number, ADF_DEC);
254 
255 	snprintf(key, ADF_CFG_MAX_STR_LEN, ADF_DC_TX_FORMAT, inst_index);
256 	ring_number = dc_inst->dc_tx;
257 	adf_cfg_add_key_value_param(
258 	    accel_dev, derived_sec, key, (void *)&ring_number, ADF_DEC);
259 
260 	snprintf(key, ADF_CFG_MAX_STR_LEN, ADF_DC_RX_FORMAT, inst_index);
261 	ring_number = dc_inst->dc_rx;
262 	adf_cfg_add_key_value_param(
263 	    accel_dev, derived_sec, key, (void *)&ring_number, ADF_DEC);
264 
265 	strlcpy(key, ADF_DC_RING_SIZE, ADF_CFG_MAX_KEY_LEN_IN_BYTES);
266 	if (adf_cfg_set_value(accel_dev, ADF_GENERAL_SEC, key, &dc_req))
267 		dc_req = ADF_CFG_DEF_DC_RING_SIZE;
268 
269 	snprintf(key, ADF_CFG_MAX_STR_LEN, ADF_DC_RING_SIZE_FORMAT, inst_index);
270 	adf_cfg_add_key_value_param(
271 	    accel_dev, derived_sec, key, (void *)&dc_req, ADF_DEC);
272 
273 	free(key, M_QAT);
274 }
275 
276 static void
277 adf_cfg_add_asym_inst_info(struct adf_accel_dev *accel_dev,
278 			   struct adf_cfg_instance *asym_inst,
279 			   const char *derived_sec,
280 			   int inst_index)
281 {
282 	char *key = NULL;
283 	unsigned long bank_number = 0;
284 	unsigned long ring_number = 0;
285 	unsigned long asym_req = 0;
286 
287 	key = malloc(ADF_CFG_MAX_KEY_LEN_IN_BYTES, M_QAT, M_WAITOK | M_ZERO);
288 
289 	snprintf(key,
290 		 ADF_CFG_MAX_KEY_LEN_IN_BYTES,
291 		 ADF_CY_BANK_NUM_FORMAT,
292 		 inst_index);
293 	bank_number = asym_inst->bundle;
294 	adf_cfg_add_key_value_param(
295 	    accel_dev, derived_sec, key, (void *)&bank_number, ADF_DEC);
296 
297 	snprintf(key,
298 		 ADF_CFG_MAX_KEY_LEN_IN_BYTES,
299 		 ADF_CY_ASYM_TX_FORMAT,
300 		 inst_index);
301 	ring_number = asym_inst->asym_tx;
302 	adf_cfg_add_key_value_param(
303 	    accel_dev, derived_sec, key, (void *)&ring_number, ADF_DEC);
304 
305 	snprintf(key,
306 		 ADF_CFG_MAX_KEY_LEN_IN_BYTES,
307 		 ADF_CY_ASYM_RX_FORMAT,
308 		 inst_index);
309 	ring_number = asym_inst->asym_rx;
310 	adf_cfg_add_key_value_param(
311 	    accel_dev, derived_sec, key, (void *)&ring_number, ADF_DEC);
312 
313 	strlcpy(key, ADF_CY_RING_ASYM_SIZE, ADF_CFG_MAX_KEY_LEN_IN_BYTES);
314 	if (adf_cfg_set_value(accel_dev, ADF_GENERAL_SEC, key, &asym_req))
315 		asym_req = ADF_CFG_DEF_CY_RING_ASYM_SIZE;
316 
317 	snprintf(key,
318 		 ADF_CFG_MAX_KEY_LEN_IN_BYTES,
319 		 ADF_CY_RING_ASYM_SIZE_FORMAT,
320 		 inst_index);
321 	adf_cfg_add_key_value_param(
322 	    accel_dev, derived_sec, key, (void *)&asym_req, ADF_DEC);
323 
324 	free(key, M_QAT);
325 }
326 
327 static void
328 adf_cfg_add_sym_inst_info(struct adf_accel_dev *accel_dev,
329 			  struct adf_cfg_instance *sym_inst,
330 			  const char *derived_sec,
331 			  int inst_index)
332 {
333 	char *key = NULL;
334 	unsigned long bank_number = 0;
335 	unsigned long ring_number = 0;
336 	unsigned long sym_req = 0;
337 
338 	key = malloc(ADF_CFG_MAX_KEY_LEN_IN_BYTES, M_QAT, M_WAITOK | M_ZERO);
339 
340 	snprintf(key,
341 		 ADF_CFG_MAX_KEY_LEN_IN_BYTES,
342 		 ADF_CY_BANK_NUM_FORMAT,
343 		 inst_index);
344 	bank_number = sym_inst->bundle;
345 	adf_cfg_add_key_value_param(
346 	    accel_dev, derived_sec, key, (void *)&bank_number, ADF_DEC);
347 
348 	snprintf(key,
349 		 ADF_CFG_MAX_KEY_LEN_IN_BYTES,
350 		 ADF_CY_SYM_TX_FORMAT,
351 		 inst_index);
352 	ring_number = sym_inst->sym_tx;
353 	adf_cfg_add_key_value_param(
354 	    accel_dev, derived_sec, key, (void *)&ring_number, ADF_DEC);
355 
356 	snprintf(key,
357 		 ADF_CFG_MAX_KEY_LEN_IN_BYTES,
358 		 ADF_CY_SYM_RX_FORMAT,
359 		 inst_index);
360 	ring_number = sym_inst->sym_rx;
361 	adf_cfg_add_key_value_param(
362 	    accel_dev, derived_sec, key, (void *)&ring_number, ADF_DEC);
363 
364 	strlcpy(key, ADF_CY_RING_SYM_SIZE, ADF_CFG_MAX_KEY_LEN_IN_BYTES);
365 	if (adf_cfg_set_value(accel_dev, ADF_GENERAL_SEC, key, &sym_req))
366 		sym_req = ADF_CFG_DEF_CY_RING_SYM_SIZE;
367 
368 	snprintf(key,
369 		 ADF_CFG_MAX_KEY_LEN_IN_BYTES,
370 		 ADF_CY_RING_SYM_SIZE_FORMAT,
371 		 inst_index);
372 	adf_cfg_add_key_value_param(
373 	    accel_dev, derived_sec, key, (void *)&sym_req, ADF_DEC);
374 
375 	free(key, M_QAT);
376 }
377 
378 static int
379 adf_cfg_section_copy(struct adf_accel_dev *accel_dev,
380 		     const char *processed_sec,
381 		     const char *derived_sec)
382 {
383 	unsigned long val = 0;
384 	struct list_head *list;
385 	struct adf_cfg_section *sec_process =
386 	    adf_cfg_sec_find(accel_dev, processed_sec);
387 	if (!sec_process)
388 		return EFAULT;
389 
390 	list_for_each(list, &sec_process->param_head)
391 	{
392 		struct adf_cfg_key_val *ptr =
393 		    list_entry(list, struct adf_cfg_key_val, list);
394 
395 		/*
396 		 * ignore CoreAffinity since it will be generated later, and
397 		 * there is no need to keep NumProcesses and LimitDevAccess.
398 		 */
399 		if (strstr(ptr->key, ADF_ETRMGR_CORE_AFFINITY) ||
400 		    strstr(ptr->key, ADF_NUM_PROCESSES) ||
401 		    strstr(ptr->key, ADF_LIMIT_DEV_ACCESS))
402 			continue;
403 
404 		if (ptr->type == ADF_DEC) {
405 			if (!compat_strtoul(ptr->val, 10, &val))
406 				adf_cfg_add_key_value_param(accel_dev,
407 							    derived_sec,
408 							    ptr->key,
409 							    (void *)&val,
410 							    ptr->type);
411 		} else if (ptr->type == ADF_STR) {
412 			adf_cfg_add_key_value_param(accel_dev,
413 						    derived_sec,
414 						    ptr->key,
415 						    (void *)ptr->val,
416 						    ptr->type);
417 		} else if (ptr->type == ADF_HEX) {
418 			if (!compat_strtoul(ptr->val, 16, &val))
419 				adf_cfg_add_key_value_param(accel_dev,
420 							    derived_sec,
421 							    ptr->key,
422 							    (void *)val,
423 							    ptr->type);
424 		}
425 	}
426 	return 0;
427 }
428 
429 static int
430 adf_cfg_create_rings_entries_for_cy_inst(struct adf_accel_dev *accel_dev,
431 					 const char *processed_sec,
432 					 const char *derived_sec,
433 					 int process_num,
434 					 enum adf_cfg_service_type serv_type)
435 {
436 	int i = 0;
437 	int ret = EFAULT;
438 	unsigned long num_inst = 0, num_dc_inst = 0;
439 	unsigned long core_number = 0;
440 	unsigned long polling_mode = 0;
441 	struct adf_cfg_instance *crypto_inst = NULL;
442 
443 	char *key = NULL;
444 	char *val = NULL;
445 
446 	key = malloc(ADF_CFG_MAX_KEY_LEN_IN_BYTES, M_QAT, M_WAITOK | M_ZERO);
447 
448 	val = malloc(ADF_CFG_MAX_VAL_LEN_IN_BYTES, M_QAT, M_WAITOK | M_ZERO);
449 
450 	snprintf(key, ADF_CFG_MAX_KEY_LEN_IN_BYTES, ADF_SERVICES_ENABLED);
451 	if (adf_cfg_get_param_value(accel_dev, ADF_GENERAL_SEC, key, val))
452 		goto failed;
453 	if ((!strncmp(val, ADF_CFG_CY, ADF_CFG_MAX_VAL_LEN_IN_BYTES)) ||
454 	    (!strncmp(val, ADF_CFG_ASYM, ADF_CFG_MAX_VAL_LEN_IN_BYTES)) ||
455 	    (!strncmp(val, ADF_CFG_SYM, ADF_CFG_MAX_VAL_LEN_IN_BYTES))) {
456 		strlcpy(key, ADF_NUM_DC, ADF_CFG_MAX_KEY_LEN_IN_BYTES);
457 		if (adf_cfg_set_value(
458 			accel_dev, processed_sec, key, &num_dc_inst))
459 			goto failed;
460 		if (num_dc_inst > 0) {
461 			device_printf(
462 			    GET_DEV(accel_dev),
463 			    "NumDcInstances > 0,when CY only is enabled\n");
464 			goto failed;
465 		}
466 	}
467 	ret = EFAULT;
468 
469 	strlcpy(key, ADF_NUM_CY, ADF_CFG_MAX_KEY_LEN_IN_BYTES);
470 	if (adf_cfg_set_value(accel_dev, processed_sec, key, &num_inst))
471 		goto failed;
472 
473 	crypto_inst = malloc(sizeof(*crypto_inst), M_QAT, M_WAITOK | M_ZERO);
474 
475 	for (i = 0; i < num_inst; i++) {
476 		memset(crypto_inst, 0, sizeof(*crypto_inst));
477 		crypto_inst->stype = serv_type;
478 		snprintf(key,
479 			 ADF_CFG_MAX_KEY_LEN_IN_BYTES,
480 			 ADF_CY_CORE_AFFINITY_FORMAT,
481 			 i);
482 		if (adf_cfg_set_core_number_for_instance(accel_dev,
483 							 processed_sec,
484 							 key,
485 							 process_num,
486 							 &core_number))
487 			goto failed;
488 
489 		if (strcmp(processed_sec, ADF_KERNEL_SEC) &&
490 		    strcmp(processed_sec, ADF_KERNEL_SAL_SEC))
491 			adf_cfg_add_key_value_param(accel_dev,
492 						    derived_sec,
493 						    key,
494 						    (void *)&core_number,
495 						    ADF_DEC);
496 
497 		snprintf(key,
498 			 ADF_CFG_MAX_KEY_LEN_IN_BYTES,
499 			 ADF_CY_NAME_FORMAT,
500 			 i);
501 		if (adf_cfg_get_param_value(accel_dev, processed_sec, key, val))
502 			goto failed;
503 
504 		strlcpy(crypto_inst->name, val, sizeof(crypto_inst->name));
505 
506 		snprintf(key,
507 			 ADF_CFG_MAX_KEY_LEN_IN_BYTES,
508 			 ADF_CY_POLL_MODE_FORMAT,
509 			 i);
510 		if (adf_cfg_set_value(
511 			accel_dev, processed_sec, key, &polling_mode))
512 			goto failed;
513 
514 		crypto_inst->polling_mode = polling_mode;
515 		CPU_ZERO(&crypto_inst->affinity_mask);
516 		CPU_SET(core_number, &crypto_inst->affinity_mask);
517 
518 		if (adf_cfg_get_ring_pairs(accel_dev->cfg->dev,
519 					   crypto_inst,
520 					   derived_sec,
521 					   accel_dev))
522 			goto failed;
523 
524 		switch (serv_type) {
525 		case CRYPTO:
526 			adf_cfg_add_cy_inst_info(accel_dev,
527 						 crypto_inst,
528 						 derived_sec,
529 						 i);
530 			break;
531 		case ASYM:
532 			adf_cfg_add_asym_inst_info(accel_dev,
533 						   crypto_inst,
534 						   derived_sec,
535 						   i);
536 			break;
537 		case SYM:
538 			adf_cfg_add_sym_inst_info(accel_dev,
539 						  crypto_inst,
540 						  derived_sec,
541 						  i);
542 			break;
543 		default:
544 			pr_err("unknown crypto instance type %d.\n", serv_type);
545 			goto failed;
546 		}
547 	}
548 
549 	ret = 0;
550 failed:
551 	free(crypto_inst, M_QAT);
552 	free(val, M_QAT);
553 	free(key, M_QAT);
554 
555 	if (ret)
556 		device_printf(GET_DEV(accel_dev),
557 			      "Failed to create rings for cy\n");
558 
559 	return ret;
560 }
561 
562 static int
563 adf_cfg_create_rings_entries_for_dc_inst(struct adf_accel_dev *accel_dev,
564 					 const char *processed_sec,
565 					 const char *derived_sec,
566 					 int process_num)
567 {
568 	int i = 0;
569 	int ret = EFAULT;
570 	unsigned long num_inst = 0, num_cy_inst = 0;
571 	unsigned long core_number = 0;
572 	unsigned long polling_mode = 0;
573 	struct adf_cfg_instance *dc_inst = NULL;
574 
575 	char *key = NULL;
576 	char *val = NULL;
577 
578 	key = malloc(ADF_CFG_MAX_KEY_LEN_IN_BYTES, M_QAT, M_WAITOK | M_ZERO);
579 
580 	val = malloc(ADF_CFG_MAX_VAL_LEN_IN_BYTES, M_QAT, M_WAITOK | M_ZERO);
581 
582 	ret = EFAULT;
583 
584 	snprintf(key, ADF_CFG_MAX_STR_LEN, ADF_SERVICES_ENABLED);
585 	if (adf_cfg_get_param_value(accel_dev, ADF_GENERAL_SEC, key, val))
586 		goto failed;
587 
588 	if (!strncmp(val, ADF_CFG_DC, ADF_CFG_MAX_VAL_LEN_IN_BYTES)) {
589 		strlcpy(key, ADF_NUM_CY, ADF_CFG_MAX_KEY_LEN_IN_BYTES);
590 		if (adf_cfg_set_value(
591 			accel_dev, processed_sec, key, &num_cy_inst))
592 			goto failed;
593 		if (num_cy_inst > 0) {
594 			device_printf(
595 			    GET_DEV(accel_dev),
596 			    "NumCyInstances > 0,when DC only is enabled\n");
597 			goto failed;
598 		}
599 	}
600 
601 	strlcpy(key, ADF_NUM_DC, ADF_CFG_MAX_KEY_LEN_IN_BYTES);
602 	if (adf_cfg_set_value(accel_dev, processed_sec, key, &num_inst))
603 		goto failed;
604 
605 	dc_inst = malloc(sizeof(*dc_inst), M_QAT, M_WAITOK | M_ZERO);
606 
607 	for (i = 0; i < num_inst; i++) {
608 		memset(dc_inst, 0, sizeof(*dc_inst));
609 		dc_inst->stype = COMP;
610 		snprintf(key,
611 			 ADF_CFG_MAX_STR_LEN,
612 			 ADF_DC_CORE_AFFINITY_FORMAT,
613 			 i);
614 
615 		if (adf_cfg_set_core_number_for_instance(accel_dev,
616 							 processed_sec,
617 							 key,
618 							 process_num,
619 							 &core_number))
620 			goto failed;
621 
622 		if (strcmp(processed_sec, ADF_KERNEL_SEC) &&
623 		    strcmp(processed_sec, ADF_KERNEL_SAL_SEC)) {
624 			adf_cfg_add_key_value_param(accel_dev,
625 						    derived_sec,
626 						    key,
627 						    (void *)&core_number,
628 						    ADF_DEC);
629 		}
630 
631 		snprintf(key,
632 			 ADF_CFG_MAX_KEY_LEN_IN_BYTES,
633 			 ADF_DC_NAME_FORMAT,
634 			 i);
635 		if (adf_cfg_get_param_value(accel_dev, processed_sec, key, val))
636 			goto failed;
637 
638 		strlcpy(dc_inst->name, val, sizeof(dc_inst->name));
639 
640 		snprintf(key,
641 			 ADF_CFG_MAX_KEY_LEN_IN_BYTES,
642 			 ADF_DC_POLL_MODE_FORMAT,
643 			 i);
644 		if (adf_cfg_set_value(
645 			accel_dev, processed_sec, key, &polling_mode))
646 			goto failed;
647 
648 		dc_inst->polling_mode = polling_mode;
649 		CPU_ZERO(&dc_inst->affinity_mask);
650 		CPU_SET(core_number, &dc_inst->affinity_mask);
651 
652 		if (adf_cfg_get_ring_pairs(
653 			accel_dev->cfg->dev, dc_inst, derived_sec, accel_dev))
654 			goto failed;
655 
656 		adf_cfg_add_dc_inst_info(accel_dev, dc_inst, derived_sec, i);
657 	}
658 
659 	ret = 0;
660 failed:
661 	free(dc_inst, M_QAT);
662 	free(val, M_QAT);
663 	free(key, M_QAT);
664 
665 	if (ret)
666 		device_printf(GET_DEV(accel_dev),
667 			      "Failed to create rings for dc\n");
668 
669 	return ret;
670 }
671 
672 static int
673 adf_cfg_process_user_section(struct adf_accel_dev *accel_dev,
674 			     const char *sec_name,
675 			     int dev)
676 {
677 	int i = 0;
678 	int ret = EFAULT;
679 	unsigned long num_processes = 0;
680 	unsigned long limit_dev_acc = 0;
681 	u8 serv_type = 0;
682 
683 	char *key = NULL;
684 	char *val = NULL;
685 	char *derived_sec_name = NULL;
686 
687 	key = malloc(ADF_CFG_MAX_KEY_LEN_IN_BYTES, M_QAT, M_WAITOK | M_ZERO);
688 
689 	val = malloc(ADF_CFG_MAX_VAL_LEN_IN_BYTES, M_QAT, M_WAITOK | M_ZERO);
690 
691 	derived_sec_name =
692 	    malloc(ADF_CFG_MAX_STR_LEN, M_QAT, M_WAITOK | M_ZERO);
693 
694 	strlcpy(key, ADF_NUM_PROCESSES, ADF_CFG_MAX_KEY_LEN_IN_BYTES);
695 	if (adf_cfg_set_value(accel_dev, sec_name, key, &num_processes))
696 		num_processes = 0;
697 
698 	strlcpy(key, ADF_LIMIT_DEV_ACCESS, ADF_CFG_MAX_KEY_LEN_IN_BYTES);
699 	if (adf_cfg_set_value(accel_dev, sec_name, key, &limit_dev_acc))
700 		limit_dev_acc = 0;
701 
702 	for (i = 0; i < num_processes; i++) {
703 		if (limit_dev_acc)
704 			snprintf(derived_sec_name,
705 				 ADF_CFG_MAX_STR_LEN,
706 				 ADF_LIMITED_USER_SECTION_NAME_FORMAT,
707 				 sec_name,
708 				 dev,
709 				 i);
710 		else
711 			snprintf(derived_sec_name,
712 				 ADF_CFG_MAX_STR_LEN,
713 				 ADF_USER_SECTION_NAME_FORMAT,
714 				 sec_name,
715 				 i);
716 
717 		if (adf_cfg_derived_section_add(accel_dev, derived_sec_name))
718 			goto failed;
719 
720 		/* copy items to the derived section */
721 		adf_cfg_section_copy(accel_dev, sec_name, derived_sec_name);
722 
723 		for (serv_type = NA; serv_type <= USED; serv_type++) {
724 			switch (serv_type) {
725 			case NA:
726 				break;
727 			case CRYPTO:
728 			case ASYM:
729 			case SYM:
730 				if (adf_cfg_is_svc_enabled(accel_dev,
731 							   serv_type))
732 					if (adf_cfg_create_rings_entries_for_cy_inst(
733 						accel_dev,
734 						sec_name,
735 						derived_sec_name,
736 						i,
737 						(enum adf_cfg_service_type)
738 						    serv_type))
739 						goto failed;
740 				break;
741 			case COMP:
742 				if (adf_cfg_is_svc_enabled(accel_dev,
743 							   serv_type))
744 					if (adf_cfg_create_rings_entries_for_dc_inst(
745 						accel_dev,
746 						sec_name,
747 						derived_sec_name,
748 						i))
749 						goto failed;
750 				break;
751 			case USED:
752 				break;
753 			default:
754 				pr_err("Unknown service type %d.\n", serv_type);
755 			}
756 		}
757 	}
758 
759 	ret = 0;
760 failed:
761 
762 	free(val, M_QAT);
763 	free(key, M_QAT);
764 	free(derived_sec_name, M_QAT);
765 
766 	if (ret)
767 		device_printf(GET_DEV(accel_dev),
768 			      "Failed to process user section %s\n",
769 			      sec_name);
770 
771 	return ret;
772 }
773 
774 static int
775 adf_cfg_cleanup_user_section(struct adf_accel_dev *accel_dev,
776 			     const char *sec_name)
777 {
778 	struct adf_cfg_section *sec = adf_cfg_sec_find(accel_dev, sec_name);
779 	struct list_head *head;
780 	struct list_head *list_ptr, *tmp;
781 
782 	if (!sec)
783 		return EFAULT;
784 
785 	if (sec->is_derived)
786 		return 0;
787 
788 	head = &sec->param_head;
789 	list_for_each_prev_safe(list_ptr, tmp, head)
790 	{
791 		struct adf_cfg_key_val *ptr =
792 		    list_entry(list_ptr, struct adf_cfg_key_val, list);
793 
794 		if (!strcmp(ptr->key, ADF_LIMIT_DEV_ACCESS))
795 			continue;
796 
797 		list_del(list_ptr);
798 		free(ptr, M_QAT);
799 	}
800 	return 0;
801 }
802 
803 static int
804 adf_cfg_process_section_no_op(struct adf_accel_dev *accel_dev,
805 			      const char *sec_name)
806 {
807 	return 0;
808 }
809 
810 static int
811 adf_cfg_cleanup_general_section(struct adf_accel_dev *accel_dev,
812 				const char *sec_name)
813 {
814 	unsigned long first_used_bundle = 0;
815 	int ret = EFAULT;
816 	char *key = NULL;
817 	char *val = NULL;
818 
819 	key = malloc(ADF_CFG_MAX_KEY_LEN_IN_BYTES, M_QAT, M_WAITOK | M_ZERO);
820 
821 	val = malloc(ADF_CFG_MAX_VAL_LEN_IN_BYTES, M_QAT, M_WAITOK | M_ZERO);
822 
823 	/* Remove sections that not needed after processing */
824 	strlcpy(key, ADF_CONFIG_VERSION, ADF_CFG_MAX_KEY_LEN_IN_BYTES);
825 	if (adf_cfg_remove_key_param(accel_dev, sec_name, key))
826 		goto failed;
827 
828 	strlcpy(key, ADF_CY ADF_RING_ASYM_SIZE, ADF_CFG_MAX_KEY_LEN_IN_BYTES);
829 	if (adf_cfg_remove_key_param(accel_dev, sec_name, key))
830 		goto failed;
831 
832 	strlcpy(key, ADF_CY ADF_RING_SYM_SIZE, ADF_CFG_MAX_KEY_LEN_IN_BYTES);
833 	if (adf_cfg_remove_key_param(accel_dev, sec_name, key))
834 		goto failed;
835 
836 	strlcpy(key, ADF_DC ADF_RING_DC_SIZE, ADF_CFG_MAX_KEY_LEN_IN_BYTES);
837 	if (adf_cfg_remove_key_param(accel_dev, sec_name, key))
838 		goto failed;
839 
840 	/* After all processing done, set the "FirstUserBundle" value */
841 	first_used_bundle = accel_dev->cfg->dev->max_kernel_bundle_nr + 1;
842 	strlcpy(key, ADF_FIRST_USER_BUNDLE, ADF_CFG_MAX_KEY_LEN_IN_BYTES);
843 	if (adf_cfg_add_key_value_param(
844 		accel_dev, sec_name, key, (void *)&first_used_bundle, ADF_DEC))
845 		goto failed;
846 
847 	ret = 0;
848 failed:
849 	free(key, M_QAT);
850 	free(val, M_QAT);
851 
852 	if (ret)
853 		device_printf(GET_DEV(accel_dev),
854 			      "Failed to clean up general section\n");
855 
856 	return ret;
857 }
858 
859 static int
860 adf_cfg_process_kernel_section(struct adf_accel_dev *accel_dev,
861 			       const char *sec_name)
862 {
863 	u8 serv_type = 0;
864 
865 	for (serv_type = NA; serv_type <= USED; serv_type++) {
866 		switch (serv_type) {
867 		case NA:
868 			break;
869 		case CRYPTO:
870 		case ASYM:
871 		case SYM:
872 			if (adf_cfg_is_svc_enabled(accel_dev, serv_type))
873 				if (adf_cfg_create_rings_entries_for_cy_inst(
874 					accel_dev,
875 					sec_name,
876 					sec_name,
877 					0,
878 					(enum adf_cfg_service_type)serv_type))
879 					goto failed;
880 			break;
881 		case COMP:
882 			if (adf_cfg_is_svc_enabled(accel_dev, serv_type))
883 				if (adf_cfg_create_rings_entries_for_dc_inst(
884 					accel_dev, sec_name, sec_name, 0))
885 					goto failed;
886 			break;
887 		case USED:
888 			break;
889 		default:
890 			pr_err("Unknown service type of instance %d.\n",
891 			       serv_type);
892 		}
893 	}
894 
895 	return 0;
896 
897 failed:
898 	return EFAULT;
899 }
900 
901 static int
902 adf_cfg_cleanup_kernel_section(struct adf_accel_dev *accel_dev,
903 			       const char *sec_name)
904 {
905 	return 0;
906 }
907 
908 static int
909 adf_cfg_create_accel_section(struct adf_accel_dev *accel_dev,
910 			     const char *sec_name)
911 {
912 	/* Find global settings for coalescing. Use defaults if not found */
913 	unsigned long accel_coales = 0;
914 	unsigned long accel_coales_timer = 0;
915 	unsigned long accel_coales_num_msg = 0;
916 	unsigned long cpu;
917 	char *key = NULL;
918 	char *val = NULL;
919 	int ret = EFAULT;
920 	int index = 0;
921 	struct adf_hw_device_data *hw_device = accel_dev->hw_device;
922 
923 	if (!hw_device)
924 		goto failed;
925 
926 	key = malloc(ADF_CFG_MAX_KEY_LEN_IN_BYTES, M_QAT, M_WAITOK | M_ZERO);
927 
928 	val = malloc(ADF_CFG_MAX_VAL_LEN_IN_BYTES, M_QAT, M_WAITOK | M_ZERO);
929 
930 	strlcpy(key,
931 		ADF_ETRMGR_COALESCING_ENABLED,
932 		ADF_CFG_MAX_KEY_LEN_IN_BYTES);
933 	if (adf_cfg_set_value(accel_dev, ADF_GENERAL_SEC, key, &accel_coales))
934 		accel_coales = ADF_CFG_ACCEL_DEF_COALES;
935 
936 	strlcpy(key, ADF_ETRMGR_COALESCE_TIMER, ADF_CFG_MAX_KEY_LEN_IN_BYTES);
937 	if (adf_cfg_set_value(
938 		accel_dev, ADF_GENERAL_SEC, key, &accel_coales_timer))
939 		accel_coales_timer = ADF_CFG_ACCEL_DEF_COALES_TIMER;
940 
941 	strlcpy(key,
942 		ADF_ETRMGR_COALESCING_MSG_ENABLED,
943 		ADF_CFG_MAX_KEY_LEN_IN_BYTES);
944 	if (adf_cfg_set_value(
945 		accel_dev, ADF_GENERAL_SEC, key, &accel_coales_num_msg))
946 		accel_coales_num_msg = ADF_CFG_ACCEL_DEF_COALES_NUM_MSG;
947 
948 	for (index = 0; index < hw_device->num_banks; index++) {
949 		snprintf(key,
950 			 ADF_CFG_MAX_KEY_LEN_IN_BYTES,
951 			 ADF_ETRMGR_COALESCING_ENABLED_FORMAT,
952 			 index);
953 		ret = adf_cfg_add_key_value_param(
954 		    accel_dev, sec_name, key, &accel_coales, ADF_DEC);
955 		if (ret != 0)
956 			goto failed;
957 
958 		snprintf(key,
959 			 ADF_CFG_MAX_KEY_LEN_IN_BYTES,
960 			 ADF_ETRMGR_COALESCE_TIMER_FORMAT,
961 			 index);
962 		ret = adf_cfg_add_key_value_param(
963 		    accel_dev, sec_name, key, &accel_coales_timer, ADF_DEC);
964 		if (ret != 0)
965 			goto failed;
966 
967 		snprintf(key,
968 			 ADF_CFG_MAX_KEY_LEN_IN_BYTES,
969 			 ADF_ETRMGR_COALESCING_MSG_ENABLED_FORMAT,
970 			 index);
971 		ret = adf_cfg_add_key_value_param(
972 		    accel_dev, sec_name, key, &accel_coales_num_msg, ADF_DEC);
973 		if (ret != 0)
974 			goto failed;
975 
976 		cpu = ADF_CFG_AFFINITY_WHATEVER;
977 
978 		snprintf(key,
979 			 ADF_CFG_MAX_KEY_LEN_IN_BYTES,
980 			 ADF_ETRMGR_CORE_AFFINITY_FORMAT,
981 			 index);
982 		ret = adf_cfg_add_key_value_param(
983 		    accel_dev, sec_name, key, &cpu, ADF_DEC);
984 		if (ret != 0)
985 			goto failed;
986 	}
987 
988 	ret = 0;
989 
990 failed:
991 	free(key, M_QAT);
992 	free(val, M_QAT);
993 
994 	if (ret)
995 		device_printf(GET_DEV(accel_dev),
996 			      "Failed to create accel section\n");
997 
998 	return ret;
999 }
1000 
1001 static int
1002 adf_cfg_cleanup_accel_section(struct adf_accel_dev *accel_dev,
1003 			      const char *sec_name)
1004 {
1005 	return 0;
1006 }
1007 
1008 static int
1009 adf_cfg_process_accel_section(struct adf_accel_dev *accel_dev,
1010 			      const char *sec_name)
1011 {
1012 	int accel_num = 0;
1013 	struct adf_hw_device_data *hw_device = accel_dev->hw_device;
1014 	char *derived_name = NULL;
1015 	int ret = EFAULT;
1016 
1017 	if (!hw_device)
1018 		goto failed;
1019 
1020 	if (hw_device->num_logical_accel == 0)
1021 		goto failed;
1022 
1023 	derived_name =
1024 	    malloc(ADF_CFG_MAX_SECTION_LEN_IN_BYTES, M_QAT, M_WAITOK | M_ZERO);
1025 
1026 	for (accel_num = 0; accel_num < hw_device->num_logical_accel;
1027 	     accel_num++) {
1028 		snprintf(derived_name,
1029 			 ADF_CFG_MAX_SECTION_LEN_IN_BYTES,
1030 			 ADF_ACCEL_STR,
1031 			 accel_num);
1032 		ret = adf_cfg_section_add(accel_dev, derived_name);
1033 		if (ret != 0)
1034 			goto failed;
1035 
1036 		ret = adf_cfg_create_accel_section(accel_dev, derived_name);
1037 		if (ret != 0)
1038 			goto failed;
1039 	}
1040 
1041 	ret = 0;
1042 failed:
1043 	free(derived_name, M_QAT);
1044 
1045 	if (ret)
1046 		device_printf(GET_DEV(accel_dev),
1047 			      "Failed to process accel section\n");
1048 
1049 	return ret;
1050 }
1051 
1052 int
1053 adf_cfg_process_section(struct adf_accel_dev *accel_dev,
1054 			const char *sec_name,
1055 			int dev)
1056 {
1057 	if (!strcmp(sec_name, ADF_GENERAL_SEC) ||
1058 	    !strcmp(sec_name, ADF_INLINE_SEC))
1059 		return adf_cfg_process_section_no_op(accel_dev, sec_name);
1060 	else if (!strcmp(sec_name, ADF_KERNEL_SEC) ||
1061 		 !strcmp(sec_name, ADF_KERNEL_SAL_SEC))
1062 		return adf_cfg_process_kernel_section(accel_dev, sec_name);
1063 	else if (!strcmp(sec_name, ADF_ACCEL_SEC))
1064 		return adf_cfg_process_accel_section(accel_dev, sec_name);
1065 	else
1066 		return adf_cfg_process_user_section(accel_dev, sec_name, dev);
1067 }
1068 
1069 int
1070 adf_cfg_cleanup_section(struct adf_accel_dev *accel_dev,
1071 			const char *sec_name,
1072 			int dev)
1073 {
1074 	if (!strcmp(sec_name, ADF_GENERAL_SEC))
1075 		return adf_cfg_cleanup_general_section(accel_dev, sec_name);
1076 	else if (!strcmp(sec_name, ADF_INLINE_SEC))
1077 		return adf_cfg_process_section_no_op(accel_dev, sec_name);
1078 	else if (!strcmp(sec_name, ADF_KERNEL_SEC) ||
1079 		 !strcmp(sec_name, ADF_KERNEL_SAL_SEC))
1080 		return adf_cfg_cleanup_kernel_section(accel_dev, sec_name);
1081 	else if (strstr(sec_name, ADF_ACCEL_SEC))
1082 		return adf_cfg_cleanup_accel_section(accel_dev, sec_name);
1083 	else
1084 		return adf_cfg_cleanup_user_section(accel_dev, sec_name);
1085 }
1086 
1087 int
1088 adf_cfg_setup_irq(struct adf_accel_dev *accel_dev)
1089 {
1090 	int ret = EFAULT;
1091 	struct adf_accel_pci *info_pci_dev = &accel_dev->accel_pci_dev;
1092 	struct adf_cfg_device *cfg_dev = NULL;
1093 	struct msix_entry *msixe = NULL;
1094 	u32 num_msix = 0;
1095 	int index = 0;
1096 	int computed_core = 0;
1097 
1098 	if (!accel_dev || !accel_dev->cfg || !accel_dev->hw_device)
1099 		goto failed;
1100 
1101 	cfg_dev = accel_dev->cfg->dev;
1102 	if (!cfg_dev)
1103 		goto failed;
1104 
1105 	msixe =
1106 	    (struct msix_entry *)accel_dev->accel_pci_dev.msix_entries.entries;
1107 	num_msix = accel_dev->accel_pci_dev.msix_entries.num_entries;
1108 	if (!msixe)
1109 		goto cleanup_and_fail;
1110 
1111 	/*
1112 	 * Here we want to set the affinity of kernel and epoll mode
1113 	 * bundle into user defined value.
1114 	 * Because in adf_isr.c we setup core affinity by round-robin
1115 	 * we need to reset it after device up done.
1116 	 */
1117 	for (index = 0; index < accel_dev->hw_device->num_banks; index++) {
1118 		struct adf_cfg_bundle *bundle = cfg_dev->bundles[index];
1119 
1120 		if (!bundle)
1121 			continue;
1122 
1123 		if (bundle->type != KERNEL &&
1124 		    bundle->polling_mode != ADF_CFG_RESP_EPOLL)
1125 			continue;
1126 
1127 		if (bundle->number >= num_msix)
1128 			goto cleanup_and_fail;
1129 
1130 		computed_core = CPU_FFS(&bundle->affinity_mask) - 1;
1131 		bus_bind_intr(info_pci_dev->pci_dev,
1132 			      msixe[index].irq,
1133 			      computed_core);
1134 	}
1135 	ret = 0;
1136 
1137 cleanup_and_fail:
1138 	adf_cfg_device_clear(cfg_dev, accel_dev);
1139 	free(cfg_dev, M_QAT);
1140 	accel_dev->cfg->dev = NULL;
1141 
1142 failed:
1143 	return ret;
1144 }
1145