Home | History | Annotate | Line # | Download | only in ixgbe
ixgbe.c revision 1.42
      1   1.1    dyoung /******************************************************************************
      2   1.1    dyoung 
      3  1.28   msaitoh   Copyright (c) 2001-2013, Intel Corporation
      4   1.1    dyoung   All rights reserved.
      5   1.1    dyoung 
      6   1.1    dyoung   Redistribution and use in source and binary forms, with or without
      7   1.1    dyoung   modification, are permitted provided that the following conditions are met:
      8   1.1    dyoung 
      9   1.1    dyoung    1. Redistributions of source code must retain the above copyright notice,
     10   1.1    dyoung       this list of conditions and the following disclaimer.
     11   1.1    dyoung 
     12   1.1    dyoung    2. Redistributions in binary form must reproduce the above copyright
     13   1.1    dyoung       notice, this list of conditions and the following disclaimer in the
     14   1.1    dyoung       documentation and/or other materials provided with the distribution.
     15   1.1    dyoung 
     16   1.1    dyoung    3. Neither the name of the Intel Corporation nor the names of its
     17   1.1    dyoung       contributors may be used to endorse or promote products derived from
     18   1.1    dyoung       this software without specific prior written permission.
     19   1.1    dyoung 
     20   1.1    dyoung   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
     21   1.1    dyoung   AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     22   1.1    dyoung   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     23   1.1    dyoung   ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
     24   1.1    dyoung   LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25   1.1    dyoung   CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26   1.1    dyoung   SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27   1.1    dyoung   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28   1.1    dyoung   CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29   1.1    dyoung   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30   1.1    dyoung   POSSIBILITY OF SUCH DAMAGE.
     31   1.1    dyoung 
     32   1.1    dyoung ******************************************************************************/
     33   1.1    dyoung /*
     34   1.1    dyoung  * Copyright (c) 2011 The NetBSD Foundation, Inc.
     35   1.1    dyoung  * All rights reserved.
     36   1.1    dyoung  *
     37   1.1    dyoung  * This code is derived from software contributed to The NetBSD Foundation
     38   1.1    dyoung  * by Coyote Point Systems, Inc.
     39   1.1    dyoung  *
     40   1.1    dyoung  * Redistribution and use in source and binary forms, with or without
     41   1.1    dyoung  * modification, are permitted provided that the following conditions
     42   1.1    dyoung  * are met:
     43   1.1    dyoung  * 1. Redistributions of source code must retain the above copyright
     44   1.1    dyoung  *    notice, this list of conditions and the following disclaimer.
     45   1.1    dyoung  * 2. Redistributions in binary form must reproduce the above copyright
     46   1.1    dyoung  *    notice, this list of conditions and the following disclaimer in the
     47   1.1    dyoung  *    documentation and/or other materials provided with the distribution.
     48   1.1    dyoung  *
     49   1.1    dyoung  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     50   1.1    dyoung  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     51   1.1    dyoung  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     52   1.1    dyoung  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     53   1.1    dyoung  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     54   1.1    dyoung  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     55   1.1    dyoung  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     56   1.1    dyoung  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     57   1.1    dyoung  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     58   1.1    dyoung  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     59   1.1    dyoung  * POSSIBILITY OF SUCH DAMAGE.
     60   1.1    dyoung  */
     61  1.33   msaitoh /*$FreeBSD: head/sys/dev/ixgbe/ixgbe.c 279805 2015-03-09 10:29:15Z araujo $*/
     62  1.42   msaitoh /*$NetBSD: ixgbe.c,v 1.42 2016/11/28 02:23:33 msaitoh Exp $*/
     63   1.1    dyoung 
     64   1.1    dyoung #include "opt_inet.h"
     65  1.22   msaitoh #include "opt_inet6.h"
     66   1.1    dyoung 
     67   1.1    dyoung #include "ixgbe.h"
     68  1.29   msaitoh #include "vlan.h"
     69   1.1    dyoung 
     70  1.33   msaitoh #include <sys/cprng.h>
     71  1.33   msaitoh 
     72   1.1    dyoung /*********************************************************************
     73   1.1    dyoung  *  Set this to one to display debug statistics
     74   1.1    dyoung  *********************************************************************/
     75   1.1    dyoung int             ixgbe_display_debug_stats = 0;
     76   1.1    dyoung 
     77   1.1    dyoung /*********************************************************************
     78   1.1    dyoung  *  Driver version
     79   1.1    dyoung  *********************************************************************/
     80  1.33   msaitoh char ixgbe_driver_version[] = "2.5.15";
     81   1.1    dyoung 
     82   1.1    dyoung /*********************************************************************
     83   1.1    dyoung  *  PCI Device ID Table
     84   1.1    dyoung  *
     85   1.1    dyoung  *  Used by probe to select devices to load on
     86   1.1    dyoung  *  Last field stores an index into ixgbe_strings
     87   1.1    dyoung  *  Last entry must be all 0s
     88   1.1    dyoung  *
     89   1.1    dyoung  *  { Vendor ID, Device ID, SubVendor ID, SubDevice ID, String Index }
     90   1.1    dyoung  *********************************************************************/
     91   1.1    dyoung 
     92   1.1    dyoung static ixgbe_vendor_info_t ixgbe_vendor_info_array[] =
     93   1.1    dyoung {
     94   1.1    dyoung 	{IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82598AF_DUAL_PORT, 0, 0, 0},
     95   1.1    dyoung 	{IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82598AF_SINGLE_PORT, 0, 0, 0},
     96   1.1    dyoung 	{IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82598EB_CX4, 0, 0, 0},
     97   1.1    dyoung 	{IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82598AT, 0, 0, 0},
     98   1.1    dyoung 	{IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82598AT2, 0, 0, 0},
     99   1.1    dyoung 	{IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82598, 0, 0, 0},
    100   1.1    dyoung 	{IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82598_DA_DUAL_PORT, 0, 0, 0},
    101   1.1    dyoung 	{IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82598_CX4_DUAL_PORT, 0, 0, 0},
    102   1.1    dyoung 	{IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82598EB_XF_LR, 0, 0, 0},
    103   1.1    dyoung 	{IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82598_SR_DUAL_PORT_EM, 0, 0, 0},
    104   1.1    dyoung 	{IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82598EB_SFP_LOM, 0, 0, 0},
    105   1.1    dyoung 	{IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82599_KX4, 0, 0, 0},
    106   1.1    dyoung 	{IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82599_KX4_MEZZ, 0, 0, 0},
    107   1.1    dyoung 	{IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82599_SFP, 0, 0, 0},
    108   1.1    dyoung 	{IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82599_XAUI_LOM, 0, 0, 0},
    109   1.1    dyoung 	{IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82599_CX4, 0, 0, 0},
    110   1.1    dyoung 	{IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82599_T3_LOM, 0, 0, 0},
    111   1.1    dyoung 	{IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82599_COMBO_BACKPLANE, 0, 0, 0},
    112   1.1    dyoung 	{IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82599_BACKPLANE_FCOE, 0, 0, 0},
    113  1.21   msaitoh 	{IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82599_SFP_SF2, 0, 0, 0},
    114   1.1    dyoung 	{IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82599_SFP_FCOE, 0, 0, 0},
    115  1.21   msaitoh 	{IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82599EN_SFP, 0, 0, 0},
    116  1.21   msaitoh 	{IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_82599_SFP_SF_QP, 0, 0, 0},
    117  1.24   msaitoh 	{IXGBE_INTEL_VENDOR_ID, IXGBE_DEV_ID_X540T, 0, 0, 0},
    118   1.1    dyoung 	/* required last entry */
    119   1.1    dyoung 	{0, 0, 0, 0, 0}
    120   1.1    dyoung };
    121   1.1    dyoung 
    122   1.1    dyoung /*********************************************************************
    123   1.1    dyoung  *  Table of branding strings
    124   1.1    dyoung  *********************************************************************/
    125   1.1    dyoung 
    126   1.1    dyoung static const char    *ixgbe_strings[] = {
    127   1.1    dyoung 	"Intel(R) PRO/10GbE PCI-Express Network Driver"
    128   1.1    dyoung };
    129   1.1    dyoung 
    130   1.1    dyoung /*********************************************************************
    131   1.1    dyoung  *  Function prototypes
    132   1.1    dyoung  *********************************************************************/
    133   1.1    dyoung static int      ixgbe_probe(device_t, cfdata_t, void *);
    134   1.1    dyoung static void     ixgbe_attach(device_t, device_t, void *);
    135   1.1    dyoung static int      ixgbe_detach(device_t, int);
    136   1.1    dyoung #if 0
    137   1.1    dyoung static int      ixgbe_shutdown(device_t);
    138   1.1    dyoung #endif
    139   1.1    dyoung static int      ixgbe_ioctl(struct ifnet *, u_long, void *);
    140   1.1    dyoung static void	ixgbe_ifstop(struct ifnet *, int);
    141   1.1    dyoung static int	ixgbe_init(struct ifnet *);
    142   1.1    dyoung static void	ixgbe_init_locked(struct adapter *);
    143   1.1    dyoung static void     ixgbe_stop(void *);
    144   1.1    dyoung static void     ixgbe_media_status(struct ifnet *, struct ifmediareq *);
    145   1.1    dyoung static int      ixgbe_media_change(struct ifnet *);
    146   1.1    dyoung static void     ixgbe_identify_hardware(struct adapter *);
    147   1.1    dyoung static int      ixgbe_allocate_pci_resources(struct adapter *,
    148   1.1    dyoung 		    const struct pci_attach_args *);
    149  1.33   msaitoh static void	ixgbe_get_slot_info(struct ixgbe_hw *);
    150   1.1    dyoung static int      ixgbe_allocate_msix(struct adapter *,
    151   1.1    dyoung 		    const struct pci_attach_args *);
    152   1.1    dyoung static int      ixgbe_allocate_legacy(struct adapter *,
    153   1.1    dyoung 		    const struct pci_attach_args *);
    154   1.1    dyoung static int	ixgbe_setup_msix(struct adapter *);
    155   1.1    dyoung static void	ixgbe_free_pci_resources(struct adapter *);
    156   1.1    dyoung static void	ixgbe_local_timer(void *);
    157   1.1    dyoung static int	ixgbe_setup_interface(device_t, struct adapter *);
    158   1.1    dyoung static void	ixgbe_config_link(struct adapter *);
    159   1.1    dyoung 
    160   1.1    dyoung static void     ixgbe_initialize_transmit_units(struct adapter *);
    161   1.1    dyoung 
    162  1.42   msaitoh 
    163   1.1    dyoung static void     ixgbe_initialize_receive_units(struct adapter *);
    164   1.1    dyoung 
    165   1.1    dyoung static void     ixgbe_enable_intr(struct adapter *);
    166   1.1    dyoung static void     ixgbe_disable_intr(struct adapter *);
    167   1.1    dyoung static void     ixgbe_update_stats_counters(struct adapter *);
    168   1.1    dyoung static void     ixgbe_set_promisc(struct adapter *);
    169   1.1    dyoung static void     ixgbe_set_multi(struct adapter *);
    170   1.1    dyoung static void     ixgbe_update_link_status(struct adapter *);
    171   1.1    dyoung static int	ixgbe_set_flowcntl(SYSCTLFN_PROTO);
    172   1.1    dyoung static int	ixgbe_set_advertise(SYSCTLFN_PROTO);
    173  1.24   msaitoh static int	ixgbe_set_thermal_test(SYSCTLFN_PROTO);
    174   1.1    dyoung static void	ixgbe_set_ivar(struct adapter *, u8, u8, s8);
    175   1.1    dyoung static void	ixgbe_configure_ivars(struct adapter *);
    176   1.1    dyoung static u8 *	ixgbe_mc_array_itr(struct ixgbe_hw *, u8 **, u32 *);
    177   1.1    dyoung 
    178   1.1    dyoung static void	ixgbe_setup_vlan_hw_support(struct adapter *);
    179   1.1    dyoung #if 0
    180   1.1    dyoung static void	ixgbe_register_vlan(void *, struct ifnet *, u16);
    181   1.1    dyoung static void	ixgbe_unregister_vlan(void *, struct ifnet *, u16);
    182   1.1    dyoung #endif
    183   1.1    dyoung 
    184   1.1    dyoung static void     ixgbe_add_hw_stats(struct adapter *adapter);
    185   1.1    dyoung 
    186  1.26   msaitoh static void	ixgbe_enable_rx_drop(struct adapter *);
    187  1.26   msaitoh static void	ixgbe_disable_rx_drop(struct adapter *);
    188  1.26   msaitoh 
    189   1.1    dyoung /* Support for pluggable optic modules */
    190   1.1    dyoung static bool	ixgbe_sfp_probe(struct adapter *);
    191   1.1    dyoung static void	ixgbe_setup_optics(struct adapter *);
    192   1.1    dyoung 
    193   1.1    dyoung /* Legacy (single vector interrupt handler */
    194   1.1    dyoung static int	ixgbe_legacy_irq(void *);
    195   1.1    dyoung 
    196   1.1    dyoung /* The MSI/X Interrupt handlers */
    197  1.34   msaitoh static int	ixgbe_msix_que(void *);
    198  1.34   msaitoh static int	ixgbe_msix_link(void *);
    199   1.1    dyoung 
    200   1.1    dyoung /* Software interrupts for deferred work */
    201   1.1    dyoung static void	ixgbe_handle_que(void *);
    202   1.1    dyoung static void	ixgbe_handle_link(void *);
    203   1.1    dyoung static void	ixgbe_handle_msf(void *);
    204   1.1    dyoung static void	ixgbe_handle_mod(void *);
    205   1.1    dyoung 
    206   1.1    dyoung const struct sysctlnode *ixgbe_sysctl_instance(struct adapter *);
    207   1.1    dyoung static ixgbe_vendor_info_t *ixgbe_lookup(const struct pci_attach_args *);
    208   1.1    dyoung 
    209   1.1    dyoung #ifdef IXGBE_FDIR
    210   1.1    dyoung static void	ixgbe_reinit_fdir(void *, int);
    211   1.1    dyoung #endif
    212   1.1    dyoung 
    213  1.33   msaitoh /* Missing shared code prototype */
    214  1.33   msaitoh extern void ixgbe_stop_mac_link_on_d3_82599(struct ixgbe_hw *hw);
    215  1.33   msaitoh 
    216   1.1    dyoung /*********************************************************************
    217   1.1    dyoung  *  FreeBSD Device Interface Entry Points
    218   1.1    dyoung  *********************************************************************/
    219   1.1    dyoung 
    220   1.1    dyoung CFATTACH_DECL3_NEW(ixg, sizeof(struct adapter),
    221   1.1    dyoung     ixgbe_probe, ixgbe_attach, ixgbe_detach, NULL, NULL, NULL,
    222   1.1    dyoung     DVF_DETACH_SHUTDOWN);
    223   1.1    dyoung 
    224   1.1    dyoung #if 0
    225   1.1    dyoung devclass_t ixgbe_devclass;
    226   1.1    dyoung DRIVER_MODULE(ixgbe, pci, ixgbe_driver, ixgbe_devclass, 0, 0);
    227   1.1    dyoung 
    228   1.1    dyoung MODULE_DEPEND(ixgbe, pci, 1, 1, 1);
    229   1.1    dyoung MODULE_DEPEND(ixgbe, ether, 1, 1, 1);
    230   1.1    dyoung #endif
    231   1.1    dyoung 
    232   1.1    dyoung /*
    233   1.1    dyoung ** TUNEABLE PARAMETERS:
    234   1.1    dyoung */
    235   1.1    dyoung 
    236   1.1    dyoung /*
    237   1.1    dyoung ** AIM: Adaptive Interrupt Moderation
    238   1.1    dyoung ** which means that the interrupt rate
    239   1.1    dyoung ** is varied over time based on the
    240   1.1    dyoung ** traffic for that interrupt vector
    241   1.1    dyoung */
    242   1.1    dyoung static int ixgbe_enable_aim = TRUE;
    243  1.33   msaitoh #define SYSCTL_INT(__x, __y)
    244  1.33   msaitoh SYSCTL_INT("hw.ixgbe.enable_aim", &ixgbe_enable_aim);
    245   1.1    dyoung 
    246  1.22   msaitoh static int ixgbe_max_interrupt_rate = (4000000 / IXGBE_LOW_LATENCY);
    247  1.33   msaitoh SYSCTL_INT("hw.ixgbe.max_interrupt_rate", &ixgbe_max_interrupt_rate);
    248   1.1    dyoung 
    249   1.1    dyoung /* How many packets rxeof tries to clean at a time */
    250   1.1    dyoung static int ixgbe_rx_process_limit = 256;
    251  1.33   msaitoh SYSCTL_INT("hw.ixgbe.rx_process_limit", &ixgbe_rx_process_limit);
    252   1.1    dyoung 
    253  1.28   msaitoh /* How many packets txeof tries to clean at a time */
    254  1.28   msaitoh static int ixgbe_tx_process_limit = 256;
    255  1.33   msaitoh SYSCTL_INT("hw.ixgbe.tx_process_limit", &ixgbe_tx_process_limit);
    256  1.28   msaitoh 
    257   1.1    dyoung /*
    258   1.1    dyoung ** Smart speed setting, default to on
    259   1.1    dyoung ** this only works as a compile option
    260   1.1    dyoung ** right now as its during attach, set
    261   1.1    dyoung ** this to 'ixgbe_smart_speed_off' to
    262   1.1    dyoung ** disable.
    263   1.1    dyoung */
    264   1.1    dyoung static int ixgbe_smart_speed = ixgbe_smart_speed_on;
    265   1.1    dyoung 
    266   1.1    dyoung /*
    267   1.1    dyoung  * MSIX should be the default for best performance,
    268   1.1    dyoung  * but this allows it to be forced off for testing.
    269   1.1    dyoung  */
    270   1.1    dyoung static int ixgbe_enable_msix = 1;
    271  1.33   msaitoh SYSCTL_INT("hw.ixgbe.enable_msix", &ixgbe_enable_msix);
    272   1.1    dyoung 
    273   1.1    dyoung /*
    274   1.1    dyoung  * Number of Queues, can be set to 0,
    275   1.1    dyoung  * it then autoconfigures based on the
    276   1.1    dyoung  * number of cpus with a max of 8. This
    277   1.1    dyoung  * can be overriden manually here.
    278   1.1    dyoung  */
    279  1.34   msaitoh static int ixgbe_num_queues = 1;
    280  1.33   msaitoh SYSCTL_INT("hw.ixgbe.num_queues", &ixgbe_num_queues);
    281   1.1    dyoung 
    282   1.1    dyoung /*
    283   1.1    dyoung ** Number of TX descriptors per ring,
    284   1.1    dyoung ** setting higher than RX as this seems
    285   1.1    dyoung ** the better performing choice.
    286   1.1    dyoung */
    287   1.1    dyoung static int ixgbe_txd = PERFORM_TXD;
    288  1.33   msaitoh SYSCTL_INT("hw.ixgbe.txd", &ixgbe_txd);
    289   1.1    dyoung 
    290   1.1    dyoung /* Number of RX descriptors per ring */
    291   1.1    dyoung static int ixgbe_rxd = PERFORM_RXD;
    292  1.33   msaitoh SYSCTL_INT("hw.ixgbe.rxd", &ixgbe_rxd);
    293  1.33   msaitoh 
    294  1.33   msaitoh /*
    295  1.33   msaitoh ** Defining this on will allow the use
    296  1.33   msaitoh ** of unsupported SFP+ modules, note that
    297  1.33   msaitoh ** doing so you are on your own :)
    298  1.33   msaitoh */
    299  1.35   msaitoh static int allow_unsupported_sfp = false;
    300  1.33   msaitoh SYSCTL_INT("hw.ix.unsupported_sfp", &allow_unsupported_sfp);
    301   1.1    dyoung 
    302   1.1    dyoung /* Keep running tab on them for sanity check */
    303   1.1    dyoung static int ixgbe_total_ports;
    304   1.1    dyoung 
    305   1.1    dyoung #ifdef IXGBE_FDIR
    306   1.1    dyoung /*
    307   1.1    dyoung ** For Flow Director: this is the
    308   1.1    dyoung ** number of TX packets we sample
    309   1.1    dyoung ** for the filter pool, this means
    310   1.1    dyoung ** every 20th packet will be probed.
    311   1.1    dyoung **
    312   1.1    dyoung ** This feature can be disabled by
    313   1.1    dyoung ** setting this to 0.
    314   1.1    dyoung */
    315   1.1    dyoung static int atr_sample_rate = 20;
    316   1.1    dyoung /*
    317   1.1    dyoung ** Flow Director actually 'steals'
    318   1.1    dyoung ** part of the packet buffer as its
    319   1.1    dyoung ** filter pool, this variable controls
    320   1.1    dyoung ** how much it uses:
    321   1.1    dyoung **  0 = 64K, 1 = 128K, 2 = 256K
    322   1.1    dyoung */
    323   1.1    dyoung static int fdir_pballoc = 1;
    324   1.1    dyoung #endif
    325   1.1    dyoung 
    326  1.22   msaitoh #ifdef DEV_NETMAP
    327  1.22   msaitoh /*
    328  1.22   msaitoh  * The #ifdef DEV_NETMAP / #endif blocks in this file are meant to
    329  1.22   msaitoh  * be a reference on how to implement netmap support in a driver.
    330  1.22   msaitoh  * Additional comments are in ixgbe_netmap.h .
    331  1.22   msaitoh  *
    332  1.25   msaitoh  * <dev/netmap/ixgbe_netmap.h> contains functions for netmap support
    333  1.22   msaitoh  * that extend the standard driver.
    334  1.22   msaitoh  */
    335  1.22   msaitoh #include <dev/netmap/ixgbe_netmap.h>
    336  1.22   msaitoh #endif /* DEV_NETMAP */
    337  1.22   msaitoh 
    338   1.1    dyoung /*********************************************************************
    339   1.1    dyoung  *  Device identification routine
    340   1.1    dyoung  *
    341   1.1    dyoung  *  ixgbe_probe determines if the driver should be loaded on
    342   1.1    dyoung  *  adapter based on PCI vendor/device id of the adapter.
    343   1.1    dyoung  *
    344   1.1    dyoung  *  return 1 on success, 0 on failure
    345   1.1    dyoung  *********************************************************************/
    346   1.1    dyoung 
    347   1.1    dyoung static int
    348   1.1    dyoung ixgbe_probe(device_t dev, cfdata_t cf, void *aux)
    349   1.1    dyoung {
    350   1.1    dyoung 	const struct pci_attach_args *pa = aux;
    351   1.1    dyoung 
    352   1.1    dyoung 	return (ixgbe_lookup(pa) != NULL) ? 1 : 0;
    353   1.1    dyoung }
    354   1.1    dyoung 
    355   1.1    dyoung static ixgbe_vendor_info_t *
    356   1.1    dyoung ixgbe_lookup(const struct pci_attach_args *pa)
    357   1.1    dyoung {
    358   1.1    dyoung 	pcireg_t subid;
    359   1.1    dyoung 	ixgbe_vendor_info_t *ent;
    360   1.1    dyoung 
    361   1.1    dyoung 	INIT_DEBUGOUT("ixgbe_probe: begin");
    362   1.1    dyoung 
    363   1.1    dyoung 	if (PCI_VENDOR(pa->pa_id) != IXGBE_INTEL_VENDOR_ID)
    364   1.1    dyoung 		return NULL;
    365   1.1    dyoung 
    366   1.1    dyoung 	subid = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_SUBSYS_ID_REG);
    367   1.1    dyoung 
    368   1.1    dyoung 	for (ent = ixgbe_vendor_info_array; ent->vendor_id != 0; ent++) {
    369   1.1    dyoung 		if (PCI_VENDOR(pa->pa_id) == ent->vendor_id &&
    370   1.1    dyoung 		    PCI_PRODUCT(pa->pa_id) == ent->device_id &&
    371   1.1    dyoung 
    372   1.1    dyoung 		    (PCI_SUBSYS_VENDOR(subid) == ent->subvendor_id ||
    373   1.1    dyoung 		     ent->subvendor_id == 0) &&
    374   1.1    dyoung 
    375   1.1    dyoung 		    (PCI_SUBSYS_ID(subid) == ent->subdevice_id ||
    376   1.1    dyoung 		     ent->subdevice_id == 0)) {
    377   1.1    dyoung 			++ixgbe_total_ports;
    378   1.1    dyoung 			return ent;
    379   1.1    dyoung 		}
    380   1.1    dyoung 	}
    381   1.1    dyoung 	return NULL;
    382   1.1    dyoung }
    383   1.1    dyoung 
    384   1.1    dyoung 
    385   1.1    dyoung static void
    386   1.1    dyoung ixgbe_sysctl_attach(struct adapter *adapter)
    387   1.1    dyoung {
    388   1.1    dyoung 	struct sysctllog **log;
    389   1.1    dyoung 	const struct sysctlnode *rnode, *cnode;
    390   1.1    dyoung 	device_t dev;
    391   1.1    dyoung 
    392   1.1    dyoung 	dev = adapter->dev;
    393   1.1    dyoung 	log = &adapter->sysctllog;
    394   1.1    dyoung 
    395   1.1    dyoung 	if ((rnode = ixgbe_sysctl_instance(adapter)) == NULL) {
    396   1.1    dyoung 		aprint_error_dev(dev, "could not create sysctl root\n");
    397   1.1    dyoung 		return;
    398   1.1    dyoung 	}
    399   1.1    dyoung 
    400   1.1    dyoung 	if (sysctl_createv(log, 0, &rnode, &cnode,
    401   1.1    dyoung 	    CTLFLAG_READONLY, CTLTYPE_INT,
    402   1.1    dyoung 	    "num_rx_desc", SYSCTL_DESCR("Number of rx descriptors"),
    403   1.1    dyoung 	    NULL, 0, &adapter->num_rx_desc, 0, CTL_CREATE, CTL_EOL) != 0)
    404   1.1    dyoung 		aprint_error_dev(dev, "could not create sysctl\n");
    405   1.1    dyoung 
    406   1.1    dyoung 	if (sysctl_createv(log, 0, &rnode, &cnode,
    407   1.1    dyoung 	    CTLFLAG_READONLY, CTLTYPE_INT,
    408   1.1    dyoung 	    "num_queues", SYSCTL_DESCR("Number of queues"),
    409   1.1    dyoung 	    NULL, 0, &adapter->num_queues, 0, CTL_CREATE, CTL_EOL) != 0)
    410   1.1    dyoung 		aprint_error_dev(dev, "could not create sysctl\n");
    411   1.1    dyoung 
    412   1.1    dyoung 	if (sysctl_createv(log, 0, &rnode, &cnode,
    413   1.1    dyoung 	    CTLFLAG_READWRITE, CTLTYPE_INT,
    414  1.24   msaitoh 	    "fc", SYSCTL_DESCR("Flow Control"),
    415   1.3       dsl 	    ixgbe_set_flowcntl, 0, (void *)adapter, 0, CTL_CREATE, CTL_EOL) != 0)
    416   1.1    dyoung 		aprint_error_dev(dev, "could not create sysctl\n");
    417   1.1    dyoung 
    418  1.24   msaitoh 	/* XXX This is an *instance* sysctl controlling a *global* variable.
    419  1.24   msaitoh 	 * XXX It's that way in the FreeBSD driver that this derives from.
    420  1.24   msaitoh 	 */
    421   1.1    dyoung 	if (sysctl_createv(log, 0, &rnode, &cnode,
    422   1.1    dyoung 	    CTLFLAG_READWRITE, CTLTYPE_INT,
    423  1.24   msaitoh 	    "enable_aim", SYSCTL_DESCR("Interrupt Moderation"),
    424  1.24   msaitoh 	    NULL, 0, &ixgbe_enable_aim, 0, CTL_CREATE, CTL_EOL) != 0)
    425  1.24   msaitoh 		aprint_error_dev(dev, "could not create sysctl\n");
    426  1.24   msaitoh 
    427  1.24   msaitoh 	if (sysctl_createv(log, 0, &rnode, &cnode,
    428  1.24   msaitoh 	    CTLFLAG_READWRITE, CTLTYPE_INT,
    429  1.24   msaitoh 	    "advertise_speed", SYSCTL_DESCR("Link Speed"),
    430   1.3       dsl 	    ixgbe_set_advertise, 0, (void *)adapter, 0, CTL_CREATE, CTL_EOL) != 0)
    431   1.1    dyoung 		aprint_error_dev(dev, "could not create sysctl\n");
    432   1.1    dyoung 
    433   1.1    dyoung 	if (sysctl_createv(log, 0, &rnode, &cnode,
    434   1.1    dyoung 	    CTLFLAG_READWRITE, CTLTYPE_INT,
    435  1.24   msaitoh 	    "ts", SYSCTL_DESCR("Thermal Test"),
    436  1.24   msaitoh 	    ixgbe_set_thermal_test, 0, (void *)adapter, 0, CTL_CREATE, CTL_EOL) != 0)
    437   1.1    dyoung 		aprint_error_dev(dev, "could not create sysctl\n");
    438   1.1    dyoung }
    439   1.1    dyoung 
    440   1.1    dyoung /*********************************************************************
    441   1.1    dyoung  *  Device initialization routine
    442   1.1    dyoung  *
    443   1.1    dyoung  *  The attach entry point is called when the driver is being loaded.
    444   1.1    dyoung  *  This routine identifies the type of hardware, allocates all resources
    445   1.1    dyoung  *  and initializes the hardware.
    446   1.1    dyoung  *
    447   1.1    dyoung  *  return 0 on success, positive on failure
    448   1.1    dyoung  *********************************************************************/
    449   1.1    dyoung 
    450   1.1    dyoung static void
    451   1.1    dyoung ixgbe_attach(device_t parent, device_t dev, void *aux)
    452   1.1    dyoung {
    453   1.1    dyoung 	struct adapter *adapter;
    454   1.1    dyoung 	struct ixgbe_hw *hw;
    455  1.34   msaitoh 	int             error = -1;
    456   1.1    dyoung 	u16		csum;
    457   1.1    dyoung 	u32		ctrl_ext;
    458   1.1    dyoung 	ixgbe_vendor_info_t *ent;
    459  1.41   msaitoh 	struct pci_attach_args *pa = aux;
    460   1.1    dyoung 
    461   1.1    dyoung 	INIT_DEBUGOUT("ixgbe_attach: begin");
    462   1.1    dyoung 
    463   1.1    dyoung 	/* Allocate, clear, and link in our adapter structure */
    464   1.1    dyoung 	adapter = device_private(dev);
    465   1.1    dyoung 	adapter->dev = adapter->osdep.dev = dev;
    466   1.1    dyoung 	hw = &adapter->hw;
    467   1.1    dyoung 	adapter->osdep.pc = pa->pa_pc;
    468   1.1    dyoung 	adapter->osdep.tag = pa->pa_tag;
    469   1.1    dyoung 	adapter->osdep.dmat = pa->pa_dmat;
    470  1.32   msaitoh 	adapter->osdep.attached = false;
    471   1.1    dyoung 
    472   1.1    dyoung 	ent = ixgbe_lookup(pa);
    473   1.1    dyoung 
    474   1.1    dyoung 	KASSERT(ent != NULL);
    475   1.1    dyoung 
    476   1.1    dyoung 	aprint_normal(": %s, Version - %s\n",
    477   1.1    dyoung 	    ixgbe_strings[ent->index], ixgbe_driver_version);
    478   1.1    dyoung 
    479   1.1    dyoung 	/* Core Lock Init*/
    480   1.1    dyoung 	IXGBE_CORE_LOCK_INIT(adapter, device_xname(dev));
    481   1.1    dyoung 
    482   1.1    dyoung 	/* SYSCTL APIs */
    483   1.1    dyoung 
    484   1.1    dyoung 	ixgbe_sysctl_attach(adapter);
    485   1.1    dyoung 
    486   1.1    dyoung 	/* Set up the timer callout */
    487   1.1    dyoung 	callout_init(&adapter->timer, 0);
    488   1.1    dyoung 
    489   1.1    dyoung 	/* Determine hardware revision */
    490   1.1    dyoung 	ixgbe_identify_hardware(adapter);
    491   1.1    dyoung 
    492   1.1    dyoung 	/* Do base PCI setup - map BAR0 */
    493   1.1    dyoung 	if (ixgbe_allocate_pci_resources(adapter, pa)) {
    494   1.1    dyoung 		aprint_error_dev(dev, "Allocation of PCI resources failed\n");
    495   1.1    dyoung 		error = ENXIO;
    496   1.1    dyoung 		goto err_out;
    497   1.1    dyoung 	}
    498   1.1    dyoung 
    499   1.1    dyoung 	/* Do descriptor calc and sanity checks */
    500   1.1    dyoung 	if (((ixgbe_txd * sizeof(union ixgbe_adv_tx_desc)) % DBA_ALIGN) != 0 ||
    501   1.1    dyoung 	    ixgbe_txd < MIN_TXD || ixgbe_txd > MAX_TXD) {
    502   1.1    dyoung 		aprint_error_dev(dev, "TXD config issue, using default!\n");
    503   1.1    dyoung 		adapter->num_tx_desc = DEFAULT_TXD;
    504   1.1    dyoung 	} else
    505   1.1    dyoung 		adapter->num_tx_desc = ixgbe_txd;
    506   1.1    dyoung 
    507   1.1    dyoung 	/*
    508   1.1    dyoung 	** With many RX rings it is easy to exceed the
    509   1.1    dyoung 	** system mbuf allocation. Tuning nmbclusters
    510   1.1    dyoung 	** can alleviate this.
    511   1.1    dyoung 	*/
    512   1.1    dyoung 	if (nmbclusters > 0 ) {
    513   1.1    dyoung 		int s;
    514   1.1    dyoung 		s = (ixgbe_rxd * adapter->num_queues) * ixgbe_total_ports;
    515   1.1    dyoung 		if (s > nmbclusters) {
    516   1.1    dyoung 			aprint_error_dev(dev, "RX Descriptors exceed "
    517   1.1    dyoung 			    "system mbuf max, using default instead!\n");
    518   1.1    dyoung 			ixgbe_rxd = DEFAULT_RXD;
    519   1.1    dyoung 		}
    520   1.1    dyoung 	}
    521   1.1    dyoung 
    522   1.1    dyoung 	if (((ixgbe_rxd * sizeof(union ixgbe_adv_rx_desc)) % DBA_ALIGN) != 0 ||
    523  1.33   msaitoh 	    ixgbe_rxd < MIN_RXD || ixgbe_rxd > MAX_RXD) {
    524   1.1    dyoung 		aprint_error_dev(dev, "RXD config issue, using default!\n");
    525   1.1    dyoung 		adapter->num_rx_desc = DEFAULT_RXD;
    526   1.1    dyoung 	} else
    527   1.1    dyoung 		adapter->num_rx_desc = ixgbe_rxd;
    528   1.1    dyoung 
    529   1.1    dyoung 	/* Allocate our TX/RX Queues */
    530   1.1    dyoung 	if (ixgbe_allocate_queues(adapter)) {
    531   1.1    dyoung 		error = ENOMEM;
    532   1.1    dyoung 		goto err_out;
    533   1.1    dyoung 	}
    534   1.1    dyoung 
    535   1.1    dyoung 	/* Allocate multicast array memory. */
    536   1.1    dyoung 	adapter->mta = malloc(sizeof(u8) * IXGBE_ETH_LENGTH_OF_ADDRESS *
    537   1.1    dyoung 	    MAX_NUM_MULTICAST_ADDRESSES, M_DEVBUF, M_NOWAIT);
    538   1.1    dyoung 	if (adapter->mta == NULL) {
    539   1.1    dyoung 		aprint_error_dev(dev, "Cannot allocate multicast setup array\n");
    540   1.1    dyoung 		error = ENOMEM;
    541   1.1    dyoung 		goto err_late;
    542   1.1    dyoung 	}
    543   1.1    dyoung 
    544   1.1    dyoung 	/* Initialize the shared code */
    545  1.33   msaitoh 	hw->allow_unsupported_sfp = allow_unsupported_sfp;
    546   1.1    dyoung 	error = ixgbe_init_shared_code(hw);
    547   1.1    dyoung 	if (error == IXGBE_ERR_SFP_NOT_PRESENT) {
    548   1.1    dyoung 		/*
    549   1.1    dyoung 		** No optics in this port, set up
    550   1.1    dyoung 		** so the timer routine will probe
    551   1.1    dyoung 		** for later insertion.
    552   1.1    dyoung 		*/
    553   1.1    dyoung 		adapter->sfp_probe = TRUE;
    554   1.1    dyoung 		error = 0;
    555  1.35   msaitoh 	} else if (error == IXGBE_ERR_SFP_NOT_SUPPORTED) {
    556   1.1    dyoung 		aprint_error_dev(dev,"Unsupported SFP+ module detected!\n");
    557   1.1    dyoung 		error = EIO;
    558   1.1    dyoung 		goto err_late;
    559   1.1    dyoung 	} else if (error) {
    560   1.1    dyoung 		aprint_error_dev(dev,"Unable to initialize the shared code\n");
    561   1.1    dyoung 		error = EIO;
    562   1.1    dyoung 		goto err_late;
    563   1.1    dyoung 	}
    564   1.1    dyoung 
    565   1.1    dyoung 	/* Make sure we have a good EEPROM before we read from it */
    566   1.1    dyoung 	if (ixgbe_validate_eeprom_checksum(&adapter->hw, &csum) < 0) {
    567   1.1    dyoung 		aprint_error_dev(dev,"The EEPROM Checksum Is Not Valid\n");
    568   1.1    dyoung 		error = EIO;
    569   1.1    dyoung 		goto err_late;
    570   1.1    dyoung 	}
    571   1.1    dyoung 
    572   1.1    dyoung 	error = ixgbe_init_hw(hw);
    573  1.25   msaitoh 	switch (error) {
    574  1.25   msaitoh 	case IXGBE_ERR_EEPROM_VERSION:
    575   1.1    dyoung 		aprint_error_dev(dev, "This device is a pre-production adapter/"
    576   1.1    dyoung 		    "LOM.  Please be aware there may be issues associated "
    577   1.1    dyoung 		    "with your hardware.\n If you are experiencing problems "
    578   1.1    dyoung 		    "please contact your Intel or hardware representative "
    579   1.1    dyoung 		    "who provided you with this hardware.\n");
    580  1.25   msaitoh 		break;
    581  1.25   msaitoh 	case IXGBE_ERR_SFP_NOT_SUPPORTED:
    582   1.1    dyoung 		aprint_error_dev(dev,"Unsupported SFP+ Module\n");
    583   1.1    dyoung 		error = EIO;
    584   1.1    dyoung 		aprint_error_dev(dev,"Hardware Initialization Failure\n");
    585   1.1    dyoung 		goto err_late;
    586  1.25   msaitoh 	case IXGBE_ERR_SFP_NOT_PRESENT:
    587  1.25   msaitoh 		device_printf(dev,"No SFP+ Module found\n");
    588  1.25   msaitoh 		/* falls thru */
    589  1.25   msaitoh 	default:
    590  1.25   msaitoh 		break;
    591   1.1    dyoung 	}
    592   1.1    dyoung 
    593   1.1    dyoung 	/* Detect and set physical type */
    594   1.1    dyoung 	ixgbe_setup_optics(adapter);
    595   1.1    dyoung 
    596  1.34   msaitoh 	error = -1;
    597   1.1    dyoung 	if ((adapter->msix > 1) && (ixgbe_enable_msix))
    598  1.34   msaitoh 		error = ixgbe_allocate_msix(adapter, pa);
    599  1.34   msaitoh 	if (error != 0)
    600  1.34   msaitoh 		error = ixgbe_allocate_legacy(adapter, pa);
    601   1.1    dyoung 	if (error)
    602   1.1    dyoung 		goto err_late;
    603   1.1    dyoung 
    604   1.1    dyoung 	/* Setup OS specific network interface */
    605   1.1    dyoung 	if (ixgbe_setup_interface(dev, adapter) != 0)
    606   1.1    dyoung 		goto err_late;
    607   1.1    dyoung 
    608   1.1    dyoung 	/* Initialize statistics */
    609   1.1    dyoung 	ixgbe_update_stats_counters(adapter);
    610   1.1    dyoung 
    611  1.33   msaitoh 	/*
    612  1.33   msaitoh 	** Check PCIE slot type/speed/width
    613  1.33   msaitoh 	*/
    614  1.33   msaitoh 	ixgbe_get_slot_info(hw);
    615   1.1    dyoung 
    616  1.28   msaitoh 	/* Set an initial default flow control value */
    617  1.28   msaitoh 	adapter->fc =  ixgbe_fc_full;
    618  1.28   msaitoh 
    619   1.1    dyoung 	/* let hardware know driver is loaded */
    620   1.1    dyoung 	ctrl_ext = IXGBE_READ_REG(hw, IXGBE_CTRL_EXT);
    621   1.1    dyoung 	ctrl_ext |= IXGBE_CTRL_EXT_DRV_LOAD;
    622   1.1    dyoung 	IXGBE_WRITE_REG(hw, IXGBE_CTRL_EXT, ctrl_ext);
    623   1.1    dyoung 
    624   1.1    dyoung 	ixgbe_add_hw_stats(adapter);
    625   1.1    dyoung 
    626  1.22   msaitoh #ifdef DEV_NETMAP
    627  1.22   msaitoh 	ixgbe_netmap_attach(adapter);
    628  1.22   msaitoh #endif /* DEV_NETMAP */
    629   1.1    dyoung 	INIT_DEBUGOUT("ixgbe_attach: end");
    630  1.32   msaitoh 	adapter->osdep.attached = true;
    631   1.1    dyoung 	return;
    632   1.1    dyoung err_late:
    633   1.1    dyoung 	ixgbe_free_transmit_structures(adapter);
    634   1.1    dyoung 	ixgbe_free_receive_structures(adapter);
    635   1.1    dyoung err_out:
    636   1.1    dyoung 	if (adapter->ifp != NULL)
    637   1.1    dyoung 		if_free(adapter->ifp);
    638   1.1    dyoung 	ixgbe_free_pci_resources(adapter);
    639   1.1    dyoung 	if (adapter->mta != NULL)
    640   1.1    dyoung 		free(adapter->mta, M_DEVBUF);
    641   1.1    dyoung 	return;
    642   1.1    dyoung 
    643   1.1    dyoung }
    644   1.1    dyoung 
    645   1.1    dyoung /*********************************************************************
    646   1.1    dyoung  *  Device removal routine
    647   1.1    dyoung  *
    648   1.1    dyoung  *  The detach entry point is called when the driver is being removed.
    649   1.1    dyoung  *  This routine stops the adapter and deallocates all the resources
    650   1.1    dyoung  *  that were allocated for driver operation.
    651   1.1    dyoung  *
    652   1.1    dyoung  *  return 0 on success, positive on failure
    653   1.1    dyoung  *********************************************************************/
    654   1.1    dyoung 
    655   1.1    dyoung static int
    656   1.1    dyoung ixgbe_detach(device_t dev, int flags)
    657   1.1    dyoung {
    658   1.1    dyoung 	struct adapter *adapter = device_private(dev);
    659   1.1    dyoung 	struct rx_ring *rxr = adapter->rx_rings;
    660   1.1    dyoung 	struct ixgbe_hw_stats *stats = &adapter->stats;
    661   1.1    dyoung 	struct ix_queue *que = adapter->queues;
    662  1.26   msaitoh 	struct tx_ring *txr = adapter->tx_rings;
    663   1.1    dyoung 	u32	ctrl_ext;
    664   1.1    dyoung 
    665   1.1    dyoung 	INIT_DEBUGOUT("ixgbe_detach: begin");
    666  1.32   msaitoh 	if (adapter->osdep.attached == false)
    667  1.32   msaitoh 		return 0;
    668   1.1    dyoung 
    669  1.29   msaitoh #if NVLAN > 0
    670   1.1    dyoung 	/* Make sure VLANs are not using driver */
    671   1.1    dyoung 	if (!VLAN_ATTACHED(&adapter->osdep.ec))
    672   1.1    dyoung 		;	/* nothing to do: no VLANs */
    673   1.1    dyoung 	else if ((flags & (DETACH_SHUTDOWN|DETACH_FORCE)) != 0)
    674   1.1    dyoung 		vlan_ifdetach(adapter->ifp);
    675   1.1    dyoung 	else {
    676   1.1    dyoung 		aprint_error_dev(dev, "VLANs in use\n");
    677   1.1    dyoung 		return EBUSY;
    678   1.1    dyoung 	}
    679  1.29   msaitoh #endif
    680   1.1    dyoung 
    681   1.1    dyoung 	IXGBE_CORE_LOCK(adapter);
    682   1.1    dyoung 	ixgbe_stop(adapter);
    683   1.1    dyoung 	IXGBE_CORE_UNLOCK(adapter);
    684   1.1    dyoung 
    685  1.26   msaitoh 	for (int i = 0; i < adapter->num_queues; i++, que++, txr++) {
    686  1.28   msaitoh #ifndef IXGBE_LEGACY_TX
    687  1.26   msaitoh 		softint_disestablish(txr->txq_si);
    688  1.26   msaitoh #endif
    689   1.1    dyoung 		softint_disestablish(que->que_si);
    690   1.1    dyoung 	}
    691   1.1    dyoung 
    692   1.1    dyoung 	/* Drain the Link queue */
    693   1.1    dyoung 	softint_disestablish(adapter->link_si);
    694   1.1    dyoung 	softint_disestablish(adapter->mod_si);
    695   1.1    dyoung 	softint_disestablish(adapter->msf_si);
    696   1.1    dyoung #ifdef IXGBE_FDIR
    697   1.1    dyoung 	softint_disestablish(adapter->fdir_si);
    698   1.1    dyoung #endif
    699   1.1    dyoung 
    700   1.1    dyoung 	/* let hardware know driver is unloading */
    701   1.1    dyoung 	ctrl_ext = IXGBE_READ_REG(&adapter->hw, IXGBE_CTRL_EXT);
    702   1.1    dyoung 	ctrl_ext &= ~IXGBE_CTRL_EXT_DRV_LOAD;
    703   1.1    dyoung 	IXGBE_WRITE_REG(&adapter->hw, IXGBE_CTRL_EXT, ctrl_ext);
    704   1.1    dyoung 
    705   1.1    dyoung 	ether_ifdetach(adapter->ifp);
    706   1.1    dyoung 	callout_halt(&adapter->timer, NULL);
    707  1.22   msaitoh #ifdef DEV_NETMAP
    708  1.22   msaitoh 	netmap_detach(adapter->ifp);
    709  1.22   msaitoh #endif /* DEV_NETMAP */
    710   1.1    dyoung 	ixgbe_free_pci_resources(adapter);
    711   1.1    dyoung #if 0	/* XXX the NetBSD port is probably missing something here */
    712   1.1    dyoung 	bus_generic_detach(dev);
    713   1.1    dyoung #endif
    714   1.1    dyoung 	if_detach(adapter->ifp);
    715   1.1    dyoung 
    716   1.1    dyoung 	sysctl_teardown(&adapter->sysctllog);
    717   1.1    dyoung 	evcnt_detach(&adapter->handleq);
    718   1.1    dyoung 	evcnt_detach(&adapter->req);
    719   1.1    dyoung 	evcnt_detach(&adapter->morerx);
    720   1.1    dyoung 	evcnt_detach(&adapter->moretx);
    721   1.1    dyoung 	evcnt_detach(&adapter->txloops);
    722   1.1    dyoung 	evcnt_detach(&adapter->efbig_tx_dma_setup);
    723   1.1    dyoung 	evcnt_detach(&adapter->m_defrag_failed);
    724   1.1    dyoung 	evcnt_detach(&adapter->efbig2_tx_dma_setup);
    725   1.1    dyoung 	evcnt_detach(&adapter->einval_tx_dma_setup);
    726   1.1    dyoung 	evcnt_detach(&adapter->other_tx_dma_setup);
    727   1.1    dyoung 	evcnt_detach(&adapter->eagain_tx_dma_setup);
    728   1.1    dyoung 	evcnt_detach(&adapter->enomem_tx_dma_setup);
    729   1.1    dyoung 	evcnt_detach(&adapter->watchdog_events);
    730   1.1    dyoung 	evcnt_detach(&adapter->tso_err);
    731   1.1    dyoung 	evcnt_detach(&adapter->link_irq);
    732  1.26   msaitoh 
    733  1.26   msaitoh 	txr = adapter->tx_rings;
    734   1.1    dyoung 	for (int i = 0; i < adapter->num_queues; i++, rxr++, txr++) {
    735   1.1    dyoung 		evcnt_detach(&txr->no_desc_avail);
    736   1.1    dyoung 		evcnt_detach(&txr->total_packets);
    737  1.28   msaitoh 		evcnt_detach(&txr->tso_tx);
    738   1.1    dyoung 
    739   1.1    dyoung 		if (i < __arraycount(adapter->stats.mpc)) {
    740   1.1    dyoung 			evcnt_detach(&adapter->stats.mpc[i]);
    741   1.1    dyoung 		}
    742   1.1    dyoung 		if (i < __arraycount(adapter->stats.pxontxc)) {
    743   1.1    dyoung 			evcnt_detach(&adapter->stats.pxontxc[i]);
    744   1.1    dyoung 			evcnt_detach(&adapter->stats.pxonrxc[i]);
    745   1.1    dyoung 			evcnt_detach(&adapter->stats.pxofftxc[i]);
    746   1.1    dyoung 			evcnt_detach(&adapter->stats.pxoffrxc[i]);
    747   1.1    dyoung 			evcnt_detach(&adapter->stats.pxon2offc[i]);
    748   1.1    dyoung 		}
    749   1.1    dyoung 		if (i < __arraycount(adapter->stats.qprc)) {
    750   1.1    dyoung 			evcnt_detach(&adapter->stats.qprc[i]);
    751   1.1    dyoung 			evcnt_detach(&adapter->stats.qptc[i]);
    752   1.1    dyoung 			evcnt_detach(&adapter->stats.qbrc[i]);
    753   1.1    dyoung 			evcnt_detach(&adapter->stats.qbtc[i]);
    754   1.1    dyoung 			evcnt_detach(&adapter->stats.qprdc[i]);
    755   1.1    dyoung 		}
    756   1.1    dyoung 
    757   1.1    dyoung 		evcnt_detach(&rxr->rx_packets);
    758   1.1    dyoung 		evcnt_detach(&rxr->rx_bytes);
    759  1.31   msaitoh 		evcnt_detach(&rxr->rx_copies);
    760   1.1    dyoung 		evcnt_detach(&rxr->no_jmbuf);
    761   1.1    dyoung 		evcnt_detach(&rxr->rx_discarded);
    762   1.1    dyoung 		evcnt_detach(&rxr->rx_irq);
    763   1.1    dyoung 	}
    764   1.1    dyoung 	evcnt_detach(&stats->ipcs);
    765   1.1    dyoung 	evcnt_detach(&stats->l4cs);
    766   1.1    dyoung 	evcnt_detach(&stats->ipcs_bad);
    767   1.1    dyoung 	evcnt_detach(&stats->l4cs_bad);
    768   1.1    dyoung 	evcnt_detach(&stats->intzero);
    769   1.1    dyoung 	evcnt_detach(&stats->legint);
    770   1.1    dyoung 	evcnt_detach(&stats->crcerrs);
    771   1.1    dyoung 	evcnt_detach(&stats->illerrc);
    772   1.1    dyoung 	evcnt_detach(&stats->errbc);
    773   1.1    dyoung 	evcnt_detach(&stats->mspdc);
    774   1.1    dyoung 	evcnt_detach(&stats->mlfc);
    775   1.1    dyoung 	evcnt_detach(&stats->mrfc);
    776   1.1    dyoung 	evcnt_detach(&stats->rlec);
    777   1.1    dyoung 	evcnt_detach(&stats->lxontxc);
    778   1.1    dyoung 	evcnt_detach(&stats->lxonrxc);
    779   1.1    dyoung 	evcnt_detach(&stats->lxofftxc);
    780   1.1    dyoung 	evcnt_detach(&stats->lxoffrxc);
    781   1.1    dyoung 
    782   1.1    dyoung 	/* Packet Reception Stats */
    783   1.1    dyoung 	evcnt_detach(&stats->tor);
    784   1.1    dyoung 	evcnt_detach(&stats->gorc);
    785   1.1    dyoung 	evcnt_detach(&stats->tpr);
    786   1.1    dyoung 	evcnt_detach(&stats->gprc);
    787   1.1    dyoung 	evcnt_detach(&stats->mprc);
    788   1.1    dyoung 	evcnt_detach(&stats->bprc);
    789   1.1    dyoung 	evcnt_detach(&stats->prc64);
    790   1.1    dyoung 	evcnt_detach(&stats->prc127);
    791   1.1    dyoung 	evcnt_detach(&stats->prc255);
    792   1.1    dyoung 	evcnt_detach(&stats->prc511);
    793   1.1    dyoung 	evcnt_detach(&stats->prc1023);
    794   1.1    dyoung 	evcnt_detach(&stats->prc1522);
    795   1.1    dyoung 	evcnt_detach(&stats->ruc);
    796   1.1    dyoung 	evcnt_detach(&stats->rfc);
    797   1.1    dyoung 	evcnt_detach(&stats->roc);
    798   1.1    dyoung 	evcnt_detach(&stats->rjc);
    799   1.1    dyoung 	evcnt_detach(&stats->mngprc);
    800   1.1    dyoung 	evcnt_detach(&stats->xec);
    801   1.1    dyoung 
    802   1.1    dyoung 	/* Packet Transmission Stats */
    803   1.1    dyoung 	evcnt_detach(&stats->gotc);
    804   1.1    dyoung 	evcnt_detach(&stats->tpt);
    805   1.1    dyoung 	evcnt_detach(&stats->gptc);
    806   1.1    dyoung 	evcnt_detach(&stats->bptc);
    807   1.1    dyoung 	evcnt_detach(&stats->mptc);
    808   1.1    dyoung 	evcnt_detach(&stats->mngptc);
    809   1.1    dyoung 	evcnt_detach(&stats->ptc64);
    810   1.1    dyoung 	evcnt_detach(&stats->ptc127);
    811   1.1    dyoung 	evcnt_detach(&stats->ptc255);
    812   1.1    dyoung 	evcnt_detach(&stats->ptc511);
    813   1.1    dyoung 	evcnt_detach(&stats->ptc1023);
    814   1.1    dyoung 	evcnt_detach(&stats->ptc1522);
    815   1.1    dyoung 
    816   1.1    dyoung 	ixgbe_free_transmit_structures(adapter);
    817   1.1    dyoung 	ixgbe_free_receive_structures(adapter);
    818   1.1    dyoung 	free(adapter->mta, M_DEVBUF);
    819   1.1    dyoung 
    820   1.1    dyoung 	IXGBE_CORE_LOCK_DESTROY(adapter);
    821   1.1    dyoung 	return (0);
    822   1.1    dyoung }
    823   1.1    dyoung 
    824   1.1    dyoung /*********************************************************************
    825   1.1    dyoung  *
    826   1.1    dyoung  *  Shutdown entry point
    827   1.1    dyoung  *
    828   1.1    dyoung  **********************************************************************/
    829   1.1    dyoung 
    830   1.1    dyoung #if 0 /* XXX NetBSD ought to register something like this through pmf(9) */
    831   1.1    dyoung static int
    832   1.1    dyoung ixgbe_shutdown(device_t dev)
    833   1.1    dyoung {
    834   1.1    dyoung 	struct adapter *adapter = device_private(dev);
    835   1.1    dyoung 	IXGBE_CORE_LOCK(adapter);
    836   1.1    dyoung 	ixgbe_stop(adapter);
    837   1.1    dyoung 	IXGBE_CORE_UNLOCK(adapter);
    838   1.1    dyoung 	return (0);
    839   1.1    dyoung }
    840   1.1    dyoung #endif
    841   1.1    dyoung 
    842   1.1    dyoung 
    843   1.1    dyoung static int
    844   1.1    dyoung ixgbe_ifflags_cb(struct ethercom *ec)
    845   1.1    dyoung {
    846   1.1    dyoung 	struct ifnet *ifp = &ec->ec_if;
    847   1.1    dyoung 	struct adapter *adapter = ifp->if_softc;
    848   1.1    dyoung 	int change = ifp->if_flags ^ adapter->if_flags, rc = 0;
    849   1.1    dyoung 
    850   1.1    dyoung 	IXGBE_CORE_LOCK(adapter);
    851   1.1    dyoung 
    852   1.1    dyoung 	if (change != 0)
    853   1.1    dyoung 		adapter->if_flags = ifp->if_flags;
    854   1.1    dyoung 
    855   1.1    dyoung 	if ((change & ~(IFF_CANTCHANGE|IFF_DEBUG)) != 0)
    856   1.1    dyoung 		rc = ENETRESET;
    857   1.1    dyoung 	else if ((change & (IFF_PROMISC | IFF_ALLMULTI)) != 0)
    858   1.1    dyoung 		ixgbe_set_promisc(adapter);
    859   1.1    dyoung 
    860  1.23   msaitoh 	/* Set up VLAN support and filter */
    861  1.23   msaitoh 	ixgbe_setup_vlan_hw_support(adapter);
    862  1.23   msaitoh 
    863   1.1    dyoung 	IXGBE_CORE_UNLOCK(adapter);
    864   1.1    dyoung 
    865   1.1    dyoung 	return rc;
    866   1.1    dyoung }
    867   1.1    dyoung 
    868   1.1    dyoung /*********************************************************************
    869   1.1    dyoung  *  Ioctl entry point
    870   1.1    dyoung  *
    871   1.1    dyoung  *  ixgbe_ioctl is called when the user wants to configure the
    872   1.1    dyoung  *  interface.
    873   1.1    dyoung  *
    874   1.1    dyoung  *  return 0 on success, positive on failure
    875   1.1    dyoung  **********************************************************************/
    876   1.1    dyoung 
    877   1.1    dyoung static int
    878   1.1    dyoung ixgbe_ioctl(struct ifnet * ifp, u_long command, void *data)
    879   1.1    dyoung {
    880   1.1    dyoung 	struct adapter	*adapter = ifp->if_softc;
    881  1.28   msaitoh 	struct ixgbe_hw *hw = &adapter->hw;
    882   1.1    dyoung 	struct ifcapreq *ifcr = data;
    883   1.1    dyoung 	struct ifreq	*ifr = data;
    884   1.1    dyoung 	int             error = 0;
    885   1.1    dyoung 	int l4csum_en;
    886   1.1    dyoung 	const int l4csum = IFCAP_CSUM_TCPv4_Rx|IFCAP_CSUM_UDPv4_Rx|
    887   1.1    dyoung 	     IFCAP_CSUM_TCPv6_Rx|IFCAP_CSUM_UDPv6_Rx;
    888   1.1    dyoung 
    889   1.1    dyoung 	switch (command) {
    890   1.1    dyoung 	case SIOCSIFFLAGS:
    891   1.1    dyoung 		IOCTL_DEBUGOUT("ioctl: SIOCSIFFLAGS (Set Interface Flags)");
    892   1.1    dyoung 		break;
    893   1.1    dyoung 	case SIOCADDMULTI:
    894   1.1    dyoung 	case SIOCDELMULTI:
    895   1.1    dyoung 		IOCTL_DEBUGOUT("ioctl: SIOC(ADD|DEL)MULTI");
    896   1.1    dyoung 		break;
    897   1.1    dyoung 	case SIOCSIFMEDIA:
    898   1.1    dyoung 	case SIOCGIFMEDIA:
    899   1.1    dyoung 		IOCTL_DEBUGOUT("ioctl: SIOCxIFMEDIA (Get/Set Interface Media)");
    900   1.1    dyoung 		break;
    901   1.1    dyoung 	case SIOCSIFCAP:
    902   1.1    dyoung 		IOCTL_DEBUGOUT("ioctl: SIOCSIFCAP (Set Capabilities)");
    903   1.1    dyoung 		break;
    904   1.1    dyoung 	case SIOCSIFMTU:
    905   1.1    dyoung 		IOCTL_DEBUGOUT("ioctl: SIOCSIFMTU (Set Interface MTU)");
    906   1.1    dyoung 		break;
    907   1.1    dyoung 	default:
    908   1.1    dyoung 		IOCTL_DEBUGOUT1("ioctl: UNKNOWN (0x%X)\n", (int)command);
    909   1.1    dyoung 		break;
    910   1.1    dyoung 	}
    911   1.1    dyoung 
    912   1.1    dyoung 	switch (command) {
    913   1.1    dyoung 	case SIOCSIFMEDIA:
    914   1.1    dyoung 	case SIOCGIFMEDIA:
    915   1.1    dyoung 		return ifmedia_ioctl(ifp, ifr, &adapter->media, command);
    916  1.28   msaitoh 	case SIOCGI2C:
    917  1.28   msaitoh 	{
    918  1.28   msaitoh 		struct ixgbe_i2c_req	i2c;
    919  1.28   msaitoh 		IOCTL_DEBUGOUT("ioctl: SIOCGI2C (Get I2C Data)");
    920  1.28   msaitoh 		error = copyin(ifr->ifr_data, &i2c, sizeof(i2c));
    921  1.33   msaitoh 		if (error != 0)
    922  1.33   msaitoh 			break;
    923  1.33   msaitoh 		if (i2c.dev_addr != 0xA0 && i2c.dev_addr != 0xA2) {
    924  1.33   msaitoh 			error = EINVAL;
    925  1.28   msaitoh 			break;
    926  1.33   msaitoh 		}
    927  1.33   msaitoh 		if (i2c.len > sizeof(i2c.data)) {
    928  1.28   msaitoh 			error = EINVAL;
    929  1.28   msaitoh 			break;
    930  1.28   msaitoh 		}
    931  1.33   msaitoh 
    932  1.28   msaitoh 		hw->phy.ops.read_i2c_byte(hw, i2c.offset,
    933  1.28   msaitoh 		    i2c.dev_addr, i2c.data);
    934  1.28   msaitoh 		error = copyout(&i2c, ifr->ifr_data, sizeof(i2c));
    935  1.28   msaitoh 		break;
    936  1.28   msaitoh 	}
    937   1.1    dyoung 	case SIOCSIFCAP:
    938   1.1    dyoung 		/* Layer-4 Rx checksum offload has to be turned on and
    939   1.1    dyoung 		 * off as a unit.
    940   1.1    dyoung 		 */
    941   1.1    dyoung 		l4csum_en = ifcr->ifcr_capenable & l4csum;
    942   1.1    dyoung 		if (l4csum_en != l4csum && l4csum_en != 0)
    943   1.1    dyoung 			return EINVAL;
    944   1.1    dyoung 		/*FALLTHROUGH*/
    945   1.1    dyoung 	case SIOCADDMULTI:
    946   1.1    dyoung 	case SIOCDELMULTI:
    947   1.1    dyoung 	case SIOCSIFFLAGS:
    948   1.1    dyoung 	case SIOCSIFMTU:
    949   1.1    dyoung 	default:
    950   1.1    dyoung 		if ((error = ether_ioctl(ifp, command, data)) != ENETRESET)
    951   1.1    dyoung 			return error;
    952   1.1    dyoung 		if ((ifp->if_flags & IFF_RUNNING) == 0)
    953   1.1    dyoung 			;
    954   1.1    dyoung 		else if (command == SIOCSIFCAP || command == SIOCSIFMTU) {
    955   1.1    dyoung 			IXGBE_CORE_LOCK(adapter);
    956   1.1    dyoung 			ixgbe_init_locked(adapter);
    957   1.1    dyoung 			IXGBE_CORE_UNLOCK(adapter);
    958   1.1    dyoung 		} else if (command == SIOCADDMULTI || command == SIOCDELMULTI) {
    959   1.1    dyoung 			/*
    960   1.1    dyoung 			 * Multicast list has changed; set the hardware filter
    961   1.1    dyoung 			 * accordingly.
    962   1.1    dyoung 			 */
    963   1.1    dyoung 			IXGBE_CORE_LOCK(adapter);
    964   1.1    dyoung 			ixgbe_disable_intr(adapter);
    965   1.1    dyoung 			ixgbe_set_multi(adapter);
    966   1.1    dyoung 			ixgbe_enable_intr(adapter);
    967   1.1    dyoung 			IXGBE_CORE_UNLOCK(adapter);
    968   1.1    dyoung 		}
    969   1.1    dyoung 		return 0;
    970   1.1    dyoung 	}
    971  1.28   msaitoh 
    972  1.28   msaitoh 	return error;
    973   1.1    dyoung }
    974   1.1    dyoung 
    975   1.1    dyoung /*********************************************************************
    976   1.1    dyoung  *  Init entry point
    977   1.1    dyoung  *
    978   1.1    dyoung  *  This routine is used in two ways. It is used by the stack as
    979   1.1    dyoung  *  init entry point in network interface structure. It is also used
    980   1.1    dyoung  *  by the driver as a hw/sw initialization routine to get to a
    981   1.1    dyoung  *  consistent state.
    982   1.1    dyoung  *
    983   1.1    dyoung  *  return 0 on success, positive on failure
    984   1.1    dyoung  **********************************************************************/
    985   1.1    dyoung #define IXGBE_MHADD_MFS_SHIFT 16
    986   1.1    dyoung 
    987   1.1    dyoung static void
    988   1.1    dyoung ixgbe_init_locked(struct adapter *adapter)
    989   1.1    dyoung {
    990   1.1    dyoung 	struct ifnet   *ifp = adapter->ifp;
    991   1.1    dyoung 	device_t 	dev = adapter->dev;
    992   1.1    dyoung 	struct ixgbe_hw *hw = &adapter->hw;
    993   1.1    dyoung 	u32		k, txdctl, mhadd, gpie;
    994   1.1    dyoung 	u32		rxdctl, rxctrl;
    995   1.1    dyoung 
    996   1.1    dyoung 	/* XXX check IFF_UP and IFF_RUNNING, power-saving state! */
    997   1.1    dyoung 
    998   1.1    dyoung 	KASSERT(mutex_owned(&adapter->core_mtx));
    999  1.33   msaitoh 	INIT_DEBUGOUT("ixgbe_init_locked: begin");
   1000   1.1    dyoung 	hw->adapter_stopped = FALSE;
   1001   1.1    dyoung 	ixgbe_stop_adapter(hw);
   1002   1.1    dyoung         callout_stop(&adapter->timer);
   1003   1.1    dyoung 
   1004   1.1    dyoung 	/* XXX I moved this here from the SIOCSIFMTU case in ixgbe_ioctl(). */
   1005   1.1    dyoung 	adapter->max_frame_size =
   1006   1.1    dyoung 		ifp->if_mtu + ETHER_HDR_LEN + ETHER_CRC_LEN;
   1007   1.1    dyoung 
   1008   1.1    dyoung         /* reprogram the RAR[0] in case user changed it. */
   1009   1.1    dyoung         ixgbe_set_rar(hw, 0, adapter->hw.mac.addr, 0, IXGBE_RAH_AV);
   1010   1.1    dyoung 
   1011   1.1    dyoung 	/* Get the latest mac address, User can use a LAA */
   1012   1.1    dyoung 	memcpy(hw->mac.addr, CLLADDR(adapter->ifp->if_sadl),
   1013   1.1    dyoung 	    IXGBE_ETH_LENGTH_OF_ADDRESS);
   1014   1.1    dyoung 	ixgbe_set_rar(hw, 0, hw->mac.addr, 0, 1);
   1015   1.1    dyoung 	hw->addr_ctrl.rar_used_count = 1;
   1016   1.1    dyoung 
   1017   1.1    dyoung 	/* Prepare transmit descriptors and buffers */
   1018   1.1    dyoung 	if (ixgbe_setup_transmit_structures(adapter)) {
   1019   1.1    dyoung 		device_printf(dev,"Could not setup transmit structures\n");
   1020   1.1    dyoung 		ixgbe_stop(adapter);
   1021   1.1    dyoung 		return;
   1022   1.1    dyoung 	}
   1023   1.1    dyoung 
   1024   1.1    dyoung 	ixgbe_init_hw(hw);
   1025   1.1    dyoung 	ixgbe_initialize_transmit_units(adapter);
   1026   1.1    dyoung 
   1027   1.1    dyoung 	/* Setup Multicast table */
   1028   1.1    dyoung 	ixgbe_set_multi(adapter);
   1029   1.1    dyoung 
   1030   1.1    dyoung 	/*
   1031   1.1    dyoung 	** Determine the correct mbuf pool
   1032  1.26   msaitoh 	** for doing jumbo frames
   1033   1.1    dyoung 	*/
   1034   1.1    dyoung 	if (adapter->max_frame_size <= 2048)
   1035   1.1    dyoung 		adapter->rx_mbuf_sz = MCLBYTES;
   1036   1.1    dyoung 	else if (adapter->max_frame_size <= 4096)
   1037   1.1    dyoung 		adapter->rx_mbuf_sz = MJUMPAGESIZE;
   1038   1.1    dyoung 	else if (adapter->max_frame_size <= 9216)
   1039   1.1    dyoung 		adapter->rx_mbuf_sz = MJUM9BYTES;
   1040   1.1    dyoung 	else
   1041   1.1    dyoung 		adapter->rx_mbuf_sz = MJUM16BYTES;
   1042   1.1    dyoung 
   1043   1.1    dyoung 	/* Prepare receive descriptors and buffers */
   1044   1.1    dyoung 	if (ixgbe_setup_receive_structures(adapter)) {
   1045   1.1    dyoung 		device_printf(dev,"Could not setup receive structures\n");
   1046   1.1    dyoung 		ixgbe_stop(adapter);
   1047   1.1    dyoung 		return;
   1048   1.1    dyoung 	}
   1049   1.1    dyoung 
   1050   1.1    dyoung 	/* Configure RX settings */
   1051   1.1    dyoung 	ixgbe_initialize_receive_units(adapter);
   1052   1.1    dyoung 
   1053   1.1    dyoung 	gpie = IXGBE_READ_REG(&adapter->hw, IXGBE_GPIE);
   1054   1.1    dyoung 
   1055   1.1    dyoung 	/* Enable Fan Failure Interrupt */
   1056   1.1    dyoung 	gpie |= IXGBE_SDP1_GPIEN;
   1057   1.1    dyoung 
   1058  1.35   msaitoh 	/* Add for Module detection */
   1059   1.1    dyoung 	if (hw->mac.type == ixgbe_mac_82599EB)
   1060   1.1    dyoung 		gpie |= IXGBE_SDP2_GPIEN;
   1061   1.1    dyoung 
   1062  1.24   msaitoh 	/* Thermal Failure Detection */
   1063  1.24   msaitoh 	if (hw->mac.type == ixgbe_mac_X540)
   1064  1.24   msaitoh 		gpie |= IXGBE_SDP0_GPIEN;
   1065  1.24   msaitoh 
   1066   1.1    dyoung 	if (adapter->msix > 1) {
   1067   1.1    dyoung 		/* Enable Enhanced MSIX mode */
   1068   1.1    dyoung 		gpie |= IXGBE_GPIE_MSIX_MODE;
   1069   1.1    dyoung 		gpie |= IXGBE_GPIE_EIAME | IXGBE_GPIE_PBA_SUPPORT |
   1070   1.1    dyoung 		    IXGBE_GPIE_OCD;
   1071   1.1    dyoung 	}
   1072   1.1    dyoung 	IXGBE_WRITE_REG(hw, IXGBE_GPIE, gpie);
   1073   1.1    dyoung 
   1074   1.1    dyoung 	/* Set MTU size */
   1075   1.1    dyoung 	if (ifp->if_mtu > ETHERMTU) {
   1076   1.1    dyoung 		mhadd = IXGBE_READ_REG(hw, IXGBE_MHADD);
   1077   1.1    dyoung 		mhadd &= ~IXGBE_MHADD_MFS_MASK;
   1078   1.1    dyoung 		mhadd |= adapter->max_frame_size << IXGBE_MHADD_MFS_SHIFT;
   1079   1.1    dyoung 		IXGBE_WRITE_REG(hw, IXGBE_MHADD, mhadd);
   1080   1.1    dyoung 	}
   1081   1.1    dyoung 
   1082   1.1    dyoung 	/* Now enable all the queues */
   1083   1.1    dyoung 
   1084   1.1    dyoung 	for (int i = 0; i < adapter->num_queues; i++) {
   1085   1.1    dyoung 		txdctl = IXGBE_READ_REG(hw, IXGBE_TXDCTL(i));
   1086   1.1    dyoung 		txdctl |= IXGBE_TXDCTL_ENABLE;
   1087   1.1    dyoung 		/* Set WTHRESH to 8, burst writeback */
   1088   1.1    dyoung 		txdctl |= (8 << 16);
   1089  1.25   msaitoh 		/*
   1090  1.25   msaitoh 		 * When the internal queue falls below PTHRESH (32),
   1091  1.25   msaitoh 		 * start prefetching as long as there are at least
   1092  1.25   msaitoh 		 * HTHRESH (1) buffers ready. The values are taken
   1093  1.25   msaitoh 		 * from the Intel linux driver 3.8.21.
   1094  1.25   msaitoh 		 * Prefetching enables tx line rate even with 1 queue.
   1095  1.25   msaitoh 		 */
   1096  1.25   msaitoh 		txdctl |= (32 << 0) | (1 << 8);
   1097   1.1    dyoung 		IXGBE_WRITE_REG(hw, IXGBE_TXDCTL(i), txdctl);
   1098   1.1    dyoung 	}
   1099   1.1    dyoung 
   1100   1.1    dyoung 	for (int i = 0; i < adapter->num_queues; i++) {
   1101   1.1    dyoung 		rxdctl = IXGBE_READ_REG(hw, IXGBE_RXDCTL(i));
   1102   1.1    dyoung 		if (hw->mac.type == ixgbe_mac_82598EB) {
   1103   1.1    dyoung 			/*
   1104   1.1    dyoung 			** PTHRESH = 21
   1105   1.1    dyoung 			** HTHRESH = 4
   1106   1.1    dyoung 			** WTHRESH = 8
   1107   1.1    dyoung 			*/
   1108   1.1    dyoung 			rxdctl &= ~0x3FFFFF;
   1109   1.1    dyoung 			rxdctl |= 0x080420;
   1110   1.1    dyoung 		}
   1111   1.1    dyoung 		rxdctl |= IXGBE_RXDCTL_ENABLE;
   1112   1.1    dyoung 		IXGBE_WRITE_REG(hw, IXGBE_RXDCTL(i), rxdctl);
   1113   1.1    dyoung 		/* XXX I don't trust this loop, and I don't trust the
   1114   1.1    dyoung 		 * XXX memory barrier.  What is this meant to do? --dyoung
   1115   1.1    dyoung 		 */
   1116   1.1    dyoung 		for (k = 0; k < 10; k++) {
   1117   1.1    dyoung 			if (IXGBE_READ_REG(hw, IXGBE_RXDCTL(i)) &
   1118   1.1    dyoung 			    IXGBE_RXDCTL_ENABLE)
   1119   1.1    dyoung 				break;
   1120   1.1    dyoung 			else
   1121   1.1    dyoung 				msec_delay(1);
   1122   1.1    dyoung 		}
   1123   1.1    dyoung 		wmb();
   1124  1.22   msaitoh #ifdef DEV_NETMAP
   1125  1.22   msaitoh 		/*
   1126  1.22   msaitoh 		 * In netmap mode, we must preserve the buffers made
   1127  1.22   msaitoh 		 * available to userspace before the if_init()
   1128  1.22   msaitoh 		 * (this is true by default on the TX side, because
   1129  1.22   msaitoh 		 * init makes all buffers available to userspace).
   1130  1.22   msaitoh 		 *
   1131  1.22   msaitoh 		 * netmap_reset() and the device specific routines
   1132  1.22   msaitoh 		 * (e.g. ixgbe_setup_receive_rings()) map these
   1133  1.22   msaitoh 		 * buffers at the end of the NIC ring, so here we
   1134  1.22   msaitoh 		 * must set the RDT (tail) register to make sure
   1135  1.22   msaitoh 		 * they are not overwritten.
   1136  1.22   msaitoh 		 *
   1137  1.22   msaitoh 		 * In this driver the NIC ring starts at RDH = 0,
   1138  1.22   msaitoh 		 * RDT points to the last slot available for reception (?),
   1139  1.22   msaitoh 		 * so RDT = num_rx_desc - 1 means the whole ring is available.
   1140  1.22   msaitoh 		 */
   1141  1.22   msaitoh 		if (ifp->if_capenable & IFCAP_NETMAP) {
   1142  1.22   msaitoh 			struct netmap_adapter *na = NA(adapter->ifp);
   1143  1.22   msaitoh 			struct netmap_kring *kring = &na->rx_rings[i];
   1144  1.33   msaitoh 			int t = na->num_rx_desc - 1 - nm_kr_rxspace(kring);
   1145  1.22   msaitoh 
   1146  1.22   msaitoh 			IXGBE_WRITE_REG(hw, IXGBE_RDT(i), t);
   1147  1.22   msaitoh 		} else
   1148  1.22   msaitoh #endif /* DEV_NETMAP */
   1149   1.1    dyoung 		IXGBE_WRITE_REG(hw, IXGBE_RDT(i), adapter->num_rx_desc - 1);
   1150   1.1    dyoung 	}
   1151   1.1    dyoung 
   1152   1.1    dyoung 	/* Enable Receive engine */
   1153   1.1    dyoung 	rxctrl = IXGBE_READ_REG(hw, IXGBE_RXCTRL);
   1154   1.1    dyoung 	if (hw->mac.type == ixgbe_mac_82598EB)
   1155   1.1    dyoung 		rxctrl |= IXGBE_RXCTRL_DMBYPS;
   1156   1.1    dyoung 	rxctrl |= IXGBE_RXCTRL_RXEN;
   1157   1.1    dyoung 	ixgbe_enable_rx_dma(hw, rxctrl);
   1158   1.1    dyoung 
   1159   1.1    dyoung 	callout_reset(&adapter->timer, hz, ixgbe_local_timer, adapter);
   1160   1.1    dyoung 
   1161   1.1    dyoung 	/* Set up MSI/X routing */
   1162   1.1    dyoung 	if (ixgbe_enable_msix)  {
   1163   1.1    dyoung 		ixgbe_configure_ivars(adapter);
   1164   1.1    dyoung 		/* Set up auto-mask */
   1165   1.1    dyoung 		if (hw->mac.type == ixgbe_mac_82598EB)
   1166   1.1    dyoung 			IXGBE_WRITE_REG(hw, IXGBE_EIAM, IXGBE_EICS_RTX_QUEUE);
   1167   1.1    dyoung 		else {
   1168   1.1    dyoung 			IXGBE_WRITE_REG(hw, IXGBE_EIAM_EX(0), 0xFFFFFFFF);
   1169   1.1    dyoung 			IXGBE_WRITE_REG(hw, IXGBE_EIAM_EX(1), 0xFFFFFFFF);
   1170   1.1    dyoung 		}
   1171   1.1    dyoung 	} else {  /* Simple settings for Legacy/MSI */
   1172   1.1    dyoung                 ixgbe_set_ivar(adapter, 0, 0, 0);
   1173   1.1    dyoung                 ixgbe_set_ivar(adapter, 0, 0, 1);
   1174   1.1    dyoung 		IXGBE_WRITE_REG(hw, IXGBE_EIAM, IXGBE_EICS_RTX_QUEUE);
   1175   1.1    dyoung 	}
   1176   1.1    dyoung 
   1177   1.1    dyoung #ifdef IXGBE_FDIR
   1178   1.1    dyoung 	/* Init Flow director */
   1179  1.24   msaitoh 	if (hw->mac.type != ixgbe_mac_82598EB) {
   1180  1.25   msaitoh 		u32 hdrm = 32 << fdir_pballoc;
   1181  1.24   msaitoh 
   1182  1.24   msaitoh 		hw->mac.ops.setup_rxpba(hw, 0, hdrm, PBA_STRATEGY_EQUAL);
   1183   1.1    dyoung 		ixgbe_init_fdir_signature_82599(&adapter->hw, fdir_pballoc);
   1184  1.24   msaitoh 	}
   1185   1.1    dyoung #endif
   1186   1.1    dyoung 
   1187   1.1    dyoung 	/*
   1188   1.1    dyoung 	** Check on any SFP devices that
   1189   1.1    dyoung 	** need to be kick-started
   1190   1.1    dyoung 	*/
   1191   1.1    dyoung 	if (hw->phy.type == ixgbe_phy_none) {
   1192   1.1    dyoung 		int err = hw->phy.ops.identify(hw);
   1193   1.1    dyoung 		if (err == IXGBE_ERR_SFP_NOT_SUPPORTED) {
   1194   1.1    dyoung                 	device_printf(dev,
   1195   1.1    dyoung 			    "Unsupported SFP+ module type was detected.\n");
   1196   1.1    dyoung 			return;
   1197   1.1    dyoung         	}
   1198   1.1    dyoung 	}
   1199   1.1    dyoung 
   1200   1.1    dyoung 	/* Set moderation on the Link interrupt */
   1201   1.1    dyoung 	IXGBE_WRITE_REG(hw, IXGBE_EITR(adapter->linkvec), IXGBE_LINK_ITR);
   1202   1.1    dyoung 
   1203   1.1    dyoung 	/* Config/Enable Link */
   1204   1.1    dyoung 	ixgbe_config_link(adapter);
   1205   1.1    dyoung 
   1206  1.25   msaitoh 	/* Hardware Packet Buffer & Flow Control setup */
   1207  1.25   msaitoh 	{
   1208  1.25   msaitoh 		u32 rxpb, frame, size, tmp;
   1209  1.25   msaitoh 
   1210  1.25   msaitoh 		frame = adapter->max_frame_size;
   1211  1.25   msaitoh 
   1212  1.25   msaitoh 		/* Calculate High Water */
   1213  1.25   msaitoh 		if (hw->mac.type == ixgbe_mac_X540)
   1214  1.25   msaitoh 			tmp = IXGBE_DV_X540(frame, frame);
   1215  1.25   msaitoh 		else
   1216  1.25   msaitoh 			tmp = IXGBE_DV(frame, frame);
   1217  1.25   msaitoh 		size = IXGBE_BT2KB(tmp);
   1218  1.25   msaitoh 		rxpb = IXGBE_READ_REG(hw, IXGBE_RXPBSIZE(0)) >> 10;
   1219  1.25   msaitoh 		hw->fc.high_water[0] = rxpb - size;
   1220  1.25   msaitoh 
   1221  1.25   msaitoh 		/* Now calculate Low Water */
   1222  1.25   msaitoh 		if (hw->mac.type == ixgbe_mac_X540)
   1223  1.25   msaitoh 			tmp = IXGBE_LOW_DV_X540(frame);
   1224  1.25   msaitoh 		else
   1225  1.25   msaitoh 			tmp = IXGBE_LOW_DV(frame);
   1226  1.25   msaitoh 		hw->fc.low_water[0] = IXGBE_BT2KB(tmp);
   1227  1.25   msaitoh 
   1228  1.28   msaitoh 		hw->fc.requested_mode = adapter->fc;
   1229  1.25   msaitoh 		hw->fc.pause_time = IXGBE_FC_PAUSE;
   1230  1.25   msaitoh 		hw->fc.send_xon = TRUE;
   1231  1.25   msaitoh 	}
   1232  1.25   msaitoh 	/* Initialize the FC settings */
   1233  1.25   msaitoh 	ixgbe_start_hw(hw);
   1234  1.25   msaitoh 
   1235  1.33   msaitoh 	/* Set up VLAN support and filter */
   1236  1.33   msaitoh 	ixgbe_setup_vlan_hw_support(adapter);
   1237  1.33   msaitoh 
   1238   1.1    dyoung 	/* And now turn on interrupts */
   1239   1.1    dyoung 	ixgbe_enable_intr(adapter);
   1240   1.1    dyoung 
   1241   1.1    dyoung 	/* Now inform the stack we're ready */
   1242   1.1    dyoung 	ifp->if_flags |= IFF_RUNNING;
   1243   1.1    dyoung 
   1244   1.1    dyoung 	return;
   1245   1.1    dyoung }
   1246   1.1    dyoung 
   1247   1.1    dyoung static int
   1248   1.1    dyoung ixgbe_init(struct ifnet *ifp)
   1249   1.1    dyoung {
   1250   1.1    dyoung 	struct adapter *adapter = ifp->if_softc;
   1251   1.1    dyoung 
   1252   1.1    dyoung 	IXGBE_CORE_LOCK(adapter);
   1253   1.1    dyoung 	ixgbe_init_locked(adapter);
   1254   1.1    dyoung 	IXGBE_CORE_UNLOCK(adapter);
   1255   1.1    dyoung 	return 0;	/* XXX ixgbe_init_locked cannot fail?  really? */
   1256   1.1    dyoung }
   1257   1.1    dyoung 
   1258   1.1    dyoung 
   1259   1.1    dyoung /*
   1260   1.1    dyoung **
   1261   1.1    dyoung ** MSIX Interrupt Handlers and Tasklets
   1262   1.1    dyoung **
   1263   1.1    dyoung */
   1264   1.1    dyoung 
   1265   1.1    dyoung static inline void
   1266   1.1    dyoung ixgbe_enable_queue(struct adapter *adapter, u32 vector)
   1267   1.1    dyoung {
   1268   1.1    dyoung 	struct ixgbe_hw *hw = &adapter->hw;
   1269  1.13  christos 	u64	queue = (u64)(1ULL << vector);
   1270   1.1    dyoung 	u32	mask;
   1271   1.1    dyoung 
   1272   1.1    dyoung 	if (hw->mac.type == ixgbe_mac_82598EB) {
   1273   1.1    dyoung                 mask = (IXGBE_EIMS_RTX_QUEUE & queue);
   1274   1.1    dyoung                 IXGBE_WRITE_REG(hw, IXGBE_EIMS, mask);
   1275   1.1    dyoung 	} else {
   1276   1.1    dyoung                 mask = (queue & 0xFFFFFFFF);
   1277   1.1    dyoung                 if (mask)
   1278   1.1    dyoung                         IXGBE_WRITE_REG(hw, IXGBE_EIMS_EX(0), mask);
   1279   1.1    dyoung                 mask = (queue >> 32);
   1280   1.1    dyoung                 if (mask)
   1281   1.1    dyoung                         IXGBE_WRITE_REG(hw, IXGBE_EIMS_EX(1), mask);
   1282   1.1    dyoung 	}
   1283   1.1    dyoung }
   1284   1.1    dyoung 
   1285  1.11     joerg __unused static inline void
   1286   1.1    dyoung ixgbe_disable_queue(struct adapter *adapter, u32 vector)
   1287   1.1    dyoung {
   1288   1.1    dyoung 	struct ixgbe_hw *hw = &adapter->hw;
   1289  1.13  christos 	u64	queue = (u64)(1ULL << vector);
   1290   1.1    dyoung 	u32	mask;
   1291   1.1    dyoung 
   1292   1.1    dyoung 	if (hw->mac.type == ixgbe_mac_82598EB) {
   1293   1.1    dyoung                 mask = (IXGBE_EIMS_RTX_QUEUE & queue);
   1294   1.1    dyoung                 IXGBE_WRITE_REG(hw, IXGBE_EIMC, mask);
   1295   1.1    dyoung 	} else {
   1296   1.1    dyoung                 mask = (queue & 0xFFFFFFFF);
   1297   1.1    dyoung                 if (mask)
   1298   1.1    dyoung                         IXGBE_WRITE_REG(hw, IXGBE_EIMC_EX(0), mask);
   1299   1.1    dyoung                 mask = (queue >> 32);
   1300   1.1    dyoung                 if (mask)
   1301   1.1    dyoung                         IXGBE_WRITE_REG(hw, IXGBE_EIMC_EX(1), mask);
   1302   1.1    dyoung 	}
   1303   1.1    dyoung }
   1304   1.1    dyoung 
   1305   1.1    dyoung static void
   1306   1.1    dyoung ixgbe_handle_que(void *context)
   1307   1.1    dyoung {
   1308   1.1    dyoung 	struct ix_queue *que = context;
   1309   1.1    dyoung 	struct adapter  *adapter = que->adapter;
   1310   1.1    dyoung 	struct tx_ring  *txr = que->txr;
   1311   1.1    dyoung 	struct ifnet    *ifp = adapter->ifp;
   1312   1.1    dyoung 
   1313   1.1    dyoung 	adapter->handleq.ev_count++;
   1314   1.1    dyoung 
   1315   1.1    dyoung 	if (ifp->if_flags & IFF_RUNNING) {
   1316  1.33   msaitoh 		ixgbe_rxeof(que);
   1317   1.1    dyoung 		IXGBE_TX_LOCK(txr);
   1318   1.1    dyoung 		ixgbe_txeof(txr);
   1319  1.28   msaitoh #ifndef IXGBE_LEGACY_TX
   1320   1.1    dyoung 		if (!drbr_empty(ifp, txr->br))
   1321  1.33   msaitoh 			ixgbe_mq_start_locked(ifp, txr);
   1322   1.1    dyoung #else
   1323   1.1    dyoung 		if (!IFQ_IS_EMPTY(&ifp->if_snd))
   1324   1.1    dyoung 			ixgbe_start_locked(txr, ifp);
   1325   1.1    dyoung #endif
   1326   1.1    dyoung 		IXGBE_TX_UNLOCK(txr);
   1327   1.1    dyoung 	}
   1328   1.1    dyoung 
   1329   1.1    dyoung 	/* Reenable this interrupt */
   1330  1.33   msaitoh 	if (que->res != NULL)
   1331  1.33   msaitoh 		ixgbe_enable_queue(adapter, que->msix);
   1332  1.33   msaitoh 	else
   1333  1.33   msaitoh 		ixgbe_enable_intr(adapter);
   1334   1.1    dyoung 	return;
   1335   1.1    dyoung }
   1336   1.1    dyoung 
   1337   1.1    dyoung 
   1338   1.1    dyoung /*********************************************************************
   1339   1.1    dyoung  *
   1340   1.1    dyoung  *  Legacy Interrupt Service routine
   1341   1.1    dyoung  *
   1342   1.1    dyoung  **********************************************************************/
   1343   1.1    dyoung 
   1344   1.1    dyoung static int
   1345   1.1    dyoung ixgbe_legacy_irq(void *arg)
   1346   1.1    dyoung {
   1347   1.1    dyoung 	struct ix_queue *que = arg;
   1348   1.1    dyoung 	struct adapter	*adapter = que->adapter;
   1349  1.33   msaitoh 	struct ixgbe_hw	*hw = &adapter->hw;
   1350  1.35   msaitoh 	struct ifnet    *ifp = adapter->ifp;
   1351   1.1    dyoung 	struct 		tx_ring *txr = adapter->tx_rings;
   1352  1.33   msaitoh 	bool		more = false;
   1353  1.33   msaitoh 	u32       	reg_eicr;
   1354   1.1    dyoung 
   1355  1.35   msaitoh 
   1356   1.1    dyoung 	reg_eicr = IXGBE_READ_REG(hw, IXGBE_EICR);
   1357   1.1    dyoung 
   1358   1.1    dyoung 	adapter->stats.legint.ev_count++;
   1359   1.1    dyoung 	++que->irqs;
   1360   1.1    dyoung 	if (reg_eicr == 0) {
   1361   1.1    dyoung 		adapter->stats.intzero.ev_count++;
   1362  1.15   msaitoh 		if ((ifp->if_flags & IFF_UP) != 0)
   1363  1.15   msaitoh 			ixgbe_enable_intr(adapter);
   1364   1.1    dyoung 		return 0;
   1365   1.1    dyoung 	}
   1366   1.1    dyoung 
   1367  1.15   msaitoh 	if ((ifp->if_flags & IFF_RUNNING) != 0) {
   1368  1.37     ozaki #ifdef __NetBSD__
   1369  1.37     ozaki 		/* Don't run ixgbe_rxeof in interrupt context */
   1370  1.37     ozaki 		more = true;
   1371  1.37     ozaki #else
   1372  1.33   msaitoh 		more = ixgbe_rxeof(que);
   1373  1.37     ozaki #endif
   1374   1.1    dyoung 
   1375  1.15   msaitoh 		IXGBE_TX_LOCK(txr);
   1376  1.33   msaitoh 		ixgbe_txeof(txr);
   1377  1.33   msaitoh #ifdef IXGBE_LEGACY_TX
   1378  1.33   msaitoh 		if (!IFQ_IS_EMPTY(&ifp->if_snd))
   1379  1.33   msaitoh 			ixgbe_start_locked(txr, ifp);
   1380  1.33   msaitoh #else
   1381  1.33   msaitoh 		if (!drbr_empty(ifp, txr->br))
   1382  1.33   msaitoh 			ixgbe_mq_start_locked(ifp, txr);
   1383  1.33   msaitoh #endif
   1384  1.15   msaitoh 		IXGBE_TX_UNLOCK(txr);
   1385  1.15   msaitoh 	}
   1386   1.1    dyoung 
   1387   1.1    dyoung 	/* Check for fan failure */
   1388   1.1    dyoung 	if ((hw->phy.media_type == ixgbe_media_type_copper) &&
   1389   1.1    dyoung 	    (reg_eicr & IXGBE_EICR_GPI_SDP1)) {
   1390   1.1    dyoung                 device_printf(adapter->dev, "\nCRITICAL: FAN FAILURE!! "
   1391   1.1    dyoung 		    "REPLACE IMMEDIATELY!!\n");
   1392   1.1    dyoung 		IXGBE_WRITE_REG(hw, IXGBE_EIMS, IXGBE_EICR_GPI_SDP1);
   1393   1.1    dyoung 	}
   1394   1.1    dyoung 
   1395   1.1    dyoung 	/* Link status change */
   1396   1.1    dyoung 	if (reg_eicr & IXGBE_EICR_LSC)
   1397   1.1    dyoung 		softint_schedule(adapter->link_si);
   1398   1.1    dyoung 
   1399  1.33   msaitoh 	if (more)
   1400  1.33   msaitoh #ifndef IXGBE_LEGACY_TX
   1401  1.33   msaitoh 		softint_schedule(txr->txq_si);
   1402  1.33   msaitoh #else
   1403  1.33   msaitoh 		softint_schedule(que->que_si);
   1404  1.33   msaitoh #endif
   1405  1.33   msaitoh 	else
   1406  1.33   msaitoh 		ixgbe_enable_intr(adapter);
   1407   1.1    dyoung 	return 1;
   1408   1.1    dyoung }
   1409   1.1    dyoung 
   1410   1.1    dyoung 
   1411   1.1    dyoung /*********************************************************************
   1412   1.1    dyoung  *
   1413  1.22   msaitoh  *  MSIX Queue Interrupt Service routine
   1414   1.1    dyoung  *
   1415   1.1    dyoung  **********************************************************************/
   1416  1.34   msaitoh static int
   1417   1.1    dyoung ixgbe_msix_que(void *arg)
   1418   1.1    dyoung {
   1419   1.1    dyoung 	struct ix_queue	*que = arg;
   1420   1.1    dyoung 	struct adapter  *adapter = que->adapter;
   1421  1.33   msaitoh 	struct ifnet    *ifp = adapter->ifp;
   1422   1.1    dyoung 	struct tx_ring	*txr = que->txr;
   1423   1.1    dyoung 	struct rx_ring	*rxr = que->rxr;
   1424  1.33   msaitoh 	bool		more;
   1425   1.1    dyoung 	u32		newitr = 0;
   1426   1.1    dyoung 
   1427  1.33   msaitoh 	/* Protect against spurious interrupts */
   1428  1.33   msaitoh 	if ((ifp->if_flags & IFF_RUNNING) == 0)
   1429  1.34   msaitoh 		return 0;
   1430  1.33   msaitoh 
   1431  1.24   msaitoh 	ixgbe_disable_queue(adapter, que->msix);
   1432   1.1    dyoung 	++que->irqs;
   1433   1.1    dyoung 
   1434  1.37     ozaki #ifdef __NetBSD__
   1435  1.37     ozaki 	/* Don't run ixgbe_rxeof in interrupt context */
   1436  1.37     ozaki 	more = true;
   1437  1.37     ozaki #else
   1438  1.33   msaitoh 	more = ixgbe_rxeof(que);
   1439  1.37     ozaki #endif
   1440   1.1    dyoung 
   1441   1.1    dyoung 	IXGBE_TX_LOCK(txr);
   1442  1.33   msaitoh 	ixgbe_txeof(txr);
   1443  1.28   msaitoh #ifdef IXGBE_LEGACY_TX
   1444  1.24   msaitoh 	if (!IFQ_IS_EMPTY(&adapter->ifp->if_snd))
   1445  1.34   msaitoh 		ixgbe_start_locked(txr, ifp);
   1446  1.22   msaitoh #else
   1447  1.33   msaitoh 	if (!drbr_empty(ifp, txr->br))
   1448  1.33   msaitoh 		ixgbe_mq_start_locked(ifp, txr);
   1449  1.22   msaitoh #endif
   1450   1.1    dyoung 	IXGBE_TX_UNLOCK(txr);
   1451   1.1    dyoung 
   1452   1.1    dyoung 	/* Do AIM now? */
   1453   1.1    dyoung 
   1454   1.1    dyoung 	if (ixgbe_enable_aim == FALSE)
   1455   1.1    dyoung 		goto no_calc;
   1456   1.1    dyoung 	/*
   1457   1.1    dyoung 	** Do Adaptive Interrupt Moderation:
   1458   1.1    dyoung         **  - Write out last calculated setting
   1459   1.1    dyoung 	**  - Calculate based on average size over
   1460   1.1    dyoung 	**    the last interval.
   1461   1.1    dyoung 	*/
   1462   1.1    dyoung         if (que->eitr_setting)
   1463   1.1    dyoung                 IXGBE_WRITE_REG(&adapter->hw,
   1464   1.1    dyoung                     IXGBE_EITR(que->msix), que->eitr_setting);
   1465   1.1    dyoung 
   1466   1.1    dyoung         que->eitr_setting = 0;
   1467   1.1    dyoung 
   1468   1.1    dyoung         /* Idle, do nothing */
   1469   1.1    dyoung         if ((txr->bytes == 0) && (rxr->bytes == 0))
   1470   1.1    dyoung                 goto no_calc;
   1471   1.1    dyoung 
   1472   1.1    dyoung 	if ((txr->bytes) && (txr->packets))
   1473   1.1    dyoung                	newitr = txr->bytes/txr->packets;
   1474   1.1    dyoung 	if ((rxr->bytes) && (rxr->packets))
   1475   1.1    dyoung 		newitr = max(newitr,
   1476   1.1    dyoung 		    (rxr->bytes / rxr->packets));
   1477   1.1    dyoung 	newitr += 24; /* account for hardware frame, crc */
   1478   1.1    dyoung 
   1479   1.1    dyoung 	/* set an upper boundary */
   1480   1.1    dyoung 	newitr = min(newitr, 3000);
   1481   1.1    dyoung 
   1482   1.1    dyoung 	/* Be nice to the mid range */
   1483   1.1    dyoung 	if ((newitr > 300) && (newitr < 1200))
   1484   1.1    dyoung 		newitr = (newitr / 3);
   1485   1.1    dyoung 	else
   1486   1.1    dyoung 		newitr = (newitr / 2);
   1487   1.1    dyoung 
   1488   1.1    dyoung         if (adapter->hw.mac.type == ixgbe_mac_82598EB)
   1489   1.1    dyoung                 newitr |= newitr << 16;
   1490   1.1    dyoung         else
   1491   1.1    dyoung                 newitr |= IXGBE_EITR_CNT_WDIS;
   1492   1.1    dyoung 
   1493   1.1    dyoung         /* save for next interrupt */
   1494   1.1    dyoung         que->eitr_setting = newitr;
   1495   1.1    dyoung 
   1496   1.1    dyoung         /* Reset state */
   1497   1.1    dyoung         txr->bytes = 0;
   1498   1.1    dyoung         txr->packets = 0;
   1499   1.1    dyoung         rxr->bytes = 0;
   1500   1.1    dyoung         rxr->packets = 0;
   1501   1.1    dyoung 
   1502   1.1    dyoung no_calc:
   1503  1.33   msaitoh 	if (more)
   1504   1.1    dyoung 		softint_schedule(que->que_si);
   1505  1.33   msaitoh 	else
   1506   1.1    dyoung 		ixgbe_enable_queue(adapter, que->msix);
   1507  1.34   msaitoh 	return 1;
   1508   1.1    dyoung }
   1509   1.1    dyoung 
   1510   1.1    dyoung 
   1511  1.34   msaitoh static int
   1512   1.1    dyoung ixgbe_msix_link(void *arg)
   1513   1.1    dyoung {
   1514   1.1    dyoung 	struct adapter	*adapter = arg;
   1515   1.1    dyoung 	struct ixgbe_hw *hw = &adapter->hw;
   1516   1.1    dyoung 	u32		reg_eicr;
   1517   1.1    dyoung 
   1518   1.1    dyoung 	++adapter->link_irq.ev_count;
   1519   1.1    dyoung 
   1520   1.1    dyoung 	/* First get the cause */
   1521   1.1    dyoung 	reg_eicr = IXGBE_READ_REG(hw, IXGBE_EICS);
   1522  1.33   msaitoh 	/* Be sure the queue bits are not cleared */
   1523  1.33   msaitoh 	reg_eicr &= ~IXGBE_EICR_RTX_QUEUE;
   1524   1.1    dyoung 	/* Clear interrupt with write */
   1525   1.1    dyoung 	IXGBE_WRITE_REG(hw, IXGBE_EICR, reg_eicr);
   1526   1.1    dyoung 
   1527   1.1    dyoung 	/* Link status change */
   1528   1.1    dyoung 	if (reg_eicr & IXGBE_EICR_LSC)
   1529   1.1    dyoung 		softint_schedule(adapter->link_si);
   1530   1.1    dyoung 
   1531   1.1    dyoung 	if (adapter->hw.mac.type != ixgbe_mac_82598EB) {
   1532   1.1    dyoung #ifdef IXGBE_FDIR
   1533   1.1    dyoung 		if (reg_eicr & IXGBE_EICR_FLOW_DIR) {
   1534   1.1    dyoung 			/* This is probably overkill :) */
   1535   1.1    dyoung 			if (!atomic_cmpset_int(&adapter->fdir_reinit, 0, 1))
   1536  1.34   msaitoh 				return 1;
   1537  1.25   msaitoh                 	/* Disable the interrupt */
   1538  1.25   msaitoh 			IXGBE_WRITE_REG(hw, IXGBE_EIMC, IXGBE_EICR_FLOW_DIR);
   1539   1.1    dyoung 			softint_schedule(adapter->fdir_si);
   1540   1.1    dyoung 		} else
   1541   1.1    dyoung #endif
   1542   1.1    dyoung 		if (reg_eicr & IXGBE_EICR_ECC) {
   1543   1.1    dyoung                 	device_printf(adapter->dev, "\nCRITICAL: ECC ERROR!! "
   1544   1.1    dyoung 			    "Please Reboot!!\n");
   1545   1.1    dyoung 			IXGBE_WRITE_REG(hw, IXGBE_EICR, IXGBE_EICR_ECC);
   1546   1.1    dyoung 		} else
   1547   1.1    dyoung 
   1548   1.1    dyoung 		if (reg_eicr & IXGBE_EICR_GPI_SDP1) {
   1549   1.1    dyoung                 	/* Clear the interrupt */
   1550   1.1    dyoung                 	IXGBE_WRITE_REG(hw, IXGBE_EICR, IXGBE_EICR_GPI_SDP1);
   1551   1.1    dyoung 			softint_schedule(adapter->msf_si);
   1552   1.1    dyoung         	} else if (reg_eicr & IXGBE_EICR_GPI_SDP2) {
   1553   1.1    dyoung                 	/* Clear the interrupt */
   1554   1.1    dyoung                 	IXGBE_WRITE_REG(hw, IXGBE_EICR, IXGBE_EICR_GPI_SDP2);
   1555   1.1    dyoung 			softint_schedule(adapter->mod_si);
   1556   1.1    dyoung 		}
   1557   1.1    dyoung         }
   1558   1.1    dyoung 
   1559   1.1    dyoung 	/* Check for fan failure */
   1560   1.1    dyoung 	if ((hw->device_id == IXGBE_DEV_ID_82598AT) &&
   1561   1.1    dyoung 	    (reg_eicr & IXGBE_EICR_GPI_SDP1)) {
   1562   1.1    dyoung                 device_printf(adapter->dev, "\nCRITICAL: FAN FAILURE!! "
   1563   1.1    dyoung 		    "REPLACE IMMEDIATELY!!\n");
   1564   1.1    dyoung 		IXGBE_WRITE_REG(hw, IXGBE_EICR, IXGBE_EICR_GPI_SDP1);
   1565   1.1    dyoung 	}
   1566   1.1    dyoung 
   1567  1.24   msaitoh 	/* Check for over temp condition */
   1568  1.24   msaitoh 	if ((hw->mac.type == ixgbe_mac_X540) &&
   1569  1.28   msaitoh 	    (reg_eicr & IXGBE_EICR_TS)) {
   1570  1.24   msaitoh 		device_printf(adapter->dev, "\nCRITICAL: OVER TEMP!! "
   1571  1.24   msaitoh 		    "PHY IS SHUT DOWN!!\n");
   1572  1.24   msaitoh 		device_printf(adapter->dev, "System shutdown required\n");
   1573  1.28   msaitoh 		IXGBE_WRITE_REG(hw, IXGBE_EICR, IXGBE_EICR_TS);
   1574  1.24   msaitoh 	}
   1575  1.24   msaitoh 
   1576   1.1    dyoung 	IXGBE_WRITE_REG(&adapter->hw, IXGBE_EIMS, IXGBE_EIMS_OTHER);
   1577  1.34   msaitoh 	return 1;
   1578   1.1    dyoung }
   1579   1.1    dyoung 
   1580   1.1    dyoung /*********************************************************************
   1581   1.1    dyoung  *
   1582   1.1    dyoung  *  Media Ioctl callback
   1583   1.1    dyoung  *
   1584   1.1    dyoung  *  This routine is called whenever the user queries the status of
   1585   1.1    dyoung  *  the interface using ifconfig.
   1586   1.1    dyoung  *
   1587   1.1    dyoung  **********************************************************************/
   1588   1.1    dyoung static void
   1589   1.1    dyoung ixgbe_media_status(struct ifnet * ifp, struct ifmediareq * ifmr)
   1590   1.1    dyoung {
   1591   1.1    dyoung 	struct adapter *adapter = ifp->if_softc;
   1592  1.33   msaitoh 	struct ixgbe_hw *hw = &adapter->hw;
   1593   1.1    dyoung 
   1594   1.1    dyoung 	INIT_DEBUGOUT("ixgbe_media_status: begin");
   1595   1.1    dyoung 	IXGBE_CORE_LOCK(adapter);
   1596   1.1    dyoung 	ixgbe_update_link_status(adapter);
   1597   1.1    dyoung 
   1598   1.1    dyoung 	ifmr->ifm_status = IFM_AVALID;
   1599   1.1    dyoung 	ifmr->ifm_active = IFM_ETHER;
   1600   1.1    dyoung 
   1601   1.1    dyoung 	if (!adapter->link_active) {
   1602   1.1    dyoung 		IXGBE_CORE_UNLOCK(adapter);
   1603   1.1    dyoung 		return;
   1604   1.1    dyoung 	}
   1605   1.1    dyoung 
   1606   1.1    dyoung 	ifmr->ifm_status |= IFM_ACTIVE;
   1607   1.1    dyoung 
   1608  1.33   msaitoh 	/*
   1609  1.33   msaitoh 	 * Not all NIC are 1000baseSX as an example X540T.
   1610  1.33   msaitoh 	 * We must set properly the media based on NIC model.
   1611  1.33   msaitoh 	 */
   1612  1.33   msaitoh 	switch (hw->device_id) {
   1613  1.33   msaitoh 	case IXGBE_DEV_ID_X540T:
   1614  1.33   msaitoh 		if (adapter->link_speed == IXGBE_LINK_SPEED_100_FULL)
   1615  1.33   msaitoh 			ifmr->ifm_active |= IFM_100_TX | IFM_FDX;
   1616  1.33   msaitoh 		else if (adapter->link_speed == IXGBE_LINK_SPEED_1GB_FULL)
   1617  1.33   msaitoh 			ifmr->ifm_active |= IFM_1000_T | IFM_FDX;
   1618  1.33   msaitoh 		else if (adapter->link_speed == IXGBE_LINK_SPEED_10GB_FULL)
   1619  1.33   msaitoh 			ifmr->ifm_active |= adapter->optics | IFM_FDX;
   1620  1.33   msaitoh 		break;
   1621  1.33   msaitoh 	default:
   1622  1.33   msaitoh 		if (adapter->link_speed == IXGBE_LINK_SPEED_100_FULL)
   1623  1.24   msaitoh 			ifmr->ifm_active |= IFM_100_TX | IFM_FDX;
   1624  1.33   msaitoh 		else if (adapter->link_speed == IXGBE_LINK_SPEED_1GB_FULL)
   1625  1.28   msaitoh 			ifmr->ifm_active |= IFM_1000_SX | IFM_FDX;
   1626  1.33   msaitoh 		else if (adapter->link_speed == IXGBE_LINK_SPEED_10GB_FULL)
   1627   1.1    dyoung 			ifmr->ifm_active |= adapter->optics | IFM_FDX;
   1628  1.33   msaitoh 		break;
   1629   1.1    dyoung 	}
   1630   1.1    dyoung 
   1631   1.1    dyoung 	IXGBE_CORE_UNLOCK(adapter);
   1632   1.1    dyoung 
   1633   1.1    dyoung 	return;
   1634   1.1    dyoung }
   1635   1.1    dyoung 
   1636   1.1    dyoung /*********************************************************************
   1637   1.1    dyoung  *
   1638   1.1    dyoung  *  Media Ioctl callback
   1639   1.1    dyoung  *
   1640   1.1    dyoung  *  This routine is called when the user changes speed/duplex using
   1641   1.1    dyoung  *  media/mediopt option with ifconfig.
   1642   1.1    dyoung  *
   1643   1.1    dyoung  **********************************************************************/
   1644   1.1    dyoung static int
   1645   1.1    dyoung ixgbe_media_change(struct ifnet * ifp)
   1646   1.1    dyoung {
   1647   1.1    dyoung 	struct adapter *adapter = ifp->if_softc;
   1648   1.1    dyoung 	struct ifmedia *ifm = &adapter->media;
   1649   1.1    dyoung 
   1650   1.1    dyoung 	INIT_DEBUGOUT("ixgbe_media_change: begin");
   1651   1.1    dyoung 
   1652   1.1    dyoung 	if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
   1653   1.1    dyoung 		return (EINVAL);
   1654   1.1    dyoung 
   1655   1.1    dyoung         switch (IFM_SUBTYPE(ifm->ifm_media)) {
   1656  1.33   msaitoh 	case IFM_10G_T:
   1657  1.33   msaitoh 	case IFM_AUTO:
   1658  1.33   msaitoh 		adapter->hw.phy.autoneg_advertised =
   1659  1.24   msaitoh 		    IXGBE_LINK_SPEED_100_FULL |
   1660  1.24   msaitoh 		    IXGBE_LINK_SPEED_1GB_FULL |
   1661  1.24   msaitoh 		    IXGBE_LINK_SPEED_10GB_FULL;
   1662   1.1    dyoung                 break;
   1663   1.1    dyoung         default:
   1664   1.1    dyoung                 device_printf(adapter->dev, "Only auto media type\n");
   1665   1.1    dyoung 		return (EINVAL);
   1666   1.1    dyoung         }
   1667   1.1    dyoung 
   1668   1.1    dyoung 	return (0);
   1669   1.1    dyoung }
   1670   1.1    dyoung 
   1671   1.1    dyoung static void
   1672   1.1    dyoung ixgbe_set_promisc(struct adapter *adapter)
   1673   1.1    dyoung {
   1674  1.28   msaitoh 	struct ether_multi *enm;
   1675  1.28   msaitoh 	struct ether_multistep step;
   1676   1.1    dyoung 	u_int32_t       reg_rctl;
   1677  1.28   msaitoh 	struct ethercom *ec = &adapter->osdep.ec;
   1678   1.1    dyoung 	struct ifnet   *ifp = adapter->ifp;
   1679  1.28   msaitoh 	int		mcnt = 0;
   1680   1.1    dyoung 
   1681   1.1    dyoung 	reg_rctl = IXGBE_READ_REG(&adapter->hw, IXGBE_FCTRL);
   1682   1.1    dyoung 	reg_rctl &= (~IXGBE_FCTRL_UPE);
   1683  1.28   msaitoh 	if (ifp->if_flags & IFF_ALLMULTI)
   1684  1.28   msaitoh 		mcnt = MAX_NUM_MULTICAST_ADDRESSES;
   1685  1.28   msaitoh 	else {
   1686  1.28   msaitoh 		ETHER_FIRST_MULTI(step, ec, enm);
   1687  1.28   msaitoh 		while (enm != NULL) {
   1688  1.28   msaitoh 			if (mcnt == MAX_NUM_MULTICAST_ADDRESSES)
   1689  1.28   msaitoh 				break;
   1690  1.28   msaitoh 			mcnt++;
   1691  1.28   msaitoh 			ETHER_NEXT_MULTI(step, enm);
   1692  1.28   msaitoh 		}
   1693  1.28   msaitoh 	}
   1694  1.28   msaitoh 	if (mcnt < MAX_NUM_MULTICAST_ADDRESSES)
   1695  1.28   msaitoh 		reg_rctl &= (~IXGBE_FCTRL_MPE);
   1696   1.1    dyoung 	IXGBE_WRITE_REG(&adapter->hw, IXGBE_FCTRL, reg_rctl);
   1697   1.1    dyoung 
   1698   1.1    dyoung 	if (ifp->if_flags & IFF_PROMISC) {
   1699   1.1    dyoung 		reg_rctl |= (IXGBE_FCTRL_UPE | IXGBE_FCTRL_MPE);
   1700   1.1    dyoung 		IXGBE_WRITE_REG(&adapter->hw, IXGBE_FCTRL, reg_rctl);
   1701   1.1    dyoung 	} else if (ifp->if_flags & IFF_ALLMULTI) {
   1702   1.1    dyoung 		reg_rctl |= IXGBE_FCTRL_MPE;
   1703   1.1    dyoung 		reg_rctl &= ~IXGBE_FCTRL_UPE;
   1704   1.1    dyoung 		IXGBE_WRITE_REG(&adapter->hw, IXGBE_FCTRL, reg_rctl);
   1705   1.1    dyoung 	}
   1706   1.1    dyoung 	return;
   1707   1.1    dyoung }
   1708   1.1    dyoung 
   1709   1.1    dyoung 
   1710   1.1    dyoung /*********************************************************************
   1711   1.1    dyoung  *  Multicast Update
   1712   1.1    dyoung  *
   1713   1.1    dyoung  *  This routine is called whenever multicast address list is updated.
   1714   1.1    dyoung  *
   1715   1.1    dyoung  **********************************************************************/
   1716   1.1    dyoung #define IXGBE_RAR_ENTRIES 16
   1717   1.1    dyoung 
   1718   1.1    dyoung static void
   1719   1.1    dyoung ixgbe_set_multi(struct adapter *adapter)
   1720   1.1    dyoung {
   1721   1.1    dyoung 	struct ether_multi *enm;
   1722   1.1    dyoung 	struct ether_multistep step;
   1723   1.1    dyoung 	u32	fctrl;
   1724   1.1    dyoung 	u8	*mta;
   1725   1.1    dyoung 	u8	*update_ptr;
   1726   1.1    dyoung 	int	mcnt = 0;
   1727   1.1    dyoung 	struct ethercom *ec = &adapter->osdep.ec;
   1728   1.1    dyoung 	struct ifnet   *ifp = adapter->ifp;
   1729   1.1    dyoung 
   1730   1.1    dyoung 	IOCTL_DEBUGOUT("ixgbe_set_multi: begin");
   1731   1.1    dyoung 
   1732   1.1    dyoung 	mta = adapter->mta;
   1733   1.1    dyoung 	bzero(mta, sizeof(u8) * IXGBE_ETH_LENGTH_OF_ADDRESS *
   1734   1.1    dyoung 	    MAX_NUM_MULTICAST_ADDRESSES);
   1735   1.1    dyoung 
   1736  1.28   msaitoh 	ifp->if_flags &= ~IFF_ALLMULTI;
   1737   1.1    dyoung 	ETHER_FIRST_MULTI(step, ec, enm);
   1738   1.1    dyoung 	while (enm != NULL) {
   1739  1.28   msaitoh 		if ((mcnt == MAX_NUM_MULTICAST_ADDRESSES) ||
   1740  1.28   msaitoh 		    (memcmp(enm->enm_addrlo, enm->enm_addrhi,
   1741  1.28   msaitoh 			ETHER_ADDR_LEN) != 0)) {
   1742  1.28   msaitoh 			ifp->if_flags |= IFF_ALLMULTI;
   1743   1.1    dyoung 			break;
   1744   1.1    dyoung 		}
   1745   1.1    dyoung 		bcopy(enm->enm_addrlo,
   1746   1.1    dyoung 		    &mta[mcnt * IXGBE_ETH_LENGTH_OF_ADDRESS],
   1747   1.1    dyoung 		    IXGBE_ETH_LENGTH_OF_ADDRESS);
   1748   1.1    dyoung 		mcnt++;
   1749   1.1    dyoung 		ETHER_NEXT_MULTI(step, enm);
   1750   1.1    dyoung 	}
   1751   1.1    dyoung 
   1752  1.28   msaitoh 	fctrl = IXGBE_READ_REG(&adapter->hw, IXGBE_FCTRL);
   1753  1.28   msaitoh 	fctrl &= ~(IXGBE_FCTRL_UPE | IXGBE_FCTRL_MPE);
   1754  1.28   msaitoh 	if (ifp->if_flags & IFF_PROMISC)
   1755  1.28   msaitoh 		fctrl |= (IXGBE_FCTRL_UPE | IXGBE_FCTRL_MPE);
   1756  1.28   msaitoh 	else if (ifp->if_flags & IFF_ALLMULTI) {
   1757  1.28   msaitoh 		fctrl |= IXGBE_FCTRL_MPE;
   1758  1.28   msaitoh 	}
   1759  1.28   msaitoh 
   1760  1.28   msaitoh 	IXGBE_WRITE_REG(&adapter->hw, IXGBE_FCTRL, fctrl);
   1761  1.28   msaitoh 
   1762  1.28   msaitoh 	if (mcnt < MAX_NUM_MULTICAST_ADDRESSES) {
   1763  1.28   msaitoh 		update_ptr = mta;
   1764  1.28   msaitoh 		ixgbe_update_mc_addr_list(&adapter->hw,
   1765  1.28   msaitoh 		    update_ptr, mcnt, ixgbe_mc_array_itr, TRUE);
   1766  1.28   msaitoh 	}
   1767   1.1    dyoung 
   1768   1.1    dyoung 	return;
   1769   1.1    dyoung }
   1770   1.1    dyoung 
   1771   1.1    dyoung /*
   1772   1.1    dyoung  * This is an iterator function now needed by the multicast
   1773   1.1    dyoung  * shared code. It simply feeds the shared code routine the
   1774   1.1    dyoung  * addresses in the array of ixgbe_set_multi() one by one.
   1775   1.1    dyoung  */
   1776   1.1    dyoung static u8 *
   1777   1.1    dyoung ixgbe_mc_array_itr(struct ixgbe_hw *hw, u8 **update_ptr, u32 *vmdq)
   1778   1.1    dyoung {
   1779   1.1    dyoung 	u8 *addr = *update_ptr;
   1780   1.1    dyoung 	u8 *newptr;
   1781   1.1    dyoung 	*vmdq = 0;
   1782   1.1    dyoung 
   1783   1.1    dyoung 	newptr = addr + IXGBE_ETH_LENGTH_OF_ADDRESS;
   1784   1.1    dyoung 	*update_ptr = newptr;
   1785   1.1    dyoung 	return addr;
   1786   1.1    dyoung }
   1787   1.1    dyoung 
   1788   1.1    dyoung 
   1789   1.1    dyoung /*********************************************************************
   1790   1.1    dyoung  *  Timer routine
   1791   1.1    dyoung  *
   1792   1.1    dyoung  *  This routine checks for link status,updates statistics,
   1793   1.1    dyoung  *  and runs the watchdog check.
   1794   1.1    dyoung  *
   1795   1.1    dyoung  **********************************************************************/
   1796   1.1    dyoung 
   1797   1.1    dyoung static void
   1798   1.1    dyoung ixgbe_local_timer1(void *arg)
   1799   1.1    dyoung {
   1800  1.24   msaitoh 	struct adapter	*adapter = arg;
   1801   1.1    dyoung 	device_t	dev = adapter->dev;
   1802  1.24   msaitoh 	struct ix_queue *que = adapter->queues;
   1803  1.24   msaitoh 	struct tx_ring	*txr = adapter->tx_rings;
   1804  1.26   msaitoh 	int		hung = 0, paused = 0;
   1805   1.1    dyoung 
   1806   1.1    dyoung 	KASSERT(mutex_owned(&adapter->core_mtx));
   1807   1.1    dyoung 
   1808   1.1    dyoung 	/* Check for pluggable optics */
   1809   1.1    dyoung 	if (adapter->sfp_probe)
   1810   1.1    dyoung 		if (!ixgbe_sfp_probe(adapter))
   1811   1.1    dyoung 			goto out; /* Nothing to do */
   1812   1.1    dyoung 
   1813   1.1    dyoung 	ixgbe_update_link_status(adapter);
   1814   1.1    dyoung 	ixgbe_update_stats_counters(adapter);
   1815   1.1    dyoung 
   1816   1.1    dyoung 	/*
   1817   1.1    dyoung 	 * If the interface has been paused
   1818   1.1    dyoung 	 * then don't do the watchdog check
   1819   1.1    dyoung 	 */
   1820   1.1    dyoung 	if (IXGBE_READ_REG(&adapter->hw, IXGBE_TFCS) & IXGBE_TFCS_TXOFF)
   1821  1.24   msaitoh 		paused = 1;
   1822   1.1    dyoung 
   1823   1.1    dyoung 	/*
   1824  1.24   msaitoh 	** Check the TX queues status
   1825  1.24   msaitoh 	**      - watchdog only if all queues show hung
   1826  1.24   msaitoh 	*/
   1827  1.24   msaitoh 	for (int i = 0; i < adapter->num_queues; i++, que++, txr++) {
   1828  1.26   msaitoh 		if ((txr->queue_status == IXGBE_QUEUE_HUNG) &&
   1829  1.24   msaitoh 		    (paused == 0))
   1830  1.24   msaitoh 			++hung;
   1831  1.26   msaitoh 		else if (txr->queue_status == IXGBE_QUEUE_WORKING)
   1832  1.33   msaitoh #ifndef IXGBE_LEGACY_TX
   1833  1.33   msaitoh 			softint_schedule(txr->txq_si);
   1834  1.33   msaitoh #else
   1835  1.24   msaitoh 			softint_schedule(que->que_si);
   1836  1.33   msaitoh #endif
   1837  1.24   msaitoh 	}
   1838  1.24   msaitoh 	/* Only truely watchdog if all queues show hung */
   1839  1.24   msaitoh 	if (hung == adapter->num_queues)
   1840  1.24   msaitoh 		goto watchdog;
   1841   1.1    dyoung 
   1842   1.1    dyoung out:
   1843   1.1    dyoung 	callout_reset(&adapter->timer, hz, ixgbe_local_timer, adapter);
   1844   1.1    dyoung 	return;
   1845   1.1    dyoung 
   1846  1.24   msaitoh watchdog:
   1847   1.1    dyoung 	device_printf(adapter->dev, "Watchdog timeout -- resetting\n");
   1848   1.1    dyoung 	device_printf(dev,"Queue(%d) tdh = %d, hw tdt = %d\n", txr->me,
   1849   1.1    dyoung 	    IXGBE_READ_REG(&adapter->hw, IXGBE_TDH(txr->me)),
   1850   1.1    dyoung 	    IXGBE_READ_REG(&adapter->hw, IXGBE_TDT(txr->me)));
   1851   1.1    dyoung 	device_printf(dev,"TX(%d) desc avail = %d,"
   1852   1.1    dyoung 	    "Next TX to Clean = %d\n",
   1853   1.1    dyoung 	    txr->me, txr->tx_avail, txr->next_to_clean);
   1854   1.1    dyoung 	adapter->ifp->if_flags &= ~IFF_RUNNING;
   1855   1.1    dyoung 	adapter->watchdog_events.ev_count++;
   1856   1.1    dyoung 	ixgbe_init_locked(adapter);
   1857   1.1    dyoung }
   1858   1.1    dyoung 
   1859   1.1    dyoung static void
   1860   1.1    dyoung ixgbe_local_timer(void *arg)
   1861   1.1    dyoung {
   1862   1.1    dyoung 	struct adapter *adapter = arg;
   1863   1.1    dyoung 
   1864   1.1    dyoung 	IXGBE_CORE_LOCK(adapter);
   1865   1.1    dyoung 	ixgbe_local_timer1(adapter);
   1866   1.1    dyoung 	IXGBE_CORE_UNLOCK(adapter);
   1867   1.1    dyoung }
   1868   1.1    dyoung 
   1869   1.1    dyoung /*
   1870   1.1    dyoung ** Note: this routine updates the OS on the link state
   1871   1.1    dyoung **	the real check of the hardware only happens with
   1872   1.1    dyoung **	a link interrupt.
   1873   1.1    dyoung */
   1874   1.1    dyoung static void
   1875   1.1    dyoung ixgbe_update_link_status(struct adapter *adapter)
   1876   1.1    dyoung {
   1877   1.1    dyoung 	struct ifnet	*ifp = adapter->ifp;
   1878   1.1    dyoung 	device_t dev = adapter->dev;
   1879   1.1    dyoung 
   1880   1.1    dyoung 
   1881   1.1    dyoung 	if (adapter->link_up){
   1882   1.1    dyoung 		if (adapter->link_active == FALSE) {
   1883   1.1    dyoung 			if (bootverbose)
   1884   1.1    dyoung 				device_printf(dev,"Link is up %d Gbps %s \n",
   1885   1.1    dyoung 				    ((adapter->link_speed == 128)? 10:1),
   1886   1.1    dyoung 				    "Full Duplex");
   1887   1.1    dyoung 			adapter->link_active = TRUE;
   1888  1.25   msaitoh 			/* Update any Flow Control changes */
   1889  1.25   msaitoh 			ixgbe_fc_enable(&adapter->hw);
   1890   1.1    dyoung 			if_link_state_change(ifp, LINK_STATE_UP);
   1891   1.1    dyoung 		}
   1892   1.1    dyoung 	} else { /* Link down */
   1893   1.1    dyoung 		if (adapter->link_active == TRUE) {
   1894   1.1    dyoung 			if (bootverbose)
   1895   1.1    dyoung 				device_printf(dev,"Link is Down\n");
   1896   1.1    dyoung 			if_link_state_change(ifp, LINK_STATE_DOWN);
   1897   1.1    dyoung 			adapter->link_active = FALSE;
   1898   1.1    dyoung 		}
   1899   1.1    dyoung 	}
   1900   1.1    dyoung 
   1901   1.1    dyoung 	return;
   1902   1.1    dyoung }
   1903   1.1    dyoung 
   1904   1.1    dyoung 
   1905   1.1    dyoung static void
   1906   1.1    dyoung ixgbe_ifstop(struct ifnet *ifp, int disable)
   1907   1.1    dyoung {
   1908   1.1    dyoung 	struct adapter *adapter = ifp->if_softc;
   1909   1.1    dyoung 
   1910   1.1    dyoung 	IXGBE_CORE_LOCK(adapter);
   1911   1.1    dyoung 	ixgbe_stop(adapter);
   1912   1.1    dyoung 	IXGBE_CORE_UNLOCK(adapter);
   1913   1.1    dyoung }
   1914   1.1    dyoung 
   1915   1.1    dyoung /*********************************************************************
   1916   1.1    dyoung  *
   1917   1.1    dyoung  *  This routine disables all traffic on the adapter by issuing a
   1918   1.1    dyoung  *  global reset on the MAC and deallocates TX/RX buffers.
   1919   1.1    dyoung  *
   1920   1.1    dyoung  **********************************************************************/
   1921   1.1    dyoung 
   1922   1.1    dyoung static void
   1923   1.1    dyoung ixgbe_stop(void *arg)
   1924   1.1    dyoung {
   1925   1.1    dyoung 	struct ifnet   *ifp;
   1926   1.1    dyoung 	struct adapter *adapter = arg;
   1927   1.1    dyoung 	struct ixgbe_hw *hw = &adapter->hw;
   1928   1.1    dyoung 	ifp = adapter->ifp;
   1929   1.1    dyoung 
   1930   1.1    dyoung 	KASSERT(mutex_owned(&adapter->core_mtx));
   1931   1.1    dyoung 
   1932   1.1    dyoung 	INIT_DEBUGOUT("ixgbe_stop: begin\n");
   1933   1.1    dyoung 	ixgbe_disable_intr(adapter);
   1934  1.24   msaitoh 	callout_stop(&adapter->timer);
   1935   1.1    dyoung 
   1936  1.24   msaitoh 	/* Let the stack know...*/
   1937  1.24   msaitoh 	ifp->if_flags &= ~IFF_RUNNING;
   1938   1.1    dyoung 
   1939   1.1    dyoung 	ixgbe_reset_hw(hw);
   1940   1.1    dyoung 	hw->adapter_stopped = FALSE;
   1941   1.1    dyoung 	ixgbe_stop_adapter(hw);
   1942  1.33   msaitoh 	if (hw->mac.type == ixgbe_mac_82599EB)
   1943  1.33   msaitoh 		ixgbe_stop_mac_link_on_d3_82599(hw);
   1944  1.33   msaitoh 	/* Turn off the laser - noop with no optics */
   1945  1.33   msaitoh 	ixgbe_disable_tx_laser(hw);
   1946  1.33   msaitoh 
   1947  1.33   msaitoh 	/* Update the stack */
   1948  1.33   msaitoh 	adapter->link_up = FALSE;
   1949  1.33   msaitoh 	ixgbe_update_link_status(adapter);
   1950   1.1    dyoung 
   1951   1.1    dyoung 	/* reprogram the RAR[0] in case user changed it. */
   1952   1.1    dyoung 	ixgbe_set_rar(&adapter->hw, 0, adapter->hw.mac.addr, 0, IXGBE_RAH_AV);
   1953   1.1    dyoung 
   1954   1.1    dyoung 	return;
   1955   1.1    dyoung }
   1956   1.1    dyoung 
   1957   1.1    dyoung 
   1958   1.1    dyoung /*********************************************************************
   1959   1.1    dyoung  *
   1960   1.1    dyoung  *  Determine hardware revision.
   1961   1.1    dyoung  *
   1962   1.1    dyoung  **********************************************************************/
   1963   1.1    dyoung static void
   1964   1.1    dyoung ixgbe_identify_hardware(struct adapter *adapter)
   1965   1.1    dyoung {
   1966   1.1    dyoung 	pcitag_t tag;
   1967   1.1    dyoung 	pci_chipset_tag_t pc;
   1968   1.1    dyoung 	pcireg_t subid, id;
   1969   1.1    dyoung 	struct ixgbe_hw *hw = &adapter->hw;
   1970   1.1    dyoung 
   1971   1.1    dyoung 	pc = adapter->osdep.pc;
   1972   1.1    dyoung 	tag = adapter->osdep.tag;
   1973   1.1    dyoung 
   1974   1.1    dyoung 	id = pci_conf_read(pc, tag, PCI_ID_REG);
   1975   1.1    dyoung 	subid = pci_conf_read(pc, tag, PCI_SUBSYS_ID_REG);
   1976   1.1    dyoung 
   1977   1.1    dyoung 	/* Save off the information about this board */
   1978   1.1    dyoung 	hw->vendor_id = PCI_VENDOR(id);
   1979   1.1    dyoung 	hw->device_id = PCI_PRODUCT(id);
   1980   1.1    dyoung 	hw->revision_id =
   1981   1.1    dyoung 	    PCI_REVISION(pci_conf_read(pc, tag, PCI_CLASS_REG));
   1982   1.1    dyoung 	hw->subsystem_vendor_id = PCI_SUBSYS_VENDOR(subid);
   1983   1.1    dyoung 	hw->subsystem_device_id = PCI_SUBSYS_ID(subid);
   1984   1.1    dyoung 
   1985   1.1    dyoung 	/* We need this here to set the num_segs below */
   1986   1.1    dyoung 	ixgbe_set_mac_type(hw);
   1987   1.1    dyoung 
   1988   1.1    dyoung 	/* Pick up the 82599 and VF settings */
   1989   1.1    dyoung 	if (hw->mac.type != ixgbe_mac_82598EB) {
   1990   1.1    dyoung 		hw->phy.smart_speed = ixgbe_smart_speed;
   1991   1.1    dyoung 		adapter->num_segs = IXGBE_82599_SCATTER;
   1992   1.1    dyoung 	} else
   1993   1.1    dyoung 		adapter->num_segs = IXGBE_82598_SCATTER;
   1994   1.1    dyoung 
   1995   1.1    dyoung 	return;
   1996   1.1    dyoung }
   1997   1.1    dyoung 
   1998   1.1    dyoung /*********************************************************************
   1999   1.1    dyoung  *
   2000   1.1    dyoung  *  Determine optic type
   2001   1.1    dyoung  *
   2002   1.1    dyoung  **********************************************************************/
   2003   1.1    dyoung static void
   2004   1.1    dyoung ixgbe_setup_optics(struct adapter *adapter)
   2005   1.1    dyoung {
   2006   1.1    dyoung 	struct ixgbe_hw *hw = &adapter->hw;
   2007   1.1    dyoung 	int		layer;
   2008  1.28   msaitoh 
   2009   1.1    dyoung 	layer = ixgbe_get_supported_physical_layer(hw);
   2010  1.26   msaitoh 
   2011  1.24   msaitoh 	if (layer & IXGBE_PHYSICAL_LAYER_10GBASE_T) {
   2012  1.24   msaitoh 		adapter->optics = IFM_10G_T;
   2013  1.24   msaitoh 		return;
   2014  1.26   msaitoh 	}
   2015  1.24   msaitoh 
   2016  1.24   msaitoh 	if (layer & IXGBE_PHYSICAL_LAYER_1000BASE_T) {
   2017  1.24   msaitoh 		adapter->optics = IFM_1000_T;
   2018  1.24   msaitoh 		return;
   2019  1.24   msaitoh 	}
   2020  1.24   msaitoh 
   2021  1.26   msaitoh 	if (layer & IXGBE_PHYSICAL_LAYER_1000BASE_SX) {
   2022  1.26   msaitoh 		adapter->optics = IFM_1000_SX;
   2023  1.26   msaitoh 		return;
   2024  1.26   msaitoh 	}
   2025  1.26   msaitoh 
   2026  1.24   msaitoh 	if (layer & (IXGBE_PHYSICAL_LAYER_10GBASE_LR |
   2027  1.24   msaitoh 	    IXGBE_PHYSICAL_LAYER_10GBASE_LRM)) {
   2028  1.24   msaitoh 		adapter->optics = IFM_10G_LR;
   2029  1.24   msaitoh 		return;
   2030  1.24   msaitoh 	}
   2031  1.24   msaitoh 
   2032  1.24   msaitoh 	if (layer & IXGBE_PHYSICAL_LAYER_10GBASE_SR) {
   2033  1.24   msaitoh 		adapter->optics = IFM_10G_SR;
   2034  1.24   msaitoh 		return;
   2035  1.24   msaitoh 	}
   2036  1.24   msaitoh 
   2037  1.24   msaitoh 	if (layer & IXGBE_PHYSICAL_LAYER_SFP_PLUS_CU) {
   2038  1.24   msaitoh 		adapter->optics = IFM_10G_TWINAX;
   2039  1.24   msaitoh 		return;
   2040  1.24   msaitoh 	}
   2041  1.24   msaitoh 
   2042  1.24   msaitoh 	if (layer & (IXGBE_PHYSICAL_LAYER_10GBASE_KX4 |
   2043  1.24   msaitoh 	    IXGBE_PHYSICAL_LAYER_10GBASE_CX4)) {
   2044  1.24   msaitoh 		adapter->optics = IFM_10G_CX4;
   2045  1.24   msaitoh 		return;
   2046   1.1    dyoung 	}
   2047  1.24   msaitoh 
   2048  1.24   msaitoh 	/* If we get here just set the default */
   2049  1.24   msaitoh 	adapter->optics = IFM_ETHER | IFM_AUTO;
   2050   1.1    dyoung 	return;
   2051   1.1    dyoung }
   2052   1.1    dyoung 
   2053   1.1    dyoung /*********************************************************************
   2054   1.1    dyoung  *
   2055   1.1    dyoung  *  Setup the Legacy or MSI Interrupt handler
   2056   1.1    dyoung  *
   2057   1.1    dyoung  **********************************************************************/
   2058   1.1    dyoung static int
   2059  1.34   msaitoh ixgbe_allocate_legacy(struct adapter *adapter,
   2060  1.34   msaitoh     const struct pci_attach_args *pa)
   2061   1.1    dyoung {
   2062  1.28   msaitoh 	device_t	dev = adapter->dev;
   2063   1.1    dyoung 	struct		ix_queue *que = adapter->queues;
   2064  1.28   msaitoh #ifndef IXGBE_LEGACY_TX
   2065  1.26   msaitoh 	struct tx_ring		*txr = adapter->tx_rings;
   2066  1.26   msaitoh #endif
   2067  1.34   msaitoh 	int		counts[PCI_INTR_TYPE_SIZE];
   2068  1.34   msaitoh 	pci_intr_type_t intr_type, max_type;
   2069   1.9     skrll 	char intrbuf[PCI_INTRSTR_LEN];
   2070  1.34   msaitoh 	const char	*intrstr = NULL;
   2071   1.1    dyoung 
   2072  1.34   msaitoh 	/* Allocation settings */
   2073  1.34   msaitoh 	max_type = PCI_INTR_TYPE_MSI;
   2074  1.34   msaitoh 	counts[PCI_INTR_TYPE_MSIX] = 0;
   2075  1.34   msaitoh 	counts[PCI_INTR_TYPE_MSI] = 1;
   2076  1.34   msaitoh 	counts[PCI_INTR_TYPE_INTX] = 1;
   2077  1.34   msaitoh 
   2078  1.34   msaitoh alloc_retry:
   2079  1.34   msaitoh 	if (pci_intr_alloc(pa, &adapter->osdep.intrs, counts, max_type) != 0) {
   2080  1.34   msaitoh 		aprint_error_dev(dev, "couldn't alloc interrupt\n");
   2081  1.34   msaitoh 		return ENXIO;
   2082  1.34   msaitoh 	}
   2083  1.34   msaitoh 	adapter->osdep.nintrs = 1;
   2084  1.34   msaitoh 	intrstr = pci_intr_string(adapter->osdep.pc, adapter->osdep.intrs[0],
   2085  1.34   msaitoh 	    intrbuf, sizeof(intrbuf));
   2086  1.40  jdolecek 	adapter->osdep.ihs[0] = pci_intr_establish_xname(adapter->osdep.pc,
   2087  1.40  jdolecek 	    adapter->osdep.intrs[0], IPL_NET, ixgbe_legacy_irq, que,
   2088  1.40  jdolecek 	    device_xname(dev));
   2089  1.34   msaitoh 	if (adapter->osdep.ihs[0] == NULL) {
   2090  1.39  knakahar 		intr_type = pci_intr_type(adapter->osdep.pc,
   2091  1.39  knakahar 		    adapter->osdep.intrs[0]);
   2092  1.34   msaitoh 		aprint_error_dev(dev,"unable to establish %s\n",
   2093  1.34   msaitoh 		    (intr_type == PCI_INTR_TYPE_MSI) ? "MSI" : "INTx");
   2094  1.34   msaitoh 		pci_intr_release(adapter->osdep.pc, adapter->osdep.intrs, 1);
   2095  1.34   msaitoh 		switch (intr_type) {
   2096  1.34   msaitoh 		case PCI_INTR_TYPE_MSI:
   2097  1.34   msaitoh 			/* The next try is for INTx: Disable MSI */
   2098  1.34   msaitoh 			max_type = PCI_INTR_TYPE_INTX;
   2099  1.34   msaitoh 			counts[PCI_INTR_TYPE_INTX] = 1;
   2100  1.34   msaitoh 			goto alloc_retry;
   2101  1.34   msaitoh 		case PCI_INTR_TYPE_INTX:
   2102  1.34   msaitoh 		default:
   2103  1.34   msaitoh 			/* See below */
   2104  1.34   msaitoh 			break;
   2105  1.34   msaitoh 		}
   2106   1.1    dyoung 	}
   2107  1.34   msaitoh 	if (adapter->osdep.ihs[0] == NULL) {
   2108  1.34   msaitoh 		aprint_error_dev(dev,
   2109  1.34   msaitoh 		    "couldn't establish interrupt%s%s\n",
   2110  1.34   msaitoh 		    intrstr ? " at " : "", intrstr ? intrstr : "");
   2111  1.34   msaitoh 		pci_intr_release(adapter->osdep.pc, adapter->osdep.intrs, 1);
   2112  1.34   msaitoh 		return ENXIO;
   2113  1.34   msaitoh 	}
   2114  1.34   msaitoh 	aprint_normal_dev(dev, "interrupting at %s\n", intrstr);
   2115   1.1    dyoung 	/*
   2116   1.1    dyoung 	 * Try allocating a fast interrupt and the associated deferred
   2117   1.1    dyoung 	 * processing contexts.
   2118   1.1    dyoung 	 */
   2119  1.28   msaitoh #ifndef IXGBE_LEGACY_TX
   2120  1.26   msaitoh 	txr->txq_si = softint_establish(SOFTINT_NET, ixgbe_deferred_mq_start,
   2121  1.26   msaitoh 	    txr);
   2122  1.26   msaitoh #endif
   2123   1.1    dyoung 	que->que_si = softint_establish(SOFTINT_NET, ixgbe_handle_que, que);
   2124   1.1    dyoung 
   2125   1.1    dyoung 	/* Tasklets for Link, SFP and Multispeed Fiber */
   2126   1.1    dyoung 	adapter->link_si =
   2127   1.1    dyoung 	    softint_establish(SOFTINT_NET, ixgbe_handle_link, adapter);
   2128   1.1    dyoung 	adapter->mod_si =
   2129   1.1    dyoung 	    softint_establish(SOFTINT_NET, ixgbe_handle_mod, adapter);
   2130   1.1    dyoung 	adapter->msf_si =
   2131   1.1    dyoung 	    softint_establish(SOFTINT_NET, ixgbe_handle_msf, adapter);
   2132   1.1    dyoung 
   2133   1.1    dyoung #ifdef IXGBE_FDIR
   2134   1.1    dyoung 	adapter->fdir_si =
   2135   1.1    dyoung 	    softint_establish(SOFTINT_NET, ixgbe_reinit_fdir, adapter);
   2136   1.1    dyoung #endif
   2137   1.1    dyoung 	if (que->que_si == NULL ||
   2138   1.1    dyoung 	    adapter->link_si == NULL ||
   2139   1.1    dyoung 	    adapter->mod_si == NULL ||
   2140   1.1    dyoung #ifdef IXGBE_FDIR
   2141   1.1    dyoung 	    adapter->fdir_si == NULL ||
   2142   1.1    dyoung #endif
   2143   1.1    dyoung 	    adapter->msf_si == NULL) {
   2144   1.1    dyoung 		aprint_error_dev(dev,
   2145   1.1    dyoung 		    "could not establish software interrupts\n");
   2146   1.1    dyoung 		return ENXIO;
   2147   1.1    dyoung 	}
   2148   1.1    dyoung 
   2149   1.1    dyoung 	/* For simplicity in the handlers */
   2150   1.1    dyoung 	adapter->que_mask = IXGBE_EIMS_ENABLE_MASK;
   2151   1.1    dyoung 
   2152   1.1    dyoung 	return (0);
   2153   1.1    dyoung }
   2154   1.1    dyoung 
   2155   1.1    dyoung 
   2156   1.1    dyoung /*********************************************************************
   2157   1.1    dyoung  *
   2158   1.1    dyoung  *  Setup MSIX Interrupt resources and handlers
   2159   1.1    dyoung  *
   2160   1.1    dyoung  **********************************************************************/
   2161   1.1    dyoung static int
   2162   1.1    dyoung ixgbe_allocate_msix(struct adapter *adapter, const struct pci_attach_args *pa)
   2163   1.1    dyoung {
   2164   1.1    dyoung 	device_t        dev = adapter->dev;
   2165   1.1    dyoung 	struct 		ix_queue *que = adapter->queues;
   2166  1.26   msaitoh 	struct  	tx_ring *txr = adapter->tx_rings;
   2167  1.34   msaitoh 	pci_chipset_tag_t pc;
   2168  1.34   msaitoh 	char		intrbuf[PCI_INTRSTR_LEN];
   2169  1.40  jdolecek 	char		intr_xname[32];
   2170  1.34   msaitoh 	const char	*intrstr = NULL;
   2171  1.34   msaitoh 	int 		error, vector = 0;
   2172  1.33   msaitoh 	int		cpu_id = 0;
   2173  1.34   msaitoh 	kcpuset_t	*affinity;
   2174  1.34   msaitoh 
   2175  1.34   msaitoh 	pc = adapter->osdep.pc;
   2176  1.33   msaitoh #ifdef	RSS
   2177  1.33   msaitoh 	cpuset_t cpu_mask;
   2178  1.33   msaitoh 	/*
   2179  1.33   msaitoh 	 * If we're doing RSS, the number of queues needs to
   2180  1.33   msaitoh 	 * match the number of RSS buckets that are configured.
   2181  1.33   msaitoh 	 *
   2182  1.33   msaitoh 	 * + If there's more queues than RSS buckets, we'll end
   2183  1.33   msaitoh 	 *   up with queues that get no traffic.
   2184  1.33   msaitoh 	 *
   2185  1.33   msaitoh 	 * + If there's more RSS buckets than queues, we'll end
   2186  1.33   msaitoh 	 *   up having multiple RSS buckets map to the same queue,
   2187  1.33   msaitoh 	 *   so there'll be some contention.
   2188  1.33   msaitoh 	 */
   2189  1.33   msaitoh 	if (adapter->num_queues != rss_getnumbuckets()) {
   2190  1.33   msaitoh 		device_printf(dev,
   2191  1.33   msaitoh 		    "%s: number of queues (%d) != number of RSS buckets (%d)"
   2192  1.33   msaitoh 		    "; performance will be impacted.\n",
   2193  1.33   msaitoh 		    __func__,
   2194  1.33   msaitoh 		    adapter->num_queues,
   2195  1.33   msaitoh 		    rss_getnumbuckets());
   2196  1.33   msaitoh 	}
   2197  1.33   msaitoh #endif
   2198   1.1    dyoung 
   2199  1.34   msaitoh 	adapter->osdep.nintrs = adapter->num_queues + 1;
   2200  1.34   msaitoh 	if (pci_msix_alloc_exact(pa, &adapter->osdep.intrs,
   2201  1.34   msaitoh 	    adapter->osdep.nintrs) != 0) {
   2202  1.34   msaitoh 		aprint_error_dev(dev,
   2203  1.34   msaitoh 		    "failed to allocate MSI-X interrupt\n");
   2204  1.34   msaitoh 		return (ENXIO);
   2205  1.34   msaitoh 	}
   2206  1.34   msaitoh 
   2207  1.34   msaitoh 	kcpuset_create(&affinity, false);
   2208  1.26   msaitoh 	for (int i = 0; i < adapter->num_queues; i++, vector++, que++, txr++) {
   2209  1.40  jdolecek 		snprintf(intr_xname, sizeof(intr_xname), "%s TX/RX",
   2210  1.40  jdolecek 		    device_xname(dev));
   2211  1.34   msaitoh 		intrstr = pci_intr_string(pc, adapter->osdep.intrs[i], intrbuf,
   2212  1.34   msaitoh 		    sizeof(intrbuf));
   2213  1.34   msaitoh #ifdef IXG_MPSAFE
   2214  1.34   msaitoh 		pci_intr_setattr(pc, adapter->osdep.intrs[i], PCI_INTR_MPSAFE,
   2215  1.34   msaitoh 		    true);
   2216  1.34   msaitoh #endif
   2217  1.34   msaitoh 		/* Set the handler function */
   2218  1.40  jdolecek 		que->res = adapter->osdep.ihs[i] = pci_intr_establish_xname(pc,
   2219  1.40  jdolecek 		    adapter->osdep.intrs[i], IPL_NET, ixgbe_msix_que, que,
   2220  1.40  jdolecek 		    intr_xname);
   2221   1.1    dyoung 		if (que->res == NULL) {
   2222  1.34   msaitoh 			pci_intr_release(pc, adapter->osdep.intrs,
   2223  1.34   msaitoh 			    adapter->osdep.nintrs);
   2224   1.1    dyoung 			aprint_error_dev(dev,
   2225   1.1    dyoung 			    "Failed to register QUE handler\n");
   2226  1.34   msaitoh 			kcpuset_destroy(affinity);
   2227  1.34   msaitoh 			return ENXIO;
   2228   1.1    dyoung 		}
   2229   1.1    dyoung 		que->msix = vector;
   2230   1.1    dyoung         	adapter->que_mask |= (u64)(1 << que->msix);
   2231  1.33   msaitoh #ifdef	RSS
   2232  1.35   msaitoh 		/*
   2233  1.33   msaitoh 		 * The queue ID is used as the RSS layer bucket ID.
   2234  1.33   msaitoh 		 * We look up the queue ID -> RSS CPU ID and select
   2235  1.33   msaitoh 		 * that.
   2236  1.33   msaitoh 		 */
   2237  1.33   msaitoh 		cpu_id = rss_getcpu(i % rss_getnumbuckets());
   2238  1.33   msaitoh #else
   2239   1.1    dyoung 		/*
   2240  1.33   msaitoh 		 * Bind the msix vector, and thus the
   2241  1.35   msaitoh 		 * rings to the corresponding cpu.
   2242  1.33   msaitoh 		 *
   2243  1.33   msaitoh 		 * This just happens to match the default RSS round-robin
   2244  1.33   msaitoh 		 * bucket -> queue -> CPU allocation.
   2245  1.33   msaitoh 		 */
   2246  1.33   msaitoh 		if (adapter->num_queues > 1)
   2247  1.33   msaitoh 			cpu_id = i;
   2248  1.33   msaitoh #endif
   2249  1.34   msaitoh 		/* Round-robin affinity */
   2250  1.34   msaitoh 		kcpuset_zero(affinity);
   2251  1.34   msaitoh 		kcpuset_set(affinity, cpu_id % ncpu);
   2252  1.36  knakahar 		error = interrupt_distribute(adapter->osdep.ihs[i], affinity,
   2253  1.34   msaitoh 		    NULL);
   2254  1.34   msaitoh 		aprint_normal_dev(dev, "for TX/RX, interrupting at %s",
   2255  1.34   msaitoh 		    intrstr);
   2256  1.34   msaitoh 		if (error == 0) {
   2257  1.33   msaitoh #ifdef	RSS
   2258  1.34   msaitoh 			aprintf_normal(", bound RSS bucket %d to CPU %d\n",
   2259  1.34   msaitoh 			    i, cpu_id);
   2260  1.33   msaitoh #else
   2261  1.34   msaitoh 			aprint_normal(", bound queue %d to cpu %d\n",
   2262  1.34   msaitoh 			    i, cpu_id);
   2263  1.33   msaitoh #endif
   2264  1.34   msaitoh 		} else
   2265  1.34   msaitoh 			aprint_normal("\n");
   2266   1.1    dyoung 
   2267  1.28   msaitoh #ifndef IXGBE_LEGACY_TX
   2268  1.26   msaitoh 		txr->txq_si = softint_establish(SOFTINT_NET,
   2269  1.26   msaitoh 		    ixgbe_deferred_mq_start, txr);
   2270  1.26   msaitoh #endif
   2271  1.26   msaitoh 		que->que_si = softint_establish(SOFTINT_NET, ixgbe_handle_que,
   2272  1.26   msaitoh 		    que);
   2273   1.1    dyoung 		if (que->que_si == NULL) {
   2274   1.1    dyoung 			aprint_error_dev(dev,
   2275   1.1    dyoung 			    "could not establish software interrupt\n");
   2276   1.1    dyoung 		}
   2277   1.1    dyoung 	}
   2278   1.1    dyoung 
   2279   1.1    dyoung 	/* and Link */
   2280  1.34   msaitoh 	cpu_id++;
   2281  1.40  jdolecek 	snprintf(intr_xname, sizeof(intr_xname), "%s link", device_xname(dev));
   2282  1.34   msaitoh 	intrstr = pci_intr_string(pc, adapter->osdep.intrs[vector], intrbuf,
   2283  1.34   msaitoh 	    sizeof(intrbuf));
   2284  1.34   msaitoh #ifdef IXG_MPSAFE
   2285  1.34   msaitoh 	pci_intr_setattr(pc, &adapter->osdep.intrs[vector], PCI_INTR_MPSAFE,
   2286  1.34   msaitoh 	    true);
   2287  1.34   msaitoh #endif
   2288   1.1    dyoung 	/* Set the link handler function */
   2289  1.40  jdolecek 	adapter->osdep.ihs[vector] = pci_intr_establish_xname(pc,
   2290  1.40  jdolecek 	    adapter->osdep.intrs[vector], IPL_NET, ixgbe_msix_link, adapter,
   2291  1.40  jdolecek 	    intr_xname);
   2292  1.34   msaitoh 	if (adapter->osdep.ihs[vector] == NULL) {
   2293   1.1    dyoung 		adapter->res = NULL;
   2294   1.1    dyoung 		aprint_error_dev(dev, "Failed to register LINK handler\n");
   2295  1.34   msaitoh 		kcpuset_destroy(affinity);
   2296  1.34   msaitoh 		return (ENXIO);
   2297   1.1    dyoung 	}
   2298  1.34   msaitoh 	/* Round-robin affinity */
   2299  1.34   msaitoh 	kcpuset_zero(affinity);
   2300  1.34   msaitoh 	kcpuset_set(affinity, cpu_id % ncpu);
   2301  1.36  knakahar 	error = interrupt_distribute(adapter->osdep.ihs[vector], affinity,NULL);
   2302  1.34   msaitoh 
   2303  1.34   msaitoh 	aprint_normal_dev(dev,
   2304  1.34   msaitoh 	    "for link, interrupting at %s", intrstr);
   2305  1.34   msaitoh 	if (error == 0)
   2306  1.34   msaitoh 		aprint_normal(", affinity to cpu %d\n", cpu_id);
   2307  1.34   msaitoh 	else
   2308  1.34   msaitoh 		aprint_normal("\n");
   2309  1.34   msaitoh 
   2310   1.1    dyoung 	adapter->linkvec = vector;
   2311   1.1    dyoung 	/* Tasklets for Link, SFP and Multispeed Fiber */
   2312   1.1    dyoung 	adapter->link_si =
   2313   1.1    dyoung 	    softint_establish(SOFTINT_NET, ixgbe_handle_link, adapter);
   2314   1.1    dyoung 	adapter->mod_si =
   2315   1.1    dyoung 	    softint_establish(SOFTINT_NET, ixgbe_handle_mod, adapter);
   2316   1.1    dyoung 	adapter->msf_si =
   2317   1.1    dyoung 	    softint_establish(SOFTINT_NET, ixgbe_handle_msf, adapter);
   2318   1.1    dyoung #ifdef IXGBE_FDIR
   2319   1.1    dyoung 	adapter->fdir_si =
   2320   1.1    dyoung 	    softint_establish(SOFTINT_NET, ixgbe_reinit_fdir, adapter);
   2321   1.1    dyoung #endif
   2322   1.1    dyoung 
   2323  1.34   msaitoh 	kcpuset_destroy(affinity);
   2324   1.1    dyoung 	return (0);
   2325   1.1    dyoung }
   2326   1.1    dyoung 
   2327   1.1    dyoung /*
   2328   1.1    dyoung  * Setup Either MSI/X or MSI
   2329   1.1    dyoung  */
   2330   1.1    dyoung static int
   2331   1.1    dyoung ixgbe_setup_msix(struct adapter *adapter)
   2332   1.1    dyoung {
   2333   1.1    dyoung 	device_t dev = adapter->dev;
   2334  1.34   msaitoh 	int want, queues, msgs;
   2335   1.1    dyoung 
   2336   1.1    dyoung 	/* Override by tuneable */
   2337   1.1    dyoung 	if (ixgbe_enable_msix == 0)
   2338   1.1    dyoung 		goto msi;
   2339   1.1    dyoung 
   2340   1.1    dyoung 	/* First try MSI/X */
   2341  1.34   msaitoh 	msgs = pci_msix_count(adapter->osdep.pc, adapter->osdep.tag);
   2342  1.34   msaitoh 	if (msgs < IXG_MSIX_NINTR)
   2343  1.33   msaitoh 		goto msi;
   2344  1.34   msaitoh 
   2345  1.34   msaitoh 	adapter->msix_mem = (void *)1; /* XXX */
   2346   1.1    dyoung 
   2347   1.1    dyoung 	/* Figure out a reasonable auto config value */
   2348  1.34   msaitoh 	queues = (ncpu > (msgs-1)) ? (msgs-1) : ncpu;
   2349   1.1    dyoung 
   2350  1.33   msaitoh 	/* Override based on tuneable */
   2351   1.1    dyoung 	if (ixgbe_num_queues != 0)
   2352   1.1    dyoung 		queues = ixgbe_num_queues;
   2353  1.33   msaitoh 
   2354  1.33   msaitoh #ifdef	RSS
   2355  1.33   msaitoh 	/* If we're doing RSS, clamp at the number of RSS buckets */
   2356  1.33   msaitoh 	if (queues > rss_getnumbuckets())
   2357  1.33   msaitoh 		queues = rss_getnumbuckets();
   2358  1.33   msaitoh #endif
   2359  1.33   msaitoh 
   2360  1.33   msaitoh 	/* reflect correct sysctl value */
   2361  1.33   msaitoh 	ixgbe_num_queues = queues;
   2362   1.1    dyoung 
   2363   1.1    dyoung 	/*
   2364   1.1    dyoung 	** Want one vector (RX/TX pair) per queue
   2365   1.1    dyoung 	** plus an additional for Link.
   2366   1.1    dyoung 	*/
   2367   1.1    dyoung 	want = queues + 1;
   2368   1.1    dyoung 	if (msgs >= want)
   2369   1.1    dyoung 		msgs = want;
   2370   1.1    dyoung 	else {
   2371  1.34   msaitoh                	aprint_error_dev(dev,
   2372   1.1    dyoung 		    "MSIX Configuration Problem, "
   2373   1.1    dyoung 		    "%d vectors but %d queues wanted!\n",
   2374   1.1    dyoung 		    msgs, want);
   2375  1.33   msaitoh 		goto msi;
   2376   1.1    dyoung 	}
   2377  1.34   msaitoh 	device_printf(dev,
   2378  1.34   msaitoh 	    "Using MSIX interrupts with %d vectors\n", msgs);
   2379  1.34   msaitoh 	adapter->num_queues = queues;
   2380  1.34   msaitoh 	return (msgs);
   2381  1.34   msaitoh 
   2382  1.33   msaitoh 	/*
   2383  1.33   msaitoh 	** If MSIX alloc failed or provided us with
   2384  1.33   msaitoh 	** less than needed, free and fall through to MSI
   2385  1.33   msaitoh 	*/
   2386   1.1    dyoung msi:
   2387  1.34   msaitoh        	msgs = pci_msi_count(adapter->osdep.pc, adapter->osdep.tag);
   2388  1.34   msaitoh 	adapter->msix_mem = NULL; /* XXX */
   2389  1.33   msaitoh        	msgs = 1;
   2390  1.34   msaitoh 	aprint_normal_dev(dev,"Using an MSI interrupt\n");
   2391  1.34   msaitoh 	return (msgs);
   2392   1.1    dyoung }
   2393   1.1    dyoung 
   2394   1.1    dyoung 
   2395   1.1    dyoung static int
   2396  1.34   msaitoh ixgbe_allocate_pci_resources(struct adapter *adapter,
   2397  1.34   msaitoh     const struct pci_attach_args *pa)
   2398   1.1    dyoung {
   2399   1.1    dyoung 	pcireg_t	memtype;
   2400   1.1    dyoung 	device_t        dev = adapter->dev;
   2401   1.1    dyoung 	bus_addr_t addr;
   2402   1.1    dyoung 	int flags;
   2403   1.1    dyoung 
   2404   1.1    dyoung 	memtype = pci_mapreg_type(pa->pa_pc, pa->pa_tag, PCI_BAR(0));
   2405   1.1    dyoung 	switch (memtype) {
   2406   1.1    dyoung 	case PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT:
   2407   1.1    dyoung 	case PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_64BIT:
   2408   1.1    dyoung 		adapter->osdep.mem_bus_space_tag = pa->pa_memt;
   2409   1.1    dyoung 		if (pci_mapreg_info(pa->pa_pc, pa->pa_tag, PCI_BAR(0),
   2410   1.1    dyoung 	              memtype, &addr, &adapter->osdep.mem_size, &flags) != 0)
   2411   1.1    dyoung 			goto map_err;
   2412   1.1    dyoung 		if ((flags & BUS_SPACE_MAP_PREFETCHABLE) != 0) {
   2413   1.1    dyoung 			aprint_normal_dev(dev, "clearing prefetchable bit\n");
   2414   1.1    dyoung 			flags &= ~BUS_SPACE_MAP_PREFETCHABLE;
   2415   1.1    dyoung 		}
   2416   1.1    dyoung 		if (bus_space_map(adapter->osdep.mem_bus_space_tag, addr,
   2417   1.1    dyoung 		     adapter->osdep.mem_size, flags,
   2418   1.1    dyoung 		     &adapter->osdep.mem_bus_space_handle) != 0) {
   2419   1.1    dyoung map_err:
   2420   1.1    dyoung 			adapter->osdep.mem_size = 0;
   2421   1.1    dyoung 			aprint_error_dev(dev, "unable to map BAR0\n");
   2422   1.1    dyoung 			return ENXIO;
   2423   1.1    dyoung 		}
   2424   1.1    dyoung 		break;
   2425   1.1    dyoung 	default:
   2426   1.1    dyoung 		aprint_error_dev(dev, "unexpected type on BAR0\n");
   2427   1.1    dyoung 		return ENXIO;
   2428   1.1    dyoung 	}
   2429   1.1    dyoung 
   2430   1.1    dyoung 	/* Legacy defaults */
   2431   1.1    dyoung 	adapter->num_queues = 1;
   2432   1.1    dyoung 	adapter->hw.back = &adapter->osdep;
   2433   1.1    dyoung 
   2434   1.1    dyoung 	/*
   2435   1.1    dyoung 	** Now setup MSI or MSI/X, should
   2436   1.1    dyoung 	** return us the number of supported
   2437   1.1    dyoung 	** vectors. (Will be 1 for MSI)
   2438   1.1    dyoung 	*/
   2439   1.1    dyoung 	adapter->msix = ixgbe_setup_msix(adapter);
   2440   1.1    dyoung 	return (0);
   2441   1.1    dyoung }
   2442   1.1    dyoung 
   2443   1.1    dyoung static void
   2444   1.1    dyoung ixgbe_free_pci_resources(struct adapter * adapter)
   2445   1.1    dyoung {
   2446   1.1    dyoung 	struct 		ix_queue *que = adapter->queues;
   2447   1.9     skrll 	int		rid;
   2448   1.1    dyoung 
   2449   1.1    dyoung 	/*
   2450   1.1    dyoung 	**  Release all msix queue resources:
   2451   1.1    dyoung 	*/
   2452   1.1    dyoung 	for (int i = 0; i < adapter->num_queues; i++, que++) {
   2453   1.1    dyoung 		if (que->res != NULL)
   2454  1.34   msaitoh 			pci_intr_disestablish(adapter->osdep.pc,
   2455  1.34   msaitoh 			    adapter->osdep.ihs[i]);
   2456   1.1    dyoung 	}
   2457   1.1    dyoung 
   2458   1.1    dyoung 	/* Clean the Legacy or Link interrupt last */
   2459   1.1    dyoung 	if (adapter->linkvec) /* we are doing MSIX */
   2460  1.34   msaitoh 		rid = adapter->linkvec;
   2461   1.1    dyoung 	else
   2462  1.34   msaitoh 		rid = 0;
   2463   1.1    dyoung 
   2464  1.34   msaitoh 	if (adapter->osdep.ihs[rid] != NULL) {
   2465  1.34   msaitoh 		pci_intr_disestablish(adapter->osdep.pc,
   2466  1.34   msaitoh 		    adapter->osdep.ihs[rid]);
   2467  1.34   msaitoh 		adapter->osdep.ihs[rid] = NULL;
   2468  1.34   msaitoh 	}
   2469   1.1    dyoung 
   2470  1.34   msaitoh 	pci_intr_release(adapter->osdep.pc, adapter->osdep.intrs,
   2471  1.34   msaitoh 	    adapter->osdep.nintrs);
   2472   1.1    dyoung 
   2473   1.1    dyoung 	if (adapter->osdep.mem_size != 0) {
   2474   1.1    dyoung 		bus_space_unmap(adapter->osdep.mem_bus_space_tag,
   2475   1.1    dyoung 		    adapter->osdep.mem_bus_space_handle,
   2476   1.1    dyoung 		    adapter->osdep.mem_size);
   2477   1.1    dyoung 	}
   2478   1.1    dyoung 
   2479   1.1    dyoung 	return;
   2480   1.1    dyoung }
   2481   1.1    dyoung 
   2482   1.1    dyoung /*********************************************************************
   2483   1.1    dyoung  *
   2484   1.1    dyoung  *  Setup networking device structure and register an interface.
   2485   1.1    dyoung  *
   2486   1.1    dyoung  **********************************************************************/
   2487   1.1    dyoung static int
   2488   1.1    dyoung ixgbe_setup_interface(device_t dev, struct adapter *adapter)
   2489   1.1    dyoung {
   2490   1.1    dyoung 	struct ethercom *ec = &adapter->osdep.ec;
   2491   1.1    dyoung 	struct ixgbe_hw *hw = &adapter->hw;
   2492   1.1    dyoung 	struct ifnet   *ifp;
   2493   1.1    dyoung 
   2494   1.1    dyoung 	INIT_DEBUGOUT("ixgbe_setup_interface: begin");
   2495   1.1    dyoung 
   2496   1.1    dyoung 	ifp = adapter->ifp = &ec->ec_if;
   2497   1.1    dyoung 	strlcpy(ifp->if_xname, device_xname(dev), IFNAMSIZ);
   2498  1.26   msaitoh 	ifp->if_baudrate = IF_Gbps(10);
   2499   1.1    dyoung 	ifp->if_init = ixgbe_init;
   2500   1.1    dyoung 	ifp->if_stop = ixgbe_ifstop;
   2501   1.1    dyoung 	ifp->if_softc = adapter;
   2502   1.1    dyoung 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
   2503   1.1    dyoung 	ifp->if_ioctl = ixgbe_ioctl;
   2504  1.28   msaitoh #ifndef IXGBE_LEGACY_TX
   2505   1.1    dyoung 	ifp->if_transmit = ixgbe_mq_start;
   2506   1.1    dyoung 	ifp->if_qflush = ixgbe_qflush;
   2507  1.26   msaitoh #else
   2508  1.26   msaitoh 	ifp->if_start = ixgbe_start;
   2509  1.26   msaitoh 	IFQ_SET_MAXLEN(&ifp->if_snd, adapter->num_tx_desc - 2);
   2510  1.33   msaitoh #if 0
   2511  1.33   msaitoh 	ifp->if_snd.ifq_drv_maxlen = adapter->num_tx_desc - 2;
   2512  1.33   msaitoh #endif
   2513  1.33   msaitoh 	IFQ_SET_READY(&ifp->if_snd);
   2514   1.1    dyoung #endif
   2515   1.1    dyoung 
   2516  1.37     ozaki 	if_initialize(ifp);
   2517   1.1    dyoung 	ether_ifattach(ifp, adapter->hw.mac.addr);
   2518  1.37     ozaki 	if_register(ifp);
   2519   1.1    dyoung 	ether_set_ifflags_cb(ec, ixgbe_ifflags_cb);
   2520   1.1    dyoung 
   2521   1.1    dyoung 	adapter->max_frame_size =
   2522   1.1    dyoung 	    ifp->if_mtu + ETHER_HDR_LEN + ETHER_CRC_LEN;
   2523   1.1    dyoung 
   2524   1.1    dyoung 	/*
   2525   1.1    dyoung 	 * Tell the upper layer(s) we support long frames.
   2526   1.1    dyoung 	 */
   2527   1.1    dyoung 	ifp->if_hdrlen = sizeof(struct ether_vlan_header);
   2528   1.1    dyoung 
   2529  1.25   msaitoh 	ifp->if_capabilities |= IFCAP_HWCSUM | IFCAP_TSOv4 | IFCAP_TSOv6;
   2530   1.1    dyoung 	ifp->if_capenable = 0;
   2531   1.1    dyoung 
   2532   1.1    dyoung 	ec->ec_capabilities |= ETHERCAP_VLAN_HWCSUM;
   2533   1.1    dyoung 	ec->ec_capabilities |= ETHERCAP_JUMBO_MTU;
   2534  1.26   msaitoh 	ifp->if_capabilities |= IFCAP_LRO;
   2535  1.22   msaitoh 	ec->ec_capabilities |= ETHERCAP_VLAN_HWTAGGING
   2536  1.22   msaitoh 	    		    | ETHERCAP_VLAN_MTU;
   2537   1.1    dyoung 	ec->ec_capenable = ec->ec_capabilities;
   2538   1.1    dyoung 
   2539   1.1    dyoung 	/*
   2540  1.22   msaitoh 	** Don't turn this on by default, if vlans are
   2541   1.1    dyoung 	** created on another pseudo device (eg. lagg)
   2542   1.1    dyoung 	** then vlan events are not passed thru, breaking
   2543   1.1    dyoung 	** operation, but with HW FILTER off it works. If
   2544  1.22   msaitoh 	** using vlans directly on the ixgbe driver you can
   2545   1.1    dyoung 	** enable this and get full hardware tag filtering.
   2546   1.1    dyoung 	*/
   2547   1.1    dyoung 	ec->ec_capabilities |= ETHERCAP_VLAN_HWFILTER;
   2548   1.1    dyoung 
   2549   1.1    dyoung 	/*
   2550   1.1    dyoung 	 * Specify the media types supported by this adapter and register
   2551   1.1    dyoung 	 * callbacks to update media and link information
   2552   1.1    dyoung 	 */
   2553   1.1    dyoung 	ifmedia_init(&adapter->media, IFM_IMASK, ixgbe_media_change,
   2554   1.1    dyoung 		     ixgbe_media_status);
   2555   1.1    dyoung 	ifmedia_add(&adapter->media, IFM_ETHER | adapter->optics, 0, NULL);
   2556   1.1    dyoung 	ifmedia_set(&adapter->media, IFM_ETHER | adapter->optics);
   2557   1.1    dyoung 	if (hw->device_id == IXGBE_DEV_ID_82598AT) {
   2558   1.1    dyoung 		ifmedia_add(&adapter->media,
   2559   1.1    dyoung 		    IFM_ETHER | IFM_1000_T | IFM_FDX, 0, NULL);
   2560   1.1    dyoung 		ifmedia_add(&adapter->media,
   2561   1.1    dyoung 		    IFM_ETHER | IFM_1000_T, 0, NULL);
   2562   1.1    dyoung 	}
   2563   1.1    dyoung 	ifmedia_add(&adapter->media, IFM_ETHER | IFM_AUTO, 0, NULL);
   2564   1.1    dyoung 	ifmedia_set(&adapter->media, IFM_ETHER | IFM_AUTO);
   2565   1.1    dyoung 
   2566   1.1    dyoung 	return (0);
   2567   1.1    dyoung }
   2568   1.1    dyoung 
   2569   1.1    dyoung static void
   2570   1.1    dyoung ixgbe_config_link(struct adapter *adapter)
   2571   1.1    dyoung {
   2572   1.1    dyoung 	struct ixgbe_hw *hw = &adapter->hw;
   2573   1.1    dyoung 	u32	autoneg, err = 0;
   2574   1.1    dyoung 	bool	sfp, negotiate;
   2575   1.1    dyoung 
   2576   1.1    dyoung 	sfp = ixgbe_is_sfp(hw);
   2577   1.1    dyoung 
   2578   1.1    dyoung 	if (sfp) {
   2579  1.19  christos 		void *ip;
   2580  1.19  christos 
   2581   1.1    dyoung 		if (hw->phy.multispeed_fiber) {
   2582   1.1    dyoung 			hw->mac.ops.setup_sfp(hw);
   2583   1.1    dyoung 			ixgbe_enable_tx_laser(hw);
   2584  1.19  christos 			ip = adapter->msf_si;
   2585   1.1    dyoung 		} else {
   2586  1.19  christos 			ip = adapter->mod_si;
   2587   1.1    dyoung 		}
   2588  1.19  christos 
   2589  1.19  christos 		kpreempt_disable();
   2590  1.19  christos 		softint_schedule(ip);
   2591  1.19  christos 		kpreempt_enable();
   2592   1.1    dyoung 	} else {
   2593   1.1    dyoung 		if (hw->mac.ops.check_link)
   2594  1.28   msaitoh 			err = ixgbe_check_link(hw, &adapter->link_speed,
   2595   1.1    dyoung 			    &adapter->link_up, FALSE);
   2596   1.1    dyoung 		if (err)
   2597   1.1    dyoung 			goto out;
   2598   1.1    dyoung 		autoneg = hw->phy.autoneg_advertised;
   2599   1.1    dyoung 		if ((!autoneg) && (hw->mac.ops.get_link_capabilities))
   2600   1.1    dyoung                 	err  = hw->mac.ops.get_link_capabilities(hw,
   2601   1.1    dyoung 			    &autoneg, &negotiate);
   2602  1.13  christos 		else
   2603  1.13  christos 			negotiate = 0;
   2604   1.1    dyoung 		if (err)
   2605   1.1    dyoung 			goto out;
   2606   1.1    dyoung 		if (hw->mac.ops.setup_link)
   2607  1.28   msaitoh                 	err = hw->mac.ops.setup_link(hw,
   2608  1.28   msaitoh 			    autoneg, adapter->link_up);
   2609   1.1    dyoung 	}
   2610   1.1    dyoung out:
   2611   1.1    dyoung 	return;
   2612   1.1    dyoung }
   2613   1.1    dyoung 
   2614   1.1    dyoung /*********************************************************************
   2615   1.1    dyoung  *
   2616  1.42   msaitoh  *  Enable transmit unit.
   2617   1.1    dyoung  *
   2618   1.1    dyoung  **********************************************************************/
   2619  1.42   msaitoh static void
   2620  1.42   msaitoh ixgbe_initialize_transmit_units(struct adapter *adapter)
   2621   1.1    dyoung {
   2622  1.42   msaitoh 	struct tx_ring	*txr = adapter->tx_rings;
   2623  1.42   msaitoh 	struct ixgbe_hw	*hw = &adapter->hw;
   2624   1.1    dyoung 
   2625  1.42   msaitoh 	/* Setup the Base and Length of the Tx Descriptor Ring */
   2626   1.1    dyoung 
   2627  1.42   msaitoh 	for (int i = 0; i < adapter->num_queues; i++, txr++) {
   2628  1.42   msaitoh 		u64	tdba = txr->txdma.dma_paddr;
   2629  1.42   msaitoh 		u32	txctrl;
   2630   1.1    dyoung 
   2631  1.42   msaitoh 		IXGBE_WRITE_REG(hw, IXGBE_TDBAL(i),
   2632  1.42   msaitoh 		       (tdba & 0x00000000ffffffffULL));
   2633  1.42   msaitoh 		IXGBE_WRITE_REG(hw, IXGBE_TDBAH(i), (tdba >> 32));
   2634  1.42   msaitoh 		IXGBE_WRITE_REG(hw, IXGBE_TDLEN(i),
   2635  1.42   msaitoh 		    adapter->num_tx_desc * sizeof(union ixgbe_adv_tx_desc));
   2636   1.1    dyoung 
   2637  1.42   msaitoh 		/* Setup the HW Tx Head and Tail descriptor pointers */
   2638  1.42   msaitoh 		IXGBE_WRITE_REG(hw, IXGBE_TDH(i), 0);
   2639  1.42   msaitoh 		IXGBE_WRITE_REG(hw, IXGBE_TDT(i), 0);
   2640   1.1    dyoung 
   2641  1.42   msaitoh 		/* Setup Transmit Descriptor Cmd Settings */
   2642  1.42   msaitoh 		txr->txd_cmd = IXGBE_TXD_CMD_IFCS;
   2643  1.42   msaitoh 		txr->queue_status = IXGBE_QUEUE_IDLE;
   2644   1.1    dyoung 
   2645  1.42   msaitoh 		/* Set the processing limit */
   2646  1.42   msaitoh 		txr->process_limit = ixgbe_tx_process_limit;
   2647   1.1    dyoung 
   2648  1.42   msaitoh 		/* Disable Head Writeback */
   2649  1.42   msaitoh 		switch (hw->mac.type) {
   2650  1.42   msaitoh 		case ixgbe_mac_82598EB:
   2651  1.42   msaitoh 			txctrl = IXGBE_READ_REG(hw, IXGBE_DCA_TXCTRL(i));
   2652  1.42   msaitoh 			break;
   2653  1.42   msaitoh 		case ixgbe_mac_82599EB:
   2654  1.42   msaitoh 		case ixgbe_mac_X540:
   2655  1.42   msaitoh 		default:
   2656  1.42   msaitoh 			txctrl = IXGBE_READ_REG(hw, IXGBE_DCA_TXCTRL_82599(i));
   2657  1.42   msaitoh 			break;
   2658  1.42   msaitoh                 }
   2659  1.42   msaitoh 		txctrl &= ~IXGBE_DCA_TXCTRL_DESC_WRO_EN;
   2660  1.42   msaitoh 		switch (hw->mac.type) {
   2661  1.42   msaitoh 		case ixgbe_mac_82598EB:
   2662  1.42   msaitoh 			IXGBE_WRITE_REG(hw, IXGBE_DCA_TXCTRL(i), txctrl);
   2663  1.42   msaitoh 			break;
   2664  1.42   msaitoh 		case ixgbe_mac_82599EB:
   2665  1.42   msaitoh 		case ixgbe_mac_X540:
   2666  1.42   msaitoh 		default:
   2667  1.42   msaitoh 			IXGBE_WRITE_REG(hw, IXGBE_DCA_TXCTRL_82599(i), txctrl);
   2668  1.42   msaitoh 			break;
   2669   1.1    dyoung 		}
   2670   1.1    dyoung 
   2671   1.1    dyoung 	}
   2672   1.1    dyoung 
   2673  1.42   msaitoh 	if (hw->mac.type != ixgbe_mac_82598EB) {
   2674   1.1    dyoung 		u32 dmatxctl, rttdcs;
   2675   1.1    dyoung 		dmatxctl = IXGBE_READ_REG(hw, IXGBE_DMATXCTL);
   2676   1.1    dyoung 		dmatxctl |= IXGBE_DMATXCTL_TE;
   2677   1.1    dyoung 		IXGBE_WRITE_REG(hw, IXGBE_DMATXCTL, dmatxctl);
   2678   1.1    dyoung 		/* Disable arbiter to set MTQC */
   2679   1.1    dyoung 		rttdcs = IXGBE_READ_REG(hw, IXGBE_RTTDCS);
   2680   1.1    dyoung 		rttdcs |= IXGBE_RTTDCS_ARBDIS;
   2681   1.1    dyoung 		IXGBE_WRITE_REG(hw, IXGBE_RTTDCS, rttdcs);
   2682   1.1    dyoung 		IXGBE_WRITE_REG(hw, IXGBE_MTQC, IXGBE_MTQC_64Q_1PB);
   2683   1.1    dyoung 		rttdcs &= ~IXGBE_RTTDCS_ARBDIS;
   2684   1.1    dyoung 		IXGBE_WRITE_REG(hw, IXGBE_RTTDCS, rttdcs);
   2685   1.1    dyoung 	}
   2686   1.1    dyoung 
   2687  1.42   msaitoh 	return;
   2688   1.1    dyoung }
   2689   1.1    dyoung 
   2690  1.33   msaitoh static void
   2691  1.33   msaitoh ixgbe_initialise_rss_mapping(struct adapter *adapter)
   2692  1.33   msaitoh {
   2693  1.33   msaitoh 	struct ixgbe_hw	*hw = &adapter->hw;
   2694  1.33   msaitoh 	uint32_t reta;
   2695  1.33   msaitoh 	int i, j, queue_id;
   2696  1.33   msaitoh 	uint32_t rss_key[10];
   2697  1.33   msaitoh 	uint32_t mrqc;
   2698  1.33   msaitoh #ifdef	RSS
   2699  1.33   msaitoh 	uint32_t rss_hash_config;
   2700  1.33   msaitoh #endif
   2701  1.33   msaitoh 
   2702  1.33   msaitoh 	/* Setup RSS */
   2703  1.33   msaitoh 	reta = 0;
   2704  1.33   msaitoh 
   2705  1.33   msaitoh #ifdef	RSS
   2706  1.33   msaitoh 	/* Fetch the configured RSS key */
   2707  1.33   msaitoh 	rss_getkey((uint8_t *) &rss_key);
   2708  1.33   msaitoh #else
   2709  1.33   msaitoh 	/* set up random bits */
   2710  1.33   msaitoh 	cprng_fast(&rss_key, sizeof(rss_key));
   2711  1.33   msaitoh #endif
   2712  1.33   msaitoh 
   2713  1.33   msaitoh 	/* Set up the redirection table */
   2714  1.33   msaitoh 	for (i = 0, j = 0; i < 128; i++, j++) {
   2715  1.33   msaitoh 		if (j == adapter->num_queues) j = 0;
   2716  1.33   msaitoh #ifdef	RSS
   2717  1.33   msaitoh 		/*
   2718  1.33   msaitoh 		 * Fetch the RSS bucket id for the given indirection entry.
   2719  1.33   msaitoh 		 * Cap it at the number of configured buckets (which is
   2720  1.33   msaitoh 		 * num_queues.)
   2721  1.33   msaitoh 		 */
   2722  1.33   msaitoh 		queue_id = rss_get_indirection_to_bucket(i);
   2723  1.33   msaitoh 		queue_id = queue_id % adapter->num_queues;
   2724  1.33   msaitoh #else
   2725  1.33   msaitoh 		queue_id = (j * 0x11);
   2726  1.33   msaitoh #endif
   2727  1.33   msaitoh 		/*
   2728  1.33   msaitoh 		 * The low 8 bits are for hash value (n+0);
   2729  1.33   msaitoh 		 * The next 8 bits are for hash value (n+1), etc.
   2730  1.33   msaitoh 		 */
   2731  1.33   msaitoh 		reta = reta >> 8;
   2732  1.33   msaitoh 		reta = reta | ( ((uint32_t) queue_id) << 24);
   2733  1.33   msaitoh 		if ((i & 3) == 3) {
   2734  1.33   msaitoh 			IXGBE_WRITE_REG(hw, IXGBE_RETA(i >> 2), reta);
   2735  1.33   msaitoh 			reta = 0;
   2736  1.33   msaitoh 		}
   2737  1.33   msaitoh 	}
   2738  1.33   msaitoh 
   2739  1.33   msaitoh 	/* Now fill our hash function seeds */
   2740  1.33   msaitoh 	for (i = 0; i < 10; i++)
   2741  1.33   msaitoh 		IXGBE_WRITE_REG(hw, IXGBE_RSSRK(i), rss_key[i]);
   2742  1.33   msaitoh 
   2743  1.33   msaitoh 	/* Perform hash on these packet types */
   2744  1.33   msaitoh #ifdef	RSS
   2745  1.33   msaitoh 	mrqc = IXGBE_MRQC_RSSEN;
   2746  1.33   msaitoh 	rss_hash_config = rss_gethashconfig();
   2747  1.33   msaitoh 	if (rss_hash_config & RSS_HASHTYPE_RSS_IPV4)
   2748  1.33   msaitoh 		mrqc |= IXGBE_MRQC_RSS_FIELD_IPV4;
   2749  1.33   msaitoh 	if (rss_hash_config & RSS_HASHTYPE_RSS_TCP_IPV4)
   2750  1.33   msaitoh 		mrqc |= IXGBE_MRQC_RSS_FIELD_IPV4_TCP;
   2751  1.33   msaitoh 	if (rss_hash_config & RSS_HASHTYPE_RSS_IPV6)
   2752  1.33   msaitoh 		mrqc |= IXGBE_MRQC_RSS_FIELD_IPV6;
   2753  1.33   msaitoh 	if (rss_hash_config & RSS_HASHTYPE_RSS_TCP_IPV6)
   2754  1.33   msaitoh 		mrqc |= IXGBE_MRQC_RSS_FIELD_IPV6_TCP;
   2755  1.33   msaitoh 	if (rss_hash_config & RSS_HASHTYPE_RSS_IPV6_EX)
   2756  1.33   msaitoh 		mrqc |= IXGBE_MRQC_RSS_FIELD_IPV6_EX;
   2757  1.33   msaitoh 	if (rss_hash_config & RSS_HASHTYPE_RSS_TCP_IPV6_EX)
   2758  1.33   msaitoh 		mrqc |= IXGBE_MRQC_RSS_FIELD_IPV6_EX_TCP;
   2759  1.33   msaitoh 	if (rss_hash_config & RSS_HASHTYPE_RSS_UDP_IPV4)
   2760  1.33   msaitoh 		mrqc |= IXGBE_MRQC_RSS_FIELD_IPV4_UDP;
   2761  1.33   msaitoh 	if (rss_hash_config & RSS_HASHTYPE_RSS_UDP_IPV4_EX)
   2762  1.33   msaitoh 		device_printf(adapter->dev,
   2763  1.33   msaitoh 		    "%s: RSS_HASHTYPE_RSS_UDP_IPV4_EX defined, "
   2764  1.33   msaitoh 		    "but not supported\n", __func__);
   2765  1.33   msaitoh 	if (rss_hash_config & RSS_HASHTYPE_RSS_UDP_IPV6)
   2766  1.33   msaitoh 		mrqc |= IXGBE_MRQC_RSS_FIELD_IPV6_UDP;
   2767  1.33   msaitoh 	if (rss_hash_config & RSS_HASHTYPE_RSS_UDP_IPV6_EX)
   2768  1.33   msaitoh 		mrqc |= IXGBE_MRQC_RSS_FIELD_IPV6_EX_UDP;
   2769  1.33   msaitoh #else
   2770  1.33   msaitoh 	/*
   2771  1.33   msaitoh 	 * Disable UDP - IP fragments aren't currently being handled
   2772  1.33   msaitoh 	 * and so we end up with a mix of 2-tuple and 4-tuple
   2773  1.33   msaitoh 	 * traffic.
   2774  1.33   msaitoh 	 */
   2775  1.33   msaitoh 	mrqc = IXGBE_MRQC_RSSEN
   2776  1.33   msaitoh 	     | IXGBE_MRQC_RSS_FIELD_IPV4
   2777  1.33   msaitoh 	     | IXGBE_MRQC_RSS_FIELD_IPV4_TCP
   2778  1.33   msaitoh #if 0
   2779  1.33   msaitoh 	     | IXGBE_MRQC_RSS_FIELD_IPV4_UDP
   2780  1.33   msaitoh #endif
   2781  1.33   msaitoh 	     | IXGBE_MRQC_RSS_FIELD_IPV6_EX_TCP
   2782  1.33   msaitoh 	     | IXGBE_MRQC_RSS_FIELD_IPV6_EX
   2783  1.33   msaitoh 	     | IXGBE_MRQC_RSS_FIELD_IPV6
   2784  1.33   msaitoh 	     | IXGBE_MRQC_RSS_FIELD_IPV6_TCP
   2785  1.33   msaitoh #if 0
   2786  1.33   msaitoh 	     | IXGBE_MRQC_RSS_FIELD_IPV6_UDP
   2787  1.33   msaitoh 	     | IXGBE_MRQC_RSS_FIELD_IPV6_EX_UDP
   2788  1.33   msaitoh #endif
   2789  1.33   msaitoh 	;
   2790  1.33   msaitoh #endif /* RSS */
   2791  1.33   msaitoh 	IXGBE_WRITE_REG(hw, IXGBE_MRQC, mrqc);
   2792  1.33   msaitoh }
   2793  1.33   msaitoh 
   2794  1.33   msaitoh 
   2795   1.1    dyoung /*********************************************************************
   2796   1.1    dyoung  *
   2797   1.1    dyoung  *  Setup receive registers and features.
   2798   1.1    dyoung  *
   2799   1.1    dyoung  **********************************************************************/
   2800   1.1    dyoung #define IXGBE_SRRCTL_BSIZEHDRSIZE_SHIFT 2
   2801   1.1    dyoung 
   2802  1.22   msaitoh #define BSIZEPKT_ROUNDUP ((1<<IXGBE_SRRCTL_BSIZEPKT_SHIFT)-1)
   2803  1.22   msaitoh 
   2804   1.1    dyoung static void
   2805   1.1    dyoung ixgbe_initialize_receive_units(struct adapter *adapter)
   2806   1.1    dyoung {
   2807   1.1    dyoung 	int i;
   2808   1.1    dyoung 	struct	rx_ring	*rxr = adapter->rx_rings;
   2809   1.1    dyoung 	struct ixgbe_hw	*hw = &adapter->hw;
   2810   1.1    dyoung 	struct ifnet   *ifp = adapter->ifp;
   2811   1.1    dyoung 	u32		bufsz, rxctrl, fctrl, srrctl, rxcsum;
   2812  1.33   msaitoh 	u32		hlreg;
   2813   1.1    dyoung 
   2814   1.1    dyoung 
   2815   1.1    dyoung 	/*
   2816   1.1    dyoung 	 * Make sure receives are disabled while
   2817   1.1    dyoung 	 * setting up the descriptor ring
   2818   1.1    dyoung 	 */
   2819   1.1    dyoung 	rxctrl = IXGBE_READ_REG(hw, IXGBE_RXCTRL);
   2820   1.1    dyoung 	IXGBE_WRITE_REG(hw, IXGBE_RXCTRL,
   2821   1.1    dyoung 	    rxctrl & ~IXGBE_RXCTRL_RXEN);
   2822   1.1    dyoung 
   2823   1.1    dyoung 	/* Enable broadcasts */
   2824   1.1    dyoung 	fctrl = IXGBE_READ_REG(hw, IXGBE_FCTRL);
   2825   1.1    dyoung 	fctrl |= IXGBE_FCTRL_BAM;
   2826   1.1    dyoung 	fctrl |= IXGBE_FCTRL_DPF;
   2827   1.1    dyoung 	fctrl |= IXGBE_FCTRL_PMCF;
   2828   1.1    dyoung 	IXGBE_WRITE_REG(hw, IXGBE_FCTRL, fctrl);
   2829   1.1    dyoung 
   2830   1.1    dyoung 	/* Set for Jumbo Frames? */
   2831   1.1    dyoung 	hlreg = IXGBE_READ_REG(hw, IXGBE_HLREG0);
   2832   1.1    dyoung 	if (ifp->if_mtu > ETHERMTU)
   2833   1.1    dyoung 		hlreg |= IXGBE_HLREG0_JUMBOEN;
   2834   1.1    dyoung 	else
   2835   1.1    dyoung 		hlreg &= ~IXGBE_HLREG0_JUMBOEN;
   2836  1.25   msaitoh #ifdef DEV_NETMAP
   2837  1.25   msaitoh 	/* crcstrip is conditional in netmap (in RDRXCTL too ?) */
   2838  1.25   msaitoh 	if (ifp->if_capenable & IFCAP_NETMAP && !ix_crcstrip)
   2839  1.25   msaitoh 		hlreg &= ~IXGBE_HLREG0_RXCRCSTRP;
   2840  1.25   msaitoh 	else
   2841  1.25   msaitoh 		hlreg |= IXGBE_HLREG0_RXCRCSTRP;
   2842  1.25   msaitoh #endif /* DEV_NETMAP */
   2843   1.1    dyoung 	IXGBE_WRITE_REG(hw, IXGBE_HLREG0, hlreg);
   2844   1.1    dyoung 
   2845  1.24   msaitoh 	bufsz = (adapter->rx_mbuf_sz +
   2846  1.24   msaitoh 	    BSIZEPKT_ROUNDUP) >> IXGBE_SRRCTL_BSIZEPKT_SHIFT;
   2847   1.1    dyoung 
   2848   1.1    dyoung 	for (i = 0; i < adapter->num_queues; i++, rxr++) {
   2849   1.1    dyoung 		u64 rdba = rxr->rxdma.dma_paddr;
   2850   1.1    dyoung 
   2851   1.1    dyoung 		/* Setup the Base and Length of the Rx Descriptor Ring */
   2852   1.1    dyoung 		IXGBE_WRITE_REG(hw, IXGBE_RDBAL(i),
   2853   1.1    dyoung 			       (rdba & 0x00000000ffffffffULL));
   2854   1.1    dyoung 		IXGBE_WRITE_REG(hw, IXGBE_RDBAH(i), (rdba >> 32));
   2855   1.1    dyoung 		IXGBE_WRITE_REG(hw, IXGBE_RDLEN(i),
   2856   1.1    dyoung 		    adapter->num_rx_desc * sizeof(union ixgbe_adv_rx_desc));
   2857   1.1    dyoung 
   2858   1.1    dyoung 		/* Set up the SRRCTL register */
   2859   1.1    dyoung 		srrctl = IXGBE_READ_REG(hw, IXGBE_SRRCTL(i));
   2860   1.1    dyoung 		srrctl &= ~IXGBE_SRRCTL_BSIZEHDR_MASK;
   2861   1.1    dyoung 		srrctl &= ~IXGBE_SRRCTL_BSIZEPKT_MASK;
   2862   1.1    dyoung 		srrctl |= bufsz;
   2863  1.26   msaitoh 		srrctl |= IXGBE_SRRCTL_DESCTYPE_ADV_ONEBUF;
   2864  1.33   msaitoh 
   2865  1.33   msaitoh 		/*
   2866  1.33   msaitoh 		 * Set DROP_EN iff we have no flow control and >1 queue.
   2867  1.33   msaitoh 		 * Note that srrctl was cleared shortly before during reset,
   2868  1.33   msaitoh 		 * so we do not need to clear the bit, but do it just in case
   2869  1.33   msaitoh 		 * this code is moved elsewhere.
   2870  1.33   msaitoh 		 */
   2871  1.33   msaitoh 		if (adapter->num_queues > 1 &&
   2872  1.33   msaitoh 		    adapter->fc == ixgbe_fc_none) {
   2873  1.33   msaitoh 			srrctl |= IXGBE_SRRCTL_DROP_EN;
   2874  1.33   msaitoh 		} else {
   2875  1.33   msaitoh 			srrctl &= ~IXGBE_SRRCTL_DROP_EN;
   2876  1.33   msaitoh 		}
   2877  1.33   msaitoh 
   2878   1.1    dyoung 		IXGBE_WRITE_REG(hw, IXGBE_SRRCTL(i), srrctl);
   2879   1.1    dyoung 
   2880   1.1    dyoung 		/* Setup the HW Rx Head and Tail Descriptor Pointers */
   2881   1.1    dyoung 		IXGBE_WRITE_REG(hw, IXGBE_RDH(i), 0);
   2882   1.1    dyoung 		IXGBE_WRITE_REG(hw, IXGBE_RDT(i), 0);
   2883  1.28   msaitoh 
   2884  1.28   msaitoh 		/* Set the processing limit */
   2885  1.28   msaitoh 		rxr->process_limit = ixgbe_rx_process_limit;
   2886   1.1    dyoung 	}
   2887   1.1    dyoung 
   2888   1.1    dyoung 	if (adapter->hw.mac.type != ixgbe_mac_82598EB) {
   2889   1.1    dyoung 		u32 psrtype = IXGBE_PSRTYPE_TCPHDR |
   2890   1.1    dyoung 			      IXGBE_PSRTYPE_UDPHDR |
   2891   1.1    dyoung 			      IXGBE_PSRTYPE_IPV4HDR |
   2892   1.1    dyoung 			      IXGBE_PSRTYPE_IPV6HDR;
   2893   1.1    dyoung 		IXGBE_WRITE_REG(hw, IXGBE_PSRTYPE(0), psrtype);
   2894   1.1    dyoung 	}
   2895   1.1    dyoung 
   2896   1.1    dyoung 	rxcsum = IXGBE_READ_REG(hw, IXGBE_RXCSUM);
   2897   1.1    dyoung 
   2898  1.33   msaitoh 	ixgbe_initialise_rss_mapping(adapter);
   2899  1.33   msaitoh 
   2900   1.1    dyoung 	if (adapter->num_queues > 1) {
   2901   1.1    dyoung 		/* RSS and RX IPP Checksum are mutually exclusive */
   2902   1.1    dyoung 		rxcsum |= IXGBE_RXCSUM_PCSD;
   2903   1.1    dyoung 	}
   2904   1.1    dyoung 
   2905   1.1    dyoung 	if (ifp->if_capenable & IFCAP_RXCSUM)
   2906   1.1    dyoung 		rxcsum |= IXGBE_RXCSUM_PCSD;
   2907   1.1    dyoung 
   2908   1.1    dyoung 	if (!(rxcsum & IXGBE_RXCSUM_PCSD))
   2909   1.1    dyoung 		rxcsum |= IXGBE_RXCSUM_IPPCSE;
   2910   1.1    dyoung 
   2911   1.1    dyoung 	IXGBE_WRITE_REG(hw, IXGBE_RXCSUM, rxcsum);
   2912   1.1    dyoung 
   2913   1.1    dyoung 	return;
   2914   1.1    dyoung }
   2915   1.1    dyoung 
   2916   1.1    dyoung #if 0	/* XXX Badly need to overhaul vlan(4) on NetBSD. */
   2917   1.1    dyoung /*
   2918   1.1    dyoung ** This routine is run via an vlan config EVENT,
   2919   1.1    dyoung ** it enables us to use the HW Filter table since
   2920   1.1    dyoung ** we can get the vlan id. This just creates the
   2921   1.1    dyoung ** entry in the soft version of the VFTA, init will
   2922   1.1    dyoung ** repopulate the real table.
   2923   1.1    dyoung */
   2924   1.1    dyoung static void
   2925   1.1    dyoung ixgbe_register_vlan(void *arg, struct ifnet *ifp, u16 vtag)
   2926   1.1    dyoung {
   2927   1.1    dyoung 	struct adapter	*adapter = ifp->if_softc;
   2928   1.1    dyoung 	u16		index, bit;
   2929   1.1    dyoung 
   2930   1.1    dyoung 	if (ifp->if_softc !=  arg)   /* Not our event */
   2931   1.1    dyoung 		return;
   2932   1.1    dyoung 
   2933   1.1    dyoung 	if ((vtag == 0) || (vtag > 4095))	/* Invalid */
   2934   1.1    dyoung 		return;
   2935   1.1    dyoung 
   2936   1.1    dyoung 	IXGBE_CORE_LOCK(adapter);
   2937   1.1    dyoung 	index = (vtag >> 5) & 0x7F;
   2938   1.1    dyoung 	bit = vtag & 0x1F;
   2939   1.1    dyoung 	adapter->shadow_vfta[index] |= (1 << bit);
   2940  1.33   msaitoh 	ixgbe_setup_vlan_hw_support(adapter);
   2941   1.1    dyoung 	IXGBE_CORE_UNLOCK(adapter);
   2942   1.1    dyoung }
   2943   1.1    dyoung 
   2944   1.1    dyoung /*
   2945   1.1    dyoung ** This routine is run via an vlan
   2946   1.1    dyoung ** unconfig EVENT, remove our entry
   2947   1.1    dyoung ** in the soft vfta.
   2948   1.1    dyoung */
   2949   1.1    dyoung static void
   2950   1.1    dyoung ixgbe_unregister_vlan(void *arg, struct ifnet *ifp, u16 vtag)
   2951   1.1    dyoung {
   2952   1.1    dyoung 	struct adapter	*adapter = ifp->if_softc;
   2953   1.1    dyoung 	u16		index, bit;
   2954   1.1    dyoung 
   2955   1.1    dyoung 	if (ifp->if_softc !=  arg)
   2956   1.1    dyoung 		return;
   2957   1.1    dyoung 
   2958   1.1    dyoung 	if ((vtag == 0) || (vtag > 4095))	/* Invalid */
   2959   1.1    dyoung 		return;
   2960   1.1    dyoung 
   2961   1.1    dyoung 	IXGBE_CORE_LOCK(adapter);
   2962   1.1    dyoung 	index = (vtag >> 5) & 0x7F;
   2963   1.1    dyoung 	bit = vtag & 0x1F;
   2964   1.1    dyoung 	adapter->shadow_vfta[index] &= ~(1 << bit);
   2965   1.1    dyoung 	/* Re-init to load the changes */
   2966  1.33   msaitoh 	ixgbe_setup_vlan_hw_support(adapter);
   2967   1.1    dyoung 	IXGBE_CORE_UNLOCK(adapter);
   2968   1.1    dyoung }
   2969   1.1    dyoung #endif
   2970   1.1    dyoung 
   2971   1.1    dyoung static void
   2972   1.1    dyoung ixgbe_setup_vlan_hw_support(struct adapter *adapter)
   2973   1.1    dyoung {
   2974   1.1    dyoung 	struct ethercom *ec = &adapter->osdep.ec;
   2975   1.1    dyoung 	struct ixgbe_hw *hw = &adapter->hw;
   2976  1.24   msaitoh 	struct rx_ring	*rxr;
   2977   1.1    dyoung 	u32		ctrl;
   2978   1.1    dyoung 
   2979  1.35   msaitoh 
   2980   1.1    dyoung 	/*
   2981   1.1    dyoung 	** We get here thru init_locked, meaning
   2982   1.1    dyoung 	** a soft reset, this has already cleared
   2983   1.1    dyoung 	** the VFTA and other state, so if there
   2984   1.1    dyoung 	** have been no vlan's registered do nothing.
   2985   1.1    dyoung 	*/
   2986   1.1    dyoung 	if (!VLAN_ATTACHED(&adapter->osdep.ec)) {
   2987   1.1    dyoung 		return;
   2988   1.1    dyoung 	}
   2989   1.1    dyoung 
   2990  1.33   msaitoh 	/* Setup the queues for vlans */
   2991  1.33   msaitoh 	for (int i = 0; i < adapter->num_queues; i++) {
   2992  1.33   msaitoh 		rxr = &adapter->rx_rings[i];
   2993  1.33   msaitoh 		/* On 82599 the VLAN enable is per/queue in RXDCTL */
   2994  1.33   msaitoh 		if (hw->mac.type != ixgbe_mac_82598EB) {
   2995  1.33   msaitoh 			ctrl = IXGBE_READ_REG(hw, IXGBE_RXDCTL(i));
   2996  1.33   msaitoh 			ctrl |= IXGBE_RXDCTL_VME;
   2997  1.33   msaitoh 			IXGBE_WRITE_REG(hw, IXGBE_RXDCTL(i), ctrl);
   2998  1.33   msaitoh 		}
   2999  1.33   msaitoh 		rxr->vtag_strip = TRUE;
   3000  1.33   msaitoh 	}
   3001  1.33   msaitoh 
   3002  1.33   msaitoh 	if ((ec->ec_capenable & ETHERCAP_VLAN_HWFILTER) == 0)
   3003  1.33   msaitoh 		return;
   3004   1.1    dyoung 	/*
   3005   1.1    dyoung 	** A soft reset zero's out the VFTA, so
   3006   1.1    dyoung 	** we need to repopulate it now.
   3007   1.1    dyoung 	*/
   3008   1.1    dyoung 	for (int i = 0; i < IXGBE_VFTA_SIZE; i++)
   3009   1.1    dyoung 		if (adapter->shadow_vfta[i] != 0)
   3010   1.1    dyoung 			IXGBE_WRITE_REG(hw, IXGBE_VFTA(i),
   3011   1.1    dyoung 			    adapter->shadow_vfta[i]);
   3012   1.1    dyoung 
   3013   1.1    dyoung 	ctrl = IXGBE_READ_REG(hw, IXGBE_VLNCTRL);
   3014   1.1    dyoung 	/* Enable the Filter Table if enabled */
   3015   1.1    dyoung 	if (ec->ec_capenable & ETHERCAP_VLAN_HWFILTER) {
   3016   1.1    dyoung 		ctrl &= ~IXGBE_VLNCTRL_CFIEN;
   3017   1.1    dyoung 		ctrl |= IXGBE_VLNCTRL_VFE;
   3018   1.1    dyoung 	}
   3019   1.1    dyoung 	if (hw->mac.type == ixgbe_mac_82598EB)
   3020   1.1    dyoung 		ctrl |= IXGBE_VLNCTRL_VME;
   3021   1.1    dyoung 	IXGBE_WRITE_REG(hw, IXGBE_VLNCTRL, ctrl);
   3022   1.1    dyoung }
   3023   1.1    dyoung 
   3024   1.1    dyoung static void
   3025   1.1    dyoung ixgbe_enable_intr(struct adapter *adapter)
   3026   1.1    dyoung {
   3027  1.28   msaitoh 	struct ixgbe_hw	*hw = &adapter->hw;
   3028  1.28   msaitoh 	struct ix_queue	*que = adapter->queues;
   3029  1.28   msaitoh 	u32		mask, fwsm;
   3030   1.1    dyoung 
   3031  1.28   msaitoh 	mask = (IXGBE_EIMS_ENABLE_MASK & ~IXGBE_EIMS_RTX_QUEUE);
   3032   1.1    dyoung 	/* Enable Fan Failure detection */
   3033   1.1    dyoung 	if (hw->device_id == IXGBE_DEV_ID_82598AT)
   3034   1.1    dyoung 		    mask |= IXGBE_EIMS_GPI_SDP1;
   3035  1.28   msaitoh 
   3036  1.28   msaitoh 	switch (adapter->hw.mac.type) {
   3037  1.28   msaitoh 		case ixgbe_mac_82599EB:
   3038  1.28   msaitoh 			mask |= IXGBE_EIMS_ECC;
   3039  1.28   msaitoh 			mask |= IXGBE_EIMS_GPI_SDP0;
   3040  1.28   msaitoh 			mask |= IXGBE_EIMS_GPI_SDP1;
   3041  1.28   msaitoh 			mask |= IXGBE_EIMS_GPI_SDP2;
   3042  1.28   msaitoh #ifdef IXGBE_FDIR
   3043  1.28   msaitoh 			mask |= IXGBE_EIMS_FLOW_DIR;
   3044  1.28   msaitoh #endif
   3045  1.28   msaitoh 			break;
   3046  1.28   msaitoh 		case ixgbe_mac_X540:
   3047  1.28   msaitoh 			mask |= IXGBE_EIMS_ECC;
   3048  1.28   msaitoh 			/* Detect if Thermal Sensor is enabled */
   3049  1.28   msaitoh 			fwsm = IXGBE_READ_REG(hw, IXGBE_FWSM);
   3050  1.28   msaitoh 			if (fwsm & IXGBE_FWSM_TS_ENABLED)
   3051  1.28   msaitoh 				mask |= IXGBE_EIMS_TS;
   3052   1.1    dyoung #ifdef IXGBE_FDIR
   3053  1.28   msaitoh 			mask |= IXGBE_EIMS_FLOW_DIR;
   3054   1.1    dyoung #endif
   3055  1.28   msaitoh 		/* falls through */
   3056  1.28   msaitoh 		default:
   3057  1.28   msaitoh 			break;
   3058   1.1    dyoung 	}
   3059   1.1    dyoung 
   3060   1.1    dyoung 	IXGBE_WRITE_REG(hw, IXGBE_EIMS, mask);
   3061   1.1    dyoung 
   3062   1.1    dyoung 	/* With RSS we use auto clear */
   3063   1.1    dyoung 	if (adapter->msix_mem) {
   3064   1.1    dyoung 		mask = IXGBE_EIMS_ENABLE_MASK;
   3065   1.1    dyoung 		/* Don't autoclear Link */
   3066   1.1    dyoung 		mask &= ~IXGBE_EIMS_OTHER;
   3067   1.1    dyoung 		mask &= ~IXGBE_EIMS_LSC;
   3068   1.1    dyoung 		IXGBE_WRITE_REG(hw, IXGBE_EIAC, mask);
   3069   1.1    dyoung 	}
   3070   1.1    dyoung 
   3071   1.1    dyoung 	/*
   3072   1.1    dyoung 	** Now enable all queues, this is done separately to
   3073   1.1    dyoung 	** allow for handling the extended (beyond 32) MSIX
   3074   1.1    dyoung 	** vectors that can be used by 82599
   3075   1.1    dyoung 	*/
   3076   1.1    dyoung         for (int i = 0; i < adapter->num_queues; i++, que++)
   3077   1.1    dyoung                 ixgbe_enable_queue(adapter, que->msix);
   3078   1.1    dyoung 
   3079   1.1    dyoung 	IXGBE_WRITE_FLUSH(hw);
   3080   1.1    dyoung 
   3081   1.1    dyoung 	return;
   3082   1.1    dyoung }
   3083   1.1    dyoung 
   3084   1.1    dyoung static void
   3085   1.1    dyoung ixgbe_disable_intr(struct adapter *adapter)
   3086   1.1    dyoung {
   3087   1.1    dyoung 	if (adapter->msix_mem)
   3088   1.1    dyoung 		IXGBE_WRITE_REG(&adapter->hw, IXGBE_EIAC, 0);
   3089   1.1    dyoung 	if (adapter->hw.mac.type == ixgbe_mac_82598EB) {
   3090   1.1    dyoung 		IXGBE_WRITE_REG(&adapter->hw, IXGBE_EIMC, ~0);
   3091   1.1    dyoung 	} else {
   3092   1.1    dyoung 		IXGBE_WRITE_REG(&adapter->hw, IXGBE_EIMC, 0xFFFF0000);
   3093   1.1    dyoung 		IXGBE_WRITE_REG(&adapter->hw, IXGBE_EIMC_EX(0), ~0);
   3094   1.1    dyoung 		IXGBE_WRITE_REG(&adapter->hw, IXGBE_EIMC_EX(1), ~0);
   3095   1.1    dyoung 	}
   3096   1.1    dyoung 	IXGBE_WRITE_FLUSH(&adapter->hw);
   3097   1.1    dyoung 	return;
   3098   1.1    dyoung }
   3099   1.1    dyoung 
   3100   1.1    dyoung u16
   3101   1.1    dyoung ixgbe_read_pci_cfg(struct ixgbe_hw *hw, u32 reg)
   3102   1.1    dyoung {
   3103   1.1    dyoung 	switch (reg % 4) {
   3104   1.1    dyoung 	case 0:
   3105   1.1    dyoung 		return pci_conf_read(hw->back->pc, hw->back->tag, reg) &
   3106   1.1    dyoung 		    __BITS(15, 0);
   3107   1.1    dyoung 	case 2:
   3108   1.1    dyoung 		return __SHIFTOUT(pci_conf_read(hw->back->pc, hw->back->tag,
   3109   1.1    dyoung 		    reg - 2), __BITS(31, 16));
   3110   1.1    dyoung 	default:
   3111   1.1    dyoung 		panic("%s: invalid register (%" PRIx32, __func__, reg);
   3112   1.1    dyoung 		break;
   3113   1.1    dyoung 	}
   3114   1.1    dyoung }
   3115   1.1    dyoung 
   3116   1.1    dyoung void
   3117   1.1    dyoung ixgbe_write_pci_cfg(struct ixgbe_hw *hw, u32 reg, u16 value)
   3118   1.1    dyoung {
   3119   1.1    dyoung 	pcireg_t old;
   3120   1.1    dyoung 
   3121   1.1    dyoung 	switch (reg % 4) {
   3122   1.1    dyoung 	case 0:
   3123   1.1    dyoung 		old = pci_conf_read(hw->back->pc, hw->back->tag, reg) &
   3124   1.1    dyoung 		    __BITS(31, 16);
   3125   1.1    dyoung 		pci_conf_write(hw->back->pc, hw->back->tag, reg, value | old);
   3126   1.1    dyoung 		break;
   3127   1.1    dyoung 	case 2:
   3128   1.1    dyoung 		old = pci_conf_read(hw->back->pc, hw->back->tag, reg - 2) &
   3129   1.1    dyoung 		    __BITS(15, 0);
   3130   1.1    dyoung 		pci_conf_write(hw->back->pc, hw->back->tag, reg - 2,
   3131   1.1    dyoung 		    __SHIFTIN(value, __BITS(31, 16)) | old);
   3132   1.1    dyoung 		break;
   3133   1.1    dyoung 	default:
   3134   1.1    dyoung 		panic("%s: invalid register (%" PRIx32, __func__, reg);
   3135   1.1    dyoung 		break;
   3136   1.1    dyoung 	}
   3137   1.1    dyoung 
   3138   1.1    dyoung 	return;
   3139   1.1    dyoung }
   3140   1.1    dyoung 
   3141   1.1    dyoung /*
   3142  1.33   msaitoh ** Get the width and transaction speed of
   3143  1.33   msaitoh ** the slot this adapter is plugged into.
   3144  1.33   msaitoh */
   3145  1.33   msaitoh static void
   3146  1.33   msaitoh ixgbe_get_slot_info(struct ixgbe_hw *hw)
   3147  1.33   msaitoh {
   3148  1.33   msaitoh 	device_t		dev = ((struct ixgbe_osdep *)hw->back)->dev;
   3149  1.33   msaitoh 	struct ixgbe_mac_info	*mac = &hw->mac;
   3150  1.33   msaitoh 	u16			link;
   3151  1.33   msaitoh 
   3152  1.33   msaitoh 	/* For most devices simply call the shared code routine */
   3153  1.33   msaitoh 	if (hw->device_id != IXGBE_DEV_ID_82599_SFP_SF_QP) {
   3154  1.33   msaitoh 		ixgbe_get_bus_info(hw);
   3155  1.33   msaitoh 		goto display;
   3156  1.33   msaitoh 	}
   3157  1.33   msaitoh 
   3158  1.33   msaitoh 	/*
   3159  1.33   msaitoh 	** For the Quad port adapter we need to parse back
   3160  1.33   msaitoh 	** up the PCI tree to find the speed of the expansion
   3161  1.33   msaitoh 	** slot into which this adapter is plugged. A bit more work.
   3162  1.33   msaitoh 	*/
   3163  1.33   msaitoh 	dev = device_parent(device_parent(dev));
   3164  1.33   msaitoh #ifdef IXGBE_DEBUG
   3165  1.33   msaitoh 	device_printf(dev, "parent pcib = %x,%x,%x\n",
   3166  1.33   msaitoh 	    pci_get_bus(dev), pci_get_slot(dev), pci_get_function(dev));
   3167  1.33   msaitoh #endif
   3168  1.33   msaitoh 	dev = device_parent(device_parent(dev));
   3169  1.33   msaitoh #ifdef IXGBE_DEBUG
   3170  1.33   msaitoh 	device_printf(dev, "slot pcib = %x,%x,%x\n",
   3171  1.33   msaitoh 	    pci_get_bus(dev), pci_get_slot(dev), pci_get_function(dev));
   3172  1.33   msaitoh #endif
   3173  1.33   msaitoh 	/* Now get the PCI Express Capabilities offset */
   3174  1.33   msaitoh 	/* ...and read the Link Status Register */
   3175  1.33   msaitoh 	link = IXGBE_READ_PCIE_WORD(hw, IXGBE_PCI_LINK_STATUS);
   3176  1.33   msaitoh 	switch (link & IXGBE_PCI_LINK_WIDTH) {
   3177  1.33   msaitoh 	case IXGBE_PCI_LINK_WIDTH_1:
   3178  1.33   msaitoh 		hw->bus.width = ixgbe_bus_width_pcie_x1;
   3179  1.33   msaitoh 		break;
   3180  1.33   msaitoh 	case IXGBE_PCI_LINK_WIDTH_2:
   3181  1.33   msaitoh 		hw->bus.width = ixgbe_bus_width_pcie_x2;
   3182  1.33   msaitoh 		break;
   3183  1.33   msaitoh 	case IXGBE_PCI_LINK_WIDTH_4:
   3184  1.33   msaitoh 		hw->bus.width = ixgbe_bus_width_pcie_x4;
   3185  1.33   msaitoh 		break;
   3186  1.33   msaitoh 	case IXGBE_PCI_LINK_WIDTH_8:
   3187  1.33   msaitoh 		hw->bus.width = ixgbe_bus_width_pcie_x8;
   3188  1.33   msaitoh 		break;
   3189  1.33   msaitoh 	default:
   3190  1.33   msaitoh 		hw->bus.width = ixgbe_bus_width_unknown;
   3191  1.33   msaitoh 		break;
   3192  1.33   msaitoh 	}
   3193  1.33   msaitoh 
   3194  1.33   msaitoh 	switch (link & IXGBE_PCI_LINK_SPEED) {
   3195  1.33   msaitoh 	case IXGBE_PCI_LINK_SPEED_2500:
   3196  1.33   msaitoh 		hw->bus.speed = ixgbe_bus_speed_2500;
   3197  1.33   msaitoh 		break;
   3198  1.33   msaitoh 	case IXGBE_PCI_LINK_SPEED_5000:
   3199  1.33   msaitoh 		hw->bus.speed = ixgbe_bus_speed_5000;
   3200  1.33   msaitoh 		break;
   3201  1.33   msaitoh 	case IXGBE_PCI_LINK_SPEED_8000:
   3202  1.33   msaitoh 		hw->bus.speed = ixgbe_bus_speed_8000;
   3203  1.33   msaitoh 		break;
   3204  1.33   msaitoh 	default:
   3205  1.33   msaitoh 		hw->bus.speed = ixgbe_bus_speed_unknown;
   3206  1.33   msaitoh 		break;
   3207  1.33   msaitoh 	}
   3208  1.33   msaitoh 
   3209  1.33   msaitoh 	mac->ops.set_lan_id(hw);
   3210  1.33   msaitoh 
   3211  1.33   msaitoh display:
   3212  1.33   msaitoh 	device_printf(dev,"PCI Express Bus: Speed %s %s\n",
   3213  1.33   msaitoh 	    ((hw->bus.speed == ixgbe_bus_speed_8000) ? "8.0GT/s":
   3214  1.33   msaitoh 	    (hw->bus.speed == ixgbe_bus_speed_5000) ? "5.0GT/s":
   3215  1.33   msaitoh 	    (hw->bus.speed == ixgbe_bus_speed_2500) ? "2.5GT/s":"Unknown"),
   3216  1.33   msaitoh 	    (hw->bus.width == ixgbe_bus_width_pcie_x8) ? "Width x8" :
   3217  1.33   msaitoh 	    (hw->bus.width == ixgbe_bus_width_pcie_x4) ? "Width x4" :
   3218  1.33   msaitoh 	    (hw->bus.width == ixgbe_bus_width_pcie_x1) ? "Width x1" :
   3219  1.33   msaitoh 	    ("Unknown"));
   3220  1.33   msaitoh 
   3221  1.33   msaitoh 	if ((hw->device_id != IXGBE_DEV_ID_82599_SFP_SF_QP) &&
   3222  1.33   msaitoh 	    ((hw->bus.width <= ixgbe_bus_width_pcie_x4) &&
   3223  1.33   msaitoh 	    (hw->bus.speed == ixgbe_bus_speed_2500))) {
   3224  1.33   msaitoh 		device_printf(dev, "PCI-Express bandwidth available"
   3225  1.33   msaitoh 		    " for this card\n     is not sufficient for"
   3226  1.33   msaitoh 		    " optimal performance.\n");
   3227  1.33   msaitoh 		device_printf(dev, "For optimal performance a x8 "
   3228  1.33   msaitoh 		    "PCIE, or x4 PCIE Gen2 slot is required.\n");
   3229  1.33   msaitoh         }
   3230  1.33   msaitoh 	if ((hw->device_id == IXGBE_DEV_ID_82599_SFP_SF_QP) &&
   3231  1.33   msaitoh 	    ((hw->bus.width <= ixgbe_bus_width_pcie_x8) &&
   3232  1.33   msaitoh 	    (hw->bus.speed < ixgbe_bus_speed_8000))) {
   3233  1.33   msaitoh 		device_printf(dev, "PCI-Express bandwidth available"
   3234  1.33   msaitoh 		    " for this card\n     is not sufficient for"
   3235  1.33   msaitoh 		    " optimal performance.\n");
   3236  1.33   msaitoh 		device_printf(dev, "For optimal performance a x8 "
   3237  1.33   msaitoh 		    "PCIE Gen3 slot is required.\n");
   3238  1.33   msaitoh         }
   3239  1.33   msaitoh 
   3240  1.33   msaitoh 	return;
   3241  1.33   msaitoh }
   3242  1.33   msaitoh 
   3243  1.33   msaitoh 
   3244  1.33   msaitoh /*
   3245   1.1    dyoung ** Setup the correct IVAR register for a particular MSIX interrupt
   3246   1.1    dyoung **   (yes this is all very magic and confusing :)
   3247   1.1    dyoung **  - entry is the register array entry
   3248   1.1    dyoung **  - vector is the MSIX vector for this queue
   3249   1.1    dyoung **  - type is RX/TX/MISC
   3250   1.1    dyoung */
   3251   1.1    dyoung static void
   3252   1.1    dyoung ixgbe_set_ivar(struct adapter *adapter, u8 entry, u8 vector, s8 type)
   3253   1.1    dyoung {
   3254   1.1    dyoung 	struct ixgbe_hw *hw = &adapter->hw;
   3255   1.1    dyoung 	u32 ivar, index;
   3256   1.1    dyoung 
   3257   1.1    dyoung 	vector |= IXGBE_IVAR_ALLOC_VAL;
   3258   1.1    dyoung 
   3259   1.1    dyoung 	switch (hw->mac.type) {
   3260   1.1    dyoung 
   3261   1.1    dyoung 	case ixgbe_mac_82598EB:
   3262   1.1    dyoung 		if (type == -1)
   3263   1.1    dyoung 			entry = IXGBE_IVAR_OTHER_CAUSES_INDEX;
   3264   1.1    dyoung 		else
   3265   1.1    dyoung 			entry += (type * 64);
   3266   1.1    dyoung 		index = (entry >> 2) & 0x1F;
   3267   1.1    dyoung 		ivar = IXGBE_READ_REG(hw, IXGBE_IVAR(index));
   3268   1.1    dyoung 		ivar &= ~(0xFF << (8 * (entry & 0x3)));
   3269   1.1    dyoung 		ivar |= (vector << (8 * (entry & 0x3)));
   3270   1.1    dyoung 		IXGBE_WRITE_REG(&adapter->hw, IXGBE_IVAR(index), ivar);
   3271   1.1    dyoung 		break;
   3272   1.1    dyoung 
   3273   1.1    dyoung 	case ixgbe_mac_82599EB:
   3274  1.24   msaitoh 	case ixgbe_mac_X540:
   3275   1.1    dyoung 		if (type == -1) { /* MISC IVAR */
   3276   1.1    dyoung 			index = (entry & 1) * 8;
   3277   1.1    dyoung 			ivar = IXGBE_READ_REG(hw, IXGBE_IVAR_MISC);
   3278   1.1    dyoung 			ivar &= ~(0xFF << index);
   3279   1.1    dyoung 			ivar |= (vector << index);
   3280   1.1    dyoung 			IXGBE_WRITE_REG(hw, IXGBE_IVAR_MISC, ivar);
   3281   1.1    dyoung 		} else {	/* RX/TX IVARS */
   3282   1.1    dyoung 			index = (16 * (entry & 1)) + (8 * type);
   3283   1.1    dyoung 			ivar = IXGBE_READ_REG(hw, IXGBE_IVAR(entry >> 1));
   3284   1.1    dyoung 			ivar &= ~(0xFF << index);
   3285   1.1    dyoung 			ivar |= (vector << index);
   3286   1.1    dyoung 			IXGBE_WRITE_REG(hw, IXGBE_IVAR(entry >> 1), ivar);
   3287   1.1    dyoung 		}
   3288   1.1    dyoung 
   3289   1.1    dyoung 	default:
   3290   1.1    dyoung 		break;
   3291   1.1    dyoung 	}
   3292   1.1    dyoung }
   3293   1.1    dyoung 
   3294   1.1    dyoung static void
   3295   1.1    dyoung ixgbe_configure_ivars(struct adapter *adapter)
   3296   1.1    dyoung {
   3297   1.1    dyoung 	struct  ix_queue *que = adapter->queues;
   3298   1.1    dyoung 	u32 newitr;
   3299   1.1    dyoung 
   3300   1.1    dyoung 	if (ixgbe_max_interrupt_rate > 0)
   3301  1.22   msaitoh 		newitr = (4000000 / ixgbe_max_interrupt_rate) & 0x0FF8;
   3302   1.1    dyoung 	else
   3303   1.1    dyoung 		newitr = 0;
   3304   1.1    dyoung 
   3305   1.1    dyoung         for (int i = 0; i < adapter->num_queues; i++, que++) {
   3306   1.1    dyoung 		/* First the RX queue entry */
   3307   1.1    dyoung                 ixgbe_set_ivar(adapter, i, que->msix, 0);
   3308   1.1    dyoung 		/* ... and the TX */
   3309   1.1    dyoung 		ixgbe_set_ivar(adapter, i, que->msix, 1);
   3310   1.1    dyoung 		/* Set an Initial EITR value */
   3311   1.1    dyoung                 IXGBE_WRITE_REG(&adapter->hw,
   3312   1.1    dyoung                     IXGBE_EITR(que->msix), newitr);
   3313   1.1    dyoung 	}
   3314   1.1    dyoung 
   3315   1.1    dyoung 	/* For the Link interrupt */
   3316   1.1    dyoung         ixgbe_set_ivar(adapter, 1, adapter->linkvec, -1);
   3317   1.1    dyoung }
   3318   1.1    dyoung 
   3319   1.1    dyoung /*
   3320   1.1    dyoung ** ixgbe_sfp_probe - called in the local timer to
   3321   1.1    dyoung ** determine if a port had optics inserted.
   3322   1.1    dyoung */
   3323   1.1    dyoung static bool ixgbe_sfp_probe(struct adapter *adapter)
   3324   1.1    dyoung {
   3325   1.1    dyoung 	struct ixgbe_hw	*hw = &adapter->hw;
   3326   1.1    dyoung 	device_t	dev = adapter->dev;
   3327   1.1    dyoung 	bool		result = FALSE;
   3328   1.1    dyoung 
   3329   1.1    dyoung 	if ((hw->phy.type == ixgbe_phy_nl) &&
   3330   1.1    dyoung 	    (hw->phy.sfp_type == ixgbe_sfp_type_not_present)) {
   3331   1.1    dyoung 		s32 ret = hw->phy.ops.identify_sfp(hw);
   3332   1.1    dyoung 		if (ret)
   3333   1.1    dyoung                         goto out;
   3334   1.1    dyoung 		ret = hw->phy.ops.reset(hw);
   3335   1.1    dyoung 		if (ret == IXGBE_ERR_SFP_NOT_SUPPORTED) {
   3336   1.1    dyoung 			device_printf(dev,"Unsupported SFP+ module detected!");
   3337   1.1    dyoung 			device_printf(dev, "Reload driver with supported module.\n");
   3338   1.1    dyoung 			adapter->sfp_probe = FALSE;
   3339   1.1    dyoung                         goto out;
   3340   1.1    dyoung 		} else
   3341   1.1    dyoung 			device_printf(dev,"SFP+ module detected!\n");
   3342   1.1    dyoung 		/* We now have supported optics */
   3343   1.1    dyoung 		adapter->sfp_probe = FALSE;
   3344   1.1    dyoung 		/* Set the optics type so system reports correctly */
   3345   1.1    dyoung 		ixgbe_setup_optics(adapter);
   3346   1.1    dyoung 		result = TRUE;
   3347   1.1    dyoung 	}
   3348   1.1    dyoung out:
   3349   1.1    dyoung 	return (result);
   3350   1.1    dyoung }
   3351   1.1    dyoung 
   3352   1.1    dyoung /*
   3353   1.1    dyoung ** Tasklet handler for MSIX Link interrupts
   3354   1.1    dyoung **  - do outside interrupt since it might sleep
   3355   1.1    dyoung */
   3356   1.1    dyoung static void
   3357   1.1    dyoung ixgbe_handle_link(void *context)
   3358   1.1    dyoung {
   3359   1.1    dyoung 	struct adapter  *adapter = context;
   3360   1.1    dyoung 
   3361  1.13  christos 	if (ixgbe_check_link(&adapter->hw,
   3362  1.13  christos 	    &adapter->link_speed, &adapter->link_up, 0) == 0)
   3363  1.13  christos 	    ixgbe_update_link_status(adapter);
   3364   1.1    dyoung }
   3365   1.1    dyoung 
   3366   1.1    dyoung /*
   3367   1.1    dyoung ** Tasklet for handling SFP module interrupts
   3368   1.1    dyoung */
   3369   1.1    dyoung static void
   3370   1.1    dyoung ixgbe_handle_mod(void *context)
   3371   1.1    dyoung {
   3372   1.1    dyoung 	struct adapter  *adapter = context;
   3373   1.1    dyoung 	struct ixgbe_hw *hw = &adapter->hw;
   3374   1.1    dyoung 	device_t	dev = adapter->dev;
   3375   1.1    dyoung 	u32 err;
   3376   1.1    dyoung 
   3377   1.1    dyoung 	err = hw->phy.ops.identify_sfp(hw);
   3378   1.1    dyoung 	if (err == IXGBE_ERR_SFP_NOT_SUPPORTED) {
   3379   1.1    dyoung 		device_printf(dev,
   3380   1.1    dyoung 		    "Unsupported SFP+ module type was detected.\n");
   3381   1.1    dyoung 		return;
   3382   1.1    dyoung 	}
   3383   1.1    dyoung 	err = hw->mac.ops.setup_sfp(hw);
   3384   1.1    dyoung 	if (err == IXGBE_ERR_SFP_NOT_SUPPORTED) {
   3385   1.1    dyoung 		device_printf(dev,
   3386   1.1    dyoung 		    "Setup failure - unsupported SFP+ module type.\n");
   3387   1.1    dyoung 		return;
   3388   1.1    dyoung 	}
   3389   1.1    dyoung 	softint_schedule(adapter->msf_si);
   3390   1.1    dyoung 	return;
   3391   1.1    dyoung }
   3392   1.1    dyoung 
   3393   1.1    dyoung 
   3394   1.1    dyoung /*
   3395   1.1    dyoung ** Tasklet for handling MSF (multispeed fiber) interrupts
   3396   1.1    dyoung */
   3397   1.1    dyoung static void
   3398   1.1    dyoung ixgbe_handle_msf(void *context)
   3399   1.1    dyoung {
   3400   1.1    dyoung 	struct adapter  *adapter = context;
   3401   1.1    dyoung 	struct ixgbe_hw *hw = &adapter->hw;
   3402   1.1    dyoung 	u32 autoneg;
   3403   1.1    dyoung 	bool negotiate;
   3404   1.1    dyoung 
   3405   1.1    dyoung 	autoneg = hw->phy.autoneg_advertised;
   3406   1.1    dyoung 	if ((!autoneg) && (hw->mac.ops.get_link_capabilities))
   3407   1.1    dyoung 		hw->mac.ops.get_link_capabilities(hw, &autoneg, &negotiate);
   3408  1.13  christos 	else
   3409  1.13  christos 		negotiate = 0;
   3410   1.1    dyoung 	if (hw->mac.ops.setup_link)
   3411  1.28   msaitoh 		hw->mac.ops.setup_link(hw, autoneg, TRUE);
   3412   1.1    dyoung 	return;
   3413   1.1    dyoung }
   3414   1.1    dyoung 
   3415   1.1    dyoung #ifdef IXGBE_FDIR
   3416   1.1    dyoung /*
   3417   1.1    dyoung ** Tasklet for reinitializing the Flow Director filter table
   3418   1.1    dyoung */
   3419   1.1    dyoung static void
   3420   1.1    dyoung ixgbe_reinit_fdir(void *context)
   3421   1.1    dyoung {
   3422   1.1    dyoung 	struct adapter  *adapter = context;
   3423   1.1    dyoung 	struct ifnet   *ifp = adapter->ifp;
   3424   1.1    dyoung 
   3425   1.1    dyoung 	if (adapter->fdir_reinit != 1) /* Shouldn't happen */
   3426   1.1    dyoung 		return;
   3427   1.1    dyoung 	ixgbe_reinit_fdir_tables_82599(&adapter->hw);
   3428   1.1    dyoung 	adapter->fdir_reinit = 0;
   3429  1.25   msaitoh 	/* re-enable flow director interrupts */
   3430  1.25   msaitoh 	IXGBE_WRITE_REG(&adapter->hw, IXGBE_EIMS, IXGBE_EIMS_FLOW_DIR);
   3431   1.1    dyoung 	/* Restart the interface */
   3432   1.1    dyoung 	ifp->if_flags |= IFF_RUNNING;
   3433   1.1    dyoung 	return;
   3434   1.1    dyoung }
   3435   1.1    dyoung #endif
   3436   1.1    dyoung 
   3437   1.1    dyoung /**********************************************************************
   3438   1.1    dyoung  *
   3439   1.1    dyoung  *  Update the board statistics counters.
   3440   1.1    dyoung  *
   3441   1.1    dyoung  **********************************************************************/
   3442   1.1    dyoung static void
   3443   1.1    dyoung ixgbe_update_stats_counters(struct adapter *adapter)
   3444   1.1    dyoung {
   3445   1.1    dyoung 	struct ifnet   *ifp = adapter->ifp;
   3446   1.1    dyoung 	struct ixgbe_hw *hw = &adapter->hw;
   3447   1.1    dyoung 	u32  missed_rx = 0, bprc, lxon, lxoff, total;
   3448   1.1    dyoung 	u64  total_missed_rx = 0;
   3449  1.27   msaitoh 	uint64_t crcerrs, rlec;
   3450   1.1    dyoung 
   3451  1.27   msaitoh 	crcerrs = IXGBE_READ_REG(hw, IXGBE_CRCERRS);
   3452  1.27   msaitoh 	adapter->stats.crcerrs.ev_count += crcerrs;
   3453   1.1    dyoung 	adapter->stats.illerrc.ev_count += IXGBE_READ_REG(hw, IXGBE_ILLERRC);
   3454   1.1    dyoung 	adapter->stats.errbc.ev_count += IXGBE_READ_REG(hw, IXGBE_ERRBC);
   3455   1.1    dyoung 	adapter->stats.mspdc.ev_count += IXGBE_READ_REG(hw, IXGBE_MSPDC);
   3456   1.1    dyoung 
   3457  1.28   msaitoh 	/*
   3458  1.28   msaitoh 	** Note: these are for the 8 possible traffic classes,
   3459  1.28   msaitoh 	**	 which in current implementation is unused,
   3460  1.28   msaitoh 	**	 therefore only 0 should read real data.
   3461  1.28   msaitoh 	*/
   3462   1.1    dyoung 	for (int i = 0; i < __arraycount(adapter->stats.mpc); i++) {
   3463   1.1    dyoung 		int j = i % adapter->num_queues;
   3464   1.1    dyoung 		u32 mp;
   3465   1.1    dyoung 		mp = IXGBE_READ_REG(hw, IXGBE_MPC(i));
   3466   1.1    dyoung 		/* missed_rx tallies misses for the gprc workaround */
   3467   1.1    dyoung 		missed_rx += mp;
   3468   1.1    dyoung 		/* global total per queue */
   3469   1.1    dyoung         	adapter->stats.mpc[j].ev_count += mp;
   3470   1.1    dyoung 		/* Running comprehensive total for stats display */
   3471  1.27   msaitoh 		total_missed_rx += mp;
   3472  1.28   msaitoh 		if (hw->mac.type == ixgbe_mac_82598EB) {
   3473   1.1    dyoung 			adapter->stats.rnbc[j] +=
   3474   1.1    dyoung 			    IXGBE_READ_REG(hw, IXGBE_RNBC(i));
   3475  1.28   msaitoh 			adapter->stats.qbtc[j].ev_count +=
   3476  1.28   msaitoh 			    IXGBE_READ_REG(hw, IXGBE_QBTC(i));
   3477  1.28   msaitoh 			adapter->stats.qbrc[j].ev_count +=
   3478  1.28   msaitoh 			    IXGBE_READ_REG(hw, IXGBE_QBRC(i));
   3479  1.28   msaitoh 			adapter->stats.pxonrxc[j].ev_count +=
   3480  1.28   msaitoh 			    IXGBE_READ_REG(hw, IXGBE_PXONRXC(i));
   3481  1.28   msaitoh 		} else {
   3482  1.28   msaitoh 			adapter->stats.pxonrxc[j].ev_count +=
   3483  1.28   msaitoh 			    IXGBE_READ_REG(hw, IXGBE_PXONRXCNT(i));
   3484  1.28   msaitoh 		}
   3485   1.1    dyoung 		adapter->stats.pxontxc[j].ev_count +=
   3486   1.1    dyoung 		    IXGBE_READ_REG(hw, IXGBE_PXONTXC(i));
   3487   1.1    dyoung 		adapter->stats.pxofftxc[j].ev_count +=
   3488   1.1    dyoung 		    IXGBE_READ_REG(hw, IXGBE_PXOFFTXC(i));
   3489   1.1    dyoung 		adapter->stats.pxoffrxc[j].ev_count +=
   3490   1.1    dyoung 		    IXGBE_READ_REG(hw, IXGBE_PXOFFRXC(i));
   3491   1.1    dyoung 		adapter->stats.pxon2offc[j].ev_count +=
   3492   1.1    dyoung 		    IXGBE_READ_REG(hw, IXGBE_PXON2OFFCNT(i));
   3493   1.1    dyoung 	}
   3494   1.1    dyoung 	for (int i = 0; i < __arraycount(adapter->stats.qprc); i++) {
   3495   1.1    dyoung 		int j = i % adapter->num_queues;
   3496   1.1    dyoung 		adapter->stats.qprc[j].ev_count += IXGBE_READ_REG(hw, IXGBE_QPRC(i));
   3497   1.1    dyoung 		adapter->stats.qptc[j].ev_count += IXGBE_READ_REG(hw, IXGBE_QPTC(i));
   3498   1.1    dyoung 		adapter->stats.qprdc[j].ev_count += IXGBE_READ_REG(hw, IXGBE_QPRDC(i));
   3499   1.1    dyoung 	}
   3500   1.1    dyoung 	adapter->stats.mlfc.ev_count += IXGBE_READ_REG(hw, IXGBE_MLFC);
   3501   1.1    dyoung 	adapter->stats.mrfc.ev_count += IXGBE_READ_REG(hw, IXGBE_MRFC);
   3502  1.27   msaitoh 	rlec = IXGBE_READ_REG(hw, IXGBE_RLEC);
   3503  1.27   msaitoh 	adapter->stats.rlec.ev_count += rlec;
   3504   1.1    dyoung 
   3505   1.1    dyoung 	/* Hardware workaround, gprc counts missed packets */
   3506   1.1    dyoung 	adapter->stats.gprc.ev_count += IXGBE_READ_REG(hw, IXGBE_GPRC) - missed_rx;
   3507   1.1    dyoung 
   3508   1.1    dyoung 	lxon = IXGBE_READ_REG(hw, IXGBE_LXONTXC);
   3509   1.1    dyoung 	adapter->stats.lxontxc.ev_count += lxon;
   3510   1.1    dyoung 	lxoff = IXGBE_READ_REG(hw, IXGBE_LXOFFTXC);
   3511   1.1    dyoung 	adapter->stats.lxofftxc.ev_count += lxoff;
   3512   1.1    dyoung 	total = lxon + lxoff;
   3513   1.1    dyoung 
   3514   1.1    dyoung 	if (hw->mac.type != ixgbe_mac_82598EB) {
   3515   1.1    dyoung 		adapter->stats.gorc.ev_count += IXGBE_READ_REG(hw, IXGBE_GORCL) +
   3516   1.1    dyoung 		    ((u64)IXGBE_READ_REG(hw, IXGBE_GORCH) << 32);
   3517   1.1    dyoung 		adapter->stats.gotc.ev_count += IXGBE_READ_REG(hw, IXGBE_GOTCL) +
   3518   1.1    dyoung 		    ((u64)IXGBE_READ_REG(hw, IXGBE_GOTCH) << 32) - total * ETHER_MIN_LEN;
   3519   1.1    dyoung 		adapter->stats.tor.ev_count += IXGBE_READ_REG(hw, IXGBE_TORL) +
   3520   1.1    dyoung 		    ((u64)IXGBE_READ_REG(hw, IXGBE_TORH) << 32);
   3521   1.1    dyoung 		adapter->stats.lxonrxc.ev_count += IXGBE_READ_REG(hw, IXGBE_LXONRXCNT);
   3522   1.1    dyoung 		adapter->stats.lxoffrxc.ev_count += IXGBE_READ_REG(hw, IXGBE_LXOFFRXCNT);
   3523   1.1    dyoung 	} else {
   3524   1.1    dyoung 		adapter->stats.lxonrxc.ev_count += IXGBE_READ_REG(hw, IXGBE_LXONRXC);
   3525   1.1    dyoung 		adapter->stats.lxoffrxc.ev_count += IXGBE_READ_REG(hw, IXGBE_LXOFFRXC);
   3526   1.1    dyoung 		/* 82598 only has a counter in the high register */
   3527   1.1    dyoung 		adapter->stats.gorc.ev_count += IXGBE_READ_REG(hw, IXGBE_GORCH);
   3528   1.1    dyoung 		adapter->stats.gotc.ev_count += IXGBE_READ_REG(hw, IXGBE_GOTCH) - total * ETHER_MIN_LEN;
   3529   1.1    dyoung 		adapter->stats.tor.ev_count += IXGBE_READ_REG(hw, IXGBE_TORH);
   3530   1.1    dyoung 	}
   3531   1.1    dyoung 
   3532   1.1    dyoung 	/*
   3533   1.1    dyoung 	 * Workaround: mprc hardware is incorrectly counting
   3534   1.1    dyoung 	 * broadcasts, so for now we subtract those.
   3535   1.1    dyoung 	 */
   3536   1.1    dyoung 	bprc = IXGBE_READ_REG(hw, IXGBE_BPRC);
   3537   1.1    dyoung 	adapter->stats.bprc.ev_count += bprc;
   3538   1.1    dyoung 	adapter->stats.mprc.ev_count += IXGBE_READ_REG(hw, IXGBE_MPRC) - ((hw->mac.type == ixgbe_mac_82598EB) ? bprc : 0);
   3539   1.1    dyoung 
   3540   1.1    dyoung 	adapter->stats.prc64.ev_count += IXGBE_READ_REG(hw, IXGBE_PRC64);
   3541   1.1    dyoung 	adapter->stats.prc127.ev_count += IXGBE_READ_REG(hw, IXGBE_PRC127);
   3542   1.1    dyoung 	adapter->stats.prc255.ev_count += IXGBE_READ_REG(hw, IXGBE_PRC255);
   3543   1.1    dyoung 	adapter->stats.prc511.ev_count += IXGBE_READ_REG(hw, IXGBE_PRC511);
   3544   1.1    dyoung 	adapter->stats.prc1023.ev_count += IXGBE_READ_REG(hw, IXGBE_PRC1023);
   3545   1.1    dyoung 	adapter->stats.prc1522.ev_count += IXGBE_READ_REG(hw, IXGBE_PRC1522);
   3546   1.1    dyoung 
   3547   1.1    dyoung 	adapter->stats.gptc.ev_count += IXGBE_READ_REG(hw, IXGBE_GPTC) - total;
   3548   1.1    dyoung 	adapter->stats.mptc.ev_count += IXGBE_READ_REG(hw, IXGBE_MPTC) - total;
   3549   1.1    dyoung 	adapter->stats.ptc64.ev_count += IXGBE_READ_REG(hw, IXGBE_PTC64) - total;
   3550   1.1    dyoung 
   3551   1.1    dyoung 	adapter->stats.ruc.ev_count += IXGBE_READ_REG(hw, IXGBE_RUC);
   3552   1.1    dyoung 	adapter->stats.rfc.ev_count += IXGBE_READ_REG(hw, IXGBE_RFC);
   3553   1.1    dyoung 	adapter->stats.roc.ev_count += IXGBE_READ_REG(hw, IXGBE_ROC);
   3554   1.1    dyoung 	adapter->stats.rjc.ev_count += IXGBE_READ_REG(hw, IXGBE_RJC);
   3555   1.1    dyoung 	adapter->stats.mngprc.ev_count += IXGBE_READ_REG(hw, IXGBE_MNGPRC);
   3556   1.1    dyoung 	adapter->stats.mngpdc.ev_count += IXGBE_READ_REG(hw, IXGBE_MNGPDC);
   3557   1.1    dyoung 	adapter->stats.mngptc.ev_count += IXGBE_READ_REG(hw, IXGBE_MNGPTC);
   3558   1.1    dyoung 	adapter->stats.tpr.ev_count += IXGBE_READ_REG(hw, IXGBE_TPR);
   3559   1.1    dyoung 	adapter->stats.tpt.ev_count += IXGBE_READ_REG(hw, IXGBE_TPT);
   3560   1.1    dyoung 	adapter->stats.ptc127.ev_count += IXGBE_READ_REG(hw, IXGBE_PTC127);
   3561   1.1    dyoung 	adapter->stats.ptc255.ev_count += IXGBE_READ_REG(hw, IXGBE_PTC255);
   3562   1.1    dyoung 	adapter->stats.ptc511.ev_count += IXGBE_READ_REG(hw, IXGBE_PTC511);
   3563   1.1    dyoung 	adapter->stats.ptc1023.ev_count += IXGBE_READ_REG(hw, IXGBE_PTC1023);
   3564   1.1    dyoung 	adapter->stats.ptc1522.ev_count += IXGBE_READ_REG(hw, IXGBE_PTC1522);
   3565   1.1    dyoung 	adapter->stats.bptc.ev_count += IXGBE_READ_REG(hw, IXGBE_BPTC);
   3566   1.1    dyoung 	adapter->stats.xec.ev_count += IXGBE_READ_REG(hw, IXGBE_XEC);
   3567   1.1    dyoung 	adapter->stats.fccrc.ev_count += IXGBE_READ_REG(hw, IXGBE_FCCRC);
   3568   1.1    dyoung 	adapter->stats.fclast.ev_count += IXGBE_READ_REG(hw, IXGBE_FCLAST);
   3569   1.1    dyoung 
   3570   1.1    dyoung 	/* Only read FCOE on 82599 */
   3571   1.1    dyoung 	if (hw->mac.type != ixgbe_mac_82598EB) {
   3572   1.1    dyoung 		adapter->stats.fcoerpdc.ev_count +=
   3573   1.1    dyoung 		    IXGBE_READ_REG(hw, IXGBE_FCOERPDC);
   3574   1.1    dyoung 		adapter->stats.fcoeprc.ev_count +=
   3575   1.1    dyoung 		    IXGBE_READ_REG(hw, IXGBE_FCOEPRC);
   3576   1.1    dyoung 		adapter->stats.fcoeptc.ev_count +=
   3577   1.1    dyoung 		    IXGBE_READ_REG(hw, IXGBE_FCOEPTC);
   3578   1.1    dyoung 		adapter->stats.fcoedwrc.ev_count +=
   3579   1.1    dyoung 		    IXGBE_READ_REG(hw, IXGBE_FCOEDWRC);
   3580   1.1    dyoung 		adapter->stats.fcoedwtc.ev_count +=
   3581   1.1    dyoung 		    IXGBE_READ_REG(hw, IXGBE_FCOEDWTC);
   3582   1.1    dyoung 	}
   3583   1.1    dyoung 
   3584   1.1    dyoung 	/* Fill out the OS statistics structure */
   3585  1.27   msaitoh 	/*
   3586  1.27   msaitoh 	 * NetBSD: Don't override if_{i|o}{packets|bytes|mcasts} with
   3587  1.27   msaitoh 	 * adapter->stats counters. It's required to make ifconfig -z
   3588  1.27   msaitoh 	 * (SOICZIFDATA) work.
   3589  1.27   msaitoh 	 */
   3590   1.1    dyoung 	ifp->if_collisions = 0;
   3591  1.28   msaitoh 
   3592   1.1    dyoung 	/* Rx Errors */
   3593  1.28   msaitoh 	ifp->if_iqdrops += total_missed_rx;
   3594  1.28   msaitoh 	ifp->if_ierrors += crcerrs + rlec;
   3595   1.1    dyoung }
   3596   1.1    dyoung 
   3597   1.1    dyoung /** ixgbe_sysctl_tdh_handler - Handler function
   3598   1.1    dyoung  *  Retrieves the TDH value from the hardware
   3599   1.1    dyoung  */
   3600   1.1    dyoung static int
   3601   1.1    dyoung ixgbe_sysctl_tdh_handler(SYSCTLFN_ARGS)
   3602   1.1    dyoung {
   3603   1.1    dyoung 	struct sysctlnode node;
   3604   1.1    dyoung 	uint32_t val;
   3605   1.1    dyoung 	struct tx_ring *txr;
   3606   1.1    dyoung 
   3607   1.1    dyoung 	node = *rnode;
   3608   1.1    dyoung 	txr = (struct tx_ring *)node.sysctl_data;
   3609   1.1    dyoung 	if (txr == NULL)
   3610   1.1    dyoung 		return 0;
   3611   1.1    dyoung 	val = IXGBE_READ_REG(&txr->adapter->hw, IXGBE_TDH(txr->me));
   3612   1.1    dyoung 	node.sysctl_data = &val;
   3613   1.1    dyoung 	return sysctl_lookup(SYSCTLFN_CALL(&node));
   3614   1.1    dyoung }
   3615   1.1    dyoung 
   3616   1.1    dyoung /** ixgbe_sysctl_tdt_handler - Handler function
   3617   1.1    dyoung  *  Retrieves the TDT value from the hardware
   3618   1.1    dyoung  */
   3619   1.1    dyoung static int
   3620   1.1    dyoung ixgbe_sysctl_tdt_handler(SYSCTLFN_ARGS)
   3621   1.1    dyoung {
   3622   1.1    dyoung 	struct sysctlnode node;
   3623   1.1    dyoung 	uint32_t val;
   3624   1.1    dyoung 	struct tx_ring *txr;
   3625   1.1    dyoung 
   3626   1.1    dyoung 	node = *rnode;
   3627   1.1    dyoung 	txr = (struct tx_ring *)node.sysctl_data;
   3628   1.1    dyoung 	if (txr == NULL)
   3629   1.1    dyoung 		return 0;
   3630   1.1    dyoung 	val = IXGBE_READ_REG(&txr->adapter->hw, IXGBE_TDT(txr->me));
   3631   1.1    dyoung 	node.sysctl_data = &val;
   3632   1.1    dyoung 	return sysctl_lookup(SYSCTLFN_CALL(&node));
   3633   1.1    dyoung }
   3634   1.1    dyoung 
   3635   1.1    dyoung /** ixgbe_sysctl_rdh_handler - Handler function
   3636   1.1    dyoung  *  Retrieves the RDH value from the hardware
   3637   1.1    dyoung  */
   3638   1.1    dyoung static int
   3639   1.1    dyoung ixgbe_sysctl_rdh_handler(SYSCTLFN_ARGS)
   3640   1.1    dyoung {
   3641   1.1    dyoung 	struct sysctlnode node;
   3642   1.1    dyoung 	uint32_t val;
   3643   1.1    dyoung 	struct rx_ring *rxr;
   3644   1.1    dyoung 
   3645   1.1    dyoung 	node = *rnode;
   3646   1.1    dyoung 	rxr = (struct rx_ring *)node.sysctl_data;
   3647   1.1    dyoung 	if (rxr == NULL)
   3648   1.1    dyoung 		return 0;
   3649   1.1    dyoung 	val = IXGBE_READ_REG(&rxr->adapter->hw, IXGBE_RDH(rxr->me));
   3650   1.1    dyoung 	node.sysctl_data = &val;
   3651   1.1    dyoung 	return sysctl_lookup(SYSCTLFN_CALL(&node));
   3652   1.1    dyoung }
   3653   1.1    dyoung 
   3654   1.1    dyoung /** ixgbe_sysctl_rdt_handler - Handler function
   3655   1.1    dyoung  *  Retrieves the RDT value from the hardware
   3656   1.1    dyoung  */
   3657   1.1    dyoung static int
   3658   1.1    dyoung ixgbe_sysctl_rdt_handler(SYSCTLFN_ARGS)
   3659   1.1    dyoung {
   3660   1.1    dyoung 	struct sysctlnode node;
   3661   1.1    dyoung 	uint32_t val;
   3662   1.1    dyoung 	struct rx_ring *rxr;
   3663   1.1    dyoung 
   3664   1.1    dyoung 	node = *rnode;
   3665   1.1    dyoung 	rxr = (struct rx_ring *)node.sysctl_data;
   3666   1.1    dyoung 	if (rxr == NULL)
   3667   1.1    dyoung 		return 0;
   3668   1.1    dyoung 	val = IXGBE_READ_REG(&rxr->adapter->hw, IXGBE_RDT(rxr->me));
   3669   1.1    dyoung 	node.sysctl_data = &val;
   3670   1.1    dyoung 	return sysctl_lookup(SYSCTLFN_CALL(&node));
   3671   1.1    dyoung }
   3672   1.1    dyoung 
   3673   1.1    dyoung static int
   3674   1.1    dyoung ixgbe_sysctl_interrupt_rate_handler(SYSCTLFN_ARGS)
   3675   1.1    dyoung {
   3676  1.22   msaitoh 	int error;
   3677   1.1    dyoung 	struct sysctlnode node;
   3678   1.1    dyoung 	struct ix_queue *que;
   3679   1.1    dyoung 	uint32_t reg, usec, rate;
   3680   1.1    dyoung 
   3681   1.1    dyoung 	node = *rnode;
   3682   1.1    dyoung 	que = (struct ix_queue *)node.sysctl_data;
   3683   1.1    dyoung 	if (que == NULL)
   3684   1.1    dyoung 		return 0;
   3685   1.1    dyoung 	reg = IXGBE_READ_REG(&que->adapter->hw, IXGBE_EITR(que->msix));
   3686   1.1    dyoung 	usec = ((reg & 0x0FF8) >> 3);
   3687   1.1    dyoung 	if (usec > 0)
   3688  1.22   msaitoh 		rate = 500000 / usec;
   3689   1.1    dyoung 	else
   3690   1.1    dyoung 		rate = 0;
   3691   1.1    dyoung 	node.sysctl_data = &rate;
   3692  1.22   msaitoh 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   3693  1.22   msaitoh 	if (error)
   3694  1.22   msaitoh 		return error;
   3695  1.22   msaitoh 	reg &= ~0xfff; /* default, no limitation */
   3696  1.22   msaitoh 	ixgbe_max_interrupt_rate = 0;
   3697  1.22   msaitoh 	if (rate > 0 && rate < 500000) {
   3698  1.22   msaitoh 		if (rate < 1000)
   3699  1.22   msaitoh 			rate = 1000;
   3700  1.22   msaitoh 		ixgbe_max_interrupt_rate = rate;
   3701  1.22   msaitoh 		reg |= ((4000000/rate) & 0xff8 );
   3702  1.22   msaitoh 	}
   3703  1.22   msaitoh 	IXGBE_WRITE_REG(&que->adapter->hw, IXGBE_EITR(que->msix), reg);
   3704  1.22   msaitoh 	return 0;
   3705   1.1    dyoung }
   3706   1.1    dyoung 
   3707   1.1    dyoung const struct sysctlnode *
   3708   1.1    dyoung ixgbe_sysctl_instance(struct adapter *adapter)
   3709   1.1    dyoung {
   3710   1.1    dyoung 	const char *dvname;
   3711   1.1    dyoung 	struct sysctllog **log;
   3712   1.1    dyoung 	int rc;
   3713   1.1    dyoung 	const struct sysctlnode *rnode;
   3714   1.1    dyoung 
   3715   1.1    dyoung 	log = &adapter->sysctllog;
   3716   1.1    dyoung 	dvname = device_xname(adapter->dev);
   3717   1.1    dyoung 
   3718   1.1    dyoung 	if ((rc = sysctl_createv(log, 0, NULL, &rnode,
   3719   1.1    dyoung 	    0, CTLTYPE_NODE, dvname,
   3720   1.1    dyoung 	    SYSCTL_DESCR("ixgbe information and settings"),
   3721   1.7     pooka 	    NULL, 0, NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL)) != 0)
   3722   1.1    dyoung 		goto err;
   3723   1.1    dyoung 
   3724   1.1    dyoung 	return rnode;
   3725   1.1    dyoung err:
   3726   1.1    dyoung 	printf("%s: sysctl_createv failed, rc = %d\n", __func__, rc);
   3727   1.1    dyoung 	return NULL;
   3728   1.1    dyoung }
   3729   1.1    dyoung 
   3730   1.1    dyoung /*
   3731   1.1    dyoung  * Add sysctl variables, one per statistic, to the system.
   3732   1.1    dyoung  */
   3733   1.1    dyoung static void
   3734   1.1    dyoung ixgbe_add_hw_stats(struct adapter *adapter)
   3735   1.1    dyoung {
   3736   1.1    dyoung 	device_t dev = adapter->dev;
   3737   1.1    dyoung 	const struct sysctlnode *rnode, *cnode;
   3738   1.1    dyoung 	struct sysctllog **log = &adapter->sysctllog;
   3739   1.1    dyoung 	struct tx_ring *txr = adapter->tx_rings;
   3740   1.1    dyoung 	struct rx_ring *rxr = adapter->rx_rings;
   3741   1.1    dyoung 	struct ixgbe_hw_stats *stats = &adapter->stats;
   3742   1.1    dyoung 
   3743   1.1    dyoung 	/* Driver Statistics */
   3744   1.1    dyoung #if 0
   3745   1.1    dyoung 	/* These counters are not updated by the software */
   3746   1.1    dyoung 	SYSCTL_ADD_ULONG(ctx, child, OID_AUTO, "dropped",
   3747   1.1    dyoung 			CTLFLAG_RD, &adapter->dropped_pkts,
   3748   1.1    dyoung 			"Driver dropped packets");
   3749   1.1    dyoung 	SYSCTL_ADD_ULONG(ctx, child, OID_AUTO, "mbuf_header_failed",
   3750   1.1    dyoung 			CTLFLAG_RD, &adapter->mbuf_header_failed,
   3751   1.1    dyoung 			"???");
   3752   1.1    dyoung 	SYSCTL_ADD_ULONG(ctx, child, OID_AUTO, "mbuf_packet_failed",
   3753   1.1    dyoung 			CTLFLAG_RD, &adapter->mbuf_packet_failed,
   3754   1.1    dyoung 			"???");
   3755   1.1    dyoung 	SYSCTL_ADD_ULONG(ctx, child, OID_AUTO, "no_tx_map_avail",
   3756   1.1    dyoung 			CTLFLAG_RD, &adapter->no_tx_map_avail,
   3757   1.1    dyoung 			"???");
   3758   1.1    dyoung #endif
   3759   1.1    dyoung 	evcnt_attach_dynamic(&adapter->handleq, EVCNT_TYPE_MISC,
   3760   1.1    dyoung 	    NULL, device_xname(dev), "Handled queue in softint");
   3761   1.1    dyoung 	evcnt_attach_dynamic(&adapter->req, EVCNT_TYPE_MISC,
   3762   1.1    dyoung 	    NULL, device_xname(dev), "Requeued in softint");
   3763   1.1    dyoung 	evcnt_attach_dynamic(&adapter->morerx, EVCNT_TYPE_MISC,
   3764   1.1    dyoung 	    NULL, device_xname(dev), "Interrupt handler more rx");
   3765   1.1    dyoung 	evcnt_attach_dynamic(&adapter->moretx, EVCNT_TYPE_MISC,
   3766   1.1    dyoung 	    NULL, device_xname(dev), "Interrupt handler more tx");
   3767   1.1    dyoung 	evcnt_attach_dynamic(&adapter->txloops, EVCNT_TYPE_MISC,
   3768   1.1    dyoung 	    NULL, device_xname(dev), "Interrupt handler tx loops");
   3769   1.1    dyoung 	evcnt_attach_dynamic(&adapter->efbig_tx_dma_setup, EVCNT_TYPE_MISC,
   3770   1.1    dyoung 	    NULL, device_xname(dev), "Driver tx dma soft fail EFBIG");
   3771   1.1    dyoung 	evcnt_attach_dynamic(&adapter->m_defrag_failed, EVCNT_TYPE_MISC,
   3772   1.1    dyoung 	    NULL, device_xname(dev), "m_defrag() failed");
   3773   1.1    dyoung 	evcnt_attach_dynamic(&adapter->efbig2_tx_dma_setup, EVCNT_TYPE_MISC,
   3774   1.1    dyoung 	    NULL, device_xname(dev), "Driver tx dma hard fail EFBIG");
   3775   1.1    dyoung 	evcnt_attach_dynamic(&adapter->einval_tx_dma_setup, EVCNT_TYPE_MISC,
   3776   1.1    dyoung 	    NULL, device_xname(dev), "Driver tx dma hard fail EINVAL");
   3777   1.1    dyoung 	evcnt_attach_dynamic(&adapter->other_tx_dma_setup, EVCNT_TYPE_MISC,
   3778   1.1    dyoung 	    NULL, device_xname(dev), "Driver tx dma hard fail other");
   3779   1.1    dyoung 	evcnt_attach_dynamic(&adapter->eagain_tx_dma_setup, EVCNT_TYPE_MISC,
   3780   1.1    dyoung 	    NULL, device_xname(dev), "Driver tx dma soft fail EAGAIN");
   3781   1.1    dyoung 	evcnt_attach_dynamic(&adapter->enomem_tx_dma_setup, EVCNT_TYPE_MISC,
   3782   1.1    dyoung 	    NULL, device_xname(dev), "Driver tx dma soft fail ENOMEM");
   3783   1.1    dyoung 	evcnt_attach_dynamic(&adapter->watchdog_events, EVCNT_TYPE_MISC,
   3784   1.1    dyoung 	    NULL, device_xname(dev), "Watchdog timeouts");
   3785   1.1    dyoung 	evcnt_attach_dynamic(&adapter->tso_err, EVCNT_TYPE_MISC,
   3786   1.1    dyoung 	    NULL, device_xname(dev), "TSO errors");
   3787   1.1    dyoung 	evcnt_attach_dynamic(&adapter->link_irq, EVCNT_TYPE_MISC,
   3788   1.1    dyoung 	    NULL, device_xname(dev), "Link MSIX IRQ Handled");
   3789   1.1    dyoung 
   3790   1.1    dyoung 	for (int i = 0; i < adapter->num_queues; i++, rxr++, txr++) {
   3791   1.1    dyoung 		snprintf(adapter->queues[i].evnamebuf,
   3792   1.1    dyoung 		    sizeof(adapter->queues[i].evnamebuf), "%s queue%d",
   3793   1.1    dyoung 		    device_xname(dev), i);
   3794   1.1    dyoung 		snprintf(adapter->queues[i].namebuf,
   3795   1.1    dyoung 		    sizeof(adapter->queues[i].namebuf), "queue%d", i);
   3796   1.1    dyoung 
   3797   1.1    dyoung 		if ((rnode = ixgbe_sysctl_instance(adapter)) == NULL) {
   3798   1.1    dyoung 			aprint_error_dev(dev, "could not create sysctl root\n");
   3799   1.1    dyoung 			break;
   3800   1.1    dyoung 		}
   3801   1.1    dyoung 
   3802   1.1    dyoung 		if (sysctl_createv(log, 0, &rnode, &rnode,
   3803   1.1    dyoung 		    0, CTLTYPE_NODE,
   3804   1.1    dyoung 		    adapter->queues[i].namebuf, SYSCTL_DESCR("Queue Name"),
   3805   1.1    dyoung 		    NULL, 0, NULL, 0, CTL_CREATE, CTL_EOL) != 0)
   3806   1.1    dyoung 			break;
   3807   1.1    dyoung 
   3808   1.1    dyoung 		if (sysctl_createv(log, 0, &rnode, &cnode,
   3809  1.22   msaitoh 		    CTLFLAG_READWRITE, CTLTYPE_INT,
   3810   1.1    dyoung 		    "interrupt_rate", SYSCTL_DESCR("Interrupt Rate"),
   3811   1.5       dsl 		    ixgbe_sysctl_interrupt_rate_handler, 0,
   3812   1.5       dsl 		    (void *)&adapter->queues[i], 0, CTL_CREATE, CTL_EOL) != 0)
   3813   1.1    dyoung 			break;
   3814   1.1    dyoung 
   3815   1.1    dyoung 		if (sysctl_createv(log, 0, &rnode, &cnode,
   3816  1.22   msaitoh 		    CTLFLAG_READONLY, CTLTYPE_QUAD,
   3817  1.22   msaitoh 		    "irqs", SYSCTL_DESCR("irqs on this queue"),
   3818  1.22   msaitoh 			NULL, 0, &(adapter->queues[i].irqs),
   3819  1.22   msaitoh 		    0, CTL_CREATE, CTL_EOL) != 0)
   3820  1.22   msaitoh 			break;
   3821  1.22   msaitoh 
   3822  1.22   msaitoh 		if (sysctl_createv(log, 0, &rnode, &cnode,
   3823   1.1    dyoung 		    CTLFLAG_READONLY, CTLTYPE_INT,
   3824   1.1    dyoung 		    "txd_head", SYSCTL_DESCR("Transmit Descriptor Head"),
   3825   1.4       dsl 		    ixgbe_sysctl_tdh_handler, 0, (void *)txr,
   3826   1.1    dyoung 		    0, CTL_CREATE, CTL_EOL) != 0)
   3827   1.1    dyoung 			break;
   3828   1.1    dyoung 
   3829   1.1    dyoung 		if (sysctl_createv(log, 0, &rnode, &cnode,
   3830   1.1    dyoung 		    CTLFLAG_READONLY, CTLTYPE_INT,
   3831   1.1    dyoung 		    "txd_tail", SYSCTL_DESCR("Transmit Descriptor Tail"),
   3832   1.4       dsl 		    ixgbe_sysctl_tdt_handler, 0, (void *)txr,
   3833   1.1    dyoung 		    0, CTL_CREATE, CTL_EOL) != 0)
   3834   1.1    dyoung 			break;
   3835   1.1    dyoung 
   3836  1.28   msaitoh 		evcnt_attach_dynamic(&txr->tso_tx, EVCNT_TYPE_MISC,
   3837  1.28   msaitoh 		    NULL, device_xname(dev), "TSO");
   3838   1.1    dyoung 		evcnt_attach_dynamic(&txr->no_desc_avail, EVCNT_TYPE_MISC,
   3839   1.1    dyoung 		    NULL, adapter->queues[i].evnamebuf,
   3840   1.1    dyoung 		    "Queue No Descriptor Available");
   3841   1.1    dyoung 		evcnt_attach_dynamic(&txr->total_packets, EVCNT_TYPE_MISC,
   3842   1.1    dyoung 		    NULL, adapter->queues[i].evnamebuf,
   3843   1.1    dyoung 		    "Queue Packets Transmitted");
   3844   1.1    dyoung 
   3845   1.1    dyoung #ifdef LRO
   3846   1.1    dyoung 		struct lro_ctrl *lro = &rxr->lro;
   3847   1.1    dyoung #endif /* LRO */
   3848   1.1    dyoung 
   3849   1.1    dyoung 		if (sysctl_createv(log, 0, &rnode, &cnode,
   3850   1.1    dyoung 		    CTLFLAG_READONLY,
   3851   1.1    dyoung 		    CTLTYPE_INT,
   3852   1.1    dyoung 		    "rxd_head", SYSCTL_DESCR("Receive Descriptor Head"),
   3853   1.4       dsl 		    ixgbe_sysctl_rdh_handler, 0, (void *)rxr, 0,
   3854   1.1    dyoung 		    CTL_CREATE, CTL_EOL) != 0)
   3855   1.1    dyoung 			break;
   3856   1.1    dyoung 
   3857   1.1    dyoung 		if (sysctl_createv(log, 0, &rnode, &cnode,
   3858   1.1    dyoung 		    CTLFLAG_READONLY,
   3859   1.1    dyoung 		    CTLTYPE_INT,
   3860   1.1    dyoung 		    "rxd_tail", SYSCTL_DESCR("Receive Descriptor Tail"),
   3861   1.4       dsl 		    ixgbe_sysctl_rdt_handler, 0, (void *)rxr, 0,
   3862   1.1    dyoung 		    CTL_CREATE, CTL_EOL) != 0)
   3863   1.1    dyoung 			break;
   3864   1.1    dyoung 
   3865   1.1    dyoung 		if (i < __arraycount(adapter->stats.mpc)) {
   3866   1.1    dyoung 			evcnt_attach_dynamic(&adapter->stats.mpc[i],
   3867   1.1    dyoung 			    EVCNT_TYPE_MISC, NULL, adapter->queues[i].evnamebuf,
   3868   1.1    dyoung 			    "Missed Packet Count");
   3869   1.1    dyoung 		}
   3870   1.1    dyoung 		if (i < __arraycount(adapter->stats.pxontxc)) {
   3871   1.1    dyoung 			evcnt_attach_dynamic(&adapter->stats.pxontxc[i],
   3872   1.1    dyoung 			    EVCNT_TYPE_MISC, NULL, adapter->queues[i].evnamebuf,
   3873   1.1    dyoung 			    "pxontxc");
   3874   1.1    dyoung 			evcnt_attach_dynamic(&adapter->stats.pxonrxc[i],
   3875   1.1    dyoung 			    EVCNT_TYPE_MISC, NULL, adapter->queues[i].evnamebuf,
   3876   1.1    dyoung 			    "pxonrxc");
   3877   1.1    dyoung 			evcnt_attach_dynamic(&adapter->stats.pxofftxc[i],
   3878   1.1    dyoung 			    EVCNT_TYPE_MISC, NULL, adapter->queues[i].evnamebuf,
   3879   1.1    dyoung 			    "pxofftxc");
   3880   1.1    dyoung 			evcnt_attach_dynamic(&adapter->stats.pxoffrxc[i],
   3881   1.1    dyoung 			    EVCNT_TYPE_MISC, NULL, adapter->queues[i].evnamebuf,
   3882   1.1    dyoung 			    "pxoffrxc");
   3883   1.1    dyoung 			evcnt_attach_dynamic(&adapter->stats.pxon2offc[i],
   3884   1.1    dyoung 			    EVCNT_TYPE_MISC, NULL, adapter->queues[i].evnamebuf,
   3885   1.1    dyoung 			    "pxon2offc");
   3886   1.1    dyoung 		}
   3887   1.1    dyoung 		if (i < __arraycount(adapter->stats.qprc)) {
   3888   1.1    dyoung 			evcnt_attach_dynamic(&adapter->stats.qprc[i],
   3889   1.1    dyoung 			    EVCNT_TYPE_MISC, NULL, adapter->queues[i].evnamebuf,
   3890   1.1    dyoung 			    "qprc");
   3891   1.1    dyoung 			evcnt_attach_dynamic(&adapter->stats.qptc[i],
   3892   1.1    dyoung 			    EVCNT_TYPE_MISC, NULL, adapter->queues[i].evnamebuf,
   3893   1.1    dyoung 			    "qptc");
   3894   1.1    dyoung 			evcnt_attach_dynamic(&adapter->stats.qbrc[i],
   3895   1.1    dyoung 			    EVCNT_TYPE_MISC, NULL, adapter->queues[i].evnamebuf,
   3896   1.1    dyoung 			    "qbrc");
   3897   1.1    dyoung 			evcnt_attach_dynamic(&adapter->stats.qbtc[i],
   3898   1.1    dyoung 			    EVCNT_TYPE_MISC, NULL, adapter->queues[i].evnamebuf,
   3899   1.1    dyoung 			    "qbtc");
   3900   1.1    dyoung 			evcnt_attach_dynamic(&adapter->stats.qprdc[i],
   3901   1.1    dyoung 			    EVCNT_TYPE_MISC, NULL, adapter->queues[i].evnamebuf,
   3902   1.1    dyoung 			    "qprdc");
   3903   1.1    dyoung 		}
   3904   1.1    dyoung 
   3905   1.1    dyoung 		evcnt_attach_dynamic(&rxr->rx_packets, EVCNT_TYPE_MISC,
   3906   1.1    dyoung 		    NULL, adapter->queues[i].evnamebuf, "Queue Packets Received");
   3907   1.1    dyoung 		evcnt_attach_dynamic(&rxr->rx_bytes, EVCNT_TYPE_MISC,
   3908   1.1    dyoung 		    NULL, adapter->queues[i].evnamebuf, "Queue Bytes Received");
   3909  1.26   msaitoh 		evcnt_attach_dynamic(&rxr->rx_copies, EVCNT_TYPE_MISC,
   3910  1.26   msaitoh 		    NULL, adapter->queues[i].evnamebuf, "Copied RX Frames");
   3911   1.1    dyoung 		evcnt_attach_dynamic(&rxr->no_jmbuf, EVCNT_TYPE_MISC,
   3912   1.1    dyoung 		    NULL, adapter->queues[i].evnamebuf, "Rx no jumbo mbuf");
   3913   1.1    dyoung 		evcnt_attach_dynamic(&rxr->rx_discarded, EVCNT_TYPE_MISC,
   3914   1.1    dyoung 		    NULL, adapter->queues[i].evnamebuf, "Rx discarded");
   3915   1.1    dyoung 		evcnt_attach_dynamic(&rxr->rx_irq, EVCNT_TYPE_MISC,
   3916   1.1    dyoung 		    NULL, adapter->queues[i].evnamebuf, "Rx interrupts");
   3917   1.1    dyoung #ifdef LRO
   3918   1.1    dyoung 		SYSCTL_ADD_INT(ctx, queue_list, OID_AUTO, "lro_queued",
   3919   1.1    dyoung 				CTLFLAG_RD, &lro->lro_queued, 0,
   3920   1.1    dyoung 				"LRO Queued");
   3921   1.1    dyoung 		SYSCTL_ADD_INT(ctx, queue_list, OID_AUTO, "lro_flushed",
   3922   1.1    dyoung 				CTLFLAG_RD, &lro->lro_flushed, 0,
   3923   1.1    dyoung 				"LRO Flushed");
   3924   1.1    dyoung #endif /* LRO */
   3925   1.1    dyoung 	}
   3926   1.1    dyoung 
   3927   1.1    dyoung 	/* MAC stats get the own sub node */
   3928   1.1    dyoung 
   3929   1.1    dyoung 
   3930   1.1    dyoung 	snprintf(stats->namebuf,
   3931   1.1    dyoung 	    sizeof(stats->namebuf), "%s MAC Statistics", device_xname(dev));
   3932   1.1    dyoung 
   3933   1.1    dyoung 	evcnt_attach_dynamic(&stats->ipcs, EVCNT_TYPE_MISC, NULL,
   3934   1.1    dyoung 	    stats->namebuf, "rx csum offload - IP");
   3935   1.1    dyoung 	evcnt_attach_dynamic(&stats->l4cs, EVCNT_TYPE_MISC, NULL,
   3936   1.1    dyoung 	    stats->namebuf, "rx csum offload - L4");
   3937   1.1    dyoung 	evcnt_attach_dynamic(&stats->ipcs_bad, EVCNT_TYPE_MISC, NULL,
   3938   1.1    dyoung 	    stats->namebuf, "rx csum offload - IP bad");
   3939   1.1    dyoung 	evcnt_attach_dynamic(&stats->l4cs_bad, EVCNT_TYPE_MISC, NULL,
   3940   1.1    dyoung 	    stats->namebuf, "rx csum offload - L4 bad");
   3941   1.1    dyoung 	evcnt_attach_dynamic(&stats->intzero, EVCNT_TYPE_MISC, NULL,
   3942   1.1    dyoung 	    stats->namebuf, "Interrupt conditions zero");
   3943   1.1    dyoung 	evcnt_attach_dynamic(&stats->legint, EVCNT_TYPE_MISC, NULL,
   3944   1.1    dyoung 	    stats->namebuf, "Legacy interrupts");
   3945   1.1    dyoung 	evcnt_attach_dynamic(&stats->crcerrs, EVCNT_TYPE_MISC, NULL,
   3946   1.1    dyoung 	    stats->namebuf, "CRC Errors");
   3947   1.1    dyoung 	evcnt_attach_dynamic(&stats->illerrc, EVCNT_TYPE_MISC, NULL,
   3948   1.1    dyoung 	    stats->namebuf, "Illegal Byte Errors");
   3949   1.1    dyoung 	evcnt_attach_dynamic(&stats->errbc, EVCNT_TYPE_MISC, NULL,
   3950   1.1    dyoung 	    stats->namebuf, "Byte Errors");
   3951   1.1    dyoung 	evcnt_attach_dynamic(&stats->mspdc, EVCNT_TYPE_MISC, NULL,
   3952   1.1    dyoung 	    stats->namebuf, "MAC Short Packets Discarded");
   3953   1.1    dyoung 	evcnt_attach_dynamic(&stats->mlfc, EVCNT_TYPE_MISC, NULL,
   3954   1.1    dyoung 	    stats->namebuf, "MAC Local Faults");
   3955   1.1    dyoung 	evcnt_attach_dynamic(&stats->mrfc, EVCNT_TYPE_MISC, NULL,
   3956   1.1    dyoung 	    stats->namebuf, "MAC Remote Faults");
   3957   1.1    dyoung 	evcnt_attach_dynamic(&stats->rlec, EVCNT_TYPE_MISC, NULL,
   3958   1.1    dyoung 	    stats->namebuf, "Receive Length Errors");
   3959   1.1    dyoung 	evcnt_attach_dynamic(&stats->lxontxc, EVCNT_TYPE_MISC, NULL,
   3960   1.1    dyoung 	    stats->namebuf, "Link XON Transmitted");
   3961   1.1    dyoung 	evcnt_attach_dynamic(&stats->lxonrxc, EVCNT_TYPE_MISC, NULL,
   3962   1.1    dyoung 	    stats->namebuf, "Link XON Received");
   3963   1.1    dyoung 	evcnt_attach_dynamic(&stats->lxofftxc, EVCNT_TYPE_MISC, NULL,
   3964   1.1    dyoung 	    stats->namebuf, "Link XOFF Transmitted");
   3965   1.1    dyoung 	evcnt_attach_dynamic(&stats->lxoffrxc, EVCNT_TYPE_MISC, NULL,
   3966   1.1    dyoung 	    stats->namebuf, "Link XOFF Received");
   3967   1.1    dyoung 
   3968   1.1    dyoung 	/* Packet Reception Stats */
   3969   1.1    dyoung 	evcnt_attach_dynamic(&stats->tor, EVCNT_TYPE_MISC, NULL,
   3970   1.1    dyoung 	    stats->namebuf, "Total Octets Received");
   3971   1.1    dyoung 	evcnt_attach_dynamic(&stats->gorc, EVCNT_TYPE_MISC, NULL,
   3972   1.1    dyoung 	    stats->namebuf, "Good Octets Received");
   3973   1.1    dyoung 	evcnt_attach_dynamic(&stats->tpr, EVCNT_TYPE_MISC, NULL,
   3974   1.1    dyoung 	    stats->namebuf, "Total Packets Received");
   3975   1.1    dyoung 	evcnt_attach_dynamic(&stats->gprc, EVCNT_TYPE_MISC, NULL,
   3976   1.1    dyoung 	    stats->namebuf, "Good Packets Received");
   3977   1.1    dyoung 	evcnt_attach_dynamic(&stats->mprc, EVCNT_TYPE_MISC, NULL,
   3978   1.1    dyoung 	    stats->namebuf, "Multicast Packets Received");
   3979   1.1    dyoung 	evcnt_attach_dynamic(&stats->bprc, EVCNT_TYPE_MISC, NULL,
   3980   1.1    dyoung 	    stats->namebuf, "Broadcast Packets Received");
   3981   1.1    dyoung 	evcnt_attach_dynamic(&stats->prc64, EVCNT_TYPE_MISC, NULL,
   3982   1.1    dyoung 	    stats->namebuf, "64 byte frames received ");
   3983   1.1    dyoung 	evcnt_attach_dynamic(&stats->prc127, EVCNT_TYPE_MISC, NULL,
   3984   1.1    dyoung 	    stats->namebuf, "65-127 byte frames received");
   3985   1.1    dyoung 	evcnt_attach_dynamic(&stats->prc255, EVCNT_TYPE_MISC, NULL,
   3986   1.1    dyoung 	    stats->namebuf, "128-255 byte frames received");
   3987   1.1    dyoung 	evcnt_attach_dynamic(&stats->prc511, EVCNT_TYPE_MISC, NULL,
   3988   1.1    dyoung 	    stats->namebuf, "256-511 byte frames received");
   3989   1.1    dyoung 	evcnt_attach_dynamic(&stats->prc1023, EVCNT_TYPE_MISC, NULL,
   3990   1.1    dyoung 	    stats->namebuf, "512-1023 byte frames received");
   3991   1.1    dyoung 	evcnt_attach_dynamic(&stats->prc1522, EVCNT_TYPE_MISC, NULL,
   3992   1.1    dyoung 	    stats->namebuf, "1023-1522 byte frames received");
   3993   1.1    dyoung 	evcnt_attach_dynamic(&stats->ruc, EVCNT_TYPE_MISC, NULL,
   3994   1.1    dyoung 	    stats->namebuf, "Receive Undersized");
   3995   1.1    dyoung 	evcnt_attach_dynamic(&stats->rfc, EVCNT_TYPE_MISC, NULL,
   3996   1.1    dyoung 	    stats->namebuf, "Fragmented Packets Received ");
   3997   1.1    dyoung 	evcnt_attach_dynamic(&stats->roc, EVCNT_TYPE_MISC, NULL,
   3998   1.1    dyoung 	    stats->namebuf, "Oversized Packets Received");
   3999   1.1    dyoung 	evcnt_attach_dynamic(&stats->rjc, EVCNT_TYPE_MISC, NULL,
   4000   1.1    dyoung 	    stats->namebuf, "Received Jabber");
   4001   1.1    dyoung 	evcnt_attach_dynamic(&stats->mngprc, EVCNT_TYPE_MISC, NULL,
   4002   1.1    dyoung 	    stats->namebuf, "Management Packets Received");
   4003   1.1    dyoung 	evcnt_attach_dynamic(&stats->xec, EVCNT_TYPE_MISC, NULL,
   4004   1.1    dyoung 	    stats->namebuf, "Checksum Errors");
   4005   1.1    dyoung 
   4006   1.1    dyoung 	/* Packet Transmission Stats */
   4007   1.1    dyoung 	evcnt_attach_dynamic(&stats->gotc, EVCNT_TYPE_MISC, NULL,
   4008   1.1    dyoung 	    stats->namebuf, "Good Octets Transmitted");
   4009   1.1    dyoung 	evcnt_attach_dynamic(&stats->tpt, EVCNT_TYPE_MISC, NULL,
   4010   1.1    dyoung 	    stats->namebuf, "Total Packets Transmitted");
   4011   1.1    dyoung 	evcnt_attach_dynamic(&stats->gptc, EVCNT_TYPE_MISC, NULL,
   4012   1.1    dyoung 	    stats->namebuf, "Good Packets Transmitted");
   4013   1.1    dyoung 	evcnt_attach_dynamic(&stats->bptc, EVCNT_TYPE_MISC, NULL,
   4014   1.1    dyoung 	    stats->namebuf, "Broadcast Packets Transmitted");
   4015   1.1    dyoung 	evcnt_attach_dynamic(&stats->mptc, EVCNT_TYPE_MISC, NULL,
   4016   1.1    dyoung 	    stats->namebuf, "Multicast Packets Transmitted");
   4017   1.1    dyoung 	evcnt_attach_dynamic(&stats->mngptc, EVCNT_TYPE_MISC, NULL,
   4018   1.1    dyoung 	    stats->namebuf, "Management Packets Transmitted");
   4019   1.1    dyoung 	evcnt_attach_dynamic(&stats->ptc64, EVCNT_TYPE_MISC, NULL,
   4020   1.1    dyoung 	    stats->namebuf, "64 byte frames transmitted ");
   4021   1.1    dyoung 	evcnt_attach_dynamic(&stats->ptc127, EVCNT_TYPE_MISC, NULL,
   4022   1.1    dyoung 	    stats->namebuf, "65-127 byte frames transmitted");
   4023   1.1    dyoung 	evcnt_attach_dynamic(&stats->ptc255, EVCNT_TYPE_MISC, NULL,
   4024   1.1    dyoung 	    stats->namebuf, "128-255 byte frames transmitted");
   4025   1.1    dyoung 	evcnt_attach_dynamic(&stats->ptc511, EVCNT_TYPE_MISC, NULL,
   4026   1.1    dyoung 	    stats->namebuf, "256-511 byte frames transmitted");
   4027   1.1    dyoung 	evcnt_attach_dynamic(&stats->ptc1023, EVCNT_TYPE_MISC, NULL,
   4028   1.1    dyoung 	    stats->namebuf, "512-1023 byte frames transmitted");
   4029   1.1    dyoung 	evcnt_attach_dynamic(&stats->ptc1522, EVCNT_TYPE_MISC, NULL,
   4030   1.1    dyoung 	    stats->namebuf, "1024-1522 byte frames transmitted");
   4031   1.1    dyoung }
   4032   1.1    dyoung 
   4033   1.1    dyoung /*
   4034   1.1    dyoung ** Set flow control using sysctl:
   4035   1.1    dyoung ** Flow control values:
   4036   1.1    dyoung ** 	0 - off
   4037   1.1    dyoung **	1 - rx pause
   4038   1.1    dyoung **	2 - tx pause
   4039   1.1    dyoung **	3 - full
   4040   1.1    dyoung */
   4041   1.1    dyoung static int
   4042   1.1    dyoung ixgbe_set_flowcntl(SYSCTLFN_ARGS)
   4043   1.1    dyoung {
   4044   1.1    dyoung 	struct sysctlnode node;
   4045  1.24   msaitoh 	int error, last;
   4046   1.1    dyoung 	struct adapter *adapter;
   4047   1.1    dyoung 
   4048   1.1    dyoung 	node = *rnode;
   4049   1.1    dyoung 	adapter = (struct adapter *)node.sysctl_data;
   4050  1.24   msaitoh 	node.sysctl_data = &adapter->fc;
   4051  1.24   msaitoh 	last = adapter->fc;
   4052   1.1    dyoung 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   4053   1.1    dyoung 	if (error != 0 || newp == NULL)
   4054   1.1    dyoung 		return error;
   4055   1.1    dyoung 
   4056   1.1    dyoung 	/* Don't bother if it's not changed */
   4057  1.24   msaitoh 	if (adapter->fc == last)
   4058   1.1    dyoung 		return (0);
   4059   1.1    dyoung 
   4060  1.24   msaitoh 	switch (adapter->fc) {
   4061   1.1    dyoung 		case ixgbe_fc_rx_pause:
   4062   1.1    dyoung 		case ixgbe_fc_tx_pause:
   4063   1.1    dyoung 		case ixgbe_fc_full:
   4064  1.24   msaitoh 			adapter->hw.fc.requested_mode = adapter->fc;
   4065  1.26   msaitoh 			if (adapter->num_queues > 1)
   4066  1.26   msaitoh 				ixgbe_disable_rx_drop(adapter);
   4067   1.1    dyoung 			break;
   4068   1.1    dyoung 		case ixgbe_fc_none:
   4069   1.1    dyoung 			adapter->hw.fc.requested_mode = ixgbe_fc_none;
   4070  1.26   msaitoh 			if (adapter->num_queues > 1)
   4071  1.26   msaitoh 				ixgbe_enable_rx_drop(adapter);
   4072  1.28   msaitoh 			break;
   4073  1.28   msaitoh 		default:
   4074  1.28   msaitoh 			adapter->fc = last;
   4075  1.28   msaitoh 			return (EINVAL);
   4076   1.1    dyoung 	}
   4077  1.25   msaitoh 	/* Don't autoneg if forcing a value */
   4078  1.25   msaitoh 	adapter->hw.fc.disable_fc_autoneg = TRUE;
   4079  1.25   msaitoh 	ixgbe_fc_enable(&adapter->hw);
   4080   1.1    dyoung 	return 0;
   4081   1.1    dyoung }
   4082   1.1    dyoung 
   4083  1.33   msaitoh 
   4084   1.1    dyoung /*
   4085   1.1    dyoung ** Control link advertise speed:
   4086   1.1    dyoung **	1 - advertise only 1G
   4087  1.24   msaitoh **	2 - advertise 100Mb
   4088  1.25   msaitoh **	3 - advertise normal
   4089   1.1    dyoung */
   4090   1.1    dyoung static int
   4091   1.1    dyoung ixgbe_set_advertise(SYSCTLFN_ARGS)
   4092   1.1    dyoung {
   4093   1.1    dyoung 	struct sysctlnode	node;
   4094  1.22   msaitoh 	int			t, error = 0;
   4095   1.1    dyoung 	struct adapter		*adapter;
   4096  1.24   msaitoh 	device_t		dev;
   4097   1.1    dyoung 	struct ixgbe_hw		*hw;
   4098   1.1    dyoung 	ixgbe_link_speed	speed, last;
   4099   1.1    dyoung 
   4100   1.1    dyoung 	node = *rnode;
   4101   1.1    dyoung 	adapter = (struct adapter *)node.sysctl_data;
   4102  1.24   msaitoh 	dev = adapter->dev;
   4103  1.25   msaitoh 	hw = &adapter->hw;
   4104  1.25   msaitoh 	last = adapter->advertise;
   4105   1.1    dyoung 	t = adapter->advertise;
   4106   1.1    dyoung 	node.sysctl_data = &t;
   4107   1.1    dyoung 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   4108   1.1    dyoung 	if (error != 0 || newp == NULL)
   4109   1.1    dyoung 		return error;
   4110   1.1    dyoung 
   4111  1.25   msaitoh 	if (adapter->advertise == last) /* no change */
   4112  1.25   msaitoh 		return (0);
   4113  1.25   msaitoh 
   4114   1.1    dyoung 	if (t == -1)
   4115   1.1    dyoung 		return 0;
   4116   1.1    dyoung 
   4117   1.1    dyoung 	adapter->advertise = t;
   4118   1.1    dyoung 
   4119   1.1    dyoung 	if (!((hw->phy.media_type == ixgbe_media_type_copper) ||
   4120   1.1    dyoung             (hw->phy.multispeed_fiber)))
   4121  1.28   msaitoh 		return (EINVAL);
   4122   1.1    dyoung 
   4123  1.24   msaitoh 	if ((adapter->advertise == 2) && (hw->mac.type != ixgbe_mac_X540)) {
   4124  1.24   msaitoh 		device_printf(dev, "Set Advertise: 100Mb on X540 only\n");
   4125  1.28   msaitoh 		return (EINVAL);
   4126  1.24   msaitoh 	}
   4127  1.24   msaitoh 
   4128   1.1    dyoung 	if (adapter->advertise == 1)
   4129   1.1    dyoung                 speed = IXGBE_LINK_SPEED_1GB_FULL;
   4130  1.24   msaitoh 	else if (adapter->advertise == 2)
   4131  1.24   msaitoh                 speed = IXGBE_LINK_SPEED_100_FULL;
   4132  1.25   msaitoh 	else if (adapter->advertise == 3)
   4133   1.1    dyoung                 speed = IXGBE_LINK_SPEED_1GB_FULL |
   4134   1.1    dyoung 			IXGBE_LINK_SPEED_10GB_FULL;
   4135  1.35   msaitoh 	else {	/* bogus value */
   4136  1.28   msaitoh 		adapter->advertise = last;
   4137  1.28   msaitoh 		return (EINVAL);
   4138  1.28   msaitoh 	}
   4139   1.1    dyoung 
   4140   1.1    dyoung 	hw->mac.autotry_restart = TRUE;
   4141  1.28   msaitoh 	hw->mac.ops.setup_link(hw, speed, TRUE);
   4142   1.1    dyoung 
   4143   1.1    dyoung 	return 0;
   4144   1.1    dyoung }
   4145  1.24   msaitoh 
   4146  1.24   msaitoh /*
   4147  1.24   msaitoh ** Thermal Shutdown Trigger
   4148  1.24   msaitoh **   - cause a Thermal Overtemp IRQ
   4149  1.33   msaitoh **   - this now requires firmware enabling
   4150  1.24   msaitoh */
   4151  1.24   msaitoh static int
   4152  1.24   msaitoh ixgbe_set_thermal_test(SYSCTLFN_ARGS)
   4153  1.24   msaitoh {
   4154  1.24   msaitoh 	struct sysctlnode node;
   4155  1.24   msaitoh 	int		error, fire = 0;
   4156  1.24   msaitoh 	struct adapter	*adapter;
   4157  1.24   msaitoh 	struct ixgbe_hw *hw;
   4158  1.24   msaitoh 
   4159  1.24   msaitoh 	node = *rnode;
   4160  1.24   msaitoh 	adapter = (struct adapter *)node.sysctl_data;
   4161  1.24   msaitoh 	hw = &adapter->hw;
   4162  1.24   msaitoh 
   4163  1.24   msaitoh 	if (hw->mac.type != ixgbe_mac_X540)
   4164  1.24   msaitoh 		return (0);
   4165  1.24   msaitoh 
   4166  1.24   msaitoh 	node.sysctl_data = &fire;
   4167  1.24   msaitoh 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   4168  1.24   msaitoh 	if ((error) || (newp == NULL))
   4169  1.24   msaitoh 		return (error);
   4170  1.24   msaitoh 
   4171  1.24   msaitoh 	if (fire) {
   4172  1.24   msaitoh 		u32 reg = IXGBE_READ_REG(hw, IXGBE_EICS);
   4173  1.24   msaitoh 		reg |= IXGBE_EICR_TS;
   4174  1.24   msaitoh 		IXGBE_WRITE_REG(hw, IXGBE_EICS, reg);
   4175  1.24   msaitoh 	}
   4176  1.24   msaitoh 
   4177  1.24   msaitoh 	return (0);
   4178  1.24   msaitoh }
   4179  1.26   msaitoh 
   4180  1.26   msaitoh /*
   4181  1.26   msaitoh ** Enable the hardware to drop packets when the buffer is
   4182  1.26   msaitoh ** full. This is useful when multiqueue,so that no single
   4183  1.26   msaitoh ** queue being full stalls the entire RX engine. We only
   4184  1.26   msaitoh ** enable this when Multiqueue AND when Flow Control is
   4185  1.26   msaitoh ** disabled.
   4186  1.26   msaitoh */
   4187  1.26   msaitoh static void
   4188  1.26   msaitoh ixgbe_enable_rx_drop(struct adapter *adapter)
   4189  1.26   msaitoh {
   4190  1.26   msaitoh         struct ixgbe_hw *hw = &adapter->hw;
   4191  1.26   msaitoh 
   4192  1.26   msaitoh 	for (int i = 0; i < adapter->num_queues; i++) {
   4193  1.26   msaitoh         	u32 srrctl = IXGBE_READ_REG(hw, IXGBE_SRRCTL(i));
   4194  1.26   msaitoh         	srrctl |= IXGBE_SRRCTL_DROP_EN;
   4195  1.26   msaitoh         	IXGBE_WRITE_REG(hw, IXGBE_SRRCTL(i), srrctl);
   4196  1.26   msaitoh 	}
   4197  1.26   msaitoh }
   4198  1.26   msaitoh 
   4199  1.26   msaitoh static void
   4200  1.26   msaitoh ixgbe_disable_rx_drop(struct adapter *adapter)
   4201  1.26   msaitoh {
   4202  1.26   msaitoh         struct ixgbe_hw *hw = &adapter->hw;
   4203  1.26   msaitoh 
   4204  1.26   msaitoh 	for (int i = 0; i < adapter->num_queues; i++) {
   4205  1.26   msaitoh         	u32 srrctl = IXGBE_READ_REG(hw, IXGBE_SRRCTL(i));
   4206  1.26   msaitoh         	srrctl &= ~IXGBE_SRRCTL_DROP_EN;
   4207  1.26   msaitoh         	IXGBE_WRITE_REG(hw, IXGBE_SRRCTL(i), srrctl);
   4208  1.26   msaitoh 	}
   4209  1.26   msaitoh }
   4210