e6a065b6bc3522411f4fd9b36fd74c52635d3005
[dpdk.git] / drivers / net / hns3 / hns3_fdir.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2018-2019 Hisilicon Limited.
3  */
4
5 #include <stdbool.h>
6 #include <rte_ethdev_driver.h>
7 #include <rte_hash.h>
8 #include <rte_hash_crc.h>
9 #include <rte_io.h>
10 #include <rte_malloc.h>
11
12 #include "hns3_ethdev.h"
13 #include "hns3_logs.h"
14
15 #define HNS3_VLAN_TAG_TYPE_NONE         0
16 #define HNS3_VLAN_TAG_TYPE_TAG2         1
17 #define HNS3_VLAN_TAG_TYPE_TAG1         2
18 #define HNS3_VLAN_TAG_TYPE_TAG1_2       3
19
20 #define HNS3_PF_ID_S                    0
21 #define HNS3_PF_ID_M                    GENMASK(2, 0)
22 #define HNS3_VF_ID_S                    3
23 #define HNS3_VF_ID_M                    GENMASK(10, 3)
24 #define HNS3_PORT_TYPE_B                11
25 #define HNS3_NETWORK_PORT_ID_S          0
26 #define HNS3_NETWORK_PORT_ID_M          GENMASK(3, 0)
27
28 #define HNS3_FD_EPORT_SW_EN_B           0
29
30 #define HNS3_FD_AD_DATA_S               32
31 #define HNS3_FD_AD_DROP_B               0
32 #define HNS3_FD_AD_DIRECT_QID_B         1
33 #define HNS3_FD_AD_QID_S                2
34 #define HNS3_FD_AD_QID_M                GENMASK(11, 2)
35 #define HNS3_FD_AD_USE_COUNTER_B        12
36 #define HNS3_FD_AD_COUNTER_NUM_S        13
37 #define HNS3_FD_AD_COUNTER_NUM_M        GENMASK(19, 13)
38 #define HNS3_FD_AD_NXT_STEP_B           20
39 #define HNS3_FD_AD_NXT_KEY_S            21
40 #define HNS3_FD_AD_NXT_KEY_M            GENMASK(25, 21)
41 #define HNS3_FD_AD_WR_RULE_ID_B         0
42 #define HNS3_FD_AD_RULE_ID_S            1
43 #define HNS3_FD_AD_RULE_ID_M            GENMASK(12, 1)
44 #define HNS3_FD_AD_QUEUE_REGION_EN_B    16
45 #define HNS3_FD_AD_QUEUE_REGION_SIZE_S  17
46 #define HNS3_FD_AD_QUEUE_REGION_SIZE_M  GENMASK(20, 17)
47 #define HNS3_FD_AD_COUNTER_HIGH_BIT     7
48 #define HNS3_FD_AD_COUNTER_HIGH_BIT_B   26
49
50 enum HNS3_PORT_TYPE {
51         HOST_PORT,
52         NETWORK_PORT
53 };
54
55 enum HNS3_FD_MODE {
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,
60 };
61
62 enum HNS3_FD_KEY_TYPE {
63         HNS3_FD_KEY_BASE_ON_PTYPE,
64         HNS3_FD_KEY_BASE_ON_TUPLE,
65 };
66
67 enum HNS3_FD_META_DATA {
68         PACKET_TYPE_ID,
69         IP_FRAGEMENT,
70         ROCE_TYPE,
71         NEXT_KEY,
72         VLAN_NUMBER,
73         SRC_VPORT,
74         DST_VPORT,
75         TUNNEL_PACKET,
76         MAX_META_DATA,
77 };
78
79 struct key_info {
80         uint8_t key_type;
81         uint8_t key_length;
82 };
83
84 static const struct key_info meta_data_key_info[] = {
85         {PACKET_TYPE_ID, 6},
86         {IP_FRAGEMENT, 1},
87         {ROCE_TYPE, 1},
88         {NEXT_KEY, 5},
89         {VLAN_NUMBER, 2},
90         {SRC_VPORT, 12},
91         {DST_VPORT, 12},
92         {TUNNEL_PACKET, 1},
93 };
94
95 static const struct key_info tuple_key_info[] = {
96         {OUTER_DST_MAC, 48},
97         {OUTER_SRC_MAC, 48},
98         {OUTER_VLAN_TAG_FST, 16},
99         {OUTER_VLAN_TAG_SEC, 16},
100         {OUTER_ETH_TYPE, 16},
101         {OUTER_L2_RSV, 16},
102         {OUTER_IP_TOS, 8},
103         {OUTER_IP_PROTO, 8},
104         {OUTER_SRC_IP, 32},
105         {OUTER_DST_IP, 32},
106         {OUTER_L3_RSV, 16},
107         {OUTER_SRC_PORT, 16},
108         {OUTER_DST_PORT, 16},
109         {OUTER_L4_RSV, 32},
110         {OUTER_TUN_VNI, 24},
111         {OUTER_TUN_FLOW_ID, 8},
112         {INNER_DST_MAC, 48},
113         {INNER_SRC_MAC, 48},
114         {INNER_VLAN_TAG1, 16},
115         {INNER_VLAN_TAG2, 16},
116         {INNER_ETH_TYPE, 16},
117         {INNER_L2_RSV, 16},
118         {INNER_IP_TOS, 8},
119         {INNER_IP_PROTO, 8},
120         {INNER_SRC_IP, 32},
121         {INNER_DST_IP, 32},
122         {INNER_L3_RSV, 16},
123         {INNER_SRC_PORT, 16},
124         {INNER_DST_PORT, 16},
125         {INNER_SCTP_TAG, 32},
126 };
127
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)
133
134 enum HNS3_FD_PACKET_TYPE {
135         NIC_PACKET,
136         ROCE_PACKET,
137 };
138
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
153  * the formulae:
154  *      x = (~k) & v
155  *      y = k & v
156  */
157 #define calc_x(x, k, v) ((x) = (~(k) & (v)))
158 #define calc_y(y, k, v) ((y) = ((k) & (v)))
159
160 struct hns3_fd_tcam_config_1_cmd {
161         uint8_t stage;
162         uint8_t xy_sel;
163         uint8_t port_info;
164         uint8_t rsv1[1];
165         rte_le32_t index;
166         uint8_t entry_vld;
167         uint8_t rsv2[7];
168         uint8_t tcam_data[8];
169 };
170
171 struct hns3_fd_tcam_config_2_cmd {
172         uint8_t tcam_data[24];
173 };
174
175 struct hns3_fd_tcam_config_3_cmd {
176         uint8_t tcam_data[20];
177         uint8_t rsv[4];
178 };
179
180 struct hns3_get_fd_mode_cmd {
181         uint8_t mode;
182         uint8_t enable;
183         uint8_t rsv[22];
184 };
185
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;
191         uint8_t rsv[12];
192 };
193
194 struct hns3_set_fd_key_config_cmd {
195         uint8_t stage;
196         uint8_t key_select;
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;
201         uint8_t rsv1[2];
202         rte_le32_t tuple_mask;
203         rte_le32_t meta_data_mask;
204         uint8_t rsv2[8];
205 };
206
207 struct hns3_fd_ad_config_cmd {
208         uint8_t stage;
209         uint8_t rsv1[3];
210         rte_le32_t index;
211         rte_le64_t ad_data;
212         uint8_t rsv2[8];
213 };
214
215 struct hns3_fd_get_cnt_cmd {
216         uint8_t stage;
217         uint8_t rsv1[3];
218         rte_le16_t index;
219         uint8_t rsv2[2];
220         rte_le64_t value;
221         uint8_t rsv3[8];
222 };
223
224 static int hns3_get_fd_mode(struct hns3_hw *hw, uint8_t *fd_mode)
225 {
226         struct hns3_get_fd_mode_cmd *req;
227         struct hns3_cmd_desc desc;
228         int ret;
229
230         hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_FD_MODE_CTRL, true);
231
232         req = (struct hns3_get_fd_mode_cmd *)desc.data;
233
234         ret = hns3_cmd_send(hw, &desc, 1);
235         if (ret) {
236                 hns3_err(hw, "Get fd mode fail, ret=%d", ret);
237                 return ret;
238         }
239
240         *fd_mode = req->mode;
241
242         return ret;
243 }
244
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)
250 {
251         struct hns3_get_fd_allocation_cmd *req;
252         struct hns3_cmd_desc desc;
253         int ret;
254
255         hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_FD_GET_ALLOCATION, true);
256
257         req = (struct hns3_get_fd_allocation_cmd *)desc.data;
258
259         ret = hns3_cmd_send(hw, &desc, 1);
260         if (ret) {
261                 hns3_err(hw, "Query fd allocation fail, ret=%d", ret);
262                 return ret;
263         }
264
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);
269
270         return ret;
271 }
272
273 static int hns3_set_fd_key_config(struct hns3_adapter *hns)
274 {
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;
280         int ret;
281
282         hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_FD_KEY_CONFIG, false);
283
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);
294
295         ret = hns3_cmd_send(hw, &desc, 1);
296         if (ret)
297                 hns3_err(hw, "Set fd key fail, ret=%d", ret);
298
299         return ret;
300 }
301
302 int hns3_init_fd_config(struct hns3_adapter *hns)
303 {
304         struct hns3_pf *pf = &hns->pf;
305         struct hns3_hw *hw = &hns->hw;
306         struct hns3_fd_key_cfg *key_cfg;
307         int ret;
308
309         ret = hns3_get_fd_mode(hw, &pf->fdir.fd_cfg.fd_mode);
310         if (ret)
311                 return ret;
312
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;
316                 break;
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");
320                 break;
321         default:
322                 hns3_err(hw, "Unsupported flow director mode %d",
323                             pf->fdir.fd_cfg.fd_mode);
324                 return -EOPNOTSUPP;
325         }
326
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;
333
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);
338
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);
347         }
348
349         /* roce_type is used to filter roce frames
350          * dst_vport is used to specify the rule
351          */
352         key_cfg->meta_data_active = BIT(DST_VPORT) | BIT(TUNNEL_PACKET) |
353             BIT(VLAN_NUMBER);
354
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]);
360         if (ret)
361                 return ret;
362
363         return hns3_set_fd_key_config(hns);
364 }
365
366 static int hns3_fd_tcam_config(struct hns3_hw *hw, bool sel_x, int loc,
367                                uint8_t *key, bool is_add)
368 {
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];
374         int len;
375         int ret;
376
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);
382
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;
386
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;
392
393         if (key) {
394                 len = sizeof(req1->tcam_data);
395                 memcpy(req1->tcam_data, key, len);
396                 key += len;
397
398                 len = sizeof(req2->tcam_data);
399                 memcpy(req2->tcam_data, key, len);
400                 key += len;
401
402                 len = sizeof(req3->tcam_data);
403                 memcpy(req3->tcam_data, key, len);
404         }
405
406         ret = hns3_cmd_send(hw, desc, FD_TCAM_CMD_NUM);
407         if (ret)
408                 hns3_err(hw, "Config tcam key fail, ret=%d loc=%d add=%d",
409                             ret, loc, is_add);
410         return ret;
411 }
412
413 static int hns3_fd_ad_config(struct hns3_hw *hw, int loc,
414                              struct hns3_fd_ad_data *action)
415 {
416         struct hns3_fd_ad_config_cmd *req;
417         struct hns3_cmd_desc desc;
418         uint64_t ad_data = 0;
419         int ret;
420
421         hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_FD_AD_OP, false);
422
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;
426
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,
430                        action->rule_id);
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));
436         }
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,
445                        action->queue_id);
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);
452
453         req->ad_data = rte_cpu_to_le_64(ad_data);
454         ret = hns3_cmd_send(hw, &desc, 1);
455         if (ret)
456                 hns3_err(hw, "Config fd ad fail, ret=%d loc=%d", ret, loc);
457
458         return ret;
459 }
460
461 static inline void hns3_fd_convert_mac(uint8_t *key, uint8_t *mask,
462                                        uint8_t *mac_x, uint8_t *mac_y)
463 {
464         uint8_t tmp;
465         int i;
466
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]);
471         }
472 }
473
474 static void hns3_fd_convert_int16(uint32_t tuple, struct hns3_fdir_rule *rule,
475                                   uint8_t *val_x, uint8_t *val_y)
476 {
477         uint16_t tmp_x_s;
478         uint16_t tmp_y_s;
479         uint16_t mask;
480         uint16_t key;
481
482         switch (tuple) {
483         case OUTER_SRC_PORT:
484                 key = rule->key_conf.spec.outer_src_port;
485                 mask = rule->key_conf.mask.outer_src_port;
486                 break;
487         case OUTER_DST_PORT:
488                 key = rule->key_conf.spec.tunnel_type;
489                 mask = rule->key_conf.mask.tunnel_type;
490                 break;
491         case OUTER_ETH_TYPE:
492                 key = rule->key_conf.spec.outer_ether_type;
493                 mask = rule->key_conf.mask.outer_ether_type;
494                 break;
495         case INNER_SRC_PORT:
496                 key = rule->key_conf.spec.src_port;
497                 mask = rule->key_conf.mask.src_port;
498                 break;
499         case INNER_DST_PORT:
500                 key = rule->key_conf.spec.dst_port;
501                 mask = rule->key_conf.mask.dst_port;
502                 break;
503         case INNER_VLAN_TAG1:
504                 key = rule->key_conf.spec.vlan_tag1;
505                 mask = rule->key_conf.mask.vlan_tag1;
506                 break;
507         case INNER_VLAN_TAG2:
508                 key = rule->key_conf.spec.vlan_tag2;
509                 mask = rule->key_conf.mask.vlan_tag2;
510                 break;
511         default:
512                 /*  INNER_ETH_TYPE: */
513                 key = rule->key_conf.spec.ether_type;
514                 mask = rule->key_conf.mask.ether_type;
515                 break;
516         }
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;
523 }
524
525 static inline void hns3_fd_convert_int32(uint32_t key, uint32_t mask,
526                                          uint8_t *val_x, uint8_t *val_y)
527 {
528         uint32_t tmp_x_l;
529         uint32_t tmp_y_l;
530
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));
535 }
536
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)
540 {
541         struct hns3_fdir_key_conf *key_conf;
542         int tmp;
543         int i;
544
545         if ((rule->input_set & BIT(tuple)) == 0)
546                 return true;
547
548         key_conf = &rule->key_conf;
549         switch (tuple) {
550         case INNER_DST_MAC:
551                 hns3_fd_convert_mac(key_conf->spec.dst_mac,
552                                     key_conf->mask.dst_mac, key_x, key_y);
553                 break;
554         case INNER_SRC_MAC:
555                 hns3_fd_convert_mac(key_conf->spec.src_mac,
556                                     key_conf->mask.src_mac, key_x, key_y);
557                 break;
558         case OUTER_SRC_PORT:
559         case OUTER_DST_PORT:
560         case OUTER_ETH_TYPE:
561         case INNER_SRC_PORT:
562         case INNER_DST_PORT:
563         case INNER_VLAN_TAG1:
564         case INNER_VLAN_TAG2:
565         case INNER_ETH_TYPE:
566                 hns3_fd_convert_int16(tuple, rule, key_x, key_y);
567                 break;
568         case INNER_SRC_IP:
569                 hns3_fd_convert_int32(key_conf->spec.src_ip[IP_ADDR_KEY_ID],
570                                       key_conf->mask.src_ip[IP_ADDR_KEY_ID],
571                                       key_x, key_y);
572                 break;
573         case INNER_DST_IP:
574                 hns3_fd_convert_int32(key_conf->spec.dst_ip[IP_ADDR_KEY_ID],
575                                       key_conf->mask.dst_ip[IP_ADDR_KEY_ID],
576                                       key_x, key_y);
577                 break;
578         case INNER_SCTP_TAG:
579                 hns3_fd_convert_int32(key_conf->spec.sctp_tag,
580                                       key_conf->mask.sctp_tag, key_x, key_y);
581                 break;
582         case OUTER_TUN_VNI:
583                 for (i = 0; i < VNI_OR_TNI_LEN; i++) {
584                         tmp = VNI_OR_TNI_LEN - 1 - i;
585                         calc_x(key_x[tmp],
586                                key_conf->spec.outer_tun_vni[i],
587                                key_conf->mask.outer_tun_vni[i]);
588                         calc_y(key_y[tmp],
589                                key_conf->spec.outer_tun_vni[i],
590                                key_conf->mask.outer_tun_vni[i]);
591                 }
592                 break;
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);
598                 break;
599         case INNER_IP_TOS:
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);
602                 break;
603         case OUTER_IP_PROTO:
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);
608                 break;
609         case INNER_IP_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);
614                 break;
615         default:
616                 hns3_warn(hw, "not support tuple of (%d)", tuple);
617                 break;
618         }
619         return true;
620 }
621
622 static uint32_t hns3_get_port_number(uint8_t pf_id, uint8_t vf_id)
623 {
624         uint32_t port_number = 0;
625
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);
629
630         return port_number;
631 }
632
633 static void hns3_fd_convert_meta_data(struct hns3_fd_key_cfg *cfg,
634                                       uint8_t vf_id,
635                                       struct hns3_fdir_rule *rule,
636                                       uint8_t *key_x, uint8_t *key_y)
637 {
638         uint16_t meta_data = 0;
639         uint32_t port_number;
640         uint8_t cur_pos = 0;
641         uint8_t tuple_size;
642         uint8_t shift_bits;
643         uint32_t tmp_x;
644         uint32_t tmp_y;
645         uint8_t i;
646
647         for (i = 0; i < MAX_META_DATA; i++) {
648                 if ((cfg->meta_data_active & BIT(i)) == 0)
649                         continue;
650
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) {
657                         uint32_t vlan_tag;
658                         uint8_t vlan_num;
659                         if (rule->key_conf.spec.tunnel_type == 0)
660                                 vlan_num = rule->key_conf.vlan_num;
661                         else
662                                 vlan_num = rule->key_conf.outer_vlan_num;
663                         if (vlan_num == 1)
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;
667                         else
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;
679                 }
680         }
681
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;
685
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;
692 }
693
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.
697  */
698 static int hns3_config_key(struct hns3_adapter *hns,
699                            struct hns3_fdir_rule *rule)
700 {
701         struct hns3_pf *pf = &hns->pf;
702         struct hns3_hw *hw = &hns->hw;
703         struct hns3_fd_key_cfg *key_cfg;
704         uint8_t *cur_key_x;
705         uint8_t *cur_key_y;
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;
710         uint8_t tuple_size;
711         uint8_t i;
712         int ret;
713
714         memset(key_x, 0, sizeof(key_x));
715         memset(key_y, 0, sizeof(key_y));
716         cur_key_x = key_x;
717         cur_key_y = key_y;
718
719         key_cfg = &pf->fdir.fd_cfg.key_cfg[HNS3_FD_STAGE_1];
720         for (i = 0; i < MAX_TUPLE; i++) {
721                 bool tuple_valid;
722
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,
726                                                             cur_key_y, rule);
727                         if (tuple_valid) {
728                                 cur_key_x += tuple_size;
729                                 cur_key_y += tuple_size;
730                         }
731                 }
732         }
733
734         meta_data_region = pf->fdir.fd_cfg.max_key_length / HNS3_BITS_PER_BYTE -
735             MAX_META_DATA_LENGTH / HNS3_BITS_PER_BYTE;
736
737         hns3_fd_convert_meta_data(key_cfg, vf_id, rule,
738                                   key_x + meta_data_region,
739                                   key_y + meta_data_region);
740
741         ret = hns3_fd_tcam_config(hw, false, rule->location, key_y, true);
742         if (ret) {
743                 hns3_err(hw, "Config fd key_y fail, loc=%d, ret=%d",
744                             rule->queue_id, ret);
745                 return ret;
746         }
747
748         ret = hns3_fd_tcam_config(hw, true, rule->location, key_x, true);
749         if (ret)
750                 hns3_err(hw, "Config fd key_x fail, loc=%d, ret=%d",
751                             rule->queue_id, ret);
752         return ret;
753 }
754
755 static int hns3_config_action(struct hns3_hw *hw, struct hns3_fdir_rule *rule)
756 {
757         struct hns3_fd_ad_data ad_data;
758
759         ad_data.ad_id = rule->location;
760
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;
765         } else {
766                 ad_data.drop_packet = false;
767                 ad_data.queue_id = rule->queue_id;
768                 ad_data.nb_queues = rule->nb_queues;
769         }
770
771         if (unlikely(rule->flags & HNS3_RULE_FLAG_COUNTER)) {
772                 ad_data.use_counter = true;
773                 ad_data.counter_id = rule->act_cnt.id;
774         } else {
775                 ad_data.use_counter = false;
776                 ad_data.counter_id = 0;
777         }
778
779         if (unlikely(rule->flags & HNS3_RULE_FLAG_FDID))
780                 ad_data.rule_id = rule->fd_id;
781         else
782                 ad_data.rule_id = rule->location;
783
784         ad_data.use_next_stage = false;
785         ad_data.next_input_key = 0;
786
787         ad_data.write_rule_id_to_bd = true;
788
789         return hns3_fd_ad_config(hw, ad_data.ad_id, &ad_data);
790 }
791
792 static int hns3_fd_clear_all_rules(struct hns3_hw *hw, uint32_t rule_num)
793 {
794         uint32_t i;
795         int ret;
796
797         for (i = 0; i < rule_num; i++) {
798                 ret = hns3_fd_tcam_config(hw, true, i, NULL, false);
799                 if (ret)
800                         return ret;
801         }
802
803         return 0;
804 }
805
806 int hns3_fdir_filter_init(struct hns3_adapter *hns)
807 {
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,
814                 .entries = rule_num,
815                 .key_len = sizeof(struct hns3_fdir_key_conf),
816                 .hash_func = rte_hash_crc,
817                 .hash_func_init_val = 0,
818         };
819         int ret;
820
821         ret = hns3_fd_clear_all_rules(&hns->hw, rule_num);
822         if (ret) {
823                 PMD_INIT_LOG(ERR, "Clear all fd rules fail! ret = %d", ret);
824                 return ret;
825         }
826
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!");
834                 return -EINVAL;
835         }
836         fdir_info->hash_map = rte_zmalloc("hns3 FDIR hash",
837                                           rule_num *
838                                           sizeof(struct hns3_fdir_rule_ele *),
839                                           0);
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);
843                 return -ENOMEM;
844         }
845
846         return 0;
847 }
848
849 void hns3_fdir_filter_uninit(struct hns3_adapter *hns)
850 {
851         struct hns3_pf *pf = &hns->pf;
852         struct hns3_fdir_info *fdir_info = &pf->fdir;
853         struct hns3_fdir_rule_ele *fdir_filter;
854
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;
859         }
860         if (fdir_info->hash_handle) {
861                 rte_hash_free(fdir_info->hash_handle);
862                 fdir_info->hash_handle = NULL;
863         }
864         rte_spinlock_unlock(&fdir_info->flows_lock);
865
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,
871                                     false);
872                 rte_free(fdir_filter);
873                 fdir_filter = TAILQ_FIRST(&fdir_info->fdir_list);
874         }
875 }
876
877 /*
878  * Find a key in the hash table.
879  * @return
880  *   - Zero and positive values are key location.
881  *   - -EINVAL if the parameters are invalid.
882  *   - -ENOENT if the key is not found.
883  */
884 static int hns3_fdir_filter_lookup(struct hns3_fdir_info *fdir_info,
885                                     struct hns3_fdir_key_conf *key)
886 {
887         hash_sig_t sig;
888         int ret;
889
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);
894
895         return ret;
896 }
897
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)
901 {
902         struct hns3_fdir_key_conf *key;
903         hash_sig_t sig;
904         int ret;
905
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);
910         if (ret < 0) {
911                 rte_spinlock_unlock(&fdir_info->flows_lock);
912                 hns3_err(hw, "Hash table full? err:%d(%s)!", ret,
913                          strerror(-ret));
914                 return ret;
915         }
916
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);
920
921         return ret;
922 }
923
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)
927 {
928         struct hns3_fdir_rule_ele *fdir_filter;
929         hash_sig_t sig;
930         int ret;
931
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);
935         if (ret < 0) {
936                 rte_spinlock_unlock(&fdir_info->flows_lock);
937                 hns3_err(hw, "Delete hash key fail ret=%d", ret);
938                 return ret;
939         }
940
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);
945
946         rte_free(fdir_filter);
947
948         return 0;
949 }
950
951 int hns3_fdir_filter_program(struct hns3_adapter *hns,
952                              struct hns3_fdir_rule *rule, bool del)
953 {
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;
958         int ret;
959
960         if (del) {
961                 ret = hns3_fd_tcam_config(hw, true, rule->location, NULL,
962                                           false);
963                 if (ret)
964                         hns3_err(hw, "Failed to delete fdir: %d src_ip:%x "
965                                  "dst_ip:%x src_port:%d dst_port:%d ret = %d",
966                                  rule->location,
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);
971                 else
972                         hns3_remove_fdir_filter(hw, fdir_info, &rule->key_conf);
973
974                 return ret;
975         }
976
977         ret = hns3_fdir_filter_lookup(fdir_info, &rule->key_conf);
978         if (ret >= 0) {
979                 hns3_err(hw, "Conflict with existing fdir loc: %d", ret);
980                 return -EINVAL;
981         }
982
983         node = rte_zmalloc("hns3 fdir rule", sizeof(struct hns3_fdir_rule_ele),
984                            0);
985         if (node == NULL) {
986                 hns3_err(hw, "Failed to allocate fdir_rule memory");
987                 return -ENOMEM;
988         }
989
990         rte_memcpy(&node->fdir_conf, rule, sizeof(struct hns3_fdir_rule));
991         ret = hns3_insert_fdir_filter(hw, fdir_info, node);
992         if (ret < 0) {
993                 rte_free(node);
994                 return ret;
995         }
996         rule->location = ret;
997         node->fdir_conf.location = ret;
998
999         rte_spinlock_lock(&fdir_info->flows_lock);
1000         ret = hns3_config_action(hw, rule);
1001         if (!ret)
1002                 ret = hns3_config_key(hns, rule);
1003         rte_spinlock_unlock(&fdir_info->flows_lock);
1004         if (ret) {
1005                 hns3_err(hw, "Failed to config fdir: %d src_ip:%x dst_ip:%x "
1006                          "src_port:%d dst_port:%d ret = %d",
1007                          rule->location,
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);
1013         }
1014
1015         return ret;
1016 }
1017
1018 /* remove all the flow director filters */
1019 int hns3_clear_all_fdir_filter(struct hns3_adapter *hns)
1020 {
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;
1025         int ret = 0;
1026
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);
1031
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,
1037                                            NULL, false);
1038                 rte_free(fdir_filter);
1039                 fdir_filter = TAILQ_FIRST(&fdir_info->fdir_list);
1040         }
1041
1042         if (ret) {
1043                 hns3_err(hw, "Fail to delete FDIR filter, ret = %d", ret);
1044                 ret = -EIO;
1045         }
1046         return ret;
1047 }
1048
1049 int hns3_restore_all_fdir_filter(struct hns3_adapter *hns)
1050 {
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;
1055         bool err = false;
1056         int ret;
1057
1058         TAILQ_FOREACH(fdir_filter, &fdir_info->fdir_list, entries) {
1059                 ret = hns3_config_action(hw, &fdir_filter->fdir_conf);
1060                 if (!ret)
1061                         ret = hns3_config_key(hns, &fdir_filter->fdir_conf);
1062                 if (ret) {
1063                         err = true;
1064                         if (ret == -EBUSY)
1065                                 break;
1066                 }
1067         }
1068
1069         if (err) {
1070                 hns3_err(hw, "Fail to restore FDIR filter, ret = %d", ret);
1071                 return -EIO;
1072         }
1073         return 0;
1074 }
1075
1076 int hns3_get_count(struct hns3_hw *hw, uint32_t id, uint64_t *value)
1077 {
1078         struct hns3_fd_get_cnt_cmd *req;
1079         struct hns3_cmd_desc desc;
1080         int ret;
1081
1082         hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_FD_COUNTER_OP, true);
1083
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);
1087
1088         ret = hns3_cmd_send(hw, &desc, 1);
1089         if (ret) {
1090                 hns3_err(hw, "Read counter fail, ret=%d", ret);
1091                 return ret;
1092         }
1093
1094         *value = req->value;
1095
1096         return ret;
1097 }