ixgbe: fix X550 flow director check
[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);
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          * Continue setup of fdirctrl register bits:
252          *  Set the maximum length per hash bucket to 0xA filters
253          *  Send interrupt when 64 filters are left
254          */
255         *fdirctrl |= (0xA << IXGBE_FDIRCTRL_MAX_LENGTH_SHIFT) |
256                     (4 << IXGBE_FDIRCTRL_FULL_THRESH_SHIFT);
257
258         return 0;
259 }
260
261 /**
262  * Reverse the bits in FDIR registers that store 2 x 16 bit masks.
263  *
264  *  @hi_dword: Bits 31:16 mask to be bit swapped.
265  *  @lo_dword: Bits 15:0  mask to be bit swapped.
266  *
267  *  Flow director uses several registers to store 2 x 16 bit masks with the
268  *  bits reversed such as FDIRTCPM, FDIRUDPM. The LS bit of the
269  *  mask affects the MS bit/byte of the target. This function reverses the
270  *  bits in these masks.
271  *  **/
272 static inline uint32_t
273 reverse_fdir_bitmasks(uint16_t hi_dword, uint16_t lo_dword)
274 {
275         uint32_t mask = hi_dword << 16;
276         mask |= lo_dword;
277         mask = ((mask & 0x55555555) << 1) | ((mask & 0xAAAAAAAA) >> 1);
278         mask = ((mask & 0x33333333) << 2) | ((mask & 0xCCCCCCCC) >> 2);
279         mask = ((mask & 0x0F0F0F0F) << 4) | ((mask & 0xF0F0F0F0) >> 4);
280         return ((mask & 0x00FF00FF) << 8) | ((mask & 0xFF00FF00) >> 8);
281 }
282
283 /*
284  * This is based on ixgbe_fdir_set_input_mask_82599() in base/ixgbe_82599.c,
285  * but makes use of the rte_fdir_masks structure to see which bits to set.
286  */
287 static int
288 fdir_set_input_mask_82599(struct rte_eth_dev *dev,
289                 const struct rte_eth_fdir_masks *input_mask)
290 {
291         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
292         struct ixgbe_hw_fdir_info *info =
293                         IXGBE_DEV_PRIVATE_TO_FDIR_INFO(dev->data->dev_private);
294         /*
295          * mask VM pool and DIPv6 since there are currently not supported
296          * mask FLEX byte, it will be set in flex_conf
297          */
298         uint32_t fdirm = IXGBE_FDIRM_POOL | IXGBE_FDIRM_DIPv6 | IXGBE_FDIRM_FLEX;
299         uint32_t fdirtcpm;  /* TCP source and destination port masks. */
300         uint32_t fdiripv6m; /* IPv6 source and destination masks. */
301         uint16_t dst_ipv6m = 0;
302         uint16_t src_ipv6m = 0;
303
304         PMD_INIT_FUNC_TRACE();
305
306         /*
307          * Program the relevant mask registers.  If src/dst_port or src/dst_addr
308          * are zero, then assume a full mask for that field. Also assume that
309          * a VLAN of 0 is unspecified, so mask that out as well.  L4type
310          * cannot be masked out in this implementation.
311          */
312         if (input_mask->dst_port_mask == 0 && input_mask->src_port_mask == 0)
313                 /* use the L4 protocol mask for raw IPv4/IPv6 traffic */
314                 fdirm |= IXGBE_FDIRM_L4P;
315
316         if (input_mask->vlan_tci_mask == 0x0FFF)
317                 /* mask VLAN Priority */
318                 fdirm |= IXGBE_FDIRM_VLANP;
319         else if (input_mask->vlan_tci_mask == 0xE000)
320                 /* mask VLAN ID */
321                 fdirm |= IXGBE_FDIRM_VLANID;
322         else if (input_mask->vlan_tci_mask == 0)
323                 /* mask VLAN ID and Priority */
324                 fdirm |= IXGBE_FDIRM_VLANID | IXGBE_FDIRM_VLANP;
325         else if (input_mask->vlan_tci_mask != 0xEFFF) {
326                 PMD_INIT_LOG(ERR, "invalid vlan_tci_mask");
327                 return -EINVAL;
328         }
329         info->mask.vlan_tci_mask = input_mask->vlan_tci_mask;
330
331         IXGBE_WRITE_REG(hw, IXGBE_FDIRM, fdirm);
332
333         /* store the TCP/UDP port masks, bit reversed from port layout */
334         fdirtcpm = reverse_fdir_bitmasks(input_mask->dst_port_mask,
335                                          input_mask->src_port_mask);
336
337         /* write all the same so that UDP, TCP and SCTP use the same mask */
338         IXGBE_WRITE_REG(hw, IXGBE_FDIRTCPM, ~fdirtcpm);
339         IXGBE_WRITE_REG(hw, IXGBE_FDIRUDPM, ~fdirtcpm);
340         IXGBE_WRITE_REG(hw, IXGBE_FDIRSCTPM, ~fdirtcpm);
341         info->mask.src_port_mask = input_mask->src_port_mask;
342         info->mask.dst_port_mask = input_mask->dst_port_mask;
343
344         /* Store source and destination IPv4 masks (big-endian) */
345         IXGBE_WRITE_REG(hw, IXGBE_FDIRSIP4M, ~(input_mask->ipv4_mask.src_ip));
346         IXGBE_WRITE_REG(hw, IXGBE_FDIRDIP4M, ~(input_mask->ipv4_mask.dst_ip));
347         info->mask.src_ipv4_mask = input_mask->ipv4_mask.src_ip;
348         info->mask.dst_ipv4_mask = input_mask->ipv4_mask.dst_ip;
349
350         if (dev->data->dev_conf.fdir_conf.mode == RTE_FDIR_MODE_SIGNATURE) {
351                 /*
352                  * IPv6 mask is only meaningful in signature mode
353                  * Store source and destination IPv6 masks (bit reversed)
354                  */
355                 IPV6_ADDR_TO_MASK(input_mask->ipv6_mask.src_ip, src_ipv6m);
356                 IPV6_ADDR_TO_MASK(input_mask->ipv6_mask.dst_ip, dst_ipv6m);
357                 fdiripv6m = (dst_ipv6m << 16) | src_ipv6m;
358
359                 IXGBE_WRITE_REG(hw, IXGBE_FDIRIP6M, ~fdiripv6m);
360                 info->mask.src_ipv6_mask = src_ipv6m;
361                 info->mask.dst_ipv6_mask = dst_ipv6m;
362         }
363
364         return IXGBE_SUCCESS;
365 }
366
367 /*
368  * ixgbe_check_fdir_flex_conf -check if the flex payload and mask configuration
369  * arguments are valid
370  */
371 static int
372 ixgbe_set_fdir_flex_conf(struct rte_eth_dev *dev,
373                 const struct rte_eth_fdir_flex_conf *conf)
374 {
375         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
376         struct ixgbe_hw_fdir_info *info =
377                         IXGBE_DEV_PRIVATE_TO_FDIR_INFO(dev->data->dev_private);
378         const struct rte_eth_flex_payload_cfg *flex_cfg;
379         const struct rte_eth_fdir_flex_mask *flex_mask;
380         uint32_t fdirctrl, fdirm;
381         uint16_t flexbytes = 0;
382         uint16_t i;
383
384         fdirctrl = IXGBE_READ_REG(hw, IXGBE_FDIRCTRL);
385         fdirm = IXGBE_READ_REG(hw, IXGBE_FDIRM);
386
387         if (conf == NULL) {
388                 PMD_DRV_LOG(INFO, "NULL pointer.");
389                 return -EINVAL;
390         }
391
392         for (i = 0; i < conf->nb_payloads; i++) {
393                 flex_cfg = &conf->flex_set[i];
394                 if (flex_cfg->type != RTE_ETH_RAW_PAYLOAD) {
395                         PMD_DRV_LOG(ERR, "unsupported payload type.");
396                         return -EINVAL;
397                 }
398                 if (((flex_cfg->src_offset[0] & 0x1) == 0) &&
399                     (flex_cfg->src_offset[1] == flex_cfg->src_offset[0] + 1) &&
400                     (flex_cfg->src_offset[0] <= IXGBE_MAX_FLX_SOURCE_OFF)) {
401                         fdirctrl &= ~IXGBE_FDIRCTRL_FLEX_MASK;
402                         fdirctrl |= (flex_cfg->src_offset[0] / sizeof(uint16_t)) <<
403                                         IXGBE_FDIRCTRL_FLEX_SHIFT;
404                 } else {
405                         PMD_DRV_LOG(ERR, "invalid flexbytes arguments.");
406                         return -EINVAL;
407                 }
408         }
409
410         for (i = 0; i < conf->nb_flexmasks; i++) {
411                 flex_mask = &conf->flex_mask[i];
412                 if (flex_mask->flow_type != RTE_ETH_FLOW_UNKNOWN) {
413                         PMD_DRV_LOG(ERR, "flexmask should be set globally.");
414                         return -EINVAL;
415                 }
416                 flexbytes = (uint16_t)(((flex_mask->mask[0] << 8) & 0xFF00) |
417                                         ((flex_mask->mask[1]) & 0xFF));
418                 if (flexbytes == UINT16_MAX)
419                         fdirm &= ~IXGBE_FDIRM_FLEX;
420                 else if (flexbytes != 0) {
421                         /* IXGBE_FDIRM_FLEX is set by default when set mask */
422                         PMD_DRV_LOG(ERR, " invalid flexbytes mask arguments.");
423                         return -EINVAL;
424                 }
425         }
426         IXGBE_WRITE_REG(hw, IXGBE_FDIRCTRL, fdirctrl);
427         IXGBE_WRITE_REG(hw, IXGBE_FDIRM, fdirm);
428         info->mask.flex_bytes_mask = flexbytes ? UINT16_MAX : 0;
429         info->flex_bytes_offset = (uint8_t)((fdirctrl &
430                                             IXGBE_FDIRCTRL_FLEX_MASK) >>
431                                             IXGBE_FDIRCTRL_FLEX_SHIFT);
432         return 0;
433 }
434
435 int
436 ixgbe_fdir_configure(struct rte_eth_dev *dev)
437 {
438         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
439         int err;
440         uint32_t fdirctrl, pbsize;
441         int i;
442
443         PMD_INIT_FUNC_TRACE();
444
445         if (hw->mac.type != ixgbe_mac_82599EB &&
446                 hw->mac.type != ixgbe_mac_X540 &&
447                 hw->mac.type != ixgbe_mac_X550 &&
448                 hw->mac.type != ixgbe_mac_X550EM_x)
449                 return -ENOSYS;
450
451         err = configure_fdir_flags(&dev->data->dev_conf.fdir_conf, &fdirctrl);
452         if (err)
453                 return err;
454
455         /*
456          * Before enabling Flow Director, the Rx Packet Buffer size
457          * must be reduced.  The new value is the current size minus
458          * flow director memory usage size.
459          */
460         pbsize = (1 << (PBALLOC_SIZE_SHIFT + (fdirctrl & FDIRCTRL_PBALLOC_MASK)));
461         IXGBE_WRITE_REG(hw, IXGBE_RXPBSIZE(0),
462             (IXGBE_READ_REG(hw, IXGBE_RXPBSIZE(0)) - pbsize));
463
464         /*
465          * The defaults in the HW for RX PB 1-7 are not zero and so should be
466          * intialized to zero for non DCB mode otherwise actual total RX PB
467          * would be bigger than programmed and filter space would run into
468          * the PB 0 region.
469          */
470         for (i = 1; i < 8; i++)
471                 IXGBE_WRITE_REG(hw, IXGBE_RXPBSIZE(i), 0);
472
473         err = fdir_set_input_mask_82599(dev, &dev->data->dev_conf.fdir_conf.mask);
474         if (err < 0) {
475                 PMD_INIT_LOG(ERR, " Error on setting FD mask");
476                 return err;
477         }
478         err = ixgbe_set_fdir_flex_conf(dev,
479                 &dev->data->dev_conf.fdir_conf.flex_conf);
480         if (err < 0) {
481                 PMD_INIT_LOG(ERR, " Error on setting FD flexible arguments.");
482                 return err;
483         }
484
485         err = fdir_enable_82599(hw, fdirctrl);
486         if (err < 0) {
487                 PMD_INIT_LOG(ERR, " Error on enabling FD.");
488                 return err;
489         }
490         return 0;
491 }
492
493 /*
494  * Convert DPDK rte_eth_fdir_filter struct to ixgbe_atr_input union that is used
495  * by the IXGBE driver code.
496  */
497 static int
498 ixgbe_fdir_filter_to_atr_input(const struct rte_eth_fdir_filter *fdir_filter,
499                 union ixgbe_atr_input *input)
500 {
501         input->formatted.vlan_id = fdir_filter->input.flow_ext.vlan_tci;
502         input->formatted.flex_bytes = (uint16_t)(
503                 (fdir_filter->input.flow_ext.flexbytes[1] << 8 & 0xFF00) |
504                 (fdir_filter->input.flow_ext.flexbytes[0] & 0xFF));
505
506         switch (fdir_filter->input.flow_type) {
507         case RTE_ETH_FLOW_NONFRAG_IPV4_UDP:
508                 input->formatted.flow_type = IXGBE_ATR_FLOW_TYPE_UDPV4;
509                 break;
510         case RTE_ETH_FLOW_NONFRAG_IPV4_TCP:
511                 input->formatted.flow_type = IXGBE_ATR_FLOW_TYPE_TCPV4;
512                 break;
513         case RTE_ETH_FLOW_NONFRAG_IPV4_SCTP:
514                 input->formatted.flow_type = IXGBE_ATR_FLOW_TYPE_SCTPV4;
515                 break;
516         case RTE_ETH_FLOW_NONFRAG_IPV4_OTHER:
517                 input->formatted.flow_type = IXGBE_ATR_FLOW_TYPE_IPV4;
518                 break;
519         case RTE_ETH_FLOW_NONFRAG_IPV6_UDP:
520                 input->formatted.flow_type = IXGBE_ATR_FLOW_TYPE_UDPV6;
521                 break;
522         case RTE_ETH_FLOW_NONFRAG_IPV6_TCP:
523                 input->formatted.flow_type = IXGBE_ATR_FLOW_TYPE_TCPV6;
524                 break;
525         case RTE_ETH_FLOW_NONFRAG_IPV6_SCTP:
526                 input->formatted.flow_type = IXGBE_ATR_FLOW_TYPE_SCTPV6;
527                 break;
528         case RTE_ETH_FLOW_NONFRAG_IPV6_OTHER:
529                 input->formatted.flow_type = IXGBE_ATR_FLOW_TYPE_IPV6;
530                 break;
531         default:
532                 PMD_DRV_LOG(ERR, " Error on flow_type input");
533                 return -EINVAL;
534         }
535
536         switch (fdir_filter->input.flow_type) {
537         case RTE_ETH_FLOW_NONFRAG_IPV4_UDP:
538         case RTE_ETH_FLOW_NONFRAG_IPV4_TCP:
539                 input->formatted.src_port =
540                         fdir_filter->input.flow.udp4_flow.src_port;
541                 input->formatted.dst_port =
542                         fdir_filter->input.flow.udp4_flow.dst_port;
543         /*for SCTP flow type, port and verify_tag are meaningless in ixgbe.*/
544         case RTE_ETH_FLOW_NONFRAG_IPV4_SCTP:
545         case RTE_ETH_FLOW_NONFRAG_IPV4_OTHER:
546                 input->formatted.src_ip[0] =
547                         fdir_filter->input.flow.ip4_flow.src_ip;
548                 input->formatted.dst_ip[0] =
549                         fdir_filter->input.flow.ip4_flow.dst_ip;
550                 break;
551
552         case RTE_ETH_FLOW_NONFRAG_IPV6_UDP:
553         case RTE_ETH_FLOW_NONFRAG_IPV6_TCP:
554                 input->formatted.src_port =
555                         fdir_filter->input.flow.udp6_flow.src_port;
556                 input->formatted.dst_port =
557                         fdir_filter->input.flow.udp6_flow.dst_port;
558         /*for SCTP flow type, port and verify_tag are meaningless in ixgbe.*/
559         case RTE_ETH_FLOW_NONFRAG_IPV6_SCTP:
560         case RTE_ETH_FLOW_NONFRAG_IPV6_OTHER:
561                 rte_memcpy(input->formatted.src_ip,
562                            fdir_filter->input.flow.ipv6_flow.src_ip,
563                            sizeof(input->formatted.src_ip));
564                 rte_memcpy(input->formatted.dst_ip,
565                            fdir_filter->input.flow.ipv6_flow.dst_ip,
566                            sizeof(input->formatted.dst_ip));
567                 break;
568         default:
569                 PMD_DRV_LOG(ERR, " Error on flow_type input");
570                 return -EINVAL;
571         }
572
573         return 0;
574 }
575
576 /*
577  * The below function is taken from the FreeBSD IXGBE drivers release
578  * 2.3.8. The only change is not to mask hash_result with IXGBE_ATR_HASH_MASK
579  * before returning, as the signature hash can use 16bits.
580  *
581  * The newer driver has optimised functions for calculating bucket and
582  * signature hashes. However they don't support IPv6 type packets for signature
583  * filters so are not used here.
584  *
585  * Note that the bkt_hash field in the ixgbe_atr_input structure is also never
586  * set.
587  *
588  * Compute the hashes for SW ATR
589  *  @stream: input bitstream to compute the hash on
590  *  @key: 32-bit hash key
591  **/
592 static uint32_t
593 ixgbe_atr_compute_hash_82599(union ixgbe_atr_input *atr_input,
594                                  uint32_t key)
595 {
596         /*
597          * The algorithm is as follows:
598          *    Hash[15:0] = Sum { S[n] x K[n+16] }, n = 0...350
599          *    where Sum {A[n]}, n = 0...n is bitwise XOR of A[0], A[1]...A[n]
600          *    and A[n] x B[n] is bitwise AND between same length strings
601          *
602          *    K[n] is 16 bits, defined as:
603          *       for n modulo 32 >= 15, K[n] = K[n % 32 : (n % 32) - 15]
604          *       for n modulo 32 < 15, K[n] =
605          *             K[(n % 32:0) | (31:31 - (14 - (n % 32)))]
606          *
607          *    S[n] is 16 bits, defined as:
608          *       for n >= 15, S[n] = S[n:n - 15]
609          *       for n < 15, S[n] = S[(n:0) | (350:350 - (14 - n))]
610          *
611          *    To simplify for programming, the algorithm is implemented
612          *    in software this way:
613          *
614          *    key[31:0], hi_hash_dword[31:0], lo_hash_dword[31:0], hash[15:0]
615          *
616          *    for (i = 0; i < 352; i+=32)
617          *        hi_hash_dword[31:0] ^= Stream[(i+31):i];
618          *
619          *    lo_hash_dword[15:0]  ^= Stream[15:0];
620          *    lo_hash_dword[15:0]  ^= hi_hash_dword[31:16];
621          *    lo_hash_dword[31:16] ^= hi_hash_dword[15:0];
622          *
623          *    hi_hash_dword[31:0]  ^= Stream[351:320];
624          *
625          *    if(key[0])
626          *        hash[15:0] ^= Stream[15:0];
627          *
628          *    for (i = 0; i < 16; i++) {
629          *        if (key[i])
630          *            hash[15:0] ^= lo_hash_dword[(i+15):i];
631          *        if (key[i + 16])
632          *            hash[15:0] ^= hi_hash_dword[(i+15):i];
633          *    }
634          *
635          */
636         __be32 common_hash_dword = 0;
637         u32 hi_hash_dword, lo_hash_dword, flow_vm_vlan;
638         u32 hash_result = 0;
639         u8 i;
640
641         /* record the flow_vm_vlan bits as they are a key part to the hash */
642         flow_vm_vlan = IXGBE_NTOHL(atr_input->dword_stream[0]);
643
644         /* generate common hash dword */
645         for (i = 1; i <= 13; i++)
646                 common_hash_dword ^= atr_input->dword_stream[i];
647
648         hi_hash_dword = IXGBE_NTOHL(common_hash_dword);
649
650         /* low dword is word swapped version of common */
651         lo_hash_dword = (hi_hash_dword >> 16) | (hi_hash_dword << 16);
652
653         /* apply flow ID/VM pool/VLAN ID bits to hash words */
654         hi_hash_dword ^= flow_vm_vlan ^ (flow_vm_vlan >> 16);
655
656         /* Process bits 0 and 16 */
657         if (key & 0x0001) hash_result ^= lo_hash_dword;
658         if (key & 0x00010000) hash_result ^= hi_hash_dword;
659
660         /*
661          * apply flow ID/VM pool/VLAN ID bits to lo hash dword, we had to
662          * delay this because bit 0 of the stream should not be processed
663          * so we do not add the vlan until after bit 0 was processed
664          */
665         lo_hash_dword ^= flow_vm_vlan ^ (flow_vm_vlan << 16);
666
667
668         /* process the remaining 30 bits in the key 2 bits at a time */
669         for (i = 15; i; i-- ) {
670                 if (key & (0x0001 << i)) hash_result ^= lo_hash_dword >> i;
671                 if (key & (0x00010000 << i)) hash_result ^= hi_hash_dword >> i;
672         }
673
674         return hash_result;
675 }
676
677 static uint32_t
678 atr_compute_perfect_hash_82599(union ixgbe_atr_input *input,
679                 enum rte_fdir_pballoc_type pballoc)
680 {
681         if (pballoc == RTE_FDIR_PBALLOC_256K)
682                 return ixgbe_atr_compute_hash_82599(input,
683                                 IXGBE_ATR_BUCKET_HASH_KEY) &
684                                 PERFECT_BUCKET_256KB_HASH_MASK;
685         else if (pballoc == RTE_FDIR_PBALLOC_128K)
686                 return ixgbe_atr_compute_hash_82599(input,
687                                 IXGBE_ATR_BUCKET_HASH_KEY) &
688                                 PERFECT_BUCKET_128KB_HASH_MASK;
689         else
690                 return ixgbe_atr_compute_hash_82599(input,
691                                 IXGBE_ATR_BUCKET_HASH_KEY) &
692                                 PERFECT_BUCKET_64KB_HASH_MASK;
693 }
694
695 /**
696  * ixgbe_fdir_check_cmd_complete - poll to check whether FDIRCMD is complete
697  * @hw: pointer to hardware structure
698  */
699 static inline int
700 ixgbe_fdir_check_cmd_complete(struct ixgbe_hw *hw, uint32_t *fdircmd)
701 {
702         int i;
703
704         for (i = 0; i < IXGBE_FDIRCMD_CMD_POLL; i++) {
705                 *fdircmd = IXGBE_READ_REG(hw, IXGBE_FDIRCMD);
706                 if (!(*fdircmd & IXGBE_FDIRCMD_CMD_MASK))
707                         return 0;
708                 rte_delay_us(IXGBE_FDIRCMD_CMD_INTERVAL_US);
709         }
710
711         return -ETIMEDOUT;
712 }
713
714 /*
715  * Calculate the hash value needed for signature-match filters. In the FreeBSD
716  * driver, this is done by the optimised function
717  * ixgbe_atr_compute_sig_hash_82599(). However that can't be used here as it
718  * doesn't support calculating a hash for an IPv6 filter.
719  */
720 static uint32_t
721 atr_compute_sig_hash_82599(union ixgbe_atr_input *input,
722                 enum rte_fdir_pballoc_type pballoc)
723 {
724         uint32_t bucket_hash, sig_hash;
725
726         if (pballoc == RTE_FDIR_PBALLOC_256K)
727                 bucket_hash = ixgbe_atr_compute_hash_82599(input,
728                                 IXGBE_ATR_BUCKET_HASH_KEY) &
729                                 SIG_BUCKET_256KB_HASH_MASK;
730         else if (pballoc == RTE_FDIR_PBALLOC_128K)
731                 bucket_hash = ixgbe_atr_compute_hash_82599(input,
732                                 IXGBE_ATR_BUCKET_HASH_KEY) &
733                                 SIG_BUCKET_128KB_HASH_MASK;
734         else
735                 bucket_hash = ixgbe_atr_compute_hash_82599(input,
736                                 IXGBE_ATR_BUCKET_HASH_KEY) &
737                                 SIG_BUCKET_64KB_HASH_MASK;
738
739         sig_hash = ixgbe_atr_compute_hash_82599(input,
740                         IXGBE_ATR_SIGNATURE_HASH_KEY);
741
742         return (sig_hash << IXGBE_FDIRHASH_SIG_SW_INDEX_SHIFT) | bucket_hash;
743 }
744
745 /*
746  * This is based on ixgbe_fdir_write_perfect_filter_82599() in
747  * base/ixgbe_82599.c, with the ability to set extra flags in FDIRCMD register
748  * added, and IPv6 support also added. The hash value is also pre-calculated
749  * as the pballoc value is needed to do it.
750  */
751 static int
752 fdir_write_perfect_filter_82599(struct ixgbe_hw *hw,
753                         union ixgbe_atr_input *input, uint8_t queue,
754                         uint32_t fdircmd, uint32_t fdirhash)
755 {
756         uint32_t fdirport, fdirvlan;
757         int err = 0;
758
759         /* record the IPv4 address (big-endian) */
760         IXGBE_WRITE_REG(hw, IXGBE_FDIRIPSA, input->formatted.src_ip[0]);
761         IXGBE_WRITE_REG(hw, IXGBE_FDIRIPDA, input->formatted.dst_ip[0]);
762
763         /* record source and destination port (little-endian)*/
764         fdirport = IXGBE_NTOHS(input->formatted.dst_port);
765         fdirport <<= IXGBE_FDIRPORT_DESTINATION_SHIFT;
766         fdirport |= IXGBE_NTOHS(input->formatted.src_port);
767         IXGBE_WRITE_REG(hw, IXGBE_FDIRPORT, fdirport);
768
769         /* record vlan (little-endian) and flex_bytes(big-endian) */
770         fdirvlan = input->formatted.flex_bytes;
771         fdirvlan <<= IXGBE_FDIRVLAN_FLEX_SHIFT;
772         fdirvlan |= IXGBE_NTOHS(input->formatted.vlan_id);
773         IXGBE_WRITE_REG(hw, IXGBE_FDIRVLAN, fdirvlan);
774
775         /* configure FDIRHASH register */
776         IXGBE_WRITE_REG(hw, IXGBE_FDIRHASH, fdirhash);
777
778         /*
779          * flush all previous writes to make certain registers are
780          * programmed prior to issuing the command
781          */
782         IXGBE_WRITE_FLUSH(hw);
783
784         /* configure FDIRCMD register */
785         fdircmd |= IXGBE_FDIRCMD_CMD_ADD_FLOW |
786                   IXGBE_FDIRCMD_LAST | IXGBE_FDIRCMD_QUEUE_EN;
787         fdircmd |= input->formatted.flow_type << IXGBE_FDIRCMD_FLOW_TYPE_SHIFT;
788         fdircmd |= (uint32_t)queue << IXGBE_FDIRCMD_RX_QUEUE_SHIFT;
789         fdircmd |= (uint32_t)input->formatted.vm_pool << IXGBE_FDIRCMD_VT_POOL_SHIFT;
790
791         IXGBE_WRITE_REG(hw, IXGBE_FDIRCMD, fdircmd);
792
793         PMD_DRV_LOG(DEBUG, "Rx Queue=%x hash=%x", queue, fdirhash);
794
795         err = ixgbe_fdir_check_cmd_complete(hw, &fdircmd);
796         if (err < 0)
797                 PMD_DRV_LOG(ERR, "Timeout writing flow director filter.");
798
799         return err;
800 }
801
802 /**
803  * This function is based on ixgbe_atr_add_signature_filter_82599() in
804  * base/ixgbe_82599.c, but uses a pre-calculated hash value. It also supports
805  * setting extra fields in the FDIRCMD register, and removes the code that was
806  * verifying the flow_type field. According to the documentation, a flow type of
807  * 00 (i.e. not TCP, UDP, or SCTP) is not supported, however it appears to
808  * work ok...
809  *
810  *  Adds a signature hash filter
811  *  @hw: pointer to hardware structure
812  *  @input: unique input dword
813  *  @queue: queue index to direct traffic to
814  *  @fdircmd: any extra flags to set in fdircmd register
815  *  @fdirhash: pre-calculated hash value for the filter
816  **/
817 static int
818 fdir_add_signature_filter_82599(struct ixgbe_hw *hw,
819                 union ixgbe_atr_input *input, u8 queue, uint32_t fdircmd,
820                 uint32_t fdirhash)
821 {
822         int err = 0;
823
824         PMD_INIT_FUNC_TRACE();
825
826         /* configure FDIRCMD register */
827         fdircmd |= IXGBE_FDIRCMD_CMD_ADD_FLOW |
828                   IXGBE_FDIRCMD_LAST | IXGBE_FDIRCMD_QUEUE_EN;
829         fdircmd |= input->formatted.flow_type << IXGBE_FDIRCMD_FLOW_TYPE_SHIFT;
830         fdircmd |= (uint32_t)queue << IXGBE_FDIRCMD_RX_QUEUE_SHIFT;
831
832         IXGBE_WRITE_REG(hw, IXGBE_FDIRHASH, fdirhash);
833         IXGBE_WRITE_REG(hw, IXGBE_FDIRCMD, fdircmd);
834
835         PMD_DRV_LOG(DEBUG, "Rx Queue=%x hash=%x", queue, fdirhash);
836
837         err = ixgbe_fdir_check_cmd_complete(hw, &fdircmd);
838         if (err < 0)
839                 PMD_DRV_LOG(ERR, "Timeout writing flow director filter.");
840
841         return err;
842 }
843
844 /*
845  * This is based on ixgbe_fdir_erase_perfect_filter_82599() in
846  * base/ixgbe_82599.c. It is modified to take in the hash as a parameter so
847  * that it can be used for removing signature and perfect filters.
848  */
849 static int
850 fdir_erase_filter_82599(struct ixgbe_hw *hw, uint32_t fdirhash)
851 {
852         uint32_t fdircmd = 0;
853         int err = 0;
854
855         IXGBE_WRITE_REG(hw, IXGBE_FDIRHASH, fdirhash);
856
857         /* flush hash to HW */
858         IXGBE_WRITE_FLUSH(hw);
859
860         /* Query if filter is present */
861         IXGBE_WRITE_REG(hw, IXGBE_FDIRCMD, IXGBE_FDIRCMD_CMD_QUERY_REM_FILT);
862
863         err = ixgbe_fdir_check_cmd_complete(hw, &fdircmd);
864         if (err < 0) {
865                 PMD_INIT_LOG(ERR, "Timeout querying for flow director filter.");
866                 return err;
867         }
868
869         /* if filter exists in hardware then remove it */
870         if (fdircmd & IXGBE_FDIRCMD_FILTER_VALID) {
871                 IXGBE_WRITE_REG(hw, IXGBE_FDIRHASH, fdirhash);
872                 IXGBE_WRITE_FLUSH(hw);
873                 IXGBE_WRITE_REG(hw, IXGBE_FDIRCMD,
874                                 IXGBE_FDIRCMD_CMD_REMOVE_FLOW);
875         }
876         err = ixgbe_fdir_check_cmd_complete(hw, &fdircmd);
877         if (err < 0)
878                 PMD_INIT_LOG(ERR, "Timeout erasing flow director filter.");
879         return err;
880
881 }
882
883 /*
884  * ixgbe_add_del_fdir_filter - add or remove a flow diretor filter.
885  * @dev: pointer to the structure rte_eth_dev
886  * @fdir_filter: fdir filter entry
887  * @del: 1 - delete, 0 - add
888  * @update: 1 - update
889  */
890 static int
891 ixgbe_add_del_fdir_filter(struct rte_eth_dev *dev,
892                               const struct rte_eth_fdir_filter *fdir_filter,
893                               bool del,
894                               bool update)
895 {
896         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
897         uint32_t fdircmd_flags;
898         uint32_t fdirhash;
899         union ixgbe_atr_input input;
900         uint8_t queue;
901         bool is_perfect = FALSE;
902         int err;
903         struct ixgbe_hw_fdir_info *info =
904                         IXGBE_DEV_PRIVATE_TO_FDIR_INFO(dev->data->dev_private);
905
906         if (dev->data->dev_conf.fdir_conf.mode == RTE_FDIR_MODE_NONE)
907                 return -ENOTSUP;
908
909         /*
910          * Sanity check for x550.
911          * When adding a new filter with flow type set to IPv4-other,
912          * the flow director mask should be configed before,
913          * and the L4 protocol and ports are masked.
914          */
915         if ((!del) &&
916             (hw->mac.type == ixgbe_mac_X550 ||
917              hw->mac.type == ixgbe_mac_X550EM_x) &&
918             (fdir_filter->input.flow_type ==
919                RTE_ETH_FLOW_NONFRAG_IPV4_OTHER) &&
920             (info->mask.src_port_mask != 0 ||
921              info->mask.dst_port_mask != 0)) {
922                 PMD_DRV_LOG(ERR, "By this device,"
923                                  " IPv4-other is not supported without"
924                                  " L4 protocol and ports masked!");
925                 return -ENOTSUP;
926         }
927
928         if (dev->data->dev_conf.fdir_conf.mode == RTE_FDIR_MODE_PERFECT)
929                 is_perfect = TRUE;
930
931         memset(&input, 0, sizeof(input));
932
933         err = ixgbe_fdir_filter_to_atr_input(fdir_filter, &input);
934         if (err)
935                 return err;
936
937         if (is_perfect) {
938                 if (input.formatted.flow_type & IXGBE_ATR_L4TYPE_IPV6_MASK) {
939                         PMD_DRV_LOG(ERR, "IPv6 is not supported in"
940                                          " perfect mode!");
941                         return -ENOTSUP;
942                 }
943                 fdirhash = atr_compute_perfect_hash_82599(&input,
944                                 dev->data->dev_conf.fdir_conf.pballoc);
945                 fdirhash |= fdir_filter->soft_id <<
946                                 IXGBE_FDIRHASH_SIG_SW_INDEX_SHIFT;
947         } else
948                 fdirhash = atr_compute_sig_hash_82599(&input,
949                                 dev->data->dev_conf.fdir_conf.pballoc);
950
951         if (del) {
952                 err = fdir_erase_filter_82599(hw, fdirhash);
953                 if (err < 0)
954                         PMD_DRV_LOG(ERR, "Fail to delete FDIR filter!");
955                 else
956                         PMD_DRV_LOG(DEBUG, "Success to delete FDIR filter!");
957                 return err;
958         }
959         /* add or update an fdir filter*/
960         fdircmd_flags = (update) ? IXGBE_FDIRCMD_FILTER_UPDATE : 0;
961         if (fdir_filter->action.behavior == RTE_ETH_FDIR_REJECT) {
962                 if (is_perfect) {
963                         queue = dev->data->dev_conf.fdir_conf.drop_queue;
964                         fdircmd_flags |= IXGBE_FDIRCMD_DROP;
965                 } else {
966                         PMD_DRV_LOG(ERR, "Drop option is not supported in"
967                                 " signature mode.");
968                         return -EINVAL;
969                 }
970         } else if (fdir_filter->action.rx_queue < IXGBE_MAX_RX_QUEUE_NUM)
971                 queue = (uint8_t)fdir_filter->action.rx_queue;
972         else
973                 return -EINVAL;
974
975         if (is_perfect) {
976                 err = fdir_write_perfect_filter_82599(hw, &input, queue,
977                                 fdircmd_flags, fdirhash);
978         } else {
979                 err = fdir_add_signature_filter_82599(hw, &input, queue,
980                                 fdircmd_flags, fdirhash);
981         }
982         if (err < 0)
983                 PMD_DRV_LOG(ERR, "Fail to add FDIR filter!");
984         else
985                 PMD_DRV_LOG(DEBUG, "Success to add FDIR filter");
986
987         return err;
988 }
989
990 static int
991 ixgbe_fdir_flush(struct rte_eth_dev *dev)
992 {
993         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
994         struct ixgbe_hw_fdir_info *info =
995                         IXGBE_DEV_PRIVATE_TO_FDIR_INFO(dev->data->dev_private);
996         int ret;
997
998         ret = ixgbe_reinit_fdir_tables_82599(hw);
999         if (ret < 0) {
1000                 PMD_INIT_LOG(ERR, "Failed to re-initialize FD table.");
1001                 return ret;
1002         }
1003
1004         info->f_add = 0;
1005         info->f_remove = 0;
1006         info->add = 0;
1007         info->remove = 0;
1008
1009         return ret;
1010 }
1011
1012 #define FDIRENTRIES_NUM_SHIFT 10
1013 static void
1014 ixgbe_fdir_info_get(struct rte_eth_dev *dev, struct rte_eth_fdir_info *fdir_info)
1015 {
1016         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1017         struct ixgbe_hw_fdir_info *info =
1018                         IXGBE_DEV_PRIVATE_TO_FDIR_INFO(dev->data->dev_private);
1019         uint32_t fdirctrl, max_num;
1020         uint8_t offset;
1021
1022         fdirctrl = IXGBE_READ_REG(hw, IXGBE_FDIRCTRL);
1023         offset = ((fdirctrl & IXGBE_FDIRCTRL_FLEX_MASK) >>
1024                         IXGBE_FDIRCTRL_FLEX_SHIFT) * sizeof(uint16_t);
1025
1026         fdir_info->mode = dev->data->dev_conf.fdir_conf.mode;
1027         max_num = (1 << (FDIRENTRIES_NUM_SHIFT +
1028                         (fdirctrl & FDIRCTRL_PBALLOC_MASK)));
1029         if (fdir_info->mode == RTE_FDIR_MODE_PERFECT)
1030                 fdir_info->guarant_spc = max_num;
1031         else if (fdir_info->mode == RTE_FDIR_MODE_SIGNATURE)
1032                 fdir_info->guarant_spc = max_num * 4;
1033
1034         fdir_info->mask.vlan_tci_mask = info->mask.vlan_tci_mask;
1035         fdir_info->mask.ipv4_mask.src_ip = info->mask.src_ipv4_mask;
1036         fdir_info->mask.ipv4_mask.dst_ip = info->mask.dst_ipv4_mask;
1037         IPV6_MASK_TO_ADDR(info->mask.src_ipv6_mask,
1038                         fdir_info->mask.ipv6_mask.src_ip);
1039         IPV6_MASK_TO_ADDR(info->mask.dst_ipv6_mask,
1040                         fdir_info->mask.ipv6_mask.dst_ip);
1041         fdir_info->mask.src_port_mask = info->mask.src_port_mask;
1042         fdir_info->mask.dst_port_mask = info->mask.dst_port_mask;
1043         fdir_info->max_flexpayload = IXGBE_FDIR_MAX_FLEX_LEN;
1044         fdir_info->flow_types_mask[0] = IXGBE_FDIR_FLOW_TYPES;
1045         fdir_info->flex_payload_unit = sizeof(uint16_t);
1046         fdir_info->max_flex_payload_segment_num = 1;
1047         fdir_info->flex_payload_limit = 62;
1048         fdir_info->flex_conf.nb_payloads = 1;
1049         fdir_info->flex_conf.flex_set[0].type = RTE_ETH_RAW_PAYLOAD;
1050         fdir_info->flex_conf.flex_set[0].src_offset[0] = offset;
1051         fdir_info->flex_conf.flex_set[0].src_offset[1] = offset + 1;
1052         fdir_info->flex_conf.nb_flexmasks = 1;
1053         fdir_info->flex_conf.flex_mask[0].flow_type = RTE_ETH_FLOW_UNKNOWN;
1054         fdir_info->flex_conf.flex_mask[0].mask[0] =
1055                         (uint8_t)(info->mask.flex_bytes_mask & 0x00FF);
1056         fdir_info->flex_conf.flex_mask[0].mask[1] =
1057                         (uint8_t)((info->mask.flex_bytes_mask & 0xFF00) >> 8);
1058 }
1059
1060 static void
1061 ixgbe_fdir_stats_get(struct rte_eth_dev *dev, struct rte_eth_fdir_stats *fdir_stats)
1062 {
1063         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1064         struct ixgbe_hw_fdir_info *info =
1065                         IXGBE_DEV_PRIVATE_TO_FDIR_INFO(dev->data->dev_private);
1066         uint32_t reg, max_num;
1067
1068         /* Get the information from registers */
1069         reg = IXGBE_READ_REG(hw, IXGBE_FDIRFREE);
1070         info->collision = (uint16_t)((reg & IXGBE_FDIRFREE_COLL_MASK) >>
1071                                         IXGBE_FDIRFREE_COLL_SHIFT);
1072         info->free = (uint16_t)((reg & IXGBE_FDIRFREE_FREE_MASK) >>
1073                                    IXGBE_FDIRFREE_FREE_SHIFT);
1074
1075         reg = IXGBE_READ_REG(hw, IXGBE_FDIRLEN);
1076         info->maxhash = (uint16_t)((reg & IXGBE_FDIRLEN_MAXHASH_MASK) >>
1077                                       IXGBE_FDIRLEN_MAXHASH_SHIFT);
1078         info->maxlen  = (uint8_t)((reg & IXGBE_FDIRLEN_MAXLEN_MASK) >>
1079                                      IXGBE_FDIRLEN_MAXLEN_SHIFT);
1080
1081         reg = IXGBE_READ_REG(hw, IXGBE_FDIRUSTAT);
1082         info->remove += (reg & IXGBE_FDIRUSTAT_REMOVE_MASK) >>
1083                 IXGBE_FDIRUSTAT_REMOVE_SHIFT;
1084         info->add += (reg & IXGBE_FDIRUSTAT_ADD_MASK) >>
1085                 IXGBE_FDIRUSTAT_ADD_SHIFT;
1086
1087         reg = IXGBE_READ_REG(hw, IXGBE_FDIRFSTAT) & 0xFFFF;
1088         info->f_remove += (reg & IXGBE_FDIRFSTAT_FREMOVE_MASK) >>
1089                 IXGBE_FDIRFSTAT_FREMOVE_SHIFT;
1090         info->f_add += (reg & IXGBE_FDIRFSTAT_FADD_MASK) >>
1091                 IXGBE_FDIRFSTAT_FADD_SHIFT;
1092
1093         /*  Copy the new information in the fdir parameter */
1094         fdir_stats->collision = info->collision;
1095         fdir_stats->free = info->free;
1096         fdir_stats->maxhash = info->maxhash;
1097         fdir_stats->maxlen = info->maxlen;
1098         fdir_stats->remove = info->remove;
1099         fdir_stats->add = info->add;
1100         fdir_stats->f_remove = info->f_remove;
1101         fdir_stats->f_add = info->f_add;
1102
1103         reg = IXGBE_READ_REG(hw, IXGBE_FDIRCTRL);
1104         max_num = (1 << (FDIRENTRIES_NUM_SHIFT +
1105                         (reg & FDIRCTRL_PBALLOC_MASK)));
1106         if (dev->data->dev_conf.fdir_conf.mode == RTE_FDIR_MODE_PERFECT)
1107                         fdir_stats->guarant_cnt = max_num - fdir_stats->free;
1108         else if (dev->data->dev_conf.fdir_conf.mode == RTE_FDIR_MODE_SIGNATURE)
1109                 fdir_stats->guarant_cnt = max_num * 4 - fdir_stats->free;
1110
1111 }
1112
1113 /*
1114  * ixgbe_fdir_ctrl_func - deal with all operations on flow director.
1115  * @dev: pointer to the structure rte_eth_dev
1116  * @filter_op:operation will be taken
1117  * @arg: a pointer to specific structure corresponding to the filter_op
1118  */
1119 int
1120 ixgbe_fdir_ctrl_func(struct rte_eth_dev *dev,
1121                         enum rte_filter_op filter_op, void *arg)
1122 {
1123         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1124         int ret = 0;
1125
1126         if (hw->mac.type != ixgbe_mac_82599EB &&
1127                 hw->mac.type != ixgbe_mac_X540 &&
1128                 hw->mac.type != ixgbe_mac_X550 &&
1129                 hw->mac.type != ixgbe_mac_X550EM_x)
1130                 return -ENOTSUP;
1131
1132         if (filter_op == RTE_ETH_FILTER_NOP)
1133                 return 0;
1134
1135         if (arg == NULL && filter_op != RTE_ETH_FILTER_FLUSH)
1136                 return -EINVAL;
1137
1138         switch (filter_op) {
1139         case RTE_ETH_FILTER_ADD:
1140                 ret = ixgbe_add_del_fdir_filter(dev,
1141                         (struct rte_eth_fdir_filter *)arg, FALSE, FALSE);
1142                 break;
1143         case RTE_ETH_FILTER_UPDATE:
1144                 ret = ixgbe_add_del_fdir_filter(dev,
1145                         (struct rte_eth_fdir_filter *)arg, FALSE, TRUE);
1146                 break;
1147         case RTE_ETH_FILTER_DELETE:
1148                 ret = ixgbe_add_del_fdir_filter(dev,
1149                         (struct rte_eth_fdir_filter *)arg, TRUE, FALSE);
1150                 break;
1151         case RTE_ETH_FILTER_FLUSH:
1152                 ret = ixgbe_fdir_flush(dev);
1153                 break;
1154         case RTE_ETH_FILTER_INFO:
1155                 ixgbe_fdir_info_get(dev, (struct rte_eth_fdir_info *)arg);
1156                 break;
1157         case RTE_ETH_FILTER_STATS:
1158                 ixgbe_fdir_stats_get(dev, (struct rte_eth_fdir_stats *)arg);
1159                 break;
1160         default:
1161                 PMD_DRV_LOG(ERR, "unknown operation %u", filter_op);
1162                 ret = -EINVAL;
1163                 break;
1164         }
1165         return ret;
1166 }