Home | History | Annotate | Line # | Download | only in igc
if_igc.c revision 1.14
      1  1.14      rin /*	$NetBSD: if_igc.c,v 1.14 2024/06/12 06:21:37 rin Exp $	*/
      2   1.1      rin /*	$OpenBSD: if_igc.c,v 1.13 2023/04/28 10:18:57 bluhm Exp $	*/
      3   1.1      rin /*-
      4   1.1      rin  * SPDX-License-Identifier: BSD-2-Clause
      5   1.1      rin  *
      6   1.1      rin  * Copyright (c) 2016 Nicole Graziano <nicole (at) nextbsd.org>
      7   1.1      rin  * All rights reserved.
      8   1.1      rin  * Copyright (c) 2021 Rubicon Communications, LLC (Netgate)
      9   1.1      rin  *
     10   1.1      rin  * Redistribution and use in source and binary forms, with or without
     11   1.1      rin  * modification, are permitted provided that the following conditions
     12   1.1      rin  * are met:
     13   1.1      rin  * 1. Redistributions of source code must retain the above copyright
     14   1.1      rin  *    notice, this list of conditions and the following disclaimer.
     15   1.1      rin  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.1      rin  *    notice, this list of conditions and the following disclaimer in the
     17   1.1      rin  *    documentation and/or other materials provided with the distribution.
     18   1.1      rin  *
     19   1.1      rin  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     20   1.1      rin  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21   1.1      rin  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22   1.1      rin  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     23   1.1      rin  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24   1.1      rin  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25   1.1      rin  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26   1.1      rin  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27   1.1      rin  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28   1.1      rin  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29   1.1      rin  * SUCH DAMAGE.
     30   1.1      rin  */
     31   1.1      rin 
     32   1.2      rin #include <sys/cdefs.h>
     33  1.14      rin __KERNEL_RCSID(0, "$NetBSD: if_igc.c,v 1.14 2024/06/12 06:21:37 rin Exp $");
     34   1.2      rin 
     35   1.2      rin #ifdef _KERNEL_OPT
     36   1.2      rin #include "opt_if_igc.h"
     37   1.2      rin #if 0 /* notyet */
     38   1.1      rin #include "vlan.h"
     39   1.2      rin #endif
     40   1.2      rin #endif
     41   1.1      rin 
     42   1.1      rin #include <sys/param.h>
     43   1.1      rin #include <sys/systm.h>
     44   1.2      rin #include <sys/bus.h>
     45   1.2      rin #include <sys/cpu.h>
     46   1.2      rin #include <sys/device.h>
     47   1.2      rin #include <sys/endian.h>
     48   1.2      rin #include <sys/intr.h>
     49   1.2      rin #include <sys/interrupt.h>
     50   1.2      rin #include <sys/kernel.h>
     51   1.2      rin #include <sys/kmem.h>
     52   1.1      rin #include <sys/mbuf.h>
     53   1.2      rin #include <sys/mutex.h>
     54   1.1      rin #include <sys/socket.h>
     55   1.2      rin #include <sys/workqueue.h>
     56   1.2      rin #include <sys/xcall.h>
     57   1.1      rin 
     58   1.2      rin #include <net/bpf.h>
     59   1.1      rin #include <net/if.h>
     60   1.2      rin #include <net/if_ether.h>
     61   1.1      rin #include <net/if_media.h>
     62   1.2      rin #include <net/if_vlanvar.h>
     63   1.2      rin #include <net/rss_config.h>
     64   1.1      rin 
     65   1.1      rin #include <netinet/in.h>
     66   1.1      rin #include <netinet/ip.h>
     67   1.1      rin #include <netinet/ip6.h>
     68   1.2      rin #include <netinet/tcp.h>
     69   1.1      rin 
     70   1.2      rin #include <dev/pci/pcivar.h>
     71   1.2      rin #include <dev/pci/pcireg.h>
     72   1.2      rin #include <dev/pci/pcidevs.h>
     73   1.2      rin 
     74   1.2      rin #include <dev/pci/igc/if_igc.h>
     75   1.2      rin #include <dev/pci/igc/igc_evcnt.h>
     76   1.2      rin #include <dev/pci/igc/igc_hw.h>
     77   1.2      rin #include <dev/mii/miivar.h>
     78   1.2      rin 
     79   1.2      rin #define IGC_WORKQUEUE_PRI	PRI_SOFTNET
     80   1.2      rin 
     81   1.2      rin #ifndef IGC_RX_INTR_PROCESS_LIMIT_DEFAULT
     82   1.2      rin #define IGC_RX_INTR_PROCESS_LIMIT_DEFAULT	0
     83   1.2      rin #endif
     84   1.2      rin #ifndef IGC_TX_INTR_PROCESS_LIMIT_DEFAULT
     85   1.2      rin #define IGC_TX_INTR_PROCESS_LIMIT_DEFAULT	0
     86   1.1      rin #endif
     87   1.1      rin 
     88   1.2      rin #ifndef IGC_RX_PROCESS_LIMIT_DEFAULT
     89   1.2      rin #define IGC_RX_PROCESS_LIMIT_DEFAULT		256
     90   1.2      rin #endif
     91   1.2      rin #ifndef IGC_TX_PROCESS_LIMIT_DEFAULT
     92   1.2      rin #define IGC_TX_PROCESS_LIMIT_DEFAULT		256
     93   1.2      rin #endif
     94   1.1      rin 
     95   1.2      rin #define	htolem32(p, x)	(*((uint32_t *)(p)) = htole32(x))
     96   1.2      rin #define	htolem64(p, x)	(*((uint64_t *)(p)) = htole64(x))
     97   1.1      rin 
     98   1.2      rin static const struct igc_product {
     99   1.2      rin 	pci_vendor_id_t		igcp_vendor;
    100   1.2      rin 	pci_product_id_t	igcp_product;
    101   1.2      rin 	const char		*igcp_name;
    102   1.2      rin } igc_products[] = {
    103   1.2      rin 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_I225_IT,
    104   1.2      rin 	    "Intel(R) Ethernet Controller I225-IT(2)" },
    105   1.2      rin 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_I226_LM,
    106   1.2      rin 	    "Intel(R) Ethernet Controller I226-LM" },
    107   1.2      rin 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_I226_V,
    108   1.2      rin 	    "Intel(R) Ethernet Controller I226-V" },
    109   1.2      rin 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_I226_IT,
    110   1.2      rin 	    "Intel(R) Ethernet Controller I226-IT" },
    111   1.2      rin 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_I221_V,
    112   1.2      rin 	    "Intel(R) Ethernet Controller I221-V" },
    113   1.2      rin 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_I226_BLANK_NVM,
    114   1.2      rin 	    "Intel(R) Ethernet Controller I226(blankNVM)" },
    115   1.2      rin 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_I225_LM,
    116   1.2      rin 	    "Intel(R) Ethernet Controller I225-LM" },
    117   1.2      rin 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_I225_V,
    118   1.2      rin 	    "Intel(R) Ethernet Controller I225-V" },
    119   1.2      rin 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_I220_V,
    120   1.2      rin 	    "Intel(R) Ethernet Controller I220-V" },
    121   1.2      rin 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_I225_I,
    122   1.2      rin 	    "Intel(R) Ethernet Controller I225-I" },
    123   1.2      rin 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_I225_BLANK_NVM,
    124   1.2      rin 	    "Intel(R) Ethernet Controller I225(blankNVM)" },
    125   1.2      rin 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_I225_K,
    126   1.2      rin 	    "Intel(R) Ethernet Controller I225-K" },
    127   1.2      rin 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_I225_K2,
    128   1.2      rin 	    "Intel(R) Ethernet Controller I225-K(2)" },
    129   1.2      rin 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_I226_K,
    130   1.2      rin 	    "Intel(R) Ethernet Controller I226-K" },
    131   1.2      rin 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_I225_LMVP,
    132   1.2      rin 	    "Intel(R) Ethernet Controller I225-LMvP(2)" },
    133   1.2      rin 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_I226_LMVP,
    134   1.2      rin 	    "Intel(R) Ethernet Controller I226-LMvP" },
    135   1.2      rin 	{ 0, 0, NULL },
    136   1.1      rin };
    137   1.1      rin 
    138   1.2      rin #define	IGC_DF_CFG	0x1
    139   1.2      rin #define	IGC_DF_TX	0x2
    140   1.2      rin #define	IGC_DF_RX	0x4
    141   1.2      rin #define	IGC_DF_MISC	0x8
    142   1.2      rin 
    143   1.2      rin #ifdef IGC_DEBUG_FLAGS
    144   1.2      rin int igc_debug_flags = IGC_DEBUG_FLAGS;
    145   1.2      rin #else
    146   1.2      rin int igc_debug_flags = 0;
    147   1.2      rin #endif
    148   1.2      rin 
    149   1.2      rin #define	DPRINTF(flag, fmt, args...)		do {			\
    150   1.2      rin 	if (igc_debug_flags & (IGC_DF_ ## flag))			\
    151   1.2      rin 		printf("%s: %d: " fmt, __func__, __LINE__, ##args);	\
    152   1.2      rin     } while (0)
    153   1.2      rin 
    154   1.1      rin /*********************************************************************
    155   1.1      rin  *  Function Prototypes
    156   1.1      rin  *********************************************************************/
    157   1.2      rin static int	igc_match(device_t, cfdata_t, void *);
    158   1.2      rin static void	igc_attach(device_t, device_t, void *);
    159   1.2      rin static int	igc_detach(device_t, int);
    160   1.2      rin 
    161   1.2      rin static void	igc_identify_hardware(struct igc_softc *);
    162   1.2      rin static int	igc_adjust_nqueues(struct igc_softc *);
    163   1.2      rin static int	igc_allocate_pci_resources(struct igc_softc *);
    164   1.2      rin static int	igc_allocate_interrupts(struct igc_softc *);
    165   1.2      rin static int	igc_allocate_queues(struct igc_softc *);
    166   1.2      rin static void	igc_free_pci_resources(struct igc_softc *);
    167   1.2      rin static void	igc_free_interrupts(struct igc_softc *);
    168   1.2      rin static void	igc_free_queues(struct igc_softc *);
    169   1.2      rin static void	igc_reset(struct igc_softc *);
    170   1.2      rin static void	igc_init_dmac(struct igc_softc *, uint32_t);
    171   1.2      rin static int	igc_setup_interrupts(struct igc_softc *);
    172   1.2      rin static void	igc_attach_counters(struct igc_softc *sc);
    173   1.2      rin static void	igc_detach_counters(struct igc_softc *sc);
    174   1.2      rin static void	igc_update_counters(struct igc_softc *sc);
    175   1.2      rin static void	igc_clear_counters(struct igc_softc *sc);
    176   1.2      rin static int	igc_setup_msix(struct igc_softc *);
    177   1.2      rin static int	igc_setup_msi(struct igc_softc *);
    178   1.2      rin static int	igc_setup_intx(struct igc_softc *);
    179   1.2      rin static int	igc_dma_malloc(struct igc_softc *, bus_size_t,
    180   1.2      rin 		    struct igc_dma_alloc *);
    181   1.2      rin static void	igc_dma_free(struct igc_softc *, struct igc_dma_alloc *);
    182   1.2      rin static void	igc_setup_interface(struct igc_softc *);
    183   1.2      rin 
    184   1.2      rin static int	igc_init(struct ifnet *);
    185   1.2      rin static int	igc_init_locked(struct igc_softc *);
    186   1.2      rin static void	igc_start(struct ifnet *);
    187   1.2      rin static int	igc_transmit(struct ifnet *, struct mbuf *);
    188   1.2      rin static void	igc_tx_common_locked(struct ifnet *, struct tx_ring *, int);
    189   1.2      rin static bool	igc_txeof(struct tx_ring *, u_int);
    190   1.2      rin static void	igc_intr_barrier(struct igc_softc *);
    191   1.2      rin static void	igc_stop(struct ifnet *, int);
    192   1.2      rin static void	igc_stop_locked(struct igc_softc *);
    193   1.2      rin static int	igc_ioctl(struct ifnet *, u_long, void *);
    194   1.2      rin #ifdef IF_RXR
    195   1.2      rin static int	igc_rxrinfo(struct igc_softc *, struct if_rxrinfo *);
    196   1.2      rin #endif
    197   1.2      rin static void	igc_rxfill(struct rx_ring *);
    198   1.2      rin static void	igc_rxrefill(struct rx_ring *, int);
    199   1.2      rin static bool	igc_rxeof(struct rx_ring *, u_int);
    200   1.2      rin static int	igc_rx_checksum(struct igc_queue *, uint64_t, uint32_t,
    201   1.2      rin 		    uint32_t);
    202   1.2      rin static void	igc_watchdog(struct ifnet *);
    203   1.2      rin static void	igc_tick(void *);
    204   1.2      rin static void	igc_media_status(struct ifnet *, struct ifmediareq *);
    205   1.2      rin static int	igc_media_change(struct ifnet *);
    206   1.2      rin static int	igc_ifflags_cb(struct ethercom *);
    207   1.2      rin static void	igc_set_filter(struct igc_softc *);
    208   1.2      rin static void	igc_update_link_status(struct igc_softc *);
    209   1.2      rin static int	igc_get_buf(struct rx_ring *, int, bool);
    210   1.2      rin static int	igc_tx_ctx_setup(struct tx_ring *, struct mbuf *, int,
    211   1.2      rin 		    uint32_t *, uint32_t *);
    212   1.2      rin static int	igc_tso_setup(struct tx_ring *, struct mbuf *, int,
    213   1.2      rin 		    uint32_t *, uint32_t *);
    214   1.2      rin 
    215   1.2      rin static void	igc_configure_queues(struct igc_softc *);
    216   1.2      rin static void	igc_set_queues(struct igc_softc *, uint32_t, uint32_t, int);
    217   1.2      rin static void	igc_enable_queue(struct igc_softc *, uint32_t);
    218   1.2      rin static void	igc_enable_intr(struct igc_softc *);
    219   1.2      rin static void	igc_disable_intr(struct igc_softc *);
    220   1.2      rin static int	igc_intr_link(void *);
    221   1.2      rin static int	igc_intr_queue(void *);
    222   1.2      rin static int	igc_intr(void *);
    223   1.2      rin static void	igc_handle_queue(void *);
    224   1.2      rin static void	igc_handle_queue_work(struct work *, void *);
    225   1.2      rin static void	igc_sched_handle_queue(struct igc_softc *, struct igc_queue *);
    226   1.2      rin static void	igc_barrier_handle_queue(struct igc_softc *);
    227   1.2      rin 
    228   1.2      rin static int	igc_allocate_transmit_buffers(struct tx_ring *);
    229   1.2      rin static int	igc_setup_transmit_structures(struct igc_softc *);
    230   1.2      rin static int	igc_setup_transmit_ring(struct tx_ring *);
    231   1.2      rin static void	igc_initialize_transmit_unit(struct igc_softc *);
    232   1.2      rin static void	igc_free_transmit_structures(struct igc_softc *);
    233   1.2      rin static void	igc_free_transmit_buffers(struct tx_ring *);
    234   1.2      rin static void	igc_withdraw_transmit_packets(struct tx_ring *, bool);
    235   1.2      rin static int	igc_allocate_receive_buffers(struct rx_ring *);
    236   1.2      rin static int	igc_setup_receive_structures(struct igc_softc *);
    237   1.2      rin static int	igc_setup_receive_ring(struct rx_ring *);
    238   1.2      rin static void	igc_initialize_receive_unit(struct igc_softc *);
    239   1.2      rin static void	igc_free_receive_structures(struct igc_softc *);
    240   1.2      rin static void	igc_free_receive_buffers(struct rx_ring *);
    241   1.2      rin static void	igc_clear_receive_status(struct rx_ring *);
    242   1.2      rin static void	igc_initialize_rss_mapping(struct igc_softc *);
    243   1.2      rin 
    244   1.2      rin static void	igc_get_hw_control(struct igc_softc *);
    245   1.2      rin static void	igc_release_hw_control(struct igc_softc *);
    246   1.2      rin static int	igc_is_valid_ether_addr(uint8_t *);
    247   1.2      rin static void	igc_print_devinfo(struct igc_softc *);
    248   1.2      rin 
    249   1.2      rin CFATTACH_DECL3_NEW(igc, sizeof(struct igc_softc),
    250   1.2      rin     igc_match, igc_attach, igc_detach, NULL, NULL, NULL, 0);
    251   1.2      rin 
    252   1.2      rin static inline int
    253   1.2      rin igc_txdesc_incr(struct igc_softc *sc, int id)
    254   1.2      rin {
    255   1.2      rin 
    256   1.2      rin 	if (++id == sc->num_tx_desc)
    257   1.2      rin 		id = 0;
    258   1.2      rin 	return id;
    259   1.2      rin }
    260   1.2      rin 
    261   1.2      rin static inline int __unused
    262   1.2      rin igc_txdesc_decr(struct igc_softc *sc, int id)
    263   1.2      rin {
    264   1.2      rin 
    265   1.2      rin 	if (--id < 0)
    266   1.2      rin 		id = sc->num_tx_desc - 1;
    267   1.2      rin 	return id;
    268   1.2      rin }
    269   1.2      rin 
    270   1.2      rin static inline void
    271   1.2      rin igc_txdesc_sync(struct tx_ring *txr, int id, int ops)
    272   1.2      rin {
    273   1.2      rin 
    274   1.2      rin 	bus_dmamap_sync(txr->txdma.dma_tag, txr->txdma.dma_map,
    275   1.2      rin 	    id * sizeof(union igc_adv_tx_desc), sizeof(union igc_adv_tx_desc),
    276   1.2      rin 	    ops);
    277   1.2      rin }
    278   1.1      rin 
    279   1.2      rin static inline int
    280   1.2      rin igc_rxdesc_incr(struct igc_softc *sc, int id)
    281   1.2      rin {
    282   1.2      rin 
    283   1.2      rin 	if (++id == sc->num_rx_desc)
    284   1.2      rin 		id = 0;
    285   1.2      rin 	return id;
    286   1.2      rin }
    287   1.2      rin 
    288   1.2      rin static inline int
    289   1.2      rin igc_rxdesc_decr(struct igc_softc *sc, int id)
    290   1.2      rin {
    291   1.2      rin 
    292   1.2      rin 	if (--id < 0)
    293   1.2      rin 		id = sc->num_rx_desc - 1;
    294   1.2      rin 	return id;
    295   1.2      rin }
    296   1.2      rin 
    297   1.2      rin static inline void
    298   1.2      rin igc_rxdesc_sync(struct rx_ring *rxr, int id, int ops)
    299   1.2      rin {
    300   1.2      rin 
    301   1.2      rin 	bus_dmamap_sync(rxr->rxdma.dma_tag, rxr->rxdma.dma_map,
    302   1.2      rin 	    id * sizeof(union igc_adv_rx_desc), sizeof(union igc_adv_rx_desc),
    303   1.2      rin 	    ops);
    304   1.2      rin }
    305   1.1      rin 
    306   1.2      rin static const struct igc_product *
    307   1.2      rin igc_lookup(const struct pci_attach_args *pa)
    308   1.2      rin {
    309   1.2      rin 	const struct igc_product *igcp;
    310   1.1      rin 
    311   1.2      rin 	for (igcp = igc_products; igcp->igcp_name != NULL; igcp++) {
    312   1.2      rin 		if (PCI_VENDOR(pa->pa_id) == igcp->igcp_vendor &&
    313   1.2      rin 		    PCI_PRODUCT(pa->pa_id) == igcp->igcp_product)
    314   1.2      rin 			return igcp;
    315   1.2      rin 	}
    316   1.2      rin 	return NULL;
    317   1.2      rin }
    318   1.1      rin 
    319   1.1      rin /*********************************************************************
    320   1.1      rin  *  Device identification routine
    321   1.1      rin  *
    322   1.1      rin  *  igc_match determines if the driver should be loaded on
    323   1.1      rin  *  adapter based on PCI vendor/device id of the adapter.
    324   1.1      rin  *
    325   1.1      rin  *  return 0 on success, positive on failure
    326   1.1      rin  *********************************************************************/
    327   1.2      rin static int
    328   1.2      rin igc_match(device_t parent, cfdata_t match, void *aux)
    329   1.1      rin {
    330   1.2      rin 	struct pci_attach_args *pa = aux;
    331   1.2      rin 
    332   1.2      rin 	if (igc_lookup(pa) != NULL)
    333   1.2      rin 		return 1;
    334   1.2      rin 
    335   1.2      rin 	return 0;
    336   1.1      rin }
    337   1.1      rin 
    338   1.1      rin /*********************************************************************
    339   1.1      rin  *  Device initialization routine
    340   1.1      rin  *
    341   1.1      rin  *  The attach entry point is called when the driver is being loaded.
    342   1.1      rin  *  This routine identifies the type of hardware, allocates all resources
    343   1.1      rin  *  and initializes the hardware.
    344   1.1      rin  *
    345   1.1      rin  *  return 0 on success, positive on failure
    346   1.1      rin  *********************************************************************/
    347   1.2      rin static void
    348   1.2      rin igc_attach(device_t parent, device_t self, void *aux)
    349   1.1      rin {
    350   1.2      rin 	struct pci_attach_args *pa = aux;
    351   1.2      rin 	struct igc_softc *sc = device_private(self);
    352   1.1      rin 	struct igc_hw *hw = &sc->hw;
    353   1.1      rin 
    354   1.2      rin 	const struct igc_product *igcp = igc_lookup(pa);
    355   1.2      rin 	KASSERT(igcp != NULL);
    356   1.2      rin 
    357   1.2      rin 	sc->sc_dev = self;
    358   1.2      rin 	callout_init(&sc->sc_tick_ch, CALLOUT_MPSAFE);
    359   1.2      rin 	callout_setfunc(&sc->sc_tick_ch, igc_tick, sc);
    360   1.2      rin 	sc->sc_core_stopping = false;
    361   1.2      rin 
    362   1.1      rin 	sc->osdep.os_sc = sc;
    363   1.1      rin 	sc->osdep.os_pa = *pa;
    364   1.8      rin #ifndef __aarch64__
    365   1.3      rin 	/*
    366   1.3      rin 	 * XXX PR port-arm/57643
    367   1.3      rin 	 * 64-bit DMA does not work at least for LX2K with 32/64GB memory.
    368   1.3      rin 	 * smmu(4) support may be required.
    369   1.3      rin 	 */
    370   1.8      rin 	if (pci_dma64_available(pa)) {
    371   1.8      rin 		aprint_verbose(", 64-bit DMA");
    372   1.8      rin 		sc->osdep.os_dmat = pa->pa_dmat64;
    373   1.8      rin 	} else
    374   1.3      rin #endif
    375   1.8      rin 	{
    376   1.8      rin 		aprint_verbose(", 32-bit DMA");
    377   1.8      rin 		sc->osdep.os_dmat = pa->pa_dmat;
    378   1.8      rin 	}
    379   1.8      rin 
    380   1.8      rin 	pci_aprint_devinfo_fancy(pa, "Ethernet controller", igcp->igcp_name, 1);
    381   1.1      rin 
    382   1.1      rin 	/* Determine hardware and mac info */
    383   1.1      rin 	igc_identify_hardware(sc);
    384   1.1      rin 
    385   1.1      rin 	sc->num_tx_desc = IGC_DEFAULT_TXD;
    386   1.1      rin 	sc->num_rx_desc = IGC_DEFAULT_RXD;
    387   1.1      rin 
    388   1.1      rin 	 /* Setup PCI resources */
    389   1.2      rin 	if (igc_allocate_pci_resources(sc)) {
    390   1.2      rin 		aprint_error_dev(sc->sc_dev,
    391   1.2      rin 		    "unable to allocate PCI resources\n");
    392   1.2      rin 		goto err_pci;
    393   1.2      rin 	}
    394   1.2      rin 
    395   1.2      rin 	if (igc_allocate_interrupts(sc)) {
    396   1.2      rin 		aprint_error_dev(sc->sc_dev, "unable to allocate interrupts\n");
    397   1.2      rin 		goto err_pci;
    398   1.2      rin 	}
    399   1.1      rin 
    400   1.1      rin 	/* Allocate TX/RX queues */
    401   1.2      rin 	if (igc_allocate_queues(sc)) {
    402   1.2      rin 		aprint_error_dev(sc->sc_dev, "unable to allocate queues\n");
    403   1.2      rin 		goto err_alloc_intr;
    404   1.2      rin 	}
    405   1.1      rin 
    406   1.1      rin 	/* Do shared code initialization */
    407   1.1      rin 	if (igc_setup_init_funcs(hw, true)) {
    408   1.2      rin 		aprint_error_dev(sc->sc_dev, "unable to initialize\n");
    409   1.2      rin 		goto err_alloc_intr;
    410   1.1      rin 	}
    411   1.1      rin 
    412   1.1      rin 	hw->mac.autoneg = DO_AUTO_NEG;
    413   1.1      rin 	hw->phy.autoneg_wait_to_complete = false;
    414   1.1      rin 	hw->phy.autoneg_advertised = AUTONEG_ADV_DEFAULT;
    415   1.1      rin 
    416   1.1      rin 	/* Copper options. */
    417   1.1      rin 	if (hw->phy.media_type == igc_media_type_copper)
    418   1.1      rin 		hw->phy.mdix = AUTO_ALL_MODES;
    419   1.1      rin 
    420   1.1      rin 	/* Set the max frame size. */
    421   1.1      rin 	sc->hw.mac.max_frame_size = 9234;
    422   1.1      rin 
    423   1.1      rin 	/* Allocate multicast array memory. */
    424   1.2      rin 	sc->mta = kmem_alloc(IGC_MTA_LEN, KM_SLEEP);
    425   1.1      rin 
    426   1.1      rin 	/* Check SOL/IDER usage. */
    427   1.2      rin 	if (igc_check_reset_block(hw)) {
    428   1.2      rin 		aprint_error_dev(sc->sc_dev,
    429   1.2      rin 		    "PHY reset is blocked due to SOL/IDER session\n");
    430   1.2      rin 	}
    431   1.1      rin 
    432   1.1      rin 	/* Disable Energy Efficient Ethernet. */
    433   1.1      rin 	sc->hw.dev_spec._i225.eee_disable = true;
    434   1.1      rin 
    435   1.1      rin 	igc_reset_hw(hw);
    436   1.1      rin 
    437   1.1      rin 	/* Make sure we have a good EEPROM before we read from it. */
    438   1.1      rin 	if (igc_validate_nvm_checksum(hw) < 0) {
    439   1.1      rin 		/*
    440   1.1      rin 		 * Some PCI-E parts fail the first check due to
    441   1.1      rin 		 * the link being in sleep state, call it again,
    442   1.1      rin 		 * if it fails a second time its a real issue.
    443   1.1      rin 		 */
    444   1.1      rin 		if (igc_validate_nvm_checksum(hw) < 0) {
    445   1.2      rin 			aprint_error_dev(sc->sc_dev,
    446   1.2      rin 			    "EEPROM checksum invalid\n");
    447   1.1      rin 			goto err_late;
    448   1.1      rin 		}
    449   1.1      rin 	}
    450   1.1      rin 
    451   1.1      rin 	/* Copy the permanent MAC address out of the EEPROM. */
    452   1.1      rin 	if (igc_read_mac_addr(hw) < 0) {
    453   1.2      rin 		aprint_error_dev(sc->sc_dev,
    454   1.2      rin 		    "unable to read MAC address from EEPROM\n");
    455   1.1      rin 		goto err_late;
    456   1.1      rin 	}
    457   1.1      rin 
    458   1.1      rin 	if (!igc_is_valid_ether_addr(hw->mac.addr)) {
    459   1.2      rin 		aprint_error_dev(sc->sc_dev, "invalid MAC address\n");
    460   1.1      rin 		goto err_late;
    461   1.1      rin 	}
    462   1.1      rin 
    463   1.2      rin 	if (igc_setup_interrupts(sc))
    464   1.2      rin 		goto err_late;
    465   1.1      rin 
    466   1.2      rin 	/* Attach counters. */
    467   1.2      rin 	igc_attach_counters(sc);
    468   1.1      rin 
    469   1.1      rin 	/* Setup OS specific network interface. */
    470   1.1      rin 	igc_setup_interface(sc);
    471   1.1      rin 
    472   1.2      rin 	igc_print_devinfo(sc);
    473   1.2      rin 
    474   1.1      rin 	igc_reset(sc);
    475   1.1      rin 	hw->mac.get_link_status = true;
    476   1.1      rin 	igc_update_link_status(sc);
    477   1.1      rin 
    478   1.1      rin 	/* The driver can now take control from firmware. */
    479   1.1      rin 	igc_get_hw_control(sc);
    480   1.1      rin 
    481   1.2      rin 	aprint_normal_dev(sc->sc_dev, "Ethernet address %s\n",
    482   1.2      rin 	    ether_sprintf(sc->hw.mac.addr));
    483   1.2      rin 
    484   1.2      rin 	if (pmf_device_register(self, NULL, NULL))
    485   1.2      rin 		pmf_class_network_register(self, &sc->sc_ec.ec_if);
    486   1.2      rin 	else
    487   1.2      rin 		aprint_error_dev(self, "couldn't establish power handler\n");
    488   1.2      rin 
    489   1.1      rin 	return;
    490   1.1      rin 
    491   1.2      rin  err_late:
    492   1.1      rin 	igc_release_hw_control(sc);
    493   1.2      rin  err_alloc_intr:
    494   1.2      rin 	igc_free_interrupts(sc);
    495   1.2      rin  err_pci:
    496   1.1      rin 	igc_free_pci_resources(sc);
    497   1.2      rin 	kmem_free(sc->mta, IGC_MTA_LEN);
    498   1.1      rin }
    499   1.1      rin 
    500   1.1      rin /*********************************************************************
    501   1.1      rin  *  Device removal routine
    502   1.1      rin  *
    503   1.1      rin  *  The detach entry point is called when the driver is being removed.
    504   1.1      rin  *  This routine stops the adapter and deallocates all the resources
    505   1.1      rin  *  that were allocated for driver operation.
    506   1.1      rin  *
    507   1.1      rin  *  return 0 on success, positive on failure
    508   1.1      rin  *********************************************************************/
    509   1.2      rin static int
    510   1.2      rin igc_detach(device_t self, int flags)
    511   1.1      rin {
    512   1.2      rin 	struct igc_softc *sc = device_private(self);
    513   1.2      rin 	struct ifnet *ifp = &sc->sc_ec.ec_if;
    514   1.2      rin 
    515   1.2      rin 	mutex_enter(&sc->sc_core_lock);
    516   1.2      rin 	igc_stop_locked(sc);
    517   1.2      rin 	mutex_exit(&sc->sc_core_lock);
    518   1.2      rin 
    519   1.2      rin 	igc_detach_counters(sc);
    520   1.1      rin 
    521   1.2      rin 	igc_free_queues(sc);
    522   1.1      rin 
    523   1.1      rin 	igc_phy_hw_reset(&sc->hw);
    524   1.1      rin 	igc_release_hw_control(sc);
    525   1.1      rin 
    526   1.1      rin 	ether_ifdetach(ifp);
    527   1.1      rin 	if_detach(ifp);
    528   1.2      rin 	ifmedia_fini(&sc->media);
    529   1.1      rin 
    530   1.2      rin 	igc_free_interrupts(sc);
    531   1.1      rin 	igc_free_pci_resources(sc);
    532   1.2      rin 	kmem_free(sc->mta, IGC_MTA_LEN);
    533   1.1      rin 
    534   1.2      rin 	mutex_destroy(&sc->sc_core_lock);
    535   1.1      rin 
    536   1.1      rin 	return 0;
    537   1.1      rin }
    538   1.1      rin 
    539   1.2      rin static void
    540   1.1      rin igc_identify_hardware(struct igc_softc *sc)
    541   1.1      rin {
    542   1.1      rin 	struct igc_osdep *os = &sc->osdep;
    543   1.1      rin 	struct pci_attach_args *pa = &os->os_pa;
    544   1.1      rin 
    545   1.1      rin 	/* Save off the information about this board. */
    546   1.1      rin 	sc->hw.device_id = PCI_PRODUCT(pa->pa_id);
    547   1.1      rin 
    548   1.1      rin 	/* Do shared code init and setup. */
    549   1.1      rin 	if (igc_set_mac_type(&sc->hw)) {
    550   1.2      rin 		aprint_error_dev(sc->sc_dev, "unable to identify hardware\n");
    551   1.1      rin 		return;
    552   1.2      rin 	}
    553   1.1      rin }
    554   1.1      rin 
    555   1.2      rin static int
    556   1.1      rin igc_allocate_pci_resources(struct igc_softc *sc)
    557   1.1      rin {
    558   1.1      rin 	struct igc_osdep *os = &sc->osdep;
    559   1.1      rin 	struct pci_attach_args *pa = &os->os_pa;
    560   1.1      rin 
    561   1.2      rin 	/*
    562   1.2      rin 	 * Enable bus mastering and memory-mapped I/O for sure.
    563   1.2      rin 	 */
    564   1.2      rin 	pcireg_t csr =
    565   1.2      rin 	    pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
    566   1.2      rin 	csr |= PCI_COMMAND_MASTER_ENABLE | PCI_COMMAND_MEM_ENABLE;
    567   1.2      rin 	pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG, csr);
    568   1.2      rin 
    569   1.2      rin 	const pcireg_t memtype =
    570   1.2      rin 	    pci_mapreg_type(pa->pa_pc, pa->pa_tag, IGC_PCIREG);
    571   1.1      rin 	if (pci_mapreg_map(pa, IGC_PCIREG, memtype, 0, &os->os_memt,
    572   1.2      rin 	    &os->os_memh, &os->os_membase, &os->os_memsize)) {
    573   1.2      rin 		aprint_error_dev(sc->sc_dev, "unable to map registers\n");
    574   1.1      rin 		return ENXIO;
    575   1.1      rin 	}
    576   1.2      rin 
    577   1.2      rin 	sc->hw.hw_addr = os->os_membase;
    578   1.1      rin 	sc->hw.back = os;
    579   1.1      rin 
    580   1.2      rin 	return 0;
    581   1.2      rin }
    582   1.2      rin 
    583   1.2      rin static int __unused
    584   1.2      rin igc_adjust_nqueues(struct igc_softc *sc)
    585   1.2      rin {
    586   1.2      rin 	struct pci_attach_args *pa = &sc->osdep.os_pa;
    587   1.2      rin 	int nqueues = MIN(IGC_MAX_NQUEUES, ncpu);
    588   1.2      rin 
    589   1.2      rin 	const int nmsix = pci_msix_count(pa->pa_pc, pa->pa_tag);
    590   1.2      rin 	if (nmsix <= 1)
    591   1.2      rin 		nqueues = 1;
    592   1.2      rin 	else if (nmsix < nqueues + 1)
    593   1.2      rin 		nqueues = nmsix - 1;
    594   1.2      rin 
    595   1.2      rin 	return nqueues;
    596   1.2      rin }
    597   1.2      rin 
    598   1.2      rin static int
    599   1.2      rin igc_allocate_interrupts(struct igc_softc *sc)
    600   1.2      rin {
    601   1.2      rin 	struct pci_attach_args *pa = &sc->osdep.os_pa;
    602   1.2      rin 	int error;
    603   1.2      rin 
    604   1.2      rin #ifndef IGC_DISABLE_MSIX
    605   1.2      rin 	const int nqueues = igc_adjust_nqueues(sc);
    606   1.2      rin 	if (nqueues > 1) {
    607   1.2      rin 		sc->sc_nintrs = nqueues + 1;
    608   1.2      rin 		error = pci_msix_alloc_exact(pa, &sc->sc_intrs, sc->sc_nintrs);
    609   1.2      rin 		if (!error) {
    610   1.2      rin 			sc->sc_nqueues = nqueues;
    611   1.2      rin 			sc->sc_intr_type = PCI_INTR_TYPE_MSIX;
    612   1.2      rin 			return 0;
    613   1.2      rin 		}
    614   1.2      rin 	}
    615   1.2      rin #endif
    616   1.2      rin 
    617   1.2      rin 	/* fallback to MSI */
    618   1.2      rin 	sc->sc_nintrs = sc->sc_nqueues = 1;
    619   1.2      rin 
    620   1.2      rin #ifndef IGC_DISABLE_MSI
    621   1.2      rin 	error = pci_msi_alloc_exact(pa, &sc->sc_intrs, sc->sc_nintrs);
    622   1.2      rin 	if (!error) {
    623   1.2      rin 		sc->sc_intr_type = PCI_INTR_TYPE_MSI;
    624   1.2      rin 		return 0;
    625   1.2      rin 	}
    626   1.2      rin #endif
    627   1.2      rin 
    628   1.2      rin 	/* fallback to INTx */
    629   1.1      rin 
    630   1.2      rin 	error = pci_intx_alloc(pa, &sc->sc_intrs);
    631   1.2      rin 	if (!error) {
    632   1.2      rin 		sc->sc_intr_type = PCI_INTR_TYPE_INTX;
    633   1.2      rin 		return 0;
    634   1.2      rin 	}
    635   1.2      rin 
    636   1.2      rin 	return error;
    637   1.1      rin }
    638   1.1      rin 
    639   1.2      rin static int
    640   1.1      rin igc_allocate_queues(struct igc_softc *sc)
    641   1.1      rin {
    642   1.2      rin 	device_t dev = sc->sc_dev;
    643   1.2      rin 	int rxconf = 0, txconf = 0;
    644   1.1      rin 
    645   1.1      rin 	/* Allocate the top level queue structs. */
    646   1.2      rin 	sc->queues =
    647   1.2      rin 	    kmem_zalloc(sc->sc_nqueues * sizeof(struct igc_queue), KM_SLEEP);
    648   1.1      rin 
    649   1.1      rin 	/* Allocate the TX ring. */
    650   1.2      rin 	sc->tx_rings =
    651   1.2      rin 	    kmem_zalloc(sc->sc_nqueues * sizeof(struct tx_ring), KM_SLEEP);
    652   1.2      rin 
    653   1.1      rin 	/* Allocate the RX ring. */
    654   1.2      rin 	sc->rx_rings =
    655   1.2      rin 	    kmem_zalloc(sc->sc_nqueues * sizeof(struct rx_ring), KM_SLEEP);
    656   1.1      rin 
    657   1.2      rin 	/* Set up the TX queues. */
    658   1.2      rin 	for (int iq = 0; iq < sc->sc_nqueues; iq++, txconf++) {
    659   1.2      rin 		struct tx_ring *txr = &sc->tx_rings[iq];
    660   1.2      rin 		const int tsize = roundup2(
    661   1.2      rin 		    sc->num_tx_desc * sizeof(union igc_adv_tx_desc),
    662   1.2      rin 		    IGC_DBA_ALIGN);
    663   1.1      rin 
    664   1.1      rin 		txr->sc = sc;
    665   1.2      rin 		txr->txr_igcq = &sc->queues[iq];
    666   1.2      rin 		txr->me = iq;
    667   1.1      rin 		if (igc_dma_malloc(sc, tsize, &txr->txdma)) {
    668   1.2      rin 			aprint_error_dev(dev,
    669   1.2      rin 			    "unable to allocate TX descriptor\n");
    670   1.2      rin 			goto fail;
    671   1.1      rin 		}
    672   1.1      rin 		txr->tx_base = (union igc_adv_tx_desc *)txr->txdma.dma_vaddr;
    673   1.2      rin 		memset(txr->tx_base, 0, tsize);
    674   1.2      rin 	}
    675   1.2      rin 
    676   1.2      rin 	/* Prepare transmit descriptors and buffers. */
    677   1.2      rin 	if (igc_setup_transmit_structures(sc)) {
    678   1.2      rin 		aprint_error_dev(dev, "unable to setup transmit structures\n");
    679   1.2      rin 		goto fail;
    680   1.1      rin 	}
    681   1.1      rin 
    682   1.1      rin 	/* Set up the RX queues. */
    683   1.2      rin 	for (int iq = 0; iq < sc->sc_nqueues; iq++, rxconf++) {
    684   1.2      rin 		struct rx_ring *rxr = &sc->rx_rings[iq];
    685   1.2      rin 		const int rsize = roundup2(
    686   1.2      rin 		    sc->num_rx_desc * sizeof(union igc_adv_rx_desc),
    687   1.2      rin 		    IGC_DBA_ALIGN);
    688   1.2      rin 
    689   1.1      rin 		rxr->sc = sc;
    690   1.2      rin 		rxr->rxr_igcq = &sc->queues[iq];
    691   1.2      rin 		rxr->me = iq;
    692   1.2      rin #ifdef OPENBSD
    693   1.1      rin 		timeout_set(&rxr->rx_refill, igc_rxrefill, rxr);
    694   1.2      rin #endif
    695   1.1      rin 		if (igc_dma_malloc(sc, rsize, &rxr->rxdma)) {
    696   1.2      rin 			aprint_error_dev(dev,
    697   1.2      rin 			    "unable to allocate RX descriptor\n");
    698   1.2      rin 			goto fail;
    699   1.1      rin 		}
    700   1.1      rin 		rxr->rx_base = (union igc_adv_rx_desc *)rxr->rxdma.dma_vaddr;
    701   1.2      rin 		memset(rxr->rx_base, 0, rsize);
    702   1.2      rin 	}
    703   1.2      rin 
    704   1.2      rin 	sc->rx_mbuf_sz = MCLBYTES;
    705   1.2      rin 	/* Prepare receive descriptors and buffers. */
    706   1.2      rin 	if (igc_setup_receive_structures(sc)) {
    707   1.2      rin 		aprint_error_dev(sc->sc_dev,
    708   1.2      rin 		    "unable to setup receive structures\n");
    709   1.2      rin 		goto fail;
    710   1.1      rin 	}
    711   1.1      rin 
    712   1.1      rin 	/* Set up the queue holding structs. */
    713   1.2      rin 	for (int iq = 0; iq < sc->sc_nqueues; iq++) {
    714   1.2      rin 		struct igc_queue *q = &sc->queues[iq];
    715   1.2      rin 
    716   1.2      rin 		q->sc = sc;
    717   1.2      rin 		q->txr = &sc->tx_rings[iq];
    718   1.2      rin 		q->rxr = &sc->rx_rings[iq];
    719   1.1      rin 	}
    720   1.1      rin 
    721   1.1      rin 	return 0;
    722   1.1      rin 
    723   1.2      rin  fail:
    724   1.2      rin 	for (struct rx_ring *rxr = sc->rx_rings; rxconf > 0; rxr++, rxconf--)
    725   1.1      rin 		igc_dma_free(sc, &rxr->rxdma);
    726   1.2      rin 	for (struct tx_ring *txr = sc->tx_rings; txconf > 0; txr++, txconf--)
    727   1.1      rin 		igc_dma_free(sc, &txr->txdma);
    728   1.2      rin 
    729   1.2      rin 	kmem_free(sc->rx_rings, sc->sc_nqueues * sizeof(struct rx_ring));
    730   1.1      rin 	sc->rx_rings = NULL;
    731   1.2      rin 	kmem_free(sc->tx_rings, sc->sc_nqueues * sizeof(struct tx_ring));
    732   1.1      rin 	sc->tx_rings = NULL;
    733   1.2      rin 	kmem_free(sc->queues, sc->sc_nqueues * sizeof(struct igc_queue));
    734   1.2      rin 	sc->queues = NULL;
    735   1.2      rin 
    736   1.1      rin 	return ENOMEM;
    737   1.1      rin }
    738   1.1      rin 
    739   1.2      rin static void
    740   1.1      rin igc_free_pci_resources(struct igc_softc *sc)
    741   1.1      rin {
    742   1.1      rin 	struct igc_osdep *os = &sc->osdep;
    743   1.1      rin 
    744   1.1      rin 	if (os->os_membase != 0)
    745   1.1      rin 		bus_space_unmap(os->os_memt, os->os_memh, os->os_memsize);
    746   1.1      rin 	os->os_membase = 0;
    747   1.1      rin }
    748   1.1      rin 
    749   1.2      rin static void
    750   1.2      rin igc_free_interrupts(struct igc_softc *sc)
    751   1.2      rin {
    752   1.2      rin 	struct pci_attach_args *pa = &sc->osdep.os_pa;
    753   1.2      rin 	pci_chipset_tag_t pc = pa->pa_pc;
    754   1.2      rin 
    755   1.2      rin 	for (int i = 0; i < sc->sc_nintrs; i++) {
    756   1.2      rin 		if (sc->sc_ihs[i] != NULL) {
    757   1.2      rin 			pci_intr_disestablish(pc, sc->sc_ihs[i]);
    758   1.2      rin 			sc->sc_ihs[i] = NULL;
    759   1.2      rin 		}
    760   1.2      rin 	}
    761   1.2      rin 	pci_intr_release(pc, sc->sc_intrs, sc->sc_nintrs);
    762   1.2      rin }
    763   1.2      rin 
    764   1.2      rin static void
    765   1.2      rin igc_free_queues(struct igc_softc *sc)
    766   1.2      rin {
    767   1.2      rin 
    768   1.2      rin 	igc_free_receive_structures(sc);
    769   1.2      rin 	for (int iq = 0; iq < sc->sc_nqueues; iq++) {
    770   1.2      rin 		struct rx_ring *rxr = &sc->rx_rings[iq];
    771   1.2      rin 
    772   1.2      rin 		igc_dma_free(sc, &rxr->rxdma);
    773   1.2      rin 	}
    774   1.2      rin 
    775   1.2      rin 	igc_free_transmit_structures(sc);
    776   1.2      rin 	for (int iq = 0; iq < sc->sc_nqueues; iq++) {
    777   1.2      rin 		struct tx_ring *txr = &sc->tx_rings[iq];
    778   1.2      rin 
    779   1.2      rin 		igc_dma_free(sc, &txr->txdma);
    780   1.2      rin 	}
    781   1.2      rin 
    782   1.2      rin 	kmem_free(sc->rx_rings, sc->sc_nqueues * sizeof(struct rx_ring));
    783   1.2      rin 	kmem_free(sc->tx_rings, sc->sc_nqueues * sizeof(struct tx_ring));
    784   1.2      rin 	kmem_free(sc->queues, sc->sc_nqueues * sizeof(struct igc_queue));
    785   1.2      rin }
    786   1.2      rin 
    787   1.1      rin /*********************************************************************
    788   1.1      rin  *
    789   1.1      rin  *  Initialize the hardware to a configuration as specified by the
    790   1.1      rin  *  adapter structure.
    791   1.1      rin  *
    792   1.1      rin  **********************************************************************/
    793   1.2      rin static void
    794   1.1      rin igc_reset(struct igc_softc *sc)
    795   1.1      rin {
    796   1.1      rin 	struct igc_hw *hw = &sc->hw;
    797   1.1      rin 
    798   1.1      rin 	/* Let the firmware know the OS is in control */
    799   1.1      rin 	igc_get_hw_control(sc);
    800   1.1      rin 
    801   1.1      rin 	/*
    802   1.1      rin 	 * Packet Buffer Allocation (PBA)
    803   1.1      rin 	 * Writing PBA sets the receive portion of the buffer
    804   1.1      rin 	 * the remainder is used for the transmit buffer.
    805   1.1      rin 	 */
    806   1.2      rin 	const uint32_t pba = IGC_PBA_34K;
    807   1.1      rin 
    808   1.1      rin 	/*
    809   1.1      rin 	 * These parameters control the automatic generation (Tx) and
    810   1.1      rin 	 * response (Rx) to Ethernet PAUSE frames.
    811   1.1      rin 	 * - High water mark should allow for at least two frames to be
    812   1.1      rin 	 *   received after sending an XOFF.
    813   1.1      rin 	 * - Low water mark works best when it is very near the high water mark.
    814   1.1      rin 	 *   This allows the receiver to restart by sending XON when it has
    815   1.1      rin 	 *   drained a bit. Here we use an arbitrary value of 1500 which will
    816   1.1      rin 	 *   restart after one full frame is pulled from the buffer. There
    817   1.1      rin 	 *   could be several smaller frames in the buffer and if so they will
    818   1.1      rin 	 *   not trigger the XON until their total number reduces the buffer
    819   1.1      rin 	 *   by 1500.
    820   1.1      rin 	 * - The pause time is fairly large at 1000 x 512ns = 512 usec.
    821   1.1      rin 	 */
    822   1.2      rin 	const uint16_t rx_buffer_size = (pba & 0xffff) << 10;
    823   1.2      rin 
    824   1.1      rin 	hw->fc.high_water = rx_buffer_size -
    825   1.1      rin 	    roundup2(sc->hw.mac.max_frame_size, 1024);
    826   1.1      rin 	/* 16-byte granularity */
    827   1.1      rin 	hw->fc.low_water = hw->fc.high_water - 16;
    828   1.1      rin 
    829   1.1      rin 	if (sc->fc) /* locally set flow control value? */
    830   1.1      rin 		hw->fc.requested_mode = sc->fc;
    831   1.1      rin 	else
    832   1.1      rin 		hw->fc.requested_mode = igc_fc_full;
    833   1.1      rin 
    834   1.1      rin 	hw->fc.pause_time = IGC_FC_PAUSE_TIME;
    835   1.1      rin 
    836   1.1      rin 	hw->fc.send_xon = true;
    837   1.1      rin 
    838   1.1      rin 	/* Issue a global reset */
    839   1.1      rin 	igc_reset_hw(hw);
    840   1.1      rin 	IGC_WRITE_REG(hw, IGC_WUC, 0);
    841   1.1      rin 
    842   1.1      rin 	/* and a re-init */
    843   1.1      rin 	if (igc_init_hw(hw) < 0) {
    844   1.2      rin 		aprint_error_dev(sc->sc_dev, "unable to reset hardware\n");
    845   1.1      rin 		return;
    846   1.1      rin 	}
    847   1.1      rin 
    848   1.1      rin 	/* Setup DMA Coalescing */
    849   1.1      rin 	igc_init_dmac(sc, pba);
    850   1.1      rin 
    851   1.1      rin 	IGC_WRITE_REG(hw, IGC_VET, ETHERTYPE_VLAN);
    852   1.1      rin 	igc_get_phy_info(hw);
    853   1.1      rin 	igc_check_for_link(hw);
    854   1.1      rin }
    855   1.1      rin 
    856   1.1      rin /*********************************************************************
    857   1.1      rin  *
    858   1.1      rin  *  Initialize the DMA Coalescing feature
    859   1.1      rin  *
    860   1.1      rin  **********************************************************************/
    861   1.2      rin static void
    862   1.1      rin igc_init_dmac(struct igc_softc *sc, uint32_t pba)
    863   1.1      rin {
    864   1.1      rin 	struct igc_hw *hw = &sc->hw;
    865   1.2      rin 	const uint16_t max_frame_size = sc->hw.mac.max_frame_size;
    866   1.2      rin 	uint32_t reg, status;
    867   1.1      rin 
    868   1.1      rin 	if (sc->dmac == 0) { /* Disabling it */
    869   1.2      rin 		reg = ~IGC_DMACR_DMAC_EN;	/* XXXRO */
    870   1.1      rin 		IGC_WRITE_REG(hw, IGC_DMACR, reg);
    871   1.2      rin 		DPRINTF(MISC, "DMA coalescing disabled\n");
    872   1.1      rin 		return;
    873   1.2      rin 	} else {
    874   1.2      rin 		device_printf(sc->sc_dev, "DMA coalescing enabled\n");
    875   1.2      rin 	}
    876   1.1      rin 
    877   1.1      rin 	/* Set starting threshold */
    878   1.1      rin 	IGC_WRITE_REG(hw, IGC_DMCTXTH, 0);
    879   1.1      rin 
    880   1.2      rin 	uint16_t hwm = 64 * pba - max_frame_size / 16;
    881   1.1      rin 	if (hwm < 64 * (pba - 6))
    882   1.1      rin 		hwm = 64 * (pba - 6);
    883   1.1      rin 	reg = IGC_READ_REG(hw, IGC_FCRTC);
    884   1.1      rin 	reg &= ~IGC_FCRTC_RTH_COAL_MASK;
    885   1.2      rin 	reg |= (hwm << IGC_FCRTC_RTH_COAL_SHIFT) & IGC_FCRTC_RTH_COAL_MASK;
    886   1.1      rin 	IGC_WRITE_REG(hw, IGC_FCRTC, reg);
    887   1.1      rin 
    888   1.2      rin 	uint32_t dmac = pba - max_frame_size / 512;
    889   1.1      rin 	if (dmac < pba - 10)
    890   1.1      rin 		dmac = pba - 10;
    891   1.1      rin 	reg = IGC_READ_REG(hw, IGC_DMACR);
    892   1.1      rin 	reg &= ~IGC_DMACR_DMACTHR_MASK;
    893   1.2      rin 	reg |= (dmac << IGC_DMACR_DMACTHR_SHIFT) & IGC_DMACR_DMACTHR_MASK;
    894   1.1      rin 
    895   1.1      rin 	/* transition to L0x or L1 if available..*/
    896   1.2      rin 	reg |= IGC_DMACR_DMAC_EN | IGC_DMACR_DMAC_LX_MASK;
    897   1.1      rin 
    898   1.1      rin 	/* Check if status is 2.5Gb backplane connection
    899   1.1      rin 	 * before configuration of watchdog timer, which is
    900   1.1      rin 	 * in msec values in 12.8usec intervals
    901   1.1      rin 	 * watchdog timer= msec values in 32usec intervals
    902   1.1      rin 	 * for non 2.5Gb connection
    903   1.1      rin 	 */
    904   1.1      rin 	status = IGC_READ_REG(hw, IGC_STATUS);
    905   1.1      rin 	if ((status & IGC_STATUS_2P5_SKU) &&
    906   1.2      rin 	    !(status & IGC_STATUS_2P5_SKU_OVER))
    907   1.2      rin 		reg |= (sc->dmac * 5) >> 6;
    908   1.1      rin 	else
    909   1.2      rin 		reg |= sc->dmac >> 5;
    910   1.1      rin 
    911   1.1      rin 	IGC_WRITE_REG(hw, IGC_DMACR, reg);
    912   1.1      rin 
    913   1.1      rin 	IGC_WRITE_REG(hw, IGC_DMCRTRH, 0);
    914   1.1      rin 
    915   1.1      rin 	/* Set the interval before transition */
    916   1.1      rin 	reg = IGC_READ_REG(hw, IGC_DMCTLX);
    917   1.1      rin 	reg |= IGC_DMCTLX_DCFLUSH_DIS;
    918   1.1      rin 
    919   1.1      rin 	/*
    920   1.2      rin 	 * in 2.5Gb connection, TTLX unit is 0.4 usec
    921   1.2      rin 	 * which is 0x4*2 = 0xA. But delay is still 4 usec
    922   1.2      rin 	 */
    923   1.1      rin 	status = IGC_READ_REG(hw, IGC_STATUS);
    924   1.1      rin 	if ((status & IGC_STATUS_2P5_SKU) &&
    925   1.2      rin 	    !(status & IGC_STATUS_2P5_SKU_OVER))
    926   1.1      rin 		reg |= 0xA;
    927   1.1      rin 	else
    928   1.1      rin 		reg |= 0x4;
    929   1.1      rin 
    930   1.1      rin 	IGC_WRITE_REG(hw, IGC_DMCTLX, reg);
    931   1.1      rin 
    932   1.1      rin 	/* free space in tx packet buffer to wake from DMA coal */
    933   1.2      rin 	IGC_WRITE_REG(hw, IGC_DMCTXTH,
    934   1.2      rin 	    (IGC_TXPBSIZE - (2 * max_frame_size)) >> 6);
    935   1.1      rin 
    936   1.1      rin 	/* make low power state decision controlled by DMA coal */
    937   1.1      rin 	reg = IGC_READ_REG(hw, IGC_PCIEMISC);
    938   1.1      rin 	reg &= ~IGC_PCIEMISC_LX_DECISION;
    939   1.1      rin 	IGC_WRITE_REG(hw, IGC_PCIEMISC, reg);
    940   1.1      rin }
    941   1.1      rin 
    942   1.2      rin static int
    943   1.2      rin igc_setup_interrupts(struct igc_softc *sc)
    944   1.2      rin {
    945   1.2      rin 	int error;
    946   1.2      rin 
    947   1.2      rin 	switch (sc->sc_intr_type) {
    948   1.2      rin 	case PCI_INTR_TYPE_MSIX:
    949   1.2      rin 		error = igc_setup_msix(sc);
    950   1.2      rin 		break;
    951   1.2      rin 	case PCI_INTR_TYPE_MSI:
    952   1.2      rin 		error = igc_setup_msi(sc);
    953   1.2      rin 		break;
    954   1.2      rin 	case PCI_INTR_TYPE_INTX:
    955   1.2      rin 		error = igc_setup_intx(sc);
    956   1.2      rin 		break;
    957   1.2      rin 	default:
    958   1.2      rin 		panic("%s: invalid interrupt type: %d",
    959   1.2      rin 		    device_xname(sc->sc_dev), sc->sc_intr_type);
    960   1.2      rin 	}
    961   1.2      rin 
    962   1.2      rin 	return error;
    963   1.2      rin }
    964   1.2      rin 
    965   1.2      rin static void
    966   1.2      rin igc_attach_counters(struct igc_softc *sc)
    967   1.1      rin {
    968   1.2      rin #ifdef IGC_EVENT_COUNTERS
    969   1.2      rin 
    970   1.2      rin 	/* Global counters */
    971   1.2      rin 	sc->sc_global_evcnts = kmem_zalloc(
    972   1.2      rin 	    IGC_GLOBAL_COUNTERS * sizeof(sc->sc_global_evcnts[0]), KM_SLEEP);
    973   1.2      rin 
    974   1.2      rin 	for (int cnt = 0; cnt < IGC_GLOBAL_COUNTERS; cnt++) {
    975   1.2      rin 		evcnt_attach_dynamic(&sc->sc_global_evcnts[cnt],
    976   1.2      rin 		    igc_global_counters[cnt].type, NULL,
    977   1.2      rin 		    device_xname(sc->sc_dev), igc_global_counters[cnt].name);
    978   1.2      rin 	}
    979   1.2      rin 
    980   1.2      rin 	/* Driver counters */
    981   1.2      rin 	sc->sc_driver_evcnts = kmem_zalloc(
    982   1.2      rin 	    IGC_DRIVER_COUNTERS * sizeof(sc->sc_driver_evcnts[0]), KM_SLEEP);
    983   1.2      rin 
    984   1.2      rin 	for (int cnt = 0; cnt < IGC_DRIVER_COUNTERS; cnt++) {
    985   1.2      rin 		evcnt_attach_dynamic(&sc->sc_driver_evcnts[cnt],
    986   1.2      rin 		    igc_driver_counters[cnt].type, NULL,
    987   1.2      rin 		    device_xname(sc->sc_dev), igc_driver_counters[cnt].name);
    988   1.2      rin 	}
    989   1.2      rin 
    990   1.2      rin 	for (int iq = 0; iq < sc->sc_nqueues; iq++) {
    991   1.2      rin 		struct igc_queue *q = &sc->queues[iq];
    992   1.2      rin 
    993   1.2      rin 		q->igcq_driver_counters = kmem_zalloc(
    994   1.2      rin 		    IGC_DRIVER_COUNTERS * sizeof(q->igcq_driver_counters[0]),
    995   1.2      rin 		    KM_SLEEP);
    996   1.2      rin 	}
    997   1.2      rin 
    998   1.2      rin 	/* Queue counters */
    999   1.2      rin 	for (int iq = 0; iq < sc->sc_nqueues; iq++) {
   1000   1.2      rin 		struct igc_queue *q = &sc->queues[iq];
   1001   1.2      rin 
   1002   1.2      rin 		snprintf(q->igcq_queue_evname, sizeof(q->igcq_queue_evname),
   1003   1.2      rin 		    "%s q%d", device_xname(sc->sc_dev), iq);
   1004   1.2      rin 
   1005   1.2      rin 		q->igcq_queue_evcnts = kmem_zalloc(
   1006   1.2      rin 		    IGC_QUEUE_COUNTERS * sizeof(q->igcq_queue_evcnts[0]),
   1007   1.2      rin 		    KM_SLEEP);
   1008   1.2      rin 
   1009   1.2      rin 		for (int cnt = 0; cnt < IGC_QUEUE_COUNTERS; cnt++) {
   1010   1.2      rin 			evcnt_attach_dynamic(&q->igcq_queue_evcnts[cnt],
   1011   1.2      rin 			    igc_queue_counters[cnt].type, NULL,
   1012   1.2      rin 			    q->igcq_queue_evname, igc_queue_counters[cnt].name);
   1013   1.1      rin 		}
   1014   1.2      rin 	}
   1015   1.2      rin 
   1016   1.2      rin 	/* MAC counters */
   1017   1.2      rin 	snprintf(sc->sc_mac_evname, sizeof(sc->sc_mac_evname),
   1018   1.2      rin 	    "%s Mac Statistics", device_xname(sc->sc_dev));
   1019   1.2      rin 
   1020   1.2      rin 	sc->sc_mac_evcnts = kmem_zalloc(
   1021   1.2      rin 	    IGC_MAC_COUNTERS * sizeof(sc->sc_mac_evcnts[0]), KM_SLEEP);
   1022   1.2      rin 
   1023   1.2      rin 	for (int cnt = 0; cnt < IGC_MAC_COUNTERS; cnt++) {
   1024   1.2      rin 		evcnt_attach_dynamic(&sc->sc_mac_evcnts[cnt], EVCNT_TYPE_MISC,
   1025   1.2      rin 		    NULL, sc->sc_mac_evname, igc_mac_counters[cnt].name);
   1026   1.2      rin 	}
   1027   1.2      rin #endif
   1028   1.2      rin }
   1029   1.2      rin 
   1030   1.2      rin static void
   1031   1.2      rin igc_detach_counters(struct igc_softc *sc)
   1032   1.2      rin {
   1033   1.2      rin #ifdef IGC_EVENT_COUNTERS
   1034   1.2      rin 
   1035   1.2      rin 	/* Global counters */
   1036   1.2      rin 	for (int cnt = 0; cnt < IGC_GLOBAL_COUNTERS; cnt++)
   1037   1.2      rin 		evcnt_detach(&sc->sc_global_evcnts[cnt]);
   1038   1.2      rin 
   1039   1.2      rin 	kmem_free(sc->sc_global_evcnts,
   1040   1.2      rin 	    IGC_GLOBAL_COUNTERS * sizeof(sc->sc_global_evcnts));
   1041   1.2      rin 
   1042   1.2      rin 	/* Driver counters */
   1043   1.2      rin 	for (int iq = 0; iq < sc->sc_nqueues; iq++) {
   1044   1.2      rin 		struct igc_queue *q = &sc->queues[iq];
   1045   1.2      rin 
   1046   1.2      rin 		kmem_free(q->igcq_driver_counters,
   1047   1.2      rin 		    IGC_DRIVER_COUNTERS * sizeof(q->igcq_driver_counters[0]));
   1048   1.2      rin 	}
   1049   1.2      rin 
   1050   1.2      rin 	for (int cnt = 0; cnt < IGC_DRIVER_COUNTERS; cnt++)
   1051   1.2      rin 		evcnt_detach(&sc->sc_driver_evcnts[cnt]);
   1052   1.2      rin 
   1053   1.2      rin 	kmem_free(sc->sc_driver_evcnts,
   1054   1.2      rin 	    IGC_DRIVER_COUNTERS * sizeof(sc->sc_driver_evcnts));
   1055   1.2      rin 
   1056   1.2      rin 	/* Queue counters */
   1057   1.2      rin 	for (int iq = 0; iq < sc->sc_nqueues; iq++) {
   1058   1.2      rin 		struct igc_queue *q = &sc->queues[iq];
   1059   1.2      rin 
   1060   1.2      rin 		for (int cnt = 0; cnt < IGC_QUEUE_COUNTERS; cnt++)
   1061   1.2      rin 			evcnt_detach(&q->igcq_queue_evcnts[cnt]);
   1062   1.2      rin 
   1063   1.2      rin 		kmem_free(q->igcq_queue_evcnts,
   1064   1.2      rin 		    IGC_QUEUE_COUNTERS * sizeof(q->igcq_queue_evcnts[0]));
   1065   1.2      rin 	}
   1066   1.2      rin 
   1067   1.2      rin 	/* MAC statistics */
   1068   1.2      rin 	for (int cnt = 0; cnt < IGC_MAC_COUNTERS; cnt++)
   1069   1.2      rin 		evcnt_detach(&sc->sc_mac_evcnts[cnt]);
   1070   1.2      rin 
   1071   1.2      rin 	kmem_free(sc->sc_mac_evcnts,
   1072   1.2      rin 	    IGC_MAC_COUNTERS * sizeof(sc->sc_mac_evcnts[0]));
   1073   1.2      rin #endif
   1074   1.2      rin }
   1075   1.2      rin 
   1076   1.2      rin /*
   1077   1.2      rin  * XXX
   1078   1.2      rin  * FreeBSD uses 4-byte-wise read for 64-bit counters, while Linux just
   1079   1.2      rin  * drops hi words.
   1080   1.2      rin  */
   1081   1.2      rin static inline uint64_t __unused
   1082   1.2      rin igc_read_mac_counter(struct igc_hw *hw, bus_size_t reg, bool is64)
   1083   1.2      rin {
   1084   1.2      rin 	uint64_t val;
   1085   1.2      rin 
   1086   1.2      rin 	val = IGC_READ_REG(hw, reg);
   1087   1.2      rin 	if (is64)
   1088   1.2      rin 		val += ((uint64_t)IGC_READ_REG(hw, reg + 4)) << 32;
   1089   1.2      rin 	return val;
   1090   1.2      rin }
   1091   1.2      rin 
   1092   1.2      rin static void
   1093   1.2      rin igc_update_counters(struct igc_softc *sc)
   1094   1.2      rin {
   1095   1.2      rin #ifdef IGC_EVENT_COUNTERS
   1096   1.2      rin 
   1097   1.2      rin 	/* Global counters: nop */
   1098   1.2      rin 
   1099   1.2      rin 	/* Driver counters */
   1100   1.2      rin 	uint64_t sum[IGC_DRIVER_COUNTERS];
   1101   1.1      rin 
   1102   1.2      rin 	memset(sum, 0, sizeof(sum));
   1103   1.2      rin 	for (int iq = 0; iq < sc->sc_nqueues; iq++) {
   1104   1.2      rin 		struct igc_queue *q = &sc->queues[iq];
   1105   1.2      rin 
   1106   1.2      rin 		for (int cnt = 0; cnt < IGC_DRIVER_COUNTERS; cnt++) {
   1107   1.2      rin 			sum[cnt] += IGC_QUEUE_DRIVER_COUNTER_VAL(q, cnt);
   1108   1.2      rin 			IGC_QUEUE_DRIVER_COUNTER_STORE(q, cnt, 0);
   1109   1.1      rin 		}
   1110   1.2      rin 	}
   1111   1.2      rin 
   1112   1.2      rin 	for (int cnt = 0; cnt < IGC_DRIVER_COUNTERS; cnt++)
   1113   1.2      rin 		IGC_DRIVER_COUNTER_ADD(sc, cnt, sum[cnt]);
   1114   1.2      rin 
   1115   1.2      rin 	/* Queue counters: nop */
   1116   1.1      rin 
   1117   1.2      rin 	/* Mac statistics */
   1118   1.2      rin 	struct igc_hw *hw = &sc->hw;
   1119  1.10  msaitoh 	struct ifnet *ifp = &sc->sc_ec.ec_if;
   1120  1.10  msaitoh 	uint64_t iqdrops = 0;
   1121   1.2      rin 
   1122   1.2      rin 	for (int cnt = 0; cnt < IGC_MAC_COUNTERS; cnt++) {
   1123  1.10  msaitoh 		uint64_t val;
   1124  1.10  msaitoh 		bus_size_t regaddr = igc_mac_counters[cnt].reg;
   1125  1.10  msaitoh 
   1126  1.10  msaitoh 		val = igc_read_mac_counter(hw, regaddr,
   1127  1.10  msaitoh 		    igc_mac_counters[cnt].is64);
   1128  1.10  msaitoh 		IGC_MAC_COUNTER_ADD(sc, cnt, val);
   1129  1.10  msaitoh 		/* XXX Count MPC to iqdrops. */
   1130  1.10  msaitoh 		if (regaddr == IGC_MPC)
   1131  1.10  msaitoh 			iqdrops += val;
   1132   1.2      rin 	}
   1133  1.10  msaitoh 
   1134  1.10  msaitoh 	for (int iq = 0; iq < sc->sc_nqueues; iq++) {
   1135  1.10  msaitoh 		uint32_t val;
   1136  1.10  msaitoh 
   1137  1.10  msaitoh 		/* XXX RQDPC should be visible via evcnt(9). */
   1138  1.10  msaitoh 		val = IGC_READ_REG(hw, IGC_RQDPC(iq));
   1139  1.10  msaitoh 
   1140  1.10  msaitoh 		/* RQDPC is not cleard on read. */
   1141  1.10  msaitoh 		if (val != 0)
   1142  1.10  msaitoh 			IGC_WRITE_REG(hw, IGC_RQDPC(iq), 0);
   1143  1.10  msaitoh 		iqdrops += val;
   1144  1.10  msaitoh 	}
   1145  1.10  msaitoh 
   1146  1.10  msaitoh 	if (iqdrops != 0)
   1147  1.10  msaitoh 		if_statadd(ifp, if_iqdrops, iqdrops);
   1148   1.2      rin #endif
   1149   1.2      rin }
   1150   1.2      rin 
   1151   1.2      rin static void
   1152   1.2      rin igc_clear_counters(struct igc_softc *sc)
   1153   1.2      rin {
   1154   1.2      rin #ifdef IGC_EVENT_COUNTERS
   1155   1.2      rin 
   1156   1.2      rin 	/* Global counters */
   1157   1.2      rin 	for (int cnt = 0; cnt < IGC_GLOBAL_COUNTERS; cnt++)
   1158   1.2      rin 		IGC_GLOBAL_COUNTER_STORE(sc, cnt, 0);
   1159   1.2      rin 
   1160   1.2      rin 	/* Driver counters */
   1161   1.2      rin 	for (int iq = 0; iq < sc->sc_nqueues; iq++) {
   1162   1.2      rin 		struct igc_queue *q = &sc->queues[iq];
   1163   1.2      rin 
   1164   1.2      rin 		for (int cnt = 0; cnt < IGC_DRIVER_COUNTERS; cnt++)
   1165   1.2      rin 			IGC_QUEUE_DRIVER_COUNTER_STORE(q, cnt, 0);
   1166   1.1      rin 	}
   1167   1.1      rin 
   1168   1.2      rin 	for (int cnt = 0; cnt < IGC_DRIVER_COUNTERS; cnt++)
   1169   1.2      rin 		IGC_DRIVER_COUNTER_STORE(sc, cnt, 0);
   1170   1.2      rin 
   1171   1.2      rin 	/* Queue counters */
   1172   1.2      rin 	for (int iq = 0; iq < sc->sc_nqueues; iq++) {
   1173   1.2      rin 		struct igc_queue *q = &sc->queues[iq];
   1174   1.2      rin 
   1175   1.2      rin 		for (int cnt = 0; cnt < IGC_QUEUE_COUNTERS; cnt++)
   1176   1.2      rin 			IGC_QUEUE_COUNTER_STORE(q, cnt, 0);
   1177   1.2      rin 	}
   1178   1.2      rin 
   1179   1.2      rin 	/* Mac statistics */
   1180   1.2      rin 	struct igc_hw *hw = &sc->hw;
   1181   1.2      rin 
   1182   1.2      rin 	for (int cnt = 0; cnt < IGC_MAC_COUNTERS; cnt++) {
   1183   1.2      rin 		(void)igc_read_mac_counter(hw, igc_mac_counters[cnt].reg,
   1184   1.2      rin 		    igc_mac_counters[cnt].is64);
   1185   1.2      rin 		IGC_MAC_COUNTER_STORE(sc, cnt, 0);
   1186   1.2      rin 	}
   1187   1.2      rin #endif
   1188   1.2      rin }
   1189   1.2      rin 
   1190   1.2      rin static int
   1191   1.2      rin igc_setup_msix(struct igc_softc *sc)
   1192   1.2      rin {
   1193   1.2      rin 	pci_chipset_tag_t pc = sc->osdep.os_pa.pa_pc;
   1194   1.2      rin 	device_t dev = sc->sc_dev;
   1195   1.2      rin 	pci_intr_handle_t *intrs;
   1196   1.2      rin 	void **ihs;
   1197   1.2      rin 	const char *intrstr;
   1198   1.2      rin 	char intrbuf[PCI_INTRSTR_LEN];
   1199   1.2      rin 	char xnamebuf[MAX(32, MAXCOMLEN)];
   1200   1.2      rin 	int iq, error;
   1201   1.2      rin 
   1202   1.2      rin 	for (iq = 0, intrs = sc->sc_intrs, ihs = sc->sc_ihs;
   1203   1.2      rin 	    iq < sc->sc_nqueues; iq++, intrs++, ihs++) {
   1204   1.2      rin 		struct igc_queue *q = &sc->queues[iq];
   1205   1.2      rin 
   1206   1.2      rin 		snprintf(xnamebuf, sizeof(xnamebuf), "%s: txrx %d",
   1207   1.2      rin 		    device_xname(dev), iq);
   1208   1.2      rin 
   1209   1.2      rin 		intrstr = pci_intr_string(pc, *intrs, intrbuf, sizeof(intrbuf));
   1210   1.2      rin 
   1211   1.2      rin 		pci_intr_setattr(pc, intrs, PCI_INTR_MPSAFE, true);
   1212   1.2      rin 		*ihs = pci_intr_establish_xname(pc, *intrs, IPL_NET,
   1213   1.2      rin 		    igc_intr_queue, q, xnamebuf);
   1214   1.2      rin 		if (*ihs == NULL) {
   1215   1.2      rin 			aprint_error_dev(dev,
   1216   1.2      rin 			    "unable to establish txrx interrupt at %s\n",
   1217   1.2      rin 			    intrstr);
   1218   1.2      rin 			return ENOBUFS;
   1219   1.2      rin 		}
   1220   1.2      rin 		aprint_normal_dev(dev, "txrx interrupting at %s\n", intrstr);
   1221   1.2      rin 
   1222   1.2      rin 		kcpuset_t *affinity;
   1223   1.2      rin 		kcpuset_create(&affinity, true);
   1224   1.2      rin 		kcpuset_set(affinity, iq % ncpu);
   1225   1.2      rin 		error = interrupt_distribute(*ihs, affinity, NULL);
   1226   1.2      rin 		if (error) {
   1227   1.2      rin 			aprint_normal_dev(dev,
   1228   1.2      rin 			    "%s: unable to change affinity, use default CPU\n",
   1229   1.2      rin 			    intrstr);
   1230   1.2      rin 		}
   1231   1.2      rin 		kcpuset_destroy(affinity);
   1232   1.2      rin 
   1233   1.2      rin 		q->igcq_si = softint_establish(SOFTINT_NET | SOFTINT_MPSAFE,
   1234   1.2      rin 		    igc_handle_queue, q);
   1235   1.2      rin 		if (q->igcq_si == NULL) {
   1236   1.2      rin 			aprint_error_dev(dev,
   1237   1.2      rin 			    "%s: unable to establish softint\n", intrstr);
   1238   1.2      rin 			return ENOBUFS;
   1239   1.2      rin 		}
   1240   1.2      rin 
   1241   1.2      rin 		q->msix = iq;
   1242   1.2      rin 		q->eims = 1 << iq;
   1243   1.1      rin 	}
   1244   1.1      rin 
   1245   1.2      rin 	snprintf(xnamebuf, MAXCOMLEN, "%s_tx_rx", device_xname(dev));
   1246   1.2      rin 	error = workqueue_create(&sc->sc_queue_wq, xnamebuf,
   1247   1.2      rin 	    igc_handle_queue_work, sc, IGC_WORKQUEUE_PRI, IPL_NET,
   1248   1.2      rin 	    WQ_PERCPU | WQ_MPSAFE);
   1249   1.2      rin 	if (error) {
   1250   1.2      rin 		aprint_error_dev(dev, "workqueue_create failed\n");
   1251   1.2      rin 		return ENOBUFS;
   1252   1.1      rin 	}
   1253   1.2      rin 	sc->sc_txrx_workqueue = false;
   1254   1.1      rin 
   1255   1.2      rin 	intrstr = pci_intr_string(pc, *intrs, intrbuf, sizeof(intrbuf));
   1256   1.2      rin 	snprintf(xnamebuf, sizeof(xnamebuf), "%s: link", device_xname(dev));
   1257   1.2      rin 	pci_intr_setattr(pc, intrs, PCI_INTR_MPSAFE, true);
   1258   1.2      rin 	*ihs = pci_intr_establish_xname(pc, *intrs, IPL_NET,
   1259   1.2      rin 	    igc_intr_link, sc, xnamebuf);
   1260   1.2      rin 	if (*ihs == NULL) {
   1261   1.2      rin 		aprint_error_dev(dev,
   1262   1.2      rin 		    "unable to establish link interrupt at %s\n", intrstr);
   1263   1.2      rin 		return ENOBUFS;
   1264   1.2      rin 	}
   1265   1.2      rin 	aprint_normal_dev(dev, "link interrupting at %s\n", intrstr);
   1266   1.2      rin 	/* use later in igc_configure_queues() */
   1267   1.2      rin 	sc->linkvec = iq;
   1268   1.1      rin 
   1269   1.1      rin 	return 0;
   1270   1.2      rin }
   1271   1.2      rin 
   1272   1.2      rin static int
   1273   1.2      rin igc_setup_msi(struct igc_softc *sc)
   1274   1.2      rin {
   1275   1.2      rin 	pci_chipset_tag_t pc = sc->osdep.os_pa.pa_pc;
   1276   1.2      rin 	device_t dev = sc->sc_dev;
   1277   1.2      rin 	pci_intr_handle_t *intr = sc->sc_intrs;
   1278   1.2      rin 	void **ihs = sc->sc_ihs;
   1279   1.2      rin 	const char *intrstr;
   1280   1.2      rin 	char intrbuf[PCI_INTRSTR_LEN];
   1281   1.2      rin 	char xnamebuf[MAX(32, MAXCOMLEN)];
   1282   1.2      rin 	int error;
   1283   1.2      rin 
   1284   1.2      rin 	intrstr = pci_intr_string(pc, *intr, intrbuf, sizeof(intrbuf));
   1285   1.2      rin 
   1286   1.2      rin 	snprintf(xnamebuf, sizeof(xnamebuf), "%s: msi", device_xname(dev));
   1287   1.2      rin 	pci_intr_setattr(pc, intr, PCI_INTR_MPSAFE, true);
   1288   1.2      rin 	*ihs = pci_intr_establish_xname(pc, *intr, IPL_NET,
   1289   1.2      rin 	    igc_intr, sc, xnamebuf);
   1290   1.2      rin 	if (*ihs == NULL) {
   1291   1.2      rin 		aprint_error_dev(dev,
   1292   1.2      rin 		    "unable to establish interrupt at %s\n", intrstr);
   1293   1.2      rin 		return ENOBUFS;
   1294   1.2      rin 	}
   1295   1.2      rin 	aprint_normal_dev(dev, "interrupting at %s\n", intrstr);
   1296   1.2      rin 
   1297   1.2      rin 	struct igc_queue *iq = sc->queues;
   1298   1.2      rin 	iq->igcq_si = softint_establish(SOFTINT_NET | SOFTINT_MPSAFE,
   1299   1.2      rin 	    igc_handle_queue, iq);
   1300   1.2      rin 	if (iq->igcq_si == NULL) {
   1301   1.2      rin 		aprint_error_dev(dev,
   1302   1.2      rin 		    "%s: unable to establish softint\n", intrstr);
   1303   1.2      rin 		return ENOBUFS;
   1304   1.2      rin 	}
   1305   1.2      rin 
   1306   1.2      rin 	snprintf(xnamebuf, MAXCOMLEN, "%s_tx_rx", device_xname(dev));
   1307   1.2      rin 	error = workqueue_create(&sc->sc_queue_wq, xnamebuf,
   1308   1.2      rin 	    igc_handle_queue_work, sc, IGC_WORKQUEUE_PRI, IPL_NET,
   1309   1.2      rin 	    WQ_PERCPU | WQ_MPSAFE);
   1310   1.2      rin 	if (error) {
   1311   1.2      rin 		aprint_error_dev(dev, "workqueue_create failed\n");
   1312   1.2      rin 		return ENOBUFS;
   1313   1.1      rin 	}
   1314   1.2      rin 	sc->sc_txrx_workqueue = false;
   1315   1.2      rin 
   1316   1.2      rin 	sc->queues[0].msix = 0;
   1317   1.2      rin 	sc->linkvec = 0;
   1318   1.1      rin 
   1319   1.2      rin 	return 0;
   1320   1.1      rin }
   1321   1.1      rin 
   1322   1.2      rin static int
   1323   1.2      rin igc_setup_intx(struct igc_softc *sc)
   1324   1.1      rin {
   1325   1.2      rin 	pci_chipset_tag_t pc = sc->osdep.os_pa.pa_pc;
   1326   1.2      rin 	device_t dev = sc->sc_dev;
   1327   1.2      rin 	pci_intr_handle_t *intr = sc->sc_intrs;
   1328   1.2      rin 	void **ihs = sc->sc_ihs;
   1329   1.2      rin 	const char *intrstr;
   1330   1.2      rin 	char intrbuf[PCI_INTRSTR_LEN];
   1331   1.2      rin 	char xnamebuf[32];
   1332   1.2      rin 
   1333   1.2      rin 	intrstr = pci_intr_string(pc, *intr, intrbuf, sizeof(intrbuf));
   1334   1.2      rin 
   1335   1.2      rin 	snprintf(xnamebuf, sizeof(xnamebuf), "%s:intx", device_xname(dev));
   1336   1.2      rin 	pci_intr_setattr(pc, intr, PCI_INTR_MPSAFE, true);
   1337   1.2      rin 	*ihs = pci_intr_establish_xname(pc, *intr, IPL_NET,
   1338   1.2      rin 	    igc_intr, sc, xnamebuf);
   1339   1.2      rin 	if (*ihs == NULL) {
   1340   1.2      rin 		aprint_error_dev(dev,
   1341   1.2      rin 		    "unable to establish interrupt at %s\n", intrstr);
   1342   1.2      rin 		return ENOBUFS;
   1343   1.2      rin 	}
   1344   1.2      rin 	aprint_normal_dev(dev, "interrupting at %s\n", intrstr);
   1345   1.2      rin 
   1346   1.2      rin 	struct igc_queue *iq = sc->queues;
   1347   1.2      rin 	iq->igcq_si = softint_establish(SOFTINT_NET | SOFTINT_MPSAFE,
   1348   1.2      rin 	    igc_handle_queue, iq);
   1349   1.2      rin 	if (iq->igcq_si == NULL) {
   1350   1.2      rin 		aprint_error_dev(dev,
   1351   1.2      rin 		    "%s: unable to establish softint\n", intrstr);
   1352   1.2      rin 		return ENOBUFS;
   1353   1.2      rin 	}
   1354   1.1      rin 
   1355   1.2      rin 	/* create workqueue? */
   1356   1.2      rin 	sc->sc_txrx_workqueue = false;
   1357   1.1      rin 
   1358   1.2      rin 	sc->queues[0].msix = 0;
   1359   1.2      rin 	sc->linkvec = 0;
   1360   1.1      rin 
   1361   1.2      rin 	return 0;
   1362   1.1      rin }
   1363   1.1      rin 
   1364   1.2      rin static int
   1365   1.1      rin igc_dma_malloc(struct igc_softc *sc, bus_size_t size, struct igc_dma_alloc *dma)
   1366   1.1      rin {
   1367   1.1      rin 	struct igc_osdep *os = &sc->osdep;
   1368   1.1      rin 
   1369   1.2      rin 	dma->dma_tag = os->os_dmat;
   1370   1.1      rin 
   1371   1.2      rin 	if (bus_dmamap_create(dma->dma_tag, size, 1, size, 0,
   1372   1.2      rin 	    BUS_DMA_WAITOK | BUS_DMA_ALLOCNOW, &dma->dma_map))
   1373   1.1      rin 		return 1;
   1374   1.1      rin 	if (bus_dmamem_alloc(dma->dma_tag, size, PAGE_SIZE, 0, &dma->dma_seg,
   1375   1.2      rin 	    1, &dma->dma_nseg, BUS_DMA_WAITOK))
   1376   1.1      rin 		goto destroy;
   1377   1.2      rin 	/*
   1378   1.2      rin 	 * XXXRO
   1379   1.2      rin 	 *
   1380   1.2      rin 	 * Coherent mapping for descriptors is required for now.
   1381   1.2      rin 	 *
   1382   1.2      rin 	 * Both TX and RX descriptors are 16-byte length, which is shorter
   1383   1.2      rin 	 * than dcache lines on modern CPUs. Therefore, sync for a descriptor
   1384   1.2      rin 	 * may overwrite DMA read for descriptors in the same cache line.
   1385   1.2      rin 	 *
   1386   1.2      rin 	 * Can't we avoid this by use cache-line-aligned descriptors at once?
   1387   1.2      rin 	 */
   1388   1.1      rin 	if (bus_dmamem_map(dma->dma_tag, &dma->dma_seg, dma->dma_nseg, size,
   1389   1.2      rin 	    &dma->dma_vaddr, BUS_DMA_WAITOK | BUS_DMA_COHERENT /* XXXRO */))
   1390   1.1      rin 		goto free;
   1391   1.1      rin 	if (bus_dmamap_load(dma->dma_tag, dma->dma_map, dma->dma_vaddr, size,
   1392   1.2      rin 	    NULL, BUS_DMA_WAITOK))
   1393   1.1      rin 		goto unmap;
   1394   1.1      rin 
   1395   1.1      rin 	dma->dma_size = size;
   1396   1.1      rin 
   1397   1.1      rin 	return 0;
   1398   1.2      rin  unmap:
   1399   1.1      rin 	bus_dmamem_unmap(dma->dma_tag, dma->dma_vaddr, size);
   1400   1.2      rin  free:
   1401   1.1      rin 	bus_dmamem_free(dma->dma_tag, &dma->dma_seg, dma->dma_nseg);
   1402   1.2      rin  destroy:
   1403   1.1      rin 	bus_dmamap_destroy(dma->dma_tag, dma->dma_map);
   1404   1.1      rin 	dma->dma_map = NULL;
   1405   1.1      rin 	dma->dma_tag = NULL;
   1406   1.1      rin 	return 1;
   1407   1.1      rin }
   1408   1.1      rin 
   1409   1.2      rin static void
   1410   1.1      rin igc_dma_free(struct igc_softc *sc, struct igc_dma_alloc *dma)
   1411   1.1      rin {
   1412   1.2      rin 
   1413   1.1      rin 	if (dma->dma_tag == NULL)
   1414   1.1      rin 		return;
   1415   1.1      rin 
   1416   1.1      rin 	if (dma->dma_map != NULL) {
   1417   1.1      rin 		bus_dmamap_sync(dma->dma_tag, dma->dma_map, 0,
   1418   1.1      rin 		    dma->dma_map->dm_mapsize,
   1419   1.1      rin 		    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
   1420   1.1      rin 		bus_dmamap_unload(dma->dma_tag, dma->dma_map);
   1421   1.1      rin 		bus_dmamem_unmap(dma->dma_tag, dma->dma_vaddr, dma->dma_size);
   1422   1.1      rin 		bus_dmamem_free(dma->dma_tag, &dma->dma_seg, dma->dma_nseg);
   1423   1.1      rin 		bus_dmamap_destroy(dma->dma_tag, dma->dma_map);
   1424   1.1      rin 		dma->dma_map = NULL;
   1425   1.1      rin 	}
   1426   1.1      rin }
   1427   1.1      rin 
   1428   1.1      rin /*********************************************************************
   1429   1.1      rin  *
   1430   1.1      rin  *  Setup networking device structure and register an interface.
   1431   1.1      rin  *
   1432   1.1      rin  **********************************************************************/
   1433   1.2      rin static void
   1434   1.1      rin igc_setup_interface(struct igc_softc *sc)
   1435   1.2      rin {
   1436   1.2      rin 	struct ifnet *ifp = &sc->sc_ec.ec_if;
   1437   1.2      rin 
   1438   1.2      rin 	strlcpy(ifp->if_xname, device_xname(sc->sc_dev), sizeof(ifp->if_xname));
   1439   1.2      rin 	ifp->if_softc = sc;
   1440   1.2      rin 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
   1441   1.2      rin 	ifp->if_extflags = IFEF_MPSAFE;
   1442   1.2      rin 	ifp->if_ioctl = igc_ioctl;
   1443   1.2      rin 	ifp->if_start = igc_start;
   1444   1.2      rin 	if (sc->sc_nqueues > 1)
   1445   1.2      rin 		ifp->if_transmit = igc_transmit;
   1446   1.2      rin 	ifp->if_watchdog = igc_watchdog;
   1447   1.2      rin 	ifp->if_init = igc_init;
   1448   1.2      rin 	ifp->if_stop = igc_stop;
   1449   1.2      rin 
   1450   1.2      rin #if 0 /* notyet */
   1451   1.2      rin 	ifp->if_capabilities = IFCAP_TSOv4 | IFCAP_TSOv6;
   1452   1.2      rin #endif
   1453   1.2      rin 
   1454   1.2      rin 	ifp->if_capabilities |=
   1455   1.2      rin 	    IFCAP_CSUM_IPv4_Tx  | IFCAP_CSUM_IPv4_Rx  |
   1456   1.2      rin 	    IFCAP_CSUM_TCPv4_Tx | IFCAP_CSUM_TCPv4_Rx |
   1457   1.2      rin 	    IFCAP_CSUM_UDPv4_Tx | IFCAP_CSUM_UDPv4_Rx |
   1458   1.2      rin 	    IFCAP_CSUM_TCPv6_Tx | IFCAP_CSUM_TCPv6_Rx |
   1459   1.2      rin 	    IFCAP_CSUM_UDPv6_Tx | IFCAP_CSUM_UDPv6_Rx;
   1460   1.2      rin 
   1461   1.2      rin 	ifp->if_capenable = 0;
   1462   1.1      rin 
   1463   1.2      rin 	sc->sc_ec.ec_capabilities |=
   1464   1.2      rin 	    ETHERCAP_JUMBO_MTU | ETHERCAP_VLAN_MTU;
   1465   1.1      rin 
   1466   1.2      rin 	IFQ_SET_MAXLEN(&ifp->if_snd, sc->num_tx_desc - 1);
   1467   1.2      rin 	IFQ_SET_READY(&ifp->if_snd);
   1468   1.1      rin 
   1469   1.1      rin #if NVLAN > 0
   1470   1.2      rin 	sc->sc_ec.ec_capabilities |=  ETHERCAP_VLAN_HWTAGGING;
   1471   1.1      rin #endif
   1472   1.1      rin 
   1473   1.2      rin 	mutex_init(&sc->sc_core_lock, MUTEX_DEFAULT, IPL_NET);
   1474   1.1      rin 
   1475   1.1      rin 	/* Initialize ifmedia structures. */
   1476   1.2      rin 	sc->sc_ec.ec_ifmedia = &sc->media;
   1477   1.2      rin 	ifmedia_init_with_lock(&sc->media, IFM_IMASK, igc_media_change,
   1478   1.2      rin 	    igc_media_status, &sc->sc_core_lock);
   1479   1.1      rin 	ifmedia_add(&sc->media, IFM_ETHER | IFM_10_T, 0, NULL);
   1480   1.1      rin 	ifmedia_add(&sc->media, IFM_ETHER | IFM_10_T | IFM_FDX, 0, NULL);
   1481   1.1      rin 	ifmedia_add(&sc->media, IFM_ETHER | IFM_100_TX, 0, NULL);
   1482   1.1      rin 	ifmedia_add(&sc->media, IFM_ETHER | IFM_100_TX | IFM_FDX, 0, NULL);
   1483   1.1      rin 	ifmedia_add(&sc->media, IFM_ETHER | IFM_1000_T | IFM_FDX, 0, NULL);
   1484   1.2      rin 	ifmedia_add(&sc->media, IFM_ETHER | IFM_2500_T | IFM_FDX, 0, NULL);
   1485   1.1      rin 	ifmedia_add(&sc->media, IFM_ETHER | IFM_AUTO, 0, NULL);
   1486   1.1      rin 	ifmedia_set(&sc->media, IFM_ETHER | IFM_AUTO);
   1487   1.1      rin 
   1488   1.2      rin 	sc->sc_rx_intr_process_limit = IGC_RX_INTR_PROCESS_LIMIT_DEFAULT;
   1489   1.2      rin 	sc->sc_tx_intr_process_limit = IGC_TX_INTR_PROCESS_LIMIT_DEFAULT;
   1490   1.2      rin 	sc->sc_rx_process_limit = IGC_RX_PROCESS_LIMIT_DEFAULT;
   1491   1.2      rin 	sc->sc_tx_process_limit = IGC_TX_PROCESS_LIMIT_DEFAULT;
   1492   1.2      rin 
   1493   1.2      rin 	if_initialize(ifp);
   1494   1.2      rin 	sc->sc_ipq = if_percpuq_create(ifp);
   1495   1.2      rin 	if_deferred_start_init(ifp, NULL);
   1496   1.2      rin 	ether_ifattach(ifp, sc->hw.mac.addr);
   1497   1.2      rin 	ether_set_ifflags_cb(&sc->sc_ec, igc_ifflags_cb);
   1498   1.2      rin 	if_register(ifp);
   1499   1.2      rin }
   1500   1.1      rin 
   1501   1.2      rin static int
   1502   1.2      rin igc_init(struct ifnet *ifp)
   1503   1.2      rin {
   1504   1.2      rin 	struct igc_softc *sc = ifp->if_softc;
   1505   1.2      rin 	int error;
   1506   1.1      rin 
   1507   1.2      rin 	mutex_enter(&sc->sc_core_lock);
   1508   1.2      rin 	error = igc_init_locked(sc);
   1509   1.2      rin 	mutex_exit(&sc->sc_core_lock);
   1510   1.1      rin 
   1511   1.2      rin 	return error;
   1512   1.1      rin }
   1513   1.1      rin 
   1514   1.2      rin static int
   1515   1.2      rin igc_init_locked(struct igc_softc *sc)
   1516   1.1      rin {
   1517   1.2      rin 	struct ethercom *ec = &sc->sc_ec;
   1518   1.2      rin 	struct ifnet *ifp = &ec->ec_if;
   1519   1.1      rin 
   1520   1.2      rin 	DPRINTF(CFG, "called\n");
   1521   1.1      rin 
   1522   1.2      rin 	KASSERT(mutex_owned(&sc->sc_core_lock));
   1523   1.1      rin 
   1524   1.2      rin 	if (ISSET(ifp->if_flags, IFF_RUNNING))
   1525   1.2      rin 		igc_stop_locked(sc);
   1526   1.1      rin 
   1527   1.1      rin 	/* Put the address into the receive address array. */
   1528   1.1      rin 	igc_rar_set(&sc->hw, sc->hw.mac.addr, 0);
   1529   1.1      rin 
   1530   1.1      rin 	/* Initialize the hardware. */
   1531   1.1      rin 	igc_reset(sc);
   1532   1.1      rin 	igc_update_link_status(sc);
   1533   1.1      rin 
   1534   1.1      rin 	/* Setup VLAN support, basic and offload if available. */
   1535   1.1      rin 	IGC_WRITE_REG(&sc->hw, IGC_VET, ETHERTYPE_VLAN);
   1536   1.1      rin 
   1537   1.1      rin 	igc_initialize_transmit_unit(sc);
   1538   1.1      rin 	igc_initialize_receive_unit(sc);
   1539   1.1      rin 
   1540   1.2      rin 	if (ec->ec_capenable & ETHERCAP_VLAN_HWTAGGING) {
   1541   1.2      rin 		uint32_t ctrl = IGC_READ_REG(&sc->hw, IGC_CTRL);
   1542   1.1      rin 		ctrl |= IGC_CTRL_VME;
   1543   1.1      rin 		IGC_WRITE_REG(&sc->hw, IGC_CTRL, ctrl);
   1544   1.1      rin 	}
   1545   1.1      rin 
   1546   1.1      rin 	/* Setup multicast table. */
   1547   1.2      rin 	igc_set_filter(sc);
   1548   1.1      rin 
   1549   1.1      rin 	igc_clear_hw_cntrs_base_generic(&sc->hw);
   1550   1.1      rin 
   1551   1.2      rin 	if (sc->sc_intr_type == PCI_INTR_TYPE_MSIX)
   1552   1.2      rin 		igc_configure_queues(sc);
   1553   1.1      rin 
   1554   1.1      rin 	/* This clears any pending interrupts */
   1555   1.1      rin 	IGC_READ_REG(&sc->hw, IGC_ICR);
   1556   1.1      rin 	IGC_WRITE_REG(&sc->hw, IGC_ICS, IGC_ICS_LSC);
   1557   1.1      rin 
   1558   1.1      rin 	/* The driver can now take control from firmware. */
   1559   1.1      rin 	igc_get_hw_control(sc);
   1560   1.1      rin 
   1561   1.1      rin 	/* Set Energy Efficient Ethernet. */
   1562   1.1      rin 	igc_set_eee_i225(&sc->hw, true, true, true);
   1563   1.1      rin 
   1564   1.2      rin 	for (int iq = 0; iq < sc->sc_nqueues; iq++) {
   1565   1.2      rin 		struct rx_ring *rxr = &sc->rx_rings[iq];
   1566   1.2      rin 
   1567   1.2      rin 		mutex_enter(&rxr->rxr_lock);
   1568   1.1      rin 		igc_rxfill(rxr);
   1569   1.2      rin 		mutex_exit(&rxr->rxr_lock);
   1570   1.1      rin 	}
   1571   1.1      rin 
   1572   1.2      rin 	sc->sc_core_stopping = false;
   1573   1.2      rin 
   1574   1.1      rin 	ifp->if_flags |= IFF_RUNNING;
   1575   1.1      rin 
   1576   1.2      rin 	/* Save last flags for the callback */
   1577   1.2      rin 	sc->sc_if_flags = ifp->if_flags;
   1578   1.2      rin 
   1579   1.6      rin 	callout_schedule(&sc->sc_tick_ch, hz);
   1580   1.6      rin 
   1581   1.6      rin 	igc_enable_intr(sc);
   1582   1.6      rin 
   1583   1.2      rin 	return 0;
   1584   1.1      rin }
   1585   1.1      rin 
   1586   1.1      rin static inline int
   1587   1.2      rin igc_load_mbuf(struct igc_queue *q, bus_dma_tag_t dmat, bus_dmamap_t map,
   1588   1.2      rin     struct mbuf *m)
   1589   1.1      rin {
   1590   1.1      rin 	int error;
   1591   1.1      rin 
   1592   1.1      rin 	error = bus_dmamap_load_mbuf(dmat, map, m,
   1593   1.2      rin 	    BUS_DMA_WRITE | BUS_DMA_NOWAIT);
   1594   1.2      rin 
   1595   1.2      rin 	if (__predict_false(error == EFBIG)) {
   1596   1.2      rin 		IGC_DRIVER_EVENT(q, txdma_efbig, 1);
   1597   1.2      rin 		m = m_defrag(m, M_NOWAIT);
   1598   1.2      rin 		if (__predict_false(m == NULL)) {
   1599   1.2      rin 			IGC_DRIVER_EVENT(q, txdma_defrag, 1);
   1600   1.2      rin 			return ENOBUFS;
   1601   1.2      rin 		}
   1602   1.2      rin 		error = bus_dmamap_load_mbuf(dmat, map, m,
   1603   1.2      rin 		    BUS_DMA_WRITE | BUS_DMA_NOWAIT);
   1604   1.2      rin 	}
   1605   1.1      rin 
   1606   1.2      rin 	switch (error) {
   1607   1.2      rin 	case 0:
   1608   1.2      rin 		break;
   1609   1.2      rin 	case ENOMEM:
   1610   1.2      rin 		IGC_DRIVER_EVENT(q, txdma_enomem, 1);
   1611   1.2      rin 		break;
   1612   1.2      rin 	case EINVAL:
   1613   1.2      rin 		IGC_DRIVER_EVENT(q, txdma_einval, 1);
   1614   1.2      rin 		break;
   1615   1.2      rin 	case EAGAIN:
   1616   1.2      rin 		IGC_DRIVER_EVENT(q, txdma_eagain, 1);
   1617   1.2      rin 		break;
   1618   1.2      rin 	default:
   1619   1.2      rin 		IGC_DRIVER_EVENT(q, txdma_other, 1);
   1620   1.2      rin 		break;
   1621   1.2      rin 	}
   1622   1.1      rin 
   1623   1.2      rin 	return error;
   1624   1.1      rin }
   1625   1.1      rin 
   1626   1.2      rin #define IGC_TX_START	1
   1627   1.2      rin #define IGC_TX_TRANSMIT	2
   1628   1.2      rin 
   1629   1.2      rin static void
   1630   1.2      rin igc_start(struct ifnet *ifp)
   1631   1.1      rin {
   1632   1.1      rin 	struct igc_softc *sc = ifp->if_softc;
   1633   1.1      rin 
   1634   1.2      rin 	if (__predict_false(!sc->link_active)) {
   1635   1.2      rin 		IFQ_PURGE(&ifp->if_snd);
   1636   1.1      rin 		return;
   1637   1.1      rin 	}
   1638   1.1      rin 
   1639   1.2      rin 	struct tx_ring *txr = &sc->tx_rings[0]; /* queue 0 */
   1640   1.2      rin 	mutex_enter(&txr->txr_lock);
   1641   1.2      rin 	igc_tx_common_locked(ifp, txr, IGC_TX_START);
   1642   1.2      rin 	mutex_exit(&txr->txr_lock);
   1643   1.2      rin }
   1644   1.2      rin 
   1645   1.2      rin static inline u_int
   1646   1.2      rin igc_select_txqueue(struct igc_softc *sc, struct mbuf *m __unused)
   1647   1.2      rin {
   1648   1.2      rin 	const u_int cpuid = cpu_index(curcpu());
   1649   1.2      rin 
   1650   1.2      rin 	return cpuid % sc->sc_nqueues;
   1651   1.2      rin }
   1652   1.2      rin 
   1653   1.2      rin static int
   1654   1.2      rin igc_transmit(struct ifnet *ifp, struct mbuf *m)
   1655   1.2      rin {
   1656   1.2      rin 	struct igc_softc *sc = ifp->if_softc;
   1657   1.2      rin 	const u_int qid = igc_select_txqueue(sc, m);
   1658   1.2      rin 	struct tx_ring *txr = &sc->tx_rings[qid];
   1659   1.2      rin 	struct igc_queue *q = txr->txr_igcq;
   1660   1.2      rin 
   1661   1.2      rin 	if (__predict_false(!pcq_put(txr->txr_interq, m))) {
   1662   1.2      rin 		IGC_QUEUE_EVENT(q, tx_pcq_drop, 1);
   1663   1.2      rin 		m_freem(m);
   1664   1.2      rin 		return ENOBUFS;
   1665   1.2      rin 	}
   1666   1.2      rin 
   1667   1.2      rin 	mutex_enter(&txr->txr_lock);
   1668   1.2      rin 	igc_tx_common_locked(ifp, txr, IGC_TX_TRANSMIT);
   1669   1.2      rin 	mutex_exit(&txr->txr_lock);
   1670   1.2      rin 
   1671   1.2      rin 	return 0;
   1672   1.2      rin }
   1673   1.2      rin 
   1674   1.2      rin static void
   1675   1.2      rin igc_tx_common_locked(struct ifnet *ifp, struct tx_ring *txr, int caller)
   1676   1.2      rin {
   1677   1.2      rin 	struct igc_softc *sc = ifp->if_softc;
   1678   1.2      rin 	struct igc_queue *q = txr->txr_igcq;
   1679   1.2      rin 	net_stat_ref_t nsr = IF_STAT_GETREF(ifp);
   1680   1.2      rin 	int prod, free, last = -1;
   1681   1.2      rin 	bool post = false;
   1682   1.2      rin 
   1683   1.1      rin 	prod = txr->next_avail_desc;
   1684   1.1      rin 	free = txr->next_to_clean;
   1685   1.1      rin 	if (free <= prod)
   1686   1.1      rin 		free += sc->num_tx_desc;
   1687   1.1      rin 	free -= prod;
   1688   1.1      rin 
   1689   1.2      rin 	DPRINTF(TX, "%s: begin: msix %d prod %d n2c %d free %d\n",
   1690   1.2      rin 	    caller == IGC_TX_TRANSMIT ? "transmit" : "start",
   1691   1.2      rin 	    txr->me, prod, txr->next_to_clean, free);
   1692   1.1      rin 
   1693   1.2      rin 	for (;;) {
   1694   1.2      rin 		struct mbuf *m;
   1695   1.1      rin 
   1696   1.2      rin 		if (__predict_false(free <= IGC_MAX_SCATTER)) {
   1697   1.2      rin 			IGC_QUEUE_EVENT(q, tx_no_desc, 1);
   1698   1.1      rin 			break;
   1699   1.1      rin 		}
   1700   1.1      rin 
   1701   1.2      rin 		if (caller == IGC_TX_TRANSMIT)
   1702   1.2      rin 			m = pcq_get(txr->txr_interq);
   1703   1.2      rin 		else
   1704   1.2      rin 			IFQ_DEQUEUE(&ifp->if_snd, m);
   1705   1.2      rin 		if (__predict_false(m == NULL))
   1706   1.1      rin 			break;
   1707   1.1      rin 
   1708   1.2      rin 		struct igc_tx_buf *txbuf = &txr->tx_buffers[prod];
   1709   1.2      rin 		bus_dmamap_t map = txbuf->map;
   1710   1.1      rin 
   1711   1.2      rin 		if (__predict_false(
   1712   1.2      rin 		    igc_load_mbuf(q, txr->txdma.dma_tag, map, m))) {
   1713   1.2      rin 			if (caller == IGC_TX_TRANSMIT)
   1714   1.2      rin 				IGC_QUEUE_EVENT(q, tx_pcq_drop, 1);
   1715   1.1      rin 			m_freem(m);
   1716   1.2      rin 			if_statinc_ref(nsr, if_oerrors);
   1717   1.1      rin 			continue;
   1718   1.1      rin 		}
   1719   1.1      rin 
   1720   1.1      rin 		bus_dmamap_sync(txr->txdma.dma_tag, map, 0,
   1721   1.1      rin 		    map->dm_mapsize, BUS_DMASYNC_PREWRITE);
   1722   1.1      rin 
   1723   1.2      rin 		uint32_t ctx_cmd_type_len = 0, olinfo_status = 0;
   1724   1.2      rin 		if (igc_tx_ctx_setup(txr, m, prod, &ctx_cmd_type_len,
   1725   1.2      rin 		    &olinfo_status)) {
   1726   1.2      rin 			IGC_QUEUE_EVENT(q, tx_ctx, 1);
   1727   1.1      rin 			/* Consume the first descriptor */
   1728   1.2      rin 			prod = igc_txdesc_incr(sc, prod);
   1729   1.1      rin 			free--;
   1730   1.1      rin 		}
   1731   1.2      rin 		for (int i = 0; i < map->dm_nsegs; i++) {
   1732   1.2      rin 			union igc_adv_tx_desc *txdesc = &txr->tx_base[prod];
   1733   1.1      rin 
   1734   1.2      rin 			uint32_t cmd_type_len = ctx_cmd_type_len |
   1735   1.2      rin 			    IGC_ADVTXD_DCMD_IFCS | IGC_ADVTXD_DTYP_DATA |
   1736   1.1      rin 			    IGC_ADVTXD_DCMD_DEXT | map->dm_segs[i].ds_len;
   1737   1.2      rin 			if (i == map->dm_nsegs - 1) {
   1738   1.2      rin 				cmd_type_len |=
   1739   1.2      rin 				    IGC_ADVTXD_DCMD_EOP | IGC_ADVTXD_DCMD_RS;
   1740   1.2      rin 			}
   1741   1.1      rin 
   1742   1.2      rin 			igc_txdesc_sync(txr, prod,
   1743   1.2      rin 			    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
   1744   1.2      rin 			htolem64(&txdesc->read.buffer_addr,
   1745   1.2      rin 			    map->dm_segs[i].ds_addr);
   1746   1.1      rin 			htolem32(&txdesc->read.cmd_type_len, cmd_type_len);
   1747   1.1      rin 			htolem32(&txdesc->read.olinfo_status, olinfo_status);
   1748   1.2      rin 			igc_txdesc_sync(txr, prod,
   1749   1.2      rin 			    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
   1750   1.1      rin 
   1751   1.1      rin 			last = prod;
   1752   1.2      rin 			prod = igc_txdesc_incr(sc, prod);
   1753   1.1      rin 		}
   1754   1.1      rin 
   1755   1.1      rin 		txbuf->m_head = m;
   1756   1.1      rin 		txbuf->eop_index = last;
   1757   1.1      rin 
   1758   1.2      rin 		bpf_mtap(ifp, m, BPF_D_OUT);
   1759   1.2      rin 
   1760   1.2      rin 		if_statadd_ref(nsr, if_obytes, m->m_pkthdr.len);
   1761   1.2      rin 		if (m->m_flags & M_MCAST)
   1762   1.2      rin 			if_statinc_ref(nsr, if_omcasts);
   1763   1.2      rin 		IGC_QUEUE_EVENT(q, tx_packets, 1);
   1764   1.2      rin 		IGC_QUEUE_EVENT(q, tx_bytes, m->m_pkthdr.len);
   1765   1.1      rin 
   1766   1.2      rin 		free -= map->dm_nsegs;
   1767   1.2      rin 		post = true;
   1768   1.1      rin 	}
   1769   1.1      rin 
   1770   1.1      rin 	if (post) {
   1771   1.1      rin 		txr->next_avail_desc = prod;
   1772   1.1      rin 		IGC_WRITE_REG(&sc->hw, IGC_TDT(txr->me), prod);
   1773   1.1      rin 	}
   1774   1.2      rin 
   1775   1.2      rin 	DPRINTF(TX, "%s: done : msix %d prod %d n2c %d free %d\n",
   1776   1.2      rin 	    caller == IGC_TX_TRANSMIT ? "transmit" : "start",
   1777   1.2      rin 	    txr->me, prod, txr->next_to_clean, free);
   1778   1.2      rin 
   1779   1.2      rin 	IF_STAT_PUTREF(ifp);
   1780   1.1      rin }
   1781   1.1      rin 
   1782   1.2      rin static bool
   1783   1.2      rin igc_txeof(struct tx_ring *txr, u_int limit)
   1784   1.1      rin {
   1785   1.1      rin 	struct igc_softc *sc = txr->sc;
   1786   1.2      rin 	struct ifnet *ifp = &sc->sc_ec.ec_if;
   1787   1.2      rin 	int cons, prod;
   1788   1.2      rin 	bool more = false;
   1789   1.1      rin 
   1790   1.1      rin 	prod = txr->next_avail_desc;
   1791   1.1      rin 	cons = txr->next_to_clean;
   1792   1.1      rin 
   1793   1.2      rin 	if (cons == prod) {
   1794   1.2      rin 		DPRINTF(TX, "false: msix %d cons %d prod %d\n",
   1795   1.2      rin 		    txr->me, cons, prod);
   1796   1.2      rin 		return false;
   1797   1.2      rin 	}
   1798   1.2      rin 
   1799   1.2      rin 	do {
   1800   1.2      rin 		struct igc_tx_buf *txbuf = &txr->tx_buffers[cons];
   1801   1.2      rin 		const int last = txbuf->eop_index;
   1802   1.1      rin 
   1803   1.2      rin 		membar_consumer();	/* XXXRO necessary? */
   1804   1.1      rin 
   1805   1.2      rin 		KASSERT(last != -1);
   1806   1.2      rin 		union igc_adv_tx_desc *txdesc = &txr->tx_base[last];
   1807   1.2      rin 		igc_txdesc_sync(txr, last, BUS_DMASYNC_POSTREAD);
   1808   1.2      rin 		const uint32_t status = le32toh(txdesc->wb.status);
   1809   1.2      rin 		igc_txdesc_sync(txr, last, BUS_DMASYNC_PREREAD);
   1810   1.1      rin 
   1811   1.2      rin 		if (!(status & IGC_TXD_STAT_DD))
   1812   1.2      rin 			break;
   1813   1.1      rin 
   1814   1.2      rin 		if (limit-- == 0) {
   1815   1.2      rin 			more = true;
   1816   1.2      rin 			DPRINTF(TX, "pending TX "
   1817   1.2      rin 			    "msix %d cons %d last %d prod %d "
   1818   1.2      rin 			    "status 0x%08x\n",
   1819   1.2      rin 			    txr->me, cons, last, prod, status);
   1820   1.1      rin 			break;
   1821   1.2      rin 		}
   1822   1.2      rin 
   1823   1.2      rin 		DPRINTF(TX, "handled TX "
   1824   1.2      rin 		    "msix %d cons %d last %d prod %d "
   1825   1.2      rin 		    "status 0x%08x\n",
   1826   1.2      rin 		    txr->me, cons, last, prod, status);
   1827   1.1      rin 
   1828   1.2      rin 		if_statinc(ifp, if_opackets);
   1829   1.1      rin 
   1830   1.2      rin 		bus_dmamap_t map = txbuf->map;
   1831   1.1      rin 		bus_dmamap_sync(txr->txdma.dma_tag, map, 0, map->dm_mapsize,
   1832   1.1      rin 		    BUS_DMASYNC_POSTWRITE);
   1833   1.1      rin 		bus_dmamap_unload(txr->txdma.dma_tag, map);
   1834   1.1      rin 		m_freem(txbuf->m_head);
   1835   1.1      rin 
   1836   1.1      rin 		txbuf->m_head = NULL;
   1837   1.1      rin 		txbuf->eop_index = -1;
   1838   1.1      rin 
   1839   1.2      rin 		cons = igc_txdesc_incr(sc, last);
   1840   1.2      rin 	} while (cons != prod);
   1841   1.2      rin 
   1842   1.2      rin 	txr->next_to_clean = cons;
   1843   1.1      rin 
   1844   1.2      rin 	return more;
   1845   1.2      rin }
   1846   1.1      rin 
   1847   1.2      rin static void
   1848   1.2      rin igc_intr_barrier(struct igc_softc *sc __unused)
   1849   1.2      rin {
   1850   1.1      rin 
   1851   1.2      rin 	xc_barrier(0);
   1852   1.2      rin }
   1853   1.1      rin 
   1854   1.2      rin static void
   1855   1.2      rin igc_stop(struct ifnet *ifp, int disable)
   1856   1.2      rin {
   1857   1.2      rin 	struct igc_softc *sc = ifp->if_softc;
   1858   1.1      rin 
   1859   1.2      rin 	mutex_enter(&sc->sc_core_lock);
   1860   1.2      rin 	igc_stop_locked(sc);
   1861   1.2      rin 	mutex_exit(&sc->sc_core_lock);
   1862   1.1      rin }
   1863   1.1      rin 
   1864   1.1      rin /*********************************************************************
   1865   1.1      rin  *
   1866   1.1      rin  *  This routine disables all traffic on the adapter by issuing a
   1867   1.1      rin  *  global reset on the MAC.
   1868   1.1      rin  *
   1869   1.1      rin  **********************************************************************/
   1870   1.2      rin static void
   1871   1.2      rin igc_stop_locked(struct igc_softc *sc)
   1872   1.1      rin {
   1873   1.2      rin 	struct ifnet *ifp = &sc->sc_ec.ec_if;
   1874   1.2      rin 
   1875   1.2      rin 	DPRINTF(CFG, "called\n");
   1876   1.2      rin 
   1877   1.2      rin 	KASSERT(mutex_owned(&sc->sc_core_lock));
   1878   1.2      rin 
   1879   1.2      rin 	/*
   1880   1.2      rin 	 * If stopping processing has already started, do nothing.
   1881   1.2      rin 	 */
   1882   1.2      rin 	if ((ifp->if_flags & IFF_RUNNING) == 0)
   1883   1.2      rin 		return;
   1884   1.1      rin 
   1885   1.1      rin 	/* Tell the stack that the interface is no longer active. */
   1886   1.2      rin 	ifp->if_flags &= ~IFF_RUNNING;
   1887   1.2      rin 
   1888   1.2      rin 	/*
   1889   1.2      rin 	 * igc_handle_queue() can enable interrupts, so wait for completion of
   1890   1.2      rin 	 * last igc_handle_queue() after unset IFF_RUNNING.
   1891   1.2      rin 	 */
   1892   1.2      rin 	mutex_exit(&sc->sc_core_lock);
   1893   1.2      rin 	igc_barrier_handle_queue(sc);
   1894   1.2      rin 	mutex_enter(&sc->sc_core_lock);
   1895   1.2      rin 
   1896   1.2      rin 	sc->sc_core_stopping = true;
   1897   1.1      rin 
   1898   1.1      rin 	igc_disable_intr(sc);
   1899   1.1      rin 
   1900   1.2      rin 	callout_halt(&sc->sc_tick_ch, &sc->sc_core_lock);
   1901   1.2      rin 
   1902   1.1      rin 	igc_reset_hw(&sc->hw);
   1903   1.1      rin 	IGC_WRITE_REG(&sc->hw, IGC_WUC, 0);
   1904   1.1      rin 
   1905   1.2      rin 	/*
   1906   1.2      rin 	 * Wait for completion of interrupt handlers.
   1907   1.2      rin 	 */
   1908   1.2      rin 	mutex_exit(&sc->sc_core_lock);
   1909   1.2      rin 	igc_intr_barrier(sc);
   1910   1.2      rin 	mutex_enter(&sc->sc_core_lock);
   1911   1.2      rin 
   1912   1.2      rin 	igc_update_link_status(sc);
   1913   1.2      rin 
   1914   1.2      rin 	for (int iq = 0; iq < sc->sc_nqueues; iq++) {
   1915   1.2      rin 		struct tx_ring *txr = &sc->tx_rings[iq];
   1916   1.2      rin 
   1917   1.2      rin 		igc_withdraw_transmit_packets(txr, false);
   1918   1.2      rin 	}
   1919   1.2      rin 
   1920   1.2      rin 	for (int iq = 0; iq < sc->sc_nqueues; iq++) {
   1921   1.2      rin 		struct rx_ring *rxr = &sc->rx_rings[iq];
   1922   1.1      rin 
   1923   1.2      rin 		igc_clear_receive_status(rxr);
   1924   1.2      rin 	}
   1925   1.1      rin 
   1926   1.2      rin 	/* Save last flags for the callback */
   1927   1.2      rin 	sc->sc_if_flags = ifp->if_flags;
   1928   1.1      rin }
   1929   1.1      rin 
   1930   1.1      rin /*********************************************************************
   1931   1.1      rin  *  Ioctl entry point
   1932   1.1      rin  *
   1933   1.1      rin  *  igc_ioctl is called when the user wants to configure the
   1934   1.1      rin  *  interface.
   1935   1.1      rin  *
   1936   1.1      rin  *  return 0 on success, positive on failure
   1937   1.1      rin  **********************************************************************/
   1938   1.2      rin static int
   1939   1.2      rin igc_ioctl(struct ifnet * ifp, u_long cmd, void *data)
   1940   1.1      rin {
   1941   1.2      rin 	struct igc_softc *sc __unused = ifp->if_softc;
   1942   1.2      rin 	int s;
   1943   1.2      rin 	int error;
   1944   1.1      rin 
   1945   1.2      rin 	DPRINTF(CFG, "cmd 0x%016lx\n", cmd);
   1946   1.1      rin 
   1947   1.1      rin 	switch (cmd) {
   1948   1.2      rin 	case SIOCADDMULTI:
   1949   1.2      rin 	case SIOCDELMULTI:
   1950   1.1      rin 		break;
   1951   1.2      rin 	default:
   1952   1.2      rin 		KASSERT(IFNET_LOCKED(ifp));
   1953   1.2      rin 	}
   1954   1.2      rin 
   1955   1.2      rin 	if (cmd == SIOCZIFDATA) {
   1956   1.2      rin 		mutex_enter(&sc->sc_core_lock);
   1957   1.2      rin 		igc_clear_counters(sc);
   1958   1.2      rin 		mutex_exit(&sc->sc_core_lock);
   1959   1.2      rin 	}
   1960   1.2      rin 
   1961   1.2      rin 	switch (cmd) {
   1962   1.2      rin #ifdef IF_RXR
   1963   1.1      rin 	case SIOCGIFRXR:
   1964   1.2      rin 		s = splnet();
   1965   1.1      rin 		error = igc_rxrinfo(sc, (struct if_rxrinfo *)ifr->ifr_data);
   1966   1.2      rin 		splx(s);
   1967   1.1      rin 		break;
   1968   1.2      rin #endif
   1969   1.1      rin 	default:
   1970   1.2      rin 		s = splnet();
   1971   1.2      rin 		error = ether_ioctl(ifp, cmd, data);
   1972   1.2      rin 		splx(s);
   1973   1.2      rin 		break;
   1974   1.1      rin 	}
   1975   1.1      rin 
   1976   1.2      rin 	if (error != ENETRESET)
   1977   1.2      rin 		return error;
   1978   1.2      rin 
   1979   1.2      rin 	error = 0;
   1980   1.2      rin 
   1981   1.2      rin 	if (cmd == SIOCSIFCAP)
   1982   1.2      rin 		error = if_init(ifp);
   1983   1.2      rin 	else if ((cmd == SIOCADDMULTI) || (cmd == SIOCDELMULTI)) {
   1984   1.2      rin 		mutex_enter(&sc->sc_core_lock);
   1985   1.2      rin 		if (sc->sc_if_flags & IFF_RUNNING) {
   1986   1.2      rin 			/*
   1987   1.2      rin 			 * Multicast list has changed; set the hardware filter
   1988   1.2      rin 			 * accordingly.
   1989   1.2      rin 			 */
   1990   1.1      rin 			igc_disable_intr(sc);
   1991   1.2      rin 			igc_set_filter(sc);
   1992   1.1      rin 			igc_enable_intr(sc);
   1993   1.1      rin 		}
   1994   1.2      rin 		mutex_exit(&sc->sc_core_lock);
   1995   1.1      rin 	}
   1996   1.1      rin 
   1997   1.1      rin 	return error;
   1998   1.1      rin }
   1999   1.1      rin 
   2000   1.2      rin #ifdef IF_RXR
   2001   1.2      rin static int
   2002   1.1      rin igc_rxrinfo(struct igc_softc *sc, struct if_rxrinfo *ifri)
   2003   1.1      rin {
   2004   1.2      rin 	struct if_rxring_info *ifr, ifr1;
   2005   1.2      rin 	int error;
   2006   1.2      rin 
   2007   1.2      rin 	if (sc->sc_nqueues > 1) {
   2008   1.2      rin 		ifr = kmem_zalloc(sc->sc_nqueues * sizeof(*ifr), KM_SLEEP);
   2009   1.2      rin 	} else {
   2010   1.2      rin 		ifr = &ifr1;
   2011   1.2      rin 		memset(ifr, 0, sizeof(*ifr));
   2012   1.2      rin 	}
   2013   1.2      rin 
   2014   1.2      rin 	for (int iq = 0; iq < sc->sc_nqueues; iq++) {
   2015   1.2      rin 		struct rx_ring *rxr = &sc->rx_rings[iq];
   2016   1.2      rin 
   2017   1.2      rin 		ifr[iq].ifr_size = MCLBYTES;
   2018   1.2      rin 		snprintf(ifr[iq].ifr_name, sizeof(ifr[iq].ifr_name), "%d", iq);
   2019   1.2      rin 		ifr[iq].ifr_info = rxr->rx_ring;
   2020   1.1      rin 	}
   2021   1.1      rin 
   2022   1.1      rin 	error = if_rxr_info_ioctl(ifri, sc->sc_nqueues, ifr);
   2023   1.2      rin 	if (sc->sc_nqueues > 1)
   2024   1.2      rin 		kmem_free(ifr, sc->sc_nqueues * sizeof(*ifr));
   2025   1.1      rin 
   2026   1.1      rin 	return error;
   2027   1.1      rin }
   2028   1.2      rin #endif
   2029   1.1      rin 
   2030   1.2      rin static void
   2031   1.1      rin igc_rxfill(struct rx_ring *rxr)
   2032   1.1      rin {
   2033   1.1      rin 	struct igc_softc *sc = rxr->sc;
   2034   1.2      rin 	int id;
   2035   1.1      rin 
   2036   1.2      rin 	for (id = 0; id < sc->num_rx_desc; id++) {
   2037   1.2      rin 		if (igc_get_buf(rxr, id, false)) {
   2038   1.2      rin 			panic("%s: msix=%d i=%d\n", __func__, rxr->me, id);
   2039   1.2      rin 		}
   2040   1.1      rin 	}
   2041   1.1      rin 
   2042   1.2      rin 	id = sc->num_rx_desc - 1;
   2043   1.2      rin 	rxr->last_desc_filled = id;
   2044   1.2      rin 	IGC_WRITE_REG(&sc->hw, IGC_RDT(rxr->me), id);
   2045   1.2      rin 	rxr->next_to_check = 0;
   2046   1.1      rin }
   2047   1.1      rin 
   2048   1.2      rin static void
   2049   1.2      rin igc_rxrefill(struct rx_ring *rxr, int end)
   2050   1.1      rin {
   2051   1.1      rin 	struct igc_softc *sc = rxr->sc;
   2052   1.2      rin 	int id;
   2053   1.1      rin 
   2054   1.2      rin 	for (id = rxr->next_to_check; id != end; id = igc_rxdesc_incr(sc, id)) {
   2055   1.2      rin 		if (igc_get_buf(rxr, id, true)) {
   2056   1.2      rin 			/* XXXRO */
   2057   1.2      rin 			panic("%s: msix=%d id=%d\n", __func__, rxr->me, id);
   2058   1.2      rin 		}
   2059   1.1      rin 	}
   2060   1.2      rin 
   2061   1.2      rin 	id = igc_rxdesc_decr(sc, id);
   2062   1.2      rin 	DPRINTF(RX, "%s RDT %d id %d\n",
   2063   1.2      rin 	    rxr->last_desc_filled == id ? "same" : "diff",
   2064   1.2      rin 	    rxr->last_desc_filled, id);
   2065   1.2      rin 	rxr->last_desc_filled = id;
   2066   1.2      rin 	IGC_WRITE_REG(&sc->hw, IGC_RDT(rxr->me), id);
   2067   1.1      rin }
   2068   1.1      rin 
   2069   1.1      rin /*********************************************************************
   2070   1.1      rin  *
   2071   1.1      rin  *  This routine executes in interrupt context. It replenishes
   2072   1.1      rin  *  the mbufs in the descriptor and sends data which has been
   2073   1.1      rin  *  dma'ed into host memory to upper layer.
   2074   1.1      rin  *
   2075   1.1      rin  *********************************************************************/
   2076   1.2      rin static bool
   2077   1.2      rin igc_rxeof(struct rx_ring *rxr, u_int limit)
   2078   1.1      rin {
   2079   1.1      rin 	struct igc_softc *sc = rxr->sc;
   2080   1.2      rin 	struct igc_queue *q = rxr->rxr_igcq;
   2081   1.2      rin 	struct ifnet *ifp = &sc->sc_ec.ec_if;
   2082   1.2      rin 	int id;
   2083   1.2      rin 	bool more = false;
   2084   1.2      rin 
   2085   1.2      rin 	id = rxr->next_to_check;
   2086   1.2      rin 	for (;;) {
   2087   1.2      rin 		union igc_adv_rx_desc *rxdesc = &rxr->rx_base[id];
   2088   1.2      rin 		struct igc_rx_buf *rxbuf, *nxbuf;
   2089   1.2      rin 		struct mbuf *mp, *m;
   2090   1.1      rin 
   2091   1.2      rin 		igc_rxdesc_sync(rxr, id,
   2092   1.2      rin 		    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
   2093   1.1      rin 
   2094   1.2      rin 		const uint32_t staterr = le32toh(rxdesc->wb.upper.status_error);
   2095   1.1      rin 
   2096   1.1      rin 		if (!ISSET(staterr, IGC_RXD_STAT_DD)) {
   2097   1.2      rin 			igc_rxdesc_sync(rxr, id,
   2098   1.2      rin 			    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
   2099   1.2      rin 			break;
   2100   1.2      rin 		}
   2101   1.2      rin 
   2102   1.2      rin 		if (limit-- == 0) {
   2103   1.2      rin 			igc_rxdesc_sync(rxr, id,
   2104   1.2      rin 			    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
   2105   1.2      rin 			DPRINTF(RX, "more=true\n");
   2106   1.2      rin 			more = true;
   2107   1.1      rin 			break;
   2108   1.1      rin 		}
   2109   1.1      rin 
   2110   1.1      rin 		/* Zero out the receive descriptors status. */
   2111   1.1      rin 		rxdesc->wb.upper.status_error = 0;
   2112   1.1      rin 
   2113   1.1      rin 		/* Pull the mbuf off the ring. */
   2114   1.2      rin 		rxbuf = &rxr->rx_buffers[id];
   2115   1.2      rin 		bus_dmamap_t map = rxbuf->map;
   2116   1.2      rin 		bus_dmamap_sync(rxr->rxdma.dma_tag, map,
   2117   1.2      rin 		    0, map->dm_mapsize, BUS_DMASYNC_POSTREAD);
   2118   1.2      rin 		bus_dmamap_unload(rxr->rxdma.dma_tag, map);
   2119   1.1      rin 
   2120   1.1      rin 		mp = rxbuf->buf;
   2121   1.2      rin 		rxbuf->buf = NULL;
   2122   1.2      rin 
   2123   1.2      rin 		const bool eop = staterr & IGC_RXD_STAT_EOP;
   2124   1.2      rin 		const uint16_t len = le16toh(rxdesc->wb.upper.length);
   2125   1.2      rin 
   2126   1.7    oster #if NVLAN > 0
   2127   1.2      rin 		const uint16_t vtag = le16toh(rxdesc->wb.upper.vlan);
   2128   1.7    oster #endif
   2129   1.2      rin 
   2130   1.2      rin 		const uint32_t ptype = le32toh(rxdesc->wb.lower.lo_dword.data) &
   2131   1.1      rin 		    IGC_PKTTYPE_MASK;
   2132   1.1      rin 
   2133   1.2      rin 		const uint32_t hash __unused =
   2134   1.2      rin 		    le32toh(rxdesc->wb.lower.hi_dword.rss);
   2135   1.2      rin 		const uint16_t hashtype __unused =
   2136   1.2      rin 		    le16toh(rxdesc->wb.lower.lo_dword.hs_rss.pkt_info) &
   2137   1.2      rin 		    IGC_RXDADV_RSSTYPE_MASK;
   2138   1.2      rin 
   2139   1.2      rin 		igc_rxdesc_sync(rxr, id,
   2140   1.2      rin 		    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
   2141   1.2      rin 
   2142   1.2      rin 		if (__predict_false(staterr & IGC_RXDEXT_STATERR_RXE)) {
   2143   1.1      rin 			if (rxbuf->fmp) {
   2144   1.1      rin 				m_freem(rxbuf->fmp);
   2145   1.1      rin 				rxbuf->fmp = NULL;
   2146   1.1      rin 			}
   2147   1.1      rin 
   2148   1.1      rin 			m_freem(mp);
   2149   1.2      rin 			m = NULL;
   2150   1.2      rin 
   2151   1.2      rin 			if_statinc(ifp, if_ierrors);
   2152   1.2      rin 			IGC_QUEUE_EVENT(q, rx_discard, 1);
   2153   1.2      rin 
   2154   1.2      rin 			DPRINTF(RX, "ierrors++\n");
   2155   1.2      rin 
   2156   1.1      rin 			goto next_desc;
   2157   1.1      rin 		}
   2158   1.1      rin 
   2159   1.2      rin 		if (__predict_false(mp == NULL)) {
   2160   1.1      rin 			panic("%s: igc_rxeof: NULL mbuf in slot %d "
   2161   1.2      rin 			    "(filled %d)", device_xname(sc->sc_dev),
   2162   1.2      rin 			    id, rxr->last_desc_filled);
   2163   1.1      rin 		}
   2164   1.1      rin 
   2165   1.1      rin 		if (!eop) {
   2166   1.1      rin 			/*
   2167   1.1      rin 			 * Figure out the next descriptor of this frame.
   2168   1.1      rin 			 */
   2169   1.2      rin 			int nextp = igc_rxdesc_incr(sc, id);
   2170   1.2      rin 
   2171   1.1      rin 			nxbuf = &rxr->rx_buffers[nextp];
   2172   1.2      rin 			/*
   2173   1.2      rin 			 * TODO prefetch(nxbuf);
   2174   1.2      rin 			 */
   2175   1.1      rin 		}
   2176   1.1      rin 
   2177   1.1      rin 		mp->m_len = len;
   2178   1.1      rin 
   2179   1.1      rin 		m = rxbuf->fmp;
   2180   1.2      rin 		rxbuf->fmp = NULL;
   2181   1.1      rin 
   2182   1.2      rin 		if (m != NULL) {
   2183   1.1      rin 			m->m_pkthdr.len += mp->m_len;
   2184   1.2      rin 		} else {
   2185   1.1      rin 			m = mp;
   2186   1.1      rin 			m->m_pkthdr.len = mp->m_len;
   2187   1.1      rin #if NVLAN > 0
   2188   1.2      rin 			if (staterr & IGC_RXD_STAT_VP)
   2189   1.2      rin 				vlan_set_tag(m, vtag);
   2190   1.1      rin #endif
   2191   1.1      rin 		}
   2192   1.1      rin 
   2193   1.1      rin 		/* Pass the head pointer on */
   2194   1.2      rin 		if (!eop) {
   2195   1.1      rin 			nxbuf->fmp = m;
   2196   1.1      rin 			m = NULL;
   2197   1.1      rin 			mp->m_next = nxbuf->buf;
   2198   1.1      rin 		} else {
   2199   1.2      rin 			m_set_rcvif(m, ifp);
   2200   1.2      rin 
   2201   1.2      rin 			m->m_pkthdr.csum_flags = igc_rx_checksum(q,
   2202   1.2      rin 			    ifp->if_capenable, staterr, ptype);
   2203   1.1      rin 
   2204   1.2      rin #ifdef notyet
   2205   1.1      rin 			if (hashtype != IGC_RXDADV_RSSTYPE_NONE) {
   2206   1.1      rin 				m->m_pkthdr.ph_flowid = hash;
   2207   1.1      rin 				SET(m->m_pkthdr.csum_flags, M_FLOWID);
   2208   1.1      rin 			}
   2209   1.2      rin 			ml_enqueue(&ml, m);
   2210   1.2      rin #endif
   2211   1.2      rin 
   2212   1.2      rin 			if_percpuq_enqueue(sc->sc_ipq, m);
   2213   1.1      rin 
   2214   1.2      rin 			if_statinc(ifp, if_ipackets);
   2215   1.2      rin 			IGC_QUEUE_EVENT(q, rx_packets, 1);
   2216   1.2      rin 			IGC_QUEUE_EVENT(q, rx_bytes, m->m_pkthdr.len);
   2217   1.1      rin 		}
   2218   1.2      rin  next_desc:
   2219   1.1      rin 		/* Advance our pointers to the next descriptor. */
   2220   1.2      rin 		id = igc_rxdesc_incr(sc, id);
   2221   1.1      rin 	}
   2222   1.1      rin 
   2223   1.2      rin 	DPRINTF(RX, "fill queue[%d]\n", rxr->me);
   2224   1.2      rin 	igc_rxrefill(rxr, id);
   2225   1.1      rin 
   2226   1.2      rin 	DPRINTF(RX, "%s n2c %d id %d\n",
   2227   1.2      rin 	    rxr->next_to_check == id ? "same" : "diff",
   2228   1.2      rin 	    rxr->next_to_check, id);
   2229   1.2      rin 	rxr->next_to_check = id;
   2230   1.2      rin 
   2231   1.2      rin #ifdef OPENBSD
   2232   1.1      rin 	if (!(staterr & IGC_RXD_STAT_DD))
   2233   1.1      rin 		return 0;
   2234   1.2      rin #endif
   2235   1.1      rin 
   2236   1.2      rin 	return more;
   2237   1.1      rin }
   2238   1.1      rin 
   2239   1.1      rin /*********************************************************************
   2240   1.1      rin  *
   2241   1.1      rin  *  Verify that the hardware indicated that the checksum is valid.
   2242   1.1      rin  *  Inform the stack about the status of checksum so that stack
   2243   1.1      rin  *  doesn't spend time verifying the checksum.
   2244   1.1      rin  *
   2245   1.1      rin  *********************************************************************/
   2246   1.2      rin static int
   2247   1.2      rin igc_rx_checksum(struct igc_queue *q, uint64_t capenable, uint32_t staterr,
   2248   1.2      rin     uint32_t ptype)
   2249   1.1      rin {
   2250   1.2      rin 	const uint16_t status = (uint16_t)staterr;
   2251   1.2      rin 	const uint8_t errors = (uint8_t)(staterr >> 24);
   2252   1.2      rin 	int flags = 0;
   2253   1.2      rin 
   2254   1.2      rin 	if ((status & IGC_RXD_STAT_IPCS) != 0 &&
   2255   1.2      rin 	    (capenable & IFCAP_CSUM_IPv4_Rx) != 0) {
   2256   1.2      rin 		IGC_DRIVER_EVENT(q, rx_ipcs, 1);
   2257   1.2      rin 		flags |= M_CSUM_IPv4;
   2258   1.2      rin 		if (__predict_false((errors & IGC_RXD_ERR_IPE) != 0)) {
   2259   1.2      rin 			IGC_DRIVER_EVENT(q, rx_ipcs_bad, 1);
   2260   1.2      rin 			flags |= M_CSUM_IPv4_BAD;
   2261   1.2      rin 		}
   2262   1.2      rin 	}
   2263   1.2      rin 
   2264   1.2      rin 	if ((status & IGC_RXD_STAT_TCPCS) != 0) {
   2265   1.2      rin 		IGC_DRIVER_EVENT(q, rx_tcpcs, 1);
   2266   1.2      rin 		if ((capenable & IFCAP_CSUM_TCPv4_Rx) != 0)
   2267   1.2      rin 			flags |= M_CSUM_TCPv4;
   2268   1.2      rin 		if ((capenable & IFCAP_CSUM_TCPv6_Rx) != 0)
   2269   1.2      rin 			flags |= M_CSUM_TCPv6;
   2270   1.2      rin 	}
   2271   1.1      rin 
   2272   1.2      rin 	if ((status & IGC_RXD_STAT_UDPCS) != 0) {
   2273   1.2      rin 		IGC_DRIVER_EVENT(q, rx_udpcs, 1);
   2274   1.2      rin 		if ((capenable & IFCAP_CSUM_UDPv4_Rx) != 0)
   2275   1.2      rin 			flags |= M_CSUM_UDPv4;
   2276   1.2      rin 		if ((capenable & IFCAP_CSUM_UDPv6_Rx) != 0)
   2277   1.2      rin 			flags |= M_CSUM_UDPv6;
   2278   1.1      rin 	}
   2279   1.1      rin 
   2280   1.2      rin 	if (__predict_false((errors & IGC_RXD_ERR_TCPE) != 0)) {
   2281   1.2      rin 		IGC_DRIVER_EVENT(q, rx_l4cs_bad, 1);
   2282   1.2      rin 		if ((flags & ~M_CSUM_IPv4) != 0)
   2283   1.2      rin 			flags |= M_CSUM_TCP_UDP_BAD;
   2284   1.1      rin 	}
   2285   1.2      rin 
   2286   1.2      rin 	return flags;
   2287   1.1      rin }
   2288   1.1      rin 
   2289   1.2      rin static void
   2290   1.1      rin igc_watchdog(struct ifnet * ifp)
   2291   1.1      rin {
   2292   1.1      rin }
   2293   1.1      rin 
   2294   1.2      rin static void
   2295   1.2      rin igc_tick(void *arg)
   2296   1.2      rin {
   2297   1.2      rin 	struct igc_softc *sc = arg;
   2298   1.2      rin 
   2299   1.2      rin 	mutex_enter(&sc->sc_core_lock);
   2300   1.2      rin 
   2301   1.2      rin 	if (__predict_false(sc->sc_core_stopping)) {
   2302   1.2      rin 		mutex_exit(&sc->sc_core_lock);
   2303   1.2      rin 		return;
   2304   1.2      rin 	}
   2305   1.2      rin 
   2306   1.2      rin 	/* XXX watchdog */
   2307   1.2      rin 	if (0) {
   2308   1.2      rin 		IGC_GLOBAL_EVENT(sc, watchdog, 1);
   2309   1.2      rin 	}
   2310   1.2      rin 
   2311   1.2      rin 	igc_update_counters(sc);
   2312   1.2      rin 
   2313   1.2      rin 	mutex_exit(&sc->sc_core_lock);
   2314   1.2      rin 
   2315   1.2      rin 	callout_schedule(&sc->sc_tick_ch, hz);
   2316   1.2      rin }
   2317   1.2      rin 
   2318   1.1      rin /*********************************************************************
   2319   1.1      rin  *
   2320   1.1      rin  *  Media Ioctl callback
   2321   1.1      rin  *
   2322   1.1      rin  *  This routine is called whenever the user queries the status of
   2323   1.1      rin  *  the interface using ifconfig.
   2324   1.1      rin  *
   2325   1.1      rin  **********************************************************************/
   2326   1.2      rin static void
   2327   1.1      rin igc_media_status(struct ifnet *ifp, struct ifmediareq *ifmr)
   2328   1.1      rin {
   2329   1.1      rin 	struct igc_softc *sc = ifp->if_softc;
   2330   1.2      rin 	struct igc_hw *hw = &sc->hw;
   2331   1.1      rin 
   2332   1.1      rin 	igc_update_link_status(sc);
   2333   1.1      rin 
   2334   1.1      rin 	ifmr->ifm_status = IFM_AVALID;
   2335   1.1      rin 	ifmr->ifm_active = IFM_ETHER;
   2336   1.1      rin 
   2337   1.1      rin 	if (!sc->link_active) {
   2338   1.1      rin 		ifmr->ifm_active |= IFM_NONE;
   2339   1.1      rin 		return;
   2340   1.1      rin 	}
   2341   1.1      rin 
   2342   1.1      rin 	ifmr->ifm_status |= IFM_ACTIVE;
   2343   1.1      rin 
   2344   1.1      rin 	switch (sc->link_speed) {
   2345   1.1      rin 	case 10:
   2346   1.1      rin 		ifmr->ifm_active |= IFM_10_T;
   2347   1.1      rin 		break;
   2348   1.1      rin 	case 100:
   2349   1.1      rin 		ifmr->ifm_active |= IFM_100_TX;
   2350   1.2      rin 		break;
   2351   1.1      rin 	case 1000:
   2352   1.1      rin 		ifmr->ifm_active |= IFM_1000_T;
   2353   1.1      rin 		break;
   2354   1.1      rin 	case 2500:
   2355   1.2      rin 		ifmr->ifm_active |= IFM_2500_T;
   2356   1.2      rin 		break;
   2357   1.1      rin 	}
   2358   1.1      rin 
   2359   1.1      rin 	if (sc->link_duplex == FULL_DUPLEX)
   2360   1.1      rin 		ifmr->ifm_active |= IFM_FDX;
   2361   1.1      rin 	else
   2362   1.1      rin 		ifmr->ifm_active |= IFM_HDX;
   2363   1.2      rin 
   2364   1.2      rin 	switch (hw->fc.current_mode) {
   2365   1.2      rin 	case igc_fc_tx_pause:
   2366   1.2      rin 		ifmr->ifm_active |= IFM_FLOW | IFM_ETH_TXPAUSE;
   2367   1.2      rin 		break;
   2368   1.2      rin 	case igc_fc_rx_pause:
   2369   1.2      rin 		ifmr->ifm_active |= IFM_FLOW | IFM_ETH_RXPAUSE;
   2370   1.2      rin 		break;
   2371   1.2      rin 	case igc_fc_full:
   2372   1.2      rin 		ifmr->ifm_active |= IFM_FLOW |
   2373   1.2      rin 		    IFM_ETH_TXPAUSE | IFM_ETH_RXPAUSE;
   2374   1.2      rin 		break;
   2375   1.2      rin 	case igc_fc_none:
   2376   1.2      rin 	default:
   2377   1.2      rin 		break;
   2378   1.2      rin 	}
   2379   1.1      rin }
   2380   1.1      rin 
   2381   1.1      rin /*********************************************************************
   2382   1.1      rin  *
   2383   1.1      rin  *  Media Ioctl callback
   2384   1.1      rin  *
   2385   1.1      rin  *  This routine is called when the user changes speed/duplex using
   2386   1.1      rin  *  media/mediopt option with ifconfig.
   2387   1.1      rin  *
   2388   1.1      rin  **********************************************************************/
   2389   1.2      rin static int
   2390   1.1      rin igc_media_change(struct ifnet *ifp)
   2391   1.1      rin {
   2392   1.1      rin 	struct igc_softc *sc = ifp->if_softc;
   2393   1.1      rin 	struct ifmedia *ifm = &sc->media;
   2394   1.1      rin 
   2395   1.1      rin 	if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
   2396   1.2      rin 		return EINVAL;
   2397   1.1      rin 
   2398   1.1      rin 	sc->hw.mac.autoneg = DO_AUTO_NEG;
   2399   1.1      rin 
   2400   1.1      rin 	switch (IFM_SUBTYPE(ifm->ifm_media)) {
   2401   1.1      rin 	case IFM_AUTO:
   2402   1.1      rin 		sc->hw.phy.autoneg_advertised = AUTONEG_ADV_DEFAULT;
   2403   1.1      rin 		break;
   2404   1.2      rin 	case IFM_2500_T:
   2405   1.2      rin 		sc->hw.phy.autoneg_advertised = ADVERTISE_2500_FULL;
   2406   1.2      rin 		break;
   2407   1.1      rin 	case IFM_1000_T:
   2408   1.1      rin 		sc->hw.phy.autoneg_advertised = ADVERTISE_1000_FULL;
   2409   1.1      rin 		break;
   2410   1.1      rin 	case IFM_100_TX:
   2411   1.4  msaitoh 		if ((ifm->ifm_media & IFM_GMASK) == IFM_FDX)
   2412   1.4  msaitoh 			sc->hw.phy.autoneg_advertised = ADVERTISE_100_FULL;
   2413   1.4  msaitoh 		else
   2414   1.1      rin 			sc->hw.phy.autoneg_advertised = ADVERTISE_100_HALF;
   2415   1.1      rin 		break;
   2416   1.1      rin 	case IFM_10_T:
   2417   1.4  msaitoh 		if ((ifm->ifm_media & IFM_GMASK) == IFM_FDX)
   2418   1.4  msaitoh 			sc->hw.phy.autoneg_advertised = ADVERTISE_10_FULL;
   2419   1.4  msaitoh 		else
   2420   1.1      rin 			sc->hw.phy.autoneg_advertised = ADVERTISE_10_HALF;
   2421   1.1      rin 		break;
   2422   1.1      rin 	default:
   2423   1.1      rin 		return EINVAL;
   2424   1.1      rin 	}
   2425   1.1      rin 
   2426   1.2      rin 	igc_init_locked(sc);
   2427   1.1      rin 
   2428   1.1      rin 	return 0;
   2429   1.1      rin }
   2430   1.1      rin 
   2431   1.2      rin static int
   2432   1.2      rin igc_ifflags_cb(struct ethercom *ec)
   2433   1.2      rin {
   2434   1.2      rin 	struct ifnet *ifp = &ec->ec_if;
   2435   1.2      rin 	struct igc_softc *sc = ifp->if_softc;
   2436   1.2      rin 	int rc = 0;
   2437   1.2      rin 	u_short iffchange;
   2438   1.2      rin 	bool needreset = false;
   2439   1.2      rin 
   2440   1.2      rin 	DPRINTF(CFG, "called\n");
   2441   1.2      rin 
   2442   1.2      rin 	KASSERT(IFNET_LOCKED(ifp));
   2443   1.2      rin 
   2444   1.2      rin 	mutex_enter(&sc->sc_core_lock);
   2445   1.2      rin 
   2446   1.2      rin 	/*
   2447   1.2      rin 	 * Check for if_flags.
   2448   1.2      rin 	 * Main usage is to prevent linkdown when opening bpf.
   2449   1.2      rin 	 */
   2450   1.2      rin 	iffchange = ifp->if_flags ^ sc->sc_if_flags;
   2451   1.2      rin 	sc->sc_if_flags = ifp->if_flags;
   2452   1.2      rin 	if ((iffchange & ~(IFF_CANTCHANGE | IFF_DEBUG)) != 0) {
   2453   1.2      rin 		needreset = true;
   2454   1.2      rin 		goto ec;
   2455   1.2      rin 	}
   2456   1.2      rin 
   2457   1.2      rin 	/* iff related updates */
   2458   1.2      rin 	if ((iffchange & IFF_PROMISC) != 0)
   2459   1.2      rin 		igc_set_filter(sc);
   2460   1.2      rin 
   2461   1.2      rin #ifdef notyet
   2462   1.2      rin 	igc_set_vlan(sc);
   2463   1.2      rin #endif
   2464   1.2      rin 
   2465   1.2      rin ec:
   2466   1.2      rin #ifdef notyet
   2467   1.2      rin 	/* Check for ec_capenable. */
   2468   1.2      rin 	ecchange = ec->ec_capenable ^ sc->sc_ec_capenable;
   2469   1.2      rin 	sc->sc_ec_capenable = ec->ec_capenable;
   2470   1.2      rin 	if ((ecchange & ~ETHERCAP_SOMETHING) != 0) {
   2471   1.2      rin 		needreset = true;
   2472   1.2      rin 		goto out;
   2473   1.2      rin 	}
   2474   1.2      rin #endif
   2475   1.2      rin 	if (needreset)
   2476   1.2      rin 		rc = ENETRESET;
   2477   1.2      rin 
   2478   1.2      rin 	mutex_exit(&sc->sc_core_lock);
   2479   1.2      rin 
   2480   1.2      rin 	return rc;
   2481   1.2      rin }
   2482   1.2      rin 
   2483   1.2      rin static void
   2484   1.2      rin igc_set_filter(struct igc_softc *sc)
   2485   1.1      rin {
   2486   1.2      rin 	struct ethercom *ec = &sc->sc_ec;
   2487   1.2      rin 	uint32_t rctl;
   2488   1.2      rin 
   2489   1.2      rin 	rctl = IGC_READ_REG(&sc->hw, IGC_RCTL);
   2490   1.2      rin 	rctl &= ~(IGC_RCTL_BAM |IGC_RCTL_UPE | IGC_RCTL_MPE);
   2491   1.2      rin 
   2492   1.2      rin 	if ((sc->sc_if_flags & IFF_BROADCAST) != 0)
   2493   1.2      rin 		rctl |= IGC_RCTL_BAM;
   2494   1.2      rin 	if ((sc->sc_if_flags & IFF_PROMISC) != 0) {
   2495   1.2      rin 		DPRINTF(CFG, "promisc\n");
   2496   1.2      rin 		rctl |= IGC_RCTL_UPE;
   2497   1.2      rin 		ETHER_LOCK(ec);
   2498   1.2      rin  allmulti:
   2499   1.2      rin 		ec->ec_flags |= ETHER_F_ALLMULTI;
   2500   1.2      rin 		ETHER_UNLOCK(ec);
   2501   1.2      rin 		rctl |= IGC_RCTL_MPE;
   2502   1.1      rin 	} else {
   2503   1.2      rin 		struct ether_multistep step;
   2504   1.2      rin 		struct ether_multi *enm;
   2505   1.2      rin 		int mcnt = 0;
   2506   1.2      rin 
   2507   1.2      rin 		memset(sc->mta, 0, IGC_MTA_LEN);
   2508   1.2      rin 
   2509   1.2      rin 		ETHER_LOCK(ec);
   2510   1.2      rin 		ETHER_FIRST_MULTI(step, ec, enm);
   2511   1.1      rin 		while (enm != NULL) {
   2512   1.2      rin 			if (((memcmp(enm->enm_addrlo, enm->enm_addrhi,
   2513   1.2      rin 					ETHER_ADDR_LEN)) != 0) ||
   2514   1.2      rin 			    (mcnt >= MAX_NUM_MULTICAST_ADDRESSES)) {
   2515   1.2      rin 				/*
   2516   1.2      rin 				 * We must listen to a range of multicast
   2517   1.2      rin 				 * addresses. For now, just accept all
   2518   1.2      rin 				 * multicasts, rather than trying to set only
   2519   1.2      rin 				 * those filter bits needed to match the range.
   2520   1.2      rin 				 * (At this time, the only use of address
   2521   1.2      rin 				 * ranges is for IP multicast routing, for
   2522   1.2      rin 				 * which the range is big enough to require all
   2523   1.2      rin 				 * bits set.)
   2524   1.2      rin 				 */
   2525   1.2      rin 				goto allmulti;
   2526   1.2      rin 			}
   2527   1.2      rin 			DPRINTF(CFG, "%d: %s\n", mcnt,
   2528   1.2      rin 			    ether_sprintf(enm->enm_addrlo));
   2529   1.2      rin 			memcpy(&sc->mta[mcnt * ETHER_ADDR_LEN],
   2530   1.2      rin 			    enm->enm_addrlo, ETHER_ADDR_LEN);
   2531   1.2      rin 
   2532   1.1      rin 			mcnt++;
   2533   1.1      rin 			ETHER_NEXT_MULTI(step, enm);
   2534   1.1      rin 		}
   2535   1.2      rin 		ec->ec_flags &= ~ETHER_F_ALLMULTI;
   2536   1.2      rin 		ETHER_UNLOCK(ec);
   2537   1.1      rin 
   2538   1.2      rin 		DPRINTF(CFG, "hw filter\n");
   2539   1.2      rin 		igc_update_mc_addr_list(&sc->hw, sc->mta, mcnt);
   2540   1.1      rin 	}
   2541   1.1      rin 
   2542   1.2      rin 	IGC_WRITE_REG(&sc->hw, IGC_RCTL, rctl);
   2543   1.1      rin }
   2544   1.1      rin 
   2545   1.2      rin static void
   2546   1.1      rin igc_update_link_status(struct igc_softc *sc)
   2547   1.1      rin {
   2548   1.2      rin 	struct ifnet *ifp = &sc->sc_ec.ec_if;
   2549   1.1      rin 	struct igc_hw *hw = &sc->hw;
   2550   1.1      rin 
   2551  1.11  msaitoh 	if (hw->mac.get_link_status == true)
   2552  1.11  msaitoh 		igc_check_for_link(hw);
   2553  1.11  msaitoh 
   2554   1.1      rin 	if (IGC_READ_REG(&sc->hw, IGC_STATUS) & IGC_STATUS_LU) {
   2555   1.1      rin 		if (sc->link_active == 0) {
   2556   1.1      rin 			igc_get_speed_and_duplex(hw, &sc->link_speed,
   2557   1.1      rin 			    &sc->link_duplex);
   2558   1.1      rin 			sc->link_active = 1;
   2559   1.1      rin 			ifp->if_baudrate = IF_Mbps(sc->link_speed);
   2560   1.2      rin 			if_link_state_change(ifp, LINK_STATE_UP);
   2561   1.1      rin 		}
   2562   1.1      rin 	} else {
   2563   1.1      rin 		if (sc->link_active == 1) {
   2564   1.1      rin 			ifp->if_baudrate = sc->link_speed = 0;
   2565   1.1      rin 			sc->link_duplex = 0;
   2566   1.1      rin 			sc->link_active = 0;
   2567   1.2      rin 			if_link_state_change(ifp, LINK_STATE_DOWN);
   2568   1.1      rin 		}
   2569   1.1      rin 	}
   2570   1.1      rin }
   2571   1.1      rin 
   2572   1.1      rin /*********************************************************************
   2573   1.1      rin  *
   2574   1.1      rin  *  Get a buffer from system mbuf buffer pool.
   2575   1.1      rin  *
   2576   1.1      rin  **********************************************************************/
   2577   1.2      rin static int
   2578   1.2      rin igc_get_buf(struct rx_ring *rxr, int id, bool strict)
   2579   1.1      rin {
   2580   1.1      rin 	struct igc_softc *sc = rxr->sc;
   2581   1.2      rin 	struct igc_queue *q = rxr->rxr_igcq;
   2582   1.2      rin 	struct igc_rx_buf *rxbuf = &rxr->rx_buffers[id];
   2583   1.2      rin 	bus_dmamap_t map = rxbuf->map;
   2584   1.2      rin 	struct mbuf *m;
   2585   1.2      rin 	int error;
   2586   1.2      rin 
   2587   1.2      rin 	if (__predict_false(rxbuf->buf)) {
   2588   1.2      rin 		if (strict) {
   2589   1.2      rin 			DPRINTF(RX, "slot %d already has an mbuf\n", id);
   2590   1.2      rin 			return EINVAL;
   2591   1.2      rin 		}
   2592   1.2      rin 		return 0;
   2593   1.2      rin 	}
   2594   1.1      rin 
   2595   1.2      rin 	MGETHDR(m, M_DONTWAIT, MT_DATA);
   2596   1.2      rin 	if (__predict_false(m == NULL)) {
   2597   1.2      rin  enobuf:
   2598   1.2      rin 		IGC_QUEUE_EVENT(q, rx_no_mbuf, 1);
   2599   1.1      rin 		return ENOBUFS;
   2600   1.1      rin 	}
   2601   1.1      rin 
   2602   1.2      rin 	MCLGET(m, M_DONTWAIT);
   2603   1.2      rin 	if (__predict_false(!(m->m_flags & M_EXT))) {
   2604   1.2      rin 		m_freem(m);
   2605   1.2      rin 		goto enobuf;
   2606   1.2      rin 	}
   2607   1.1      rin 
   2608   1.1      rin 	m->m_len = m->m_pkthdr.len = sc->rx_mbuf_sz;
   2609   1.1      rin 
   2610   1.2      rin 	error = bus_dmamap_load_mbuf(rxr->rxdma.dma_tag, map, m,
   2611   1.2      rin 	    BUS_DMA_READ | BUS_DMA_NOWAIT);
   2612   1.1      rin 	if (error) {
   2613   1.1      rin 		m_freem(m);
   2614   1.1      rin 		return error;
   2615   1.1      rin 	}
   2616   1.1      rin 
   2617   1.2      rin 	bus_dmamap_sync(rxr->rxdma.dma_tag, map, 0,
   2618   1.2      rin 	    map->dm_mapsize, BUS_DMASYNC_PREREAD);
   2619   1.1      rin 	rxbuf->buf = m;
   2620   1.1      rin 
   2621   1.2      rin 	union igc_adv_rx_desc *rxdesc = &rxr->rx_base[id];
   2622   1.2      rin 	igc_rxdesc_sync(rxr, id, BUS_DMASYNC_POSTWRITE | BUS_DMASYNC_POSTREAD);
   2623   1.2      rin 	rxdesc->read.pkt_addr = htole64(map->dm_segs[0].ds_addr);
   2624   1.2      rin 	igc_rxdesc_sync(rxr, id, BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
   2625   1.1      rin 
   2626   1.1      rin 	return 0;
   2627   1.1      rin }
   2628   1.1      rin 
   2629   1.2      rin static void
   2630   1.1      rin igc_configure_queues(struct igc_softc *sc)
   2631   1.1      rin {
   2632   1.1      rin 	struct igc_hw *hw = &sc->hw;
   2633   1.2      rin 	uint32_t ivar;
   2634   1.1      rin 
   2635   1.1      rin 	/* First turn on RSS capability */
   2636   1.1      rin 	IGC_WRITE_REG(hw, IGC_GPIE, IGC_GPIE_MSIX_MODE | IGC_GPIE_EIAME |
   2637   1.1      rin 	    IGC_GPIE_PBA | IGC_GPIE_NSICR);
   2638   1.1      rin 
   2639   1.1      rin 	/* Set the starting interrupt rate */
   2640   1.2      rin 	uint32_t newitr = (4000000 / MAX_INTS_PER_SEC) & 0x7FFC;
   2641   1.1      rin 	newitr |= IGC_EITR_CNT_IGNR;
   2642   1.1      rin 
   2643   1.1      rin 	/* Turn on MSI-X */
   2644   1.2      rin 	uint32_t newmask = 0;
   2645   1.2      rin 	for (int iq = 0; iq < sc->sc_nqueues; iq++) {
   2646   1.2      rin 		struct igc_queue *q = &sc->queues[iq];
   2647   1.2      rin 
   2648   1.1      rin 		/* RX entries */
   2649   1.2      rin 		igc_set_queues(sc, iq, q->msix, 0);
   2650   1.1      rin 		/* TX entries */
   2651   1.2      rin 		igc_set_queues(sc, iq, q->msix, 1);
   2652   1.2      rin 		newmask |= q->eims;
   2653   1.2      rin 		IGC_WRITE_REG(hw, IGC_EITR(q->msix), newitr);
   2654   1.2      rin 	}
   2655   1.2      rin 	sc->msix_queuesmask = newmask;
   2656   1.2      rin 
   2657   1.2      rin #if 1
   2658   1.2      rin 	ivar = IGC_READ_REG_ARRAY(hw, IGC_IVAR0, 0);
   2659   1.2      rin 	DPRINTF(CFG, "ivar(0)=0x%x\n", ivar);
   2660   1.2      rin 	ivar = IGC_READ_REG_ARRAY(hw, IGC_IVAR0, 1);
   2661   1.2      rin 	DPRINTF(CFG, "ivar(1)=0x%x\n", ivar);
   2662   1.2      rin #endif
   2663   1.1      rin 
   2664   1.1      rin 	/* And for the link interrupt */
   2665   1.1      rin 	ivar = (sc->linkvec | IGC_IVAR_VALID) << 8;
   2666   1.1      rin 	sc->msix_linkmask = 1 << sc->linkvec;
   2667   1.1      rin 	IGC_WRITE_REG(hw, IGC_IVAR_MISC, ivar);
   2668   1.1      rin }
   2669   1.1      rin 
   2670   1.2      rin static void
   2671   1.1      rin igc_set_queues(struct igc_softc *sc, uint32_t entry, uint32_t vector, int type)
   2672   1.1      rin {
   2673   1.1      rin 	struct igc_hw *hw = &sc->hw;
   2674   1.2      rin 	const uint32_t index = entry >> 1;
   2675   1.2      rin 	uint32_t ivar = IGC_READ_REG_ARRAY(hw, IGC_IVAR0, index);
   2676   1.1      rin 
   2677   1.1      rin 	if (type) {
   2678   1.1      rin 		if (entry & 1) {
   2679   1.1      rin 			ivar &= 0x00FFFFFF;
   2680   1.1      rin 			ivar |= (vector | IGC_IVAR_VALID) << 24;
   2681   1.1      rin 		} else {
   2682   1.1      rin 			ivar &= 0xFFFF00FF;
   2683   1.1      rin 			ivar |= (vector | IGC_IVAR_VALID) << 8;
   2684   1.1      rin 		}
   2685   1.1      rin 	} else {
   2686   1.1      rin 		if (entry & 1) {
   2687   1.1      rin 			ivar &= 0xFF00FFFF;
   2688   1.1      rin 			ivar |= (vector | IGC_IVAR_VALID) << 16;
   2689   1.1      rin 		} else {
   2690   1.1      rin 			ivar &= 0xFFFFFF00;
   2691   1.1      rin 			ivar |= vector | IGC_IVAR_VALID;
   2692   1.1      rin 		}
   2693   1.1      rin 	}
   2694   1.1      rin 	IGC_WRITE_REG_ARRAY(hw, IGC_IVAR0, index, ivar);
   2695   1.1      rin }
   2696   1.1      rin 
   2697   1.2      rin static void
   2698   1.1      rin igc_enable_queue(struct igc_softc *sc, uint32_t eims)
   2699   1.1      rin {
   2700   1.1      rin 	IGC_WRITE_REG(&sc->hw, IGC_EIMS, eims);
   2701   1.1      rin }
   2702   1.1      rin 
   2703   1.2      rin static void
   2704   1.1      rin igc_enable_intr(struct igc_softc *sc)
   2705   1.1      rin {
   2706   1.1      rin 	struct igc_hw *hw = &sc->hw;
   2707   1.1      rin 
   2708   1.2      rin 	if (sc->sc_intr_type == PCI_INTR_TYPE_MSIX) {
   2709   1.2      rin 		const uint32_t mask = sc->msix_queuesmask | sc->msix_linkmask;
   2710   1.2      rin 
   2711   1.2      rin 		IGC_WRITE_REG(hw, IGC_EIAC, mask);
   2712   1.2      rin 		IGC_WRITE_REG(hw, IGC_EIAM, mask);
   2713   1.2      rin 		IGC_WRITE_REG(hw, IGC_EIMS, mask);
   2714   1.2      rin 		IGC_WRITE_REG(hw, IGC_IMS, IGC_IMS_LSC);
   2715   1.2      rin 	} else {
   2716   1.2      rin 		IGC_WRITE_REG(hw, IGC_IMS, IMS_ENABLE_MASK);
   2717   1.2      rin 	}
   2718   1.1      rin 	IGC_WRITE_FLUSH(hw);
   2719   1.1      rin }
   2720   1.1      rin 
   2721   1.2      rin static void
   2722   1.1      rin igc_disable_intr(struct igc_softc *sc)
   2723   1.1      rin {
   2724   1.1      rin 	struct igc_hw *hw = &sc->hw;
   2725   1.1      rin 
   2726   1.2      rin 	if (sc->sc_intr_type == PCI_INTR_TYPE_MSIX) {
   2727   1.2      rin 		IGC_WRITE_REG(hw, IGC_EIMC, 0xffffffff);
   2728   1.2      rin 		IGC_WRITE_REG(hw, IGC_EIAC, 0);
   2729   1.2      rin 	}
   2730   1.1      rin 	IGC_WRITE_REG(hw, IGC_IMC, 0xffffffff);
   2731   1.1      rin 	IGC_WRITE_FLUSH(hw);
   2732   1.1      rin }
   2733   1.1      rin 
   2734   1.2      rin static int
   2735   1.1      rin igc_intr_link(void *arg)
   2736   1.1      rin {
   2737   1.1      rin 	struct igc_softc *sc = (struct igc_softc *)arg;
   2738   1.2      rin 	const uint32_t reg_icr = IGC_READ_REG(&sc->hw, IGC_ICR);
   2739   1.2      rin 
   2740   1.2      rin 	IGC_GLOBAL_EVENT(sc, link, 1);
   2741   1.1      rin 
   2742   1.1      rin 	if (reg_icr & IGC_ICR_LSC) {
   2743   1.2      rin 		mutex_enter(&sc->sc_core_lock);
   2744   1.1      rin 		sc->hw.mac.get_link_status = true;
   2745   1.1      rin 		igc_update_link_status(sc);
   2746   1.2      rin 		mutex_exit(&sc->sc_core_lock);
   2747   1.1      rin 	}
   2748   1.1      rin 
   2749   1.1      rin 	IGC_WRITE_REG(&sc->hw, IGC_IMS, IGC_IMS_LSC);
   2750   1.1      rin 	IGC_WRITE_REG(&sc->hw, IGC_EIMS, sc->msix_linkmask);
   2751   1.1      rin 
   2752   1.1      rin 	return 1;
   2753   1.1      rin }
   2754   1.1      rin 
   2755   1.2      rin static int
   2756   1.1      rin igc_intr_queue(void *arg)
   2757   1.1      rin {
   2758   1.1      rin 	struct igc_queue *iq = arg;
   2759   1.1      rin 	struct igc_softc *sc = iq->sc;
   2760   1.2      rin 	struct ifnet *ifp = &sc->sc_ec.ec_if;
   2761   1.2      rin 	struct rx_ring *rxr = iq->rxr;
   2762   1.2      rin 	struct tx_ring *txr = iq->txr;
   2763   1.2      rin 	const u_int txlimit = sc->sc_tx_intr_process_limit,
   2764   1.2      rin 		    rxlimit = sc->sc_rx_intr_process_limit;
   2765   1.2      rin 	bool txmore, rxmore;
   2766   1.2      rin 
   2767   1.2      rin 	IGC_QUEUE_EVENT(iq, irqs, 1);
   2768   1.2      rin 
   2769   1.2      rin 	if (__predict_false(!ISSET(ifp->if_flags, IFF_RUNNING)))
   2770   1.2      rin 		return 0;
   2771   1.2      rin 
   2772   1.2      rin 	mutex_enter(&txr->txr_lock);
   2773   1.2      rin 	txmore = igc_txeof(txr, txlimit);
   2774   1.2      rin 	mutex_exit(&txr->txr_lock);
   2775   1.2      rin 	mutex_enter(&rxr->rxr_lock);
   2776   1.2      rin 	rxmore = igc_rxeof(rxr, rxlimit);
   2777   1.2      rin 	mutex_exit(&rxr->rxr_lock);
   2778   1.2      rin 
   2779   1.2      rin 	if (txmore || rxmore) {
   2780   1.2      rin 		IGC_QUEUE_EVENT(iq, req, 1);
   2781   1.2      rin 		igc_sched_handle_queue(sc, iq);
   2782   1.2      rin 	} else {
   2783   1.2      rin 		igc_enable_queue(sc, iq->eims);
   2784   1.2      rin 	}
   2785   1.2      rin 
   2786   1.2      rin 	return 1;
   2787   1.2      rin }
   2788   1.2      rin 
   2789   1.2      rin static int
   2790   1.2      rin igc_intr(void *arg)
   2791   1.2      rin {
   2792   1.2      rin 	struct igc_softc *sc = arg;
   2793   1.2      rin 	struct ifnet *ifp = &sc->sc_ec.ec_if;
   2794   1.2      rin 	struct igc_queue *iq = &sc->queues[0];
   2795   1.1      rin 	struct rx_ring *rxr = iq->rxr;
   2796   1.1      rin 	struct tx_ring *txr = iq->txr;
   2797   1.2      rin 	const u_int txlimit = sc->sc_tx_intr_process_limit,
   2798   1.2      rin 		    rxlimit = sc->sc_rx_intr_process_limit;
   2799   1.2      rin 	bool txmore, rxmore;
   2800   1.2      rin 
   2801   1.2      rin 	if (__predict_false(!ISSET(ifp->if_flags, IFF_RUNNING)))
   2802   1.2      rin 		return 0;
   2803   1.2      rin 
   2804   1.2      rin 	const uint32_t reg_icr = IGC_READ_REG(&sc->hw, IGC_ICR);
   2805   1.2      rin 	DPRINTF(MISC, "reg_icr=0x%x\n", reg_icr);
   2806   1.2      rin 
   2807   1.2      rin 	/* Definitely not our interrupt. */
   2808   1.2      rin 	if (reg_icr == 0x0) {
   2809  1.14      rin 		DPRINTF(MISC, "not for me\n");
   2810   1.2      rin 		return 0;
   2811   1.2      rin 	}
   2812   1.2      rin 
   2813   1.2      rin 	IGC_QUEUE_EVENT(iq, irqs, 1);
   2814   1.2      rin 
   2815   1.2      rin 	/* Hot eject? */
   2816   1.2      rin 	if (__predict_false(reg_icr == 0xffffffff)) {
   2817   1.2      rin 		DPRINTF(MISC, "hot eject\n");
   2818   1.2      rin 		return 0;
   2819   1.2      rin 	}
   2820   1.2      rin 
   2821   1.2      rin 	if (__predict_false(!(reg_icr & IGC_ICR_INT_ASSERTED))) {
   2822   1.2      rin 		DPRINTF(MISC, "not set IGC_ICR_INT_ASSERTED");
   2823   1.2      rin 		return 0;
   2824   1.2      rin 	}
   2825   1.2      rin 
   2826   1.2      rin 	/*
   2827   1.2      rin 	 * Only MSI-X interrupts have one-shot behavior by taking advantage
   2828   1.2      rin 	 * of the EIAC register.  Thus, explicitly disable interrupts.  This
   2829   1.2      rin 	 * also works around the MSI message reordering errata on certain
   2830   1.2      rin 	 * systems.
   2831   1.2      rin 	 */
   2832   1.2      rin 	igc_disable_intr(sc);
   2833   1.1      rin 
   2834   1.2      rin 	mutex_enter(&txr->txr_lock);
   2835   1.2      rin 	txmore = igc_txeof(txr, txlimit);
   2836   1.2      rin 	mutex_exit(&txr->txr_lock);
   2837   1.2      rin 	mutex_enter(&rxr->rxr_lock);
   2838   1.2      rin 	rxmore = igc_rxeof(rxr, rxlimit);
   2839   1.2      rin 	mutex_exit(&rxr->rxr_lock);
   2840   1.2      rin 
   2841   1.2      rin 	/* Link status change */
   2842   1.2      rin 	// XXXX FreeBSD checks IGC_ICR_RXSEQ
   2843   1.2      rin 	if (__predict_false(reg_icr & IGC_ICR_LSC)) {
   2844   1.2      rin 		IGC_GLOBAL_EVENT(sc, link, 1);
   2845   1.2      rin 		mutex_enter(&sc->sc_core_lock);
   2846   1.2      rin 		sc->hw.mac.get_link_status = true;
   2847   1.2      rin 		igc_update_link_status(sc);
   2848   1.2      rin 		mutex_exit(&sc->sc_core_lock);
   2849   1.1      rin 	}
   2850   1.1      rin 
   2851   1.2      rin 	if (txmore || rxmore) {
   2852   1.2      rin 		IGC_QUEUE_EVENT(iq, req, 1);
   2853   1.2      rin 		igc_sched_handle_queue(sc, iq);
   2854   1.2      rin 	} else {
   2855   1.2      rin 		igc_enable_intr(sc);
   2856   1.2      rin 	}
   2857   1.1      rin 
   2858   1.1      rin 	return 1;
   2859   1.1      rin }
   2860   1.1      rin 
   2861   1.2      rin static void
   2862   1.2      rin igc_handle_queue(void *arg)
   2863   1.2      rin {
   2864   1.2      rin 	struct igc_queue *iq = arg;
   2865   1.2      rin 	struct igc_softc *sc = iq->sc;
   2866   1.2      rin 	struct tx_ring *txr = iq->txr;
   2867   1.2      rin 	struct rx_ring *rxr = iq->rxr;
   2868   1.2      rin 	const u_int txlimit = sc->sc_tx_process_limit,
   2869   1.2      rin 		    rxlimit = sc->sc_rx_process_limit;
   2870   1.2      rin 	bool txmore, rxmore;
   2871   1.2      rin 
   2872   1.2      rin 	IGC_QUEUE_EVENT(iq, handleq, 1);
   2873   1.2      rin 
   2874   1.2      rin 	mutex_enter(&txr->txr_lock);
   2875   1.2      rin 	txmore = igc_txeof(txr, txlimit);
   2876   1.2      rin 	/* for ALTQ, dequeue from if_snd */
   2877   1.2      rin 	if (txr->me == 0) {
   2878   1.2      rin 		struct ifnet *ifp = &sc->sc_ec.ec_if;
   2879   1.2      rin 
   2880   1.2      rin 		igc_tx_common_locked(ifp, txr, IGC_TX_START);
   2881   1.2      rin 	}
   2882   1.2      rin 	mutex_exit(&txr->txr_lock);
   2883   1.2      rin 
   2884   1.2      rin 	mutex_enter(&rxr->rxr_lock);
   2885   1.2      rin 	rxmore = igc_rxeof(rxr, rxlimit);
   2886   1.2      rin 	mutex_exit(&rxr->rxr_lock);
   2887   1.2      rin 
   2888   1.2      rin 	if (txmore || rxmore) {
   2889   1.2      rin 		igc_sched_handle_queue(sc, iq);
   2890   1.2      rin 	} else {
   2891   1.2      rin 		if (sc->sc_intr_type == PCI_INTR_TYPE_MSIX)
   2892   1.2      rin 			igc_enable_queue(sc, iq->eims);
   2893   1.2      rin 		else
   2894   1.2      rin 			igc_enable_intr(sc);
   2895   1.2      rin 	}
   2896   1.2      rin }
   2897   1.2      rin 
   2898   1.2      rin static void
   2899   1.2      rin igc_handle_queue_work(struct work *wk, void *context)
   2900   1.2      rin {
   2901   1.2      rin 	struct igc_queue *iq =
   2902   1.2      rin 	    container_of(wk, struct igc_queue, igcq_wq_cookie);
   2903   1.2      rin 
   2904   1.2      rin 	igc_handle_queue(iq);
   2905   1.2      rin }
   2906   1.2      rin 
   2907   1.2      rin static void
   2908   1.2      rin igc_sched_handle_queue(struct igc_softc *sc, struct igc_queue *iq)
   2909   1.2      rin {
   2910   1.2      rin 
   2911   1.2      rin 	if (iq->igcq_workqueue) {
   2912   1.2      rin 		/* XXXRO notyet */
   2913   1.2      rin 		workqueue_enqueue(sc->sc_queue_wq, &iq->igcq_wq_cookie,
   2914   1.2      rin 		    curcpu());
   2915   1.2      rin 	} else {
   2916   1.2      rin 		softint_schedule(iq->igcq_si);
   2917   1.2      rin 	}
   2918   1.2      rin }
   2919   1.2      rin 
   2920   1.2      rin static void
   2921   1.2      rin igc_barrier_handle_queue(struct igc_softc *sc)
   2922   1.2      rin {
   2923   1.2      rin 
   2924   1.2      rin 	if (sc->sc_txrx_workqueue) {
   2925   1.2      rin 		for (int iq = 0; iq < sc->sc_nqueues; iq++) {
   2926   1.2      rin 			struct igc_queue *q = &sc->queues[iq];
   2927   1.2      rin 
   2928   1.2      rin 			workqueue_wait(sc->sc_queue_wq, &q->igcq_wq_cookie);
   2929   1.2      rin 		}
   2930   1.2      rin 	} else {
   2931   1.2      rin 		xc_barrier(0);
   2932   1.2      rin 	}
   2933   1.2      rin }
   2934   1.2      rin 
   2935   1.1      rin /*********************************************************************
   2936   1.1      rin  *
   2937   1.1      rin  *  Allocate memory for tx_buffer structures. The tx_buffer stores all
   2938   1.1      rin  *  the information needed to transmit a packet on the wire.
   2939   1.1      rin  *
   2940   1.1      rin  **********************************************************************/
   2941   1.2      rin static int
   2942   1.1      rin igc_allocate_transmit_buffers(struct tx_ring *txr)
   2943   1.1      rin {
   2944   1.1      rin 	struct igc_softc *sc = txr->sc;
   2945   1.2      rin 	int error;
   2946   1.1      rin 
   2947   1.2      rin 	txr->tx_buffers =
   2948   1.2      rin 	    kmem_zalloc(sc->num_tx_desc * sizeof(struct igc_tx_buf), KM_SLEEP);
   2949   1.1      rin 	txr->txtag = txr->txdma.dma_tag;
   2950   1.1      rin 
   2951   1.1      rin 	/* Create the descriptor buffer dma maps. */
   2952   1.2      rin 	for (int id = 0; id < sc->num_tx_desc; id++) {
   2953   1.2      rin 		struct igc_tx_buf *txbuf = &txr->tx_buffers[id];
   2954   1.2      rin 
   2955   1.2      rin 		error = bus_dmamap_create(txr->txdma.dma_tag,
   2956   1.2      rin 		    round_page(IGC_TSO_SIZE + sizeof(struct ether_vlan_header)),
   2957   1.1      rin 		    IGC_MAX_SCATTER, PAGE_SIZE, 0, BUS_DMA_NOWAIT, &txbuf->map);
   2958   1.1      rin 		if (error != 0) {
   2959   1.2      rin 			aprint_error_dev(sc->sc_dev,
   2960   1.2      rin 			    "unable to create TX DMA map\n");
   2961   1.1      rin 			goto fail;
   2962   1.1      rin 		}
   2963   1.2      rin 
   2964   1.2      rin 		txbuf->eop_index = -1;
   2965   1.1      rin 	}
   2966   1.1      rin 
   2967   1.1      rin 	return 0;
   2968   1.2      rin  fail:
   2969   1.1      rin 	return error;
   2970   1.1      rin }
   2971   1.1      rin 
   2972   1.1      rin 
   2973   1.1      rin /*********************************************************************
   2974   1.1      rin  *
   2975   1.1      rin  *  Allocate and initialize transmit structures.
   2976   1.1      rin  *
   2977   1.1      rin  **********************************************************************/
   2978   1.2      rin static int
   2979   1.1      rin igc_setup_transmit_structures(struct igc_softc *sc)
   2980   1.1      rin {
   2981   1.1      rin 
   2982   1.2      rin 	for (int iq = 0; iq < sc->sc_nqueues; iq++) {
   2983   1.2      rin 		struct tx_ring *txr = &sc->tx_rings[iq];
   2984   1.2      rin 
   2985   1.1      rin 		if (igc_setup_transmit_ring(txr))
   2986   1.1      rin 			goto fail;
   2987   1.1      rin 	}
   2988   1.1      rin 
   2989   1.1      rin 	return 0;
   2990   1.2      rin  fail:
   2991   1.1      rin 	igc_free_transmit_structures(sc);
   2992   1.1      rin 	return ENOBUFS;
   2993   1.1      rin }
   2994   1.1      rin 
   2995   1.1      rin /*********************************************************************
   2996   1.1      rin  *
   2997   1.1      rin  *  Initialize a transmit ring.
   2998   1.1      rin  *
   2999   1.1      rin  **********************************************************************/
   3000   1.2      rin static int
   3001   1.1      rin igc_setup_transmit_ring(struct tx_ring *txr)
   3002   1.1      rin {
   3003   1.1      rin 	struct igc_softc *sc = txr->sc;
   3004   1.1      rin 
   3005   1.1      rin 	/* Now allocate transmit buffers for the ring. */
   3006   1.1      rin 	if (igc_allocate_transmit_buffers(txr))
   3007   1.1      rin 		return ENOMEM;
   3008   1.1      rin 
   3009   1.1      rin 	/* Clear the old ring contents */
   3010   1.2      rin 	memset(txr->tx_base, 0,
   3011   1.2      rin 	    sizeof(union igc_adv_tx_desc) * sc->num_tx_desc);
   3012   1.1      rin 
   3013   1.1      rin 	/* Reset indices. */
   3014   1.1      rin 	txr->next_avail_desc = 0;
   3015   1.1      rin 	txr->next_to_clean = 0;
   3016   1.1      rin 
   3017   1.1      rin 	bus_dmamap_sync(txr->txdma.dma_tag, txr->txdma.dma_map, 0,
   3018   1.1      rin 	    txr->txdma.dma_map->dm_mapsize,
   3019   1.1      rin 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
   3020   1.1      rin 
   3021   1.2      rin 	txr->txr_interq = pcq_create(sc->num_tx_desc, KM_SLEEP);
   3022   1.2      rin 
   3023   1.2      rin 	mutex_init(&txr->txr_lock, MUTEX_DEFAULT, IPL_NET);
   3024   1.2      rin 
   3025   1.1      rin 	return 0;
   3026   1.1      rin }
   3027   1.1      rin 
   3028   1.1      rin /*********************************************************************
   3029   1.1      rin  *
   3030   1.1      rin  *  Enable transmit unit.
   3031   1.1      rin  *
   3032   1.1      rin  **********************************************************************/
   3033   1.2      rin static void
   3034   1.1      rin igc_initialize_transmit_unit(struct igc_softc *sc)
   3035   1.1      rin {
   3036   1.2      rin 	struct ifnet *ifp = &sc->sc_ec.ec_if;
   3037   1.1      rin 	struct igc_hw *hw = &sc->hw;
   3038   1.1      rin 
   3039   1.1      rin 	/* Setup the Base and Length of the TX descriptor ring. */
   3040   1.2      rin 	for (int iq = 0; iq < sc->sc_nqueues; iq++) {
   3041   1.2      rin 		struct tx_ring *txr = &sc->tx_rings[iq];
   3042   1.2      rin 		const uint64_t bus_addr =
   3043   1.2      rin 		    txr->txdma.dma_map->dm_segs[0].ds_addr;
   3044   1.1      rin 
   3045   1.1      rin 		/* Base and len of TX ring */
   3046   1.2      rin 		IGC_WRITE_REG(hw, IGC_TDLEN(iq),
   3047   1.1      rin 		    sc->num_tx_desc * sizeof(union igc_adv_tx_desc));
   3048   1.2      rin 		IGC_WRITE_REG(hw, IGC_TDBAH(iq), (uint32_t)(bus_addr >> 32));
   3049   1.2      rin 		IGC_WRITE_REG(hw, IGC_TDBAL(iq), (uint32_t)bus_addr);
   3050   1.1      rin 
   3051   1.1      rin 		/* Init the HEAD/TAIL indices */
   3052   1.2      rin 		IGC_WRITE_REG(hw, IGC_TDT(iq), 0 /* XXX txr->next_avail_desc */);
   3053   1.2      rin 		IGC_WRITE_REG(hw, IGC_TDH(iq), 0);
   3054   1.1      rin 
   3055   1.1      rin 		txr->watchdog_timer = 0;
   3056   1.1      rin 
   3057   1.2      rin 		uint32_t txdctl = 0;	/* Clear txdctl */
   3058   1.1      rin 		txdctl |= 0x1f;		/* PTHRESH */
   3059   1.1      rin 		txdctl |= 1 << 8;	/* HTHRESH */
   3060   1.1      rin 		txdctl |= 1 << 16;	/* WTHRESH */
   3061   1.1      rin 		txdctl |= 1 << 22;	/* Reserved bit 22 must always be 1 */
   3062   1.1      rin 		txdctl |= IGC_TXDCTL_GRAN;
   3063   1.1      rin 		txdctl |= 1 << 25;	/* LWTHRESH */
   3064   1.1      rin 
   3065   1.2      rin 		IGC_WRITE_REG(hw, IGC_TXDCTL(iq), txdctl);
   3066   1.1      rin 	}
   3067   1.1      rin 	ifp->if_timer = 0;
   3068   1.1      rin 
   3069   1.1      rin 	/* Program the Transmit Control Register */
   3070   1.2      rin 	uint32_t tctl = IGC_READ_REG(&sc->hw, IGC_TCTL);
   3071   1.1      rin 	tctl &= ~IGC_TCTL_CT;
   3072   1.1      rin 	tctl |= (IGC_TCTL_PSP | IGC_TCTL_RTLC | IGC_TCTL_EN |
   3073   1.1      rin 	    (IGC_COLLISION_THRESHOLD << IGC_CT_SHIFT));
   3074   1.1      rin 
   3075   1.1      rin 	/* This write will effectively turn on the transmit unit. */
   3076   1.1      rin 	IGC_WRITE_REG(&sc->hw, IGC_TCTL, tctl);
   3077   1.1      rin }
   3078   1.1      rin 
   3079   1.1      rin /*********************************************************************
   3080   1.1      rin  *
   3081   1.1      rin  *  Free all transmit rings.
   3082   1.1      rin  *
   3083   1.1      rin  **********************************************************************/
   3084   1.2      rin static void
   3085   1.1      rin igc_free_transmit_structures(struct igc_softc *sc)
   3086   1.1      rin {
   3087   1.1      rin 
   3088   1.2      rin 	for (int iq = 0; iq < sc->sc_nqueues; iq++) {
   3089   1.2      rin 		struct tx_ring *txr = &sc->tx_rings[iq];
   3090   1.2      rin 
   3091   1.1      rin 		igc_free_transmit_buffers(txr);
   3092   1.2      rin 	}
   3093   1.1      rin }
   3094   1.1      rin 
   3095   1.1      rin /*********************************************************************
   3096   1.1      rin  *
   3097   1.1      rin  *  Free transmit ring related data structures.
   3098   1.1      rin  *
   3099   1.1      rin  **********************************************************************/
   3100   1.2      rin static void
   3101   1.1      rin igc_free_transmit_buffers(struct tx_ring *txr)
   3102   1.1      rin {
   3103   1.1      rin 	struct igc_softc *sc = txr->sc;
   3104   1.1      rin 
   3105   1.1      rin 	if (txr->tx_buffers == NULL)
   3106   1.1      rin 		return;
   3107   1.1      rin 
   3108   1.2      rin 	igc_withdraw_transmit_packets(txr, true);
   3109   1.2      rin 
   3110   1.2      rin 	kmem_free(txr->tx_buffers,
   3111   1.2      rin 	    sc->num_tx_desc * sizeof(struct igc_tx_buf));
   3112   1.2      rin 	txr->tx_buffers = NULL;
   3113   1.2      rin 	txr->txtag = NULL;
   3114   1.2      rin 
   3115   1.2      rin 	pcq_destroy(txr->txr_interq);
   3116   1.2      rin 	mutex_destroy(&txr->txr_lock);
   3117   1.2      rin }
   3118   1.2      rin 
   3119   1.2      rin /*********************************************************************
   3120   1.2      rin  *
   3121   1.2      rin  *  Withdraw transmit packets.
   3122   1.2      rin  *
   3123   1.2      rin  **********************************************************************/
   3124   1.2      rin static void
   3125   1.2      rin igc_withdraw_transmit_packets(struct tx_ring *txr, bool destroy)
   3126   1.2      rin {
   3127   1.2      rin 	struct igc_softc *sc = txr->sc;
   3128   1.2      rin 	struct igc_queue *q = txr->txr_igcq;
   3129   1.2      rin 
   3130   1.2      rin 	mutex_enter(&txr->txr_lock);
   3131   1.2      rin 
   3132   1.2      rin 	for (int id = 0; id < sc->num_tx_desc; id++) {
   3133   1.2      rin 		union igc_adv_tx_desc *txdesc = &txr->tx_base[id];
   3134   1.2      rin 
   3135   1.2      rin 		igc_txdesc_sync(txr, id,
   3136   1.2      rin 		    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
   3137   1.2      rin 		txdesc->read.buffer_addr = 0;
   3138   1.2      rin 		txdesc->read.cmd_type_len = 0;
   3139   1.2      rin 		txdesc->read.olinfo_status = 0;
   3140   1.2      rin 		igc_txdesc_sync(txr, id,
   3141   1.2      rin 		    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
   3142   1.2      rin 
   3143   1.2      rin 		struct igc_tx_buf *txbuf = &txr->tx_buffers[id];
   3144   1.2      rin 		bus_dmamap_t map = txbuf->map;
   3145   1.2      rin 
   3146   1.2      rin 		if (map != NULL && map->dm_nsegs > 0) {
   3147   1.2      rin 			bus_dmamap_sync(txr->txdma.dma_tag, map,
   3148   1.2      rin 			    0, map->dm_mapsize, BUS_DMASYNC_POSTWRITE);
   3149   1.2      rin 			bus_dmamap_unload(txr->txdma.dma_tag, map);
   3150   1.1      rin 		}
   3151   1.1      rin 		if (txbuf->m_head != NULL) {
   3152   1.1      rin 			m_freem(txbuf->m_head);
   3153   1.1      rin 			txbuf->m_head = NULL;
   3154   1.1      rin 		}
   3155   1.2      rin 		if (map != NULL && destroy) {
   3156   1.2      rin 			bus_dmamap_destroy(txr->txdma.dma_tag, map);
   3157   1.1      rin 			txbuf->map = NULL;
   3158   1.1      rin 		}
   3159   1.2      rin 		txbuf->eop_index = -1;
   3160   1.2      rin 
   3161   1.2      rin 		txr->next_avail_desc = 0;
   3162   1.2      rin 		txr->next_to_clean = 0;
   3163   1.2      rin 	}
   3164   1.2      rin 
   3165   1.2      rin 	struct mbuf *m;
   3166   1.2      rin 	while ((m = pcq_get(txr->txr_interq)) != NULL) {
   3167   1.2      rin 		IGC_QUEUE_EVENT(q, tx_pcq_drop, 1);
   3168   1.2      rin 		m_freem(m);
   3169   1.1      rin 	}
   3170   1.1      rin 
   3171   1.2      rin 	mutex_exit(&txr->txr_lock);
   3172   1.1      rin }
   3173   1.1      rin 
   3174   1.1      rin 
   3175   1.1      rin /*********************************************************************
   3176   1.1      rin  *
   3177   1.1      rin  *  Advanced Context Descriptor setup for VLAN, CSUM or TSO
   3178   1.1      rin  *
   3179   1.1      rin  **********************************************************************/
   3180   1.1      rin 
   3181   1.2      rin static int
   3182   1.1      rin igc_tx_ctx_setup(struct tx_ring *txr, struct mbuf *mp, int prod,
   3183   1.2      rin     uint32_t *cmd_type_len, uint32_t *olinfo_status)
   3184   1.1      rin {
   3185   1.2      rin 	struct ether_vlan_header *evl;
   3186   1.1      rin 	uint32_t type_tucmd_mlhl = 0;
   3187   1.1      rin 	uint32_t vlan_macip_lens = 0;
   3188   1.2      rin 	uint32_t ehlen, iphlen;
   3189   1.2      rin 	uint16_t ehtype;
   3190   1.1      rin 	int off = 0;
   3191   1.1      rin 
   3192   1.2      rin 	const int csum_flags = mp->m_pkthdr.csum_flags;
   3193   1.2      rin 
   3194   1.2      rin 	/* First check if TSO is to be used */
   3195   1.2      rin 	if ((csum_flags & (M_CSUM_TSOv4 | M_CSUM_TSOv6)) != 0) {
   3196   1.2      rin 		return igc_tso_setup(txr, mp, prod, cmd_type_len,
   3197   1.2      rin 		    olinfo_status);
   3198   1.2      rin 	}
   3199   1.2      rin 
   3200   1.2      rin 	const bool v4 = (csum_flags &
   3201   1.2      rin 	    (M_CSUM_IPv4 | M_CSUM_TCPv4 | M_CSUM_UDPv4)) != 0;
   3202   1.2      rin 	const bool v6 = (csum_flags & (M_CSUM_UDPv6 | M_CSUM_TCPv6)) != 0;
   3203   1.2      rin 
   3204   1.2      rin 	/* Indicate the whole packet as payload when not doing TSO */
   3205   1.2      rin 	*olinfo_status |= mp->m_pkthdr.len << IGC_ADVTXD_PAYLEN_SHIFT;
   3206   1.1      rin 
   3207   1.1      rin 	/*
   3208   1.1      rin 	 * In advanced descriptors the vlan tag must
   3209   1.1      rin 	 * be placed into the context descriptor. Hence
   3210   1.1      rin 	 * we need to make one even if not doing offloads.
   3211   1.1      rin 	 */
   3212   1.1      rin #if NVLAN > 0
   3213   1.2      rin 	if (vlan_has_tag(mp)) {
   3214   1.2      rin 		vlan_macip_lens |= (uint32_t)vlan_get_tag(mp)
   3215   1.2      rin 		    << IGC_ADVTXD_VLAN_SHIFT;
   3216   1.1      rin 		off = 1;
   3217   1.2      rin 	} else
   3218   1.2      rin #endif
   3219   1.2      rin 	if (!v4 && !v6)
   3220   1.2      rin 		return 0;
   3221   1.2      rin 
   3222   1.2      rin 	KASSERT(mp->m_len >= sizeof(struct ether_header));
   3223   1.2      rin 	evl = mtod(mp, struct ether_vlan_header *);
   3224   1.2      rin 	if (evl->evl_encap_proto == htons(ETHERTYPE_VLAN)) {
   3225   1.2      rin 		KASSERT(mp->m_len >= sizeof(struct ether_vlan_header));
   3226   1.2      rin 		ehlen = ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN;
   3227   1.2      rin 		ehtype = evl->evl_proto;
   3228   1.2      rin 	} else {
   3229   1.2      rin 		ehlen = ETHER_HDR_LEN;
   3230   1.2      rin 		ehtype = evl->evl_encap_proto;
   3231   1.1      rin 	}
   3232   1.1      rin 
   3233   1.2      rin 	vlan_macip_lens |= ehlen << IGC_ADVTXD_MACLEN_SHIFT;
   3234   1.2      rin 
   3235   1.2      rin #ifdef IGC_DEBUG
   3236   1.2      rin 	/*
   3237   1.2      rin 	 * For checksum offloading, L3 headers are not mandatory.
   3238   1.2      rin 	 * We use these only for consistency checks.
   3239   1.2      rin 	 */
   3240   1.2      rin 	struct ip *ip;
   3241   1.2      rin 	struct ip6_hdr *ip6;
   3242   1.2      rin 	uint8_t ipproto;
   3243   1.2      rin 	char *l3d;
   3244   1.1      rin 
   3245   1.2      rin 	if (mp->m_len == ehlen && mp->m_next != NULL)
   3246   1.2      rin 		l3d = mtod(mp->m_next, char *);
   3247   1.2      rin 	else
   3248   1.2      rin 		l3d = mtod(mp, char *) + ehlen;
   3249   1.2      rin #endif
   3250   1.1      rin 
   3251   1.2      rin 	switch (ntohs(ehtype)) {
   3252   1.2      rin 	case ETHERTYPE_IP:
   3253   1.2      rin 		iphlen = M_CSUM_DATA_IPv4_IPHL(mp->m_pkthdr.csum_data);
   3254   1.1      rin 		type_tucmd_mlhl |= IGC_ADVTXD_TUCMD_IPV4;
   3255   1.2      rin 
   3256   1.2      rin 		if ((csum_flags & M_CSUM_IPv4) != 0) {
   3257   1.1      rin 			*olinfo_status |= IGC_TXD_POPTS_IXSM << 8;
   3258   1.1      rin 			off = 1;
   3259   1.1      rin 		}
   3260   1.2      rin #ifdef IGC_DEBUG
   3261   1.2      rin 		KASSERT(!v6);
   3262   1.2      rin 		ip = (void *)l3d;
   3263   1.2      rin 		ipproto = ip->ip_p;
   3264   1.2      rin 		KASSERT(iphlen == ip->ip_hl << 2);
   3265   1.2      rin 		KASSERT((mp->m_pkthdr.csum_flags & M_CSUM_IPv4) == 0 ||
   3266   1.2      rin 		    ip->ip_sum == 0);
   3267   1.2      rin #endif
   3268   1.2      rin 		break;
   3269   1.2      rin 	case ETHERTYPE_IPV6:
   3270   1.2      rin 		iphlen = M_CSUM_DATA_IPv6_IPHL(mp->m_pkthdr.csum_data);
   3271   1.1      rin 		type_tucmd_mlhl |= IGC_ADVTXD_TUCMD_IPV6;
   3272   1.2      rin #ifdef IGC_DEBUG
   3273   1.2      rin 		KASSERT(!v4);
   3274   1.2      rin 		ip6 = (void *)l3d;
   3275   1.2      rin 		ipproto = ip6->ip6_nxt;	/* XXX */
   3276   1.2      rin 		KASSERT(iphlen == sizeof(struct ip6_hdr));
   3277   1.2      rin #endif
   3278   1.2      rin 		break;
   3279   1.2      rin 	default:
   3280   1.2      rin 		/*
   3281   1.2      rin 		 * Unknown L3 protocol. Clear L3 header length and proceed for
   3282   1.2      rin 		 * LAN as done by Linux driver.
   3283   1.2      rin 		 */
   3284   1.2      rin 		iphlen = 0;
   3285   1.2      rin #ifdef IGC_DEBUG
   3286   1.2      rin 		KASSERT(!v4 && !v6);
   3287   1.2      rin 		ipproto = 0;
   3288   1.1      rin #endif
   3289   1.2      rin 		break;
   3290   1.1      rin 	}
   3291   1.1      rin 
   3292   1.1      rin 	vlan_macip_lens |= iphlen;
   3293   1.1      rin 
   3294   1.2      rin 	const bool tcp = (csum_flags & (M_CSUM_TCPv4 | M_CSUM_TCPv6)) != 0;
   3295   1.2      rin 	const bool udp = (csum_flags & (M_CSUM_UDPv4 | M_CSUM_UDPv6)) != 0;
   3296   1.2      rin 
   3297   1.2      rin 	if (tcp) {
   3298   1.2      rin #ifdef IGC_DEBUG
   3299   1.2      rin 		KASSERTMSG(ipproto == IPPROTO_TCP, "ipproto = %d", ipproto);
   3300   1.2      rin #endif
   3301   1.1      rin 		type_tucmd_mlhl |= IGC_ADVTXD_TUCMD_L4T_TCP;
   3302   1.2      rin 		*olinfo_status |= IGC_TXD_POPTS_TXSM << 8;
   3303   1.2      rin 		off = 1;
   3304   1.2      rin 	} else if (udp) {
   3305   1.2      rin #ifdef IGC_DEBUG
   3306   1.2      rin 		KASSERTMSG(ipproto == IPPROTO_UDP, "ipproto = %d", ipproto);
   3307   1.2      rin #endif
   3308   1.1      rin 		type_tucmd_mlhl |= IGC_ADVTXD_TUCMD_L4T_UDP;
   3309   1.2      rin 		*olinfo_status |= IGC_TXD_POPTS_TXSM << 8;
   3310   1.2      rin 		off = 1;
   3311   1.1      rin 	}
   3312   1.1      rin 
   3313   1.1      rin 	if (off == 0)
   3314   1.1      rin 		return 0;
   3315   1.1      rin 
   3316   1.2      rin 	type_tucmd_mlhl |= IGC_ADVTXD_DCMD_DEXT | IGC_ADVTXD_DTYP_CTXT;
   3317   1.2      rin 
   3318   1.1      rin 	/* Now ready a context descriptor */
   3319   1.2      rin 	struct igc_adv_tx_context_desc *txdesc =
   3320   1.2      rin 	    (struct igc_adv_tx_context_desc *)&txr->tx_base[prod];
   3321   1.1      rin 
   3322   1.1      rin 	/* Now copy bits into descriptor */
   3323   1.2      rin 	igc_txdesc_sync(txr, prod,
   3324   1.2      rin 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
   3325   1.1      rin 	htolem32(&txdesc->vlan_macip_lens, vlan_macip_lens);
   3326   1.1      rin 	htolem32(&txdesc->type_tucmd_mlhl, type_tucmd_mlhl);
   3327   1.1      rin 	htolem32(&txdesc->seqnum_seed, 0);
   3328   1.1      rin 	htolem32(&txdesc->mss_l4len_idx, 0);
   3329   1.2      rin 	igc_txdesc_sync(txr, prod,
   3330   1.2      rin 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
   3331   1.2      rin 
   3332   1.2      rin 	return 1;
   3333   1.2      rin }
   3334   1.2      rin 
   3335   1.2      rin /*********************************************************************
   3336   1.2      rin  *
   3337   1.2      rin  *  Advanced Context Descriptor setup for TSO
   3338   1.2      rin  *
   3339   1.2      rin  *  XXX XXXRO
   3340   1.2      rin  *	Not working. Some packets are sent with correct csums, but
   3341   1.2      rin  *	others aren't. th->th_sum may be adjusted.
   3342   1.2      rin  *
   3343   1.2      rin  **********************************************************************/
   3344   1.2      rin 
   3345   1.2      rin static int
   3346   1.2      rin igc_tso_setup(struct tx_ring *txr, struct mbuf *mp, int prod,
   3347   1.2      rin     uint32_t *cmd_type_len, uint32_t *olinfo_status)
   3348   1.2      rin {
   3349   1.2      rin #if 1 /* notyet */
   3350   1.2      rin 	return 0;
   3351   1.2      rin #else
   3352   1.2      rin 	struct ether_vlan_header *evl;
   3353   1.2      rin 	struct ip *ip;
   3354   1.2      rin 	struct ip6_hdr *ip6;
   3355   1.2      rin 	struct tcphdr *th;
   3356   1.2      rin 	uint32_t type_tucmd_mlhl = 0;
   3357   1.2      rin 	uint32_t vlan_macip_lens = 0;
   3358   1.2      rin 	uint32_t mss_l4len_idx = 0;
   3359   1.2      rin 	uint32_t ehlen, iphlen, tcphlen, paylen;
   3360   1.2      rin 	uint16_t ehtype;
   3361   1.2      rin 
   3362   1.2      rin 	/*
   3363   1.2      rin 	 * In advanced descriptors the vlan tag must
   3364   1.2      rin 	 * be placed into the context descriptor. Hence
   3365   1.2      rin 	 * we need to make one even if not doing offloads.
   3366   1.2      rin 	 */
   3367   1.2      rin #if NVLAN > 0
   3368   1.2      rin 	if (vlan_has_tag(mp)) {
   3369   1.2      rin 		vlan_macip_lens |= (uint32_t)vlan_get_tag(mp)
   3370   1.2      rin 		    << IGC_ADVTXD_VLAN_SHIFT;
   3371   1.2      rin 	}
   3372   1.2      rin #endif
   3373   1.2      rin 
   3374   1.2      rin 	KASSERT(mp->m_len >= sizeof(struct ether_header));
   3375   1.2      rin 	evl = mtod(mp, struct ether_vlan_header *);
   3376   1.2      rin 	if (evl->evl_encap_proto == htons(ETHERTYPE_VLAN)) {
   3377   1.2      rin 		KASSERT(mp->m_len >= sizeof(struct ether_vlan_header));
   3378   1.2      rin 		ehlen = ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN;
   3379   1.2      rin 		ehtype = evl->evl_proto;
   3380   1.2      rin 	} else {
   3381   1.2      rin 		ehlen = ETHER_HDR_LEN;
   3382   1.2      rin 		ehtype = evl->evl_encap_proto;
   3383   1.2      rin 	}
   3384   1.2      rin 
   3385   1.2      rin 	vlan_macip_lens |= ehlen << IGC_ADVTXD_MACLEN_SHIFT;
   3386   1.2      rin 
   3387   1.2      rin 	switch (ntohs(ehtype)) {
   3388   1.2      rin 	case ETHERTYPE_IP:
   3389   1.2      rin 		iphlen = M_CSUM_DATA_IPv4_IPHL(mp->m_pkthdr.csum_data);
   3390   1.2      rin 		type_tucmd_mlhl |= IGC_ADVTXD_TUCMD_IPV4;
   3391   1.2      rin 		*olinfo_status |= IGC_TXD_POPTS_IXSM << 8;
   3392   1.2      rin 
   3393   1.2      rin 		KASSERT(mp->m_len >= ehlen + sizeof(*ip));
   3394   1.2      rin 		ip = (void *)(mtod(mp, char *) + ehlen);
   3395   1.2      rin 		ip->ip_len = 0;
   3396   1.2      rin 		KASSERT(iphlen == ip->ip_hl << 2);
   3397   1.2      rin 		KASSERT(ip->ip_sum == 0);
   3398   1.2      rin 		KASSERT(ip->ip_p == IPPROTO_TCP);
   3399   1.2      rin 
   3400   1.2      rin 		KASSERT(mp->m_len >= ehlen + iphlen + sizeof(*th));
   3401   1.2      rin 		th = (void *)((char *)ip + iphlen);
   3402   1.2      rin 		th->th_sum = in_cksum_phdr(ip->ip_src.s_addr, ip->ip_dst.s_addr,
   3403   1.2      rin 		    htons(IPPROTO_TCP));
   3404   1.2      rin 		break;
   3405   1.2      rin 	case ETHERTYPE_IPV6:
   3406   1.2      rin 		iphlen = M_CSUM_DATA_IPv6_IPHL(mp->m_pkthdr.csum_data);
   3407   1.2      rin 		type_tucmd_mlhl |= IGC_ADVTXD_TUCMD_IPV6;
   3408   1.2      rin 
   3409   1.2      rin 		KASSERT(mp->m_len >= ehlen + sizeof(*ip6));
   3410   1.2      rin 		ip6 = (void *)(mtod(mp, char *) + ehlen);
   3411   1.2      rin 		ip6->ip6_plen = 0;
   3412   1.2      rin 		KASSERT(iphlen == sizeof(struct ip6_hdr));
   3413   1.2      rin 		KASSERT(ip6->ip6_nxt == IPPROTO_TCP);
   3414   1.2      rin 
   3415   1.2      rin 		KASSERT(mp->m_len >= ehlen + iphlen + sizeof(*th));
   3416   1.2      rin 		th = (void *)((char *)ip6 + iphlen);
   3417   1.2      rin 		tcphlen = th->th_off << 2;
   3418   1.2      rin 		paylen = mp->m_pkthdr.len - ehlen - iphlen - tcphlen;
   3419   1.2      rin 		th->th_sum = in6_cksum_phdr(&ip6->ip6_src, &ip6->ip6_dst, 0,
   3420   1.2      rin 		    htonl(IPPROTO_TCP));
   3421   1.2      rin 		break;
   3422   1.2      rin 	default:
   3423   1.2      rin 		panic("%s", __func__);
   3424   1.2      rin 	}
   3425   1.2      rin 
   3426   1.2      rin 	tcphlen = th->th_off << 2;
   3427   1.2      rin 	paylen = mp->m_pkthdr.len - ehlen - iphlen - tcphlen;
   3428   1.2      rin 
   3429   1.2      rin 	vlan_macip_lens |= iphlen;
   3430   1.2      rin 
   3431   1.2      rin 	type_tucmd_mlhl |= IGC_ADVTXD_DCMD_DEXT | IGC_ADVTXD_DTYP_CTXT;
   3432   1.2      rin 	type_tucmd_mlhl |= IGC_ADVTXD_TUCMD_L4T_TCP;
   3433   1.2      rin 
   3434   1.2      rin 	mss_l4len_idx |= mp->m_pkthdr.segsz << IGC_ADVTXD_MSS_SHIFT;
   3435   1.2      rin 	mss_l4len_idx |= tcphlen << IGC_ADVTXD_L4LEN_SHIFT;
   3436   1.2      rin 
   3437   1.2      rin 	/* Now ready a context descriptor */
   3438   1.2      rin 	struct igc_adv_tx_context_desc *txdesc =
   3439   1.2      rin 	    (struct igc_adv_tx_context_desc *)&txr->tx_base[prod];
   3440   1.2      rin 
   3441   1.2      rin 	/* Now copy bits into descriptor */
   3442   1.2      rin 	igc_txdesc_sync(txr, prod,
   3443   1.2      rin 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
   3444   1.2      rin 	htolem32(&txdesc->vlan_macip_lens, vlan_macip_lens);
   3445   1.2      rin 	htolem32(&txdesc->type_tucmd_mlhl, type_tucmd_mlhl);
   3446   1.2      rin 	htolem32(&txdesc->seqnum_seed, 0);
   3447   1.2      rin 	htolem32(&txdesc->mss_l4len_idx, mss_l4len_idx);
   3448   1.2      rin 	igc_txdesc_sync(txr, prod,
   3449   1.2      rin 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
   3450   1.2      rin 
   3451   1.2      rin 	*cmd_type_len |= IGC_ADVTXD_DCMD_TSE;
   3452   1.2      rin 	*olinfo_status |= IGC_TXD_POPTS_TXSM << 8;
   3453   1.2      rin 	*olinfo_status |= paylen << IGC_ADVTXD_PAYLEN_SHIFT;
   3454   1.1      rin 
   3455   1.1      rin 	return 1;
   3456   1.2      rin #endif /* notyet */
   3457   1.1      rin }
   3458   1.1      rin 
   3459   1.1      rin /*********************************************************************
   3460   1.1      rin  *
   3461   1.1      rin  *  Allocate memory for rx_buffer structures. Since we use one
   3462   1.1      rin  *  rx_buffer per received packet, the maximum number of rx_buffer's
   3463   1.1      rin  *  that we'll need is equal to the number of receive descriptors
   3464   1.1      rin  *  that we've allocated.
   3465   1.1      rin  *
   3466   1.1      rin  **********************************************************************/
   3467   1.2      rin static int
   3468   1.1      rin igc_allocate_receive_buffers(struct rx_ring *rxr)
   3469   1.1      rin {
   3470   1.1      rin 	struct igc_softc *sc = rxr->sc;
   3471   1.2      rin 	int error;
   3472   1.1      rin 
   3473   1.2      rin 	rxr->rx_buffers =
   3474   1.2      rin 	    kmem_zalloc(sc->num_rx_desc * sizeof(struct igc_rx_buf), KM_SLEEP);
   3475   1.2      rin 
   3476   1.2      rin 	for (int id = 0; id < sc->num_rx_desc; id++) {
   3477   1.2      rin 		struct igc_rx_buf *rxbuf = &rxr->rx_buffers[id];
   3478   1.1      rin 
   3479   1.2      rin 		error = bus_dmamap_create(rxr->rxdma.dma_tag, MCLBYTES, 1,
   3480   1.2      rin 		    MCLBYTES, 0, BUS_DMA_WAITOK, &rxbuf->map);
   3481   1.1      rin 		if (error) {
   3482   1.2      rin 			aprint_error_dev(sc->sc_dev,
   3483   1.2      rin 			    "unable to create RX DMA map\n");
   3484   1.1      rin 			goto fail;
   3485   1.1      rin 		}
   3486   1.1      rin 	}
   3487   1.1      rin 	bus_dmamap_sync(rxr->rxdma.dma_tag, rxr->rxdma.dma_map, 0,
   3488   1.1      rin 	    rxr->rxdma.dma_map->dm_mapsize,
   3489   1.1      rin 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
   3490   1.1      rin 
   3491   1.1      rin 	return 0;
   3492   1.2      rin  fail:
   3493   1.1      rin 	return error;
   3494   1.1      rin }
   3495   1.1      rin 
   3496   1.1      rin /*********************************************************************
   3497   1.1      rin  *
   3498   1.1      rin  *  Allocate and initialize receive structures.
   3499   1.1      rin  *
   3500   1.1      rin  **********************************************************************/
   3501   1.2      rin static int
   3502   1.1      rin igc_setup_receive_structures(struct igc_softc *sc)
   3503   1.1      rin {
   3504   1.1      rin 
   3505   1.2      rin 	for (int iq = 0; iq < sc->sc_nqueues; iq++) {
   3506   1.2      rin 		struct rx_ring *rxr = &sc->rx_rings[iq];
   3507   1.2      rin 
   3508   1.1      rin 		if (igc_setup_receive_ring(rxr))
   3509   1.1      rin 			goto fail;
   3510   1.1      rin 	}
   3511   1.1      rin 
   3512   1.1      rin 	return 0;
   3513   1.2      rin  fail:
   3514   1.1      rin 	igc_free_receive_structures(sc);
   3515   1.1      rin 	return ENOBUFS;
   3516   1.1      rin }
   3517   1.1      rin 
   3518   1.1      rin /*********************************************************************
   3519   1.1      rin  *
   3520   1.1      rin  *  Initialize a receive ring and its buffers.
   3521   1.1      rin  *
   3522   1.1      rin  **********************************************************************/
   3523   1.2      rin static int
   3524   1.1      rin igc_setup_receive_ring(struct rx_ring *rxr)
   3525   1.1      rin {
   3526   1.1      rin 	struct igc_softc *sc = rxr->sc;
   3527   1.2      rin 	const int rsize = roundup2(
   3528   1.2      rin 	    sc->num_rx_desc * sizeof(union igc_adv_rx_desc), IGC_DBA_ALIGN);
   3529   1.1      rin 
   3530   1.1      rin 	/* Clear the ring contents. */
   3531   1.2      rin 	memset(rxr->rx_base, 0, rsize);
   3532   1.1      rin 
   3533   1.1      rin 	if (igc_allocate_receive_buffers(rxr))
   3534   1.1      rin 		return ENOMEM;
   3535   1.1      rin 
   3536   1.1      rin 	/* Setup our descriptor indices. */
   3537   1.1      rin 	rxr->next_to_check = 0;
   3538   1.2      rin 	rxr->last_desc_filled = 0;
   3539   1.1      rin 
   3540   1.2      rin 	mutex_init(&rxr->rxr_lock, MUTEX_DEFAULT, IPL_NET);
   3541   1.1      rin 
   3542   1.1      rin 	return 0;
   3543   1.1      rin }
   3544   1.1      rin 
   3545   1.1      rin /*********************************************************************
   3546   1.1      rin  *
   3547   1.1      rin  *  Enable receive unit.
   3548   1.1      rin  *
   3549   1.1      rin  **********************************************************************/
   3550   1.2      rin static void
   3551   1.1      rin igc_initialize_receive_unit(struct igc_softc *sc)
   3552   1.1      rin {
   3553   1.2      rin 	struct ifnet *ifp = &sc->sc_ec.ec_if;
   3554   1.2      rin 	struct igc_hw *hw = &sc->hw;
   3555   1.2      rin 	uint32_t rctl, rxcsum, srrctl;
   3556   1.2      rin 
   3557   1.2      rin 	DPRINTF(RX, "called\n");
   3558   1.1      rin 
   3559   1.1      rin 	/*
   3560   1.1      rin 	 * Make sure receives are disabled while setting
   3561   1.1      rin 	 * up the descriptor ring.
   3562   1.1      rin 	 */
   3563   1.1      rin 	rctl = IGC_READ_REG(hw, IGC_RCTL);
   3564   1.1      rin 	IGC_WRITE_REG(hw, IGC_RCTL, rctl & ~IGC_RCTL_EN);
   3565   1.1      rin 
   3566   1.1      rin 	/* Setup the Receive Control Register */
   3567   1.1      rin 	rctl &= ~(3 << IGC_RCTL_MO_SHIFT);
   3568   1.1      rin 	rctl |= IGC_RCTL_EN | IGC_RCTL_BAM | IGC_RCTL_LBM_NO |
   3569   1.1      rin 	    IGC_RCTL_RDMTS_HALF | (hw->mac.mc_filter_type << IGC_RCTL_MO_SHIFT);
   3570   1.1      rin 
   3571   1.2      rin #if 1
   3572   1.1      rin 	/* Do not store bad packets */
   3573   1.1      rin 	rctl &= ~IGC_RCTL_SBP;
   3574   1.2      rin #else
   3575   1.2      rin 	/* for debug */
   3576   1.2      rin 	rctl |= IGC_RCTL_SBP;
   3577   1.2      rin #endif
   3578   1.1      rin 
   3579   1.1      rin 	/* Enable Long Packet receive */
   3580   1.2      rin 	if (sc->hw.mac.max_frame_size > ETHER_MAX_LEN)
   3581   1.1      rin 		rctl |= IGC_RCTL_LPE;
   3582   1.2      rin 	else
   3583   1.2      rin 		rctl &= ~IGC_RCTL_LPE;
   3584   1.1      rin 
   3585   1.1      rin 	/* Strip the CRC */
   3586   1.1      rin 	rctl |= IGC_RCTL_SECRC;
   3587   1.1      rin 
   3588   1.1      rin 	/*
   3589   1.1      rin 	 * Set the interrupt throttling rate. Value is calculated
   3590   1.1      rin 	 * as DEFAULT_ITR = 1/(MAX_INTS_PER_SEC * 256ns)
   3591   1.2      rin 	 *
   3592   1.2      rin 	 * XXX Sync with Linux, especially for jumbo MTU or TSO.
   3593   1.2      rin 	 * XXX Shouldn't be here?
   3594   1.1      rin 	 */
   3595   1.1      rin 	IGC_WRITE_REG(hw, IGC_ITR, DEFAULT_ITR);
   3596   1.1      rin 
   3597   1.1      rin 	rxcsum = IGC_READ_REG(hw, IGC_RXCSUM);
   3598   1.2      rin 	rxcsum &= ~(IGC_RXCSUM_IPOFL | IGC_RXCSUM_TUOFL | IGC_RXCSUM_PCSD);
   3599   1.2      rin 	if (ifp->if_capenable & IFCAP_CSUM_IPv4_Rx)
   3600   1.2      rin 		rxcsum |= IGC_RXCSUM_IPOFL;
   3601   1.2      rin 	if (ifp->if_capenable & (IFCAP_CSUM_TCPv4_Rx | IFCAP_CSUM_UDPv4_Rx |
   3602   1.2      rin 				 IFCAP_CSUM_TCPv6_Rx | IFCAP_CSUM_UDPv6_Rx))
   3603   1.2      rin 		rxcsum |= IGC_RXCSUM_TUOFL;
   3604   1.1      rin 	if (sc->sc_nqueues > 1)
   3605   1.1      rin 		rxcsum |= IGC_RXCSUM_PCSD;
   3606   1.1      rin 	IGC_WRITE_REG(hw, IGC_RXCSUM, rxcsum);
   3607   1.1      rin 
   3608   1.1      rin 	if (sc->sc_nqueues > 1)
   3609   1.1      rin 		igc_initialize_rss_mapping(sc);
   3610   1.1      rin 
   3611   1.2      rin 	srrctl = 0;
   3612   1.1      rin #if 0
   3613   1.1      rin 	srrctl |= 4096 >> IGC_SRRCTL_BSIZEPKT_SHIFT;
   3614   1.1      rin 	rctl |= IGC_RCTL_SZ_4096 | IGC_RCTL_BSEX;
   3615   1.2      rin #else
   3616   1.1      rin 	srrctl |= 2048 >> IGC_SRRCTL_BSIZEPKT_SHIFT;
   3617   1.1      rin 	rctl |= IGC_RCTL_SZ_2048;
   3618   1.2      rin #endif
   3619   1.1      rin 
   3620   1.1      rin 	/*
   3621   1.1      rin 	 * If TX flow control is disabled and there's > 1 queue defined,
   3622   1.1      rin 	 * enable DROP.
   3623   1.1      rin 	 *
   3624   1.1      rin 	 * This drops frames rather than hanging the RX MAC for all queues.
   3625   1.1      rin 	 */
   3626   1.2      rin 	if (sc->sc_nqueues > 1 &&
   3627   1.2      rin 	    (sc->fc == igc_fc_none || sc->fc == igc_fc_rx_pause))
   3628   1.1      rin 		srrctl |= IGC_SRRCTL_DROP_EN;
   3629   1.1      rin 
   3630   1.1      rin 	/* Setup the Base and Length of the RX descriptor rings. */
   3631   1.2      rin 	for (int iq = 0; iq < sc->sc_nqueues; iq++) {
   3632   1.2      rin 		struct rx_ring *rxr = &sc->rx_rings[iq];
   3633   1.2      rin 		const uint64_t bus_addr =
   3634   1.2      rin 		    rxr->rxdma.dma_map->dm_segs[0].ds_addr;
   3635   1.2      rin 
   3636   1.2      rin 		IGC_WRITE_REG(hw, IGC_RXDCTL(iq), 0);
   3637   1.1      rin 
   3638   1.1      rin 		srrctl |= IGC_SRRCTL_DESCTYPE_ADV_ONEBUF;
   3639   1.1      rin 
   3640   1.2      rin 		IGC_WRITE_REG(hw, IGC_RDLEN(iq),
   3641   1.1      rin 		    sc->num_rx_desc * sizeof(union igc_adv_rx_desc));
   3642   1.2      rin 		IGC_WRITE_REG(hw, IGC_RDBAH(iq), (uint32_t)(bus_addr >> 32));
   3643   1.2      rin 		IGC_WRITE_REG(hw, IGC_RDBAL(iq), (uint32_t)bus_addr);
   3644   1.2      rin 		IGC_WRITE_REG(hw, IGC_SRRCTL(iq), srrctl);
   3645   1.1      rin 
   3646   1.1      rin 		/* Setup the Head and Tail Descriptor Pointers */
   3647   1.2      rin 		IGC_WRITE_REG(hw, IGC_RDH(iq), 0);
   3648   1.2      rin 		IGC_WRITE_REG(hw, IGC_RDT(iq), 0 /* XXX rxr->last_desc_filled */);
   3649   1.1      rin 
   3650   1.1      rin 		/* Enable this Queue */
   3651   1.2      rin 		uint32_t rxdctl = IGC_READ_REG(hw, IGC_RXDCTL(iq));
   3652   1.1      rin 		rxdctl |= IGC_RXDCTL_QUEUE_ENABLE;
   3653   1.1      rin 		rxdctl &= 0xFFF00000;
   3654   1.1      rin 		rxdctl |= IGC_RX_PTHRESH;
   3655   1.1      rin 		rxdctl |= IGC_RX_HTHRESH << 8;
   3656   1.1      rin 		rxdctl |= IGC_RX_WTHRESH << 16;
   3657   1.2      rin 		IGC_WRITE_REG(hw, IGC_RXDCTL(iq), rxdctl);
   3658   1.1      rin 	}
   3659   1.1      rin 
   3660   1.1      rin 	/* Make sure VLAN Filters are off */
   3661   1.1      rin 	rctl &= ~IGC_RCTL_VFE;
   3662   1.1      rin 
   3663   1.1      rin 	/* Write out the settings */
   3664   1.1      rin 	IGC_WRITE_REG(hw, IGC_RCTL, rctl);
   3665   1.1      rin }
   3666   1.1      rin 
   3667   1.1      rin /*********************************************************************
   3668   1.1      rin  *
   3669   1.1      rin  *  Free all receive rings.
   3670   1.1      rin  *
   3671   1.1      rin  **********************************************************************/
   3672   1.2      rin static void
   3673   1.1      rin igc_free_receive_structures(struct igc_softc *sc)
   3674   1.1      rin {
   3675   1.1      rin 
   3676   1.2      rin 	for (int iq = 0; iq < sc->sc_nqueues; iq++) {
   3677   1.2      rin 		struct rx_ring *rxr = &sc->rx_rings[iq];
   3678   1.1      rin 
   3679   1.1      rin 		igc_free_receive_buffers(rxr);
   3680   1.2      rin 	}
   3681   1.1      rin }
   3682   1.1      rin 
   3683   1.1      rin /*********************************************************************
   3684   1.1      rin  *
   3685   1.1      rin  *  Free receive ring data structures
   3686   1.1      rin  *
   3687   1.1      rin  **********************************************************************/
   3688   1.2      rin static void
   3689   1.1      rin igc_free_receive_buffers(struct rx_ring *rxr)
   3690   1.1      rin {
   3691   1.1      rin 	struct igc_softc *sc = rxr->sc;
   3692   1.1      rin 
   3693   1.1      rin 	if (rxr->rx_buffers != NULL) {
   3694   1.2      rin 		for (int id = 0; id < sc->num_rx_desc; id++) {
   3695   1.2      rin 			struct igc_rx_buf *rxbuf = &rxr->rx_buffers[id];
   3696   1.2      rin 			bus_dmamap_t map = rxbuf->map;
   3697   1.2      rin 
   3698   1.1      rin 			if (rxbuf->buf != NULL) {
   3699   1.2      rin 				bus_dmamap_sync(rxr->rxdma.dma_tag, map,
   3700   1.2      rin 				    0, map->dm_mapsize, BUS_DMASYNC_POSTREAD);
   3701   1.2      rin 				bus_dmamap_unload(rxr->rxdma.dma_tag, map);
   3702   1.1      rin 				m_freem(rxbuf->buf);
   3703   1.1      rin 				rxbuf->buf = NULL;
   3704   1.1      rin 			}
   3705   1.2      rin 			bus_dmamap_destroy(rxr->rxdma.dma_tag, map);
   3706   1.1      rin 			rxbuf->map = NULL;
   3707   1.1      rin 		}
   3708   1.2      rin 		kmem_free(rxr->rx_buffers,
   3709   1.1      rin 		    sc->num_rx_desc * sizeof(struct igc_rx_buf));
   3710   1.1      rin 		rxr->rx_buffers = NULL;
   3711   1.1      rin 	}
   3712   1.2      rin 
   3713   1.2      rin 	mutex_destroy(&rxr->rxr_lock);
   3714   1.2      rin }
   3715   1.2      rin 
   3716   1.2      rin /*********************************************************************
   3717   1.2      rin  *
   3718   1.2      rin  * Clear status registers in all RX descriptors.
   3719   1.2      rin  *
   3720   1.2      rin  **********************************************************************/
   3721   1.2      rin static void
   3722   1.2      rin igc_clear_receive_status(struct rx_ring *rxr)
   3723   1.2      rin {
   3724   1.2      rin 	struct igc_softc *sc = rxr->sc;
   3725   1.2      rin 
   3726   1.2      rin 	mutex_enter(&rxr->rxr_lock);
   3727   1.2      rin 
   3728   1.2      rin 	for (int id = 0; id < sc->num_rx_desc; id++) {
   3729   1.2      rin 		union igc_adv_rx_desc *rxdesc = &rxr->rx_base[id];
   3730   1.2      rin 
   3731   1.2      rin 		igc_rxdesc_sync(rxr, id,
   3732   1.2      rin 		    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
   3733   1.2      rin 		rxdesc->wb.upper.status_error = 0;
   3734   1.2      rin 		igc_rxdesc_sync(rxr, id,
   3735   1.2      rin 		    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
   3736   1.2      rin 	}
   3737   1.2      rin 
   3738   1.2      rin 	mutex_exit(&rxr->rxr_lock);
   3739   1.1      rin }
   3740   1.1      rin 
   3741   1.1      rin /*
   3742   1.1      rin  * Initialise the RSS mapping for NICs that support multiple transmit/
   3743   1.1      rin  * receive rings.
   3744   1.1      rin  */
   3745   1.2      rin static void
   3746   1.1      rin igc_initialize_rss_mapping(struct igc_softc *sc)
   3747   1.1      rin {
   3748   1.1      rin 	struct igc_hw *hw = &sc->hw;
   3749   1.1      rin 
   3750   1.1      rin 	/*
   3751   1.1      rin 	 * The redirection table controls which destination
   3752   1.1      rin 	 * queue each bucket redirects traffic to.
   3753   1.1      rin 	 * Each DWORD represents four queues, with the LSB
   3754   1.1      rin 	 * being the first queue in the DWORD.
   3755   1.1      rin 	 *
   3756   1.1      rin 	 * This just allocates buckets to queues using round-robin
   3757   1.1      rin 	 * allocation.
   3758   1.1      rin 	 *
   3759   1.1      rin 	 * NOTE: It Just Happens to line up with the default
   3760   1.1      rin 	 * RSS allocation method.
   3761   1.1      rin 	 */
   3762   1.1      rin 
   3763   1.1      rin 	/* Warning FM follows */
   3764   1.2      rin 	uint32_t reta = 0;
   3765   1.2      rin 	for (int i = 0; i < 128; i++) {
   3766   1.2      rin 		const int shift = 0; /* XXXRO */
   3767   1.2      rin 		int queue_id = i % sc->sc_nqueues;
   3768   1.1      rin 		/* Adjust if required */
   3769   1.2      rin 		queue_id <<= shift;
   3770   1.1      rin 
   3771   1.1      rin 		/*
   3772   1.1      rin 		 * The low 8 bits are for hash value (n+0);
   3773   1.1      rin 		 * The next 8 bits are for hash value (n+1), etc.
   3774   1.1      rin 		 */
   3775   1.2      rin 		reta >>= 8;
   3776   1.2      rin 		reta |= ((uint32_t)queue_id) << 24;
   3777   1.1      rin 		if ((i & 3) == 3) {
   3778   1.1      rin 			IGC_WRITE_REG(hw, IGC_RETA(i >> 2), reta);
   3779   1.1      rin 			reta = 0;
   3780   1.1      rin 		}
   3781   1.1      rin 	}
   3782   1.1      rin 
   3783   1.1      rin 	/*
   3784   1.1      rin 	 * MRQC: Multiple Receive Queues Command
   3785   1.1      rin 	 * Set queuing to RSS control, number depends on the device.
   3786   1.1      rin 	 */
   3787   1.1      rin 
   3788   1.1      rin 	/* Set up random bits */
   3789   1.2      rin 	uint32_t rss_key[RSS_KEYSIZE / sizeof(uint32_t)];
   3790   1.2      rin 	rss_getkey((uint8_t *)rss_key);
   3791   1.1      rin 
   3792   1.1      rin 	/* Now fill our hash function seeds */
   3793   1.2      rin 	for (int i = 0; i < __arraycount(rss_key); i++)
   3794   1.1      rin 		IGC_WRITE_REG_ARRAY(hw, IGC_RSSRK(0), i, rss_key[i]);
   3795   1.1      rin 
   3796   1.1      rin 	/*
   3797   1.1      rin 	 * Configure the RSS fields to hash upon.
   3798   1.1      rin 	 */
   3799   1.2      rin 	uint32_t mrqc = IGC_MRQC_ENABLE_RSS_4Q;
   3800   1.2      rin 	mrqc |= IGC_MRQC_RSS_FIELD_IPV4 | IGC_MRQC_RSS_FIELD_IPV4_TCP;
   3801   1.2      rin 	mrqc |= IGC_MRQC_RSS_FIELD_IPV6 | IGC_MRQC_RSS_FIELD_IPV6_TCP;
   3802   1.1      rin 	mrqc |= IGC_MRQC_RSS_FIELD_IPV6_TCP_EX;
   3803   1.1      rin 
   3804   1.1      rin 	IGC_WRITE_REG(hw, IGC_MRQC, mrqc);
   3805   1.1      rin }
   3806   1.1      rin 
   3807   1.1      rin /*
   3808   1.1      rin  * igc_get_hw_control sets the {CTRL_EXT|FWSM}:DRV_LOAD bit.
   3809   1.1      rin  * For ASF and Pass Through versions of f/w this means
   3810   1.1      rin  * that the driver is loaded. For AMT version type f/w
   3811   1.1      rin  * this means that the network i/f is open.
   3812   1.1      rin  */
   3813   1.2      rin static void
   3814   1.1      rin igc_get_hw_control(struct igc_softc *sc)
   3815   1.1      rin {
   3816   1.2      rin 	const uint32_t ctrl_ext = IGC_READ_REG(&sc->hw, IGC_CTRL_EXT);
   3817   1.1      rin 
   3818   1.1      rin 	IGC_WRITE_REG(&sc->hw, IGC_CTRL_EXT, ctrl_ext | IGC_CTRL_EXT_DRV_LOAD);
   3819   1.1      rin }
   3820   1.1      rin 
   3821   1.1      rin /*
   3822   1.1      rin  * igc_release_hw_control resets {CTRL_EXT|FWSM}:DRV_LOAD bit.
   3823   1.1      rin  * For ASF and Pass Through versions of f/w this means that
   3824   1.1      rin  * the driver is no longer loaded. For AMT versions of the
   3825   1.1      rin  * f/w this means that the network i/f is closed.
   3826   1.1      rin  */
   3827   1.2      rin static void
   3828   1.1      rin igc_release_hw_control(struct igc_softc *sc)
   3829   1.1      rin {
   3830   1.2      rin 	const uint32_t ctrl_ext = IGC_READ_REG(&sc->hw, IGC_CTRL_EXT);
   3831   1.1      rin 
   3832   1.1      rin 	IGC_WRITE_REG(&sc->hw, IGC_CTRL_EXT, ctrl_ext & ~IGC_CTRL_EXT_DRV_LOAD);
   3833   1.1      rin }
   3834   1.1      rin 
   3835   1.2      rin static int
   3836   1.1      rin igc_is_valid_ether_addr(uint8_t *addr)
   3837   1.1      rin {
   3838   1.2      rin 	const char zero_addr[6] = { 0, 0, 0, 0, 0, 0 };
   3839   1.1      rin 
   3840   1.2      rin 	if ((addr[0] & 1) || !bcmp(addr, zero_addr, ETHER_ADDR_LEN))
   3841   1.1      rin 		return 0;
   3842   1.1      rin 
   3843   1.1      rin 	return 1;
   3844   1.1      rin }
   3845   1.2      rin 
   3846   1.2      rin static void
   3847   1.2      rin igc_print_devinfo(struct igc_softc *sc)
   3848   1.2      rin {
   3849   1.2      rin 	device_t dev = sc->sc_dev;
   3850   1.2      rin 	struct igc_hw *hw = &sc->hw;
   3851   1.2      rin 	struct igc_phy_info *phy = &hw->phy;
   3852   1.2      rin 	u_int oui, model, rev;
   3853  1.13  msaitoh 	uint16_t id1, id2, nvm_ver, phy_ver, etk_lo, etk_hi;
   3854   1.2      rin 	char descr[MII_MAX_DESCR_LEN];
   3855   1.2      rin 
   3856   1.2      rin 	/* Print PHY Info */
   3857   1.2      rin 	id1 = phy->id >> 16;
   3858   1.2      rin 	/* The revision field in phy->id is cleard and it's in phy->revision */
   3859   1.2      rin 	id2 = (phy->id & 0xfff0) | phy->revision;
   3860   1.2      rin 	oui = MII_OUI(id1, id2);
   3861   1.2      rin 	model = MII_MODEL(id2);
   3862   1.2      rin 	rev = MII_REV(id2);
   3863   1.2      rin 	mii_get_descr(descr, sizeof(descr), oui, model);
   3864   1.2      rin 	if (descr[0])
   3865  1.12  msaitoh 		aprint_normal_dev(dev, "PHY: %s, rev. %d",
   3866   1.2      rin 		    descr, rev);
   3867   1.2      rin 	else
   3868   1.2      rin 		aprint_normal_dev(dev,
   3869  1.12  msaitoh 		    "PHY OUI 0x%06x, model 0x%04x, rev. %d",
   3870   1.2      rin 		    oui, model, rev);
   3871   1.2      rin 
   3872  1.12  msaitoh 	/* PHY FW version */
   3873  1.12  msaitoh 	phy->ops.read_reg(hw, 0x1e, &phy_ver);
   3874  1.12  msaitoh 	aprint_normal(", PHY FW version 0x%04hx\n", phy_ver);
   3875  1.12  msaitoh 
   3876  1.13  msaitoh 	/* NVM version */
   3877   1.2      rin 	hw->nvm.ops.read(hw, NVM_VERSION, 1, &nvm_ver);
   3878   1.2      rin 
   3879  1.13  msaitoh 	/* EtrackID */
   3880  1.13  msaitoh 	hw->nvm.ops.read(hw, NVM_ETKID_LO, 1, &etk_lo);
   3881  1.13  msaitoh 	hw->nvm.ops.read(hw, NVM_ETKID_HI, 1, &etk_hi);
   3882  1.13  msaitoh 
   3883  1.13  msaitoh 	aprint_normal_dev(dev,
   3884  1.13  msaitoh 	    "NVM image version %x.%02x, EtrackID %04hx%04hx\n",
   3885   1.2      rin 	    (nvm_ver & NVM_VERSION_MAJOR) >> NVM_VERSION_MAJOR_SHIFT,
   3886  1.13  msaitoh 	    nvm_ver & NVM_VERSION_MINOR, etk_hi, etk_lo);
   3887   1.2      rin }
   3888