net/hns3: add default to switch when parsing fd tuple
[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(12, 2)
35 #define HNS3_FD_AD_USE_COUNTER_B        12
36 #define HNS3_FD_AD_COUNTER_NUM_S        13
37 #define HNS3_FD_AD_COUNTER_NUM_M        GENMASK(20, 13)
38 #define HNS3_FD_AD_NXT_STEP_B           20
39 #define HNS3_FD_AD_NXT_KEY_S            21
40 #define HNS3_FD_AD_NXT_KEY_M            GENMASK(26, 21)
41 #define HNS3_FD_AD_WR_RULE_ID_B 0
42 #define HNS3_FD_AD_RULE_ID_S            1
43 #define HNS3_FD_AD_RULE_ID_M            GENMASK(13, 1)
44 #define HNS3_FD_AD_COUNTER_HIGH_BIT     7
45 #define HNS3_FD_AD_COUNTER_HIGH_BIT_B   26
46
47 enum HNS3_PORT_TYPE {
48         HOST_PORT,
49         NETWORK_PORT
50 };
51
52 enum HNS3_FD_MODE {
53         HNS3_FD_MODE_DEPTH_2K_WIDTH_400B_STAGE_1,
54         HNS3_FD_MODE_DEPTH_1K_WIDTH_400B_STAGE_2,
55         HNS3_FD_MODE_DEPTH_4K_WIDTH_200B_STAGE_1,
56         HNS3_FD_MODE_DEPTH_2K_WIDTH_200B_STAGE_2,
57 };
58
59 enum HNS3_FD_KEY_TYPE {
60         HNS3_FD_KEY_BASE_ON_PTYPE,
61         HNS3_FD_KEY_BASE_ON_TUPLE,
62 };
63
64 enum HNS3_FD_META_DATA {
65         PACKET_TYPE_ID,
66         IP_FRAGEMENT,
67         ROCE_TYPE,
68         NEXT_KEY,
69         VLAN_NUMBER,
70         SRC_VPORT,
71         DST_VPORT,
72         TUNNEL_PACKET,
73         MAX_META_DATA,
74 };
75
76 struct key_info {
77         uint8_t key_type;
78         uint8_t key_length;
79 };
80
81 static const struct key_info meta_data_key_info[] = {
82         {PACKET_TYPE_ID, 6},
83         {IP_FRAGEMENT, 1},
84         {ROCE_TYPE, 1},
85         {NEXT_KEY, 5},
86         {VLAN_NUMBER, 2},
87         {SRC_VPORT, 12},
88         {DST_VPORT, 12},
89         {TUNNEL_PACKET, 1},
90 };
91
92 static const struct key_info tuple_key_info[] = {
93         {OUTER_DST_MAC, 48},
94         {OUTER_SRC_MAC, 48},
95         {OUTER_VLAN_TAG_FST, 16},
96         {OUTER_VLAN_TAG_SEC, 16},
97         {OUTER_ETH_TYPE, 16},
98         {OUTER_L2_RSV, 16},
99         {OUTER_IP_TOS, 8},
100         {OUTER_IP_PROTO, 8},
101         {OUTER_SRC_IP, 32},
102         {OUTER_DST_IP, 32},
103         {OUTER_L3_RSV, 16},
104         {OUTER_SRC_PORT, 16},
105         {OUTER_DST_PORT, 16},
106         {OUTER_L4_RSV, 32},
107         {OUTER_TUN_VNI, 24},
108         {OUTER_TUN_FLOW_ID, 8},
109         {INNER_DST_MAC, 48},
110         {INNER_SRC_MAC, 48},
111         {INNER_VLAN_TAG1, 16},
112         {INNER_VLAN_TAG2, 16},
113         {INNER_ETH_TYPE, 16},
114         {INNER_L2_RSV, 16},
115         {INNER_IP_TOS, 8},
116         {INNER_IP_PROTO, 8},
117         {INNER_SRC_IP, 32},
118         {INNER_DST_IP, 32},
119         {INNER_L3_RSV, 16},
120         {INNER_SRC_PORT, 16},
121         {INNER_DST_PORT, 16},
122         {INNER_SCTP_TAG, 32},
123 };
124
125 #define HNS3_BITS_PER_BYTE      8
126 #define MAX_KEY_LENGTH          400
127 #define MAX_200B_KEY_LENGTH     200
128 #define MAX_META_DATA_LENGTH    16
129 #define MAX_KEY_DWORDS  DIV_ROUND_UP(MAX_KEY_LENGTH / HNS3_BITS_PER_BYTE, 4)
130 #define MAX_KEY_BYTES   (MAX_KEY_DWORDS * 4)
131
132 enum HNS3_FD_PACKET_TYPE {
133         NIC_PACKET,
134         ROCE_PACKET,
135 };
136
137 /* For each bit of TCAM entry, it uses a pair of 'x' and
138  * 'y' to indicate which value to match, like below:
139  * ----------------------------------
140  * | bit x | bit y |  search value  |
141  * ----------------------------------
142  * |   0   |   0   |   always hit   |
143  * ----------------------------------
144  * |   1   |   0   |   match '0'    |
145  * ----------------------------------
146  * |   0   |   1   |   match '1'    |
147  * ----------------------------------
148  * |   1   |   1   |   invalid      |
149  * ----------------------------------
150  * Then for input key(k) and mask(v), we can calculate the value by
151  * the formulae:
152  *      x = (~k) & v
153  *      y = k & v
154  */
155 #define calc_x(x, k, v) ((x) = (~(k) & (v)))
156 #define calc_y(y, k, v) ((y) = ((k) & (v)))
157
158 struct hns3_fd_tcam_config_1_cmd {
159         uint8_t stage;
160         uint8_t xy_sel;
161         uint8_t port_info;
162         uint8_t rsv1[1];
163         rte_le32_t index;
164         uint8_t entry_vld;
165         uint8_t rsv2[7];
166         uint8_t tcam_data[8];
167 };
168
169 struct hns3_fd_tcam_config_2_cmd {
170         uint8_t tcam_data[24];
171 };
172
173 struct hns3_fd_tcam_config_3_cmd {
174         uint8_t tcam_data[20];
175         uint8_t rsv[4];
176 };
177
178 struct hns3_get_fd_mode_cmd {
179         uint8_t mode;
180         uint8_t enable;
181         uint8_t rsv[22];
182 };
183
184 struct hns3_get_fd_allocation_cmd {
185         rte_le32_t stage1_entry_num;
186         rte_le32_t stage2_entry_num;
187         rte_le16_t stage1_counter_num;
188         rte_le16_t stage2_counter_num;
189         uint8_t rsv[12];
190 };
191
192 struct hns3_set_fd_key_config_cmd {
193         uint8_t stage;
194         uint8_t key_select;
195         uint8_t inner_sipv6_word_en;
196         uint8_t inner_dipv6_word_en;
197         uint8_t outer_sipv6_word_en;
198         uint8_t outer_dipv6_word_en;
199         uint8_t rsv1[2];
200         rte_le32_t tuple_mask;
201         rte_le32_t meta_data_mask;
202         uint8_t rsv2[8];
203 };
204
205 struct hns3_fd_ad_config_cmd {
206         uint8_t stage;
207         uint8_t rsv1[3];
208         rte_le32_t index;
209         rte_le64_t ad_data;
210         uint8_t rsv2[8];
211 };
212
213 struct hns3_fd_get_cnt_cmd {
214         uint8_t stage;
215         uint8_t rsv1[3];
216         rte_le16_t index;
217         uint8_t rsv2[2];
218         rte_le64_t value;
219         uint8_t rsv3[8];
220 };
221
222 static int hns3_get_fd_mode(struct hns3_hw *hw, uint8_t *fd_mode)
223 {
224         struct hns3_get_fd_mode_cmd *req;
225         struct hns3_cmd_desc desc;
226         int ret;
227
228         hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_FD_MODE_CTRL, true);
229
230         req = (struct hns3_get_fd_mode_cmd *)desc.data;
231
232         ret = hns3_cmd_send(hw, &desc, 1);
233         if (ret) {
234                 hns3_err(hw, "Get fd mode fail, ret=%d", ret);
235                 return ret;
236         }
237
238         *fd_mode = req->mode;
239
240         return ret;
241 }
242
243 static int hns3_get_fd_allocation(struct hns3_hw *hw,
244                                   uint32_t *stage1_entry_num,
245                                   uint32_t *stage2_entry_num,
246                                   uint16_t *stage1_counter_num,
247                                   uint16_t *stage2_counter_num)
248 {
249         struct hns3_get_fd_allocation_cmd *req;
250         struct hns3_cmd_desc desc;
251         int ret;
252
253         hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_FD_GET_ALLOCATION, true);
254
255         req = (struct hns3_get_fd_allocation_cmd *)desc.data;
256
257         ret = hns3_cmd_send(hw, &desc, 1);
258         if (ret) {
259                 hns3_err(hw, "Query fd allocation fail, ret=%d", ret);
260                 return ret;
261         }
262
263         *stage1_entry_num = rte_le_to_cpu_32(req->stage1_entry_num);
264         *stage2_entry_num = rte_le_to_cpu_32(req->stage2_entry_num);
265         *stage1_counter_num = rte_le_to_cpu_16(req->stage1_counter_num);
266         *stage2_counter_num = rte_le_to_cpu_16(req->stage2_counter_num);
267
268         return ret;
269 }
270
271 static int hns3_set_fd_key_config(struct hns3_adapter *hns)
272 {
273         struct hns3_set_fd_key_config_cmd *req;
274         struct hns3_fd_key_cfg *key_cfg;
275         struct hns3_pf *pf = &hns->pf;
276         struct hns3_hw *hw = &hns->hw;
277         struct hns3_cmd_desc desc;
278         int ret;
279
280         hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_FD_KEY_CONFIG, false);
281
282         req = (struct hns3_set_fd_key_config_cmd *)desc.data;
283         key_cfg = &pf->fdir.fd_cfg.key_cfg[HNS3_FD_STAGE_1];
284         req->stage = HNS3_FD_STAGE_1;
285         req->key_select = key_cfg->key_sel;
286         req->inner_sipv6_word_en = key_cfg->inner_sipv6_word_en;
287         req->inner_dipv6_word_en = key_cfg->inner_dipv6_word_en;
288         req->outer_sipv6_word_en = key_cfg->outer_sipv6_word_en;
289         req->outer_dipv6_word_en = key_cfg->outer_dipv6_word_en;
290         req->tuple_mask = rte_cpu_to_le_32(~key_cfg->tuple_active);
291         req->meta_data_mask = rte_cpu_to_le_32(~key_cfg->meta_data_active);
292
293         ret = hns3_cmd_send(hw, &desc, 1);
294         if (ret)
295                 hns3_err(hw, "Set fd key fail, ret=%d", ret);
296
297         return ret;
298 }
299
300 int hns3_init_fd_config(struct hns3_adapter *hns)
301 {
302         struct hns3_pf *pf = &hns->pf;
303         struct hns3_hw *hw = &hns->hw;
304         struct hns3_fd_key_cfg *key_cfg;
305         int ret;
306
307         ret = hns3_get_fd_mode(hw, &pf->fdir.fd_cfg.fd_mode);
308         if (ret)
309                 return ret;
310
311         switch (pf->fdir.fd_cfg.fd_mode) {
312         case HNS3_FD_MODE_DEPTH_2K_WIDTH_400B_STAGE_1:
313                 pf->fdir.fd_cfg.max_key_length = MAX_KEY_LENGTH;
314                 break;
315         case HNS3_FD_MODE_DEPTH_4K_WIDTH_200B_STAGE_1:
316                 pf->fdir.fd_cfg.max_key_length = MAX_200B_KEY_LENGTH;
317                 hns3_warn(hw, "Unsupported tunnel filter in 4K*200Bit");
318                 break;
319         default:
320                 hns3_err(hw, "Unsupported flow director mode %d",
321                             pf->fdir.fd_cfg.fd_mode);
322                 return -EOPNOTSUPP;
323         }
324
325         key_cfg = &pf->fdir.fd_cfg.key_cfg[HNS3_FD_STAGE_1];
326         key_cfg->key_sel = HNS3_FD_KEY_BASE_ON_TUPLE;
327         key_cfg->inner_sipv6_word_en = IPV6_ADDR_WORD_MASK;
328         key_cfg->inner_dipv6_word_en = IPV6_ADDR_WORD_MASK;
329         key_cfg->outer_sipv6_word_en = 0;
330         key_cfg->outer_dipv6_word_en = 0;
331
332         key_cfg->tuple_active = BIT(INNER_VLAN_TAG1) | BIT(INNER_ETH_TYPE) |
333             BIT(INNER_IP_PROTO) | BIT(INNER_IP_TOS) |
334             BIT(INNER_SRC_IP) | BIT(INNER_DST_IP) |
335             BIT(INNER_SRC_PORT) | BIT(INNER_DST_PORT);
336
337         /* If use max 400bit key, we can support tuples for ether type */
338         if (pf->fdir.fd_cfg.max_key_length == MAX_KEY_LENGTH) {
339                 key_cfg->tuple_active |=
340                     BIT(INNER_DST_MAC) | BIT(INNER_SRC_MAC) |
341                     BIT(OUTER_SRC_PORT) | BIT(INNER_SCTP_TAG) |
342                     BIT(OUTER_DST_PORT) | BIT(INNER_VLAN_TAG2) |
343                     BIT(OUTER_TUN_VNI) | BIT(OUTER_TUN_FLOW_ID) |
344                     BIT(OUTER_ETH_TYPE) | BIT(OUTER_IP_PROTO);
345         }
346
347         /* roce_type is used to filter roce frames
348          * dst_vport is used to specify the rule
349          */
350         key_cfg->meta_data_active = BIT(DST_VPORT) | BIT(TUNNEL_PACKET) |
351             BIT(VLAN_NUMBER);
352
353         ret = hns3_get_fd_allocation(hw,
354                                      &pf->fdir.fd_cfg.rule_num[HNS3_FD_STAGE_1],
355                                      &pf->fdir.fd_cfg.rule_num[HNS3_FD_STAGE_2],
356                                      &pf->fdir.fd_cfg.cnt_num[HNS3_FD_STAGE_1],
357                                      &pf->fdir.fd_cfg.cnt_num[HNS3_FD_STAGE_2]);
358         if (ret)
359                 return ret;
360
361         return hns3_set_fd_key_config(hns);
362 }
363
364 static int hns3_fd_tcam_config(struct hns3_hw *hw, bool sel_x, int loc,
365                                uint8_t *key, bool is_add)
366 {
367 #define FD_TCAM_CMD_NUM 3
368         struct hns3_fd_tcam_config_1_cmd *req1;
369         struct hns3_fd_tcam_config_2_cmd *req2;
370         struct hns3_fd_tcam_config_3_cmd *req3;
371         struct hns3_cmd_desc desc[FD_TCAM_CMD_NUM];
372         int len;
373         int ret;
374
375         hns3_cmd_setup_basic_desc(&desc[0], HNS3_OPC_FD_TCAM_OP, false);
376         desc[0].flag |= rte_cpu_to_le_16(HNS3_CMD_FLAG_NEXT);
377         hns3_cmd_setup_basic_desc(&desc[1], HNS3_OPC_FD_TCAM_OP, false);
378         desc[1].flag |= rte_cpu_to_le_16(HNS3_CMD_FLAG_NEXT);
379         hns3_cmd_setup_basic_desc(&desc[2], HNS3_OPC_FD_TCAM_OP, false);
380
381         req1 = (struct hns3_fd_tcam_config_1_cmd *)desc[0].data;
382         req2 = (struct hns3_fd_tcam_config_2_cmd *)desc[1].data;
383         req3 = (struct hns3_fd_tcam_config_3_cmd *)desc[2].data;
384
385         req1->stage = HNS3_FD_STAGE_1;
386         req1->xy_sel = sel_x ? 1 : 0;
387         hns3_set_bit(req1->port_info, HNS3_FD_EPORT_SW_EN_B, 0);
388         req1->index = rte_cpu_to_le_32(loc);
389         req1->entry_vld = sel_x ? is_add : 0;
390
391         if (key) {
392                 len = sizeof(req1->tcam_data);
393                 memcpy(req1->tcam_data, key, len);
394                 key += len;
395
396                 len = sizeof(req2->tcam_data);
397                 memcpy(req2->tcam_data, key, len);
398                 key += len;
399
400                 len = sizeof(req3->tcam_data);
401                 memcpy(req3->tcam_data, key, len);
402         }
403
404         ret = hns3_cmd_send(hw, desc, FD_TCAM_CMD_NUM);
405         if (ret)
406                 hns3_err(hw, "Config tcam key fail, ret=%d loc=%d add=%d",
407                             ret, loc, is_add);
408         return ret;
409 }
410
411 static int hns3_fd_ad_config(struct hns3_hw *hw, int loc,
412                              struct hns3_fd_ad_data *action)
413 {
414         struct hns3_fd_ad_config_cmd *req;
415         struct hns3_cmd_desc desc;
416         uint64_t ad_data = 0;
417         int ret;
418
419         hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_FD_AD_OP, false);
420
421         req = (struct hns3_fd_ad_config_cmd *)desc.data;
422         req->index = rte_cpu_to_le_32(loc);
423         req->stage = HNS3_FD_STAGE_1;
424
425         hns3_set_bit(ad_data, HNS3_FD_AD_WR_RULE_ID_B,
426                      action->write_rule_id_to_bd);
427         hns3_set_field(ad_data, HNS3_FD_AD_RULE_ID_M, HNS3_FD_AD_RULE_ID_S,
428                        action->rule_id);
429         /* set extend bit if counter_id is in [128 ~ 255] */
430         if (action->counter_id & BIT(HNS3_FD_AD_COUNTER_HIGH_BIT))
431                 hns3_set_bit(ad_data, HNS3_FD_AD_COUNTER_HIGH_BIT_B, 1);
432         ad_data <<= HNS3_FD_AD_DATA_S;
433         hns3_set_bit(ad_data, HNS3_FD_AD_DROP_B, action->drop_packet);
434         hns3_set_bit(ad_data, HNS3_FD_AD_DIRECT_QID_B,
435                      action->forward_to_direct_queue);
436         hns3_set_field(ad_data, HNS3_FD_AD_QID_M, HNS3_FD_AD_QID_S,
437                        action->queue_id);
438         hns3_set_bit(ad_data, HNS3_FD_AD_USE_COUNTER_B, action->use_counter);
439         hns3_set_field(ad_data, HNS3_FD_AD_COUNTER_NUM_M,
440                        HNS3_FD_AD_COUNTER_NUM_S, action->counter_id);
441         hns3_set_bit(ad_data, HNS3_FD_AD_NXT_STEP_B, action->use_next_stage);
442         hns3_set_field(ad_data, HNS3_FD_AD_NXT_KEY_M, HNS3_FD_AD_NXT_KEY_S,
443                        action->counter_id);
444
445         req->ad_data = rte_cpu_to_le_64(ad_data);
446         ret = hns3_cmd_send(hw, &desc, 1);
447         if (ret)
448                 hns3_err(hw, "Config fd ad fail, ret=%d loc=%d", ret, loc);
449
450         return ret;
451 }
452
453 static inline void hns3_fd_convert_mac(uint8_t *key, uint8_t *mask,
454                                        uint8_t *mac_x, uint8_t *mac_y)
455 {
456         uint8_t tmp;
457         int i;
458
459         for (i = 0; i < RTE_ETHER_ADDR_LEN; i++) {
460                 tmp = RTE_ETHER_ADDR_LEN - 1 - i;
461                 calc_x(mac_x[tmp], key[i], mask[i]);
462                 calc_y(mac_y[tmp], key[i], mask[i]);
463         }
464 }
465
466 static void hns3_fd_convert_int16(uint32_t tuple, struct hns3_fdir_rule *rule,
467                                   uint8_t *val_x, uint8_t *val_y)
468 {
469         uint16_t tmp_x_s;
470         uint16_t tmp_y_s;
471         uint16_t mask;
472         uint16_t key;
473
474         switch (tuple) {
475         case OUTER_SRC_PORT:
476                 key = rule->key_conf.spec.outer_src_port;
477                 mask = rule->key_conf.mask.outer_src_port;
478                 break;
479         case OUTER_DST_PORT:
480                 key = rule->key_conf.spec.tunnel_type;
481                 mask = rule->key_conf.mask.tunnel_type;
482                 break;
483         case OUTER_ETH_TYPE:
484                 key = rule->key_conf.spec.outer_ether_type;
485                 mask = rule->key_conf.mask.outer_ether_type;
486                 break;
487         case INNER_SRC_PORT:
488                 key = rule->key_conf.spec.src_port;
489                 mask = rule->key_conf.mask.src_port;
490                 break;
491         case INNER_DST_PORT:
492                 key = rule->key_conf.spec.dst_port;
493                 mask = rule->key_conf.mask.dst_port;
494                 break;
495         case INNER_VLAN_TAG1:
496                 key = rule->key_conf.spec.vlan_tag1;
497                 mask = rule->key_conf.mask.vlan_tag1;
498                 break;
499         case INNER_VLAN_TAG2:
500                 key = rule->key_conf.spec.vlan_tag2;
501                 mask = rule->key_conf.mask.vlan_tag2;
502                 break;
503         default:
504                 /*  INNER_ETH_TYPE: */
505                 key = rule->key_conf.spec.ether_type;
506                 mask = rule->key_conf.mask.ether_type;
507                 break;
508         }
509         calc_x(tmp_x_s, key, mask);
510         calc_y(tmp_y_s, key, mask);
511         val_x[0] = rte_cpu_to_le_16(tmp_x_s) & 0xFF;
512         val_x[1] = rte_cpu_to_le_16(tmp_x_s) >> HNS3_BITS_PER_BYTE;
513         val_y[0] = rte_cpu_to_le_16(tmp_y_s) & 0xFF;
514         val_y[1] = rte_cpu_to_le_16(tmp_y_s) >> HNS3_BITS_PER_BYTE;
515 }
516
517 static inline void hns3_fd_convert_int32(uint32_t key, uint32_t mask,
518                                          uint8_t *val_x, uint8_t *val_y)
519 {
520         uint32_t tmp_x_l;
521         uint32_t tmp_y_l;
522
523         calc_x(tmp_x_l, key, mask);
524         calc_y(tmp_y_l, key, mask);
525         memcpy(val_x, &tmp_x_l, sizeof(tmp_x_l));
526         memcpy(val_y, &tmp_y_l, sizeof(tmp_y_l));
527 }
528
529 static bool hns3_fd_convert_tuple(struct hns3_hw *hw,
530                                   uint32_t tuple, uint8_t *key_x,
531                                   uint8_t *key_y, struct hns3_fdir_rule *rule)
532 {
533         struct hns3_fdir_key_conf *key_conf;
534         int tmp;
535         int i;
536
537         if ((rule->input_set & BIT(tuple)) == 0)
538                 return true;
539
540         key_conf = &rule->key_conf;
541         switch (tuple) {
542         case INNER_DST_MAC:
543                 hns3_fd_convert_mac(key_conf->spec.dst_mac,
544                                     key_conf->mask.dst_mac, key_x, key_y);
545                 break;
546         case INNER_SRC_MAC:
547                 hns3_fd_convert_mac(key_conf->spec.src_mac,
548                                     key_conf->mask.src_mac, key_x, key_y);
549                 break;
550         case OUTER_SRC_PORT:
551         case OUTER_DST_PORT:
552         case OUTER_ETH_TYPE:
553         case INNER_SRC_PORT:
554         case INNER_DST_PORT:
555         case INNER_VLAN_TAG1:
556         case INNER_VLAN_TAG2:
557         case INNER_ETH_TYPE:
558                 hns3_fd_convert_int16(tuple, rule, key_x, key_y);
559                 break;
560         case INNER_SRC_IP:
561                 hns3_fd_convert_int32(key_conf->spec.src_ip[IP_ADDR_KEY_ID],
562                                       key_conf->mask.src_ip[IP_ADDR_KEY_ID],
563                                       key_x, key_y);
564                 break;
565         case INNER_DST_IP:
566                 hns3_fd_convert_int32(key_conf->spec.dst_ip[IP_ADDR_KEY_ID],
567                                       key_conf->mask.dst_ip[IP_ADDR_KEY_ID],
568                                       key_x, key_y);
569                 break;
570         case INNER_SCTP_TAG:
571                 hns3_fd_convert_int32(key_conf->spec.sctp_tag,
572                                       key_conf->mask.sctp_tag, key_x, key_y);
573                 break;
574         case OUTER_TUN_VNI:
575                 for (i = 0; i < VNI_OR_TNI_LEN; i++) {
576                         tmp = VNI_OR_TNI_LEN - 1 - i;
577                         calc_x(key_x[tmp],
578                                key_conf->spec.outer_tun_vni[i],
579                                key_conf->mask.outer_tun_vni[i]);
580                         calc_y(key_y[tmp],
581                                key_conf->spec.outer_tun_vni[i],
582                                key_conf->mask.outer_tun_vni[i]);
583                 }
584                 break;
585         case OUTER_TUN_FLOW_ID:
586                 calc_x(*key_x, key_conf->spec.outer_tun_flow_id,
587                        key_conf->mask.outer_tun_flow_id);
588                 calc_y(*key_y, key_conf->spec.outer_tun_flow_id,
589                        key_conf->mask.outer_tun_flow_id);
590                 break;
591         case INNER_IP_TOS:
592                 calc_x(*key_x, key_conf->spec.ip_tos, key_conf->mask.ip_tos);
593                 calc_y(*key_y, key_conf->spec.ip_tos, key_conf->mask.ip_tos);
594                 break;
595         case OUTER_IP_PROTO:
596                 calc_x(*key_x, key_conf->spec.outer_proto,
597                        key_conf->mask.outer_proto);
598                 calc_y(*key_y, key_conf->spec.outer_proto,
599                        key_conf->mask.outer_proto);
600                 break;
601         case INNER_IP_PROTO:
602                 calc_x(*key_x, key_conf->spec.ip_proto,
603                        key_conf->mask.ip_proto);
604                 calc_y(*key_y, key_conf->spec.ip_proto,
605                        key_conf->mask.ip_proto);
606                 break;
607         default:
608                 hns3_warn(hw, "not support tuple of (%d)", tuple);
609                 break;
610         }
611         return true;
612 }
613
614 static uint32_t hns3_get_port_number(uint8_t pf_id, uint8_t vf_id)
615 {
616         uint32_t port_number = 0;
617
618         hns3_set_field(port_number, HNS3_PF_ID_M, HNS3_PF_ID_S, pf_id);
619         hns3_set_field(port_number, HNS3_VF_ID_M, HNS3_VF_ID_S, vf_id);
620         hns3_set_bit(port_number, HNS3_PORT_TYPE_B, HOST_PORT);
621
622         return port_number;
623 }
624
625 static void hns3_fd_convert_meta_data(struct hns3_fd_key_cfg *cfg,
626                                       uint8_t vf_id,
627                                       struct hns3_fdir_rule *rule,
628                                       uint8_t *key_x, uint8_t *key_y)
629 {
630         uint16_t meta_data = 0;
631         uint32_t port_number;
632         uint8_t cur_pos = 0;
633         uint8_t tuple_size;
634         uint8_t shift_bits;
635         uint32_t tmp_x;
636         uint32_t tmp_y;
637         uint8_t i;
638
639         for (i = 0; i < MAX_META_DATA; i++) {
640                 if ((cfg->meta_data_active & BIT(i)) == 0)
641                         continue;
642
643                 tuple_size = meta_data_key_info[i].key_length;
644                 if (i == TUNNEL_PACKET) {
645                         hns3_set_bit(meta_data, cur_pos,
646                                      rule->key_conf.spec.tunnel_type ? 1 : 0);
647                         cur_pos += tuple_size;
648                 } else if (i == VLAN_NUMBER) {
649                         uint32_t vlan_tag;
650                         uint8_t vlan_num;
651                         if (rule->key_conf.spec.tunnel_type == 0)
652                                 vlan_num = rule->key_conf.vlan_num;
653                         else
654                                 vlan_num = rule->key_conf.outer_vlan_num;
655                         if (vlan_num == 1)
656                                 vlan_tag = HNS3_VLAN_TAG_TYPE_TAG1;
657                         else if (vlan_num == VLAN_TAG_NUM_MAX)
658                                 vlan_tag = HNS3_VLAN_TAG_TYPE_TAG1_2;
659                         else
660                                 vlan_tag = HNS3_VLAN_TAG_TYPE_NONE;
661                         hns3_set_field(meta_data,
662                                        GENMASK(cur_pos + tuple_size,
663                                                cur_pos), cur_pos, vlan_tag);
664                         cur_pos += tuple_size;
665                 } else if (i == DST_VPORT) {
666                         port_number = hns3_get_port_number(0, vf_id);
667                         hns3_set_field(meta_data,
668                                        GENMASK(cur_pos + tuple_size, cur_pos),
669                                        cur_pos, port_number);
670                         cur_pos += tuple_size;
671                 }
672         }
673
674         calc_x(tmp_x, meta_data, 0xFFFF);
675         calc_y(tmp_y, meta_data, 0xFFFF);
676         shift_bits = sizeof(meta_data) * HNS3_BITS_PER_BYTE - cur_pos;
677
678         tmp_x = rte_cpu_to_le_32(tmp_x << shift_bits);
679         tmp_y = rte_cpu_to_le_32(tmp_y << shift_bits);
680         key_x[0] = tmp_x & 0xFF;
681         key_x[1] = (tmp_x >> HNS3_BITS_PER_BYTE) & 0xFF;
682         key_y[0] = tmp_y & 0xFF;
683         key_y[1] = (tmp_y >> HNS3_BITS_PER_BYTE) & 0xFF;
684 }
685
686 /* A complete key is combined with meta data key and tuple key.
687  * Meta data key is stored at the MSB region, and tuple key is stored at
688  * the LSB region, unused bits will be filled 0.
689  */
690 static int hns3_config_key(struct hns3_adapter *hns,
691                            struct hns3_fdir_rule *rule)
692 {
693         struct hns3_pf *pf = &hns->pf;
694         struct hns3_hw *hw = &hns->hw;
695         struct hns3_fd_key_cfg *key_cfg;
696         uint8_t *cur_key_x;
697         uint8_t *cur_key_y;
698         uint8_t key_x[MAX_KEY_BYTES] __rte_aligned(4);
699         uint8_t key_y[MAX_KEY_BYTES] __rte_aligned(4);
700         uint8_t vf_id = rule->vf_id;
701         uint8_t meta_data_region;
702         uint8_t tuple_size;
703         uint8_t i;
704         int ret;
705
706         memset(key_x, 0, sizeof(key_x));
707         memset(key_y, 0, sizeof(key_y));
708         cur_key_x = key_x;
709         cur_key_y = key_y;
710
711         key_cfg = &pf->fdir.fd_cfg.key_cfg[HNS3_FD_STAGE_1];
712         for (i = 0; i < MAX_TUPLE; i++) {
713                 bool tuple_valid;
714
715                 tuple_size = tuple_key_info[i].key_length / HNS3_BITS_PER_BYTE;
716                 if (key_cfg->tuple_active & BIT(i)) {
717                         tuple_valid = hns3_fd_convert_tuple(hw, i, cur_key_x,
718                                                             cur_key_y, rule);
719                         if (tuple_valid) {
720                                 cur_key_x += tuple_size;
721                                 cur_key_y += tuple_size;
722                         }
723                 }
724         }
725
726         meta_data_region = pf->fdir.fd_cfg.max_key_length / HNS3_BITS_PER_BYTE -
727             MAX_META_DATA_LENGTH / HNS3_BITS_PER_BYTE;
728
729         hns3_fd_convert_meta_data(key_cfg, vf_id, rule,
730                                   key_x + meta_data_region,
731                                   key_y + meta_data_region);
732
733         ret = hns3_fd_tcam_config(hw, false, rule->location, key_y, true);
734         if (ret) {
735                 hns3_err(hw, "Config fd key_y fail, loc=%d, ret=%d",
736                             rule->queue_id, ret);
737                 return ret;
738         }
739
740         ret = hns3_fd_tcam_config(hw, true, rule->location, key_x, true);
741         if (ret)
742                 hns3_err(hw, "Config fd key_x fail, loc=%d, ret=%d",
743                             rule->queue_id, ret);
744         return ret;
745 }
746
747 static int hns3_config_action(struct hns3_hw *hw, struct hns3_fdir_rule *rule)
748 {
749         struct hns3_fd_ad_data ad_data;
750
751         ad_data.ad_id = rule->location;
752
753         if (rule->action == HNS3_FD_ACTION_DROP_PACKET) {
754                 ad_data.drop_packet = true;
755                 ad_data.forward_to_direct_queue = false;
756                 ad_data.queue_id = 0;
757         } else {
758                 ad_data.drop_packet = false;
759                 ad_data.forward_to_direct_queue = true;
760                 ad_data.queue_id = rule->queue_id;
761         }
762
763         if (unlikely(rule->flags & HNS3_RULE_FLAG_COUNTER)) {
764                 ad_data.use_counter = true;
765                 ad_data.counter_id = rule->act_cnt.id;
766         } else {
767                 ad_data.use_counter = false;
768                 ad_data.counter_id = 0;
769         }
770
771         if (unlikely(rule->flags & HNS3_RULE_FLAG_FDID))
772                 ad_data.rule_id = rule->fd_id;
773         else
774                 ad_data.rule_id = rule->location;
775
776         ad_data.use_next_stage = false;
777         ad_data.next_input_key = 0;
778
779         ad_data.write_rule_id_to_bd = true;
780
781         return hns3_fd_ad_config(hw, ad_data.ad_id, &ad_data);
782 }
783
784 static int hns3_fd_clear_all_rules(struct hns3_hw *hw, uint32_t rule_num)
785 {
786         uint32_t i;
787         int ret;
788
789         for (i = 0; i < rule_num; i++) {
790                 ret = hns3_fd_tcam_config(hw, true, i, NULL, false);
791                 if (ret)
792                         return ret;
793         }
794
795         return 0;
796 }
797
798 int hns3_fdir_filter_init(struct hns3_adapter *hns)
799 {
800         struct hns3_pf *pf = &hns->pf;
801         struct hns3_fdir_info *fdir_info = &pf->fdir;
802         uint32_t rule_num = fdir_info->fd_cfg.rule_num[HNS3_FD_STAGE_1];
803         char fdir_hash_name[RTE_HASH_NAMESIZE];
804         struct rte_hash_parameters fdir_hash_params = {
805                 .name = fdir_hash_name,
806                 .entries = rule_num,
807                 .key_len = sizeof(struct hns3_fdir_key_conf),
808                 .hash_func = rte_hash_crc,
809                 .hash_func_init_val = 0,
810         };
811         int ret;
812
813         ret = hns3_fd_clear_all_rules(&hns->hw, rule_num);
814         if (ret) {
815                 PMD_INIT_LOG(ERR, "Clear all fd rules fail! ret = %d", ret);
816                 return ret;
817         }
818
819         fdir_hash_params.socket_id = rte_socket_id();
820         TAILQ_INIT(&fdir_info->fdir_list);
821         rte_spinlock_init(&fdir_info->flows_lock);
822         snprintf(fdir_hash_name, RTE_HASH_NAMESIZE, "%s", hns->hw.data->name);
823         fdir_info->hash_handle = rte_hash_create(&fdir_hash_params);
824         if (fdir_info->hash_handle == NULL) {
825                 PMD_INIT_LOG(ERR, "Create FDIR hash handle fail!");
826                 return -EINVAL;
827         }
828         fdir_info->hash_map = rte_zmalloc("hns3 FDIR hash",
829                                           rule_num *
830                                           sizeof(struct hns3_fdir_rule_ele *),
831                                           0);
832         if (fdir_info->hash_map == NULL) {
833                 PMD_INIT_LOG(ERR, "Allocate memory for FDIR hash map fail!");
834                 rte_hash_free(fdir_info->hash_handle);
835                 return -ENOMEM;
836         }
837
838         return 0;
839 }
840
841 void hns3_fdir_filter_uninit(struct hns3_adapter *hns)
842 {
843         struct hns3_pf *pf = &hns->pf;
844         struct hns3_fdir_info *fdir_info = &pf->fdir;
845         struct hns3_fdir_rule_ele *fdir_filter;
846
847         rte_spinlock_lock(&fdir_info->flows_lock);
848         if (fdir_info->hash_map) {
849                 rte_free(fdir_info->hash_map);
850                 fdir_info->hash_map = NULL;
851         }
852         if (fdir_info->hash_handle) {
853                 rte_hash_free(fdir_info->hash_handle);
854                 fdir_info->hash_handle = NULL;
855         }
856         rte_spinlock_unlock(&fdir_info->flows_lock);
857
858         fdir_filter = TAILQ_FIRST(&fdir_info->fdir_list);
859         while (fdir_filter) {
860                 TAILQ_REMOVE(&fdir_info->fdir_list, fdir_filter, entries);
861                 hns3_fd_tcam_config(&hns->hw, true,
862                                     fdir_filter->fdir_conf.location, NULL,
863                                     false);
864                 rte_free(fdir_filter);
865                 fdir_filter = TAILQ_FIRST(&fdir_info->fdir_list);
866         }
867 }
868
869 /*
870  * Find a key in the hash table.
871  * @return
872  *   - Zero and positive values are key location.
873  *   - -EINVAL if the parameters are invalid.
874  *   - -ENOENT if the key is not found.
875  */
876 static int hns3_fdir_filter_lookup(struct hns3_fdir_info *fdir_info,
877                                     struct hns3_fdir_key_conf *key)
878 {
879         hash_sig_t sig;
880         int ret;
881
882         rte_spinlock_lock(&fdir_info->flows_lock);
883         sig = rte_hash_crc(key, sizeof(*key), 0);
884         ret = rte_hash_lookup_with_hash(fdir_info->hash_handle, key, sig);
885         rte_spinlock_unlock(&fdir_info->flows_lock);
886
887         return ret;
888 }
889
890 static int hns3_insert_fdir_filter(struct hns3_hw *hw,
891                                    struct hns3_fdir_info *fdir_info,
892                                    struct hns3_fdir_rule_ele *fdir_filter)
893 {
894         struct hns3_fdir_key_conf *key;
895         hash_sig_t sig;
896         int ret;
897
898         key = &fdir_filter->fdir_conf.key_conf;
899         rte_spinlock_lock(&fdir_info->flows_lock);
900         sig = rte_hash_crc(key, sizeof(*key), 0);
901         ret = rte_hash_add_key_with_hash(fdir_info->hash_handle, key, sig);
902         if (ret < 0) {
903                 rte_spinlock_unlock(&fdir_info->flows_lock);
904                 hns3_err(hw, "Hash table full? err:%d(%s)!", ret,
905                          strerror(-ret));
906                 return ret;
907         }
908
909         fdir_info->hash_map[ret] = fdir_filter;
910         TAILQ_INSERT_TAIL(&fdir_info->fdir_list, fdir_filter, entries);
911         rte_spinlock_unlock(&fdir_info->flows_lock);
912
913         return ret;
914 }
915
916 static int hns3_remove_fdir_filter(struct hns3_hw *hw,
917                                    struct hns3_fdir_info *fdir_info,
918                                    struct hns3_fdir_key_conf *key)
919 {
920         struct hns3_fdir_rule_ele *fdir_filter;
921         hash_sig_t sig;
922         int ret;
923
924         rte_spinlock_lock(&fdir_info->flows_lock);
925         sig = rte_hash_crc(key, sizeof(*key), 0);
926         ret = rte_hash_del_key_with_hash(fdir_info->hash_handle, key, sig);
927         if (ret < 0) {
928                 rte_spinlock_unlock(&fdir_info->flows_lock);
929                 hns3_err(hw, "Delete hash key fail ret=%d", ret);
930                 return ret;
931         }
932
933         fdir_filter = fdir_info->hash_map[ret];
934         fdir_info->hash_map[ret] = NULL;
935         TAILQ_REMOVE(&fdir_info->fdir_list, fdir_filter, entries);
936         rte_spinlock_unlock(&fdir_info->flows_lock);
937
938         rte_free(fdir_filter);
939
940         return 0;
941 }
942
943 int hns3_fdir_filter_program(struct hns3_adapter *hns,
944                              struct hns3_fdir_rule *rule, bool del)
945 {
946         struct hns3_pf *pf = &hns->pf;
947         struct hns3_fdir_info *fdir_info = &pf->fdir;
948         struct hns3_fdir_rule_ele *node;
949         struct hns3_hw *hw = &hns->hw;
950         int ret;
951
952         if (del) {
953                 ret = hns3_fd_tcam_config(hw, true, rule->location, NULL,
954                                           false);
955                 if (ret)
956                         hns3_err(hw, "Failed to delete fdir: %d src_ip:%x "
957                                  "dst_ip:%x src_port:%d dst_port:%d ret = %d",
958                                  rule->location,
959                                  rule->key_conf.spec.src_ip[IP_ADDR_KEY_ID],
960                                  rule->key_conf.spec.dst_ip[IP_ADDR_KEY_ID],
961                                  rule->key_conf.spec.src_port,
962                                  rule->key_conf.spec.dst_port, ret);
963                 else
964                         hns3_remove_fdir_filter(hw, fdir_info, &rule->key_conf);
965
966                 return ret;
967         }
968
969         ret = hns3_fdir_filter_lookup(fdir_info, &rule->key_conf);
970         if (ret >= 0) {
971                 hns3_err(hw, "Conflict with existing fdir loc: %d", ret);
972                 return -EINVAL;
973         }
974
975         node = rte_zmalloc("hns3 fdir rule", sizeof(struct hns3_fdir_rule_ele),
976                            0);
977         if (node == NULL) {
978                 hns3_err(hw, "Failed to allocate fdir_rule memory");
979                 return -ENOMEM;
980         }
981
982         rte_memcpy(&node->fdir_conf, rule, sizeof(struct hns3_fdir_rule));
983         ret = hns3_insert_fdir_filter(hw, fdir_info, node);
984         if (ret < 0) {
985                 rte_free(node);
986                 return ret;
987         }
988         rule->location = ret;
989         node->fdir_conf.location = ret;
990
991         rte_spinlock_lock(&fdir_info->flows_lock);
992         ret = hns3_config_action(hw, rule);
993         if (!ret)
994                 ret = hns3_config_key(hns, rule);
995         rte_spinlock_unlock(&fdir_info->flows_lock);
996         if (ret) {
997                 hns3_err(hw, "Failed to config fdir: %d src_ip:%x dst_ip:%x "
998                          "src_port:%d dst_port:%d ret = %d",
999                          rule->location,
1000                          rule->key_conf.spec.src_ip[IP_ADDR_KEY_ID],
1001                          rule->key_conf.spec.dst_ip[IP_ADDR_KEY_ID],
1002                          rule->key_conf.spec.src_port,
1003                          rule->key_conf.spec.dst_port, ret);
1004                 (void)hns3_remove_fdir_filter(hw, fdir_info, &rule->key_conf);
1005         }
1006
1007         return ret;
1008 }
1009
1010 /* remove all the flow director filters */
1011 int hns3_clear_all_fdir_filter(struct hns3_adapter *hns)
1012 {
1013         struct hns3_pf *pf = &hns->pf;
1014         struct hns3_fdir_info *fdir_info = &pf->fdir;
1015         struct hns3_fdir_rule_ele *fdir_filter;
1016         struct hns3_hw *hw = &hns->hw;
1017         int ret = 0;
1018
1019         /* flush flow director */
1020         rte_spinlock_lock(&fdir_info->flows_lock);
1021         rte_hash_reset(fdir_info->hash_handle);
1022         rte_spinlock_unlock(&fdir_info->flows_lock);
1023
1024         fdir_filter = TAILQ_FIRST(&fdir_info->fdir_list);
1025         while (fdir_filter) {
1026                 TAILQ_REMOVE(&fdir_info->fdir_list, fdir_filter, entries);
1027                 ret += hns3_fd_tcam_config(hw, true,
1028                                            fdir_filter->fdir_conf.location,
1029                                            NULL, false);
1030                 rte_free(fdir_filter);
1031                 fdir_filter = TAILQ_FIRST(&fdir_info->fdir_list);
1032         }
1033
1034         if (ret) {
1035                 hns3_err(hw, "Fail to delete FDIR filter, ret = %d", ret);
1036                 ret = -EIO;
1037         }
1038         return ret;
1039 }
1040
1041 int hns3_restore_all_fdir_filter(struct hns3_adapter *hns)
1042 {
1043         struct hns3_pf *pf = &hns->pf;
1044         struct hns3_fdir_info *fdir_info = &pf->fdir;
1045         struct hns3_fdir_rule_ele *fdir_filter;
1046         struct hns3_hw *hw = &hns->hw;
1047         bool err = false;
1048         int ret;
1049
1050         TAILQ_FOREACH(fdir_filter, &fdir_info->fdir_list, entries) {
1051                 ret = hns3_config_action(hw, &fdir_filter->fdir_conf);
1052                 if (!ret)
1053                         ret = hns3_config_key(hns, &fdir_filter->fdir_conf);
1054                 if (ret) {
1055                         err = true;
1056                         if (ret == -EBUSY)
1057                                 break;
1058                 }
1059         }
1060
1061         if (err) {
1062                 hns3_err(hw, "Fail to restore FDIR filter, ret = %d", ret);
1063                 return -EIO;
1064         }
1065         return 0;
1066 }
1067
1068 int hns3_get_count(struct hns3_hw *hw, uint32_t id, uint64_t *value)
1069 {
1070         struct hns3_fd_get_cnt_cmd *req;
1071         struct hns3_cmd_desc desc;
1072         int ret;
1073
1074         hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_FD_COUNTER_OP, true);
1075
1076         req = (struct hns3_fd_get_cnt_cmd *)desc.data;
1077         req->stage = HNS3_FD_STAGE_1;
1078         req->index = rte_cpu_to_le_32(id);
1079
1080         ret = hns3_cmd_send(hw, &desc, 1);
1081         if (ret) {
1082                 hns3_err(hw, "Read counter fail, ret=%d", ret);
1083                 return ret;
1084         }
1085
1086         *value = req->value;
1087
1088         return ret;
1089 }