ixgbe: fix flow director flexbytes offset
[dpdk.git] / drivers / net / ixgbe / ixgbe_fdir.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2015 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include <stdio.h>
35 #include <stdint.h>
36 #include <stdarg.h>
37 #include <errno.h>
38 #include <sys/queue.h>
39
40 #include <rte_interrupts.h>
41 #include <rte_log.h>
42 #include <rte_debug.h>
43 #include <rte_pci.h>
44 #include <rte_ether.h>
45 #include <rte_ethdev.h>
46
47 #include "ixgbe_logs.h"
48 #include "base/ixgbe_api.h"
49 #include "base/ixgbe_common.h"
50 #include "ixgbe_ethdev.h"
51
52 /* To get PBALLOC (Packet Buffer Allocation) bits from FDIRCTRL value */
53 #define FDIRCTRL_PBALLOC_MASK           0x03
54
55 /* For calculating memory required for FDIR filters */
56 #define PBALLOC_SIZE_SHIFT              15
57
58 /* Number of bits used to mask bucket hash for different pballoc sizes */
59 #define PERFECT_BUCKET_64KB_HASH_MASK   0x07FF  /* 11 bits */
60 #define PERFECT_BUCKET_128KB_HASH_MASK  0x0FFF  /* 12 bits */
61 #define PERFECT_BUCKET_256KB_HASH_MASK  0x1FFF  /* 13 bits */
62 #define SIG_BUCKET_64KB_HASH_MASK       0x1FFF  /* 13 bits */
63 #define SIG_BUCKET_128KB_HASH_MASK      0x3FFF  /* 14 bits */
64 #define SIG_BUCKET_256KB_HASH_MASK      0x7FFF  /* 15 bits */
65 #define IXGBE_DEFAULT_FLEXBYTES_OFFSET  12 /* default flexbytes offset in bytes */
66 #define IXGBE_FDIR_MAX_FLEX_LEN         2 /* len in bytes of flexbytes */
67 #define IXGBE_MAX_FLX_SOURCE_OFF        62
68 #define IXGBE_FDIRCTRL_FLEX_MASK        (0x1F << IXGBE_FDIRCTRL_FLEX_SHIFT)
69 #define IXGBE_FDIRCMD_CMD_INTERVAL_US   10
70
71 #define IXGBE_FDIR_FLOW_TYPES ( \
72         (1 << RTE_ETH_FLOW_NONFRAG_IPV4_UDP) | \
73         (1 << RTE_ETH_FLOW_NONFRAG_IPV4_TCP) | \
74         (1 << RTE_ETH_FLOW_NONFRAG_IPV4_SCTP) | \
75         (1 << RTE_ETH_FLOW_NONFRAG_IPV4_OTHER) | \
76         (1 << RTE_ETH_FLOW_NONFRAG_IPV6_UDP) | \
77         (1 << RTE_ETH_FLOW_NONFRAG_IPV6_TCP) | \
78         (1 << RTE_ETH_FLOW_NONFRAG_IPV6_SCTP) | \
79         (1 << RTE_ETH_FLOW_NONFRAG_IPV6_OTHER))
80
81 #define IPV6_ADDR_TO_MASK(ipaddr, ipv6m) do { \
82         uint8_t ipv6_addr[16]; \
83         uint8_t i; \
84         rte_memcpy(ipv6_addr, (ipaddr), sizeof(ipv6_addr));\
85         (ipv6m) = 0; \
86         for (i = 0; i < sizeof(ipv6_addr); i++) { \
87                 if (ipv6_addr[i] == UINT8_MAX) \
88                         (ipv6m) |= 1 << i; \
89                 else if (ipv6_addr[i] != 0) { \
90                         PMD_DRV_LOG(ERR, " invalid IPv6 address mask."); \
91                         return -EINVAL; \
92                 } \
93         } \
94 } while (0)
95
96 #define IPV6_MASK_TO_ADDR(ipv6m, ipaddr) do { \
97         uint8_t ipv6_addr[16]; \
98         uint8_t i; \
99         for (i = 0; i < sizeof(ipv6_addr); i++) { \
100                 if ((ipv6m) & (1 << i)) \
101                         ipv6_addr[i] = UINT8_MAX; \
102                 else \
103                         ipv6_addr[i] = 0; \
104         } \
105         rte_memcpy((ipaddr), ipv6_addr, sizeof(ipv6_addr));\
106 } while (0)
107
108 static int fdir_erase_filter_82599(struct ixgbe_hw *hw, uint32_t fdirhash);
109 static int fdir_set_input_mask_82599(struct rte_eth_dev *dev,
110                 const struct rte_eth_fdir_masks *input_mask);
111 static int ixgbe_set_fdir_flex_conf(struct rte_eth_dev *dev,
112                 const struct rte_eth_fdir_flex_conf *conf, uint32_t *fdirctrl);
113 static int fdir_enable_82599(struct ixgbe_hw *hw, uint32_t fdirctrl);
114 static int ixgbe_fdir_filter_to_atr_input(
115                 const struct rte_eth_fdir_filter *fdir_filter,
116                 union ixgbe_atr_input *input);
117 static uint32_t ixgbe_atr_compute_hash_82599(union ixgbe_atr_input *atr_input,
118                                  uint32_t key);
119 static uint32_t atr_compute_sig_hash_82599(union ixgbe_atr_input *input,
120                 enum rte_fdir_pballoc_type pballoc);
121 static uint32_t atr_compute_perfect_hash_82599(union ixgbe_atr_input *input,
122                 enum rte_fdir_pballoc_type pballoc);
123 static int fdir_write_perfect_filter_82599(struct ixgbe_hw *hw,
124                         union ixgbe_atr_input *input, uint8_t queue,
125                         uint32_t fdircmd, uint32_t fdirhash);
126 static int fdir_add_signature_filter_82599(struct ixgbe_hw *hw,
127                 union ixgbe_atr_input *input, u8 queue, uint32_t fdircmd,
128                 uint32_t fdirhash);
129 static int ixgbe_add_del_fdir_filter(struct rte_eth_dev *dev,
130                               const struct rte_eth_fdir_filter *fdir_filter,
131                               bool del,
132                               bool update);
133 static int ixgbe_fdir_flush(struct rte_eth_dev *dev);
134 static void ixgbe_fdir_info_get(struct rte_eth_dev *dev,
135                         struct rte_eth_fdir_info *fdir_info);
136 static void ixgbe_fdir_stats_get(struct rte_eth_dev *dev,
137                         struct rte_eth_fdir_stats *fdir_stats);
138
139 /**
140  * This function is based on ixgbe_fdir_enable_82599() in base/ixgbe_82599.c.
141  * It adds extra configuration of fdirctrl that is common for all filter types.
142  *
143  *  Initialize Flow Director control registers
144  *  @hw: pointer to hardware structure
145  *  @fdirctrl: value to write to flow director control register
146  **/
147 static int
148 fdir_enable_82599(struct ixgbe_hw *hw, uint32_t fdirctrl)
149 {
150         int i;
151
152         PMD_INIT_FUNC_TRACE();
153
154         /* Prime the keys for hashing */
155         IXGBE_WRITE_REG(hw, IXGBE_FDIRHKEY, IXGBE_ATR_BUCKET_HASH_KEY);
156         IXGBE_WRITE_REG(hw, IXGBE_FDIRSKEY, IXGBE_ATR_SIGNATURE_HASH_KEY);
157
158         /*
159          * Continue setup of fdirctrl register bits:
160          *  Set the maximum length per hash bucket to 0xA filters
161          *  Send interrupt when 64 filters are left
162          */
163         fdirctrl |= (0xA << IXGBE_FDIRCTRL_MAX_LENGTH_SHIFT) |
164                     (4 << IXGBE_FDIRCTRL_FULL_THRESH_SHIFT);
165
166         /*
167          * Poll init-done after we write the register.  Estimated times:
168          *      10G: PBALLOC = 11b, timing is 60us
169          *       1G: PBALLOC = 11b, timing is 600us
170          *     100M: PBALLOC = 11b, timing is 6ms
171          *
172          *     Multiple these timings by 4 if under full Rx load
173          *
174          * So we'll poll for IXGBE_FDIR_INIT_DONE_POLL times, sleeping for
175          * 1 msec per poll time.  If we're at line rate and drop to 100M, then
176          * this might not finish in our poll time, but we can live with that
177          * for now.
178          */
179         IXGBE_WRITE_REG(hw, IXGBE_FDIRCTRL, fdirctrl);
180         IXGBE_WRITE_FLUSH(hw);
181         for (i = 0; i < IXGBE_FDIR_INIT_DONE_POLL; i++) {
182                 if (IXGBE_READ_REG(hw, IXGBE_FDIRCTRL) &
183                                    IXGBE_FDIRCTRL_INIT_DONE)
184                         break;
185                 msec_delay(1);
186         }
187
188         if (i >= IXGBE_FDIR_INIT_DONE_POLL) {
189                 PMD_INIT_LOG(ERR, "Flow Director poll time exceeded "
190                         "during enabling!");
191                 return -ETIMEDOUT;
192         }
193         return 0;
194 }
195
196 /*
197  * Set appropriate bits in fdirctrl for: variable reporting levels, moving
198  * flexbytes matching field, and drop queue (only for perfect matching mode).
199  */
200 static inline int
201 configure_fdir_flags(const struct rte_fdir_conf *conf, uint32_t *fdirctrl)
202 {
203         *fdirctrl = 0;
204
205         switch (conf->pballoc) {
206         case RTE_FDIR_PBALLOC_64K:
207                 /* 8k - 1 signature filters */
208                 *fdirctrl |= IXGBE_FDIRCTRL_PBALLOC_64K;
209                 break;
210         case RTE_FDIR_PBALLOC_128K:
211                 /* 16k - 1 signature filters */
212                 *fdirctrl |= IXGBE_FDIRCTRL_PBALLOC_128K;
213                 break;
214         case RTE_FDIR_PBALLOC_256K:
215                 /* 32k - 1 signature filters */
216                 *fdirctrl |= IXGBE_FDIRCTRL_PBALLOC_256K;
217                 break;
218         default:
219                 /* bad value */
220                 PMD_INIT_LOG(ERR, "Invalid fdir_conf->pballoc value");
221                 return -EINVAL;
222         };
223
224         /* status flags: write hash & swindex in the rx descriptor */
225         switch (conf->status) {
226         case RTE_FDIR_NO_REPORT_STATUS:
227                 /* do nothing, default mode */
228                 break;
229         case RTE_FDIR_REPORT_STATUS:
230                 /* report status when the packet matches a fdir rule */
231                 *fdirctrl |= IXGBE_FDIRCTRL_REPORT_STATUS;
232                 break;
233         case RTE_FDIR_REPORT_STATUS_ALWAYS:
234                 /* always report status */
235                 *fdirctrl |= IXGBE_FDIRCTRL_REPORT_STATUS_ALWAYS;
236                 break;
237         default:
238                 /* bad value */
239                 PMD_INIT_LOG(ERR, "Invalid fdir_conf->status value");
240                 return -EINVAL;
241         };
242
243         *fdirctrl |= (IXGBE_DEFAULT_FLEXBYTES_OFFSET / sizeof(uint16_t)) <<
244                      IXGBE_FDIRCTRL_FLEX_SHIFT;
245
246         if (conf->mode == RTE_FDIR_MODE_PERFECT) {
247                 *fdirctrl |= IXGBE_FDIRCTRL_PERFECT_MATCH;
248                 *fdirctrl |= (conf->drop_queue << IXGBE_FDIRCTRL_DROP_Q_SHIFT);
249         }
250
251         return 0;
252 }
253
254 /**
255  * Reverse the bits in FDIR registers that store 2 x 16 bit masks.
256  *
257  *  @hi_dword: Bits 31:16 mask to be bit swapped.
258  *  @lo_dword: Bits 15:0  mask to be bit swapped.
259  *
260  *  Flow director uses several registers to store 2 x 16 bit masks with the
261  *  bits reversed such as FDIRTCPM, FDIRUDPM. The LS bit of the
262  *  mask affects the MS bit/byte of the target. This function reverses the
263  *  bits in these masks.
264  *  **/
265 static inline uint32_t
266 reverse_fdir_bitmasks(uint16_t hi_dword, uint16_t lo_dword)
267 {
268         uint32_t mask = hi_dword << 16;
269         mask |= lo_dword;
270         mask = ((mask & 0x55555555) << 1) | ((mask & 0xAAAAAAAA) >> 1);
271         mask = ((mask & 0x33333333) << 2) | ((mask & 0xCCCCCCCC) >> 2);
272         mask = ((mask & 0x0F0F0F0F) << 4) | ((mask & 0xF0F0F0F0) >> 4);
273         return ((mask & 0x00FF00FF) << 8) | ((mask & 0xFF00FF00) >> 8);
274 }
275
276 /*
277  * This is based on ixgbe_fdir_set_input_mask_82599() in base/ixgbe_82599.c,
278  * but makes use of the rte_fdir_masks structure to see which bits to set.
279  */
280 static int
281 fdir_set_input_mask_82599(struct rte_eth_dev *dev,
282                 const struct rte_eth_fdir_masks *input_mask)
283 {
284         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
285         struct ixgbe_hw_fdir_info *info =
286                         IXGBE_DEV_PRIVATE_TO_FDIR_INFO(dev->data->dev_private);
287         /*
288          * mask VM pool and DIPv6 since there are currently not supported
289          * mask FLEX byte, it will be set in flex_conf
290          */
291         uint32_t fdirm = IXGBE_FDIRM_POOL | IXGBE_FDIRM_DIPv6 | IXGBE_FDIRM_FLEX;
292         uint32_t fdirtcpm;  /* TCP source and destination port masks. */
293         uint32_t fdiripv6m; /* IPv6 source and destination masks. */
294         uint16_t dst_ipv6m = 0;
295         uint16_t src_ipv6m = 0;
296
297         PMD_INIT_FUNC_TRACE();
298
299         /*
300          * Program the relevant mask registers.  If src/dst_port or src/dst_addr
301          * are zero, then assume a full mask for that field. Also assume that
302          * a VLAN of 0 is unspecified, so mask that out as well.  L4type
303          * cannot be masked out in this implementation.
304          */
305         if (input_mask->dst_port_mask == 0 && input_mask->src_port_mask == 0)
306                 /* use the L4 protocol mask for raw IPv4/IPv6 traffic */
307                 fdirm |= IXGBE_FDIRM_L4P;
308
309         if (input_mask->vlan_tci_mask == 0x0FFF)
310                 /* mask VLAN Priority */
311                 fdirm |= IXGBE_FDIRM_VLANP;
312         else if (input_mask->vlan_tci_mask == 0xE000)
313                 /* mask VLAN ID */
314                 fdirm |= IXGBE_FDIRM_VLANID;
315         else if (input_mask->vlan_tci_mask == 0)
316                 /* mask VLAN ID and Priority */
317                 fdirm |= IXGBE_FDIRM_VLANID | IXGBE_FDIRM_VLANP;
318         else if (input_mask->vlan_tci_mask != 0xEFFF) {
319                 PMD_INIT_LOG(ERR, "invalid vlan_tci_mask");
320                 return -EINVAL;
321         }
322         info->mask.vlan_tci_mask = input_mask->vlan_tci_mask;
323
324         IXGBE_WRITE_REG(hw, IXGBE_FDIRM, fdirm);
325
326         /* store the TCP/UDP port masks, bit reversed from port layout */
327         fdirtcpm = reverse_fdir_bitmasks(input_mask->dst_port_mask,
328                                          input_mask->src_port_mask);
329
330         /* write all the same so that UDP, TCP and SCTP use the same mask */
331         IXGBE_WRITE_REG(hw, IXGBE_FDIRTCPM, ~fdirtcpm);
332         IXGBE_WRITE_REG(hw, IXGBE_FDIRUDPM, ~fdirtcpm);
333         IXGBE_WRITE_REG(hw, IXGBE_FDIRSCTPM, ~fdirtcpm);
334         info->mask.src_port_mask = input_mask->src_port_mask;
335         info->mask.dst_port_mask = input_mask->dst_port_mask;
336
337         /* Store source and destination IPv4 masks (big-endian) */
338         IXGBE_WRITE_REG(hw, IXGBE_FDIRSIP4M, ~(input_mask->ipv4_mask.src_ip));
339         IXGBE_WRITE_REG(hw, IXGBE_FDIRDIP4M, ~(input_mask->ipv4_mask.dst_ip));
340         info->mask.src_ipv4_mask = input_mask->ipv4_mask.src_ip;
341         info->mask.dst_ipv4_mask = input_mask->ipv4_mask.dst_ip;
342
343         if (dev->data->dev_conf.fdir_conf.mode == RTE_FDIR_MODE_SIGNATURE) {
344                 /*
345                  * IPv6 mask is only meaningful in signature mode
346                  * Store source and destination IPv6 masks (bit reversed)
347                  */
348                 IPV6_ADDR_TO_MASK(input_mask->ipv6_mask.src_ip, src_ipv6m);
349                 IPV6_ADDR_TO_MASK(input_mask->ipv6_mask.dst_ip, dst_ipv6m);
350                 fdiripv6m = (dst_ipv6m << 16) | src_ipv6m;
351
352                 IXGBE_WRITE_REG(hw, IXGBE_FDIRIP6M, ~fdiripv6m);
353                 info->mask.src_ipv6_mask = src_ipv6m;
354                 info->mask.dst_ipv6_mask = dst_ipv6m;
355         }
356
357         return IXGBE_SUCCESS;
358 }
359
360 /*
361  * ixgbe_check_fdir_flex_conf -check if the flex payload and mask configuration
362  * arguments are valid
363  */
364 static int
365 ixgbe_set_fdir_flex_conf(struct rte_eth_dev *dev,
366                 const struct rte_eth_fdir_flex_conf *conf, uint32_t *fdirctrl)
367 {
368         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
369         struct ixgbe_hw_fdir_info *info =
370                         IXGBE_DEV_PRIVATE_TO_FDIR_INFO(dev->data->dev_private);
371         const struct rte_eth_flex_payload_cfg *flex_cfg;
372         const struct rte_eth_fdir_flex_mask *flex_mask;
373         uint32_t fdirm;
374         uint16_t flexbytes = 0;
375         uint16_t i;
376
377         fdirm = IXGBE_READ_REG(hw, IXGBE_FDIRM);
378
379         if (conf == NULL) {
380                 PMD_DRV_LOG(INFO, "NULL pointer.");
381                 return -EINVAL;
382         }
383
384         for (i = 0; i < conf->nb_payloads; i++) {
385                 flex_cfg = &conf->flex_set[i];
386                 if (flex_cfg->type != RTE_ETH_RAW_PAYLOAD) {
387                         PMD_DRV_LOG(ERR, "unsupported payload type.");
388                         return -EINVAL;
389                 }
390                 if (((flex_cfg->src_offset[0] & 0x1) == 0) &&
391                     (flex_cfg->src_offset[1] == flex_cfg->src_offset[0] + 1) &&
392                     (flex_cfg->src_offset[0] <= IXGBE_MAX_FLX_SOURCE_OFF)) {
393                         *fdirctrl &= ~IXGBE_FDIRCTRL_FLEX_MASK;
394                         *fdirctrl |=
395                                 (flex_cfg->src_offset[0] / sizeof(uint16_t)) <<
396                                         IXGBE_FDIRCTRL_FLEX_SHIFT;
397                 } else {
398                         PMD_DRV_LOG(ERR, "invalid flexbytes arguments.");
399                         return -EINVAL;
400                 }
401         }
402
403         for (i = 0; i < conf->nb_flexmasks; i++) {
404                 flex_mask = &conf->flex_mask[i];
405                 if (flex_mask->flow_type != RTE_ETH_FLOW_UNKNOWN) {
406                         PMD_DRV_LOG(ERR, "flexmask should be set globally.");
407                         return -EINVAL;
408                 }
409                 flexbytes = (uint16_t)(((flex_mask->mask[0] << 8) & 0xFF00) |
410                                         ((flex_mask->mask[1]) & 0xFF));
411                 if (flexbytes == UINT16_MAX)
412                         fdirm &= ~IXGBE_FDIRM_FLEX;
413                 else if (flexbytes != 0) {
414                         /* IXGBE_FDIRM_FLEX is set by default when set mask */
415                         PMD_DRV_LOG(ERR, " invalid flexbytes mask arguments.");
416                         return -EINVAL;
417                 }
418         }
419         IXGBE_WRITE_REG(hw, IXGBE_FDIRM, fdirm);
420         info->mask.flex_bytes_mask = flexbytes ? UINT16_MAX : 0;
421         info->flex_bytes_offset = (uint8_t)((*fdirctrl &
422                                             IXGBE_FDIRCTRL_FLEX_MASK) >>
423                                             IXGBE_FDIRCTRL_FLEX_SHIFT);
424         return 0;
425 }
426
427 int
428 ixgbe_fdir_configure(struct rte_eth_dev *dev)
429 {
430         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
431         int err;
432         uint32_t fdirctrl, pbsize;
433         int i;
434
435         PMD_INIT_FUNC_TRACE();
436
437         if (hw->mac.type != ixgbe_mac_82599EB &&
438                 hw->mac.type != ixgbe_mac_X540 &&
439                 hw->mac.type != ixgbe_mac_X550 &&
440                 hw->mac.type != ixgbe_mac_X550EM_x)
441                 return -ENOSYS;
442
443         err = configure_fdir_flags(&dev->data->dev_conf.fdir_conf, &fdirctrl);
444         if (err)
445                 return err;
446
447         /*
448          * Before enabling Flow Director, the Rx Packet Buffer size
449          * must be reduced.  The new value is the current size minus
450          * flow director memory usage size.
451          */
452         pbsize = (1 << (PBALLOC_SIZE_SHIFT + (fdirctrl & FDIRCTRL_PBALLOC_MASK)));
453         IXGBE_WRITE_REG(hw, IXGBE_RXPBSIZE(0),
454             (IXGBE_READ_REG(hw, IXGBE_RXPBSIZE(0)) - pbsize));
455
456         /*
457          * The defaults in the HW for RX PB 1-7 are not zero and so should be
458          * intialized to zero for non DCB mode otherwise actual total RX PB
459          * would be bigger than programmed and filter space would run into
460          * the PB 0 region.
461          */
462         for (i = 1; i < 8; i++)
463                 IXGBE_WRITE_REG(hw, IXGBE_RXPBSIZE(i), 0);
464
465         err = fdir_set_input_mask_82599(dev, &dev->data->dev_conf.fdir_conf.mask);
466         if (err < 0) {
467                 PMD_INIT_LOG(ERR, " Error on setting FD mask");
468                 return err;
469         }
470         err = ixgbe_set_fdir_flex_conf(dev,
471                 &dev->data->dev_conf.fdir_conf.flex_conf, &fdirctrl);
472         if (err < 0) {
473                 PMD_INIT_LOG(ERR, " Error on setting FD flexible arguments.");
474                 return err;
475         }
476
477         err = fdir_enable_82599(hw, fdirctrl);
478         if (err < 0) {
479                 PMD_INIT_LOG(ERR, " Error on enabling FD.");
480                 return err;
481         }
482         return 0;
483 }
484
485 /*
486  * Convert DPDK rte_eth_fdir_filter struct to ixgbe_atr_input union that is used
487  * by the IXGBE driver code.
488  */
489 static int
490 ixgbe_fdir_filter_to_atr_input(const struct rte_eth_fdir_filter *fdir_filter,
491                 union ixgbe_atr_input *input)
492 {
493         input->formatted.vlan_id = fdir_filter->input.flow_ext.vlan_tci;
494         input->formatted.flex_bytes = (uint16_t)(
495                 (fdir_filter->input.flow_ext.flexbytes[1] << 8 & 0xFF00) |
496                 (fdir_filter->input.flow_ext.flexbytes[0] & 0xFF));
497
498         switch (fdir_filter->input.flow_type) {
499         case RTE_ETH_FLOW_NONFRAG_IPV4_UDP:
500                 input->formatted.flow_type = IXGBE_ATR_FLOW_TYPE_UDPV4;
501                 break;
502         case RTE_ETH_FLOW_NONFRAG_IPV4_TCP:
503                 input->formatted.flow_type = IXGBE_ATR_FLOW_TYPE_TCPV4;
504                 break;
505         case RTE_ETH_FLOW_NONFRAG_IPV4_SCTP:
506                 input->formatted.flow_type = IXGBE_ATR_FLOW_TYPE_SCTPV4;
507                 break;
508         case RTE_ETH_FLOW_NONFRAG_IPV4_OTHER:
509                 input->formatted.flow_type = IXGBE_ATR_FLOW_TYPE_IPV4;
510                 break;
511         case RTE_ETH_FLOW_NONFRAG_IPV6_UDP:
512                 input->formatted.flow_type = IXGBE_ATR_FLOW_TYPE_UDPV6;
513                 break;
514         case RTE_ETH_FLOW_NONFRAG_IPV6_TCP:
515                 input->formatted.flow_type = IXGBE_ATR_FLOW_TYPE_TCPV6;
516                 break;
517         case RTE_ETH_FLOW_NONFRAG_IPV6_SCTP:
518                 input->formatted.flow_type = IXGBE_ATR_FLOW_TYPE_SCTPV6;
519                 break;
520         case RTE_ETH_FLOW_NONFRAG_IPV6_OTHER:
521                 input->formatted.flow_type = IXGBE_ATR_FLOW_TYPE_IPV6;
522                 break;
523         default:
524                 PMD_DRV_LOG(ERR, " Error on flow_type input");
525                 return -EINVAL;
526         }
527
528         switch (fdir_filter->input.flow_type) {
529         case RTE_ETH_FLOW_NONFRAG_IPV4_UDP:
530         case RTE_ETH_FLOW_NONFRAG_IPV4_TCP:
531                 input->formatted.src_port =
532                         fdir_filter->input.flow.udp4_flow.src_port;
533                 input->formatted.dst_port =
534                         fdir_filter->input.flow.udp4_flow.dst_port;
535         /*for SCTP flow type, port and verify_tag are meaningless in ixgbe.*/
536         case RTE_ETH_FLOW_NONFRAG_IPV4_SCTP:
537         case RTE_ETH_FLOW_NONFRAG_IPV4_OTHER:
538                 input->formatted.src_ip[0] =
539                         fdir_filter->input.flow.ip4_flow.src_ip;
540                 input->formatted.dst_ip[0] =
541                         fdir_filter->input.flow.ip4_flow.dst_ip;
542                 break;
543
544         case RTE_ETH_FLOW_NONFRAG_IPV6_UDP:
545         case RTE_ETH_FLOW_NONFRAG_IPV6_TCP:
546                 input->formatted.src_port =
547                         fdir_filter->input.flow.udp6_flow.src_port;
548                 input->formatted.dst_port =
549                         fdir_filter->input.flow.udp6_flow.dst_port;
550         /*for SCTP flow type, port and verify_tag are meaningless in ixgbe.*/
551         case RTE_ETH_FLOW_NONFRAG_IPV6_SCTP:
552         case RTE_ETH_FLOW_NONFRAG_IPV6_OTHER:
553                 rte_memcpy(input->formatted.src_ip,
554                            fdir_filter->input.flow.ipv6_flow.src_ip,
555                            sizeof(input->formatted.src_ip));
556                 rte_memcpy(input->formatted.dst_ip,
557                            fdir_filter->input.flow.ipv6_flow.dst_ip,
558                            sizeof(input->formatted.dst_ip));
559                 break;
560         default:
561                 PMD_DRV_LOG(ERR, " Error on flow_type input");
562                 return -EINVAL;
563         }
564
565         return 0;
566 }
567
568 /*
569  * The below function is taken from the FreeBSD IXGBE drivers release
570  * 2.3.8. The only change is not to mask hash_result with IXGBE_ATR_HASH_MASK
571  * before returning, as the signature hash can use 16bits.
572  *
573  * The newer driver has optimised functions for calculating bucket and
574  * signature hashes. However they don't support IPv6 type packets for signature
575  * filters so are not used here.
576  *
577  * Note that the bkt_hash field in the ixgbe_atr_input structure is also never
578  * set.
579  *
580  * Compute the hashes for SW ATR
581  *  @stream: input bitstream to compute the hash on
582  *  @key: 32-bit hash key
583  **/
584 static uint32_t
585 ixgbe_atr_compute_hash_82599(union ixgbe_atr_input *atr_input,
586                                  uint32_t key)
587 {
588         /*
589          * The algorithm is as follows:
590          *    Hash[15:0] = Sum { S[n] x K[n+16] }, n = 0...350
591          *    where Sum {A[n]}, n = 0...n is bitwise XOR of A[0], A[1]...A[n]
592          *    and A[n] x B[n] is bitwise AND between same length strings
593          *
594          *    K[n] is 16 bits, defined as:
595          *       for n modulo 32 >= 15, K[n] = K[n % 32 : (n % 32) - 15]
596          *       for n modulo 32 < 15, K[n] =
597          *             K[(n % 32:0) | (31:31 - (14 - (n % 32)))]
598          *
599          *    S[n] is 16 bits, defined as:
600          *       for n >= 15, S[n] = S[n:n - 15]
601          *       for n < 15, S[n] = S[(n:0) | (350:350 - (14 - n))]
602          *
603          *    To simplify for programming, the algorithm is implemented
604          *    in software this way:
605          *
606          *    key[31:0], hi_hash_dword[31:0], lo_hash_dword[31:0], hash[15:0]
607          *
608          *    for (i = 0; i < 352; i+=32)
609          *        hi_hash_dword[31:0] ^= Stream[(i+31):i];
610          *
611          *    lo_hash_dword[15:0]  ^= Stream[15:0];
612          *    lo_hash_dword[15:0]  ^= hi_hash_dword[31:16];
613          *    lo_hash_dword[31:16] ^= hi_hash_dword[15:0];
614          *
615          *    hi_hash_dword[31:0]  ^= Stream[351:320];
616          *
617          *    if(key[0])
618          *        hash[15:0] ^= Stream[15:0];
619          *
620          *    for (i = 0; i < 16; i++) {
621          *        if (key[i])
622          *            hash[15:0] ^= lo_hash_dword[(i+15):i];
623          *        if (key[i + 16])
624          *            hash[15:0] ^= hi_hash_dword[(i+15):i];
625          *    }
626          *
627          */
628         __be32 common_hash_dword = 0;
629         u32 hi_hash_dword, lo_hash_dword, flow_vm_vlan;
630         u32 hash_result = 0;
631         u8 i;
632
633         /* record the flow_vm_vlan bits as they are a key part to the hash */
634         flow_vm_vlan = IXGBE_NTOHL(atr_input->dword_stream[0]);
635
636         /* generate common hash dword */
637         for (i = 1; i <= 13; i++)
638                 common_hash_dword ^= atr_input->dword_stream[i];
639
640         hi_hash_dword = IXGBE_NTOHL(common_hash_dword);
641
642         /* low dword is word swapped version of common */
643         lo_hash_dword = (hi_hash_dword >> 16) | (hi_hash_dword << 16);
644
645         /* apply flow ID/VM pool/VLAN ID bits to hash words */
646         hi_hash_dword ^= flow_vm_vlan ^ (flow_vm_vlan >> 16);
647
648         /* Process bits 0 and 16 */
649         if (key & 0x0001) hash_result ^= lo_hash_dword;
650         if (key & 0x00010000) hash_result ^= hi_hash_dword;
651
652         /*
653          * apply flow ID/VM pool/VLAN ID bits to lo hash dword, we had to
654          * delay this because bit 0 of the stream should not be processed
655          * so we do not add the vlan until after bit 0 was processed
656          */
657         lo_hash_dword ^= flow_vm_vlan ^ (flow_vm_vlan << 16);
658
659
660         /* process the remaining 30 bits in the key 2 bits at a time */
661         for (i = 15; i; i-- ) {
662                 if (key & (0x0001 << i)) hash_result ^= lo_hash_dword >> i;
663                 if (key & (0x00010000 << i)) hash_result ^= hi_hash_dword >> i;
664         }
665
666         return hash_result;
667 }
668
669 static uint32_t
670 atr_compute_perfect_hash_82599(union ixgbe_atr_input *input,
671                 enum rte_fdir_pballoc_type pballoc)
672 {
673         if (pballoc == RTE_FDIR_PBALLOC_256K)
674                 return ixgbe_atr_compute_hash_82599(input,
675                                 IXGBE_ATR_BUCKET_HASH_KEY) &
676                                 PERFECT_BUCKET_256KB_HASH_MASK;
677         else if (pballoc == RTE_FDIR_PBALLOC_128K)
678                 return ixgbe_atr_compute_hash_82599(input,
679                                 IXGBE_ATR_BUCKET_HASH_KEY) &
680                                 PERFECT_BUCKET_128KB_HASH_MASK;
681         else
682                 return ixgbe_atr_compute_hash_82599(input,
683                                 IXGBE_ATR_BUCKET_HASH_KEY) &
684                                 PERFECT_BUCKET_64KB_HASH_MASK;
685 }
686
687 /**
688  * ixgbe_fdir_check_cmd_complete - poll to check whether FDIRCMD is complete
689  * @hw: pointer to hardware structure
690  */
691 static inline int
692 ixgbe_fdir_check_cmd_complete(struct ixgbe_hw *hw, uint32_t *fdircmd)
693 {
694         int i;
695
696         for (i = 0; i < IXGBE_FDIRCMD_CMD_POLL; i++) {
697                 *fdircmd = IXGBE_READ_REG(hw, IXGBE_FDIRCMD);
698                 if (!(*fdircmd & IXGBE_FDIRCMD_CMD_MASK))
699                         return 0;
700                 rte_delay_us(IXGBE_FDIRCMD_CMD_INTERVAL_US);
701         }
702
703         return -ETIMEDOUT;
704 }
705
706 /*
707  * Calculate the hash value needed for signature-match filters. In the FreeBSD
708  * driver, this is done by the optimised function
709  * ixgbe_atr_compute_sig_hash_82599(). However that can't be used here as it
710  * doesn't support calculating a hash for an IPv6 filter.
711  */
712 static uint32_t
713 atr_compute_sig_hash_82599(union ixgbe_atr_input *input,
714                 enum rte_fdir_pballoc_type pballoc)
715 {
716         uint32_t bucket_hash, sig_hash;
717
718         if (pballoc == RTE_FDIR_PBALLOC_256K)
719                 bucket_hash = ixgbe_atr_compute_hash_82599(input,
720                                 IXGBE_ATR_BUCKET_HASH_KEY) &
721                                 SIG_BUCKET_256KB_HASH_MASK;
722         else if (pballoc == RTE_FDIR_PBALLOC_128K)
723                 bucket_hash = ixgbe_atr_compute_hash_82599(input,
724                                 IXGBE_ATR_BUCKET_HASH_KEY) &
725                                 SIG_BUCKET_128KB_HASH_MASK;
726         else
727                 bucket_hash = ixgbe_atr_compute_hash_82599(input,
728                                 IXGBE_ATR_BUCKET_HASH_KEY) &
729                                 SIG_BUCKET_64KB_HASH_MASK;
730
731         sig_hash = ixgbe_atr_compute_hash_82599(input,
732                         IXGBE_ATR_SIGNATURE_HASH_KEY);
733
734         return (sig_hash << IXGBE_FDIRHASH_SIG_SW_INDEX_SHIFT) | bucket_hash;
735 }
736
737 /*
738  * This is based on ixgbe_fdir_write_perfect_filter_82599() in
739  * base/ixgbe_82599.c, with the ability to set extra flags in FDIRCMD register
740  * added, and IPv6 support also added. The hash value is also pre-calculated
741  * as the pballoc value is needed to do it.
742  */
743 static int
744 fdir_write_perfect_filter_82599(struct ixgbe_hw *hw,
745                         union ixgbe_atr_input *input, uint8_t queue,
746                         uint32_t fdircmd, uint32_t fdirhash)
747 {
748         uint32_t fdirport, fdirvlan;
749         int err = 0;
750
751         /* record the IPv4 address (big-endian) */
752         IXGBE_WRITE_REG(hw, IXGBE_FDIRIPSA, input->formatted.src_ip[0]);
753         IXGBE_WRITE_REG(hw, IXGBE_FDIRIPDA, input->formatted.dst_ip[0]);
754
755         /* record source and destination port (little-endian)*/
756         fdirport = IXGBE_NTOHS(input->formatted.dst_port);
757         fdirport <<= IXGBE_FDIRPORT_DESTINATION_SHIFT;
758         fdirport |= IXGBE_NTOHS(input->formatted.src_port);
759         IXGBE_WRITE_REG(hw, IXGBE_FDIRPORT, fdirport);
760
761         /* record vlan (little-endian) and flex_bytes(big-endian) */
762         fdirvlan = input->formatted.flex_bytes;
763         fdirvlan <<= IXGBE_FDIRVLAN_FLEX_SHIFT;
764         fdirvlan |= IXGBE_NTOHS(input->formatted.vlan_id);
765         IXGBE_WRITE_REG(hw, IXGBE_FDIRVLAN, fdirvlan);
766
767         /* configure FDIRHASH register */
768         IXGBE_WRITE_REG(hw, IXGBE_FDIRHASH, fdirhash);
769
770         /*
771          * flush all previous writes to make certain registers are
772          * programmed prior to issuing the command
773          */
774         IXGBE_WRITE_FLUSH(hw);
775
776         /* configure FDIRCMD register */
777         fdircmd |= IXGBE_FDIRCMD_CMD_ADD_FLOW |
778                   IXGBE_FDIRCMD_LAST | IXGBE_FDIRCMD_QUEUE_EN;
779         fdircmd |= input->formatted.flow_type << IXGBE_FDIRCMD_FLOW_TYPE_SHIFT;
780         fdircmd |= (uint32_t)queue << IXGBE_FDIRCMD_RX_QUEUE_SHIFT;
781         fdircmd |= (uint32_t)input->formatted.vm_pool << IXGBE_FDIRCMD_VT_POOL_SHIFT;
782
783         IXGBE_WRITE_REG(hw, IXGBE_FDIRCMD, fdircmd);
784
785         PMD_DRV_LOG(DEBUG, "Rx Queue=%x hash=%x", queue, fdirhash);
786
787         err = ixgbe_fdir_check_cmd_complete(hw, &fdircmd);
788         if (err < 0)
789                 PMD_DRV_LOG(ERR, "Timeout writing flow director filter.");
790
791         return err;
792 }
793
794 /**
795  * This function is based on ixgbe_atr_add_signature_filter_82599() in
796  * base/ixgbe_82599.c, but uses a pre-calculated hash value. It also supports
797  * setting extra fields in the FDIRCMD register, and removes the code that was
798  * verifying the flow_type field. According to the documentation, a flow type of
799  * 00 (i.e. not TCP, UDP, or SCTP) is not supported, however it appears to
800  * work ok...
801  *
802  *  Adds a signature hash filter
803  *  @hw: pointer to hardware structure
804  *  @input: unique input dword
805  *  @queue: queue index to direct traffic to
806  *  @fdircmd: any extra flags to set in fdircmd register
807  *  @fdirhash: pre-calculated hash value for the filter
808  **/
809 static int
810 fdir_add_signature_filter_82599(struct ixgbe_hw *hw,
811                 union ixgbe_atr_input *input, u8 queue, uint32_t fdircmd,
812                 uint32_t fdirhash)
813 {
814         int err = 0;
815
816         PMD_INIT_FUNC_TRACE();
817
818         /* configure FDIRCMD register */
819         fdircmd |= IXGBE_FDIRCMD_CMD_ADD_FLOW |
820                   IXGBE_FDIRCMD_LAST | IXGBE_FDIRCMD_QUEUE_EN;
821         fdircmd |= input->formatted.flow_type << IXGBE_FDIRCMD_FLOW_TYPE_SHIFT;
822         fdircmd |= (uint32_t)queue << IXGBE_FDIRCMD_RX_QUEUE_SHIFT;
823
824         IXGBE_WRITE_REG(hw, IXGBE_FDIRHASH, fdirhash);
825         IXGBE_WRITE_REG(hw, IXGBE_FDIRCMD, fdircmd);
826
827         PMD_DRV_LOG(DEBUG, "Rx Queue=%x hash=%x", queue, fdirhash);
828
829         err = ixgbe_fdir_check_cmd_complete(hw, &fdircmd);
830         if (err < 0)
831                 PMD_DRV_LOG(ERR, "Timeout writing flow director filter.");
832
833         return err;
834 }
835
836 /*
837  * This is based on ixgbe_fdir_erase_perfect_filter_82599() in
838  * base/ixgbe_82599.c. It is modified to take in the hash as a parameter so
839  * that it can be used for removing signature and perfect filters.
840  */
841 static int
842 fdir_erase_filter_82599(struct ixgbe_hw *hw, uint32_t fdirhash)
843 {
844         uint32_t fdircmd = 0;
845         int err = 0;
846
847         IXGBE_WRITE_REG(hw, IXGBE_FDIRHASH, fdirhash);
848
849         /* flush hash to HW */
850         IXGBE_WRITE_FLUSH(hw);
851
852         /* Query if filter is present */
853         IXGBE_WRITE_REG(hw, IXGBE_FDIRCMD, IXGBE_FDIRCMD_CMD_QUERY_REM_FILT);
854
855         err = ixgbe_fdir_check_cmd_complete(hw, &fdircmd);
856         if (err < 0) {
857                 PMD_INIT_LOG(ERR, "Timeout querying for flow director filter.");
858                 return err;
859         }
860
861         /* if filter exists in hardware then remove it */
862         if (fdircmd & IXGBE_FDIRCMD_FILTER_VALID) {
863                 IXGBE_WRITE_REG(hw, IXGBE_FDIRHASH, fdirhash);
864                 IXGBE_WRITE_FLUSH(hw);
865                 IXGBE_WRITE_REG(hw, IXGBE_FDIRCMD,
866                                 IXGBE_FDIRCMD_CMD_REMOVE_FLOW);
867         }
868         err = ixgbe_fdir_check_cmd_complete(hw, &fdircmd);
869         if (err < 0)
870                 PMD_INIT_LOG(ERR, "Timeout erasing flow director filter.");
871         return err;
872
873 }
874
875 /*
876  * ixgbe_add_del_fdir_filter - add or remove a flow diretor filter.
877  * @dev: pointer to the structure rte_eth_dev
878  * @fdir_filter: fdir filter entry
879  * @del: 1 - delete, 0 - add
880  * @update: 1 - update
881  */
882 static int
883 ixgbe_add_del_fdir_filter(struct rte_eth_dev *dev,
884                               const struct rte_eth_fdir_filter *fdir_filter,
885                               bool del,
886                               bool update)
887 {
888         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
889         uint32_t fdircmd_flags;
890         uint32_t fdirhash;
891         union ixgbe_atr_input input;
892         uint8_t queue;
893         bool is_perfect = FALSE;
894         int err;
895         struct ixgbe_hw_fdir_info *info =
896                         IXGBE_DEV_PRIVATE_TO_FDIR_INFO(dev->data->dev_private);
897
898         if (dev->data->dev_conf.fdir_conf.mode == RTE_FDIR_MODE_NONE)
899                 return -ENOTSUP;
900
901         /*
902          * Sanity check for x550.
903          * When adding a new filter with flow type set to IPv4-other,
904          * the flow director mask should be configed before,
905          * and the L4 protocol and ports are masked.
906          */
907         if ((!del) &&
908             (hw->mac.type == ixgbe_mac_X550 ||
909              hw->mac.type == ixgbe_mac_X550EM_x) &&
910             (fdir_filter->input.flow_type ==
911                RTE_ETH_FLOW_NONFRAG_IPV4_OTHER) &&
912             (info->mask.src_port_mask != 0 ||
913              info->mask.dst_port_mask != 0)) {
914                 PMD_DRV_LOG(ERR, "By this device,"
915                                  " IPv4-other is not supported without"
916                                  " L4 protocol and ports masked!");
917                 return -ENOTSUP;
918         }
919
920         if (dev->data->dev_conf.fdir_conf.mode == RTE_FDIR_MODE_PERFECT)
921                 is_perfect = TRUE;
922
923         memset(&input, 0, sizeof(input));
924
925         err = ixgbe_fdir_filter_to_atr_input(fdir_filter, &input);
926         if (err)
927                 return err;
928
929         if (is_perfect) {
930                 if (input.formatted.flow_type & IXGBE_ATR_L4TYPE_IPV6_MASK) {
931                         PMD_DRV_LOG(ERR, "IPv6 is not supported in"
932                                          " perfect mode!");
933                         return -ENOTSUP;
934                 }
935                 fdirhash = atr_compute_perfect_hash_82599(&input,
936                                 dev->data->dev_conf.fdir_conf.pballoc);
937                 fdirhash |= fdir_filter->soft_id <<
938                                 IXGBE_FDIRHASH_SIG_SW_INDEX_SHIFT;
939         } else
940                 fdirhash = atr_compute_sig_hash_82599(&input,
941                                 dev->data->dev_conf.fdir_conf.pballoc);
942
943         if (del) {
944                 err = fdir_erase_filter_82599(hw, fdirhash);
945                 if (err < 0)
946                         PMD_DRV_LOG(ERR, "Fail to delete FDIR filter!");
947                 else
948                         PMD_DRV_LOG(DEBUG, "Success to delete FDIR filter!");
949                 return err;
950         }
951         /* add or update an fdir filter*/
952         fdircmd_flags = (update) ? IXGBE_FDIRCMD_FILTER_UPDATE : 0;
953         if (fdir_filter->action.behavior == RTE_ETH_FDIR_REJECT) {
954                 if (is_perfect) {
955                         queue = dev->data->dev_conf.fdir_conf.drop_queue;
956                         fdircmd_flags |= IXGBE_FDIRCMD_DROP;
957                 } else {
958                         PMD_DRV_LOG(ERR, "Drop option is not supported in"
959                                 " signature mode.");
960                         return -EINVAL;
961                 }
962         } else if (fdir_filter->action.rx_queue < IXGBE_MAX_RX_QUEUE_NUM)
963                 queue = (uint8_t)fdir_filter->action.rx_queue;
964         else
965                 return -EINVAL;
966
967         if (is_perfect) {
968                 err = fdir_write_perfect_filter_82599(hw, &input, queue,
969                                 fdircmd_flags, fdirhash);
970         } else {
971                 err = fdir_add_signature_filter_82599(hw, &input, queue,
972                                 fdircmd_flags, fdirhash);
973         }
974         if (err < 0)
975                 PMD_DRV_LOG(ERR, "Fail to add FDIR filter!");
976         else
977                 PMD_DRV_LOG(DEBUG, "Success to add FDIR filter");
978
979         return err;
980 }
981
982 static int
983 ixgbe_fdir_flush(struct rte_eth_dev *dev)
984 {
985         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
986         struct ixgbe_hw_fdir_info *info =
987                         IXGBE_DEV_PRIVATE_TO_FDIR_INFO(dev->data->dev_private);
988         int ret;
989
990         ret = ixgbe_reinit_fdir_tables_82599(hw);
991         if (ret < 0) {
992                 PMD_INIT_LOG(ERR, "Failed to re-initialize FD table.");
993                 return ret;
994         }
995
996         info->f_add = 0;
997         info->f_remove = 0;
998         info->add = 0;
999         info->remove = 0;
1000
1001         return ret;
1002 }
1003
1004 #define FDIRENTRIES_NUM_SHIFT 10
1005 static void
1006 ixgbe_fdir_info_get(struct rte_eth_dev *dev, struct rte_eth_fdir_info *fdir_info)
1007 {
1008         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1009         struct ixgbe_hw_fdir_info *info =
1010                         IXGBE_DEV_PRIVATE_TO_FDIR_INFO(dev->data->dev_private);
1011         uint32_t fdirctrl, max_num;
1012         uint8_t offset;
1013
1014         fdirctrl = IXGBE_READ_REG(hw, IXGBE_FDIRCTRL);
1015         offset = ((fdirctrl & IXGBE_FDIRCTRL_FLEX_MASK) >>
1016                         IXGBE_FDIRCTRL_FLEX_SHIFT) * sizeof(uint16_t);
1017
1018         fdir_info->mode = dev->data->dev_conf.fdir_conf.mode;
1019         max_num = (1 << (FDIRENTRIES_NUM_SHIFT +
1020                         (fdirctrl & FDIRCTRL_PBALLOC_MASK)));
1021         if (fdir_info->mode == RTE_FDIR_MODE_PERFECT)
1022                 fdir_info->guarant_spc = max_num;
1023         else if (fdir_info->mode == RTE_FDIR_MODE_SIGNATURE)
1024                 fdir_info->guarant_spc = max_num * 4;
1025
1026         fdir_info->mask.vlan_tci_mask = info->mask.vlan_tci_mask;
1027         fdir_info->mask.ipv4_mask.src_ip = info->mask.src_ipv4_mask;
1028         fdir_info->mask.ipv4_mask.dst_ip = info->mask.dst_ipv4_mask;
1029         IPV6_MASK_TO_ADDR(info->mask.src_ipv6_mask,
1030                         fdir_info->mask.ipv6_mask.src_ip);
1031         IPV6_MASK_TO_ADDR(info->mask.dst_ipv6_mask,
1032                         fdir_info->mask.ipv6_mask.dst_ip);
1033         fdir_info->mask.src_port_mask = info->mask.src_port_mask;
1034         fdir_info->mask.dst_port_mask = info->mask.dst_port_mask;
1035         fdir_info->max_flexpayload = IXGBE_FDIR_MAX_FLEX_LEN;
1036         fdir_info->flow_types_mask[0] = IXGBE_FDIR_FLOW_TYPES;
1037         fdir_info->flex_payload_unit = sizeof(uint16_t);
1038         fdir_info->max_flex_payload_segment_num = 1;
1039         fdir_info->flex_payload_limit = 62;
1040         fdir_info->flex_conf.nb_payloads = 1;
1041         fdir_info->flex_conf.flex_set[0].type = RTE_ETH_RAW_PAYLOAD;
1042         fdir_info->flex_conf.flex_set[0].src_offset[0] = offset;
1043         fdir_info->flex_conf.flex_set[0].src_offset[1] = offset + 1;
1044         fdir_info->flex_conf.nb_flexmasks = 1;
1045         fdir_info->flex_conf.flex_mask[0].flow_type = RTE_ETH_FLOW_UNKNOWN;
1046         fdir_info->flex_conf.flex_mask[0].mask[0] =
1047                         (uint8_t)(info->mask.flex_bytes_mask & 0x00FF);
1048         fdir_info->flex_conf.flex_mask[0].mask[1] =
1049                         (uint8_t)((info->mask.flex_bytes_mask & 0xFF00) >> 8);
1050 }
1051
1052 static void
1053 ixgbe_fdir_stats_get(struct rte_eth_dev *dev, struct rte_eth_fdir_stats *fdir_stats)
1054 {
1055         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1056         struct ixgbe_hw_fdir_info *info =
1057                         IXGBE_DEV_PRIVATE_TO_FDIR_INFO(dev->data->dev_private);
1058         uint32_t reg, max_num;
1059
1060         /* Get the information from registers */
1061         reg = IXGBE_READ_REG(hw, IXGBE_FDIRFREE);
1062         info->collision = (uint16_t)((reg & IXGBE_FDIRFREE_COLL_MASK) >>
1063                                         IXGBE_FDIRFREE_COLL_SHIFT);
1064         info->free = (uint16_t)((reg & IXGBE_FDIRFREE_FREE_MASK) >>
1065                                    IXGBE_FDIRFREE_FREE_SHIFT);
1066
1067         reg = IXGBE_READ_REG(hw, IXGBE_FDIRLEN);
1068         info->maxhash = (uint16_t)((reg & IXGBE_FDIRLEN_MAXHASH_MASK) >>
1069                                       IXGBE_FDIRLEN_MAXHASH_SHIFT);
1070         info->maxlen  = (uint8_t)((reg & IXGBE_FDIRLEN_MAXLEN_MASK) >>
1071                                      IXGBE_FDIRLEN_MAXLEN_SHIFT);
1072
1073         reg = IXGBE_READ_REG(hw, IXGBE_FDIRUSTAT);
1074         info->remove += (reg & IXGBE_FDIRUSTAT_REMOVE_MASK) >>
1075                 IXGBE_FDIRUSTAT_REMOVE_SHIFT;
1076         info->add += (reg & IXGBE_FDIRUSTAT_ADD_MASK) >>
1077                 IXGBE_FDIRUSTAT_ADD_SHIFT;
1078
1079         reg = IXGBE_READ_REG(hw, IXGBE_FDIRFSTAT) & 0xFFFF;
1080         info->f_remove += (reg & IXGBE_FDIRFSTAT_FREMOVE_MASK) >>
1081                 IXGBE_FDIRFSTAT_FREMOVE_SHIFT;
1082         info->f_add += (reg & IXGBE_FDIRFSTAT_FADD_MASK) >>
1083                 IXGBE_FDIRFSTAT_FADD_SHIFT;
1084
1085         /*  Copy the new information in the fdir parameter */
1086         fdir_stats->collision = info->collision;
1087         fdir_stats->free = info->free;
1088         fdir_stats->maxhash = info->maxhash;
1089         fdir_stats->maxlen = info->maxlen;
1090         fdir_stats->remove = info->remove;
1091         fdir_stats->add = info->add;
1092         fdir_stats->f_remove = info->f_remove;
1093         fdir_stats->f_add = info->f_add;
1094
1095         reg = IXGBE_READ_REG(hw, IXGBE_FDIRCTRL);
1096         max_num = (1 << (FDIRENTRIES_NUM_SHIFT +
1097                         (reg & FDIRCTRL_PBALLOC_MASK)));
1098         if (dev->data->dev_conf.fdir_conf.mode == RTE_FDIR_MODE_PERFECT)
1099                         fdir_stats->guarant_cnt = max_num - fdir_stats->free;
1100         else if (dev->data->dev_conf.fdir_conf.mode == RTE_FDIR_MODE_SIGNATURE)
1101                 fdir_stats->guarant_cnt = max_num * 4 - fdir_stats->free;
1102
1103 }
1104
1105 /*
1106  * ixgbe_fdir_ctrl_func - deal with all operations on flow director.
1107  * @dev: pointer to the structure rte_eth_dev
1108  * @filter_op:operation will be taken
1109  * @arg: a pointer to specific structure corresponding to the filter_op
1110  */
1111 int
1112 ixgbe_fdir_ctrl_func(struct rte_eth_dev *dev,
1113                         enum rte_filter_op filter_op, void *arg)
1114 {
1115         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1116         int ret = 0;
1117
1118         if (hw->mac.type != ixgbe_mac_82599EB &&
1119                 hw->mac.type != ixgbe_mac_X540 &&
1120                 hw->mac.type != ixgbe_mac_X550 &&
1121                 hw->mac.type != ixgbe_mac_X550EM_x)
1122                 return -ENOTSUP;
1123
1124         if (filter_op == RTE_ETH_FILTER_NOP)
1125                 return 0;
1126
1127         if (arg == NULL && filter_op != RTE_ETH_FILTER_FLUSH)
1128                 return -EINVAL;
1129
1130         switch (filter_op) {
1131         case RTE_ETH_FILTER_ADD:
1132                 ret = ixgbe_add_del_fdir_filter(dev,
1133                         (struct rte_eth_fdir_filter *)arg, FALSE, FALSE);
1134                 break;
1135         case RTE_ETH_FILTER_UPDATE:
1136                 ret = ixgbe_add_del_fdir_filter(dev,
1137                         (struct rte_eth_fdir_filter *)arg, FALSE, TRUE);
1138                 break;
1139         case RTE_ETH_FILTER_DELETE:
1140                 ret = ixgbe_add_del_fdir_filter(dev,
1141                         (struct rte_eth_fdir_filter *)arg, TRUE, FALSE);
1142                 break;
1143         case RTE_ETH_FILTER_FLUSH:
1144                 ret = ixgbe_fdir_flush(dev);
1145                 break;
1146         case RTE_ETH_FILTER_INFO:
1147                 ixgbe_fdir_info_get(dev, (struct rte_eth_fdir_info *)arg);
1148                 break;
1149         case RTE_ETH_FILTER_STATS:
1150                 ixgbe_fdir_stats_get(dev, (struct rte_eth_fdir_stats *)arg);
1151                 break;
1152         default:
1153                 PMD_DRV_LOG(ERR, "unknown operation %u", filter_op);
1154                 ret = -EINVAL;
1155                 break;
1156         }
1157         return ret;
1158 }