Home | History | Annotate | Line # | Download | only in ixgbe
ixgbe.c revision 1.37
      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.37     ozaki /*$NetBSD: ixgbe.c,v 1.37 2016/02/09 08:32:11 ozaki-r 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.35   msaitoh #ifdef IXGBE_LEGACY_TX
    140  1.28   msaitoh static void     ixgbe_start(struct ifnet *);
    141  1.28   msaitoh static void     ixgbe_start_locked(struct tx_ring *, struct ifnet *);
    142  1.35   msaitoh #else /* ! IXGBE_LEGACY_TX */
    143   1.1    dyoung static int	ixgbe_mq_start(struct ifnet *, struct mbuf *);
    144  1.33   msaitoh static int	ixgbe_mq_start_locked(struct ifnet *, struct tx_ring *);
    145   1.1    dyoung static void	ixgbe_qflush(struct ifnet *);
    146  1.35   msaitoh static void	ixgbe_deferred_mq_start(void *, int);
    147  1.35   msaitoh #endif /* IXGBE_LEGACY_TX */
    148   1.1    dyoung static int      ixgbe_ioctl(struct ifnet *, u_long, void *);
    149   1.1    dyoung static void	ixgbe_ifstop(struct ifnet *, int);
    150   1.1    dyoung static int	ixgbe_init(struct ifnet *);
    151   1.1    dyoung static void	ixgbe_init_locked(struct adapter *);
    152   1.1    dyoung static void     ixgbe_stop(void *);
    153   1.1    dyoung static void     ixgbe_media_status(struct ifnet *, struct ifmediareq *);
    154   1.1    dyoung static int      ixgbe_media_change(struct ifnet *);
    155   1.1    dyoung static void     ixgbe_identify_hardware(struct adapter *);
    156   1.1    dyoung static int      ixgbe_allocate_pci_resources(struct adapter *,
    157   1.1    dyoung 		    const struct pci_attach_args *);
    158  1.33   msaitoh static void	ixgbe_get_slot_info(struct ixgbe_hw *);
    159   1.1    dyoung static int      ixgbe_allocate_msix(struct adapter *,
    160   1.1    dyoung 		    const struct pci_attach_args *);
    161   1.1    dyoung static int      ixgbe_allocate_legacy(struct adapter *,
    162   1.1    dyoung 		    const struct pci_attach_args *);
    163   1.1    dyoung static int	ixgbe_allocate_queues(struct adapter *);
    164   1.1    dyoung static int	ixgbe_setup_msix(struct adapter *);
    165   1.1    dyoung static void	ixgbe_free_pci_resources(struct adapter *);
    166   1.1    dyoung static void	ixgbe_local_timer(void *);
    167   1.1    dyoung static int	ixgbe_setup_interface(device_t, struct adapter *);
    168   1.1    dyoung static void	ixgbe_config_link(struct adapter *);
    169   1.1    dyoung 
    170   1.1    dyoung static int      ixgbe_allocate_transmit_buffers(struct tx_ring *);
    171   1.1    dyoung static int	ixgbe_setup_transmit_structures(struct adapter *);
    172   1.1    dyoung static void	ixgbe_setup_transmit_ring(struct tx_ring *);
    173   1.1    dyoung static void     ixgbe_initialize_transmit_units(struct adapter *);
    174   1.1    dyoung static void     ixgbe_free_transmit_structures(struct adapter *);
    175   1.1    dyoung static void     ixgbe_free_transmit_buffers(struct tx_ring *);
    176   1.1    dyoung 
    177   1.1    dyoung static int      ixgbe_allocate_receive_buffers(struct rx_ring *);
    178   1.1    dyoung static int      ixgbe_setup_receive_structures(struct adapter *);
    179   1.1    dyoung static int	ixgbe_setup_receive_ring(struct rx_ring *);
    180   1.1    dyoung static void     ixgbe_initialize_receive_units(struct adapter *);
    181   1.1    dyoung static void     ixgbe_free_receive_structures(struct adapter *);
    182   1.1    dyoung static void     ixgbe_free_receive_buffers(struct rx_ring *);
    183   1.1    dyoung static void	ixgbe_setup_hw_rsc(struct rx_ring *);
    184   1.1    dyoung 
    185   1.1    dyoung static void     ixgbe_enable_intr(struct adapter *);
    186   1.1    dyoung static void     ixgbe_disable_intr(struct adapter *);
    187   1.1    dyoung static void     ixgbe_update_stats_counters(struct adapter *);
    188  1.33   msaitoh static void	ixgbe_txeof(struct tx_ring *);
    189  1.28   msaitoh static bool	ixgbe_rxeof(struct ix_queue *);
    190   1.1    dyoung static void	ixgbe_rx_checksum(u32, struct mbuf *, u32,
    191   1.1    dyoung 		    struct ixgbe_hw_stats *);
    192   1.1    dyoung static void     ixgbe_set_promisc(struct adapter *);
    193   1.1    dyoung static void     ixgbe_set_multi(struct adapter *);
    194   1.1    dyoung static void     ixgbe_update_link_status(struct adapter *);
    195   1.1    dyoung static void	ixgbe_refresh_mbufs(struct rx_ring *, int);
    196   1.1    dyoung static int      ixgbe_xmit(struct tx_ring *, struct mbuf *);
    197   1.1    dyoung static int	ixgbe_set_flowcntl(SYSCTLFN_PROTO);
    198   1.1    dyoung static int	ixgbe_set_advertise(SYSCTLFN_PROTO);
    199  1.24   msaitoh static int	ixgbe_set_thermal_test(SYSCTLFN_PROTO);
    200   1.1    dyoung static int	ixgbe_dma_malloc(struct adapter *, bus_size_t,
    201   1.1    dyoung 		    struct ixgbe_dma_alloc *, int);
    202   1.1    dyoung static void     ixgbe_dma_free(struct adapter *, struct ixgbe_dma_alloc *);
    203  1.28   msaitoh static int	ixgbe_tx_ctx_setup(struct tx_ring *,
    204  1.28   msaitoh 		    struct mbuf *, u32 *, u32 *);
    205  1.28   msaitoh static int	ixgbe_tso_setup(struct tx_ring *,
    206  1.28   msaitoh 		    struct mbuf *, u32 *, u32 *);
    207   1.1    dyoung static void	ixgbe_set_ivar(struct adapter *, u8, u8, s8);
    208   1.1    dyoung static void	ixgbe_configure_ivars(struct adapter *);
    209   1.1    dyoung static u8 *	ixgbe_mc_array_itr(struct ixgbe_hw *, u8 **, u32 *);
    210   1.1    dyoung 
    211   1.1    dyoung static void	ixgbe_setup_vlan_hw_support(struct adapter *);
    212   1.1    dyoung #if 0
    213   1.1    dyoung static void	ixgbe_register_vlan(void *, struct ifnet *, u16);
    214   1.1    dyoung static void	ixgbe_unregister_vlan(void *, struct ifnet *, u16);
    215   1.1    dyoung #endif
    216   1.1    dyoung 
    217   1.1    dyoung static void     ixgbe_add_hw_stats(struct adapter *adapter);
    218   1.1    dyoung 
    219   1.1    dyoung static __inline void ixgbe_rx_discard(struct rx_ring *, int);
    220   1.1    dyoung static __inline void ixgbe_rx_input(struct rx_ring *, struct ifnet *,
    221   1.1    dyoung 		    struct mbuf *, u32);
    222   1.1    dyoung 
    223  1.26   msaitoh static void	ixgbe_enable_rx_drop(struct adapter *);
    224  1.26   msaitoh static void	ixgbe_disable_rx_drop(struct adapter *);
    225  1.26   msaitoh 
    226   1.1    dyoung /* Support for pluggable optic modules */
    227   1.1    dyoung static bool	ixgbe_sfp_probe(struct adapter *);
    228   1.1    dyoung static void	ixgbe_setup_optics(struct adapter *);
    229   1.1    dyoung 
    230   1.1    dyoung /* Legacy (single vector interrupt handler */
    231   1.1    dyoung static int	ixgbe_legacy_irq(void *);
    232   1.1    dyoung 
    233   1.1    dyoung #if defined(NETBSD_MSI_OR_MSIX)
    234   1.1    dyoung /* The MSI/X Interrupt handlers */
    235  1.34   msaitoh static int	ixgbe_msix_que(void *);
    236  1.34   msaitoh static int	ixgbe_msix_link(void *);
    237   1.1    dyoung #endif
    238   1.1    dyoung 
    239   1.1    dyoung /* Software interrupts for deferred work */
    240   1.1    dyoung static void	ixgbe_handle_que(void *);
    241   1.1    dyoung static void	ixgbe_handle_link(void *);
    242   1.1    dyoung static void	ixgbe_handle_msf(void *);
    243   1.1    dyoung static void	ixgbe_handle_mod(void *);
    244   1.1    dyoung 
    245   1.1    dyoung const struct sysctlnode *ixgbe_sysctl_instance(struct adapter *);
    246   1.1    dyoung static ixgbe_vendor_info_t *ixgbe_lookup(const struct pci_attach_args *);
    247   1.1    dyoung 
    248   1.1    dyoung #ifdef IXGBE_FDIR
    249   1.1    dyoung static void	ixgbe_atr(struct tx_ring *, struct mbuf *);
    250   1.1    dyoung static void	ixgbe_reinit_fdir(void *, int);
    251   1.1    dyoung #endif
    252   1.1    dyoung 
    253  1.33   msaitoh /* Missing shared code prototype */
    254  1.33   msaitoh extern void ixgbe_stop_mac_link_on_d3_82599(struct ixgbe_hw *hw);
    255  1.33   msaitoh 
    256   1.1    dyoung /*********************************************************************
    257   1.1    dyoung  *  FreeBSD Device Interface Entry Points
    258   1.1    dyoung  *********************************************************************/
    259   1.1    dyoung 
    260   1.1    dyoung CFATTACH_DECL3_NEW(ixg, sizeof(struct adapter),
    261   1.1    dyoung     ixgbe_probe, ixgbe_attach, ixgbe_detach, NULL, NULL, NULL,
    262   1.1    dyoung     DVF_DETACH_SHUTDOWN);
    263   1.1    dyoung 
    264   1.1    dyoung #if 0
    265   1.1    dyoung devclass_t ixgbe_devclass;
    266   1.1    dyoung DRIVER_MODULE(ixgbe, pci, ixgbe_driver, ixgbe_devclass, 0, 0);
    267   1.1    dyoung 
    268   1.1    dyoung MODULE_DEPEND(ixgbe, pci, 1, 1, 1);
    269   1.1    dyoung MODULE_DEPEND(ixgbe, ether, 1, 1, 1);
    270   1.1    dyoung #endif
    271   1.1    dyoung 
    272   1.1    dyoung /*
    273   1.1    dyoung ** TUNEABLE PARAMETERS:
    274   1.1    dyoung */
    275   1.1    dyoung 
    276   1.1    dyoung /*
    277   1.1    dyoung ** AIM: Adaptive Interrupt Moderation
    278   1.1    dyoung ** which means that the interrupt rate
    279   1.1    dyoung ** is varied over time based on the
    280   1.1    dyoung ** traffic for that interrupt vector
    281   1.1    dyoung */
    282   1.1    dyoung static int ixgbe_enable_aim = TRUE;
    283  1.33   msaitoh #define SYSCTL_INT(__x, __y)
    284  1.33   msaitoh SYSCTL_INT("hw.ixgbe.enable_aim", &ixgbe_enable_aim);
    285   1.1    dyoung 
    286  1.22   msaitoh static int ixgbe_max_interrupt_rate = (4000000 / IXGBE_LOW_LATENCY);
    287  1.33   msaitoh SYSCTL_INT("hw.ixgbe.max_interrupt_rate", &ixgbe_max_interrupt_rate);
    288   1.1    dyoung 
    289   1.1    dyoung /* How many packets rxeof tries to clean at a time */
    290   1.1    dyoung static int ixgbe_rx_process_limit = 256;
    291  1.33   msaitoh SYSCTL_INT("hw.ixgbe.rx_process_limit", &ixgbe_rx_process_limit);
    292   1.1    dyoung 
    293  1.28   msaitoh /* How many packets txeof tries to clean at a time */
    294  1.28   msaitoh static int ixgbe_tx_process_limit = 256;
    295  1.33   msaitoh SYSCTL_INT("hw.ixgbe.tx_process_limit", &ixgbe_tx_process_limit);
    296  1.28   msaitoh 
    297   1.1    dyoung /*
    298   1.1    dyoung ** Smart speed setting, default to on
    299   1.1    dyoung ** this only works as a compile option
    300   1.1    dyoung ** right now as its during attach, set
    301   1.1    dyoung ** this to 'ixgbe_smart_speed_off' to
    302   1.1    dyoung ** disable.
    303   1.1    dyoung */
    304   1.1    dyoung static int ixgbe_smart_speed = ixgbe_smart_speed_on;
    305   1.1    dyoung 
    306   1.1    dyoung /*
    307   1.1    dyoung  * MSIX should be the default for best performance,
    308   1.1    dyoung  * but this allows it to be forced off for testing.
    309   1.1    dyoung  */
    310   1.1    dyoung static int ixgbe_enable_msix = 1;
    311  1.33   msaitoh SYSCTL_INT("hw.ixgbe.enable_msix", &ixgbe_enable_msix);
    312   1.1    dyoung 
    313   1.1    dyoung #if defined(NETBSD_MSI_OR_MSIX)
    314   1.1    dyoung /*
    315   1.1    dyoung  * Number of Queues, can be set to 0,
    316   1.1    dyoung  * it then autoconfigures based on the
    317   1.1    dyoung  * number of cpus with a max of 8. This
    318   1.1    dyoung  * can be overriden manually here.
    319   1.1    dyoung  */
    320  1.34   msaitoh static int ixgbe_num_queues = 1;
    321  1.33   msaitoh SYSCTL_INT("hw.ixgbe.num_queues", &ixgbe_num_queues);
    322   1.1    dyoung #endif
    323   1.1    dyoung 
    324   1.1    dyoung /*
    325   1.1    dyoung ** Number of TX descriptors per ring,
    326   1.1    dyoung ** setting higher than RX as this seems
    327   1.1    dyoung ** the better performing choice.
    328   1.1    dyoung */
    329   1.1    dyoung static int ixgbe_txd = PERFORM_TXD;
    330  1.33   msaitoh SYSCTL_INT("hw.ixgbe.txd", &ixgbe_txd);
    331   1.1    dyoung 
    332   1.1    dyoung /* Number of RX descriptors per ring */
    333   1.1    dyoung static int ixgbe_rxd = PERFORM_RXD;
    334  1.33   msaitoh SYSCTL_INT("hw.ixgbe.rxd", &ixgbe_rxd);
    335  1.33   msaitoh 
    336  1.33   msaitoh /*
    337  1.33   msaitoh ** Defining this on will allow the use
    338  1.33   msaitoh ** of unsupported SFP+ modules, note that
    339  1.33   msaitoh ** doing so you are on your own :)
    340  1.33   msaitoh */
    341  1.35   msaitoh static int allow_unsupported_sfp = false;
    342  1.33   msaitoh SYSCTL_INT("hw.ix.unsupported_sfp", &allow_unsupported_sfp);
    343   1.1    dyoung 
    344  1.26   msaitoh /*
    345  1.26   msaitoh ** HW RSC control:
    346  1.26   msaitoh **  this feature only works with
    347  1.26   msaitoh **  IPv4, and only on 82599 and later.
    348  1.26   msaitoh **  Also this will cause IP forwarding to
    349  1.26   msaitoh **  fail and that can't be controlled by
    350  1.26   msaitoh **  the stack as LRO can. For all these
    351  1.26   msaitoh **  reasons I've deemed it best to leave
    352  1.26   msaitoh **  this off and not bother with a tuneable
    353  1.26   msaitoh **  interface, this would need to be compiled
    354  1.26   msaitoh **  to enable.
    355  1.26   msaitoh */
    356  1.26   msaitoh static bool ixgbe_rsc_enable = FALSE;
    357  1.26   msaitoh 
    358   1.1    dyoung /* Keep running tab on them for sanity check */
    359   1.1    dyoung static int ixgbe_total_ports;
    360   1.1    dyoung 
    361   1.1    dyoung #ifdef IXGBE_FDIR
    362   1.1    dyoung /*
    363   1.1    dyoung ** For Flow Director: this is the
    364   1.1    dyoung ** number of TX packets we sample
    365   1.1    dyoung ** for the filter pool, this means
    366   1.1    dyoung ** every 20th packet will be probed.
    367   1.1    dyoung **
    368   1.1    dyoung ** This feature can be disabled by
    369   1.1    dyoung ** setting this to 0.
    370   1.1    dyoung */
    371   1.1    dyoung static int atr_sample_rate = 20;
    372   1.1    dyoung /*
    373   1.1    dyoung ** Flow Director actually 'steals'
    374   1.1    dyoung ** part of the packet buffer as its
    375   1.1    dyoung ** filter pool, this variable controls
    376   1.1    dyoung ** how much it uses:
    377   1.1    dyoung **  0 = 64K, 1 = 128K, 2 = 256K
    378   1.1    dyoung */
    379   1.1    dyoung static int fdir_pballoc = 1;
    380   1.1    dyoung #endif
    381   1.1    dyoung 
    382  1.22   msaitoh #ifdef DEV_NETMAP
    383  1.22   msaitoh /*
    384  1.22   msaitoh  * The #ifdef DEV_NETMAP / #endif blocks in this file are meant to
    385  1.22   msaitoh  * be a reference on how to implement netmap support in a driver.
    386  1.22   msaitoh  * Additional comments are in ixgbe_netmap.h .
    387  1.22   msaitoh  *
    388  1.25   msaitoh  * <dev/netmap/ixgbe_netmap.h> contains functions for netmap support
    389  1.22   msaitoh  * that extend the standard driver.
    390  1.22   msaitoh  */
    391  1.22   msaitoh #include <dev/netmap/ixgbe_netmap.h>
    392  1.22   msaitoh #endif /* DEV_NETMAP */
    393  1.22   msaitoh 
    394   1.1    dyoung /*********************************************************************
    395   1.1    dyoung  *  Device identification routine
    396   1.1    dyoung  *
    397   1.1    dyoung  *  ixgbe_probe determines if the driver should be loaded on
    398   1.1    dyoung  *  adapter based on PCI vendor/device id of the adapter.
    399   1.1    dyoung  *
    400   1.1    dyoung  *  return 1 on success, 0 on failure
    401   1.1    dyoung  *********************************************************************/
    402   1.1    dyoung 
    403   1.1    dyoung static int
    404   1.1    dyoung ixgbe_probe(device_t dev, cfdata_t cf, void *aux)
    405   1.1    dyoung {
    406   1.1    dyoung 	const struct pci_attach_args *pa = aux;
    407   1.1    dyoung 
    408   1.1    dyoung 	return (ixgbe_lookup(pa) != NULL) ? 1 : 0;
    409   1.1    dyoung }
    410   1.1    dyoung 
    411   1.1    dyoung static ixgbe_vendor_info_t *
    412   1.1    dyoung ixgbe_lookup(const struct pci_attach_args *pa)
    413   1.1    dyoung {
    414   1.1    dyoung 	pcireg_t subid;
    415   1.1    dyoung 	ixgbe_vendor_info_t *ent;
    416   1.1    dyoung 
    417   1.1    dyoung 	INIT_DEBUGOUT("ixgbe_probe: begin");
    418   1.1    dyoung 
    419   1.1    dyoung 	if (PCI_VENDOR(pa->pa_id) != IXGBE_INTEL_VENDOR_ID)
    420   1.1    dyoung 		return NULL;
    421   1.1    dyoung 
    422   1.1    dyoung 	subid = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_SUBSYS_ID_REG);
    423   1.1    dyoung 
    424   1.1    dyoung 	for (ent = ixgbe_vendor_info_array; ent->vendor_id != 0; ent++) {
    425   1.1    dyoung 		if (PCI_VENDOR(pa->pa_id) == ent->vendor_id &&
    426   1.1    dyoung 		    PCI_PRODUCT(pa->pa_id) == ent->device_id &&
    427   1.1    dyoung 
    428   1.1    dyoung 		    (PCI_SUBSYS_VENDOR(subid) == ent->subvendor_id ||
    429   1.1    dyoung 		     ent->subvendor_id == 0) &&
    430   1.1    dyoung 
    431   1.1    dyoung 		    (PCI_SUBSYS_ID(subid) == ent->subdevice_id ||
    432   1.1    dyoung 		     ent->subdevice_id == 0)) {
    433   1.1    dyoung 			++ixgbe_total_ports;
    434   1.1    dyoung 			return ent;
    435   1.1    dyoung 		}
    436   1.1    dyoung 	}
    437   1.1    dyoung 	return NULL;
    438   1.1    dyoung }
    439   1.1    dyoung 
    440   1.1    dyoung 
    441   1.1    dyoung static void
    442   1.1    dyoung ixgbe_sysctl_attach(struct adapter *adapter)
    443   1.1    dyoung {
    444   1.1    dyoung 	struct sysctllog **log;
    445   1.1    dyoung 	const struct sysctlnode *rnode, *cnode;
    446   1.1    dyoung 	device_t dev;
    447   1.1    dyoung 
    448   1.1    dyoung 	dev = adapter->dev;
    449   1.1    dyoung 	log = &adapter->sysctllog;
    450   1.1    dyoung 
    451   1.1    dyoung 	if ((rnode = ixgbe_sysctl_instance(adapter)) == NULL) {
    452   1.1    dyoung 		aprint_error_dev(dev, "could not create sysctl root\n");
    453   1.1    dyoung 		return;
    454   1.1    dyoung 	}
    455   1.1    dyoung 
    456   1.1    dyoung 	if (sysctl_createv(log, 0, &rnode, &cnode,
    457   1.1    dyoung 	    CTLFLAG_READONLY, CTLTYPE_INT,
    458   1.1    dyoung 	    "num_rx_desc", SYSCTL_DESCR("Number of rx descriptors"),
    459   1.1    dyoung 	    NULL, 0, &adapter->num_rx_desc, 0, CTL_CREATE, CTL_EOL) != 0)
    460   1.1    dyoung 		aprint_error_dev(dev, "could not create sysctl\n");
    461   1.1    dyoung 
    462   1.1    dyoung 	if (sysctl_createv(log, 0, &rnode, &cnode,
    463   1.1    dyoung 	    CTLFLAG_READONLY, CTLTYPE_INT,
    464   1.1    dyoung 	    "num_queues", SYSCTL_DESCR("Number of queues"),
    465   1.1    dyoung 	    NULL, 0, &adapter->num_queues, 0, CTL_CREATE, CTL_EOL) != 0)
    466   1.1    dyoung 		aprint_error_dev(dev, "could not create sysctl\n");
    467   1.1    dyoung 
    468   1.1    dyoung 	if (sysctl_createv(log, 0, &rnode, &cnode,
    469   1.1    dyoung 	    CTLFLAG_READWRITE, CTLTYPE_INT,
    470  1.24   msaitoh 	    "fc", SYSCTL_DESCR("Flow Control"),
    471   1.3       dsl 	    ixgbe_set_flowcntl, 0, (void *)adapter, 0, CTL_CREATE, CTL_EOL) != 0)
    472   1.1    dyoung 		aprint_error_dev(dev, "could not create sysctl\n");
    473   1.1    dyoung 
    474  1.24   msaitoh 	/* XXX This is an *instance* sysctl controlling a *global* variable.
    475  1.24   msaitoh 	 * XXX It's that way in the FreeBSD driver that this derives from.
    476  1.24   msaitoh 	 */
    477   1.1    dyoung 	if (sysctl_createv(log, 0, &rnode, &cnode,
    478   1.1    dyoung 	    CTLFLAG_READWRITE, CTLTYPE_INT,
    479  1.24   msaitoh 	    "enable_aim", SYSCTL_DESCR("Interrupt Moderation"),
    480  1.24   msaitoh 	    NULL, 0, &ixgbe_enable_aim, 0, CTL_CREATE, CTL_EOL) != 0)
    481  1.24   msaitoh 		aprint_error_dev(dev, "could not create sysctl\n");
    482  1.24   msaitoh 
    483  1.24   msaitoh 	if (sysctl_createv(log, 0, &rnode, &cnode,
    484  1.24   msaitoh 	    CTLFLAG_READWRITE, CTLTYPE_INT,
    485  1.24   msaitoh 	    "advertise_speed", SYSCTL_DESCR("Link Speed"),
    486   1.3       dsl 	    ixgbe_set_advertise, 0, (void *)adapter, 0, CTL_CREATE, CTL_EOL) != 0)
    487   1.1    dyoung 		aprint_error_dev(dev, "could not create sysctl\n");
    488   1.1    dyoung 
    489   1.1    dyoung 	if (sysctl_createv(log, 0, &rnode, &cnode,
    490   1.1    dyoung 	    CTLFLAG_READWRITE, CTLTYPE_INT,
    491  1.24   msaitoh 	    "ts", SYSCTL_DESCR("Thermal Test"),
    492  1.24   msaitoh 	    ixgbe_set_thermal_test, 0, (void *)adapter, 0, CTL_CREATE, CTL_EOL) != 0)
    493   1.1    dyoung 		aprint_error_dev(dev, "could not create sysctl\n");
    494   1.1    dyoung }
    495   1.1    dyoung 
    496   1.1    dyoung /*********************************************************************
    497   1.1    dyoung  *  Device initialization routine
    498   1.1    dyoung  *
    499   1.1    dyoung  *  The attach entry point is called when the driver is being loaded.
    500   1.1    dyoung  *  This routine identifies the type of hardware, allocates all resources
    501   1.1    dyoung  *  and initializes the hardware.
    502   1.1    dyoung  *
    503   1.1    dyoung  *  return 0 on success, positive on failure
    504   1.1    dyoung  *********************************************************************/
    505   1.1    dyoung 
    506   1.1    dyoung static void
    507   1.1    dyoung ixgbe_attach(device_t parent, device_t dev, void *aux)
    508   1.1    dyoung {
    509   1.1    dyoung 	struct adapter *adapter;
    510   1.1    dyoung 	struct ixgbe_hw *hw;
    511  1.34   msaitoh 	int             error = -1;
    512   1.1    dyoung 	u16		csum;
    513   1.1    dyoung 	u32		ctrl_ext;
    514   1.1    dyoung 	ixgbe_vendor_info_t *ent;
    515   1.1    dyoung 	const struct pci_attach_args *pa = aux;
    516   1.1    dyoung 
    517   1.1    dyoung 	INIT_DEBUGOUT("ixgbe_attach: begin");
    518   1.1    dyoung 
    519   1.1    dyoung 	/* Allocate, clear, and link in our adapter structure */
    520   1.1    dyoung 	adapter = device_private(dev);
    521   1.1    dyoung 	adapter->dev = adapter->osdep.dev = dev;
    522   1.1    dyoung 	hw = &adapter->hw;
    523   1.1    dyoung 	adapter->osdep.pc = pa->pa_pc;
    524   1.1    dyoung 	adapter->osdep.tag = pa->pa_tag;
    525   1.1    dyoung 	adapter->osdep.dmat = pa->pa_dmat;
    526  1.32   msaitoh 	adapter->osdep.attached = false;
    527   1.1    dyoung 
    528   1.1    dyoung 	ent = ixgbe_lookup(pa);
    529   1.1    dyoung 
    530   1.1    dyoung 	KASSERT(ent != NULL);
    531   1.1    dyoung 
    532   1.1    dyoung 	aprint_normal(": %s, Version - %s\n",
    533   1.1    dyoung 	    ixgbe_strings[ent->index], ixgbe_driver_version);
    534   1.1    dyoung 
    535   1.1    dyoung 	/* Core Lock Init*/
    536   1.1    dyoung 	IXGBE_CORE_LOCK_INIT(adapter, device_xname(dev));
    537   1.1    dyoung 
    538   1.1    dyoung 	/* SYSCTL APIs */
    539   1.1    dyoung 
    540   1.1    dyoung 	ixgbe_sysctl_attach(adapter);
    541   1.1    dyoung 
    542   1.1    dyoung 	/* Set up the timer callout */
    543   1.1    dyoung 	callout_init(&adapter->timer, 0);
    544   1.1    dyoung 
    545   1.1    dyoung 	/* Determine hardware revision */
    546   1.1    dyoung 	ixgbe_identify_hardware(adapter);
    547   1.1    dyoung 
    548   1.1    dyoung 	/* Do base PCI setup - map BAR0 */
    549   1.1    dyoung 	if (ixgbe_allocate_pci_resources(adapter, pa)) {
    550   1.1    dyoung 		aprint_error_dev(dev, "Allocation of PCI resources failed\n");
    551   1.1    dyoung 		error = ENXIO;
    552   1.1    dyoung 		goto err_out;
    553   1.1    dyoung 	}
    554   1.1    dyoung 
    555   1.1    dyoung 	/* Do descriptor calc and sanity checks */
    556   1.1    dyoung 	if (((ixgbe_txd * sizeof(union ixgbe_adv_tx_desc)) % DBA_ALIGN) != 0 ||
    557   1.1    dyoung 	    ixgbe_txd < MIN_TXD || ixgbe_txd > MAX_TXD) {
    558   1.1    dyoung 		aprint_error_dev(dev, "TXD config issue, using default!\n");
    559   1.1    dyoung 		adapter->num_tx_desc = DEFAULT_TXD;
    560   1.1    dyoung 	} else
    561   1.1    dyoung 		adapter->num_tx_desc = ixgbe_txd;
    562   1.1    dyoung 
    563   1.1    dyoung 	/*
    564   1.1    dyoung 	** With many RX rings it is easy to exceed the
    565   1.1    dyoung 	** system mbuf allocation. Tuning nmbclusters
    566   1.1    dyoung 	** can alleviate this.
    567   1.1    dyoung 	*/
    568   1.1    dyoung 	if (nmbclusters > 0 ) {
    569   1.1    dyoung 		int s;
    570   1.1    dyoung 		s = (ixgbe_rxd * adapter->num_queues) * ixgbe_total_ports;
    571   1.1    dyoung 		if (s > nmbclusters) {
    572   1.1    dyoung 			aprint_error_dev(dev, "RX Descriptors exceed "
    573   1.1    dyoung 			    "system mbuf max, using default instead!\n");
    574   1.1    dyoung 			ixgbe_rxd = DEFAULT_RXD;
    575   1.1    dyoung 		}
    576   1.1    dyoung 	}
    577   1.1    dyoung 
    578   1.1    dyoung 	if (((ixgbe_rxd * sizeof(union ixgbe_adv_rx_desc)) % DBA_ALIGN) != 0 ||
    579  1.33   msaitoh 	    ixgbe_rxd < MIN_RXD || ixgbe_rxd > MAX_RXD) {
    580   1.1    dyoung 		aprint_error_dev(dev, "RXD config issue, using default!\n");
    581   1.1    dyoung 		adapter->num_rx_desc = DEFAULT_RXD;
    582   1.1    dyoung 	} else
    583   1.1    dyoung 		adapter->num_rx_desc = ixgbe_rxd;
    584   1.1    dyoung 
    585   1.1    dyoung 	/* Allocate our TX/RX Queues */
    586   1.1    dyoung 	if (ixgbe_allocate_queues(adapter)) {
    587   1.1    dyoung 		error = ENOMEM;
    588   1.1    dyoung 		goto err_out;
    589   1.1    dyoung 	}
    590   1.1    dyoung 
    591   1.1    dyoung 	/* Allocate multicast array memory. */
    592   1.1    dyoung 	adapter->mta = malloc(sizeof(u8) * IXGBE_ETH_LENGTH_OF_ADDRESS *
    593   1.1    dyoung 	    MAX_NUM_MULTICAST_ADDRESSES, M_DEVBUF, M_NOWAIT);
    594   1.1    dyoung 	if (adapter->mta == NULL) {
    595   1.1    dyoung 		aprint_error_dev(dev, "Cannot allocate multicast setup array\n");
    596   1.1    dyoung 		error = ENOMEM;
    597   1.1    dyoung 		goto err_late;
    598   1.1    dyoung 	}
    599   1.1    dyoung 
    600   1.1    dyoung 	/* Initialize the shared code */
    601  1.33   msaitoh 	hw->allow_unsupported_sfp = allow_unsupported_sfp;
    602   1.1    dyoung 	error = ixgbe_init_shared_code(hw);
    603   1.1    dyoung 	if (error == IXGBE_ERR_SFP_NOT_PRESENT) {
    604   1.1    dyoung 		/*
    605   1.1    dyoung 		** No optics in this port, set up
    606   1.1    dyoung 		** so the timer routine will probe
    607   1.1    dyoung 		** for later insertion.
    608   1.1    dyoung 		*/
    609   1.1    dyoung 		adapter->sfp_probe = TRUE;
    610   1.1    dyoung 		error = 0;
    611  1.35   msaitoh 	} else if (error == IXGBE_ERR_SFP_NOT_SUPPORTED) {
    612   1.1    dyoung 		aprint_error_dev(dev,"Unsupported SFP+ module detected!\n");
    613   1.1    dyoung 		error = EIO;
    614   1.1    dyoung 		goto err_late;
    615   1.1    dyoung 	} else if (error) {
    616   1.1    dyoung 		aprint_error_dev(dev,"Unable to initialize the shared code\n");
    617   1.1    dyoung 		error = EIO;
    618   1.1    dyoung 		goto err_late;
    619   1.1    dyoung 	}
    620   1.1    dyoung 
    621   1.1    dyoung 	/* Make sure we have a good EEPROM before we read from it */
    622   1.1    dyoung 	if (ixgbe_validate_eeprom_checksum(&adapter->hw, &csum) < 0) {
    623   1.1    dyoung 		aprint_error_dev(dev,"The EEPROM Checksum Is Not Valid\n");
    624   1.1    dyoung 		error = EIO;
    625   1.1    dyoung 		goto err_late;
    626   1.1    dyoung 	}
    627   1.1    dyoung 
    628   1.1    dyoung 	error = ixgbe_init_hw(hw);
    629  1.25   msaitoh 	switch (error) {
    630  1.25   msaitoh 	case IXGBE_ERR_EEPROM_VERSION:
    631   1.1    dyoung 		aprint_error_dev(dev, "This device is a pre-production adapter/"
    632   1.1    dyoung 		    "LOM.  Please be aware there may be issues associated "
    633   1.1    dyoung 		    "with your hardware.\n If you are experiencing problems "
    634   1.1    dyoung 		    "please contact your Intel or hardware representative "
    635   1.1    dyoung 		    "who provided you with this hardware.\n");
    636  1.25   msaitoh 		break;
    637  1.25   msaitoh 	case IXGBE_ERR_SFP_NOT_SUPPORTED:
    638   1.1    dyoung 		aprint_error_dev(dev,"Unsupported SFP+ Module\n");
    639   1.1    dyoung 		error = EIO;
    640   1.1    dyoung 		aprint_error_dev(dev,"Hardware Initialization Failure\n");
    641   1.1    dyoung 		goto err_late;
    642  1.25   msaitoh 	case IXGBE_ERR_SFP_NOT_PRESENT:
    643  1.25   msaitoh 		device_printf(dev,"No SFP+ Module found\n");
    644  1.25   msaitoh 		/* falls thru */
    645  1.25   msaitoh 	default:
    646  1.25   msaitoh 		break;
    647   1.1    dyoung 	}
    648   1.1    dyoung 
    649   1.1    dyoung 	/* Detect and set physical type */
    650   1.1    dyoung 	ixgbe_setup_optics(adapter);
    651   1.1    dyoung 
    652  1.34   msaitoh 	error = -1;
    653   1.1    dyoung 	if ((adapter->msix > 1) && (ixgbe_enable_msix))
    654  1.34   msaitoh 		error = ixgbe_allocate_msix(adapter, pa);
    655  1.34   msaitoh 	if (error != 0)
    656  1.34   msaitoh 		error = ixgbe_allocate_legacy(adapter, pa);
    657   1.1    dyoung 	if (error)
    658   1.1    dyoung 		goto err_late;
    659   1.1    dyoung 
    660   1.1    dyoung 	/* Setup OS specific network interface */
    661   1.1    dyoung 	if (ixgbe_setup_interface(dev, adapter) != 0)
    662   1.1    dyoung 		goto err_late;
    663   1.1    dyoung 
    664   1.1    dyoung 	/* Initialize statistics */
    665   1.1    dyoung 	ixgbe_update_stats_counters(adapter);
    666   1.1    dyoung 
    667  1.33   msaitoh 	/*
    668  1.33   msaitoh 	** Check PCIE slot type/speed/width
    669  1.33   msaitoh 	*/
    670  1.33   msaitoh 	ixgbe_get_slot_info(hw);
    671   1.1    dyoung 
    672  1.28   msaitoh 	/* Set an initial default flow control value */
    673  1.28   msaitoh 	adapter->fc =  ixgbe_fc_full;
    674  1.28   msaitoh 
    675   1.1    dyoung 	/* let hardware know driver is loaded */
    676   1.1    dyoung 	ctrl_ext = IXGBE_READ_REG(hw, IXGBE_CTRL_EXT);
    677   1.1    dyoung 	ctrl_ext |= IXGBE_CTRL_EXT_DRV_LOAD;
    678   1.1    dyoung 	IXGBE_WRITE_REG(hw, IXGBE_CTRL_EXT, ctrl_ext);
    679   1.1    dyoung 
    680   1.1    dyoung 	ixgbe_add_hw_stats(adapter);
    681   1.1    dyoung 
    682  1.22   msaitoh #ifdef DEV_NETMAP
    683  1.22   msaitoh 	ixgbe_netmap_attach(adapter);
    684  1.22   msaitoh #endif /* DEV_NETMAP */
    685   1.1    dyoung 	INIT_DEBUGOUT("ixgbe_attach: end");
    686  1.32   msaitoh 	adapter->osdep.attached = true;
    687   1.1    dyoung 	return;
    688   1.1    dyoung err_late:
    689   1.1    dyoung 	ixgbe_free_transmit_structures(adapter);
    690   1.1    dyoung 	ixgbe_free_receive_structures(adapter);
    691   1.1    dyoung err_out:
    692   1.1    dyoung 	if (adapter->ifp != NULL)
    693   1.1    dyoung 		if_free(adapter->ifp);
    694   1.1    dyoung 	ixgbe_free_pci_resources(adapter);
    695   1.1    dyoung 	if (adapter->mta != NULL)
    696   1.1    dyoung 		free(adapter->mta, M_DEVBUF);
    697   1.1    dyoung 	return;
    698   1.1    dyoung 
    699   1.1    dyoung }
    700   1.1    dyoung 
    701   1.1    dyoung /*********************************************************************
    702   1.1    dyoung  *  Device removal routine
    703   1.1    dyoung  *
    704   1.1    dyoung  *  The detach entry point is called when the driver is being removed.
    705   1.1    dyoung  *  This routine stops the adapter and deallocates all the resources
    706   1.1    dyoung  *  that were allocated for driver operation.
    707   1.1    dyoung  *
    708   1.1    dyoung  *  return 0 on success, positive on failure
    709   1.1    dyoung  *********************************************************************/
    710   1.1    dyoung 
    711   1.1    dyoung static int
    712   1.1    dyoung ixgbe_detach(device_t dev, int flags)
    713   1.1    dyoung {
    714   1.1    dyoung 	struct adapter *adapter = device_private(dev);
    715   1.1    dyoung 	struct rx_ring *rxr = adapter->rx_rings;
    716   1.1    dyoung 	struct ixgbe_hw_stats *stats = &adapter->stats;
    717   1.1    dyoung 	struct ix_queue *que = adapter->queues;
    718  1.26   msaitoh 	struct tx_ring *txr = adapter->tx_rings;
    719   1.1    dyoung 	u32	ctrl_ext;
    720   1.1    dyoung 
    721   1.1    dyoung 	INIT_DEBUGOUT("ixgbe_detach: begin");
    722  1.32   msaitoh 	if (adapter->osdep.attached == false)
    723  1.32   msaitoh 		return 0;
    724   1.1    dyoung 
    725  1.29   msaitoh #if NVLAN > 0
    726   1.1    dyoung 	/* Make sure VLANs are not using driver */
    727   1.1    dyoung 	if (!VLAN_ATTACHED(&adapter->osdep.ec))
    728   1.1    dyoung 		;	/* nothing to do: no VLANs */
    729   1.1    dyoung 	else if ((flags & (DETACH_SHUTDOWN|DETACH_FORCE)) != 0)
    730   1.1    dyoung 		vlan_ifdetach(adapter->ifp);
    731   1.1    dyoung 	else {
    732   1.1    dyoung 		aprint_error_dev(dev, "VLANs in use\n");
    733   1.1    dyoung 		return EBUSY;
    734   1.1    dyoung 	}
    735  1.29   msaitoh #endif
    736   1.1    dyoung 
    737   1.1    dyoung 	IXGBE_CORE_LOCK(adapter);
    738   1.1    dyoung 	ixgbe_stop(adapter);
    739   1.1    dyoung 	IXGBE_CORE_UNLOCK(adapter);
    740   1.1    dyoung 
    741  1.26   msaitoh 	for (int i = 0; i < adapter->num_queues; i++, que++, txr++) {
    742  1.28   msaitoh #ifndef IXGBE_LEGACY_TX
    743  1.26   msaitoh 		softint_disestablish(txr->txq_si);
    744  1.26   msaitoh #endif
    745   1.1    dyoung 		softint_disestablish(que->que_si);
    746   1.1    dyoung 	}
    747   1.1    dyoung 
    748   1.1    dyoung 	/* Drain the Link queue */
    749   1.1    dyoung 	softint_disestablish(adapter->link_si);
    750   1.1    dyoung 	softint_disestablish(adapter->mod_si);
    751   1.1    dyoung 	softint_disestablish(adapter->msf_si);
    752   1.1    dyoung #ifdef IXGBE_FDIR
    753   1.1    dyoung 	softint_disestablish(adapter->fdir_si);
    754   1.1    dyoung #endif
    755   1.1    dyoung 
    756   1.1    dyoung 	/* let hardware know driver is unloading */
    757   1.1    dyoung 	ctrl_ext = IXGBE_READ_REG(&adapter->hw, IXGBE_CTRL_EXT);
    758   1.1    dyoung 	ctrl_ext &= ~IXGBE_CTRL_EXT_DRV_LOAD;
    759   1.1    dyoung 	IXGBE_WRITE_REG(&adapter->hw, IXGBE_CTRL_EXT, ctrl_ext);
    760   1.1    dyoung 
    761   1.1    dyoung 	ether_ifdetach(adapter->ifp);
    762   1.1    dyoung 	callout_halt(&adapter->timer, NULL);
    763  1.22   msaitoh #ifdef DEV_NETMAP
    764  1.22   msaitoh 	netmap_detach(adapter->ifp);
    765  1.22   msaitoh #endif /* DEV_NETMAP */
    766   1.1    dyoung 	ixgbe_free_pci_resources(adapter);
    767   1.1    dyoung #if 0	/* XXX the NetBSD port is probably missing something here */
    768   1.1    dyoung 	bus_generic_detach(dev);
    769   1.1    dyoung #endif
    770   1.1    dyoung 	if_detach(adapter->ifp);
    771   1.1    dyoung 
    772   1.1    dyoung 	sysctl_teardown(&adapter->sysctllog);
    773   1.1    dyoung 	evcnt_detach(&adapter->handleq);
    774   1.1    dyoung 	evcnt_detach(&adapter->req);
    775   1.1    dyoung 	evcnt_detach(&adapter->morerx);
    776   1.1    dyoung 	evcnt_detach(&adapter->moretx);
    777   1.1    dyoung 	evcnt_detach(&adapter->txloops);
    778   1.1    dyoung 	evcnt_detach(&adapter->efbig_tx_dma_setup);
    779   1.1    dyoung 	evcnt_detach(&adapter->m_defrag_failed);
    780   1.1    dyoung 	evcnt_detach(&adapter->efbig2_tx_dma_setup);
    781   1.1    dyoung 	evcnt_detach(&adapter->einval_tx_dma_setup);
    782   1.1    dyoung 	evcnt_detach(&adapter->other_tx_dma_setup);
    783   1.1    dyoung 	evcnt_detach(&adapter->eagain_tx_dma_setup);
    784   1.1    dyoung 	evcnt_detach(&adapter->enomem_tx_dma_setup);
    785   1.1    dyoung 	evcnt_detach(&adapter->watchdog_events);
    786   1.1    dyoung 	evcnt_detach(&adapter->tso_err);
    787   1.1    dyoung 	evcnt_detach(&adapter->link_irq);
    788  1.26   msaitoh 
    789  1.26   msaitoh 	txr = adapter->tx_rings;
    790   1.1    dyoung 	for (int i = 0; i < adapter->num_queues; i++, rxr++, txr++) {
    791   1.1    dyoung 		evcnt_detach(&txr->no_desc_avail);
    792   1.1    dyoung 		evcnt_detach(&txr->total_packets);
    793  1.28   msaitoh 		evcnt_detach(&txr->tso_tx);
    794   1.1    dyoung 
    795   1.1    dyoung 		if (i < __arraycount(adapter->stats.mpc)) {
    796   1.1    dyoung 			evcnt_detach(&adapter->stats.mpc[i]);
    797   1.1    dyoung 		}
    798   1.1    dyoung 		if (i < __arraycount(adapter->stats.pxontxc)) {
    799   1.1    dyoung 			evcnt_detach(&adapter->stats.pxontxc[i]);
    800   1.1    dyoung 			evcnt_detach(&adapter->stats.pxonrxc[i]);
    801   1.1    dyoung 			evcnt_detach(&adapter->stats.pxofftxc[i]);
    802   1.1    dyoung 			evcnt_detach(&adapter->stats.pxoffrxc[i]);
    803   1.1    dyoung 			evcnt_detach(&adapter->stats.pxon2offc[i]);
    804   1.1    dyoung 		}
    805   1.1    dyoung 		if (i < __arraycount(adapter->stats.qprc)) {
    806   1.1    dyoung 			evcnt_detach(&adapter->stats.qprc[i]);
    807   1.1    dyoung 			evcnt_detach(&adapter->stats.qptc[i]);
    808   1.1    dyoung 			evcnt_detach(&adapter->stats.qbrc[i]);
    809   1.1    dyoung 			evcnt_detach(&adapter->stats.qbtc[i]);
    810   1.1    dyoung 			evcnt_detach(&adapter->stats.qprdc[i]);
    811   1.1    dyoung 		}
    812   1.1    dyoung 
    813   1.1    dyoung 		evcnt_detach(&rxr->rx_packets);
    814   1.1    dyoung 		evcnt_detach(&rxr->rx_bytes);
    815  1.31   msaitoh 		evcnt_detach(&rxr->rx_copies);
    816   1.1    dyoung 		evcnt_detach(&rxr->no_jmbuf);
    817   1.1    dyoung 		evcnt_detach(&rxr->rx_discarded);
    818   1.1    dyoung 		evcnt_detach(&rxr->rx_irq);
    819   1.1    dyoung 	}
    820   1.1    dyoung 	evcnt_detach(&stats->ipcs);
    821   1.1    dyoung 	evcnt_detach(&stats->l4cs);
    822   1.1    dyoung 	evcnt_detach(&stats->ipcs_bad);
    823   1.1    dyoung 	evcnt_detach(&stats->l4cs_bad);
    824   1.1    dyoung 	evcnt_detach(&stats->intzero);
    825   1.1    dyoung 	evcnt_detach(&stats->legint);
    826   1.1    dyoung 	evcnt_detach(&stats->crcerrs);
    827   1.1    dyoung 	evcnt_detach(&stats->illerrc);
    828   1.1    dyoung 	evcnt_detach(&stats->errbc);
    829   1.1    dyoung 	evcnt_detach(&stats->mspdc);
    830   1.1    dyoung 	evcnt_detach(&stats->mlfc);
    831   1.1    dyoung 	evcnt_detach(&stats->mrfc);
    832   1.1    dyoung 	evcnt_detach(&stats->rlec);
    833   1.1    dyoung 	evcnt_detach(&stats->lxontxc);
    834   1.1    dyoung 	evcnt_detach(&stats->lxonrxc);
    835   1.1    dyoung 	evcnt_detach(&stats->lxofftxc);
    836   1.1    dyoung 	evcnt_detach(&stats->lxoffrxc);
    837   1.1    dyoung 
    838   1.1    dyoung 	/* Packet Reception Stats */
    839   1.1    dyoung 	evcnt_detach(&stats->tor);
    840   1.1    dyoung 	evcnt_detach(&stats->gorc);
    841   1.1    dyoung 	evcnt_detach(&stats->tpr);
    842   1.1    dyoung 	evcnt_detach(&stats->gprc);
    843   1.1    dyoung 	evcnt_detach(&stats->mprc);
    844   1.1    dyoung 	evcnt_detach(&stats->bprc);
    845   1.1    dyoung 	evcnt_detach(&stats->prc64);
    846   1.1    dyoung 	evcnt_detach(&stats->prc127);
    847   1.1    dyoung 	evcnt_detach(&stats->prc255);
    848   1.1    dyoung 	evcnt_detach(&stats->prc511);
    849   1.1    dyoung 	evcnt_detach(&stats->prc1023);
    850   1.1    dyoung 	evcnt_detach(&stats->prc1522);
    851   1.1    dyoung 	evcnt_detach(&stats->ruc);
    852   1.1    dyoung 	evcnt_detach(&stats->rfc);
    853   1.1    dyoung 	evcnt_detach(&stats->roc);
    854   1.1    dyoung 	evcnt_detach(&stats->rjc);
    855   1.1    dyoung 	evcnt_detach(&stats->mngprc);
    856   1.1    dyoung 	evcnt_detach(&stats->xec);
    857   1.1    dyoung 
    858   1.1    dyoung 	/* Packet Transmission Stats */
    859   1.1    dyoung 	evcnt_detach(&stats->gotc);
    860   1.1    dyoung 	evcnt_detach(&stats->tpt);
    861   1.1    dyoung 	evcnt_detach(&stats->gptc);
    862   1.1    dyoung 	evcnt_detach(&stats->bptc);
    863   1.1    dyoung 	evcnt_detach(&stats->mptc);
    864   1.1    dyoung 	evcnt_detach(&stats->mngptc);
    865   1.1    dyoung 	evcnt_detach(&stats->ptc64);
    866   1.1    dyoung 	evcnt_detach(&stats->ptc127);
    867   1.1    dyoung 	evcnt_detach(&stats->ptc255);
    868   1.1    dyoung 	evcnt_detach(&stats->ptc511);
    869   1.1    dyoung 	evcnt_detach(&stats->ptc1023);
    870   1.1    dyoung 	evcnt_detach(&stats->ptc1522);
    871   1.1    dyoung 
    872   1.1    dyoung 	ixgbe_free_transmit_structures(adapter);
    873   1.1    dyoung 	ixgbe_free_receive_structures(adapter);
    874   1.1    dyoung 	free(adapter->mta, M_DEVBUF);
    875   1.1    dyoung 
    876   1.1    dyoung 	IXGBE_CORE_LOCK_DESTROY(adapter);
    877   1.1    dyoung 	return (0);
    878   1.1    dyoung }
    879   1.1    dyoung 
    880   1.1    dyoung /*********************************************************************
    881   1.1    dyoung  *
    882   1.1    dyoung  *  Shutdown entry point
    883   1.1    dyoung  *
    884   1.1    dyoung  **********************************************************************/
    885   1.1    dyoung 
    886   1.1    dyoung #if 0 /* XXX NetBSD ought to register something like this through pmf(9) */
    887   1.1    dyoung static int
    888   1.1    dyoung ixgbe_shutdown(device_t dev)
    889   1.1    dyoung {
    890   1.1    dyoung 	struct adapter *adapter = device_private(dev);
    891   1.1    dyoung 	IXGBE_CORE_LOCK(adapter);
    892   1.1    dyoung 	ixgbe_stop(adapter);
    893   1.1    dyoung 	IXGBE_CORE_UNLOCK(adapter);
    894   1.1    dyoung 	return (0);
    895   1.1    dyoung }
    896   1.1    dyoung #endif
    897   1.1    dyoung 
    898   1.1    dyoung 
    899  1.28   msaitoh #ifdef IXGBE_LEGACY_TX
    900   1.1    dyoung /*********************************************************************
    901   1.1    dyoung  *  Transmit entry point
    902   1.1    dyoung  *
    903   1.1    dyoung  *  ixgbe_start is called by the stack to initiate a transmit.
    904   1.1    dyoung  *  The driver will remain in this routine as long as there are
    905   1.1    dyoung  *  packets to transmit and transmit resources are available.
    906   1.1    dyoung  *  In case resources are not available stack is notified and
    907   1.1    dyoung  *  the packet is requeued.
    908   1.1    dyoung  **********************************************************************/
    909   1.1    dyoung 
    910   1.1    dyoung static void
    911   1.1    dyoung ixgbe_start_locked(struct tx_ring *txr, struct ifnet * ifp)
    912   1.1    dyoung {
    913   1.1    dyoung 	int rc;
    914   1.1    dyoung 	struct mbuf    *m_head;
    915   1.1    dyoung 	struct adapter *adapter = txr->adapter;
    916   1.1    dyoung 
    917   1.1    dyoung 	IXGBE_TX_LOCK_ASSERT(txr);
    918   1.1    dyoung 
    919  1.26   msaitoh 	if ((ifp->if_flags & IFF_RUNNING) == 0)
    920   1.1    dyoung 		return;
    921   1.1    dyoung 	if (!adapter->link_active)
    922   1.1    dyoung 		return;
    923   1.1    dyoung 
    924   1.1    dyoung 	while (!IFQ_IS_EMPTY(&ifp->if_snd)) {
    925  1.26   msaitoh 		if (txr->tx_avail <= IXGBE_QUEUE_MIN_FREE)
    926  1.24   msaitoh 			break;
    927   1.1    dyoung 
    928   1.1    dyoung 		IFQ_POLL(&ifp->if_snd, m_head);
    929   1.1    dyoung 		if (m_head == NULL)
    930   1.1    dyoung 			break;
    931   1.1    dyoung 
    932   1.1    dyoung 		if ((rc = ixgbe_xmit(txr, m_head)) == EAGAIN) {
    933   1.1    dyoung 			break;
    934   1.1    dyoung 		}
    935   1.1    dyoung 		IFQ_DEQUEUE(&ifp->if_snd, m_head);
    936   1.1    dyoung 		if (rc == EFBIG) {
    937   1.1    dyoung 			struct mbuf *mtmp;
    938   1.1    dyoung 
    939  1.28   msaitoh 			if ((mtmp = m_defrag(m_head, M_NOWAIT)) != NULL) {
    940   1.1    dyoung 				m_head = mtmp;
    941   1.1    dyoung 				rc = ixgbe_xmit(txr, m_head);
    942   1.1    dyoung 				if (rc != 0)
    943   1.1    dyoung 					adapter->efbig2_tx_dma_setup.ev_count++;
    944   1.1    dyoung 			} else
    945   1.1    dyoung 				adapter->m_defrag_failed.ev_count++;
    946   1.1    dyoung 		}
    947   1.1    dyoung 		if (rc != 0) {
    948   1.1    dyoung 			m_freem(m_head);
    949   1.1    dyoung 			continue;
    950   1.1    dyoung 		}
    951   1.1    dyoung 
    952   1.1    dyoung 		/* Send a copy of the frame to the BPF listener */
    953   1.1    dyoung 		bpf_mtap(ifp, m_head);
    954   1.1    dyoung 
    955   1.1    dyoung 		/* Set watchdog on */
    956   1.1    dyoung 		getmicrotime(&txr->watchdog_time);
    957   1.1    dyoung 		txr->queue_status = IXGBE_QUEUE_WORKING;
    958   1.1    dyoung 
    959   1.1    dyoung 	}
    960   1.1    dyoung 	return;
    961   1.1    dyoung }
    962   1.1    dyoung 
    963   1.1    dyoung /*
    964   1.1    dyoung  * Legacy TX start - called by the stack, this
    965   1.1    dyoung  * always uses the first tx ring, and should
    966   1.1    dyoung  * not be used with multiqueue tx enabled.
    967   1.1    dyoung  */
    968   1.1    dyoung static void
    969   1.1    dyoung ixgbe_start(struct ifnet *ifp)
    970   1.1    dyoung {
    971   1.1    dyoung 	struct adapter *adapter = ifp->if_softc;
    972   1.1    dyoung 	struct tx_ring	*txr = adapter->tx_rings;
    973   1.1    dyoung 
    974   1.1    dyoung 	if (ifp->if_flags & IFF_RUNNING) {
    975   1.1    dyoung 		IXGBE_TX_LOCK(txr);
    976   1.1    dyoung 		ixgbe_start_locked(txr, ifp);
    977   1.1    dyoung 		IXGBE_TX_UNLOCK(txr);
    978   1.1    dyoung 	}
    979   1.1    dyoung 	return;
    980   1.1    dyoung }
    981   1.1    dyoung 
    982  1.28   msaitoh #else /* ! IXGBE_LEGACY_TX */
    983  1.28   msaitoh 
    984   1.1    dyoung /*
    985   1.1    dyoung ** Multiqueue Transmit driver
    986   1.1    dyoung **
    987   1.1    dyoung */
    988   1.1    dyoung static int
    989   1.1    dyoung ixgbe_mq_start(struct ifnet *ifp, struct mbuf *m)
    990   1.1    dyoung {
    991   1.1    dyoung 	struct adapter	*adapter = ifp->if_softc;
    992   1.1    dyoung 	struct ix_queue	*que;
    993   1.1    dyoung 	struct tx_ring	*txr;
    994  1.33   msaitoh 	int 		i, err = 0;
    995  1.33   msaitoh #ifdef	RSS
    996  1.33   msaitoh 	uint32_t bucket_id;
    997  1.33   msaitoh #endif
    998   1.1    dyoung 
    999   1.1    dyoung 	/* Which queue to use */
   1000  1.33   msaitoh 	/*
   1001  1.33   msaitoh 	 * When doing RSS, map it to the same outbound queue
   1002  1.33   msaitoh 	 * as the incoming flow would be mapped to.
   1003  1.33   msaitoh 	 *
   1004  1.33   msaitoh 	 * If everything is setup correctly, it should be the
   1005  1.33   msaitoh 	 * same bucket that the current CPU we're on is.
   1006  1.33   msaitoh 	 */
   1007  1.33   msaitoh 	if (M_HASHTYPE_GET(m) != M_HASHTYPE_NONE) {
   1008  1.33   msaitoh #ifdef	RSS
   1009  1.33   msaitoh 		if (rss_hash2bucket(m->m_pkthdr.flowid,
   1010  1.33   msaitoh 		    M_HASHTYPE_GET(m), &bucket_id) == 0) {
   1011  1.33   msaitoh 			/* XXX TODO: spit out something if bucket_id > num_queues? */
   1012  1.33   msaitoh 			i = bucket_id % adapter->num_queues;
   1013  1.33   msaitoh 		} else {
   1014  1.33   msaitoh #endif
   1015  1.33   msaitoh 			i = m->m_pkthdr.flowid % adapter->num_queues;
   1016  1.33   msaitoh #ifdef	RSS
   1017  1.33   msaitoh 		}
   1018  1.33   msaitoh #endif
   1019  1.33   msaitoh 	} else {
   1020  1.33   msaitoh 		i = curcpu % adapter->num_queues;
   1021  1.33   msaitoh 	}
   1022   1.1    dyoung 
   1023   1.1    dyoung 	txr = &adapter->tx_rings[i];
   1024   1.1    dyoung 	que = &adapter->queues[i];
   1025   1.1    dyoung 
   1026  1.33   msaitoh 	err = drbr_enqueue(ifp, txr->br, m);
   1027  1.33   msaitoh 	if (err)
   1028  1.33   msaitoh 		return (err);
   1029  1.26   msaitoh 	if (IXGBE_TX_TRYLOCK(txr)) {
   1030  1.33   msaitoh 		ixgbe_mq_start_locked(ifp, txr);
   1031   1.1    dyoung 		IXGBE_TX_UNLOCK(txr);
   1032  1.33   msaitoh 	} else
   1033  1.26   msaitoh 		softint_schedule(txr->txq_si);
   1034   1.1    dyoung 
   1035  1.33   msaitoh 	return (0);
   1036   1.1    dyoung }
   1037   1.1    dyoung 
   1038   1.1    dyoung static int
   1039  1.33   msaitoh ixgbe_mq_start_locked(struct ifnet *ifp, struct tx_ring *txr)
   1040   1.1    dyoung {
   1041   1.1    dyoung 	struct adapter  *adapter = txr->adapter;
   1042  1.33   msaitoh 	struct mbuf     *next;
   1043  1.33   msaitoh 	int             enqueued = 0, err = 0;
   1044   1.1    dyoung 
   1045  1.24   msaitoh 	if (((ifp->if_flags & IFF_RUNNING) == 0) ||
   1046  1.33   msaitoh 	    adapter->link_active == 0)
   1047  1.33   msaitoh 		return (ENETDOWN);
   1048   1.1    dyoung 
   1049   1.1    dyoung 	/* Process the queue */
   1050  1.33   msaitoh #if __FreeBSD_version < 901504
   1051  1.33   msaitoh 	next = drbr_dequeue(ifp, txr->br);
   1052  1.33   msaitoh 	while (next != NULL) {
   1053  1.33   msaitoh 		if ((err = ixgbe_xmit(txr, &next)) != 0) {
   1054  1.33   msaitoh 			if (next != NULL)
   1055  1.33   msaitoh 				err = drbr_enqueue(ifp, txr->br, next);
   1056  1.33   msaitoh #else
   1057  1.28   msaitoh 	while ((next = drbr_peek(ifp, txr->br)) != NULL) {
   1058   1.1    dyoung 		if ((err = ixgbe_xmit(txr, &next)) != 0) {
   1059  1.28   msaitoh 			if (next == NULL) {
   1060  1.28   msaitoh 				drbr_advance(ifp, txr->br);
   1061  1.28   msaitoh 			} else {
   1062  1.28   msaitoh 				drbr_putback(ifp, txr->br, next);
   1063  1.28   msaitoh 			}
   1064  1.33   msaitoh #endif
   1065   1.1    dyoung 			break;
   1066   1.1    dyoung 		}
   1067  1.33   msaitoh #if __FreeBSD_version >= 901504
   1068  1.28   msaitoh 		drbr_advance(ifp, txr->br);
   1069  1.33   msaitoh #endif
   1070   1.1    dyoung 		enqueued++;
   1071   1.1    dyoung 		/* Send a copy of the frame to the BPF listener */
   1072   1.1    dyoung 		bpf_mtap(ifp, next);
   1073   1.1    dyoung 		if ((ifp->if_flags & IFF_RUNNING) == 0)
   1074   1.1    dyoung 			break;
   1075  1.33   msaitoh #if __FreeBSD_version < 901504
   1076  1.33   msaitoh 		next = drbr_dequeue(ifp, txr->br);
   1077  1.33   msaitoh #endif
   1078   1.1    dyoung 	}
   1079   1.1    dyoung 
   1080   1.1    dyoung 	if (enqueued > 0) {
   1081   1.1    dyoung 		/* Set watchdog on */
   1082  1.26   msaitoh 		txr->queue_status = IXGBE_QUEUE_WORKING;
   1083   1.1    dyoung 		getmicrotime(&txr->watchdog_time);
   1084   1.1    dyoung 	}
   1085   1.1    dyoung 
   1086  1.24   msaitoh 	if (txr->tx_avail < IXGBE_TX_CLEANUP_THRESHOLD)
   1087  1.24   msaitoh 		ixgbe_txeof(txr);
   1088  1.24   msaitoh 
   1089   1.1    dyoung 	return (err);
   1090   1.1    dyoung }
   1091   1.1    dyoung 
   1092   1.1    dyoung /*
   1093  1.26   msaitoh  * Called from a taskqueue to drain queued transmit packets.
   1094  1.26   msaitoh  */
   1095  1.26   msaitoh static void
   1096  1.35   msaitoh ixgbe_deferred_mq_start(void *arg, int pending)
   1097  1.26   msaitoh {
   1098  1.26   msaitoh 	struct tx_ring *txr = arg;
   1099  1.26   msaitoh 	struct adapter *adapter = txr->adapter;
   1100  1.26   msaitoh 	struct ifnet *ifp = adapter->ifp;
   1101  1.26   msaitoh 
   1102  1.26   msaitoh 	IXGBE_TX_LOCK(txr);
   1103  1.26   msaitoh 	if (!drbr_empty(ifp, txr->br))
   1104  1.33   msaitoh 		ixgbe_mq_start_locked(ifp, txr);
   1105  1.26   msaitoh 	IXGBE_TX_UNLOCK(txr);
   1106  1.26   msaitoh }
   1107  1.26   msaitoh 
   1108  1.26   msaitoh /*
   1109   1.1    dyoung ** Flush all ring buffers
   1110   1.1    dyoung */
   1111   1.1    dyoung static void
   1112   1.1    dyoung ixgbe_qflush(struct ifnet *ifp)
   1113   1.1    dyoung {
   1114   1.1    dyoung 	struct adapter	*adapter = ifp->if_softc;
   1115   1.1    dyoung 	struct tx_ring	*txr = adapter->tx_rings;
   1116   1.1    dyoung 	struct mbuf	*m;
   1117   1.1    dyoung 
   1118   1.1    dyoung 	for (int i = 0; i < adapter->num_queues; i++, txr++) {
   1119   1.1    dyoung 		IXGBE_TX_LOCK(txr);
   1120   1.1    dyoung 		while ((m = buf_ring_dequeue_sc(txr->br)) != NULL)
   1121   1.1    dyoung 			m_freem(m);
   1122   1.1    dyoung 		IXGBE_TX_UNLOCK(txr);
   1123   1.1    dyoung 	}
   1124   1.1    dyoung 	if_qflush(ifp);
   1125   1.1    dyoung }
   1126  1.28   msaitoh #endif /* IXGBE_LEGACY_TX */
   1127   1.1    dyoung 
   1128   1.1    dyoung static int
   1129   1.1    dyoung ixgbe_ifflags_cb(struct ethercom *ec)
   1130   1.1    dyoung {
   1131   1.1    dyoung 	struct ifnet *ifp = &ec->ec_if;
   1132   1.1    dyoung 	struct adapter *adapter = ifp->if_softc;
   1133   1.1    dyoung 	int change = ifp->if_flags ^ adapter->if_flags, rc = 0;
   1134   1.1    dyoung 
   1135   1.1    dyoung 	IXGBE_CORE_LOCK(adapter);
   1136   1.1    dyoung 
   1137   1.1    dyoung 	if (change != 0)
   1138   1.1    dyoung 		adapter->if_flags = ifp->if_flags;
   1139   1.1    dyoung 
   1140   1.1    dyoung 	if ((change & ~(IFF_CANTCHANGE|IFF_DEBUG)) != 0)
   1141   1.1    dyoung 		rc = ENETRESET;
   1142   1.1    dyoung 	else if ((change & (IFF_PROMISC | IFF_ALLMULTI)) != 0)
   1143   1.1    dyoung 		ixgbe_set_promisc(adapter);
   1144   1.1    dyoung 
   1145  1.23   msaitoh 	/* Set up VLAN support and filter */
   1146  1.23   msaitoh 	ixgbe_setup_vlan_hw_support(adapter);
   1147  1.23   msaitoh 
   1148   1.1    dyoung 	IXGBE_CORE_UNLOCK(adapter);
   1149   1.1    dyoung 
   1150   1.1    dyoung 	return rc;
   1151   1.1    dyoung }
   1152   1.1    dyoung 
   1153   1.1    dyoung /*********************************************************************
   1154   1.1    dyoung  *  Ioctl entry point
   1155   1.1    dyoung  *
   1156   1.1    dyoung  *  ixgbe_ioctl is called when the user wants to configure the
   1157   1.1    dyoung  *  interface.
   1158   1.1    dyoung  *
   1159   1.1    dyoung  *  return 0 on success, positive on failure
   1160   1.1    dyoung  **********************************************************************/
   1161   1.1    dyoung 
   1162   1.1    dyoung static int
   1163   1.1    dyoung ixgbe_ioctl(struct ifnet * ifp, u_long command, void *data)
   1164   1.1    dyoung {
   1165   1.1    dyoung 	struct adapter	*adapter = ifp->if_softc;
   1166  1.28   msaitoh 	struct ixgbe_hw *hw = &adapter->hw;
   1167   1.1    dyoung 	struct ifcapreq *ifcr = data;
   1168   1.1    dyoung 	struct ifreq	*ifr = data;
   1169   1.1    dyoung 	int             error = 0;
   1170   1.1    dyoung 	int l4csum_en;
   1171   1.1    dyoung 	const int l4csum = IFCAP_CSUM_TCPv4_Rx|IFCAP_CSUM_UDPv4_Rx|
   1172   1.1    dyoung 	     IFCAP_CSUM_TCPv6_Rx|IFCAP_CSUM_UDPv6_Rx;
   1173   1.1    dyoung 
   1174   1.1    dyoung 	switch (command) {
   1175   1.1    dyoung 	case SIOCSIFFLAGS:
   1176   1.1    dyoung 		IOCTL_DEBUGOUT("ioctl: SIOCSIFFLAGS (Set Interface Flags)");
   1177   1.1    dyoung 		break;
   1178   1.1    dyoung 	case SIOCADDMULTI:
   1179   1.1    dyoung 	case SIOCDELMULTI:
   1180   1.1    dyoung 		IOCTL_DEBUGOUT("ioctl: SIOC(ADD|DEL)MULTI");
   1181   1.1    dyoung 		break;
   1182   1.1    dyoung 	case SIOCSIFMEDIA:
   1183   1.1    dyoung 	case SIOCGIFMEDIA:
   1184   1.1    dyoung 		IOCTL_DEBUGOUT("ioctl: SIOCxIFMEDIA (Get/Set Interface Media)");
   1185   1.1    dyoung 		break;
   1186   1.1    dyoung 	case SIOCSIFCAP:
   1187   1.1    dyoung 		IOCTL_DEBUGOUT("ioctl: SIOCSIFCAP (Set Capabilities)");
   1188   1.1    dyoung 		break;
   1189   1.1    dyoung 	case SIOCSIFMTU:
   1190   1.1    dyoung 		IOCTL_DEBUGOUT("ioctl: SIOCSIFMTU (Set Interface MTU)");
   1191   1.1    dyoung 		break;
   1192   1.1    dyoung 	default:
   1193   1.1    dyoung 		IOCTL_DEBUGOUT1("ioctl: UNKNOWN (0x%X)\n", (int)command);
   1194   1.1    dyoung 		break;
   1195   1.1    dyoung 	}
   1196   1.1    dyoung 
   1197   1.1    dyoung 	switch (command) {
   1198   1.1    dyoung 	case SIOCSIFMEDIA:
   1199   1.1    dyoung 	case SIOCGIFMEDIA:
   1200   1.1    dyoung 		return ifmedia_ioctl(ifp, ifr, &adapter->media, command);
   1201  1.28   msaitoh 	case SIOCGI2C:
   1202  1.28   msaitoh 	{
   1203  1.28   msaitoh 		struct ixgbe_i2c_req	i2c;
   1204  1.28   msaitoh 		IOCTL_DEBUGOUT("ioctl: SIOCGI2C (Get I2C Data)");
   1205  1.28   msaitoh 		error = copyin(ifr->ifr_data, &i2c, sizeof(i2c));
   1206  1.33   msaitoh 		if (error != 0)
   1207  1.33   msaitoh 			break;
   1208  1.33   msaitoh 		if (i2c.dev_addr != 0xA0 && i2c.dev_addr != 0xA2) {
   1209  1.33   msaitoh 			error = EINVAL;
   1210  1.28   msaitoh 			break;
   1211  1.33   msaitoh 		}
   1212  1.33   msaitoh 		if (i2c.len > sizeof(i2c.data)) {
   1213  1.28   msaitoh 			error = EINVAL;
   1214  1.28   msaitoh 			break;
   1215  1.28   msaitoh 		}
   1216  1.33   msaitoh 
   1217  1.28   msaitoh 		hw->phy.ops.read_i2c_byte(hw, i2c.offset,
   1218  1.28   msaitoh 		    i2c.dev_addr, i2c.data);
   1219  1.28   msaitoh 		error = copyout(&i2c, ifr->ifr_data, sizeof(i2c));
   1220  1.28   msaitoh 		break;
   1221  1.28   msaitoh 	}
   1222   1.1    dyoung 	case SIOCSIFCAP:
   1223   1.1    dyoung 		/* Layer-4 Rx checksum offload has to be turned on and
   1224   1.1    dyoung 		 * off as a unit.
   1225   1.1    dyoung 		 */
   1226   1.1    dyoung 		l4csum_en = ifcr->ifcr_capenable & l4csum;
   1227   1.1    dyoung 		if (l4csum_en != l4csum && l4csum_en != 0)
   1228   1.1    dyoung 			return EINVAL;
   1229   1.1    dyoung 		/*FALLTHROUGH*/
   1230   1.1    dyoung 	case SIOCADDMULTI:
   1231   1.1    dyoung 	case SIOCDELMULTI:
   1232   1.1    dyoung 	case SIOCSIFFLAGS:
   1233   1.1    dyoung 	case SIOCSIFMTU:
   1234   1.1    dyoung 	default:
   1235   1.1    dyoung 		if ((error = ether_ioctl(ifp, command, data)) != ENETRESET)
   1236   1.1    dyoung 			return error;
   1237   1.1    dyoung 		if ((ifp->if_flags & IFF_RUNNING) == 0)
   1238   1.1    dyoung 			;
   1239   1.1    dyoung 		else if (command == SIOCSIFCAP || command == SIOCSIFMTU) {
   1240   1.1    dyoung 			IXGBE_CORE_LOCK(adapter);
   1241   1.1    dyoung 			ixgbe_init_locked(adapter);
   1242   1.1    dyoung 			IXGBE_CORE_UNLOCK(adapter);
   1243   1.1    dyoung 		} else if (command == SIOCADDMULTI || command == SIOCDELMULTI) {
   1244   1.1    dyoung 			/*
   1245   1.1    dyoung 			 * Multicast list has changed; set the hardware filter
   1246   1.1    dyoung 			 * accordingly.
   1247   1.1    dyoung 			 */
   1248   1.1    dyoung 			IXGBE_CORE_LOCK(adapter);
   1249   1.1    dyoung 			ixgbe_disable_intr(adapter);
   1250   1.1    dyoung 			ixgbe_set_multi(adapter);
   1251   1.1    dyoung 			ixgbe_enable_intr(adapter);
   1252   1.1    dyoung 			IXGBE_CORE_UNLOCK(adapter);
   1253   1.1    dyoung 		}
   1254   1.1    dyoung 		return 0;
   1255   1.1    dyoung 	}
   1256  1.28   msaitoh 
   1257  1.28   msaitoh 	return error;
   1258   1.1    dyoung }
   1259   1.1    dyoung 
   1260   1.1    dyoung /*********************************************************************
   1261   1.1    dyoung  *  Init entry point
   1262   1.1    dyoung  *
   1263   1.1    dyoung  *  This routine is used in two ways. It is used by the stack as
   1264   1.1    dyoung  *  init entry point in network interface structure. It is also used
   1265   1.1    dyoung  *  by the driver as a hw/sw initialization routine to get to a
   1266   1.1    dyoung  *  consistent state.
   1267   1.1    dyoung  *
   1268   1.1    dyoung  *  return 0 on success, positive on failure
   1269   1.1    dyoung  **********************************************************************/
   1270   1.1    dyoung #define IXGBE_MHADD_MFS_SHIFT 16
   1271   1.1    dyoung 
   1272   1.1    dyoung static void
   1273   1.1    dyoung ixgbe_init_locked(struct adapter *adapter)
   1274   1.1    dyoung {
   1275   1.1    dyoung 	struct ifnet   *ifp = adapter->ifp;
   1276   1.1    dyoung 	device_t 	dev = adapter->dev;
   1277   1.1    dyoung 	struct ixgbe_hw *hw = &adapter->hw;
   1278   1.1    dyoung 	u32		k, txdctl, mhadd, gpie;
   1279   1.1    dyoung 	u32		rxdctl, rxctrl;
   1280   1.1    dyoung 
   1281   1.1    dyoung 	/* XXX check IFF_UP and IFF_RUNNING, power-saving state! */
   1282   1.1    dyoung 
   1283   1.1    dyoung 	KASSERT(mutex_owned(&adapter->core_mtx));
   1284  1.33   msaitoh 	INIT_DEBUGOUT("ixgbe_init_locked: begin");
   1285   1.1    dyoung 	hw->adapter_stopped = FALSE;
   1286   1.1    dyoung 	ixgbe_stop_adapter(hw);
   1287   1.1    dyoung         callout_stop(&adapter->timer);
   1288   1.1    dyoung 
   1289   1.1    dyoung 	/* XXX I moved this here from the SIOCSIFMTU case in ixgbe_ioctl(). */
   1290   1.1    dyoung 	adapter->max_frame_size =
   1291   1.1    dyoung 		ifp->if_mtu + ETHER_HDR_LEN + ETHER_CRC_LEN;
   1292   1.1    dyoung 
   1293   1.1    dyoung         /* reprogram the RAR[0] in case user changed it. */
   1294   1.1    dyoung         ixgbe_set_rar(hw, 0, adapter->hw.mac.addr, 0, IXGBE_RAH_AV);
   1295   1.1    dyoung 
   1296   1.1    dyoung 	/* Get the latest mac address, User can use a LAA */
   1297   1.1    dyoung 	memcpy(hw->mac.addr, CLLADDR(adapter->ifp->if_sadl),
   1298   1.1    dyoung 	    IXGBE_ETH_LENGTH_OF_ADDRESS);
   1299   1.1    dyoung 	ixgbe_set_rar(hw, 0, hw->mac.addr, 0, 1);
   1300   1.1    dyoung 	hw->addr_ctrl.rar_used_count = 1;
   1301   1.1    dyoung 
   1302   1.1    dyoung 	/* Prepare transmit descriptors and buffers */
   1303   1.1    dyoung 	if (ixgbe_setup_transmit_structures(adapter)) {
   1304   1.1    dyoung 		device_printf(dev,"Could not setup transmit structures\n");
   1305   1.1    dyoung 		ixgbe_stop(adapter);
   1306   1.1    dyoung 		return;
   1307   1.1    dyoung 	}
   1308   1.1    dyoung 
   1309   1.1    dyoung 	ixgbe_init_hw(hw);
   1310   1.1    dyoung 	ixgbe_initialize_transmit_units(adapter);
   1311   1.1    dyoung 
   1312   1.1    dyoung 	/* Setup Multicast table */
   1313   1.1    dyoung 	ixgbe_set_multi(adapter);
   1314   1.1    dyoung 
   1315   1.1    dyoung 	/*
   1316   1.1    dyoung 	** Determine the correct mbuf pool
   1317  1.26   msaitoh 	** for doing jumbo frames
   1318   1.1    dyoung 	*/
   1319   1.1    dyoung 	if (adapter->max_frame_size <= 2048)
   1320   1.1    dyoung 		adapter->rx_mbuf_sz = MCLBYTES;
   1321   1.1    dyoung 	else if (adapter->max_frame_size <= 4096)
   1322   1.1    dyoung 		adapter->rx_mbuf_sz = MJUMPAGESIZE;
   1323   1.1    dyoung 	else if (adapter->max_frame_size <= 9216)
   1324   1.1    dyoung 		adapter->rx_mbuf_sz = MJUM9BYTES;
   1325   1.1    dyoung 	else
   1326   1.1    dyoung 		adapter->rx_mbuf_sz = MJUM16BYTES;
   1327   1.1    dyoung 
   1328   1.1    dyoung 	/* Prepare receive descriptors and buffers */
   1329   1.1    dyoung 	if (ixgbe_setup_receive_structures(adapter)) {
   1330   1.1    dyoung 		device_printf(dev,"Could not setup receive structures\n");
   1331   1.1    dyoung 		ixgbe_stop(adapter);
   1332   1.1    dyoung 		return;
   1333   1.1    dyoung 	}
   1334   1.1    dyoung 
   1335   1.1    dyoung 	/* Configure RX settings */
   1336   1.1    dyoung 	ixgbe_initialize_receive_units(adapter);
   1337   1.1    dyoung 
   1338   1.1    dyoung 	gpie = IXGBE_READ_REG(&adapter->hw, IXGBE_GPIE);
   1339   1.1    dyoung 
   1340   1.1    dyoung 	/* Enable Fan Failure Interrupt */
   1341   1.1    dyoung 	gpie |= IXGBE_SDP1_GPIEN;
   1342   1.1    dyoung 
   1343  1.35   msaitoh 	/* Add for Module detection */
   1344   1.1    dyoung 	if (hw->mac.type == ixgbe_mac_82599EB)
   1345   1.1    dyoung 		gpie |= IXGBE_SDP2_GPIEN;
   1346   1.1    dyoung 
   1347  1.24   msaitoh 	/* Thermal Failure Detection */
   1348  1.24   msaitoh 	if (hw->mac.type == ixgbe_mac_X540)
   1349  1.24   msaitoh 		gpie |= IXGBE_SDP0_GPIEN;
   1350  1.24   msaitoh 
   1351   1.1    dyoung 	if (adapter->msix > 1) {
   1352   1.1    dyoung 		/* Enable Enhanced MSIX mode */
   1353   1.1    dyoung 		gpie |= IXGBE_GPIE_MSIX_MODE;
   1354   1.1    dyoung 		gpie |= IXGBE_GPIE_EIAME | IXGBE_GPIE_PBA_SUPPORT |
   1355   1.1    dyoung 		    IXGBE_GPIE_OCD;
   1356   1.1    dyoung 	}
   1357   1.1    dyoung 	IXGBE_WRITE_REG(hw, IXGBE_GPIE, gpie);
   1358   1.1    dyoung 
   1359   1.1    dyoung 	/* Set MTU size */
   1360   1.1    dyoung 	if (ifp->if_mtu > ETHERMTU) {
   1361   1.1    dyoung 		mhadd = IXGBE_READ_REG(hw, IXGBE_MHADD);
   1362   1.1    dyoung 		mhadd &= ~IXGBE_MHADD_MFS_MASK;
   1363   1.1    dyoung 		mhadd |= adapter->max_frame_size << IXGBE_MHADD_MFS_SHIFT;
   1364   1.1    dyoung 		IXGBE_WRITE_REG(hw, IXGBE_MHADD, mhadd);
   1365   1.1    dyoung 	}
   1366   1.1    dyoung 
   1367   1.1    dyoung 	/* Now enable all the queues */
   1368   1.1    dyoung 
   1369   1.1    dyoung 	for (int i = 0; i < adapter->num_queues; i++) {
   1370   1.1    dyoung 		txdctl = IXGBE_READ_REG(hw, IXGBE_TXDCTL(i));
   1371   1.1    dyoung 		txdctl |= IXGBE_TXDCTL_ENABLE;
   1372   1.1    dyoung 		/* Set WTHRESH to 8, burst writeback */
   1373   1.1    dyoung 		txdctl |= (8 << 16);
   1374  1.25   msaitoh 		/*
   1375  1.25   msaitoh 		 * When the internal queue falls below PTHRESH (32),
   1376  1.25   msaitoh 		 * start prefetching as long as there are at least
   1377  1.25   msaitoh 		 * HTHRESH (1) buffers ready. The values are taken
   1378  1.25   msaitoh 		 * from the Intel linux driver 3.8.21.
   1379  1.25   msaitoh 		 * Prefetching enables tx line rate even with 1 queue.
   1380  1.25   msaitoh 		 */
   1381  1.25   msaitoh 		txdctl |= (32 << 0) | (1 << 8);
   1382   1.1    dyoung 		IXGBE_WRITE_REG(hw, IXGBE_TXDCTL(i), txdctl);
   1383   1.1    dyoung 	}
   1384   1.1    dyoung 
   1385   1.1    dyoung 	for (int i = 0; i < adapter->num_queues; i++) {
   1386   1.1    dyoung 		rxdctl = IXGBE_READ_REG(hw, IXGBE_RXDCTL(i));
   1387   1.1    dyoung 		if (hw->mac.type == ixgbe_mac_82598EB) {
   1388   1.1    dyoung 			/*
   1389   1.1    dyoung 			** PTHRESH = 21
   1390   1.1    dyoung 			** HTHRESH = 4
   1391   1.1    dyoung 			** WTHRESH = 8
   1392   1.1    dyoung 			*/
   1393   1.1    dyoung 			rxdctl &= ~0x3FFFFF;
   1394   1.1    dyoung 			rxdctl |= 0x080420;
   1395   1.1    dyoung 		}
   1396   1.1    dyoung 		rxdctl |= IXGBE_RXDCTL_ENABLE;
   1397   1.1    dyoung 		IXGBE_WRITE_REG(hw, IXGBE_RXDCTL(i), rxdctl);
   1398   1.1    dyoung 		/* XXX I don't trust this loop, and I don't trust the
   1399   1.1    dyoung 		 * XXX memory barrier.  What is this meant to do? --dyoung
   1400   1.1    dyoung 		 */
   1401   1.1    dyoung 		for (k = 0; k < 10; k++) {
   1402   1.1    dyoung 			if (IXGBE_READ_REG(hw, IXGBE_RXDCTL(i)) &
   1403   1.1    dyoung 			    IXGBE_RXDCTL_ENABLE)
   1404   1.1    dyoung 				break;
   1405   1.1    dyoung 			else
   1406   1.1    dyoung 				msec_delay(1);
   1407   1.1    dyoung 		}
   1408   1.1    dyoung 		wmb();
   1409  1.22   msaitoh #ifdef DEV_NETMAP
   1410  1.22   msaitoh 		/*
   1411  1.22   msaitoh 		 * In netmap mode, we must preserve the buffers made
   1412  1.22   msaitoh 		 * available to userspace before the if_init()
   1413  1.22   msaitoh 		 * (this is true by default on the TX side, because
   1414  1.22   msaitoh 		 * init makes all buffers available to userspace).
   1415  1.22   msaitoh 		 *
   1416  1.22   msaitoh 		 * netmap_reset() and the device specific routines
   1417  1.22   msaitoh 		 * (e.g. ixgbe_setup_receive_rings()) map these
   1418  1.22   msaitoh 		 * buffers at the end of the NIC ring, so here we
   1419  1.22   msaitoh 		 * must set the RDT (tail) register to make sure
   1420  1.22   msaitoh 		 * they are not overwritten.
   1421  1.22   msaitoh 		 *
   1422  1.22   msaitoh 		 * In this driver the NIC ring starts at RDH = 0,
   1423  1.22   msaitoh 		 * RDT points to the last slot available for reception (?),
   1424  1.22   msaitoh 		 * so RDT = num_rx_desc - 1 means the whole ring is available.
   1425  1.22   msaitoh 		 */
   1426  1.22   msaitoh 		if (ifp->if_capenable & IFCAP_NETMAP) {
   1427  1.22   msaitoh 			struct netmap_adapter *na = NA(adapter->ifp);
   1428  1.22   msaitoh 			struct netmap_kring *kring = &na->rx_rings[i];
   1429  1.33   msaitoh 			int t = na->num_rx_desc - 1 - nm_kr_rxspace(kring);
   1430  1.22   msaitoh 
   1431  1.22   msaitoh 			IXGBE_WRITE_REG(hw, IXGBE_RDT(i), t);
   1432  1.22   msaitoh 		} else
   1433  1.22   msaitoh #endif /* DEV_NETMAP */
   1434   1.1    dyoung 		IXGBE_WRITE_REG(hw, IXGBE_RDT(i), adapter->num_rx_desc - 1);
   1435   1.1    dyoung 	}
   1436   1.1    dyoung 
   1437   1.1    dyoung 	/* Enable Receive engine */
   1438   1.1    dyoung 	rxctrl = IXGBE_READ_REG(hw, IXGBE_RXCTRL);
   1439   1.1    dyoung 	if (hw->mac.type == ixgbe_mac_82598EB)
   1440   1.1    dyoung 		rxctrl |= IXGBE_RXCTRL_DMBYPS;
   1441   1.1    dyoung 	rxctrl |= IXGBE_RXCTRL_RXEN;
   1442   1.1    dyoung 	ixgbe_enable_rx_dma(hw, rxctrl);
   1443   1.1    dyoung 
   1444   1.1    dyoung 	callout_reset(&adapter->timer, hz, ixgbe_local_timer, adapter);
   1445   1.1    dyoung 
   1446   1.1    dyoung 	/* Set up MSI/X routing */
   1447   1.1    dyoung 	if (ixgbe_enable_msix)  {
   1448   1.1    dyoung 		ixgbe_configure_ivars(adapter);
   1449   1.1    dyoung 		/* Set up auto-mask */
   1450   1.1    dyoung 		if (hw->mac.type == ixgbe_mac_82598EB)
   1451   1.1    dyoung 			IXGBE_WRITE_REG(hw, IXGBE_EIAM, IXGBE_EICS_RTX_QUEUE);
   1452   1.1    dyoung 		else {
   1453   1.1    dyoung 			IXGBE_WRITE_REG(hw, IXGBE_EIAM_EX(0), 0xFFFFFFFF);
   1454   1.1    dyoung 			IXGBE_WRITE_REG(hw, IXGBE_EIAM_EX(1), 0xFFFFFFFF);
   1455   1.1    dyoung 		}
   1456   1.1    dyoung 	} else {  /* Simple settings for Legacy/MSI */
   1457   1.1    dyoung                 ixgbe_set_ivar(adapter, 0, 0, 0);
   1458   1.1    dyoung                 ixgbe_set_ivar(adapter, 0, 0, 1);
   1459   1.1    dyoung 		IXGBE_WRITE_REG(hw, IXGBE_EIAM, IXGBE_EICS_RTX_QUEUE);
   1460   1.1    dyoung 	}
   1461   1.1    dyoung 
   1462   1.1    dyoung #ifdef IXGBE_FDIR
   1463   1.1    dyoung 	/* Init Flow director */
   1464  1.24   msaitoh 	if (hw->mac.type != ixgbe_mac_82598EB) {
   1465  1.25   msaitoh 		u32 hdrm = 32 << fdir_pballoc;
   1466  1.24   msaitoh 
   1467  1.24   msaitoh 		hw->mac.ops.setup_rxpba(hw, 0, hdrm, PBA_STRATEGY_EQUAL);
   1468   1.1    dyoung 		ixgbe_init_fdir_signature_82599(&adapter->hw, fdir_pballoc);
   1469  1.24   msaitoh 	}
   1470   1.1    dyoung #endif
   1471   1.1    dyoung 
   1472   1.1    dyoung 	/*
   1473   1.1    dyoung 	** Check on any SFP devices that
   1474   1.1    dyoung 	** need to be kick-started
   1475   1.1    dyoung 	*/
   1476   1.1    dyoung 	if (hw->phy.type == ixgbe_phy_none) {
   1477   1.1    dyoung 		int err = hw->phy.ops.identify(hw);
   1478   1.1    dyoung 		if (err == IXGBE_ERR_SFP_NOT_SUPPORTED) {
   1479   1.1    dyoung                 	device_printf(dev,
   1480   1.1    dyoung 			    "Unsupported SFP+ module type was detected.\n");
   1481   1.1    dyoung 			return;
   1482   1.1    dyoung         	}
   1483   1.1    dyoung 	}
   1484   1.1    dyoung 
   1485   1.1    dyoung 	/* Set moderation on the Link interrupt */
   1486   1.1    dyoung 	IXGBE_WRITE_REG(hw, IXGBE_EITR(adapter->linkvec), IXGBE_LINK_ITR);
   1487   1.1    dyoung 
   1488   1.1    dyoung 	/* Config/Enable Link */
   1489   1.1    dyoung 	ixgbe_config_link(adapter);
   1490   1.1    dyoung 
   1491  1.25   msaitoh 	/* Hardware Packet Buffer & Flow Control setup */
   1492  1.25   msaitoh 	{
   1493  1.25   msaitoh 		u32 rxpb, frame, size, tmp;
   1494  1.25   msaitoh 
   1495  1.25   msaitoh 		frame = adapter->max_frame_size;
   1496  1.25   msaitoh 
   1497  1.25   msaitoh 		/* Calculate High Water */
   1498  1.25   msaitoh 		if (hw->mac.type == ixgbe_mac_X540)
   1499  1.25   msaitoh 			tmp = IXGBE_DV_X540(frame, frame);
   1500  1.25   msaitoh 		else
   1501  1.25   msaitoh 			tmp = IXGBE_DV(frame, frame);
   1502  1.25   msaitoh 		size = IXGBE_BT2KB(tmp);
   1503  1.25   msaitoh 		rxpb = IXGBE_READ_REG(hw, IXGBE_RXPBSIZE(0)) >> 10;
   1504  1.25   msaitoh 		hw->fc.high_water[0] = rxpb - size;
   1505  1.25   msaitoh 
   1506  1.25   msaitoh 		/* Now calculate Low Water */
   1507  1.25   msaitoh 		if (hw->mac.type == ixgbe_mac_X540)
   1508  1.25   msaitoh 			tmp = IXGBE_LOW_DV_X540(frame);
   1509  1.25   msaitoh 		else
   1510  1.25   msaitoh 			tmp = IXGBE_LOW_DV(frame);
   1511  1.25   msaitoh 		hw->fc.low_water[0] = IXGBE_BT2KB(tmp);
   1512  1.25   msaitoh 
   1513  1.28   msaitoh 		hw->fc.requested_mode = adapter->fc;
   1514  1.25   msaitoh 		hw->fc.pause_time = IXGBE_FC_PAUSE;
   1515  1.25   msaitoh 		hw->fc.send_xon = TRUE;
   1516  1.25   msaitoh 	}
   1517  1.25   msaitoh 	/* Initialize the FC settings */
   1518  1.25   msaitoh 	ixgbe_start_hw(hw);
   1519  1.25   msaitoh 
   1520  1.33   msaitoh 	/* Set up VLAN support and filter */
   1521  1.33   msaitoh 	ixgbe_setup_vlan_hw_support(adapter);
   1522  1.33   msaitoh 
   1523   1.1    dyoung 	/* And now turn on interrupts */
   1524   1.1    dyoung 	ixgbe_enable_intr(adapter);
   1525   1.1    dyoung 
   1526   1.1    dyoung 	/* Now inform the stack we're ready */
   1527   1.1    dyoung 	ifp->if_flags |= IFF_RUNNING;
   1528   1.1    dyoung 
   1529   1.1    dyoung 	return;
   1530   1.1    dyoung }
   1531   1.1    dyoung 
   1532   1.1    dyoung static int
   1533   1.1    dyoung ixgbe_init(struct ifnet *ifp)
   1534   1.1    dyoung {
   1535   1.1    dyoung 	struct adapter *adapter = ifp->if_softc;
   1536   1.1    dyoung 
   1537   1.1    dyoung 	IXGBE_CORE_LOCK(adapter);
   1538   1.1    dyoung 	ixgbe_init_locked(adapter);
   1539   1.1    dyoung 	IXGBE_CORE_UNLOCK(adapter);
   1540   1.1    dyoung 	return 0;	/* XXX ixgbe_init_locked cannot fail?  really? */
   1541   1.1    dyoung }
   1542   1.1    dyoung 
   1543   1.1    dyoung 
   1544   1.1    dyoung /*
   1545   1.1    dyoung **
   1546   1.1    dyoung ** MSIX Interrupt Handlers and Tasklets
   1547   1.1    dyoung **
   1548   1.1    dyoung */
   1549   1.1    dyoung 
   1550   1.1    dyoung static inline void
   1551   1.1    dyoung ixgbe_enable_queue(struct adapter *adapter, u32 vector)
   1552   1.1    dyoung {
   1553   1.1    dyoung 	struct ixgbe_hw *hw = &adapter->hw;
   1554  1.13  christos 	u64	queue = (u64)(1ULL << vector);
   1555   1.1    dyoung 	u32	mask;
   1556   1.1    dyoung 
   1557   1.1    dyoung 	if (hw->mac.type == ixgbe_mac_82598EB) {
   1558   1.1    dyoung                 mask = (IXGBE_EIMS_RTX_QUEUE & queue);
   1559   1.1    dyoung                 IXGBE_WRITE_REG(hw, IXGBE_EIMS, mask);
   1560   1.1    dyoung 	} else {
   1561   1.1    dyoung                 mask = (queue & 0xFFFFFFFF);
   1562   1.1    dyoung                 if (mask)
   1563   1.1    dyoung                         IXGBE_WRITE_REG(hw, IXGBE_EIMS_EX(0), mask);
   1564   1.1    dyoung                 mask = (queue >> 32);
   1565   1.1    dyoung                 if (mask)
   1566   1.1    dyoung                         IXGBE_WRITE_REG(hw, IXGBE_EIMS_EX(1), mask);
   1567   1.1    dyoung 	}
   1568   1.1    dyoung }
   1569   1.1    dyoung 
   1570  1.11     joerg __unused static inline void
   1571   1.1    dyoung ixgbe_disable_queue(struct adapter *adapter, u32 vector)
   1572   1.1    dyoung {
   1573   1.1    dyoung 	struct ixgbe_hw *hw = &adapter->hw;
   1574  1.13  christos 	u64	queue = (u64)(1ULL << vector);
   1575   1.1    dyoung 	u32	mask;
   1576   1.1    dyoung 
   1577   1.1    dyoung 	if (hw->mac.type == ixgbe_mac_82598EB) {
   1578   1.1    dyoung                 mask = (IXGBE_EIMS_RTX_QUEUE & queue);
   1579   1.1    dyoung                 IXGBE_WRITE_REG(hw, IXGBE_EIMC, mask);
   1580   1.1    dyoung 	} else {
   1581   1.1    dyoung                 mask = (queue & 0xFFFFFFFF);
   1582   1.1    dyoung                 if (mask)
   1583   1.1    dyoung                         IXGBE_WRITE_REG(hw, IXGBE_EIMC_EX(0), mask);
   1584   1.1    dyoung                 mask = (queue >> 32);
   1585   1.1    dyoung                 if (mask)
   1586   1.1    dyoung                         IXGBE_WRITE_REG(hw, IXGBE_EIMC_EX(1), mask);
   1587   1.1    dyoung 	}
   1588   1.1    dyoung }
   1589   1.1    dyoung 
   1590   1.1    dyoung static void
   1591   1.1    dyoung ixgbe_handle_que(void *context)
   1592   1.1    dyoung {
   1593   1.1    dyoung 	struct ix_queue *que = context;
   1594   1.1    dyoung 	struct adapter  *adapter = que->adapter;
   1595   1.1    dyoung 	struct tx_ring  *txr = que->txr;
   1596   1.1    dyoung 	struct ifnet    *ifp = adapter->ifp;
   1597   1.1    dyoung 
   1598   1.1    dyoung 	adapter->handleq.ev_count++;
   1599   1.1    dyoung 
   1600   1.1    dyoung 	if (ifp->if_flags & IFF_RUNNING) {
   1601  1.33   msaitoh 		ixgbe_rxeof(que);
   1602   1.1    dyoung 		IXGBE_TX_LOCK(txr);
   1603   1.1    dyoung 		ixgbe_txeof(txr);
   1604  1.28   msaitoh #ifndef IXGBE_LEGACY_TX
   1605   1.1    dyoung 		if (!drbr_empty(ifp, txr->br))
   1606  1.33   msaitoh 			ixgbe_mq_start_locked(ifp, txr);
   1607   1.1    dyoung #else
   1608   1.1    dyoung 		if (!IFQ_IS_EMPTY(&ifp->if_snd))
   1609   1.1    dyoung 			ixgbe_start_locked(txr, ifp);
   1610   1.1    dyoung #endif
   1611   1.1    dyoung 		IXGBE_TX_UNLOCK(txr);
   1612   1.1    dyoung 	}
   1613   1.1    dyoung 
   1614   1.1    dyoung 	/* Reenable this interrupt */
   1615  1.33   msaitoh 	if (que->res != NULL)
   1616  1.33   msaitoh 		ixgbe_enable_queue(adapter, que->msix);
   1617  1.33   msaitoh 	else
   1618  1.33   msaitoh 		ixgbe_enable_intr(adapter);
   1619   1.1    dyoung 	return;
   1620   1.1    dyoung }
   1621   1.1    dyoung 
   1622   1.1    dyoung 
   1623   1.1    dyoung /*********************************************************************
   1624   1.1    dyoung  *
   1625   1.1    dyoung  *  Legacy Interrupt Service routine
   1626   1.1    dyoung  *
   1627   1.1    dyoung  **********************************************************************/
   1628   1.1    dyoung 
   1629   1.1    dyoung static int
   1630   1.1    dyoung ixgbe_legacy_irq(void *arg)
   1631   1.1    dyoung {
   1632   1.1    dyoung 	struct ix_queue *que = arg;
   1633   1.1    dyoung 	struct adapter	*adapter = que->adapter;
   1634  1.33   msaitoh 	struct ixgbe_hw	*hw = &adapter->hw;
   1635  1.35   msaitoh 	struct ifnet    *ifp = adapter->ifp;
   1636   1.1    dyoung 	struct 		tx_ring *txr = adapter->tx_rings;
   1637  1.33   msaitoh 	bool		more = false;
   1638  1.33   msaitoh 	u32       	reg_eicr;
   1639   1.1    dyoung 
   1640  1.35   msaitoh 
   1641   1.1    dyoung 	reg_eicr = IXGBE_READ_REG(hw, IXGBE_EICR);
   1642   1.1    dyoung 
   1643   1.1    dyoung 	adapter->stats.legint.ev_count++;
   1644   1.1    dyoung 	++que->irqs;
   1645   1.1    dyoung 	if (reg_eicr == 0) {
   1646   1.1    dyoung 		adapter->stats.intzero.ev_count++;
   1647  1.15   msaitoh 		if ((ifp->if_flags & IFF_UP) != 0)
   1648  1.15   msaitoh 			ixgbe_enable_intr(adapter);
   1649   1.1    dyoung 		return 0;
   1650   1.1    dyoung 	}
   1651   1.1    dyoung 
   1652  1.15   msaitoh 	if ((ifp->if_flags & IFF_RUNNING) != 0) {
   1653  1.37     ozaki #ifdef __NetBSD__
   1654  1.37     ozaki 		/* Don't run ixgbe_rxeof in interrupt context */
   1655  1.37     ozaki 		more = true;
   1656  1.37     ozaki #else
   1657  1.33   msaitoh 		more = ixgbe_rxeof(que);
   1658  1.37     ozaki #endif
   1659   1.1    dyoung 
   1660  1.15   msaitoh 		IXGBE_TX_LOCK(txr);
   1661  1.33   msaitoh 		ixgbe_txeof(txr);
   1662  1.33   msaitoh #ifdef IXGBE_LEGACY_TX
   1663  1.33   msaitoh 		if (!IFQ_IS_EMPTY(&ifp->if_snd))
   1664  1.33   msaitoh 			ixgbe_start_locked(txr, ifp);
   1665  1.33   msaitoh #else
   1666  1.33   msaitoh 		if (!drbr_empty(ifp, txr->br))
   1667  1.33   msaitoh 			ixgbe_mq_start_locked(ifp, txr);
   1668  1.33   msaitoh #endif
   1669  1.15   msaitoh 		IXGBE_TX_UNLOCK(txr);
   1670  1.15   msaitoh 	}
   1671   1.1    dyoung 
   1672   1.1    dyoung 	/* Check for fan failure */
   1673   1.1    dyoung 	if ((hw->phy.media_type == ixgbe_media_type_copper) &&
   1674   1.1    dyoung 	    (reg_eicr & IXGBE_EICR_GPI_SDP1)) {
   1675   1.1    dyoung                 device_printf(adapter->dev, "\nCRITICAL: FAN FAILURE!! "
   1676   1.1    dyoung 		    "REPLACE IMMEDIATELY!!\n");
   1677   1.1    dyoung 		IXGBE_WRITE_REG(hw, IXGBE_EIMS, IXGBE_EICR_GPI_SDP1);
   1678   1.1    dyoung 	}
   1679   1.1    dyoung 
   1680   1.1    dyoung 	/* Link status change */
   1681   1.1    dyoung 	if (reg_eicr & IXGBE_EICR_LSC)
   1682   1.1    dyoung 		softint_schedule(adapter->link_si);
   1683   1.1    dyoung 
   1684  1.33   msaitoh 	if (more)
   1685  1.33   msaitoh #ifndef IXGBE_LEGACY_TX
   1686  1.33   msaitoh 		softint_schedule(txr->txq_si);
   1687  1.33   msaitoh #else
   1688  1.33   msaitoh 		softint_schedule(que->que_si);
   1689  1.33   msaitoh #endif
   1690  1.33   msaitoh 	else
   1691  1.33   msaitoh 		ixgbe_enable_intr(adapter);
   1692   1.1    dyoung 	return 1;
   1693   1.1    dyoung }
   1694   1.1    dyoung 
   1695   1.1    dyoung 
   1696   1.1    dyoung #if defined(NETBSD_MSI_OR_MSIX)
   1697   1.1    dyoung /*********************************************************************
   1698   1.1    dyoung  *
   1699  1.22   msaitoh  *  MSIX Queue Interrupt Service routine
   1700   1.1    dyoung  *
   1701   1.1    dyoung  **********************************************************************/
   1702  1.34   msaitoh static int
   1703   1.1    dyoung ixgbe_msix_que(void *arg)
   1704   1.1    dyoung {
   1705   1.1    dyoung 	struct ix_queue	*que = arg;
   1706   1.1    dyoung 	struct adapter  *adapter = que->adapter;
   1707  1.33   msaitoh 	struct ifnet    *ifp = adapter->ifp;
   1708   1.1    dyoung 	struct tx_ring	*txr = que->txr;
   1709   1.1    dyoung 	struct rx_ring	*rxr = que->rxr;
   1710  1.33   msaitoh 	bool		more;
   1711   1.1    dyoung 	u32		newitr = 0;
   1712   1.1    dyoung 
   1713  1.33   msaitoh 	/* Protect against spurious interrupts */
   1714  1.33   msaitoh 	if ((ifp->if_flags & IFF_RUNNING) == 0)
   1715  1.34   msaitoh 		return 0;
   1716  1.33   msaitoh 
   1717  1.24   msaitoh 	ixgbe_disable_queue(adapter, que->msix);
   1718   1.1    dyoung 	++que->irqs;
   1719   1.1    dyoung 
   1720  1.37     ozaki #ifdef __NetBSD__
   1721  1.37     ozaki 	/* Don't run ixgbe_rxeof in interrupt context */
   1722  1.37     ozaki 	more = true;
   1723  1.37     ozaki #else
   1724  1.33   msaitoh 	more = ixgbe_rxeof(que);
   1725  1.37     ozaki #endif
   1726   1.1    dyoung 
   1727   1.1    dyoung 	IXGBE_TX_LOCK(txr);
   1728  1.33   msaitoh 	ixgbe_txeof(txr);
   1729  1.28   msaitoh #ifdef IXGBE_LEGACY_TX
   1730  1.24   msaitoh 	if (!IFQ_IS_EMPTY(&adapter->ifp->if_snd))
   1731  1.34   msaitoh 		ixgbe_start_locked(txr, ifp);
   1732  1.22   msaitoh #else
   1733  1.33   msaitoh 	if (!drbr_empty(ifp, txr->br))
   1734  1.33   msaitoh 		ixgbe_mq_start_locked(ifp, txr);
   1735  1.22   msaitoh #endif
   1736   1.1    dyoung 	IXGBE_TX_UNLOCK(txr);
   1737   1.1    dyoung 
   1738   1.1    dyoung 	/* Do AIM now? */
   1739   1.1    dyoung 
   1740   1.1    dyoung 	if (ixgbe_enable_aim == FALSE)
   1741   1.1    dyoung 		goto no_calc;
   1742   1.1    dyoung 	/*
   1743   1.1    dyoung 	** Do Adaptive Interrupt Moderation:
   1744   1.1    dyoung         **  - Write out last calculated setting
   1745   1.1    dyoung 	**  - Calculate based on average size over
   1746   1.1    dyoung 	**    the last interval.
   1747   1.1    dyoung 	*/
   1748   1.1    dyoung         if (que->eitr_setting)
   1749   1.1    dyoung                 IXGBE_WRITE_REG(&adapter->hw,
   1750   1.1    dyoung                     IXGBE_EITR(que->msix), que->eitr_setting);
   1751   1.1    dyoung 
   1752   1.1    dyoung         que->eitr_setting = 0;
   1753   1.1    dyoung 
   1754   1.1    dyoung         /* Idle, do nothing */
   1755   1.1    dyoung         if ((txr->bytes == 0) && (rxr->bytes == 0))
   1756   1.1    dyoung                 goto no_calc;
   1757   1.1    dyoung 
   1758   1.1    dyoung 	if ((txr->bytes) && (txr->packets))
   1759   1.1    dyoung                	newitr = txr->bytes/txr->packets;
   1760   1.1    dyoung 	if ((rxr->bytes) && (rxr->packets))
   1761   1.1    dyoung 		newitr = max(newitr,
   1762   1.1    dyoung 		    (rxr->bytes / rxr->packets));
   1763   1.1    dyoung 	newitr += 24; /* account for hardware frame, crc */
   1764   1.1    dyoung 
   1765   1.1    dyoung 	/* set an upper boundary */
   1766   1.1    dyoung 	newitr = min(newitr, 3000);
   1767   1.1    dyoung 
   1768   1.1    dyoung 	/* Be nice to the mid range */
   1769   1.1    dyoung 	if ((newitr > 300) && (newitr < 1200))
   1770   1.1    dyoung 		newitr = (newitr / 3);
   1771   1.1    dyoung 	else
   1772   1.1    dyoung 		newitr = (newitr / 2);
   1773   1.1    dyoung 
   1774   1.1    dyoung         if (adapter->hw.mac.type == ixgbe_mac_82598EB)
   1775   1.1    dyoung                 newitr |= newitr << 16;
   1776   1.1    dyoung         else
   1777   1.1    dyoung                 newitr |= IXGBE_EITR_CNT_WDIS;
   1778   1.1    dyoung 
   1779   1.1    dyoung         /* save for next interrupt */
   1780   1.1    dyoung         que->eitr_setting = newitr;
   1781   1.1    dyoung 
   1782   1.1    dyoung         /* Reset state */
   1783   1.1    dyoung         txr->bytes = 0;
   1784   1.1    dyoung         txr->packets = 0;
   1785   1.1    dyoung         rxr->bytes = 0;
   1786   1.1    dyoung         rxr->packets = 0;
   1787   1.1    dyoung 
   1788   1.1    dyoung no_calc:
   1789  1.33   msaitoh 	if (more)
   1790   1.1    dyoung 		softint_schedule(que->que_si);
   1791  1.33   msaitoh 	else
   1792   1.1    dyoung 		ixgbe_enable_queue(adapter, que->msix);
   1793  1.34   msaitoh 	return 1;
   1794   1.1    dyoung }
   1795   1.1    dyoung 
   1796   1.1    dyoung 
   1797  1.34   msaitoh static int
   1798   1.1    dyoung ixgbe_msix_link(void *arg)
   1799   1.1    dyoung {
   1800   1.1    dyoung 	struct adapter	*adapter = arg;
   1801   1.1    dyoung 	struct ixgbe_hw *hw = &adapter->hw;
   1802   1.1    dyoung 	u32		reg_eicr;
   1803   1.1    dyoung 
   1804   1.1    dyoung 	++adapter->link_irq.ev_count;
   1805   1.1    dyoung 
   1806   1.1    dyoung 	/* First get the cause */
   1807   1.1    dyoung 	reg_eicr = IXGBE_READ_REG(hw, IXGBE_EICS);
   1808  1.33   msaitoh 	/* Be sure the queue bits are not cleared */
   1809  1.33   msaitoh 	reg_eicr &= ~IXGBE_EICR_RTX_QUEUE;
   1810   1.1    dyoung 	/* Clear interrupt with write */
   1811   1.1    dyoung 	IXGBE_WRITE_REG(hw, IXGBE_EICR, reg_eicr);
   1812   1.1    dyoung 
   1813   1.1    dyoung 	/* Link status change */
   1814   1.1    dyoung 	if (reg_eicr & IXGBE_EICR_LSC)
   1815   1.1    dyoung 		softint_schedule(adapter->link_si);
   1816   1.1    dyoung 
   1817   1.1    dyoung 	if (adapter->hw.mac.type != ixgbe_mac_82598EB) {
   1818   1.1    dyoung #ifdef IXGBE_FDIR
   1819   1.1    dyoung 		if (reg_eicr & IXGBE_EICR_FLOW_DIR) {
   1820   1.1    dyoung 			/* This is probably overkill :) */
   1821   1.1    dyoung 			if (!atomic_cmpset_int(&adapter->fdir_reinit, 0, 1))
   1822  1.34   msaitoh 				return 1;
   1823  1.25   msaitoh                 	/* Disable the interrupt */
   1824  1.25   msaitoh 			IXGBE_WRITE_REG(hw, IXGBE_EIMC, IXGBE_EICR_FLOW_DIR);
   1825   1.1    dyoung 			softint_schedule(adapter->fdir_si);
   1826   1.1    dyoung 		} else
   1827   1.1    dyoung #endif
   1828   1.1    dyoung 		if (reg_eicr & IXGBE_EICR_ECC) {
   1829   1.1    dyoung                 	device_printf(adapter->dev, "\nCRITICAL: ECC ERROR!! "
   1830   1.1    dyoung 			    "Please Reboot!!\n");
   1831   1.1    dyoung 			IXGBE_WRITE_REG(hw, IXGBE_EICR, IXGBE_EICR_ECC);
   1832   1.1    dyoung 		} else
   1833   1.1    dyoung 
   1834   1.1    dyoung 		if (reg_eicr & IXGBE_EICR_GPI_SDP1) {
   1835   1.1    dyoung                 	/* Clear the interrupt */
   1836   1.1    dyoung                 	IXGBE_WRITE_REG(hw, IXGBE_EICR, IXGBE_EICR_GPI_SDP1);
   1837   1.1    dyoung 			softint_schedule(adapter->msf_si);
   1838   1.1    dyoung         	} else if (reg_eicr & IXGBE_EICR_GPI_SDP2) {
   1839   1.1    dyoung                 	/* Clear the interrupt */
   1840   1.1    dyoung                 	IXGBE_WRITE_REG(hw, IXGBE_EICR, IXGBE_EICR_GPI_SDP2);
   1841   1.1    dyoung 			softint_schedule(adapter->mod_si);
   1842   1.1    dyoung 		}
   1843   1.1    dyoung         }
   1844   1.1    dyoung 
   1845   1.1    dyoung 	/* Check for fan failure */
   1846   1.1    dyoung 	if ((hw->device_id == IXGBE_DEV_ID_82598AT) &&
   1847   1.1    dyoung 	    (reg_eicr & IXGBE_EICR_GPI_SDP1)) {
   1848   1.1    dyoung                 device_printf(adapter->dev, "\nCRITICAL: FAN FAILURE!! "
   1849   1.1    dyoung 		    "REPLACE IMMEDIATELY!!\n");
   1850   1.1    dyoung 		IXGBE_WRITE_REG(hw, IXGBE_EICR, IXGBE_EICR_GPI_SDP1);
   1851   1.1    dyoung 	}
   1852   1.1    dyoung 
   1853  1.24   msaitoh 	/* Check for over temp condition */
   1854  1.24   msaitoh 	if ((hw->mac.type == ixgbe_mac_X540) &&
   1855  1.28   msaitoh 	    (reg_eicr & IXGBE_EICR_TS)) {
   1856  1.24   msaitoh 		device_printf(adapter->dev, "\nCRITICAL: OVER TEMP!! "
   1857  1.24   msaitoh 		    "PHY IS SHUT DOWN!!\n");
   1858  1.24   msaitoh 		device_printf(adapter->dev, "System shutdown required\n");
   1859  1.28   msaitoh 		IXGBE_WRITE_REG(hw, IXGBE_EICR, IXGBE_EICR_TS);
   1860  1.24   msaitoh 	}
   1861  1.24   msaitoh 
   1862   1.1    dyoung 	IXGBE_WRITE_REG(&adapter->hw, IXGBE_EIMS, IXGBE_EIMS_OTHER);
   1863  1.34   msaitoh 	return 1;
   1864   1.1    dyoung }
   1865   1.1    dyoung #endif
   1866   1.1    dyoung 
   1867   1.1    dyoung /*********************************************************************
   1868   1.1    dyoung  *
   1869   1.1    dyoung  *  Media Ioctl callback
   1870   1.1    dyoung  *
   1871   1.1    dyoung  *  This routine is called whenever the user queries the status of
   1872   1.1    dyoung  *  the interface using ifconfig.
   1873   1.1    dyoung  *
   1874   1.1    dyoung  **********************************************************************/
   1875   1.1    dyoung static void
   1876   1.1    dyoung ixgbe_media_status(struct ifnet * ifp, struct ifmediareq * ifmr)
   1877   1.1    dyoung {
   1878   1.1    dyoung 	struct adapter *adapter = ifp->if_softc;
   1879  1.33   msaitoh 	struct ixgbe_hw *hw = &adapter->hw;
   1880   1.1    dyoung 
   1881   1.1    dyoung 	INIT_DEBUGOUT("ixgbe_media_status: begin");
   1882   1.1    dyoung 	IXGBE_CORE_LOCK(adapter);
   1883   1.1    dyoung 	ixgbe_update_link_status(adapter);
   1884   1.1    dyoung 
   1885   1.1    dyoung 	ifmr->ifm_status = IFM_AVALID;
   1886   1.1    dyoung 	ifmr->ifm_active = IFM_ETHER;
   1887   1.1    dyoung 
   1888   1.1    dyoung 	if (!adapter->link_active) {
   1889   1.1    dyoung 		IXGBE_CORE_UNLOCK(adapter);
   1890   1.1    dyoung 		return;
   1891   1.1    dyoung 	}
   1892   1.1    dyoung 
   1893   1.1    dyoung 	ifmr->ifm_status |= IFM_ACTIVE;
   1894   1.1    dyoung 
   1895  1.33   msaitoh 	/*
   1896  1.33   msaitoh 	 * Not all NIC are 1000baseSX as an example X540T.
   1897  1.33   msaitoh 	 * We must set properly the media based on NIC model.
   1898  1.33   msaitoh 	 */
   1899  1.33   msaitoh 	switch (hw->device_id) {
   1900  1.33   msaitoh 	case IXGBE_DEV_ID_X540T:
   1901  1.33   msaitoh 		if (adapter->link_speed == IXGBE_LINK_SPEED_100_FULL)
   1902  1.33   msaitoh 			ifmr->ifm_active |= IFM_100_TX | IFM_FDX;
   1903  1.33   msaitoh 		else if (adapter->link_speed == IXGBE_LINK_SPEED_1GB_FULL)
   1904  1.33   msaitoh 			ifmr->ifm_active |= IFM_1000_T | IFM_FDX;
   1905  1.33   msaitoh 		else if (adapter->link_speed == IXGBE_LINK_SPEED_10GB_FULL)
   1906  1.33   msaitoh 			ifmr->ifm_active |= adapter->optics | IFM_FDX;
   1907  1.33   msaitoh 		break;
   1908  1.33   msaitoh 	default:
   1909  1.33   msaitoh 		if (adapter->link_speed == IXGBE_LINK_SPEED_100_FULL)
   1910  1.24   msaitoh 			ifmr->ifm_active |= IFM_100_TX | IFM_FDX;
   1911  1.33   msaitoh 		else if (adapter->link_speed == IXGBE_LINK_SPEED_1GB_FULL)
   1912  1.28   msaitoh 			ifmr->ifm_active |= IFM_1000_SX | IFM_FDX;
   1913  1.33   msaitoh 		else if (adapter->link_speed == IXGBE_LINK_SPEED_10GB_FULL)
   1914   1.1    dyoung 			ifmr->ifm_active |= adapter->optics | IFM_FDX;
   1915  1.33   msaitoh 		break;
   1916   1.1    dyoung 	}
   1917   1.1    dyoung 
   1918   1.1    dyoung 	IXGBE_CORE_UNLOCK(adapter);
   1919   1.1    dyoung 
   1920   1.1    dyoung 	return;
   1921   1.1    dyoung }
   1922   1.1    dyoung 
   1923   1.1    dyoung /*********************************************************************
   1924   1.1    dyoung  *
   1925   1.1    dyoung  *  Media Ioctl callback
   1926   1.1    dyoung  *
   1927   1.1    dyoung  *  This routine is called when the user changes speed/duplex using
   1928   1.1    dyoung  *  media/mediopt option with ifconfig.
   1929   1.1    dyoung  *
   1930   1.1    dyoung  **********************************************************************/
   1931   1.1    dyoung static int
   1932   1.1    dyoung ixgbe_media_change(struct ifnet * ifp)
   1933   1.1    dyoung {
   1934   1.1    dyoung 	struct adapter *adapter = ifp->if_softc;
   1935   1.1    dyoung 	struct ifmedia *ifm = &adapter->media;
   1936   1.1    dyoung 
   1937   1.1    dyoung 	INIT_DEBUGOUT("ixgbe_media_change: begin");
   1938   1.1    dyoung 
   1939   1.1    dyoung 	if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
   1940   1.1    dyoung 		return (EINVAL);
   1941   1.1    dyoung 
   1942   1.1    dyoung         switch (IFM_SUBTYPE(ifm->ifm_media)) {
   1943  1.33   msaitoh 	case IFM_10G_T:
   1944  1.33   msaitoh 	case IFM_AUTO:
   1945  1.33   msaitoh 		adapter->hw.phy.autoneg_advertised =
   1946  1.24   msaitoh 		    IXGBE_LINK_SPEED_100_FULL |
   1947  1.24   msaitoh 		    IXGBE_LINK_SPEED_1GB_FULL |
   1948  1.24   msaitoh 		    IXGBE_LINK_SPEED_10GB_FULL;
   1949   1.1    dyoung                 break;
   1950   1.1    dyoung         default:
   1951   1.1    dyoung                 device_printf(adapter->dev, "Only auto media type\n");
   1952   1.1    dyoung 		return (EINVAL);
   1953   1.1    dyoung         }
   1954   1.1    dyoung 
   1955   1.1    dyoung 	return (0);
   1956   1.1    dyoung }
   1957   1.1    dyoung 
   1958   1.1    dyoung /*********************************************************************
   1959   1.1    dyoung  *
   1960   1.1    dyoung  *  This routine maps the mbufs to tx descriptors, allowing the
   1961   1.1    dyoung  *  TX engine to transmit the packets.
   1962   1.1    dyoung  *  	- return 0 on success, positive on failure
   1963   1.1    dyoung  *
   1964   1.1    dyoung  **********************************************************************/
   1965   1.1    dyoung 
   1966   1.1    dyoung static int
   1967   1.1    dyoung ixgbe_xmit(struct tx_ring *txr, struct mbuf *m_head)
   1968   1.1    dyoung {
   1969   1.1    dyoung 	struct m_tag *mtag;
   1970   1.1    dyoung 	struct adapter  *adapter = txr->adapter;
   1971   1.1    dyoung 	struct ethercom *ec = &adapter->osdep.ec;
   1972   1.1    dyoung 	u32		olinfo_status = 0, cmd_type_len;
   1973   1.1    dyoung 	int             i, j, error;
   1974  1.28   msaitoh 	int		first;
   1975   1.1    dyoung 	bus_dmamap_t	map;
   1976   1.9     skrll 	struct ixgbe_tx_buf *txbuf;
   1977   1.1    dyoung 	union ixgbe_adv_tx_desc *txd = NULL;
   1978   1.1    dyoung 
   1979   1.1    dyoung 	/* Basic descriptor defines */
   1980   1.1    dyoung         cmd_type_len = (IXGBE_ADVTXD_DTYP_DATA |
   1981   1.1    dyoung 	    IXGBE_ADVTXD_DCMD_IFCS | IXGBE_ADVTXD_DCMD_DEXT);
   1982   1.1    dyoung 
   1983   1.1    dyoung 	if ((mtag = VLAN_OUTPUT_TAG(ec, m_head)) != NULL)
   1984   1.1    dyoung         	cmd_type_len |= IXGBE_ADVTXD_DCMD_VLE;
   1985   1.1    dyoung 
   1986   1.1    dyoung         /*
   1987   1.1    dyoung          * Important to capture the first descriptor
   1988   1.1    dyoung          * used because it will contain the index of
   1989   1.1    dyoung          * the one we tell the hardware to report back
   1990   1.1    dyoung          */
   1991   1.1    dyoung         first = txr->next_avail_desc;
   1992   1.1    dyoung 	txbuf = &txr->tx_buffers[first];
   1993   1.1    dyoung 	map = txbuf->map;
   1994   1.1    dyoung 
   1995   1.1    dyoung 	/*
   1996   1.1    dyoung 	 * Map the packet for DMA.
   1997   1.1    dyoung 	 */
   1998   1.1    dyoung 	error = bus_dmamap_load_mbuf(txr->txtag->dt_dmat, map,
   1999   1.1    dyoung 	    m_head, BUS_DMA_NOWAIT);
   2000   1.1    dyoung 
   2001  1.28   msaitoh 	if (__predict_false(error)) {
   2002  1.28   msaitoh 
   2003  1.28   msaitoh 		switch (error) {
   2004  1.28   msaitoh 		case EAGAIN:
   2005  1.28   msaitoh 			adapter->eagain_tx_dma_setup.ev_count++;
   2006  1.28   msaitoh 			return EAGAIN;
   2007  1.28   msaitoh 		case ENOMEM:
   2008  1.28   msaitoh 			adapter->enomem_tx_dma_setup.ev_count++;
   2009  1.28   msaitoh 			return EAGAIN;
   2010  1.28   msaitoh 		case EFBIG:
   2011  1.28   msaitoh 			/*
   2012  1.28   msaitoh 			 * XXX Try it again?
   2013  1.28   msaitoh 			 * do m_defrag() and retry bus_dmamap_load_mbuf().
   2014  1.28   msaitoh 			 */
   2015  1.28   msaitoh 			adapter->efbig_tx_dma_setup.ev_count++;
   2016  1.28   msaitoh 			return error;
   2017  1.28   msaitoh 		case EINVAL:
   2018  1.28   msaitoh 			adapter->einval_tx_dma_setup.ev_count++;
   2019  1.28   msaitoh 			return error;
   2020  1.28   msaitoh 		default:
   2021  1.28   msaitoh 			adapter->other_tx_dma_setup.ev_count++;
   2022  1.28   msaitoh 			return error;
   2023  1.28   msaitoh 		}
   2024   1.1    dyoung 	}
   2025   1.1    dyoung 
   2026   1.1    dyoung 	/* Make certain there are enough descriptors */
   2027   1.1    dyoung 	if (map->dm_nsegs > txr->tx_avail - 2) {
   2028   1.1    dyoung 		txr->no_desc_avail.ev_count++;
   2029   1.1    dyoung 		ixgbe_dmamap_unload(txr->txtag, txbuf->map);
   2030   1.1    dyoung 		return EAGAIN;
   2031   1.1    dyoung 	}
   2032   1.1    dyoung 
   2033   1.1    dyoung 	/*
   2034   1.1    dyoung 	** Set up the appropriate offload context
   2035  1.28   msaitoh 	** this will consume the first descriptor
   2036   1.1    dyoung 	*/
   2037  1.28   msaitoh 	error = ixgbe_tx_ctx_setup(txr, m_head, &cmd_type_len, &olinfo_status);
   2038  1.28   msaitoh 	if (__predict_false(error)) {
   2039  1.28   msaitoh 		return (error);
   2040  1.28   msaitoh 	}
   2041   1.1    dyoung 
   2042   1.1    dyoung #ifdef IXGBE_FDIR
   2043   1.1    dyoung 	/* Do the flow director magic */
   2044   1.1    dyoung 	if ((txr->atr_sample) && (!adapter->fdir_reinit)) {
   2045   1.1    dyoung 		++txr->atr_count;
   2046   1.1    dyoung 		if (txr->atr_count >= atr_sample_rate) {
   2047   1.1    dyoung 			ixgbe_atr(txr, m_head);
   2048   1.1    dyoung 			txr->atr_count = 0;
   2049   1.1    dyoung 		}
   2050   1.1    dyoung 	}
   2051   1.1    dyoung #endif
   2052   1.1    dyoung 
   2053   1.1    dyoung 	i = txr->next_avail_desc;
   2054   1.1    dyoung 	for (j = 0; j < map->dm_nsegs; j++) {
   2055   1.1    dyoung 		bus_size_t seglen;
   2056   1.1    dyoung 		bus_addr_t segaddr;
   2057   1.1    dyoung 
   2058   1.1    dyoung 		txbuf = &txr->tx_buffers[i];
   2059   1.1    dyoung 		txd = &txr->tx_base[i];
   2060   1.1    dyoung 		seglen = map->dm_segs[j].ds_len;
   2061   1.1    dyoung 		segaddr = htole64(map->dm_segs[j].ds_addr);
   2062   1.1    dyoung 
   2063   1.1    dyoung 		txd->read.buffer_addr = segaddr;
   2064   1.1    dyoung 		txd->read.cmd_type_len = htole32(txr->txd_cmd |
   2065   1.1    dyoung 		    cmd_type_len |seglen);
   2066   1.1    dyoung 		txd->read.olinfo_status = htole32(olinfo_status);
   2067   1.1    dyoung 
   2068  1.28   msaitoh 		if (++i == txr->num_desc)
   2069   1.1    dyoung 			i = 0;
   2070   1.1    dyoung 	}
   2071   1.1    dyoung 
   2072   1.1    dyoung 	txd->read.cmd_type_len |=
   2073   1.1    dyoung 	    htole32(IXGBE_TXD_CMD_EOP | IXGBE_TXD_CMD_RS);
   2074   1.1    dyoung 	txr->tx_avail -= map->dm_nsegs;
   2075   1.1    dyoung 	txr->next_avail_desc = i;
   2076   1.1    dyoung 
   2077   1.1    dyoung 	txbuf->m_head = m_head;
   2078  1.28   msaitoh 	/*
   2079  1.28   msaitoh 	** Here we swap the map so the last descriptor,
   2080  1.28   msaitoh 	** which gets the completion interrupt has the
   2081  1.28   msaitoh 	** real map, and the first descriptor gets the
   2082  1.28   msaitoh 	** unused map from this descriptor.
   2083  1.28   msaitoh 	*/
   2084   1.1    dyoung 	txr->tx_buffers[first].map = txbuf->map;
   2085   1.1    dyoung 	txbuf->map = map;
   2086   1.1    dyoung 	bus_dmamap_sync(txr->txtag->dt_dmat, map, 0, m_head->m_pkthdr.len,
   2087   1.1    dyoung 	    BUS_DMASYNC_PREWRITE);
   2088   1.1    dyoung 
   2089  1.28   msaitoh         /* Set the EOP descriptor that will be marked done */
   2090   1.1    dyoung         txbuf = &txr->tx_buffers[first];
   2091  1.28   msaitoh 	txbuf->eop = txd;
   2092   1.1    dyoung 
   2093   1.1    dyoung         ixgbe_dmamap_sync(txr->txdma.dma_tag, txr->txdma.dma_map,
   2094   1.1    dyoung 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
   2095   1.1    dyoung 	/*
   2096   1.1    dyoung 	 * Advance the Transmit Descriptor Tail (Tdt), this tells the
   2097   1.1    dyoung 	 * hardware that this frame is available to transmit.
   2098   1.1    dyoung 	 */
   2099   1.1    dyoung 	++txr->total_packets.ev_count;
   2100   1.1    dyoung 	IXGBE_WRITE_REG(&adapter->hw, IXGBE_TDT(txr->me), i);
   2101   1.1    dyoung 
   2102   1.1    dyoung 	return 0;
   2103   1.1    dyoung }
   2104   1.1    dyoung 
   2105   1.1    dyoung static void
   2106   1.1    dyoung ixgbe_set_promisc(struct adapter *adapter)
   2107   1.1    dyoung {
   2108  1.28   msaitoh 	struct ether_multi *enm;
   2109  1.28   msaitoh 	struct ether_multistep step;
   2110   1.1    dyoung 	u_int32_t       reg_rctl;
   2111  1.28   msaitoh 	struct ethercom *ec = &adapter->osdep.ec;
   2112   1.1    dyoung 	struct ifnet   *ifp = adapter->ifp;
   2113  1.28   msaitoh 	int		mcnt = 0;
   2114   1.1    dyoung 
   2115   1.1    dyoung 	reg_rctl = IXGBE_READ_REG(&adapter->hw, IXGBE_FCTRL);
   2116   1.1    dyoung 	reg_rctl &= (~IXGBE_FCTRL_UPE);
   2117  1.28   msaitoh 	if (ifp->if_flags & IFF_ALLMULTI)
   2118  1.28   msaitoh 		mcnt = MAX_NUM_MULTICAST_ADDRESSES;
   2119  1.28   msaitoh 	else {
   2120  1.28   msaitoh 		ETHER_FIRST_MULTI(step, ec, enm);
   2121  1.28   msaitoh 		while (enm != NULL) {
   2122  1.28   msaitoh 			if (mcnt == MAX_NUM_MULTICAST_ADDRESSES)
   2123  1.28   msaitoh 				break;
   2124  1.28   msaitoh 			mcnt++;
   2125  1.28   msaitoh 			ETHER_NEXT_MULTI(step, enm);
   2126  1.28   msaitoh 		}
   2127  1.28   msaitoh 	}
   2128  1.28   msaitoh 	if (mcnt < MAX_NUM_MULTICAST_ADDRESSES)
   2129  1.28   msaitoh 		reg_rctl &= (~IXGBE_FCTRL_MPE);
   2130   1.1    dyoung 	IXGBE_WRITE_REG(&adapter->hw, IXGBE_FCTRL, reg_rctl);
   2131   1.1    dyoung 
   2132   1.1    dyoung 	if (ifp->if_flags & IFF_PROMISC) {
   2133   1.1    dyoung 		reg_rctl |= (IXGBE_FCTRL_UPE | IXGBE_FCTRL_MPE);
   2134   1.1    dyoung 		IXGBE_WRITE_REG(&adapter->hw, IXGBE_FCTRL, reg_rctl);
   2135   1.1    dyoung 	} else if (ifp->if_flags & IFF_ALLMULTI) {
   2136   1.1    dyoung 		reg_rctl |= IXGBE_FCTRL_MPE;
   2137   1.1    dyoung 		reg_rctl &= ~IXGBE_FCTRL_UPE;
   2138   1.1    dyoung 		IXGBE_WRITE_REG(&adapter->hw, IXGBE_FCTRL, reg_rctl);
   2139   1.1    dyoung 	}
   2140   1.1    dyoung 	return;
   2141   1.1    dyoung }
   2142   1.1    dyoung 
   2143   1.1    dyoung 
   2144   1.1    dyoung /*********************************************************************
   2145   1.1    dyoung  *  Multicast Update
   2146   1.1    dyoung  *
   2147   1.1    dyoung  *  This routine is called whenever multicast address list is updated.
   2148   1.1    dyoung  *
   2149   1.1    dyoung  **********************************************************************/
   2150   1.1    dyoung #define IXGBE_RAR_ENTRIES 16
   2151   1.1    dyoung 
   2152   1.1    dyoung static void
   2153   1.1    dyoung ixgbe_set_multi(struct adapter *adapter)
   2154   1.1    dyoung {
   2155   1.1    dyoung 	struct ether_multi *enm;
   2156   1.1    dyoung 	struct ether_multistep step;
   2157   1.1    dyoung 	u32	fctrl;
   2158   1.1    dyoung 	u8	*mta;
   2159   1.1    dyoung 	u8	*update_ptr;
   2160   1.1    dyoung 	int	mcnt = 0;
   2161   1.1    dyoung 	struct ethercom *ec = &adapter->osdep.ec;
   2162   1.1    dyoung 	struct ifnet   *ifp = adapter->ifp;
   2163   1.1    dyoung 
   2164   1.1    dyoung 	IOCTL_DEBUGOUT("ixgbe_set_multi: begin");
   2165   1.1    dyoung 
   2166   1.1    dyoung 	mta = adapter->mta;
   2167   1.1    dyoung 	bzero(mta, sizeof(u8) * IXGBE_ETH_LENGTH_OF_ADDRESS *
   2168   1.1    dyoung 	    MAX_NUM_MULTICAST_ADDRESSES);
   2169   1.1    dyoung 
   2170  1.28   msaitoh 	ifp->if_flags &= ~IFF_ALLMULTI;
   2171   1.1    dyoung 	ETHER_FIRST_MULTI(step, ec, enm);
   2172   1.1    dyoung 	while (enm != NULL) {
   2173  1.28   msaitoh 		if ((mcnt == MAX_NUM_MULTICAST_ADDRESSES) ||
   2174  1.28   msaitoh 		    (memcmp(enm->enm_addrlo, enm->enm_addrhi,
   2175  1.28   msaitoh 			ETHER_ADDR_LEN) != 0)) {
   2176  1.28   msaitoh 			ifp->if_flags |= IFF_ALLMULTI;
   2177   1.1    dyoung 			break;
   2178   1.1    dyoung 		}
   2179   1.1    dyoung 		bcopy(enm->enm_addrlo,
   2180   1.1    dyoung 		    &mta[mcnt * IXGBE_ETH_LENGTH_OF_ADDRESS],
   2181   1.1    dyoung 		    IXGBE_ETH_LENGTH_OF_ADDRESS);
   2182   1.1    dyoung 		mcnt++;
   2183   1.1    dyoung 		ETHER_NEXT_MULTI(step, enm);
   2184   1.1    dyoung 	}
   2185   1.1    dyoung 
   2186  1.28   msaitoh 	fctrl = IXGBE_READ_REG(&adapter->hw, IXGBE_FCTRL);
   2187  1.28   msaitoh 	fctrl &= ~(IXGBE_FCTRL_UPE | IXGBE_FCTRL_MPE);
   2188  1.28   msaitoh 	if (ifp->if_flags & IFF_PROMISC)
   2189  1.28   msaitoh 		fctrl |= (IXGBE_FCTRL_UPE | IXGBE_FCTRL_MPE);
   2190  1.28   msaitoh 	else if (ifp->if_flags & IFF_ALLMULTI) {
   2191  1.28   msaitoh 		fctrl |= IXGBE_FCTRL_MPE;
   2192  1.28   msaitoh 	}
   2193  1.28   msaitoh 
   2194  1.28   msaitoh 	IXGBE_WRITE_REG(&adapter->hw, IXGBE_FCTRL, fctrl);
   2195  1.28   msaitoh 
   2196  1.28   msaitoh 	if (mcnt < MAX_NUM_MULTICAST_ADDRESSES) {
   2197  1.28   msaitoh 		update_ptr = mta;
   2198  1.28   msaitoh 		ixgbe_update_mc_addr_list(&adapter->hw,
   2199  1.28   msaitoh 		    update_ptr, mcnt, ixgbe_mc_array_itr, TRUE);
   2200  1.28   msaitoh 	}
   2201   1.1    dyoung 
   2202   1.1    dyoung 	return;
   2203   1.1    dyoung }
   2204   1.1    dyoung 
   2205   1.1    dyoung /*
   2206   1.1    dyoung  * This is an iterator function now needed by the multicast
   2207   1.1    dyoung  * shared code. It simply feeds the shared code routine the
   2208   1.1    dyoung  * addresses in the array of ixgbe_set_multi() one by one.
   2209   1.1    dyoung  */
   2210   1.1    dyoung static u8 *
   2211   1.1    dyoung ixgbe_mc_array_itr(struct ixgbe_hw *hw, u8 **update_ptr, u32 *vmdq)
   2212   1.1    dyoung {
   2213   1.1    dyoung 	u8 *addr = *update_ptr;
   2214   1.1    dyoung 	u8 *newptr;
   2215   1.1    dyoung 	*vmdq = 0;
   2216   1.1    dyoung 
   2217   1.1    dyoung 	newptr = addr + IXGBE_ETH_LENGTH_OF_ADDRESS;
   2218   1.1    dyoung 	*update_ptr = newptr;
   2219   1.1    dyoung 	return addr;
   2220   1.1    dyoung }
   2221   1.1    dyoung 
   2222   1.1    dyoung 
   2223   1.1    dyoung /*********************************************************************
   2224   1.1    dyoung  *  Timer routine
   2225   1.1    dyoung  *
   2226   1.1    dyoung  *  This routine checks for link status,updates statistics,
   2227   1.1    dyoung  *  and runs the watchdog check.
   2228   1.1    dyoung  *
   2229   1.1    dyoung  **********************************************************************/
   2230   1.1    dyoung 
   2231   1.1    dyoung static void
   2232   1.1    dyoung ixgbe_local_timer1(void *arg)
   2233   1.1    dyoung {
   2234  1.24   msaitoh 	struct adapter	*adapter = arg;
   2235   1.1    dyoung 	device_t	dev = adapter->dev;
   2236  1.24   msaitoh 	struct ix_queue *que = adapter->queues;
   2237  1.24   msaitoh 	struct tx_ring	*txr = adapter->tx_rings;
   2238  1.26   msaitoh 	int		hung = 0, paused = 0;
   2239   1.1    dyoung 
   2240   1.1    dyoung 	KASSERT(mutex_owned(&adapter->core_mtx));
   2241   1.1    dyoung 
   2242   1.1    dyoung 	/* Check for pluggable optics */
   2243   1.1    dyoung 	if (adapter->sfp_probe)
   2244   1.1    dyoung 		if (!ixgbe_sfp_probe(adapter))
   2245   1.1    dyoung 			goto out; /* Nothing to do */
   2246   1.1    dyoung 
   2247   1.1    dyoung 	ixgbe_update_link_status(adapter);
   2248   1.1    dyoung 	ixgbe_update_stats_counters(adapter);
   2249   1.1    dyoung 
   2250   1.1    dyoung 	/*
   2251   1.1    dyoung 	 * If the interface has been paused
   2252   1.1    dyoung 	 * then don't do the watchdog check
   2253   1.1    dyoung 	 */
   2254   1.1    dyoung 	if (IXGBE_READ_REG(&adapter->hw, IXGBE_TFCS) & IXGBE_TFCS_TXOFF)
   2255  1.24   msaitoh 		paused = 1;
   2256   1.1    dyoung 
   2257   1.1    dyoung 	/*
   2258  1.24   msaitoh 	** Check the TX queues status
   2259  1.24   msaitoh 	**      - watchdog only if all queues show hung
   2260  1.24   msaitoh 	*/
   2261  1.24   msaitoh 	for (int i = 0; i < adapter->num_queues; i++, que++, txr++) {
   2262  1.26   msaitoh 		if ((txr->queue_status == IXGBE_QUEUE_HUNG) &&
   2263  1.24   msaitoh 		    (paused == 0))
   2264  1.24   msaitoh 			++hung;
   2265  1.26   msaitoh 		else if (txr->queue_status == IXGBE_QUEUE_WORKING)
   2266  1.33   msaitoh #ifndef IXGBE_LEGACY_TX
   2267  1.33   msaitoh 			softint_schedule(txr->txq_si);
   2268  1.33   msaitoh #else
   2269  1.24   msaitoh 			softint_schedule(que->que_si);
   2270  1.33   msaitoh #endif
   2271  1.24   msaitoh 	}
   2272  1.24   msaitoh 	/* Only truely watchdog if all queues show hung */
   2273  1.24   msaitoh 	if (hung == adapter->num_queues)
   2274  1.24   msaitoh 		goto watchdog;
   2275   1.1    dyoung 
   2276   1.1    dyoung out:
   2277   1.1    dyoung 	callout_reset(&adapter->timer, hz, ixgbe_local_timer, adapter);
   2278   1.1    dyoung 	return;
   2279   1.1    dyoung 
   2280  1.24   msaitoh watchdog:
   2281   1.1    dyoung 	device_printf(adapter->dev, "Watchdog timeout -- resetting\n");
   2282   1.1    dyoung 	device_printf(dev,"Queue(%d) tdh = %d, hw tdt = %d\n", txr->me,
   2283   1.1    dyoung 	    IXGBE_READ_REG(&adapter->hw, IXGBE_TDH(txr->me)),
   2284   1.1    dyoung 	    IXGBE_READ_REG(&adapter->hw, IXGBE_TDT(txr->me)));
   2285   1.1    dyoung 	device_printf(dev,"TX(%d) desc avail = %d,"
   2286   1.1    dyoung 	    "Next TX to Clean = %d\n",
   2287   1.1    dyoung 	    txr->me, txr->tx_avail, txr->next_to_clean);
   2288   1.1    dyoung 	adapter->ifp->if_flags &= ~IFF_RUNNING;
   2289   1.1    dyoung 	adapter->watchdog_events.ev_count++;
   2290   1.1    dyoung 	ixgbe_init_locked(adapter);
   2291   1.1    dyoung }
   2292   1.1    dyoung 
   2293   1.1    dyoung static void
   2294   1.1    dyoung ixgbe_local_timer(void *arg)
   2295   1.1    dyoung {
   2296   1.1    dyoung 	struct adapter *adapter = arg;
   2297   1.1    dyoung 
   2298   1.1    dyoung 	IXGBE_CORE_LOCK(adapter);
   2299   1.1    dyoung 	ixgbe_local_timer1(adapter);
   2300   1.1    dyoung 	IXGBE_CORE_UNLOCK(adapter);
   2301   1.1    dyoung }
   2302   1.1    dyoung 
   2303   1.1    dyoung /*
   2304   1.1    dyoung ** Note: this routine updates the OS on the link state
   2305   1.1    dyoung **	the real check of the hardware only happens with
   2306   1.1    dyoung **	a link interrupt.
   2307   1.1    dyoung */
   2308   1.1    dyoung static void
   2309   1.1    dyoung ixgbe_update_link_status(struct adapter *adapter)
   2310   1.1    dyoung {
   2311   1.1    dyoung 	struct ifnet	*ifp = adapter->ifp;
   2312   1.1    dyoung 	device_t dev = adapter->dev;
   2313   1.1    dyoung 
   2314   1.1    dyoung 
   2315   1.1    dyoung 	if (adapter->link_up){
   2316   1.1    dyoung 		if (adapter->link_active == FALSE) {
   2317   1.1    dyoung 			if (bootverbose)
   2318   1.1    dyoung 				device_printf(dev,"Link is up %d Gbps %s \n",
   2319   1.1    dyoung 				    ((adapter->link_speed == 128)? 10:1),
   2320   1.1    dyoung 				    "Full Duplex");
   2321   1.1    dyoung 			adapter->link_active = TRUE;
   2322  1.25   msaitoh 			/* Update any Flow Control changes */
   2323  1.25   msaitoh 			ixgbe_fc_enable(&adapter->hw);
   2324   1.1    dyoung 			if_link_state_change(ifp, LINK_STATE_UP);
   2325   1.1    dyoung 		}
   2326   1.1    dyoung 	} else { /* Link down */
   2327   1.1    dyoung 		if (adapter->link_active == TRUE) {
   2328   1.1    dyoung 			if (bootverbose)
   2329   1.1    dyoung 				device_printf(dev,"Link is Down\n");
   2330   1.1    dyoung 			if_link_state_change(ifp, LINK_STATE_DOWN);
   2331   1.1    dyoung 			adapter->link_active = FALSE;
   2332   1.1    dyoung 		}
   2333   1.1    dyoung 	}
   2334   1.1    dyoung 
   2335   1.1    dyoung 	return;
   2336   1.1    dyoung }
   2337   1.1    dyoung 
   2338   1.1    dyoung 
   2339   1.1    dyoung static void
   2340   1.1    dyoung ixgbe_ifstop(struct ifnet *ifp, int disable)
   2341   1.1    dyoung {
   2342   1.1    dyoung 	struct adapter *adapter = ifp->if_softc;
   2343   1.1    dyoung 
   2344   1.1    dyoung 	IXGBE_CORE_LOCK(adapter);
   2345   1.1    dyoung 	ixgbe_stop(adapter);
   2346   1.1    dyoung 	IXGBE_CORE_UNLOCK(adapter);
   2347   1.1    dyoung }
   2348   1.1    dyoung 
   2349   1.1    dyoung /*********************************************************************
   2350   1.1    dyoung  *
   2351   1.1    dyoung  *  This routine disables all traffic on the adapter by issuing a
   2352   1.1    dyoung  *  global reset on the MAC and deallocates TX/RX buffers.
   2353   1.1    dyoung  *
   2354   1.1    dyoung  **********************************************************************/
   2355   1.1    dyoung 
   2356   1.1    dyoung static void
   2357   1.1    dyoung ixgbe_stop(void *arg)
   2358   1.1    dyoung {
   2359   1.1    dyoung 	struct ifnet   *ifp;
   2360   1.1    dyoung 	struct adapter *adapter = arg;
   2361   1.1    dyoung 	struct ixgbe_hw *hw = &adapter->hw;
   2362   1.1    dyoung 	ifp = adapter->ifp;
   2363   1.1    dyoung 
   2364   1.1    dyoung 	KASSERT(mutex_owned(&adapter->core_mtx));
   2365   1.1    dyoung 
   2366   1.1    dyoung 	INIT_DEBUGOUT("ixgbe_stop: begin\n");
   2367   1.1    dyoung 	ixgbe_disable_intr(adapter);
   2368  1.24   msaitoh 	callout_stop(&adapter->timer);
   2369   1.1    dyoung 
   2370  1.24   msaitoh 	/* Let the stack know...*/
   2371  1.24   msaitoh 	ifp->if_flags &= ~IFF_RUNNING;
   2372   1.1    dyoung 
   2373   1.1    dyoung 	ixgbe_reset_hw(hw);
   2374   1.1    dyoung 	hw->adapter_stopped = FALSE;
   2375   1.1    dyoung 	ixgbe_stop_adapter(hw);
   2376  1.33   msaitoh 	if (hw->mac.type == ixgbe_mac_82599EB)
   2377  1.33   msaitoh 		ixgbe_stop_mac_link_on_d3_82599(hw);
   2378  1.33   msaitoh 	/* Turn off the laser - noop with no optics */
   2379  1.33   msaitoh 	ixgbe_disable_tx_laser(hw);
   2380  1.33   msaitoh 
   2381  1.33   msaitoh 	/* Update the stack */
   2382  1.33   msaitoh 	adapter->link_up = FALSE;
   2383  1.33   msaitoh 	ixgbe_update_link_status(adapter);
   2384   1.1    dyoung 
   2385   1.1    dyoung 	/* reprogram the RAR[0] in case user changed it. */
   2386   1.1    dyoung 	ixgbe_set_rar(&adapter->hw, 0, adapter->hw.mac.addr, 0, IXGBE_RAH_AV);
   2387   1.1    dyoung 
   2388   1.1    dyoung 	return;
   2389   1.1    dyoung }
   2390   1.1    dyoung 
   2391   1.1    dyoung 
   2392   1.1    dyoung /*********************************************************************
   2393   1.1    dyoung  *
   2394   1.1    dyoung  *  Determine hardware revision.
   2395   1.1    dyoung  *
   2396   1.1    dyoung  **********************************************************************/
   2397   1.1    dyoung static void
   2398   1.1    dyoung ixgbe_identify_hardware(struct adapter *adapter)
   2399   1.1    dyoung {
   2400   1.1    dyoung 	pcitag_t tag;
   2401   1.1    dyoung 	pci_chipset_tag_t pc;
   2402   1.1    dyoung 	pcireg_t subid, id;
   2403   1.1    dyoung 	struct ixgbe_hw *hw = &adapter->hw;
   2404   1.1    dyoung 
   2405   1.1    dyoung 	pc = adapter->osdep.pc;
   2406   1.1    dyoung 	tag = adapter->osdep.tag;
   2407   1.1    dyoung 
   2408   1.1    dyoung 	id = pci_conf_read(pc, tag, PCI_ID_REG);
   2409   1.1    dyoung 	subid = pci_conf_read(pc, tag, PCI_SUBSYS_ID_REG);
   2410   1.1    dyoung 
   2411   1.1    dyoung 	/* Save off the information about this board */
   2412   1.1    dyoung 	hw->vendor_id = PCI_VENDOR(id);
   2413   1.1    dyoung 	hw->device_id = PCI_PRODUCT(id);
   2414   1.1    dyoung 	hw->revision_id =
   2415   1.1    dyoung 	    PCI_REVISION(pci_conf_read(pc, tag, PCI_CLASS_REG));
   2416   1.1    dyoung 	hw->subsystem_vendor_id = PCI_SUBSYS_VENDOR(subid);
   2417   1.1    dyoung 	hw->subsystem_device_id = PCI_SUBSYS_ID(subid);
   2418   1.1    dyoung 
   2419   1.1    dyoung 	/* We need this here to set the num_segs below */
   2420   1.1    dyoung 	ixgbe_set_mac_type(hw);
   2421   1.1    dyoung 
   2422   1.1    dyoung 	/* Pick up the 82599 and VF settings */
   2423   1.1    dyoung 	if (hw->mac.type != ixgbe_mac_82598EB) {
   2424   1.1    dyoung 		hw->phy.smart_speed = ixgbe_smart_speed;
   2425   1.1    dyoung 		adapter->num_segs = IXGBE_82599_SCATTER;
   2426   1.1    dyoung 	} else
   2427   1.1    dyoung 		adapter->num_segs = IXGBE_82598_SCATTER;
   2428   1.1    dyoung 
   2429   1.1    dyoung 	return;
   2430   1.1    dyoung }
   2431   1.1    dyoung 
   2432   1.1    dyoung /*********************************************************************
   2433   1.1    dyoung  *
   2434   1.1    dyoung  *  Determine optic type
   2435   1.1    dyoung  *
   2436   1.1    dyoung  **********************************************************************/
   2437   1.1    dyoung static void
   2438   1.1    dyoung ixgbe_setup_optics(struct adapter *adapter)
   2439   1.1    dyoung {
   2440   1.1    dyoung 	struct ixgbe_hw *hw = &adapter->hw;
   2441   1.1    dyoung 	int		layer;
   2442  1.28   msaitoh 
   2443   1.1    dyoung 	layer = ixgbe_get_supported_physical_layer(hw);
   2444  1.26   msaitoh 
   2445  1.24   msaitoh 	if (layer & IXGBE_PHYSICAL_LAYER_10GBASE_T) {
   2446  1.24   msaitoh 		adapter->optics = IFM_10G_T;
   2447  1.24   msaitoh 		return;
   2448  1.26   msaitoh 	}
   2449  1.24   msaitoh 
   2450  1.24   msaitoh 	if (layer & IXGBE_PHYSICAL_LAYER_1000BASE_T) {
   2451  1.24   msaitoh 		adapter->optics = IFM_1000_T;
   2452  1.24   msaitoh 		return;
   2453  1.24   msaitoh 	}
   2454  1.24   msaitoh 
   2455  1.26   msaitoh 	if (layer & IXGBE_PHYSICAL_LAYER_1000BASE_SX) {
   2456  1.26   msaitoh 		adapter->optics = IFM_1000_SX;
   2457  1.26   msaitoh 		return;
   2458  1.26   msaitoh 	}
   2459  1.26   msaitoh 
   2460  1.24   msaitoh 	if (layer & (IXGBE_PHYSICAL_LAYER_10GBASE_LR |
   2461  1.24   msaitoh 	    IXGBE_PHYSICAL_LAYER_10GBASE_LRM)) {
   2462  1.24   msaitoh 		adapter->optics = IFM_10G_LR;
   2463  1.24   msaitoh 		return;
   2464  1.24   msaitoh 	}
   2465  1.24   msaitoh 
   2466  1.24   msaitoh 	if (layer & IXGBE_PHYSICAL_LAYER_10GBASE_SR) {
   2467  1.24   msaitoh 		adapter->optics = IFM_10G_SR;
   2468  1.24   msaitoh 		return;
   2469  1.24   msaitoh 	}
   2470  1.24   msaitoh 
   2471  1.24   msaitoh 	if (layer & IXGBE_PHYSICAL_LAYER_SFP_PLUS_CU) {
   2472  1.24   msaitoh 		adapter->optics = IFM_10G_TWINAX;
   2473  1.24   msaitoh 		return;
   2474  1.24   msaitoh 	}
   2475  1.24   msaitoh 
   2476  1.24   msaitoh 	if (layer & (IXGBE_PHYSICAL_LAYER_10GBASE_KX4 |
   2477  1.24   msaitoh 	    IXGBE_PHYSICAL_LAYER_10GBASE_CX4)) {
   2478  1.24   msaitoh 		adapter->optics = IFM_10G_CX4;
   2479  1.24   msaitoh 		return;
   2480   1.1    dyoung 	}
   2481  1.24   msaitoh 
   2482  1.24   msaitoh 	/* If we get here just set the default */
   2483  1.24   msaitoh 	adapter->optics = IFM_ETHER | IFM_AUTO;
   2484   1.1    dyoung 	return;
   2485   1.1    dyoung }
   2486   1.1    dyoung 
   2487   1.1    dyoung /*********************************************************************
   2488   1.1    dyoung  *
   2489   1.1    dyoung  *  Setup the Legacy or MSI Interrupt handler
   2490   1.1    dyoung  *
   2491   1.1    dyoung  **********************************************************************/
   2492   1.1    dyoung static int
   2493  1.34   msaitoh ixgbe_allocate_legacy(struct adapter *adapter,
   2494  1.34   msaitoh     const struct pci_attach_args *pa)
   2495   1.1    dyoung {
   2496  1.28   msaitoh 	device_t	dev = adapter->dev;
   2497   1.1    dyoung 	struct		ix_queue *que = adapter->queues;
   2498  1.28   msaitoh #ifndef IXGBE_LEGACY_TX
   2499  1.26   msaitoh 	struct tx_ring		*txr = adapter->tx_rings;
   2500  1.26   msaitoh #endif
   2501  1.34   msaitoh #ifndef NETBSD_MSI_OR_MSIX
   2502  1.34   msaitoh 	pci_intr_handle_t	ih;
   2503  1.34   msaitoh #else
   2504  1.34   msaitoh 	int		counts[PCI_INTR_TYPE_SIZE];
   2505  1.34   msaitoh 	pci_intr_type_t intr_type, max_type;
   2506  1.34   msaitoh #endif
   2507   1.9     skrll 	char intrbuf[PCI_INTRSTR_LEN];
   2508  1.34   msaitoh 	const char	*intrstr = NULL;
   2509   1.1    dyoung 
   2510  1.34   msaitoh #ifndef NETBSD_MSI_OR_MSIX
   2511   1.1    dyoung 	/* We allocate a single interrupt resource */
   2512  1.34   msaitoh  	if (pci_intr_map(pa, &ih) != 0) {
   2513   1.1    dyoung 		aprint_error_dev(dev, "unable to map interrupt\n");
   2514   1.1    dyoung 		return ENXIO;
   2515   1.1    dyoung 	} else {
   2516  1.34   msaitoh 		intrstr = pci_intr_string(adapter->osdep.pc, ih, intrbuf,
   2517  1.34   msaitoh 		    sizeof(intrbuf));
   2518  1.34   msaitoh 	}
   2519  1.34   msaitoh 	adapter->osdep.ihs[0] = pci_intr_establish(adapter->osdep.pc, ih,
   2520  1.34   msaitoh 	    IPL_NET, ixgbe_legacy_irq, que);
   2521  1.34   msaitoh #else
   2522  1.34   msaitoh 	/* Allocation settings */
   2523  1.34   msaitoh 	max_type = PCI_INTR_TYPE_MSI;
   2524  1.34   msaitoh 	counts[PCI_INTR_TYPE_MSIX] = 0;
   2525  1.34   msaitoh 	counts[PCI_INTR_TYPE_MSI] = 1;
   2526  1.34   msaitoh 	counts[PCI_INTR_TYPE_INTX] = 1;
   2527  1.34   msaitoh 
   2528  1.34   msaitoh alloc_retry:
   2529  1.34   msaitoh 	if (pci_intr_alloc(pa, &adapter->osdep.intrs, counts, max_type) != 0) {
   2530  1.34   msaitoh 		aprint_error_dev(dev, "couldn't alloc interrupt\n");
   2531  1.34   msaitoh 		return ENXIO;
   2532  1.34   msaitoh 	}
   2533  1.34   msaitoh 	adapter->osdep.nintrs = 1;
   2534  1.34   msaitoh 	intrstr = pci_intr_string(adapter->osdep.pc, adapter->osdep.intrs[0],
   2535  1.34   msaitoh 	    intrbuf, sizeof(intrbuf));
   2536  1.34   msaitoh 	adapter->osdep.ihs[0] = pci_intr_establish(adapter->osdep.pc,
   2537  1.34   msaitoh 	    adapter->osdep.intrs[0], IPL_NET, ixgbe_legacy_irq, que);
   2538  1.34   msaitoh 	if (adapter->osdep.ihs[0] == NULL) {
   2539  1.34   msaitoh 		intr_type = pci_intr_type(adapter->osdep.intrs[0]);
   2540  1.34   msaitoh 		aprint_error_dev(dev,"unable to establish %s\n",
   2541  1.34   msaitoh 		    (intr_type == PCI_INTR_TYPE_MSI) ? "MSI" : "INTx");
   2542  1.34   msaitoh 		pci_intr_release(adapter->osdep.pc, adapter->osdep.intrs, 1);
   2543  1.34   msaitoh 		switch (intr_type) {
   2544  1.34   msaitoh 		case PCI_INTR_TYPE_MSI:
   2545  1.34   msaitoh 			/* The next try is for INTx: Disable MSI */
   2546  1.34   msaitoh 			max_type = PCI_INTR_TYPE_INTX;
   2547  1.34   msaitoh 			counts[PCI_INTR_TYPE_INTX] = 1;
   2548  1.34   msaitoh 			goto alloc_retry;
   2549  1.34   msaitoh 		case PCI_INTR_TYPE_INTX:
   2550  1.34   msaitoh 		default:
   2551  1.34   msaitoh 			/* See below */
   2552  1.34   msaitoh 			break;
   2553  1.34   msaitoh 		}
   2554   1.1    dyoung 	}
   2555  1.34   msaitoh #endif
   2556  1.34   msaitoh 	if (adapter->osdep.ihs[0] == NULL) {
   2557  1.34   msaitoh 		aprint_error_dev(dev,
   2558  1.34   msaitoh 		    "couldn't establish interrupt%s%s\n",
   2559  1.34   msaitoh 		    intrstr ? " at " : "", intrstr ? intrstr : "");
   2560  1.34   msaitoh #ifdef NETBSD_MSI_OR_MSIX
   2561  1.34   msaitoh 		pci_intr_release(adapter->osdep.pc, adapter->osdep.intrs, 1);
   2562  1.34   msaitoh #endif
   2563  1.34   msaitoh 		return ENXIO;
   2564  1.34   msaitoh 	}
   2565  1.34   msaitoh 	aprint_normal_dev(dev, "interrupting at %s\n", intrstr);
   2566   1.1    dyoung 	/*
   2567   1.1    dyoung 	 * Try allocating a fast interrupt and the associated deferred
   2568   1.1    dyoung 	 * processing contexts.
   2569   1.1    dyoung 	 */
   2570  1.28   msaitoh #ifndef IXGBE_LEGACY_TX
   2571  1.26   msaitoh 	txr->txq_si = softint_establish(SOFTINT_NET, ixgbe_deferred_mq_start,
   2572  1.26   msaitoh 	    txr);
   2573  1.26   msaitoh #endif
   2574   1.1    dyoung 	que->que_si = softint_establish(SOFTINT_NET, ixgbe_handle_que, que);
   2575   1.1    dyoung 
   2576   1.1    dyoung 	/* Tasklets for Link, SFP and Multispeed Fiber */
   2577   1.1    dyoung 	adapter->link_si =
   2578   1.1    dyoung 	    softint_establish(SOFTINT_NET, ixgbe_handle_link, adapter);
   2579   1.1    dyoung 	adapter->mod_si =
   2580   1.1    dyoung 	    softint_establish(SOFTINT_NET, ixgbe_handle_mod, adapter);
   2581   1.1    dyoung 	adapter->msf_si =
   2582   1.1    dyoung 	    softint_establish(SOFTINT_NET, ixgbe_handle_msf, adapter);
   2583   1.1    dyoung 
   2584   1.1    dyoung #ifdef IXGBE_FDIR
   2585   1.1    dyoung 	adapter->fdir_si =
   2586   1.1    dyoung 	    softint_establish(SOFTINT_NET, ixgbe_reinit_fdir, adapter);
   2587   1.1    dyoung #endif
   2588   1.1    dyoung 	if (que->que_si == NULL ||
   2589   1.1    dyoung 	    adapter->link_si == NULL ||
   2590   1.1    dyoung 	    adapter->mod_si == NULL ||
   2591   1.1    dyoung #ifdef IXGBE_FDIR
   2592   1.1    dyoung 	    adapter->fdir_si == NULL ||
   2593   1.1    dyoung #endif
   2594   1.1    dyoung 	    adapter->msf_si == NULL) {
   2595   1.1    dyoung 		aprint_error_dev(dev,
   2596   1.1    dyoung 		    "could not establish software interrupts\n");
   2597   1.1    dyoung 		return ENXIO;
   2598   1.1    dyoung 	}
   2599   1.1    dyoung 
   2600   1.1    dyoung 	/* For simplicity in the handlers */
   2601   1.1    dyoung 	adapter->que_mask = IXGBE_EIMS_ENABLE_MASK;
   2602   1.1    dyoung 
   2603   1.1    dyoung 	return (0);
   2604   1.1    dyoung }
   2605   1.1    dyoung 
   2606   1.1    dyoung 
   2607   1.1    dyoung /*********************************************************************
   2608   1.1    dyoung  *
   2609   1.1    dyoung  *  Setup MSIX Interrupt resources and handlers
   2610   1.1    dyoung  *
   2611   1.1    dyoung  **********************************************************************/
   2612   1.1    dyoung static int
   2613   1.1    dyoung ixgbe_allocate_msix(struct adapter *adapter, const struct pci_attach_args *pa)
   2614   1.1    dyoung {
   2615   1.1    dyoung #if !defined(NETBSD_MSI_OR_MSIX)
   2616   1.1    dyoung 	return 0;
   2617   1.1    dyoung #else
   2618   1.1    dyoung 	device_t        dev = adapter->dev;
   2619   1.1    dyoung 	struct 		ix_queue *que = adapter->queues;
   2620  1.26   msaitoh 	struct  	tx_ring *txr = adapter->tx_rings;
   2621  1.34   msaitoh 	pci_chipset_tag_t pc;
   2622  1.34   msaitoh 	char		intrbuf[PCI_INTRSTR_LEN];
   2623  1.34   msaitoh 	const char	*intrstr = NULL;
   2624  1.34   msaitoh 	int 		error, vector = 0;
   2625  1.33   msaitoh 	int		cpu_id = 0;
   2626  1.34   msaitoh 	kcpuset_t	*affinity;
   2627  1.34   msaitoh 
   2628  1.34   msaitoh 	pc = adapter->osdep.pc;
   2629  1.33   msaitoh #ifdef	RSS
   2630  1.33   msaitoh 	cpuset_t cpu_mask;
   2631  1.33   msaitoh 	/*
   2632  1.33   msaitoh 	 * If we're doing RSS, the number of queues needs to
   2633  1.33   msaitoh 	 * match the number of RSS buckets that are configured.
   2634  1.33   msaitoh 	 *
   2635  1.33   msaitoh 	 * + If there's more queues than RSS buckets, we'll end
   2636  1.33   msaitoh 	 *   up with queues that get no traffic.
   2637  1.33   msaitoh 	 *
   2638  1.33   msaitoh 	 * + If there's more RSS buckets than queues, we'll end
   2639  1.33   msaitoh 	 *   up having multiple RSS buckets map to the same queue,
   2640  1.33   msaitoh 	 *   so there'll be some contention.
   2641  1.33   msaitoh 	 */
   2642  1.33   msaitoh 	if (adapter->num_queues != rss_getnumbuckets()) {
   2643  1.33   msaitoh 		device_printf(dev,
   2644  1.33   msaitoh 		    "%s: number of queues (%d) != number of RSS buckets (%d)"
   2645  1.33   msaitoh 		    "; performance will be impacted.\n",
   2646  1.33   msaitoh 		    __func__,
   2647  1.33   msaitoh 		    adapter->num_queues,
   2648  1.33   msaitoh 		    rss_getnumbuckets());
   2649  1.33   msaitoh 	}
   2650  1.33   msaitoh #endif
   2651   1.1    dyoung 
   2652  1.34   msaitoh 	adapter->osdep.nintrs = adapter->num_queues + 1;
   2653  1.34   msaitoh 	if (pci_msix_alloc_exact(pa, &adapter->osdep.intrs,
   2654  1.34   msaitoh 	    adapter->osdep.nintrs) != 0) {
   2655  1.34   msaitoh 		aprint_error_dev(dev,
   2656  1.34   msaitoh 		    "failed to allocate MSI-X interrupt\n");
   2657  1.34   msaitoh 		return (ENXIO);
   2658  1.34   msaitoh 	}
   2659  1.34   msaitoh 
   2660  1.34   msaitoh 	kcpuset_create(&affinity, false);
   2661  1.26   msaitoh 	for (int i = 0; i < adapter->num_queues; i++, vector++, que++, txr++) {
   2662  1.34   msaitoh 		intrstr = pci_intr_string(pc, adapter->osdep.intrs[i], intrbuf,
   2663  1.34   msaitoh 		    sizeof(intrbuf));
   2664  1.34   msaitoh #ifdef IXG_MPSAFE
   2665  1.34   msaitoh 		pci_intr_setattr(pc, adapter->osdep.intrs[i], PCI_INTR_MPSAFE,
   2666  1.34   msaitoh 		    true);
   2667  1.34   msaitoh #endif
   2668  1.34   msaitoh 		/* Set the handler function */
   2669  1.34   msaitoh 		que->res = adapter->osdep.ihs[i] = pci_intr_establish(pc,
   2670  1.34   msaitoh 		    adapter->osdep.intrs[i], IPL_NET, ixgbe_msix_que, que);
   2671   1.1    dyoung 		if (que->res == NULL) {
   2672  1.34   msaitoh 			pci_intr_release(pc, adapter->osdep.intrs,
   2673  1.34   msaitoh 			    adapter->osdep.nintrs);
   2674   1.1    dyoung 			aprint_error_dev(dev,
   2675   1.1    dyoung 			    "Failed to register QUE handler\n");
   2676  1.34   msaitoh 			kcpuset_destroy(affinity);
   2677  1.34   msaitoh 			return ENXIO;
   2678   1.1    dyoung 		}
   2679   1.1    dyoung 		que->msix = vector;
   2680   1.1    dyoung         	adapter->que_mask |= (u64)(1 << que->msix);
   2681  1.33   msaitoh #ifdef	RSS
   2682  1.35   msaitoh 		/*
   2683  1.33   msaitoh 		 * The queue ID is used as the RSS layer bucket ID.
   2684  1.33   msaitoh 		 * We look up the queue ID -> RSS CPU ID and select
   2685  1.33   msaitoh 		 * that.
   2686  1.33   msaitoh 		 */
   2687  1.33   msaitoh 		cpu_id = rss_getcpu(i % rss_getnumbuckets());
   2688  1.33   msaitoh #else
   2689   1.1    dyoung 		/*
   2690  1.33   msaitoh 		 * Bind the msix vector, and thus the
   2691  1.35   msaitoh 		 * rings to the corresponding cpu.
   2692  1.33   msaitoh 		 *
   2693  1.33   msaitoh 		 * This just happens to match the default RSS round-robin
   2694  1.33   msaitoh 		 * bucket -> queue -> CPU allocation.
   2695  1.33   msaitoh 		 */
   2696  1.33   msaitoh 		if (adapter->num_queues > 1)
   2697  1.33   msaitoh 			cpu_id = i;
   2698  1.33   msaitoh #endif
   2699  1.34   msaitoh 		/* Round-robin affinity */
   2700  1.34   msaitoh 		kcpuset_zero(affinity);
   2701  1.34   msaitoh 		kcpuset_set(affinity, cpu_id % ncpu);
   2702  1.36  knakahar 		error = interrupt_distribute(adapter->osdep.ihs[i], affinity,
   2703  1.34   msaitoh 		    NULL);
   2704  1.34   msaitoh 		aprint_normal_dev(dev, "for TX/RX, interrupting at %s",
   2705  1.34   msaitoh 		    intrstr);
   2706  1.34   msaitoh 		if (error == 0) {
   2707  1.33   msaitoh #ifdef	RSS
   2708  1.34   msaitoh 			aprintf_normal(", bound RSS bucket %d to CPU %d\n",
   2709  1.34   msaitoh 			    i, cpu_id);
   2710  1.33   msaitoh #else
   2711  1.34   msaitoh 			aprint_normal(", bound queue %d to cpu %d\n",
   2712  1.34   msaitoh 			    i, cpu_id);
   2713  1.33   msaitoh #endif
   2714  1.34   msaitoh 		} else
   2715  1.34   msaitoh 			aprint_normal("\n");
   2716   1.1    dyoung 
   2717  1.28   msaitoh #ifndef IXGBE_LEGACY_TX
   2718  1.26   msaitoh 		txr->txq_si = softint_establish(SOFTINT_NET,
   2719  1.26   msaitoh 		    ixgbe_deferred_mq_start, txr);
   2720  1.26   msaitoh #endif
   2721  1.26   msaitoh 		que->que_si = softint_establish(SOFTINT_NET, ixgbe_handle_que,
   2722  1.26   msaitoh 		    que);
   2723   1.1    dyoung 		if (que->que_si == NULL) {
   2724   1.1    dyoung 			aprint_error_dev(dev,
   2725   1.1    dyoung 			    "could not establish software interrupt\n");
   2726   1.1    dyoung 		}
   2727   1.1    dyoung 	}
   2728   1.1    dyoung 
   2729   1.1    dyoung 	/* and Link */
   2730  1.34   msaitoh 	cpu_id++;
   2731  1.34   msaitoh 	intrstr = pci_intr_string(pc, adapter->osdep.intrs[vector], intrbuf,
   2732  1.34   msaitoh 	    sizeof(intrbuf));
   2733  1.34   msaitoh #ifdef IXG_MPSAFE
   2734  1.34   msaitoh 	pci_intr_setattr(pc, &adapter->osdep.intrs[vector], PCI_INTR_MPSAFE,
   2735  1.34   msaitoh 	    true);
   2736  1.34   msaitoh #endif
   2737   1.1    dyoung 	/* Set the link handler function */
   2738  1.34   msaitoh 	adapter->osdep.ihs[vector] = pci_intr_establish(pc,
   2739  1.34   msaitoh 	    adapter->osdep.intrs[vector], IPL_NET, ixgbe_msix_link, adapter);
   2740  1.34   msaitoh 	if (adapter->osdep.ihs[vector] == NULL) {
   2741   1.1    dyoung 		adapter->res = NULL;
   2742   1.1    dyoung 		aprint_error_dev(dev, "Failed to register LINK handler\n");
   2743  1.34   msaitoh 		kcpuset_destroy(affinity);
   2744  1.34   msaitoh 		return (ENXIO);
   2745   1.1    dyoung 	}
   2746  1.34   msaitoh 	/* Round-robin affinity */
   2747  1.34   msaitoh 	kcpuset_zero(affinity);
   2748  1.34   msaitoh 	kcpuset_set(affinity, cpu_id % ncpu);
   2749  1.36  knakahar 	error = interrupt_distribute(adapter->osdep.ihs[vector], affinity,NULL);
   2750  1.34   msaitoh 
   2751  1.34   msaitoh 	aprint_normal_dev(dev,
   2752  1.34   msaitoh 	    "for link, interrupting at %s", intrstr);
   2753  1.34   msaitoh 	if (error == 0)
   2754  1.34   msaitoh 		aprint_normal(", affinity to cpu %d\n", cpu_id);
   2755  1.34   msaitoh 	else
   2756  1.34   msaitoh 		aprint_normal("\n");
   2757  1.34   msaitoh 
   2758   1.1    dyoung 	adapter->linkvec = vector;
   2759   1.1    dyoung 	/* Tasklets for Link, SFP and Multispeed Fiber */
   2760   1.1    dyoung 	adapter->link_si =
   2761   1.1    dyoung 	    softint_establish(SOFTINT_NET, ixgbe_handle_link, adapter);
   2762   1.1    dyoung 	adapter->mod_si =
   2763   1.1    dyoung 	    softint_establish(SOFTINT_NET, ixgbe_handle_mod, adapter);
   2764   1.1    dyoung 	adapter->msf_si =
   2765   1.1    dyoung 	    softint_establish(SOFTINT_NET, ixgbe_handle_msf, adapter);
   2766   1.1    dyoung #ifdef IXGBE_FDIR
   2767   1.1    dyoung 	adapter->fdir_si =
   2768   1.1    dyoung 	    softint_establish(SOFTINT_NET, ixgbe_reinit_fdir, adapter);
   2769   1.1    dyoung #endif
   2770   1.1    dyoung 
   2771  1.34   msaitoh 	kcpuset_destroy(affinity);
   2772   1.1    dyoung 	return (0);
   2773   1.1    dyoung #endif
   2774   1.1    dyoung }
   2775   1.1    dyoung 
   2776   1.1    dyoung /*
   2777   1.1    dyoung  * Setup Either MSI/X or MSI
   2778   1.1    dyoung  */
   2779   1.1    dyoung static int
   2780   1.1    dyoung ixgbe_setup_msix(struct adapter *adapter)
   2781   1.1    dyoung {
   2782   1.1    dyoung #if !defined(NETBSD_MSI_OR_MSIX)
   2783   1.1    dyoung 	return 0;
   2784   1.1    dyoung #else
   2785   1.1    dyoung 	device_t dev = adapter->dev;
   2786  1.34   msaitoh 	int want, queues, msgs;
   2787   1.1    dyoung 
   2788   1.1    dyoung 	/* Override by tuneable */
   2789   1.1    dyoung 	if (ixgbe_enable_msix == 0)
   2790   1.1    dyoung 		goto msi;
   2791   1.1    dyoung 
   2792   1.1    dyoung 	/* First try MSI/X */
   2793  1.34   msaitoh 	msgs = pci_msix_count(adapter->osdep.pc, adapter->osdep.tag);
   2794  1.34   msaitoh 	if (msgs < IXG_MSIX_NINTR)
   2795  1.33   msaitoh 		goto msi;
   2796  1.34   msaitoh 
   2797  1.34   msaitoh 	adapter->msix_mem = (void *)1; /* XXX */
   2798   1.1    dyoung 
   2799   1.1    dyoung 	/* Figure out a reasonable auto config value */
   2800  1.34   msaitoh 	queues = (ncpu > (msgs-1)) ? (msgs-1) : ncpu;
   2801   1.1    dyoung 
   2802  1.33   msaitoh 	/* Override based on tuneable */
   2803   1.1    dyoung 	if (ixgbe_num_queues != 0)
   2804   1.1    dyoung 		queues = ixgbe_num_queues;
   2805  1.33   msaitoh 
   2806  1.33   msaitoh #ifdef	RSS
   2807  1.33   msaitoh 	/* If we're doing RSS, clamp at the number of RSS buckets */
   2808  1.33   msaitoh 	if (queues > rss_getnumbuckets())
   2809  1.33   msaitoh 		queues = rss_getnumbuckets();
   2810  1.33   msaitoh #endif
   2811  1.33   msaitoh 
   2812  1.33   msaitoh 	/* reflect correct sysctl value */
   2813  1.33   msaitoh 	ixgbe_num_queues = queues;
   2814   1.1    dyoung 
   2815   1.1    dyoung 	/*
   2816   1.1    dyoung 	** Want one vector (RX/TX pair) per queue
   2817   1.1    dyoung 	** plus an additional for Link.
   2818   1.1    dyoung 	*/
   2819   1.1    dyoung 	want = queues + 1;
   2820   1.1    dyoung 	if (msgs >= want)
   2821   1.1    dyoung 		msgs = want;
   2822   1.1    dyoung 	else {
   2823  1.34   msaitoh                	aprint_error_dev(dev,
   2824   1.1    dyoung 		    "MSIX Configuration Problem, "
   2825   1.1    dyoung 		    "%d vectors but %d queues wanted!\n",
   2826   1.1    dyoung 		    msgs, want);
   2827  1.33   msaitoh 		goto msi;
   2828   1.1    dyoung 	}
   2829  1.34   msaitoh 	device_printf(dev,
   2830  1.34   msaitoh 	    "Using MSIX interrupts with %d vectors\n", msgs);
   2831  1.34   msaitoh 	adapter->num_queues = queues;
   2832  1.34   msaitoh 	return (msgs);
   2833  1.34   msaitoh 
   2834  1.33   msaitoh 	/*
   2835  1.33   msaitoh 	** If MSIX alloc failed or provided us with
   2836  1.33   msaitoh 	** less than needed, free and fall through to MSI
   2837  1.33   msaitoh 	*/
   2838   1.1    dyoung msi:
   2839  1.34   msaitoh        	msgs = pci_msi_count(adapter->osdep.pc, adapter->osdep.tag);
   2840  1.34   msaitoh 	adapter->msix_mem = NULL; /* XXX */
   2841  1.33   msaitoh        	msgs = 1;
   2842  1.34   msaitoh 	aprint_normal_dev(dev,"Using an MSI interrupt\n");
   2843  1.34   msaitoh 	return (msgs);
   2844   1.1    dyoung #endif
   2845   1.1    dyoung }
   2846   1.1    dyoung 
   2847   1.1    dyoung 
   2848   1.1    dyoung static int
   2849  1.34   msaitoh ixgbe_allocate_pci_resources(struct adapter *adapter,
   2850  1.34   msaitoh     const struct pci_attach_args *pa)
   2851   1.1    dyoung {
   2852   1.1    dyoung 	pcireg_t	memtype;
   2853   1.1    dyoung 	device_t        dev = adapter->dev;
   2854   1.1    dyoung 	bus_addr_t addr;
   2855   1.1    dyoung 	int flags;
   2856   1.1    dyoung 
   2857   1.1    dyoung 	memtype = pci_mapreg_type(pa->pa_pc, pa->pa_tag, PCI_BAR(0));
   2858   1.1    dyoung 	switch (memtype) {
   2859   1.1    dyoung 	case PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT:
   2860   1.1    dyoung 	case PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_64BIT:
   2861   1.1    dyoung 		adapter->osdep.mem_bus_space_tag = pa->pa_memt;
   2862   1.1    dyoung 		if (pci_mapreg_info(pa->pa_pc, pa->pa_tag, PCI_BAR(0),
   2863   1.1    dyoung 	              memtype, &addr, &adapter->osdep.mem_size, &flags) != 0)
   2864   1.1    dyoung 			goto map_err;
   2865   1.1    dyoung 		if ((flags & BUS_SPACE_MAP_PREFETCHABLE) != 0) {
   2866   1.1    dyoung 			aprint_normal_dev(dev, "clearing prefetchable bit\n");
   2867   1.1    dyoung 			flags &= ~BUS_SPACE_MAP_PREFETCHABLE;
   2868   1.1    dyoung 		}
   2869   1.1    dyoung 		if (bus_space_map(adapter->osdep.mem_bus_space_tag, addr,
   2870   1.1    dyoung 		     adapter->osdep.mem_size, flags,
   2871   1.1    dyoung 		     &adapter->osdep.mem_bus_space_handle) != 0) {
   2872   1.1    dyoung map_err:
   2873   1.1    dyoung 			adapter->osdep.mem_size = 0;
   2874   1.1    dyoung 			aprint_error_dev(dev, "unable to map BAR0\n");
   2875   1.1    dyoung 			return ENXIO;
   2876   1.1    dyoung 		}
   2877   1.1    dyoung 		break;
   2878   1.1    dyoung 	default:
   2879   1.1    dyoung 		aprint_error_dev(dev, "unexpected type on BAR0\n");
   2880   1.1    dyoung 		return ENXIO;
   2881   1.1    dyoung 	}
   2882   1.1    dyoung 
   2883   1.1    dyoung 	/* Legacy defaults */
   2884   1.1    dyoung 	adapter->num_queues = 1;
   2885   1.1    dyoung 	adapter->hw.back = &adapter->osdep;
   2886   1.1    dyoung 
   2887   1.1    dyoung 	/*
   2888   1.1    dyoung 	** Now setup MSI or MSI/X, should
   2889   1.1    dyoung 	** return us the number of supported
   2890   1.1    dyoung 	** vectors. (Will be 1 for MSI)
   2891   1.1    dyoung 	*/
   2892   1.1    dyoung 	adapter->msix = ixgbe_setup_msix(adapter);
   2893   1.1    dyoung 	return (0);
   2894   1.1    dyoung }
   2895   1.1    dyoung 
   2896   1.1    dyoung static void
   2897   1.1    dyoung ixgbe_free_pci_resources(struct adapter * adapter)
   2898   1.1    dyoung {
   2899   1.1    dyoung #if defined(NETBSD_MSI_OR_MSIX)
   2900   1.1    dyoung 	struct 		ix_queue *que = adapter->queues;
   2901   1.1    dyoung #endif
   2902   1.9     skrll 	int		rid;
   2903   1.1    dyoung 
   2904   1.9     skrll #if defined(NETBSD_MSI_OR_MSIX)
   2905   1.1    dyoung 	/*
   2906   1.1    dyoung 	**  Release all msix queue resources:
   2907   1.1    dyoung 	*/
   2908   1.1    dyoung 	for (int i = 0; i < adapter->num_queues; i++, que++) {
   2909   1.1    dyoung 		if (que->res != NULL)
   2910  1.34   msaitoh 			pci_intr_disestablish(adapter->osdep.pc,
   2911  1.34   msaitoh 			    adapter->osdep.ihs[i]);
   2912   1.1    dyoung 	}
   2913   1.1    dyoung #endif
   2914   1.1    dyoung 
   2915   1.1    dyoung 	/* Clean the Legacy or Link interrupt last */
   2916   1.1    dyoung 	if (adapter->linkvec) /* we are doing MSIX */
   2917  1.34   msaitoh 		rid = adapter->linkvec;
   2918   1.1    dyoung 	else
   2919  1.34   msaitoh 		rid = 0;
   2920   1.1    dyoung 
   2921  1.34   msaitoh 	if (adapter->osdep.ihs[rid] != NULL) {
   2922  1.34   msaitoh 		pci_intr_disestablish(adapter->osdep.pc,
   2923  1.34   msaitoh 		    adapter->osdep.ihs[rid]);
   2924  1.34   msaitoh 		adapter->osdep.ihs[rid] = NULL;
   2925  1.34   msaitoh 	}
   2926   1.1    dyoung 
   2927   1.1    dyoung #if defined(NETBSD_MSI_OR_MSIX)
   2928  1.34   msaitoh 	pci_intr_release(adapter->osdep.pc, adapter->osdep.intrs,
   2929  1.34   msaitoh 	    adapter->osdep.nintrs);
   2930   1.1    dyoung #endif
   2931   1.1    dyoung 
   2932   1.1    dyoung 	if (adapter->osdep.mem_size != 0) {
   2933   1.1    dyoung 		bus_space_unmap(adapter->osdep.mem_bus_space_tag,
   2934   1.1    dyoung 		    adapter->osdep.mem_bus_space_handle,
   2935   1.1    dyoung 		    adapter->osdep.mem_size);
   2936   1.1    dyoung 	}
   2937   1.1    dyoung 
   2938   1.1    dyoung 	return;
   2939   1.1    dyoung }
   2940   1.1    dyoung 
   2941   1.1    dyoung /*********************************************************************
   2942   1.1    dyoung  *
   2943   1.1    dyoung  *  Setup networking device structure and register an interface.
   2944   1.1    dyoung  *
   2945   1.1    dyoung  **********************************************************************/
   2946   1.1    dyoung static int
   2947   1.1    dyoung ixgbe_setup_interface(device_t dev, struct adapter *adapter)
   2948   1.1    dyoung {
   2949   1.1    dyoung 	struct ethercom *ec = &adapter->osdep.ec;
   2950   1.1    dyoung 	struct ixgbe_hw *hw = &adapter->hw;
   2951   1.1    dyoung 	struct ifnet   *ifp;
   2952   1.1    dyoung 
   2953   1.1    dyoung 	INIT_DEBUGOUT("ixgbe_setup_interface: begin");
   2954   1.1    dyoung 
   2955   1.1    dyoung 	ifp = adapter->ifp = &ec->ec_if;
   2956   1.1    dyoung 	strlcpy(ifp->if_xname, device_xname(dev), IFNAMSIZ);
   2957  1.26   msaitoh 	ifp->if_baudrate = IF_Gbps(10);
   2958   1.1    dyoung 	ifp->if_init = ixgbe_init;
   2959   1.1    dyoung 	ifp->if_stop = ixgbe_ifstop;
   2960   1.1    dyoung 	ifp->if_softc = adapter;
   2961   1.1    dyoung 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
   2962   1.1    dyoung 	ifp->if_ioctl = ixgbe_ioctl;
   2963  1.28   msaitoh #ifndef IXGBE_LEGACY_TX
   2964   1.1    dyoung 	ifp->if_transmit = ixgbe_mq_start;
   2965   1.1    dyoung 	ifp->if_qflush = ixgbe_qflush;
   2966  1.26   msaitoh #else
   2967  1.26   msaitoh 	ifp->if_start = ixgbe_start;
   2968  1.26   msaitoh 	IFQ_SET_MAXLEN(&ifp->if_snd, adapter->num_tx_desc - 2);
   2969  1.33   msaitoh #if 0
   2970  1.33   msaitoh 	ifp->if_snd.ifq_drv_maxlen = adapter->num_tx_desc - 2;
   2971  1.33   msaitoh #endif
   2972  1.33   msaitoh 	IFQ_SET_READY(&ifp->if_snd);
   2973   1.1    dyoung #endif
   2974   1.1    dyoung 
   2975  1.37     ozaki 	if_initialize(ifp);
   2976   1.1    dyoung 	ether_ifattach(ifp, adapter->hw.mac.addr);
   2977  1.37     ozaki 	if_register(ifp);
   2978   1.1    dyoung 	ether_set_ifflags_cb(ec, ixgbe_ifflags_cb);
   2979   1.1    dyoung 
   2980   1.1    dyoung 	adapter->max_frame_size =
   2981   1.1    dyoung 	    ifp->if_mtu + ETHER_HDR_LEN + ETHER_CRC_LEN;
   2982   1.1    dyoung 
   2983   1.1    dyoung 	/*
   2984   1.1    dyoung 	 * Tell the upper layer(s) we support long frames.
   2985   1.1    dyoung 	 */
   2986   1.1    dyoung 	ifp->if_hdrlen = sizeof(struct ether_vlan_header);
   2987   1.1    dyoung 
   2988  1.25   msaitoh 	ifp->if_capabilities |= IFCAP_HWCSUM | IFCAP_TSOv4 | IFCAP_TSOv6;
   2989   1.1    dyoung 	ifp->if_capenable = 0;
   2990   1.1    dyoung 
   2991   1.1    dyoung 	ec->ec_capabilities |= ETHERCAP_VLAN_HWCSUM;
   2992   1.1    dyoung 	ec->ec_capabilities |= ETHERCAP_JUMBO_MTU;
   2993  1.26   msaitoh 	ifp->if_capabilities |= IFCAP_LRO;
   2994  1.22   msaitoh 	ec->ec_capabilities |= ETHERCAP_VLAN_HWTAGGING
   2995  1.22   msaitoh 	    		    | ETHERCAP_VLAN_MTU;
   2996   1.1    dyoung 	ec->ec_capenable = ec->ec_capabilities;
   2997   1.1    dyoung 
   2998   1.1    dyoung 	/*
   2999  1.22   msaitoh 	** Don't turn this on by default, if vlans are
   3000   1.1    dyoung 	** created on another pseudo device (eg. lagg)
   3001   1.1    dyoung 	** then vlan events are not passed thru, breaking
   3002   1.1    dyoung 	** operation, but with HW FILTER off it works. If
   3003  1.22   msaitoh 	** using vlans directly on the ixgbe driver you can
   3004   1.1    dyoung 	** enable this and get full hardware tag filtering.
   3005   1.1    dyoung 	*/
   3006   1.1    dyoung 	ec->ec_capabilities |= ETHERCAP_VLAN_HWFILTER;
   3007   1.1    dyoung 
   3008   1.1    dyoung 	/*
   3009   1.1    dyoung 	 * Specify the media types supported by this adapter and register
   3010   1.1    dyoung 	 * callbacks to update media and link information
   3011   1.1    dyoung 	 */
   3012   1.1    dyoung 	ifmedia_init(&adapter->media, IFM_IMASK, ixgbe_media_change,
   3013   1.1    dyoung 		     ixgbe_media_status);
   3014   1.1    dyoung 	ifmedia_add(&adapter->media, IFM_ETHER | adapter->optics, 0, NULL);
   3015   1.1    dyoung 	ifmedia_set(&adapter->media, IFM_ETHER | adapter->optics);
   3016   1.1    dyoung 	if (hw->device_id == IXGBE_DEV_ID_82598AT) {
   3017   1.1    dyoung 		ifmedia_add(&adapter->media,
   3018   1.1    dyoung 		    IFM_ETHER | IFM_1000_T | IFM_FDX, 0, NULL);
   3019   1.1    dyoung 		ifmedia_add(&adapter->media,
   3020   1.1    dyoung 		    IFM_ETHER | IFM_1000_T, 0, NULL);
   3021   1.1    dyoung 	}
   3022   1.1    dyoung 	ifmedia_add(&adapter->media, IFM_ETHER | IFM_AUTO, 0, NULL);
   3023   1.1    dyoung 	ifmedia_set(&adapter->media, IFM_ETHER | IFM_AUTO);
   3024   1.1    dyoung 
   3025   1.1    dyoung 	return (0);
   3026   1.1    dyoung }
   3027   1.1    dyoung 
   3028   1.1    dyoung static void
   3029   1.1    dyoung ixgbe_config_link(struct adapter *adapter)
   3030   1.1    dyoung {
   3031   1.1    dyoung 	struct ixgbe_hw *hw = &adapter->hw;
   3032   1.1    dyoung 	u32	autoneg, err = 0;
   3033   1.1    dyoung 	bool	sfp, negotiate;
   3034   1.1    dyoung 
   3035   1.1    dyoung 	sfp = ixgbe_is_sfp(hw);
   3036   1.1    dyoung 
   3037   1.1    dyoung 	if (sfp) {
   3038  1.19  christos 		void *ip;
   3039  1.19  christos 
   3040   1.1    dyoung 		if (hw->phy.multispeed_fiber) {
   3041   1.1    dyoung 			hw->mac.ops.setup_sfp(hw);
   3042   1.1    dyoung 			ixgbe_enable_tx_laser(hw);
   3043  1.19  christos 			ip = adapter->msf_si;
   3044   1.1    dyoung 		} else {
   3045  1.19  christos 			ip = adapter->mod_si;
   3046   1.1    dyoung 		}
   3047  1.19  christos 
   3048  1.19  christos 		kpreempt_disable();
   3049  1.19  christos 		softint_schedule(ip);
   3050  1.19  christos 		kpreempt_enable();
   3051   1.1    dyoung 	} else {
   3052   1.1    dyoung 		if (hw->mac.ops.check_link)
   3053  1.28   msaitoh 			err = ixgbe_check_link(hw, &adapter->link_speed,
   3054   1.1    dyoung 			    &adapter->link_up, FALSE);
   3055   1.1    dyoung 		if (err)
   3056   1.1    dyoung 			goto out;
   3057   1.1    dyoung 		autoneg = hw->phy.autoneg_advertised;
   3058   1.1    dyoung 		if ((!autoneg) && (hw->mac.ops.get_link_capabilities))
   3059   1.1    dyoung                 	err  = hw->mac.ops.get_link_capabilities(hw,
   3060   1.1    dyoung 			    &autoneg, &negotiate);
   3061  1.13  christos 		else
   3062  1.13  christos 			negotiate = 0;
   3063   1.1    dyoung 		if (err)
   3064   1.1    dyoung 			goto out;
   3065   1.1    dyoung 		if (hw->mac.ops.setup_link)
   3066  1.28   msaitoh                 	err = hw->mac.ops.setup_link(hw,
   3067  1.28   msaitoh 			    autoneg, adapter->link_up);
   3068   1.1    dyoung 	}
   3069   1.1    dyoung out:
   3070   1.1    dyoung 	return;
   3071   1.1    dyoung }
   3072   1.1    dyoung 
   3073   1.1    dyoung /********************************************************************
   3074   1.1    dyoung  * Manage DMA'able memory.
   3075   1.1    dyoung  *******************************************************************/
   3076   1.1    dyoung 
   3077   1.1    dyoung static int
   3078   1.1    dyoung ixgbe_dma_malloc(struct adapter *adapter, const bus_size_t size,
   3079   1.1    dyoung 		struct ixgbe_dma_alloc *dma, const int mapflags)
   3080   1.1    dyoung {
   3081   1.1    dyoung 	device_t dev = adapter->dev;
   3082   1.1    dyoung 	int             r, rsegs;
   3083   1.1    dyoung 
   3084   1.1    dyoung 	r = ixgbe_dma_tag_create(adapter->osdep.dmat,	/* parent */
   3085   1.1    dyoung 			       DBA_ALIGN, 0,	/* alignment, bounds */
   3086   1.1    dyoung 			       size,	/* maxsize */
   3087   1.1    dyoung 			       1,	/* nsegments */
   3088   1.1    dyoung 			       size,	/* maxsegsize */
   3089   1.1    dyoung 			       BUS_DMA_ALLOCNOW,	/* flags */
   3090   1.1    dyoung 			       &dma->dma_tag);
   3091   1.1    dyoung 	if (r != 0) {
   3092   1.1    dyoung 		aprint_error_dev(dev,
   3093   1.1    dyoung 		    "%s: ixgbe_dma_tag_create failed; error %d\n", __func__, r);
   3094   1.1    dyoung 		goto fail_0;
   3095   1.1    dyoung 	}
   3096   1.1    dyoung 
   3097   1.1    dyoung 	r = bus_dmamem_alloc(dma->dma_tag->dt_dmat,
   3098   1.1    dyoung 		size,
   3099   1.1    dyoung 		dma->dma_tag->dt_alignment,
   3100   1.1    dyoung 		dma->dma_tag->dt_boundary,
   3101   1.1    dyoung 		&dma->dma_seg, 1, &rsegs, BUS_DMA_NOWAIT);
   3102   1.1    dyoung 	if (r != 0) {
   3103   1.1    dyoung 		aprint_error_dev(dev,
   3104   1.1    dyoung 		    "%s: bus_dmamem_alloc failed; error %d\n", __func__, r);
   3105   1.1    dyoung 		goto fail_1;
   3106   1.1    dyoung 	}
   3107   1.1    dyoung 
   3108   1.1    dyoung 	r = bus_dmamem_map(dma->dma_tag->dt_dmat, &dma->dma_seg, rsegs,
   3109   1.1    dyoung 	    size, &dma->dma_vaddr, BUS_DMA_NOWAIT);
   3110   1.1    dyoung 	if (r != 0) {
   3111   1.1    dyoung 		aprint_error_dev(dev, "%s: bus_dmamem_map failed; error %d\n",
   3112   1.1    dyoung 		    __func__, r);
   3113   1.1    dyoung 		goto fail_2;
   3114   1.1    dyoung 	}
   3115   1.1    dyoung 
   3116   1.1    dyoung 	r = ixgbe_dmamap_create(dma->dma_tag, 0, &dma->dma_map);
   3117   1.1    dyoung 	if (r != 0) {
   3118   1.1    dyoung 		aprint_error_dev(dev, "%s: bus_dmamem_map failed; error %d\n",
   3119   1.1    dyoung 		    __func__, r);
   3120   1.1    dyoung 		goto fail_3;
   3121   1.1    dyoung 	}
   3122   1.1    dyoung 
   3123   1.1    dyoung 	r = bus_dmamap_load(dma->dma_tag->dt_dmat, dma->dma_map, dma->dma_vaddr,
   3124   1.1    dyoung 			    size,
   3125   1.1    dyoung 			    NULL,
   3126   1.1    dyoung 			    mapflags | BUS_DMA_NOWAIT);
   3127   1.1    dyoung 	if (r != 0) {
   3128   1.1    dyoung 		aprint_error_dev(dev, "%s: bus_dmamap_load failed; error %d\n",
   3129   1.1    dyoung 		    __func__, r);
   3130   1.1    dyoung 		goto fail_4;
   3131   1.1    dyoung 	}
   3132   1.1    dyoung 	dma->dma_paddr = dma->dma_map->dm_segs[0].ds_addr;
   3133   1.1    dyoung 	dma->dma_size = size;
   3134   1.1    dyoung 	return 0;
   3135   1.1    dyoung fail_4:
   3136   1.1    dyoung 	ixgbe_dmamap_destroy(dma->dma_tag, dma->dma_map);
   3137   1.1    dyoung fail_3:
   3138   1.1    dyoung 	bus_dmamem_unmap(dma->dma_tag->dt_dmat, dma->dma_vaddr, size);
   3139   1.1    dyoung fail_2:
   3140   1.1    dyoung 	bus_dmamem_free(dma->dma_tag->dt_dmat, &dma->dma_seg, rsegs);
   3141   1.1    dyoung fail_1:
   3142   1.1    dyoung 	ixgbe_dma_tag_destroy(dma->dma_tag);
   3143   1.1    dyoung fail_0:
   3144   1.1    dyoung 	return r;
   3145   1.1    dyoung }
   3146   1.1    dyoung 
   3147   1.1    dyoung static void
   3148   1.1    dyoung ixgbe_dma_free(struct adapter *adapter, struct ixgbe_dma_alloc *dma)
   3149   1.1    dyoung {
   3150   1.1    dyoung 	bus_dmamap_sync(dma->dma_tag->dt_dmat, dma->dma_map, 0, dma->dma_size,
   3151   1.1    dyoung 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
   3152   1.1    dyoung 	ixgbe_dmamap_unload(dma->dma_tag, dma->dma_map);
   3153   1.1    dyoung 	bus_dmamem_free(dma->dma_tag->dt_dmat, &dma->dma_seg, 1);
   3154   1.1    dyoung 	ixgbe_dma_tag_destroy(dma->dma_tag);
   3155   1.1    dyoung }
   3156   1.1    dyoung 
   3157   1.1    dyoung 
   3158   1.1    dyoung /*********************************************************************
   3159   1.1    dyoung  *
   3160   1.1    dyoung  *  Allocate memory for the transmit and receive rings, and then
   3161   1.1    dyoung  *  the descriptors associated with each, called only once at attach.
   3162   1.1    dyoung  *
   3163   1.1    dyoung  **********************************************************************/
   3164   1.1    dyoung static int
   3165   1.1    dyoung ixgbe_allocate_queues(struct adapter *adapter)
   3166   1.1    dyoung {
   3167   1.1    dyoung 	device_t	dev = adapter->dev;
   3168   1.1    dyoung 	struct ix_queue	*que;
   3169   1.1    dyoung 	struct tx_ring	*txr;
   3170   1.1    dyoung 	struct rx_ring	*rxr;
   3171   1.1    dyoung 	int rsize, tsize, error = IXGBE_SUCCESS;
   3172   1.1    dyoung 	int txconf = 0, rxconf = 0;
   3173   1.1    dyoung 
   3174   1.1    dyoung         /* First allocate the top level queue structs */
   3175   1.1    dyoung         if (!(adapter->queues =
   3176   1.1    dyoung             (struct ix_queue *) malloc(sizeof(struct ix_queue) *
   3177   1.1    dyoung             adapter->num_queues, M_DEVBUF, M_NOWAIT | M_ZERO))) {
   3178   1.1    dyoung                 aprint_error_dev(dev, "Unable to allocate queue memory\n");
   3179   1.1    dyoung                 error = ENOMEM;
   3180   1.1    dyoung                 goto fail;
   3181   1.1    dyoung         }
   3182   1.1    dyoung 
   3183   1.1    dyoung 	/* First allocate the TX ring struct memory */
   3184   1.1    dyoung 	if (!(adapter->tx_rings =
   3185   1.1    dyoung 	    (struct tx_ring *) malloc(sizeof(struct tx_ring) *
   3186   1.1    dyoung 	    adapter->num_queues, M_DEVBUF, M_NOWAIT | M_ZERO))) {
   3187   1.1    dyoung 		aprint_error_dev(dev, "Unable to allocate TX ring memory\n");
   3188   1.1    dyoung 		error = ENOMEM;
   3189   1.1    dyoung 		goto tx_fail;
   3190   1.1    dyoung 	}
   3191   1.1    dyoung 
   3192   1.1    dyoung 	/* Next allocate the RX */
   3193   1.1    dyoung 	if (!(adapter->rx_rings =
   3194   1.1    dyoung 	    (struct rx_ring *) malloc(sizeof(struct rx_ring) *
   3195   1.1    dyoung 	    adapter->num_queues, M_DEVBUF, M_NOWAIT | M_ZERO))) {
   3196   1.1    dyoung 		aprint_error_dev(dev, "Unable to allocate RX ring memory\n");
   3197   1.1    dyoung 		error = ENOMEM;
   3198   1.1    dyoung 		goto rx_fail;
   3199   1.1    dyoung 	}
   3200   1.1    dyoung 
   3201   1.1    dyoung 	/* For the ring itself */
   3202   1.1    dyoung 	tsize = roundup2(adapter->num_tx_desc *
   3203   1.1    dyoung 	    sizeof(union ixgbe_adv_tx_desc), DBA_ALIGN);
   3204   1.1    dyoung 
   3205   1.1    dyoung 	/*
   3206   1.1    dyoung 	 * Now set up the TX queues, txconf is needed to handle the
   3207   1.1    dyoung 	 * possibility that things fail midcourse and we need to
   3208   1.1    dyoung 	 * undo memory gracefully
   3209   1.1    dyoung 	 */
   3210   1.1    dyoung 	for (int i = 0; i < adapter->num_queues; i++, txconf++) {
   3211   1.1    dyoung 		/* Set up some basics */
   3212   1.1    dyoung 		txr = &adapter->tx_rings[i];
   3213   1.1    dyoung 		txr->adapter = adapter;
   3214   1.1    dyoung 		txr->me = i;
   3215  1.28   msaitoh 		txr->num_desc = adapter->num_tx_desc;
   3216   1.1    dyoung 
   3217   1.1    dyoung 		/* Initialize the TX side lock */
   3218   1.1    dyoung 		snprintf(txr->mtx_name, sizeof(txr->mtx_name), "%s:tx(%d)",
   3219   1.1    dyoung 		    device_xname(dev), txr->me);
   3220   1.1    dyoung 		mutex_init(&txr->tx_mtx, MUTEX_DEFAULT, IPL_NET);
   3221   1.1    dyoung 
   3222   1.1    dyoung 		if (ixgbe_dma_malloc(adapter, tsize,
   3223   1.1    dyoung 			&txr->txdma, BUS_DMA_NOWAIT)) {
   3224   1.1    dyoung 			aprint_error_dev(dev,
   3225   1.1    dyoung 			    "Unable to allocate TX Descriptor memory\n");
   3226   1.1    dyoung 			error = ENOMEM;
   3227   1.1    dyoung 			goto err_tx_desc;
   3228   1.1    dyoung 		}
   3229   1.1    dyoung 		txr->tx_base = (union ixgbe_adv_tx_desc *)txr->txdma.dma_vaddr;
   3230   1.1    dyoung 		bzero((void *)txr->tx_base, tsize);
   3231   1.1    dyoung 
   3232   1.1    dyoung         	/* Now allocate transmit buffers for the ring */
   3233   1.1    dyoung         	if (ixgbe_allocate_transmit_buffers(txr)) {
   3234   1.1    dyoung 			aprint_error_dev(dev,
   3235   1.1    dyoung 			    "Critical Failure setting up transmit buffers\n");
   3236   1.1    dyoung 			error = ENOMEM;
   3237   1.1    dyoung 			goto err_tx_desc;
   3238   1.1    dyoung         	}
   3239  1.28   msaitoh #ifndef IXGBE_LEGACY_TX
   3240   1.1    dyoung 		/* Allocate a buf ring */
   3241   1.1    dyoung 		txr->br = buf_ring_alloc(IXGBE_BR_SIZE, M_DEVBUF,
   3242   1.1    dyoung 		    M_WAITOK, &txr->tx_mtx);
   3243   1.1    dyoung 		if (txr->br == NULL) {
   3244   1.1    dyoung 			aprint_error_dev(dev,
   3245   1.1    dyoung 			    "Critical Failure setting up buf ring\n");
   3246   1.1    dyoung 			error = ENOMEM;
   3247   1.1    dyoung 			goto err_tx_desc;
   3248   1.1    dyoung         	}
   3249   1.1    dyoung #endif
   3250   1.1    dyoung 	}
   3251   1.1    dyoung 
   3252   1.1    dyoung 	/*
   3253   1.1    dyoung 	 * Next the RX queues...
   3254   1.1    dyoung 	 */
   3255   1.1    dyoung 	rsize = roundup2(adapter->num_rx_desc *
   3256   1.1    dyoung 	    sizeof(union ixgbe_adv_rx_desc), DBA_ALIGN);
   3257   1.1    dyoung 	for (int i = 0; i < adapter->num_queues; i++, rxconf++) {
   3258   1.1    dyoung 		rxr = &adapter->rx_rings[i];
   3259   1.1    dyoung 		/* Set up some basics */
   3260   1.1    dyoung 		rxr->adapter = adapter;
   3261   1.1    dyoung 		rxr->me = i;
   3262  1.28   msaitoh 		rxr->num_desc = adapter->num_rx_desc;
   3263   1.1    dyoung 
   3264   1.1    dyoung 		/* Initialize the RX side lock */
   3265   1.1    dyoung 		snprintf(rxr->mtx_name, sizeof(rxr->mtx_name), "%s:rx(%d)",
   3266   1.1    dyoung 		    device_xname(dev), rxr->me);
   3267   1.1    dyoung 		mutex_init(&rxr->rx_mtx, MUTEX_DEFAULT, IPL_NET);
   3268   1.1    dyoung 
   3269   1.1    dyoung 		if (ixgbe_dma_malloc(adapter, rsize,
   3270   1.1    dyoung 			&rxr->rxdma, BUS_DMA_NOWAIT)) {
   3271   1.1    dyoung 			aprint_error_dev(dev,
   3272   1.1    dyoung 			    "Unable to allocate RxDescriptor memory\n");
   3273   1.1    dyoung 			error = ENOMEM;
   3274   1.1    dyoung 			goto err_rx_desc;
   3275   1.1    dyoung 		}
   3276   1.1    dyoung 		rxr->rx_base = (union ixgbe_adv_rx_desc *)rxr->rxdma.dma_vaddr;
   3277   1.1    dyoung 		bzero((void *)rxr->rx_base, rsize);
   3278   1.1    dyoung 
   3279   1.1    dyoung         	/* Allocate receive buffers for the ring*/
   3280   1.1    dyoung 		if (ixgbe_allocate_receive_buffers(rxr)) {
   3281   1.1    dyoung 			aprint_error_dev(dev,
   3282   1.1    dyoung 			    "Critical Failure setting up receive buffers\n");
   3283   1.1    dyoung 			error = ENOMEM;
   3284   1.1    dyoung 			goto err_rx_desc;
   3285   1.1    dyoung 		}
   3286   1.1    dyoung 	}
   3287   1.1    dyoung 
   3288   1.1    dyoung 	/*
   3289   1.1    dyoung 	** Finally set up the queue holding structs
   3290   1.1    dyoung 	*/
   3291   1.1    dyoung 	for (int i = 0; i < adapter->num_queues; i++) {
   3292   1.1    dyoung 		que = &adapter->queues[i];
   3293   1.1    dyoung 		que->adapter = adapter;
   3294   1.1    dyoung 		que->txr = &adapter->tx_rings[i];
   3295   1.1    dyoung 		que->rxr = &adapter->rx_rings[i];
   3296   1.1    dyoung 	}
   3297   1.1    dyoung 
   3298   1.1    dyoung 	return (0);
   3299   1.1    dyoung 
   3300   1.1    dyoung err_rx_desc:
   3301   1.1    dyoung 	for (rxr = adapter->rx_rings; rxconf > 0; rxr++, rxconf--)
   3302   1.1    dyoung 		ixgbe_dma_free(adapter, &rxr->rxdma);
   3303   1.1    dyoung err_tx_desc:
   3304   1.1    dyoung 	for (txr = adapter->tx_rings; txconf > 0; txr++, txconf--)
   3305   1.1    dyoung 		ixgbe_dma_free(adapter, &txr->txdma);
   3306   1.1    dyoung 	free(adapter->rx_rings, M_DEVBUF);
   3307   1.1    dyoung rx_fail:
   3308   1.1    dyoung 	free(adapter->tx_rings, M_DEVBUF);
   3309   1.1    dyoung tx_fail:
   3310   1.1    dyoung 	free(adapter->queues, M_DEVBUF);
   3311   1.1    dyoung fail:
   3312   1.1    dyoung 	return (error);
   3313   1.1    dyoung }
   3314   1.1    dyoung 
   3315   1.1    dyoung /*********************************************************************
   3316   1.1    dyoung  *
   3317   1.1    dyoung  *  Allocate memory for tx_buffer structures. The tx_buffer stores all
   3318   1.1    dyoung  *  the information needed to transmit a packet on the wire. This is
   3319   1.1    dyoung  *  called only once at attach, setup is done every reset.
   3320   1.1    dyoung  *
   3321   1.1    dyoung  **********************************************************************/
   3322   1.1    dyoung static int
   3323   1.1    dyoung ixgbe_allocate_transmit_buffers(struct tx_ring *txr)
   3324   1.1    dyoung {
   3325   1.1    dyoung 	struct adapter *adapter = txr->adapter;
   3326   1.1    dyoung 	device_t dev = adapter->dev;
   3327   1.1    dyoung 	struct ixgbe_tx_buf *txbuf;
   3328   1.1    dyoung 	int error, i;
   3329   1.1    dyoung 
   3330   1.1    dyoung 	/*
   3331   1.1    dyoung 	 * Setup DMA descriptor areas.
   3332   1.1    dyoung 	 */
   3333   1.1    dyoung 	if ((error = ixgbe_dma_tag_create(adapter->osdep.dmat,	/* parent */
   3334   1.1    dyoung 			       1, 0,		/* alignment, bounds */
   3335   1.1    dyoung 			       IXGBE_TSO_SIZE,		/* maxsize */
   3336   1.1    dyoung 			       adapter->num_segs,	/* nsegments */
   3337   1.1    dyoung 			       PAGE_SIZE,		/* maxsegsize */
   3338   1.1    dyoung 			       0,			/* flags */
   3339   1.1    dyoung 			       &txr->txtag))) {
   3340   1.1    dyoung 		aprint_error_dev(dev,"Unable to allocate TX DMA tag\n");
   3341   1.1    dyoung 		goto fail;
   3342   1.1    dyoung 	}
   3343   1.1    dyoung 
   3344   1.1    dyoung 	if (!(txr->tx_buffers =
   3345   1.1    dyoung 	    (struct ixgbe_tx_buf *) malloc(sizeof(struct ixgbe_tx_buf) *
   3346   1.1    dyoung 	    adapter->num_tx_desc, M_DEVBUF, M_NOWAIT | M_ZERO))) {
   3347   1.1    dyoung 		aprint_error_dev(dev, "Unable to allocate tx_buffer memory\n");
   3348   1.1    dyoung 		error = ENOMEM;
   3349   1.1    dyoung 		goto fail;
   3350   1.1    dyoung 	}
   3351   1.1    dyoung 
   3352   1.1    dyoung         /* Create the descriptor buffer dma maps */
   3353   1.1    dyoung 	txbuf = txr->tx_buffers;
   3354   1.1    dyoung 	for (i = 0; i < adapter->num_tx_desc; i++, txbuf++) {
   3355   1.1    dyoung 		error = ixgbe_dmamap_create(txr->txtag, 0, &txbuf->map);
   3356   1.1    dyoung 		if (error != 0) {
   3357  1.25   msaitoh 			aprint_error_dev(dev,
   3358  1.25   msaitoh 			    "Unable to create TX DMA map (%d)\n", error);
   3359   1.1    dyoung 			goto fail;
   3360   1.1    dyoung 		}
   3361   1.1    dyoung 	}
   3362   1.1    dyoung 
   3363   1.1    dyoung 	return 0;
   3364   1.1    dyoung fail:
   3365   1.1    dyoung 	/* We free all, it handles case where we are in the middle */
   3366   1.1    dyoung 	ixgbe_free_transmit_structures(adapter);
   3367   1.1    dyoung 	return (error);
   3368   1.1    dyoung }
   3369   1.1    dyoung 
   3370   1.1    dyoung /*********************************************************************
   3371   1.1    dyoung  *
   3372   1.1    dyoung  *  Initialize a transmit ring.
   3373   1.1    dyoung  *
   3374   1.1    dyoung  **********************************************************************/
   3375   1.1    dyoung static void
   3376   1.1    dyoung ixgbe_setup_transmit_ring(struct tx_ring *txr)
   3377   1.1    dyoung {
   3378   1.1    dyoung 	struct adapter *adapter = txr->adapter;
   3379   1.1    dyoung 	struct ixgbe_tx_buf *txbuf;
   3380   1.1    dyoung 	int i;
   3381  1.22   msaitoh #ifdef DEV_NETMAP
   3382  1.22   msaitoh 	struct netmap_adapter *na = NA(adapter->ifp);
   3383  1.22   msaitoh 	struct netmap_slot *slot;
   3384  1.22   msaitoh #endif /* DEV_NETMAP */
   3385   1.1    dyoung 
   3386   1.1    dyoung 	/* Clear the old ring contents */
   3387   1.1    dyoung 	IXGBE_TX_LOCK(txr);
   3388  1.22   msaitoh #ifdef DEV_NETMAP
   3389  1.22   msaitoh 	/*
   3390  1.22   msaitoh 	 * (under lock): if in netmap mode, do some consistency
   3391  1.22   msaitoh 	 * checks and set slot to entry 0 of the netmap ring.
   3392  1.22   msaitoh 	 */
   3393  1.22   msaitoh 	slot = netmap_reset(na, NR_TX, txr->me, 0);
   3394  1.22   msaitoh #endif /* DEV_NETMAP */
   3395   1.1    dyoung 	bzero((void *)txr->tx_base,
   3396   1.1    dyoung 	      (sizeof(union ixgbe_adv_tx_desc)) * adapter->num_tx_desc);
   3397   1.1    dyoung 	/* Reset indices */
   3398   1.1    dyoung 	txr->next_avail_desc = 0;
   3399   1.1    dyoung 	txr->next_to_clean = 0;
   3400   1.1    dyoung 
   3401   1.1    dyoung 	/* Free any existing tx buffers. */
   3402   1.1    dyoung         txbuf = txr->tx_buffers;
   3403  1.28   msaitoh 	for (i = 0; i < txr->num_desc; i++, txbuf++) {
   3404   1.1    dyoung 		if (txbuf->m_head != NULL) {
   3405   1.1    dyoung 			bus_dmamap_sync(txr->txtag->dt_dmat, txbuf->map,
   3406   1.1    dyoung 			    0, txbuf->m_head->m_pkthdr.len,
   3407   1.1    dyoung 			    BUS_DMASYNC_POSTWRITE);
   3408   1.1    dyoung 			ixgbe_dmamap_unload(txr->txtag, txbuf->map);
   3409   1.1    dyoung 			m_freem(txbuf->m_head);
   3410   1.1    dyoung 			txbuf->m_head = NULL;
   3411   1.1    dyoung 		}
   3412  1.22   msaitoh #ifdef DEV_NETMAP
   3413  1.22   msaitoh 		/*
   3414  1.22   msaitoh 		 * In netmap mode, set the map for the packet buffer.
   3415  1.22   msaitoh 		 * NOTE: Some drivers (not this one) also need to set
   3416  1.22   msaitoh 		 * the physical buffer address in the NIC ring.
   3417  1.22   msaitoh 		 * Slots in the netmap ring (indexed by "si") are
   3418  1.22   msaitoh 		 * kring->nkr_hwofs positions "ahead" wrt the
   3419  1.22   msaitoh 		 * corresponding slot in the NIC ring. In some drivers
   3420  1.25   msaitoh 		 * (not here) nkr_hwofs can be negative. Function
   3421  1.25   msaitoh 		 * netmap_idx_n2k() handles wraparounds properly.
   3422  1.22   msaitoh 		 */
   3423  1.22   msaitoh 		if (slot) {
   3424  1.25   msaitoh 			int si = netmap_idx_n2k(&na->tx_rings[txr->me], i);
   3425  1.33   msaitoh 			netmap_load_map(na, txr->txtag, txbuf->map, NMB(na, slot + si));
   3426  1.22   msaitoh 		}
   3427  1.22   msaitoh #endif /* DEV_NETMAP */
   3428  1.28   msaitoh 		/* Clear the EOP descriptor pointer */
   3429  1.28   msaitoh 		txbuf->eop = NULL;
   3430   1.1    dyoung         }
   3431   1.1    dyoung 
   3432   1.1    dyoung #ifdef IXGBE_FDIR
   3433   1.1    dyoung 	/* Set the rate at which we sample packets */
   3434   1.1    dyoung 	if (adapter->hw.mac.type != ixgbe_mac_82598EB)
   3435   1.1    dyoung 		txr->atr_sample = atr_sample_rate;
   3436   1.1    dyoung #endif
   3437   1.1    dyoung 
   3438   1.1    dyoung 	/* Set number of descriptors available */
   3439   1.1    dyoung 	txr->tx_avail = adapter->num_tx_desc;
   3440   1.1    dyoung 
   3441   1.1    dyoung 	ixgbe_dmamap_sync(txr->txdma.dma_tag, txr->txdma.dma_map,
   3442   1.1    dyoung 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
   3443   1.1    dyoung 	IXGBE_TX_UNLOCK(txr);
   3444   1.1    dyoung }
   3445   1.1    dyoung 
   3446   1.1    dyoung /*********************************************************************
   3447   1.1    dyoung  *
   3448   1.1    dyoung  *  Initialize all transmit rings.
   3449   1.1    dyoung  *
   3450   1.1    dyoung  **********************************************************************/
   3451   1.1    dyoung static int
   3452   1.1    dyoung ixgbe_setup_transmit_structures(struct adapter *adapter)
   3453   1.1    dyoung {
   3454   1.1    dyoung 	struct tx_ring *txr = adapter->tx_rings;
   3455   1.1    dyoung 
   3456   1.1    dyoung 	for (int i = 0; i < adapter->num_queues; i++, txr++)
   3457   1.1    dyoung 		ixgbe_setup_transmit_ring(txr);
   3458   1.1    dyoung 
   3459   1.1    dyoung 	return (0);
   3460   1.1    dyoung }
   3461   1.1    dyoung 
   3462   1.1    dyoung /*********************************************************************
   3463   1.1    dyoung  *
   3464   1.1    dyoung  *  Enable transmit unit.
   3465   1.1    dyoung  *
   3466   1.1    dyoung  **********************************************************************/
   3467   1.1    dyoung static void
   3468   1.1    dyoung ixgbe_initialize_transmit_units(struct adapter *adapter)
   3469   1.1    dyoung {
   3470   1.1    dyoung 	struct tx_ring	*txr = adapter->tx_rings;
   3471   1.1    dyoung 	struct ixgbe_hw	*hw = &adapter->hw;
   3472   1.1    dyoung 
   3473   1.1    dyoung 	/* Setup the Base and Length of the Tx Descriptor Ring */
   3474   1.1    dyoung 
   3475   1.1    dyoung 	for (int i = 0; i < adapter->num_queues; i++, txr++) {
   3476   1.1    dyoung 		u64	tdba = txr->txdma.dma_paddr;
   3477   1.1    dyoung 		u32	txctrl;
   3478   1.1    dyoung 
   3479   1.1    dyoung 		IXGBE_WRITE_REG(hw, IXGBE_TDBAL(i),
   3480   1.1    dyoung 		       (tdba & 0x00000000ffffffffULL));
   3481   1.1    dyoung 		IXGBE_WRITE_REG(hw, IXGBE_TDBAH(i), (tdba >> 32));
   3482   1.1    dyoung 		IXGBE_WRITE_REG(hw, IXGBE_TDLEN(i),
   3483  1.28   msaitoh 		    adapter->num_tx_desc * sizeof(union ixgbe_adv_tx_desc));
   3484   1.1    dyoung 
   3485   1.1    dyoung 		/* Setup the HW Tx Head and Tail descriptor pointers */
   3486   1.1    dyoung 		IXGBE_WRITE_REG(hw, IXGBE_TDH(i), 0);
   3487   1.1    dyoung 		IXGBE_WRITE_REG(hw, IXGBE_TDT(i), 0);
   3488   1.1    dyoung 
   3489   1.1    dyoung 		/* Setup Transmit Descriptor Cmd Settings */
   3490   1.1    dyoung 		txr->txd_cmd = IXGBE_TXD_CMD_IFCS;
   3491   1.1    dyoung 		txr->queue_status = IXGBE_QUEUE_IDLE;
   3492   1.1    dyoung 
   3493  1.28   msaitoh 		/* Set the processing limit */
   3494  1.28   msaitoh 		txr->process_limit = ixgbe_tx_process_limit;
   3495  1.28   msaitoh 
   3496   1.1    dyoung 		/* Disable Head Writeback */
   3497   1.1    dyoung 		switch (hw->mac.type) {
   3498   1.1    dyoung 		case ixgbe_mac_82598EB:
   3499   1.1    dyoung 			txctrl = IXGBE_READ_REG(hw, IXGBE_DCA_TXCTRL(i));
   3500   1.1    dyoung 			break;
   3501   1.1    dyoung 		case ixgbe_mac_82599EB:
   3502  1.24   msaitoh 		case ixgbe_mac_X540:
   3503   1.1    dyoung 		default:
   3504   1.1    dyoung 			txctrl = IXGBE_READ_REG(hw, IXGBE_DCA_TXCTRL_82599(i));
   3505   1.1    dyoung 			break;
   3506   1.1    dyoung                 }
   3507  1.25   msaitoh 		txctrl &= ~IXGBE_DCA_TXCTRL_DESC_WRO_EN;
   3508   1.1    dyoung 		switch (hw->mac.type) {
   3509   1.1    dyoung 		case ixgbe_mac_82598EB:
   3510   1.1    dyoung 			IXGBE_WRITE_REG(hw, IXGBE_DCA_TXCTRL(i), txctrl);
   3511   1.1    dyoung 			break;
   3512   1.1    dyoung 		case ixgbe_mac_82599EB:
   3513  1.24   msaitoh 		case ixgbe_mac_X540:
   3514   1.1    dyoung 		default:
   3515   1.1    dyoung 			IXGBE_WRITE_REG(hw, IXGBE_DCA_TXCTRL_82599(i), txctrl);
   3516   1.1    dyoung 			break;
   3517   1.1    dyoung 		}
   3518   1.1    dyoung 
   3519   1.1    dyoung 	}
   3520   1.1    dyoung 
   3521   1.1    dyoung 	if (hw->mac.type != ixgbe_mac_82598EB) {
   3522   1.1    dyoung 		u32 dmatxctl, rttdcs;
   3523   1.1    dyoung 		dmatxctl = IXGBE_READ_REG(hw, IXGBE_DMATXCTL);
   3524   1.1    dyoung 		dmatxctl |= IXGBE_DMATXCTL_TE;
   3525   1.1    dyoung 		IXGBE_WRITE_REG(hw, IXGBE_DMATXCTL, dmatxctl);
   3526   1.1    dyoung 		/* Disable arbiter to set MTQC */
   3527   1.1    dyoung 		rttdcs = IXGBE_READ_REG(hw, IXGBE_RTTDCS);
   3528   1.1    dyoung 		rttdcs |= IXGBE_RTTDCS_ARBDIS;
   3529   1.1    dyoung 		IXGBE_WRITE_REG(hw, IXGBE_RTTDCS, rttdcs);
   3530   1.1    dyoung 		IXGBE_WRITE_REG(hw, IXGBE_MTQC, IXGBE_MTQC_64Q_1PB);
   3531   1.1    dyoung 		rttdcs &= ~IXGBE_RTTDCS_ARBDIS;
   3532   1.1    dyoung 		IXGBE_WRITE_REG(hw, IXGBE_RTTDCS, rttdcs);
   3533   1.1    dyoung 	}
   3534   1.1    dyoung 
   3535   1.1    dyoung 	return;
   3536   1.1    dyoung }
   3537   1.1    dyoung 
   3538   1.1    dyoung /*********************************************************************
   3539   1.1    dyoung  *
   3540   1.1    dyoung  *  Free all transmit rings.
   3541   1.1    dyoung  *
   3542   1.1    dyoung  **********************************************************************/
   3543   1.1    dyoung static void
   3544   1.1    dyoung ixgbe_free_transmit_structures(struct adapter *adapter)
   3545   1.1    dyoung {
   3546   1.1    dyoung 	struct tx_ring *txr = adapter->tx_rings;
   3547   1.1    dyoung 
   3548   1.1    dyoung 	for (int i = 0; i < adapter->num_queues; i++, txr++) {
   3549   1.1    dyoung 		ixgbe_free_transmit_buffers(txr);
   3550   1.1    dyoung 		ixgbe_dma_free(adapter, &txr->txdma);
   3551   1.1    dyoung 		IXGBE_TX_LOCK_DESTROY(txr);
   3552   1.1    dyoung 	}
   3553   1.1    dyoung 	free(adapter->tx_rings, M_DEVBUF);
   3554   1.1    dyoung }
   3555   1.1    dyoung 
   3556   1.1    dyoung /*********************************************************************
   3557   1.1    dyoung  *
   3558   1.1    dyoung  *  Free transmit ring related data structures.
   3559   1.1    dyoung  *
   3560   1.1    dyoung  **********************************************************************/
   3561   1.1    dyoung static void
   3562   1.1    dyoung ixgbe_free_transmit_buffers(struct tx_ring *txr)
   3563   1.1    dyoung {
   3564   1.1    dyoung 	struct adapter *adapter = txr->adapter;
   3565   1.1    dyoung 	struct ixgbe_tx_buf *tx_buffer;
   3566   1.1    dyoung 	int             i;
   3567   1.1    dyoung 
   3568  1.33   msaitoh 	INIT_DEBUGOUT("ixgbe_free_transmit_ring: begin");
   3569   1.1    dyoung 
   3570   1.1    dyoung 	if (txr->tx_buffers == NULL)
   3571   1.1    dyoung 		return;
   3572   1.1    dyoung 
   3573   1.1    dyoung 	tx_buffer = txr->tx_buffers;
   3574   1.1    dyoung 	for (i = 0; i < adapter->num_tx_desc; i++, tx_buffer++) {
   3575   1.1    dyoung 		if (tx_buffer->m_head != NULL) {
   3576   1.1    dyoung 			bus_dmamap_sync(txr->txtag->dt_dmat, tx_buffer->map,
   3577   1.1    dyoung 			    0, tx_buffer->m_head->m_pkthdr.len,
   3578   1.1    dyoung 			    BUS_DMASYNC_POSTWRITE);
   3579   1.1    dyoung 			ixgbe_dmamap_unload(txr->txtag, tx_buffer->map);
   3580   1.1    dyoung 			m_freem(tx_buffer->m_head);
   3581   1.1    dyoung 			tx_buffer->m_head = NULL;
   3582   1.1    dyoung 			if (tx_buffer->map != NULL) {
   3583   1.1    dyoung 				ixgbe_dmamap_destroy(txr->txtag,
   3584   1.1    dyoung 				    tx_buffer->map);
   3585   1.1    dyoung 				tx_buffer->map = NULL;
   3586   1.1    dyoung 			}
   3587   1.1    dyoung 		} else if (tx_buffer->map != NULL) {
   3588   1.1    dyoung 			ixgbe_dmamap_unload(txr->txtag, tx_buffer->map);
   3589   1.1    dyoung 			ixgbe_dmamap_destroy(txr->txtag, tx_buffer->map);
   3590   1.1    dyoung 			tx_buffer->map = NULL;
   3591   1.1    dyoung 		}
   3592   1.1    dyoung 	}
   3593  1.28   msaitoh #ifndef IXGBE_LEGACY_TX
   3594   1.1    dyoung 	if (txr->br != NULL)
   3595   1.1    dyoung 		buf_ring_free(txr->br, M_DEVBUF);
   3596   1.1    dyoung #endif
   3597   1.1    dyoung 	if (txr->tx_buffers != NULL) {
   3598   1.1    dyoung 		free(txr->tx_buffers, M_DEVBUF);
   3599   1.1    dyoung 		txr->tx_buffers = NULL;
   3600   1.1    dyoung 	}
   3601   1.1    dyoung 	if (txr->txtag != NULL) {
   3602   1.1    dyoung 		ixgbe_dma_tag_destroy(txr->txtag);
   3603   1.1    dyoung 		txr->txtag = NULL;
   3604   1.1    dyoung 	}
   3605   1.1    dyoung 	return;
   3606   1.1    dyoung }
   3607   1.1    dyoung 
   3608   1.1    dyoung /*********************************************************************
   3609   1.1    dyoung  *
   3610  1.28   msaitoh  *  Advanced Context Descriptor setup for VLAN, CSUM or TSO
   3611   1.1    dyoung  *
   3612   1.1    dyoung  **********************************************************************/
   3613   1.1    dyoung 
   3614  1.28   msaitoh static int
   3615  1.28   msaitoh ixgbe_tx_ctx_setup(struct tx_ring *txr, struct mbuf *mp,
   3616  1.28   msaitoh     u32 *cmd_type_len, u32 *olinfo_status)
   3617   1.1    dyoung {
   3618   1.1    dyoung 	struct m_tag *mtag;
   3619   1.1    dyoung 	struct adapter *adapter = txr->adapter;
   3620   1.1    dyoung 	struct ethercom *ec = &adapter->osdep.ec;
   3621   1.1    dyoung 	struct ixgbe_adv_tx_context_desc *TXD;
   3622   1.1    dyoung 	struct ether_vlan_header *eh;
   3623   1.1    dyoung 	struct ip ip;
   3624   1.1    dyoung 	struct ip6_hdr ip6;
   3625  1.28   msaitoh 	u32 vlan_macip_lens = 0, type_tucmd_mlhl = 0;
   3626  1.28   msaitoh 	int	ehdrlen, ip_hlen = 0;
   3627   1.1    dyoung 	u16	etype;
   3628  1.12   hannken 	u8	ipproto __diagused = 0;
   3629  1.28   msaitoh 	int	offload = TRUE;
   3630  1.28   msaitoh 	int	ctxd = txr->next_avail_desc;
   3631  1.28   msaitoh 	u16	vtag = 0;
   3632  1.28   msaitoh 
   3633  1.28   msaitoh 	/* First check if TSO is to be used */
   3634  1.28   msaitoh 	if (mp->m_pkthdr.csum_flags & (M_CSUM_TSOv4|M_CSUM_TSOv6))
   3635  1.28   msaitoh 		return (ixgbe_tso_setup(txr, mp, cmd_type_len, olinfo_status));
   3636  1.28   msaitoh 
   3637  1.28   msaitoh 	if ((mp->m_pkthdr.csum_flags & M_CSUM_OFFLOAD) == 0)
   3638  1.28   msaitoh 		offload = FALSE;
   3639   1.1    dyoung 
   3640  1.28   msaitoh 	/* Indicate the whole packet as payload when not doing TSO */
   3641  1.28   msaitoh        	*olinfo_status |= mp->m_pkthdr.len << IXGBE_ADVTXD_PAYLEN_SHIFT;
   3642   1.1    dyoung 
   3643  1.28   msaitoh 	/* Now ready a context descriptor */
   3644   1.1    dyoung 	TXD = (struct ixgbe_adv_tx_context_desc *) &txr->tx_base[ctxd];
   3645   1.1    dyoung 
   3646   1.1    dyoung 	/*
   3647   1.1    dyoung 	** In advanced descriptors the vlan tag must
   3648  1.28   msaitoh 	** be placed into the context descriptor. Hence
   3649  1.28   msaitoh 	** we need to make one even if not doing offloads.
   3650   1.1    dyoung 	*/
   3651   1.1    dyoung 	if ((mtag = VLAN_OUTPUT_TAG(ec, mp)) != NULL) {
   3652   1.1    dyoung 		vtag = htole16(VLAN_TAG_VALUE(mtag) & 0xffff);
   3653   1.1    dyoung 		vlan_macip_lens |= (vtag << IXGBE_ADVTXD_VLAN_SHIFT);
   3654  1.28   msaitoh 	} else if (offload == FALSE) /* ... no offload to do */
   3655   1.1    dyoung 		return 0;
   3656   1.1    dyoung 
   3657   1.1    dyoung 	/*
   3658   1.1    dyoung 	 * Determine where frame payload starts.
   3659   1.1    dyoung 	 * Jump over vlan headers if already present,
   3660   1.1    dyoung 	 * helpful for QinQ too.
   3661   1.1    dyoung 	 */
   3662   1.1    dyoung 	KASSERT(mp->m_len >= offsetof(struct ether_vlan_header, evl_tag));
   3663   1.1    dyoung 	eh = mtod(mp, struct ether_vlan_header *);
   3664   1.1    dyoung 	if (eh->evl_encap_proto == htons(ETHERTYPE_VLAN)) {
   3665   1.1    dyoung 		KASSERT(mp->m_len >= sizeof(struct ether_vlan_header));
   3666   1.1    dyoung 		etype = ntohs(eh->evl_proto);
   3667   1.1    dyoung 		ehdrlen = ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN;
   3668   1.1    dyoung 	} else {
   3669   1.1    dyoung 		etype = ntohs(eh->evl_encap_proto);
   3670   1.1    dyoung 		ehdrlen = ETHER_HDR_LEN;
   3671   1.1    dyoung 	}
   3672   1.1    dyoung 
   3673   1.1    dyoung 	/* Set the ether header length */
   3674   1.1    dyoung 	vlan_macip_lens |= ehdrlen << IXGBE_ADVTXD_MACLEN_SHIFT;
   3675   1.1    dyoung 
   3676   1.1    dyoung 	switch (etype) {
   3677   1.1    dyoung 	case ETHERTYPE_IP:
   3678   1.1    dyoung 		m_copydata(mp, ehdrlen, sizeof(ip), &ip);
   3679   1.1    dyoung 		ip_hlen = ip.ip_hl << 2;
   3680   1.1    dyoung 		ipproto = ip.ip_p;
   3681   1.1    dyoung #if 0
   3682   1.1    dyoung 		ip.ip_sum = 0;
   3683   1.1    dyoung 		m_copyback(mp, ehdrlen, sizeof(ip), &ip);
   3684   1.1    dyoung #else
   3685   1.1    dyoung 		KASSERT((mp->m_pkthdr.csum_flags & M_CSUM_IPv4) == 0 ||
   3686   1.1    dyoung 		    ip.ip_sum == 0);
   3687   1.1    dyoung #endif
   3688   1.1    dyoung 		type_tucmd_mlhl |= IXGBE_ADVTXD_TUCMD_IPV4;
   3689   1.1    dyoung 		break;
   3690   1.1    dyoung 	case ETHERTYPE_IPV6:
   3691   1.1    dyoung 		m_copydata(mp, ehdrlen, sizeof(ip6), &ip6);
   3692   1.1    dyoung 		ip_hlen = sizeof(ip6);
   3693  1.25   msaitoh 		/* XXX-BZ this will go badly in case of ext hdrs. */
   3694   1.1    dyoung 		ipproto = ip6.ip6_nxt;
   3695   1.1    dyoung 		type_tucmd_mlhl |= IXGBE_ADVTXD_TUCMD_IPV6;
   3696   1.1    dyoung 		break;
   3697   1.1    dyoung 	default:
   3698   1.1    dyoung 		break;
   3699   1.1    dyoung 	}
   3700   1.1    dyoung 
   3701   1.1    dyoung 	if ((mp->m_pkthdr.csum_flags & M_CSUM_IPv4) != 0)
   3702  1.28   msaitoh 		*olinfo_status |= IXGBE_TXD_POPTS_IXSM << 8;
   3703   1.1    dyoung 
   3704   1.1    dyoung 	vlan_macip_lens |= ip_hlen;
   3705   1.1    dyoung 	type_tucmd_mlhl |= IXGBE_ADVTXD_DCMD_DEXT | IXGBE_ADVTXD_DTYP_CTXT;
   3706   1.1    dyoung 
   3707   1.1    dyoung 	if (mp->m_pkthdr.csum_flags & (M_CSUM_TCPv4|M_CSUM_TCPv6)) {
   3708   1.1    dyoung 		type_tucmd_mlhl |= IXGBE_ADVTXD_TUCMD_L4T_TCP;
   3709  1.28   msaitoh 		*olinfo_status |= IXGBE_TXD_POPTS_TXSM << 8;
   3710   1.1    dyoung 		KASSERT(ipproto == IPPROTO_TCP);
   3711   1.1    dyoung 	} else if (mp->m_pkthdr.csum_flags & (M_CSUM_UDPv4|M_CSUM_UDPv6)) {
   3712   1.1    dyoung 		type_tucmd_mlhl |= IXGBE_ADVTXD_TUCMD_L4T_UDP;
   3713  1.28   msaitoh 		*olinfo_status |= IXGBE_TXD_POPTS_TXSM << 8;
   3714   1.1    dyoung 		KASSERT(ipproto == IPPROTO_UDP);
   3715   1.1    dyoung 	}
   3716   1.1    dyoung 
   3717   1.1    dyoung 	/* Now copy bits into descriptor */
   3718  1.28   msaitoh 	TXD->vlan_macip_lens = htole32(vlan_macip_lens);
   3719  1.28   msaitoh 	TXD->type_tucmd_mlhl = htole32(type_tucmd_mlhl);
   3720   1.1    dyoung 	TXD->seqnum_seed = htole32(0);
   3721   1.1    dyoung 	TXD->mss_l4len_idx = htole32(0);
   3722   1.1    dyoung 
   3723   1.1    dyoung 	/* We've consumed the first desc, adjust counters */
   3724  1.28   msaitoh 	if (++ctxd == txr->num_desc)
   3725   1.1    dyoung 		ctxd = 0;
   3726   1.1    dyoung 	txr->next_avail_desc = ctxd;
   3727   1.1    dyoung 	--txr->tx_avail;
   3728   1.1    dyoung 
   3729  1.28   msaitoh         return 0;
   3730   1.1    dyoung }
   3731   1.1    dyoung 
   3732   1.1    dyoung /**********************************************************************
   3733   1.1    dyoung  *
   3734   1.1    dyoung  *  Setup work for hardware segmentation offload (TSO) on
   3735   1.1    dyoung  *  adapters using advanced tx descriptors
   3736   1.1    dyoung  *
   3737   1.1    dyoung  **********************************************************************/
   3738  1.28   msaitoh static int
   3739  1.28   msaitoh ixgbe_tso_setup(struct tx_ring *txr, struct mbuf *mp,
   3740  1.28   msaitoh     u32 *cmd_type_len, u32 *olinfo_status)
   3741   1.1    dyoung {
   3742   1.1    dyoung 	struct m_tag *mtag;
   3743   1.1    dyoung 	struct adapter *adapter = txr->adapter;
   3744   1.1    dyoung 	struct ethercom *ec = &adapter->osdep.ec;
   3745   1.1    dyoung 	struct ixgbe_adv_tx_context_desc *TXD;
   3746   1.1    dyoung 	u32 vlan_macip_lens = 0, type_tucmd_mlhl = 0;
   3747  1.28   msaitoh 	u32 mss_l4len_idx = 0, paylen;
   3748  1.25   msaitoh 	u16 vtag = 0, eh_type;
   3749  1.25   msaitoh 	int ctxd, ehdrlen, ip_hlen, tcp_hlen;
   3750   1.1    dyoung 	struct ether_vlan_header *eh;
   3751  1.25   msaitoh #ifdef INET6
   3752  1.25   msaitoh 	struct ip6_hdr *ip6;
   3753  1.25   msaitoh #endif
   3754  1.25   msaitoh #ifdef INET
   3755   1.1    dyoung 	struct ip *ip;
   3756  1.25   msaitoh #endif
   3757   1.1    dyoung 	struct tcphdr *th;
   3758   1.1    dyoung 
   3759   1.1    dyoung 
   3760   1.1    dyoung 	/*
   3761   1.1    dyoung 	 * Determine where frame payload starts.
   3762   1.1    dyoung 	 * Jump over vlan headers if already present
   3763   1.1    dyoung 	 */
   3764   1.1    dyoung 	eh = mtod(mp, struct ether_vlan_header *);
   3765  1.25   msaitoh 	if (eh->evl_encap_proto == htons(ETHERTYPE_VLAN)) {
   3766   1.1    dyoung 		ehdrlen = ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN;
   3767  1.25   msaitoh 		eh_type = eh->evl_proto;
   3768  1.25   msaitoh 	} else {
   3769   1.1    dyoung 		ehdrlen = ETHER_HDR_LEN;
   3770  1.25   msaitoh 		eh_type = eh->evl_encap_proto;
   3771  1.25   msaitoh 	}
   3772   1.1    dyoung 
   3773  1.25   msaitoh 	switch (ntohs(eh_type)) {
   3774  1.25   msaitoh #ifdef INET6
   3775  1.25   msaitoh 	case ETHERTYPE_IPV6:
   3776  1.25   msaitoh 		ip6 = (struct ip6_hdr *)(mp->m_data + ehdrlen);
   3777  1.25   msaitoh 		/* XXX-BZ For now we do not pretend to support ext. hdrs. */
   3778  1.25   msaitoh 		if (ip6->ip6_nxt != IPPROTO_TCP)
   3779  1.28   msaitoh 			return (ENXIO);
   3780  1.25   msaitoh 		ip_hlen = sizeof(struct ip6_hdr);
   3781  1.28   msaitoh 		ip6 = (struct ip6_hdr *)(mp->m_data + ehdrlen);
   3782  1.25   msaitoh 		th = (struct tcphdr *)((char *)ip6 + ip_hlen);
   3783  1.25   msaitoh 		th->th_sum = in6_cksum_phdr(&ip6->ip6_src,
   3784  1.25   msaitoh 		    &ip6->ip6_dst, 0, htonl(IPPROTO_TCP));
   3785  1.25   msaitoh 		type_tucmd_mlhl |= IXGBE_ADVTXD_TUCMD_IPV6;
   3786  1.25   msaitoh 		break;
   3787  1.25   msaitoh #endif
   3788  1.25   msaitoh #ifdef INET
   3789  1.25   msaitoh 	case ETHERTYPE_IP:
   3790  1.25   msaitoh 		ip = (struct ip *)(mp->m_data + ehdrlen);
   3791  1.25   msaitoh 		if (ip->ip_p != IPPROTO_TCP)
   3792  1.28   msaitoh 			return (ENXIO);
   3793  1.25   msaitoh 		ip->ip_sum = 0;
   3794  1.25   msaitoh 		ip_hlen = ip->ip_hl << 2;
   3795  1.25   msaitoh 		th = (struct tcphdr *)((char *)ip + ip_hlen);
   3796  1.25   msaitoh 		th->th_sum = in_cksum_phdr(ip->ip_src.s_addr,
   3797  1.25   msaitoh 		    ip->ip_dst.s_addr, htons(IPPROTO_TCP));
   3798  1.25   msaitoh 		type_tucmd_mlhl |= IXGBE_ADVTXD_TUCMD_IPV4;
   3799  1.25   msaitoh 		/* Tell transmit desc to also do IPv4 checksum. */
   3800  1.25   msaitoh 		*olinfo_status |= IXGBE_TXD_POPTS_IXSM << 8;
   3801  1.25   msaitoh 		break;
   3802  1.25   msaitoh #endif
   3803  1.25   msaitoh 	default:
   3804  1.25   msaitoh 		panic("%s: CSUM_TSO but no supported IP version (0x%04x)",
   3805  1.25   msaitoh 		    __func__, ntohs(eh_type));
   3806  1.25   msaitoh 		break;
   3807  1.25   msaitoh 	}
   3808   1.1    dyoung 
   3809   1.1    dyoung 	ctxd = txr->next_avail_desc;
   3810   1.1    dyoung 	TXD = (struct ixgbe_adv_tx_context_desc *) &txr->tx_base[ctxd];
   3811   1.1    dyoung 
   3812   1.1    dyoung 	tcp_hlen = th->th_off << 2;
   3813   1.1    dyoung 
   3814   1.1    dyoung 	/* This is used in the transmit desc in encap */
   3815  1.28   msaitoh 	paylen = mp->m_pkthdr.len - ehdrlen - ip_hlen - tcp_hlen;
   3816   1.1    dyoung 
   3817   1.1    dyoung 	/* VLAN MACLEN IPLEN */
   3818   1.1    dyoung 	if ((mtag = VLAN_OUTPUT_TAG(ec, mp)) != NULL) {
   3819   1.1    dyoung 		vtag = htole16(VLAN_TAG_VALUE(mtag) & 0xffff);
   3820   1.1    dyoung                 vlan_macip_lens |= (vtag << IXGBE_ADVTXD_VLAN_SHIFT);
   3821   1.1    dyoung 	}
   3822   1.1    dyoung 
   3823   1.1    dyoung 	vlan_macip_lens |= ehdrlen << IXGBE_ADVTXD_MACLEN_SHIFT;
   3824   1.1    dyoung 	vlan_macip_lens |= ip_hlen;
   3825  1.28   msaitoh 	TXD->vlan_macip_lens = htole32(vlan_macip_lens);
   3826   1.1    dyoung 
   3827   1.1    dyoung 	/* ADV DTYPE TUCMD */
   3828   1.1    dyoung 	type_tucmd_mlhl |= IXGBE_ADVTXD_DCMD_DEXT | IXGBE_ADVTXD_DTYP_CTXT;
   3829   1.1    dyoung 	type_tucmd_mlhl |= IXGBE_ADVTXD_TUCMD_L4T_TCP;
   3830  1.28   msaitoh 	TXD->type_tucmd_mlhl = htole32(type_tucmd_mlhl);
   3831   1.1    dyoung 
   3832   1.1    dyoung 	/* MSS L4LEN IDX */
   3833   1.1    dyoung 	mss_l4len_idx |= (mp->m_pkthdr.segsz << IXGBE_ADVTXD_MSS_SHIFT);
   3834   1.1    dyoung 	mss_l4len_idx |= (tcp_hlen << IXGBE_ADVTXD_L4LEN_SHIFT);
   3835   1.1    dyoung 	TXD->mss_l4len_idx = htole32(mss_l4len_idx);
   3836   1.1    dyoung 
   3837   1.1    dyoung 	TXD->seqnum_seed = htole32(0);
   3838   1.1    dyoung 
   3839  1.28   msaitoh 	if (++ctxd == txr->num_desc)
   3840   1.1    dyoung 		ctxd = 0;
   3841   1.1    dyoung 
   3842   1.1    dyoung 	txr->tx_avail--;
   3843   1.1    dyoung 	txr->next_avail_desc = ctxd;
   3844  1.28   msaitoh 	*cmd_type_len |= IXGBE_ADVTXD_DCMD_TSE;
   3845  1.28   msaitoh 	*olinfo_status |= IXGBE_TXD_POPTS_TXSM << 8;
   3846  1.28   msaitoh 	*olinfo_status |= paylen << IXGBE_ADVTXD_PAYLEN_SHIFT;
   3847  1.28   msaitoh 	++txr->tso_tx.ev_count;
   3848  1.28   msaitoh 	return (0);
   3849   1.1    dyoung }
   3850   1.1    dyoung 
   3851   1.1    dyoung #ifdef IXGBE_FDIR
   3852   1.1    dyoung /*
   3853   1.1    dyoung ** This routine parses packet headers so that Flow
   3854   1.1    dyoung ** Director can make a hashed filter table entry
   3855   1.1    dyoung ** allowing traffic flows to be identified and kept
   3856   1.1    dyoung ** on the same cpu.  This would be a performance
   3857   1.1    dyoung ** hit, but we only do it at IXGBE_FDIR_RATE of
   3858   1.1    dyoung ** packets.
   3859   1.1    dyoung */
   3860   1.1    dyoung static void
   3861   1.1    dyoung ixgbe_atr(struct tx_ring *txr, struct mbuf *mp)
   3862   1.1    dyoung {
   3863   1.1    dyoung 	struct adapter			*adapter = txr->adapter;
   3864   1.1    dyoung 	struct ix_queue			*que;
   3865   1.1    dyoung 	struct ip			*ip;
   3866   1.1    dyoung 	struct tcphdr			*th;
   3867   1.1    dyoung 	struct udphdr			*uh;
   3868   1.1    dyoung 	struct ether_vlan_header	*eh;
   3869   1.1    dyoung 	union ixgbe_atr_hash_dword	input = {.dword = 0};
   3870   1.1    dyoung 	union ixgbe_atr_hash_dword	common = {.dword = 0};
   3871   1.1    dyoung 	int  				ehdrlen, ip_hlen;
   3872   1.1    dyoung 	u16				etype;
   3873   1.1    dyoung 
   3874   1.1    dyoung 	eh = mtod(mp, struct ether_vlan_header *);
   3875   1.1    dyoung 	if (eh->evl_encap_proto == htons(ETHERTYPE_VLAN)) {
   3876   1.1    dyoung 		ehdrlen = ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN;
   3877   1.1    dyoung 		etype = eh->evl_proto;
   3878   1.1    dyoung 	} else {
   3879   1.1    dyoung 		ehdrlen = ETHER_HDR_LEN;
   3880   1.1    dyoung 		etype = eh->evl_encap_proto;
   3881   1.1    dyoung 	}
   3882   1.1    dyoung 
   3883   1.1    dyoung 	/* Only handling IPv4 */
   3884   1.1    dyoung 	if (etype != htons(ETHERTYPE_IP))
   3885   1.1    dyoung 		return;
   3886   1.1    dyoung 
   3887   1.1    dyoung 	ip = (struct ip *)(mp->m_data + ehdrlen);
   3888   1.1    dyoung 	ip_hlen = ip->ip_hl << 2;
   3889   1.1    dyoung 
   3890   1.1    dyoung 	/* check if we're UDP or TCP */
   3891   1.1    dyoung 	switch (ip->ip_p) {
   3892   1.1    dyoung 	case IPPROTO_TCP:
   3893   1.1    dyoung 		th = (struct tcphdr *)((char *)ip + ip_hlen);
   3894   1.1    dyoung 		/* src and dst are inverted */
   3895   1.1    dyoung 		common.port.dst ^= th->th_sport;
   3896   1.1    dyoung 		common.port.src ^= th->th_dport;
   3897   1.1    dyoung 		input.formatted.flow_type ^= IXGBE_ATR_FLOW_TYPE_TCPV4;
   3898   1.1    dyoung 		break;
   3899   1.1    dyoung 	case IPPROTO_UDP:
   3900   1.1    dyoung 		uh = (struct udphdr *)((char *)ip + ip_hlen);
   3901   1.1    dyoung 		/* src and dst are inverted */
   3902   1.1    dyoung 		common.port.dst ^= uh->uh_sport;
   3903   1.1    dyoung 		common.port.src ^= uh->uh_dport;
   3904   1.1    dyoung 		input.formatted.flow_type ^= IXGBE_ATR_FLOW_TYPE_UDPV4;
   3905   1.1    dyoung 		break;
   3906   1.1    dyoung 	default:
   3907   1.1    dyoung 		return;
   3908   1.1    dyoung 	}
   3909   1.1    dyoung 
   3910   1.1    dyoung 	input.formatted.vlan_id = htobe16(mp->m_pkthdr.ether_vtag);
   3911   1.1    dyoung 	if (mp->m_pkthdr.ether_vtag)
   3912   1.1    dyoung 		common.flex_bytes ^= htons(ETHERTYPE_VLAN);
   3913   1.1    dyoung 	else
   3914   1.1    dyoung 		common.flex_bytes ^= etype;
   3915   1.1    dyoung 	common.ip ^= ip->ip_src.s_addr ^ ip->ip_dst.s_addr;
   3916   1.1    dyoung 
   3917   1.1    dyoung 	que = &adapter->queues[txr->me];
   3918   1.1    dyoung 	/*
   3919   1.1    dyoung 	** This assumes the Rx queue and Tx
   3920   1.1    dyoung 	** queue are bound to the same CPU
   3921   1.1    dyoung 	*/
   3922   1.1    dyoung 	ixgbe_fdir_add_signature_filter_82599(&adapter->hw,
   3923   1.1    dyoung 	    input, common, que->msix);
   3924   1.1    dyoung }
   3925   1.1    dyoung #endif /* IXGBE_FDIR */
   3926   1.1    dyoung 
   3927   1.1    dyoung /**********************************************************************
   3928   1.1    dyoung  *
   3929   1.1    dyoung  *  Examine each tx_buffer in the used queue. If the hardware is done
   3930   1.1    dyoung  *  processing the packet then free associated resources. The
   3931   1.1    dyoung  *  tx_buffer is put back on the free queue.
   3932   1.1    dyoung  *
   3933   1.1    dyoung  **********************************************************************/
   3934  1.33   msaitoh static void
   3935   1.1    dyoung ixgbe_txeof(struct tx_ring *txr)
   3936   1.1    dyoung {
   3937  1.28   msaitoh 	struct adapter		*adapter = txr->adapter;
   3938  1.28   msaitoh 	struct ifnet		*ifp = adapter->ifp;
   3939  1.28   msaitoh 	u32			work, processed = 0;
   3940  1.28   msaitoh 	u16			limit = txr->process_limit;
   3941  1.28   msaitoh 	struct ixgbe_tx_buf	*buf;
   3942  1.28   msaitoh 	union ixgbe_adv_tx_desc *txd;
   3943   1.1    dyoung 	struct timeval now, elapsed;
   3944   1.1    dyoung 
   3945   1.1    dyoung 	KASSERT(mutex_owned(&txr->tx_mtx));
   3946   1.1    dyoung 
   3947  1.22   msaitoh #ifdef DEV_NETMAP
   3948  1.22   msaitoh 	if (ifp->if_capenable & IFCAP_NETMAP) {
   3949  1.22   msaitoh 		struct netmap_adapter *na = NA(ifp);
   3950  1.22   msaitoh 		struct netmap_kring *kring = &na->tx_rings[txr->me];
   3951  1.28   msaitoh 		txd = txr->tx_base;
   3952  1.22   msaitoh 		bus_dmamap_sync(txr->txdma.dma_tag, txr->txdma.dma_map,
   3953  1.22   msaitoh 		    BUS_DMASYNC_POSTREAD);
   3954  1.22   msaitoh 		/*
   3955  1.22   msaitoh 		 * In netmap mode, all the work is done in the context
   3956  1.22   msaitoh 		 * of the client thread. Interrupt handlers only wake up
   3957  1.22   msaitoh 		 * clients, which may be sleeping on individual rings
   3958  1.22   msaitoh 		 * or on a global resource for all rings.
   3959  1.22   msaitoh 		 * To implement tx interrupt mitigation, we wake up the client
   3960  1.22   msaitoh 		 * thread roughly every half ring, even if the NIC interrupts
   3961  1.22   msaitoh 		 * more frequently. This is implemented as follows:
   3962  1.22   msaitoh 		 * - ixgbe_txsync() sets kring->nr_kflags with the index of
   3963  1.22   msaitoh 		 *   the slot that should wake up the thread (nkr_num_slots
   3964  1.22   msaitoh 		 *   means the user thread should not be woken up);
   3965  1.22   msaitoh 		 * - the driver ignores tx interrupts unless netmap_mitigate=0
   3966  1.22   msaitoh 		 *   or the slot has the DD bit set.
   3967  1.22   msaitoh 		 */
   3968  1.22   msaitoh 		if (!netmap_mitigate ||
   3969  1.22   msaitoh 		    (kring->nr_kflags < kring->nkr_num_slots &&
   3970  1.28   msaitoh 		    txd[kring->nr_kflags].wb.status & IXGBE_TXD_STAT_DD)) {
   3971  1.33   msaitoh 			netmap_tx_irq(ifp, txr->me);
   3972  1.22   msaitoh 		}
   3973  1.33   msaitoh 		return;
   3974  1.22   msaitoh 	}
   3975  1.22   msaitoh #endif /* DEV_NETMAP */
   3976  1.22   msaitoh 
   3977  1.28   msaitoh 	if (txr->tx_avail == txr->num_desc) {
   3978   1.1    dyoung 		txr->queue_status = IXGBE_QUEUE_IDLE;
   3979  1.33   msaitoh 		return;
   3980   1.1    dyoung 	}
   3981   1.1    dyoung 
   3982  1.28   msaitoh 	/* Get work starting point */
   3983  1.28   msaitoh 	work = txr->next_to_clean;
   3984  1.28   msaitoh 	buf = &txr->tx_buffers[work];
   3985  1.28   msaitoh 	txd = &txr->tx_base[work];
   3986  1.28   msaitoh 	work -= txr->num_desc; /* The distance to ring end */
   3987  1.28   msaitoh         ixgbe_dmamap_sync(txr->txdma.dma_tag, txr->txdma.dma_map,
   3988  1.28   msaitoh 	    BUS_DMASYNC_POSTREAD);
   3989  1.28   msaitoh 	do {
   3990  1.28   msaitoh 		union ixgbe_adv_tx_desc *eop= buf->eop;
   3991  1.28   msaitoh 		if (eop == NULL) /* No work */
   3992  1.28   msaitoh 			break;
   3993   1.1    dyoung 
   3994  1.28   msaitoh 		if ((eop->wb.status & IXGBE_TXD_STAT_DD) == 0)
   3995  1.28   msaitoh 			break;	/* I/O not complete */
   3996   1.1    dyoung 
   3997  1.28   msaitoh 		if (buf->m_head) {
   3998  1.28   msaitoh 			txr->bytes +=
   3999  1.28   msaitoh 			    buf->m_head->m_pkthdr.len;
   4000  1.28   msaitoh 			bus_dmamap_sync(txr->txtag->dt_dmat,
   4001  1.28   msaitoh 			    buf->map,
   4002  1.28   msaitoh 			    0, buf->m_head->m_pkthdr.len,
   4003  1.28   msaitoh 			    BUS_DMASYNC_POSTWRITE);
   4004  1.28   msaitoh 			ixgbe_dmamap_unload(txr->txtag,
   4005  1.28   msaitoh 			    buf->map);
   4006  1.28   msaitoh 			m_freem(buf->m_head);
   4007  1.28   msaitoh 			buf->m_head = NULL;
   4008  1.28   msaitoh 			/*
   4009  1.28   msaitoh 			 * NetBSD: Don't override buf->map with NULL here.
   4010  1.28   msaitoh 			 * It'll panic when a ring runs one lap around.
   4011  1.28   msaitoh 			 */
   4012  1.28   msaitoh 		}
   4013  1.28   msaitoh 		buf->eop = NULL;
   4014  1.28   msaitoh 		++txr->tx_avail;
   4015   1.1    dyoung 
   4016  1.28   msaitoh 		/* We clean the range if multi segment */
   4017  1.28   msaitoh 		while (txd != eop) {
   4018  1.28   msaitoh 			++txd;
   4019  1.28   msaitoh 			++buf;
   4020  1.28   msaitoh 			++work;
   4021  1.28   msaitoh 			/* wrap the ring? */
   4022  1.28   msaitoh 			if (__predict_false(!work)) {
   4023  1.28   msaitoh 				work -= txr->num_desc;
   4024  1.28   msaitoh 				buf = txr->tx_buffers;
   4025  1.28   msaitoh 				txd = txr->tx_base;
   4026  1.28   msaitoh 			}
   4027  1.28   msaitoh 			if (buf->m_head) {
   4028   1.1    dyoung 				txr->bytes +=
   4029  1.28   msaitoh 				    buf->m_head->m_pkthdr.len;
   4030   1.1    dyoung 				bus_dmamap_sync(txr->txtag->dt_dmat,
   4031  1.28   msaitoh 				    buf->map,
   4032  1.28   msaitoh 				    0, buf->m_head->m_pkthdr.len,
   4033   1.1    dyoung 				    BUS_DMASYNC_POSTWRITE);
   4034  1.28   msaitoh 				ixgbe_dmamap_unload(txr->txtag,
   4035  1.28   msaitoh 				    buf->map);
   4036  1.28   msaitoh 				m_freem(buf->m_head);
   4037  1.28   msaitoh 				buf->m_head = NULL;
   4038  1.28   msaitoh 				/*
   4039  1.28   msaitoh 				 * NetBSD: Don't override buf->map with NULL
   4040  1.28   msaitoh 				 * here. It'll panic when a ring runs one lap
   4041  1.28   msaitoh 				 * around.
   4042  1.28   msaitoh 				 */
   4043   1.1    dyoung 			}
   4044  1.28   msaitoh 			++txr->tx_avail;
   4045  1.28   msaitoh 			buf->eop = NULL;
   4046   1.1    dyoung 
   4047   1.1    dyoung 		}
   4048   1.1    dyoung 		++txr->packets;
   4049  1.28   msaitoh 		++processed;
   4050   1.1    dyoung 		++ifp->if_opackets;
   4051  1.28   msaitoh 		getmicrotime(&txr->watchdog_time);
   4052  1.28   msaitoh 
   4053  1.28   msaitoh 		/* Try the next packet */
   4054  1.28   msaitoh 		++txd;
   4055  1.28   msaitoh 		++buf;
   4056  1.28   msaitoh 		++work;
   4057  1.28   msaitoh 		/* reset with a wrap */
   4058  1.28   msaitoh 		if (__predict_false(!work)) {
   4059  1.28   msaitoh 			work -= txr->num_desc;
   4060  1.28   msaitoh 			buf = txr->tx_buffers;
   4061  1.28   msaitoh 			txd = txr->tx_base;
   4062  1.28   msaitoh 		}
   4063  1.28   msaitoh 		prefetch(txd);
   4064  1.28   msaitoh 	} while (__predict_true(--limit));
   4065  1.28   msaitoh 
   4066   1.1    dyoung 	ixgbe_dmamap_sync(txr->txdma.dma_tag, txr->txdma.dma_map,
   4067   1.1    dyoung 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
   4068   1.1    dyoung 
   4069  1.28   msaitoh 	work += txr->num_desc;
   4070  1.28   msaitoh 	txr->next_to_clean = work;
   4071   1.1    dyoung 
   4072   1.1    dyoung 	/*
   4073   1.1    dyoung 	** Watchdog calculation, we know there's
   4074   1.1    dyoung 	** work outstanding or the first return
   4075   1.1    dyoung 	** would have been taken, so none processed
   4076   1.1    dyoung 	** for too long indicates a hang.
   4077   1.1    dyoung 	*/
   4078   1.1    dyoung 	getmicrotime(&now);
   4079   1.1    dyoung 	timersub(&now, &txr->watchdog_time, &elapsed);
   4080   1.1    dyoung 	if (!processed && tvtohz(&elapsed) > IXGBE_WATCHDOG)
   4081   1.1    dyoung 		txr->queue_status = IXGBE_QUEUE_HUNG;
   4082   1.1    dyoung 
   4083  1.33   msaitoh 	if (txr->tx_avail == txr->num_desc)
   4084  1.24   msaitoh 		txr->queue_status = IXGBE_QUEUE_IDLE;
   4085   1.1    dyoung 
   4086  1.33   msaitoh 	return;
   4087   1.1    dyoung }
   4088   1.1    dyoung 
   4089   1.1    dyoung /*********************************************************************
   4090   1.1    dyoung  *
   4091   1.1    dyoung  *  Refresh mbuf buffers for RX descriptor rings
   4092   1.1    dyoung  *   - now keeps its own state so discards due to resource
   4093   1.1    dyoung  *     exhaustion are unnecessary, if an mbuf cannot be obtained
   4094   1.1    dyoung  *     it just returns, keeping its placeholder, thus it can simply
   4095   1.1    dyoung  *     be recalled to try again.
   4096   1.1    dyoung  *
   4097   1.1    dyoung  **********************************************************************/
   4098   1.1    dyoung static void
   4099   1.1    dyoung ixgbe_refresh_mbufs(struct rx_ring *rxr, int limit)
   4100   1.1    dyoung {
   4101   1.1    dyoung 	struct adapter		*adapter = rxr->adapter;
   4102   1.1    dyoung 	struct ixgbe_rx_buf	*rxbuf;
   4103  1.26   msaitoh 	struct mbuf		*mp;
   4104   1.1    dyoung 	int			i, j, error;
   4105   1.1    dyoung 	bool			refreshed = false;
   4106   1.1    dyoung 
   4107   1.1    dyoung 	i = j = rxr->next_to_refresh;
   4108   1.1    dyoung 	/* Control the loop with one beyond */
   4109  1.28   msaitoh 	if (++j == rxr->num_desc)
   4110   1.1    dyoung 		j = 0;
   4111   1.1    dyoung 
   4112   1.1    dyoung 	while (j != limit) {
   4113   1.1    dyoung 		rxbuf = &rxr->rx_buffers[i];
   4114  1.26   msaitoh 		if (rxbuf->buf == NULL) {
   4115  1.28   msaitoh 			mp = ixgbe_getjcl(&adapter->jcl_head, M_NOWAIT,
   4116  1.28   msaitoh 			    MT_DATA, M_PKTHDR, rxr->mbuf_sz);
   4117   1.1    dyoung 			if (mp == NULL) {
   4118   1.1    dyoung 				rxr->no_jmbuf.ev_count++;
   4119   1.1    dyoung 				goto update;
   4120   1.1    dyoung 			}
   4121  1.28   msaitoh 			if (adapter->max_frame_size <= (MCLBYTES - ETHER_ALIGN))
   4122  1.28   msaitoh 				m_adj(mp, ETHER_ALIGN);
   4123   1.1    dyoung 		} else
   4124  1.26   msaitoh 			mp = rxbuf->buf;
   4125   1.1    dyoung 
   4126  1.28   msaitoh 		mp->m_pkthdr.len = mp->m_len = rxr->mbuf_sz;
   4127  1.35   msaitoh 
   4128  1.26   msaitoh 		/* If we're dealing with an mbuf that was copied rather
   4129  1.26   msaitoh 		 * than replaced, there's no need to go through busdma.
   4130  1.26   msaitoh 		 */
   4131  1.26   msaitoh 		if ((rxbuf->flags & IXGBE_RX_COPY) == 0) {
   4132  1.26   msaitoh 			/* Get the memory mapping */
   4133  1.28   msaitoh 			error = bus_dmamap_load_mbuf(rxr->ptag->dt_dmat,
   4134  1.28   msaitoh 			    rxbuf->pmap, mp, BUS_DMA_NOWAIT);
   4135  1.26   msaitoh 			if (error != 0) {
   4136  1.26   msaitoh 				printf("Refresh mbufs: payload dmamap load"
   4137  1.26   msaitoh 				    " failure - %d\n", error);
   4138  1.26   msaitoh 				m_free(mp);
   4139  1.26   msaitoh 				rxbuf->buf = NULL;
   4140  1.26   msaitoh 				goto update;
   4141  1.26   msaitoh 			}
   4142  1.26   msaitoh 			rxbuf->buf = mp;
   4143  1.28   msaitoh 			bus_dmamap_sync(rxr->ptag->dt_dmat, rxbuf->pmap,
   4144  1.26   msaitoh 			    0, mp->m_pkthdr.len, BUS_DMASYNC_PREREAD);
   4145  1.26   msaitoh 			rxbuf->addr = rxr->rx_base[i].read.pkt_addr =
   4146  1.28   msaitoh 			    htole64(rxbuf->pmap->dm_segs[0].ds_addr);
   4147  1.26   msaitoh 		} else {
   4148  1.26   msaitoh 			rxr->rx_base[i].read.pkt_addr = rxbuf->addr;
   4149  1.26   msaitoh 			rxbuf->flags &= ~IXGBE_RX_COPY;
   4150  1.26   msaitoh 		}
   4151   1.1    dyoung 
   4152   1.1    dyoung 		refreshed = true;
   4153   1.1    dyoung 		/* Next is precalculated */
   4154   1.1    dyoung 		i = j;
   4155   1.1    dyoung 		rxr->next_to_refresh = i;
   4156  1.28   msaitoh 		if (++j == rxr->num_desc)
   4157   1.1    dyoung 			j = 0;
   4158   1.1    dyoung 	}
   4159   1.1    dyoung update:
   4160   1.1    dyoung 	if (refreshed) /* Update hardware tail index */
   4161   1.1    dyoung 		IXGBE_WRITE_REG(&adapter->hw,
   4162   1.1    dyoung 		    IXGBE_RDT(rxr->me), rxr->next_to_refresh);
   4163   1.1    dyoung 	return;
   4164   1.1    dyoung }
   4165   1.1    dyoung 
   4166   1.1    dyoung /*********************************************************************
   4167   1.1    dyoung  *
   4168   1.1    dyoung  *  Allocate memory for rx_buffer structures. Since we use one
   4169   1.1    dyoung  *  rx_buffer per received packet, the maximum number of rx_buffer's
   4170   1.1    dyoung  *  that we'll need is equal to the number of receive descriptors
   4171   1.1    dyoung  *  that we've allocated.
   4172   1.1    dyoung  *
   4173   1.1    dyoung  **********************************************************************/
   4174   1.1    dyoung static int
   4175   1.1    dyoung ixgbe_allocate_receive_buffers(struct rx_ring *rxr)
   4176   1.1    dyoung {
   4177   1.1    dyoung 	struct	adapter 	*adapter = rxr->adapter;
   4178   1.1    dyoung 	device_t 		dev = adapter->dev;
   4179   1.1    dyoung 	struct ixgbe_rx_buf 	*rxbuf;
   4180   1.1    dyoung 	int             	i, bsize, error;
   4181   1.1    dyoung 
   4182  1.28   msaitoh 	bsize = sizeof(struct ixgbe_rx_buf) * rxr->num_desc;
   4183   1.1    dyoung 	if (!(rxr->rx_buffers =
   4184   1.1    dyoung 	    (struct ixgbe_rx_buf *) malloc(bsize,
   4185   1.1    dyoung 	    M_DEVBUF, M_NOWAIT | M_ZERO))) {
   4186   1.1    dyoung 		aprint_error_dev(dev, "Unable to allocate rx_buffer memory\n");
   4187   1.1    dyoung 		error = ENOMEM;
   4188   1.1    dyoung 		goto fail;
   4189   1.1    dyoung 	}
   4190   1.1    dyoung 
   4191   1.1    dyoung 	if ((error = ixgbe_dma_tag_create(adapter->osdep.dmat,	/* parent */
   4192   1.1    dyoung 				   1, 0,	/* alignment, bounds */
   4193   1.1    dyoung 				   MJUM16BYTES,		/* maxsize */
   4194   1.1    dyoung 				   1,			/* nsegments */
   4195   1.1    dyoung 				   MJUM16BYTES,		/* maxsegsize */
   4196   1.1    dyoung 				   0,			/* flags */
   4197  1.28   msaitoh 				   &rxr->ptag))) {
   4198   1.1    dyoung 		aprint_error_dev(dev, "Unable to create RX DMA tag\n");
   4199   1.1    dyoung 		goto fail;
   4200   1.1    dyoung 	}
   4201   1.1    dyoung 
   4202  1.28   msaitoh 	for (i = 0; i < rxr->num_desc; i++, rxbuf++) {
   4203   1.1    dyoung 		rxbuf = &rxr->rx_buffers[i];
   4204  1.28   msaitoh 		error = ixgbe_dmamap_create(rxr->ptag,
   4205  1.28   msaitoh 		    BUS_DMA_NOWAIT, &rxbuf->pmap);
   4206   1.1    dyoung 		if (error) {
   4207  1.26   msaitoh 			aprint_error_dev(dev, "Unable to create RX dma map\n");
   4208   1.1    dyoung 			goto fail;
   4209   1.1    dyoung 		}
   4210   1.1    dyoung 	}
   4211   1.1    dyoung 
   4212   1.1    dyoung 	return (0);
   4213   1.1    dyoung 
   4214   1.1    dyoung fail:
   4215   1.1    dyoung 	/* Frees all, but can handle partial completion */
   4216   1.1    dyoung 	ixgbe_free_receive_structures(adapter);
   4217   1.1    dyoung 	return (error);
   4218   1.1    dyoung }
   4219   1.1    dyoung 
   4220   1.1    dyoung /*
   4221   1.1    dyoung ** Used to detect a descriptor that has
   4222   1.1    dyoung ** been merged by Hardware RSC.
   4223   1.1    dyoung */
   4224   1.1    dyoung static inline u32
   4225   1.1    dyoung ixgbe_rsc_count(union ixgbe_adv_rx_desc *rx)
   4226   1.1    dyoung {
   4227   1.1    dyoung 	return (le32toh(rx->wb.lower.lo_dword.data) &
   4228   1.1    dyoung 	    IXGBE_RXDADV_RSCCNT_MASK) >> IXGBE_RXDADV_RSCCNT_SHIFT;
   4229   1.1    dyoung }
   4230   1.1    dyoung 
   4231   1.1    dyoung /*********************************************************************
   4232   1.1    dyoung  *
   4233   1.1    dyoung  *  Initialize Hardware RSC (LRO) feature on 82599
   4234   1.1    dyoung  *  for an RX ring, this is toggled by the LRO capability
   4235   1.1    dyoung  *  even though it is transparent to the stack.
   4236   1.1    dyoung  *
   4237  1.26   msaitoh  *  NOTE: since this HW feature only works with IPV4 and
   4238  1.26   msaitoh  *        our testing has shown soft LRO to be as effective
   4239  1.26   msaitoh  *        I have decided to disable this by default.
   4240  1.26   msaitoh  *
   4241   1.1    dyoung  **********************************************************************/
   4242   1.1    dyoung static void
   4243   1.1    dyoung ixgbe_setup_hw_rsc(struct rx_ring *rxr)
   4244   1.1    dyoung {
   4245   1.1    dyoung 	struct	adapter 	*adapter = rxr->adapter;
   4246   1.1    dyoung 	struct	ixgbe_hw	*hw = &adapter->hw;
   4247   1.1    dyoung 	u32			rscctrl, rdrxctl;
   4248   1.1    dyoung 
   4249  1.26   msaitoh 	/* If turning LRO/RSC off we need to disable it */
   4250  1.26   msaitoh 	if ((adapter->ifp->if_capenable & IFCAP_LRO) == 0) {
   4251  1.26   msaitoh 		rscctrl = IXGBE_READ_REG(hw, IXGBE_RSCCTL(rxr->me));
   4252  1.26   msaitoh 		rscctrl &= ~IXGBE_RSCCTL_RSCEN;
   4253  1.26   msaitoh 		return;
   4254  1.26   msaitoh 	}
   4255  1.26   msaitoh 
   4256   1.1    dyoung 	rdrxctl = IXGBE_READ_REG(hw, IXGBE_RDRXCTL);
   4257   1.1    dyoung 	rdrxctl &= ~IXGBE_RDRXCTL_RSCFRSTSIZE;
   4258  1.25   msaitoh #ifdef DEV_NETMAP /* crcstrip is optional in netmap */
   4259  1.25   msaitoh 	if (adapter->ifp->if_capenable & IFCAP_NETMAP && !ix_crcstrip)
   4260  1.25   msaitoh #endif /* DEV_NETMAP */
   4261   1.1    dyoung 	rdrxctl |= IXGBE_RDRXCTL_CRCSTRIP;
   4262   1.1    dyoung 	rdrxctl |= IXGBE_RDRXCTL_RSCACKC;
   4263   1.1    dyoung 	IXGBE_WRITE_REG(hw, IXGBE_RDRXCTL, rdrxctl);
   4264   1.1    dyoung 
   4265   1.1    dyoung 	rscctrl = IXGBE_READ_REG(hw, IXGBE_RSCCTL(rxr->me));
   4266   1.1    dyoung 	rscctrl |= IXGBE_RSCCTL_RSCEN;
   4267   1.1    dyoung 	/*
   4268   1.1    dyoung 	** Limit the total number of descriptors that
   4269   1.1    dyoung 	** can be combined, so it does not exceed 64K
   4270   1.1    dyoung 	*/
   4271  1.28   msaitoh 	if (rxr->mbuf_sz == MCLBYTES)
   4272   1.1    dyoung 		rscctrl |= IXGBE_RSCCTL_MAXDESC_16;
   4273  1.28   msaitoh 	else if (rxr->mbuf_sz == MJUMPAGESIZE)
   4274   1.1    dyoung 		rscctrl |= IXGBE_RSCCTL_MAXDESC_8;
   4275  1.28   msaitoh 	else if (rxr->mbuf_sz == MJUM9BYTES)
   4276   1.1    dyoung 		rscctrl |= IXGBE_RSCCTL_MAXDESC_4;
   4277   1.1    dyoung 	else  /* Using 16K cluster */
   4278   1.1    dyoung 		rscctrl |= IXGBE_RSCCTL_MAXDESC_1;
   4279   1.1    dyoung 
   4280   1.1    dyoung 	IXGBE_WRITE_REG(hw, IXGBE_RSCCTL(rxr->me), rscctrl);
   4281   1.1    dyoung 
   4282   1.1    dyoung 	/* Enable TCP header recognition */
   4283   1.1    dyoung 	IXGBE_WRITE_REG(hw, IXGBE_PSRTYPE(0),
   4284   1.1    dyoung 	    (IXGBE_READ_REG(hw, IXGBE_PSRTYPE(0)) |
   4285   1.1    dyoung 	    IXGBE_PSRTYPE_TCPHDR));
   4286   1.1    dyoung 
   4287   1.1    dyoung 	/* Disable RSC for ACK packets */
   4288   1.1    dyoung 	IXGBE_WRITE_REG(hw, IXGBE_RSCDBU,
   4289   1.1    dyoung 	    (IXGBE_RSCDBU_RSCACKDIS | IXGBE_READ_REG(hw, IXGBE_RSCDBU)));
   4290   1.1    dyoung 
   4291   1.1    dyoung 	rxr->hw_rsc = TRUE;
   4292   1.1    dyoung }
   4293   1.1    dyoung 
   4294   1.1    dyoung 
   4295   1.1    dyoung static void
   4296   1.1    dyoung ixgbe_free_receive_ring(struct rx_ring *rxr)
   4297   1.1    dyoung {
   4298   1.1    dyoung 	struct ixgbe_rx_buf       *rxbuf;
   4299   1.1    dyoung 	int i;
   4300   1.1    dyoung 
   4301  1.28   msaitoh 	for (i = 0; i < rxr->num_desc; i++) {
   4302   1.1    dyoung 		rxbuf = &rxr->rx_buffers[i];
   4303  1.26   msaitoh 		if (rxbuf->buf != NULL) {
   4304  1.28   msaitoh 			bus_dmamap_sync(rxr->ptag->dt_dmat, rxbuf->pmap,
   4305  1.26   msaitoh 			    0, rxbuf->buf->m_pkthdr.len,
   4306   1.1    dyoung 			    BUS_DMASYNC_POSTREAD);
   4307  1.28   msaitoh 			ixgbe_dmamap_unload(rxr->ptag, rxbuf->pmap);
   4308  1.26   msaitoh 			rxbuf->buf->m_flags |= M_PKTHDR;
   4309  1.26   msaitoh 			m_freem(rxbuf->buf);
   4310  1.26   msaitoh 			rxbuf->buf = NULL;
   4311  1.33   msaitoh 			rxbuf->flags = 0;
   4312   1.1    dyoung 		}
   4313   1.1    dyoung 	}
   4314   1.1    dyoung }
   4315   1.1    dyoung 
   4316   1.1    dyoung 
   4317   1.1    dyoung /*********************************************************************
   4318   1.1    dyoung  *
   4319   1.1    dyoung  *  Initialize a receive ring and its buffers.
   4320   1.1    dyoung  *
   4321   1.1    dyoung  **********************************************************************/
   4322   1.1    dyoung static int
   4323   1.1    dyoung ixgbe_setup_receive_ring(struct rx_ring *rxr)
   4324   1.1    dyoung {
   4325   1.1    dyoung 	struct	adapter 	*adapter;
   4326   1.1    dyoung 	struct ixgbe_rx_buf	*rxbuf;
   4327   1.1    dyoung #ifdef LRO
   4328  1.26   msaitoh 	struct ifnet		*ifp;
   4329   1.1    dyoung 	struct lro_ctrl		*lro = &rxr->lro;
   4330   1.1    dyoung #endif /* LRO */
   4331   1.1    dyoung 	int			rsize, error = 0;
   4332  1.22   msaitoh #ifdef DEV_NETMAP
   4333  1.22   msaitoh 	struct netmap_adapter *na = NA(rxr->adapter->ifp);
   4334  1.22   msaitoh 	struct netmap_slot *slot;
   4335  1.22   msaitoh #endif /* DEV_NETMAP */
   4336   1.1    dyoung 
   4337   1.1    dyoung 	adapter = rxr->adapter;
   4338  1.26   msaitoh #ifdef LRO
   4339   1.1    dyoung 	ifp = adapter->ifp;
   4340  1.26   msaitoh #endif /* LRO */
   4341   1.1    dyoung 
   4342   1.1    dyoung 	/* Clear the ring contents */
   4343   1.1    dyoung 	IXGBE_RX_LOCK(rxr);
   4344  1.22   msaitoh #ifdef DEV_NETMAP
   4345  1.22   msaitoh 	/* same as in ixgbe_setup_transmit_ring() */
   4346  1.22   msaitoh 	slot = netmap_reset(na, NR_RX, rxr->me, 0);
   4347  1.22   msaitoh #endif /* DEV_NETMAP */
   4348   1.1    dyoung 	rsize = roundup2(adapter->num_rx_desc *
   4349   1.1    dyoung 	    sizeof(union ixgbe_adv_rx_desc), DBA_ALIGN);
   4350   1.1    dyoung 	bzero((void *)rxr->rx_base, rsize);
   4351  1.28   msaitoh 	/* Cache the size */
   4352  1.28   msaitoh 	rxr->mbuf_sz = adapter->rx_mbuf_sz;
   4353   1.1    dyoung 
   4354   1.1    dyoung 	/* Free current RX buffer structs and their mbufs */
   4355   1.1    dyoung 	ixgbe_free_receive_ring(rxr);
   4356   1.1    dyoung 
   4357  1.18   msaitoh 	IXGBE_RX_UNLOCK(rxr);
   4358  1.18   msaitoh 
   4359   1.1    dyoung 	/* Now reinitialize our supply of jumbo mbufs.  The number
   4360   1.1    dyoung 	 * or size of jumbo mbufs may have changed.
   4361   1.1    dyoung 	 */
   4362  1.28   msaitoh 	ixgbe_jcl_reinit(&adapter->jcl_head, rxr->ptag->dt_dmat,
   4363   1.1    dyoung 	    2 * adapter->num_rx_desc, adapter->rx_mbuf_sz);
   4364   1.1    dyoung 
   4365  1.18   msaitoh 	IXGBE_RX_LOCK(rxr);
   4366  1.18   msaitoh 
   4367   1.1    dyoung 	/* Now replenish the mbufs */
   4368  1.28   msaitoh 	for (int j = 0; j != rxr->num_desc; ++j) {
   4369  1.26   msaitoh 		struct mbuf	*mp;
   4370   1.1    dyoung 
   4371   1.1    dyoung 		rxbuf = &rxr->rx_buffers[j];
   4372  1.22   msaitoh #ifdef DEV_NETMAP
   4373  1.22   msaitoh 		/*
   4374  1.22   msaitoh 		 * In netmap mode, fill the map and set the buffer
   4375  1.22   msaitoh 		 * address in the NIC ring, considering the offset
   4376  1.22   msaitoh 		 * between the netmap and NIC rings (see comment in
   4377  1.22   msaitoh 		 * ixgbe_setup_transmit_ring() ). No need to allocate
   4378  1.22   msaitoh 		 * an mbuf, so end the block with a continue;
   4379  1.22   msaitoh 		 */
   4380  1.22   msaitoh 		if (slot) {
   4381  1.25   msaitoh 			int sj = netmap_idx_n2k(&na->rx_rings[rxr->me], j);
   4382  1.22   msaitoh 			uint64_t paddr;
   4383  1.22   msaitoh 			void *addr;
   4384  1.22   msaitoh 
   4385  1.33   msaitoh 			addr = PNMB(na, slot + sj, &paddr);
   4386  1.33   msaitoh 			netmap_load_map(na, rxr->ptag, rxbuf->pmap, addr);
   4387  1.33   msaitoh 			/* Update descriptor and the cached value */
   4388  1.22   msaitoh 			rxr->rx_base[j].read.pkt_addr = htole64(paddr);
   4389  1.33   msaitoh 			rxbuf->addr = htole64(paddr);
   4390  1.22   msaitoh 			continue;
   4391  1.22   msaitoh 		}
   4392  1.22   msaitoh #endif /* DEV_NETMAP */
   4393  1.33   msaitoh 		rxbuf->flags = 0;
   4394  1.28   msaitoh 		rxbuf->buf = ixgbe_getjcl(&adapter->jcl_head, M_NOWAIT,
   4395   1.1    dyoung 		    MT_DATA, M_PKTHDR, adapter->rx_mbuf_sz);
   4396  1.26   msaitoh 		if (rxbuf->buf == NULL) {
   4397   1.1    dyoung 			error = ENOBUFS;
   4398   1.1    dyoung                         goto fail;
   4399   1.1    dyoung 		}
   4400  1.26   msaitoh 		mp = rxbuf->buf;
   4401  1.28   msaitoh 		mp->m_pkthdr.len = mp->m_len = rxr->mbuf_sz;
   4402   1.1    dyoung 		/* Get the memory mapping */
   4403  1.28   msaitoh 		error = bus_dmamap_load_mbuf(rxr->ptag->dt_dmat,
   4404  1.28   msaitoh 		    rxbuf->pmap, mp, BUS_DMA_NOWAIT);
   4405   1.1    dyoung 		if (error != 0)
   4406   1.1    dyoung                         goto fail;
   4407  1.28   msaitoh 		bus_dmamap_sync(rxr->ptag->dt_dmat, rxbuf->pmap,
   4408   1.1    dyoung 		    0, adapter->rx_mbuf_sz, BUS_DMASYNC_PREREAD);
   4409  1.33   msaitoh 		/* Update the descriptor and the cached value */
   4410   1.1    dyoung 		rxr->rx_base[j].read.pkt_addr =
   4411  1.28   msaitoh 		    htole64(rxbuf->pmap->dm_segs[0].ds_addr);
   4412  1.33   msaitoh 		rxbuf->addr = htole64(rxbuf->pmap->dm_segs[0].ds_addr);
   4413   1.1    dyoung 	}
   4414   1.1    dyoung 
   4415   1.1    dyoung 
   4416   1.1    dyoung 	/* Setup our descriptor indices */
   4417   1.1    dyoung 	rxr->next_to_check = 0;
   4418   1.1    dyoung 	rxr->next_to_refresh = 0;
   4419   1.1    dyoung 	rxr->lro_enabled = FALSE;
   4420  1.26   msaitoh 	rxr->rx_copies.ev_count = 0;
   4421   1.1    dyoung 	rxr->rx_bytes.ev_count = 0;
   4422  1.24   msaitoh 	rxr->vtag_strip = FALSE;
   4423   1.1    dyoung 
   4424   1.1    dyoung 	ixgbe_dmamap_sync(rxr->rxdma.dma_tag, rxr->rxdma.dma_map,
   4425   1.1    dyoung 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
   4426   1.1    dyoung 
   4427   1.1    dyoung 	/*
   4428   1.1    dyoung 	** Now set up the LRO interface:
   4429   1.1    dyoung 	*/
   4430  1.26   msaitoh 	if (ixgbe_rsc_enable)
   4431   1.1    dyoung 		ixgbe_setup_hw_rsc(rxr);
   4432   1.1    dyoung #ifdef LRO
   4433   1.1    dyoung 	else if (ifp->if_capenable & IFCAP_LRO) {
   4434   1.9     skrll 		device_t dev = adapter->dev;
   4435   1.1    dyoung 		int err = tcp_lro_init(lro);
   4436   1.1    dyoung 		if (err) {
   4437   1.1    dyoung 			device_printf(dev, "LRO Initialization failed!\n");
   4438   1.1    dyoung 			goto fail;
   4439   1.1    dyoung 		}
   4440   1.1    dyoung 		INIT_DEBUGOUT("RX Soft LRO Initialized\n");
   4441   1.1    dyoung 		rxr->lro_enabled = TRUE;
   4442   1.1    dyoung 		lro->ifp = adapter->ifp;
   4443   1.1    dyoung 	}
   4444   1.1    dyoung #endif /* LRO */
   4445   1.1    dyoung 
   4446   1.1    dyoung 	IXGBE_RX_UNLOCK(rxr);
   4447   1.1    dyoung 	return (0);
   4448   1.1    dyoung 
   4449   1.1    dyoung fail:
   4450   1.1    dyoung 	ixgbe_free_receive_ring(rxr);
   4451   1.1    dyoung 	IXGBE_RX_UNLOCK(rxr);
   4452   1.1    dyoung 	return (error);
   4453   1.1    dyoung }
   4454   1.1    dyoung 
   4455   1.1    dyoung /*********************************************************************
   4456   1.1    dyoung  *
   4457   1.1    dyoung  *  Initialize all receive rings.
   4458   1.1    dyoung  *
   4459   1.1    dyoung  **********************************************************************/
   4460   1.1    dyoung static int
   4461   1.1    dyoung ixgbe_setup_receive_structures(struct adapter *adapter)
   4462   1.1    dyoung {
   4463   1.1    dyoung 	struct rx_ring *rxr = adapter->rx_rings;
   4464   1.1    dyoung 	int j;
   4465   1.1    dyoung 
   4466   1.1    dyoung 	for (j = 0; j < adapter->num_queues; j++, rxr++)
   4467   1.1    dyoung 		if (ixgbe_setup_receive_ring(rxr))
   4468   1.1    dyoung 			goto fail;
   4469   1.1    dyoung 
   4470   1.1    dyoung 	return (0);
   4471   1.1    dyoung fail:
   4472   1.1    dyoung 	/*
   4473   1.1    dyoung 	 * Free RX buffers allocated so far, we will only handle
   4474   1.1    dyoung 	 * the rings that completed, the failing case will have
   4475   1.1    dyoung 	 * cleaned up for itself. 'j' failed, so its the terminus.
   4476   1.1    dyoung 	 */
   4477   1.1    dyoung 	for (int i = 0; i < j; ++i) {
   4478   1.1    dyoung 		rxr = &adapter->rx_rings[i];
   4479   1.1    dyoung 		ixgbe_free_receive_ring(rxr);
   4480   1.1    dyoung 	}
   4481   1.1    dyoung 
   4482   1.1    dyoung 	return (ENOBUFS);
   4483   1.1    dyoung }
   4484   1.1    dyoung 
   4485  1.33   msaitoh static void
   4486  1.33   msaitoh ixgbe_initialise_rss_mapping(struct adapter *adapter)
   4487  1.33   msaitoh {
   4488  1.33   msaitoh 	struct ixgbe_hw	*hw = &adapter->hw;
   4489  1.33   msaitoh 	uint32_t reta;
   4490  1.33   msaitoh 	int i, j, queue_id;
   4491  1.33   msaitoh 	uint32_t rss_key[10];
   4492  1.33   msaitoh 	uint32_t mrqc;
   4493  1.33   msaitoh #ifdef	RSS
   4494  1.33   msaitoh 	uint32_t rss_hash_config;
   4495  1.33   msaitoh #endif
   4496  1.33   msaitoh 
   4497  1.33   msaitoh 	/* Setup RSS */
   4498  1.33   msaitoh 	reta = 0;
   4499  1.33   msaitoh 
   4500  1.33   msaitoh #ifdef	RSS
   4501  1.33   msaitoh 	/* Fetch the configured RSS key */
   4502  1.33   msaitoh 	rss_getkey((uint8_t *) &rss_key);
   4503  1.33   msaitoh #else
   4504  1.33   msaitoh 	/* set up random bits */
   4505  1.33   msaitoh 	cprng_fast(&rss_key, sizeof(rss_key));
   4506  1.33   msaitoh #endif
   4507  1.33   msaitoh 
   4508  1.33   msaitoh 	/* Set up the redirection table */
   4509  1.33   msaitoh 	for (i = 0, j = 0; i < 128; i++, j++) {
   4510  1.33   msaitoh 		if (j == adapter->num_queues) j = 0;
   4511  1.33   msaitoh #ifdef	RSS
   4512  1.33   msaitoh 		/*
   4513  1.33   msaitoh 		 * Fetch the RSS bucket id for the given indirection entry.
   4514  1.33   msaitoh 		 * Cap it at the number of configured buckets (which is
   4515  1.33   msaitoh 		 * num_queues.)
   4516  1.33   msaitoh 		 */
   4517  1.33   msaitoh 		queue_id = rss_get_indirection_to_bucket(i);
   4518  1.33   msaitoh 		queue_id = queue_id % adapter->num_queues;
   4519  1.33   msaitoh #else
   4520  1.33   msaitoh 		queue_id = (j * 0x11);
   4521  1.33   msaitoh #endif
   4522  1.33   msaitoh 		/*
   4523  1.33   msaitoh 		 * The low 8 bits are for hash value (n+0);
   4524  1.33   msaitoh 		 * The next 8 bits are for hash value (n+1), etc.
   4525  1.33   msaitoh 		 */
   4526  1.33   msaitoh 		reta = reta >> 8;
   4527  1.33   msaitoh 		reta = reta | ( ((uint32_t) queue_id) << 24);
   4528  1.33   msaitoh 		if ((i & 3) == 3) {
   4529  1.33   msaitoh 			IXGBE_WRITE_REG(hw, IXGBE_RETA(i >> 2), reta);
   4530  1.33   msaitoh 			reta = 0;
   4531  1.33   msaitoh 		}
   4532  1.33   msaitoh 	}
   4533  1.33   msaitoh 
   4534  1.33   msaitoh 	/* Now fill our hash function seeds */
   4535  1.33   msaitoh 	for (i = 0; i < 10; i++)
   4536  1.33   msaitoh 		IXGBE_WRITE_REG(hw, IXGBE_RSSRK(i), rss_key[i]);
   4537  1.33   msaitoh 
   4538  1.33   msaitoh 	/* Perform hash on these packet types */
   4539  1.33   msaitoh #ifdef	RSS
   4540  1.33   msaitoh 	mrqc = IXGBE_MRQC_RSSEN;
   4541  1.33   msaitoh 	rss_hash_config = rss_gethashconfig();
   4542  1.33   msaitoh 	if (rss_hash_config & RSS_HASHTYPE_RSS_IPV4)
   4543  1.33   msaitoh 		mrqc |= IXGBE_MRQC_RSS_FIELD_IPV4;
   4544  1.33   msaitoh 	if (rss_hash_config & RSS_HASHTYPE_RSS_TCP_IPV4)
   4545  1.33   msaitoh 		mrqc |= IXGBE_MRQC_RSS_FIELD_IPV4_TCP;
   4546  1.33   msaitoh 	if (rss_hash_config & RSS_HASHTYPE_RSS_IPV6)
   4547  1.33   msaitoh 		mrqc |= IXGBE_MRQC_RSS_FIELD_IPV6;
   4548  1.33   msaitoh 	if (rss_hash_config & RSS_HASHTYPE_RSS_TCP_IPV6)
   4549  1.33   msaitoh 		mrqc |= IXGBE_MRQC_RSS_FIELD_IPV6_TCP;
   4550  1.33   msaitoh 	if (rss_hash_config & RSS_HASHTYPE_RSS_IPV6_EX)
   4551  1.33   msaitoh 		mrqc |= IXGBE_MRQC_RSS_FIELD_IPV6_EX;
   4552  1.33   msaitoh 	if (rss_hash_config & RSS_HASHTYPE_RSS_TCP_IPV6_EX)
   4553  1.33   msaitoh 		mrqc |= IXGBE_MRQC_RSS_FIELD_IPV6_EX_TCP;
   4554  1.33   msaitoh 	if (rss_hash_config & RSS_HASHTYPE_RSS_UDP_IPV4)
   4555  1.33   msaitoh 		mrqc |= IXGBE_MRQC_RSS_FIELD_IPV4_UDP;
   4556  1.33   msaitoh 	if (rss_hash_config & RSS_HASHTYPE_RSS_UDP_IPV4_EX)
   4557  1.33   msaitoh 		device_printf(adapter->dev,
   4558  1.33   msaitoh 		    "%s: RSS_HASHTYPE_RSS_UDP_IPV4_EX defined, "
   4559  1.33   msaitoh 		    "but not supported\n", __func__);
   4560  1.33   msaitoh 	if (rss_hash_config & RSS_HASHTYPE_RSS_UDP_IPV6)
   4561  1.33   msaitoh 		mrqc |= IXGBE_MRQC_RSS_FIELD_IPV6_UDP;
   4562  1.33   msaitoh 	if (rss_hash_config & RSS_HASHTYPE_RSS_UDP_IPV6_EX)
   4563  1.33   msaitoh 		mrqc |= IXGBE_MRQC_RSS_FIELD_IPV6_EX_UDP;
   4564  1.33   msaitoh #else
   4565  1.33   msaitoh 	/*
   4566  1.33   msaitoh 	 * Disable UDP - IP fragments aren't currently being handled
   4567  1.33   msaitoh 	 * and so we end up with a mix of 2-tuple and 4-tuple
   4568  1.33   msaitoh 	 * traffic.
   4569  1.33   msaitoh 	 */
   4570  1.33   msaitoh 	mrqc = IXGBE_MRQC_RSSEN
   4571  1.33   msaitoh 	     | IXGBE_MRQC_RSS_FIELD_IPV4
   4572  1.33   msaitoh 	     | IXGBE_MRQC_RSS_FIELD_IPV4_TCP
   4573  1.33   msaitoh #if 0
   4574  1.33   msaitoh 	     | IXGBE_MRQC_RSS_FIELD_IPV4_UDP
   4575  1.33   msaitoh #endif
   4576  1.33   msaitoh 	     | IXGBE_MRQC_RSS_FIELD_IPV6_EX_TCP
   4577  1.33   msaitoh 	     | IXGBE_MRQC_RSS_FIELD_IPV6_EX
   4578  1.33   msaitoh 	     | IXGBE_MRQC_RSS_FIELD_IPV6
   4579  1.33   msaitoh 	     | IXGBE_MRQC_RSS_FIELD_IPV6_TCP
   4580  1.33   msaitoh #if 0
   4581  1.33   msaitoh 	     | IXGBE_MRQC_RSS_FIELD_IPV6_UDP
   4582  1.33   msaitoh 	     | IXGBE_MRQC_RSS_FIELD_IPV6_EX_UDP
   4583  1.33   msaitoh #endif
   4584  1.33   msaitoh 	;
   4585  1.33   msaitoh #endif /* RSS */
   4586  1.33   msaitoh 	IXGBE_WRITE_REG(hw, IXGBE_MRQC, mrqc);
   4587  1.33   msaitoh }
   4588  1.33   msaitoh 
   4589  1.33   msaitoh 
   4590   1.1    dyoung /*********************************************************************
   4591   1.1    dyoung  *
   4592   1.1    dyoung  *  Setup receive registers and features.
   4593   1.1    dyoung  *
   4594   1.1    dyoung  **********************************************************************/
   4595   1.1    dyoung #define IXGBE_SRRCTL_BSIZEHDRSIZE_SHIFT 2
   4596   1.1    dyoung 
   4597  1.22   msaitoh #define BSIZEPKT_ROUNDUP ((1<<IXGBE_SRRCTL_BSIZEPKT_SHIFT)-1)
   4598  1.22   msaitoh 
   4599   1.1    dyoung static void
   4600   1.1    dyoung ixgbe_initialize_receive_units(struct adapter *adapter)
   4601   1.1    dyoung {
   4602   1.1    dyoung 	int i;
   4603   1.1    dyoung 	struct	rx_ring	*rxr = adapter->rx_rings;
   4604   1.1    dyoung 	struct ixgbe_hw	*hw = &adapter->hw;
   4605   1.1    dyoung 	struct ifnet   *ifp = adapter->ifp;
   4606   1.1    dyoung 	u32		bufsz, rxctrl, fctrl, srrctl, rxcsum;
   4607  1.33   msaitoh 	u32		hlreg;
   4608   1.1    dyoung 
   4609   1.1    dyoung 
   4610   1.1    dyoung 	/*
   4611   1.1    dyoung 	 * Make sure receives are disabled while
   4612   1.1    dyoung 	 * setting up the descriptor ring
   4613   1.1    dyoung 	 */
   4614   1.1    dyoung 	rxctrl = IXGBE_READ_REG(hw, IXGBE_RXCTRL);
   4615   1.1    dyoung 	IXGBE_WRITE_REG(hw, IXGBE_RXCTRL,
   4616   1.1    dyoung 	    rxctrl & ~IXGBE_RXCTRL_RXEN);
   4617   1.1    dyoung 
   4618   1.1    dyoung 	/* Enable broadcasts */
   4619   1.1    dyoung 	fctrl = IXGBE_READ_REG(hw, IXGBE_FCTRL);
   4620   1.1    dyoung 	fctrl |= IXGBE_FCTRL_BAM;
   4621   1.1    dyoung 	fctrl |= IXGBE_FCTRL_DPF;
   4622   1.1    dyoung 	fctrl |= IXGBE_FCTRL_PMCF;
   4623   1.1    dyoung 	IXGBE_WRITE_REG(hw, IXGBE_FCTRL, fctrl);
   4624   1.1    dyoung 
   4625   1.1    dyoung 	/* Set for Jumbo Frames? */
   4626   1.1    dyoung 	hlreg = IXGBE_READ_REG(hw, IXGBE_HLREG0);
   4627   1.1    dyoung 	if (ifp->if_mtu > ETHERMTU)
   4628   1.1    dyoung 		hlreg |= IXGBE_HLREG0_JUMBOEN;
   4629   1.1    dyoung 	else
   4630   1.1    dyoung 		hlreg &= ~IXGBE_HLREG0_JUMBOEN;
   4631  1.25   msaitoh #ifdef DEV_NETMAP
   4632  1.25   msaitoh 	/* crcstrip is conditional in netmap (in RDRXCTL too ?) */
   4633  1.25   msaitoh 	if (ifp->if_capenable & IFCAP_NETMAP && !ix_crcstrip)
   4634  1.25   msaitoh 		hlreg &= ~IXGBE_HLREG0_RXCRCSTRP;
   4635  1.25   msaitoh 	else
   4636  1.25   msaitoh 		hlreg |= IXGBE_HLREG0_RXCRCSTRP;
   4637  1.25   msaitoh #endif /* DEV_NETMAP */
   4638   1.1    dyoung 	IXGBE_WRITE_REG(hw, IXGBE_HLREG0, hlreg);
   4639   1.1    dyoung 
   4640  1.24   msaitoh 	bufsz = (adapter->rx_mbuf_sz +
   4641  1.24   msaitoh 	    BSIZEPKT_ROUNDUP) >> IXGBE_SRRCTL_BSIZEPKT_SHIFT;
   4642   1.1    dyoung 
   4643   1.1    dyoung 	for (i = 0; i < adapter->num_queues; i++, rxr++) {
   4644   1.1    dyoung 		u64 rdba = rxr->rxdma.dma_paddr;
   4645   1.1    dyoung 
   4646   1.1    dyoung 		/* Setup the Base and Length of the Rx Descriptor Ring */
   4647   1.1    dyoung 		IXGBE_WRITE_REG(hw, IXGBE_RDBAL(i),
   4648   1.1    dyoung 			       (rdba & 0x00000000ffffffffULL));
   4649   1.1    dyoung 		IXGBE_WRITE_REG(hw, IXGBE_RDBAH(i), (rdba >> 32));
   4650   1.1    dyoung 		IXGBE_WRITE_REG(hw, IXGBE_RDLEN(i),
   4651   1.1    dyoung 		    adapter->num_rx_desc * sizeof(union ixgbe_adv_rx_desc));
   4652   1.1    dyoung 
   4653   1.1    dyoung 		/* Set up the SRRCTL register */
   4654   1.1    dyoung 		srrctl = IXGBE_READ_REG(hw, IXGBE_SRRCTL(i));
   4655   1.1    dyoung 		srrctl &= ~IXGBE_SRRCTL_BSIZEHDR_MASK;
   4656   1.1    dyoung 		srrctl &= ~IXGBE_SRRCTL_BSIZEPKT_MASK;
   4657   1.1    dyoung 		srrctl |= bufsz;
   4658  1.26   msaitoh 		srrctl |= IXGBE_SRRCTL_DESCTYPE_ADV_ONEBUF;
   4659  1.33   msaitoh 
   4660  1.33   msaitoh 		/*
   4661  1.33   msaitoh 		 * Set DROP_EN iff we have no flow control and >1 queue.
   4662  1.33   msaitoh 		 * Note that srrctl was cleared shortly before during reset,
   4663  1.33   msaitoh 		 * so we do not need to clear the bit, but do it just in case
   4664  1.33   msaitoh 		 * this code is moved elsewhere.
   4665  1.33   msaitoh 		 */
   4666  1.33   msaitoh 		if (adapter->num_queues > 1 &&
   4667  1.33   msaitoh 		    adapter->fc == ixgbe_fc_none) {
   4668  1.33   msaitoh 			srrctl |= IXGBE_SRRCTL_DROP_EN;
   4669  1.33   msaitoh 		} else {
   4670  1.33   msaitoh 			srrctl &= ~IXGBE_SRRCTL_DROP_EN;
   4671  1.33   msaitoh 		}
   4672  1.33   msaitoh 
   4673   1.1    dyoung 		IXGBE_WRITE_REG(hw, IXGBE_SRRCTL(i), srrctl);
   4674   1.1    dyoung 
   4675   1.1    dyoung 		/* Setup the HW Rx Head and Tail Descriptor Pointers */
   4676   1.1    dyoung 		IXGBE_WRITE_REG(hw, IXGBE_RDH(i), 0);
   4677   1.1    dyoung 		IXGBE_WRITE_REG(hw, IXGBE_RDT(i), 0);
   4678  1.28   msaitoh 
   4679  1.28   msaitoh 		/* Set the processing limit */
   4680  1.28   msaitoh 		rxr->process_limit = ixgbe_rx_process_limit;
   4681   1.1    dyoung 	}
   4682   1.1    dyoung 
   4683   1.1    dyoung 	if (adapter->hw.mac.type != ixgbe_mac_82598EB) {
   4684   1.1    dyoung 		u32 psrtype = IXGBE_PSRTYPE_TCPHDR |
   4685   1.1    dyoung 			      IXGBE_PSRTYPE_UDPHDR |
   4686   1.1    dyoung 			      IXGBE_PSRTYPE_IPV4HDR |
   4687   1.1    dyoung 			      IXGBE_PSRTYPE_IPV6HDR;
   4688   1.1    dyoung 		IXGBE_WRITE_REG(hw, IXGBE_PSRTYPE(0), psrtype);
   4689   1.1    dyoung 	}
   4690   1.1    dyoung 
   4691   1.1    dyoung 	rxcsum = IXGBE_READ_REG(hw, IXGBE_RXCSUM);
   4692   1.1    dyoung 
   4693  1.33   msaitoh 	ixgbe_initialise_rss_mapping(adapter);
   4694  1.33   msaitoh 
   4695   1.1    dyoung 	if (adapter->num_queues > 1) {
   4696   1.1    dyoung 		/* RSS and RX IPP Checksum are mutually exclusive */
   4697   1.1    dyoung 		rxcsum |= IXGBE_RXCSUM_PCSD;
   4698   1.1    dyoung 	}
   4699   1.1    dyoung 
   4700   1.1    dyoung 	if (ifp->if_capenable & IFCAP_RXCSUM)
   4701   1.1    dyoung 		rxcsum |= IXGBE_RXCSUM_PCSD;
   4702   1.1    dyoung 
   4703   1.1    dyoung 	if (!(rxcsum & IXGBE_RXCSUM_PCSD))
   4704   1.1    dyoung 		rxcsum |= IXGBE_RXCSUM_IPPCSE;
   4705   1.1    dyoung 
   4706   1.1    dyoung 	IXGBE_WRITE_REG(hw, IXGBE_RXCSUM, rxcsum);
   4707   1.1    dyoung 
   4708   1.1    dyoung 	return;
   4709   1.1    dyoung }
   4710   1.1    dyoung 
   4711   1.1    dyoung /*********************************************************************
   4712   1.1    dyoung  *
   4713   1.1    dyoung  *  Free all receive rings.
   4714   1.1    dyoung  *
   4715   1.1    dyoung  **********************************************************************/
   4716   1.1    dyoung static void
   4717   1.1    dyoung ixgbe_free_receive_structures(struct adapter *adapter)
   4718   1.1    dyoung {
   4719   1.1    dyoung 	struct rx_ring *rxr = adapter->rx_rings;
   4720   1.1    dyoung 
   4721  1.33   msaitoh 	INIT_DEBUGOUT("ixgbe_free_receive_structures: begin");
   4722  1.33   msaitoh 
   4723   1.1    dyoung 	for (int i = 0; i < adapter->num_queues; i++, rxr++) {
   4724   1.1    dyoung #ifdef LRO
   4725   1.1    dyoung 		struct lro_ctrl		*lro = &rxr->lro;
   4726   1.1    dyoung #endif /* LRO */
   4727   1.1    dyoung 		ixgbe_free_receive_buffers(rxr);
   4728   1.1    dyoung #ifdef LRO
   4729   1.1    dyoung 		/* Free LRO memory */
   4730   1.1    dyoung 		tcp_lro_free(lro);
   4731   1.1    dyoung #endif /* LRO */
   4732   1.1    dyoung 		/* Free the ring memory as well */
   4733   1.1    dyoung 		ixgbe_dma_free(adapter, &rxr->rxdma);
   4734  1.16   msaitoh 		IXGBE_RX_LOCK_DESTROY(rxr);
   4735   1.1    dyoung 	}
   4736   1.1    dyoung 
   4737   1.1    dyoung 	free(adapter->rx_rings, M_DEVBUF);
   4738   1.1    dyoung }
   4739   1.1    dyoung 
   4740   1.1    dyoung 
   4741   1.1    dyoung /*********************************************************************
   4742   1.1    dyoung  *
   4743   1.1    dyoung  *  Free receive ring data structures
   4744   1.1    dyoung  *
   4745   1.1    dyoung  **********************************************************************/
   4746   1.1    dyoung static void
   4747   1.1    dyoung ixgbe_free_receive_buffers(struct rx_ring *rxr)
   4748   1.1    dyoung {
   4749   1.1    dyoung 	struct adapter		*adapter = rxr->adapter;
   4750   1.1    dyoung 	struct ixgbe_rx_buf	*rxbuf;
   4751   1.1    dyoung 
   4752  1.33   msaitoh 	INIT_DEBUGOUT("ixgbe_free_receive_buffers: begin");
   4753   1.1    dyoung 
   4754   1.1    dyoung 	/* Cleanup any existing buffers */
   4755   1.1    dyoung 	if (rxr->rx_buffers != NULL) {
   4756   1.1    dyoung 		for (int i = 0; i < adapter->num_rx_desc; i++) {
   4757   1.1    dyoung 			rxbuf = &rxr->rx_buffers[i];
   4758  1.26   msaitoh 			if (rxbuf->buf != NULL) {
   4759  1.28   msaitoh 				bus_dmamap_sync(rxr->ptag->dt_dmat,
   4760  1.28   msaitoh 				    rxbuf->pmap, 0, rxbuf->buf->m_pkthdr.len,
   4761   1.1    dyoung 				    BUS_DMASYNC_POSTREAD);
   4762  1.28   msaitoh 				ixgbe_dmamap_unload(rxr->ptag, rxbuf->pmap);
   4763  1.26   msaitoh 				rxbuf->buf->m_flags |= M_PKTHDR;
   4764  1.26   msaitoh 				m_freem(rxbuf->buf);
   4765   1.1    dyoung 			}
   4766  1.26   msaitoh 			rxbuf->buf = NULL;
   4767  1.28   msaitoh 			if (rxbuf->pmap != NULL) {
   4768  1.28   msaitoh 				ixgbe_dmamap_destroy(rxr->ptag, rxbuf->pmap);
   4769  1.28   msaitoh 				rxbuf->pmap = NULL;
   4770   1.1    dyoung 			}
   4771   1.1    dyoung 		}
   4772   1.1    dyoung 		if (rxr->rx_buffers != NULL) {
   4773   1.1    dyoung 			free(rxr->rx_buffers, M_DEVBUF);
   4774   1.1    dyoung 			rxr->rx_buffers = NULL;
   4775   1.1    dyoung 		}
   4776   1.1    dyoung 	}
   4777   1.1    dyoung 
   4778  1.28   msaitoh 	if (rxr->ptag != NULL) {
   4779  1.28   msaitoh 		ixgbe_dma_tag_destroy(rxr->ptag);
   4780  1.28   msaitoh 		rxr->ptag = NULL;
   4781   1.1    dyoung 	}
   4782   1.1    dyoung 
   4783   1.1    dyoung 	return;
   4784   1.1    dyoung }
   4785   1.1    dyoung 
   4786   1.1    dyoung static __inline void
   4787   1.1    dyoung ixgbe_rx_input(struct rx_ring *rxr, struct ifnet *ifp, struct mbuf *m, u32 ptype)
   4788   1.1    dyoung {
   4789   1.1    dyoung 	int s;
   4790   1.1    dyoung 
   4791   1.9     skrll #ifdef LRO
   4792   1.9     skrll 	struct adapter	*adapter = ifp->if_softc;
   4793   1.9     skrll 	struct ethercom *ec = &adapter->osdep.ec;
   4794   1.1    dyoung 
   4795   1.1    dyoung         /*
   4796  1.25   msaitoh          * ATM LRO is only for IP/TCP packets and TCP checksum of the packet
   4797   1.1    dyoung          * should be computed by hardware. Also it should not have VLAN tag in
   4798  1.25   msaitoh          * ethernet header.  In case of IPv6 we do not yet support ext. hdrs.
   4799   1.1    dyoung          */
   4800   1.1    dyoung         if (rxr->lro_enabled &&
   4801   1.1    dyoung             (ec->ec_capenable & ETHERCAP_VLAN_HWTAGGING) != 0 &&
   4802   1.1    dyoung             (ptype & IXGBE_RXDADV_PKTTYPE_ETQF) == 0 &&
   4803  1.25   msaitoh             ((ptype & (IXGBE_RXDADV_PKTTYPE_IPV4 | IXGBE_RXDADV_PKTTYPE_TCP)) ==
   4804  1.25   msaitoh             (IXGBE_RXDADV_PKTTYPE_IPV4 | IXGBE_RXDADV_PKTTYPE_TCP) ||
   4805  1.25   msaitoh             (ptype & (IXGBE_RXDADV_PKTTYPE_IPV6 | IXGBE_RXDADV_PKTTYPE_TCP)) ==
   4806  1.25   msaitoh             (IXGBE_RXDADV_PKTTYPE_IPV6 | IXGBE_RXDADV_PKTTYPE_TCP)) &&
   4807   1.1    dyoung             (m->m_pkthdr.csum_flags & (CSUM_DATA_VALID | CSUM_PSEUDO_HDR)) ==
   4808   1.1    dyoung             (CSUM_DATA_VALID | CSUM_PSEUDO_HDR)) {
   4809   1.1    dyoung                 /*
   4810   1.1    dyoung                  * Send to the stack if:
   4811   1.1    dyoung                  **  - LRO not enabled, or
   4812   1.1    dyoung                  **  - no LRO resources, or
   4813   1.1    dyoung                  **  - lro enqueue fails
   4814   1.1    dyoung                  */
   4815   1.1    dyoung                 if (rxr->lro.lro_cnt != 0)
   4816   1.1    dyoung                         if (tcp_lro_rx(&rxr->lro, m, 0) == 0)
   4817   1.1    dyoung                                 return;
   4818   1.1    dyoung         }
   4819   1.1    dyoung #endif /* LRO */
   4820   1.1    dyoung 
   4821   1.1    dyoung 	IXGBE_RX_UNLOCK(rxr);
   4822   1.1    dyoung 
   4823   1.1    dyoung 	s = splnet();
   4824   1.1    dyoung 	/* Pass this up to any BPF listeners. */
   4825   1.1    dyoung 	bpf_mtap(ifp, m);
   4826  1.37     ozaki 	if_input(ifp, m);
   4827   1.1    dyoung 	splx(s);
   4828   1.1    dyoung 
   4829   1.1    dyoung 	IXGBE_RX_LOCK(rxr);
   4830   1.1    dyoung }
   4831   1.1    dyoung 
   4832   1.1    dyoung static __inline void
   4833   1.1    dyoung ixgbe_rx_discard(struct rx_ring *rxr, int i)
   4834   1.1    dyoung {
   4835   1.1    dyoung 	struct ixgbe_rx_buf	*rbuf;
   4836   1.1    dyoung 
   4837   1.1    dyoung 	rbuf = &rxr->rx_buffers[i];
   4838   1.1    dyoung 
   4839  1.35   msaitoh 
   4840   1.1    dyoung 	/*
   4841   1.1    dyoung 	** With advanced descriptors the writeback
   4842   1.1    dyoung 	** clobbers the buffer addrs, so its easier
   4843   1.1    dyoung 	** to just free the existing mbufs and take
   4844   1.1    dyoung 	** the normal refresh path to get new buffers
   4845   1.1    dyoung 	** and mapping.
   4846   1.1    dyoung 	*/
   4847  1.35   msaitoh 
   4848  1.33   msaitoh 	if (rbuf->buf != NULL) {/* Partial chain ? */
   4849  1.33   msaitoh 		rbuf->fmp->m_flags |= M_PKTHDR;
   4850  1.33   msaitoh 		m_freem(rbuf->fmp);
   4851  1.33   msaitoh 		rbuf->fmp = NULL;
   4852  1.33   msaitoh 		rbuf->buf = NULL; /* rbuf->buf is part of fmp's chain */
   4853  1.33   msaitoh 	} else if (rbuf->buf) {
   4854  1.35   msaitoh 		m_free(rbuf->buf);
   4855  1.35   msaitoh 		rbuf->buf = NULL;
   4856   1.1    dyoung 	}
   4857  1.35   msaitoh 
   4858  1.33   msaitoh 	rbuf->flags = 0;
   4859  1.33   msaitoh 
   4860   1.1    dyoung 	return;
   4861   1.1    dyoung }
   4862   1.1    dyoung 
   4863   1.1    dyoung 
   4864   1.1    dyoung /*********************************************************************
   4865   1.1    dyoung  *
   4866   1.1    dyoung  *  This routine executes in interrupt context. It replenishes
   4867   1.1    dyoung  *  the mbufs in the descriptor and sends data which has been
   4868   1.1    dyoung  *  dma'ed into host memory to upper layer.
   4869   1.1    dyoung  *
   4870   1.1    dyoung  *  We loop at most count times if count is > 0, or until done if
   4871   1.1    dyoung  *  count < 0.
   4872   1.1    dyoung  *
   4873   1.1    dyoung  *  Return TRUE for more work, FALSE for all clean.
   4874   1.1    dyoung  *********************************************************************/
   4875   1.1    dyoung static bool
   4876  1.28   msaitoh ixgbe_rxeof(struct ix_queue *que)
   4877   1.1    dyoung {
   4878   1.1    dyoung 	struct adapter		*adapter = que->adapter;
   4879   1.1    dyoung 	struct rx_ring		*rxr = que->rxr;
   4880   1.1    dyoung 	struct ifnet		*ifp = adapter->ifp;
   4881   1.1    dyoung #ifdef LRO
   4882   1.1    dyoung 	struct lro_ctrl		*lro = &rxr->lro;
   4883   1.1    dyoung 	struct lro_entry	*queued;
   4884   1.1    dyoung #endif /* LRO */
   4885   1.1    dyoung 	int			i, nextp, processed = 0;
   4886   1.1    dyoung 	u32			staterr = 0;
   4887  1.28   msaitoh 	u16			count = rxr->process_limit;
   4888   1.1    dyoung 	union ixgbe_adv_rx_desc	*cur;
   4889   1.1    dyoung 	struct ixgbe_rx_buf	*rbuf, *nbuf;
   4890  1.33   msaitoh #ifdef RSS
   4891  1.33   msaitoh 	u16			pkt_info;
   4892  1.33   msaitoh #endif
   4893   1.1    dyoung 
   4894   1.1    dyoung 	IXGBE_RX_LOCK(rxr);
   4895   1.1    dyoung 
   4896  1.22   msaitoh #ifdef DEV_NETMAP
   4897  1.28   msaitoh 	/* Same as the txeof routine: wakeup clients on intr. */
   4898  1.33   msaitoh 	if (netmap_rx_irq(ifp, rxr->me, &processed)) {
   4899  1.33   msaitoh 		IXGBE_RX_UNLOCK(rxr);
   4900  1.22   msaitoh 		return (FALSE);
   4901  1.33   msaitoh 	}
   4902  1.22   msaitoh #endif /* DEV_NETMAP */
   4903  1.33   msaitoh 
   4904   1.1    dyoung 	for (i = rxr->next_to_check; count != 0;) {
   4905  1.26   msaitoh 		struct mbuf	*sendmp, *mp;
   4906   1.1    dyoung 		u32		rsc, ptype;
   4907  1.26   msaitoh 		u16		len;
   4908  1.24   msaitoh 		u16		vtag = 0;
   4909   1.1    dyoung 		bool		eop;
   4910   1.1    dyoung 
   4911   1.1    dyoung 		/* Sync the ring. */
   4912   1.1    dyoung 		ixgbe_dmamap_sync(rxr->rxdma.dma_tag, rxr->rxdma.dma_map,
   4913   1.1    dyoung 		    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
   4914   1.1    dyoung 
   4915   1.1    dyoung 		cur = &rxr->rx_base[i];
   4916   1.1    dyoung 		staterr = le32toh(cur->wb.upper.status_error);
   4917  1.33   msaitoh #ifdef RSS
   4918  1.33   msaitoh 		pkt_info = le16toh(cur->wb.lower.lo_dword.hs_rss.pkt_info);
   4919  1.33   msaitoh #endif
   4920   1.1    dyoung 
   4921   1.1    dyoung 		if ((staterr & IXGBE_RXD_STAT_DD) == 0)
   4922   1.1    dyoung 			break;
   4923   1.1    dyoung 		if ((ifp->if_flags & IFF_RUNNING) == 0)
   4924   1.1    dyoung 			break;
   4925   1.1    dyoung 
   4926   1.1    dyoung 		count--;
   4927   1.1    dyoung 		sendmp = NULL;
   4928   1.1    dyoung 		nbuf = NULL;
   4929   1.1    dyoung 		rsc = 0;
   4930   1.1    dyoung 		cur->wb.upper.status_error = 0;
   4931   1.1    dyoung 		rbuf = &rxr->rx_buffers[i];
   4932  1.26   msaitoh 		mp = rbuf->buf;
   4933   1.1    dyoung 
   4934  1.26   msaitoh 		len = le16toh(cur->wb.upper.length);
   4935   1.1    dyoung 		ptype = le32toh(cur->wb.lower.lo_dword.data) &
   4936   1.1    dyoung 		    IXGBE_RXDADV_PKTTYPE_MASK;
   4937   1.1    dyoung 		eop = ((staterr & IXGBE_RXD_STAT_EOP) != 0);
   4938   1.1    dyoung 
   4939   1.1    dyoung 		/* Make sure bad packets are discarded */
   4940  1.33   msaitoh 		if (eop && (staterr & IXGBE_RXDADV_ERR_FRAME_ERR_MASK) != 0) {
   4941   1.1    dyoung 			rxr->rx_discarded.ev_count++;
   4942   1.1    dyoung 			ixgbe_rx_discard(rxr, i);
   4943   1.1    dyoung 			goto next_desc;
   4944   1.1    dyoung 		}
   4945   1.1    dyoung 
   4946   1.1    dyoung 		/*
   4947   1.1    dyoung 		** On 82599 which supports a hardware
   4948   1.1    dyoung 		** LRO (called HW RSC), packets need
   4949   1.1    dyoung 		** not be fragmented across sequential
   4950   1.1    dyoung 		** descriptors, rather the next descriptor
   4951   1.1    dyoung 		** is indicated in bits of the descriptor.
   4952   1.1    dyoung 		** This also means that we might proceses
   4953   1.1    dyoung 		** more than one packet at a time, something
   4954   1.1    dyoung 		** that has never been true before, it
   4955   1.1    dyoung 		** required eliminating global chain pointers
   4956   1.1    dyoung 		** in favor of what we are doing here.  -jfv
   4957   1.1    dyoung 		*/
   4958   1.1    dyoung 		if (!eop) {
   4959   1.1    dyoung 			/*
   4960   1.1    dyoung 			** Figure out the next descriptor
   4961   1.1    dyoung 			** of this frame.
   4962   1.1    dyoung 			*/
   4963   1.1    dyoung 			if (rxr->hw_rsc == TRUE) {
   4964   1.1    dyoung 				rsc = ixgbe_rsc_count(cur);
   4965   1.1    dyoung 				rxr->rsc_num += (rsc - 1);
   4966   1.1    dyoung 			}
   4967   1.1    dyoung 			if (rsc) { /* Get hardware index */
   4968   1.1    dyoung 				nextp = ((staterr &
   4969   1.1    dyoung 				    IXGBE_RXDADV_NEXTP_MASK) >>
   4970   1.1    dyoung 				    IXGBE_RXDADV_NEXTP_SHIFT);
   4971   1.1    dyoung 			} else { /* Just sequential */
   4972   1.1    dyoung 				nextp = i + 1;
   4973   1.1    dyoung 				if (nextp == adapter->num_rx_desc)
   4974   1.1    dyoung 					nextp = 0;
   4975   1.1    dyoung 			}
   4976   1.1    dyoung 			nbuf = &rxr->rx_buffers[nextp];
   4977   1.1    dyoung 			prefetch(nbuf);
   4978   1.1    dyoung 		}
   4979   1.1    dyoung 		/*
   4980   1.1    dyoung 		** Rather than using the fmp/lmp global pointers
   4981   1.1    dyoung 		** we now keep the head of a packet chain in the
   4982   1.1    dyoung 		** buffer struct and pass this along from one
   4983   1.1    dyoung 		** descriptor to the next, until we get EOP.
   4984   1.1    dyoung 		*/
   4985  1.26   msaitoh 		mp->m_len = len;
   4986  1.26   msaitoh 		/*
   4987  1.26   msaitoh 		** See if there is a stored head
   4988  1.26   msaitoh 		** that determines what we are
   4989  1.26   msaitoh 		*/
   4990  1.26   msaitoh 		sendmp = rbuf->fmp;
   4991  1.26   msaitoh 		if (sendmp != NULL) {  /* secondary frag */
   4992  1.26   msaitoh 			rbuf->buf = rbuf->fmp = NULL;
   4993  1.26   msaitoh 			mp->m_flags &= ~M_PKTHDR;
   4994  1.26   msaitoh 			sendmp->m_pkthdr.len += mp->m_len;
   4995  1.26   msaitoh 		} else {
   4996   1.1    dyoung 			/*
   4997  1.26   msaitoh 			 * Optimize.  This might be a small packet,
   4998  1.26   msaitoh 			 * maybe just a TCP ACK.  Do a fast copy that
   4999  1.26   msaitoh 			 * is cache aligned into a new mbuf, and
   5000  1.26   msaitoh 			 * leave the old mbuf+cluster for re-use.
   5001  1.26   msaitoh 			 */
   5002  1.26   msaitoh 			if (eop && len <= IXGBE_RX_COPY_LEN) {
   5003  1.28   msaitoh 				sendmp = m_gethdr(M_NOWAIT, MT_DATA);
   5004  1.26   msaitoh 				if (sendmp != NULL) {
   5005  1.26   msaitoh 					sendmp->m_data +=
   5006  1.26   msaitoh 					    IXGBE_RX_COPY_ALIGN;
   5007  1.26   msaitoh 					ixgbe_bcopy(mp->m_data,
   5008  1.26   msaitoh 					    sendmp->m_data, len);
   5009  1.26   msaitoh 					sendmp->m_len = len;
   5010  1.26   msaitoh 					rxr->rx_copies.ev_count++;
   5011  1.26   msaitoh 					rbuf->flags |= IXGBE_RX_COPY;
   5012  1.26   msaitoh 				}
   5013   1.1    dyoung 			}
   5014  1.26   msaitoh 			if (sendmp == NULL) {
   5015  1.26   msaitoh 				rbuf->buf = rbuf->fmp = NULL;
   5016   1.1    dyoung 				sendmp = mp;
   5017   1.1    dyoung 			}
   5018  1.26   msaitoh 
   5019  1.26   msaitoh 			/* first desc of a non-ps chain */
   5020  1.26   msaitoh 			sendmp->m_flags |= M_PKTHDR;
   5021  1.26   msaitoh 			sendmp->m_pkthdr.len = mp->m_len;
   5022   1.1    dyoung 		}
   5023   1.1    dyoung 		++processed;
   5024  1.35   msaitoh 
   5025  1.26   msaitoh 		/* Pass the head pointer on */
   5026  1.26   msaitoh 		if (eop == 0) {
   5027  1.26   msaitoh 			nbuf->fmp = sendmp;
   5028  1.26   msaitoh 			sendmp = NULL;
   5029  1.26   msaitoh 			mp->m_next = nbuf->buf;
   5030  1.26   msaitoh 		} else { /* Sending this frame */
   5031   1.1    dyoung 			sendmp->m_pkthdr.rcvif = ifp;
   5032   1.1    dyoung 			ifp->if_ipackets++;
   5033   1.1    dyoung 			rxr->rx_packets.ev_count++;
   5034   1.1    dyoung 			/* capture data for AIM */
   5035   1.1    dyoung 			rxr->bytes += sendmp->m_pkthdr.len;
   5036   1.1    dyoung 			rxr->rx_bytes.ev_count += sendmp->m_pkthdr.len;
   5037  1.26   msaitoh 			/* Process vlan info */
   5038  1.26   msaitoh 			if ((rxr->vtag_strip) &&
   5039  1.26   msaitoh 			    (staterr & IXGBE_RXD_STAT_VP))
   5040  1.26   msaitoh 				vtag = le16toh(cur->wb.upper.vlan);
   5041  1.26   msaitoh 			if (vtag) {
   5042  1.26   msaitoh 				VLAN_INPUT_TAG(ifp, sendmp, vtag,
   5043  1.26   msaitoh 				    printf("%s: could not apply VLAN "
   5044  1.26   msaitoh 					"tag", __func__));
   5045  1.26   msaitoh 			}
   5046   1.1    dyoung 			if ((ifp->if_capenable & IFCAP_RXCSUM) != 0) {
   5047   1.1    dyoung 				ixgbe_rx_checksum(staterr, sendmp, ptype,
   5048   1.1    dyoung 				   &adapter->stats);
   5049   1.1    dyoung 			}
   5050   1.1    dyoung #if __FreeBSD_version >= 800000
   5051  1.33   msaitoh #ifdef RSS
   5052  1.33   msaitoh 			sendmp->m_pkthdr.flowid =
   5053  1.33   msaitoh 			    le32toh(cur->wb.lower.hi_dword.rss);
   5054  1.33   msaitoh 			switch (pkt_info & IXGBE_RXDADV_RSSTYPE_MASK) {
   5055  1.33   msaitoh 			case IXGBE_RXDADV_RSSTYPE_IPV4_TCP:
   5056  1.33   msaitoh 				M_HASHTYPE_SET(sendmp, M_HASHTYPE_RSS_TCP_IPV4);
   5057  1.33   msaitoh 				break;
   5058  1.33   msaitoh 			case IXGBE_RXDADV_RSSTYPE_IPV4:
   5059  1.33   msaitoh 				M_HASHTYPE_SET(sendmp, M_HASHTYPE_RSS_IPV4);
   5060  1.33   msaitoh 				break;
   5061  1.33   msaitoh 			case IXGBE_RXDADV_RSSTYPE_IPV6_TCP:
   5062  1.33   msaitoh 				M_HASHTYPE_SET(sendmp, M_HASHTYPE_RSS_TCP_IPV6);
   5063  1.33   msaitoh 				break;
   5064  1.33   msaitoh 			case IXGBE_RXDADV_RSSTYPE_IPV6_EX:
   5065  1.33   msaitoh 				M_HASHTYPE_SET(sendmp, M_HASHTYPE_RSS_IPV6_EX);
   5066  1.33   msaitoh 				break;
   5067  1.33   msaitoh 			case IXGBE_RXDADV_RSSTYPE_IPV6:
   5068  1.33   msaitoh 				M_HASHTYPE_SET(sendmp, M_HASHTYPE_RSS_IPV6);
   5069  1.33   msaitoh 				break;
   5070  1.33   msaitoh 			case IXGBE_RXDADV_RSSTYPE_IPV6_TCP_EX:
   5071  1.33   msaitoh 				M_HASHTYPE_SET(sendmp, M_HASHTYPE_RSS_TCP_IPV6_EX);
   5072  1.33   msaitoh 				break;
   5073  1.33   msaitoh 			case IXGBE_RXDADV_RSSTYPE_IPV4_UDP:
   5074  1.33   msaitoh 				M_HASHTYPE_SET(sendmp, M_HASHTYPE_RSS_UDP_IPV4);
   5075  1.33   msaitoh 				break;
   5076  1.33   msaitoh 			case IXGBE_RXDADV_RSSTYPE_IPV6_UDP:
   5077  1.33   msaitoh 				M_HASHTYPE_SET(sendmp, M_HASHTYPE_RSS_UDP_IPV6);
   5078  1.33   msaitoh 				break;
   5079  1.33   msaitoh 			case IXGBE_RXDADV_RSSTYPE_IPV6_UDP_EX:
   5080  1.33   msaitoh 				M_HASHTYPE_SET(sendmp, M_HASHTYPE_RSS_UDP_IPV6_EX);
   5081  1.33   msaitoh 				break;
   5082  1.33   msaitoh 			default:
   5083  1.33   msaitoh 				/* XXX fallthrough */
   5084  1.33   msaitoh 				M_HASHTYPE_SET(sendmp, M_HASHTYPE_OPAQUE);
   5085  1.33   msaitoh 				break;
   5086  1.33   msaitoh 			}
   5087  1.33   msaitoh #else /* RSS */
   5088   1.1    dyoung 			sendmp->m_pkthdr.flowid = que->msix;
   5089  1.33   msaitoh 			M_HASHTYPE_SET(sendmp, M_HASHTYPE_OPAQUE);
   5090  1.33   msaitoh #endif /* RSS */
   5091  1.33   msaitoh #endif /* FreeBSD_version */
   5092   1.1    dyoung 		}
   5093   1.1    dyoung next_desc:
   5094   1.1    dyoung 		ixgbe_dmamap_sync(rxr->rxdma.dma_tag, rxr->rxdma.dma_map,
   5095   1.1    dyoung 		    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
   5096   1.1    dyoung 
   5097   1.1    dyoung 		/* Advance our pointers to the next descriptor. */
   5098  1.28   msaitoh 		if (++i == rxr->num_desc)
   5099   1.1    dyoung 			i = 0;
   5100   1.1    dyoung 
   5101   1.1    dyoung 		/* Now send to the stack or do LRO */
   5102   1.1    dyoung 		if (sendmp != NULL) {
   5103   1.1    dyoung 			rxr->next_to_check = i;
   5104   1.1    dyoung 			ixgbe_rx_input(rxr, ifp, sendmp, ptype);
   5105   1.1    dyoung 			i = rxr->next_to_check;
   5106   1.1    dyoung 		}
   5107   1.1    dyoung 
   5108   1.1    dyoung                /* Every 8 descriptors we go to refresh mbufs */
   5109   1.1    dyoung 		if (processed == 8) {
   5110   1.1    dyoung 			ixgbe_refresh_mbufs(rxr, i);
   5111   1.1    dyoung 			processed = 0;
   5112   1.1    dyoung 		}
   5113   1.1    dyoung 	}
   5114   1.1    dyoung 
   5115   1.1    dyoung 	/* Refresh any remaining buf structs */
   5116   1.1    dyoung 	if (ixgbe_rx_unrefreshed(rxr))
   5117   1.1    dyoung 		ixgbe_refresh_mbufs(rxr, i);
   5118   1.1    dyoung 
   5119   1.1    dyoung 	rxr->next_to_check = i;
   5120   1.1    dyoung 
   5121   1.1    dyoung #ifdef LRO
   5122   1.1    dyoung 	/*
   5123   1.1    dyoung 	 * Flush any outstanding LRO work
   5124   1.1    dyoung 	 */
   5125   1.1    dyoung 	while ((queued = SLIST_FIRST(&lro->lro_active)) != NULL) {
   5126   1.1    dyoung 		SLIST_REMOVE_HEAD(&lro->lro_active, next);
   5127   1.1    dyoung 		tcp_lro_flush(lro, queued);
   5128   1.1    dyoung 	}
   5129   1.1    dyoung #endif /* LRO */
   5130   1.1    dyoung 
   5131   1.1    dyoung 	IXGBE_RX_UNLOCK(rxr);
   5132   1.1    dyoung 
   5133   1.1    dyoung 	/*
   5134  1.33   msaitoh 	** Still have cleaning to do?
   5135   1.1    dyoung 	*/
   5136  1.33   msaitoh 	if ((staterr & IXGBE_RXD_STAT_DD) != 0)
   5137   1.1    dyoung 		return true;
   5138  1.33   msaitoh 	else
   5139  1.33   msaitoh 		return false;
   5140   1.1    dyoung }
   5141   1.1    dyoung 
   5142   1.1    dyoung 
   5143   1.1    dyoung /*********************************************************************
   5144   1.1    dyoung  *
   5145   1.1    dyoung  *  Verify that the hardware indicated that the checksum is valid.
   5146   1.1    dyoung  *  Inform the stack about the status of checksum so that stack
   5147   1.1    dyoung  *  doesn't spend time verifying the checksum.
   5148   1.1    dyoung  *
   5149   1.1    dyoung  *********************************************************************/
   5150   1.1    dyoung static void
   5151   1.1    dyoung ixgbe_rx_checksum(u32 staterr, struct mbuf * mp, u32 ptype,
   5152   1.1    dyoung     struct ixgbe_hw_stats *stats)
   5153   1.1    dyoung {
   5154   1.1    dyoung 	u16	status = (u16) staterr;
   5155   1.1    dyoung 	u8	errors = (u8) (staterr >> 24);
   5156   1.9     skrll #if 0
   5157   1.1    dyoung 	bool	sctp = FALSE;
   5158   1.1    dyoung 
   5159   1.1    dyoung 	if ((ptype & IXGBE_RXDADV_PKTTYPE_ETQF) == 0 &&
   5160   1.1    dyoung 	    (ptype & IXGBE_RXDADV_PKTTYPE_SCTP) != 0)
   5161   1.1    dyoung 		sctp = TRUE;
   5162   1.9     skrll #endif
   5163   1.1    dyoung 
   5164   1.1    dyoung 	if (status & IXGBE_RXD_STAT_IPCS) {
   5165   1.1    dyoung 		stats->ipcs.ev_count++;
   5166   1.1    dyoung 		if (!(errors & IXGBE_RXD_ERR_IPE)) {
   5167   1.1    dyoung 			/* IP Checksum Good */
   5168   1.1    dyoung 			mp->m_pkthdr.csum_flags = M_CSUM_IPv4;
   5169   1.1    dyoung 
   5170   1.1    dyoung 		} else {
   5171   1.1    dyoung 			stats->ipcs_bad.ev_count++;
   5172   1.1    dyoung 			mp->m_pkthdr.csum_flags = M_CSUM_IPv4|M_CSUM_IPv4_BAD;
   5173   1.1    dyoung 		}
   5174   1.1    dyoung 	}
   5175   1.1    dyoung 	if (status & IXGBE_RXD_STAT_L4CS) {
   5176   1.1    dyoung 		stats->l4cs.ev_count++;
   5177  1.33   msaitoh 		int type = M_CSUM_TCPv4|M_CSUM_TCPv6|M_CSUM_UDPv4|M_CSUM_UDPv6;
   5178   1.1    dyoung 		if (!(errors & IXGBE_RXD_ERR_TCPE)) {
   5179   1.1    dyoung 			mp->m_pkthdr.csum_flags |= type;
   5180   1.1    dyoung 		} else {
   5181   1.1    dyoung 			stats->l4cs_bad.ev_count++;
   5182   1.1    dyoung 			mp->m_pkthdr.csum_flags |= type | M_CSUM_TCP_UDP_BAD;
   5183   1.1    dyoung 		}
   5184   1.1    dyoung 	}
   5185   1.1    dyoung 	return;
   5186   1.1    dyoung }
   5187   1.1    dyoung 
   5188   1.1    dyoung 
   5189   1.1    dyoung #if 0	/* XXX Badly need to overhaul vlan(4) on NetBSD. */
   5190   1.1    dyoung /*
   5191   1.1    dyoung ** This routine is run via an vlan config EVENT,
   5192   1.1    dyoung ** it enables us to use the HW Filter table since
   5193   1.1    dyoung ** we can get the vlan id. This just creates the
   5194   1.1    dyoung ** entry in the soft version of the VFTA, init will
   5195   1.1    dyoung ** repopulate the real table.
   5196   1.1    dyoung */
   5197   1.1    dyoung static void
   5198   1.1    dyoung ixgbe_register_vlan(void *arg, struct ifnet *ifp, u16 vtag)
   5199   1.1    dyoung {
   5200   1.1    dyoung 	struct adapter	*adapter = ifp->if_softc;
   5201   1.1    dyoung 	u16		index, bit;
   5202   1.1    dyoung 
   5203   1.1    dyoung 	if (ifp->if_softc !=  arg)   /* Not our event */
   5204   1.1    dyoung 		return;
   5205   1.1    dyoung 
   5206   1.1    dyoung 	if ((vtag == 0) || (vtag > 4095))	/* Invalid */
   5207   1.1    dyoung 		return;
   5208   1.1    dyoung 
   5209   1.1    dyoung 	IXGBE_CORE_LOCK(adapter);
   5210   1.1    dyoung 	index = (vtag >> 5) & 0x7F;
   5211   1.1    dyoung 	bit = vtag & 0x1F;
   5212   1.1    dyoung 	adapter->shadow_vfta[index] |= (1 << bit);
   5213  1.33   msaitoh 	ixgbe_setup_vlan_hw_support(adapter);
   5214   1.1    dyoung 	IXGBE_CORE_UNLOCK(adapter);
   5215   1.1    dyoung }
   5216   1.1    dyoung 
   5217   1.1    dyoung /*
   5218   1.1    dyoung ** This routine is run via an vlan
   5219   1.1    dyoung ** unconfig EVENT, remove our entry
   5220   1.1    dyoung ** in the soft vfta.
   5221   1.1    dyoung */
   5222   1.1    dyoung static void
   5223   1.1    dyoung ixgbe_unregister_vlan(void *arg, struct ifnet *ifp, u16 vtag)
   5224   1.1    dyoung {
   5225   1.1    dyoung 	struct adapter	*adapter = ifp->if_softc;
   5226   1.1    dyoung 	u16		index, bit;
   5227   1.1    dyoung 
   5228   1.1    dyoung 	if (ifp->if_softc !=  arg)
   5229   1.1    dyoung 		return;
   5230   1.1    dyoung 
   5231   1.1    dyoung 	if ((vtag == 0) || (vtag > 4095))	/* Invalid */
   5232   1.1    dyoung 		return;
   5233   1.1    dyoung 
   5234   1.1    dyoung 	IXGBE_CORE_LOCK(adapter);
   5235   1.1    dyoung 	index = (vtag >> 5) & 0x7F;
   5236   1.1    dyoung 	bit = vtag & 0x1F;
   5237   1.1    dyoung 	adapter->shadow_vfta[index] &= ~(1 << bit);
   5238   1.1    dyoung 	/* Re-init to load the changes */
   5239  1.33   msaitoh 	ixgbe_setup_vlan_hw_support(adapter);
   5240   1.1    dyoung 	IXGBE_CORE_UNLOCK(adapter);
   5241   1.1    dyoung }
   5242   1.1    dyoung #endif
   5243   1.1    dyoung 
   5244   1.1    dyoung static void
   5245   1.1    dyoung ixgbe_setup_vlan_hw_support(struct adapter *adapter)
   5246   1.1    dyoung {
   5247   1.1    dyoung 	struct ethercom *ec = &adapter->osdep.ec;
   5248   1.1    dyoung 	struct ixgbe_hw *hw = &adapter->hw;
   5249  1.24   msaitoh 	struct rx_ring	*rxr;
   5250   1.1    dyoung 	u32		ctrl;
   5251   1.1    dyoung 
   5252  1.35   msaitoh 
   5253   1.1    dyoung 	/*
   5254   1.1    dyoung 	** We get here thru init_locked, meaning
   5255   1.1    dyoung 	** a soft reset, this has already cleared
   5256   1.1    dyoung 	** the VFTA and other state, so if there
   5257   1.1    dyoung 	** have been no vlan's registered do nothing.
   5258   1.1    dyoung 	*/
   5259   1.1    dyoung 	if (!VLAN_ATTACHED(&adapter->osdep.ec)) {
   5260   1.1    dyoung 		return;
   5261   1.1    dyoung 	}
   5262   1.1    dyoung 
   5263  1.33   msaitoh 	/* Setup the queues for vlans */
   5264  1.33   msaitoh 	for (int i = 0; i < adapter->num_queues; i++) {
   5265  1.33   msaitoh 		rxr = &adapter->rx_rings[i];
   5266  1.33   msaitoh 		/* On 82599 the VLAN enable is per/queue in RXDCTL */
   5267  1.33   msaitoh 		if (hw->mac.type != ixgbe_mac_82598EB) {
   5268  1.33   msaitoh 			ctrl = IXGBE_READ_REG(hw, IXGBE_RXDCTL(i));
   5269  1.33   msaitoh 			ctrl |= IXGBE_RXDCTL_VME;
   5270  1.33   msaitoh 			IXGBE_WRITE_REG(hw, IXGBE_RXDCTL(i), ctrl);
   5271  1.33   msaitoh 		}
   5272  1.33   msaitoh 		rxr->vtag_strip = TRUE;
   5273  1.33   msaitoh 	}
   5274  1.33   msaitoh 
   5275  1.33   msaitoh 	if ((ec->ec_capenable & ETHERCAP_VLAN_HWFILTER) == 0)
   5276  1.33   msaitoh 		return;
   5277   1.1    dyoung 	/*
   5278   1.1    dyoung 	** A soft reset zero's out the VFTA, so
   5279   1.1    dyoung 	** we need to repopulate it now.
   5280   1.1    dyoung 	*/
   5281   1.1    dyoung 	for (int i = 0; i < IXGBE_VFTA_SIZE; i++)
   5282   1.1    dyoung 		if (adapter->shadow_vfta[i] != 0)
   5283   1.1    dyoung 			IXGBE_WRITE_REG(hw, IXGBE_VFTA(i),
   5284   1.1    dyoung 			    adapter->shadow_vfta[i]);
   5285   1.1    dyoung 
   5286   1.1    dyoung 	ctrl = IXGBE_READ_REG(hw, IXGBE_VLNCTRL);
   5287   1.1    dyoung 	/* Enable the Filter Table if enabled */
   5288   1.1    dyoung 	if (ec->ec_capenable & ETHERCAP_VLAN_HWFILTER) {
   5289   1.1    dyoung 		ctrl &= ~IXGBE_VLNCTRL_CFIEN;
   5290   1.1    dyoung 		ctrl |= IXGBE_VLNCTRL_VFE;
   5291   1.1    dyoung 	}
   5292   1.1    dyoung 	if (hw->mac.type == ixgbe_mac_82598EB)
   5293   1.1    dyoung 		ctrl |= IXGBE_VLNCTRL_VME;
   5294   1.1    dyoung 	IXGBE_WRITE_REG(hw, IXGBE_VLNCTRL, ctrl);
   5295   1.1    dyoung }
   5296   1.1    dyoung 
   5297   1.1    dyoung static void
   5298   1.1    dyoung ixgbe_enable_intr(struct adapter *adapter)
   5299   1.1    dyoung {
   5300  1.28   msaitoh 	struct ixgbe_hw	*hw = &adapter->hw;
   5301  1.28   msaitoh 	struct ix_queue	*que = adapter->queues;
   5302  1.28   msaitoh 	u32		mask, fwsm;
   5303   1.1    dyoung 
   5304  1.28   msaitoh 	mask = (IXGBE_EIMS_ENABLE_MASK & ~IXGBE_EIMS_RTX_QUEUE);
   5305   1.1    dyoung 	/* Enable Fan Failure detection */
   5306   1.1    dyoung 	if (hw->device_id == IXGBE_DEV_ID_82598AT)
   5307   1.1    dyoung 		    mask |= IXGBE_EIMS_GPI_SDP1;
   5308  1.28   msaitoh 
   5309  1.28   msaitoh 	switch (adapter->hw.mac.type) {
   5310  1.28   msaitoh 		case ixgbe_mac_82599EB:
   5311  1.28   msaitoh 			mask |= IXGBE_EIMS_ECC;
   5312  1.28   msaitoh 			mask |= IXGBE_EIMS_GPI_SDP0;
   5313  1.28   msaitoh 			mask |= IXGBE_EIMS_GPI_SDP1;
   5314  1.28   msaitoh 			mask |= IXGBE_EIMS_GPI_SDP2;
   5315  1.28   msaitoh #ifdef IXGBE_FDIR
   5316  1.28   msaitoh 			mask |= IXGBE_EIMS_FLOW_DIR;
   5317  1.28   msaitoh #endif
   5318  1.28   msaitoh 			break;
   5319  1.28   msaitoh 		case ixgbe_mac_X540:
   5320  1.28   msaitoh 			mask |= IXGBE_EIMS_ECC;
   5321  1.28   msaitoh 			/* Detect if Thermal Sensor is enabled */
   5322  1.28   msaitoh 			fwsm = IXGBE_READ_REG(hw, IXGBE_FWSM);
   5323  1.28   msaitoh 			if (fwsm & IXGBE_FWSM_TS_ENABLED)
   5324  1.28   msaitoh 				mask |= IXGBE_EIMS_TS;
   5325   1.1    dyoung #ifdef IXGBE_FDIR
   5326  1.28   msaitoh 			mask |= IXGBE_EIMS_FLOW_DIR;
   5327   1.1    dyoung #endif
   5328  1.28   msaitoh 		/* falls through */
   5329  1.28   msaitoh 		default:
   5330  1.28   msaitoh 			break;
   5331   1.1    dyoung 	}
   5332   1.1    dyoung 
   5333   1.1    dyoung 	IXGBE_WRITE_REG(hw, IXGBE_EIMS, mask);
   5334   1.1    dyoung 
   5335   1.1    dyoung 	/* With RSS we use auto clear */
   5336   1.1    dyoung 	if (adapter->msix_mem) {
   5337   1.1    dyoung 		mask = IXGBE_EIMS_ENABLE_MASK;
   5338   1.1    dyoung 		/* Don't autoclear Link */
   5339   1.1    dyoung 		mask &= ~IXGBE_EIMS_OTHER;
   5340   1.1    dyoung 		mask &= ~IXGBE_EIMS_LSC;
   5341   1.1    dyoung 		IXGBE_WRITE_REG(hw, IXGBE_EIAC, mask);
   5342   1.1    dyoung 	}
   5343   1.1    dyoung 
   5344   1.1    dyoung 	/*
   5345   1.1    dyoung 	** Now enable all queues, this is done separately to
   5346   1.1    dyoung 	** allow for handling the extended (beyond 32) MSIX
   5347   1.1    dyoung 	** vectors that can be used by 82599
   5348   1.1    dyoung 	*/
   5349   1.1    dyoung         for (int i = 0; i < adapter->num_queues; i++, que++)
   5350   1.1    dyoung                 ixgbe_enable_queue(adapter, que->msix);
   5351   1.1    dyoung 
   5352   1.1    dyoung 	IXGBE_WRITE_FLUSH(hw);
   5353   1.1    dyoung 
   5354   1.1    dyoung 	return;
   5355   1.1    dyoung }
   5356   1.1    dyoung 
   5357   1.1    dyoung static void
   5358   1.1    dyoung ixgbe_disable_intr(struct adapter *adapter)
   5359   1.1    dyoung {
   5360   1.1    dyoung 	if (adapter->msix_mem)
   5361   1.1    dyoung 		IXGBE_WRITE_REG(&adapter->hw, IXGBE_EIAC, 0);
   5362   1.1    dyoung 	if (adapter->hw.mac.type == ixgbe_mac_82598EB) {
   5363   1.1    dyoung 		IXGBE_WRITE_REG(&adapter->hw, IXGBE_EIMC, ~0);
   5364   1.1    dyoung 	} else {
   5365   1.1    dyoung 		IXGBE_WRITE_REG(&adapter->hw, IXGBE_EIMC, 0xFFFF0000);
   5366   1.1    dyoung 		IXGBE_WRITE_REG(&adapter->hw, IXGBE_EIMC_EX(0), ~0);
   5367   1.1    dyoung 		IXGBE_WRITE_REG(&adapter->hw, IXGBE_EIMC_EX(1), ~0);
   5368   1.1    dyoung 	}
   5369   1.1    dyoung 	IXGBE_WRITE_FLUSH(&adapter->hw);
   5370   1.1    dyoung 	return;
   5371   1.1    dyoung }
   5372   1.1    dyoung 
   5373   1.1    dyoung u16
   5374   1.1    dyoung ixgbe_read_pci_cfg(struct ixgbe_hw *hw, u32 reg)
   5375   1.1    dyoung {
   5376   1.1    dyoung 	switch (reg % 4) {
   5377   1.1    dyoung 	case 0:
   5378   1.1    dyoung 		return pci_conf_read(hw->back->pc, hw->back->tag, reg) &
   5379   1.1    dyoung 		    __BITS(15, 0);
   5380   1.1    dyoung 	case 2:
   5381   1.1    dyoung 		return __SHIFTOUT(pci_conf_read(hw->back->pc, hw->back->tag,
   5382   1.1    dyoung 		    reg - 2), __BITS(31, 16));
   5383   1.1    dyoung 	default:
   5384   1.1    dyoung 		panic("%s: invalid register (%" PRIx32, __func__, reg);
   5385   1.1    dyoung 		break;
   5386   1.1    dyoung 	}
   5387   1.1    dyoung }
   5388   1.1    dyoung 
   5389   1.1    dyoung void
   5390   1.1    dyoung ixgbe_write_pci_cfg(struct ixgbe_hw *hw, u32 reg, u16 value)
   5391   1.1    dyoung {
   5392   1.1    dyoung 	pcireg_t old;
   5393   1.1    dyoung 
   5394   1.1    dyoung 	switch (reg % 4) {
   5395   1.1    dyoung 	case 0:
   5396   1.1    dyoung 		old = pci_conf_read(hw->back->pc, hw->back->tag, reg) &
   5397   1.1    dyoung 		    __BITS(31, 16);
   5398   1.1    dyoung 		pci_conf_write(hw->back->pc, hw->back->tag, reg, value | old);
   5399   1.1    dyoung 		break;
   5400   1.1    dyoung 	case 2:
   5401   1.1    dyoung 		old = pci_conf_read(hw->back->pc, hw->back->tag, reg - 2) &
   5402   1.1    dyoung 		    __BITS(15, 0);
   5403   1.1    dyoung 		pci_conf_write(hw->back->pc, hw->back->tag, reg - 2,
   5404   1.1    dyoung 		    __SHIFTIN(value, __BITS(31, 16)) | old);
   5405   1.1    dyoung 		break;
   5406   1.1    dyoung 	default:
   5407   1.1    dyoung 		panic("%s: invalid register (%" PRIx32, __func__, reg);
   5408   1.1    dyoung 		break;
   5409   1.1    dyoung 	}
   5410   1.1    dyoung 
   5411   1.1    dyoung 	return;
   5412   1.1    dyoung }
   5413   1.1    dyoung 
   5414   1.1    dyoung /*
   5415  1.33   msaitoh ** Get the width and transaction speed of
   5416  1.33   msaitoh ** the slot this adapter is plugged into.
   5417  1.33   msaitoh */
   5418  1.33   msaitoh static void
   5419  1.33   msaitoh ixgbe_get_slot_info(struct ixgbe_hw *hw)
   5420  1.33   msaitoh {
   5421  1.33   msaitoh 	device_t		dev = ((struct ixgbe_osdep *)hw->back)->dev;
   5422  1.33   msaitoh 	struct ixgbe_mac_info	*mac = &hw->mac;
   5423  1.33   msaitoh 	u16			link;
   5424  1.33   msaitoh 
   5425  1.33   msaitoh 	/* For most devices simply call the shared code routine */
   5426  1.33   msaitoh 	if (hw->device_id != IXGBE_DEV_ID_82599_SFP_SF_QP) {
   5427  1.33   msaitoh 		ixgbe_get_bus_info(hw);
   5428  1.33   msaitoh 		goto display;
   5429  1.33   msaitoh 	}
   5430  1.33   msaitoh 
   5431  1.33   msaitoh 	/*
   5432  1.33   msaitoh 	** For the Quad port adapter we need to parse back
   5433  1.33   msaitoh 	** up the PCI tree to find the speed of the expansion
   5434  1.33   msaitoh 	** slot into which this adapter is plugged. A bit more work.
   5435  1.33   msaitoh 	*/
   5436  1.33   msaitoh 	dev = device_parent(device_parent(dev));
   5437  1.33   msaitoh #ifdef IXGBE_DEBUG
   5438  1.33   msaitoh 	device_printf(dev, "parent pcib = %x,%x,%x\n",
   5439  1.33   msaitoh 	    pci_get_bus(dev), pci_get_slot(dev), pci_get_function(dev));
   5440  1.33   msaitoh #endif
   5441  1.33   msaitoh 	dev = device_parent(device_parent(dev));
   5442  1.33   msaitoh #ifdef IXGBE_DEBUG
   5443  1.33   msaitoh 	device_printf(dev, "slot pcib = %x,%x,%x\n",
   5444  1.33   msaitoh 	    pci_get_bus(dev), pci_get_slot(dev), pci_get_function(dev));
   5445  1.33   msaitoh #endif
   5446  1.33   msaitoh 	/* Now get the PCI Express Capabilities offset */
   5447  1.33   msaitoh 	/* ...and read the Link Status Register */
   5448  1.33   msaitoh 	link = IXGBE_READ_PCIE_WORD(hw, IXGBE_PCI_LINK_STATUS);
   5449  1.33   msaitoh 	switch (link & IXGBE_PCI_LINK_WIDTH) {
   5450  1.33   msaitoh 	case IXGBE_PCI_LINK_WIDTH_1:
   5451  1.33   msaitoh 		hw->bus.width = ixgbe_bus_width_pcie_x1;
   5452  1.33   msaitoh 		break;
   5453  1.33   msaitoh 	case IXGBE_PCI_LINK_WIDTH_2:
   5454  1.33   msaitoh 		hw->bus.width = ixgbe_bus_width_pcie_x2;
   5455  1.33   msaitoh 		break;
   5456  1.33   msaitoh 	case IXGBE_PCI_LINK_WIDTH_4:
   5457  1.33   msaitoh 		hw->bus.width = ixgbe_bus_width_pcie_x4;
   5458  1.33   msaitoh 		break;
   5459  1.33   msaitoh 	case IXGBE_PCI_LINK_WIDTH_8:
   5460  1.33   msaitoh 		hw->bus.width = ixgbe_bus_width_pcie_x8;
   5461  1.33   msaitoh 		break;
   5462  1.33   msaitoh 	default:
   5463  1.33   msaitoh 		hw->bus.width = ixgbe_bus_width_unknown;
   5464  1.33   msaitoh 		break;
   5465  1.33   msaitoh 	}
   5466  1.33   msaitoh 
   5467  1.33   msaitoh 	switch (link & IXGBE_PCI_LINK_SPEED) {
   5468  1.33   msaitoh 	case IXGBE_PCI_LINK_SPEED_2500:
   5469  1.33   msaitoh 		hw->bus.speed = ixgbe_bus_speed_2500;
   5470  1.33   msaitoh 		break;
   5471  1.33   msaitoh 	case IXGBE_PCI_LINK_SPEED_5000:
   5472  1.33   msaitoh 		hw->bus.speed = ixgbe_bus_speed_5000;
   5473  1.33   msaitoh 		break;
   5474  1.33   msaitoh 	case IXGBE_PCI_LINK_SPEED_8000:
   5475  1.33   msaitoh 		hw->bus.speed = ixgbe_bus_speed_8000;
   5476  1.33   msaitoh 		break;
   5477  1.33   msaitoh 	default:
   5478  1.33   msaitoh 		hw->bus.speed = ixgbe_bus_speed_unknown;
   5479  1.33   msaitoh 		break;
   5480  1.33   msaitoh 	}
   5481  1.33   msaitoh 
   5482  1.33   msaitoh 	mac->ops.set_lan_id(hw);
   5483  1.33   msaitoh 
   5484  1.33   msaitoh display:
   5485  1.33   msaitoh 	device_printf(dev,"PCI Express Bus: Speed %s %s\n",
   5486  1.33   msaitoh 	    ((hw->bus.speed == ixgbe_bus_speed_8000) ? "8.0GT/s":
   5487  1.33   msaitoh 	    (hw->bus.speed == ixgbe_bus_speed_5000) ? "5.0GT/s":
   5488  1.33   msaitoh 	    (hw->bus.speed == ixgbe_bus_speed_2500) ? "2.5GT/s":"Unknown"),
   5489  1.33   msaitoh 	    (hw->bus.width == ixgbe_bus_width_pcie_x8) ? "Width x8" :
   5490  1.33   msaitoh 	    (hw->bus.width == ixgbe_bus_width_pcie_x4) ? "Width x4" :
   5491  1.33   msaitoh 	    (hw->bus.width == ixgbe_bus_width_pcie_x1) ? "Width x1" :
   5492  1.33   msaitoh 	    ("Unknown"));
   5493  1.33   msaitoh 
   5494  1.33   msaitoh 	if ((hw->device_id != IXGBE_DEV_ID_82599_SFP_SF_QP) &&
   5495  1.33   msaitoh 	    ((hw->bus.width <= ixgbe_bus_width_pcie_x4) &&
   5496  1.33   msaitoh 	    (hw->bus.speed == ixgbe_bus_speed_2500))) {
   5497  1.33   msaitoh 		device_printf(dev, "PCI-Express bandwidth available"
   5498  1.33   msaitoh 		    " for this card\n     is not sufficient for"
   5499  1.33   msaitoh 		    " optimal performance.\n");
   5500  1.33   msaitoh 		device_printf(dev, "For optimal performance a x8 "
   5501  1.33   msaitoh 		    "PCIE, or x4 PCIE Gen2 slot is required.\n");
   5502  1.33   msaitoh         }
   5503  1.33   msaitoh 	if ((hw->device_id == IXGBE_DEV_ID_82599_SFP_SF_QP) &&
   5504  1.33   msaitoh 	    ((hw->bus.width <= ixgbe_bus_width_pcie_x8) &&
   5505  1.33   msaitoh 	    (hw->bus.speed < ixgbe_bus_speed_8000))) {
   5506  1.33   msaitoh 		device_printf(dev, "PCI-Express bandwidth available"
   5507  1.33   msaitoh 		    " for this card\n     is not sufficient for"
   5508  1.33   msaitoh 		    " optimal performance.\n");
   5509  1.33   msaitoh 		device_printf(dev, "For optimal performance a x8 "
   5510  1.33   msaitoh 		    "PCIE Gen3 slot is required.\n");
   5511  1.33   msaitoh         }
   5512  1.33   msaitoh 
   5513  1.33   msaitoh 	return;
   5514  1.33   msaitoh }
   5515  1.33   msaitoh 
   5516  1.33   msaitoh 
   5517  1.33   msaitoh /*
   5518   1.1    dyoung ** Setup the correct IVAR register for a particular MSIX interrupt
   5519   1.1    dyoung **   (yes this is all very magic and confusing :)
   5520   1.1    dyoung **  - entry is the register array entry
   5521   1.1    dyoung **  - vector is the MSIX vector for this queue
   5522   1.1    dyoung **  - type is RX/TX/MISC
   5523   1.1    dyoung */
   5524   1.1    dyoung static void
   5525   1.1    dyoung ixgbe_set_ivar(struct adapter *adapter, u8 entry, u8 vector, s8 type)
   5526   1.1    dyoung {
   5527   1.1    dyoung 	struct ixgbe_hw *hw = &adapter->hw;
   5528   1.1    dyoung 	u32 ivar, index;
   5529   1.1    dyoung 
   5530   1.1    dyoung 	vector |= IXGBE_IVAR_ALLOC_VAL;
   5531   1.1    dyoung 
   5532   1.1    dyoung 	switch (hw->mac.type) {
   5533   1.1    dyoung 
   5534   1.1    dyoung 	case ixgbe_mac_82598EB:
   5535   1.1    dyoung 		if (type == -1)
   5536   1.1    dyoung 			entry = IXGBE_IVAR_OTHER_CAUSES_INDEX;
   5537   1.1    dyoung 		else
   5538   1.1    dyoung 			entry += (type * 64);
   5539   1.1    dyoung 		index = (entry >> 2) & 0x1F;
   5540   1.1    dyoung 		ivar = IXGBE_READ_REG(hw, IXGBE_IVAR(index));
   5541   1.1    dyoung 		ivar &= ~(0xFF << (8 * (entry & 0x3)));
   5542   1.1    dyoung 		ivar |= (vector << (8 * (entry & 0x3)));
   5543   1.1    dyoung 		IXGBE_WRITE_REG(&adapter->hw, IXGBE_IVAR(index), ivar);
   5544   1.1    dyoung 		break;
   5545   1.1    dyoung 
   5546   1.1    dyoung 	case ixgbe_mac_82599EB:
   5547  1.24   msaitoh 	case ixgbe_mac_X540:
   5548   1.1    dyoung 		if (type == -1) { /* MISC IVAR */
   5549   1.1    dyoung 			index = (entry & 1) * 8;
   5550   1.1    dyoung 			ivar = IXGBE_READ_REG(hw, IXGBE_IVAR_MISC);
   5551   1.1    dyoung 			ivar &= ~(0xFF << index);
   5552   1.1    dyoung 			ivar |= (vector << index);
   5553   1.1    dyoung 			IXGBE_WRITE_REG(hw, IXGBE_IVAR_MISC, ivar);
   5554   1.1    dyoung 		} else {	/* RX/TX IVARS */
   5555   1.1    dyoung 			index = (16 * (entry & 1)) + (8 * type);
   5556   1.1    dyoung 			ivar = IXGBE_READ_REG(hw, IXGBE_IVAR(entry >> 1));
   5557   1.1    dyoung 			ivar &= ~(0xFF << index);
   5558   1.1    dyoung 			ivar |= (vector << index);
   5559   1.1    dyoung 			IXGBE_WRITE_REG(hw, IXGBE_IVAR(entry >> 1), ivar);
   5560   1.1    dyoung 		}
   5561   1.1    dyoung 
   5562   1.1    dyoung 	default:
   5563   1.1    dyoung 		break;
   5564   1.1    dyoung 	}
   5565   1.1    dyoung }
   5566   1.1    dyoung 
   5567   1.1    dyoung static void
   5568   1.1    dyoung ixgbe_configure_ivars(struct adapter *adapter)
   5569   1.1    dyoung {
   5570   1.1    dyoung 	struct  ix_queue *que = adapter->queues;
   5571   1.1    dyoung 	u32 newitr;
   5572   1.1    dyoung 
   5573   1.1    dyoung 	if (ixgbe_max_interrupt_rate > 0)
   5574  1.22   msaitoh 		newitr = (4000000 / ixgbe_max_interrupt_rate) & 0x0FF8;
   5575   1.1    dyoung 	else
   5576   1.1    dyoung 		newitr = 0;
   5577   1.1    dyoung 
   5578   1.1    dyoung         for (int i = 0; i < adapter->num_queues; i++, que++) {
   5579   1.1    dyoung 		/* First the RX queue entry */
   5580   1.1    dyoung                 ixgbe_set_ivar(adapter, i, que->msix, 0);
   5581   1.1    dyoung 		/* ... and the TX */
   5582   1.1    dyoung 		ixgbe_set_ivar(adapter, i, que->msix, 1);
   5583   1.1    dyoung 		/* Set an Initial EITR value */
   5584   1.1    dyoung                 IXGBE_WRITE_REG(&adapter->hw,
   5585   1.1    dyoung                     IXGBE_EITR(que->msix), newitr);
   5586   1.1    dyoung 	}
   5587   1.1    dyoung 
   5588   1.1    dyoung 	/* For the Link interrupt */
   5589   1.1    dyoung         ixgbe_set_ivar(adapter, 1, adapter->linkvec, -1);
   5590   1.1    dyoung }
   5591   1.1    dyoung 
   5592   1.1    dyoung /*
   5593   1.1    dyoung ** ixgbe_sfp_probe - called in the local timer to
   5594   1.1    dyoung ** determine if a port had optics inserted.
   5595   1.1    dyoung */
   5596   1.1    dyoung static bool ixgbe_sfp_probe(struct adapter *adapter)
   5597   1.1    dyoung {
   5598   1.1    dyoung 	struct ixgbe_hw	*hw = &adapter->hw;
   5599   1.1    dyoung 	device_t	dev = adapter->dev;
   5600   1.1    dyoung 	bool		result = FALSE;
   5601   1.1    dyoung 
   5602   1.1    dyoung 	if ((hw->phy.type == ixgbe_phy_nl) &&
   5603   1.1    dyoung 	    (hw->phy.sfp_type == ixgbe_sfp_type_not_present)) {
   5604   1.1    dyoung 		s32 ret = hw->phy.ops.identify_sfp(hw);
   5605   1.1    dyoung 		if (ret)
   5606   1.1    dyoung                         goto out;
   5607   1.1    dyoung 		ret = hw->phy.ops.reset(hw);
   5608   1.1    dyoung 		if (ret == IXGBE_ERR_SFP_NOT_SUPPORTED) {
   5609   1.1    dyoung 			device_printf(dev,"Unsupported SFP+ module detected!");
   5610   1.1    dyoung 			device_printf(dev, "Reload driver with supported module.\n");
   5611   1.1    dyoung 			adapter->sfp_probe = FALSE;
   5612   1.1    dyoung                         goto out;
   5613   1.1    dyoung 		} else
   5614   1.1    dyoung 			device_printf(dev,"SFP+ module detected!\n");
   5615   1.1    dyoung 		/* We now have supported optics */
   5616   1.1    dyoung 		adapter->sfp_probe = FALSE;
   5617   1.1    dyoung 		/* Set the optics type so system reports correctly */
   5618   1.1    dyoung 		ixgbe_setup_optics(adapter);
   5619   1.1    dyoung 		result = TRUE;
   5620   1.1    dyoung 	}
   5621   1.1    dyoung out:
   5622   1.1    dyoung 	return (result);
   5623   1.1    dyoung }
   5624   1.1    dyoung 
   5625   1.1    dyoung /*
   5626   1.1    dyoung ** Tasklet handler for MSIX Link interrupts
   5627   1.1    dyoung **  - do outside interrupt since it might sleep
   5628   1.1    dyoung */
   5629   1.1    dyoung static void
   5630   1.1    dyoung ixgbe_handle_link(void *context)
   5631   1.1    dyoung {
   5632   1.1    dyoung 	struct adapter  *adapter = context;
   5633   1.1    dyoung 
   5634  1.13  christos 	if (ixgbe_check_link(&adapter->hw,
   5635  1.13  christos 	    &adapter->link_speed, &adapter->link_up, 0) == 0)
   5636  1.13  christos 	    ixgbe_update_link_status(adapter);
   5637   1.1    dyoung }
   5638   1.1    dyoung 
   5639   1.1    dyoung /*
   5640   1.1    dyoung ** Tasklet for handling SFP module interrupts
   5641   1.1    dyoung */
   5642   1.1    dyoung static void
   5643   1.1    dyoung ixgbe_handle_mod(void *context)
   5644   1.1    dyoung {
   5645   1.1    dyoung 	struct adapter  *adapter = context;
   5646   1.1    dyoung 	struct ixgbe_hw *hw = &adapter->hw;
   5647   1.1    dyoung 	device_t	dev = adapter->dev;
   5648   1.1    dyoung 	u32 err;
   5649   1.1    dyoung 
   5650   1.1    dyoung 	err = hw->phy.ops.identify_sfp(hw);
   5651   1.1    dyoung 	if (err == IXGBE_ERR_SFP_NOT_SUPPORTED) {
   5652   1.1    dyoung 		device_printf(dev,
   5653   1.1    dyoung 		    "Unsupported SFP+ module type was detected.\n");
   5654   1.1    dyoung 		return;
   5655   1.1    dyoung 	}
   5656   1.1    dyoung 	err = hw->mac.ops.setup_sfp(hw);
   5657   1.1    dyoung 	if (err == IXGBE_ERR_SFP_NOT_SUPPORTED) {
   5658   1.1    dyoung 		device_printf(dev,
   5659   1.1    dyoung 		    "Setup failure - unsupported SFP+ module type.\n");
   5660   1.1    dyoung 		return;
   5661   1.1    dyoung 	}
   5662   1.1    dyoung 	softint_schedule(adapter->msf_si);
   5663   1.1    dyoung 	return;
   5664   1.1    dyoung }
   5665   1.1    dyoung 
   5666   1.1    dyoung 
   5667   1.1    dyoung /*
   5668   1.1    dyoung ** Tasklet for handling MSF (multispeed fiber) interrupts
   5669   1.1    dyoung */
   5670   1.1    dyoung static void
   5671   1.1    dyoung ixgbe_handle_msf(void *context)
   5672   1.1    dyoung {
   5673   1.1    dyoung 	struct adapter  *adapter = context;
   5674   1.1    dyoung 	struct ixgbe_hw *hw = &adapter->hw;
   5675   1.1    dyoung 	u32 autoneg;
   5676   1.1    dyoung 	bool negotiate;
   5677   1.1    dyoung 
   5678   1.1    dyoung 	autoneg = hw->phy.autoneg_advertised;
   5679   1.1    dyoung 	if ((!autoneg) && (hw->mac.ops.get_link_capabilities))
   5680   1.1    dyoung 		hw->mac.ops.get_link_capabilities(hw, &autoneg, &negotiate);
   5681  1.13  christos 	else
   5682  1.13  christos 		negotiate = 0;
   5683   1.1    dyoung 	if (hw->mac.ops.setup_link)
   5684  1.28   msaitoh 		hw->mac.ops.setup_link(hw, autoneg, TRUE);
   5685   1.1    dyoung 	return;
   5686   1.1    dyoung }
   5687   1.1    dyoung 
   5688   1.1    dyoung #ifdef IXGBE_FDIR
   5689   1.1    dyoung /*
   5690   1.1    dyoung ** Tasklet for reinitializing the Flow Director filter table
   5691   1.1    dyoung */
   5692   1.1    dyoung static void
   5693   1.1    dyoung ixgbe_reinit_fdir(void *context)
   5694   1.1    dyoung {
   5695   1.1    dyoung 	struct adapter  *adapter = context;
   5696   1.1    dyoung 	struct ifnet   *ifp = adapter->ifp;
   5697   1.1    dyoung 
   5698   1.1    dyoung 	if (adapter->fdir_reinit != 1) /* Shouldn't happen */
   5699   1.1    dyoung 		return;
   5700   1.1    dyoung 	ixgbe_reinit_fdir_tables_82599(&adapter->hw);
   5701   1.1    dyoung 	adapter->fdir_reinit = 0;
   5702  1.25   msaitoh 	/* re-enable flow director interrupts */
   5703  1.25   msaitoh 	IXGBE_WRITE_REG(&adapter->hw, IXGBE_EIMS, IXGBE_EIMS_FLOW_DIR);
   5704   1.1    dyoung 	/* Restart the interface */
   5705   1.1    dyoung 	ifp->if_flags |= IFF_RUNNING;
   5706   1.1    dyoung 	return;
   5707   1.1    dyoung }
   5708   1.1    dyoung #endif
   5709   1.1    dyoung 
   5710   1.1    dyoung /**********************************************************************
   5711   1.1    dyoung  *
   5712   1.1    dyoung  *  Update the board statistics counters.
   5713   1.1    dyoung  *
   5714   1.1    dyoung  **********************************************************************/
   5715   1.1    dyoung static void
   5716   1.1    dyoung ixgbe_update_stats_counters(struct adapter *adapter)
   5717   1.1    dyoung {
   5718   1.1    dyoung 	struct ifnet   *ifp = adapter->ifp;
   5719   1.1    dyoung 	struct ixgbe_hw *hw = &adapter->hw;
   5720   1.1    dyoung 	u32  missed_rx = 0, bprc, lxon, lxoff, total;
   5721   1.1    dyoung 	u64  total_missed_rx = 0;
   5722  1.27   msaitoh 	uint64_t crcerrs, rlec;
   5723   1.1    dyoung 
   5724  1.27   msaitoh 	crcerrs = IXGBE_READ_REG(hw, IXGBE_CRCERRS);
   5725  1.27   msaitoh 	adapter->stats.crcerrs.ev_count += crcerrs;
   5726   1.1    dyoung 	adapter->stats.illerrc.ev_count += IXGBE_READ_REG(hw, IXGBE_ILLERRC);
   5727   1.1    dyoung 	adapter->stats.errbc.ev_count += IXGBE_READ_REG(hw, IXGBE_ERRBC);
   5728   1.1    dyoung 	adapter->stats.mspdc.ev_count += IXGBE_READ_REG(hw, IXGBE_MSPDC);
   5729   1.1    dyoung 
   5730  1.28   msaitoh 	/*
   5731  1.28   msaitoh 	** Note: these are for the 8 possible traffic classes,
   5732  1.28   msaitoh 	**	 which in current implementation is unused,
   5733  1.28   msaitoh 	**	 therefore only 0 should read real data.
   5734  1.28   msaitoh 	*/
   5735   1.1    dyoung 	for (int i = 0; i < __arraycount(adapter->stats.mpc); i++) {
   5736   1.1    dyoung 		int j = i % adapter->num_queues;
   5737   1.1    dyoung 		u32 mp;
   5738   1.1    dyoung 		mp = IXGBE_READ_REG(hw, IXGBE_MPC(i));
   5739   1.1    dyoung 		/* missed_rx tallies misses for the gprc workaround */
   5740   1.1    dyoung 		missed_rx += mp;
   5741   1.1    dyoung 		/* global total per queue */
   5742   1.1    dyoung         	adapter->stats.mpc[j].ev_count += mp;
   5743   1.1    dyoung 		/* Running comprehensive total for stats display */
   5744  1.27   msaitoh 		total_missed_rx += mp;
   5745  1.28   msaitoh 		if (hw->mac.type == ixgbe_mac_82598EB) {
   5746   1.1    dyoung 			adapter->stats.rnbc[j] +=
   5747   1.1    dyoung 			    IXGBE_READ_REG(hw, IXGBE_RNBC(i));
   5748  1.28   msaitoh 			adapter->stats.qbtc[j].ev_count +=
   5749  1.28   msaitoh 			    IXGBE_READ_REG(hw, IXGBE_QBTC(i));
   5750  1.28   msaitoh 			adapter->stats.qbrc[j].ev_count +=
   5751  1.28   msaitoh 			    IXGBE_READ_REG(hw, IXGBE_QBRC(i));
   5752  1.28   msaitoh 			adapter->stats.pxonrxc[j].ev_count +=
   5753  1.28   msaitoh 			    IXGBE_READ_REG(hw, IXGBE_PXONRXC(i));
   5754  1.28   msaitoh 		} else {
   5755  1.28   msaitoh 			adapter->stats.pxonrxc[j].ev_count +=
   5756  1.28   msaitoh 			    IXGBE_READ_REG(hw, IXGBE_PXONRXCNT(i));
   5757  1.28   msaitoh 		}
   5758   1.1    dyoung 		adapter->stats.pxontxc[j].ev_count +=
   5759   1.1    dyoung 		    IXGBE_READ_REG(hw, IXGBE_PXONTXC(i));
   5760   1.1    dyoung 		adapter->stats.pxofftxc[j].ev_count +=
   5761   1.1    dyoung 		    IXGBE_READ_REG(hw, IXGBE_PXOFFTXC(i));
   5762   1.1    dyoung 		adapter->stats.pxoffrxc[j].ev_count +=
   5763   1.1    dyoung 		    IXGBE_READ_REG(hw, IXGBE_PXOFFRXC(i));
   5764   1.1    dyoung 		adapter->stats.pxon2offc[j].ev_count +=
   5765   1.1    dyoung 		    IXGBE_READ_REG(hw, IXGBE_PXON2OFFCNT(i));
   5766   1.1    dyoung 	}
   5767   1.1    dyoung 	for (int i = 0; i < __arraycount(adapter->stats.qprc); i++) {
   5768   1.1    dyoung 		int j = i % adapter->num_queues;
   5769   1.1    dyoung 		adapter->stats.qprc[j].ev_count += IXGBE_READ_REG(hw, IXGBE_QPRC(i));
   5770   1.1    dyoung 		adapter->stats.qptc[j].ev_count += IXGBE_READ_REG(hw, IXGBE_QPTC(i));
   5771   1.1    dyoung 		adapter->stats.qprdc[j].ev_count += IXGBE_READ_REG(hw, IXGBE_QPRDC(i));
   5772   1.1    dyoung 	}
   5773   1.1    dyoung 	adapter->stats.mlfc.ev_count += IXGBE_READ_REG(hw, IXGBE_MLFC);
   5774   1.1    dyoung 	adapter->stats.mrfc.ev_count += IXGBE_READ_REG(hw, IXGBE_MRFC);
   5775  1.27   msaitoh 	rlec = IXGBE_READ_REG(hw, IXGBE_RLEC);
   5776  1.27   msaitoh 	adapter->stats.rlec.ev_count += rlec;
   5777   1.1    dyoung 
   5778   1.1    dyoung 	/* Hardware workaround, gprc counts missed packets */
   5779   1.1    dyoung 	adapter->stats.gprc.ev_count += IXGBE_READ_REG(hw, IXGBE_GPRC) - missed_rx;
   5780   1.1    dyoung 
   5781   1.1    dyoung 	lxon = IXGBE_READ_REG(hw, IXGBE_LXONTXC);
   5782   1.1    dyoung 	adapter->stats.lxontxc.ev_count += lxon;
   5783   1.1    dyoung 	lxoff = IXGBE_READ_REG(hw, IXGBE_LXOFFTXC);
   5784   1.1    dyoung 	adapter->stats.lxofftxc.ev_count += lxoff;
   5785   1.1    dyoung 	total = lxon + lxoff;
   5786   1.1    dyoung 
   5787   1.1    dyoung 	if (hw->mac.type != ixgbe_mac_82598EB) {
   5788   1.1    dyoung 		adapter->stats.gorc.ev_count += IXGBE_READ_REG(hw, IXGBE_GORCL) +
   5789   1.1    dyoung 		    ((u64)IXGBE_READ_REG(hw, IXGBE_GORCH) << 32);
   5790   1.1    dyoung 		adapter->stats.gotc.ev_count += IXGBE_READ_REG(hw, IXGBE_GOTCL) +
   5791   1.1    dyoung 		    ((u64)IXGBE_READ_REG(hw, IXGBE_GOTCH) << 32) - total * ETHER_MIN_LEN;
   5792   1.1    dyoung 		adapter->stats.tor.ev_count += IXGBE_READ_REG(hw, IXGBE_TORL) +
   5793   1.1    dyoung 		    ((u64)IXGBE_READ_REG(hw, IXGBE_TORH) << 32);
   5794   1.1    dyoung 		adapter->stats.lxonrxc.ev_count += IXGBE_READ_REG(hw, IXGBE_LXONRXCNT);
   5795   1.1    dyoung 		adapter->stats.lxoffrxc.ev_count += IXGBE_READ_REG(hw, IXGBE_LXOFFRXCNT);
   5796   1.1    dyoung 	} else {
   5797   1.1    dyoung 		adapter->stats.lxonrxc.ev_count += IXGBE_READ_REG(hw, IXGBE_LXONRXC);
   5798   1.1    dyoung 		adapter->stats.lxoffrxc.ev_count += IXGBE_READ_REG(hw, IXGBE_LXOFFRXC);
   5799   1.1    dyoung 		/* 82598 only has a counter in the high register */
   5800   1.1    dyoung 		adapter->stats.gorc.ev_count += IXGBE_READ_REG(hw, IXGBE_GORCH);
   5801   1.1    dyoung 		adapter->stats.gotc.ev_count += IXGBE_READ_REG(hw, IXGBE_GOTCH) - total * ETHER_MIN_LEN;
   5802   1.1    dyoung 		adapter->stats.tor.ev_count += IXGBE_READ_REG(hw, IXGBE_TORH);
   5803   1.1    dyoung 	}
   5804   1.1    dyoung 
   5805   1.1    dyoung 	/*
   5806   1.1    dyoung 	 * Workaround: mprc hardware is incorrectly counting
   5807   1.1    dyoung 	 * broadcasts, so for now we subtract those.
   5808   1.1    dyoung 	 */
   5809   1.1    dyoung 	bprc = IXGBE_READ_REG(hw, IXGBE_BPRC);
   5810   1.1    dyoung 	adapter->stats.bprc.ev_count += bprc;
   5811   1.1    dyoung 	adapter->stats.mprc.ev_count += IXGBE_READ_REG(hw, IXGBE_MPRC) - ((hw->mac.type == ixgbe_mac_82598EB) ? bprc : 0);
   5812   1.1    dyoung 
   5813   1.1    dyoung 	adapter->stats.prc64.ev_count += IXGBE_READ_REG(hw, IXGBE_PRC64);
   5814   1.1    dyoung 	adapter->stats.prc127.ev_count += IXGBE_READ_REG(hw, IXGBE_PRC127);
   5815   1.1    dyoung 	adapter->stats.prc255.ev_count += IXGBE_READ_REG(hw, IXGBE_PRC255);
   5816   1.1    dyoung 	adapter->stats.prc511.ev_count += IXGBE_READ_REG(hw, IXGBE_PRC511);
   5817   1.1    dyoung 	adapter->stats.prc1023.ev_count += IXGBE_READ_REG(hw, IXGBE_PRC1023);
   5818   1.1    dyoung 	adapter->stats.prc1522.ev_count += IXGBE_READ_REG(hw, IXGBE_PRC1522);
   5819   1.1    dyoung 
   5820   1.1    dyoung 	adapter->stats.gptc.ev_count += IXGBE_READ_REG(hw, IXGBE_GPTC) - total;
   5821   1.1    dyoung 	adapter->stats.mptc.ev_count += IXGBE_READ_REG(hw, IXGBE_MPTC) - total;
   5822   1.1    dyoung 	adapter->stats.ptc64.ev_count += IXGBE_READ_REG(hw, IXGBE_PTC64) - total;
   5823   1.1    dyoung 
   5824   1.1    dyoung 	adapter->stats.ruc.ev_count += IXGBE_READ_REG(hw, IXGBE_RUC);
   5825   1.1    dyoung 	adapter->stats.rfc.ev_count += IXGBE_READ_REG(hw, IXGBE_RFC);
   5826   1.1    dyoung 	adapter->stats.roc.ev_count += IXGBE_READ_REG(hw, IXGBE_ROC);
   5827   1.1    dyoung 	adapter->stats.rjc.ev_count += IXGBE_READ_REG(hw, IXGBE_RJC);
   5828   1.1    dyoung 	adapter->stats.mngprc.ev_count += IXGBE_READ_REG(hw, IXGBE_MNGPRC);
   5829   1.1    dyoung 	adapter->stats.mngpdc.ev_count += IXGBE_READ_REG(hw, IXGBE_MNGPDC);
   5830   1.1    dyoung 	adapter->stats.mngptc.ev_count += IXGBE_READ_REG(hw, IXGBE_MNGPTC);
   5831   1.1    dyoung 	adapter->stats.tpr.ev_count += IXGBE_READ_REG(hw, IXGBE_TPR);
   5832   1.1    dyoung 	adapter->stats.tpt.ev_count += IXGBE_READ_REG(hw, IXGBE_TPT);
   5833   1.1    dyoung 	adapter->stats.ptc127.ev_count += IXGBE_READ_REG(hw, IXGBE_PTC127);
   5834   1.1    dyoung 	adapter->stats.ptc255.ev_count += IXGBE_READ_REG(hw, IXGBE_PTC255);
   5835   1.1    dyoung 	adapter->stats.ptc511.ev_count += IXGBE_READ_REG(hw, IXGBE_PTC511);
   5836   1.1    dyoung 	adapter->stats.ptc1023.ev_count += IXGBE_READ_REG(hw, IXGBE_PTC1023);
   5837   1.1    dyoung 	adapter->stats.ptc1522.ev_count += IXGBE_READ_REG(hw, IXGBE_PTC1522);
   5838   1.1    dyoung 	adapter->stats.bptc.ev_count += IXGBE_READ_REG(hw, IXGBE_BPTC);
   5839   1.1    dyoung 	adapter->stats.xec.ev_count += IXGBE_READ_REG(hw, IXGBE_XEC);
   5840   1.1    dyoung 	adapter->stats.fccrc.ev_count += IXGBE_READ_REG(hw, IXGBE_FCCRC);
   5841   1.1    dyoung 	adapter->stats.fclast.ev_count += IXGBE_READ_REG(hw, IXGBE_FCLAST);
   5842   1.1    dyoung 
   5843   1.1    dyoung 	/* Only read FCOE on 82599 */
   5844   1.1    dyoung 	if (hw->mac.type != ixgbe_mac_82598EB) {
   5845   1.1    dyoung 		adapter->stats.fcoerpdc.ev_count +=
   5846   1.1    dyoung 		    IXGBE_READ_REG(hw, IXGBE_FCOERPDC);
   5847   1.1    dyoung 		adapter->stats.fcoeprc.ev_count +=
   5848   1.1    dyoung 		    IXGBE_READ_REG(hw, IXGBE_FCOEPRC);
   5849   1.1    dyoung 		adapter->stats.fcoeptc.ev_count +=
   5850   1.1    dyoung 		    IXGBE_READ_REG(hw, IXGBE_FCOEPTC);
   5851   1.1    dyoung 		adapter->stats.fcoedwrc.ev_count +=
   5852   1.1    dyoung 		    IXGBE_READ_REG(hw, IXGBE_FCOEDWRC);
   5853   1.1    dyoung 		adapter->stats.fcoedwtc.ev_count +=
   5854   1.1    dyoung 		    IXGBE_READ_REG(hw, IXGBE_FCOEDWTC);
   5855   1.1    dyoung 	}
   5856   1.1    dyoung 
   5857   1.1    dyoung 	/* Fill out the OS statistics structure */
   5858  1.27   msaitoh 	/*
   5859  1.27   msaitoh 	 * NetBSD: Don't override if_{i|o}{packets|bytes|mcasts} with
   5860  1.27   msaitoh 	 * adapter->stats counters. It's required to make ifconfig -z
   5861  1.27   msaitoh 	 * (SOICZIFDATA) work.
   5862  1.27   msaitoh 	 */
   5863   1.1    dyoung 	ifp->if_collisions = 0;
   5864  1.28   msaitoh 
   5865   1.1    dyoung 	/* Rx Errors */
   5866  1.28   msaitoh 	ifp->if_iqdrops += total_missed_rx;
   5867  1.28   msaitoh 	ifp->if_ierrors += crcerrs + rlec;
   5868   1.1    dyoung }
   5869   1.1    dyoung 
   5870   1.1    dyoung /** ixgbe_sysctl_tdh_handler - Handler function
   5871   1.1    dyoung  *  Retrieves the TDH value from the hardware
   5872   1.1    dyoung  */
   5873   1.1    dyoung static int
   5874   1.1    dyoung ixgbe_sysctl_tdh_handler(SYSCTLFN_ARGS)
   5875   1.1    dyoung {
   5876   1.1    dyoung 	struct sysctlnode node;
   5877   1.1    dyoung 	uint32_t val;
   5878   1.1    dyoung 	struct tx_ring *txr;
   5879   1.1    dyoung 
   5880   1.1    dyoung 	node = *rnode;
   5881   1.1    dyoung 	txr = (struct tx_ring *)node.sysctl_data;
   5882   1.1    dyoung 	if (txr == NULL)
   5883   1.1    dyoung 		return 0;
   5884   1.1    dyoung 	val = IXGBE_READ_REG(&txr->adapter->hw, IXGBE_TDH(txr->me));
   5885   1.1    dyoung 	node.sysctl_data = &val;
   5886   1.1    dyoung 	return sysctl_lookup(SYSCTLFN_CALL(&node));
   5887   1.1    dyoung }
   5888   1.1    dyoung 
   5889   1.1    dyoung /** ixgbe_sysctl_tdt_handler - Handler function
   5890   1.1    dyoung  *  Retrieves the TDT value from the hardware
   5891   1.1    dyoung  */
   5892   1.1    dyoung static int
   5893   1.1    dyoung ixgbe_sysctl_tdt_handler(SYSCTLFN_ARGS)
   5894   1.1    dyoung {
   5895   1.1    dyoung 	struct sysctlnode node;
   5896   1.1    dyoung 	uint32_t val;
   5897   1.1    dyoung 	struct tx_ring *txr;
   5898   1.1    dyoung 
   5899   1.1    dyoung 	node = *rnode;
   5900   1.1    dyoung 	txr = (struct tx_ring *)node.sysctl_data;
   5901   1.1    dyoung 	if (txr == NULL)
   5902   1.1    dyoung 		return 0;
   5903   1.1    dyoung 	val = IXGBE_READ_REG(&txr->adapter->hw, IXGBE_TDT(txr->me));
   5904   1.1    dyoung 	node.sysctl_data = &val;
   5905   1.1    dyoung 	return sysctl_lookup(SYSCTLFN_CALL(&node));
   5906   1.1    dyoung }
   5907   1.1    dyoung 
   5908   1.1    dyoung /** ixgbe_sysctl_rdh_handler - Handler function
   5909   1.1    dyoung  *  Retrieves the RDH value from the hardware
   5910   1.1    dyoung  */
   5911   1.1    dyoung static int
   5912   1.1    dyoung ixgbe_sysctl_rdh_handler(SYSCTLFN_ARGS)
   5913   1.1    dyoung {
   5914   1.1    dyoung 	struct sysctlnode node;
   5915   1.1    dyoung 	uint32_t val;
   5916   1.1    dyoung 	struct rx_ring *rxr;
   5917   1.1    dyoung 
   5918   1.1    dyoung 	node = *rnode;
   5919   1.1    dyoung 	rxr = (struct rx_ring *)node.sysctl_data;
   5920   1.1    dyoung 	if (rxr == NULL)
   5921   1.1    dyoung 		return 0;
   5922   1.1    dyoung 	val = IXGBE_READ_REG(&rxr->adapter->hw, IXGBE_RDH(rxr->me));
   5923   1.1    dyoung 	node.sysctl_data = &val;
   5924   1.1    dyoung 	return sysctl_lookup(SYSCTLFN_CALL(&node));
   5925   1.1    dyoung }
   5926   1.1    dyoung 
   5927   1.1    dyoung /** ixgbe_sysctl_rdt_handler - Handler function
   5928   1.1    dyoung  *  Retrieves the RDT value from the hardware
   5929   1.1    dyoung  */
   5930   1.1    dyoung static int
   5931   1.1    dyoung ixgbe_sysctl_rdt_handler(SYSCTLFN_ARGS)
   5932   1.1    dyoung {
   5933   1.1    dyoung 	struct sysctlnode node;
   5934   1.1    dyoung 	uint32_t val;
   5935   1.1    dyoung 	struct rx_ring *rxr;
   5936   1.1    dyoung 
   5937   1.1    dyoung 	node = *rnode;
   5938   1.1    dyoung 	rxr = (struct rx_ring *)node.sysctl_data;
   5939   1.1    dyoung 	if (rxr == NULL)
   5940   1.1    dyoung 		return 0;
   5941   1.1    dyoung 	val = IXGBE_READ_REG(&rxr->adapter->hw, IXGBE_RDT(rxr->me));
   5942   1.1    dyoung 	node.sysctl_data = &val;
   5943   1.1    dyoung 	return sysctl_lookup(SYSCTLFN_CALL(&node));
   5944   1.1    dyoung }
   5945   1.1    dyoung 
   5946   1.1    dyoung static int
   5947   1.1    dyoung ixgbe_sysctl_interrupt_rate_handler(SYSCTLFN_ARGS)
   5948   1.1    dyoung {
   5949  1.22   msaitoh 	int error;
   5950   1.1    dyoung 	struct sysctlnode node;
   5951   1.1    dyoung 	struct ix_queue *que;
   5952   1.1    dyoung 	uint32_t reg, usec, rate;
   5953   1.1    dyoung 
   5954   1.1    dyoung 	node = *rnode;
   5955   1.1    dyoung 	que = (struct ix_queue *)node.sysctl_data;
   5956   1.1    dyoung 	if (que == NULL)
   5957   1.1    dyoung 		return 0;
   5958   1.1    dyoung 	reg = IXGBE_READ_REG(&que->adapter->hw, IXGBE_EITR(que->msix));
   5959   1.1    dyoung 	usec = ((reg & 0x0FF8) >> 3);
   5960   1.1    dyoung 	if (usec > 0)
   5961  1.22   msaitoh 		rate = 500000 / usec;
   5962   1.1    dyoung 	else
   5963   1.1    dyoung 		rate = 0;
   5964   1.1    dyoung 	node.sysctl_data = &rate;
   5965  1.22   msaitoh 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   5966  1.22   msaitoh 	if (error)
   5967  1.22   msaitoh 		return error;
   5968  1.22   msaitoh 	reg &= ~0xfff; /* default, no limitation */
   5969  1.22   msaitoh 	ixgbe_max_interrupt_rate = 0;
   5970  1.22   msaitoh 	if (rate > 0 && rate < 500000) {
   5971  1.22   msaitoh 		if (rate < 1000)
   5972  1.22   msaitoh 			rate = 1000;
   5973  1.22   msaitoh 		ixgbe_max_interrupt_rate = rate;
   5974  1.22   msaitoh 		reg |= ((4000000/rate) & 0xff8 );
   5975  1.22   msaitoh 	}
   5976  1.22   msaitoh 	IXGBE_WRITE_REG(&que->adapter->hw, IXGBE_EITR(que->msix), reg);
   5977  1.22   msaitoh 	return 0;
   5978   1.1    dyoung }
   5979   1.1    dyoung 
   5980   1.1    dyoung const struct sysctlnode *
   5981   1.1    dyoung ixgbe_sysctl_instance(struct adapter *adapter)
   5982   1.1    dyoung {
   5983   1.1    dyoung 	const char *dvname;
   5984   1.1    dyoung 	struct sysctllog **log;
   5985   1.1    dyoung 	int rc;
   5986   1.1    dyoung 	const struct sysctlnode *rnode;
   5987   1.1    dyoung 
   5988   1.1    dyoung 	log = &adapter->sysctllog;
   5989   1.1    dyoung 	dvname = device_xname(adapter->dev);
   5990   1.1    dyoung 
   5991   1.1    dyoung 	if ((rc = sysctl_createv(log, 0, NULL, &rnode,
   5992   1.1    dyoung 	    0, CTLTYPE_NODE, dvname,
   5993   1.1    dyoung 	    SYSCTL_DESCR("ixgbe information and settings"),
   5994   1.7     pooka 	    NULL, 0, NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL)) != 0)
   5995   1.1    dyoung 		goto err;
   5996   1.1    dyoung 
   5997   1.1    dyoung 	return rnode;
   5998   1.1    dyoung err:
   5999   1.1    dyoung 	printf("%s: sysctl_createv failed, rc = %d\n", __func__, rc);
   6000   1.1    dyoung 	return NULL;
   6001   1.1    dyoung }
   6002   1.1    dyoung 
   6003   1.1    dyoung /*
   6004   1.1    dyoung  * Add sysctl variables, one per statistic, to the system.
   6005   1.1    dyoung  */
   6006   1.1    dyoung static void
   6007   1.1    dyoung ixgbe_add_hw_stats(struct adapter *adapter)
   6008   1.1    dyoung {
   6009   1.1    dyoung 	device_t dev = adapter->dev;
   6010   1.1    dyoung 	const struct sysctlnode *rnode, *cnode;
   6011   1.1    dyoung 	struct sysctllog **log = &adapter->sysctllog;
   6012   1.1    dyoung 	struct tx_ring *txr = adapter->tx_rings;
   6013   1.1    dyoung 	struct rx_ring *rxr = adapter->rx_rings;
   6014   1.1    dyoung 	struct ixgbe_hw_stats *stats = &adapter->stats;
   6015   1.1    dyoung 
   6016   1.1    dyoung 	/* Driver Statistics */
   6017   1.1    dyoung #if 0
   6018   1.1    dyoung 	/* These counters are not updated by the software */
   6019   1.1    dyoung 	SYSCTL_ADD_ULONG(ctx, child, OID_AUTO, "dropped",
   6020   1.1    dyoung 			CTLFLAG_RD, &adapter->dropped_pkts,
   6021   1.1    dyoung 			"Driver dropped packets");
   6022   1.1    dyoung 	SYSCTL_ADD_ULONG(ctx, child, OID_AUTO, "mbuf_header_failed",
   6023   1.1    dyoung 			CTLFLAG_RD, &adapter->mbuf_header_failed,
   6024   1.1    dyoung 			"???");
   6025   1.1    dyoung 	SYSCTL_ADD_ULONG(ctx, child, OID_AUTO, "mbuf_packet_failed",
   6026   1.1    dyoung 			CTLFLAG_RD, &adapter->mbuf_packet_failed,
   6027   1.1    dyoung 			"???");
   6028   1.1    dyoung 	SYSCTL_ADD_ULONG(ctx, child, OID_AUTO, "no_tx_map_avail",
   6029   1.1    dyoung 			CTLFLAG_RD, &adapter->no_tx_map_avail,
   6030   1.1    dyoung 			"???");
   6031   1.1    dyoung #endif
   6032   1.1    dyoung 	evcnt_attach_dynamic(&adapter->handleq, EVCNT_TYPE_MISC,
   6033   1.1    dyoung 	    NULL, device_xname(dev), "Handled queue in softint");
   6034   1.1    dyoung 	evcnt_attach_dynamic(&adapter->req, EVCNT_TYPE_MISC,
   6035   1.1    dyoung 	    NULL, device_xname(dev), "Requeued in softint");
   6036   1.1    dyoung 	evcnt_attach_dynamic(&adapter->morerx, EVCNT_TYPE_MISC,
   6037   1.1    dyoung 	    NULL, device_xname(dev), "Interrupt handler more rx");
   6038   1.1    dyoung 	evcnt_attach_dynamic(&adapter->moretx, EVCNT_TYPE_MISC,
   6039   1.1    dyoung 	    NULL, device_xname(dev), "Interrupt handler more tx");
   6040   1.1    dyoung 	evcnt_attach_dynamic(&adapter->txloops, EVCNT_TYPE_MISC,
   6041   1.1    dyoung 	    NULL, device_xname(dev), "Interrupt handler tx loops");
   6042   1.1    dyoung 	evcnt_attach_dynamic(&adapter->efbig_tx_dma_setup, EVCNT_TYPE_MISC,
   6043   1.1    dyoung 	    NULL, device_xname(dev), "Driver tx dma soft fail EFBIG");
   6044   1.1    dyoung 	evcnt_attach_dynamic(&adapter->m_defrag_failed, EVCNT_TYPE_MISC,
   6045   1.1    dyoung 	    NULL, device_xname(dev), "m_defrag() failed");
   6046   1.1    dyoung 	evcnt_attach_dynamic(&adapter->efbig2_tx_dma_setup, EVCNT_TYPE_MISC,
   6047   1.1    dyoung 	    NULL, device_xname(dev), "Driver tx dma hard fail EFBIG");
   6048   1.1    dyoung 	evcnt_attach_dynamic(&adapter->einval_tx_dma_setup, EVCNT_TYPE_MISC,
   6049   1.1    dyoung 	    NULL, device_xname(dev), "Driver tx dma hard fail EINVAL");
   6050   1.1    dyoung 	evcnt_attach_dynamic(&adapter->other_tx_dma_setup, EVCNT_TYPE_MISC,
   6051   1.1    dyoung 	    NULL, device_xname(dev), "Driver tx dma hard fail other");
   6052   1.1    dyoung 	evcnt_attach_dynamic(&adapter->eagain_tx_dma_setup, EVCNT_TYPE_MISC,
   6053   1.1    dyoung 	    NULL, device_xname(dev), "Driver tx dma soft fail EAGAIN");
   6054   1.1    dyoung 	evcnt_attach_dynamic(&adapter->enomem_tx_dma_setup, EVCNT_TYPE_MISC,
   6055   1.1    dyoung 	    NULL, device_xname(dev), "Driver tx dma soft fail ENOMEM");
   6056   1.1    dyoung 	evcnt_attach_dynamic(&adapter->watchdog_events, EVCNT_TYPE_MISC,
   6057   1.1    dyoung 	    NULL, device_xname(dev), "Watchdog timeouts");
   6058   1.1    dyoung 	evcnt_attach_dynamic(&adapter->tso_err, EVCNT_TYPE_MISC,
   6059   1.1    dyoung 	    NULL, device_xname(dev), "TSO errors");
   6060   1.1    dyoung 	evcnt_attach_dynamic(&adapter->link_irq, EVCNT_TYPE_MISC,
   6061   1.1    dyoung 	    NULL, device_xname(dev), "Link MSIX IRQ Handled");
   6062   1.1    dyoung 
   6063   1.1    dyoung 	for (int i = 0; i < adapter->num_queues; i++, rxr++, txr++) {
   6064   1.1    dyoung 		snprintf(adapter->queues[i].evnamebuf,
   6065   1.1    dyoung 		    sizeof(adapter->queues[i].evnamebuf), "%s queue%d",
   6066   1.1    dyoung 		    device_xname(dev), i);
   6067   1.1    dyoung 		snprintf(adapter->queues[i].namebuf,
   6068   1.1    dyoung 		    sizeof(adapter->queues[i].namebuf), "queue%d", i);
   6069   1.1    dyoung 
   6070   1.1    dyoung 		if ((rnode = ixgbe_sysctl_instance(adapter)) == NULL) {
   6071   1.1    dyoung 			aprint_error_dev(dev, "could not create sysctl root\n");
   6072   1.1    dyoung 			break;
   6073   1.1    dyoung 		}
   6074   1.1    dyoung 
   6075   1.1    dyoung 		if (sysctl_createv(log, 0, &rnode, &rnode,
   6076   1.1    dyoung 		    0, CTLTYPE_NODE,
   6077   1.1    dyoung 		    adapter->queues[i].namebuf, SYSCTL_DESCR("Queue Name"),
   6078   1.1    dyoung 		    NULL, 0, NULL, 0, CTL_CREATE, CTL_EOL) != 0)
   6079   1.1    dyoung 			break;
   6080   1.1    dyoung 
   6081   1.1    dyoung 		if (sysctl_createv(log, 0, &rnode, &cnode,
   6082  1.22   msaitoh 		    CTLFLAG_READWRITE, CTLTYPE_INT,
   6083   1.1    dyoung 		    "interrupt_rate", SYSCTL_DESCR("Interrupt Rate"),
   6084   1.5       dsl 		    ixgbe_sysctl_interrupt_rate_handler, 0,
   6085   1.5       dsl 		    (void *)&adapter->queues[i], 0, CTL_CREATE, CTL_EOL) != 0)
   6086   1.1    dyoung 			break;
   6087   1.1    dyoung 
   6088   1.1    dyoung 		if (sysctl_createv(log, 0, &rnode, &cnode,
   6089  1.22   msaitoh 		    CTLFLAG_READONLY, CTLTYPE_QUAD,
   6090  1.22   msaitoh 		    "irqs", SYSCTL_DESCR("irqs on this queue"),
   6091  1.22   msaitoh 			NULL, 0, &(adapter->queues[i].irqs),
   6092  1.22   msaitoh 		    0, CTL_CREATE, CTL_EOL) != 0)
   6093  1.22   msaitoh 			break;
   6094  1.22   msaitoh 
   6095  1.22   msaitoh 		if (sysctl_createv(log, 0, &rnode, &cnode,
   6096   1.1    dyoung 		    CTLFLAG_READONLY, CTLTYPE_INT,
   6097   1.1    dyoung 		    "txd_head", SYSCTL_DESCR("Transmit Descriptor Head"),
   6098   1.4       dsl 		    ixgbe_sysctl_tdh_handler, 0, (void *)txr,
   6099   1.1    dyoung 		    0, CTL_CREATE, CTL_EOL) != 0)
   6100   1.1    dyoung 			break;
   6101   1.1    dyoung 
   6102   1.1    dyoung 		if (sysctl_createv(log, 0, &rnode, &cnode,
   6103   1.1    dyoung 		    CTLFLAG_READONLY, CTLTYPE_INT,
   6104   1.1    dyoung 		    "txd_tail", SYSCTL_DESCR("Transmit Descriptor Tail"),
   6105   1.4       dsl 		    ixgbe_sysctl_tdt_handler, 0, (void *)txr,
   6106   1.1    dyoung 		    0, CTL_CREATE, CTL_EOL) != 0)
   6107   1.1    dyoung 			break;
   6108   1.1    dyoung 
   6109  1.28   msaitoh 		evcnt_attach_dynamic(&txr->tso_tx, EVCNT_TYPE_MISC,
   6110  1.28   msaitoh 		    NULL, device_xname(dev), "TSO");
   6111   1.1    dyoung 		evcnt_attach_dynamic(&txr->no_desc_avail, EVCNT_TYPE_MISC,
   6112   1.1    dyoung 		    NULL, adapter->queues[i].evnamebuf,
   6113   1.1    dyoung 		    "Queue No Descriptor Available");
   6114   1.1    dyoung 		evcnt_attach_dynamic(&txr->total_packets, EVCNT_TYPE_MISC,
   6115   1.1    dyoung 		    NULL, adapter->queues[i].evnamebuf,
   6116   1.1    dyoung 		    "Queue Packets Transmitted");
   6117   1.1    dyoung 
   6118   1.1    dyoung #ifdef LRO
   6119   1.1    dyoung 		struct lro_ctrl *lro = &rxr->lro;
   6120   1.1    dyoung #endif /* LRO */
   6121   1.1    dyoung 
   6122   1.1    dyoung 		if (sysctl_createv(log, 0, &rnode, &cnode,
   6123   1.1    dyoung 		    CTLFLAG_READONLY,
   6124   1.1    dyoung 		    CTLTYPE_INT,
   6125   1.1    dyoung 		    "rxd_head", SYSCTL_DESCR("Receive Descriptor Head"),
   6126   1.4       dsl 		    ixgbe_sysctl_rdh_handler, 0, (void *)rxr, 0,
   6127   1.1    dyoung 		    CTL_CREATE, CTL_EOL) != 0)
   6128   1.1    dyoung 			break;
   6129   1.1    dyoung 
   6130   1.1    dyoung 		if (sysctl_createv(log, 0, &rnode, &cnode,
   6131   1.1    dyoung 		    CTLFLAG_READONLY,
   6132   1.1    dyoung 		    CTLTYPE_INT,
   6133   1.1    dyoung 		    "rxd_tail", SYSCTL_DESCR("Receive Descriptor Tail"),
   6134   1.4       dsl 		    ixgbe_sysctl_rdt_handler, 0, (void *)rxr, 0,
   6135   1.1    dyoung 		    CTL_CREATE, CTL_EOL) != 0)
   6136   1.1    dyoung 			break;
   6137   1.1    dyoung 
   6138   1.1    dyoung 		if (i < __arraycount(adapter->stats.mpc)) {
   6139   1.1    dyoung 			evcnt_attach_dynamic(&adapter->stats.mpc[i],
   6140   1.1    dyoung 			    EVCNT_TYPE_MISC, NULL, adapter->queues[i].evnamebuf,
   6141   1.1    dyoung 			    "Missed Packet Count");
   6142   1.1    dyoung 		}
   6143   1.1    dyoung 		if (i < __arraycount(adapter->stats.pxontxc)) {
   6144   1.1    dyoung 			evcnt_attach_dynamic(&adapter->stats.pxontxc[i],
   6145   1.1    dyoung 			    EVCNT_TYPE_MISC, NULL, adapter->queues[i].evnamebuf,
   6146   1.1    dyoung 			    "pxontxc");
   6147   1.1    dyoung 			evcnt_attach_dynamic(&adapter->stats.pxonrxc[i],
   6148   1.1    dyoung 			    EVCNT_TYPE_MISC, NULL, adapter->queues[i].evnamebuf,
   6149   1.1    dyoung 			    "pxonrxc");
   6150   1.1    dyoung 			evcnt_attach_dynamic(&adapter->stats.pxofftxc[i],
   6151   1.1    dyoung 			    EVCNT_TYPE_MISC, NULL, adapter->queues[i].evnamebuf,
   6152   1.1    dyoung 			    "pxofftxc");
   6153   1.1    dyoung 			evcnt_attach_dynamic(&adapter->stats.pxoffrxc[i],
   6154   1.1    dyoung 			    EVCNT_TYPE_MISC, NULL, adapter->queues[i].evnamebuf,
   6155   1.1    dyoung 			    "pxoffrxc");
   6156   1.1    dyoung 			evcnt_attach_dynamic(&adapter->stats.pxon2offc[i],
   6157   1.1    dyoung 			    EVCNT_TYPE_MISC, NULL, adapter->queues[i].evnamebuf,
   6158   1.1    dyoung 			    "pxon2offc");
   6159   1.1    dyoung 		}
   6160   1.1    dyoung 		if (i < __arraycount(adapter->stats.qprc)) {
   6161   1.1    dyoung 			evcnt_attach_dynamic(&adapter->stats.qprc[i],
   6162   1.1    dyoung 			    EVCNT_TYPE_MISC, NULL, adapter->queues[i].evnamebuf,
   6163   1.1    dyoung 			    "qprc");
   6164   1.1    dyoung 			evcnt_attach_dynamic(&adapter->stats.qptc[i],
   6165   1.1    dyoung 			    EVCNT_TYPE_MISC, NULL, adapter->queues[i].evnamebuf,
   6166   1.1    dyoung 			    "qptc");
   6167   1.1    dyoung 			evcnt_attach_dynamic(&adapter->stats.qbrc[i],
   6168   1.1    dyoung 			    EVCNT_TYPE_MISC, NULL, adapter->queues[i].evnamebuf,
   6169   1.1    dyoung 			    "qbrc");
   6170   1.1    dyoung 			evcnt_attach_dynamic(&adapter->stats.qbtc[i],
   6171   1.1    dyoung 			    EVCNT_TYPE_MISC, NULL, adapter->queues[i].evnamebuf,
   6172   1.1    dyoung 			    "qbtc");
   6173   1.1    dyoung 			evcnt_attach_dynamic(&adapter->stats.qprdc[i],
   6174   1.1    dyoung 			    EVCNT_TYPE_MISC, NULL, adapter->queues[i].evnamebuf,
   6175   1.1    dyoung 			    "qprdc");
   6176   1.1    dyoung 		}
   6177   1.1    dyoung 
   6178   1.1    dyoung 		evcnt_attach_dynamic(&rxr->rx_packets, EVCNT_TYPE_MISC,
   6179   1.1    dyoung 		    NULL, adapter->queues[i].evnamebuf, "Queue Packets Received");
   6180   1.1    dyoung 		evcnt_attach_dynamic(&rxr->rx_bytes, EVCNT_TYPE_MISC,
   6181   1.1    dyoung 		    NULL, adapter->queues[i].evnamebuf, "Queue Bytes Received");
   6182  1.26   msaitoh 		evcnt_attach_dynamic(&rxr->rx_copies, EVCNT_TYPE_MISC,
   6183  1.26   msaitoh 		    NULL, adapter->queues[i].evnamebuf, "Copied RX Frames");
   6184   1.1    dyoung 		evcnt_attach_dynamic(&rxr->no_jmbuf, EVCNT_TYPE_MISC,
   6185   1.1    dyoung 		    NULL, adapter->queues[i].evnamebuf, "Rx no jumbo mbuf");
   6186   1.1    dyoung 		evcnt_attach_dynamic(&rxr->rx_discarded, EVCNT_TYPE_MISC,
   6187   1.1    dyoung 		    NULL, adapter->queues[i].evnamebuf, "Rx discarded");
   6188   1.1    dyoung 		evcnt_attach_dynamic(&rxr->rx_irq, EVCNT_TYPE_MISC,
   6189   1.1    dyoung 		    NULL, adapter->queues[i].evnamebuf, "Rx interrupts");
   6190   1.1    dyoung #ifdef LRO
   6191   1.1    dyoung 		SYSCTL_ADD_INT(ctx, queue_list, OID_AUTO, "lro_queued",
   6192   1.1    dyoung 				CTLFLAG_RD, &lro->lro_queued, 0,
   6193   1.1    dyoung 				"LRO Queued");
   6194   1.1    dyoung 		SYSCTL_ADD_INT(ctx, queue_list, OID_AUTO, "lro_flushed",
   6195   1.1    dyoung 				CTLFLAG_RD, &lro->lro_flushed, 0,
   6196   1.1    dyoung 				"LRO Flushed");
   6197   1.1    dyoung #endif /* LRO */
   6198   1.1    dyoung 	}
   6199   1.1    dyoung 
   6200   1.1    dyoung 	/* MAC stats get the own sub node */
   6201   1.1    dyoung 
   6202   1.1    dyoung 
   6203   1.1    dyoung 	snprintf(stats->namebuf,
   6204   1.1    dyoung 	    sizeof(stats->namebuf), "%s MAC Statistics", device_xname(dev));
   6205   1.1    dyoung 
   6206   1.1    dyoung 	evcnt_attach_dynamic(&stats->ipcs, EVCNT_TYPE_MISC, NULL,
   6207   1.1    dyoung 	    stats->namebuf, "rx csum offload - IP");
   6208   1.1    dyoung 	evcnt_attach_dynamic(&stats->l4cs, EVCNT_TYPE_MISC, NULL,
   6209   1.1    dyoung 	    stats->namebuf, "rx csum offload - L4");
   6210   1.1    dyoung 	evcnt_attach_dynamic(&stats->ipcs_bad, EVCNT_TYPE_MISC, NULL,
   6211   1.1    dyoung 	    stats->namebuf, "rx csum offload - IP bad");
   6212   1.1    dyoung 	evcnt_attach_dynamic(&stats->l4cs_bad, EVCNT_TYPE_MISC, NULL,
   6213   1.1    dyoung 	    stats->namebuf, "rx csum offload - L4 bad");
   6214   1.1    dyoung 	evcnt_attach_dynamic(&stats->intzero, EVCNT_TYPE_MISC, NULL,
   6215   1.1    dyoung 	    stats->namebuf, "Interrupt conditions zero");
   6216   1.1    dyoung 	evcnt_attach_dynamic(&stats->legint, EVCNT_TYPE_MISC, NULL,
   6217   1.1    dyoung 	    stats->namebuf, "Legacy interrupts");
   6218   1.1    dyoung 	evcnt_attach_dynamic(&stats->crcerrs, EVCNT_TYPE_MISC, NULL,
   6219   1.1    dyoung 	    stats->namebuf, "CRC Errors");
   6220   1.1    dyoung 	evcnt_attach_dynamic(&stats->illerrc, EVCNT_TYPE_MISC, NULL,
   6221   1.1    dyoung 	    stats->namebuf, "Illegal Byte Errors");
   6222   1.1    dyoung 	evcnt_attach_dynamic(&stats->errbc, EVCNT_TYPE_MISC, NULL,
   6223   1.1    dyoung 	    stats->namebuf, "Byte Errors");
   6224   1.1    dyoung 	evcnt_attach_dynamic(&stats->mspdc, EVCNT_TYPE_MISC, NULL,
   6225   1.1    dyoung 	    stats->namebuf, "MAC Short Packets Discarded");
   6226   1.1    dyoung 	evcnt_attach_dynamic(&stats->mlfc, EVCNT_TYPE_MISC, NULL,
   6227   1.1    dyoung 	    stats->namebuf, "MAC Local Faults");
   6228   1.1    dyoung 	evcnt_attach_dynamic(&stats->mrfc, EVCNT_TYPE_MISC, NULL,
   6229   1.1    dyoung 	    stats->namebuf, "MAC Remote Faults");
   6230   1.1    dyoung 	evcnt_attach_dynamic(&stats->rlec, EVCNT_TYPE_MISC, NULL,
   6231   1.1    dyoung 	    stats->namebuf, "Receive Length Errors");
   6232   1.1    dyoung 	evcnt_attach_dynamic(&stats->lxontxc, EVCNT_TYPE_MISC, NULL,
   6233   1.1    dyoung 	    stats->namebuf, "Link XON Transmitted");
   6234   1.1    dyoung 	evcnt_attach_dynamic(&stats->lxonrxc, EVCNT_TYPE_MISC, NULL,
   6235   1.1    dyoung 	    stats->namebuf, "Link XON Received");
   6236   1.1    dyoung 	evcnt_attach_dynamic(&stats->lxofftxc, EVCNT_TYPE_MISC, NULL,
   6237   1.1    dyoung 	    stats->namebuf, "Link XOFF Transmitted");
   6238   1.1    dyoung 	evcnt_attach_dynamic(&stats->lxoffrxc, EVCNT_TYPE_MISC, NULL,
   6239   1.1    dyoung 	    stats->namebuf, "Link XOFF Received");
   6240   1.1    dyoung 
   6241   1.1    dyoung 	/* Packet Reception Stats */
   6242   1.1    dyoung 	evcnt_attach_dynamic(&stats->tor, EVCNT_TYPE_MISC, NULL,
   6243   1.1    dyoung 	    stats->namebuf, "Total Octets Received");
   6244   1.1    dyoung 	evcnt_attach_dynamic(&stats->gorc, EVCNT_TYPE_MISC, NULL,
   6245   1.1    dyoung 	    stats->namebuf, "Good Octets Received");
   6246   1.1    dyoung 	evcnt_attach_dynamic(&stats->tpr, EVCNT_TYPE_MISC, NULL,
   6247   1.1    dyoung 	    stats->namebuf, "Total Packets Received");
   6248   1.1    dyoung 	evcnt_attach_dynamic(&stats->gprc, EVCNT_TYPE_MISC, NULL,
   6249   1.1    dyoung 	    stats->namebuf, "Good Packets Received");
   6250   1.1    dyoung 	evcnt_attach_dynamic(&stats->mprc, EVCNT_TYPE_MISC, NULL,
   6251   1.1    dyoung 	    stats->namebuf, "Multicast Packets Received");
   6252   1.1    dyoung 	evcnt_attach_dynamic(&stats->bprc, EVCNT_TYPE_MISC, NULL,
   6253   1.1    dyoung 	    stats->namebuf, "Broadcast Packets Received");
   6254   1.1    dyoung 	evcnt_attach_dynamic(&stats->prc64, EVCNT_TYPE_MISC, NULL,
   6255   1.1    dyoung 	    stats->namebuf, "64 byte frames received ");
   6256   1.1    dyoung 	evcnt_attach_dynamic(&stats->prc127, EVCNT_TYPE_MISC, NULL,
   6257   1.1    dyoung 	    stats->namebuf, "65-127 byte frames received");
   6258   1.1    dyoung 	evcnt_attach_dynamic(&stats->prc255, EVCNT_TYPE_MISC, NULL,
   6259   1.1    dyoung 	    stats->namebuf, "128-255 byte frames received");
   6260   1.1    dyoung 	evcnt_attach_dynamic(&stats->prc511, EVCNT_TYPE_MISC, NULL,
   6261   1.1    dyoung 	    stats->namebuf, "256-511 byte frames received");
   6262   1.1    dyoung 	evcnt_attach_dynamic(&stats->prc1023, EVCNT_TYPE_MISC, NULL,
   6263   1.1    dyoung 	    stats->namebuf, "512-1023 byte frames received");
   6264   1.1    dyoung 	evcnt_attach_dynamic(&stats->prc1522, EVCNT_TYPE_MISC, NULL,
   6265   1.1    dyoung 	    stats->namebuf, "1023-1522 byte frames received");
   6266   1.1    dyoung 	evcnt_attach_dynamic(&stats->ruc, EVCNT_TYPE_MISC, NULL,
   6267   1.1    dyoung 	    stats->namebuf, "Receive Undersized");
   6268   1.1    dyoung 	evcnt_attach_dynamic(&stats->rfc, EVCNT_TYPE_MISC, NULL,
   6269   1.1    dyoung 	    stats->namebuf, "Fragmented Packets Received ");
   6270   1.1    dyoung 	evcnt_attach_dynamic(&stats->roc, EVCNT_TYPE_MISC, NULL,
   6271   1.1    dyoung 	    stats->namebuf, "Oversized Packets Received");
   6272   1.1    dyoung 	evcnt_attach_dynamic(&stats->rjc, EVCNT_TYPE_MISC, NULL,
   6273   1.1    dyoung 	    stats->namebuf, "Received Jabber");
   6274   1.1    dyoung 	evcnt_attach_dynamic(&stats->mngprc, EVCNT_TYPE_MISC, NULL,
   6275   1.1    dyoung 	    stats->namebuf, "Management Packets Received");
   6276   1.1    dyoung 	evcnt_attach_dynamic(&stats->xec, EVCNT_TYPE_MISC, NULL,
   6277   1.1    dyoung 	    stats->namebuf, "Checksum Errors");
   6278   1.1    dyoung 
   6279   1.1    dyoung 	/* Packet Transmission Stats */
   6280   1.1    dyoung 	evcnt_attach_dynamic(&stats->gotc, EVCNT_TYPE_MISC, NULL,
   6281   1.1    dyoung 	    stats->namebuf, "Good Octets Transmitted");
   6282   1.1    dyoung 	evcnt_attach_dynamic(&stats->tpt, EVCNT_TYPE_MISC, NULL,
   6283   1.1    dyoung 	    stats->namebuf, "Total Packets Transmitted");
   6284   1.1    dyoung 	evcnt_attach_dynamic(&stats->gptc, EVCNT_TYPE_MISC, NULL,
   6285   1.1    dyoung 	    stats->namebuf, "Good Packets Transmitted");
   6286   1.1    dyoung 	evcnt_attach_dynamic(&stats->bptc, EVCNT_TYPE_MISC, NULL,
   6287   1.1    dyoung 	    stats->namebuf, "Broadcast Packets Transmitted");
   6288   1.1    dyoung 	evcnt_attach_dynamic(&stats->mptc, EVCNT_TYPE_MISC, NULL,
   6289   1.1    dyoung 	    stats->namebuf, "Multicast Packets Transmitted");
   6290   1.1    dyoung 	evcnt_attach_dynamic(&stats->mngptc, EVCNT_TYPE_MISC, NULL,
   6291   1.1    dyoung 	    stats->namebuf, "Management Packets Transmitted");
   6292   1.1    dyoung 	evcnt_attach_dynamic(&stats->ptc64, EVCNT_TYPE_MISC, NULL,
   6293   1.1    dyoung 	    stats->namebuf, "64 byte frames transmitted ");
   6294   1.1    dyoung 	evcnt_attach_dynamic(&stats->ptc127, EVCNT_TYPE_MISC, NULL,
   6295   1.1    dyoung 	    stats->namebuf, "65-127 byte frames transmitted");
   6296   1.1    dyoung 	evcnt_attach_dynamic(&stats->ptc255, EVCNT_TYPE_MISC, NULL,
   6297   1.1    dyoung 	    stats->namebuf, "128-255 byte frames transmitted");
   6298   1.1    dyoung 	evcnt_attach_dynamic(&stats->ptc511, EVCNT_TYPE_MISC, NULL,
   6299   1.1    dyoung 	    stats->namebuf, "256-511 byte frames transmitted");
   6300   1.1    dyoung 	evcnt_attach_dynamic(&stats->ptc1023, EVCNT_TYPE_MISC, NULL,
   6301   1.1    dyoung 	    stats->namebuf, "512-1023 byte frames transmitted");
   6302   1.1    dyoung 	evcnt_attach_dynamic(&stats->ptc1522, EVCNT_TYPE_MISC, NULL,
   6303   1.1    dyoung 	    stats->namebuf, "1024-1522 byte frames transmitted");
   6304   1.1    dyoung }
   6305   1.1    dyoung 
   6306   1.1    dyoung /*
   6307   1.1    dyoung ** Set flow control using sysctl:
   6308   1.1    dyoung ** Flow control values:
   6309   1.1    dyoung ** 	0 - off
   6310   1.1    dyoung **	1 - rx pause
   6311   1.1    dyoung **	2 - tx pause
   6312   1.1    dyoung **	3 - full
   6313   1.1    dyoung */
   6314   1.1    dyoung static int
   6315   1.1    dyoung ixgbe_set_flowcntl(SYSCTLFN_ARGS)
   6316   1.1    dyoung {
   6317   1.1    dyoung 	struct sysctlnode node;
   6318  1.24   msaitoh 	int error, last;
   6319   1.1    dyoung 	struct adapter *adapter;
   6320   1.1    dyoung 
   6321   1.1    dyoung 	node = *rnode;
   6322   1.1    dyoung 	adapter = (struct adapter *)node.sysctl_data;
   6323  1.24   msaitoh 	node.sysctl_data = &adapter->fc;
   6324  1.24   msaitoh 	last = adapter->fc;
   6325   1.1    dyoung 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   6326   1.1    dyoung 	if (error != 0 || newp == NULL)
   6327   1.1    dyoung 		return error;
   6328   1.1    dyoung 
   6329   1.1    dyoung 	/* Don't bother if it's not changed */
   6330  1.24   msaitoh 	if (adapter->fc == last)
   6331   1.1    dyoung 		return (0);
   6332   1.1    dyoung 
   6333  1.24   msaitoh 	switch (adapter->fc) {
   6334   1.1    dyoung 		case ixgbe_fc_rx_pause:
   6335   1.1    dyoung 		case ixgbe_fc_tx_pause:
   6336   1.1    dyoung 		case ixgbe_fc_full:
   6337  1.24   msaitoh 			adapter->hw.fc.requested_mode = adapter->fc;
   6338  1.26   msaitoh 			if (adapter->num_queues > 1)
   6339  1.26   msaitoh 				ixgbe_disable_rx_drop(adapter);
   6340   1.1    dyoung 			break;
   6341   1.1    dyoung 		case ixgbe_fc_none:
   6342   1.1    dyoung 			adapter->hw.fc.requested_mode = ixgbe_fc_none;
   6343  1.26   msaitoh 			if (adapter->num_queues > 1)
   6344  1.26   msaitoh 				ixgbe_enable_rx_drop(adapter);
   6345  1.28   msaitoh 			break;
   6346  1.28   msaitoh 		default:
   6347  1.28   msaitoh 			adapter->fc = last;
   6348  1.28   msaitoh 			return (EINVAL);
   6349   1.1    dyoung 	}
   6350  1.25   msaitoh 	/* Don't autoneg if forcing a value */
   6351  1.25   msaitoh 	adapter->hw.fc.disable_fc_autoneg = TRUE;
   6352  1.25   msaitoh 	ixgbe_fc_enable(&adapter->hw);
   6353   1.1    dyoung 	return 0;
   6354   1.1    dyoung }
   6355   1.1    dyoung 
   6356  1.33   msaitoh 
   6357   1.1    dyoung /*
   6358   1.1    dyoung ** Control link advertise speed:
   6359   1.1    dyoung **	1 - advertise only 1G
   6360  1.24   msaitoh **	2 - advertise 100Mb
   6361  1.25   msaitoh **	3 - advertise normal
   6362   1.1    dyoung */
   6363   1.1    dyoung static int
   6364   1.1    dyoung ixgbe_set_advertise(SYSCTLFN_ARGS)
   6365   1.1    dyoung {
   6366   1.1    dyoung 	struct sysctlnode	node;
   6367  1.22   msaitoh 	int			t, error = 0;
   6368   1.1    dyoung 	struct adapter		*adapter;
   6369  1.24   msaitoh 	device_t		dev;
   6370   1.1    dyoung 	struct ixgbe_hw		*hw;
   6371   1.1    dyoung 	ixgbe_link_speed	speed, last;
   6372   1.1    dyoung 
   6373   1.1    dyoung 	node = *rnode;
   6374   1.1    dyoung 	adapter = (struct adapter *)node.sysctl_data;
   6375  1.24   msaitoh 	dev = adapter->dev;
   6376  1.25   msaitoh 	hw = &adapter->hw;
   6377  1.25   msaitoh 	last = adapter->advertise;
   6378   1.1    dyoung 	t = adapter->advertise;
   6379   1.1    dyoung 	node.sysctl_data = &t;
   6380   1.1    dyoung 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   6381   1.1    dyoung 	if (error != 0 || newp == NULL)
   6382   1.1    dyoung 		return error;
   6383   1.1    dyoung 
   6384  1.25   msaitoh 	if (adapter->advertise == last) /* no change */
   6385  1.25   msaitoh 		return (0);
   6386  1.25   msaitoh 
   6387   1.1    dyoung 	if (t == -1)
   6388   1.1    dyoung 		return 0;
   6389   1.1    dyoung 
   6390   1.1    dyoung 	adapter->advertise = t;
   6391   1.1    dyoung 
   6392   1.1    dyoung 	if (!((hw->phy.media_type == ixgbe_media_type_copper) ||
   6393   1.1    dyoung             (hw->phy.multispeed_fiber)))
   6394  1.28   msaitoh 		return (EINVAL);
   6395   1.1    dyoung 
   6396  1.24   msaitoh 	if ((adapter->advertise == 2) && (hw->mac.type != ixgbe_mac_X540)) {
   6397  1.24   msaitoh 		device_printf(dev, "Set Advertise: 100Mb on X540 only\n");
   6398  1.28   msaitoh 		return (EINVAL);
   6399  1.24   msaitoh 	}
   6400  1.24   msaitoh 
   6401   1.1    dyoung 	if (adapter->advertise == 1)
   6402   1.1    dyoung                 speed = IXGBE_LINK_SPEED_1GB_FULL;
   6403  1.24   msaitoh 	else if (adapter->advertise == 2)
   6404  1.24   msaitoh                 speed = IXGBE_LINK_SPEED_100_FULL;
   6405  1.25   msaitoh 	else if (adapter->advertise == 3)
   6406   1.1    dyoung                 speed = IXGBE_LINK_SPEED_1GB_FULL |
   6407   1.1    dyoung 			IXGBE_LINK_SPEED_10GB_FULL;
   6408  1.35   msaitoh 	else {	/* bogus value */
   6409  1.28   msaitoh 		adapter->advertise = last;
   6410  1.28   msaitoh 		return (EINVAL);
   6411  1.28   msaitoh 	}
   6412   1.1    dyoung 
   6413   1.1    dyoung 	hw->mac.autotry_restart = TRUE;
   6414  1.28   msaitoh 	hw->mac.ops.setup_link(hw, speed, TRUE);
   6415   1.1    dyoung 
   6416   1.1    dyoung 	return 0;
   6417   1.1    dyoung }
   6418  1.24   msaitoh 
   6419  1.24   msaitoh /*
   6420  1.24   msaitoh ** Thermal Shutdown Trigger
   6421  1.24   msaitoh **   - cause a Thermal Overtemp IRQ
   6422  1.33   msaitoh **   - this now requires firmware enabling
   6423  1.24   msaitoh */
   6424  1.24   msaitoh static int
   6425  1.24   msaitoh ixgbe_set_thermal_test(SYSCTLFN_ARGS)
   6426  1.24   msaitoh {
   6427  1.24   msaitoh 	struct sysctlnode node;
   6428  1.24   msaitoh 	int		error, fire = 0;
   6429  1.24   msaitoh 	struct adapter	*adapter;
   6430  1.24   msaitoh 	struct ixgbe_hw *hw;
   6431  1.24   msaitoh 
   6432  1.24   msaitoh 	node = *rnode;
   6433  1.24   msaitoh 	adapter = (struct adapter *)node.sysctl_data;
   6434  1.24   msaitoh 	hw = &adapter->hw;
   6435  1.24   msaitoh 
   6436  1.24   msaitoh 	if (hw->mac.type != ixgbe_mac_X540)
   6437  1.24   msaitoh 		return (0);
   6438  1.24   msaitoh 
   6439  1.24   msaitoh 	node.sysctl_data = &fire;
   6440  1.24   msaitoh 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   6441  1.24   msaitoh 	if ((error) || (newp == NULL))
   6442  1.24   msaitoh 		return (error);
   6443  1.24   msaitoh 
   6444  1.24   msaitoh 	if (fire) {
   6445  1.24   msaitoh 		u32 reg = IXGBE_READ_REG(hw, IXGBE_EICS);
   6446  1.24   msaitoh 		reg |= IXGBE_EICR_TS;
   6447  1.24   msaitoh 		IXGBE_WRITE_REG(hw, IXGBE_EICS, reg);
   6448  1.24   msaitoh 	}
   6449  1.24   msaitoh 
   6450  1.24   msaitoh 	return (0);
   6451  1.24   msaitoh }
   6452  1.26   msaitoh 
   6453  1.26   msaitoh /*
   6454  1.26   msaitoh ** Enable the hardware to drop packets when the buffer is
   6455  1.26   msaitoh ** full. This is useful when multiqueue,so that no single
   6456  1.26   msaitoh ** queue being full stalls the entire RX engine. We only
   6457  1.26   msaitoh ** enable this when Multiqueue AND when Flow Control is
   6458  1.26   msaitoh ** disabled.
   6459  1.26   msaitoh */
   6460  1.26   msaitoh static void
   6461  1.26   msaitoh ixgbe_enable_rx_drop(struct adapter *adapter)
   6462  1.26   msaitoh {
   6463  1.26   msaitoh         struct ixgbe_hw *hw = &adapter->hw;
   6464  1.26   msaitoh 
   6465  1.26   msaitoh 	for (int i = 0; i < adapter->num_queues; i++) {
   6466  1.26   msaitoh         	u32 srrctl = IXGBE_READ_REG(hw, IXGBE_SRRCTL(i));
   6467  1.26   msaitoh         	srrctl |= IXGBE_SRRCTL_DROP_EN;
   6468  1.26   msaitoh         	IXGBE_WRITE_REG(hw, IXGBE_SRRCTL(i), srrctl);
   6469  1.26   msaitoh 	}
   6470  1.26   msaitoh }
   6471  1.26   msaitoh 
   6472  1.26   msaitoh static void
   6473  1.26   msaitoh ixgbe_disable_rx_drop(struct adapter *adapter)
   6474  1.26   msaitoh {
   6475  1.26   msaitoh         struct ixgbe_hw *hw = &adapter->hw;
   6476  1.26   msaitoh 
   6477  1.26   msaitoh 	for (int i = 0; i < adapter->num_queues; i++) {
   6478  1.26   msaitoh         	u32 srrctl = IXGBE_READ_REG(hw, IXGBE_SRRCTL(i));
   6479  1.26   msaitoh         	srrctl &= ~IXGBE_SRRCTL_DROP_EN;
   6480  1.26   msaitoh         	IXGBE_WRITE_REG(hw, IXGBE_SRRCTL(i), srrctl);
   6481  1.26   msaitoh 	}
   6482  1.26   msaitoh }
   6483