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