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