67a4be8b7621b049817d17928d6602cadab96f17
[dpdk.git] / drivers / net / ixgbe / ixgbe_ethdev.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 <sys/queue.h>
35 #include <stdio.h>
36 #include <errno.h>
37 #include <stdint.h>
38 #include <string.h>
39 #include <unistd.h>
40 #include <stdarg.h>
41 #include <inttypes.h>
42 #include <netinet/in.h>
43 #include <rte_byteorder.h>
44 #include <rte_common.h>
45 #include <rte_cycles.h>
46
47 #include <rte_interrupts.h>
48 #include <rte_log.h>
49 #include <rte_debug.h>
50 #include <rte_pci.h>
51 #include <rte_atomic.h>
52 #include <rte_branch_prediction.h>
53 #include <rte_memory.h>
54 #include <rte_memzone.h>
55 #include <rte_eal.h>
56 #include <rte_alarm.h>
57 #include <rte_ether.h>
58 #include <rte_ethdev.h>
59 #include <rte_atomic.h>
60 #include <rte_malloc.h>
61 #include <rte_random.h>
62 #include <rte_dev.h>
63
64 #include "ixgbe_logs.h"
65 #include "base/ixgbe_api.h"
66 #include "base/ixgbe_vf.h"
67 #include "base/ixgbe_common.h"
68 #include "ixgbe_ethdev.h"
69 #include "ixgbe_bypass.h"
70 #include "ixgbe_rxtx.h"
71 #include "base/ixgbe_type.h"
72 #include "base/ixgbe_phy.h"
73 #include "ixgbe_regs.h"
74
75 /*
76  * High threshold controlling when to start sending XOFF frames. Must be at
77  * least 8 bytes less than receive packet buffer size. This value is in units
78  * of 1024 bytes.
79  */
80 #define IXGBE_FC_HI    0x80
81
82 /*
83  * Low threshold controlling when to start sending XON frames. This value is
84  * in units of 1024 bytes.
85  */
86 #define IXGBE_FC_LO    0x40
87
88 /* Default minimum inter-interrupt interval for EITR configuration */
89 #define IXGBE_MIN_INTER_INTERRUPT_INTERVAL_DEFAULT    0x79E
90
91 /* Timer value included in XOFF frames. */
92 #define IXGBE_FC_PAUSE 0x680
93
94 #define IXGBE_LINK_DOWN_CHECK_TIMEOUT 4000 /* ms */
95 #define IXGBE_LINK_UP_CHECK_TIMEOUT   1000 /* ms */
96 #define IXGBE_VMDQ_NUM_UC_MAC         4096 /* Maximum nb. of UC MAC addr. */
97
98 #define IXGBE_MMW_SIZE_DEFAULT        0x4
99 #define IXGBE_MMW_SIZE_JUMBO_FRAME    0x14
100 #define IXGBE_MAX_RING_DESC           4096 /* replicate define from rxtx */
101
102 /*
103  *  Default values for RX/TX configuration
104  */
105 #define IXGBE_DEFAULT_RX_FREE_THRESH  32
106 #define IXGBE_DEFAULT_RX_PTHRESH      8
107 #define IXGBE_DEFAULT_RX_HTHRESH      8
108 #define IXGBE_DEFAULT_RX_WTHRESH      0
109
110 #define IXGBE_DEFAULT_TX_FREE_THRESH  32
111 #define IXGBE_DEFAULT_TX_PTHRESH      32
112 #define IXGBE_DEFAULT_TX_HTHRESH      0
113 #define IXGBE_DEFAULT_TX_WTHRESH      0
114 #define IXGBE_DEFAULT_TX_RSBIT_THRESH 32
115
116 /* Bit shift and mask */
117 #define IXGBE_4_BIT_WIDTH  (CHAR_BIT / 2)
118 #define IXGBE_4_BIT_MASK   RTE_LEN2MASK(IXGBE_4_BIT_WIDTH, uint8_t)
119 #define IXGBE_8_BIT_WIDTH  CHAR_BIT
120 #define IXGBE_8_BIT_MASK   UINT8_MAX
121
122 #define IXGBEVF_PMD_NAME "rte_ixgbevf_pmd" /* PMD name */
123
124 #define IXGBE_QUEUE_STAT_COUNTERS (sizeof(hw_stats->qprc) / sizeof(hw_stats->qprc[0]))
125
126 #define IXGBE_HKEY_MAX_INDEX 10
127
128 /* Additional timesync values. */
129 #define NSEC_PER_SEC             1000000000L
130 #define IXGBE_INCVAL_10GB        0x66666666
131 #define IXGBE_INCVAL_1GB         0x40000000
132 #define IXGBE_INCVAL_100         0x50000000
133 #define IXGBE_INCVAL_SHIFT_10GB  28
134 #define IXGBE_INCVAL_SHIFT_1GB   24
135 #define IXGBE_INCVAL_SHIFT_100   21
136 #define IXGBE_INCVAL_SHIFT_82599 7
137 #define IXGBE_INCPER_SHIFT_82599 24
138
139 #define IXGBE_CYCLECOUNTER_MASK   0xffffffffffffffffULL
140
141 #define IXGBE_VT_CTL_POOLING_MODE_MASK         0x00030000
142 #define IXGBE_VT_CTL_POOLING_MODE_ETAG         0x00010000
143 #define DEFAULT_ETAG_ETYPE                     0x893f
144 #define IXGBE_ETAG_ETYPE                       0x00005084
145 #define IXGBE_ETAG_ETYPE_MASK                  0x0000ffff
146 #define IXGBE_ETAG_ETYPE_VALID                 0x80000000
147 #define IXGBE_RAH_ADTYPE                       0x40000000
148 #define IXGBE_RAL_ETAG_FILTER_MASK             0x00003fff
149 #define IXGBE_VMVIR_TAGA_MASK                  0x18000000
150 #define IXGBE_VMVIR_TAGA_ETAG_INSERT           0x08000000
151 #define IXGBE_VMTIR(_i) (0x00017000 + ((_i) * 4)) /* 64 of these (0-63) */
152 #define IXGBE_QDE_STRIP_TAG                    0x00000004
153
154 static int eth_ixgbe_dev_init(struct rte_eth_dev *eth_dev);
155 static int eth_ixgbe_dev_uninit(struct rte_eth_dev *eth_dev);
156 static int  ixgbe_dev_configure(struct rte_eth_dev *dev);
157 static int  ixgbe_dev_start(struct rte_eth_dev *dev);
158 static void ixgbe_dev_stop(struct rte_eth_dev *dev);
159 static int  ixgbe_dev_set_link_up(struct rte_eth_dev *dev);
160 static int  ixgbe_dev_set_link_down(struct rte_eth_dev *dev);
161 static void ixgbe_dev_close(struct rte_eth_dev *dev);
162 static void ixgbe_dev_promiscuous_enable(struct rte_eth_dev *dev);
163 static void ixgbe_dev_promiscuous_disable(struct rte_eth_dev *dev);
164 static void ixgbe_dev_allmulticast_enable(struct rte_eth_dev *dev);
165 static void ixgbe_dev_allmulticast_disable(struct rte_eth_dev *dev);
166 static int ixgbe_dev_link_update(struct rte_eth_dev *dev,
167                                 int wait_to_complete);
168 static void ixgbe_dev_stats_get(struct rte_eth_dev *dev,
169                                 struct rte_eth_stats *stats);
170 static int ixgbe_dev_xstats_get(struct rte_eth_dev *dev,
171                                 struct rte_eth_xstats *xstats, unsigned n);
172 static int ixgbevf_dev_xstats_get(struct rte_eth_dev *dev,
173                                   struct rte_eth_xstats *xstats, unsigned n);
174 static void ixgbe_dev_stats_reset(struct rte_eth_dev *dev);
175 static void ixgbe_dev_xstats_reset(struct rte_eth_dev *dev);
176 static int ixgbe_dev_queue_stats_mapping_set(struct rte_eth_dev *eth_dev,
177                                              uint16_t queue_id,
178                                              uint8_t stat_idx,
179                                              uint8_t is_rx);
180 static void ixgbe_dev_info_get(struct rte_eth_dev *dev,
181                                struct rte_eth_dev_info *dev_info);
182 static void ixgbevf_dev_info_get(struct rte_eth_dev *dev,
183                                  struct rte_eth_dev_info *dev_info);
184 static int ixgbe_dev_mtu_set(struct rte_eth_dev *dev, uint16_t mtu);
185
186 static int ixgbe_vlan_filter_set(struct rte_eth_dev *dev,
187                 uint16_t vlan_id, int on);
188 static int ixgbe_vlan_tpid_set(struct rte_eth_dev *dev,
189                                enum rte_vlan_type vlan_type,
190                                uint16_t tpid_id);
191 static void ixgbe_vlan_hw_strip_bitmap_set(struct rte_eth_dev *dev,
192                 uint16_t queue, bool on);
193 static void ixgbe_vlan_strip_queue_set(struct rte_eth_dev *dev, uint16_t queue,
194                 int on);
195 static void ixgbe_vlan_offload_set(struct rte_eth_dev *dev, int mask);
196 static void ixgbe_vlan_hw_strip_enable(struct rte_eth_dev *dev, uint16_t queue);
197 static void ixgbe_vlan_hw_strip_disable(struct rte_eth_dev *dev, uint16_t queue);
198 static void ixgbe_vlan_hw_extend_enable(struct rte_eth_dev *dev);
199 static void ixgbe_vlan_hw_extend_disable(struct rte_eth_dev *dev);
200
201 static int ixgbe_dev_led_on(struct rte_eth_dev *dev);
202 static int ixgbe_dev_led_off(struct rte_eth_dev *dev);
203 static int ixgbe_flow_ctrl_get(struct rte_eth_dev *dev,
204                                struct rte_eth_fc_conf *fc_conf);
205 static int ixgbe_flow_ctrl_set(struct rte_eth_dev *dev,
206                                struct rte_eth_fc_conf *fc_conf);
207 static int ixgbe_priority_flow_ctrl_set(struct rte_eth_dev *dev,
208                 struct rte_eth_pfc_conf *pfc_conf);
209 static int ixgbe_dev_rss_reta_update(struct rte_eth_dev *dev,
210                         struct rte_eth_rss_reta_entry64 *reta_conf,
211                         uint16_t reta_size);
212 static int ixgbe_dev_rss_reta_query(struct rte_eth_dev *dev,
213                         struct rte_eth_rss_reta_entry64 *reta_conf,
214                         uint16_t reta_size);
215 static void ixgbe_dev_link_status_print(struct rte_eth_dev *dev);
216 static int ixgbe_dev_lsc_interrupt_setup(struct rte_eth_dev *dev);
217 static int ixgbe_dev_rxq_interrupt_setup(struct rte_eth_dev *dev);
218 static int ixgbe_dev_interrupt_get_status(struct rte_eth_dev *dev);
219 static int ixgbe_dev_interrupt_action(struct rte_eth_dev *dev);
220 static void ixgbe_dev_interrupt_handler(struct rte_intr_handle *handle,
221                 void *param);
222 static void ixgbe_dev_interrupt_delayed_handler(void *param);
223 static void ixgbe_add_rar(struct rte_eth_dev *dev, struct ether_addr *mac_addr,
224                 uint32_t index, uint32_t pool);
225 static void ixgbe_remove_rar(struct rte_eth_dev *dev, uint32_t index);
226 static void ixgbe_set_default_mac_addr(struct rte_eth_dev *dev,
227                                            struct ether_addr *mac_addr);
228 static void ixgbe_dcb_init(struct ixgbe_hw *hw,struct ixgbe_dcb_config *dcb_config);
229
230 /* For Virtual Function support */
231 static int eth_ixgbevf_dev_init(struct rte_eth_dev *eth_dev);
232 static int eth_ixgbevf_dev_uninit(struct rte_eth_dev *eth_dev);
233 static int  ixgbevf_dev_configure(struct rte_eth_dev *dev);
234 static int  ixgbevf_dev_start(struct rte_eth_dev *dev);
235 static void ixgbevf_dev_stop(struct rte_eth_dev *dev);
236 static void ixgbevf_dev_close(struct rte_eth_dev *dev);
237 static void ixgbevf_intr_disable(struct ixgbe_hw *hw);
238 static void ixgbevf_intr_enable(struct ixgbe_hw *hw);
239 static void ixgbevf_dev_stats_get(struct rte_eth_dev *dev,
240                 struct rte_eth_stats *stats);
241 static void ixgbevf_dev_stats_reset(struct rte_eth_dev *dev);
242 static int ixgbevf_vlan_filter_set(struct rte_eth_dev *dev,
243                 uint16_t vlan_id, int on);
244 static void ixgbevf_vlan_strip_queue_set(struct rte_eth_dev *dev,
245                 uint16_t queue, int on);
246 static void ixgbevf_vlan_offload_set(struct rte_eth_dev *dev, int mask);
247 static void ixgbevf_set_vfta_all(struct rte_eth_dev *dev, bool on);
248 static int ixgbevf_dev_rx_queue_intr_enable(struct rte_eth_dev *dev,
249                                             uint16_t queue_id);
250 static int ixgbevf_dev_rx_queue_intr_disable(struct rte_eth_dev *dev,
251                                              uint16_t queue_id);
252 static void ixgbevf_set_ivar_map(struct ixgbe_hw *hw, int8_t direction,
253                                  uint8_t queue, uint8_t msix_vector);
254 static void ixgbevf_configure_msix(struct rte_eth_dev *dev);
255
256 /* For Eth VMDQ APIs support */
257 static int ixgbe_uc_hash_table_set(struct rte_eth_dev *dev, struct
258                 ether_addr* mac_addr,uint8_t on);
259 static int ixgbe_uc_all_hash_table_set(struct rte_eth_dev *dev,uint8_t on);
260 static int  ixgbe_set_pool_rx_mode(struct rte_eth_dev *dev,  uint16_t pool,
261                 uint16_t rx_mask, uint8_t on);
262 static int ixgbe_set_pool_rx(struct rte_eth_dev *dev,uint16_t pool,uint8_t on);
263 static int ixgbe_set_pool_tx(struct rte_eth_dev *dev,uint16_t pool,uint8_t on);
264 static int ixgbe_set_pool_vlan_filter(struct rte_eth_dev *dev, uint16_t vlan,
265                 uint64_t pool_mask,uint8_t vlan_on);
266 static int ixgbe_mirror_rule_set(struct rte_eth_dev *dev,
267                 struct rte_eth_mirror_conf *mirror_conf,
268                 uint8_t rule_id, uint8_t on);
269 static int ixgbe_mirror_rule_reset(struct rte_eth_dev *dev,
270                 uint8_t rule_id);
271 static int ixgbe_dev_rx_queue_intr_enable(struct rte_eth_dev *dev,
272                                           uint16_t queue_id);
273 static int ixgbe_dev_rx_queue_intr_disable(struct rte_eth_dev *dev,
274                                            uint16_t queue_id);
275 static void ixgbe_set_ivar_map(struct ixgbe_hw *hw, int8_t direction,
276                                uint8_t queue, uint8_t msix_vector);
277 static void ixgbe_configure_msix(struct rte_eth_dev *dev);
278
279 static int ixgbe_set_queue_rate_limit(struct rte_eth_dev *dev,
280                 uint16_t queue_idx, uint16_t tx_rate);
281 static int ixgbe_set_vf_rate_limit(struct rte_eth_dev *dev, uint16_t vf,
282                 uint16_t tx_rate, uint64_t q_msk);
283
284 static void ixgbevf_add_mac_addr(struct rte_eth_dev *dev,
285                                  struct ether_addr *mac_addr,
286                                  uint32_t index, uint32_t pool);
287 static void ixgbevf_remove_mac_addr(struct rte_eth_dev *dev, uint32_t index);
288 static void ixgbevf_set_default_mac_addr(struct rte_eth_dev *dev,
289                                              struct ether_addr *mac_addr);
290 static int ixgbe_syn_filter_set(struct rte_eth_dev *dev,
291                         struct rte_eth_syn_filter *filter,
292                         bool add);
293 static int ixgbe_syn_filter_get(struct rte_eth_dev *dev,
294                         struct rte_eth_syn_filter *filter);
295 static int ixgbe_syn_filter_handle(struct rte_eth_dev *dev,
296                         enum rte_filter_op filter_op,
297                         void *arg);
298 static int ixgbe_add_5tuple_filter(struct rte_eth_dev *dev,
299                         struct ixgbe_5tuple_filter *filter);
300 static void ixgbe_remove_5tuple_filter(struct rte_eth_dev *dev,
301                         struct ixgbe_5tuple_filter *filter);
302 static int ixgbe_add_del_ntuple_filter(struct rte_eth_dev *dev,
303                         struct rte_eth_ntuple_filter *filter,
304                         bool add);
305 static int ixgbe_ntuple_filter_handle(struct rte_eth_dev *dev,
306                                 enum rte_filter_op filter_op,
307                                 void *arg);
308 static int ixgbe_get_ntuple_filter(struct rte_eth_dev *dev,
309                         struct rte_eth_ntuple_filter *filter);
310 static int ixgbe_add_del_ethertype_filter(struct rte_eth_dev *dev,
311                         struct rte_eth_ethertype_filter *filter,
312                         bool add);
313 static int ixgbe_ethertype_filter_handle(struct rte_eth_dev *dev,
314                                 enum rte_filter_op filter_op,
315                                 void *arg);
316 static int ixgbe_get_ethertype_filter(struct rte_eth_dev *dev,
317                         struct rte_eth_ethertype_filter *filter);
318 static int ixgbe_dev_filter_ctrl(struct rte_eth_dev *dev,
319                      enum rte_filter_type filter_type,
320                      enum rte_filter_op filter_op,
321                      void *arg);
322 static int ixgbevf_dev_set_mtu(struct rte_eth_dev *dev, uint16_t mtu);
323
324 static int ixgbe_dev_set_mc_addr_list(struct rte_eth_dev *dev,
325                                       struct ether_addr *mc_addr_set,
326                                       uint32_t nb_mc_addr);
327 static int ixgbe_dev_get_dcb_info(struct rte_eth_dev *dev,
328                                    struct rte_eth_dcb_info *dcb_info);
329
330 static int ixgbe_get_reg_length(struct rte_eth_dev *dev);
331 static int ixgbe_get_regs(struct rte_eth_dev *dev,
332                             struct rte_dev_reg_info *regs);
333 static int ixgbe_get_eeprom_length(struct rte_eth_dev *dev);
334 static int ixgbe_get_eeprom(struct rte_eth_dev *dev,
335                                 struct rte_dev_eeprom_info *eeprom);
336 static int ixgbe_set_eeprom(struct rte_eth_dev *dev,
337                                 struct rte_dev_eeprom_info *eeprom);
338
339 static int ixgbevf_get_reg_length(struct rte_eth_dev *dev);
340 static int ixgbevf_get_regs(struct rte_eth_dev *dev,
341                                 struct rte_dev_reg_info *regs);
342
343 static int ixgbe_timesync_enable(struct rte_eth_dev *dev);
344 static int ixgbe_timesync_disable(struct rte_eth_dev *dev);
345 static int ixgbe_timesync_read_rx_timestamp(struct rte_eth_dev *dev,
346                                             struct timespec *timestamp,
347                                             uint32_t flags);
348 static int ixgbe_timesync_read_tx_timestamp(struct rte_eth_dev *dev,
349                                             struct timespec *timestamp);
350 static int ixgbe_timesync_adjust_time(struct rte_eth_dev *dev, int64_t delta);
351 static int ixgbe_timesync_read_time(struct rte_eth_dev *dev,
352                                    struct timespec *timestamp);
353 static int ixgbe_timesync_write_time(struct rte_eth_dev *dev,
354                                    const struct timespec *timestamp);
355
356 static int ixgbe_dev_l2_tunnel_eth_type_conf
357         (struct rte_eth_dev *dev, struct rte_eth_l2_tunnel_conf *l2_tunnel);
358 static int ixgbe_dev_l2_tunnel_offload_set
359         (struct rte_eth_dev *dev,
360          struct rte_eth_l2_tunnel_conf *l2_tunnel,
361          uint32_t mask,
362          uint8_t en);
363 static int ixgbe_dev_l2_tunnel_filter_handle(struct rte_eth_dev *dev,
364                                              enum rte_filter_op filter_op,
365                                              void *arg);
366
367 static int ixgbe_dev_udp_tunnel_port_add(struct rte_eth_dev *dev,
368                                          struct rte_eth_udp_tunnel *udp_tunnel);
369 static int ixgbe_dev_udp_tunnel_port_del(struct rte_eth_dev *dev,
370                                          struct rte_eth_udp_tunnel *udp_tunnel);
371
372 /*
373  * Define VF Stats MACRO for Non "cleared on read" register
374  */
375 #define UPDATE_VF_STAT(reg, last, cur)                          \
376 {                                                               \
377         uint32_t latest = IXGBE_READ_REG(hw, reg);              \
378         cur += (latest - last) & UINT_MAX;                      \
379         last = latest;                                          \
380 }
381
382 #define UPDATE_VF_STAT_36BIT(lsb, msb, last, cur)                \
383 {                                                                \
384         u64 new_lsb = IXGBE_READ_REG(hw, lsb);                   \
385         u64 new_msb = IXGBE_READ_REG(hw, msb);                   \
386         u64 latest = ((new_msb << 32) | new_lsb);                \
387         cur += (0x1000000000LL + latest - last) & 0xFFFFFFFFFLL; \
388         last = latest;                                           \
389 }
390
391 #define IXGBE_SET_HWSTRIP(h, q) do{\
392                 uint32_t idx = (q) / (sizeof ((h)->bitmap[0]) * NBBY); \
393                 uint32_t bit = (q) % (sizeof ((h)->bitmap[0]) * NBBY); \
394                 (h)->bitmap[idx] |= 1 << bit;\
395         }while(0)
396
397 #define IXGBE_CLEAR_HWSTRIP(h, q) do{\
398                 uint32_t idx = (q) / (sizeof ((h)->bitmap[0]) * NBBY); \
399                 uint32_t bit = (q) % (sizeof ((h)->bitmap[0]) * NBBY); \
400                 (h)->bitmap[idx] &= ~(1 << bit);\
401         }while(0)
402
403 #define IXGBE_GET_HWSTRIP(h, q, r) do{\
404                 uint32_t idx = (q) / (sizeof ((h)->bitmap[0]) * NBBY); \
405                 uint32_t bit = (q) % (sizeof ((h)->bitmap[0]) * NBBY); \
406                 (r) = (h)->bitmap[idx] >> bit & 1;\
407         }while(0)
408
409 /*
410  * The set of PCI devices this driver supports
411  */
412 static const struct rte_pci_id pci_id_ixgbe_map[] = {
413
414 #define RTE_PCI_DEV_ID_DECL_IXGBE(vend, dev) {RTE_PCI_DEVICE(vend, dev)},
415 #include "rte_pci_dev_ids.h"
416
417 { .vendor_id = 0, /* sentinel */ },
418 };
419
420
421 /*
422  * The set of PCI devices this driver supports (for 82599 VF)
423  */
424 static const struct rte_pci_id pci_id_ixgbevf_map[] = {
425
426 #define RTE_PCI_DEV_ID_DECL_IXGBEVF(vend, dev) {RTE_PCI_DEVICE(vend, dev)},
427 #include "rte_pci_dev_ids.h"
428 { .vendor_id = 0, /* sentinel */ },
429
430 };
431
432 static const struct rte_eth_desc_lim rx_desc_lim = {
433         .nb_max = IXGBE_MAX_RING_DESC,
434         .nb_min = IXGBE_MIN_RING_DESC,
435         .nb_align = IXGBE_RXD_ALIGN,
436 };
437
438 static const struct rte_eth_desc_lim tx_desc_lim = {
439         .nb_max = IXGBE_MAX_RING_DESC,
440         .nb_min = IXGBE_MIN_RING_DESC,
441         .nb_align = IXGBE_TXD_ALIGN,
442 };
443
444 static const struct eth_dev_ops ixgbe_eth_dev_ops = {
445         .dev_configure        = ixgbe_dev_configure,
446         .dev_start            = ixgbe_dev_start,
447         .dev_stop             = ixgbe_dev_stop,
448         .dev_set_link_up    = ixgbe_dev_set_link_up,
449         .dev_set_link_down  = ixgbe_dev_set_link_down,
450         .dev_close            = ixgbe_dev_close,
451         .promiscuous_enable   = ixgbe_dev_promiscuous_enable,
452         .promiscuous_disable  = ixgbe_dev_promiscuous_disable,
453         .allmulticast_enable  = ixgbe_dev_allmulticast_enable,
454         .allmulticast_disable = ixgbe_dev_allmulticast_disable,
455         .link_update          = ixgbe_dev_link_update,
456         .stats_get            = ixgbe_dev_stats_get,
457         .xstats_get           = ixgbe_dev_xstats_get,
458         .stats_reset          = ixgbe_dev_stats_reset,
459         .xstats_reset         = ixgbe_dev_xstats_reset,
460         .queue_stats_mapping_set = ixgbe_dev_queue_stats_mapping_set,
461         .dev_infos_get        = ixgbe_dev_info_get,
462         .mtu_set              = ixgbe_dev_mtu_set,
463         .vlan_filter_set      = ixgbe_vlan_filter_set,
464         .vlan_tpid_set        = ixgbe_vlan_tpid_set,
465         .vlan_offload_set     = ixgbe_vlan_offload_set,
466         .vlan_strip_queue_set = ixgbe_vlan_strip_queue_set,
467         .rx_queue_start       = ixgbe_dev_rx_queue_start,
468         .rx_queue_stop        = ixgbe_dev_rx_queue_stop,
469         .tx_queue_start       = ixgbe_dev_tx_queue_start,
470         .tx_queue_stop        = ixgbe_dev_tx_queue_stop,
471         .rx_queue_setup       = ixgbe_dev_rx_queue_setup,
472         .rx_queue_intr_enable = ixgbe_dev_rx_queue_intr_enable,
473         .rx_queue_intr_disable = ixgbe_dev_rx_queue_intr_disable,
474         .rx_queue_release     = ixgbe_dev_rx_queue_release,
475         .rx_queue_count       = ixgbe_dev_rx_queue_count,
476         .rx_descriptor_done   = ixgbe_dev_rx_descriptor_done,
477         .tx_queue_setup       = ixgbe_dev_tx_queue_setup,
478         .tx_queue_release     = ixgbe_dev_tx_queue_release,
479         .dev_led_on           = ixgbe_dev_led_on,
480         .dev_led_off          = ixgbe_dev_led_off,
481         .flow_ctrl_get        = ixgbe_flow_ctrl_get,
482         .flow_ctrl_set        = ixgbe_flow_ctrl_set,
483         .priority_flow_ctrl_set = ixgbe_priority_flow_ctrl_set,
484         .mac_addr_add         = ixgbe_add_rar,
485         .mac_addr_remove      = ixgbe_remove_rar,
486         .mac_addr_set         = ixgbe_set_default_mac_addr,
487         .uc_hash_table_set    = ixgbe_uc_hash_table_set,
488         .uc_all_hash_table_set  = ixgbe_uc_all_hash_table_set,
489         .mirror_rule_set      = ixgbe_mirror_rule_set,
490         .mirror_rule_reset    = ixgbe_mirror_rule_reset,
491         .set_vf_rx_mode       = ixgbe_set_pool_rx_mode,
492         .set_vf_rx            = ixgbe_set_pool_rx,
493         .set_vf_tx            = ixgbe_set_pool_tx,
494         .set_vf_vlan_filter   = ixgbe_set_pool_vlan_filter,
495         .set_queue_rate_limit = ixgbe_set_queue_rate_limit,
496         .set_vf_rate_limit    = ixgbe_set_vf_rate_limit,
497         .reta_update          = ixgbe_dev_rss_reta_update,
498         .reta_query           = ixgbe_dev_rss_reta_query,
499 #ifdef RTE_NIC_BYPASS
500         .bypass_init          = ixgbe_bypass_init,
501         .bypass_state_set     = ixgbe_bypass_state_store,
502         .bypass_state_show    = ixgbe_bypass_state_show,
503         .bypass_event_set     = ixgbe_bypass_event_store,
504         .bypass_event_show    = ixgbe_bypass_event_show,
505         .bypass_wd_timeout_set  = ixgbe_bypass_wd_timeout_store,
506         .bypass_wd_timeout_show = ixgbe_bypass_wd_timeout_show,
507         .bypass_ver_show      = ixgbe_bypass_ver_show,
508         .bypass_wd_reset      = ixgbe_bypass_wd_reset,
509 #endif /* RTE_NIC_BYPASS */
510         .rss_hash_update      = ixgbe_dev_rss_hash_update,
511         .rss_hash_conf_get    = ixgbe_dev_rss_hash_conf_get,
512         .filter_ctrl          = ixgbe_dev_filter_ctrl,
513         .set_mc_addr_list     = ixgbe_dev_set_mc_addr_list,
514         .rxq_info_get         = ixgbe_rxq_info_get,
515         .txq_info_get         = ixgbe_txq_info_get,
516         .timesync_enable      = ixgbe_timesync_enable,
517         .timesync_disable     = ixgbe_timesync_disable,
518         .timesync_read_rx_timestamp = ixgbe_timesync_read_rx_timestamp,
519         .timesync_read_tx_timestamp = ixgbe_timesync_read_tx_timestamp,
520         .get_reg_length       = ixgbe_get_reg_length,
521         .get_reg              = ixgbe_get_regs,
522         .get_eeprom_length    = ixgbe_get_eeprom_length,
523         .get_eeprom           = ixgbe_get_eeprom,
524         .set_eeprom           = ixgbe_set_eeprom,
525         .get_dcb_info         = ixgbe_dev_get_dcb_info,
526         .timesync_adjust_time = ixgbe_timesync_adjust_time,
527         .timesync_read_time   = ixgbe_timesync_read_time,
528         .timesync_write_time  = ixgbe_timesync_write_time,
529         .l2_tunnel_eth_type_conf = ixgbe_dev_l2_tunnel_eth_type_conf,
530         .l2_tunnel_offload_set   = ixgbe_dev_l2_tunnel_offload_set,
531         .udp_tunnel_port_add  = ixgbe_dev_udp_tunnel_port_add,
532         .udp_tunnel_port_del  = ixgbe_dev_udp_tunnel_port_del,
533 };
534
535 /*
536  * dev_ops for virtual function, bare necessities for basic vf
537  * operation have been implemented
538  */
539 static const struct eth_dev_ops ixgbevf_eth_dev_ops = {
540         .dev_configure        = ixgbevf_dev_configure,
541         .dev_start            = ixgbevf_dev_start,
542         .dev_stop             = ixgbevf_dev_stop,
543         .link_update          = ixgbe_dev_link_update,
544         .stats_get            = ixgbevf_dev_stats_get,
545         .xstats_get           = ixgbevf_dev_xstats_get,
546         .stats_reset          = ixgbevf_dev_stats_reset,
547         .xstats_reset         = ixgbevf_dev_stats_reset,
548         .dev_close            = ixgbevf_dev_close,
549         .dev_infos_get        = ixgbevf_dev_info_get,
550         .mtu_set              = ixgbevf_dev_set_mtu,
551         .vlan_filter_set      = ixgbevf_vlan_filter_set,
552         .vlan_strip_queue_set = ixgbevf_vlan_strip_queue_set,
553         .vlan_offload_set     = ixgbevf_vlan_offload_set,
554         .rx_queue_setup       = ixgbe_dev_rx_queue_setup,
555         .rx_queue_release     = ixgbe_dev_rx_queue_release,
556         .rx_descriptor_done   = ixgbe_dev_rx_descriptor_done,
557         .tx_queue_setup       = ixgbe_dev_tx_queue_setup,
558         .tx_queue_release     = ixgbe_dev_tx_queue_release,
559         .rx_queue_intr_enable = ixgbevf_dev_rx_queue_intr_enable,
560         .rx_queue_intr_disable = ixgbevf_dev_rx_queue_intr_disable,
561         .mac_addr_add         = ixgbevf_add_mac_addr,
562         .mac_addr_remove      = ixgbevf_remove_mac_addr,
563         .set_mc_addr_list     = ixgbe_dev_set_mc_addr_list,
564         .rxq_info_get         = ixgbe_rxq_info_get,
565         .txq_info_get         = ixgbe_txq_info_get,
566         .mac_addr_set         = ixgbevf_set_default_mac_addr,
567         .get_reg_length       = ixgbevf_get_reg_length,
568         .get_reg              = ixgbevf_get_regs,
569         .reta_update          = ixgbe_dev_rss_reta_update,
570         .reta_query           = ixgbe_dev_rss_reta_query,
571         .rss_hash_update      = ixgbe_dev_rss_hash_update,
572         .rss_hash_conf_get    = ixgbe_dev_rss_hash_conf_get,
573 };
574
575 /* store statistics names and its offset in stats structure */
576 struct rte_ixgbe_xstats_name_off {
577         char name[RTE_ETH_XSTATS_NAME_SIZE];
578         unsigned offset;
579 };
580
581 static const struct rte_ixgbe_xstats_name_off rte_ixgbe_stats_strings[] = {
582         {"rx_crc_errors", offsetof(struct ixgbe_hw_stats, crcerrs)},
583         {"rx_illegal_byte_errors", offsetof(struct ixgbe_hw_stats, illerrc)},
584         {"rx_error_bytes", offsetof(struct ixgbe_hw_stats, errbc)},
585         {"mac_local_errors", offsetof(struct ixgbe_hw_stats, mlfc)},
586         {"mac_remote_errors", offsetof(struct ixgbe_hw_stats, mrfc)},
587         {"rx_length_errors", offsetof(struct ixgbe_hw_stats, rlec)},
588         {"tx_xon_packets", offsetof(struct ixgbe_hw_stats, lxontxc)},
589         {"rx_xon_packets", offsetof(struct ixgbe_hw_stats, lxonrxc)},
590         {"tx_xoff_packets", offsetof(struct ixgbe_hw_stats, lxofftxc)},
591         {"rx_xoff_packets", offsetof(struct ixgbe_hw_stats, lxoffrxc)},
592         {"rx_size_64_packets", offsetof(struct ixgbe_hw_stats, prc64)},
593         {"rx_size_65_to_127_packets", offsetof(struct ixgbe_hw_stats, prc127)},
594         {"rx_size_128_to_255_packets", offsetof(struct ixgbe_hw_stats, prc255)},
595         {"rx_size_256_to_511_packets", offsetof(struct ixgbe_hw_stats, prc511)},
596         {"rx_size_512_to_1023_packets", offsetof(struct ixgbe_hw_stats,
597                 prc1023)},
598         {"rx_size_1024_to_max_packets", offsetof(struct ixgbe_hw_stats,
599                 prc1522)},
600         {"rx_broadcast_packets", offsetof(struct ixgbe_hw_stats, bprc)},
601         {"rx_multicast_packets", offsetof(struct ixgbe_hw_stats, mprc)},
602         {"rx_fragment_errors", offsetof(struct ixgbe_hw_stats, rfc)},
603         {"rx_undersize_errors", offsetof(struct ixgbe_hw_stats, ruc)},
604         {"rx_oversize_errors", offsetof(struct ixgbe_hw_stats, roc)},
605         {"rx_jabber_errors", offsetof(struct ixgbe_hw_stats, rjc)},
606         {"rx_management_packets", offsetof(struct ixgbe_hw_stats, mngprc)},
607         {"rx_management_dropped", offsetof(struct ixgbe_hw_stats, mngpdc)},
608         {"tx_management_packets", offsetof(struct ixgbe_hw_stats, mngptc)},
609         {"rx_total_packets", offsetof(struct ixgbe_hw_stats, tpr)},
610         {"rx_total_bytes", offsetof(struct ixgbe_hw_stats, tor)},
611         {"tx_total_packets", offsetof(struct ixgbe_hw_stats, tpt)},
612         {"tx_size_64_packets", offsetof(struct ixgbe_hw_stats, ptc64)},
613         {"tx_size_65_to_127_packets", offsetof(struct ixgbe_hw_stats, ptc127)},
614         {"tx_size_128_to_255_packets", offsetof(struct ixgbe_hw_stats, ptc255)},
615         {"tx_size_256_to_511_packets", offsetof(struct ixgbe_hw_stats, ptc511)},
616         {"tx_size_512_to_1023_packets", offsetof(struct ixgbe_hw_stats,
617                 ptc1023)},
618         {"tx_size_1024_to_max_packets", offsetof(struct ixgbe_hw_stats,
619                 ptc1522)},
620         {"tx_multicast_packets", offsetof(struct ixgbe_hw_stats, mptc)},
621         {"tx_broadcast_packets", offsetof(struct ixgbe_hw_stats, bptc)},
622         {"rx_mac_short_packet_dropped", offsetof(struct ixgbe_hw_stats, mspdc)},
623         {"rx_l3_l4_xsum_error", offsetof(struct ixgbe_hw_stats, xec)},
624
625         {"flow_director_added_filters", offsetof(struct ixgbe_hw_stats,
626                 fdirustat_add)},
627         {"flow_director_removed_filters", offsetof(struct ixgbe_hw_stats,
628                 fdirustat_remove)},
629         {"flow_director_filter_add_errors", offsetof(struct ixgbe_hw_stats,
630                 fdirfstat_fadd)},
631         {"flow_director_filter_remove_errors", offsetof(struct ixgbe_hw_stats,
632                 fdirfstat_fremove)},
633         {"flow_director_matched_filters", offsetof(struct ixgbe_hw_stats,
634                 fdirmatch)},
635         {"flow_director_missed_filters", offsetof(struct ixgbe_hw_stats,
636                 fdirmiss)},
637
638         {"rx_fcoe_crc_errors", offsetof(struct ixgbe_hw_stats, fccrc)},
639         {"rx_fcoe_dropped", offsetof(struct ixgbe_hw_stats, fcoerpdc)},
640         {"rx_fcoe_mbuf_allocation_errors", offsetof(struct ixgbe_hw_stats,
641                 fclast)},
642         {"rx_fcoe_packets", offsetof(struct ixgbe_hw_stats, fcoeprc)},
643         {"tx_fcoe_packets", offsetof(struct ixgbe_hw_stats, fcoeptc)},
644         {"rx_fcoe_bytes", offsetof(struct ixgbe_hw_stats, fcoedwrc)},
645         {"tx_fcoe_bytes", offsetof(struct ixgbe_hw_stats, fcoedwtc)},
646         {"rx_fcoe_no_direct_data_placement", offsetof(struct ixgbe_hw_stats,
647                 fcoe_noddp)},
648         {"rx_fcoe_no_direct_data_placement_ext_buff",
649                 offsetof(struct ixgbe_hw_stats, fcoe_noddp_ext_buff)},
650
651         {"tx_flow_control_xon_packets", offsetof(struct ixgbe_hw_stats,
652                 lxontxc)},
653         {"rx_flow_control_xon_packets", offsetof(struct ixgbe_hw_stats,
654                 lxonrxc)},
655         {"tx_flow_control_xoff_packets", offsetof(struct ixgbe_hw_stats,
656                 lxofftxc)},
657         {"rx_flow_control_xoff_packets", offsetof(struct ixgbe_hw_stats,
658                 lxoffrxc)},
659         {"rx_total_missed_packets", offsetof(struct ixgbe_hw_stats, mpctotal)},
660 };
661
662 #define IXGBE_NB_HW_STATS (sizeof(rte_ixgbe_stats_strings) / \
663                            sizeof(rte_ixgbe_stats_strings[0]))
664
665 /* Per-queue statistics */
666 static const struct rte_ixgbe_xstats_name_off rte_ixgbe_rxq_strings[] = {
667         {"mbuf_allocation_errors", offsetof(struct ixgbe_hw_stats, rnbc)},
668         {"dropped", offsetof(struct ixgbe_hw_stats, mpc)},
669         {"xon_packets", offsetof(struct ixgbe_hw_stats, pxonrxc)},
670         {"xoff_packets", offsetof(struct ixgbe_hw_stats, pxoffrxc)},
671 };
672
673 #define IXGBE_NB_RXQ_PRIO_STATS (sizeof(rte_ixgbe_rxq_strings) / \
674                            sizeof(rte_ixgbe_rxq_strings[0]))
675
676 static const struct rte_ixgbe_xstats_name_off rte_ixgbe_txq_strings[] = {
677         {"xon_packets", offsetof(struct ixgbe_hw_stats, pxontxc)},
678         {"xoff_packets", offsetof(struct ixgbe_hw_stats, pxofftxc)},
679         {"xon_to_xoff_packets", offsetof(struct ixgbe_hw_stats,
680                 pxon2offc)},
681 };
682
683 #define IXGBE_NB_TXQ_PRIO_STATS (sizeof(rte_ixgbe_txq_strings) / \
684                            sizeof(rte_ixgbe_txq_strings[0]))
685
686 static const struct rte_ixgbe_xstats_name_off rte_ixgbevf_stats_strings[] = {
687         {"rx_multicast_packets", offsetof(struct ixgbevf_hw_stats, vfmprc)},
688 };
689
690 #define IXGBEVF_NB_XSTATS (sizeof(rte_ixgbevf_stats_strings) /  \
691                 sizeof(rte_ixgbevf_stats_strings[0]))
692
693 /**
694  * Atomically reads the link status information from global
695  * structure rte_eth_dev.
696  *
697  * @param dev
698  *   - Pointer to the structure rte_eth_dev to read from.
699  *   - Pointer to the buffer to be saved with the link status.
700  *
701  * @return
702  *   - On success, zero.
703  *   - On failure, negative value.
704  */
705 static inline int
706 rte_ixgbe_dev_atomic_read_link_status(struct rte_eth_dev *dev,
707                                 struct rte_eth_link *link)
708 {
709         struct rte_eth_link *dst = link;
710         struct rte_eth_link *src = &(dev->data->dev_link);
711
712         if (rte_atomic64_cmpset((uint64_t *)dst, *(uint64_t *)dst,
713                                         *(uint64_t *)src) == 0)
714                 return -1;
715
716         return 0;
717 }
718
719 /**
720  * Atomically writes the link status information into global
721  * structure rte_eth_dev.
722  *
723  * @param dev
724  *   - Pointer to the structure rte_eth_dev to read from.
725  *   - Pointer to the buffer to be saved with the link status.
726  *
727  * @return
728  *   - On success, zero.
729  *   - On failure, negative value.
730  */
731 static inline int
732 rte_ixgbe_dev_atomic_write_link_status(struct rte_eth_dev *dev,
733                                 struct rte_eth_link *link)
734 {
735         struct rte_eth_link *dst = &(dev->data->dev_link);
736         struct rte_eth_link *src = link;
737
738         if (rte_atomic64_cmpset((uint64_t *)dst, *(uint64_t *)dst,
739                                         *(uint64_t *)src) == 0)
740                 return -1;
741
742         return 0;
743 }
744
745 /*
746  * This function is the same as ixgbe_is_sfp() in base/ixgbe.h.
747  */
748 static inline int
749 ixgbe_is_sfp(struct ixgbe_hw *hw)
750 {
751         switch (hw->phy.type) {
752         case ixgbe_phy_sfp_avago:
753         case ixgbe_phy_sfp_ftl:
754         case ixgbe_phy_sfp_intel:
755         case ixgbe_phy_sfp_unknown:
756         case ixgbe_phy_sfp_passive_tyco:
757         case ixgbe_phy_sfp_passive_unknown:
758                 return 1;
759         default:
760                 return 0;
761         }
762 }
763
764 static inline int32_t
765 ixgbe_pf_reset_hw(struct ixgbe_hw *hw)
766 {
767         uint32_t ctrl_ext;
768         int32_t status;
769
770         status = ixgbe_reset_hw(hw);
771
772         ctrl_ext = IXGBE_READ_REG(hw, IXGBE_CTRL_EXT);
773         /* Set PF Reset Done bit so PF/VF Mail Ops can work */
774         ctrl_ext |= IXGBE_CTRL_EXT_PFRSTD;
775         IXGBE_WRITE_REG(hw, IXGBE_CTRL_EXT, ctrl_ext);
776         IXGBE_WRITE_FLUSH(hw);
777
778         return status;
779 }
780
781 static inline void
782 ixgbe_enable_intr(struct rte_eth_dev *dev)
783 {
784         struct ixgbe_interrupt *intr =
785                 IXGBE_DEV_PRIVATE_TO_INTR(dev->data->dev_private);
786         struct ixgbe_hw *hw =
787                 IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
788
789         IXGBE_WRITE_REG(hw, IXGBE_EIMS, intr->mask);
790         IXGBE_WRITE_FLUSH(hw);
791 }
792
793 /*
794  * This function is based on ixgbe_disable_intr() in base/ixgbe.h.
795  */
796 static void
797 ixgbe_disable_intr(struct ixgbe_hw *hw)
798 {
799         PMD_INIT_FUNC_TRACE();
800
801         if (hw->mac.type == ixgbe_mac_82598EB) {
802                 IXGBE_WRITE_REG(hw, IXGBE_EIMC, ~0);
803         } else {
804                 IXGBE_WRITE_REG(hw, IXGBE_EIMC, 0xFFFF0000);
805                 IXGBE_WRITE_REG(hw, IXGBE_EIMC_EX(0), ~0);
806                 IXGBE_WRITE_REG(hw, IXGBE_EIMC_EX(1), ~0);
807         }
808         IXGBE_WRITE_FLUSH(hw);
809 }
810
811 /*
812  * This function resets queue statistics mapping registers.
813  * From Niantic datasheet, Initialization of Statistics section:
814  * "...if software requires the queue counters, the RQSMR and TQSM registers
815  * must be re-programmed following a device reset.
816  */
817 static void
818 ixgbe_reset_qstat_mappings(struct ixgbe_hw *hw)
819 {
820         uint32_t i;
821
822         for(i = 0; i != IXGBE_NB_STAT_MAPPING_REGS; i++) {
823                 IXGBE_WRITE_REG(hw, IXGBE_RQSMR(i), 0);
824                 IXGBE_WRITE_REG(hw, IXGBE_TQSM(i), 0);
825         }
826 }
827
828
829 static int
830 ixgbe_dev_queue_stats_mapping_set(struct rte_eth_dev *eth_dev,
831                                   uint16_t queue_id,
832                                   uint8_t stat_idx,
833                                   uint8_t is_rx)
834 {
835 #define QSM_REG_NB_BITS_PER_QMAP_FIELD 8
836 #define NB_QMAP_FIELDS_PER_QSM_REG 4
837 #define QMAP_FIELD_RESERVED_BITS_MASK 0x0f
838
839         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(eth_dev->data->dev_private);
840         struct ixgbe_stat_mapping_registers *stat_mappings =
841                 IXGBE_DEV_PRIVATE_TO_STAT_MAPPINGS(eth_dev->data->dev_private);
842         uint32_t qsmr_mask = 0;
843         uint32_t clearing_mask = QMAP_FIELD_RESERVED_BITS_MASK;
844         uint32_t q_map;
845         uint8_t n, offset;
846
847         if ((hw->mac.type != ixgbe_mac_82599EB) &&
848                 (hw->mac.type != ixgbe_mac_X540) &&
849                 (hw->mac.type != ixgbe_mac_X550) &&
850                 (hw->mac.type != ixgbe_mac_X550EM_x))
851                 return -ENOSYS;
852
853         PMD_INIT_LOG(DEBUG, "Setting port %d, %s queue_id %d to stat index %d",
854                      (int)(eth_dev->data->port_id), is_rx ? "RX" : "TX",
855                      queue_id, stat_idx);
856
857         n = (uint8_t)(queue_id / NB_QMAP_FIELDS_PER_QSM_REG);
858         if (n >= IXGBE_NB_STAT_MAPPING_REGS) {
859                 PMD_INIT_LOG(ERR, "Nb of stat mapping registers exceeded");
860                 return -EIO;
861         }
862         offset = (uint8_t)(queue_id % NB_QMAP_FIELDS_PER_QSM_REG);
863
864         /* Now clear any previous stat_idx set */
865         clearing_mask <<= (QSM_REG_NB_BITS_PER_QMAP_FIELD * offset);
866         if (!is_rx)
867                 stat_mappings->tqsm[n] &= ~clearing_mask;
868         else
869                 stat_mappings->rqsmr[n] &= ~clearing_mask;
870
871         q_map = (uint32_t)stat_idx;
872         q_map &= QMAP_FIELD_RESERVED_BITS_MASK;
873         qsmr_mask = q_map << (QSM_REG_NB_BITS_PER_QMAP_FIELD * offset);
874         if (!is_rx)
875                 stat_mappings->tqsm[n] |= qsmr_mask;
876         else
877                 stat_mappings->rqsmr[n] |= qsmr_mask;
878
879         PMD_INIT_LOG(DEBUG, "Set port %d, %s queue_id %d to stat index %d",
880                      (int)(eth_dev->data->port_id), is_rx ? "RX" : "TX",
881                      queue_id, stat_idx);
882         PMD_INIT_LOG(DEBUG, "%s[%d] = 0x%08x", is_rx ? "RQSMR" : "TQSM", n,
883                      is_rx ? stat_mappings->rqsmr[n] : stat_mappings->tqsm[n]);
884
885         /* Now write the mapping in the appropriate register */
886         if (is_rx) {
887                 PMD_INIT_LOG(DEBUG, "Write 0x%x to RX IXGBE stat mapping reg:%d",
888                              stat_mappings->rqsmr[n], n);
889                 IXGBE_WRITE_REG(hw, IXGBE_RQSMR(n), stat_mappings->rqsmr[n]);
890         }
891         else {
892                 PMD_INIT_LOG(DEBUG, "Write 0x%x to TX IXGBE stat mapping reg:%d",
893                              stat_mappings->tqsm[n], n);
894                 IXGBE_WRITE_REG(hw, IXGBE_TQSM(n), stat_mappings->tqsm[n]);
895         }
896         return 0;
897 }
898
899 static void
900 ixgbe_restore_statistics_mapping(struct rte_eth_dev * dev)
901 {
902         struct ixgbe_stat_mapping_registers *stat_mappings =
903                 IXGBE_DEV_PRIVATE_TO_STAT_MAPPINGS(dev->data->dev_private);
904         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
905         int i;
906
907         /* write whatever was in stat mapping table to the NIC */
908         for (i = 0; i < IXGBE_NB_STAT_MAPPING_REGS; i++) {
909                 /* rx */
910                 IXGBE_WRITE_REG(hw, IXGBE_RQSMR(i), stat_mappings->rqsmr[i]);
911
912                 /* tx */
913                 IXGBE_WRITE_REG(hw, IXGBE_TQSM(i), stat_mappings->tqsm[i]);
914         }
915 }
916
917 static void
918 ixgbe_dcb_init(struct ixgbe_hw *hw,struct ixgbe_dcb_config *dcb_config)
919 {
920         uint8_t i;
921         struct ixgbe_dcb_tc_config *tc;
922         uint8_t dcb_max_tc = IXGBE_DCB_MAX_TRAFFIC_CLASS;
923
924         dcb_config->num_tcs.pg_tcs = dcb_max_tc;
925         dcb_config->num_tcs.pfc_tcs = dcb_max_tc;
926         for (i = 0; i < dcb_max_tc; i++) {
927                 tc = &dcb_config->tc_config[i];
928                 tc->path[IXGBE_DCB_TX_CONFIG].bwg_id = i;
929                 tc->path[IXGBE_DCB_TX_CONFIG].bwg_percent =
930                                  (uint8_t)(100/dcb_max_tc + (i & 1));
931                 tc->path[IXGBE_DCB_RX_CONFIG].bwg_id = i;
932                 tc->path[IXGBE_DCB_RX_CONFIG].bwg_percent =
933                                  (uint8_t)(100/dcb_max_tc + (i & 1));
934                 tc->pfc = ixgbe_dcb_pfc_disabled;
935         }
936
937         /* Initialize default user to priority mapping, UPx->TC0 */
938         tc = &dcb_config->tc_config[0];
939         tc->path[IXGBE_DCB_TX_CONFIG].up_to_tc_bitmap = 0xFF;
940         tc->path[IXGBE_DCB_RX_CONFIG].up_to_tc_bitmap = 0xFF;
941         for (i = 0; i< IXGBE_DCB_MAX_BW_GROUP; i++) {
942                 dcb_config->bw_percentage[IXGBE_DCB_TX_CONFIG][i] = 100;
943                 dcb_config->bw_percentage[IXGBE_DCB_RX_CONFIG][i] = 100;
944         }
945         dcb_config->rx_pba_cfg = ixgbe_dcb_pba_equal;
946         dcb_config->pfc_mode_enable = false;
947         dcb_config->vt_mode = true;
948         dcb_config->round_robin_enable = false;
949         /* support all DCB capabilities in 82599 */
950         dcb_config->support.capabilities = 0xFF;
951
952         /*we only support 4 Tcs for X540, X550 */
953         if (hw->mac.type == ixgbe_mac_X540 ||
954                 hw->mac.type == ixgbe_mac_X550 ||
955                 hw->mac.type == ixgbe_mac_X550EM_x) {
956                 dcb_config->num_tcs.pg_tcs = 4;
957                 dcb_config->num_tcs.pfc_tcs = 4;
958         }
959 }
960
961 /*
962  * Ensure that all locks are released before first NVM or PHY access
963  */
964 static void
965 ixgbe_swfw_lock_reset(struct ixgbe_hw *hw)
966 {
967         uint16_t mask;
968
969         /*
970          * Phy lock should not fail in this early stage. If this is the case,
971          * it is due to an improper exit of the application.
972          * So force the release of the faulty lock. Release of common lock
973          * is done automatically by swfw_sync function.
974          */
975         mask = IXGBE_GSSR_PHY0_SM << hw->bus.func;
976         if (ixgbe_acquire_swfw_semaphore(hw, mask) < 0) {
977                 PMD_DRV_LOG(DEBUG, "SWFW phy%d lock released", hw->bus.func);
978         }
979         ixgbe_release_swfw_semaphore(hw, mask);
980
981         /*
982          * These ones are more tricky since they are common to all ports; but
983          * swfw_sync retries last long enough (1s) to be almost sure that if
984          * lock can not be taken it is due to an improper lock of the
985          * semaphore.
986          */
987         mask = IXGBE_GSSR_EEP_SM | IXGBE_GSSR_MAC_CSR_SM | IXGBE_GSSR_SW_MNG_SM;
988         if (ixgbe_acquire_swfw_semaphore(hw, mask) < 0) {
989                 PMD_DRV_LOG(DEBUG, "SWFW common locks released");
990         }
991         ixgbe_release_swfw_semaphore(hw, mask);
992 }
993
994 /*
995  * This function is based on code in ixgbe_attach() in base/ixgbe.c.
996  * It returns 0 on success.
997  */
998 static int
999 eth_ixgbe_dev_init(struct rte_eth_dev *eth_dev)
1000 {
1001         struct rte_pci_device *pci_dev;
1002         struct ixgbe_hw *hw =
1003                 IXGBE_DEV_PRIVATE_TO_HW(eth_dev->data->dev_private);
1004         struct ixgbe_vfta * shadow_vfta =
1005                 IXGBE_DEV_PRIVATE_TO_VFTA(eth_dev->data->dev_private);
1006         struct ixgbe_hwstrip *hwstrip =
1007                 IXGBE_DEV_PRIVATE_TO_HWSTRIP_BITMAP(eth_dev->data->dev_private);
1008         struct ixgbe_dcb_config *dcb_config =
1009                 IXGBE_DEV_PRIVATE_TO_DCB_CFG(eth_dev->data->dev_private);
1010         struct ixgbe_filter_info *filter_info =
1011                 IXGBE_DEV_PRIVATE_TO_FILTER_INFO(eth_dev->data->dev_private);
1012         uint32_t ctrl_ext;
1013         uint16_t csum;
1014         int diag, i;
1015
1016         PMD_INIT_FUNC_TRACE();
1017
1018         eth_dev->dev_ops = &ixgbe_eth_dev_ops;
1019         eth_dev->rx_pkt_burst = &ixgbe_recv_pkts;
1020         eth_dev->tx_pkt_burst = &ixgbe_xmit_pkts;
1021
1022         /*
1023          * For secondary processes, we don't initialise any further as primary
1024          * has already done this work. Only check we don't need a different
1025          * RX and TX function.
1026          */
1027         if (rte_eal_process_type() != RTE_PROC_PRIMARY){
1028                 struct ixgbe_tx_queue *txq;
1029                 /* TX queue function in primary, set by last queue initialized
1030                  * Tx queue may not initialized by primary process */
1031                 if (eth_dev->data->tx_queues) {
1032                         txq = eth_dev->data->tx_queues[eth_dev->data->nb_tx_queues-1];
1033                         ixgbe_set_tx_function(eth_dev, txq);
1034                 } else {
1035                         /* Use default TX function if we get here */
1036                         PMD_INIT_LOG(NOTICE, "No TX queues configured yet. "
1037                                              "Using default TX function.");
1038                 }
1039
1040                 ixgbe_set_rx_function(eth_dev);
1041
1042                 return 0;
1043         }
1044         pci_dev = eth_dev->pci_dev;
1045
1046         rte_eth_copy_pci_info(eth_dev, pci_dev);
1047
1048         /* Vendor and Device ID need to be set before init of shared code */
1049         hw->device_id = pci_dev->id.device_id;
1050         hw->vendor_id = pci_dev->id.vendor_id;
1051         hw->hw_addr = (void *)pci_dev->mem_resource[0].addr;
1052         hw->allow_unsupported_sfp = 1;
1053
1054         /* Initialize the shared code (base driver) */
1055 #ifdef RTE_NIC_BYPASS
1056         diag = ixgbe_bypass_init_shared_code(hw);
1057 #else
1058         diag = ixgbe_init_shared_code(hw);
1059 #endif /* RTE_NIC_BYPASS */
1060
1061         if (diag != IXGBE_SUCCESS) {
1062                 PMD_INIT_LOG(ERR, "Shared code init failed: %d", diag);
1063                 return -EIO;
1064         }
1065
1066         /* pick up the PCI bus settings for reporting later */
1067         ixgbe_get_bus_info(hw);
1068
1069         /* Unlock any pending hardware semaphore */
1070         ixgbe_swfw_lock_reset(hw);
1071
1072         /* Initialize DCB configuration*/
1073         memset(dcb_config, 0, sizeof(struct ixgbe_dcb_config));
1074         ixgbe_dcb_init(hw,dcb_config);
1075         /* Get Hardware Flow Control setting */
1076         hw->fc.requested_mode = ixgbe_fc_full;
1077         hw->fc.current_mode = ixgbe_fc_full;
1078         hw->fc.pause_time = IXGBE_FC_PAUSE;
1079         for (i = 0; i < IXGBE_DCB_MAX_TRAFFIC_CLASS; i++) {
1080                 hw->fc.low_water[i] = IXGBE_FC_LO;
1081                 hw->fc.high_water[i] = IXGBE_FC_HI;
1082         }
1083         hw->fc.send_xon = 1;
1084
1085         /* Make sure we have a good EEPROM before we read from it */
1086         diag = ixgbe_validate_eeprom_checksum(hw, &csum);
1087         if (diag != IXGBE_SUCCESS) {
1088                 PMD_INIT_LOG(ERR, "The EEPROM checksum is not valid: %d", diag);
1089                 return -EIO;
1090         }
1091
1092 #ifdef RTE_NIC_BYPASS
1093         diag = ixgbe_bypass_init_hw(hw);
1094 #else
1095         diag = ixgbe_init_hw(hw);
1096 #endif /* RTE_NIC_BYPASS */
1097
1098         /*
1099          * Devices with copper phys will fail to initialise if ixgbe_init_hw()
1100          * is called too soon after the kernel driver unbinding/binding occurs.
1101          * The failure occurs in ixgbe_identify_phy_generic() for all devices,
1102          * but for non-copper devies, ixgbe_identify_sfp_module_generic() is
1103          * also called. See ixgbe_identify_phy_82599(). The reason for the
1104          * failure is not known, and only occuts when virtualisation features
1105          * are disabled in the bios. A delay of 100ms  was found to be enough by
1106          * trial-and-error, and is doubled to be safe.
1107          */
1108         if (diag && (hw->mac.ops.get_media_type(hw) == ixgbe_media_type_copper)) {
1109                 rte_delay_ms(200);
1110                 diag = ixgbe_init_hw(hw);
1111         }
1112
1113         if (diag == IXGBE_ERR_EEPROM_VERSION) {
1114                 PMD_INIT_LOG(ERR, "This device is a pre-production adapter/"
1115                     "LOM.  Please be aware there may be issues associated "
1116                     "with your hardware.");
1117                 PMD_INIT_LOG(ERR, "If you are experiencing problems "
1118                     "please contact your Intel or hardware representative "
1119                     "who provided you with this hardware.");
1120         } else if (diag == IXGBE_ERR_SFP_NOT_SUPPORTED)
1121                 PMD_INIT_LOG(ERR, "Unsupported SFP+ Module");
1122         if (diag) {
1123                 PMD_INIT_LOG(ERR, "Hardware Initialization Failure: %d", diag);
1124                 return -EIO;
1125         }
1126
1127         /* Reset the hw statistics */
1128         ixgbe_dev_stats_reset(eth_dev);
1129
1130         /* disable interrupt */
1131         ixgbe_disable_intr(hw);
1132
1133         /* reset mappings for queue statistics hw counters*/
1134         ixgbe_reset_qstat_mappings(hw);
1135
1136         /* Allocate memory for storing MAC addresses */
1137         eth_dev->data->mac_addrs = rte_zmalloc("ixgbe", ETHER_ADDR_LEN *
1138                         hw->mac.num_rar_entries, 0);
1139         if (eth_dev->data->mac_addrs == NULL) {
1140                 PMD_INIT_LOG(ERR,
1141                         "Failed to allocate %u bytes needed to store "
1142                         "MAC addresses",
1143                         ETHER_ADDR_LEN * hw->mac.num_rar_entries);
1144                 return -ENOMEM;
1145         }
1146         /* Copy the permanent MAC address */
1147         ether_addr_copy((struct ether_addr *) hw->mac.perm_addr,
1148                         &eth_dev->data->mac_addrs[0]);
1149
1150         /* Allocate memory for storing hash filter MAC addresses */
1151         eth_dev->data->hash_mac_addrs = rte_zmalloc("ixgbe", ETHER_ADDR_LEN *
1152                         IXGBE_VMDQ_NUM_UC_MAC, 0);
1153         if (eth_dev->data->hash_mac_addrs == NULL) {
1154                 PMD_INIT_LOG(ERR,
1155                         "Failed to allocate %d bytes needed to store MAC addresses",
1156                         ETHER_ADDR_LEN * IXGBE_VMDQ_NUM_UC_MAC);
1157                 return -ENOMEM;
1158         }
1159
1160         /* initialize the vfta */
1161         memset(shadow_vfta, 0, sizeof(*shadow_vfta));
1162
1163         /* initialize the hw strip bitmap*/
1164         memset(hwstrip, 0, sizeof(*hwstrip));
1165
1166         /* initialize PF if max_vfs not zero */
1167         ixgbe_pf_host_init(eth_dev);
1168
1169         ctrl_ext = IXGBE_READ_REG(hw, IXGBE_CTRL_EXT);
1170         /* let hardware know driver is loaded */
1171         ctrl_ext |= IXGBE_CTRL_EXT_DRV_LOAD;
1172         /* Set PF Reset Done bit so PF/VF Mail Ops can work */
1173         ctrl_ext |= IXGBE_CTRL_EXT_PFRSTD;
1174         IXGBE_WRITE_REG(hw, IXGBE_CTRL_EXT, ctrl_ext);
1175         IXGBE_WRITE_FLUSH(hw);
1176
1177         if (ixgbe_is_sfp(hw) && hw->phy.sfp_type != ixgbe_sfp_type_not_present)
1178                 PMD_INIT_LOG(DEBUG, "MAC: %d, PHY: %d, SFP+: %d",
1179                              (int) hw->mac.type, (int) hw->phy.type,
1180                              (int) hw->phy.sfp_type);
1181         else
1182                 PMD_INIT_LOG(DEBUG, "MAC: %d, PHY: %d",
1183                              (int) hw->mac.type, (int) hw->phy.type);
1184
1185         PMD_INIT_LOG(DEBUG, "port %d vendorID=0x%x deviceID=0x%x",
1186                         eth_dev->data->port_id, pci_dev->id.vendor_id,
1187                         pci_dev->id.device_id);
1188
1189         rte_intr_callback_register(&pci_dev->intr_handle,
1190                                    ixgbe_dev_interrupt_handler,
1191                                    (void *)eth_dev);
1192
1193         /* enable uio/vfio intr/eventfd mapping */
1194         rte_intr_enable(&pci_dev->intr_handle);
1195
1196         /* enable support intr */
1197         ixgbe_enable_intr(eth_dev);
1198
1199         /* initialize 5tuple filter list */
1200         TAILQ_INIT(&filter_info->fivetuple_list);
1201         memset(filter_info->fivetuple_mask, 0,
1202                 sizeof(uint32_t) * IXGBE_5TUPLE_ARRAY_SIZE);
1203
1204         return 0;
1205 }
1206
1207 static int
1208 eth_ixgbe_dev_uninit(struct rte_eth_dev *eth_dev)
1209 {
1210         struct rte_pci_device *pci_dev;
1211         struct ixgbe_hw *hw;
1212
1213         PMD_INIT_FUNC_TRACE();
1214
1215         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
1216                 return -EPERM;
1217
1218         hw = IXGBE_DEV_PRIVATE_TO_HW(eth_dev->data->dev_private);
1219         pci_dev = eth_dev->pci_dev;
1220
1221         if (hw->adapter_stopped == 0)
1222                 ixgbe_dev_close(eth_dev);
1223
1224         eth_dev->dev_ops = NULL;
1225         eth_dev->rx_pkt_burst = NULL;
1226         eth_dev->tx_pkt_burst = NULL;
1227
1228         /* Unlock any pending hardware semaphore */
1229         ixgbe_swfw_lock_reset(hw);
1230
1231         /* disable uio intr before callback unregister */
1232         rte_intr_disable(&(pci_dev->intr_handle));
1233         rte_intr_callback_unregister(&(pci_dev->intr_handle),
1234                 ixgbe_dev_interrupt_handler, (void *)eth_dev);
1235
1236         /* uninitialize PF if max_vfs not zero */
1237         ixgbe_pf_host_uninit(eth_dev);
1238
1239         rte_free(eth_dev->data->mac_addrs);
1240         eth_dev->data->mac_addrs = NULL;
1241
1242         rte_free(eth_dev->data->hash_mac_addrs);
1243         eth_dev->data->hash_mac_addrs = NULL;
1244
1245         return 0;
1246 }
1247
1248 /*
1249  * Negotiate mailbox API version with the PF.
1250  * After reset API version is always set to the basic one (ixgbe_mbox_api_10).
1251  * Then we try to negotiate starting with the most recent one.
1252  * If all negotiation attempts fail, then we will proceed with
1253  * the default one (ixgbe_mbox_api_10).
1254  */
1255 static void
1256 ixgbevf_negotiate_api(struct ixgbe_hw *hw)
1257 {
1258         int32_t i;
1259
1260         /* start with highest supported, proceed down */
1261         static const enum ixgbe_pfvf_api_rev sup_ver[] = {
1262                 ixgbe_mbox_api_11,
1263                 ixgbe_mbox_api_10,
1264         };
1265
1266         for (i = 0;
1267                         i != RTE_DIM(sup_ver) &&
1268                         ixgbevf_negotiate_api_version(hw, sup_ver[i]) != 0;
1269                         i++)
1270                 ;
1271 }
1272
1273 static void
1274 generate_random_mac_addr(struct ether_addr *mac_addr)
1275 {
1276         uint64_t random;
1277
1278         /* Set Organizationally Unique Identifier (OUI) prefix. */
1279         mac_addr->addr_bytes[0] = 0x00;
1280         mac_addr->addr_bytes[1] = 0x09;
1281         mac_addr->addr_bytes[2] = 0xC0;
1282         /* Force indication of locally assigned MAC address. */
1283         mac_addr->addr_bytes[0] |= ETHER_LOCAL_ADMIN_ADDR;
1284         /* Generate the last 3 bytes of the MAC address with a random number. */
1285         random = rte_rand();
1286         memcpy(&mac_addr->addr_bytes[3], &random, 3);
1287 }
1288
1289 /*
1290  * Virtual Function device init
1291  */
1292 static int
1293 eth_ixgbevf_dev_init(struct rte_eth_dev *eth_dev)
1294 {
1295         int diag;
1296         uint32_t tc, tcs;
1297         struct rte_pci_device *pci_dev;
1298         struct ixgbe_hw *hw =
1299                 IXGBE_DEV_PRIVATE_TO_HW(eth_dev->data->dev_private);
1300         struct ixgbe_vfta * shadow_vfta =
1301                 IXGBE_DEV_PRIVATE_TO_VFTA(eth_dev->data->dev_private);
1302         struct ixgbe_hwstrip *hwstrip =
1303                 IXGBE_DEV_PRIVATE_TO_HWSTRIP_BITMAP(eth_dev->data->dev_private);
1304         struct ether_addr *perm_addr = (struct ether_addr *) hw->mac.perm_addr;
1305
1306         PMD_INIT_FUNC_TRACE();
1307
1308         eth_dev->dev_ops = &ixgbevf_eth_dev_ops;
1309         eth_dev->rx_pkt_burst = &ixgbe_recv_pkts;
1310         eth_dev->tx_pkt_burst = &ixgbe_xmit_pkts;
1311
1312         /* for secondary processes, we don't initialise any further as primary
1313          * has already done this work. Only check we don't need a different
1314          * RX function */
1315         if (rte_eal_process_type() != RTE_PROC_PRIMARY){
1316                 if (eth_dev->data->scattered_rx)
1317                         eth_dev->rx_pkt_burst = ixgbe_recv_pkts_lro_single_alloc;
1318                 return 0;
1319         }
1320
1321         pci_dev = eth_dev->pci_dev;
1322
1323         rte_eth_copy_pci_info(eth_dev, pci_dev);
1324
1325         hw->device_id = pci_dev->id.device_id;
1326         hw->vendor_id = pci_dev->id.vendor_id;
1327         hw->hw_addr = (void *)pci_dev->mem_resource[0].addr;
1328
1329         /* initialize the vfta */
1330         memset(shadow_vfta, 0, sizeof(*shadow_vfta));
1331
1332         /* initialize the hw strip bitmap*/
1333         memset(hwstrip, 0, sizeof(*hwstrip));
1334
1335         /* Initialize the shared code (base driver) */
1336         diag = ixgbe_init_shared_code(hw);
1337         if (diag != IXGBE_SUCCESS) {
1338                 PMD_INIT_LOG(ERR, "Shared code init failed for ixgbevf: %d", diag);
1339                 return -EIO;
1340         }
1341
1342         /* init_mailbox_params */
1343         hw->mbx.ops.init_params(hw);
1344
1345         /* Reset the hw statistics */
1346         ixgbevf_dev_stats_reset(eth_dev);
1347
1348         /* Disable the interrupts for VF */
1349         ixgbevf_intr_disable(hw);
1350
1351         hw->mac.num_rar_entries = 128; /* The MAX of the underlying PF */
1352         diag = hw->mac.ops.reset_hw(hw);
1353
1354         /*
1355          * The VF reset operation returns the IXGBE_ERR_INVALID_MAC_ADDR when
1356          * the underlying PF driver has not assigned a MAC address to the VF.
1357          * In this case, assign a random MAC address.
1358          */
1359         if ((diag != IXGBE_SUCCESS) && (diag != IXGBE_ERR_INVALID_MAC_ADDR)) {
1360                 PMD_INIT_LOG(ERR, "VF Initialization Failure: %d", diag);
1361                 return diag;
1362         }
1363
1364         /* negotiate mailbox API version to use with the PF. */
1365         ixgbevf_negotiate_api(hw);
1366
1367         /* Get Rx/Tx queue count via mailbox, which is ready after reset_hw */
1368         ixgbevf_get_queues(hw, &tcs, &tc);
1369
1370         /* Allocate memory for storing MAC addresses */
1371         eth_dev->data->mac_addrs = rte_zmalloc("ixgbevf", ETHER_ADDR_LEN *
1372                         hw->mac.num_rar_entries, 0);
1373         if (eth_dev->data->mac_addrs == NULL) {
1374                 PMD_INIT_LOG(ERR,
1375                         "Failed to allocate %u bytes needed to store "
1376                         "MAC addresses",
1377                         ETHER_ADDR_LEN * hw->mac.num_rar_entries);
1378                 return -ENOMEM;
1379         }
1380
1381         /* Generate a random MAC address, if none was assigned by PF. */
1382         if (is_zero_ether_addr(perm_addr)) {
1383                 generate_random_mac_addr(perm_addr);
1384                 diag = ixgbe_set_rar_vf(hw, 1, perm_addr->addr_bytes, 0, 1);
1385                 if (diag) {
1386                         rte_free(eth_dev->data->mac_addrs);
1387                         eth_dev->data->mac_addrs = NULL;
1388                         return diag;
1389                 }
1390                 PMD_INIT_LOG(INFO, "\tVF MAC address not assigned by Host PF");
1391                 PMD_INIT_LOG(INFO, "\tAssign randomly generated MAC address "
1392                              "%02x:%02x:%02x:%02x:%02x:%02x",
1393                              perm_addr->addr_bytes[0],
1394                              perm_addr->addr_bytes[1],
1395                              perm_addr->addr_bytes[2],
1396                              perm_addr->addr_bytes[3],
1397                              perm_addr->addr_bytes[4],
1398                              perm_addr->addr_bytes[5]);
1399         }
1400
1401         /* Copy the permanent MAC address */
1402         ether_addr_copy(perm_addr, &eth_dev->data->mac_addrs[0]);
1403
1404         /* reset the hardware with the new settings */
1405         diag = hw->mac.ops.start_hw(hw);
1406         switch (diag) {
1407                 case  0:
1408                         break;
1409
1410                 default:
1411                         PMD_INIT_LOG(ERR, "VF Initialization Failure: %d", diag);
1412                         return -EIO;
1413         }
1414
1415         PMD_INIT_LOG(DEBUG, "port %d vendorID=0x%x deviceID=0x%x mac.type=%s",
1416                      eth_dev->data->port_id, pci_dev->id.vendor_id,
1417                      pci_dev->id.device_id, "ixgbe_mac_82599_vf");
1418
1419         return 0;
1420 }
1421
1422 /* Virtual Function device uninit */
1423
1424 static int
1425 eth_ixgbevf_dev_uninit(struct rte_eth_dev *eth_dev)
1426 {
1427         struct ixgbe_hw *hw;
1428         unsigned i;
1429
1430         PMD_INIT_FUNC_TRACE();
1431
1432         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
1433                 return -EPERM;
1434
1435         hw = IXGBE_DEV_PRIVATE_TO_HW(eth_dev->data->dev_private);
1436
1437         if (hw->adapter_stopped == 0)
1438                 ixgbevf_dev_close(eth_dev);
1439
1440         eth_dev->dev_ops = NULL;
1441         eth_dev->rx_pkt_burst = NULL;
1442         eth_dev->tx_pkt_burst = NULL;
1443
1444         /* Disable the interrupts for VF */
1445         ixgbevf_intr_disable(hw);
1446
1447         for (i = 0; i < eth_dev->data->nb_rx_queues; i++) {
1448                 ixgbe_dev_rx_queue_release(eth_dev->data->rx_queues[i]);
1449                 eth_dev->data->rx_queues[i] = NULL;
1450         }
1451         eth_dev->data->nb_rx_queues = 0;
1452
1453         for (i = 0; i < eth_dev->data->nb_tx_queues; i++) {
1454                 ixgbe_dev_tx_queue_release(eth_dev->data->tx_queues[i]);
1455                 eth_dev->data->tx_queues[i] = NULL;
1456         }
1457         eth_dev->data->nb_tx_queues = 0;
1458
1459         rte_free(eth_dev->data->mac_addrs);
1460         eth_dev->data->mac_addrs = NULL;
1461
1462         return 0;
1463 }
1464
1465 static struct eth_driver rte_ixgbe_pmd = {
1466         .pci_drv = {
1467                 .name = "rte_ixgbe_pmd",
1468                 .id_table = pci_id_ixgbe_map,
1469                 .drv_flags = RTE_PCI_DRV_NEED_MAPPING | RTE_PCI_DRV_INTR_LSC |
1470                         RTE_PCI_DRV_DETACHABLE,
1471         },
1472         .eth_dev_init = eth_ixgbe_dev_init,
1473         .eth_dev_uninit = eth_ixgbe_dev_uninit,
1474         .dev_private_size = sizeof(struct ixgbe_adapter),
1475 };
1476
1477 /*
1478  * virtual function driver struct
1479  */
1480 static struct eth_driver rte_ixgbevf_pmd = {
1481         .pci_drv = {
1482                 .name = "rte_ixgbevf_pmd",
1483                 .id_table = pci_id_ixgbevf_map,
1484                 .drv_flags = RTE_PCI_DRV_NEED_MAPPING | RTE_PCI_DRV_DETACHABLE,
1485         },
1486         .eth_dev_init = eth_ixgbevf_dev_init,
1487         .eth_dev_uninit = eth_ixgbevf_dev_uninit,
1488         .dev_private_size = sizeof(struct ixgbe_adapter),
1489 };
1490
1491 /*
1492  * Driver initialization routine.
1493  * Invoked once at EAL init time.
1494  * Register itself as the [Poll Mode] Driver of PCI IXGBE devices.
1495  */
1496 static int
1497 rte_ixgbe_pmd_init(const char *name __rte_unused, const char *params __rte_unused)
1498 {
1499         PMD_INIT_FUNC_TRACE();
1500
1501         rte_eth_driver_register(&rte_ixgbe_pmd);
1502         return 0;
1503 }
1504
1505 /*
1506  * VF Driver initialization routine.
1507  * Invoked one at EAL init time.
1508  * Register itself as the [Virtual Poll Mode] Driver of PCI niantic devices.
1509  */
1510 static int
1511 rte_ixgbevf_pmd_init(const char *name __rte_unused, const char *param __rte_unused)
1512 {
1513         PMD_INIT_FUNC_TRACE();
1514
1515         rte_eth_driver_register(&rte_ixgbevf_pmd);
1516         return 0;
1517 }
1518
1519 static int
1520 ixgbe_vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
1521 {
1522         struct ixgbe_hw *hw =
1523                 IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1524         struct ixgbe_vfta * shadow_vfta =
1525                 IXGBE_DEV_PRIVATE_TO_VFTA(dev->data->dev_private);
1526         uint32_t vfta;
1527         uint32_t vid_idx;
1528         uint32_t vid_bit;
1529
1530         vid_idx = (uint32_t) ((vlan_id >> 5) & 0x7F);
1531         vid_bit = (uint32_t) (1 << (vlan_id & 0x1F));
1532         vfta = IXGBE_READ_REG(hw, IXGBE_VFTA(vid_idx));
1533         if (on)
1534                 vfta |= vid_bit;
1535         else
1536                 vfta &= ~vid_bit;
1537         IXGBE_WRITE_REG(hw, IXGBE_VFTA(vid_idx), vfta);
1538
1539         /* update local VFTA copy */
1540         shadow_vfta->vfta[vid_idx] = vfta;
1541
1542         return 0;
1543 }
1544
1545 static void
1546 ixgbe_vlan_strip_queue_set(struct rte_eth_dev *dev, uint16_t queue, int on)
1547 {
1548         if (on)
1549                 ixgbe_vlan_hw_strip_enable(dev, queue);
1550         else
1551                 ixgbe_vlan_hw_strip_disable(dev, queue);
1552 }
1553
1554 static int
1555 ixgbe_vlan_tpid_set(struct rte_eth_dev *dev,
1556                     enum rte_vlan_type vlan_type,
1557                     uint16_t tpid)
1558 {
1559         struct ixgbe_hw *hw =
1560                 IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1561         int ret = 0;
1562
1563         switch (vlan_type) {
1564         case ETH_VLAN_TYPE_INNER:
1565                 /* Only the high 16-bits is valid */
1566                 IXGBE_WRITE_REG(hw, IXGBE_EXVET, tpid << 16);
1567                 break;
1568         default:
1569                 ret = -EINVAL;
1570                 PMD_DRV_LOG(ERR, "Unsupported vlan type %d\n", vlan_type);
1571                 break;
1572         }
1573
1574         return ret;
1575 }
1576
1577 void
1578 ixgbe_vlan_hw_filter_disable(struct rte_eth_dev *dev)
1579 {
1580         struct ixgbe_hw *hw =
1581                 IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1582         uint32_t vlnctrl;
1583
1584         PMD_INIT_FUNC_TRACE();
1585
1586         /* Filter Table Disable */
1587         vlnctrl = IXGBE_READ_REG(hw, IXGBE_VLNCTRL);
1588         vlnctrl &= ~IXGBE_VLNCTRL_VFE;
1589
1590         IXGBE_WRITE_REG(hw, IXGBE_VLNCTRL, vlnctrl);
1591 }
1592
1593 void
1594 ixgbe_vlan_hw_filter_enable(struct rte_eth_dev *dev)
1595 {
1596         struct ixgbe_hw *hw =
1597                 IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1598         struct ixgbe_vfta * shadow_vfta =
1599                 IXGBE_DEV_PRIVATE_TO_VFTA(dev->data->dev_private);
1600         uint32_t vlnctrl;
1601         uint16_t i;
1602
1603         PMD_INIT_FUNC_TRACE();
1604
1605         /* Filter Table Enable */
1606         vlnctrl = IXGBE_READ_REG(hw, IXGBE_VLNCTRL);
1607         vlnctrl &= ~IXGBE_VLNCTRL_CFIEN;
1608         vlnctrl |= IXGBE_VLNCTRL_VFE;
1609
1610         IXGBE_WRITE_REG(hw, IXGBE_VLNCTRL, vlnctrl);
1611
1612         /* write whatever is in local vfta copy */
1613         for (i = 0; i < IXGBE_VFTA_SIZE; i++)
1614                 IXGBE_WRITE_REG(hw, IXGBE_VFTA(i), shadow_vfta->vfta[i]);
1615 }
1616
1617 static void
1618 ixgbe_vlan_hw_strip_bitmap_set(struct rte_eth_dev *dev, uint16_t queue, bool on)
1619 {
1620         struct ixgbe_hwstrip *hwstrip =
1621                 IXGBE_DEV_PRIVATE_TO_HWSTRIP_BITMAP(dev->data->dev_private);
1622
1623         if(queue >= IXGBE_MAX_RX_QUEUE_NUM)
1624                 return;
1625
1626         if (on)
1627                 IXGBE_SET_HWSTRIP(hwstrip, queue);
1628         else
1629                 IXGBE_CLEAR_HWSTRIP(hwstrip, queue);
1630 }
1631
1632 static void
1633 ixgbe_vlan_hw_strip_disable(struct rte_eth_dev *dev, uint16_t queue)
1634 {
1635         struct ixgbe_hw *hw =
1636                 IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1637         uint32_t ctrl;
1638
1639         PMD_INIT_FUNC_TRACE();
1640
1641         if (hw->mac.type == ixgbe_mac_82598EB) {
1642                 /* No queue level support */
1643                 PMD_INIT_LOG(NOTICE, "82598EB not support queue level hw strip");
1644                 return;
1645         }
1646         else {
1647                 /* Other 10G NIC, the VLAN strip can be setup per queue in RXDCTL */
1648                 ctrl = IXGBE_READ_REG(hw, IXGBE_RXDCTL(queue));
1649                 ctrl &= ~IXGBE_RXDCTL_VME;
1650                 IXGBE_WRITE_REG(hw, IXGBE_RXDCTL(queue), ctrl);
1651         }
1652         /* record those setting for HW strip per queue */
1653         ixgbe_vlan_hw_strip_bitmap_set(dev, queue, 0);
1654 }
1655
1656 static void
1657 ixgbe_vlan_hw_strip_enable(struct rte_eth_dev *dev, uint16_t queue)
1658 {
1659         struct ixgbe_hw *hw =
1660                 IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1661         uint32_t ctrl;
1662
1663         PMD_INIT_FUNC_TRACE();
1664
1665         if (hw->mac.type == ixgbe_mac_82598EB) {
1666                 /* No queue level supported */
1667                 PMD_INIT_LOG(NOTICE, "82598EB not support queue level hw strip");
1668                 return;
1669         }
1670         else {
1671                 /* Other 10G NIC, the VLAN strip can be setup per queue in RXDCTL */
1672                 ctrl = IXGBE_READ_REG(hw, IXGBE_RXDCTL(queue));
1673                 ctrl |= IXGBE_RXDCTL_VME;
1674                 IXGBE_WRITE_REG(hw, IXGBE_RXDCTL(queue), ctrl);
1675         }
1676         /* record those setting for HW strip per queue */
1677         ixgbe_vlan_hw_strip_bitmap_set(dev, queue, 1);
1678 }
1679
1680 void
1681 ixgbe_vlan_hw_strip_disable_all(struct rte_eth_dev *dev)
1682 {
1683         struct ixgbe_hw *hw =
1684                 IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1685         uint32_t ctrl;
1686         uint16_t i;
1687
1688         PMD_INIT_FUNC_TRACE();
1689
1690         if (hw->mac.type == ixgbe_mac_82598EB) {
1691                 ctrl = IXGBE_READ_REG(hw, IXGBE_VLNCTRL);
1692                 ctrl &= ~IXGBE_VLNCTRL_VME;
1693                 IXGBE_WRITE_REG(hw, IXGBE_VLNCTRL, ctrl);
1694         }
1695         else {
1696                 /* Other 10G NIC, the VLAN strip can be setup per queue in RXDCTL */
1697                 for (i = 0; i < dev->data->nb_rx_queues; i++) {
1698                         ctrl = IXGBE_READ_REG(hw, IXGBE_RXDCTL(i));
1699                         ctrl &= ~IXGBE_RXDCTL_VME;
1700                         IXGBE_WRITE_REG(hw, IXGBE_RXDCTL(i), ctrl);
1701
1702                         /* record those setting for HW strip per queue */
1703                         ixgbe_vlan_hw_strip_bitmap_set(dev, i, 0);
1704                 }
1705         }
1706 }
1707
1708 void
1709 ixgbe_vlan_hw_strip_enable_all(struct rte_eth_dev *dev)
1710 {
1711         struct ixgbe_hw *hw =
1712                 IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1713         uint32_t ctrl;
1714         uint16_t i;
1715
1716         PMD_INIT_FUNC_TRACE();
1717
1718         if (hw->mac.type == ixgbe_mac_82598EB) {
1719                 ctrl = IXGBE_READ_REG(hw, IXGBE_VLNCTRL);
1720                 ctrl |= IXGBE_VLNCTRL_VME;
1721                 IXGBE_WRITE_REG(hw, IXGBE_VLNCTRL, ctrl);
1722         }
1723         else {
1724                 /* Other 10G NIC, the VLAN strip can be setup per queue in RXDCTL */
1725                 for (i = 0; i < dev->data->nb_rx_queues; i++) {
1726                         ctrl = IXGBE_READ_REG(hw, IXGBE_RXDCTL(i));
1727                         ctrl |= IXGBE_RXDCTL_VME;
1728                         IXGBE_WRITE_REG(hw, IXGBE_RXDCTL(i), ctrl);
1729
1730                         /* record those setting for HW strip per queue */
1731                         ixgbe_vlan_hw_strip_bitmap_set(dev, i, 1);
1732                 }
1733         }
1734 }
1735
1736 static void
1737 ixgbe_vlan_hw_extend_disable(struct rte_eth_dev *dev)
1738 {
1739         struct ixgbe_hw *hw =
1740                 IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1741         uint32_t ctrl;
1742
1743         PMD_INIT_FUNC_TRACE();
1744
1745         /* DMATXCTRL: Geric Double VLAN Disable */
1746         ctrl = IXGBE_READ_REG(hw, IXGBE_DMATXCTL);
1747         ctrl &= ~IXGBE_DMATXCTL_GDV;
1748         IXGBE_WRITE_REG(hw, IXGBE_DMATXCTL, ctrl);
1749
1750         /* CTRL_EXT: Global Double VLAN Disable */
1751         ctrl = IXGBE_READ_REG(hw, IXGBE_CTRL_EXT);
1752         ctrl &= ~IXGBE_EXTENDED_VLAN;
1753         IXGBE_WRITE_REG(hw, IXGBE_CTRL_EXT, ctrl);
1754
1755 }
1756
1757 static void
1758 ixgbe_vlan_hw_extend_enable(struct rte_eth_dev *dev)
1759 {
1760         struct ixgbe_hw *hw =
1761                 IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1762         uint32_t ctrl;
1763
1764         PMD_INIT_FUNC_TRACE();
1765
1766         /* DMATXCTRL: Geric Double VLAN Enable */
1767         ctrl  = IXGBE_READ_REG(hw, IXGBE_DMATXCTL);
1768         ctrl |= IXGBE_DMATXCTL_GDV;
1769         IXGBE_WRITE_REG(hw, IXGBE_DMATXCTL, ctrl);
1770
1771         /* CTRL_EXT: Global Double VLAN Enable */
1772         ctrl  = IXGBE_READ_REG(hw, IXGBE_CTRL_EXT);
1773         ctrl |= IXGBE_EXTENDED_VLAN;
1774         IXGBE_WRITE_REG(hw, IXGBE_CTRL_EXT, ctrl);
1775
1776         /* Clear pooling mode of PFVTCTL. It's required by X550. */
1777         if (hw->mac.type == ixgbe_mac_X550 ||
1778             hw->mac.type == ixgbe_mac_X550EM_x) {
1779                 ctrl = IXGBE_READ_REG(hw, IXGBE_VT_CTL);
1780                 ctrl &= ~IXGBE_VT_CTL_POOLING_MODE_MASK;
1781                 IXGBE_WRITE_REG(hw, IXGBE_VT_CTL, ctrl);
1782         }
1783
1784         /*
1785          * VET EXT field in the EXVET register = 0x8100 by default
1786          * So no need to change. Same to VT field of DMATXCTL register
1787          */
1788 }
1789
1790 static void
1791 ixgbe_vlan_offload_set(struct rte_eth_dev *dev, int mask)
1792 {
1793         if(mask & ETH_VLAN_STRIP_MASK){
1794                 if (dev->data->dev_conf.rxmode.hw_vlan_strip)
1795                         ixgbe_vlan_hw_strip_enable_all(dev);
1796                 else
1797                         ixgbe_vlan_hw_strip_disable_all(dev);
1798         }
1799
1800         if(mask & ETH_VLAN_FILTER_MASK){
1801                 if (dev->data->dev_conf.rxmode.hw_vlan_filter)
1802                         ixgbe_vlan_hw_filter_enable(dev);
1803                 else
1804                         ixgbe_vlan_hw_filter_disable(dev);
1805         }
1806
1807         if(mask & ETH_VLAN_EXTEND_MASK){
1808                 if (dev->data->dev_conf.rxmode.hw_vlan_extend)
1809                         ixgbe_vlan_hw_extend_enable(dev);
1810                 else
1811                         ixgbe_vlan_hw_extend_disable(dev);
1812         }
1813 }
1814
1815 static void
1816 ixgbe_vmdq_vlan_hw_filter_enable(struct rte_eth_dev *dev)
1817 {
1818         struct ixgbe_hw *hw =
1819                 IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1820         /* VLNCTRL: enable vlan filtering and allow all vlan tags through */
1821         uint32_t vlanctrl = IXGBE_READ_REG(hw, IXGBE_VLNCTRL);
1822         vlanctrl |= IXGBE_VLNCTRL_VFE ; /* enable vlan filters */
1823         IXGBE_WRITE_REG(hw, IXGBE_VLNCTRL, vlanctrl);
1824 }
1825
1826 static int
1827 ixgbe_check_vf_rss_rxq_num(struct rte_eth_dev *dev, uint16_t nb_rx_q)
1828 {
1829         switch (nb_rx_q) {
1830         case 1:
1831         case 2:
1832                 RTE_ETH_DEV_SRIOV(dev).active = ETH_64_POOLS;
1833                 break;
1834         case 4:
1835                 RTE_ETH_DEV_SRIOV(dev).active = ETH_32_POOLS;
1836                 break;
1837         default:
1838                 return -EINVAL;
1839         }
1840
1841         RTE_ETH_DEV_SRIOV(dev).nb_q_per_pool = nb_rx_q;
1842         RTE_ETH_DEV_SRIOV(dev).def_pool_q_idx = dev->pci_dev->max_vfs * nb_rx_q;
1843
1844         return 0;
1845 }
1846
1847 static int
1848 ixgbe_check_mq_mode(struct rte_eth_dev *dev)
1849 {
1850         struct rte_eth_conf *dev_conf = &dev->data->dev_conf;
1851         uint16_t nb_rx_q = dev->data->nb_rx_queues;
1852         uint16_t nb_tx_q = dev->data->nb_rx_queues;
1853
1854         if (RTE_ETH_DEV_SRIOV(dev).active != 0) {
1855                 /* check multi-queue mode */
1856                 switch (dev_conf->rxmode.mq_mode) {
1857                 case ETH_MQ_RX_VMDQ_DCB:
1858                 case ETH_MQ_RX_VMDQ_DCB_RSS:
1859                         /* DCB/RSS VMDQ in SRIOV mode, not implement yet */
1860                         PMD_INIT_LOG(ERR, "SRIOV active,"
1861                                         " unsupported mq_mode rx %d.",
1862                                         dev_conf->rxmode.mq_mode);
1863                         return -EINVAL;
1864                 case ETH_MQ_RX_RSS:
1865                 case ETH_MQ_RX_VMDQ_RSS:
1866                         dev->data->dev_conf.rxmode.mq_mode = ETH_MQ_RX_VMDQ_RSS;
1867                         if (nb_rx_q <= RTE_ETH_DEV_SRIOV(dev).nb_q_per_pool)
1868                                 if (ixgbe_check_vf_rss_rxq_num(dev, nb_rx_q)) {
1869                                         PMD_INIT_LOG(ERR, "SRIOV is active,"
1870                                                 " invalid queue number"
1871                                                 " for VMDQ RSS, allowed"
1872                                                 " value are 1, 2 or 4.");
1873                                         return -EINVAL;
1874                                 }
1875                         break;
1876                 case ETH_MQ_RX_VMDQ_ONLY:
1877                 case ETH_MQ_RX_NONE:
1878                         /* if nothing mq mode configure, use default scheme */
1879                         dev->data->dev_conf.rxmode.mq_mode = ETH_MQ_RX_VMDQ_ONLY;
1880                         if (RTE_ETH_DEV_SRIOV(dev).nb_q_per_pool > 1)
1881                                 RTE_ETH_DEV_SRIOV(dev).nb_q_per_pool = 1;
1882                         break;
1883                 default: /* ETH_MQ_RX_DCB, ETH_MQ_RX_DCB_RSS or ETH_MQ_TX_DCB*/
1884                         /* SRIOV only works in VMDq enable mode */
1885                         PMD_INIT_LOG(ERR, "SRIOV is active,"
1886                                         " wrong mq_mode rx %d.",
1887                                         dev_conf->rxmode.mq_mode);
1888                         return -EINVAL;
1889                 }
1890
1891                 switch (dev_conf->txmode.mq_mode) {
1892                 case ETH_MQ_TX_VMDQ_DCB:
1893                         /* DCB VMDQ in SRIOV mode, not implement yet */
1894                         PMD_INIT_LOG(ERR, "SRIOV is active,"
1895                                         " unsupported VMDQ mq_mode tx %d.",
1896                                         dev_conf->txmode.mq_mode);
1897                         return -EINVAL;
1898                 default: /* ETH_MQ_TX_VMDQ_ONLY or ETH_MQ_TX_NONE */
1899                         dev->data->dev_conf.txmode.mq_mode = ETH_MQ_TX_VMDQ_ONLY;
1900                         break;
1901                 }
1902
1903                 /* check valid queue number */
1904                 if ((nb_rx_q > RTE_ETH_DEV_SRIOV(dev).nb_q_per_pool) ||
1905                     (nb_tx_q > RTE_ETH_DEV_SRIOV(dev).nb_q_per_pool)) {
1906                         PMD_INIT_LOG(ERR, "SRIOV is active,"
1907                                         " queue number must less equal to %d.",
1908                                         RTE_ETH_DEV_SRIOV(dev).nb_q_per_pool);
1909                         return -EINVAL;
1910                 }
1911         } else {
1912                 /* check configuration for vmdb+dcb mode */
1913                 if (dev_conf->rxmode.mq_mode == ETH_MQ_RX_VMDQ_DCB) {
1914                         const struct rte_eth_vmdq_dcb_conf *conf;
1915
1916                         if (nb_rx_q != IXGBE_VMDQ_DCB_NB_QUEUES) {
1917                                 PMD_INIT_LOG(ERR, "VMDQ+DCB, nb_rx_q != %d.",
1918                                                 IXGBE_VMDQ_DCB_NB_QUEUES);
1919                                 return -EINVAL;
1920                         }
1921                         conf = &dev_conf->rx_adv_conf.vmdq_dcb_conf;
1922                         if (!(conf->nb_queue_pools == ETH_16_POOLS ||
1923                                conf->nb_queue_pools == ETH_32_POOLS)) {
1924                                 PMD_INIT_LOG(ERR, "VMDQ+DCB selected,"
1925                                                 " nb_queue_pools must be %d or %d.",
1926                                                 ETH_16_POOLS, ETH_32_POOLS);
1927                                 return -EINVAL;
1928                         }
1929                 }
1930                 if (dev_conf->txmode.mq_mode == ETH_MQ_TX_VMDQ_DCB) {
1931                         const struct rte_eth_vmdq_dcb_tx_conf *conf;
1932
1933                         if (nb_tx_q != IXGBE_VMDQ_DCB_NB_QUEUES) {
1934                                 PMD_INIT_LOG(ERR, "VMDQ+DCB, nb_tx_q != %d",
1935                                                  IXGBE_VMDQ_DCB_NB_QUEUES);
1936                                 return -EINVAL;
1937                         }
1938                         conf = &dev_conf->tx_adv_conf.vmdq_dcb_tx_conf;
1939                         if (!(conf->nb_queue_pools == ETH_16_POOLS ||
1940                                conf->nb_queue_pools == ETH_32_POOLS)) {
1941                                 PMD_INIT_LOG(ERR, "VMDQ+DCB selected,"
1942                                                 " nb_queue_pools != %d and"
1943                                                 " nb_queue_pools != %d.",
1944                                                 ETH_16_POOLS, ETH_32_POOLS);
1945                                 return -EINVAL;
1946                         }
1947                 }
1948
1949                 /* For DCB mode check our configuration before we go further */
1950                 if (dev_conf->rxmode.mq_mode == ETH_MQ_RX_DCB) {
1951                         const struct rte_eth_dcb_rx_conf *conf;
1952
1953                         if (nb_rx_q != IXGBE_DCB_NB_QUEUES) {
1954                                 PMD_INIT_LOG(ERR, "DCB selected, nb_rx_q != %d.",
1955                                                  IXGBE_DCB_NB_QUEUES);
1956                                 return -EINVAL;
1957                         }
1958                         conf = &dev_conf->rx_adv_conf.dcb_rx_conf;
1959                         if (!(conf->nb_tcs == ETH_4_TCS ||
1960                                conf->nb_tcs == ETH_8_TCS)) {
1961                                 PMD_INIT_LOG(ERR, "DCB selected, nb_tcs != %d"
1962                                                 " and nb_tcs != %d.",
1963                                                 ETH_4_TCS, ETH_8_TCS);
1964                                 return -EINVAL;
1965                         }
1966                 }
1967
1968                 if (dev_conf->txmode.mq_mode == ETH_MQ_TX_DCB) {
1969                         const struct rte_eth_dcb_tx_conf *conf;
1970
1971                         if (nb_tx_q != IXGBE_DCB_NB_QUEUES) {
1972                                 PMD_INIT_LOG(ERR, "DCB, nb_tx_q != %d.",
1973                                                  IXGBE_DCB_NB_QUEUES);
1974                                 return -EINVAL;
1975                         }
1976                         conf = &dev_conf->tx_adv_conf.dcb_tx_conf;
1977                         if (!(conf->nb_tcs == ETH_4_TCS ||
1978                                conf->nb_tcs == ETH_8_TCS)) {
1979                                 PMD_INIT_LOG(ERR, "DCB selected, nb_tcs != %d"
1980                                                 " and nb_tcs != %d.",
1981                                                 ETH_4_TCS, ETH_8_TCS);
1982                                 return -EINVAL;
1983                         }
1984                 }
1985         }
1986         return 0;
1987 }
1988
1989 static int
1990 ixgbe_dev_configure(struct rte_eth_dev *dev)
1991 {
1992         struct ixgbe_interrupt *intr =
1993                 IXGBE_DEV_PRIVATE_TO_INTR(dev->data->dev_private);
1994         struct ixgbe_adapter *adapter =
1995                 (struct ixgbe_adapter *)dev->data->dev_private;
1996         int ret;
1997
1998         PMD_INIT_FUNC_TRACE();
1999         /* multipe queue mode checking */
2000         ret  = ixgbe_check_mq_mode(dev);
2001         if (ret != 0) {
2002                 PMD_DRV_LOG(ERR, "ixgbe_check_mq_mode fails with %d.",
2003                             ret);
2004                 return ret;
2005         }
2006
2007         /* set flag to update link status after init */
2008         intr->flags |= IXGBE_FLAG_NEED_LINK_UPDATE;
2009
2010         /*
2011          * Initialize to TRUE. If any of Rx queues doesn't meet the bulk
2012          * allocation or vector Rx preconditions we will reset it.
2013          */
2014         adapter->rx_bulk_alloc_allowed = true;
2015         adapter->rx_vec_allowed = true;
2016
2017         return 0;
2018 }
2019
2020 /*
2021  * Configure device link speed and setup link.
2022  * It returns 0 on success.
2023  */
2024 static int
2025 ixgbe_dev_start(struct rte_eth_dev *dev)
2026 {
2027         struct ixgbe_hw *hw =
2028                 IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2029         struct ixgbe_vf_info *vfinfo =
2030                 *IXGBE_DEV_PRIVATE_TO_P_VFDATA(dev->data->dev_private);
2031         struct rte_intr_handle *intr_handle = &dev->pci_dev->intr_handle;
2032         uint32_t intr_vector = 0;
2033         int err, link_up = 0, negotiate = 0;
2034         uint32_t speed = 0;
2035         int mask = 0;
2036         int status;
2037         uint16_t vf, idx;
2038
2039         PMD_INIT_FUNC_TRACE();
2040
2041         /* IXGBE devices don't support half duplex */
2042         if ((dev->data->dev_conf.link_duplex != ETH_LINK_AUTONEG_DUPLEX) &&
2043                         (dev->data->dev_conf.link_duplex != ETH_LINK_FULL_DUPLEX)) {
2044                 PMD_INIT_LOG(ERR, "Invalid link_duplex (%hu) for port %hhu",
2045                              dev->data->dev_conf.link_duplex,
2046                              dev->data->port_id);
2047                 return -EINVAL;
2048         }
2049
2050         /* disable uio/vfio intr/eventfd mapping */
2051         rte_intr_disable(intr_handle);
2052
2053         /* stop adapter */
2054         hw->adapter_stopped = 0;
2055         ixgbe_stop_adapter(hw);
2056
2057         /* reinitialize adapter
2058          * this calls reset and start */
2059         status = ixgbe_pf_reset_hw(hw);
2060         if (status != 0)
2061                 return -1;
2062         hw->mac.ops.start_hw(hw);
2063         hw->mac.get_link_status = true;
2064
2065         /* configure PF module if SRIOV enabled */
2066         ixgbe_pf_host_configure(dev);
2067
2068         /* check and configure queue intr-vector mapping */
2069         if ((rte_intr_cap_multiple(intr_handle) ||
2070              !RTE_ETH_DEV_SRIOV(dev).active) &&
2071             dev->data->dev_conf.intr_conf.rxq != 0) {
2072                 intr_vector = dev->data->nb_rx_queues;
2073                 if (rte_intr_efd_enable(intr_handle, intr_vector))
2074                         return -1;
2075         }
2076
2077         if (rte_intr_dp_is_en(intr_handle) && !intr_handle->intr_vec) {
2078                 intr_handle->intr_vec =
2079                         rte_zmalloc("intr_vec",
2080                                     dev->data->nb_rx_queues * sizeof(int), 0);
2081                 if (intr_handle->intr_vec == NULL) {
2082                         PMD_INIT_LOG(ERR, "Failed to allocate %d rx_queues"
2083                                      " intr_vec\n", dev->data->nb_rx_queues);
2084                         return -ENOMEM;
2085                 }
2086         }
2087
2088         /* confiugre msix for sleep until rx interrupt */
2089         ixgbe_configure_msix(dev);
2090
2091         /* initialize transmission unit */
2092         ixgbe_dev_tx_init(dev);
2093
2094         /* This can fail when allocating mbufs for descriptor rings */
2095         err = ixgbe_dev_rx_init(dev);
2096         if (err) {
2097                 PMD_INIT_LOG(ERR, "Unable to initialize RX hardware");
2098                 goto error;
2099         }
2100
2101         err = ixgbe_dev_rxtx_start(dev);
2102         if (err < 0) {
2103                 PMD_INIT_LOG(ERR, "Unable to start rxtx queues");
2104                 goto error;
2105         }
2106
2107         /* Skip link setup if loopback mode is enabled for 82599. */
2108         if (hw->mac.type == ixgbe_mac_82599EB &&
2109                         dev->data->dev_conf.lpbk_mode == IXGBE_LPBK_82599_TX_RX)
2110                 goto skip_link_setup;
2111
2112         if (ixgbe_is_sfp(hw) && hw->phy.multispeed_fiber) {
2113                 err = hw->mac.ops.setup_sfp(hw);
2114                 if (err)
2115                         goto error;
2116         }
2117
2118         if (hw->mac.ops.get_media_type(hw) == ixgbe_media_type_copper) {
2119                 /* Turn on the copper */
2120                 ixgbe_set_phy_power(hw, true);
2121         } else {
2122                 /* Turn on the laser */
2123                 ixgbe_enable_tx_laser(hw);
2124         }
2125
2126         err = ixgbe_check_link(hw, &speed, &link_up, 0);
2127         if (err)
2128                 goto error;
2129         dev->data->dev_link.link_status = link_up;
2130
2131         err = ixgbe_get_link_capabilities(hw, &speed, &negotiate);
2132         if (err)
2133                 goto error;
2134
2135         switch(dev->data->dev_conf.link_speed) {
2136         case ETH_LINK_SPEED_AUTONEG:
2137                 speed = (hw->mac.type != ixgbe_mac_82598EB) ?
2138                                 IXGBE_LINK_SPEED_82599_AUTONEG :
2139                                 IXGBE_LINK_SPEED_82598_AUTONEG;
2140                 break;
2141         case ETH_LINK_SPEED_100:
2142                 /*
2143                  * Invalid for 82598 but error will be detected by
2144                  * ixgbe_setup_link()
2145                  */
2146                 speed = IXGBE_LINK_SPEED_100_FULL;
2147                 break;
2148         case ETH_LINK_SPEED_1000:
2149                 speed = IXGBE_LINK_SPEED_1GB_FULL;
2150                 break;
2151         case ETH_LINK_SPEED_10000:
2152                 speed = IXGBE_LINK_SPEED_10GB_FULL;
2153                 break;
2154         default:
2155                 PMD_INIT_LOG(ERR, "Invalid link_speed (%hu) for port %hhu",
2156                              dev->data->dev_conf.link_speed,
2157                              dev->data->port_id);
2158                 goto error;
2159         }
2160
2161         err = ixgbe_setup_link(hw, speed, link_up);
2162         if (err)
2163                 goto error;
2164
2165 skip_link_setup:
2166
2167         if (rte_intr_allow_others(intr_handle)) {
2168                 /* check if lsc interrupt is enabled */
2169                 if (dev->data->dev_conf.intr_conf.lsc != 0)
2170                         ixgbe_dev_lsc_interrupt_setup(dev);
2171         } else {
2172                 rte_intr_callback_unregister(intr_handle,
2173                                              ixgbe_dev_interrupt_handler,
2174                                              (void *)dev);
2175                 if (dev->data->dev_conf.intr_conf.lsc != 0)
2176                         PMD_INIT_LOG(INFO, "lsc won't enable because of"
2177                                      " no intr multiplex\n");
2178         }
2179
2180         /* check if rxq interrupt is enabled */
2181         if (dev->data->dev_conf.intr_conf.rxq != 0 &&
2182             rte_intr_dp_is_en(intr_handle))
2183                 ixgbe_dev_rxq_interrupt_setup(dev);
2184
2185         /* enable uio/vfio intr/eventfd mapping */
2186         rte_intr_enable(intr_handle);
2187
2188         /* resume enabled intr since hw reset */
2189         ixgbe_enable_intr(dev);
2190
2191         mask = ETH_VLAN_STRIP_MASK | ETH_VLAN_FILTER_MASK | \
2192                 ETH_VLAN_EXTEND_MASK;
2193         ixgbe_vlan_offload_set(dev, mask);
2194
2195         if (dev->data->dev_conf.rxmode.mq_mode == ETH_MQ_RX_VMDQ_ONLY) {
2196                 /* Enable vlan filtering for VMDq */
2197                 ixgbe_vmdq_vlan_hw_filter_enable(dev);
2198         }
2199
2200         /* Configure DCB hw */
2201         ixgbe_configure_dcb(dev);
2202
2203         if (dev->data->dev_conf.fdir_conf.mode != RTE_FDIR_MODE_NONE) {
2204                 err = ixgbe_fdir_configure(dev);
2205                 if (err)
2206                         goto error;
2207         }
2208
2209         /* Restore vf rate limit */
2210         if (vfinfo != NULL) {
2211                 for (vf = 0; vf < dev->pci_dev->max_vfs; vf++)
2212                         for (idx = 0; idx < IXGBE_MAX_QUEUE_NUM_PER_VF; idx++)
2213                                 if (vfinfo[vf].tx_rate[idx] != 0)
2214                                         ixgbe_set_vf_rate_limit(dev, vf,
2215                                                 vfinfo[vf].tx_rate[idx],
2216                                                 1 << idx);
2217         }
2218
2219         ixgbe_restore_statistics_mapping(dev);
2220
2221         return 0;
2222
2223 error:
2224         PMD_INIT_LOG(ERR, "failure in ixgbe_dev_start(): %d", err);
2225         ixgbe_dev_clear_queues(dev);
2226         return -EIO;
2227 }
2228
2229 /*
2230  * Stop device: disable rx and tx functions to allow for reconfiguring.
2231  */
2232 static void
2233 ixgbe_dev_stop(struct rte_eth_dev *dev)
2234 {
2235         struct rte_eth_link link;
2236         struct ixgbe_hw *hw =
2237                 IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2238         struct ixgbe_vf_info *vfinfo =
2239                 *IXGBE_DEV_PRIVATE_TO_P_VFDATA(dev->data->dev_private);
2240         struct ixgbe_filter_info *filter_info =
2241                 IXGBE_DEV_PRIVATE_TO_FILTER_INFO(dev->data->dev_private);
2242         struct ixgbe_5tuple_filter *p_5tuple, *p_5tuple_next;
2243         struct rte_intr_handle *intr_handle = &dev->pci_dev->intr_handle;
2244         int vf;
2245
2246         PMD_INIT_FUNC_TRACE();
2247
2248         /* disable interrupts */
2249         ixgbe_disable_intr(hw);
2250
2251         /* disable intr eventfd mapping */
2252         rte_intr_disable(intr_handle);
2253
2254         /* reset the NIC */
2255         ixgbe_pf_reset_hw(hw);
2256         hw->adapter_stopped = 0;
2257
2258         /* stop adapter */
2259         ixgbe_stop_adapter(hw);
2260
2261         for (vf = 0; vfinfo != NULL &&
2262                      vf < dev->pci_dev->max_vfs; vf++)
2263                 vfinfo[vf].clear_to_send = false;
2264
2265         if (hw->mac.ops.get_media_type(hw) == ixgbe_media_type_copper) {
2266                 /* Turn off the copper */
2267                 ixgbe_set_phy_power(hw, false);
2268         } else {
2269                 /* Turn off the laser */
2270                 ixgbe_disable_tx_laser(hw);
2271         }
2272
2273         ixgbe_dev_clear_queues(dev);
2274
2275         /* Clear stored conf */
2276         dev->data->scattered_rx = 0;
2277         dev->data->lro = 0;
2278
2279         /* Clear recorded link status */
2280         memset(&link, 0, sizeof(link));
2281         rte_ixgbe_dev_atomic_write_link_status(dev, &link);
2282
2283         /* Remove all ntuple filters of the device */
2284         for (p_5tuple = TAILQ_FIRST(&filter_info->fivetuple_list);
2285              p_5tuple != NULL; p_5tuple = p_5tuple_next) {
2286                 p_5tuple_next = TAILQ_NEXT(p_5tuple, entries);
2287                 TAILQ_REMOVE(&filter_info->fivetuple_list,
2288                              p_5tuple, entries);
2289                 rte_free(p_5tuple);
2290         }
2291         memset(filter_info->fivetuple_mask, 0,
2292                 sizeof(uint32_t) * IXGBE_5TUPLE_ARRAY_SIZE);
2293
2294         if (!rte_intr_allow_others(intr_handle))
2295                 /* resume to the default handler */
2296                 rte_intr_callback_register(intr_handle,
2297                                            ixgbe_dev_interrupt_handler,
2298                                            (void *)dev);
2299
2300         /* Clean datapath event and queue/vec mapping */
2301         rte_intr_efd_disable(intr_handle);
2302         if (intr_handle->intr_vec != NULL) {
2303                 rte_free(intr_handle->intr_vec);
2304                 intr_handle->intr_vec = NULL;
2305         }
2306 }
2307
2308 /*
2309  * Set device link up: enable tx.
2310  */
2311 static int
2312 ixgbe_dev_set_link_up(struct rte_eth_dev *dev)
2313 {
2314         struct ixgbe_hw *hw =
2315                 IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2316         if (hw->mac.type == ixgbe_mac_82599EB) {
2317 #ifdef RTE_NIC_BYPASS
2318                 if (hw->device_id == IXGBE_DEV_ID_82599_BYPASS) {
2319                         /* Not suported in bypass mode */
2320                         PMD_INIT_LOG(ERR, "Set link up is not supported "
2321                                      "by device id 0x%x", hw->device_id);
2322                         return -ENOTSUP;
2323                 }
2324 #endif
2325         }
2326
2327         if (hw->mac.ops.get_media_type(hw) == ixgbe_media_type_copper) {
2328                 /* Turn on the copper */
2329                 ixgbe_set_phy_power(hw, true);
2330         } else {
2331                 /* Turn on the laser */
2332                 ixgbe_enable_tx_laser(hw);
2333         }
2334
2335         return 0;
2336 }
2337
2338 /*
2339  * Set device link down: disable tx.
2340  */
2341 static int
2342 ixgbe_dev_set_link_down(struct rte_eth_dev *dev)
2343 {
2344         struct ixgbe_hw *hw =
2345                 IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2346         if (hw->mac.type == ixgbe_mac_82599EB) {
2347 #ifdef RTE_NIC_BYPASS
2348                 if (hw->device_id == IXGBE_DEV_ID_82599_BYPASS) {
2349                         /* Not suported in bypass mode */
2350                         PMD_INIT_LOG(ERR, "Set link down is not supported "
2351                                      "by device id 0x%x", hw->device_id);
2352                         return -ENOTSUP;
2353                 }
2354 #endif
2355         }
2356
2357         if (hw->mac.ops.get_media_type(hw) == ixgbe_media_type_copper) {
2358                 /* Turn off the copper */
2359                 ixgbe_set_phy_power(hw, false);
2360         } else {
2361                 /* Turn off the laser */
2362                 ixgbe_disable_tx_laser(hw);
2363         }
2364
2365         return 0;
2366 }
2367
2368 /*
2369  * Reest and stop device.
2370  */
2371 static void
2372 ixgbe_dev_close(struct rte_eth_dev *dev)
2373 {
2374         struct ixgbe_hw *hw =
2375                 IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2376
2377         PMD_INIT_FUNC_TRACE();
2378
2379         ixgbe_pf_reset_hw(hw);
2380
2381         ixgbe_dev_stop(dev);
2382         hw->adapter_stopped = 1;
2383
2384         ixgbe_dev_free_queues(dev);
2385
2386         ixgbe_disable_pcie_master(hw);
2387
2388         /* reprogram the RAR[0] in case user changed it. */
2389         ixgbe_set_rar(hw, 0, hw->mac.addr, 0, IXGBE_RAH_AV);
2390 }
2391
2392 static void
2393 ixgbe_read_stats_registers(struct ixgbe_hw *hw,
2394                            struct ixgbe_hw_stats *hw_stats,
2395                            uint64_t *total_missed_rx, uint64_t *total_qbrc,
2396                            uint64_t *total_qprc, uint64_t *total_qprdc)
2397 {
2398         uint32_t bprc, lxon, lxoff, total;
2399         uint32_t delta_gprc = 0;
2400         unsigned i;
2401         /* Workaround for RX byte count not including CRC bytes when CRC
2402 +        * strip is enabled. CRC bytes are removed from counters when crc_strip
2403          * is disabled.
2404 +        */
2405         int crc_strip = (IXGBE_READ_REG(hw, IXGBE_HLREG0) &
2406                         IXGBE_HLREG0_RXCRCSTRP);
2407
2408         hw_stats->crcerrs += IXGBE_READ_REG(hw, IXGBE_CRCERRS);
2409         hw_stats->illerrc += IXGBE_READ_REG(hw, IXGBE_ILLERRC);
2410         hw_stats->errbc += IXGBE_READ_REG(hw, IXGBE_ERRBC);
2411         hw_stats->mspdc += IXGBE_READ_REG(hw, IXGBE_MSPDC);
2412
2413         for (i = 0; i < 8; i++) {
2414                 uint32_t mp;
2415                 mp = IXGBE_READ_REG(hw, IXGBE_MPC(i));
2416                 /* global total per queue */
2417                 hw_stats->mpc[i] += mp;
2418                 /* Running comprehensive total for stats display */
2419                 *total_missed_rx += hw_stats->mpc[i];
2420                 if (hw->mac.type == ixgbe_mac_82598EB) {
2421                         hw_stats->rnbc[i] +=
2422                             IXGBE_READ_REG(hw, IXGBE_RNBC(i));
2423                         hw_stats->pxonrxc[i] +=
2424                                 IXGBE_READ_REG(hw, IXGBE_PXONRXC(i));
2425                         hw_stats->pxoffrxc[i] +=
2426                                 IXGBE_READ_REG(hw, IXGBE_PXOFFRXC(i));
2427                 } else {
2428                         hw_stats->pxonrxc[i] +=
2429                                 IXGBE_READ_REG(hw, IXGBE_PXONRXCNT(i));
2430                         hw_stats->pxoffrxc[i] +=
2431                                 IXGBE_READ_REG(hw, IXGBE_PXOFFRXCNT(i));
2432                         hw_stats->pxon2offc[i] +=
2433                                 IXGBE_READ_REG(hw, IXGBE_PXON2OFFCNT(i));
2434                 }
2435                 hw_stats->pxontxc[i] +=
2436                     IXGBE_READ_REG(hw, IXGBE_PXONTXC(i));
2437                 hw_stats->pxofftxc[i] +=
2438                     IXGBE_READ_REG(hw, IXGBE_PXOFFTXC(i));
2439         }
2440         for (i = 0; i < IXGBE_QUEUE_STAT_COUNTERS; i++) {
2441                 uint32_t delta_qprc = IXGBE_READ_REG(hw, IXGBE_QPRC(i));
2442                 uint32_t delta_qptc = IXGBE_READ_REG(hw, IXGBE_QPTC(i));
2443                 uint32_t delta_qprdc = IXGBE_READ_REG(hw, IXGBE_QPRDC(i));
2444
2445                 delta_gprc += delta_qprc;
2446
2447                 hw_stats->qprc[i] += delta_qprc;
2448                 hw_stats->qptc[i] += delta_qptc;
2449
2450                 hw_stats->qbrc[i] += IXGBE_READ_REG(hw, IXGBE_QBRC_L(i));
2451                 hw_stats->qbrc[i] +=
2452                     ((uint64_t)IXGBE_READ_REG(hw, IXGBE_QBRC_H(i)) << 32);
2453                 if (crc_strip == 0)
2454                         hw_stats->qbrc[i] -= delta_qprc * ETHER_CRC_LEN;
2455
2456                 hw_stats->qbtc[i] += IXGBE_READ_REG(hw, IXGBE_QBTC_L(i));
2457                 hw_stats->qbtc[i] +=
2458                     ((uint64_t)IXGBE_READ_REG(hw, IXGBE_QBTC_H(i)) << 32);
2459
2460                 hw_stats->qprdc[i] += delta_qprdc;
2461                 *total_qprdc += hw_stats->qprdc[i];
2462
2463                 *total_qprc += hw_stats->qprc[i];
2464                 *total_qbrc += hw_stats->qbrc[i];
2465         }
2466         hw_stats->mlfc += IXGBE_READ_REG(hw, IXGBE_MLFC);
2467         hw_stats->mrfc += IXGBE_READ_REG(hw, IXGBE_MRFC);
2468         hw_stats->rlec += IXGBE_READ_REG(hw, IXGBE_RLEC);
2469
2470         /*
2471          * An errata states that gprc actually counts good + missed packets:
2472          * Workaround to set gprc to summated queue packet receives
2473          */
2474         hw_stats->gprc = *total_qprc;
2475
2476         if (hw->mac.type != ixgbe_mac_82598EB) {
2477                 hw_stats->gorc += IXGBE_READ_REG(hw, IXGBE_GORCL);
2478                 hw_stats->gorc += ((u64)IXGBE_READ_REG(hw, IXGBE_GORCH) << 32);
2479                 hw_stats->gotc += IXGBE_READ_REG(hw, IXGBE_GOTCL);
2480                 hw_stats->gotc += ((u64)IXGBE_READ_REG(hw, IXGBE_GOTCH) << 32);
2481                 hw_stats->tor += IXGBE_READ_REG(hw, IXGBE_TORL);
2482                 hw_stats->tor += ((u64)IXGBE_READ_REG(hw, IXGBE_TORH) << 32);
2483                 hw_stats->lxonrxc += IXGBE_READ_REG(hw, IXGBE_LXONRXCNT);
2484                 hw_stats->lxoffrxc += IXGBE_READ_REG(hw, IXGBE_LXOFFRXCNT);
2485         } else {
2486                 hw_stats->lxonrxc += IXGBE_READ_REG(hw, IXGBE_LXONRXC);
2487                 hw_stats->lxoffrxc += IXGBE_READ_REG(hw, IXGBE_LXOFFRXC);
2488                 /* 82598 only has a counter in the high register */
2489                 hw_stats->gorc += IXGBE_READ_REG(hw, IXGBE_GORCH);
2490                 hw_stats->gotc += IXGBE_READ_REG(hw, IXGBE_GOTCH);
2491                 hw_stats->tor += IXGBE_READ_REG(hw, IXGBE_TORH);
2492         }
2493         uint64_t old_tpr = hw_stats->tpr;
2494
2495         hw_stats->tpr += IXGBE_READ_REG(hw, IXGBE_TPR);
2496         hw_stats->tpt += IXGBE_READ_REG(hw, IXGBE_TPT);
2497
2498         if (crc_strip == 0)
2499                 hw_stats->gorc -= delta_gprc * ETHER_CRC_LEN;
2500
2501         uint64_t delta_gptc = IXGBE_READ_REG(hw, IXGBE_GPTC);
2502         hw_stats->gptc += delta_gptc;
2503         hw_stats->gotc -= delta_gptc * ETHER_CRC_LEN;
2504         hw_stats->tor -= (hw_stats->tpr - old_tpr) * ETHER_CRC_LEN;
2505
2506         /*
2507          * Workaround: mprc hardware is incorrectly counting
2508          * broadcasts, so for now we subtract those.
2509          */
2510         bprc = IXGBE_READ_REG(hw, IXGBE_BPRC);
2511         hw_stats->bprc += bprc;
2512         hw_stats->mprc += IXGBE_READ_REG(hw, IXGBE_MPRC);
2513         if (hw->mac.type == ixgbe_mac_82598EB)
2514                 hw_stats->mprc -= bprc;
2515
2516         hw_stats->prc64 += IXGBE_READ_REG(hw, IXGBE_PRC64);
2517         hw_stats->prc127 += IXGBE_READ_REG(hw, IXGBE_PRC127);
2518         hw_stats->prc255 += IXGBE_READ_REG(hw, IXGBE_PRC255);
2519         hw_stats->prc511 += IXGBE_READ_REG(hw, IXGBE_PRC511);
2520         hw_stats->prc1023 += IXGBE_READ_REG(hw, IXGBE_PRC1023);
2521         hw_stats->prc1522 += IXGBE_READ_REG(hw, IXGBE_PRC1522);
2522
2523         lxon = IXGBE_READ_REG(hw, IXGBE_LXONTXC);
2524         hw_stats->lxontxc += lxon;
2525         lxoff = IXGBE_READ_REG(hw, IXGBE_LXOFFTXC);
2526         hw_stats->lxofftxc += lxoff;
2527         total = lxon + lxoff;
2528
2529         hw_stats->mptc += IXGBE_READ_REG(hw, IXGBE_MPTC);
2530         hw_stats->ptc64 += IXGBE_READ_REG(hw, IXGBE_PTC64);
2531         hw_stats->gptc -= total;
2532         hw_stats->mptc -= total;
2533         hw_stats->ptc64 -= total;
2534         hw_stats->gotc -= total * ETHER_MIN_LEN;
2535
2536         hw_stats->ruc += IXGBE_READ_REG(hw, IXGBE_RUC);
2537         hw_stats->rfc += IXGBE_READ_REG(hw, IXGBE_RFC);
2538         hw_stats->roc += IXGBE_READ_REG(hw, IXGBE_ROC);
2539         hw_stats->rjc += IXGBE_READ_REG(hw, IXGBE_RJC);
2540         hw_stats->mngprc += IXGBE_READ_REG(hw, IXGBE_MNGPRC);
2541         hw_stats->mngpdc += IXGBE_READ_REG(hw, IXGBE_MNGPDC);
2542         hw_stats->mngptc += IXGBE_READ_REG(hw, IXGBE_MNGPTC);
2543         hw_stats->ptc127 += IXGBE_READ_REG(hw, IXGBE_PTC127);
2544         hw_stats->ptc255 += IXGBE_READ_REG(hw, IXGBE_PTC255);
2545         hw_stats->ptc511 += IXGBE_READ_REG(hw, IXGBE_PTC511);
2546         hw_stats->ptc1023 += IXGBE_READ_REG(hw, IXGBE_PTC1023);
2547         hw_stats->ptc1522 += IXGBE_READ_REG(hw, IXGBE_PTC1522);
2548         hw_stats->bptc += IXGBE_READ_REG(hw, IXGBE_BPTC);
2549         hw_stats->xec += IXGBE_READ_REG(hw, IXGBE_XEC);
2550         hw_stats->fccrc += IXGBE_READ_REG(hw, IXGBE_FCCRC);
2551         hw_stats->fclast += IXGBE_READ_REG(hw, IXGBE_FCLAST);
2552         /* Only read FCOE on 82599 */
2553         if (hw->mac.type != ixgbe_mac_82598EB) {
2554                 hw_stats->fcoerpdc += IXGBE_READ_REG(hw, IXGBE_FCOERPDC);
2555                 hw_stats->fcoeprc += IXGBE_READ_REG(hw, IXGBE_FCOEPRC);
2556                 hw_stats->fcoeptc += IXGBE_READ_REG(hw, IXGBE_FCOEPTC);
2557                 hw_stats->fcoedwrc += IXGBE_READ_REG(hw, IXGBE_FCOEDWRC);
2558                 hw_stats->fcoedwtc += IXGBE_READ_REG(hw, IXGBE_FCOEDWTC);
2559         }
2560
2561         /* Flow Director Stats registers */
2562         hw_stats->fdirmatch += IXGBE_READ_REG(hw, IXGBE_FDIRMATCH);
2563         hw_stats->fdirmiss += IXGBE_READ_REG(hw, IXGBE_FDIRMISS);
2564 }
2565
2566 /*
2567  * This function is based on ixgbe_update_stats_counters() in ixgbe/ixgbe.c
2568  */
2569 static void
2570 ixgbe_dev_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
2571 {
2572         struct ixgbe_hw *hw =
2573                         IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2574         struct ixgbe_hw_stats *hw_stats =
2575                         IXGBE_DEV_PRIVATE_TO_STATS(dev->data->dev_private);
2576         uint64_t total_missed_rx, total_qbrc, total_qprc, total_qprdc;
2577         unsigned i;
2578
2579         total_missed_rx = 0;
2580         total_qbrc = 0;
2581         total_qprc = 0;
2582         total_qprdc = 0;
2583
2584         ixgbe_read_stats_registers(hw, hw_stats, &total_missed_rx, &total_qbrc,
2585                         &total_qprc, &total_qprdc);
2586
2587         if (stats == NULL)
2588                 return;
2589
2590         /* Fill out the rte_eth_stats statistics structure */
2591         stats->ipackets = total_qprc;
2592         stats->ibytes = total_qbrc;
2593         stats->opackets = hw_stats->gptc;
2594         stats->obytes = hw_stats->gotc;
2595
2596         for (i = 0; i < IXGBE_QUEUE_STAT_COUNTERS; i++) {
2597                 stats->q_ipackets[i] = hw_stats->qprc[i];
2598                 stats->q_opackets[i] = hw_stats->qptc[i];
2599                 stats->q_ibytes[i] = hw_stats->qbrc[i];
2600                 stats->q_obytes[i] = hw_stats->qbtc[i];
2601                 stats->q_errors[i] = hw_stats->qprdc[i];
2602         }
2603
2604         /* Rx Errors */
2605         stats->imissed  = total_missed_rx;
2606         stats->ierrors  = hw_stats->crcerrs +
2607                           hw_stats->mspdc +
2608                           hw_stats->rlec +
2609                           hw_stats->ruc +
2610                           hw_stats->roc +
2611                           total_missed_rx +
2612                           hw_stats->illerrc +
2613                           hw_stats->errbc +
2614                           hw_stats->rfc +
2615                           hw_stats->fccrc +
2616                           hw_stats->fclast;
2617
2618         /* Tx Errors */
2619         stats->oerrors  = 0;
2620 }
2621
2622 static void
2623 ixgbe_dev_stats_reset(struct rte_eth_dev *dev)
2624 {
2625         struct ixgbe_hw_stats *stats =
2626                         IXGBE_DEV_PRIVATE_TO_STATS(dev->data->dev_private);
2627
2628         /* HW registers are cleared on read */
2629         ixgbe_dev_stats_get(dev, NULL);
2630
2631         /* Reset software totals */
2632         memset(stats, 0, sizeof(*stats));
2633 }
2634
2635 /* This function calculates the number of xstats based on the current config */
2636 static unsigned
2637 ixgbe_xstats_calc_num(void) {
2638         return IXGBE_NB_HW_STATS + (IXGBE_NB_RXQ_PRIO_STATS * 8) +
2639                 (IXGBE_NB_TXQ_PRIO_STATS * 8);
2640 }
2641
2642 static int
2643 ixgbe_dev_xstats_get(struct rte_eth_dev *dev, struct rte_eth_xstats *xstats,
2644                                          unsigned n)
2645 {
2646         struct ixgbe_hw *hw =
2647                         IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2648         struct ixgbe_hw_stats *hw_stats =
2649                         IXGBE_DEV_PRIVATE_TO_STATS(dev->data->dev_private);
2650         uint64_t total_missed_rx, total_qbrc, total_qprc, total_qprdc;
2651         unsigned i, stat, count = 0;
2652
2653         count = ixgbe_xstats_calc_num();
2654
2655         if (n < count)
2656                 return count;
2657
2658         total_missed_rx = 0;
2659         total_qbrc = 0;
2660         total_qprc = 0;
2661         total_qprdc = 0;
2662
2663         ixgbe_read_stats_registers(hw, hw_stats, &total_missed_rx, &total_qbrc,
2664                                    &total_qprc, &total_qprdc);
2665
2666         /* If this is a reset xstats is NULL, and we have cleared the
2667          * registers by reading them.
2668          */
2669         if (!xstats)
2670                 return 0;
2671
2672         /* Extended stats from ixgbe_hw_stats */
2673         count = 0;
2674         for (i = 0; i < IXGBE_NB_HW_STATS; i++) {
2675                 snprintf(xstats[count].name, sizeof(xstats[count].name), "%s",
2676                          rte_ixgbe_stats_strings[i].name);
2677                 xstats[count].value = *(uint64_t *)(((char *)hw_stats) +
2678                                 rte_ixgbe_stats_strings[i].offset);
2679                 count++;
2680         }
2681
2682         /* RX Priority Stats */
2683         for (stat = 0; stat < IXGBE_NB_RXQ_PRIO_STATS; stat++) {
2684                 for (i = 0; i < 8; i++) {
2685                         snprintf(xstats[count].name, sizeof(xstats[count].name),
2686                                  "rx_priority%u_%s", i,
2687                                  rte_ixgbe_rxq_strings[stat].name);
2688                         xstats[count].value = *(uint64_t *)(((char *)hw_stats) +
2689                                         rte_ixgbe_rxq_strings[stat].offset +
2690                                         (sizeof(uint64_t) * i));
2691                         count++;
2692                 }
2693         }
2694
2695         /* TX Priority Stats */
2696         for (stat = 0; stat < IXGBE_NB_TXQ_PRIO_STATS; stat++) {
2697                 for (i = 0; i < 8; i++) {
2698                         snprintf(xstats[count].name, sizeof(xstats[count].name),
2699                                  "tx_priority%u_%s", i,
2700                                  rte_ixgbe_txq_strings[stat].name);
2701                         xstats[count].value = *(uint64_t *)(((char *)hw_stats) +
2702                                         rte_ixgbe_txq_strings[stat].offset +
2703                                         (sizeof(uint64_t) * i));
2704                         count++;
2705                 }
2706         }
2707
2708         return count;
2709 }
2710
2711 static void
2712 ixgbe_dev_xstats_reset(struct rte_eth_dev *dev)
2713 {
2714         struct ixgbe_hw_stats *stats =
2715                         IXGBE_DEV_PRIVATE_TO_STATS(dev->data->dev_private);
2716
2717         unsigned count = ixgbe_xstats_calc_num();
2718
2719         /* HW registers are cleared on read */
2720         ixgbe_dev_xstats_get(dev, NULL, count);
2721
2722         /* Reset software totals */
2723         memset(stats, 0, sizeof(*stats));
2724 }
2725
2726 static void
2727 ixgbevf_update_stats(struct rte_eth_dev *dev)
2728 {
2729         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2730         struct ixgbevf_hw_stats *hw_stats = (struct ixgbevf_hw_stats*)
2731                           IXGBE_DEV_PRIVATE_TO_STATS(dev->data->dev_private);
2732
2733         /* Good Rx packet, include VF loopback */
2734         UPDATE_VF_STAT(IXGBE_VFGPRC,
2735             hw_stats->last_vfgprc, hw_stats->vfgprc);
2736
2737         /* Good Rx octets, include VF loopback */
2738         UPDATE_VF_STAT_36BIT(IXGBE_VFGORC_LSB, IXGBE_VFGORC_MSB,
2739             hw_stats->last_vfgorc, hw_stats->vfgorc);
2740
2741         /* Good Tx packet, include VF loopback */
2742         UPDATE_VF_STAT(IXGBE_VFGPTC,
2743             hw_stats->last_vfgptc, hw_stats->vfgptc);
2744
2745         /* Good Tx octets, include VF loopback */
2746         UPDATE_VF_STAT_36BIT(IXGBE_VFGOTC_LSB, IXGBE_VFGOTC_MSB,
2747             hw_stats->last_vfgotc, hw_stats->vfgotc);
2748
2749         /* Rx Multicst Packet */
2750         UPDATE_VF_STAT(IXGBE_VFMPRC,
2751             hw_stats->last_vfmprc, hw_stats->vfmprc);
2752 }
2753
2754 static int
2755 ixgbevf_dev_xstats_get(struct rte_eth_dev *dev, struct rte_eth_xstats *xstats,
2756                        unsigned n)
2757 {
2758         struct ixgbevf_hw_stats *hw_stats = (struct ixgbevf_hw_stats *)
2759                         IXGBE_DEV_PRIVATE_TO_STATS(dev->data->dev_private);
2760         unsigned i;
2761
2762         if (n < IXGBEVF_NB_XSTATS)
2763                 return IXGBEVF_NB_XSTATS;
2764
2765         ixgbevf_update_stats(dev);
2766
2767         if (!xstats)
2768                 return 0;
2769
2770         /* Extended stats */
2771         for (i = 0; i < IXGBEVF_NB_XSTATS; i++) {
2772                 snprintf(xstats[i].name, sizeof(xstats[i].name),
2773                          "%s", rte_ixgbevf_stats_strings[i].name);
2774                 xstats[i].value = *(uint64_t *)(((char *)hw_stats) +
2775                         rte_ixgbevf_stats_strings[i].offset);
2776         }
2777
2778         return IXGBEVF_NB_XSTATS;
2779 }
2780
2781 static void
2782 ixgbevf_dev_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
2783 {
2784         struct ixgbevf_hw_stats *hw_stats = (struct ixgbevf_hw_stats *)
2785                           IXGBE_DEV_PRIVATE_TO_STATS(dev->data->dev_private);
2786
2787         ixgbevf_update_stats(dev);
2788
2789         if (stats == NULL)
2790                 return;
2791
2792         stats->ipackets = hw_stats->vfgprc;
2793         stats->ibytes = hw_stats->vfgorc;
2794         stats->opackets = hw_stats->vfgptc;
2795         stats->obytes = hw_stats->vfgotc;
2796         stats->imcasts = hw_stats->vfmprc;
2797         /* stats->imcasts should be removed as imcasts is deprecated */
2798 }
2799
2800 static void
2801 ixgbevf_dev_stats_reset(struct rte_eth_dev *dev)
2802 {
2803         struct ixgbevf_hw_stats *hw_stats = (struct ixgbevf_hw_stats*)
2804                         IXGBE_DEV_PRIVATE_TO_STATS(dev->data->dev_private);
2805
2806         /* Sync HW register to the last stats */
2807         ixgbevf_dev_stats_get(dev, NULL);
2808
2809         /* reset HW current stats*/
2810         hw_stats->vfgprc = 0;
2811         hw_stats->vfgorc = 0;
2812         hw_stats->vfgptc = 0;
2813         hw_stats->vfgotc = 0;
2814         hw_stats->vfmprc = 0;
2815
2816 }
2817
2818 static void
2819 ixgbe_dev_info_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *dev_info)
2820 {
2821         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2822
2823         dev_info->max_rx_queues = (uint16_t)hw->mac.max_rx_queues;
2824         dev_info->max_tx_queues = (uint16_t)hw->mac.max_tx_queues;
2825         dev_info->min_rx_bufsize = 1024; /* cf BSIZEPACKET in SRRCTL register */
2826         dev_info->max_rx_pktlen = 15872; /* includes CRC, cf MAXFRS register */
2827         dev_info->max_mac_addrs = hw->mac.num_rar_entries;
2828         dev_info->max_hash_mac_addrs = IXGBE_VMDQ_NUM_UC_MAC;
2829         dev_info->max_vfs = dev->pci_dev->max_vfs;
2830         if (hw->mac.type == ixgbe_mac_82598EB)
2831                 dev_info->max_vmdq_pools = ETH_16_POOLS;
2832         else
2833                 dev_info->max_vmdq_pools = ETH_64_POOLS;
2834         dev_info->vmdq_queue_num = dev_info->max_rx_queues;
2835         dev_info->rx_offload_capa =
2836                 DEV_RX_OFFLOAD_VLAN_STRIP |
2837                 DEV_RX_OFFLOAD_IPV4_CKSUM |
2838                 DEV_RX_OFFLOAD_UDP_CKSUM  |
2839                 DEV_RX_OFFLOAD_TCP_CKSUM;
2840
2841         /*
2842          * RSC is only supported by 82599 and x540 PF devices in a non-SR-IOV
2843          * mode.
2844          */
2845         if ((hw->mac.type == ixgbe_mac_82599EB ||
2846              hw->mac.type == ixgbe_mac_X540) &&
2847             !RTE_ETH_DEV_SRIOV(dev).active)
2848                 dev_info->rx_offload_capa |= DEV_RX_OFFLOAD_TCP_LRO;
2849
2850         dev_info->tx_offload_capa =
2851                 DEV_TX_OFFLOAD_VLAN_INSERT |
2852                 DEV_TX_OFFLOAD_IPV4_CKSUM  |
2853                 DEV_TX_OFFLOAD_UDP_CKSUM   |
2854                 DEV_TX_OFFLOAD_TCP_CKSUM   |
2855                 DEV_TX_OFFLOAD_SCTP_CKSUM  |
2856                 DEV_TX_OFFLOAD_TCP_TSO;
2857
2858         dev_info->default_rxconf = (struct rte_eth_rxconf) {
2859                 .rx_thresh = {
2860                         .pthresh = IXGBE_DEFAULT_RX_PTHRESH,
2861                         .hthresh = IXGBE_DEFAULT_RX_HTHRESH,
2862                         .wthresh = IXGBE_DEFAULT_RX_WTHRESH,
2863                 },
2864                 .rx_free_thresh = IXGBE_DEFAULT_RX_FREE_THRESH,
2865                 .rx_drop_en = 0,
2866         };
2867
2868         dev_info->default_txconf = (struct rte_eth_txconf) {
2869                 .tx_thresh = {
2870                         .pthresh = IXGBE_DEFAULT_TX_PTHRESH,
2871                         .hthresh = IXGBE_DEFAULT_TX_HTHRESH,
2872                         .wthresh = IXGBE_DEFAULT_TX_WTHRESH,
2873                 },
2874                 .tx_free_thresh = IXGBE_DEFAULT_TX_FREE_THRESH,
2875                 .tx_rs_thresh = IXGBE_DEFAULT_TX_RSBIT_THRESH,
2876                 .txq_flags = ETH_TXQ_FLAGS_NOMULTSEGS |
2877                                 ETH_TXQ_FLAGS_NOOFFLOADS,
2878         };
2879
2880         dev_info->rx_desc_lim = rx_desc_lim;
2881         dev_info->tx_desc_lim = tx_desc_lim;
2882
2883         dev_info->hash_key_size = IXGBE_HKEY_MAX_INDEX * sizeof(uint32_t);
2884         dev_info->reta_size = ixgbe_reta_size_get(hw->mac.type);
2885         dev_info->flow_type_rss_offloads = IXGBE_RSS_OFFLOAD_ALL;
2886 }
2887
2888 static void
2889 ixgbevf_dev_info_get(struct rte_eth_dev *dev,
2890                      struct rte_eth_dev_info *dev_info)
2891 {
2892         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2893
2894         dev_info->max_rx_queues = (uint16_t)hw->mac.max_rx_queues;
2895         dev_info->max_tx_queues = (uint16_t)hw->mac.max_tx_queues;
2896         dev_info->min_rx_bufsize = 1024; /* cf BSIZEPACKET in SRRCTL reg */
2897         dev_info->max_rx_pktlen = 15872; /* includes CRC, cf MAXFRS reg */
2898         dev_info->max_mac_addrs = hw->mac.num_rar_entries;
2899         dev_info->max_hash_mac_addrs = IXGBE_VMDQ_NUM_UC_MAC;
2900         dev_info->max_vfs = dev->pci_dev->max_vfs;
2901         if (hw->mac.type == ixgbe_mac_82598EB)
2902                 dev_info->max_vmdq_pools = ETH_16_POOLS;
2903         else
2904                 dev_info->max_vmdq_pools = ETH_64_POOLS;
2905         dev_info->rx_offload_capa = DEV_RX_OFFLOAD_VLAN_STRIP |
2906                                 DEV_RX_OFFLOAD_IPV4_CKSUM |
2907                                 DEV_RX_OFFLOAD_UDP_CKSUM  |
2908                                 DEV_RX_OFFLOAD_TCP_CKSUM;
2909         dev_info->tx_offload_capa = DEV_TX_OFFLOAD_VLAN_INSERT |
2910                                 DEV_TX_OFFLOAD_IPV4_CKSUM  |
2911                                 DEV_TX_OFFLOAD_UDP_CKSUM   |
2912                                 DEV_TX_OFFLOAD_TCP_CKSUM   |
2913                                 DEV_TX_OFFLOAD_SCTP_CKSUM  |
2914                                 DEV_TX_OFFLOAD_TCP_TSO;
2915
2916         dev_info->default_rxconf = (struct rte_eth_rxconf) {
2917                 .rx_thresh = {
2918                         .pthresh = IXGBE_DEFAULT_RX_PTHRESH,
2919                         .hthresh = IXGBE_DEFAULT_RX_HTHRESH,
2920                         .wthresh = IXGBE_DEFAULT_RX_WTHRESH,
2921                 },
2922                 .rx_free_thresh = IXGBE_DEFAULT_RX_FREE_THRESH,
2923                 .rx_drop_en = 0,
2924         };
2925
2926         dev_info->default_txconf = (struct rte_eth_txconf) {
2927                 .tx_thresh = {
2928                         .pthresh = IXGBE_DEFAULT_TX_PTHRESH,
2929                         .hthresh = IXGBE_DEFAULT_TX_HTHRESH,
2930                         .wthresh = IXGBE_DEFAULT_TX_WTHRESH,
2931                 },
2932                 .tx_free_thresh = IXGBE_DEFAULT_TX_FREE_THRESH,
2933                 .tx_rs_thresh = IXGBE_DEFAULT_TX_RSBIT_THRESH,
2934                 .txq_flags = ETH_TXQ_FLAGS_NOMULTSEGS |
2935                                 ETH_TXQ_FLAGS_NOOFFLOADS,
2936         };
2937
2938         dev_info->rx_desc_lim = rx_desc_lim;
2939         dev_info->tx_desc_lim = tx_desc_lim;
2940 }
2941
2942 /* return 0 means link status changed, -1 means not changed */
2943 static int
2944 ixgbe_dev_link_update(struct rte_eth_dev *dev, int wait_to_complete)
2945 {
2946         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2947         struct rte_eth_link link, old;
2948         ixgbe_link_speed link_speed = IXGBE_LINK_SPEED_UNKNOWN;
2949         int link_up;
2950         int diag;
2951
2952         link.link_status = 0;
2953         link.link_speed = 0;
2954         link.link_duplex = 0;
2955         memset(&old, 0, sizeof(old));
2956         rte_ixgbe_dev_atomic_read_link_status(dev, &old);
2957
2958         hw->mac.get_link_status = true;
2959
2960         /* check if it needs to wait to complete, if lsc interrupt is enabled */
2961         if (wait_to_complete == 0 || dev->data->dev_conf.intr_conf.lsc != 0)
2962                 diag = ixgbe_check_link(hw, &link_speed, &link_up, 0);
2963         else
2964                 diag = ixgbe_check_link(hw, &link_speed, &link_up, 1);
2965
2966         if (diag != 0) {
2967                 link.link_speed = ETH_LINK_SPEED_100;
2968                 link.link_duplex = ETH_LINK_HALF_DUPLEX;
2969                 rte_ixgbe_dev_atomic_write_link_status(dev, &link);
2970                 if (link.link_status == old.link_status)
2971                         return -1;
2972                 return 0;
2973         }
2974
2975         if (link_up == 0) {
2976                 rte_ixgbe_dev_atomic_write_link_status(dev, &link);
2977                 if (link.link_status == old.link_status)
2978                         return -1;
2979                 return 0;
2980         }
2981         link.link_status = 1;
2982         link.link_duplex = ETH_LINK_FULL_DUPLEX;
2983
2984         switch (link_speed) {
2985         default:
2986         case IXGBE_LINK_SPEED_UNKNOWN:
2987                 link.link_duplex = ETH_LINK_HALF_DUPLEX;
2988                 link.link_speed = ETH_LINK_SPEED_100;
2989                 break;
2990
2991         case IXGBE_LINK_SPEED_100_FULL:
2992                 link.link_speed = ETH_LINK_SPEED_100;
2993                 break;
2994
2995         case IXGBE_LINK_SPEED_1GB_FULL:
2996                 link.link_speed = ETH_LINK_SPEED_1000;
2997                 break;
2998
2999         case IXGBE_LINK_SPEED_10GB_FULL:
3000                 link.link_speed = ETH_LINK_SPEED_10000;
3001                 break;
3002         }
3003         rte_ixgbe_dev_atomic_write_link_status(dev, &link);
3004
3005         if (link.link_status == old.link_status)
3006                 return -1;
3007
3008         return 0;
3009 }
3010
3011 static void
3012 ixgbe_dev_promiscuous_enable(struct rte_eth_dev *dev)
3013 {
3014         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
3015         uint32_t fctrl;
3016
3017         fctrl = IXGBE_READ_REG(hw, IXGBE_FCTRL);
3018         fctrl |= (IXGBE_FCTRL_UPE | IXGBE_FCTRL_MPE);
3019         IXGBE_WRITE_REG(hw, IXGBE_FCTRL, fctrl);
3020 }
3021
3022 static void
3023 ixgbe_dev_promiscuous_disable(struct rte_eth_dev *dev)
3024 {
3025         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
3026         uint32_t fctrl;
3027
3028         fctrl = IXGBE_READ_REG(hw, IXGBE_FCTRL);
3029         fctrl &= (~IXGBE_FCTRL_UPE);
3030         if (dev->data->all_multicast == 1)
3031                 fctrl |= IXGBE_FCTRL_MPE;
3032         else
3033                 fctrl &= (~IXGBE_FCTRL_MPE);
3034         IXGBE_WRITE_REG(hw, IXGBE_FCTRL, fctrl);
3035 }
3036
3037 static void
3038 ixgbe_dev_allmulticast_enable(struct rte_eth_dev *dev)
3039 {
3040         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
3041         uint32_t fctrl;
3042
3043         fctrl = IXGBE_READ_REG(hw, IXGBE_FCTRL);
3044         fctrl |= IXGBE_FCTRL_MPE;
3045         IXGBE_WRITE_REG(hw, IXGBE_FCTRL, fctrl);
3046 }
3047
3048 static void
3049 ixgbe_dev_allmulticast_disable(struct rte_eth_dev *dev)
3050 {
3051         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
3052         uint32_t fctrl;
3053
3054         if (dev->data->promiscuous == 1)
3055                 return; /* must remain in all_multicast mode */
3056
3057         fctrl = IXGBE_READ_REG(hw, IXGBE_FCTRL);
3058         fctrl &= (~IXGBE_FCTRL_MPE);
3059         IXGBE_WRITE_REG(hw, IXGBE_FCTRL, fctrl);
3060 }
3061
3062 /**
3063  * It clears the interrupt causes and enables the interrupt.
3064  * It will be called once only during nic initialized.
3065  *
3066  * @param dev
3067  *  Pointer to struct rte_eth_dev.
3068  *
3069  * @return
3070  *  - On success, zero.
3071  *  - On failure, a negative value.
3072  */
3073 static int
3074 ixgbe_dev_lsc_interrupt_setup(struct rte_eth_dev *dev)
3075 {
3076         struct ixgbe_interrupt *intr =
3077                 IXGBE_DEV_PRIVATE_TO_INTR(dev->data->dev_private);
3078
3079         ixgbe_dev_link_status_print(dev);
3080         intr->mask |= IXGBE_EICR_LSC;
3081
3082         return 0;
3083 }
3084
3085 /**
3086  * It clears the interrupt causes and enables the interrupt.
3087  * It will be called once only during nic initialized.
3088  *
3089  * @param dev
3090  *  Pointer to struct rte_eth_dev.
3091  *
3092  * @return
3093  *  - On success, zero.
3094  *  - On failure, a negative value.
3095  */
3096 static int
3097 ixgbe_dev_rxq_interrupt_setup(struct rte_eth_dev *dev)
3098 {
3099         struct ixgbe_interrupt *intr =
3100                 IXGBE_DEV_PRIVATE_TO_INTR(dev->data->dev_private);
3101
3102         intr->mask |= IXGBE_EICR_RTX_QUEUE;
3103
3104         return 0;
3105 }
3106
3107 /*
3108  * It reads ICR and sets flag (IXGBE_EICR_LSC) for the link_update.
3109  *
3110  * @param dev
3111  *  Pointer to struct rte_eth_dev.
3112  *
3113  * @return
3114  *  - On success, zero.
3115  *  - On failure, a negative value.
3116  */
3117 static int
3118 ixgbe_dev_interrupt_get_status(struct rte_eth_dev *dev)
3119 {
3120         uint32_t eicr;
3121         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
3122         struct ixgbe_interrupt *intr =
3123                 IXGBE_DEV_PRIVATE_TO_INTR(dev->data->dev_private);
3124
3125         /* clear all cause mask */
3126         ixgbe_disable_intr(hw);
3127
3128         /* read-on-clear nic registers here */
3129         eicr = IXGBE_READ_REG(hw, IXGBE_EICR);
3130         PMD_DRV_LOG(DEBUG, "eicr %x", eicr);
3131
3132         intr->flags = 0;
3133
3134         /* set flag for async link update */
3135         if (eicr & IXGBE_EICR_LSC)
3136                 intr->flags |= IXGBE_FLAG_NEED_LINK_UPDATE;
3137
3138         if (eicr & IXGBE_EICR_MAILBOX)
3139                 intr->flags |= IXGBE_FLAG_MAILBOX;
3140
3141         return 0;
3142 }
3143
3144 /**
3145  * It gets and then prints the link status.
3146  *
3147  * @param dev
3148  *  Pointer to struct rte_eth_dev.
3149  *
3150  * @return
3151  *  - On success, zero.
3152  *  - On failure, a negative value.
3153  */
3154 static void
3155 ixgbe_dev_link_status_print(struct rte_eth_dev *dev)
3156 {
3157         struct rte_eth_link link;
3158
3159         memset(&link, 0, sizeof(link));
3160         rte_ixgbe_dev_atomic_read_link_status(dev, &link);
3161         if (link.link_status) {
3162                 PMD_INIT_LOG(INFO, "Port %d: Link Up - speed %u Mbps - %s",
3163                                         (int)(dev->data->port_id),
3164                                         (unsigned)link.link_speed,
3165                         link.link_duplex == ETH_LINK_FULL_DUPLEX ?
3166                                         "full-duplex" : "half-duplex");
3167         } else {
3168                 PMD_INIT_LOG(INFO, " Port %d: Link Down",
3169                                 (int)(dev->data->port_id));
3170         }
3171         PMD_INIT_LOG(DEBUG, "PCI Address: %04d:%02d:%02d:%d",
3172                                 dev->pci_dev->addr.domain,
3173                                 dev->pci_dev->addr.bus,
3174                                 dev->pci_dev->addr.devid,
3175                                 dev->pci_dev->addr.function);
3176 }
3177
3178 /*
3179  * It executes link_update after knowing an interrupt occurred.
3180  *
3181  * @param dev
3182  *  Pointer to struct rte_eth_dev.
3183  *
3184  * @return
3185  *  - On success, zero.
3186  *  - On failure, a negative value.
3187  */
3188 static int
3189 ixgbe_dev_interrupt_action(struct rte_eth_dev *dev)
3190 {
3191         struct ixgbe_interrupt *intr =
3192                 IXGBE_DEV_PRIVATE_TO_INTR(dev->data->dev_private);
3193         int64_t timeout;
3194         struct rte_eth_link link;
3195         int intr_enable_delay = false;
3196
3197         PMD_DRV_LOG(DEBUG, "intr action type %d", intr->flags);
3198
3199         if (intr->flags & IXGBE_FLAG_MAILBOX) {
3200                 ixgbe_pf_mbx_process(dev);
3201                 intr->flags &= ~IXGBE_FLAG_MAILBOX;
3202         }
3203
3204         if (intr->flags & IXGBE_FLAG_NEED_LINK_UPDATE) {
3205                 /* get the link status before link update, for predicting later */
3206                 memset(&link, 0, sizeof(link));
3207                 rte_ixgbe_dev_atomic_read_link_status(dev, &link);
3208
3209                 ixgbe_dev_link_update(dev, 0);
3210
3211                 /* likely to up */
3212                 if (!link.link_status)
3213                         /* handle it 1 sec later, wait it being stable */
3214                         timeout = IXGBE_LINK_UP_CHECK_TIMEOUT;
3215                 /* likely to down */
3216                 else
3217                         /* handle it 4 sec later, wait it being stable */
3218                         timeout = IXGBE_LINK_DOWN_CHECK_TIMEOUT;
3219
3220                 ixgbe_dev_link_status_print(dev);
3221
3222                 intr_enable_delay = true;
3223         }
3224
3225         if (intr_enable_delay) {
3226                 if (rte_eal_alarm_set(timeout * 1000,
3227                                       ixgbe_dev_interrupt_delayed_handler, (void*)dev) < 0)
3228                         PMD_DRV_LOG(ERR, "Error setting alarm");
3229         } else {
3230                 PMD_DRV_LOG(DEBUG, "enable intr immediately");
3231                 ixgbe_enable_intr(dev);
3232                 rte_intr_enable(&(dev->pci_dev->intr_handle));
3233         }
3234
3235
3236         return 0;
3237 }
3238
3239 /**
3240  * Interrupt handler which shall be registered for alarm callback for delayed
3241  * handling specific interrupt to wait for the stable nic state. As the
3242  * NIC interrupt state is not stable for ixgbe after link is just down,
3243  * it needs to wait 4 seconds to get the stable status.
3244  *
3245  * @param handle
3246  *  Pointer to interrupt handle.
3247  * @param param
3248  *  The address of parameter (struct rte_eth_dev *) regsitered before.
3249  *
3250  * @return
3251  *  void
3252  */
3253 static void
3254 ixgbe_dev_interrupt_delayed_handler(void *param)
3255 {
3256         struct rte_eth_dev *dev = (struct rte_eth_dev *)param;
3257         struct ixgbe_interrupt *intr =
3258                 IXGBE_DEV_PRIVATE_TO_INTR(dev->data->dev_private);
3259         struct ixgbe_hw *hw =
3260                 IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
3261         uint32_t eicr;
3262
3263         eicr = IXGBE_READ_REG(hw, IXGBE_EICR);
3264         if (eicr & IXGBE_EICR_MAILBOX)
3265                 ixgbe_pf_mbx_process(dev);
3266
3267         if (intr->flags & IXGBE_FLAG_NEED_LINK_UPDATE) {
3268                 ixgbe_dev_link_update(dev, 0);
3269                 intr->flags &= ~IXGBE_FLAG_NEED_LINK_UPDATE;
3270                 ixgbe_dev_link_status_print(dev);
3271                 _rte_eth_dev_callback_process(dev, RTE_ETH_EVENT_INTR_LSC);
3272         }
3273
3274         PMD_DRV_LOG(DEBUG, "enable intr in delayed handler S[%08x]", eicr);
3275         ixgbe_enable_intr(dev);
3276         rte_intr_enable(&(dev->pci_dev->intr_handle));
3277 }
3278
3279 /**
3280  * Interrupt handler triggered by NIC  for handling
3281  * specific interrupt.
3282  *
3283  * @param handle
3284  *  Pointer to interrupt handle.
3285  * @param param
3286  *  The address of parameter (struct rte_eth_dev *) regsitered before.
3287  *
3288  * @return
3289  *  void
3290  */
3291 static void
3292 ixgbe_dev_interrupt_handler(__rte_unused struct rte_intr_handle *handle,
3293                             void *param)
3294 {
3295         struct rte_eth_dev *dev = (struct rte_eth_dev *)param;
3296
3297         ixgbe_dev_interrupt_get_status(dev);
3298         ixgbe_dev_interrupt_action(dev);
3299 }
3300
3301 static int
3302 ixgbe_dev_led_on(struct rte_eth_dev *dev)
3303 {
3304         struct ixgbe_hw *hw;
3305
3306         hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
3307         return ixgbe_led_on(hw, 0) == IXGBE_SUCCESS ? 0 : -ENOTSUP;
3308 }
3309
3310 static int
3311 ixgbe_dev_led_off(struct rte_eth_dev *dev)
3312 {
3313         struct ixgbe_hw *hw;
3314
3315         hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
3316         return ixgbe_led_off(hw, 0) == IXGBE_SUCCESS ? 0 : -ENOTSUP;
3317 }
3318
3319 static int
3320 ixgbe_flow_ctrl_get(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
3321 {
3322         struct ixgbe_hw *hw;
3323         uint32_t mflcn_reg;
3324         uint32_t fccfg_reg;
3325         int rx_pause;
3326         int tx_pause;
3327
3328         hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
3329
3330         fc_conf->pause_time = hw->fc.pause_time;
3331         fc_conf->high_water = hw->fc.high_water[0];
3332         fc_conf->low_water = hw->fc.low_water[0];
3333         fc_conf->send_xon = hw->fc.send_xon;
3334         fc_conf->autoneg = !hw->fc.disable_fc_autoneg;
3335
3336         /*
3337          * Return rx_pause status according to actual setting of
3338          * MFLCN register.
3339          */
3340         mflcn_reg = IXGBE_READ_REG(hw, IXGBE_MFLCN);
3341         if (mflcn_reg & (IXGBE_MFLCN_RPFCE | IXGBE_MFLCN_RFCE))
3342                 rx_pause = 1;
3343         else
3344                 rx_pause = 0;
3345
3346         /*
3347          * Return tx_pause status according to actual setting of
3348          * FCCFG register.
3349          */
3350         fccfg_reg = IXGBE_READ_REG(hw, IXGBE_FCCFG);
3351         if (fccfg_reg & (IXGBE_FCCFG_TFCE_802_3X | IXGBE_FCCFG_TFCE_PRIORITY))
3352                 tx_pause = 1;
3353         else
3354                 tx_pause = 0;
3355
3356         if (rx_pause && tx_pause)
3357                 fc_conf->mode = RTE_FC_FULL;
3358         else if (rx_pause)
3359                 fc_conf->mode = RTE_FC_RX_PAUSE;
3360         else if (tx_pause)
3361                 fc_conf->mode = RTE_FC_TX_PAUSE;
3362         else
3363                 fc_conf->mode = RTE_FC_NONE;
3364
3365         return 0;
3366 }
3367
3368 static int
3369 ixgbe_flow_ctrl_set(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
3370 {
3371         struct ixgbe_hw *hw;
3372         int err;
3373         uint32_t rx_buf_size;
3374         uint32_t max_high_water;
3375         uint32_t mflcn;
3376         enum ixgbe_fc_mode rte_fcmode_2_ixgbe_fcmode[] = {
3377                 ixgbe_fc_none,
3378                 ixgbe_fc_rx_pause,
3379                 ixgbe_fc_tx_pause,
3380                 ixgbe_fc_full
3381         };
3382
3383         PMD_INIT_FUNC_TRACE();
3384
3385         hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
3386         rx_buf_size = IXGBE_READ_REG(hw, IXGBE_RXPBSIZE(0));
3387         PMD_INIT_LOG(DEBUG, "Rx packet buffer size = 0x%x", rx_buf_size);
3388
3389         /*
3390          * At least reserve one Ethernet frame for watermark
3391          * high_water/low_water in kilo bytes for ixgbe
3392          */
3393         max_high_water = (rx_buf_size - ETHER_MAX_LEN) >> IXGBE_RXPBSIZE_SHIFT;
3394         if ((fc_conf->high_water > max_high_water) ||
3395                 (fc_conf->high_water < fc_conf->low_water)) {
3396                 PMD_INIT_LOG(ERR, "Invalid high/low water setup value in KB");
3397                 PMD_INIT_LOG(ERR, "High_water must <= 0x%x", max_high_water);
3398                 return -EINVAL;
3399         }
3400
3401         hw->fc.requested_mode = rte_fcmode_2_ixgbe_fcmode[fc_conf->mode];
3402         hw->fc.pause_time     = fc_conf->pause_time;
3403         hw->fc.high_water[0]  = fc_conf->high_water;
3404         hw->fc.low_water[0]   = fc_conf->low_water;
3405         hw->fc.send_xon       = fc_conf->send_xon;
3406         hw->fc.disable_fc_autoneg = !fc_conf->autoneg;
3407
3408         err = ixgbe_fc_enable(hw);
3409
3410         /* Not negotiated is not an error case */
3411         if ((err == IXGBE_SUCCESS) || (err == IXGBE_ERR_FC_NOT_NEGOTIATED)) {
3412
3413                 /* check if we want to forward MAC frames - driver doesn't have native
3414                  * capability to do that, so we'll write the registers ourselves */
3415
3416                 mflcn = IXGBE_READ_REG(hw, IXGBE_MFLCN);
3417
3418                 /* set or clear MFLCN.PMCF bit depending on configuration */
3419                 if (fc_conf->mac_ctrl_frame_fwd != 0)
3420                         mflcn |= IXGBE_MFLCN_PMCF;
3421                 else
3422                         mflcn &= ~IXGBE_MFLCN_PMCF;
3423
3424                 IXGBE_WRITE_REG(hw, IXGBE_MFLCN, mflcn);
3425                 IXGBE_WRITE_FLUSH(hw);
3426
3427                 return 0;
3428         }
3429
3430         PMD_INIT_LOG(ERR, "ixgbe_fc_enable = 0x%x", err);
3431         return -EIO;
3432 }
3433
3434 /**
3435  *  ixgbe_pfc_enable_generic - Enable flow control
3436  *  @hw: pointer to hardware structure
3437  *  @tc_num: traffic class number
3438  *  Enable flow control according to the current settings.
3439  */
3440 static int
3441 ixgbe_dcb_pfc_enable_generic(struct ixgbe_hw *hw,uint8_t tc_num)
3442 {
3443         int ret_val = 0;
3444         uint32_t mflcn_reg, fccfg_reg;
3445         uint32_t reg;
3446         uint32_t fcrtl, fcrth;
3447         uint8_t i;
3448         uint8_t nb_rx_en;
3449
3450         /* Validate the water mark configuration */
3451         if (!hw->fc.pause_time) {
3452                 ret_val = IXGBE_ERR_INVALID_LINK_SETTINGS;
3453                 goto out;
3454         }
3455
3456         /* Low water mark of zero causes XOFF floods */
3457         if (hw->fc.current_mode & ixgbe_fc_tx_pause) {
3458                  /* High/Low water can not be 0 */
3459                 if( (!hw->fc.high_water[tc_num])|| (!hw->fc.low_water[tc_num])) {
3460                         PMD_INIT_LOG(ERR, "Invalid water mark configuration");
3461                         ret_val = IXGBE_ERR_INVALID_LINK_SETTINGS;
3462                         goto out;
3463                 }
3464
3465                 if(hw->fc.low_water[tc_num] >= hw->fc.high_water[tc_num]) {
3466                         PMD_INIT_LOG(ERR, "Invalid water mark configuration");
3467                         ret_val = IXGBE_ERR_INVALID_LINK_SETTINGS;
3468                         goto out;
3469                 }
3470         }
3471         /* Negotiate the fc mode to use */
3472         ixgbe_fc_autoneg(hw);
3473
3474         /* Disable any previous flow control settings */
3475         mflcn_reg = IXGBE_READ_REG(hw, IXGBE_MFLCN);
3476         mflcn_reg &= ~(IXGBE_MFLCN_RPFCE_SHIFT | IXGBE_MFLCN_RFCE|IXGBE_MFLCN_RPFCE);
3477
3478         fccfg_reg = IXGBE_READ_REG(hw, IXGBE_FCCFG);
3479         fccfg_reg &= ~(IXGBE_FCCFG_TFCE_802_3X | IXGBE_FCCFG_TFCE_PRIORITY);
3480
3481         switch (hw->fc.current_mode) {
3482         case ixgbe_fc_none:
3483                 /*
3484                  * If the count of enabled RX Priority Flow control >1,
3485                  * and the TX pause can not be disabled
3486                  */
3487                 nb_rx_en = 0;
3488                 for (i =0; i < IXGBE_DCB_MAX_TRAFFIC_CLASS; i++) {
3489                         reg = IXGBE_READ_REG(hw, IXGBE_FCRTH_82599(i));
3490                         if (reg & IXGBE_FCRTH_FCEN)
3491                                 nb_rx_en++;
3492                 }
3493                 if (nb_rx_en > 1)
3494                         fccfg_reg |=IXGBE_FCCFG_TFCE_PRIORITY;
3495                 break;
3496         case ixgbe_fc_rx_pause:
3497                 /*
3498                  * Rx Flow control is enabled and Tx Flow control is
3499                  * disabled by software override. Since there really
3500                  * isn't a way to advertise that we are capable of RX
3501                  * Pause ONLY, we will advertise that we support both
3502                  * symmetric and asymmetric Rx PAUSE.  Later, we will
3503                  * disable the adapter's ability to send PAUSE frames.
3504                  */
3505                 mflcn_reg |= IXGBE_MFLCN_RPFCE;
3506                 /*
3507                  * If the count of enabled RX Priority Flow control >1,
3508                  * and the TX pause can not be disabled
3509                  */
3510                 nb_rx_en = 0;
3511                 for (i =0; i < IXGBE_DCB_MAX_TRAFFIC_CLASS; i++) {
3512                         reg = IXGBE_READ_REG(hw, IXGBE_FCRTH_82599(i));
3513                         if (reg & IXGBE_FCRTH_FCEN)
3514                                 nb_rx_en++;
3515                 }
3516                 if (nb_rx_en > 1)
3517                         fccfg_reg |=IXGBE_FCCFG_TFCE_PRIORITY;
3518                 break;
3519         case ixgbe_fc_tx_pause:
3520                 /*
3521                  * Tx Flow control is enabled, and Rx Flow control is
3522                  * disabled by software override.
3523                  */
3524                 fccfg_reg |=IXGBE_FCCFG_TFCE_PRIORITY;
3525                 break;
3526         case ixgbe_fc_full:
3527                 /* Flow control (both Rx and Tx) is enabled by SW override. */
3528                 mflcn_reg |= IXGBE_MFLCN_RPFCE;
3529                 fccfg_reg |= IXGBE_FCCFG_TFCE_PRIORITY;
3530                 break;
3531         default:
3532                 PMD_DRV_LOG(DEBUG, "Flow control param set incorrectly");
3533                 ret_val = IXGBE_ERR_CONFIG;
3534                 goto out;
3535                 break;
3536         }
3537
3538         /* Set 802.3x based flow control settings. */
3539         mflcn_reg |= IXGBE_MFLCN_DPF;
3540         IXGBE_WRITE_REG(hw, IXGBE_MFLCN, mflcn_reg);
3541         IXGBE_WRITE_REG(hw, IXGBE_FCCFG, fccfg_reg);
3542
3543         /* Set up and enable Rx high/low water mark thresholds, enable XON. */
3544         if ((hw->fc.current_mode & ixgbe_fc_tx_pause) &&
3545                 hw->fc.high_water[tc_num]) {
3546                 fcrtl = (hw->fc.low_water[tc_num] << 10) | IXGBE_FCRTL_XONE;
3547                 IXGBE_WRITE_REG(hw, IXGBE_FCRTL_82599(tc_num), fcrtl);
3548                 fcrth = (hw->fc.high_water[tc_num] << 10) | IXGBE_FCRTH_FCEN;
3549         } else {
3550                 IXGBE_WRITE_REG(hw, IXGBE_FCRTL_82599(tc_num), 0);
3551                 /*
3552                  * In order to prevent Tx hangs when the internal Tx
3553                  * switch is enabled we must set the high water mark
3554                  * to the maximum FCRTH value.  This allows the Tx
3555                  * switch to function even under heavy Rx workloads.
3556                  */
3557                 fcrth = IXGBE_READ_REG(hw, IXGBE_RXPBSIZE(tc_num)) - 32;
3558         }
3559         IXGBE_WRITE_REG(hw, IXGBE_FCRTH_82599(tc_num), fcrth);
3560
3561         /* Configure pause time (2 TCs per register) */
3562         reg = hw->fc.pause_time * 0x00010001;
3563         for (i = 0; i < (IXGBE_DCB_MAX_TRAFFIC_CLASS / 2); i++)
3564                 IXGBE_WRITE_REG(hw, IXGBE_FCTTV(i), reg);
3565
3566         /* Configure flow control refresh threshold value */
3567         IXGBE_WRITE_REG(hw, IXGBE_FCRTV, hw->fc.pause_time / 2);
3568
3569 out:
3570         return ret_val;
3571 }
3572
3573 static int
3574 ixgbe_dcb_pfc_enable(struct rte_eth_dev *dev,uint8_t tc_num)
3575 {
3576         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
3577         int32_t ret_val = IXGBE_NOT_IMPLEMENTED;
3578
3579         if(hw->mac.type != ixgbe_mac_82598EB) {
3580                 ret_val = ixgbe_dcb_pfc_enable_generic(hw,tc_num);
3581         }
3582         return ret_val;
3583 }
3584
3585 static int
3586 ixgbe_priority_flow_ctrl_set(struct rte_eth_dev *dev, struct rte_eth_pfc_conf *pfc_conf)
3587 {
3588         int err;
3589         uint32_t rx_buf_size;
3590         uint32_t max_high_water;
3591         uint8_t tc_num;
3592         uint8_t  map[IXGBE_DCB_MAX_USER_PRIORITY] = { 0 };
3593         struct ixgbe_hw *hw =
3594                 IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
3595         struct ixgbe_dcb_config *dcb_config =
3596                 IXGBE_DEV_PRIVATE_TO_DCB_CFG(dev->data->dev_private);
3597
3598         enum ixgbe_fc_mode rte_fcmode_2_ixgbe_fcmode[] = {
3599                 ixgbe_fc_none,
3600                 ixgbe_fc_rx_pause,
3601                 ixgbe_fc_tx_pause,
3602                 ixgbe_fc_full
3603         };
3604
3605         PMD_INIT_FUNC_TRACE();
3606
3607         ixgbe_dcb_unpack_map_cee(dcb_config, IXGBE_DCB_RX_CONFIG, map);
3608         tc_num = map[pfc_conf->priority];
3609         rx_buf_size = IXGBE_READ_REG(hw, IXGBE_RXPBSIZE(tc_num));
3610         PMD_INIT_LOG(DEBUG, "Rx packet buffer size = 0x%x", rx_buf_size);
3611         /*
3612          * At least reserve one Ethernet frame for watermark
3613          * high_water/low_water in kilo bytes for ixgbe
3614          */
3615         max_high_water = (rx_buf_size - ETHER_MAX_LEN) >> IXGBE_RXPBSIZE_SHIFT;
3616         if ((pfc_conf->fc.high_water > max_high_water) ||
3617             (pfc_conf->fc.high_water <= pfc_conf->fc.low_water)) {
3618                 PMD_INIT_LOG(ERR, "Invalid high/low water setup value in KB");
3619                 PMD_INIT_LOG(ERR, "High_water must <= 0x%x", max_high_water);
3620                 return -EINVAL;
3621         }
3622
3623         hw->fc.requested_mode = rte_fcmode_2_ixgbe_fcmode[pfc_conf->fc.mode];
3624         hw->fc.pause_time = pfc_conf->fc.pause_time;
3625         hw->fc.send_xon = pfc_conf->fc.send_xon;
3626         hw->fc.low_water[tc_num] =  pfc_conf->fc.low_water;
3627         hw->fc.high_water[tc_num] = pfc_conf->fc.high_water;
3628
3629         err = ixgbe_dcb_pfc_enable(dev,tc_num);
3630
3631         /* Not negotiated is not an error case */
3632         if ((err == IXGBE_SUCCESS) || (err == IXGBE_ERR_FC_NOT_NEGOTIATED))
3633                 return 0;
3634
3635         PMD_INIT_LOG(ERR, "ixgbe_dcb_pfc_enable = 0x%x", err);
3636         return -EIO;
3637 }
3638
3639 static int
3640 ixgbe_dev_rss_reta_update(struct rte_eth_dev *dev,
3641                           struct rte_eth_rss_reta_entry64 *reta_conf,
3642                           uint16_t reta_size)
3643 {
3644         uint8_t i, j, mask;
3645         uint32_t reta, r;
3646         uint16_t idx, shift;
3647         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
3648         uint16_t sp_reta_size;
3649         uint32_t reta_reg;
3650
3651         PMD_INIT_FUNC_TRACE();
3652
3653         if (!ixgbe_rss_update_sp(hw->mac.type)) {
3654                 PMD_DRV_LOG(ERR, "RSS reta update is not supported on this "
3655                         "NIC.");
3656                 return -ENOTSUP;
3657         }
3658
3659         sp_reta_size = ixgbe_reta_size_get(hw->mac.type);
3660         if (reta_size != sp_reta_size) {
3661                 PMD_DRV_LOG(ERR, "The size of hash lookup table configured "
3662                         "(%d) doesn't match the number hardware can supported "
3663                         "(%d)\n", reta_size, sp_reta_size);
3664                 return -EINVAL;
3665         }
3666
3667         for (i = 0; i < reta_size; i += IXGBE_4_BIT_WIDTH) {
3668                 idx = i / RTE_RETA_GROUP_SIZE;
3669                 shift = i % RTE_RETA_GROUP_SIZE;
3670                 mask = (uint8_t)((reta_conf[idx].mask >> shift) &
3671                                                 IXGBE_4_BIT_MASK);
3672                 if (!mask)
3673                         continue;
3674                 reta_reg = ixgbe_reta_reg_get(hw->mac.type, i);
3675                 if (mask == IXGBE_4_BIT_MASK)
3676                         r = 0;
3677                 else
3678                         r = IXGBE_READ_REG(hw, reta_reg);
3679                 for (j = 0, reta = 0; j < IXGBE_4_BIT_WIDTH; j++) {
3680                         if (mask & (0x1 << j))
3681                                 reta |= reta_conf[idx].reta[shift + j] <<
3682                                                         (CHAR_BIT * j);
3683                         else
3684                                 reta |= r & (IXGBE_8_BIT_MASK <<
3685                                                 (CHAR_BIT * j));
3686                 }
3687                 IXGBE_WRITE_REG(hw, reta_reg, reta);
3688         }
3689
3690         return 0;
3691 }
3692
3693 static int
3694 ixgbe_dev_rss_reta_query(struct rte_eth_dev *dev,
3695                          struct rte_eth_rss_reta_entry64 *reta_conf,
3696                          uint16_t reta_size)
3697 {
3698         uint8_t i, j, mask;
3699         uint32_t reta;
3700         uint16_t idx, shift;
3701         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
3702         uint16_t sp_reta_size;
3703         uint32_t reta_reg;
3704
3705         PMD_INIT_FUNC_TRACE();
3706         sp_reta_size = ixgbe_reta_size_get(hw->mac.type);
3707         if (reta_size != sp_reta_size) {
3708                 PMD_DRV_LOG(ERR, "The size of hash lookup table configured "
3709                         "(%d) doesn't match the number hardware can supported "
3710                         "(%d)\n", reta_size, sp_reta_size);
3711                 return -EINVAL;
3712         }
3713
3714         for (i = 0; i < reta_size; i += IXGBE_4_BIT_WIDTH) {
3715                 idx = i / RTE_RETA_GROUP_SIZE;
3716                 shift = i % RTE_RETA_GROUP_SIZE;
3717                 mask = (uint8_t)((reta_conf[idx].mask >> shift) &
3718                                                 IXGBE_4_BIT_MASK);
3719                 if (!mask)
3720                         continue;
3721
3722                 reta_reg = ixgbe_reta_reg_get(hw->mac.type, i);
3723                 reta = IXGBE_READ_REG(hw, reta_reg);
3724                 for (j = 0; j < IXGBE_4_BIT_WIDTH; j++) {
3725                         if (mask & (0x1 << j))
3726                                 reta_conf[idx].reta[shift + j] =
3727                                         ((reta >> (CHAR_BIT * j)) &
3728                                                 IXGBE_8_BIT_MASK);
3729                 }
3730         }
3731
3732         return 0;
3733 }
3734
3735 static void
3736 ixgbe_add_rar(struct rte_eth_dev *dev, struct ether_addr *mac_addr,
3737                                 uint32_t index, uint32_t pool)
3738 {
3739         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
3740         uint32_t enable_addr = 1;
3741
3742         ixgbe_set_rar(hw, index, mac_addr->addr_bytes, pool, enable_addr);
3743 }
3744
3745 static void
3746 ixgbe_remove_rar(struct rte_eth_dev *dev, uint32_t index)
3747 {
3748         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
3749
3750         ixgbe_clear_rar(hw, index);
3751 }
3752
3753 static void
3754 ixgbe_set_default_mac_addr(struct rte_eth_dev *dev, struct ether_addr *addr)
3755 {
3756         ixgbe_remove_rar(dev, 0);
3757
3758         ixgbe_add_rar(dev, addr, 0, 0);
3759 }
3760
3761 static int
3762 ixgbe_dev_mtu_set(struct rte_eth_dev *dev, uint16_t mtu)
3763 {
3764         uint32_t hlreg0;
3765         uint32_t maxfrs;
3766         struct ixgbe_hw *hw;
3767         struct rte_eth_dev_info dev_info;
3768         uint32_t frame_size = mtu + ETHER_HDR_LEN + ETHER_CRC_LEN;
3769
3770         ixgbe_dev_info_get(dev, &dev_info);
3771
3772         /* check that mtu is within the allowed range */
3773         if ((mtu < ETHER_MIN_MTU) || (frame_size > dev_info.max_rx_pktlen))
3774                 return -EINVAL;
3775
3776         /* refuse mtu that requires the support of scattered packets when this
3777          * feature has not been enabled before. */
3778         if (!dev->data->scattered_rx &&
3779             (frame_size + 2 * IXGBE_VLAN_TAG_SIZE >
3780              dev->data->min_rx_buf_size - RTE_PKTMBUF_HEADROOM))
3781                 return -EINVAL;
3782
3783         hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
3784         hlreg0 = IXGBE_READ_REG(hw, IXGBE_HLREG0);
3785
3786         /* switch to jumbo mode if needed */
3787         if (frame_size > ETHER_MAX_LEN) {
3788                 dev->data->dev_conf.rxmode.jumbo_frame = 1;
3789                 hlreg0 |= IXGBE_HLREG0_JUMBOEN;
3790         } else {
3791                 dev->data->dev_conf.rxmode.jumbo_frame = 0;
3792                 hlreg0 &= ~IXGBE_HLREG0_JUMBOEN;
3793         }
3794         IXGBE_WRITE_REG(hw, IXGBE_HLREG0, hlreg0);
3795
3796         /* update max frame size */
3797         dev->data->dev_conf.rxmode.max_rx_pkt_len = frame_size;
3798
3799         maxfrs = IXGBE_READ_REG(hw, IXGBE_MAXFRS);
3800         maxfrs &= 0x0000FFFF;
3801         maxfrs |= (dev->data->dev_conf.rxmode.max_rx_pkt_len << 16);
3802         IXGBE_WRITE_REG(hw, IXGBE_MAXFRS, maxfrs);
3803
3804         return 0;
3805 }
3806
3807 /*
3808  * Virtual Function operations
3809  */
3810 static void
3811 ixgbevf_intr_disable(struct ixgbe_hw *hw)
3812 {
3813         PMD_INIT_FUNC_TRACE();
3814
3815         /* Clear interrupt mask to stop from interrupts being generated */
3816         IXGBE_WRITE_REG(hw, IXGBE_VTEIMC, IXGBE_VF_IRQ_CLEAR_MASK);
3817
3818         IXGBE_WRITE_FLUSH(hw);
3819 }
3820
3821 static void
3822 ixgbevf_intr_enable(struct ixgbe_hw *hw)
3823 {
3824         PMD_INIT_FUNC_TRACE();
3825
3826         /* VF enable interrupt autoclean */
3827         IXGBE_WRITE_REG(hw, IXGBE_VTEIAM, IXGBE_VF_IRQ_ENABLE_MASK);
3828         IXGBE_WRITE_REG(hw, IXGBE_VTEIAC, IXGBE_VF_IRQ_ENABLE_MASK);
3829         IXGBE_WRITE_REG(hw, IXGBE_VTEIMS, IXGBE_VF_IRQ_ENABLE_MASK);
3830
3831         IXGBE_WRITE_FLUSH(hw);
3832 }
3833
3834 static int
3835 ixgbevf_dev_configure(struct rte_eth_dev *dev)
3836 {
3837         struct rte_eth_conf* conf = &dev->data->dev_conf;
3838         struct ixgbe_adapter *adapter =
3839                         (struct ixgbe_adapter *)dev->data->dev_private;
3840
3841         PMD_INIT_LOG(DEBUG, "Configured Virtual Function port id: %d",
3842                      dev->data->port_id);
3843
3844         /*
3845          * VF has no ability to enable/disable HW CRC
3846          * Keep the persistent behavior the same as Host PF
3847          */
3848 #ifndef RTE_LIBRTE_IXGBE_PF_DISABLE_STRIP_CRC
3849         if (!conf->rxmode.hw_strip_crc) {
3850                 PMD_INIT_LOG(NOTICE, "VF can't disable HW CRC Strip");
3851                 conf->rxmode.hw_strip_crc = 1;
3852         }
3853 #else
3854         if (conf->rxmode.hw_strip_crc) {
3855                 PMD_INIT_LOG(NOTICE, "VF can't enable HW CRC Strip");
3856                 conf->rxmode.hw_strip_crc = 0;
3857         }
3858 #endif
3859
3860         /*
3861          * Initialize to TRUE. If any of Rx queues doesn't meet the bulk
3862          * allocation or vector Rx preconditions we will reset it.
3863          */
3864         adapter->rx_bulk_alloc_allowed = true;
3865         adapter->rx_vec_allowed = true;
3866
3867         return 0;
3868 }
3869
3870 static int
3871 ixgbevf_dev_start(struct rte_eth_dev *dev)
3872 {
3873         struct ixgbe_hw *hw =
3874                 IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
3875         uint32_t intr_vector = 0;
3876         struct rte_intr_handle *intr_handle = &dev->pci_dev->intr_handle;
3877
3878         int err, mask = 0;
3879
3880         PMD_INIT_FUNC_TRACE();
3881
3882         hw->mac.ops.reset_hw(hw);
3883         hw->mac.get_link_status = true;
3884
3885         /* negotiate mailbox API version to use with the PF. */
3886         ixgbevf_negotiate_api(hw);
3887
3888         ixgbevf_dev_tx_init(dev);
3889
3890         /* This can fail when allocating mbufs for descriptor rings */
3891         err = ixgbevf_dev_rx_init(dev);
3892         if (err) {
3893                 PMD_INIT_LOG(ERR, "Unable to initialize RX hardware (%d)", err);
3894                 ixgbe_dev_clear_queues(dev);
3895                 return err;
3896         }
3897
3898         /* Set vfta */
3899         ixgbevf_set_vfta_all(dev,1);
3900
3901         /* Set HW strip */
3902         mask = ETH_VLAN_STRIP_MASK | ETH_VLAN_FILTER_MASK | \
3903                 ETH_VLAN_EXTEND_MASK;
3904         ixgbevf_vlan_offload_set(dev, mask);
3905
3906         ixgbevf_dev_rxtx_start(dev);
3907
3908         /* check and configure queue intr-vector mapping */
3909         if (dev->data->dev_conf.intr_conf.rxq != 0) {
3910                 intr_vector = dev->data->nb_rx_queues;
3911                 if (rte_intr_efd_enable(intr_handle, intr_vector))
3912                         return -1;
3913         }
3914
3915         if (rte_intr_dp_is_en(intr_handle) && !intr_handle->intr_vec) {
3916                 intr_handle->intr_vec =
3917                         rte_zmalloc("intr_vec",
3918                                     dev->data->nb_rx_queues * sizeof(int), 0);
3919                 if (intr_handle->intr_vec == NULL) {
3920                         PMD_INIT_LOG(ERR, "Failed to allocate %d rx_queues"
3921                                      " intr_vec\n", dev->data->nb_rx_queues);
3922                         return -ENOMEM;
3923                 }
3924         }
3925         ixgbevf_configure_msix(dev);
3926
3927         rte_intr_enable(intr_handle);
3928
3929         /* Re-enable interrupt for VF */
3930         ixgbevf_intr_enable(hw);
3931
3932         return 0;
3933 }
3934
3935 static void
3936 ixgbevf_dev_stop(struct rte_eth_dev *dev)
3937 {
3938         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
3939         struct rte_intr_handle *intr_handle = &dev->pci_dev->intr_handle;
3940
3941         PMD_INIT_FUNC_TRACE();
3942
3943         hw->adapter_stopped = 1;
3944         ixgbe_stop_adapter(hw);
3945
3946         /*
3947           * Clear what we set, but we still keep shadow_vfta to
3948           * restore after device starts
3949           */
3950         ixgbevf_set_vfta_all(dev,0);
3951
3952         /* Clear stored conf */
3953         dev->data->scattered_rx = 0;
3954
3955         ixgbe_dev_clear_queues(dev);
3956
3957         /* disable intr eventfd mapping */
3958         rte_intr_disable(intr_handle);
3959
3960         /* Clean datapath event and queue/vec mapping */
3961         rte_intr_efd_disable(intr_handle);
3962         if (intr_handle->intr_vec != NULL) {
3963                 rte_free(intr_handle->intr_vec);
3964                 intr_handle->intr_vec = NULL;
3965         }
3966 }
3967
3968 static void
3969 ixgbevf_dev_close(struct rte_eth_dev *dev)
3970 {
3971         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
3972
3973         PMD_INIT_FUNC_TRACE();
3974
3975         ixgbe_reset_hw(hw);
3976
3977         ixgbevf_dev_stop(dev);
3978
3979         ixgbe_dev_free_queues(dev);
3980
3981         /* reprogram the RAR[0] in case user changed it. */
3982         ixgbe_set_rar(hw, 0, hw->mac.addr, 0, IXGBE_RAH_AV);
3983 }
3984
3985 static void ixgbevf_set_vfta_all(struct rte_eth_dev *dev, bool on)
3986 {
3987         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
3988         struct ixgbe_vfta * shadow_vfta =
3989                 IXGBE_DEV_PRIVATE_TO_VFTA(dev->data->dev_private);
3990         int i = 0, j = 0, vfta = 0, mask = 1;
3991
3992         for (i = 0; i < IXGBE_VFTA_SIZE; i++){
3993                 vfta = shadow_vfta->vfta[i];
3994                 if(vfta){
3995                         mask = 1;
3996                         for (j = 0; j < 32; j++){
3997                                 if(vfta & mask)
3998                                         ixgbe_set_vfta(hw, (i<<5)+j, 0, on);
3999                                 mask<<=1;
4000                         }
4001                 }
4002         }
4003
4004 }
4005
4006 static int
4007 ixgbevf_vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
4008 {
4009         struct ixgbe_hw *hw =
4010                 IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
4011         struct ixgbe_vfta * shadow_vfta =
4012                 IXGBE_DEV_PRIVATE_TO_VFTA(dev->data->dev_private);
4013         uint32_t vid_idx = 0;
4014         uint32_t vid_bit = 0;
4015         int ret = 0;
4016
4017         PMD_INIT_FUNC_TRACE();
4018
4019         /* vind is not used in VF driver, set to 0, check ixgbe_set_vfta_vf */
4020         ret = ixgbe_set_vfta(hw, vlan_id, 0, !!on);
4021         if(ret){
4022                 PMD_INIT_LOG(ERR, "Unable to set VF vlan");
4023                 return ret;
4024         }
4025         vid_idx = (uint32_t) ((vlan_id >> 5) & 0x7F);
4026         vid_bit = (uint32_t) (1 << (vlan_id & 0x1F));
4027
4028         /* Save what we set and retore it after device reset */
4029         if (on)
4030                 shadow_vfta->vfta[vid_idx] |= vid_bit;
4031         else
4032                 shadow_vfta->vfta[vid_idx] &= ~vid_bit;
4033
4034         return 0;
4035 }
4036
4037 static void
4038 ixgbevf_vlan_strip_queue_set(struct rte_eth_dev *dev, uint16_t queue, int on)
4039 {
4040         struct ixgbe_hw *hw =
4041                 IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
4042         uint32_t ctrl;
4043
4044         PMD_INIT_FUNC_TRACE();
4045
4046         if(queue >= hw->mac.max_rx_queues)
4047                 return;
4048
4049         ctrl = IXGBE_READ_REG(hw, IXGBE_RXDCTL(queue));
4050         if(on)
4051                 ctrl |= IXGBE_RXDCTL_VME;
4052         else
4053                 ctrl &= ~IXGBE_RXDCTL_VME;
4054         IXGBE_WRITE_REG(hw, IXGBE_RXDCTL(queue), ctrl);
4055
4056         ixgbe_vlan_hw_strip_bitmap_set( dev, queue, on);
4057 }
4058
4059 static void
4060 ixgbevf_vlan_offload_set(struct rte_eth_dev *dev, int mask)
4061 {
4062         struct ixgbe_hw *hw =
4063                 IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
4064         uint16_t i;
4065         int on = 0;
4066
4067         /* VF function only support hw strip feature, others are not support */
4068         if(mask & ETH_VLAN_STRIP_MASK){
4069                 on = !!(dev->data->dev_conf.rxmode.hw_vlan_strip);
4070
4071                 for(i=0; i < hw->mac.max_rx_queues; i++)
4072                         ixgbevf_vlan_strip_queue_set(dev,i,on);
4073         }
4074 }
4075
4076 static int
4077 ixgbe_vmdq_mode_check(struct ixgbe_hw *hw)
4078 {
4079         uint32_t reg_val;
4080
4081         /* we only need to do this if VMDq is enabled */
4082         reg_val = IXGBE_READ_REG(hw, IXGBE_VT_CTL);
4083         if (!(reg_val & IXGBE_VT_CTL_VT_ENABLE)) {
4084                 PMD_INIT_LOG(ERR, "VMDq must be enabled for this setting");
4085                 return -1;
4086         }
4087
4088         return 0;
4089 }
4090
4091 static uint32_t
4092 ixgbe_uta_vector(struct ixgbe_hw *hw, struct ether_addr* uc_addr)
4093 {
4094         uint32_t vector = 0;
4095         switch (hw->mac.mc_filter_type) {
4096         case 0:   /* use bits [47:36] of the address */
4097                 vector = ((uc_addr->addr_bytes[4] >> 4) |
4098                         (((uint16_t)uc_addr->addr_bytes[5]) << 4));
4099                 break;
4100         case 1:   /* use bits [46:35] of the address */
4101                 vector = ((uc_addr->addr_bytes[4] >> 3) |
4102                         (((uint16_t)uc_addr->addr_bytes[5]) << 5));
4103                 break;
4104         case 2:   /* use bits [45:34] of the address */
4105                 vector = ((uc_addr->addr_bytes[4] >> 2) |
4106                         (((uint16_t)uc_addr->addr_bytes[5]) << 6));
4107                 break;
4108         case 3:   /* use bits [43:32] of the address */
4109                 vector = ((uc_addr->addr_bytes[4]) |
4110                         (((uint16_t)uc_addr->addr_bytes[5]) << 8));
4111                 break;
4112         default:  /* Invalid mc_filter_type */
4113                 break;
4114         }
4115
4116         /* vector can only be 12-bits or boundary will be exceeded */
4117         vector &= 0xFFF;
4118         return vector;
4119 }
4120
4121 static int
4122 ixgbe_uc_hash_table_set(struct rte_eth_dev *dev,struct ether_addr* mac_addr,
4123                                uint8_t on)
4124 {
4125         uint32_t vector;
4126         uint32_t uta_idx;
4127         uint32_t reg_val;
4128         uint32_t uta_shift;
4129         uint32_t rc;
4130         const uint32_t ixgbe_uta_idx_mask = 0x7F;
4131         const uint32_t ixgbe_uta_bit_shift = 5;
4132         const uint32_t ixgbe_uta_bit_mask = (0x1 << ixgbe_uta_bit_shift) - 1;
4133         const uint32_t bit1 = 0x1;
4134
4135         struct ixgbe_hw *hw =
4136                 IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
4137         struct ixgbe_uta_info *uta_info =
4138                 IXGBE_DEV_PRIVATE_TO_UTA(dev->data->dev_private);
4139
4140         /* The UTA table only exists on 82599 hardware and newer */
4141         if (hw->mac.type < ixgbe_mac_82599EB)
4142                 return -ENOTSUP;
4143
4144         vector = ixgbe_uta_vector(hw,mac_addr);
4145         uta_idx = (vector >> ixgbe_uta_bit_shift) & ixgbe_uta_idx_mask;
4146         uta_shift = vector & ixgbe_uta_bit_mask;
4147
4148         rc = ((uta_info->uta_shadow[uta_idx] >> uta_shift & bit1) != 0);
4149         if(rc == on)
4150                 return 0;
4151
4152         reg_val = IXGBE_READ_REG(hw, IXGBE_UTA(uta_idx));
4153         if (on) {
4154                 uta_info->uta_in_use++;
4155                 reg_val |= (bit1 << uta_shift);
4156                 uta_info->uta_shadow[uta_idx] |= (bit1 << uta_shift);
4157         } else {
4158                 uta_info->uta_in_use--;
4159                 reg_val &= ~(bit1 << uta_shift);
4160                 uta_info->uta_shadow[uta_idx] &= ~(bit1 << uta_shift);
4161         }
4162
4163         IXGBE_WRITE_REG(hw, IXGBE_UTA(uta_idx), reg_val);
4164
4165         if (uta_info->uta_in_use > 0)
4166                 IXGBE_WRITE_REG(hw, IXGBE_MCSTCTRL,
4167                                 IXGBE_MCSTCTRL_MFE | hw->mac.mc_filter_type);
4168         else
4169                 IXGBE_WRITE_REG(hw, IXGBE_MCSTCTRL,hw->mac.mc_filter_type);
4170
4171         return 0;
4172 }
4173
4174 static int
4175 ixgbe_uc_all_hash_table_set(struct rte_eth_dev *dev, uint8_t on)
4176 {
4177         int i;
4178         struct ixgbe_hw *hw =
4179                 IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
4180         struct ixgbe_uta_info *uta_info =
4181                 IXGBE_DEV_PRIVATE_TO_UTA(dev->data->dev_private);
4182
4183         /* The UTA table only exists on 82599 hardware and newer */
4184         if (hw->mac.type < ixgbe_mac_82599EB)
4185                 return -ENOTSUP;
4186
4187         if(on) {
4188                 for (i = 0; i < ETH_VMDQ_NUM_UC_HASH_ARRAY; i++) {
4189                         uta_info->uta_shadow[i] = ~0;
4190                         IXGBE_WRITE_REG(hw, IXGBE_UTA(i), ~0);
4191                 }
4192         } else {
4193                 for (i = 0; i < ETH_VMDQ_NUM_UC_HASH_ARRAY; i++) {
4194                         uta_info->uta_shadow[i] = 0;
4195                         IXGBE_WRITE_REG(hw, IXGBE_UTA(i), 0);
4196                 }
4197         }
4198         return 0;
4199
4200 }
4201
4202 uint32_t
4203 ixgbe_convert_vm_rx_mask_to_val(uint16_t rx_mask, uint32_t orig_val)
4204 {
4205         uint32_t new_val = orig_val;
4206
4207         if (rx_mask & ETH_VMDQ_ACCEPT_UNTAG)
4208                 new_val |= IXGBE_VMOLR_AUPE;
4209         if (rx_mask & ETH_VMDQ_ACCEPT_HASH_MC)
4210                 new_val |= IXGBE_VMOLR_ROMPE;
4211         if (rx_mask & ETH_VMDQ_ACCEPT_HASH_UC)
4212                 new_val |= IXGBE_VMOLR_ROPE;
4213         if (rx_mask & ETH_VMDQ_ACCEPT_BROADCAST)
4214                 new_val |= IXGBE_VMOLR_BAM;
4215         if (rx_mask & ETH_VMDQ_ACCEPT_MULTICAST)
4216                 new_val |= IXGBE_VMOLR_MPE;
4217
4218         return new_val;
4219 }
4220
4221 static int
4222 ixgbe_set_pool_rx_mode(struct rte_eth_dev *dev, uint16_t pool,
4223                                uint16_t rx_mask, uint8_t on)
4224 {
4225         int val = 0;
4226
4227         struct ixgbe_hw *hw =
4228                 IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
4229         uint32_t vmolr = IXGBE_READ_REG(hw, IXGBE_VMOLR(pool));
4230
4231         if (hw->mac.type == ixgbe_mac_82598EB) {
4232                 PMD_INIT_LOG(ERR, "setting VF receive mode set should be done"
4233                              " on 82599 hardware and newer");
4234                 return -ENOTSUP;
4235         }
4236         if (ixgbe_vmdq_mode_check(hw) < 0)
4237                 return -ENOTSUP;
4238
4239         val = ixgbe_convert_vm_rx_mask_to_val(rx_mask, val);
4240
4241         if (on)
4242                 vmolr |= val;
4243         else
4244                 vmolr &= ~val;
4245
4246         IXGBE_WRITE_REG(hw, IXGBE_VMOLR(pool), vmolr);
4247
4248         return 0;
4249 }
4250
4251 static int
4252 ixgbe_set_pool_rx(struct rte_eth_dev *dev, uint16_t pool, uint8_t on)
4253 {
4254         uint32_t reg,addr;
4255         uint32_t val;
4256         const uint8_t bit1 = 0x1;
4257
4258         struct ixgbe_hw *hw =
4259                 IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
4260
4261         if (ixgbe_vmdq_mode_check(hw) < 0)
4262                 return -ENOTSUP;
4263
4264         addr = IXGBE_VFRE(pool >= ETH_64_POOLS/2);
4265         reg = IXGBE_READ_REG(hw, addr);
4266         val = bit1 << pool;
4267
4268         if (on)
4269                 reg |= val;
4270         else
4271                 reg &= ~val;
4272
4273         IXGBE_WRITE_REG(hw, addr,reg);
4274
4275         return 0;
4276 }
4277
4278 static int
4279 ixgbe_set_pool_tx(struct rte_eth_dev *dev, uint16_t pool, uint8_t on)
4280 {
4281         uint32_t reg,addr;
4282         uint32_t val;
4283         const uint8_t bit1 = 0x1;
4284
4285         struct ixgbe_hw *hw =
4286                 IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
4287
4288         if (ixgbe_vmdq_mode_check(hw) < 0)
4289                 return -ENOTSUP;
4290
4291         addr = IXGBE_VFTE(pool >= ETH_64_POOLS/2);
4292         reg = IXGBE_READ_REG(hw, addr);
4293         val = bit1 << pool;
4294
4295         if (on)
4296                 reg |= val;
4297         else
4298                 reg &= ~val;
4299
4300         IXGBE_WRITE_REG(hw, addr,reg);
4301
4302         return 0;
4303 }
4304
4305 static int
4306 ixgbe_set_pool_vlan_filter(struct rte_eth_dev *dev, uint16_t vlan,
4307                         uint64_t pool_mask, uint8_t vlan_on)
4308 {
4309         int ret = 0;
4310         uint16_t pool_idx;
4311         struct ixgbe_hw *hw =
4312                 IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
4313
4314         if (ixgbe_vmdq_mode_check(hw) < 0)
4315                 return -ENOTSUP;
4316         for (pool_idx = 0; pool_idx < ETH_64_POOLS; pool_idx++) {
4317                 if (pool_mask & ((uint64_t)(1ULL << pool_idx)))
4318                         ret = hw->mac.ops.set_vfta(hw,vlan,pool_idx,vlan_on);
4319                         if (ret < 0)
4320                                 return ret;
4321         }
4322
4323         return ret;
4324 }
4325
4326 #define IXGBE_MRCTL_VPME  0x01 /* Virtual Pool Mirroring. */
4327 #define IXGBE_MRCTL_UPME  0x02 /* Uplink Port Mirroring. */
4328 #define IXGBE_MRCTL_DPME  0x04 /* Downlink Port Mirroring. */
4329 #define IXGBE_MRCTL_VLME  0x08 /* VLAN Mirroring. */
4330 #define IXGBE_INVALID_MIRROR_TYPE(mirror_type) \
4331         ((mirror_type) & ~(uint8_t)(ETH_MIRROR_VIRTUAL_POOL_UP | \
4332         ETH_MIRROR_UPLINK_PORT | ETH_MIRROR_DOWNLINK_PORT | ETH_MIRROR_VLAN))
4333
4334 static int
4335 ixgbe_mirror_rule_set(struct rte_eth_dev *dev,
4336                         struct rte_eth_mirror_conf *mirror_conf,
4337                         uint8_t rule_id, uint8_t on)
4338 {
4339         uint32_t mr_ctl,vlvf;
4340         uint32_t mp_lsb = 0;
4341         uint32_t mv_msb = 0;
4342         uint32_t mv_lsb = 0;
4343         uint32_t mp_msb = 0;
4344         uint8_t i = 0;
4345         int reg_index = 0;
4346         uint64_t vlan_mask = 0;
4347
4348         const uint8_t pool_mask_offset = 32;
4349         const uint8_t vlan_mask_offset = 32;
4350         const uint8_t dst_pool_offset = 8;
4351         const uint8_t rule_mr_offset  = 4;
4352         const uint8_t mirror_rule_mask= 0x0F;
4353
4354         struct ixgbe_mirror_info *mr_info =
4355                         (IXGBE_DEV_PRIVATE_TO_PFDATA(dev->data->dev_private));
4356         struct ixgbe_hw *hw =
4357                 IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
4358         uint8_t mirror_type = 0;
4359
4360         if (ixgbe_vmdq_mode_check(hw) < 0)
4361                 return -ENOTSUP;
4362
4363         if (rule_id >= IXGBE_MAX_MIRROR_RULES)
4364                 return -EINVAL;
4365
4366         if (IXGBE_INVALID_MIRROR_TYPE(mirror_conf->rule_type)) {
4367                 PMD_DRV_LOG(ERR, "unsupported mirror type 0x%x.",
4368                         mirror_conf->rule_type);
4369                 return -EINVAL;
4370         }
4371
4372         if (mirror_conf->rule_type & ETH_MIRROR_VLAN) {
4373                 mirror_type |= IXGBE_MRCTL_VLME;
4374                 /* Check if vlan id is valid and find conresponding VLAN ID index in VLVF */
4375                 for (i = 0;i < IXGBE_VLVF_ENTRIES; i++) {
4376                         if (mirror_conf->vlan.vlan_mask & (1ULL << i)) {
4377                                 /* search vlan id related pool vlan filter index */
4378                                 reg_index = ixgbe_find_vlvf_slot(hw,
4379                                                 mirror_conf->vlan.vlan_id[i]);
4380                                 if(reg_index < 0)
4381                                         return -EINVAL;
4382                                 vlvf = IXGBE_READ_REG(hw, IXGBE_VLVF(reg_index));
4383                                 if ((vlvf & IXGBE_VLVF_VIEN) &&
4384                                     ((vlvf & IXGBE_VLVF_VLANID_MASK) ==
4385                                       mirror_conf->vlan.vlan_id[i]))
4386                                         vlan_mask |= (1ULL << reg_index);
4387                                 else
4388                                         return -EINVAL;
4389                         }
4390                 }
4391
4392                 if (on) {
4393                         mv_lsb = vlan_mask & 0xFFFFFFFF;
4394                         mv_msb = vlan_mask >> vlan_mask_offset;
4395
4396                         mr_info->mr_conf[rule_id].vlan.vlan_mask =
4397                                                 mirror_conf->vlan.vlan_mask;
4398                         for(i = 0 ;i < ETH_VMDQ_MAX_VLAN_FILTERS; i++) {
4399                                 if(mirror_conf->vlan.vlan_mask & (1ULL << i))
4400                                         mr_info->mr_conf[rule_id].vlan.vlan_id[i] =
4401                                                 mirror_conf->vlan.vlan_id[i];
4402                         }
4403                 } else {
4404                         mv_lsb = 0;
4405                         mv_msb = 0;
4406                         mr_info->mr_conf[rule_id].vlan.vlan_mask = 0;
4407                         for(i = 0 ;i < ETH_VMDQ_MAX_VLAN_FILTERS; i++)
4408                                 mr_info->mr_conf[rule_id].vlan.vlan_id[i] = 0;
4409                 }
4410         }
4411
4412         /*
4413          * if enable pool mirror, write related pool mask register,if disable
4414          * pool mirror, clear PFMRVM register
4415          */
4416         if (mirror_conf->rule_type & ETH_MIRROR_VIRTUAL_POOL_UP) {
4417                 mirror_type |= IXGBE_MRCTL_VPME;
4418                 if (on) {
4419                         mp_lsb = mirror_conf->pool_mask & 0xFFFFFFFF;
4420                         mp_msb = mirror_conf->pool_mask >> pool_mask_offset;
4421                         mr_info->mr_conf[rule_id].pool_mask =
4422                                         mirror_conf->pool_mask;
4423
4424                 } else {
4425                         mp_lsb = 0;
4426                         mp_msb = 0;
4427                         mr_info->mr_conf[rule_id].pool_mask = 0;
4428                 }
4429         }
4430         if (mirror_conf->rule_type & ETH_MIRROR_UPLINK_PORT)
4431                 mirror_type |= IXGBE_MRCTL_UPME;
4432         if (mirror_conf->rule_type & ETH_MIRROR_DOWNLINK_PORT)
4433                 mirror_type |= IXGBE_MRCTL_DPME;
4434
4435         /* read  mirror control register and recalculate it */
4436         mr_ctl = IXGBE_READ_REG(hw, IXGBE_MRCTL(rule_id));
4437
4438         if (on) {
4439                 mr_ctl |= mirror_type;
4440                 mr_ctl &= mirror_rule_mask;
4441                 mr_ctl |= mirror_conf->dst_pool << dst_pool_offset;
4442         } else
4443                 mr_ctl &= ~(mirror_conf->rule_type & mirror_rule_mask);
4444
4445         mr_info->mr_conf[rule_id].rule_type = mirror_conf->rule_type;
4446         mr_info->mr_conf[rule_id].dst_pool = mirror_conf->dst_pool;
4447
4448         /* write mirrror control  register */
4449         IXGBE_WRITE_REG(hw, IXGBE_MRCTL(rule_id), mr_ctl);
4450
4451         /* write pool mirrror control  register */
4452         if (mirror_conf->rule_type == ETH_MIRROR_VIRTUAL_POOL_UP) {
4453                 IXGBE_WRITE_REG(hw, IXGBE_VMRVM(rule_id), mp_lsb);
4454                 IXGBE_WRITE_REG(hw, IXGBE_VMRVM(rule_id + rule_mr_offset),
4455                                 mp_msb);
4456         }
4457         /* write VLAN mirrror control  register */
4458         if (mirror_conf->rule_type == ETH_MIRROR_VLAN) {
4459                 IXGBE_WRITE_REG(hw, IXGBE_VMRVLAN(rule_id), mv_lsb);
4460                 IXGBE_WRITE_REG(hw, IXGBE_VMRVLAN(rule_id + rule_mr_offset),
4461                                 mv_msb);
4462         }
4463
4464         return 0;
4465 }
4466
4467 static int
4468 ixgbe_mirror_rule_reset(struct rte_eth_dev *dev, uint8_t rule_id)
4469 {
4470         int mr_ctl = 0;
4471         uint32_t lsb_val = 0;
4472         uint32_t msb_val = 0;
4473         const uint8_t rule_mr_offset = 4;
4474
4475         struct ixgbe_hw *hw =
4476                 IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
4477         struct ixgbe_mirror_info *mr_info =
4478                 (IXGBE_DEV_PRIVATE_TO_PFDATA(dev->data->dev_private));
4479
4480         if (ixgbe_vmdq_mode_check(hw) < 0)
4481                 return -ENOTSUP;
4482
4483         memset(&mr_info->mr_conf[rule_id], 0,
4484                 sizeof(struct rte_eth_mirror_conf));
4485
4486         /* clear PFVMCTL register */
4487         IXGBE_WRITE_REG(hw, IXGBE_MRCTL(rule_id), mr_ctl);
4488
4489         /* clear pool mask register */
4490         IXGBE_WRITE_REG(hw, IXGBE_VMRVM(rule_id), lsb_val);
4491         IXGBE_WRITE_REG(hw, IXGBE_VMRVM(rule_id + rule_mr_offset), msb_val);
4492
4493         /* clear vlan mask register */
4494         IXGBE_WRITE_REG(hw, IXGBE_VMRVLAN(rule_id), lsb_val);
4495         IXGBE_WRITE_REG(hw, IXGBE_VMRVLAN(rule_id + rule_mr_offset), msb_val);
4496
4497         return 0;
4498 }
4499
4500 static int
4501 ixgbevf_dev_rx_queue_intr_enable(struct rte_eth_dev *dev, uint16_t queue_id)
4502 {
4503         uint32_t mask;
4504         struct ixgbe_hw *hw =
4505                 IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
4506
4507         mask = IXGBE_READ_REG(hw, IXGBE_VTEIMS);
4508         mask |= (1 << IXGBE_MISC_VEC_ID);
4509         RTE_SET_USED(queue_id);
4510         IXGBE_WRITE_REG(hw, IXGBE_VTEIMS, mask);
4511
4512         rte_intr_enable(&dev->pci_dev->intr_handle);
4513
4514         return 0;
4515 }
4516
4517 static int
4518 ixgbevf_dev_rx_queue_intr_disable(struct rte_eth_dev *dev, uint16_t queue_id)
4519 {
4520         uint32_t mask;
4521         struct ixgbe_hw *hw =
4522                 IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
4523
4524         mask = IXGBE_READ_REG(hw, IXGBE_VTEIMS);
4525         mask &= ~(1 << IXGBE_MISC_VEC_ID);
4526         RTE_SET_USED(queue_id);
4527         IXGBE_WRITE_REG(hw, IXGBE_VTEIMS, mask);
4528
4529         return 0;
4530 }
4531
4532 static int
4533 ixgbe_dev_rx_queue_intr_enable(struct rte_eth_dev *dev, uint16_t queue_id)
4534 {
4535         uint32_t mask;
4536         struct ixgbe_hw *hw =
4537                 IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
4538         struct ixgbe_interrupt *intr =
4539                 IXGBE_DEV_PRIVATE_TO_INTR(dev->data->dev_private);
4540
4541         if (queue_id < 16) {
4542                 ixgbe_disable_intr(hw);
4543                 intr->mask |= (1 << queue_id);
4544                 ixgbe_enable_intr(dev);
4545         } else if (queue_id < 32) {
4546                 mask = IXGBE_READ_REG(hw, IXGBE_EIMS_EX(0));
4547                 mask &= (1 << queue_id);
4548                 IXGBE_WRITE_REG(hw, IXGBE_EIMS_EX(0), mask);
4549         } else if (queue_id < 64) {
4550                 mask = IXGBE_READ_REG(hw, IXGBE_EIMS_EX(1));
4551                 mask &= (1 << (queue_id - 32));
4552                 IXGBE_WRITE_REG(hw, IXGBE_EIMS_EX(1), mask);
4553         }
4554         rte_intr_enable(&dev->pci_dev->intr_handle);
4555
4556         return 0;
4557 }
4558
4559 static int
4560 ixgbe_dev_rx_queue_intr_disable(struct rte_eth_dev *dev, uint16_t queue_id)
4561 {
4562         uint32_t mask;
4563         struct ixgbe_hw *hw =
4564                 IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
4565         struct ixgbe_interrupt *intr =
4566                 IXGBE_DEV_PRIVATE_TO_INTR(dev->data->dev_private);
4567
4568         if (queue_id < 16) {
4569                 ixgbe_disable_intr(hw);
4570                 intr->mask &= ~(1 << queue_id);
4571                 ixgbe_enable_intr(dev);
4572         } else if (queue_id < 32) {
4573                 mask = IXGBE_READ_REG(hw, IXGBE_EIMS_EX(0));
4574                 mask &= ~(1 << queue_id);
4575                 IXGBE_WRITE_REG(hw, IXGBE_EIMS_EX(0), mask);
4576         } else if (queue_id < 64) {
4577                 mask = IXGBE_READ_REG(hw, IXGBE_EIMS_EX(1));
4578                 mask &= ~(1 << (queue_id - 32));
4579                 IXGBE_WRITE_REG(hw, IXGBE_EIMS_EX(1), mask);
4580         }
4581
4582         return 0;
4583 }
4584
4585 static void
4586 ixgbevf_set_ivar_map(struct ixgbe_hw *hw, int8_t direction,
4587                      uint8_t queue, uint8_t msix_vector)
4588 {
4589         uint32_t tmp, idx;
4590
4591         if (direction == -1) {
4592                 /* other causes */
4593                 msix_vector |= IXGBE_IVAR_ALLOC_VAL;
4594                 tmp = IXGBE_READ_REG(hw, IXGBE_VTIVAR_MISC);
4595                 tmp &= ~0xFF;
4596                 tmp |= msix_vector;
4597                 IXGBE_WRITE_REG(hw, IXGBE_VTIVAR_MISC, tmp);
4598         } else {
4599                 /* rx or tx cause */
4600                 msix_vector |= IXGBE_IVAR_ALLOC_VAL;
4601                 idx = ((16 * (queue & 1)) + (8 * direction));
4602                 tmp = IXGBE_READ_REG(hw, IXGBE_VTIVAR(queue >> 1));
4603                 tmp &= ~(0xFF << idx);
4604                 tmp |= (msix_vector << idx);
4605                 IXGBE_WRITE_REG(hw, IXGBE_VTIVAR(queue >> 1), tmp);
4606         }
4607 }
4608
4609 /**
4610  * set the IVAR registers, mapping interrupt causes to vectors
4611  * @param hw
4612  *  pointer to ixgbe_hw struct
4613  * @direction
4614  *  0 for Rx, 1 for Tx, -1 for other causes
4615  * @queue
4616  *  queue to map the corresponding interrupt to
4617  * @msix_vector
4618  *  the vector to map to the corresponding queue
4619  */
4620 static void
4621 ixgbe_set_ivar_map(struct ixgbe_hw *hw, int8_t direction,
4622                    uint8_t queue, uint8_t msix_vector)
4623 {
4624         uint32_t tmp, idx;
4625
4626         msix_vector |= IXGBE_IVAR_ALLOC_VAL;
4627         if (hw->mac.type == ixgbe_mac_82598EB) {
4628                 if (direction == -1)
4629                         direction = 0;
4630                 idx = (((direction * 64) + queue) >> 2) & 0x1F;
4631                 tmp = IXGBE_READ_REG(hw, IXGBE_IVAR(idx));
4632                 tmp &= ~(0xFF << (8 * (queue & 0x3)));
4633                 tmp |= (msix_vector << (8 * (queue & 0x3)));
4634                 IXGBE_WRITE_REG(hw, IXGBE_IVAR(idx), tmp);
4635         } else if ((hw->mac.type == ixgbe_mac_82599EB) ||
4636                         (hw->mac.type == ixgbe_mac_X540)) {
4637                 if (direction == -1) {
4638                         /* other causes */
4639                         idx = ((queue & 1) * 8);
4640                         tmp = IXGBE_READ_REG(hw, IXGBE_IVAR_MISC);
4641                         tmp &= ~(0xFF << idx);
4642                         tmp |= (msix_vector << idx);
4643                         IXGBE_WRITE_REG(hw, IXGBE_IVAR_MISC, tmp);
4644                 } else {
4645                         /* rx or tx causes */
4646                         idx = ((16 * (queue & 1)) + (8 * direction));
4647                         tmp = IXGBE_READ_REG(hw, IXGBE_IVAR(queue >> 1));
4648                         tmp &= ~(0xFF << idx);
4649                         tmp |= (msix_vector << idx);
4650                         IXGBE_WRITE_REG(hw, IXGBE_IVAR(queue >> 1), tmp);
4651                 }
4652         }
4653 }
4654
4655 static void
4656 ixgbevf_configure_msix(struct rte_eth_dev *dev)
4657 {
4658         struct rte_intr_handle *intr_handle = &dev->pci_dev->intr_handle;
4659         struct ixgbe_hw *hw =
4660                 IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
4661         uint32_t q_idx;
4662         uint32_t vector_idx = IXGBE_MISC_VEC_ID;
4663
4664         /* won't configure msix register if no mapping is done
4665          * between intr vector and event fd.
4666          */
4667         if (!rte_intr_dp_is_en(intr_handle))
4668                 return;
4669
4670         /* Configure all RX queues of VF */
4671         for (q_idx = 0; q_idx < dev->data->nb_rx_queues; q_idx++) {
4672                 /* Force all queue use vector 0,
4673                  * as IXGBE_VF_MAXMSIVECOTR = 1
4674                  */
4675                 ixgbevf_set_ivar_map(hw, 0, q_idx, vector_idx);
4676                 intr_handle->intr_vec[q_idx] = vector_idx;
4677         }
4678
4679         /* Configure VF other cause ivar */
4680         ixgbevf_set_ivar_map(hw, -1, 1, vector_idx);
4681 }
4682
4683 /**
4684  * Sets up the hardware to properly generate MSI-X interrupts
4685  * @hw
4686  *  board private structure
4687  */
4688 static void
4689 ixgbe_configure_msix(struct rte_eth_dev *dev)
4690 {
4691         struct rte_intr_handle *intr_handle = &dev->pci_dev->intr_handle;
4692         struct ixgbe_hw *hw =
4693                 IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
4694         uint32_t queue_id, base = IXGBE_MISC_VEC_ID;
4695         uint32_t vec = IXGBE_MISC_VEC_ID;
4696         uint32_t mask;
4697         uint32_t gpie;
4698
4699         /* won't configure msix register if no mapping is done
4700          * between intr vector and event fd
4701          */
4702         if (!rte_intr_dp_is_en(intr_handle))
4703                 return;
4704
4705         if (rte_intr_allow_others(intr_handle))
4706                 vec = base = IXGBE_RX_VEC_START;
4707
4708         /* setup GPIE for MSI-x mode */
4709         gpie = IXGBE_READ_REG(hw, IXGBE_GPIE);
4710         gpie |= IXGBE_GPIE_MSIX_MODE | IXGBE_GPIE_PBA_SUPPORT |
4711                 IXGBE_GPIE_OCD | IXGBE_GPIE_EIAME;
4712         /* auto clearing and auto setting corresponding bits in EIMS
4713          * when MSI-X interrupt is triggered
4714          */
4715         if (hw->mac.type == ixgbe_mac_82598EB) {
4716                 IXGBE_WRITE_REG(hw, IXGBE_EIAM, IXGBE_EICS_RTX_QUEUE);
4717         } else {
4718                 IXGBE_WRITE_REG(hw, IXGBE_EIAM_EX(0), 0xFFFFFFFF);
4719                 IXGBE_WRITE_REG(hw, IXGBE_EIAM_EX(1), 0xFFFFFFFF);
4720         }
4721         IXGBE_WRITE_REG(hw, IXGBE_GPIE, gpie);
4722
4723         /* Populate the IVAR table and set the ITR values to the
4724          * corresponding register.
4725          */
4726         for (queue_id = 0; queue_id < dev->data->nb_rx_queues;
4727              queue_id++) {
4728                 /* by default, 1:1 mapping */
4729                 ixgbe_set_ivar_map(hw, 0, queue_id, vec);
4730                 intr_handle->intr_vec[queue_id] = vec;
4731                 if (vec < base + intr_handle->nb_efd - 1)
4732                         vec++;
4733         }
4734
4735         switch (hw->mac.type) {
4736         case ixgbe_mac_82598EB:
4737                 ixgbe_set_ivar_map(hw, -1, IXGBE_IVAR_OTHER_CAUSES_INDEX,
4738                                    IXGBE_MISC_VEC_ID);
4739                 break;
4740         case ixgbe_mac_82599EB:
4741         case ixgbe_mac_X540:
4742                 ixgbe_set_ivar_map(hw, -1, 1, IXGBE_MISC_VEC_ID);
4743                 break;
4744         default:
4745                 break;
4746         }
4747         IXGBE_WRITE_REG(hw, IXGBE_EITR(IXGBE_MISC_VEC_ID),
4748                         IXGBE_MIN_INTER_INTERRUPT_INTERVAL_DEFAULT & 0xFFF);
4749
4750         /* set up to autoclear timer, and the vectors */
4751         mask = IXGBE_EIMS_ENABLE_MASK;
4752         mask &= ~(IXGBE_EIMS_OTHER |
4753                   IXGBE_EIMS_MAILBOX |
4754                   IXGBE_EIMS_LSC);
4755
4756         IXGBE_WRITE_REG(hw, IXGBE_EIAC, mask);
4757 }
4758
4759 static int ixgbe_set_queue_rate_limit(struct rte_eth_dev *dev,
4760         uint16_t queue_idx, uint16_t tx_rate)
4761 {
4762         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
4763         uint32_t rf_dec, rf_int;
4764         uint32_t bcnrc_val;
4765         uint16_t link_speed = dev->data->dev_link.link_speed;
4766
4767         if (queue_idx >= hw->mac.max_tx_queues)
4768                 return -EINVAL;
4769
4770         if (tx_rate != 0) {
4771                 /* Calculate the rate factor values to set */
4772                 rf_int = (uint32_t)link_speed / (uint32_t)tx_rate;
4773                 rf_dec = (uint32_t)link_speed % (uint32_t)tx_rate;
4774                 rf_dec = (rf_dec << IXGBE_RTTBCNRC_RF_INT_SHIFT) / tx_rate;
4775
4776                 bcnrc_val = IXGBE_RTTBCNRC_RS_ENA;
4777                 bcnrc_val |= ((rf_int << IXGBE_RTTBCNRC_RF_INT_SHIFT) &
4778                                 IXGBE_RTTBCNRC_RF_INT_MASK_M);
4779                 bcnrc_val |= (rf_dec & IXGBE_RTTBCNRC_RF_DEC_MASK);
4780         } else {
4781                 bcnrc_val = 0;
4782         }
4783
4784         /*
4785          * Set global transmit compensation time to the MMW_SIZE in RTTBCNRM
4786          * register. MMW_SIZE=0x014 if 9728-byte jumbo is supported, otherwise
4787          * set as 0x4.
4788          */
4789         if ((dev->data->dev_conf.rxmode.jumbo_frame == 1) &&
4790                 (dev->data->dev_conf.rxmode.max_rx_pkt_len >=
4791                                 IXGBE_MAX_JUMBO_FRAME_SIZE))
4792                 IXGBE_WRITE_REG(hw, IXGBE_RTTBCNRM,
4793                         IXGBE_MMW_SIZE_JUMBO_FRAME);
4794         else
4795                 IXGBE_WRITE_REG(hw, IXGBE_RTTBCNRM,
4796                         IXGBE_MMW_SIZE_DEFAULT);
4797
4798         /* Set RTTBCNRC of queue X */
4799         IXGBE_WRITE_REG(hw, IXGBE_RTTDQSEL, queue_idx);
4800         IXGBE_WRITE_REG(hw, IXGBE_RTTBCNRC, bcnrc_val);
4801         IXGBE_WRITE_FLUSH(hw);
4802
4803         return 0;
4804 }
4805
4806 static int ixgbe_set_vf_rate_limit(struct rte_eth_dev *dev, uint16_t vf,
4807         uint16_t tx_rate, uint64_t q_msk)
4808 {
4809         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
4810         struct ixgbe_vf_info *vfinfo =
4811                 *(IXGBE_DEV_PRIVATE_TO_P_VFDATA(dev->data->dev_private));
4812         uint8_t  nb_q_per_pool = RTE_ETH_DEV_SRIOV(dev).nb_q_per_pool;
4813         uint32_t queue_stride =
4814                 IXGBE_MAX_RX_QUEUE_NUM / RTE_ETH_DEV_SRIOV(dev).active;
4815         uint32_t queue_idx = vf * queue_stride, idx = 0, vf_idx;
4816         uint32_t queue_end = queue_idx + nb_q_per_pool - 1;
4817         uint16_t total_rate = 0;
4818
4819         if (queue_end >= hw->mac.max_tx_queues)
4820                 return -EINVAL;
4821
4822         if (vfinfo != NULL) {
4823                 for (vf_idx = 0; vf_idx < dev->pci_dev->max_vfs; vf_idx++) {
4824                         if (vf_idx == vf)
4825                                 continue;
4826                         for (idx = 0; idx < RTE_DIM(vfinfo[vf_idx].tx_rate);
4827                                 idx++)
4828                                 total_rate += vfinfo[vf_idx].tx_rate[idx];
4829                 }
4830         } else
4831                 return -EINVAL;
4832
4833         /* Store tx_rate for this vf. */
4834         for (idx = 0; idx < nb_q_per_pool; idx++) {
4835                 if (((uint64_t)0x1 << idx) & q_msk) {
4836                         if (vfinfo[vf].tx_rate[idx] != tx_rate)
4837                                 vfinfo[vf].tx_rate[idx] = tx_rate;
4838                         total_rate += tx_rate;
4839                 }
4840         }
4841
4842         if (total_rate > dev->data->dev_link.link_speed) {
4843                 /*
4844                  * Reset stored TX rate of the VF if it causes exceed
4845                  * link speed.
4846                  */
4847                 memset(vfinfo[vf].tx_rate, 0, sizeof(vfinfo[vf].tx_rate));
4848                 return -EINVAL;
4849         }
4850
4851         /* Set RTTBCNRC of each queue/pool for vf X  */
4852         for (; queue_idx <= queue_end; queue_idx++) {
4853                 if (0x1 & q_msk)
4854                         ixgbe_set_queue_rate_limit(dev, queue_idx, tx_rate);
4855                 q_msk = q_msk >> 1;
4856         }
4857
4858         return 0;
4859 }
4860
4861 static void
4862 ixgbevf_add_mac_addr(struct rte_eth_dev *dev, struct ether_addr *mac_addr,
4863                      __attribute__((unused)) uint32_t index,
4864                      __attribute__((unused)) uint32_t pool)
4865 {
4866         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
4867         int diag;
4868
4869         /*
4870          * On a 82599 VF, adding again the same MAC addr is not an idempotent
4871          * operation. Trap this case to avoid exhausting the [very limited]
4872          * set of PF resources used to store VF MAC addresses.
4873          */
4874         if (memcmp(hw->mac.perm_addr, mac_addr, sizeof(struct ether_addr)) == 0)
4875                 return;
4876         diag = ixgbevf_set_uc_addr_vf(hw, 2, mac_addr->addr_bytes);
4877         if (diag == 0)
4878                 return;
4879         PMD_DRV_LOG(ERR, "Unable to add MAC address - diag=%d", diag);
4880 }
4881
4882 static void
4883 ixgbevf_remove_mac_addr(struct rte_eth_dev *dev, uint32_t index)
4884 {
4885         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
4886         struct ether_addr *perm_addr = (struct ether_addr *) hw->mac.perm_addr;
4887         struct ether_addr *mac_addr;
4888         uint32_t i;
4889         int diag;
4890
4891         /*
4892          * The IXGBE_VF_SET_MACVLAN command of the ixgbe-pf driver does
4893          * not support the deletion of a given MAC address.
4894          * Instead, it imposes to delete all MAC addresses, then to add again
4895          * all MAC addresses with the exception of the one to be deleted.
4896          */
4897         (void) ixgbevf_set_uc_addr_vf(hw, 0, NULL);
4898
4899         /*
4900          * Add again all MAC addresses, with the exception of the deleted one
4901          * and of the permanent MAC address.
4902          */
4903         for (i = 0, mac_addr = dev->data->mac_addrs;
4904              i < hw->mac.num_rar_entries; i++, mac_addr++) {
4905                 /* Skip the deleted MAC address */
4906                 if (i == index)
4907                         continue;
4908                 /* Skip NULL MAC addresses */
4909                 if (is_zero_ether_addr(mac_addr))
4910                         continue;
4911                 /* Skip the permanent MAC address */
4912                 if (memcmp(perm_addr, mac_addr, sizeof(struct ether_addr)) == 0)
4913                         continue;
4914                 diag = ixgbevf_set_uc_addr_vf(hw, 2, mac_addr->addr_bytes);
4915                 if (diag != 0)
4916                         PMD_DRV_LOG(ERR,
4917                                     "Adding again MAC address "
4918                                     "%02x:%02x:%02x:%02x:%02x:%02x failed "
4919                                     "diag=%d",
4920                                     mac_addr->addr_bytes[0],
4921                                     mac_addr->addr_bytes[1],
4922                                     mac_addr->addr_bytes[2],
4923                                     mac_addr->addr_bytes[3],
4924                                     mac_addr->addr_bytes[4],
4925                                     mac_addr->addr_bytes[5],
4926                                     diag);
4927         }
4928 }
4929
4930 static void
4931 ixgbevf_set_default_mac_addr(struct rte_eth_dev *dev, struct ether_addr *addr)
4932 {
4933         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
4934
4935         hw->mac.ops.set_rar(hw, 0, (void *)addr, 0, 0);
4936 }
4937
4938 #define MAC_TYPE_FILTER_SUP(type)    do {\
4939         if ((type) != ixgbe_mac_82599EB && (type) != ixgbe_mac_X540 &&\
4940                 (type) != ixgbe_mac_X550)\
4941                 return -ENOTSUP;\
4942 } while (0)
4943
4944 static int
4945 ixgbe_syn_filter_set(struct rte_eth_dev *dev,
4946                         struct rte_eth_syn_filter *filter,
4947                         bool add)
4948 {
4949         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
4950         uint32_t synqf;
4951
4952         if (filter->queue >= IXGBE_MAX_RX_QUEUE_NUM)
4953                 return -EINVAL;
4954
4955         synqf = IXGBE_READ_REG(hw, IXGBE_SYNQF);
4956
4957         if (add) {
4958                 if (synqf & IXGBE_SYN_FILTER_ENABLE)
4959                         return -EINVAL;
4960                 synqf = (uint32_t)(((filter->queue << IXGBE_SYN_FILTER_QUEUE_SHIFT) &
4961                         IXGBE_SYN_FILTER_QUEUE) | IXGBE_SYN_FILTER_ENABLE);
4962
4963                 if (filter->hig_pri)
4964                         synqf |= IXGBE_SYN_FILTER_SYNQFP;
4965                 else
4966                         synqf &= ~IXGBE_SYN_FILTER_SYNQFP;
4967         } else {
4968                 if (!(synqf & IXGBE_SYN_FILTER_ENABLE))
4969                         return -ENOENT;
4970                 synqf &= ~(IXGBE_SYN_FILTER_QUEUE | IXGBE_SYN_FILTER_ENABLE);
4971         }
4972         IXGBE_WRITE_REG(hw, IXGBE_SYNQF, synqf);
4973         IXGBE_WRITE_FLUSH(hw);
4974         return 0;
4975 }
4976
4977 static int
4978 ixgbe_syn_filter_get(struct rte_eth_dev *dev,
4979                         struct rte_eth_syn_filter *filter)
4980 {
4981         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
4982         uint32_t synqf = IXGBE_READ_REG(hw, IXGBE_SYNQF);
4983
4984         if (synqf & IXGBE_SYN_FILTER_ENABLE) {
4985                 filter->hig_pri = (synqf & IXGBE_SYN_FILTER_SYNQFP) ? 1 : 0;
4986                 filter->queue = (uint16_t)((synqf & IXGBE_SYN_FILTER_QUEUE) >> 1);
4987                 return 0;
4988         }
4989         return -ENOENT;
4990 }
4991
4992 static int
4993 ixgbe_syn_filter_handle(struct rte_eth_dev *dev,
4994                         enum rte_filter_op filter_op,
4995                         void *arg)
4996 {
4997         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
4998         int ret;
4999
5000         MAC_TYPE_FILTER_SUP(hw->mac.type);
5001
5002         if (filter_op == RTE_ETH_FILTER_NOP)
5003                 return 0;
5004
5005         if (arg == NULL) {
5006                 PMD_DRV_LOG(ERR, "arg shouldn't be NULL for operation %u",
5007                             filter_op);
5008                 return -EINVAL;
5009         }
5010
5011         switch (filter_op) {
5012         case RTE_ETH_FILTER_ADD:
5013                 ret = ixgbe_syn_filter_set(dev,
5014                                 (struct rte_eth_syn_filter *)arg,
5015                                 TRUE);
5016                 break;
5017         case RTE_ETH_FILTER_DELETE:
5018                 ret = ixgbe_syn_filter_set(dev,
5019                                 (struct rte_eth_syn_filter *)arg,
5020                                 FALSE);
5021                 break;
5022         case RTE_ETH_FILTER_GET:
5023                 ret = ixgbe_syn_filter_get(dev,
5024                                 (struct rte_eth_syn_filter *)arg);
5025                 break;
5026         default:
5027                 PMD_DRV_LOG(ERR, "unsupported operation %u\n", filter_op);
5028                 ret = -EINVAL;
5029                 break;
5030         }
5031
5032         return ret;
5033 }
5034
5035
5036 static inline enum ixgbe_5tuple_protocol
5037 convert_protocol_type(uint8_t protocol_value)
5038 {
5039         if (protocol_value == IPPROTO_TCP)
5040                 return IXGBE_FILTER_PROTOCOL_TCP;
5041         else if (protocol_value == IPPROTO_UDP)
5042                 return IXGBE_FILTER_PROTOCOL_UDP;
5043         else if (protocol_value == IPPROTO_SCTP)
5044                 return IXGBE_FILTER_PROTOCOL_SCTP;
5045         else
5046                 return IXGBE_FILTER_PROTOCOL_NONE;
5047 }
5048
5049 /*
5050  * add a 5tuple filter
5051  *
5052  * @param
5053  * dev: Pointer to struct rte_eth_dev.
5054  * index: the index the filter allocates.
5055  * filter: ponter to the filter that will be added.
5056  * rx_queue: the queue id the filter assigned to.
5057  *
5058  * @return
5059  *    - On success, zero.
5060  *    - On failure, a negative value.
5061  */
5062 static int
5063 ixgbe_add_5tuple_filter(struct rte_eth_dev *dev,
5064                         struct ixgbe_5tuple_filter *filter)
5065 {
5066         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
5067         struct ixgbe_filter_info *filter_info =
5068                 IXGBE_DEV_PRIVATE_TO_FILTER_INFO(dev->data->dev_private);
5069         int i, idx, shift;
5070         uint32_t ftqf, sdpqf;
5071         uint32_t l34timir = 0;
5072         uint8_t mask = 0xff;
5073
5074         /*
5075          * look for an unused 5tuple filter index,
5076          * and insert the filter to list.
5077          */
5078         for (i = 0; i < IXGBE_MAX_FTQF_FILTERS; i++) {
5079                 idx = i / (sizeof(uint32_t) * NBBY);
5080                 shift = i % (sizeof(uint32_t) * NBBY);
5081                 if (!(filter_info->fivetuple_mask[idx] & (1 << shift))) {
5082                         filter_info->fivetuple_mask[idx] |= 1 << shift;
5083                         filter->index = i;
5084                         TAILQ_INSERT_TAIL(&filter_info->fivetuple_list,
5085                                           filter,
5086                                           entries);
5087                         break;
5088                 }
5089         }
5090         if (i >= IXGBE_MAX_FTQF_FILTERS) {
5091                 PMD_DRV_LOG(ERR, "5tuple filters are full.");
5092                 return -ENOSYS;
5093         }
5094
5095         sdpqf = (uint32_t)(filter->filter_info.dst_port <<
5096                                 IXGBE_SDPQF_DSTPORT_SHIFT);
5097         sdpqf = sdpqf | (filter->filter_info.src_port & IXGBE_SDPQF_SRCPORT);
5098
5099         ftqf = (uint32_t)(filter->filter_info.proto &
5100                 IXGBE_FTQF_PROTOCOL_MASK);
5101         ftqf |= (uint32_t)((filter->filter_info.priority &
5102                 IXGBE_FTQF_PRIORITY_MASK) << IXGBE_FTQF_PRIORITY_SHIFT);
5103         if (filter->filter_info.src_ip_mask == 0) /* 0 means compare. */
5104                 mask &= IXGBE_FTQF_SOURCE_ADDR_MASK;
5105         if (filter->filter_info.dst_ip_mask == 0)
5106                 mask &= IXGBE_FTQF_DEST_ADDR_MASK;
5107         if (filter->filter_info.src_port_mask == 0)
5108                 mask &= IXGBE_FTQF_SOURCE_PORT_MASK;
5109         if (filter->filter_info.dst_port_mask == 0)
5110                 mask &= IXGBE_FTQF_DEST_PORT_MASK;
5111         if (filter->filter_info.proto_mask == 0)
5112                 mask &= IXGBE_FTQF_PROTOCOL_COMP_MASK;
5113         ftqf |= mask << IXGBE_FTQF_5TUPLE_MASK_SHIFT;
5114         ftqf |= IXGBE_FTQF_POOL_MASK_EN;
5115         ftqf |= IXGBE_FTQF_QUEUE_ENABLE;
5116
5117         IXGBE_WRITE_REG(hw, IXGBE_DAQF(i), filter->filter_info.dst_ip);
5118         IXGBE_WRITE_REG(hw, IXGBE_SAQF(i), filter->filter_info.src_ip);
5119         IXGBE_WRITE_REG(hw, IXGBE_SDPQF(i), sdpqf);
5120         IXGBE_WRITE_REG(hw, IXGBE_FTQF(i), ftqf);
5121
5122         l34timir |= IXGBE_L34T_IMIR_RESERVE;
5123         l34timir |= (uint32_t)(filter->queue <<
5124                                 IXGBE_L34T_IMIR_QUEUE_SHIFT);
5125         IXGBE_WRITE_REG(hw, IXGBE_L34T_IMIR(i), l34timir);
5126         return 0;
5127 }
5128
5129 /*
5130  * remove a 5tuple filter
5131  *
5132  * @param
5133  * dev: Pointer to struct rte_eth_dev.
5134  * filter: the pointer of the filter will be removed.
5135  */
5136 static void
5137 ixgbe_remove_5tuple_filter(struct rte_eth_dev *dev,
5138                         struct ixgbe_5tuple_filter *filter)
5139 {
5140         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
5141         struct ixgbe_filter_info *filter_info =
5142                 IXGBE_DEV_PRIVATE_TO_FILTER_INFO(dev->data->dev_private);
5143         uint16_t index = filter->index;
5144
5145         filter_info->fivetuple_mask[index / (sizeof(uint32_t) * NBBY)] &=
5146                                 ~(1 << (index % (sizeof(uint32_t) * NBBY)));
5147         TAILQ_REMOVE(&filter_info->fivetuple_list, filter, entries);
5148         rte_free(filter);
5149
5150         IXGBE_WRITE_REG(hw, IXGBE_DAQF(index), 0);
5151         IXGBE_WRITE_REG(hw, IXGBE_SAQF(index), 0);
5152         IXGBE_WRITE_REG(hw, IXGBE_SDPQF(index), 0);
5153         IXGBE_WRITE_REG(hw, IXGBE_FTQF(index), 0);
5154         IXGBE_WRITE_REG(hw, IXGBE_L34T_IMIR(index), 0);
5155 }
5156
5157 static int
5158 ixgbevf_dev_set_mtu(struct rte_eth_dev *dev, uint16_t mtu)
5159 {
5160         struct ixgbe_hw *hw;
5161         uint32_t max_frame = mtu + ETHER_HDR_LEN + ETHER_CRC_LEN;
5162
5163         hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
5164
5165         if ((mtu < ETHER_MIN_MTU) || (max_frame > ETHER_MAX_JUMBO_FRAME_LEN))
5166                 return -EINVAL;
5167
5168         /* refuse mtu that requires the support of scattered packets when this
5169          * feature has not been enabled before. */
5170         if (!dev->data->scattered_rx &&
5171             (max_frame + 2 * IXGBE_VLAN_TAG_SIZE >
5172              dev->data->min_rx_buf_size - RTE_PKTMBUF_HEADROOM))
5173                 return -EINVAL;
5174
5175         /*
5176          * When supported by the underlying PF driver, use the IXGBE_VF_SET_MTU
5177          * request of the version 2.0 of the mailbox API.
5178          * For now, use the IXGBE_VF_SET_LPE request of the version 1.0
5179          * of the mailbox API.
5180          * This call to IXGBE_SET_LPE action won't work with ixgbe pf drivers
5181          * prior to 3.11.33 which contains the following change:
5182          * "ixgbe: Enable jumbo frames support w/ SR-IOV"
5183          */
5184         ixgbevf_rlpml_set_vf(hw, max_frame);
5185
5186         /* update max frame size */
5187         dev->data->dev_conf.rxmode.max_rx_pkt_len = max_frame;
5188         return 0;
5189 }
5190
5191 #define MAC_TYPE_FILTER_SUP_EXT(type)    do {\
5192         if ((type) != ixgbe_mac_82599EB && (type) != ixgbe_mac_X540)\
5193                 return -ENOTSUP;\
5194 } while (0)
5195
5196 static inline struct ixgbe_5tuple_filter *
5197 ixgbe_5tuple_filter_lookup(struct ixgbe_5tuple_filter_list *filter_list,
5198                         struct ixgbe_5tuple_filter_info *key)
5199 {
5200         struct ixgbe_5tuple_filter *it;
5201
5202         TAILQ_FOREACH(it, filter_list, entries) {
5203                 if (memcmp(key, &it->filter_info,
5204                         sizeof(struct ixgbe_5tuple_filter_info)) == 0) {
5205                         return it;
5206                 }
5207         }
5208         return NULL;
5209 }
5210
5211 /* translate elements in struct rte_eth_ntuple_filter to struct ixgbe_5tuple_filter_info*/
5212 static inline int
5213 ntuple_filter_to_5tuple(struct rte_eth_ntuple_filter *filter,
5214                         struct ixgbe_5tuple_filter_info *filter_info)
5215 {
5216         if (filter->queue >= IXGBE_MAX_RX_QUEUE_NUM ||
5217                 filter->priority > IXGBE_5TUPLE_MAX_PRI ||
5218                 filter->priority < IXGBE_5TUPLE_MIN_PRI)
5219                 return -EINVAL;
5220
5221         switch (filter->dst_ip_mask) {
5222         case UINT32_MAX:
5223                 filter_info->dst_ip_mask = 0;
5224                 filter_info->dst_ip = filter->dst_ip;
5225                 break;
5226         case 0:
5227                 filter_info->dst_ip_mask = 1;
5228                 break;
5229         default:
5230                 PMD_DRV_LOG(ERR, "invalid dst_ip mask.");
5231                 return -EINVAL;
5232         }
5233
5234         switch (filter->src_ip_mask) {
5235         case UINT32_MAX:
5236                 filter_info->src_ip_mask = 0;
5237                 filter_info->src_ip = filter->src_ip;
5238                 break;
5239         case 0:
5240                 filter_info->src_ip_mask = 1;
5241                 break;
5242         default:
5243                 PMD_DRV_LOG(ERR, "invalid src_ip mask.");
5244                 return -EINVAL;
5245         }
5246
5247         switch (filter->dst_port_mask) {
5248         case UINT16_MAX:
5249                 filter_info->dst_port_mask = 0;
5250                 filter_info->dst_port = filter->dst_port;
5251                 break;
5252         case 0:
5253                 filter_info->dst_port_mask = 1;
5254                 break;
5255         default:
5256                 PMD_DRV_LOG(ERR, "invalid dst_port mask.");
5257                 return -EINVAL;
5258         }
5259
5260         switch (filter->src_port_mask) {
5261         case UINT16_MAX:
5262                 filter_info->src_port_mask = 0;
5263                 filter_info->src_port = filter->src_port;
5264                 break;
5265         case 0:
5266                 filter_info->src_port_mask = 1;
5267                 break;
5268         default:
5269                 PMD_DRV_LOG(ERR, "invalid src_port mask.");
5270                 return -EINVAL;
5271         }
5272
5273         switch (filter->proto_mask) {
5274         case UINT8_MAX:
5275                 filter_info->proto_mask = 0;
5276                 filter_info->proto =
5277                         convert_protocol_type(filter->proto);
5278                 break;
5279         case 0:
5280                 filter_info->proto_mask = 1;
5281                 break;
5282         default:
5283                 PMD_DRV_LOG(ERR, "invalid protocol mask.");
5284                 return -EINVAL;
5285         }
5286
5287         filter_info->priority = (uint8_t)filter->priority;
5288         return 0;
5289 }
5290
5291 /*
5292  * add or delete a ntuple filter
5293  *
5294  * @param
5295  * dev: Pointer to struct rte_eth_dev.
5296  * ntuple_filter: Pointer to struct rte_eth_ntuple_filter
5297  * add: if true, add filter, if false, remove filter
5298  *
5299  * @return
5300  *    - On success, zero.
5301  *    - On failure, a negative value.
5302  */
5303 static int
5304 ixgbe_add_del_ntuple_filter(struct rte_eth_dev *dev,
5305                         struct rte_eth_ntuple_filter *ntuple_filter,
5306                         bool add)
5307 {
5308         struct ixgbe_filter_info *filter_info =
5309                 IXGBE_DEV_PRIVATE_TO_FILTER_INFO(dev->data->dev_private);
5310         struct ixgbe_5tuple_filter_info filter_5tuple;
5311         struct ixgbe_5tuple_filter *filter;
5312         int ret;
5313
5314         if (ntuple_filter->flags != RTE_5TUPLE_FLAGS) {
5315                 PMD_DRV_LOG(ERR, "only 5tuple is supported.");
5316                 return -EINVAL;
5317         }
5318
5319         memset(&filter_5tuple, 0, sizeof(struct ixgbe_5tuple_filter_info));
5320         ret = ntuple_filter_to_5tuple(ntuple_filter, &filter_5tuple);
5321         if (ret < 0)
5322                 return ret;
5323
5324         filter = ixgbe_5tuple_filter_lookup(&filter_info->fivetuple_list,
5325                                          &filter_5tuple);
5326         if (filter != NULL && add) {
5327                 PMD_DRV_LOG(ERR, "filter exists.");
5328                 return -EEXIST;
5329         }
5330         if (filter == NULL && !add) {
5331                 PMD_DRV_LOG(ERR, "filter doesn't exist.");
5332                 return -ENOENT;
5333         }
5334
5335         if (add) {
5336                 filter = rte_zmalloc("ixgbe_5tuple_filter",
5337                                 sizeof(struct ixgbe_5tuple_filter), 0);
5338                 if (filter == NULL)
5339                         return -ENOMEM;
5340                 (void)rte_memcpy(&filter->filter_info,
5341                                  &filter_5tuple,
5342                                  sizeof(struct ixgbe_5tuple_filter_info));
5343                 filter->queue = ntuple_filter->queue;
5344                 ret = ixgbe_add_5tuple_filter(dev, filter);
5345                 if (ret < 0) {
5346                         rte_free(filter);
5347                         return ret;
5348                 }
5349         } else
5350                 ixgbe_remove_5tuple_filter(dev, filter);
5351
5352         return 0;
5353 }
5354
5355 /*
5356  * get a ntuple filter
5357  *
5358  * @param
5359  * dev: Pointer to struct rte_eth_dev.
5360  * ntuple_filter: Pointer to struct rte_eth_ntuple_filter
5361  *
5362  * @return
5363  *    - On success, zero.
5364  *    - On failure, a negative value.
5365  */
5366 static int
5367 ixgbe_get_ntuple_filter(struct rte_eth_dev *dev,
5368                         struct rte_eth_ntuple_filter *ntuple_filter)
5369 {
5370         struct ixgbe_filter_info *filter_info =
5371                 IXGBE_DEV_PRIVATE_TO_FILTER_INFO(dev->data->dev_private);
5372         struct ixgbe_5tuple_filter_info filter_5tuple;
5373         struct ixgbe_5tuple_filter *filter;
5374         int ret;
5375
5376         if (ntuple_filter->flags != RTE_5TUPLE_FLAGS) {
5377                 PMD_DRV_LOG(ERR, "only 5tuple is supported.");
5378                 return -EINVAL;
5379         }
5380
5381         memset(&filter_5tuple, 0, sizeof(struct ixgbe_5tuple_filter_info));
5382         ret = ntuple_filter_to_5tuple(ntuple_filter, &filter_5tuple);
5383         if (ret < 0)
5384                 return ret;
5385
5386         filter = ixgbe_5tuple_filter_lookup(&filter_info->fivetuple_list,
5387                                          &filter_5tuple);
5388         if (filter == NULL) {
5389                 PMD_DRV_LOG(ERR, "filter doesn't exist.");
5390                 return -ENOENT;
5391         }
5392         ntuple_filter->queue = filter->queue;
5393         return 0;
5394 }
5395
5396 /*
5397  * ixgbe_ntuple_filter_handle - Handle operations for ntuple filter.
5398  * @dev: pointer to rte_eth_dev structure
5399  * @filter_op:operation will be taken.
5400  * @arg: a pointer to specific structure corresponding to the filter_op
5401  *
5402  * @return
5403  *    - On success, zero.
5404  *    - On failure, a negative value.
5405  */
5406 static int
5407 ixgbe_ntuple_filter_handle(struct rte_eth_dev *dev,
5408                                 enum rte_filter_op filter_op,
5409                                 void *arg)
5410 {
5411         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
5412         int ret;
5413
5414         MAC_TYPE_FILTER_SUP_EXT(hw->mac.type);
5415
5416         if (filter_op == RTE_ETH_FILTER_NOP)
5417                 return 0;
5418
5419         if (arg == NULL) {
5420                 PMD_DRV_LOG(ERR, "arg shouldn't be NULL for operation %u.",
5421                             filter_op);
5422                 return -EINVAL;
5423         }
5424
5425         switch (filter_op) {
5426         case RTE_ETH_FILTER_ADD:
5427                 ret = ixgbe_add_del_ntuple_filter(dev,
5428                         (struct rte_eth_ntuple_filter *)arg,
5429                         TRUE);
5430                 break;
5431         case RTE_ETH_FILTER_DELETE:
5432                 ret = ixgbe_add_del_ntuple_filter(dev,
5433                         (struct rte_eth_ntuple_filter *)arg,
5434                         FALSE);
5435                 break;
5436         case RTE_ETH_FILTER_GET:
5437                 ret = ixgbe_get_ntuple_filter(dev,
5438                         (struct rte_eth_ntuple_filter *)arg);
5439                 break;
5440         default:
5441                 PMD_DRV_LOG(ERR, "unsupported operation %u.", filter_op);
5442                 ret = -EINVAL;
5443                 break;
5444         }
5445         return ret;
5446 }
5447
5448 static inline int
5449 ixgbe_ethertype_filter_lookup(struct ixgbe_filter_info *filter_info,
5450                         uint16_t ethertype)
5451 {
5452         int i;
5453
5454         for (i = 0; i < IXGBE_MAX_ETQF_FILTERS; i++) {
5455                 if (filter_info->ethertype_filters[i] == ethertype &&
5456                     (filter_info->ethertype_mask & (1 << i)))
5457                         return i;
5458         }
5459         return -1;
5460 }
5461
5462 static inline int
5463 ixgbe_ethertype_filter_insert(struct ixgbe_filter_info *filter_info,
5464                         uint16_t ethertype)
5465 {
5466         int i;
5467
5468         for (i = 0; i < IXGBE_MAX_ETQF_FILTERS; i++) {
5469                 if (!(filter_info->ethertype_mask & (1 << i))) {
5470                         filter_info->ethertype_mask |= 1 << i;
5471                         filter_info->ethertype_filters[i] = ethertype;
5472                         return i;
5473                 }
5474         }
5475         return -1;
5476 }
5477
5478 static inline int
5479 ixgbe_ethertype_filter_remove(struct ixgbe_filter_info *filter_info,
5480                         uint8_t idx)
5481 {
5482         if (idx >= IXGBE_MAX_ETQF_FILTERS)
5483                 return -1;
5484         filter_info->ethertype_mask &= ~(1 << idx);
5485         filter_info->ethertype_filters[idx] = 0;
5486         return idx;
5487 }
5488
5489 static int
5490 ixgbe_add_del_ethertype_filter(struct rte_eth_dev *dev,
5491                         struct rte_eth_ethertype_filter *filter,
5492                         bool add)
5493 {
5494         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
5495         struct ixgbe_filter_info *filter_info =
5496                 IXGBE_DEV_PRIVATE_TO_FILTER_INFO(dev->data->dev_private);
5497         uint32_t etqf = 0;
5498         uint32_t etqs = 0;
5499         int ret;
5500
5501         if (filter->queue >= IXGBE_MAX_RX_QUEUE_NUM)
5502                 return -EINVAL;
5503
5504         if (filter->ether_type == ETHER_TYPE_IPv4 ||
5505                 filter->ether_type == ETHER_TYPE_IPv6) {
5506                 PMD_DRV_LOG(ERR, "unsupported ether_type(0x%04x) in"
5507                         " ethertype filter.", filter->ether_type);
5508                 return -EINVAL;
5509         }
5510
5511         if (filter->flags & RTE_ETHTYPE_FLAGS_MAC) {
5512                 PMD_DRV_LOG(ERR, "mac compare is unsupported.");
5513                 return -EINVAL;
5514         }
5515         if (filter->flags & RTE_ETHTYPE_FLAGS_DROP) {
5516                 PMD_DRV_LOG(ERR, "drop option is unsupported.");
5517                 return -EINVAL;
5518         }
5519
5520         ret = ixgbe_ethertype_filter_lookup(filter_info, filter->ether_type);
5521         if (ret >= 0 && add) {
5522                 PMD_DRV_LOG(ERR, "ethertype (0x%04x) filter exists.",
5523                             filter->ether_type);
5524                 return -EEXIST;
5525         }
5526         if (ret < 0 && !add) {
5527                 PMD_DRV_LOG(ERR, "ethertype (0x%04x) filter doesn't exist.",
5528                             filter->ether_type);
5529                 return -ENOENT;
5530         }
5531
5532         if (add) {
5533                 ret = ixgbe_ethertype_filter_insert(filter_info,
5534                         filter->ether_type);
5535                 if (ret < 0) {
5536                         PMD_DRV_LOG(ERR, "ethertype filters are full.");
5537                         return -ENOSYS;
5538                 }
5539                 etqf = IXGBE_ETQF_FILTER_EN;
5540                 etqf |= (uint32_t)filter->ether_type;
5541                 etqs |= (uint32_t)((filter->queue <<
5542                                     IXGBE_ETQS_RX_QUEUE_SHIFT) &
5543                                     IXGBE_ETQS_RX_QUEUE);
5544                 etqs |= IXGBE_ETQS_QUEUE_EN;
5545         } else {
5546                 ret = ixgbe_ethertype_filter_remove(filter_info, (uint8_t)ret);
5547                 if (ret < 0)
5548                         return -ENOSYS;
5549         }
5550         IXGBE_WRITE_REG(hw, IXGBE_ETQF(ret), etqf);
5551         IXGBE_WRITE_REG(hw, IXGBE_ETQS(ret), etqs);
5552         IXGBE_WRITE_FLUSH(hw);
5553
5554         return 0;
5555 }
5556
5557 static int
5558 ixgbe_get_ethertype_filter(struct rte_eth_dev *dev,
5559                         struct rte_eth_ethertype_filter *filter)
5560 {
5561         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
5562         struct ixgbe_filter_info *filter_info =
5563                 IXGBE_DEV_PRIVATE_TO_FILTER_INFO(dev->data->dev_private);
5564         uint32_t etqf, etqs;
5565         int ret;
5566
5567         ret = ixgbe_ethertype_filter_lookup(filter_info, filter->ether_type);
5568         if (ret < 0) {
5569                 PMD_DRV_LOG(ERR, "ethertype (0x%04x) filter doesn't exist.",
5570                             filter->ether_type);
5571                 return -ENOENT;
5572         }
5573
5574         etqf = IXGBE_READ_REG(hw, IXGBE_ETQF(ret));
5575         if (etqf & IXGBE_ETQF_FILTER_EN) {
5576                 etqs = IXGBE_READ_REG(hw, IXGBE_ETQS(ret));
5577                 filter->ether_type = etqf & IXGBE_ETQF_ETHERTYPE;
5578                 filter->flags = 0;
5579                 filter->queue = (etqs & IXGBE_ETQS_RX_QUEUE) >>
5580                                IXGBE_ETQS_RX_QUEUE_SHIFT;
5581                 return 0;
5582         }
5583         return -ENOENT;
5584 }
5585
5586 /*
5587  * ixgbe_ethertype_filter_handle - Handle operations for ethertype filter.
5588  * @dev: pointer to rte_eth_dev structure
5589  * @filter_op:operation will be taken.
5590  * @arg: a pointer to specific structure corresponding to the filter_op
5591  */
5592 static int
5593 ixgbe_ethertype_filter_handle(struct rte_eth_dev *dev,
5594                                 enum rte_filter_op filter_op,
5595                                 void *arg)
5596 {
5597         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
5598         int ret;
5599
5600         MAC_TYPE_FILTER_SUP(hw->mac.type);
5601
5602         if (filter_op == RTE_ETH_FILTER_NOP)
5603                 return 0;
5604
5605         if (arg == NULL) {
5606                 PMD_DRV_LOG(ERR, "arg shouldn't be NULL for operation %u.",
5607                             filter_op);
5608                 return -EINVAL;
5609         }
5610
5611         switch (filter_op) {
5612         case RTE_ETH_FILTER_ADD:
5613                 ret = ixgbe_add_del_ethertype_filter(dev,
5614                         (struct rte_eth_ethertype_filter *)arg,
5615                         TRUE);
5616                 break;
5617         case RTE_ETH_FILTER_DELETE:
5618                 ret = ixgbe_add_del_ethertype_filter(dev,
5619                         (struct rte_eth_ethertype_filter *)arg,
5620                         FALSE);
5621                 break;
5622         case RTE_ETH_FILTER_GET:
5623                 ret = ixgbe_get_ethertype_filter(dev,
5624                         (struct rte_eth_ethertype_filter *)arg);
5625                 break;
5626         default:
5627                 PMD_DRV_LOG(ERR, "unsupported operation %u.", filter_op);
5628                 ret = -EINVAL;
5629                 break;
5630         }
5631         return ret;
5632 }
5633
5634 static int
5635 ixgbe_dev_filter_ctrl(struct rte_eth_dev *dev,
5636                      enum rte_filter_type filter_type,
5637                      enum rte_filter_op filter_op,
5638                      void *arg)
5639 {
5640         int ret = -EINVAL;
5641
5642         switch (filter_type) {
5643         case RTE_ETH_FILTER_NTUPLE:
5644                 ret = ixgbe_ntuple_filter_handle(dev, filter_op, arg);
5645                 break;
5646         case RTE_ETH_FILTER_ETHERTYPE:
5647                 ret = ixgbe_ethertype_filter_handle(dev, filter_op, arg);
5648                 break;
5649         case RTE_ETH_FILTER_SYN:
5650                 ret = ixgbe_syn_filter_handle(dev, filter_op, arg);
5651                 break;
5652         case RTE_ETH_FILTER_FDIR:
5653                 ret = ixgbe_fdir_ctrl_func(dev, filter_op, arg);
5654                 break;
5655         case RTE_ETH_FILTER_L2_TUNNEL:
5656                 ret = ixgbe_dev_l2_tunnel_filter_handle(dev, filter_op, arg);
5657                 break;
5658         default:
5659                 PMD_DRV_LOG(WARNING, "Filter type (%d) not supported",
5660                                                         filter_type);
5661                 break;
5662         }
5663
5664         return ret;
5665 }
5666
5667 static u8 *
5668 ixgbe_dev_addr_list_itr(__attribute__((unused)) struct ixgbe_hw *hw,
5669                         u8 **mc_addr_ptr, u32 *vmdq)
5670 {
5671         u8 *mc_addr;
5672
5673         *vmdq = 0;
5674         mc_addr = *mc_addr_ptr;
5675         *mc_addr_ptr = (mc_addr + sizeof(struct ether_addr));
5676         return mc_addr;
5677 }
5678
5679 static int
5680 ixgbe_dev_set_mc_addr_list(struct rte_eth_dev *dev,
5681                           struct ether_addr *mc_addr_set,
5682                           uint32_t nb_mc_addr)
5683 {
5684         struct ixgbe_hw *hw;
5685         u8 *mc_addr_list;
5686
5687         hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
5688         mc_addr_list = (u8 *)mc_addr_set;
5689         return ixgbe_update_mc_addr_list(hw, mc_addr_list, nb_mc_addr,
5690                                          ixgbe_dev_addr_list_itr, TRUE);
5691 }
5692
5693 static uint64_t
5694 ixgbe_read_systime_cyclecounter(struct rte_eth_dev *dev)
5695 {
5696         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
5697         uint64_t systime_cycles;
5698
5699         switch (hw->mac.type) {
5700         case ixgbe_mac_X550:
5701                 /* SYSTIMEL stores ns and SYSTIMEH stores seconds. */
5702                 systime_cycles = (uint64_t)IXGBE_READ_REG(hw, IXGBE_SYSTIML);
5703                 systime_cycles += (uint64_t)IXGBE_READ_REG(hw, IXGBE_SYSTIMH)
5704                                 * NSEC_PER_SEC;
5705                 break;
5706         default:
5707                 systime_cycles = (uint64_t)IXGBE_READ_REG(hw, IXGBE_SYSTIML);
5708                 systime_cycles |= (uint64_t)IXGBE_READ_REG(hw, IXGBE_SYSTIMH)
5709                                 << 32;
5710         }
5711
5712         return systime_cycles;
5713 }
5714
5715 static uint64_t
5716 ixgbe_read_rx_tstamp_cyclecounter(struct rte_eth_dev *dev)
5717 {
5718         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
5719         uint64_t rx_tstamp_cycles;
5720
5721         switch (hw->mac.type) {
5722         case ixgbe_mac_X550:
5723                 /* RXSTMPL stores ns and RXSTMPH stores seconds. */
5724                 rx_tstamp_cycles = (uint64_t)IXGBE_READ_REG(hw, IXGBE_RXSTMPL);
5725                 rx_tstamp_cycles += (uint64_t)IXGBE_READ_REG(hw, IXGBE_RXSTMPH)
5726                                 * NSEC_PER_SEC;
5727                 break;
5728         default:
5729                 /* RXSTMPL stores ns and RXSTMPH stores seconds. */
5730                 rx_tstamp_cycles = (uint64_t)IXGBE_READ_REG(hw, IXGBE_RXSTMPL);
5731                 rx_tstamp_cycles |= (uint64_t)IXGBE_READ_REG(hw, IXGBE_RXSTMPH)
5732                                 << 32;
5733         }
5734
5735         return rx_tstamp_cycles;
5736 }
5737
5738 static uint64_t
5739 ixgbe_read_tx_tstamp_cyclecounter(struct rte_eth_dev *dev)
5740 {
5741         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
5742         uint64_t tx_tstamp_cycles;
5743
5744         switch (hw->mac.type) {
5745         case ixgbe_mac_X550:
5746                 /* TXSTMPL stores ns and TXSTMPH stores seconds. */
5747                 tx_tstamp_cycles = (uint64_t)IXGBE_READ_REG(hw, IXGBE_TXSTMPL);
5748                 tx_tstamp_cycles += (uint64_t)IXGBE_READ_REG(hw, IXGBE_TXSTMPH)
5749                                 * NSEC_PER_SEC;
5750                 break;
5751         default:
5752                 /* TXSTMPL stores ns and TXSTMPH stores seconds. */
5753                 tx_tstamp_cycles = (uint64_t)IXGBE_READ_REG(hw, IXGBE_TXSTMPL);
5754                 tx_tstamp_cycles |= (uint64_t)IXGBE_READ_REG(hw, IXGBE_TXSTMPH)
5755                                 << 32;
5756         }
5757
5758         return tx_tstamp_cycles;
5759 }
5760
5761 static void
5762 ixgbe_start_timecounters(struct rte_eth_dev *dev)
5763 {
5764         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
5765         struct ixgbe_adapter *adapter =
5766                 (struct ixgbe_adapter *)dev->data->dev_private;
5767         struct rte_eth_link link;
5768         uint32_t incval = 0;
5769         uint32_t shift = 0;
5770
5771         /* Get current link speed. */
5772         memset(&link, 0, sizeof(link));
5773         ixgbe_dev_link_update(dev, 1);
5774         rte_ixgbe_dev_atomic_read_link_status(dev, &link);
5775
5776         switch (link.link_speed) {
5777         case ETH_LINK_SPEED_100:
5778                 incval = IXGBE_INCVAL_100;
5779                 shift = IXGBE_INCVAL_SHIFT_100;
5780                 break;
5781         case ETH_LINK_SPEED_1000:
5782                 incval = IXGBE_INCVAL_1GB;
5783                 shift = IXGBE_INCVAL_SHIFT_1GB;
5784                 break;
5785         case ETH_LINK_SPEED_10000:
5786         default:
5787                 incval = IXGBE_INCVAL_10GB;
5788                 shift = IXGBE_INCVAL_SHIFT_10GB;
5789                 break;
5790         }
5791
5792         switch (hw->mac.type) {
5793         case ixgbe_mac_X550:
5794                 /* Independent of link speed. */
5795                 incval = 1;
5796                 /* Cycles read will be interpreted as ns. */
5797                 shift = 0;
5798                 /* Fall-through */
5799         case ixgbe_mac_X540:
5800                 IXGBE_WRITE_REG(hw, IXGBE_TIMINCA, incval);
5801                 break;
5802         case ixgbe_mac_82599EB:
5803                 incval >>= IXGBE_INCVAL_SHIFT_82599;
5804                 shift -= IXGBE_INCVAL_SHIFT_82599;
5805                 IXGBE_WRITE_REG(hw, IXGBE_TIMINCA,
5806                                 (1 << IXGBE_INCPER_SHIFT_82599) | incval);
5807                 break;
5808         default:
5809                 /* Not supported. */
5810                 return;
5811         }
5812
5813         memset(&adapter->systime_tc, 0, sizeof(struct rte_timecounter));
5814         memset(&adapter->rx_tstamp_tc, 0, sizeof(struct rte_timecounter));
5815         memset(&adapter->tx_tstamp_tc, 0, sizeof(struct rte_timecounter));
5816
5817         adapter->systime_tc.cc_mask = IXGBE_CYCLECOUNTER_MASK;
5818         adapter->systime_tc.cc_shift = shift;
5819         adapter->systime_tc.nsec_mask = (1ULL << shift) - 1;
5820
5821         adapter->rx_tstamp_tc.cc_mask = IXGBE_CYCLECOUNTER_MASK;
5822         adapter->rx_tstamp_tc.cc_shift = shift;
5823         adapter->rx_tstamp_tc.nsec_mask = (1ULL << shift) - 1;
5824
5825         adapter->tx_tstamp_tc.cc_mask = IXGBE_CYCLECOUNTER_MASK;
5826         adapter->tx_tstamp_tc.cc_shift = shift;
5827         adapter->tx_tstamp_tc.nsec_mask = (1ULL << shift) - 1;
5828 }
5829
5830 static int
5831 ixgbe_timesync_adjust_time(struct rte_eth_dev *dev, int64_t delta)
5832 {
5833         struct ixgbe_adapter *adapter =
5834                         (struct ixgbe_adapter *)dev->data->dev_private;
5835
5836         adapter->systime_tc.nsec += delta;
5837         adapter->rx_tstamp_tc.nsec += delta;
5838         adapter->tx_tstamp_tc.nsec += delta;
5839
5840         return 0;
5841 }
5842
5843 static int
5844 ixgbe_timesync_write_time(struct rte_eth_dev *dev, const struct timespec *ts)
5845 {
5846         uint64_t ns;
5847         struct ixgbe_adapter *adapter =
5848                         (struct ixgbe_adapter *)dev->data->dev_private;
5849
5850         ns = rte_timespec_to_ns(ts);
5851         /* Set the timecounters to a new value. */
5852         adapter->systime_tc.nsec = ns;
5853         adapter->rx_tstamp_tc.nsec = ns;
5854         adapter->tx_tstamp_tc.nsec = ns;
5855
5856         return 0;
5857 }
5858
5859 static int
5860 ixgbe_timesync_read_time(struct rte_eth_dev *dev, struct timespec *ts)
5861 {
5862         uint64_t ns, systime_cycles;
5863         struct ixgbe_adapter *adapter =
5864                         (struct ixgbe_adapter *)dev->data->dev_private;
5865
5866         systime_cycles = ixgbe_read_systime_cyclecounter(dev);
5867         ns = rte_timecounter_update(&adapter->systime_tc, systime_cycles);
5868         *ts = rte_ns_to_timespec(ns);
5869
5870         return 0;
5871 }
5872
5873 static int
5874 ixgbe_timesync_enable(struct rte_eth_dev *dev)
5875 {
5876         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
5877         uint32_t tsync_ctl;
5878         uint32_t tsauxc;
5879
5880         /* Stop the timesync system time. */
5881         IXGBE_WRITE_REG(hw, IXGBE_TIMINCA, 0x0);
5882         /* Reset the timesync system time value. */
5883         IXGBE_WRITE_REG(hw, IXGBE_SYSTIML, 0x0);
5884         IXGBE_WRITE_REG(hw, IXGBE_SYSTIMH, 0x0);
5885
5886         /* Enable system time for platforms where it isn't on by default. */
5887         tsauxc = IXGBE_READ_REG(hw, IXGBE_TSAUXC);
5888         tsauxc &= ~IXGBE_TSAUXC_DISABLE_SYSTIME;
5889         IXGBE_WRITE_REG(hw, IXGBE_TSAUXC, tsauxc);
5890
5891         ixgbe_start_timecounters(dev);
5892
5893         /* Enable L2 filtering of IEEE1588/802.1AS Ethernet frame types. */
5894         IXGBE_WRITE_REG(hw, IXGBE_ETQF(IXGBE_ETQF_FILTER_1588),
5895                         (ETHER_TYPE_1588 |
5896                          IXGBE_ETQF_FILTER_EN |
5897                          IXGBE_ETQF_1588));
5898
5899         /* Enable timestamping of received PTP packets. */
5900         tsync_ctl = IXGBE_READ_REG(hw, IXGBE_TSYNCRXCTL);
5901         tsync_ctl |= IXGBE_TSYNCRXCTL_ENABLED;
5902         IXGBE_WRITE_REG(hw, IXGBE_TSYNCRXCTL, tsync_ctl);
5903
5904         /* Enable timestamping of transmitted PTP packets. */
5905         tsync_ctl = IXGBE_READ_REG(hw, IXGBE_TSYNCTXCTL);
5906         tsync_ctl |= IXGBE_TSYNCTXCTL_ENABLED;
5907         IXGBE_WRITE_REG(hw, IXGBE_TSYNCTXCTL, tsync_ctl);
5908
5909         IXGBE_WRITE_FLUSH(hw);
5910
5911         return 0;
5912 }
5913
5914 static int
5915 ixgbe_timesync_disable(struct rte_eth_dev *dev)
5916 {
5917         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
5918         uint32_t tsync_ctl;
5919
5920         /* Disable timestamping of transmitted PTP packets. */
5921         tsync_ctl = IXGBE_READ_REG(hw, IXGBE_TSYNCTXCTL);
5922         tsync_ctl &= ~IXGBE_TSYNCTXCTL_ENABLED;
5923         IXGBE_WRITE_REG(hw, IXGBE_TSYNCTXCTL, tsync_ctl);
5924
5925         /* Disable timestamping of received PTP packets. */
5926         tsync_ctl = IXGBE_READ_REG(hw, IXGBE_TSYNCRXCTL);
5927         tsync_ctl &= ~IXGBE_TSYNCRXCTL_ENABLED;
5928         IXGBE_WRITE_REG(hw, IXGBE_TSYNCRXCTL, tsync_ctl);
5929
5930         /* Disable L2 filtering of IEEE1588/802.1AS Ethernet frame types. */
5931         IXGBE_WRITE_REG(hw, IXGBE_ETQF(IXGBE_ETQF_FILTER_1588), 0);
5932
5933         /* Stop incrementating the System Time registers. */
5934         IXGBE_WRITE_REG(hw, IXGBE_TIMINCA, 0);
5935
5936         return 0;
5937 }
5938
5939 static int
5940 ixgbe_timesync_read_rx_timestamp(struct rte_eth_dev *dev,
5941                                  struct timespec *timestamp,
5942                                  uint32_t flags __rte_unused)
5943 {
5944         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
5945         struct ixgbe_adapter *adapter =
5946                 (struct ixgbe_adapter *)dev->data->dev_private;
5947         uint32_t tsync_rxctl;
5948         uint64_t rx_tstamp_cycles;
5949         uint64_t ns;
5950
5951         tsync_rxctl = IXGBE_READ_REG(hw, IXGBE_TSYNCRXCTL);
5952         if ((tsync_rxctl & IXGBE_TSYNCRXCTL_VALID) == 0)
5953                 return -EINVAL;
5954
5955         rx_tstamp_cycles = ixgbe_read_rx_tstamp_cyclecounter(dev);
5956         ns = rte_timecounter_update(&adapter->rx_tstamp_tc, rx_tstamp_cycles);
5957         *timestamp = rte_ns_to_timespec(ns);
5958
5959         return  0;
5960 }
5961
5962 static int
5963 ixgbe_timesync_read_tx_timestamp(struct rte_eth_dev *dev,
5964                                  struct timespec *timestamp)
5965 {
5966         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
5967         struct ixgbe_adapter *adapter =
5968                 (struct ixgbe_adapter *)dev->data->dev_private;
5969         uint32_t tsync_txctl;
5970         uint64_t tx_tstamp_cycles;
5971         uint64_t ns;
5972
5973         tsync_txctl = IXGBE_READ_REG(hw, IXGBE_TSYNCTXCTL);
5974         if ((tsync_txctl & IXGBE_TSYNCTXCTL_VALID) == 0)
5975                 return -EINVAL;
5976
5977         tx_tstamp_cycles = ixgbe_read_tx_tstamp_cyclecounter(dev);
5978         ns = rte_timecounter_update(&adapter->tx_tstamp_tc, tx_tstamp_cycles);
5979         *timestamp = rte_ns_to_timespec(ns);
5980
5981         return 0;
5982 }
5983
5984 static int
5985 ixgbe_get_reg_length(struct rte_eth_dev *dev)
5986 {
5987         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
5988         int count = 0;
5989         int g_ind = 0;
5990         const struct reg_info *reg_group;
5991         const struct reg_info **reg_set = (hw->mac.type == ixgbe_mac_82598EB) ?
5992                                     ixgbe_regs_mac_82598EB : ixgbe_regs_others;
5993
5994         while ((reg_group = reg_set[g_ind++]))
5995                 count += ixgbe_regs_group_count(reg_group);
5996
5997         return count;
5998 }
5999
6000 static int
6001 ixgbevf_get_reg_length(struct rte_eth_dev *dev __rte_unused)
6002 {
6003         int count = 0;
6004         int g_ind = 0;
6005         const struct reg_info *reg_group;
6006
6007         while ((reg_group = ixgbevf_regs[g_ind++]))
6008                 count += ixgbe_regs_group_count(reg_group);
6009
6010         return count;
6011 }
6012
6013 static int
6014 ixgbe_get_regs(struct rte_eth_dev *dev,
6015               struct rte_dev_reg_info *regs)
6016 {
6017         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
6018         uint32_t *data = regs->data;
6019         int g_ind = 0;
6020         int count = 0;
6021         const struct reg_info *reg_group;
6022         const struct reg_info **reg_set = (hw->mac.type == ixgbe_mac_82598EB) ?
6023                                     ixgbe_regs_mac_82598EB : ixgbe_regs_others;
6024
6025         /* Support only full register dump */
6026         if ((regs->length == 0) ||
6027             (regs->length == (uint32_t)ixgbe_get_reg_length(dev))) {
6028                 regs->version = hw->mac.type << 24 | hw->revision_id << 16 |
6029                         hw->device_id;
6030                 while ((reg_group = reg_set[g_ind++]))
6031                         count += ixgbe_read_regs_group(dev, &data[count],
6032                                 reg_group);
6033                 return 0;
6034         }
6035
6036         return -ENOTSUP;
6037 }
6038
6039 static int
6040 ixgbevf_get_regs(struct rte_eth_dev *dev,
6041                 struct rte_dev_reg_info *regs)
6042 {
6043         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
6044         uint32_t *data = regs->data;
6045         int g_ind = 0;
6046         int count = 0;
6047         const struct reg_info *reg_group;
6048
6049         /* Support only full register dump */
6050         if ((regs->length == 0) ||
6051             (regs->length == (uint32_t)ixgbevf_get_reg_length(dev))) {
6052                 regs->version = hw->mac.type << 24 | hw->revision_id << 16 |
6053                         hw->device_id;
6054                 while ((reg_group = ixgbevf_regs[g_ind++]))
6055                         count += ixgbe_read_regs_group(dev, &data[count],
6056                                                       reg_group);
6057                 return 0;
6058         }
6059
6060         return -ENOTSUP;
6061 }
6062
6063 static int
6064 ixgbe_get_eeprom_length(struct rte_eth_dev *dev)
6065 {
6066         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
6067
6068         /* Return unit is byte count */
6069         return hw->eeprom.word_size * 2;
6070 }
6071
6072 static int
6073 ixgbe_get_eeprom(struct rte_eth_dev *dev,
6074                 struct rte_dev_eeprom_info *in_eeprom)
6075 {
6076         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
6077         struct ixgbe_eeprom_info *eeprom = &hw->eeprom;
6078         uint16_t *data = in_eeprom->data;
6079         int first, length;
6080
6081         first = in_eeprom->offset >> 1;
6082         length = in_eeprom->length >> 1;
6083         if ((first > hw->eeprom.word_size) ||
6084             ((first + length) > hw->eeprom.word_size))
6085                 return -EINVAL;
6086
6087         in_eeprom->magic = hw->vendor_id | (hw->device_id << 16);
6088
6089         return eeprom->ops.read_buffer(hw, first, length, data);
6090 }
6091
6092 static int
6093 ixgbe_set_eeprom(struct rte_eth_dev *dev,
6094                 struct rte_dev_eeprom_info *in_eeprom)
6095 {
6096         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
6097         struct ixgbe_eeprom_info *eeprom = &hw->eeprom;
6098         uint16_t *data = in_eeprom->data;
6099         int first, length;
6100
6101         first = in_eeprom->offset >> 1;
6102         length = in_eeprom->length >> 1;
6103         if ((first > hw->eeprom.word_size) ||
6104             ((first + length) > hw->eeprom.word_size))
6105                 return -EINVAL;
6106
6107         in_eeprom->magic = hw->vendor_id | (hw->device_id << 16);
6108
6109         return eeprom->ops.write_buffer(hw,  first, length, data);
6110 }
6111
6112 uint16_t
6113 ixgbe_reta_size_get(enum ixgbe_mac_type mac_type) {
6114         switch (mac_type) {
6115         case ixgbe_mac_X550:
6116         case ixgbe_mac_X550EM_x:
6117                 return ETH_RSS_RETA_SIZE_512;
6118         case ixgbe_mac_X550_vf:
6119         case ixgbe_mac_X550EM_x_vf:
6120                 return ETH_RSS_RETA_SIZE_64;
6121         default:
6122                 return ETH_RSS_RETA_SIZE_128;
6123         }
6124 }
6125
6126 uint32_t
6127 ixgbe_reta_reg_get(enum ixgbe_mac_type mac_type, uint16_t reta_idx) {
6128         switch (mac_type) {
6129         case ixgbe_mac_X550:
6130         case ixgbe_mac_X550EM_x:
6131                 if (reta_idx < ETH_RSS_RETA_SIZE_128)
6132                         return IXGBE_RETA(reta_idx >> 2);
6133                 else
6134                         return IXGBE_ERETA((reta_idx - ETH_RSS_RETA_SIZE_128) >> 2);
6135         case ixgbe_mac_X550_vf:
6136         case ixgbe_mac_X550EM_x_vf:
6137                 return IXGBE_VFRETA(reta_idx >> 2);
6138         default:
6139                 return IXGBE_RETA(reta_idx >> 2);
6140         }
6141 }
6142
6143 uint32_t
6144 ixgbe_mrqc_reg_get(enum ixgbe_mac_type mac_type) {
6145         switch (mac_type) {
6146         case ixgbe_mac_X550_vf:
6147         case ixgbe_mac_X550EM_x_vf:
6148                 return IXGBE_VFMRQC;
6149         default:
6150                 return IXGBE_MRQC;
6151         }
6152 }
6153
6154 uint32_t
6155 ixgbe_rssrk_reg_get(enum ixgbe_mac_type mac_type, uint8_t i) {
6156         switch (mac_type) {
6157         case ixgbe_mac_X550_vf:
6158         case ixgbe_mac_X550EM_x_vf:
6159                 return IXGBE_VFRSSRK(i);
6160         default:
6161                 return IXGBE_RSSRK(i);
6162         }
6163 }
6164
6165 bool
6166 ixgbe_rss_update_sp(enum ixgbe_mac_type mac_type) {
6167         switch (mac_type) {
6168         case ixgbe_mac_82599_vf:
6169         case ixgbe_mac_X540_vf:
6170                 return 0;
6171         default:
6172                 return 1;
6173         }
6174 }
6175
6176 static int
6177 ixgbe_dev_get_dcb_info(struct rte_eth_dev *dev,
6178                         struct rte_eth_dcb_info *dcb_info)
6179 {
6180         struct ixgbe_dcb_config *dcb_config =
6181                         IXGBE_DEV_PRIVATE_TO_DCB_CFG(dev->data->dev_private);
6182         struct ixgbe_dcb_tc_config *tc;
6183         uint8_t i, j;
6184
6185         if (dev->data->dev_conf.rxmode.mq_mode & ETH_MQ_RX_DCB_FLAG)
6186                 dcb_info->nb_tcs = dcb_config->num_tcs.pg_tcs;
6187         else
6188                 dcb_info->nb_tcs = 1;
6189
6190         if (dcb_config->vt_mode) { /* vt is enabled*/
6191                 struct rte_eth_vmdq_dcb_conf *vmdq_rx_conf =
6192                                 &dev->data->dev_conf.rx_adv_conf.vmdq_dcb_conf;
6193                 for (i = 0; i < ETH_DCB_NUM_USER_PRIORITIES; i++)
6194                         dcb_info->prio_tc[i] = vmdq_rx_conf->dcb_tc[i];
6195                 for (i = 0; i < vmdq_rx_conf->nb_queue_pools; i++) {
6196                         for (j = 0; j < dcb_info->nb_tcs; j++) {
6197                                 dcb_info->tc_queue.tc_rxq[i][j].base =
6198                                                 i * dcb_info->nb_tcs + j;
6199                                 dcb_info->tc_queue.tc_rxq[i][j].nb_queue = 1;
6200                                 dcb_info->tc_queue.tc_txq[i][j].base =
6201                                                 i * dcb_info->nb_tcs + j;
6202                                 dcb_info->tc_queue.tc_txq[i][j].nb_queue = 1;
6203                         }
6204                 }
6205         } else { /* vt is disabled*/
6206                 struct rte_eth_dcb_rx_conf *rx_conf =
6207                                 &dev->data->dev_conf.rx_adv_conf.dcb_rx_conf;
6208                 for (i = 0; i < ETH_DCB_NUM_USER_PRIORITIES; i++)
6209                         dcb_info->prio_tc[i] = rx_conf->dcb_tc[i];
6210                 if (dcb_info->nb_tcs == ETH_4_TCS) {
6211                         for (i = 0; i < dcb_info->nb_tcs; i++) {
6212                                 dcb_info->tc_queue.tc_rxq[0][i].base = i * 32;
6213                                 dcb_info->tc_queue.tc_rxq[0][i].nb_queue = 16;
6214                         }
6215                         dcb_info->tc_queue.tc_txq[0][0].base = 0;
6216                         dcb_info->tc_queue.tc_txq[0][1].base = 64;
6217                         dcb_info->tc_queue.tc_txq[0][2].base = 96;
6218                         dcb_info->tc_queue.tc_txq[0][3].base = 112;
6219                         dcb_info->tc_queue.tc_txq[0][0].nb_queue = 64;
6220                         dcb_info->tc_queue.tc_txq[0][1].nb_queue = 32;
6221                         dcb_info->tc_queue.tc_txq[0][2].nb_queue = 16;
6222                         dcb_info->tc_queue.tc_txq[0][3].nb_queue = 16;
6223                 } else if (dcb_info->nb_tcs == ETH_8_TCS) {
6224                         for (i = 0; i < dcb_info->nb_tcs; i++) {
6225                                 dcb_info->tc_queue.tc_rxq[0][i].base = i * 16;
6226                                 dcb_info->tc_queue.tc_rxq[0][i].nb_queue = 16;
6227                         }
6228                         dcb_info->tc_queue.tc_txq[0][0].base = 0;
6229                         dcb_info->tc_queue.tc_txq[0][1].base = 32;
6230                         dcb_info->tc_queue.tc_txq[0][2].base = 64;
6231                         dcb_info->tc_queue.tc_txq[0][3].base = 80;
6232                         dcb_info->tc_queue.tc_txq[0][4].base = 96;
6233                         dcb_info->tc_queue.tc_txq[0][5].base = 104;
6234                         dcb_info->tc_queue.tc_txq[0][6].base = 112;
6235                         dcb_info->tc_queue.tc_txq[0][7].base = 120;
6236                         dcb_info->tc_queue.tc_txq[0][0].nb_queue = 32;
6237                         dcb_info->tc_queue.tc_txq[0][1].nb_queue = 32;
6238                         dcb_info->tc_queue.tc_txq[0][2].nb_queue = 16;
6239                         dcb_info->tc_queue.tc_txq[0][3].nb_queue = 16;
6240                         dcb_info->tc_queue.tc_txq[0][4].nb_queue = 8;
6241                         dcb_info->tc_queue.tc_txq[0][5].nb_queue = 8;
6242                         dcb_info->tc_queue.tc_txq[0][6].nb_queue = 8;
6243                         dcb_info->tc_queue.tc_txq[0][7].nb_queue = 8;
6244                 }
6245         }
6246         for (i = 0; i < dcb_info->nb_tcs; i++) {
6247                 tc = &dcb_config->tc_config[i];
6248                 dcb_info->tc_bws[i] = tc->path[IXGBE_DCB_TX_CONFIG].bwg_percent;
6249         }
6250         return 0;
6251 }
6252
6253 /* Update e-tag ether type */
6254 static int
6255 ixgbe_update_e_tag_eth_type(struct ixgbe_hw *hw,
6256                             uint16_t ether_type)
6257 {
6258         uint32_t etag_etype;
6259
6260         if (hw->mac.type != ixgbe_mac_X550 &&
6261             hw->mac.type != ixgbe_mac_X550EM_x) {
6262                 return -ENOTSUP;
6263         }
6264
6265         etag_etype = IXGBE_READ_REG(hw, IXGBE_ETAG_ETYPE);
6266         etag_etype &= ~IXGBE_ETAG_ETYPE_MASK;
6267         etag_etype |= ether_type;
6268         IXGBE_WRITE_REG(hw, IXGBE_ETAG_ETYPE, etag_etype);
6269         IXGBE_WRITE_FLUSH(hw);
6270
6271         return 0;
6272 }
6273
6274 /* Config l2 tunnel ether type */
6275 static int
6276 ixgbe_dev_l2_tunnel_eth_type_conf(struct rte_eth_dev *dev,
6277                                   struct rte_eth_l2_tunnel_conf *l2_tunnel)
6278 {
6279         int ret = 0;
6280         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
6281
6282         if (l2_tunnel == NULL)
6283                 return -EINVAL;
6284
6285         switch (l2_tunnel->l2_tunnel_type) {
6286         case RTE_L2_TUNNEL_TYPE_E_TAG:
6287                 ret = ixgbe_update_e_tag_eth_type(hw, l2_tunnel->ether_type);
6288                 break;
6289         default:
6290                 PMD_DRV_LOG(ERR, "Invalid tunnel type");
6291                 ret = -EINVAL;
6292                 break;
6293         }
6294
6295         return ret;
6296 }
6297
6298 /* Enable e-tag tunnel */
6299 static int
6300 ixgbe_e_tag_enable(struct ixgbe_hw *hw)
6301 {
6302         uint32_t etag_etype;
6303
6304         if (hw->mac.type != ixgbe_mac_X550 &&
6305             hw->mac.type != ixgbe_mac_X550EM_x) {
6306                 return -ENOTSUP;
6307         }
6308
6309         etag_etype = IXGBE_READ_REG(hw, IXGBE_ETAG_ETYPE);
6310         etag_etype |= IXGBE_ETAG_ETYPE_VALID;
6311         IXGBE_WRITE_REG(hw, IXGBE_ETAG_ETYPE, etag_etype);
6312         IXGBE_WRITE_FLUSH(hw);
6313
6314         return 0;
6315 }
6316
6317 /* Enable l2 tunnel */
6318 static int
6319 ixgbe_dev_l2_tunnel_enable(struct rte_eth_dev *dev,
6320                            enum rte_eth_tunnel_type l2_tunnel_type)
6321 {
6322         int ret = 0;
6323         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
6324
6325         switch (l2_tunnel_type) {
6326         case RTE_L2_TUNNEL_TYPE_E_TAG:
6327                 ret = ixgbe_e_tag_enable(hw);
6328                 break;
6329         default:
6330                 PMD_DRV_LOG(ERR, "Invalid tunnel type");
6331                 ret = -EINVAL;
6332                 break;
6333         }
6334
6335         return ret;
6336 }
6337
6338 /* Disable e-tag tunnel */
6339 static int
6340 ixgbe_e_tag_disable(struct ixgbe_hw *hw)
6341 {
6342         uint32_t etag_etype;
6343
6344         if (hw->mac.type != ixgbe_mac_X550 &&
6345             hw->mac.type != ixgbe_mac_X550EM_x) {
6346                 return -ENOTSUP;
6347         }
6348
6349         etag_etype = IXGBE_READ_REG(hw, IXGBE_ETAG_ETYPE);
6350         etag_etype &= ~IXGBE_ETAG_ETYPE_VALID;
6351         IXGBE_WRITE_REG(hw, IXGBE_ETAG_ETYPE, etag_etype);
6352         IXGBE_WRITE_FLUSH(hw);
6353
6354         return 0;
6355 }
6356
6357 /* Disable l2 tunnel */
6358 static int
6359 ixgbe_dev_l2_tunnel_disable(struct rte_eth_dev *dev,
6360                             enum rte_eth_tunnel_type l2_tunnel_type)
6361 {
6362         int ret = 0;
6363         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
6364
6365         switch (l2_tunnel_type) {
6366         case RTE_L2_TUNNEL_TYPE_E_TAG:
6367                 ret = ixgbe_e_tag_disable(hw);
6368                 break;
6369         default:
6370                 PMD_DRV_LOG(ERR, "Invalid tunnel type");
6371                 ret = -EINVAL;
6372                 break;
6373         }
6374
6375         return ret;
6376 }
6377
6378 static int
6379 ixgbe_e_tag_filter_del(struct rte_eth_dev *dev,
6380                        struct rte_eth_l2_tunnel_conf *l2_tunnel)
6381 {
6382         int ret = 0;
6383         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
6384         uint32_t i, rar_entries;
6385         uint32_t rar_low, rar_high;
6386
6387         if (hw->mac.type != ixgbe_mac_X550 &&
6388             hw->mac.type != ixgbe_mac_X550EM_x) {
6389                 return -ENOTSUP;
6390         }
6391
6392         rar_entries = ixgbe_get_num_rx_addrs(hw);
6393
6394         for (i = 1; i < rar_entries; i++) {
6395                 rar_high = IXGBE_READ_REG(hw, IXGBE_RAH(i));
6396                 rar_low  = IXGBE_READ_REG(hw, IXGBE_RAL(i));
6397                 if ((rar_high & IXGBE_RAH_AV) &&
6398                     (rar_high & IXGBE_RAH_ADTYPE) &&
6399                     ((rar_low & IXGBE_RAL_ETAG_FILTER_MASK) ==
6400                      l2_tunnel->tunnel_id)) {
6401                         IXGBE_WRITE_REG(hw, IXGBE_RAL(i), 0);
6402                         IXGBE_WRITE_REG(hw, IXGBE_RAH(i), 0);
6403
6404                         ixgbe_clear_vmdq(hw, i, IXGBE_CLEAR_VMDQ_ALL);
6405
6406                         return ret;
6407                 }
6408         }
6409
6410         return ret;
6411 }
6412
6413 static int
6414 ixgbe_e_tag_filter_add(struct rte_eth_dev *dev,
6415                        struct rte_eth_l2_tunnel_conf *l2_tunnel)
6416 {
6417         int ret = 0;
6418         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
6419         uint32_t i, rar_entries;
6420         uint32_t rar_low, rar_high;
6421
6422         if (hw->mac.type != ixgbe_mac_X550 &&
6423             hw->mac.type != ixgbe_mac_X550EM_x) {
6424                 return -ENOTSUP;
6425         }
6426
6427         /* One entry for one tunnel. Try to remove potential existing entry. */
6428         ixgbe_e_tag_filter_del(dev, l2_tunnel);
6429
6430         rar_entries = ixgbe_get_num_rx_addrs(hw);
6431
6432         for (i = 1; i < rar_entries; i++) {
6433                 rar_high = IXGBE_READ_REG(hw, IXGBE_RAH(i));
6434                 if (rar_high & IXGBE_RAH_AV) {
6435                         continue;
6436                 } else {
6437                         ixgbe_set_vmdq(hw, i, l2_tunnel->pool);
6438                         rar_high = IXGBE_RAH_AV | IXGBE_RAH_ADTYPE;
6439                         rar_low = l2_tunnel->tunnel_id;
6440
6441                         IXGBE_WRITE_REG(hw, IXGBE_RAL(i), rar_low);
6442                         IXGBE_WRITE_REG(hw, IXGBE_RAH(i), rar_high);
6443
6444                         return ret;
6445                 }
6446         }
6447
6448         PMD_INIT_LOG(NOTICE, "The table of E-tag forwarding rule is full."
6449                      " Please remove a rule before adding a new one.");
6450         return -EINVAL;
6451 }
6452
6453 /* Add l2 tunnel filter */
6454 static int
6455 ixgbe_dev_l2_tunnel_filter_add(struct rte_eth_dev *dev,
6456                                struct rte_eth_l2_tunnel_conf *l2_tunnel)
6457 {
6458         int ret = 0;
6459
6460         switch (l2_tunnel->l2_tunnel_type) {
6461         case RTE_L2_TUNNEL_TYPE_E_TAG:
6462                 ret = ixgbe_e_tag_filter_add(dev, l2_tunnel);
6463                 break;
6464         default:
6465                 PMD_DRV_LOG(ERR, "Invalid tunnel type");
6466                 ret = -EINVAL;
6467                 break;
6468         }
6469
6470         return ret;
6471 }
6472
6473 /* Delete l2 tunnel filter */
6474 static int
6475 ixgbe_dev_l2_tunnel_filter_del(struct rte_eth_dev *dev,
6476                                struct rte_eth_l2_tunnel_conf *l2_tunnel)
6477 {
6478         int ret = 0;
6479
6480         switch (l2_tunnel->l2_tunnel_type) {
6481         case RTE_L2_TUNNEL_TYPE_E_TAG:
6482                 ret = ixgbe_e_tag_filter_del(dev, l2_tunnel);
6483                 break;
6484         default:
6485                 PMD_DRV_LOG(ERR, "Invalid tunnel type");
6486                 ret = -EINVAL;
6487                 break;
6488         }
6489
6490         return ret;
6491 }
6492
6493 /**
6494  * ixgbe_dev_l2_tunnel_filter_handle - Handle operations for l2 tunnel filter.
6495  * @dev: pointer to rte_eth_dev structure
6496  * @filter_op:operation will be taken.
6497  * @arg: a pointer to specific structure corresponding to the filter_op
6498  */
6499 static int
6500 ixgbe_dev_l2_tunnel_filter_handle(struct rte_eth_dev *dev,
6501                                   enum rte_filter_op filter_op,
6502                                   void *arg)
6503 {
6504         int ret = 0;
6505
6506         if (filter_op == RTE_ETH_FILTER_NOP)
6507                 return 0;
6508
6509         if (arg == NULL) {
6510                 PMD_DRV_LOG(ERR, "arg shouldn't be NULL for operation %u.",
6511                             filter_op);
6512                 return -EINVAL;
6513         }
6514
6515         switch (filter_op) {
6516         case RTE_ETH_FILTER_ADD:
6517                 ret = ixgbe_dev_l2_tunnel_filter_add
6518                         (dev,
6519                          (struct rte_eth_l2_tunnel_conf *)arg);
6520                 break;
6521         case RTE_ETH_FILTER_DELETE:
6522                 ret = ixgbe_dev_l2_tunnel_filter_del
6523                         (dev,
6524                          (struct rte_eth_l2_tunnel_conf *)arg);
6525                 break;
6526         default:
6527                 PMD_DRV_LOG(ERR, "unsupported operation %u.", filter_op);
6528                 ret = -EINVAL;
6529                 break;
6530         }
6531         return ret;
6532 }
6533
6534 static int
6535 ixgbe_e_tag_forwarding_en_dis(struct rte_eth_dev *dev, bool en)
6536 {
6537         int ret = 0;
6538         uint32_t ctrl;
6539         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
6540
6541         if (hw->mac.type != ixgbe_mac_X550 &&
6542             hw->mac.type != ixgbe_mac_X550EM_x) {
6543                 return -ENOTSUP;
6544         }
6545
6546         ctrl = IXGBE_READ_REG(hw, IXGBE_VT_CTL);
6547         ctrl &= ~IXGBE_VT_CTL_POOLING_MODE_MASK;
6548         if (en)
6549                 ctrl |= IXGBE_VT_CTL_POOLING_MODE_ETAG;
6550         IXGBE_WRITE_REG(hw, IXGBE_VT_CTL, ctrl);
6551
6552         return ret;
6553 }
6554
6555 /* Enable l2 tunnel forwarding */
6556 static int
6557 ixgbe_dev_l2_tunnel_forwarding_enable
6558         (struct rte_eth_dev *dev,
6559          enum rte_eth_tunnel_type l2_tunnel_type)
6560 {
6561         int ret = 0;
6562
6563         switch (l2_tunnel_type) {
6564         case RTE_L2_TUNNEL_TYPE_E_TAG:
6565                 ret = ixgbe_e_tag_forwarding_en_dis(dev, 1);
6566                 break;
6567         default:
6568                 PMD_DRV_LOG(ERR, "Invalid tunnel type");
6569                 ret = -EINVAL;
6570                 break;
6571         }
6572
6573         return ret;
6574 }
6575
6576 /* Disable l2 tunnel forwarding */
6577 static int
6578 ixgbe_dev_l2_tunnel_forwarding_disable
6579         (struct rte_eth_dev *dev,
6580          enum rte_eth_tunnel_type l2_tunnel_type)
6581 {
6582         int ret = 0;
6583
6584         switch (l2_tunnel_type) {
6585         case RTE_L2_TUNNEL_TYPE_E_TAG:
6586                 ret = ixgbe_e_tag_forwarding_en_dis(dev, 0);
6587                 break;
6588         default:
6589                 PMD_DRV_LOG(ERR, "Invalid tunnel type");
6590                 ret = -EINVAL;
6591                 break;
6592         }
6593
6594         return ret;
6595 }
6596
6597 static int
6598 ixgbe_e_tag_insertion_en_dis(struct rte_eth_dev *dev,
6599                              struct rte_eth_l2_tunnel_conf *l2_tunnel,
6600                              bool en)
6601 {
6602         int ret = 0;
6603         uint32_t vmtir, vmvir;
6604         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
6605
6606         if (l2_tunnel->vf_id >= dev->pci_dev->max_vfs) {
6607                 PMD_DRV_LOG(ERR,
6608                             "VF id %u should be less than %u",
6609                             l2_tunnel->vf_id,
6610                             dev->pci_dev->max_vfs);
6611                 return -EINVAL;
6612         }
6613
6614         if (hw->mac.type != ixgbe_mac_X550 &&
6615             hw->mac.type != ixgbe_mac_X550EM_x) {
6616                 return -ENOTSUP;
6617         }
6618
6619         if (en)
6620                 vmtir = l2_tunnel->tunnel_id;
6621         else
6622                 vmtir = 0;
6623
6624         IXGBE_WRITE_REG(hw, IXGBE_VMTIR(l2_tunnel->vf_id), vmtir);
6625
6626         vmvir = IXGBE_READ_REG(hw, IXGBE_VMVIR(l2_tunnel->vf_id));
6627         vmvir &= ~IXGBE_VMVIR_TAGA_MASK;
6628         if (en)
6629                 vmvir |= IXGBE_VMVIR_TAGA_ETAG_INSERT;
6630         IXGBE_WRITE_REG(hw, IXGBE_VMVIR(l2_tunnel->vf_id), vmvir);
6631
6632         return ret;
6633 }
6634
6635 /* Enable l2 tunnel tag insertion */
6636 static int
6637 ixgbe_dev_l2_tunnel_insertion_enable(struct rte_eth_dev *dev,
6638                                      struct rte_eth_l2_tunnel_conf *l2_tunnel)
6639 {
6640         int ret = 0;
6641
6642         switch (l2_tunnel->l2_tunnel_type) {
6643         case RTE_L2_TUNNEL_TYPE_E_TAG:
6644                 ret = ixgbe_e_tag_insertion_en_dis(dev, l2_tunnel, 1);
6645                 break;
6646         default:
6647                 PMD_DRV_LOG(ERR, "Invalid tunnel type");
6648                 ret = -EINVAL;
6649                 break;
6650         }
6651
6652         return ret;
6653 }
6654
6655 /* Disable l2 tunnel tag insertion */
6656 static int
6657 ixgbe_dev_l2_tunnel_insertion_disable
6658         (struct rte_eth_dev *dev,
6659          struct rte_eth_l2_tunnel_conf *l2_tunnel)
6660 {
6661         int ret = 0;
6662
6663         switch (l2_tunnel->l2_tunnel_type) {
6664         case RTE_L2_TUNNEL_TYPE_E_TAG:
6665                 ret = ixgbe_e_tag_insertion_en_dis(dev, l2_tunnel, 0);
6666                 break;
6667         default:
6668                 PMD_DRV_LOG(ERR, "Invalid tunnel type");
6669                 ret = -EINVAL;
6670                 break;
6671         }
6672
6673         return ret;
6674 }
6675
6676 static int
6677 ixgbe_e_tag_stripping_en_dis(struct rte_eth_dev *dev,
6678                              bool en)
6679 {
6680         int ret = 0;
6681         uint32_t qde;
6682         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
6683
6684         if (hw->mac.type != ixgbe_mac_X550 &&
6685             hw->mac.type != ixgbe_mac_X550EM_x) {
6686                 return -ENOTSUP;
6687         }
6688
6689         qde = IXGBE_READ_REG(hw, IXGBE_QDE);
6690         if (en)
6691                 qde |= IXGBE_QDE_STRIP_TAG;
6692         else
6693                 qde &= ~IXGBE_QDE_STRIP_TAG;
6694         qde &= ~IXGBE_QDE_READ;
6695         qde |= IXGBE_QDE_WRITE;
6696         IXGBE_WRITE_REG(hw, IXGBE_QDE, qde);
6697
6698         return ret;
6699 }
6700
6701 /* Enable l2 tunnel tag stripping */
6702 static int
6703 ixgbe_dev_l2_tunnel_stripping_enable
6704         (struct rte_eth_dev *dev,
6705          enum rte_eth_tunnel_type l2_tunnel_type)
6706 {
6707         int ret = 0;
6708
6709         switch (l2_tunnel_type) {
6710         case RTE_L2_TUNNEL_TYPE_E_TAG:
6711                 ret = ixgbe_e_tag_stripping_en_dis(dev, 1);
6712                 break;
6713         default:
6714                 PMD_DRV_LOG(ERR, "Invalid tunnel type");
6715                 ret = -EINVAL;
6716                 break;
6717         }
6718
6719         return ret;
6720 }
6721
6722 /* Disable l2 tunnel tag stripping */
6723 static int
6724 ixgbe_dev_l2_tunnel_stripping_disable
6725         (struct rte_eth_dev *dev,
6726          enum rte_eth_tunnel_type l2_tunnel_type)
6727 {
6728         int ret = 0;
6729
6730         switch (l2_tunnel_type) {
6731         case RTE_L2_TUNNEL_TYPE_E_TAG:
6732                 ret = ixgbe_e_tag_stripping_en_dis(dev, 0);
6733                 break;
6734         default:
6735                 PMD_DRV_LOG(ERR, "Invalid tunnel type");
6736                 ret = -EINVAL;
6737                 break;
6738         }
6739
6740         return ret;
6741 }
6742
6743 /* Enable/disable l2 tunnel offload functions */
6744 static int
6745 ixgbe_dev_l2_tunnel_offload_set
6746         (struct rte_eth_dev *dev,
6747          struct rte_eth_l2_tunnel_conf *l2_tunnel,
6748          uint32_t mask,
6749          uint8_t en)
6750 {
6751         int ret = 0;
6752
6753         if (l2_tunnel == NULL)
6754                 return -EINVAL;
6755
6756         ret = -EINVAL;
6757         if (mask & ETH_L2_TUNNEL_ENABLE_MASK) {
6758                 if (en)
6759                         ret = ixgbe_dev_l2_tunnel_enable(
6760                                 dev,
6761                                 l2_tunnel->l2_tunnel_type);
6762                 else
6763                         ret = ixgbe_dev_l2_tunnel_disable(
6764                                 dev,
6765                                 l2_tunnel->l2_tunnel_type);
6766         }
6767
6768         if (mask & ETH_L2_TUNNEL_INSERTION_MASK) {
6769                 if (en)
6770                         ret = ixgbe_dev_l2_tunnel_insertion_enable(
6771                                 dev,
6772                                 l2_tunnel);
6773                 else
6774                         ret = ixgbe_dev_l2_tunnel_insertion_disable(
6775                                 dev,
6776                                 l2_tunnel);
6777         }
6778
6779         if (mask & ETH_L2_TUNNEL_STRIPPING_MASK) {
6780                 if (en)
6781                         ret = ixgbe_dev_l2_tunnel_stripping_enable(
6782                                 dev,
6783                                 l2_tunnel->l2_tunnel_type);
6784                 else
6785                         ret = ixgbe_dev_l2_tunnel_stripping_disable(
6786                                 dev,
6787                                 l2_tunnel->l2_tunnel_type);
6788         }
6789
6790         if (mask & ETH_L2_TUNNEL_FORWARDING_MASK) {
6791                 if (en)
6792                         ret = ixgbe_dev_l2_tunnel_forwarding_enable(
6793                                 dev,
6794                                 l2_tunnel->l2_tunnel_type);
6795                 else
6796                         ret = ixgbe_dev_l2_tunnel_forwarding_disable(
6797                                 dev,
6798                                 l2_tunnel->l2_tunnel_type);
6799         }
6800
6801         return ret;
6802 }
6803
6804 static int
6805 ixgbe_update_vxlan_port(struct ixgbe_hw *hw,
6806                         uint16_t port)
6807 {
6808         IXGBE_WRITE_REG(hw, IXGBE_VXLANCTRL, port);
6809         IXGBE_WRITE_FLUSH(hw);
6810
6811         return 0;
6812 }
6813
6814 /* There's only one register for VxLAN UDP port.
6815  * So, we cannot add several ports. Will update it.
6816  */
6817 static int
6818 ixgbe_add_vxlan_port(struct ixgbe_hw *hw,
6819                      uint16_t port)
6820 {
6821         if (port == 0) {
6822                 PMD_DRV_LOG(ERR, "Add VxLAN port 0 is not allowed.");
6823                 return -EINVAL;
6824         }
6825
6826         return ixgbe_update_vxlan_port(hw, port);
6827 }
6828
6829 /* We cannot delete the VxLAN port. For there's a register for VxLAN
6830  * UDP port, it must have a value.
6831  * So, will reset it to the original value 0.
6832  */
6833 static int
6834 ixgbe_del_vxlan_port(struct ixgbe_hw *hw,
6835                      uint16_t port)
6836 {
6837         uint16_t cur_port;
6838
6839         cur_port = (uint16_t)IXGBE_READ_REG(hw, IXGBE_VXLANCTRL);
6840
6841         if (cur_port != port) {
6842                 PMD_DRV_LOG(ERR, "Port %u does not exist.", port);
6843                 return -EINVAL;
6844         }
6845
6846         return ixgbe_update_vxlan_port(hw, 0);
6847 }
6848
6849 /* Add UDP tunneling port */
6850 static int
6851 ixgbe_dev_udp_tunnel_port_add(struct rte_eth_dev *dev,
6852                               struct rte_eth_udp_tunnel *udp_tunnel)
6853 {
6854         int ret = 0;
6855         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
6856
6857         if (hw->mac.type != ixgbe_mac_X550 &&
6858             hw->mac.type != ixgbe_mac_X550EM_x) {
6859                 return -ENOTSUP;
6860         }
6861
6862         if (udp_tunnel == NULL)
6863                 return -EINVAL;
6864
6865         switch (udp_tunnel->prot_type) {
6866         case RTE_TUNNEL_TYPE_VXLAN:
6867                 ret = ixgbe_add_vxlan_port(hw, udp_tunnel->udp_port);
6868                 break;
6869
6870         case RTE_TUNNEL_TYPE_GENEVE:
6871         case RTE_TUNNEL_TYPE_TEREDO:
6872                 PMD_DRV_LOG(ERR, "Tunnel type is not supported now.");
6873                 ret = -EINVAL;
6874                 break;
6875
6876         default:
6877                 PMD_DRV_LOG(ERR, "Invalid tunnel type");
6878                 ret = -EINVAL;
6879                 break;
6880         }
6881
6882         return ret;
6883 }
6884
6885 /* Remove UDP tunneling port */
6886 static int
6887 ixgbe_dev_udp_tunnel_port_del(struct rte_eth_dev *dev,
6888                               struct rte_eth_udp_tunnel *udp_tunnel)
6889 {
6890         int ret = 0;
6891         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
6892
6893         if (hw->mac.type != ixgbe_mac_X550 &&
6894             hw->mac.type != ixgbe_mac_X550EM_x) {
6895                 return -ENOTSUP;
6896         }
6897
6898         if (udp_tunnel == NULL)
6899                 return -EINVAL;
6900
6901         switch (udp_tunnel->prot_type) {
6902         case RTE_TUNNEL_TYPE_VXLAN:
6903                 ret = ixgbe_del_vxlan_port(hw, udp_tunnel->udp_port);
6904                 break;
6905         case RTE_TUNNEL_TYPE_GENEVE:
6906         case RTE_TUNNEL_TYPE_TEREDO:
6907                 PMD_DRV_LOG(ERR, "Tunnel type is not supported now.");
6908                 ret = -EINVAL;
6909                 break;
6910         default:
6911                 PMD_DRV_LOG(ERR, "Invalid tunnel type");
6912                 ret = -EINVAL;
6913                 break;
6914         }
6915
6916         return ret;
6917 }
6918
6919 static struct rte_driver rte_ixgbe_driver = {
6920         .type = PMD_PDEV,
6921         .init = rte_ixgbe_pmd_init,
6922 };
6923
6924 static struct rte_driver rte_ixgbevf_driver = {
6925         .type = PMD_PDEV,
6926         .init = rte_ixgbevf_pmd_init,
6927 };
6928
6929 PMD_REGISTER_DRIVER(rte_ixgbe_driver);
6930 PMD_REGISTER_DRIVER(rte_ixgbevf_driver);