ixgbe: reset hardware stats on initialization
[dpdk.git] / drivers / net / ixgbe / ixgbe_ethdev.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2015 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include <sys/queue.h>
35 #include <stdio.h>
36 #include <errno.h>
37 #include <stdint.h>
38 #include <string.h>
39 #include <unistd.h>
40 #include <stdarg.h>
41 #include <inttypes.h>
42 #include <netinet/in.h>
43 #include <rte_byteorder.h>
44 #include <rte_common.h>
45 #include <rte_cycles.h>
46
47 #include <rte_interrupts.h>
48 #include <rte_log.h>
49 #include <rte_debug.h>
50 #include <rte_pci.h>
51 #include <rte_atomic.h>
52 #include <rte_branch_prediction.h>
53 #include <rte_memory.h>
54 #include <rte_memzone.h>
55 #include <rte_eal.h>
56 #include <rte_alarm.h>
57 #include <rte_ether.h>
58 #include <rte_ethdev.h>
59 #include <rte_atomic.h>
60 #include <rte_malloc.h>
61 #include <rte_random.h>
62 #include <rte_dev.h>
63
64 #include "ixgbe_logs.h"
65 #include "base/ixgbe_api.h"
66 #include "base/ixgbe_vf.h"
67 #include "base/ixgbe_common.h"
68 #include "ixgbe_ethdev.h"
69 #include "ixgbe_bypass.h"
70 #include "ixgbe_rxtx.h"
71
72 /*
73  * High threshold controlling when to start sending XOFF frames. Must be at
74  * least 8 bytes less than receive packet buffer size. This value is in units
75  * of 1024 bytes.
76  */
77 #define IXGBE_FC_HI    0x80
78
79 /*
80  * Low threshold controlling when to start sending XON frames. This value is
81  * in units of 1024 bytes.
82  */
83 #define IXGBE_FC_LO    0x40
84
85 /* Timer value included in XOFF frames. */
86 #define IXGBE_FC_PAUSE 0x680
87
88 #define IXGBE_LINK_DOWN_CHECK_TIMEOUT 4000 /* ms */
89 #define IXGBE_LINK_UP_CHECK_TIMEOUT   1000 /* ms */
90 #define IXGBE_VMDQ_NUM_UC_MAC         4096 /* Maximum nb. of UC MAC addr. */
91
92 #define IXGBE_MMW_SIZE_DEFAULT        0x4
93 #define IXGBE_MMW_SIZE_JUMBO_FRAME    0x14
94
95 /*
96  *  Default values for RX/TX configuration
97  */
98 #define IXGBE_DEFAULT_RX_FREE_THRESH  32
99 #define IXGBE_DEFAULT_RX_PTHRESH      8
100 #define IXGBE_DEFAULT_RX_HTHRESH      8
101 #define IXGBE_DEFAULT_RX_WTHRESH      0
102
103 #define IXGBE_DEFAULT_TX_FREE_THRESH  32
104 #define IXGBE_DEFAULT_TX_PTHRESH      32
105 #define IXGBE_DEFAULT_TX_HTHRESH      0
106 #define IXGBE_DEFAULT_TX_WTHRESH      0
107 #define IXGBE_DEFAULT_TX_RSBIT_THRESH 32
108
109 /* Bit shift and mask */
110 #define IXGBE_4_BIT_WIDTH  (CHAR_BIT / 2)
111 #define IXGBE_4_BIT_MASK   RTE_LEN2MASK(IXGBE_4_BIT_WIDTH, uint8_t)
112 #define IXGBE_8_BIT_WIDTH  CHAR_BIT
113 #define IXGBE_8_BIT_MASK   UINT8_MAX
114
115 #define IXGBEVF_PMD_NAME "rte_ixgbevf_pmd" /* PMD name */
116
117 #define IXGBE_QUEUE_STAT_COUNTERS (sizeof(hw_stats->qprc) / sizeof(hw_stats->qprc[0]))
118
119 #define IXGBE_HKEY_MAX_INDEX 10
120
121 /* Additional timesync values. */
122 #define IXGBE_TIMINCA_16NS_SHIFT 24
123 #define IXGBE_TIMINCA_INCVALUE   16000000
124 #define IXGBE_TIMINCA_INIT       ((0x02 << IXGBE_TIMINCA_16NS_SHIFT) \
125                                   | IXGBE_TIMINCA_INCVALUE)
126
127 static int eth_ixgbe_dev_init(struct rte_eth_dev *eth_dev);
128 static int  ixgbe_dev_configure(struct rte_eth_dev *dev);
129 static int  ixgbe_dev_start(struct rte_eth_dev *dev);
130 static void ixgbe_dev_stop(struct rte_eth_dev *dev);
131 static int  ixgbe_dev_set_link_up(struct rte_eth_dev *dev);
132 static int  ixgbe_dev_set_link_down(struct rte_eth_dev *dev);
133 static void ixgbe_dev_close(struct rte_eth_dev *dev);
134 static void ixgbe_dev_promiscuous_enable(struct rte_eth_dev *dev);
135 static void ixgbe_dev_promiscuous_disable(struct rte_eth_dev *dev);
136 static void ixgbe_dev_allmulticast_enable(struct rte_eth_dev *dev);
137 static void ixgbe_dev_allmulticast_disable(struct rte_eth_dev *dev);
138 static int ixgbe_dev_link_update(struct rte_eth_dev *dev,
139                                 int wait_to_complete);
140 static void ixgbe_dev_stats_get(struct rte_eth_dev *dev,
141                                 struct rte_eth_stats *stats);
142 static void ixgbe_dev_stats_reset(struct rte_eth_dev *dev);
143 static int ixgbe_dev_queue_stats_mapping_set(struct rte_eth_dev *eth_dev,
144                                              uint16_t queue_id,
145                                              uint8_t stat_idx,
146                                              uint8_t is_rx);
147 static void ixgbe_dev_info_get(struct rte_eth_dev *dev,
148                                struct rte_eth_dev_info *dev_info);
149 static void ixgbevf_dev_info_get(struct rte_eth_dev *dev,
150                                  struct rte_eth_dev_info *dev_info);
151 static int ixgbe_dev_mtu_set(struct rte_eth_dev *dev, uint16_t mtu);
152
153 static int ixgbe_vlan_filter_set(struct rte_eth_dev *dev,
154                 uint16_t vlan_id, int on);
155 static void ixgbe_vlan_tpid_set(struct rte_eth_dev *dev, uint16_t tpid_id);
156 static void ixgbe_vlan_hw_strip_bitmap_set(struct rte_eth_dev *dev,
157                 uint16_t queue, bool on);
158 static void ixgbe_vlan_strip_queue_set(struct rte_eth_dev *dev, uint16_t queue,
159                 int on);
160 static void ixgbe_vlan_offload_set(struct rte_eth_dev *dev, int mask);
161 static void ixgbe_vlan_hw_strip_enable(struct rte_eth_dev *dev, uint16_t queue);
162 static void ixgbe_vlan_hw_strip_disable(struct rte_eth_dev *dev, uint16_t queue);
163 static void ixgbe_vlan_hw_extend_enable(struct rte_eth_dev *dev);
164 static void ixgbe_vlan_hw_extend_disable(struct rte_eth_dev *dev);
165
166 static int ixgbe_dev_led_on(struct rte_eth_dev *dev);
167 static int ixgbe_dev_led_off(struct rte_eth_dev *dev);
168 static int ixgbe_flow_ctrl_get(struct rte_eth_dev *dev,
169                                struct rte_eth_fc_conf *fc_conf);
170 static int ixgbe_flow_ctrl_set(struct rte_eth_dev *dev,
171                                struct rte_eth_fc_conf *fc_conf);
172 static int ixgbe_priority_flow_ctrl_set(struct rte_eth_dev *dev,
173                 struct rte_eth_pfc_conf *pfc_conf);
174 static int ixgbe_dev_rss_reta_update(struct rte_eth_dev *dev,
175                         struct rte_eth_rss_reta_entry64 *reta_conf,
176                         uint16_t reta_size);
177 static int ixgbe_dev_rss_reta_query(struct rte_eth_dev *dev,
178                         struct rte_eth_rss_reta_entry64 *reta_conf,
179                         uint16_t reta_size);
180 static void ixgbe_dev_link_status_print(struct rte_eth_dev *dev);
181 static int ixgbe_dev_lsc_interrupt_setup(struct rte_eth_dev *dev);
182 static int ixgbe_dev_interrupt_get_status(struct rte_eth_dev *dev);
183 static int ixgbe_dev_interrupt_action(struct rte_eth_dev *dev);
184 static void ixgbe_dev_interrupt_handler(struct rte_intr_handle *handle,
185                 void *param);
186 static void ixgbe_dev_interrupt_delayed_handler(void *param);
187 static void ixgbe_add_rar(struct rte_eth_dev *dev, struct ether_addr *mac_addr,
188                 uint32_t index, uint32_t pool);
189 static void ixgbe_remove_rar(struct rte_eth_dev *dev, uint32_t index);
190 static void ixgbe_dcb_init(struct ixgbe_hw *hw,struct ixgbe_dcb_config *dcb_config);
191
192 /* For Virtual Function support */
193 static int eth_ixgbevf_dev_init(struct rte_eth_dev *eth_dev);
194 static int  ixgbevf_dev_configure(struct rte_eth_dev *dev);
195 static int  ixgbevf_dev_start(struct rte_eth_dev *dev);
196 static void ixgbevf_dev_stop(struct rte_eth_dev *dev);
197 static void ixgbevf_dev_close(struct rte_eth_dev *dev);
198 static void ixgbevf_intr_disable(struct ixgbe_hw *hw);
199 static void ixgbevf_dev_stats_get(struct rte_eth_dev *dev,
200                 struct rte_eth_stats *stats);
201 static void ixgbevf_dev_stats_reset(struct rte_eth_dev *dev);
202 static int ixgbevf_vlan_filter_set(struct rte_eth_dev *dev,
203                 uint16_t vlan_id, int on);
204 static void ixgbevf_vlan_strip_queue_set(struct rte_eth_dev *dev,
205                 uint16_t queue, int on);
206 static void ixgbevf_vlan_offload_set(struct rte_eth_dev *dev, int mask);
207 static void ixgbevf_set_vfta_all(struct rte_eth_dev *dev, bool on);
208
209 /* For Eth VMDQ APIs support */
210 static int ixgbe_uc_hash_table_set(struct rte_eth_dev *dev, struct
211                 ether_addr* mac_addr,uint8_t on);
212 static int ixgbe_uc_all_hash_table_set(struct rte_eth_dev *dev,uint8_t on);
213 static int  ixgbe_set_pool_rx_mode(struct rte_eth_dev *dev,  uint16_t pool,
214                 uint16_t rx_mask, uint8_t on);
215 static int ixgbe_set_pool_rx(struct rte_eth_dev *dev,uint16_t pool,uint8_t on);
216 static int ixgbe_set_pool_tx(struct rte_eth_dev *dev,uint16_t pool,uint8_t on);
217 static int ixgbe_set_pool_vlan_filter(struct rte_eth_dev *dev, uint16_t vlan,
218                 uint64_t pool_mask,uint8_t vlan_on);
219 static int ixgbe_mirror_rule_set(struct rte_eth_dev *dev,
220                 struct rte_eth_mirror_conf *mirror_conf,
221                 uint8_t rule_id, uint8_t on);
222 static int ixgbe_mirror_rule_reset(struct rte_eth_dev *dev,
223                 uint8_t rule_id);
224
225 static int ixgbe_set_queue_rate_limit(struct rte_eth_dev *dev,
226                 uint16_t queue_idx, uint16_t tx_rate);
227 static int ixgbe_set_vf_rate_limit(struct rte_eth_dev *dev, uint16_t vf,
228                 uint16_t tx_rate, uint64_t q_msk);
229
230 static void ixgbevf_add_mac_addr(struct rte_eth_dev *dev,
231                                  struct ether_addr *mac_addr,
232                                  uint32_t index, uint32_t pool);
233 static void ixgbevf_remove_mac_addr(struct rte_eth_dev *dev, uint32_t index);
234 static int ixgbe_syn_filter_set(struct rte_eth_dev *dev,
235                         struct rte_eth_syn_filter *filter,
236                         bool add);
237 static int ixgbe_syn_filter_get(struct rte_eth_dev *dev,
238                         struct rte_eth_syn_filter *filter);
239 static int ixgbe_syn_filter_handle(struct rte_eth_dev *dev,
240                         enum rte_filter_op filter_op,
241                         void *arg);
242 static int ixgbe_add_5tuple_filter(struct rte_eth_dev *dev,
243                         struct ixgbe_5tuple_filter *filter);
244 static void ixgbe_remove_5tuple_filter(struct rte_eth_dev *dev,
245                         struct ixgbe_5tuple_filter *filter);
246 static int ixgbe_add_del_ntuple_filter(struct rte_eth_dev *dev,
247                         struct rte_eth_ntuple_filter *filter,
248                         bool add);
249 static int ixgbe_ntuple_filter_handle(struct rte_eth_dev *dev,
250                                 enum rte_filter_op filter_op,
251                                 void *arg);
252 static int ixgbe_get_ntuple_filter(struct rte_eth_dev *dev,
253                         struct rte_eth_ntuple_filter *filter);
254 static int ixgbe_add_del_ethertype_filter(struct rte_eth_dev *dev,
255                         struct rte_eth_ethertype_filter *filter,
256                         bool add);
257 static int ixgbe_ethertype_filter_handle(struct rte_eth_dev *dev,
258                                 enum rte_filter_op filter_op,
259                                 void *arg);
260 static int ixgbe_get_ethertype_filter(struct rte_eth_dev *dev,
261                         struct rte_eth_ethertype_filter *filter);
262 static int ixgbe_dev_filter_ctrl(struct rte_eth_dev *dev,
263                      enum rte_filter_type filter_type,
264                      enum rte_filter_op filter_op,
265                      void *arg);
266 static int ixgbevf_dev_set_mtu(struct rte_eth_dev *dev, uint16_t mtu);
267
268 static int ixgbe_dev_set_mc_addr_list(struct rte_eth_dev *dev,
269                                       struct ether_addr *mc_addr_set,
270                                       uint32_t nb_mc_addr);
271
272 static int ixgbe_timesync_enable(struct rte_eth_dev *dev);
273 static int ixgbe_timesync_disable(struct rte_eth_dev *dev);
274 static int ixgbe_timesync_read_rx_timestamp(struct rte_eth_dev *dev,
275                                             struct timespec *timestamp,
276                                             uint32_t flags);
277 static int ixgbe_timesync_read_tx_timestamp(struct rte_eth_dev *dev,
278                                             struct timespec *timestamp);
279
280 /*
281  * Define VF Stats MACRO for Non "cleared on read" register
282  */
283 #define UPDATE_VF_STAT(reg, last, cur)                          \
284 {                                                               \
285         u32 latest = IXGBE_READ_REG(hw, reg);                   \
286         cur += latest - last;                                   \
287         last = latest;                                          \
288 }
289
290 #define UPDATE_VF_STAT_36BIT(lsb, msb, last, cur)                \
291 {                                                                \
292         u64 new_lsb = IXGBE_READ_REG(hw, lsb);                   \
293         u64 new_msb = IXGBE_READ_REG(hw, msb);                   \
294         u64 latest = ((new_msb << 32) | new_lsb);                \
295         cur += (0x1000000000LL + latest - last) & 0xFFFFFFFFFLL; \
296         last = latest;                                           \
297 }
298
299 #define IXGBE_SET_HWSTRIP(h, q) do{\
300                 uint32_t idx = (q) / (sizeof ((h)->bitmap[0]) * NBBY); \
301                 uint32_t bit = (q) % (sizeof ((h)->bitmap[0]) * NBBY); \
302                 (h)->bitmap[idx] |= 1 << bit;\
303         }while(0)
304
305 #define IXGBE_CLEAR_HWSTRIP(h, q) do{\
306                 uint32_t idx = (q) / (sizeof ((h)->bitmap[0]) * NBBY); \
307                 uint32_t bit = (q) % (sizeof ((h)->bitmap[0]) * NBBY); \
308                 (h)->bitmap[idx] &= ~(1 << bit);\
309         }while(0)
310
311 #define IXGBE_GET_HWSTRIP(h, q, r) do{\
312                 uint32_t idx = (q) / (sizeof ((h)->bitmap[0]) * NBBY); \
313                 uint32_t bit = (q) % (sizeof ((h)->bitmap[0]) * NBBY); \
314                 (r) = (h)->bitmap[idx] >> bit & 1;\
315         }while(0)
316
317 /*
318  * The set of PCI devices this driver supports
319  */
320 static const struct rte_pci_id pci_id_ixgbe_map[] = {
321
322 #define RTE_PCI_DEV_ID_DECL_IXGBE(vend, dev) {RTE_PCI_DEVICE(vend, dev)},
323 #include "rte_pci_dev_ids.h"
324
325 { .vendor_id = 0, /* sentinel */ },
326 };
327
328
329 /*
330  * The set of PCI devices this driver supports (for 82599 VF)
331  */
332 static const struct rte_pci_id pci_id_ixgbevf_map[] = {
333
334 #define RTE_PCI_DEV_ID_DECL_IXGBEVF(vend, dev) {RTE_PCI_DEVICE(vend, dev)},
335 #include "rte_pci_dev_ids.h"
336 { .vendor_id = 0, /* sentinel */ },
337
338 };
339
340 static const struct eth_dev_ops ixgbe_eth_dev_ops = {
341         .dev_configure        = ixgbe_dev_configure,
342         .dev_start            = ixgbe_dev_start,
343         .dev_stop             = ixgbe_dev_stop,
344         .dev_set_link_up    = ixgbe_dev_set_link_up,
345         .dev_set_link_down  = ixgbe_dev_set_link_down,
346         .dev_close            = ixgbe_dev_close,
347         .promiscuous_enable   = ixgbe_dev_promiscuous_enable,
348         .promiscuous_disable  = ixgbe_dev_promiscuous_disable,
349         .allmulticast_enable  = ixgbe_dev_allmulticast_enable,
350         .allmulticast_disable = ixgbe_dev_allmulticast_disable,
351         .link_update          = ixgbe_dev_link_update,
352         .stats_get            = ixgbe_dev_stats_get,
353         .stats_reset          = ixgbe_dev_stats_reset,
354         .queue_stats_mapping_set = ixgbe_dev_queue_stats_mapping_set,
355         .dev_infos_get        = ixgbe_dev_info_get,
356         .mtu_set              = ixgbe_dev_mtu_set,
357         .vlan_filter_set      = ixgbe_vlan_filter_set,
358         .vlan_tpid_set        = ixgbe_vlan_tpid_set,
359         .vlan_offload_set     = ixgbe_vlan_offload_set,
360         .vlan_strip_queue_set = ixgbe_vlan_strip_queue_set,
361         .rx_queue_start       = ixgbe_dev_rx_queue_start,
362         .rx_queue_stop        = ixgbe_dev_rx_queue_stop,
363         .tx_queue_start       = ixgbe_dev_tx_queue_start,
364         .tx_queue_stop        = ixgbe_dev_tx_queue_stop,
365         .rx_queue_setup       = ixgbe_dev_rx_queue_setup,
366         .rx_queue_release     = ixgbe_dev_rx_queue_release,
367         .rx_queue_count       = ixgbe_dev_rx_queue_count,
368         .rx_descriptor_done   = ixgbe_dev_rx_descriptor_done,
369         .tx_queue_setup       = ixgbe_dev_tx_queue_setup,
370         .tx_queue_release     = ixgbe_dev_tx_queue_release,
371         .dev_led_on           = ixgbe_dev_led_on,
372         .dev_led_off          = ixgbe_dev_led_off,
373         .flow_ctrl_get        = ixgbe_flow_ctrl_get,
374         .flow_ctrl_set        = ixgbe_flow_ctrl_set,
375         .priority_flow_ctrl_set = ixgbe_priority_flow_ctrl_set,
376         .mac_addr_add         = ixgbe_add_rar,
377         .mac_addr_remove      = ixgbe_remove_rar,
378         .uc_hash_table_set    = ixgbe_uc_hash_table_set,
379         .uc_all_hash_table_set  = ixgbe_uc_all_hash_table_set,
380         .mirror_rule_set      = ixgbe_mirror_rule_set,
381         .mirror_rule_reset    = ixgbe_mirror_rule_reset,
382         .set_vf_rx_mode       = ixgbe_set_pool_rx_mode,
383         .set_vf_rx            = ixgbe_set_pool_rx,
384         .set_vf_tx            = ixgbe_set_pool_tx,
385         .set_vf_vlan_filter   = ixgbe_set_pool_vlan_filter,
386         .set_queue_rate_limit = ixgbe_set_queue_rate_limit,
387         .set_vf_rate_limit    = ixgbe_set_vf_rate_limit,
388         .reta_update          = ixgbe_dev_rss_reta_update,
389         .reta_query           = ixgbe_dev_rss_reta_query,
390 #ifdef RTE_NIC_BYPASS
391         .bypass_init          = ixgbe_bypass_init,
392         .bypass_state_set     = ixgbe_bypass_state_store,
393         .bypass_state_show    = ixgbe_bypass_state_show,
394         .bypass_event_set     = ixgbe_bypass_event_store,
395         .bypass_event_show    = ixgbe_bypass_event_show,
396         .bypass_wd_timeout_set  = ixgbe_bypass_wd_timeout_store,
397         .bypass_wd_timeout_show = ixgbe_bypass_wd_timeout_show,
398         .bypass_ver_show      = ixgbe_bypass_ver_show,
399         .bypass_wd_reset      = ixgbe_bypass_wd_reset,
400 #endif /* RTE_NIC_BYPASS */
401         .rss_hash_update      = ixgbe_dev_rss_hash_update,
402         .rss_hash_conf_get    = ixgbe_dev_rss_hash_conf_get,
403         .filter_ctrl          = ixgbe_dev_filter_ctrl,
404         .set_mc_addr_list     = ixgbe_dev_set_mc_addr_list,
405         .timesync_enable      = ixgbe_timesync_enable,
406         .timesync_disable     = ixgbe_timesync_disable,
407         .timesync_read_rx_timestamp = ixgbe_timesync_read_rx_timestamp,
408         .timesync_read_tx_timestamp = ixgbe_timesync_read_tx_timestamp,
409 };
410
411 /*
412  * dev_ops for virtual function, bare necessities for basic vf
413  * operation have been implemented
414  */
415 static const struct eth_dev_ops ixgbevf_eth_dev_ops = {
416         .dev_configure        = ixgbevf_dev_configure,
417         .dev_start            = ixgbevf_dev_start,
418         .dev_stop             = ixgbevf_dev_stop,
419         .link_update          = ixgbe_dev_link_update,
420         .stats_get            = ixgbevf_dev_stats_get,
421         .stats_reset          = ixgbevf_dev_stats_reset,
422         .dev_close            = ixgbevf_dev_close,
423         .dev_infos_get        = ixgbevf_dev_info_get,
424         .mtu_set              = ixgbevf_dev_set_mtu,
425         .vlan_filter_set      = ixgbevf_vlan_filter_set,
426         .vlan_strip_queue_set = ixgbevf_vlan_strip_queue_set,
427         .vlan_offload_set     = ixgbevf_vlan_offload_set,
428         .rx_queue_setup       = ixgbe_dev_rx_queue_setup,
429         .rx_queue_release     = ixgbe_dev_rx_queue_release,
430         .tx_queue_setup       = ixgbe_dev_tx_queue_setup,
431         .tx_queue_release     = ixgbe_dev_tx_queue_release,
432         .mac_addr_add         = ixgbevf_add_mac_addr,
433         .mac_addr_remove      = ixgbevf_remove_mac_addr,
434         .set_mc_addr_list     = ixgbe_dev_set_mc_addr_list,
435 };
436
437 /**
438  * Atomically reads the link status information from global
439  * structure rte_eth_dev.
440  *
441  * @param dev
442  *   - Pointer to the structure rte_eth_dev to read from.
443  *   - Pointer to the buffer to be saved with the link status.
444  *
445  * @return
446  *   - On success, zero.
447  *   - On failure, negative value.
448  */
449 static inline int
450 rte_ixgbe_dev_atomic_read_link_status(struct rte_eth_dev *dev,
451                                 struct rte_eth_link *link)
452 {
453         struct rte_eth_link *dst = link;
454         struct rte_eth_link *src = &(dev->data->dev_link);
455
456         if (rte_atomic64_cmpset((uint64_t *)dst, *(uint64_t *)dst,
457                                         *(uint64_t *)src) == 0)
458                 return -1;
459
460         return 0;
461 }
462
463 /**
464  * Atomically writes the link status information into global
465  * structure rte_eth_dev.
466  *
467  * @param dev
468  *   - Pointer to the structure rte_eth_dev to read from.
469  *   - Pointer to the buffer to be saved with the link status.
470  *
471  * @return
472  *   - On success, zero.
473  *   - On failure, negative value.
474  */
475 static inline int
476 rte_ixgbe_dev_atomic_write_link_status(struct rte_eth_dev *dev,
477                                 struct rte_eth_link *link)
478 {
479         struct rte_eth_link *dst = &(dev->data->dev_link);
480         struct rte_eth_link *src = link;
481
482         if (rte_atomic64_cmpset((uint64_t *)dst, *(uint64_t *)dst,
483                                         *(uint64_t *)src) == 0)
484                 return -1;
485
486         return 0;
487 }
488
489 /*
490  * This function is the same as ixgbe_is_sfp() in base/ixgbe.h.
491  */
492 static inline int
493 ixgbe_is_sfp(struct ixgbe_hw *hw)
494 {
495         switch (hw->phy.type) {
496         case ixgbe_phy_sfp_avago:
497         case ixgbe_phy_sfp_ftl:
498         case ixgbe_phy_sfp_intel:
499         case ixgbe_phy_sfp_unknown:
500         case ixgbe_phy_sfp_passive_tyco:
501         case ixgbe_phy_sfp_passive_unknown:
502                 return 1;
503         default:
504                 return 0;
505         }
506 }
507
508 static inline int32_t
509 ixgbe_pf_reset_hw(struct ixgbe_hw *hw)
510 {
511         uint32_t ctrl_ext;
512         int32_t status;
513
514         status = ixgbe_reset_hw(hw);
515
516         ctrl_ext = IXGBE_READ_REG(hw, IXGBE_CTRL_EXT);
517         /* Set PF Reset Done bit so PF/VF Mail Ops can work */
518         ctrl_ext |= IXGBE_CTRL_EXT_PFRSTD;
519         IXGBE_WRITE_REG(hw, IXGBE_CTRL_EXT, ctrl_ext);
520         IXGBE_WRITE_FLUSH(hw);
521
522         return status;
523 }
524
525 static inline void
526 ixgbe_enable_intr(struct rte_eth_dev *dev)
527 {
528         struct ixgbe_interrupt *intr =
529                 IXGBE_DEV_PRIVATE_TO_INTR(dev->data->dev_private);
530         struct ixgbe_hw *hw =
531                 IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
532
533         IXGBE_WRITE_REG(hw, IXGBE_EIMS, intr->mask);
534         IXGBE_WRITE_FLUSH(hw);
535 }
536
537 /*
538  * This function is based on ixgbe_disable_intr() in base/ixgbe.h.
539  */
540 static void
541 ixgbe_disable_intr(struct ixgbe_hw *hw)
542 {
543         PMD_INIT_FUNC_TRACE();
544
545         if (hw->mac.type == ixgbe_mac_82598EB) {
546                 IXGBE_WRITE_REG(hw, IXGBE_EIMC, ~0);
547         } else {
548                 IXGBE_WRITE_REG(hw, IXGBE_EIMC, 0xFFFF0000);
549                 IXGBE_WRITE_REG(hw, IXGBE_EIMC_EX(0), ~0);
550                 IXGBE_WRITE_REG(hw, IXGBE_EIMC_EX(1), ~0);
551         }
552         IXGBE_WRITE_FLUSH(hw);
553 }
554
555 /*
556  * This function resets queue statistics mapping registers.
557  * From Niantic datasheet, Initialization of Statistics section:
558  * "...if software requires the queue counters, the RQSMR and TQSM registers
559  * must be re-programmed following a device reset.
560  */
561 static void
562 ixgbe_reset_qstat_mappings(struct ixgbe_hw *hw)
563 {
564         uint32_t i;
565
566         for(i = 0; i != IXGBE_NB_STAT_MAPPING_REGS; i++) {
567                 IXGBE_WRITE_REG(hw, IXGBE_RQSMR(i), 0);
568                 IXGBE_WRITE_REG(hw, IXGBE_TQSM(i), 0);
569         }
570 }
571
572
573 static int
574 ixgbe_dev_queue_stats_mapping_set(struct rte_eth_dev *eth_dev,
575                                   uint16_t queue_id,
576                                   uint8_t stat_idx,
577                                   uint8_t is_rx)
578 {
579 #define QSM_REG_NB_BITS_PER_QMAP_FIELD 8
580 #define NB_QMAP_FIELDS_PER_QSM_REG 4
581 #define QMAP_FIELD_RESERVED_BITS_MASK 0x0f
582
583         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(eth_dev->data->dev_private);
584         struct ixgbe_stat_mapping_registers *stat_mappings =
585                 IXGBE_DEV_PRIVATE_TO_STAT_MAPPINGS(eth_dev->data->dev_private);
586         uint32_t qsmr_mask = 0;
587         uint32_t clearing_mask = QMAP_FIELD_RESERVED_BITS_MASK;
588         uint32_t q_map;
589         uint8_t n, offset;
590
591         if ((hw->mac.type != ixgbe_mac_82599EB) &&
592                 (hw->mac.type != ixgbe_mac_X540) &&
593                 (hw->mac.type != ixgbe_mac_X550) &&
594                 (hw->mac.type != ixgbe_mac_X550EM_x))
595                 return -ENOSYS;
596
597         PMD_INIT_LOG(INFO, "Setting port %d, %s queue_id %d to stat index %d",
598                      (int)(eth_dev->data->port_id), is_rx ? "RX" : "TX",
599                      queue_id, stat_idx);
600
601         n = (uint8_t)(queue_id / NB_QMAP_FIELDS_PER_QSM_REG);
602         if (n >= IXGBE_NB_STAT_MAPPING_REGS) {
603                 PMD_INIT_LOG(ERR, "Nb of stat mapping registers exceeded");
604                 return -EIO;
605         }
606         offset = (uint8_t)(queue_id % NB_QMAP_FIELDS_PER_QSM_REG);
607
608         /* Now clear any previous stat_idx set */
609         clearing_mask <<= (QSM_REG_NB_BITS_PER_QMAP_FIELD * offset);
610         if (!is_rx)
611                 stat_mappings->tqsm[n] &= ~clearing_mask;
612         else
613                 stat_mappings->rqsmr[n] &= ~clearing_mask;
614
615         q_map = (uint32_t)stat_idx;
616         q_map &= QMAP_FIELD_RESERVED_BITS_MASK;
617         qsmr_mask = q_map << (QSM_REG_NB_BITS_PER_QMAP_FIELD * offset);
618         if (!is_rx)
619                 stat_mappings->tqsm[n] |= qsmr_mask;
620         else
621                 stat_mappings->rqsmr[n] |= qsmr_mask;
622
623         PMD_INIT_LOG(INFO, "Set port %d, %s queue_id %d to stat index %d",
624                      (int)(eth_dev->data->port_id), is_rx ? "RX" : "TX",
625                      queue_id, stat_idx);
626         PMD_INIT_LOG(INFO, "%s[%d] = 0x%08x", is_rx ? "RQSMR" : "TQSM", n,
627                      is_rx ? stat_mappings->rqsmr[n] : stat_mappings->tqsm[n]);
628
629         /* Now write the mapping in the appropriate register */
630         if (is_rx) {
631                 PMD_INIT_LOG(INFO, "Write 0x%x to RX IXGBE stat mapping reg:%d",
632                              stat_mappings->rqsmr[n], n);
633                 IXGBE_WRITE_REG(hw, IXGBE_RQSMR(n), stat_mappings->rqsmr[n]);
634         }
635         else {
636                 PMD_INIT_LOG(INFO, "Write 0x%x to TX IXGBE stat mapping reg:%d",
637                              stat_mappings->tqsm[n], n);
638                 IXGBE_WRITE_REG(hw, IXGBE_TQSM(n), stat_mappings->tqsm[n]);
639         }
640         return 0;
641 }
642
643 static void
644 ixgbe_restore_statistics_mapping(struct rte_eth_dev * dev)
645 {
646         struct ixgbe_stat_mapping_registers *stat_mappings =
647                 IXGBE_DEV_PRIVATE_TO_STAT_MAPPINGS(dev->data->dev_private);
648         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
649         int i;
650
651         /* write whatever was in stat mapping table to the NIC */
652         for (i = 0; i < IXGBE_NB_STAT_MAPPING_REGS; i++) {
653                 /* rx */
654                 IXGBE_WRITE_REG(hw, IXGBE_RQSMR(i), stat_mappings->rqsmr[i]);
655
656                 /* tx */
657                 IXGBE_WRITE_REG(hw, IXGBE_TQSM(i), stat_mappings->tqsm[i]);
658         }
659 }
660
661 static void
662 ixgbe_dcb_init(struct ixgbe_hw *hw,struct ixgbe_dcb_config *dcb_config)
663 {
664         uint8_t i;
665         struct ixgbe_dcb_tc_config *tc;
666         uint8_t dcb_max_tc = IXGBE_DCB_MAX_TRAFFIC_CLASS;
667
668         dcb_config->num_tcs.pg_tcs = dcb_max_tc;
669         dcb_config->num_tcs.pfc_tcs = dcb_max_tc;
670         for (i = 0; i < dcb_max_tc; i++) {
671                 tc = &dcb_config->tc_config[i];
672                 tc->path[IXGBE_DCB_TX_CONFIG].bwg_id = i;
673                 tc->path[IXGBE_DCB_TX_CONFIG].bwg_percent =
674                                  (uint8_t)(100/dcb_max_tc + (i & 1));
675                 tc->path[IXGBE_DCB_RX_CONFIG].bwg_id = i;
676                 tc->path[IXGBE_DCB_RX_CONFIG].bwg_percent =
677                                  (uint8_t)(100/dcb_max_tc + (i & 1));
678                 tc->pfc = ixgbe_dcb_pfc_disabled;
679         }
680
681         /* Initialize default user to priority mapping, UPx->TC0 */
682         tc = &dcb_config->tc_config[0];
683         tc->path[IXGBE_DCB_TX_CONFIG].up_to_tc_bitmap = 0xFF;
684         tc->path[IXGBE_DCB_RX_CONFIG].up_to_tc_bitmap = 0xFF;
685         for (i = 0; i< IXGBE_DCB_MAX_BW_GROUP; i++) {
686                 dcb_config->bw_percentage[IXGBE_DCB_TX_CONFIG][i] = 100;
687                 dcb_config->bw_percentage[IXGBE_DCB_RX_CONFIG][i] = 100;
688         }
689         dcb_config->rx_pba_cfg = ixgbe_dcb_pba_equal;
690         dcb_config->pfc_mode_enable = false;
691         dcb_config->vt_mode = true;
692         dcb_config->round_robin_enable = false;
693         /* support all DCB capabilities in 82599 */
694         dcb_config->support.capabilities = 0xFF;
695
696         /*we only support 4 Tcs for X540, X550 */
697         if (hw->mac.type == ixgbe_mac_X540 ||
698                 hw->mac.type == ixgbe_mac_X550 ||
699                 hw->mac.type == ixgbe_mac_X550EM_x) {
700                 dcb_config->num_tcs.pg_tcs = 4;
701                 dcb_config->num_tcs.pfc_tcs = 4;
702         }
703 }
704
705 /*
706  * Ensure that all locks are released before first NVM or PHY access
707  */
708 static void
709 ixgbe_swfw_lock_reset(struct ixgbe_hw *hw)
710 {
711         uint16_t mask;
712
713         /*
714          * Phy lock should not fail in this early stage. If this is the case,
715          * it is due to an improper exit of the application.
716          * So force the release of the faulty lock. Release of common lock
717          * is done automatically by swfw_sync function.
718          */
719         mask = IXGBE_GSSR_PHY0_SM << hw->bus.func;
720         if (ixgbe_acquire_swfw_semaphore(hw, mask) < 0) {
721                 PMD_DRV_LOG(DEBUG, "SWFW phy%d lock released", hw->bus.func);
722         }
723         ixgbe_release_swfw_semaphore(hw, mask);
724
725         /*
726          * These ones are more tricky since they are common to all ports; but
727          * swfw_sync retries last long enough (1s) to be almost sure that if
728          * lock can not be taken it is due to an improper lock of the
729          * semaphore.
730          */
731         mask = IXGBE_GSSR_EEP_SM | IXGBE_GSSR_MAC_CSR_SM | IXGBE_GSSR_SW_MNG_SM;
732         if (ixgbe_acquire_swfw_semaphore(hw, mask) < 0) {
733                 PMD_DRV_LOG(DEBUG, "SWFW common locks released");
734         }
735         ixgbe_release_swfw_semaphore(hw, mask);
736 }
737
738 /*
739  * This function is based on code in ixgbe_attach() in base/ixgbe.c.
740  * It returns 0 on success.
741  */
742 static int
743 eth_ixgbe_dev_init(struct rte_eth_dev *eth_dev)
744 {
745         struct rte_pci_device *pci_dev;
746         struct ixgbe_hw *hw =
747                 IXGBE_DEV_PRIVATE_TO_HW(eth_dev->data->dev_private);
748         struct ixgbe_vfta * shadow_vfta =
749                 IXGBE_DEV_PRIVATE_TO_VFTA(eth_dev->data->dev_private);
750         struct ixgbe_hwstrip *hwstrip =
751                 IXGBE_DEV_PRIVATE_TO_HWSTRIP_BITMAP(eth_dev->data->dev_private);
752         struct ixgbe_dcb_config *dcb_config =
753                 IXGBE_DEV_PRIVATE_TO_DCB_CFG(eth_dev->data->dev_private);
754         struct ixgbe_filter_info *filter_info =
755                 IXGBE_DEV_PRIVATE_TO_FILTER_INFO(eth_dev->data->dev_private);
756         uint32_t ctrl_ext;
757         uint16_t csum;
758         int diag, i;
759
760         PMD_INIT_FUNC_TRACE();
761
762         eth_dev->dev_ops = &ixgbe_eth_dev_ops;
763         eth_dev->rx_pkt_burst = &ixgbe_recv_pkts;
764         eth_dev->tx_pkt_burst = &ixgbe_xmit_pkts;
765
766         /*
767          * For secondary processes, we don't initialise any further as primary
768          * has already done this work. Only check we don't need a different
769          * RX and TX function.
770          */
771         if (rte_eal_process_type() != RTE_PROC_PRIMARY){
772                 struct ixgbe_tx_queue *txq;
773                 /* TX queue function in primary, set by last queue initialized
774                  * Tx queue may not initialized by primary process */
775                 if (eth_dev->data->tx_queues) {
776                         txq = eth_dev->data->tx_queues[eth_dev->data->nb_tx_queues-1];
777                         ixgbe_set_tx_function(eth_dev, txq);
778                 } else {
779                         /* Use default TX function if we get here */
780                         PMD_INIT_LOG(INFO, "No TX queues configured yet. "
781                                            "Using default TX function.");
782                 }
783
784                 ixgbe_set_rx_function(eth_dev);
785
786                 return 0;
787         }
788         pci_dev = eth_dev->pci_dev;
789
790         /* Vendor and Device ID need to be set before init of shared code */
791         hw->device_id = pci_dev->id.device_id;
792         hw->vendor_id = pci_dev->id.vendor_id;
793         hw->hw_addr = (void *)pci_dev->mem_resource[0].addr;
794         hw->allow_unsupported_sfp = 1;
795
796         /* Initialize the shared code (base driver) */
797 #ifdef RTE_NIC_BYPASS
798         diag = ixgbe_bypass_init_shared_code(hw);
799 #else
800         diag = ixgbe_init_shared_code(hw);
801 #endif /* RTE_NIC_BYPASS */
802
803         if (diag != IXGBE_SUCCESS) {
804                 PMD_INIT_LOG(ERR, "Shared code init failed: %d", diag);
805                 return -EIO;
806         }
807
808         /* pick up the PCI bus settings for reporting later */
809         ixgbe_get_bus_info(hw);
810
811         /* Unlock any pending hardware semaphore */
812         ixgbe_swfw_lock_reset(hw);
813
814         /* Initialize DCB configuration*/
815         memset(dcb_config, 0, sizeof(struct ixgbe_dcb_config));
816         ixgbe_dcb_init(hw,dcb_config);
817         /* Get Hardware Flow Control setting */
818         hw->fc.requested_mode = ixgbe_fc_full;
819         hw->fc.current_mode = ixgbe_fc_full;
820         hw->fc.pause_time = IXGBE_FC_PAUSE;
821         for (i = 0; i < IXGBE_DCB_MAX_TRAFFIC_CLASS; i++) {
822                 hw->fc.low_water[i] = IXGBE_FC_LO;
823                 hw->fc.high_water[i] = IXGBE_FC_HI;
824         }
825         hw->fc.send_xon = 1;
826
827         /* Make sure we have a good EEPROM before we read from it */
828         diag = ixgbe_validate_eeprom_checksum(hw, &csum);
829         if (diag != IXGBE_SUCCESS) {
830                 PMD_INIT_LOG(ERR, "The EEPROM checksum is not valid: %d", diag);
831                 return -EIO;
832         }
833
834 #ifdef RTE_NIC_BYPASS
835         diag = ixgbe_bypass_init_hw(hw);
836 #else
837         diag = ixgbe_init_hw(hw);
838 #endif /* RTE_NIC_BYPASS */
839
840         /*
841          * Devices with copper phys will fail to initialise if ixgbe_init_hw()
842          * is called too soon after the kernel driver unbinding/binding occurs.
843          * The failure occurs in ixgbe_identify_phy_generic() for all devices,
844          * but for non-copper devies, ixgbe_identify_sfp_module_generic() is
845          * also called. See ixgbe_identify_phy_82599(). The reason for the
846          * failure is not known, and only occuts when virtualisation features
847          * are disabled in the bios. A delay of 100ms  was found to be enough by
848          * trial-and-error, and is doubled to be safe.
849          */
850         if (diag && (hw->mac.ops.get_media_type(hw) == ixgbe_media_type_copper)) {
851                 rte_delay_ms(200);
852                 diag = ixgbe_init_hw(hw);
853         }
854
855         if (diag == IXGBE_ERR_EEPROM_VERSION) {
856                 PMD_INIT_LOG(ERR, "This device is a pre-production adapter/"
857                     "LOM.  Please be aware there may be issues associated "
858                     "with your hardware.");
859                 PMD_INIT_LOG(ERR, "If you are experiencing problems "
860                     "please contact your Intel or hardware representative "
861                     "who provided you with this hardware.");
862         } else if (diag == IXGBE_ERR_SFP_NOT_SUPPORTED)
863                 PMD_INIT_LOG(ERR, "Unsupported SFP+ Module");
864         if (diag) {
865                 PMD_INIT_LOG(ERR, "Hardware Initialization Failure: %d", diag);
866                 return -EIO;
867         }
868
869         /* Reset the hw statistics */
870         ixgbe_dev_stats_reset(eth_dev);
871
872         /* disable interrupt */
873         ixgbe_disable_intr(hw);
874
875         /* reset mappings for queue statistics hw counters*/
876         ixgbe_reset_qstat_mappings(hw);
877
878         /* Allocate memory for storing MAC addresses */
879         eth_dev->data->mac_addrs = rte_zmalloc("ixgbe", ETHER_ADDR_LEN *
880                         hw->mac.num_rar_entries, 0);
881         if (eth_dev->data->mac_addrs == NULL) {
882                 PMD_INIT_LOG(ERR,
883                         "Failed to allocate %u bytes needed to store "
884                         "MAC addresses",
885                         ETHER_ADDR_LEN * hw->mac.num_rar_entries);
886                 return -ENOMEM;
887         }
888         /* Copy the permanent MAC address */
889         ether_addr_copy((struct ether_addr *) hw->mac.perm_addr,
890                         &eth_dev->data->mac_addrs[0]);
891
892         /* Allocate memory for storing hash filter MAC addresses */
893         eth_dev->data->hash_mac_addrs = rte_zmalloc("ixgbe", ETHER_ADDR_LEN *
894                         IXGBE_VMDQ_NUM_UC_MAC, 0);
895         if (eth_dev->data->hash_mac_addrs == NULL) {
896                 PMD_INIT_LOG(ERR,
897                         "Failed to allocate %d bytes needed to store MAC addresses",
898                         ETHER_ADDR_LEN * IXGBE_VMDQ_NUM_UC_MAC);
899                 return -ENOMEM;
900         }
901
902         /* initialize the vfta */
903         memset(shadow_vfta, 0, sizeof(*shadow_vfta));
904
905         /* initialize the hw strip bitmap*/
906         memset(hwstrip, 0, sizeof(*hwstrip));
907
908         /* initialize PF if max_vfs not zero */
909         ixgbe_pf_host_init(eth_dev);
910
911         ctrl_ext = IXGBE_READ_REG(hw, IXGBE_CTRL_EXT);
912         /* let hardware know driver is loaded */
913         ctrl_ext |= IXGBE_CTRL_EXT_DRV_LOAD;
914         /* Set PF Reset Done bit so PF/VF Mail Ops can work */
915         ctrl_ext |= IXGBE_CTRL_EXT_PFRSTD;
916         IXGBE_WRITE_REG(hw, IXGBE_CTRL_EXT, ctrl_ext);
917         IXGBE_WRITE_FLUSH(hw);
918
919         if (ixgbe_is_sfp(hw) && hw->phy.sfp_type != ixgbe_sfp_type_not_present)
920                 PMD_INIT_LOG(DEBUG, "MAC: %d, PHY: %d, SFP+: %d",
921                              (int) hw->mac.type, (int) hw->phy.type,
922                              (int) hw->phy.sfp_type);
923         else
924                 PMD_INIT_LOG(DEBUG, "MAC: %d, PHY: %d",
925                              (int) hw->mac.type, (int) hw->phy.type);
926
927         PMD_INIT_LOG(DEBUG, "port %d vendorID=0x%x deviceID=0x%x",
928                         eth_dev->data->port_id, pci_dev->id.vendor_id,
929                         pci_dev->id.device_id);
930
931         rte_intr_callback_register(&(pci_dev->intr_handle),
932                 ixgbe_dev_interrupt_handler, (void *)eth_dev);
933
934         /* enable uio intr after callback register */
935         rte_intr_enable(&(pci_dev->intr_handle));
936
937         /* enable support intr */
938         ixgbe_enable_intr(eth_dev);
939
940         /* initialize 5tuple filter list */
941         TAILQ_INIT(&filter_info->fivetuple_list);
942         memset(filter_info->fivetuple_mask, 0,
943                 sizeof(uint32_t) * IXGBE_5TUPLE_ARRAY_SIZE);
944
945         return 0;
946 }
947
948
949 /*
950  * Negotiate mailbox API version with the PF.
951  * After reset API version is always set to the basic one (ixgbe_mbox_api_10).
952  * Then we try to negotiate starting with the most recent one.
953  * If all negotiation attempts fail, then we will proceed with
954  * the default one (ixgbe_mbox_api_10).
955  */
956 static void
957 ixgbevf_negotiate_api(struct ixgbe_hw *hw)
958 {
959         int32_t i;
960
961         /* start with highest supported, proceed down */
962         static const enum ixgbe_pfvf_api_rev sup_ver[] = {
963                 ixgbe_mbox_api_11,
964                 ixgbe_mbox_api_10,
965         };
966
967         for (i = 0;
968                         i != RTE_DIM(sup_ver) &&
969                         ixgbevf_negotiate_api_version(hw, sup_ver[i]) != 0;
970                         i++)
971                 ;
972 }
973
974 static void
975 generate_random_mac_addr(struct ether_addr *mac_addr)
976 {
977         uint64_t random;
978
979         /* Set Organizationally Unique Identifier (OUI) prefix. */
980         mac_addr->addr_bytes[0] = 0x00;
981         mac_addr->addr_bytes[1] = 0x09;
982         mac_addr->addr_bytes[2] = 0xC0;
983         /* Force indication of locally assigned MAC address. */
984         mac_addr->addr_bytes[0] |= ETHER_LOCAL_ADMIN_ADDR;
985         /* Generate the last 3 bytes of the MAC address with a random number. */
986         random = rte_rand();
987         memcpy(&mac_addr->addr_bytes[3], &random, 3);
988 }
989
990 /*
991  * Virtual Function device init
992  */
993 static int
994 eth_ixgbevf_dev_init(struct rte_eth_dev *eth_dev)
995 {
996         int diag;
997         uint32_t tc, tcs;
998         struct rte_pci_device *pci_dev;
999         struct ixgbe_hw *hw =
1000                 IXGBE_DEV_PRIVATE_TO_HW(eth_dev->data->dev_private);
1001         struct ixgbe_vfta * shadow_vfta =
1002                 IXGBE_DEV_PRIVATE_TO_VFTA(eth_dev->data->dev_private);
1003         struct ixgbe_hwstrip *hwstrip =
1004                 IXGBE_DEV_PRIVATE_TO_HWSTRIP_BITMAP(eth_dev->data->dev_private);
1005         struct ether_addr *perm_addr = (struct ether_addr *) hw->mac.perm_addr;
1006
1007         PMD_INIT_FUNC_TRACE();
1008
1009         eth_dev->dev_ops = &ixgbevf_eth_dev_ops;
1010         eth_dev->rx_pkt_burst = &ixgbe_recv_pkts;
1011         eth_dev->tx_pkt_burst = &ixgbe_xmit_pkts;
1012
1013         /* for secondary processes, we don't initialise any further as primary
1014          * has already done this work. Only check we don't need a different
1015          * RX function */
1016         if (rte_eal_process_type() != RTE_PROC_PRIMARY){
1017                 if (eth_dev->data->scattered_rx)
1018                         eth_dev->rx_pkt_burst = ixgbe_recv_pkts_lro_single_alloc;
1019                 return 0;
1020         }
1021
1022         pci_dev = eth_dev->pci_dev;
1023
1024         hw->device_id = pci_dev->id.device_id;
1025         hw->vendor_id = pci_dev->id.vendor_id;
1026         hw->hw_addr = (void *)pci_dev->mem_resource[0].addr;
1027
1028         /* initialize the vfta */
1029         memset(shadow_vfta, 0, sizeof(*shadow_vfta));
1030
1031         /* initialize the hw strip bitmap*/
1032         memset(hwstrip, 0, sizeof(*hwstrip));
1033
1034         /* Initialize the shared code (base driver) */
1035         diag = ixgbe_init_shared_code(hw);
1036         if (diag != IXGBE_SUCCESS) {
1037                 PMD_INIT_LOG(ERR, "Shared code init failed for ixgbevf: %d", diag);
1038                 return -EIO;
1039         }
1040
1041         /* init_mailbox_params */
1042         hw->mbx.ops.init_params(hw);
1043
1044         /* Reset the hw statistics */
1045         ixgbevf_dev_stats_reset(eth_dev);
1046
1047         /* Disable the interrupts for VF */
1048         ixgbevf_intr_disable(hw);
1049
1050         hw->mac.num_rar_entries = 128; /* The MAX of the underlying PF */
1051         diag = hw->mac.ops.reset_hw(hw);
1052
1053         /*
1054          * The VF reset operation returns the IXGBE_ERR_INVALID_MAC_ADDR when
1055          * the underlying PF driver has not assigned a MAC address to the VF.
1056          * In this case, assign a random MAC address.
1057          */
1058         if ((diag != IXGBE_SUCCESS) && (diag != IXGBE_ERR_INVALID_MAC_ADDR)) {
1059                 PMD_INIT_LOG(ERR, "VF Initialization Failure: %d", diag);
1060                 return (diag);
1061         }
1062
1063         /* negotiate mailbox API version to use with the PF. */
1064         ixgbevf_negotiate_api(hw);
1065
1066         /* Get Rx/Tx queue count via mailbox, which is ready after reset_hw */
1067         ixgbevf_get_queues(hw, &tcs, &tc);
1068
1069         /* Allocate memory for storing MAC addresses */
1070         eth_dev->data->mac_addrs = rte_zmalloc("ixgbevf", ETHER_ADDR_LEN *
1071                         hw->mac.num_rar_entries, 0);
1072         if (eth_dev->data->mac_addrs == NULL) {
1073                 PMD_INIT_LOG(ERR,
1074                         "Failed to allocate %u bytes needed to store "
1075                         "MAC addresses",
1076                         ETHER_ADDR_LEN * hw->mac.num_rar_entries);
1077                 return -ENOMEM;
1078         }
1079
1080         /* Generate a random MAC address, if none was assigned by PF. */
1081         if (is_zero_ether_addr(perm_addr)) {
1082                 generate_random_mac_addr(perm_addr);
1083                 diag = ixgbe_set_rar_vf(hw, 1, perm_addr->addr_bytes, 0, 1);
1084                 if (diag) {
1085                         rte_free(eth_dev->data->mac_addrs);
1086                         eth_dev->data->mac_addrs = NULL;
1087                         return diag;
1088                 }
1089                 PMD_INIT_LOG(INFO, "\tVF MAC address not assigned by Host PF");
1090                 PMD_INIT_LOG(INFO, "\tAssign randomly generated MAC address "
1091                              "%02x:%02x:%02x:%02x:%02x:%02x",
1092                              perm_addr->addr_bytes[0],
1093                              perm_addr->addr_bytes[1],
1094                              perm_addr->addr_bytes[2],
1095                              perm_addr->addr_bytes[3],
1096                              perm_addr->addr_bytes[4],
1097                              perm_addr->addr_bytes[5]);
1098         }
1099
1100         /* Copy the permanent MAC address */
1101         ether_addr_copy(perm_addr, &eth_dev->data->mac_addrs[0]);
1102
1103         /* reset the hardware with the new settings */
1104         diag = hw->mac.ops.start_hw(hw);
1105         switch (diag) {
1106                 case  0:
1107                         break;
1108
1109                 default:
1110                         PMD_INIT_LOG(ERR, "VF Initialization Failure: %d", diag);
1111                         return (-EIO);
1112         }
1113
1114         PMD_INIT_LOG(DEBUG, "port %d vendorID=0x%x deviceID=0x%x mac.type=%s",
1115                      eth_dev->data->port_id, pci_dev->id.vendor_id,
1116                      pci_dev->id.device_id, "ixgbe_mac_82599_vf");
1117
1118         return 0;
1119 }
1120
1121 static struct eth_driver rte_ixgbe_pmd = {
1122         .pci_drv = {
1123                 .name = "rte_ixgbe_pmd",
1124                 .id_table = pci_id_ixgbe_map,
1125                 .drv_flags = RTE_PCI_DRV_NEED_MAPPING | RTE_PCI_DRV_INTR_LSC,
1126         },
1127         .eth_dev_init = eth_ixgbe_dev_init,
1128         .dev_private_size = sizeof(struct ixgbe_adapter),
1129 };
1130
1131 /*
1132  * virtual function driver struct
1133  */
1134 static struct eth_driver rte_ixgbevf_pmd = {
1135         .pci_drv = {
1136                 .name = "rte_ixgbevf_pmd",
1137                 .id_table = pci_id_ixgbevf_map,
1138                 .drv_flags = RTE_PCI_DRV_NEED_MAPPING,
1139         },
1140         .eth_dev_init = eth_ixgbevf_dev_init,
1141         .dev_private_size = sizeof(struct ixgbe_adapter),
1142 };
1143
1144 /*
1145  * Driver initialization routine.
1146  * Invoked once at EAL init time.
1147  * Register itself as the [Poll Mode] Driver of PCI IXGBE devices.
1148  */
1149 static int
1150 rte_ixgbe_pmd_init(const char *name __rte_unused, const char *params __rte_unused)
1151 {
1152         PMD_INIT_FUNC_TRACE();
1153
1154         rte_eth_driver_register(&rte_ixgbe_pmd);
1155         return 0;
1156 }
1157
1158 /*
1159  * VF Driver initialization routine.
1160  * Invoked one at EAL init time.
1161  * Register itself as the [Virtual Poll Mode] Driver of PCI niantic devices.
1162  */
1163 static int
1164 rte_ixgbevf_pmd_init(const char *name __rte_unused, const char *param __rte_unused)
1165 {
1166         PMD_INIT_FUNC_TRACE();
1167
1168         rte_eth_driver_register(&rte_ixgbevf_pmd);
1169         return (0);
1170 }
1171
1172 static int
1173 ixgbe_vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
1174 {
1175         struct ixgbe_hw *hw =
1176                 IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1177         struct ixgbe_vfta * shadow_vfta =
1178                 IXGBE_DEV_PRIVATE_TO_VFTA(dev->data->dev_private);
1179         uint32_t vfta;
1180         uint32_t vid_idx;
1181         uint32_t vid_bit;
1182
1183         vid_idx = (uint32_t) ((vlan_id >> 5) & 0x7F);
1184         vid_bit = (uint32_t) (1 << (vlan_id & 0x1F));
1185         vfta = IXGBE_READ_REG(hw, IXGBE_VFTA(vid_idx));
1186         if (on)
1187                 vfta |= vid_bit;
1188         else
1189                 vfta &= ~vid_bit;
1190         IXGBE_WRITE_REG(hw, IXGBE_VFTA(vid_idx), vfta);
1191
1192         /* update local VFTA copy */
1193         shadow_vfta->vfta[vid_idx] = vfta;
1194
1195         return 0;
1196 }
1197
1198 static void
1199 ixgbe_vlan_strip_queue_set(struct rte_eth_dev *dev, uint16_t queue, int on)
1200 {
1201         if (on)
1202                 ixgbe_vlan_hw_strip_enable(dev, queue);
1203         else
1204                 ixgbe_vlan_hw_strip_disable(dev, queue);
1205 }
1206
1207 static void
1208 ixgbe_vlan_tpid_set(struct rte_eth_dev *dev, uint16_t tpid)
1209 {
1210         struct ixgbe_hw *hw =
1211                 IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1212
1213         /* Only the high 16-bits is valid */
1214         IXGBE_WRITE_REG(hw, IXGBE_EXVET, tpid << 16);
1215 }
1216
1217 void
1218 ixgbe_vlan_hw_filter_disable(struct rte_eth_dev *dev)
1219 {
1220         struct ixgbe_hw *hw =
1221                 IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1222         uint32_t vlnctrl;
1223
1224         PMD_INIT_FUNC_TRACE();
1225
1226         /* Filter Table Disable */
1227         vlnctrl = IXGBE_READ_REG(hw, IXGBE_VLNCTRL);
1228         vlnctrl &= ~IXGBE_VLNCTRL_VFE;
1229
1230         IXGBE_WRITE_REG(hw, IXGBE_VLNCTRL, vlnctrl);
1231 }
1232
1233 void
1234 ixgbe_vlan_hw_filter_enable(struct rte_eth_dev *dev)
1235 {
1236         struct ixgbe_hw *hw =
1237                 IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1238         struct ixgbe_vfta * shadow_vfta =
1239                 IXGBE_DEV_PRIVATE_TO_VFTA(dev->data->dev_private);
1240         uint32_t vlnctrl;
1241         uint16_t i;
1242
1243         PMD_INIT_FUNC_TRACE();
1244
1245         /* Filter Table Enable */
1246         vlnctrl = IXGBE_READ_REG(hw, IXGBE_VLNCTRL);
1247         vlnctrl &= ~IXGBE_VLNCTRL_CFIEN;
1248         vlnctrl |= IXGBE_VLNCTRL_VFE;
1249
1250         IXGBE_WRITE_REG(hw, IXGBE_VLNCTRL, vlnctrl);
1251
1252         /* write whatever is in local vfta copy */
1253         for (i = 0; i < IXGBE_VFTA_SIZE; i++)
1254                 IXGBE_WRITE_REG(hw, IXGBE_VFTA(i), shadow_vfta->vfta[i]);
1255 }
1256
1257 static void
1258 ixgbe_vlan_hw_strip_bitmap_set(struct rte_eth_dev *dev, uint16_t queue, bool on)
1259 {
1260         struct ixgbe_hwstrip *hwstrip =
1261                 IXGBE_DEV_PRIVATE_TO_HWSTRIP_BITMAP(dev->data->dev_private);
1262
1263         if(queue >= IXGBE_MAX_RX_QUEUE_NUM)
1264                 return;
1265
1266         if (on)
1267                 IXGBE_SET_HWSTRIP(hwstrip, queue);
1268         else
1269                 IXGBE_CLEAR_HWSTRIP(hwstrip, queue);
1270 }
1271
1272 static void
1273 ixgbe_vlan_hw_strip_disable(struct rte_eth_dev *dev, uint16_t queue)
1274 {
1275         struct ixgbe_hw *hw =
1276                 IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1277         uint32_t ctrl;
1278
1279         PMD_INIT_FUNC_TRACE();
1280
1281         if (hw->mac.type == ixgbe_mac_82598EB) {
1282                 /* No queue level support */
1283                 PMD_INIT_LOG(INFO, "82598EB not support queue level hw strip");
1284                 return;
1285         }
1286         else {
1287                 /* Other 10G NIC, the VLAN strip can be setup per queue in RXDCTL */
1288                 ctrl = IXGBE_READ_REG(hw, IXGBE_RXDCTL(queue));
1289                 ctrl &= ~IXGBE_RXDCTL_VME;
1290                 IXGBE_WRITE_REG(hw, IXGBE_RXDCTL(queue), ctrl);
1291         }
1292         /* record those setting for HW strip per queue */
1293         ixgbe_vlan_hw_strip_bitmap_set(dev, queue, 0);
1294 }
1295
1296 static void
1297 ixgbe_vlan_hw_strip_enable(struct rte_eth_dev *dev, uint16_t queue)
1298 {
1299         struct ixgbe_hw *hw =
1300                 IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1301         uint32_t ctrl;
1302
1303         PMD_INIT_FUNC_TRACE();
1304
1305         if (hw->mac.type == ixgbe_mac_82598EB) {
1306                 /* No queue level supported */
1307                 PMD_INIT_LOG(INFO, "82598EB not support queue level hw strip");
1308                 return;
1309         }
1310         else {
1311                 /* Other 10G NIC, the VLAN strip can be setup per queue in RXDCTL */
1312                 ctrl = IXGBE_READ_REG(hw, IXGBE_RXDCTL(queue));
1313                 ctrl |= IXGBE_RXDCTL_VME;
1314                 IXGBE_WRITE_REG(hw, IXGBE_RXDCTL(queue), ctrl);
1315         }
1316         /* record those setting for HW strip per queue */
1317         ixgbe_vlan_hw_strip_bitmap_set(dev, queue, 1);
1318 }
1319
1320 void
1321 ixgbe_vlan_hw_strip_disable_all(struct rte_eth_dev *dev)
1322 {
1323         struct ixgbe_hw *hw =
1324                 IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1325         uint32_t ctrl;
1326         uint16_t i;
1327
1328         PMD_INIT_FUNC_TRACE();
1329
1330         if (hw->mac.type == ixgbe_mac_82598EB) {
1331                 ctrl = IXGBE_READ_REG(hw, IXGBE_VLNCTRL);
1332                 ctrl &= ~IXGBE_VLNCTRL_VME;
1333                 IXGBE_WRITE_REG(hw, IXGBE_VLNCTRL, ctrl);
1334         }
1335         else {
1336                 /* Other 10G NIC, the VLAN strip can be setup per queue in RXDCTL */
1337                 for (i = 0; i < dev->data->nb_rx_queues; i++) {
1338                         ctrl = IXGBE_READ_REG(hw, IXGBE_RXDCTL(i));
1339                         ctrl &= ~IXGBE_RXDCTL_VME;
1340                         IXGBE_WRITE_REG(hw, IXGBE_RXDCTL(i), ctrl);
1341
1342                         /* record those setting for HW strip per queue */
1343                         ixgbe_vlan_hw_strip_bitmap_set(dev, i, 0);
1344                 }
1345         }
1346 }
1347
1348 void
1349 ixgbe_vlan_hw_strip_enable_all(struct rte_eth_dev *dev)
1350 {
1351         struct ixgbe_hw *hw =
1352                 IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1353         uint32_t ctrl;
1354         uint16_t i;
1355
1356         PMD_INIT_FUNC_TRACE();
1357
1358         if (hw->mac.type == ixgbe_mac_82598EB) {
1359                 ctrl = IXGBE_READ_REG(hw, IXGBE_VLNCTRL);
1360                 ctrl |= IXGBE_VLNCTRL_VME;
1361                 IXGBE_WRITE_REG(hw, IXGBE_VLNCTRL, ctrl);
1362         }
1363         else {
1364                 /* Other 10G NIC, the VLAN strip can be setup per queue in RXDCTL */
1365                 for (i = 0; i < dev->data->nb_rx_queues; i++) {
1366                         ctrl = IXGBE_READ_REG(hw, IXGBE_RXDCTL(i));
1367                         ctrl |= IXGBE_RXDCTL_VME;
1368                         IXGBE_WRITE_REG(hw, IXGBE_RXDCTL(i), ctrl);
1369
1370                         /* record those setting for HW strip per queue */
1371                         ixgbe_vlan_hw_strip_bitmap_set(dev, i, 1);
1372                 }
1373         }
1374 }
1375
1376 static void
1377 ixgbe_vlan_hw_extend_disable(struct rte_eth_dev *dev)
1378 {
1379         struct ixgbe_hw *hw =
1380                 IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1381         uint32_t ctrl;
1382
1383         PMD_INIT_FUNC_TRACE();
1384
1385         /* DMATXCTRL: Geric Double VLAN Disable */
1386         ctrl = IXGBE_READ_REG(hw, IXGBE_DMATXCTL);
1387         ctrl &= ~IXGBE_DMATXCTL_GDV;
1388         IXGBE_WRITE_REG(hw, IXGBE_DMATXCTL, ctrl);
1389
1390         /* CTRL_EXT: Global Double VLAN Disable */
1391         ctrl = IXGBE_READ_REG(hw, IXGBE_CTRL_EXT);
1392         ctrl &= ~IXGBE_EXTENDED_VLAN;
1393         IXGBE_WRITE_REG(hw, IXGBE_CTRL_EXT, ctrl);
1394
1395 }
1396
1397 static void
1398 ixgbe_vlan_hw_extend_enable(struct rte_eth_dev *dev)
1399 {
1400         struct ixgbe_hw *hw =
1401                 IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1402         uint32_t ctrl;
1403
1404         PMD_INIT_FUNC_TRACE();
1405
1406         /* DMATXCTRL: Geric Double VLAN Enable */
1407         ctrl  = IXGBE_READ_REG(hw, IXGBE_DMATXCTL);
1408         ctrl |= IXGBE_DMATXCTL_GDV;
1409         IXGBE_WRITE_REG(hw, IXGBE_DMATXCTL, ctrl);
1410
1411         /* CTRL_EXT: Global Double VLAN Enable */
1412         ctrl  = IXGBE_READ_REG(hw, IXGBE_CTRL_EXT);
1413         ctrl |= IXGBE_EXTENDED_VLAN;
1414         IXGBE_WRITE_REG(hw, IXGBE_CTRL_EXT, ctrl);
1415
1416         /*
1417          * VET EXT field in the EXVET register = 0x8100 by default
1418          * So no need to change. Same to VT field of DMATXCTL register
1419          */
1420 }
1421
1422 static void
1423 ixgbe_vlan_offload_set(struct rte_eth_dev *dev, int mask)
1424 {
1425         if(mask & ETH_VLAN_STRIP_MASK){
1426                 if (dev->data->dev_conf.rxmode.hw_vlan_strip)
1427                         ixgbe_vlan_hw_strip_enable_all(dev);
1428                 else
1429                         ixgbe_vlan_hw_strip_disable_all(dev);
1430         }
1431
1432         if(mask & ETH_VLAN_FILTER_MASK){
1433                 if (dev->data->dev_conf.rxmode.hw_vlan_filter)
1434                         ixgbe_vlan_hw_filter_enable(dev);
1435                 else
1436                         ixgbe_vlan_hw_filter_disable(dev);
1437         }
1438
1439         if(mask & ETH_VLAN_EXTEND_MASK){
1440                 if (dev->data->dev_conf.rxmode.hw_vlan_extend)
1441                         ixgbe_vlan_hw_extend_enable(dev);
1442                 else
1443                         ixgbe_vlan_hw_extend_disable(dev);
1444         }
1445 }
1446
1447 static void
1448 ixgbe_vmdq_vlan_hw_filter_enable(struct rte_eth_dev *dev)
1449 {
1450         struct ixgbe_hw *hw =
1451                 IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1452         /* VLNCTRL: enable vlan filtering and allow all vlan tags through */
1453         uint32_t vlanctrl = IXGBE_READ_REG(hw, IXGBE_VLNCTRL);
1454         vlanctrl |= IXGBE_VLNCTRL_VFE ; /* enable vlan filters */
1455         IXGBE_WRITE_REG(hw, IXGBE_VLNCTRL, vlanctrl);
1456 }
1457
1458 static int
1459 ixgbe_dev_configure(struct rte_eth_dev *dev)
1460 {
1461         struct ixgbe_interrupt *intr =
1462                 IXGBE_DEV_PRIVATE_TO_INTR(dev->data->dev_private);
1463         struct ixgbe_adapter *adapter =
1464                 (struct ixgbe_adapter *)dev->data->dev_private;
1465
1466         PMD_INIT_FUNC_TRACE();
1467
1468         /* set flag to update link status after init */
1469         intr->flags |= IXGBE_FLAG_NEED_LINK_UPDATE;
1470
1471         /*
1472          * Initialize to TRUE. If any of Rx queues doesn't meet the bulk
1473          * allocation or vector Rx preconditions we will reset it.
1474          */
1475         adapter->rx_bulk_alloc_allowed = true;
1476         adapter->rx_vec_allowed = true;
1477
1478         return 0;
1479 }
1480
1481 /*
1482  * Configure device link speed and setup link.
1483  * It returns 0 on success.
1484  */
1485 static int
1486 ixgbe_dev_start(struct rte_eth_dev *dev)
1487 {
1488         struct ixgbe_hw *hw =
1489                 IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1490         struct ixgbe_vf_info *vfinfo =
1491                 *IXGBE_DEV_PRIVATE_TO_P_VFDATA(dev->data->dev_private);
1492         int err, link_up = 0, negotiate = 0;
1493         uint32_t speed = 0;
1494         int mask = 0;
1495         int status;
1496         uint16_t vf, idx;
1497
1498         PMD_INIT_FUNC_TRACE();
1499
1500         /* IXGBE devices don't support half duplex */
1501         if ((dev->data->dev_conf.link_duplex != ETH_LINK_AUTONEG_DUPLEX) &&
1502                         (dev->data->dev_conf.link_duplex != ETH_LINK_FULL_DUPLEX)) {
1503                 PMD_INIT_LOG(ERR, "Invalid link_duplex (%hu) for port %hhu",
1504                              dev->data->dev_conf.link_duplex,
1505                              dev->data->port_id);
1506                 return -EINVAL;
1507         }
1508
1509         /* stop adapter */
1510         hw->adapter_stopped = FALSE;
1511         ixgbe_stop_adapter(hw);
1512
1513         /* reinitialize adapter
1514          * this calls reset and start */
1515         status = ixgbe_pf_reset_hw(hw);
1516         if (status != 0)
1517                 return -1;
1518         hw->mac.ops.start_hw(hw);
1519         hw->mac.get_link_status = true;
1520
1521         /* configure PF module if SRIOV enabled */
1522         ixgbe_pf_host_configure(dev);
1523
1524         /* initialize transmission unit */
1525         ixgbe_dev_tx_init(dev);
1526
1527         /* This can fail when allocating mbufs for descriptor rings */
1528         err = ixgbe_dev_rx_init(dev);
1529         if (err) {
1530                 PMD_INIT_LOG(ERR, "Unable to initialize RX hardware");
1531                 goto error;
1532         }
1533
1534         err = ixgbe_dev_rxtx_start(dev);
1535         if (err < 0) {
1536                 PMD_INIT_LOG(ERR, "Unable to start rxtx queues");
1537                 goto error;
1538         }
1539
1540         /* Skip link setup if loopback mode is enabled for 82599. */
1541         if (hw->mac.type == ixgbe_mac_82599EB &&
1542                         dev->data->dev_conf.lpbk_mode == IXGBE_LPBK_82599_TX_RX)
1543                 goto skip_link_setup;
1544
1545         if (ixgbe_is_sfp(hw) && hw->phy.multispeed_fiber) {
1546                 err = hw->mac.ops.setup_sfp(hw);
1547                 if (err)
1548                         goto error;
1549         }
1550
1551         if (hw->mac.ops.get_media_type(hw) == ixgbe_media_type_copper) {
1552                 /* Turn on the copper */
1553                 ixgbe_set_phy_power(hw, true);
1554         } else {
1555                 /* Turn on the laser */
1556                 ixgbe_enable_tx_laser(hw);
1557         }
1558
1559         err = ixgbe_check_link(hw, &speed, &link_up, 0);
1560         if (err)
1561                 goto error;
1562         dev->data->dev_link.link_status = link_up;
1563
1564         err = ixgbe_get_link_capabilities(hw, &speed, &negotiate);
1565         if (err)
1566                 goto error;
1567
1568         switch(dev->data->dev_conf.link_speed) {
1569         case ETH_LINK_SPEED_AUTONEG:
1570                 speed = (hw->mac.type != ixgbe_mac_82598EB) ?
1571                                 IXGBE_LINK_SPEED_82599_AUTONEG :
1572                                 IXGBE_LINK_SPEED_82598_AUTONEG;
1573                 break;
1574         case ETH_LINK_SPEED_100:
1575                 /*
1576                  * Invalid for 82598 but error will be detected by
1577                  * ixgbe_setup_link()
1578                  */
1579                 speed = IXGBE_LINK_SPEED_100_FULL;
1580                 break;
1581         case ETH_LINK_SPEED_1000:
1582                 speed = IXGBE_LINK_SPEED_1GB_FULL;
1583                 break;
1584         case ETH_LINK_SPEED_10000:
1585                 speed = IXGBE_LINK_SPEED_10GB_FULL;
1586                 break;
1587         default:
1588                 PMD_INIT_LOG(ERR, "Invalid link_speed (%hu) for port %hhu",
1589                              dev->data->dev_conf.link_speed,
1590                              dev->data->port_id);
1591                 goto error;
1592         }
1593
1594         err = ixgbe_setup_link(hw, speed, link_up);
1595         if (err)
1596                 goto error;
1597
1598 skip_link_setup:
1599
1600         /* check if lsc interrupt is enabled */
1601         if (dev->data->dev_conf.intr_conf.lsc != 0)
1602                 ixgbe_dev_lsc_interrupt_setup(dev);
1603
1604         /* resume enabled intr since hw reset */
1605         ixgbe_enable_intr(dev);
1606
1607         mask = ETH_VLAN_STRIP_MASK | ETH_VLAN_FILTER_MASK | \
1608                 ETH_VLAN_EXTEND_MASK;
1609         ixgbe_vlan_offload_set(dev, mask);
1610
1611         if (dev->data->dev_conf.rxmode.mq_mode == ETH_MQ_RX_VMDQ_ONLY) {
1612                 /* Enable vlan filtering for VMDq */
1613                 ixgbe_vmdq_vlan_hw_filter_enable(dev);
1614         }
1615
1616         /* Configure DCB hw */
1617         ixgbe_configure_dcb(dev);
1618
1619         if (dev->data->dev_conf.fdir_conf.mode != RTE_FDIR_MODE_NONE) {
1620                 err = ixgbe_fdir_configure(dev);
1621                 if (err)
1622                         goto error;
1623         }
1624
1625         /* Restore vf rate limit */
1626         if (vfinfo != NULL) {
1627                 for (vf = 0; vf < dev->pci_dev->max_vfs; vf++)
1628                         for (idx = 0; idx < IXGBE_MAX_QUEUE_NUM_PER_VF; idx++)
1629                                 if (vfinfo[vf].tx_rate[idx] != 0)
1630                                         ixgbe_set_vf_rate_limit(dev, vf,
1631                                                 vfinfo[vf].tx_rate[idx],
1632                                                 1 << idx);
1633         }
1634
1635         ixgbe_restore_statistics_mapping(dev);
1636
1637         return (0);
1638
1639 error:
1640         PMD_INIT_LOG(ERR, "failure in ixgbe_dev_start(): %d", err);
1641         ixgbe_dev_clear_queues(dev);
1642         return -EIO;
1643 }
1644
1645 /*
1646  * Stop device: disable rx and tx functions to allow for reconfiguring.
1647  */
1648 static void
1649 ixgbe_dev_stop(struct rte_eth_dev *dev)
1650 {
1651         struct rte_eth_link link;
1652         struct ixgbe_hw *hw =
1653                 IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1654         struct ixgbe_vf_info *vfinfo =
1655                 *IXGBE_DEV_PRIVATE_TO_P_VFDATA(dev->data->dev_private);
1656         struct ixgbe_filter_info *filter_info =
1657                 IXGBE_DEV_PRIVATE_TO_FILTER_INFO(dev->data->dev_private);
1658         struct ixgbe_5tuple_filter *p_5tuple, *p_5tuple_next;
1659         int vf;
1660
1661         PMD_INIT_FUNC_TRACE();
1662
1663         /* disable interrupts */
1664         ixgbe_disable_intr(hw);
1665
1666         /* reset the NIC */
1667         ixgbe_pf_reset_hw(hw);
1668         hw->adapter_stopped = FALSE;
1669
1670         /* stop adapter */
1671         ixgbe_stop_adapter(hw);
1672
1673         for (vf = 0; vfinfo != NULL &&
1674                      vf < dev->pci_dev->max_vfs; vf++)
1675                 vfinfo[vf].clear_to_send = false;
1676
1677         if (hw->mac.ops.get_media_type(hw) == ixgbe_media_type_copper) {
1678                 /* Turn off the copper */
1679                 ixgbe_set_phy_power(hw, false);
1680         } else {
1681                 /* Turn off the laser */
1682                 ixgbe_disable_tx_laser(hw);
1683         }
1684
1685         ixgbe_dev_clear_queues(dev);
1686
1687         /* Clear stored conf */
1688         dev->data->scattered_rx = 0;
1689         dev->data->lro = 0;
1690
1691         /* Clear recorded link status */
1692         memset(&link, 0, sizeof(link));
1693         rte_ixgbe_dev_atomic_write_link_status(dev, &link);
1694
1695         /* Remove all ntuple filters of the device */
1696         for (p_5tuple = TAILQ_FIRST(&filter_info->fivetuple_list);
1697              p_5tuple != NULL; p_5tuple = p_5tuple_next) {
1698                 p_5tuple_next = TAILQ_NEXT(p_5tuple, entries);
1699                 TAILQ_REMOVE(&filter_info->fivetuple_list,
1700                              p_5tuple, entries);
1701                 rte_free(p_5tuple);
1702         }
1703         memset(filter_info->fivetuple_mask, 0,
1704                 sizeof(uint32_t) * IXGBE_5TUPLE_ARRAY_SIZE);
1705
1706 }
1707
1708 /*
1709  * Set device link up: enable tx.
1710  */
1711 static int
1712 ixgbe_dev_set_link_up(struct rte_eth_dev *dev)
1713 {
1714         struct ixgbe_hw *hw =
1715                 IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1716         if (hw->mac.type == ixgbe_mac_82599EB) {
1717 #ifdef RTE_NIC_BYPASS
1718                 if (hw->device_id == IXGBE_DEV_ID_82599_BYPASS) {
1719                         /* Not suported in bypass mode */
1720                         PMD_INIT_LOG(ERR, "Set link up is not supported "
1721                                      "by device id 0x%x", hw->device_id);
1722                         return -ENOTSUP;
1723                 }
1724 #endif
1725         }
1726
1727         if (hw->mac.ops.get_media_type(hw) == ixgbe_media_type_copper) {
1728                 /* Turn on the copper */
1729                 ixgbe_set_phy_power(hw, true);
1730         } else {
1731                 /* Turn on the laser */
1732                 ixgbe_enable_tx_laser(hw);
1733         }
1734
1735         return 0;
1736 }
1737
1738 /*
1739  * Set device link down: disable tx.
1740  */
1741 static int
1742 ixgbe_dev_set_link_down(struct rte_eth_dev *dev)
1743 {
1744         struct ixgbe_hw *hw =
1745                 IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1746         if (hw->mac.type == ixgbe_mac_82599EB) {
1747 #ifdef RTE_NIC_BYPASS
1748                 if (hw->device_id == IXGBE_DEV_ID_82599_BYPASS) {
1749                         /* Not suported in bypass mode */
1750                         PMD_INIT_LOG(ERR, "Set link down is not supported "
1751                                      "by device id 0x%x", hw->device_id);
1752                         return -ENOTSUP;
1753                 }
1754 #endif
1755         }
1756
1757         if (hw->mac.ops.get_media_type(hw) == ixgbe_media_type_copper) {
1758                 /* Turn off the copper */
1759                 ixgbe_set_phy_power(hw, false);
1760         } else {
1761                 /* Turn off the laser */
1762                 ixgbe_disable_tx_laser(hw);
1763         }
1764
1765         return 0;
1766 }
1767
1768 /*
1769  * Reest and stop device.
1770  */
1771 static void
1772 ixgbe_dev_close(struct rte_eth_dev *dev)
1773 {
1774         struct ixgbe_hw *hw =
1775                 IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1776
1777         PMD_INIT_FUNC_TRACE();
1778
1779         ixgbe_pf_reset_hw(hw);
1780
1781         ixgbe_dev_stop(dev);
1782         hw->adapter_stopped = 1;
1783
1784         ixgbe_disable_pcie_master(hw);
1785
1786         /* reprogram the RAR[0] in case user changed it. */
1787         ixgbe_set_rar(hw, 0, hw->mac.addr, 0, IXGBE_RAH_AV);
1788 }
1789
1790 /*
1791  * This function is based on ixgbe_update_stats_counters() in base/ixgbe.c
1792  */
1793 static void
1794 ixgbe_dev_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
1795 {
1796         struct ixgbe_hw *hw =
1797                         IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1798         struct ixgbe_hw_stats *hw_stats =
1799                         IXGBE_DEV_PRIVATE_TO_STATS(dev->data->dev_private);
1800         uint32_t bprc, lxon, lxoff, total;
1801         uint64_t total_missed_rx, total_qbrc, total_qprc;
1802         unsigned i;
1803
1804         total_missed_rx = 0;
1805         total_qbrc = 0;
1806         total_qprc = 0;
1807
1808         hw_stats->crcerrs += IXGBE_READ_REG(hw, IXGBE_CRCERRS);
1809         hw_stats->illerrc += IXGBE_READ_REG(hw, IXGBE_ILLERRC);
1810         hw_stats->errbc += IXGBE_READ_REG(hw, IXGBE_ERRBC);
1811         hw_stats->mspdc += IXGBE_READ_REG(hw, IXGBE_MSPDC);
1812
1813         for (i = 0; i < 8; i++) {
1814                 uint32_t mp;
1815                 mp = IXGBE_READ_REG(hw, IXGBE_MPC(i));
1816                 /* global total per queue */
1817                 hw_stats->mpc[i] += mp;
1818                 /* Running comprehensive total for stats display */
1819                 total_missed_rx += hw_stats->mpc[i];
1820                 if (hw->mac.type == ixgbe_mac_82598EB)
1821                         hw_stats->rnbc[i] +=
1822                             IXGBE_READ_REG(hw, IXGBE_RNBC(i));
1823                 hw_stats->pxontxc[i] +=
1824                     IXGBE_READ_REG(hw, IXGBE_PXONTXC(i));
1825                 hw_stats->pxonrxc[i] +=
1826                     IXGBE_READ_REG(hw, IXGBE_PXONRXC(i));
1827                 hw_stats->pxofftxc[i] +=
1828                     IXGBE_READ_REG(hw, IXGBE_PXOFFTXC(i));
1829                 hw_stats->pxoffrxc[i] +=
1830                     IXGBE_READ_REG(hw, IXGBE_PXOFFRXC(i));
1831                 hw_stats->pxon2offc[i] +=
1832                     IXGBE_READ_REG(hw, IXGBE_PXON2OFFCNT(i));
1833         }
1834         for (i = 0; i < IXGBE_QUEUE_STAT_COUNTERS; i++) {
1835                 hw_stats->qprc[i] += IXGBE_READ_REG(hw, IXGBE_QPRC(i));
1836                 hw_stats->qptc[i] += IXGBE_READ_REG(hw, IXGBE_QPTC(i));
1837                 hw_stats->qbrc[i] += IXGBE_READ_REG(hw, IXGBE_QBRC_L(i));
1838                 hw_stats->qbrc[i] +=
1839                     ((uint64_t)IXGBE_READ_REG(hw, IXGBE_QBRC_H(i)) << 32);
1840                 hw_stats->qbtc[i] += IXGBE_READ_REG(hw, IXGBE_QBTC_L(i));
1841                 hw_stats->qbtc[i] +=
1842                     ((uint64_t)IXGBE_READ_REG(hw, IXGBE_QBTC_H(i)) << 32);
1843                 hw_stats->qprdc[i] += IXGBE_READ_REG(hw, IXGBE_QPRDC(i));
1844
1845                 total_qprc += hw_stats->qprc[i];
1846                 total_qbrc += hw_stats->qbrc[i];
1847         }
1848         hw_stats->mlfc += IXGBE_READ_REG(hw, IXGBE_MLFC);
1849         hw_stats->mrfc += IXGBE_READ_REG(hw, IXGBE_MRFC);
1850         hw_stats->rlec += IXGBE_READ_REG(hw, IXGBE_RLEC);
1851
1852         /* Note that gprc counts missed packets */
1853         hw_stats->gprc += IXGBE_READ_REG(hw, IXGBE_GPRC);
1854
1855         if (hw->mac.type != ixgbe_mac_82598EB) {
1856                 hw_stats->gorc += IXGBE_READ_REG(hw, IXGBE_GORCL);
1857                 hw_stats->gorc += ((u64)IXGBE_READ_REG(hw, IXGBE_GORCH) << 32);
1858                 hw_stats->gotc += IXGBE_READ_REG(hw, IXGBE_GOTCL);
1859                 hw_stats->gotc += ((u64)IXGBE_READ_REG(hw, IXGBE_GOTCH) << 32);
1860                 hw_stats->tor += IXGBE_READ_REG(hw, IXGBE_TORL);
1861                 hw_stats->tor += ((u64)IXGBE_READ_REG(hw, IXGBE_TORH) << 32);
1862                 hw_stats->lxonrxc += IXGBE_READ_REG(hw, IXGBE_LXONRXCNT);
1863                 hw_stats->lxoffrxc += IXGBE_READ_REG(hw, IXGBE_LXOFFRXCNT);
1864         } else {
1865                 hw_stats->lxonrxc += IXGBE_READ_REG(hw, IXGBE_LXONRXC);
1866                 hw_stats->lxoffrxc += IXGBE_READ_REG(hw, IXGBE_LXOFFRXC);
1867                 /* 82598 only has a counter in the high register */
1868                 hw_stats->gorc += IXGBE_READ_REG(hw, IXGBE_GORCH);
1869                 hw_stats->gotc += IXGBE_READ_REG(hw, IXGBE_GOTCH);
1870                 hw_stats->tor += IXGBE_READ_REG(hw, IXGBE_TORH);
1871         }
1872
1873         /*
1874          * Workaround: mprc hardware is incorrectly counting
1875          * broadcasts, so for now we subtract those.
1876          */
1877         bprc = IXGBE_READ_REG(hw, IXGBE_BPRC);
1878         hw_stats->bprc += bprc;
1879         hw_stats->mprc += IXGBE_READ_REG(hw, IXGBE_MPRC);
1880         if (hw->mac.type == ixgbe_mac_82598EB)
1881                 hw_stats->mprc -= bprc;
1882
1883         hw_stats->prc64 += IXGBE_READ_REG(hw, IXGBE_PRC64);
1884         hw_stats->prc127 += IXGBE_READ_REG(hw, IXGBE_PRC127);
1885         hw_stats->prc255 += IXGBE_READ_REG(hw, IXGBE_PRC255);
1886         hw_stats->prc511 += IXGBE_READ_REG(hw, IXGBE_PRC511);
1887         hw_stats->prc1023 += IXGBE_READ_REG(hw, IXGBE_PRC1023);
1888         hw_stats->prc1522 += IXGBE_READ_REG(hw, IXGBE_PRC1522);
1889
1890         lxon = IXGBE_READ_REG(hw, IXGBE_LXONTXC);
1891         hw_stats->lxontxc += lxon;
1892         lxoff = IXGBE_READ_REG(hw, IXGBE_LXOFFTXC);
1893         hw_stats->lxofftxc += lxoff;
1894         total = lxon + lxoff;
1895
1896         hw_stats->gptc += IXGBE_READ_REG(hw, IXGBE_GPTC);
1897         hw_stats->mptc += IXGBE_READ_REG(hw, IXGBE_MPTC);
1898         hw_stats->ptc64 += IXGBE_READ_REG(hw, IXGBE_PTC64);
1899         hw_stats->gptc -= total;
1900         hw_stats->mptc -= total;
1901         hw_stats->ptc64 -= total;
1902         hw_stats->gotc -= total * ETHER_MIN_LEN;
1903
1904         hw_stats->ruc += IXGBE_READ_REG(hw, IXGBE_RUC);
1905         hw_stats->rfc += IXGBE_READ_REG(hw, IXGBE_RFC);
1906         hw_stats->roc += IXGBE_READ_REG(hw, IXGBE_ROC);
1907         hw_stats->rjc += IXGBE_READ_REG(hw, IXGBE_RJC);
1908         hw_stats->mngprc += IXGBE_READ_REG(hw, IXGBE_MNGPRC);
1909         hw_stats->mngpdc += IXGBE_READ_REG(hw, IXGBE_MNGPDC);
1910         hw_stats->mngptc += IXGBE_READ_REG(hw, IXGBE_MNGPTC);
1911         hw_stats->tpr += IXGBE_READ_REG(hw, IXGBE_TPR);
1912         hw_stats->tpt += IXGBE_READ_REG(hw, IXGBE_TPT);
1913         hw_stats->ptc127 += IXGBE_READ_REG(hw, IXGBE_PTC127);
1914         hw_stats->ptc255 += IXGBE_READ_REG(hw, IXGBE_PTC255);
1915         hw_stats->ptc511 += IXGBE_READ_REG(hw, IXGBE_PTC511);
1916         hw_stats->ptc1023 += IXGBE_READ_REG(hw, IXGBE_PTC1023);
1917         hw_stats->ptc1522 += IXGBE_READ_REG(hw, IXGBE_PTC1522);
1918         hw_stats->bptc += IXGBE_READ_REG(hw, IXGBE_BPTC);
1919         hw_stats->xec += IXGBE_READ_REG(hw, IXGBE_XEC);
1920         hw_stats->fccrc += IXGBE_READ_REG(hw, IXGBE_FCCRC);
1921         hw_stats->fclast += IXGBE_READ_REG(hw, IXGBE_FCLAST);
1922         /* Only read FCOE on 82599 */
1923         if (hw->mac.type != ixgbe_mac_82598EB) {
1924                 hw_stats->fcoerpdc += IXGBE_READ_REG(hw, IXGBE_FCOERPDC);
1925                 hw_stats->fcoeprc += IXGBE_READ_REG(hw, IXGBE_FCOEPRC);
1926                 hw_stats->fcoeptc += IXGBE_READ_REG(hw, IXGBE_FCOEPTC);
1927                 hw_stats->fcoedwrc += IXGBE_READ_REG(hw, IXGBE_FCOEDWRC);
1928                 hw_stats->fcoedwtc += IXGBE_READ_REG(hw, IXGBE_FCOEDWTC);
1929         }
1930
1931         if (stats == NULL)
1932                 return;
1933
1934         /* Fill out the rte_eth_stats statistics structure */
1935         stats->ipackets = total_qprc;
1936         stats->ibytes = total_qbrc;
1937         stats->opackets = hw_stats->gptc;
1938         stats->obytes = hw_stats->gotc;
1939         stats->imcasts = hw_stats->mprc;
1940
1941         for (i = 0; i < IXGBE_QUEUE_STAT_COUNTERS; i++) {
1942                 stats->q_ipackets[i] = hw_stats->qprc[i];
1943                 stats->q_opackets[i] = hw_stats->qptc[i];
1944                 stats->q_ibytes[i] = hw_stats->qbrc[i];
1945                 stats->q_obytes[i] = hw_stats->qbtc[i];
1946                 stats->q_errors[i] = hw_stats->qprdc[i];
1947         }
1948
1949         /* Rx Errors */
1950         stats->ibadcrc  = hw_stats->crcerrs;
1951         stats->ibadlen  = hw_stats->rlec + hw_stats->ruc + hw_stats->roc;
1952         stats->imissed  = total_missed_rx;
1953         stats->ierrors  = stats->ibadcrc +
1954                           stats->ibadlen +
1955                           stats->imissed +
1956                           hw_stats->illerrc + hw_stats->errbc;
1957
1958         /* Tx Errors */
1959         stats->oerrors  = 0;
1960
1961         /* XON/XOFF pause frames */
1962         stats->tx_pause_xon  = hw_stats->lxontxc;
1963         stats->rx_pause_xon  = hw_stats->lxonrxc;
1964         stats->tx_pause_xoff = hw_stats->lxofftxc;
1965         stats->rx_pause_xoff = hw_stats->lxoffrxc;
1966
1967         /* Flow Director Stats registers */
1968         hw_stats->fdirmatch += IXGBE_READ_REG(hw, IXGBE_FDIRMATCH);
1969         hw_stats->fdirmiss += IXGBE_READ_REG(hw, IXGBE_FDIRMISS);
1970         stats->fdirmatch = hw_stats->fdirmatch;
1971         stats->fdirmiss = hw_stats->fdirmiss;
1972 }
1973
1974 static void
1975 ixgbe_dev_stats_reset(struct rte_eth_dev *dev)
1976 {
1977         struct ixgbe_hw_stats *stats =
1978                         IXGBE_DEV_PRIVATE_TO_STATS(dev->data->dev_private);
1979
1980         /* HW registers are cleared on read */
1981         ixgbe_dev_stats_get(dev, NULL);
1982
1983         /* Reset software totals */
1984         memset(stats, 0, sizeof(*stats));
1985 }
1986
1987 static void
1988 ixgbevf_dev_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
1989 {
1990         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1991         struct ixgbevf_hw_stats *hw_stats = (struct ixgbevf_hw_stats*)
1992                           IXGBE_DEV_PRIVATE_TO_STATS(dev->data->dev_private);
1993
1994         /* Good Rx packet, include VF loopback */
1995         UPDATE_VF_STAT(IXGBE_VFGPRC,
1996             hw_stats->last_vfgprc, hw_stats->vfgprc);
1997
1998         /* Good Rx octets, include VF loopback */
1999         UPDATE_VF_STAT_36BIT(IXGBE_VFGORC_LSB, IXGBE_VFGORC_MSB,
2000             hw_stats->last_vfgorc, hw_stats->vfgorc);
2001
2002         /* Good Tx packet, include VF loopback */
2003         UPDATE_VF_STAT(IXGBE_VFGPTC,
2004             hw_stats->last_vfgptc, hw_stats->vfgptc);
2005
2006         /* Good Tx octets, include VF loopback */
2007         UPDATE_VF_STAT_36BIT(IXGBE_VFGOTC_LSB, IXGBE_VFGOTC_MSB,
2008             hw_stats->last_vfgotc, hw_stats->vfgotc);
2009
2010         /* Rx Multicst Packet */
2011         UPDATE_VF_STAT(IXGBE_VFMPRC,
2012             hw_stats->last_vfmprc, hw_stats->vfmprc);
2013
2014         if (stats == NULL)
2015                 return;
2016
2017         stats->ipackets = hw_stats->vfgprc;
2018         stats->ibytes = hw_stats->vfgorc;
2019         stats->opackets = hw_stats->vfgptc;
2020         stats->obytes = hw_stats->vfgotc;
2021         stats->imcasts = hw_stats->vfmprc;
2022 }
2023
2024 static void
2025 ixgbevf_dev_stats_reset(struct rte_eth_dev *dev)
2026 {
2027         struct ixgbevf_hw_stats *hw_stats = (struct ixgbevf_hw_stats*)
2028                         IXGBE_DEV_PRIVATE_TO_STATS(dev->data->dev_private);
2029
2030         /* Sync HW register to the last stats */
2031         ixgbevf_dev_stats_get(dev, NULL);
2032
2033         /* reset HW current stats*/
2034         hw_stats->vfgprc = 0;
2035         hw_stats->vfgorc = 0;
2036         hw_stats->vfgptc = 0;
2037         hw_stats->vfgotc = 0;
2038         hw_stats->vfmprc = 0;
2039
2040 }
2041
2042 static void
2043 ixgbe_dev_info_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *dev_info)
2044 {
2045         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2046
2047         dev_info->max_rx_queues = (uint16_t)hw->mac.max_rx_queues;
2048         dev_info->max_tx_queues = (uint16_t)hw->mac.max_tx_queues;
2049         dev_info->min_rx_bufsize = 1024; /* cf BSIZEPACKET in SRRCTL register */
2050         dev_info->max_rx_pktlen = 15872; /* includes CRC, cf MAXFRS register */
2051         dev_info->max_mac_addrs = hw->mac.num_rar_entries;
2052         dev_info->max_hash_mac_addrs = IXGBE_VMDQ_NUM_UC_MAC;
2053         dev_info->max_vfs = dev->pci_dev->max_vfs;
2054         if (hw->mac.type == ixgbe_mac_82598EB)
2055                 dev_info->max_vmdq_pools = ETH_16_POOLS;
2056         else
2057                 dev_info->max_vmdq_pools = ETH_64_POOLS;
2058         dev_info->vmdq_queue_num = dev_info->max_rx_queues;
2059         dev_info->rx_offload_capa =
2060                 DEV_RX_OFFLOAD_VLAN_STRIP |
2061                 DEV_RX_OFFLOAD_IPV4_CKSUM |
2062                 DEV_RX_OFFLOAD_UDP_CKSUM  |
2063                 DEV_RX_OFFLOAD_TCP_CKSUM;
2064
2065         /*
2066          * RSC is only supported by 82599 and x540 PF devices in a non-SR-IOV
2067          * mode.
2068          */
2069         if ((hw->mac.type == ixgbe_mac_82599EB ||
2070              hw->mac.type == ixgbe_mac_X540) &&
2071             !RTE_ETH_DEV_SRIOV(dev).active)
2072                 dev_info->rx_offload_capa |= DEV_RX_OFFLOAD_TCP_LRO;
2073
2074         dev_info->tx_offload_capa =
2075                 DEV_TX_OFFLOAD_VLAN_INSERT |
2076                 DEV_TX_OFFLOAD_IPV4_CKSUM  |
2077                 DEV_TX_OFFLOAD_UDP_CKSUM   |
2078                 DEV_TX_OFFLOAD_TCP_CKSUM   |
2079                 DEV_TX_OFFLOAD_SCTP_CKSUM  |
2080                 DEV_TX_OFFLOAD_TCP_TSO;
2081
2082         dev_info->default_rxconf = (struct rte_eth_rxconf) {
2083                 .rx_thresh = {
2084                         .pthresh = IXGBE_DEFAULT_RX_PTHRESH,
2085                         .hthresh = IXGBE_DEFAULT_RX_HTHRESH,
2086                         .wthresh = IXGBE_DEFAULT_RX_WTHRESH,
2087                 },
2088                 .rx_free_thresh = IXGBE_DEFAULT_RX_FREE_THRESH,
2089                 .rx_drop_en = 0,
2090         };
2091
2092         dev_info->default_txconf = (struct rte_eth_txconf) {
2093                 .tx_thresh = {
2094                         .pthresh = IXGBE_DEFAULT_TX_PTHRESH,
2095                         .hthresh = IXGBE_DEFAULT_TX_HTHRESH,
2096                         .wthresh = IXGBE_DEFAULT_TX_WTHRESH,
2097                 },
2098                 .tx_free_thresh = IXGBE_DEFAULT_TX_FREE_THRESH,
2099                 .tx_rs_thresh = IXGBE_DEFAULT_TX_RSBIT_THRESH,
2100                 .txq_flags = ETH_TXQ_FLAGS_NOMULTSEGS |
2101                                 ETH_TXQ_FLAGS_NOOFFLOADS,
2102         };
2103         dev_info->hash_key_size = IXGBE_HKEY_MAX_INDEX * sizeof(uint32_t);
2104         dev_info->reta_size = ETH_RSS_RETA_SIZE_128;
2105         dev_info->flow_type_rss_offloads = IXGBE_RSS_OFFLOAD_ALL;
2106 }
2107
2108 static void
2109 ixgbevf_dev_info_get(struct rte_eth_dev *dev,
2110                      struct rte_eth_dev_info *dev_info)
2111 {
2112         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2113
2114         dev_info->max_rx_queues = (uint16_t)hw->mac.max_rx_queues;
2115         dev_info->max_tx_queues = (uint16_t)hw->mac.max_tx_queues;
2116         dev_info->min_rx_bufsize = 1024; /* cf BSIZEPACKET in SRRCTL reg */
2117         dev_info->max_rx_pktlen = 15872; /* includes CRC, cf MAXFRS reg */
2118         dev_info->max_mac_addrs = hw->mac.num_rar_entries;
2119         dev_info->max_hash_mac_addrs = IXGBE_VMDQ_NUM_UC_MAC;
2120         dev_info->max_vfs = dev->pci_dev->max_vfs;
2121         if (hw->mac.type == ixgbe_mac_82598EB)
2122                 dev_info->max_vmdq_pools = ETH_16_POOLS;
2123         else
2124                 dev_info->max_vmdq_pools = ETH_64_POOLS;
2125         dev_info->rx_offload_capa = DEV_RX_OFFLOAD_VLAN_STRIP |
2126                                 DEV_RX_OFFLOAD_IPV4_CKSUM |
2127                                 DEV_RX_OFFLOAD_UDP_CKSUM  |
2128                                 DEV_RX_OFFLOAD_TCP_CKSUM;
2129         dev_info->tx_offload_capa = DEV_TX_OFFLOAD_VLAN_INSERT |
2130                                 DEV_TX_OFFLOAD_IPV4_CKSUM  |
2131                                 DEV_TX_OFFLOAD_UDP_CKSUM   |
2132                                 DEV_TX_OFFLOAD_TCP_CKSUM   |
2133                                 DEV_TX_OFFLOAD_SCTP_CKSUM;
2134
2135         dev_info->default_rxconf = (struct rte_eth_rxconf) {
2136                 .rx_thresh = {
2137                         .pthresh = IXGBE_DEFAULT_RX_PTHRESH,
2138                         .hthresh = IXGBE_DEFAULT_RX_HTHRESH,
2139                         .wthresh = IXGBE_DEFAULT_RX_WTHRESH,
2140                 },
2141                 .rx_free_thresh = IXGBE_DEFAULT_RX_FREE_THRESH,
2142                 .rx_drop_en = 0,
2143         };
2144
2145         dev_info->default_txconf = (struct rte_eth_txconf) {
2146                 .tx_thresh = {
2147                         .pthresh = IXGBE_DEFAULT_TX_PTHRESH,
2148                         .hthresh = IXGBE_DEFAULT_TX_HTHRESH,
2149                         .wthresh = IXGBE_DEFAULT_TX_WTHRESH,
2150                 },
2151                 .tx_free_thresh = IXGBE_DEFAULT_TX_FREE_THRESH,
2152                 .tx_rs_thresh = IXGBE_DEFAULT_TX_RSBIT_THRESH,
2153                 .txq_flags = ETH_TXQ_FLAGS_NOMULTSEGS |
2154                                 ETH_TXQ_FLAGS_NOOFFLOADS,
2155         };
2156 }
2157
2158 /* return 0 means link status changed, -1 means not changed */
2159 static int
2160 ixgbe_dev_link_update(struct rte_eth_dev *dev, int wait_to_complete)
2161 {
2162         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2163         struct rte_eth_link link, old;
2164         ixgbe_link_speed link_speed = IXGBE_LINK_SPEED_UNKNOWN;
2165         int link_up;
2166         int diag;
2167
2168         link.link_status = 0;
2169         link.link_speed = 0;
2170         link.link_duplex = 0;
2171         memset(&old, 0, sizeof(old));
2172         rte_ixgbe_dev_atomic_read_link_status(dev, &old);
2173
2174         hw->mac.get_link_status = true;
2175
2176         /* check if it needs to wait to complete, if lsc interrupt is enabled */
2177         if (wait_to_complete == 0 || dev->data->dev_conf.intr_conf.lsc != 0)
2178                 diag = ixgbe_check_link(hw, &link_speed, &link_up, 0);
2179         else
2180                 diag = ixgbe_check_link(hw, &link_speed, &link_up, 1);
2181
2182         if (diag != 0) {
2183                 link.link_speed = ETH_LINK_SPEED_100;
2184                 link.link_duplex = ETH_LINK_HALF_DUPLEX;
2185                 rte_ixgbe_dev_atomic_write_link_status(dev, &link);
2186                 if (link.link_status == old.link_status)
2187                         return -1;
2188                 return 0;
2189         }
2190
2191         if (link_up == 0) {
2192                 rte_ixgbe_dev_atomic_write_link_status(dev, &link);
2193                 if (link.link_status == old.link_status)
2194                         return -1;
2195                 return 0;
2196         }
2197         link.link_status = 1;
2198         link.link_duplex = ETH_LINK_FULL_DUPLEX;
2199
2200         switch (link_speed) {
2201         default:
2202         case IXGBE_LINK_SPEED_UNKNOWN:
2203                 link.link_duplex = ETH_LINK_HALF_DUPLEX;
2204                 link.link_speed = ETH_LINK_SPEED_100;
2205                 break;
2206
2207         case IXGBE_LINK_SPEED_100_FULL:
2208                 link.link_speed = ETH_LINK_SPEED_100;
2209                 break;
2210
2211         case IXGBE_LINK_SPEED_1GB_FULL:
2212                 link.link_speed = ETH_LINK_SPEED_1000;
2213                 break;
2214
2215         case IXGBE_LINK_SPEED_10GB_FULL:
2216                 link.link_speed = ETH_LINK_SPEED_10000;
2217                 break;
2218         }
2219         rte_ixgbe_dev_atomic_write_link_status(dev, &link);
2220
2221         if (link.link_status == old.link_status)
2222                 return -1;
2223
2224         return 0;
2225 }
2226
2227 static void
2228 ixgbe_dev_promiscuous_enable(struct rte_eth_dev *dev)
2229 {
2230         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2231         uint32_t fctrl;
2232
2233         fctrl = IXGBE_READ_REG(hw, IXGBE_FCTRL);
2234         fctrl |= (IXGBE_FCTRL_UPE | IXGBE_FCTRL_MPE);
2235         IXGBE_WRITE_REG(hw, IXGBE_FCTRL, fctrl);
2236 }
2237
2238 static void
2239 ixgbe_dev_promiscuous_disable(struct rte_eth_dev *dev)
2240 {
2241         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2242         uint32_t fctrl;
2243
2244         fctrl = IXGBE_READ_REG(hw, IXGBE_FCTRL);
2245         fctrl &= (~IXGBE_FCTRL_UPE);
2246         if (dev->data->all_multicast == 1)
2247                 fctrl |= IXGBE_FCTRL_MPE;
2248         else
2249                 fctrl &= (~IXGBE_FCTRL_MPE);
2250         IXGBE_WRITE_REG(hw, IXGBE_FCTRL, fctrl);
2251 }
2252
2253 static void
2254 ixgbe_dev_allmulticast_enable(struct rte_eth_dev *dev)
2255 {
2256         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2257         uint32_t fctrl;
2258
2259         fctrl = IXGBE_READ_REG(hw, IXGBE_FCTRL);
2260         fctrl |= IXGBE_FCTRL_MPE;
2261         IXGBE_WRITE_REG(hw, IXGBE_FCTRL, fctrl);
2262 }
2263
2264 static void
2265 ixgbe_dev_allmulticast_disable(struct rte_eth_dev *dev)
2266 {
2267         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2268         uint32_t fctrl;
2269
2270         if (dev->data->promiscuous == 1)
2271                 return; /* must remain in all_multicast mode */
2272
2273         fctrl = IXGBE_READ_REG(hw, IXGBE_FCTRL);
2274         fctrl &= (~IXGBE_FCTRL_MPE);
2275         IXGBE_WRITE_REG(hw, IXGBE_FCTRL, fctrl);
2276 }
2277
2278 /**
2279  * It clears the interrupt causes and enables the interrupt.
2280  * It will be called once only during nic initialized.
2281  *
2282  * @param dev
2283  *  Pointer to struct rte_eth_dev.
2284  *
2285  * @return
2286  *  - On success, zero.
2287  *  - On failure, a negative value.
2288  */
2289 static int
2290 ixgbe_dev_lsc_interrupt_setup(struct rte_eth_dev *dev)
2291 {
2292         struct ixgbe_interrupt *intr =
2293                 IXGBE_DEV_PRIVATE_TO_INTR(dev->data->dev_private);
2294
2295         ixgbe_dev_link_status_print(dev);
2296         intr->mask |= IXGBE_EICR_LSC;
2297
2298         return 0;
2299 }
2300
2301 /*
2302  * It reads ICR and sets flag (IXGBE_EICR_LSC) for the link_update.
2303  *
2304  * @param dev
2305  *  Pointer to struct rte_eth_dev.
2306  *
2307  * @return
2308  *  - On success, zero.
2309  *  - On failure, a negative value.
2310  */
2311 static int
2312 ixgbe_dev_interrupt_get_status(struct rte_eth_dev *dev)
2313 {
2314         uint32_t eicr;
2315         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2316         struct ixgbe_interrupt *intr =
2317                 IXGBE_DEV_PRIVATE_TO_INTR(dev->data->dev_private);
2318
2319         /* clear all cause mask */
2320         ixgbe_disable_intr(hw);
2321
2322         /* read-on-clear nic registers here */
2323         eicr = IXGBE_READ_REG(hw, IXGBE_EICR);
2324         PMD_DRV_LOG(INFO, "eicr %x", eicr);
2325
2326         intr->flags = 0;
2327         if (eicr & IXGBE_EICR_LSC) {
2328                 /* set flag for async link update */
2329                 intr->flags |= IXGBE_FLAG_NEED_LINK_UPDATE;
2330         }
2331
2332         if (eicr & IXGBE_EICR_MAILBOX)
2333                 intr->flags |= IXGBE_FLAG_MAILBOX;
2334
2335         return 0;
2336 }
2337
2338 /**
2339  * It gets and then prints the link status.
2340  *
2341  * @param dev
2342  *  Pointer to struct rte_eth_dev.
2343  *
2344  * @return
2345  *  - On success, zero.
2346  *  - On failure, a negative value.
2347  */
2348 static void
2349 ixgbe_dev_link_status_print(struct rte_eth_dev *dev)
2350 {
2351         struct rte_eth_link link;
2352
2353         memset(&link, 0, sizeof(link));
2354         rte_ixgbe_dev_atomic_read_link_status(dev, &link);
2355         if (link.link_status) {
2356                 PMD_INIT_LOG(INFO, "Port %d: Link Up - speed %u Mbps - %s",
2357                                         (int)(dev->data->port_id),
2358                                         (unsigned)link.link_speed,
2359                         link.link_duplex == ETH_LINK_FULL_DUPLEX ?
2360                                         "full-duplex" : "half-duplex");
2361         } else {
2362                 PMD_INIT_LOG(INFO, " Port %d: Link Down",
2363                                 (int)(dev->data->port_id));
2364         }
2365         PMD_INIT_LOG(INFO, "PCI Address: %04d:%02d:%02d:%d",
2366                                 dev->pci_dev->addr.domain,
2367                                 dev->pci_dev->addr.bus,
2368                                 dev->pci_dev->addr.devid,
2369                                 dev->pci_dev->addr.function);
2370 }
2371
2372 /*
2373  * It executes link_update after knowing an interrupt occurred.
2374  *
2375  * @param dev
2376  *  Pointer to struct rte_eth_dev.
2377  *
2378  * @return
2379  *  - On success, zero.
2380  *  - On failure, a negative value.
2381  */
2382 static int
2383 ixgbe_dev_interrupt_action(struct rte_eth_dev *dev)
2384 {
2385         struct ixgbe_interrupt *intr =
2386                 IXGBE_DEV_PRIVATE_TO_INTR(dev->data->dev_private);
2387         int64_t timeout;
2388         struct rte_eth_link link;
2389         int intr_enable_delay = false;
2390
2391         PMD_DRV_LOG(DEBUG, "intr action type %d", intr->flags);
2392
2393         if (intr->flags & IXGBE_FLAG_MAILBOX) {
2394                 ixgbe_pf_mbx_process(dev);
2395                 intr->flags &= ~IXGBE_FLAG_MAILBOX;
2396         }
2397
2398         if (intr->flags & IXGBE_FLAG_NEED_LINK_UPDATE) {
2399                 /* get the link status before link update, for predicting later */
2400                 memset(&link, 0, sizeof(link));
2401                 rte_ixgbe_dev_atomic_read_link_status(dev, &link);
2402
2403                 ixgbe_dev_link_update(dev, 0);
2404
2405                 /* likely to up */
2406                 if (!link.link_status)
2407                         /* handle it 1 sec later, wait it being stable */
2408                         timeout = IXGBE_LINK_UP_CHECK_TIMEOUT;
2409                 /* likely to down */
2410                 else
2411                         /* handle it 4 sec later, wait it being stable */
2412                         timeout = IXGBE_LINK_DOWN_CHECK_TIMEOUT;
2413
2414                 ixgbe_dev_link_status_print(dev);
2415
2416                 intr_enable_delay = true;
2417         }
2418
2419         if (intr_enable_delay) {
2420                 if (rte_eal_alarm_set(timeout * 1000,
2421                                       ixgbe_dev_interrupt_delayed_handler, (void*)dev) < 0)
2422                         PMD_DRV_LOG(ERR, "Error setting alarm");
2423         } else {
2424                 PMD_DRV_LOG(DEBUG, "enable intr immediately");
2425                 ixgbe_enable_intr(dev);
2426                 rte_intr_enable(&(dev->pci_dev->intr_handle));
2427         }
2428
2429
2430         return 0;
2431 }
2432
2433 /**
2434  * Interrupt handler which shall be registered for alarm callback for delayed
2435  * handling specific interrupt to wait for the stable nic state. As the
2436  * NIC interrupt state is not stable for ixgbe after link is just down,
2437  * it needs to wait 4 seconds to get the stable status.
2438  *
2439  * @param handle
2440  *  Pointer to interrupt handle.
2441  * @param param
2442  *  The address of parameter (struct rte_eth_dev *) regsitered before.
2443  *
2444  * @return
2445  *  void
2446  */
2447 static void
2448 ixgbe_dev_interrupt_delayed_handler(void *param)
2449 {
2450         struct rte_eth_dev *dev = (struct rte_eth_dev *)param;
2451         struct ixgbe_interrupt *intr =
2452                 IXGBE_DEV_PRIVATE_TO_INTR(dev->data->dev_private);
2453         struct ixgbe_hw *hw =
2454                 IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2455         uint32_t eicr;
2456
2457         eicr = IXGBE_READ_REG(hw, IXGBE_EICR);
2458         if (eicr & IXGBE_EICR_MAILBOX)
2459                 ixgbe_pf_mbx_process(dev);
2460
2461         if (intr->flags & IXGBE_FLAG_NEED_LINK_UPDATE) {
2462                 ixgbe_dev_link_update(dev, 0);
2463                 intr->flags &= ~IXGBE_FLAG_NEED_LINK_UPDATE;
2464                 ixgbe_dev_link_status_print(dev);
2465                 _rte_eth_dev_callback_process(dev, RTE_ETH_EVENT_INTR_LSC);
2466         }
2467
2468         PMD_DRV_LOG(DEBUG, "enable intr in delayed handler S[%08x]", eicr);
2469         ixgbe_enable_intr(dev);
2470         rte_intr_enable(&(dev->pci_dev->intr_handle));
2471 }
2472
2473 /**
2474  * Interrupt handler triggered by NIC  for handling
2475  * specific interrupt.
2476  *
2477  * @param handle
2478  *  Pointer to interrupt handle.
2479  * @param param
2480  *  The address of parameter (struct rte_eth_dev *) regsitered before.
2481  *
2482  * @return
2483  *  void
2484  */
2485 static void
2486 ixgbe_dev_interrupt_handler(__rte_unused struct rte_intr_handle *handle,
2487                                                         void *param)
2488 {
2489         struct rte_eth_dev *dev = (struct rte_eth_dev *)param;
2490         ixgbe_dev_interrupt_get_status(dev);
2491         ixgbe_dev_interrupt_action(dev);
2492 }
2493
2494 static int
2495 ixgbe_dev_led_on(struct rte_eth_dev *dev)
2496 {
2497         struct ixgbe_hw *hw;
2498
2499         hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2500         return (ixgbe_led_on(hw, 0) == IXGBE_SUCCESS ? 0 : -ENOTSUP);
2501 }
2502
2503 static int
2504 ixgbe_dev_led_off(struct rte_eth_dev *dev)
2505 {
2506         struct ixgbe_hw *hw;
2507
2508         hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2509         return (ixgbe_led_off(hw, 0) == IXGBE_SUCCESS ? 0 : -ENOTSUP);
2510 }
2511
2512 static int
2513 ixgbe_flow_ctrl_get(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
2514 {
2515         struct ixgbe_hw *hw;
2516         uint32_t mflcn_reg;
2517         uint32_t fccfg_reg;
2518         int rx_pause;
2519         int tx_pause;
2520
2521         hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2522
2523         fc_conf->pause_time = hw->fc.pause_time;
2524         fc_conf->high_water = hw->fc.high_water[0];
2525         fc_conf->low_water = hw->fc.low_water[0];
2526         fc_conf->send_xon = hw->fc.send_xon;
2527         fc_conf->autoneg = !hw->fc.disable_fc_autoneg;
2528
2529         /*
2530          * Return rx_pause status according to actual setting of
2531          * MFLCN register.
2532          */
2533         mflcn_reg = IXGBE_READ_REG(hw, IXGBE_MFLCN);
2534         if (mflcn_reg & (IXGBE_MFLCN_RPFCE | IXGBE_MFLCN_RFCE))
2535                 rx_pause = 1;
2536         else
2537                 rx_pause = 0;
2538
2539         /*
2540          * Return tx_pause status according to actual setting of
2541          * FCCFG register.
2542          */
2543         fccfg_reg = IXGBE_READ_REG(hw, IXGBE_FCCFG);
2544         if (fccfg_reg & (IXGBE_FCCFG_TFCE_802_3X | IXGBE_FCCFG_TFCE_PRIORITY))
2545                 tx_pause = 1;
2546         else
2547                 tx_pause = 0;
2548
2549         if (rx_pause && tx_pause)
2550                 fc_conf->mode = RTE_FC_FULL;
2551         else if (rx_pause)
2552                 fc_conf->mode = RTE_FC_RX_PAUSE;
2553         else if (tx_pause)
2554                 fc_conf->mode = RTE_FC_TX_PAUSE;
2555         else
2556                 fc_conf->mode = RTE_FC_NONE;
2557
2558         return 0;
2559 }
2560
2561 static int
2562 ixgbe_flow_ctrl_set(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
2563 {
2564         struct ixgbe_hw *hw;
2565         int err;
2566         uint32_t rx_buf_size;
2567         uint32_t max_high_water;
2568         uint32_t mflcn;
2569         enum ixgbe_fc_mode rte_fcmode_2_ixgbe_fcmode[] = {
2570                 ixgbe_fc_none,
2571                 ixgbe_fc_rx_pause,
2572                 ixgbe_fc_tx_pause,
2573                 ixgbe_fc_full
2574         };
2575
2576         PMD_INIT_FUNC_TRACE();
2577
2578         hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2579         if (fc_conf->autoneg != !hw->fc.disable_fc_autoneg)
2580                 return -ENOTSUP;
2581         rx_buf_size = IXGBE_READ_REG(hw, IXGBE_RXPBSIZE(0));
2582         PMD_INIT_LOG(DEBUG, "Rx packet buffer size = 0x%x", rx_buf_size);
2583
2584         /*
2585          * At least reserve one Ethernet frame for watermark
2586          * high_water/low_water in kilo bytes for ixgbe
2587          */
2588         max_high_water = (rx_buf_size - ETHER_MAX_LEN) >> IXGBE_RXPBSIZE_SHIFT;
2589         if ((fc_conf->high_water > max_high_water) ||
2590                 (fc_conf->high_water < fc_conf->low_water)) {
2591                 PMD_INIT_LOG(ERR, "Invalid high/low water setup value in KB");
2592                 PMD_INIT_LOG(ERR, "High_water must <= 0x%x", max_high_water);
2593                 return (-EINVAL);
2594         }
2595
2596         hw->fc.requested_mode = rte_fcmode_2_ixgbe_fcmode[fc_conf->mode];
2597         hw->fc.pause_time     = fc_conf->pause_time;
2598         hw->fc.high_water[0]  = fc_conf->high_water;
2599         hw->fc.low_water[0]   = fc_conf->low_water;
2600         hw->fc.send_xon       = fc_conf->send_xon;
2601
2602         err = ixgbe_fc_enable(hw);
2603
2604         /* Not negotiated is not an error case */
2605         if ((err == IXGBE_SUCCESS) || (err == IXGBE_ERR_FC_NOT_NEGOTIATED)) {
2606
2607                 /* check if we want to forward MAC frames - driver doesn't have native
2608                  * capability to do that, so we'll write the registers ourselves */
2609
2610                 mflcn = IXGBE_READ_REG(hw, IXGBE_MFLCN);
2611
2612                 /* set or clear MFLCN.PMCF bit depending on configuration */
2613                 if (fc_conf->mac_ctrl_frame_fwd != 0)
2614                         mflcn |= IXGBE_MFLCN_PMCF;
2615                 else
2616                         mflcn &= ~IXGBE_MFLCN_PMCF;
2617
2618                 IXGBE_WRITE_REG(hw, IXGBE_MFLCN, mflcn);
2619                 IXGBE_WRITE_FLUSH(hw);
2620
2621                 return 0;
2622         }
2623
2624         PMD_INIT_LOG(ERR, "ixgbe_fc_enable = 0x%x", err);
2625         return -EIO;
2626 }
2627
2628 /**
2629  *  ixgbe_pfc_enable_generic - Enable flow control
2630  *  @hw: pointer to hardware structure
2631  *  @tc_num: traffic class number
2632  *  Enable flow control according to the current settings.
2633  */
2634 static int
2635 ixgbe_dcb_pfc_enable_generic(struct ixgbe_hw *hw,uint8_t tc_num)
2636 {
2637         int ret_val = 0;
2638         uint32_t mflcn_reg, fccfg_reg;
2639         uint32_t reg;
2640         uint32_t fcrtl, fcrth;
2641         uint8_t i;
2642         uint8_t nb_rx_en;
2643
2644         /* Validate the water mark configuration */
2645         if (!hw->fc.pause_time) {
2646                 ret_val = IXGBE_ERR_INVALID_LINK_SETTINGS;
2647                 goto out;
2648         }
2649
2650         /* Low water mark of zero causes XOFF floods */
2651         if (hw->fc.current_mode & ixgbe_fc_tx_pause) {
2652                  /* High/Low water can not be 0 */
2653                 if( (!hw->fc.high_water[tc_num])|| (!hw->fc.low_water[tc_num])) {
2654                         PMD_INIT_LOG(ERR, "Invalid water mark configuration");
2655                         ret_val = IXGBE_ERR_INVALID_LINK_SETTINGS;
2656                         goto out;
2657                 }
2658
2659                 if(hw->fc.low_water[tc_num] >= hw->fc.high_water[tc_num]) {
2660                         PMD_INIT_LOG(ERR, "Invalid water mark configuration");
2661                         ret_val = IXGBE_ERR_INVALID_LINK_SETTINGS;
2662                         goto out;
2663                 }
2664         }
2665         /* Negotiate the fc mode to use */
2666         ixgbe_fc_autoneg(hw);
2667
2668         /* Disable any previous flow control settings */
2669         mflcn_reg = IXGBE_READ_REG(hw, IXGBE_MFLCN);
2670         mflcn_reg &= ~(IXGBE_MFLCN_RPFCE_SHIFT | IXGBE_MFLCN_RFCE|IXGBE_MFLCN_RPFCE);
2671
2672         fccfg_reg = IXGBE_READ_REG(hw, IXGBE_FCCFG);
2673         fccfg_reg &= ~(IXGBE_FCCFG_TFCE_802_3X | IXGBE_FCCFG_TFCE_PRIORITY);
2674
2675         switch (hw->fc.current_mode) {
2676         case ixgbe_fc_none:
2677                 /*
2678                  * If the count of enabled RX Priority Flow control >1,
2679                  * and the TX pause can not be disabled
2680                  */
2681                 nb_rx_en = 0;
2682                 for (i =0; i < IXGBE_DCB_MAX_TRAFFIC_CLASS; i++) {
2683                         reg = IXGBE_READ_REG(hw, IXGBE_FCRTH_82599(i));
2684                         if (reg & IXGBE_FCRTH_FCEN)
2685                                 nb_rx_en++;
2686                 }
2687                 if (nb_rx_en > 1)
2688                         fccfg_reg |=IXGBE_FCCFG_TFCE_PRIORITY;
2689                 break;
2690         case ixgbe_fc_rx_pause:
2691                 /*
2692                  * Rx Flow control is enabled and Tx Flow control is
2693                  * disabled by software override. Since there really
2694                  * isn't a way to advertise that we are capable of RX
2695                  * Pause ONLY, we will advertise that we support both
2696                  * symmetric and asymmetric Rx PAUSE.  Later, we will
2697                  * disable the adapter's ability to send PAUSE frames.
2698                  */
2699                 mflcn_reg |= IXGBE_MFLCN_RPFCE;
2700                 /*
2701                  * If the count of enabled RX Priority Flow control >1,
2702                  * and the TX pause can not be disabled
2703                  */
2704                 nb_rx_en = 0;
2705                 for (i =0; i < IXGBE_DCB_MAX_TRAFFIC_CLASS; i++) {
2706                         reg = IXGBE_READ_REG(hw, IXGBE_FCRTH_82599(i));
2707                         if (reg & IXGBE_FCRTH_FCEN)
2708                                 nb_rx_en++;
2709                 }
2710                 if (nb_rx_en > 1)
2711                         fccfg_reg |=IXGBE_FCCFG_TFCE_PRIORITY;
2712                 break;
2713         case ixgbe_fc_tx_pause:
2714                 /*
2715                  * Tx Flow control is enabled, and Rx Flow control is
2716                  * disabled by software override.
2717                  */
2718                 fccfg_reg |=IXGBE_FCCFG_TFCE_PRIORITY;
2719                 break;
2720         case ixgbe_fc_full:
2721                 /* Flow control (both Rx and Tx) is enabled by SW override. */
2722                 mflcn_reg |= IXGBE_MFLCN_RPFCE;
2723                 fccfg_reg |= IXGBE_FCCFG_TFCE_PRIORITY;
2724                 break;
2725         default:
2726                 PMD_DRV_LOG(DEBUG, "Flow control param set incorrectly");
2727                 ret_val = IXGBE_ERR_CONFIG;
2728                 goto out;
2729                 break;
2730         }
2731
2732         /* Set 802.3x based flow control settings. */
2733         mflcn_reg |= IXGBE_MFLCN_DPF;
2734         IXGBE_WRITE_REG(hw, IXGBE_MFLCN, mflcn_reg);
2735         IXGBE_WRITE_REG(hw, IXGBE_FCCFG, fccfg_reg);
2736
2737         /* Set up and enable Rx high/low water mark thresholds, enable XON. */
2738         if ((hw->fc.current_mode & ixgbe_fc_tx_pause) &&
2739                 hw->fc.high_water[tc_num]) {
2740                 fcrtl = (hw->fc.low_water[tc_num] << 10) | IXGBE_FCRTL_XONE;
2741                 IXGBE_WRITE_REG(hw, IXGBE_FCRTL_82599(tc_num), fcrtl);
2742                 fcrth = (hw->fc.high_water[tc_num] << 10) | IXGBE_FCRTH_FCEN;
2743         } else {
2744                 IXGBE_WRITE_REG(hw, IXGBE_FCRTL_82599(tc_num), 0);
2745                 /*
2746                  * In order to prevent Tx hangs when the internal Tx
2747                  * switch is enabled we must set the high water mark
2748                  * to the maximum FCRTH value.  This allows the Tx
2749                  * switch to function even under heavy Rx workloads.
2750                  */
2751                 fcrth = IXGBE_READ_REG(hw, IXGBE_RXPBSIZE(tc_num)) - 32;
2752         }
2753         IXGBE_WRITE_REG(hw, IXGBE_FCRTH_82599(tc_num), fcrth);
2754
2755         /* Configure pause time (2 TCs per register) */
2756         reg = hw->fc.pause_time * 0x00010001;
2757         for (i = 0; i < (IXGBE_DCB_MAX_TRAFFIC_CLASS / 2); i++)
2758                 IXGBE_WRITE_REG(hw, IXGBE_FCTTV(i), reg);
2759
2760         /* Configure flow control refresh threshold value */
2761         IXGBE_WRITE_REG(hw, IXGBE_FCRTV, hw->fc.pause_time / 2);
2762
2763 out:
2764         return ret_val;
2765 }
2766
2767 static int
2768 ixgbe_dcb_pfc_enable(struct rte_eth_dev *dev,uint8_t tc_num)
2769 {
2770         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2771         int32_t ret_val = IXGBE_NOT_IMPLEMENTED;
2772
2773         if(hw->mac.type != ixgbe_mac_82598EB) {
2774                 ret_val = ixgbe_dcb_pfc_enable_generic(hw,tc_num);
2775         }
2776         return ret_val;
2777 }
2778
2779 static int
2780 ixgbe_priority_flow_ctrl_set(struct rte_eth_dev *dev, struct rte_eth_pfc_conf *pfc_conf)
2781 {
2782         int err;
2783         uint32_t rx_buf_size;
2784         uint32_t max_high_water;
2785         uint8_t tc_num;
2786         uint8_t  map[IXGBE_DCB_MAX_USER_PRIORITY] = { 0 };
2787         struct ixgbe_hw *hw =
2788                 IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2789         struct ixgbe_dcb_config *dcb_config =
2790                 IXGBE_DEV_PRIVATE_TO_DCB_CFG(dev->data->dev_private);
2791
2792         enum ixgbe_fc_mode rte_fcmode_2_ixgbe_fcmode[] = {
2793                 ixgbe_fc_none,
2794                 ixgbe_fc_rx_pause,
2795                 ixgbe_fc_tx_pause,
2796                 ixgbe_fc_full
2797         };
2798
2799         PMD_INIT_FUNC_TRACE();
2800
2801         ixgbe_dcb_unpack_map_cee(dcb_config, IXGBE_DCB_RX_CONFIG, map);
2802         tc_num = map[pfc_conf->priority];
2803         rx_buf_size = IXGBE_READ_REG(hw, IXGBE_RXPBSIZE(tc_num));
2804         PMD_INIT_LOG(DEBUG, "Rx packet buffer size = 0x%x", rx_buf_size);
2805         /*
2806          * At least reserve one Ethernet frame for watermark
2807          * high_water/low_water in kilo bytes for ixgbe
2808          */
2809         max_high_water = (rx_buf_size - ETHER_MAX_LEN) >> IXGBE_RXPBSIZE_SHIFT;
2810         if ((pfc_conf->fc.high_water > max_high_water) ||
2811             (pfc_conf->fc.high_water <= pfc_conf->fc.low_water)) {
2812                 PMD_INIT_LOG(ERR, "Invalid high/low water setup value in KB");
2813                 PMD_INIT_LOG(ERR, "High_water must <= 0x%x", max_high_water);
2814                 return (-EINVAL);
2815         }
2816
2817         hw->fc.requested_mode = rte_fcmode_2_ixgbe_fcmode[pfc_conf->fc.mode];
2818         hw->fc.pause_time = pfc_conf->fc.pause_time;
2819         hw->fc.send_xon = pfc_conf->fc.send_xon;
2820         hw->fc.low_water[tc_num] =  pfc_conf->fc.low_water;
2821         hw->fc.high_water[tc_num] = pfc_conf->fc.high_water;
2822
2823         err = ixgbe_dcb_pfc_enable(dev,tc_num);
2824
2825         /* Not negotiated is not an error case */
2826         if ((err == IXGBE_SUCCESS) || (err == IXGBE_ERR_FC_NOT_NEGOTIATED))
2827                 return 0;
2828
2829         PMD_INIT_LOG(ERR, "ixgbe_dcb_pfc_enable = 0x%x", err);
2830         return -EIO;
2831 }
2832
2833 static int
2834 ixgbe_dev_rss_reta_update(struct rte_eth_dev *dev,
2835                           struct rte_eth_rss_reta_entry64 *reta_conf,
2836                           uint16_t reta_size)
2837 {
2838         uint8_t i, j, mask;
2839         uint32_t reta, r;
2840         uint16_t idx, shift;
2841         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2842
2843         PMD_INIT_FUNC_TRACE();
2844         if (reta_size != ETH_RSS_RETA_SIZE_128) {
2845                 PMD_DRV_LOG(ERR, "The size of hash lookup table configured "
2846                         "(%d) doesn't match the number hardware can supported "
2847                         "(%d)\n", reta_size, ETH_RSS_RETA_SIZE_128);
2848                 return -EINVAL;
2849         }
2850
2851         for (i = 0; i < reta_size; i += IXGBE_4_BIT_WIDTH) {
2852                 idx = i / RTE_RETA_GROUP_SIZE;
2853                 shift = i % RTE_RETA_GROUP_SIZE;
2854                 mask = (uint8_t)((reta_conf[idx].mask >> shift) &
2855                                                 IXGBE_4_BIT_MASK);
2856                 if (!mask)
2857                         continue;
2858                 if (mask == IXGBE_4_BIT_MASK)
2859                         r = 0;
2860                 else
2861                         r = IXGBE_READ_REG(hw, IXGBE_RETA(i >> 2));
2862                 for (j = 0, reta = 0; j < IXGBE_4_BIT_WIDTH; j++) {
2863                         if (mask & (0x1 << j))
2864                                 reta |= reta_conf[idx].reta[shift + j] <<
2865                                                         (CHAR_BIT * j);
2866                         else
2867                                 reta |= r & (IXGBE_8_BIT_MASK <<
2868                                                 (CHAR_BIT * j));
2869                 }
2870                 IXGBE_WRITE_REG(hw, IXGBE_RETA(i >> 2), reta);
2871         }
2872
2873         return 0;
2874 }
2875
2876 static int
2877 ixgbe_dev_rss_reta_query(struct rte_eth_dev *dev,
2878                          struct rte_eth_rss_reta_entry64 *reta_conf,
2879                          uint16_t reta_size)
2880 {
2881         uint8_t i, j, mask;
2882         uint32_t reta;
2883         uint16_t idx, shift;
2884         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2885
2886         PMD_INIT_FUNC_TRACE();
2887         if (reta_size != ETH_RSS_RETA_SIZE_128) {
2888                 PMD_DRV_LOG(ERR, "The size of hash lookup table configured "
2889                         "(%d) doesn't match the number hardware can supported "
2890                                 "(%d)\n", reta_size, ETH_RSS_RETA_SIZE_128);
2891                 return -EINVAL;
2892         }
2893
2894         for (i = 0; i < ETH_RSS_RETA_SIZE_128; i += IXGBE_4_BIT_WIDTH) {
2895                 idx = i / RTE_RETA_GROUP_SIZE;
2896                 shift = i % RTE_RETA_GROUP_SIZE;
2897                 mask = (uint8_t)((reta_conf[idx].mask >> shift) &
2898                                                 IXGBE_4_BIT_MASK);
2899                 if (!mask)
2900                         continue;
2901
2902                 reta = IXGBE_READ_REG(hw, IXGBE_RETA(i >> 2));
2903                 for (j = 0; j < IXGBE_4_BIT_WIDTH; j++) {
2904                         if (mask & (0x1 << j))
2905                                 reta_conf[idx].reta[shift + j] =
2906                                         ((reta >> (CHAR_BIT * j)) &
2907                                                 IXGBE_8_BIT_MASK);
2908                 }
2909         }
2910
2911         return 0;
2912 }
2913
2914 static void
2915 ixgbe_add_rar(struct rte_eth_dev *dev, struct ether_addr *mac_addr,
2916                                 uint32_t index, uint32_t pool)
2917 {
2918         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2919         uint32_t enable_addr = 1;
2920
2921         ixgbe_set_rar(hw, index, mac_addr->addr_bytes, pool, enable_addr);
2922 }
2923
2924 static void
2925 ixgbe_remove_rar(struct rte_eth_dev *dev, uint32_t index)
2926 {
2927         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2928
2929         ixgbe_clear_rar(hw, index);
2930 }
2931
2932 static int
2933 ixgbe_dev_mtu_set(struct rte_eth_dev *dev, uint16_t mtu)
2934 {
2935         uint32_t hlreg0;
2936         uint32_t maxfrs;
2937         struct ixgbe_hw *hw;
2938         struct rte_eth_dev_info dev_info;
2939         uint32_t frame_size = mtu + ETHER_HDR_LEN + ETHER_CRC_LEN;
2940
2941         ixgbe_dev_info_get(dev, &dev_info);
2942
2943         /* check that mtu is within the allowed range */
2944         if ((mtu < ETHER_MIN_MTU) || (frame_size > dev_info.max_rx_pktlen))
2945                 return -EINVAL;
2946
2947         /* refuse mtu that requires the support of scattered packets when this
2948          * feature has not been enabled before. */
2949         if (!dev->data->scattered_rx &&
2950             (frame_size + 2 * IXGBE_VLAN_TAG_SIZE >
2951              dev->data->min_rx_buf_size - RTE_PKTMBUF_HEADROOM))
2952                 return -EINVAL;
2953
2954         hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2955         hlreg0 = IXGBE_READ_REG(hw, IXGBE_HLREG0);
2956
2957         /* switch to jumbo mode if needed */
2958         if (frame_size > ETHER_MAX_LEN) {
2959                 dev->data->dev_conf.rxmode.jumbo_frame = 1;
2960                 hlreg0 |= IXGBE_HLREG0_JUMBOEN;
2961         } else {
2962                 dev->data->dev_conf.rxmode.jumbo_frame = 0;
2963                 hlreg0 &= ~IXGBE_HLREG0_JUMBOEN;
2964         }
2965         IXGBE_WRITE_REG(hw, IXGBE_HLREG0, hlreg0);
2966
2967         /* update max frame size */
2968         dev->data->dev_conf.rxmode.max_rx_pkt_len = frame_size;
2969
2970         maxfrs = IXGBE_READ_REG(hw, IXGBE_MAXFRS);
2971         maxfrs &= 0x0000FFFF;
2972         maxfrs |= (dev->data->dev_conf.rxmode.max_rx_pkt_len << 16);
2973         IXGBE_WRITE_REG(hw, IXGBE_MAXFRS, maxfrs);
2974
2975         return 0;
2976 }
2977
2978 /*
2979  * Virtual Function operations
2980  */
2981 static void
2982 ixgbevf_intr_disable(struct ixgbe_hw *hw)
2983 {
2984         PMD_INIT_FUNC_TRACE();
2985
2986         /* Clear interrupt mask to stop from interrupts being generated */
2987         IXGBE_WRITE_REG(hw, IXGBE_VTEIMC, IXGBE_VF_IRQ_CLEAR_MASK);
2988
2989         IXGBE_WRITE_FLUSH(hw);
2990 }
2991
2992 static int
2993 ixgbevf_dev_configure(struct rte_eth_dev *dev)
2994 {
2995         struct rte_eth_conf* conf = &dev->data->dev_conf;
2996         struct ixgbe_adapter *adapter =
2997                         (struct ixgbe_adapter *)dev->data->dev_private;
2998
2999         PMD_INIT_LOG(DEBUG, "Configured Virtual Function port id: %d",
3000                      dev->data->port_id);
3001
3002         /*
3003          * VF has no ability to enable/disable HW CRC
3004          * Keep the persistent behavior the same as Host PF
3005          */
3006 #ifndef RTE_LIBRTE_IXGBE_PF_DISABLE_STRIP_CRC
3007         if (!conf->rxmode.hw_strip_crc) {
3008                 PMD_INIT_LOG(INFO, "VF can't disable HW CRC Strip");
3009                 conf->rxmode.hw_strip_crc = 1;
3010         }
3011 #else
3012         if (conf->rxmode.hw_strip_crc) {
3013                 PMD_INIT_LOG(INFO, "VF can't enable HW CRC Strip");
3014                 conf->rxmode.hw_strip_crc = 0;
3015         }
3016 #endif
3017
3018         /*
3019          * Initialize to TRUE. If any of Rx queues doesn't meet the bulk
3020          * allocation or vector Rx preconditions we will reset it.
3021          */
3022         adapter->rx_bulk_alloc_allowed = true;
3023         adapter->rx_vec_allowed = true;
3024
3025         return 0;
3026 }
3027
3028 static int
3029 ixgbevf_dev_start(struct rte_eth_dev *dev)
3030 {
3031         struct ixgbe_hw *hw =
3032                 IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
3033         int err, mask = 0;
3034
3035         PMD_INIT_FUNC_TRACE();
3036
3037         hw->mac.ops.reset_hw(hw);
3038         hw->mac.get_link_status = true;
3039
3040         /* negotiate mailbox API version to use with the PF. */
3041         ixgbevf_negotiate_api(hw);
3042
3043         ixgbevf_dev_tx_init(dev);
3044
3045         /* This can fail when allocating mbufs for descriptor rings */
3046         err = ixgbevf_dev_rx_init(dev);
3047         if (err) {
3048                 PMD_INIT_LOG(ERR, "Unable to initialize RX hardware (%d)", err);
3049                 ixgbe_dev_clear_queues(dev);
3050                 return err;
3051         }
3052
3053         /* Set vfta */
3054         ixgbevf_set_vfta_all(dev,1);
3055
3056         /* Set HW strip */
3057         mask = ETH_VLAN_STRIP_MASK | ETH_VLAN_FILTER_MASK | \
3058                 ETH_VLAN_EXTEND_MASK;
3059         ixgbevf_vlan_offload_set(dev, mask);
3060
3061         ixgbevf_dev_rxtx_start(dev);
3062
3063         return 0;
3064 }
3065
3066 static void
3067 ixgbevf_dev_stop(struct rte_eth_dev *dev)
3068 {
3069         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
3070
3071         PMD_INIT_FUNC_TRACE();
3072
3073         hw->adapter_stopped = TRUE;
3074         ixgbe_stop_adapter(hw);
3075
3076         /*
3077           * Clear what we set, but we still keep shadow_vfta to
3078           * restore after device starts
3079           */
3080         ixgbevf_set_vfta_all(dev,0);
3081
3082         /* Clear stored conf */
3083         dev->data->scattered_rx = 0;
3084
3085         ixgbe_dev_clear_queues(dev);
3086 }
3087
3088 static void
3089 ixgbevf_dev_close(struct rte_eth_dev *dev)
3090 {
3091         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
3092
3093         PMD_INIT_FUNC_TRACE();
3094
3095         ixgbe_reset_hw(hw);
3096
3097         ixgbevf_dev_stop(dev);
3098
3099         /* reprogram the RAR[0] in case user changed it. */
3100         ixgbe_set_rar(hw, 0, hw->mac.addr, 0, IXGBE_RAH_AV);
3101 }
3102
3103 static void ixgbevf_set_vfta_all(struct rte_eth_dev *dev, bool on)
3104 {
3105         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
3106         struct ixgbe_vfta * shadow_vfta =
3107                 IXGBE_DEV_PRIVATE_TO_VFTA(dev->data->dev_private);
3108         int i = 0, j = 0, vfta = 0, mask = 1;
3109
3110         for (i = 0; i < IXGBE_VFTA_SIZE; i++){
3111                 vfta = shadow_vfta->vfta[i];
3112                 if(vfta){
3113                         mask = 1;
3114                         for (j = 0; j < 32; j++){
3115                                 if(vfta & mask)
3116                                         ixgbe_set_vfta(hw, (i<<5)+j, 0, on);
3117                                 mask<<=1;
3118                         }
3119                 }
3120         }
3121
3122 }
3123
3124 static int
3125 ixgbevf_vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
3126 {
3127         struct ixgbe_hw *hw =
3128                 IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
3129         struct ixgbe_vfta * shadow_vfta =
3130                 IXGBE_DEV_PRIVATE_TO_VFTA(dev->data->dev_private);
3131         uint32_t vid_idx = 0;
3132         uint32_t vid_bit = 0;
3133         int ret = 0;
3134
3135         PMD_INIT_FUNC_TRACE();
3136
3137         /* vind is not used in VF driver, set to 0, check ixgbe_set_vfta_vf */
3138         ret = ixgbe_set_vfta(hw, vlan_id, 0, !!on);
3139         if(ret){
3140                 PMD_INIT_LOG(ERR, "Unable to set VF vlan");
3141                 return ret;
3142         }
3143         vid_idx = (uint32_t) ((vlan_id >> 5) & 0x7F);
3144         vid_bit = (uint32_t) (1 << (vlan_id & 0x1F));
3145
3146         /* Save what we set and retore it after device reset */
3147         if (on)
3148                 shadow_vfta->vfta[vid_idx] |= vid_bit;
3149         else
3150                 shadow_vfta->vfta[vid_idx] &= ~vid_bit;
3151
3152         return 0;
3153 }
3154
3155 static void
3156 ixgbevf_vlan_strip_queue_set(struct rte_eth_dev *dev, uint16_t queue, int on)
3157 {
3158         struct ixgbe_hw *hw =
3159                 IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
3160         uint32_t ctrl;
3161
3162         PMD_INIT_FUNC_TRACE();
3163
3164         if(queue >= hw->mac.max_rx_queues)
3165                 return;
3166
3167         ctrl = IXGBE_READ_REG(hw, IXGBE_RXDCTL(queue));
3168         if(on)
3169                 ctrl |= IXGBE_RXDCTL_VME;
3170         else
3171                 ctrl &= ~IXGBE_RXDCTL_VME;
3172         IXGBE_WRITE_REG(hw, IXGBE_RXDCTL(queue), ctrl);
3173
3174         ixgbe_vlan_hw_strip_bitmap_set( dev, queue, on);
3175 }
3176
3177 static void
3178 ixgbevf_vlan_offload_set(struct rte_eth_dev *dev, int mask)
3179 {
3180         struct ixgbe_hw *hw =
3181                 IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
3182         uint16_t i;
3183         int on = 0;
3184
3185         /* VF function only support hw strip feature, others are not support */
3186         if(mask & ETH_VLAN_STRIP_MASK){
3187                 on = !!(dev->data->dev_conf.rxmode.hw_vlan_strip);
3188
3189                 for(i=0; i < hw->mac.max_rx_queues; i++)
3190                         ixgbevf_vlan_strip_queue_set(dev,i,on);
3191         }
3192 }
3193
3194 static int
3195 ixgbe_vmdq_mode_check(struct ixgbe_hw *hw)
3196 {
3197         uint32_t reg_val;
3198
3199         /* we only need to do this if VMDq is enabled */
3200         reg_val = IXGBE_READ_REG(hw, IXGBE_VT_CTL);
3201         if (!(reg_val & IXGBE_VT_CTL_VT_ENABLE)) {
3202                 PMD_INIT_LOG(ERR, "VMDq must be enabled for this setting");
3203                 return (-1);
3204         }
3205
3206         return 0;
3207 }
3208
3209 static uint32_t
3210 ixgbe_uta_vector(struct ixgbe_hw *hw, struct ether_addr* uc_addr)
3211 {
3212         uint32_t vector = 0;
3213         switch (hw->mac.mc_filter_type) {
3214         case 0:   /* use bits [47:36] of the address */
3215                 vector = ((uc_addr->addr_bytes[4] >> 4) |
3216                         (((uint16_t)uc_addr->addr_bytes[5]) << 4));
3217                 break;
3218         case 1:   /* use bits [46:35] of the address */
3219                 vector = ((uc_addr->addr_bytes[4] >> 3) |
3220                         (((uint16_t)uc_addr->addr_bytes[5]) << 5));
3221                 break;
3222         case 2:   /* use bits [45:34] of the address */
3223                 vector = ((uc_addr->addr_bytes[4] >> 2) |
3224                         (((uint16_t)uc_addr->addr_bytes[5]) << 6));
3225                 break;
3226         case 3:   /* use bits [43:32] of the address */
3227                 vector = ((uc_addr->addr_bytes[4]) |
3228                         (((uint16_t)uc_addr->addr_bytes[5]) << 8));
3229                 break;
3230         default:  /* Invalid mc_filter_type */
3231                 break;
3232         }
3233
3234         /* vector can only be 12-bits or boundary will be exceeded */
3235         vector &= 0xFFF;
3236         return vector;
3237 }
3238
3239 static int
3240 ixgbe_uc_hash_table_set(struct rte_eth_dev *dev,struct ether_addr* mac_addr,
3241                                uint8_t on)
3242 {
3243         uint32_t vector;
3244         uint32_t uta_idx;
3245         uint32_t reg_val;
3246         uint32_t uta_shift;
3247         uint32_t rc;
3248         const uint32_t ixgbe_uta_idx_mask = 0x7F;
3249         const uint32_t ixgbe_uta_bit_shift = 5;
3250         const uint32_t ixgbe_uta_bit_mask = (0x1 << ixgbe_uta_bit_shift) - 1;
3251         const uint32_t bit1 = 0x1;
3252
3253         struct ixgbe_hw *hw =
3254                 IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
3255         struct ixgbe_uta_info *uta_info =
3256                 IXGBE_DEV_PRIVATE_TO_UTA(dev->data->dev_private);
3257
3258         /* The UTA table only exists on 82599 hardware and newer */
3259         if (hw->mac.type < ixgbe_mac_82599EB)
3260                 return (-ENOTSUP);
3261
3262         vector = ixgbe_uta_vector(hw,mac_addr);
3263         uta_idx = (vector >> ixgbe_uta_bit_shift) & ixgbe_uta_idx_mask;
3264         uta_shift = vector & ixgbe_uta_bit_mask;
3265
3266         rc = ((uta_info->uta_shadow[uta_idx] >> uta_shift & bit1) != 0);
3267         if(rc == on)
3268                 return 0;
3269
3270         reg_val = IXGBE_READ_REG(hw, IXGBE_UTA(uta_idx));
3271         if (on) {
3272                 uta_info->uta_in_use++;
3273                 reg_val |= (bit1 << uta_shift);
3274                 uta_info->uta_shadow[uta_idx] |= (bit1 << uta_shift);
3275         } else {
3276                 uta_info->uta_in_use--;
3277                 reg_val &= ~(bit1 << uta_shift);
3278                 uta_info->uta_shadow[uta_idx] &= ~(bit1 << uta_shift);
3279         }
3280
3281         IXGBE_WRITE_REG(hw, IXGBE_UTA(uta_idx), reg_val);
3282
3283         if (uta_info->uta_in_use > 0)
3284                 IXGBE_WRITE_REG(hw, IXGBE_MCSTCTRL,
3285                                 IXGBE_MCSTCTRL_MFE | hw->mac.mc_filter_type);
3286         else
3287                 IXGBE_WRITE_REG(hw, IXGBE_MCSTCTRL,hw->mac.mc_filter_type);
3288
3289         return 0;
3290 }
3291
3292 static int
3293 ixgbe_uc_all_hash_table_set(struct rte_eth_dev *dev, uint8_t on)
3294 {
3295         int i;
3296         struct ixgbe_hw *hw =
3297                 IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
3298         struct ixgbe_uta_info *uta_info =
3299                 IXGBE_DEV_PRIVATE_TO_UTA(dev->data->dev_private);
3300
3301         /* The UTA table only exists on 82599 hardware and newer */
3302         if (hw->mac.type < ixgbe_mac_82599EB)
3303                 return (-ENOTSUP);
3304
3305         if(on) {
3306                 for (i = 0; i < ETH_VMDQ_NUM_UC_HASH_ARRAY; i++) {
3307                         uta_info->uta_shadow[i] = ~0;
3308                         IXGBE_WRITE_REG(hw, IXGBE_UTA(i), ~0);
3309                 }
3310         } else {
3311                 for (i = 0; i < ETH_VMDQ_NUM_UC_HASH_ARRAY; i++) {
3312                         uta_info->uta_shadow[i] = 0;
3313                         IXGBE_WRITE_REG(hw, IXGBE_UTA(i), 0);
3314                 }
3315         }
3316         return 0;
3317
3318 }
3319
3320 uint32_t
3321 ixgbe_convert_vm_rx_mask_to_val(uint16_t rx_mask, uint32_t orig_val)
3322 {
3323         uint32_t new_val = orig_val;
3324
3325         if (rx_mask & ETH_VMDQ_ACCEPT_UNTAG)
3326                 new_val |= IXGBE_VMOLR_AUPE;
3327         if (rx_mask & ETH_VMDQ_ACCEPT_HASH_MC)
3328                 new_val |= IXGBE_VMOLR_ROMPE;
3329         if (rx_mask & ETH_VMDQ_ACCEPT_HASH_UC)
3330                 new_val |= IXGBE_VMOLR_ROPE;
3331         if (rx_mask & ETH_VMDQ_ACCEPT_BROADCAST)
3332                 new_val |= IXGBE_VMOLR_BAM;
3333         if (rx_mask & ETH_VMDQ_ACCEPT_MULTICAST)
3334                 new_val |= IXGBE_VMOLR_MPE;
3335
3336         return new_val;
3337 }
3338
3339 static int
3340 ixgbe_set_pool_rx_mode(struct rte_eth_dev *dev, uint16_t pool,
3341                                uint16_t rx_mask, uint8_t on)
3342 {
3343         int val = 0;
3344
3345         struct ixgbe_hw *hw =
3346                 IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
3347         uint32_t vmolr = IXGBE_READ_REG(hw, IXGBE_VMOLR(pool));
3348
3349         if (hw->mac.type == ixgbe_mac_82598EB) {
3350                 PMD_INIT_LOG(ERR, "setting VF receive mode set should be done"
3351                              " on 82599 hardware and newer");
3352                 return (-ENOTSUP);
3353         }
3354         if (ixgbe_vmdq_mode_check(hw) < 0)
3355                 return (-ENOTSUP);
3356
3357         val = ixgbe_convert_vm_rx_mask_to_val(rx_mask, val);
3358
3359         if (on)
3360                 vmolr |= val;
3361         else
3362                 vmolr &= ~val;
3363
3364         IXGBE_WRITE_REG(hw, IXGBE_VMOLR(pool), vmolr);
3365
3366         return 0;
3367 }
3368
3369 static int
3370 ixgbe_set_pool_rx(struct rte_eth_dev *dev, uint16_t pool, uint8_t on)
3371 {
3372         uint32_t reg,addr;
3373         uint32_t val;
3374         const uint8_t bit1 = 0x1;
3375
3376         struct ixgbe_hw *hw =
3377                 IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
3378
3379         if (ixgbe_vmdq_mode_check(hw) < 0)
3380                 return (-ENOTSUP);
3381
3382         addr = IXGBE_VFRE(pool >= ETH_64_POOLS/2);
3383         reg = IXGBE_READ_REG(hw, addr);
3384         val = bit1 << pool;
3385
3386         if (on)
3387                 reg |= val;
3388         else
3389                 reg &= ~val;
3390
3391         IXGBE_WRITE_REG(hw, addr,reg);
3392
3393         return 0;
3394 }
3395
3396 static int
3397 ixgbe_set_pool_tx(struct rte_eth_dev *dev, uint16_t pool, uint8_t on)
3398 {
3399         uint32_t reg,addr;
3400         uint32_t val;
3401         const uint8_t bit1 = 0x1;
3402
3403         struct ixgbe_hw *hw =
3404                 IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
3405
3406         if (ixgbe_vmdq_mode_check(hw) < 0)
3407                 return (-ENOTSUP);
3408
3409         addr = IXGBE_VFTE(pool >= ETH_64_POOLS/2);
3410         reg = IXGBE_READ_REG(hw, addr);
3411         val = bit1 << pool;
3412
3413         if (on)
3414                 reg |= val;
3415         else
3416                 reg &= ~val;
3417
3418         IXGBE_WRITE_REG(hw, addr,reg);
3419
3420         return 0;
3421 }
3422
3423 static int
3424 ixgbe_set_pool_vlan_filter(struct rte_eth_dev *dev, uint16_t vlan,
3425                         uint64_t pool_mask, uint8_t vlan_on)
3426 {
3427         int ret = 0;
3428         uint16_t pool_idx;
3429         struct ixgbe_hw *hw =
3430                 IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
3431
3432         if (ixgbe_vmdq_mode_check(hw) < 0)
3433                 return (-ENOTSUP);
3434         for (pool_idx = 0; pool_idx < ETH_64_POOLS; pool_idx++) {
3435                 if (pool_mask & ((uint64_t)(1ULL << pool_idx)))
3436                         ret = hw->mac.ops.set_vfta(hw,vlan,pool_idx,vlan_on);
3437                         if (ret < 0)
3438                                 return ret;
3439         }
3440
3441         return ret;
3442 }
3443
3444 #define IXGBE_MRCTL_VPME  0x01 /* Virtual Pool Mirroring. */
3445 #define IXGBE_MRCTL_UPME  0x02 /* Uplink Port Mirroring. */
3446 #define IXGBE_MRCTL_DPME  0x04 /* Downlink Port Mirroring. */
3447 #define IXGBE_MRCTL_VLME  0x08 /* VLAN Mirroring. */
3448 #define IXGBE_INVALID_MIRROR_TYPE(mirror_type) \
3449         ((mirror_type) & ~(uint8_t)(ETH_MIRROR_VIRTUAL_POOL_UP | \
3450         ETH_MIRROR_UPLINK_PORT | ETH_MIRROR_DOWNLINK_PORT | ETH_MIRROR_VLAN))
3451
3452 static int
3453 ixgbe_mirror_rule_set(struct rte_eth_dev *dev,
3454                         struct rte_eth_mirror_conf *mirror_conf,
3455                         uint8_t rule_id, uint8_t on)
3456 {
3457         uint32_t mr_ctl,vlvf;
3458         uint32_t mp_lsb = 0;
3459         uint32_t mv_msb = 0;
3460         uint32_t mv_lsb = 0;
3461         uint32_t mp_msb = 0;
3462         uint8_t i = 0;
3463         int reg_index = 0;
3464         uint64_t vlan_mask = 0;
3465
3466         const uint8_t pool_mask_offset = 32;
3467         const uint8_t vlan_mask_offset = 32;
3468         const uint8_t dst_pool_offset = 8;
3469         const uint8_t rule_mr_offset  = 4;
3470         const uint8_t mirror_rule_mask= 0x0F;
3471
3472         struct ixgbe_mirror_info *mr_info =
3473                         (IXGBE_DEV_PRIVATE_TO_PFDATA(dev->data->dev_private));
3474         struct ixgbe_hw *hw =
3475                 IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
3476         uint8_t mirror_type = 0;
3477
3478         if (ixgbe_vmdq_mode_check(hw) < 0)
3479                 return -ENOTSUP;
3480
3481         if (rule_id >= IXGBE_MAX_MIRROR_RULES)
3482                 return -EINVAL;
3483
3484         if (IXGBE_INVALID_MIRROR_TYPE(mirror_conf->rule_type)) {
3485                 PMD_DRV_LOG(ERR, "unsupported mirror type 0x%x.",
3486                         mirror_conf->rule_type);
3487                 return -EINVAL;
3488         }
3489
3490         if (mirror_conf->rule_type & ETH_MIRROR_VLAN) {
3491                 mirror_type |= IXGBE_MRCTL_VLME;
3492                 /* Check if vlan id is valid and find conresponding VLAN ID index in VLVF */
3493                 for (i = 0;i < IXGBE_VLVF_ENTRIES; i++) {
3494                         if (mirror_conf->vlan.vlan_mask & (1ULL << i)) {
3495                                 /* search vlan id related pool vlan filter index */
3496                                 reg_index = ixgbe_find_vlvf_slot(hw,
3497                                                 mirror_conf->vlan.vlan_id[i]);
3498                                 if(reg_index < 0)
3499                                         return -EINVAL;
3500                                 vlvf = IXGBE_READ_REG(hw, IXGBE_VLVF(reg_index));
3501                                 if ((vlvf & IXGBE_VLVF_VIEN) &&
3502                                     ((vlvf & IXGBE_VLVF_VLANID_MASK) ==
3503                                       mirror_conf->vlan.vlan_id[i]))
3504                                         vlan_mask |= (1ULL << reg_index);
3505                                 else
3506                                         return -EINVAL;
3507                         }
3508                 }
3509
3510                 if (on) {
3511                         mv_lsb = vlan_mask & 0xFFFFFFFF;
3512                         mv_msb = vlan_mask >> vlan_mask_offset;
3513
3514                         mr_info->mr_conf[rule_id].vlan.vlan_mask =
3515                                                 mirror_conf->vlan.vlan_mask;
3516                         for(i = 0 ;i < ETH_VMDQ_MAX_VLAN_FILTERS; i++) {
3517                                 if(mirror_conf->vlan.vlan_mask & (1ULL << i))
3518                                         mr_info->mr_conf[rule_id].vlan.vlan_id[i] =
3519                                                 mirror_conf->vlan.vlan_id[i];
3520                         }
3521                 } else {
3522                         mv_lsb = 0;
3523                         mv_msb = 0;
3524                         mr_info->mr_conf[rule_id].vlan.vlan_mask = 0;
3525                         for(i = 0 ;i < ETH_VMDQ_MAX_VLAN_FILTERS; i++)
3526                                 mr_info->mr_conf[rule_id].vlan.vlan_id[i] = 0;
3527                 }
3528         }
3529
3530         /*
3531          * if enable pool mirror, write related pool mask register,if disable
3532          * pool mirror, clear PFMRVM register
3533          */
3534         if (mirror_conf->rule_type & ETH_MIRROR_VIRTUAL_POOL_UP) {
3535                 mirror_type |= IXGBE_MRCTL_VPME;
3536                 if (on) {
3537                         mp_lsb = mirror_conf->pool_mask & 0xFFFFFFFF;
3538                         mp_msb = mirror_conf->pool_mask >> pool_mask_offset;
3539                         mr_info->mr_conf[rule_id].pool_mask =
3540                                         mirror_conf->pool_mask;
3541
3542                 } else {
3543                         mp_lsb = 0;
3544                         mp_msb = 0;
3545                         mr_info->mr_conf[rule_id].pool_mask = 0;
3546                 }
3547         }
3548         if (mirror_conf->rule_type & ETH_MIRROR_UPLINK_PORT)
3549                 mirror_type |= IXGBE_MRCTL_UPME;
3550         if (mirror_conf->rule_type & ETH_MIRROR_DOWNLINK_PORT)
3551                 mirror_type |= IXGBE_MRCTL_DPME;
3552
3553         /* read  mirror control register and recalculate it */
3554         mr_ctl = IXGBE_READ_REG(hw, IXGBE_MRCTL(rule_id));
3555
3556         if (on) {
3557                 mr_ctl |= mirror_type;
3558                 mr_ctl &= mirror_rule_mask;
3559                 mr_ctl |= mirror_conf->dst_pool << dst_pool_offset;
3560         } else
3561                 mr_ctl &= ~(mirror_conf->rule_type & mirror_rule_mask);
3562
3563         mr_info->mr_conf[rule_id].rule_type = mirror_conf->rule_type;
3564         mr_info->mr_conf[rule_id].dst_pool = mirror_conf->dst_pool;
3565
3566         /* write mirrror control  register */
3567         IXGBE_WRITE_REG(hw, IXGBE_MRCTL(rule_id), mr_ctl);
3568
3569         /* write pool mirrror control  register */
3570         if (mirror_conf->rule_type == ETH_MIRROR_VIRTUAL_POOL_UP) {
3571                 IXGBE_WRITE_REG(hw, IXGBE_VMRVM(rule_id), mp_lsb);
3572                 IXGBE_WRITE_REG(hw, IXGBE_VMRVM(rule_id + rule_mr_offset),
3573                                 mp_msb);
3574         }
3575         /* write VLAN mirrror control  register */
3576         if (mirror_conf->rule_type == ETH_MIRROR_VLAN) {
3577                 IXGBE_WRITE_REG(hw, IXGBE_VMRVLAN(rule_id), mv_lsb);
3578                 IXGBE_WRITE_REG(hw, IXGBE_VMRVLAN(rule_id + rule_mr_offset),
3579                                 mv_msb);
3580         }
3581
3582         return 0;
3583 }
3584
3585 static int
3586 ixgbe_mirror_rule_reset(struct rte_eth_dev *dev, uint8_t rule_id)
3587 {
3588         int mr_ctl = 0;
3589         uint32_t lsb_val = 0;
3590         uint32_t msb_val = 0;
3591         const uint8_t rule_mr_offset = 4;
3592
3593         struct ixgbe_hw *hw =
3594                 IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
3595         struct ixgbe_mirror_info *mr_info =
3596                 (IXGBE_DEV_PRIVATE_TO_PFDATA(dev->data->dev_private));
3597
3598         if (ixgbe_vmdq_mode_check(hw) < 0)
3599                 return (-ENOTSUP);
3600
3601         memset(&mr_info->mr_conf[rule_id], 0,
3602                 sizeof(struct rte_eth_mirror_conf));
3603
3604         /* clear PFVMCTL register */
3605         IXGBE_WRITE_REG(hw, IXGBE_MRCTL(rule_id), mr_ctl);
3606
3607         /* clear pool mask register */
3608         IXGBE_WRITE_REG(hw, IXGBE_VMRVM(rule_id), lsb_val);
3609         IXGBE_WRITE_REG(hw, IXGBE_VMRVM(rule_id + rule_mr_offset), msb_val);
3610
3611         /* clear vlan mask register */
3612         IXGBE_WRITE_REG(hw, IXGBE_VMRVLAN(rule_id), lsb_val);
3613         IXGBE_WRITE_REG(hw, IXGBE_VMRVLAN(rule_id + rule_mr_offset), msb_val);
3614
3615         return 0;
3616 }
3617
3618 static int ixgbe_set_queue_rate_limit(struct rte_eth_dev *dev,
3619         uint16_t queue_idx, uint16_t tx_rate)
3620 {
3621         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
3622         uint32_t rf_dec, rf_int;
3623         uint32_t bcnrc_val;
3624         uint16_t link_speed = dev->data->dev_link.link_speed;
3625
3626         if (queue_idx >= hw->mac.max_tx_queues)
3627                 return -EINVAL;
3628
3629         if (tx_rate != 0) {
3630                 /* Calculate the rate factor values to set */
3631                 rf_int = (uint32_t)link_speed / (uint32_t)tx_rate;
3632                 rf_dec = (uint32_t)link_speed % (uint32_t)tx_rate;
3633                 rf_dec = (rf_dec << IXGBE_RTTBCNRC_RF_INT_SHIFT) / tx_rate;
3634
3635                 bcnrc_val = IXGBE_RTTBCNRC_RS_ENA;
3636                 bcnrc_val |= ((rf_int << IXGBE_RTTBCNRC_RF_INT_SHIFT) &
3637                                 IXGBE_RTTBCNRC_RF_INT_MASK_M);
3638                 bcnrc_val |= (rf_dec & IXGBE_RTTBCNRC_RF_DEC_MASK);
3639         } else {
3640                 bcnrc_val = 0;
3641         }
3642
3643         /*
3644          * Set global transmit compensation time to the MMW_SIZE in RTTBCNRM
3645          * register. MMW_SIZE=0x014 if 9728-byte jumbo is supported, otherwise
3646          * set as 0x4.
3647          */
3648         if ((dev->data->dev_conf.rxmode.jumbo_frame == 1) &&
3649                 (dev->data->dev_conf.rxmode.max_rx_pkt_len >=
3650                                 IXGBE_MAX_JUMBO_FRAME_SIZE))
3651                 IXGBE_WRITE_REG(hw, IXGBE_RTTBCNRM,
3652                         IXGBE_MMW_SIZE_JUMBO_FRAME);
3653         else
3654                 IXGBE_WRITE_REG(hw, IXGBE_RTTBCNRM,
3655                         IXGBE_MMW_SIZE_DEFAULT);
3656
3657         /* Set RTTBCNRC of queue X */
3658         IXGBE_WRITE_REG(hw, IXGBE_RTTDQSEL, queue_idx);
3659         IXGBE_WRITE_REG(hw, IXGBE_RTTBCNRC, bcnrc_val);
3660         IXGBE_WRITE_FLUSH(hw);
3661
3662         return 0;
3663 }
3664
3665 static int ixgbe_set_vf_rate_limit(struct rte_eth_dev *dev, uint16_t vf,
3666         uint16_t tx_rate, uint64_t q_msk)
3667 {
3668         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
3669         struct ixgbe_vf_info *vfinfo =
3670                 *(IXGBE_DEV_PRIVATE_TO_P_VFDATA(dev->data->dev_private));
3671         uint8_t  nb_q_per_pool = RTE_ETH_DEV_SRIOV(dev).nb_q_per_pool;
3672         uint32_t queue_stride =
3673                 IXGBE_MAX_RX_QUEUE_NUM / RTE_ETH_DEV_SRIOV(dev).active;
3674         uint32_t queue_idx = vf * queue_stride, idx = 0, vf_idx;
3675         uint32_t queue_end = queue_idx + nb_q_per_pool - 1;
3676         uint16_t total_rate = 0;
3677
3678         if (queue_end >= hw->mac.max_tx_queues)
3679                 return -EINVAL;
3680
3681         if (vfinfo != NULL) {
3682                 for (vf_idx = 0; vf_idx < dev->pci_dev->max_vfs; vf_idx++) {
3683                         if (vf_idx == vf)
3684                                 continue;
3685                         for (idx = 0; idx < RTE_DIM(vfinfo[vf_idx].tx_rate);
3686                                 idx++)
3687                                 total_rate += vfinfo[vf_idx].tx_rate[idx];
3688                 }
3689         } else
3690                 return -EINVAL;
3691
3692         /* Store tx_rate for this vf. */
3693         for (idx = 0; idx < nb_q_per_pool; idx++) {
3694                 if (((uint64_t)0x1 << idx) & q_msk) {
3695                         if (vfinfo[vf].tx_rate[idx] != tx_rate)
3696                                 vfinfo[vf].tx_rate[idx] = tx_rate;
3697                         total_rate += tx_rate;
3698                 }
3699         }
3700
3701         if (total_rate > dev->data->dev_link.link_speed) {
3702                 /*
3703                  * Reset stored TX rate of the VF if it causes exceed
3704                  * link speed.
3705                  */
3706                 memset(vfinfo[vf].tx_rate, 0, sizeof(vfinfo[vf].tx_rate));
3707                 return -EINVAL;
3708         }
3709
3710         /* Set RTTBCNRC of each queue/pool for vf X  */
3711         for (; queue_idx <= queue_end; queue_idx++) {
3712                 if (0x1 & q_msk)
3713                         ixgbe_set_queue_rate_limit(dev, queue_idx, tx_rate);
3714                 q_msk = q_msk >> 1;
3715         }
3716
3717         return 0;
3718 }
3719
3720 static void
3721 ixgbevf_add_mac_addr(struct rte_eth_dev *dev, struct ether_addr *mac_addr,
3722                      __attribute__((unused)) uint32_t index,
3723                      __attribute__((unused)) uint32_t pool)
3724 {
3725         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
3726         int diag;
3727
3728         /*
3729          * On a 82599 VF, adding again the same MAC addr is not an idempotent
3730          * operation. Trap this case to avoid exhausting the [very limited]
3731          * set of PF resources used to store VF MAC addresses.
3732          */
3733         if (memcmp(hw->mac.perm_addr, mac_addr, sizeof(struct ether_addr)) == 0)
3734                 return;
3735         diag = ixgbevf_set_uc_addr_vf(hw, 2, mac_addr->addr_bytes);
3736         if (diag == 0)
3737                 return;
3738         PMD_DRV_LOG(ERR, "Unable to add MAC address - diag=%d", diag);
3739 }
3740
3741 static void
3742 ixgbevf_remove_mac_addr(struct rte_eth_dev *dev, uint32_t index)
3743 {
3744         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
3745         struct ether_addr *perm_addr = (struct ether_addr *) hw->mac.perm_addr;
3746         struct ether_addr *mac_addr;
3747         uint32_t i;
3748         int diag;
3749
3750         /*
3751          * The IXGBE_VF_SET_MACVLAN command of the ixgbe-pf driver does
3752          * not support the deletion of a given MAC address.
3753          * Instead, it imposes to delete all MAC addresses, then to add again
3754          * all MAC addresses with the exception of the one to be deleted.
3755          */
3756         (void) ixgbevf_set_uc_addr_vf(hw, 0, NULL);
3757
3758         /*
3759          * Add again all MAC addresses, with the exception of the deleted one
3760          * and of the permanent MAC address.
3761          */
3762         for (i = 0, mac_addr = dev->data->mac_addrs;
3763              i < hw->mac.num_rar_entries; i++, mac_addr++) {
3764                 /* Skip the deleted MAC address */
3765                 if (i == index)
3766                         continue;
3767                 /* Skip NULL MAC addresses */
3768                 if (is_zero_ether_addr(mac_addr))
3769                         continue;
3770                 /* Skip the permanent MAC address */
3771                 if (memcmp(perm_addr, mac_addr, sizeof(struct ether_addr)) == 0)
3772                         continue;
3773                 diag = ixgbevf_set_uc_addr_vf(hw, 2, mac_addr->addr_bytes);
3774                 if (diag != 0)
3775                         PMD_DRV_LOG(ERR,
3776                                     "Adding again MAC address "
3777                                     "%02x:%02x:%02x:%02x:%02x:%02x failed "
3778                                     "diag=%d",
3779                                     mac_addr->addr_bytes[0],
3780                                     mac_addr->addr_bytes[1],
3781                                     mac_addr->addr_bytes[2],
3782                                     mac_addr->addr_bytes[3],
3783                                     mac_addr->addr_bytes[4],
3784                                     mac_addr->addr_bytes[5],
3785                                     diag);
3786         }
3787 }
3788
3789 #define MAC_TYPE_FILTER_SUP(type)    do {\
3790         if ((type) != ixgbe_mac_82599EB && (type) != ixgbe_mac_X540 &&\
3791                 (type) != ixgbe_mac_X550)\
3792                 return -ENOTSUP;\
3793 } while (0)
3794
3795 static int
3796 ixgbe_syn_filter_set(struct rte_eth_dev *dev,
3797                         struct rte_eth_syn_filter *filter,
3798                         bool add)
3799 {
3800         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
3801         uint32_t synqf;
3802
3803         if (filter->queue >= IXGBE_MAX_RX_QUEUE_NUM)
3804                 return -EINVAL;
3805
3806         synqf = IXGBE_READ_REG(hw, IXGBE_SYNQF);
3807
3808         if (add) {
3809                 if (synqf & IXGBE_SYN_FILTER_ENABLE)
3810                         return -EINVAL;
3811                 synqf = (uint32_t)(((filter->queue << IXGBE_SYN_FILTER_QUEUE_SHIFT) &
3812                         IXGBE_SYN_FILTER_QUEUE) | IXGBE_SYN_FILTER_ENABLE);
3813
3814                 if (filter->hig_pri)
3815                         synqf |= IXGBE_SYN_FILTER_SYNQFP;
3816                 else
3817                         synqf &= ~IXGBE_SYN_FILTER_SYNQFP;
3818         } else {
3819                 if (!(synqf & IXGBE_SYN_FILTER_ENABLE))
3820                         return -ENOENT;
3821                 synqf &= ~(IXGBE_SYN_FILTER_QUEUE | IXGBE_SYN_FILTER_ENABLE);
3822         }
3823         IXGBE_WRITE_REG(hw, IXGBE_SYNQF, synqf);
3824         IXGBE_WRITE_FLUSH(hw);
3825         return 0;
3826 }
3827
3828 static int
3829 ixgbe_syn_filter_get(struct rte_eth_dev *dev,
3830                         struct rte_eth_syn_filter *filter)
3831 {
3832         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
3833         uint32_t synqf = IXGBE_READ_REG(hw, IXGBE_SYNQF);
3834
3835         if (synqf & IXGBE_SYN_FILTER_ENABLE) {
3836                 filter->hig_pri = (synqf & IXGBE_SYN_FILTER_SYNQFP) ? 1 : 0;
3837                 filter->queue = (uint16_t)((synqf & IXGBE_SYN_FILTER_QUEUE) >> 1);
3838                 return 0;
3839         }
3840         return -ENOENT;
3841 }
3842
3843 static int
3844 ixgbe_syn_filter_handle(struct rte_eth_dev *dev,
3845                         enum rte_filter_op filter_op,
3846                         void *arg)
3847 {
3848         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
3849         int ret;
3850
3851         MAC_TYPE_FILTER_SUP(hw->mac.type);
3852
3853         if (filter_op == RTE_ETH_FILTER_NOP)
3854                 return 0;
3855
3856         if (arg == NULL) {
3857                 PMD_DRV_LOG(ERR, "arg shouldn't be NULL for operation %u",
3858                             filter_op);
3859                 return -EINVAL;
3860         }
3861
3862         switch (filter_op) {
3863         case RTE_ETH_FILTER_ADD:
3864                 ret = ixgbe_syn_filter_set(dev,
3865                                 (struct rte_eth_syn_filter *)arg,
3866                                 TRUE);
3867                 break;
3868         case RTE_ETH_FILTER_DELETE:
3869                 ret = ixgbe_syn_filter_set(dev,
3870                                 (struct rte_eth_syn_filter *)arg,
3871                                 FALSE);
3872                 break;
3873         case RTE_ETH_FILTER_GET:
3874                 ret = ixgbe_syn_filter_get(dev,
3875                                 (struct rte_eth_syn_filter *)arg);
3876                 break;
3877         default:
3878                 PMD_DRV_LOG(ERR, "unsupported operation %u\n", filter_op);
3879                 ret = -EINVAL;
3880                 break;
3881         }
3882
3883         return ret;
3884 }
3885
3886
3887 static inline enum ixgbe_5tuple_protocol
3888 convert_protocol_type(uint8_t protocol_value)
3889 {
3890         if (protocol_value == IPPROTO_TCP)
3891                 return IXGBE_FILTER_PROTOCOL_TCP;
3892         else if (protocol_value == IPPROTO_UDP)
3893                 return IXGBE_FILTER_PROTOCOL_UDP;
3894         else if (protocol_value == IPPROTO_SCTP)
3895                 return IXGBE_FILTER_PROTOCOL_SCTP;
3896         else
3897                 return IXGBE_FILTER_PROTOCOL_NONE;
3898 }
3899
3900 /*
3901  * add a 5tuple filter
3902  *
3903  * @param
3904  * dev: Pointer to struct rte_eth_dev.
3905  * index: the index the filter allocates.
3906  * filter: ponter to the filter that will be added.
3907  * rx_queue: the queue id the filter assigned to.
3908  *
3909  * @return
3910  *    - On success, zero.
3911  *    - On failure, a negative value.
3912  */
3913 static int
3914 ixgbe_add_5tuple_filter(struct rte_eth_dev *dev,
3915                         struct ixgbe_5tuple_filter *filter)
3916 {
3917         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
3918         struct ixgbe_filter_info *filter_info =
3919                 IXGBE_DEV_PRIVATE_TO_FILTER_INFO(dev->data->dev_private);
3920         int i, idx, shift;
3921         uint32_t ftqf, sdpqf;
3922         uint32_t l34timir = 0;
3923         uint8_t mask = 0xff;
3924
3925         /*
3926          * look for an unused 5tuple filter index,
3927          * and insert the filter to list.
3928          */
3929         for (i = 0; i < IXGBE_MAX_FTQF_FILTERS; i++) {
3930                 idx = i / (sizeof(uint32_t) * NBBY);
3931                 shift = i % (sizeof(uint32_t) * NBBY);
3932                 if (!(filter_info->fivetuple_mask[idx] & (1 << shift))) {
3933                         filter_info->fivetuple_mask[idx] |= 1 << shift;
3934                         filter->index = i;
3935                         TAILQ_INSERT_TAIL(&filter_info->fivetuple_list,
3936                                           filter,
3937                                           entries);
3938                         break;
3939                 }
3940         }
3941         if (i >= IXGBE_MAX_FTQF_FILTERS) {
3942                 PMD_DRV_LOG(ERR, "5tuple filters are full.");
3943                 return -ENOSYS;
3944         }
3945
3946         sdpqf = (uint32_t)(filter->filter_info.dst_port <<
3947                                 IXGBE_SDPQF_DSTPORT_SHIFT);
3948         sdpqf = sdpqf | (filter->filter_info.src_port & IXGBE_SDPQF_SRCPORT);
3949
3950         ftqf = (uint32_t)(filter->filter_info.proto &
3951                 IXGBE_FTQF_PROTOCOL_MASK);
3952         ftqf |= (uint32_t)((filter->filter_info.priority &
3953                 IXGBE_FTQF_PRIORITY_MASK) << IXGBE_FTQF_PRIORITY_SHIFT);
3954         if (filter->filter_info.src_ip_mask == 0) /* 0 means compare. */
3955                 mask &= IXGBE_FTQF_SOURCE_ADDR_MASK;
3956         if (filter->filter_info.dst_ip_mask == 0)
3957                 mask &= IXGBE_FTQF_DEST_ADDR_MASK;
3958         if (filter->filter_info.src_port_mask == 0)
3959                 mask &= IXGBE_FTQF_SOURCE_PORT_MASK;
3960         if (filter->filter_info.dst_port_mask == 0)
3961                 mask &= IXGBE_FTQF_DEST_PORT_MASK;
3962         if (filter->filter_info.proto_mask == 0)
3963                 mask &= IXGBE_FTQF_PROTOCOL_COMP_MASK;
3964         ftqf |= mask << IXGBE_FTQF_5TUPLE_MASK_SHIFT;
3965         ftqf |= IXGBE_FTQF_POOL_MASK_EN;
3966         ftqf |= IXGBE_FTQF_QUEUE_ENABLE;
3967
3968         IXGBE_WRITE_REG(hw, IXGBE_DAQF(i), filter->filter_info.dst_ip);
3969         IXGBE_WRITE_REG(hw, IXGBE_SAQF(i), filter->filter_info.src_ip);
3970         IXGBE_WRITE_REG(hw, IXGBE_SDPQF(i), sdpqf);
3971         IXGBE_WRITE_REG(hw, IXGBE_FTQF(i), ftqf);
3972
3973         l34timir |= IXGBE_L34T_IMIR_RESERVE;
3974         l34timir |= (uint32_t)(filter->queue <<
3975                                 IXGBE_L34T_IMIR_QUEUE_SHIFT);
3976         IXGBE_WRITE_REG(hw, IXGBE_L34T_IMIR(i), l34timir);
3977         return 0;
3978 }
3979
3980 /*
3981  * remove a 5tuple filter
3982  *
3983  * @param
3984  * dev: Pointer to struct rte_eth_dev.
3985  * filter: the pointer of the filter will be removed.
3986  */
3987 static void
3988 ixgbe_remove_5tuple_filter(struct rte_eth_dev *dev,
3989                         struct ixgbe_5tuple_filter *filter)
3990 {
3991         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
3992         struct ixgbe_filter_info *filter_info =
3993                 IXGBE_DEV_PRIVATE_TO_FILTER_INFO(dev->data->dev_private);
3994         uint16_t index = filter->index;
3995
3996         filter_info->fivetuple_mask[index / (sizeof(uint32_t) * NBBY)] &=
3997                                 ~(1 << (index % (sizeof(uint32_t) * NBBY)));
3998         TAILQ_REMOVE(&filter_info->fivetuple_list, filter, entries);
3999         rte_free(filter);
4000
4001         IXGBE_WRITE_REG(hw, IXGBE_DAQF(index), 0);
4002         IXGBE_WRITE_REG(hw, IXGBE_SAQF(index), 0);
4003         IXGBE_WRITE_REG(hw, IXGBE_SDPQF(index), 0);
4004         IXGBE_WRITE_REG(hw, IXGBE_FTQF(index), 0);
4005         IXGBE_WRITE_REG(hw, IXGBE_L34T_IMIR(index), 0);
4006 }
4007
4008 static int
4009 ixgbevf_dev_set_mtu(struct rte_eth_dev *dev, uint16_t mtu)
4010 {
4011         struct ixgbe_hw *hw;
4012         uint32_t max_frame = mtu + ETHER_HDR_LEN + ETHER_CRC_LEN;
4013
4014         hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
4015
4016         if ((mtu < ETHER_MIN_MTU) || (max_frame > ETHER_MAX_JUMBO_FRAME_LEN))
4017                 return -EINVAL;
4018
4019         /* refuse mtu that requires the support of scattered packets when this
4020          * feature has not been enabled before. */
4021         if (!dev->data->scattered_rx &&
4022             (max_frame + 2 * IXGBE_VLAN_TAG_SIZE >
4023              dev->data->min_rx_buf_size - RTE_PKTMBUF_HEADROOM))
4024                 return -EINVAL;
4025
4026         /*
4027          * When supported by the underlying PF driver, use the IXGBE_VF_SET_MTU
4028          * request of the version 2.0 of the mailbox API.
4029          * For now, use the IXGBE_VF_SET_LPE request of the version 1.0
4030          * of the mailbox API.
4031          * This call to IXGBE_SET_LPE action won't work with ixgbe pf drivers
4032          * prior to 3.11.33 which contains the following change:
4033          * "ixgbe: Enable jumbo frames support w/ SR-IOV"
4034          */
4035         ixgbevf_rlpml_set_vf(hw, max_frame);
4036
4037         /* update max frame size */
4038         dev->data->dev_conf.rxmode.max_rx_pkt_len = max_frame;
4039         return 0;
4040 }
4041
4042 #define MAC_TYPE_FILTER_SUP_EXT(type)    do {\
4043         if ((type) != ixgbe_mac_82599EB && (type) != ixgbe_mac_X540)\
4044                 return -ENOTSUP;\
4045 } while (0)
4046
4047 static inline struct ixgbe_5tuple_filter *
4048 ixgbe_5tuple_filter_lookup(struct ixgbe_5tuple_filter_list *filter_list,
4049                         struct ixgbe_5tuple_filter_info *key)
4050 {
4051         struct ixgbe_5tuple_filter *it;
4052
4053         TAILQ_FOREACH(it, filter_list, entries) {
4054                 if (memcmp(key, &it->filter_info,
4055                         sizeof(struct ixgbe_5tuple_filter_info)) == 0) {
4056                         return it;
4057                 }
4058         }
4059         return NULL;
4060 }
4061
4062 /* translate elements in struct rte_eth_ntuple_filter to struct ixgbe_5tuple_filter_info*/
4063 static inline int
4064 ntuple_filter_to_5tuple(struct rte_eth_ntuple_filter *filter,
4065                         struct ixgbe_5tuple_filter_info *filter_info)
4066 {
4067         if (filter->queue >= IXGBE_MAX_RX_QUEUE_NUM ||
4068                 filter->priority > IXGBE_5TUPLE_MAX_PRI ||
4069                 filter->priority < IXGBE_5TUPLE_MIN_PRI)
4070                 return -EINVAL;
4071
4072         switch (filter->dst_ip_mask) {
4073         case UINT32_MAX:
4074                 filter_info->dst_ip_mask = 0;
4075                 filter_info->dst_ip = filter->dst_ip;
4076                 break;
4077         case 0:
4078                 filter_info->dst_ip_mask = 1;
4079                 break;
4080         default:
4081                 PMD_DRV_LOG(ERR, "invalid dst_ip mask.");
4082                 return -EINVAL;
4083         }
4084
4085         switch (filter->src_ip_mask) {
4086         case UINT32_MAX:
4087                 filter_info->src_ip_mask = 0;
4088                 filter_info->src_ip = filter->src_ip;
4089                 break;
4090         case 0:
4091                 filter_info->src_ip_mask = 1;
4092                 break;
4093         default:
4094                 PMD_DRV_LOG(ERR, "invalid src_ip mask.");
4095                 return -EINVAL;
4096         }
4097
4098         switch (filter->dst_port_mask) {
4099         case UINT16_MAX:
4100                 filter_info->dst_port_mask = 0;
4101                 filter_info->dst_port = filter->dst_port;
4102                 break;
4103         case 0:
4104                 filter_info->dst_port_mask = 1;
4105                 break;
4106         default:
4107                 PMD_DRV_LOG(ERR, "invalid dst_port mask.");
4108                 return -EINVAL;
4109         }
4110
4111         switch (filter->src_port_mask) {
4112         case UINT16_MAX:
4113                 filter_info->src_port_mask = 0;
4114                 filter_info->src_port = filter->src_port;
4115                 break;
4116         case 0:
4117                 filter_info->src_port_mask = 1;
4118                 break;
4119         default:
4120                 PMD_DRV_LOG(ERR, "invalid src_port mask.");
4121                 return -EINVAL;
4122         }
4123
4124         switch (filter->proto_mask) {
4125         case UINT8_MAX:
4126                 filter_info->proto_mask = 0;
4127                 filter_info->proto =
4128                         convert_protocol_type(filter->proto);
4129                 break;
4130         case 0:
4131                 filter_info->proto_mask = 1;
4132                 break;
4133         default:
4134                 PMD_DRV_LOG(ERR, "invalid protocol mask.");
4135                 return -EINVAL;
4136         }
4137
4138         filter_info->priority = (uint8_t)filter->priority;
4139         return 0;
4140 }
4141
4142 /*
4143  * add or delete a ntuple filter
4144  *
4145  * @param
4146  * dev: Pointer to struct rte_eth_dev.
4147  * ntuple_filter: Pointer to struct rte_eth_ntuple_filter
4148  * add: if true, add filter, if false, remove filter
4149  *
4150  * @return
4151  *    - On success, zero.
4152  *    - On failure, a negative value.
4153  */
4154 static int
4155 ixgbe_add_del_ntuple_filter(struct rte_eth_dev *dev,
4156                         struct rte_eth_ntuple_filter *ntuple_filter,
4157                         bool add)
4158 {
4159         struct ixgbe_filter_info *filter_info =
4160                 IXGBE_DEV_PRIVATE_TO_FILTER_INFO(dev->data->dev_private);
4161         struct ixgbe_5tuple_filter_info filter_5tuple;
4162         struct ixgbe_5tuple_filter *filter;
4163         int ret;
4164
4165         if (ntuple_filter->flags != RTE_5TUPLE_FLAGS) {
4166                 PMD_DRV_LOG(ERR, "only 5tuple is supported.");
4167                 return -EINVAL;
4168         }
4169
4170         memset(&filter_5tuple, 0, sizeof(struct ixgbe_5tuple_filter_info));
4171         ret = ntuple_filter_to_5tuple(ntuple_filter, &filter_5tuple);
4172         if (ret < 0)
4173                 return ret;
4174
4175         filter = ixgbe_5tuple_filter_lookup(&filter_info->fivetuple_list,
4176                                          &filter_5tuple);
4177         if (filter != NULL && add) {
4178                 PMD_DRV_LOG(ERR, "filter exists.");
4179                 return -EEXIST;
4180         }
4181         if (filter == NULL && !add) {
4182                 PMD_DRV_LOG(ERR, "filter doesn't exist.");
4183                 return -ENOENT;
4184         }
4185
4186         if (add) {
4187                 filter = rte_zmalloc("ixgbe_5tuple_filter",
4188                                 sizeof(struct ixgbe_5tuple_filter), 0);
4189                 if (filter == NULL)
4190                         return -ENOMEM;
4191                 (void)rte_memcpy(&filter->filter_info,
4192                                  &filter_5tuple,
4193                                  sizeof(struct ixgbe_5tuple_filter_info));
4194                 filter->queue = ntuple_filter->queue;
4195                 ret = ixgbe_add_5tuple_filter(dev, filter);
4196                 if (ret < 0) {
4197                         rte_free(filter);
4198                         return ret;
4199                 }
4200         } else
4201                 ixgbe_remove_5tuple_filter(dev, filter);
4202
4203         return 0;
4204 }
4205
4206 /*
4207  * get a ntuple filter
4208  *
4209  * @param
4210  * dev: Pointer to struct rte_eth_dev.
4211  * ntuple_filter: Pointer to struct rte_eth_ntuple_filter
4212  *
4213  * @return
4214  *    - On success, zero.
4215  *    - On failure, a negative value.
4216  */
4217 static int
4218 ixgbe_get_ntuple_filter(struct rte_eth_dev *dev,
4219                         struct rte_eth_ntuple_filter *ntuple_filter)
4220 {
4221         struct ixgbe_filter_info *filter_info =
4222                 IXGBE_DEV_PRIVATE_TO_FILTER_INFO(dev->data->dev_private);
4223         struct ixgbe_5tuple_filter_info filter_5tuple;
4224         struct ixgbe_5tuple_filter *filter;
4225         int ret;
4226
4227         if (ntuple_filter->flags != RTE_5TUPLE_FLAGS) {
4228                 PMD_DRV_LOG(ERR, "only 5tuple is supported.");
4229                 return -EINVAL;
4230         }
4231
4232         memset(&filter_5tuple, 0, sizeof(struct ixgbe_5tuple_filter_info));
4233         ret = ntuple_filter_to_5tuple(ntuple_filter, &filter_5tuple);
4234         if (ret < 0)
4235                 return ret;
4236
4237         filter = ixgbe_5tuple_filter_lookup(&filter_info->fivetuple_list,
4238                                          &filter_5tuple);
4239         if (filter == NULL) {
4240                 PMD_DRV_LOG(ERR, "filter doesn't exist.");
4241                 return -ENOENT;
4242         }
4243         ntuple_filter->queue = filter->queue;
4244         return 0;
4245 }
4246
4247 /*
4248  * ixgbe_ntuple_filter_handle - Handle operations for ntuple filter.
4249  * @dev: pointer to rte_eth_dev structure
4250  * @filter_op:operation will be taken.
4251  * @arg: a pointer to specific structure corresponding to the filter_op
4252  *
4253  * @return
4254  *    - On success, zero.
4255  *    - On failure, a negative value.
4256  */
4257 static int
4258 ixgbe_ntuple_filter_handle(struct rte_eth_dev *dev,
4259                                 enum rte_filter_op filter_op,
4260                                 void *arg)
4261 {
4262         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
4263         int ret;
4264
4265         MAC_TYPE_FILTER_SUP_EXT(hw->mac.type);
4266
4267         if (filter_op == RTE_ETH_FILTER_NOP)
4268                 return 0;
4269
4270         if (arg == NULL) {
4271                 PMD_DRV_LOG(ERR, "arg shouldn't be NULL for operation %u.",
4272                             filter_op);
4273                 return -EINVAL;
4274         }
4275
4276         switch (filter_op) {
4277         case RTE_ETH_FILTER_ADD:
4278                 ret = ixgbe_add_del_ntuple_filter(dev,
4279                         (struct rte_eth_ntuple_filter *)arg,
4280                         TRUE);
4281                 break;
4282         case RTE_ETH_FILTER_DELETE:
4283                 ret = ixgbe_add_del_ntuple_filter(dev,
4284                         (struct rte_eth_ntuple_filter *)arg,
4285                         FALSE);
4286                 break;
4287         case RTE_ETH_FILTER_GET:
4288                 ret = ixgbe_get_ntuple_filter(dev,
4289                         (struct rte_eth_ntuple_filter *)arg);
4290                 break;
4291         default:
4292                 PMD_DRV_LOG(ERR, "unsupported operation %u.", filter_op);
4293                 ret = -EINVAL;
4294                 break;
4295         }
4296         return ret;
4297 }
4298
4299 static inline int
4300 ixgbe_ethertype_filter_lookup(struct ixgbe_filter_info *filter_info,
4301                         uint16_t ethertype)
4302 {
4303         int i;
4304
4305         for (i = 0; i < IXGBE_MAX_ETQF_FILTERS; i++) {
4306                 if (filter_info->ethertype_filters[i] == ethertype &&
4307                     (filter_info->ethertype_mask & (1 << i)))
4308                         return i;
4309         }
4310         return -1;
4311 }
4312
4313 static inline int
4314 ixgbe_ethertype_filter_insert(struct ixgbe_filter_info *filter_info,
4315                         uint16_t ethertype)
4316 {
4317         int i;
4318
4319         for (i = 0; i < IXGBE_MAX_ETQF_FILTERS; i++) {
4320                 if (!(filter_info->ethertype_mask & (1 << i))) {
4321                         filter_info->ethertype_mask |= 1 << i;
4322                         filter_info->ethertype_filters[i] = ethertype;
4323                         return i;
4324                 }
4325         }
4326         return -1;
4327 }
4328
4329 static inline int
4330 ixgbe_ethertype_filter_remove(struct ixgbe_filter_info *filter_info,
4331                         uint8_t idx)
4332 {
4333         if (idx >= IXGBE_MAX_ETQF_FILTERS)
4334                 return -1;
4335         filter_info->ethertype_mask &= ~(1 << idx);
4336         filter_info->ethertype_filters[idx] = 0;
4337         return idx;
4338 }
4339
4340 static int
4341 ixgbe_add_del_ethertype_filter(struct rte_eth_dev *dev,
4342                         struct rte_eth_ethertype_filter *filter,
4343                         bool add)
4344 {
4345         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
4346         struct ixgbe_filter_info *filter_info =
4347                 IXGBE_DEV_PRIVATE_TO_FILTER_INFO(dev->data->dev_private);
4348         uint32_t etqf = 0;
4349         uint32_t etqs = 0;
4350         int ret;
4351
4352         if (filter->queue >= IXGBE_MAX_RX_QUEUE_NUM)
4353                 return -EINVAL;
4354
4355         if (filter->ether_type == ETHER_TYPE_IPv4 ||
4356                 filter->ether_type == ETHER_TYPE_IPv6) {
4357                 PMD_DRV_LOG(ERR, "unsupported ether_type(0x%04x) in"
4358                         " ethertype filter.", filter->ether_type);
4359                 return -EINVAL;
4360         }
4361
4362         if (filter->flags & RTE_ETHTYPE_FLAGS_MAC) {
4363                 PMD_DRV_LOG(ERR, "mac compare is unsupported.");
4364                 return -EINVAL;
4365         }
4366         if (filter->flags & RTE_ETHTYPE_FLAGS_DROP) {
4367                 PMD_DRV_LOG(ERR, "drop option is unsupported.");
4368                 return -EINVAL;
4369         }
4370
4371         ret = ixgbe_ethertype_filter_lookup(filter_info, filter->ether_type);
4372         if (ret >= 0 && add) {
4373                 PMD_DRV_LOG(ERR, "ethertype (0x%04x) filter exists.",
4374                             filter->ether_type);
4375                 return -EEXIST;
4376         }
4377         if (ret < 0 && !add) {
4378                 PMD_DRV_LOG(ERR, "ethertype (0x%04x) filter doesn't exist.",
4379                             filter->ether_type);
4380                 return -ENOENT;
4381         }
4382
4383         if (add) {
4384                 ret = ixgbe_ethertype_filter_insert(filter_info,
4385                         filter->ether_type);
4386                 if (ret < 0) {
4387                         PMD_DRV_LOG(ERR, "ethertype filters are full.");
4388                         return -ENOSYS;
4389                 }
4390                 etqf = IXGBE_ETQF_FILTER_EN;
4391                 etqf |= (uint32_t)filter->ether_type;
4392                 etqs |= (uint32_t)((filter->queue <<
4393                                     IXGBE_ETQS_RX_QUEUE_SHIFT) &
4394                                     IXGBE_ETQS_RX_QUEUE);
4395                 etqs |= IXGBE_ETQS_QUEUE_EN;
4396         } else {
4397                 ret = ixgbe_ethertype_filter_remove(filter_info, (uint8_t)ret);
4398                 if (ret < 0)
4399                         return -ENOSYS;
4400         }
4401         IXGBE_WRITE_REG(hw, IXGBE_ETQF(ret), etqf);
4402         IXGBE_WRITE_REG(hw, IXGBE_ETQS(ret), etqs);
4403         IXGBE_WRITE_FLUSH(hw);
4404
4405         return 0;
4406 }
4407
4408 static int
4409 ixgbe_get_ethertype_filter(struct rte_eth_dev *dev,
4410                         struct rte_eth_ethertype_filter *filter)
4411 {
4412         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
4413         struct ixgbe_filter_info *filter_info =
4414                 IXGBE_DEV_PRIVATE_TO_FILTER_INFO(dev->data->dev_private);
4415         uint32_t etqf, etqs;
4416         int ret;
4417
4418         ret = ixgbe_ethertype_filter_lookup(filter_info, filter->ether_type);
4419         if (ret < 0) {
4420                 PMD_DRV_LOG(ERR, "ethertype (0x%04x) filter doesn't exist.",
4421                             filter->ether_type);
4422                 return -ENOENT;
4423         }
4424
4425         etqf = IXGBE_READ_REG(hw, IXGBE_ETQF(ret));
4426         if (etqf & IXGBE_ETQF_FILTER_EN) {
4427                 etqs = IXGBE_READ_REG(hw, IXGBE_ETQS(ret));
4428                 filter->ether_type = etqf & IXGBE_ETQF_ETHERTYPE;
4429                 filter->flags = 0;
4430                 filter->queue = (etqs & IXGBE_ETQS_RX_QUEUE) >>
4431                                IXGBE_ETQS_RX_QUEUE_SHIFT;
4432                 return 0;
4433         }
4434         return -ENOENT;
4435 }
4436
4437 /*
4438  * ixgbe_ethertype_filter_handle - Handle operations for ethertype filter.
4439  * @dev: pointer to rte_eth_dev structure
4440  * @filter_op:operation will be taken.
4441  * @arg: a pointer to specific structure corresponding to the filter_op
4442  */
4443 static int
4444 ixgbe_ethertype_filter_handle(struct rte_eth_dev *dev,
4445                                 enum rte_filter_op filter_op,
4446                                 void *arg)
4447 {
4448         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
4449         int ret;
4450
4451         MAC_TYPE_FILTER_SUP(hw->mac.type);
4452
4453         if (filter_op == RTE_ETH_FILTER_NOP)
4454                 return 0;
4455
4456         if (arg == NULL) {
4457                 PMD_DRV_LOG(ERR, "arg shouldn't be NULL for operation %u.",
4458                             filter_op);
4459                 return -EINVAL;
4460         }
4461
4462         switch (filter_op) {
4463         case RTE_ETH_FILTER_ADD:
4464                 ret = ixgbe_add_del_ethertype_filter(dev,
4465                         (struct rte_eth_ethertype_filter *)arg,
4466                         TRUE);
4467                 break;
4468         case RTE_ETH_FILTER_DELETE:
4469                 ret = ixgbe_add_del_ethertype_filter(dev,
4470                         (struct rte_eth_ethertype_filter *)arg,
4471                         FALSE);
4472                 break;
4473         case RTE_ETH_FILTER_GET:
4474                 ret = ixgbe_get_ethertype_filter(dev,
4475                         (struct rte_eth_ethertype_filter *)arg);
4476                 break;
4477         default:
4478                 PMD_DRV_LOG(ERR, "unsupported operation %u.", filter_op);
4479                 ret = -EINVAL;
4480                 break;
4481         }
4482         return ret;
4483 }
4484
4485 static int
4486 ixgbe_dev_filter_ctrl(struct rte_eth_dev *dev,
4487                      enum rte_filter_type filter_type,
4488                      enum rte_filter_op filter_op,
4489                      void *arg)
4490 {
4491         int ret = -EINVAL;
4492
4493         switch (filter_type) {
4494         case RTE_ETH_FILTER_NTUPLE:
4495                 ret = ixgbe_ntuple_filter_handle(dev, filter_op, arg);
4496                 break;
4497         case RTE_ETH_FILTER_ETHERTYPE:
4498                 ret = ixgbe_ethertype_filter_handle(dev, filter_op, arg);
4499                 break;
4500         case RTE_ETH_FILTER_SYN:
4501                 ret = ixgbe_syn_filter_handle(dev, filter_op, arg);
4502                 break;
4503         case RTE_ETH_FILTER_FDIR:
4504                 ret = ixgbe_fdir_ctrl_func(dev, filter_op, arg);
4505                 break;
4506         default:
4507                 PMD_DRV_LOG(WARNING, "Filter type (%d) not supported",
4508                                                         filter_type);
4509                 break;
4510         }
4511
4512         return ret;
4513 }
4514
4515 static u8 *
4516 ixgbe_dev_addr_list_itr(__attribute__((unused)) struct ixgbe_hw *hw,
4517                         u8 **mc_addr_ptr, u32 *vmdq)
4518 {
4519         u8 *mc_addr;
4520
4521         *vmdq = 0;
4522         mc_addr = *mc_addr_ptr;
4523         *mc_addr_ptr = (mc_addr + sizeof(struct ether_addr));
4524         return mc_addr;
4525 }
4526
4527 static int
4528 ixgbe_dev_set_mc_addr_list(struct rte_eth_dev *dev,
4529                            struct ether_addr *mc_addr_set,
4530                            uint32_t nb_mc_addr)
4531 {
4532         struct ixgbe_hw *hw;
4533         u8 *mc_addr_list;
4534
4535         hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
4536         mc_addr_list = (u8 *)mc_addr_set;
4537         return ixgbe_update_mc_addr_list(hw, mc_addr_list, nb_mc_addr,
4538                                          ixgbe_dev_addr_list_itr, TRUE);
4539 }
4540
4541 static int
4542 ixgbe_timesync_enable(struct rte_eth_dev *dev)
4543 {
4544         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
4545         uint32_t tsync_ctl;
4546         uint32_t tsauxc;
4547
4548         /* Enable system time for platforms where it isn't on by default. */
4549         tsauxc = IXGBE_READ_REG(hw, IXGBE_TSAUXC);
4550         tsauxc &= ~IXGBE_TSAUXC_DISABLE_SYSTIME;
4551         IXGBE_WRITE_REG(hw, IXGBE_TSAUXC, tsauxc);
4552
4553         /* Start incrementing the register used to timestamp PTP packets. */
4554         IXGBE_WRITE_REG(hw, IXGBE_TIMINCA, IXGBE_TIMINCA_INIT);
4555
4556         /* Enable L2 filtering of IEEE1588/802.1AS Ethernet frame types. */
4557         IXGBE_WRITE_REG(hw, IXGBE_ETQF(IXGBE_ETQF_FILTER_1588),
4558                         (ETHER_TYPE_1588 |
4559                          IXGBE_ETQF_FILTER_EN |
4560                          IXGBE_ETQF_1588));
4561
4562         /* Enable timestamping of received PTP packets. */
4563         tsync_ctl = IXGBE_READ_REG(hw, IXGBE_TSYNCRXCTL);
4564         tsync_ctl |= IXGBE_TSYNCRXCTL_ENABLED;
4565         IXGBE_WRITE_REG(hw, IXGBE_TSYNCRXCTL, tsync_ctl);
4566
4567         /* Enable timestamping of transmitted PTP packets. */
4568         tsync_ctl = IXGBE_READ_REG(hw, IXGBE_TSYNCTXCTL);
4569         tsync_ctl |= IXGBE_TSYNCTXCTL_ENABLED;
4570         IXGBE_WRITE_REG(hw, IXGBE_TSYNCTXCTL, tsync_ctl);
4571
4572         return 0;
4573 }
4574
4575 static int
4576 ixgbe_timesync_disable(struct rte_eth_dev *dev)
4577 {
4578         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
4579         uint32_t tsync_ctl;
4580
4581         /* Disable timestamping of transmitted PTP packets. */
4582         tsync_ctl = IXGBE_READ_REG(hw, IXGBE_TSYNCTXCTL);
4583         tsync_ctl &= ~IXGBE_TSYNCTXCTL_ENABLED;
4584         IXGBE_WRITE_REG(hw, IXGBE_TSYNCTXCTL, tsync_ctl);
4585
4586         /* Disable timestamping of received PTP packets. */
4587         tsync_ctl = IXGBE_READ_REG(hw, IXGBE_TSYNCRXCTL);
4588         tsync_ctl &= ~IXGBE_TSYNCRXCTL_ENABLED;
4589         IXGBE_WRITE_REG(hw, IXGBE_TSYNCRXCTL, tsync_ctl);
4590
4591         /* Disable L2 filtering of IEEE1588/802.1AS Ethernet frame types. */
4592         IXGBE_WRITE_REG(hw, IXGBE_ETQF(IXGBE_ETQF_FILTER_1588), 0);
4593
4594         /* Stop incrementating the System Time registers. */
4595         IXGBE_WRITE_REG(hw, IXGBE_TIMINCA, 0);
4596
4597         return 0;
4598 }
4599
4600 static int
4601 ixgbe_timesync_read_rx_timestamp(struct rte_eth_dev *dev,
4602                                  struct timespec *timestamp,
4603                                  uint32_t flags __rte_unused)
4604 {
4605         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
4606         uint32_t tsync_rxctl;
4607         uint32_t rx_stmpl;
4608         uint32_t rx_stmph;
4609
4610         tsync_rxctl = IXGBE_READ_REG(hw, IXGBE_TSYNCRXCTL);
4611         if ((tsync_rxctl & IXGBE_TSYNCRXCTL_VALID) == 0)
4612                 return -EINVAL;
4613
4614         rx_stmpl = IXGBE_READ_REG(hw, IXGBE_RXSTMPL);
4615         rx_stmph = IXGBE_READ_REG(hw, IXGBE_RXSTMPH);
4616
4617         timestamp->tv_sec = (uint64_t)(((uint64_t)rx_stmph << 32) | rx_stmpl);
4618         timestamp->tv_nsec = 0;
4619
4620         return  0;
4621 }
4622
4623 static int
4624 ixgbe_timesync_read_tx_timestamp(struct rte_eth_dev *dev,
4625                                  struct timespec *timestamp)
4626 {
4627         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
4628         uint32_t tsync_txctl;
4629         uint32_t tx_stmpl;
4630         uint32_t tx_stmph;
4631
4632         tsync_txctl = IXGBE_READ_REG(hw, IXGBE_TSYNCTXCTL);
4633         if ((tsync_txctl & IXGBE_TSYNCTXCTL_VALID) == 0)
4634                 return -EINVAL;
4635
4636         tx_stmpl = IXGBE_READ_REG(hw, IXGBE_TXSTMPL);
4637         tx_stmph = IXGBE_READ_REG(hw, IXGBE_TXSTMPH);
4638
4639         timestamp->tv_sec = (uint64_t)(((uint64_t)tx_stmph << 32) | tx_stmpl);
4640         timestamp->tv_nsec = 0;
4641
4642         return  0;
4643 }
4644
4645 static struct rte_driver rte_ixgbe_driver = {
4646         .type = PMD_PDEV,
4647         .init = rte_ixgbe_pmd_init,
4648 };
4649
4650 static struct rte_driver rte_ixgbevf_driver = {
4651         .type = PMD_PDEV,
4652         .init = rte_ixgbevf_pmd_init,
4653 };
4654
4655 PMD_REGISTER_DRIVER(rte_ixgbe_driver);
4656 PMD_REGISTER_DRIVER(rte_ixgbevf_driver);