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(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
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,
64 enum HNS3_FD_KEY_TYPE {
65 HNS3_FD_KEY_BASE_ON_PTYPE,
66 HNS3_FD_KEY_BASE_ON_TUPLE,
69 enum HNS3_FD_META_DATA {
86 static const struct key_info meta_data_key_info[] = {
97 static const struct key_info tuple_key_info[] = {
100 {OUTER_VLAN_TAG_FST, 16},
101 {OUTER_VLAN_TAG_SEC, 16},
102 {OUTER_ETH_TYPE, 16},
109 {OUTER_SRC_PORT, 16},
110 {OUTER_DST_PORT, 16},
113 {OUTER_TUN_FLOW_ID, 8},
116 {INNER_VLAN_TAG1, 16},
117 {INNER_VLAN_TAG2, 16},
118 {INNER_ETH_TYPE, 16},
125 {INNER_SRC_PORT, 16},
126 {INNER_DST_PORT, 16},
127 {INNER_SCTP_TAG, 32},
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 DIV_ROUND_UP(MAX_KEY_LENGTH / HNS3_BITS_PER_BYTE, 4)
134 #define MAX_KEY_BYTES (MAX_KEY_DWORDS * 4)
136 enum HNS3_FD_PACKET_TYPE {
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
159 #define calc_x(x, k, v) ((x) = (~(k) & (v)))
160 #define calc_y(y, k, v) ((y) = ((k) & (v)))
162 struct hns3_fd_tcam_config_1_cmd {
170 uint8_t tcam_data[8];
173 struct hns3_fd_tcam_config_2_cmd {
174 uint8_t tcam_data[24];
177 struct hns3_fd_tcam_config_3_cmd {
178 uint8_t tcam_data[20];
182 struct hns3_get_fd_mode_cmd {
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;
196 struct hns3_set_fd_key_config_cmd {
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;
204 rte_le32_t tuple_mask;
205 rte_le32_t meta_data_mask;
209 struct hns3_fd_ad_config_cmd {
217 struct hns3_fd_get_cnt_cmd {
226 static int hns3_get_fd_mode(struct hns3_hw *hw, uint8_t *fd_mode)
228 struct hns3_get_fd_mode_cmd *req;
229 struct hns3_cmd_desc desc;
232 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_FD_MODE_CTRL, true);
234 req = (struct hns3_get_fd_mode_cmd *)desc.data;
236 ret = hns3_cmd_send(hw, &desc, 1);
238 hns3_err(hw, "Get fd mode fail, ret=%d", ret);
242 *fd_mode = req->mode;
247 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)
253 struct hns3_get_fd_allocation_cmd *req;
254 struct hns3_cmd_desc desc;
257 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_FD_GET_ALLOCATION, true);
259 req = (struct hns3_get_fd_allocation_cmd *)desc.data;
261 ret = hns3_cmd_send(hw, &desc, 1);
263 hns3_err(hw, "Query fd allocation fail, ret=%d", ret);
267 *stage1_entry_num = rte_le_to_cpu_32(req->stage1_entry_num);
268 *stage2_entry_num = rte_le_to_cpu_32(req->stage2_entry_num);
269 *stage1_counter_num = rte_le_to_cpu_16(req->stage1_counter_num);
270 *stage2_counter_num = rte_le_to_cpu_16(req->stage2_counter_num);
275 static int hns3_set_fd_key_config(struct hns3_adapter *hns)
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 struct hns3_hw *hw = &hns->hw;
281 struct hns3_cmd_desc desc;
284 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_FD_KEY_CONFIG, false);
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 req->stage = HNS3_FD_STAGE_1;
289 req->key_select = key_cfg->key_sel;
290 req->inner_sipv6_word_en = key_cfg->inner_sipv6_word_en;
291 req->inner_dipv6_word_en = key_cfg->inner_dipv6_word_en;
292 req->outer_sipv6_word_en = key_cfg->outer_sipv6_word_en;
293 req->outer_dipv6_word_en = key_cfg->outer_dipv6_word_en;
294 req->tuple_mask = rte_cpu_to_le_32(~key_cfg->tuple_active);
295 req->meta_data_mask = rte_cpu_to_le_32(~key_cfg->meta_data_active);
297 ret = hns3_cmd_send(hw, &desc, 1);
299 hns3_err(hw, "Set fd key fail, ret=%d", ret);
304 int hns3_init_fd_config(struct hns3_adapter *hns)
306 struct hns3_pf *pf = &hns->pf;
307 struct hns3_hw *hw = &hns->hw;
308 struct hns3_fd_key_cfg *key_cfg;
311 ret = hns3_get_fd_mode(hw, &pf->fdir.fd_cfg.fd_mode);
315 switch (pf->fdir.fd_cfg.fd_mode) {
316 case HNS3_FD_MODE_DEPTH_2K_WIDTH_400B_STAGE_1:
317 pf->fdir.fd_cfg.max_key_length = MAX_KEY_LENGTH;
319 case HNS3_FD_MODE_DEPTH_4K_WIDTH_200B_STAGE_1:
320 pf->fdir.fd_cfg.max_key_length = MAX_200B_KEY_LENGTH;
321 hns3_warn(hw, "Unsupported tunnel filter in 4K*200Bit");
324 hns3_err(hw, "Unsupported flow director mode %d",
325 pf->fdir.fd_cfg.fd_mode);
329 key_cfg = &pf->fdir.fd_cfg.key_cfg[HNS3_FD_STAGE_1];
330 key_cfg->key_sel = HNS3_FD_KEY_BASE_ON_TUPLE;
331 key_cfg->inner_sipv6_word_en = IPV6_ADDR_WORD_MASK;
332 key_cfg->inner_dipv6_word_en = IPV6_ADDR_WORD_MASK;
333 key_cfg->outer_sipv6_word_en = 0;
334 key_cfg->outer_dipv6_word_en = 0;
336 key_cfg->tuple_active = BIT(INNER_VLAN_TAG1) | BIT(INNER_ETH_TYPE) |
337 BIT(INNER_IP_PROTO) | BIT(INNER_IP_TOS) |
338 BIT(INNER_SRC_IP) | BIT(INNER_DST_IP) |
339 BIT(INNER_SRC_PORT) | BIT(INNER_DST_PORT);
341 /* If use max 400bit key, we can support tuples for ether type */
342 if (pf->fdir.fd_cfg.max_key_length == MAX_KEY_LENGTH) {
343 key_cfg->tuple_active |=
344 BIT(INNER_DST_MAC) | BIT(INNER_SRC_MAC) |
345 BIT(OUTER_SRC_PORT) | BIT(INNER_SCTP_TAG) |
346 BIT(OUTER_DST_PORT) | BIT(INNER_VLAN_TAG2) |
347 BIT(OUTER_TUN_VNI) | BIT(OUTER_TUN_FLOW_ID) |
348 BIT(OUTER_ETH_TYPE) | BIT(OUTER_IP_PROTO);
351 /* roce_type is used to filter roce frames
352 * dst_vport is used to specify the rule
354 key_cfg->meta_data_active = BIT(DST_VPORT) | BIT(TUNNEL_PACKET) |
357 ret = hns3_get_fd_allocation(hw,
358 &pf->fdir.fd_cfg.rule_num[HNS3_FD_STAGE_1],
359 &pf->fdir.fd_cfg.rule_num[HNS3_FD_STAGE_2],
360 &pf->fdir.fd_cfg.cnt_num[HNS3_FD_STAGE_1],
361 &pf->fdir.fd_cfg.cnt_num[HNS3_FD_STAGE_2]);
365 return hns3_set_fd_key_config(hns);
368 static int hns3_fd_tcam_config(struct hns3_hw *hw, bool sel_x, int loc,
369 uint8_t *key, bool is_add)
371 #define FD_TCAM_CMD_NUM 3
372 struct hns3_fd_tcam_config_1_cmd *req1;
373 struct hns3_fd_tcam_config_2_cmd *req2;
374 struct hns3_fd_tcam_config_3_cmd *req3;
375 struct hns3_cmd_desc desc[FD_TCAM_CMD_NUM];
379 hns3_cmd_setup_basic_desc(&desc[0], HNS3_OPC_FD_TCAM_OP, false);
380 desc[0].flag |= rte_cpu_to_le_16(HNS3_CMD_FLAG_NEXT);
381 hns3_cmd_setup_basic_desc(&desc[1], HNS3_OPC_FD_TCAM_OP, false);
382 desc[1].flag |= rte_cpu_to_le_16(HNS3_CMD_FLAG_NEXT);
383 hns3_cmd_setup_basic_desc(&desc[2], HNS3_OPC_FD_TCAM_OP, false);
385 req1 = (struct hns3_fd_tcam_config_1_cmd *)desc[0].data;
386 req2 = (struct hns3_fd_tcam_config_2_cmd *)desc[1].data;
387 req3 = (struct hns3_fd_tcam_config_3_cmd *)desc[2].data;
389 req1->stage = HNS3_FD_STAGE_1;
390 req1->xy_sel = sel_x ? 1 : 0;
391 hns3_set_bit(req1->port_info, HNS3_FD_EPORT_SW_EN_B, 0);
392 req1->index = rte_cpu_to_le_32(loc);
393 req1->entry_vld = sel_x ? is_add : 0;
396 len = sizeof(req1->tcam_data);
397 memcpy(req1->tcam_data, key, len);
400 len = sizeof(req2->tcam_data);
401 memcpy(req2->tcam_data, key, len);
404 len = sizeof(req3->tcam_data);
405 memcpy(req3->tcam_data, key, len);
408 ret = hns3_cmd_send(hw, desc, FD_TCAM_CMD_NUM);
410 hns3_err(hw, "Config tcam key fail, ret=%d loc=%d add=%d",
415 static int hns3_fd_ad_config(struct hns3_hw *hw, int loc,
416 struct hns3_fd_ad_data *action)
418 struct hns3_fd_ad_config_cmd *req;
419 struct hns3_cmd_desc desc;
420 uint64_t ad_data = 0;
423 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_FD_AD_OP, false);
425 req = (struct hns3_fd_ad_config_cmd *)desc.data;
426 req->index = rte_cpu_to_le_32(loc);
427 req->stage = HNS3_FD_STAGE_1;
429 hns3_set_bit(ad_data, HNS3_FD_AD_WR_RULE_ID_B,
430 action->write_rule_id_to_bd);
431 hns3_set_field(ad_data, HNS3_FD_AD_RULE_ID_M, HNS3_FD_AD_RULE_ID_S,
433 if (action->nb_queues > 1) {
434 hns3_set_bit(ad_data, HNS3_FD_AD_QUEUE_REGION_EN_B, 1);
435 hns3_set_field(ad_data, HNS3_FD_AD_QUEUE_REGION_SIZE_M,
436 HNS3_FD_AD_QUEUE_REGION_SIZE_S,
437 rte_log2_u32(action->nb_queues));
439 /* set extend bit if counter_id is in [128 ~ 255] */
440 if (action->counter_id & BIT(HNS3_FD_AD_COUNTER_HIGH_BIT))
441 hns3_set_bit(ad_data, HNS3_FD_AD_COUNTER_HIGH_BIT_B, 1);
442 /* set extend bit if queue id > 1024 */
443 if (action->queue_id & BIT(HNS3_FD_AD_QUEUE_ID_HIGH_BIT))
444 hns3_set_bit(ad_data, HNS3_FD_AD_QUEUE_ID_HIGH_BIT_B, 1);
445 ad_data <<= HNS3_FD_AD_DATA_S;
446 hns3_set_bit(ad_data, HNS3_FD_AD_DROP_B, action->drop_packet);
447 if (action->nb_queues == 1)
448 hns3_set_bit(ad_data, HNS3_FD_AD_DIRECT_QID_B, 1);
449 hns3_set_field(ad_data, HNS3_FD_AD_QID_M, HNS3_FD_AD_QID_S,
451 hns3_set_bit(ad_data, HNS3_FD_AD_USE_COUNTER_B, action->use_counter);
452 hns3_set_field(ad_data, HNS3_FD_AD_COUNTER_NUM_M,
453 HNS3_FD_AD_COUNTER_NUM_S, action->counter_id);
454 hns3_set_bit(ad_data, HNS3_FD_AD_NXT_STEP_B, action->use_next_stage);
455 hns3_set_field(ad_data, HNS3_FD_AD_NXT_KEY_M, HNS3_FD_AD_NXT_KEY_S,
456 action->next_input_key);
458 req->ad_data = rte_cpu_to_le_64(ad_data);
459 ret = hns3_cmd_send(hw, &desc, 1);
461 hns3_err(hw, "Config fd ad fail, ret=%d loc=%d", ret, loc);
466 static inline void hns3_fd_convert_mac(uint8_t *key, uint8_t *mask,
467 uint8_t *mac_x, uint8_t *mac_y)
472 for (i = 0; i < RTE_ETHER_ADDR_LEN; i++) {
473 tmp = RTE_ETHER_ADDR_LEN - 1 - i;
474 calc_x(mac_x[tmp], key[i], mask[i]);
475 calc_y(mac_y[tmp], key[i], mask[i]);
479 static void hns3_fd_convert_int16(uint32_t tuple, struct hns3_fdir_rule *rule,
480 uint8_t *val_x, uint8_t *val_y)
489 key = rule->key_conf.spec.outer_src_port;
490 mask = rule->key_conf.mask.outer_src_port;
493 key = rule->key_conf.spec.tunnel_type;
494 mask = rule->key_conf.mask.tunnel_type;
497 key = rule->key_conf.spec.outer_ether_type;
498 mask = rule->key_conf.mask.outer_ether_type;
501 key = rule->key_conf.spec.src_port;
502 mask = rule->key_conf.mask.src_port;
505 key = rule->key_conf.spec.dst_port;
506 mask = rule->key_conf.mask.dst_port;
508 case INNER_VLAN_TAG1:
509 key = rule->key_conf.spec.vlan_tag1;
510 mask = rule->key_conf.mask.vlan_tag1;
512 case INNER_VLAN_TAG2:
513 key = rule->key_conf.spec.vlan_tag2;
514 mask = rule->key_conf.mask.vlan_tag2;
517 /* INNER_ETH_TYPE: */
518 key = rule->key_conf.spec.ether_type;
519 mask = rule->key_conf.mask.ether_type;
522 calc_x(tmp_x_s, key, mask);
523 calc_y(tmp_y_s, key, mask);
524 val_x[0] = rte_cpu_to_le_16(tmp_x_s) & 0xFF;
525 val_x[1] = rte_cpu_to_le_16(tmp_x_s) >> HNS3_BITS_PER_BYTE;
526 val_y[0] = rte_cpu_to_le_16(tmp_y_s) & 0xFF;
527 val_y[1] = rte_cpu_to_le_16(tmp_y_s) >> HNS3_BITS_PER_BYTE;
530 static inline void hns3_fd_convert_int32(uint32_t key, uint32_t mask,
531 uint8_t *val_x, uint8_t *val_y)
536 calc_x(tmp_x_l, key, mask);
537 calc_y(tmp_y_l, key, mask);
538 memcpy(val_x, &tmp_x_l, sizeof(tmp_x_l));
539 memcpy(val_y, &tmp_y_l, sizeof(tmp_y_l));
542 static bool hns3_fd_convert_tuple(struct hns3_hw *hw,
543 uint32_t tuple, uint8_t *key_x,
544 uint8_t *key_y, struct hns3_fdir_rule *rule)
546 struct hns3_fdir_key_conf *key_conf;
550 if ((rule->input_set & BIT(tuple)) == 0)
553 key_conf = &rule->key_conf;
556 hns3_fd_convert_mac(key_conf->spec.dst_mac,
557 key_conf->mask.dst_mac, key_x, key_y);
560 hns3_fd_convert_mac(key_conf->spec.src_mac,
561 key_conf->mask.src_mac, key_x, key_y);
568 case INNER_VLAN_TAG1:
569 case INNER_VLAN_TAG2:
571 hns3_fd_convert_int16(tuple, rule, key_x, key_y);
574 hns3_fd_convert_int32(key_conf->spec.src_ip[IP_ADDR_KEY_ID],
575 key_conf->mask.src_ip[IP_ADDR_KEY_ID],
579 hns3_fd_convert_int32(key_conf->spec.dst_ip[IP_ADDR_KEY_ID],
580 key_conf->mask.dst_ip[IP_ADDR_KEY_ID],
584 hns3_fd_convert_int32(key_conf->spec.sctp_tag,
585 key_conf->mask.sctp_tag, key_x, key_y);
588 for (i = 0; i < VNI_OR_TNI_LEN; i++) {
589 tmp = VNI_OR_TNI_LEN - 1 - i;
591 key_conf->spec.outer_tun_vni[i],
592 key_conf->mask.outer_tun_vni[i]);
594 key_conf->spec.outer_tun_vni[i],
595 key_conf->mask.outer_tun_vni[i]);
598 case OUTER_TUN_FLOW_ID:
599 calc_x(*key_x, key_conf->spec.outer_tun_flow_id,
600 key_conf->mask.outer_tun_flow_id);
601 calc_y(*key_y, key_conf->spec.outer_tun_flow_id,
602 key_conf->mask.outer_tun_flow_id);
605 calc_x(*key_x, key_conf->spec.ip_tos, key_conf->mask.ip_tos);
606 calc_y(*key_y, key_conf->spec.ip_tos, key_conf->mask.ip_tos);
609 calc_x(*key_x, key_conf->spec.outer_proto,
610 key_conf->mask.outer_proto);
611 calc_y(*key_y, key_conf->spec.outer_proto,
612 key_conf->mask.outer_proto);
615 calc_x(*key_x, key_conf->spec.ip_proto,
616 key_conf->mask.ip_proto);
617 calc_y(*key_y, key_conf->spec.ip_proto,
618 key_conf->mask.ip_proto);
621 hns3_warn(hw, "not support tuple of (%d)", tuple);
627 static uint32_t hns3_get_port_number(uint8_t pf_id, uint8_t vf_id)
629 uint32_t port_number = 0;
631 hns3_set_field(port_number, HNS3_PF_ID_M, HNS3_PF_ID_S, pf_id);
632 hns3_set_field(port_number, HNS3_VF_ID_M, HNS3_VF_ID_S, vf_id);
633 hns3_set_bit(port_number, HNS3_PORT_TYPE_B, HOST_PORT);
638 static void hns3_fd_convert_meta_data(struct hns3_fd_key_cfg *cfg,
640 struct hns3_fdir_rule *rule,
641 uint8_t *key_x, uint8_t *key_y)
643 uint16_t meta_data = 0;
644 uint32_t port_number;
652 for (i = 0; i < MAX_META_DATA; i++) {
653 if ((cfg->meta_data_active & BIT(i)) == 0)
656 tuple_size = meta_data_key_info[i].key_length;
657 if (i == TUNNEL_PACKET) {
658 hns3_set_bit(meta_data, cur_pos,
659 rule->key_conf.spec.tunnel_type ? 1 : 0);
660 cur_pos += tuple_size;
661 } else if (i == VLAN_NUMBER) {
664 if (rule->key_conf.spec.tunnel_type == 0)
665 vlan_num = rule->key_conf.vlan_num;
667 vlan_num = rule->key_conf.outer_vlan_num;
669 vlan_tag = HNS3_VLAN_TAG_TYPE_TAG1;
670 else if (vlan_num == VLAN_TAG_NUM_MAX)
671 vlan_tag = HNS3_VLAN_TAG_TYPE_TAG1_2;
673 vlan_tag = HNS3_VLAN_TAG_TYPE_NONE;
674 hns3_set_field(meta_data,
675 GENMASK(cur_pos + tuple_size,
676 cur_pos), cur_pos, vlan_tag);
677 cur_pos += tuple_size;
678 } else if (i == DST_VPORT) {
679 port_number = hns3_get_port_number(0, vf_id);
680 hns3_set_field(meta_data,
681 GENMASK(cur_pos + tuple_size, cur_pos),
682 cur_pos, port_number);
683 cur_pos += tuple_size;
687 calc_x(tmp_x, meta_data, 0xFFFF);
688 calc_y(tmp_y, meta_data, 0xFFFF);
689 shift_bits = sizeof(meta_data) * HNS3_BITS_PER_BYTE - cur_pos;
691 tmp_x = rte_cpu_to_le_32(tmp_x << shift_bits);
692 tmp_y = rte_cpu_to_le_32(tmp_y << shift_bits);
693 key_x[0] = tmp_x & 0xFF;
694 key_x[1] = (tmp_x >> HNS3_BITS_PER_BYTE) & 0xFF;
695 key_y[0] = tmp_y & 0xFF;
696 key_y[1] = (tmp_y >> HNS3_BITS_PER_BYTE) & 0xFF;
699 /* A complete key is combined with meta data key and tuple key.
700 * Meta data key is stored at the MSB region, and tuple key is stored at
701 * the LSB region, unused bits will be filled 0.
703 static int hns3_config_key(struct hns3_adapter *hns,
704 struct hns3_fdir_rule *rule)
706 struct hns3_pf *pf = &hns->pf;
707 struct hns3_hw *hw = &hns->hw;
708 struct hns3_fd_key_cfg *key_cfg;
711 uint8_t key_x[MAX_KEY_BYTES] __rte_aligned(4);
712 uint8_t key_y[MAX_KEY_BYTES] __rte_aligned(4);
713 uint8_t vf_id = rule->vf_id;
714 uint8_t meta_data_region;
719 memset(key_x, 0, sizeof(key_x));
720 memset(key_y, 0, sizeof(key_y));
724 key_cfg = &pf->fdir.fd_cfg.key_cfg[HNS3_FD_STAGE_1];
725 for (i = 0; i < MAX_TUPLE; i++) {
728 tuple_size = tuple_key_info[i].key_length / HNS3_BITS_PER_BYTE;
729 if (key_cfg->tuple_active & BIT(i)) {
730 tuple_valid = hns3_fd_convert_tuple(hw, i, cur_key_x,
733 cur_key_x += tuple_size;
734 cur_key_y += tuple_size;
739 meta_data_region = pf->fdir.fd_cfg.max_key_length / HNS3_BITS_PER_BYTE -
740 MAX_META_DATA_LENGTH / HNS3_BITS_PER_BYTE;
742 hns3_fd_convert_meta_data(key_cfg, vf_id, rule,
743 key_x + meta_data_region,
744 key_y + meta_data_region);
746 ret = hns3_fd_tcam_config(hw, false, rule->location, key_y, true);
748 hns3_err(hw, "Config fd key_y fail, loc=%d, ret=%d",
749 rule->queue_id, ret);
753 ret = hns3_fd_tcam_config(hw, true, rule->location, key_x, true);
755 hns3_err(hw, "Config fd key_x fail, loc=%d, ret=%d",
756 rule->queue_id, ret);
760 static int hns3_config_action(struct hns3_hw *hw, struct hns3_fdir_rule *rule)
762 struct hns3_fd_ad_data ad_data;
764 ad_data.ad_id = rule->location;
766 if (rule->action == HNS3_FD_ACTION_DROP_PACKET) {
767 ad_data.drop_packet = true;
768 ad_data.queue_id = 0;
769 ad_data.nb_queues = 0;
771 ad_data.drop_packet = false;
772 ad_data.queue_id = rule->queue_id;
773 ad_data.nb_queues = rule->nb_queues;
776 if (unlikely(rule->flags & HNS3_RULE_FLAG_COUNTER)) {
777 ad_data.use_counter = true;
778 ad_data.counter_id = rule->act_cnt.id;
780 ad_data.use_counter = false;
781 ad_data.counter_id = 0;
784 if (unlikely(rule->flags & HNS3_RULE_FLAG_FDID))
785 ad_data.rule_id = rule->fd_id;
787 ad_data.rule_id = rule->location;
789 ad_data.use_next_stage = false;
790 ad_data.next_input_key = 0;
792 ad_data.write_rule_id_to_bd = true;
794 return hns3_fd_ad_config(hw, ad_data.ad_id, &ad_data);
797 static int hns3_fd_clear_all_rules(struct hns3_hw *hw, uint32_t rule_num)
802 for (i = 0; i < rule_num; i++) {
803 ret = hns3_fd_tcam_config(hw, true, i, NULL, false);
811 int hns3_fdir_filter_init(struct hns3_adapter *hns)
813 struct hns3_pf *pf = &hns->pf;
814 struct hns3_fdir_info *fdir_info = &pf->fdir;
815 uint32_t rule_num = fdir_info->fd_cfg.rule_num[HNS3_FD_STAGE_1];
816 char fdir_hash_name[RTE_HASH_NAMESIZE];
817 struct rte_hash_parameters fdir_hash_params = {
818 .name = fdir_hash_name,
820 .key_len = sizeof(struct hns3_fdir_key_conf),
821 .hash_func = rte_hash_crc,
822 .hash_func_init_val = 0,
826 ret = hns3_fd_clear_all_rules(&hns->hw, rule_num);
828 PMD_INIT_LOG(ERR, "Clear all fd rules fail! ret = %d", ret);
832 fdir_hash_params.socket_id = rte_socket_id();
833 TAILQ_INIT(&fdir_info->fdir_list);
834 rte_spinlock_init(&fdir_info->flows_lock);
835 snprintf(fdir_hash_name, RTE_HASH_NAMESIZE, "%s", hns->hw.data->name);
836 fdir_info->hash_handle = rte_hash_create(&fdir_hash_params);
837 if (fdir_info->hash_handle == NULL) {
838 PMD_INIT_LOG(ERR, "Create FDIR hash handle fail!");
841 fdir_info->hash_map = rte_zmalloc("hns3 FDIR hash",
843 sizeof(struct hns3_fdir_rule_ele *),
845 if (fdir_info->hash_map == NULL) {
846 PMD_INIT_LOG(ERR, "Allocate memory for FDIR hash map fail!");
847 rte_hash_free(fdir_info->hash_handle);
854 void hns3_fdir_filter_uninit(struct hns3_adapter *hns)
856 struct hns3_pf *pf = &hns->pf;
857 struct hns3_fdir_info *fdir_info = &pf->fdir;
858 struct hns3_fdir_rule_ele *fdir_filter;
860 rte_spinlock_lock(&fdir_info->flows_lock);
861 if (fdir_info->hash_map) {
862 rte_free(fdir_info->hash_map);
863 fdir_info->hash_map = NULL;
865 if (fdir_info->hash_handle) {
866 rte_hash_free(fdir_info->hash_handle);
867 fdir_info->hash_handle = NULL;
869 rte_spinlock_unlock(&fdir_info->flows_lock);
871 fdir_filter = TAILQ_FIRST(&fdir_info->fdir_list);
872 while (fdir_filter) {
873 TAILQ_REMOVE(&fdir_info->fdir_list, fdir_filter, entries);
874 hns3_fd_tcam_config(&hns->hw, true,
875 fdir_filter->fdir_conf.location, NULL,
877 rte_free(fdir_filter);
878 fdir_filter = TAILQ_FIRST(&fdir_info->fdir_list);
883 * Find a key in the hash table.
885 * - Zero and positive values are key location.
886 * - -EINVAL if the parameters are invalid.
887 * - -ENOENT if the key is not found.
889 static int hns3_fdir_filter_lookup(struct hns3_fdir_info *fdir_info,
890 struct hns3_fdir_key_conf *key)
895 rte_spinlock_lock(&fdir_info->flows_lock);
896 sig = rte_hash_crc(key, sizeof(*key), 0);
897 ret = rte_hash_lookup_with_hash(fdir_info->hash_handle, key, sig);
898 rte_spinlock_unlock(&fdir_info->flows_lock);
903 static int hns3_insert_fdir_filter(struct hns3_hw *hw,
904 struct hns3_fdir_info *fdir_info,
905 struct hns3_fdir_rule_ele *fdir_filter)
907 struct hns3_fdir_key_conf *key;
911 key = &fdir_filter->fdir_conf.key_conf;
912 rte_spinlock_lock(&fdir_info->flows_lock);
913 sig = rte_hash_crc(key, sizeof(*key), 0);
914 ret = rte_hash_add_key_with_hash(fdir_info->hash_handle, key, sig);
916 rte_spinlock_unlock(&fdir_info->flows_lock);
917 hns3_err(hw, "Hash table full? err:%d(%s)!", ret,
922 fdir_info->hash_map[ret] = fdir_filter;
923 TAILQ_INSERT_TAIL(&fdir_info->fdir_list, fdir_filter, entries);
924 rte_spinlock_unlock(&fdir_info->flows_lock);
929 static int hns3_remove_fdir_filter(struct hns3_hw *hw,
930 struct hns3_fdir_info *fdir_info,
931 struct hns3_fdir_key_conf *key)
933 struct hns3_fdir_rule_ele *fdir_filter;
937 rte_spinlock_lock(&fdir_info->flows_lock);
938 sig = rte_hash_crc(key, sizeof(*key), 0);
939 ret = rte_hash_del_key_with_hash(fdir_info->hash_handle, key, sig);
941 rte_spinlock_unlock(&fdir_info->flows_lock);
942 hns3_err(hw, "Delete hash key fail ret=%d", ret);
946 fdir_filter = fdir_info->hash_map[ret];
947 fdir_info->hash_map[ret] = NULL;
948 TAILQ_REMOVE(&fdir_info->fdir_list, fdir_filter, entries);
949 rte_spinlock_unlock(&fdir_info->flows_lock);
951 rte_free(fdir_filter);
956 int hns3_fdir_filter_program(struct hns3_adapter *hns,
957 struct hns3_fdir_rule *rule, bool del)
959 struct hns3_pf *pf = &hns->pf;
960 struct hns3_fdir_info *fdir_info = &pf->fdir;
961 struct hns3_fdir_rule_ele *node;
962 struct hns3_hw *hw = &hns->hw;
966 ret = hns3_fd_tcam_config(hw, true, rule->location, NULL,
969 hns3_err(hw, "Failed to delete fdir: %d src_ip:%x "
970 "dst_ip:%x src_port:%d dst_port:%d ret = %d",
972 rule->key_conf.spec.src_ip[IP_ADDR_KEY_ID],
973 rule->key_conf.spec.dst_ip[IP_ADDR_KEY_ID],
974 rule->key_conf.spec.src_port,
975 rule->key_conf.spec.dst_port, ret);
977 hns3_remove_fdir_filter(hw, fdir_info, &rule->key_conf);
982 ret = hns3_fdir_filter_lookup(fdir_info, &rule->key_conf);
984 hns3_err(hw, "Conflict with existing fdir loc: %d", ret);
988 node = rte_zmalloc("hns3 fdir rule", sizeof(struct hns3_fdir_rule_ele),
991 hns3_err(hw, "Failed to allocate fdir_rule memory");
995 rte_memcpy(&node->fdir_conf, rule, sizeof(struct hns3_fdir_rule));
996 ret = hns3_insert_fdir_filter(hw, fdir_info, node);
1001 rule->location = ret;
1002 node->fdir_conf.location = ret;
1004 rte_spinlock_lock(&fdir_info->flows_lock);
1005 ret = hns3_config_action(hw, rule);
1007 ret = hns3_config_key(hns, rule);
1008 rte_spinlock_unlock(&fdir_info->flows_lock);
1010 hns3_err(hw, "Failed to config fdir: %d src_ip:%x dst_ip:%x "
1011 "src_port:%d dst_port:%d ret = %d",
1013 rule->key_conf.spec.src_ip[IP_ADDR_KEY_ID],
1014 rule->key_conf.spec.dst_ip[IP_ADDR_KEY_ID],
1015 rule->key_conf.spec.src_port,
1016 rule->key_conf.spec.dst_port, ret);
1017 (void)hns3_remove_fdir_filter(hw, fdir_info, &rule->key_conf);
1023 /* remove all the flow director filters */
1024 int hns3_clear_all_fdir_filter(struct hns3_adapter *hns)
1026 struct hns3_pf *pf = &hns->pf;
1027 struct hns3_fdir_info *fdir_info = &pf->fdir;
1028 struct hns3_fdir_rule_ele *fdir_filter;
1029 struct hns3_hw *hw = &hns->hw;
1032 /* flush flow director */
1033 rte_spinlock_lock(&fdir_info->flows_lock);
1034 rte_hash_reset(fdir_info->hash_handle);
1035 rte_spinlock_unlock(&fdir_info->flows_lock);
1037 fdir_filter = TAILQ_FIRST(&fdir_info->fdir_list);
1038 while (fdir_filter) {
1039 TAILQ_REMOVE(&fdir_info->fdir_list, fdir_filter, entries);
1040 ret += hns3_fd_tcam_config(hw, true,
1041 fdir_filter->fdir_conf.location,
1043 rte_free(fdir_filter);
1044 fdir_filter = TAILQ_FIRST(&fdir_info->fdir_list);
1048 hns3_err(hw, "Fail to delete FDIR filter, ret = %d", ret);
1054 int hns3_restore_all_fdir_filter(struct hns3_adapter *hns)
1056 struct hns3_pf *pf = &hns->pf;
1057 struct hns3_fdir_info *fdir_info = &pf->fdir;
1058 struct hns3_fdir_rule_ele *fdir_filter;
1059 struct hns3_hw *hw = &hns->hw;
1063 TAILQ_FOREACH(fdir_filter, &fdir_info->fdir_list, entries) {
1064 ret = hns3_config_action(hw, &fdir_filter->fdir_conf);
1066 ret = hns3_config_key(hns, &fdir_filter->fdir_conf);
1075 hns3_err(hw, "Fail to restore FDIR filter, ret = %d", ret);
1081 int hns3_get_count(struct hns3_hw *hw, uint32_t id, uint64_t *value)
1083 struct hns3_fd_get_cnt_cmd *req;
1084 struct hns3_cmd_desc desc;
1087 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_FD_COUNTER_OP, true);
1089 req = (struct hns3_fd_get_cnt_cmd *)desc.data;
1090 req->stage = HNS3_FD_STAGE_1;
1091 req->index = rte_cpu_to_le_32(id);
1093 ret = hns3_cmd_send(hw, &desc, 1);
1095 hns3_err(hw, "Read counter fail, ret=%d", ret);
1099 *value = req->value;