net/igb: restore flex type filter
[dpdk.git] / drivers / net / e1000 / igb_ethdev.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2016 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 <stdarg.h>
39
40 #include <rte_common.h>
41 #include <rte_interrupts.h>
42 #include <rte_byteorder.h>
43 #include <rte_log.h>
44 #include <rte_debug.h>
45 #include <rte_pci.h>
46 #include <rte_ether.h>
47 #include <rte_ethdev.h>
48 #include <rte_ethdev_pci.h>
49 #include <rte_memory.h>
50 #include <rte_memzone.h>
51 #include <rte_eal.h>
52 #include <rte_atomic.h>
53 #include <rte_malloc.h>
54 #include <rte_dev.h>
55
56 #include "e1000_logs.h"
57 #include "base/e1000_api.h"
58 #include "e1000_ethdev.h"
59 #include "igb_regs.h"
60
61 /*
62  * Default values for port configuration
63  */
64 #define IGB_DEFAULT_RX_FREE_THRESH  32
65
66 #define IGB_DEFAULT_RX_PTHRESH      ((hw->mac.type == e1000_i354) ? 12 : 8)
67 #define IGB_DEFAULT_RX_HTHRESH      8
68 #define IGB_DEFAULT_RX_WTHRESH      ((hw->mac.type == e1000_82576) ? 1 : 4)
69
70 #define IGB_DEFAULT_TX_PTHRESH      ((hw->mac.type == e1000_i354) ? 20 : 8)
71 #define IGB_DEFAULT_TX_HTHRESH      1
72 #define IGB_DEFAULT_TX_WTHRESH      ((hw->mac.type == e1000_82576) ? 1 : 16)
73
74 #define IGB_HKEY_MAX_INDEX 10
75
76 /* Bit shift and mask */
77 #define IGB_4_BIT_WIDTH  (CHAR_BIT / 2)
78 #define IGB_4_BIT_MASK   RTE_LEN2MASK(IGB_4_BIT_WIDTH, uint8_t)
79 #define IGB_8_BIT_WIDTH  CHAR_BIT
80 #define IGB_8_BIT_MASK   UINT8_MAX
81
82 /* Additional timesync values. */
83 #define E1000_CYCLECOUNTER_MASK      0xffffffffffffffffULL
84 #define E1000_ETQF_FILTER_1588       3
85 #define IGB_82576_TSYNC_SHIFT        16
86 #define E1000_INCPERIOD_82576        (1 << E1000_TIMINCA_16NS_SHIFT)
87 #define E1000_INCVALUE_82576         (16 << IGB_82576_TSYNC_SHIFT)
88 #define E1000_TSAUXC_DISABLE_SYSTIME 0x80000000
89
90 #define E1000_VTIVAR_MISC                0x01740
91 #define E1000_VTIVAR_MISC_MASK           0xFF
92 #define E1000_VTIVAR_VALID               0x80
93 #define E1000_VTIVAR_MISC_MAILBOX        0
94 #define E1000_VTIVAR_MISC_INTR_MASK      0x3
95
96 /* External VLAN Enable bit mask */
97 #define E1000_CTRL_EXT_EXT_VLAN      (1 << 26)
98
99 /* External VLAN Ether Type bit mask and shift */
100 #define E1000_VET_VET_EXT            0xFFFF0000
101 #define E1000_VET_VET_EXT_SHIFT      16
102
103 static int  eth_igb_configure(struct rte_eth_dev *dev);
104 static int  eth_igb_start(struct rte_eth_dev *dev);
105 static void eth_igb_stop(struct rte_eth_dev *dev);
106 static int  eth_igb_dev_set_link_up(struct rte_eth_dev *dev);
107 static int  eth_igb_dev_set_link_down(struct rte_eth_dev *dev);
108 static void eth_igb_close(struct rte_eth_dev *dev);
109 static void eth_igb_promiscuous_enable(struct rte_eth_dev *dev);
110 static void eth_igb_promiscuous_disable(struct rte_eth_dev *dev);
111 static void eth_igb_allmulticast_enable(struct rte_eth_dev *dev);
112 static void eth_igb_allmulticast_disable(struct rte_eth_dev *dev);
113 static int  eth_igb_link_update(struct rte_eth_dev *dev,
114                                 int wait_to_complete);
115 static void eth_igb_stats_get(struct rte_eth_dev *dev,
116                                 struct rte_eth_stats *rte_stats);
117 static int eth_igb_xstats_get(struct rte_eth_dev *dev,
118                               struct rte_eth_xstat *xstats, unsigned n);
119 static int eth_igb_xstats_get_by_id(struct rte_eth_dev *dev,
120                 const uint64_t *ids,
121                 uint64_t *values, unsigned int n);
122 static int eth_igb_xstats_get_names(struct rte_eth_dev *dev,
123                                     struct rte_eth_xstat_name *xstats_names,
124                                     unsigned int size);
125 static int eth_igb_xstats_get_names_by_id(struct rte_eth_dev *dev,
126                 struct rte_eth_xstat_name *xstats_names, const uint64_t *ids,
127                 unsigned int limit);
128 static void eth_igb_stats_reset(struct rte_eth_dev *dev);
129 static void eth_igb_xstats_reset(struct rte_eth_dev *dev);
130 static int eth_igb_fw_version_get(struct rte_eth_dev *dev,
131                                    char *fw_version, size_t fw_size);
132 static void eth_igb_infos_get(struct rte_eth_dev *dev,
133                               struct rte_eth_dev_info *dev_info);
134 static const uint32_t *eth_igb_supported_ptypes_get(struct rte_eth_dev *dev);
135 static void eth_igbvf_infos_get(struct rte_eth_dev *dev,
136                                 struct rte_eth_dev_info *dev_info);
137 static int  eth_igb_flow_ctrl_get(struct rte_eth_dev *dev,
138                                 struct rte_eth_fc_conf *fc_conf);
139 static int  eth_igb_flow_ctrl_set(struct rte_eth_dev *dev,
140                                 struct rte_eth_fc_conf *fc_conf);
141 static int eth_igb_lsc_interrupt_setup(struct rte_eth_dev *dev);
142 static int eth_igb_rxq_interrupt_setup(struct rte_eth_dev *dev);
143 static int eth_igb_interrupt_get_status(struct rte_eth_dev *dev);
144 static int eth_igb_interrupt_action(struct rte_eth_dev *dev,
145                                     struct rte_intr_handle *handle);
146 static void eth_igb_interrupt_handler(void *param);
147 static int  igb_hardware_init(struct e1000_hw *hw);
148 static void igb_hw_control_acquire(struct e1000_hw *hw);
149 static void igb_hw_control_release(struct e1000_hw *hw);
150 static void igb_init_manageability(struct e1000_hw *hw);
151 static void igb_release_manageability(struct e1000_hw *hw);
152
153 static int  eth_igb_mtu_set(struct rte_eth_dev *dev, uint16_t mtu);
154
155 static int eth_igb_vlan_filter_set(struct rte_eth_dev *dev,
156                 uint16_t vlan_id, int on);
157 static int eth_igb_vlan_tpid_set(struct rte_eth_dev *dev,
158                                  enum rte_vlan_type vlan_type,
159                                  uint16_t tpid_id);
160 static void eth_igb_vlan_offload_set(struct rte_eth_dev *dev, int mask);
161
162 static void igb_vlan_hw_filter_enable(struct rte_eth_dev *dev);
163 static void igb_vlan_hw_filter_disable(struct rte_eth_dev *dev);
164 static void igb_vlan_hw_strip_enable(struct rte_eth_dev *dev);
165 static void igb_vlan_hw_strip_disable(struct rte_eth_dev *dev);
166 static void igb_vlan_hw_extend_enable(struct rte_eth_dev *dev);
167 static void igb_vlan_hw_extend_disable(struct rte_eth_dev *dev);
168
169 static int eth_igb_led_on(struct rte_eth_dev *dev);
170 static int eth_igb_led_off(struct rte_eth_dev *dev);
171
172 static void igb_intr_disable(struct e1000_hw *hw);
173 static int  igb_get_rx_buffer_size(struct e1000_hw *hw);
174 static int eth_igb_rar_set(struct rte_eth_dev *dev,
175                            struct ether_addr *mac_addr,
176                            uint32_t index, uint32_t pool);
177 static void eth_igb_rar_clear(struct rte_eth_dev *dev, uint32_t index);
178 static void eth_igb_default_mac_addr_set(struct rte_eth_dev *dev,
179                 struct ether_addr *addr);
180
181 static void igbvf_intr_disable(struct e1000_hw *hw);
182 static int igbvf_dev_configure(struct rte_eth_dev *dev);
183 static int igbvf_dev_start(struct rte_eth_dev *dev);
184 static void igbvf_dev_stop(struct rte_eth_dev *dev);
185 static void igbvf_dev_close(struct rte_eth_dev *dev);
186 static void igbvf_promiscuous_enable(struct rte_eth_dev *dev);
187 static void igbvf_promiscuous_disable(struct rte_eth_dev *dev);
188 static void igbvf_allmulticast_enable(struct rte_eth_dev *dev);
189 static void igbvf_allmulticast_disable(struct rte_eth_dev *dev);
190 static int eth_igbvf_link_update(struct e1000_hw *hw);
191 static void eth_igbvf_stats_get(struct rte_eth_dev *dev,
192                                 struct rte_eth_stats *rte_stats);
193 static int eth_igbvf_xstats_get(struct rte_eth_dev *dev,
194                                 struct rte_eth_xstat *xstats, unsigned n);
195 static int eth_igbvf_xstats_get_names(struct rte_eth_dev *dev,
196                                       struct rte_eth_xstat_name *xstats_names,
197                                       unsigned limit);
198 static void eth_igbvf_stats_reset(struct rte_eth_dev *dev);
199 static int igbvf_vlan_filter_set(struct rte_eth_dev *dev,
200                 uint16_t vlan_id, int on);
201 static int igbvf_set_vfta(struct e1000_hw *hw, uint16_t vid, bool on);
202 static void igbvf_set_vfta_all(struct rte_eth_dev *dev, bool on);
203 static void igbvf_default_mac_addr_set(struct rte_eth_dev *dev,
204                 struct ether_addr *addr);
205 static int igbvf_get_reg_length(struct rte_eth_dev *dev);
206 static int igbvf_get_regs(struct rte_eth_dev *dev,
207                 struct rte_dev_reg_info *regs);
208
209 static int eth_igb_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 eth_igb_rss_reta_query(struct rte_eth_dev *dev,
213                                   struct rte_eth_rss_reta_entry64 *reta_conf,
214                                   uint16_t reta_size);
215
216 static int eth_igb_syn_filter_set(struct rte_eth_dev *dev,
217                         struct rte_eth_syn_filter *filter,
218                         bool add);
219 static int eth_igb_syn_filter_get(struct rte_eth_dev *dev,
220                         struct rte_eth_syn_filter *filter);
221 static int eth_igb_syn_filter_handle(struct rte_eth_dev *dev,
222                         enum rte_filter_op filter_op,
223                         void *arg);
224 static int igb_add_2tuple_filter(struct rte_eth_dev *dev,
225                         struct rte_eth_ntuple_filter *ntuple_filter);
226 static int igb_remove_2tuple_filter(struct rte_eth_dev *dev,
227                         struct rte_eth_ntuple_filter *ntuple_filter);
228 static int eth_igb_add_del_flex_filter(struct rte_eth_dev *dev,
229                         struct rte_eth_flex_filter *filter,
230                         bool add);
231 static int eth_igb_get_flex_filter(struct rte_eth_dev *dev,
232                         struct rte_eth_flex_filter *filter);
233 static int eth_igb_flex_filter_handle(struct rte_eth_dev *dev,
234                         enum rte_filter_op filter_op,
235                         void *arg);
236 static int igb_add_5tuple_filter_82576(struct rte_eth_dev *dev,
237                         struct rte_eth_ntuple_filter *ntuple_filter);
238 static int igb_remove_5tuple_filter_82576(struct rte_eth_dev *dev,
239                         struct rte_eth_ntuple_filter *ntuple_filter);
240 static int igb_add_del_ntuple_filter(struct rte_eth_dev *dev,
241                         struct rte_eth_ntuple_filter *filter,
242                         bool add);
243 static int igb_get_ntuple_filter(struct rte_eth_dev *dev,
244                         struct rte_eth_ntuple_filter *filter);
245 static int igb_ntuple_filter_handle(struct rte_eth_dev *dev,
246                                 enum rte_filter_op filter_op,
247                                 void *arg);
248 static int igb_add_del_ethertype_filter(struct rte_eth_dev *dev,
249                         struct rte_eth_ethertype_filter *filter,
250                         bool add);
251 static int igb_ethertype_filter_handle(struct rte_eth_dev *dev,
252                                 enum rte_filter_op filter_op,
253                                 void *arg);
254 static int igb_get_ethertype_filter(struct rte_eth_dev *dev,
255                         struct rte_eth_ethertype_filter *filter);
256 static int eth_igb_filter_ctrl(struct rte_eth_dev *dev,
257                      enum rte_filter_type filter_type,
258                      enum rte_filter_op filter_op,
259                      void *arg);
260 static int eth_igb_get_reg_length(struct rte_eth_dev *dev);
261 static int eth_igb_get_regs(struct rte_eth_dev *dev,
262                 struct rte_dev_reg_info *regs);
263 static int eth_igb_get_eeprom_length(struct rte_eth_dev *dev);
264 static int eth_igb_get_eeprom(struct rte_eth_dev *dev,
265                 struct rte_dev_eeprom_info *eeprom);
266 static int eth_igb_set_eeprom(struct rte_eth_dev *dev,
267                 struct rte_dev_eeprom_info *eeprom);
268 static int eth_igb_set_mc_addr_list(struct rte_eth_dev *dev,
269                                     struct ether_addr *mc_addr_set,
270                                     uint32_t nb_mc_addr);
271 static int igb_timesync_enable(struct rte_eth_dev *dev);
272 static int igb_timesync_disable(struct rte_eth_dev *dev);
273 static int igb_timesync_read_rx_timestamp(struct rte_eth_dev *dev,
274                                           struct timespec *timestamp,
275                                           uint32_t flags);
276 static int igb_timesync_read_tx_timestamp(struct rte_eth_dev *dev,
277                                           struct timespec *timestamp);
278 static int igb_timesync_adjust_time(struct rte_eth_dev *dev, int64_t delta);
279 static int igb_timesync_read_time(struct rte_eth_dev *dev,
280                                   struct timespec *timestamp);
281 static int igb_timesync_write_time(struct rte_eth_dev *dev,
282                                    const struct timespec *timestamp);
283 static int eth_igb_rx_queue_intr_enable(struct rte_eth_dev *dev,
284                                         uint16_t queue_id);
285 static int eth_igb_rx_queue_intr_disable(struct rte_eth_dev *dev,
286                                          uint16_t queue_id);
287 static void eth_igb_assign_msix_vector(struct e1000_hw *hw, int8_t direction,
288                                        uint8_t queue, uint8_t msix_vector);
289 static void eth_igb_write_ivar(struct e1000_hw *hw, uint8_t msix_vector,
290                                uint8_t index, uint8_t offset);
291 static void eth_igb_configure_msix_intr(struct rte_eth_dev *dev);
292 static void eth_igbvf_interrupt_handler(void *param);
293 static void igbvf_mbx_process(struct rte_eth_dev *dev);
294 static int igb_filter_restore(struct rte_eth_dev *dev);
295
296 /*
297  * Define VF Stats MACRO for Non "cleared on read" register
298  */
299 #define UPDATE_VF_STAT(reg, last, cur)            \
300 {                                                 \
301         u32 latest = E1000_READ_REG(hw, reg);     \
302         cur += (latest - last) & UINT_MAX;        \
303         last = latest;                            \
304 }
305
306 #define IGB_FC_PAUSE_TIME 0x0680
307 #define IGB_LINK_UPDATE_CHECK_TIMEOUT  90  /* 9s */
308 #define IGB_LINK_UPDATE_CHECK_INTERVAL 100 /* ms */
309
310 #define IGBVF_PMD_NAME "rte_igbvf_pmd"     /* PMD name */
311
312 static enum e1000_fc_mode igb_fc_setting = e1000_fc_full;
313
314 /*
315  * The set of PCI devices this driver supports
316  */
317 static const struct rte_pci_id pci_id_igb_map[] = {
318         { RTE_PCI_DEVICE(E1000_INTEL_VENDOR_ID, E1000_DEV_ID_82576) },
319         { RTE_PCI_DEVICE(E1000_INTEL_VENDOR_ID, E1000_DEV_ID_82576_FIBER) },
320         { RTE_PCI_DEVICE(E1000_INTEL_VENDOR_ID, E1000_DEV_ID_82576_SERDES) },
321         { RTE_PCI_DEVICE(E1000_INTEL_VENDOR_ID, E1000_DEV_ID_82576_QUAD_COPPER) },
322         { RTE_PCI_DEVICE(E1000_INTEL_VENDOR_ID, E1000_DEV_ID_82576_QUAD_COPPER_ET2) },
323         { RTE_PCI_DEVICE(E1000_INTEL_VENDOR_ID, E1000_DEV_ID_82576_NS) },
324         { RTE_PCI_DEVICE(E1000_INTEL_VENDOR_ID, E1000_DEV_ID_82576_NS_SERDES) },
325         { RTE_PCI_DEVICE(E1000_INTEL_VENDOR_ID, E1000_DEV_ID_82576_SERDES_QUAD) },
326
327         { RTE_PCI_DEVICE(E1000_INTEL_VENDOR_ID, E1000_DEV_ID_82575EB_COPPER) },
328         { RTE_PCI_DEVICE(E1000_INTEL_VENDOR_ID, E1000_DEV_ID_82575EB_FIBER_SERDES) },
329         { RTE_PCI_DEVICE(E1000_INTEL_VENDOR_ID, E1000_DEV_ID_82575GB_QUAD_COPPER) },
330
331         { RTE_PCI_DEVICE(E1000_INTEL_VENDOR_ID, E1000_DEV_ID_82580_COPPER) },
332         { RTE_PCI_DEVICE(E1000_INTEL_VENDOR_ID, E1000_DEV_ID_82580_FIBER) },
333         { RTE_PCI_DEVICE(E1000_INTEL_VENDOR_ID, E1000_DEV_ID_82580_SERDES) },
334         { RTE_PCI_DEVICE(E1000_INTEL_VENDOR_ID, E1000_DEV_ID_82580_SGMII) },
335         { RTE_PCI_DEVICE(E1000_INTEL_VENDOR_ID, E1000_DEV_ID_82580_COPPER_DUAL) },
336         { RTE_PCI_DEVICE(E1000_INTEL_VENDOR_ID, E1000_DEV_ID_82580_QUAD_FIBER) },
337
338         { RTE_PCI_DEVICE(E1000_INTEL_VENDOR_ID, E1000_DEV_ID_I350_COPPER) },
339         { RTE_PCI_DEVICE(E1000_INTEL_VENDOR_ID, E1000_DEV_ID_I350_FIBER) },
340         { RTE_PCI_DEVICE(E1000_INTEL_VENDOR_ID, E1000_DEV_ID_I350_SERDES) },
341         { RTE_PCI_DEVICE(E1000_INTEL_VENDOR_ID, E1000_DEV_ID_I350_SGMII) },
342         { RTE_PCI_DEVICE(E1000_INTEL_VENDOR_ID, E1000_DEV_ID_I350_DA4) },
343         { RTE_PCI_DEVICE(E1000_INTEL_VENDOR_ID, E1000_DEV_ID_I210_COPPER) },
344         { RTE_PCI_DEVICE(E1000_INTEL_VENDOR_ID, E1000_DEV_ID_I210_COPPER_OEM1) },
345         { RTE_PCI_DEVICE(E1000_INTEL_VENDOR_ID, E1000_DEV_ID_I210_COPPER_IT) },
346         { RTE_PCI_DEVICE(E1000_INTEL_VENDOR_ID, E1000_DEV_ID_I210_FIBER) },
347         { RTE_PCI_DEVICE(E1000_INTEL_VENDOR_ID, E1000_DEV_ID_I210_SERDES) },
348         { RTE_PCI_DEVICE(E1000_INTEL_VENDOR_ID, E1000_DEV_ID_I210_SGMII) },
349         { RTE_PCI_DEVICE(E1000_INTEL_VENDOR_ID, E1000_DEV_ID_I211_COPPER) },
350         { RTE_PCI_DEVICE(E1000_INTEL_VENDOR_ID, E1000_DEV_ID_I354_BACKPLANE_1GBPS) },
351         { RTE_PCI_DEVICE(E1000_INTEL_VENDOR_ID, E1000_DEV_ID_I354_SGMII) },
352         { RTE_PCI_DEVICE(E1000_INTEL_VENDOR_ID, E1000_DEV_ID_I354_BACKPLANE_2_5GBPS) },
353         { RTE_PCI_DEVICE(E1000_INTEL_VENDOR_ID, E1000_DEV_ID_DH89XXCC_SGMII) },
354         { RTE_PCI_DEVICE(E1000_INTEL_VENDOR_ID, E1000_DEV_ID_DH89XXCC_SERDES) },
355         { RTE_PCI_DEVICE(E1000_INTEL_VENDOR_ID, E1000_DEV_ID_DH89XXCC_BACKPLANE) },
356         { RTE_PCI_DEVICE(E1000_INTEL_VENDOR_ID, E1000_DEV_ID_DH89XXCC_SFP) },
357         { .vendor_id = 0, /* sentinel */ },
358 };
359
360 /*
361  * The set of PCI devices this driver supports (for 82576&I350 VF)
362  */
363 static const struct rte_pci_id pci_id_igbvf_map[] = {
364         { RTE_PCI_DEVICE(E1000_INTEL_VENDOR_ID, E1000_DEV_ID_82576_VF) },
365         { RTE_PCI_DEVICE(E1000_INTEL_VENDOR_ID, E1000_DEV_ID_82576_VF_HV) },
366         { RTE_PCI_DEVICE(E1000_INTEL_VENDOR_ID, E1000_DEV_ID_I350_VF) },
367         { RTE_PCI_DEVICE(E1000_INTEL_VENDOR_ID, E1000_DEV_ID_I350_VF_HV) },
368         { .vendor_id = 0, /* sentinel */ },
369 };
370
371 static const struct rte_eth_desc_lim rx_desc_lim = {
372         .nb_max = E1000_MAX_RING_DESC,
373         .nb_min = E1000_MIN_RING_DESC,
374         .nb_align = IGB_RXD_ALIGN,
375 };
376
377 static const struct rte_eth_desc_lim tx_desc_lim = {
378         .nb_max = E1000_MAX_RING_DESC,
379         .nb_min = E1000_MIN_RING_DESC,
380         .nb_align = IGB_RXD_ALIGN,
381         .nb_seg_max = IGB_TX_MAX_SEG,
382         .nb_mtu_seg_max = IGB_TX_MAX_MTU_SEG,
383 };
384
385 static const struct eth_dev_ops eth_igb_ops = {
386         .dev_configure        = eth_igb_configure,
387         .dev_start            = eth_igb_start,
388         .dev_stop             = eth_igb_stop,
389         .dev_set_link_up      = eth_igb_dev_set_link_up,
390         .dev_set_link_down    = eth_igb_dev_set_link_down,
391         .dev_close            = eth_igb_close,
392         .promiscuous_enable   = eth_igb_promiscuous_enable,
393         .promiscuous_disable  = eth_igb_promiscuous_disable,
394         .allmulticast_enable  = eth_igb_allmulticast_enable,
395         .allmulticast_disable = eth_igb_allmulticast_disable,
396         .link_update          = eth_igb_link_update,
397         .stats_get            = eth_igb_stats_get,
398         .xstats_get           = eth_igb_xstats_get,
399         .xstats_get_by_id     = eth_igb_xstats_get_by_id,
400         .xstats_get_names_by_id = eth_igb_xstats_get_names_by_id,
401         .xstats_get_names     = eth_igb_xstats_get_names,
402         .stats_reset          = eth_igb_stats_reset,
403         .xstats_reset         = eth_igb_xstats_reset,
404         .fw_version_get       = eth_igb_fw_version_get,
405         .dev_infos_get        = eth_igb_infos_get,
406         .dev_supported_ptypes_get = eth_igb_supported_ptypes_get,
407         .mtu_set              = eth_igb_mtu_set,
408         .vlan_filter_set      = eth_igb_vlan_filter_set,
409         .vlan_tpid_set        = eth_igb_vlan_tpid_set,
410         .vlan_offload_set     = eth_igb_vlan_offload_set,
411         .rx_queue_setup       = eth_igb_rx_queue_setup,
412         .rx_queue_intr_enable = eth_igb_rx_queue_intr_enable,
413         .rx_queue_intr_disable = eth_igb_rx_queue_intr_disable,
414         .rx_queue_release     = eth_igb_rx_queue_release,
415         .rx_queue_count       = eth_igb_rx_queue_count,
416         .rx_descriptor_done   = eth_igb_rx_descriptor_done,
417         .rx_descriptor_status = eth_igb_rx_descriptor_status,
418         .tx_descriptor_status = eth_igb_tx_descriptor_status,
419         .tx_queue_setup       = eth_igb_tx_queue_setup,
420         .tx_queue_release     = eth_igb_tx_queue_release,
421         .tx_done_cleanup      = eth_igb_tx_done_cleanup,
422         .dev_led_on           = eth_igb_led_on,
423         .dev_led_off          = eth_igb_led_off,
424         .flow_ctrl_get        = eth_igb_flow_ctrl_get,
425         .flow_ctrl_set        = eth_igb_flow_ctrl_set,
426         .mac_addr_add         = eth_igb_rar_set,
427         .mac_addr_remove      = eth_igb_rar_clear,
428         .mac_addr_set         = eth_igb_default_mac_addr_set,
429         .reta_update          = eth_igb_rss_reta_update,
430         .reta_query           = eth_igb_rss_reta_query,
431         .rss_hash_update      = eth_igb_rss_hash_update,
432         .rss_hash_conf_get    = eth_igb_rss_hash_conf_get,
433         .filter_ctrl          = eth_igb_filter_ctrl,
434         .set_mc_addr_list     = eth_igb_set_mc_addr_list,
435         .rxq_info_get         = igb_rxq_info_get,
436         .txq_info_get         = igb_txq_info_get,
437         .timesync_enable      = igb_timesync_enable,
438         .timesync_disable     = igb_timesync_disable,
439         .timesync_read_rx_timestamp = igb_timesync_read_rx_timestamp,
440         .timesync_read_tx_timestamp = igb_timesync_read_tx_timestamp,
441         .get_reg              = eth_igb_get_regs,
442         .get_eeprom_length    = eth_igb_get_eeprom_length,
443         .get_eeprom           = eth_igb_get_eeprom,
444         .set_eeprom           = eth_igb_set_eeprom,
445         .timesync_adjust_time = igb_timesync_adjust_time,
446         .timesync_read_time   = igb_timesync_read_time,
447         .timesync_write_time  = igb_timesync_write_time,
448 };
449
450 /*
451  * dev_ops for virtual function, bare necessities for basic vf
452  * operation have been implemented
453  */
454 static const struct eth_dev_ops igbvf_eth_dev_ops = {
455         .dev_configure        = igbvf_dev_configure,
456         .dev_start            = igbvf_dev_start,
457         .dev_stop             = igbvf_dev_stop,
458         .dev_close            = igbvf_dev_close,
459         .promiscuous_enable   = igbvf_promiscuous_enable,
460         .promiscuous_disable  = igbvf_promiscuous_disable,
461         .allmulticast_enable  = igbvf_allmulticast_enable,
462         .allmulticast_disable = igbvf_allmulticast_disable,
463         .link_update          = eth_igb_link_update,
464         .stats_get            = eth_igbvf_stats_get,
465         .xstats_get           = eth_igbvf_xstats_get,
466         .xstats_get_names     = eth_igbvf_xstats_get_names,
467         .stats_reset          = eth_igbvf_stats_reset,
468         .xstats_reset         = eth_igbvf_stats_reset,
469         .vlan_filter_set      = igbvf_vlan_filter_set,
470         .dev_infos_get        = eth_igbvf_infos_get,
471         .dev_supported_ptypes_get = eth_igb_supported_ptypes_get,
472         .rx_queue_setup       = eth_igb_rx_queue_setup,
473         .rx_queue_release     = eth_igb_rx_queue_release,
474         .tx_queue_setup       = eth_igb_tx_queue_setup,
475         .tx_queue_release     = eth_igb_tx_queue_release,
476         .set_mc_addr_list     = eth_igb_set_mc_addr_list,
477         .rxq_info_get         = igb_rxq_info_get,
478         .txq_info_get         = igb_txq_info_get,
479         .mac_addr_set         = igbvf_default_mac_addr_set,
480         .get_reg              = igbvf_get_regs,
481 };
482
483 /* store statistics names and its offset in stats structure */
484 struct rte_igb_xstats_name_off {
485         char name[RTE_ETH_XSTATS_NAME_SIZE];
486         unsigned offset;
487 };
488
489 static const struct rte_igb_xstats_name_off rte_igb_stats_strings[] = {
490         {"rx_crc_errors", offsetof(struct e1000_hw_stats, crcerrs)},
491         {"rx_align_errors", offsetof(struct e1000_hw_stats, algnerrc)},
492         {"rx_symbol_errors", offsetof(struct e1000_hw_stats, symerrs)},
493         {"rx_missed_packets", offsetof(struct e1000_hw_stats, mpc)},
494         {"tx_single_collision_packets", offsetof(struct e1000_hw_stats, scc)},
495         {"tx_multiple_collision_packets", offsetof(struct e1000_hw_stats, mcc)},
496         {"tx_excessive_collision_packets", offsetof(struct e1000_hw_stats,
497                 ecol)},
498         {"tx_late_collisions", offsetof(struct e1000_hw_stats, latecol)},
499         {"tx_total_collisions", offsetof(struct e1000_hw_stats, colc)},
500         {"tx_deferred_packets", offsetof(struct e1000_hw_stats, dc)},
501         {"tx_no_carrier_sense_packets", offsetof(struct e1000_hw_stats, tncrs)},
502         {"rx_carrier_ext_errors", offsetof(struct e1000_hw_stats, cexterr)},
503         {"rx_length_errors", offsetof(struct e1000_hw_stats, rlec)},
504         {"rx_xon_packets", offsetof(struct e1000_hw_stats, xonrxc)},
505         {"tx_xon_packets", offsetof(struct e1000_hw_stats, xontxc)},
506         {"rx_xoff_packets", offsetof(struct e1000_hw_stats, xoffrxc)},
507         {"tx_xoff_packets", offsetof(struct e1000_hw_stats, xofftxc)},
508         {"rx_flow_control_unsupported_packets", offsetof(struct e1000_hw_stats,
509                 fcruc)},
510         {"rx_size_64_packets", offsetof(struct e1000_hw_stats, prc64)},
511         {"rx_size_65_to_127_packets", offsetof(struct e1000_hw_stats, prc127)},
512         {"rx_size_128_to_255_packets", offsetof(struct e1000_hw_stats, prc255)},
513         {"rx_size_256_to_511_packets", offsetof(struct e1000_hw_stats, prc511)},
514         {"rx_size_512_to_1023_packets", offsetof(struct e1000_hw_stats,
515                 prc1023)},
516         {"rx_size_1024_to_max_packets", offsetof(struct e1000_hw_stats,
517                 prc1522)},
518         {"rx_broadcast_packets", offsetof(struct e1000_hw_stats, bprc)},
519         {"rx_multicast_packets", offsetof(struct e1000_hw_stats, mprc)},
520         {"rx_undersize_errors", offsetof(struct e1000_hw_stats, ruc)},
521         {"rx_fragment_errors", offsetof(struct e1000_hw_stats, rfc)},
522         {"rx_oversize_errors", offsetof(struct e1000_hw_stats, roc)},
523         {"rx_jabber_errors", offsetof(struct e1000_hw_stats, rjc)},
524         {"rx_management_packets", offsetof(struct e1000_hw_stats, mgprc)},
525         {"rx_management_dropped", offsetof(struct e1000_hw_stats, mgpdc)},
526         {"tx_management_packets", offsetof(struct e1000_hw_stats, mgptc)},
527         {"rx_total_packets", offsetof(struct e1000_hw_stats, tpr)},
528         {"tx_total_packets", offsetof(struct e1000_hw_stats, tpt)},
529         {"rx_total_bytes", offsetof(struct e1000_hw_stats, tor)},
530         {"tx_total_bytes", offsetof(struct e1000_hw_stats, tot)},
531         {"tx_size_64_packets", offsetof(struct e1000_hw_stats, ptc64)},
532         {"tx_size_65_to_127_packets", offsetof(struct e1000_hw_stats, ptc127)},
533         {"tx_size_128_to_255_packets", offsetof(struct e1000_hw_stats, ptc255)},
534         {"tx_size_256_to_511_packets", offsetof(struct e1000_hw_stats, ptc511)},
535         {"tx_size_512_to_1023_packets", offsetof(struct e1000_hw_stats,
536                 ptc1023)},
537         {"tx_size_1023_to_max_packets", offsetof(struct e1000_hw_stats,
538                 ptc1522)},
539         {"tx_multicast_packets", offsetof(struct e1000_hw_stats, mptc)},
540         {"tx_broadcast_packets", offsetof(struct e1000_hw_stats, bptc)},
541         {"tx_tso_packets", offsetof(struct e1000_hw_stats, tsctc)},
542         {"tx_tso_errors", offsetof(struct e1000_hw_stats, tsctfc)},
543         {"rx_sent_to_host_packets", offsetof(struct e1000_hw_stats, rpthc)},
544         {"tx_sent_by_host_packets", offsetof(struct e1000_hw_stats, hgptc)},
545         {"rx_code_violation_packets", offsetof(struct e1000_hw_stats, scvpc)},
546
547         {"interrupt_assert_count", offsetof(struct e1000_hw_stats, iac)},
548 };
549
550 #define IGB_NB_XSTATS (sizeof(rte_igb_stats_strings) / \
551                 sizeof(rte_igb_stats_strings[0]))
552
553 static const struct rte_igb_xstats_name_off rte_igbvf_stats_strings[] = {
554         {"rx_multicast_packets", offsetof(struct e1000_vf_stats, mprc)},
555         {"rx_good_loopback_packets", offsetof(struct e1000_vf_stats, gprlbc)},
556         {"tx_good_loopback_packets", offsetof(struct e1000_vf_stats, gptlbc)},
557         {"rx_good_loopback_bytes", offsetof(struct e1000_vf_stats, gorlbc)},
558         {"tx_good_loopback_bytes", offsetof(struct e1000_vf_stats, gotlbc)},
559 };
560
561 #define IGBVF_NB_XSTATS (sizeof(rte_igbvf_stats_strings) / \
562                 sizeof(rte_igbvf_stats_strings[0]))
563
564 /**
565  * Atomically reads the link status information from global
566  * structure rte_eth_dev.
567  *
568  * @param dev
569  *   - Pointer to the structure rte_eth_dev to read from.
570  *   - Pointer to the buffer to be saved with the link status.
571  *
572  * @return
573  *   - On success, zero.
574  *   - On failure, negative value.
575  */
576 static inline int
577 rte_igb_dev_atomic_read_link_status(struct rte_eth_dev *dev,
578                                 struct rte_eth_link *link)
579 {
580         struct rte_eth_link *dst = link;
581         struct rte_eth_link *src = &(dev->data->dev_link);
582
583         if (rte_atomic64_cmpset((uint64_t *)dst, *(uint64_t *)dst,
584                                         *(uint64_t *)src) == 0)
585                 return -1;
586
587         return 0;
588 }
589
590 /**
591  * Atomically writes the link status information into global
592  * structure rte_eth_dev.
593  *
594  * @param dev
595  *   - Pointer to the structure rte_eth_dev to read from.
596  *   - Pointer to the buffer to be saved with the link status.
597  *
598  * @return
599  *   - On success, zero.
600  *   - On failure, negative value.
601  */
602 static inline int
603 rte_igb_dev_atomic_write_link_status(struct rte_eth_dev *dev,
604                                 struct rte_eth_link *link)
605 {
606         struct rte_eth_link *dst = &(dev->data->dev_link);
607         struct rte_eth_link *src = link;
608
609         if (rte_atomic64_cmpset((uint64_t *)dst, *(uint64_t *)dst,
610                                         *(uint64_t *)src) == 0)
611                 return -1;
612
613         return 0;
614 }
615
616 static inline void
617 igb_intr_enable(struct rte_eth_dev *dev)
618 {
619         struct e1000_interrupt *intr =
620                 E1000_DEV_PRIVATE_TO_INTR(dev->data->dev_private);
621         struct e1000_hw *hw =
622                 E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
623
624         E1000_WRITE_REG(hw, E1000_IMS, intr->mask);
625         E1000_WRITE_FLUSH(hw);
626 }
627
628 static void
629 igb_intr_disable(struct e1000_hw *hw)
630 {
631         E1000_WRITE_REG(hw, E1000_IMC, ~0);
632         E1000_WRITE_FLUSH(hw);
633 }
634
635 static inline void
636 igbvf_intr_enable(struct rte_eth_dev *dev)
637 {
638         struct e1000_hw *hw =
639                 E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
640
641         /* only for mailbox */
642         E1000_WRITE_REG(hw, E1000_EIAM, 1 << E1000_VTIVAR_MISC_MAILBOX);
643         E1000_WRITE_REG(hw, E1000_EIAC, 1 << E1000_VTIVAR_MISC_MAILBOX);
644         E1000_WRITE_REG(hw, E1000_EIMS, 1 << E1000_VTIVAR_MISC_MAILBOX);
645         E1000_WRITE_FLUSH(hw);
646 }
647
648 /* only for mailbox now. If RX/TX needed, should extend this function.  */
649 static void
650 igbvf_set_ivar_map(struct e1000_hw *hw, uint8_t msix_vector)
651 {
652         uint32_t tmp = 0;
653
654         /* mailbox */
655         tmp |= (msix_vector & E1000_VTIVAR_MISC_INTR_MASK);
656         tmp |= E1000_VTIVAR_VALID;
657         E1000_WRITE_REG(hw, E1000_VTIVAR_MISC, tmp);
658 }
659
660 static void
661 eth_igbvf_configure_msix_intr(struct rte_eth_dev *dev)
662 {
663         struct e1000_hw *hw =
664                 E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
665
666         /* Configure VF other cause ivar */
667         igbvf_set_ivar_map(hw, E1000_VTIVAR_MISC_MAILBOX);
668 }
669
670 static inline int32_t
671 igb_pf_reset_hw(struct e1000_hw *hw)
672 {
673         uint32_t ctrl_ext;
674         int32_t status;
675
676         status = e1000_reset_hw(hw);
677
678         ctrl_ext = E1000_READ_REG(hw, E1000_CTRL_EXT);
679         /* Set PF Reset Done bit so PF/VF Mail Ops can work */
680         ctrl_ext |= E1000_CTRL_EXT_PFRSTD;
681         E1000_WRITE_REG(hw, E1000_CTRL_EXT, ctrl_ext);
682         E1000_WRITE_FLUSH(hw);
683
684         return status;
685 }
686
687 static void
688 igb_identify_hardware(struct rte_eth_dev *dev, struct rte_pci_device *pci_dev)
689 {
690         struct e1000_hw *hw =
691                 E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
692
693
694         hw->vendor_id = pci_dev->id.vendor_id;
695         hw->device_id = pci_dev->id.device_id;
696         hw->subsystem_vendor_id = pci_dev->id.subsystem_vendor_id;
697         hw->subsystem_device_id = pci_dev->id.subsystem_device_id;
698
699         e1000_set_mac_type(hw);
700
701         /* need to check if it is a vf device below */
702 }
703
704 static int
705 igb_reset_swfw_lock(struct e1000_hw *hw)
706 {
707         int ret_val;
708
709         /*
710          * Do mac ops initialization manually here, since we will need
711          * some function pointers set by this call.
712          */
713         ret_val = e1000_init_mac_params(hw);
714         if (ret_val)
715                 return ret_val;
716
717         /*
718          * SMBI lock should not fail in this early stage. If this is the case,
719          * it is due to an improper exit of the application.
720          * So force the release of the faulty lock.
721          */
722         if (e1000_get_hw_semaphore_generic(hw) < 0) {
723                 PMD_DRV_LOG(DEBUG, "SMBI lock released");
724         }
725         e1000_put_hw_semaphore_generic(hw);
726
727         if (hw->mac.ops.acquire_swfw_sync != NULL) {
728                 uint16_t mask;
729
730                 /*
731                  * Phy lock should not fail in this early stage. If this is the case,
732                  * it is due to an improper exit of the application.
733                  * So force the release of the faulty lock.
734                  */
735                 mask = E1000_SWFW_PHY0_SM << hw->bus.func;
736                 if (hw->bus.func > E1000_FUNC_1)
737                         mask <<= 2;
738                 if (hw->mac.ops.acquire_swfw_sync(hw, mask) < 0) {
739                         PMD_DRV_LOG(DEBUG, "SWFW phy%d lock released",
740                                     hw->bus.func);
741                 }
742                 hw->mac.ops.release_swfw_sync(hw, mask);
743
744                 /*
745                  * This one is more tricky since it is common to all ports; but
746                  * swfw_sync retries last long enough (1s) to be almost sure that if
747                  * lock can not be taken it is due to an improper lock of the
748                  * semaphore.
749                  */
750                 mask = E1000_SWFW_EEP_SM;
751                 if (hw->mac.ops.acquire_swfw_sync(hw, mask) < 0) {
752                         PMD_DRV_LOG(DEBUG, "SWFW common locks released");
753                 }
754                 hw->mac.ops.release_swfw_sync(hw, mask);
755         }
756
757         return E1000_SUCCESS;
758 }
759
760 /* Remove all ntuple filters of the device */
761 static int igb_ntuple_filter_uninit(struct rte_eth_dev *eth_dev)
762 {
763         struct e1000_filter_info *filter_info =
764                 E1000_DEV_PRIVATE_TO_FILTER_INFO(eth_dev->data->dev_private);
765         struct e1000_5tuple_filter *p_5tuple;
766         struct e1000_2tuple_filter *p_2tuple;
767
768         while ((p_5tuple = TAILQ_FIRST(&filter_info->fivetuple_list))) {
769                 TAILQ_REMOVE(&filter_info->fivetuple_list,
770                         p_5tuple, entries);
771                         rte_free(p_5tuple);
772         }
773         filter_info->fivetuple_mask = 0;
774         while ((p_2tuple = TAILQ_FIRST(&filter_info->twotuple_list))) {
775                 TAILQ_REMOVE(&filter_info->twotuple_list,
776                         p_2tuple, entries);
777                         rte_free(p_2tuple);
778         }
779         filter_info->twotuple_mask = 0;
780
781         return 0;
782 }
783
784 /* Remove all flex filters of the device */
785 static int igb_flex_filter_uninit(struct rte_eth_dev *eth_dev)
786 {
787         struct e1000_filter_info *filter_info =
788                 E1000_DEV_PRIVATE_TO_FILTER_INFO(eth_dev->data->dev_private);
789         struct e1000_flex_filter *p_flex;
790
791         while ((p_flex = TAILQ_FIRST(&filter_info->flex_list))) {
792                 TAILQ_REMOVE(&filter_info->flex_list, p_flex, entries);
793                 rte_free(p_flex);
794         }
795         filter_info->flex_mask = 0;
796
797         return 0;
798 }
799
800 static int
801 eth_igb_dev_init(struct rte_eth_dev *eth_dev)
802 {
803         int error = 0;
804         struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(eth_dev);
805         struct e1000_hw *hw =
806                 E1000_DEV_PRIVATE_TO_HW(eth_dev->data->dev_private);
807         struct e1000_vfta * shadow_vfta =
808                 E1000_DEV_PRIVATE_TO_VFTA(eth_dev->data->dev_private);
809         struct e1000_filter_info *filter_info =
810                 E1000_DEV_PRIVATE_TO_FILTER_INFO(eth_dev->data->dev_private);
811         struct e1000_adapter *adapter =
812                 E1000_DEV_PRIVATE(eth_dev->data->dev_private);
813
814         uint32_t ctrl_ext;
815
816         eth_dev->dev_ops = &eth_igb_ops;
817         eth_dev->rx_pkt_burst = &eth_igb_recv_pkts;
818         eth_dev->tx_pkt_burst = &eth_igb_xmit_pkts;
819         eth_dev->tx_pkt_prepare = &eth_igb_prep_pkts;
820
821         /* for secondary processes, we don't initialise any further as primary
822          * has already done this work. Only check we don't need a different
823          * RX function */
824         if (rte_eal_process_type() != RTE_PROC_PRIMARY){
825                 if (eth_dev->data->scattered_rx)
826                         eth_dev->rx_pkt_burst = &eth_igb_recv_scattered_pkts;
827                 return 0;
828         }
829
830         rte_eth_copy_pci_info(eth_dev, pci_dev);
831         eth_dev->data->dev_flags |= RTE_ETH_DEV_DETACHABLE;
832
833         hw->hw_addr= (void *)pci_dev->mem_resource[0].addr;
834
835         igb_identify_hardware(eth_dev, pci_dev);
836         if (e1000_setup_init_funcs(hw, FALSE) != E1000_SUCCESS) {
837                 error = -EIO;
838                 goto err_late;
839         }
840
841         e1000_get_bus_info(hw);
842
843         /* Reset any pending lock */
844         if (igb_reset_swfw_lock(hw) != E1000_SUCCESS) {
845                 error = -EIO;
846                 goto err_late;
847         }
848
849         /* Finish initialization */
850         if (e1000_setup_init_funcs(hw, TRUE) != E1000_SUCCESS) {
851                 error = -EIO;
852                 goto err_late;
853         }
854
855         hw->mac.autoneg = 1;
856         hw->phy.autoneg_wait_to_complete = 0;
857         hw->phy.autoneg_advertised = E1000_ALL_SPEED_DUPLEX;
858
859         /* Copper options */
860         if (hw->phy.media_type == e1000_media_type_copper) {
861                 hw->phy.mdix = 0; /* AUTO_ALL_MODES */
862                 hw->phy.disable_polarity_correction = 0;
863                 hw->phy.ms_type = e1000_ms_hw_default;
864         }
865
866         /*
867          * Start from a known state, this is important in reading the nvm
868          * and mac from that.
869          */
870         igb_pf_reset_hw(hw);
871
872         /* Make sure we have a good EEPROM before we read from it */
873         if (e1000_validate_nvm_checksum(hw) < 0) {
874                 /*
875                  * Some PCI-E parts fail the first check due to
876                  * the link being in sleep state, call it again,
877                  * if it fails a second time its a real issue.
878                  */
879                 if (e1000_validate_nvm_checksum(hw) < 0) {
880                         PMD_INIT_LOG(ERR, "EEPROM checksum invalid");
881                         error = -EIO;
882                         goto err_late;
883                 }
884         }
885
886         /* Read the permanent MAC address out of the EEPROM */
887         if (e1000_read_mac_addr(hw) != 0) {
888                 PMD_INIT_LOG(ERR, "EEPROM error while reading MAC address");
889                 error = -EIO;
890                 goto err_late;
891         }
892
893         /* Allocate memory for storing MAC addresses */
894         eth_dev->data->mac_addrs = rte_zmalloc("e1000",
895                 ETHER_ADDR_LEN * hw->mac.rar_entry_count, 0);
896         if (eth_dev->data->mac_addrs == NULL) {
897                 PMD_INIT_LOG(ERR, "Failed to allocate %d bytes needed to "
898                                                 "store MAC addresses",
899                                 ETHER_ADDR_LEN * hw->mac.rar_entry_count);
900                 error = -ENOMEM;
901                 goto err_late;
902         }
903
904         /* Copy the permanent MAC address */
905         ether_addr_copy((struct ether_addr *)hw->mac.addr, &eth_dev->data->mac_addrs[0]);
906
907         /* initialize the vfta */
908         memset(shadow_vfta, 0, sizeof(*shadow_vfta));
909
910         /* Now initialize the hardware */
911         if (igb_hardware_init(hw) != 0) {
912                 PMD_INIT_LOG(ERR, "Hardware initialization failed");
913                 rte_free(eth_dev->data->mac_addrs);
914                 eth_dev->data->mac_addrs = NULL;
915                 error = -ENODEV;
916                 goto err_late;
917         }
918         hw->mac.get_link_status = 1;
919         adapter->stopped = 0;
920
921         /* Indicate SOL/IDER usage */
922         if (e1000_check_reset_block(hw) < 0) {
923                 PMD_INIT_LOG(ERR, "PHY reset is blocked due to"
924                                         "SOL/IDER session");
925         }
926
927         /* initialize PF if max_vfs not zero */
928         igb_pf_host_init(eth_dev);
929
930         ctrl_ext = E1000_READ_REG(hw, E1000_CTRL_EXT);
931         /* Set PF Reset Done bit so PF/VF Mail Ops can work */
932         ctrl_ext |= E1000_CTRL_EXT_PFRSTD;
933         E1000_WRITE_REG(hw, E1000_CTRL_EXT, ctrl_ext);
934         E1000_WRITE_FLUSH(hw);
935
936         PMD_INIT_LOG(DEBUG, "port_id %d vendorID=0x%x deviceID=0x%x",
937                      eth_dev->data->port_id, pci_dev->id.vendor_id,
938                      pci_dev->id.device_id);
939
940         rte_intr_callback_register(&pci_dev->intr_handle,
941                                    eth_igb_interrupt_handler,
942                                    (void *)eth_dev);
943
944         /* enable uio/vfio intr/eventfd mapping */
945         rte_intr_enable(&pci_dev->intr_handle);
946
947         /* enable support intr */
948         igb_intr_enable(eth_dev);
949
950         /* initialize filter info */
951         memset(filter_info, 0,
952                sizeof(struct e1000_filter_info));
953
954         TAILQ_INIT(&filter_info->flex_list);
955         TAILQ_INIT(&filter_info->twotuple_list);
956         TAILQ_INIT(&filter_info->fivetuple_list);
957
958         return 0;
959
960 err_late:
961         igb_hw_control_release(hw);
962
963         return error;
964 }
965
966 static int
967 eth_igb_dev_uninit(struct rte_eth_dev *eth_dev)
968 {
969         struct rte_pci_device *pci_dev;
970         struct rte_intr_handle *intr_handle;
971         struct e1000_hw *hw;
972         struct e1000_adapter *adapter =
973                 E1000_DEV_PRIVATE(eth_dev->data->dev_private);
974         struct e1000_filter_info *filter_info =
975                 E1000_DEV_PRIVATE_TO_FILTER_INFO(eth_dev->data->dev_private);
976
977         PMD_INIT_FUNC_TRACE();
978
979         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
980                 return -EPERM;
981
982         hw = E1000_DEV_PRIVATE_TO_HW(eth_dev->data->dev_private);
983         pci_dev = RTE_ETH_DEV_TO_PCI(eth_dev);
984         intr_handle = &pci_dev->intr_handle;
985
986         if (adapter->stopped == 0)
987                 eth_igb_close(eth_dev);
988
989         eth_dev->dev_ops = NULL;
990         eth_dev->rx_pkt_burst = NULL;
991         eth_dev->tx_pkt_burst = NULL;
992
993         /* Reset any pending lock */
994         igb_reset_swfw_lock(hw);
995
996         rte_free(eth_dev->data->mac_addrs);
997         eth_dev->data->mac_addrs = NULL;
998
999         /* uninitialize PF if max_vfs not zero */
1000         igb_pf_host_uninit(eth_dev);
1001
1002         /* disable uio intr before callback unregister */
1003         rte_intr_disable(intr_handle);
1004         rte_intr_callback_unregister(intr_handle,
1005                                      eth_igb_interrupt_handler, eth_dev);
1006
1007         /* clear the SYN filter info */
1008         filter_info->syn_info = 0;
1009
1010         /* clear the ethertype filters info */
1011         filter_info->ethertype_mask = 0;
1012         memset(filter_info->ethertype_filters, 0,
1013                 E1000_MAX_ETQF_FILTERS * sizeof(struct igb_ethertype_filter));
1014
1015         /* remove all ntuple filters of the device */
1016         igb_ntuple_filter_uninit(eth_dev);
1017
1018         /* remove all flex filters of the device */
1019         igb_flex_filter_uninit(eth_dev);
1020
1021         return 0;
1022 }
1023
1024 /*
1025  * Virtual Function device init
1026  */
1027 static int
1028 eth_igbvf_dev_init(struct rte_eth_dev *eth_dev)
1029 {
1030         struct rte_pci_device *pci_dev;
1031         struct rte_intr_handle *intr_handle;
1032         struct e1000_adapter *adapter =
1033                 E1000_DEV_PRIVATE(eth_dev->data->dev_private);
1034         struct e1000_hw *hw =
1035                 E1000_DEV_PRIVATE_TO_HW(eth_dev->data->dev_private);
1036         int diag;
1037         struct ether_addr *perm_addr = (struct ether_addr *)hw->mac.perm_addr;
1038
1039         PMD_INIT_FUNC_TRACE();
1040
1041         eth_dev->dev_ops = &igbvf_eth_dev_ops;
1042         eth_dev->rx_pkt_burst = &eth_igb_recv_pkts;
1043         eth_dev->tx_pkt_burst = &eth_igb_xmit_pkts;
1044         eth_dev->tx_pkt_prepare = &eth_igb_prep_pkts;
1045
1046         /* for secondary processes, we don't initialise any further as primary
1047          * has already done this work. Only check we don't need a different
1048          * RX function */
1049         if (rte_eal_process_type() != RTE_PROC_PRIMARY){
1050                 if (eth_dev->data->scattered_rx)
1051                         eth_dev->rx_pkt_burst = &eth_igb_recv_scattered_pkts;
1052                 return 0;
1053         }
1054
1055         pci_dev = RTE_ETH_DEV_TO_PCI(eth_dev);
1056         rte_eth_copy_pci_info(eth_dev, pci_dev);
1057         eth_dev->data->dev_flags |= RTE_ETH_DEV_DETACHABLE;
1058
1059         hw->device_id = pci_dev->id.device_id;
1060         hw->vendor_id = pci_dev->id.vendor_id;
1061         hw->hw_addr = (void *)pci_dev->mem_resource[0].addr;
1062         adapter->stopped = 0;
1063
1064         /* Initialize the shared code (base driver) */
1065         diag = e1000_setup_init_funcs(hw, TRUE);
1066         if (diag != 0) {
1067                 PMD_INIT_LOG(ERR, "Shared code init failed for igbvf: %d",
1068                         diag);
1069                 return -EIO;
1070         }
1071
1072         /* init_mailbox_params */
1073         hw->mbx.ops.init_params(hw);
1074
1075         /* Disable the interrupts for VF */
1076         igbvf_intr_disable(hw);
1077
1078         diag = hw->mac.ops.reset_hw(hw);
1079
1080         /* Allocate memory for storing MAC addresses */
1081         eth_dev->data->mac_addrs = rte_zmalloc("igbvf", ETHER_ADDR_LEN *
1082                 hw->mac.rar_entry_count, 0);
1083         if (eth_dev->data->mac_addrs == NULL) {
1084                 PMD_INIT_LOG(ERR,
1085                         "Failed to allocate %d bytes needed to store MAC "
1086                         "addresses",
1087                         ETHER_ADDR_LEN * hw->mac.rar_entry_count);
1088                 return -ENOMEM;
1089         }
1090
1091         /* Generate a random MAC address, if none was assigned by PF. */
1092         if (is_zero_ether_addr(perm_addr)) {
1093                 eth_random_addr(perm_addr->addr_bytes);
1094                 PMD_INIT_LOG(INFO, "\tVF MAC address not assigned by Host PF");
1095                 PMD_INIT_LOG(INFO, "\tAssign randomly generated MAC address "
1096                              "%02x:%02x:%02x:%02x:%02x:%02x",
1097                              perm_addr->addr_bytes[0],
1098                              perm_addr->addr_bytes[1],
1099                              perm_addr->addr_bytes[2],
1100                              perm_addr->addr_bytes[3],
1101                              perm_addr->addr_bytes[4],
1102                              perm_addr->addr_bytes[5]);
1103         }
1104
1105         diag = e1000_rar_set(hw, perm_addr->addr_bytes, 0);
1106         if (diag) {
1107                 rte_free(eth_dev->data->mac_addrs);
1108                 eth_dev->data->mac_addrs = NULL;
1109                 return diag;
1110         }
1111         /* Copy the permanent MAC address */
1112         ether_addr_copy((struct ether_addr *) hw->mac.perm_addr,
1113                         &eth_dev->data->mac_addrs[0]);
1114
1115         PMD_INIT_LOG(DEBUG, "port %d vendorID=0x%x deviceID=0x%x "
1116                      "mac.type=%s",
1117                      eth_dev->data->port_id, pci_dev->id.vendor_id,
1118                      pci_dev->id.device_id, "igb_mac_82576_vf");
1119
1120         intr_handle = &pci_dev->intr_handle;
1121         rte_intr_callback_register(intr_handle,
1122                                    eth_igbvf_interrupt_handler, eth_dev);
1123
1124         return 0;
1125 }
1126
1127 static int
1128 eth_igbvf_dev_uninit(struct rte_eth_dev *eth_dev)
1129 {
1130         struct e1000_adapter *adapter =
1131                 E1000_DEV_PRIVATE(eth_dev->data->dev_private);
1132         struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(eth_dev);
1133
1134         PMD_INIT_FUNC_TRACE();
1135
1136         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
1137                 return -EPERM;
1138
1139         if (adapter->stopped == 0)
1140                 igbvf_dev_close(eth_dev);
1141
1142         eth_dev->dev_ops = NULL;
1143         eth_dev->rx_pkt_burst = NULL;
1144         eth_dev->tx_pkt_burst = NULL;
1145
1146         rte_free(eth_dev->data->mac_addrs);
1147         eth_dev->data->mac_addrs = NULL;
1148
1149         /* disable uio intr before callback unregister */
1150         rte_intr_disable(&pci_dev->intr_handle);
1151         rte_intr_callback_unregister(&pci_dev->intr_handle,
1152                                      eth_igbvf_interrupt_handler,
1153                                      (void *)eth_dev);
1154
1155         return 0;
1156 }
1157
1158 static int eth_igb_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
1159         struct rte_pci_device *pci_dev)
1160 {
1161         return rte_eth_dev_pci_generic_probe(pci_dev,
1162                 sizeof(struct e1000_adapter), eth_igb_dev_init);
1163 }
1164
1165 static int eth_igb_pci_remove(struct rte_pci_device *pci_dev)
1166 {
1167         return rte_eth_dev_pci_generic_remove(pci_dev, eth_igb_dev_uninit);
1168 }
1169
1170 static struct rte_pci_driver rte_igb_pmd = {
1171         .id_table = pci_id_igb_map,
1172         .drv_flags = RTE_PCI_DRV_NEED_MAPPING | RTE_PCI_DRV_INTR_LSC,
1173         .probe = eth_igb_pci_probe,
1174         .remove = eth_igb_pci_remove,
1175 };
1176
1177
1178 static int eth_igbvf_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
1179         struct rte_pci_device *pci_dev)
1180 {
1181         return rte_eth_dev_pci_generic_probe(pci_dev,
1182                 sizeof(struct e1000_adapter), eth_igbvf_dev_init);
1183 }
1184
1185 static int eth_igbvf_pci_remove(struct rte_pci_device *pci_dev)
1186 {
1187         return rte_eth_dev_pci_generic_remove(pci_dev, eth_igbvf_dev_uninit);
1188 }
1189
1190 /*
1191  * virtual function driver struct
1192  */
1193 static struct rte_pci_driver rte_igbvf_pmd = {
1194         .id_table = pci_id_igbvf_map,
1195         .drv_flags = RTE_PCI_DRV_NEED_MAPPING,
1196         .probe = eth_igbvf_pci_probe,
1197         .remove = eth_igbvf_pci_remove,
1198 };
1199
1200 static void
1201 igb_vmdq_vlan_hw_filter_enable(struct rte_eth_dev *dev)
1202 {
1203         struct e1000_hw *hw =
1204                 E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1205         /* RCTL: enable VLAN filter since VMDq always use VLAN filter */
1206         uint32_t rctl = E1000_READ_REG(hw, E1000_RCTL);
1207         rctl |= E1000_RCTL_VFE;
1208         E1000_WRITE_REG(hw, E1000_RCTL, rctl);
1209 }
1210
1211 static int
1212 igb_check_mq_mode(struct rte_eth_dev *dev)
1213 {
1214         enum rte_eth_rx_mq_mode rx_mq_mode = dev->data->dev_conf.rxmode.mq_mode;
1215         enum rte_eth_tx_mq_mode tx_mq_mode = dev->data->dev_conf.txmode.mq_mode;
1216         uint16_t nb_rx_q = dev->data->nb_rx_queues;
1217         uint16_t nb_tx_q = dev->data->nb_rx_queues;
1218
1219         if ((rx_mq_mode & ETH_MQ_RX_DCB_FLAG) ||
1220             tx_mq_mode == ETH_MQ_TX_DCB ||
1221             tx_mq_mode == ETH_MQ_TX_VMDQ_DCB) {
1222                 PMD_INIT_LOG(ERR, "DCB mode is not supported.");
1223                 return -EINVAL;
1224         }
1225         if (RTE_ETH_DEV_SRIOV(dev).active != 0) {
1226                 /* Check multi-queue mode.
1227                  * To no break software we accept ETH_MQ_RX_NONE as this might
1228                  * be used to turn off VLAN filter.
1229                  */
1230
1231                 if (rx_mq_mode == ETH_MQ_RX_NONE ||
1232                     rx_mq_mode == ETH_MQ_RX_VMDQ_ONLY) {
1233                         dev->data->dev_conf.rxmode.mq_mode = ETH_MQ_RX_VMDQ_ONLY;
1234                         RTE_ETH_DEV_SRIOV(dev).nb_q_per_pool = 1;
1235                 } else {
1236                         /* Only support one queue on VFs.
1237                          * RSS together with SRIOV is not supported.
1238                          */
1239                         PMD_INIT_LOG(ERR, "SRIOV is active,"
1240                                         " wrong mq_mode rx %d.",
1241                                         rx_mq_mode);
1242                         return -EINVAL;
1243                 }
1244                 /* TX mode is not used here, so mode might be ignored.*/
1245                 if (tx_mq_mode != ETH_MQ_TX_VMDQ_ONLY) {
1246                         /* SRIOV only works in VMDq enable mode */
1247                         PMD_INIT_LOG(WARNING, "SRIOV is active,"
1248                                         " TX mode %d is not supported. "
1249                                         " Driver will behave as %d mode.",
1250                                         tx_mq_mode, ETH_MQ_TX_VMDQ_ONLY);
1251                 }
1252
1253                 /* check valid queue number */
1254                 if ((nb_rx_q > 1) || (nb_tx_q > 1)) {
1255                         PMD_INIT_LOG(ERR, "SRIOV is active,"
1256                                         " only support one queue on VFs.");
1257                         return -EINVAL;
1258                 }
1259         } else {
1260                 /* To no break software that set invalid mode, only display
1261                  * warning if invalid mode is used.
1262                  */
1263                 if (rx_mq_mode != ETH_MQ_RX_NONE &&
1264                     rx_mq_mode != ETH_MQ_RX_VMDQ_ONLY &&
1265                     rx_mq_mode != ETH_MQ_RX_RSS) {
1266                         /* RSS together with VMDq not supported*/
1267                         PMD_INIT_LOG(ERR, "RX mode %d is not supported.",
1268                                      rx_mq_mode);
1269                         return -EINVAL;
1270                 }
1271
1272                 if (tx_mq_mode != ETH_MQ_TX_NONE &&
1273                     tx_mq_mode != ETH_MQ_TX_VMDQ_ONLY) {
1274                         PMD_INIT_LOG(WARNING, "TX mode %d is not supported."
1275                                         " Due to txmode is meaningless in this"
1276                                         " driver, just ignore.",
1277                                         tx_mq_mode);
1278                 }
1279         }
1280         return 0;
1281 }
1282
1283 static int
1284 eth_igb_configure(struct rte_eth_dev *dev)
1285 {
1286         struct e1000_interrupt *intr =
1287                 E1000_DEV_PRIVATE_TO_INTR(dev->data->dev_private);
1288         int ret;
1289
1290         PMD_INIT_FUNC_TRACE();
1291
1292         /* multipe queue mode checking */
1293         ret  = igb_check_mq_mode(dev);
1294         if (ret != 0) {
1295                 PMD_DRV_LOG(ERR, "igb_check_mq_mode fails with %d.",
1296                             ret);
1297                 return ret;
1298         }
1299
1300         intr->flags |= E1000_FLAG_NEED_LINK_UPDATE;
1301         PMD_INIT_FUNC_TRACE();
1302
1303         return 0;
1304 }
1305
1306 static int
1307 eth_igb_start(struct rte_eth_dev *dev)
1308 {
1309         struct e1000_hw *hw =
1310                 E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1311         struct e1000_adapter *adapter =
1312                 E1000_DEV_PRIVATE(dev->data->dev_private);
1313         struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
1314         struct rte_intr_handle *intr_handle = &pci_dev->intr_handle;
1315         int ret, mask;
1316         uint32_t intr_vector = 0;
1317         uint32_t ctrl_ext;
1318         uint32_t *speeds;
1319         int num_speeds;
1320         bool autoneg;
1321
1322         PMD_INIT_FUNC_TRACE();
1323
1324         /* disable uio/vfio intr/eventfd mapping */
1325         rte_intr_disable(intr_handle);
1326
1327         /* Power up the phy. Needed to make the link go Up */
1328         eth_igb_dev_set_link_up(dev);
1329
1330         /*
1331          * Packet Buffer Allocation (PBA)
1332          * Writing PBA sets the receive portion of the buffer
1333          * the remainder is used for the transmit buffer.
1334          */
1335         if (hw->mac.type == e1000_82575) {
1336                 uint32_t pba;
1337
1338                 pba = E1000_PBA_32K; /* 32K for Rx, 16K for Tx */
1339                 E1000_WRITE_REG(hw, E1000_PBA, pba);
1340         }
1341
1342         /* Put the address into the Receive Address Array */
1343         e1000_rar_set(hw, hw->mac.addr, 0);
1344
1345         /* Initialize the hardware */
1346         if (igb_hardware_init(hw)) {
1347                 PMD_INIT_LOG(ERR, "Unable to initialize the hardware");
1348                 return -EIO;
1349         }
1350         adapter->stopped = 0;
1351
1352         E1000_WRITE_REG(hw, E1000_VET, ETHER_TYPE_VLAN << 16 | ETHER_TYPE_VLAN);
1353
1354         ctrl_ext = E1000_READ_REG(hw, E1000_CTRL_EXT);
1355         /* Set PF Reset Done bit so PF/VF Mail Ops can work */
1356         ctrl_ext |= E1000_CTRL_EXT_PFRSTD;
1357         E1000_WRITE_REG(hw, E1000_CTRL_EXT, ctrl_ext);
1358         E1000_WRITE_FLUSH(hw);
1359
1360         /* configure PF module if SRIOV enabled */
1361         igb_pf_host_configure(dev);
1362
1363         /* check and configure queue intr-vector mapping */
1364         if ((rte_intr_cap_multiple(intr_handle) ||
1365              !RTE_ETH_DEV_SRIOV(dev).active) &&
1366             dev->data->dev_conf.intr_conf.rxq != 0) {
1367                 intr_vector = dev->data->nb_rx_queues;
1368                 if (rte_intr_efd_enable(intr_handle, intr_vector))
1369                         return -1;
1370         }
1371
1372         if (rte_intr_dp_is_en(intr_handle) && !intr_handle->intr_vec) {
1373                 intr_handle->intr_vec =
1374                         rte_zmalloc("intr_vec",
1375                                     dev->data->nb_rx_queues * sizeof(int), 0);
1376                 if (intr_handle->intr_vec == NULL) {
1377                         PMD_INIT_LOG(ERR, "Failed to allocate %d rx_queues"
1378                                      " intr_vec", dev->data->nb_rx_queues);
1379                         return -ENOMEM;
1380                 }
1381         }
1382
1383         /* confiugre msix for rx interrupt */
1384         eth_igb_configure_msix_intr(dev);
1385
1386         /* Configure for OS presence */
1387         igb_init_manageability(hw);
1388
1389         eth_igb_tx_init(dev);
1390
1391         /* This can fail when allocating mbufs for descriptor rings */
1392         ret = eth_igb_rx_init(dev);
1393         if (ret) {
1394                 PMD_INIT_LOG(ERR, "Unable to initialize RX hardware");
1395                 igb_dev_clear_queues(dev);
1396                 return ret;
1397         }
1398
1399         e1000_clear_hw_cntrs_base_generic(hw);
1400
1401         /*
1402          * VLAN Offload Settings
1403          */
1404         mask = ETH_VLAN_STRIP_MASK | ETH_VLAN_FILTER_MASK | \
1405                         ETH_VLAN_EXTEND_MASK;
1406         eth_igb_vlan_offload_set(dev, mask);
1407
1408         if (dev->data->dev_conf.rxmode.mq_mode == ETH_MQ_RX_VMDQ_ONLY) {
1409                 /* Enable VLAN filter since VMDq always use VLAN filter */
1410                 igb_vmdq_vlan_hw_filter_enable(dev);
1411         }
1412
1413         if ((hw->mac.type == e1000_82576) || (hw->mac.type == e1000_82580) ||
1414                 (hw->mac.type == e1000_i350) || (hw->mac.type == e1000_i210) ||
1415                 (hw->mac.type == e1000_i211)) {
1416                 /* Configure EITR with the maximum possible value (0xFFFF) */
1417                 E1000_WRITE_REG(hw, E1000_EITR(0), 0xFFFF);
1418         }
1419
1420         /* Setup link speed and duplex */
1421         speeds = &dev->data->dev_conf.link_speeds;
1422         if (*speeds == ETH_LINK_SPEED_AUTONEG) {
1423                 hw->phy.autoneg_advertised = E1000_ALL_SPEED_DUPLEX;
1424                 hw->mac.autoneg = 1;
1425         } else {
1426                 num_speeds = 0;
1427                 autoneg = (*speeds & ETH_LINK_SPEED_FIXED) == 0;
1428
1429                 /* Reset */
1430                 hw->phy.autoneg_advertised = 0;
1431
1432                 if (*speeds & ~(ETH_LINK_SPEED_10M_HD | ETH_LINK_SPEED_10M |
1433                                 ETH_LINK_SPEED_100M_HD | ETH_LINK_SPEED_100M |
1434                                 ETH_LINK_SPEED_1G | ETH_LINK_SPEED_FIXED)) {
1435                         num_speeds = -1;
1436                         goto error_invalid_config;
1437                 }
1438                 if (*speeds & ETH_LINK_SPEED_10M_HD) {
1439                         hw->phy.autoneg_advertised |= ADVERTISE_10_HALF;
1440                         num_speeds++;
1441                 }
1442                 if (*speeds & ETH_LINK_SPEED_10M) {
1443                         hw->phy.autoneg_advertised |= ADVERTISE_10_FULL;
1444                         num_speeds++;
1445                 }
1446                 if (*speeds & ETH_LINK_SPEED_100M_HD) {
1447                         hw->phy.autoneg_advertised |= ADVERTISE_100_HALF;
1448                         num_speeds++;
1449                 }
1450                 if (*speeds & ETH_LINK_SPEED_100M) {
1451                         hw->phy.autoneg_advertised |= ADVERTISE_100_FULL;
1452                         num_speeds++;
1453                 }
1454                 if (*speeds & ETH_LINK_SPEED_1G) {
1455                         hw->phy.autoneg_advertised |= ADVERTISE_1000_FULL;
1456                         num_speeds++;
1457                 }
1458                 if (num_speeds == 0 || (!autoneg && (num_speeds > 1)))
1459                         goto error_invalid_config;
1460
1461                 /* Set/reset the mac.autoneg based on the link speed,
1462                  * fixed or not
1463                  */
1464                 if (!autoneg) {
1465                         hw->mac.autoneg = 0;
1466                         hw->mac.forced_speed_duplex =
1467                                         hw->phy.autoneg_advertised;
1468                 } else {
1469                         hw->mac.autoneg = 1;
1470                 }
1471         }
1472
1473         e1000_setup_link(hw);
1474
1475         if (rte_intr_allow_others(intr_handle)) {
1476                 /* check if lsc interrupt is enabled */
1477                 if (dev->data->dev_conf.intr_conf.lsc != 0)
1478                         eth_igb_lsc_interrupt_setup(dev);
1479         } else {
1480                 rte_intr_callback_unregister(intr_handle,
1481                                              eth_igb_interrupt_handler,
1482                                              (void *)dev);
1483                 if (dev->data->dev_conf.intr_conf.lsc != 0)
1484                         PMD_INIT_LOG(INFO, "lsc won't enable because of"
1485                                      " no intr multiplex");
1486         }
1487
1488         /* check if rxq interrupt is enabled */
1489         if (dev->data->dev_conf.intr_conf.rxq != 0 &&
1490             rte_intr_dp_is_en(intr_handle))
1491                 eth_igb_rxq_interrupt_setup(dev);
1492
1493         /* enable uio/vfio intr/eventfd mapping */
1494         rte_intr_enable(intr_handle);
1495
1496         /* resume enabled intr since hw reset */
1497         igb_intr_enable(dev);
1498
1499         /* restore all types filter */
1500         igb_filter_restore(dev);
1501
1502         PMD_INIT_LOG(DEBUG, "<<");
1503
1504         return 0;
1505
1506 error_invalid_config:
1507         PMD_INIT_LOG(ERR, "Invalid advertised speeds (%u) for port %u",
1508                      dev->data->dev_conf.link_speeds, dev->data->port_id);
1509         igb_dev_clear_queues(dev);
1510         return -EINVAL;
1511 }
1512
1513 /*********************************************************************
1514  *
1515  *  This routine disables all traffic on the adapter by issuing a
1516  *  global reset on the MAC.
1517  *
1518  **********************************************************************/
1519 static void
1520 eth_igb_stop(struct rte_eth_dev *dev)
1521 {
1522         struct e1000_hw *hw = E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1523         struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
1524         struct rte_eth_link link;
1525         struct rte_intr_handle *intr_handle = &pci_dev->intr_handle;
1526
1527         igb_intr_disable(hw);
1528
1529         /* disable intr eventfd mapping */
1530         rte_intr_disable(intr_handle);
1531
1532         igb_pf_reset_hw(hw);
1533         E1000_WRITE_REG(hw, E1000_WUC, 0);
1534
1535         /* Set bit for Go Link disconnect */
1536         if (hw->mac.type >= e1000_82580) {
1537                 uint32_t phpm_reg;
1538
1539                 phpm_reg = E1000_READ_REG(hw, E1000_82580_PHY_POWER_MGMT);
1540                 phpm_reg |= E1000_82580_PM_GO_LINKD;
1541                 E1000_WRITE_REG(hw, E1000_82580_PHY_POWER_MGMT, phpm_reg);
1542         }
1543
1544         /* Power down the phy. Needed to make the link go Down */
1545         eth_igb_dev_set_link_down(dev);
1546
1547         igb_dev_clear_queues(dev);
1548
1549         /* clear the recorded link status */
1550         memset(&link, 0, sizeof(link));
1551         rte_igb_dev_atomic_write_link_status(dev, &link);
1552
1553         if (!rte_intr_allow_others(intr_handle))
1554                 /* resume to the default handler */
1555                 rte_intr_callback_register(intr_handle,
1556                                            eth_igb_interrupt_handler,
1557                                            (void *)dev);
1558
1559         /* Clean datapath event and queue/vec mapping */
1560         rte_intr_efd_disable(intr_handle);
1561         if (intr_handle->intr_vec != NULL) {
1562                 rte_free(intr_handle->intr_vec);
1563                 intr_handle->intr_vec = NULL;
1564         }
1565 }
1566
1567 static int
1568 eth_igb_dev_set_link_up(struct rte_eth_dev *dev)
1569 {
1570         struct e1000_hw *hw = E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1571
1572         if (hw->phy.media_type == e1000_media_type_copper)
1573                 e1000_power_up_phy(hw);
1574         else
1575                 e1000_power_up_fiber_serdes_link(hw);
1576
1577         return 0;
1578 }
1579
1580 static int
1581 eth_igb_dev_set_link_down(struct rte_eth_dev *dev)
1582 {
1583         struct e1000_hw *hw = E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1584
1585         if (hw->phy.media_type == e1000_media_type_copper)
1586                 e1000_power_down_phy(hw);
1587         else
1588                 e1000_shutdown_fiber_serdes_link(hw);
1589
1590         return 0;
1591 }
1592
1593 static void
1594 eth_igb_close(struct rte_eth_dev *dev)
1595 {
1596         struct e1000_hw *hw = E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1597         struct e1000_adapter *adapter =
1598                 E1000_DEV_PRIVATE(dev->data->dev_private);
1599         struct rte_eth_link link;
1600         struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
1601         struct rte_intr_handle *intr_handle = &pci_dev->intr_handle;
1602
1603         eth_igb_stop(dev);
1604         adapter->stopped = 1;
1605
1606         e1000_phy_hw_reset(hw);
1607         igb_release_manageability(hw);
1608         igb_hw_control_release(hw);
1609
1610         /* Clear bit for Go Link disconnect */
1611         if (hw->mac.type >= e1000_82580) {
1612                 uint32_t phpm_reg;
1613
1614                 phpm_reg = E1000_READ_REG(hw, E1000_82580_PHY_POWER_MGMT);
1615                 phpm_reg &= ~E1000_82580_PM_GO_LINKD;
1616                 E1000_WRITE_REG(hw, E1000_82580_PHY_POWER_MGMT, phpm_reg);
1617         }
1618
1619         igb_dev_free_queues(dev);
1620
1621         if (intr_handle->intr_vec) {
1622                 rte_free(intr_handle->intr_vec);
1623                 intr_handle->intr_vec = NULL;
1624         }
1625
1626         memset(&link, 0, sizeof(link));
1627         rte_igb_dev_atomic_write_link_status(dev, &link);
1628 }
1629
1630 static int
1631 igb_get_rx_buffer_size(struct e1000_hw *hw)
1632 {
1633         uint32_t rx_buf_size;
1634         if (hw->mac.type == e1000_82576) {
1635                 rx_buf_size = (E1000_READ_REG(hw, E1000_RXPBS) & 0xffff) << 10;
1636         } else if (hw->mac.type == e1000_82580 || hw->mac.type == e1000_i350) {
1637                 /* PBS needs to be translated according to a lookup table */
1638                 rx_buf_size = (E1000_READ_REG(hw, E1000_RXPBS) & 0xf);
1639                 rx_buf_size = (uint32_t) e1000_rxpbs_adjust_82580(rx_buf_size);
1640                 rx_buf_size = (rx_buf_size << 10);
1641         } else if (hw->mac.type == e1000_i210 || hw->mac.type == e1000_i211) {
1642                 rx_buf_size = (E1000_READ_REG(hw, E1000_RXPBS) & 0x3f) << 10;
1643         } else {
1644                 rx_buf_size = (E1000_READ_REG(hw, E1000_PBA) & 0xffff) << 10;
1645         }
1646
1647         return rx_buf_size;
1648 }
1649
1650 /*********************************************************************
1651  *
1652  *  Initialize the hardware
1653  *
1654  **********************************************************************/
1655 static int
1656 igb_hardware_init(struct e1000_hw *hw)
1657 {
1658         uint32_t rx_buf_size;
1659         int diag;
1660
1661         /* Let the firmware know the OS is in control */
1662         igb_hw_control_acquire(hw);
1663
1664         /*
1665          * These parameters control the automatic generation (Tx) and
1666          * response (Rx) to Ethernet PAUSE frames.
1667          * - High water mark should allow for at least two standard size (1518)
1668          *   frames to be received after sending an XOFF.
1669          * - Low water mark works best when it is very near the high water mark.
1670          *   This allows the receiver to restart by sending XON when it has
1671          *   drained a bit. Here we use an arbitrary value of 1500 which will
1672          *   restart after one full frame is pulled from the buffer. There
1673          *   could be several smaller frames in the buffer and if so they will
1674          *   not trigger the XON until their total number reduces the buffer
1675          *   by 1500.
1676          * - The pause time is fairly large at 1000 x 512ns = 512 usec.
1677          */
1678         rx_buf_size = igb_get_rx_buffer_size(hw);
1679
1680         hw->fc.high_water = rx_buf_size - (ETHER_MAX_LEN * 2);
1681         hw->fc.low_water = hw->fc.high_water - 1500;
1682         hw->fc.pause_time = IGB_FC_PAUSE_TIME;
1683         hw->fc.send_xon = 1;
1684
1685         /* Set Flow control, use the tunable location if sane */
1686         if ((igb_fc_setting != e1000_fc_none) && (igb_fc_setting < 4))
1687                 hw->fc.requested_mode = igb_fc_setting;
1688         else
1689                 hw->fc.requested_mode = e1000_fc_none;
1690
1691         /* Issue a global reset */
1692         igb_pf_reset_hw(hw);
1693         E1000_WRITE_REG(hw, E1000_WUC, 0);
1694
1695         diag = e1000_init_hw(hw);
1696         if (diag < 0)
1697                 return diag;
1698
1699         E1000_WRITE_REG(hw, E1000_VET, ETHER_TYPE_VLAN << 16 | ETHER_TYPE_VLAN);
1700         e1000_get_phy_info(hw);
1701         e1000_check_for_link(hw);
1702
1703         return 0;
1704 }
1705
1706 /* This function is based on igb_update_stats_counters() in igb/if_igb.c */
1707 static void
1708 igb_read_stats_registers(struct e1000_hw *hw, struct e1000_hw_stats *stats)
1709 {
1710         int pause_frames;
1711
1712         uint64_t old_gprc  = stats->gprc;
1713         uint64_t old_gptc  = stats->gptc;
1714         uint64_t old_tpr   = stats->tpr;
1715         uint64_t old_tpt   = stats->tpt;
1716         uint64_t old_rpthc = stats->rpthc;
1717         uint64_t old_hgptc = stats->hgptc;
1718
1719         if(hw->phy.media_type == e1000_media_type_copper ||
1720             (E1000_READ_REG(hw, E1000_STATUS) & E1000_STATUS_LU)) {
1721                 stats->symerrs +=
1722                     E1000_READ_REG(hw,E1000_SYMERRS);
1723                 stats->sec += E1000_READ_REG(hw, E1000_SEC);
1724         }
1725
1726         stats->crcerrs += E1000_READ_REG(hw, E1000_CRCERRS);
1727         stats->mpc += E1000_READ_REG(hw, E1000_MPC);
1728         stats->scc += E1000_READ_REG(hw, E1000_SCC);
1729         stats->ecol += E1000_READ_REG(hw, E1000_ECOL);
1730
1731         stats->mcc += E1000_READ_REG(hw, E1000_MCC);
1732         stats->latecol += E1000_READ_REG(hw, E1000_LATECOL);
1733         stats->colc += E1000_READ_REG(hw, E1000_COLC);
1734         stats->dc += E1000_READ_REG(hw, E1000_DC);
1735         stats->rlec += E1000_READ_REG(hw, E1000_RLEC);
1736         stats->xonrxc += E1000_READ_REG(hw, E1000_XONRXC);
1737         stats->xontxc += E1000_READ_REG(hw, E1000_XONTXC);
1738         /*
1739         ** For watchdog management we need to know if we have been
1740         ** paused during the last interval, so capture that here.
1741         */
1742         pause_frames = E1000_READ_REG(hw, E1000_XOFFRXC);
1743         stats->xoffrxc += pause_frames;
1744         stats->xofftxc += E1000_READ_REG(hw, E1000_XOFFTXC);
1745         stats->fcruc += E1000_READ_REG(hw, E1000_FCRUC);
1746         stats->prc64 += E1000_READ_REG(hw, E1000_PRC64);
1747         stats->prc127 += E1000_READ_REG(hw, E1000_PRC127);
1748         stats->prc255 += E1000_READ_REG(hw, E1000_PRC255);
1749         stats->prc511 += E1000_READ_REG(hw, E1000_PRC511);
1750         stats->prc1023 += E1000_READ_REG(hw, E1000_PRC1023);
1751         stats->prc1522 += E1000_READ_REG(hw, E1000_PRC1522);
1752         stats->gprc += E1000_READ_REG(hw, E1000_GPRC);
1753         stats->bprc += E1000_READ_REG(hw, E1000_BPRC);
1754         stats->mprc += E1000_READ_REG(hw, E1000_MPRC);
1755         stats->gptc += E1000_READ_REG(hw, E1000_GPTC);
1756
1757         /* For the 64-bit byte counters the low dword must be read first. */
1758         /* Both registers clear on the read of the high dword */
1759
1760         /* Workaround CRC bytes included in size, take away 4 bytes/packet */
1761         stats->gorc += E1000_READ_REG(hw, E1000_GORCL);
1762         stats->gorc += ((uint64_t)E1000_READ_REG(hw, E1000_GORCH) << 32);
1763         stats->gorc -= (stats->gprc - old_gprc) * ETHER_CRC_LEN;
1764         stats->gotc += E1000_READ_REG(hw, E1000_GOTCL);
1765         stats->gotc += ((uint64_t)E1000_READ_REG(hw, E1000_GOTCH) << 32);
1766         stats->gotc -= (stats->gptc - old_gptc) * ETHER_CRC_LEN;
1767
1768         stats->rnbc += E1000_READ_REG(hw, E1000_RNBC);
1769         stats->ruc += E1000_READ_REG(hw, E1000_RUC);
1770         stats->rfc += E1000_READ_REG(hw, E1000_RFC);
1771         stats->roc += E1000_READ_REG(hw, E1000_ROC);
1772         stats->rjc += E1000_READ_REG(hw, E1000_RJC);
1773
1774         stats->tpr += E1000_READ_REG(hw, E1000_TPR);
1775         stats->tpt += E1000_READ_REG(hw, E1000_TPT);
1776
1777         stats->tor += E1000_READ_REG(hw, E1000_TORL);
1778         stats->tor += ((uint64_t)E1000_READ_REG(hw, E1000_TORH) << 32);
1779         stats->tor -= (stats->tpr - old_tpr) * ETHER_CRC_LEN;
1780         stats->tot += E1000_READ_REG(hw, E1000_TOTL);
1781         stats->tot += ((uint64_t)E1000_READ_REG(hw, E1000_TOTH) << 32);
1782         stats->tot -= (stats->tpt - old_tpt) * ETHER_CRC_LEN;
1783
1784         stats->ptc64 += E1000_READ_REG(hw, E1000_PTC64);
1785         stats->ptc127 += E1000_READ_REG(hw, E1000_PTC127);
1786         stats->ptc255 += E1000_READ_REG(hw, E1000_PTC255);
1787         stats->ptc511 += E1000_READ_REG(hw, E1000_PTC511);
1788         stats->ptc1023 += E1000_READ_REG(hw, E1000_PTC1023);
1789         stats->ptc1522 += E1000_READ_REG(hw, E1000_PTC1522);
1790         stats->mptc += E1000_READ_REG(hw, E1000_MPTC);
1791         stats->bptc += E1000_READ_REG(hw, E1000_BPTC);
1792
1793         /* Interrupt Counts */
1794
1795         stats->iac += E1000_READ_REG(hw, E1000_IAC);
1796         stats->icrxptc += E1000_READ_REG(hw, E1000_ICRXPTC);
1797         stats->icrxatc += E1000_READ_REG(hw, E1000_ICRXATC);
1798         stats->ictxptc += E1000_READ_REG(hw, E1000_ICTXPTC);
1799         stats->ictxatc += E1000_READ_REG(hw, E1000_ICTXATC);
1800         stats->ictxqec += E1000_READ_REG(hw, E1000_ICTXQEC);
1801         stats->ictxqmtc += E1000_READ_REG(hw, E1000_ICTXQMTC);
1802         stats->icrxdmtc += E1000_READ_REG(hw, E1000_ICRXDMTC);
1803         stats->icrxoc += E1000_READ_REG(hw, E1000_ICRXOC);
1804
1805         /* Host to Card Statistics */
1806
1807         stats->cbtmpc += E1000_READ_REG(hw, E1000_CBTMPC);
1808         stats->htdpmc += E1000_READ_REG(hw, E1000_HTDPMC);
1809         stats->cbrdpc += E1000_READ_REG(hw, E1000_CBRDPC);
1810         stats->cbrmpc += E1000_READ_REG(hw, E1000_CBRMPC);
1811         stats->rpthc += E1000_READ_REG(hw, E1000_RPTHC);
1812         stats->hgptc += E1000_READ_REG(hw, E1000_HGPTC);
1813         stats->htcbdpc += E1000_READ_REG(hw, E1000_HTCBDPC);
1814         stats->hgorc += E1000_READ_REG(hw, E1000_HGORCL);
1815         stats->hgorc += ((uint64_t)E1000_READ_REG(hw, E1000_HGORCH) << 32);
1816         stats->hgorc -= (stats->rpthc - old_rpthc) * ETHER_CRC_LEN;
1817         stats->hgotc += E1000_READ_REG(hw, E1000_HGOTCL);
1818         stats->hgotc += ((uint64_t)E1000_READ_REG(hw, E1000_HGOTCH) << 32);
1819         stats->hgotc -= (stats->hgptc - old_hgptc) * ETHER_CRC_LEN;
1820         stats->lenerrs += E1000_READ_REG(hw, E1000_LENERRS);
1821         stats->scvpc += E1000_READ_REG(hw, E1000_SCVPC);
1822         stats->hrmpc += E1000_READ_REG(hw, E1000_HRMPC);
1823
1824         stats->algnerrc += E1000_READ_REG(hw, E1000_ALGNERRC);
1825         stats->rxerrc += E1000_READ_REG(hw, E1000_RXERRC);
1826         stats->tncrs += E1000_READ_REG(hw, E1000_TNCRS);
1827         stats->cexterr += E1000_READ_REG(hw, E1000_CEXTERR);
1828         stats->tsctc += E1000_READ_REG(hw, E1000_TSCTC);
1829         stats->tsctfc += E1000_READ_REG(hw, E1000_TSCTFC);
1830 }
1831
1832 static void
1833 eth_igb_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *rte_stats)
1834 {
1835         struct e1000_hw *hw = E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1836         struct e1000_hw_stats *stats =
1837                         E1000_DEV_PRIVATE_TO_STATS(dev->data->dev_private);
1838
1839         igb_read_stats_registers(hw, stats);
1840
1841         if (rte_stats == NULL)
1842                 return;
1843
1844         /* Rx Errors */
1845         rte_stats->imissed = stats->mpc;
1846         rte_stats->ierrors = stats->crcerrs +
1847                              stats->rlec + stats->ruc + stats->roc +
1848                              stats->rxerrc + stats->algnerrc + stats->cexterr;
1849
1850         /* Tx Errors */
1851         rte_stats->oerrors = stats->ecol + stats->latecol;
1852
1853         rte_stats->ipackets = stats->gprc;
1854         rte_stats->opackets = stats->gptc;
1855         rte_stats->ibytes   = stats->gorc;
1856         rte_stats->obytes   = stats->gotc;
1857 }
1858
1859 static void
1860 eth_igb_stats_reset(struct rte_eth_dev *dev)
1861 {
1862         struct e1000_hw_stats *hw_stats =
1863                         E1000_DEV_PRIVATE_TO_STATS(dev->data->dev_private);
1864
1865         /* HW registers are cleared on read */
1866         eth_igb_stats_get(dev, NULL);
1867
1868         /* Reset software totals */
1869         memset(hw_stats, 0, sizeof(*hw_stats));
1870 }
1871
1872 static void
1873 eth_igb_xstats_reset(struct rte_eth_dev *dev)
1874 {
1875         struct e1000_hw_stats *stats =
1876                         E1000_DEV_PRIVATE_TO_STATS(dev->data->dev_private);
1877
1878         /* HW registers are cleared on read */
1879         eth_igb_xstats_get(dev, NULL, IGB_NB_XSTATS);
1880
1881         /* Reset software totals */
1882         memset(stats, 0, sizeof(*stats));
1883 }
1884
1885 static int eth_igb_xstats_get_names(__rte_unused struct rte_eth_dev *dev,
1886         struct rte_eth_xstat_name *xstats_names,
1887         __rte_unused unsigned int size)
1888 {
1889         unsigned i;
1890
1891         if (xstats_names == NULL)
1892                 return IGB_NB_XSTATS;
1893
1894         /* Note: limit checked in rte_eth_xstats_names() */
1895
1896         for (i = 0; i < IGB_NB_XSTATS; i++) {
1897                 snprintf(xstats_names[i].name, sizeof(xstats_names[i].name),
1898                          "%s", rte_igb_stats_strings[i].name);
1899         }
1900
1901         return IGB_NB_XSTATS;
1902 }
1903
1904 static int eth_igb_xstats_get_names_by_id(struct rte_eth_dev *dev,
1905                 struct rte_eth_xstat_name *xstats_names, const uint64_t *ids,
1906                 unsigned int limit)
1907 {
1908         unsigned int i;
1909
1910         if (!ids) {
1911                 if (xstats_names == NULL)
1912                         return IGB_NB_XSTATS;
1913
1914                 for (i = 0; i < IGB_NB_XSTATS; i++)
1915                         snprintf(xstats_names[i].name,
1916                                         sizeof(xstats_names[i].name),
1917                                         "%s", rte_igb_stats_strings[i].name);
1918
1919                 return IGB_NB_XSTATS;
1920
1921         } else {
1922                 struct rte_eth_xstat_name xstats_names_copy[IGB_NB_XSTATS];
1923
1924                 eth_igb_xstats_get_names_by_id(dev, xstats_names_copy, NULL,
1925                                 IGB_NB_XSTATS);
1926
1927                 for (i = 0; i < limit; i++) {
1928                         if (ids[i] >= IGB_NB_XSTATS) {
1929                                 PMD_INIT_LOG(ERR, "id value isn't valid");
1930                                 return -1;
1931                         }
1932                         strcpy(xstats_names[i].name,
1933                                         xstats_names_copy[ids[i]].name);
1934                 }
1935                 return limit;
1936         }
1937 }
1938
1939 static int
1940 eth_igb_xstats_get(struct rte_eth_dev *dev, struct rte_eth_xstat *xstats,
1941                    unsigned n)
1942 {
1943         struct e1000_hw *hw = E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1944         struct e1000_hw_stats *hw_stats =
1945                         E1000_DEV_PRIVATE_TO_STATS(dev->data->dev_private);
1946         unsigned i;
1947
1948         if (n < IGB_NB_XSTATS)
1949                 return IGB_NB_XSTATS;
1950
1951         igb_read_stats_registers(hw, hw_stats);
1952
1953         /* If this is a reset xstats is NULL, and we have cleared the
1954          * registers by reading them.
1955          */
1956         if (!xstats)
1957                 return 0;
1958
1959         /* Extended stats */
1960         for (i = 0; i < IGB_NB_XSTATS; i++) {
1961                 xstats[i].id = i;
1962                 xstats[i].value = *(uint64_t *)(((char *)hw_stats) +
1963                         rte_igb_stats_strings[i].offset);
1964         }
1965
1966         return IGB_NB_XSTATS;
1967 }
1968
1969 static int
1970 eth_igb_xstats_get_by_id(struct rte_eth_dev *dev, const uint64_t *ids,
1971                 uint64_t *values, unsigned int n)
1972 {
1973         unsigned int i;
1974
1975         if (!ids) {
1976                 struct e1000_hw *hw =
1977                         E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1978                 struct e1000_hw_stats *hw_stats =
1979                         E1000_DEV_PRIVATE_TO_STATS(dev->data->dev_private);
1980
1981                 if (n < IGB_NB_XSTATS)
1982                         return IGB_NB_XSTATS;
1983
1984                 igb_read_stats_registers(hw, hw_stats);
1985
1986                 /* If this is a reset xstats is NULL, and we have cleared the
1987                  * registers by reading them.
1988                  */
1989                 if (!values)
1990                         return 0;
1991
1992                 /* Extended stats */
1993                 for (i = 0; i < IGB_NB_XSTATS; i++)
1994                         values[i] = *(uint64_t *)(((char *)hw_stats) +
1995                                         rte_igb_stats_strings[i].offset);
1996
1997                 return IGB_NB_XSTATS;
1998
1999         } else {
2000                 uint64_t values_copy[IGB_NB_XSTATS];
2001
2002                 eth_igb_xstats_get_by_id(dev, NULL, values_copy,
2003                                 IGB_NB_XSTATS);
2004
2005                 for (i = 0; i < n; i++) {
2006                         if (ids[i] >= IGB_NB_XSTATS) {
2007                                 PMD_INIT_LOG(ERR, "id value isn't valid");
2008                                 return -1;
2009                         }
2010                         values[i] = values_copy[ids[i]];
2011                 }
2012                 return n;
2013         }
2014 }
2015
2016 static void
2017 igbvf_read_stats_registers(struct e1000_hw *hw, struct e1000_vf_stats *hw_stats)
2018 {
2019         /* Good Rx packets, include VF loopback */
2020         UPDATE_VF_STAT(E1000_VFGPRC,
2021             hw_stats->last_gprc, hw_stats->gprc);
2022
2023         /* Good Rx octets, include VF loopback */
2024         UPDATE_VF_STAT(E1000_VFGORC,
2025             hw_stats->last_gorc, hw_stats->gorc);
2026
2027         /* Good Tx packets, include VF loopback */
2028         UPDATE_VF_STAT(E1000_VFGPTC,
2029             hw_stats->last_gptc, hw_stats->gptc);
2030
2031         /* Good Tx octets, include VF loopback */
2032         UPDATE_VF_STAT(E1000_VFGOTC,
2033             hw_stats->last_gotc, hw_stats->gotc);
2034
2035         /* Rx Multicst packets */
2036         UPDATE_VF_STAT(E1000_VFMPRC,
2037             hw_stats->last_mprc, hw_stats->mprc);
2038
2039         /* Good Rx loopback packets */
2040         UPDATE_VF_STAT(E1000_VFGPRLBC,
2041             hw_stats->last_gprlbc, hw_stats->gprlbc);
2042
2043         /* Good Rx loopback octets */
2044         UPDATE_VF_STAT(E1000_VFGORLBC,
2045             hw_stats->last_gorlbc, hw_stats->gorlbc);
2046
2047         /* Good Tx loopback packets */
2048         UPDATE_VF_STAT(E1000_VFGPTLBC,
2049             hw_stats->last_gptlbc, hw_stats->gptlbc);
2050
2051         /* Good Tx loopback octets */
2052         UPDATE_VF_STAT(E1000_VFGOTLBC,
2053             hw_stats->last_gotlbc, hw_stats->gotlbc);
2054 }
2055
2056 static int eth_igbvf_xstats_get_names(__rte_unused struct rte_eth_dev *dev,
2057                                      struct rte_eth_xstat_name *xstats_names,
2058                                      __rte_unused unsigned limit)
2059 {
2060         unsigned i;
2061
2062         if (xstats_names != NULL)
2063                 for (i = 0; i < IGBVF_NB_XSTATS; i++) {
2064                         snprintf(xstats_names[i].name,
2065                                 sizeof(xstats_names[i].name), "%s",
2066                                 rte_igbvf_stats_strings[i].name);
2067                 }
2068         return IGBVF_NB_XSTATS;
2069 }
2070
2071 static int
2072 eth_igbvf_xstats_get(struct rte_eth_dev *dev, struct rte_eth_xstat *xstats,
2073                      unsigned n)
2074 {
2075         struct e1000_hw *hw = E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2076         struct e1000_vf_stats *hw_stats = (struct e1000_vf_stats *)
2077                         E1000_DEV_PRIVATE_TO_STATS(dev->data->dev_private);
2078         unsigned i;
2079
2080         if (n < IGBVF_NB_XSTATS)
2081                 return IGBVF_NB_XSTATS;
2082
2083         igbvf_read_stats_registers(hw, hw_stats);
2084
2085         if (!xstats)
2086                 return 0;
2087
2088         for (i = 0; i < IGBVF_NB_XSTATS; i++) {
2089                 xstats[i].id = i;
2090                 xstats[i].value = *(uint64_t *)(((char *)hw_stats) +
2091                         rte_igbvf_stats_strings[i].offset);
2092         }
2093
2094         return IGBVF_NB_XSTATS;
2095 }
2096
2097 static void
2098 eth_igbvf_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *rte_stats)
2099 {
2100         struct e1000_hw *hw = E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2101         struct e1000_vf_stats *hw_stats = (struct e1000_vf_stats *)
2102                           E1000_DEV_PRIVATE_TO_STATS(dev->data->dev_private);
2103
2104         igbvf_read_stats_registers(hw, hw_stats);
2105
2106         if (rte_stats == NULL)
2107                 return;
2108
2109         rte_stats->ipackets = hw_stats->gprc;
2110         rte_stats->ibytes = hw_stats->gorc;
2111         rte_stats->opackets = hw_stats->gptc;
2112         rte_stats->obytes = hw_stats->gotc;
2113 }
2114
2115 static void
2116 eth_igbvf_stats_reset(struct rte_eth_dev *dev)
2117 {
2118         struct e1000_vf_stats *hw_stats = (struct e1000_vf_stats*)
2119                         E1000_DEV_PRIVATE_TO_STATS(dev->data->dev_private);
2120
2121         /* Sync HW register to the last stats */
2122         eth_igbvf_stats_get(dev, NULL);
2123
2124         /* reset HW current stats*/
2125         memset(&hw_stats->gprc, 0, sizeof(*hw_stats) -
2126                offsetof(struct e1000_vf_stats, gprc));
2127 }
2128
2129 static int
2130 eth_igb_fw_version_get(struct rte_eth_dev *dev, char *fw_version,
2131                        size_t fw_size)
2132 {
2133         struct e1000_hw *hw = E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2134         struct e1000_fw_version fw;
2135         int ret;
2136
2137         e1000_get_fw_version(hw, &fw);
2138
2139         switch (hw->mac.type) {
2140         case e1000_i210:
2141         case e1000_i211:
2142                 if (!(e1000_get_flash_presence_i210(hw))) {
2143                         ret = snprintf(fw_version, fw_size,
2144                                  "%2d.%2d-%d",
2145                                  fw.invm_major, fw.invm_minor,
2146                                  fw.invm_img_type);
2147                         break;
2148                 }
2149                 /* fall through */
2150         default:
2151                 /* if option rom is valid, display its version too */
2152                 if (fw.or_valid) {
2153                         ret = snprintf(fw_version, fw_size,
2154                                  "%d.%d, 0x%08x, %d.%d.%d",
2155                                  fw.eep_major, fw.eep_minor, fw.etrack_id,
2156                                  fw.or_major, fw.or_build, fw.or_patch);
2157                 /* no option rom */
2158                 } else {
2159                         if (fw.etrack_id != 0X0000) {
2160                                 ret = snprintf(fw_version, fw_size,
2161                                          "%d.%d, 0x%08x",
2162                                          fw.eep_major, fw.eep_minor,
2163                                          fw.etrack_id);
2164                         } else {
2165                                 ret = snprintf(fw_version, fw_size,
2166                                          "%d.%d.%d",
2167                                          fw.eep_major, fw.eep_minor,
2168                                          fw.eep_build);
2169                         }
2170                 }
2171                 break;
2172         }
2173
2174         ret += 1; /* add the size of '\0' */
2175         if (fw_size < (u32)ret)
2176                 return ret;
2177         else
2178                 return 0;
2179 }
2180
2181 static void
2182 eth_igb_infos_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *dev_info)
2183 {
2184         struct e1000_hw *hw = E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2185
2186         dev_info->pci_dev = RTE_ETH_DEV_TO_PCI(dev);
2187         dev_info->min_rx_bufsize = 256; /* See BSIZE field of RCTL register. */
2188         dev_info->max_rx_pktlen  = 0x3FFF; /* See RLPML register. */
2189         dev_info->max_mac_addrs = hw->mac.rar_entry_count;
2190         dev_info->rx_offload_capa =
2191                 DEV_RX_OFFLOAD_VLAN_STRIP |
2192                 DEV_RX_OFFLOAD_IPV4_CKSUM |
2193                 DEV_RX_OFFLOAD_UDP_CKSUM  |
2194                 DEV_RX_OFFLOAD_TCP_CKSUM;
2195         dev_info->tx_offload_capa =
2196                 DEV_TX_OFFLOAD_VLAN_INSERT |
2197                 DEV_TX_OFFLOAD_IPV4_CKSUM  |
2198                 DEV_TX_OFFLOAD_UDP_CKSUM   |
2199                 DEV_TX_OFFLOAD_TCP_CKSUM   |
2200                 DEV_TX_OFFLOAD_SCTP_CKSUM  |
2201                 DEV_TX_OFFLOAD_TCP_TSO;
2202
2203         switch (hw->mac.type) {
2204         case e1000_82575:
2205                 dev_info->max_rx_queues = 4;
2206                 dev_info->max_tx_queues = 4;
2207                 dev_info->max_vmdq_pools = 0;
2208                 break;
2209
2210         case e1000_82576:
2211                 dev_info->max_rx_queues = 16;
2212                 dev_info->max_tx_queues = 16;
2213                 dev_info->max_vmdq_pools = ETH_8_POOLS;
2214                 dev_info->vmdq_queue_num = 16;
2215                 break;
2216
2217         case e1000_82580:
2218                 dev_info->max_rx_queues = 8;
2219                 dev_info->max_tx_queues = 8;
2220                 dev_info->max_vmdq_pools = ETH_8_POOLS;
2221                 dev_info->vmdq_queue_num = 8;
2222                 break;
2223
2224         case e1000_i350:
2225                 dev_info->max_rx_queues = 8;
2226                 dev_info->max_tx_queues = 8;
2227                 dev_info->max_vmdq_pools = ETH_8_POOLS;
2228                 dev_info->vmdq_queue_num = 8;
2229                 break;
2230
2231         case e1000_i354:
2232                 dev_info->max_rx_queues = 8;
2233                 dev_info->max_tx_queues = 8;
2234                 break;
2235
2236         case e1000_i210:
2237                 dev_info->max_rx_queues = 4;
2238                 dev_info->max_tx_queues = 4;
2239                 dev_info->max_vmdq_pools = 0;
2240                 break;
2241
2242         case e1000_i211:
2243                 dev_info->max_rx_queues = 2;
2244                 dev_info->max_tx_queues = 2;
2245                 dev_info->max_vmdq_pools = 0;
2246                 break;
2247
2248         default:
2249                 /* Should not happen */
2250                 break;
2251         }
2252         dev_info->hash_key_size = IGB_HKEY_MAX_INDEX * sizeof(uint32_t);
2253         dev_info->reta_size = ETH_RSS_RETA_SIZE_128;
2254         dev_info->flow_type_rss_offloads = IGB_RSS_OFFLOAD_ALL;
2255
2256         dev_info->default_rxconf = (struct rte_eth_rxconf) {
2257                 .rx_thresh = {
2258                         .pthresh = IGB_DEFAULT_RX_PTHRESH,
2259                         .hthresh = IGB_DEFAULT_RX_HTHRESH,
2260                         .wthresh = IGB_DEFAULT_RX_WTHRESH,
2261                 },
2262                 .rx_free_thresh = IGB_DEFAULT_RX_FREE_THRESH,
2263                 .rx_drop_en = 0,
2264         };
2265
2266         dev_info->default_txconf = (struct rte_eth_txconf) {
2267                 .tx_thresh = {
2268                         .pthresh = IGB_DEFAULT_TX_PTHRESH,
2269                         .hthresh = IGB_DEFAULT_TX_HTHRESH,
2270                         .wthresh = IGB_DEFAULT_TX_WTHRESH,
2271                 },
2272                 .txq_flags = 0,
2273         };
2274
2275         dev_info->rx_desc_lim = rx_desc_lim;
2276         dev_info->tx_desc_lim = tx_desc_lim;
2277
2278         dev_info->speed_capa = ETH_LINK_SPEED_10M_HD | ETH_LINK_SPEED_10M |
2279                         ETH_LINK_SPEED_100M_HD | ETH_LINK_SPEED_100M |
2280                         ETH_LINK_SPEED_1G;
2281 }
2282
2283 static const uint32_t *
2284 eth_igb_supported_ptypes_get(struct rte_eth_dev *dev)
2285 {
2286         static const uint32_t ptypes[] = {
2287                 /* refers to igb_rxd_pkt_info_to_pkt_type() */
2288                 RTE_PTYPE_L2_ETHER,
2289                 RTE_PTYPE_L3_IPV4,
2290                 RTE_PTYPE_L3_IPV4_EXT,
2291                 RTE_PTYPE_L3_IPV6,
2292                 RTE_PTYPE_L3_IPV6_EXT,
2293                 RTE_PTYPE_L4_TCP,
2294                 RTE_PTYPE_L4_UDP,
2295                 RTE_PTYPE_L4_SCTP,
2296                 RTE_PTYPE_TUNNEL_IP,
2297                 RTE_PTYPE_INNER_L3_IPV6,
2298                 RTE_PTYPE_INNER_L3_IPV6_EXT,
2299                 RTE_PTYPE_INNER_L4_TCP,
2300                 RTE_PTYPE_INNER_L4_UDP,
2301                 RTE_PTYPE_UNKNOWN
2302         };
2303
2304         if (dev->rx_pkt_burst == eth_igb_recv_pkts ||
2305             dev->rx_pkt_burst == eth_igb_recv_scattered_pkts)
2306                 return ptypes;
2307         return NULL;
2308 }
2309
2310 static void
2311 eth_igbvf_infos_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *dev_info)
2312 {
2313         struct e1000_hw *hw = E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2314
2315         dev_info->pci_dev = RTE_ETH_DEV_TO_PCI(dev);
2316         dev_info->min_rx_bufsize = 256; /* See BSIZE field of RCTL register. */
2317         dev_info->max_rx_pktlen  = 0x3FFF; /* See RLPML register. */
2318         dev_info->max_mac_addrs = hw->mac.rar_entry_count;
2319         dev_info->rx_offload_capa = DEV_RX_OFFLOAD_VLAN_STRIP |
2320                                 DEV_RX_OFFLOAD_IPV4_CKSUM |
2321                                 DEV_RX_OFFLOAD_UDP_CKSUM  |
2322                                 DEV_RX_OFFLOAD_TCP_CKSUM;
2323         dev_info->tx_offload_capa = DEV_TX_OFFLOAD_VLAN_INSERT |
2324                                 DEV_TX_OFFLOAD_IPV4_CKSUM  |
2325                                 DEV_TX_OFFLOAD_UDP_CKSUM   |
2326                                 DEV_TX_OFFLOAD_TCP_CKSUM   |
2327                                 DEV_TX_OFFLOAD_SCTP_CKSUM  |
2328                                 DEV_TX_OFFLOAD_TCP_TSO;
2329         switch (hw->mac.type) {
2330         case e1000_vfadapt:
2331                 dev_info->max_rx_queues = 2;
2332                 dev_info->max_tx_queues = 2;
2333                 break;
2334         case e1000_vfadapt_i350:
2335                 dev_info->max_rx_queues = 1;
2336                 dev_info->max_tx_queues = 1;
2337                 break;
2338         default:
2339                 /* Should not happen */
2340                 break;
2341         }
2342
2343         dev_info->default_rxconf = (struct rte_eth_rxconf) {
2344                 .rx_thresh = {
2345                         .pthresh = IGB_DEFAULT_RX_PTHRESH,
2346                         .hthresh = IGB_DEFAULT_RX_HTHRESH,
2347                         .wthresh = IGB_DEFAULT_RX_WTHRESH,
2348                 },
2349                 .rx_free_thresh = IGB_DEFAULT_RX_FREE_THRESH,
2350                 .rx_drop_en = 0,
2351         };
2352
2353         dev_info->default_txconf = (struct rte_eth_txconf) {
2354                 .tx_thresh = {
2355                         .pthresh = IGB_DEFAULT_TX_PTHRESH,
2356                         .hthresh = IGB_DEFAULT_TX_HTHRESH,
2357                         .wthresh = IGB_DEFAULT_TX_WTHRESH,
2358                 },
2359                 .txq_flags = 0,
2360         };
2361
2362         dev_info->rx_desc_lim = rx_desc_lim;
2363         dev_info->tx_desc_lim = tx_desc_lim;
2364 }
2365
2366 /* return 0 means link status changed, -1 means not changed */
2367 static int
2368 eth_igb_link_update(struct rte_eth_dev *dev, int wait_to_complete)
2369 {
2370         struct e1000_hw *hw =
2371                 E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2372         struct rte_eth_link link, old;
2373         int link_check, count;
2374
2375         link_check = 0;
2376         hw->mac.get_link_status = 1;
2377
2378         /* possible wait-to-complete in up to 9 seconds */
2379         for (count = 0; count < IGB_LINK_UPDATE_CHECK_TIMEOUT; count ++) {
2380                 /* Read the real link status */
2381                 switch (hw->phy.media_type) {
2382                 case e1000_media_type_copper:
2383                         /* Do the work to read phy */
2384                         e1000_check_for_link(hw);
2385                         link_check = !hw->mac.get_link_status;
2386                         break;
2387
2388                 case e1000_media_type_fiber:
2389                         e1000_check_for_link(hw);
2390                         link_check = (E1000_READ_REG(hw, E1000_STATUS) &
2391                                       E1000_STATUS_LU);
2392                         break;
2393
2394                 case e1000_media_type_internal_serdes:
2395                         e1000_check_for_link(hw);
2396                         link_check = hw->mac.serdes_has_link;
2397                         break;
2398
2399                 /* VF device is type_unknown */
2400                 case e1000_media_type_unknown:
2401                         eth_igbvf_link_update(hw);
2402                         link_check = !hw->mac.get_link_status;
2403                         break;
2404
2405                 default:
2406                         break;
2407                 }
2408                 if (link_check || wait_to_complete == 0)
2409                         break;
2410                 rte_delay_ms(IGB_LINK_UPDATE_CHECK_INTERVAL);
2411         }
2412         memset(&link, 0, sizeof(link));
2413         rte_igb_dev_atomic_read_link_status(dev, &link);
2414         old = link;
2415
2416         /* Now we check if a transition has happened */
2417         if (link_check) {
2418                 uint16_t duplex, speed;
2419                 hw->mac.ops.get_link_up_info(hw, &speed, &duplex);
2420                 link.link_duplex = (duplex == FULL_DUPLEX) ?
2421                                 ETH_LINK_FULL_DUPLEX :
2422                                 ETH_LINK_HALF_DUPLEX;
2423                 link.link_speed = speed;
2424                 link.link_status = ETH_LINK_UP;
2425                 link.link_autoneg = !(dev->data->dev_conf.link_speeds &
2426                                 ETH_LINK_SPEED_FIXED);
2427         } else if (!link_check) {
2428                 link.link_speed = 0;
2429                 link.link_duplex = ETH_LINK_HALF_DUPLEX;
2430                 link.link_status = ETH_LINK_DOWN;
2431                 link.link_autoneg = ETH_LINK_SPEED_FIXED;
2432         }
2433         rte_igb_dev_atomic_write_link_status(dev, &link);
2434
2435         /* not changed */
2436         if (old.link_status == link.link_status)
2437                 return -1;
2438
2439         /* changed */
2440         return 0;
2441 }
2442
2443 /*
2444  * igb_hw_control_acquire sets CTRL_EXT:DRV_LOAD bit.
2445  * For ASF and Pass Through versions of f/w this means
2446  * that the driver is loaded.
2447  */
2448 static void
2449 igb_hw_control_acquire(struct e1000_hw *hw)
2450 {
2451         uint32_t ctrl_ext;
2452
2453         /* Let firmware know the driver has taken over */
2454         ctrl_ext = E1000_READ_REG(hw, E1000_CTRL_EXT);
2455         E1000_WRITE_REG(hw, E1000_CTRL_EXT, ctrl_ext | E1000_CTRL_EXT_DRV_LOAD);
2456 }
2457
2458 /*
2459  * igb_hw_control_release resets CTRL_EXT:DRV_LOAD bit.
2460  * For ASF and Pass Through versions of f/w this means that the
2461  * driver is no longer loaded.
2462  */
2463 static void
2464 igb_hw_control_release(struct e1000_hw *hw)
2465 {
2466         uint32_t ctrl_ext;
2467
2468         /* Let firmware taken over control of h/w */
2469         ctrl_ext = E1000_READ_REG(hw, E1000_CTRL_EXT);
2470         E1000_WRITE_REG(hw, E1000_CTRL_EXT,
2471                         ctrl_ext & ~E1000_CTRL_EXT_DRV_LOAD);
2472 }
2473
2474 /*
2475  * Bit of a misnomer, what this really means is
2476  * to enable OS management of the system... aka
2477  * to disable special hardware management features.
2478  */
2479 static void
2480 igb_init_manageability(struct e1000_hw *hw)
2481 {
2482         if (e1000_enable_mng_pass_thru(hw)) {
2483                 uint32_t manc2h = E1000_READ_REG(hw, E1000_MANC2H);
2484                 uint32_t manc = E1000_READ_REG(hw, E1000_MANC);
2485
2486                 /* disable hardware interception of ARP */
2487                 manc &= ~(E1000_MANC_ARP_EN);
2488
2489                 /* enable receiving management packets to the host */
2490                 manc |= E1000_MANC_EN_MNG2HOST;
2491                 manc2h |= 1 << 5;  /* Mng Port 623 */
2492                 manc2h |= 1 << 6;  /* Mng Port 664 */
2493                 E1000_WRITE_REG(hw, E1000_MANC2H, manc2h);
2494                 E1000_WRITE_REG(hw, E1000_MANC, manc);
2495         }
2496 }
2497
2498 static void
2499 igb_release_manageability(struct e1000_hw *hw)
2500 {
2501         if (e1000_enable_mng_pass_thru(hw)) {
2502                 uint32_t manc = E1000_READ_REG(hw, E1000_MANC);
2503
2504                 manc |= E1000_MANC_ARP_EN;
2505                 manc &= ~E1000_MANC_EN_MNG2HOST;
2506
2507                 E1000_WRITE_REG(hw, E1000_MANC, manc);
2508         }
2509 }
2510
2511 static void
2512 eth_igb_promiscuous_enable(struct rte_eth_dev *dev)
2513 {
2514         struct e1000_hw *hw =
2515                 E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2516         uint32_t rctl;
2517
2518         rctl = E1000_READ_REG(hw, E1000_RCTL);
2519         rctl |= (E1000_RCTL_UPE | E1000_RCTL_MPE);
2520         E1000_WRITE_REG(hw, E1000_RCTL, rctl);
2521 }
2522
2523 static void
2524 eth_igb_promiscuous_disable(struct rte_eth_dev *dev)
2525 {
2526         struct e1000_hw *hw =
2527                 E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2528         uint32_t rctl;
2529
2530         rctl = E1000_READ_REG(hw, E1000_RCTL);
2531         rctl &= (~E1000_RCTL_UPE);
2532         if (dev->data->all_multicast == 1)
2533                 rctl |= E1000_RCTL_MPE;
2534         else
2535                 rctl &= (~E1000_RCTL_MPE);
2536         E1000_WRITE_REG(hw, E1000_RCTL, rctl);
2537 }
2538
2539 static void
2540 eth_igb_allmulticast_enable(struct rte_eth_dev *dev)
2541 {
2542         struct e1000_hw *hw =
2543                 E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2544         uint32_t rctl;
2545
2546         rctl = E1000_READ_REG(hw, E1000_RCTL);
2547         rctl |= E1000_RCTL_MPE;
2548         E1000_WRITE_REG(hw, E1000_RCTL, rctl);
2549 }
2550
2551 static void
2552 eth_igb_allmulticast_disable(struct rte_eth_dev *dev)
2553 {
2554         struct e1000_hw *hw =
2555                 E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2556         uint32_t rctl;
2557
2558         if (dev->data->promiscuous == 1)
2559                 return; /* must remain in all_multicast mode */
2560         rctl = E1000_READ_REG(hw, E1000_RCTL);
2561         rctl &= (~E1000_RCTL_MPE);
2562         E1000_WRITE_REG(hw, E1000_RCTL, rctl);
2563 }
2564
2565 static int
2566 eth_igb_vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
2567 {
2568         struct e1000_hw *hw =
2569                 E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2570         struct e1000_vfta * shadow_vfta =
2571                 E1000_DEV_PRIVATE_TO_VFTA(dev->data->dev_private);
2572         uint32_t vfta;
2573         uint32_t vid_idx;
2574         uint32_t vid_bit;
2575
2576         vid_idx = (uint32_t) ((vlan_id >> E1000_VFTA_ENTRY_SHIFT) &
2577                               E1000_VFTA_ENTRY_MASK);
2578         vid_bit = (uint32_t) (1 << (vlan_id & E1000_VFTA_ENTRY_BIT_SHIFT_MASK));
2579         vfta = E1000_READ_REG_ARRAY(hw, E1000_VFTA, vid_idx);
2580         if (on)
2581                 vfta |= vid_bit;
2582         else
2583                 vfta &= ~vid_bit;
2584         E1000_WRITE_REG_ARRAY(hw, E1000_VFTA, vid_idx, vfta);
2585
2586         /* update local VFTA copy */
2587         shadow_vfta->vfta[vid_idx] = vfta;
2588
2589         return 0;
2590 }
2591
2592 static int
2593 eth_igb_vlan_tpid_set(struct rte_eth_dev *dev,
2594                       enum rte_vlan_type vlan_type,
2595                       uint16_t tpid)
2596 {
2597         struct e1000_hw *hw =
2598                 E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2599         uint32_t reg, qinq;
2600
2601         qinq = E1000_READ_REG(hw, E1000_CTRL_EXT);
2602         qinq &= E1000_CTRL_EXT_EXT_VLAN;
2603
2604         /* only outer TPID of double VLAN can be configured*/
2605         if (qinq && vlan_type == ETH_VLAN_TYPE_OUTER) {
2606                 reg = E1000_READ_REG(hw, E1000_VET);
2607                 reg = (reg & (~E1000_VET_VET_EXT)) |
2608                         ((uint32_t)tpid << E1000_VET_VET_EXT_SHIFT);
2609                 E1000_WRITE_REG(hw, E1000_VET, reg);
2610
2611                 return 0;
2612         }
2613
2614         /* all other TPID values are read-only*/
2615         PMD_DRV_LOG(ERR, "Not supported");
2616
2617         return -ENOTSUP;
2618 }
2619
2620 static void
2621 igb_vlan_hw_filter_disable(struct rte_eth_dev *dev)
2622 {
2623         struct e1000_hw *hw =
2624                 E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2625         uint32_t reg;
2626
2627         /* Filter Table Disable */
2628         reg = E1000_READ_REG(hw, E1000_RCTL);
2629         reg &= ~E1000_RCTL_CFIEN;
2630         reg &= ~E1000_RCTL_VFE;
2631         E1000_WRITE_REG(hw, E1000_RCTL, reg);
2632 }
2633
2634 static void
2635 igb_vlan_hw_filter_enable(struct rte_eth_dev *dev)
2636 {
2637         struct e1000_hw *hw =
2638                 E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2639         struct e1000_vfta * shadow_vfta =
2640                 E1000_DEV_PRIVATE_TO_VFTA(dev->data->dev_private);
2641         uint32_t reg;
2642         int i;
2643
2644         /* Filter Table Enable, CFI not used for packet acceptance */
2645         reg = E1000_READ_REG(hw, E1000_RCTL);
2646         reg &= ~E1000_RCTL_CFIEN;
2647         reg |= E1000_RCTL_VFE;
2648         E1000_WRITE_REG(hw, E1000_RCTL, reg);
2649
2650         /* restore VFTA table */
2651         for (i = 0; i < IGB_VFTA_SIZE; i++)
2652                 E1000_WRITE_REG_ARRAY(hw, E1000_VFTA, i, shadow_vfta->vfta[i]);
2653 }
2654
2655 static void
2656 igb_vlan_hw_strip_disable(struct rte_eth_dev *dev)
2657 {
2658         struct e1000_hw *hw =
2659                 E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2660         uint32_t reg;
2661
2662         /* VLAN Mode Disable */
2663         reg = E1000_READ_REG(hw, E1000_CTRL);
2664         reg &= ~E1000_CTRL_VME;
2665         E1000_WRITE_REG(hw, E1000_CTRL, reg);
2666 }
2667
2668 static void
2669 igb_vlan_hw_strip_enable(struct rte_eth_dev *dev)
2670 {
2671         struct e1000_hw *hw =
2672                 E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2673         uint32_t reg;
2674
2675         /* VLAN Mode Enable */
2676         reg = E1000_READ_REG(hw, E1000_CTRL);
2677         reg |= E1000_CTRL_VME;
2678         E1000_WRITE_REG(hw, E1000_CTRL, reg);
2679 }
2680
2681 static void
2682 igb_vlan_hw_extend_disable(struct rte_eth_dev *dev)
2683 {
2684         struct e1000_hw *hw =
2685                 E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2686         uint32_t reg;
2687
2688         /* CTRL_EXT: Extended VLAN */
2689         reg = E1000_READ_REG(hw, E1000_CTRL_EXT);
2690         reg &= ~E1000_CTRL_EXT_EXTEND_VLAN;
2691         E1000_WRITE_REG(hw, E1000_CTRL_EXT, reg);
2692
2693         /* Update maximum packet length */
2694         if (dev->data->dev_conf.rxmode.jumbo_frame == 1)
2695                 E1000_WRITE_REG(hw, E1000_RLPML,
2696                         dev->data->dev_conf.rxmode.max_rx_pkt_len +
2697                                                 VLAN_TAG_SIZE);
2698 }
2699
2700 static void
2701 igb_vlan_hw_extend_enable(struct rte_eth_dev *dev)
2702 {
2703         struct e1000_hw *hw =
2704                 E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2705         uint32_t reg;
2706
2707         /* CTRL_EXT: Extended VLAN */
2708         reg = E1000_READ_REG(hw, E1000_CTRL_EXT);
2709         reg |= E1000_CTRL_EXT_EXTEND_VLAN;
2710         E1000_WRITE_REG(hw, E1000_CTRL_EXT, reg);
2711
2712         /* Update maximum packet length */
2713         if (dev->data->dev_conf.rxmode.jumbo_frame == 1)
2714                 E1000_WRITE_REG(hw, E1000_RLPML,
2715                         dev->data->dev_conf.rxmode.max_rx_pkt_len +
2716                                                 2 * VLAN_TAG_SIZE);
2717 }
2718
2719 static void
2720 eth_igb_vlan_offload_set(struct rte_eth_dev *dev, int mask)
2721 {
2722         if(mask & ETH_VLAN_STRIP_MASK){
2723                 if (dev->data->dev_conf.rxmode.hw_vlan_strip)
2724                         igb_vlan_hw_strip_enable(dev);
2725                 else
2726                         igb_vlan_hw_strip_disable(dev);
2727         }
2728
2729         if(mask & ETH_VLAN_FILTER_MASK){
2730                 if (dev->data->dev_conf.rxmode.hw_vlan_filter)
2731                         igb_vlan_hw_filter_enable(dev);
2732                 else
2733                         igb_vlan_hw_filter_disable(dev);
2734         }
2735
2736         if(mask & ETH_VLAN_EXTEND_MASK){
2737                 if (dev->data->dev_conf.rxmode.hw_vlan_extend)
2738                         igb_vlan_hw_extend_enable(dev);
2739                 else
2740                         igb_vlan_hw_extend_disable(dev);
2741         }
2742 }
2743
2744
2745 /**
2746  * It enables the interrupt mask and then enable the interrupt.
2747  *
2748  * @param dev
2749  *  Pointer to struct rte_eth_dev.
2750  *
2751  * @return
2752  *  - On success, zero.
2753  *  - On failure, a negative value.
2754  */
2755 static int
2756 eth_igb_lsc_interrupt_setup(struct rte_eth_dev *dev)
2757 {
2758         struct e1000_interrupt *intr =
2759                 E1000_DEV_PRIVATE_TO_INTR(dev->data->dev_private);
2760
2761         intr->mask |= E1000_ICR_LSC;
2762
2763         return 0;
2764 }
2765
2766 /* It clears the interrupt causes and enables the interrupt.
2767  * It will be called once only during nic initialized.
2768  *
2769  * @param dev
2770  *  Pointer to struct rte_eth_dev.
2771  *
2772  * @return
2773  *  - On success, zero.
2774  *  - On failure, a negative value.
2775  */
2776 static int eth_igb_rxq_interrupt_setup(struct rte_eth_dev *dev)
2777 {
2778         uint32_t mask, regval;
2779         struct e1000_hw *hw =
2780                 E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2781         struct rte_eth_dev_info dev_info;
2782
2783         memset(&dev_info, 0, sizeof(dev_info));
2784         eth_igb_infos_get(dev, &dev_info);
2785
2786         mask = 0xFFFFFFFF >> (32 - dev_info.max_rx_queues);
2787         regval = E1000_READ_REG(hw, E1000_EIMS);
2788         E1000_WRITE_REG(hw, E1000_EIMS, regval | mask);
2789
2790         return 0;
2791 }
2792
2793 /*
2794  * It reads ICR and gets interrupt causes, check it and set a bit flag
2795  * to update link status.
2796  *
2797  * @param dev
2798  *  Pointer to struct rte_eth_dev.
2799  *
2800  * @return
2801  *  - On success, zero.
2802  *  - On failure, a negative value.
2803  */
2804 static int
2805 eth_igb_interrupt_get_status(struct rte_eth_dev *dev)
2806 {
2807         uint32_t icr;
2808         struct e1000_hw *hw =
2809                 E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2810         struct e1000_interrupt *intr =
2811                 E1000_DEV_PRIVATE_TO_INTR(dev->data->dev_private);
2812
2813         igb_intr_disable(hw);
2814
2815         /* read-on-clear nic registers here */
2816         icr = E1000_READ_REG(hw, E1000_ICR);
2817
2818         intr->flags = 0;
2819         if (icr & E1000_ICR_LSC) {
2820                 intr->flags |= E1000_FLAG_NEED_LINK_UPDATE;
2821         }
2822
2823         if (icr & E1000_ICR_VMMB)
2824                 intr->flags |= E1000_FLAG_MAILBOX;
2825
2826         return 0;
2827 }
2828
2829 /*
2830  * It executes link_update after knowing an interrupt is prsent.
2831  *
2832  * @param dev
2833  *  Pointer to struct rte_eth_dev.
2834  *
2835  * @return
2836  *  - On success, zero.
2837  *  - On failure, a negative value.
2838  */
2839 static int
2840 eth_igb_interrupt_action(struct rte_eth_dev *dev,
2841                          struct rte_intr_handle *intr_handle)
2842 {
2843         struct e1000_hw *hw =
2844                 E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2845         struct e1000_interrupt *intr =
2846                 E1000_DEV_PRIVATE_TO_INTR(dev->data->dev_private);
2847         struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
2848         uint32_t tctl, rctl;
2849         struct rte_eth_link link;
2850         int ret;
2851
2852         if (intr->flags & E1000_FLAG_MAILBOX) {
2853                 igb_pf_mbx_process(dev);
2854                 intr->flags &= ~E1000_FLAG_MAILBOX;
2855         }
2856
2857         igb_intr_enable(dev);
2858         rte_intr_enable(intr_handle);
2859
2860         if (intr->flags & E1000_FLAG_NEED_LINK_UPDATE) {
2861                 intr->flags &= ~E1000_FLAG_NEED_LINK_UPDATE;
2862
2863                 /* set get_link_status to check register later */
2864                 hw->mac.get_link_status = 1;
2865                 ret = eth_igb_link_update(dev, 0);
2866
2867                 /* check if link has changed */
2868                 if (ret < 0)
2869                         return 0;
2870
2871                 memset(&link, 0, sizeof(link));
2872                 rte_igb_dev_atomic_read_link_status(dev, &link);
2873                 if (link.link_status) {
2874                         PMD_INIT_LOG(INFO,
2875                                      " Port %d: Link Up - speed %u Mbps - %s",
2876                                      dev->data->port_id,
2877                                      (unsigned)link.link_speed,
2878                                      link.link_duplex == ETH_LINK_FULL_DUPLEX ?
2879                                      "full-duplex" : "half-duplex");
2880                 } else {
2881                         PMD_INIT_LOG(INFO, " Port %d: Link Down",
2882                                      dev->data->port_id);
2883                 }
2884
2885                 PMD_INIT_LOG(DEBUG, "PCI Address: %04d:%02d:%02d:%d",
2886                              pci_dev->addr.domain,
2887                              pci_dev->addr.bus,
2888                              pci_dev->addr.devid,
2889                              pci_dev->addr.function);
2890                 tctl = E1000_READ_REG(hw, E1000_TCTL);
2891                 rctl = E1000_READ_REG(hw, E1000_RCTL);
2892                 if (link.link_status) {
2893                         /* enable Tx/Rx */
2894                         tctl |= E1000_TCTL_EN;
2895                         rctl |= E1000_RCTL_EN;
2896                 } else {
2897                         /* disable Tx/Rx */
2898                         tctl &= ~E1000_TCTL_EN;
2899                         rctl &= ~E1000_RCTL_EN;
2900                 }
2901                 E1000_WRITE_REG(hw, E1000_TCTL, tctl);
2902                 E1000_WRITE_REG(hw, E1000_RCTL, rctl);
2903                 E1000_WRITE_FLUSH(hw);
2904                 _rte_eth_dev_callback_process(dev, RTE_ETH_EVENT_INTR_LSC, NULL);
2905         }
2906
2907         return 0;
2908 }
2909
2910 /**
2911  * Interrupt handler which shall be registered at first.
2912  *
2913  * @param handle
2914  *  Pointer to interrupt handle.
2915  * @param param
2916  *  The address of parameter (struct rte_eth_dev *) regsitered before.
2917  *
2918  * @return
2919  *  void
2920  */
2921 static void
2922 eth_igb_interrupt_handler(void *param)
2923 {
2924         struct rte_eth_dev *dev = (struct rte_eth_dev *)param;
2925
2926         eth_igb_interrupt_get_status(dev);
2927         eth_igb_interrupt_action(dev, dev->intr_handle);
2928 }
2929
2930 static int
2931 eth_igbvf_interrupt_get_status(struct rte_eth_dev *dev)
2932 {
2933         uint32_t eicr;
2934         struct e1000_hw *hw =
2935                 E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2936         struct e1000_interrupt *intr =
2937                 E1000_DEV_PRIVATE_TO_INTR(dev->data->dev_private);
2938
2939         igbvf_intr_disable(hw);
2940
2941         /* read-on-clear nic registers here */
2942         eicr = E1000_READ_REG(hw, E1000_EICR);
2943         intr->flags = 0;
2944
2945         if (eicr == E1000_VTIVAR_MISC_MAILBOX)
2946                 intr->flags |= E1000_FLAG_MAILBOX;
2947
2948         return 0;
2949 }
2950
2951 void igbvf_mbx_process(struct rte_eth_dev *dev)
2952 {
2953         struct e1000_hw *hw =
2954                 E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2955         struct e1000_mbx_info *mbx = &hw->mbx;
2956         u32 in_msg = 0;
2957
2958         if (mbx->ops.read(hw, &in_msg, 1, 0))
2959                 return;
2960
2961         /* PF reset VF event */
2962         if (in_msg == E1000_PF_CONTROL_MSG)
2963                 _rte_eth_dev_callback_process(dev, RTE_ETH_EVENT_INTR_RESET, NULL);
2964 }
2965
2966 static int
2967 eth_igbvf_interrupt_action(struct rte_eth_dev *dev, struct rte_intr_handle *intr_handle)
2968 {
2969         struct e1000_interrupt *intr =
2970                 E1000_DEV_PRIVATE_TO_INTR(dev->data->dev_private);
2971
2972         if (intr->flags & E1000_FLAG_MAILBOX) {
2973                 igbvf_mbx_process(dev);
2974                 intr->flags &= ~E1000_FLAG_MAILBOX;
2975         }
2976
2977         igbvf_intr_enable(dev);
2978         rte_intr_enable(intr_handle);
2979
2980         return 0;
2981 }
2982
2983 static void
2984 eth_igbvf_interrupt_handler(void *param)
2985 {
2986         struct rte_eth_dev *dev = (struct rte_eth_dev *)param;
2987
2988         eth_igbvf_interrupt_get_status(dev);
2989         eth_igbvf_interrupt_action(dev, dev->intr_handle);
2990 }
2991
2992 static int
2993 eth_igb_led_on(struct rte_eth_dev *dev)
2994 {
2995         struct e1000_hw *hw;
2996
2997         hw = E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2998         return e1000_led_on(hw) == E1000_SUCCESS ? 0 : -ENOTSUP;
2999 }
3000
3001 static int
3002 eth_igb_led_off(struct rte_eth_dev *dev)
3003 {
3004         struct e1000_hw *hw;
3005
3006         hw = E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
3007         return e1000_led_off(hw) == E1000_SUCCESS ? 0 : -ENOTSUP;
3008 }
3009
3010 static int
3011 eth_igb_flow_ctrl_get(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
3012 {
3013         struct e1000_hw *hw;
3014         uint32_t ctrl;
3015         int tx_pause;
3016         int rx_pause;
3017
3018         hw = E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
3019         fc_conf->pause_time = hw->fc.pause_time;
3020         fc_conf->high_water = hw->fc.high_water;
3021         fc_conf->low_water = hw->fc.low_water;
3022         fc_conf->send_xon = hw->fc.send_xon;
3023         fc_conf->autoneg = hw->mac.autoneg;
3024
3025         /*
3026          * Return rx_pause and tx_pause status according to actual setting of
3027          * the TFCE and RFCE bits in the CTRL register.
3028          */
3029         ctrl = E1000_READ_REG(hw, E1000_CTRL);
3030         if (ctrl & E1000_CTRL_TFCE)
3031                 tx_pause = 1;
3032         else
3033                 tx_pause = 0;
3034
3035         if (ctrl & E1000_CTRL_RFCE)
3036                 rx_pause = 1;
3037         else
3038                 rx_pause = 0;
3039
3040         if (rx_pause && tx_pause)
3041                 fc_conf->mode = RTE_FC_FULL;
3042         else if (rx_pause)
3043                 fc_conf->mode = RTE_FC_RX_PAUSE;
3044         else if (tx_pause)
3045                 fc_conf->mode = RTE_FC_TX_PAUSE;
3046         else
3047                 fc_conf->mode = RTE_FC_NONE;
3048
3049         return 0;
3050 }
3051
3052 static int
3053 eth_igb_flow_ctrl_set(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
3054 {
3055         struct e1000_hw *hw;
3056         int err;
3057         enum e1000_fc_mode rte_fcmode_2_e1000_fcmode[] = {
3058                 e1000_fc_none,
3059                 e1000_fc_rx_pause,
3060                 e1000_fc_tx_pause,
3061                 e1000_fc_full
3062         };
3063         uint32_t rx_buf_size;
3064         uint32_t max_high_water;
3065         uint32_t rctl;
3066
3067         hw = E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
3068         if (fc_conf->autoneg != hw->mac.autoneg)
3069                 return -ENOTSUP;
3070         rx_buf_size = igb_get_rx_buffer_size(hw);
3071         PMD_INIT_LOG(DEBUG, "Rx packet buffer size = 0x%x", rx_buf_size);
3072
3073         /* At least reserve one Ethernet frame for watermark */
3074         max_high_water = rx_buf_size - ETHER_MAX_LEN;
3075         if ((fc_conf->high_water > max_high_water) ||
3076             (fc_conf->high_water < fc_conf->low_water)) {
3077                 PMD_INIT_LOG(ERR, "e1000 incorrect high/low water value");
3078                 PMD_INIT_LOG(ERR, "high water must <=  0x%x", max_high_water);
3079                 return -EINVAL;
3080         }
3081
3082         hw->fc.requested_mode = rte_fcmode_2_e1000_fcmode[fc_conf->mode];
3083         hw->fc.pause_time     = fc_conf->pause_time;
3084         hw->fc.high_water     = fc_conf->high_water;
3085         hw->fc.low_water      = fc_conf->low_water;
3086         hw->fc.send_xon       = fc_conf->send_xon;
3087
3088         err = e1000_setup_link_generic(hw);
3089         if (err == E1000_SUCCESS) {
3090
3091                 /* check if we want to forward MAC frames - driver doesn't have native
3092                  * capability to do that, so we'll write the registers ourselves */
3093
3094                 rctl = E1000_READ_REG(hw, E1000_RCTL);
3095
3096                 /* set or clear MFLCN.PMCF bit depending on configuration */
3097                 if (fc_conf->mac_ctrl_frame_fwd != 0)
3098                         rctl |= E1000_RCTL_PMCF;
3099                 else
3100                         rctl &= ~E1000_RCTL_PMCF;
3101
3102                 E1000_WRITE_REG(hw, E1000_RCTL, rctl);
3103                 E1000_WRITE_FLUSH(hw);
3104
3105                 return 0;
3106         }
3107
3108         PMD_INIT_LOG(ERR, "e1000_setup_link_generic = 0x%x", err);
3109         return -EIO;
3110 }
3111
3112 #define E1000_RAH_POOLSEL_SHIFT      (18)
3113 static int
3114 eth_igb_rar_set(struct rte_eth_dev *dev, struct ether_addr *mac_addr,
3115                 uint32_t index, uint32_t pool)
3116 {
3117         struct e1000_hw *hw = E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
3118         uint32_t rah;
3119
3120         e1000_rar_set(hw, mac_addr->addr_bytes, index);
3121         rah = E1000_READ_REG(hw, E1000_RAH(index));
3122         rah |= (0x1 << (E1000_RAH_POOLSEL_SHIFT + pool));
3123         E1000_WRITE_REG(hw, E1000_RAH(index), rah);
3124         return 0;
3125 }
3126
3127 static void
3128 eth_igb_rar_clear(struct rte_eth_dev *dev, uint32_t index)
3129 {
3130         uint8_t addr[ETHER_ADDR_LEN];
3131         struct e1000_hw *hw = E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
3132
3133         memset(addr, 0, sizeof(addr));
3134
3135         e1000_rar_set(hw, addr, index);
3136 }
3137
3138 static void
3139 eth_igb_default_mac_addr_set(struct rte_eth_dev *dev,
3140                                 struct ether_addr *addr)
3141 {
3142         eth_igb_rar_clear(dev, 0);
3143
3144         eth_igb_rar_set(dev, (void *)addr, 0, 0);
3145 }
3146 /*
3147  * Virtual Function operations
3148  */
3149 static void
3150 igbvf_intr_disable(struct e1000_hw *hw)
3151 {
3152         PMD_INIT_FUNC_TRACE();
3153
3154         /* Clear interrupt mask to stop from interrupts being generated */
3155         E1000_WRITE_REG(hw, E1000_EIMC, 0xFFFF);
3156
3157         E1000_WRITE_FLUSH(hw);
3158 }
3159
3160 static void
3161 igbvf_stop_adapter(struct rte_eth_dev *dev)
3162 {
3163         u32 reg_val;
3164         u16 i;
3165         struct rte_eth_dev_info dev_info;
3166         struct e1000_hw *hw = E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
3167
3168         memset(&dev_info, 0, sizeof(dev_info));
3169         eth_igbvf_infos_get(dev, &dev_info);
3170
3171         /* Clear interrupt mask to stop from interrupts being generated */
3172         igbvf_intr_disable(hw);
3173
3174         /* Clear any pending interrupts, flush previous writes */
3175         E1000_READ_REG(hw, E1000_EICR);
3176
3177         /* Disable the transmit unit.  Each queue must be disabled. */
3178         for (i = 0; i < dev_info.max_tx_queues; i++)
3179                 E1000_WRITE_REG(hw, E1000_TXDCTL(i), E1000_TXDCTL_SWFLSH);
3180
3181         /* Disable the receive unit by stopping each queue */
3182         for (i = 0; i < dev_info.max_rx_queues; i++) {
3183                 reg_val = E1000_READ_REG(hw, E1000_RXDCTL(i));
3184                 reg_val &= ~E1000_RXDCTL_QUEUE_ENABLE;
3185                 E1000_WRITE_REG(hw, E1000_RXDCTL(i), reg_val);
3186                 while (E1000_READ_REG(hw, E1000_RXDCTL(i)) & E1000_RXDCTL_QUEUE_ENABLE)
3187                         ;
3188         }
3189
3190         /* flush all queues disables */
3191         E1000_WRITE_FLUSH(hw);
3192         msec_delay(2);
3193 }
3194
3195 static int eth_igbvf_link_update(struct e1000_hw *hw)
3196 {
3197         struct e1000_mbx_info *mbx = &hw->mbx;
3198         struct e1000_mac_info *mac = &hw->mac;
3199         int ret_val = E1000_SUCCESS;
3200
3201         PMD_INIT_LOG(DEBUG, "e1000_check_for_link_vf");
3202
3203         /*
3204          * We only want to run this if there has been a rst asserted.
3205          * in this case that could mean a link change, device reset,
3206          * or a virtual function reset
3207          */
3208
3209         /* If we were hit with a reset or timeout drop the link */
3210         if (!e1000_check_for_rst(hw, 0) || !mbx->timeout)
3211                 mac->get_link_status = TRUE;
3212
3213         if (!mac->get_link_status)
3214                 goto out;
3215
3216         /* if link status is down no point in checking to see if pf is up */
3217         if (!(E1000_READ_REG(hw, E1000_STATUS) & E1000_STATUS_LU))
3218                 goto out;
3219
3220         /* if we passed all the tests above then the link is up and we no
3221          * longer need to check for link */
3222         mac->get_link_status = FALSE;
3223
3224 out:
3225         return ret_val;
3226 }
3227
3228
3229 static int
3230 igbvf_dev_configure(struct rte_eth_dev *dev)
3231 {
3232         struct rte_eth_conf* conf = &dev->data->dev_conf;
3233
3234         PMD_INIT_LOG(DEBUG, "Configured Virtual Function port id: %d",
3235                      dev->data->port_id);
3236
3237         /*
3238          * VF has no ability to enable/disable HW CRC
3239          * Keep the persistent behavior the same as Host PF
3240          */
3241 #ifndef RTE_LIBRTE_E1000_PF_DISABLE_STRIP_CRC
3242         if (!conf->rxmode.hw_strip_crc) {
3243                 PMD_INIT_LOG(NOTICE, "VF can't disable HW CRC Strip");
3244                 conf->rxmode.hw_strip_crc = 1;
3245         }
3246 #else
3247         if (conf->rxmode.hw_strip_crc) {
3248                 PMD_INIT_LOG(NOTICE, "VF can't enable HW CRC Strip");
3249                 conf->rxmode.hw_strip_crc = 0;
3250         }
3251 #endif
3252
3253         return 0;
3254 }
3255
3256 static int
3257 igbvf_dev_start(struct rte_eth_dev *dev)
3258 {
3259         struct e1000_hw *hw =
3260                 E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
3261         struct e1000_adapter *adapter =
3262                 E1000_DEV_PRIVATE(dev->data->dev_private);
3263         struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
3264         struct rte_intr_handle *intr_handle = &pci_dev->intr_handle;
3265         int ret;
3266         uint32_t intr_vector = 0;
3267
3268         PMD_INIT_FUNC_TRACE();
3269
3270         hw->mac.ops.reset_hw(hw);
3271         adapter->stopped = 0;
3272
3273         /* Set all vfta */
3274         igbvf_set_vfta_all(dev,1);
3275
3276         eth_igbvf_tx_init(dev);
3277
3278         /* This can fail when allocating mbufs for descriptor rings */
3279         ret = eth_igbvf_rx_init(dev);
3280         if (ret) {
3281                 PMD_INIT_LOG(ERR, "Unable to initialize RX hardware");
3282                 igb_dev_clear_queues(dev);
3283                 return ret;
3284         }
3285
3286         /* check and configure queue intr-vector mapping */
3287         if (dev->data->dev_conf.intr_conf.rxq != 0) {
3288                 intr_vector = dev->data->nb_rx_queues;
3289                 ret = rte_intr_efd_enable(intr_handle, intr_vector);
3290                 if (ret)
3291                         return ret;
3292         }
3293
3294         if (rte_intr_dp_is_en(intr_handle) && !intr_handle->intr_vec) {
3295                 intr_handle->intr_vec =
3296                         rte_zmalloc("intr_vec",
3297                                     dev->data->nb_rx_queues * sizeof(int), 0);
3298                 if (!intr_handle->intr_vec) {
3299                         PMD_INIT_LOG(ERR, "Failed to allocate %d rx_queues"
3300                                      " intr_vec", dev->data->nb_rx_queues);
3301                         return -ENOMEM;
3302                 }
3303         }
3304
3305         eth_igbvf_configure_msix_intr(dev);
3306
3307         /* enable uio/vfio intr/eventfd mapping */
3308         rte_intr_enable(intr_handle);
3309
3310         /* resume enabled intr since hw reset */
3311         igbvf_intr_enable(dev);
3312
3313         return 0;
3314 }
3315
3316 static void
3317 igbvf_dev_stop(struct rte_eth_dev *dev)
3318 {
3319         struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
3320         struct rte_intr_handle *intr_handle = &pci_dev->intr_handle;
3321
3322         PMD_INIT_FUNC_TRACE();
3323
3324         igbvf_stop_adapter(dev);
3325
3326         /*
3327           * Clear what we set, but we still keep shadow_vfta to
3328           * restore after device starts
3329           */
3330         igbvf_set_vfta_all(dev,0);
3331
3332         igb_dev_clear_queues(dev);
3333
3334         /* disable intr eventfd mapping */
3335         rte_intr_disable(intr_handle);
3336
3337         /* Clean datapath event and queue/vec mapping */
3338         rte_intr_efd_disable(intr_handle);
3339         if (intr_handle->intr_vec) {
3340                 rte_free(intr_handle->intr_vec);
3341                 intr_handle->intr_vec = NULL;
3342         }
3343 }
3344
3345 static void
3346 igbvf_dev_close(struct rte_eth_dev *dev)
3347 {
3348         struct e1000_hw *hw = E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
3349         struct e1000_adapter *adapter =
3350                 E1000_DEV_PRIVATE(dev->data->dev_private);
3351         struct ether_addr addr;
3352
3353         PMD_INIT_FUNC_TRACE();
3354
3355         e1000_reset_hw(hw);
3356
3357         igbvf_dev_stop(dev);
3358         adapter->stopped = 1;
3359         igb_dev_free_queues(dev);
3360
3361         /**
3362          * reprogram the RAR with a zero mac address,
3363          * to ensure that the VF traffic goes to the PF
3364          * after stop, close and detach of the VF.
3365          **/
3366
3367         memset(&addr, 0, sizeof(addr));
3368         igbvf_default_mac_addr_set(dev, &addr);
3369 }
3370
3371 static void
3372 igbvf_promiscuous_enable(struct rte_eth_dev *dev)
3373 {
3374         struct e1000_hw *hw = E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
3375
3376         /* Set both unicast and multicast promisc */
3377         e1000_promisc_set_vf(hw, e1000_promisc_enabled);
3378 }
3379
3380 static void
3381 igbvf_promiscuous_disable(struct rte_eth_dev *dev)
3382 {
3383         struct e1000_hw *hw = E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
3384
3385         /* If in allmulticast mode leave multicast promisc */
3386         if (dev->data->all_multicast == 1)
3387                 e1000_promisc_set_vf(hw, e1000_promisc_multicast);
3388         else
3389                 e1000_promisc_set_vf(hw, e1000_promisc_disabled);
3390 }
3391
3392 static void
3393 igbvf_allmulticast_enable(struct rte_eth_dev *dev)
3394 {
3395         struct e1000_hw *hw = E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
3396
3397         /* In promiscuous mode multicast promisc already set */
3398         if (dev->data->promiscuous == 0)
3399                 e1000_promisc_set_vf(hw, e1000_promisc_multicast);
3400 }
3401
3402 static void
3403 igbvf_allmulticast_disable(struct rte_eth_dev *dev)
3404 {
3405         struct e1000_hw *hw = E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
3406
3407         /* In promiscuous mode leave multicast promisc enabled */
3408         if (dev->data->promiscuous == 0)
3409                 e1000_promisc_set_vf(hw, e1000_promisc_disabled);
3410 }
3411
3412 static int igbvf_set_vfta(struct e1000_hw *hw, uint16_t vid, bool on)
3413 {
3414         struct e1000_mbx_info *mbx = &hw->mbx;
3415         uint32_t msgbuf[2];
3416         s32 err;
3417
3418         /* After set vlan, vlan strip will also be enabled in igb driver*/
3419         msgbuf[0] = E1000_VF_SET_VLAN;
3420         msgbuf[1] = vid;
3421         /* Setting the 8 bit field MSG INFO to TRUE indicates "add" */
3422         if (on)
3423                 msgbuf[0] |= E1000_VF_SET_VLAN_ADD;
3424
3425         err = mbx->ops.write_posted(hw, msgbuf, 2, 0);
3426         if (err)
3427                 goto mbx_err;
3428
3429         err = mbx->ops.read_posted(hw, msgbuf, 2, 0);
3430         if (err)
3431                 goto mbx_err;
3432
3433         msgbuf[0] &= ~E1000_VT_MSGTYPE_CTS;
3434         if (msgbuf[0] == (E1000_VF_SET_VLAN | E1000_VT_MSGTYPE_NACK))
3435                 err = -EINVAL;
3436
3437 mbx_err:
3438         return err;
3439 }
3440
3441 static void igbvf_set_vfta_all(struct rte_eth_dev *dev, bool on)
3442 {
3443         struct e1000_hw *hw =
3444                 E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
3445         struct e1000_vfta * shadow_vfta =
3446                 E1000_DEV_PRIVATE_TO_VFTA(dev->data->dev_private);
3447         int i = 0, j = 0, vfta = 0, mask = 1;
3448
3449         for (i = 0; i < IGB_VFTA_SIZE; i++){
3450                 vfta = shadow_vfta->vfta[i];
3451                 if(vfta){
3452                         mask = 1;
3453                         for (j = 0; j < 32; j++){
3454                                 if(vfta & mask)
3455                                         igbvf_set_vfta(hw,
3456                                                 (uint16_t)((i<<5)+j), on);
3457                                 mask<<=1;
3458                         }
3459                 }
3460         }
3461
3462 }
3463
3464 static int
3465 igbvf_vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
3466 {
3467         struct e1000_hw *hw =
3468                 E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
3469         struct e1000_vfta * shadow_vfta =
3470                 E1000_DEV_PRIVATE_TO_VFTA(dev->data->dev_private);
3471         uint32_t vid_idx = 0;
3472         uint32_t vid_bit = 0;
3473         int ret = 0;
3474
3475         PMD_INIT_FUNC_TRACE();
3476
3477         /*vind is not used in VF driver, set to 0, check ixgbe_set_vfta_vf*/
3478         ret = igbvf_set_vfta(hw, vlan_id, !!on);
3479         if(ret){
3480                 PMD_INIT_LOG(ERR, "Unable to set VF vlan");
3481                 return ret;
3482         }
3483         vid_idx = (uint32_t) ((vlan_id >> 5) & 0x7F);
3484         vid_bit = (uint32_t) (1 << (vlan_id & 0x1F));
3485
3486         /*Save what we set and retore it after device reset*/
3487         if (on)
3488                 shadow_vfta->vfta[vid_idx] |= vid_bit;
3489         else
3490                 shadow_vfta->vfta[vid_idx] &= ~vid_bit;
3491
3492         return 0;
3493 }
3494
3495 static void
3496 igbvf_default_mac_addr_set(struct rte_eth_dev *dev, struct ether_addr *addr)
3497 {
3498         struct e1000_hw *hw =
3499                 E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
3500
3501         /* index is not used by rar_set() */
3502         hw->mac.ops.rar_set(hw, (void *)addr, 0);
3503 }
3504
3505
3506 static int
3507 eth_igb_rss_reta_update(struct rte_eth_dev *dev,
3508                         struct rte_eth_rss_reta_entry64 *reta_conf,
3509                         uint16_t reta_size)
3510 {
3511         uint8_t i, j, mask;
3512         uint32_t reta, r;
3513         uint16_t idx, shift;
3514         struct e1000_hw *hw = E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
3515
3516         if (reta_size != ETH_RSS_RETA_SIZE_128) {
3517                 PMD_DRV_LOG(ERR, "The size of hash lookup table configured "
3518                         "(%d) doesn't match the number hardware can supported "
3519                         "(%d)", reta_size, ETH_RSS_RETA_SIZE_128);
3520                 return -EINVAL;
3521         }
3522
3523         for (i = 0; i < reta_size; i += IGB_4_BIT_WIDTH) {
3524                 idx = i / RTE_RETA_GROUP_SIZE;
3525                 shift = i % RTE_RETA_GROUP_SIZE;
3526                 mask = (uint8_t)((reta_conf[idx].mask >> shift) &
3527                                                 IGB_4_BIT_MASK);
3528                 if (!mask)
3529                         continue;
3530                 if (mask == IGB_4_BIT_MASK)
3531                         r = 0;
3532                 else
3533                         r = E1000_READ_REG(hw, E1000_RETA(i >> 2));
3534                 for (j = 0, reta = 0; j < IGB_4_BIT_WIDTH; j++) {
3535                         if (mask & (0x1 << j))
3536                                 reta |= reta_conf[idx].reta[shift + j] <<
3537                                                         (CHAR_BIT * j);
3538                         else
3539                                 reta |= r & (IGB_8_BIT_MASK << (CHAR_BIT * j));
3540                 }
3541                 E1000_WRITE_REG(hw, E1000_RETA(i >> 2), reta);
3542         }
3543
3544         return 0;
3545 }
3546
3547 static int
3548 eth_igb_rss_reta_query(struct rte_eth_dev *dev,
3549                        struct rte_eth_rss_reta_entry64 *reta_conf,
3550                        uint16_t reta_size)
3551 {
3552         uint8_t i, j, mask;
3553         uint32_t reta;
3554         uint16_t idx, shift;
3555         struct e1000_hw *hw = E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
3556
3557         if (reta_size != ETH_RSS_RETA_SIZE_128) {
3558                 PMD_DRV_LOG(ERR, "The size of hash lookup table configured "
3559                         "(%d) doesn't match the number hardware can supported "
3560                         "(%d)", reta_size, ETH_RSS_RETA_SIZE_128);
3561                 return -EINVAL;
3562         }
3563
3564         for (i = 0; i < reta_size; i += IGB_4_BIT_WIDTH) {
3565                 idx = i / RTE_RETA_GROUP_SIZE;
3566                 shift = i % RTE_RETA_GROUP_SIZE;
3567                 mask = (uint8_t)((reta_conf[idx].mask >> shift) &
3568                                                 IGB_4_BIT_MASK);
3569                 if (!mask)
3570                         continue;
3571                 reta = E1000_READ_REG(hw, E1000_RETA(i >> 2));
3572                 for (j = 0; j < IGB_4_BIT_WIDTH; j++) {
3573                         if (mask & (0x1 << j))
3574                                 reta_conf[idx].reta[shift + j] =
3575                                         ((reta >> (CHAR_BIT * j)) &
3576                                                 IGB_8_BIT_MASK);
3577                 }
3578         }
3579
3580         return 0;
3581 }
3582
3583 #define MAC_TYPE_FILTER_SUP(type)    do {\
3584         if ((type) != e1000_82580 && (type) != e1000_i350 &&\
3585                 (type) != e1000_82576 && (type) != e1000_i210 &&\
3586                 (type) != e1000_i211)\
3587                 return -ENOTSUP;\
3588 } while (0)
3589
3590 static int
3591 eth_igb_syn_filter_set(struct rte_eth_dev *dev,
3592                         struct rte_eth_syn_filter *filter,
3593                         bool add)
3594 {
3595         struct e1000_hw *hw = E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
3596         struct e1000_filter_info *filter_info =
3597                 E1000_DEV_PRIVATE_TO_FILTER_INFO(dev->data->dev_private);
3598         uint32_t synqf, rfctl;
3599
3600         if (filter->queue >= IGB_MAX_RX_QUEUE_NUM)
3601                 return -EINVAL;
3602
3603         synqf = E1000_READ_REG(hw, E1000_SYNQF(0));
3604
3605         if (add) {
3606                 if (synqf & E1000_SYN_FILTER_ENABLE)
3607                         return -EINVAL;
3608
3609                 synqf = (uint32_t)(((filter->queue << E1000_SYN_FILTER_QUEUE_SHIFT) &
3610                         E1000_SYN_FILTER_QUEUE) | E1000_SYN_FILTER_ENABLE);
3611
3612                 rfctl = E1000_READ_REG(hw, E1000_RFCTL);
3613                 if (filter->hig_pri)
3614                         rfctl |= E1000_RFCTL_SYNQFP;
3615                 else
3616                         rfctl &= ~E1000_RFCTL_SYNQFP;
3617
3618                 E1000_WRITE_REG(hw, E1000_RFCTL, rfctl);
3619         } else {
3620                 if (!(synqf & E1000_SYN_FILTER_ENABLE))
3621                         return -ENOENT;
3622                 synqf = 0;
3623         }
3624
3625         filter_info->syn_info = synqf;
3626         E1000_WRITE_REG(hw, E1000_SYNQF(0), synqf);
3627         E1000_WRITE_FLUSH(hw);
3628         return 0;
3629 }
3630
3631 static int
3632 eth_igb_syn_filter_get(struct rte_eth_dev *dev,
3633                         struct rte_eth_syn_filter *filter)
3634 {
3635         struct e1000_hw *hw = E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
3636         uint32_t synqf, rfctl;
3637
3638         synqf = E1000_READ_REG(hw, E1000_SYNQF(0));
3639         if (synqf & E1000_SYN_FILTER_ENABLE) {
3640                 rfctl = E1000_READ_REG(hw, E1000_RFCTL);
3641                 filter->hig_pri = (rfctl & E1000_RFCTL_SYNQFP) ? 1 : 0;
3642                 filter->queue = (uint8_t)((synqf & E1000_SYN_FILTER_QUEUE) >>
3643                                 E1000_SYN_FILTER_QUEUE_SHIFT);
3644                 return 0;
3645         }
3646
3647         return -ENOENT;
3648 }
3649
3650 static int
3651 eth_igb_syn_filter_handle(struct rte_eth_dev *dev,
3652                         enum rte_filter_op filter_op,
3653                         void *arg)
3654 {
3655         struct e1000_hw *hw = E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
3656         int ret;
3657
3658         MAC_TYPE_FILTER_SUP(hw->mac.type);
3659
3660         if (filter_op == RTE_ETH_FILTER_NOP)
3661                 return 0;
3662
3663         if (arg == NULL) {
3664                 PMD_DRV_LOG(ERR, "arg shouldn't be NULL for operation %u",
3665                             filter_op);
3666                 return -EINVAL;
3667         }
3668
3669         switch (filter_op) {
3670         case RTE_ETH_FILTER_ADD:
3671                 ret = eth_igb_syn_filter_set(dev,
3672                                 (struct rte_eth_syn_filter *)arg,
3673                                 TRUE);
3674                 break;
3675         case RTE_ETH_FILTER_DELETE:
3676                 ret = eth_igb_syn_filter_set(dev,
3677                                 (struct rte_eth_syn_filter *)arg,
3678                                 FALSE);
3679                 break;
3680         case RTE_ETH_FILTER_GET:
3681                 ret = eth_igb_syn_filter_get(dev,
3682                                 (struct rte_eth_syn_filter *)arg);
3683                 break;
3684         default:
3685                 PMD_DRV_LOG(ERR, "unsupported operation %u", filter_op);
3686                 ret = -EINVAL;
3687                 break;
3688         }
3689
3690         return ret;
3691 }
3692
3693 #define MAC_TYPE_FILTER_SUP_EXT(type)    do {\
3694         if ((type) != e1000_82580 && (type) != e1000_i350 &&\
3695                 (type) != e1000_i210 && (type) != e1000_i211)\
3696                 return -ENOSYS; \
3697 } while (0)
3698
3699 /* translate elements in struct rte_eth_ntuple_filter to struct e1000_2tuple_filter_info*/
3700 static inline int
3701 ntuple_filter_to_2tuple(struct rte_eth_ntuple_filter *filter,
3702                         struct e1000_2tuple_filter_info *filter_info)
3703 {
3704         if (filter->queue >= IGB_MAX_RX_QUEUE_NUM)
3705                 return -EINVAL;
3706         if (filter->priority > E1000_2TUPLE_MAX_PRI)
3707                 return -EINVAL;  /* filter index is out of range. */
3708         if (filter->tcp_flags > TCP_FLAG_ALL)
3709                 return -EINVAL;  /* flags is invalid. */
3710
3711         switch (filter->dst_port_mask) {
3712         case UINT16_MAX:
3713                 filter_info->dst_port_mask = 0;
3714                 filter_info->dst_port = filter->dst_port;
3715                 break;
3716         case 0:
3717                 filter_info->dst_port_mask = 1;
3718                 break;
3719         default:
3720                 PMD_DRV_LOG(ERR, "invalid dst_port mask.");
3721                 return -EINVAL;
3722         }
3723
3724         switch (filter->proto_mask) {
3725         case UINT8_MAX:
3726                 filter_info->proto_mask = 0;
3727                 filter_info->proto = filter->proto;
3728                 break;
3729         case 0:
3730                 filter_info->proto_mask = 1;
3731                 break;
3732         default:
3733                 PMD_DRV_LOG(ERR, "invalid protocol mask.");
3734                 return -EINVAL;
3735         }
3736
3737         filter_info->priority = (uint8_t)filter->priority;
3738         if (filter->flags & RTE_NTUPLE_FLAGS_TCP_FLAG)
3739                 filter_info->tcp_flags = filter->tcp_flags;
3740         else
3741                 filter_info->tcp_flags = 0;
3742
3743         return 0;
3744 }
3745
3746 static inline struct e1000_2tuple_filter *
3747 igb_2tuple_filter_lookup(struct e1000_2tuple_filter_list *filter_list,
3748                         struct e1000_2tuple_filter_info *key)
3749 {
3750         struct e1000_2tuple_filter *it;
3751
3752         TAILQ_FOREACH(it, filter_list, entries) {
3753                 if (memcmp(key, &it->filter_info,
3754                         sizeof(struct e1000_2tuple_filter_info)) == 0) {
3755                         return it;
3756                 }
3757         }
3758         return NULL;
3759 }
3760
3761 /* inject a igb 2tuple filter to HW */
3762 static inline void
3763 igb_inject_2uple_filter(struct rte_eth_dev *dev,
3764                            struct e1000_2tuple_filter *filter)
3765 {
3766         struct e1000_hw *hw = E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
3767         uint32_t ttqf = E1000_TTQF_DISABLE_MASK;
3768         uint32_t imir, imir_ext = E1000_IMIREXT_SIZE_BP;
3769         int i;
3770
3771         i = filter->index;
3772         imir = (uint32_t)(filter->filter_info.dst_port & E1000_IMIR_DSTPORT);
3773         if (filter->filter_info.dst_port_mask == 1) /* 1b means not compare. */
3774                 imir |= E1000_IMIR_PORT_BP;
3775         else
3776                 imir &= ~E1000_IMIR_PORT_BP;
3777
3778         imir |= filter->filter_info.priority << E1000_IMIR_PRIORITY_SHIFT;
3779
3780         ttqf |= E1000_TTQF_QUEUE_ENABLE;
3781         ttqf |= (uint32_t)(filter->queue << E1000_TTQF_QUEUE_SHIFT);
3782         ttqf |= (uint32_t)(filter->filter_info.proto &
3783                                                 E1000_TTQF_PROTOCOL_MASK);
3784         if (filter->filter_info.proto_mask == 0)
3785                 ttqf &= ~E1000_TTQF_MASK_ENABLE;
3786
3787         /* tcp flags bits setting. */
3788         if (filter->filter_info.tcp_flags & TCP_FLAG_ALL) {
3789                 if (filter->filter_info.tcp_flags & TCP_URG_FLAG)
3790                         imir_ext |= E1000_IMIREXT_CTRL_URG;
3791                 if (filter->filter_info.tcp_flags & TCP_ACK_FLAG)
3792                         imir_ext |= E1000_IMIREXT_CTRL_ACK;
3793                 if (filter->filter_info.tcp_flags & TCP_PSH_FLAG)
3794                         imir_ext |= E1000_IMIREXT_CTRL_PSH;
3795                 if (filter->filter_info.tcp_flags & TCP_RST_FLAG)
3796                         imir_ext |= E1000_IMIREXT_CTRL_RST;
3797                 if (filter->filter_info.tcp_flags & TCP_SYN_FLAG)
3798                         imir_ext |= E1000_IMIREXT_CTRL_SYN;
3799                 if (filter->filter_info.tcp_flags & TCP_FIN_FLAG)
3800                         imir_ext |= E1000_IMIREXT_CTRL_FIN;
3801         } else {
3802                 imir_ext |= E1000_IMIREXT_CTRL_BP;
3803         }
3804         E1000_WRITE_REG(hw, E1000_IMIR(i), imir);
3805         E1000_WRITE_REG(hw, E1000_TTQF(i), ttqf);
3806         E1000_WRITE_REG(hw, E1000_IMIREXT(i), imir_ext);
3807 }
3808
3809 /*
3810  * igb_add_2tuple_filter - add a 2tuple filter
3811  *
3812  * @param
3813  * dev: Pointer to struct rte_eth_dev.
3814  * ntuple_filter: ponter to the filter that will be added.
3815  *
3816  * @return
3817  *    - On success, zero.
3818  *    - On failure, a negative value.
3819  */
3820 static int
3821 igb_add_2tuple_filter(struct rte_eth_dev *dev,
3822                         struct rte_eth_ntuple_filter *ntuple_filter)
3823 {
3824         struct e1000_filter_info *filter_info =
3825                 E1000_DEV_PRIVATE_TO_FILTER_INFO(dev->data->dev_private);
3826         struct e1000_2tuple_filter *filter;
3827         int i, ret;
3828
3829         filter = rte_zmalloc("e1000_2tuple_filter",
3830                         sizeof(struct e1000_2tuple_filter), 0);
3831         if (filter == NULL)
3832                 return -ENOMEM;
3833
3834         ret = ntuple_filter_to_2tuple(ntuple_filter,
3835                                       &filter->filter_info);
3836         if (ret < 0) {
3837                 rte_free(filter);
3838                 return ret;
3839         }
3840         if (igb_2tuple_filter_lookup(&filter_info->twotuple_list,
3841                                          &filter->filter_info) != NULL) {
3842                 PMD_DRV_LOG(ERR, "filter exists.");
3843                 rte_free(filter);
3844                 return -EEXIST;
3845         }
3846         filter->queue = ntuple_filter->queue;
3847
3848         /*
3849          * look for an unused 2tuple filter index,
3850          * and insert the filter to list.
3851          */
3852         for (i = 0; i < E1000_MAX_TTQF_FILTERS; i++) {
3853                 if (!(filter_info->twotuple_mask & (1 << i))) {
3854                         filter_info->twotuple_mask |= 1 << i;
3855                         filter->index = i;
3856                         TAILQ_INSERT_TAIL(&filter_info->twotuple_list,
3857                                           filter,
3858                                           entries);
3859                         break;
3860                 }
3861         }
3862         if (i >= E1000_MAX_TTQF_FILTERS) {
3863                 PMD_DRV_LOG(ERR, "2tuple filters are full.");
3864                 rte_free(filter);
3865                 return -ENOSYS;
3866         }
3867
3868         igb_inject_2uple_filter(dev, filter);
3869         return 0;
3870 }
3871
3872 /*
3873  * igb_remove_2tuple_filter - remove a 2tuple filter
3874  *
3875  * @param
3876  * dev: Pointer to struct rte_eth_dev.
3877  * ntuple_filter: ponter to the filter that will be removed.
3878  *
3879  * @return
3880  *    - On success, zero.
3881  *    - On failure, a negative value.
3882  */
3883 static int
3884 igb_remove_2tuple_filter(struct rte_eth_dev *dev,
3885                         struct rte_eth_ntuple_filter *ntuple_filter)
3886 {
3887         struct e1000_hw *hw = E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
3888         struct e1000_filter_info *filter_info =
3889                 E1000_DEV_PRIVATE_TO_FILTER_INFO(dev->data->dev_private);
3890         struct e1000_2tuple_filter_info filter_2tuple;
3891         struct e1000_2tuple_filter *filter;
3892         int ret;
3893
3894         memset(&filter_2tuple, 0, sizeof(struct e1000_2tuple_filter_info));
3895         ret = ntuple_filter_to_2tuple(ntuple_filter,
3896                                       &filter_2tuple);
3897         if (ret < 0)
3898                 return ret;
3899
3900         filter = igb_2tuple_filter_lookup(&filter_info->twotuple_list,
3901                                          &filter_2tuple);
3902         if (filter == NULL) {
3903                 PMD_DRV_LOG(ERR, "filter doesn't exist.");
3904                 return -ENOENT;
3905         }
3906
3907         filter_info->twotuple_mask &= ~(1 << filter->index);
3908         TAILQ_REMOVE(&filter_info->twotuple_list, filter, entries);
3909         rte_free(filter);
3910
3911         E1000_WRITE_REG(hw, E1000_TTQF(filter->index), E1000_TTQF_DISABLE_MASK);
3912         E1000_WRITE_REG(hw, E1000_IMIR(filter->index), 0);
3913         E1000_WRITE_REG(hw, E1000_IMIREXT(filter->index), 0);
3914         return 0;
3915 }
3916
3917 /* inject a igb flex filter to HW */
3918 static inline void
3919 igb_inject_flex_filter(struct rte_eth_dev *dev,
3920                            struct e1000_flex_filter *filter)
3921 {
3922         struct e1000_hw *hw = E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
3923         uint32_t wufc, queueing;
3924         uint32_t reg_off;
3925         uint8_t i, j = 0;
3926
3927         wufc = E1000_READ_REG(hw, E1000_WUFC);
3928         if (filter->index < E1000_MAX_FHFT)
3929                 reg_off = E1000_FHFT(filter->index);
3930         else
3931                 reg_off = E1000_FHFT_EXT(filter->index - E1000_MAX_FHFT);
3932
3933         E1000_WRITE_REG(hw, E1000_WUFC, wufc | E1000_WUFC_FLEX_HQ |
3934                         (E1000_WUFC_FLX0 << filter->index));
3935         queueing = filter->filter_info.len |
3936                 (filter->queue << E1000_FHFT_QUEUEING_QUEUE_SHIFT) |
3937                 (filter->filter_info.priority <<
3938                         E1000_FHFT_QUEUEING_PRIO_SHIFT);
3939         E1000_WRITE_REG(hw, reg_off + E1000_FHFT_QUEUEING_OFFSET,
3940                         queueing);
3941
3942         for (i = 0; i < E1000_FLEX_FILTERS_MASK_SIZE; i++) {
3943                 E1000_WRITE_REG(hw, reg_off,
3944                                 filter->filter_info.dwords[j]);
3945                 reg_off += sizeof(uint32_t);
3946                 E1000_WRITE_REG(hw, reg_off,
3947                                 filter->filter_info.dwords[++j]);
3948                 reg_off += sizeof(uint32_t);
3949                 E1000_WRITE_REG(hw, reg_off,
3950                         (uint32_t)filter->filter_info.mask[i]);
3951                 reg_off += sizeof(uint32_t) * 2;
3952                 ++j;
3953         }
3954 }
3955
3956 static inline struct e1000_flex_filter *
3957 eth_igb_flex_filter_lookup(struct e1000_flex_filter_list *filter_list,
3958                         struct e1000_flex_filter_info *key)
3959 {
3960         struct e1000_flex_filter *it;
3961
3962         TAILQ_FOREACH(it, filter_list, entries) {
3963                 if (memcmp(key, &it->filter_info,
3964                         sizeof(struct e1000_flex_filter_info)) == 0)
3965                         return it;
3966         }
3967
3968         return NULL;
3969 }
3970
3971 /* remove a flex byte filter
3972  * @param
3973  * dev: Pointer to struct rte_eth_dev.
3974  * filter: the pointer of the filter will be removed.
3975  */
3976 static void
3977 igb_remove_flex_filter(struct rte_eth_dev *dev,
3978                         struct e1000_flex_filter *filter)
3979 {
3980         struct e1000_filter_info *filter_info =
3981                 E1000_DEV_PRIVATE_TO_FILTER_INFO(dev->data->dev_private);
3982         struct e1000_hw *hw = E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
3983         uint32_t wufc, i;
3984         uint32_t reg_off;
3985
3986         wufc = E1000_READ_REG(hw, E1000_WUFC);
3987         if (filter->index < E1000_MAX_FHFT)
3988                 reg_off = E1000_FHFT(filter->index);
3989         else
3990                 reg_off = E1000_FHFT_EXT(filter->index - E1000_MAX_FHFT);
3991
3992         for (i = 0; i < E1000_FHFT_SIZE_IN_DWD; i++)
3993                 E1000_WRITE_REG(hw, reg_off + i * sizeof(uint32_t), 0);
3994
3995         E1000_WRITE_REG(hw, E1000_WUFC, wufc &
3996                 (~(E1000_WUFC_FLX0 << filter->index)));
3997
3998         filter_info->flex_mask &= ~(1 << filter->index);
3999         TAILQ_REMOVE(&filter_info->flex_list, filter, entries);
4000         rte_free(filter);
4001 }
4002
4003 int
4004 eth_igb_add_del_flex_filter(struct rte_eth_dev *dev,
4005                         struct rte_eth_flex_filter *filter,
4006                         bool add)
4007 {
4008         struct e1000_filter_info *filter_info =
4009                 E1000_DEV_PRIVATE_TO_FILTER_INFO(dev->data->dev_private);
4010         struct e1000_flex_filter *flex_filter, *it;
4011         uint32_t mask;
4012         uint8_t shift, i;
4013
4014         flex_filter = rte_zmalloc("e1000_flex_filter",
4015                         sizeof(struct e1000_flex_filter), 0);
4016         if (flex_filter == NULL)
4017                 return -ENOMEM;
4018
4019         flex_filter->filter_info.len = filter->len;
4020         flex_filter->filter_info.priority = filter->priority;
4021         memcpy(flex_filter->filter_info.dwords, filter->bytes, filter->len);
4022         for (i = 0; i < RTE_ALIGN(filter->len, CHAR_BIT) / CHAR_BIT; i++) {
4023                 mask = 0;
4024                 /* reverse bits in flex filter's mask*/
4025                 for (shift = 0; shift < CHAR_BIT; shift++) {
4026                         if (filter->mask[i] & (0x01 << shift))
4027                                 mask |= (0x80 >> shift);
4028                 }
4029                 flex_filter->filter_info.mask[i] = mask;
4030         }
4031
4032         it = eth_igb_flex_filter_lookup(&filter_info->flex_list,
4033                                 &flex_filter->filter_info);
4034         if (it == NULL && !add) {
4035                 PMD_DRV_LOG(ERR, "filter doesn't exist.");
4036                 rte_free(flex_filter);
4037                 return -ENOENT;
4038         }
4039         if (it != NULL && add) {
4040                 PMD_DRV_LOG(ERR, "filter exists.");
4041                 rte_free(flex_filter);
4042                 return -EEXIST;
4043         }
4044
4045         if (add) {
4046                 flex_filter->queue = filter->queue;
4047                 /*
4048                  * look for an unused flex filter index
4049                  * and insert the filter into the list.
4050                  */
4051                 for (i = 0; i < E1000_MAX_FLEX_FILTERS; i++) {
4052                         if (!(filter_info->flex_mask & (1 << i))) {
4053                                 filter_info->flex_mask |= 1 << i;
4054                                 flex_filter->index = i;
4055                                 TAILQ_INSERT_TAIL(&filter_info->flex_list,
4056                                         flex_filter,
4057                                         entries);
4058                                 break;
4059                         }
4060                 }
4061                 if (i >= E1000_MAX_FLEX_FILTERS) {
4062                         PMD_DRV_LOG(ERR, "flex filters are full.");
4063                         rte_free(flex_filter);
4064                         return -ENOSYS;
4065                 }
4066
4067                 igb_inject_flex_filter(dev, flex_filter);
4068
4069         } else {
4070                 igb_remove_flex_filter(dev, it);
4071                 rte_free(flex_filter);
4072         }
4073
4074         return 0;
4075 }
4076
4077 static int
4078 eth_igb_get_flex_filter(struct rte_eth_dev *dev,
4079                         struct rte_eth_flex_filter *filter)
4080 {
4081         struct e1000_hw *hw = E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
4082         struct e1000_filter_info *filter_info =
4083                 E1000_DEV_PRIVATE_TO_FILTER_INFO(dev->data->dev_private);
4084         struct e1000_flex_filter flex_filter, *it;
4085         uint32_t wufc, queueing, wufc_en = 0;
4086
4087         memset(&flex_filter, 0, sizeof(struct e1000_flex_filter));
4088         flex_filter.filter_info.len = filter->len;
4089         flex_filter.filter_info.priority = filter->priority;
4090         memcpy(flex_filter.filter_info.dwords, filter->bytes, filter->len);
4091         memcpy(flex_filter.filter_info.mask, filter->mask,
4092                         RTE_ALIGN(filter->len, sizeof(char)) / sizeof(char));
4093
4094         it = eth_igb_flex_filter_lookup(&filter_info->flex_list,
4095                                 &flex_filter.filter_info);
4096         if (it == NULL) {
4097                 PMD_DRV_LOG(ERR, "filter doesn't exist.");
4098                 return -ENOENT;
4099         }
4100
4101         wufc = E1000_READ_REG(hw, E1000_WUFC);
4102         wufc_en = E1000_WUFC_FLEX_HQ | (E1000_WUFC_FLX0 << it->index);
4103
4104         if ((wufc & wufc_en) == wufc_en) {
4105                 uint32_t reg_off = 0;
4106                 if (it->index < E1000_MAX_FHFT)
4107                         reg_off = E1000_FHFT(it->index);
4108                 else
4109                         reg_off = E1000_FHFT_EXT(it->index - E1000_MAX_FHFT);
4110
4111                 queueing = E1000_READ_REG(hw,
4112                                 reg_off + E1000_FHFT_QUEUEING_OFFSET);
4113                 filter->len = queueing & E1000_FHFT_QUEUEING_LEN;
4114                 filter->priority = (queueing & E1000_FHFT_QUEUEING_PRIO) >>
4115                         E1000_FHFT_QUEUEING_PRIO_SHIFT;
4116                 filter->queue = (queueing & E1000_FHFT_QUEUEING_QUEUE) >>
4117                         E1000_FHFT_QUEUEING_QUEUE_SHIFT;
4118                 return 0;
4119         }
4120         return -ENOENT;
4121 }
4122
4123 static int
4124 eth_igb_flex_filter_handle(struct rte_eth_dev *dev,
4125                         enum rte_filter_op filter_op,
4126                         void *arg)
4127 {
4128         struct e1000_hw *hw = E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
4129         struct rte_eth_flex_filter *filter;
4130         int ret = 0;
4131
4132         MAC_TYPE_FILTER_SUP_EXT(hw->mac.type);
4133
4134         if (filter_op == RTE_ETH_FILTER_NOP)
4135                 return ret;
4136
4137         if (arg == NULL) {
4138                 PMD_DRV_LOG(ERR, "arg shouldn't be NULL for operation %u",
4139                             filter_op);
4140                 return -EINVAL;
4141         }
4142
4143         filter = (struct rte_eth_flex_filter *)arg;
4144         if (filter->len == 0 || filter->len > E1000_MAX_FLEX_FILTER_LEN
4145             || filter->len % sizeof(uint64_t) != 0) {
4146                 PMD_DRV_LOG(ERR, "filter's length is out of range");
4147                 return -EINVAL;
4148         }
4149         if (filter->priority > E1000_MAX_FLEX_FILTER_PRI) {
4150                 PMD_DRV_LOG(ERR, "filter's priority is out of range");
4151                 return -EINVAL;
4152         }
4153
4154         switch (filter_op) {
4155         case RTE_ETH_FILTER_ADD:
4156                 ret = eth_igb_add_del_flex_filter(dev, filter, TRUE);
4157                 break;
4158         case RTE_ETH_FILTER_DELETE:
4159                 ret = eth_igb_add_del_flex_filter(dev, filter, FALSE);
4160                 break;
4161         case RTE_ETH_FILTER_GET:
4162                 ret = eth_igb_get_flex_filter(dev, filter);
4163                 break;
4164         default:
4165                 PMD_DRV_LOG(ERR, "unsupported operation %u", filter_op);
4166                 ret = -EINVAL;
4167                 break;
4168         }
4169
4170         return ret;
4171 }
4172
4173 /* translate elements in struct rte_eth_ntuple_filter to struct e1000_5tuple_filter_info*/
4174 static inline int
4175 ntuple_filter_to_5tuple_82576(struct rte_eth_ntuple_filter *filter,
4176                         struct e1000_5tuple_filter_info *filter_info)
4177 {
4178         if (filter->queue >= IGB_MAX_RX_QUEUE_NUM_82576)
4179                 return -EINVAL;
4180         if (filter->priority > E1000_2TUPLE_MAX_PRI)
4181                 return -EINVAL;  /* filter index is out of range. */
4182         if (filter->tcp_flags > TCP_FLAG_ALL)
4183                 return -EINVAL;  /* flags is invalid. */
4184
4185         switch (filter->dst_ip_mask) {
4186         case UINT32_MAX:
4187                 filter_info->dst_ip_mask = 0;
4188                 filter_info->dst_ip = filter->dst_ip;
4189                 break;
4190         case 0:
4191                 filter_info->dst_ip_mask = 1;
4192                 break;
4193         default:
4194                 PMD_DRV_LOG(ERR, "invalid dst_ip mask.");
4195                 return -EINVAL;
4196         }
4197
4198         switch (filter->src_ip_mask) {
4199         case UINT32_MAX:
4200                 filter_info->src_ip_mask = 0;
4201                 filter_info->src_ip = filter->src_ip;
4202                 break;
4203         case 0:
4204                 filter_info->src_ip_mask = 1;
4205                 break;
4206         default:
4207                 PMD_DRV_LOG(ERR, "invalid src_ip mask.");
4208                 return -EINVAL;
4209         }
4210
4211         switch (filter->dst_port_mask) {
4212         case UINT16_MAX:
4213                 filter_info->dst_port_mask = 0;
4214                 filter_info->dst_port = filter->dst_port;
4215                 break;
4216         case 0:
4217                 filter_info->dst_port_mask = 1;
4218                 break;
4219         default:
4220                 PMD_DRV_LOG(ERR, "invalid dst_port mask.");
4221                 return -EINVAL;
4222         }
4223
4224         switch (filter->src_port_mask) {
4225         case UINT16_MAX:
4226                 filter_info->src_port_mask = 0;
4227                 filter_info->src_port = filter->src_port;
4228                 break;
4229         case 0:
4230                 filter_info->src_port_mask = 1;
4231                 break;
4232         default:
4233                 PMD_DRV_LOG(ERR, "invalid src_port mask.");
4234                 return -EINVAL;
4235         }
4236
4237         switch (filter->proto_mask) {
4238         case UINT8_MAX:
4239                 filter_info->proto_mask = 0;
4240                 filter_info->proto = filter->proto;
4241                 break;
4242         case 0:
4243                 filter_info->proto_mask = 1;
4244                 break;
4245         default:
4246                 PMD_DRV_LOG(ERR, "invalid protocol mask.");
4247                 return -EINVAL;
4248         }
4249
4250         filter_info->priority = (uint8_t)filter->priority;
4251         if (filter->flags & RTE_NTUPLE_FLAGS_TCP_FLAG)
4252                 filter_info->tcp_flags = filter->tcp_flags;
4253         else
4254                 filter_info->tcp_flags = 0;
4255
4256         return 0;
4257 }
4258
4259 static inline struct e1000_5tuple_filter *
4260 igb_5tuple_filter_lookup_82576(struct e1000_5tuple_filter_list *filter_list,
4261                         struct e1000_5tuple_filter_info *key)
4262 {
4263         struct e1000_5tuple_filter *it;
4264
4265         TAILQ_FOREACH(it, filter_list, entries) {
4266                 if (memcmp(key, &it->filter_info,
4267                         sizeof(struct e1000_5tuple_filter_info)) == 0) {
4268                         return it;
4269                 }
4270         }
4271         return NULL;
4272 }
4273
4274 /* inject a igb 5-tuple filter to HW */
4275 static inline void
4276 igb_inject_5tuple_filter_82576(struct rte_eth_dev *dev,
4277                            struct e1000_5tuple_filter *filter)
4278 {
4279         struct e1000_hw *hw = E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
4280         uint32_t ftqf = E1000_FTQF_VF_BP | E1000_FTQF_MASK;
4281         uint32_t spqf, imir, imir_ext = E1000_IMIREXT_SIZE_BP;
4282         uint8_t i;
4283
4284         i = filter->index;
4285         ftqf |= filter->filter_info.proto & E1000_FTQF_PROTOCOL_MASK;
4286         if (filter->filter_info.src_ip_mask == 0) /* 0b means compare. */
4287                 ftqf &= ~E1000_FTQF_MASK_SOURCE_ADDR_BP;
4288         if (filter->filter_info.dst_ip_mask == 0)
4289                 ftqf &= ~E1000_FTQF_MASK_DEST_ADDR_BP;
4290         if (filter->filter_info.src_port_mask == 0)
4291                 ftqf &= ~E1000_FTQF_MASK_SOURCE_PORT_BP;
4292         if (filter->filter_info.proto_mask == 0)
4293                 ftqf &= ~E1000_FTQF_MASK_PROTO_BP;
4294         ftqf |= (filter->queue << E1000_FTQF_QUEUE_SHIFT) &
4295                 E1000_FTQF_QUEUE_MASK;
4296         ftqf |= E1000_FTQF_QUEUE_ENABLE;
4297         E1000_WRITE_REG(hw, E1000_FTQF(i), ftqf);
4298         E1000_WRITE_REG(hw, E1000_DAQF(i), filter->filter_info.dst_ip);
4299         E1000_WRITE_REG(hw, E1000_SAQF(i), filter->filter_info.src_ip);
4300
4301         spqf = filter->filter_info.src_port & E1000_SPQF_SRCPORT;
4302         E1000_WRITE_REG(hw, E1000_SPQF(i), spqf);
4303
4304         imir = (uint32_t)(filter->filter_info.dst_port & E1000_IMIR_DSTPORT);
4305         if (filter->filter_info.dst_port_mask == 1) /* 1b means not compare. */
4306                 imir |= E1000_IMIR_PORT_BP;
4307         else
4308                 imir &= ~E1000_IMIR_PORT_BP;
4309         imir |= filter->filter_info.priority << E1000_IMIR_PRIORITY_SHIFT;
4310
4311         /* tcp flags bits setting. */
4312         if (filter->filter_info.tcp_flags & TCP_FLAG_ALL) {
4313                 if (filter->filter_info.tcp_flags & TCP_URG_FLAG)
4314                         imir_ext |= E1000_IMIREXT_CTRL_URG;
4315                 if (filter->filter_info.tcp_flags & TCP_ACK_FLAG)
4316                         imir_ext |= E1000_IMIREXT_CTRL_ACK;
4317                 if (filter->filter_info.tcp_flags & TCP_PSH_FLAG)
4318                         imir_ext |= E1000_IMIREXT_CTRL_PSH;
4319                 if (filter->filter_info.tcp_flags & TCP_RST_FLAG)
4320                         imir_ext |= E1000_IMIREXT_CTRL_RST;
4321                 if (filter->filter_info.tcp_flags & TCP_SYN_FLAG)
4322                         imir_ext |= E1000_IMIREXT_CTRL_SYN;
4323                 if (filter->filter_info.tcp_flags & TCP_FIN_FLAG)
4324                         imir_ext |= E1000_IMIREXT_CTRL_FIN;
4325         } else {
4326                 imir_ext |= E1000_IMIREXT_CTRL_BP;
4327         }
4328         E1000_WRITE_REG(hw, E1000_IMIR(i), imir);
4329         E1000_WRITE_REG(hw, E1000_IMIREXT(i), imir_ext);
4330 }
4331
4332 /*
4333  * igb_add_5tuple_filter_82576 - add a 5tuple filter
4334  *
4335  * @param
4336  * dev: Pointer to struct rte_eth_dev.
4337  * ntuple_filter: ponter to the filter that will be added.
4338  *
4339  * @return
4340  *    - On success, zero.
4341  *    - On failure, a negative value.
4342  */
4343 static int
4344 igb_add_5tuple_filter_82576(struct rte_eth_dev *dev,
4345                         struct rte_eth_ntuple_filter *ntuple_filter)
4346 {
4347         struct e1000_filter_info *filter_info =
4348                 E1000_DEV_PRIVATE_TO_FILTER_INFO(dev->data->dev_private);
4349         struct e1000_5tuple_filter *filter;
4350         uint8_t i;
4351         int ret;
4352
4353         filter = rte_zmalloc("e1000_5tuple_filter",
4354                         sizeof(struct e1000_5tuple_filter), 0);
4355         if (filter == NULL)
4356                 return -ENOMEM;
4357
4358         ret = ntuple_filter_to_5tuple_82576(ntuple_filter,
4359                                             &filter->filter_info);
4360         if (ret < 0) {
4361                 rte_free(filter);
4362                 return ret;
4363         }
4364
4365         if (igb_5tuple_filter_lookup_82576(&filter_info->fivetuple_list,
4366                                          &filter->filter_info) != NULL) {
4367                 PMD_DRV_LOG(ERR, "filter exists.");
4368                 rte_free(filter);
4369                 return -EEXIST;
4370         }
4371         filter->queue = ntuple_filter->queue;
4372
4373         /*
4374          * look for an unused 5tuple filter index,
4375          * and insert the filter to list.
4376          */
4377         for (i = 0; i < E1000_MAX_FTQF_FILTERS; i++) {
4378                 if (!(filter_info->fivetuple_mask & (1 << i))) {
4379                         filter_info->fivetuple_mask |= 1 << i;
4380                         filter->index = i;
4381                         TAILQ_INSERT_TAIL(&filter_info->fivetuple_list,
4382                                           filter,
4383                                           entries);
4384                         break;
4385                 }
4386         }
4387         if (i >= E1000_MAX_FTQF_FILTERS) {
4388                 PMD_DRV_LOG(ERR, "5tuple filters are full.");
4389                 rte_free(filter);
4390                 return -ENOSYS;
4391         }
4392
4393         igb_inject_5tuple_filter_82576(dev, filter);
4394         return 0;
4395 }
4396
4397 /*
4398  * igb_remove_5tuple_filter_82576 - remove a 5tuple filter
4399  *
4400  * @param
4401  * dev: Pointer to struct rte_eth_dev.
4402  * ntuple_filter: ponter to the filter that will be removed.
4403  *
4404  * @return
4405  *    - On success, zero.
4406  *    - On failure, a negative value.
4407  */
4408 static int
4409 igb_remove_5tuple_filter_82576(struct rte_eth_dev *dev,
4410                                 struct rte_eth_ntuple_filter *ntuple_filter)
4411 {
4412         struct e1000_hw *hw = E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
4413         struct e1000_filter_info *filter_info =
4414                 E1000_DEV_PRIVATE_TO_FILTER_INFO(dev->data->dev_private);
4415         struct e1000_5tuple_filter_info filter_5tuple;
4416         struct e1000_5tuple_filter *filter;
4417         int ret;
4418
4419         memset(&filter_5tuple, 0, sizeof(struct e1000_5tuple_filter_info));
4420         ret = ntuple_filter_to_5tuple_82576(ntuple_filter,
4421                                             &filter_5tuple);
4422         if (ret < 0)
4423                 return ret;
4424
4425         filter = igb_5tuple_filter_lookup_82576(&filter_info->fivetuple_list,
4426                                          &filter_5tuple);
4427         if (filter == NULL) {
4428                 PMD_DRV_LOG(ERR, "filter doesn't exist.");
4429                 return -ENOENT;
4430         }
4431
4432         filter_info->fivetuple_mask &= ~(1 << filter->index);
4433         TAILQ_REMOVE(&filter_info->fivetuple_list, filter, entries);
4434         rte_free(filter);
4435
4436         E1000_WRITE_REG(hw, E1000_FTQF(filter->index),
4437                         E1000_FTQF_VF_BP | E1000_FTQF_MASK);
4438         E1000_WRITE_REG(hw, E1000_DAQF(filter->index), 0);
4439         E1000_WRITE_REG(hw, E1000_SAQF(filter->index), 0);
4440         E1000_WRITE_REG(hw, E1000_SPQF(filter->index), 0);
4441         E1000_WRITE_REG(hw, E1000_IMIR(filter->index), 0);
4442         E1000_WRITE_REG(hw, E1000_IMIREXT(filter->index), 0);
4443         return 0;
4444 }
4445
4446 static int
4447 eth_igb_mtu_set(struct rte_eth_dev *dev, uint16_t mtu)
4448 {
4449         uint32_t rctl;
4450         struct e1000_hw *hw;
4451         struct rte_eth_dev_info dev_info;
4452         uint32_t frame_size = mtu + (ETHER_HDR_LEN + ETHER_CRC_LEN +
4453                                      VLAN_TAG_SIZE);
4454
4455         hw = E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
4456
4457 #ifdef RTE_LIBRTE_82571_SUPPORT
4458         /* XXX: not bigger than max_rx_pktlen */
4459         if (hw->mac.type == e1000_82571)
4460                 return -ENOTSUP;
4461 #endif
4462         eth_igb_infos_get(dev, &dev_info);
4463
4464         /* check that mtu is within the allowed range */
4465         if ((mtu < ETHER_MIN_MTU) ||
4466             (frame_size > dev_info.max_rx_pktlen))
4467                 return -EINVAL;
4468
4469         /* refuse mtu that requires the support of scattered packets when this
4470          * feature has not been enabled before. */
4471         if (!dev->data->scattered_rx &&
4472             frame_size > dev->data->min_rx_buf_size - RTE_PKTMBUF_HEADROOM)
4473                 return -EINVAL;
4474
4475         rctl = E1000_READ_REG(hw, E1000_RCTL);
4476
4477         /* switch to jumbo mode if needed */
4478         if (frame_size > ETHER_MAX_LEN) {
4479                 dev->data->dev_conf.rxmode.jumbo_frame = 1;
4480                 rctl |= E1000_RCTL_LPE;
4481         } else {
4482                 dev->data->dev_conf.rxmode.jumbo_frame = 0;
4483                 rctl &= ~E1000_RCTL_LPE;
4484         }
4485         E1000_WRITE_REG(hw, E1000_RCTL, rctl);
4486
4487         /* update max frame size */
4488         dev->data->dev_conf.rxmode.max_rx_pkt_len = frame_size;
4489
4490         E1000_WRITE_REG(hw, E1000_RLPML,
4491                         dev->data->dev_conf.rxmode.max_rx_pkt_len);
4492
4493         return 0;
4494 }
4495
4496 /*
4497  * igb_add_del_ntuple_filter - add or delete a ntuple filter
4498  *
4499  * @param
4500  * dev: Pointer to struct rte_eth_dev.
4501  * ntuple_filter: Pointer to struct rte_eth_ntuple_filter
4502  * add: if true, add filter, if false, remove filter
4503  *
4504  * @return
4505  *    - On success, zero.
4506  *    - On failure, a negative value.
4507  */
4508 static int
4509 igb_add_del_ntuple_filter(struct rte_eth_dev *dev,
4510                         struct rte_eth_ntuple_filter *ntuple_filter,
4511                         bool add)
4512 {
4513         struct e1000_hw *hw = E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
4514         int ret;
4515
4516         switch (ntuple_filter->flags) {
4517         case RTE_5TUPLE_FLAGS:
4518         case (RTE_5TUPLE_FLAGS | RTE_NTUPLE_FLAGS_TCP_FLAG):
4519                 if (hw->mac.type != e1000_82576)
4520                         return -ENOTSUP;
4521                 if (add)
4522                         ret = igb_add_5tuple_filter_82576(dev,
4523                                                           ntuple_filter);
4524                 else
4525                         ret = igb_remove_5tuple_filter_82576(dev,
4526                                                              ntuple_filter);
4527                 break;
4528         case RTE_2TUPLE_FLAGS:
4529         case (RTE_2TUPLE_FLAGS | RTE_NTUPLE_FLAGS_TCP_FLAG):
4530                 if (hw->mac.type != e1000_82580 && hw->mac.type != e1000_i350 &&
4531                         hw->mac.type != e1000_i210 &&
4532                         hw->mac.type != e1000_i211)
4533                         return -ENOTSUP;
4534                 if (add)
4535                         ret = igb_add_2tuple_filter(dev, ntuple_filter);
4536                 else
4537                         ret = igb_remove_2tuple_filter(dev, ntuple_filter);
4538                 break;
4539         default:
4540                 ret = -EINVAL;
4541                 break;
4542         }
4543
4544         return ret;
4545 }
4546
4547 /*
4548  * igb_get_ntuple_filter - get a ntuple filter
4549  *
4550  * @param
4551  * dev: Pointer to struct rte_eth_dev.
4552  * ntuple_filter: Pointer to struct rte_eth_ntuple_filter
4553  *
4554  * @return
4555  *    - On success, zero.
4556  *    - On failure, a negative value.
4557  */
4558 static int
4559 igb_get_ntuple_filter(struct rte_eth_dev *dev,
4560                         struct rte_eth_ntuple_filter *ntuple_filter)
4561 {
4562         struct e1000_hw *hw = E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
4563         struct e1000_filter_info *filter_info =
4564                 E1000_DEV_PRIVATE_TO_FILTER_INFO(dev->data->dev_private);
4565         struct e1000_5tuple_filter_info filter_5tuple;
4566         struct e1000_2tuple_filter_info filter_2tuple;
4567         struct e1000_5tuple_filter *p_5tuple_filter;
4568         struct e1000_2tuple_filter *p_2tuple_filter;
4569         int ret;
4570
4571         switch (ntuple_filter->flags) {
4572         case RTE_5TUPLE_FLAGS:
4573         case (RTE_5TUPLE_FLAGS | RTE_NTUPLE_FLAGS_TCP_FLAG):
4574                 if (hw->mac.type != e1000_82576)
4575                         return -ENOTSUP;
4576                 memset(&filter_5tuple,
4577                         0,
4578                         sizeof(struct e1000_5tuple_filter_info));
4579                 ret = ntuple_filter_to_5tuple_82576(ntuple_filter,
4580                                                     &filter_5tuple);
4581                 if (ret < 0)
4582                         return ret;
4583                 p_5tuple_filter = igb_5tuple_filter_lookup_82576(
4584                                         &filter_info->fivetuple_list,
4585                                         &filter_5tuple);
4586                 if (p_5tuple_filter == NULL) {
4587                         PMD_DRV_LOG(ERR, "filter doesn't exist.");
4588                         return -ENOENT;
4589                 }
4590                 ntuple_filter->queue = p_5tuple_filter->queue;
4591                 break;
4592         case RTE_2TUPLE_FLAGS:
4593         case (RTE_2TUPLE_FLAGS | RTE_NTUPLE_FLAGS_TCP_FLAG):
4594                 if (hw->mac.type != e1000_82580 && hw->mac.type != e1000_i350)
4595                         return -ENOTSUP;
4596                 memset(&filter_2tuple,
4597                         0,
4598                         sizeof(struct e1000_2tuple_filter_info));
4599                 ret = ntuple_filter_to_2tuple(ntuple_filter, &filter_2tuple);
4600                 if (ret < 0)
4601                         return ret;
4602                 p_2tuple_filter = igb_2tuple_filter_lookup(
4603                                         &filter_info->twotuple_list,
4604                                         &filter_2tuple);
4605                 if (p_2tuple_filter == NULL) {
4606                         PMD_DRV_LOG(ERR, "filter doesn't exist.");
4607                         return -ENOENT;
4608                 }
4609                 ntuple_filter->queue = p_2tuple_filter->queue;
4610                 break;
4611         default:
4612                 ret = -EINVAL;
4613                 break;
4614         }
4615
4616         return 0;
4617 }
4618
4619 /*
4620  * igb_ntuple_filter_handle - Handle operations for ntuple filter.
4621  * @dev: pointer to rte_eth_dev structure
4622  * @filter_op:operation will be taken.
4623  * @arg: a pointer to specific structure corresponding to the filter_op
4624  */
4625 static int
4626 igb_ntuple_filter_handle(struct rte_eth_dev *dev,
4627                                 enum rte_filter_op filter_op,
4628                                 void *arg)
4629 {
4630         struct e1000_hw *hw = E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
4631         int ret;
4632
4633         MAC_TYPE_FILTER_SUP(hw->mac.type);
4634
4635         if (filter_op == RTE_ETH_FILTER_NOP)
4636                 return 0;
4637
4638         if (arg == NULL) {
4639                 PMD_DRV_LOG(ERR, "arg shouldn't be NULL for operation %u.",
4640                             filter_op);
4641                 return -EINVAL;
4642         }
4643
4644         switch (filter_op) {
4645         case RTE_ETH_FILTER_ADD:
4646                 ret = igb_add_del_ntuple_filter(dev,
4647                         (struct rte_eth_ntuple_filter *)arg,
4648                         TRUE);
4649                 break;
4650         case RTE_ETH_FILTER_DELETE:
4651                 ret = igb_add_del_ntuple_filter(dev,
4652                         (struct rte_eth_ntuple_filter *)arg,
4653                         FALSE);
4654                 break;
4655         case RTE_ETH_FILTER_GET:
4656                 ret = igb_get_ntuple_filter(dev,
4657                         (struct rte_eth_ntuple_filter *)arg);
4658                 break;
4659         default:
4660                 PMD_DRV_LOG(ERR, "unsupported operation %u.", filter_op);
4661                 ret = -EINVAL;
4662                 break;
4663         }
4664         return ret;
4665 }
4666
4667 static inline int
4668 igb_ethertype_filter_lookup(struct e1000_filter_info *filter_info,
4669                         uint16_t ethertype)
4670 {
4671         int i;
4672
4673         for (i = 0; i < E1000_MAX_ETQF_FILTERS; i++) {
4674                 if (filter_info->ethertype_filters[i].ethertype == ethertype &&
4675                     (filter_info->ethertype_mask & (1 << i)))
4676                         return i;
4677         }
4678         return -1;
4679 }
4680
4681 static inline int
4682 igb_ethertype_filter_insert(struct e1000_filter_info *filter_info,
4683                         uint16_t ethertype, uint32_t etqf)
4684 {
4685         int i;
4686
4687         for (i = 0; i < E1000_MAX_ETQF_FILTERS; i++) {
4688                 if (!(filter_info->ethertype_mask & (1 << i))) {
4689                         filter_info->ethertype_mask |= 1 << i;
4690                         filter_info->ethertype_filters[i].ethertype = ethertype;
4691                         filter_info->ethertype_filters[i].etqf = etqf;
4692                         return i;
4693                 }
4694         }
4695         return -1;
4696 }
4697
4698 static int
4699 igb_ethertype_filter_remove(struct e1000_filter_info *filter_info,
4700                         uint8_t idx)
4701 {
4702         if (idx >= E1000_MAX_ETQF_FILTERS)
4703                 return -1;
4704         filter_info->ethertype_mask &= ~(1 << idx);
4705         filter_info->ethertype_filters[idx].ethertype = 0;
4706         filter_info->ethertype_filters[idx].etqf = 0;
4707         return idx;
4708 }
4709
4710
4711 int
4712 igb_add_del_ethertype_filter(struct rte_eth_dev *dev,
4713                         struct rte_eth_ethertype_filter *filter,
4714                         bool add)
4715 {
4716         struct e1000_hw *hw = E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
4717         struct e1000_filter_info *filter_info =
4718                 E1000_DEV_PRIVATE_TO_FILTER_INFO(dev->data->dev_private);
4719         uint32_t etqf = 0;
4720         int ret;
4721
4722         if (filter->ether_type == ETHER_TYPE_IPv4 ||
4723                 filter->ether_type == ETHER_TYPE_IPv6) {
4724                 PMD_DRV_LOG(ERR, "unsupported ether_type(0x%04x) in"
4725                         " ethertype filter.", filter->ether_type);
4726                 return -EINVAL;
4727         }
4728
4729         if (filter->flags & RTE_ETHTYPE_FLAGS_MAC) {
4730                 PMD_DRV_LOG(ERR, "mac compare is unsupported.");
4731                 return -EINVAL;
4732         }
4733         if (filter->flags & RTE_ETHTYPE_FLAGS_DROP) {
4734                 PMD_DRV_LOG(ERR, "drop option is unsupported.");
4735                 return -EINVAL;
4736         }
4737
4738         ret = igb_ethertype_filter_lookup(filter_info, filter->ether_type);
4739         if (ret >= 0 && add) {
4740                 PMD_DRV_LOG(ERR, "ethertype (0x%04x) filter exists.",
4741                             filter->ether_type);
4742                 return -EEXIST;
4743         }
4744         if (ret < 0 && !add) {
4745                 PMD_DRV_LOG(ERR, "ethertype (0x%04x) filter doesn't exist.",
4746                             filter->ether_type);
4747                 return -ENOENT;
4748         }
4749
4750         if (add) {
4751                 etqf |= E1000_ETQF_FILTER_ENABLE | E1000_ETQF_QUEUE_ENABLE;
4752                 etqf |= (uint32_t)(filter->ether_type & E1000_ETQF_ETHERTYPE);
4753                 etqf |= filter->queue << E1000_ETQF_QUEUE_SHIFT;
4754                 ret = igb_ethertype_filter_insert(filter_info,
4755                                 filter->ether_type, etqf);
4756                 if (ret < 0) {
4757                         PMD_DRV_LOG(ERR, "ethertype filters are full.");
4758                         return -ENOSYS;
4759                 }
4760         } else {
4761                 ret = igb_ethertype_filter_remove(filter_info, (uint8_t)ret);
4762                 if (ret < 0)
4763                         return -ENOSYS;
4764         }
4765         E1000_WRITE_REG(hw, E1000_ETQF(ret), etqf);
4766         E1000_WRITE_FLUSH(hw);
4767
4768         return 0;
4769 }
4770
4771 static int
4772 igb_get_ethertype_filter(struct rte_eth_dev *dev,
4773                         struct rte_eth_ethertype_filter *filter)
4774 {
4775         struct e1000_hw *hw = E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
4776         struct e1000_filter_info *filter_info =
4777                 E1000_DEV_PRIVATE_TO_FILTER_INFO(dev->data->dev_private);
4778         uint32_t etqf;
4779         int ret;
4780
4781         ret = igb_ethertype_filter_lookup(filter_info, filter->ether_type);
4782         if (ret < 0) {
4783                 PMD_DRV_LOG(ERR, "ethertype (0x%04x) filter doesn't exist.",
4784                             filter->ether_type);
4785                 return -ENOENT;
4786         }
4787
4788         etqf = E1000_READ_REG(hw, E1000_ETQF(ret));
4789         if (etqf & E1000_ETQF_FILTER_ENABLE) {
4790                 filter->ether_type = etqf & E1000_ETQF_ETHERTYPE;
4791                 filter->flags = 0;
4792                 filter->queue = (etqf & E1000_ETQF_QUEUE) >>
4793                                 E1000_ETQF_QUEUE_SHIFT;
4794                 return 0;
4795         }
4796
4797         return -ENOENT;
4798 }
4799
4800 /*
4801  * igb_ethertype_filter_handle - Handle operations for ethertype filter.
4802  * @dev: pointer to rte_eth_dev structure
4803  * @filter_op:operation will be taken.
4804  * @arg: a pointer to specific structure corresponding to the filter_op
4805  */
4806 static int
4807 igb_ethertype_filter_handle(struct rte_eth_dev *dev,
4808                                 enum rte_filter_op filter_op,
4809                                 void *arg)
4810 {
4811         struct e1000_hw *hw = E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
4812         int ret;
4813
4814         MAC_TYPE_FILTER_SUP(hw->mac.type);
4815
4816         if (filter_op == RTE_ETH_FILTER_NOP)
4817                 return 0;
4818
4819         if (arg == NULL) {
4820                 PMD_DRV_LOG(ERR, "arg shouldn't be NULL for operation %u.",
4821                             filter_op);
4822                 return -EINVAL;
4823         }
4824
4825         switch (filter_op) {
4826         case RTE_ETH_FILTER_ADD:
4827                 ret = igb_add_del_ethertype_filter(dev,
4828                         (struct rte_eth_ethertype_filter *)arg,
4829                         TRUE);
4830                 break;
4831         case RTE_ETH_FILTER_DELETE:
4832                 ret = igb_add_del_ethertype_filter(dev,
4833                         (struct rte_eth_ethertype_filter *)arg,
4834                         FALSE);
4835                 break;
4836         case RTE_ETH_FILTER_GET:
4837                 ret = igb_get_ethertype_filter(dev,
4838                         (struct rte_eth_ethertype_filter *)arg);
4839                 break;
4840         default:
4841                 PMD_DRV_LOG(ERR, "unsupported operation %u.", filter_op);
4842                 ret = -EINVAL;
4843                 break;
4844         }
4845         return ret;
4846 }
4847
4848 static int
4849 eth_igb_filter_ctrl(struct rte_eth_dev *dev,
4850                      enum rte_filter_type filter_type,
4851                      enum rte_filter_op filter_op,
4852                      void *arg)
4853 {
4854         int ret = -EINVAL;
4855
4856         switch (filter_type) {
4857         case RTE_ETH_FILTER_NTUPLE:
4858                 ret = igb_ntuple_filter_handle(dev, filter_op, arg);
4859                 break;
4860         case RTE_ETH_FILTER_ETHERTYPE:
4861                 ret = igb_ethertype_filter_handle(dev, filter_op, arg);
4862                 break;
4863         case RTE_ETH_FILTER_SYN:
4864                 ret = eth_igb_syn_filter_handle(dev, filter_op, arg);
4865                 break;
4866         case RTE_ETH_FILTER_FLEXIBLE:
4867                 ret = eth_igb_flex_filter_handle(dev, filter_op, arg);
4868                 break;
4869         default:
4870                 PMD_DRV_LOG(WARNING, "Filter type (%d) not supported",
4871                                                         filter_type);
4872                 break;
4873         }
4874
4875         return ret;
4876 }
4877
4878 static int
4879 eth_igb_set_mc_addr_list(struct rte_eth_dev *dev,
4880                          struct ether_addr *mc_addr_set,
4881                          uint32_t nb_mc_addr)
4882 {
4883         struct e1000_hw *hw;
4884
4885         hw = E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
4886         e1000_update_mc_addr_list(hw, (u8 *)mc_addr_set, nb_mc_addr);
4887         return 0;
4888 }
4889
4890 static uint64_t
4891 igb_read_systime_cyclecounter(struct rte_eth_dev *dev)
4892 {
4893         struct e1000_hw *hw = E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
4894         uint64_t systime_cycles;
4895
4896         switch (hw->mac.type) {
4897         case e1000_i210:
4898         case e1000_i211:
4899                 /*
4900                  * Need to read System Time Residue Register to be able
4901                  * to read the other two registers.
4902                  */
4903                 E1000_READ_REG(hw, E1000_SYSTIMR);
4904                 /* SYSTIMEL stores ns and SYSTIMEH stores seconds. */
4905                 systime_cycles = (uint64_t)E1000_READ_REG(hw, E1000_SYSTIML);
4906                 systime_cycles += (uint64_t)E1000_READ_REG(hw, E1000_SYSTIMH)
4907                                 * NSEC_PER_SEC;
4908                 break;
4909         case e1000_82580:
4910         case e1000_i350:
4911         case e1000_i354:
4912                 /*
4913                  * Need to read System Time Residue Register to be able
4914                  * to read the other two registers.
4915                  */
4916                 E1000_READ_REG(hw, E1000_SYSTIMR);
4917                 systime_cycles = (uint64_t)E1000_READ_REG(hw, E1000_SYSTIML);
4918                 /* Only the 8 LSB are valid. */
4919                 systime_cycles |= (uint64_t)(E1000_READ_REG(hw, E1000_SYSTIMH)
4920                                 & 0xff) << 32;
4921                 break;
4922         default:
4923                 systime_cycles = (uint64_t)E1000_READ_REG(hw, E1000_SYSTIML);
4924                 systime_cycles |= (uint64_t)E1000_READ_REG(hw, E1000_SYSTIMH)
4925                                 << 32;
4926                 break;
4927         }
4928
4929         return systime_cycles;
4930 }
4931
4932 static uint64_t
4933 igb_read_rx_tstamp_cyclecounter(struct rte_eth_dev *dev)
4934 {
4935         struct e1000_hw *hw = E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
4936         uint64_t rx_tstamp_cycles;
4937
4938         switch (hw->mac.type) {
4939         case e1000_i210:
4940         case e1000_i211:
4941                 /* RXSTMPL stores ns and RXSTMPH stores seconds. */
4942                 rx_tstamp_cycles = (uint64_t)E1000_READ_REG(hw, E1000_RXSTMPL);
4943                 rx_tstamp_cycles += (uint64_t)E1000_READ_REG(hw, E1000_RXSTMPH)
4944                                 * NSEC_PER_SEC;
4945                 break;
4946         case e1000_82580:
4947         case e1000_i350:
4948         case e1000_i354:
4949                 rx_tstamp_cycles = (uint64_t)E1000_READ_REG(hw, E1000_RXSTMPL);
4950                 /* Only the 8 LSB are valid. */
4951                 rx_tstamp_cycles |= (uint64_t)(E1000_READ_REG(hw, E1000_RXSTMPH)
4952                                 & 0xff) << 32;
4953                 break;
4954         default:
4955                 rx_tstamp_cycles = (uint64_t)E1000_READ_REG(hw, E1000_RXSTMPL);
4956                 rx_tstamp_cycles |= (uint64_t)E1000_READ_REG(hw, E1000_RXSTMPH)
4957                                 << 32;
4958                 break;
4959         }
4960
4961         return rx_tstamp_cycles;
4962 }
4963
4964 static uint64_t
4965 igb_read_tx_tstamp_cyclecounter(struct rte_eth_dev *dev)
4966 {
4967         struct e1000_hw *hw = E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
4968         uint64_t tx_tstamp_cycles;
4969
4970         switch (hw->mac.type) {
4971         case e1000_i210:
4972         case e1000_i211:
4973                 /* RXSTMPL stores ns and RXSTMPH stores seconds. */
4974                 tx_tstamp_cycles = (uint64_t)E1000_READ_REG(hw, E1000_TXSTMPL);
4975                 tx_tstamp_cycles += (uint64_t)E1000_READ_REG(hw, E1000_TXSTMPH)
4976                                 * NSEC_PER_SEC;
4977                 break;
4978         case e1000_82580:
4979         case e1000_i350:
4980         case e1000_i354:
4981                 tx_tstamp_cycles = (uint64_t)E1000_READ_REG(hw, E1000_TXSTMPL);
4982                 /* Only the 8 LSB are valid. */
4983                 tx_tstamp_cycles |= (uint64_t)(E1000_READ_REG(hw, E1000_TXSTMPH)
4984                                 & 0xff) << 32;
4985                 break;
4986         default:
4987                 tx_tstamp_cycles = (uint64_t)E1000_READ_REG(hw, E1000_TXSTMPL);
4988                 tx_tstamp_cycles |= (uint64_t)E1000_READ_REG(hw, E1000_TXSTMPH)
4989                                 << 32;
4990                 break;
4991         }
4992
4993         return tx_tstamp_cycles;
4994 }
4995
4996 static void
4997 igb_start_timecounters(struct rte_eth_dev *dev)
4998 {
4999         struct e1000_hw *hw = E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
5000         struct e1000_adapter *adapter =
5001                 (struct e1000_adapter *)dev->data->dev_private;
5002         uint32_t incval = 1;
5003         uint32_t shift = 0;
5004         uint64_t mask = E1000_CYCLECOUNTER_MASK;
5005
5006         switch (hw->mac.type) {
5007         case e1000_82580:
5008         case e1000_i350:
5009         case e1000_i354:
5010                 /* 32 LSB bits + 8 MSB bits = 40 bits */
5011                 mask = (1ULL << 40) - 1;
5012                 /* fall-through */
5013         case e1000_i210:
5014         case e1000_i211:
5015                 /*
5016                  * Start incrementing the register
5017                  * used to timestamp PTP packets.
5018                  */
5019                 E1000_WRITE_REG(hw, E1000_TIMINCA, incval);
5020                 break;
5021         case e1000_82576:
5022                 incval = E1000_INCVALUE_82576;
5023                 shift = IGB_82576_TSYNC_SHIFT;
5024                 E1000_WRITE_REG(hw, E1000_TIMINCA,
5025                                 E1000_INCPERIOD_82576 | incval);
5026                 break;
5027         default:
5028                 /* Not supported */
5029                 return;
5030         }
5031
5032         memset(&adapter->systime_tc, 0, sizeof(struct rte_timecounter));
5033         memset(&adapter->rx_tstamp_tc, 0, sizeof(struct rte_timecounter));
5034         memset(&adapter->tx_tstamp_tc, 0, sizeof(struct rte_timecounter));
5035
5036         adapter->systime_tc.cc_mask = mask;
5037         adapter->systime_tc.cc_shift = shift;
5038         adapter->systime_tc.nsec_mask = (1ULL << shift) - 1;
5039
5040         adapter->rx_tstamp_tc.cc_mask = mask;
5041         adapter->rx_tstamp_tc.cc_shift = shift;
5042         adapter->rx_tstamp_tc.nsec_mask = (1ULL << shift) - 1;
5043
5044         adapter->tx_tstamp_tc.cc_mask = mask;
5045         adapter->tx_tstamp_tc.cc_shift = shift;
5046         adapter->tx_tstamp_tc.nsec_mask = (1ULL << shift) - 1;
5047 }
5048
5049 static int
5050 igb_timesync_adjust_time(struct rte_eth_dev *dev, int64_t delta)
5051 {
5052         struct e1000_adapter *adapter =
5053                         (struct e1000_adapter *)dev->data->dev_private;
5054
5055         adapter->systime_tc.nsec += delta;
5056         adapter->rx_tstamp_tc.nsec += delta;
5057         adapter->tx_tstamp_tc.nsec += delta;
5058
5059         return 0;
5060 }
5061
5062 static int
5063 igb_timesync_write_time(struct rte_eth_dev *dev, const struct timespec *ts)
5064 {
5065         uint64_t ns;
5066         struct e1000_adapter *adapter =
5067                         (struct e1000_adapter *)dev->data->dev_private;
5068
5069         ns = rte_timespec_to_ns(ts);
5070
5071         /* Set the timecounters to a new value. */
5072         adapter->systime_tc.nsec = ns;
5073         adapter->rx_tstamp_tc.nsec = ns;
5074         adapter->tx_tstamp_tc.nsec = ns;
5075
5076         return 0;
5077 }
5078
5079 static int
5080 igb_timesync_read_time(struct rte_eth_dev *dev, struct timespec *ts)
5081 {
5082         uint64_t ns, systime_cycles;
5083         struct e1000_adapter *adapter =
5084                         (struct e1000_adapter *)dev->data->dev_private;
5085
5086         systime_cycles = igb_read_systime_cyclecounter(dev);
5087         ns = rte_timecounter_update(&adapter->systime_tc, systime_cycles);
5088         *ts = rte_ns_to_timespec(ns);
5089
5090         return 0;
5091 }
5092
5093 static int
5094 igb_timesync_enable(struct rte_eth_dev *dev)
5095 {
5096         struct e1000_hw *hw = E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
5097         uint32_t tsync_ctl;
5098         uint32_t tsauxc;
5099
5100         /* Stop the timesync system time. */
5101         E1000_WRITE_REG(hw, E1000_TIMINCA, 0x0);
5102         /* Reset the timesync system time value. */
5103         switch (hw->mac.type) {
5104         case e1000_82580:
5105         case e1000_i350:
5106         case e1000_i354:
5107         case e1000_i210:
5108         case e1000_i211:
5109                 E1000_WRITE_REG(hw, E1000_SYSTIMR, 0x0);
5110                 /* fall-through */
5111         case e1000_82576:
5112                 E1000_WRITE_REG(hw, E1000_SYSTIML, 0x0);
5113                 E1000_WRITE_REG(hw, E1000_SYSTIMH, 0x0);
5114                 break;
5115         default:
5116                 /* Not supported. */
5117                 return -ENOTSUP;
5118         }
5119
5120         /* Enable system time for it isn't on by default. */
5121         tsauxc = E1000_READ_REG(hw, E1000_TSAUXC);
5122         tsauxc &= ~E1000_TSAUXC_DISABLE_SYSTIME;
5123         E1000_WRITE_REG(hw, E1000_TSAUXC, tsauxc);
5124
5125         igb_start_timecounters(dev);
5126
5127         /* Enable L2 filtering of IEEE1588/802.1AS Ethernet frame types. */
5128         E1000_WRITE_REG(hw, E1000_ETQF(E1000_ETQF_FILTER_1588),
5129                         (ETHER_TYPE_1588 |
5130                          E1000_ETQF_FILTER_ENABLE |
5131                          E1000_ETQF_1588));
5132
5133         /* Enable timestamping of received PTP packets. */
5134         tsync_ctl = E1000_READ_REG(hw, E1000_TSYNCRXCTL);
5135         tsync_ctl |= E1000_TSYNCRXCTL_ENABLED;
5136         E1000_WRITE_REG(hw, E1000_TSYNCRXCTL, tsync_ctl);
5137
5138         /* Enable Timestamping of transmitted PTP packets. */
5139         tsync_ctl = E1000_READ_REG(hw, E1000_TSYNCTXCTL);
5140         tsync_ctl |= E1000_TSYNCTXCTL_ENABLED;
5141         E1000_WRITE_REG(hw, E1000_TSYNCTXCTL, tsync_ctl);
5142
5143         return 0;
5144 }
5145
5146 static int
5147 igb_timesync_disable(struct rte_eth_dev *dev)
5148 {
5149         struct e1000_hw *hw = E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
5150         uint32_t tsync_ctl;
5151
5152         /* Disable timestamping of transmitted PTP packets. */
5153         tsync_ctl = E1000_READ_REG(hw, E1000_TSYNCTXCTL);
5154         tsync_ctl &= ~E1000_TSYNCTXCTL_ENABLED;
5155         E1000_WRITE_REG(hw, E1000_TSYNCTXCTL, tsync_ctl);
5156
5157         /* Disable timestamping of received PTP packets. */
5158         tsync_ctl = E1000_READ_REG(hw, E1000_TSYNCRXCTL);
5159         tsync_ctl &= ~E1000_TSYNCRXCTL_ENABLED;
5160         E1000_WRITE_REG(hw, E1000_TSYNCRXCTL, tsync_ctl);
5161
5162         /* Disable L2 filtering of IEEE1588/802.1AS Ethernet frame types. */
5163         E1000_WRITE_REG(hw, E1000_ETQF(E1000_ETQF_FILTER_1588), 0);
5164
5165         /* Stop incrementating the System Time registers. */
5166         E1000_WRITE_REG(hw, E1000_TIMINCA, 0);
5167
5168         return 0;
5169 }
5170
5171 static int
5172 igb_timesync_read_rx_timestamp(struct rte_eth_dev *dev,
5173                                struct timespec *timestamp,
5174                                uint32_t flags __rte_unused)
5175 {
5176         struct e1000_hw *hw = E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
5177         struct e1000_adapter *adapter =
5178                         (struct e1000_adapter *)dev->data->dev_private;
5179         uint32_t tsync_rxctl;
5180         uint64_t rx_tstamp_cycles;
5181         uint64_t ns;
5182
5183         tsync_rxctl = E1000_READ_REG(hw, E1000_TSYNCRXCTL);
5184         if ((tsync_rxctl & E1000_TSYNCRXCTL_VALID) == 0)
5185                 return -EINVAL;
5186
5187         rx_tstamp_cycles = igb_read_rx_tstamp_cyclecounter(dev);
5188         ns = rte_timecounter_update(&adapter->rx_tstamp_tc, rx_tstamp_cycles);
5189         *timestamp = rte_ns_to_timespec(ns);
5190
5191         return  0;
5192 }
5193
5194 static int
5195 igb_timesync_read_tx_timestamp(struct rte_eth_dev *dev,
5196                                struct timespec *timestamp)
5197 {
5198         struct e1000_hw *hw = E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
5199         struct e1000_adapter *adapter =
5200                         (struct e1000_adapter *)dev->data->dev_private;
5201         uint32_t tsync_txctl;
5202         uint64_t tx_tstamp_cycles;
5203         uint64_t ns;
5204
5205         tsync_txctl = E1000_READ_REG(hw, E1000_TSYNCTXCTL);
5206         if ((tsync_txctl & E1000_TSYNCTXCTL_VALID) == 0)
5207                 return -EINVAL;
5208
5209         tx_tstamp_cycles = igb_read_tx_tstamp_cyclecounter(dev);
5210         ns = rte_timecounter_update(&adapter->tx_tstamp_tc, tx_tstamp_cycles);
5211         *timestamp = rte_ns_to_timespec(ns);
5212
5213         return  0;
5214 }
5215
5216 static int
5217 eth_igb_get_reg_length(struct rte_eth_dev *dev __rte_unused)
5218 {
5219         int count = 0;
5220         int g_ind = 0;
5221         const struct reg_info *reg_group;
5222
5223         while ((reg_group = igb_regs[g_ind++]))
5224                 count += igb_reg_group_count(reg_group);
5225
5226         return count;
5227 }
5228
5229 static int
5230 igbvf_get_reg_length(struct rte_eth_dev *dev __rte_unused)
5231 {
5232         int count = 0;
5233         int g_ind = 0;
5234         const struct reg_info *reg_group;
5235
5236         while ((reg_group = igbvf_regs[g_ind++]))
5237                 count += igb_reg_group_count(reg_group);
5238
5239         return count;
5240 }
5241
5242 static int
5243 eth_igb_get_regs(struct rte_eth_dev *dev,
5244         struct rte_dev_reg_info *regs)
5245 {
5246         struct e1000_hw *hw = E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
5247         uint32_t *data = regs->data;
5248         int g_ind = 0;
5249         int count = 0;
5250         const struct reg_info *reg_group;
5251
5252         if (data == NULL) {
5253                 regs->length = eth_igb_get_reg_length(dev);
5254                 regs->width = sizeof(uint32_t);
5255                 return 0;
5256         }
5257
5258         /* Support only full register dump */
5259         if ((regs->length == 0) ||
5260             (regs->length == (uint32_t)eth_igb_get_reg_length(dev))) {
5261                 regs->version = hw->mac.type << 24 | hw->revision_id << 16 |
5262                         hw->device_id;
5263                 while ((reg_group = igb_regs[g_ind++]))
5264                         count += igb_read_regs_group(dev, &data[count],
5265                                                         reg_group);
5266                 return 0;
5267         }
5268
5269         return -ENOTSUP;
5270 }
5271
5272 static int
5273 igbvf_get_regs(struct rte_eth_dev *dev,
5274         struct rte_dev_reg_info *regs)
5275 {
5276         struct e1000_hw *hw = E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
5277         uint32_t *data = regs->data;
5278         int g_ind = 0;
5279         int count = 0;
5280         const struct reg_info *reg_group;
5281
5282         if (data == NULL) {
5283                 regs->length = igbvf_get_reg_length(dev);
5284                 regs->width = sizeof(uint32_t);
5285                 return 0;
5286         }
5287
5288         /* Support only full register dump */
5289         if ((regs->length == 0) ||
5290             (regs->length == (uint32_t)igbvf_get_reg_length(dev))) {
5291                 regs->version = hw->mac.type << 24 | hw->revision_id << 16 |
5292                         hw->device_id;
5293                 while ((reg_group = igbvf_regs[g_ind++]))
5294                         count += igb_read_regs_group(dev, &data[count],
5295                                                         reg_group);
5296                 return 0;
5297         }
5298
5299         return -ENOTSUP;
5300 }
5301
5302 static int
5303 eth_igb_get_eeprom_length(struct rte_eth_dev *dev)
5304 {
5305         struct e1000_hw *hw = E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
5306
5307         /* Return unit is byte count */
5308         return hw->nvm.word_size * 2;
5309 }
5310
5311 static int
5312 eth_igb_get_eeprom(struct rte_eth_dev *dev,
5313         struct rte_dev_eeprom_info *in_eeprom)
5314 {
5315         struct e1000_hw *hw = E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
5316         struct e1000_nvm_info *nvm = &hw->nvm;
5317         uint16_t *data = in_eeprom->data;
5318         int first, length;
5319
5320         first = in_eeprom->offset >> 1;
5321         length = in_eeprom->length >> 1;
5322         if ((first >= hw->nvm.word_size) ||
5323             ((first + length) >= hw->nvm.word_size))
5324                 return -EINVAL;
5325
5326         in_eeprom->magic = hw->vendor_id |
5327                 ((uint32_t)hw->device_id << 16);
5328
5329         if ((nvm->ops.read) == NULL)
5330                 return -ENOTSUP;
5331
5332         return nvm->ops.read(hw, first, length, data);
5333 }
5334
5335 static int
5336 eth_igb_set_eeprom(struct rte_eth_dev *dev,
5337         struct rte_dev_eeprom_info *in_eeprom)
5338 {
5339         struct e1000_hw *hw = E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
5340         struct e1000_nvm_info *nvm = &hw->nvm;
5341         uint16_t *data = in_eeprom->data;
5342         int first, length;
5343
5344         first = in_eeprom->offset >> 1;
5345         length = in_eeprom->length >> 1;
5346         if ((first >= hw->nvm.word_size) ||
5347             ((first + length) >= hw->nvm.word_size))
5348                 return -EINVAL;
5349
5350         in_eeprom->magic = (uint32_t)hw->vendor_id |
5351                 ((uint32_t)hw->device_id << 16);
5352
5353         if ((nvm->ops.write) == NULL)
5354                 return -ENOTSUP;
5355         return nvm->ops.write(hw,  first, length, data);
5356 }
5357
5358 static int
5359 eth_igb_rx_queue_intr_disable(struct rte_eth_dev *dev, uint16_t queue_id)
5360 {
5361         struct e1000_hw *hw =
5362                 E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
5363         uint32_t mask = 1 << queue_id;
5364
5365         E1000_WRITE_REG(hw, E1000_EIMC, mask);
5366         E1000_WRITE_FLUSH(hw);
5367
5368         return 0;
5369 }
5370
5371 static int
5372 eth_igb_rx_queue_intr_enable(struct rte_eth_dev *dev, uint16_t queue_id)
5373 {
5374         struct e1000_hw *hw =
5375                 E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
5376         struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
5377         struct rte_intr_handle *intr_handle = &pci_dev->intr_handle;
5378         uint32_t mask = 1 << queue_id;
5379         uint32_t regval;
5380
5381         regval = E1000_READ_REG(hw, E1000_EIMS);
5382         E1000_WRITE_REG(hw, E1000_EIMS, regval | mask);
5383         E1000_WRITE_FLUSH(hw);
5384
5385         rte_intr_enable(intr_handle);
5386
5387         return 0;
5388 }
5389
5390 static void
5391 eth_igb_write_ivar(struct e1000_hw *hw, uint8_t  msix_vector,
5392                    uint8_t index, uint8_t offset)
5393 {
5394         uint32_t val = E1000_READ_REG_ARRAY(hw, E1000_IVAR0, index);
5395
5396         /* clear bits */
5397         val &= ~((uint32_t)0xFF << offset);
5398
5399         /* write vector and valid bit */
5400         val |= (msix_vector | E1000_IVAR_VALID) << offset;
5401
5402         E1000_WRITE_REG_ARRAY(hw, E1000_IVAR0, index, val);
5403 }
5404
5405 static void
5406 eth_igb_assign_msix_vector(struct e1000_hw *hw, int8_t direction,
5407                            uint8_t queue, uint8_t msix_vector)
5408 {
5409         uint32_t tmp = 0;
5410
5411         if (hw->mac.type == e1000_82575) {
5412                 if (direction == 0)
5413                         tmp = E1000_EICR_RX_QUEUE0 << queue;
5414                 else if (direction == 1)
5415                         tmp = E1000_EICR_TX_QUEUE0 << queue;
5416                 E1000_WRITE_REG(hw, E1000_MSIXBM(msix_vector), tmp);
5417         } else if (hw->mac.type == e1000_82576) {
5418                 if ((direction == 0) || (direction == 1))
5419                         eth_igb_write_ivar(hw, msix_vector, queue & 0x7,
5420                                            ((queue & 0x8) << 1) +
5421                                            8 * direction);
5422         } else if ((hw->mac.type == e1000_82580) ||
5423                         (hw->mac.type == e1000_i350) ||
5424                         (hw->mac.type == e1000_i354) ||
5425                         (hw->mac.type == e1000_i210) ||
5426                         (hw->mac.type == e1000_i211)) {
5427                 if ((direction == 0) || (direction == 1))
5428                         eth_igb_write_ivar(hw, msix_vector,
5429                                            queue >> 1,
5430                                            ((queue & 0x1) << 4) +
5431                                            8 * direction);
5432         }
5433 }
5434
5435 /* Sets up the hardware to generate MSI-X interrupts properly
5436  * @hw
5437  *  board private structure
5438  */
5439 static void
5440 eth_igb_configure_msix_intr(struct rte_eth_dev *dev)
5441 {
5442         int queue_id;
5443         uint32_t tmpval, regval, intr_mask;
5444         struct e1000_hw *hw =
5445                 E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
5446         uint32_t vec = E1000_MISC_VEC_ID;
5447         uint32_t base = E1000_MISC_VEC_ID;
5448         uint32_t misc_shift = 0;
5449         struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
5450         struct rte_intr_handle *intr_handle = &pci_dev->intr_handle;
5451
5452         /* won't configure msix register if no mapping is done
5453          * between intr vector and event fd
5454          */
5455         if (!rte_intr_dp_is_en(intr_handle))
5456                 return;
5457
5458         if (rte_intr_allow_others(intr_handle)) {
5459                 vec = base = E1000_RX_VEC_START;
5460                 misc_shift = 1;
5461         }
5462
5463         /* set interrupt vector for other causes */
5464         if (hw->mac.type == e1000_82575) {
5465                 tmpval = E1000_READ_REG(hw, E1000_CTRL_EXT);
5466                 /* enable MSI-X PBA support */
5467                 tmpval |= E1000_CTRL_EXT_PBA_CLR;
5468
5469                 /* Auto-Mask interrupts upon ICR read */
5470                 tmpval |= E1000_CTRL_EXT_EIAME;
5471                 tmpval |= E1000_CTRL_EXT_IRCA;
5472
5473                 E1000_WRITE_REG(hw, E1000_CTRL_EXT, tmpval);
5474
5475                 /* enable msix_other interrupt */
5476                 E1000_WRITE_REG_ARRAY(hw, E1000_MSIXBM(0), 0, E1000_EIMS_OTHER);
5477                 regval = E1000_READ_REG(hw, E1000_EIAC);
5478                 E1000_WRITE_REG(hw, E1000_EIAC, regval | E1000_EIMS_OTHER);
5479                 regval = E1000_READ_REG(hw, E1000_EIAM);
5480                 E1000_WRITE_REG(hw, E1000_EIMS, regval | E1000_EIMS_OTHER);
5481         } else if ((hw->mac.type == e1000_82576) ||
5482                         (hw->mac.type == e1000_82580) ||
5483                         (hw->mac.type == e1000_i350) ||
5484                         (hw->mac.type == e1000_i354) ||
5485                         (hw->mac.type == e1000_i210) ||
5486                         (hw->mac.type == e1000_i211)) {
5487                 /* turn on MSI-X capability first */
5488                 E1000_WRITE_REG(hw, E1000_GPIE, E1000_GPIE_MSIX_MODE |
5489                                         E1000_GPIE_PBA | E1000_GPIE_EIAME |
5490                                         E1000_GPIE_NSICR);
5491                 intr_mask = RTE_LEN2MASK(intr_handle->nb_efd, uint32_t) <<
5492                         misc_shift;
5493                 regval = E1000_READ_REG(hw, E1000_EIAC);
5494                 E1000_WRITE_REG(hw, E1000_EIAC, regval | intr_mask);
5495
5496                 /* enable msix_other interrupt */
5497                 regval = E1000_READ_REG(hw, E1000_EIMS);
5498                 E1000_WRITE_REG(hw, E1000_EIMS, regval | intr_mask);
5499                 tmpval = (dev->data->nb_rx_queues | E1000_IVAR_VALID) << 8;
5500                 E1000_WRITE_REG(hw, E1000_IVAR_MISC, tmpval);
5501         }
5502
5503         /* use EIAM to auto-mask when MSI-X interrupt
5504          * is asserted, this saves a register write for every interrupt
5505          */
5506         intr_mask = RTE_LEN2MASK(intr_handle->nb_efd, uint32_t) <<
5507                 misc_shift;
5508         regval = E1000_READ_REG(hw, E1000_EIAM);
5509         E1000_WRITE_REG(hw, E1000_EIAM, regval | intr_mask);
5510
5511         for (queue_id = 0; queue_id < dev->data->nb_rx_queues; queue_id++) {
5512                 eth_igb_assign_msix_vector(hw, 0, queue_id, vec);
5513                 intr_handle->intr_vec[queue_id] = vec;
5514                 if (vec < base + intr_handle->nb_efd - 1)
5515                         vec++;
5516         }
5517
5518         E1000_WRITE_FLUSH(hw);
5519 }
5520
5521 /* restore n-tuple filter */
5522 static inline void
5523 igb_ntuple_filter_restore(struct rte_eth_dev *dev)
5524 {
5525         struct e1000_filter_info *filter_info =
5526                 E1000_DEV_PRIVATE_TO_FILTER_INFO(dev->data->dev_private);
5527         struct e1000_5tuple_filter *p_5tuple;
5528         struct e1000_2tuple_filter *p_2tuple;
5529
5530         TAILQ_FOREACH(p_5tuple, &filter_info->fivetuple_list, entries) {
5531                 igb_inject_5tuple_filter_82576(dev, p_5tuple);
5532         }
5533
5534         TAILQ_FOREACH(p_2tuple, &filter_info->twotuple_list, entries) {
5535                 igb_inject_2uple_filter(dev, p_2tuple);
5536         }
5537 }
5538
5539 /* restore SYN filter */
5540 static inline void
5541 igb_syn_filter_restore(struct rte_eth_dev *dev)
5542 {
5543         struct e1000_hw *hw = E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
5544         struct e1000_filter_info *filter_info =
5545                 E1000_DEV_PRIVATE_TO_FILTER_INFO(dev->data->dev_private);
5546         uint32_t synqf;
5547
5548         synqf = filter_info->syn_info;
5549
5550         if (synqf & E1000_SYN_FILTER_ENABLE) {
5551                 E1000_WRITE_REG(hw, E1000_SYNQF(0), synqf);
5552                 E1000_WRITE_FLUSH(hw);
5553         }
5554 }
5555
5556 /* restore ethernet type filter */
5557 static inline void
5558 igb_ethertype_filter_restore(struct rte_eth_dev *dev)
5559 {
5560         struct e1000_hw *hw = E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
5561         struct e1000_filter_info *filter_info =
5562                 E1000_DEV_PRIVATE_TO_FILTER_INFO(dev->data->dev_private);
5563         int i;
5564
5565         for (i = 0; i < E1000_MAX_ETQF_FILTERS; i++) {
5566                 if (filter_info->ethertype_mask & (1 << i)) {
5567                         E1000_WRITE_REG(hw, E1000_ETQF(i),
5568                                 filter_info->ethertype_filters[i].etqf);
5569                         E1000_WRITE_FLUSH(hw);
5570                 }
5571         }
5572 }
5573
5574 /* restore flex byte filter */
5575 static inline void
5576 igb_flex_filter_restore(struct rte_eth_dev *dev)
5577 {
5578         struct e1000_filter_info *filter_info =
5579                 E1000_DEV_PRIVATE_TO_FILTER_INFO(dev->data->dev_private);
5580         struct e1000_flex_filter *flex_filter;
5581
5582         TAILQ_FOREACH(flex_filter, &filter_info->flex_list, entries) {
5583                 igb_inject_flex_filter(dev, flex_filter);
5584         }
5585 }
5586
5587 /* restore all types filter */
5588 static int
5589 igb_filter_restore(struct rte_eth_dev *dev)
5590 {
5591         igb_ntuple_filter_restore(dev);
5592         igb_ethertype_filter_restore(dev);
5593         igb_syn_filter_restore(dev);
5594         igb_flex_filter_restore(dev);
5595
5596         return 0;
5597 }
5598
5599 RTE_PMD_REGISTER_PCI(net_e1000_igb, rte_igb_pmd);
5600 RTE_PMD_REGISTER_PCI_TABLE(net_e1000_igb, pci_id_igb_map);
5601 RTE_PMD_REGISTER_KMOD_DEP(net_e1000_igb, "* igb_uio | uio_pci_generic | vfio-pci");
5602 RTE_PMD_REGISTER_PCI(net_e1000_igb_vf, rte_igbvf_pmd);
5603 RTE_PMD_REGISTER_PCI_TABLE(net_e1000_igb_vf, pci_id_igbvf_map);
5604 RTE_PMD_REGISTER_KMOD_DEP(net_e1000_igb_vf, "* igb_uio | vfio-pci");