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
56 HNS3_FD_MODE_DEPTH_2K_WIDTH_400B_STAGE_1,
57 HNS3_FD_MODE_DEPTH_1K_WIDTH_400B_STAGE_2,
58 HNS3_FD_MODE_DEPTH_4K_WIDTH_200B_STAGE_1,
59 HNS3_FD_MODE_DEPTH_2K_WIDTH_200B_STAGE_2,
62 enum HNS3_FD_KEY_TYPE {
63 HNS3_FD_KEY_BASE_ON_PTYPE,
64 HNS3_FD_KEY_BASE_ON_TUPLE,
67 enum HNS3_FD_META_DATA {
84 static const struct key_info meta_data_key_info[] = {
95 static const struct key_info tuple_key_info[] = {
98 {OUTER_VLAN_TAG_FST, 16},
99 {OUTER_VLAN_TAG_SEC, 16},
100 {OUTER_ETH_TYPE, 16},
107 {OUTER_SRC_PORT, 16},
108 {OUTER_DST_PORT, 16},
111 {OUTER_TUN_FLOW_ID, 8},
114 {INNER_VLAN_TAG1, 16},
115 {INNER_VLAN_TAG2, 16},
116 {INNER_ETH_TYPE, 16},
123 {INNER_SRC_PORT, 16},
124 {INNER_DST_PORT, 16},
125 {INNER_SCTP_TAG, 32},
128 #define MAX_KEY_LENGTH 400
129 #define MAX_200B_KEY_LENGTH 200
130 #define MAX_META_DATA_LENGTH 16
131 #define MAX_KEY_DWORDS DIV_ROUND_UP(MAX_KEY_LENGTH / HNS3_BITS_PER_BYTE, 4)
132 #define MAX_KEY_BYTES (MAX_KEY_DWORDS * 4)
134 enum HNS3_FD_PACKET_TYPE {
139 /* For each bit of TCAM entry, it uses a pair of 'x' and
140 * 'y' to indicate which value to match, like below:
141 * ----------------------------------
142 * | bit x | bit y | search value |
143 * ----------------------------------
144 * | 0 | 0 | always hit |
145 * ----------------------------------
146 * | 1 | 0 | match '0' |
147 * ----------------------------------
148 * | 0 | 1 | match '1' |
149 * ----------------------------------
150 * | 1 | 1 | invalid |
151 * ----------------------------------
152 * Then for input key(k) and mask(v), we can calculate the value by
157 #define calc_x(x, k, v) ((x) = (~(k) & (v)))
158 #define calc_y(y, k, v) ((y) = ((k) & (v)))
160 struct hns3_fd_tcam_config_1_cmd {
168 uint8_t tcam_data[8];
171 struct hns3_fd_tcam_config_2_cmd {
172 uint8_t tcam_data[24];
175 struct hns3_fd_tcam_config_3_cmd {
176 uint8_t tcam_data[20];
180 struct hns3_get_fd_mode_cmd {
186 struct hns3_get_fd_allocation_cmd {
187 rte_le32_t stage1_entry_num;
188 rte_le32_t stage2_entry_num;
189 rte_le16_t stage1_counter_num;
190 rte_le16_t stage2_counter_num;
194 struct hns3_set_fd_key_config_cmd {
197 uint8_t inner_sipv6_word_en;
198 uint8_t inner_dipv6_word_en;
199 uint8_t outer_sipv6_word_en;
200 uint8_t outer_dipv6_word_en;
202 rte_le32_t tuple_mask;
203 rte_le32_t meta_data_mask;
207 struct hns3_fd_ad_config_cmd {
215 struct hns3_fd_get_cnt_cmd {
224 static int hns3_get_fd_mode(struct hns3_hw *hw, uint8_t *fd_mode)
226 struct hns3_get_fd_mode_cmd *req;
227 struct hns3_cmd_desc desc;
230 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_FD_MODE_CTRL, true);
232 req = (struct hns3_get_fd_mode_cmd *)desc.data;
234 ret = hns3_cmd_send(hw, &desc, 1);
236 hns3_err(hw, "Get fd mode fail, ret=%d", ret);
240 *fd_mode = req->mode;
245 static int hns3_get_fd_allocation(struct hns3_hw *hw,
246 uint32_t *stage1_entry_num,
247 uint32_t *stage2_entry_num,
248 uint16_t *stage1_counter_num,
249 uint16_t *stage2_counter_num)
251 struct hns3_get_fd_allocation_cmd *req;
252 struct hns3_cmd_desc desc;
255 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_FD_GET_ALLOCATION, true);
257 req = (struct hns3_get_fd_allocation_cmd *)desc.data;
259 ret = hns3_cmd_send(hw, &desc, 1);
261 hns3_err(hw, "Query fd allocation fail, ret=%d", ret);
265 *stage1_entry_num = rte_le_to_cpu_32(req->stage1_entry_num);
266 *stage2_entry_num = rte_le_to_cpu_32(req->stage2_entry_num);
267 *stage1_counter_num = rte_le_to_cpu_16(req->stage1_counter_num);
268 *stage2_counter_num = rte_le_to_cpu_16(req->stage2_counter_num);
273 static int hns3_set_fd_key_config(struct hns3_adapter *hns)
275 struct hns3_set_fd_key_config_cmd *req;
276 struct hns3_fd_key_cfg *key_cfg;
277 struct hns3_pf *pf = &hns->pf;
278 struct hns3_hw *hw = &hns->hw;
279 struct hns3_cmd_desc desc;
282 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_FD_KEY_CONFIG, false);
284 req = (struct hns3_set_fd_key_config_cmd *)desc.data;
285 key_cfg = &pf->fdir.fd_cfg.key_cfg[HNS3_FD_STAGE_1];
286 req->stage = HNS3_FD_STAGE_1;
287 req->key_select = key_cfg->key_sel;
288 req->inner_sipv6_word_en = key_cfg->inner_sipv6_word_en;
289 req->inner_dipv6_word_en = key_cfg->inner_dipv6_word_en;
290 req->outer_sipv6_word_en = key_cfg->outer_sipv6_word_en;
291 req->outer_dipv6_word_en = key_cfg->outer_dipv6_word_en;
292 req->tuple_mask = rte_cpu_to_le_32(~key_cfg->tuple_active);
293 req->meta_data_mask = rte_cpu_to_le_32(~key_cfg->meta_data_active);
295 ret = hns3_cmd_send(hw, &desc, 1);
297 hns3_err(hw, "Set fd key fail, ret=%d", ret);
302 int hns3_init_fd_config(struct hns3_adapter *hns)
304 struct hns3_pf *pf = &hns->pf;
305 struct hns3_hw *hw = &hns->hw;
306 struct hns3_fd_key_cfg *key_cfg;
309 ret = hns3_get_fd_mode(hw, &pf->fdir.fd_cfg.fd_mode);
313 switch (pf->fdir.fd_cfg.fd_mode) {
314 case HNS3_FD_MODE_DEPTH_2K_WIDTH_400B_STAGE_1:
315 pf->fdir.fd_cfg.max_key_length = MAX_KEY_LENGTH;
317 case HNS3_FD_MODE_DEPTH_4K_WIDTH_200B_STAGE_1:
318 pf->fdir.fd_cfg.max_key_length = MAX_200B_KEY_LENGTH;
319 hns3_warn(hw, "Unsupported tunnel filter in 4K*200Bit");
322 hns3_err(hw, "Unsupported flow director mode %d",
323 pf->fdir.fd_cfg.fd_mode);
327 key_cfg = &pf->fdir.fd_cfg.key_cfg[HNS3_FD_STAGE_1];
328 key_cfg->key_sel = HNS3_FD_KEY_BASE_ON_TUPLE;
329 key_cfg->inner_sipv6_word_en = IPV6_ADDR_WORD_MASK;
330 key_cfg->inner_dipv6_word_en = IPV6_ADDR_WORD_MASK;
331 key_cfg->outer_sipv6_word_en = 0;
332 key_cfg->outer_dipv6_word_en = 0;
334 key_cfg->tuple_active = BIT(INNER_VLAN_TAG1) | BIT(INNER_ETH_TYPE) |
335 BIT(INNER_IP_PROTO) | BIT(INNER_IP_TOS) |
336 BIT(INNER_SRC_IP) | BIT(INNER_DST_IP) |
337 BIT(INNER_SRC_PORT) | BIT(INNER_DST_PORT);
339 /* If use max 400bit key, we can support tuples for ether type */
340 if (pf->fdir.fd_cfg.max_key_length == MAX_KEY_LENGTH) {
341 key_cfg->tuple_active |=
342 BIT(INNER_DST_MAC) | BIT(INNER_SRC_MAC) |
343 BIT(OUTER_SRC_PORT) | BIT(INNER_SCTP_TAG) |
344 BIT(OUTER_DST_PORT) | BIT(INNER_VLAN_TAG2) |
345 BIT(OUTER_TUN_VNI) | BIT(OUTER_TUN_FLOW_ID) |
346 BIT(OUTER_ETH_TYPE) | BIT(OUTER_IP_PROTO);
349 /* roce_type is used to filter roce frames
350 * dst_vport is used to specify the rule
352 key_cfg->meta_data_active = BIT(DST_VPORT) | BIT(TUNNEL_PACKET) |
355 ret = hns3_get_fd_allocation(hw,
356 &pf->fdir.fd_cfg.rule_num[HNS3_FD_STAGE_1],
357 &pf->fdir.fd_cfg.rule_num[HNS3_FD_STAGE_2],
358 &pf->fdir.fd_cfg.cnt_num[HNS3_FD_STAGE_1],
359 &pf->fdir.fd_cfg.cnt_num[HNS3_FD_STAGE_2]);
363 return hns3_set_fd_key_config(hns);
366 static int hns3_fd_tcam_config(struct hns3_hw *hw, bool sel_x, int loc,
367 uint8_t *key, bool is_add)
369 #define FD_TCAM_CMD_NUM 3
370 struct hns3_fd_tcam_config_1_cmd *req1;
371 struct hns3_fd_tcam_config_2_cmd *req2;
372 struct hns3_fd_tcam_config_3_cmd *req3;
373 struct hns3_cmd_desc desc[FD_TCAM_CMD_NUM];
377 hns3_cmd_setup_basic_desc(&desc[0], HNS3_OPC_FD_TCAM_OP, false);
378 desc[0].flag |= rte_cpu_to_le_16(HNS3_CMD_FLAG_NEXT);
379 hns3_cmd_setup_basic_desc(&desc[1], HNS3_OPC_FD_TCAM_OP, false);
380 desc[1].flag |= rte_cpu_to_le_16(HNS3_CMD_FLAG_NEXT);
381 hns3_cmd_setup_basic_desc(&desc[2], HNS3_OPC_FD_TCAM_OP, false);
383 req1 = (struct hns3_fd_tcam_config_1_cmd *)desc[0].data;
384 req2 = (struct hns3_fd_tcam_config_2_cmd *)desc[1].data;
385 req3 = (struct hns3_fd_tcam_config_3_cmd *)desc[2].data;
387 req1->stage = HNS3_FD_STAGE_1;
388 req1->xy_sel = sel_x ? 1 : 0;
389 hns3_set_bit(req1->port_info, HNS3_FD_EPORT_SW_EN_B, 0);
390 req1->index = rte_cpu_to_le_32(loc);
391 req1->entry_vld = sel_x ? is_add : 0;
394 len = sizeof(req1->tcam_data);
395 memcpy(req1->tcam_data, key, len);
398 len = sizeof(req2->tcam_data);
399 memcpy(req2->tcam_data, key, len);
402 len = sizeof(req3->tcam_data);
403 memcpy(req3->tcam_data, key, len);
406 ret = hns3_cmd_send(hw, desc, FD_TCAM_CMD_NUM);
408 hns3_err(hw, "Config tcam key fail, ret=%d loc=%d add=%d",
413 static int hns3_fd_ad_config(struct hns3_hw *hw, int loc,
414 struct hns3_fd_ad_data *action)
416 struct hns3_fd_ad_config_cmd *req;
417 struct hns3_cmd_desc desc;
418 uint64_t ad_data = 0;
421 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_FD_AD_OP, false);
423 req = (struct hns3_fd_ad_config_cmd *)desc.data;
424 req->index = rte_cpu_to_le_32(loc);
425 req->stage = HNS3_FD_STAGE_1;
427 hns3_set_bit(ad_data, HNS3_FD_AD_WR_RULE_ID_B,
428 action->write_rule_id_to_bd);
429 hns3_set_field(ad_data, HNS3_FD_AD_RULE_ID_M, HNS3_FD_AD_RULE_ID_S,
431 if (action->nb_queues > 1) {
432 hns3_set_bit(ad_data, HNS3_FD_AD_QUEUE_REGION_EN_B, 1);
433 hns3_set_field(ad_data, HNS3_FD_AD_QUEUE_REGION_SIZE_M,
434 HNS3_FD_AD_QUEUE_REGION_SIZE_S,
435 rte_log2_u32(action->nb_queues));
437 /* set extend bit if counter_id is in [128 ~ 255] */
438 if (action->counter_id & BIT(HNS3_FD_AD_COUNTER_HIGH_BIT))
439 hns3_set_bit(ad_data, HNS3_FD_AD_COUNTER_HIGH_BIT_B, 1);
440 ad_data <<= HNS3_FD_AD_DATA_S;
441 hns3_set_bit(ad_data, HNS3_FD_AD_DROP_B, action->drop_packet);
442 if (action->nb_queues == 1)
443 hns3_set_bit(ad_data, HNS3_FD_AD_DIRECT_QID_B, 1);
444 hns3_set_field(ad_data, HNS3_FD_AD_QID_M, HNS3_FD_AD_QID_S,
446 hns3_set_bit(ad_data, HNS3_FD_AD_USE_COUNTER_B, action->use_counter);
447 hns3_set_field(ad_data, HNS3_FD_AD_COUNTER_NUM_M,
448 HNS3_FD_AD_COUNTER_NUM_S, action->counter_id);
449 hns3_set_bit(ad_data, HNS3_FD_AD_NXT_STEP_B, action->use_next_stage);
450 hns3_set_field(ad_data, HNS3_FD_AD_NXT_KEY_M, HNS3_FD_AD_NXT_KEY_S,
451 action->next_input_key);
453 req->ad_data = rte_cpu_to_le_64(ad_data);
454 ret = hns3_cmd_send(hw, &desc, 1);
456 hns3_err(hw, "Config fd ad fail, ret=%d loc=%d", ret, loc);
461 static inline void hns3_fd_convert_mac(uint8_t *key, uint8_t *mask,
462 uint8_t *mac_x, uint8_t *mac_y)
467 for (i = 0; i < RTE_ETHER_ADDR_LEN; i++) {
468 tmp = RTE_ETHER_ADDR_LEN - 1 - i;
469 calc_x(mac_x[tmp], key[i], mask[i]);
470 calc_y(mac_y[tmp], key[i], mask[i]);
474 static void hns3_fd_convert_int16(uint32_t tuple, struct hns3_fdir_rule *rule,
475 uint8_t *val_x, uint8_t *val_y)
484 key = rule->key_conf.spec.outer_src_port;
485 mask = rule->key_conf.mask.outer_src_port;
488 key = rule->key_conf.spec.tunnel_type;
489 mask = rule->key_conf.mask.tunnel_type;
492 key = rule->key_conf.spec.outer_ether_type;
493 mask = rule->key_conf.mask.outer_ether_type;
496 key = rule->key_conf.spec.src_port;
497 mask = rule->key_conf.mask.src_port;
500 key = rule->key_conf.spec.dst_port;
501 mask = rule->key_conf.mask.dst_port;
503 case INNER_VLAN_TAG1:
504 key = rule->key_conf.spec.vlan_tag1;
505 mask = rule->key_conf.mask.vlan_tag1;
507 case INNER_VLAN_TAG2:
508 key = rule->key_conf.spec.vlan_tag2;
509 mask = rule->key_conf.mask.vlan_tag2;
512 /* INNER_ETH_TYPE: */
513 key = rule->key_conf.spec.ether_type;
514 mask = rule->key_conf.mask.ether_type;
517 calc_x(tmp_x_s, key, mask);
518 calc_y(tmp_y_s, key, mask);
519 val_x[0] = rte_cpu_to_le_16(tmp_x_s) & 0xFF;
520 val_x[1] = rte_cpu_to_le_16(tmp_x_s) >> HNS3_BITS_PER_BYTE;
521 val_y[0] = rte_cpu_to_le_16(tmp_y_s) & 0xFF;
522 val_y[1] = rte_cpu_to_le_16(tmp_y_s) >> HNS3_BITS_PER_BYTE;
525 static inline void hns3_fd_convert_int32(uint32_t key, uint32_t mask,
526 uint8_t *val_x, uint8_t *val_y)
531 calc_x(tmp_x_l, key, mask);
532 calc_y(tmp_y_l, key, mask);
533 memcpy(val_x, &tmp_x_l, sizeof(tmp_x_l));
534 memcpy(val_y, &tmp_y_l, sizeof(tmp_y_l));
537 static bool hns3_fd_convert_tuple(struct hns3_hw *hw,
538 uint32_t tuple, uint8_t *key_x,
539 uint8_t *key_y, struct hns3_fdir_rule *rule)
541 struct hns3_fdir_key_conf *key_conf;
545 if ((rule->input_set & BIT(tuple)) == 0)
548 key_conf = &rule->key_conf;
551 hns3_fd_convert_mac(key_conf->spec.dst_mac,
552 key_conf->mask.dst_mac, key_x, key_y);
555 hns3_fd_convert_mac(key_conf->spec.src_mac,
556 key_conf->mask.src_mac, key_x, key_y);
563 case INNER_VLAN_TAG1:
564 case INNER_VLAN_TAG2:
566 hns3_fd_convert_int16(tuple, rule, key_x, key_y);
569 hns3_fd_convert_int32(key_conf->spec.src_ip[IP_ADDR_KEY_ID],
570 key_conf->mask.src_ip[IP_ADDR_KEY_ID],
574 hns3_fd_convert_int32(key_conf->spec.dst_ip[IP_ADDR_KEY_ID],
575 key_conf->mask.dst_ip[IP_ADDR_KEY_ID],
579 hns3_fd_convert_int32(key_conf->spec.sctp_tag,
580 key_conf->mask.sctp_tag, key_x, key_y);
583 for (i = 0; i < VNI_OR_TNI_LEN; i++) {
584 tmp = VNI_OR_TNI_LEN - 1 - i;
586 key_conf->spec.outer_tun_vni[i],
587 key_conf->mask.outer_tun_vni[i]);
589 key_conf->spec.outer_tun_vni[i],
590 key_conf->mask.outer_tun_vni[i]);
593 case OUTER_TUN_FLOW_ID:
594 calc_x(*key_x, key_conf->spec.outer_tun_flow_id,
595 key_conf->mask.outer_tun_flow_id);
596 calc_y(*key_y, key_conf->spec.outer_tun_flow_id,
597 key_conf->mask.outer_tun_flow_id);
600 calc_x(*key_x, key_conf->spec.ip_tos, key_conf->mask.ip_tos);
601 calc_y(*key_y, key_conf->spec.ip_tos, key_conf->mask.ip_tos);
604 calc_x(*key_x, key_conf->spec.outer_proto,
605 key_conf->mask.outer_proto);
606 calc_y(*key_y, key_conf->spec.outer_proto,
607 key_conf->mask.outer_proto);
610 calc_x(*key_x, key_conf->spec.ip_proto,
611 key_conf->mask.ip_proto);
612 calc_y(*key_y, key_conf->spec.ip_proto,
613 key_conf->mask.ip_proto);
616 hns3_warn(hw, "not support tuple of (%d)", tuple);
622 static uint32_t hns3_get_port_number(uint8_t pf_id, uint8_t vf_id)
624 uint32_t port_number = 0;
626 hns3_set_field(port_number, HNS3_PF_ID_M, HNS3_PF_ID_S, pf_id);
627 hns3_set_field(port_number, HNS3_VF_ID_M, HNS3_VF_ID_S, vf_id);
628 hns3_set_bit(port_number, HNS3_PORT_TYPE_B, HOST_PORT);
633 static void hns3_fd_convert_meta_data(struct hns3_fd_key_cfg *cfg,
635 struct hns3_fdir_rule *rule,
636 uint8_t *key_x, uint8_t *key_y)
638 uint16_t meta_data = 0;
639 uint32_t port_number;
647 for (i = 0; i < MAX_META_DATA; i++) {
648 if ((cfg->meta_data_active & BIT(i)) == 0)
651 tuple_size = meta_data_key_info[i].key_length;
652 if (i == TUNNEL_PACKET) {
653 hns3_set_bit(meta_data, cur_pos,
654 rule->key_conf.spec.tunnel_type ? 1 : 0);
655 cur_pos += tuple_size;
656 } else if (i == VLAN_NUMBER) {
659 if (rule->key_conf.spec.tunnel_type == 0)
660 vlan_num = rule->key_conf.vlan_num;
662 vlan_num = rule->key_conf.outer_vlan_num;
664 vlan_tag = HNS3_VLAN_TAG_TYPE_TAG1;
665 else if (vlan_num == VLAN_TAG_NUM_MAX)
666 vlan_tag = HNS3_VLAN_TAG_TYPE_TAG1_2;
668 vlan_tag = HNS3_VLAN_TAG_TYPE_NONE;
669 hns3_set_field(meta_data,
670 GENMASK(cur_pos + tuple_size,
671 cur_pos), cur_pos, vlan_tag);
672 cur_pos += tuple_size;
673 } else if (i == DST_VPORT) {
674 port_number = hns3_get_port_number(0, vf_id);
675 hns3_set_field(meta_data,
676 GENMASK(cur_pos + tuple_size, cur_pos),
677 cur_pos, port_number);
678 cur_pos += tuple_size;
682 calc_x(tmp_x, meta_data, 0xFFFF);
683 calc_y(tmp_y, meta_data, 0xFFFF);
684 shift_bits = sizeof(meta_data) * HNS3_BITS_PER_BYTE - cur_pos;
686 tmp_x = rte_cpu_to_le_32(tmp_x << shift_bits);
687 tmp_y = rte_cpu_to_le_32(tmp_y << shift_bits);
688 key_x[0] = tmp_x & 0xFF;
689 key_x[1] = (tmp_x >> HNS3_BITS_PER_BYTE) & 0xFF;
690 key_y[0] = tmp_y & 0xFF;
691 key_y[1] = (tmp_y >> HNS3_BITS_PER_BYTE) & 0xFF;
694 /* A complete key is combined with meta data key and tuple key.
695 * Meta data key is stored at the MSB region, and tuple key is stored at
696 * the LSB region, unused bits will be filled 0.
698 static int hns3_config_key(struct hns3_adapter *hns,
699 struct hns3_fdir_rule *rule)
701 struct hns3_pf *pf = &hns->pf;
702 struct hns3_hw *hw = &hns->hw;
703 struct hns3_fd_key_cfg *key_cfg;
706 uint8_t key_x[MAX_KEY_BYTES] __rte_aligned(4);
707 uint8_t key_y[MAX_KEY_BYTES] __rte_aligned(4);
708 uint8_t vf_id = rule->vf_id;
709 uint8_t meta_data_region;
714 memset(key_x, 0, sizeof(key_x));
715 memset(key_y, 0, sizeof(key_y));
719 key_cfg = &pf->fdir.fd_cfg.key_cfg[HNS3_FD_STAGE_1];
720 for (i = 0; i < MAX_TUPLE; i++) {
723 tuple_size = tuple_key_info[i].key_length / HNS3_BITS_PER_BYTE;
724 if (key_cfg->tuple_active & BIT(i)) {
725 tuple_valid = hns3_fd_convert_tuple(hw, i, cur_key_x,
728 cur_key_x += tuple_size;
729 cur_key_y += tuple_size;
734 meta_data_region = pf->fdir.fd_cfg.max_key_length / HNS3_BITS_PER_BYTE -
735 MAX_META_DATA_LENGTH / HNS3_BITS_PER_BYTE;
737 hns3_fd_convert_meta_data(key_cfg, vf_id, rule,
738 key_x + meta_data_region,
739 key_y + meta_data_region);
741 ret = hns3_fd_tcam_config(hw, false, rule->location, key_y, true);
743 hns3_err(hw, "Config fd key_y fail, loc=%d, ret=%d",
744 rule->queue_id, ret);
748 ret = hns3_fd_tcam_config(hw, true, rule->location, key_x, true);
750 hns3_err(hw, "Config fd key_x fail, loc=%d, ret=%d",
751 rule->queue_id, ret);
755 static int hns3_config_action(struct hns3_hw *hw, struct hns3_fdir_rule *rule)
757 struct hns3_fd_ad_data ad_data;
759 ad_data.ad_id = rule->location;
761 if (rule->action == HNS3_FD_ACTION_DROP_PACKET) {
762 ad_data.drop_packet = true;
763 ad_data.queue_id = 0;
764 ad_data.nb_queues = 0;
766 ad_data.drop_packet = false;
767 ad_data.queue_id = rule->queue_id;
768 ad_data.nb_queues = rule->nb_queues;
771 if (unlikely(rule->flags & HNS3_RULE_FLAG_COUNTER)) {
772 ad_data.use_counter = true;
773 ad_data.counter_id = rule->act_cnt.id;
775 ad_data.use_counter = false;
776 ad_data.counter_id = 0;
779 if (unlikely(rule->flags & HNS3_RULE_FLAG_FDID))
780 ad_data.rule_id = rule->fd_id;
782 ad_data.rule_id = rule->location;
784 ad_data.use_next_stage = false;
785 ad_data.next_input_key = 0;
787 ad_data.write_rule_id_to_bd = true;
789 return hns3_fd_ad_config(hw, ad_data.ad_id, &ad_data);
792 static int hns3_fd_clear_all_rules(struct hns3_hw *hw, uint32_t rule_num)
797 for (i = 0; i < rule_num; i++) {
798 ret = hns3_fd_tcam_config(hw, true, i, NULL, false);
806 int hns3_fdir_filter_init(struct hns3_adapter *hns)
808 struct hns3_pf *pf = &hns->pf;
809 struct hns3_fdir_info *fdir_info = &pf->fdir;
810 uint32_t rule_num = fdir_info->fd_cfg.rule_num[HNS3_FD_STAGE_1];
811 char fdir_hash_name[RTE_HASH_NAMESIZE];
812 struct rte_hash_parameters fdir_hash_params = {
813 .name = fdir_hash_name,
815 .key_len = sizeof(struct hns3_fdir_key_conf),
816 .hash_func = rte_hash_crc,
817 .hash_func_init_val = 0,
821 ret = hns3_fd_clear_all_rules(&hns->hw, rule_num);
823 PMD_INIT_LOG(ERR, "Clear all fd rules fail! ret = %d", ret);
827 fdir_hash_params.socket_id = rte_socket_id();
828 TAILQ_INIT(&fdir_info->fdir_list);
829 rte_spinlock_init(&fdir_info->flows_lock);
830 snprintf(fdir_hash_name, RTE_HASH_NAMESIZE, "%s", hns->hw.data->name);
831 fdir_info->hash_handle = rte_hash_create(&fdir_hash_params);
832 if (fdir_info->hash_handle == NULL) {
833 PMD_INIT_LOG(ERR, "Create FDIR hash handle fail!");
836 fdir_info->hash_map = rte_zmalloc("hns3 FDIR hash",
838 sizeof(struct hns3_fdir_rule_ele *),
840 if (fdir_info->hash_map == NULL) {
841 PMD_INIT_LOG(ERR, "Allocate memory for FDIR hash map fail!");
842 rte_hash_free(fdir_info->hash_handle);
849 void hns3_fdir_filter_uninit(struct hns3_adapter *hns)
851 struct hns3_pf *pf = &hns->pf;
852 struct hns3_fdir_info *fdir_info = &pf->fdir;
853 struct hns3_fdir_rule_ele *fdir_filter;
855 rte_spinlock_lock(&fdir_info->flows_lock);
856 if (fdir_info->hash_map) {
857 rte_free(fdir_info->hash_map);
858 fdir_info->hash_map = NULL;
860 if (fdir_info->hash_handle) {
861 rte_hash_free(fdir_info->hash_handle);
862 fdir_info->hash_handle = NULL;
864 rte_spinlock_unlock(&fdir_info->flows_lock);
866 fdir_filter = TAILQ_FIRST(&fdir_info->fdir_list);
867 while (fdir_filter) {
868 TAILQ_REMOVE(&fdir_info->fdir_list, fdir_filter, entries);
869 hns3_fd_tcam_config(&hns->hw, true,
870 fdir_filter->fdir_conf.location, NULL,
872 rte_free(fdir_filter);
873 fdir_filter = TAILQ_FIRST(&fdir_info->fdir_list);
878 * Find a key in the hash table.
880 * - Zero and positive values are key location.
881 * - -EINVAL if the parameters are invalid.
882 * - -ENOENT if the key is not found.
884 static int hns3_fdir_filter_lookup(struct hns3_fdir_info *fdir_info,
885 struct hns3_fdir_key_conf *key)
890 rte_spinlock_lock(&fdir_info->flows_lock);
891 sig = rte_hash_crc(key, sizeof(*key), 0);
892 ret = rte_hash_lookup_with_hash(fdir_info->hash_handle, key, sig);
893 rte_spinlock_unlock(&fdir_info->flows_lock);
898 static int hns3_insert_fdir_filter(struct hns3_hw *hw,
899 struct hns3_fdir_info *fdir_info,
900 struct hns3_fdir_rule_ele *fdir_filter)
902 struct hns3_fdir_key_conf *key;
906 key = &fdir_filter->fdir_conf.key_conf;
907 rte_spinlock_lock(&fdir_info->flows_lock);
908 sig = rte_hash_crc(key, sizeof(*key), 0);
909 ret = rte_hash_add_key_with_hash(fdir_info->hash_handle, key, sig);
911 rte_spinlock_unlock(&fdir_info->flows_lock);
912 hns3_err(hw, "Hash table full? err:%d(%s)!", ret,
917 fdir_info->hash_map[ret] = fdir_filter;
918 TAILQ_INSERT_TAIL(&fdir_info->fdir_list, fdir_filter, entries);
919 rte_spinlock_unlock(&fdir_info->flows_lock);
924 static int hns3_remove_fdir_filter(struct hns3_hw *hw,
925 struct hns3_fdir_info *fdir_info,
926 struct hns3_fdir_key_conf *key)
928 struct hns3_fdir_rule_ele *fdir_filter;
932 rte_spinlock_lock(&fdir_info->flows_lock);
933 sig = rte_hash_crc(key, sizeof(*key), 0);
934 ret = rte_hash_del_key_with_hash(fdir_info->hash_handle, key, sig);
936 rte_spinlock_unlock(&fdir_info->flows_lock);
937 hns3_err(hw, "Delete hash key fail ret=%d", ret);
941 fdir_filter = fdir_info->hash_map[ret];
942 fdir_info->hash_map[ret] = NULL;
943 TAILQ_REMOVE(&fdir_info->fdir_list, fdir_filter, entries);
944 rte_spinlock_unlock(&fdir_info->flows_lock);
946 rte_free(fdir_filter);
951 int hns3_fdir_filter_program(struct hns3_adapter *hns,
952 struct hns3_fdir_rule *rule, bool del)
954 struct hns3_pf *pf = &hns->pf;
955 struct hns3_fdir_info *fdir_info = &pf->fdir;
956 struct hns3_fdir_rule_ele *node;
957 struct hns3_hw *hw = &hns->hw;
961 ret = hns3_fd_tcam_config(hw, true, rule->location, NULL,
964 hns3_err(hw, "Failed to delete fdir: %d src_ip:%x "
965 "dst_ip:%x src_port:%d dst_port:%d ret = %d",
967 rule->key_conf.spec.src_ip[IP_ADDR_KEY_ID],
968 rule->key_conf.spec.dst_ip[IP_ADDR_KEY_ID],
969 rule->key_conf.spec.src_port,
970 rule->key_conf.spec.dst_port, ret);
972 hns3_remove_fdir_filter(hw, fdir_info, &rule->key_conf);
977 ret = hns3_fdir_filter_lookup(fdir_info, &rule->key_conf);
979 hns3_err(hw, "Conflict with existing fdir loc: %d", ret);
983 node = rte_zmalloc("hns3 fdir rule", sizeof(struct hns3_fdir_rule_ele),
986 hns3_err(hw, "Failed to allocate fdir_rule memory");
990 rte_memcpy(&node->fdir_conf, rule, sizeof(struct hns3_fdir_rule));
991 ret = hns3_insert_fdir_filter(hw, fdir_info, node);
996 rule->location = ret;
997 node->fdir_conf.location = ret;
999 rte_spinlock_lock(&fdir_info->flows_lock);
1000 ret = hns3_config_action(hw, rule);
1002 ret = hns3_config_key(hns, rule);
1003 rte_spinlock_unlock(&fdir_info->flows_lock);
1005 hns3_err(hw, "Failed to config fdir: %d src_ip:%x dst_ip:%x "
1006 "src_port:%d dst_port:%d ret = %d",
1008 rule->key_conf.spec.src_ip[IP_ADDR_KEY_ID],
1009 rule->key_conf.spec.dst_ip[IP_ADDR_KEY_ID],
1010 rule->key_conf.spec.src_port,
1011 rule->key_conf.spec.dst_port, ret);
1012 (void)hns3_remove_fdir_filter(hw, fdir_info, &rule->key_conf);
1018 /* remove all the flow director filters */
1019 int hns3_clear_all_fdir_filter(struct hns3_adapter *hns)
1021 struct hns3_pf *pf = &hns->pf;
1022 struct hns3_fdir_info *fdir_info = &pf->fdir;
1023 struct hns3_fdir_rule_ele *fdir_filter;
1024 struct hns3_hw *hw = &hns->hw;
1027 /* flush flow director */
1028 rte_spinlock_lock(&fdir_info->flows_lock);
1029 rte_hash_reset(fdir_info->hash_handle);
1030 rte_spinlock_unlock(&fdir_info->flows_lock);
1032 fdir_filter = TAILQ_FIRST(&fdir_info->fdir_list);
1033 while (fdir_filter) {
1034 TAILQ_REMOVE(&fdir_info->fdir_list, fdir_filter, entries);
1035 ret += hns3_fd_tcam_config(hw, true,
1036 fdir_filter->fdir_conf.location,
1038 rte_free(fdir_filter);
1039 fdir_filter = TAILQ_FIRST(&fdir_info->fdir_list);
1043 hns3_err(hw, "Fail to delete FDIR filter, ret = %d", ret);
1049 int hns3_restore_all_fdir_filter(struct hns3_adapter *hns)
1051 struct hns3_pf *pf = &hns->pf;
1052 struct hns3_fdir_info *fdir_info = &pf->fdir;
1053 struct hns3_fdir_rule_ele *fdir_filter;
1054 struct hns3_hw *hw = &hns->hw;
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);
1070 hns3_err(hw, "Fail to restore FDIR filter, ret = %d", ret);
1076 int hns3_get_count(struct hns3_hw *hw, uint32_t id, uint64_t *value)
1078 struct hns3_fd_get_cnt_cmd *req;
1079 struct hns3_cmd_desc desc;
1082 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_FD_COUNTER_OP, true);
1084 req = (struct hns3_fd_get_cnt_cmd *)desc.data;
1085 req->stage = HNS3_FD_STAGE_1;
1086 req->index = rte_cpu_to_le_32(id);
1088 ret = hns3_cmd_send(hw, &desc, 1);
1090 hns3_err(hw, "Read counter fail, ret=%d", ret);
1094 *value = req->value;