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