1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright(c) 2022 Intel Corporation */
3 #include "adf_accel_devices.h"
4 #include "adf_cfg.h"
5 #include "adf_cfg_strings.h"
6 #include "adf_gen2_config.h"
7 #include "adf_common_drv.h"
8 #include "qat_crypto.h"
9 #include "qat_compression.h"
10 #include "adf_transport_access_macros.h"
11 
12 static int adf_gen2_crypto_dev_config(struct adf_accel_dev *accel_dev)
13 {
14 	char key[ADF_CFG_MAX_KEY_LEN_IN_BYTES];
15 	int banks = GET_MAX_BANKS(accel_dev);
16 	int cpus = num_online_cpus();
17 	unsigned long val;
18 	int instances;
19 	int ret;
20 	int i;
21 
22 	if (adf_hw_dev_has_crypto(accel_dev))
23 		instances = min(cpus, banks);
24 	else
25 		instances = 0;
26 
27 	for (i = 0; i < instances; i++) {
28 		val = i;
29 		snprintf(key, sizeof(key), ADF_CY "%d" ADF_RING_ASYM_BANK_NUM, i);
30 		ret = adf_cfg_add_key_value_param(accel_dev, ADF_KERNEL_SEC,
31 						  key, &val, ADF_DEC);
32 		if (ret)
33 			goto err;
34 
35 		snprintf(key, sizeof(key), ADF_CY "%d" ADF_RING_SYM_BANK_NUM, i);
36 		ret = adf_cfg_add_key_value_param(accel_dev, ADF_KERNEL_SEC,
37 						  key, &val, ADF_DEC);
38 		if (ret)
39 			goto err;
40 
41 		snprintf(key, sizeof(key), ADF_CY "%d" ADF_ETRMGR_CORE_AFFINITY,
42 			 i);
43 		ret = adf_cfg_add_key_value_param(accel_dev, ADF_KERNEL_SEC,
44 						  key, &val, ADF_DEC);
45 		if (ret)
46 			goto err;
47 
48 		snprintf(key, sizeof(key), ADF_CY "%d" ADF_RING_ASYM_SIZE, i);
49 		val = 128;
50 		ret = adf_cfg_add_key_value_param(accel_dev, ADF_KERNEL_SEC,
51 						  key, &val, ADF_DEC);
52 		if (ret)
53 			goto err;
54 
55 		val = 512;
56 		snprintf(key, sizeof(key), ADF_CY "%d" ADF_RING_SYM_SIZE, i);
57 		ret = adf_cfg_add_key_value_param(accel_dev, ADF_KERNEL_SEC,
58 						  key, &val, ADF_DEC);
59 		if (ret)
60 			goto err;
61 
62 		val = 0;
63 		snprintf(key, sizeof(key), ADF_CY "%d" ADF_RING_ASYM_TX, i);
64 		ret = adf_cfg_add_key_value_param(accel_dev, ADF_KERNEL_SEC,
65 						  key, &val, ADF_DEC);
66 		if (ret)
67 			goto err;
68 
69 		val = 2;
70 		snprintf(key, sizeof(key), ADF_CY "%d" ADF_RING_SYM_TX, i);
71 		ret = adf_cfg_add_key_value_param(accel_dev, ADF_KERNEL_SEC,
72 						  key, &val, ADF_DEC);
73 		if (ret)
74 			goto err;
75 
76 		val = 8;
77 		snprintf(key, sizeof(key), ADF_CY "%d" ADF_RING_ASYM_RX, i);
78 		ret = adf_cfg_add_key_value_param(accel_dev, ADF_KERNEL_SEC,
79 						  key, &val, ADF_DEC);
80 		if (ret)
81 			goto err;
82 
83 		val = 10;
84 		snprintf(key, sizeof(key), ADF_CY "%d" ADF_RING_SYM_RX, i);
85 		ret = adf_cfg_add_key_value_param(accel_dev, ADF_KERNEL_SEC,
86 						  key, &val, ADF_DEC);
87 		if (ret)
88 			goto err;
89 
90 		val = ADF_COALESCING_DEF_TIME;
91 		snprintf(key, sizeof(key), ADF_ETRMGR_COALESCE_TIMER_FORMAT, i);
92 		ret = adf_cfg_add_key_value_param(accel_dev, "Accelerator0",
93 						  key, &val, ADF_DEC);
94 		if (ret)
95 			goto err;
96 	}
97 
98 	val = i;
99 	ret = adf_cfg_add_key_value_param(accel_dev, ADF_KERNEL_SEC, ADF_NUM_CY,
100 					  &val, ADF_DEC);
101 	if (ret)
102 		goto err;
103 
104 	return ret;
105 
106 err:
107 	dev_err(&GET_DEV(accel_dev), "Failed to add configuration for crypto\n");
108 	return ret;
109 }
110 
111 static int adf_gen2_comp_dev_config(struct adf_accel_dev *accel_dev)
112 {
113 	char key[ADF_CFG_MAX_KEY_LEN_IN_BYTES];
114 	int banks = GET_MAX_BANKS(accel_dev);
115 	int cpus = num_online_cpus();
116 	unsigned long val;
117 	int instances;
118 	int ret;
119 	int i;
120 
121 	if (adf_hw_dev_has_compression(accel_dev))
122 		instances = min(cpus, banks);
123 	else
124 		instances = 0;
125 
126 	for (i = 0; i < instances; i++) {
127 		val = i;
128 		snprintf(key, sizeof(key), ADF_DC "%d" ADF_RING_DC_BANK_NUM, i);
129 		ret = adf_cfg_add_key_value_param(accel_dev, ADF_KERNEL_SEC,
130 						  key, &val, ADF_DEC);
131 		if (ret)
132 			goto err;
133 
134 		val = 512;
135 		snprintf(key, sizeof(key), ADF_DC "%d" ADF_RING_DC_SIZE, i);
136 		ret = adf_cfg_add_key_value_param(accel_dev, ADF_KERNEL_SEC,
137 						  key, &val, ADF_DEC);
138 		if (ret)
139 			goto err;
140 
141 		val = 6;
142 		snprintf(key, sizeof(key), ADF_DC "%d" ADF_RING_DC_TX, i);
143 		ret = adf_cfg_add_key_value_param(accel_dev, ADF_KERNEL_SEC,
144 						  key, &val, ADF_DEC);
145 		if (ret)
146 			goto err;
147 
148 		val = 14;
149 		snprintf(key, sizeof(key), ADF_DC "%d" ADF_RING_DC_RX, i);
150 		ret = adf_cfg_add_key_value_param(accel_dev, ADF_KERNEL_SEC,
151 						  key, &val, ADF_DEC);
152 		if (ret)
153 			goto err;
154 	}
155 
156 	val = i;
157 	ret = adf_cfg_add_key_value_param(accel_dev, ADF_KERNEL_SEC, ADF_NUM_DC,
158 					  &val, ADF_DEC);
159 	if (ret)
160 		return ret;
161 
162 	return ret;
163 
164 err:
165 	dev_err(&GET_DEV(accel_dev), "Failed to add configuration for compression\n");
166 	return ret;
167 }
168 
169 /**
170  * adf_gen2_dev_config() - create dev config required to create instances
171  *
172  * @accel_dev: Pointer to acceleration device.
173  *
174  * Function creates device configuration required to create instances
175  *
176  * Return: 0 on success, error code otherwise.
177  */
178 int adf_gen2_dev_config(struct adf_accel_dev *accel_dev)
179 {
180 	int ret;
181 
182 	ret = adf_cfg_section_add(accel_dev, ADF_KERNEL_SEC);
183 	if (ret)
184 		goto err;
185 
186 	ret = adf_cfg_section_add(accel_dev, "Accelerator0");
187 	if (ret)
188 		goto err;
189 
190 	ret = adf_gen2_crypto_dev_config(accel_dev);
191 	if (ret)
192 		goto err;
193 
194 	ret = adf_gen2_comp_dev_config(accel_dev);
195 	if (ret)
196 		goto err;
197 
198 	set_bit(ADF_STATUS_CONFIGURED, &accel_dev->status);
199 
200 	return ret;
201 
202 err:
203 	dev_err(&GET_DEV(accel_dev), "Failed to configure QAT driver\n");
204 	return ret;
205 }
206 EXPORT_SYMBOL_GPL(adf_gen2_dev_config);
207