ixgbe: fix vf irq storm when running on Xen Dom0.
[dpdk.git] / lib / librte_pmd_ixgbe / ixgbe_ethdev.c
1 /*-
2  *   BSD LICENSE
3  * 
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  * 
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  * 
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  * 
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include <sys/queue.h>
35 #include <stdio.h>
36 #include <errno.h>
37 #include <stdint.h>
38 #include <string.h>
39 #include <unistd.h>
40 #include <stdarg.h>
41 #include <inttypes.h>
42 #include <rte_byteorder.h>
43 #include <rte_common.h>
44 #include <rte_cycles.h>
45
46 #include <rte_interrupts.h>
47 #include <rte_log.h>
48 #include <rte_debug.h>
49 #include <rte_pci.h>
50 #include <rte_atomic.h>
51 #include <rte_branch_prediction.h>
52 #include <rte_memory.h>
53 #include <rte_memzone.h>
54 #include <rte_tailq.h>
55 #include <rte_eal.h>
56 #include <rte_alarm.h>
57 #include <rte_ether.h>
58 #include <rte_ethdev.h>
59 #include <rte_atomic.h>
60 #include <rte_malloc.h>
61
62 #include "ixgbe_logs.h"
63 #include "ixgbe/ixgbe_api.h"
64 #include "ixgbe/ixgbe_vf.h"
65 #include "ixgbe/ixgbe_common.h"
66 #include "ixgbe_ethdev.h"
67 #include "ixgbe_bypass.h"
68
69 /*
70  * High threshold controlling when to start sending XOFF frames. Must be at
71  * least 8 bytes less than receive packet buffer size. This value is in units
72  * of 1024 bytes.
73  */
74 #define IXGBE_FC_HI    0x80
75
76 /*
77  * Low threshold controlling when to start sending XON frames. This value is
78  * in units of 1024 bytes.
79  */
80 #define IXGBE_FC_LO    0x40
81
82 /* Timer value included in XOFF frames. */
83 #define IXGBE_FC_PAUSE 0x680
84
85 #define IXGBE_LINK_DOWN_CHECK_TIMEOUT 4000 /* ms */
86 #define IXGBE_LINK_UP_CHECK_TIMEOUT   1000 /* ms */
87 #define IXGBE_VMDQ_NUM_UC_MAC         4096 /* Maximum nb. of UC MAC addr. */
88
89
90 #define IXGBEVF_PMD_NAME "rte_ixgbevf_pmd" /* PMD name */
91
92 #define IXGBE_QUEUE_STAT_COUNTERS (sizeof(hw_stats->qprc) / sizeof(hw_stats->qprc[0]))
93
94 static int eth_ixgbe_dev_init(struct eth_driver *eth_drv,
95                 struct rte_eth_dev *eth_dev);
96 static int  ixgbe_dev_configure(struct rte_eth_dev *dev);
97 static int  ixgbe_dev_start(struct rte_eth_dev *dev);
98 static void ixgbe_dev_stop(struct rte_eth_dev *dev);
99 static void ixgbe_dev_close(struct rte_eth_dev *dev);
100 static void ixgbe_dev_promiscuous_enable(struct rte_eth_dev *dev);
101 static void ixgbe_dev_promiscuous_disable(struct rte_eth_dev *dev);
102 static void ixgbe_dev_allmulticast_enable(struct rte_eth_dev *dev);
103 static void ixgbe_dev_allmulticast_disable(struct rte_eth_dev *dev);
104 static int ixgbe_dev_link_update(struct rte_eth_dev *dev,
105                                 int wait_to_complete);
106 static void ixgbe_dev_stats_get(struct rte_eth_dev *dev,
107                                 struct rte_eth_stats *stats);
108 static void ixgbe_dev_stats_reset(struct rte_eth_dev *dev);
109 static int ixgbe_dev_queue_stats_mapping_set(struct rte_eth_dev *eth_dev,
110                                              uint16_t queue_id,
111                                              uint8_t stat_idx,
112                                              uint8_t is_rx);
113 static void ixgbe_dev_info_get(struct rte_eth_dev *dev,
114                                 struct rte_eth_dev_info *dev_info);
115 static int ixgbe_vlan_filter_set(struct rte_eth_dev *dev,
116                 uint16_t vlan_id, int on);
117 static void ixgbe_vlan_tpid_set(struct rte_eth_dev *dev, uint16_t tpid_id);
118 static void ixgbe_vlan_hw_strip_bitmap_set(struct rte_eth_dev *dev, 
119                 uint16_t queue, bool on);
120 static void ixgbe_vlan_strip_queue_set(struct rte_eth_dev *dev, uint16_t queue,
121                 int on);
122 static void ixgbe_vlan_offload_set(struct rte_eth_dev *dev, int mask);
123 static void ixgbe_vlan_hw_strip_enable(struct rte_eth_dev *dev, uint16_t queue);
124 static void ixgbe_vlan_hw_strip_disable(struct rte_eth_dev *dev, uint16_t queue);
125 static void ixgbe_vlan_hw_extend_enable(struct rte_eth_dev *dev);
126 static void ixgbe_vlan_hw_extend_disable(struct rte_eth_dev *dev);
127
128 static int ixgbe_dev_led_on(struct rte_eth_dev *dev);
129 static int ixgbe_dev_led_off(struct rte_eth_dev *dev);
130 static int  ixgbe_flow_ctrl_set(struct rte_eth_dev *dev,
131                 struct rte_eth_fc_conf *fc_conf);
132 static int ixgbe_priority_flow_ctrl_set(struct rte_eth_dev *dev,
133                 struct rte_eth_pfc_conf *pfc_conf);
134 static int ixgbe_dev_rss_reta_update(struct rte_eth_dev *dev,
135                 struct rte_eth_rss_reta *reta_conf);
136 static int ixgbe_dev_rss_reta_query(struct rte_eth_dev *dev,
137                 struct rte_eth_rss_reta *reta_conf);    
138 static void ixgbe_dev_link_status_print(struct rte_eth_dev *dev);
139 static int ixgbe_dev_lsc_interrupt_setup(struct rte_eth_dev *dev);
140 static int ixgbe_dev_interrupt_get_status(struct rte_eth_dev *dev);
141 static int ixgbe_dev_interrupt_action(struct rte_eth_dev *dev);
142 static void ixgbe_dev_interrupt_handler(struct rte_intr_handle *handle,
143                 void *param);
144 static void ixgbe_dev_interrupt_delayed_handler(void *param);
145 static void ixgbe_add_rar(struct rte_eth_dev *dev, struct ether_addr *mac_addr,
146                 uint32_t index, uint32_t pool);
147 static void ixgbe_remove_rar(struct rte_eth_dev *dev, uint32_t index);
148 static void ixgbe_dcb_init(struct ixgbe_hw *hw,struct ixgbe_dcb_config *dcb_config);
149
150 /* For Virtual Function support */
151 static int eth_ixgbevf_dev_init(struct eth_driver *eth_drv,
152                 struct rte_eth_dev *eth_dev);
153 static int  ixgbevf_dev_configure(struct rte_eth_dev *dev);
154 static int  ixgbevf_dev_start(struct rte_eth_dev *dev);
155 static void ixgbevf_dev_stop(struct rte_eth_dev *dev);
156 static void ixgbevf_dev_close(struct rte_eth_dev *dev);
157 static void ixgbevf_intr_disable(struct ixgbe_hw *hw);
158 static void ixgbevf_dev_stats_get(struct rte_eth_dev *dev,
159                 struct rte_eth_stats *stats);
160 static void ixgbevf_dev_stats_reset(struct rte_eth_dev *dev);
161 static int ixgbevf_vlan_filter_set(struct rte_eth_dev *dev, 
162                 uint16_t vlan_id, int on);
163 static void ixgbevf_vlan_strip_queue_set(struct rte_eth_dev *dev,
164                 uint16_t queue, int on);
165 static void ixgbevf_vlan_offload_set(struct rte_eth_dev *dev, int mask);
166 static void ixgbevf_set_vfta_all(struct rte_eth_dev *dev, bool on);
167
168 /* For Eth VMDQ APIs support */
169 static int ixgbe_uc_hash_table_set(struct rte_eth_dev *dev, struct
170                 ether_addr* mac_addr,uint8_t on);
171 static int ixgbe_uc_all_hash_table_set(struct rte_eth_dev *dev,uint8_t on);
172 static int  ixgbe_set_pool_rx_mode(struct rte_eth_dev *dev,  uint16_t pool, 
173                 uint16_t rx_mask, uint8_t on);
174 static int ixgbe_set_pool_rx(struct rte_eth_dev *dev,uint16_t pool,uint8_t on);
175 static int ixgbe_set_pool_tx(struct rte_eth_dev *dev,uint16_t pool,uint8_t on);
176 static int ixgbe_set_pool_vlan_filter(struct rte_eth_dev *dev, uint16_t vlan, 
177                 uint64_t pool_mask,uint8_t vlan_on);
178 static int ixgbe_mirror_rule_set(struct rte_eth_dev *dev, 
179                 struct rte_eth_vmdq_mirror_conf *mirror_conf, 
180                 uint8_t rule_id, uint8_t on);
181 static int ixgbe_mirror_rule_reset(struct rte_eth_dev *dev,
182                 uint8_t rule_id);
183
184 /*
185  * Define VF Stats MACRO for Non "cleared on read" register
186  */
187 #define UPDATE_VF_STAT(reg, last, cur)                          \
188 {                                                               \
189         u32 latest = IXGBE_READ_REG(hw, reg);                   \
190         cur += latest - last;                                   \
191         last = latest;                                          \
192 }
193
194 #define UPDATE_VF_STAT_36BIT(lsb, msb, last, cur)                \
195 {                                                                \
196         u64 new_lsb = IXGBE_READ_REG(hw, lsb);                   \
197         u64 new_msb = IXGBE_READ_REG(hw, msb);                   \
198         u64 latest = ((new_msb << 32) | new_lsb);                \
199         cur += (0x1000000000LL + latest - last) & 0xFFFFFFFFFLL; \
200         last = latest;                                           \
201 }
202
203 #define IXGBE_SET_HWSTRIP(h, q) do{\
204                 uint32_t idx = (q) / (sizeof ((h)->bitmap[0]) * NBBY); \
205                 uint32_t bit = (q) % (sizeof ((h)->bitmap[0]) * NBBY); \
206                 (h)->bitmap[idx] |= 1 << bit;\
207         }while(0)
208         
209 #define IXGBE_CLEAR_HWSTRIP(h, q) do{\
210                 uint32_t idx = (q) / (sizeof ((h)->bitmap[0]) * NBBY); \
211                 uint32_t bit = (q) % (sizeof ((h)->bitmap[0]) * NBBY); \
212                 (h)->bitmap[idx] &= ~(1 << bit);\
213         }while(0)
214  
215 #define IXGBE_GET_HWSTRIP(h, q, r) do{\
216                 uint32_t idx = (q) / (sizeof ((h)->bitmap[0]) * NBBY); \
217                 uint32_t bit = (q) % (sizeof ((h)->bitmap[0]) * NBBY); \
218                 (r) = (h)->bitmap[idx] >> bit & 1;\
219         }while(0)
220
221 /*
222  * The set of PCI devices this driver supports
223  */
224 static struct rte_pci_id pci_id_ixgbe_map[] = {
225
226 #define RTE_PCI_DEV_ID_DECL_IXGBE(vend, dev) {RTE_PCI_DEVICE(vend, dev)},
227 #include "rte_pci_dev_ids.h"
228
229 { .vendor_id = 0, /* sentinel */ },
230 };
231
232
233 /*
234  * The set of PCI devices this driver supports (for 82599 VF)
235  */
236 static struct rte_pci_id pci_id_ixgbevf_map[] = {
237
238 #define RTE_PCI_DEV_ID_DECL_IXGBEVF(vend, dev) {RTE_PCI_DEVICE(vend, dev)},
239 #include "rte_pci_dev_ids.h"
240 { .vendor_id = 0, /* sentinel */ },
241
242 };
243
244 static struct eth_dev_ops ixgbe_eth_dev_ops = {
245         .dev_configure        = ixgbe_dev_configure,
246         .dev_start            = ixgbe_dev_start,
247         .dev_stop             = ixgbe_dev_stop,
248         .dev_close            = ixgbe_dev_close,
249         .promiscuous_enable   = ixgbe_dev_promiscuous_enable,
250         .promiscuous_disable  = ixgbe_dev_promiscuous_disable,
251         .allmulticast_enable  = ixgbe_dev_allmulticast_enable,
252         .allmulticast_disable = ixgbe_dev_allmulticast_disable,
253         .link_update          = ixgbe_dev_link_update,
254         .stats_get            = ixgbe_dev_stats_get,
255         .stats_reset          = ixgbe_dev_stats_reset,
256         .queue_stats_mapping_set = ixgbe_dev_queue_stats_mapping_set,
257         .dev_infos_get        = ixgbe_dev_info_get,
258         .vlan_filter_set      = ixgbe_vlan_filter_set,
259         .vlan_tpid_set        = ixgbe_vlan_tpid_set,
260         .vlan_offload_set     = ixgbe_vlan_offload_set,
261         .vlan_strip_queue_set = ixgbe_vlan_strip_queue_set,
262         .rx_queue_setup       = ixgbe_dev_rx_queue_setup,
263         .rx_queue_release     = ixgbe_dev_rx_queue_release,
264         .rx_queue_count       = ixgbe_dev_rx_queue_count,
265         .rx_descriptor_done   = ixgbe_dev_rx_descriptor_done,
266         .tx_queue_setup       = ixgbe_dev_tx_queue_setup,
267         .tx_queue_release     = ixgbe_dev_tx_queue_release,
268         .dev_led_on           = ixgbe_dev_led_on,
269         .dev_led_off          = ixgbe_dev_led_off,
270         .flow_ctrl_set        = ixgbe_flow_ctrl_set,
271         .priority_flow_ctrl_set = ixgbe_priority_flow_ctrl_set,
272         .mac_addr_add         = ixgbe_add_rar,
273         .mac_addr_remove      = ixgbe_remove_rar,
274         .uc_hash_table_set    = ixgbe_uc_hash_table_set,
275         .uc_all_hash_table_set  = ixgbe_uc_all_hash_table_set,
276         .mirror_rule_set        = ixgbe_mirror_rule_set,
277         .mirror_rule_reset      = ixgbe_mirror_rule_reset,
278         .set_vf_rx_mode       = ixgbe_set_pool_rx_mode,
279         .set_vf_rx            = ixgbe_set_pool_rx,
280         .set_vf_tx            = ixgbe_set_pool_tx,
281         .set_vf_vlan_filter   = ixgbe_set_pool_vlan_filter,
282         .fdir_add_signature_filter    = ixgbe_fdir_add_signature_filter,
283         .fdir_update_signature_filter = ixgbe_fdir_update_signature_filter,
284         .fdir_remove_signature_filter = ixgbe_fdir_remove_signature_filter,
285         .fdir_infos_get               = ixgbe_fdir_info_get,
286         .fdir_add_perfect_filter      = ixgbe_fdir_add_perfect_filter,
287         .fdir_update_perfect_filter   = ixgbe_fdir_update_perfect_filter,
288         .fdir_remove_perfect_filter   = ixgbe_fdir_remove_perfect_filter,
289         .fdir_set_masks               = ixgbe_fdir_set_masks,
290         .reta_update          = ixgbe_dev_rss_reta_update,
291         .reta_query           = ixgbe_dev_rss_reta_query,
292 #ifdef RTE_NIC_BYPASS
293         .bypass_init          = ixgbe_bypass_init,
294         .bypass_state_set     = ixgbe_bypass_state_store,
295         .bypass_state_show    = ixgbe_bypass_state_show,
296         .bypass_event_set     = ixgbe_bypass_event_store,
297         .bypass_event_show    = ixgbe_bypass_event_show,
298         .bypass_wd_timeout_set  = ixgbe_bypass_wd_timeout_store,
299         .bypass_wd_timeout_show = ixgbe_bypass_wd_timeout_show,
300         .bypass_ver_show      = ixgbe_bypass_ver_show,
301         .bypass_wd_reset      = ixgbe_bypass_wd_reset,
302 #endif /* RTE_NIC_BYPASS */
303 };
304
305 /*
306  * dev_ops for virtual function, bare necessities for basic vf
307  * operation have been implemented
308  */
309 static struct eth_dev_ops ixgbevf_eth_dev_ops = {
310
311         .dev_configure        = ixgbevf_dev_configure,
312         .dev_start            = ixgbevf_dev_start,
313         .dev_stop             = ixgbevf_dev_stop,
314         .link_update          = ixgbe_dev_link_update,
315         .stats_get            = ixgbevf_dev_stats_get,
316         .stats_reset          = ixgbevf_dev_stats_reset,
317         .dev_close            = ixgbevf_dev_close,
318         .dev_infos_get        = ixgbe_dev_info_get,
319         .vlan_filter_set      = ixgbevf_vlan_filter_set,
320         .vlan_strip_queue_set = ixgbevf_vlan_strip_queue_set,
321         .vlan_offload_set     = ixgbevf_vlan_offload_set,
322         .rx_queue_setup       = ixgbe_dev_rx_queue_setup,
323         .rx_queue_release     = ixgbe_dev_rx_queue_release,
324         .tx_queue_setup       = ixgbe_dev_tx_queue_setup,
325         .tx_queue_release     = ixgbe_dev_tx_queue_release,
326 };
327
328 /**
329  * Atomically reads the link status information from global
330  * structure rte_eth_dev.
331  *
332  * @param dev
333  *   - Pointer to the structure rte_eth_dev to read from.
334  *   - Pointer to the buffer to be saved with the link status.
335  *
336  * @return
337  *   - On success, zero.
338  *   - On failure, negative value.
339  */
340 static inline int
341 rte_ixgbe_dev_atomic_read_link_status(struct rte_eth_dev *dev,
342                                 struct rte_eth_link *link)
343 {
344         struct rte_eth_link *dst = link;
345         struct rte_eth_link *src = &(dev->data->dev_link);
346
347         if (rte_atomic64_cmpset((uint64_t *)dst, *(uint64_t *)dst,
348                                         *(uint64_t *)src) == 0)
349                 return -1;
350
351         return 0;
352 }
353
354 /**
355  * Atomically writes the link status information into global
356  * structure rte_eth_dev.
357  *
358  * @param dev
359  *   - Pointer to the structure rte_eth_dev to read from.
360  *   - Pointer to the buffer to be saved with the link status.
361  *
362  * @return
363  *   - On success, zero.
364  *   - On failure, negative value.
365  */
366 static inline int
367 rte_ixgbe_dev_atomic_write_link_status(struct rte_eth_dev *dev,
368                                 struct rte_eth_link *link)
369 {
370         struct rte_eth_link *dst = &(dev->data->dev_link);
371         struct rte_eth_link *src = link;
372
373         if (rte_atomic64_cmpset((uint64_t *)dst, *(uint64_t *)dst,
374                                         *(uint64_t *)src) == 0)
375                 return -1;
376
377         return 0;
378 }
379
380 /*
381  * This function is the same as ixgbe_is_sfp() in ixgbe/ixgbe.h.
382  */
383 static inline int
384 ixgbe_is_sfp(struct ixgbe_hw *hw)
385 {
386         switch (hw->phy.type) {
387         case ixgbe_phy_sfp_avago:
388         case ixgbe_phy_sfp_ftl:
389         case ixgbe_phy_sfp_intel:
390         case ixgbe_phy_sfp_unknown:
391         case ixgbe_phy_sfp_passive_tyco:
392         case ixgbe_phy_sfp_passive_unknown:
393                 return 1;
394         default:
395                 return 0;
396         }
397 }
398
399 static inline int32_t
400 ixgbe_pf_reset_hw(struct ixgbe_hw *hw)
401 {
402         uint32_t ctrl_ext;
403         int32_t status;
404
405         status = ixgbe_reset_hw(hw);
406
407         ctrl_ext = IXGBE_READ_REG(hw, IXGBE_CTRL_EXT);
408         /* Set PF Reset Done bit so PF/VF Mail Ops can work */
409         ctrl_ext |= IXGBE_CTRL_EXT_PFRSTD;
410         IXGBE_WRITE_REG(hw, IXGBE_CTRL_EXT, ctrl_ext);
411         IXGBE_WRITE_FLUSH(hw);
412
413         return status;
414 }
415
416 static inline void
417 ixgbe_enable_intr(struct rte_eth_dev *dev)
418 {
419         struct ixgbe_interrupt *intr =
420                 IXGBE_DEV_PRIVATE_TO_INTR(dev->data->dev_private);
421         struct ixgbe_hw *hw = 
422                 IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
423         
424         IXGBE_WRITE_REG(hw, IXGBE_EIMS, intr->mask);
425         IXGBE_WRITE_FLUSH(hw);
426 }
427
428 /*
429  * This function is based on ixgbe_disable_intr() in ixgbe/ixgbe.h.
430  */
431 static void
432 ixgbe_disable_intr(struct ixgbe_hw *hw)
433 {
434         PMD_INIT_FUNC_TRACE();
435
436         if (hw->mac.type == ixgbe_mac_82598EB) {
437                 IXGBE_WRITE_REG(hw, IXGBE_EIMC, ~0);
438         } else {
439                 IXGBE_WRITE_REG(hw, IXGBE_EIMC, 0xFFFF0000);
440                 IXGBE_WRITE_REG(hw, IXGBE_EIMC_EX(0), ~0);
441                 IXGBE_WRITE_REG(hw, IXGBE_EIMC_EX(1), ~0);
442         }
443         IXGBE_WRITE_FLUSH(hw);
444 }
445
446 /*
447  * This function resets queue statistics mapping registers.
448  * From Niantic datasheet, Initialization of Statistics section:
449  * "...if software requires the queue counters, the RQSMR and TQSM registers
450  * must be re-programmed following a device reset.
451  */
452 static void
453 ixgbe_reset_qstat_mappings(struct ixgbe_hw *hw)
454 {
455         uint32_t i;
456
457         for(i = 0; i != IXGBE_NB_STAT_MAPPING_REGS; i++) {
458                 IXGBE_WRITE_REG(hw, IXGBE_RQSMR(i), 0);
459                 IXGBE_WRITE_REG(hw, IXGBE_TQSM(i), 0);
460         }
461 }
462
463
464 static int
465 ixgbe_dev_queue_stats_mapping_set(struct rte_eth_dev *eth_dev,
466                                   uint16_t queue_id,
467                                   uint8_t stat_idx,
468                                   uint8_t is_rx)
469 {
470 #define QSM_REG_NB_BITS_PER_QMAP_FIELD 8
471 #define NB_QMAP_FIELDS_PER_QSM_REG 4
472 #define QMAP_FIELD_RESERVED_BITS_MASK 0x0f
473
474         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(eth_dev->data->dev_private);
475         struct ixgbe_stat_mapping_registers *stat_mappings =
476                 IXGBE_DEV_PRIVATE_TO_STAT_MAPPINGS(eth_dev->data->dev_private);
477         uint32_t qsmr_mask = 0;
478         uint32_t clearing_mask = QMAP_FIELD_RESERVED_BITS_MASK;
479         uint32_t q_map;
480         uint8_t n, offset;
481
482         if ((hw->mac.type != ixgbe_mac_82599EB) && (hw->mac.type != ixgbe_mac_X540))
483                 return -ENOSYS;
484
485         PMD_INIT_LOG(INFO, "Setting port %d, %s queue_id %d to stat index %d\n",
486                      (int)(eth_dev->data->port_id), is_rx ? "RX" : "TX", queue_id, stat_idx);
487
488         n = (uint8_t)(queue_id / NB_QMAP_FIELDS_PER_QSM_REG);
489         if (n >= IXGBE_NB_STAT_MAPPING_REGS) {
490                 PMD_INIT_LOG(ERR, "Nb of stat mapping registers exceeded\n");
491                 return -EIO;
492         }
493         offset = (uint8_t)(queue_id % NB_QMAP_FIELDS_PER_QSM_REG);
494
495         /* Now clear any previous stat_idx set */
496         clearing_mask <<= (QSM_REG_NB_BITS_PER_QMAP_FIELD * offset);
497         if (!is_rx)
498                 stat_mappings->tqsm[n] &= ~clearing_mask;
499         else
500                 stat_mappings->rqsmr[n] &= ~clearing_mask;
501
502         q_map = (uint32_t)stat_idx;
503         q_map &= QMAP_FIELD_RESERVED_BITS_MASK;
504         qsmr_mask = q_map << (QSM_REG_NB_BITS_PER_QMAP_FIELD * offset);
505         if (!is_rx)
506                 stat_mappings->tqsm[n] |= qsmr_mask;
507         else
508                 stat_mappings->rqsmr[n] |= qsmr_mask;
509
510         PMD_INIT_LOG(INFO, "Set port %d, %s queue_id %d to stat index %d\n"
511                      "%s[%d] = 0x%08x\n",
512                      (int)(eth_dev->data->port_id), is_rx ? "RX" : "TX", queue_id, stat_idx,
513                      is_rx ? "RQSMR" : "TQSM",n, is_rx ? stat_mappings->rqsmr[n] : stat_mappings->tqsm[n]);
514
515         /* Now write the mapping in the appropriate register */
516         if (is_rx) {
517                 PMD_INIT_LOG(INFO, "Write 0x%x to RX IXGBE stat mapping reg:%d\n",
518                              stat_mappings->rqsmr[n], n);
519                 IXGBE_WRITE_REG(hw, IXGBE_RQSMR(n), stat_mappings->rqsmr[n]);
520         }
521         else {
522                 PMD_INIT_LOG(INFO, "Write 0x%x to TX IXGBE stat mapping reg:%d\n",
523                              stat_mappings->tqsm[n], n);
524                 IXGBE_WRITE_REG(hw, IXGBE_TQSM(n), stat_mappings->tqsm[n]);
525         }
526         return 0;
527 }
528
529 static void
530 ixgbe_restore_statistics_mapping(struct rte_eth_dev * dev)
531 {
532         struct ixgbe_stat_mapping_registers *stat_mappings =
533                 IXGBE_DEV_PRIVATE_TO_STAT_MAPPINGS(dev->data->dev_private);
534         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
535         int i;
536
537         /* write whatever was in stat mapping table to the NIC */
538         for (i = 0; i < IXGBE_NB_STAT_MAPPING_REGS; i++) {
539                 /* rx */
540                 IXGBE_WRITE_REG(hw, IXGBE_RQSMR(i), stat_mappings->rqsmr[i]);
541
542                 /* tx */
543                 IXGBE_WRITE_REG(hw, IXGBE_TQSM(i), stat_mappings->tqsm[i]);
544         }
545 }
546
547 static void
548 ixgbe_dcb_init(struct ixgbe_hw *hw,struct ixgbe_dcb_config *dcb_config)
549 {
550         uint8_t i;
551         struct ixgbe_dcb_tc_config *tc;
552         uint8_t dcb_max_tc = IXGBE_DCB_MAX_TRAFFIC_CLASS;
553
554         dcb_config->num_tcs.pg_tcs = dcb_max_tc;
555         dcb_config->num_tcs.pfc_tcs = dcb_max_tc;
556         for (i = 0; i < dcb_max_tc; i++) {
557                 tc = &dcb_config->tc_config[i];
558                 tc->path[IXGBE_DCB_TX_CONFIG].bwg_id = i;
559                 tc->path[IXGBE_DCB_TX_CONFIG].bwg_percent =
560                                  (uint8_t)(100/dcb_max_tc + (i & 1));
561                 tc->path[IXGBE_DCB_RX_CONFIG].bwg_id = i;
562                 tc->path[IXGBE_DCB_RX_CONFIG].bwg_percent = 
563                                  (uint8_t)(100/dcb_max_tc + (i & 1));
564                 tc->pfc = ixgbe_dcb_pfc_disabled;
565         }
566
567         /* Initialize default user to priority mapping, UPx->TC0 */
568         tc = &dcb_config->tc_config[0];
569         tc->path[IXGBE_DCB_TX_CONFIG].up_to_tc_bitmap = 0xFF;
570         tc->path[IXGBE_DCB_RX_CONFIG].up_to_tc_bitmap = 0xFF;
571         for (i = 0; i< IXGBE_DCB_MAX_BW_GROUP; i++) {
572                 dcb_config->bw_percentage[IXGBE_DCB_TX_CONFIG][i] = 100;
573                 dcb_config->bw_percentage[IXGBE_DCB_RX_CONFIG][i] = 100;
574         }
575         dcb_config->rx_pba_cfg = ixgbe_dcb_pba_equal;
576         dcb_config->pfc_mode_enable = false;
577         dcb_config->vt_mode = true;
578         dcb_config->round_robin_enable = false;
579         /* support all DCB capabilities in 82599 */
580         dcb_config->support.capabilities = 0xFF;
581
582         /*we only support 4 Tcs for X540*/              
583         if (hw->mac.type == ixgbe_mac_X540) {
584                 dcb_config->num_tcs.pg_tcs = 4;
585                 dcb_config->num_tcs.pfc_tcs = 4;
586         }
587
588
589 /*
590  * This function is based on code in ixgbe_attach() in ixgbe/ixgbe.c.
591  * It returns 0 on success.
592  */
593 static int
594 eth_ixgbe_dev_init(__attribute__((unused)) struct eth_driver *eth_drv,
595                      struct rte_eth_dev *eth_dev)
596 {
597         struct rte_pci_device *pci_dev;
598         struct ixgbe_hw *hw =
599                 IXGBE_DEV_PRIVATE_TO_HW(eth_dev->data->dev_private);
600         struct ixgbe_vfta * shadow_vfta =
601                 IXGBE_DEV_PRIVATE_TO_VFTA(eth_dev->data->dev_private);
602         struct ixgbe_hwstrip *hwstrip = 
603                 IXGBE_DEV_PRIVATE_TO_HWSTRIP_BITMAP(eth_dev->data->dev_private);
604         struct ixgbe_dcb_config *dcb_config =
605                 IXGBE_DEV_PRIVATE_TO_DCB_CFG(eth_dev->data->dev_private);
606         uint32_t ctrl_ext;
607         uint16_t csum;
608         int diag, i;
609
610         PMD_INIT_FUNC_TRACE();
611
612         eth_dev->dev_ops = &ixgbe_eth_dev_ops;
613         eth_dev->rx_pkt_burst = &ixgbe_recv_pkts;
614         eth_dev->tx_pkt_burst = &ixgbe_xmit_pkts;
615
616         /* for secondary processes, we don't initialise any further as primary
617          * has already done this work. Only check we don't need a different
618          * RX function */
619         if (rte_eal_process_type() != RTE_PROC_PRIMARY){
620                 if (eth_dev->data->scattered_rx)
621                         eth_dev->rx_pkt_burst = ixgbe_recv_scattered_pkts;
622                 return 0;
623         }
624         pci_dev = eth_dev->pci_dev;
625
626         /* Vendor and Device ID need to be set before init of shared code */
627         hw->device_id = pci_dev->id.device_id;
628         hw->vendor_id = pci_dev->id.vendor_id;
629         hw->hw_addr = (void *)pci_dev->mem_resource[0].addr;
630 #ifdef RTE_LIBRTE_IXGBE_ALLOW_UNSUPPORTED_SFP
631         hw->allow_unsupported_sfp = 1;
632 #endif
633
634         /* Initialize the shared code */
635 #ifdef RTE_NIC_BYPASS
636         diag = ixgbe_bypass_init_shared_code(hw);
637 #else
638         diag = ixgbe_init_shared_code(hw);
639 #endif /* RTE_NIC_BYPASS */
640
641         if (diag != IXGBE_SUCCESS) {
642                 PMD_INIT_LOG(ERR, "Shared code init failed: %d", diag);
643                 return -EIO;
644         }
645
646         /* Initialize DCB configuration*/
647         memset(dcb_config, 0, sizeof(struct ixgbe_dcb_config));
648         ixgbe_dcb_init(hw,dcb_config);
649         /* Get Hardware Flow Control setting */
650         hw->fc.requested_mode = ixgbe_fc_full;
651         hw->fc.current_mode = ixgbe_fc_full;
652         hw->fc.pause_time = IXGBE_FC_PAUSE;
653         for (i = 0; i < IXGBE_DCB_MAX_TRAFFIC_CLASS; i++) {
654                 hw->fc.low_water[i] = IXGBE_FC_LO;
655                 hw->fc.high_water[i] = IXGBE_FC_HI;
656         }
657         hw->fc.send_xon = 1;
658
659         /* Make sure we have a good EEPROM before we read from it */
660         diag = ixgbe_validate_eeprom_checksum(hw, &csum);
661         if (diag != IXGBE_SUCCESS) {
662                 PMD_INIT_LOG(ERR, "The EEPROM checksum is not valid: %d", diag);
663                 return -EIO;
664         }
665
666 #ifdef RTE_NIC_BYPASS
667         diag = ixgbe_bypass_init_hw(hw);
668 #else
669         diag = ixgbe_init_hw(hw);
670 #endif /* RTE_NIC_BYPASS */
671
672         /*
673          * Devices with copper phys will fail to initialise if ixgbe_init_hw()
674          * is called too soon after the kernel driver unbinding/binding occurs.
675          * The failure occurs in ixgbe_identify_phy_generic() for all devices,
676          * but for non-copper devies, ixgbe_identify_sfp_module_generic() is
677          * also called. See ixgbe_identify_phy_82599(). The reason for the
678          * failure is not known, and only occuts when virtualisation features
679          * are disabled in the bios. A delay of 100ms  was found to be enough by
680          * trial-and-error, and is doubled to be safe.
681          */
682         if (diag && (hw->mac.ops.get_media_type(hw) == ixgbe_media_type_copper)) {
683                 rte_delay_ms(200);
684                 diag = ixgbe_init_hw(hw);
685         }
686
687         if (diag == IXGBE_ERR_EEPROM_VERSION) {
688                 PMD_INIT_LOG(ERR, "This device is a pre-production adapter/"
689                     "LOM.  Please be aware there may be issues associated "
690                     "with your hardware.\n If you are experiencing problems "
691                     "please contact your Intel or hardware representative "
692                     "who provided you with this hardware.\n");
693         } else if (diag == IXGBE_ERR_SFP_NOT_SUPPORTED)
694                 PMD_INIT_LOG(ERR, "Unsupported SFP+ Module\n");
695         if (diag) {
696                 PMD_INIT_LOG(ERR, "Hardware Initialization Failure: %d", diag);
697                 return -EIO;
698         }
699
700         /* disable interrupt */
701         ixgbe_disable_intr(hw);
702
703         /* pick up the PCI bus settings for reporting later */
704         ixgbe_get_bus_info(hw);
705
706         /* reset mappings for queue statistics hw counters*/
707         ixgbe_reset_qstat_mappings(hw);
708
709         /* Allocate memory for storing MAC addresses */
710         eth_dev->data->mac_addrs = rte_zmalloc("ixgbe", ETHER_ADDR_LEN *
711                         hw->mac.num_rar_entries, 0);
712         if (eth_dev->data->mac_addrs == NULL) {
713                 PMD_INIT_LOG(ERR,
714                         "Failed to allocate %u bytes needed to store "
715                         "MAC addresses",
716                         ETHER_ADDR_LEN * hw->mac.num_rar_entries);
717                 return -ENOMEM;
718         }
719         /* Copy the permanent MAC address */
720         ether_addr_copy((struct ether_addr *) hw->mac.perm_addr,
721                         &eth_dev->data->mac_addrs[0]);
722         
723         /* Allocate memory for storing hash filter MAC addresses */
724         eth_dev->data->hash_mac_addrs = rte_zmalloc("ixgbe", ETHER_ADDR_LEN *
725                         IXGBE_VMDQ_NUM_UC_MAC, 0);
726         if (eth_dev->data->hash_mac_addrs == NULL) {
727                 PMD_INIT_LOG(ERR,
728                         "Failed to allocate %d bytes needed to store MAC addresses",
729                         ETHER_ADDR_LEN * IXGBE_VMDQ_NUM_UC_MAC);
730                 return -ENOMEM;
731         }
732
733         /* initialize the vfta */
734         memset(shadow_vfta, 0, sizeof(*shadow_vfta));
735
736         /* initialize the hw strip bitmap*/
737         memset(hwstrip, 0, sizeof(*hwstrip));
738
739         /* initialize PF if max_vfs not zero */
740         ixgbe_pf_host_init(eth_dev);
741
742         ctrl_ext = IXGBE_READ_REG(hw, IXGBE_CTRL_EXT);
743         /* let hardware know driver is loaded */
744         ctrl_ext |= IXGBE_CTRL_EXT_DRV_LOAD;
745         /* Set PF Reset Done bit so PF/VF Mail Ops can work */
746         ctrl_ext |= IXGBE_CTRL_EXT_PFRSTD;
747         IXGBE_WRITE_REG(hw, IXGBE_CTRL_EXT, ctrl_ext);
748         IXGBE_WRITE_FLUSH(hw);
749
750         if (ixgbe_is_sfp(hw) && hw->phy.sfp_type != ixgbe_sfp_type_not_present)
751                 PMD_INIT_LOG(DEBUG,
752                              "MAC: %d, PHY: %d, SFP+: %d<n",
753                              (int) hw->mac.type, (int) hw->phy.type,
754                              (int) hw->phy.sfp_type);
755         else
756                 PMD_INIT_LOG(DEBUG, "MAC: %d, PHY: %d\n",
757                              (int) hw->mac.type, (int) hw->phy.type);
758
759         PMD_INIT_LOG(DEBUG, "port %d vendorID=0x%x deviceID=0x%x",
760                         eth_dev->data->port_id, pci_dev->id.vendor_id,
761                         pci_dev->id.device_id);
762
763         rte_intr_callback_register(&(pci_dev->intr_handle),
764                 ixgbe_dev_interrupt_handler, (void *)eth_dev);
765
766         /* enable uio intr after callback register */
767         rte_intr_enable(&(pci_dev->intr_handle));
768
769         /* enable support intr */
770         ixgbe_enable_intr(eth_dev);
771
772         return 0;
773 }
774
775 static void ixgbevf_get_queue_num(struct ixgbe_hw *hw)
776 {
777         /* Traffic classes are not supported by now */
778         unsigned int tcs, tc;
779
780         /*
781          * Must let PF know we are at mailbox API version 1.1.
782          * Otherwise PF won't answer properly.
783          * In case that PF fails to provide Rx/Tx queue number,
784          * max_tx_queues and max_rx_queues remain to be 1.
785          */
786         if (!ixgbevf_negotiate_api_version(hw, ixgbe_mbox_api_11))
787                 ixgbevf_get_queues(hw, &tcs, &tc);
788 }
789
790 /*
791  * Virtual Function device init
792  */
793 static int
794 eth_ixgbevf_dev_init(__attribute__((unused)) struct eth_driver *eth_drv,
795                      struct rte_eth_dev *eth_dev)
796 {
797         struct rte_pci_device *pci_dev;
798         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(eth_dev->data->dev_private);
799         int diag;
800         struct ixgbe_vfta * shadow_vfta =
801                 IXGBE_DEV_PRIVATE_TO_VFTA(eth_dev->data->dev_private);
802         struct ixgbe_hwstrip *hwstrip = 
803                 IXGBE_DEV_PRIVATE_TO_HWSTRIP_BITMAP(eth_dev->data->dev_private);
804
805         PMD_INIT_LOG(DEBUG, "eth_ixgbevf_dev_init");
806
807         eth_dev->dev_ops = &ixgbevf_eth_dev_ops;
808         eth_dev->rx_pkt_burst = &ixgbe_recv_pkts;
809         eth_dev->tx_pkt_burst = &ixgbe_xmit_pkts;
810
811         /* for secondary processes, we don't initialise any further as primary
812          * has already done this work. Only check we don't need a different
813          * RX function */
814         if (rte_eal_process_type() != RTE_PROC_PRIMARY){
815                 if (eth_dev->data->scattered_rx)
816                         eth_dev->rx_pkt_burst = ixgbe_recv_scattered_pkts;
817                 return 0;
818         }
819
820         pci_dev = eth_dev->pci_dev;
821
822         hw->device_id = pci_dev->id.device_id;
823         hw->vendor_id = pci_dev->id.vendor_id;
824         hw->hw_addr = (void *)pci_dev->mem_resource[0].addr;
825
826         /* initialize the vfta */
827         memset(shadow_vfta, 0, sizeof(*shadow_vfta));
828
829         /* initialize the hw strip bitmap*/
830         memset(hwstrip, 0, sizeof(*hwstrip));
831
832         /* Initialize the shared code */
833         diag = ixgbe_init_shared_code(hw);
834         if (diag != IXGBE_SUCCESS) {
835                 PMD_INIT_LOG(ERR, "Shared code init failed for ixgbevf: %d", diag);
836                 return -EIO;
837         }
838
839         /* init_mailbox_params */
840         hw->mbx.ops.init_params(hw);
841
842         /* Disable the interrupts for VF */
843         ixgbevf_intr_disable(hw);
844
845         hw->mac.num_rar_entries = hw->mac.max_rx_queues;
846         diag = hw->mac.ops.reset_hw(hw);
847
848         if (diag != IXGBE_SUCCESS) {
849                 PMD_INIT_LOG(ERR, "VF Initialization Failure: %d", diag);
850                         RTE_LOG(ERR, PMD, "\tThe MAC address is not valid.\n"
851                                         "\tThe most likely cause of this error is that the VM host\n"
852                                         "\thas not assigned a valid MAC address to this VF device.\n"
853                                         "\tPlease consult the DPDK Release Notes (FAQ section) for\n"
854                                         "\ta possible solution to this problem.\n");
855                 return (diag);
856         }
857
858         /* Get Rx/Tx queue count via mailbox, which is ready after reset_hw */
859         ixgbevf_get_queue_num(hw);
860
861         /* Allocate memory for storing MAC addresses */
862         eth_dev->data->mac_addrs = rte_zmalloc("ixgbevf", ETHER_ADDR_LEN *
863                         hw->mac.num_rar_entries, 0);
864         if (eth_dev->data->mac_addrs == NULL) {
865                 PMD_INIT_LOG(ERR,
866                         "Failed to allocate %u bytes needed to store "
867                         "MAC addresses",
868                         ETHER_ADDR_LEN * hw->mac.num_rar_entries);
869                 return -ENOMEM;
870         }
871
872         /* Copy the permanent MAC address */
873         ether_addr_copy((struct ether_addr *) hw->mac.perm_addr,
874                         &eth_dev->data->mac_addrs[0]);
875
876         /* reset the hardware with the new settings */
877         diag = hw->mac.ops.start_hw(hw);
878         switch (diag) {
879                 case  0:
880                         break;
881
882                 default:
883                         PMD_INIT_LOG(ERR, "VF Initialization Failure: %d", diag);
884                         return (-EIO);
885         }
886
887         PMD_INIT_LOG(DEBUG, "\nport %d vendorID=0x%x deviceID=0x%x mac.type=%s\n",
888                          eth_dev->data->port_id, pci_dev->id.vendor_id, pci_dev->id.device_id,
889                          "ixgbe_mac_82599_vf");
890
891         return 0;
892 }
893
894 static struct eth_driver rte_ixgbe_pmd = {
895         {
896                 .name = "rte_ixgbe_pmd",
897                 .id_table = pci_id_ixgbe_map,
898 #ifdef RTE_EAL_UNBIND_PORTS
899                 .drv_flags = RTE_PCI_DRV_NEED_IGB_UIO,
900 #endif
901         },
902         .eth_dev_init = eth_ixgbe_dev_init,
903         .dev_private_size = sizeof(struct ixgbe_adapter),
904 };
905
906 /*
907  * virtual function driver struct
908  */
909 static struct eth_driver rte_ixgbevf_pmd = {
910         {
911                 .name = "rte_ixgbevf_pmd",
912                 .id_table = pci_id_ixgbevf_map,
913 #ifdef RTE_EAL_UNBIND_PORTS
914                 .drv_flags = RTE_PCI_DRV_NEED_IGB_UIO,
915 #endif
916         },
917         .eth_dev_init = eth_ixgbevf_dev_init,
918         .dev_private_size = sizeof(struct ixgbe_adapter),
919 };
920
921 /*
922  * Driver initialization routine.
923  * Invoked once at EAL init time.
924  * Register itself as the [Poll Mode] Driver of PCI IXGBE devices.
925  */
926 int
927 rte_ixgbe_pmd_init(void)
928 {
929         PMD_INIT_FUNC_TRACE();
930
931         rte_eth_driver_register(&rte_ixgbe_pmd);
932         return 0;
933 }
934
935 /*
936  * VF Driver initialization routine.
937  * Invoked one at EAL init time.
938  * Register itself as the [Virtual Poll Mode] Driver of PCI niantic devices.
939  */
940 int
941 rte_ixgbevf_pmd_init(void)
942 {
943         DEBUGFUNC("rte_ixgbevf_pmd_init");
944
945         rte_eth_driver_register(&rte_ixgbevf_pmd);
946         return (0);
947 }
948
949 static int
950 ixgbe_vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
951 {
952         struct ixgbe_hw *hw =
953                 IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
954         struct ixgbe_vfta * shadow_vfta =
955                 IXGBE_DEV_PRIVATE_TO_VFTA(dev->data->dev_private);
956         uint32_t vfta;
957         uint32_t vid_idx;
958         uint32_t vid_bit;
959
960         vid_idx = (uint32_t) ((vlan_id >> 5) & 0x7F);
961         vid_bit = (uint32_t) (1 << (vlan_id & 0x1F));
962         vfta = IXGBE_READ_REG(hw, IXGBE_VFTA(vid_idx));
963         if (on)
964                 vfta |= vid_bit;
965         else
966                 vfta &= ~vid_bit;
967         IXGBE_WRITE_REG(hw, IXGBE_VFTA(vid_idx), vfta);
968
969         /* update local VFTA copy */
970         shadow_vfta->vfta[vid_idx] = vfta;
971
972         return 0;
973 }
974
975 static void
976 ixgbe_vlan_strip_queue_set(struct rte_eth_dev *dev, uint16_t queue, int on)
977 {
978         if (on)
979                 ixgbe_vlan_hw_strip_enable(dev, queue);
980         else
981                 ixgbe_vlan_hw_strip_disable(dev, queue);
982 }
983
984 static void
985 ixgbe_vlan_tpid_set(struct rte_eth_dev *dev, uint16_t tpid)
986 {
987         struct ixgbe_hw *hw =
988                 IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
989
990         /* Only the high 16-bits is valid */
991         IXGBE_WRITE_REG(hw, IXGBE_EXVET, tpid << 16);
992 }
993
994 void
995 ixgbe_vlan_hw_filter_disable(struct rte_eth_dev *dev)
996 {
997         struct ixgbe_hw *hw =
998                 IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
999         uint32_t vlnctrl;
1000
1001         PMD_INIT_FUNC_TRACE();
1002
1003         /* Filter Table Disable */
1004         vlnctrl = IXGBE_READ_REG(hw, IXGBE_VLNCTRL);
1005         vlnctrl &= ~IXGBE_VLNCTRL_VFE;
1006
1007         IXGBE_WRITE_REG(hw, IXGBE_VLNCTRL, vlnctrl);
1008 }
1009
1010 void
1011 ixgbe_vlan_hw_filter_enable(struct rte_eth_dev *dev)
1012 {
1013         struct ixgbe_hw *hw =
1014                 IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1015         struct ixgbe_vfta * shadow_vfta =
1016                 IXGBE_DEV_PRIVATE_TO_VFTA(dev->data->dev_private);
1017         uint32_t vlnctrl;
1018         uint16_t i;
1019
1020         PMD_INIT_FUNC_TRACE();
1021
1022         /* Filter Table Enable */
1023         vlnctrl = IXGBE_READ_REG(hw, IXGBE_VLNCTRL);
1024         vlnctrl &= ~IXGBE_VLNCTRL_CFIEN;
1025         vlnctrl |= IXGBE_VLNCTRL_VFE;
1026
1027         IXGBE_WRITE_REG(hw, IXGBE_VLNCTRL, vlnctrl);
1028
1029         /* write whatever is in local vfta copy */
1030         for (i = 0; i < IXGBE_VFTA_SIZE; i++)
1031                 IXGBE_WRITE_REG(hw, IXGBE_VFTA(i), shadow_vfta->vfta[i]);
1032 }
1033
1034 static void 
1035 ixgbe_vlan_hw_strip_bitmap_set(struct rte_eth_dev *dev, uint16_t queue, bool on)
1036 {
1037         struct ixgbe_hwstrip *hwstrip = 
1038                 IXGBE_DEV_PRIVATE_TO_HWSTRIP_BITMAP(dev->data->dev_private);
1039
1040         if(queue >= IXGBE_MAX_RX_QUEUE_NUM)
1041                 return;
1042
1043         if (on)
1044                 IXGBE_SET_HWSTRIP(hwstrip, queue);
1045         else
1046                 IXGBE_CLEAR_HWSTRIP(hwstrip, queue);
1047 }
1048
1049 static void
1050 ixgbe_vlan_hw_strip_disable(struct rte_eth_dev *dev, uint16_t queue)
1051 {
1052         struct ixgbe_hw *hw =
1053                 IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1054         uint32_t ctrl;
1055
1056         PMD_INIT_FUNC_TRACE();
1057
1058         if (hw->mac.type == ixgbe_mac_82598EB) {
1059                 /* No queue level support */
1060                 PMD_INIT_LOG(INFO, "82598EB not support queue level hw strip");
1061                 return;
1062         }
1063         else {
1064                 /* Other 10G NIC, the VLAN strip can be setup per queue in RXDCTL */
1065                 ctrl = IXGBE_READ_REG(hw, IXGBE_RXDCTL(queue));
1066                 ctrl &= ~IXGBE_RXDCTL_VME;
1067                 IXGBE_WRITE_REG(hw, IXGBE_RXDCTL(queue), ctrl);
1068         }
1069         /* record those setting for HW strip per queue */
1070         ixgbe_vlan_hw_strip_bitmap_set(dev, queue, 0);
1071 }
1072
1073 static void
1074 ixgbe_vlan_hw_strip_enable(struct rte_eth_dev *dev, uint16_t queue)
1075 {
1076         struct ixgbe_hw *hw =
1077                 IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1078         uint32_t ctrl;
1079
1080         PMD_INIT_FUNC_TRACE();
1081
1082         if (hw->mac.type == ixgbe_mac_82598EB) {
1083                 /* No queue level supported */
1084                 PMD_INIT_LOG(INFO, "82598EB not support queue level hw strip");
1085                 return;
1086         }
1087         else {
1088                 /* Other 10G NIC, the VLAN strip can be setup per queue in RXDCTL */
1089                 ctrl = IXGBE_READ_REG(hw, IXGBE_RXDCTL(queue));
1090                 ctrl |= IXGBE_RXDCTL_VME;
1091                 IXGBE_WRITE_REG(hw, IXGBE_RXDCTL(queue), ctrl);
1092         }
1093         /* record those setting for HW strip per queue */
1094         ixgbe_vlan_hw_strip_bitmap_set(dev, queue, 1);
1095 }
1096
1097 void
1098 ixgbe_vlan_hw_strip_disable_all(struct rte_eth_dev *dev)
1099 {
1100         struct ixgbe_hw *hw =
1101                 IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1102         uint32_t ctrl;
1103         uint16_t i;
1104
1105         PMD_INIT_FUNC_TRACE();
1106
1107         if (hw->mac.type == ixgbe_mac_82598EB) {
1108                 ctrl = IXGBE_READ_REG(hw, IXGBE_VLNCTRL);
1109                 ctrl &= ~IXGBE_VLNCTRL_VME;
1110                 IXGBE_WRITE_REG(hw, IXGBE_VLNCTRL, ctrl);
1111         }
1112         else {
1113                 /* Other 10G NIC, the VLAN strip can be setup per queue in RXDCTL */
1114                 for (i = 0; i < dev->data->nb_rx_queues; i++) {
1115                         ctrl = IXGBE_READ_REG(hw, IXGBE_RXDCTL(i));
1116                         ctrl &= ~IXGBE_RXDCTL_VME;
1117                         IXGBE_WRITE_REG(hw, IXGBE_RXDCTL(i), ctrl);
1118
1119                         /* record those setting for HW strip per queue */
1120                         ixgbe_vlan_hw_strip_bitmap_set(dev, i, 0);
1121                 }
1122         }
1123 }
1124
1125 void
1126 ixgbe_vlan_hw_strip_enable_all(struct rte_eth_dev *dev)
1127 {
1128         struct ixgbe_hw *hw =
1129                 IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1130         uint32_t ctrl;
1131         uint16_t i;
1132
1133         PMD_INIT_FUNC_TRACE();
1134
1135         if (hw->mac.type == ixgbe_mac_82598EB) {
1136                 ctrl = IXGBE_READ_REG(hw, IXGBE_VLNCTRL);
1137                 ctrl |= IXGBE_VLNCTRL_VME;
1138                 IXGBE_WRITE_REG(hw, IXGBE_VLNCTRL, ctrl);
1139         }
1140         else {
1141                 /* Other 10G NIC, the VLAN strip can be setup per queue in RXDCTL */
1142                 for (i = 0; i < dev->data->nb_rx_queues; i++) {
1143                         ctrl = IXGBE_READ_REG(hw, IXGBE_RXDCTL(i));
1144                         ctrl |= IXGBE_RXDCTL_VME;
1145                         IXGBE_WRITE_REG(hw, IXGBE_RXDCTL(i), ctrl);
1146
1147                         /* record those setting for HW strip per queue */
1148                         ixgbe_vlan_hw_strip_bitmap_set(dev, i, 1);                      
1149                 }
1150         }
1151 }
1152
1153 static void
1154 ixgbe_vlan_hw_extend_disable(struct rte_eth_dev *dev)
1155 {
1156         struct ixgbe_hw *hw =
1157                 IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1158         uint32_t ctrl;
1159
1160         PMD_INIT_FUNC_TRACE();
1161
1162         /* DMATXCTRL: Geric Double VLAN Disable */
1163         ctrl = IXGBE_READ_REG(hw, IXGBE_DMATXCTL);
1164         ctrl &= ~IXGBE_DMATXCTL_GDV;
1165         IXGBE_WRITE_REG(hw, IXGBE_DMATXCTL, ctrl);
1166
1167         /* CTRL_EXT: Global Double VLAN Disable */
1168         ctrl = IXGBE_READ_REG(hw, IXGBE_CTRL_EXT);
1169         ctrl &= ~IXGBE_EXTENDED_VLAN;
1170         IXGBE_WRITE_REG(hw, IXGBE_CTRL_EXT, ctrl);
1171
1172 }
1173
1174 static void
1175 ixgbe_vlan_hw_extend_enable(struct rte_eth_dev *dev)
1176 {
1177         struct ixgbe_hw *hw =
1178                 IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1179         uint32_t ctrl;
1180
1181         PMD_INIT_FUNC_TRACE();
1182
1183         /* DMATXCTRL: Geric Double VLAN Enable */
1184         ctrl  = IXGBE_READ_REG(hw, IXGBE_DMATXCTL);
1185         ctrl |= IXGBE_DMATXCTL_GDV;
1186         IXGBE_WRITE_REG(hw, IXGBE_DMATXCTL, ctrl);
1187
1188         /* CTRL_EXT: Global Double VLAN Enable */
1189         ctrl  = IXGBE_READ_REG(hw, IXGBE_CTRL_EXT);
1190         ctrl |= IXGBE_EXTENDED_VLAN;
1191         IXGBE_WRITE_REG(hw, IXGBE_CTRL_EXT, ctrl);
1192
1193         /*
1194          * VET EXT field in the EXVET register = 0x8100 by default
1195          * So no need to change. Same to VT field of DMATXCTL register
1196          */
1197 }
1198
1199 static void
1200 ixgbe_vlan_offload_set(struct rte_eth_dev *dev, int mask)
1201 {
1202         if(mask & ETH_VLAN_STRIP_MASK){
1203                 if (dev->data->dev_conf.rxmode.hw_vlan_strip)
1204                         ixgbe_vlan_hw_strip_enable_all(dev);
1205                 else
1206                         ixgbe_vlan_hw_strip_disable_all(dev);
1207         }
1208
1209         if(mask & ETH_VLAN_FILTER_MASK){
1210                 if (dev->data->dev_conf.rxmode.hw_vlan_filter)
1211                         ixgbe_vlan_hw_filter_enable(dev);
1212                 else
1213                         ixgbe_vlan_hw_filter_disable(dev);
1214         }
1215
1216         if(mask & ETH_VLAN_EXTEND_MASK){
1217                 if (dev->data->dev_conf.rxmode.hw_vlan_extend)
1218                         ixgbe_vlan_hw_extend_enable(dev);
1219                 else
1220                         ixgbe_vlan_hw_extend_disable(dev);
1221         }
1222 }
1223
1224 static void
1225 ixgbe_vmdq_vlan_hw_filter_enable(struct rte_eth_dev *dev)
1226 {
1227         struct ixgbe_hw *hw =
1228                 IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1229         /* VLNCTRL: enable vlan filtering and allow all vlan tags through */
1230         uint32_t vlanctrl = IXGBE_READ_REG(hw, IXGBE_VLNCTRL);
1231         vlanctrl |= IXGBE_VLNCTRL_VFE ; /* enable vlan filters */
1232         IXGBE_WRITE_REG(hw, IXGBE_VLNCTRL, vlanctrl);
1233 }
1234
1235 static int
1236 ixgbe_dev_configure(struct rte_eth_dev *dev)
1237 {
1238         struct ixgbe_interrupt *intr =
1239                 IXGBE_DEV_PRIVATE_TO_INTR(dev->data->dev_private);
1240
1241         PMD_INIT_FUNC_TRACE();
1242
1243         /* set flag to update link status after init */
1244         intr->flags |= IXGBE_FLAG_NEED_LINK_UPDATE;
1245
1246         return 0;
1247 }
1248
1249 /*
1250  * Configure device link speed and setup link.
1251  * It returns 0 on success.
1252  */
1253 static int
1254 ixgbe_dev_start(struct rte_eth_dev *dev)
1255 {
1256         struct ixgbe_hw *hw =
1257                 IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1258         int err, link_up = 0, negotiate = 0;
1259         uint32_t speed = 0;
1260         int mask = 0;
1261         int status;
1262         
1263         PMD_INIT_FUNC_TRACE();
1264
1265         /* IXGBE devices don't support half duplex */
1266         if ((dev->data->dev_conf.link_duplex != ETH_LINK_AUTONEG_DUPLEX) &&
1267                         (dev->data->dev_conf.link_duplex != ETH_LINK_FULL_DUPLEX)) {
1268                 PMD_INIT_LOG(ERR, "Invalid link_duplex (%hu) for port %hhu\n",
1269                                 dev->data->dev_conf.link_duplex,
1270                                 dev->data->port_id);
1271                 return -EINVAL;
1272         }
1273
1274         /* stop adapter */
1275         hw->adapter_stopped = FALSE;
1276         ixgbe_stop_adapter(hw);
1277
1278         /* reinitialize adapter
1279          * this calls reset and start */
1280         status = ixgbe_pf_reset_hw(hw);
1281         if (status != 0)
1282                 return -1;
1283         hw->mac.ops.start_hw(hw);
1284
1285         /* configure PF module if SRIOV enabled */
1286         ixgbe_pf_host_configure(dev);
1287
1288         /* initialize transmission unit */
1289         ixgbe_dev_tx_init(dev);
1290       
1291         /* This can fail when allocating mbufs for descriptor rings */
1292         err = ixgbe_dev_rx_init(dev);
1293         if (err) {
1294                 PMD_INIT_LOG(ERR, "Unable to initialize RX hardware\n");
1295                 goto error;
1296         }
1297
1298         ixgbe_dev_rxtx_start(dev);
1299
1300         if (ixgbe_is_sfp(hw) && hw->phy.multispeed_fiber) {
1301                 err = hw->mac.ops.setup_sfp(hw);
1302                 if (err)
1303                         goto error;
1304         }
1305
1306         /* Turn on the laser */
1307         ixgbe_enable_tx_laser(hw);
1308
1309         /* Skip link setup if loopback mode is enabled for 82599. */
1310         if (hw->mac.type == ixgbe_mac_82599EB &&
1311                         dev->data->dev_conf.lpbk_mode == IXGBE_LPBK_82599_TX_RX)
1312                 goto skip_link_setup;
1313
1314         err = ixgbe_check_link(hw, &speed, &link_up, 0);
1315         if (err)
1316                 goto error;
1317         err = ixgbe_get_link_capabilities(hw, &speed, &negotiate);
1318         if (err)
1319                 goto error;
1320
1321         switch(dev->data->dev_conf.link_speed) {
1322         case ETH_LINK_SPEED_AUTONEG:
1323                 speed = (hw->mac.type != ixgbe_mac_82598EB) ?
1324                                 IXGBE_LINK_SPEED_82599_AUTONEG :
1325                                 IXGBE_LINK_SPEED_82598_AUTONEG;
1326                 break;
1327         case ETH_LINK_SPEED_100:
1328                 /*
1329                  * Invalid for 82598 but error will be detected by
1330                  * ixgbe_setup_link()
1331                  */
1332                 speed = IXGBE_LINK_SPEED_100_FULL;
1333                 break;
1334         case ETH_LINK_SPEED_1000:
1335                 speed = IXGBE_LINK_SPEED_1GB_FULL;
1336                 break;
1337         case ETH_LINK_SPEED_10000:
1338                 speed = IXGBE_LINK_SPEED_10GB_FULL;
1339                 break;
1340         default:
1341                 PMD_INIT_LOG(ERR, "Invalid link_speed (%hu) for port %hhu\n",
1342                                 dev->data->dev_conf.link_speed,
1343                                 dev->data->port_id);
1344                 goto error;
1345         }
1346
1347         err = ixgbe_setup_link(hw, speed, negotiate, link_up);
1348         if (err)
1349                 goto error;
1350
1351 skip_link_setup:
1352
1353         /* check if lsc interrupt is enabled */
1354         if (dev->data->dev_conf.intr_conf.lsc != 0)
1355                 ixgbe_dev_lsc_interrupt_setup(dev);
1356
1357         /* resume enabled intr since hw reset */
1358         ixgbe_enable_intr(dev);
1359
1360         mask = ETH_VLAN_STRIP_MASK | ETH_VLAN_FILTER_MASK | \
1361                 ETH_VLAN_EXTEND_MASK;
1362         ixgbe_vlan_offload_set(dev, mask);
1363
1364         if (dev->data->dev_conf.rxmode.mq_mode == ETH_MQ_RX_VMDQ_ONLY) {
1365                 /* Enable vlan filtering for VMDq */
1366                 ixgbe_vmdq_vlan_hw_filter_enable(dev);
1367         }       
1368
1369         /* Configure DCB hw */
1370         ixgbe_configure_dcb(dev); 
1371
1372         if (dev->data->dev_conf.fdir_conf.mode != RTE_FDIR_MODE_NONE) {
1373                 err = ixgbe_fdir_configure(dev);
1374                 if (err)
1375                         goto error;
1376         }
1377
1378         ixgbe_restore_statistics_mapping(dev);
1379
1380         return (0);
1381
1382 error:
1383         PMD_INIT_LOG(ERR, "failure in ixgbe_dev_start(): %d", err);
1384         ixgbe_dev_clear_queues(dev);
1385         return -EIO;
1386 }
1387
1388 /*
1389  * Stop device: disable rx and tx functions to allow for reconfiguring.
1390  */
1391 static void
1392 ixgbe_dev_stop(struct rte_eth_dev *dev)
1393 {
1394         struct rte_eth_link link;
1395         struct ixgbe_hw *hw =
1396                 IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1397         struct ixgbe_vf_info *vfinfo = 
1398                 *IXGBE_DEV_PRIVATE_TO_P_VFDATA(dev->data->dev_private);
1399         int vf;
1400
1401         PMD_INIT_FUNC_TRACE();
1402
1403         /* disable interrupts */
1404         ixgbe_disable_intr(hw);
1405
1406         /* reset the NIC */
1407         ixgbe_pf_reset_hw(hw);
1408         hw->adapter_stopped = FALSE;
1409
1410         /* stop adapter */
1411         ixgbe_stop_adapter(hw);
1412
1413         for (vf = 0; vfinfo != NULL && 
1414                      vf < dev->pci_dev->max_vfs; vf++)
1415                 vfinfo[vf].clear_to_send = false;
1416
1417         /* Turn off the laser */
1418         ixgbe_disable_tx_laser(hw);
1419
1420         ixgbe_dev_clear_queues(dev);
1421
1422         /* Clear recorded link status */
1423         memset(&link, 0, sizeof(link));
1424         rte_ixgbe_dev_atomic_write_link_status(dev, &link);
1425 }
1426
1427 /*
1428  * Reest and stop device.
1429  */
1430 static void
1431 ixgbe_dev_close(struct rte_eth_dev *dev)
1432 {
1433         struct ixgbe_hw *hw =
1434                 IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1435
1436         PMD_INIT_FUNC_TRACE();
1437
1438         ixgbe_pf_reset_hw(hw);
1439
1440         ixgbe_dev_stop(dev);
1441         hw->adapter_stopped = 1;
1442
1443         ixgbe_disable_pcie_master(hw);
1444
1445         /* reprogram the RAR[0] in case user changed it. */
1446         ixgbe_set_rar(hw, 0, hw->mac.addr, 0, IXGBE_RAH_AV);
1447 }
1448
1449 /*
1450  * This function is based on ixgbe_update_stats_counters() in ixgbe/ixgbe.c
1451  */
1452 static void
1453 ixgbe_dev_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
1454 {
1455         struct ixgbe_hw *hw =
1456                         IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1457         struct ixgbe_hw_stats *hw_stats =
1458                         IXGBE_DEV_PRIVATE_TO_STATS(dev->data->dev_private);
1459         uint32_t bprc, lxon, lxoff, total;
1460         uint64_t total_missed_rx, total_qbrc, total_qprc;
1461         unsigned i;
1462
1463         total_missed_rx = 0;
1464         total_qbrc = 0;
1465         total_qprc = 0;
1466
1467         hw_stats->crcerrs += IXGBE_READ_REG(hw, IXGBE_CRCERRS);
1468         hw_stats->illerrc += IXGBE_READ_REG(hw, IXGBE_ILLERRC);
1469         hw_stats->errbc += IXGBE_READ_REG(hw, IXGBE_ERRBC);
1470         hw_stats->mspdc += IXGBE_READ_REG(hw, IXGBE_MSPDC);
1471
1472         for (i = 0; i < 8; i++) {
1473                 uint32_t mp;
1474                 mp = IXGBE_READ_REG(hw, IXGBE_MPC(i));
1475                 /* global total per queue */
1476                 hw_stats->mpc[i] += mp;
1477                 /* Running comprehensive total for stats display */
1478                 total_missed_rx += hw_stats->mpc[i];
1479                 if (hw->mac.type == ixgbe_mac_82598EB)
1480                         hw_stats->rnbc[i] +=
1481                             IXGBE_READ_REG(hw, IXGBE_RNBC(i));
1482                 hw_stats->pxontxc[i] +=
1483                     IXGBE_READ_REG(hw, IXGBE_PXONTXC(i));
1484                 hw_stats->pxonrxc[i] +=
1485                     IXGBE_READ_REG(hw, IXGBE_PXONRXC(i));
1486                 hw_stats->pxofftxc[i] +=
1487                     IXGBE_READ_REG(hw, IXGBE_PXOFFTXC(i));
1488                 hw_stats->pxoffrxc[i] +=
1489                     IXGBE_READ_REG(hw, IXGBE_PXOFFRXC(i));
1490                 hw_stats->pxon2offc[i] +=
1491                     IXGBE_READ_REG(hw, IXGBE_PXON2OFFCNT(i));
1492         }
1493         for (i = 0; i < IXGBE_QUEUE_STAT_COUNTERS; i++) {
1494                 hw_stats->qprc[i] += IXGBE_READ_REG(hw, IXGBE_QPRC(i));
1495                 hw_stats->qptc[i] += IXGBE_READ_REG(hw, IXGBE_QPTC(i));
1496                 hw_stats->qbrc[i] += IXGBE_READ_REG(hw, IXGBE_QBRC_L(i));
1497                 hw_stats->qbrc[i] +=
1498                     ((uint64_t)IXGBE_READ_REG(hw, IXGBE_QBRC_H(i)) << 32);
1499                 hw_stats->qbtc[i] += IXGBE_READ_REG(hw, IXGBE_QBTC_L(i));
1500                 hw_stats->qbtc[i] +=
1501                     ((uint64_t)IXGBE_READ_REG(hw, IXGBE_QBTC_H(i)) << 32);
1502                 hw_stats->qprdc[i] += IXGBE_READ_REG(hw, IXGBE_QPRDC(i));
1503
1504                 total_qprc += hw_stats->qprc[i];
1505                 total_qbrc += hw_stats->qbrc[i];
1506         }
1507         hw_stats->mlfc += IXGBE_READ_REG(hw, IXGBE_MLFC);
1508         hw_stats->mrfc += IXGBE_READ_REG(hw, IXGBE_MRFC);
1509         hw_stats->rlec += IXGBE_READ_REG(hw, IXGBE_RLEC);
1510
1511         /* Note that gprc counts missed packets */
1512         hw_stats->gprc += IXGBE_READ_REG(hw, IXGBE_GPRC);
1513
1514         if (hw->mac.type != ixgbe_mac_82598EB) {
1515                 hw_stats->gorc += IXGBE_READ_REG(hw, IXGBE_GORCL);
1516                 hw_stats->gorc += ((u64)IXGBE_READ_REG(hw, IXGBE_GORCH) << 32);
1517                 hw_stats->gotc += IXGBE_READ_REG(hw, IXGBE_GOTCL);
1518                 hw_stats->gotc += ((u64)IXGBE_READ_REG(hw, IXGBE_GOTCH) << 32);
1519                 hw_stats->tor += IXGBE_READ_REG(hw, IXGBE_TORL);
1520                 hw_stats->tor += ((u64)IXGBE_READ_REG(hw, IXGBE_TORH) << 32);
1521                 hw_stats->lxonrxc += IXGBE_READ_REG(hw, IXGBE_LXONRXCNT);
1522                 hw_stats->lxoffrxc += IXGBE_READ_REG(hw, IXGBE_LXOFFRXCNT);
1523         } else {
1524                 hw_stats->lxonrxc += IXGBE_READ_REG(hw, IXGBE_LXONRXC);
1525                 hw_stats->lxoffrxc += IXGBE_READ_REG(hw, IXGBE_LXOFFRXC);
1526                 /* 82598 only has a counter in the high register */
1527                 hw_stats->gorc += IXGBE_READ_REG(hw, IXGBE_GORCH);
1528                 hw_stats->gotc += IXGBE_READ_REG(hw, IXGBE_GOTCH);
1529                 hw_stats->tor += IXGBE_READ_REG(hw, IXGBE_TORH);
1530         }
1531
1532         /*
1533          * Workaround: mprc hardware is incorrectly counting
1534          * broadcasts, so for now we subtract those.
1535          */
1536         bprc = IXGBE_READ_REG(hw, IXGBE_BPRC);
1537         hw_stats->bprc += bprc;
1538         hw_stats->mprc += IXGBE_READ_REG(hw, IXGBE_MPRC);
1539         if (hw->mac.type == ixgbe_mac_82598EB)
1540                 hw_stats->mprc -= bprc;
1541
1542         hw_stats->prc64 += IXGBE_READ_REG(hw, IXGBE_PRC64);
1543         hw_stats->prc127 += IXGBE_READ_REG(hw, IXGBE_PRC127);
1544         hw_stats->prc255 += IXGBE_READ_REG(hw, IXGBE_PRC255);
1545         hw_stats->prc511 += IXGBE_READ_REG(hw, IXGBE_PRC511);
1546         hw_stats->prc1023 += IXGBE_READ_REG(hw, IXGBE_PRC1023);
1547         hw_stats->prc1522 += IXGBE_READ_REG(hw, IXGBE_PRC1522);
1548
1549         lxon = IXGBE_READ_REG(hw, IXGBE_LXONTXC);
1550         hw_stats->lxontxc += lxon;
1551         lxoff = IXGBE_READ_REG(hw, IXGBE_LXOFFTXC);
1552         hw_stats->lxofftxc += lxoff;
1553         total = lxon + lxoff;
1554
1555         hw_stats->gptc += IXGBE_READ_REG(hw, IXGBE_GPTC);
1556         hw_stats->mptc += IXGBE_READ_REG(hw, IXGBE_MPTC);
1557         hw_stats->ptc64 += IXGBE_READ_REG(hw, IXGBE_PTC64);
1558         hw_stats->gptc -= total;
1559         hw_stats->mptc -= total;
1560         hw_stats->ptc64 -= total;
1561         hw_stats->gotc -= total * ETHER_MIN_LEN;
1562
1563         hw_stats->ruc += IXGBE_READ_REG(hw, IXGBE_RUC);
1564         hw_stats->rfc += IXGBE_READ_REG(hw, IXGBE_RFC);
1565         hw_stats->roc += IXGBE_READ_REG(hw, IXGBE_ROC);
1566         hw_stats->rjc += IXGBE_READ_REG(hw, IXGBE_RJC);
1567         hw_stats->mngprc += IXGBE_READ_REG(hw, IXGBE_MNGPRC);
1568         hw_stats->mngpdc += IXGBE_READ_REG(hw, IXGBE_MNGPDC);
1569         hw_stats->mngptc += IXGBE_READ_REG(hw, IXGBE_MNGPTC);
1570         hw_stats->tpr += IXGBE_READ_REG(hw, IXGBE_TPR);
1571         hw_stats->tpt += IXGBE_READ_REG(hw, IXGBE_TPT);
1572         hw_stats->ptc127 += IXGBE_READ_REG(hw, IXGBE_PTC127);
1573         hw_stats->ptc255 += IXGBE_READ_REG(hw, IXGBE_PTC255);
1574         hw_stats->ptc511 += IXGBE_READ_REG(hw, IXGBE_PTC511);
1575         hw_stats->ptc1023 += IXGBE_READ_REG(hw, IXGBE_PTC1023);
1576         hw_stats->ptc1522 += IXGBE_READ_REG(hw, IXGBE_PTC1522);
1577         hw_stats->bptc += IXGBE_READ_REG(hw, IXGBE_BPTC);
1578         hw_stats->xec += IXGBE_READ_REG(hw, IXGBE_XEC);
1579         hw_stats->fccrc += IXGBE_READ_REG(hw, IXGBE_FCCRC);
1580         hw_stats->fclast += IXGBE_READ_REG(hw, IXGBE_FCLAST);
1581         /* Only read FCOE on 82599 */
1582         if (hw->mac.type != ixgbe_mac_82598EB) {
1583                 hw_stats->fcoerpdc += IXGBE_READ_REG(hw, IXGBE_FCOERPDC);
1584                 hw_stats->fcoeprc += IXGBE_READ_REG(hw, IXGBE_FCOEPRC);
1585                 hw_stats->fcoeptc += IXGBE_READ_REG(hw, IXGBE_FCOEPTC);
1586                 hw_stats->fcoedwrc += IXGBE_READ_REG(hw, IXGBE_FCOEDWRC);
1587                 hw_stats->fcoedwtc += IXGBE_READ_REG(hw, IXGBE_FCOEDWTC);
1588         }
1589
1590         if (stats == NULL)
1591                 return;
1592
1593         /* Fill out the rte_eth_stats statistics structure */
1594         stats->ipackets = total_qprc;
1595         stats->ibytes = total_qbrc;
1596         stats->opackets = hw_stats->gptc;
1597         stats->obytes = hw_stats->gotc;
1598         stats->imcasts = hw_stats->mprc;
1599
1600         for (i = 0; i < IXGBE_QUEUE_STAT_COUNTERS; i++) {
1601                 stats->q_ipackets[i] = hw_stats->qprc[i];
1602                 stats->q_opackets[i] = hw_stats->qptc[i];
1603                 stats->q_ibytes[i] = hw_stats->qbrc[i];
1604                 stats->q_obytes[i] = hw_stats->qbtc[i];
1605                 stats->q_errors[i] = hw_stats->qprdc[i];
1606         }
1607
1608         /* Rx Errors */
1609         stats->ierrors = total_missed_rx + hw_stats->crcerrs +
1610                 hw_stats->rlec;
1611
1612         stats->oerrors  = 0;
1613
1614         /* Flow Director Stats registers */
1615         hw_stats->fdirmatch += IXGBE_READ_REG(hw, IXGBE_FDIRMATCH);
1616         hw_stats->fdirmiss += IXGBE_READ_REG(hw, IXGBE_FDIRMISS);
1617         stats->fdirmatch = hw_stats->fdirmatch;
1618         stats->fdirmiss = hw_stats->fdirmiss;
1619 }
1620
1621 static void
1622 ixgbe_dev_stats_reset(struct rte_eth_dev *dev)
1623 {
1624         struct ixgbe_hw_stats *stats =
1625                         IXGBE_DEV_PRIVATE_TO_STATS(dev->data->dev_private);
1626
1627         /* HW registers are cleared on read */
1628         ixgbe_dev_stats_get(dev, NULL);
1629
1630         /* Reset software totals */
1631         memset(stats, 0, sizeof(*stats));
1632 }
1633
1634 static void
1635 ixgbevf_dev_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
1636 {
1637         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1638         struct ixgbevf_hw_stats *hw_stats = (struct ixgbevf_hw_stats*)
1639                           IXGBE_DEV_PRIVATE_TO_STATS(dev->data->dev_private);
1640
1641         /* Good Rx packet, include VF loopback */
1642         UPDATE_VF_STAT(IXGBE_VFGPRC,
1643             hw_stats->last_vfgprc, hw_stats->vfgprc);
1644
1645         /* Good Rx octets, include VF loopback */
1646         UPDATE_VF_STAT_36BIT(IXGBE_VFGORC_LSB, IXGBE_VFGORC_MSB,
1647             hw_stats->last_vfgorc, hw_stats->vfgorc);
1648
1649         /* Good Tx packet, include VF loopback */
1650         UPDATE_VF_STAT(IXGBE_VFGPTC,
1651             hw_stats->last_vfgptc, hw_stats->vfgptc);
1652
1653         /* Good Tx octets, include VF loopback */
1654         UPDATE_VF_STAT_36BIT(IXGBE_VFGOTC_LSB, IXGBE_VFGOTC_MSB,
1655             hw_stats->last_vfgotc, hw_stats->vfgotc);
1656
1657         /* Rx Multicst Packet */
1658         UPDATE_VF_STAT(IXGBE_VFMPRC,
1659             hw_stats->last_vfmprc, hw_stats->vfmprc);
1660
1661         if (stats == NULL)
1662                 return;
1663
1664         memset(stats, 0, sizeof(*stats));
1665         stats->ipackets = hw_stats->vfgprc;
1666         stats->ibytes = hw_stats->vfgorc;
1667         stats->opackets = hw_stats->vfgptc;
1668         stats->obytes = hw_stats->vfgotc;
1669         stats->imcasts = hw_stats->vfmprc;
1670 }
1671
1672 static void
1673 ixgbevf_dev_stats_reset(struct rte_eth_dev *dev)
1674 {
1675         struct ixgbevf_hw_stats *hw_stats = (struct ixgbevf_hw_stats*)
1676                         IXGBE_DEV_PRIVATE_TO_STATS(dev->data->dev_private);
1677
1678         /* Sync HW register to the last stats */
1679         ixgbevf_dev_stats_get(dev, NULL);
1680
1681         /* reset HW current stats*/
1682         hw_stats->vfgprc = 0;
1683         hw_stats->vfgorc = 0;
1684         hw_stats->vfgptc = 0;
1685         hw_stats->vfgotc = 0;
1686         hw_stats->vfmprc = 0;
1687
1688 }
1689
1690 static void
1691 ixgbe_dev_info_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *dev_info)
1692 {
1693         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1694
1695         dev_info->max_rx_queues = (uint16_t)hw->mac.max_rx_queues;
1696         dev_info->max_tx_queues = (uint16_t)hw->mac.max_tx_queues;
1697         dev_info->min_rx_bufsize = 1024; /* cf BSIZEPACKET in SRRCTL register */
1698         dev_info->max_rx_pktlen = 15872; /* includes CRC, cf MAXFRS register */
1699         dev_info->max_mac_addrs = hw->mac.num_rar_entries;
1700         dev_info->max_hash_mac_addrs = IXGBE_VMDQ_NUM_UC_MAC;
1701         dev_info->max_vfs = dev->pci_dev->max_vfs;
1702         if (hw->mac.type == ixgbe_mac_82598EB)
1703                 dev_info->max_vmdq_pools = ETH_16_POOLS;
1704         else
1705                 dev_info->max_vmdq_pools = ETH_64_POOLS;
1706 }
1707
1708 /* return 0 means link status changed, -1 means not changed */
1709 static int
1710 ixgbe_dev_link_update(struct rte_eth_dev *dev, int wait_to_complete)
1711 {
1712         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1713         struct rte_eth_link link, old;
1714         ixgbe_link_speed link_speed;
1715         int link_up;
1716         int diag;
1717
1718         link.link_status = 0;
1719         link.link_speed = 0;
1720         link.link_duplex = 0;
1721         memset(&old, 0, sizeof(old));
1722         rte_ixgbe_dev_atomic_read_link_status(dev, &old);
1723
1724         /* check if it needs to wait to complete, if lsc interrupt is enabled */
1725         if (wait_to_complete == 0 || dev->data->dev_conf.intr_conf.lsc != 0)
1726                 diag = ixgbe_check_link(hw, &link_speed, &link_up, 0);
1727         else
1728                 diag = ixgbe_check_link(hw, &link_speed, &link_up, 1);
1729         if (diag != 0) {
1730                 link.link_speed = ETH_LINK_SPEED_100;
1731                 link.link_duplex = ETH_LINK_HALF_DUPLEX;
1732                 rte_ixgbe_dev_atomic_write_link_status(dev, &link);
1733                 if (link.link_status == old.link_status)
1734                         return -1;
1735                 return 0;
1736         }
1737
1738         if (link_up == 0) {
1739                 rte_ixgbe_dev_atomic_write_link_status(dev, &link);
1740                 if (link.link_status == old.link_status)
1741                         return -1;
1742                 return 0;
1743         }
1744         link.link_status = 1;
1745         link.link_duplex = ETH_LINK_FULL_DUPLEX;
1746
1747         switch (link_speed) {
1748         default:
1749         case IXGBE_LINK_SPEED_UNKNOWN:
1750                 link.link_duplex = ETH_LINK_HALF_DUPLEX;
1751                 link.link_speed = ETH_LINK_SPEED_100;
1752                 break;
1753
1754         case IXGBE_LINK_SPEED_100_FULL:
1755                 link.link_speed = ETH_LINK_SPEED_100;
1756                 break;
1757
1758         case IXGBE_LINK_SPEED_1GB_FULL:
1759                 link.link_speed = ETH_LINK_SPEED_1000;
1760                 break;
1761
1762         case IXGBE_LINK_SPEED_10GB_FULL:
1763                 link.link_speed = ETH_LINK_SPEED_10000;
1764                 break;
1765         }
1766         rte_ixgbe_dev_atomic_write_link_status(dev, &link);
1767
1768         if (link.link_status == old.link_status)
1769                 return -1;
1770
1771         return 0;
1772 }
1773
1774 static void
1775 ixgbe_dev_promiscuous_enable(struct rte_eth_dev *dev)
1776 {
1777         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1778         uint32_t fctrl;
1779
1780         fctrl = IXGBE_READ_REG(hw, IXGBE_FCTRL);
1781         fctrl |= (IXGBE_FCTRL_UPE | IXGBE_FCTRL_MPE);
1782         IXGBE_WRITE_REG(hw, IXGBE_FCTRL, fctrl);
1783 }
1784
1785 static void
1786 ixgbe_dev_promiscuous_disable(struct rte_eth_dev *dev)
1787 {
1788         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1789         uint32_t fctrl;
1790
1791         fctrl = IXGBE_READ_REG(hw, IXGBE_FCTRL);
1792         fctrl &= (~IXGBE_FCTRL_UPE);
1793         if (dev->data->all_multicast == 1)
1794                 fctrl |= IXGBE_FCTRL_MPE;
1795         else
1796                 fctrl &= (~IXGBE_FCTRL_MPE);
1797         IXGBE_WRITE_REG(hw, IXGBE_FCTRL, fctrl);
1798 }
1799
1800 static void
1801 ixgbe_dev_allmulticast_enable(struct rte_eth_dev *dev)
1802 {
1803         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1804         uint32_t fctrl;
1805
1806         fctrl = IXGBE_READ_REG(hw, IXGBE_FCTRL);
1807         fctrl |= IXGBE_FCTRL_MPE;
1808         IXGBE_WRITE_REG(hw, IXGBE_FCTRL, fctrl);
1809 }
1810
1811 static void
1812 ixgbe_dev_allmulticast_disable(struct rte_eth_dev *dev)
1813 {
1814         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1815         uint32_t fctrl;
1816
1817         if (dev->data->promiscuous == 1)
1818                 return; /* must remain in all_multicast mode */
1819
1820         fctrl = IXGBE_READ_REG(hw, IXGBE_FCTRL);
1821         fctrl &= (~IXGBE_FCTRL_MPE);
1822         IXGBE_WRITE_REG(hw, IXGBE_FCTRL, fctrl);
1823 }
1824
1825 /**
1826  * It clears the interrupt causes and enables the interrupt.
1827  * It will be called once only during nic initialized.
1828  *
1829  * @param dev
1830  *  Pointer to struct rte_eth_dev.
1831  *
1832  * @return
1833  *  - On success, zero.
1834  *  - On failure, a negative value.
1835  */
1836 static int
1837 ixgbe_dev_lsc_interrupt_setup(struct rte_eth_dev *dev)
1838 {
1839         struct ixgbe_interrupt *intr =
1840                 IXGBE_DEV_PRIVATE_TO_INTR(dev->data->dev_private);
1841
1842         ixgbe_dev_link_status_print(dev);
1843         intr->mask |= IXGBE_EICR_LSC;
1844
1845         return 0;
1846 }
1847
1848 /*
1849  * It reads ICR and sets flag (IXGBE_EICR_LSC) for the link_update.
1850  *
1851  * @param dev
1852  *  Pointer to struct rte_eth_dev.
1853  *
1854  * @return
1855  *  - On success, zero.
1856  *  - On failure, a negative value.
1857  */
1858 static int
1859 ixgbe_dev_interrupt_get_status(struct rte_eth_dev *dev)
1860 {
1861         uint32_t eicr;
1862         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1863         struct ixgbe_interrupt *intr =
1864                 IXGBE_DEV_PRIVATE_TO_INTR(dev->data->dev_private);
1865
1866         /* clear all cause mask */
1867         ixgbe_disable_intr(hw);
1868
1869         /* read-on-clear nic registers here */
1870         eicr = IXGBE_READ_REG(hw, IXGBE_EICR);
1871         PMD_DRV_LOG(INFO, "eicr %x", eicr);
1872         
1873         intr->flags = 0;
1874         if (eicr & IXGBE_EICR_LSC) {
1875                 /* set flag for async link update */
1876                 intr->flags |= IXGBE_FLAG_NEED_LINK_UPDATE;
1877         }
1878
1879         if (eicr & IXGBE_EICR_MAILBOX)
1880                 intr->flags |= IXGBE_FLAG_MAILBOX;
1881
1882         return 0;
1883 }
1884
1885 /**
1886  * It gets and then prints the link status.
1887  *
1888  * @param dev
1889  *  Pointer to struct rte_eth_dev.
1890  *
1891  * @return
1892  *  - On success, zero.
1893  *  - On failure, a negative value.
1894  */
1895 static void
1896 ixgbe_dev_link_status_print(struct rte_eth_dev *dev)
1897 {
1898         struct rte_eth_link link;
1899
1900         memset(&link, 0, sizeof(link));
1901         rte_ixgbe_dev_atomic_read_link_status(dev, &link);
1902         if (link.link_status) {
1903                 PMD_INIT_LOG(INFO, "Port %d: Link Up - speed %u Mbps - %s",
1904                                         (int)(dev->data->port_id),
1905                                         (unsigned)link.link_speed,
1906                         link.link_duplex == ETH_LINK_FULL_DUPLEX ?
1907                                         "full-duplex" : "half-duplex");
1908         } else {
1909                 PMD_INIT_LOG(INFO, " Port %d: Link Down",
1910                                 (int)(dev->data->port_id));
1911         }
1912         PMD_INIT_LOG(INFO, "PCI Address: %04d:%02d:%02d:%d",
1913                                 dev->pci_dev->addr.domain,
1914                                 dev->pci_dev->addr.bus,
1915                                 dev->pci_dev->addr.devid,
1916                                 dev->pci_dev->addr.function);
1917 }
1918
1919 /*
1920  * It executes link_update after knowing an interrupt occured.
1921  *
1922  * @param dev
1923  *  Pointer to struct rte_eth_dev.
1924  *
1925  * @return
1926  *  - On success, zero.
1927  *  - On failure, a negative value.
1928  */
1929 static int
1930 ixgbe_dev_interrupt_action(struct rte_eth_dev *dev)
1931 {
1932         struct ixgbe_interrupt *intr =
1933                 IXGBE_DEV_PRIVATE_TO_INTR(dev->data->dev_private);
1934         int64_t timeout;
1935         struct rte_eth_link link;
1936         int intr_enable_delay = false;  
1937
1938         PMD_DRV_LOG(DEBUG, "intr action type %d\n", intr->flags);
1939
1940         if (intr->flags & IXGBE_FLAG_MAILBOX) {
1941                 ixgbe_pf_mbx_process(dev);
1942                 intr->flags &= ~IXGBE_FLAG_MAILBOX;
1943         } 
1944
1945         if (intr->flags & IXGBE_FLAG_NEED_LINK_UPDATE) {
1946                 /* get the link status before link update, for predicting later */
1947                 memset(&link, 0, sizeof(link));
1948                 rte_ixgbe_dev_atomic_read_link_status(dev, &link);
1949
1950                 ixgbe_dev_link_update(dev, 0);
1951
1952                 /* likely to up */
1953                 if (!link.link_status)
1954                         /* handle it 1 sec later, wait it being stable */
1955                         timeout = IXGBE_LINK_UP_CHECK_TIMEOUT;
1956                 /* likely to down */
1957                 else
1958                         /* handle it 4 sec later, wait it being stable */
1959                         timeout = IXGBE_LINK_DOWN_CHECK_TIMEOUT;
1960                 
1961                 ixgbe_dev_link_status_print(dev);
1962
1963                 intr_enable_delay = true;
1964         } 
1965
1966         if (intr_enable_delay) {
1967                 if (rte_eal_alarm_set(timeout * 1000,
1968                                       ixgbe_dev_interrupt_delayed_handler, (void*)dev) < 0)
1969                         PMD_DRV_LOG(ERR, "Error setting alarm");
1970         } else {
1971                 PMD_DRV_LOG(DEBUG, "enable intr immediately");
1972                 ixgbe_enable_intr(dev);
1973                 rte_intr_enable(&(dev->pci_dev->intr_handle));
1974         }
1975                         
1976
1977         return 0;
1978 }
1979
1980 /**
1981  * Interrupt handler which shall be registered for alarm callback for delayed
1982  * handling specific interrupt to wait for the stable nic state. As the
1983  * NIC interrupt state is not stable for ixgbe after link is just down,
1984  * it needs to wait 4 seconds to get the stable status.
1985  *
1986  * @param handle
1987  *  Pointer to interrupt handle.
1988  * @param param
1989  *  The address of parameter (struct rte_eth_dev *) regsitered before.
1990  *
1991  * @return
1992  *  void
1993  */
1994 static void
1995 ixgbe_dev_interrupt_delayed_handler(void *param)
1996 {
1997         struct rte_eth_dev *dev = (struct rte_eth_dev *)param;
1998         struct ixgbe_interrupt *intr =
1999                 IXGBE_DEV_PRIVATE_TO_INTR(dev->data->dev_private);
2000         struct ixgbe_hw *hw =
2001                 IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2002         uint32_t eicr;
2003
2004         eicr = IXGBE_READ_REG(hw, IXGBE_EICR);
2005         if (eicr & IXGBE_EICR_MAILBOX)
2006                 ixgbe_pf_mbx_process(dev);
2007
2008         if (intr->flags & IXGBE_FLAG_NEED_LINK_UPDATE) {
2009                 ixgbe_dev_link_update(dev, 0);
2010                 intr->flags &= ~IXGBE_FLAG_NEED_LINK_UPDATE;
2011                 ixgbe_dev_link_status_print(dev);
2012                 _rte_eth_dev_callback_process(dev, RTE_ETH_EVENT_INTR_LSC);
2013         }
2014
2015         PMD_DRV_LOG(DEBUG, "enable intr in delayed handler S[%08x]\n", eicr);
2016         ixgbe_enable_intr(dev);
2017         rte_intr_enable(&(dev->pci_dev->intr_handle));
2018 }
2019
2020 /**
2021  * Interrupt handler triggered by NIC  for handling
2022  * specific interrupt.
2023  *
2024  * @param handle
2025  *  Pointer to interrupt handle.
2026  * @param param
2027  *  The address of parameter (struct rte_eth_dev *) regsitered before.
2028  *
2029  * @return
2030  *  void
2031  */
2032 static void
2033 ixgbe_dev_interrupt_handler(__rte_unused struct rte_intr_handle *handle,
2034                                                         void *param)
2035 {
2036         struct rte_eth_dev *dev = (struct rte_eth_dev *)param;
2037         ixgbe_dev_interrupt_get_status(dev);
2038         ixgbe_dev_interrupt_action(dev);
2039 }
2040
2041 static int
2042 ixgbe_dev_led_on(struct rte_eth_dev *dev)
2043 {
2044         struct ixgbe_hw *hw;
2045
2046         hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2047         return (ixgbe_led_on(hw, 0) == IXGBE_SUCCESS ? 0 : -ENOTSUP);
2048 }
2049
2050 static int
2051 ixgbe_dev_led_off(struct rte_eth_dev *dev)
2052 {
2053         struct ixgbe_hw *hw;
2054
2055         hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2056         return (ixgbe_led_off(hw, 0) == IXGBE_SUCCESS ? 0 : -ENOTSUP);
2057 }
2058
2059 static int
2060 ixgbe_flow_ctrl_set(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
2061 {
2062         struct ixgbe_hw *hw;
2063         int err;
2064         uint32_t rx_buf_size;
2065         uint32_t max_high_water;
2066         uint32_t mflcn;
2067         enum ixgbe_fc_mode rte_fcmode_2_ixgbe_fcmode[] = {
2068                 ixgbe_fc_none,
2069                 ixgbe_fc_rx_pause,
2070                 ixgbe_fc_tx_pause,
2071                 ixgbe_fc_full
2072         };
2073
2074         PMD_INIT_FUNC_TRACE();
2075
2076         hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2077         rx_buf_size = IXGBE_READ_REG(hw, IXGBE_RXPBSIZE(0));
2078         PMD_INIT_LOG(DEBUG, "Rx packet buffer size = 0x%x \n", rx_buf_size);
2079
2080         /*
2081          * At least reserve one Ethernet frame for watermark
2082          * high_water/low_water in kilo bytes for ixgbe
2083          */
2084         max_high_water = (rx_buf_size - ETHER_MAX_LEN) >> IXGBE_RXPBSIZE_SHIFT;
2085         if ((fc_conf->high_water > max_high_water) ||
2086                 (fc_conf->high_water < fc_conf->low_water)) {
2087                 PMD_INIT_LOG(ERR, "Invalid high/low water setup value in KB\n");
2088                 PMD_INIT_LOG(ERR, "High_water must <=  0x%x\n", max_high_water);
2089                 return (-EINVAL);
2090         }
2091
2092         hw->fc.requested_mode = rte_fcmode_2_ixgbe_fcmode[fc_conf->mode];
2093         hw->fc.pause_time     = fc_conf->pause_time;
2094         hw->fc.high_water[0]  = fc_conf->high_water;
2095         hw->fc.low_water[0]   = fc_conf->low_water;
2096         hw->fc.send_xon       = fc_conf->send_xon;
2097
2098         err = ixgbe_fc_enable(hw);
2099
2100         /* Not negotiated is not an error case */
2101         if ((err == IXGBE_SUCCESS) || (err == IXGBE_ERR_FC_NOT_NEGOTIATED)) {
2102
2103                 /* check if we want to forward MAC frames - driver doesn't have native
2104                  * capability to do that, so we'll write the registers ourselves */
2105
2106                 mflcn = IXGBE_READ_REG(hw, IXGBE_MFLCN);
2107
2108                 /* set or clear MFLCN.PMCF bit depending on configuration */
2109                 if (fc_conf->mac_ctrl_frame_fwd != 0)
2110                         mflcn |= IXGBE_MFLCN_PMCF;
2111                 else
2112                         mflcn &= ~IXGBE_MFLCN_PMCF;
2113
2114                 IXGBE_WRITE_REG(hw, IXGBE_MFLCN, mflcn);
2115                 IXGBE_WRITE_FLUSH(hw);
2116
2117                 return 0;
2118         }
2119
2120         PMD_INIT_LOG(ERR, "ixgbe_fc_enable = 0x%x \n", err);
2121         return -EIO;
2122 }
2123
2124 /**
2125  *  ixgbe_pfc_enable_generic - Enable flow control
2126  *  @hw: pointer to hardware structure
2127  *  @tc_num: traffic class number
2128  *  Enable flow control according to the current settings.
2129  */
2130 static int 
2131 ixgbe_dcb_pfc_enable_generic(struct ixgbe_hw *hw,uint8_t tc_num)
2132 {
2133         int ret_val = 0;
2134         uint32_t mflcn_reg, fccfg_reg;
2135         uint32_t reg;
2136         uint32_t fcrtl, fcrth;
2137         uint8_t i;
2138         uint8_t nb_rx_en;
2139         
2140         /* Validate the water mark configuration */
2141         if (!hw->fc.pause_time) {
2142                 ret_val = IXGBE_ERR_INVALID_LINK_SETTINGS;
2143                 goto out;
2144         }
2145
2146         /* Low water mark of zero causes XOFF floods */
2147         if (hw->fc.current_mode & ixgbe_fc_tx_pause) {
2148                  /* High/Low water can not be 0 */
2149                 if( (!hw->fc.high_water[tc_num])|| (!hw->fc.low_water[tc_num])) {
2150                         PMD_INIT_LOG(ERR,"Invalid water mark configuration\n");
2151                         ret_val = IXGBE_ERR_INVALID_LINK_SETTINGS;
2152                         goto out;
2153                 }
2154  
2155                 if(hw->fc.low_water[tc_num] >= hw->fc.high_water[tc_num]) {
2156                         PMD_INIT_LOG(ERR,"Invalid water mark configuration\n");
2157                         ret_val = IXGBE_ERR_INVALID_LINK_SETTINGS;
2158                         goto out;
2159                 }
2160         }
2161         /* Negotiate the fc mode to use */
2162         ixgbe_fc_autoneg(hw);
2163
2164         /* Disable any previous flow control settings */
2165         mflcn_reg = IXGBE_READ_REG(hw, IXGBE_MFLCN);
2166         mflcn_reg &= ~(IXGBE_MFLCN_RPFCE_SHIFT | IXGBE_MFLCN_RFCE|IXGBE_MFLCN_RPFCE);
2167
2168         fccfg_reg = IXGBE_READ_REG(hw, IXGBE_FCCFG);
2169         fccfg_reg &= ~(IXGBE_FCCFG_TFCE_802_3X | IXGBE_FCCFG_TFCE_PRIORITY);
2170
2171         switch (hw->fc.current_mode) {
2172         case ixgbe_fc_none:
2173                 /*
2174                  * If the count of enabled RX Priority Flow control >1,
2175                  * and the TX pause can not be disabled 
2176                  */
2177                 nb_rx_en = 0;
2178                 for (i =0; i < IXGBE_DCB_MAX_TRAFFIC_CLASS; i++) {
2179                         reg = IXGBE_READ_REG(hw, IXGBE_FCRTH_82599(i));
2180                         if (reg & IXGBE_FCRTH_FCEN)
2181                                 nb_rx_en++;
2182                 }
2183                 if (nb_rx_en > 1)
2184                         fccfg_reg |=IXGBE_FCCFG_TFCE_PRIORITY;
2185                 break;
2186         case ixgbe_fc_rx_pause:
2187                 /*
2188                  * Rx Flow control is enabled and Tx Flow control is
2189                  * disabled by software override. Since there really
2190                  * isn't a way to advertise that we are capable of RX
2191                  * Pause ONLY, we will advertise that we support both
2192                  * symmetric and asymmetric Rx PAUSE.  Later, we will
2193                  * disable the adapter's ability to send PAUSE frames.
2194                  */
2195                 mflcn_reg |= IXGBE_MFLCN_RPFCE;
2196                 /*
2197                  * If the count of enabled RX Priority Flow control >1,
2198                  * and the TX pause can not be disabled
2199                  */
2200                 nb_rx_en = 0;
2201                 for (i =0; i < IXGBE_DCB_MAX_TRAFFIC_CLASS; i++) {
2202                         reg = IXGBE_READ_REG(hw, IXGBE_FCRTH_82599(i));
2203                         if (reg & IXGBE_FCRTH_FCEN)
2204                                 nb_rx_en++;
2205                 }
2206                 if (nb_rx_en > 1)
2207                         fccfg_reg |=IXGBE_FCCFG_TFCE_PRIORITY;
2208                 break;
2209         case ixgbe_fc_tx_pause:
2210                 /*
2211                  * Tx Flow control is enabled, and Rx Flow control is
2212                  * disabled by software override.
2213                  */
2214                 fccfg_reg |=IXGBE_FCCFG_TFCE_PRIORITY;
2215                 break;
2216         case ixgbe_fc_full:
2217                 /* Flow control (both Rx and Tx) is enabled by SW override. */
2218                 mflcn_reg |= IXGBE_MFLCN_RPFCE;
2219                 fccfg_reg |= IXGBE_FCCFG_TFCE_PRIORITY;
2220                 break;
2221         default:
2222                 DEBUGOUT("Flow control param set incorrectly\n");
2223                 ret_val = IXGBE_ERR_CONFIG;
2224                 goto out;
2225                 break;
2226         }
2227
2228         /* Set 802.3x based flow control settings. */
2229         mflcn_reg |= IXGBE_MFLCN_DPF;
2230         IXGBE_WRITE_REG(hw, IXGBE_MFLCN, mflcn_reg);
2231         IXGBE_WRITE_REG(hw, IXGBE_FCCFG, fccfg_reg);
2232
2233         /* Set up and enable Rx high/low water mark thresholds, enable XON. */
2234         if ((hw->fc.current_mode & ixgbe_fc_tx_pause) &&
2235                 hw->fc.high_water[tc_num]) {
2236                 fcrtl = (hw->fc.low_water[tc_num] << 10) | IXGBE_FCRTL_XONE;
2237                 IXGBE_WRITE_REG(hw, IXGBE_FCRTL_82599(tc_num), fcrtl);
2238                 fcrth = (hw->fc.high_water[tc_num] << 10) | IXGBE_FCRTH_FCEN;
2239         } else {
2240                 IXGBE_WRITE_REG(hw, IXGBE_FCRTL_82599(tc_num), 0);
2241                 /*
2242                  * In order to prevent Tx hangs when the internal Tx
2243                  * switch is enabled we must set the high water mark
2244                  * to the maximum FCRTH value.  This allows the Tx
2245                  * switch to function even under heavy Rx workloads.
2246                  */
2247                 fcrth = IXGBE_READ_REG(hw, IXGBE_RXPBSIZE(tc_num)) - 32;
2248         }
2249         IXGBE_WRITE_REG(hw, IXGBE_FCRTH_82599(tc_num), fcrth);
2250
2251         /* Configure pause time (2 TCs per register) */
2252         reg = hw->fc.pause_time * 0x00010001;
2253         for (i = 0; i < (IXGBE_DCB_MAX_TRAFFIC_CLASS / 2); i++)
2254                 IXGBE_WRITE_REG(hw, IXGBE_FCTTV(i), reg);
2255
2256         /* Configure flow control refresh threshold value */
2257         IXGBE_WRITE_REG(hw, IXGBE_FCRTV, hw->fc.pause_time / 2);
2258
2259 out:
2260         return ret_val;
2261 }
2262
2263 static int 
2264 ixgbe_dcb_pfc_enable(struct rte_eth_dev *dev,uint8_t tc_num)
2265 {
2266         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2267         int32_t ret_val = IXGBE_NOT_IMPLEMENTED;
2268
2269         if(hw->mac.type != ixgbe_mac_82598EB) {
2270                 ret_val = ixgbe_dcb_pfc_enable_generic(hw,tc_num);
2271         }
2272         return ret_val;
2273 }
2274
2275 static int 
2276 ixgbe_priority_flow_ctrl_set(struct rte_eth_dev *dev, struct rte_eth_pfc_conf *pfc_conf)
2277 {
2278         int err;
2279         uint32_t rx_buf_size;
2280         uint32_t max_high_water;
2281         uint8_t tc_num;
2282         uint8_t  map[IXGBE_DCB_MAX_USER_PRIORITY] = { 0 };
2283         struct ixgbe_hw *hw =
2284                 IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2285         struct ixgbe_dcb_config *dcb_config =
2286                 IXGBE_DEV_PRIVATE_TO_DCB_CFG(dev->data->dev_private);
2287         
2288         enum ixgbe_fc_mode rte_fcmode_2_ixgbe_fcmode[] = {
2289                 ixgbe_fc_none,
2290                 ixgbe_fc_rx_pause,
2291                 ixgbe_fc_tx_pause,
2292                 ixgbe_fc_full
2293         };
2294         
2295         PMD_INIT_FUNC_TRACE();
2296         
2297         ixgbe_dcb_unpack_map_cee(dcb_config, IXGBE_DCB_RX_CONFIG, map);
2298         tc_num = map[pfc_conf->priority];
2299         rx_buf_size = IXGBE_READ_REG(hw, IXGBE_RXPBSIZE(tc_num));
2300         PMD_INIT_LOG(DEBUG, "Rx packet buffer size = 0x%x \n", rx_buf_size);
2301         /*
2302          * At least reserve one Ethernet frame for watermark
2303          * high_water/low_water in kilo bytes for ixgbe
2304          */
2305         max_high_water = (rx_buf_size - ETHER_MAX_LEN) >> IXGBE_RXPBSIZE_SHIFT;
2306         if ((pfc_conf->fc.high_water > max_high_water) ||
2307                 (pfc_conf->fc.high_water <= pfc_conf->fc.low_water)) {
2308                 PMD_INIT_LOG(ERR, "Invalid high/low water setup value in KB\n");
2309                 PMD_INIT_LOG(ERR, "High_water must <=  0x%x\n", max_high_water);
2310                 return (-EINVAL);
2311         }
2312
2313         hw->fc.requested_mode = rte_fcmode_2_ixgbe_fcmode[pfc_conf->fc.mode];
2314         hw->fc.pause_time = pfc_conf->fc.pause_time;
2315         hw->fc.send_xon = pfc_conf->fc.send_xon;
2316         hw->fc.low_water[tc_num] =  pfc_conf->fc.low_water;
2317         hw->fc.high_water[tc_num] = pfc_conf->fc.high_water;
2318                 
2319         err = ixgbe_dcb_pfc_enable(dev,tc_num);
2320         
2321         /* Not negotiated is not an error case */
2322         if ((err == IXGBE_SUCCESS) || (err == IXGBE_ERR_FC_NOT_NEGOTIATED)) 
2323                 return 0;
2324
2325         PMD_INIT_LOG(ERR, "ixgbe_dcb_pfc_enable = 0x%x \n", err);
2326         return -EIO;
2327 }       
2328
2329 static int 
2330 ixgbe_dev_rss_reta_update(struct rte_eth_dev *dev,
2331                                 struct rte_eth_rss_reta *reta_conf)
2332 {       
2333         uint8_t i,j,mask;
2334         uint32_t reta;
2335         struct ixgbe_hw *hw = 
2336                         IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2337
2338         PMD_INIT_FUNC_TRACE();
2339         /*  
2340         * Update Redirection Table RETA[n],n=0...31,The redirection table has 
2341         * 128-entries in 32 registers
2342          */ 
2343         for(i = 0; i < ETH_RSS_RETA_NUM_ENTRIES; i += 4) {
2344                 if (i < ETH_RSS_RETA_NUM_ENTRIES/2) 
2345                         mask = (uint8_t)((reta_conf->mask_lo >> i) & 0xF);
2346                 else
2347                         mask = (uint8_t)((reta_conf->mask_hi >> 
2348                                 (i - ETH_RSS_RETA_NUM_ENTRIES/2)) & 0xF);
2349                 if (mask != 0) {
2350                         reta = 0;
2351                         if (mask != 0xF)
2352                                 reta = IXGBE_READ_REG(hw,IXGBE_RETA(i >> 2));
2353
2354                         for (j = 0; j < 4; j++) {
2355                                 if (mask & (0x1 << j)) {
2356                                         if (mask != 0xF)
2357                                                 reta &= ~(0xFF << 8 * j);
2358                                         reta |= reta_conf->reta[i + j] << 8*j;
2359                                 }
2360                         }
2361                         IXGBE_WRITE_REG(hw, IXGBE_RETA(i >> 2),reta);
2362                 }
2363         }
2364
2365         return 0;
2366 }
2367
2368 static int
2369 ixgbe_dev_rss_reta_query(struct rte_eth_dev *dev,
2370                                 struct rte_eth_rss_reta *reta_conf)
2371 {
2372         uint8_t i,j,mask;
2373         uint32_t reta;
2374         struct ixgbe_hw *hw =
2375                         IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2376         
2377         PMD_INIT_FUNC_TRACE();
2378         /* 
2379          * Read Redirection Table RETA[n],n=0...31,The redirection table has 
2380          * 128-entries in 32 registers
2381          */
2382         for(i = 0; i < ETH_RSS_RETA_NUM_ENTRIES; i += 4) {
2383                 if (i < ETH_RSS_RETA_NUM_ENTRIES/2)
2384                         mask = (uint8_t)((reta_conf->mask_lo >> i) & 0xF);
2385                 else
2386                         mask = (uint8_t)((reta_conf->mask_hi >> 
2387                                 (i - ETH_RSS_RETA_NUM_ENTRIES/2)) & 0xF);
2388
2389                 if (mask != 0) {
2390                         reta = IXGBE_READ_REG(hw,IXGBE_RETA(i >> 2));
2391                         for (j = 0; j < 4; j++) {
2392                                 if (mask & (0x1 << j))
2393                                         reta_conf->reta[i + j] = 
2394                                                 (uint8_t)((reta >> 8 * j) & 0xFF);
2395                         } 
2396                 }
2397         }
2398
2399         return 0;               
2400 }
2401
2402 static void
2403 ixgbe_add_rar(struct rte_eth_dev *dev, struct ether_addr *mac_addr,
2404                                 uint32_t index, uint32_t pool)
2405 {
2406         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2407         uint32_t enable_addr = 1;
2408
2409         ixgbe_set_rar(hw, index, mac_addr->addr_bytes, pool, enable_addr);
2410 }
2411
2412 static void
2413 ixgbe_remove_rar(struct rte_eth_dev *dev, uint32_t index)
2414 {
2415         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2416
2417         ixgbe_clear_rar(hw, index);
2418 }
2419
2420 /*
2421  * Virtual Function operations
2422  */
2423 static void
2424 ixgbevf_intr_disable(struct ixgbe_hw *hw)
2425 {
2426         PMD_INIT_LOG(DEBUG, "ixgbevf_intr_disable");
2427
2428         /* Clear interrupt mask to stop from interrupts being generated */
2429         IXGBE_WRITE_REG(hw, IXGBE_VTEIMC, IXGBE_VF_IRQ_CLEAR_MASK);
2430
2431         IXGBE_WRITE_FLUSH(hw);
2432 }
2433
2434 static int
2435 ixgbevf_dev_configure(struct rte_eth_dev *dev)
2436 {
2437         struct rte_eth_conf* conf = &dev->data->dev_conf;
2438
2439         PMD_INIT_LOG(DEBUG, "\nConfigured Virtual Function port id: %d\n",
2440                 dev->data->port_id);
2441
2442         /*
2443          * VF has no ability to enable/disable HW CRC
2444          * Keep the persistent behavior the same as Host PF
2445          */
2446 #ifndef RTE_LIBRTE_IXGBE_PF_DISABLE_STRIP_CRC
2447         if (!conf->rxmode.hw_strip_crc) {
2448                 PMD_INIT_LOG(INFO, "VF can't disable HW CRC Strip\n");
2449                 conf->rxmode.hw_strip_crc = 1;
2450         }
2451 #else
2452         if (conf->rxmode.hw_strip_crc) {
2453                 PMD_INIT_LOG(INFO, "VF can't enable HW CRC Strip\n");
2454                 conf->rxmode.hw_strip_crc = 0;
2455         }
2456 #endif
2457
2458         return 0;
2459 }
2460
2461 static int
2462 ixgbevf_dev_start(struct rte_eth_dev *dev)
2463 {
2464         struct ixgbe_hw *hw = 
2465                 IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2466         int err, mask = 0;
2467         
2468         PMD_INIT_LOG(DEBUG, "ixgbevf_dev_start");
2469
2470         hw->mac.ops.reset_hw(hw);
2471
2472         ixgbevf_dev_tx_init(dev);
2473
2474         /* This can fail when allocating mbufs for descriptor rings */
2475         err = ixgbevf_dev_rx_init(dev);
2476         if (err) {
2477                 PMD_INIT_LOG(ERR, "Unable to initialize RX hardware (%d)\n", err);
2478                 ixgbe_dev_clear_queues(dev);
2479                 return err;
2480         }
2481         
2482         /* Set vfta */
2483         ixgbevf_set_vfta_all(dev,1);
2484
2485         /* Set HW strip */
2486         mask = ETH_VLAN_STRIP_MASK | ETH_VLAN_FILTER_MASK | \
2487                 ETH_VLAN_EXTEND_MASK;
2488         ixgbevf_vlan_offload_set(dev, mask);
2489
2490         ixgbevf_dev_rxtx_start(dev);
2491
2492         return 0;
2493 }
2494
2495 static void
2496 ixgbevf_dev_stop(struct rte_eth_dev *dev)
2497 {
2498         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2499
2500         PMD_INIT_LOG(DEBUG, "ixgbevf_dev_stop");
2501                 
2502         hw->adapter_stopped = TRUE;
2503         ixgbe_stop_adapter(hw);
2504
2505         /* 
2506           * Clear what we set, but we still keep shadow_vfta to 
2507           * restore after device starts
2508           */
2509         ixgbevf_set_vfta_all(dev,0);
2510
2511         ixgbe_dev_clear_queues(dev);
2512 }
2513
2514 static void
2515 ixgbevf_dev_close(struct rte_eth_dev *dev)
2516 {
2517         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2518
2519         PMD_INIT_LOG(DEBUG, "ixgbevf_dev_close");
2520
2521         ixgbe_reset_hw(hw);
2522
2523         ixgbevf_dev_stop(dev);
2524
2525         /* reprogram the RAR[0] in case user changed it. */
2526         ixgbe_set_rar(hw, 0, hw->mac.addr, 0, IXGBE_RAH_AV);
2527 }
2528
2529 static void ixgbevf_set_vfta_all(struct rte_eth_dev *dev, bool on)
2530 {
2531         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2532         struct ixgbe_vfta * shadow_vfta =
2533                 IXGBE_DEV_PRIVATE_TO_VFTA(dev->data->dev_private);
2534         int i = 0, j = 0, vfta = 0, mask = 1;
2535
2536         for (i = 0; i < IXGBE_VFTA_SIZE; i++){
2537                 vfta = shadow_vfta->vfta[i];
2538                 if(vfta){
2539                         mask = 1;
2540                         for (j = 0; j < 32; j++){
2541                                 if(vfta & mask)
2542                                         ixgbe_set_vfta(hw, (i<<5)+j, 0, on);
2543                                 mask<<=1;
2544                         }
2545                 }
2546         }
2547
2548 }
2549
2550 static int
2551 ixgbevf_vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
2552 {
2553         struct ixgbe_hw *hw =
2554                 IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2555         struct ixgbe_vfta * shadow_vfta =
2556                 IXGBE_DEV_PRIVATE_TO_VFTA(dev->data->dev_private);
2557         uint32_t vid_idx = 0;
2558         uint32_t vid_bit = 0;
2559         int ret = 0;
2560         
2561         PMD_INIT_FUNC_TRACE();
2562
2563         /* vind is not used in VF driver, set to 0, check ixgbe_set_vfta_vf */
2564         ret = ixgbe_set_vfta(hw, vlan_id, 0, !!on);
2565         if(ret){
2566                 PMD_INIT_LOG(ERR, "Unable to set VF vlan");
2567                 return ret;
2568         }
2569         vid_idx = (uint32_t) ((vlan_id >> 5) & 0x7F);
2570         vid_bit = (uint32_t) (1 << (vlan_id & 0x1F));
2571
2572         /* Save what we set and retore it after device reset */
2573         if (on)
2574                 shadow_vfta->vfta[vid_idx] |= vid_bit;
2575         else
2576                 shadow_vfta->vfta[vid_idx] &= ~vid_bit;
2577
2578         return 0;
2579 }
2580
2581 static void
2582 ixgbevf_vlan_strip_queue_set(struct rte_eth_dev *dev, uint16_t queue, int on)
2583 {
2584         struct ixgbe_hw *hw =
2585                 IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2586         uint32_t ctrl;
2587
2588         PMD_INIT_FUNC_TRACE();
2589         
2590         if(queue >= hw->mac.max_rx_queues)
2591                 return;
2592
2593         ctrl = IXGBE_READ_REG(hw, IXGBE_RXDCTL(queue));
2594         if(on)
2595                 ctrl |= IXGBE_RXDCTL_VME;
2596         else 
2597                 ctrl &= ~IXGBE_RXDCTL_VME;
2598         IXGBE_WRITE_REG(hw, IXGBE_RXDCTL(queue), ctrl);
2599
2600         ixgbe_vlan_hw_strip_bitmap_set( dev, queue, on);
2601 }
2602
2603 static void
2604 ixgbevf_vlan_offload_set(struct rte_eth_dev *dev, int mask)
2605 {
2606         struct ixgbe_hw *hw =
2607                 IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2608         uint16_t i;
2609         int on = 0;
2610
2611         /* VF function only support hw strip feature, others are not support */
2612         if(mask & ETH_VLAN_STRIP_MASK){
2613                 on = !!(dev->data->dev_conf.rxmode.hw_vlan_strip);
2614
2615                 for(i=0; i < hw->mac.max_rx_queues; i++)
2616                         ixgbevf_vlan_strip_queue_set(dev,i,on);
2617         }
2618 }
2619
2620 static int
2621 ixgbe_vmdq_mode_check(struct ixgbe_hw *hw)
2622 {
2623         uint32_t reg_val;
2624         
2625         /* we only need to do this if VMDq is enabled */
2626         reg_val = IXGBE_READ_REG(hw, IXGBE_VT_CTL);
2627         if (!(reg_val & IXGBE_VT_CTL_VT_ENABLE)) {
2628                 PMD_INIT_LOG(ERR, "VMDq must be enabled for this setting\n");
2629                 return (-1);
2630         }
2631         
2632         return 0;
2633 }
2634
2635 static uint32_t 
2636 ixgbe_uta_vector(struct ixgbe_hw *hw, struct ether_addr* uc_addr)
2637 {
2638         uint32_t vector = 0;
2639         switch (hw->mac.mc_filter_type) {
2640         case 0:   /* use bits [47:36] of the address */
2641                 vector = ((uc_addr->addr_bytes[4] >> 4) | 
2642                         (((uint16_t)uc_addr->addr_bytes[5]) << 4));
2643                 break;
2644         case 1:   /* use bits [46:35] of the address */
2645                 vector = ((uc_addr->addr_bytes[4] >> 3) | 
2646                         (((uint16_t)uc_addr->addr_bytes[5]) << 5));
2647                 break;
2648         case 2:   /* use bits [45:34] of the address */
2649                 vector = ((uc_addr->addr_bytes[4] >> 2) | 
2650                         (((uint16_t)uc_addr->addr_bytes[5]) << 6));
2651                 break;
2652         case 3:   /* use bits [43:32] of the address */
2653                 vector = ((uc_addr->addr_bytes[4]) | 
2654                         (((uint16_t)uc_addr->addr_bytes[5]) << 8));
2655                 break;
2656         default:  /* Invalid mc_filter_type */
2657                 break;
2658         }
2659
2660         /* vector can only be 12-bits or boundary will be exceeded */
2661         vector &= 0xFFF;
2662         return vector;
2663 }
2664
2665 static int 
2666 ixgbe_uc_hash_table_set(struct rte_eth_dev *dev,struct ether_addr* mac_addr,
2667                                uint8_t on)
2668 {
2669         uint32_t vector;
2670         uint32_t uta_idx;
2671         uint32_t reg_val;
2672         uint32_t uta_shift;
2673         uint32_t rc;
2674         const uint32_t ixgbe_uta_idx_mask = 0x7F;
2675         const uint32_t ixgbe_uta_bit_shift = 5;
2676         const uint32_t ixgbe_uta_bit_mask = (0x1 << ixgbe_uta_bit_shift) - 1;
2677         const uint32_t bit1 = 0x1;
2678         
2679         struct ixgbe_hw *hw =
2680                 IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2681         struct ixgbe_uta_info *uta_info =
2682                 IXGBE_DEV_PRIVATE_TO_UTA(dev->data->dev_private);
2683         
2684         /* The UTA table only exists on 82599 hardware and newer */
2685         if (hw->mac.type < ixgbe_mac_82599EB)
2686                 return (-ENOTSUP);
2687         
2688         vector = ixgbe_uta_vector(hw,mac_addr);
2689         uta_idx = (vector >> ixgbe_uta_bit_shift) & ixgbe_uta_idx_mask;
2690         uta_shift = vector & ixgbe_uta_bit_mask;
2691         
2692         rc = ((uta_info->uta_shadow[uta_idx] >> uta_shift & bit1) != 0);
2693         if(rc == on)
2694                 return 0;
2695         
2696         reg_val = IXGBE_READ_REG(hw, IXGBE_UTA(uta_idx));
2697         if (on) {
2698                 uta_info->uta_in_use++;
2699                 reg_val |= (bit1 << uta_shift);
2700                 uta_info->uta_shadow[uta_idx] |= (bit1 << uta_shift);
2701         } else {
2702                 uta_info->uta_in_use--;
2703                 reg_val &= ~(bit1 << uta_shift);
2704                 uta_info->uta_shadow[uta_idx] &= ~(bit1 << uta_shift);
2705         }
2706         
2707         IXGBE_WRITE_REG(hw, IXGBE_UTA(uta_idx), reg_val);
2708         
2709         if (uta_info->uta_in_use > 0)
2710                 IXGBE_WRITE_REG(hw, IXGBE_MCSTCTRL,
2711                                 IXGBE_MCSTCTRL_MFE | hw->mac.mc_filter_type);
2712         else
2713                 IXGBE_WRITE_REG(hw, IXGBE_MCSTCTRL,hw->mac.mc_filter_type);
2714         
2715         return 0;
2716 }
2717
2718 static int
2719 ixgbe_uc_all_hash_table_set(struct rte_eth_dev *dev, uint8_t on)
2720 {
2721         int i;
2722         struct ixgbe_hw *hw =
2723                 IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2724         struct ixgbe_uta_info *uta_info =
2725                 IXGBE_DEV_PRIVATE_TO_UTA(dev->data->dev_private);
2726
2727         /* The UTA table only exists on 82599 hardware and newer */
2728         if (hw->mac.type < ixgbe_mac_82599EB)
2729                 return (-ENOTSUP);
2730         
2731         if(on) {
2732                 for (i = 0; i < ETH_VMDQ_NUM_UC_HASH_ARRAY; i++) {
2733                         uta_info->uta_shadow[i] = ~0;
2734                         IXGBE_WRITE_REG(hw, IXGBE_UTA(i), ~0);
2735                 }
2736         } else {
2737                 for (i = 0; i < ETH_VMDQ_NUM_UC_HASH_ARRAY; i++) {
2738                         uta_info->uta_shadow[i] = 0;
2739                         IXGBE_WRITE_REG(hw, IXGBE_UTA(i), 0);
2740                 }
2741         }
2742         return 0;
2743         
2744 }
2745 static int
2746 ixgbe_set_pool_rx_mode(struct rte_eth_dev *dev, uint16_t pool,
2747                                uint16_t rx_mask, uint8_t on)
2748 {
2749         int val = 0;
2750         
2751         struct ixgbe_hw *hw =
2752                 IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2753         uint32_t vmolr = IXGBE_READ_REG(hw, IXGBE_VMOLR(pool));
2754         
2755         if (hw->mac.type == ixgbe_mac_82598EB) {
2756                 PMD_INIT_LOG(ERR, "setting VF receive mode set should be done"
2757                         " on 82599 hardware and newer\n");
2758                 return (-ENOTSUP);
2759         }
2760         if (ixgbe_vmdq_mode_check(hw) < 0)
2761                 return (-ENOTSUP);
2762
2763         if (rx_mask & ETH_VMDQ_ACCEPT_UNTAG )
2764                 val |= IXGBE_VMOLR_AUPE;
2765         if (rx_mask & ETH_VMDQ_ACCEPT_HASH_MC )
2766                 val |= IXGBE_VMOLR_ROMPE;
2767         if (rx_mask & ETH_VMDQ_ACCEPT_HASH_UC)
2768                 val |= IXGBE_VMOLR_ROPE;
2769         if (rx_mask & ETH_VMDQ_ACCEPT_BROADCAST)
2770                 val |= IXGBE_VMOLR_BAM;
2771         if (rx_mask & ETH_VMDQ_ACCEPT_MULTICAST)
2772                 val |= IXGBE_VMOLR_MPE;
2773
2774         if (on)
2775                 vmolr |= val;
2776         else 
2777                 vmolr &= ~val;
2778
2779         IXGBE_WRITE_REG(hw, IXGBE_VMOLR(pool), vmolr);
2780         
2781         return 0;
2782 }
2783
2784 static int
2785 ixgbe_set_pool_rx(struct rte_eth_dev *dev, uint16_t pool, uint8_t on)
2786 {
2787         uint32_t reg,addr;
2788         uint32_t val;
2789         const uint8_t bit1 = 0x1;
2790         
2791         struct ixgbe_hw *hw =
2792                 IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2793
2794         if (ixgbe_vmdq_mode_check(hw) < 0)
2795                 return (-ENOTSUP);
2796         
2797         addr = IXGBE_VFRE(pool >= ETH_64_POOLS/2);
2798         reg = IXGBE_READ_REG(hw, addr);
2799         val = bit1 << pool;
2800
2801         if (on)
2802                 reg |= val;
2803         else
2804                 reg &= ~val;
2805         
2806         IXGBE_WRITE_REG(hw, addr,reg);
2807         
2808         return 0;
2809 }
2810
2811 static int
2812 ixgbe_set_pool_tx(struct rte_eth_dev *dev, uint16_t pool, uint8_t on)
2813 {
2814         uint32_t reg,addr;
2815         uint32_t val;
2816         const uint8_t bit1 = 0x1;
2817         
2818         struct ixgbe_hw *hw =
2819                 IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2820
2821         if (ixgbe_vmdq_mode_check(hw) < 0)
2822                 return (-ENOTSUP);
2823         
2824         addr = IXGBE_VFTE(pool >= ETH_64_POOLS/2);
2825         reg = IXGBE_READ_REG(hw, addr);
2826         val = bit1 << pool;
2827
2828         if (on)
2829                 reg |= val;
2830         else
2831                 reg &= ~val;
2832         
2833         IXGBE_WRITE_REG(hw, addr,reg);
2834         
2835         return 0;
2836 }
2837
2838 static int 
2839 ixgbe_set_pool_vlan_filter(struct rte_eth_dev *dev, uint16_t vlan,
2840                         uint64_t pool_mask, uint8_t vlan_on)
2841 {
2842         int ret = 0;
2843         uint16_t pool_idx;
2844         struct ixgbe_hw *hw =
2845                 IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2846         
2847         if (ixgbe_vmdq_mode_check(hw) < 0)
2848                 return (-ENOTSUP);
2849         for (pool_idx = 0; pool_idx < ETH_64_POOLS; pool_idx++) {
2850                 if (pool_mask & ((uint64_t)(1ULL << pool_idx))) 
2851                         ret = hw->mac.ops.set_vfta(hw,vlan,pool_idx,vlan_on);
2852                         if (ret < 0) 
2853                                 return ret;     
2854         }
2855
2856         return ret;
2857 }
2858
2859 static int
2860 ixgbe_mirror_rule_set(struct rte_eth_dev *dev,
2861                         struct rte_eth_vmdq_mirror_conf *mirror_conf, 
2862                         uint8_t rule_id, uint8_t on)
2863 {
2864         uint32_t mr_ctl,vlvf;
2865         uint32_t mp_lsb = 0;
2866         uint32_t mv_msb = 0;
2867         uint32_t mv_lsb = 0;
2868         uint32_t mp_msb = 0;
2869         uint8_t i = 0;
2870         int reg_index = 0;
2871         uint64_t vlan_mask = 0;
2872         
2873         const uint8_t pool_mask_offset = 32;
2874         const uint8_t vlan_mask_offset = 32;
2875         const uint8_t dst_pool_offset = 8;
2876         const uint8_t rule_mr_offset  = 4;
2877         const uint8_t mirror_rule_mask= 0x0F;
2878
2879         struct ixgbe_mirror_info *mr_info =
2880                         (IXGBE_DEV_PRIVATE_TO_PFDATA(dev->data->dev_private));
2881         struct ixgbe_hw *hw =
2882                 IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2883
2884         if (ixgbe_vmdq_mode_check(hw) < 0)
2885                 return (-ENOTSUP);
2886
2887         /* Check if vlan mask is valid */
2888         if ((mirror_conf->rule_type_mask & ETH_VMDQ_VLAN_MIRROR) && (on)) {
2889                 if (mirror_conf->vlan.vlan_mask == 0)
2890                         return (-EINVAL);
2891         }
2892
2893         /* Check if vlan id is valid and find conresponding VLAN ID index in VLVF */
2894         if (mirror_conf->rule_type_mask & ETH_VMDQ_VLAN_MIRROR) {
2895                 for (i = 0;i < IXGBE_VLVF_ENTRIES; i++) {
2896                         if (mirror_conf->vlan.vlan_mask & (1ULL << i)) {
2897                                 /* search vlan id related pool vlan filter index */
2898                                 reg_index = ixgbe_find_vlvf_slot(hw,
2899                                                 mirror_conf->vlan.vlan_id[i]);
2900                                 if(reg_index < 0)
2901                                         return (-EINVAL);
2902                                 vlvf = IXGBE_READ_REG(hw, IXGBE_VLVF(reg_index));
2903                                 if ((vlvf & IXGBE_VLVF_VIEN) &&
2904                                         ((vlvf & IXGBE_VLVF_VLANID_MASK)
2905                                                 == mirror_conf->vlan.vlan_id[i]))
2906                                         vlan_mask |= (1ULL << reg_index);
2907                                 else
2908                                         return (-EINVAL);
2909                         }
2910                 }
2911
2912                 if (on) {
2913                         mv_lsb = vlan_mask & 0xFFFFFFFF;
2914                         mv_msb = vlan_mask >> vlan_mask_offset;
2915                         
2916                         mr_info->mr_conf[rule_id].vlan.vlan_mask =
2917                                                 mirror_conf->vlan.vlan_mask;
2918                         for(i = 0 ;i < ETH_VMDQ_MAX_VLAN_FILTERS; i++) {
2919                                 if(mirror_conf->vlan.vlan_mask & (1ULL << i))
2920                                         mr_info->mr_conf[rule_id].vlan.vlan_id[i] =
2921                                                 mirror_conf->vlan.vlan_id[i];
2922                         }
2923                 } else {
2924                         mv_lsb = 0;
2925                         mv_msb = 0;
2926                         mr_info->mr_conf[rule_id].vlan.vlan_mask = 0;
2927                         for(i = 0 ;i < ETH_VMDQ_MAX_VLAN_FILTERS; i++)
2928                                 mr_info->mr_conf[rule_id].vlan.vlan_id[i] = 0;
2929                 }
2930         }
2931
2932         /*
2933          * if enable pool mirror, write related pool mask register,if disable 
2934          * pool mirror, clear PFMRVM register
2935          */
2936         if (mirror_conf->rule_type_mask & ETH_VMDQ_POOL_MIRROR) {
2937                 if (on) { 
2938                         mp_lsb = mirror_conf->pool_mask & 0xFFFFFFFF;
2939                         mp_msb = mirror_conf->pool_mask >> pool_mask_offset;
2940                         mr_info->mr_conf[rule_id].pool_mask = 
2941                                         mirror_conf->pool_mask;
2942                         
2943                 } else {
2944                         mp_lsb = 0;
2945                         mp_msb = 0;
2946                         mr_info->mr_conf[rule_id].pool_mask = 0;
2947                 }
2948         }
2949         
2950         /* read  mirror control register and recalculate it */
2951         mr_ctl = IXGBE_READ_REG(hw,IXGBE_MRCTL(rule_id));
2952
2953         if (on) {
2954                 mr_ctl |= mirror_conf->rule_type_mask;
2955                 mr_ctl &= mirror_rule_mask;
2956                 mr_ctl |= mirror_conf->dst_pool << dst_pool_offset;
2957         } else
2958                 mr_ctl &= ~(mirror_conf->rule_type_mask & mirror_rule_mask);
2959
2960         mr_info->mr_conf[rule_id].rule_type_mask = (uint8_t)(mr_ctl & mirror_rule_mask);
2961         mr_info->mr_conf[rule_id].dst_pool = mirror_conf->dst_pool;
2962
2963         /* write mirrror control  register */
2964         IXGBE_WRITE_REG(hw, IXGBE_MRCTL(rule_id), mr_ctl);
2965         
2966         /* write pool mirrror control  register */
2967         if (mirror_conf->rule_type_mask & ETH_VMDQ_POOL_MIRROR) {
2968                 IXGBE_WRITE_REG(hw, IXGBE_VMRVM(rule_id), mp_lsb);
2969                 IXGBE_WRITE_REG(hw, IXGBE_VMRVM(rule_id + rule_mr_offset),
2970                                 mp_msb);
2971         }
2972         /* write VLAN mirrror control  register */
2973         if (mirror_conf->rule_type_mask & ETH_VMDQ_VLAN_MIRROR) {
2974                 IXGBE_WRITE_REG(hw, IXGBE_VMRVLAN(rule_id), mv_lsb);
2975                 IXGBE_WRITE_REG(hw, IXGBE_VMRVLAN(rule_id + rule_mr_offset),
2976                                 mv_msb);
2977         }
2978
2979         return 0;
2980 }
2981
2982 static int 
2983 ixgbe_mirror_rule_reset(struct rte_eth_dev *dev, uint8_t rule_id)
2984 {
2985         int mr_ctl = 0;
2986         uint32_t lsb_val = 0;
2987         uint32_t msb_val = 0;
2988         const uint8_t rule_mr_offset = 4;
2989         
2990         struct ixgbe_hw *hw =
2991                 IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2992         struct ixgbe_mirror_info *mr_info = 
2993                 (IXGBE_DEV_PRIVATE_TO_PFDATA(dev->data->dev_private));
2994         
2995         if (ixgbe_vmdq_mode_check(hw) < 0)
2996                 return (-ENOTSUP);
2997
2998         memset(&mr_info->mr_conf[rule_id], 0,
2999                 sizeof(struct rte_eth_vmdq_mirror_conf));
3000
3001         /* clear PFVMCTL register */
3002         IXGBE_WRITE_REG(hw, IXGBE_MRCTL(rule_id), mr_ctl);
3003
3004         /* clear pool mask register */
3005         IXGBE_WRITE_REG(hw, IXGBE_VMRVM(rule_id), lsb_val);
3006         IXGBE_WRITE_REG(hw, IXGBE_VMRVM(rule_id + rule_mr_offset), msb_val);
3007
3008         /* clear vlan mask register */
3009         IXGBE_WRITE_REG(hw, IXGBE_VMRVLAN(rule_id), lsb_val);
3010         IXGBE_WRITE_REG(hw, IXGBE_VMRVLAN(rule_id + rule_mr_offset), msb_val);
3011
3012         return 0;
3013 }