1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2018-2019 Hisilicon Limited.
6 #include <rte_ethdev_driver.h>
8 #include <rte_hash_crc.h>
10 #include <rte_malloc.h>
12 #include "hns3_ethdev.h"
13 #include "hns3_logs.h"
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
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)
28 #define HNS3_FD_EPORT_SW_EN_B 0
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(12, 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(20, 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(26, 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(13, 1)
51 HNS3_FD_MODE_DEPTH_2K_WIDTH_400B_STAGE_1,
52 HNS3_FD_MODE_DEPTH_1K_WIDTH_400B_STAGE_2,
53 HNS3_FD_MODE_DEPTH_4K_WIDTH_200B_STAGE_1,
54 HNS3_FD_MODE_DEPTH_2K_WIDTH_200B_STAGE_2,
57 enum HNS3_FD_KEY_TYPE {
58 HNS3_FD_KEY_BASE_ON_PTYPE,
59 HNS3_FD_KEY_BASE_ON_TUPLE,
62 enum HNS3_FD_META_DATA {
79 static const struct key_info meta_data_key_info[] = {
90 static const struct key_info tuple_key_info[] = {
93 {OUTER_VLAN_TAG_FST, 16},
94 {OUTER_VLAN_TAG_SEC, 16},
102 {OUTER_SRC_PORT, 16},
103 {OUTER_DST_PORT, 16},
106 {OUTER_TUN_FLOW_ID, 8},
109 {INNER_VLAN_TAG1, 16},
110 {INNER_VLAN_TAG2, 16},
111 {INNER_ETH_TYPE, 16},
118 {INNER_SRC_PORT, 16},
119 {INNER_DST_PORT, 16},
120 {INNER_SCTP_TAG, 32},
123 #define HNS3_BITS_PER_BYTE 8
124 #define MAX_KEY_LENGTH 400
125 #define MAX_200B_KEY_LENGTH 200
126 #define MAX_META_DATA_LENGTH 16
127 #define MAX_KEY_DWORDS DIV_ROUND_UP(MAX_KEY_LENGTH / HNS3_BITS_PER_BYTE, 4)
128 #define MAX_KEY_BYTES (MAX_KEY_DWORDS * 4)
130 enum HNS3_FD_PACKET_TYPE {
135 /* For each bit of TCAM entry, it uses a pair of 'x' and
136 * 'y' to indicate which value to match, like below:
137 * ----------------------------------
138 * | bit x | bit y | search value |
139 * ----------------------------------
140 * | 0 | 0 | always hit |
141 * ----------------------------------
142 * | 1 | 0 | match '0' |
143 * ----------------------------------
144 * | 0 | 1 | match '1' |
145 * ----------------------------------
146 * | 1 | 1 | invalid |
147 * ----------------------------------
148 * Then for input key(k) and mask(v), we can calculate the value by
153 #define calc_x(x, k, v) ((x) = (~(k) & (v)))
154 #define calc_y(y, k, v) ((y) = ((k) & (v)))
156 struct hns3_fd_tcam_config_1_cmd {
164 uint8_t tcam_data[8];
167 struct hns3_fd_tcam_config_2_cmd {
168 uint8_t tcam_data[24];
171 struct hns3_fd_tcam_config_3_cmd {
172 uint8_t tcam_data[20];
176 struct hns3_get_fd_mode_cmd {
182 struct hns3_get_fd_allocation_cmd {
183 rte_le32_t stage1_entry_num;
184 rte_le32_t stage2_entry_num;
185 rte_le16_t stage1_counter_num;
186 rte_le16_t stage2_counter_num;
190 struct hns3_set_fd_key_config_cmd {
193 uint8_t inner_sipv6_word_en;
194 uint8_t inner_dipv6_word_en;
195 uint8_t outer_sipv6_word_en;
196 uint8_t outer_dipv6_word_en;
198 rte_le32_t tuple_mask;
199 rte_le32_t meta_data_mask;
203 struct hns3_fd_ad_config_cmd {
211 struct hns3_fd_get_cnt_cmd {
220 static int hns3_get_fd_mode(struct hns3_hw *hw, uint8_t *fd_mode)
222 struct hns3_get_fd_mode_cmd *req;
223 struct hns3_cmd_desc desc;
226 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_FD_MODE_CTRL, true);
228 req = (struct hns3_get_fd_mode_cmd *)desc.data;
230 ret = hns3_cmd_send(hw, &desc, 1);
232 hns3_err(hw, "Get fd mode fail, ret=%d", ret);
236 *fd_mode = req->mode;
241 static int hns3_get_fd_allocation(struct hns3_hw *hw,
242 uint32_t *stage1_entry_num,
243 uint32_t *stage2_entry_num,
244 uint16_t *stage1_counter_num,
245 uint16_t *stage2_counter_num)
247 struct hns3_get_fd_allocation_cmd *req;
248 struct hns3_cmd_desc desc;
251 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_FD_GET_ALLOCATION, true);
253 req = (struct hns3_get_fd_allocation_cmd *)desc.data;
255 ret = hns3_cmd_send(hw, &desc, 1);
257 hns3_err(hw, "Query fd allocation fail, ret=%d", ret);
261 *stage1_entry_num = rte_le_to_cpu_32(req->stage1_entry_num);
262 *stage2_entry_num = rte_le_to_cpu_32(req->stage2_entry_num);
263 *stage1_counter_num = rte_le_to_cpu_16(req->stage1_counter_num);
264 *stage2_counter_num = rte_le_to_cpu_16(req->stage2_counter_num);
269 static int hns3_set_fd_key_config(struct hns3_adapter *hns)
271 struct hns3_set_fd_key_config_cmd *req;
272 struct hns3_fd_key_cfg *key_cfg;
273 struct hns3_pf *pf = &hns->pf;
274 struct hns3_hw *hw = &hns->hw;
275 struct hns3_cmd_desc desc;
278 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_FD_KEY_CONFIG, false);
280 req = (struct hns3_set_fd_key_config_cmd *)desc.data;
281 key_cfg = &pf->fdir.fd_cfg.key_cfg[HNS3_FD_STAGE_1];
282 req->stage = HNS3_FD_STAGE_1;
283 req->key_select = key_cfg->key_sel;
284 req->inner_sipv6_word_en = key_cfg->inner_sipv6_word_en;
285 req->inner_dipv6_word_en = key_cfg->inner_dipv6_word_en;
286 req->outer_sipv6_word_en = key_cfg->outer_sipv6_word_en;
287 req->outer_dipv6_word_en = key_cfg->outer_dipv6_word_en;
288 req->tuple_mask = rte_cpu_to_le_32(~key_cfg->tuple_active);
289 req->meta_data_mask = rte_cpu_to_le_32(~key_cfg->meta_data_active);
291 ret = hns3_cmd_send(hw, &desc, 1);
293 hns3_err(hw, "Set fd key fail, ret=%d", ret);
298 int hns3_init_fd_config(struct hns3_adapter *hns)
300 struct hns3_pf *pf = &hns->pf;
301 struct hns3_hw *hw = &hns->hw;
302 struct hns3_fd_key_cfg *key_cfg;
305 ret = hns3_get_fd_mode(hw, &pf->fdir.fd_cfg.fd_mode);
309 switch (pf->fdir.fd_cfg.fd_mode) {
310 case HNS3_FD_MODE_DEPTH_2K_WIDTH_400B_STAGE_1:
311 pf->fdir.fd_cfg.max_key_length = MAX_KEY_LENGTH;
313 case HNS3_FD_MODE_DEPTH_4K_WIDTH_200B_STAGE_1:
314 pf->fdir.fd_cfg.max_key_length = MAX_200B_KEY_LENGTH;
315 hns3_warn(hw, "Unsupported tunnel filter in 4K*200Bit");
318 hns3_err(hw, "Unsupported flow director mode %d",
319 pf->fdir.fd_cfg.fd_mode);
323 key_cfg = &pf->fdir.fd_cfg.key_cfg[HNS3_FD_STAGE_1];
324 key_cfg->key_sel = HNS3_FD_KEY_BASE_ON_TUPLE;
325 key_cfg->inner_sipv6_word_en = IPV6_ADDR_WORD_MASK;
326 key_cfg->inner_dipv6_word_en = IPV6_ADDR_WORD_MASK;
327 key_cfg->outer_sipv6_word_en = 0;
328 key_cfg->outer_dipv6_word_en = 0;
330 key_cfg->tuple_active = BIT(INNER_VLAN_TAG1) | BIT(INNER_ETH_TYPE) |
331 BIT(INNER_IP_PROTO) | BIT(INNER_IP_TOS) |
332 BIT(INNER_SRC_IP) | BIT(INNER_DST_IP) |
333 BIT(INNER_SRC_PORT) | BIT(INNER_DST_PORT);
335 /* If use max 400bit key, we can support tuples for ether type */
336 if (pf->fdir.fd_cfg.max_key_length == MAX_KEY_LENGTH) {
337 key_cfg->tuple_active |=
338 BIT(INNER_DST_MAC) | BIT(INNER_SRC_MAC) |
339 BIT(OUTER_SRC_PORT) | BIT(INNER_SCTP_TAG) |
340 BIT(OUTER_DST_PORT) | BIT(INNER_VLAN_TAG2) |
341 BIT(OUTER_TUN_VNI) | BIT(OUTER_TUN_FLOW_ID) |
342 BIT(OUTER_ETH_TYPE) | BIT(OUTER_IP_PROTO);
345 /* roce_type is used to filter roce frames
346 * dst_vport is used to specify the rule
348 key_cfg->meta_data_active = BIT(DST_VPORT) | BIT(TUNNEL_PACKET) |
351 ret = hns3_get_fd_allocation(hw,
352 &pf->fdir.fd_cfg.rule_num[HNS3_FD_STAGE_1],
353 &pf->fdir.fd_cfg.rule_num[HNS3_FD_STAGE_2],
354 &pf->fdir.fd_cfg.cnt_num[HNS3_FD_STAGE_1],
355 &pf->fdir.fd_cfg.cnt_num[HNS3_FD_STAGE_2]);
359 return hns3_set_fd_key_config(hns);
362 static int hns3_fd_tcam_config(struct hns3_hw *hw, bool sel_x, int loc,
363 uint8_t *key, bool is_add)
365 #define FD_TCAM_CMD_NUM 3
366 struct hns3_fd_tcam_config_1_cmd *req1;
367 struct hns3_fd_tcam_config_2_cmd *req2;
368 struct hns3_fd_tcam_config_3_cmd *req3;
369 struct hns3_cmd_desc desc[FD_TCAM_CMD_NUM];
373 hns3_cmd_setup_basic_desc(&desc[0], HNS3_OPC_FD_TCAM_OP, false);
374 desc[0].flag |= rte_cpu_to_le_16(HNS3_CMD_FLAG_NEXT);
375 hns3_cmd_setup_basic_desc(&desc[1], HNS3_OPC_FD_TCAM_OP, false);
376 desc[1].flag |= rte_cpu_to_le_16(HNS3_CMD_FLAG_NEXT);
377 hns3_cmd_setup_basic_desc(&desc[2], HNS3_OPC_FD_TCAM_OP, false);
379 req1 = (struct hns3_fd_tcam_config_1_cmd *)desc[0].data;
380 req2 = (struct hns3_fd_tcam_config_2_cmd *)desc[1].data;
381 req3 = (struct hns3_fd_tcam_config_3_cmd *)desc[2].data;
383 req1->stage = HNS3_FD_STAGE_1;
384 req1->xy_sel = sel_x ? 1 : 0;
385 hns3_set_bit(req1->port_info, HNS3_FD_EPORT_SW_EN_B, 0);
386 req1->index = rte_cpu_to_le_32(loc);
387 req1->entry_vld = sel_x ? is_add : 0;
390 len = sizeof(req1->tcam_data);
391 memcpy(req1->tcam_data, key, len);
394 len = sizeof(req2->tcam_data);
395 memcpy(req2->tcam_data, key, len);
398 len = sizeof(req3->tcam_data);
399 memcpy(req3->tcam_data, key, len);
402 ret = hns3_cmd_send(hw, desc, FD_TCAM_CMD_NUM);
404 hns3_err(hw, "Config tcam key fail, ret=%d loc=%d add=%d",
409 static int hns3_fd_ad_config(struct hns3_hw *hw, int loc,
410 struct hns3_fd_ad_data *action)
412 struct hns3_fd_ad_config_cmd *req;
413 struct hns3_cmd_desc desc;
414 uint64_t ad_data = 0;
417 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_FD_AD_OP, false);
419 req = (struct hns3_fd_ad_config_cmd *)desc.data;
420 req->index = rte_cpu_to_le_32(loc);
421 req->stage = HNS3_FD_STAGE_1;
423 hns3_set_bit(ad_data, HNS3_FD_AD_WR_RULE_ID_B,
424 action->write_rule_id_to_bd);
425 hns3_set_field(ad_data, HNS3_FD_AD_RULE_ID_M, HNS3_FD_AD_RULE_ID_S,
427 ad_data <<= HNS3_FD_AD_DATA_S;
428 hns3_set_bit(ad_data, HNS3_FD_AD_DROP_B, action->drop_packet);
429 hns3_set_bit(ad_data, HNS3_FD_AD_DIRECT_QID_B,
430 action->forward_to_direct_queue);
431 hns3_set_field(ad_data, HNS3_FD_AD_QID_M, HNS3_FD_AD_QID_S,
433 hns3_set_bit(ad_data, HNS3_FD_AD_USE_COUNTER_B, action->use_counter);
434 hns3_set_field(ad_data, HNS3_FD_AD_COUNTER_NUM_M,
435 HNS3_FD_AD_COUNTER_NUM_S, action->counter_id);
436 hns3_set_bit(ad_data, HNS3_FD_AD_NXT_STEP_B, action->use_next_stage);
437 hns3_set_field(ad_data, HNS3_FD_AD_NXT_KEY_M, HNS3_FD_AD_NXT_KEY_S,
440 req->ad_data = rte_cpu_to_le_64(ad_data);
441 ret = hns3_cmd_send(hw, &desc, 1);
443 hns3_err(hw, "Config fd ad fail, ret=%d loc=%d", ret, loc);
448 static inline void hns3_fd_convert_mac(uint8_t *key, uint8_t *mask,
449 uint8_t *mac_x, uint8_t *mac_y)
454 for (i = 0; i < RTE_ETHER_ADDR_LEN; i++) {
455 tmp = RTE_ETHER_ADDR_LEN - 1 - i;
456 calc_x(mac_x[tmp], key[i], mask[i]);
457 calc_y(mac_y[tmp], key[i], mask[i]);
461 static void hns3_fd_convert_int16(uint32_t tuple, struct hns3_fdir_rule *rule,
462 uint8_t *val_x, uint8_t *val_y)
471 key = rule->key_conf.spec.outer_src_port;
472 mask = rule->key_conf.mask.outer_src_port;
475 key = rule->key_conf.spec.tunnel_type;
476 mask = rule->key_conf.mask.tunnel_type;
479 key = rule->key_conf.spec.outer_ether_type;
480 mask = rule->key_conf.mask.outer_ether_type;
483 key = rule->key_conf.spec.src_port;
484 mask = rule->key_conf.mask.src_port;
487 key = rule->key_conf.spec.dst_port;
488 mask = rule->key_conf.mask.dst_port;
490 case INNER_VLAN_TAG1:
491 key = rule->key_conf.spec.vlan_tag1;
492 mask = rule->key_conf.mask.vlan_tag1;
494 case INNER_VLAN_TAG2:
495 key = rule->key_conf.spec.vlan_tag2;
496 mask = rule->key_conf.mask.vlan_tag2;
499 /* INNER_ETH_TYPE: */
500 key = rule->key_conf.spec.ether_type;
501 mask = rule->key_conf.mask.ether_type;
504 calc_x(tmp_x_s, key, mask);
505 calc_y(tmp_y_s, key, mask);
506 val_x[0] = rte_cpu_to_le_16(tmp_x_s) & 0xFF;
507 val_x[1] = rte_cpu_to_le_16(tmp_x_s) >> HNS3_BITS_PER_BYTE;
508 val_y[0] = rte_cpu_to_le_16(tmp_y_s) & 0xFF;
509 val_y[1] = rte_cpu_to_le_16(tmp_y_s) >> HNS3_BITS_PER_BYTE;
512 static inline void hns3_fd_convert_int32(uint32_t key, uint32_t mask,
513 uint8_t *val_x, uint8_t *val_y)
518 calc_x(tmp_x_l, key, mask);
519 calc_y(tmp_y_l, key, mask);
520 memcpy(val_x, &tmp_x_l, sizeof(tmp_x_l));
521 memcpy(val_y, &tmp_y_l, sizeof(tmp_y_l));
524 static bool hns3_fd_convert_tuple(uint32_t tuple, uint8_t *key_x,
525 uint8_t *key_y, struct hns3_fdir_rule *rule)
527 struct hns3_fdir_key_conf *key_conf;
531 if ((rule->input_set & BIT(tuple)) == 0)
534 key_conf = &rule->key_conf;
537 hns3_fd_convert_mac(key_conf->spec.dst_mac,
538 key_conf->mask.dst_mac, key_x, key_y);
541 hns3_fd_convert_mac(key_conf->spec.src_mac,
542 key_conf->mask.src_mac, key_x, key_y);
549 case INNER_VLAN_TAG1:
550 case INNER_VLAN_TAG2:
552 hns3_fd_convert_int16(tuple, rule, key_x, key_y);
555 hns3_fd_convert_int32(key_conf->spec.src_ip[IP_ADDR_KEY_ID],
556 key_conf->mask.src_ip[IP_ADDR_KEY_ID],
560 hns3_fd_convert_int32(key_conf->spec.dst_ip[IP_ADDR_KEY_ID],
561 key_conf->mask.dst_ip[IP_ADDR_KEY_ID],
565 hns3_fd_convert_int32(key_conf->spec.sctp_tag,
566 key_conf->mask.sctp_tag, key_x, key_y);
569 for (i = 0; i < VNI_OR_TNI_LEN; i++) {
570 tmp = VNI_OR_TNI_LEN - 1 - i;
572 key_conf->spec.outer_tun_vni[i],
573 key_conf->mask.outer_tun_vni[i]);
575 key_conf->spec.outer_tun_vni[i],
576 key_conf->mask.outer_tun_vni[i]);
579 case OUTER_TUN_FLOW_ID:
580 calc_x(*key_x, key_conf->spec.outer_tun_flow_id,
581 key_conf->mask.outer_tun_flow_id);
582 calc_y(*key_y, key_conf->spec.outer_tun_flow_id,
583 key_conf->mask.outer_tun_flow_id);
586 calc_x(*key_x, key_conf->spec.ip_tos, key_conf->mask.ip_tos);
587 calc_y(*key_y, key_conf->spec.ip_tos, key_conf->mask.ip_tos);
590 calc_x(*key_x, key_conf->spec.outer_proto,
591 key_conf->mask.outer_proto);
592 calc_y(*key_y, key_conf->spec.outer_proto,
593 key_conf->mask.outer_proto);
596 calc_x(*key_x, key_conf->spec.ip_proto,
597 key_conf->mask.ip_proto);
598 calc_y(*key_y, key_conf->spec.ip_proto,
599 key_conf->mask.ip_proto);
605 static uint32_t hns3_get_port_number(uint8_t pf_id, uint8_t vf_id)
607 uint32_t port_number = 0;
609 hns3_set_field(port_number, HNS3_PF_ID_M, HNS3_PF_ID_S, pf_id);
610 hns3_set_field(port_number, HNS3_VF_ID_M, HNS3_VF_ID_S, vf_id);
611 hns3_set_bit(port_number, HNS3_PORT_TYPE_B, HOST_PORT);
616 static void hns3_fd_convert_meta_data(struct hns3_fd_key_cfg *cfg,
618 struct hns3_fdir_rule *rule,
619 uint8_t *key_x, uint8_t *key_y)
621 uint16_t meta_data = 0;
622 uint16_t port_number;
630 for (i = 0; i < MAX_META_DATA; i++) {
631 if ((cfg->meta_data_active & BIT(i)) == 0)
634 tuple_size = meta_data_key_info[i].key_length;
635 if (i == TUNNEL_PACKET) {
636 hns3_set_bit(meta_data, cur_pos,
637 rule->key_conf.spec.tunnel_type ? 1 : 0);
638 cur_pos += tuple_size;
639 } else if (i == VLAN_NUMBER) {
642 if (rule->key_conf.spec.tunnel_type == 0)
643 vlan_num = rule->key_conf.vlan_num;
645 vlan_num = rule->key_conf.outer_vlan_num;
647 vlan_tag = HNS3_VLAN_TAG_TYPE_TAG1;
648 else if (vlan_num == VLAN_TAG_NUM_MAX)
649 vlan_tag = HNS3_VLAN_TAG_TYPE_TAG1_2;
651 vlan_tag = HNS3_VLAN_TAG_TYPE_NONE;
652 hns3_set_field(meta_data,
653 GENMASK(cur_pos + tuple_size,
654 cur_pos), cur_pos, vlan_tag);
655 cur_pos += tuple_size;
656 } else if (i == DST_VPORT) {
657 port_number = hns3_get_port_number(0, vf_id);
658 hns3_set_field(meta_data,
659 GENMASK(cur_pos + tuple_size, cur_pos),
660 cur_pos, port_number);
661 cur_pos += tuple_size;
665 calc_x(tmp_x, meta_data, 0xFFFF);
666 calc_y(tmp_y, meta_data, 0xFFFF);
667 shift_bits = sizeof(meta_data) * HNS3_BITS_PER_BYTE - cur_pos;
669 tmp_x = rte_cpu_to_le_32(tmp_x << shift_bits);
670 tmp_y = rte_cpu_to_le_32(tmp_y << shift_bits);
671 key_x[0] = tmp_x & 0xFF;
672 key_x[1] = (tmp_x >> HNS3_BITS_PER_BYTE) & 0xFF;
673 key_y[0] = tmp_y & 0xFF;
674 key_y[1] = (tmp_y >> HNS3_BITS_PER_BYTE) & 0xFF;
677 /* A complete key is combined with meta data key and tuple key.
678 * Meta data key is stored at the MSB region, and tuple key is stored at
679 * the LSB region, unused bits will be filled 0.
681 static int hns3_config_key(struct hns3_adapter *hns,
682 struct hns3_fdir_rule *rule)
684 struct hns3_pf *pf = &hns->pf;
685 struct hns3_hw *hw = &hns->hw;
686 struct hns3_fd_key_cfg *key_cfg;
689 uint8_t key_x[MAX_KEY_BYTES] __rte_aligned(4);
690 uint8_t key_y[MAX_KEY_BYTES] __rte_aligned(4);
691 uint8_t vf_id = rule->vf_id;
692 uint8_t meta_data_region;
697 memset(key_x, 0, sizeof(key_x));
698 memset(key_y, 0, sizeof(key_y));
702 key_cfg = &pf->fdir.fd_cfg.key_cfg[HNS3_FD_STAGE_1];
703 for (i = 0; i < MAX_TUPLE; i++) {
706 tuple_size = tuple_key_info[i].key_length / HNS3_BITS_PER_BYTE;
707 if (key_cfg->tuple_active & BIT(i)) {
708 tuple_valid = hns3_fd_convert_tuple(i, cur_key_x,
711 cur_key_x += tuple_size;
712 cur_key_y += tuple_size;
717 meta_data_region = pf->fdir.fd_cfg.max_key_length / HNS3_BITS_PER_BYTE -
718 MAX_META_DATA_LENGTH / HNS3_BITS_PER_BYTE;
720 hns3_fd_convert_meta_data(key_cfg, vf_id, rule,
721 key_x + meta_data_region,
722 key_y + meta_data_region);
724 ret = hns3_fd_tcam_config(hw, false, rule->location, key_y, true);
726 hns3_err(hw, "Config fd key_y fail, loc=%d, ret=%d",
727 rule->queue_id, ret);
731 ret = hns3_fd_tcam_config(hw, true, rule->location, key_x, true);
733 hns3_err(hw, "Config fd key_x fail, loc=%d, ret=%d",
734 rule->queue_id, ret);
738 static int hns3_config_action(struct hns3_hw *hw, struct hns3_fdir_rule *rule)
740 struct hns3_fd_ad_data ad_data;
742 ad_data.ad_id = rule->location;
744 if (rule->action == HNS3_FD_ACTION_DROP_PACKET) {
745 ad_data.drop_packet = true;
746 ad_data.forward_to_direct_queue = false;
747 ad_data.queue_id = 0;
749 ad_data.drop_packet = false;
750 ad_data.forward_to_direct_queue = true;
751 ad_data.queue_id = rule->queue_id;
754 if (unlikely(rule->flags & HNS3_RULE_FLAG_COUNTER)) {
755 ad_data.use_counter = true;
756 ad_data.counter_id = rule->act_cnt.id;
758 ad_data.use_counter = false;
759 ad_data.counter_id = 0;
762 if (unlikely(rule->flags & HNS3_RULE_FLAG_FDID))
763 ad_data.rule_id = rule->fd_id;
765 ad_data.rule_id = rule->location;
767 ad_data.use_next_stage = false;
768 ad_data.next_input_key = 0;
770 ad_data.write_rule_id_to_bd = true;
772 return hns3_fd_ad_config(hw, ad_data.ad_id, &ad_data);
775 int hns3_fdir_filter_init(struct hns3_adapter *hns)
777 struct hns3_pf *pf = &hns->pf;
778 struct hns3_fdir_info *fdir_info = &pf->fdir;
779 uint32_t rule_num = fdir_info->fd_cfg.rule_num[HNS3_FD_STAGE_1];
780 char fdir_hash_name[RTE_HASH_NAMESIZE];
781 struct rte_hash_parameters fdir_hash_params = {
782 .name = fdir_hash_name,
784 .key_len = sizeof(struct hns3_fdir_key_conf),
785 .hash_func = rte_hash_crc,
786 .hash_func_init_val = 0,
789 fdir_hash_params.socket_id = rte_socket_id();
790 TAILQ_INIT(&fdir_info->fdir_list);
791 rte_spinlock_init(&fdir_info->flows_lock);
792 snprintf(fdir_hash_name, RTE_HASH_NAMESIZE, "%s", hns->hw.data->name);
793 fdir_info->hash_handle = rte_hash_create(&fdir_hash_params);
794 if (fdir_info->hash_handle == NULL) {
795 PMD_INIT_LOG(ERR, "Create FDIR hash handle fail!");
798 fdir_info->hash_map = rte_zmalloc("hns3 FDIR hash",
800 sizeof(struct hns3_fdir_rule_ele *),
802 if (fdir_info->hash_map == NULL) {
803 PMD_INIT_LOG(ERR, "Allocate memory for FDIR hash map fail!");
804 rte_hash_free(fdir_info->hash_handle);
811 void hns3_fdir_filter_uninit(struct hns3_adapter *hns)
813 struct hns3_pf *pf = &hns->pf;
814 struct hns3_fdir_info *fdir_info = &pf->fdir;
815 struct hns3_fdir_rule_ele *fdir_filter;
817 rte_spinlock_lock(&fdir_info->flows_lock);
818 if (fdir_info->hash_map) {
819 rte_free(fdir_info->hash_map);
820 fdir_info->hash_map = NULL;
822 if (fdir_info->hash_handle) {
823 rte_hash_free(fdir_info->hash_handle);
824 fdir_info->hash_handle = NULL;
826 rte_spinlock_unlock(&fdir_info->flows_lock);
828 fdir_filter = TAILQ_FIRST(&fdir_info->fdir_list);
829 while (fdir_filter) {
830 TAILQ_REMOVE(&fdir_info->fdir_list, fdir_filter, entries);
831 hns3_fd_tcam_config(&hns->hw, true,
832 fdir_filter->fdir_conf.location, NULL,
834 rte_free(fdir_filter);
835 fdir_filter = TAILQ_FIRST(&fdir_info->fdir_list);
840 * Find a key in the hash table.
842 * - Zero and positive values are key location.
843 * - -EINVAL if the parameters are invalid.
844 * - -ENOENT if the key is not found.
846 static int hns3_fdir_filter_lookup(struct hns3_fdir_info *fdir_info,
847 struct hns3_fdir_key_conf *key)
852 rte_spinlock_lock(&fdir_info->flows_lock);
853 sig = rte_hash_crc(key, sizeof(*key), 0);
854 ret = rte_hash_lookup_with_hash(fdir_info->hash_handle, key, sig);
855 rte_spinlock_unlock(&fdir_info->flows_lock);
860 static int hns3_insert_fdir_filter(struct hns3_hw *hw,
861 struct hns3_fdir_info *fdir_info,
862 struct hns3_fdir_rule_ele *fdir_filter)
864 struct hns3_fdir_key_conf *key;
868 key = &fdir_filter->fdir_conf.key_conf;
869 rte_spinlock_lock(&fdir_info->flows_lock);
870 sig = rte_hash_crc(key, sizeof(*key), 0);
871 ret = rte_hash_add_key_with_hash(fdir_info->hash_handle, key, sig);
873 rte_spinlock_unlock(&fdir_info->flows_lock);
874 hns3_err(hw, "Hash table full? err:%d(%s)!", ret,
879 fdir_info->hash_map[ret] = fdir_filter;
880 TAILQ_INSERT_TAIL(&fdir_info->fdir_list, fdir_filter, entries);
881 rte_spinlock_unlock(&fdir_info->flows_lock);
886 static int hns3_remove_fdir_filter(struct hns3_hw *hw,
887 struct hns3_fdir_info *fdir_info,
888 struct hns3_fdir_key_conf *key)
890 struct hns3_fdir_rule_ele *fdir_filter;
894 rte_spinlock_lock(&fdir_info->flows_lock);
895 sig = rte_hash_crc(key, sizeof(*key), 0);
896 ret = rte_hash_del_key_with_hash(fdir_info->hash_handle, key, sig);
898 rte_spinlock_unlock(&fdir_info->flows_lock);
899 hns3_err(hw, "Delete hash key fail ret=%d", ret);
903 fdir_filter = fdir_info->hash_map[ret];
904 fdir_info->hash_map[ret] = NULL;
905 TAILQ_REMOVE(&fdir_info->fdir_list, fdir_filter, entries);
906 rte_spinlock_unlock(&fdir_info->flows_lock);
908 rte_free(fdir_filter);
913 int hns3_fdir_filter_program(struct hns3_adapter *hns,
914 struct hns3_fdir_rule *rule, bool del)
916 struct hns3_pf *pf = &hns->pf;
917 struct hns3_fdir_info *fdir_info = &pf->fdir;
918 struct hns3_fdir_rule_ele *node;
919 struct hns3_hw *hw = &hns->hw;
923 ret = hns3_fd_tcam_config(hw, true, rule->location, NULL,
926 hns3_err(hw, "Failed to delete fdir: %d src_ip:%x "
927 "dst_ip:%x src_port:%d dst_port:%d",
929 rule->key_conf.spec.src_ip[IP_ADDR_KEY_ID],
930 rule->key_conf.spec.dst_ip[IP_ADDR_KEY_ID],
931 rule->key_conf.spec.src_port,
932 rule->key_conf.spec.dst_port);
934 hns3_remove_fdir_filter(hw, fdir_info, &rule->key_conf);
939 ret = hns3_fdir_filter_lookup(fdir_info, &rule->key_conf);
941 hns3_err(hw, "Conflict with existing fdir loc: %d", ret);
945 node = rte_zmalloc("hns3 fdir rule", sizeof(struct hns3_fdir_rule_ele),
948 hns3_err(hw, "Failed to allocate fdir_rule memory");
952 rte_memcpy(&node->fdir_conf, rule, sizeof(struct hns3_fdir_rule));
953 ret = hns3_insert_fdir_filter(hw, fdir_info, node);
958 rule->location = ret;
959 node->fdir_conf.location = ret;
961 rte_spinlock_lock(&fdir_info->flows_lock);
962 ret = hns3_config_action(hw, rule);
964 ret = hns3_config_key(hns, rule);
965 rte_spinlock_unlock(&fdir_info->flows_lock);
967 hns3_err(hw, "Failed to config fdir: %d src_ip:%x dst_ip:%x "
968 "src_port:%d dst_port:%d",
970 rule->key_conf.spec.src_ip[IP_ADDR_KEY_ID],
971 rule->key_conf.spec.dst_ip[IP_ADDR_KEY_ID],
972 rule->key_conf.spec.src_port,
973 rule->key_conf.spec.dst_port);
974 (void)hns3_remove_fdir_filter(hw, fdir_info, &rule->key_conf);
980 /* remove all the flow director filters */
981 int hns3_clear_all_fdir_filter(struct hns3_adapter *hns)
983 struct hns3_pf *pf = &hns->pf;
984 struct hns3_fdir_info *fdir_info = &pf->fdir;
985 struct hns3_fdir_rule_ele *fdir_filter;
986 struct hns3_hw *hw = &hns->hw;
989 /* flush flow director */
990 rte_spinlock_lock(&fdir_info->flows_lock);
991 rte_hash_reset(fdir_info->hash_handle);
992 rte_spinlock_unlock(&fdir_info->flows_lock);
994 fdir_filter = TAILQ_FIRST(&fdir_info->fdir_list);
995 while (fdir_filter) {
996 TAILQ_REMOVE(&fdir_info->fdir_list, fdir_filter, entries);
997 ret += hns3_fd_tcam_config(hw, true,
998 fdir_filter->fdir_conf.location,
1000 rte_free(fdir_filter);
1001 fdir_filter = TAILQ_FIRST(&fdir_info->fdir_list);
1005 hns3_err(hw, "Fail to delete FDIR filter!");
1011 int hns3_restore_all_fdir_filter(struct hns3_adapter *hns)
1013 struct hns3_pf *pf = &hns->pf;
1014 struct hns3_fdir_info *fdir_info = &pf->fdir;
1015 struct hns3_fdir_rule_ele *fdir_filter;
1016 struct hns3_hw *hw = &hns->hw;
1020 TAILQ_FOREACH(fdir_filter, &fdir_info->fdir_list, entries) {
1021 ret = hns3_config_action(hw, &fdir_filter->fdir_conf);
1023 ret = hns3_config_key(hns, &fdir_filter->fdir_conf);
1032 hns3_err(hw, "Fail to restore FDIR filter!");
1038 int hns3_get_count(struct hns3_hw *hw, uint32_t id, uint64_t *value)
1040 struct hns3_fd_get_cnt_cmd *req;
1041 struct hns3_cmd_desc desc;
1044 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_FD_COUNTER_OP, true);
1046 req = (struct hns3_fd_get_cnt_cmd *)desc.data;
1047 req->stage = HNS3_FD_STAGE_1;
1048 req->index = rte_cpu_to_le_32(id);
1050 ret = hns3_cmd_send(hw, &desc, 1);
1052 hns3_err(hw, "Read counter fail, ret=%d", ret);
1056 *value = req->value;