net/ice: refactor flow pattern parser
[dpdk.git] / drivers / net / ice / ice_fdir_filter.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2019 Intel Corporation
3  */
4
5 #include <stdio.h>
6 #include <rte_flow.h>
7 #include <rte_hash.h>
8 #include <rte_hash_crc.h>
9 #include "base/ice_fdir.h"
10 #include "base/ice_flow.h"
11 #include "base/ice_type.h"
12 #include "ice_ethdev.h"
13 #include "ice_rxtx.h"
14 #include "ice_generic_flow.h"
15
16 #define ICE_FDIR_IPV6_TC_OFFSET         20
17 #define ICE_IPV6_TC_MASK                (0xFF << ICE_FDIR_IPV6_TC_OFFSET)
18
19 #define ICE_FDIR_MAX_QREGION_SIZE       128
20
21 #define ICE_FDIR_INSET_ETH (\
22         ICE_INSET_DMAC | ICE_INSET_SMAC | ICE_INSET_ETHERTYPE)
23
24 #define ICE_FDIR_INSET_ETH_IPV4 (\
25         ICE_FDIR_INSET_ETH | \
26         ICE_INSET_IPV4_SRC | ICE_INSET_IPV4_DST | ICE_INSET_IPV4_TOS | \
27         ICE_INSET_IPV4_TTL | ICE_INSET_IPV4_PROTO)
28
29 #define ICE_FDIR_INSET_ETH_IPV4_UDP (\
30         ICE_FDIR_INSET_ETH_IPV4 | \
31         ICE_INSET_UDP_SRC_PORT | ICE_INSET_UDP_DST_PORT)
32
33 #define ICE_FDIR_INSET_ETH_IPV4_TCP (\
34         ICE_FDIR_INSET_ETH_IPV4 | \
35         ICE_INSET_TCP_SRC_PORT | ICE_INSET_TCP_DST_PORT)
36
37 #define ICE_FDIR_INSET_ETH_IPV4_SCTP (\
38         ICE_FDIR_INSET_ETH_IPV4 | \
39         ICE_INSET_SCTP_SRC_PORT | ICE_INSET_SCTP_DST_PORT)
40
41 #define ICE_FDIR_INSET_ETH_IPV6 (\
42         ICE_INSET_DMAC | \
43         ICE_INSET_IPV6_SRC | ICE_INSET_IPV6_DST | ICE_INSET_IPV6_TC | \
44         ICE_INSET_IPV6_HOP_LIMIT | ICE_INSET_IPV6_NEXT_HDR)
45
46 #define ICE_FDIR_INSET_ETH_IPV6_UDP (\
47         ICE_FDIR_INSET_ETH_IPV6 | \
48         ICE_INSET_UDP_SRC_PORT | ICE_INSET_UDP_DST_PORT)
49
50 #define ICE_FDIR_INSET_ETH_IPV6_TCP (\
51         ICE_FDIR_INSET_ETH_IPV6 | \
52         ICE_INSET_TCP_SRC_PORT | ICE_INSET_TCP_DST_PORT)
53
54 #define ICE_FDIR_INSET_ETH_IPV6_SCTP (\
55         ICE_FDIR_INSET_ETH_IPV6 | \
56         ICE_INSET_SCTP_SRC_PORT | ICE_INSET_SCTP_DST_PORT)
57
58 #define ICE_FDIR_INSET_VXLAN_IPV4 (\
59         ICE_INSET_IPV4_SRC | ICE_INSET_IPV4_DST)
60
61 #define ICE_FDIR_INSET_VXLAN_IPV4_TCP (\
62         ICE_FDIR_INSET_VXLAN_IPV4 | \
63         ICE_INSET_TCP_SRC_PORT | ICE_INSET_TCP_DST_PORT)
64
65 #define ICE_FDIR_INSET_VXLAN_IPV4_UDP (\
66         ICE_FDIR_INSET_VXLAN_IPV4 | \
67         ICE_INSET_UDP_SRC_PORT | ICE_INSET_UDP_DST_PORT)
68
69 #define ICE_FDIR_INSET_VXLAN_IPV4_SCTP (\
70         ICE_FDIR_INSET_VXLAN_IPV4 | \
71         ICE_INSET_SCTP_SRC_PORT | ICE_INSET_SCTP_DST_PORT)
72
73 #define ICE_FDIR_INSET_IPV4_GTPU (\
74         ICE_INSET_IPV4_SRC | ICE_INSET_IPV4_DST | ICE_INSET_GTPU_TEID)
75
76 #define ICE_FDIR_INSET_IPV4_GTPU_EH (\
77         ICE_INSET_IPV4_SRC | ICE_INSET_IPV4_DST | \
78         ICE_INSET_GTPU_TEID | ICE_INSET_GTPU_QFI)
79
80 #define ICE_FDIR_INSET_IPV6_GTPU (\
81         ICE_INSET_IPV6_SRC | ICE_INSET_IPV6_DST | ICE_INSET_GTPU_TEID)
82
83 #define ICE_FDIR_INSET_IPV6_GTPU_EH (\
84         ICE_INSET_IPV6_SRC | ICE_INSET_IPV6_DST | \
85         ICE_INSET_GTPU_TEID | ICE_INSET_GTPU_QFI)
86
87 static struct ice_pattern_match_item ice_fdir_pattern_list[] = {
88         {pattern_ethertype,            ICE_FDIR_INSET_ETH,                   ICE_INSET_NONE},
89         {pattern_eth_ipv4,             ICE_FDIR_INSET_ETH_IPV4,              ICE_INSET_NONE},
90         {pattern_eth_ipv4_udp,         ICE_FDIR_INSET_ETH_IPV4_UDP,          ICE_INSET_NONE},
91         {pattern_eth_ipv4_tcp,         ICE_FDIR_INSET_ETH_IPV4_TCP,          ICE_INSET_NONE},
92         {pattern_eth_ipv4_sctp,        ICE_FDIR_INSET_ETH_IPV4_SCTP,         ICE_INSET_NONE},
93         {pattern_eth_ipv6,             ICE_FDIR_INSET_ETH_IPV6,              ICE_INSET_NONE},
94         {pattern_eth_ipv6_udp,         ICE_FDIR_INSET_ETH_IPV6_UDP,          ICE_INSET_NONE},
95         {pattern_eth_ipv6_tcp,         ICE_FDIR_INSET_ETH_IPV6_TCP,          ICE_INSET_NONE},
96         {pattern_eth_ipv6_sctp,        ICE_FDIR_INSET_ETH_IPV6_SCTP,         ICE_INSET_NONE},
97         {pattern_eth_ipv4_udp_vxlan_ipv4,
98                                        ICE_FDIR_INSET_VXLAN_IPV4,            ICE_INSET_NONE},
99         {pattern_eth_ipv4_udp_vxlan_ipv4_udp,
100                                        ICE_FDIR_INSET_VXLAN_IPV4_UDP,        ICE_INSET_NONE},
101         {pattern_eth_ipv4_udp_vxlan_ipv4_tcp,
102                                        ICE_FDIR_INSET_VXLAN_IPV4_TCP,        ICE_INSET_NONE},
103         {pattern_eth_ipv4_udp_vxlan_ipv4_sctp,
104                                        ICE_FDIR_INSET_VXLAN_IPV4_SCTP,       ICE_INSET_NONE},
105         {pattern_eth_ipv4_udp_vxlan_eth_ipv4,
106                                        ICE_FDIR_INSET_VXLAN_IPV4,            ICE_INSET_NONE},
107         {pattern_eth_ipv4_udp_vxlan_eth_ipv4_udp,
108                                        ICE_FDIR_INSET_VXLAN_IPV4_UDP,        ICE_INSET_NONE},
109         {pattern_eth_ipv4_udp_vxlan_eth_ipv4_tcp,
110                                        ICE_FDIR_INSET_VXLAN_IPV4_TCP,        ICE_INSET_NONE},
111         {pattern_eth_ipv4_udp_vxlan_eth_ipv4_sctp,
112                                        ICE_FDIR_INSET_VXLAN_IPV4_SCTP,       ICE_INSET_NONE},
113         {pattern_eth_ipv4_gtpu,        ICE_FDIR_INSET_IPV4_GTPU,             ICE_INSET_NONE},
114         {pattern_eth_ipv4_gtpu_eh,     ICE_FDIR_INSET_IPV4_GTPU_EH,          ICE_INSET_NONE},
115         {pattern_eth_ipv6_gtpu,        ICE_FDIR_INSET_IPV6_GTPU,             ICE_INSET_NONE},
116         {pattern_eth_ipv6_gtpu_eh,     ICE_FDIR_INSET_IPV6_GTPU_EH,          ICE_INSET_NONE},
117 };
118
119 static struct ice_flow_parser ice_fdir_parser;
120
121 static int
122 ice_fdir_is_tunnel_profile(enum ice_fdir_tunnel_type tunnel_type);
123
124 static const struct rte_memzone *
125 ice_memzone_reserve(const char *name, uint32_t len, int socket_id)
126 {
127         const struct rte_memzone *mz;
128
129         mz = rte_memzone_lookup(name);
130         if (mz)
131                 return mz;
132
133         return rte_memzone_reserve_aligned(name, len, socket_id,
134                                            RTE_MEMZONE_IOVA_CONTIG,
135                                            ICE_RING_BASE_ALIGN);
136 }
137
138 #define ICE_FDIR_MZ_NAME        "FDIR_MEMZONE"
139
140 static int
141 ice_fdir_prof_alloc(struct ice_hw *hw)
142 {
143         enum ice_fltr_ptype ptype, fltr_ptype;
144
145         if (!hw->fdir_prof) {
146                 hw->fdir_prof = (struct ice_fd_hw_prof **)
147                         ice_malloc(hw, ICE_FLTR_PTYPE_MAX *
148                                    sizeof(*hw->fdir_prof));
149                 if (!hw->fdir_prof)
150                         return -ENOMEM;
151         }
152         for (ptype = ICE_FLTR_PTYPE_NONF_NONE + 1;
153              ptype < ICE_FLTR_PTYPE_MAX;
154              ptype++) {
155                 if (!hw->fdir_prof[ptype]) {
156                         hw->fdir_prof[ptype] = (struct ice_fd_hw_prof *)
157                                 ice_malloc(hw, sizeof(**hw->fdir_prof));
158                         if (!hw->fdir_prof[ptype])
159                                 goto fail_mem;
160                 }
161         }
162         return 0;
163
164 fail_mem:
165         for (fltr_ptype = ICE_FLTR_PTYPE_NONF_NONE + 1;
166              fltr_ptype < ptype;
167              fltr_ptype++) {
168                 rte_free(hw->fdir_prof[fltr_ptype]);
169                 hw->fdir_prof[fltr_ptype] = NULL;
170         }
171
172         rte_free(hw->fdir_prof);
173         hw->fdir_prof = NULL;
174
175         return -ENOMEM;
176 }
177
178 static int
179 ice_fdir_counter_pool_add(__rte_unused struct ice_pf *pf,
180                           struct ice_fdir_counter_pool_container *container,
181                           uint32_t index_start,
182                           uint32_t len)
183 {
184         struct ice_fdir_counter_pool *pool;
185         uint32_t i;
186         int ret = 0;
187
188         pool = rte_zmalloc("ice_fdir_counter_pool",
189                            sizeof(*pool) +
190                            sizeof(struct ice_fdir_counter) * len,
191                            0);
192         if (!pool) {
193                 PMD_INIT_LOG(ERR,
194                              "Failed to allocate memory for fdir counter pool");
195                 return -ENOMEM;
196         }
197
198         TAILQ_INIT(&pool->counter_list);
199         TAILQ_INSERT_TAIL(&container->pool_list, pool, next);
200
201         for (i = 0; i < len; i++) {
202                 struct ice_fdir_counter *counter = &pool->counters[i];
203
204                 counter->hw_index = index_start + i;
205                 TAILQ_INSERT_TAIL(&pool->counter_list, counter, next);
206         }
207
208         if (container->index_free == ICE_FDIR_COUNTER_MAX_POOL_SIZE) {
209                 PMD_INIT_LOG(ERR, "FDIR counter pool is full");
210                 ret = -EINVAL;
211                 goto free_pool;
212         }
213
214         container->pools[container->index_free++] = pool;
215         return 0;
216
217 free_pool:
218         rte_free(pool);
219         return ret;
220 }
221
222 static int
223 ice_fdir_counter_init(struct ice_pf *pf)
224 {
225         struct ice_hw *hw = ICE_PF_TO_HW(pf);
226         struct ice_fdir_info *fdir_info = &pf->fdir;
227         struct ice_fdir_counter_pool_container *container =
228                                 &fdir_info->counter;
229         uint32_t cnt_index, len;
230         int ret;
231
232         TAILQ_INIT(&container->pool_list);
233
234         cnt_index = ICE_FDIR_COUNTER_INDEX(hw->fd_ctr_base);
235         len = ICE_FDIR_COUNTERS_PER_BLOCK;
236
237         ret = ice_fdir_counter_pool_add(pf, container, cnt_index, len);
238         if (ret) {
239                 PMD_INIT_LOG(ERR, "Failed to add fdir pool to container");
240                 return ret;
241         }
242
243         return 0;
244 }
245
246 static int
247 ice_fdir_counter_release(struct ice_pf *pf)
248 {
249         struct ice_fdir_info *fdir_info = &pf->fdir;
250         struct ice_fdir_counter_pool_container *container =
251                                 &fdir_info->counter;
252         uint8_t i;
253
254         for (i = 0; i < container->index_free; i++) {
255                 rte_free(container->pools[i]);
256                 container->pools[i] = NULL;
257         }
258
259         TAILQ_INIT(&container->pool_list);
260         container->index_free = 0;
261
262         return 0;
263 }
264
265 static struct ice_fdir_counter *
266 ice_fdir_counter_shared_search(struct ice_fdir_counter_pool_container
267                                         *container,
268                                uint32_t id)
269 {
270         struct ice_fdir_counter_pool *pool;
271         struct ice_fdir_counter *counter;
272         int i;
273
274         TAILQ_FOREACH(pool, &container->pool_list, next) {
275                 for (i = 0; i < ICE_FDIR_COUNTERS_PER_BLOCK; i++) {
276                         counter = &pool->counters[i];
277
278                         if (counter->shared &&
279                             counter->ref_cnt &&
280                             counter->id == id)
281                                 return counter;
282                 }
283         }
284
285         return NULL;
286 }
287
288 static struct ice_fdir_counter *
289 ice_fdir_counter_alloc(struct ice_pf *pf, uint32_t shared, uint32_t id)
290 {
291         struct ice_hw *hw = ICE_PF_TO_HW(pf);
292         struct ice_fdir_info *fdir_info = &pf->fdir;
293         struct ice_fdir_counter_pool_container *container =
294                                 &fdir_info->counter;
295         struct ice_fdir_counter_pool *pool = NULL;
296         struct ice_fdir_counter *counter_free = NULL;
297
298         if (shared) {
299                 counter_free = ice_fdir_counter_shared_search(container, id);
300                 if (counter_free) {
301                         if (counter_free->ref_cnt + 1 == 0) {
302                                 rte_errno = E2BIG;
303                                 return NULL;
304                         }
305                         counter_free->ref_cnt++;
306                         return counter_free;
307                 }
308         }
309
310         TAILQ_FOREACH(pool, &container->pool_list, next) {
311                 counter_free = TAILQ_FIRST(&pool->counter_list);
312                 if (counter_free)
313                         break;
314                 counter_free = NULL;
315         }
316
317         if (!counter_free) {
318                 PMD_DRV_LOG(ERR, "No free counter found\n");
319                 return NULL;
320         }
321
322         counter_free->shared = shared;
323         counter_free->id = id;
324         counter_free->ref_cnt = 1;
325         counter_free->pool = pool;
326
327         /* reset statistic counter value */
328         ICE_WRITE_REG(hw, GLSTAT_FD_CNT0H(counter_free->hw_index), 0);
329         ICE_WRITE_REG(hw, GLSTAT_FD_CNT0L(counter_free->hw_index), 0);
330
331         TAILQ_REMOVE(&pool->counter_list, counter_free, next);
332         if (TAILQ_EMPTY(&pool->counter_list)) {
333                 TAILQ_REMOVE(&container->pool_list, pool, next);
334                 TAILQ_INSERT_TAIL(&container->pool_list, pool, next);
335         }
336
337         return counter_free;
338 }
339
340 static void
341 ice_fdir_counter_free(__rte_unused struct ice_pf *pf,
342                       struct ice_fdir_counter *counter)
343 {
344         if (!counter)
345                 return;
346
347         if (--counter->ref_cnt == 0) {
348                 struct ice_fdir_counter_pool *pool = counter->pool;
349
350                 TAILQ_INSERT_TAIL(&pool->counter_list, counter, next);
351         }
352 }
353
354 static int
355 ice_fdir_init_filter_list(struct ice_pf *pf)
356 {
357         struct rte_eth_dev *dev = pf->adapter->eth_dev;
358         struct ice_fdir_info *fdir_info = &pf->fdir;
359         char fdir_hash_name[RTE_HASH_NAMESIZE];
360         int ret;
361
362         struct rte_hash_parameters fdir_hash_params = {
363                 .name = fdir_hash_name,
364                 .entries = ICE_MAX_FDIR_FILTER_NUM,
365                 .key_len = sizeof(struct ice_fdir_fltr_pattern),
366                 .hash_func = rte_hash_crc,
367                 .hash_func_init_val = 0,
368                 .socket_id = rte_socket_id(),
369                 .extra_flag = RTE_HASH_EXTRA_FLAGS_EXT_TABLE,
370         };
371
372         /* Initialize hash */
373         snprintf(fdir_hash_name, RTE_HASH_NAMESIZE,
374                  "fdir_%s", dev->device->name);
375         fdir_info->hash_table = rte_hash_create(&fdir_hash_params);
376         if (!fdir_info->hash_table) {
377                 PMD_INIT_LOG(ERR, "Failed to create fdir hash table!");
378                 return -EINVAL;
379         }
380         fdir_info->hash_map = rte_zmalloc("ice_fdir_hash_map",
381                                           sizeof(*fdir_info->hash_map) *
382                                           ICE_MAX_FDIR_FILTER_NUM,
383                                           0);
384         if (!fdir_info->hash_map) {
385                 PMD_INIT_LOG(ERR,
386                              "Failed to allocate memory for fdir hash map!");
387                 ret = -ENOMEM;
388                 goto err_fdir_hash_map_alloc;
389         }
390         return 0;
391
392 err_fdir_hash_map_alloc:
393         rte_hash_free(fdir_info->hash_table);
394
395         return ret;
396 }
397
398 static void
399 ice_fdir_release_filter_list(struct ice_pf *pf)
400 {
401         struct ice_fdir_info *fdir_info = &pf->fdir;
402
403         if (fdir_info->hash_map)
404                 rte_free(fdir_info->hash_map);
405         if (fdir_info->hash_table)
406                 rte_hash_free(fdir_info->hash_table);
407
408         fdir_info->hash_map = NULL;
409         fdir_info->hash_table = NULL;
410 }
411
412 /*
413  * ice_fdir_setup - reserve and initialize the Flow Director resources
414  * @pf: board private structure
415  */
416 static int
417 ice_fdir_setup(struct ice_pf *pf)
418 {
419         struct rte_eth_dev *eth_dev = pf->adapter->eth_dev;
420         struct ice_hw *hw = ICE_PF_TO_HW(pf);
421         const struct rte_memzone *mz = NULL;
422         char z_name[RTE_MEMZONE_NAMESIZE];
423         struct ice_vsi *vsi;
424         int err = ICE_SUCCESS;
425
426         if ((pf->flags & ICE_FLAG_FDIR) == 0) {
427                 PMD_INIT_LOG(ERR, "HW doesn't support FDIR");
428                 return -ENOTSUP;
429         }
430
431         PMD_DRV_LOG(INFO, "FDIR HW Capabilities: fd_fltr_guar = %u,"
432                     " fd_fltr_best_effort = %u.",
433                     hw->func_caps.fd_fltr_guar,
434                     hw->func_caps.fd_fltr_best_effort);
435
436         if (pf->fdir.fdir_vsi) {
437                 PMD_DRV_LOG(INFO, "FDIR initialization has been done.");
438                 return ICE_SUCCESS;
439         }
440
441         /* make new FDIR VSI */
442         vsi = ice_setup_vsi(pf, ICE_VSI_CTRL);
443         if (!vsi) {
444                 PMD_DRV_LOG(ERR, "Couldn't create FDIR VSI.");
445                 return -EINVAL;
446         }
447         pf->fdir.fdir_vsi = vsi;
448
449         err = ice_fdir_init_filter_list(pf);
450         if (err) {
451                 PMD_DRV_LOG(ERR, "Failed to init FDIR filter list.");
452                 return -EINVAL;
453         }
454
455         err = ice_fdir_counter_init(pf);
456         if (err) {
457                 PMD_DRV_LOG(ERR, "Failed to init FDIR counter.");
458                 return -EINVAL;
459         }
460
461         /*Fdir tx queue setup*/
462         err = ice_fdir_setup_tx_resources(pf);
463         if (err) {
464                 PMD_DRV_LOG(ERR, "Failed to setup FDIR TX resources.");
465                 goto fail_setup_tx;
466         }
467
468         /*Fdir rx queue setup*/
469         err = ice_fdir_setup_rx_resources(pf);
470         if (err) {
471                 PMD_DRV_LOG(ERR, "Failed to setup FDIR RX resources.");
472                 goto fail_setup_rx;
473         }
474
475         err = ice_fdir_tx_queue_start(eth_dev, pf->fdir.txq->queue_id);
476         if (err) {
477                 PMD_DRV_LOG(ERR, "Failed to start FDIR TX queue.");
478                 goto fail_mem;
479         }
480
481         err = ice_fdir_rx_queue_start(eth_dev, pf->fdir.rxq->queue_id);
482         if (err) {
483                 PMD_DRV_LOG(ERR, "Failed to start FDIR RX queue.");
484                 goto fail_mem;
485         }
486
487         /* Enable FDIR MSIX interrupt */
488         vsi->nb_used_qps = 1;
489         ice_vsi_queues_bind_intr(vsi);
490         ice_vsi_enable_queues_intr(vsi);
491
492         /* reserve memory for the fdir programming packet */
493         snprintf(z_name, sizeof(z_name), "ICE_%s_%d",
494                  ICE_FDIR_MZ_NAME,
495                  eth_dev->data->port_id);
496         mz = ice_memzone_reserve(z_name, ICE_FDIR_PKT_LEN, SOCKET_ID_ANY);
497         if (!mz) {
498                 PMD_DRV_LOG(ERR, "Cannot init memzone for "
499                             "flow director program packet.");
500                 err = -ENOMEM;
501                 goto fail_mem;
502         }
503         pf->fdir.prg_pkt = mz->addr;
504         pf->fdir.dma_addr = mz->iova;
505         pf->fdir.mz = mz;
506
507         err = ice_fdir_prof_alloc(hw);
508         if (err) {
509                 PMD_DRV_LOG(ERR, "Cannot allocate memory for "
510                             "flow director profile.");
511                 err = -ENOMEM;
512                 goto fail_prof;
513         }
514
515         PMD_DRV_LOG(INFO, "FDIR setup successfully, with programming queue %u.",
516                     vsi->base_queue);
517         return ICE_SUCCESS;
518
519 fail_prof:
520         rte_memzone_free(pf->fdir.mz);
521         pf->fdir.mz = NULL;
522 fail_mem:
523         ice_rx_queue_release(pf->fdir.rxq);
524         pf->fdir.rxq = NULL;
525 fail_setup_rx:
526         ice_tx_queue_release(pf->fdir.txq);
527         pf->fdir.txq = NULL;
528 fail_setup_tx:
529         ice_release_vsi(vsi);
530         pf->fdir.fdir_vsi = NULL;
531         return err;
532 }
533
534 static void
535 ice_fdir_prof_free(struct ice_hw *hw)
536 {
537         enum ice_fltr_ptype ptype;
538
539         for (ptype = ICE_FLTR_PTYPE_NONF_NONE + 1;
540              ptype < ICE_FLTR_PTYPE_MAX;
541              ptype++) {
542                 rte_free(hw->fdir_prof[ptype]);
543                 hw->fdir_prof[ptype] = NULL;
544         }
545
546         rte_free(hw->fdir_prof);
547         hw->fdir_prof = NULL;
548 }
549
550 /* Remove a profile for some filter type */
551 static void
552 ice_fdir_prof_rm(struct ice_pf *pf, enum ice_fltr_ptype ptype, bool is_tunnel)
553 {
554         struct ice_hw *hw = ICE_PF_TO_HW(pf);
555         struct ice_fd_hw_prof *hw_prof;
556         uint64_t prof_id;
557         uint16_t vsi_num;
558         int i;
559
560         if (!hw->fdir_prof || !hw->fdir_prof[ptype])
561                 return;
562
563         hw_prof = hw->fdir_prof[ptype];
564
565         prof_id = ptype + is_tunnel * ICE_FLTR_PTYPE_MAX;
566         for (i = 0; i < pf->hw_prof_cnt[ptype][is_tunnel]; i++) {
567                 if (hw_prof->entry_h[i][is_tunnel]) {
568                         vsi_num = ice_get_hw_vsi_num(hw,
569                                                      hw_prof->vsi_h[i]);
570                         ice_rem_prof_id_flow(hw, ICE_BLK_FD,
571                                              vsi_num, ptype);
572                         ice_flow_rem_entry(hw, ICE_BLK_FD,
573                                            hw_prof->entry_h[i][is_tunnel]);
574                         hw_prof->entry_h[i][is_tunnel] = 0;
575                 }
576         }
577         ice_flow_rem_prof(hw, ICE_BLK_FD, prof_id);
578         rte_free(hw_prof->fdir_seg[is_tunnel]);
579         hw_prof->fdir_seg[is_tunnel] = NULL;
580
581         for (i = 0; i < hw_prof->cnt; i++)
582                 hw_prof->vsi_h[i] = 0;
583         pf->hw_prof_cnt[ptype][is_tunnel] = 0;
584 }
585
586 /* Remove all created profiles */
587 static void
588 ice_fdir_prof_rm_all(struct ice_pf *pf)
589 {
590         enum ice_fltr_ptype ptype;
591
592         for (ptype = ICE_FLTR_PTYPE_NONF_NONE + 1;
593              ptype < ICE_FLTR_PTYPE_MAX;
594              ptype++) {
595                 ice_fdir_prof_rm(pf, ptype, false);
596                 ice_fdir_prof_rm(pf, ptype, true);
597         }
598 }
599
600 /*
601  * ice_fdir_teardown - release the Flow Director resources
602  * @pf: board private structure
603  */
604 static void
605 ice_fdir_teardown(struct ice_pf *pf)
606 {
607         struct rte_eth_dev *eth_dev = pf->adapter->eth_dev;
608         struct ice_hw *hw = ICE_PF_TO_HW(pf);
609         struct ice_vsi *vsi;
610         int err;
611
612         vsi = pf->fdir.fdir_vsi;
613         if (!vsi)
614                 return;
615
616         ice_vsi_disable_queues_intr(vsi);
617
618         err = ice_fdir_tx_queue_stop(eth_dev, pf->fdir.txq->queue_id);
619         if (err)
620                 PMD_DRV_LOG(ERR, "Failed to stop TX queue.");
621
622         err = ice_fdir_rx_queue_stop(eth_dev, pf->fdir.rxq->queue_id);
623         if (err)
624                 PMD_DRV_LOG(ERR, "Failed to stop RX queue.");
625
626         err = ice_fdir_counter_release(pf);
627         if (err)
628                 PMD_DRV_LOG(ERR, "Failed to release FDIR counter resource.");
629
630         ice_fdir_release_filter_list(pf);
631
632         ice_tx_queue_release(pf->fdir.txq);
633         pf->fdir.txq = NULL;
634         ice_rx_queue_release(pf->fdir.rxq);
635         pf->fdir.rxq = NULL;
636         ice_fdir_prof_rm_all(pf);
637         ice_fdir_prof_free(hw);
638         ice_release_vsi(vsi);
639         pf->fdir.fdir_vsi = NULL;
640
641         if (pf->fdir.mz) {
642                 err = rte_memzone_free(pf->fdir.mz);
643                 pf->fdir.mz = NULL;
644                 if (err)
645                         PMD_DRV_LOG(ERR, "Failed to free FDIR memezone.");
646         }
647 }
648
649 static int
650 ice_fdir_cur_prof_conflict(struct ice_pf *pf,
651                            enum ice_fltr_ptype ptype,
652                            struct ice_flow_seg_info *seg,
653                            bool is_tunnel)
654 {
655         struct ice_hw *hw = ICE_PF_TO_HW(pf);
656         struct ice_flow_seg_info *ori_seg;
657         struct ice_fd_hw_prof *hw_prof;
658
659         hw_prof = hw->fdir_prof[ptype];
660         ori_seg = hw_prof->fdir_seg[is_tunnel];
661
662         /* profile does not exist */
663         if (!ori_seg)
664                 return 0;
665
666         /* if no input set conflict, return -EEXIST */
667         if ((!is_tunnel && !memcmp(ori_seg, seg, sizeof(*seg))) ||
668             (is_tunnel && !memcmp(&ori_seg[1], &seg[1], sizeof(*seg)))) {
669                 PMD_DRV_LOG(DEBUG, "Profile already exists for flow type %d.",
670                             ptype);
671                 return -EEXIST;
672         }
673
674         /* a rule with input set conflict already exist, so give up */
675         if (pf->fdir_fltr_cnt[ptype][is_tunnel]) {
676                 PMD_DRV_LOG(DEBUG, "Failed to create profile for flow type %d due to conflict with existing rule.",
677                             ptype);
678                 return -EINVAL;
679         }
680
681         /* it's safe to delete an empty profile */
682         ice_fdir_prof_rm(pf, ptype, is_tunnel);
683         return 0;
684 }
685
686 static bool
687 ice_fdir_prof_resolve_conflict(struct ice_pf *pf,
688                                enum ice_fltr_ptype ptype,
689                                bool is_tunnel)
690 {
691         struct ice_hw *hw = ICE_PF_TO_HW(pf);
692         struct ice_fd_hw_prof *hw_prof;
693         struct ice_flow_seg_info *seg;
694
695         hw_prof = hw->fdir_prof[ptype];
696         seg = hw_prof->fdir_seg[is_tunnel];
697
698         /* profile does not exist */
699         if (!seg)
700                 return true;
701
702         /* profile exists and rule exists, fail to resolve the conflict */
703         if (pf->fdir_fltr_cnt[ptype][is_tunnel] != 0)
704                 return false;
705
706         /* it's safe to delete an empty profile */
707         ice_fdir_prof_rm(pf, ptype, is_tunnel);
708
709         return true;
710 }
711
712 static int
713 ice_fdir_cross_prof_conflict(struct ice_pf *pf,
714                              enum ice_fltr_ptype ptype,
715                              bool is_tunnel)
716 {
717         enum ice_fltr_ptype cflct_ptype;
718
719         switch (ptype) {
720         /* IPv4 */
721         case ICE_FLTR_PTYPE_NONF_IPV4_UDP:
722         case ICE_FLTR_PTYPE_NONF_IPV4_TCP:
723         case ICE_FLTR_PTYPE_NONF_IPV4_SCTP:
724                 cflct_ptype = ICE_FLTR_PTYPE_NONF_IPV4_OTHER;
725                 if (!ice_fdir_prof_resolve_conflict
726                         (pf, cflct_ptype, is_tunnel))
727                         goto err;
728                 break;
729         case ICE_FLTR_PTYPE_NONF_IPV4_OTHER:
730                 cflct_ptype = ICE_FLTR_PTYPE_NONF_IPV4_UDP;
731                 if (!ice_fdir_prof_resolve_conflict
732                         (pf, cflct_ptype, is_tunnel))
733                         goto err;
734                 cflct_ptype = ICE_FLTR_PTYPE_NONF_IPV4_TCP;
735                 if (!ice_fdir_prof_resolve_conflict
736                         (pf, cflct_ptype, is_tunnel))
737                         goto err;
738                 cflct_ptype = ICE_FLTR_PTYPE_NONF_IPV4_SCTP;
739                 if (!ice_fdir_prof_resolve_conflict
740                         (pf, cflct_ptype, is_tunnel))
741                         goto err;
742                 break;
743         /* IPv4 GTPU */
744         case ICE_FLTR_PTYPE_NONF_IPV4_GTPU_IPV4_UDP:
745         case ICE_FLTR_PTYPE_NONF_IPV4_GTPU_IPV4_TCP:
746         case ICE_FLTR_PTYPE_NONF_IPV4_GTPU_IPV4_ICMP:
747                 cflct_ptype = ICE_FLTR_PTYPE_NONF_IPV4_GTPU_IPV4_OTHER;
748                 if (!ice_fdir_prof_resolve_conflict
749                         (pf, cflct_ptype, is_tunnel))
750                         goto err;
751                 break;
752         case ICE_FLTR_PTYPE_NONF_IPV4_GTPU_IPV4_OTHER:
753                 cflct_ptype = ICE_FLTR_PTYPE_NONF_IPV4_GTPU_IPV4_UDP;
754                 if (!ice_fdir_prof_resolve_conflict
755                         (pf, cflct_ptype, is_tunnel))
756                         goto err;
757                 cflct_ptype = ICE_FLTR_PTYPE_NONF_IPV4_GTPU_IPV4_TCP;
758                 if (!ice_fdir_prof_resolve_conflict
759                         (pf, cflct_ptype, is_tunnel))
760                         goto err;
761                 cflct_ptype = ICE_FLTR_PTYPE_NONF_IPV4_GTPU_IPV4_ICMP;
762                 if (!ice_fdir_prof_resolve_conflict
763                         (pf, cflct_ptype, is_tunnel))
764                         goto err;
765                 break;
766         /* IPv6 */
767         case ICE_FLTR_PTYPE_NONF_IPV6_UDP:
768         case ICE_FLTR_PTYPE_NONF_IPV6_TCP:
769         case ICE_FLTR_PTYPE_NONF_IPV6_SCTP:
770                 cflct_ptype = ICE_FLTR_PTYPE_NONF_IPV6_OTHER;
771                 if (!ice_fdir_prof_resolve_conflict
772                         (pf, cflct_ptype, is_tunnel))
773                         goto err;
774                 break;
775         case ICE_FLTR_PTYPE_NONF_IPV6_OTHER:
776                 cflct_ptype = ICE_FLTR_PTYPE_NONF_IPV6_UDP;
777                 if (!ice_fdir_prof_resolve_conflict
778                         (pf, cflct_ptype, is_tunnel))
779                         goto err;
780                 cflct_ptype = ICE_FLTR_PTYPE_NONF_IPV6_TCP;
781                 if (!ice_fdir_prof_resolve_conflict
782                         (pf, cflct_ptype, is_tunnel))
783                         goto err;
784                 cflct_ptype = ICE_FLTR_PTYPE_NONF_IPV6_SCTP;
785                 if (!ice_fdir_prof_resolve_conflict
786                         (pf, cflct_ptype, is_tunnel))
787                         goto err;
788                 break;
789         default:
790                 break;
791         }
792         return 0;
793 err:
794         PMD_DRV_LOG(DEBUG, "Failed to create profile for flow type %d due to conflict with existing rule of flow type %d.",
795                     ptype, cflct_ptype);
796         return -EINVAL;
797 }
798
799 static int
800 ice_fdir_hw_tbl_conf(struct ice_pf *pf, struct ice_vsi *vsi,
801                      struct ice_vsi *ctrl_vsi,
802                      struct ice_flow_seg_info *seg,
803                      enum ice_fltr_ptype ptype,
804                      bool is_tunnel)
805 {
806         struct ice_hw *hw = ICE_PF_TO_HW(pf);
807         enum ice_flow_dir dir = ICE_FLOW_RX;
808         struct ice_fd_hw_prof *hw_prof;
809         struct ice_flow_prof *prof;
810         uint64_t entry_1 = 0;
811         uint64_t entry_2 = 0;
812         uint16_t vsi_num;
813         int ret;
814         uint64_t prof_id;
815
816         /* check if have input set conflict on current profile. */
817         ret = ice_fdir_cur_prof_conflict(pf, ptype, seg, is_tunnel);
818         if (ret)
819                 return ret;
820
821         /* check if the profile is conflict with other profile. */
822         ret = ice_fdir_cross_prof_conflict(pf, ptype, is_tunnel);
823         if (ret)
824                 return ret;
825
826         prof_id = ptype + is_tunnel * ICE_FLTR_PTYPE_MAX;
827         ret = ice_flow_add_prof(hw, ICE_BLK_FD, dir, prof_id, seg,
828                                 (is_tunnel) ? 2 : 1, NULL, 0, &prof);
829         if (ret)
830                 return ret;
831         ret = ice_flow_add_entry(hw, ICE_BLK_FD, prof_id, vsi->idx,
832                                  vsi->idx, ICE_FLOW_PRIO_NORMAL,
833                                  seg, NULL, 0, &entry_1);
834         if (ret) {
835                 PMD_DRV_LOG(ERR, "Failed to add main VSI flow entry for %d.",
836                             ptype);
837                 goto err_add_prof;
838         }
839         ret = ice_flow_add_entry(hw, ICE_BLK_FD, prof_id, vsi->idx,
840                                  ctrl_vsi->idx, ICE_FLOW_PRIO_NORMAL,
841                                  seg, NULL, 0, &entry_2);
842         if (ret) {
843                 PMD_DRV_LOG(ERR, "Failed to add control VSI flow entry for %d.",
844                             ptype);
845                 goto err_add_entry;
846         }
847
848         hw_prof = hw->fdir_prof[ptype];
849         pf->hw_prof_cnt[ptype][is_tunnel] = 0;
850         hw_prof->cnt = 0;
851         hw_prof->fdir_seg[is_tunnel] = seg;
852         hw_prof->vsi_h[hw_prof->cnt] = vsi->idx;
853         hw_prof->entry_h[hw_prof->cnt++][is_tunnel] = entry_1;
854         pf->hw_prof_cnt[ptype][is_tunnel]++;
855         hw_prof->vsi_h[hw_prof->cnt] = ctrl_vsi->idx;
856         hw_prof->entry_h[hw_prof->cnt++][is_tunnel] = entry_2;
857         pf->hw_prof_cnt[ptype][is_tunnel]++;
858
859         return ret;
860
861 err_add_entry:
862         vsi_num = ice_get_hw_vsi_num(hw, vsi->idx);
863         ice_rem_prof_id_flow(hw, ICE_BLK_FD, vsi_num, prof_id);
864         ice_flow_rem_entry(hw, ICE_BLK_FD, entry_1);
865 err_add_prof:
866         ice_flow_rem_prof(hw, ICE_BLK_FD, prof_id);
867
868         return ret;
869 }
870
871 static void
872 ice_fdir_input_set_parse(uint64_t inset, enum ice_flow_field *field)
873 {
874         uint32_t i, j;
875
876         struct ice_inset_map {
877                 uint64_t inset;
878                 enum ice_flow_field fld;
879         };
880         static const struct ice_inset_map ice_inset_map[] = {
881                 {ICE_INSET_DMAC, ICE_FLOW_FIELD_IDX_ETH_DA},
882                 {ICE_INSET_ETHERTYPE, ICE_FLOW_FIELD_IDX_ETH_TYPE},
883                 {ICE_INSET_IPV4_SRC, ICE_FLOW_FIELD_IDX_IPV4_SA},
884                 {ICE_INSET_IPV4_DST, ICE_FLOW_FIELD_IDX_IPV4_DA},
885                 {ICE_INSET_IPV4_TOS, ICE_FLOW_FIELD_IDX_IPV4_DSCP},
886                 {ICE_INSET_IPV4_TTL, ICE_FLOW_FIELD_IDX_IPV4_TTL},
887                 {ICE_INSET_IPV4_PROTO, ICE_FLOW_FIELD_IDX_IPV4_PROT},
888                 {ICE_INSET_IPV6_SRC, ICE_FLOW_FIELD_IDX_IPV6_SA},
889                 {ICE_INSET_IPV6_DST, ICE_FLOW_FIELD_IDX_IPV6_DA},
890                 {ICE_INSET_IPV6_TC, ICE_FLOW_FIELD_IDX_IPV6_DSCP},
891                 {ICE_INSET_IPV6_NEXT_HDR, ICE_FLOW_FIELD_IDX_IPV6_PROT},
892                 {ICE_INSET_IPV6_HOP_LIMIT, ICE_FLOW_FIELD_IDX_IPV6_TTL},
893                 {ICE_INSET_TCP_SRC_PORT, ICE_FLOW_FIELD_IDX_TCP_SRC_PORT},
894                 {ICE_INSET_TCP_DST_PORT, ICE_FLOW_FIELD_IDX_TCP_DST_PORT},
895                 {ICE_INSET_UDP_SRC_PORT, ICE_FLOW_FIELD_IDX_UDP_SRC_PORT},
896                 {ICE_INSET_UDP_DST_PORT, ICE_FLOW_FIELD_IDX_UDP_DST_PORT},
897                 {ICE_INSET_SCTP_SRC_PORT, ICE_FLOW_FIELD_IDX_SCTP_SRC_PORT},
898                 {ICE_INSET_SCTP_DST_PORT, ICE_FLOW_FIELD_IDX_SCTP_DST_PORT},
899                 {ICE_INSET_TUN_IPV4_SRC, ICE_FLOW_FIELD_IDX_IPV4_SA},
900                 {ICE_INSET_TUN_IPV4_DST, ICE_FLOW_FIELD_IDX_IPV4_DA},
901                 {ICE_INSET_TUN_TCP_SRC_PORT, ICE_FLOW_FIELD_IDX_TCP_SRC_PORT},
902                 {ICE_INSET_TUN_TCP_DST_PORT, ICE_FLOW_FIELD_IDX_TCP_DST_PORT},
903                 {ICE_INSET_TUN_UDP_SRC_PORT, ICE_FLOW_FIELD_IDX_UDP_SRC_PORT},
904                 {ICE_INSET_TUN_UDP_DST_PORT, ICE_FLOW_FIELD_IDX_UDP_DST_PORT},
905                 {ICE_INSET_TUN_SCTP_SRC_PORT, ICE_FLOW_FIELD_IDX_SCTP_SRC_PORT},
906                 {ICE_INSET_TUN_SCTP_DST_PORT, ICE_FLOW_FIELD_IDX_SCTP_DST_PORT},
907                 {ICE_INSET_GTPU_TEID, ICE_FLOW_FIELD_IDX_GTPU_IP_TEID},
908                 {ICE_INSET_GTPU_QFI, ICE_FLOW_FIELD_IDX_GTPU_EH_QFI},
909         };
910
911         for (i = 0, j = 0; i < RTE_DIM(ice_inset_map); i++) {
912                 if ((inset & ice_inset_map[i].inset) ==
913                     ice_inset_map[i].inset)
914                         field[j++] = ice_inset_map[i].fld;
915         }
916 }
917
918 static void
919 ice_fdir_input_set_hdrs(enum ice_fltr_ptype flow, struct ice_flow_seg_info *seg)
920 {
921         switch (flow) {
922         case ICE_FLTR_PTYPE_NONF_IPV4_UDP:
923                 ICE_FLOW_SET_HDRS(seg, ICE_FLOW_SEG_HDR_UDP |
924                                   ICE_FLOW_SEG_HDR_IPV4 |
925                                   ICE_FLOW_SEG_HDR_IPV_OTHER);
926                 break;
927         case ICE_FLTR_PTYPE_NONF_IPV4_TCP:
928                 ICE_FLOW_SET_HDRS(seg, ICE_FLOW_SEG_HDR_TCP |
929                                   ICE_FLOW_SEG_HDR_IPV4 |
930                                   ICE_FLOW_SEG_HDR_IPV_OTHER);
931                 break;
932         case ICE_FLTR_PTYPE_NONF_IPV4_SCTP:
933                 ICE_FLOW_SET_HDRS(seg, ICE_FLOW_SEG_HDR_SCTP |
934                                   ICE_FLOW_SEG_HDR_IPV4 |
935                                   ICE_FLOW_SEG_HDR_IPV_OTHER);
936                 break;
937         case ICE_FLTR_PTYPE_NONF_IPV4_OTHER:
938                 ICE_FLOW_SET_HDRS(seg, ICE_FLOW_SEG_HDR_IPV4 |
939                                   ICE_FLOW_SEG_HDR_IPV_OTHER);
940                 break;
941         case ICE_FLTR_PTYPE_NONF_IPV6_UDP:
942                 ICE_FLOW_SET_HDRS(seg, ICE_FLOW_SEG_HDR_UDP |
943                                   ICE_FLOW_SEG_HDR_IPV6 |
944                                   ICE_FLOW_SEG_HDR_IPV_OTHER);
945                 break;
946         case ICE_FLTR_PTYPE_NONF_IPV6_TCP:
947                 ICE_FLOW_SET_HDRS(seg, ICE_FLOW_SEG_HDR_TCP |
948                                   ICE_FLOW_SEG_HDR_IPV6 |
949                                   ICE_FLOW_SEG_HDR_IPV_OTHER);
950                 break;
951         case ICE_FLTR_PTYPE_NONF_IPV6_SCTP:
952                 ICE_FLOW_SET_HDRS(seg, ICE_FLOW_SEG_HDR_SCTP |
953                                   ICE_FLOW_SEG_HDR_IPV6 |
954                                   ICE_FLOW_SEG_HDR_IPV_OTHER);
955                 break;
956         case ICE_FLTR_PTYPE_NONF_IPV6_OTHER:
957                 ICE_FLOW_SET_HDRS(seg, ICE_FLOW_SEG_HDR_IPV6 |
958                                   ICE_FLOW_SEG_HDR_IPV_OTHER);
959                 break;
960         case ICE_FLTR_PTYPE_NONF_IPV4_GTPU_IPV4_UDP:
961         case ICE_FLTR_PTYPE_NONF_IPV4_GTPU_IPV4_TCP:
962         case ICE_FLTR_PTYPE_NONF_IPV4_GTPU_IPV4_ICMP:
963         case ICE_FLTR_PTYPE_NONF_IPV4_GTPU_IPV4_OTHER:
964                 ICE_FLOW_SET_HDRS(seg, ICE_FLOW_SEG_HDR_GTPU_IP |
965                                   ICE_FLOW_SEG_HDR_IPV4 |
966                                   ICE_FLOW_SEG_HDR_IPV_OTHER);
967                 break;
968         case ICE_FLTR_PTYPE_NONF_IPV4_GTPU_EH_IPV4_OTHER:
969                 ICE_FLOW_SET_HDRS(seg, ICE_FLOW_SEG_HDR_GTPU_EH |
970                                   ICE_FLOW_SEG_HDR_GTPU_IP |
971                                   ICE_FLOW_SEG_HDR_IPV4 |
972                                   ICE_FLOW_SEG_HDR_IPV_OTHER);
973                 break;
974         case ICE_FLTR_PTYPE_NONF_IPV6_GTPU_IPV6_OTHER:
975                 ICE_FLOW_SET_HDRS(seg, ICE_FLOW_SEG_HDR_GTPU_IP |
976                                   ICE_FLOW_SEG_HDR_IPV6 |
977                                   ICE_FLOW_SEG_HDR_IPV_OTHER);
978                 break;
979         case ICE_FLTR_PTYPE_NONF_IPV6_GTPU_EH_IPV6_OTHER:
980                 ICE_FLOW_SET_HDRS(seg, ICE_FLOW_SEG_HDR_GTPU_EH |
981                                   ICE_FLOW_SEG_HDR_GTPU_IP |
982                                   ICE_FLOW_SEG_HDR_IPV6 |
983                                   ICE_FLOW_SEG_HDR_IPV_OTHER);
984                 break;
985         case ICE_FLTR_PTYPE_NON_IP_L2:
986                 ICE_FLOW_SET_HDRS(seg, ICE_FLOW_SEG_HDR_ETH_NON_IP);
987                 break;
988         default:
989                 PMD_DRV_LOG(ERR, "not supported filter type.");
990                 break;
991         }
992 }
993
994 static int
995 ice_fdir_input_set_conf(struct ice_pf *pf, enum ice_fltr_ptype flow,
996                         uint64_t inner_input_set, uint64_t outer_input_set,
997                         enum ice_fdir_tunnel_type ttype)
998 {
999         struct ice_flow_seg_info *seg;
1000         struct ice_flow_seg_info *seg_tun = NULL;
1001         enum ice_flow_field field[ICE_FLOW_FIELD_IDX_MAX];
1002         uint64_t input_set;
1003         bool is_tunnel;
1004         int k, i, ret = 0;
1005
1006         if (!(inner_input_set | outer_input_set))
1007                 return -EINVAL;
1008
1009         seg_tun = (struct ice_flow_seg_info *)
1010                 ice_malloc(hw, sizeof(*seg_tun) * ICE_FD_HW_SEG_MAX);
1011         if (!seg_tun) {
1012                 PMD_DRV_LOG(ERR, "No memory can be allocated");
1013                 return -ENOMEM;
1014         }
1015
1016         /* use seg_tun[1] to record tunnel inner part or non-tunnel */
1017         for (k = 0; k <= ICE_FD_HW_SEG_TUN; k++) {
1018                 seg = &seg_tun[k];
1019                 input_set = (k == ICE_FD_HW_SEG_TUN) ? inner_input_set : outer_input_set;
1020                 if (input_set == 0)
1021                         continue;
1022
1023                 for (i = 0; i < ICE_FLOW_FIELD_IDX_MAX; i++)
1024                         field[i] = ICE_FLOW_FIELD_IDX_MAX;
1025
1026                 ice_fdir_input_set_parse(input_set, field);
1027
1028                 ice_fdir_input_set_hdrs(flow, seg);
1029
1030                 for (i = 0; field[i] != ICE_FLOW_FIELD_IDX_MAX; i++) {
1031                         ice_flow_set_fld(seg, field[i],
1032                                          ICE_FLOW_FLD_OFF_INVAL,
1033                                          ICE_FLOW_FLD_OFF_INVAL,
1034                                          ICE_FLOW_FLD_OFF_INVAL, false);
1035                 }
1036         }
1037
1038         is_tunnel = ice_fdir_is_tunnel_profile(ttype);
1039         if (!is_tunnel) {
1040                 ret = ice_fdir_hw_tbl_conf(pf, pf->main_vsi, pf->fdir.fdir_vsi,
1041                                            seg_tun + 1, flow, false);
1042         } else {
1043                 ret = ice_fdir_hw_tbl_conf(pf, pf->main_vsi, pf->fdir.fdir_vsi,
1044                                            seg_tun, flow, true);
1045         }
1046
1047         if (!ret) {
1048                 return ret;
1049         } else if (ret < 0) {
1050                 rte_free(seg_tun);
1051                 return (ret == -EEXIST) ? 0 : ret;
1052         } else {
1053                 return ret;
1054         }
1055 }
1056
1057 static void
1058 ice_fdir_cnt_update(struct ice_pf *pf, enum ice_fltr_ptype ptype,
1059                     bool is_tunnel, bool add)
1060 {
1061         struct ice_hw *hw = ICE_PF_TO_HW(pf);
1062         int cnt;
1063
1064         cnt = (add) ? 1 : -1;
1065         hw->fdir_active_fltr += cnt;
1066         if (ptype == ICE_FLTR_PTYPE_NONF_NONE || ptype >= ICE_FLTR_PTYPE_MAX)
1067                 PMD_DRV_LOG(ERR, "Unknown filter type %d", ptype);
1068         else
1069                 pf->fdir_fltr_cnt[ptype][is_tunnel] += cnt;
1070 }
1071
1072 static int
1073 ice_fdir_init(struct ice_adapter *ad)
1074 {
1075         struct ice_pf *pf = &ad->pf;
1076         struct ice_flow_parser *parser;
1077         int ret;
1078
1079         if (ad->hw.dcf_enabled)
1080                 return 0;
1081
1082         ret = ice_fdir_setup(pf);
1083         if (ret)
1084                 return ret;
1085
1086         parser = &ice_fdir_parser;
1087
1088         return ice_register_parser(parser, ad);
1089 }
1090
1091 static void
1092 ice_fdir_uninit(struct ice_adapter *ad)
1093 {
1094         struct ice_flow_parser *parser;
1095         struct ice_pf *pf = &ad->pf;
1096
1097         if (ad->hw.dcf_enabled)
1098                 return;
1099
1100         parser = &ice_fdir_parser;
1101
1102         ice_unregister_parser(parser, ad);
1103
1104         ice_fdir_teardown(pf);
1105 }
1106
1107 static int
1108 ice_fdir_is_tunnel_profile(enum ice_fdir_tunnel_type tunnel_type)
1109 {
1110         if (tunnel_type == ICE_FDIR_TUNNEL_TYPE_VXLAN)
1111                 return 1;
1112         else
1113                 return 0;
1114 }
1115
1116 static int
1117 ice_fdir_add_del_filter(struct ice_pf *pf,
1118                         struct ice_fdir_filter_conf *filter,
1119                         bool add)
1120 {
1121         struct ice_fltr_desc desc;
1122         struct ice_hw *hw = ICE_PF_TO_HW(pf);
1123         unsigned char *pkt = (unsigned char *)pf->fdir.prg_pkt;
1124         bool is_tun;
1125         int ret;
1126
1127         filter->input.dest_vsi = pf->main_vsi->idx;
1128
1129         memset(&desc, 0, sizeof(desc));
1130         filter->input.comp_report = ICE_FXD_FLTR_QW0_COMP_REPORT_SW;
1131         ice_fdir_get_prgm_desc(hw, &filter->input, &desc, add);
1132
1133         is_tun = ice_fdir_is_tunnel_profile(filter->tunnel_type);
1134
1135         memset(pkt, 0, ICE_FDIR_PKT_LEN);
1136         ret = ice_fdir_get_gen_prgm_pkt(hw, &filter->input, pkt, false, is_tun);
1137         if (ret) {
1138                 PMD_DRV_LOG(ERR, "Generate dummy packet failed");
1139                 return -EINVAL;
1140         }
1141
1142         return ice_fdir_programming(pf, &desc);
1143 }
1144
1145 static void
1146 ice_fdir_extract_fltr_key(struct ice_fdir_fltr_pattern *key,
1147                           struct ice_fdir_filter_conf *filter)
1148 {
1149         struct ice_fdir_fltr *input = &filter->input;
1150         memset(key, 0, sizeof(*key));
1151
1152         key->flow_type = input->flow_type;
1153         rte_memcpy(&key->ip, &input->ip, sizeof(key->ip));
1154         rte_memcpy(&key->mask, &input->mask, sizeof(key->mask));
1155         rte_memcpy(&key->ext_data, &input->ext_data, sizeof(key->ext_data));
1156         rte_memcpy(&key->ext_mask, &input->ext_mask, sizeof(key->ext_mask));
1157
1158         rte_memcpy(&key->gtpu_data, &input->gtpu_data, sizeof(key->gtpu_data));
1159         rte_memcpy(&key->gtpu_mask, &input->gtpu_mask, sizeof(key->gtpu_mask));
1160
1161         key->tunnel_type = filter->tunnel_type;
1162 }
1163
1164 /* Check if there exists the flow director filter */
1165 static struct ice_fdir_filter_conf *
1166 ice_fdir_entry_lookup(struct ice_fdir_info *fdir_info,
1167                         const struct ice_fdir_fltr_pattern *key)
1168 {
1169         int ret;
1170
1171         ret = rte_hash_lookup(fdir_info->hash_table, key);
1172         if (ret < 0)
1173                 return NULL;
1174
1175         return fdir_info->hash_map[ret];
1176 }
1177
1178 /* Add a flow director entry into the SW list */
1179 static int
1180 ice_fdir_entry_insert(struct ice_pf *pf,
1181                       struct ice_fdir_filter_conf *entry,
1182                       struct ice_fdir_fltr_pattern *key)
1183 {
1184         struct ice_fdir_info *fdir_info = &pf->fdir;
1185         int ret;
1186
1187         ret = rte_hash_add_key(fdir_info->hash_table, key);
1188         if (ret < 0) {
1189                 PMD_DRV_LOG(ERR,
1190                             "Failed to insert fdir entry to hash table %d!",
1191                             ret);
1192                 return ret;
1193         }
1194         fdir_info->hash_map[ret] = entry;
1195
1196         return 0;
1197 }
1198
1199 /* Delete a flow director entry from the SW list */
1200 static int
1201 ice_fdir_entry_del(struct ice_pf *pf, struct ice_fdir_fltr_pattern *key)
1202 {
1203         struct ice_fdir_info *fdir_info = &pf->fdir;
1204         int ret;
1205
1206         ret = rte_hash_del_key(fdir_info->hash_table, key);
1207         if (ret < 0) {
1208                 PMD_DRV_LOG(ERR,
1209                             "Failed to delete fdir filter to hash table %d!",
1210                             ret);
1211                 return ret;
1212         }
1213         fdir_info->hash_map[ret] = NULL;
1214
1215         return 0;
1216 }
1217
1218 static int
1219 ice_fdir_create_filter(struct ice_adapter *ad,
1220                        struct rte_flow *flow,
1221                        void *meta,
1222                        struct rte_flow_error *error)
1223 {
1224         struct ice_pf *pf = &ad->pf;
1225         struct ice_fdir_filter_conf *filter = meta;
1226         struct ice_fdir_info *fdir_info = &pf->fdir;
1227         struct ice_fdir_filter_conf *entry, *node;
1228         struct ice_fdir_fltr_pattern key;
1229         bool is_tun;
1230         int ret;
1231
1232         ice_fdir_extract_fltr_key(&key, filter);
1233         node = ice_fdir_entry_lookup(fdir_info, &key);
1234         if (node) {
1235                 rte_flow_error_set(error, EEXIST,
1236                                    RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
1237                                    "Rule already exists!");
1238                 return -rte_errno;
1239         }
1240
1241         entry = rte_zmalloc("fdir_entry", sizeof(*entry), 0);
1242         if (!entry) {
1243                 rte_flow_error_set(error, ENOMEM,
1244                                    RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
1245                                    "Failed to allocate memory");
1246                 return -rte_errno;
1247         }
1248
1249         is_tun = ice_fdir_is_tunnel_profile(filter->tunnel_type);
1250
1251         ret = ice_fdir_input_set_conf(pf, filter->input.flow_type,
1252                                       filter->input_set_i, filter->input_set_o,
1253                                       filter->tunnel_type);
1254         if (ret) {
1255                 rte_flow_error_set(error, -ret,
1256                                    RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
1257                                    "Profile configure failed.");
1258                 goto free_entry;
1259         }
1260
1261         /* alloc counter for FDIR */
1262         if (filter->input.cnt_ena) {
1263                 struct rte_flow_action_count *act_count = &filter->act_count;
1264
1265                 filter->counter = ice_fdir_counter_alloc(pf,
1266                                                          act_count->shared,
1267                                                          act_count->id);
1268                 if (!filter->counter) {
1269                         rte_flow_error_set(error, EINVAL,
1270                                         RTE_FLOW_ERROR_TYPE_ACTION, NULL,
1271                                         "Failed to alloc FDIR counter.");
1272                         goto free_entry;
1273                 }
1274                 filter->input.cnt_index = filter->counter->hw_index;
1275         }
1276
1277         ret = ice_fdir_add_del_filter(pf, filter, true);
1278         if (ret) {
1279                 rte_flow_error_set(error, -ret,
1280                                    RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
1281                                    "Add filter rule failed.");
1282                 goto free_counter;
1283         }
1284
1285         if (filter->mark_flag == 1)
1286                 ice_fdir_rx_parsing_enable(ad, 1);
1287
1288         rte_memcpy(entry, filter, sizeof(*entry));
1289         ret = ice_fdir_entry_insert(pf, entry, &key);
1290         if (ret) {
1291                 rte_flow_error_set(error, -ret,
1292                                    RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
1293                                    "Insert entry to table failed.");
1294                 goto free_entry;
1295         }
1296
1297         flow->rule = entry;
1298         ice_fdir_cnt_update(pf, filter->input.flow_type, is_tun, true);
1299
1300         return 0;
1301
1302 free_counter:
1303         if (filter->counter) {
1304                 ice_fdir_counter_free(pf, filter->counter);
1305                 filter->counter = NULL;
1306         }
1307
1308 free_entry:
1309         rte_free(entry);
1310         return -rte_errno;
1311 }
1312
1313 static int
1314 ice_fdir_destroy_filter(struct ice_adapter *ad,
1315                         struct rte_flow *flow,
1316                         struct rte_flow_error *error)
1317 {
1318         struct ice_pf *pf = &ad->pf;
1319         struct ice_fdir_info *fdir_info = &pf->fdir;
1320         struct ice_fdir_filter_conf *filter, *entry;
1321         struct ice_fdir_fltr_pattern key;
1322         bool is_tun;
1323         int ret;
1324
1325         filter = (struct ice_fdir_filter_conf *)flow->rule;
1326
1327         is_tun = ice_fdir_is_tunnel_profile(filter->tunnel_type);
1328
1329         if (filter->counter) {
1330                 ice_fdir_counter_free(pf, filter->counter);
1331                 filter->counter = NULL;
1332         }
1333
1334         ice_fdir_extract_fltr_key(&key, filter);
1335         entry = ice_fdir_entry_lookup(fdir_info, &key);
1336         if (!entry) {
1337                 rte_flow_error_set(error, ENOENT,
1338                                    RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
1339                                    "Can't find entry.");
1340                 return -rte_errno;
1341         }
1342
1343         ret = ice_fdir_add_del_filter(pf, filter, false);
1344         if (ret) {
1345                 rte_flow_error_set(error, -ret,
1346                                    RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
1347                                    "Del filter rule failed.");
1348                 return -rte_errno;
1349         }
1350
1351         ret = ice_fdir_entry_del(pf, &key);
1352         if (ret) {
1353                 rte_flow_error_set(error, -ret,
1354                                    RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
1355                                    "Remove entry from table failed.");
1356                 return -rte_errno;
1357         }
1358
1359         ice_fdir_cnt_update(pf, filter->input.flow_type, is_tun, false);
1360
1361         if (filter->mark_flag == 1)
1362                 ice_fdir_rx_parsing_enable(ad, 0);
1363
1364         flow->rule = NULL;
1365
1366         rte_free(filter);
1367
1368         return 0;
1369 }
1370
1371 static int
1372 ice_fdir_query_count(struct ice_adapter *ad,
1373                       struct rte_flow *flow,
1374                       struct rte_flow_query_count *flow_stats,
1375                       struct rte_flow_error *error)
1376 {
1377         struct ice_pf *pf = &ad->pf;
1378         struct ice_hw *hw = ICE_PF_TO_HW(pf);
1379         struct ice_fdir_filter_conf *filter = flow->rule;
1380         struct ice_fdir_counter *counter = filter->counter;
1381         uint64_t hits_lo, hits_hi;
1382
1383         if (!counter) {
1384                 rte_flow_error_set(error, EINVAL,
1385                                   RTE_FLOW_ERROR_TYPE_ACTION,
1386                                   NULL,
1387                                   "FDIR counters not available");
1388                 return -rte_errno;
1389         }
1390
1391         /*
1392          * Reading the low 32-bits latches the high 32-bits into a shadow
1393          * register. Reading the high 32-bit returns the value in the
1394          * shadow register.
1395          */
1396         hits_lo = ICE_READ_REG(hw, GLSTAT_FD_CNT0L(counter->hw_index));
1397         hits_hi = ICE_READ_REG(hw, GLSTAT_FD_CNT0H(counter->hw_index));
1398
1399         flow_stats->hits_set = 1;
1400         flow_stats->hits = hits_lo | (hits_hi << 32);
1401         flow_stats->bytes_set = 0;
1402         flow_stats->bytes = 0;
1403
1404         if (flow_stats->reset) {
1405                 /* reset statistic counter value */
1406                 ICE_WRITE_REG(hw, GLSTAT_FD_CNT0H(counter->hw_index), 0);
1407                 ICE_WRITE_REG(hw, GLSTAT_FD_CNT0L(counter->hw_index), 0);
1408         }
1409
1410         return 0;
1411 }
1412
1413 static struct ice_flow_engine ice_fdir_engine = {
1414         .init = ice_fdir_init,
1415         .uninit = ice_fdir_uninit,
1416         .create = ice_fdir_create_filter,
1417         .destroy = ice_fdir_destroy_filter,
1418         .query_count = ice_fdir_query_count,
1419         .type = ICE_FLOW_ENGINE_FDIR,
1420 };
1421
1422 static int
1423 ice_fdir_parse_action_qregion(struct ice_pf *pf,
1424                               struct rte_flow_error *error,
1425                               const struct rte_flow_action *act,
1426                               struct ice_fdir_filter_conf *filter)
1427 {
1428         const struct rte_flow_action_rss *rss = act->conf;
1429         uint32_t i;
1430
1431         if (act->type != RTE_FLOW_ACTION_TYPE_RSS) {
1432                 rte_flow_error_set(error, EINVAL,
1433                                    RTE_FLOW_ERROR_TYPE_ACTION, act,
1434                                    "Invalid action.");
1435                 return -rte_errno;
1436         }
1437
1438         if (rss->queue_num <= 1) {
1439                 rte_flow_error_set(error, EINVAL,
1440                                    RTE_FLOW_ERROR_TYPE_ACTION, act,
1441                                    "Queue region size can't be 0 or 1.");
1442                 return -rte_errno;
1443         }
1444
1445         /* check if queue index for queue region is continuous */
1446         for (i = 0; i < rss->queue_num - 1; i++) {
1447                 if (rss->queue[i + 1] != rss->queue[i] + 1) {
1448                         rte_flow_error_set(error, EINVAL,
1449                                            RTE_FLOW_ERROR_TYPE_ACTION, act,
1450                                            "Discontinuous queue region");
1451                         return -rte_errno;
1452                 }
1453         }
1454
1455         if (rss->queue[rss->queue_num - 1] >= pf->dev_data->nb_rx_queues) {
1456                 rte_flow_error_set(error, EINVAL,
1457                                    RTE_FLOW_ERROR_TYPE_ACTION, act,
1458                                    "Invalid queue region indexes.");
1459                 return -rte_errno;
1460         }
1461
1462         if (!(rte_is_power_of_2(rss->queue_num) &&
1463              (rss->queue_num <= ICE_FDIR_MAX_QREGION_SIZE))) {
1464                 rte_flow_error_set(error, EINVAL,
1465                                    RTE_FLOW_ERROR_TYPE_ACTION, act,
1466                                    "The region size should be any of the following values:"
1467                                    "1, 2, 4, 8, 16, 32, 64, 128 as long as the total number "
1468                                    "of queues do not exceed the VSI allocation.");
1469                 return -rte_errno;
1470         }
1471
1472         filter->input.q_index = rss->queue[0];
1473         filter->input.q_region = rte_fls_u32(rss->queue_num) - 1;
1474         filter->input.dest_ctl = ICE_FLTR_PRGM_DESC_DEST_DIRECT_PKT_QGROUP;
1475
1476         return 0;
1477 }
1478
1479 static int
1480 ice_fdir_parse_action(struct ice_adapter *ad,
1481                       const struct rte_flow_action actions[],
1482                       struct rte_flow_error *error,
1483                       struct ice_fdir_filter_conf *filter)
1484 {
1485         struct ice_pf *pf = &ad->pf;
1486         const struct rte_flow_action_queue *act_q;
1487         const struct rte_flow_action_mark *mark_spec = NULL;
1488         const struct rte_flow_action_count *act_count;
1489         uint32_t dest_num = 0;
1490         uint32_t mark_num = 0;
1491         uint32_t counter_num = 0;
1492         int ret;
1493
1494         for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
1495                 switch (actions->type) {
1496                 case RTE_FLOW_ACTION_TYPE_VOID:
1497                         break;
1498                 case RTE_FLOW_ACTION_TYPE_QUEUE:
1499                         dest_num++;
1500
1501                         act_q = actions->conf;
1502                         filter->input.q_index = act_q->index;
1503                         if (filter->input.q_index >=
1504                                         pf->dev_data->nb_rx_queues) {
1505                                 rte_flow_error_set(error, EINVAL,
1506                                                    RTE_FLOW_ERROR_TYPE_ACTION,
1507                                                    actions,
1508                                                    "Invalid queue for FDIR.");
1509                                 return -rte_errno;
1510                         }
1511                         filter->input.dest_ctl =
1512                                 ICE_FLTR_PRGM_DESC_DEST_DIRECT_PKT_QINDEX;
1513                         break;
1514                 case RTE_FLOW_ACTION_TYPE_DROP:
1515                         dest_num++;
1516
1517                         filter->input.dest_ctl =
1518                                 ICE_FLTR_PRGM_DESC_DEST_DROP_PKT;
1519                         break;
1520                 case RTE_FLOW_ACTION_TYPE_PASSTHRU:
1521                         dest_num++;
1522
1523                         filter->input.dest_ctl =
1524                                 ICE_FLTR_PRGM_DESC_DEST_DIRECT_PKT_OTHER;
1525                         break;
1526                 case RTE_FLOW_ACTION_TYPE_RSS:
1527                         dest_num++;
1528
1529                         ret = ice_fdir_parse_action_qregion(pf,
1530                                                 error, actions, filter);
1531                         if (ret)
1532                                 return ret;
1533                         break;
1534                 case RTE_FLOW_ACTION_TYPE_MARK:
1535                         mark_num++;
1536                         filter->mark_flag = 1;
1537                         mark_spec = actions->conf;
1538                         filter->input.fltr_id = mark_spec->id;
1539                         filter->input.fdid_prio = ICE_FXD_FLTR_QW1_FDID_PRI_ONE;
1540                         break;
1541                 case RTE_FLOW_ACTION_TYPE_COUNT:
1542                         counter_num++;
1543
1544                         act_count = actions->conf;
1545                         filter->input.cnt_ena = ICE_FXD_FLTR_QW0_STAT_ENA_PKTS;
1546                         rte_memcpy(&filter->act_count, act_count,
1547                                                 sizeof(filter->act_count));
1548
1549                         break;
1550                 default:
1551                         rte_flow_error_set(error, EINVAL,
1552                                    RTE_FLOW_ERROR_TYPE_ACTION, actions,
1553                                    "Invalid action.");
1554                         return -rte_errno;
1555                 }
1556         }
1557
1558         if (dest_num >= 2) {
1559                 rte_flow_error_set(error, EINVAL,
1560                            RTE_FLOW_ERROR_TYPE_ACTION, actions,
1561                            "Unsupported action combination");
1562                 return -rte_errno;
1563         }
1564
1565         if (mark_num >= 2) {
1566                 rte_flow_error_set(error, EINVAL,
1567                            RTE_FLOW_ERROR_TYPE_ACTION, actions,
1568                            "Too many mark actions");
1569                 return -rte_errno;
1570         }
1571
1572         if (counter_num >= 2) {
1573                 rte_flow_error_set(error, EINVAL,
1574                            RTE_FLOW_ERROR_TYPE_ACTION, actions,
1575                            "Too many count actions");
1576                 return -rte_errno;
1577         }
1578
1579         if (dest_num + mark_num + counter_num == 0) {
1580                 rte_flow_error_set(error, EINVAL,
1581                            RTE_FLOW_ERROR_TYPE_ACTION, actions,
1582                            "Empty action");
1583                 return -rte_errno;
1584         }
1585
1586         /* set default action to PASSTHRU mode, in "mark/count only" case. */
1587         if (dest_num == 0)
1588                 filter->input.dest_ctl =
1589                         ICE_FLTR_PRGM_DESC_DEST_DIRECT_PKT_OTHER;
1590
1591         return 0;
1592 }
1593
1594 static int
1595 ice_fdir_parse_pattern(__rte_unused struct ice_adapter *ad,
1596                        const struct rte_flow_item pattern[],
1597                        struct rte_flow_error *error,
1598                        struct ice_fdir_filter_conf *filter)
1599 {
1600         const struct rte_flow_item *item = pattern;
1601         enum rte_flow_item_type item_type;
1602         enum rte_flow_item_type l3 = RTE_FLOW_ITEM_TYPE_END;
1603         enum ice_fdir_tunnel_type tunnel_type = ICE_FDIR_TUNNEL_TYPE_NONE;
1604         const struct rte_flow_item_eth *eth_spec, *eth_mask;
1605         const struct rte_flow_item_ipv4 *ipv4_spec, *ipv4_mask;
1606         const struct rte_flow_item_ipv6 *ipv6_spec, *ipv6_mask;
1607         const struct rte_flow_item_tcp *tcp_spec, *tcp_mask;
1608         const struct rte_flow_item_udp *udp_spec, *udp_mask;
1609         const struct rte_flow_item_sctp *sctp_spec, *sctp_mask;
1610         const struct rte_flow_item_vxlan *vxlan_spec, *vxlan_mask;
1611         const struct rte_flow_item_gtp *gtp_spec, *gtp_mask;
1612         const struct rte_flow_item_gtp_psc *gtp_psc_spec, *gtp_psc_mask;
1613         uint64_t input_set_i = ICE_INSET_NONE; /* only for tunnel inner */
1614         uint64_t input_set_o = ICE_INSET_NONE; /* non-tunnel and tunnel outer */
1615         uint64_t *input_set;
1616         uint8_t flow_type = ICE_FLTR_PTYPE_NONF_NONE;
1617         uint8_t  ipv6_addr_mask[16] = {
1618                 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
1619                 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
1620         };
1621         uint32_t vtc_flow_cpu;
1622         uint16_t ether_type;
1623         enum rte_flow_item_type next_type;
1624         bool is_outer = true;
1625         struct ice_fdir_extra *p_ext_data;
1626         struct ice_fdir_v4 *p_v4 = NULL;
1627         struct ice_fdir_v6 *p_v6 = NULL;
1628
1629         for (item = pattern; item->type != RTE_FLOW_ITEM_TYPE_END; item++) {
1630                 if (item->type == RTE_FLOW_ITEM_TYPE_VXLAN)
1631                         tunnel_type = ICE_FDIR_TUNNEL_TYPE_VXLAN;
1632                 /* To align with shared code behavior, save gtpu outer
1633                  * fields in inner struct.
1634                  */
1635                 if (item->type == RTE_FLOW_ITEM_TYPE_GTPU ||
1636                     item->type == RTE_FLOW_ITEM_TYPE_GTP_PSC) {
1637                         is_outer = false;
1638                 }
1639         }
1640
1641         /* This loop parse flow pattern and distinguish Non-tunnel and tunnel
1642          * flow. input_set_i is used for inner part.
1643          */
1644         for (item = pattern; item->type != RTE_FLOW_ITEM_TYPE_END; item++) {
1645                 if (item->last) {
1646                         rte_flow_error_set(error, EINVAL,
1647                                            RTE_FLOW_ERROR_TYPE_ITEM,
1648                                            item,
1649                                            "Not support range");
1650                         return -rte_errno;
1651                 }
1652                 item_type = item->type;
1653
1654                 input_set = (tunnel_type && !is_outer) ?
1655                             &input_set_i : &input_set_o;
1656
1657                 switch (item_type) {
1658                 case RTE_FLOW_ITEM_TYPE_ETH:
1659                         flow_type = ICE_FLTR_PTYPE_NON_IP_L2;
1660                         eth_spec = item->spec;
1661                         eth_mask = item->mask;
1662
1663                         if (!(eth_spec && eth_mask))
1664                                 break;
1665
1666                         if (!rte_is_zero_ether_addr(&eth_mask->dst))
1667                                 *input_set |= ICE_INSET_DMAC;
1668                         if (!rte_is_zero_ether_addr(&eth_mask->src))
1669                                 *input_set |= ICE_INSET_SMAC;
1670
1671                         next_type = (item + 1)->type;
1672                         /* Ignore this field except for ICE_FLTR_PTYPE_NON_IP_L2 */
1673                         if (eth_mask->type == RTE_BE16(0xffff) &&
1674                             next_type == RTE_FLOW_ITEM_TYPE_END) {
1675                                 *input_set |= ICE_INSET_ETHERTYPE;
1676                                 ether_type = rte_be_to_cpu_16(eth_spec->type);
1677
1678                                 if (ether_type == RTE_ETHER_TYPE_IPV4 ||
1679                                     ether_type == RTE_ETHER_TYPE_IPV6) {
1680                                         rte_flow_error_set(error, EINVAL,
1681                                                            RTE_FLOW_ERROR_TYPE_ITEM,
1682                                                            item,
1683                                                            "Unsupported ether_type.");
1684                                         return -rte_errno;
1685                                 }
1686                         }
1687
1688                         p_ext_data = (tunnel_type && is_outer) ?
1689                                      &filter->input.ext_data_outer :
1690                                      &filter->input.ext_data;
1691                         rte_memcpy(&p_ext_data->src_mac,
1692                                    &eth_spec->src, RTE_ETHER_ADDR_LEN);
1693                         rte_memcpy(&p_ext_data->dst_mac,
1694                                    &eth_spec->dst, RTE_ETHER_ADDR_LEN);
1695                         rte_memcpy(&p_ext_data->ether_type,
1696                                    &eth_spec->type, sizeof(eth_spec->type));
1697                         break;
1698                 case RTE_FLOW_ITEM_TYPE_IPV4:
1699                         flow_type = ICE_FLTR_PTYPE_NONF_IPV4_OTHER;
1700                         l3 = RTE_FLOW_ITEM_TYPE_IPV4;
1701                         ipv4_spec = item->spec;
1702                         ipv4_mask = item->mask;
1703                         p_v4 = (tunnel_type && is_outer) ?
1704                                &filter->input.ip_outer.v4 :
1705                                &filter->input.ip.v4;
1706
1707                         if (!(ipv4_spec && ipv4_mask))
1708                                 break;
1709
1710                         /* Check IPv4 mask and update input set */
1711                         if (ipv4_mask->hdr.version_ihl ||
1712                             ipv4_mask->hdr.total_length ||
1713                             ipv4_mask->hdr.packet_id ||
1714                             ipv4_mask->hdr.fragment_offset ||
1715                             ipv4_mask->hdr.hdr_checksum) {
1716                                 rte_flow_error_set(error, EINVAL,
1717                                                    RTE_FLOW_ERROR_TYPE_ITEM,
1718                                                    item,
1719                                                    "Invalid IPv4 mask.");
1720                                 return -rte_errno;
1721                         }
1722
1723                         if (ipv4_mask->hdr.dst_addr == UINT32_MAX)
1724                                 *input_set |= ICE_INSET_IPV4_DST;
1725                         if (ipv4_mask->hdr.src_addr == UINT32_MAX)
1726                                 *input_set |= ICE_INSET_IPV4_SRC;
1727                         if (ipv4_mask->hdr.time_to_live == UINT8_MAX)
1728                                 *input_set |= ICE_INSET_IPV4_TTL;
1729                         if (ipv4_mask->hdr.next_proto_id == UINT8_MAX)
1730                                 *input_set |= ICE_INSET_IPV4_PROTO;
1731                         if (ipv4_mask->hdr.type_of_service == UINT8_MAX)
1732                                 *input_set |= ICE_INSET_IPV4_TOS;
1733
1734                         p_v4->dst_ip = ipv4_spec->hdr.dst_addr;
1735                         p_v4->src_ip = ipv4_spec->hdr.src_addr;
1736                         p_v4->ttl = ipv4_spec->hdr.time_to_live;
1737                         p_v4->proto = ipv4_spec->hdr.next_proto_id;
1738                         p_v4->tos = ipv4_spec->hdr.type_of_service;
1739                         break;
1740                 case RTE_FLOW_ITEM_TYPE_IPV6:
1741                         flow_type = ICE_FLTR_PTYPE_NONF_IPV6_OTHER;
1742                         l3 = RTE_FLOW_ITEM_TYPE_IPV6;
1743                         ipv6_spec = item->spec;
1744                         ipv6_mask = item->mask;
1745                         p_v6 = (tunnel_type && is_outer) ?
1746                                &filter->input.ip_outer.v6 :
1747                                &filter->input.ip.v6;
1748
1749                         if (!(ipv6_spec && ipv6_mask))
1750                                 break;
1751
1752                         /* Check IPv6 mask and update input set */
1753                         if (ipv6_mask->hdr.payload_len) {
1754                                 rte_flow_error_set(error, EINVAL,
1755                                                    RTE_FLOW_ERROR_TYPE_ITEM,
1756                                                    item,
1757                                                    "Invalid IPv6 mask");
1758                                 return -rte_errno;
1759                         }
1760
1761                         if (!memcmp(ipv6_mask->hdr.src_addr, ipv6_addr_mask,
1762                                     RTE_DIM(ipv6_mask->hdr.src_addr)))
1763                                 *input_set |= ICE_INSET_IPV6_SRC;
1764                         if (!memcmp(ipv6_mask->hdr.dst_addr, ipv6_addr_mask,
1765                                     RTE_DIM(ipv6_mask->hdr.dst_addr)))
1766                                 *input_set |= ICE_INSET_IPV6_DST;
1767
1768                         if ((ipv6_mask->hdr.vtc_flow &
1769                              rte_cpu_to_be_32(ICE_IPV6_TC_MASK))
1770                             == rte_cpu_to_be_32(ICE_IPV6_TC_MASK))
1771                                 *input_set |= ICE_INSET_IPV6_TC;
1772                         if (ipv6_mask->hdr.proto == UINT8_MAX)
1773                                 *input_set |= ICE_INSET_IPV6_NEXT_HDR;
1774                         if (ipv6_mask->hdr.hop_limits == UINT8_MAX)
1775                                 *input_set |= ICE_INSET_IPV6_HOP_LIMIT;
1776
1777                         rte_memcpy(&p_v6->dst_ip, ipv6_spec->hdr.dst_addr, 16);
1778                         rte_memcpy(&p_v6->src_ip, ipv6_spec->hdr.src_addr, 16);
1779                         vtc_flow_cpu = rte_be_to_cpu_32(ipv6_spec->hdr.vtc_flow);
1780                         p_v6->tc = (uint8_t)(vtc_flow_cpu >> ICE_FDIR_IPV6_TC_OFFSET);
1781                         p_v6->proto = ipv6_spec->hdr.proto;
1782                         p_v6->hlim = ipv6_spec->hdr.hop_limits;
1783                         break;
1784                 case RTE_FLOW_ITEM_TYPE_TCP:
1785                         if (l3 == RTE_FLOW_ITEM_TYPE_IPV4)
1786                                 flow_type = ICE_FLTR_PTYPE_NONF_IPV4_TCP;
1787                         if (l3 == RTE_FLOW_ITEM_TYPE_IPV6)
1788                                 flow_type = ICE_FLTR_PTYPE_NONF_IPV6_TCP;
1789
1790                         tcp_spec = item->spec;
1791                         tcp_mask = item->mask;
1792
1793                         if (!(tcp_spec && tcp_mask))
1794                                 break;
1795
1796                         /* Check TCP mask and update input set */
1797                         if (tcp_mask->hdr.sent_seq ||
1798                             tcp_mask->hdr.recv_ack ||
1799                             tcp_mask->hdr.data_off ||
1800                             tcp_mask->hdr.tcp_flags ||
1801                             tcp_mask->hdr.rx_win ||
1802                             tcp_mask->hdr.cksum ||
1803                             tcp_mask->hdr.tcp_urp) {
1804                                 rte_flow_error_set(error, EINVAL,
1805                                                    RTE_FLOW_ERROR_TYPE_ITEM,
1806                                                    item,
1807                                                    "Invalid TCP mask");
1808                                 return -rte_errno;
1809                         }
1810
1811                         if (tcp_mask->hdr.src_port == UINT16_MAX)
1812                                 *input_set |= ICE_INSET_TCP_SRC_PORT;
1813                         if (tcp_mask->hdr.dst_port == UINT16_MAX)
1814                                 *input_set |= ICE_INSET_TCP_DST_PORT;
1815
1816                         /* Get filter info */
1817                         if (l3 == RTE_FLOW_ITEM_TYPE_IPV4) {
1818                                 assert(p_v4);
1819                                 p_v4->dst_port = tcp_spec->hdr.dst_port;
1820                                 p_v4->src_port = tcp_spec->hdr.src_port;
1821                         } else if (l3 == RTE_FLOW_ITEM_TYPE_IPV6) {
1822                                 assert(p_v6);
1823                                 p_v6->dst_port = tcp_spec->hdr.dst_port;
1824                                 p_v6->src_port = tcp_spec->hdr.src_port;
1825                         }
1826                         break;
1827                 case RTE_FLOW_ITEM_TYPE_UDP:
1828                         if (l3 == RTE_FLOW_ITEM_TYPE_IPV4)
1829                                 flow_type = ICE_FLTR_PTYPE_NONF_IPV4_UDP;
1830                         if (l3 == RTE_FLOW_ITEM_TYPE_IPV6)
1831                                 flow_type = ICE_FLTR_PTYPE_NONF_IPV6_UDP;
1832
1833                         udp_spec = item->spec;
1834                         udp_mask = item->mask;
1835
1836                         if (!(udp_spec && udp_mask))
1837                                 break;
1838
1839                         /* Check UDP mask and update input set*/
1840                         if (udp_mask->hdr.dgram_len ||
1841                             udp_mask->hdr.dgram_cksum) {
1842                                 rte_flow_error_set(error, EINVAL,
1843                                                    RTE_FLOW_ERROR_TYPE_ITEM,
1844                                                    item,
1845                                                    "Invalid UDP mask");
1846                                 return -rte_errno;
1847                         }
1848
1849                         if (udp_mask->hdr.src_port == UINT16_MAX)
1850                                 *input_set |= ICE_INSET_UDP_SRC_PORT;
1851                         if (udp_mask->hdr.dst_port == UINT16_MAX)
1852                                 *input_set |= ICE_INSET_UDP_DST_PORT;
1853
1854                         /* Get filter info */
1855                         if (l3 == RTE_FLOW_ITEM_TYPE_IPV4) {
1856                                 assert(p_v4);
1857                                 p_v4->dst_port = udp_spec->hdr.dst_port;
1858                                 p_v4->src_port = udp_spec->hdr.src_port;
1859                         } else if (l3 == RTE_FLOW_ITEM_TYPE_IPV6) {
1860                                 assert(p_v6);
1861                                 p_v6->src_port = udp_spec->hdr.src_port;
1862                                 p_v6->dst_port = udp_spec->hdr.dst_port;
1863                         }
1864                         break;
1865                 case RTE_FLOW_ITEM_TYPE_SCTP:
1866                         if (l3 == RTE_FLOW_ITEM_TYPE_IPV4)
1867                                 flow_type = ICE_FLTR_PTYPE_NONF_IPV4_SCTP;
1868                         if (l3 == RTE_FLOW_ITEM_TYPE_IPV6)
1869                                 flow_type = ICE_FLTR_PTYPE_NONF_IPV6_SCTP;
1870
1871                         sctp_spec = item->spec;
1872                         sctp_mask = item->mask;
1873
1874                         if (!(sctp_spec && sctp_mask))
1875                                 break;
1876
1877                         /* Check SCTP mask and update input set */
1878                         if (sctp_mask->hdr.cksum) {
1879                                 rte_flow_error_set(error, EINVAL,
1880                                                    RTE_FLOW_ERROR_TYPE_ITEM,
1881                                                    item,
1882                                                    "Invalid UDP mask");
1883                                 return -rte_errno;
1884                         }
1885
1886                         if (sctp_mask->hdr.src_port == UINT16_MAX)
1887                                 *input_set |= ICE_INSET_SCTP_SRC_PORT;
1888                         if (sctp_mask->hdr.dst_port == UINT16_MAX)
1889                                 *input_set |= ICE_INSET_SCTP_DST_PORT;
1890
1891                         /* Get filter info */
1892                         if (l3 == RTE_FLOW_ITEM_TYPE_IPV4) {
1893                                 assert(p_v4);
1894                                 p_v4->dst_port = sctp_spec->hdr.dst_port;
1895                                 p_v4->src_port = sctp_spec->hdr.src_port;
1896                         } else if (l3 == RTE_FLOW_ITEM_TYPE_IPV6) {
1897                                 assert(p_v6);
1898                                 p_v6->dst_port = sctp_spec->hdr.dst_port;
1899                                 p_v6->src_port = sctp_spec->hdr.src_port;
1900                         }
1901                         break;
1902                 case RTE_FLOW_ITEM_TYPE_VOID:
1903                         break;
1904                 case RTE_FLOW_ITEM_TYPE_VXLAN:
1905                         l3 = RTE_FLOW_ITEM_TYPE_END;
1906                         vxlan_spec = item->spec;
1907                         vxlan_mask = item->mask;
1908                         is_outer = false;
1909
1910                         if (vxlan_spec || vxlan_mask) {
1911                                 rte_flow_error_set(error, EINVAL,
1912                                                    RTE_FLOW_ERROR_TYPE_ITEM,
1913                                                    item,
1914                                                    "Invalid vxlan field");
1915                                 return -rte_errno;
1916                         }
1917
1918                         break;
1919                 case RTE_FLOW_ITEM_TYPE_GTPU:
1920                         l3 = RTE_FLOW_ITEM_TYPE_END;
1921                         tunnel_type = ICE_FDIR_TUNNEL_TYPE_GTPU;
1922                         gtp_spec = item->spec;
1923                         gtp_mask = item->mask;
1924
1925                         if (!(gtp_spec && gtp_mask))
1926                                 break;
1927
1928                         if (gtp_mask->v_pt_rsv_flags ||
1929                             gtp_mask->msg_type ||
1930                             gtp_mask->msg_len) {
1931                                 rte_flow_error_set(error, EINVAL,
1932                                                    RTE_FLOW_ERROR_TYPE_ITEM,
1933                                                    item,
1934                                                    "Invalid GTP mask");
1935                                 return -rte_errno;
1936                         }
1937
1938                         if (gtp_mask->teid == UINT32_MAX)
1939                                 input_set_o |= ICE_INSET_GTPU_TEID;
1940
1941                         filter->input.gtpu_data.teid = gtp_spec->teid;
1942                         break;
1943                 case RTE_FLOW_ITEM_TYPE_GTP_PSC:
1944                         tunnel_type = ICE_FDIR_TUNNEL_TYPE_GTPU_EH;
1945                         gtp_psc_spec = item->spec;
1946                         gtp_psc_mask = item->mask;
1947
1948                         if (!(gtp_psc_spec && gtp_psc_mask))
1949                                 break;
1950
1951                         if (gtp_psc_mask->qfi == UINT8_MAX)
1952                                 input_set_o |= ICE_INSET_GTPU_QFI;
1953
1954                         filter->input.gtpu_data.qfi =
1955                                 gtp_psc_spec->qfi;
1956                         break;
1957                 default:
1958                         rte_flow_error_set(error, EINVAL,
1959                                            RTE_FLOW_ERROR_TYPE_ITEM,
1960                                            item,
1961                                            "Invalid pattern item.");
1962                         return -rte_errno;
1963                 }
1964         }
1965
1966         if (tunnel_type == ICE_FDIR_TUNNEL_TYPE_GTPU &&
1967                 flow_type == ICE_FLTR_PTYPE_NONF_IPV4_UDP)
1968                 flow_type = ICE_FLTR_PTYPE_NONF_IPV4_GTPU_IPV4_OTHER;
1969         else if (tunnel_type == ICE_FDIR_TUNNEL_TYPE_GTPU_EH &&
1970                 flow_type == ICE_FLTR_PTYPE_NONF_IPV4_UDP)
1971                 flow_type = ICE_FLTR_PTYPE_NONF_IPV4_GTPU_EH_IPV4_OTHER;
1972         else if (tunnel_type == ICE_FDIR_TUNNEL_TYPE_GTPU &&
1973                 flow_type == ICE_FLTR_PTYPE_NONF_IPV6_UDP)
1974                 flow_type = ICE_FLTR_PTYPE_NONF_IPV6_GTPU_IPV6_OTHER;
1975         else if (tunnel_type == ICE_FDIR_TUNNEL_TYPE_GTPU_EH &&
1976                 flow_type == ICE_FLTR_PTYPE_NONF_IPV6_UDP)
1977                 flow_type = ICE_FLTR_PTYPE_NONF_IPV6_GTPU_EH_IPV6_OTHER;
1978
1979         filter->tunnel_type = tunnel_type;
1980         filter->input.flow_type = flow_type;
1981         filter->input_set_o = input_set_o;
1982         filter->input_set_i = input_set_i;
1983
1984         return 0;
1985 }
1986
1987 static int
1988 ice_fdir_parse(struct ice_adapter *ad,
1989                struct ice_pattern_match_item *array,
1990                uint32_t array_len,
1991                const struct rte_flow_item pattern[],
1992                const struct rte_flow_action actions[],
1993                void **meta,
1994                struct rte_flow_error *error)
1995 {
1996         struct ice_pf *pf = &ad->pf;
1997         struct ice_fdir_filter_conf *filter = &pf->fdir.conf;
1998         struct ice_pattern_match_item *item = NULL;
1999         uint64_t input_set;
2000         int ret;
2001
2002         memset(filter, 0, sizeof(*filter));
2003         item = ice_search_pattern_match_item(ad, pattern, array, array_len,
2004                                              error);
2005         if (!item)
2006                 return -rte_errno;
2007
2008         ret = ice_fdir_parse_pattern(ad, pattern, error, filter);
2009         if (ret)
2010                 goto error;
2011         input_set = filter->input_set_o | filter->input_set_i;
2012         if (!input_set || input_set & ~item->input_set_mask) {
2013                 rte_flow_error_set(error, EINVAL,
2014                                    RTE_FLOW_ERROR_TYPE_ITEM_SPEC,
2015                                    pattern,
2016                                    "Invalid input set");
2017                 ret = -rte_errno;
2018                 goto error;
2019         }
2020
2021         ret = ice_fdir_parse_action(ad, actions, error, filter);
2022         if (ret)
2023                 goto error;
2024
2025         if (meta)
2026                 *meta = filter;
2027 error:
2028         rte_free(item);
2029         return ret;
2030 }
2031
2032 static struct ice_flow_parser ice_fdir_parser = {
2033         .engine = &ice_fdir_engine,
2034         .array = ice_fdir_pattern_list,
2035         .array_len = RTE_DIM(ice_fdir_pattern_list),
2036         .parse_pattern_action = ice_fdir_parse,
2037         .stage = ICE_FLOW_STAGE_DISTRIBUTOR,
2038 };
2039
2040 RTE_INIT(ice_fdir_engine_register)
2041 {
2042         ice_register_flow_engine(&ice_fdir_engine);
2043 }