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