xref: /freebsd/sys/dev/qat/qat_common/adf_cfg_section.c (revision 9f23cbd6cae82fd77edfad7173432fa8dccd0a95)
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 	if (adf_cy_inst_cross_banks(accel_dev))
290 		snprintf(key,
291 			 ADF_CFG_MAX_KEY_LEN_IN_BYTES,
292 			 ADF_CY_ASYM_BANK_NUM_FORMAT,
293 			 inst_index);
294 	else
295 		snprintf(key,
296 			 ADF_CFG_MAX_KEY_LEN_IN_BYTES,
297 			 ADF_CY_BANK_NUM_FORMAT,
298 			 inst_index);
299 	bank_number = asym_inst->bundle;
300 	adf_cfg_add_key_value_param(
301 	    accel_dev, derived_sec, key, (void *)&bank_number, ADF_DEC);
302 
303 	snprintf(key,
304 		 ADF_CFG_MAX_KEY_LEN_IN_BYTES,
305 		 ADF_CY_ASYM_TX_FORMAT,
306 		 inst_index);
307 	ring_number = asym_inst->asym_tx;
308 	adf_cfg_add_key_value_param(
309 	    accel_dev, derived_sec, key, (void *)&ring_number, ADF_DEC);
310 
311 	snprintf(key,
312 		 ADF_CFG_MAX_KEY_LEN_IN_BYTES,
313 		 ADF_CY_ASYM_RX_FORMAT,
314 		 inst_index);
315 	ring_number = asym_inst->asym_rx;
316 	adf_cfg_add_key_value_param(
317 	    accel_dev, derived_sec, key, (void *)&ring_number, ADF_DEC);
318 
319 	strlcpy(key, ADF_CY_RING_ASYM_SIZE, ADF_CFG_MAX_KEY_LEN_IN_BYTES);
320 	if (adf_cfg_set_value(accel_dev, ADF_GENERAL_SEC, key, &asym_req))
321 		asym_req = ADF_CFG_DEF_CY_RING_ASYM_SIZE;
322 
323 	snprintf(key,
324 		 ADF_CFG_MAX_KEY_LEN_IN_BYTES,
325 		 ADF_CY_RING_ASYM_SIZE_FORMAT,
326 		 inst_index);
327 	adf_cfg_add_key_value_param(
328 	    accel_dev, derived_sec, key, (void *)&asym_req, ADF_DEC);
329 
330 	free(key, M_QAT);
331 }
332 
333 static void
334 adf_cfg_add_sym_inst_info(struct adf_accel_dev *accel_dev,
335 			  struct adf_cfg_instance *sym_inst,
336 			  const char *derived_sec,
337 			  int inst_index)
338 {
339 	char *key = NULL;
340 	unsigned long bank_number = 0;
341 	unsigned long ring_number = 0;
342 	unsigned long sym_req = 0;
343 
344 	key = malloc(ADF_CFG_MAX_KEY_LEN_IN_BYTES, M_QAT, M_WAITOK | M_ZERO);
345 
346 	if (adf_cy_inst_cross_banks(accel_dev))
347 		snprintf(key,
348 			 ADF_CFG_MAX_KEY_LEN_IN_BYTES,
349 			 ADF_CY_SYM_BANK_NUM_FORMAT,
350 			 inst_index);
351 	else
352 		snprintf(key,
353 			 ADF_CFG_MAX_KEY_LEN_IN_BYTES,
354 			 ADF_CY_BANK_NUM_FORMAT,
355 			 inst_index);
356 
357 	bank_number = sym_inst->bundle;
358 	adf_cfg_add_key_value_param(
359 	    accel_dev, derived_sec, key, (void *)&bank_number, ADF_DEC);
360 
361 	snprintf(key,
362 		 ADF_CFG_MAX_KEY_LEN_IN_BYTES,
363 		 ADF_CY_SYM_TX_FORMAT,
364 		 inst_index);
365 	ring_number = sym_inst->sym_tx;
366 	adf_cfg_add_key_value_param(
367 	    accel_dev, derived_sec, key, (void *)&ring_number, ADF_DEC);
368 
369 	snprintf(key,
370 		 ADF_CFG_MAX_KEY_LEN_IN_BYTES,
371 		 ADF_CY_SYM_RX_FORMAT,
372 		 inst_index);
373 	ring_number = sym_inst->sym_rx;
374 	adf_cfg_add_key_value_param(
375 	    accel_dev, derived_sec, key, (void *)&ring_number, ADF_DEC);
376 
377 	strlcpy(key, ADF_CY_RING_SYM_SIZE, ADF_CFG_MAX_KEY_LEN_IN_BYTES);
378 	if (adf_cfg_set_value(accel_dev, ADF_GENERAL_SEC, key, &sym_req))
379 		sym_req = ADF_CFG_DEF_CY_RING_SYM_SIZE;
380 
381 	snprintf(key,
382 		 ADF_CFG_MAX_KEY_LEN_IN_BYTES,
383 		 ADF_CY_RING_SYM_SIZE_FORMAT,
384 		 inst_index);
385 	adf_cfg_add_key_value_param(
386 	    accel_dev, derived_sec, key, (void *)&sym_req, ADF_DEC);
387 
388 	free(key, M_QAT);
389 }
390 
391 static int
392 adf_cfg_section_copy(struct adf_accel_dev *accel_dev,
393 		     const char *processed_sec,
394 		     const char *derived_sec)
395 {
396 	unsigned long val = 0;
397 	struct list_head *list;
398 	struct adf_cfg_section *sec_process =
399 	    adf_cfg_sec_find(accel_dev, processed_sec);
400 	if (!sec_process)
401 		return EFAULT;
402 
403 	list_for_each(list, &sec_process->param_head)
404 	{
405 		struct adf_cfg_key_val *ptr =
406 		    list_entry(list, struct adf_cfg_key_val, list);
407 
408 		/*
409 		 * ignore CoreAffinity since it will be generated later, and
410 		 * there is no need to keep NumProcesses and LimitDevAccess.
411 		 */
412 		if (strstr(ptr->key, ADF_ETRMGR_CORE_AFFINITY) ||
413 		    strstr(ptr->key, ADF_NUM_PROCESSES) ||
414 		    strstr(ptr->key, ADF_LIMIT_DEV_ACCESS))
415 			continue;
416 
417 		if (ptr->type == ADF_DEC) {
418 			if (!compat_strtoul(ptr->val, 10, &val))
419 				adf_cfg_add_key_value_param(accel_dev,
420 							    derived_sec,
421 							    ptr->key,
422 							    (void *)&val,
423 							    ptr->type);
424 		} else if (ptr->type == ADF_STR) {
425 			adf_cfg_add_key_value_param(accel_dev,
426 						    derived_sec,
427 						    ptr->key,
428 						    (void *)ptr->val,
429 						    ptr->type);
430 		} else if (ptr->type == ADF_HEX) {
431 			if (!compat_strtoul(ptr->val, 16, &val))
432 				adf_cfg_add_key_value_param(accel_dev,
433 							    derived_sec,
434 							    ptr->key,
435 							    (void *)val,
436 							    ptr->type);
437 		}
438 	}
439 	return 0;
440 }
441 
442 static int
443 adf_cfg_create_rings_entries_for_cy_inst(struct adf_accel_dev *accel_dev,
444 					 const char *processed_sec,
445 					 const char *derived_sec,
446 					 int process_num,
447 					 enum adf_cfg_service_type serv_type)
448 {
449 	int i = 0;
450 	int ret = EFAULT;
451 	unsigned long num_inst = 0, num_dc_inst = 0;
452 	unsigned long core_number = 0;
453 	unsigned long polling_mode = 0;
454 	struct adf_cfg_instance *crypto_inst = NULL;
455 
456 	char *key = NULL;
457 	char *val = NULL;
458 
459 	key = malloc(ADF_CFG_MAX_KEY_LEN_IN_BYTES, M_QAT, M_WAITOK | M_ZERO);
460 
461 	val = malloc(ADF_CFG_MAX_VAL_LEN_IN_BYTES, M_QAT, M_WAITOK | M_ZERO);
462 
463 	snprintf(key, ADF_CFG_MAX_KEY_LEN_IN_BYTES, ADF_SERVICES_ENABLED);
464 	if (adf_cfg_get_param_value(accel_dev, ADF_GENERAL_SEC, key, val))
465 		goto failed;
466 	if ((!strncmp(val, ADF_CFG_CY, ADF_CFG_MAX_VAL_LEN_IN_BYTES)) ||
467 	    (!strncmp(val, ADF_CFG_ASYM, ADF_CFG_MAX_VAL_LEN_IN_BYTES)) ||
468 	    (!strncmp(val, ADF_CFG_SYM, ADF_CFG_MAX_VAL_LEN_IN_BYTES))) {
469 		strlcpy(key, ADF_NUM_DC, ADF_CFG_MAX_KEY_LEN_IN_BYTES);
470 		if (adf_cfg_set_value(
471 			accel_dev, processed_sec, key, &num_dc_inst))
472 			goto failed;
473 		if (num_dc_inst > 0) {
474 			device_printf(
475 			    GET_DEV(accel_dev),
476 			    "NumDcInstances > 0,when CY only is enabled\n");
477 			goto failed;
478 		}
479 	}
480 	ret = EFAULT;
481 
482 	strlcpy(key, ADF_NUM_CY, ADF_CFG_MAX_KEY_LEN_IN_BYTES);
483 	if (adf_cfg_set_value(accel_dev, processed_sec, key, &num_inst))
484 		goto failed;
485 
486 	crypto_inst = malloc(sizeof(*crypto_inst), M_QAT, M_WAITOK | M_ZERO);
487 
488 	for (i = 0; i < num_inst; i++) {
489 		memset(crypto_inst, 0, sizeof(*crypto_inst));
490 		crypto_inst->stype = serv_type;
491 		snprintf(key,
492 			 ADF_CFG_MAX_KEY_LEN_IN_BYTES,
493 			 ADF_CY_CORE_AFFINITY_FORMAT,
494 			 i);
495 		if (adf_cfg_set_core_number_for_instance(accel_dev,
496 							 processed_sec,
497 							 key,
498 							 process_num,
499 							 &core_number))
500 			goto failed;
501 
502 		if (strcmp(processed_sec, ADF_KERNEL_SEC) &&
503 		    strcmp(processed_sec, ADF_KERNEL_SAL_SEC))
504 			adf_cfg_add_key_value_param(accel_dev,
505 						    derived_sec,
506 						    key,
507 						    (void *)&core_number,
508 						    ADF_DEC);
509 
510 		snprintf(key,
511 			 ADF_CFG_MAX_KEY_LEN_IN_BYTES,
512 			 ADF_CY_NAME_FORMAT,
513 			 i);
514 		if (adf_cfg_get_param_value(accel_dev, processed_sec, key, val))
515 			goto failed;
516 
517 		strlcpy(crypto_inst->name, val, sizeof(crypto_inst->name));
518 
519 		snprintf(key,
520 			 ADF_CFG_MAX_KEY_LEN_IN_BYTES,
521 			 ADF_CY_POLL_MODE_FORMAT,
522 			 i);
523 		if (adf_cfg_set_value(
524 			accel_dev, processed_sec, key, &polling_mode))
525 			goto failed;
526 
527 		crypto_inst->polling_mode = polling_mode;
528 		CPU_ZERO(&crypto_inst->affinity_mask);
529 		CPU_SET(core_number, &crypto_inst->affinity_mask);
530 
531 		if (adf_cfg_get_ring_pairs(accel_dev->cfg->dev,
532 					   crypto_inst,
533 					   derived_sec,
534 					   accel_dev))
535 			goto failed;
536 
537 		switch (serv_type) {
538 		case CRYPTO:
539 			adf_cfg_add_cy_inst_info(accel_dev,
540 						 crypto_inst,
541 						 derived_sec,
542 						 i);
543 			break;
544 		case ASYM:
545 			adf_cfg_add_asym_inst_info(accel_dev,
546 						   crypto_inst,
547 						   derived_sec,
548 						   i);
549 			break;
550 		case SYM:
551 			adf_cfg_add_sym_inst_info(accel_dev,
552 						  crypto_inst,
553 						  derived_sec,
554 						  i);
555 			break;
556 		default:
557 			pr_err("unknown crypto instance type %d.\n", serv_type);
558 			goto failed;
559 		}
560 	}
561 
562 	ret = 0;
563 failed:
564 	free(crypto_inst, M_QAT);
565 	free(val, M_QAT);
566 	free(key, M_QAT);
567 
568 	if (ret)
569 		device_printf(GET_DEV(accel_dev),
570 			      "Failed to create rings for cy\n");
571 
572 	return ret;
573 }
574 
575 static int
576 adf_cfg_create_rings_entries_for_dc_inst(struct adf_accel_dev *accel_dev,
577 					 const char *processed_sec,
578 					 const char *derived_sec,
579 					 int process_num)
580 {
581 	int i = 0;
582 	int ret = EFAULT;
583 	unsigned long num_inst = 0, num_cy_inst = 0;
584 	unsigned long core_number = 0;
585 	unsigned long polling_mode = 0;
586 	struct adf_cfg_instance *dc_inst = NULL;
587 
588 	char *key = NULL;
589 	char *val = NULL;
590 
591 	key = malloc(ADF_CFG_MAX_KEY_LEN_IN_BYTES, M_QAT, M_WAITOK | M_ZERO);
592 
593 	val = malloc(ADF_CFG_MAX_VAL_LEN_IN_BYTES, M_QAT, M_WAITOK | M_ZERO);
594 
595 	ret = EFAULT;
596 
597 	snprintf(key, ADF_CFG_MAX_STR_LEN, ADF_SERVICES_ENABLED);
598 	if (adf_cfg_get_param_value(accel_dev, ADF_GENERAL_SEC, key, val))
599 		goto failed;
600 
601 	if (!strncmp(val, ADF_CFG_DC, ADF_CFG_MAX_VAL_LEN_IN_BYTES)) {
602 		strlcpy(key, ADF_NUM_CY, ADF_CFG_MAX_KEY_LEN_IN_BYTES);
603 		if (adf_cfg_set_value(
604 			accel_dev, processed_sec, key, &num_cy_inst))
605 			goto failed;
606 		if (num_cy_inst > 0) {
607 			device_printf(
608 			    GET_DEV(accel_dev),
609 			    "NumCyInstances > 0,when DC only is enabled\n");
610 			goto failed;
611 		}
612 	}
613 
614 	strlcpy(key, ADF_NUM_DC, ADF_CFG_MAX_KEY_LEN_IN_BYTES);
615 	if (adf_cfg_set_value(accel_dev, processed_sec, key, &num_inst))
616 		goto failed;
617 
618 	dc_inst = malloc(sizeof(*dc_inst), M_QAT, M_WAITOK | M_ZERO);
619 
620 	for (i = 0; i < num_inst; i++) {
621 		memset(dc_inst, 0, sizeof(*dc_inst));
622 		dc_inst->stype = COMP;
623 		snprintf(key,
624 			 ADF_CFG_MAX_STR_LEN,
625 			 ADF_DC_CORE_AFFINITY_FORMAT,
626 			 i);
627 
628 		if (adf_cfg_set_core_number_for_instance(accel_dev,
629 							 processed_sec,
630 							 key,
631 							 process_num,
632 							 &core_number))
633 			goto failed;
634 
635 		if (strcmp(processed_sec, ADF_KERNEL_SEC) &&
636 		    strcmp(processed_sec, ADF_KERNEL_SAL_SEC)) {
637 			adf_cfg_add_key_value_param(accel_dev,
638 						    derived_sec,
639 						    key,
640 						    (void *)&core_number,
641 						    ADF_DEC);
642 		}
643 
644 		snprintf(key,
645 			 ADF_CFG_MAX_KEY_LEN_IN_BYTES,
646 			 ADF_DC_NAME_FORMAT,
647 			 i);
648 		if (adf_cfg_get_param_value(accel_dev, processed_sec, key, val))
649 			goto failed;
650 
651 		strlcpy(dc_inst->name, val, sizeof(dc_inst->name));
652 
653 		snprintf(key,
654 			 ADF_CFG_MAX_KEY_LEN_IN_BYTES,
655 			 ADF_DC_POLL_MODE_FORMAT,
656 			 i);
657 		if (adf_cfg_set_value(
658 			accel_dev, processed_sec, key, &polling_mode))
659 			goto failed;
660 
661 		dc_inst->polling_mode = polling_mode;
662 		CPU_ZERO(&dc_inst->affinity_mask);
663 		CPU_SET(core_number, &dc_inst->affinity_mask);
664 
665 		if (adf_cfg_get_ring_pairs(
666 			accel_dev->cfg->dev, dc_inst, derived_sec, accel_dev))
667 			goto failed;
668 
669 		adf_cfg_add_dc_inst_info(accel_dev, dc_inst, derived_sec, i);
670 	}
671 
672 	ret = 0;
673 failed:
674 	free(dc_inst, M_QAT);
675 	free(val, M_QAT);
676 	free(key, M_QAT);
677 
678 	if (ret)
679 		device_printf(GET_DEV(accel_dev),
680 			      "Failed to create rings for dc\n");
681 
682 	return ret;
683 }
684 
685 static int
686 adf_cfg_process_user_section(struct adf_accel_dev *accel_dev,
687 			     const char *sec_name,
688 			     int dev)
689 {
690 	int i = 0;
691 	int ret = EFAULT;
692 	unsigned long num_processes = 0;
693 	unsigned long limit_dev_acc = 0;
694 	u8 serv_type = 0;
695 
696 	char *key = NULL;
697 	char *val = NULL;
698 	char *derived_sec_name = NULL;
699 
700 	key = malloc(ADF_CFG_MAX_KEY_LEN_IN_BYTES, M_QAT, M_WAITOK | M_ZERO);
701 
702 	val = malloc(ADF_CFG_MAX_VAL_LEN_IN_BYTES, M_QAT, M_WAITOK | M_ZERO);
703 
704 	derived_sec_name =
705 	    malloc(ADF_CFG_MAX_STR_LEN, M_QAT, M_WAITOK | M_ZERO);
706 
707 	strlcpy(key, ADF_NUM_PROCESSES, ADF_CFG_MAX_KEY_LEN_IN_BYTES);
708 	if (adf_cfg_set_value(accel_dev, sec_name, key, &num_processes))
709 		num_processes = 0;
710 
711 	strlcpy(key, ADF_LIMIT_DEV_ACCESS, ADF_CFG_MAX_KEY_LEN_IN_BYTES);
712 	if (adf_cfg_set_value(accel_dev, sec_name, key, &limit_dev_acc))
713 		limit_dev_acc = 0;
714 
715 	for (i = 0; i < num_processes; i++) {
716 		if (limit_dev_acc)
717 			snprintf(derived_sec_name,
718 				 ADF_CFG_MAX_STR_LEN,
719 				 ADF_LIMITED_USER_SECTION_NAME_FORMAT,
720 				 sec_name,
721 				 dev,
722 				 i);
723 		else
724 			snprintf(derived_sec_name,
725 				 ADF_CFG_MAX_STR_LEN,
726 				 ADF_USER_SECTION_NAME_FORMAT,
727 				 sec_name,
728 				 i);
729 
730 		if (adf_cfg_derived_section_add(accel_dev, derived_sec_name))
731 			goto failed;
732 
733 		/* copy items to the derived section */
734 		adf_cfg_section_copy(accel_dev, sec_name, derived_sec_name);
735 
736 		for (serv_type = NA; serv_type <= USED; serv_type++) {
737 			switch (serv_type) {
738 			case NA:
739 				break;
740 			case CRYPTO:
741 			case ASYM:
742 			case SYM:
743 				if (adf_cfg_is_svc_enabled(accel_dev,
744 							   serv_type))
745 					if (adf_cfg_create_rings_entries_for_cy_inst(
746 						accel_dev,
747 						sec_name,
748 						derived_sec_name,
749 						i,
750 						(enum adf_cfg_service_type)
751 						    serv_type))
752 						goto failed;
753 				break;
754 			case COMP:
755 				if (adf_cfg_is_svc_enabled(accel_dev,
756 							   serv_type))
757 					if (adf_cfg_create_rings_entries_for_dc_inst(
758 						accel_dev,
759 						sec_name,
760 						derived_sec_name,
761 						i))
762 						goto failed;
763 				break;
764 			case USED:
765 				break;
766 			default:
767 				pr_err("Unknown service type %d.\n", serv_type);
768 			}
769 		}
770 	}
771 
772 	ret = 0;
773 failed:
774 
775 	free(val, M_QAT);
776 	free(key, M_QAT);
777 	free(derived_sec_name, M_QAT);
778 
779 	if (ret)
780 		device_printf(GET_DEV(accel_dev),
781 			      "Failed to process user section %s\n",
782 			      sec_name);
783 
784 	return ret;
785 }
786 
787 static int
788 adf_cfg_cleanup_user_section(struct adf_accel_dev *accel_dev,
789 			     const char *sec_name)
790 {
791 	struct adf_cfg_section *sec = adf_cfg_sec_find(accel_dev, sec_name);
792 	struct list_head *head;
793 	struct list_head *list_ptr, *tmp;
794 
795 	if (!sec)
796 		return EFAULT;
797 
798 	if (sec->is_derived)
799 		return 0;
800 
801 	head = &sec->param_head;
802 	list_for_each_prev_safe(list_ptr, tmp, head)
803 	{
804 		struct adf_cfg_key_val *ptr =
805 		    list_entry(list_ptr, struct adf_cfg_key_val, list);
806 
807 		if (!strcmp(ptr->key, ADF_LIMIT_DEV_ACCESS))
808 			continue;
809 
810 		list_del(list_ptr);
811 		free(ptr, M_QAT);
812 	}
813 	return 0;
814 }
815 
816 static int
817 adf_cfg_process_section_no_op(struct adf_accel_dev *accel_dev,
818 			      const char *sec_name)
819 {
820 	return 0;
821 }
822 
823 static int
824 adf_cfg_cleanup_general_section(struct adf_accel_dev *accel_dev,
825 				const char *sec_name)
826 {
827 	unsigned long first_used_bundle = 0;
828 	int ret = EFAULT;
829 	char *key = NULL;
830 	char *val = NULL;
831 
832 	key = malloc(ADF_CFG_MAX_KEY_LEN_IN_BYTES, M_QAT, M_WAITOK | M_ZERO);
833 
834 	val = malloc(ADF_CFG_MAX_VAL_LEN_IN_BYTES, M_QAT, M_WAITOK | M_ZERO);
835 
836 	/* Remove sections that not needed after processing */
837 	strlcpy(key, ADF_CONFIG_VERSION, ADF_CFG_MAX_KEY_LEN_IN_BYTES);
838 	if (adf_cfg_remove_key_param(accel_dev, sec_name, key))
839 		goto failed;
840 
841 	strlcpy(key, ADF_CY ADF_RING_ASYM_SIZE, ADF_CFG_MAX_KEY_LEN_IN_BYTES);
842 	if (adf_cfg_remove_key_param(accel_dev, sec_name, key))
843 		goto failed;
844 
845 	strlcpy(key, ADF_CY ADF_RING_SYM_SIZE, ADF_CFG_MAX_KEY_LEN_IN_BYTES);
846 	if (adf_cfg_remove_key_param(accel_dev, sec_name, key))
847 		goto failed;
848 
849 	strlcpy(key, ADF_DC ADF_RING_DC_SIZE, ADF_CFG_MAX_KEY_LEN_IN_BYTES);
850 	if (adf_cfg_remove_key_param(accel_dev, sec_name, key))
851 		goto failed;
852 
853 	/* After all processing done, set the "FirstUserBundle" value */
854 	first_used_bundle = accel_dev->cfg->dev->max_kernel_bundle_nr + 1;
855 	strlcpy(key, ADF_FIRST_USER_BUNDLE, ADF_CFG_MAX_KEY_LEN_IN_BYTES);
856 	if (adf_cfg_add_key_value_param(
857 		accel_dev, sec_name, key, (void *)&first_used_bundle, ADF_DEC))
858 		goto failed;
859 
860 	ret = 0;
861 failed:
862 	free(key, M_QAT);
863 	free(val, M_QAT);
864 
865 	if (ret)
866 		device_printf(GET_DEV(accel_dev),
867 			      "Failed to clean up general section\n");
868 
869 	return ret;
870 }
871 
872 static int
873 adf_cfg_process_kernel_section(struct adf_accel_dev *accel_dev,
874 			       const char *sec_name)
875 {
876 	u8 serv_type = 0;
877 
878 	for (serv_type = NA; serv_type <= USED; serv_type++) {
879 		switch (serv_type) {
880 		case NA:
881 			break;
882 		case CRYPTO:
883 		case ASYM:
884 		case SYM:
885 			if (adf_cfg_is_svc_enabled(accel_dev, serv_type))
886 				if (adf_cfg_create_rings_entries_for_cy_inst(
887 					accel_dev,
888 					sec_name,
889 					sec_name,
890 					0,
891 					(enum adf_cfg_service_type)serv_type))
892 					goto failed;
893 			break;
894 		case COMP:
895 			if (adf_cfg_is_svc_enabled(accel_dev, serv_type))
896 				if (adf_cfg_create_rings_entries_for_dc_inst(
897 					accel_dev, sec_name, sec_name, 0))
898 					goto failed;
899 			break;
900 		case USED:
901 			break;
902 		default:
903 			pr_err("Unknown service type of instance %d.\n",
904 			       serv_type);
905 		}
906 	}
907 
908 	return 0;
909 
910 failed:
911 	return EFAULT;
912 }
913 
914 static int
915 adf_cfg_cleanup_kernel_section(struct adf_accel_dev *accel_dev,
916 			       const char *sec_name)
917 {
918 	return 0;
919 }
920 
921 static int
922 adf_cfg_create_accel_section(struct adf_accel_dev *accel_dev,
923 			     const char *sec_name)
924 {
925 	/* Find global settings for coalescing. Use defaults if not found */
926 	unsigned long accel_coales = 0;
927 	unsigned long accel_coales_timer = 0;
928 	unsigned long accel_coales_num_msg = 0;
929 	unsigned long cpu;
930 	char *key = NULL;
931 	char *val = NULL;
932 	int ret = EFAULT;
933 	int index = 0;
934 	struct adf_hw_device_data *hw_device = accel_dev->hw_device;
935 
936 	if (!hw_device)
937 		goto failed;
938 
939 	key = malloc(ADF_CFG_MAX_KEY_LEN_IN_BYTES, M_QAT, M_WAITOK | M_ZERO);
940 
941 	val = malloc(ADF_CFG_MAX_VAL_LEN_IN_BYTES, M_QAT, M_WAITOK | M_ZERO);
942 
943 	strlcpy(key,
944 		ADF_ETRMGR_COALESCING_ENABLED,
945 		ADF_CFG_MAX_KEY_LEN_IN_BYTES);
946 	if (adf_cfg_set_value(accel_dev, ADF_GENERAL_SEC, key, &accel_coales))
947 		accel_coales = ADF_CFG_ACCEL_DEF_COALES;
948 
949 	strlcpy(key, ADF_ETRMGR_COALESCE_TIMER, ADF_CFG_MAX_KEY_LEN_IN_BYTES);
950 	if (adf_cfg_set_value(
951 		accel_dev, ADF_GENERAL_SEC, key, &accel_coales_timer))
952 		accel_coales_timer = ADF_CFG_ACCEL_DEF_COALES_TIMER;
953 
954 	strlcpy(key,
955 		ADF_ETRMGR_COALESCING_MSG_ENABLED,
956 		ADF_CFG_MAX_KEY_LEN_IN_BYTES);
957 	if (adf_cfg_set_value(
958 		accel_dev, ADF_GENERAL_SEC, key, &accel_coales_num_msg))
959 		accel_coales_num_msg = ADF_CFG_ACCEL_DEF_COALES_NUM_MSG;
960 
961 	for (index = 0; index < hw_device->num_banks; index++) {
962 		snprintf(key,
963 			 ADF_CFG_MAX_KEY_LEN_IN_BYTES,
964 			 ADF_ETRMGR_COALESCING_ENABLED_FORMAT,
965 			 index);
966 		ret = adf_cfg_add_key_value_param(
967 		    accel_dev, sec_name, key, &accel_coales, ADF_DEC);
968 		if (ret != 0)
969 			goto failed;
970 
971 		snprintf(key,
972 			 ADF_CFG_MAX_KEY_LEN_IN_BYTES,
973 			 ADF_ETRMGR_COALESCE_TIMER_FORMAT,
974 			 index);
975 		ret = adf_cfg_add_key_value_param(
976 		    accel_dev, sec_name, key, &accel_coales_timer, ADF_DEC);
977 		if (ret != 0)
978 			goto failed;
979 
980 		snprintf(key,
981 			 ADF_CFG_MAX_KEY_LEN_IN_BYTES,
982 			 ADF_ETRMGR_COALESCING_MSG_ENABLED_FORMAT,
983 			 index);
984 		ret = adf_cfg_add_key_value_param(
985 		    accel_dev, sec_name, key, &accel_coales_num_msg, ADF_DEC);
986 		if (ret != 0)
987 			goto failed;
988 
989 		cpu = ADF_CFG_AFFINITY_WHATEVER;
990 
991 		snprintf(key,
992 			 ADF_CFG_MAX_KEY_LEN_IN_BYTES,
993 			 ADF_ETRMGR_CORE_AFFINITY_FORMAT,
994 			 index);
995 		ret = adf_cfg_add_key_value_param(
996 		    accel_dev, sec_name, key, &cpu, ADF_DEC);
997 		if (ret != 0)
998 			goto failed;
999 	}
1000 
1001 	ret = 0;
1002 
1003 failed:
1004 	free(key, M_QAT);
1005 	free(val, M_QAT);
1006 
1007 	if (ret)
1008 		device_printf(GET_DEV(accel_dev),
1009 			      "Failed to create accel section\n");
1010 
1011 	return ret;
1012 }
1013 
1014 static int
1015 adf_cfg_cleanup_accel_section(struct adf_accel_dev *accel_dev,
1016 			      const char *sec_name)
1017 {
1018 	return 0;
1019 }
1020 
1021 static int
1022 adf_cfg_process_accel_section(struct adf_accel_dev *accel_dev,
1023 			      const char *sec_name)
1024 {
1025 	int accel_num = 0;
1026 	struct adf_hw_device_data *hw_device = accel_dev->hw_device;
1027 	char *derived_name = NULL;
1028 	int ret = EFAULT;
1029 
1030 	if (!hw_device)
1031 		goto failed;
1032 
1033 	if (hw_device->num_logical_accel == 0)
1034 		goto failed;
1035 
1036 	derived_name =
1037 	    malloc(ADF_CFG_MAX_SECTION_LEN_IN_BYTES, M_QAT, M_WAITOK | M_ZERO);
1038 
1039 	for (accel_num = 0; accel_num < hw_device->num_logical_accel;
1040 	     accel_num++) {
1041 		snprintf(derived_name,
1042 			 ADF_CFG_MAX_SECTION_LEN_IN_BYTES,
1043 			 ADF_ACCEL_STR,
1044 			 accel_num);
1045 		ret = adf_cfg_section_add(accel_dev, derived_name);
1046 		if (ret != 0)
1047 			goto failed;
1048 
1049 		ret = adf_cfg_create_accel_section(accel_dev, derived_name);
1050 		if (ret != 0)
1051 			goto failed;
1052 	}
1053 
1054 	ret = 0;
1055 failed:
1056 	free(derived_name, M_QAT);
1057 
1058 	if (ret)
1059 		device_printf(GET_DEV(accel_dev),
1060 			      "Failed to process accel section\n");
1061 
1062 	return ret;
1063 }
1064 
1065 int
1066 adf_cfg_process_section(struct adf_accel_dev *accel_dev,
1067 			const char *sec_name,
1068 			int dev)
1069 {
1070 	if (!strcmp(sec_name, ADF_GENERAL_SEC) ||
1071 	    !strcmp(sec_name, ADF_INLINE_SEC))
1072 		return adf_cfg_process_section_no_op(accel_dev, sec_name);
1073 	else if (!strcmp(sec_name, ADF_KERNEL_SEC) ||
1074 		 !strcmp(sec_name, ADF_KERNEL_SAL_SEC))
1075 		return adf_cfg_process_kernel_section(accel_dev, sec_name);
1076 	else if (!strcmp(sec_name, ADF_ACCEL_SEC))
1077 		return adf_cfg_process_accel_section(accel_dev, sec_name);
1078 	else
1079 		return adf_cfg_process_user_section(accel_dev, sec_name, dev);
1080 }
1081 
1082 int
1083 adf_cfg_cleanup_section(struct adf_accel_dev *accel_dev,
1084 			const char *sec_name,
1085 			int dev)
1086 {
1087 	if (!strcmp(sec_name, ADF_GENERAL_SEC))
1088 		return adf_cfg_cleanup_general_section(accel_dev, sec_name);
1089 	else if (!strcmp(sec_name, ADF_INLINE_SEC))
1090 		return adf_cfg_process_section_no_op(accel_dev, sec_name);
1091 	else if (!strcmp(sec_name, ADF_KERNEL_SEC) ||
1092 		 !strcmp(sec_name, ADF_KERNEL_SAL_SEC))
1093 		return adf_cfg_cleanup_kernel_section(accel_dev, sec_name);
1094 	else if (strstr(sec_name, ADF_ACCEL_SEC))
1095 		return adf_cfg_cleanup_accel_section(accel_dev, sec_name);
1096 	else
1097 		return adf_cfg_cleanup_user_section(accel_dev, sec_name);
1098 }
1099 
1100 int
1101 adf_cfg_setup_irq(struct adf_accel_dev *accel_dev)
1102 {
1103 	int ret = EFAULT;
1104 	struct adf_accel_pci *info_pci_dev = &accel_dev->accel_pci_dev;
1105 	struct adf_cfg_device *cfg_dev = NULL;
1106 	struct msix_entry *msixe = NULL;
1107 	u32 num_msix = 0;
1108 	int index = 0;
1109 	int computed_core = 0;
1110 
1111 	if (!accel_dev || !accel_dev->cfg || !accel_dev->hw_device)
1112 		goto failed;
1113 
1114 	cfg_dev = accel_dev->cfg->dev;
1115 	if (!cfg_dev)
1116 		goto failed;
1117 
1118 	msixe =
1119 	    (struct msix_entry *)accel_dev->accel_pci_dev.msix_entries.entries;
1120 	num_msix = accel_dev->accel_pci_dev.msix_entries.num_entries;
1121 	if (!msixe)
1122 		goto cleanup_and_fail;
1123 
1124 	/*
1125 	 * Here we want to set the affinity of kernel and epoll mode
1126 	 * bundle into user defined value.
1127 	 * Because in adf_isr.c we setup core affinity by round-robin
1128 	 * we need to reset it after device up done.
1129 	 */
1130 	for (index = 0; index < accel_dev->hw_device->num_banks; index++) {
1131 		struct adf_cfg_bundle *bundle = cfg_dev->bundles[index];
1132 
1133 		if (!bundle)
1134 			continue;
1135 
1136 		if (bundle->type != KERNEL &&
1137 		    bundle->polling_mode != ADF_CFG_RESP_EPOLL)
1138 			continue;
1139 
1140 		if (bundle->number >= num_msix)
1141 			goto cleanup_and_fail;
1142 
1143 		computed_core = CPU_FFS(&bundle->affinity_mask) - 1;
1144 		bus_bind_intr(info_pci_dev->pci_dev,
1145 			      msixe[index].irq,
1146 			      computed_core);
1147 	}
1148 	ret = 0;
1149 
1150 cleanup_and_fail:
1151 	adf_cfg_device_clear(cfg_dev, accel_dev);
1152 	free(cfg_dev, M_QAT);
1153 	accel_dev->cfg->dev = NULL;
1154 
1155 failed:
1156 	return ret;
1157 }
1158