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