net/ice: refactor input set config
[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 */
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
1040         ret = ice_fdir_hw_tbl_conf(pf, pf->main_vsi, pf->fdir.fdir_vsi,
1041                                    seg_tun, flow, is_tunnel);
1042
1043         if (!ret) {
1044                 return ret;
1045         } else if (ret < 0) {
1046                 rte_free(seg_tun);
1047                 return (ret == -EEXIST) ? 0 : ret;
1048         } else {
1049                 return ret;
1050         }
1051 }
1052
1053 static void
1054 ice_fdir_cnt_update(struct ice_pf *pf, enum ice_fltr_ptype ptype,
1055                     bool is_tunnel, bool add)
1056 {
1057         struct ice_hw *hw = ICE_PF_TO_HW(pf);
1058         int cnt;
1059
1060         cnt = (add) ? 1 : -1;
1061         hw->fdir_active_fltr += cnt;
1062         if (ptype == ICE_FLTR_PTYPE_NONF_NONE || ptype >= ICE_FLTR_PTYPE_MAX)
1063                 PMD_DRV_LOG(ERR, "Unknown filter type %d", ptype);
1064         else
1065                 pf->fdir_fltr_cnt[ptype][is_tunnel] += cnt;
1066 }
1067
1068 static int
1069 ice_fdir_init(struct ice_adapter *ad)
1070 {
1071         struct ice_pf *pf = &ad->pf;
1072         struct ice_flow_parser *parser;
1073         int ret;
1074
1075         if (ad->hw.dcf_enabled)
1076                 return 0;
1077
1078         ret = ice_fdir_setup(pf);
1079         if (ret)
1080                 return ret;
1081
1082         parser = &ice_fdir_parser;
1083
1084         return ice_register_parser(parser, ad);
1085 }
1086
1087 static void
1088 ice_fdir_uninit(struct ice_adapter *ad)
1089 {
1090         struct ice_flow_parser *parser;
1091         struct ice_pf *pf = &ad->pf;
1092
1093         if (ad->hw.dcf_enabled)
1094                 return;
1095
1096         parser = &ice_fdir_parser;
1097
1098         ice_unregister_parser(parser, ad);
1099
1100         ice_fdir_teardown(pf);
1101 }
1102
1103 static int
1104 ice_fdir_is_tunnel_profile(enum ice_fdir_tunnel_type tunnel_type)
1105 {
1106         if (tunnel_type == ICE_FDIR_TUNNEL_TYPE_VXLAN)
1107                 return 1;
1108         else
1109                 return 0;
1110 }
1111
1112 static int
1113 ice_fdir_add_del_filter(struct ice_pf *pf,
1114                         struct ice_fdir_filter_conf *filter,
1115                         bool add)
1116 {
1117         struct ice_fltr_desc desc;
1118         struct ice_hw *hw = ICE_PF_TO_HW(pf);
1119         unsigned char *pkt = (unsigned char *)pf->fdir.prg_pkt;
1120         bool is_tun;
1121         int ret;
1122
1123         filter->input.dest_vsi = pf->main_vsi->idx;
1124
1125         memset(&desc, 0, sizeof(desc));
1126         filter->input.comp_report = ICE_FXD_FLTR_QW0_COMP_REPORT_SW;
1127         ice_fdir_get_prgm_desc(hw, &filter->input, &desc, add);
1128
1129         is_tun = ice_fdir_is_tunnel_profile(filter->tunnel_type);
1130
1131         memset(pkt, 0, ICE_FDIR_PKT_LEN);
1132         ret = ice_fdir_get_gen_prgm_pkt(hw, &filter->input, pkt, false, is_tun);
1133         if (ret) {
1134                 PMD_DRV_LOG(ERR, "Generate dummy packet failed");
1135                 return -EINVAL;
1136         }
1137
1138         return ice_fdir_programming(pf, &desc);
1139 }
1140
1141 static void
1142 ice_fdir_extract_fltr_key(struct ice_fdir_fltr_pattern *key,
1143                           struct ice_fdir_filter_conf *filter)
1144 {
1145         struct ice_fdir_fltr *input = &filter->input;
1146         memset(key, 0, sizeof(*key));
1147
1148         key->flow_type = input->flow_type;
1149         rte_memcpy(&key->ip, &input->ip, sizeof(key->ip));
1150         rte_memcpy(&key->mask, &input->mask, sizeof(key->mask));
1151         rte_memcpy(&key->ext_data, &input->ext_data, sizeof(key->ext_data));
1152         rte_memcpy(&key->ext_mask, &input->ext_mask, sizeof(key->ext_mask));
1153
1154         rte_memcpy(&key->gtpu_data, &input->gtpu_data, sizeof(key->gtpu_data));
1155         rte_memcpy(&key->gtpu_mask, &input->gtpu_mask, sizeof(key->gtpu_mask));
1156
1157         key->tunnel_type = filter->tunnel_type;
1158 }
1159
1160 /* Check if there exists the flow director filter */
1161 static struct ice_fdir_filter_conf *
1162 ice_fdir_entry_lookup(struct ice_fdir_info *fdir_info,
1163                         const struct ice_fdir_fltr_pattern *key)
1164 {
1165         int ret;
1166
1167         ret = rte_hash_lookup(fdir_info->hash_table, key);
1168         if (ret < 0)
1169                 return NULL;
1170
1171         return fdir_info->hash_map[ret];
1172 }
1173
1174 /* Add a flow director entry into the SW list */
1175 static int
1176 ice_fdir_entry_insert(struct ice_pf *pf,
1177                       struct ice_fdir_filter_conf *entry,
1178                       struct ice_fdir_fltr_pattern *key)
1179 {
1180         struct ice_fdir_info *fdir_info = &pf->fdir;
1181         int ret;
1182
1183         ret = rte_hash_add_key(fdir_info->hash_table, key);
1184         if (ret < 0) {
1185                 PMD_DRV_LOG(ERR,
1186                             "Failed to insert fdir entry to hash table %d!",
1187                             ret);
1188                 return ret;
1189         }
1190         fdir_info->hash_map[ret] = entry;
1191
1192         return 0;
1193 }
1194
1195 /* Delete a flow director entry from the SW list */
1196 static int
1197 ice_fdir_entry_del(struct ice_pf *pf, struct ice_fdir_fltr_pattern *key)
1198 {
1199         struct ice_fdir_info *fdir_info = &pf->fdir;
1200         int ret;
1201
1202         ret = rte_hash_del_key(fdir_info->hash_table, key);
1203         if (ret < 0) {
1204                 PMD_DRV_LOG(ERR,
1205                             "Failed to delete fdir filter to hash table %d!",
1206                             ret);
1207                 return ret;
1208         }
1209         fdir_info->hash_map[ret] = NULL;
1210
1211         return 0;
1212 }
1213
1214 static int
1215 ice_fdir_create_filter(struct ice_adapter *ad,
1216                        struct rte_flow *flow,
1217                        void *meta,
1218                        struct rte_flow_error *error)
1219 {
1220         struct ice_pf *pf = &ad->pf;
1221         struct ice_fdir_filter_conf *filter = meta;
1222         struct ice_fdir_info *fdir_info = &pf->fdir;
1223         struct ice_fdir_filter_conf *entry, *node;
1224         struct ice_fdir_fltr_pattern key;
1225         bool is_tun;
1226         int ret;
1227
1228         ice_fdir_extract_fltr_key(&key, filter);
1229         node = ice_fdir_entry_lookup(fdir_info, &key);
1230         if (node) {
1231                 rte_flow_error_set(error, EEXIST,
1232                                    RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
1233                                    "Rule already exists!");
1234                 return -rte_errno;
1235         }
1236
1237         entry = rte_zmalloc("fdir_entry", sizeof(*entry), 0);
1238         if (!entry) {
1239                 rte_flow_error_set(error, ENOMEM,
1240                                    RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
1241                                    "Failed to allocate memory");
1242                 return -rte_errno;
1243         }
1244
1245         is_tun = ice_fdir_is_tunnel_profile(filter->tunnel_type);
1246
1247         ret = ice_fdir_input_set_conf(pf, filter->input.flow_type,
1248                                       filter->input_set_i, filter->input_set_o,
1249                                       filter->tunnel_type);
1250         if (ret) {
1251                 rte_flow_error_set(error, -ret,
1252                                    RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
1253                                    "Profile configure failed.");
1254                 goto free_entry;
1255         }
1256
1257         /* alloc counter for FDIR */
1258         if (filter->input.cnt_ena) {
1259                 struct rte_flow_action_count *act_count = &filter->act_count;
1260
1261                 filter->counter = ice_fdir_counter_alloc(pf,
1262                                                          act_count->shared,
1263                                                          act_count->id);
1264                 if (!filter->counter) {
1265                         rte_flow_error_set(error, EINVAL,
1266                                         RTE_FLOW_ERROR_TYPE_ACTION, NULL,
1267                                         "Failed to alloc FDIR counter.");
1268                         goto free_entry;
1269                 }
1270                 filter->input.cnt_index = filter->counter->hw_index;
1271         }
1272
1273         ret = ice_fdir_add_del_filter(pf, filter, true);
1274         if (ret) {
1275                 rte_flow_error_set(error, -ret,
1276                                    RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
1277                                    "Add filter rule failed.");
1278                 goto free_counter;
1279         }
1280
1281         if (filter->mark_flag == 1)
1282                 ice_fdir_rx_parsing_enable(ad, 1);
1283
1284         rte_memcpy(entry, filter, sizeof(*entry));
1285         ret = ice_fdir_entry_insert(pf, entry, &key);
1286         if (ret) {
1287                 rte_flow_error_set(error, -ret,
1288                                    RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
1289                                    "Insert entry to table failed.");
1290                 goto free_entry;
1291         }
1292
1293         flow->rule = entry;
1294         ice_fdir_cnt_update(pf, filter->input.flow_type, is_tun, true);
1295
1296         return 0;
1297
1298 free_counter:
1299         if (filter->counter) {
1300                 ice_fdir_counter_free(pf, filter->counter);
1301                 filter->counter = NULL;
1302         }
1303
1304 free_entry:
1305         rte_free(entry);
1306         return -rte_errno;
1307 }
1308
1309 static int
1310 ice_fdir_destroy_filter(struct ice_adapter *ad,
1311                         struct rte_flow *flow,
1312                         struct rte_flow_error *error)
1313 {
1314         struct ice_pf *pf = &ad->pf;
1315         struct ice_fdir_info *fdir_info = &pf->fdir;
1316         struct ice_fdir_filter_conf *filter, *entry;
1317         struct ice_fdir_fltr_pattern key;
1318         bool is_tun;
1319         int ret;
1320
1321         filter = (struct ice_fdir_filter_conf *)flow->rule;
1322
1323         is_tun = ice_fdir_is_tunnel_profile(filter->tunnel_type);
1324
1325         if (filter->counter) {
1326                 ice_fdir_counter_free(pf, filter->counter);
1327                 filter->counter = NULL;
1328         }
1329
1330         ice_fdir_extract_fltr_key(&key, filter);
1331         entry = ice_fdir_entry_lookup(fdir_info, &key);
1332         if (!entry) {
1333                 rte_flow_error_set(error, ENOENT,
1334                                    RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
1335                                    "Can't find entry.");
1336                 return -rte_errno;
1337         }
1338
1339         ret = ice_fdir_add_del_filter(pf, filter, false);
1340         if (ret) {
1341                 rte_flow_error_set(error, -ret,
1342                                    RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
1343                                    "Del filter rule failed.");
1344                 return -rte_errno;
1345         }
1346
1347         ret = ice_fdir_entry_del(pf, &key);
1348         if (ret) {
1349                 rte_flow_error_set(error, -ret,
1350                                    RTE_FLOW_ERROR_TYPE_HANDLE, NULL,
1351                                    "Remove entry from table failed.");
1352                 return -rte_errno;
1353         }
1354
1355         ice_fdir_cnt_update(pf, filter->input.flow_type, is_tun, false);
1356
1357         if (filter->mark_flag == 1)
1358                 ice_fdir_rx_parsing_enable(ad, 0);
1359
1360         flow->rule = NULL;
1361
1362         rte_free(filter);
1363
1364         return 0;
1365 }
1366
1367 static int
1368 ice_fdir_query_count(struct ice_adapter *ad,
1369                       struct rte_flow *flow,
1370                       struct rte_flow_query_count *flow_stats,
1371                       struct rte_flow_error *error)
1372 {
1373         struct ice_pf *pf = &ad->pf;
1374         struct ice_hw *hw = ICE_PF_TO_HW(pf);
1375         struct ice_fdir_filter_conf *filter = flow->rule;
1376         struct ice_fdir_counter *counter = filter->counter;
1377         uint64_t hits_lo, hits_hi;
1378
1379         if (!counter) {
1380                 rte_flow_error_set(error, EINVAL,
1381                                   RTE_FLOW_ERROR_TYPE_ACTION,
1382                                   NULL,
1383                                   "FDIR counters not available");
1384                 return -rte_errno;
1385         }
1386
1387         /*
1388          * Reading the low 32-bits latches the high 32-bits into a shadow
1389          * register. Reading the high 32-bit returns the value in the
1390          * shadow register.
1391          */
1392         hits_lo = ICE_READ_REG(hw, GLSTAT_FD_CNT0L(counter->hw_index));
1393         hits_hi = ICE_READ_REG(hw, GLSTAT_FD_CNT0H(counter->hw_index));
1394
1395         flow_stats->hits_set = 1;
1396         flow_stats->hits = hits_lo | (hits_hi << 32);
1397         flow_stats->bytes_set = 0;
1398         flow_stats->bytes = 0;
1399
1400         if (flow_stats->reset) {
1401                 /* reset statistic counter value */
1402                 ICE_WRITE_REG(hw, GLSTAT_FD_CNT0H(counter->hw_index), 0);
1403                 ICE_WRITE_REG(hw, GLSTAT_FD_CNT0L(counter->hw_index), 0);
1404         }
1405
1406         return 0;
1407 }
1408
1409 static struct ice_flow_engine ice_fdir_engine = {
1410         .init = ice_fdir_init,
1411         .uninit = ice_fdir_uninit,
1412         .create = ice_fdir_create_filter,
1413         .destroy = ice_fdir_destroy_filter,
1414         .query_count = ice_fdir_query_count,
1415         .type = ICE_FLOW_ENGINE_FDIR,
1416 };
1417
1418 static int
1419 ice_fdir_parse_action_qregion(struct ice_pf *pf,
1420                               struct rte_flow_error *error,
1421                               const struct rte_flow_action *act,
1422                               struct ice_fdir_filter_conf *filter)
1423 {
1424         const struct rte_flow_action_rss *rss = act->conf;
1425         uint32_t i;
1426
1427         if (act->type != RTE_FLOW_ACTION_TYPE_RSS) {
1428                 rte_flow_error_set(error, EINVAL,
1429                                    RTE_FLOW_ERROR_TYPE_ACTION, act,
1430                                    "Invalid action.");
1431                 return -rte_errno;
1432         }
1433
1434         if (rss->queue_num <= 1) {
1435                 rte_flow_error_set(error, EINVAL,
1436                                    RTE_FLOW_ERROR_TYPE_ACTION, act,
1437                                    "Queue region size can't be 0 or 1.");
1438                 return -rte_errno;
1439         }
1440
1441         /* check if queue index for queue region is continuous */
1442         for (i = 0; i < rss->queue_num - 1; i++) {
1443                 if (rss->queue[i + 1] != rss->queue[i] + 1) {
1444                         rte_flow_error_set(error, EINVAL,
1445                                            RTE_FLOW_ERROR_TYPE_ACTION, act,
1446                                            "Discontinuous queue region");
1447                         return -rte_errno;
1448                 }
1449         }
1450
1451         if (rss->queue[rss->queue_num - 1] >= pf->dev_data->nb_rx_queues) {
1452                 rte_flow_error_set(error, EINVAL,
1453                                    RTE_FLOW_ERROR_TYPE_ACTION, act,
1454                                    "Invalid queue region indexes.");
1455                 return -rte_errno;
1456         }
1457
1458         if (!(rte_is_power_of_2(rss->queue_num) &&
1459              (rss->queue_num <= ICE_FDIR_MAX_QREGION_SIZE))) {
1460                 rte_flow_error_set(error, EINVAL,
1461                                    RTE_FLOW_ERROR_TYPE_ACTION, act,
1462                                    "The region size should be any of the following values:"
1463                                    "1, 2, 4, 8, 16, 32, 64, 128 as long as the total number "
1464                                    "of queues do not exceed the VSI allocation.");
1465                 return -rte_errno;
1466         }
1467
1468         filter->input.q_index = rss->queue[0];
1469         filter->input.q_region = rte_fls_u32(rss->queue_num) - 1;
1470         filter->input.dest_ctl = ICE_FLTR_PRGM_DESC_DEST_DIRECT_PKT_QGROUP;
1471
1472         return 0;
1473 }
1474
1475 static int
1476 ice_fdir_parse_action(struct ice_adapter *ad,
1477                       const struct rte_flow_action actions[],
1478                       struct rte_flow_error *error,
1479                       struct ice_fdir_filter_conf *filter)
1480 {
1481         struct ice_pf *pf = &ad->pf;
1482         const struct rte_flow_action_queue *act_q;
1483         const struct rte_flow_action_mark *mark_spec = NULL;
1484         const struct rte_flow_action_count *act_count;
1485         uint32_t dest_num = 0;
1486         uint32_t mark_num = 0;
1487         uint32_t counter_num = 0;
1488         int ret;
1489
1490         for (; actions->type != RTE_FLOW_ACTION_TYPE_END; actions++) {
1491                 switch (actions->type) {
1492                 case RTE_FLOW_ACTION_TYPE_VOID:
1493                         break;
1494                 case RTE_FLOW_ACTION_TYPE_QUEUE:
1495                         dest_num++;
1496
1497                         act_q = actions->conf;
1498                         filter->input.q_index = act_q->index;
1499                         if (filter->input.q_index >=
1500                                         pf->dev_data->nb_rx_queues) {
1501                                 rte_flow_error_set(error, EINVAL,
1502                                                    RTE_FLOW_ERROR_TYPE_ACTION,
1503                                                    actions,
1504                                                    "Invalid queue for FDIR.");
1505                                 return -rte_errno;
1506                         }
1507                         filter->input.dest_ctl =
1508                                 ICE_FLTR_PRGM_DESC_DEST_DIRECT_PKT_QINDEX;
1509                         break;
1510                 case RTE_FLOW_ACTION_TYPE_DROP:
1511                         dest_num++;
1512
1513                         filter->input.dest_ctl =
1514                                 ICE_FLTR_PRGM_DESC_DEST_DROP_PKT;
1515                         break;
1516                 case RTE_FLOW_ACTION_TYPE_PASSTHRU:
1517                         dest_num++;
1518
1519                         filter->input.dest_ctl =
1520                                 ICE_FLTR_PRGM_DESC_DEST_DIRECT_PKT_OTHER;
1521                         break;
1522                 case RTE_FLOW_ACTION_TYPE_RSS:
1523                         dest_num++;
1524
1525                         ret = ice_fdir_parse_action_qregion(pf,
1526                                                 error, actions, filter);
1527                         if (ret)
1528                                 return ret;
1529                         break;
1530                 case RTE_FLOW_ACTION_TYPE_MARK:
1531                         mark_num++;
1532                         filter->mark_flag = 1;
1533                         mark_spec = actions->conf;
1534                         filter->input.fltr_id = mark_spec->id;
1535                         filter->input.fdid_prio = ICE_FXD_FLTR_QW1_FDID_PRI_ONE;
1536                         break;
1537                 case RTE_FLOW_ACTION_TYPE_COUNT:
1538                         counter_num++;
1539
1540                         act_count = actions->conf;
1541                         filter->input.cnt_ena = ICE_FXD_FLTR_QW0_STAT_ENA_PKTS;
1542                         rte_memcpy(&filter->act_count, act_count,
1543                                                 sizeof(filter->act_count));
1544
1545                         break;
1546                 default:
1547                         rte_flow_error_set(error, EINVAL,
1548                                    RTE_FLOW_ERROR_TYPE_ACTION, actions,
1549                                    "Invalid action.");
1550                         return -rte_errno;
1551                 }
1552         }
1553
1554         if (dest_num >= 2) {
1555                 rte_flow_error_set(error, EINVAL,
1556                            RTE_FLOW_ERROR_TYPE_ACTION, actions,
1557                            "Unsupported action combination");
1558                 return -rte_errno;
1559         }
1560
1561         if (mark_num >= 2) {
1562                 rte_flow_error_set(error, EINVAL,
1563                            RTE_FLOW_ERROR_TYPE_ACTION, actions,
1564                            "Too many mark actions");
1565                 return -rte_errno;
1566         }
1567
1568         if (counter_num >= 2) {
1569                 rte_flow_error_set(error, EINVAL,
1570                            RTE_FLOW_ERROR_TYPE_ACTION, actions,
1571                            "Too many count actions");
1572                 return -rte_errno;
1573         }
1574
1575         if (dest_num + mark_num + counter_num == 0) {
1576                 rte_flow_error_set(error, EINVAL,
1577                            RTE_FLOW_ERROR_TYPE_ACTION, actions,
1578                            "Empty action");
1579                 return -rte_errno;
1580         }
1581
1582         /* set default action to PASSTHRU mode, in "mark/count only" case. */
1583         if (dest_num == 0)
1584                 filter->input.dest_ctl =
1585                         ICE_FLTR_PRGM_DESC_DEST_DIRECT_PKT_OTHER;
1586
1587         return 0;
1588 }
1589
1590 static int
1591 ice_fdir_parse_pattern(__rte_unused struct ice_adapter *ad,
1592                        const struct rte_flow_item pattern[],
1593                        struct rte_flow_error *error,
1594                        struct ice_fdir_filter_conf *filter)
1595 {
1596         const struct rte_flow_item *item = pattern;
1597         enum rte_flow_item_type item_type;
1598         enum rte_flow_item_type l3 = RTE_FLOW_ITEM_TYPE_END;
1599         enum ice_fdir_tunnel_type tunnel_type = ICE_FDIR_TUNNEL_TYPE_NONE;
1600         const struct rte_flow_item_eth *eth_spec, *eth_mask;
1601         const struct rte_flow_item_ipv4 *ipv4_spec, *ipv4_mask;
1602         const struct rte_flow_item_ipv6 *ipv6_spec, *ipv6_mask;
1603         const struct rte_flow_item_tcp *tcp_spec, *tcp_mask;
1604         const struct rte_flow_item_udp *udp_spec, *udp_mask;
1605         const struct rte_flow_item_sctp *sctp_spec, *sctp_mask;
1606         const struct rte_flow_item_vxlan *vxlan_spec, *vxlan_mask;
1607         const struct rte_flow_item_gtp *gtp_spec, *gtp_mask;
1608         const struct rte_flow_item_gtp_psc *gtp_psc_spec, *gtp_psc_mask;
1609         uint64_t input_set_i = ICE_INSET_NONE; /* only for tunnel inner */
1610         uint64_t input_set_o = ICE_INSET_NONE; /* non-tunnel and tunnel outer */
1611         uint64_t *input_set;
1612         uint8_t flow_type = ICE_FLTR_PTYPE_NONF_NONE;
1613         uint8_t  ipv6_addr_mask[16] = {
1614                 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
1615                 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
1616         };
1617         uint32_t vtc_flow_cpu;
1618         uint16_t ether_type;
1619         enum rte_flow_item_type next_type;
1620         bool is_outer = true;
1621         struct ice_fdir_extra *p_ext_data;
1622         struct ice_fdir_v4 *p_v4 = NULL;
1623         struct ice_fdir_v6 *p_v6 = NULL;
1624
1625         for (item = pattern; item->type != RTE_FLOW_ITEM_TYPE_END; item++) {
1626                 if (item->type == RTE_FLOW_ITEM_TYPE_VXLAN)
1627                         tunnel_type = ICE_FDIR_TUNNEL_TYPE_VXLAN;
1628                 /* To align with shared code behavior, save gtpu outer
1629                  * fields in inner struct.
1630                  */
1631                 if (item->type == RTE_FLOW_ITEM_TYPE_GTPU ||
1632                     item->type == RTE_FLOW_ITEM_TYPE_GTP_PSC) {
1633                         is_outer = false;
1634                 }
1635         }
1636
1637         /* This loop parse flow pattern and distinguish Non-tunnel and tunnel
1638          * flow. input_set_i is used for inner part.
1639          */
1640         for (item = pattern; item->type != RTE_FLOW_ITEM_TYPE_END; item++) {
1641                 if (item->last) {
1642                         rte_flow_error_set(error, EINVAL,
1643                                            RTE_FLOW_ERROR_TYPE_ITEM,
1644                                            item,
1645                                            "Not support range");
1646                         return -rte_errno;
1647                 }
1648                 item_type = item->type;
1649
1650                 input_set = (tunnel_type && !is_outer) ?
1651                             &input_set_i : &input_set_o;
1652
1653                 switch (item_type) {
1654                 case RTE_FLOW_ITEM_TYPE_ETH:
1655                         flow_type = ICE_FLTR_PTYPE_NON_IP_L2;
1656                         eth_spec = item->spec;
1657                         eth_mask = item->mask;
1658
1659                         if (!(eth_spec && eth_mask))
1660                                 break;
1661
1662                         if (!rte_is_zero_ether_addr(&eth_mask->dst))
1663                                 *input_set |= ICE_INSET_DMAC;
1664                         if (!rte_is_zero_ether_addr(&eth_mask->src))
1665                                 *input_set |= ICE_INSET_SMAC;
1666
1667                         next_type = (item + 1)->type;
1668                         /* Ignore this field except for ICE_FLTR_PTYPE_NON_IP_L2 */
1669                         if (eth_mask->type == RTE_BE16(0xffff) &&
1670                             next_type == RTE_FLOW_ITEM_TYPE_END) {
1671                                 *input_set |= ICE_INSET_ETHERTYPE;
1672                                 ether_type = rte_be_to_cpu_16(eth_spec->type);
1673
1674                                 if (ether_type == RTE_ETHER_TYPE_IPV4 ||
1675                                     ether_type == RTE_ETHER_TYPE_IPV6) {
1676                                         rte_flow_error_set(error, EINVAL,
1677                                                            RTE_FLOW_ERROR_TYPE_ITEM,
1678                                                            item,
1679                                                            "Unsupported ether_type.");
1680                                         return -rte_errno;
1681                                 }
1682                         }
1683
1684                         p_ext_data = (tunnel_type && is_outer) ?
1685                                      &filter->input.ext_data_outer :
1686                                      &filter->input.ext_data;
1687                         rte_memcpy(&p_ext_data->src_mac,
1688                                    &eth_spec->src, RTE_ETHER_ADDR_LEN);
1689                         rte_memcpy(&p_ext_data->dst_mac,
1690                                    &eth_spec->dst, RTE_ETHER_ADDR_LEN);
1691                         rte_memcpy(&p_ext_data->ether_type,
1692                                    &eth_spec->type, sizeof(eth_spec->type));
1693                         break;
1694                 case RTE_FLOW_ITEM_TYPE_IPV4:
1695                         flow_type = ICE_FLTR_PTYPE_NONF_IPV4_OTHER;
1696                         l3 = RTE_FLOW_ITEM_TYPE_IPV4;
1697                         ipv4_spec = item->spec;
1698                         ipv4_mask = item->mask;
1699                         p_v4 = (tunnel_type && is_outer) ?
1700                                &filter->input.ip_outer.v4 :
1701                                &filter->input.ip.v4;
1702
1703                         if (!(ipv4_spec && ipv4_mask))
1704                                 break;
1705
1706                         /* Check IPv4 mask and update input set */
1707                         if (ipv4_mask->hdr.version_ihl ||
1708                             ipv4_mask->hdr.total_length ||
1709                             ipv4_mask->hdr.packet_id ||
1710                             ipv4_mask->hdr.fragment_offset ||
1711                             ipv4_mask->hdr.hdr_checksum) {
1712                                 rte_flow_error_set(error, EINVAL,
1713                                                    RTE_FLOW_ERROR_TYPE_ITEM,
1714                                                    item,
1715                                                    "Invalid IPv4 mask.");
1716                                 return -rte_errno;
1717                         }
1718
1719                         if (ipv4_mask->hdr.dst_addr == UINT32_MAX)
1720                                 *input_set |= ICE_INSET_IPV4_DST;
1721                         if (ipv4_mask->hdr.src_addr == UINT32_MAX)
1722                                 *input_set |= ICE_INSET_IPV4_SRC;
1723                         if (ipv4_mask->hdr.time_to_live == UINT8_MAX)
1724                                 *input_set |= ICE_INSET_IPV4_TTL;
1725                         if (ipv4_mask->hdr.next_proto_id == UINT8_MAX)
1726                                 *input_set |= ICE_INSET_IPV4_PROTO;
1727                         if (ipv4_mask->hdr.type_of_service == UINT8_MAX)
1728                                 *input_set |= ICE_INSET_IPV4_TOS;
1729
1730                         p_v4->dst_ip = ipv4_spec->hdr.dst_addr;
1731                         p_v4->src_ip = ipv4_spec->hdr.src_addr;
1732                         p_v4->ttl = ipv4_spec->hdr.time_to_live;
1733                         p_v4->proto = ipv4_spec->hdr.next_proto_id;
1734                         p_v4->tos = ipv4_spec->hdr.type_of_service;
1735                         break;
1736                 case RTE_FLOW_ITEM_TYPE_IPV6:
1737                         flow_type = ICE_FLTR_PTYPE_NONF_IPV6_OTHER;
1738                         l3 = RTE_FLOW_ITEM_TYPE_IPV6;
1739                         ipv6_spec = item->spec;
1740                         ipv6_mask = item->mask;
1741                         p_v6 = (tunnel_type && is_outer) ?
1742                                &filter->input.ip_outer.v6 :
1743                                &filter->input.ip.v6;
1744
1745                         if (!(ipv6_spec && ipv6_mask))
1746                                 break;
1747
1748                         /* Check IPv6 mask and update input set */
1749                         if (ipv6_mask->hdr.payload_len) {
1750                                 rte_flow_error_set(error, EINVAL,
1751                                                    RTE_FLOW_ERROR_TYPE_ITEM,
1752                                                    item,
1753                                                    "Invalid IPv6 mask");
1754                                 return -rte_errno;
1755                         }
1756
1757                         if (!memcmp(ipv6_mask->hdr.src_addr, ipv6_addr_mask,
1758                                     RTE_DIM(ipv6_mask->hdr.src_addr)))
1759                                 *input_set |= ICE_INSET_IPV6_SRC;
1760                         if (!memcmp(ipv6_mask->hdr.dst_addr, ipv6_addr_mask,
1761                                     RTE_DIM(ipv6_mask->hdr.dst_addr)))
1762                                 *input_set |= ICE_INSET_IPV6_DST;
1763
1764                         if ((ipv6_mask->hdr.vtc_flow &
1765                              rte_cpu_to_be_32(ICE_IPV6_TC_MASK))
1766                             == rte_cpu_to_be_32(ICE_IPV6_TC_MASK))
1767                                 *input_set |= ICE_INSET_IPV6_TC;
1768                         if (ipv6_mask->hdr.proto == UINT8_MAX)
1769                                 *input_set |= ICE_INSET_IPV6_NEXT_HDR;
1770                         if (ipv6_mask->hdr.hop_limits == UINT8_MAX)
1771                                 *input_set |= ICE_INSET_IPV6_HOP_LIMIT;
1772
1773                         rte_memcpy(&p_v6->dst_ip, ipv6_spec->hdr.dst_addr, 16);
1774                         rte_memcpy(&p_v6->src_ip, ipv6_spec->hdr.src_addr, 16);
1775                         vtc_flow_cpu = rte_be_to_cpu_32(ipv6_spec->hdr.vtc_flow);
1776                         p_v6->tc = (uint8_t)(vtc_flow_cpu >> ICE_FDIR_IPV6_TC_OFFSET);
1777                         p_v6->proto = ipv6_spec->hdr.proto;
1778                         p_v6->hlim = ipv6_spec->hdr.hop_limits;
1779                         break;
1780                 case RTE_FLOW_ITEM_TYPE_TCP:
1781                         if (l3 == RTE_FLOW_ITEM_TYPE_IPV4)
1782                                 flow_type = ICE_FLTR_PTYPE_NONF_IPV4_TCP;
1783                         if (l3 == RTE_FLOW_ITEM_TYPE_IPV6)
1784                                 flow_type = ICE_FLTR_PTYPE_NONF_IPV6_TCP;
1785
1786                         tcp_spec = item->spec;
1787                         tcp_mask = item->mask;
1788
1789                         if (!(tcp_spec && tcp_mask))
1790                                 break;
1791
1792                         /* Check TCP mask and update input set */
1793                         if (tcp_mask->hdr.sent_seq ||
1794                             tcp_mask->hdr.recv_ack ||
1795                             tcp_mask->hdr.data_off ||
1796                             tcp_mask->hdr.tcp_flags ||
1797                             tcp_mask->hdr.rx_win ||
1798                             tcp_mask->hdr.cksum ||
1799                             tcp_mask->hdr.tcp_urp) {
1800                                 rte_flow_error_set(error, EINVAL,
1801                                                    RTE_FLOW_ERROR_TYPE_ITEM,
1802                                                    item,
1803                                                    "Invalid TCP mask");
1804                                 return -rte_errno;
1805                         }
1806
1807                         if (tcp_mask->hdr.src_port == UINT16_MAX)
1808                                 *input_set |= ICE_INSET_TCP_SRC_PORT;
1809                         if (tcp_mask->hdr.dst_port == UINT16_MAX)
1810                                 *input_set |= ICE_INSET_TCP_DST_PORT;
1811
1812                         /* Get filter info */
1813                         if (l3 == RTE_FLOW_ITEM_TYPE_IPV4) {
1814                                 assert(p_v4);
1815                                 p_v4->dst_port = tcp_spec->hdr.dst_port;
1816                                 p_v4->src_port = tcp_spec->hdr.src_port;
1817                         } else if (l3 == RTE_FLOW_ITEM_TYPE_IPV6) {
1818                                 assert(p_v6);
1819                                 p_v6->dst_port = tcp_spec->hdr.dst_port;
1820                                 p_v6->src_port = tcp_spec->hdr.src_port;
1821                         }
1822                         break;
1823                 case RTE_FLOW_ITEM_TYPE_UDP:
1824                         if (l3 == RTE_FLOW_ITEM_TYPE_IPV4)
1825                                 flow_type = ICE_FLTR_PTYPE_NONF_IPV4_UDP;
1826                         if (l3 == RTE_FLOW_ITEM_TYPE_IPV6)
1827                                 flow_type = ICE_FLTR_PTYPE_NONF_IPV6_UDP;
1828
1829                         udp_spec = item->spec;
1830                         udp_mask = item->mask;
1831
1832                         if (!(udp_spec && udp_mask))
1833                                 break;
1834
1835                         /* Check UDP mask and update input set*/
1836                         if (udp_mask->hdr.dgram_len ||
1837                             udp_mask->hdr.dgram_cksum) {
1838                                 rte_flow_error_set(error, EINVAL,
1839                                                    RTE_FLOW_ERROR_TYPE_ITEM,
1840                                                    item,
1841                                                    "Invalid UDP mask");
1842                                 return -rte_errno;
1843                         }
1844
1845                         if (udp_mask->hdr.src_port == UINT16_MAX)
1846                                 *input_set |= ICE_INSET_UDP_SRC_PORT;
1847                         if (udp_mask->hdr.dst_port == UINT16_MAX)
1848                                 *input_set |= ICE_INSET_UDP_DST_PORT;
1849
1850                         /* Get filter info */
1851                         if (l3 == RTE_FLOW_ITEM_TYPE_IPV4) {
1852                                 assert(p_v4);
1853                                 p_v4->dst_port = udp_spec->hdr.dst_port;
1854                                 p_v4->src_port = udp_spec->hdr.src_port;
1855                         } else if (l3 == RTE_FLOW_ITEM_TYPE_IPV6) {
1856                                 assert(p_v6);
1857                                 p_v6->src_port = udp_spec->hdr.src_port;
1858                                 p_v6->dst_port = udp_spec->hdr.dst_port;
1859                         }
1860                         break;
1861                 case RTE_FLOW_ITEM_TYPE_SCTP:
1862                         if (l3 == RTE_FLOW_ITEM_TYPE_IPV4)
1863                                 flow_type = ICE_FLTR_PTYPE_NONF_IPV4_SCTP;
1864                         if (l3 == RTE_FLOW_ITEM_TYPE_IPV6)
1865                                 flow_type = ICE_FLTR_PTYPE_NONF_IPV6_SCTP;
1866
1867                         sctp_spec = item->spec;
1868                         sctp_mask = item->mask;
1869
1870                         if (!(sctp_spec && sctp_mask))
1871                                 break;
1872
1873                         /* Check SCTP mask and update input set */
1874                         if (sctp_mask->hdr.cksum) {
1875                                 rte_flow_error_set(error, EINVAL,
1876                                                    RTE_FLOW_ERROR_TYPE_ITEM,
1877                                                    item,
1878                                                    "Invalid UDP mask");
1879                                 return -rte_errno;
1880                         }
1881
1882                         if (sctp_mask->hdr.src_port == UINT16_MAX)
1883                                 *input_set |= ICE_INSET_SCTP_SRC_PORT;
1884                         if (sctp_mask->hdr.dst_port == UINT16_MAX)
1885                                 *input_set |= ICE_INSET_SCTP_DST_PORT;
1886
1887                         /* Get filter info */
1888                         if (l3 == RTE_FLOW_ITEM_TYPE_IPV4) {
1889                                 assert(p_v4);
1890                                 p_v4->dst_port = sctp_spec->hdr.dst_port;
1891                                 p_v4->src_port = sctp_spec->hdr.src_port;
1892                         } else if (l3 == RTE_FLOW_ITEM_TYPE_IPV6) {
1893                                 assert(p_v6);
1894                                 p_v6->dst_port = sctp_spec->hdr.dst_port;
1895                                 p_v6->src_port = sctp_spec->hdr.src_port;
1896                         }
1897                         break;
1898                 case RTE_FLOW_ITEM_TYPE_VOID:
1899                         break;
1900                 case RTE_FLOW_ITEM_TYPE_VXLAN:
1901                         l3 = RTE_FLOW_ITEM_TYPE_END;
1902                         vxlan_spec = item->spec;
1903                         vxlan_mask = item->mask;
1904                         is_outer = false;
1905
1906                         if (vxlan_spec || vxlan_mask) {
1907                                 rte_flow_error_set(error, EINVAL,
1908                                                    RTE_FLOW_ERROR_TYPE_ITEM,
1909                                                    item,
1910                                                    "Invalid vxlan field");
1911                                 return -rte_errno;
1912                         }
1913
1914                         break;
1915                 case RTE_FLOW_ITEM_TYPE_GTPU:
1916                         l3 = RTE_FLOW_ITEM_TYPE_END;
1917                         tunnel_type = ICE_FDIR_TUNNEL_TYPE_GTPU;
1918                         gtp_spec = item->spec;
1919                         gtp_mask = item->mask;
1920
1921                         if (!(gtp_spec && gtp_mask))
1922                                 break;
1923
1924                         if (gtp_mask->v_pt_rsv_flags ||
1925                             gtp_mask->msg_type ||
1926                             gtp_mask->msg_len) {
1927                                 rte_flow_error_set(error, EINVAL,
1928                                                    RTE_FLOW_ERROR_TYPE_ITEM,
1929                                                    item,
1930                                                    "Invalid GTP mask");
1931                                 return -rte_errno;
1932                         }
1933
1934                         if (gtp_mask->teid == UINT32_MAX)
1935                                 input_set_o |= ICE_INSET_GTPU_TEID;
1936
1937                         filter->input.gtpu_data.teid = gtp_spec->teid;
1938                         break;
1939                 case RTE_FLOW_ITEM_TYPE_GTP_PSC:
1940                         tunnel_type = ICE_FDIR_TUNNEL_TYPE_GTPU_EH;
1941                         gtp_psc_spec = item->spec;
1942                         gtp_psc_mask = item->mask;
1943
1944                         if (!(gtp_psc_spec && gtp_psc_mask))
1945                                 break;
1946
1947                         if (gtp_psc_mask->qfi == UINT8_MAX)
1948                                 input_set_o |= ICE_INSET_GTPU_QFI;
1949
1950                         filter->input.gtpu_data.qfi =
1951                                 gtp_psc_spec->qfi;
1952                         break;
1953                 default:
1954                         rte_flow_error_set(error, EINVAL,
1955                                            RTE_FLOW_ERROR_TYPE_ITEM,
1956                                            item,
1957                                            "Invalid pattern item.");
1958                         return -rte_errno;
1959                 }
1960         }
1961
1962         if (tunnel_type == ICE_FDIR_TUNNEL_TYPE_GTPU &&
1963                 flow_type == ICE_FLTR_PTYPE_NONF_IPV4_UDP)
1964                 flow_type = ICE_FLTR_PTYPE_NONF_IPV4_GTPU_IPV4_OTHER;
1965         else if (tunnel_type == ICE_FDIR_TUNNEL_TYPE_GTPU_EH &&
1966                 flow_type == ICE_FLTR_PTYPE_NONF_IPV4_UDP)
1967                 flow_type = ICE_FLTR_PTYPE_NONF_IPV4_GTPU_EH_IPV4_OTHER;
1968         else if (tunnel_type == ICE_FDIR_TUNNEL_TYPE_GTPU &&
1969                 flow_type == ICE_FLTR_PTYPE_NONF_IPV6_UDP)
1970                 flow_type = ICE_FLTR_PTYPE_NONF_IPV6_GTPU_IPV6_OTHER;
1971         else if (tunnel_type == ICE_FDIR_TUNNEL_TYPE_GTPU_EH &&
1972                 flow_type == ICE_FLTR_PTYPE_NONF_IPV6_UDP)
1973                 flow_type = ICE_FLTR_PTYPE_NONF_IPV6_GTPU_EH_IPV6_OTHER;
1974
1975         filter->tunnel_type = tunnel_type;
1976         filter->input.flow_type = flow_type;
1977         filter->input_set_o = input_set_o;
1978         filter->input_set_i = input_set_i;
1979
1980         return 0;
1981 }
1982
1983 static int
1984 ice_fdir_parse(struct ice_adapter *ad,
1985                struct ice_pattern_match_item *array,
1986                uint32_t array_len,
1987                const struct rte_flow_item pattern[],
1988                const struct rte_flow_action actions[],
1989                void **meta,
1990                struct rte_flow_error *error)
1991 {
1992         struct ice_pf *pf = &ad->pf;
1993         struct ice_fdir_filter_conf *filter = &pf->fdir.conf;
1994         struct ice_pattern_match_item *item = NULL;
1995         uint64_t input_set;
1996         int ret;
1997
1998         memset(filter, 0, sizeof(*filter));
1999         item = ice_search_pattern_match_item(ad, pattern, array, array_len,
2000                                              error);
2001         if (!item)
2002                 return -rte_errno;
2003
2004         ret = ice_fdir_parse_pattern(ad, pattern, error, filter);
2005         if (ret)
2006                 goto error;
2007         input_set = filter->input_set_o | filter->input_set_i;
2008         if (!input_set || input_set & ~item->input_set_mask) {
2009                 rte_flow_error_set(error, EINVAL,
2010                                    RTE_FLOW_ERROR_TYPE_ITEM_SPEC,
2011                                    pattern,
2012                                    "Invalid input set");
2013                 ret = -rte_errno;
2014                 goto error;
2015         }
2016
2017         ret = ice_fdir_parse_action(ad, actions, error, filter);
2018         if (ret)
2019                 goto error;
2020
2021         if (meta)
2022                 *meta = filter;
2023 error:
2024         rte_free(item);
2025         return ret;
2026 }
2027
2028 static struct ice_flow_parser ice_fdir_parser = {
2029         .engine = &ice_fdir_engine,
2030         .array = ice_fdir_pattern_list,
2031         .array_len = RTE_DIM(ice_fdir_pattern_list),
2032         .parse_pattern_action = ice_fdir_parse,
2033         .stage = ICE_FLOW_STAGE_DISTRIBUTOR,
2034 };
2035
2036 RTE_INIT(ice_fdir_engine_register)
2037 {
2038         ice_register_flow_engine(&ice_fdir_engine);
2039 }