ixgbe: check for supported flow director behaviors
[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 #define DEFAULT_VXLAN_PORT 4789
109 #define IXGBE_FDIRIP6M_INNER_MAC_SHIFT 4
110
111 static int fdir_erase_filter_82599(struct ixgbe_hw *hw, uint32_t fdirhash);
112 static int fdir_set_input_mask(struct rte_eth_dev *dev,
113                                const struct rte_eth_fdir_masks *input_mask);
114 static int fdir_set_input_mask_82599(struct rte_eth_dev *dev,
115                 const struct rte_eth_fdir_masks *input_mask);
116 static int fdir_set_input_mask_x550(struct rte_eth_dev *dev,
117                                     const struct rte_eth_fdir_masks *input_mask);
118 static int ixgbe_set_fdir_flex_conf(struct rte_eth_dev *dev,
119                 const struct rte_eth_fdir_flex_conf *conf, uint32_t *fdirctrl);
120 static int fdir_enable_82599(struct ixgbe_hw *hw, uint32_t fdirctrl);
121 static int ixgbe_fdir_filter_to_atr_input(
122                 const struct rte_eth_fdir_filter *fdir_filter,
123                 union ixgbe_atr_input *input,
124                 enum rte_fdir_mode mode);
125 static uint32_t ixgbe_atr_compute_hash_82599(union ixgbe_atr_input *atr_input,
126                                  uint32_t key);
127 static uint32_t atr_compute_sig_hash_82599(union ixgbe_atr_input *input,
128                 enum rte_fdir_pballoc_type pballoc);
129 static uint32_t atr_compute_perfect_hash_82599(union ixgbe_atr_input *input,
130                 enum rte_fdir_pballoc_type pballoc);
131 static int fdir_write_perfect_filter_82599(struct ixgbe_hw *hw,
132                         union ixgbe_atr_input *input, uint8_t queue,
133                         uint32_t fdircmd, uint32_t fdirhash,
134                         enum rte_fdir_mode mode);
135 static int fdir_add_signature_filter_82599(struct ixgbe_hw *hw,
136                 union ixgbe_atr_input *input, u8 queue, uint32_t fdircmd,
137                 uint32_t fdirhash);
138 static int ixgbe_add_del_fdir_filter(struct rte_eth_dev *dev,
139                               const struct rte_eth_fdir_filter *fdir_filter,
140                               bool del,
141                               bool update);
142 static int ixgbe_fdir_flush(struct rte_eth_dev *dev);
143 static void ixgbe_fdir_info_get(struct rte_eth_dev *dev,
144                         struct rte_eth_fdir_info *fdir_info);
145 static void ixgbe_fdir_stats_get(struct rte_eth_dev *dev,
146                         struct rte_eth_fdir_stats *fdir_stats);
147
148 /**
149  * This function is based on ixgbe_fdir_enable_82599() in base/ixgbe_82599.c.
150  * It adds extra configuration of fdirctrl that is common for all filter types.
151  *
152  *  Initialize Flow Director control registers
153  *  @hw: pointer to hardware structure
154  *  @fdirctrl: value to write to flow director control register
155  **/
156 static int
157 fdir_enable_82599(struct ixgbe_hw *hw, uint32_t fdirctrl)
158 {
159         int i;
160
161         PMD_INIT_FUNC_TRACE();
162
163         /* Prime the keys for hashing */
164         IXGBE_WRITE_REG(hw, IXGBE_FDIRHKEY, IXGBE_ATR_BUCKET_HASH_KEY);
165         IXGBE_WRITE_REG(hw, IXGBE_FDIRSKEY, IXGBE_ATR_SIGNATURE_HASH_KEY);
166
167         /*
168          * Continue setup of fdirctrl register bits:
169          *  Set the maximum length per hash bucket to 0xA filters
170          *  Send interrupt when 64 filters are left
171          */
172         fdirctrl |= (0xA << IXGBE_FDIRCTRL_MAX_LENGTH_SHIFT) |
173                     (4 << IXGBE_FDIRCTRL_FULL_THRESH_SHIFT);
174
175         /*
176          * Poll init-done after we write the register.  Estimated times:
177          *      10G: PBALLOC = 11b, timing is 60us
178          *       1G: PBALLOC = 11b, timing is 600us
179          *     100M: PBALLOC = 11b, timing is 6ms
180          *
181          *     Multiple these timings by 4 if under full Rx load
182          *
183          * So we'll poll for IXGBE_FDIR_INIT_DONE_POLL times, sleeping for
184          * 1 msec per poll time.  If we're at line rate and drop to 100M, then
185          * this might not finish in our poll time, but we can live with that
186          * for now.
187          */
188         IXGBE_WRITE_REG(hw, IXGBE_FDIRCTRL, fdirctrl);
189         IXGBE_WRITE_FLUSH(hw);
190         for (i = 0; i < IXGBE_FDIR_INIT_DONE_POLL; i++) {
191                 if (IXGBE_READ_REG(hw, IXGBE_FDIRCTRL) &
192                                    IXGBE_FDIRCTRL_INIT_DONE)
193                         break;
194                 msec_delay(1);
195         }
196
197         if (i >= IXGBE_FDIR_INIT_DONE_POLL) {
198                 PMD_INIT_LOG(ERR, "Flow Director poll time exceeded "
199                         "during enabling!");
200                 return -ETIMEDOUT;
201         }
202         return 0;
203 }
204
205 /*
206  * Set appropriate bits in fdirctrl for: variable reporting levels, moving
207  * flexbytes matching field, and drop queue (only for perfect matching mode).
208  */
209 static inline int
210 configure_fdir_flags(const struct rte_fdir_conf *conf, uint32_t *fdirctrl)
211 {
212         *fdirctrl = 0;
213
214         switch (conf->pballoc) {
215         case RTE_FDIR_PBALLOC_64K:
216                 /* 8k - 1 signature filters */
217                 *fdirctrl |= IXGBE_FDIRCTRL_PBALLOC_64K;
218                 break;
219         case RTE_FDIR_PBALLOC_128K:
220                 /* 16k - 1 signature filters */
221                 *fdirctrl |= IXGBE_FDIRCTRL_PBALLOC_128K;
222                 break;
223         case RTE_FDIR_PBALLOC_256K:
224                 /* 32k - 1 signature filters */
225                 *fdirctrl |= IXGBE_FDIRCTRL_PBALLOC_256K;
226                 break;
227         default:
228                 /* bad value */
229                 PMD_INIT_LOG(ERR, "Invalid fdir_conf->pballoc value");
230                 return -EINVAL;
231         };
232
233         /* status flags: write hash & swindex in the rx descriptor */
234         switch (conf->status) {
235         case RTE_FDIR_NO_REPORT_STATUS:
236                 /* do nothing, default mode */
237                 break;
238         case RTE_FDIR_REPORT_STATUS:
239                 /* report status when the packet matches a fdir rule */
240                 *fdirctrl |= IXGBE_FDIRCTRL_REPORT_STATUS;
241                 break;
242         case RTE_FDIR_REPORT_STATUS_ALWAYS:
243                 /* always report status */
244                 *fdirctrl |= IXGBE_FDIRCTRL_REPORT_STATUS_ALWAYS;
245                 break;
246         default:
247                 /* bad value */
248                 PMD_INIT_LOG(ERR, "Invalid fdir_conf->status value");
249                 return -EINVAL;
250         };
251
252         *fdirctrl |= (IXGBE_DEFAULT_FLEXBYTES_OFFSET / sizeof(uint16_t)) <<
253                      IXGBE_FDIRCTRL_FLEX_SHIFT;
254
255         if (conf->mode >= RTE_FDIR_MODE_PERFECT &&
256             conf->mode <= RTE_FDIR_MODE_PERFECT_TUNNEL) {
257                 *fdirctrl |= IXGBE_FDIRCTRL_PERFECT_MATCH;
258                 *fdirctrl |= (conf->drop_queue << IXGBE_FDIRCTRL_DROP_Q_SHIFT);
259                 if (conf->mode == RTE_FDIR_MODE_PERFECT_MAC_VLAN)
260                         *fdirctrl |= (IXGBE_FDIRCTRL_FILTERMODE_MACVLAN
261                                         << IXGBE_FDIRCTRL_FILTERMODE_SHIFT);
262                 else if (conf->mode == RTE_FDIR_MODE_PERFECT_TUNNEL)
263                         *fdirctrl |= (IXGBE_FDIRCTRL_FILTERMODE_CLOUD
264                                         << IXGBE_FDIRCTRL_FILTERMODE_SHIFT);
265         }
266
267         return 0;
268 }
269
270 /**
271  * Reverse the bits in FDIR registers that store 2 x 16 bit masks.
272  *
273  *  @hi_dword: Bits 31:16 mask to be bit swapped.
274  *  @lo_dword: Bits 15:0  mask to be bit swapped.
275  *
276  *  Flow director uses several registers to store 2 x 16 bit masks with the
277  *  bits reversed such as FDIRTCPM, FDIRUDPM. The LS bit of the
278  *  mask affects the MS bit/byte of the target. This function reverses the
279  *  bits in these masks.
280  *  **/
281 static inline uint32_t
282 reverse_fdir_bitmasks(uint16_t hi_dword, uint16_t lo_dword)
283 {
284         uint32_t mask = hi_dword << 16;
285         mask |= lo_dword;
286         mask = ((mask & 0x55555555) << 1) | ((mask & 0xAAAAAAAA) >> 1);
287         mask = ((mask & 0x33333333) << 2) | ((mask & 0xCCCCCCCC) >> 2);
288         mask = ((mask & 0x0F0F0F0F) << 4) | ((mask & 0xF0F0F0F0) >> 4);
289         return ((mask & 0x00FF00FF) << 8) | ((mask & 0xFF00FF00) >> 8);
290 }
291
292 /*
293  * This references ixgbe_fdir_set_input_mask_82599() in base/ixgbe_82599.c,
294  * but makes use of the rte_fdir_masks structure to see which bits to set.
295  */
296 static int
297 fdir_set_input_mask_82599(struct rte_eth_dev *dev,
298                 const struct rte_eth_fdir_masks *input_mask)
299 {
300         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
301         struct ixgbe_hw_fdir_info *info =
302                         IXGBE_DEV_PRIVATE_TO_FDIR_INFO(dev->data->dev_private);
303         /*
304          * mask VM pool and DIPv6 since there are currently not supported
305          * mask FLEX byte, it will be set in flex_conf
306          */
307         uint32_t fdirm = IXGBE_FDIRM_POOL | IXGBE_FDIRM_DIPv6 | IXGBE_FDIRM_FLEX;
308         uint32_t fdirtcpm;  /* TCP source and destination port masks. */
309         uint32_t fdiripv6m; /* IPv6 source and destination masks. */
310         uint16_t dst_ipv6m = 0;
311         uint16_t src_ipv6m = 0;
312
313         PMD_INIT_FUNC_TRACE();
314
315         /*
316          * Program the relevant mask registers.  If src/dst_port or src/dst_addr
317          * are zero, then assume a full mask for that field. Also assume that
318          * a VLAN of 0 is unspecified, so mask that out as well.  L4type
319          * cannot be masked out in this implementation.
320          */
321         if (input_mask->dst_port_mask == 0 && input_mask->src_port_mask == 0)
322                 /* use the L4 protocol mask for raw IPv4/IPv6 traffic */
323                 fdirm |= IXGBE_FDIRM_L4P;
324
325         if (input_mask->vlan_tci_mask == 0x0FFF)
326                 /* mask VLAN Priority */
327                 fdirm |= IXGBE_FDIRM_VLANP;
328         else if (input_mask->vlan_tci_mask == 0xE000)
329                 /* mask VLAN ID */
330                 fdirm |= IXGBE_FDIRM_VLANID;
331         else if (input_mask->vlan_tci_mask == 0)
332                 /* mask VLAN ID and Priority */
333                 fdirm |= IXGBE_FDIRM_VLANID | IXGBE_FDIRM_VLANP;
334         else if (input_mask->vlan_tci_mask != 0xEFFF) {
335                 PMD_INIT_LOG(ERR, "invalid vlan_tci_mask");
336                 return -EINVAL;
337         }
338         info->mask.vlan_tci_mask = input_mask->vlan_tci_mask;
339
340         IXGBE_WRITE_REG(hw, IXGBE_FDIRM, fdirm);
341
342         /* store the TCP/UDP port masks, bit reversed from port layout */
343         fdirtcpm = reverse_fdir_bitmasks(input_mask->dst_port_mask,
344                                          input_mask->src_port_mask);
345
346         /* write all the same so that UDP, TCP and SCTP use the same mask */
347         IXGBE_WRITE_REG(hw, IXGBE_FDIRTCPM, ~fdirtcpm);
348         IXGBE_WRITE_REG(hw, IXGBE_FDIRUDPM, ~fdirtcpm);
349         IXGBE_WRITE_REG(hw, IXGBE_FDIRSCTPM, ~fdirtcpm);
350         info->mask.src_port_mask = input_mask->src_port_mask;
351         info->mask.dst_port_mask = input_mask->dst_port_mask;
352
353         /* Store source and destination IPv4 masks (big-endian) */
354         IXGBE_WRITE_REG(hw, IXGBE_FDIRSIP4M, ~(input_mask->ipv4_mask.src_ip));
355         IXGBE_WRITE_REG(hw, IXGBE_FDIRDIP4M, ~(input_mask->ipv4_mask.dst_ip));
356         info->mask.src_ipv4_mask = input_mask->ipv4_mask.src_ip;
357         info->mask.dst_ipv4_mask = input_mask->ipv4_mask.dst_ip;
358
359         if (dev->data->dev_conf.fdir_conf.mode == RTE_FDIR_MODE_SIGNATURE) {
360                 /*
361                  * Store source and destination IPv6 masks (bit reversed)
362                  */
363                 IPV6_ADDR_TO_MASK(input_mask->ipv6_mask.src_ip, src_ipv6m);
364                 IPV6_ADDR_TO_MASK(input_mask->ipv6_mask.dst_ip, dst_ipv6m);
365                 fdiripv6m = (dst_ipv6m << 16) | src_ipv6m;
366
367                 IXGBE_WRITE_REG(hw, IXGBE_FDIRIP6M, ~fdiripv6m);
368                 info->mask.src_ipv6_mask = src_ipv6m;
369                 info->mask.dst_ipv6_mask = dst_ipv6m;
370         }
371
372         return IXGBE_SUCCESS;
373 }
374
375 /*
376  * This references ixgbe_fdir_set_input_mask_82599() in base/ixgbe_82599.c,
377  * but makes use of the rte_fdir_masks structure to see which bits to set.
378  */
379 static int
380 fdir_set_input_mask_x550(struct rte_eth_dev *dev,
381                          const struct rte_eth_fdir_masks *input_mask)
382 {
383         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
384         struct ixgbe_hw_fdir_info *info =
385                         IXGBE_DEV_PRIVATE_TO_FDIR_INFO(dev->data->dev_private);
386         /* mask VM pool and DIPv6 since there are currently not supported
387          * mask FLEX byte, it will be set in flex_conf
388          */
389         uint32_t fdirm = IXGBE_FDIRM_POOL | IXGBE_FDIRM_DIPv6 |
390                          IXGBE_FDIRM_FLEX;
391         uint32_t fdiripv6m;
392         enum rte_fdir_mode mode = dev->data->dev_conf.fdir_conf.mode;
393         uint16_t mac_mask;
394
395         PMD_INIT_FUNC_TRACE();
396
397         /* set the default UDP port for VxLAN */
398         if (mode == RTE_FDIR_MODE_PERFECT_TUNNEL)
399                 IXGBE_WRITE_REG(hw, IXGBE_VXLANCTRL, DEFAULT_VXLAN_PORT);
400
401         /* some bits must be set for mac vlan or tunnel mode */
402         fdirm |= IXGBE_FDIRM_L4P | IXGBE_FDIRM_L3P;
403
404         if (input_mask->vlan_tci_mask == 0x0FFF)
405                 /* mask VLAN Priority */
406                 fdirm |= IXGBE_FDIRM_VLANP;
407         else if (input_mask->vlan_tci_mask == 0xE000)
408                 /* mask VLAN ID */
409                 fdirm |= IXGBE_FDIRM_VLANID;
410         else if (input_mask->vlan_tci_mask == 0)
411                 /* mask VLAN ID and Priority */
412                 fdirm |= IXGBE_FDIRM_VLANID | IXGBE_FDIRM_VLANP;
413         else if (input_mask->vlan_tci_mask != 0xEFFF) {
414                 PMD_INIT_LOG(ERR, "invalid vlan_tci_mask");
415                 return -EINVAL;
416         }
417         info->mask.vlan_tci_mask = input_mask->vlan_tci_mask;
418
419         IXGBE_WRITE_REG(hw, IXGBE_FDIRM, fdirm);
420
421         fdiripv6m = ((u32)0xFFFFU << IXGBE_FDIRIP6M_DIPM_SHIFT);
422         fdiripv6m |= IXGBE_FDIRIP6M_ALWAYS_MASK;
423         if (mode == RTE_FDIR_MODE_PERFECT_MAC_VLAN)
424                 fdiripv6m |= IXGBE_FDIRIP6M_TUNNEL_TYPE |
425                                 IXGBE_FDIRIP6M_TNI_VNI;
426
427         mac_mask = input_mask->mac_addr_byte_mask;
428         fdiripv6m |= (mac_mask << IXGBE_FDIRIP6M_INNER_MAC_SHIFT)
429                         & IXGBE_FDIRIP6M_INNER_MAC;
430         info->mask.mac_addr_byte_mask = input_mask->mac_addr_byte_mask;
431
432         if (mode == RTE_FDIR_MODE_PERFECT_TUNNEL) {
433                 switch (input_mask->tunnel_type_mask) {
434                 case 0:
435                         /* Mask turnnel type */
436                         fdiripv6m |= IXGBE_FDIRIP6M_TUNNEL_TYPE;
437                         break;
438                 case 1:
439                         break;
440                 default:
441                         PMD_INIT_LOG(ERR, "invalid tunnel_type_mask");
442                         return -EINVAL;
443                 }
444                 info->mask.tunnel_type_mask =
445                         input_mask->tunnel_type_mask;
446
447                 switch (input_mask->tunnel_id_mask & 0xFFFFFFFF) {
448                 case 0x0:
449                         /* Mask vxlan id */
450                         fdiripv6m |= IXGBE_FDIRIP6M_TNI_VNI;
451                         break;
452                 case 0x00FFFFFF:
453                         fdiripv6m |= IXGBE_FDIRIP6M_TNI_VNI_24;
454                         break;
455                 case 0xFFFFFFFF:
456                         break;
457                 default:
458                         PMD_INIT_LOG(ERR, "invalid tunnel_id_mask");
459                         return -EINVAL;
460                 }
461                 info->mask.tunnel_id_mask =
462                         input_mask->tunnel_id_mask;
463         }
464
465         IXGBE_WRITE_REG(hw, IXGBE_FDIRIP6M, fdiripv6m);
466         IXGBE_WRITE_REG(hw, IXGBE_FDIRTCPM, 0xFFFFFFFF);
467         IXGBE_WRITE_REG(hw, IXGBE_FDIRUDPM, 0xFFFFFFFF);
468         IXGBE_WRITE_REG(hw, IXGBE_FDIRSCTPM, 0xFFFFFFFF);
469         IXGBE_WRITE_REG(hw, IXGBE_FDIRDIP4M, 0xFFFFFFFF);
470         IXGBE_WRITE_REG(hw, IXGBE_FDIRSIP4M, 0xFFFFFFFF);
471
472         return IXGBE_SUCCESS;
473 }
474
475 static int
476 fdir_set_input_mask(struct rte_eth_dev *dev,
477                     const struct rte_eth_fdir_masks *input_mask)
478 {
479         enum rte_fdir_mode mode = dev->data->dev_conf.fdir_conf.mode;
480
481         if (mode >= RTE_FDIR_MODE_SIGNATURE &&
482             mode <= RTE_FDIR_MODE_PERFECT)
483                 return fdir_set_input_mask_82599(dev, input_mask);
484         else if (mode >= RTE_FDIR_MODE_PERFECT_MAC_VLAN &&
485                  mode <= RTE_FDIR_MODE_PERFECT_TUNNEL)
486                 return fdir_set_input_mask_x550(dev, input_mask);
487
488         PMD_DRV_LOG(ERR, "Not supported fdir mode - %d!", mode);
489         return -ENOTSUP;
490 }
491
492 /*
493  * ixgbe_check_fdir_flex_conf -check if the flex payload and mask configuration
494  * arguments are valid
495  */
496 static int
497 ixgbe_set_fdir_flex_conf(struct rte_eth_dev *dev,
498                 const struct rte_eth_fdir_flex_conf *conf, uint32_t *fdirctrl)
499 {
500         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
501         struct ixgbe_hw_fdir_info *info =
502                         IXGBE_DEV_PRIVATE_TO_FDIR_INFO(dev->data->dev_private);
503         const struct rte_eth_flex_payload_cfg *flex_cfg;
504         const struct rte_eth_fdir_flex_mask *flex_mask;
505         uint32_t fdirm;
506         uint16_t flexbytes = 0;
507         uint16_t i;
508
509         fdirm = IXGBE_READ_REG(hw, IXGBE_FDIRM);
510
511         if (conf == NULL) {
512                 PMD_DRV_LOG(ERR, "NULL pointer.");
513                 return -EINVAL;
514         }
515
516         for (i = 0; i < conf->nb_payloads; i++) {
517                 flex_cfg = &conf->flex_set[i];
518                 if (flex_cfg->type != RTE_ETH_RAW_PAYLOAD) {
519                         PMD_DRV_LOG(ERR, "unsupported payload type.");
520                         return -EINVAL;
521                 }
522                 if (((flex_cfg->src_offset[0] & 0x1) == 0) &&
523                     (flex_cfg->src_offset[1] == flex_cfg->src_offset[0] + 1) &&
524                     (flex_cfg->src_offset[0] <= IXGBE_MAX_FLX_SOURCE_OFF)) {
525                         *fdirctrl &= ~IXGBE_FDIRCTRL_FLEX_MASK;
526                         *fdirctrl |=
527                                 (flex_cfg->src_offset[0] / sizeof(uint16_t)) <<
528                                         IXGBE_FDIRCTRL_FLEX_SHIFT;
529                 } else {
530                         PMD_DRV_LOG(ERR, "invalid flexbytes arguments.");
531                         return -EINVAL;
532                 }
533         }
534
535         for (i = 0; i < conf->nb_flexmasks; i++) {
536                 flex_mask = &conf->flex_mask[i];
537                 if (flex_mask->flow_type != RTE_ETH_FLOW_UNKNOWN) {
538                         PMD_DRV_LOG(ERR, "flexmask should be set globally.");
539                         return -EINVAL;
540                 }
541                 flexbytes = (uint16_t)(((flex_mask->mask[0] << 8) & 0xFF00) |
542                                         ((flex_mask->mask[1]) & 0xFF));
543                 if (flexbytes == UINT16_MAX)
544                         fdirm &= ~IXGBE_FDIRM_FLEX;
545                 else if (flexbytes != 0) {
546                         /* IXGBE_FDIRM_FLEX is set by default when set mask */
547                         PMD_DRV_LOG(ERR, " invalid flexbytes mask arguments.");
548                         return -EINVAL;
549                 }
550         }
551         IXGBE_WRITE_REG(hw, IXGBE_FDIRM, fdirm);
552         info->mask.flex_bytes_mask = flexbytes ? UINT16_MAX : 0;
553         info->flex_bytes_offset = (uint8_t)((*fdirctrl &
554                                             IXGBE_FDIRCTRL_FLEX_MASK) >>
555                                             IXGBE_FDIRCTRL_FLEX_SHIFT);
556         return 0;
557 }
558
559 int
560 ixgbe_fdir_configure(struct rte_eth_dev *dev)
561 {
562         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
563         int err;
564         uint32_t fdirctrl, pbsize;
565         int i;
566         enum rte_fdir_mode mode = dev->data->dev_conf.fdir_conf.mode;
567
568         PMD_INIT_FUNC_TRACE();
569
570         if (hw->mac.type != ixgbe_mac_82599EB &&
571                 hw->mac.type != ixgbe_mac_X540 &&
572                 hw->mac.type != ixgbe_mac_X550 &&
573                 hw->mac.type != ixgbe_mac_X550EM_x)
574                 return -ENOSYS;
575
576         /* x550 supports mac-vlan and tunnel mode but other NICs not */
577         if (hw->mac.type != ixgbe_mac_X550 &&
578             hw->mac.type != ixgbe_mac_X550EM_x &&
579             mode != RTE_FDIR_MODE_SIGNATURE &&
580             mode != RTE_FDIR_MODE_PERFECT)
581                 return -ENOSYS;
582
583         err = configure_fdir_flags(&dev->data->dev_conf.fdir_conf, &fdirctrl);
584         if (err)
585                 return err;
586
587         /*
588          * Before enabling Flow Director, the Rx Packet Buffer size
589          * must be reduced.  The new value is the current size minus
590          * flow director memory usage size.
591          */
592         pbsize = (1 << (PBALLOC_SIZE_SHIFT + (fdirctrl & FDIRCTRL_PBALLOC_MASK)));
593         IXGBE_WRITE_REG(hw, IXGBE_RXPBSIZE(0),
594             (IXGBE_READ_REG(hw, IXGBE_RXPBSIZE(0)) - pbsize));
595
596         /*
597          * The defaults in the HW for RX PB 1-7 are not zero and so should be
598          * intialized to zero for non DCB mode otherwise actual total RX PB
599          * would be bigger than programmed and filter space would run into
600          * the PB 0 region.
601          */
602         for (i = 1; i < 8; i++)
603                 IXGBE_WRITE_REG(hw, IXGBE_RXPBSIZE(i), 0);
604
605         err = fdir_set_input_mask(dev, &dev->data->dev_conf.fdir_conf.mask);
606         if (err < 0) {
607                 PMD_INIT_LOG(ERR, " Error on setting FD mask");
608                 return err;
609         }
610         err = ixgbe_set_fdir_flex_conf(dev,
611                 &dev->data->dev_conf.fdir_conf.flex_conf, &fdirctrl);
612         if (err < 0) {
613                 PMD_INIT_LOG(ERR, " Error on setting FD flexible arguments.");
614                 return err;
615         }
616
617         err = fdir_enable_82599(hw, fdirctrl);
618         if (err < 0) {
619                 PMD_INIT_LOG(ERR, " Error on enabling FD.");
620                 return err;
621         }
622         return 0;
623 }
624
625 /*
626  * Convert DPDK rte_eth_fdir_filter struct to ixgbe_atr_input union that is used
627  * by the IXGBE driver code.
628  */
629 static int
630 ixgbe_fdir_filter_to_atr_input(const struct rte_eth_fdir_filter *fdir_filter,
631                 union ixgbe_atr_input *input, enum rte_fdir_mode mode)
632 {
633         input->formatted.vlan_id = fdir_filter->input.flow_ext.vlan_tci;
634         input->formatted.flex_bytes = (uint16_t)(
635                 (fdir_filter->input.flow_ext.flexbytes[1] << 8 & 0xFF00) |
636                 (fdir_filter->input.flow_ext.flexbytes[0] & 0xFF));
637
638         switch (fdir_filter->input.flow_type) {
639         case RTE_ETH_FLOW_NONFRAG_IPV4_UDP:
640                 input->formatted.flow_type = IXGBE_ATR_FLOW_TYPE_UDPV4;
641                 break;
642         case RTE_ETH_FLOW_NONFRAG_IPV4_TCP:
643                 input->formatted.flow_type = IXGBE_ATR_FLOW_TYPE_TCPV4;
644                 break;
645         case RTE_ETH_FLOW_NONFRAG_IPV4_SCTP:
646                 input->formatted.flow_type = IXGBE_ATR_FLOW_TYPE_SCTPV4;
647                 break;
648         case RTE_ETH_FLOW_NONFRAG_IPV4_OTHER:
649                 input->formatted.flow_type = IXGBE_ATR_FLOW_TYPE_IPV4;
650                 break;
651         case RTE_ETH_FLOW_NONFRAG_IPV6_UDP:
652                 input->formatted.flow_type = IXGBE_ATR_FLOW_TYPE_UDPV6;
653                 break;
654         case RTE_ETH_FLOW_NONFRAG_IPV6_TCP:
655                 input->formatted.flow_type = IXGBE_ATR_FLOW_TYPE_TCPV6;
656                 break;
657         case RTE_ETH_FLOW_NONFRAG_IPV6_SCTP:
658                 input->formatted.flow_type = IXGBE_ATR_FLOW_TYPE_SCTPV6;
659                 break;
660         case RTE_ETH_FLOW_NONFRAG_IPV6_OTHER:
661                 input->formatted.flow_type = IXGBE_ATR_FLOW_TYPE_IPV6;
662                 break;
663         default:
664                 break;
665         }
666
667         switch (fdir_filter->input.flow_type) {
668         case RTE_ETH_FLOW_NONFRAG_IPV4_UDP:
669         case RTE_ETH_FLOW_NONFRAG_IPV4_TCP:
670                 input->formatted.src_port =
671                         fdir_filter->input.flow.udp4_flow.src_port;
672                 input->formatted.dst_port =
673                         fdir_filter->input.flow.udp4_flow.dst_port;
674         /*for SCTP flow type, port and verify_tag are meaningless in ixgbe.*/
675         case RTE_ETH_FLOW_NONFRAG_IPV4_SCTP:
676         case RTE_ETH_FLOW_NONFRAG_IPV4_OTHER:
677                 input->formatted.src_ip[0] =
678                         fdir_filter->input.flow.ip4_flow.src_ip;
679                 input->formatted.dst_ip[0] =
680                         fdir_filter->input.flow.ip4_flow.dst_ip;
681                 break;
682
683         case RTE_ETH_FLOW_NONFRAG_IPV6_UDP:
684         case RTE_ETH_FLOW_NONFRAG_IPV6_TCP:
685                 input->formatted.src_port =
686                         fdir_filter->input.flow.udp6_flow.src_port;
687                 input->formatted.dst_port =
688                         fdir_filter->input.flow.udp6_flow.dst_port;
689         /*for SCTP flow type, port and verify_tag are meaningless in ixgbe.*/
690         case RTE_ETH_FLOW_NONFRAG_IPV6_SCTP:
691         case RTE_ETH_FLOW_NONFRAG_IPV6_OTHER:
692                 rte_memcpy(input->formatted.src_ip,
693                            fdir_filter->input.flow.ipv6_flow.src_ip,
694                            sizeof(input->formatted.src_ip));
695                 rte_memcpy(input->formatted.dst_ip,
696                            fdir_filter->input.flow.ipv6_flow.dst_ip,
697                            sizeof(input->formatted.dst_ip));
698                 break;
699         default:
700                 break;
701         }
702
703         if (mode == RTE_FDIR_MODE_PERFECT_MAC_VLAN) {
704                 rte_memcpy(
705                         input->formatted.inner_mac,
706                         fdir_filter->input.flow.mac_vlan_flow.mac_addr.addr_bytes,
707                         sizeof(input->formatted.inner_mac));
708         } else if (mode == RTE_FDIR_MODE_PERFECT_TUNNEL) {
709                 rte_memcpy(
710                         input->formatted.inner_mac,
711                         fdir_filter->input.flow.tunnel_flow.mac_addr.addr_bytes,
712                         sizeof(input->formatted.inner_mac));
713                 input->formatted.tunnel_type =
714                         fdir_filter->input.flow.tunnel_flow.tunnel_type;
715                 input->formatted.tni_vni =
716                         fdir_filter->input.flow.tunnel_flow.tunnel_id;
717         }
718
719         return 0;
720 }
721
722 /*
723  * The below function is taken from the FreeBSD IXGBE drivers release
724  * 2.3.8. The only change is not to mask hash_result with IXGBE_ATR_HASH_MASK
725  * before returning, as the signature hash can use 16bits.
726  *
727  * The newer driver has optimised functions for calculating bucket and
728  * signature hashes. However they don't support IPv6 type packets for signature
729  * filters so are not used here.
730  *
731  * Note that the bkt_hash field in the ixgbe_atr_input structure is also never
732  * set.
733  *
734  * Compute the hashes for SW ATR
735  *  @stream: input bitstream to compute the hash on
736  *  @key: 32-bit hash key
737  **/
738 static uint32_t
739 ixgbe_atr_compute_hash_82599(union ixgbe_atr_input *atr_input,
740                                  uint32_t key)
741 {
742         /*
743          * The algorithm is as follows:
744          *    Hash[15:0] = Sum { S[n] x K[n+16] }, n = 0...350
745          *    where Sum {A[n]}, n = 0...n is bitwise XOR of A[0], A[1]...A[n]
746          *    and A[n] x B[n] is bitwise AND between same length strings
747          *
748          *    K[n] is 16 bits, defined as:
749          *       for n modulo 32 >= 15, K[n] = K[n % 32 : (n % 32) - 15]
750          *       for n modulo 32 < 15, K[n] =
751          *             K[(n % 32:0) | (31:31 - (14 - (n % 32)))]
752          *
753          *    S[n] is 16 bits, defined as:
754          *       for n >= 15, S[n] = S[n:n - 15]
755          *       for n < 15, S[n] = S[(n:0) | (350:350 - (14 - n))]
756          *
757          *    To simplify for programming, the algorithm is implemented
758          *    in software this way:
759          *
760          *    key[31:0], hi_hash_dword[31:0], lo_hash_dword[31:0], hash[15:0]
761          *
762          *    for (i = 0; i < 352; i+=32)
763          *        hi_hash_dword[31:0] ^= Stream[(i+31):i];
764          *
765          *    lo_hash_dword[15:0]  ^= Stream[15:0];
766          *    lo_hash_dword[15:0]  ^= hi_hash_dword[31:16];
767          *    lo_hash_dword[31:16] ^= hi_hash_dword[15:0];
768          *
769          *    hi_hash_dword[31:0]  ^= Stream[351:320];
770          *
771          *    if(key[0])
772          *        hash[15:0] ^= Stream[15:0];
773          *
774          *    for (i = 0; i < 16; i++) {
775          *        if (key[i])
776          *            hash[15:0] ^= lo_hash_dword[(i+15):i];
777          *        if (key[i + 16])
778          *            hash[15:0] ^= hi_hash_dword[(i+15):i];
779          *    }
780          *
781          */
782         __be32 common_hash_dword = 0;
783         u32 hi_hash_dword, lo_hash_dword, flow_vm_vlan;
784         u32 hash_result = 0;
785         u8 i;
786
787         /* record the flow_vm_vlan bits as they are a key part to the hash */
788         flow_vm_vlan = IXGBE_NTOHL(atr_input->dword_stream[0]);
789
790         /* generate common hash dword */
791         for (i = 1; i <= 13; i++)
792                 common_hash_dword ^= atr_input->dword_stream[i];
793
794         hi_hash_dword = IXGBE_NTOHL(common_hash_dword);
795
796         /* low dword is word swapped version of common */
797         lo_hash_dword = (hi_hash_dword >> 16) | (hi_hash_dword << 16);
798
799         /* apply flow ID/VM pool/VLAN ID bits to hash words */
800         hi_hash_dword ^= flow_vm_vlan ^ (flow_vm_vlan >> 16);
801
802         /* Process bits 0 and 16 */
803         if (key & 0x0001) hash_result ^= lo_hash_dword;
804         if (key & 0x00010000) hash_result ^= hi_hash_dword;
805
806         /*
807          * apply flow ID/VM pool/VLAN ID bits to lo hash dword, we had to
808          * delay this because bit 0 of the stream should not be processed
809          * so we do not add the vlan until after bit 0 was processed
810          */
811         lo_hash_dword ^= flow_vm_vlan ^ (flow_vm_vlan << 16);
812
813
814         /* process the remaining 30 bits in the key 2 bits at a time */
815         for (i = 15; i; i-- ) {
816                 if (key & (0x0001 << i)) hash_result ^= lo_hash_dword >> i;
817                 if (key & (0x00010000 << i)) hash_result ^= hi_hash_dword >> i;
818         }
819
820         return hash_result;
821 }
822
823 static uint32_t
824 atr_compute_perfect_hash_82599(union ixgbe_atr_input *input,
825                 enum rte_fdir_pballoc_type pballoc)
826 {
827         if (pballoc == RTE_FDIR_PBALLOC_256K)
828                 return ixgbe_atr_compute_hash_82599(input,
829                                 IXGBE_ATR_BUCKET_HASH_KEY) &
830                                 PERFECT_BUCKET_256KB_HASH_MASK;
831         else if (pballoc == RTE_FDIR_PBALLOC_128K)
832                 return ixgbe_atr_compute_hash_82599(input,
833                                 IXGBE_ATR_BUCKET_HASH_KEY) &
834                                 PERFECT_BUCKET_128KB_HASH_MASK;
835         else
836                 return ixgbe_atr_compute_hash_82599(input,
837                                 IXGBE_ATR_BUCKET_HASH_KEY) &
838                                 PERFECT_BUCKET_64KB_HASH_MASK;
839 }
840
841 /**
842  * ixgbe_fdir_check_cmd_complete - poll to check whether FDIRCMD is complete
843  * @hw: pointer to hardware structure
844  */
845 static inline int
846 ixgbe_fdir_check_cmd_complete(struct ixgbe_hw *hw, uint32_t *fdircmd)
847 {
848         int i;
849
850         for (i = 0; i < IXGBE_FDIRCMD_CMD_POLL; i++) {
851                 *fdircmd = IXGBE_READ_REG(hw, IXGBE_FDIRCMD);
852                 if (!(*fdircmd & IXGBE_FDIRCMD_CMD_MASK))
853                         return 0;
854                 rte_delay_us(IXGBE_FDIRCMD_CMD_INTERVAL_US);
855         }
856
857         return -ETIMEDOUT;
858 }
859
860 /*
861  * Calculate the hash value needed for signature-match filters. In the FreeBSD
862  * driver, this is done by the optimised function
863  * ixgbe_atr_compute_sig_hash_82599(). However that can't be used here as it
864  * doesn't support calculating a hash for an IPv6 filter.
865  */
866 static uint32_t
867 atr_compute_sig_hash_82599(union ixgbe_atr_input *input,
868                 enum rte_fdir_pballoc_type pballoc)
869 {
870         uint32_t bucket_hash, sig_hash;
871
872         if (pballoc == RTE_FDIR_PBALLOC_256K)
873                 bucket_hash = ixgbe_atr_compute_hash_82599(input,
874                                 IXGBE_ATR_BUCKET_HASH_KEY) &
875                                 SIG_BUCKET_256KB_HASH_MASK;
876         else if (pballoc == RTE_FDIR_PBALLOC_128K)
877                 bucket_hash = ixgbe_atr_compute_hash_82599(input,
878                                 IXGBE_ATR_BUCKET_HASH_KEY) &
879                                 SIG_BUCKET_128KB_HASH_MASK;
880         else
881                 bucket_hash = ixgbe_atr_compute_hash_82599(input,
882                                 IXGBE_ATR_BUCKET_HASH_KEY) &
883                                 SIG_BUCKET_64KB_HASH_MASK;
884
885         sig_hash = ixgbe_atr_compute_hash_82599(input,
886                         IXGBE_ATR_SIGNATURE_HASH_KEY);
887
888         return (sig_hash << IXGBE_FDIRHASH_SIG_SW_INDEX_SHIFT) | bucket_hash;
889 }
890
891 /*
892  * This is based on ixgbe_fdir_write_perfect_filter_82599() in
893  * base/ixgbe_82599.c, with the ability to set extra flags in FDIRCMD register
894  * added, and IPv6 support also added. The hash value is also pre-calculated
895  * as the pballoc value is needed to do it.
896  */
897 static int
898 fdir_write_perfect_filter_82599(struct ixgbe_hw *hw,
899                         union ixgbe_atr_input *input, uint8_t queue,
900                         uint32_t fdircmd, uint32_t fdirhash,
901                         enum rte_fdir_mode mode)
902 {
903         uint32_t fdirport, fdirvlan;
904         u32 addr_low, addr_high;
905         u32 tunnel_type = 0;
906         int err = 0;
907
908         if (mode == RTE_FDIR_MODE_PERFECT) {
909                 /* record the IPv4 address (big-endian) */
910                 IXGBE_WRITE_REG(hw, IXGBE_FDIRIPSA,
911                                 input->formatted.src_ip[0]);
912                 IXGBE_WRITE_REG(hw, IXGBE_FDIRIPDA,
913                                 input->formatted.dst_ip[0]);
914
915                 /* record source and destination port (little-endian)*/
916                 fdirport = IXGBE_NTOHS(input->formatted.dst_port);
917                 fdirport <<= IXGBE_FDIRPORT_DESTINATION_SHIFT;
918                 fdirport |= IXGBE_NTOHS(input->formatted.src_port);
919                 IXGBE_WRITE_REG(hw, IXGBE_FDIRPORT, fdirport);
920         } else if (mode >= RTE_FDIR_MODE_PERFECT_MAC_VLAN &&
921                    mode <= RTE_FDIR_MODE_PERFECT_TUNNEL) {
922                 /* for mac vlan and tunnel modes */
923                 addr_low = ((u32)input->formatted.inner_mac[0] |
924                             ((u32)input->formatted.inner_mac[1] << 8) |
925                             ((u32)input->formatted.inner_mac[2] << 16) |
926                             ((u32)input->formatted.inner_mac[3] << 24));
927                 addr_high = ((u32)input->formatted.inner_mac[4] |
928                              ((u32)input->formatted.inner_mac[5] << 8));
929
930                 if (mode == RTE_FDIR_MODE_PERFECT_MAC_VLAN) {
931                         IXGBE_WRITE_REG(hw, IXGBE_FDIRSIPv6(0), addr_low);
932                         IXGBE_WRITE_REG(hw, IXGBE_FDIRSIPv6(1), addr_high);
933                         IXGBE_WRITE_REG(hw, IXGBE_FDIRSIPv6(2), 0);
934                 } else {
935                         /* tunnel mode */
936                         if (input->formatted.tunnel_type !=
937                                 RTE_FDIR_TUNNEL_TYPE_NVGRE)
938                                 tunnel_type = 0x80000000;
939                         tunnel_type |= addr_high;
940                         IXGBE_WRITE_REG(hw, IXGBE_FDIRSIPv6(0), addr_low);
941                         IXGBE_WRITE_REG(hw, IXGBE_FDIRSIPv6(1), tunnel_type);
942                         IXGBE_WRITE_REG(hw, IXGBE_FDIRSIPv6(2),
943                                         input->formatted.tni_vni);
944                 }
945         }
946
947         /* record vlan (little-endian) and flex_bytes(big-endian) */
948         fdirvlan = input->formatted.flex_bytes;
949         fdirvlan <<= IXGBE_FDIRVLAN_FLEX_SHIFT;
950         fdirvlan |= IXGBE_NTOHS(input->formatted.vlan_id);
951         IXGBE_WRITE_REG(hw, IXGBE_FDIRVLAN, fdirvlan);
952
953         /* configure FDIRHASH register */
954         IXGBE_WRITE_REG(hw, IXGBE_FDIRHASH, fdirhash);
955
956         /*
957          * flush all previous writes to make certain registers are
958          * programmed prior to issuing the command
959          */
960         IXGBE_WRITE_FLUSH(hw);
961
962         /* configure FDIRCMD register */
963         fdircmd |= IXGBE_FDIRCMD_CMD_ADD_FLOW |
964                   IXGBE_FDIRCMD_LAST | IXGBE_FDIRCMD_QUEUE_EN;
965         fdircmd |= input->formatted.flow_type << IXGBE_FDIRCMD_FLOW_TYPE_SHIFT;
966         fdircmd |= (uint32_t)queue << IXGBE_FDIRCMD_RX_QUEUE_SHIFT;
967         fdircmd |= (uint32_t)input->formatted.vm_pool << IXGBE_FDIRCMD_VT_POOL_SHIFT;
968
969         IXGBE_WRITE_REG(hw, IXGBE_FDIRCMD, fdircmd);
970
971         PMD_DRV_LOG(DEBUG, "Rx Queue=%x hash=%x", queue, fdirhash);
972
973         err = ixgbe_fdir_check_cmd_complete(hw, &fdircmd);
974         if (err < 0)
975                 PMD_DRV_LOG(ERR, "Timeout writing flow director filter.");
976
977         return err;
978 }
979
980 /**
981  * This function is based on ixgbe_atr_add_signature_filter_82599() in
982  * base/ixgbe_82599.c, but uses a pre-calculated hash value. It also supports
983  * setting extra fields in the FDIRCMD register, and removes the code that was
984  * verifying the flow_type field. According to the documentation, a flow type of
985  * 00 (i.e. not TCP, UDP, or SCTP) is not supported, however it appears to
986  * work ok...
987  *
988  *  Adds a signature hash filter
989  *  @hw: pointer to hardware structure
990  *  @input: unique input dword
991  *  @queue: queue index to direct traffic to
992  *  @fdircmd: any extra flags to set in fdircmd register
993  *  @fdirhash: pre-calculated hash value for the filter
994  **/
995 static int
996 fdir_add_signature_filter_82599(struct ixgbe_hw *hw,
997                 union ixgbe_atr_input *input, u8 queue, uint32_t fdircmd,
998                 uint32_t fdirhash)
999 {
1000         int err = 0;
1001
1002         PMD_INIT_FUNC_TRACE();
1003
1004         /* configure FDIRCMD register */
1005         fdircmd |= IXGBE_FDIRCMD_CMD_ADD_FLOW |
1006                   IXGBE_FDIRCMD_LAST | IXGBE_FDIRCMD_QUEUE_EN;
1007         fdircmd |= input->formatted.flow_type << IXGBE_FDIRCMD_FLOW_TYPE_SHIFT;
1008         fdircmd |= (uint32_t)queue << IXGBE_FDIRCMD_RX_QUEUE_SHIFT;
1009
1010         IXGBE_WRITE_REG(hw, IXGBE_FDIRHASH, fdirhash);
1011         IXGBE_WRITE_REG(hw, IXGBE_FDIRCMD, fdircmd);
1012
1013         PMD_DRV_LOG(DEBUG, "Rx Queue=%x hash=%x", queue, fdirhash);
1014
1015         err = ixgbe_fdir_check_cmd_complete(hw, &fdircmd);
1016         if (err < 0)
1017                 PMD_DRV_LOG(ERR, "Timeout writing flow director filter.");
1018
1019         return err;
1020 }
1021
1022 /*
1023  * This is based on ixgbe_fdir_erase_perfect_filter_82599() in
1024  * base/ixgbe_82599.c. It is modified to take in the hash as a parameter so
1025  * that it can be used for removing signature and perfect filters.
1026  */
1027 static int
1028 fdir_erase_filter_82599(struct ixgbe_hw *hw, uint32_t fdirhash)
1029 {
1030         uint32_t fdircmd = 0;
1031         int err = 0;
1032
1033         IXGBE_WRITE_REG(hw, IXGBE_FDIRHASH, fdirhash);
1034
1035         /* flush hash to HW */
1036         IXGBE_WRITE_FLUSH(hw);
1037
1038         /* Query if filter is present */
1039         IXGBE_WRITE_REG(hw, IXGBE_FDIRCMD, IXGBE_FDIRCMD_CMD_QUERY_REM_FILT);
1040
1041         err = ixgbe_fdir_check_cmd_complete(hw, &fdircmd);
1042         if (err < 0) {
1043                 PMD_INIT_LOG(ERR, "Timeout querying for flow director filter.");
1044                 return err;
1045         }
1046
1047         /* if filter exists in hardware then remove it */
1048         if (fdircmd & IXGBE_FDIRCMD_FILTER_VALID) {
1049                 IXGBE_WRITE_REG(hw, IXGBE_FDIRHASH, fdirhash);
1050                 IXGBE_WRITE_FLUSH(hw);
1051                 IXGBE_WRITE_REG(hw, IXGBE_FDIRCMD,
1052                                 IXGBE_FDIRCMD_CMD_REMOVE_FLOW);
1053         }
1054         err = ixgbe_fdir_check_cmd_complete(hw, &fdircmd);
1055         if (err < 0)
1056                 PMD_INIT_LOG(ERR, "Timeout erasing flow director filter.");
1057         return err;
1058
1059 }
1060
1061 /*
1062  * ixgbe_add_del_fdir_filter - add or remove a flow diretor filter.
1063  * @dev: pointer to the structure rte_eth_dev
1064  * @fdir_filter: fdir filter entry
1065  * @del: 1 - delete, 0 - add
1066  * @update: 1 - update
1067  */
1068 static int
1069 ixgbe_add_del_fdir_filter(struct rte_eth_dev *dev,
1070                               const struct rte_eth_fdir_filter *fdir_filter,
1071                               bool del,
1072                               bool update)
1073 {
1074         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1075         uint32_t fdircmd_flags;
1076         uint32_t fdirhash;
1077         union ixgbe_atr_input input;
1078         uint8_t queue;
1079         bool is_perfect = FALSE;
1080         int err;
1081         struct ixgbe_hw_fdir_info *info =
1082                         IXGBE_DEV_PRIVATE_TO_FDIR_INFO(dev->data->dev_private);
1083         enum rte_fdir_mode fdir_mode = dev->data->dev_conf.fdir_conf.mode;
1084
1085         if (fdir_mode == RTE_FDIR_MODE_NONE)
1086                 return -ENOTSUP;
1087
1088         /*
1089          * Sanity check for x550.
1090          * When adding a new filter with flow type set to IPv4-other,
1091          * the flow director mask should be configed before,
1092          * and the L4 protocol and ports are masked.
1093          */
1094         if ((!del) &&
1095             (hw->mac.type == ixgbe_mac_X550 ||
1096              hw->mac.type == ixgbe_mac_X550EM_x) &&
1097             (fdir_filter->input.flow_type ==
1098                RTE_ETH_FLOW_NONFRAG_IPV4_OTHER) &&
1099             (info->mask.src_port_mask != 0 ||
1100              info->mask.dst_port_mask != 0)) {
1101                 PMD_DRV_LOG(ERR, "By this device,"
1102                                  " IPv4-other is not supported without"
1103                                  " L4 protocol and ports masked!");
1104                 return -ENOTSUP;
1105         }
1106
1107         if (fdir_mode >= RTE_FDIR_MODE_PERFECT &&
1108             fdir_mode <= RTE_FDIR_MODE_PERFECT_TUNNEL)
1109                 is_perfect = TRUE;
1110
1111         memset(&input, 0, sizeof(input));
1112
1113         err = ixgbe_fdir_filter_to_atr_input(fdir_filter, &input,
1114                                              fdir_mode);
1115         if (err)
1116                 return err;
1117
1118         if (is_perfect) {
1119                 if (input.formatted.flow_type & IXGBE_ATR_L4TYPE_IPV6_MASK) {
1120                         PMD_DRV_LOG(ERR, "IPv6 is not supported in"
1121                                          " perfect mode!");
1122                         return -ENOTSUP;
1123                 }
1124                 fdirhash = atr_compute_perfect_hash_82599(&input,
1125                                 dev->data->dev_conf.fdir_conf.pballoc);
1126                 fdirhash |= fdir_filter->soft_id <<
1127                                 IXGBE_FDIRHASH_SIG_SW_INDEX_SHIFT;
1128         } else
1129                 fdirhash = atr_compute_sig_hash_82599(&input,
1130                                 dev->data->dev_conf.fdir_conf.pballoc);
1131
1132         if (del) {
1133                 err = fdir_erase_filter_82599(hw, fdirhash);
1134                 if (err < 0)
1135                         PMD_DRV_LOG(ERR, "Fail to delete FDIR filter!");
1136                 else
1137                         PMD_DRV_LOG(DEBUG, "Success to delete FDIR filter!");
1138                 return err;
1139         }
1140         /* add or update an fdir filter*/
1141         fdircmd_flags = (update) ? IXGBE_FDIRCMD_FILTER_UPDATE : 0;
1142         if (fdir_filter->action.behavior == RTE_ETH_FDIR_REJECT) {
1143                 if (is_perfect) {
1144                         queue = dev->data->dev_conf.fdir_conf.drop_queue;
1145                         fdircmd_flags |= IXGBE_FDIRCMD_DROP;
1146                 } else {
1147                         PMD_DRV_LOG(ERR, "Drop option is not supported in"
1148                                 " signature mode.");
1149                         return -EINVAL;
1150                 }
1151         } else if (fdir_filter->action.behavior == RTE_ETH_FDIR_ACCEPT &&
1152                         fdir_filter->action.rx_queue < IXGBE_MAX_RX_QUEUE_NUM)
1153                 queue = (uint8_t)fdir_filter->action.rx_queue;
1154         else
1155                 return -EINVAL;
1156
1157         if (is_perfect) {
1158                 err = fdir_write_perfect_filter_82599(hw, &input, queue,
1159                                 fdircmd_flags, fdirhash,
1160                                 fdir_mode);
1161         } else {
1162                 err = fdir_add_signature_filter_82599(hw, &input, queue,
1163                                 fdircmd_flags, fdirhash);
1164         }
1165         if (err < 0)
1166                 PMD_DRV_LOG(ERR, "Fail to add FDIR filter!");
1167         else
1168                 PMD_DRV_LOG(DEBUG, "Success to add FDIR filter");
1169
1170         return err;
1171 }
1172
1173 static int
1174 ixgbe_fdir_flush(struct rte_eth_dev *dev)
1175 {
1176         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1177         struct ixgbe_hw_fdir_info *info =
1178                         IXGBE_DEV_PRIVATE_TO_FDIR_INFO(dev->data->dev_private);
1179         int ret;
1180
1181         ret = ixgbe_reinit_fdir_tables_82599(hw);
1182         if (ret < 0) {
1183                 PMD_INIT_LOG(ERR, "Failed to re-initialize FD table.");
1184                 return ret;
1185         }
1186
1187         info->f_add = 0;
1188         info->f_remove = 0;
1189         info->add = 0;
1190         info->remove = 0;
1191
1192         return ret;
1193 }
1194
1195 #define FDIRENTRIES_NUM_SHIFT 10
1196 static void
1197 ixgbe_fdir_info_get(struct rte_eth_dev *dev, struct rte_eth_fdir_info *fdir_info)
1198 {
1199         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1200         struct ixgbe_hw_fdir_info *info =
1201                         IXGBE_DEV_PRIVATE_TO_FDIR_INFO(dev->data->dev_private);
1202         uint32_t fdirctrl, max_num;
1203         uint8_t offset;
1204
1205         fdirctrl = IXGBE_READ_REG(hw, IXGBE_FDIRCTRL);
1206         offset = ((fdirctrl & IXGBE_FDIRCTRL_FLEX_MASK) >>
1207                         IXGBE_FDIRCTRL_FLEX_SHIFT) * sizeof(uint16_t);
1208
1209         fdir_info->mode = dev->data->dev_conf.fdir_conf.mode;
1210         max_num = (1 << (FDIRENTRIES_NUM_SHIFT +
1211                         (fdirctrl & FDIRCTRL_PBALLOC_MASK)));
1212         if (fdir_info->mode >= RTE_FDIR_MODE_PERFECT &&
1213             fdir_info->mode <= RTE_FDIR_MODE_PERFECT_TUNNEL)
1214                 fdir_info->guarant_spc = max_num;
1215         else if (fdir_info->mode == RTE_FDIR_MODE_SIGNATURE)
1216                 fdir_info->guarant_spc = max_num * 4;
1217
1218         fdir_info->mask.vlan_tci_mask = info->mask.vlan_tci_mask;
1219         fdir_info->mask.ipv4_mask.src_ip = info->mask.src_ipv4_mask;
1220         fdir_info->mask.ipv4_mask.dst_ip = info->mask.dst_ipv4_mask;
1221         IPV6_MASK_TO_ADDR(info->mask.src_ipv6_mask,
1222                         fdir_info->mask.ipv6_mask.src_ip);
1223         IPV6_MASK_TO_ADDR(info->mask.dst_ipv6_mask,
1224                         fdir_info->mask.ipv6_mask.dst_ip);
1225         fdir_info->mask.src_port_mask = info->mask.src_port_mask;
1226         fdir_info->mask.dst_port_mask = info->mask.dst_port_mask;
1227         fdir_info->mask.mac_addr_byte_mask = info->mask.mac_addr_byte_mask;
1228         fdir_info->mask.tunnel_id_mask = info->mask.tunnel_id_mask;
1229         fdir_info->mask.tunnel_type_mask = info->mask.tunnel_type_mask;
1230         fdir_info->max_flexpayload = IXGBE_FDIR_MAX_FLEX_LEN;
1231
1232         if (fdir_info->mode == RTE_FDIR_MODE_PERFECT_MAC_VLAN ||
1233             fdir_info->mode == RTE_FDIR_MODE_PERFECT_TUNNEL)
1234                 fdir_info->flow_types_mask[0] = 0;
1235         else
1236                 fdir_info->flow_types_mask[0] = IXGBE_FDIR_FLOW_TYPES;
1237
1238         fdir_info->flex_payload_unit = sizeof(uint16_t);
1239         fdir_info->max_flex_payload_segment_num = 1;
1240         fdir_info->flex_payload_limit = IXGBE_MAX_FLX_SOURCE_OFF;
1241         fdir_info->flex_conf.nb_payloads = 1;
1242         fdir_info->flex_conf.flex_set[0].type = RTE_ETH_RAW_PAYLOAD;
1243         fdir_info->flex_conf.flex_set[0].src_offset[0] = offset;
1244         fdir_info->flex_conf.flex_set[0].src_offset[1] = offset + 1;
1245         fdir_info->flex_conf.nb_flexmasks = 1;
1246         fdir_info->flex_conf.flex_mask[0].flow_type = RTE_ETH_FLOW_UNKNOWN;
1247         fdir_info->flex_conf.flex_mask[0].mask[0] =
1248                         (uint8_t)(info->mask.flex_bytes_mask & 0x00FF);
1249         fdir_info->flex_conf.flex_mask[0].mask[1] =
1250                         (uint8_t)((info->mask.flex_bytes_mask & 0xFF00) >> 8);
1251 }
1252
1253 static void
1254 ixgbe_fdir_stats_get(struct rte_eth_dev *dev, struct rte_eth_fdir_stats *fdir_stats)
1255 {
1256         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1257         struct ixgbe_hw_fdir_info *info =
1258                         IXGBE_DEV_PRIVATE_TO_FDIR_INFO(dev->data->dev_private);
1259         uint32_t reg, max_num;
1260         enum rte_fdir_mode fdir_mode = dev->data->dev_conf.fdir_conf.mode;
1261
1262         /* Get the information from registers */
1263         reg = IXGBE_READ_REG(hw, IXGBE_FDIRFREE);
1264         info->collision = (uint16_t)((reg & IXGBE_FDIRFREE_COLL_MASK) >>
1265                                         IXGBE_FDIRFREE_COLL_SHIFT);
1266         info->free = (uint16_t)((reg & IXGBE_FDIRFREE_FREE_MASK) >>
1267                                    IXGBE_FDIRFREE_FREE_SHIFT);
1268
1269         reg = IXGBE_READ_REG(hw, IXGBE_FDIRLEN);
1270         info->maxhash = (uint16_t)((reg & IXGBE_FDIRLEN_MAXHASH_MASK) >>
1271                                       IXGBE_FDIRLEN_MAXHASH_SHIFT);
1272         info->maxlen  = (uint8_t)((reg & IXGBE_FDIRLEN_MAXLEN_MASK) >>
1273                                      IXGBE_FDIRLEN_MAXLEN_SHIFT);
1274
1275         reg = IXGBE_READ_REG(hw, IXGBE_FDIRUSTAT);
1276         info->remove += (reg & IXGBE_FDIRUSTAT_REMOVE_MASK) >>
1277                 IXGBE_FDIRUSTAT_REMOVE_SHIFT;
1278         info->add += (reg & IXGBE_FDIRUSTAT_ADD_MASK) >>
1279                 IXGBE_FDIRUSTAT_ADD_SHIFT;
1280
1281         reg = IXGBE_READ_REG(hw, IXGBE_FDIRFSTAT) & 0xFFFF;
1282         info->f_remove += (reg & IXGBE_FDIRFSTAT_FREMOVE_MASK) >>
1283                 IXGBE_FDIRFSTAT_FREMOVE_SHIFT;
1284         info->f_add += (reg & IXGBE_FDIRFSTAT_FADD_MASK) >>
1285                 IXGBE_FDIRFSTAT_FADD_SHIFT;
1286
1287         /*  Copy the new information in the fdir parameter */
1288         fdir_stats->collision = info->collision;
1289         fdir_stats->free = info->free;
1290         fdir_stats->maxhash = info->maxhash;
1291         fdir_stats->maxlen = info->maxlen;
1292         fdir_stats->remove = info->remove;
1293         fdir_stats->add = info->add;
1294         fdir_stats->f_remove = info->f_remove;
1295         fdir_stats->f_add = info->f_add;
1296
1297         reg = IXGBE_READ_REG(hw, IXGBE_FDIRCTRL);
1298         max_num = (1 << (FDIRENTRIES_NUM_SHIFT +
1299                         (reg & FDIRCTRL_PBALLOC_MASK)));
1300         if (fdir_mode >= RTE_FDIR_MODE_PERFECT &&
1301             fdir_mode <= RTE_FDIR_MODE_PERFECT_TUNNEL)
1302                         fdir_stats->guarant_cnt = max_num - fdir_stats->free;
1303         else if (fdir_mode == RTE_FDIR_MODE_SIGNATURE)
1304                 fdir_stats->guarant_cnt = max_num * 4 - fdir_stats->free;
1305
1306 }
1307
1308 /*
1309  * ixgbe_fdir_ctrl_func - deal with all operations on flow director.
1310  * @dev: pointer to the structure rte_eth_dev
1311  * @filter_op:operation will be taken
1312  * @arg: a pointer to specific structure corresponding to the filter_op
1313  */
1314 int
1315 ixgbe_fdir_ctrl_func(struct rte_eth_dev *dev,
1316                         enum rte_filter_op filter_op, void *arg)
1317 {
1318         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1319         int ret = 0;
1320
1321         if (hw->mac.type != ixgbe_mac_82599EB &&
1322                 hw->mac.type != ixgbe_mac_X540 &&
1323                 hw->mac.type != ixgbe_mac_X550 &&
1324                 hw->mac.type != ixgbe_mac_X550EM_x)
1325                 return -ENOTSUP;
1326
1327         if (filter_op == RTE_ETH_FILTER_NOP)
1328                 return 0;
1329
1330         if (arg == NULL && filter_op != RTE_ETH_FILTER_FLUSH)
1331                 return -EINVAL;
1332
1333         switch (filter_op) {
1334         case RTE_ETH_FILTER_ADD:
1335                 ret = ixgbe_add_del_fdir_filter(dev,
1336                         (struct rte_eth_fdir_filter *)arg, FALSE, FALSE);
1337                 break;
1338         case RTE_ETH_FILTER_UPDATE:
1339                 ret = ixgbe_add_del_fdir_filter(dev,
1340                         (struct rte_eth_fdir_filter *)arg, FALSE, TRUE);
1341                 break;
1342         case RTE_ETH_FILTER_DELETE:
1343                 ret = ixgbe_add_del_fdir_filter(dev,
1344                         (struct rte_eth_fdir_filter *)arg, TRUE, FALSE);
1345                 break;
1346         case RTE_ETH_FILTER_FLUSH:
1347                 ret = ixgbe_fdir_flush(dev);
1348                 break;
1349         case RTE_ETH_FILTER_INFO:
1350                 ixgbe_fdir_info_get(dev, (struct rte_eth_fdir_info *)arg);
1351                 break;
1352         case RTE_ETH_FILTER_STATS:
1353                 ixgbe_fdir_stats_get(dev, (struct rte_eth_fdir_stats *)arg);
1354                 break;
1355         default:
1356                 PMD_DRV_LOG(ERR, "unknown operation %u", filter_op);
1357                 ret = -EINVAL;
1358                 break;
1359         }
1360         return ret;
1361 }