1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2018-2021 HiSilicon Limited.
5 #include <ethdev_driver.h>
7 #include <rte_hash_crc.h>
9 #include <rte_malloc.h>
11 #include "hns3_ethdev.h"
12 #include "hns3_logs.h"
14 #define HNS3_VLAN_TAG_TYPE_NONE 0
15 #define HNS3_VLAN_TAG_TYPE_TAG2 1
16 #define HNS3_VLAN_TAG_TYPE_TAG1 2
17 #define HNS3_VLAN_TAG_TYPE_TAG1_2 3
19 #define HNS3_PF_ID_S 0
20 #define HNS3_PF_ID_M GENMASK(2, 0)
21 #define HNS3_VF_ID_S 3
22 #define HNS3_VF_ID_M GENMASK(10, 3)
23 #define HNS3_PORT_TYPE_B 11
24 #define HNS3_NETWORK_PORT_ID_S 0
25 #define HNS3_NETWORK_PORT_ID_M GENMASK(3, 0)
27 #define HNS3_FD_EPORT_SW_EN_B 0
29 #define HNS3_FD_AD_DATA_S 32
30 #define HNS3_FD_AD_DROP_B 0
31 #define HNS3_FD_AD_DIRECT_QID_B 1
32 #define HNS3_FD_AD_QID_S 2
33 #define HNS3_FD_AD_QID_M GENMASK(11, 2)
34 #define HNS3_FD_AD_USE_COUNTER_B 12
35 #define HNS3_FD_AD_COUNTER_NUM_S 13
36 #define HNS3_FD_AD_COUNTER_NUM_M GENMASK(19, 13)
37 #define HNS3_FD_AD_NXT_STEP_B 20
38 #define HNS3_FD_AD_NXT_KEY_S 21
39 #define HNS3_FD_AD_NXT_KEY_M GENMASK(25, 21)
40 #define HNS3_FD_AD_WR_RULE_ID_B 0
41 #define HNS3_FD_AD_RULE_ID_S 1
42 #define HNS3_FD_AD_RULE_ID_M GENMASK(12, 1)
43 #define HNS3_FD_AD_QUEUE_REGION_EN_B 16
44 #define HNS3_FD_AD_QUEUE_REGION_SIZE_S 17
45 #define HNS3_FD_AD_QUEUE_REGION_SIZE_M GENMASK(20, 17)
46 #define HNS3_FD_AD_COUNTER_HIGH_BIT 7
47 #define HNS3_FD_AD_COUNTER_HIGH_BIT_B 26
48 #define HNS3_FD_AD_QUEUE_ID_HIGH_BIT 10
49 #define HNS3_FD_AD_QUEUE_ID_HIGH_BIT_B 21
57 HNS3_FD_MODE_DEPTH_2K_WIDTH_400B_STAGE_1,
58 HNS3_FD_MODE_DEPTH_1K_WIDTH_400B_STAGE_2,
59 HNS3_FD_MODE_DEPTH_4K_WIDTH_200B_STAGE_1,
60 HNS3_FD_MODE_DEPTH_2K_WIDTH_200B_STAGE_2,
63 enum HNS3_FD_KEY_TYPE {
64 HNS3_FD_KEY_BASE_ON_PTYPE,
65 HNS3_FD_KEY_BASE_ON_TUPLE,
68 enum HNS3_FD_META_DATA {
85 static const struct key_info meta_data_key_info[] = {
96 static const struct key_info tuple_key_info[] = {
99 {OUTER_VLAN_TAG_FST, 16},
100 {OUTER_VLAN_TAG_SEC, 16},
101 {OUTER_ETH_TYPE, 16},
108 {OUTER_SRC_PORT, 16},
109 {OUTER_DST_PORT, 16},
112 {OUTER_TUN_FLOW_ID, 8},
115 {INNER_VLAN_TAG1, 16},
116 {INNER_VLAN_TAG2, 16},
117 {INNER_ETH_TYPE, 16},
124 {INNER_SRC_PORT, 16},
125 {INNER_DST_PORT, 16},
126 {INNER_SCTP_TAG, 32},
129 #define MAX_KEY_LENGTH 400
130 #define MAX_200B_KEY_LENGTH 200
131 #define MAX_META_DATA_LENGTH 16
132 #define MAX_KEY_DWORDS DIV_ROUND_UP(MAX_KEY_LENGTH / HNS3_BITS_PER_BYTE, 4)
133 #define MAX_KEY_BYTES (MAX_KEY_DWORDS * 4)
135 enum HNS3_FD_PACKET_TYPE {
140 /* For each bit of TCAM entry, it uses a pair of 'x' and
141 * 'y' to indicate which value to match, like below:
142 * ----------------------------------
143 * | bit x | bit y | search value |
144 * ----------------------------------
145 * | 0 | 0 | always hit |
146 * ----------------------------------
147 * | 1 | 0 | match '0' |
148 * ----------------------------------
149 * | 0 | 1 | match '1' |
150 * ----------------------------------
151 * | 1 | 1 | invalid |
152 * ----------------------------------
153 * Then for input key(k) and mask(v), we can calculate the value by
158 #define calc_x(x, k, v) ((x) = (~(k) & (v)))
159 #define calc_y(y, k, v) ((y) = ((k) & (v)))
161 struct hns3_fd_tcam_config_1_cmd {
169 uint8_t tcam_data[8];
172 struct hns3_fd_tcam_config_2_cmd {
173 uint8_t tcam_data[24];
176 struct hns3_fd_tcam_config_3_cmd {
177 uint8_t tcam_data[20];
181 struct hns3_get_fd_mode_cmd {
187 struct hns3_get_fd_allocation_cmd {
188 rte_le32_t stage1_entry_num;
189 rte_le32_t stage2_entry_num;
190 rte_le16_t stage1_counter_num;
191 rte_le16_t stage2_counter_num;
195 struct hns3_set_fd_key_config_cmd {
198 uint8_t inner_sipv6_word_en;
199 uint8_t inner_dipv6_word_en;
200 uint8_t outer_sipv6_word_en;
201 uint8_t outer_dipv6_word_en;
203 rte_le32_t tuple_mask;
204 rte_le32_t meta_data_mask;
208 struct hns3_fd_ad_config_cmd {
216 struct hns3_fd_get_cnt_cmd {
225 static int hns3_get_fd_mode(struct hns3_hw *hw, uint8_t *fd_mode)
227 struct hns3_get_fd_mode_cmd *req;
228 struct hns3_cmd_desc desc;
231 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_FD_MODE_CTRL, true);
233 req = (struct hns3_get_fd_mode_cmd *)desc.data;
235 ret = hns3_cmd_send(hw, &desc, 1);
237 hns3_err(hw, "Get fd mode fail, ret=%d", ret);
241 *fd_mode = req->mode;
246 static int hns3_get_fd_allocation(struct hns3_hw *hw,
247 uint32_t *stage1_entry_num,
248 uint32_t *stage2_entry_num,
249 uint16_t *stage1_counter_num,
250 uint16_t *stage2_counter_num)
252 struct hns3_get_fd_allocation_cmd *req;
253 struct hns3_cmd_desc desc;
256 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_FD_GET_ALLOCATION, true);
258 req = (struct hns3_get_fd_allocation_cmd *)desc.data;
260 ret = hns3_cmd_send(hw, &desc, 1);
262 hns3_err(hw, "Query fd allocation fail, ret=%d", ret);
266 *stage1_entry_num = rte_le_to_cpu_32(req->stage1_entry_num);
267 *stage2_entry_num = rte_le_to_cpu_32(req->stage2_entry_num);
268 *stage1_counter_num = rte_le_to_cpu_16(req->stage1_counter_num);
269 *stage2_counter_num = rte_le_to_cpu_16(req->stage2_counter_num);
274 static int hns3_set_fd_key_config(struct hns3_adapter *hns)
276 struct hns3_set_fd_key_config_cmd *req;
277 struct hns3_fd_key_cfg *key_cfg;
278 struct hns3_pf *pf = &hns->pf;
279 struct hns3_hw *hw = &hns->hw;
280 struct hns3_cmd_desc desc;
283 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_FD_KEY_CONFIG, false);
285 req = (struct hns3_set_fd_key_config_cmd *)desc.data;
286 key_cfg = &pf->fdir.fd_cfg.key_cfg[HNS3_FD_STAGE_1];
287 req->stage = HNS3_FD_STAGE_1;
288 req->key_select = key_cfg->key_sel;
289 req->inner_sipv6_word_en = key_cfg->inner_sipv6_word_en;
290 req->inner_dipv6_word_en = key_cfg->inner_dipv6_word_en;
291 req->outer_sipv6_word_en = key_cfg->outer_sipv6_word_en;
292 req->outer_dipv6_word_en = key_cfg->outer_dipv6_word_en;
293 req->tuple_mask = rte_cpu_to_le_32(~key_cfg->tuple_active);
294 req->meta_data_mask = rte_cpu_to_le_32(~key_cfg->meta_data_active);
296 ret = hns3_cmd_send(hw, &desc, 1);
298 hns3_err(hw, "Set fd key fail, ret=%d", ret);
303 int hns3_init_fd_config(struct hns3_adapter *hns)
305 struct hns3_pf *pf = &hns->pf;
306 struct hns3_hw *hw = &hns->hw;
307 struct hns3_fd_key_cfg *key_cfg;
310 ret = hns3_get_fd_mode(hw, &pf->fdir.fd_cfg.fd_mode);
314 switch (pf->fdir.fd_cfg.fd_mode) {
315 case HNS3_FD_MODE_DEPTH_2K_WIDTH_400B_STAGE_1:
316 pf->fdir.fd_cfg.max_key_length = MAX_KEY_LENGTH;
318 case HNS3_FD_MODE_DEPTH_4K_WIDTH_200B_STAGE_1:
319 pf->fdir.fd_cfg.max_key_length = MAX_200B_KEY_LENGTH;
320 hns3_warn(hw, "Unsupported tunnel filter in 4K*200Bit");
323 hns3_err(hw, "Unsupported flow director mode %u",
324 pf->fdir.fd_cfg.fd_mode);
328 key_cfg = &pf->fdir.fd_cfg.key_cfg[HNS3_FD_STAGE_1];
329 key_cfg->key_sel = HNS3_FD_KEY_BASE_ON_TUPLE;
330 key_cfg->inner_sipv6_word_en = IPV6_ADDR_WORD_MASK;
331 key_cfg->inner_dipv6_word_en = IPV6_ADDR_WORD_MASK;
332 key_cfg->outer_sipv6_word_en = 0;
333 key_cfg->outer_dipv6_word_en = 0;
335 key_cfg->tuple_active = BIT(INNER_VLAN_TAG1) | BIT(INNER_ETH_TYPE) |
336 BIT(INNER_IP_PROTO) | BIT(INNER_IP_TOS) |
337 BIT(INNER_SRC_IP) | BIT(INNER_DST_IP) |
338 BIT(INNER_SRC_PORT) | BIT(INNER_DST_PORT);
340 /* If use max 400bit key, we can support tuples for ether type */
341 if (pf->fdir.fd_cfg.max_key_length == MAX_KEY_LENGTH) {
342 key_cfg->tuple_active |=
343 BIT(INNER_DST_MAC) | BIT(INNER_SRC_MAC) |
344 BIT(OUTER_SRC_PORT) | BIT(INNER_SCTP_TAG) |
345 BIT(OUTER_DST_PORT) | BIT(INNER_VLAN_TAG2) |
346 BIT(OUTER_TUN_VNI) | BIT(OUTER_TUN_FLOW_ID) |
347 BIT(OUTER_ETH_TYPE) | BIT(OUTER_IP_PROTO);
350 /* roce_type is used to filter roce frames
351 * dst_vport is used to specify the rule
353 key_cfg->meta_data_active = BIT(DST_VPORT) | BIT(TUNNEL_PACKET) |
356 ret = hns3_get_fd_allocation(hw,
357 &pf->fdir.fd_cfg.rule_num[HNS3_FD_STAGE_1],
358 &pf->fdir.fd_cfg.rule_num[HNS3_FD_STAGE_2],
359 &pf->fdir.fd_cfg.cnt_num[HNS3_FD_STAGE_1],
360 &pf->fdir.fd_cfg.cnt_num[HNS3_FD_STAGE_2]);
364 return hns3_set_fd_key_config(hns);
367 static int hns3_fd_tcam_config(struct hns3_hw *hw, bool sel_x, int loc,
368 uint8_t *key, bool is_add)
370 #define FD_TCAM_CMD_NUM 3
371 struct hns3_fd_tcam_config_1_cmd *req1;
372 struct hns3_fd_tcam_config_2_cmd *req2;
373 struct hns3_fd_tcam_config_3_cmd *req3;
374 struct hns3_cmd_desc desc[FD_TCAM_CMD_NUM];
378 hns3_cmd_setup_basic_desc(&desc[0], HNS3_OPC_FD_TCAM_OP, false);
379 desc[0].flag |= rte_cpu_to_le_16(HNS3_CMD_FLAG_NEXT);
380 hns3_cmd_setup_basic_desc(&desc[1], HNS3_OPC_FD_TCAM_OP, false);
381 desc[1].flag |= rte_cpu_to_le_16(HNS3_CMD_FLAG_NEXT);
382 hns3_cmd_setup_basic_desc(&desc[2], HNS3_OPC_FD_TCAM_OP, false);
384 req1 = (struct hns3_fd_tcam_config_1_cmd *)desc[0].data;
385 req2 = (struct hns3_fd_tcam_config_2_cmd *)desc[1].data;
386 req3 = (struct hns3_fd_tcam_config_3_cmd *)desc[2].data;
388 req1->stage = HNS3_FD_STAGE_1;
389 req1->xy_sel = sel_x ? 1 : 0;
390 hns3_set_bit(req1->port_info, HNS3_FD_EPORT_SW_EN_B, 0);
391 req1->index = rte_cpu_to_le_32(loc);
392 req1->entry_vld = sel_x ? is_add : 0;
395 len = sizeof(req1->tcam_data);
396 memcpy(req1->tcam_data, key, len);
399 len = sizeof(req2->tcam_data);
400 memcpy(req2->tcam_data, key, len);
403 len = sizeof(req3->tcam_data);
404 memcpy(req3->tcam_data, key, len);
407 ret = hns3_cmd_send(hw, desc, FD_TCAM_CMD_NUM);
409 hns3_err(hw, "Config tcam key fail, ret=%d loc=%d add=%d",
414 static int hns3_fd_ad_config(struct hns3_hw *hw, int loc,
415 struct hns3_fd_ad_data *action)
417 struct hns3_fd_ad_config_cmd *req;
418 struct hns3_cmd_desc desc;
419 uint64_t ad_data = 0;
422 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_FD_AD_OP, false);
424 req = (struct hns3_fd_ad_config_cmd *)desc.data;
425 req->index = rte_cpu_to_le_32(loc);
426 req->stage = HNS3_FD_STAGE_1;
428 hns3_set_bit(ad_data, HNS3_FD_AD_WR_RULE_ID_B,
429 action->write_rule_id_to_bd);
430 hns3_set_field(ad_data, HNS3_FD_AD_RULE_ID_M, HNS3_FD_AD_RULE_ID_S,
432 if (action->nb_queues > 1) {
433 hns3_set_bit(ad_data, HNS3_FD_AD_QUEUE_REGION_EN_B, 1);
434 hns3_set_field(ad_data, HNS3_FD_AD_QUEUE_REGION_SIZE_M,
435 HNS3_FD_AD_QUEUE_REGION_SIZE_S,
436 rte_log2_u32(action->nb_queues));
438 /* set extend bit if counter_id is in [128 ~ 255] */
439 if (action->counter_id & BIT(HNS3_FD_AD_COUNTER_HIGH_BIT))
440 hns3_set_bit(ad_data, HNS3_FD_AD_COUNTER_HIGH_BIT_B, 1);
441 /* set extend bit if queue id > 1024 */
442 if (action->queue_id & BIT(HNS3_FD_AD_QUEUE_ID_HIGH_BIT))
443 hns3_set_bit(ad_data, HNS3_FD_AD_QUEUE_ID_HIGH_BIT_B, 1);
444 ad_data <<= HNS3_FD_AD_DATA_S;
445 hns3_set_bit(ad_data, HNS3_FD_AD_DROP_B, action->drop_packet);
446 if (action->nb_queues == 1)
447 hns3_set_bit(ad_data, HNS3_FD_AD_DIRECT_QID_B, 1);
448 hns3_set_field(ad_data, HNS3_FD_AD_QID_M, HNS3_FD_AD_QID_S,
450 hns3_set_bit(ad_data, HNS3_FD_AD_USE_COUNTER_B, action->use_counter);
451 hns3_set_field(ad_data, HNS3_FD_AD_COUNTER_NUM_M,
452 HNS3_FD_AD_COUNTER_NUM_S, action->counter_id);
453 hns3_set_bit(ad_data, HNS3_FD_AD_NXT_STEP_B, action->use_next_stage);
454 hns3_set_field(ad_data, HNS3_FD_AD_NXT_KEY_M, HNS3_FD_AD_NXT_KEY_S,
455 action->next_input_key);
457 req->ad_data = rte_cpu_to_le_64(ad_data);
458 ret = hns3_cmd_send(hw, &desc, 1);
460 hns3_err(hw, "Config fd ad fail, ret=%d loc=%d", ret, loc);
465 static inline void hns3_fd_convert_mac(uint8_t *key, uint8_t *mask,
466 uint8_t *mac_x, uint8_t *mac_y)
471 for (i = 0; i < RTE_ETHER_ADDR_LEN; i++) {
472 tmp = RTE_ETHER_ADDR_LEN - 1 - i;
473 calc_x(mac_x[tmp], key[i], mask[i]);
474 calc_y(mac_y[tmp], key[i], mask[i]);
478 static void hns3_fd_convert_int16(uint32_t tuple, struct hns3_fdir_rule *rule,
479 uint8_t *val_x, uint8_t *val_y)
488 key = rule->key_conf.spec.outer_src_port;
489 mask = rule->key_conf.mask.outer_src_port;
492 key = rule->key_conf.spec.tunnel_type;
493 mask = rule->key_conf.mask.tunnel_type;
496 key = rule->key_conf.spec.outer_ether_type;
497 mask = rule->key_conf.mask.outer_ether_type;
500 key = rule->key_conf.spec.src_port;
501 mask = rule->key_conf.mask.src_port;
504 key = rule->key_conf.spec.dst_port;
505 mask = rule->key_conf.mask.dst_port;
507 case INNER_VLAN_TAG1:
508 key = rule->key_conf.spec.vlan_tag1;
509 mask = rule->key_conf.mask.vlan_tag1;
511 case INNER_VLAN_TAG2:
512 key = rule->key_conf.spec.vlan_tag2;
513 mask = rule->key_conf.mask.vlan_tag2;
516 /* INNER_ETH_TYPE: */
517 key = rule->key_conf.spec.ether_type;
518 mask = rule->key_conf.mask.ether_type;
521 calc_x(tmp_x_s, key, mask);
522 calc_y(tmp_y_s, key, mask);
523 val_x[0] = rte_cpu_to_le_16(tmp_x_s) & 0xFF;
524 val_x[1] = rte_cpu_to_le_16(tmp_x_s) >> HNS3_BITS_PER_BYTE;
525 val_y[0] = rte_cpu_to_le_16(tmp_y_s) & 0xFF;
526 val_y[1] = rte_cpu_to_le_16(tmp_y_s) >> HNS3_BITS_PER_BYTE;
529 static inline void hns3_fd_convert_int32(uint32_t key, uint32_t mask,
530 uint8_t *val_x, uint8_t *val_y)
535 calc_x(tmp_x_l, key, mask);
536 calc_y(tmp_y_l, key, mask);
537 memcpy(val_x, &tmp_x_l, sizeof(tmp_x_l));
538 memcpy(val_y, &tmp_y_l, sizeof(tmp_y_l));
541 static bool hns3_fd_convert_tuple(struct hns3_hw *hw,
542 uint32_t tuple, uint8_t *key_x,
543 uint8_t *key_y, struct hns3_fdir_rule *rule)
545 struct hns3_fdir_key_conf *key_conf;
549 if ((rule->input_set & BIT(tuple)) == 0)
552 key_conf = &rule->key_conf;
555 hns3_fd_convert_mac(key_conf->spec.dst_mac,
556 key_conf->mask.dst_mac, key_x, key_y);
559 hns3_fd_convert_mac(key_conf->spec.src_mac,
560 key_conf->mask.src_mac, key_x, key_y);
567 case INNER_VLAN_TAG1:
568 case INNER_VLAN_TAG2:
570 hns3_fd_convert_int16(tuple, rule, key_x, key_y);
573 hns3_fd_convert_int32(key_conf->spec.src_ip[IP_ADDR_KEY_ID],
574 key_conf->mask.src_ip[IP_ADDR_KEY_ID],
578 hns3_fd_convert_int32(key_conf->spec.dst_ip[IP_ADDR_KEY_ID],
579 key_conf->mask.dst_ip[IP_ADDR_KEY_ID],
583 hns3_fd_convert_int32(key_conf->spec.sctp_tag,
584 key_conf->mask.sctp_tag, key_x, key_y);
587 for (i = 0; i < VNI_OR_TNI_LEN; i++) {
588 tmp = VNI_OR_TNI_LEN - 1 - i;
590 key_conf->spec.outer_tun_vni[i],
591 key_conf->mask.outer_tun_vni[i]);
593 key_conf->spec.outer_tun_vni[i],
594 key_conf->mask.outer_tun_vni[i]);
597 case OUTER_TUN_FLOW_ID:
598 calc_x(*key_x, key_conf->spec.outer_tun_flow_id,
599 key_conf->mask.outer_tun_flow_id);
600 calc_y(*key_y, key_conf->spec.outer_tun_flow_id,
601 key_conf->mask.outer_tun_flow_id);
604 calc_x(*key_x, key_conf->spec.ip_tos, key_conf->mask.ip_tos);
605 calc_y(*key_y, key_conf->spec.ip_tos, key_conf->mask.ip_tos);
608 calc_x(*key_x, key_conf->spec.outer_proto,
609 key_conf->mask.outer_proto);
610 calc_y(*key_y, key_conf->spec.outer_proto,
611 key_conf->mask.outer_proto);
614 calc_x(*key_x, key_conf->spec.ip_proto,
615 key_conf->mask.ip_proto);
616 calc_y(*key_y, key_conf->spec.ip_proto,
617 key_conf->mask.ip_proto);
620 hns3_warn(hw, "not support tuple of (%u)", tuple);
626 static uint32_t hns3_get_port_number(uint8_t pf_id, uint8_t vf_id)
628 uint32_t port_number = 0;
630 hns3_set_field(port_number, HNS3_PF_ID_M, HNS3_PF_ID_S, pf_id);
631 hns3_set_field(port_number, HNS3_VF_ID_M, HNS3_VF_ID_S, vf_id);
632 hns3_set_bit(port_number, HNS3_PORT_TYPE_B, HOST_PORT);
637 static void hns3_fd_convert_meta_data(struct hns3_fd_key_cfg *cfg,
639 struct hns3_fdir_rule *rule,
640 uint8_t *key_x, uint8_t *key_y)
642 uint16_t meta_data = 0;
643 uint32_t port_number;
651 for (i = 0; i < MAX_META_DATA; i++) {
652 if ((cfg->meta_data_active & BIT(i)) == 0)
655 tuple_size = meta_data_key_info[i].key_length;
656 if (i == TUNNEL_PACKET) {
657 hns3_set_bit(meta_data, cur_pos,
658 rule->key_conf.spec.tunnel_type ? 1 : 0);
659 cur_pos += tuple_size;
660 } else if (i == VLAN_NUMBER) {
663 if (rule->key_conf.spec.tunnel_type == 0)
664 vlan_num = rule->key_conf.vlan_num;
666 vlan_num = rule->key_conf.outer_vlan_num;
668 vlan_tag = HNS3_VLAN_TAG_TYPE_TAG1;
669 else if (vlan_num == VLAN_TAG_NUM_MAX)
670 vlan_tag = HNS3_VLAN_TAG_TYPE_TAG1_2;
672 vlan_tag = HNS3_VLAN_TAG_TYPE_NONE;
673 hns3_set_field(meta_data,
674 GENMASK(cur_pos + tuple_size,
675 cur_pos), cur_pos, vlan_tag);
676 cur_pos += tuple_size;
677 } else if (i == DST_VPORT) {
678 port_number = hns3_get_port_number(0, vf_id);
679 hns3_set_field(meta_data,
680 GENMASK(cur_pos + tuple_size, cur_pos),
681 cur_pos, port_number);
682 cur_pos += tuple_size;
686 calc_x(tmp_x, meta_data, 0xFFFF);
687 calc_y(tmp_y, meta_data, 0xFFFF);
688 shift_bits = sizeof(meta_data) * HNS3_BITS_PER_BYTE - cur_pos;
690 tmp_x = rte_cpu_to_le_32(tmp_x << shift_bits);
691 tmp_y = rte_cpu_to_le_32(tmp_y << shift_bits);
692 key_x[0] = tmp_x & 0xFF;
693 key_x[1] = (tmp_x >> HNS3_BITS_PER_BYTE) & 0xFF;
694 key_y[0] = tmp_y & 0xFF;
695 key_y[1] = (tmp_y >> HNS3_BITS_PER_BYTE) & 0xFF;
698 /* A complete key is combined with meta data key and tuple key.
699 * Meta data key is stored at the MSB region, and tuple key is stored at
700 * the LSB region, unused bits will be filled 0.
702 static int hns3_config_key(struct hns3_adapter *hns,
703 struct hns3_fdir_rule *rule)
705 struct hns3_pf *pf = &hns->pf;
706 struct hns3_hw *hw = &hns->hw;
707 struct hns3_fd_key_cfg *key_cfg;
710 uint8_t key_x[MAX_KEY_BYTES] __rte_aligned(4);
711 uint8_t key_y[MAX_KEY_BYTES] __rte_aligned(4);
712 uint8_t vf_id = rule->vf_id;
713 uint8_t meta_data_region;
718 memset(key_x, 0, sizeof(key_x));
719 memset(key_y, 0, sizeof(key_y));
723 key_cfg = &pf->fdir.fd_cfg.key_cfg[HNS3_FD_STAGE_1];
724 for (i = 0; i < MAX_TUPLE; i++) {
727 tuple_size = tuple_key_info[i].key_length / HNS3_BITS_PER_BYTE;
728 if (key_cfg->tuple_active & BIT(i)) {
729 tuple_valid = hns3_fd_convert_tuple(hw, i, cur_key_x,
732 cur_key_x += tuple_size;
733 cur_key_y += tuple_size;
738 meta_data_region = pf->fdir.fd_cfg.max_key_length / HNS3_BITS_PER_BYTE -
739 MAX_META_DATA_LENGTH / HNS3_BITS_PER_BYTE;
741 hns3_fd_convert_meta_data(key_cfg, vf_id, rule,
742 key_x + meta_data_region,
743 key_y + meta_data_region);
745 ret = hns3_fd_tcam_config(hw, false, rule->location, key_y, true);
747 hns3_err(hw, "Config fd key_y fail, loc=%u, ret=%d",
748 rule->queue_id, ret);
752 ret = hns3_fd_tcam_config(hw, true, rule->location, key_x, true);
754 hns3_err(hw, "Config fd key_x fail, loc=%u, ret=%d",
755 rule->queue_id, ret);
759 static int hns3_config_action(struct hns3_hw *hw, struct hns3_fdir_rule *rule)
761 struct hns3_fd_ad_data ad_data;
763 ad_data.ad_id = rule->location;
765 if (rule->action == HNS3_FD_ACTION_DROP_PACKET) {
766 ad_data.drop_packet = true;
767 ad_data.queue_id = 0;
768 ad_data.nb_queues = 0;
770 ad_data.drop_packet = false;
771 ad_data.queue_id = rule->queue_id;
772 ad_data.nb_queues = rule->nb_queues;
775 if (unlikely(rule->flags & HNS3_RULE_FLAG_COUNTER)) {
776 ad_data.use_counter = true;
777 ad_data.counter_id = rule->act_cnt.id;
779 ad_data.use_counter = false;
780 ad_data.counter_id = 0;
783 if (unlikely(rule->flags & HNS3_RULE_FLAG_FDID))
784 ad_data.rule_id = rule->fd_id;
786 ad_data.rule_id = rule->location;
788 ad_data.use_next_stage = false;
789 ad_data.next_input_key = 0;
791 ad_data.write_rule_id_to_bd = true;
793 return hns3_fd_ad_config(hw, ad_data.ad_id, &ad_data);
796 static int hns3_fd_clear_all_rules(struct hns3_hw *hw, uint32_t rule_num)
801 for (i = 0; i < rule_num; i++) {
802 ret = hns3_fd_tcam_config(hw, true, i, NULL, false);
810 int hns3_fdir_filter_init(struct hns3_adapter *hns)
812 struct hns3_pf *pf = &hns->pf;
813 struct hns3_fdir_info *fdir_info = &pf->fdir;
814 uint32_t rule_num = fdir_info->fd_cfg.rule_num[HNS3_FD_STAGE_1];
815 char fdir_hash_name[RTE_HASH_NAMESIZE];
816 struct rte_hash_parameters fdir_hash_params = {
817 .name = fdir_hash_name,
819 .key_len = sizeof(struct hns3_fdir_key_conf),
820 .hash_func = rte_hash_crc,
821 .hash_func_init_val = 0,
825 ret = hns3_fd_clear_all_rules(&hns->hw, rule_num);
827 PMD_INIT_LOG(ERR, "Clear all fd rules fail! ret = %d", ret);
831 fdir_hash_params.socket_id = rte_socket_id();
832 TAILQ_INIT(&fdir_info->fdir_list);
833 snprintf(fdir_hash_name, RTE_HASH_NAMESIZE, "%s", hns->hw.data->name);
834 fdir_info->hash_handle = rte_hash_create(&fdir_hash_params);
835 if (fdir_info->hash_handle == NULL) {
836 PMD_INIT_LOG(ERR, "Create FDIR hash handle fail!");
839 fdir_info->hash_map = rte_zmalloc("hns3 FDIR hash",
841 sizeof(struct hns3_fdir_rule_ele *),
843 if (fdir_info->hash_map == NULL) {
844 PMD_INIT_LOG(ERR, "Allocate memory for FDIR hash map fail!");
845 rte_hash_free(fdir_info->hash_handle);
852 void hns3_fdir_filter_uninit(struct hns3_adapter *hns)
854 struct hns3_pf *pf = &hns->pf;
855 struct hns3_fdir_info *fdir_info = &pf->fdir;
856 struct hns3_fdir_rule_ele *fdir_filter;
858 if (fdir_info->hash_map) {
859 rte_free(fdir_info->hash_map);
860 fdir_info->hash_map = NULL;
862 if (fdir_info->hash_handle) {
863 rte_hash_free(fdir_info->hash_handle);
864 fdir_info->hash_handle = NULL;
867 fdir_filter = TAILQ_FIRST(&fdir_info->fdir_list);
868 while (fdir_filter) {
869 TAILQ_REMOVE(&fdir_info->fdir_list, fdir_filter, entries);
870 hns3_fd_tcam_config(&hns->hw, true,
871 fdir_filter->fdir_conf.location, NULL,
873 rte_free(fdir_filter);
874 fdir_filter = TAILQ_FIRST(&fdir_info->fdir_list);
879 * Find a key in the hash table.
881 * - Zero and positive values are key location.
882 * - -EINVAL if the parameters are invalid.
883 * - -ENOENT if the key is not found.
885 static int hns3_fdir_filter_lookup(struct hns3_fdir_info *fdir_info,
886 struct hns3_fdir_key_conf *key)
891 sig = rte_hash_crc(key, sizeof(*key), 0);
892 ret = rte_hash_lookup_with_hash(fdir_info->hash_handle, key, sig);
897 static int hns3_insert_fdir_filter(struct hns3_hw *hw,
898 struct hns3_fdir_info *fdir_info,
899 struct hns3_fdir_rule_ele *fdir_filter)
901 struct hns3_fdir_key_conf *key;
905 key = &fdir_filter->fdir_conf.key_conf;
906 sig = rte_hash_crc(key, sizeof(*key), 0);
907 ret = rte_hash_add_key_with_hash(fdir_info->hash_handle, key, sig);
909 hns3_err(hw, "Hash table full? err:%d(%s)!", ret,
914 fdir_info->hash_map[ret] = fdir_filter;
915 TAILQ_INSERT_TAIL(&fdir_info->fdir_list, fdir_filter, entries);
920 static int hns3_remove_fdir_filter(struct hns3_hw *hw,
921 struct hns3_fdir_info *fdir_info,
922 struct hns3_fdir_key_conf *key)
924 struct hns3_fdir_rule_ele *fdir_filter;
928 sig = rte_hash_crc(key, sizeof(*key), 0);
929 ret = rte_hash_del_key_with_hash(fdir_info->hash_handle, key, sig);
931 hns3_err(hw, "Delete hash key fail ret=%d", ret);
935 fdir_filter = fdir_info->hash_map[ret];
936 fdir_info->hash_map[ret] = NULL;
937 TAILQ_REMOVE(&fdir_info->fdir_list, fdir_filter, entries);
939 rte_free(fdir_filter);
944 int hns3_fdir_filter_program(struct hns3_adapter *hns,
945 struct hns3_fdir_rule *rule, bool del)
947 struct hns3_pf *pf = &hns->pf;
948 struct hns3_fdir_info *fdir_info = &pf->fdir;
949 struct hns3_fdir_rule_ele *node;
950 struct hns3_hw *hw = &hns->hw;
954 ret = hns3_fd_tcam_config(hw, true, rule->location, NULL,
957 hns3_err(hw, "Failed to delete fdir: %u src_ip:%x "
958 "dst_ip:%x src_port:%u dst_port:%u ret = %d",
960 rule->key_conf.spec.src_ip[IP_ADDR_KEY_ID],
961 rule->key_conf.spec.dst_ip[IP_ADDR_KEY_ID],
962 rule->key_conf.spec.src_port,
963 rule->key_conf.spec.dst_port, ret);
965 hns3_remove_fdir_filter(hw, fdir_info, &rule->key_conf);
970 ret = hns3_fdir_filter_lookup(fdir_info, &rule->key_conf);
972 hns3_err(hw, "Conflict with existing fdir loc: %d", ret);
976 node = rte_zmalloc("hns3 fdir rule", sizeof(struct hns3_fdir_rule_ele),
979 hns3_err(hw, "Failed to allocate fdir_rule memory");
983 rte_memcpy(&node->fdir_conf, rule, sizeof(struct hns3_fdir_rule));
984 ret = hns3_insert_fdir_filter(hw, fdir_info, node);
989 rule->location = ret;
990 node->fdir_conf.location = ret;
992 ret = hns3_config_action(hw, rule);
994 ret = hns3_config_key(hns, rule);
996 hns3_err(hw, "Failed to config fdir: %u src_ip:%x dst_ip:%x "
997 "src_port:%u dst_port:%u ret = %d",
999 rule->key_conf.spec.src_ip[IP_ADDR_KEY_ID],
1000 rule->key_conf.spec.dst_ip[IP_ADDR_KEY_ID],
1001 rule->key_conf.spec.src_port,
1002 rule->key_conf.spec.dst_port, ret);
1003 (void)hns3_remove_fdir_filter(hw, fdir_info, &rule->key_conf);
1009 /* remove all the flow director filters */
1010 int hns3_clear_all_fdir_filter(struct hns3_adapter *hns)
1012 struct hns3_pf *pf = &hns->pf;
1013 struct hns3_fdir_info *fdir_info = &pf->fdir;
1014 struct hns3_fdir_rule_ele *fdir_filter;
1015 struct hns3_hw *hw = &hns->hw;
1018 /* flush flow director */
1019 rte_hash_reset(fdir_info->hash_handle);
1021 fdir_filter = TAILQ_FIRST(&fdir_info->fdir_list);
1022 while (fdir_filter) {
1023 TAILQ_REMOVE(&fdir_info->fdir_list, fdir_filter, entries);
1024 ret += hns3_fd_tcam_config(hw, true,
1025 fdir_filter->fdir_conf.location,
1027 rte_free(fdir_filter);
1028 fdir_filter = TAILQ_FIRST(&fdir_info->fdir_list);
1032 hns3_err(hw, "Fail to delete FDIR filter, ret = %d", ret);
1038 int hns3_restore_all_fdir_filter(struct hns3_adapter *hns)
1040 struct hns3_pf *pf = &hns->pf;
1041 struct hns3_fdir_info *fdir_info = &pf->fdir;
1042 struct hns3_fdir_rule_ele *fdir_filter;
1043 struct hns3_hw *hw = &hns->hw;
1048 * This API is called in the reset recovery process, the parent function
1049 * must hold hw->lock.
1050 * There maybe deadlock if acquire hw->flows_lock directly because rte
1051 * flow driver ops first acquire hw->flows_lock and then may acquire
1053 * So here first release the hw->lock and then acquire the
1054 * hw->flows_lock to avoid deadlock.
1056 rte_spinlock_unlock(&hw->lock);
1057 pthread_mutex_lock(&hw->flows_lock);
1058 TAILQ_FOREACH(fdir_filter, &fdir_info->fdir_list, entries) {
1059 ret = hns3_config_action(hw, &fdir_filter->fdir_conf);
1061 ret = hns3_config_key(hns, &fdir_filter->fdir_conf);
1068 pthread_mutex_unlock(&hw->flows_lock);
1069 rte_spinlock_lock(&hw->lock);
1072 hns3_err(hw, "Fail to restore FDIR filter, ret = %d", ret);
1078 int hns3_get_count(struct hns3_hw *hw, uint32_t id, uint64_t *value)
1080 struct hns3_fd_get_cnt_cmd *req;
1081 struct hns3_cmd_desc desc;
1084 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_FD_COUNTER_OP, true);
1086 req = (struct hns3_fd_get_cnt_cmd *)desc.data;
1087 req->stage = HNS3_FD_STAGE_1;
1088 req->index = rte_cpu_to_le_32(id);
1090 ret = hns3_cmd_send(hw, &desc, 1);
1092 hns3_err(hw, "Read counter fail, ret=%d", ret);
1096 *value = req->value;