Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright(c) 2018-2021 HiSilicon Limited.
3 : : */
4 : :
5 : : #include <ethdev_driver.h>
6 : : #include <rte_hash.h>
7 : : #include <rte_hash_crc.h>
8 : : #include <rte_io.h>
9 : : #include <rte_malloc.h>
10 : : #include <rte_common.h>
11 : :
12 : : #include "hns3_ethdev.h"
13 : : #include "hns3_logs.h"
14 : :
15 : : #define HNS3_VLAN_TAG_TYPE_NONE 0
16 : : #define HNS3_VLAN_TAG_TYPE_TAG2 1
17 : : #define HNS3_VLAN_TAG_TYPE_TAG1 2
18 : : #define HNS3_VLAN_TAG_TYPE_TAG1_2 3
19 : :
20 : : #define HNS3_PF_ID_S 0
21 : : #define HNS3_PF_ID_M GENMASK(2, 0)
22 : : #define HNS3_VF_ID_S 3
23 : : #define HNS3_VF_ID_M GENMASK(10, 3)
24 : : #define HNS3_PORT_TYPE_B 11
25 : : #define HNS3_NETWORK_PORT_ID_S 0
26 : : #define HNS3_NETWORK_PORT_ID_M GENMASK(3, 0)
27 : :
28 : : #define HNS3_FD_EPORT_SW_EN_B 0
29 : :
30 : : #define HNS3_FD_AD_DATA_S 32
31 : : #define HNS3_FD_AD_DROP_B 0
32 : : #define HNS3_FD_AD_DIRECT_QID_B 1
33 : : #define HNS3_FD_AD_QID_S 2
34 : : #define HNS3_FD_AD_QID_M GENMASK(11, 2)
35 : : #define HNS3_FD_AD_USE_COUNTER_B 12
36 : : #define HNS3_FD_AD_COUNTER_NUM_S 13
37 : : #define HNS3_FD_AD_COUNTER_NUM_M GENMASK(19, 13)
38 : : #define HNS3_FD_AD_NXT_STEP_B 20
39 : : #define HNS3_FD_AD_NXT_KEY_S 21
40 : : #define HNS3_FD_AD_NXT_KEY_M GENMASK(25, 21)
41 : : #define HNS3_FD_AD_WR_RULE_ID_B 0
42 : : #define HNS3_FD_AD_RULE_ID_S 1
43 : : #define HNS3_FD_AD_RULE_ID_M GENMASK(12, 1)
44 : : #define HNS3_FD_AD_QUEUE_REGION_EN_B 16
45 : : #define HNS3_FD_AD_QUEUE_REGION_SIZE_S 17
46 : : #define HNS3_FD_AD_QUEUE_REGION_SIZE_M GENMASK(20, 17)
47 : : #define HNS3_FD_AD_COUNTER_HIGH_BIT 7
48 : : #define HNS3_FD_AD_COUNTER_HIGH_BIT_B 26
49 : : #define HNS3_FD_AD_QUEUE_ID_HIGH_BIT 10
50 : : #define HNS3_FD_AD_QUEUE_ID_HIGH_BIT_B 21
51 : :
52 : : enum HNS3_PORT_TYPE {
53 : : HOST_PORT,
54 : : NETWORK_PORT
55 : : };
56 : :
57 : : enum HNS3_FD_MODE {
58 : : HNS3_FD_MODE_DEPTH_2K_WIDTH_400B_STAGE_1,
59 : : HNS3_FD_MODE_DEPTH_1K_WIDTH_400B_STAGE_2,
60 : : HNS3_FD_MODE_DEPTH_4K_WIDTH_200B_STAGE_1,
61 : : HNS3_FD_MODE_DEPTH_2K_WIDTH_200B_STAGE_2,
62 : : };
63 : :
64 : : enum HNS3_FD_KEY_TYPE {
65 : : HNS3_FD_KEY_BASE_ON_PTYPE,
66 : : HNS3_FD_KEY_BASE_ON_TUPLE,
67 : : };
68 : :
69 : : enum HNS3_FD_META_DATA {
70 : : PACKET_TYPE_ID,
71 : : IP_FRAGEMENT,
72 : : ROCE_TYPE,
73 : : NEXT_KEY,
74 : : VLAN_NUMBER,
75 : : SRC_VPORT,
76 : : DST_VPORT,
77 : : TUNNEL_PACKET,
78 : : MAX_META_DATA,
79 : : };
80 : :
81 : : struct key_info {
82 : : uint8_t key_type;
83 : : uint8_t key_length;
84 : : };
85 : :
86 : : static const struct key_info meta_data_key_info[] = {
87 : : {PACKET_TYPE_ID, 6},
88 : : {IP_FRAGEMENT, 1},
89 : : {ROCE_TYPE, 1},
90 : : {NEXT_KEY, 5},
91 : : {VLAN_NUMBER, 2},
92 : : {SRC_VPORT, 12},
93 : : {DST_VPORT, 12},
94 : : {TUNNEL_PACKET, 1},
95 : : };
96 : :
97 : : static const struct key_info tuple_key_info[] = {
98 : : {OUTER_DST_MAC, 48},
99 : : {OUTER_SRC_MAC, 48},
100 : : {OUTER_VLAN_TAG_FST, 16},
101 : : {OUTER_VLAN_TAG_SEC, 16},
102 : : {OUTER_ETH_TYPE, 16},
103 : : {OUTER_L2_RSV, 16},
104 : : {OUTER_IP_TOS, 8},
105 : : {OUTER_IP_PROTO, 8},
106 : : {OUTER_SRC_IP, 32},
107 : : {OUTER_DST_IP, 32},
108 : : {OUTER_L3_RSV, 16},
109 : : {OUTER_SRC_PORT, 16},
110 : : {OUTER_DST_PORT, 16},
111 : : {OUTER_L4_RSV, 32},
112 : : {OUTER_TUN_VNI, 24},
113 : : {OUTER_TUN_FLOW_ID, 8},
114 : : {INNER_DST_MAC, 48},
115 : : {INNER_SRC_MAC, 48},
116 : : {INNER_VLAN_TAG1, 16},
117 : : {INNER_VLAN_TAG2, 16},
118 : : {INNER_ETH_TYPE, 16},
119 : : {INNER_L2_RSV, 16},
120 : : {INNER_IP_TOS, 8},
121 : : {INNER_IP_PROTO, 8},
122 : : {INNER_SRC_IP, 32},
123 : : {INNER_DST_IP, 32},
124 : : {INNER_L3_RSV, 16},
125 : : {INNER_SRC_PORT, 16},
126 : : {INNER_DST_PORT, 16},
127 : : {INNER_SCTP_TAG, 32},
128 : : };
129 : :
130 : : #define MAX_KEY_LENGTH 400
131 : : #define MAX_200B_KEY_LENGTH 200
132 : : #define MAX_META_DATA_LENGTH 16
133 : : #define MAX_KEY_DWORDS RTE_DIV_ROUND_UP(MAX_KEY_LENGTH / HNS3_BITS_PER_BYTE, 4)
134 : : #define MAX_KEY_BYTES (MAX_KEY_DWORDS * 4)
135 : :
136 : : enum HNS3_FD_PACKET_TYPE {
137 : : NIC_PACKET,
138 : : ROCE_PACKET,
139 : : };
140 : :
141 : : /* For each bit of TCAM entry, it uses a pair of 'x' and
142 : : * 'y' to indicate which value to match, like below:
143 : : * ----------------------------------
144 : : * | bit x | bit y | search value |
145 : : * ----------------------------------
146 : : * | 0 | 0 | always hit |
147 : : * ----------------------------------
148 : : * | 1 | 0 | match '0' |
149 : : * ----------------------------------
150 : : * | 0 | 1 | match '1' |
151 : : * ----------------------------------
152 : : * | 1 | 1 | invalid |
153 : : * ----------------------------------
154 : : * Then for input key(k) and mask(v), we can calculate the value by
155 : : * the formulae:
156 : : * x = (~k) & v
157 : : * y = k & v
158 : : */
159 : : #define calc_x(x, k, v) ((x) = (~(k) & (v)))
160 : : #define calc_y(y, k, v) ((y) = ((k) & (v)))
161 : :
162 : : struct hns3_fd_tcam_config_1_cmd {
163 : : uint8_t stage;
164 : : uint8_t xy_sel;
165 : : uint8_t port_info;
166 : : uint8_t rsv1[1];
167 : : rte_le32_t index;
168 : : uint8_t entry_vld;
169 : : uint8_t rsv2[7];
170 : : uint8_t tcam_data[8];
171 : : };
172 : :
173 : : struct hns3_fd_tcam_config_2_cmd {
174 : : uint8_t tcam_data[24];
175 : : };
176 : :
177 : : struct hns3_fd_tcam_config_3_cmd {
178 : : uint8_t tcam_data[20];
179 : : uint8_t rsv[4];
180 : : };
181 : :
182 : : struct hns3_get_fd_mode_cmd {
183 : : uint8_t mode;
184 : : uint8_t enable;
185 : : uint8_t rsv[22];
186 : : };
187 : :
188 : : struct hns3_get_fd_allocation_cmd {
189 : : rte_le32_t stage1_entry_num;
190 : : rte_le32_t stage2_entry_num;
191 : : rte_le16_t stage1_counter_num;
192 : : rte_le16_t stage2_counter_num;
193 : : uint8_t rsv[12];
194 : : };
195 : :
196 : : struct hns3_set_fd_key_config_cmd {
197 : : uint8_t stage;
198 : : uint8_t key_select;
199 : : uint8_t inner_sipv6_word_en;
200 : : uint8_t inner_dipv6_word_en;
201 : : uint8_t outer_sipv6_word_en;
202 : : uint8_t outer_dipv6_word_en;
203 : : uint8_t rsv1[2];
204 : : rte_le32_t tuple_mask;
205 : : rte_le32_t meta_data_mask;
206 : : uint8_t rsv2[8];
207 : : };
208 : :
209 : : struct hns3_fd_ad_config_cmd {
210 : : uint8_t stage;
211 : : uint8_t rsv1[3];
212 : : rte_le32_t index;
213 : : rte_le64_t ad_data;
214 : : uint8_t rsv2[8];
215 : : };
216 : :
217 : : struct hns3_fd_get_cnt_cmd {
218 : : uint8_t stage;
219 : : uint8_t rsv1[3];
220 : : rte_le16_t index;
221 : : uint8_t rsv2[2];
222 : : rte_le64_t value;
223 : : uint8_t rsv3[8];
224 : : };
225 : :
226 : 0 : static int hns3_get_fd_mode(struct hns3_hw *hw, uint8_t *fd_mode)
227 : : {
228 : : struct hns3_get_fd_mode_cmd *req;
229 : : struct hns3_cmd_desc desc;
230 : : int ret;
231 : :
232 : 0 : hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_FD_MODE_CTRL, true);
233 : :
234 : : req = (struct hns3_get_fd_mode_cmd *)desc.data;
235 : :
236 : 0 : ret = hns3_cmd_send(hw, &desc, 1);
237 [ # # ]: 0 : if (ret) {
238 : 0 : hns3_err(hw, "Get fd mode fail, ret=%d", ret);
239 : 0 : return ret;
240 : : }
241 : :
242 : 0 : *fd_mode = req->mode;
243 : :
244 : 0 : return ret;
245 : : }
246 : :
247 : 0 : static int hns3_get_fd_allocation(struct hns3_hw *hw,
248 : : uint32_t *stage1_entry_num,
249 : : uint32_t *stage2_entry_num,
250 : : uint16_t *stage1_counter_num,
251 : : uint16_t *stage2_counter_num)
252 : : {
253 : : struct hns3_get_fd_allocation_cmd *req;
254 : : struct hns3_cmd_desc desc;
255 : : int ret;
256 : :
257 : 0 : hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_FD_GET_ALLOCATION, true);
258 : :
259 : : req = (struct hns3_get_fd_allocation_cmd *)desc.data;
260 : :
261 : 0 : ret = hns3_cmd_send(hw, &desc, 1);
262 [ # # ]: 0 : if (ret) {
263 : 0 : hns3_err(hw, "Query fd allocation fail, ret=%d", ret);
264 : 0 : return ret;
265 : : }
266 : :
267 : 0 : *stage1_entry_num = rte_le_to_cpu_32(req->stage1_entry_num);
268 : 0 : *stage2_entry_num = rte_le_to_cpu_32(req->stage2_entry_num);
269 : 0 : *stage1_counter_num = rte_le_to_cpu_16(req->stage1_counter_num);
270 : 0 : *stage2_counter_num = rte_le_to_cpu_16(req->stage2_counter_num);
271 : :
272 : 0 : return ret;
273 : : }
274 : :
275 : 0 : static int hns3_set_fd_key_config(struct hns3_adapter *hns)
276 : : {
277 : : struct hns3_set_fd_key_config_cmd *req;
278 : : struct hns3_fd_key_cfg *key_cfg;
279 : : struct hns3_pf *pf = &hns->pf;
280 : 0 : struct hns3_hw *hw = &hns->hw;
281 : : struct hns3_cmd_desc desc;
282 : : int ret;
283 : :
284 : 0 : hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_FD_KEY_CONFIG, false);
285 : :
286 : : req = (struct hns3_set_fd_key_config_cmd *)desc.data;
287 : : key_cfg = &pf->fdir.fd_cfg.key_cfg[HNS3_FD_STAGE_1];
288 : 0 : req->stage = HNS3_FD_STAGE_1;
289 : 0 : req->key_select = key_cfg->key_sel;
290 : 0 : req->inner_sipv6_word_en = key_cfg->inner_sipv6_word_en;
291 : 0 : req->inner_dipv6_word_en = key_cfg->inner_dipv6_word_en;
292 : 0 : req->outer_sipv6_word_en = key_cfg->outer_sipv6_word_en;
293 : 0 : req->outer_dipv6_word_en = key_cfg->outer_dipv6_word_en;
294 : 0 : req->tuple_mask = rte_cpu_to_le_32(~key_cfg->tuple_active);
295 : 0 : req->meta_data_mask = rte_cpu_to_le_32(~key_cfg->meta_data_active);
296 : :
297 : 0 : ret = hns3_cmd_send(hw, &desc, 1);
298 [ # # ]: 0 : if (ret)
299 : 0 : hns3_err(hw, "Set fd key fail, ret=%d", ret);
300 : :
301 : 0 : return ret;
302 : : }
303 : :
304 : 0 : static void hns3_set_tuple_config(struct hns3_adapter *hns,
305 : : struct hns3_fd_key_cfg *key_cfg)
306 : : {
307 : 0 : enum hns3_fdir_tuple_config tuple_cfg = hns->pf.fdir.tuple_cfg;
308 : :
309 [ # # ]: 0 : if (tuple_cfg == HNS3_FDIR_TUPLE_CONFIG_DEFAULT)
310 : : return;
311 : :
312 [ # # ]: 0 : if (hns->pf.fdir.fd_cfg.max_key_length != MAX_KEY_LENGTH) {
313 : 0 : hns3_warn(&hns->hw, "fdir tuple config only valid with 400bit key!");
314 : 0 : return;
315 : : }
316 : :
317 [ # # # # : 0 : switch (tuple_cfg) {
# # # ]
318 : 0 : case HNS3_FDIR_TUPLE_OUTVLAN_REPLACE_INSMAC:
319 : 0 : key_cfg->tuple_active &= ~BIT(INNER_SRC_MAC);
320 : 0 : key_cfg->tuple_active |= BIT(OUTER_VLAN_TAG_FST);
321 : 0 : key_cfg->tuple_active |= BIT(OUTER_VLAN_TAG_SEC);
322 : 0 : break;
323 : 0 : case HNS3_FDIR_TUPLE_OUTVLAN_REPLACE_INDMAC:
324 : 0 : key_cfg->tuple_active &= ~BIT(INNER_DST_MAC);
325 : 0 : key_cfg->tuple_active |= BIT(OUTER_VLAN_TAG_FST);
326 : 0 : key_cfg->tuple_active |= BIT(OUTER_VLAN_TAG_SEC);
327 : 0 : break;
328 : 0 : case HNS3_FDIR_TUPLE_OUTVLAN_REPLACE_INSIP:
329 : 0 : key_cfg->tuple_active &= ~BIT(INNER_SRC_IP);
330 : 0 : key_cfg->tuple_active |= BIT(OUTER_VLAN_TAG_FST);
331 : 0 : key_cfg->tuple_active |= BIT(OUTER_VLAN_TAG_SEC);
332 : 0 : break;
333 : 0 : case HNS3_FDIR_TUPLE_OUTVLAN_REPLACE_INDIP:
334 : 0 : key_cfg->tuple_active &= ~BIT(INNER_DST_IP);
335 : 0 : key_cfg->tuple_active |= BIT(OUTER_VLAN_TAG_FST);
336 : 0 : key_cfg->tuple_active |= BIT(OUTER_VLAN_TAG_SEC);
337 : 0 : break;
338 : 0 : case HNS3_FDIR_TUPLE_OUTVLAN_REPLACE_SCTPTAG:
339 : 0 : key_cfg->tuple_active &= ~BIT(INNER_SCTP_TAG);
340 : 0 : key_cfg->tuple_active |= BIT(OUTER_VLAN_TAG_FST);
341 : 0 : key_cfg->tuple_active |= BIT(OUTER_VLAN_TAG_SEC);
342 : 0 : break;
343 : 0 : case HNS3_FDIR_TUPLE_OUTVLAN_REPLACE_TUNVNI:
344 : 0 : key_cfg->tuple_active &= ~BIT(OUTER_TUN_VNI);
345 : 0 : key_cfg->tuple_active |= BIT(OUTER_VLAN_TAG_FST);
346 : 0 : key_cfg->tuple_active |= BIT(OUTER_VLAN_TAG_SEC);
347 : 0 : break;
348 : 0 : default:
349 : 0 : hns3_err(&hns->hw, "invalid fdir tuple config %u!", tuple_cfg);
350 : 0 : return;
351 : : }
352 : :
353 : 0 : hns3_info(&hns->hw, "fdir tuple config %s!", hns3_tuple_config_name(tuple_cfg));
354 : : }
355 : :
356 : 0 : int hns3_init_fd_config(struct hns3_adapter *hns)
357 : : {
358 : : struct hns3_pf *pf = &hns->pf;
359 : 0 : struct hns3_hw *hw = &hns->hw;
360 : : struct hns3_fd_key_cfg *key_cfg;
361 : : int ret;
362 : :
363 : 0 : ret = hns3_get_fd_mode(hw, &pf->fdir.fd_cfg.fd_mode);
364 [ # # ]: 0 : if (ret)
365 : : return ret;
366 : :
367 [ # # # ]: 0 : switch (pf->fdir.fd_cfg.fd_mode) {
368 : 0 : case HNS3_FD_MODE_DEPTH_2K_WIDTH_400B_STAGE_1:
369 : 0 : pf->fdir.fd_cfg.max_key_length = MAX_KEY_LENGTH;
370 : 0 : break;
371 : 0 : case HNS3_FD_MODE_DEPTH_4K_WIDTH_200B_STAGE_1:
372 : 0 : pf->fdir.fd_cfg.max_key_length = MAX_200B_KEY_LENGTH;
373 : 0 : hns3_warn(hw, "Unsupported tunnel filter in 4K*200Bit");
374 : 0 : break;
375 : 0 : default:
376 : 0 : hns3_err(hw, "Unsupported flow director mode %u",
377 : : pf->fdir.fd_cfg.fd_mode);
378 : 0 : return -EOPNOTSUPP;
379 : : }
380 : :
381 : 0 : key_cfg = &pf->fdir.fd_cfg.key_cfg[HNS3_FD_STAGE_1];
382 : 0 : key_cfg->key_sel = HNS3_FD_KEY_BASE_ON_TUPLE;
383 : 0 : key_cfg->inner_sipv6_word_en = IPV6_ADDR_WORD_MASK;
384 : 0 : key_cfg->inner_dipv6_word_en = IPV6_ADDR_WORD_MASK;
385 : 0 : key_cfg->outer_sipv6_word_en = 0;
386 : 0 : key_cfg->outer_dipv6_word_en = 0;
387 : :
388 : 0 : key_cfg->tuple_active = BIT(INNER_VLAN_TAG1) | BIT(INNER_ETH_TYPE) |
389 : : BIT(INNER_IP_PROTO) | BIT(INNER_IP_TOS) |
390 : : BIT(INNER_SRC_IP) | BIT(INNER_DST_IP) |
391 : : BIT(INNER_SRC_PORT) | BIT(INNER_DST_PORT);
392 : 0 : hns3_dbg(hw, "fdir tuple: inner<vlan_tag1 eth_type ip_src ip_dst "
393 : : "ip_proto ip_tos l4_src_port l4_dst_port>");
394 : :
395 : : /* If use max 400bit key, we can support tuples for ether type */
396 [ # # ]: 0 : if (pf->fdir.fd_cfg.max_key_length == MAX_KEY_LENGTH) {
397 : 0 : key_cfg->tuple_active |=
398 : : BIT(INNER_DST_MAC) | BIT(INNER_SRC_MAC) |
399 : : BIT(OUTER_SRC_PORT) | BIT(INNER_SCTP_TAG) |
400 : : BIT(OUTER_DST_PORT) | BIT(INNER_VLAN_TAG2) |
401 : : BIT(OUTER_TUN_VNI) | BIT(OUTER_TUN_FLOW_ID) |
402 : : BIT(OUTER_ETH_TYPE) | BIT(OUTER_IP_PROTO);
403 : 0 : hns3_dbg(hw, "fdir tuple more: inner<dst_mac src_mac "
404 : : "vlan_tag2 sctp_tag> outer<eth_type ip_proto "
405 : : "l4_src_port l4_dst_port tun_vni tun_flow_id>");
406 : : }
407 : :
408 : 0 : hns3_set_tuple_config(hns, key_cfg);
409 : :
410 : : /* roce_type is used to filter roce frames
411 : : * dst_vport is used to specify the rule
412 : : */
413 : 0 : key_cfg->meta_data_active = BIT(DST_VPORT) | BIT(TUNNEL_PACKET);
414 [ # # ]: 0 : if (pf->fdir.vlan_match_mode)
415 : 0 : key_cfg->meta_data_active |= BIT(VLAN_NUMBER);
416 : :
417 [ # # ]: 0 : hns3_dbg(hw, "fdir meta data: dst_vport tunnel_packet %s",
418 : : (pf->fdir.vlan_match_mode == HNS3_FDIR_VLAN_STRICT_MATCH) ?
419 : : "vlan_number" : "");
420 : :
421 : 0 : ret = hns3_get_fd_allocation(hw,
422 : : &pf->fdir.fd_cfg.rule_num[HNS3_FD_STAGE_1],
423 : : &pf->fdir.fd_cfg.rule_num[HNS3_FD_STAGE_2],
424 : : &pf->fdir.fd_cfg.cnt_num[HNS3_FD_STAGE_1],
425 : : &pf->fdir.fd_cfg.cnt_num[HNS3_FD_STAGE_2]);
426 [ # # ]: 0 : if (ret)
427 : : return ret;
428 : :
429 : 0 : hns3_dbg(hw, "fdir: stage1<rules-%u counters-%u> stage2<rules-%u counters=%u>",
430 : : pf->fdir.fd_cfg.rule_num[HNS3_FD_STAGE_1],
431 : : pf->fdir.fd_cfg.cnt_num[HNS3_FD_STAGE_1],
432 : : pf->fdir.fd_cfg.rule_num[HNS3_FD_STAGE_2],
433 : : pf->fdir.fd_cfg.cnt_num[HNS3_FD_STAGE_2]);
434 : :
435 : 0 : return hns3_set_fd_key_config(hns);
436 : : }
437 : :
438 : 0 : static int hns3_fd_tcam_config(struct hns3_hw *hw, bool sel_x, int loc,
439 : : uint8_t *key, bool is_add)
440 : : {
441 : : #define FD_TCAM_CMD_NUM 3
442 : : struct hns3_fd_tcam_config_1_cmd *req1;
443 : : struct hns3_fd_tcam_config_2_cmd *req2;
444 : : struct hns3_fd_tcam_config_3_cmd *req3;
445 : : struct hns3_cmd_desc desc[FD_TCAM_CMD_NUM];
446 : : int len;
447 : : int ret;
448 : :
449 : 0 : hns3_cmd_setup_basic_desc(&desc[0], HNS3_OPC_FD_TCAM_OP, false);
450 : 0 : desc[0].flag |= rte_cpu_to_le_16(HNS3_CMD_FLAG_NEXT);
451 : 0 : hns3_cmd_setup_basic_desc(&desc[1], HNS3_OPC_FD_TCAM_OP, false);
452 : 0 : desc[1].flag |= rte_cpu_to_le_16(HNS3_CMD_FLAG_NEXT);
453 : 0 : hns3_cmd_setup_basic_desc(&desc[2], HNS3_OPC_FD_TCAM_OP, false);
454 : :
455 : : req1 = (struct hns3_fd_tcam_config_1_cmd *)desc[0].data;
456 : : req2 = (struct hns3_fd_tcam_config_2_cmd *)desc[1].data;
457 : : req3 = (struct hns3_fd_tcam_config_3_cmd *)desc[2].data;
458 : :
459 : 0 : req1->stage = HNS3_FD_STAGE_1;
460 : 0 : req1->xy_sel = sel_x ? 1 : 0;
461 : 0 : hns3_set_bit(req1->port_info, HNS3_FD_EPORT_SW_EN_B, 0);
462 : 0 : req1->index = rte_cpu_to_le_32(loc);
463 [ # # ]: 0 : req1->entry_vld = sel_x ? is_add : 0;
464 : :
465 [ # # ]: 0 : if (key) {
466 : : len = sizeof(req1->tcam_data);
467 : : memcpy(req1->tcam_data, key, len);
468 : 0 : key += len;
469 : :
470 : : len = sizeof(req2->tcam_data);
471 : : memcpy(req2->tcam_data, key, len);
472 : 0 : key += len;
473 : :
474 : : len = sizeof(req3->tcam_data);
475 : : memcpy(req3->tcam_data, key, len);
476 : : }
477 : :
478 : 0 : ret = hns3_cmd_send(hw, desc, FD_TCAM_CMD_NUM);
479 [ # # ]: 0 : if (ret)
480 : 0 : hns3_err(hw, "Config tcam key fail, ret=%d loc=%d add=%d",
481 : : ret, loc, is_add);
482 : 0 : return ret;
483 : : }
484 : :
485 : 0 : static int hns3_fd_ad_config(struct hns3_hw *hw, int loc,
486 : : struct hns3_fd_ad_data *action)
487 : : {
488 : : struct hns3_fd_ad_config_cmd *req;
489 : : struct hns3_cmd_desc desc;
490 : : uint64_t ad_data = 0;
491 : : int ret;
492 : :
493 : 0 : hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_FD_AD_OP, false);
494 : :
495 : : req = (struct hns3_fd_ad_config_cmd *)desc.data;
496 : 0 : req->index = rte_cpu_to_le_32(loc);
497 : 0 : req->stage = HNS3_FD_STAGE_1;
498 : :
499 : 0 : hns3_set_bit(ad_data, HNS3_FD_AD_WR_RULE_ID_B,
500 : : action->write_rule_id_to_bd);
501 : 0 : hns3_set_field(ad_data, HNS3_FD_AD_RULE_ID_M, HNS3_FD_AD_RULE_ID_S,
502 : : action->rule_id);
503 [ # # ]: 0 : if (action->nb_queues > 1) {
504 : 0 : hns3_set_bit(ad_data, HNS3_FD_AD_QUEUE_REGION_EN_B, 1);
505 : 0 : hns3_set_field(ad_data, HNS3_FD_AD_QUEUE_REGION_SIZE_M,
506 : : HNS3_FD_AD_QUEUE_REGION_SIZE_S,
507 : : rte_log2_u32(action->nb_queues));
508 : : }
509 : : /* set extend bit if counter_id is in [128 ~ 255] */
510 [ # # ]: 0 : if (action->counter_id & BIT(HNS3_FD_AD_COUNTER_HIGH_BIT))
511 : 0 : hns3_set_bit(ad_data, HNS3_FD_AD_COUNTER_HIGH_BIT_B, 1);
512 : : /* set extend bit if queue id > 1024 */
513 [ # # ]: 0 : if (action->queue_id & BIT(HNS3_FD_AD_QUEUE_ID_HIGH_BIT))
514 : 0 : hns3_set_bit(ad_data, HNS3_FD_AD_QUEUE_ID_HIGH_BIT_B, 1);
515 : 0 : ad_data <<= HNS3_FD_AD_DATA_S;
516 : 0 : hns3_set_bit(ad_data, HNS3_FD_AD_DROP_B, action->drop_packet);
517 [ # # ]: 0 : if (action->nb_queues == 1)
518 : 0 : hns3_set_bit(ad_data, HNS3_FD_AD_DIRECT_QID_B, 1);
519 : 0 : hns3_set_field(ad_data, HNS3_FD_AD_QID_M, HNS3_FD_AD_QID_S,
520 : : action->queue_id);
521 : 0 : hns3_set_bit(ad_data, HNS3_FD_AD_USE_COUNTER_B, action->use_counter);
522 : 0 : hns3_set_field(ad_data, HNS3_FD_AD_COUNTER_NUM_M,
523 : : HNS3_FD_AD_COUNTER_NUM_S, action->counter_id);
524 : 0 : hns3_set_bit(ad_data, HNS3_FD_AD_NXT_STEP_B, action->use_next_stage);
525 : 0 : hns3_set_field(ad_data, HNS3_FD_AD_NXT_KEY_M, HNS3_FD_AD_NXT_KEY_S,
526 : : action->next_input_key);
527 : :
528 : 0 : req->ad_data = rte_cpu_to_le_64(ad_data);
529 : 0 : ret = hns3_cmd_send(hw, &desc, 1);
530 [ # # ]: 0 : if (ret)
531 : 0 : hns3_err(hw, "Config fd ad fail, ret=%d loc=%d", ret, loc);
532 : :
533 : 0 : return ret;
534 : : }
535 : :
536 : : static inline void hns3_fd_convert_mac(uint8_t *key, uint8_t *mask,
537 : : uint8_t *mac_x, uint8_t *mac_y)
538 : : {
539 : : uint8_t tmp;
540 : : int i;
541 : :
542 [ # # # # ]: 0 : for (i = 0; i < RTE_ETHER_ADDR_LEN; i++) {
543 : 0 : tmp = RTE_ETHER_ADDR_LEN - 1 - i;
544 : 0 : calc_x(mac_x[tmp], key[i], mask[i]);
545 : 0 : calc_y(mac_y[tmp], key[i], mask[i]);
546 : : }
547 : : }
548 : :
549 : 0 : static void hns3_fd_convert_int16(uint32_t tuple, struct hns3_fdir_rule *rule,
550 : : uint8_t *val_x, uint8_t *val_y)
551 : : {
552 : : uint16_t tmp_x_s;
553 : : uint16_t tmp_y_s;
554 : : uint16_t mask;
555 : : uint16_t key;
556 : :
557 [ # # # # : 0 : switch (tuple) {
# # # # #
# ]
558 : 0 : case OUTER_VLAN_TAG_FST:
559 : 0 : key = rule->key_conf.spec.outer_vlan_tag1;
560 : 0 : mask = rule->key_conf.mask.outer_vlan_tag1;
561 : 0 : break;
562 : 0 : case OUTER_VLAN_TAG_SEC:
563 : 0 : key = rule->key_conf.spec.outer_vlan_tag2;
564 : 0 : mask = rule->key_conf.mask.outer_vlan_tag2;
565 : 0 : break;
566 : 0 : case OUTER_SRC_PORT:
567 : 0 : key = rule->key_conf.spec.outer_src_port;
568 : 0 : mask = rule->key_conf.mask.outer_src_port;
569 : 0 : break;
570 : 0 : case OUTER_DST_PORT:
571 : 0 : key = rule->key_conf.spec.tunnel_type;
572 : 0 : mask = rule->key_conf.mask.tunnel_type;
573 : 0 : break;
574 : 0 : case OUTER_ETH_TYPE:
575 : 0 : key = rule->key_conf.spec.outer_ether_type;
576 : 0 : mask = rule->key_conf.mask.outer_ether_type;
577 : 0 : break;
578 : 0 : case INNER_SRC_PORT:
579 : 0 : key = rule->key_conf.spec.src_port;
580 : 0 : mask = rule->key_conf.mask.src_port;
581 : 0 : break;
582 : 0 : case INNER_DST_PORT:
583 : 0 : key = rule->key_conf.spec.dst_port;
584 : 0 : mask = rule->key_conf.mask.dst_port;
585 : 0 : break;
586 : 0 : case INNER_VLAN_TAG1:
587 : 0 : key = rule->key_conf.spec.vlan_tag1;
588 : 0 : mask = rule->key_conf.mask.vlan_tag1;
589 : 0 : break;
590 : 0 : case INNER_VLAN_TAG2:
591 : 0 : key = rule->key_conf.spec.vlan_tag2;
592 : 0 : mask = rule->key_conf.mask.vlan_tag2;
593 : 0 : break;
594 : 0 : default:
595 : : /* INNER_ETH_TYPE: */
596 : 0 : key = rule->key_conf.spec.ether_type;
597 : 0 : mask = rule->key_conf.mask.ether_type;
598 : 0 : break;
599 : : }
600 : 0 : calc_x(tmp_x_s, key, mask);
601 : 0 : calc_y(tmp_y_s, key, mask);
602 : 0 : val_x[0] = rte_cpu_to_le_16(tmp_x_s) & 0xFF;
603 : 0 : val_x[1] = rte_cpu_to_le_16(tmp_x_s) >> HNS3_BITS_PER_BYTE;
604 : 0 : val_y[0] = rte_cpu_to_le_16(tmp_y_s) & 0xFF;
605 : 0 : val_y[1] = rte_cpu_to_le_16(tmp_y_s) >> HNS3_BITS_PER_BYTE;
606 : 0 : }
607 : :
608 : 0 : static inline void hns3_fd_convert_int32(uint32_t key, uint32_t mask,
609 : : uint8_t *val_x, uint8_t *val_y)
610 : : {
611 : : uint32_t tmp_x_l;
612 : : uint32_t tmp_y_l;
613 : :
614 : 0 : calc_x(tmp_x_l, key, mask);
615 : 0 : calc_y(tmp_y_l, key, mask);
616 : : memcpy(val_x, &tmp_x_l, sizeof(tmp_x_l));
617 : : memcpy(val_y, &tmp_y_l, sizeof(tmp_y_l));
618 : 0 : }
619 : :
620 : 0 : static bool hns3_fd_convert_tuple(struct hns3_hw *hw,
621 : : uint32_t tuple, uint8_t *key_x,
622 : : uint8_t *key_y, struct hns3_fdir_rule *rule)
623 : : {
624 : : struct hns3_fdir_key_conf *key_conf;
625 : : int tmp;
626 : : int i;
627 : :
628 [ # # ]: 0 : if ((rule->input_set & BIT(tuple)) == 0)
629 : : return true;
630 : :
631 : : key_conf = &rule->key_conf;
632 [ # # # # : 0 : switch (tuple) {
# # # # #
# # # ]
633 : 0 : case INNER_DST_MAC:
634 : 0 : hns3_fd_convert_mac(key_conf->spec.dst_mac,
635 : 0 : key_conf->mask.dst_mac, key_x, key_y);
636 : : break;
637 : 0 : case INNER_SRC_MAC:
638 : 0 : hns3_fd_convert_mac(key_conf->spec.src_mac,
639 : 0 : key_conf->mask.src_mac, key_x, key_y);
640 : : break;
641 : 0 : case OUTER_VLAN_TAG_FST:
642 : : case OUTER_VLAN_TAG_SEC:
643 : : case OUTER_SRC_PORT:
644 : : case OUTER_DST_PORT:
645 : : case OUTER_ETH_TYPE:
646 : : case INNER_SRC_PORT:
647 : : case INNER_DST_PORT:
648 : : case INNER_VLAN_TAG1:
649 : : case INNER_VLAN_TAG2:
650 : : case INNER_ETH_TYPE:
651 : 0 : hns3_fd_convert_int16(tuple, rule, key_x, key_y);
652 : 0 : break;
653 : 0 : case INNER_SRC_IP:
654 : 0 : hns3_fd_convert_int32(key_conf->spec.src_ip[IP_ADDR_KEY_ID],
655 : : key_conf->mask.src_ip[IP_ADDR_KEY_ID],
656 : : key_x, key_y);
657 : 0 : break;
658 : 0 : case INNER_DST_IP:
659 : 0 : hns3_fd_convert_int32(key_conf->spec.dst_ip[IP_ADDR_KEY_ID],
660 : : key_conf->mask.dst_ip[IP_ADDR_KEY_ID],
661 : : key_x, key_y);
662 : 0 : break;
663 : 0 : case INNER_SCTP_TAG:
664 : 0 : hns3_fd_convert_int32(key_conf->spec.sctp_tag,
665 : : key_conf->mask.sctp_tag, key_x, key_y);
666 : 0 : break;
667 : : case OUTER_TUN_VNI:
668 [ # # ]: 0 : for (i = 0; i < VNI_OR_TNI_LEN; i++) {
669 : 0 : tmp = VNI_OR_TNI_LEN - 1 - i;
670 : 0 : calc_x(key_x[tmp],
671 : : key_conf->spec.outer_tun_vni[i],
672 : : key_conf->mask.outer_tun_vni[i]);
673 : 0 : calc_y(key_y[tmp],
674 : : key_conf->spec.outer_tun_vni[i],
675 : : key_conf->mask.outer_tun_vni[i]);
676 : : }
677 : : break;
678 : 0 : case OUTER_TUN_FLOW_ID:
679 : 0 : calc_x(*key_x, key_conf->spec.outer_tun_flow_id,
680 : : key_conf->mask.outer_tun_flow_id);
681 : 0 : calc_y(*key_y, key_conf->spec.outer_tun_flow_id,
682 : : key_conf->mask.outer_tun_flow_id);
683 : 0 : break;
684 : 0 : case INNER_IP_TOS:
685 : 0 : calc_x(*key_x, key_conf->spec.ip_tos, key_conf->mask.ip_tos);
686 : 0 : calc_y(*key_y, key_conf->spec.ip_tos, key_conf->mask.ip_tos);
687 : 0 : break;
688 : 0 : case OUTER_IP_PROTO:
689 : 0 : calc_x(*key_x, key_conf->spec.outer_proto,
690 : : key_conf->mask.outer_proto);
691 : 0 : calc_y(*key_y, key_conf->spec.outer_proto,
692 : : key_conf->mask.outer_proto);
693 : 0 : break;
694 : 0 : case INNER_IP_PROTO:
695 : 0 : calc_x(*key_x, key_conf->spec.ip_proto,
696 : : key_conf->mask.ip_proto);
697 : 0 : calc_y(*key_y, key_conf->spec.ip_proto,
698 : : key_conf->mask.ip_proto);
699 : 0 : break;
700 : 0 : default:
701 : 0 : hns3_warn(hw, "not support tuple of (%u)", tuple);
702 : 0 : return false;
703 : : }
704 : : return true;
705 : : }
706 : :
707 : : static uint32_t hns3_get_port_number(uint8_t pf_id, uint8_t vf_id)
708 : : {
709 : : uint32_t port_number = 0;
710 : :
711 : : hns3_set_field(port_number, HNS3_PF_ID_M, HNS3_PF_ID_S, pf_id);
712 : 0 : hns3_set_field(port_number, HNS3_VF_ID_M, HNS3_VF_ID_S, vf_id);
713 : : hns3_set_bit(port_number, HNS3_PORT_TYPE_B, HOST_PORT);
714 : :
715 : : return port_number;
716 : : }
717 : :
718 : 0 : static void hns3_fd_convert_meta_data(struct hns3_fd_key_cfg *cfg,
719 : : uint8_t vf_id,
720 : : struct hns3_fdir_rule *rule,
721 : : uint8_t *key_x, uint8_t *key_y)
722 : : {
723 : : uint16_t meta_data = 0;
724 : : uint32_t port_number;
725 : : uint8_t cur_pos = 0;
726 : : uint8_t tuple_size;
727 : : uint8_t shift_bits;
728 : : uint32_t tmp_x;
729 : : uint32_t tmp_y;
730 : : uint8_t i;
731 : :
732 [ # # ]: 0 : for (i = 0; i < MAX_META_DATA; i++) {
733 [ # # ]: 0 : if ((cfg->meta_data_active & BIT(i)) == 0)
734 : 0 : continue;
735 : :
736 : 0 : tuple_size = meta_data_key_info[i].key_length;
737 [ # # ]: 0 : if (i == TUNNEL_PACKET) {
738 : 0 : hns3_set_bit(meta_data, cur_pos,
739 : : rule->key_conf.spec.tunnel_type ? 1 : 0);
740 : 0 : cur_pos += tuple_size;
741 [ # # ]: 0 : } else if (i == VLAN_NUMBER) {
742 : : uint32_t vlan_tag;
743 : : uint8_t vlan_num;
744 : :
745 [ # # ]: 0 : if (rule->key_conf.spec.tunnel_type == 0)
746 : 0 : vlan_num = rule->key_conf.vlan_num;
747 : : else
748 : 0 : vlan_num = rule->key_conf.outer_vlan_num;
749 [ # # ]: 0 : if (vlan_num == 1)
750 : : vlan_tag = HNS3_VLAN_TAG_TYPE_TAG1;
751 [ # # ]: 0 : else if (vlan_num == VLAN_TAG_NUM_MAX)
752 : : vlan_tag = HNS3_VLAN_TAG_TYPE_TAG1_2;
753 : : else
754 : : vlan_tag = HNS3_VLAN_TAG_TYPE_NONE;
755 : 0 : hns3_set_field(meta_data,
756 : : GENMASK(cur_pos + tuple_size,
757 : : cur_pos), cur_pos, vlan_tag);
758 : 0 : cur_pos += tuple_size;
759 [ # # ]: 0 : } else if (i == DST_VPORT) {
760 : 0 : port_number = hns3_get_port_number(0, vf_id);
761 : 0 : hns3_set_field(meta_data,
762 : : GENMASK(cur_pos + tuple_size, cur_pos),
763 : : cur_pos, port_number);
764 : 0 : cur_pos += tuple_size;
765 : : }
766 : : }
767 : :
768 : 0 : calc_x(tmp_x, meta_data, 0xFFFF);
769 : 0 : calc_y(tmp_y, meta_data, 0xFFFF);
770 : 0 : shift_bits = sizeof(meta_data) * HNS3_BITS_PER_BYTE - cur_pos;
771 : :
772 : 0 : tmp_x = rte_cpu_to_le_32(tmp_x << shift_bits);
773 : 0 : tmp_y = rte_cpu_to_le_32(tmp_y << shift_bits);
774 : 0 : key_x[0] = tmp_x & 0xFF;
775 : 0 : key_x[1] = (tmp_x >> HNS3_BITS_PER_BYTE) & 0xFF;
776 : 0 : key_y[0] = tmp_y & 0xFF;
777 : 0 : key_y[1] = (tmp_y >> HNS3_BITS_PER_BYTE) & 0xFF;
778 : 0 : }
779 : :
780 : : /* A complete key is combined with meta data key and tuple key.
781 : : * Meta data key is stored at the MSB region, and tuple key is stored at
782 : : * the LSB region, unused bits will be filled 0.
783 : : */
784 : 0 : static int hns3_config_key(struct hns3_adapter *hns,
785 : : struct hns3_fdir_rule *rule)
786 : : {
787 : : struct hns3_pf *pf = &hns->pf;
788 : 0 : struct hns3_hw *hw = &hns->hw;
789 : : struct hns3_fd_key_cfg *key_cfg;
790 : : uint8_t *cur_key_x;
791 : : uint8_t *cur_key_y;
792 : : alignas(4) uint8_t key_x[MAX_KEY_BYTES];
793 : : alignas(4) uint8_t key_y[MAX_KEY_BYTES];
794 : 0 : uint8_t vf_id = rule->vf_id;
795 : : uint8_t meta_data_region;
796 : : uint8_t tuple_size;
797 : : uint8_t i;
798 : : int ret;
799 : :
800 : : memset(key_x, 0, sizeof(key_x));
801 : : memset(key_y, 0, sizeof(key_y));
802 : : cur_key_x = key_x;
803 : : cur_key_y = key_y;
804 : :
805 : 0 : key_cfg = &pf->fdir.fd_cfg.key_cfg[HNS3_FD_STAGE_1];
806 [ # # ]: 0 : for (i = 0; i < MAX_TUPLE; i++) {
807 : : bool tuple_valid;
808 : :
809 : 0 : tuple_size = tuple_key_info[i].key_length / HNS3_BITS_PER_BYTE;
810 [ # # ]: 0 : if (key_cfg->tuple_active & BIT(i)) {
811 : 0 : tuple_valid = hns3_fd_convert_tuple(hw, i, cur_key_x,
812 : : cur_key_y, rule);
813 [ # # ]: 0 : if (tuple_valid) {
814 : 0 : cur_key_x += tuple_size;
815 : 0 : cur_key_y += tuple_size;
816 : : }
817 : : }
818 : : }
819 : :
820 : 0 : meta_data_region = pf->fdir.fd_cfg.max_key_length / HNS3_BITS_PER_BYTE -
821 : : MAX_META_DATA_LENGTH / HNS3_BITS_PER_BYTE;
822 : :
823 : 0 : hns3_fd_convert_meta_data(key_cfg, vf_id, rule,
824 : : key_x + meta_data_region,
825 : : key_y + meta_data_region);
826 : :
827 : 0 : ret = hns3_fd_tcam_config(hw, false, rule->location, key_y, true);
828 [ # # ]: 0 : if (ret) {
829 : 0 : hns3_err(hw, "Config fd key_y fail, loc=%u, ret=%d",
830 : : rule->queue_id, ret);
831 : 0 : return ret;
832 : : }
833 : :
834 : 0 : ret = hns3_fd_tcam_config(hw, true, rule->location, key_x, true);
835 [ # # ]: 0 : if (ret)
836 : 0 : hns3_err(hw, "Config fd key_x fail, loc=%u, ret=%d",
837 : : rule->queue_id, ret);
838 : : return ret;
839 : : }
840 : :
841 : 0 : static int hns3_config_action(struct hns3_hw *hw, struct hns3_fdir_rule *rule)
842 : : {
843 : : struct hns3_fd_ad_data ad_data;
844 : :
845 : 0 : ad_data.ad_id = rule->location;
846 : :
847 [ # # ]: 0 : if (rule->action == HNS3_FD_ACTION_DROP_PACKET) {
848 : 0 : ad_data.drop_packet = true;
849 : 0 : ad_data.queue_id = 0;
850 : 0 : ad_data.nb_queues = 0;
851 : : } else {
852 : 0 : ad_data.drop_packet = false;
853 : 0 : ad_data.queue_id = rule->queue_id;
854 : 0 : ad_data.nb_queues = rule->nb_queues;
855 : : }
856 : :
857 [ # # ]: 0 : if (unlikely(rule->flags & HNS3_RULE_FLAG_COUNTER)) {
858 : 0 : ad_data.use_counter = true;
859 : 0 : ad_data.counter_id = rule->act_cnt.id;
860 : : } else {
861 : 0 : ad_data.use_counter = false;
862 : 0 : ad_data.counter_id = 0;
863 : : }
864 : :
865 [ # # ]: 0 : if (unlikely(rule->flags & HNS3_RULE_FLAG_FDID))
866 : 0 : ad_data.rule_id = rule->fd_id;
867 : : else
868 : 0 : ad_data.rule_id = rule->location;
869 : :
870 : 0 : ad_data.use_next_stage = false;
871 : 0 : ad_data.next_input_key = 0;
872 : :
873 : 0 : ad_data.write_rule_id_to_bd = true;
874 : :
875 : 0 : return hns3_fd_ad_config(hw, ad_data.ad_id, &ad_data);
876 : : }
877 : :
878 : 0 : static int hns3_fd_clear_all_rules(struct hns3_hw *hw, uint32_t rule_num)
879 : : {
880 : : uint32_t i;
881 : : int ret;
882 : :
883 [ # # ]: 0 : for (i = 0; i < rule_num; i++) {
884 : 0 : ret = hns3_fd_tcam_config(hw, true, i, NULL, false);
885 [ # # ]: 0 : if (ret)
886 : 0 : return ret;
887 : : }
888 : :
889 : : return 0;
890 : : }
891 : :
892 : 0 : int hns3_fdir_filter_init(struct hns3_adapter *hns)
893 : : {
894 : : struct hns3_pf *pf = &hns->pf;
895 : : struct hns3_fdir_info *fdir_info = &pf->fdir;
896 : 0 : uint32_t rule_num = fdir_info->fd_cfg.rule_num[HNS3_FD_STAGE_1];
897 : : char fdir_hash_name[RTE_HASH_NAMESIZE];
898 : 0 : struct rte_hash_parameters fdir_hash_params = {
899 : : .name = fdir_hash_name,
900 : : .entries = rule_num,
901 : : .key_len = sizeof(struct hns3_fdir_key_conf),
902 : : .hash_func = rte_hash_crc,
903 : : .hash_func_init_val = 0,
904 : : .extra_flag = RTE_HASH_EXTRA_FLAGS_EXT_TABLE,
905 : : };
906 : : int ret;
907 : :
908 : 0 : ret = hns3_fd_clear_all_rules(&hns->hw, rule_num);
909 [ # # ]: 0 : if (ret) {
910 : 0 : PMD_INIT_LOG(ERR, "Clear all fd rules fail! ret = %d", ret);
911 : 0 : return ret;
912 : : }
913 : :
914 : 0 : fdir_hash_params.socket_id = rte_socket_id();
915 : 0 : TAILQ_INIT(&fdir_info->fdir_list);
916 : 0 : snprintf(fdir_hash_name, RTE_HASH_NAMESIZE, "%s", hns->hw.data->name);
917 : 0 : fdir_info->hash_handle = rte_hash_create(&fdir_hash_params);
918 [ # # ]: 0 : if (fdir_info->hash_handle == NULL) {
919 : 0 : PMD_INIT_LOG(ERR, "Create FDIR hash handle fail!");
920 : 0 : return -EINVAL;
921 : : }
922 : 0 : fdir_info->hash_map = rte_zmalloc("hns3 FDIR hash",
923 : : rule_num *
924 : : sizeof(struct hns3_fdir_rule_ele *),
925 : : 0);
926 [ # # ]: 0 : if (fdir_info->hash_map == NULL) {
927 : 0 : PMD_INIT_LOG(ERR, "Allocate memory for FDIR hash map fail!");
928 : 0 : rte_hash_free(fdir_info->hash_handle);
929 : 0 : return -ENOMEM;
930 : : }
931 : :
932 : : return 0;
933 : : }
934 : :
935 : 0 : void hns3_fdir_filter_uninit(struct hns3_adapter *hns)
936 : : {
937 : : struct hns3_pf *pf = &hns->pf;
938 : : struct hns3_fdir_info *fdir_info = &pf->fdir;
939 : : struct hns3_fdir_rule_ele *fdir_filter;
940 : :
941 [ # # ]: 0 : if (fdir_info->hash_map) {
942 : 0 : rte_free(fdir_info->hash_map);
943 : 0 : fdir_info->hash_map = NULL;
944 : : }
945 [ # # ]: 0 : if (fdir_info->hash_handle) {
946 : 0 : rte_hash_free(fdir_info->hash_handle);
947 : 0 : fdir_info->hash_handle = NULL;
948 : : }
949 : :
950 : 0 : fdir_filter = TAILQ_FIRST(&fdir_info->fdir_list);
951 [ # # ]: 0 : while (fdir_filter) {
952 [ # # ]: 0 : TAILQ_REMOVE(&fdir_info->fdir_list, fdir_filter, entries);
953 : 0 : hns3_fd_tcam_config(&hns->hw, true,
954 : 0 : fdir_filter->fdir_conf.location, NULL,
955 : : false);
956 : 0 : rte_free(fdir_filter);
957 : 0 : fdir_filter = TAILQ_FIRST(&fdir_info->fdir_list);
958 : : }
959 : 0 : }
960 : :
961 : : /*
962 : : * Find a key in the hash table.
963 : : * @return
964 : : * - Zero and positive values are key location.
965 : : * - -EINVAL if the parameters are invalid.
966 : : * - -ENOENT if the key is not found.
967 : : */
968 : 0 : static int hns3_fdir_filter_lookup(struct hns3_fdir_info *fdir_info,
969 : : struct hns3_fdir_key_conf *key)
970 : : {
971 : : hash_sig_t sig;
972 : : int ret;
973 : :
974 : 0 : sig = rte_hash_crc(key, sizeof(*key), 0);
975 : 0 : ret = rte_hash_lookup_with_hash(fdir_info->hash_handle, key, sig);
976 : :
977 : 0 : return ret;
978 : : }
979 : :
980 : 0 : static int hns3_insert_fdir_filter(struct hns3_hw *hw,
981 : : struct hns3_fdir_info *fdir_info,
982 : : struct hns3_fdir_rule_ele *fdir_filter)
983 : : {
984 : : struct hns3_fdir_key_conf *key;
985 : : hash_sig_t sig;
986 : : int index;
987 : :
988 : 0 : key = &fdir_filter->fdir_conf.key_conf;
989 : 0 : sig = rte_hash_crc(key, sizeof(*key), 0);
990 : 0 : index = rte_hash_add_key_with_hash(fdir_info->hash_handle, key, sig);
991 [ # # ]: 0 : if (index < 0) {
992 : 0 : hns3_err(hw, "Hash table full? err:%d!", index);
993 : 0 : return index;
994 : : }
995 : :
996 [ # # ]: 0 : if (fdir_info->index_cfg == HNS3_FDIR_INDEX_CONFIG_PRIORITY)
997 : 0 : index = fdir_filter->fdir_conf.location;
998 : :
999 : 0 : fdir_info->hash_map[index] = fdir_filter;
1000 : 0 : TAILQ_INSERT_TAIL(&fdir_info->fdir_list, fdir_filter, entries);
1001 : :
1002 : 0 : return index;
1003 : : }
1004 : :
1005 : 0 : static int hns3_remove_fdir_filter(struct hns3_hw *hw,
1006 : : struct hns3_fdir_info *fdir_info,
1007 : : struct hns3_fdir_rule *rule)
1008 : : {
1009 : : struct hns3_fdir_rule_ele *fdir_filter;
1010 : : hash_sig_t sig;
1011 : : int index;
1012 : :
1013 : 0 : sig = rte_hash_crc(&rule->key_conf, sizeof(rule->key_conf), 0);
1014 : 0 : index = rte_hash_del_key_with_hash(fdir_info->hash_handle, &rule->key_conf, sig);
1015 [ # # ]: 0 : if (index < 0) {
1016 : 0 : hns3_err(hw, "Delete hash key fail ret=%d", index);
1017 : 0 : return index;
1018 : : }
1019 : :
1020 [ # # ]: 0 : if (fdir_info->index_cfg == HNS3_FDIR_INDEX_CONFIG_PRIORITY)
1021 : 0 : index = rule->location;
1022 : 0 : fdir_filter = fdir_info->hash_map[index];
1023 : 0 : fdir_info->hash_map[index] = NULL;
1024 [ # # ]: 0 : TAILQ_REMOVE(&fdir_info->fdir_list, fdir_filter, entries);
1025 : :
1026 : 0 : rte_free(fdir_filter);
1027 : :
1028 : 0 : return 0;
1029 : : }
1030 : :
1031 : 0 : int hns3_fdir_filter_program(struct hns3_adapter *hns,
1032 : : struct hns3_fdir_rule *rule, bool del)
1033 : : {
1034 : : struct hns3_pf *pf = &hns->pf;
1035 : 0 : struct hns3_fdir_info *fdir_info = &pf->fdir;
1036 : : struct hns3_fdir_rule_ele *node;
1037 : 0 : struct hns3_hw *hw = &hns->hw;
1038 : : int ret;
1039 : :
1040 [ # # ]: 0 : if (del) {
1041 : 0 : ret = hns3_fd_tcam_config(hw, true, rule->location, NULL,
1042 : : false);
1043 [ # # ]: 0 : if (ret)
1044 : 0 : hns3_err(hw, "Failed to delete fdir: %u src_ip:%x "
1045 : : "dst_ip:%x src_port:%u dst_port:%u ret = %d",
1046 : : rule->location,
1047 : : rule->key_conf.spec.src_ip[IP_ADDR_KEY_ID],
1048 : : rule->key_conf.spec.dst_ip[IP_ADDR_KEY_ID],
1049 : : rule->key_conf.spec.src_port,
1050 : : rule->key_conf.spec.dst_port, ret);
1051 : : else
1052 : 0 : ret = hns3_remove_fdir_filter(hw, fdir_info, rule);
1053 : :
1054 : 0 : return ret;
1055 : : }
1056 : :
1057 : 0 : ret = hns3_fdir_filter_lookup(fdir_info, &rule->key_conf);
1058 [ # # ]: 0 : if (ret >= 0) {
1059 : 0 : hns3_err(hw, "Conflict with existing fdir loc: %d", ret);
1060 : 0 : return -EINVAL;
1061 : : }
1062 : :
1063 : 0 : node = rte_zmalloc("hns3 fdir rule", sizeof(struct hns3_fdir_rule_ele),
1064 : : 0);
1065 [ # # ]: 0 : if (node == NULL) {
1066 : 0 : hns3_err(hw, "Failed to allocate fdir_rule memory");
1067 : 0 : return -ENOMEM;
1068 : : }
1069 : :
1070 [ # # ]: 0 : rte_memcpy(&node->fdir_conf, rule, sizeof(struct hns3_fdir_rule));
1071 : 0 : ret = hns3_insert_fdir_filter(hw, fdir_info, node);
1072 [ # # ]: 0 : if (ret < 0) {
1073 : 0 : rte_free(node);
1074 : 0 : return ret;
1075 : : }
1076 : 0 : rule->location = ret;
1077 : 0 : node->fdir_conf.location = ret;
1078 : :
1079 : 0 : ret = hns3_config_action(hw, rule);
1080 [ # # ]: 0 : if (!ret)
1081 : 0 : ret = hns3_config_key(hns, rule);
1082 [ # # ]: 0 : if (ret) {
1083 : 0 : hns3_err(hw, "Failed to config fdir: %u src_ip:%x dst_ip:%x "
1084 : : "src_port:%u dst_port:%u ret = %d",
1085 : : rule->location,
1086 : : rule->key_conf.spec.src_ip[IP_ADDR_KEY_ID],
1087 : : rule->key_conf.spec.dst_ip[IP_ADDR_KEY_ID],
1088 : : rule->key_conf.spec.src_port,
1089 : : rule->key_conf.spec.dst_port, ret);
1090 : 0 : (void)hns3_remove_fdir_filter(hw, fdir_info, rule);
1091 : : }
1092 : :
1093 : : return ret;
1094 : : }
1095 : :
1096 : : /* remove all the flow director filters */
1097 : 0 : int hns3_clear_all_fdir_filter(struct hns3_adapter *hns)
1098 : : {
1099 : : struct hns3_pf *pf = &hns->pf;
1100 : : struct hns3_fdir_info *fdir_info = &pf->fdir;
1101 : : struct hns3_fdir_rule_ele *fdir_filter;
1102 : 0 : struct hns3_hw *hw = &hns->hw;
1103 : : int succ_cnt = 0;
1104 : : int fail_cnt = 0;
1105 : : int ret = 0;
1106 : :
1107 : : /* flush flow director */
1108 : 0 : rte_hash_reset(fdir_info->hash_handle);
1109 : :
1110 : 0 : memset(fdir_info->hash_map, 0,
1111 : : sizeof(struct hns3_fdir_rule_ele *) *
1112 : 0 : fdir_info->fd_cfg.rule_num[HNS3_FD_STAGE_1]);
1113 : :
1114 : 0 : fdir_filter = TAILQ_FIRST(&fdir_info->fdir_list);
1115 [ # # ]: 0 : while (fdir_filter) {
1116 [ # # ]: 0 : TAILQ_REMOVE(&fdir_info->fdir_list, fdir_filter, entries);
1117 : 0 : ret = hns3_fd_tcam_config(hw, true,
1118 : 0 : fdir_filter->fdir_conf.location,
1119 : : NULL, false);
1120 [ # # ]: 0 : if (ret == 0)
1121 : 0 : succ_cnt++;
1122 : : else
1123 : 0 : fail_cnt++;
1124 : 0 : rte_free(fdir_filter);
1125 : 0 : fdir_filter = TAILQ_FIRST(&fdir_info->fdir_list);
1126 : : }
1127 : :
1128 [ # # ]: 0 : if (fail_cnt > 0) {
1129 : 0 : hns3_err(hw, "fail to delete all FDIR filter, success num = %d "
1130 : : "fail num = %d", succ_cnt, fail_cnt);
1131 : : ret = -EIO;
1132 : : }
1133 : :
1134 : 0 : return ret;
1135 : : }
1136 : :
1137 : 0 : int hns3_restore_all_fdir_filter(struct hns3_adapter *hns)
1138 : : {
1139 : : struct hns3_pf *pf = &hns->pf;
1140 : : struct hns3_fdir_info *fdir_info = &pf->fdir;
1141 : : struct hns3_fdir_rule_ele *fdir_filter;
1142 : 0 : struct hns3_hw *hw = &hns->hw;
1143 : : bool err = false;
1144 : : int ret;
1145 : :
1146 [ # # ]: 0 : if (hns->is_vf)
1147 : : return 0;
1148 : :
1149 [ # # ]: 0 : TAILQ_FOREACH(fdir_filter, &fdir_info->fdir_list, entries) {
1150 : 0 : ret = hns3_config_action(hw, &fdir_filter->fdir_conf);
1151 [ # # ]: 0 : if (!ret)
1152 : 0 : ret = hns3_config_key(hns, &fdir_filter->fdir_conf);
1153 [ # # ]: 0 : if (ret) {
1154 : : err = true;
1155 [ # # ]: 0 : if (ret == -EBUSY)
1156 : : break;
1157 : : }
1158 : : }
1159 : :
1160 [ # # ]: 0 : if (err) {
1161 : 0 : hns3_err(hw, "Fail to restore FDIR filter, ret = %d", ret);
1162 : 0 : return -EIO;
1163 : : }
1164 : : return 0;
1165 : : }
1166 : :
1167 : 0 : int hns3_fd_get_count(struct hns3_hw *hw, uint32_t id, uint64_t *value)
1168 : : {
1169 : : struct hns3_fd_get_cnt_cmd *req;
1170 : : struct hns3_cmd_desc desc;
1171 : : int ret;
1172 : :
1173 : 0 : hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_FD_COUNTER_OP, true);
1174 : :
1175 : : req = (struct hns3_fd_get_cnt_cmd *)desc.data;
1176 : 0 : req->stage = HNS3_FD_STAGE_1;
1177 : 0 : req->index = rte_cpu_to_le_32(id);
1178 : :
1179 : 0 : ret = hns3_cmd_send(hw, &desc, 1);
1180 [ # # ]: 0 : if (ret) {
1181 : 0 : hns3_err(hw, "Read counter fail, ret=%d", ret);
1182 : 0 : return ret;
1183 : : }
1184 : :
1185 : 0 : *value = req->value;
1186 : :
1187 : 0 : return ret;
1188 : : }
1189 : :
1190 : : static struct {
1191 : : enum hns3_fdir_tuple_config tuple_cfg;
1192 : : const char *name;
1193 : : } tuple_config_map[] = {
1194 : : { HNS3_FDIR_TUPLE_CONFIG_DEFAULT, "default" },
1195 : : { HNS3_FDIR_TUPLE_OUTVLAN_REPLACE_INSMAC, "+outvlan-insmac" },
1196 : : { HNS3_FDIR_TUPLE_OUTVLAN_REPLACE_INDMAC, "+outvlan-indmac" },
1197 : : { HNS3_FDIR_TUPLE_OUTVLAN_REPLACE_INSIP, "+outvlan-insip" },
1198 : : { HNS3_FDIR_TUPLE_OUTVLAN_REPLACE_INDIP, "+outvlan-indip" },
1199 : : { HNS3_FDIR_TUPLE_OUTVLAN_REPLACE_SCTPTAG, "+outvlan-sctptag" },
1200 : : { HNS3_FDIR_TUPLE_OUTVLAN_REPLACE_TUNVNI, "+outvlan-tunvni" }
1201 : : };
1202 : :
1203 : : enum hns3_fdir_tuple_config
1204 : 0 : hns3_parse_tuple_config(const char *name)
1205 : : {
1206 : : uint32_t i;
1207 : :
1208 [ # # ]: 0 : for (i = 0; i < RTE_DIM(tuple_config_map); i++) {
1209 [ # # ]: 0 : if (!strcmp(name, tuple_config_map[i].name))
1210 : 0 : return tuple_config_map[i].tuple_cfg;
1211 : : }
1212 : :
1213 : : return HNS3_FDIR_TUPLE_CONFIG_BUTT;
1214 : : }
1215 : :
1216 : : const char *
1217 : 0 : hns3_tuple_config_name(enum hns3_fdir_tuple_config tuple_cfg)
1218 : : {
1219 : : uint32_t i;
1220 : :
1221 [ # # ]: 0 : for (i = 0; i < RTE_DIM(tuple_config_map); i++) {
1222 [ # # ]: 0 : if (tuple_cfg == tuple_config_map[i].tuple_cfg)
1223 : 0 : return tuple_config_map[i].name;
1224 : : }
1225 : :
1226 : : return "unknown";
1227 : : }
1228 : :
1229 : : static struct {
1230 : : enum hns3_fdir_index_config cfg;
1231 : : const char *name;
1232 : : } index_cfg_map[] = {
1233 : : { HNS3_FDIR_INDEX_CONFIG_HASH, "hash"},
1234 : : { HNS3_FDIR_INDEX_CONFIG_PRIORITY, "priority"},
1235 : : };
1236 : :
1237 : : const char *
1238 : 0 : hns3_fdir_index_config_name(enum hns3_fdir_index_config cfg)
1239 : : {
1240 : : uint32_t i;
1241 : :
1242 [ # # ]: 0 : for (i = 0; i < RTE_DIM(index_cfg_map); i++) {
1243 [ # # ]: 0 : if (cfg == index_cfg_map[i].cfg)
1244 : 0 : return index_cfg_map[i].name;
1245 : : }
1246 : :
1247 : : return "unknown";
1248 : : }
|