xref: /dpdk/drivers/net/hns3/hns3_rss.c (revision e4f0e2158b8e210065e91f45fd83aee118cbbd96)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2018-2021 HiSilicon Limited.
3  */
4 
5 #include <rte_ethdev.h>
6 #include <rte_io.h>
7 #include <rte_malloc.h>
8 
9 #include "hns3_ethdev.h"
10 #include "hns3_logs.h"
11 
12 /* Default hash keys */
13 const uint8_t hns3_hash_key[HNS3_RSS_KEY_SIZE] = {
14 	0x6D, 0x5A, 0x56, 0xDA, 0x25, 0x5B, 0x0E, 0xC2,
15 	0x41, 0x67, 0x25, 0x3D, 0x43, 0xA3, 0x8F, 0xB0,
16 	0xD0, 0xCA, 0x2B, 0xCB, 0xAE, 0x7B, 0x30, 0xB4,
17 	0x77, 0xCB, 0x2D, 0xA3, 0x80, 0x30, 0xF2, 0x0C,
18 	0x6A, 0x42, 0xB7, 0x3B, 0xBE, 0xAC, 0x01, 0xFA
19 };
20 
21 const uint8_t hns3_hash_func_map[] = {
22 	[RTE_ETH_HASH_FUNCTION_DEFAULT] = HNS3_RSS_HASH_ALGO_TOEPLITZ,
23 	[RTE_ETH_HASH_FUNCTION_TOEPLITZ] = HNS3_RSS_HASH_ALGO_TOEPLITZ,
24 	[RTE_ETH_HASH_FUNCTION_SIMPLE_XOR] = HNS3_RSS_HASH_ALGO_SIMPLE,
25 	[RTE_ETH_HASH_FUNCTION_SYMMETRIC_TOEPLITZ] = HNS3_RSS_HASH_ALGO_SYMMETRIC_TOEP,
26 };
27 
28 enum hns3_rss_tuple_type {
29 	HNS3_RSS_IP_TUPLE,
30 	HNS3_RSS_IP_L4_TUPLE,
31 };
32 
33 static const struct {
34 	uint64_t rss_types;
35 	uint16_t tuple_type;
36 	uint64_t rss_field;
37 	uint64_t tuple_mask;
38 } hns3_set_tuple_table[] = {
39 	/* IPV4-FRAG */
40 	{ RTE_ETH_RSS_FRAG_IPV4 | RTE_ETH_RSS_L3_SRC_ONLY,
41 	  HNS3_RSS_IP_TUPLE,
42 	  BIT_ULL(HNS3_RSS_FIELD_IPV4_EN_FRAG_IP_S),
43 	  HNS3_RSS_TUPLE_IPV4_FLAG_M },
44 	{ RTE_ETH_RSS_FRAG_IPV4 | RTE_ETH_RSS_L3_DST_ONLY,
45 	  HNS3_RSS_IP_TUPLE,
46 	  BIT_ULL(HNS3_RSS_FIELD_IPV4_EN_FRAG_IP_D),
47 	  HNS3_RSS_TUPLE_IPV4_FLAG_M },
48 	{ RTE_ETH_RSS_FRAG_IPV4,
49 	  HNS3_RSS_IP_TUPLE,
50 	  BIT_ULL(HNS3_RSS_FIELD_IPV4_EN_FRAG_IP_S) |
51 	  BIT_ULL(HNS3_RSS_FIELD_IPV4_EN_FRAG_IP_D),
52 	  HNS3_RSS_TUPLE_IPV4_FLAG_M },
53 
54 	/* IPV4 */
55 	{ RTE_ETH_RSS_IPV4 | RTE_ETH_RSS_L3_SRC_ONLY,
56 	  HNS3_RSS_IP_TUPLE,
57 	  BIT_ULL(HNS3_RSS_FIELD_IPV4_EN_NONFRAG_IP_S),
58 	  HNS3_RSS_TUPLE_IPV4_NONF_M },
59 	{ RTE_ETH_RSS_IPV4 | RTE_ETH_RSS_L3_DST_ONLY,
60 	  HNS3_RSS_IP_TUPLE,
61 	  BIT_ULL(HNS3_RSS_FIELD_IPV4_EN_NONFRAG_IP_D),
62 	  HNS3_RSS_TUPLE_IPV4_NONF_M },
63 	{ RTE_ETH_RSS_IPV4,
64 	  HNS3_RSS_IP_TUPLE,
65 	  BIT_ULL(HNS3_RSS_FIELD_IPV4_EN_NONFRAG_IP_S) |
66 	  BIT_ULL(HNS3_RSS_FIELD_IPV4_EN_NONFRAG_IP_D),
67 	  HNS3_RSS_TUPLE_IPV4_NONF_M },
68 
69 	/* IPV4-OTHER */
70 	{ RTE_ETH_RSS_NONFRAG_IPV4_OTHER | RTE_ETH_RSS_L3_SRC_ONLY,
71 	  HNS3_RSS_IP_TUPLE,
72 	  BIT_ULL(HNS3_RSS_FIELD_IPV4_EN_NONFRAG_IP_S),
73 	  HNS3_RSS_TUPLE_IPV4_NONF_M },
74 	{ RTE_ETH_RSS_NONFRAG_IPV4_OTHER | RTE_ETH_RSS_L3_DST_ONLY,
75 	  HNS3_RSS_IP_TUPLE,
76 	  BIT_ULL(HNS3_RSS_FIELD_IPV4_EN_NONFRAG_IP_D),
77 	  HNS3_RSS_TUPLE_IPV4_NONF_M },
78 	{ RTE_ETH_RSS_NONFRAG_IPV4_OTHER,
79 	  HNS3_RSS_IP_TUPLE,
80 	  BIT_ULL(HNS3_RSS_FIELD_IPV4_EN_NONFRAG_IP_S) |
81 	  BIT_ULL(HNS3_RSS_FIELD_IPV4_EN_NONFRAG_IP_D),
82 	  HNS3_RSS_TUPLE_IPV4_NONF_M },
83 
84 	/* IPV4-TCP */
85 	{ RTE_ETH_RSS_NONFRAG_IPV4_TCP | RTE_ETH_RSS_L3_SRC_ONLY,
86 	  HNS3_RSS_IP_L4_TUPLE,
87 	  BIT_ULL(HNS3_RSS_FIELD_IPV4_TCP_EN_IP_S),
88 	  HNS3_RSS_TUPLE_IPV4_TCP_M },
89 	{ RTE_ETH_RSS_NONFRAG_IPV4_TCP | RTE_ETH_RSS_L3_DST_ONLY,
90 	  HNS3_RSS_IP_L4_TUPLE,
91 	  BIT_ULL(HNS3_RSS_FIELD_IPV4_TCP_EN_IP_D),
92 	  HNS3_RSS_TUPLE_IPV4_TCP_M },
93 	{ RTE_ETH_RSS_NONFRAG_IPV4_TCP | RTE_ETH_RSS_L4_SRC_ONLY,
94 	  HNS3_RSS_IP_L4_TUPLE,
95 	  BIT_ULL(HNS3_RSS_FIELD_IPV4_TCP_EN_TCP_S),
96 	  HNS3_RSS_TUPLE_IPV4_TCP_M },
97 	{ RTE_ETH_RSS_NONFRAG_IPV4_TCP | RTE_ETH_RSS_L4_DST_ONLY,
98 	  HNS3_RSS_IP_L4_TUPLE,
99 	  BIT_ULL(HNS3_RSS_FIELD_IPV4_TCP_EN_TCP_D),
100 	  HNS3_RSS_TUPLE_IPV4_TCP_M },
101 	{ RTE_ETH_RSS_NONFRAG_IPV4_TCP,
102 	  HNS3_RSS_IP_L4_TUPLE,
103 	  BIT_ULL(HNS3_RSS_FIELD_IPV4_TCP_EN_IP_S) |
104 	  BIT_ULL(HNS3_RSS_FIELD_IPV4_TCP_EN_IP_D) |
105 	  BIT_ULL(HNS3_RSS_FIELD_IPV4_TCP_EN_TCP_S) |
106 	  BIT_ULL(HNS3_RSS_FIELD_IPV4_TCP_EN_TCP_D),
107 	  HNS3_RSS_TUPLE_IPV4_TCP_M },
108 
109 	/* IPV4-UDP */
110 	{ RTE_ETH_RSS_NONFRAG_IPV4_UDP | RTE_ETH_RSS_L3_SRC_ONLY,
111 	  HNS3_RSS_IP_L4_TUPLE,
112 	  BIT_ULL(HNS3_RSS_FIELD_IPV4_UDP_EN_IP_S),
113 	  HNS3_RSS_TUPLE_IPV4_UDP_M },
114 	{ RTE_ETH_RSS_NONFRAG_IPV4_UDP | RTE_ETH_RSS_L3_DST_ONLY,
115 	  HNS3_RSS_IP_L4_TUPLE,
116 	  BIT_ULL(HNS3_RSS_FIELD_IPV4_UDP_EN_IP_D),
117 	  HNS3_RSS_TUPLE_IPV4_UDP_M },
118 	{ RTE_ETH_RSS_NONFRAG_IPV4_UDP | RTE_ETH_RSS_L4_SRC_ONLY,
119 	  HNS3_RSS_IP_L4_TUPLE,
120 	  BIT_ULL(HNS3_RSS_FIELD_IPV4_UDP_EN_UDP_S),
121 	  HNS3_RSS_TUPLE_IPV4_UDP_M },
122 	{ RTE_ETH_RSS_NONFRAG_IPV4_UDP | RTE_ETH_RSS_L4_DST_ONLY,
123 	  HNS3_RSS_IP_L4_TUPLE,
124 	  BIT_ULL(HNS3_RSS_FIELD_IPV4_UDP_EN_UDP_D),
125 	  HNS3_RSS_TUPLE_IPV4_UDP_M },
126 	{ RTE_ETH_RSS_NONFRAG_IPV4_UDP,
127 	  HNS3_RSS_IP_L4_TUPLE,
128 	  BIT_ULL(HNS3_RSS_FIELD_IPV4_UDP_EN_IP_S) |
129 	  BIT_ULL(HNS3_RSS_FIELD_IPV4_UDP_EN_IP_D) |
130 	  BIT_ULL(HNS3_RSS_FIELD_IPV4_UDP_EN_UDP_S) |
131 	  BIT_ULL(HNS3_RSS_FIELD_IPV4_UDP_EN_UDP_D),
132 	  HNS3_RSS_TUPLE_IPV4_UDP_M },
133 
134 	/* IPV4-SCTP */
135 	{ RTE_ETH_RSS_NONFRAG_IPV4_SCTP | RTE_ETH_RSS_L3_SRC_ONLY,
136 	  HNS3_RSS_IP_L4_TUPLE,
137 	  BIT_ULL(HNS3_RSS_FIELD_IPV4_SCTP_EN_IP_S),
138 	  HNS3_RSS_TUPLE_IPV4_SCTP_M },
139 	{ RTE_ETH_RSS_NONFRAG_IPV4_SCTP | RTE_ETH_RSS_L3_DST_ONLY,
140 	  HNS3_RSS_IP_L4_TUPLE,
141 	  BIT_ULL(HNS3_RSS_FIELD_IPV4_SCTP_EN_IP_D),
142 	  HNS3_RSS_TUPLE_IPV4_SCTP_M },
143 	{ RTE_ETH_RSS_NONFRAG_IPV4_SCTP | RTE_ETH_RSS_L4_SRC_ONLY,
144 	  HNS3_RSS_IP_L4_TUPLE,
145 	  BIT_ULL(HNS3_RSS_FIELD_IPV4_SCTP_EN_SCTP_S),
146 	  HNS3_RSS_TUPLE_IPV4_SCTP_M },
147 	{ RTE_ETH_RSS_NONFRAG_IPV4_SCTP | RTE_ETH_RSS_L4_DST_ONLY,
148 	  HNS3_RSS_IP_L4_TUPLE,
149 	  BIT_ULL(HNS3_RSS_FIELD_IPV4_SCTP_EN_SCTP_D),
150 	  HNS3_RSS_TUPLE_IPV4_SCTP_M },
151 	{ RTE_ETH_RSS_NONFRAG_IPV4_SCTP,
152 	  HNS3_RSS_IP_L4_TUPLE,
153 	  BIT_ULL(HNS3_RSS_FIELD_IPV4_SCTP_EN_IP_S) |
154 	  BIT_ULL(HNS3_RSS_FIELD_IPV4_SCTP_EN_IP_D) |
155 	  BIT_ULL(HNS3_RSS_FIELD_IPV4_SCTP_EN_SCTP_S) |
156 	  BIT_ULL(HNS3_RSS_FIELD_IPV4_SCTP_EN_SCTP_D) |
157 	  BIT_ULL(HNS3_RSS_FIELD_IPV4_SCTP_EN_SCTP_VER),
158 	  HNS3_RSS_TUPLE_IPV4_SCTP_M },
159 
160 	/* IPV6-FRAG */
161 	{ RTE_ETH_RSS_FRAG_IPV6 | RTE_ETH_RSS_L3_SRC_ONLY,
162 	  HNS3_RSS_IP_TUPLE,
163 	  BIT_ULL(HNS3_RSS_FIELD_IPV6_FRAG_IP_S),
164 	  HNS3_RSS_TUPLE_IPV6_FLAG_M },
165 	{ RTE_ETH_RSS_FRAG_IPV6 | RTE_ETH_RSS_L3_DST_ONLY,
166 	  HNS3_RSS_IP_TUPLE,
167 	  BIT_ULL(HNS3_RSS_FIELD_IPV6_FRAG_IP_D),
168 	  HNS3_RSS_TUPLE_IPV6_FLAG_M },
169 	{ RTE_ETH_RSS_FRAG_IPV6,
170 	  HNS3_RSS_IP_TUPLE,
171 	  BIT_ULL(HNS3_RSS_FIELD_IPV6_FRAG_IP_S) |
172 	  BIT_ULL(HNS3_RSS_FIELD_IPV6_FRAG_IP_D),
173 	  HNS3_RSS_TUPLE_IPV6_FLAG_M },
174 
175 	/* IPV6 */
176 	{ RTE_ETH_RSS_IPV6 | RTE_ETH_RSS_L3_SRC_ONLY,
177 	  HNS3_RSS_IP_TUPLE,
178 	  BIT_ULL(HNS3_RSS_FIELD_IPV6_NONFRAG_IP_S),
179 	  HNS3_RSS_TUPLE_IPV6_NONF_M },
180 	{ RTE_ETH_RSS_IPV6 | RTE_ETH_RSS_L3_DST_ONLY,
181 	  HNS3_RSS_IP_TUPLE,
182 	  BIT_ULL(HNS3_RSS_FIELD_IPV6_NONFRAG_IP_D),
183 	  HNS3_RSS_TUPLE_IPV6_NONF_M },
184 	{ RTE_ETH_RSS_IPV6,
185 	  HNS3_RSS_IP_TUPLE,
186 	  BIT_ULL(HNS3_RSS_FIELD_IPV6_NONFRAG_IP_S) |
187 	  BIT_ULL(HNS3_RSS_FIELD_IPV6_NONFRAG_IP_D),
188 	  HNS3_RSS_TUPLE_IPV6_NONF_M },
189 
190 	/* IPV6-OTHER */
191 	{ RTE_ETH_RSS_NONFRAG_IPV6_OTHER | RTE_ETH_RSS_L3_SRC_ONLY,
192 	  HNS3_RSS_IP_TUPLE,
193 	  BIT_ULL(HNS3_RSS_FIELD_IPV6_NONFRAG_IP_S),
194 	  HNS3_RSS_TUPLE_IPV6_NONF_M },
195 	{ RTE_ETH_RSS_NONFRAG_IPV6_OTHER | RTE_ETH_RSS_L3_DST_ONLY,
196 	  HNS3_RSS_IP_TUPLE,
197 	  BIT_ULL(HNS3_RSS_FIELD_IPV6_NONFRAG_IP_D),
198 	  HNS3_RSS_TUPLE_IPV6_NONF_M },
199 	{ RTE_ETH_RSS_NONFRAG_IPV6_OTHER,
200 	  HNS3_RSS_IP_TUPLE,
201 	  BIT_ULL(HNS3_RSS_FIELD_IPV6_NONFRAG_IP_S) |
202 	  BIT_ULL(HNS3_RSS_FIELD_IPV6_NONFRAG_IP_D),
203 	  HNS3_RSS_TUPLE_IPV6_NONF_M },
204 
205 	/* IPV6-TCP */
206 	{ RTE_ETH_RSS_NONFRAG_IPV6_TCP | RTE_ETH_RSS_L3_SRC_ONLY,
207 	  HNS3_RSS_IP_L4_TUPLE,
208 	  BIT_ULL(HNS3_RSS_FIELD_IPV6_TCP_EN_IP_S),
209 	  HNS3_RSS_TUPLE_IPV6_TCP_M },
210 	{ RTE_ETH_RSS_NONFRAG_IPV6_TCP | RTE_ETH_RSS_L3_DST_ONLY,
211 	  HNS3_RSS_IP_L4_TUPLE,
212 	  BIT_ULL(HNS3_RSS_FIELD_IPV6_TCP_EN_IP_D),
213 	  HNS3_RSS_TUPLE_IPV6_TCP_M },
214 	{ RTE_ETH_RSS_NONFRAG_IPV6_TCP | RTE_ETH_RSS_L4_SRC_ONLY,
215 	  HNS3_RSS_IP_L4_TUPLE,
216 	  BIT_ULL(HNS3_RSS_FIELD_IPV6_TCP_EN_TCP_S),
217 	  HNS3_RSS_TUPLE_IPV6_TCP_M },
218 	{ RTE_ETH_RSS_NONFRAG_IPV6_TCP | RTE_ETH_RSS_L4_DST_ONLY,
219 	  HNS3_RSS_IP_L4_TUPLE,
220 	  BIT_ULL(HNS3_RSS_FIELD_IPV6_TCP_EN_TCP_D),
221 	  HNS3_RSS_TUPLE_IPV6_TCP_M },
222 	{ RTE_ETH_RSS_NONFRAG_IPV6_TCP,
223 	  HNS3_RSS_IP_L4_TUPLE,
224 	  BIT_ULL(HNS3_RSS_FIELD_IPV6_TCP_EN_IP_S) |
225 	  BIT_ULL(HNS3_RSS_FIELD_IPV6_TCP_EN_IP_D) |
226 	  BIT_ULL(HNS3_RSS_FIELD_IPV6_TCP_EN_TCP_S) |
227 	  BIT_ULL(HNS3_RSS_FIELD_IPV6_TCP_EN_TCP_D),
228 	  HNS3_RSS_TUPLE_IPV6_TCP_M },
229 
230 	/* IPV6-UDP */
231 	{ RTE_ETH_RSS_NONFRAG_IPV6_UDP | RTE_ETH_RSS_L3_SRC_ONLY,
232 	  HNS3_RSS_IP_L4_TUPLE,
233 	  BIT_ULL(HNS3_RSS_FIELD_IPV6_UDP_EN_IP_S),
234 	  HNS3_RSS_TUPLE_IPV6_UDP_M },
235 	{ RTE_ETH_RSS_NONFRAG_IPV6_UDP | RTE_ETH_RSS_L3_DST_ONLY,
236 	  HNS3_RSS_IP_L4_TUPLE,
237 	  BIT_ULL(HNS3_RSS_FIELD_IPV6_UDP_EN_IP_D),
238 	  HNS3_RSS_TUPLE_IPV6_UDP_M },
239 	{ RTE_ETH_RSS_NONFRAG_IPV6_UDP | RTE_ETH_RSS_L4_SRC_ONLY,
240 	  HNS3_RSS_IP_L4_TUPLE,
241 	  BIT_ULL(HNS3_RSS_FIELD_IPV6_UDP_EN_UDP_S),
242 	  HNS3_RSS_TUPLE_IPV6_UDP_M },
243 	{ RTE_ETH_RSS_NONFRAG_IPV6_UDP | RTE_ETH_RSS_L4_DST_ONLY,
244 	  HNS3_RSS_IP_L4_TUPLE,
245 	  BIT_ULL(HNS3_RSS_FIELD_IPV6_UDP_EN_UDP_D),
246 	  HNS3_RSS_TUPLE_IPV6_UDP_M },
247 	{ RTE_ETH_RSS_NONFRAG_IPV6_UDP,
248 	  HNS3_RSS_IP_L4_TUPLE,
249 	  BIT_ULL(HNS3_RSS_FIELD_IPV6_UDP_EN_IP_S) |
250 	  BIT_ULL(HNS3_RSS_FIELD_IPV6_UDP_EN_IP_D) |
251 	  BIT_ULL(HNS3_RSS_FIELD_IPV6_UDP_EN_UDP_S) |
252 	  BIT_ULL(HNS3_RSS_FIELD_IPV6_UDP_EN_UDP_D),
253 	  HNS3_RSS_TUPLE_IPV6_UDP_M },
254 
255 	/* IPV6-SCTP */
256 	{ RTE_ETH_RSS_NONFRAG_IPV6_SCTP | RTE_ETH_RSS_L3_SRC_ONLY,
257 	  HNS3_RSS_IP_L4_TUPLE,
258 	  BIT_ULL(HNS3_RSS_FIELD_IPV6_SCTP_EN_IP_S),
259 	  HNS3_RSS_TUPLE_IPV6_SCTP_M },
260 	{ RTE_ETH_RSS_NONFRAG_IPV6_SCTP | RTE_ETH_RSS_L3_DST_ONLY,
261 	  HNS3_RSS_IP_L4_TUPLE,
262 	  BIT_ULL(HNS3_RSS_FIELD_IPV6_SCTP_EN_IP_D),
263 	  HNS3_RSS_TUPLE_IPV6_SCTP_M },
264 	{ RTE_ETH_RSS_NONFRAG_IPV6_SCTP | RTE_ETH_RSS_L4_SRC_ONLY,
265 	  HNS3_RSS_IP_L4_TUPLE,
266 	  BIT_ULL(HNS3_RSS_FIELD_IPV6_SCTP_EN_SCTP_S),
267 	  HNS3_RSS_TUPLE_IPV6_SCTP_M },
268 	{ RTE_ETH_RSS_NONFRAG_IPV6_SCTP | RTE_ETH_RSS_L4_DST_ONLY,
269 	  HNS3_RSS_IP_L4_TUPLE,
270 	  BIT_ULL(HNS3_RSS_FIELD_IPV6_SCTP_EN_SCTP_D),
271 	  HNS3_RSS_TUPLE_IPV6_SCTP_M },
272 	{ RTE_ETH_RSS_NONFRAG_IPV6_SCTP,
273 	  HNS3_RSS_IP_L4_TUPLE,
274 	  BIT_ULL(HNS3_RSS_FIELD_IPV6_SCTP_EN_IP_S) |
275 	  BIT_ULL(HNS3_RSS_FIELD_IPV6_SCTP_EN_IP_D) |
276 	  BIT_ULL(HNS3_RSS_FIELD_IPV6_SCTP_EN_SCTP_D) |
277 	  BIT_ULL(HNS3_RSS_FIELD_IPV6_SCTP_EN_SCTP_S) |
278 	  BIT_ULL(HNS3_RSS_FIELD_IPV6_SCTP_EN_SCTP_VER),
279 	  HNS3_RSS_TUPLE_IPV6_SCTP_M },
280 };
281 
282 /*
283  * rss_generic_config command function, opcode:0x0D01.
284  * Used to set algorithm and hash key of RSS.
285  */
286 int
287 hns3_rss_set_algo_key(struct hns3_hw *hw, uint8_t hash_algo,
288 		      const uint8_t *key, uint8_t key_len)
289 {
290 	struct hns3_rss_generic_config_cmd *req;
291 	struct hns3_cmd_desc desc;
292 	const uint8_t *cur_key;
293 	uint16_t cur_key_size;
294 	uint16_t max_bd_num;
295 	uint16_t idx;
296 	int ret;
297 
298 	req = (struct hns3_rss_generic_config_cmd *)desc.data;
299 
300 	max_bd_num = DIV_ROUND_UP(key_len, HNS3_RSS_HASH_KEY_NUM);
301 	for (idx = 0; idx < max_bd_num; idx++) {
302 		hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_RSS_GENERIC_CONFIG,
303 					  false);
304 
305 		req->hash_config |= (hash_algo & HNS3_RSS_HASH_ALGO_MASK);
306 		req->hash_config |= (idx << HNS3_RSS_HASH_KEY_OFFSET_B);
307 
308 		if (idx == max_bd_num - 1 &&
309 		    (key_len % HNS3_RSS_HASH_KEY_NUM) != 0)
310 			cur_key_size = key_len % HNS3_RSS_HASH_KEY_NUM;
311 		else
312 			cur_key_size = HNS3_RSS_HASH_KEY_NUM;
313 
314 		cur_key = key + idx * HNS3_RSS_HASH_KEY_NUM;
315 		memcpy(req->hash_key, cur_key, cur_key_size);
316 
317 		ret = hns3_cmd_send(hw, &desc, 1);
318 		if (ret) {
319 			hns3_err(hw, "Configure RSS algo key failed %d", ret);
320 			return ret;
321 		}
322 	}
323 
324 	return 0;
325 }
326 
327 int
328 hns3_rss_get_algo_key(struct hns3_hw *hw,  uint8_t *hash_algo,
329 		      uint8_t *key, uint8_t key_len)
330 {
331 	struct hns3_rss_generic_config_cmd *req;
332 	struct hns3_cmd_desc desc;
333 	uint16_t cur_key_size;
334 	uint16_t max_bd_num;
335 	uint8_t *cur_key;
336 	uint16_t idx;
337 	int ret;
338 
339 	req = (struct hns3_rss_generic_config_cmd *)desc.data;
340 	max_bd_num = DIV_ROUND_UP(key_len, HNS3_RSS_HASH_KEY_NUM);
341 	for (idx = 0; idx < max_bd_num; idx++) {
342 		hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_RSS_GENERIC_CONFIG,
343 					  true);
344 
345 		req->hash_config |= (idx << HNS3_RSS_HASH_KEY_OFFSET_B);
346 		ret = hns3_cmd_send(hw, &desc, 1);
347 		if (ret) {
348 			hns3_err(hw, "fail to obtain RSS algo and key from firmware, ret = %d",
349 				 ret);
350 			return ret;
351 		}
352 
353 		if (idx == 0)
354 			*hash_algo = req->hash_config & HNS3_RSS_HASH_ALGO_MASK;
355 
356 		if (idx == max_bd_num - 1 &&
357 		    (key_len % HNS3_RSS_HASH_KEY_NUM) != 0)
358 			cur_key_size = key_len % HNS3_RSS_HASH_KEY_NUM;
359 		else
360 			cur_key_size = HNS3_RSS_HASH_KEY_NUM;
361 
362 		cur_key = key + idx * HNS3_RSS_HASH_KEY_NUM;
363 		memcpy(cur_key, req->hash_key, cur_key_size);
364 	}
365 
366 	return 0;
367 }
368 
369 /*
370  * rss_indirection_table command function, opcode:0x0D07.
371  * Used to configure the indirection table of rss.
372  */
373 int
374 hns3_set_rss_indir_table(struct hns3_hw *hw, uint16_t *indir, uint16_t size)
375 {
376 	struct hns3_rss_indirection_table_cmd *req;
377 	uint16_t max_bd_num, cfg_tbl_size;
378 	struct hns3_cmd_desc desc;
379 	uint8_t qid_msb_off;
380 	uint8_t qid_msb_val;
381 	uint16_t q_id;
382 	uint16_t i, j;
383 	int ret;
384 
385 	req = (struct hns3_rss_indirection_table_cmd *)desc.data;
386 	max_bd_num = DIV_ROUND_UP(size, HNS3_RSS_CFG_TBL_SIZE);
387 	for (i = 0; i < max_bd_num; i++) {
388 		hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_RSS_INDIR_TABLE,
389 					  false);
390 		req->start_table_index =
391 				rte_cpu_to_le_16(i * HNS3_RSS_CFG_TBL_SIZE);
392 		req->rss_set_bitmap = rte_cpu_to_le_16(HNS3_RSS_SET_BITMAP_MSK);
393 
394 		if (i == max_bd_num - 1 && (size % HNS3_RSS_CFG_TBL_SIZE) != 0)
395 			cfg_tbl_size = size % HNS3_RSS_CFG_TBL_SIZE;
396 		else
397 			cfg_tbl_size = HNS3_RSS_CFG_TBL_SIZE;
398 
399 		for (j = 0; j < cfg_tbl_size; j++) {
400 			q_id = indir[i * HNS3_RSS_CFG_TBL_SIZE + j];
401 			req->rss_result_l[j] = q_id & 0xff;
402 
403 			qid_msb_off =
404 				j * HNS3_RSS_CFG_TBL_BW_H / HNS3_BITS_PER_BYTE;
405 			qid_msb_val = (q_id >> HNS3_RSS_CFG_TBL_BW_L & 0x1)
406 					<< (j * HNS3_RSS_CFG_TBL_BW_H %
407 					HNS3_BITS_PER_BYTE);
408 			req->rss_result_h[qid_msb_off] |= qid_msb_val;
409 		}
410 
411 		ret = hns3_cmd_send(hw, &desc, 1);
412 		if (ret) {
413 			hns3_err(hw,
414 				 "Sets RSS indirection table failed %d size %u",
415 				 ret, size);
416 			return ret;
417 		}
418 	}
419 
420 	return 0;
421 }
422 
423 static int
424 hns3_get_rss_indir_table(struct hns3_hw *hw, uint16_t *indir, uint16_t size)
425 {
426 	struct hns3_rss_indirection_table_cmd *req;
427 	uint16_t max_bd_num, cfg_tbl_size;
428 	uint8_t qid_msb_off, qid_msb_idx;
429 	struct hns3_cmd_desc desc;
430 	uint16_t q_id, q_hi, q_lo;
431 	uint8_t rss_result_h;
432 	uint16_t i, j;
433 	int ret;
434 
435 	req = (struct hns3_rss_indirection_table_cmd *)desc.data;
436 	max_bd_num = DIV_ROUND_UP(size, HNS3_RSS_CFG_TBL_SIZE);
437 	for (i = 0; i < max_bd_num; i++) {
438 		hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_RSS_INDIR_TABLE,
439 					  true);
440 		req->start_table_index =
441 				rte_cpu_to_le_16(i * HNS3_RSS_CFG_TBL_SIZE);
442 		ret = hns3_cmd_send(hw, &desc, 1);
443 		if (ret) {
444 			hns3_err(hw, "fail to get RSS indirection table from firmware, ret = %d",
445 				 ret);
446 			return ret;
447 		}
448 
449 		if (i == max_bd_num - 1 && (size % HNS3_RSS_CFG_TBL_SIZE) != 0)
450 			cfg_tbl_size = size % HNS3_RSS_CFG_TBL_SIZE;
451 		else
452 			cfg_tbl_size = HNS3_RSS_CFG_TBL_SIZE;
453 
454 		for (j = 0; j < cfg_tbl_size; j++) {
455 			qid_msb_idx =
456 				j * HNS3_RSS_CFG_TBL_BW_H / HNS3_BITS_PER_BYTE;
457 			rss_result_h = req->rss_result_h[qid_msb_idx];
458 			qid_msb_off =
459 				j * HNS3_RSS_CFG_TBL_BW_H % HNS3_BITS_PER_BYTE;
460 			q_hi = (rss_result_h >> qid_msb_off) &
461 						HNS3_RSS_CFG_TBL_BW_H_M;
462 			q_lo = req->rss_result_l[j];
463 			q_id = (q_hi << HNS3_RSS_CFG_TBL_BW_L) | q_lo;
464 			indir[i * HNS3_RSS_CFG_TBL_SIZE + j] = q_id;
465 		}
466 	}
467 
468 	return 0;
469 }
470 
471 int
472 hns3_rss_reset_indir_table(struct hns3_hw *hw)
473 {
474 	uint16_t *lut;
475 	int ret;
476 
477 	lut = rte_zmalloc("hns3_rss_lut",
478 			  hw->rss_ind_tbl_size * sizeof(uint16_t), 0);
479 	if (lut == NULL) {
480 		hns3_err(hw, "No hns3_rss_lut memory can be allocated");
481 		return -ENOMEM;
482 	}
483 
484 	ret = hns3_set_rss_indir_table(hw, lut, hw->rss_ind_tbl_size);
485 	if (ret != 0)
486 		hns3_err(hw, "RSS uninit indir table failed, ret = %d.", ret);
487 	else
488 		memcpy(hw->rss_info.rss_indirection_tbl, lut,
489 		       sizeof(uint16_t) * hw->rss_ind_tbl_size);
490 	rte_free(lut);
491 
492 	return ret;
493 }
494 
495 bool
496 hns3_check_rss_types_valid(struct hns3_hw *hw, uint64_t types)
497 {
498 	uint64_t ip_mask = RTE_ETH_RSS_IPV4 | RTE_ETH_RSS_FRAG_IPV4 |
499 			   RTE_ETH_RSS_NONFRAG_IPV4_OTHER |
500 			   RTE_ETH_RSS_IPV6 | RTE_ETH_RSS_FRAG_IPV6 |
501 			   RTE_ETH_RSS_NONFRAG_IPV6_OTHER;
502 	uint64_t ip_l4_mask = RTE_ETH_RSS_NONFRAG_IPV4_TCP |
503 			      RTE_ETH_RSS_NONFRAG_IPV4_UDP |
504 			      RTE_ETH_RSS_NONFRAG_IPV4_SCTP |
505 			      RTE_ETH_RSS_NONFRAG_IPV6_TCP |
506 			      RTE_ETH_RSS_NONFRAG_IPV6_UDP |
507 			      RTE_ETH_RSS_NONFRAG_IPV6_SCTP;
508 	bool has_l4_src_dst = !!(types & HNS3_RSS_SUPPORT_L4_SRC_DST);
509 	bool has_ip_pkt = !!(types & ip_mask);
510 	uint64_t final_types;
511 
512 	if (types == 0)
513 		return true;
514 
515 	if ((types & HNS3_ETH_RSS_SUPPORT) == 0) {
516 		hns3_err(hw, "specified types(0x%" PRIx64 ") are unsupported.",
517 			 types);
518 		return false;
519 	}
520 
521 	if ((types & HNS3_RSS_SUPPORT_L3_SRC_DST) != 0 &&
522 	    (types & HNS3_RSS_SUPPORT_FLOW_TYPE) == 0) {
523 		hns3_err(hw, "IP or IP-TCP/UDP/SCTP packet type isn't specified, L3_SRC/DST_ONLY cannot be set.");
524 		return false;
525 	}
526 
527 	if (has_l4_src_dst && (types & ip_l4_mask) == 0) {
528 		if (!has_ip_pkt) {
529 			hns3_err(hw, "IP-TCP/UDP/SCTP packet type isn't specified, L4_SRC/DST_ONLY cannot be set.");
530 			return false;
531 		}
532 		/*
533 		 * For the case that the types has L4_SRC/DST_ONLY but hasn't
534 		 * IP-TCP/UDP/SCTP packet type, this types is considered valid
535 		 * if it also has IP packet type.
536 		 */
537 		hns3_warn(hw, "L4_SRC/DST_ONLY is ignored because of no including L4 packet.");
538 	}
539 
540 	if ((types & ~HNS3_ETH_RSS_SUPPORT) != 0) {
541 		final_types = types & HNS3_ETH_RSS_SUPPORT;
542 		hns3_warn(hw, "set RSS types based on hardware support, requested:0x%" PRIx64 " configured:0x%" PRIx64 "",
543 			  types, final_types);
544 	}
545 
546 	return true;
547 }
548 
549 uint64_t
550 hns3_rss_calc_tuple_filed(uint64_t rss_hf)
551 {
552 	uint64_t l3_only_mask = RTE_ETH_RSS_L3_SRC_ONLY |
553 				RTE_ETH_RSS_L3_DST_ONLY;
554 	uint64_t l4_only_mask = RTE_ETH_RSS_L4_SRC_ONLY |
555 				RTE_ETH_RSS_L4_DST_ONLY;
556 	uint64_t l3_l4_only_mask = l3_only_mask | l4_only_mask;
557 	bool has_l3_l4_only = !!(rss_hf & l3_l4_only_mask);
558 	bool has_l3_only = !!(rss_hf & l3_only_mask);
559 	uint64_t tuple = 0;
560 	uint32_t i;
561 
562 	for (i = 0; i < RTE_DIM(hns3_set_tuple_table); i++) {
563 		if ((rss_hf & hns3_set_tuple_table[i].rss_types) !=
564 		    hns3_set_tuple_table[i].rss_types)
565 			continue;
566 
567 		if (hns3_set_tuple_table[i].tuple_type == HNS3_RSS_IP_TUPLE) {
568 			if (hns3_set_tuple_table[i].rss_types & l3_only_mask ||
569 			    !has_l3_only)
570 				tuple |= hns3_set_tuple_table[i].rss_field;
571 			continue;
572 		}
573 
574 		/* For IP types with L4, we need check both L3 and L4 */
575 		if (hns3_set_tuple_table[i].rss_types & l3_l4_only_mask ||
576 		    !has_l3_l4_only)
577 			tuple |= hns3_set_tuple_table[i].rss_field;
578 	}
579 
580 	return tuple;
581 }
582 
583 int
584 hns3_set_rss_tuple_field(struct hns3_hw *hw, uint64_t tuple_fields)
585 {
586 	struct hns3_rss_input_tuple_cmd *req;
587 	struct hns3_cmd_desc desc;
588 	int ret;
589 
590 	hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_RSS_INPUT_TUPLE, false);
591 	req = (struct hns3_rss_input_tuple_cmd *)desc.data;
592 	req->tuple_field = rte_cpu_to_le_64(tuple_fields);
593 	ret = hns3_cmd_send(hw, &desc, 1);
594 	if (ret != 0)
595 		hns3_err(hw, "set RSS hash tuple fields failed ret = %d", ret);
596 
597 	return ret;
598 }
599 
600 int
601 hns3_set_rss_tuple_by_rss_hf(struct hns3_hw *hw, uint64_t rss_hf)
602 {
603 	uint64_t tuple_fields;
604 	int ret;
605 
606 	tuple_fields = hns3_rss_calc_tuple_filed(rss_hf);
607 	ret = hns3_set_rss_tuple_field(hw, tuple_fields);
608 	if (ret != 0)
609 		hns3_err(hw, "Update RSS flow types tuples failed, ret = %d",
610 			 ret);
611 
612 	return ret;
613 }
614 
615 /*
616  * Configure RSS hash protocols and hash key.
617  * @param dev
618  *   Pointer to Ethernet device.
619  * @praram rss_conf
620  *   The configuration select of  rss key size and tuple flow_types.
621  * @return
622  *   0 on success, a negative errno value otherwise is set.
623  */
624 int
625 hns3_dev_rss_hash_update(struct rte_eth_dev *dev,
626 			 struct rte_eth_rss_conf *rss_conf)
627 {
628 	struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
629 	uint64_t rss_hf_bk = hw->rss_info.rss_hf;
630 	uint8_t key_len = rss_conf->rss_key_len;
631 	uint64_t rss_hf = rss_conf->rss_hf;
632 	uint8_t *key = rss_conf->rss_key;
633 	int ret;
634 
635 	if (key && key_len != hw->rss_key_size) {
636 		hns3_err(hw, "the hash key len(%u) is invalid, must be %u",
637 			 key_len, hw->rss_key_size);
638 		return -EINVAL;
639 	}
640 
641 	if (!hns3_check_rss_types_valid(hw, rss_hf))
642 		return -EINVAL;
643 
644 	rte_spinlock_lock(&hw->lock);
645 	ret = hns3_set_rss_tuple_by_rss_hf(hw, rss_hf);
646 	if (ret)
647 		goto set_tuple_fail;
648 
649 	if (key) {
650 		ret = hns3_rss_set_algo_key(hw, hw->rss_info.hash_algo,
651 					    key, hw->rss_key_size);
652 		if (ret)
653 			goto set_algo_key_fail;
654 		/* Update the shadow RSS key with user specified */
655 		memcpy(hw->rss_info.key, key, hw->rss_key_size);
656 	}
657 	hw->rss_info.rss_hf = rss_hf;
658 	rte_spinlock_unlock(&hw->lock);
659 
660 	return 0;
661 
662 set_algo_key_fail:
663 	(void)hns3_set_rss_tuple_by_rss_hf(hw, rss_hf_bk);
664 set_tuple_fail:
665 	rte_spinlock_unlock(&hw->lock);
666 	return ret;
667 }
668 
669 int
670 hns3_get_rss_tuple_field(struct hns3_hw *hw, uint64_t *tuple_fields)
671 {
672 	struct hns3_rss_input_tuple_cmd *req;
673 	struct hns3_cmd_desc desc;
674 	int ret;
675 
676 	hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_RSS_INPUT_TUPLE, true);
677 	req = (struct hns3_rss_input_tuple_cmd *)desc.data;
678 	ret = hns3_cmd_send(hw, &desc, 1);
679 	if (ret != 0) {
680 		hns3_err(hw, "fail to get RSS hash tuple fields from firmware, ret = %d",
681 			 ret);
682 		return ret;
683 	}
684 
685 	*tuple_fields = rte_le_to_cpu_64(req->tuple_field);
686 
687 	return 0;
688 }
689 
690 static uint64_t
691 hns3_rss_tuple_fields_to_rss_hf(struct hns3_hw *hw, uint64_t tuple_fields)
692 {
693 	uint64_t ipv6_sctp_l4_mask =
694 				BIT_ULL(HNS3_RSS_FIELD_IPV6_SCTP_EN_SCTP_D) |
695 				BIT_ULL(HNS3_RSS_FIELD_IPV6_SCTP_EN_SCTP_S);
696 	uint64_t rss_hf = 0;
697 	uint64_t tuple_mask;
698 	uint32_t i;
699 
700 	for (i = 0; i < RTE_DIM(hns3_set_tuple_table); i++) {
701 		tuple_mask = hns3_set_tuple_table[i].tuple_mask;
702 		/*
703 		 * The RSS hash of the packet type is disabled if its tuples is
704 		 * zero.
705 		 */
706 		if ((tuple_fields & tuple_mask) == 0)
707 			continue;
708 
709 		/*
710 		 * Some hardware don't support to use src/dst port fields to
711 		 * hash for IPV6-SCTP packet.
712 		 */
713 		if ((hns3_set_tuple_table[i].rss_types &
714 					RTE_ETH_RSS_NONFRAG_IPV6_SCTP) &&
715 		    !hw->rss_info.ipv6_sctp_offload_supported)
716 			tuple_mask &= ~ipv6_sctp_l4_mask;
717 
718 		/*
719 		 * The framework (ethdev ops) or driver (rte flow API) ensure
720 		 * that both L3_SRC/DST_ONLY and L4_SRC/DST_ONLY cannot be set
721 		 * to driver at the same time. But if user doesn't specify
722 		 * anything L3/L4_SRC/DST_ONLY, driver enables all tuple fields.
723 		 * In this case, driver should not report L3/L4_SRC/DST_ONLY.
724 		 */
725 		if ((tuple_fields & tuple_mask) == tuple_mask) {
726 			/* Skip the item enabled part tuples. */
727 			if ((tuple_fields & hns3_set_tuple_table[i].rss_field) !=
728 					tuple_mask)
729 				continue;
730 
731 			rss_hf |= hns3_set_tuple_table[i].rss_types;
732 			continue;
733 		}
734 
735 		/* Match the item enabled part tuples.*/
736 		if ((tuple_fields & hns3_set_tuple_table[i].rss_field) ==
737 					hns3_set_tuple_table[i].rss_field)
738 			rss_hf |= hns3_set_tuple_table[i].rss_types;
739 	}
740 
741 	return rss_hf;
742 }
743 
744 static int
745 hns3_rss_hash_get_rss_hf(struct hns3_hw *hw, uint64_t *rss_hf)
746 {
747 	uint64_t tuple_fields;
748 	int ret;
749 
750 	ret = hns3_get_rss_tuple_field(hw, &tuple_fields);
751 	if (ret != 0)
752 		return ret;
753 
754 	*rss_hf = hns3_rss_tuple_fields_to_rss_hf(hw, tuple_fields);
755 
756 	return 0;
757 }
758 
759 /*
760  * Get rss key and rss_hf types set of RSS hash configuration.
761  * @param dev
762  *   Pointer to Ethernet device.
763  * @praram rss_conf
764  *   The buffer to get rss key size and tuple types.
765  * @return
766  *   0 on success.
767  */
768 int
769 hns3_dev_rss_hash_conf_get(struct rte_eth_dev *dev,
770 			   struct rte_eth_rss_conf *rss_conf)
771 {
772 	struct hns3_adapter *hns = dev->data->dev_private;
773 	struct hns3_hw *hw = &hns->hw;
774 	uint8_t hash_algo;
775 	int ret;
776 
777 	rte_spinlock_lock(&hw->lock);
778 	ret = hns3_rss_hash_get_rss_hf(hw, &rss_conf->rss_hf);
779 	if (ret != 0) {
780 		hns3_err(hw, "obtain hash tuples failed, ret = %d", ret);
781 		goto out;
782 	}
783 
784 	/* Get the RSS Key required by the user */
785 	if (rss_conf->rss_key && rss_conf->rss_key_len >= hw->rss_key_size) {
786 		ret = hns3_rss_get_algo_key(hw, &hash_algo, rss_conf->rss_key,
787 					    hw->rss_key_size);
788 		if (ret != 0) {
789 			hns3_err(hw, "obtain hash algo and key failed, ret = %d",
790 				 ret);
791 			goto out;
792 		}
793 		rss_conf->rss_key_len = hw->rss_key_size;
794 	}
795 
796 out:
797 	rte_spinlock_unlock(&hw->lock);
798 
799 	return ret;
800 }
801 
802 /*
803  * Update rss redirection table of RSS.
804  * @param dev
805  *   Pointer to Ethernet device.
806  * @praram reta_conf
807  *   Pointer to the configuration select of mask and redirection tables.
808  * @param reta_size
809  *   Redirection table size.
810  * @return
811  *   0 on success, a negative errno value otherwise is set.
812  */
813 int
814 hns3_dev_rss_reta_update(struct rte_eth_dev *dev,
815 			 struct rte_eth_rss_reta_entry64 *reta_conf,
816 			 uint16_t reta_size)
817 {
818 	struct hns3_adapter *hns = dev->data->dev_private;
819 	struct hns3_hw *hw = &hns->hw;
820 	struct hns3_rss_conf *rss_cfg = &hw->rss_info;
821 	uint16_t indirection_tbl[HNS3_RSS_IND_TBL_SIZE_MAX];
822 	uint16_t idx, shift;
823 	uint16_t i;
824 	int ret;
825 
826 	if (reta_size != hw->rss_ind_tbl_size) {
827 		hns3_err(hw, "The size of hash lookup table configured (%u)"
828 			 "doesn't match the number hardware can supported"
829 			 "(%u)", reta_size, hw->rss_ind_tbl_size);
830 		return -EINVAL;
831 	}
832 	rte_spinlock_lock(&hw->lock);
833 	memcpy(indirection_tbl, rss_cfg->rss_indirection_tbl,
834 	       sizeof(rss_cfg->rss_indirection_tbl));
835 	for (i = 0; i < reta_size; i++) {
836 		idx = i / RTE_ETH_RETA_GROUP_SIZE;
837 		shift = i % RTE_ETH_RETA_GROUP_SIZE;
838 		if (reta_conf[idx].reta[shift] >= hw->alloc_rss_size) {
839 			hns3_err(hw, "queue id(%u) set to redirection table "
840 				 "exceeds queue number(%u) allocated to a TC",
841 				 reta_conf[idx].reta[shift],
842 				 hw->alloc_rss_size);
843 			ret = -EINVAL;
844 			goto out;
845 		}
846 
847 		if (reta_conf[idx].mask & (1ULL << shift))
848 			indirection_tbl[i] = reta_conf[idx].reta[shift];
849 	}
850 
851 	ret = hns3_set_rss_indir_table(hw, indirection_tbl,
852 				       hw->rss_ind_tbl_size);
853 	if (ret != 0)
854 		goto out;
855 
856 	memcpy(rss_cfg->rss_indirection_tbl, indirection_tbl,
857 	       sizeof(uint16_t) * hw->rss_ind_tbl_size);
858 
859 out:
860 	rte_spinlock_unlock(&hw->lock);
861 	return ret;
862 }
863 
864 /*
865  * Get rss redirection table of RSS hash configuration.
866  * @param dev
867  *   Pointer to Ethernet device.
868  * @praram reta_conf
869  *   Pointer to the configuration select of mask and redirection tables.
870  * @param reta_size
871  *   Redirection table size.
872  * @return
873  *   0 on success, a negative errno value otherwise is set.
874  */
875 int
876 hns3_dev_rss_reta_query(struct rte_eth_dev *dev,
877 			struct rte_eth_rss_reta_entry64 *reta_conf,
878 			uint16_t reta_size)
879 {
880 	struct hns3_adapter *hns = dev->data->dev_private;
881 	uint16_t reta_table[HNS3_RSS_IND_TBL_SIZE_MAX];
882 	struct hns3_hw *hw = &hns->hw;
883 	uint16_t idx, shift;
884 	uint16_t i;
885 	int ret;
886 
887 	if (reta_size != hw->rss_ind_tbl_size) {
888 		hns3_err(hw, "The size of hash lookup table configured (%u)"
889 			 " doesn't match the number hardware can supported"
890 			 "(%u)", reta_size, hw->rss_ind_tbl_size);
891 		return -EINVAL;
892 	}
893 	rte_spinlock_lock(&hw->lock);
894 	ret = hns3_get_rss_indir_table(hw, reta_table, reta_size);
895 	if (ret != 0) {
896 		rte_spinlock_unlock(&hw->lock);
897 		hns3_err(hw, "query RSS redirection table failed, ret = %d.",
898 			 ret);
899 		return ret;
900 	}
901 	rte_spinlock_unlock(&hw->lock);
902 
903 	for (i = 0; i < reta_size; i++) {
904 		idx = i / RTE_ETH_RETA_GROUP_SIZE;
905 		shift = i % RTE_ETH_RETA_GROUP_SIZE;
906 		if (reta_conf[idx].mask & (1ULL << shift))
907 			reta_conf[idx].reta[shift] = reta_table[i];
908 	}
909 
910 	return 0;
911 }
912 
913 static void
914 hns3_set_rss_tc_mode_entry(struct hns3_hw *hw, uint8_t *tc_valid,
915 			   uint16_t *tc_size, uint16_t *tc_offset,
916 			   uint8_t tc_num)
917 {
918 	struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
919 	uint16_t rss_size = hw->alloc_rss_size;
920 	uint16_t roundup_size;
921 	uint16_t i;
922 
923 	roundup_size = roundup_pow_of_two(rss_size);
924 	roundup_size = ilog2(roundup_size);
925 
926 	for (i = 0; i < tc_num; i++) {
927 		if (hns->is_vf) {
928 			/*
929 			 * For packets with VLAN priorities destined for the VF,
930 			 * hardware still assign Rx queue based on the Up-to-TC
931 			 * mapping PF configured. But VF has only one TC. If
932 			 * other TC don't enable, it causes that the priority
933 			 * packets that aren't destined for TC0 aren't received
934 			 * by RSS hash but is destined for queue 0. So driver
935 			 * has to enable the unused TC by using TC0 queue
936 			 * mapping configuration.
937 			 */
938 			tc_valid[i] = (hw->hw_tc_map & BIT(i)) ?
939 					!!(hw->hw_tc_map & BIT(i)) : 1;
940 			tc_size[i] = roundup_size;
941 			tc_offset[i] = (hw->hw_tc_map & BIT(i)) ?
942 					rss_size * i : 0;
943 		} else {
944 			tc_valid[i] = !!(hw->hw_tc_map & BIT(i));
945 			tc_size[i] = tc_valid[i] ? roundup_size : 0;
946 			tc_offset[i] = tc_valid[i] ? rss_size * i : 0;
947 		}
948 	}
949 }
950 
951 static int
952 hns3_set_rss_tc_mode(struct hns3_hw *hw)
953 {
954 	struct hns3_rss_tc_mode_cmd *req;
955 	uint16_t tc_offset[HNS3_MAX_TC_NUM];
956 	uint8_t tc_valid[HNS3_MAX_TC_NUM];
957 	uint16_t tc_size[HNS3_MAX_TC_NUM];
958 	struct hns3_cmd_desc desc;
959 	uint16_t i;
960 	int ret;
961 
962 	hns3_set_rss_tc_mode_entry(hw, tc_valid, tc_size,
963 				   tc_offset, HNS3_MAX_TC_NUM);
964 
965 	req = (struct hns3_rss_tc_mode_cmd *)desc.data;
966 	hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_RSS_TC_MODE, false);
967 	for (i = 0; i < HNS3_MAX_TC_NUM; i++) {
968 		uint16_t mode = 0;
969 
970 		hns3_set_bit(mode, HNS3_RSS_TC_VALID_B, (tc_valid[i] & 0x1));
971 		hns3_set_field(mode, HNS3_RSS_TC_SIZE_M, HNS3_RSS_TC_SIZE_S,
972 			       tc_size[i]);
973 		if (tc_size[i] >> HNS3_RSS_TC_SIZE_MSB_OFFSET > 0)
974 			hns3_set_bit(mode, HNS3_RSS_TC_SIZE_MSB_S, 1);
975 		hns3_set_field(mode, HNS3_RSS_TC_OFFSET_M, HNS3_RSS_TC_OFFSET_S,
976 			       tc_offset[i]);
977 
978 		req->rss_tc_mode[i] = rte_cpu_to_le_16(mode);
979 	}
980 	ret = hns3_cmd_send(hw, &desc, 1);
981 	if (ret)
982 		hns3_err(hw, "Sets rss tc mode failed %d", ret);
983 
984 	return ret;
985 }
986 
987 /*
988  * Note: the 'hash_algo' is defined by enum rte_eth_hash_function.
989  */
990 int
991 hns3_update_rss_algo_key(struct hns3_hw *hw, uint8_t hash_func,
992 			 uint8_t *key, uint8_t key_len)
993 {
994 	uint8_t rss_key[HNS3_RSS_KEY_SIZE_MAX] = {0};
995 	bool modify_key, modify_algo;
996 	uint8_t hash_algo;
997 	int ret;
998 
999 	modify_key = (key != NULL && key_len > 0);
1000 	modify_algo = hash_func != RTE_ETH_HASH_FUNCTION_DEFAULT;
1001 	if (!modify_key && !modify_algo)
1002 		return 0;
1003 
1004 	if (modify_algo && hash_func >= RTE_DIM(hns3_hash_func_map)) {
1005 		hns3_err(hw, "hash func (%u) is unsupported.", hash_func);
1006 		return -ENOTSUP;
1007 	}
1008 	if (modify_key && key_len != hw->rss_key_size) {
1009 		hns3_err(hw, "hash key length (%u) is invalid.", key_len);
1010 		return -EINVAL;
1011 	}
1012 
1013 	ret = hns3_rss_get_algo_key(hw, &hash_algo, rss_key, hw->rss_key_size);
1014 	if (ret != 0) {
1015 		hns3_err(hw, "fail to get RSS hash algorithm and key, ret = %d",
1016 			 ret);
1017 		return ret;
1018 	}
1019 
1020 	if (modify_algo)
1021 		hash_algo = hns3_hash_func_map[hash_func];
1022 	if (modify_key)
1023 		memcpy(rss_key, key, key_len);
1024 
1025 	ret = hns3_rss_set_algo_key(hw, hash_algo, rss_key, hw->rss_key_size);
1026 	if (ret != 0)
1027 		hns3_err(hw, "fail to set RSS hash algorithm and key, ret = %d",
1028 			 ret);
1029 
1030 	return ret;
1031 }
1032 
1033 static void
1034 hns3_rss_tuple_uninit(struct hns3_hw *hw)
1035 {
1036 	struct hns3_cmd_desc desc;
1037 	int ret;
1038 
1039 	hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_RSS_INPUT_TUPLE, false);
1040 
1041 	ret = hns3_cmd_send(hw, &desc, 1);
1042 	if (ret) {
1043 		hns3_err(hw, "RSS uninit tuple failed %d", ret);
1044 		return;
1045 	}
1046 }
1047 
1048 /*
1049  * Set the default rss configuration in the init of driver.
1050  */
1051 void
1052 hns3_rss_set_default_args(struct hns3_hw *hw)
1053 {
1054 	struct hns3_rss_conf *rss_cfg = &hw->rss_info;
1055 	uint16_t queue_num = hw->alloc_rss_size;
1056 	uint16_t i;
1057 
1058 	/* Default hash algorithm */
1059 	rss_cfg->hash_algo = HNS3_RSS_HASH_ALGO_TOEPLITZ;
1060 
1061 	hw->rss_info.rss_hf = 0;
1062 	memcpy(rss_cfg->key, hns3_hash_key,
1063 		RTE_MIN(sizeof(hns3_hash_key), hw->rss_key_size));
1064 
1065 	/* Initialize RSS indirection table */
1066 	for (i = 0; i < hw->rss_ind_tbl_size; i++)
1067 		rss_cfg->rss_indirection_tbl[i] = i % queue_num;
1068 }
1069 
1070 /*
1071  * RSS initialization for hns3 PMD.
1072  */
1073 int
1074 hns3_config_rss(struct hns3_adapter *hns)
1075 {
1076 	struct hns3_hw *hw = &hns->hw;
1077 	struct hns3_rss_conf *rss_cfg = &hw->rss_info;
1078 	uint8_t *hash_key = rss_cfg->key;
1079 	uint64_t rss_hf;
1080 	int ret;
1081 
1082 	enum rte_eth_rx_mq_mode mq_mode = hw->data->dev_conf.rxmode.mq_mode;
1083 
1084 	ret = hns3_rss_set_algo_key(hw, rss_cfg->hash_algo,
1085 				    hash_key, hw->rss_key_size);
1086 	if (ret)
1087 		return ret;
1088 
1089 	ret = hns3_set_rss_indir_table(hw, rss_cfg->rss_indirection_tbl,
1090 				       hw->rss_ind_tbl_size);
1091 	if (ret)
1092 		return ret;
1093 
1094 	ret = hns3_set_rss_tc_mode(hw);
1095 	if (ret)
1096 		return ret;
1097 
1098 	/*
1099 	 * When multi-queue RSS mode flag is not set or unsupported tuples are
1100 	 * set, disable all tuples.
1101 	 */
1102 	rss_hf = hw->rss_info.rss_hf;
1103 	if (!((uint32_t)mq_mode & RTE_ETH_MQ_RX_RSS_FLAG) ||
1104 	    !(rss_hf & HNS3_ETH_RSS_SUPPORT))
1105 		rss_hf = 0;
1106 
1107 	ret = hns3_set_rss_tuple_by_rss_hf(hw, rss_hf);
1108 	if (ret != 0) {
1109 		hns3_err(hw, "set RSS tuples failed, ret = %d.", ret);
1110 		return ret;
1111 	}
1112 	hw->rss_info.rss_hf = rss_hf;
1113 
1114 	return 0;
1115 }
1116 
1117 /*
1118  * RSS uninitialization for hns3 PMD.
1119  */
1120 void
1121 hns3_rss_uninit(struct hns3_adapter *hns)
1122 {
1123 	struct hns3_hw *hw = &hns->hw;
1124 	int ret;
1125 
1126 	hns3_rss_tuple_uninit(hw);
1127 	ret = hns3_rss_reset_indir_table(hw);
1128 	if (ret != 0)
1129 		return;
1130 
1131 	/* Disable RSS */
1132 	hw->rss_info.rss_hf = 0;
1133 }
1134