85495bbe89d90a11311cc9e62d1b4c763faeef1c
[dpdk.git] / drivers / net / hns3 / hns3_rss.c
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 /*
13  * The hash key used for rss initialization.
14  */
15 static const uint8_t hns3_hash_key[] = {
16         0x6D, 0x5A, 0x56, 0xDA, 0x25, 0x5B, 0x0E, 0xC2,
17         0x41, 0x67, 0x25, 0x3D, 0x43, 0xA3, 0x8F, 0xB0,
18         0xD0, 0xCA, 0x2B, 0xCB, 0xAE, 0x7B, 0x30, 0xB4,
19         0x77, 0xCB, 0x2D, 0xA3, 0x80, 0x30, 0xF2, 0x0C,
20         0x6A, 0x42, 0xB7, 0x3B, 0xBE, 0xAC, 0x01, 0xFA
21 };
22
23 enum hns3_tuple_field {
24         /* IPV4_TCP ENABLE FIELD */
25         HNS3_RSS_FIELD_IPV4_TCP_EN_TCP_D = 0,
26         HNS3_RSS_FIELD_IPV4_TCP_EN_TCP_S,
27         HNS3_RSS_FIELD_IPV4_TCP_EN_IP_D,
28         HNS3_RSS_FIELD_IPV4_TCP_EN_IP_S,
29
30         /* IPV4_UDP ENABLE FIELD */
31         HNS3_RSS_FIELD_IPV4_UDP_EN_UDP_D = 8,
32         HNS3_RSS_FIELD_IPV4_UDP_EN_UDP_S,
33         HNS3_RSS_FIELD_IPV4_UDP_EN_IP_D,
34         HNS3_RSS_FIELD_IPV4_UDP_EN_IP_S,
35
36         /* IPV4_SCTP ENABLE FIELD */
37         HNS3_RSS_FIELD_IPV4_SCTP_EN_SCTP_D = 16,
38         HNS3_RSS_FIELD_IPV4_SCTP_EN_SCTP_S,
39         HNS3_RSS_FIELD_IPV4_SCTP_EN_IP_D,
40         HNS3_RSS_FIELD_IPV4_SCTP_EN_IP_S,
41         HNS3_RSS_FIELD_IPV4_SCTP_EN_SCTP_VER,
42
43         /* IPV4 ENABLE FIELD */
44         HNS3_RSS_FIELD_IPV4_EN_NONFRAG_IP_D = 24,
45         HNS3_RSS_FIELD_IPV4_EN_NONFRAG_IP_S,
46         HNS3_RSS_FIELD_IPV4_EN_FRAG_IP_D,
47         HNS3_RSS_FIELD_IPV4_EN_FRAG_IP_S,
48
49         /* IPV6_TCP ENABLE FIELD */
50         HNS3_RSS_FIELD_IPV6_TCP_EN_TCP_D = 32,
51         HNS3_RSS_FIELD_IPV6_TCP_EN_TCP_S,
52         HNS3_RSS_FIELD_IPV6_TCP_EN_IP_D,
53         HNS3_RSS_FIELD_IPV6_TCP_EN_IP_S,
54
55         /* IPV6_UDP ENABLE FIELD */
56         HNS3_RSS_FIELD_IPV6_UDP_EN_UDP_D = 40,
57         HNS3_RSS_FIELD_IPV6_UDP_EN_UDP_S,
58         HNS3_RSS_FIELD_IPV6_UDP_EN_IP_D,
59         HNS3_RSS_FIELD_IPV6_UDP_EN_IP_S,
60
61         /* IPV6_SCTP ENABLE FIELD */
62         HNS3_RSS_FILED_IPV6_SCTP_EN_SCTP_D = 48,
63         HNS3_RSS_FILED_IPV6_SCTP_EN_SCTP_S,
64         HNS3_RSS_FIELD_IPV6_SCTP_EN_IP_D,
65         HNS3_RSS_FIELD_IPV6_SCTP_EN_IP_S,
66         HNS3_RSS_FIELD_IPV6_SCTP_EN_SCTP_VER,
67
68         /* IPV6 ENABLE FIELD */
69         HNS3_RSS_FIELD_IPV6_NONFRAG_IP_D = 56,
70         HNS3_RSS_FIELD_IPV6_NONFRAG_IP_S,
71         HNS3_RSS_FIELD_IPV6_FRAG_IP_D,
72         HNS3_RSS_FIELD_IPV6_FRAG_IP_S
73 };
74
75 static const struct {
76         uint64_t rss_types;
77         uint64_t rss_field;
78 } hns3_set_tuple_table[] = {
79         { RTE_ETH_RSS_FRAG_IPV4 | RTE_ETH_RSS_L3_SRC_ONLY,
80           BIT_ULL(HNS3_RSS_FIELD_IPV4_EN_FRAG_IP_S) },
81         { RTE_ETH_RSS_FRAG_IPV4 | RTE_ETH_RSS_L3_DST_ONLY,
82           BIT_ULL(HNS3_RSS_FIELD_IPV4_EN_FRAG_IP_D) },
83         { RTE_ETH_RSS_NONFRAG_IPV4_TCP | RTE_ETH_RSS_L3_SRC_ONLY,
84           BIT_ULL(HNS3_RSS_FIELD_IPV4_TCP_EN_IP_S) },
85         { RTE_ETH_RSS_NONFRAG_IPV4_TCP | RTE_ETH_RSS_L3_DST_ONLY,
86           BIT_ULL(HNS3_RSS_FIELD_IPV4_TCP_EN_IP_D) },
87         { RTE_ETH_RSS_NONFRAG_IPV4_TCP | RTE_ETH_RSS_L4_SRC_ONLY,
88           BIT_ULL(HNS3_RSS_FIELD_IPV4_TCP_EN_TCP_S) },
89         { RTE_ETH_RSS_NONFRAG_IPV4_TCP | RTE_ETH_RSS_L4_DST_ONLY,
90           BIT_ULL(HNS3_RSS_FIELD_IPV4_TCP_EN_TCP_D) },
91         { RTE_ETH_RSS_NONFRAG_IPV4_UDP | RTE_ETH_RSS_L3_SRC_ONLY,
92           BIT_ULL(HNS3_RSS_FIELD_IPV4_UDP_EN_IP_S) },
93         { RTE_ETH_RSS_NONFRAG_IPV4_UDP | RTE_ETH_RSS_L3_DST_ONLY,
94           BIT_ULL(HNS3_RSS_FIELD_IPV4_UDP_EN_IP_D) },
95         { RTE_ETH_RSS_NONFRAG_IPV4_UDP | RTE_ETH_RSS_L4_SRC_ONLY,
96           BIT_ULL(HNS3_RSS_FIELD_IPV4_UDP_EN_UDP_S) },
97         { RTE_ETH_RSS_NONFRAG_IPV4_UDP | RTE_ETH_RSS_L4_DST_ONLY,
98           BIT_ULL(HNS3_RSS_FIELD_IPV4_UDP_EN_UDP_D) },
99         { RTE_ETH_RSS_NONFRAG_IPV4_SCTP | RTE_ETH_RSS_L3_SRC_ONLY,
100           BIT_ULL(HNS3_RSS_FIELD_IPV4_SCTP_EN_IP_S) },
101         { RTE_ETH_RSS_NONFRAG_IPV4_SCTP | RTE_ETH_RSS_L3_DST_ONLY,
102           BIT_ULL(HNS3_RSS_FIELD_IPV4_SCTP_EN_IP_D) },
103         { RTE_ETH_RSS_NONFRAG_IPV4_SCTP | RTE_ETH_RSS_L4_SRC_ONLY,
104           BIT_ULL(HNS3_RSS_FIELD_IPV4_SCTP_EN_SCTP_S) },
105         { RTE_ETH_RSS_NONFRAG_IPV4_SCTP | RTE_ETH_RSS_L4_DST_ONLY,
106           BIT_ULL(HNS3_RSS_FIELD_IPV4_SCTP_EN_SCTP_D) },
107         { RTE_ETH_RSS_NONFRAG_IPV4_OTHER | RTE_ETH_RSS_L3_SRC_ONLY,
108           BIT_ULL(HNS3_RSS_FIELD_IPV4_EN_NONFRAG_IP_S) },
109         { RTE_ETH_RSS_NONFRAG_IPV4_OTHER | RTE_ETH_RSS_L3_DST_ONLY,
110           BIT_ULL(HNS3_RSS_FIELD_IPV4_EN_NONFRAG_IP_D) },
111         { RTE_ETH_RSS_FRAG_IPV6 | RTE_ETH_RSS_L3_SRC_ONLY,
112           BIT_ULL(HNS3_RSS_FIELD_IPV6_FRAG_IP_S) },
113         { RTE_ETH_RSS_FRAG_IPV6 | RTE_ETH_RSS_L3_DST_ONLY,
114           BIT_ULL(HNS3_RSS_FIELD_IPV6_FRAG_IP_D) },
115         { RTE_ETH_RSS_NONFRAG_IPV6_TCP | RTE_ETH_RSS_L3_SRC_ONLY,
116           BIT_ULL(HNS3_RSS_FIELD_IPV6_TCP_EN_IP_S) },
117         { RTE_ETH_RSS_NONFRAG_IPV6_TCP | RTE_ETH_RSS_L3_DST_ONLY,
118           BIT_ULL(HNS3_RSS_FIELD_IPV6_TCP_EN_IP_D) },
119         { RTE_ETH_RSS_NONFRAG_IPV6_TCP | RTE_ETH_RSS_L4_SRC_ONLY,
120           BIT_ULL(HNS3_RSS_FIELD_IPV6_TCP_EN_TCP_S) },
121         { RTE_ETH_RSS_NONFRAG_IPV6_TCP | RTE_ETH_RSS_L4_DST_ONLY,
122           BIT_ULL(HNS3_RSS_FIELD_IPV6_TCP_EN_TCP_D) },
123         { RTE_ETH_RSS_NONFRAG_IPV6_UDP | RTE_ETH_RSS_L3_SRC_ONLY,
124           BIT_ULL(HNS3_RSS_FIELD_IPV6_UDP_EN_IP_S) },
125         { RTE_ETH_RSS_NONFRAG_IPV6_UDP | RTE_ETH_RSS_L3_DST_ONLY,
126           BIT_ULL(HNS3_RSS_FIELD_IPV6_UDP_EN_IP_D) },
127         { RTE_ETH_RSS_NONFRAG_IPV6_UDP | RTE_ETH_RSS_L4_SRC_ONLY,
128           BIT_ULL(HNS3_RSS_FIELD_IPV6_UDP_EN_UDP_S) },
129         { RTE_ETH_RSS_NONFRAG_IPV6_UDP | RTE_ETH_RSS_L4_DST_ONLY,
130           BIT_ULL(HNS3_RSS_FIELD_IPV6_UDP_EN_UDP_D) },
131         { RTE_ETH_RSS_NONFRAG_IPV6_SCTP | RTE_ETH_RSS_L3_SRC_ONLY,
132           BIT_ULL(HNS3_RSS_FIELD_IPV6_SCTP_EN_IP_S) },
133         { RTE_ETH_RSS_NONFRAG_IPV6_SCTP | RTE_ETH_RSS_L3_DST_ONLY,
134           BIT_ULL(HNS3_RSS_FIELD_IPV6_SCTP_EN_IP_D) },
135         { RTE_ETH_RSS_NONFRAG_IPV6_SCTP | RTE_ETH_RSS_L4_SRC_ONLY,
136           BIT_ULL(HNS3_RSS_FILED_IPV6_SCTP_EN_SCTP_S) },
137         { RTE_ETH_RSS_NONFRAG_IPV6_SCTP | RTE_ETH_RSS_L4_DST_ONLY,
138           BIT_ULL(HNS3_RSS_FILED_IPV6_SCTP_EN_SCTP_D) },
139         { RTE_ETH_RSS_NONFRAG_IPV6_OTHER | RTE_ETH_RSS_L3_SRC_ONLY,
140           BIT_ULL(HNS3_RSS_FIELD_IPV6_NONFRAG_IP_S) },
141         { RTE_ETH_RSS_NONFRAG_IPV6_OTHER | RTE_ETH_RSS_L3_DST_ONLY,
142           BIT_ULL(HNS3_RSS_FIELD_IPV6_NONFRAG_IP_D) },
143 };
144
145 static const struct {
146         uint64_t rss_types;
147         uint64_t rss_field;
148 } hns3_set_rss_types[] = {
149         { RTE_ETH_RSS_FRAG_IPV4, BIT_ULL(HNS3_RSS_FIELD_IPV4_EN_FRAG_IP_D) |
150           BIT_ULL(HNS3_RSS_FIELD_IPV4_EN_FRAG_IP_S) },
151         { RTE_ETH_RSS_NONFRAG_IPV4_TCP, BIT_ULL(HNS3_RSS_FIELD_IPV4_TCP_EN_IP_S) |
152           BIT_ULL(HNS3_RSS_FIELD_IPV4_TCP_EN_IP_D) |
153           BIT_ULL(HNS3_RSS_FIELD_IPV4_TCP_EN_TCP_S) |
154           BIT_ULL(HNS3_RSS_FIELD_IPV4_TCP_EN_TCP_D) },
155         { RTE_ETH_RSS_NONFRAG_IPV4_TCP, BIT_ULL(HNS3_RSS_FIELD_IPV4_TCP_EN_IP_S) |
156           BIT_ULL(HNS3_RSS_FIELD_IPV4_TCP_EN_IP_D) |
157           BIT_ULL(HNS3_RSS_FIELD_IPV4_TCP_EN_TCP_S) |
158           BIT_ULL(HNS3_RSS_FIELD_IPV4_TCP_EN_TCP_D) },
159         { RTE_ETH_RSS_NONFRAG_IPV4_UDP, BIT_ULL(HNS3_RSS_FIELD_IPV4_UDP_EN_IP_S) |
160           BIT_ULL(HNS3_RSS_FIELD_IPV4_UDP_EN_IP_D) |
161           BIT_ULL(HNS3_RSS_FIELD_IPV4_UDP_EN_UDP_S) |
162           BIT_ULL(HNS3_RSS_FIELD_IPV4_UDP_EN_UDP_D) },
163         { RTE_ETH_RSS_NONFRAG_IPV4_SCTP, BIT_ULL(HNS3_RSS_FIELD_IPV4_SCTP_EN_IP_S) |
164           BIT_ULL(HNS3_RSS_FIELD_IPV4_SCTP_EN_IP_D) |
165           BIT_ULL(HNS3_RSS_FIELD_IPV4_SCTP_EN_SCTP_S) |
166           BIT_ULL(HNS3_RSS_FIELD_IPV4_SCTP_EN_SCTP_D) |
167           BIT_ULL(HNS3_RSS_FIELD_IPV4_SCTP_EN_SCTP_VER) },
168         { RTE_ETH_RSS_NONFRAG_IPV4_OTHER,
169           BIT_ULL(HNS3_RSS_FIELD_IPV4_EN_NONFRAG_IP_S) |
170           BIT_ULL(HNS3_RSS_FIELD_IPV4_EN_NONFRAG_IP_D) },
171         { RTE_ETH_RSS_FRAG_IPV6, BIT_ULL(HNS3_RSS_FIELD_IPV6_FRAG_IP_S) |
172           BIT_ULL(HNS3_RSS_FIELD_IPV6_FRAG_IP_D) },
173         { RTE_ETH_RSS_NONFRAG_IPV6_TCP, BIT_ULL(HNS3_RSS_FIELD_IPV6_TCP_EN_IP_S) |
174           BIT_ULL(HNS3_RSS_FIELD_IPV6_TCP_EN_IP_D) |
175           BIT_ULL(HNS3_RSS_FIELD_IPV6_TCP_EN_TCP_S) |
176           BIT_ULL(HNS3_RSS_FIELD_IPV6_TCP_EN_TCP_D) },
177         { RTE_ETH_RSS_NONFRAG_IPV6_UDP, BIT_ULL(HNS3_RSS_FIELD_IPV6_UDP_EN_IP_S) |
178           BIT_ULL(HNS3_RSS_FIELD_IPV6_UDP_EN_IP_D) |
179           BIT_ULL(HNS3_RSS_FIELD_IPV6_UDP_EN_UDP_S) |
180           BIT_ULL(HNS3_RSS_FIELD_IPV6_UDP_EN_UDP_D) },
181         { RTE_ETH_RSS_NONFRAG_IPV6_SCTP, BIT_ULL(HNS3_RSS_FIELD_IPV6_SCTP_EN_IP_S) |
182           BIT_ULL(HNS3_RSS_FIELD_IPV6_SCTP_EN_IP_D) |
183           BIT_ULL(HNS3_RSS_FILED_IPV6_SCTP_EN_SCTP_D) |
184           BIT_ULL(HNS3_RSS_FILED_IPV6_SCTP_EN_SCTP_S) |
185           BIT_ULL(HNS3_RSS_FIELD_IPV6_SCTP_EN_SCTP_VER) },
186         { RTE_ETH_RSS_NONFRAG_IPV6_OTHER,
187           BIT_ULL(HNS3_RSS_FIELD_IPV6_NONFRAG_IP_S) |
188           BIT_ULL(HNS3_RSS_FIELD_IPV6_NONFRAG_IP_D) }
189 };
190
191 /*
192  * rss_generic_config command function, opcode:0x0D01.
193  * Used to set algorithm, key_offset and hash key of rss.
194  */
195 int
196 hns3_rss_set_algo_key(struct hns3_hw *hw, const uint8_t *key)
197 {
198 #define HNS3_KEY_OFFSET_MAX     3
199 #define HNS3_SET_HASH_KEY_BYTE_FOUR     2
200
201         struct hns3_rss_generic_config_cmd *req;
202         struct hns3_cmd_desc desc;
203         uint32_t key_offset, key_size;
204         const uint8_t *key_cur;
205         uint8_t cur_offset;
206         int ret;
207
208         req = (struct hns3_rss_generic_config_cmd *)desc.data;
209
210         /*
211          * key_offset=0, hash key byte0~15 is set to hardware.
212          * key_offset=1, hash key byte16~31 is set to hardware.
213          * key_offset=2, hash key byte32~39 is set to hardware.
214          */
215         for (key_offset = 0; key_offset < HNS3_KEY_OFFSET_MAX; key_offset++) {
216                 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_RSS_GENERIC_CONFIG,
217                                           false);
218
219                 req->hash_config |=
220                         (hw->rss_info.hash_algo & HNS3_RSS_HASH_ALGO_MASK);
221                 req->hash_config |= (key_offset << HNS3_RSS_HASH_KEY_OFFSET_B);
222
223                 if (key_offset == HNS3_SET_HASH_KEY_BYTE_FOUR)
224                         key_size = HNS3_RSS_KEY_SIZE - HNS3_RSS_HASH_KEY_NUM *
225                         HNS3_SET_HASH_KEY_BYTE_FOUR;
226                 else
227                         key_size = HNS3_RSS_HASH_KEY_NUM;
228
229                 cur_offset = key_offset * HNS3_RSS_HASH_KEY_NUM;
230                 key_cur = key + cur_offset;
231                 memcpy(req->hash_key, key_cur, key_size);
232
233                 ret = hns3_cmd_send(hw, &desc, 1);
234                 if (ret) {
235                         hns3_err(hw, "Configure RSS algo key failed %d", ret);
236                         return ret;
237                 }
238         }
239         /* Update the shadow RSS key with user specified */
240         memcpy(hw->rss_info.key, key, HNS3_RSS_KEY_SIZE);
241         return 0;
242 }
243
244 /*
245  * Used to configure the tuple selection for RSS hash input.
246  */
247 static int
248 hns3_rss_set_input_tuple(struct hns3_hw *hw)
249 {
250         struct hns3_rss_conf *rss_config = &hw->rss_info;
251         struct hns3_rss_input_tuple_cmd *req;
252         struct hns3_cmd_desc desc_tuple;
253         int ret;
254
255         hns3_cmd_setup_basic_desc(&desc_tuple, HNS3_OPC_RSS_INPUT_TUPLE, false);
256
257         req = (struct hns3_rss_input_tuple_cmd *)desc_tuple.data;
258
259         req->tuple_field =
260                 rte_cpu_to_le_64(rss_config->rss_tuple_sets.rss_tuple_fields);
261
262         ret = hns3_cmd_send(hw, &desc_tuple, 1);
263         if (ret)
264                 hns3_err(hw, "Configure RSS input tuple mode failed %d", ret);
265
266         return ret;
267 }
268
269 /*
270  * rss_indirection_table command function, opcode:0x0D07.
271  * Used to configure the indirection table of rss.
272  */
273 int
274 hns3_set_rss_indir_table(struct hns3_hw *hw, uint16_t *indir, uint16_t size)
275 {
276         struct hns3_rss_indirection_table_cmd *req;
277         struct hns3_cmd_desc desc;
278         uint8_t qid_msb_off;
279         uint8_t qid_msb_val;
280         uint16_t q_id;
281         uint16_t i, j;
282         int ret;
283
284         req = (struct hns3_rss_indirection_table_cmd *)desc.data;
285
286         for (i = 0; i < size / HNS3_RSS_CFG_TBL_SIZE; i++) {
287                 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_RSS_INDIR_TABLE,
288                                           false);
289                 req->start_table_index =
290                                 rte_cpu_to_le_16(i * HNS3_RSS_CFG_TBL_SIZE);
291                 req->rss_set_bitmap = rte_cpu_to_le_16(HNS3_RSS_SET_BITMAP_MSK);
292                 for (j = 0; j < HNS3_RSS_CFG_TBL_SIZE; j++) {
293                         q_id = indir[i * HNS3_RSS_CFG_TBL_SIZE + j];
294                         req->rss_result_l[j] = q_id & 0xff;
295
296                         qid_msb_off =
297                                 j * HNS3_RSS_CFG_TBL_BW_H / HNS3_BITS_PER_BYTE;
298                         qid_msb_val = (q_id >> HNS3_RSS_CFG_TBL_BW_L & 0x1)
299                                         << (j * HNS3_RSS_CFG_TBL_BW_H %
300                                         HNS3_BITS_PER_BYTE);
301                         req->rss_result_h[qid_msb_off] |= qid_msb_val;
302                 }
303
304                 ret = hns3_cmd_send(hw, &desc, 1);
305                 if (ret) {
306                         hns3_err(hw,
307                                  "Sets RSS indirection table failed %d size %u",
308                                  ret, size);
309                         return ret;
310                 }
311         }
312
313         /* Update redirection table of hw */
314         memcpy(hw->rss_info.rss_indirection_tbl, indir,
315                sizeof(uint16_t) * size);
316
317         return 0;
318 }
319
320 int
321 hns3_rss_reset_indir_table(struct hns3_hw *hw)
322 {
323         uint16_t *lut;
324         int ret;
325
326         lut = rte_zmalloc("hns3_rss_lut",
327                           hw->rss_ind_tbl_size * sizeof(uint16_t), 0);
328         if (lut == NULL) {
329                 hns3_err(hw, "No hns3_rss_lut memory can be allocated");
330                 return -ENOMEM;
331         }
332
333         ret = hns3_set_rss_indir_table(hw, lut, hw->rss_ind_tbl_size);
334         if (ret)
335                 hns3_err(hw, "RSS uninit indir table failed: %d", ret);
336         rte_free(lut);
337
338         return ret;
339 }
340
341 int
342 hns3_set_rss_tuple_by_rss_hf(struct hns3_hw *hw,
343                              struct hns3_rss_tuple_cfg *tuple, uint64_t rss_hf)
344 {
345         struct hns3_rss_input_tuple_cmd *req;
346         struct hns3_cmd_desc desc;
347         uint32_t fields_count = 0; /* count times for setting tuple fields */
348         uint32_t i;
349         int ret;
350
351         hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_RSS_INPUT_TUPLE, false);
352
353         req = (struct hns3_rss_input_tuple_cmd *)desc.data;
354
355         for (i = 0; i < RTE_DIM(hns3_set_tuple_table); i++) {
356                 if ((rss_hf & hns3_set_tuple_table[i].rss_types) ==
357                      hns3_set_tuple_table[i].rss_types) {
358                         req->tuple_field |=
359                             rte_cpu_to_le_64(hns3_set_tuple_table[i].rss_field);
360                         fields_count++;
361                 }
362         }
363
364         /*
365          * When user does not specify the following types or a combination of
366          * the following types, it enables all fields for the supported RSS
367          * types. the following types as:
368          * - RTE_ETH_RSS_L3_SRC_ONLY
369          * - RTE_ETH_RSS_L3_DST_ONLY
370          * - RTE_ETH_RSS_L4_SRC_ONLY
371          * - RTE_ETH_RSS_L4_DST_ONLY
372          */
373         if (fields_count == 0) {
374                 for (i = 0; i < RTE_DIM(hns3_set_rss_types); i++) {
375                         if ((rss_hf & hns3_set_rss_types[i].rss_types) ==
376                              hns3_set_rss_types[i].rss_types)
377                                 req->tuple_field |= rte_cpu_to_le_64(
378                                         hns3_set_rss_types[i].rss_field);
379                 }
380         }
381
382         ret = hns3_cmd_send(hw, &desc, 1);
383         if (ret) {
384                 hns3_err(hw, "Update RSS flow types tuples failed %d", ret);
385                 return ret;
386         }
387
388         tuple->rss_tuple_fields = rte_le_to_cpu_64(req->tuple_field);
389
390         return 0;
391 }
392
393 /*
394  * Configure RSS hash protocols and hash key.
395  * @param dev
396  *   Pointer to Ethernet device.
397  * @praram rss_conf
398  *   The configuration select of  rss key size and tuple flow_types.
399  * @return
400  *   0 on success, a negative errno value otherwise is set.
401  */
402 int
403 hns3_dev_rss_hash_update(struct rte_eth_dev *dev,
404                          struct rte_eth_rss_conf *rss_conf)
405 {
406         struct hns3_adapter *hns = dev->data->dev_private;
407         struct hns3_hw *hw = &hns->hw;
408         struct hns3_rss_tuple_cfg *tuple = &hw->rss_info.rss_tuple_sets;
409         struct hns3_rss_conf *rss_cfg = &hw->rss_info;
410         uint8_t key_len = rss_conf->rss_key_len;
411         uint64_t rss_hf = rss_conf->rss_hf;
412         uint8_t *key = rss_conf->rss_key;
413         int ret;
414
415         if (hw->rss_dis_flag)
416                 return -EINVAL;
417
418         rte_spinlock_lock(&hw->lock);
419         ret = hns3_set_rss_tuple_by_rss_hf(hw, tuple, rss_hf);
420         if (ret)
421                 goto conf_err;
422
423         if (rss_cfg->conf.types && rss_hf == 0) {
424                 /* Disable RSS, reset indirection table by local variable */
425                 ret = hns3_rss_reset_indir_table(hw);
426                 if (ret)
427                         goto conf_err;
428         } else if (rss_hf && rss_cfg->conf.types == 0) {
429                 /* Enable RSS, restore indirection table by hw's config */
430                 ret = hns3_set_rss_indir_table(hw, rss_cfg->rss_indirection_tbl,
431                                                hw->rss_ind_tbl_size);
432                 if (ret)
433                         goto conf_err;
434         }
435
436         /* Update supported flow types when set tuple success */
437         rss_cfg->conf.types = rss_hf;
438
439         if (key) {
440                 if (key_len != HNS3_RSS_KEY_SIZE) {
441                         hns3_err(hw, "The hash key len(%u) is invalid",
442                                  key_len);
443                         ret = -EINVAL;
444                         goto conf_err;
445                 }
446                 ret = hns3_rss_set_algo_key(hw, key);
447                 if (ret)
448                         goto conf_err;
449         }
450         rte_spinlock_unlock(&hw->lock);
451
452         return 0;
453
454 conf_err:
455         rte_spinlock_unlock(&hw->lock);
456         return ret;
457 }
458
459 /*
460  * Get rss key and rss_hf types set of RSS hash configuration.
461  * @param dev
462  *   Pointer to Ethernet device.
463  * @praram rss_conf
464  *   The buffer to get rss key size and tuple types.
465  * @return
466  *   0 on success.
467  */
468 int
469 hns3_dev_rss_hash_conf_get(struct rte_eth_dev *dev,
470                            struct rte_eth_rss_conf *rss_conf)
471 {
472         struct hns3_adapter *hns = dev->data->dev_private;
473         struct hns3_hw *hw = &hns->hw;
474         struct hns3_rss_conf *rss_cfg = &hw->rss_info;
475
476         rte_spinlock_lock(&hw->lock);
477         rss_conf->rss_hf = rss_cfg->conf.types;
478
479         /* Get the RSS Key required by the user */
480         if (rss_conf->rss_key && rss_conf->rss_key_len >= HNS3_RSS_KEY_SIZE) {
481                 memcpy(rss_conf->rss_key, rss_cfg->key, HNS3_RSS_KEY_SIZE);
482                 rss_conf->rss_key_len = HNS3_RSS_KEY_SIZE;
483         }
484         rte_spinlock_unlock(&hw->lock);
485
486         return 0;
487 }
488
489 /*
490  * Update rss redirection table of RSS.
491  * @param dev
492  *   Pointer to Ethernet device.
493  * @praram reta_conf
494  *   Pointer to the configuration select of mask and redirection tables.
495  * @param reta_size
496  *   Redirection table size.
497  * @return
498  *   0 on success, a negative errno value otherwise is set.
499  */
500 int
501 hns3_dev_rss_reta_update(struct rte_eth_dev *dev,
502                          struct rte_eth_rss_reta_entry64 *reta_conf,
503                          uint16_t reta_size)
504 {
505         struct hns3_adapter *hns = dev->data->dev_private;
506         struct hns3_hw *hw = &hns->hw;
507         struct hns3_rss_conf *rss_cfg = &hw->rss_info;
508         uint16_t indirection_tbl[HNS3_RSS_IND_TBL_SIZE_MAX];
509         uint16_t idx, shift;
510         uint16_t i;
511         int ret;
512
513         if (reta_size != hw->rss_ind_tbl_size) {
514                 hns3_err(hw, "The size of hash lookup table configured (%u)"
515                          "doesn't match the number hardware can supported"
516                          "(%u)", reta_size, hw->rss_ind_tbl_size);
517                 return -EINVAL;
518         }
519         rte_spinlock_lock(&hw->lock);
520         memcpy(indirection_tbl, rss_cfg->rss_indirection_tbl,
521                sizeof(rss_cfg->rss_indirection_tbl));
522         for (i = 0; i < reta_size; i++) {
523                 idx = i / RTE_ETH_RETA_GROUP_SIZE;
524                 shift = i % RTE_ETH_RETA_GROUP_SIZE;
525                 if (reta_conf[idx].reta[shift] >= hw->alloc_rss_size) {
526                         rte_spinlock_unlock(&hw->lock);
527                         hns3_err(hw, "queue id(%u) set to redirection table "
528                                  "exceeds queue number(%u) allocated to a TC",
529                                  reta_conf[idx].reta[shift],
530                                  hw->alloc_rss_size);
531                         return -EINVAL;
532                 }
533
534                 if (reta_conf[idx].mask & (1ULL << shift))
535                         indirection_tbl[i] = reta_conf[idx].reta[shift];
536         }
537
538         ret = hns3_set_rss_indir_table(hw, indirection_tbl,
539                                        hw->rss_ind_tbl_size);
540
541         rte_spinlock_unlock(&hw->lock);
542         return ret;
543 }
544
545 /*
546  * Get rss redirection table of RSS hash configuration.
547  * @param dev
548  *   Pointer to Ethernet device.
549  * @praram reta_conf
550  *   Pointer to the configuration select of mask and redirection tables.
551  * @param reta_size
552  *   Redirection table size.
553  * @return
554  *   0 on success, a negative errno value otherwise is set.
555  */
556 int
557 hns3_dev_rss_reta_query(struct rte_eth_dev *dev,
558                         struct rte_eth_rss_reta_entry64 *reta_conf,
559                         uint16_t reta_size)
560 {
561         struct hns3_adapter *hns = dev->data->dev_private;
562         struct hns3_hw *hw = &hns->hw;
563         struct hns3_rss_conf *rss_cfg = &hw->rss_info;
564         uint16_t idx, shift;
565         uint16_t i;
566
567         if (reta_size != hw->rss_ind_tbl_size) {
568                 hns3_err(hw, "The size of hash lookup table configured (%u)"
569                          " doesn't match the number hardware can supported"
570                          "(%u)", reta_size, hw->rss_ind_tbl_size);
571                 return -EINVAL;
572         }
573         rte_spinlock_lock(&hw->lock);
574         for (i = 0; i < reta_size; i++) {
575                 idx = i / RTE_ETH_RETA_GROUP_SIZE;
576                 shift = i % RTE_ETH_RETA_GROUP_SIZE;
577                 if (reta_conf[idx].mask & (1ULL << shift))
578                         reta_conf[idx].reta[shift] =
579                                                 rss_cfg->rss_indirection_tbl[i];
580         }
581         rte_spinlock_unlock(&hw->lock);
582         return 0;
583 }
584
585 /*
586  * Used to configure the tc_size and tc_offset.
587  */
588 static int
589 hns3_set_rss_tc_mode(struct hns3_hw *hw)
590 {
591         uint16_t rss_size = hw->alloc_rss_size;
592         struct hns3_rss_tc_mode_cmd *req;
593         uint16_t tc_offset[HNS3_MAX_TC_NUM];
594         uint8_t tc_valid[HNS3_MAX_TC_NUM];
595         uint16_t tc_size[HNS3_MAX_TC_NUM];
596         struct hns3_cmd_desc desc;
597         uint16_t roundup_size;
598         uint16_t i;
599         int ret;
600
601         req = (struct hns3_rss_tc_mode_cmd *)desc.data;
602
603         roundup_size = roundup_pow_of_two(rss_size);
604         roundup_size = ilog2(roundup_size);
605
606         for (i = 0; i < HNS3_MAX_TC_NUM; i++) {
607                 tc_valid[i] = !!(hw->hw_tc_map & BIT(i));
608                 tc_size[i] = roundup_size;
609                 tc_offset[i] = rss_size * i;
610         }
611
612         hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_RSS_TC_MODE, false);
613         for (i = 0; i < HNS3_MAX_TC_NUM; i++) {
614                 uint16_t mode = 0;
615
616                 hns3_set_bit(mode, HNS3_RSS_TC_VALID_B, (tc_valid[i] & 0x1));
617                 hns3_set_field(mode, HNS3_RSS_TC_SIZE_M, HNS3_RSS_TC_SIZE_S,
618                                tc_size[i]);
619                 if (tc_size[i] >> HNS3_RSS_TC_SIZE_MSB_OFFSET > 0)
620                         hns3_set_bit(mode, HNS3_RSS_TC_SIZE_MSB_S, 1);
621                 hns3_set_field(mode, HNS3_RSS_TC_OFFSET_M, HNS3_RSS_TC_OFFSET_S,
622                                tc_offset[i]);
623
624                 req->rss_tc_mode[i] = rte_cpu_to_le_16(mode);
625         }
626         ret = hns3_cmd_send(hw, &desc, 1);
627         if (ret)
628                 hns3_err(hw, "Sets rss tc mode failed %d", ret);
629
630         return ret;
631 }
632
633 static void
634 hns3_rss_tuple_uninit(struct hns3_hw *hw)
635 {
636         struct hns3_cmd_desc desc;
637         int ret;
638
639         hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_RSS_INPUT_TUPLE, false);
640
641         ret = hns3_cmd_send(hw, &desc, 1);
642         if (ret) {
643                 hns3_err(hw, "RSS uninit tuple failed %d", ret);
644                 return;
645         }
646 }
647
648 /*
649  * Set the default rss configuration in the init of driver.
650  */
651 void
652 hns3_rss_set_default_args(struct hns3_hw *hw)
653 {
654         struct hns3_rss_conf *rss_cfg = &hw->rss_info;
655         uint16_t queue_num = hw->alloc_rss_size;
656         int i;
657
658         /* Default hash algorithm */
659         rss_cfg->conf.func = RTE_ETH_HASH_FUNCTION_TOEPLITZ;
660
661         /* Default RSS key */
662         memcpy(rss_cfg->key, hns3_hash_key, HNS3_RSS_KEY_SIZE);
663
664         /* Initialize RSS indirection table */
665         for (i = 0; i < hw->rss_ind_tbl_size; i++)
666                 rss_cfg->rss_indirection_tbl[i] = i % queue_num;
667 }
668
669 /*
670  * RSS initialization for hns3 pmd driver.
671  */
672 int
673 hns3_config_rss(struct hns3_adapter *hns)
674 {
675         struct hns3_hw *hw = &hns->hw;
676         struct hns3_rss_conf *rss_cfg = &hw->rss_info;
677         uint8_t *hash_key = rss_cfg->key;
678         int ret, ret1;
679
680         enum rte_eth_rx_mq_mode mq_mode = hw->data->dev_conf.rxmode.mq_mode;
681
682         switch (hw->rss_info.conf.func) {
683         case RTE_ETH_HASH_FUNCTION_SIMPLE_XOR:
684                 hw->rss_info.hash_algo = HNS3_RSS_HASH_ALGO_SIMPLE;
685                 break;
686         case RTE_ETH_HASH_FUNCTION_SYMMETRIC_TOEPLITZ:
687                 hw->rss_info.hash_algo = HNS3_RSS_HASH_ALGO_SYMMETRIC_TOEP;
688                 break;
689         default:
690                 hw->rss_info.hash_algo = HNS3_RSS_HASH_ALGO_TOEPLITZ;
691                 break;
692         }
693
694         /* When RSS is off, redirect the packet queue 0 */
695         if (((uint32_t)mq_mode & RTE_ETH_MQ_RX_RSS_FLAG) == 0)
696                 hns3_rss_uninit(hns);
697
698         /* Configure RSS hash algorithm and hash key offset */
699         ret = hns3_rss_set_algo_key(hw, hash_key);
700         if (ret)
701                 return ret;
702
703         /* Configure the tuple selection for RSS hash input */
704         ret = hns3_rss_set_input_tuple(hw);
705         if (ret)
706                 return ret;
707
708         /*
709          * When RSS is off, it doesn't need to configure rss redirection table
710          * to hardware.
711          */
712         if (((uint32_t)mq_mode & RTE_ETH_MQ_RX_RSS_FLAG)) {
713                 ret = hns3_set_rss_indir_table(hw, rss_cfg->rss_indirection_tbl,
714                                                hw->rss_ind_tbl_size);
715                 if (ret)
716                         goto rss_tuple_uninit;
717         }
718
719         ret = hns3_set_rss_tc_mode(hw);
720         if (ret)
721                 goto rss_indir_table_uninit;
722
723         return ret;
724
725 rss_indir_table_uninit:
726         if (((uint32_t)mq_mode & RTE_ETH_MQ_RX_RSS_FLAG)) {
727                 ret1 = hns3_rss_reset_indir_table(hw);
728                 if (ret1 != 0)
729                         return ret;
730         }
731
732 rss_tuple_uninit:
733         hns3_rss_tuple_uninit(hw);
734
735         /* Disable RSS */
736         hw->rss_info.conf.types = 0;
737
738         return ret;
739 }
740
741 /*
742  * RSS uninitialization for hns3 pmd driver.
743  */
744 void
745 hns3_rss_uninit(struct hns3_adapter *hns)
746 {
747         struct hns3_hw *hw = &hns->hw;
748         int ret;
749
750         hns3_rss_tuple_uninit(hw);
751         ret = hns3_rss_reset_indir_table(hw);
752         if (ret != 0)
753                 return;
754
755         /* Disable RSS */
756         hw->rss_info.conf.types = 0;
757 }