Home | History | Annotate | Line # | Download | only in pci
if_wm.c revision 1.752
      1 /*	$NetBSD: if_wm.c,v 1.752 2022/08/05 05:50:54 skrll Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2001, 2002, 2003, 2004 Wasabi Systems, Inc.
      5  * All rights reserved.
      6  *
      7  * Written by Jason R. Thorpe for Wasabi Systems, Inc.
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  * 3. All advertising materials mentioning features or use of this software
     18  *    must display the following acknowledgement:
     19  *	This product includes software developed for the NetBSD Project by
     20  *	Wasabi Systems, Inc.
     21  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
     22  *    or promote products derived from this software without specific prior
     23  *    written permission.
     24  *
     25  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
     26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     27  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     28  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
     29  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     30  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     31  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     32  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     33  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     34  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     35  * POSSIBILITY OF SUCH DAMAGE.
     36  */
     37 
     38 /*******************************************************************************
     39 
     40   Copyright (c) 2001-2005, Intel Corporation
     41   All rights reserved.
     42 
     43   Redistribution and use in source and binary forms, with or without
     44   modification, are permitted provided that the following conditions are met:
     45 
     46    1. Redistributions of source code must retain the above copyright notice,
     47       this list of conditions and the following disclaimer.
     48 
     49    2. Redistributions in binary form must reproduce the above copyright
     50       notice, this list of conditions and the following disclaimer in the
     51       documentation and/or other materials provided with the distribution.
     52 
     53    3. Neither the name of the Intel Corporation nor the names of its
     54       contributors may be used to endorse or promote products derived from
     55       this software without specific prior written permission.
     56 
     57   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
     58   AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     59   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     60   ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
     61   LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     62   CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     63   SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     64   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     65   CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     66   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     67   POSSIBILITY OF SUCH DAMAGE.
     68 
     69 *******************************************************************************/
     70 /*
     71  * Device driver for the Intel i8254x family of Gigabit Ethernet chips.
     72  *
     73  * TODO (in order of importance):
     74  *
     75  *	- Check XXX'ed comments
     76  *	- TX Multi queue improvement (refine queue selection logic)
     77  *	- Split header buffer for newer descriptors
     78  *	- EEE (Energy Efficiency Ethernet) for I354
     79  *	- Virtual Function
     80  *	- Set LED correctly (based on contents in EEPROM)
     81  *	- Rework how parameters are loaded from the EEPROM.
     82  */
     83 
     84 #include <sys/cdefs.h>
     85 __KERNEL_RCSID(0, "$NetBSD: if_wm.c,v 1.752 2022/08/05 05:50:54 skrll Exp $");
     86 
     87 #ifdef _KERNEL_OPT
     88 #include "opt_net_mpsafe.h"
     89 #include "opt_if_wm.h"
     90 #endif
     91 
     92 #include <sys/param.h>
     93 
     94 #include <sys/atomic.h>
     95 #include <sys/callout.h>
     96 #include <sys/cpu.h>
     97 #include <sys/device.h>
     98 #include <sys/errno.h>
     99 #include <sys/interrupt.h>
    100 #include <sys/ioctl.h>
    101 #include <sys/kernel.h>
    102 #include <sys/kmem.h>
    103 #include <sys/mbuf.h>
    104 #include <sys/pcq.h>
    105 #include <sys/queue.h>
    106 #include <sys/rndsource.h>
    107 #include <sys/socket.h>
    108 #include <sys/sysctl.h>
    109 #include <sys/syslog.h>
    110 #include <sys/systm.h>
    111 #include <sys/workqueue.h>
    112 
    113 #include <net/if.h>
    114 #include <net/if_dl.h>
    115 #include <net/if_media.h>
    116 #include <net/if_ether.h>
    117 
    118 #include <net/bpf.h>
    119 
    120 #include <net/rss_config.h>
    121 
    122 #include <netinet/in.h>			/* XXX for struct ip */
    123 #include <netinet/in_systm.h>		/* XXX for struct ip */
    124 #include <netinet/ip.h>			/* XXX for struct ip */
    125 #include <netinet/ip6.h>		/* XXX for struct ip6_hdr */
    126 #include <netinet/tcp.h>		/* XXX for struct tcphdr */
    127 
    128 #include <sys/bus.h>
    129 #include <sys/intr.h>
    130 #include <machine/endian.h>
    131 
    132 #include <dev/mii/mii.h>
    133 #include <dev/mii/mdio.h>
    134 #include <dev/mii/miivar.h>
    135 #include <dev/mii/miidevs.h>
    136 #include <dev/mii/mii_bitbang.h>
    137 #include <dev/mii/ikphyreg.h>
    138 #include <dev/mii/igphyreg.h>
    139 #include <dev/mii/igphyvar.h>
    140 #include <dev/mii/inbmphyreg.h>
    141 #include <dev/mii/ihphyreg.h>
    142 #include <dev/mii/makphyreg.h>
    143 
    144 #include <dev/pci/pcireg.h>
    145 #include <dev/pci/pcivar.h>
    146 #include <dev/pci/pcidevs.h>
    147 
    148 #include <dev/pci/if_wmreg.h>
    149 #include <dev/pci/if_wmvar.h>
    150 
    151 #ifdef WM_DEBUG
    152 #define	WM_DEBUG_LINK		__BIT(0)
    153 #define	WM_DEBUG_TX		__BIT(1)
    154 #define	WM_DEBUG_RX		__BIT(2)
    155 #define	WM_DEBUG_GMII		__BIT(3)
    156 #define	WM_DEBUG_MANAGE		__BIT(4)
    157 #define	WM_DEBUG_NVM		__BIT(5)
    158 #define	WM_DEBUG_INIT		__BIT(6)
    159 #define	WM_DEBUG_LOCK		__BIT(7)
    160 
    161 #if 0
    162 #define WM_DEBUG_DEFAULT	WM_DEBUG_TX | WM_DEBUG_RX | WM_DEBUG_LINK | \
    163 	WM_DEBUG_GMII | WM_DEBUG_MANAGE | WM_DEBUG_NVM | WM_DEBUG_INIT |    \
    164 	WM_DEBUG_LOCK
    165 #endif
    166 
    167 #define	DPRINTF(sc, x, y)			  \
    168 	do {					  \
    169 		if ((sc)->sc_debug & (x))	  \
    170 			printf y;		  \
    171 	} while (0)
    172 #else
    173 #define	DPRINTF(sc, x, y)	__nothing
    174 #endif /* WM_DEBUG */
    175 
    176 #ifdef NET_MPSAFE
    177 #define WM_MPSAFE	1
    178 #define WM_CALLOUT_FLAGS	CALLOUT_MPSAFE
    179 #define WM_SOFTINT_FLAGS	SOFTINT_MPSAFE
    180 #define WM_WORKQUEUE_FLAGS	WQ_PERCPU | WQ_MPSAFE
    181 #else
    182 #define WM_CALLOUT_FLAGS	0
    183 #define WM_SOFTINT_FLAGS	0
    184 #define WM_WORKQUEUE_FLAGS	WQ_PERCPU
    185 #endif
    186 
    187 #define WM_WORKQUEUE_PRI PRI_SOFTNET
    188 
    189 /*
    190  * This device driver's max interrupt numbers.
    191  */
    192 #define WM_MAX_NQUEUEINTR	16
    193 #define WM_MAX_NINTR		(WM_MAX_NQUEUEINTR + 1)
    194 
    195 #ifndef WM_DISABLE_MSI
    196 #define	WM_DISABLE_MSI 0
    197 #endif
    198 #ifndef WM_DISABLE_MSIX
    199 #define	WM_DISABLE_MSIX 0
    200 #endif
    201 
    202 int wm_disable_msi = WM_DISABLE_MSI;
    203 int wm_disable_msix = WM_DISABLE_MSIX;
    204 
    205 #ifndef WM_WATCHDOG_TIMEOUT
    206 #define WM_WATCHDOG_TIMEOUT 5
    207 #endif
    208 static int wm_watchdog_timeout = WM_WATCHDOG_TIMEOUT;
    209 
    210 /*
    211  * Transmit descriptor list size.  Due to errata, we can only have
    212  * 256 hardware descriptors in the ring on < 82544, but we use 4096
    213  * on >= 82544. We tell the upper layers that they can queue a lot
    214  * of packets, and we go ahead and manage up to 64 (16 for the i82547)
    215  * of them at a time.
    216  *
    217  * We allow up to 64 DMA segments per packet.  Pathological packet
    218  * chains containing many small mbufs have been observed in zero-copy
    219  * situations with jumbo frames. If a mbuf chain has more than 64 DMA segments,
    220  * m_defrag() is called to reduce it.
    221  */
    222 #define	WM_NTXSEGS		64
    223 #define	WM_IFQUEUELEN		256
    224 #define	WM_TXQUEUELEN_MAX	64
    225 #define	WM_TXQUEUELEN_MAX_82547	16
    226 #define	WM_TXQUEUELEN(txq)	((txq)->txq_num)
    227 #define	WM_TXQUEUELEN_MASK(txq)	(WM_TXQUEUELEN(txq) - 1)
    228 #define	WM_TXQUEUE_GC(txq)	(WM_TXQUEUELEN(txq) / 8)
    229 #define	WM_NTXDESC_82542	256
    230 #define	WM_NTXDESC_82544	4096
    231 #define	WM_NTXDESC(txq)		((txq)->txq_ndesc)
    232 #define	WM_NTXDESC_MASK(txq)	(WM_NTXDESC(txq) - 1)
    233 #define	WM_TXDESCS_SIZE(txq)	(WM_NTXDESC(txq) * (txq)->txq_descsize)
    234 #define	WM_NEXTTX(txq, x)	(((x) + 1) & WM_NTXDESC_MASK(txq))
    235 #define	WM_NEXTTXS(txq, x)	(((x) + 1) & WM_TXQUEUELEN_MASK(txq))
    236 
    237 #define	WM_MAXTXDMA		 (2 * round_page(IP_MAXPACKET)) /* for TSO */
    238 
    239 #define	WM_TXINTERQSIZE		256
    240 
    241 #ifndef WM_TX_PROCESS_LIMIT_DEFAULT
    242 #define	WM_TX_PROCESS_LIMIT_DEFAULT		100U
    243 #endif
    244 #ifndef WM_TX_INTR_PROCESS_LIMIT_DEFAULT
    245 #define	WM_TX_INTR_PROCESS_LIMIT_DEFAULT	0U
    246 #endif
    247 
    248 /*
    249  * Receive descriptor list size.  We have one Rx buffer for normal
    250  * sized packets.  Jumbo packets consume 5 Rx buffers for a full-sized
    251  * packet.  We allocate 256 receive descriptors, each with a 2k
    252  * buffer (MCLBYTES), which gives us room for 50 jumbo packets.
    253  */
    254 #define	WM_NRXDESC		256U
    255 #define	WM_NRXDESC_MASK		(WM_NRXDESC - 1)
    256 #define	WM_NEXTRX(x)		(((x) + 1) & WM_NRXDESC_MASK)
    257 #define	WM_PREVRX(x)		(((x) - 1) & WM_NRXDESC_MASK)
    258 
    259 #ifndef WM_RX_PROCESS_LIMIT_DEFAULT
    260 #define	WM_RX_PROCESS_LIMIT_DEFAULT		100U
    261 #endif
    262 #ifndef WM_RX_INTR_PROCESS_LIMIT_DEFAULT
    263 #define	WM_RX_INTR_PROCESS_LIMIT_DEFAULT	0U
    264 #endif
    265 
    266 typedef union txdescs {
    267 	wiseman_txdesc_t sctxu_txdescs[WM_NTXDESC_82544];
    268 	nq_txdesc_t	 sctxu_nq_txdescs[WM_NTXDESC_82544];
    269 } txdescs_t;
    270 
    271 typedef union rxdescs {
    272 	wiseman_rxdesc_t sctxu_rxdescs[WM_NRXDESC];
    273 	ext_rxdesc_t	 sctxu_ext_rxdescs[WM_NRXDESC]; /* 82574 only */
    274 	nq_rxdesc_t	 sctxu_nq_rxdescs[WM_NRXDESC]; /* 82575 and newer */
    275 } rxdescs_t;
    276 
    277 #define	WM_CDTXOFF(txq, x)	((txq)->txq_descsize * (x))
    278 #define	WM_CDRXOFF(rxq, x)	((rxq)->rxq_descsize * (x))
    279 
    280 /*
    281  * Software state for transmit jobs.
    282  */
    283 struct wm_txsoft {
    284 	struct mbuf *txs_mbuf;		/* head of our mbuf chain */
    285 	bus_dmamap_t txs_dmamap;	/* our DMA map */
    286 	int txs_firstdesc;		/* first descriptor in packet */
    287 	int txs_lastdesc;		/* last descriptor in packet */
    288 	int txs_ndesc;			/* # of descriptors used */
    289 };
    290 
    291 /*
    292  * Software state for receive buffers. Each descriptor gets a 2k (MCLBYTES)
    293  * buffer and a DMA map. For packets which fill more than one buffer, we chain
    294  * them together.
    295  */
    296 struct wm_rxsoft {
    297 	struct mbuf *rxs_mbuf;		/* head of our mbuf chain */
    298 	bus_dmamap_t rxs_dmamap;	/* our DMA map */
    299 };
    300 
    301 #define WM_LINKUP_TIMEOUT	50
    302 
    303 static uint16_t swfwphysem[] = {
    304 	SWFW_PHY0_SM,
    305 	SWFW_PHY1_SM,
    306 	SWFW_PHY2_SM,
    307 	SWFW_PHY3_SM
    308 };
    309 
    310 static const uint32_t wm_82580_rxpbs_table[] = {
    311 	36, 72, 144, 1, 2, 4, 8, 16, 35, 70, 140
    312 };
    313 
    314 struct wm_softc;
    315 
    316 #if defined(_LP64) && !defined(WM_DISABLE_EVENT_COUNTERS)
    317 #if !defined(WM_EVENT_COUNTERS)
    318 #define WM_EVENT_COUNTERS 1
    319 #endif
    320 #endif
    321 
    322 #ifdef WM_EVENT_COUNTERS
    323 #define WM_Q_EVCNT_DEFINE(qname, evname)				 \
    324 	char qname##_##evname##_evcnt_name[sizeof("qname##XX##evname")]; \
    325 	struct evcnt qname##_ev_##evname
    326 
    327 #define WM_Q_EVCNT_ATTACH(qname, evname, q, qnum, xname, evtype)	\
    328 	do {								\
    329 		snprintf((q)->qname##_##evname##_evcnt_name,		\
    330 		    sizeof((q)->qname##_##evname##_evcnt_name),		\
    331 		    "%s%02d%s", #qname, (qnum), #evname);		\
    332 		evcnt_attach_dynamic(&(q)->qname##_ev_##evname,		\
    333 		    (evtype), NULL, (xname),				\
    334 		    (q)->qname##_##evname##_evcnt_name);		\
    335 	} while (0)
    336 
    337 #define WM_Q_MISC_EVCNT_ATTACH(qname, evname, q, qnum, xname)		\
    338 	WM_Q_EVCNT_ATTACH(qname, evname, q, qnum, xname, EVCNT_TYPE_MISC)
    339 
    340 #define WM_Q_INTR_EVCNT_ATTACH(qname, evname, q, qnum, xname)		\
    341 	WM_Q_EVCNT_ATTACH(qname, evname, q, qnum, xname, EVCNT_TYPE_INTR)
    342 
    343 #define WM_Q_EVCNT_DETACH(qname, evname, q, qnum)	\
    344 	evcnt_detach(&(q)->qname##_ev_##evname)
    345 #endif /* WM_EVENT_COUNTERS */
    346 
    347 struct wm_txqueue {
    348 	kmutex_t *txq_lock;		/* lock for tx operations */
    349 
    350 	struct wm_softc *txq_sc;	/* shortcut (skip struct wm_queue) */
    351 
    352 	/* Software state for the transmit descriptors. */
    353 	int txq_num;			/* must be a power of two */
    354 	struct wm_txsoft txq_soft[WM_TXQUEUELEN_MAX];
    355 
    356 	/* TX control data structures. */
    357 	int txq_ndesc;			/* must be a power of two */
    358 	size_t txq_descsize;		/* a tx descriptor size */
    359 	txdescs_t *txq_descs_u;
    360 	bus_dmamap_t txq_desc_dmamap;	/* control data DMA map */
    361 	bus_dma_segment_t txq_desc_seg;	/* control data segment */
    362 	int txq_desc_rseg;		/* real number of control segment */
    363 #define	txq_desc_dma	txq_desc_dmamap->dm_segs[0].ds_addr
    364 #define	txq_descs	txq_descs_u->sctxu_txdescs
    365 #define	txq_nq_descs	txq_descs_u->sctxu_nq_txdescs
    366 
    367 	bus_addr_t txq_tdt_reg;		/* offset of TDT register */
    368 
    369 	int txq_free;			/* number of free Tx descriptors */
    370 	int txq_next;			/* next ready Tx descriptor */
    371 
    372 	int txq_sfree;			/* number of free Tx jobs */
    373 	int txq_snext;			/* next free Tx job */
    374 	int txq_sdirty;			/* dirty Tx jobs */
    375 
    376 	/* These 4 variables are used only on the 82547. */
    377 	int txq_fifo_size;		/* Tx FIFO size */
    378 	int txq_fifo_head;		/* current head of FIFO */
    379 	uint32_t txq_fifo_addr;		/* internal address of start of FIFO */
    380 	int txq_fifo_stall;		/* Tx FIFO is stalled */
    381 
    382 	/*
    383 	 * When ncpu > number of Tx queues, a Tx queue is shared by multiple
    384 	 * CPUs. This queue intermediate them without block.
    385 	 */
    386 	pcq_t *txq_interq;
    387 
    388 	/*
    389 	 * NEWQUEUE devices must use not ifp->if_flags but txq->txq_flags
    390 	 * to manage Tx H/W queue's busy flag.
    391 	 */
    392 	int txq_flags;			/* flags for H/W queue, see below */
    393 #define	WM_TXQ_NO_SPACE		0x1
    394 #define	WM_TXQ_LINKDOWN_DISCARD	0x2
    395 
    396 	bool txq_stopping;
    397 
    398 	bool txq_sending;
    399 	time_t txq_lastsent;
    400 
    401 	/* Checksum flags used for previous packet */
    402 	uint32_t	txq_last_hw_cmd;
    403 	uint8_t		txq_last_hw_fields;
    404 	uint16_t	txq_last_hw_ipcs;
    405 	uint16_t	txq_last_hw_tucs;
    406 
    407 	uint32_t txq_packets;		/* for AIM */
    408 	uint32_t txq_bytes;		/* for AIM */
    409 #ifdef WM_EVENT_COUNTERS
    410 	/* TX event counters */
    411 	WM_Q_EVCNT_DEFINE(txq, txsstall);   /* Stalled due to no txs */
    412 	WM_Q_EVCNT_DEFINE(txq, txdstall);   /* Stalled due to no txd */
    413 	WM_Q_EVCNT_DEFINE(txq, fifo_stall); /* FIFO stalls (82547) */
    414 	WM_Q_EVCNT_DEFINE(txq, txdw);	    /* Tx descriptor interrupts */
    415 	WM_Q_EVCNT_DEFINE(txq, txqe);	    /* Tx queue empty interrupts */
    416 					    /* XXX not used? */
    417 
    418 	WM_Q_EVCNT_DEFINE(txq, ipsum);	    /* IP checksums comp. */
    419 	WM_Q_EVCNT_DEFINE(txq, tusum);	    /* TCP/UDP cksums comp. */
    420 	WM_Q_EVCNT_DEFINE(txq, tusum6);	    /* TCP/UDP v6 cksums comp. */
    421 	WM_Q_EVCNT_DEFINE(txq, tso);	    /* TCP seg offload (IPv4) */
    422 	WM_Q_EVCNT_DEFINE(txq, tso6);	    /* TCP seg offload (IPv6) */
    423 	WM_Q_EVCNT_DEFINE(txq, tsopain);    /* Painful header manip. for TSO */
    424 	WM_Q_EVCNT_DEFINE(txq, pcqdrop);    /* Pkt dropped in pcq */
    425 	WM_Q_EVCNT_DEFINE(txq, descdrop);   /* Pkt dropped in MAC desc ring */
    426 					    /* other than toomanyseg */
    427 
    428 	WM_Q_EVCNT_DEFINE(txq, toomanyseg); /* Pkt dropped(toomany DMA segs) */
    429 	WM_Q_EVCNT_DEFINE(txq, defrag);	    /* m_defrag() */
    430 	WM_Q_EVCNT_DEFINE(txq, underrun);   /* Tx underrun */
    431 	WM_Q_EVCNT_DEFINE(txq, skipcontext); /* Tx skip wrong cksum context */
    432 
    433 	char txq_txseg_evcnt_names[WM_NTXSEGS][sizeof("txqXXtxsegXXX")];
    434 	struct evcnt txq_ev_txseg[WM_NTXSEGS]; /* Tx packets w/ N segments */
    435 #endif /* WM_EVENT_COUNTERS */
    436 };
    437 
    438 struct wm_rxqueue {
    439 	kmutex_t *rxq_lock;		/* lock for rx operations */
    440 
    441 	struct wm_softc *rxq_sc;	/* shortcut (skip struct wm_queue) */
    442 
    443 	/* Software state for the receive descriptors. */
    444 	struct wm_rxsoft rxq_soft[WM_NRXDESC];
    445 
    446 	/* RX control data structures. */
    447 	int rxq_ndesc;			/* must be a power of two */
    448 	size_t rxq_descsize;		/* a rx descriptor size */
    449 	rxdescs_t *rxq_descs_u;
    450 	bus_dmamap_t rxq_desc_dmamap;	/* control data DMA map */
    451 	bus_dma_segment_t rxq_desc_seg;	/* control data segment */
    452 	int rxq_desc_rseg;		/* real number of control segment */
    453 #define	rxq_desc_dma	rxq_desc_dmamap->dm_segs[0].ds_addr
    454 #define	rxq_descs	rxq_descs_u->sctxu_rxdescs
    455 #define	rxq_ext_descs	rxq_descs_u->sctxu_ext_rxdescs
    456 #define	rxq_nq_descs	rxq_descs_u->sctxu_nq_rxdescs
    457 
    458 	bus_addr_t rxq_rdt_reg;		/* offset of RDT register */
    459 
    460 	int rxq_ptr;			/* next ready Rx desc/queue ent */
    461 	int rxq_discard;
    462 	int rxq_len;
    463 	struct mbuf *rxq_head;
    464 	struct mbuf *rxq_tail;
    465 	struct mbuf **rxq_tailp;
    466 
    467 	bool rxq_stopping;
    468 
    469 	uint32_t rxq_packets;		/* for AIM */
    470 	uint32_t rxq_bytes;		/* for AIM */
    471 #ifdef WM_EVENT_COUNTERS
    472 	/* RX event counters */
    473 	WM_Q_EVCNT_DEFINE(rxq, intr);	/* Interrupts */
    474 	WM_Q_EVCNT_DEFINE(rxq, defer);	/* Rx deferred processing */
    475 
    476 	WM_Q_EVCNT_DEFINE(rxq, ipsum);	/* IP checksums checked */
    477 	WM_Q_EVCNT_DEFINE(rxq, tusum);	/* TCP/UDP cksums checked */
    478 #endif
    479 };
    480 
    481 struct wm_queue {
    482 	int wmq_id;			/* index of TX/RX queues */
    483 	int wmq_intr_idx;		/* index of MSI-X tables */
    484 
    485 	uint32_t wmq_itr;		/* interrupt interval per queue. */
    486 	bool wmq_set_itr;
    487 
    488 	struct wm_txqueue wmq_txq;
    489 	struct wm_rxqueue wmq_rxq;
    490 	char sysctlname[32];		/* Name for sysctl */
    491 
    492 	bool wmq_txrx_use_workqueue;
    493 	struct work wmq_cookie;
    494 	void *wmq_si;
    495 };
    496 
    497 struct wm_phyop {
    498 	int (*acquire)(struct wm_softc *);
    499 	void (*release)(struct wm_softc *);
    500 	int (*readreg_locked)(device_t, int, int, uint16_t *);
    501 	int (*writereg_locked)(device_t, int, int, uint16_t);
    502 	int reset_delay_us;
    503 	bool no_errprint;
    504 };
    505 
    506 struct wm_nvmop {
    507 	int (*acquire)(struct wm_softc *);
    508 	void (*release)(struct wm_softc *);
    509 	int (*read)(struct wm_softc *, int, int, uint16_t *);
    510 };
    511 
    512 /*
    513  * Software state per device.
    514  */
    515 struct wm_softc {
    516 	device_t sc_dev;		/* generic device information */
    517 	bus_space_tag_t sc_st;		/* bus space tag */
    518 	bus_space_handle_t sc_sh;	/* bus space handle */
    519 	bus_size_t sc_ss;		/* bus space size */
    520 	bus_space_tag_t sc_iot;		/* I/O space tag */
    521 	bus_space_handle_t sc_ioh;	/* I/O space handle */
    522 	bus_size_t sc_ios;		/* I/O space size */
    523 	bus_space_tag_t sc_flasht;	/* flash registers space tag */
    524 	bus_space_handle_t sc_flashh;	/* flash registers space handle */
    525 	bus_size_t sc_flashs;		/* flash registers space size */
    526 	off_t sc_flashreg_offset;	/*
    527 					 * offset to flash registers from
    528 					 * start of BAR
    529 					 */
    530 	bus_dma_tag_t sc_dmat;		/* bus DMA tag */
    531 
    532 	struct ethercom sc_ethercom;	/* Ethernet common data */
    533 	struct mii_data sc_mii;		/* MII/media information */
    534 
    535 	pci_chipset_tag_t sc_pc;
    536 	pcitag_t sc_pcitag;
    537 	int sc_bus_speed;		/* PCI/PCIX bus speed */
    538 	int sc_pcixe_capoff;		/* PCI[Xe] capability reg offset */
    539 
    540 	uint16_t sc_pcidevid;		/* PCI device ID */
    541 	wm_chip_type sc_type;		/* MAC type */
    542 	int sc_rev;			/* MAC revision */
    543 	wm_phy_type sc_phytype;		/* PHY type */
    544 	uint8_t sc_sfptype;		/* SFP type */
    545 	uint32_t sc_mediatype;		/* Media type (Copper, Fiber, SERDES)*/
    546 #define	WM_MEDIATYPE_UNKNOWN		0x00
    547 #define	WM_MEDIATYPE_FIBER		0x01
    548 #define	WM_MEDIATYPE_COPPER		0x02
    549 #define	WM_MEDIATYPE_SERDES		0x03 /* Internal SERDES */
    550 	int sc_funcid;			/* unit number of the chip (0 to 3) */
    551 	int sc_flags;			/* flags; see below */
    552 	u_short sc_if_flags;		/* last if_flags */
    553 	int sc_ec_capenable;		/* last ec_capenable */
    554 	int sc_flowflags;		/* 802.3x flow control flags */
    555 	uint16_t eee_lp_ability;	/* EEE link partner's ability */
    556 	int sc_align_tweak;
    557 
    558 	void *sc_ihs[WM_MAX_NINTR];	/*
    559 					 * interrupt cookie.
    560 					 * - legacy and msi use sc_ihs[0] only
    561 					 * - msix use sc_ihs[0] to sc_ihs[nintrs-1]
    562 					 */
    563 	pci_intr_handle_t *sc_intrs;	/*
    564 					 * legacy and msi use sc_intrs[0] only
    565 					 * msix use sc_intrs[0] to sc_ihs[nintrs-1]
    566 					 */
    567 	int sc_nintrs;			/* number of interrupts */
    568 
    569 	int sc_link_intr_idx;		/* index of MSI-X tables */
    570 
    571 	callout_t sc_tick_ch;		/* tick callout */
    572 	bool sc_core_stopping;
    573 
    574 	int sc_nvm_ver_major;
    575 	int sc_nvm_ver_minor;
    576 	int sc_nvm_ver_build;
    577 	int sc_nvm_addrbits;		/* NVM address bits */
    578 	unsigned int sc_nvm_wordsize;	/* NVM word size */
    579 	int sc_ich8_flash_base;
    580 	int sc_ich8_flash_bank_size;
    581 	int sc_nvm_k1_enabled;
    582 
    583 	int sc_nqueues;
    584 	struct wm_queue *sc_queue;
    585 	u_int sc_tx_process_limit;	/* Tx proc. repeat limit in softint */
    586 	u_int sc_tx_intr_process_limit;	/* Tx proc. repeat limit in H/W intr */
    587 	u_int sc_rx_process_limit;	/* Rx proc. repeat limit in softint */
    588 	u_int sc_rx_intr_process_limit;	/* Rx proc. repeat limit in H/W intr */
    589 	struct workqueue *sc_queue_wq;
    590 	bool sc_txrx_use_workqueue;
    591 
    592 	int sc_affinity_offset;
    593 
    594 #ifdef WM_EVENT_COUNTERS
    595 	/* Event counters. */
    596 	struct evcnt sc_ev_linkintr;	/* Link interrupts */
    597 
    598 	/* >= WM_T_82542_2_1 */
    599 	struct evcnt sc_ev_tx_xoff;	/* Tx PAUSE(!0) frames */
    600 	struct evcnt sc_ev_tx_xon;	/* Tx PAUSE(0) frames */
    601 	struct evcnt sc_ev_rx_xoff;	/* Rx PAUSE(!0) frames */
    602 	struct evcnt sc_ev_rx_xon;	/* Rx PAUSE(0) frames */
    603 	struct evcnt sc_ev_rx_macctl;	/* Rx Unsupported */
    604 
    605 	struct evcnt sc_ev_crcerrs;	/* CRC Error */
    606 	struct evcnt sc_ev_algnerrc;	/* Alignment Error */
    607 	struct evcnt sc_ev_symerrc;	/* Symbol Error */
    608 	struct evcnt sc_ev_rxerrc;	/* Receive Error */
    609 	struct evcnt sc_ev_mpc;		/* Missed Packets */
    610 	struct evcnt sc_ev_colc;	/* Collision */
    611 	struct evcnt sc_ev_sec;		/* Sequence Error */
    612 	struct evcnt sc_ev_cexterr;	/* Carrier Extension Error */
    613 	struct evcnt sc_ev_rlec;	/* Receive Length Error */
    614 	struct evcnt sc_ev_scc;		/* Single Collision */
    615 	struct evcnt sc_ev_ecol;	/* Excessive Collision */
    616 	struct evcnt sc_ev_mcc;		/* Multiple Collision */
    617 	struct evcnt sc_ev_latecol;	/* Late Collision */
    618 	struct evcnt sc_ev_dc;		/* Defer */
    619 	struct evcnt sc_ev_gprc;	/* Good Packets Rx */
    620 	struct evcnt sc_ev_bprc;	/* Broadcast Packets Rx */
    621 	struct evcnt sc_ev_mprc;	/* Multicast Packets Rx */
    622 	struct evcnt sc_ev_gptc;	/* Good Packets Tx */
    623 	struct evcnt sc_ev_gorc;	/* Good Octets Rx */
    624 	struct evcnt sc_ev_gotc;	/* Good Octets Tx */
    625 	struct evcnt sc_ev_rnbc;	/* Rx No Buffers */
    626 	struct evcnt sc_ev_ruc;		/* Rx Undersize */
    627 	struct evcnt sc_ev_rfc;		/* Rx Fragment */
    628 	struct evcnt sc_ev_roc;		/* Rx Oversize */
    629 	struct evcnt sc_ev_rjc;		/* Rx Jabber */
    630 	struct evcnt sc_ev_tor;		/* Total Octets Rx */
    631 	struct evcnt sc_ev_tot;		/* Total Octets Tx */
    632 	struct evcnt sc_ev_tpr;		/* Total Packets Rx */
    633 	struct evcnt sc_ev_tpt;		/* Total Packets Tx */
    634 	struct evcnt sc_ev_mptc;	/* Multicast Packets Tx */
    635 	struct evcnt sc_ev_bptc;	/* Broadcast Packets Tx Count */
    636 	struct evcnt sc_ev_prc64;	/* Packets Rx (64 bytes) */
    637 	struct evcnt sc_ev_prc127;	/* Packets Rx (65-127 bytes) */
    638 	struct evcnt sc_ev_prc255;	/* Packets Rx (128-255 bytes) */
    639 	struct evcnt sc_ev_prc511;	/* Packets Rx (255-511 bytes) */
    640 	struct evcnt sc_ev_prc1023;	/* Packets Rx (512-1023 bytes) */
    641 	struct evcnt sc_ev_prc1522;	/* Packets Rx (1024-1522 bytes) */
    642 	struct evcnt sc_ev_ptc64;	/* Packets Tx (64 bytes) */
    643 	struct evcnt sc_ev_ptc127;	/* Packets Tx (65-127 bytes) */
    644 	struct evcnt sc_ev_ptc255;	/* Packets Tx (128-255 bytes) */
    645 	struct evcnt sc_ev_ptc511;	/* Packets Tx (256-511 bytes) */
    646 	struct evcnt sc_ev_ptc1023;	/* Packets Tx (512-1023 bytes) */
    647 	struct evcnt sc_ev_ptc1522;	/* Packets Tx (1024-1522 Bytes) */
    648 	struct evcnt sc_ev_iac;		/* Interrupt Assertion */
    649 	struct evcnt sc_ev_icrxptc;	/* Intr. Cause Rx Pkt Timer Expire */
    650 	struct evcnt sc_ev_icrxatc;	/* Intr. Cause Rx Abs Timer Expire */
    651 	struct evcnt sc_ev_ictxptc;	/* Intr. Cause Tx Pkt Timer Expire */
    652 	struct evcnt sc_ev_ictxact;	/* Intr. Cause Tx Abs Timer Expire */
    653 	struct evcnt sc_ev_ictxqec;	/* Intr. Cause Tx Queue Empty */
    654 	struct evcnt sc_ev_ictxqmtc;	/* Intr. Cause Tx Queue Min Thresh */
    655 	struct evcnt sc_ev_icrxdmtc;	/* Intr. Cause Rx Desc Min Thresh */
    656 	struct evcnt sc_ev_icrxoc;	/* Intr. Cause Receiver Overrun */
    657 	struct evcnt sc_ev_tncrs;	/* Tx-No CRS */
    658 	struct evcnt sc_ev_tsctc;	/* TCP Segmentation Context Tx */
    659 	struct evcnt sc_ev_tsctfc;	/* TCP Segmentation Context Tx Fail */
    660 	struct evcnt sc_ev_mgtprc;	/* Management Packets RX */
    661 	struct evcnt sc_ev_mgtpdc;	/* Management Packets Dropped */
    662 	struct evcnt sc_ev_mgtptc;	/* Management Packets TX */
    663 	struct evcnt sc_ev_b2ogprc;	/* BMC2OS pkts received by host */
    664 	struct evcnt sc_ev_o2bspc;	/* OS2BMC pkts transmitted by host */
    665 	struct evcnt sc_ev_b2ospc;	/* BMC2OS pkts sent by BMC */
    666 	struct evcnt sc_ev_o2bgptc;	/* OS2BMC pkts received by BMC */
    667 
    668 #endif /* WM_EVENT_COUNTERS */
    669 
    670 	struct sysctllog *sc_sysctllog;
    671 
    672 	/* This variable are used only on the 82547. */
    673 	callout_t sc_txfifo_ch;		/* Tx FIFO stall work-around timer */
    674 
    675 	uint32_t sc_ctrl;		/* prototype CTRL register */
    676 #if 0
    677 	uint32_t sc_ctrl_ext;		/* prototype CTRL_EXT register */
    678 #endif
    679 	uint32_t sc_icr;		/* prototype interrupt bits */
    680 	uint32_t sc_itr_init;		/* prototype intr throttling reg */
    681 	uint32_t sc_tctl;		/* prototype TCTL register */
    682 	uint32_t sc_rctl;		/* prototype RCTL register */
    683 	uint32_t sc_txcw;		/* prototype TXCW register */
    684 	uint32_t sc_tipg;		/* prototype TIPG register */
    685 	uint32_t sc_fcrtl;		/* prototype FCRTL register */
    686 	uint32_t sc_pba;		/* prototype PBA register */
    687 
    688 	int sc_tbi_linkup;		/* TBI link status */
    689 	int sc_tbi_serdes_anegticks;	/* autonegotiation ticks */
    690 	int sc_tbi_serdes_ticks;	/* tbi ticks */
    691 
    692 	int sc_mchash_type;		/* multicast filter offset */
    693 
    694 	krndsource_t rnd_source;	/* random source */
    695 
    696 	struct if_percpuq *sc_ipq;	/* softint-based input queues */
    697 
    698 	kmutex_t *sc_core_lock;		/* lock for softc operations */
    699 	kmutex_t *sc_ich_phymtx;	/*
    700 					 * 82574/82583/ICH/PCH specific PHY
    701 					 * mutex. For 82574/82583, the mutex
    702 					 * is used for both PHY and NVM.
    703 					 */
    704 	kmutex_t *sc_ich_nvmmtx;	/* ICH/PCH specific NVM mutex */
    705 
    706 	struct wm_phyop phy;
    707 	struct wm_nvmop nvm;
    708 #ifdef WM_DEBUG
    709 	uint32_t sc_debug;
    710 #endif
    711 };
    712 
    713 #define WM_CORE_LOCK(_sc)						\
    714 	if ((_sc)->sc_core_lock) mutex_enter((_sc)->sc_core_lock)
    715 #define WM_CORE_UNLOCK(_sc)						\
    716 	if ((_sc)->sc_core_lock) mutex_exit((_sc)->sc_core_lock)
    717 #define WM_CORE_LOCKED(_sc)						\
    718 	(!(_sc)->sc_core_lock || mutex_owned((_sc)->sc_core_lock))
    719 
    720 #define	WM_RXCHAIN_RESET(rxq)						\
    721 do {									\
    722 	(rxq)->rxq_tailp = &(rxq)->rxq_head;				\
    723 	*(rxq)->rxq_tailp = NULL;					\
    724 	(rxq)->rxq_len = 0;						\
    725 } while (/*CONSTCOND*/0)
    726 
    727 #define	WM_RXCHAIN_LINK(rxq, m)						\
    728 do {									\
    729 	*(rxq)->rxq_tailp = (rxq)->rxq_tail = (m);			\
    730 	(rxq)->rxq_tailp = &(m)->m_next;				\
    731 } while (/*CONSTCOND*/0)
    732 
    733 #ifdef WM_EVENT_COUNTERS
    734 #ifdef __HAVE_ATOMIC64_LOADSTORE
    735 #define	WM_EVCNT_INCR(ev)						\
    736 	atomic_store_relaxed(&((ev)->ev_count),				\
    737 	    atomic_load_relaxed(&(ev)->ev_count) + 1)
    738 #define	WM_EVCNT_ADD(ev, val)						\
    739 	atomic_store_relaxed(&((ev)->ev_count),				\
    740 	    atomic_load_relaxed(&(ev)->ev_count) + (val))
    741 #else
    742 #define	WM_EVCNT_INCR(ev)						\
    743 	((ev)->ev_count)++
    744 #define	WM_EVCNT_ADD(ev, val)						\
    745 	(ev)->ev_count += (val)
    746 #endif
    747 
    748 #define WM_Q_EVCNT_INCR(qname, evname)			\
    749 	WM_EVCNT_INCR(&(qname)->qname##_ev_##evname)
    750 #define WM_Q_EVCNT_ADD(qname, evname, val)		\
    751 	WM_EVCNT_ADD(&(qname)->qname##_ev_##evname, (val))
    752 #else /* !WM_EVENT_COUNTERS */
    753 #define	WM_EVCNT_INCR(ev)	/* nothing */
    754 #define	WM_EVCNT_ADD(ev, val)	/* nothing */
    755 
    756 #define WM_Q_EVCNT_INCR(qname, evname)		/* nothing */
    757 #define WM_Q_EVCNT_ADD(qname, evname, val)	/* nothing */
    758 #endif /* !WM_EVENT_COUNTERS */
    759 
    760 #define	CSR_READ(sc, reg)						\
    761 	bus_space_read_4((sc)->sc_st, (sc)->sc_sh, (reg))
    762 #define	CSR_WRITE(sc, reg, val)						\
    763 	bus_space_write_4((sc)->sc_st, (sc)->sc_sh, (reg), (val))
    764 #define	CSR_WRITE_FLUSH(sc)						\
    765 	(void)CSR_READ((sc), WMREG_STATUS)
    766 
    767 #define ICH8_FLASH_READ32(sc, reg)					\
    768 	bus_space_read_4((sc)->sc_flasht, (sc)->sc_flashh,		\
    769 	    (reg) + sc->sc_flashreg_offset)
    770 #define ICH8_FLASH_WRITE32(sc, reg, data)				\
    771 	bus_space_write_4((sc)->sc_flasht, (sc)->sc_flashh,		\
    772 	    (reg) + sc->sc_flashreg_offset, (data))
    773 
    774 #define ICH8_FLASH_READ16(sc, reg)					\
    775 	bus_space_read_2((sc)->sc_flasht, (sc)->sc_flashh,		\
    776 	    (reg) + sc->sc_flashreg_offset)
    777 #define ICH8_FLASH_WRITE16(sc, reg, data)				\
    778 	bus_space_write_2((sc)->sc_flasht, (sc)->sc_flashh,		\
    779 	    (reg) + sc->sc_flashreg_offset, (data))
    780 
    781 #define	WM_CDTXADDR(txq, x)	((txq)->txq_desc_dma + WM_CDTXOFF((txq), (x)))
    782 #define	WM_CDRXADDR(rxq, x)	((rxq)->rxq_desc_dma + WM_CDRXOFF((rxq), (x)))
    783 
    784 #define	WM_CDTXADDR_LO(txq, x)	(WM_CDTXADDR((txq), (x)) & 0xffffffffU)
    785 #define	WM_CDTXADDR_HI(txq, x)						\
    786 	(sizeof(bus_addr_t) == 8 ?					\
    787 	 (uint64_t)WM_CDTXADDR((txq), (x)) >> 32 : 0)
    788 
    789 #define	WM_CDRXADDR_LO(rxq, x)	(WM_CDRXADDR((rxq), (x)) & 0xffffffffU)
    790 #define	WM_CDRXADDR_HI(rxq, x)						\
    791 	(sizeof(bus_addr_t) == 8 ?					\
    792 	 (uint64_t)WM_CDRXADDR((rxq), (x)) >> 32 : 0)
    793 
    794 /*
    795  * Register read/write functions.
    796  * Other than CSR_{READ|WRITE}().
    797  */
    798 #if 0
    799 static inline uint32_t wm_io_read(struct wm_softc *, int);
    800 #endif
    801 static inline void wm_io_write(struct wm_softc *, int, uint32_t);
    802 static inline void wm_82575_write_8bit_ctlr_reg(struct wm_softc *, uint32_t,
    803     uint32_t, uint32_t);
    804 static inline void wm_set_dma_addr(volatile wiseman_addr_t *, bus_addr_t);
    805 
    806 /*
    807  * Descriptor sync/init functions.
    808  */
    809 static inline void wm_cdtxsync(struct wm_txqueue *, int, int, int);
    810 static inline void wm_cdrxsync(struct wm_rxqueue *, int, int);
    811 static inline void wm_init_rxdesc(struct wm_rxqueue *, int);
    812 
    813 /*
    814  * Device driver interface functions and commonly used functions.
    815  * match, attach, detach, init, start, stop, ioctl, watchdog and so on.
    816  */
    817 static const struct wm_product *wm_lookup(const struct pci_attach_args *);
    818 static int	wm_match(device_t, cfdata_t, void *);
    819 static void	wm_attach(device_t, device_t, void *);
    820 static int	wm_detach(device_t, int);
    821 static bool	wm_suspend(device_t, const pmf_qual_t *);
    822 static bool	wm_resume(device_t, const pmf_qual_t *);
    823 static void	wm_watchdog(struct ifnet *);
    824 static void	wm_watchdog_txq(struct ifnet *, struct wm_txqueue *,
    825     uint16_t *);
    826 static void	wm_watchdog_txq_locked(struct ifnet *, struct wm_txqueue *,
    827     uint16_t *);
    828 static void	wm_tick(void *);
    829 static int	wm_ifflags_cb(struct ethercom *);
    830 static int	wm_ioctl(struct ifnet *, u_long, void *);
    831 /* MAC address related */
    832 static uint16_t	wm_check_alt_mac_addr(struct wm_softc *);
    833 static int	wm_read_mac_addr(struct wm_softc *, uint8_t *);
    834 static void	wm_set_ral(struct wm_softc *, const uint8_t *, int);
    835 static uint32_t	wm_mchash(struct wm_softc *, const uint8_t *);
    836 static int	wm_rar_count(struct wm_softc *);
    837 static void	wm_set_filter(struct wm_softc *);
    838 /* Reset and init related */
    839 static void	wm_set_vlan(struct wm_softc *);
    840 static void	wm_set_pcie_completion_timeout(struct wm_softc *);
    841 static void	wm_get_auto_rd_done(struct wm_softc *);
    842 static void	wm_lan_init_done(struct wm_softc *);
    843 static void	wm_get_cfg_done(struct wm_softc *);
    844 static int	wm_phy_post_reset(struct wm_softc *);
    845 static int	wm_write_smbus_addr(struct wm_softc *);
    846 static int	wm_init_lcd_from_nvm(struct wm_softc *);
    847 static int	wm_oem_bits_config_ich8lan(struct wm_softc *, bool);
    848 static void	wm_initialize_hardware_bits(struct wm_softc *);
    849 static uint32_t	wm_rxpbs_adjust_82580(uint32_t);
    850 static int	wm_reset_phy(struct wm_softc *);
    851 static void	wm_flush_desc_rings(struct wm_softc *);
    852 static void	wm_reset(struct wm_softc *);
    853 static int	wm_add_rxbuf(struct wm_rxqueue *, int);
    854 static void	wm_rxdrain(struct wm_rxqueue *);
    855 static void	wm_init_rss(struct wm_softc *);
    856 static void	wm_adjust_qnum(struct wm_softc *, int);
    857 static inline bool	wm_is_using_msix(struct wm_softc *);
    858 static inline bool	wm_is_using_multiqueue(struct wm_softc *);
    859 static int	wm_softint_establish_queue(struct wm_softc *, int, int);
    860 static int	wm_setup_legacy(struct wm_softc *);
    861 static int	wm_setup_msix(struct wm_softc *);
    862 static int	wm_init(struct ifnet *);
    863 static int	wm_init_locked(struct ifnet *);
    864 static void	wm_init_sysctls(struct wm_softc *);
    865 static void	wm_unset_stopping_flags(struct wm_softc *);
    866 static void	wm_set_stopping_flags(struct wm_softc *);
    867 static void	wm_stop(struct ifnet *, int);
    868 static void	wm_stop_locked(struct ifnet *, bool, bool);
    869 static void	wm_dump_mbuf_chain(struct wm_softc *, struct mbuf *);
    870 static void	wm_82547_txfifo_stall(void *);
    871 static int	wm_82547_txfifo_bugchk(struct wm_softc *, struct mbuf *);
    872 static void	wm_itrs_writereg(struct wm_softc *, struct wm_queue *);
    873 /* DMA related */
    874 static int	wm_alloc_tx_descs(struct wm_softc *, struct wm_txqueue *);
    875 static void	wm_free_tx_descs(struct wm_softc *, struct wm_txqueue *);
    876 static void	wm_init_tx_descs(struct wm_softc *, struct wm_txqueue *);
    877 static void	wm_init_tx_regs(struct wm_softc *, struct wm_queue *,
    878     struct wm_txqueue *);
    879 static int	wm_alloc_rx_descs(struct wm_softc *, struct wm_rxqueue *);
    880 static void	wm_free_rx_descs(struct wm_softc *, struct wm_rxqueue *);
    881 static void	wm_init_rx_regs(struct wm_softc *, struct wm_queue *,
    882     struct wm_rxqueue *);
    883 static int	wm_alloc_tx_buffer(struct wm_softc *, struct wm_txqueue *);
    884 static void	wm_free_tx_buffer(struct wm_softc *, struct wm_txqueue *);
    885 static void	wm_init_tx_buffer(struct wm_softc *, struct wm_txqueue *);
    886 static int	wm_alloc_rx_buffer(struct wm_softc *, struct wm_rxqueue *);
    887 static void	wm_free_rx_buffer(struct wm_softc *, struct wm_rxqueue *);
    888 static int	wm_init_rx_buffer(struct wm_softc *, struct wm_rxqueue *);
    889 static void	wm_init_tx_queue(struct wm_softc *, struct wm_queue *,
    890     struct wm_txqueue *);
    891 static int	wm_init_rx_queue(struct wm_softc *, struct wm_queue *,
    892     struct wm_rxqueue *);
    893 static int	wm_alloc_txrx_queues(struct wm_softc *);
    894 static void	wm_free_txrx_queues(struct wm_softc *);
    895 static int	wm_init_txrx_queues(struct wm_softc *);
    896 /* Start */
    897 static void	wm_tx_offload(struct wm_softc *, struct wm_txqueue *,
    898     struct wm_txsoft *, uint32_t *, uint8_t *);
    899 static inline int	wm_select_txqueue(struct ifnet *, struct mbuf *);
    900 static void	wm_start(struct ifnet *);
    901 static void	wm_start_locked(struct ifnet *);
    902 static int	wm_transmit(struct ifnet *, struct mbuf *);
    903 static void	wm_transmit_locked(struct ifnet *, struct wm_txqueue *);
    904 static void	wm_send_common_locked(struct ifnet *, struct wm_txqueue *,
    905 		    bool);
    906 static void	wm_nq_tx_offload(struct wm_softc *, struct wm_txqueue *,
    907     struct wm_txsoft *, uint32_t *, uint32_t *, bool *);
    908 static void	wm_nq_start(struct ifnet *);
    909 static void	wm_nq_start_locked(struct ifnet *);
    910 static int	wm_nq_transmit(struct ifnet *, struct mbuf *);
    911 static void	wm_nq_transmit_locked(struct ifnet *, struct wm_txqueue *);
    912 static void	wm_nq_send_common_locked(struct ifnet *, struct wm_txqueue *,
    913 		    bool);
    914 static void	wm_deferred_start_locked(struct wm_txqueue *);
    915 static void	wm_handle_queue(void *);
    916 static void	wm_handle_queue_work(struct work *, void *);
    917 /* Interrupt */
    918 static bool	wm_txeof(struct wm_txqueue *, u_int);
    919 static bool	wm_rxeof(struct wm_rxqueue *, u_int);
    920 static void	wm_linkintr_gmii(struct wm_softc *, uint32_t);
    921 static void	wm_linkintr_tbi(struct wm_softc *, uint32_t);
    922 static void	wm_linkintr_serdes(struct wm_softc *, uint32_t);
    923 static void	wm_linkintr(struct wm_softc *, uint32_t);
    924 static int	wm_intr_legacy(void *);
    925 static inline void	wm_txrxintr_disable(struct wm_queue *);
    926 static inline void	wm_txrxintr_enable(struct wm_queue *);
    927 static void	wm_itrs_calculate(struct wm_softc *, struct wm_queue *);
    928 static int	wm_txrxintr_msix(void *);
    929 static int	wm_linkintr_msix(void *);
    930 
    931 /*
    932  * Media related.
    933  * GMII, SGMII, TBI, SERDES and SFP.
    934  */
    935 /* Common */
    936 static void	wm_tbi_serdes_set_linkled(struct wm_softc *);
    937 /* GMII related */
    938 static void	wm_gmii_reset(struct wm_softc *);
    939 static void	wm_gmii_setup_phytype(struct wm_softc *, uint32_t, uint16_t);
    940 static int	wm_get_phy_id_82575(struct wm_softc *);
    941 static void	wm_gmii_mediainit(struct wm_softc *, pci_product_id_t);
    942 static int	wm_gmii_mediachange(struct ifnet *);
    943 static void	wm_gmii_mediastatus(struct ifnet *, struct ifmediareq *);
    944 static void	wm_i82543_mii_sendbits(struct wm_softc *, uint32_t, int);
    945 static uint16_t	wm_i82543_mii_recvbits(struct wm_softc *);
    946 static int	wm_gmii_i82543_readreg(device_t, int, int, uint16_t *);
    947 static int	wm_gmii_i82543_writereg(device_t, int, int, uint16_t);
    948 static int	wm_gmii_mdic_readreg(device_t, int, int, uint16_t *);
    949 static int	wm_gmii_mdic_writereg(device_t, int, int, uint16_t);
    950 static int	wm_gmii_i82544_readreg(device_t, int, int, uint16_t *);
    951 static int	wm_gmii_i82544_readreg_locked(device_t, int, int, uint16_t *);
    952 static int	wm_gmii_i82544_writereg(device_t, int, int, uint16_t);
    953 static int	wm_gmii_i82544_writereg_locked(device_t, int, int, uint16_t);
    954 static int	wm_gmii_i80003_readreg(device_t, int, int, uint16_t *);
    955 static int	wm_gmii_i80003_writereg(device_t, int, int, uint16_t);
    956 static int	wm_gmii_bm_readreg(device_t, int, int, uint16_t *);
    957 static int	wm_gmii_bm_writereg(device_t, int, int, uint16_t);
    958 static int	wm_enable_phy_wakeup_reg_access_bm(device_t, uint16_t *);
    959 static int	wm_disable_phy_wakeup_reg_access_bm(device_t, uint16_t *);
    960 static int	wm_access_phy_wakeup_reg_bm(device_t, int, int16_t *, int,
    961 	bool);
    962 static int	wm_gmii_hv_readreg(device_t, int, int, uint16_t *);
    963 static int	wm_gmii_hv_readreg_locked(device_t, int, int, uint16_t *);
    964 static int	wm_gmii_hv_writereg(device_t, int, int, uint16_t);
    965 static int	wm_gmii_hv_writereg_locked(device_t, int, int, uint16_t);
    966 static int	wm_gmii_82580_readreg(device_t, int, int, uint16_t *);
    967 static int	wm_gmii_82580_writereg(device_t, int, int, uint16_t);
    968 static int	wm_gmii_gs40g_readreg(device_t, int, int, uint16_t *);
    969 static int	wm_gmii_gs40g_writereg(device_t, int, int, uint16_t);
    970 static void	wm_gmii_statchg(struct ifnet *);
    971 /*
    972  * kumeran related (80003, ICH* and PCH*).
    973  * These functions are not for accessing MII registers but for accessing
    974  * kumeran specific registers.
    975  */
    976 static int	wm_kmrn_readreg(struct wm_softc *, int, uint16_t *);
    977 static int	wm_kmrn_readreg_locked(struct wm_softc *, int, uint16_t *);
    978 static int	wm_kmrn_writereg(struct wm_softc *, int, uint16_t);
    979 static int	wm_kmrn_writereg_locked(struct wm_softc *, int, uint16_t);
    980 /* EMI register related */
    981 static int	wm_access_emi_reg_locked(device_t, int, uint16_t *, bool);
    982 static int	wm_read_emi_reg_locked(device_t, int, uint16_t *);
    983 static int	wm_write_emi_reg_locked(device_t, int, uint16_t);
    984 /* SGMII */
    985 static bool	wm_sgmii_uses_mdio(struct wm_softc *);
    986 static void	wm_sgmii_sfp_preconfig(struct wm_softc *);
    987 static int	wm_sgmii_readreg(device_t, int, int, uint16_t *);
    988 static int	wm_sgmii_readreg_locked(device_t, int, int, uint16_t *);
    989 static int	wm_sgmii_writereg(device_t, int, int, uint16_t);
    990 static int	wm_sgmii_writereg_locked(device_t, int, int, uint16_t);
    991 /* TBI related */
    992 static bool	wm_tbi_havesignal(struct wm_softc *, uint32_t);
    993 static void	wm_tbi_mediainit(struct wm_softc *);
    994 static int	wm_tbi_mediachange(struct ifnet *);
    995 static void	wm_tbi_mediastatus(struct ifnet *, struct ifmediareq *);
    996 static int	wm_check_for_link(struct wm_softc *);
    997 static void	wm_tbi_tick(struct wm_softc *);
    998 /* SERDES related */
    999 static void	wm_serdes_power_up_link_82575(struct wm_softc *);
   1000 static int	wm_serdes_mediachange(struct ifnet *);
   1001 static void	wm_serdes_mediastatus(struct ifnet *, struct ifmediareq *);
   1002 static void	wm_serdes_tick(struct wm_softc *);
   1003 /* SFP related */
   1004 static int	wm_sfp_read_data_byte(struct wm_softc *, uint16_t, uint8_t *);
   1005 static uint32_t	wm_sfp_get_media_type(struct wm_softc *);
   1006 
   1007 /*
   1008  * NVM related.
   1009  * Microwire, SPI (w/wo EERD) and Flash.
   1010  */
   1011 /* Misc functions */
   1012 static void	wm_eeprom_sendbits(struct wm_softc *, uint32_t, int);
   1013 static void	wm_eeprom_recvbits(struct wm_softc *, uint32_t *, int);
   1014 static int	wm_nvm_set_addrbits_size_eecd(struct wm_softc *);
   1015 /* Microwire */
   1016 static int	wm_nvm_read_uwire(struct wm_softc *, int, int, uint16_t *);
   1017 /* SPI */
   1018 static int	wm_nvm_ready_spi(struct wm_softc *);
   1019 static int	wm_nvm_read_spi(struct wm_softc *, int, int, uint16_t *);
   1020 /* Using with EERD */
   1021 static int	wm_poll_eerd_eewr_done(struct wm_softc *, int);
   1022 static int	wm_nvm_read_eerd(struct wm_softc *, int, int, uint16_t *);
   1023 /* Flash */
   1024 static int	wm_nvm_valid_bank_detect_ich8lan(struct wm_softc *,
   1025     unsigned int *);
   1026 static int32_t	wm_ich8_cycle_init(struct wm_softc *);
   1027 static int32_t	wm_ich8_flash_cycle(struct wm_softc *, uint32_t);
   1028 static int32_t	wm_read_ich8_data(struct wm_softc *, uint32_t, uint32_t,
   1029     uint32_t *);
   1030 static int32_t	wm_read_ich8_byte(struct wm_softc *, uint32_t, uint8_t *);
   1031 static int32_t	wm_read_ich8_word(struct wm_softc *, uint32_t, uint16_t *);
   1032 static int32_t	wm_read_ich8_dword(struct wm_softc *, uint32_t, uint32_t *);
   1033 static int	wm_nvm_read_ich8(struct wm_softc *, int, int, uint16_t *);
   1034 static int	wm_nvm_read_spt(struct wm_softc *, int, int, uint16_t *);
   1035 /* iNVM */
   1036 static int	wm_nvm_read_word_invm(struct wm_softc *, uint16_t, uint16_t *);
   1037 static int	wm_nvm_read_invm(struct wm_softc *, int, int, uint16_t *);
   1038 /* Lock, detecting NVM type, validate checksum and read */
   1039 static int	wm_nvm_is_onboard_eeprom(struct wm_softc *);
   1040 static int	wm_nvm_flash_presence_i210(struct wm_softc *);
   1041 static int	wm_nvm_validate_checksum(struct wm_softc *);
   1042 static void	wm_nvm_version_invm(struct wm_softc *);
   1043 static void	wm_nvm_version(struct wm_softc *);
   1044 static int	wm_nvm_read(struct wm_softc *, int, int, uint16_t *);
   1045 
   1046 /*
   1047  * Hardware semaphores.
   1048  * Very complexed...
   1049  */
   1050 static int	wm_get_null(struct wm_softc *);
   1051 static void	wm_put_null(struct wm_softc *);
   1052 static int	wm_get_eecd(struct wm_softc *);
   1053 static void	wm_put_eecd(struct wm_softc *);
   1054 static int	wm_get_swsm_semaphore(struct wm_softc *); /* 8257[123] */
   1055 static void	wm_put_swsm_semaphore(struct wm_softc *);
   1056 static int	wm_get_swfw_semaphore(struct wm_softc *, uint16_t);
   1057 static void	wm_put_swfw_semaphore(struct wm_softc *, uint16_t);
   1058 static int	wm_get_nvm_80003(struct wm_softc *);
   1059 static void	wm_put_nvm_80003(struct wm_softc *);
   1060 static int	wm_get_nvm_82571(struct wm_softc *);
   1061 static void	wm_put_nvm_82571(struct wm_softc *);
   1062 static int	wm_get_phy_82575(struct wm_softc *);
   1063 static void	wm_put_phy_82575(struct wm_softc *);
   1064 static int	wm_get_swfwhw_semaphore(struct wm_softc *); /* For 574/583 */
   1065 static void	wm_put_swfwhw_semaphore(struct wm_softc *);
   1066 static int	wm_get_swflag_ich8lan(struct wm_softc *);	/* For PHY */
   1067 static void	wm_put_swflag_ich8lan(struct wm_softc *);
   1068 static int	wm_get_nvm_ich8lan(struct wm_softc *);
   1069 static void	wm_put_nvm_ich8lan(struct wm_softc *);
   1070 static int	wm_get_hw_semaphore_82573(struct wm_softc *);
   1071 static void	wm_put_hw_semaphore_82573(struct wm_softc *);
   1072 
   1073 /*
   1074  * Management mode and power management related subroutines.
   1075  * BMC, AMT, suspend/resume and EEE.
   1076  */
   1077 #if 0
   1078 static int	wm_check_mng_mode(struct wm_softc *);
   1079 static int	wm_check_mng_mode_ich8lan(struct wm_softc *);
   1080 static int	wm_check_mng_mode_82574(struct wm_softc *);
   1081 static int	wm_check_mng_mode_generic(struct wm_softc *);
   1082 #endif
   1083 static int	wm_enable_mng_pass_thru(struct wm_softc *);
   1084 static bool	wm_phy_resetisblocked(struct wm_softc *);
   1085 static void	wm_get_hw_control(struct wm_softc *);
   1086 static void	wm_release_hw_control(struct wm_softc *);
   1087 static void	wm_gate_hw_phy_config_ich8lan(struct wm_softc *, bool);
   1088 static int	wm_init_phy_workarounds_pchlan(struct wm_softc *);
   1089 static void	wm_init_manageability(struct wm_softc *);
   1090 static void	wm_release_manageability(struct wm_softc *);
   1091 static void	wm_get_wakeup(struct wm_softc *);
   1092 static int	wm_ulp_disable(struct wm_softc *);
   1093 static int	wm_enable_phy_wakeup(struct wm_softc *);
   1094 static void	wm_igp3_phy_powerdown_workaround_ich8lan(struct wm_softc *);
   1095 static void	wm_suspend_workarounds_ich8lan(struct wm_softc *);
   1096 static int	wm_resume_workarounds_pchlan(struct wm_softc *);
   1097 static void	wm_enable_wakeup(struct wm_softc *);
   1098 static void	wm_disable_aspm(struct wm_softc *);
   1099 /* LPLU (Low Power Link Up) */
   1100 static void	wm_lplu_d0_disable(struct wm_softc *);
   1101 /* EEE */
   1102 static int	wm_set_eee_i350(struct wm_softc *);
   1103 static int	wm_set_eee_pchlan(struct wm_softc *);
   1104 static int	wm_set_eee(struct wm_softc *);
   1105 
   1106 /*
   1107  * Workarounds (mainly PHY related).
   1108  * Basically, PHY's workarounds are in the PHY drivers.
   1109  */
   1110 static int	wm_kmrn_lock_loss_workaround_ich8lan(struct wm_softc *);
   1111 static void	wm_gig_downshift_workaround_ich8lan(struct wm_softc *);
   1112 static int	wm_hv_phy_workarounds_ich8lan(struct wm_softc *);
   1113 static void	wm_copy_rx_addrs_to_phy_ich8lan(struct wm_softc *);
   1114 static void	wm_copy_rx_addrs_to_phy_ich8lan_locked(struct wm_softc *);
   1115 static int	wm_lv_jumbo_workaround_ich8lan(struct wm_softc *, bool);
   1116 static int	wm_lv_phy_workarounds_ich8lan(struct wm_softc *);
   1117 static int	wm_k1_workaround_lpt_lp(struct wm_softc *, bool);
   1118 static int	wm_k1_gig_workaround_hv(struct wm_softc *, int);
   1119 static int	wm_k1_workaround_lv(struct wm_softc *);
   1120 static int	wm_link_stall_workaround_hv(struct wm_softc *);
   1121 static int	wm_set_mdio_slow_mode_hv(struct wm_softc *);
   1122 static void	wm_configure_k1_ich8lan(struct wm_softc *, int);
   1123 static void	wm_reset_init_script_82575(struct wm_softc *);
   1124 static void	wm_reset_mdicnfg_82580(struct wm_softc *);
   1125 static bool	wm_phy_is_accessible_pchlan(struct wm_softc *);
   1126 static void	wm_toggle_lanphypc_pch_lpt(struct wm_softc *);
   1127 static int	wm_platform_pm_pch_lpt(struct wm_softc *, bool);
   1128 static int	wm_pll_workaround_i210(struct wm_softc *);
   1129 static void	wm_legacy_irq_quirk_spt(struct wm_softc *);
   1130 static bool	wm_phy_need_linkdown_discard(struct wm_softc *);
   1131 static void	wm_set_linkdown_discard(struct wm_softc *);
   1132 static void	wm_clear_linkdown_discard(struct wm_softc *);
   1133 
   1134 static int	wm_sysctl_tdh_handler(SYSCTLFN_PROTO);
   1135 static int	wm_sysctl_tdt_handler(SYSCTLFN_PROTO);
   1136 #ifdef WM_DEBUG
   1137 static int	wm_sysctl_debug(SYSCTLFN_PROTO);
   1138 #endif
   1139 
   1140 CFATTACH_DECL3_NEW(wm, sizeof(struct wm_softc),
   1141     wm_match, wm_attach, wm_detach, NULL, NULL, NULL, DVF_DETACH_SHUTDOWN);
   1142 
   1143 /*
   1144  * Devices supported by this driver.
   1145  */
   1146 static const struct wm_product {
   1147 	pci_vendor_id_t		wmp_vendor;
   1148 	pci_product_id_t	wmp_product;
   1149 	const char		*wmp_name;
   1150 	wm_chip_type		wmp_type;
   1151 	uint32_t		wmp_flags;
   1152 #define	WMP_F_UNKNOWN		WM_MEDIATYPE_UNKNOWN
   1153 #define	WMP_F_FIBER		WM_MEDIATYPE_FIBER
   1154 #define	WMP_F_COPPER		WM_MEDIATYPE_COPPER
   1155 #define	WMP_F_SERDES		WM_MEDIATYPE_SERDES
   1156 #define WMP_MEDIATYPE(x)	((x) & 0x03)
   1157 } wm_products[] = {
   1158 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82542,
   1159 	  "Intel i82542 1000BASE-X Ethernet",
   1160 	  WM_T_82542_2_1,	WMP_F_FIBER },
   1161 
   1162 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82543GC_FIBER,
   1163 	  "Intel i82543GC 1000BASE-X Ethernet",
   1164 	  WM_T_82543,		WMP_F_FIBER },
   1165 
   1166 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82543GC_COPPER,
   1167 	  "Intel i82543GC 1000BASE-T Ethernet",
   1168 	  WM_T_82543,		WMP_F_COPPER },
   1169 
   1170 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82544EI_COPPER,
   1171 	  "Intel i82544EI 1000BASE-T Ethernet",
   1172 	  WM_T_82544,		WMP_F_COPPER },
   1173 
   1174 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82544EI_FIBER,
   1175 	  "Intel i82544EI 1000BASE-X Ethernet",
   1176 	  WM_T_82544,		WMP_F_FIBER },
   1177 
   1178 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82544GC_COPPER,
   1179 	  "Intel i82544GC 1000BASE-T Ethernet",
   1180 	  WM_T_82544,		WMP_F_COPPER },
   1181 
   1182 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82544GC_LOM,
   1183 	  "Intel i82544GC (LOM) 1000BASE-T Ethernet",
   1184 	  WM_T_82544,		WMP_F_COPPER },
   1185 
   1186 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82540EM,
   1187 	  "Intel i82540EM 1000BASE-T Ethernet",
   1188 	  WM_T_82540,		WMP_F_COPPER },
   1189 
   1190 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82540EM_LOM,
   1191 	  "Intel i82540EM (LOM) 1000BASE-T Ethernet",
   1192 	  WM_T_82540,		WMP_F_COPPER },
   1193 
   1194 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82540EP_LOM,
   1195 	  "Intel i82540EP 1000BASE-T Ethernet",
   1196 	  WM_T_82540,		WMP_F_COPPER },
   1197 
   1198 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82540EP,
   1199 	  "Intel i82540EP 1000BASE-T Ethernet",
   1200 	  WM_T_82540,		WMP_F_COPPER },
   1201 
   1202 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82540EP_LP,
   1203 	  "Intel i82540EP 1000BASE-T Ethernet",
   1204 	  WM_T_82540,		WMP_F_COPPER },
   1205 
   1206 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82545EM_COPPER,
   1207 	  "Intel i82545EM 1000BASE-T Ethernet",
   1208 	  WM_T_82545,		WMP_F_COPPER },
   1209 
   1210 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82545GM_COPPER,
   1211 	  "Intel i82545GM 1000BASE-T Ethernet",
   1212 	  WM_T_82545_3,		WMP_F_COPPER },
   1213 
   1214 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82545GM_FIBER,
   1215 	  "Intel i82545GM 1000BASE-X Ethernet",
   1216 	  WM_T_82545_3,		WMP_F_FIBER },
   1217 
   1218 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82545GM_SERDES,
   1219 	  "Intel i82545GM Gigabit Ethernet (SERDES)",
   1220 	  WM_T_82545_3,		WMP_F_SERDES },
   1221 
   1222 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82546EB_COPPER,
   1223 	  "Intel i82546EB 1000BASE-T Ethernet",
   1224 	  WM_T_82546,		WMP_F_COPPER },
   1225 
   1226 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82546EB_QUAD,
   1227 	  "Intel i82546EB 1000BASE-T Ethernet",
   1228 	  WM_T_82546,		WMP_F_COPPER },
   1229 
   1230 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82545EM_FIBER,
   1231 	  "Intel i82545EM 1000BASE-X Ethernet",
   1232 	  WM_T_82545,		WMP_F_FIBER },
   1233 
   1234 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82546EB_FIBER,
   1235 	  "Intel i82546EB 1000BASE-X Ethernet",
   1236 	  WM_T_82546,		WMP_F_FIBER },
   1237 
   1238 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82546GB_COPPER,
   1239 	  "Intel i82546GB 1000BASE-T Ethernet",
   1240 	  WM_T_82546_3,		WMP_F_COPPER },
   1241 
   1242 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82546GB_FIBER,
   1243 	  "Intel i82546GB 1000BASE-X Ethernet",
   1244 	  WM_T_82546_3,		WMP_F_FIBER },
   1245 
   1246 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82546GB_SERDES,
   1247 	  "Intel i82546GB Gigabit Ethernet (SERDES)",
   1248 	  WM_T_82546_3,		WMP_F_SERDES },
   1249 
   1250 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82546GB_QUAD_COPPER,
   1251 	  "i82546GB quad-port Gigabit Ethernet",
   1252 	  WM_T_82546_3,		WMP_F_COPPER },
   1253 
   1254 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82546GB_QUAD_COPPER_KSP3,
   1255 	  "i82546GB quad-port Gigabit Ethernet (KSP3)",
   1256 	  WM_T_82546_3,		WMP_F_COPPER },
   1257 
   1258 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82546GB_PCIE,
   1259 	  "Intel PRO/1000MT (82546GB)",
   1260 	  WM_T_82546_3,		WMP_F_COPPER },
   1261 
   1262 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82541EI,
   1263 	  "Intel i82541EI 1000BASE-T Ethernet",
   1264 	  WM_T_82541,		WMP_F_COPPER },
   1265 
   1266 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82541ER_LOM,
   1267 	  "Intel i82541ER (LOM) 1000BASE-T Ethernet",
   1268 	  WM_T_82541,		WMP_F_COPPER },
   1269 
   1270 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82541EI_MOBILE,
   1271 	  "Intel i82541EI Mobile 1000BASE-T Ethernet",
   1272 	  WM_T_82541,		WMP_F_COPPER },
   1273 
   1274 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82541ER,
   1275 	  "Intel i82541ER 1000BASE-T Ethernet",
   1276 	  WM_T_82541_2,		WMP_F_COPPER },
   1277 
   1278 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82541GI,
   1279 	  "Intel i82541GI 1000BASE-T Ethernet",
   1280 	  WM_T_82541_2,		WMP_F_COPPER },
   1281 
   1282 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82541GI_MOBILE,
   1283 	  "Intel i82541GI Mobile 1000BASE-T Ethernet",
   1284 	  WM_T_82541_2,		WMP_F_COPPER },
   1285 
   1286 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82541PI,
   1287 	  "Intel i82541PI 1000BASE-T Ethernet",
   1288 	  WM_T_82541_2,		WMP_F_COPPER },
   1289 
   1290 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82547EI,
   1291 	  "Intel i82547EI 1000BASE-T Ethernet",
   1292 	  WM_T_82547,		WMP_F_COPPER },
   1293 
   1294 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82547EI_MOBILE,
   1295 	  "Intel i82547EI Mobile 1000BASE-T Ethernet",
   1296 	  WM_T_82547,		WMP_F_COPPER },
   1297 
   1298 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82547GI,
   1299 	  "Intel i82547GI 1000BASE-T Ethernet",
   1300 	  WM_T_82547_2,		WMP_F_COPPER },
   1301 
   1302 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82571EB_COPPER,
   1303 	  "Intel PRO/1000 PT (82571EB)",
   1304 	  WM_T_82571,		WMP_F_COPPER },
   1305 
   1306 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82571EB_FIBER,
   1307 	  "Intel PRO/1000 PF (82571EB)",
   1308 	  WM_T_82571,		WMP_F_FIBER },
   1309 
   1310 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82571EB_SERDES,
   1311 	  "Intel PRO/1000 PB (82571EB)",
   1312 	  WM_T_82571,		WMP_F_SERDES },
   1313 
   1314 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82571EB_QUAD_COPPER,
   1315 	  "Intel PRO/1000 QT (82571EB)",
   1316 	  WM_T_82571,		WMP_F_COPPER },
   1317 
   1318 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82571GB_QUAD_COPPER,
   1319 	  "Intel PRO/1000 PT Quad Port Server Adapter",
   1320 	  WM_T_82571,		WMP_F_COPPER },
   1321 
   1322 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82571PT_QUAD_COPPER,
   1323 	  "Intel Gigabit PT Quad Port Server ExpressModule",
   1324 	  WM_T_82571,		WMP_F_COPPER },
   1325 
   1326 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82571EB_DUAL_SERDES,
   1327 	  "Intel 82571EB Dual Gigabit Ethernet (SERDES)",
   1328 	  WM_T_82571,		WMP_F_SERDES },
   1329 
   1330 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82571EB_QUAD_SERDES,
   1331 	  "Intel 82571EB Quad Gigabit Ethernet (SERDES)",
   1332 	  WM_T_82571,		WMP_F_SERDES },
   1333 
   1334 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82571EB_QUAD_FIBER,
   1335 	  "Intel 82571EB Quad 1000baseX Ethernet",
   1336 	  WM_T_82571,		WMP_F_FIBER },
   1337 
   1338 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82572EI_COPPER,
   1339 	  "Intel i82572EI 1000baseT Ethernet",
   1340 	  WM_T_82572,		WMP_F_COPPER },
   1341 
   1342 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82572EI_FIBER,
   1343 	  "Intel i82572EI 1000baseX Ethernet",
   1344 	  WM_T_82572,		WMP_F_FIBER },
   1345 
   1346 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82572EI_SERDES,
   1347 	  "Intel i82572EI Gigabit Ethernet (SERDES)",
   1348 	  WM_T_82572,		WMP_F_SERDES },
   1349 
   1350 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82572EI,
   1351 	  "Intel i82572EI 1000baseT Ethernet",
   1352 	  WM_T_82572,		WMP_F_COPPER },
   1353 
   1354 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82573E,
   1355 	  "Intel i82573E",
   1356 	  WM_T_82573,		WMP_F_COPPER },
   1357 
   1358 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82573E_IAMT,
   1359 	  "Intel i82573E IAMT",
   1360 	  WM_T_82573,		WMP_F_COPPER },
   1361 
   1362 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82573L,
   1363 	  "Intel i82573L Gigabit Ethernet",
   1364 	  WM_T_82573,		WMP_F_COPPER },
   1365 
   1366 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82574L,
   1367 	  "Intel i82574L",
   1368 	  WM_T_82574,		WMP_F_COPPER },
   1369 
   1370 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82574LA,
   1371 	  "Intel i82574L",
   1372 	  WM_T_82574,		WMP_F_COPPER },
   1373 
   1374 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82583V,
   1375 	  "Intel i82583V",
   1376 	  WM_T_82583,		WMP_F_COPPER },
   1377 
   1378 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_80K3LAN_CPR_DPT,
   1379 	  "i80003 dual 1000baseT Ethernet",
   1380 	  WM_T_80003,		WMP_F_COPPER },
   1381 
   1382 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_80K3LAN_FIB_DPT,
   1383 	  "i80003 dual 1000baseX Ethernet",
   1384 	  WM_T_80003,		WMP_F_COPPER },
   1385 
   1386 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_80K3LAN_SDS_DPT,
   1387 	  "Intel i80003ES2 dual Gigabit Ethernet (SERDES)",
   1388 	  WM_T_80003,		WMP_F_SERDES },
   1389 
   1390 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_80K3LAN_CPR_SPT,
   1391 	  "Intel i80003 1000baseT Ethernet",
   1392 	  WM_T_80003,		WMP_F_COPPER },
   1393 
   1394 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_80K3LAN_SDS_SPT,
   1395 	  "Intel i80003 Gigabit Ethernet (SERDES)",
   1396 	  WM_T_80003,		WMP_F_SERDES },
   1397 
   1398 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82801H_M_AMT,
   1399 	  "Intel i82801H (M_AMT) LAN Controller",
   1400 	  WM_T_ICH8,		WMP_F_COPPER },
   1401 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82801H_AMT,
   1402 	  "Intel i82801H (AMT) LAN Controller",
   1403 	  WM_T_ICH8,		WMP_F_COPPER },
   1404 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82801H_LAN,
   1405 	  "Intel i82801H LAN Controller",
   1406 	  WM_T_ICH8,		WMP_F_COPPER },
   1407 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82801H_IFE_LAN,
   1408 	  "Intel i82801H (IFE) 10/100 LAN Controller",
   1409 	  WM_T_ICH8,		WMP_F_COPPER },
   1410 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82801H_M_LAN,
   1411 	  "Intel i82801H (M) LAN Controller",
   1412 	  WM_T_ICH8,		WMP_F_COPPER },
   1413 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82801H_IFE_GT,
   1414 	  "Intel i82801H IFE (GT) 10/100 LAN Controller",
   1415 	  WM_T_ICH8,		WMP_F_COPPER },
   1416 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82801H_IFE_G,
   1417 	  "Intel i82801H IFE (G) 10/100 LAN Controller",
   1418 	  WM_T_ICH8,		WMP_F_COPPER },
   1419 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82801H_82567V_3,
   1420 	  "82567V-3 LAN Controller",
   1421 	  WM_T_ICH8,		WMP_F_COPPER },
   1422 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82801I_IGP_AMT,
   1423 	  "82801I (AMT) LAN Controller",
   1424 	  WM_T_ICH9,		WMP_F_COPPER },
   1425 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82801I_IFE,
   1426 	  "82801I 10/100 LAN Controller",
   1427 	  WM_T_ICH9,		WMP_F_COPPER },
   1428 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82801I_IFE_G,
   1429 	  "82801I (G) 10/100 LAN Controller",
   1430 	  WM_T_ICH9,		WMP_F_COPPER },
   1431 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82801I_IFE_GT,
   1432 	  "82801I (GT) 10/100 LAN Controller",
   1433 	  WM_T_ICH9,		WMP_F_COPPER },
   1434 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82801I_IGP_C,
   1435 	  "82801I (C) LAN Controller",
   1436 	  WM_T_ICH9,		WMP_F_COPPER },
   1437 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82801I_IGP_M,
   1438 	  "82801I mobile LAN Controller",
   1439 	  WM_T_ICH9,		WMP_F_COPPER },
   1440 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82801I_IGP_M_V,
   1441 	  "82801I mobile (V) LAN Controller",
   1442 	  WM_T_ICH9,		WMP_F_COPPER },
   1443 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82801I_IGP_M_AMT,
   1444 	  "82801I mobile (AMT) LAN Controller",
   1445 	  WM_T_ICH9,		WMP_F_COPPER },
   1446 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82801I_BM,
   1447 	  "82567LM-4 LAN Controller",
   1448 	  WM_T_ICH9,		WMP_F_COPPER },
   1449 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82801J_R_BM_LM,
   1450 	  "82567LM-2 LAN Controller",
   1451 	  WM_T_ICH10,		WMP_F_COPPER },
   1452 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82801J_R_BM_LF,
   1453 	  "82567LF-2 LAN Controller",
   1454 	  WM_T_ICH10,		WMP_F_COPPER },
   1455 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82801J_D_BM_LM,
   1456 	  "82567LM-3 LAN Controller",
   1457 	  WM_T_ICH10,		WMP_F_COPPER },
   1458 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82801J_D_BM_LF,
   1459 	  "82567LF-3 LAN Controller",
   1460 	  WM_T_ICH10,		WMP_F_COPPER },
   1461 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82801J_R_BM_V,
   1462 	  "82567V-2 LAN Controller",
   1463 	  WM_T_ICH10,		WMP_F_COPPER },
   1464 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82801J_D_BM_V,
   1465 	  "82567V-3? LAN Controller",
   1466 	  WM_T_ICH10,		WMP_F_COPPER },
   1467 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_HANKSVILLE,
   1468 	  "HANKSVILLE LAN Controller",
   1469 	  WM_T_ICH10,		WMP_F_COPPER },
   1470 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_PCH_M_LM,
   1471 	  "PCH LAN (82577LM) Controller",
   1472 	  WM_T_PCH,		WMP_F_COPPER },
   1473 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_PCH_M_LC,
   1474 	  "PCH LAN (82577LC) Controller",
   1475 	  WM_T_PCH,		WMP_F_COPPER },
   1476 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_PCH_D_DM,
   1477 	  "PCH LAN (82578DM) Controller",
   1478 	  WM_T_PCH,		WMP_F_COPPER },
   1479 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_PCH_D_DC,
   1480 	  "PCH LAN (82578DC) Controller",
   1481 	  WM_T_PCH,		WMP_F_COPPER },
   1482 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_PCH2_LV_LM,
   1483 	  "PCH2 LAN (82579LM) Controller",
   1484 	  WM_T_PCH2,		WMP_F_COPPER },
   1485 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_PCH2_LV_V,
   1486 	  "PCH2 LAN (82579V) Controller",
   1487 	  WM_T_PCH2,		WMP_F_COPPER },
   1488 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82575EB_COPPER,
   1489 	  "82575EB dual-1000baseT Ethernet",
   1490 	  WM_T_82575,		WMP_F_COPPER },
   1491 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82575EB_FIBER_SERDES,
   1492 	  "82575EB dual-1000baseX Ethernet (SERDES)",
   1493 	  WM_T_82575,		WMP_F_SERDES },
   1494 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82575GB_QUAD_COPPER,
   1495 	  "82575GB quad-1000baseT Ethernet",
   1496 	  WM_T_82575,		WMP_F_COPPER },
   1497 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82575GB_QUAD_COPPER_PM,
   1498 	  "82575GB quad-1000baseT Ethernet (PM)",
   1499 	  WM_T_82575,		WMP_F_COPPER },
   1500 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82576_COPPER,
   1501 	  "82576 1000BaseT Ethernet",
   1502 	  WM_T_82576,		WMP_F_COPPER },
   1503 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82576_FIBER,
   1504 	  "82576 1000BaseX Ethernet",
   1505 	  WM_T_82576,		WMP_F_FIBER },
   1506 
   1507 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82576_SERDES,
   1508 	  "82576 gigabit Ethernet (SERDES)",
   1509 	  WM_T_82576,		WMP_F_SERDES },
   1510 
   1511 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82576_QUAD_COPPER,
   1512 	  "82576 quad-1000BaseT Ethernet",
   1513 	  WM_T_82576,		WMP_F_COPPER },
   1514 
   1515 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82576_QUAD_COPPER_ET2,
   1516 	  "82576 Gigabit ET2 Quad Port Server Adapter",
   1517 	  WM_T_82576,		WMP_F_COPPER },
   1518 
   1519 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82576_NS,
   1520 	  "82576 gigabit Ethernet",
   1521 	  WM_T_82576,		WMP_F_COPPER },
   1522 
   1523 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82576_NS_SERDES,
   1524 	  "82576 gigabit Ethernet (SERDES)",
   1525 	  WM_T_82576,		WMP_F_SERDES },
   1526 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82576_SERDES_QUAD,
   1527 	  "82576 quad-gigabit Ethernet (SERDES)",
   1528 	  WM_T_82576,		WMP_F_SERDES },
   1529 
   1530 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82580_COPPER,
   1531 	  "82580 1000BaseT Ethernet",
   1532 	  WM_T_82580,		WMP_F_COPPER },
   1533 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82580_FIBER,
   1534 	  "82580 1000BaseX Ethernet",
   1535 	  WM_T_82580,		WMP_F_FIBER },
   1536 
   1537 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82580_SERDES,
   1538 	  "82580 1000BaseT Ethernet (SERDES)",
   1539 	  WM_T_82580,		WMP_F_SERDES },
   1540 
   1541 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82580_SGMII,
   1542 	  "82580 gigabit Ethernet (SGMII)",
   1543 	  WM_T_82580,		WMP_F_COPPER },
   1544 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82580_COPPER_DUAL,
   1545 	  "82580 dual-1000BaseT Ethernet",
   1546 	  WM_T_82580,		WMP_F_COPPER },
   1547 
   1548 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_82580_QUAD_FIBER,
   1549 	  "82580 quad-1000BaseX Ethernet",
   1550 	  WM_T_82580,		WMP_F_FIBER },
   1551 
   1552 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_DH89XXCC_SGMII,
   1553 	  "DH89XXCC Gigabit Ethernet (SGMII)",
   1554 	  WM_T_82580,		WMP_F_COPPER },
   1555 
   1556 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_DH89XXCC_SERDES,
   1557 	  "DH89XXCC Gigabit Ethernet (SERDES)",
   1558 	  WM_T_82580,		WMP_F_SERDES },
   1559 
   1560 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_DH89XXCC_BPLANE,
   1561 	  "DH89XXCC 1000BASE-KX Ethernet",
   1562 	  WM_T_82580,		WMP_F_SERDES },
   1563 
   1564 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_DH89XXCC_SFP,
   1565 	  "DH89XXCC Gigabit Ethernet (SFP)",
   1566 	  WM_T_82580,		WMP_F_SERDES },
   1567 
   1568 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_I350_COPPER,
   1569 	  "I350 Gigabit Network Connection",
   1570 	  WM_T_I350,		WMP_F_COPPER },
   1571 
   1572 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_I350_FIBER,
   1573 	  "I350 Gigabit Fiber Network Connection",
   1574 	  WM_T_I350,		WMP_F_FIBER },
   1575 
   1576 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_I350_SERDES,
   1577 	  "I350 Gigabit Backplane Connection",
   1578 	  WM_T_I350,		WMP_F_SERDES },
   1579 
   1580 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_I350_DA4,
   1581 	  "I350 Quad Port Gigabit Ethernet",
   1582 	  WM_T_I350,		WMP_F_SERDES },
   1583 
   1584 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_I350_SGMII,
   1585 	  "I350 Gigabit Connection",
   1586 	  WM_T_I350,		WMP_F_COPPER },
   1587 
   1588 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_C2000_1000KX,
   1589 	  "I354 Gigabit Ethernet (KX)",
   1590 	  WM_T_I354,		WMP_F_SERDES },
   1591 
   1592 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_C2000_SGMII,
   1593 	  "I354 Gigabit Ethernet (SGMII)",
   1594 	  WM_T_I354,		WMP_F_COPPER },
   1595 
   1596 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_C2000_25GBE,
   1597 	  "I354 Gigabit Ethernet (2.5G)",
   1598 	  WM_T_I354,		WMP_F_COPPER },
   1599 
   1600 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_I210_T1,
   1601 	  "I210-T1 Ethernet Server Adapter",
   1602 	  WM_T_I210,		WMP_F_COPPER },
   1603 
   1604 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_I210_COPPER_OEM1,
   1605 	  "I210 Ethernet (Copper OEM)",
   1606 	  WM_T_I210,		WMP_F_COPPER },
   1607 
   1608 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_I210_COPPER_IT,
   1609 	  "I210 Ethernet (Copper IT)",
   1610 	  WM_T_I210,		WMP_F_COPPER },
   1611 
   1612 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_I210_COPPER_WOF,
   1613 	  "I210 Ethernet (Copper, FLASH less)",
   1614 	  WM_T_I210,		WMP_F_COPPER },
   1615 
   1616 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_I210_FIBER,
   1617 	  "I210 Gigabit Ethernet (Fiber)",
   1618 	  WM_T_I210,		WMP_F_FIBER },
   1619 
   1620 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_I210_SERDES,
   1621 	  "I210 Gigabit Ethernet (SERDES)",
   1622 	  WM_T_I210,		WMP_F_SERDES },
   1623 
   1624 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_I210_SERDES_WOF,
   1625 	  "I210 Gigabit Ethernet (SERDES, FLASH less)",
   1626 	  WM_T_I210,		WMP_F_SERDES },
   1627 
   1628 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_I210_SGMII,
   1629 	  "I210 Gigabit Ethernet (SGMII)",
   1630 	  WM_T_I210,		WMP_F_COPPER },
   1631 
   1632 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_I210_SGMII_WOF,
   1633 	  "I210 Gigabit Ethernet (SGMII, FLASH less)",
   1634 	  WM_T_I210,		WMP_F_COPPER },
   1635 
   1636 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_I211_COPPER,
   1637 	  "I211 Ethernet (COPPER)",
   1638 	  WM_T_I211,		WMP_F_COPPER },
   1639 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_I217_V,
   1640 	  "I217 V Ethernet Connection",
   1641 	  WM_T_PCH_LPT,		WMP_F_COPPER },
   1642 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_I217_LM,
   1643 	  "I217 LM Ethernet Connection",
   1644 	  WM_T_PCH_LPT,		WMP_F_COPPER },
   1645 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_I218_V,
   1646 	  "I218 V Ethernet Connection",
   1647 	  WM_T_PCH_LPT,		WMP_F_COPPER },
   1648 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_I218_V2,
   1649 	  "I218 V Ethernet Connection",
   1650 	  WM_T_PCH_LPT,		WMP_F_COPPER },
   1651 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_I218_V3,
   1652 	  "I218 V Ethernet Connection",
   1653 	  WM_T_PCH_LPT,		WMP_F_COPPER },
   1654 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_I218_LM,
   1655 	  "I218 LM Ethernet Connection",
   1656 	  WM_T_PCH_LPT,		WMP_F_COPPER },
   1657 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_I218_LM2,
   1658 	  "I218 LM Ethernet Connection",
   1659 	  WM_T_PCH_LPT,		WMP_F_COPPER },
   1660 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_I218_LM3,
   1661 	  "I218 LM Ethernet Connection",
   1662 	  WM_T_PCH_LPT,		WMP_F_COPPER },
   1663 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_I219_LM,
   1664 	  "I219 LM Ethernet Connection",
   1665 	  WM_T_PCH_SPT,		WMP_F_COPPER },
   1666 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_I219_LM2,
   1667 	  "I219 LM (2) Ethernet Connection",
   1668 	  WM_T_PCH_SPT,		WMP_F_COPPER },
   1669 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_I219_LM3,
   1670 	  "I219 LM (3) Ethernet Connection",
   1671 	  WM_T_PCH_SPT,		WMP_F_COPPER },
   1672 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_I219_LM4,
   1673 	  "I219 LM (4) Ethernet Connection",
   1674 	  WM_T_PCH_SPT,		WMP_F_COPPER },
   1675 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_I219_LM5,
   1676 	  "I219 LM (5) Ethernet Connection",
   1677 	  WM_T_PCH_SPT,		WMP_F_COPPER },
   1678 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_I219_LM6,
   1679 	  "I219 LM (6) Ethernet Connection",
   1680 	  WM_T_PCH_CNP,		WMP_F_COPPER },
   1681 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_I219_LM7,
   1682 	  "I219 LM (7) Ethernet Connection",
   1683 	  WM_T_PCH_CNP,		WMP_F_COPPER },
   1684 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_I219_LM8,
   1685 	  "I219 LM (8) Ethernet Connection",
   1686 	  WM_T_PCH_CNP,		WMP_F_COPPER },
   1687 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_I219_LM9,
   1688 	  "I219 LM (9) Ethernet Connection",
   1689 	  WM_T_PCH_CNP,		WMP_F_COPPER },
   1690 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_I219_LM10,
   1691 	  "I219 LM (10) Ethernet Connection",
   1692 	  WM_T_PCH_CNP,		WMP_F_COPPER },
   1693 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_I219_LM11,
   1694 	  "I219 LM (11) Ethernet Connection",
   1695 	  WM_T_PCH_CNP,		WMP_F_COPPER },
   1696 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_I219_LM12,
   1697 	  "I219 LM (12) Ethernet Connection",
   1698 	  WM_T_PCH_SPT,		WMP_F_COPPER },
   1699 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_I219_LM13,
   1700 	  "I219 LM (13) Ethernet Connection",
   1701 	  WM_T_PCH_CNP,		WMP_F_COPPER },
   1702 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_I219_LM14,
   1703 	  "I219 LM (14) Ethernet Connection",
   1704 	  WM_T_PCH_CNP,		WMP_F_COPPER },
   1705 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_I219_LM15,
   1706 	  "I219 LM (15) Ethernet Connection",
   1707 	  WM_T_PCH_CNP,		WMP_F_COPPER },
   1708 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_I219_LM16,
   1709 	  "I219 LM (16) Ethernet Connection",
   1710 	  WM_T_PCH_CNP,		WMP_F_COPPER },
   1711 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_I219_LM17,
   1712 	  "I219 LM (17) Ethernet Connection",
   1713 	  WM_T_PCH_CNP,		WMP_F_COPPER },
   1714 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_I219_LM18,
   1715 	  "I219 LM (18) Ethernet Connection",
   1716 	  WM_T_PCH_CNP,		WMP_F_COPPER },
   1717 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_I219_LM19,
   1718 	  "I219 LM (19) Ethernet Connection",
   1719 	  WM_T_PCH_CNP,		WMP_F_COPPER },
   1720 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_I219_V,
   1721 	  "I219 V Ethernet Connection",
   1722 	  WM_T_PCH_SPT,		WMP_F_COPPER },
   1723 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_I219_V2,
   1724 	  "I219 V (2) Ethernet Connection",
   1725 	  WM_T_PCH_SPT,		WMP_F_COPPER },
   1726 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_I219_V4,
   1727 	  "I219 V (4) Ethernet Connection",
   1728 	  WM_T_PCH_SPT,		WMP_F_COPPER },
   1729 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_I219_V5,
   1730 	  "I219 V (5) Ethernet Connection",
   1731 	  WM_T_PCH_SPT,		WMP_F_COPPER },
   1732 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_I219_V6,
   1733 	  "I219 V (6) Ethernet Connection",
   1734 	  WM_T_PCH_CNP,		WMP_F_COPPER },
   1735 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_I219_V7,
   1736 	  "I219 V (7) Ethernet Connection",
   1737 	  WM_T_PCH_CNP,		WMP_F_COPPER },
   1738 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_I219_V8,
   1739 	  "I219 V (8) Ethernet Connection",
   1740 	  WM_T_PCH_CNP,		WMP_F_COPPER },
   1741 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_I219_V9,
   1742 	  "I219 V (9) Ethernet Connection",
   1743 	  WM_T_PCH_CNP,		WMP_F_COPPER },
   1744 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_I219_V10,
   1745 	  "I219 V (10) Ethernet Connection",
   1746 	  WM_T_PCH_CNP,		WMP_F_COPPER },
   1747 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_I219_V11,
   1748 	  "I219 V (11) Ethernet Connection",
   1749 	  WM_T_PCH_CNP,		WMP_F_COPPER },
   1750 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_I219_V12,
   1751 	  "I219 V (12) Ethernet Connection",
   1752 	  WM_T_PCH_SPT,		WMP_F_COPPER },
   1753 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_I219_V13,
   1754 	  "I219 V (13) Ethernet Connection",
   1755 	  WM_T_PCH_CNP,		WMP_F_COPPER },
   1756 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_I219_V14,
   1757 	  "I219 V (14) Ethernet Connection",
   1758 	  WM_T_PCH_CNP,		WMP_F_COPPER },
   1759 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_I219_V15,
   1760 	  "I219 V (15) Ethernet Connection",
   1761 	  WM_T_PCH_CNP,		WMP_F_COPPER },
   1762 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_I219_V16,
   1763 	  "I219 V (16) Ethernet Connection",
   1764 	  WM_T_PCH_CNP,		WMP_F_COPPER },
   1765 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_I219_V17,
   1766 	  "I219 V (17) Ethernet Connection",
   1767 	  WM_T_PCH_CNP,		WMP_F_COPPER },
   1768 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_I219_V18,
   1769 	  "I219 V (18) Ethernet Connection",
   1770 	  WM_T_PCH_CNP,		WMP_F_COPPER },
   1771 	{ PCI_VENDOR_INTEL,	PCI_PRODUCT_INTEL_I219_V19,
   1772 	  "I219 V (19) Ethernet Connection",
   1773 	  WM_T_PCH_CNP,		WMP_F_COPPER },
   1774 	{ 0,			0,
   1775 	  NULL,
   1776 	  0,			0 },
   1777 };
   1778 
   1779 /*
   1780  * Register read/write functions.
   1781  * Other than CSR_{READ|WRITE}().
   1782  */
   1783 
   1784 #if 0 /* Not currently used */
   1785 static inline uint32_t
   1786 wm_io_read(struct wm_softc *sc, int reg)
   1787 {
   1788 
   1789 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, 0, reg);
   1790 	return (bus_space_read_4(sc->sc_iot, sc->sc_ioh, 4));
   1791 }
   1792 #endif
   1793 
   1794 static inline void
   1795 wm_io_write(struct wm_softc *sc, int reg, uint32_t val)
   1796 {
   1797 
   1798 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, 0, reg);
   1799 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, 4, val);
   1800 }
   1801 
   1802 static inline void
   1803 wm_82575_write_8bit_ctlr_reg(struct wm_softc *sc, uint32_t reg, uint32_t off,
   1804     uint32_t data)
   1805 {
   1806 	uint32_t regval;
   1807 	int i;
   1808 
   1809 	regval = (data & SCTL_CTL_DATA_MASK) | (off << SCTL_CTL_ADDR_SHIFT);
   1810 
   1811 	CSR_WRITE(sc, reg, regval);
   1812 
   1813 	for (i = 0; i < SCTL_CTL_POLL_TIMEOUT; i++) {
   1814 		delay(5);
   1815 		if (CSR_READ(sc, reg) & SCTL_CTL_READY)
   1816 			break;
   1817 	}
   1818 	if (i == SCTL_CTL_POLL_TIMEOUT) {
   1819 		aprint_error("%s: WARNING:"
   1820 		    " i82575 reg 0x%08x setup did not indicate ready\n",
   1821 		    device_xname(sc->sc_dev), reg);
   1822 	}
   1823 }
   1824 
   1825 static inline void
   1826 wm_set_dma_addr(volatile wiseman_addr_t *wa, bus_addr_t v)
   1827 {
   1828 	wa->wa_low = htole32(BUS_ADDR_LO32(v));
   1829 	wa->wa_high = htole32(BUS_ADDR_HI32(v));
   1830 }
   1831 
   1832 /*
   1833  * Descriptor sync/init functions.
   1834  */
   1835 static inline void
   1836 wm_cdtxsync(struct wm_txqueue *txq, int start, int num, int ops)
   1837 {
   1838 	struct wm_softc *sc = txq->txq_sc;
   1839 
   1840 	/* If it will wrap around, sync to the end of the ring. */
   1841 	if ((start + num) > WM_NTXDESC(txq)) {
   1842 		bus_dmamap_sync(sc->sc_dmat, txq->txq_desc_dmamap,
   1843 		    WM_CDTXOFF(txq, start), txq->txq_descsize *
   1844 		    (WM_NTXDESC(txq) - start), ops);
   1845 		num -= (WM_NTXDESC(txq) - start);
   1846 		start = 0;
   1847 	}
   1848 
   1849 	/* Now sync whatever is left. */
   1850 	bus_dmamap_sync(sc->sc_dmat, txq->txq_desc_dmamap,
   1851 	    WM_CDTXOFF(txq, start), txq->txq_descsize * num, ops);
   1852 }
   1853 
   1854 static inline void
   1855 wm_cdrxsync(struct wm_rxqueue *rxq, int start, int ops)
   1856 {
   1857 	struct wm_softc *sc = rxq->rxq_sc;
   1858 
   1859 	bus_dmamap_sync(sc->sc_dmat, rxq->rxq_desc_dmamap,
   1860 	    WM_CDRXOFF(rxq, start), rxq->rxq_descsize, ops);
   1861 }
   1862 
   1863 static inline void
   1864 wm_init_rxdesc(struct wm_rxqueue *rxq, int start)
   1865 {
   1866 	struct wm_softc *sc = rxq->rxq_sc;
   1867 	struct wm_rxsoft *rxs = &rxq->rxq_soft[start];
   1868 	struct mbuf *m = rxs->rxs_mbuf;
   1869 
   1870 	/*
   1871 	 * Note: We scoot the packet forward 2 bytes in the buffer
   1872 	 * so that the payload after the Ethernet header is aligned
   1873 	 * to a 4-byte boundary.
   1874 
   1875 	 * XXX BRAINDAMAGE ALERT!
   1876 	 * The stupid chip uses the same size for every buffer, which
   1877 	 * is set in the Receive Control register.  We are using the 2K
   1878 	 * size option, but what we REALLY want is (2K - 2)!  For this
   1879 	 * reason, we can't "scoot" packets longer than the standard
   1880 	 * Ethernet MTU.  On strict-alignment platforms, if the total
   1881 	 * size exceeds (2K - 2) we set align_tweak to 0 and let
   1882 	 * the upper layer copy the headers.
   1883 	 */
   1884 	m->m_data = m->m_ext.ext_buf + sc->sc_align_tweak;
   1885 
   1886 	if (sc->sc_type == WM_T_82574) {
   1887 		ext_rxdesc_t *rxd = &rxq->rxq_ext_descs[start];
   1888 		rxd->erx_data.erxd_addr =
   1889 		    htole64(rxs->rxs_dmamap->dm_segs[0].ds_addr + sc->sc_align_tweak);
   1890 		rxd->erx_data.erxd_dd = 0;
   1891 	} else if ((sc->sc_flags & WM_F_NEWQUEUE) != 0) {
   1892 		nq_rxdesc_t *rxd = &rxq->rxq_nq_descs[start];
   1893 
   1894 		rxd->nqrx_data.nrxd_paddr =
   1895 		    htole64(rxs->rxs_dmamap->dm_segs[0].ds_addr + sc->sc_align_tweak);
   1896 		/* Currently, split header is not supported. */
   1897 		rxd->nqrx_data.nrxd_haddr = 0;
   1898 	} else {
   1899 		wiseman_rxdesc_t *rxd = &rxq->rxq_descs[start];
   1900 
   1901 		wm_set_dma_addr(&rxd->wrx_addr,
   1902 		    rxs->rxs_dmamap->dm_segs[0].ds_addr + sc->sc_align_tweak);
   1903 		rxd->wrx_len = 0;
   1904 		rxd->wrx_cksum = 0;
   1905 		rxd->wrx_status = 0;
   1906 		rxd->wrx_errors = 0;
   1907 		rxd->wrx_special = 0;
   1908 	}
   1909 	wm_cdrxsync(rxq, start, BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
   1910 
   1911 	CSR_WRITE(sc, rxq->rxq_rdt_reg, start);
   1912 }
   1913 
   1914 /*
   1915  * Device driver interface functions and commonly used functions.
   1916  * match, attach, detach, init, start, stop, ioctl, watchdog and so on.
   1917  */
   1918 
   1919 /* Lookup supported device table */
   1920 static const struct wm_product *
   1921 wm_lookup(const struct pci_attach_args *pa)
   1922 {
   1923 	const struct wm_product *wmp;
   1924 
   1925 	for (wmp = wm_products; wmp->wmp_name != NULL; wmp++) {
   1926 		if (PCI_VENDOR(pa->pa_id) == wmp->wmp_vendor &&
   1927 		    PCI_PRODUCT(pa->pa_id) == wmp->wmp_product)
   1928 			return wmp;
   1929 	}
   1930 	return NULL;
   1931 }
   1932 
   1933 /* The match function (ca_match) */
   1934 static int
   1935 wm_match(device_t parent, cfdata_t cf, void *aux)
   1936 {
   1937 	struct pci_attach_args *pa = aux;
   1938 
   1939 	if (wm_lookup(pa) != NULL)
   1940 		return 1;
   1941 
   1942 	return 0;
   1943 }
   1944 
   1945 /* The attach function (ca_attach) */
   1946 static void
   1947 wm_attach(device_t parent, device_t self, void *aux)
   1948 {
   1949 	struct wm_softc *sc = device_private(self);
   1950 	struct pci_attach_args *pa = aux;
   1951 	prop_dictionary_t dict;
   1952 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1953 	pci_chipset_tag_t pc = pa->pa_pc;
   1954 	int counts[PCI_INTR_TYPE_SIZE];
   1955 	pci_intr_type_t max_type;
   1956 	const char *eetype, *xname;
   1957 	bus_space_tag_t memt;
   1958 	bus_space_handle_t memh;
   1959 	bus_size_t memsize;
   1960 	int memh_valid;
   1961 	int i, error;
   1962 	const struct wm_product *wmp;
   1963 	prop_data_t ea;
   1964 	prop_number_t pn;
   1965 	uint8_t enaddr[ETHER_ADDR_LEN];
   1966 	char buf[256];
   1967 	char wqname[MAXCOMLEN];
   1968 	uint16_t cfg1, cfg2, swdpin, nvmword;
   1969 	pcireg_t preg, memtype;
   1970 	uint16_t eeprom_data, apme_mask;
   1971 	bool force_clear_smbi;
   1972 	uint32_t link_mode;
   1973 	uint32_t reg;
   1974 
   1975 #if defined(WM_DEBUG) && defined(WM_DEBUG_DEFAULT)
   1976 	sc->sc_debug = WM_DEBUG_DEFAULT;
   1977 #endif
   1978 	sc->sc_dev = self;
   1979 	callout_init(&sc->sc_tick_ch, WM_CALLOUT_FLAGS);
   1980 	callout_setfunc(&sc->sc_tick_ch, wm_tick, sc);
   1981 	sc->sc_core_stopping = false;
   1982 
   1983 	wmp = wm_lookup(pa);
   1984 #ifdef DIAGNOSTIC
   1985 	if (wmp == NULL) {
   1986 		printf("\n");
   1987 		panic("wm_attach: impossible");
   1988 	}
   1989 #endif
   1990 	sc->sc_mediatype = WMP_MEDIATYPE(wmp->wmp_flags);
   1991 
   1992 	sc->sc_pc = pa->pa_pc;
   1993 	sc->sc_pcitag = pa->pa_tag;
   1994 
   1995 	if (pci_dma64_available(pa)) {
   1996 		aprint_verbose(", 64-bit DMA");
   1997 		sc->sc_dmat = pa->pa_dmat64;
   1998 	} else {
   1999 		aprint_verbose(", 32-bit DMA");
   2000 		sc->sc_dmat = pa->pa_dmat;
   2001 	}
   2002 
   2003 	sc->sc_pcidevid = PCI_PRODUCT(pa->pa_id);
   2004 	sc->sc_rev = PCI_REVISION(pci_conf_read(pc, pa->pa_tag,PCI_CLASS_REG));
   2005 	pci_aprint_devinfo_fancy(pa, "Ethernet controller", wmp->wmp_name, 1);
   2006 
   2007 	sc->sc_type = wmp->wmp_type;
   2008 
   2009 	/* Set default function pointers */
   2010 	sc->phy.acquire = sc->nvm.acquire = wm_get_null;
   2011 	sc->phy.release = sc->nvm.release = wm_put_null;
   2012 	sc->phy.reset_delay_us = (sc->sc_type >= WM_T_82571) ? 100 : 10000;
   2013 
   2014 	if (sc->sc_type < WM_T_82543) {
   2015 		if (sc->sc_rev < 2) {
   2016 			aprint_error_dev(sc->sc_dev,
   2017 			    "i82542 must be at least rev. 2\n");
   2018 			return;
   2019 		}
   2020 		if (sc->sc_rev < 3)
   2021 			sc->sc_type = WM_T_82542_2_0;
   2022 	}
   2023 
   2024 	/*
   2025 	 * Disable MSI for Errata:
   2026 	 * "Message Signaled Interrupt Feature May Corrupt Write Transactions"
   2027 	 *
   2028 	 *  82544: Errata 25
   2029 	 *  82540: Errata  6 (easy to reproduce device timeout)
   2030 	 *  82545: Errata  4 (easy to reproduce device timeout)
   2031 	 *  82546: Errata 26 (easy to reproduce device timeout)
   2032 	 *  82541: Errata  7 (easy to reproduce device timeout)
   2033 	 *
   2034 	 * "Byte Enables 2 and 3 are not set on MSI writes"
   2035 	 *
   2036 	 *  82571 & 82572: Errata 63
   2037 	 */
   2038 	if ((sc->sc_type <= WM_T_82541_2) || (sc->sc_type == WM_T_82571)
   2039 	    || (sc->sc_type == WM_T_82572))
   2040 		pa->pa_flags &= ~PCI_FLAGS_MSI_OKAY;
   2041 
   2042 	if ((sc->sc_type == WM_T_82575) || (sc->sc_type == WM_T_82576)
   2043 	    || (sc->sc_type == WM_T_82580)
   2044 	    || (sc->sc_type == WM_T_I350) || (sc->sc_type == WM_T_I354)
   2045 	    || (sc->sc_type == WM_T_I210) || (sc->sc_type == WM_T_I211))
   2046 		sc->sc_flags |= WM_F_NEWQUEUE;
   2047 
   2048 	/* Set device properties (mactype) */
   2049 	dict = device_properties(sc->sc_dev);
   2050 	prop_dictionary_set_uint32(dict, "mactype", sc->sc_type);
   2051 
   2052 	/*
   2053 	 * Map the device.  All devices support memory-mapped acccess,
   2054 	 * and it is really required for normal operation.
   2055 	 */
   2056 	memtype = pci_mapreg_type(pa->pa_pc, pa->pa_tag, WM_PCI_MMBA);
   2057 	switch (memtype) {
   2058 	case PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT:
   2059 	case PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_64BIT:
   2060 		memh_valid = (pci_mapreg_map(pa, WM_PCI_MMBA,
   2061 			memtype, 0, &memt, &memh, NULL, &memsize) == 0);
   2062 		break;
   2063 	default:
   2064 		memh_valid = 0;
   2065 		break;
   2066 	}
   2067 
   2068 	if (memh_valid) {
   2069 		sc->sc_st = memt;
   2070 		sc->sc_sh = memh;
   2071 		sc->sc_ss = memsize;
   2072 	} else {
   2073 		aprint_error_dev(sc->sc_dev,
   2074 		    "unable to map device registers\n");
   2075 		return;
   2076 	}
   2077 
   2078 	/*
   2079 	 * In addition, i82544 and later support I/O mapped indirect
   2080 	 * register access.  It is not desirable (nor supported in
   2081 	 * this driver) to use it for normal operation, though it is
   2082 	 * required to work around bugs in some chip versions.
   2083 	 */
   2084 	switch (sc->sc_type) {
   2085 	case WM_T_82544:
   2086 	case WM_T_82541:
   2087 	case WM_T_82541_2:
   2088 	case WM_T_82547:
   2089 	case WM_T_82547_2:
   2090 		/* First we have to find the I/O BAR. */
   2091 		for (i = PCI_MAPREG_START; i < PCI_MAPREG_END; i += 4) {
   2092 			memtype = pci_mapreg_type(pa->pa_pc, pa->pa_tag, i);
   2093 			if (memtype == PCI_MAPREG_TYPE_IO)
   2094 				break;
   2095 			if (PCI_MAPREG_MEM_TYPE(memtype) ==
   2096 			    PCI_MAPREG_MEM_TYPE_64BIT)
   2097 				i += 4;	/* skip high bits, too */
   2098 		}
   2099 		if (i < PCI_MAPREG_END) {
   2100 			/*
   2101 			 * We found PCI_MAPREG_TYPE_IO. Note that 82580
   2102 			 * (and newer?) chip has no PCI_MAPREG_TYPE_IO.
   2103 			 * It's no problem because newer chips has no this
   2104 			 * bug.
   2105 			 *
   2106 			 * The i8254x doesn't apparently respond when the
   2107 			 * I/O BAR is 0, which looks somewhat like it's not
   2108 			 * been configured.
   2109 			 */
   2110 			preg = pci_conf_read(pc, pa->pa_tag, i);
   2111 			if (PCI_MAPREG_MEM_ADDR(preg) == 0) {
   2112 				aprint_error_dev(sc->sc_dev,
   2113 				    "WARNING: I/O BAR at zero.\n");
   2114 			} else if (pci_mapreg_map(pa, i, PCI_MAPREG_TYPE_IO,
   2115 			    0, &sc->sc_iot, &sc->sc_ioh, NULL, &sc->sc_ios)
   2116 			    == 0) {
   2117 				sc->sc_flags |= WM_F_IOH_VALID;
   2118 			} else
   2119 				aprint_error_dev(sc->sc_dev,
   2120 				    "WARNING: unable to map I/O space\n");
   2121 		}
   2122 		break;
   2123 	default:
   2124 		break;
   2125 	}
   2126 
   2127 	/* Enable bus mastering.  Disable MWI on the i82542 2.0. */
   2128 	preg = pci_conf_read(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
   2129 	preg |= PCI_COMMAND_MASTER_ENABLE;
   2130 	if (sc->sc_type < WM_T_82542_2_1)
   2131 		preg &= ~PCI_COMMAND_INVALIDATE_ENABLE;
   2132 	pci_conf_write(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG, preg);
   2133 
   2134 	/* Power up chip */
   2135 	if ((error = pci_activate(pa->pa_pc, pa->pa_tag, self, NULL))
   2136 	    && error != EOPNOTSUPP) {
   2137 		aprint_error_dev(sc->sc_dev, "cannot activate %d\n", error);
   2138 		return;
   2139 	}
   2140 
   2141 	wm_adjust_qnum(sc, pci_msix_count(pa->pa_pc, pa->pa_tag));
   2142 	/*
   2143 	 *  Don't use MSI-X if we can use only one queue to save interrupt
   2144 	 * resource.
   2145 	 */
   2146 	if (sc->sc_nqueues > 1) {
   2147 		max_type = PCI_INTR_TYPE_MSIX;
   2148 		/*
   2149 		 *  82583 has a MSI-X capability in the PCI configuration space
   2150 		 * but it doesn't support it. At least the document doesn't
   2151 		 * say anything about MSI-X.
   2152 		 */
   2153 		counts[PCI_INTR_TYPE_MSIX]
   2154 		    = (sc->sc_type == WM_T_82583) ? 0 : sc->sc_nqueues + 1;
   2155 	} else {
   2156 		max_type = PCI_INTR_TYPE_MSI;
   2157 		counts[PCI_INTR_TYPE_MSIX] = 0;
   2158 	}
   2159 
   2160 	/* Allocation settings */
   2161 	counts[PCI_INTR_TYPE_MSI] = 1;
   2162 	counts[PCI_INTR_TYPE_INTX] = 1;
   2163 	/* overridden by disable flags */
   2164 	if (wm_disable_msi != 0) {
   2165 		counts[PCI_INTR_TYPE_MSI] = 0;
   2166 		if (wm_disable_msix != 0) {
   2167 			max_type = PCI_INTR_TYPE_INTX;
   2168 			counts[PCI_INTR_TYPE_MSIX] = 0;
   2169 		}
   2170 	} else if (wm_disable_msix != 0) {
   2171 		max_type = PCI_INTR_TYPE_MSI;
   2172 		counts[PCI_INTR_TYPE_MSIX] = 0;
   2173 	}
   2174 
   2175 alloc_retry:
   2176 	if (pci_intr_alloc(pa, &sc->sc_intrs, counts, max_type) != 0) {
   2177 		aprint_error_dev(sc->sc_dev, "failed to allocate interrupt\n");
   2178 		return;
   2179 	}
   2180 
   2181 	if (pci_intr_type(pc, sc->sc_intrs[0]) == PCI_INTR_TYPE_MSIX) {
   2182 		error = wm_setup_msix(sc);
   2183 		if (error) {
   2184 			pci_intr_release(pc, sc->sc_intrs,
   2185 			    counts[PCI_INTR_TYPE_MSIX]);
   2186 
   2187 			/* Setup for MSI: Disable MSI-X */
   2188 			max_type = PCI_INTR_TYPE_MSI;
   2189 			counts[PCI_INTR_TYPE_MSI] = 1;
   2190 			counts[PCI_INTR_TYPE_INTX] = 1;
   2191 			goto alloc_retry;
   2192 		}
   2193 	} else if (pci_intr_type(pc, sc->sc_intrs[0]) == PCI_INTR_TYPE_MSI) {
   2194 		wm_adjust_qnum(sc, 0);	/* Must not use multiqueue */
   2195 		error = wm_setup_legacy(sc);
   2196 		if (error) {
   2197 			pci_intr_release(sc->sc_pc, sc->sc_intrs,
   2198 			    counts[PCI_INTR_TYPE_MSI]);
   2199 
   2200 			/* The next try is for INTx: Disable MSI */
   2201 			max_type = PCI_INTR_TYPE_INTX;
   2202 			counts[PCI_INTR_TYPE_INTX] = 1;
   2203 			goto alloc_retry;
   2204 		}
   2205 	} else {
   2206 		wm_adjust_qnum(sc, 0);	/* Must not use multiqueue */
   2207 		error = wm_setup_legacy(sc);
   2208 		if (error) {
   2209 			pci_intr_release(sc->sc_pc, sc->sc_intrs,
   2210 			    counts[PCI_INTR_TYPE_INTX]);
   2211 			return;
   2212 		}
   2213 	}
   2214 
   2215 	snprintf(wqname, sizeof(wqname), "%sTxRx", device_xname(sc->sc_dev));
   2216 	error = workqueue_create(&sc->sc_queue_wq, wqname,
   2217 	    wm_handle_queue_work, sc, WM_WORKQUEUE_PRI, IPL_NET,
   2218 	    WM_WORKQUEUE_FLAGS);
   2219 	if (error) {
   2220 		aprint_error_dev(sc->sc_dev,
   2221 		    "unable to create workqueue\n");
   2222 		goto out;
   2223 	}
   2224 
   2225 	/*
   2226 	 * Check the function ID (unit number of the chip).
   2227 	 */
   2228 	if ((sc->sc_type == WM_T_82546) || (sc->sc_type == WM_T_82546_3)
   2229 	    || (sc->sc_type == WM_T_82571) || (sc->sc_type == WM_T_80003)
   2230 	    || (sc->sc_type == WM_T_82575) || (sc->sc_type == WM_T_82576)
   2231 	    || (sc->sc_type == WM_T_82580)
   2232 	    || (sc->sc_type == WM_T_I350) || (sc->sc_type == WM_T_I354))
   2233 		sc->sc_funcid = (CSR_READ(sc, WMREG_STATUS)
   2234 		    >> STATUS_FUNCID_SHIFT) & STATUS_FUNCID_MASK;
   2235 	else
   2236 		sc->sc_funcid = 0;
   2237 
   2238 	/*
   2239 	 * Determine a few things about the bus we're connected to.
   2240 	 */
   2241 	if (sc->sc_type < WM_T_82543) {
   2242 		/* We don't really know the bus characteristics here. */
   2243 		sc->sc_bus_speed = 33;
   2244 	} else if (sc->sc_type == WM_T_82547 || sc->sc_type == WM_T_82547_2) {
   2245 		/*
   2246 		 * CSA (Communication Streaming Architecture) is about as fast
   2247 		 * a 32-bit 66MHz PCI Bus.
   2248 		 */
   2249 		sc->sc_flags |= WM_F_CSA;
   2250 		sc->sc_bus_speed = 66;
   2251 		aprint_verbose_dev(sc->sc_dev,
   2252 		    "Communication Streaming Architecture\n");
   2253 		if (sc->sc_type == WM_T_82547) {
   2254 			callout_init(&sc->sc_txfifo_ch, WM_CALLOUT_FLAGS);
   2255 			callout_setfunc(&sc->sc_txfifo_ch,
   2256 			    wm_82547_txfifo_stall, sc);
   2257 			aprint_verbose_dev(sc->sc_dev,
   2258 			    "using 82547 Tx FIFO stall work-around\n");
   2259 		}
   2260 	} else if (sc->sc_type >= WM_T_82571) {
   2261 		sc->sc_flags |= WM_F_PCIE;
   2262 		if ((sc->sc_type != WM_T_ICH8) && (sc->sc_type != WM_T_ICH9)
   2263 		    && (sc->sc_type != WM_T_ICH10)
   2264 		    && (sc->sc_type != WM_T_PCH)
   2265 		    && (sc->sc_type != WM_T_PCH2)
   2266 		    && (sc->sc_type != WM_T_PCH_LPT)
   2267 		    && (sc->sc_type != WM_T_PCH_SPT)
   2268 		    && (sc->sc_type != WM_T_PCH_CNP)) {
   2269 			/* ICH* and PCH* have no PCIe capability registers */
   2270 			if (pci_get_capability(pa->pa_pc, pa->pa_tag,
   2271 				PCI_CAP_PCIEXPRESS, &sc->sc_pcixe_capoff,
   2272 				NULL) == 0)
   2273 				aprint_error_dev(sc->sc_dev,
   2274 				    "unable to find PCIe capability\n");
   2275 		}
   2276 		aprint_verbose_dev(sc->sc_dev, "PCI-Express bus\n");
   2277 	} else {
   2278 		reg = CSR_READ(sc, WMREG_STATUS);
   2279 		if (reg & STATUS_BUS64)
   2280 			sc->sc_flags |= WM_F_BUS64;
   2281 		if ((reg & STATUS_PCIX_MODE) != 0) {
   2282 			pcireg_t pcix_cmd, pcix_sts, bytecnt, maxb;
   2283 
   2284 			sc->sc_flags |= WM_F_PCIX;
   2285 			if (pci_get_capability(pa->pa_pc, pa->pa_tag,
   2286 				PCI_CAP_PCIX, &sc->sc_pcixe_capoff, NULL) == 0)
   2287 				aprint_error_dev(sc->sc_dev,
   2288 				    "unable to find PCIX capability\n");
   2289 			else if (sc->sc_type != WM_T_82545_3 &&
   2290 				 sc->sc_type != WM_T_82546_3) {
   2291 				/*
   2292 				 * Work around a problem caused by the BIOS
   2293 				 * setting the max memory read byte count
   2294 				 * incorrectly.
   2295 				 */
   2296 				pcix_cmd = pci_conf_read(pa->pa_pc, pa->pa_tag,
   2297 				    sc->sc_pcixe_capoff + PCIX_CMD);
   2298 				pcix_sts = pci_conf_read(pa->pa_pc, pa->pa_tag,
   2299 				    sc->sc_pcixe_capoff + PCIX_STATUS);
   2300 
   2301 				bytecnt = (pcix_cmd & PCIX_CMD_BYTECNT_MASK) >>
   2302 				    PCIX_CMD_BYTECNT_SHIFT;
   2303 				maxb = (pcix_sts & PCIX_STATUS_MAXB_MASK) >>
   2304 				    PCIX_STATUS_MAXB_SHIFT;
   2305 				if (bytecnt > maxb) {
   2306 					aprint_verbose_dev(sc->sc_dev,
   2307 					    "resetting PCI-X MMRBC: %d -> %d\n",
   2308 					    512 << bytecnt, 512 << maxb);
   2309 					pcix_cmd = (pcix_cmd &
   2310 					    ~PCIX_CMD_BYTECNT_MASK) |
   2311 					    (maxb << PCIX_CMD_BYTECNT_SHIFT);
   2312 					pci_conf_write(pa->pa_pc, pa->pa_tag,
   2313 					    sc->sc_pcixe_capoff + PCIX_CMD,
   2314 					    pcix_cmd);
   2315 				}
   2316 			}
   2317 		}
   2318 		/*
   2319 		 * The quad port adapter is special; it has a PCIX-PCIX
   2320 		 * bridge on the board, and can run the secondary bus at
   2321 		 * a higher speed.
   2322 		 */
   2323 		if (wmp->wmp_product == PCI_PRODUCT_INTEL_82546EB_QUAD) {
   2324 			sc->sc_bus_speed = (sc->sc_flags & WM_F_PCIX) ? 120
   2325 								      : 66;
   2326 		} else if (sc->sc_flags & WM_F_PCIX) {
   2327 			switch (reg & STATUS_PCIXSPD_MASK) {
   2328 			case STATUS_PCIXSPD_50_66:
   2329 				sc->sc_bus_speed = 66;
   2330 				break;
   2331 			case STATUS_PCIXSPD_66_100:
   2332 				sc->sc_bus_speed = 100;
   2333 				break;
   2334 			case STATUS_PCIXSPD_100_133:
   2335 				sc->sc_bus_speed = 133;
   2336 				break;
   2337 			default:
   2338 				aprint_error_dev(sc->sc_dev,
   2339 				    "unknown PCIXSPD %d; assuming 66MHz\n",
   2340 				    reg & STATUS_PCIXSPD_MASK);
   2341 				sc->sc_bus_speed = 66;
   2342 				break;
   2343 			}
   2344 		} else
   2345 			sc->sc_bus_speed = (reg & STATUS_PCI66) ? 66 : 33;
   2346 		aprint_verbose_dev(sc->sc_dev, "%d-bit %dMHz %s bus\n",
   2347 		    (sc->sc_flags & WM_F_BUS64) ? 64 : 32, sc->sc_bus_speed,
   2348 		    (sc->sc_flags & WM_F_PCIX) ? "PCIX" : "PCI");
   2349 	}
   2350 
   2351 	/* clear interesting stat counters */
   2352 	CSR_READ(sc, WMREG_COLC);
   2353 	CSR_READ(sc, WMREG_RXERRC);
   2354 
   2355 	if ((sc->sc_type == WM_T_82574) || (sc->sc_type == WM_T_82583)
   2356 	    || (sc->sc_type >= WM_T_ICH8))
   2357 		sc->sc_ich_phymtx = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NET);
   2358 	if (sc->sc_type >= WM_T_ICH8)
   2359 		sc->sc_ich_nvmmtx = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NET);
   2360 
   2361 	/* Set PHY, NVM mutex related stuff */
   2362 	switch (sc->sc_type) {
   2363 	case WM_T_82542_2_0:
   2364 	case WM_T_82542_2_1:
   2365 	case WM_T_82543:
   2366 	case WM_T_82544:
   2367 		/* Microwire */
   2368 		sc->nvm.read = wm_nvm_read_uwire;
   2369 		sc->sc_nvm_wordsize = 64;
   2370 		sc->sc_nvm_addrbits = 6;
   2371 		break;
   2372 	case WM_T_82540:
   2373 	case WM_T_82545:
   2374 	case WM_T_82545_3:
   2375 	case WM_T_82546:
   2376 	case WM_T_82546_3:
   2377 		/* Microwire */
   2378 		sc->nvm.read = wm_nvm_read_uwire;
   2379 		reg = CSR_READ(sc, WMREG_EECD);
   2380 		if (reg & EECD_EE_SIZE) {
   2381 			sc->sc_nvm_wordsize = 256;
   2382 			sc->sc_nvm_addrbits = 8;
   2383 		} else {
   2384 			sc->sc_nvm_wordsize = 64;
   2385 			sc->sc_nvm_addrbits = 6;
   2386 		}
   2387 		sc->sc_flags |= WM_F_LOCK_EECD;
   2388 		sc->nvm.acquire = wm_get_eecd;
   2389 		sc->nvm.release = wm_put_eecd;
   2390 		break;
   2391 	case WM_T_82541:
   2392 	case WM_T_82541_2:
   2393 	case WM_T_82547:
   2394 	case WM_T_82547_2:
   2395 		reg = CSR_READ(sc, WMREG_EECD);
   2396 		/*
   2397 		 * wm_nvm_set_addrbits_size_eecd() accesses SPI in it only
   2398 		 * on 8254[17], so set flags and functios before calling it.
   2399 		 */
   2400 		sc->sc_flags |= WM_F_LOCK_EECD;
   2401 		sc->nvm.acquire = wm_get_eecd;
   2402 		sc->nvm.release = wm_put_eecd;
   2403 		if (reg & EECD_EE_TYPE) {
   2404 			/* SPI */
   2405 			sc->nvm.read = wm_nvm_read_spi;
   2406 			sc->sc_flags |= WM_F_EEPROM_SPI;
   2407 			wm_nvm_set_addrbits_size_eecd(sc);
   2408 		} else {
   2409 			/* Microwire */
   2410 			sc->nvm.read = wm_nvm_read_uwire;
   2411 			if ((reg & EECD_EE_ABITS) != 0) {
   2412 				sc->sc_nvm_wordsize = 256;
   2413 				sc->sc_nvm_addrbits = 8;
   2414 			} else {
   2415 				sc->sc_nvm_wordsize = 64;
   2416 				sc->sc_nvm_addrbits = 6;
   2417 			}
   2418 		}
   2419 		break;
   2420 	case WM_T_82571:
   2421 	case WM_T_82572:
   2422 		/* SPI */
   2423 		sc->nvm.read = wm_nvm_read_eerd;
   2424 		/* Not use WM_F_LOCK_EECD because we use EERD */
   2425 		sc->sc_flags |= WM_F_EEPROM_SPI;
   2426 		wm_nvm_set_addrbits_size_eecd(sc);
   2427 		sc->phy.acquire = wm_get_swsm_semaphore;
   2428 		sc->phy.release = wm_put_swsm_semaphore;
   2429 		sc->nvm.acquire = wm_get_nvm_82571;
   2430 		sc->nvm.release = wm_put_nvm_82571;
   2431 		break;
   2432 	case WM_T_82573:
   2433 	case WM_T_82574:
   2434 	case WM_T_82583:
   2435 		sc->nvm.read = wm_nvm_read_eerd;
   2436 		/* Not use WM_F_LOCK_EECD because we use EERD */
   2437 		if (sc->sc_type == WM_T_82573) {
   2438 			sc->phy.acquire = wm_get_swsm_semaphore;
   2439 			sc->phy.release = wm_put_swsm_semaphore;
   2440 			sc->nvm.acquire = wm_get_nvm_82571;
   2441 			sc->nvm.release = wm_put_nvm_82571;
   2442 		} else {
   2443 			/* Both PHY and NVM use the same semaphore. */
   2444 			sc->phy.acquire = sc->nvm.acquire
   2445 			    = wm_get_swfwhw_semaphore;
   2446 			sc->phy.release = sc->nvm.release
   2447 			    = wm_put_swfwhw_semaphore;
   2448 		}
   2449 		if (wm_nvm_is_onboard_eeprom(sc) == 0) {
   2450 			sc->sc_flags |= WM_F_EEPROM_FLASH;
   2451 			sc->sc_nvm_wordsize = 2048;
   2452 		} else {
   2453 			/* SPI */
   2454 			sc->sc_flags |= WM_F_EEPROM_SPI;
   2455 			wm_nvm_set_addrbits_size_eecd(sc);
   2456 		}
   2457 		break;
   2458 	case WM_T_82575:
   2459 	case WM_T_82576:
   2460 	case WM_T_82580:
   2461 	case WM_T_I350:
   2462 	case WM_T_I354:
   2463 	case WM_T_80003:
   2464 		/* SPI */
   2465 		sc->sc_flags |= WM_F_EEPROM_SPI;
   2466 		wm_nvm_set_addrbits_size_eecd(sc);
   2467 		if ((sc->sc_type == WM_T_80003)
   2468 		    || (sc->sc_nvm_wordsize < (1 << 15))) {
   2469 			sc->nvm.read = wm_nvm_read_eerd;
   2470 			/* Don't use WM_F_LOCK_EECD because we use EERD */
   2471 		} else {
   2472 			sc->nvm.read = wm_nvm_read_spi;
   2473 			sc->sc_flags |= WM_F_LOCK_EECD;
   2474 		}
   2475 		sc->phy.acquire = wm_get_phy_82575;
   2476 		sc->phy.release = wm_put_phy_82575;
   2477 		sc->nvm.acquire = wm_get_nvm_80003;
   2478 		sc->nvm.release = wm_put_nvm_80003;
   2479 		break;
   2480 	case WM_T_ICH8:
   2481 	case WM_T_ICH9:
   2482 	case WM_T_ICH10:
   2483 	case WM_T_PCH:
   2484 	case WM_T_PCH2:
   2485 	case WM_T_PCH_LPT:
   2486 		sc->nvm.read = wm_nvm_read_ich8;
   2487 		/* FLASH */
   2488 		sc->sc_flags |= WM_F_EEPROM_FLASH;
   2489 		sc->sc_nvm_wordsize = 2048;
   2490 		memtype = pci_mapreg_type(pa->pa_pc, pa->pa_tag,WM_ICH8_FLASH);
   2491 		if (pci_mapreg_map(pa, WM_ICH8_FLASH, memtype, 0,
   2492 		    &sc->sc_flasht, &sc->sc_flashh, NULL, &sc->sc_flashs)) {
   2493 			aprint_error_dev(sc->sc_dev,
   2494 			    "can't map FLASH registers\n");
   2495 			goto out;
   2496 		}
   2497 		reg = ICH8_FLASH_READ32(sc, ICH_FLASH_GFPREG);
   2498 		sc->sc_ich8_flash_base = (reg & ICH_GFPREG_BASE_MASK) *
   2499 		    ICH_FLASH_SECTOR_SIZE;
   2500 		sc->sc_ich8_flash_bank_size =
   2501 		    ((reg >> 16) & ICH_GFPREG_BASE_MASK) + 1;
   2502 		sc->sc_ich8_flash_bank_size -= (reg & ICH_GFPREG_BASE_MASK);
   2503 		sc->sc_ich8_flash_bank_size *= ICH_FLASH_SECTOR_SIZE;
   2504 		sc->sc_ich8_flash_bank_size /= 2 * sizeof(uint16_t);
   2505 		sc->sc_flashreg_offset = 0;
   2506 		sc->phy.acquire = wm_get_swflag_ich8lan;
   2507 		sc->phy.release = wm_put_swflag_ich8lan;
   2508 		sc->nvm.acquire = wm_get_nvm_ich8lan;
   2509 		sc->nvm.release = wm_put_nvm_ich8lan;
   2510 		break;
   2511 	case WM_T_PCH_SPT:
   2512 	case WM_T_PCH_CNP:
   2513 		sc->nvm.read = wm_nvm_read_spt;
   2514 		/* SPT has no GFPREG; flash registers mapped through BAR0 */
   2515 		sc->sc_flags |= WM_F_EEPROM_FLASH;
   2516 		sc->sc_flasht = sc->sc_st;
   2517 		sc->sc_flashh = sc->sc_sh;
   2518 		sc->sc_ich8_flash_base = 0;
   2519 		sc->sc_nvm_wordsize =
   2520 		    (((CSR_READ(sc, WMREG_STRAP) >> 1) & 0x1F) + 1)
   2521 		    * NVM_SIZE_MULTIPLIER;
   2522 		/* It is size in bytes, we want words */
   2523 		sc->sc_nvm_wordsize /= 2;
   2524 		/* Assume 2 banks */
   2525 		sc->sc_ich8_flash_bank_size = sc->sc_nvm_wordsize / 2;
   2526 		sc->sc_flashreg_offset = WM_PCH_SPT_FLASHOFFSET;
   2527 		sc->phy.acquire = wm_get_swflag_ich8lan;
   2528 		sc->phy.release = wm_put_swflag_ich8lan;
   2529 		sc->nvm.acquire = wm_get_nvm_ich8lan;
   2530 		sc->nvm.release = wm_put_nvm_ich8lan;
   2531 		break;
   2532 	case WM_T_I210:
   2533 	case WM_T_I211:
   2534 		/* Allow a single clear of the SW semaphore on I210 and newer*/
   2535 		sc->sc_flags |= WM_F_WA_I210_CLSEM;
   2536 		if (wm_nvm_flash_presence_i210(sc)) {
   2537 			sc->nvm.read = wm_nvm_read_eerd;
   2538 			/* Don't use WM_F_LOCK_EECD because we use EERD */
   2539 			sc->sc_flags |= WM_F_EEPROM_FLASH_HW;
   2540 			wm_nvm_set_addrbits_size_eecd(sc);
   2541 		} else {
   2542 			sc->nvm.read = wm_nvm_read_invm;
   2543 			sc->sc_flags |= WM_F_EEPROM_INVM;
   2544 			sc->sc_nvm_wordsize = INVM_SIZE;
   2545 		}
   2546 		sc->phy.acquire = wm_get_phy_82575;
   2547 		sc->phy.release = wm_put_phy_82575;
   2548 		sc->nvm.acquire = wm_get_nvm_80003;
   2549 		sc->nvm.release = wm_put_nvm_80003;
   2550 		break;
   2551 	default:
   2552 		break;
   2553 	}
   2554 
   2555 	/* Ensure the SMBI bit is clear before first NVM or PHY access */
   2556 	switch (sc->sc_type) {
   2557 	case WM_T_82571:
   2558 	case WM_T_82572:
   2559 		reg = CSR_READ(sc, WMREG_SWSM2);
   2560 		if ((reg & SWSM2_LOCK) == 0) {
   2561 			CSR_WRITE(sc, WMREG_SWSM2, reg | SWSM2_LOCK);
   2562 			force_clear_smbi = true;
   2563 		} else
   2564 			force_clear_smbi = false;
   2565 		break;
   2566 	case WM_T_82573:
   2567 	case WM_T_82574:
   2568 	case WM_T_82583:
   2569 		force_clear_smbi = true;
   2570 		break;
   2571 	default:
   2572 		force_clear_smbi = false;
   2573 		break;
   2574 	}
   2575 	if (force_clear_smbi) {
   2576 		reg = CSR_READ(sc, WMREG_SWSM);
   2577 		if ((reg & SWSM_SMBI) != 0)
   2578 			aprint_error_dev(sc->sc_dev,
   2579 			    "Please update the Bootagent\n");
   2580 		CSR_WRITE(sc, WMREG_SWSM, reg & ~SWSM_SMBI);
   2581 	}
   2582 
   2583 	/*
   2584 	 * Defer printing the EEPROM type until after verifying the checksum
   2585 	 * This allows the EEPROM type to be printed correctly in the case
   2586 	 * that no EEPROM is attached.
   2587 	 */
   2588 	/*
   2589 	 * Validate the EEPROM checksum. If the checksum fails, flag
   2590 	 * this for later, so we can fail future reads from the EEPROM.
   2591 	 */
   2592 	if (wm_nvm_validate_checksum(sc)) {
   2593 		/*
   2594 		 * Read twice again because some PCI-e parts fail the
   2595 		 * first check due to the link being in sleep state.
   2596 		 */
   2597 		if (wm_nvm_validate_checksum(sc))
   2598 			sc->sc_flags |= WM_F_EEPROM_INVALID;
   2599 	}
   2600 
   2601 	if (sc->sc_flags & WM_F_EEPROM_INVALID)
   2602 		aprint_verbose_dev(sc->sc_dev, "No EEPROM");
   2603 	else {
   2604 		aprint_verbose_dev(sc->sc_dev, "%u words ",
   2605 		    sc->sc_nvm_wordsize);
   2606 		if (sc->sc_flags & WM_F_EEPROM_INVM)
   2607 			aprint_verbose("iNVM");
   2608 		else if (sc->sc_flags & WM_F_EEPROM_FLASH_HW)
   2609 			aprint_verbose("FLASH(HW)");
   2610 		else if (sc->sc_flags & WM_F_EEPROM_FLASH)
   2611 			aprint_verbose("FLASH");
   2612 		else {
   2613 			if (sc->sc_flags & WM_F_EEPROM_SPI)
   2614 				eetype = "SPI";
   2615 			else
   2616 				eetype = "MicroWire";
   2617 			aprint_verbose("(%d address bits) %s EEPROM",
   2618 			    sc->sc_nvm_addrbits, eetype);
   2619 		}
   2620 	}
   2621 	wm_nvm_version(sc);
   2622 	aprint_verbose("\n");
   2623 
   2624 	/*
   2625 	 * XXX The first call of wm_gmii_setup_phytype. The result might be
   2626 	 * incorrect.
   2627 	 */
   2628 	wm_gmii_setup_phytype(sc, 0, 0);
   2629 
   2630 	/* Check for WM_F_WOL on some chips before wm_reset() */
   2631 	switch (sc->sc_type) {
   2632 	case WM_T_ICH8:
   2633 	case WM_T_ICH9:
   2634 	case WM_T_ICH10:
   2635 	case WM_T_PCH:
   2636 	case WM_T_PCH2:
   2637 	case WM_T_PCH_LPT:
   2638 	case WM_T_PCH_SPT:
   2639 	case WM_T_PCH_CNP:
   2640 		apme_mask = WUC_APME;
   2641 		eeprom_data = CSR_READ(sc, WMREG_WUC);
   2642 		if ((eeprom_data & apme_mask) != 0)
   2643 			sc->sc_flags |= WM_F_WOL;
   2644 		break;
   2645 	default:
   2646 		break;
   2647 	}
   2648 
   2649 	/* Reset the chip to a known state. */
   2650 	wm_reset(sc);
   2651 
   2652 	/*
   2653 	 * Check for I21[01] PLL workaround.
   2654 	 *
   2655 	 * Three cases:
   2656 	 * a) Chip is I211.
   2657 	 * b) Chip is I210 and it uses INVM (not FLASH).
   2658 	 * c) Chip is I210 (and it uses FLASH) and the NVM image version < 3.25
   2659 	 */
   2660 	if (sc->sc_type == WM_T_I211)
   2661 		sc->sc_flags |= WM_F_PLL_WA_I210;
   2662 	if (sc->sc_type == WM_T_I210) {
   2663 		if (!wm_nvm_flash_presence_i210(sc))
   2664 			sc->sc_flags |= WM_F_PLL_WA_I210;
   2665 		else if ((sc->sc_nvm_ver_major < 3)
   2666 		    || ((sc->sc_nvm_ver_major == 3)
   2667 			&& (sc->sc_nvm_ver_minor < 25))) {
   2668 			aprint_verbose_dev(sc->sc_dev,
   2669 			    "ROM image version %d.%d is older than 3.25\n",
   2670 			    sc->sc_nvm_ver_major, sc->sc_nvm_ver_minor);
   2671 			sc->sc_flags |= WM_F_PLL_WA_I210;
   2672 		}
   2673 	}
   2674 	if ((sc->sc_flags & WM_F_PLL_WA_I210) != 0)
   2675 		wm_pll_workaround_i210(sc);
   2676 
   2677 	wm_get_wakeup(sc);
   2678 
   2679 	/* Non-AMT based hardware can now take control from firmware */
   2680 	if ((sc->sc_flags & WM_F_HAS_AMT) == 0)
   2681 		wm_get_hw_control(sc);
   2682 
   2683 	/*
   2684 	 * Read the Ethernet address from the EEPROM, if not first found
   2685 	 * in device properties.
   2686 	 */
   2687 	ea = prop_dictionary_get(dict, "mac-address");
   2688 	if (ea != NULL) {
   2689 		KASSERT(prop_object_type(ea) == PROP_TYPE_DATA);
   2690 		KASSERT(prop_data_size(ea) == ETHER_ADDR_LEN);
   2691 		memcpy(enaddr, prop_data_value(ea), ETHER_ADDR_LEN);
   2692 	} else {
   2693 		if (wm_read_mac_addr(sc, enaddr) != 0) {
   2694 			aprint_error_dev(sc->sc_dev,
   2695 			    "unable to read Ethernet address\n");
   2696 			goto out;
   2697 		}
   2698 	}
   2699 
   2700 	aprint_normal_dev(sc->sc_dev, "Ethernet address %s\n",
   2701 	    ether_sprintf(enaddr));
   2702 
   2703 	/*
   2704 	 * Read the config info from the EEPROM, and set up various
   2705 	 * bits in the control registers based on their contents.
   2706 	 */
   2707 	pn = prop_dictionary_get(dict, "i82543-cfg1");
   2708 	if (pn != NULL) {
   2709 		KASSERT(prop_object_type(pn) == PROP_TYPE_NUMBER);
   2710 		cfg1 = (uint16_t) prop_number_signed_value(pn);
   2711 	} else {
   2712 		if (wm_nvm_read(sc, NVM_OFF_CFG1, 1, &cfg1)) {
   2713 			aprint_error_dev(sc->sc_dev, "unable to read CFG1\n");
   2714 			goto out;
   2715 		}
   2716 	}
   2717 
   2718 	pn = prop_dictionary_get(dict, "i82543-cfg2");
   2719 	if (pn != NULL) {
   2720 		KASSERT(prop_object_type(pn) == PROP_TYPE_NUMBER);
   2721 		cfg2 = (uint16_t) prop_number_signed_value(pn);
   2722 	} else {
   2723 		if (wm_nvm_read(sc, NVM_OFF_CFG2, 1, &cfg2)) {
   2724 			aprint_error_dev(sc->sc_dev, "unable to read CFG2\n");
   2725 			goto out;
   2726 		}
   2727 	}
   2728 
   2729 	/* check for WM_F_WOL */
   2730 	switch (sc->sc_type) {
   2731 	case WM_T_82542_2_0:
   2732 	case WM_T_82542_2_1:
   2733 	case WM_T_82543:
   2734 		/* dummy? */
   2735 		eeprom_data = 0;
   2736 		apme_mask = NVM_CFG3_APME;
   2737 		break;
   2738 	case WM_T_82544:
   2739 		apme_mask = NVM_CFG2_82544_APM_EN;
   2740 		eeprom_data = cfg2;
   2741 		break;
   2742 	case WM_T_82546:
   2743 	case WM_T_82546_3:
   2744 	case WM_T_82571:
   2745 	case WM_T_82572:
   2746 	case WM_T_82573:
   2747 	case WM_T_82574:
   2748 	case WM_T_82583:
   2749 	case WM_T_80003:
   2750 	case WM_T_82575:
   2751 	case WM_T_82576:
   2752 		apme_mask = NVM_CFG3_APME;
   2753 		wm_nvm_read(sc, (sc->sc_funcid == 1) ? NVM_OFF_CFG3_PORTB
   2754 		    : NVM_OFF_CFG3_PORTA, 1, &eeprom_data);
   2755 		break;
   2756 	case WM_T_82580:
   2757 	case WM_T_I350:
   2758 	case WM_T_I354:
   2759 	case WM_T_I210:
   2760 	case WM_T_I211:
   2761 		apme_mask = NVM_CFG3_APME;
   2762 		wm_nvm_read(sc,
   2763 		    NVM_OFF_LAN_FUNC_82580(sc->sc_funcid) + NVM_OFF_CFG3_PORTA,
   2764 		    1, &eeprom_data);
   2765 		break;
   2766 	case WM_T_ICH8:
   2767 	case WM_T_ICH9:
   2768 	case WM_T_ICH10:
   2769 	case WM_T_PCH:
   2770 	case WM_T_PCH2:
   2771 	case WM_T_PCH_LPT:
   2772 	case WM_T_PCH_SPT:
   2773 	case WM_T_PCH_CNP:
   2774 		/* Already checked before wm_reset () */
   2775 		apme_mask = eeprom_data = 0;
   2776 		break;
   2777 	default: /* XXX 82540 */
   2778 		apme_mask = NVM_CFG3_APME;
   2779 		wm_nvm_read(sc, NVM_OFF_CFG3_PORTA, 1, &eeprom_data);
   2780 		break;
   2781 	}
   2782 	/* Check for WM_F_WOL flag after the setting of the EEPROM stuff */
   2783 	if ((eeprom_data & apme_mask) != 0)
   2784 		sc->sc_flags |= WM_F_WOL;
   2785 
   2786 	/*
   2787 	 * We have the eeprom settings, now apply the special cases
   2788 	 * where the eeprom may be wrong or the board won't support
   2789 	 * wake on lan on a particular port
   2790 	 */
   2791 	switch (sc->sc_pcidevid) {
   2792 	case PCI_PRODUCT_INTEL_82546GB_PCIE:
   2793 		sc->sc_flags &= ~WM_F_WOL;
   2794 		break;
   2795 	case PCI_PRODUCT_INTEL_82546EB_FIBER:
   2796 	case PCI_PRODUCT_INTEL_82546GB_FIBER:
   2797 		/* Wake events only supported on port A for dual fiber
   2798 		 * regardless of eeprom setting */
   2799 		if (sc->sc_funcid == 1)
   2800 			sc->sc_flags &= ~WM_F_WOL;
   2801 		break;
   2802 	case PCI_PRODUCT_INTEL_82546GB_QUAD_COPPER_KSP3:
   2803 		/* If quad port adapter, disable WoL on all but port A */
   2804 		if (sc->sc_funcid != 0)
   2805 			sc->sc_flags &= ~WM_F_WOL;
   2806 		break;
   2807 	case PCI_PRODUCT_INTEL_82571EB_FIBER:
   2808 		/* Wake events only supported on port A for dual fiber
   2809 		 * regardless of eeprom setting */
   2810 		if (sc->sc_funcid == 1)
   2811 			sc->sc_flags &= ~WM_F_WOL;
   2812 		break;
   2813 	case PCI_PRODUCT_INTEL_82571EB_QUAD_COPPER:
   2814 	case PCI_PRODUCT_INTEL_82571EB_QUAD_FIBER:
   2815 	case PCI_PRODUCT_INTEL_82571GB_QUAD_COPPER:
   2816 		/* If quad port adapter, disable WoL on all but port A */
   2817 		if (sc->sc_funcid != 0)
   2818 			sc->sc_flags &= ~WM_F_WOL;
   2819 		break;
   2820 	}
   2821 
   2822 	if (sc->sc_type >= WM_T_82575) {
   2823 		if (wm_nvm_read(sc, NVM_OFF_COMPAT, 1, &nvmword) == 0) {
   2824 			aprint_debug_dev(sc->sc_dev, "COMPAT = %hx\n",
   2825 			    nvmword);
   2826 			if ((sc->sc_type == WM_T_82575) ||
   2827 			    (sc->sc_type == WM_T_82576)) {
   2828 				/* Check NVM for autonegotiation */
   2829 				if ((nvmword & NVM_COMPAT_SERDES_FORCE_MODE)
   2830 				    != 0)
   2831 					sc->sc_flags |= WM_F_PCS_DIS_AUTONEGO;
   2832 			}
   2833 			if ((sc->sc_type == WM_T_82575) ||
   2834 			    (sc->sc_type == WM_T_I350)) {
   2835 				if (nvmword & NVM_COMPAT_MAS_EN(sc->sc_funcid))
   2836 					sc->sc_flags |= WM_F_MAS;
   2837 			}
   2838 		}
   2839 	}
   2840 
   2841 	/*
   2842 	 * XXX need special handling for some multiple port cards
   2843 	 * to disable a paticular port.
   2844 	 */
   2845 
   2846 	if (sc->sc_type >= WM_T_82544) {
   2847 		pn = prop_dictionary_get(dict, "i82543-swdpin");
   2848 		if (pn != NULL) {
   2849 			KASSERT(prop_object_type(pn) == PROP_TYPE_NUMBER);
   2850 			swdpin = (uint16_t) prop_number_signed_value(pn);
   2851 		} else {
   2852 			if (wm_nvm_read(sc, NVM_OFF_SWDPIN, 1, &swdpin)) {
   2853 				aprint_error_dev(sc->sc_dev,
   2854 				    "unable to read SWDPIN\n");
   2855 				goto out;
   2856 			}
   2857 		}
   2858 	}
   2859 
   2860 	if (cfg1 & NVM_CFG1_ILOS)
   2861 		sc->sc_ctrl |= CTRL_ILOS;
   2862 
   2863 	/*
   2864 	 * XXX
   2865 	 * This code isn't correct because pin 2 and 3 are located
   2866 	 * in different position on newer chips. Check all datasheet.
   2867 	 *
   2868 	 * Until resolve this problem, check if a chip < 82580
   2869 	 */
   2870 	if (sc->sc_type <= WM_T_82580) {
   2871 		if (sc->sc_type >= WM_T_82544) {
   2872 			sc->sc_ctrl |=
   2873 			    ((swdpin >> NVM_SWDPIN_SWDPIO_SHIFT) & 0xf) <<
   2874 			    CTRL_SWDPIO_SHIFT;
   2875 			sc->sc_ctrl |=
   2876 			    ((swdpin >> NVM_SWDPIN_SWDPIN_SHIFT) & 0xf) <<
   2877 			    CTRL_SWDPINS_SHIFT;
   2878 		} else {
   2879 			sc->sc_ctrl |=
   2880 			    ((cfg1 >> NVM_CFG1_SWDPIO_SHIFT) & 0xf) <<
   2881 			    CTRL_SWDPIO_SHIFT;
   2882 		}
   2883 	}
   2884 
   2885 	if ((sc->sc_type >= WM_T_82580) && (sc->sc_type <= WM_T_I211)) {
   2886 		wm_nvm_read(sc,
   2887 		    NVM_OFF_LAN_FUNC_82580(sc->sc_funcid) + NVM_OFF_CFG3_PORTA,
   2888 		    1, &nvmword);
   2889 		if (nvmword & NVM_CFG3_ILOS)
   2890 			sc->sc_ctrl |= CTRL_ILOS;
   2891 	}
   2892 
   2893 #if 0
   2894 	if (sc->sc_type >= WM_T_82544) {
   2895 		if (cfg1 & NVM_CFG1_IPS0)
   2896 			sc->sc_ctrl_ext |= CTRL_EXT_IPS;
   2897 		if (cfg1 & NVM_CFG1_IPS1)
   2898 			sc->sc_ctrl_ext |= CTRL_EXT_IPS1;
   2899 		sc->sc_ctrl_ext |=
   2900 		    ((swdpin >> (NVM_SWDPIN_SWDPIO_SHIFT + 4)) & 0xd) <<
   2901 		    CTRL_EXT_SWDPIO_SHIFT;
   2902 		sc->sc_ctrl_ext |=
   2903 		    ((swdpin >> (NVM_SWDPIN_SWDPIN_SHIFT + 4)) & 0xd) <<
   2904 		    CTRL_EXT_SWDPINS_SHIFT;
   2905 	} else {
   2906 		sc->sc_ctrl_ext |=
   2907 		    ((cfg2 >> NVM_CFG2_SWDPIO_SHIFT) & 0xf) <<
   2908 		    CTRL_EXT_SWDPIO_SHIFT;
   2909 	}
   2910 #endif
   2911 
   2912 	CSR_WRITE(sc, WMREG_CTRL, sc->sc_ctrl);
   2913 #if 0
   2914 	CSR_WRITE(sc, WMREG_CTRL_EXT, sc->sc_ctrl_ext);
   2915 #endif
   2916 
   2917 	if (sc->sc_type == WM_T_PCH) {
   2918 		uint16_t val;
   2919 
   2920 		/* Save the NVM K1 bit setting */
   2921 		wm_nvm_read(sc, NVM_OFF_K1_CONFIG, 1, &val);
   2922 
   2923 		if ((val & NVM_K1_CONFIG_ENABLE) != 0)
   2924 			sc->sc_nvm_k1_enabled = 1;
   2925 		else
   2926 			sc->sc_nvm_k1_enabled = 0;
   2927 	}
   2928 
   2929 	/* Determine if we're GMII, TBI, SERDES or SGMII mode */
   2930 	if (sc->sc_type == WM_T_ICH8 || sc->sc_type == WM_T_ICH9
   2931 	    || sc->sc_type == WM_T_ICH10 || sc->sc_type == WM_T_PCH
   2932 	    || sc->sc_type == WM_T_PCH2 || sc->sc_type == WM_T_PCH_LPT
   2933 	    || sc->sc_type == WM_T_PCH_SPT || sc->sc_type == WM_T_PCH_CNP
   2934 	    || sc->sc_type == WM_T_82573
   2935 	    || sc->sc_type == WM_T_82574 || sc->sc_type == WM_T_82583) {
   2936 		/* Copper only */
   2937 	} else if ((sc->sc_type == WM_T_82575) || (sc->sc_type == WM_T_82576)
   2938 	    || (sc->sc_type ==WM_T_82580) || (sc->sc_type ==WM_T_I350)
   2939 	    || (sc->sc_type ==WM_T_I354) || (sc->sc_type ==WM_T_I210)
   2940 	    || (sc->sc_type ==WM_T_I211)) {
   2941 		reg = CSR_READ(sc, WMREG_CTRL_EXT);
   2942 		link_mode = reg & CTRL_EXT_LINK_MODE_MASK;
   2943 		switch (link_mode) {
   2944 		case CTRL_EXT_LINK_MODE_1000KX:
   2945 			aprint_normal_dev(sc->sc_dev, "1000KX\n");
   2946 			sc->sc_mediatype = WM_MEDIATYPE_SERDES;
   2947 			break;
   2948 		case CTRL_EXT_LINK_MODE_SGMII:
   2949 			if (wm_sgmii_uses_mdio(sc)) {
   2950 				aprint_normal_dev(sc->sc_dev,
   2951 				    "SGMII(MDIO)\n");
   2952 				sc->sc_flags |= WM_F_SGMII;
   2953 				sc->sc_mediatype = WM_MEDIATYPE_COPPER;
   2954 				break;
   2955 			}
   2956 			aprint_verbose_dev(sc->sc_dev, "SGMII(I2C)\n");
   2957 			/*FALLTHROUGH*/
   2958 		case CTRL_EXT_LINK_MODE_PCIE_SERDES:
   2959 			sc->sc_mediatype = wm_sfp_get_media_type(sc);
   2960 			if (sc->sc_mediatype == WM_MEDIATYPE_UNKNOWN) {
   2961 				if (link_mode
   2962 				    == CTRL_EXT_LINK_MODE_SGMII) {
   2963 					sc->sc_mediatype = WM_MEDIATYPE_COPPER;
   2964 					sc->sc_flags |= WM_F_SGMII;
   2965 					aprint_verbose_dev(sc->sc_dev,
   2966 					    "SGMII\n");
   2967 				} else {
   2968 					sc->sc_mediatype = WM_MEDIATYPE_SERDES;
   2969 					aprint_verbose_dev(sc->sc_dev,
   2970 					    "SERDES\n");
   2971 				}
   2972 				break;
   2973 			}
   2974 			if (sc->sc_mediatype == WM_MEDIATYPE_SERDES)
   2975 				aprint_normal_dev(sc->sc_dev, "SERDES(SFP)\n");
   2976 			else if (sc->sc_mediatype == WM_MEDIATYPE_COPPER) {
   2977 				aprint_normal_dev(sc->sc_dev, "SGMII(SFP)\n");
   2978 				sc->sc_flags |= WM_F_SGMII;
   2979 			}
   2980 			/* Do not change link mode for 100BaseFX */
   2981 			if (sc->sc_sfptype == SFF_SFP_ETH_FLAGS_100FX)
   2982 				break;
   2983 
   2984 			/* Change current link mode setting */
   2985 			reg &= ~CTRL_EXT_LINK_MODE_MASK;
   2986 			if (sc->sc_mediatype == WM_MEDIATYPE_COPPER)
   2987 				reg |= CTRL_EXT_LINK_MODE_SGMII;
   2988 			else
   2989 				reg |= CTRL_EXT_LINK_MODE_PCIE_SERDES;
   2990 			CSR_WRITE(sc, WMREG_CTRL_EXT, reg);
   2991 			break;
   2992 		case CTRL_EXT_LINK_MODE_GMII:
   2993 		default:
   2994 			aprint_normal_dev(sc->sc_dev, "Copper\n");
   2995 			sc->sc_mediatype = WM_MEDIATYPE_COPPER;
   2996 			break;
   2997 		}
   2998 
   2999 		reg &= ~CTRL_EXT_I2C_ENA;
   3000 		if ((sc->sc_flags & WM_F_SGMII) != 0)
   3001 			reg |= CTRL_EXT_I2C_ENA;
   3002 		else
   3003 			reg &= ~CTRL_EXT_I2C_ENA;
   3004 		CSR_WRITE(sc, WMREG_CTRL_EXT, reg);
   3005 		if ((sc->sc_flags & WM_F_SGMII) != 0) {
   3006 			if (!wm_sgmii_uses_mdio(sc))
   3007 				wm_gmii_setup_phytype(sc, 0, 0);
   3008 			wm_reset_mdicnfg_82580(sc);
   3009 		}
   3010 	} else if (sc->sc_type < WM_T_82543 ||
   3011 	    (CSR_READ(sc, WMREG_STATUS) & STATUS_TBIMODE) != 0) {
   3012 		if (sc->sc_mediatype == WM_MEDIATYPE_COPPER) {
   3013 			aprint_error_dev(sc->sc_dev,
   3014 			    "WARNING: TBIMODE set on 1000BASE-T product!\n");
   3015 			sc->sc_mediatype = WM_MEDIATYPE_FIBER;
   3016 		}
   3017 	} else {
   3018 		if (sc->sc_mediatype == WM_MEDIATYPE_FIBER) {
   3019 			aprint_error_dev(sc->sc_dev,
   3020 			    "WARNING: TBIMODE clear on 1000BASE-X product!\n");
   3021 			sc->sc_mediatype = WM_MEDIATYPE_COPPER;
   3022 		}
   3023 	}
   3024 
   3025 	if (sc->sc_type >= WM_T_PCH2)
   3026 		sc->sc_flags |= WM_F_EEE;
   3027 	else if ((sc->sc_type >= WM_T_I350) && (sc->sc_type <= WM_T_I211)
   3028 	    && (sc->sc_mediatype == WM_MEDIATYPE_COPPER)) {
   3029 		/* XXX: Need special handling for I354. (not yet) */
   3030 		if (sc->sc_type != WM_T_I354)
   3031 			sc->sc_flags |= WM_F_EEE;
   3032 	}
   3033 
   3034 	/*
   3035 	 * The I350 has a bug where it always strips the CRC whether
   3036 	 * asked to or not. So ask for stripped CRC here and cope in rxeof
   3037 	 */
   3038 	if ((sc->sc_type == WM_T_I350) || (sc->sc_type == WM_T_I354)
   3039 	    || (sc->sc_type == WM_T_I210) || (sc->sc_type == WM_T_I211))
   3040 		sc->sc_flags |= WM_F_CRC_STRIP;
   3041 
   3042 	/* Set device properties (macflags) */
   3043 	prop_dictionary_set_uint32(dict, "macflags", sc->sc_flags);
   3044 
   3045 	if (sc->sc_flags != 0) {
   3046 		snprintb(buf, sizeof(buf), WM_FLAGS, sc->sc_flags);
   3047 		aprint_verbose_dev(sc->sc_dev, "%s\n", buf);
   3048 	}
   3049 
   3050 #ifdef WM_MPSAFE
   3051 	sc->sc_core_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NET);
   3052 #else
   3053 	sc->sc_core_lock = NULL;
   3054 #endif
   3055 
   3056 	/* Initialize the media structures accordingly. */
   3057 	if (sc->sc_mediatype == WM_MEDIATYPE_COPPER)
   3058 		wm_gmii_mediainit(sc, wmp->wmp_product);
   3059 	else
   3060 		wm_tbi_mediainit(sc); /* All others */
   3061 
   3062 	ifp = &sc->sc_ethercom.ec_if;
   3063 	xname = device_xname(sc->sc_dev);
   3064 	strlcpy(ifp->if_xname, xname, IFNAMSIZ);
   3065 	ifp->if_softc = sc;
   3066 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
   3067 #ifdef WM_MPSAFE
   3068 	ifp->if_extflags = IFEF_MPSAFE;
   3069 #endif
   3070 	ifp->if_ioctl = wm_ioctl;
   3071 	if ((sc->sc_flags & WM_F_NEWQUEUE) != 0) {
   3072 		ifp->if_start = wm_nq_start;
   3073 		/*
   3074 		 * When the number of CPUs is one and the controller can use
   3075 		 * MSI-X, wm(4) use MSI-X but *does not* use multiqueue.
   3076 		 * That is, wm(4) use two interrupts, one is used for Tx/Rx
   3077 		 * and the other is used for link status changing.
   3078 		 * In this situation, wm_nq_transmit() is disadvantageous
   3079 		 * because of wm_select_txqueue() and pcq(9) overhead.
   3080 		 */
   3081 		if (wm_is_using_multiqueue(sc))
   3082 			ifp->if_transmit = wm_nq_transmit;
   3083 	} else {
   3084 		ifp->if_start = wm_start;
   3085 		/*
   3086 		 * wm_transmit() has the same disadvantages as wm_nq_transmit()
   3087 		 * described above.
   3088 		 */
   3089 		if (wm_is_using_multiqueue(sc))
   3090 			ifp->if_transmit = wm_transmit;
   3091 	}
   3092 	/* wm(4) doest not use ifp->if_watchdog, use wm_tick as watchdog. */
   3093 	ifp->if_init = wm_init;
   3094 	ifp->if_stop = wm_stop;
   3095 	IFQ_SET_MAXLEN(&ifp->if_snd, uimax(WM_IFQUEUELEN, IFQ_MAXLEN));
   3096 	IFQ_SET_READY(&ifp->if_snd);
   3097 
   3098 	/* Check for jumbo frame */
   3099 	switch (sc->sc_type) {
   3100 	case WM_T_82573:
   3101 		/* XXX limited to 9234 if ASPM is disabled */
   3102 		wm_nvm_read(sc, NVM_OFF_INIT_3GIO_3, 1, &nvmword);
   3103 		if ((nvmword & NVM_3GIO_3_ASPM_MASK) != 0)
   3104 			sc->sc_ethercom.ec_capabilities |= ETHERCAP_JUMBO_MTU;
   3105 		break;
   3106 	case WM_T_82571:
   3107 	case WM_T_82572:
   3108 	case WM_T_82574:
   3109 	case WM_T_82583:
   3110 	case WM_T_82575:
   3111 	case WM_T_82576:
   3112 	case WM_T_82580:
   3113 	case WM_T_I350:
   3114 	case WM_T_I354:
   3115 	case WM_T_I210:
   3116 	case WM_T_I211:
   3117 	case WM_T_80003:
   3118 	case WM_T_ICH9:
   3119 	case WM_T_ICH10:
   3120 	case WM_T_PCH2:	/* PCH2 supports 9K frame size */
   3121 	case WM_T_PCH_LPT:
   3122 	case WM_T_PCH_SPT:
   3123 	case WM_T_PCH_CNP:
   3124 		/* XXX limited to 9234 */
   3125 		sc->sc_ethercom.ec_capabilities |= ETHERCAP_JUMBO_MTU;
   3126 		break;
   3127 	case WM_T_PCH:
   3128 		/* XXX limited to 4096 */
   3129 		sc->sc_ethercom.ec_capabilities |= ETHERCAP_JUMBO_MTU;
   3130 		break;
   3131 	case WM_T_82542_2_0:
   3132 	case WM_T_82542_2_1:
   3133 	case WM_T_ICH8:
   3134 		/* No support for jumbo frame */
   3135 		break;
   3136 	default:
   3137 		/* ETHER_MAX_LEN_JUMBO */
   3138 		sc->sc_ethercom.ec_capabilities |= ETHERCAP_JUMBO_MTU;
   3139 		break;
   3140 	}
   3141 
   3142 	/* If we're a i82543 or greater, we can support VLANs. */
   3143 	if (sc->sc_type >= WM_T_82543) {
   3144 		sc->sc_ethercom.ec_capabilities |=
   3145 		    ETHERCAP_VLAN_MTU | ETHERCAP_VLAN_HWTAGGING;
   3146 		sc->sc_ethercom.ec_capenable |= ETHERCAP_VLAN_HWTAGGING;
   3147 	}
   3148 
   3149 	if ((sc->sc_flags & WM_F_EEE) != 0)
   3150 		sc->sc_ethercom.ec_capabilities |= ETHERCAP_EEE;
   3151 
   3152 	/*
   3153 	 * We can perform TCPv4 and UDPv4 checksums in-bound.  Only
   3154 	 * on i82543 and later.
   3155 	 */
   3156 	if (sc->sc_type >= WM_T_82543) {
   3157 		ifp->if_capabilities |=
   3158 		    IFCAP_CSUM_IPv4_Tx | IFCAP_CSUM_IPv4_Rx |
   3159 		    IFCAP_CSUM_TCPv4_Tx | IFCAP_CSUM_TCPv4_Rx |
   3160 		    IFCAP_CSUM_UDPv4_Tx | IFCAP_CSUM_UDPv4_Rx |
   3161 		    IFCAP_CSUM_TCPv6_Tx |
   3162 		    IFCAP_CSUM_UDPv6_Tx;
   3163 	}
   3164 
   3165 	/*
   3166 	 * XXXyamt: i'm not sure which chips support RXCSUM_IPV6OFL.
   3167 	 *
   3168 	 *	82541GI (8086:1076) ... no
   3169 	 *	82572EI (8086:10b9) ... yes
   3170 	 */
   3171 	if (sc->sc_type >= WM_T_82571) {
   3172 		ifp->if_capabilities |=
   3173 		    IFCAP_CSUM_TCPv6_Rx | IFCAP_CSUM_UDPv6_Rx;
   3174 	}
   3175 
   3176 	/*
   3177 	 * If we're a i82544 or greater (except i82547), we can do
   3178 	 * TCP segmentation offload.
   3179 	 */
   3180 	if (sc->sc_type >= WM_T_82544 && sc->sc_type != WM_T_82547)
   3181 		ifp->if_capabilities |= IFCAP_TSOv4;
   3182 
   3183 	if (sc->sc_type >= WM_T_82571)
   3184 		ifp->if_capabilities |= IFCAP_TSOv6;
   3185 
   3186 	sc->sc_tx_process_limit = WM_TX_PROCESS_LIMIT_DEFAULT;
   3187 	sc->sc_tx_intr_process_limit = WM_TX_INTR_PROCESS_LIMIT_DEFAULT;
   3188 	sc->sc_rx_process_limit = WM_RX_PROCESS_LIMIT_DEFAULT;
   3189 	sc->sc_rx_intr_process_limit = WM_RX_INTR_PROCESS_LIMIT_DEFAULT;
   3190 
   3191 	/* Attach the interface. */
   3192 	if_initialize(ifp);
   3193 	sc->sc_ipq = if_percpuq_create(&sc->sc_ethercom.ec_if);
   3194 	ether_ifattach(ifp, enaddr);
   3195 	ether_set_ifflags_cb(&sc->sc_ethercom, wm_ifflags_cb);
   3196 	if_register(ifp);
   3197 	rnd_attach_source(&sc->rnd_source, xname, RND_TYPE_NET,
   3198 	    RND_FLAG_DEFAULT);
   3199 
   3200 #ifdef WM_EVENT_COUNTERS
   3201 	/* Attach event counters. */
   3202 	evcnt_attach_dynamic(&sc->sc_ev_linkintr, EVCNT_TYPE_INTR,
   3203 	    NULL, xname, "linkintr");
   3204 
   3205 	if (sc->sc_type >= WM_T_82542_2_1) {
   3206 		evcnt_attach_dynamic(&sc->sc_ev_tx_xoff, EVCNT_TYPE_MISC,
   3207 		    NULL, xname, "tx_xoff");
   3208 		evcnt_attach_dynamic(&sc->sc_ev_tx_xon, EVCNT_TYPE_MISC,
   3209 		    NULL, xname, "tx_xon");
   3210 		evcnt_attach_dynamic(&sc->sc_ev_rx_xoff, EVCNT_TYPE_MISC,
   3211 		    NULL, xname, "rx_xoff");
   3212 		evcnt_attach_dynamic(&sc->sc_ev_rx_xon, EVCNT_TYPE_MISC,
   3213 		    NULL, xname, "rx_xon");
   3214 		evcnt_attach_dynamic(&sc->sc_ev_rx_macctl, EVCNT_TYPE_MISC,
   3215 		    NULL, xname, "rx_macctl");
   3216 	}
   3217 
   3218 	evcnt_attach_dynamic(&sc->sc_ev_crcerrs, EVCNT_TYPE_MISC,
   3219 	    NULL, xname, "CRC Error");
   3220 	evcnt_attach_dynamic(&sc->sc_ev_symerrc, EVCNT_TYPE_MISC,
   3221 	    NULL, xname, "Symbol Error");
   3222 
   3223 	if (sc->sc_type >= WM_T_82543) {
   3224 		evcnt_attach_dynamic(&sc->sc_ev_algnerrc, EVCNT_TYPE_MISC,
   3225 		    NULL, xname, "Alignment Error");
   3226 		evcnt_attach_dynamic(&sc->sc_ev_rxerrc, EVCNT_TYPE_MISC,
   3227 		    NULL, xname, "Receive Error");
   3228 		evcnt_attach_dynamic(&sc->sc_ev_cexterr, EVCNT_TYPE_MISC,
   3229 		    NULL, xname, "Carrier Extension Error");
   3230 	}
   3231 
   3232 	evcnt_attach_dynamic(&sc->sc_ev_mpc, EVCNT_TYPE_MISC,
   3233 	    NULL, xname, "Missed Packets");
   3234 	evcnt_attach_dynamic(&sc->sc_ev_colc, EVCNT_TYPE_MISC,
   3235 	    NULL, xname, "Collision");
   3236 	evcnt_attach_dynamic(&sc->sc_ev_sec, EVCNT_TYPE_MISC,
   3237 	    NULL, xname, "Sequence Error");
   3238 	evcnt_attach_dynamic(&sc->sc_ev_rlec, EVCNT_TYPE_MISC,
   3239 	    NULL, xname, "Receive Length Error");
   3240 	evcnt_attach_dynamic(&sc->sc_ev_scc, EVCNT_TYPE_MISC,
   3241 	    NULL, xname, "Single Collision");
   3242 	evcnt_attach_dynamic(&sc->sc_ev_ecol, EVCNT_TYPE_MISC,
   3243 	    NULL, xname, "Excessive Collisions");
   3244 	evcnt_attach_dynamic(&sc->sc_ev_mcc, EVCNT_TYPE_MISC,
   3245 	    NULL, xname, "Multiple Collision");
   3246 	evcnt_attach_dynamic(&sc->sc_ev_latecol, EVCNT_TYPE_MISC,
   3247 	    NULL, xname, "Late Collisions");
   3248 	evcnt_attach_dynamic(&sc->sc_ev_dc, EVCNT_TYPE_MISC,
   3249 	    NULL, xname, "Defer");
   3250 	evcnt_attach_dynamic(&sc->sc_ev_gprc, EVCNT_TYPE_MISC,
   3251 	    NULL, xname, "Good Packets Rx");
   3252 	evcnt_attach_dynamic(&sc->sc_ev_bprc, EVCNT_TYPE_MISC,
   3253 	    NULL, xname, "Broadcast Packets Rx");
   3254 	evcnt_attach_dynamic(&sc->sc_ev_mprc, EVCNT_TYPE_MISC,
   3255 	    NULL, xname, "Multicast Packets Rx");
   3256 	evcnt_attach_dynamic(&sc->sc_ev_gptc, EVCNT_TYPE_MISC,
   3257 	    NULL, xname, "Good Packets Tx");
   3258 	evcnt_attach_dynamic(&sc->sc_ev_gorc, EVCNT_TYPE_MISC,
   3259 	    NULL, xname, "Good Octets Rx");
   3260 	evcnt_attach_dynamic(&sc->sc_ev_gotc, EVCNT_TYPE_MISC,
   3261 	    NULL, xname, "Good Octets Tx");
   3262 	evcnt_attach_dynamic(&sc->sc_ev_rnbc, EVCNT_TYPE_MISC,
   3263 	    NULL, xname, "Rx No Buffers");
   3264 	evcnt_attach_dynamic(&sc->sc_ev_ruc, EVCNT_TYPE_MISC,
   3265 	    NULL, xname, "Rx Undersize");
   3266 	evcnt_attach_dynamic(&sc->sc_ev_rfc, EVCNT_TYPE_MISC,
   3267 	    NULL, xname, "Rx Fragment");
   3268 	evcnt_attach_dynamic(&sc->sc_ev_roc, EVCNT_TYPE_MISC,
   3269 	    NULL, xname, "Rx Oversize");
   3270 	evcnt_attach_dynamic(&sc->sc_ev_rjc, EVCNT_TYPE_MISC,
   3271 	    NULL, xname, "Rx Jabber");
   3272 	evcnt_attach_dynamic(&sc->sc_ev_tor, EVCNT_TYPE_MISC,
   3273 	    NULL, xname, "Total Octets Rx");
   3274 	evcnt_attach_dynamic(&sc->sc_ev_tot, EVCNT_TYPE_MISC,
   3275 	    NULL, xname, "Total Octets Tx");
   3276 	evcnt_attach_dynamic(&sc->sc_ev_tpr, EVCNT_TYPE_MISC,
   3277 	    NULL, xname, "Total Packets Rx");
   3278 	evcnt_attach_dynamic(&sc->sc_ev_tpt, EVCNT_TYPE_MISC,
   3279 	    NULL, xname, "Total Packets Tx");
   3280 	evcnt_attach_dynamic(&sc->sc_ev_mptc, EVCNT_TYPE_MISC,
   3281 	    NULL, xname, "Multicast Packets Tx");
   3282 	evcnt_attach_dynamic(&sc->sc_ev_bptc, EVCNT_TYPE_MISC,
   3283 	    NULL, xname, "Broadcast Packets Tx Count");
   3284 	evcnt_attach_dynamic(&sc->sc_ev_prc64, EVCNT_TYPE_MISC,
   3285 	    NULL, xname, "Packets Rx (64 bytes)");
   3286 	evcnt_attach_dynamic(&sc->sc_ev_prc127, EVCNT_TYPE_MISC,
   3287 	    NULL, xname, "Packets Rx (65-127 bytes)");
   3288 	evcnt_attach_dynamic(&sc->sc_ev_prc255, EVCNT_TYPE_MISC,
   3289 	    NULL, xname, "Packets Rx (128-255 bytes)");
   3290 	evcnt_attach_dynamic(&sc->sc_ev_prc511, EVCNT_TYPE_MISC,
   3291 	    NULL, xname, "Packets Rx (255-511 bytes)");
   3292 	evcnt_attach_dynamic(&sc->sc_ev_prc1023, EVCNT_TYPE_MISC,
   3293 	    NULL, xname, "Packets Rx (512-1023 bytes)");
   3294 	evcnt_attach_dynamic(&sc->sc_ev_prc1522, EVCNT_TYPE_MISC,
   3295 	    NULL, xname, "Packets Rx (1024-1522 bytes)");
   3296 	evcnt_attach_dynamic(&sc->sc_ev_ptc64, EVCNT_TYPE_MISC,
   3297 	    NULL, xname, "Packets Tx (64 bytes)");
   3298 	evcnt_attach_dynamic(&sc->sc_ev_ptc127, EVCNT_TYPE_MISC,
   3299 	    NULL, xname, "Packets Tx (65-127 bytes)");
   3300 	evcnt_attach_dynamic(&sc->sc_ev_ptc255, EVCNT_TYPE_MISC,
   3301 	    NULL, xname, "Packets Tx (128-255 bytes)");
   3302 	evcnt_attach_dynamic(&sc->sc_ev_ptc511, EVCNT_TYPE_MISC,
   3303 	    NULL, xname, "Packets Tx (256-511 bytes)");
   3304 	evcnt_attach_dynamic(&sc->sc_ev_ptc1023, EVCNT_TYPE_MISC,
   3305 	    NULL, xname, "Packets Tx (512-1023 bytes)");
   3306 	evcnt_attach_dynamic(&sc->sc_ev_ptc1522, EVCNT_TYPE_MISC,
   3307 	    NULL, xname, "Packets Tx (1024-1522 Bytes)");
   3308 	evcnt_attach_dynamic(&sc->sc_ev_iac, EVCNT_TYPE_MISC,
   3309 	    NULL, xname, "Interrupt Assertion");
   3310 	evcnt_attach_dynamic(&sc->sc_ev_icrxptc, EVCNT_TYPE_MISC,
   3311 	    NULL, xname, "Intr. Cause Rx Pkt Timer Expire");
   3312 	evcnt_attach_dynamic(&sc->sc_ev_icrxatc, EVCNT_TYPE_MISC,
   3313 	    NULL, xname, "Intr. Cause Rx Abs Timer Expire");
   3314 	evcnt_attach_dynamic(&sc->sc_ev_ictxptc, EVCNT_TYPE_MISC,
   3315 	    NULL, xname, "Intr. Cause Tx Pkt Timer Expire");
   3316 	evcnt_attach_dynamic(&sc->sc_ev_ictxact, EVCNT_TYPE_MISC,
   3317 	    NULL, xname, "Intr. Cause Tx Abs Timer Expire");
   3318 	evcnt_attach_dynamic(&sc->sc_ev_ictxqec, EVCNT_TYPE_MISC,
   3319 	    NULL, xname, "Intr. Cause Tx Queue Empty");
   3320 	evcnt_attach_dynamic(&sc->sc_ev_ictxqmtc, EVCNT_TYPE_MISC,
   3321 	    NULL, xname, "Intr. Cause Tx Queue Min Thresh");
   3322 	evcnt_attach_dynamic(&sc->sc_ev_icrxdmtc, EVCNT_TYPE_MISC,
   3323 	    NULL, xname, "Intr. Cause Rx Desc Min Thresh");
   3324 	evcnt_attach_dynamic(&sc->sc_ev_icrxoc, EVCNT_TYPE_MISC,
   3325 	    NULL, xname, "Interrupt Cause Receiver Overrun");
   3326 	if (sc->sc_type >= WM_T_82543) {
   3327 		evcnt_attach_dynamic(&sc->sc_ev_tncrs, EVCNT_TYPE_MISC,
   3328 		    NULL, xname, "Tx with No CRS");
   3329 		evcnt_attach_dynamic(&sc->sc_ev_tsctc, EVCNT_TYPE_MISC,
   3330 		    NULL, xname, "TCP Segmentation Context Tx");
   3331 		evcnt_attach_dynamic(&sc->sc_ev_tsctfc, EVCNT_TYPE_MISC,
   3332 		    NULL, xname, "TCP Segmentation Context Tx Fail");
   3333 	}
   3334 	if (sc->sc_type >= WM_T_82540) {
   3335 		evcnt_attach_dynamic(&sc->sc_ev_mgtprc, EVCNT_TYPE_MISC,
   3336 		    NULL, xname, "Management Packets RX");
   3337 		evcnt_attach_dynamic(&sc->sc_ev_mgtpdc, EVCNT_TYPE_MISC,
   3338 		    NULL, xname, "Management Packets Dropped");
   3339 		evcnt_attach_dynamic(&sc->sc_ev_mgtptc, EVCNT_TYPE_MISC,
   3340 		    NULL, xname, "Management Packets TX");
   3341 	}
   3342 	if ((sc->sc_type >= WM_T_I350) && (sc->sc_type < WM_T_80003)) {
   3343 		evcnt_attach_dynamic(&sc->sc_ev_b2ogprc, EVCNT_TYPE_MISC,
   3344 		    NULL, xname, "BMC2OS Packets received by host");
   3345 		evcnt_attach_dynamic(&sc->sc_ev_o2bspc, EVCNT_TYPE_MISC,
   3346 		    NULL, xname, "OS2BMC Packets transmitted by host");
   3347 		evcnt_attach_dynamic(&sc->sc_ev_b2ospc, EVCNT_TYPE_MISC,
   3348 		    NULL, xname, "BMC2OS Packets sent by BMC");
   3349 		evcnt_attach_dynamic(&sc->sc_ev_o2bgptc, EVCNT_TYPE_MISC,
   3350 		    NULL, xname, "OS2BMC Packets received by BMC");
   3351 	}
   3352 #endif /* WM_EVENT_COUNTERS */
   3353 
   3354 	sc->sc_txrx_use_workqueue = false;
   3355 
   3356 	if (wm_phy_need_linkdown_discard(sc)) {
   3357 		DPRINTF(sc, WM_DEBUG_LINK,
   3358 		    ("%s: %s: Set linkdown discard flag\n",
   3359 			device_xname(sc->sc_dev), __func__));
   3360 		wm_set_linkdown_discard(sc);
   3361 	}
   3362 
   3363 	wm_init_sysctls(sc);
   3364 
   3365 	if (pmf_device_register(self, wm_suspend, wm_resume))
   3366 		pmf_class_network_register(self, ifp);
   3367 	else
   3368 		aprint_error_dev(self, "couldn't establish power handler\n");
   3369 
   3370 	sc->sc_flags |= WM_F_ATTACHED;
   3371 out:
   3372 	return;
   3373 }
   3374 
   3375 /* The detach function (ca_detach) */
   3376 static int
   3377 wm_detach(device_t self, int flags __unused)
   3378 {
   3379 	struct wm_softc *sc = device_private(self);
   3380 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   3381 	int i;
   3382 
   3383 	if ((sc->sc_flags & WM_F_ATTACHED) == 0)
   3384 		return 0;
   3385 
   3386 	/* Stop the interface. Callouts are stopped in it. */
   3387 	wm_stop(ifp, 1);
   3388 
   3389 	pmf_device_deregister(self);
   3390 
   3391 	sysctl_teardown(&sc->sc_sysctllog);
   3392 
   3393 #ifdef WM_EVENT_COUNTERS
   3394 	evcnt_detach(&sc->sc_ev_linkintr);
   3395 
   3396 	if (sc->sc_type >= WM_T_82542_2_1) {
   3397 		evcnt_detach(&sc->sc_ev_tx_xoff);
   3398 		evcnt_detach(&sc->sc_ev_tx_xon);
   3399 		evcnt_detach(&sc->sc_ev_rx_xoff);
   3400 		evcnt_detach(&sc->sc_ev_rx_xon);
   3401 		evcnt_detach(&sc->sc_ev_rx_macctl);
   3402 	}
   3403 
   3404 	evcnt_detach(&sc->sc_ev_crcerrs);
   3405 	evcnt_detach(&sc->sc_ev_symerrc);
   3406 
   3407 	if (sc->sc_type >= WM_T_82543) {
   3408 		evcnt_detach(&sc->sc_ev_algnerrc);
   3409 		evcnt_detach(&sc->sc_ev_rxerrc);
   3410 		evcnt_detach(&sc->sc_ev_cexterr);
   3411 	}
   3412 	evcnt_detach(&sc->sc_ev_mpc);
   3413 	evcnt_detach(&sc->sc_ev_colc);
   3414 	evcnt_detach(&sc->sc_ev_sec);
   3415 	evcnt_detach(&sc->sc_ev_rlec);
   3416 	evcnt_detach(&sc->sc_ev_scc);
   3417 	evcnt_detach(&sc->sc_ev_ecol);
   3418 	evcnt_detach(&sc->sc_ev_mcc);
   3419 	evcnt_detach(&sc->sc_ev_latecol);
   3420 	evcnt_detach(&sc->sc_ev_dc);
   3421 	evcnt_detach(&sc->sc_ev_gprc);
   3422 	evcnt_detach(&sc->sc_ev_bprc);
   3423 	evcnt_detach(&sc->sc_ev_mprc);
   3424 	evcnt_detach(&sc->sc_ev_gptc);
   3425 	evcnt_detach(&sc->sc_ev_gorc);
   3426 	evcnt_detach(&sc->sc_ev_gotc);
   3427 	evcnt_detach(&sc->sc_ev_rnbc);
   3428 	evcnt_detach(&sc->sc_ev_ruc);
   3429 	evcnt_detach(&sc->sc_ev_rfc);
   3430 	evcnt_detach(&sc->sc_ev_roc);
   3431 	evcnt_detach(&sc->sc_ev_rjc);
   3432 	evcnt_detach(&sc->sc_ev_tor);
   3433 	evcnt_detach(&sc->sc_ev_tot);
   3434 	evcnt_detach(&sc->sc_ev_tpr);
   3435 	evcnt_detach(&sc->sc_ev_tpt);
   3436 	evcnt_detach(&sc->sc_ev_mptc);
   3437 	evcnt_detach(&sc->sc_ev_bptc);
   3438 	evcnt_detach(&sc->sc_ev_prc64);
   3439 	evcnt_detach(&sc->sc_ev_prc127);
   3440 	evcnt_detach(&sc->sc_ev_prc255);
   3441 	evcnt_detach(&sc->sc_ev_prc511);
   3442 	evcnt_detach(&sc->sc_ev_prc1023);
   3443 	evcnt_detach(&sc->sc_ev_prc1522);
   3444 	evcnt_detach(&sc->sc_ev_ptc64);
   3445 	evcnt_detach(&sc->sc_ev_ptc127);
   3446 	evcnt_detach(&sc->sc_ev_ptc255);
   3447 	evcnt_detach(&sc->sc_ev_ptc511);
   3448 	evcnt_detach(&sc->sc_ev_ptc1023);
   3449 	evcnt_detach(&sc->sc_ev_ptc1522);
   3450 	evcnt_detach(&sc->sc_ev_iac);
   3451 	evcnt_detach(&sc->sc_ev_icrxptc);
   3452 	evcnt_detach(&sc->sc_ev_icrxatc);
   3453 	evcnt_detach(&sc->sc_ev_ictxptc);
   3454 	evcnt_detach(&sc->sc_ev_ictxact);
   3455 	evcnt_detach(&sc->sc_ev_ictxqec);
   3456 	evcnt_detach(&sc->sc_ev_ictxqmtc);
   3457 	evcnt_detach(&sc->sc_ev_icrxdmtc);
   3458 	evcnt_detach(&sc->sc_ev_icrxoc);
   3459 	if (sc->sc_type >= WM_T_82543) {
   3460 		evcnt_detach(&sc->sc_ev_tncrs);
   3461 		evcnt_detach(&sc->sc_ev_tsctc);
   3462 		evcnt_detach(&sc->sc_ev_tsctfc);
   3463 	}
   3464 	if (sc->sc_type >= WM_T_82540) {
   3465 		evcnt_detach(&sc->sc_ev_mgtprc);
   3466 		evcnt_detach(&sc->sc_ev_mgtpdc);
   3467 		evcnt_detach(&sc->sc_ev_mgtptc);
   3468 	}
   3469 	if ((sc->sc_type >= WM_T_I350) && (sc->sc_type < WM_T_80003)) {
   3470 		evcnt_detach(&sc->sc_ev_b2ogprc);
   3471 		evcnt_detach(&sc->sc_ev_o2bspc);
   3472 		evcnt_detach(&sc->sc_ev_b2ospc);
   3473 		evcnt_detach(&sc->sc_ev_o2bgptc);
   3474 	}
   3475 #endif /* WM_EVENT_COUNTERS */
   3476 
   3477 	rnd_detach_source(&sc->rnd_source);
   3478 
   3479 	/* Tell the firmware about the release */
   3480 	WM_CORE_LOCK(sc);
   3481 	wm_release_manageability(sc);
   3482 	wm_release_hw_control(sc);
   3483 	wm_enable_wakeup(sc);
   3484 	WM_CORE_UNLOCK(sc);
   3485 
   3486 	mii_detach(&sc->sc_mii, MII_PHY_ANY, MII_OFFSET_ANY);
   3487 
   3488 	ether_ifdetach(ifp);
   3489 	if_detach(ifp);
   3490 	if_percpuq_destroy(sc->sc_ipq);
   3491 
   3492 	/* Delete all remaining media. */
   3493 	ifmedia_fini(&sc->sc_mii.mii_media);
   3494 
   3495 	/* Unload RX dmamaps and free mbufs */
   3496 	for (i = 0; i < sc->sc_nqueues; i++) {
   3497 		struct wm_rxqueue *rxq = &sc->sc_queue[i].wmq_rxq;
   3498 		mutex_enter(rxq->rxq_lock);
   3499 		wm_rxdrain(rxq);
   3500 		mutex_exit(rxq->rxq_lock);
   3501 	}
   3502 	/* Must unlock here */
   3503 
   3504 	/* Disestablish the interrupt handler */
   3505 	for (i = 0; i < sc->sc_nintrs; i++) {
   3506 		if (sc->sc_ihs[i] != NULL) {
   3507 			pci_intr_disestablish(sc->sc_pc, sc->sc_ihs[i]);
   3508 			sc->sc_ihs[i] = NULL;
   3509 		}
   3510 	}
   3511 	pci_intr_release(sc->sc_pc, sc->sc_intrs, sc->sc_nintrs);
   3512 
   3513 	/* wm_stop() ensured that the workqueue is stopped. */
   3514 	workqueue_destroy(sc->sc_queue_wq);
   3515 
   3516 	for (i = 0; i < sc->sc_nqueues; i++)
   3517 		softint_disestablish(sc->sc_queue[i].wmq_si);
   3518 
   3519 	wm_free_txrx_queues(sc);
   3520 
   3521 	/* Unmap the registers */
   3522 	if (sc->sc_ss) {
   3523 		bus_space_unmap(sc->sc_st, sc->sc_sh, sc->sc_ss);
   3524 		sc->sc_ss = 0;
   3525 	}
   3526 	if (sc->sc_ios) {
   3527 		bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_ios);
   3528 		sc->sc_ios = 0;
   3529 	}
   3530 	if (sc->sc_flashs) {
   3531 		bus_space_unmap(sc->sc_flasht, sc->sc_flashh, sc->sc_flashs);
   3532 		sc->sc_flashs = 0;
   3533 	}
   3534 
   3535 	if (sc->sc_core_lock)
   3536 		mutex_obj_free(sc->sc_core_lock);
   3537 	if (sc->sc_ich_phymtx)
   3538 		mutex_obj_free(sc->sc_ich_phymtx);
   3539 	if (sc->sc_ich_nvmmtx)
   3540 		mutex_obj_free(sc->sc_ich_nvmmtx);
   3541 
   3542 	return 0;
   3543 }
   3544 
   3545 static bool
   3546 wm_suspend(device_t self, const pmf_qual_t *qual)
   3547 {
   3548 	struct wm_softc *sc = device_private(self);
   3549 
   3550 	wm_release_manageability(sc);
   3551 	wm_release_hw_control(sc);
   3552 	wm_enable_wakeup(sc);
   3553 
   3554 	return true;
   3555 }
   3556 
   3557 static bool
   3558 wm_resume(device_t self, const pmf_qual_t *qual)
   3559 {
   3560 	struct wm_softc *sc = device_private(self);
   3561 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   3562 	pcireg_t reg;
   3563 	char buf[256];
   3564 
   3565 	reg = CSR_READ(sc, WMREG_WUS);
   3566 	if (reg != 0) {
   3567 		snprintb(buf, sizeof(buf), WUS_FLAGS, reg);
   3568 		device_printf(sc->sc_dev, "wakeup status %s\n", buf);
   3569 		CSR_WRITE(sc, WMREG_WUS, 0xffffffff); /* W1C */
   3570 	}
   3571 
   3572 	if (sc->sc_type >= WM_T_PCH2)
   3573 		wm_resume_workarounds_pchlan(sc);
   3574 	if ((ifp->if_flags & IFF_UP) == 0) {
   3575 		/* >= PCH_SPT hardware workaround before reset. */
   3576 		if (sc->sc_type >= WM_T_PCH_SPT)
   3577 			wm_flush_desc_rings(sc);
   3578 
   3579 		wm_reset(sc);
   3580 		/* Non-AMT based hardware can now take control from firmware */
   3581 		if ((sc->sc_flags & WM_F_HAS_AMT) == 0)
   3582 			wm_get_hw_control(sc);
   3583 		wm_init_manageability(sc);
   3584 	} else {
   3585 		/*
   3586 		 * We called pmf_class_network_register(), so if_init() is
   3587 		 * automatically called when IFF_UP. wm_reset(),
   3588 		 * wm_get_hw_control() and wm_init_manageability() are called
   3589 		 * via wm_init().
   3590 		 */
   3591 	}
   3592 
   3593 	return true;
   3594 }
   3595 
   3596 /*
   3597  * wm_watchdog:		[ifnet interface function]
   3598  *
   3599  *	Watchdog timer handler.
   3600  */
   3601 static void
   3602 wm_watchdog(struct ifnet *ifp)
   3603 {
   3604 	int qid;
   3605 	struct wm_softc *sc = ifp->if_softc;
   3606 	uint16_t hang_queue = 0; /* Max queue number of wm(4) is 82576's 16. */
   3607 
   3608 	for (qid = 0; qid < sc->sc_nqueues; qid++) {
   3609 		struct wm_txqueue *txq = &sc->sc_queue[qid].wmq_txq;
   3610 
   3611 		wm_watchdog_txq(ifp, txq, &hang_queue);
   3612 	}
   3613 
   3614 	/* IF any of queues hanged up, reset the interface. */
   3615 	if (hang_queue != 0) {
   3616 		(void)wm_init(ifp);
   3617 
   3618 		/*
   3619 		 * There are still some upper layer processing which call
   3620 		 * ifp->if_start(). e.g. ALTQ or one CPU system
   3621 		 */
   3622 		/* Try to get more packets going. */
   3623 		ifp->if_start(ifp);
   3624 	}
   3625 }
   3626 
   3627 
   3628 static void
   3629 wm_watchdog_txq(struct ifnet *ifp, struct wm_txqueue *txq, uint16_t *hang)
   3630 {
   3631 
   3632 	mutex_enter(txq->txq_lock);
   3633 	if (txq->txq_sending &&
   3634 	    time_uptime - txq->txq_lastsent > wm_watchdog_timeout)
   3635 		wm_watchdog_txq_locked(ifp, txq, hang);
   3636 
   3637 	mutex_exit(txq->txq_lock);
   3638 }
   3639 
   3640 static void
   3641 wm_watchdog_txq_locked(struct ifnet *ifp, struct wm_txqueue *txq,
   3642     uint16_t *hang)
   3643 {
   3644 	struct wm_softc *sc = ifp->if_softc;
   3645 	struct wm_queue *wmq = container_of(txq, struct wm_queue, wmq_txq);
   3646 
   3647 	KASSERT(mutex_owned(txq->txq_lock));
   3648 
   3649 	/*
   3650 	 * Since we're using delayed interrupts, sweep up
   3651 	 * before we report an error.
   3652 	 */
   3653 	wm_txeof(txq, UINT_MAX);
   3654 
   3655 	if (txq->txq_sending)
   3656 		*hang |= __BIT(wmq->wmq_id);
   3657 
   3658 	if (txq->txq_free == WM_NTXDESC(txq)) {
   3659 		log(LOG_ERR, "%s: device timeout (lost interrupt)\n",
   3660 		    device_xname(sc->sc_dev));
   3661 	} else {
   3662 #ifdef WM_DEBUG
   3663 		int i, j;
   3664 		struct wm_txsoft *txs;
   3665 #endif
   3666 		log(LOG_ERR,
   3667 		    "%s: device timeout (txfree %d txsfree %d txnext %d)\n",
   3668 		    device_xname(sc->sc_dev), txq->txq_free, txq->txq_sfree,
   3669 		    txq->txq_next);
   3670 		if_statinc(ifp, if_oerrors);
   3671 #ifdef WM_DEBUG
   3672 		for (i = txq->txq_sdirty; i != txq->txq_snext;
   3673 		    i = WM_NEXTTXS(txq, i)) {
   3674 			txs = &txq->txq_soft[i];
   3675 			printf("txs %d tx %d -> %d\n",
   3676 			    i, txs->txs_firstdesc, txs->txs_lastdesc);
   3677 			for (j = txs->txs_firstdesc; ; j = WM_NEXTTX(txq, j)) {
   3678 				if ((sc->sc_flags & WM_F_NEWQUEUE) != 0) {
   3679 					printf("\tdesc %d: 0x%" PRIx64 "\n", j,
   3680 					    txq->txq_nq_descs[j].nqtx_data.nqtxd_addr);
   3681 					printf("\t %#08x%08x\n",
   3682 					    txq->txq_nq_descs[j].nqtx_data.nqtxd_fields,
   3683 					    txq->txq_nq_descs[j].nqtx_data.nqtxd_cmdlen);
   3684 				} else {
   3685 					printf("\tdesc %d: 0x%" PRIx64 "\n", j,
   3686 					    (uint64_t)txq->txq_descs[j].wtx_addr.wa_high << 32 |
   3687 					    txq->txq_descs[j].wtx_addr.wa_low);
   3688 					printf("\t %#04x%02x%02x%08x\n",
   3689 					    txq->txq_descs[j].wtx_fields.wtxu_vlan,
   3690 					    txq->txq_descs[j].wtx_fields.wtxu_options,
   3691 					    txq->txq_descs[j].wtx_fields.wtxu_status,
   3692 					    txq->txq_descs[j].wtx_cmdlen);
   3693 				}
   3694 				if (j == txs->txs_lastdesc)
   3695 					break;
   3696 			}
   3697 		}
   3698 #endif
   3699 	}
   3700 }
   3701 
   3702 /*
   3703  * wm_tick:
   3704  *
   3705  *	One second timer, used to check link status, sweep up
   3706  *	completed transmit jobs, etc.
   3707  */
   3708 static void
   3709 wm_tick(void *arg)
   3710 {
   3711 	struct wm_softc *sc = arg;
   3712 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   3713 	uint64_t crcerrs, algnerrc, symerrc, mpc, colc,  sec, rlec, rxerrc,
   3714 	    cexterr;
   3715 #ifndef WM_MPSAFE
   3716 	int s = splnet();
   3717 #endif
   3718 
   3719 	WM_CORE_LOCK(sc);
   3720 
   3721 	if (sc->sc_core_stopping) {
   3722 		WM_CORE_UNLOCK(sc);
   3723 #ifndef WM_MPSAFE
   3724 		splx(s);
   3725 #endif
   3726 		return;
   3727 	}
   3728 
   3729 	crcerrs = CSR_READ(sc, WMREG_CRCERRS);
   3730 	symerrc = CSR_READ(sc, WMREG_SYMERRC);
   3731 	mpc = CSR_READ(sc, WMREG_MPC);
   3732 	colc = CSR_READ(sc, WMREG_COLC);
   3733 	sec = CSR_READ(sc, WMREG_SEC);
   3734 	rlec = CSR_READ(sc, WMREG_RLEC);
   3735 
   3736 	WM_EVCNT_ADD(&sc->sc_ev_crcerrs, crcerrs);
   3737 	WM_EVCNT_ADD(&sc->sc_ev_symerrc, symerrc);
   3738 	WM_EVCNT_ADD(&sc->sc_ev_mpc, mpc);
   3739 	WM_EVCNT_ADD(&sc->sc_ev_colc, colc);
   3740 	WM_EVCNT_ADD(&sc->sc_ev_sec, sec);
   3741 	WM_EVCNT_ADD(&sc->sc_ev_rlec, rlec);
   3742 
   3743 	if (sc->sc_type >= WM_T_82542_2_1) {
   3744 		WM_EVCNT_ADD(&sc->sc_ev_rx_xon, CSR_READ(sc, WMREG_XONRXC));
   3745 		WM_EVCNT_ADD(&sc->sc_ev_tx_xon, CSR_READ(sc, WMREG_XONTXC));
   3746 		WM_EVCNT_ADD(&sc->sc_ev_rx_xoff, CSR_READ(sc, WMREG_XOFFRXC));
   3747 		WM_EVCNT_ADD(&sc->sc_ev_tx_xoff, CSR_READ(sc, WMREG_XOFFTXC));
   3748 		WM_EVCNT_ADD(&sc->sc_ev_rx_macctl, CSR_READ(sc, WMREG_FCRUC));
   3749 	}
   3750 	WM_EVCNT_ADD(&sc->sc_ev_scc, CSR_READ(sc, WMREG_SCC));
   3751 	WM_EVCNT_ADD(&sc->sc_ev_ecol, CSR_READ(sc, WMREG_ECOL));
   3752 	WM_EVCNT_ADD(&sc->sc_ev_mcc, CSR_READ(sc, WMREG_MCC));
   3753 	WM_EVCNT_ADD(&sc->sc_ev_latecol, CSR_READ(sc, WMREG_LATECOL));
   3754 	WM_EVCNT_ADD(&sc->sc_ev_dc, CSR_READ(sc, WMREG_DC));
   3755 	WM_EVCNT_ADD(&sc->sc_ev_gprc, CSR_READ(sc, WMREG_GPRC));
   3756 	WM_EVCNT_ADD(&sc->sc_ev_bprc, CSR_READ(sc, WMREG_BPRC));
   3757 	WM_EVCNT_ADD(&sc->sc_ev_mprc, CSR_READ(sc, WMREG_MPRC));
   3758 	WM_EVCNT_ADD(&sc->sc_ev_gptc, CSR_READ(sc, WMREG_GPTC));
   3759 
   3760 	WM_EVCNT_ADD(&sc->sc_ev_gorc,
   3761 	    CSR_READ(sc, WMREG_GORCL) + CSR_READ(sc, WMREG_GORCH));
   3762 	WM_EVCNT_ADD(&sc->sc_ev_gotc,
   3763 	    CSR_READ(sc, WMREG_GOTCL) + CSR_READ(sc, WMREG_GOTCH));
   3764 
   3765 	WM_EVCNT_ADD(&sc->sc_ev_rnbc, CSR_READ(sc, WMREG_RNBC));
   3766 	WM_EVCNT_ADD(&sc->sc_ev_ruc, CSR_READ(sc, WMREG_RUC));
   3767 	WM_EVCNT_ADD(&sc->sc_ev_rfc, CSR_READ(sc, WMREG_RFC));
   3768 	WM_EVCNT_ADD(&sc->sc_ev_roc, CSR_READ(sc, WMREG_ROC));
   3769 	WM_EVCNT_ADD(&sc->sc_ev_rjc, CSR_READ(sc, WMREG_RJC));
   3770 
   3771 	WM_EVCNT_ADD(&sc->sc_ev_tor,
   3772 	    CSR_READ(sc, WMREG_TORL) + CSR_READ(sc, WMREG_TORH));
   3773 	WM_EVCNT_ADD(&sc->sc_ev_tot,
   3774 	    CSR_READ(sc, WMREG_TOTL) + CSR_READ(sc, WMREG_TOTH));
   3775 
   3776 	WM_EVCNT_ADD(&sc->sc_ev_tpr, CSR_READ(sc, WMREG_TPR));
   3777 	WM_EVCNT_ADD(&sc->sc_ev_tpt, CSR_READ(sc, WMREG_TPT));
   3778 	WM_EVCNT_ADD(&sc->sc_ev_mptc, CSR_READ(sc, WMREG_MPTC));
   3779 	WM_EVCNT_ADD(&sc->sc_ev_bptc, CSR_READ(sc, WMREG_BPTC));
   3780 	WM_EVCNT_ADD(&sc->sc_ev_prc64, CSR_READ(sc, WMREG_PRC64));
   3781 	WM_EVCNT_ADD(&sc->sc_ev_prc127, CSR_READ(sc, WMREG_PRC127));
   3782 	WM_EVCNT_ADD(&sc->sc_ev_prc255, CSR_READ(sc, WMREG_PRC255));
   3783 	WM_EVCNT_ADD(&sc->sc_ev_prc511, CSR_READ(sc, WMREG_PRC511));
   3784 	WM_EVCNT_ADD(&sc->sc_ev_prc1023, CSR_READ(sc, WMREG_PRC1023));
   3785 	WM_EVCNT_ADD(&sc->sc_ev_prc1522, CSR_READ(sc, WMREG_PRC1522));
   3786 	WM_EVCNT_ADD(&sc->sc_ev_ptc64, CSR_READ(sc, WMREG_PTC64));
   3787 	WM_EVCNT_ADD(&sc->sc_ev_ptc127, CSR_READ(sc, WMREG_PTC127));
   3788 	WM_EVCNT_ADD(&sc->sc_ev_ptc255, CSR_READ(sc, WMREG_PTC255));
   3789 	WM_EVCNT_ADD(&sc->sc_ev_ptc511, CSR_READ(sc, WMREG_PTC511));
   3790 	WM_EVCNT_ADD(&sc->sc_ev_ptc1023, CSR_READ(sc, WMREG_PTC1023));
   3791 	WM_EVCNT_ADD(&sc->sc_ev_ptc1522, CSR_READ(sc, WMREG_PTC1522));
   3792 	WM_EVCNT_ADD(&sc->sc_ev_iac, CSR_READ(sc, WMREG_IAC));
   3793 	WM_EVCNT_ADD(&sc->sc_ev_icrxptc, CSR_READ(sc, WMREG_ICRXPTC));
   3794 	WM_EVCNT_ADD(&sc->sc_ev_icrxatc, CSR_READ(sc, WMREG_ICRXATC));
   3795 	WM_EVCNT_ADD(&sc->sc_ev_ictxptc, CSR_READ(sc, WMREG_ICTXPTC));
   3796 	WM_EVCNT_ADD(&sc->sc_ev_ictxact, CSR_READ(sc, WMREG_ICTXATC));
   3797 	WM_EVCNT_ADD(&sc->sc_ev_ictxqec, CSR_READ(sc, WMREG_ICTXQEC));
   3798 	WM_EVCNT_ADD(&sc->sc_ev_ictxqmtc, CSR_READ(sc, WMREG_ICTXQMTC));
   3799 	WM_EVCNT_ADD(&sc->sc_ev_icrxdmtc, CSR_READ(sc, WMREG_ICRXDMTC));
   3800 	WM_EVCNT_ADD(&sc->sc_ev_icrxoc, CSR_READ(sc, WMREG_ICRXOC));
   3801 
   3802 	if (sc->sc_type >= WM_T_82543) {
   3803 		algnerrc = CSR_READ(sc, WMREG_ALGNERRC);
   3804 		rxerrc = CSR_READ(sc, WMREG_RXERRC);
   3805 		cexterr = CSR_READ(sc, WMREG_CEXTERR);
   3806 		WM_EVCNT_ADD(&sc->sc_ev_algnerrc, algnerrc);
   3807 		WM_EVCNT_ADD(&sc->sc_ev_rxerrc, rxerrc);
   3808 		WM_EVCNT_ADD(&sc->sc_ev_cexterr, cexterr);
   3809 
   3810 		WM_EVCNT_ADD(&sc->sc_ev_tncrs, CSR_READ(sc, WMREG_TNCRS));
   3811 		WM_EVCNT_ADD(&sc->sc_ev_tsctc, CSR_READ(sc, WMREG_TSCTC));
   3812 		WM_EVCNT_ADD(&sc->sc_ev_tsctfc, CSR_READ(sc, WMREG_TSCTFC));
   3813 	} else
   3814 		algnerrc = rxerrc = cexterr = 0;
   3815 
   3816 	if (sc->sc_type >= WM_T_82540) {
   3817 		WM_EVCNT_ADD(&sc->sc_ev_mgtprc, CSR_READ(sc, WMREG_MGTPRC));
   3818 		WM_EVCNT_ADD(&sc->sc_ev_mgtpdc, CSR_READ(sc, WMREG_MGTPDC));
   3819 		WM_EVCNT_ADD(&sc->sc_ev_mgtptc, CSR_READ(sc, WMREG_MGTPTC));
   3820 	}
   3821 	if (((sc->sc_type >= WM_T_I350) && (sc->sc_type < WM_T_80003))
   3822 	    && ((CSR_READ(sc, WMREG_MANC) & MANC_EN_BMC2OS) != 0)) {
   3823 		WM_EVCNT_ADD(&sc->sc_ev_b2ogprc, CSR_READ(sc, WMREG_B2OGPRC));
   3824 		WM_EVCNT_ADD(&sc->sc_ev_o2bspc, CSR_READ(sc, WMREG_O2BSPC));
   3825 		WM_EVCNT_ADD(&sc->sc_ev_b2ospc, CSR_READ(sc, WMREG_B2OSPC));
   3826 		WM_EVCNT_ADD(&sc->sc_ev_o2bgptc, CSR_READ(sc, WMREG_O2BGPTC));
   3827 	}
   3828 	net_stat_ref_t nsr = IF_STAT_GETREF(ifp);
   3829 	if_statadd_ref(nsr, if_collisions, colc);
   3830 	if_statadd_ref(nsr, if_ierrors,
   3831 	    crcerrs + algnerrc + symerrc + rxerrc + sec + cexterr + rlec);
   3832 	/*
   3833 	 * WMREG_RNBC is incremented when there are no available buffers in host
   3834 	 * memory. It does not mean the number of dropped packets, because an
   3835 	 * Ethernet controller can receive packets in such case if there is
   3836 	 * space in the phy's FIFO.
   3837 	 *
   3838 	 * If you want to know the nubmer of WMREG_RMBC, you should use such as
   3839 	 * own EVCNT instead of if_iqdrops.
   3840 	 */
   3841 	if_statadd_ref(nsr, if_iqdrops, mpc);
   3842 	IF_STAT_PUTREF(ifp);
   3843 
   3844 	if (sc->sc_flags & WM_F_HAS_MII)
   3845 		mii_tick(&sc->sc_mii);
   3846 	else if ((sc->sc_type >= WM_T_82575) && (sc->sc_type <= WM_T_I211)
   3847 	    && (sc->sc_mediatype == WM_MEDIATYPE_SERDES))
   3848 		wm_serdes_tick(sc);
   3849 	else
   3850 		wm_tbi_tick(sc);
   3851 
   3852 	WM_CORE_UNLOCK(sc);
   3853 #ifndef WM_MPSAFE
   3854 	splx(s);
   3855 #endif
   3856 
   3857 	wm_watchdog(ifp);
   3858 
   3859 	callout_schedule(&sc->sc_tick_ch, hz);
   3860 }
   3861 
   3862 static int
   3863 wm_ifflags_cb(struct ethercom *ec)
   3864 {
   3865 	struct ifnet *ifp = &ec->ec_if;
   3866 	struct wm_softc *sc = ifp->if_softc;
   3867 	u_short iffchange;
   3868 	int ecchange;
   3869 	bool needreset = false;
   3870 	int rc = 0;
   3871 
   3872 	DPRINTF(sc, WM_DEBUG_INIT, ("%s: %s called\n",
   3873 		device_xname(sc->sc_dev), __func__));
   3874 
   3875 	KASSERT(IFNET_LOCKED(ifp));
   3876 	WM_CORE_LOCK(sc);
   3877 
   3878 	/*
   3879 	 * Check for if_flags.
   3880 	 * Main usage is to prevent linkdown when opening bpf.
   3881 	 */
   3882 	iffchange = ifp->if_flags ^ sc->sc_if_flags;
   3883 	sc->sc_if_flags = ifp->if_flags;
   3884 	if ((iffchange & ~(IFF_CANTCHANGE | IFF_DEBUG)) != 0) {
   3885 		needreset = true;
   3886 		goto ec;
   3887 	}
   3888 
   3889 	/* iff related updates */
   3890 	if ((iffchange & IFF_PROMISC) != 0)
   3891 		wm_set_filter(sc);
   3892 
   3893 	wm_set_vlan(sc);
   3894 
   3895 ec:
   3896 	/* Check for ec_capenable. */
   3897 	ecchange = ec->ec_capenable ^ sc->sc_ec_capenable;
   3898 	sc->sc_ec_capenable = ec->ec_capenable;
   3899 	if ((ecchange & ~ETHERCAP_EEE) != 0) {
   3900 		needreset = true;
   3901 		goto out;
   3902 	}
   3903 
   3904 	/* ec related updates */
   3905 	wm_set_eee(sc);
   3906 
   3907 out:
   3908 	if (needreset)
   3909 		rc = ENETRESET;
   3910 	WM_CORE_UNLOCK(sc);
   3911 
   3912 	return rc;
   3913 }
   3914 
   3915 static bool
   3916 wm_phy_need_linkdown_discard(struct wm_softc *sc)
   3917 {
   3918 
   3919 	switch (sc->sc_phytype) {
   3920 	case WMPHY_82577: /* ihphy */
   3921 	case WMPHY_82578: /* atphy */
   3922 	case WMPHY_82579: /* ihphy */
   3923 	case WMPHY_I217: /* ihphy */
   3924 	case WMPHY_82580: /* ihphy */
   3925 	case WMPHY_I350: /* ihphy */
   3926 		return true;
   3927 	default:
   3928 		return false;
   3929 	}
   3930 }
   3931 
   3932 static void
   3933 wm_set_linkdown_discard(struct wm_softc *sc)
   3934 {
   3935 
   3936 	for (int i = 0; i < sc->sc_nqueues; i++) {
   3937 		struct wm_txqueue *txq = &sc->sc_queue[i].wmq_txq;
   3938 
   3939 		mutex_enter(txq->txq_lock);
   3940 		txq->txq_flags |= WM_TXQ_LINKDOWN_DISCARD;
   3941 		mutex_exit(txq->txq_lock);
   3942 	}
   3943 }
   3944 
   3945 static void
   3946 wm_clear_linkdown_discard(struct wm_softc *sc)
   3947 {
   3948 
   3949 	for (int i = 0; i < sc->sc_nqueues; i++) {
   3950 		struct wm_txqueue *txq = &sc->sc_queue[i].wmq_txq;
   3951 
   3952 		mutex_enter(txq->txq_lock);
   3953 		txq->txq_flags &= ~WM_TXQ_LINKDOWN_DISCARD;
   3954 		mutex_exit(txq->txq_lock);
   3955 	}
   3956 }
   3957 
   3958 /*
   3959  * wm_ioctl:		[ifnet interface function]
   3960  *
   3961  *	Handle control requests from the operator.
   3962  */
   3963 static int
   3964 wm_ioctl(struct ifnet *ifp, u_long cmd, void *data)
   3965 {
   3966 	struct wm_softc *sc = ifp->if_softc;
   3967 	struct ifreq *ifr = (struct ifreq *)data;
   3968 	struct ifaddr *ifa = (struct ifaddr *)data;
   3969 	struct sockaddr_dl *sdl;
   3970 	int error;
   3971 
   3972 	DPRINTF(sc, WM_DEBUG_INIT, ("%s: %s called\n",
   3973 		device_xname(sc->sc_dev), __func__));
   3974 
   3975 	switch (cmd) {
   3976 	case SIOCADDMULTI:
   3977 	case SIOCDELMULTI:
   3978 		break;
   3979 	default:
   3980 		KASSERT(IFNET_LOCKED(ifp));
   3981 	}
   3982 
   3983 #ifndef WM_MPSAFE
   3984 	const int s = splnet();
   3985 #endif
   3986 	switch (cmd) {
   3987 	case SIOCSIFMEDIA:
   3988 		WM_CORE_LOCK(sc);
   3989 		/* Flow control requires full-duplex mode. */
   3990 		if (IFM_SUBTYPE(ifr->ifr_media) == IFM_AUTO ||
   3991 		    (ifr->ifr_media & IFM_FDX) == 0)
   3992 			ifr->ifr_media &= ~IFM_ETH_FMASK;
   3993 		if (IFM_SUBTYPE(ifr->ifr_media) != IFM_AUTO) {
   3994 			if ((ifr->ifr_media & IFM_ETH_FMASK) == IFM_FLOW) {
   3995 				/* We can do both TXPAUSE and RXPAUSE. */
   3996 				ifr->ifr_media |=
   3997 				    IFM_ETH_TXPAUSE | IFM_ETH_RXPAUSE;
   3998 			}
   3999 			sc->sc_flowflags = ifr->ifr_media & IFM_ETH_FMASK;
   4000 		}
   4001 		WM_CORE_UNLOCK(sc);
   4002 		error = ifmedia_ioctl(ifp, ifr, &sc->sc_mii.mii_media, cmd);
   4003 		if (error == 0 && wm_phy_need_linkdown_discard(sc)) {
   4004 			if (IFM_SUBTYPE(ifr->ifr_media) == IFM_NONE) {
   4005 				DPRINTF(sc, WM_DEBUG_LINK,
   4006 				    ("%s: %s: Set linkdown discard flag\n",
   4007 					device_xname(sc->sc_dev), __func__));
   4008 				wm_set_linkdown_discard(sc);
   4009 			}
   4010 		}
   4011 		break;
   4012 	case SIOCINITIFADDR:
   4013 		WM_CORE_LOCK(sc);
   4014 		if (ifa->ifa_addr->sa_family == AF_LINK) {
   4015 			sdl = satosdl(ifp->if_dl->ifa_addr);
   4016 			(void)sockaddr_dl_setaddr(sdl, sdl->sdl_len,
   4017 			    LLADDR(satosdl(ifa->ifa_addr)), ifp->if_addrlen);
   4018 			/* Unicast address is the first multicast entry */
   4019 			wm_set_filter(sc);
   4020 			error = 0;
   4021 			WM_CORE_UNLOCK(sc);
   4022 			break;
   4023 		}
   4024 		WM_CORE_UNLOCK(sc);
   4025 		/*FALLTHROUGH*/
   4026 	default:
   4027 		if (cmd == SIOCSIFFLAGS && wm_phy_need_linkdown_discard(sc)) {
   4028 			if (((ifp->if_flags & IFF_UP) != 0) &&
   4029 			    ((ifr->ifr_flags & IFF_UP) == 0)) {
   4030 				DPRINTF(sc, WM_DEBUG_LINK,
   4031 				    ("%s: %s: Set linkdown discard flag\n",
   4032 					device_xname(sc->sc_dev), __func__));
   4033 				wm_set_linkdown_discard(sc);
   4034 			}
   4035 		}
   4036 #ifdef WM_MPSAFE
   4037 		const int s = splnet();
   4038 #endif
   4039 		/* It may call wm_start, so unlock here */
   4040 		error = ether_ioctl(ifp, cmd, data);
   4041 #ifdef WM_MPSAFE
   4042 		splx(s);
   4043 #endif
   4044 		if (error != ENETRESET)
   4045 			break;
   4046 
   4047 		error = 0;
   4048 
   4049 		if (cmd == SIOCSIFCAP)
   4050 			error = if_init(ifp);
   4051 		else if (cmd == SIOCADDMULTI || cmd == SIOCDELMULTI) {
   4052 			WM_CORE_LOCK(sc);
   4053 			if (sc->sc_if_flags & IFF_RUNNING) {
   4054 				/*
   4055 				 * Multicast list has changed; set the hardware filter
   4056 				 * accordingly.
   4057 				 */
   4058 				wm_set_filter(sc);
   4059 			}
   4060 			WM_CORE_UNLOCK(sc);
   4061 		}
   4062 		break;
   4063 	}
   4064 
   4065 #ifndef WM_MPSAFE
   4066 	splx(s);
   4067 #endif
   4068 	return error;
   4069 }
   4070 
   4071 /* MAC address related */
   4072 
   4073 /*
   4074  * Get the offset of MAC address and return it.
   4075  * If error occured, use offset 0.
   4076  */
   4077 static uint16_t
   4078 wm_check_alt_mac_addr(struct wm_softc *sc)
   4079 {
   4080 	uint16_t myea[ETHER_ADDR_LEN / 2];
   4081 	uint16_t offset = NVM_OFF_MACADDR;
   4082 
   4083 	/* Try to read alternative MAC address pointer */
   4084 	if (wm_nvm_read(sc, NVM_OFF_ALT_MAC_ADDR_PTR, 1, &offset) != 0)
   4085 		return 0;
   4086 
   4087 	/* Check pointer if it's valid or not. */
   4088 	if ((offset == 0x0000) || (offset == 0xffff))
   4089 		return 0;
   4090 
   4091 	offset += NVM_OFF_MACADDR_82571(sc->sc_funcid);
   4092 	/*
   4093 	 * Check whether alternative MAC address is valid or not.
   4094 	 * Some cards have non 0xffff pointer but those don't use
   4095 	 * alternative MAC address in reality.
   4096 	 *
   4097 	 * Check whether the broadcast bit is set or not.
   4098 	 */
   4099 	if (wm_nvm_read(sc, offset, 1, myea) == 0)
   4100 		if (((myea[0] & 0xff) & 0x01) == 0)
   4101 			return offset; /* Found */
   4102 
   4103 	/* Not found */
   4104 	return 0;
   4105 }
   4106 
   4107 static int
   4108 wm_read_mac_addr(struct wm_softc *sc, uint8_t *enaddr)
   4109 {
   4110 	uint16_t myea[ETHER_ADDR_LEN / 2];
   4111 	uint16_t offset = NVM_OFF_MACADDR;
   4112 	int do_invert = 0;
   4113 
   4114 	switch (sc->sc_type) {
   4115 	case WM_T_82580:
   4116 	case WM_T_I350:
   4117 	case WM_T_I354:
   4118 		/* EEPROM Top Level Partitioning */
   4119 		offset = NVM_OFF_LAN_FUNC_82580(sc->sc_funcid) + 0;
   4120 		break;
   4121 	case WM_T_82571:
   4122 	case WM_T_82575:
   4123 	case WM_T_82576:
   4124 	case WM_T_80003:
   4125 	case WM_T_I210:
   4126 	case WM_T_I211:
   4127 		offset = wm_check_alt_mac_addr(sc);
   4128 		if (offset == 0)
   4129 			if ((sc->sc_funcid & 0x01) == 1)
   4130 				do_invert = 1;
   4131 		break;
   4132 	default:
   4133 		if ((sc->sc_funcid & 0x01) == 1)
   4134 			do_invert = 1;
   4135 		break;
   4136 	}
   4137 
   4138 	if (wm_nvm_read(sc, offset, sizeof(myea) / sizeof(myea[0]), myea) != 0)
   4139 		goto bad;
   4140 
   4141 	enaddr[0] = myea[0] & 0xff;
   4142 	enaddr[1] = myea[0] >> 8;
   4143 	enaddr[2] = myea[1] & 0xff;
   4144 	enaddr[3] = myea[1] >> 8;
   4145 	enaddr[4] = myea[2] & 0xff;
   4146 	enaddr[5] = myea[2] >> 8;
   4147 
   4148 	/*
   4149 	 * Toggle the LSB of the MAC address on the second port
   4150 	 * of some dual port cards.
   4151 	 */
   4152 	if (do_invert != 0)
   4153 		enaddr[5] ^= 1;
   4154 
   4155 	return 0;
   4156 
   4157  bad:
   4158 	return -1;
   4159 }
   4160 
   4161 /*
   4162  * wm_set_ral:
   4163  *
   4164  *	Set an entery in the receive address list.
   4165  */
   4166 static void
   4167 wm_set_ral(struct wm_softc *sc, const uint8_t *enaddr, int idx)
   4168 {
   4169 	uint32_t ral_lo, ral_hi, addrl, addrh;
   4170 	uint32_t wlock_mac;
   4171 	int rv;
   4172 
   4173 	if (enaddr != NULL) {
   4174 		ral_lo = (uint32_t)enaddr[0] | ((uint32_t)enaddr[1] << 8) |
   4175 		    ((uint32_t)enaddr[2] << 16) | ((uint32_t)enaddr[3] << 24);
   4176 		ral_hi = (uint32_t)enaddr[4] | ((uint32_t)enaddr[5] << 8);
   4177 		ral_hi |= RAL_AV;
   4178 	} else {
   4179 		ral_lo = 0;
   4180 		ral_hi = 0;
   4181 	}
   4182 
   4183 	switch (sc->sc_type) {
   4184 	case WM_T_82542_2_0:
   4185 	case WM_T_82542_2_1:
   4186 	case WM_T_82543:
   4187 		CSR_WRITE(sc, WMREG_RAL(idx), ral_lo);
   4188 		CSR_WRITE_FLUSH(sc);
   4189 		CSR_WRITE(sc, WMREG_RAH(idx), ral_hi);
   4190 		CSR_WRITE_FLUSH(sc);
   4191 		break;
   4192 	case WM_T_PCH2:
   4193 	case WM_T_PCH_LPT:
   4194 	case WM_T_PCH_SPT:
   4195 	case WM_T_PCH_CNP:
   4196 		if (idx == 0) {
   4197 			CSR_WRITE(sc, WMREG_CORDOVA_RAL(idx), ral_lo);
   4198 			CSR_WRITE_FLUSH(sc);
   4199 			CSR_WRITE(sc, WMREG_CORDOVA_RAH(idx), ral_hi);
   4200 			CSR_WRITE_FLUSH(sc);
   4201 			return;
   4202 		}
   4203 		if (sc->sc_type != WM_T_PCH2) {
   4204 			wlock_mac = __SHIFTOUT(CSR_READ(sc, WMREG_FWSM),
   4205 			    FWSM_WLOCK_MAC);
   4206 			addrl = WMREG_SHRAL(idx - 1);
   4207 			addrh = WMREG_SHRAH(idx - 1);
   4208 		} else {
   4209 			wlock_mac = 0;
   4210 			addrl = WMREG_PCH_LPT_SHRAL(idx - 1);
   4211 			addrh = WMREG_PCH_LPT_SHRAH(idx - 1);
   4212 		}
   4213 
   4214 		if ((wlock_mac == 0) || (idx <= wlock_mac)) {
   4215 			rv = wm_get_swflag_ich8lan(sc);
   4216 			if (rv != 0)
   4217 				return;
   4218 			CSR_WRITE(sc, addrl, ral_lo);
   4219 			CSR_WRITE_FLUSH(sc);
   4220 			CSR_WRITE(sc, addrh, ral_hi);
   4221 			CSR_WRITE_FLUSH(sc);
   4222 			wm_put_swflag_ich8lan(sc);
   4223 		}
   4224 
   4225 		break;
   4226 	default:
   4227 		CSR_WRITE(sc, WMREG_CORDOVA_RAL(idx), ral_lo);
   4228 		CSR_WRITE_FLUSH(sc);
   4229 		CSR_WRITE(sc, WMREG_CORDOVA_RAH(idx), ral_hi);
   4230 		CSR_WRITE_FLUSH(sc);
   4231 		break;
   4232 	}
   4233 }
   4234 
   4235 /*
   4236  * wm_mchash:
   4237  *
   4238  *	Compute the hash of the multicast address for the 4096-bit
   4239  *	multicast filter.
   4240  */
   4241 static uint32_t
   4242 wm_mchash(struct wm_softc *sc, const uint8_t *enaddr)
   4243 {
   4244 	static const int lo_shift[4] = { 4, 3, 2, 0 };
   4245 	static const int hi_shift[4] = { 4, 5, 6, 8 };
   4246 	static const int ich8_lo_shift[4] = { 6, 5, 4, 2 };
   4247 	static const int ich8_hi_shift[4] = { 2, 3, 4, 6 };
   4248 	uint32_t hash;
   4249 
   4250 	if ((sc->sc_type == WM_T_ICH8) || (sc->sc_type == WM_T_ICH9)
   4251 	    || (sc->sc_type == WM_T_ICH10) || (sc->sc_type == WM_T_PCH)
   4252 	    || (sc->sc_type == WM_T_PCH2) || (sc->sc_type == WM_T_PCH_LPT)
   4253 	    || (sc->sc_type == WM_T_PCH_SPT) || (sc->sc_type == WM_T_PCH_CNP)){
   4254 		hash = (enaddr[4] >> ich8_lo_shift[sc->sc_mchash_type]) |
   4255 		    (((uint16_t)enaddr[5]) << ich8_hi_shift[sc->sc_mchash_type]);
   4256 		return (hash & 0x3ff);
   4257 	}
   4258 	hash = (enaddr[4] >> lo_shift[sc->sc_mchash_type]) |
   4259 	    (((uint16_t)enaddr[5]) << hi_shift[sc->sc_mchash_type]);
   4260 
   4261 	return (hash & 0xfff);
   4262 }
   4263 
   4264 /*
   4265  *
   4266  *
   4267  */
   4268 static int
   4269 wm_rar_count(struct wm_softc *sc)
   4270 {
   4271 	int size;
   4272 
   4273 	switch (sc->sc_type) {
   4274 	case WM_T_ICH8:
   4275 		size = WM_RAL_TABSIZE_ICH8 -1;
   4276 		break;
   4277 	case WM_T_ICH9:
   4278 	case WM_T_ICH10:
   4279 	case WM_T_PCH:
   4280 		size = WM_RAL_TABSIZE_ICH8;
   4281 		break;
   4282 	case WM_T_PCH2:
   4283 		size = WM_RAL_TABSIZE_PCH2;
   4284 		break;
   4285 	case WM_T_PCH_LPT:
   4286 	case WM_T_PCH_SPT:
   4287 	case WM_T_PCH_CNP:
   4288 		size = WM_RAL_TABSIZE_PCH_LPT;
   4289 		break;
   4290 	case WM_T_82575:
   4291 	case WM_T_I210:
   4292 	case WM_T_I211:
   4293 		size = WM_RAL_TABSIZE_82575;
   4294 		break;
   4295 	case WM_T_82576:
   4296 	case WM_T_82580:
   4297 		size = WM_RAL_TABSIZE_82576;
   4298 		break;
   4299 	case WM_T_I350:
   4300 	case WM_T_I354:
   4301 		size = WM_RAL_TABSIZE_I350;
   4302 		break;
   4303 	default:
   4304 		size = WM_RAL_TABSIZE;
   4305 	}
   4306 
   4307 	return size;
   4308 }
   4309 
   4310 /*
   4311  * wm_set_filter:
   4312  *
   4313  *	Set up the receive filter.
   4314  */
   4315 static void
   4316 wm_set_filter(struct wm_softc *sc)
   4317 {
   4318 	struct ethercom *ec = &sc->sc_ethercom;
   4319 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   4320 	struct ether_multi *enm;
   4321 	struct ether_multistep step;
   4322 	bus_addr_t mta_reg;
   4323 	uint32_t hash, reg, bit;
   4324 	int i, size, ralmax, rv;
   4325 
   4326 	DPRINTF(sc, WM_DEBUG_INIT, ("%s: %s called\n",
   4327 		device_xname(sc->sc_dev), __func__));
   4328 
   4329 	if (sc->sc_type >= WM_T_82544)
   4330 		mta_reg = WMREG_CORDOVA_MTA;
   4331 	else
   4332 		mta_reg = WMREG_MTA;
   4333 
   4334 	sc->sc_rctl &= ~(RCTL_BAM | RCTL_UPE | RCTL_MPE);
   4335 
   4336 	if (ifp->if_flags & IFF_BROADCAST)
   4337 		sc->sc_rctl |= RCTL_BAM;
   4338 	if (ifp->if_flags & IFF_PROMISC) {
   4339 		sc->sc_rctl |= RCTL_UPE;
   4340 		ETHER_LOCK(ec);
   4341 		ec->ec_flags |= ETHER_F_ALLMULTI;
   4342 		ETHER_UNLOCK(ec);
   4343 		goto allmulti;
   4344 	}
   4345 
   4346 	/*
   4347 	 * Set the station address in the first RAL slot, and
   4348 	 * clear the remaining slots.
   4349 	 */
   4350 	size = wm_rar_count(sc);
   4351 	wm_set_ral(sc, CLLADDR(ifp->if_sadl), 0);
   4352 
   4353 	if ((sc->sc_type == WM_T_PCH_LPT) || (sc->sc_type == WM_T_PCH_SPT)
   4354 	    || (sc->sc_type == WM_T_PCH_CNP)) {
   4355 		i = __SHIFTOUT(CSR_READ(sc, WMREG_FWSM), FWSM_WLOCK_MAC);
   4356 		switch (i) {
   4357 		case 0:
   4358 			/* We can use all entries */
   4359 			ralmax = size;
   4360 			break;
   4361 		case 1:
   4362 			/* Only RAR[0] */
   4363 			ralmax = 1;
   4364 			break;
   4365 		default:
   4366 			/* Available SHRA + RAR[0] */
   4367 			ralmax = i + 1;
   4368 		}
   4369 	} else
   4370 		ralmax = size;
   4371 	for (i = 1; i < size; i++) {
   4372 		if (i < ralmax)
   4373 			wm_set_ral(sc, NULL, i);
   4374 	}
   4375 
   4376 	if ((sc->sc_type == WM_T_ICH8) || (sc->sc_type == WM_T_ICH9)
   4377 	    || (sc->sc_type == WM_T_ICH10) || (sc->sc_type == WM_T_PCH)
   4378 	    || (sc->sc_type == WM_T_PCH2) || (sc->sc_type == WM_T_PCH_LPT)
   4379 	    || (sc->sc_type == WM_T_PCH_SPT) || (sc->sc_type == WM_T_PCH_CNP))
   4380 		size = WM_ICH8_MC_TABSIZE;
   4381 	else
   4382 		size = WM_MC_TABSIZE;
   4383 	/* Clear out the multicast table. */
   4384 	for (i = 0; i < size; i++) {
   4385 		CSR_WRITE(sc, mta_reg + (i << 2), 0);
   4386 		CSR_WRITE_FLUSH(sc);
   4387 	}
   4388 
   4389 	ETHER_LOCK(ec);
   4390 	ETHER_FIRST_MULTI(step, ec, enm);
   4391 	while (enm != NULL) {
   4392 		if (memcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
   4393 			ec->ec_flags |= ETHER_F_ALLMULTI;
   4394 			ETHER_UNLOCK(ec);
   4395 			/*
   4396 			 * We must listen to a range of multicast addresses.
   4397 			 * For now, just accept all multicasts, rather than
   4398 			 * trying to set only those filter bits needed to match
   4399 			 * the range.  (At this time, the only use of address
   4400 			 * ranges is for IP multicast routing, for which the
   4401 			 * range is big enough to require all bits set.)
   4402 			 */
   4403 			goto allmulti;
   4404 		}
   4405 
   4406 		hash = wm_mchash(sc, enm->enm_addrlo);
   4407 
   4408 		reg = (hash >> 5);
   4409 		if ((sc->sc_type == WM_T_ICH8) || (sc->sc_type == WM_T_ICH9)
   4410 		    || (sc->sc_type == WM_T_ICH10) || (sc->sc_type == WM_T_PCH)
   4411 		    || (sc->sc_type == WM_T_PCH2)
   4412 		    || (sc->sc_type == WM_T_PCH_LPT)
   4413 		    || (sc->sc_type == WM_T_PCH_SPT)
   4414 		    || (sc->sc_type == WM_T_PCH_CNP))
   4415 			reg &= 0x1f;
   4416 		else
   4417 			reg &= 0x7f;
   4418 		bit = hash & 0x1f;
   4419 
   4420 		hash = CSR_READ(sc, mta_reg + (reg << 2));
   4421 		hash |= 1U << bit;
   4422 
   4423 		if (sc->sc_type == WM_T_82544 && (reg & 1) != 0) {
   4424 			/*
   4425 			 * 82544 Errata 9: Certain register cannot be written
   4426 			 * with particular alignments in PCI-X bus operation
   4427 			 * (FCAH, MTA and VFTA).
   4428 			 */
   4429 			bit = CSR_READ(sc, mta_reg + ((reg - 1) << 2));
   4430 			CSR_WRITE(sc, mta_reg + (reg << 2), hash);
   4431 			CSR_WRITE_FLUSH(sc);
   4432 			CSR_WRITE(sc, mta_reg + ((reg - 1) << 2), bit);
   4433 			CSR_WRITE_FLUSH(sc);
   4434 		} else {
   4435 			CSR_WRITE(sc, mta_reg + (reg << 2), hash);
   4436 			CSR_WRITE_FLUSH(sc);
   4437 		}
   4438 
   4439 		ETHER_NEXT_MULTI(step, enm);
   4440 	}
   4441 	ec->ec_flags &= ~ETHER_F_ALLMULTI;
   4442 	ETHER_UNLOCK(ec);
   4443 
   4444 	goto setit;
   4445 
   4446  allmulti:
   4447 	sc->sc_rctl |= RCTL_MPE;
   4448 
   4449  setit:
   4450 	if (sc->sc_type >= WM_T_PCH2) {
   4451 		if (((ec->ec_capabilities & ETHERCAP_JUMBO_MTU) != 0)
   4452 		    && (ifp->if_mtu > ETHERMTU))
   4453 			rv = wm_lv_jumbo_workaround_ich8lan(sc, true);
   4454 		else
   4455 			rv = wm_lv_jumbo_workaround_ich8lan(sc, false);
   4456 		if (rv != 0)
   4457 			device_printf(sc->sc_dev,
   4458 			    "Failed to do workaround for jumbo frame.\n");
   4459 	}
   4460 
   4461 	CSR_WRITE(sc, WMREG_RCTL, sc->sc_rctl);
   4462 }
   4463 
   4464 /* Reset and init related */
   4465 
   4466 static void
   4467 wm_set_vlan(struct wm_softc *sc)
   4468 {
   4469 
   4470 	DPRINTF(sc, WM_DEBUG_INIT, ("%s: %s called\n",
   4471 		device_xname(sc->sc_dev), __func__));
   4472 
   4473 	/* Deal with VLAN enables. */
   4474 	if (VLAN_ATTACHED(&sc->sc_ethercom))
   4475 		sc->sc_ctrl |= CTRL_VME;
   4476 	else
   4477 		sc->sc_ctrl &= ~CTRL_VME;
   4478 
   4479 	/* Write the control registers. */
   4480 	CSR_WRITE(sc, WMREG_CTRL, sc->sc_ctrl);
   4481 }
   4482 
   4483 static void
   4484 wm_set_pcie_completion_timeout(struct wm_softc *sc)
   4485 {
   4486 	uint32_t gcr;
   4487 	pcireg_t ctrl2;
   4488 
   4489 	gcr = CSR_READ(sc, WMREG_GCR);
   4490 
   4491 	/* Only take action if timeout value is defaulted to 0 */
   4492 	if ((gcr & GCR_CMPL_TMOUT_MASK) != 0)
   4493 		goto out;
   4494 
   4495 	if ((gcr & GCR_CAP_VER2) == 0) {
   4496 		gcr |= GCR_CMPL_TMOUT_10MS;
   4497 		goto out;
   4498 	}
   4499 
   4500 	ctrl2 = pci_conf_read(sc->sc_pc, sc->sc_pcitag,
   4501 	    sc->sc_pcixe_capoff + PCIE_DCSR2);
   4502 	ctrl2 |= WM_PCIE_DCSR2_16MS;
   4503 	pci_conf_write(sc->sc_pc, sc->sc_pcitag,
   4504 	    sc->sc_pcixe_capoff + PCIE_DCSR2, ctrl2);
   4505 
   4506 out:
   4507 	/* Disable completion timeout resend */
   4508 	gcr &= ~GCR_CMPL_TMOUT_RESEND;
   4509 
   4510 	CSR_WRITE(sc, WMREG_GCR, gcr);
   4511 }
   4512 
   4513 void
   4514 wm_get_auto_rd_done(struct wm_softc *sc)
   4515 {
   4516 	int i;
   4517 
   4518 	/* wait for eeprom to reload */
   4519 	switch (sc->sc_type) {
   4520 	case WM_T_82571:
   4521 	case WM_T_82572:
   4522 	case WM_T_82573:
   4523 	case WM_T_82574:
   4524 	case WM_T_82583:
   4525 	case WM_T_82575:
   4526 	case WM_T_82576:
   4527 	case WM_T_82580:
   4528 	case WM_T_I350:
   4529 	case WM_T_I354:
   4530 	case WM_T_I210:
   4531 	case WM_T_I211:
   4532 	case WM_T_80003:
   4533 	case WM_T_ICH8:
   4534 	case WM_T_ICH9:
   4535 		for (i = 0; i < 10; i++) {
   4536 			if (CSR_READ(sc, WMREG_EECD) & EECD_EE_AUTORD)
   4537 				break;
   4538 			delay(1000);
   4539 		}
   4540 		if (i == 10) {
   4541 			log(LOG_ERR, "%s: auto read from eeprom failed to "
   4542 			    "complete\n", device_xname(sc->sc_dev));
   4543 		}
   4544 		break;
   4545 	default:
   4546 		break;
   4547 	}
   4548 }
   4549 
   4550 void
   4551 wm_lan_init_done(struct wm_softc *sc)
   4552 {
   4553 	uint32_t reg = 0;
   4554 	int i;
   4555 
   4556 	DPRINTF(sc, WM_DEBUG_INIT, ("%s: %s called\n",
   4557 		device_xname(sc->sc_dev), __func__));
   4558 
   4559 	/* Wait for eeprom to reload */
   4560 	switch (sc->sc_type) {
   4561 	case WM_T_ICH10:
   4562 	case WM_T_PCH:
   4563 	case WM_T_PCH2:
   4564 	case WM_T_PCH_LPT:
   4565 	case WM_T_PCH_SPT:
   4566 	case WM_T_PCH_CNP:
   4567 		for (i = 0; i < WM_ICH8_LAN_INIT_TIMEOUT; i++) {
   4568 			reg = CSR_READ(sc, WMREG_STATUS);
   4569 			if ((reg & STATUS_LAN_INIT_DONE) != 0)
   4570 				break;
   4571 			delay(100);
   4572 		}
   4573 		if (i >= WM_ICH8_LAN_INIT_TIMEOUT) {
   4574 			log(LOG_ERR, "%s: %s: lan_init_done failed to "
   4575 			    "complete\n", device_xname(sc->sc_dev), __func__);
   4576 		}
   4577 		break;
   4578 	default:
   4579 		panic("%s: %s: unknown type\n", device_xname(sc->sc_dev),
   4580 		    __func__);
   4581 		break;
   4582 	}
   4583 
   4584 	reg &= ~STATUS_LAN_INIT_DONE;
   4585 	CSR_WRITE(sc, WMREG_STATUS, reg);
   4586 }
   4587 
   4588 void
   4589 wm_get_cfg_done(struct wm_softc *sc)
   4590 {
   4591 	int mask;
   4592 	uint32_t reg;
   4593 	int i;
   4594 
   4595 	DPRINTF(sc, WM_DEBUG_INIT, ("%s: %s called\n",
   4596 		device_xname(sc->sc_dev), __func__));
   4597 
   4598 	/* Wait for eeprom to reload */
   4599 	switch (sc->sc_type) {
   4600 	case WM_T_82542_2_0:
   4601 	case WM_T_82542_2_1:
   4602 		/* null */
   4603 		break;
   4604 	case WM_T_82543:
   4605 	case WM_T_82544:
   4606 	case WM_T_82540:
   4607 	case WM_T_82545:
   4608 	case WM_T_82545_3:
   4609 	case WM_T_82546:
   4610 	case WM_T_82546_3:
   4611 	case WM_T_82541:
   4612 	case WM_T_82541_2:
   4613 	case WM_T_82547:
   4614 	case WM_T_82547_2:
   4615 	case WM_T_82573:
   4616 	case WM_T_82574:
   4617 	case WM_T_82583:
   4618 		/* generic */
   4619 		delay(10*1000);
   4620 		break;
   4621 	case WM_T_80003:
   4622 	case WM_T_82571:
   4623 	case WM_T_82572:
   4624 	case WM_T_82575:
   4625 	case WM_T_82576:
   4626 	case WM_T_82580:
   4627 	case WM_T_I350:
   4628 	case WM_T_I354:
   4629 	case WM_T_I210:
   4630 	case WM_T_I211:
   4631 		if (sc->sc_type == WM_T_82571) {
   4632 			/* Only 82571 shares port 0 */
   4633 			mask = EEMNGCTL_CFGDONE_0;
   4634 		} else
   4635 			mask = EEMNGCTL_CFGDONE_0 << sc->sc_funcid;
   4636 		for (i = 0; i < WM_PHY_CFG_TIMEOUT; i++) {
   4637 			if (CSR_READ(sc, WMREG_EEMNGCTL) & mask)
   4638 				break;
   4639 			delay(1000);
   4640 		}
   4641 		if (i >= WM_PHY_CFG_TIMEOUT)
   4642 			DPRINTF(sc, WM_DEBUG_GMII, ("%s: %s failed\n",
   4643 				device_xname(sc->sc_dev), __func__));
   4644 		break;
   4645 	case WM_T_ICH8:
   4646 	case WM_T_ICH9:
   4647 	case WM_T_ICH10:
   4648 	case WM_T_PCH:
   4649 	case WM_T_PCH2:
   4650 	case WM_T_PCH_LPT:
   4651 	case WM_T_PCH_SPT:
   4652 	case WM_T_PCH_CNP:
   4653 		delay(10*1000);
   4654 		if (sc->sc_type >= WM_T_ICH10)
   4655 			wm_lan_init_done(sc);
   4656 		else
   4657 			wm_get_auto_rd_done(sc);
   4658 
   4659 		/* Clear PHY Reset Asserted bit */
   4660 		reg = CSR_READ(sc, WMREG_STATUS);
   4661 		if ((reg & STATUS_PHYRA) != 0)
   4662 			CSR_WRITE(sc, WMREG_STATUS, reg & ~STATUS_PHYRA);
   4663 		break;
   4664 	default:
   4665 		panic("%s: %s: unknown type\n", device_xname(sc->sc_dev),
   4666 		    __func__);
   4667 		break;
   4668 	}
   4669 }
   4670 
   4671 int
   4672 wm_phy_post_reset(struct wm_softc *sc)
   4673 {
   4674 	device_t dev = sc->sc_dev;
   4675 	uint16_t reg;
   4676 	int rv = 0;
   4677 
   4678 	/* This function is only for ICH8 and newer. */
   4679 	if (sc->sc_type < WM_T_ICH8)
   4680 		return 0;
   4681 
   4682 	if (wm_phy_resetisblocked(sc)) {
   4683 		/* XXX */
   4684 		device_printf(dev, "PHY is blocked\n");
   4685 		return -1;
   4686 	}
   4687 
   4688 	/* Allow time for h/w to get to quiescent state after reset */
   4689 	delay(10*1000);
   4690 
   4691 	/* Perform any necessary post-reset workarounds */
   4692 	if (sc->sc_type == WM_T_PCH)
   4693 		rv = wm_hv_phy_workarounds_ich8lan(sc);
   4694 	else if (sc->sc_type == WM_T_PCH2)
   4695 		rv = wm_lv_phy_workarounds_ich8lan(sc);
   4696 	if (rv != 0)
   4697 		return rv;
   4698 
   4699 	/* Clear the host wakeup bit after lcd reset */
   4700 	if (sc->sc_type >= WM_T_PCH) {
   4701 		wm_gmii_hv_readreg(dev, 2, BM_PORT_GEN_CFG, &reg);
   4702 		reg &= ~BM_WUC_HOST_WU_BIT;
   4703 		wm_gmii_hv_writereg(dev, 2, BM_PORT_GEN_CFG, reg);
   4704 	}
   4705 
   4706 	/* Configure the LCD with the extended configuration region in NVM */
   4707 	if ((rv = wm_init_lcd_from_nvm(sc)) != 0)
   4708 		return rv;
   4709 
   4710 	/* Configure the LCD with the OEM bits in NVM */
   4711 	rv = wm_oem_bits_config_ich8lan(sc, true);
   4712 
   4713 	if (sc->sc_type == WM_T_PCH2) {
   4714 		/* Ungate automatic PHY configuration on non-managed 82579 */
   4715 		if ((CSR_READ(sc, WMREG_FWSM) & FWSM_FW_VALID) == 0) {
   4716 			delay(10 * 1000);
   4717 			wm_gate_hw_phy_config_ich8lan(sc, false);
   4718 		}
   4719 		/* Set EEE LPI Update Timer to 200usec */
   4720 		rv = sc->phy.acquire(sc);
   4721 		if (rv)
   4722 			return rv;
   4723 		rv = wm_write_emi_reg_locked(dev,
   4724 		    I82579_LPI_UPDATE_TIMER, 0x1387);
   4725 		sc->phy.release(sc);
   4726 	}
   4727 
   4728 	return rv;
   4729 }
   4730 
   4731 /* Only for PCH and newer */
   4732 static int
   4733 wm_write_smbus_addr(struct wm_softc *sc)
   4734 {
   4735 	uint32_t strap, freq;
   4736 	uint16_t phy_data;
   4737 	int rv;
   4738 
   4739 	DPRINTF(sc, WM_DEBUG_INIT, ("%s: %s called\n",
   4740 		device_xname(sc->sc_dev), __func__));
   4741 	KASSERT(CSR_READ(sc, WMREG_EXTCNFCTR) & EXTCNFCTR_MDIO_SW_OWNERSHIP);
   4742 
   4743 	strap = CSR_READ(sc, WMREG_STRAP);
   4744 	freq = __SHIFTOUT(strap, STRAP_FREQ);
   4745 
   4746 	rv = wm_gmii_hv_readreg_locked(sc->sc_dev, 2, HV_SMB_ADDR, &phy_data);
   4747 	if (rv != 0)
   4748 		return -1;
   4749 
   4750 	phy_data &= ~HV_SMB_ADDR_ADDR;
   4751 	phy_data |= __SHIFTOUT(strap, STRAP_SMBUSADDR);
   4752 	phy_data |= HV_SMB_ADDR_PEC_EN | HV_SMB_ADDR_VALID;
   4753 
   4754 	if (sc->sc_phytype == WMPHY_I217) {
   4755 		/* Restore SMBus frequency */
   4756 		if (freq --) {
   4757 			phy_data &= ~(HV_SMB_ADDR_FREQ_LOW
   4758 			    | HV_SMB_ADDR_FREQ_HIGH);
   4759 			phy_data |= __SHIFTIN((freq & 0x01) != 0,
   4760 			    HV_SMB_ADDR_FREQ_LOW);
   4761 			phy_data |= __SHIFTIN((freq & 0x02) != 0,
   4762 			    HV_SMB_ADDR_FREQ_HIGH);
   4763 		} else
   4764 			DPRINTF(sc, WM_DEBUG_INIT,
   4765 			    ("%s: %s Unsupported SMB frequency in PHY\n",
   4766 				device_xname(sc->sc_dev), __func__));
   4767 	}
   4768 
   4769 	return wm_gmii_hv_writereg_locked(sc->sc_dev, 2, HV_SMB_ADDR,
   4770 	    phy_data);
   4771 }
   4772 
   4773 static int
   4774 wm_init_lcd_from_nvm(struct wm_softc *sc)
   4775 {
   4776 	uint32_t extcnfctr, sw_cfg_mask, cnf_size, word_addr, i, reg;
   4777 	uint16_t phy_page = 0;
   4778 	int rv = 0;
   4779 
   4780 	DPRINTF(sc, WM_DEBUG_INIT, ("%s: %s called\n",
   4781 		device_xname(sc->sc_dev), __func__));
   4782 
   4783 	switch (sc->sc_type) {
   4784 	case WM_T_ICH8:
   4785 		if ((sc->sc_phytype == WMPHY_UNKNOWN)
   4786 		    || (sc->sc_phytype != WMPHY_IGP_3))
   4787 			return 0;
   4788 
   4789 		if ((sc->sc_pcidevid == PCI_PRODUCT_INTEL_82801H_AMT)
   4790 		    || (sc->sc_pcidevid == PCI_PRODUCT_INTEL_82801H_LAN)) {
   4791 			sw_cfg_mask = FEXTNVM_SW_CONFIG;
   4792 			break;
   4793 		}
   4794 		/* FALLTHROUGH */
   4795 	case WM_T_PCH:
   4796 	case WM_T_PCH2:
   4797 	case WM_T_PCH_LPT:
   4798 	case WM_T_PCH_SPT:
   4799 	case WM_T_PCH_CNP:
   4800 		sw_cfg_mask = FEXTNVM_SW_CONFIG_ICH8M;
   4801 		break;
   4802 	default:
   4803 		return 0;
   4804 	}
   4805 
   4806 	if ((rv = sc->phy.acquire(sc)) != 0)
   4807 		return rv;
   4808 
   4809 	reg = CSR_READ(sc, WMREG_FEXTNVM);
   4810 	if ((reg & sw_cfg_mask) == 0)
   4811 		goto release;
   4812 
   4813 	/*
   4814 	 * Make sure HW does not configure LCD from PHY extended configuration
   4815 	 * before SW configuration
   4816 	 */
   4817 	extcnfctr = CSR_READ(sc, WMREG_EXTCNFCTR);
   4818 	if ((sc->sc_type < WM_T_PCH2)
   4819 	    && ((extcnfctr & EXTCNFCTR_PCIE_WRITE_ENABLE) != 0))
   4820 		goto release;
   4821 
   4822 	DPRINTF(sc, WM_DEBUG_INIT, ("%s: %s: Configure LCD by software\n",
   4823 		device_xname(sc->sc_dev), __func__));
   4824 	/* word_addr is in DWORD */
   4825 	word_addr = __SHIFTOUT(extcnfctr, EXTCNFCTR_EXT_CNF_POINTER) << 1;
   4826 
   4827 	reg = CSR_READ(sc, WMREG_EXTCNFSIZE);
   4828 	cnf_size = __SHIFTOUT(reg, EXTCNFSIZE_LENGTH);
   4829 	if (cnf_size == 0)
   4830 		goto release;
   4831 
   4832 	if (((sc->sc_type == WM_T_PCH)
   4833 		&& ((extcnfctr & EXTCNFCTR_OEM_WRITE_ENABLE) == 0))
   4834 	    || (sc->sc_type > WM_T_PCH)) {
   4835 		/*
   4836 		 * HW configures the SMBus address and LEDs when the OEM and
   4837 		 * LCD Write Enable bits are set in the NVM. When both NVM bits
   4838 		 * are cleared, SW will configure them instead.
   4839 		 */
   4840 		DPRINTF(sc, WM_DEBUG_INIT, ("%s: %s: Configure SMBus and LED\n",
   4841 			device_xname(sc->sc_dev), __func__));
   4842 		if ((rv = wm_write_smbus_addr(sc)) != 0)
   4843 			goto release;
   4844 
   4845 		reg = CSR_READ(sc, WMREG_LEDCTL);
   4846 		rv = wm_gmii_hv_writereg_locked(sc->sc_dev, 1, HV_LED_CONFIG,
   4847 		    (uint16_t)reg);
   4848 		if (rv != 0)
   4849 			goto release;
   4850 	}
   4851 
   4852 	/* Configure LCD from extended configuration region. */
   4853 	for (i = 0; i < cnf_size; i++) {
   4854 		uint16_t reg_data, reg_addr;
   4855 
   4856 		if (wm_nvm_read(sc, (word_addr + i * 2), 1, &reg_data) != 0)
   4857 			goto release;
   4858 
   4859 		if (wm_nvm_read(sc, (word_addr + i * 2 + 1), 1, &reg_addr) !=0)
   4860 			goto release;
   4861 
   4862 		if (reg_addr == IGPHY_PAGE_SELECT)
   4863 			phy_page = reg_data;
   4864 
   4865 		reg_addr &= IGPHY_MAXREGADDR;
   4866 		reg_addr |= phy_page;
   4867 
   4868 		KASSERT(sc->phy.writereg_locked != NULL);
   4869 		rv = sc->phy.writereg_locked(sc->sc_dev, 1, reg_addr,
   4870 		    reg_data);
   4871 	}
   4872 
   4873 release:
   4874 	sc->phy.release(sc);
   4875 	return rv;
   4876 }
   4877 
   4878 /*
   4879  *  wm_oem_bits_config_ich8lan - SW-based LCD Configuration
   4880  *  @sc:       pointer to the HW structure
   4881  *  @d0_state: boolean if entering d0 or d3 device state
   4882  *
   4883  *  SW will configure Gbe Disable and LPLU based on the NVM. The four bits are
   4884  *  collectively called OEM bits.  The OEM Write Enable bit and SW Config bit
   4885  *  in NVM determines whether HW should configure LPLU and Gbe Disable.
   4886  */
   4887 int
   4888 wm_oem_bits_config_ich8lan(struct wm_softc *sc, bool d0_state)
   4889 {
   4890 	uint32_t mac_reg;
   4891 	uint16_t oem_reg;
   4892 	int rv;
   4893 
   4894 	if (sc->sc_type < WM_T_PCH)
   4895 		return 0;
   4896 
   4897 	rv = sc->phy.acquire(sc);
   4898 	if (rv != 0)
   4899 		return rv;
   4900 
   4901 	if (sc->sc_type == WM_T_PCH) {
   4902 		mac_reg = CSR_READ(sc, WMREG_EXTCNFCTR);
   4903 		if ((mac_reg & EXTCNFCTR_OEM_WRITE_ENABLE) != 0)
   4904 			goto release;
   4905 	}
   4906 
   4907 	mac_reg = CSR_READ(sc, WMREG_FEXTNVM);
   4908 	if ((mac_reg & FEXTNVM_SW_CONFIG_ICH8M) == 0)
   4909 		goto release;
   4910 
   4911 	mac_reg = CSR_READ(sc, WMREG_PHY_CTRL);
   4912 
   4913 	rv = wm_gmii_hv_readreg_locked(sc->sc_dev, 1, HV_OEM_BITS, &oem_reg);
   4914 	if (rv != 0)
   4915 		goto release;
   4916 	oem_reg &= ~(HV_OEM_BITS_A1KDIS | HV_OEM_BITS_LPLU);
   4917 
   4918 	if (d0_state) {
   4919 		if ((mac_reg & PHY_CTRL_GBE_DIS) != 0)
   4920 			oem_reg |= HV_OEM_BITS_A1KDIS;
   4921 		if ((mac_reg & PHY_CTRL_D0A_LPLU) != 0)
   4922 			oem_reg |= HV_OEM_BITS_LPLU;
   4923 	} else {
   4924 		if ((mac_reg & (PHY_CTRL_GBE_DIS | PHY_CTRL_NOND0A_GBE_DIS))
   4925 		    != 0)
   4926 			oem_reg |= HV_OEM_BITS_A1KDIS;
   4927 		if ((mac_reg & (PHY_CTRL_D0A_LPLU | PHY_CTRL_NOND0A_LPLU))
   4928 		    != 0)
   4929 			oem_reg |= HV_OEM_BITS_LPLU;
   4930 	}
   4931 
   4932 	/* Set Restart auto-neg to activate the bits */
   4933 	if ((d0_state || (sc->sc_type != WM_T_PCH))
   4934 	    && (wm_phy_resetisblocked(sc) == false))
   4935 		oem_reg |= HV_OEM_BITS_ANEGNOW;
   4936 
   4937 	rv = wm_gmii_hv_writereg_locked(sc->sc_dev, 1, HV_OEM_BITS, oem_reg);
   4938 
   4939 release:
   4940 	sc->phy.release(sc);
   4941 
   4942 	return rv;
   4943 }
   4944 
   4945 /* Init hardware bits */
   4946 void
   4947 wm_initialize_hardware_bits(struct wm_softc *sc)
   4948 {
   4949 	uint32_t tarc0, tarc1, reg;
   4950 
   4951 	DPRINTF(sc, WM_DEBUG_INIT, ("%s: %s called\n",
   4952 		device_xname(sc->sc_dev), __func__));
   4953 
   4954 	/* For 82571 variant, 80003 and ICHs */
   4955 	if (((sc->sc_type >= WM_T_82571) && (sc->sc_type <= WM_T_82583))
   4956 	    || (sc->sc_type >= WM_T_80003)) {
   4957 
   4958 		/* Transmit Descriptor Control 0 */
   4959 		reg = CSR_READ(sc, WMREG_TXDCTL(0));
   4960 		reg |= TXDCTL_COUNT_DESC;
   4961 		CSR_WRITE(sc, WMREG_TXDCTL(0), reg);
   4962 
   4963 		/* Transmit Descriptor Control 1 */
   4964 		reg = CSR_READ(sc, WMREG_TXDCTL(1));
   4965 		reg |= TXDCTL_COUNT_DESC;
   4966 		CSR_WRITE(sc, WMREG_TXDCTL(1), reg);
   4967 
   4968 		/* TARC0 */
   4969 		tarc0 = CSR_READ(sc, WMREG_TARC0);
   4970 		switch (sc->sc_type) {
   4971 		case WM_T_82571:
   4972 		case WM_T_82572:
   4973 		case WM_T_82573:
   4974 		case WM_T_82574:
   4975 		case WM_T_82583:
   4976 		case WM_T_80003:
   4977 			/* Clear bits 30..27 */
   4978 			tarc0 &= ~__BITS(30, 27);
   4979 			break;
   4980 		default:
   4981 			break;
   4982 		}
   4983 
   4984 		switch (sc->sc_type) {
   4985 		case WM_T_82571:
   4986 		case WM_T_82572:
   4987 			tarc0 |= __BITS(26, 23); /* TARC0 bits 23-26 */
   4988 
   4989 			tarc1 = CSR_READ(sc, WMREG_TARC1);
   4990 			tarc1 &= ~__BITS(30, 29); /* Clear bits 30 and 29 */
   4991 			tarc1 |= __BITS(26, 24); /* TARC1 bits 26-24 */
   4992 			/* 8257[12] Errata No.7 */
   4993 			tarc1 |= __BIT(22); /* TARC1 bits 22 */
   4994 
   4995 			/* TARC1 bit 28 */
   4996 			if ((CSR_READ(sc, WMREG_TCTL) & TCTL_MULR) != 0)
   4997 				tarc1 &= ~__BIT(28);
   4998 			else
   4999 				tarc1 |= __BIT(28);
   5000 			CSR_WRITE(sc, WMREG_TARC1, tarc1);
   5001 
   5002 			/*
   5003 			 * 8257[12] Errata No.13
   5004 			 * Disable Dyamic Clock Gating.
   5005 			 */
   5006 			reg = CSR_READ(sc, WMREG_CTRL_EXT);
   5007 			reg &= ~CTRL_EXT_DMA_DYN_CLK;
   5008 			CSR_WRITE(sc, WMREG_CTRL_EXT, reg);
   5009 			break;
   5010 		case WM_T_82573:
   5011 		case WM_T_82574:
   5012 		case WM_T_82583:
   5013 			if ((sc->sc_type == WM_T_82574)
   5014 			    || (sc->sc_type == WM_T_82583))
   5015 				tarc0 |= __BIT(26); /* TARC0 bit 26 */
   5016 
   5017 			/* Extended Device Control */
   5018 			reg = CSR_READ(sc, WMREG_CTRL_EXT);
   5019 			reg &= ~__BIT(23);	/* Clear bit 23 */
   5020 			reg |= __BIT(22);	/* Set bit 22 */
   5021 			CSR_WRITE(sc, WMREG_CTRL_EXT, reg);
   5022 
   5023 			/* Device Control */
   5024 			sc->sc_ctrl &= ~__BIT(29);	/* Clear bit 29 */
   5025 			CSR_WRITE(sc, WMREG_CTRL, sc->sc_ctrl);
   5026 
   5027 			/* PCIe Control Register */
   5028 			/*
   5029 			 * 82573 Errata (unknown).
   5030 			 *
   5031 			 * 82574 Errata 25 and 82583 Errata 12
   5032 			 * "Dropped Rx Packets":
   5033 			 *   NVM Image Version 2.1.4 and newer has no this bug.
   5034 			 */
   5035 			reg = CSR_READ(sc, WMREG_GCR);
   5036 			reg |= GCR_L1_ACT_WITHOUT_L0S_RX;
   5037 			CSR_WRITE(sc, WMREG_GCR, reg);
   5038 
   5039 			if ((sc->sc_type == WM_T_82574)
   5040 			    || (sc->sc_type == WM_T_82583)) {
   5041 				/*
   5042 				 * Document says this bit must be set for
   5043 				 * proper operation.
   5044 				 */
   5045 				reg = CSR_READ(sc, WMREG_GCR);
   5046 				reg |= __BIT(22);
   5047 				CSR_WRITE(sc, WMREG_GCR, reg);
   5048 
   5049 				/*
   5050 				 * Apply workaround for hardware errata
   5051 				 * documented in errata docs Fixes issue where
   5052 				 * some error prone or unreliable PCIe
   5053 				 * completions are occurring, particularly
   5054 				 * with ASPM enabled. Without fix, issue can
   5055 				 * cause Tx timeouts.
   5056 				 */
   5057 				reg = CSR_READ(sc, WMREG_GCR2);
   5058 				reg |= __BIT(0);
   5059 				CSR_WRITE(sc, WMREG_GCR2, reg);
   5060 			}
   5061 			break;
   5062 		case WM_T_80003:
   5063 			/* TARC0 */
   5064 			if ((sc->sc_mediatype == WM_MEDIATYPE_FIBER)
   5065 			    || (sc->sc_mediatype == WM_MEDIATYPE_SERDES))
   5066 				tarc0 &= ~__BIT(20); /* Clear bits 20 */
   5067 
   5068 			/* TARC1 bit 28 */
   5069 			tarc1 = CSR_READ(sc, WMREG_TARC1);
   5070 			if ((CSR_READ(sc, WMREG_TCTL) & TCTL_MULR) != 0)
   5071 				tarc1 &= ~__BIT(28);
   5072 			else
   5073 				tarc1 |= __BIT(28);
   5074 			CSR_WRITE(sc, WMREG_TARC1, tarc1);
   5075 			break;
   5076 		case WM_T_ICH8:
   5077 		case WM_T_ICH9:
   5078 		case WM_T_ICH10:
   5079 		case WM_T_PCH:
   5080 		case WM_T_PCH2:
   5081 		case WM_T_PCH_LPT:
   5082 		case WM_T_PCH_SPT:
   5083 		case WM_T_PCH_CNP:
   5084 			/* TARC0 */
   5085 			if (sc->sc_type == WM_T_ICH8) {
   5086 				/* Set TARC0 bits 29 and 28 */
   5087 				tarc0 |= __BITS(29, 28);
   5088 			} else if (sc->sc_type == WM_T_PCH_SPT) {
   5089 				tarc0 |= __BIT(29);
   5090 				/*
   5091 				 *  Drop bit 28. From Linux.
   5092 				 * See I218/I219 spec update
   5093 				 * "5. Buffer Overrun While the I219 is
   5094 				 * Processing DMA Transactions"
   5095 				 */
   5096 				tarc0 &= ~__BIT(28);
   5097 			}
   5098 			/* Set TARC0 bits 23,24,26,27 */
   5099 			tarc0 |= __BITS(27, 26) | __BITS(24, 23);
   5100 
   5101 			/* CTRL_EXT */
   5102 			reg = CSR_READ(sc, WMREG_CTRL_EXT);
   5103 			reg |= __BIT(22);	/* Set bit 22 */
   5104 			/*
   5105 			 * Enable PHY low-power state when MAC is at D3
   5106 			 * w/o WoL
   5107 			 */
   5108 			if (sc->sc_type >= WM_T_PCH)
   5109 				reg |= CTRL_EXT_PHYPDEN;
   5110 			CSR_WRITE(sc, WMREG_CTRL_EXT, reg);
   5111 
   5112 			/* TARC1 */
   5113 			tarc1 = CSR_READ(sc, WMREG_TARC1);
   5114 			/* bit 28 */
   5115 			if ((CSR_READ(sc, WMREG_TCTL) & TCTL_MULR) != 0)
   5116 				tarc1 &= ~__BIT(28);
   5117 			else
   5118 				tarc1 |= __BIT(28);
   5119 			tarc1 |= __BIT(24) | __BIT(26) | __BIT(30);
   5120 			CSR_WRITE(sc, WMREG_TARC1, tarc1);
   5121 
   5122 			/* Device Status */
   5123 			if (sc->sc_type == WM_T_ICH8) {
   5124 				reg = CSR_READ(sc, WMREG_STATUS);
   5125 				reg &= ~__BIT(31);
   5126 				CSR_WRITE(sc, WMREG_STATUS, reg);
   5127 
   5128 			}
   5129 
   5130 			/* IOSFPC */
   5131 			if (sc->sc_type == WM_T_PCH_SPT) {
   5132 				reg = CSR_READ(sc, WMREG_IOSFPC);
   5133 				reg |= RCTL_RDMTS_HEX; /* XXX RTCL bit? */
   5134 				CSR_WRITE(sc, WMREG_IOSFPC, reg);
   5135 			}
   5136 			/*
   5137 			 * Work-around descriptor data corruption issue during
   5138 			 * NFS v2 UDP traffic, just disable the NFS filtering
   5139 			 * capability.
   5140 			 */
   5141 			reg = CSR_READ(sc, WMREG_RFCTL);
   5142 			reg |= WMREG_RFCTL_NFSWDIS | WMREG_RFCTL_NFSRDIS;
   5143 			CSR_WRITE(sc, WMREG_RFCTL, reg);
   5144 			break;
   5145 		default:
   5146 			break;
   5147 		}
   5148 		CSR_WRITE(sc, WMREG_TARC0, tarc0);
   5149 
   5150 		switch (sc->sc_type) {
   5151 		/*
   5152 		 * 8257[12] Errata No.52, 82573 Errata No.43 and some others.
   5153 		 * Avoid RSS Hash Value bug.
   5154 		 */
   5155 		case WM_T_82571:
   5156 		case WM_T_82572:
   5157 		case WM_T_82573:
   5158 		case WM_T_80003:
   5159 		case WM_T_ICH8:
   5160 			reg = CSR_READ(sc, WMREG_RFCTL);
   5161 			reg |= WMREG_RFCTL_NEWIPV6EXDIS |WMREG_RFCTL_IPV6EXDIS;
   5162 			CSR_WRITE(sc, WMREG_RFCTL, reg);
   5163 			break;
   5164 		case WM_T_82574:
   5165 			/* Use extened Rx descriptor. */
   5166 			reg = CSR_READ(sc, WMREG_RFCTL);
   5167 			reg |= WMREG_RFCTL_EXSTEN;
   5168 			CSR_WRITE(sc, WMREG_RFCTL, reg);
   5169 			break;
   5170 		default:
   5171 			break;
   5172 		}
   5173 	} else if ((sc->sc_type >= WM_T_82575) && (sc->sc_type <= WM_T_I211)) {
   5174 		/*
   5175 		 * 82575 Errata XXX, 82576 Errata 46, 82580 Errata 24,
   5176 		 * I350 Errata 37, I210 Errata No. 31 and I211 Errata No. 11:
   5177 		 * "Certain Malformed IPv6 Extension Headers are Not Processed
   5178 		 * Correctly by the Device"
   5179 		 *
   5180 		 * I354(C2000) Errata AVR53:
   5181 		 * "Malformed IPv6 Extension Headers May Result in LAN Device
   5182 		 * Hang"
   5183 		 */
   5184 		reg = CSR_READ(sc, WMREG_RFCTL);
   5185 		reg |= WMREG_RFCTL_IPV6EXDIS;
   5186 		CSR_WRITE(sc, WMREG_RFCTL, reg);
   5187 	}
   5188 }
   5189 
   5190 static uint32_t
   5191 wm_rxpbs_adjust_82580(uint32_t val)
   5192 {
   5193 	uint32_t rv = 0;
   5194 
   5195 	if (val < __arraycount(wm_82580_rxpbs_table))
   5196 		rv = wm_82580_rxpbs_table[val];
   5197 
   5198 	return rv;
   5199 }
   5200 
   5201 /*
   5202  * wm_reset_phy:
   5203  *
   5204  *	generic PHY reset function.
   5205  *	Same as e1000_phy_hw_reset_generic()
   5206  */
   5207 static int
   5208 wm_reset_phy(struct wm_softc *sc)
   5209 {
   5210 	uint32_t reg;
   5211 
   5212 	DPRINTF(sc, WM_DEBUG_INIT, ("%s: %s called\n",
   5213 		device_xname(sc->sc_dev), __func__));
   5214 	if (wm_phy_resetisblocked(sc))
   5215 		return -1;
   5216 
   5217 	sc->phy.acquire(sc);
   5218 
   5219 	reg = CSR_READ(sc, WMREG_CTRL);
   5220 	CSR_WRITE(sc, WMREG_CTRL, reg | CTRL_PHY_RESET);
   5221 	CSR_WRITE_FLUSH(sc);
   5222 
   5223 	delay(sc->phy.reset_delay_us);
   5224 
   5225 	CSR_WRITE(sc, WMREG_CTRL, reg);
   5226 	CSR_WRITE_FLUSH(sc);
   5227 
   5228 	delay(150);
   5229 
   5230 	sc->phy.release(sc);
   5231 
   5232 	wm_get_cfg_done(sc);
   5233 	wm_phy_post_reset(sc);
   5234 
   5235 	return 0;
   5236 }
   5237 
   5238 /*
   5239  * wm_flush_desc_rings - remove all descriptors from the descriptor rings.
   5240  *
   5241  * In i219, the descriptor rings must be emptied before resetting the HW
   5242  * or before changing the device state to D3 during runtime (runtime PM).
   5243  *
   5244  * Failure to do this will cause the HW to enter a unit hang state which can
   5245  * only be released by PCI reset on the device.
   5246  *
   5247  * I219 does not use multiqueue, so it is enough to check sc->sc_queue[0] only.
   5248  */
   5249 static void
   5250 wm_flush_desc_rings(struct wm_softc *sc)
   5251 {
   5252 	pcireg_t preg;
   5253 	uint32_t reg;
   5254 	struct wm_txqueue *txq;
   5255 	wiseman_txdesc_t *txd;
   5256 	int nexttx;
   5257 	uint32_t rctl;
   5258 
   5259 	/* First, disable MULR fix in FEXTNVM11 */
   5260 	reg = CSR_READ(sc, WMREG_FEXTNVM11);
   5261 	reg |= FEXTNVM11_DIS_MULRFIX;
   5262 	CSR_WRITE(sc, WMREG_FEXTNVM11, reg);
   5263 
   5264 	preg = pci_conf_read(sc->sc_pc, sc->sc_pcitag, WM_PCI_DESCRING_STATUS);
   5265 	reg = CSR_READ(sc, WMREG_TDLEN(0));
   5266 	if (((preg & DESCRING_STATUS_FLUSH_REQ) == 0) || (reg == 0))
   5267 		return;
   5268 
   5269 	/*
   5270 	 * Remove all descriptors from the tx_ring.
   5271 	 *
   5272 	 * We want to clear all pending descriptors from the TX ring. Zeroing
   5273 	 * happens when the HW reads the regs. We assign the ring itself as
   5274 	 * the data of the next descriptor. We don't care about the data we are
   5275 	 * about to reset the HW.
   5276 	 */
   5277 #ifdef WM_DEBUG
   5278 	device_printf(sc->sc_dev, "Need TX flush (reg = %08x)\n", preg);
   5279 #endif
   5280 	reg = CSR_READ(sc, WMREG_TCTL);
   5281 	CSR_WRITE(sc, WMREG_TCTL, reg | TCTL_EN);
   5282 
   5283 	txq = &sc->sc_queue[0].wmq_txq;
   5284 	nexttx = txq->txq_next;
   5285 	txd = &txq->txq_descs[nexttx];
   5286 	wm_set_dma_addr(&txd->wtx_addr, txq->txq_desc_dma);
   5287 	txd->wtx_cmdlen = htole32(WTX_CMD_IFCS | 512);
   5288 	txd->wtx_fields.wtxu_status = 0;
   5289 	txd->wtx_fields.wtxu_options = 0;
   5290 	txd->wtx_fields.wtxu_vlan = 0;
   5291 
   5292 	wm_cdtxsync(txq, 0, WM_NTXDESC(txq),
   5293 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
   5294 
   5295 	txq->txq_next = WM_NEXTTX(txq, txq->txq_next);
   5296 	CSR_WRITE(sc, WMREG_TDT(0), txq->txq_next);
   5297 	CSR_WRITE_FLUSH(sc);
   5298 	delay(250);
   5299 
   5300 	preg = pci_conf_read(sc->sc_pc, sc->sc_pcitag, WM_PCI_DESCRING_STATUS);
   5301 	if ((preg & DESCRING_STATUS_FLUSH_REQ) == 0)
   5302 		return;
   5303 
   5304 	/*
   5305 	 * Mark all descriptors in the RX ring as consumed and disable the
   5306 	 * rx ring.
   5307 	 */
   5308 #ifdef WM_DEBUG
   5309 	device_printf(sc->sc_dev, "Need RX flush (reg = %08x)\n", preg);
   5310 #endif
   5311 	rctl = CSR_READ(sc, WMREG_RCTL);
   5312 	CSR_WRITE(sc, WMREG_RCTL, rctl & ~RCTL_EN);
   5313 	CSR_WRITE_FLUSH(sc);
   5314 	delay(150);
   5315 
   5316 	reg = CSR_READ(sc, WMREG_RXDCTL(0));
   5317 	/* Zero the lower 14 bits (prefetch and host thresholds) */
   5318 	reg &= 0xffffc000;
   5319 	/*
   5320 	 * Update thresholds: prefetch threshold to 31, host threshold
   5321 	 * to 1 and make sure the granularity is "descriptors" and not
   5322 	 * "cache lines"
   5323 	 */
   5324 	reg |= (0x1f | (1 << 8) | RXDCTL_GRAN);
   5325 	CSR_WRITE(sc, WMREG_RXDCTL(0), reg);
   5326 
   5327 	/* Momentarily enable the RX ring for the changes to take effect */
   5328 	CSR_WRITE(sc, WMREG_RCTL, rctl | RCTL_EN);
   5329 	CSR_WRITE_FLUSH(sc);
   5330 	delay(150);
   5331 	CSR_WRITE(sc, WMREG_RCTL, rctl & ~RCTL_EN);
   5332 }
   5333 
   5334 /*
   5335  * wm_reset:
   5336  *
   5337  *	Reset the i82542 chip.
   5338  */
   5339 static void
   5340 wm_reset(struct wm_softc *sc)
   5341 {
   5342 	int phy_reset = 0;
   5343 	int i, error = 0;
   5344 	uint32_t reg;
   5345 	uint16_t kmreg;
   5346 	int rv;
   5347 
   5348 	DPRINTF(sc, WM_DEBUG_INIT, ("%s: %s called\n",
   5349 		device_xname(sc->sc_dev), __func__));
   5350 	KASSERT(sc->sc_type != 0);
   5351 
   5352 	/*
   5353 	 * Allocate on-chip memory according to the MTU size.
   5354 	 * The Packet Buffer Allocation register must be written
   5355 	 * before the chip is reset.
   5356 	 */
   5357 	switch (sc->sc_type) {
   5358 	case WM_T_82547:
   5359 	case WM_T_82547_2:
   5360 		sc->sc_pba = sc->sc_ethercom.ec_if.if_mtu > 8192 ?
   5361 		    PBA_22K : PBA_30K;
   5362 		for (i = 0; i < sc->sc_nqueues; i++) {
   5363 			struct wm_txqueue *txq = &sc->sc_queue[i].wmq_txq;
   5364 			txq->txq_fifo_head = 0;
   5365 			txq->txq_fifo_addr = sc->sc_pba << PBA_ADDR_SHIFT;
   5366 			txq->txq_fifo_size =
   5367 			    (PBA_40K - sc->sc_pba) << PBA_BYTE_SHIFT;
   5368 			txq->txq_fifo_stall = 0;
   5369 		}
   5370 		break;
   5371 	case WM_T_82571:
   5372 	case WM_T_82572:
   5373 	case WM_T_82575:	/* XXX need special handing for jumbo frames */
   5374 	case WM_T_80003:
   5375 		sc->sc_pba = PBA_32K;
   5376 		break;
   5377 	case WM_T_82573:
   5378 		sc->sc_pba = PBA_12K;
   5379 		break;
   5380 	case WM_T_82574:
   5381 	case WM_T_82583:
   5382 		sc->sc_pba = PBA_20K;
   5383 		break;
   5384 	case WM_T_82576:
   5385 		sc->sc_pba = CSR_READ(sc, WMREG_RXPBS);
   5386 		sc->sc_pba &= RXPBS_SIZE_MASK_82576;
   5387 		break;
   5388 	case WM_T_82580:
   5389 	case WM_T_I350:
   5390 	case WM_T_I354:
   5391 		sc->sc_pba = wm_rxpbs_adjust_82580(CSR_READ(sc, WMREG_RXPBS));
   5392 		break;
   5393 	case WM_T_I210:
   5394 	case WM_T_I211:
   5395 		sc->sc_pba = PBA_34K;
   5396 		break;
   5397 	case WM_T_ICH8:
   5398 		/* Workaround for a bit corruption issue in FIFO memory */
   5399 		sc->sc_pba = PBA_8K;
   5400 		CSR_WRITE(sc, WMREG_PBS, PBA_16K);
   5401 		break;
   5402 	case WM_T_ICH9:
   5403 	case WM_T_ICH10:
   5404 		sc->sc_pba = sc->sc_ethercom.ec_if.if_mtu > 4096 ?
   5405 		    PBA_14K : PBA_10K;
   5406 		break;
   5407 	case WM_T_PCH:
   5408 	case WM_T_PCH2:	/* XXX 14K? */
   5409 	case WM_T_PCH_LPT:
   5410 	case WM_T_PCH_SPT:
   5411 	case WM_T_PCH_CNP:
   5412 		sc->sc_pba = sc->sc_ethercom.ec_if.if_mtu > 1500 ?
   5413 		    PBA_12K : PBA_26K;
   5414 		break;
   5415 	default:
   5416 		sc->sc_pba = sc->sc_ethercom.ec_if.if_mtu > 8192 ?
   5417 		    PBA_40K : PBA_48K;
   5418 		break;
   5419 	}
   5420 	/*
   5421 	 * Only old or non-multiqueue devices have the PBA register
   5422 	 * XXX Need special handling for 82575.
   5423 	 */
   5424 	if (((sc->sc_flags & WM_F_NEWQUEUE) == 0)
   5425 	    || (sc->sc_type == WM_T_82575))
   5426 		CSR_WRITE(sc, WMREG_PBA, sc->sc_pba);
   5427 
   5428 	/* Prevent the PCI-E bus from sticking */
   5429 	if (sc->sc_flags & WM_F_PCIE) {
   5430 		int timeout = 800;
   5431 
   5432 		sc->sc_ctrl |= CTRL_GIO_M_DIS;
   5433 		CSR_WRITE(sc, WMREG_CTRL, sc->sc_ctrl);
   5434 
   5435 		while (timeout--) {
   5436 			if ((CSR_READ(sc, WMREG_STATUS) & STATUS_GIO_M_ENA)
   5437 			    == 0)
   5438 				break;
   5439 			delay(100);
   5440 		}
   5441 		if (timeout == 0)
   5442 			device_printf(sc->sc_dev,
   5443 			    "failed to disable bus mastering\n");
   5444 	}
   5445 
   5446 	/* Set the completion timeout for interface */
   5447 	if ((sc->sc_type == WM_T_82575) || (sc->sc_type == WM_T_82576)
   5448 	    || (sc->sc_type == WM_T_82580)
   5449 	    || (sc->sc_type == WM_T_I350) || (sc->sc_type == WM_T_I354)
   5450 	    || (sc->sc_type == WM_T_I210) || (sc->sc_type == WM_T_I211))
   5451 		wm_set_pcie_completion_timeout(sc);
   5452 
   5453 	/* Clear interrupt */
   5454 	CSR_WRITE(sc, WMREG_IMC, 0xffffffffU);
   5455 	if (wm_is_using_msix(sc)) {
   5456 		if (sc->sc_type != WM_T_82574) {
   5457 			CSR_WRITE(sc, WMREG_EIMC, 0xffffffffU);
   5458 			CSR_WRITE(sc, WMREG_EIAC, 0);
   5459 		} else
   5460 			CSR_WRITE(sc, WMREG_EIAC_82574, 0);
   5461 	}
   5462 
   5463 	/* Stop the transmit and receive processes. */
   5464 	CSR_WRITE(sc, WMREG_RCTL, 0);
   5465 	sc->sc_rctl &= ~RCTL_EN;
   5466 	CSR_WRITE(sc, WMREG_TCTL, TCTL_PSP);
   5467 	CSR_WRITE_FLUSH(sc);
   5468 
   5469 	/* XXX set_tbi_sbp_82543() */
   5470 
   5471 	delay(10*1000);
   5472 
   5473 	/* Must acquire the MDIO ownership before MAC reset */
   5474 	switch (sc->sc_type) {
   5475 	case WM_T_82573:
   5476 	case WM_T_82574:
   5477 	case WM_T_82583:
   5478 		error = wm_get_hw_semaphore_82573(sc);
   5479 		break;
   5480 	default:
   5481 		break;
   5482 	}
   5483 
   5484 	/*
   5485 	 * 82541 Errata 29? & 82547 Errata 28?
   5486 	 * See also the description about PHY_RST bit in CTRL register
   5487 	 * in 8254x_GBe_SDM.pdf.
   5488 	 */
   5489 	if ((sc->sc_type == WM_T_82541) || (sc->sc_type == WM_T_82547)) {
   5490 		CSR_WRITE(sc, WMREG_CTRL,
   5491 		    CSR_READ(sc, WMREG_CTRL) | CTRL_PHY_RESET);
   5492 		CSR_WRITE_FLUSH(sc);
   5493 		delay(5000);
   5494 	}
   5495 
   5496 	switch (sc->sc_type) {
   5497 	case WM_T_82544: /* XXX check whether WM_F_IOH_VALID is set */
   5498 	case WM_T_82541:
   5499 	case WM_T_82541_2:
   5500 	case WM_T_82547:
   5501 	case WM_T_82547_2:
   5502 		/*
   5503 		 * On some chipsets, a reset through a memory-mapped write
   5504 		 * cycle can cause the chip to reset before completing the
   5505 		 * write cycle. This causes major headache that can be avoided
   5506 		 * by issuing the reset via indirect register writes through
   5507 		 * I/O space.
   5508 		 *
   5509 		 * So, if we successfully mapped the I/O BAR at attach time,
   5510 		 * use that. Otherwise, try our luck with a memory-mapped
   5511 		 * reset.
   5512 		 */
   5513 		if (sc->sc_flags & WM_F_IOH_VALID)
   5514 			wm_io_write(sc, WMREG_CTRL, CTRL_RST);
   5515 		else
   5516 			CSR_WRITE(sc, WMREG_CTRL, CTRL_RST);
   5517 		break;
   5518 	case WM_T_82545_3:
   5519 	case WM_T_82546_3:
   5520 		/* Use the shadow control register on these chips. */
   5521 		CSR_WRITE(sc, WMREG_CTRL_SHADOW, CTRL_RST);
   5522 		break;
   5523 	case WM_T_80003:
   5524 		reg = CSR_READ(sc, WMREG_CTRL) | CTRL_RST;
   5525 		sc->phy.acquire(sc);
   5526 		CSR_WRITE(sc, WMREG_CTRL, reg);
   5527 		sc->phy.release(sc);
   5528 		break;
   5529 	case WM_T_ICH8:
   5530 	case WM_T_ICH9:
   5531 	case WM_T_ICH10:
   5532 	case WM_T_PCH:
   5533 	case WM_T_PCH2:
   5534 	case WM_T_PCH_LPT:
   5535 	case WM_T_PCH_SPT:
   5536 	case WM_T_PCH_CNP:
   5537 		reg = CSR_READ(sc, WMREG_CTRL) | CTRL_RST;
   5538 		if (wm_phy_resetisblocked(sc) == false) {
   5539 			/*
   5540 			 * Gate automatic PHY configuration by hardware on
   5541 			 * non-managed 82579
   5542 			 */
   5543 			if ((sc->sc_type == WM_T_PCH2)
   5544 			    && ((CSR_READ(sc, WMREG_FWSM) & FWSM_FW_VALID)
   5545 				== 0))
   5546 				wm_gate_hw_phy_config_ich8lan(sc, true);
   5547 
   5548 			reg |= CTRL_PHY_RESET;
   5549 			phy_reset = 1;
   5550 		} else
   5551 			device_printf(sc->sc_dev, "XXX reset is blocked!!!\n");
   5552 		sc->phy.acquire(sc);
   5553 		CSR_WRITE(sc, WMREG_CTRL, reg);
   5554 		/* Don't insert a completion barrier when reset */
   5555 		delay(20*1000);
   5556 		mutex_exit(sc->sc_ich_phymtx);
   5557 		break;
   5558 	case WM_T_82580:
   5559 	case WM_T_I350:
   5560 	case WM_T_I354:
   5561 	case WM_T_I210:
   5562 	case WM_T_I211:
   5563 		CSR_WRITE(sc, WMREG_CTRL, CSR_READ(sc, WMREG_CTRL) | CTRL_RST);
   5564 		if (sc->sc_pcidevid != PCI_PRODUCT_INTEL_DH89XXCC_SGMII)
   5565 			CSR_WRITE_FLUSH(sc);
   5566 		delay(5000);
   5567 		break;
   5568 	case WM_T_82542_2_0:
   5569 	case WM_T_82542_2_1:
   5570 	case WM_T_82543:
   5571 	case WM_T_82540:
   5572 	case WM_T_82545:
   5573 	case WM_T_82546:
   5574 	case WM_T_82571:
   5575 	case WM_T_82572:
   5576 	case WM_T_82573:
   5577 	case WM_T_82574:
   5578 	case WM_T_82575:
   5579 	case WM_T_82576:
   5580 	case WM_T_82583:
   5581 	default:
   5582 		/* Everything else can safely use the documented method. */
   5583 		CSR_WRITE(sc, WMREG_CTRL, CSR_READ(sc, WMREG_CTRL) | CTRL_RST);
   5584 		break;
   5585 	}
   5586 
   5587 	/* Must release the MDIO ownership after MAC reset */
   5588 	switch (sc->sc_type) {
   5589 	case WM_T_82573:
   5590 	case WM_T_82574:
   5591 	case WM_T_82583:
   5592 		if (error == 0)
   5593 			wm_put_hw_semaphore_82573(sc);
   5594 		break;
   5595 	default:
   5596 		break;
   5597 	}
   5598 
   5599 	/* Set Phy Config Counter to 50msec */
   5600 	if (sc->sc_type == WM_T_PCH2) {
   5601 		reg = CSR_READ(sc, WMREG_FEXTNVM3);
   5602 		reg &= ~FEXTNVM3_PHY_CFG_COUNTER_MASK;
   5603 		reg |= FEXTNVM3_PHY_CFG_COUNTER_50MS;
   5604 		CSR_WRITE(sc, WMREG_FEXTNVM3, reg);
   5605 	}
   5606 
   5607 	if (phy_reset != 0)
   5608 		wm_get_cfg_done(sc);
   5609 
   5610 	/* Reload EEPROM */
   5611 	switch (sc->sc_type) {
   5612 	case WM_T_82542_2_0:
   5613 	case WM_T_82542_2_1:
   5614 	case WM_T_82543:
   5615 	case WM_T_82544:
   5616 		delay(10);
   5617 		reg = CSR_READ(sc, WMREG_CTRL_EXT) | CTRL_EXT_EE_RST;
   5618 		CSR_WRITE(sc, WMREG_CTRL_EXT, reg);
   5619 		CSR_WRITE_FLUSH(sc);
   5620 		delay(2000);
   5621 		break;
   5622 	case WM_T_82540:
   5623 	case WM_T_82545:
   5624 	case WM_T_82545_3:
   5625 	case WM_T_82546:
   5626 	case WM_T_82546_3:
   5627 		delay(5*1000);
   5628 		/* XXX Disable HW ARPs on ASF enabled adapters */
   5629 		break;
   5630 	case WM_T_82541:
   5631 	case WM_T_82541_2:
   5632 	case WM_T_82547:
   5633 	case WM_T_82547_2:
   5634 		delay(20000);
   5635 		/* XXX Disable HW ARPs on ASF enabled adapters */
   5636 		break;
   5637 	case WM_T_82571:
   5638 	case WM_T_82572:
   5639 	case WM_T_82573:
   5640 	case WM_T_82574:
   5641 	case WM_T_82583:
   5642 		if (sc->sc_flags & WM_F_EEPROM_FLASH) {
   5643 			delay(10);
   5644 			reg = CSR_READ(sc, WMREG_CTRL_EXT) | CTRL_EXT_EE_RST;
   5645 			CSR_WRITE(sc, WMREG_CTRL_EXT, reg);
   5646 			CSR_WRITE_FLUSH(sc);
   5647 		}
   5648 		/* check EECD_EE_AUTORD */
   5649 		wm_get_auto_rd_done(sc);
   5650 		/*
   5651 		 * Phy configuration from NVM just starts after EECD_AUTO_RD
   5652 		 * is set.
   5653 		 */
   5654 		if ((sc->sc_type == WM_T_82573) || (sc->sc_type == WM_T_82574)
   5655 		    || (sc->sc_type == WM_T_82583))
   5656 			delay(25*1000);
   5657 		break;
   5658 	case WM_T_82575:
   5659 	case WM_T_82576:
   5660 	case WM_T_82580:
   5661 	case WM_T_I350:
   5662 	case WM_T_I354:
   5663 	case WM_T_I210:
   5664 	case WM_T_I211:
   5665 	case WM_T_80003:
   5666 		/* check EECD_EE_AUTORD */
   5667 		wm_get_auto_rd_done(sc);
   5668 		break;
   5669 	case WM_T_ICH8:
   5670 	case WM_T_ICH9:
   5671 	case WM_T_ICH10:
   5672 	case WM_T_PCH:
   5673 	case WM_T_PCH2:
   5674 	case WM_T_PCH_LPT:
   5675 	case WM_T_PCH_SPT:
   5676 	case WM_T_PCH_CNP:
   5677 		break;
   5678 	default:
   5679 		panic("%s: unknown type\n", __func__);
   5680 	}
   5681 
   5682 	/* Check whether EEPROM is present or not */
   5683 	switch (sc->sc_type) {
   5684 	case WM_T_82575:
   5685 	case WM_T_82576:
   5686 	case WM_T_82580:
   5687 	case WM_T_I350:
   5688 	case WM_T_I354:
   5689 	case WM_T_ICH8:
   5690 	case WM_T_ICH9:
   5691 		if ((CSR_READ(sc, WMREG_EECD) & EECD_EE_PRES) == 0) {
   5692 			/* Not found */
   5693 			sc->sc_flags |= WM_F_EEPROM_INVALID;
   5694 			if (sc->sc_type == WM_T_82575)
   5695 				wm_reset_init_script_82575(sc);
   5696 		}
   5697 		break;
   5698 	default:
   5699 		break;
   5700 	}
   5701 
   5702 	if (phy_reset != 0)
   5703 		wm_phy_post_reset(sc);
   5704 
   5705 	if ((sc->sc_type == WM_T_82580)
   5706 	    || (sc->sc_type == WM_T_I350) || (sc->sc_type == WM_T_I354)) {
   5707 		/* Clear global device reset status bit */
   5708 		CSR_WRITE(sc, WMREG_STATUS, STATUS_DEV_RST_SET);
   5709 	}
   5710 
   5711 	/* Clear any pending interrupt events. */
   5712 	CSR_WRITE(sc, WMREG_IMC, 0xffffffffU);
   5713 	reg = CSR_READ(sc, WMREG_ICR);
   5714 	if (wm_is_using_msix(sc)) {
   5715 		if (sc->sc_type != WM_T_82574) {
   5716 			CSR_WRITE(sc, WMREG_EIMC, 0xffffffffU);
   5717 			CSR_WRITE(sc, WMREG_EIAC, 0);
   5718 		} else
   5719 			CSR_WRITE(sc, WMREG_EIAC_82574, 0);
   5720 	}
   5721 
   5722 	if ((sc->sc_type == WM_T_ICH8) || (sc->sc_type == WM_T_ICH9)
   5723 	    || (sc->sc_type == WM_T_ICH10) || (sc->sc_type == WM_T_PCH)
   5724 	    || (sc->sc_type == WM_T_PCH2) || (sc->sc_type == WM_T_PCH_LPT)
   5725 	    || (sc->sc_type == WM_T_PCH_SPT) || (sc->sc_type == WM_T_PCH_CNP)){
   5726 		reg = CSR_READ(sc, WMREG_KABGTXD);
   5727 		reg |= KABGTXD_BGSQLBIAS;
   5728 		CSR_WRITE(sc, WMREG_KABGTXD, reg);
   5729 	}
   5730 
   5731 	/* Reload sc_ctrl */
   5732 	sc->sc_ctrl = CSR_READ(sc, WMREG_CTRL);
   5733 
   5734 	wm_set_eee(sc);
   5735 
   5736 	/*
   5737 	 * For PCH, this write will make sure that any noise will be detected
   5738 	 * as a CRC error and be dropped rather than show up as a bad packet
   5739 	 * to the DMA engine
   5740 	 */
   5741 	if (sc->sc_type == WM_T_PCH)
   5742 		CSR_WRITE(sc, WMREG_CRC_OFFSET, 0x65656565);
   5743 
   5744 	if (sc->sc_type >= WM_T_82544)
   5745 		CSR_WRITE(sc, WMREG_WUC, 0);
   5746 
   5747 	if (sc->sc_type < WM_T_82575)
   5748 		wm_disable_aspm(sc); /* Workaround for some chips */
   5749 
   5750 	wm_reset_mdicnfg_82580(sc);
   5751 
   5752 	if ((sc->sc_flags & WM_F_PLL_WA_I210) != 0)
   5753 		wm_pll_workaround_i210(sc);
   5754 
   5755 	if (sc->sc_type == WM_T_80003) {
   5756 		/* Default to TRUE to enable the MDIC W/A */
   5757 		sc->sc_flags |= WM_F_80003_MDIC_WA;
   5758 
   5759 		rv = wm_kmrn_readreg(sc,
   5760 		    KUMCTRLSTA_OFFSET >> KUMCTRLSTA_OFFSET_SHIFT, &kmreg);
   5761 		if (rv == 0) {
   5762 			if ((kmreg & KUMCTRLSTA_OPMODE_MASK)
   5763 			    == KUMCTRLSTA_OPMODE_INBAND_MDIO)
   5764 				sc->sc_flags &= ~WM_F_80003_MDIC_WA;
   5765 			else
   5766 				sc->sc_flags |= WM_F_80003_MDIC_WA;
   5767 		}
   5768 	}
   5769 }
   5770 
   5771 /*
   5772  * wm_add_rxbuf:
   5773  *
   5774  *	Add a receive buffer to the indiciated descriptor.
   5775  */
   5776 static int
   5777 wm_add_rxbuf(struct wm_rxqueue *rxq, int idx)
   5778 {
   5779 	struct wm_softc *sc = rxq->rxq_sc;
   5780 	struct wm_rxsoft *rxs = &rxq->rxq_soft[idx];
   5781 	struct mbuf *m;
   5782 	int error;
   5783 
   5784 	KASSERT(mutex_owned(rxq->rxq_lock));
   5785 
   5786 	MGETHDR(m, M_DONTWAIT, MT_DATA);
   5787 	if (m == NULL)
   5788 		return ENOBUFS;
   5789 
   5790 	MCLGET(m, M_DONTWAIT);
   5791 	if ((m->m_flags & M_EXT) == 0) {
   5792 		m_freem(m);
   5793 		return ENOBUFS;
   5794 	}
   5795 
   5796 	if (rxs->rxs_mbuf != NULL)
   5797 		bus_dmamap_unload(sc->sc_dmat, rxs->rxs_dmamap);
   5798 
   5799 	rxs->rxs_mbuf = m;
   5800 
   5801 	m->m_len = m->m_pkthdr.len = m->m_ext.ext_size;
   5802 	/*
   5803 	 * Cannot use bus_dmamap_load_mbuf() here because m_data may be
   5804 	 * sc_align_tweak'd between bus_dmamap_load() and bus_dmamap_sync().
   5805 	 */
   5806 	error = bus_dmamap_load(sc->sc_dmat, rxs->rxs_dmamap, m->m_ext.ext_buf,
   5807 	    m->m_ext.ext_size, NULL, BUS_DMA_READ | BUS_DMA_NOWAIT);
   5808 	if (error) {
   5809 		/* XXX XXX XXX */
   5810 		aprint_error_dev(sc->sc_dev,
   5811 		    "unable to load rx DMA map %d, error = %d\n", idx, error);
   5812 		panic("wm_add_rxbuf");
   5813 	}
   5814 
   5815 	bus_dmamap_sync(sc->sc_dmat, rxs->rxs_dmamap, 0,
   5816 	    rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
   5817 
   5818 	if ((sc->sc_flags & WM_F_NEWQUEUE) != 0) {
   5819 		if ((sc->sc_rctl & RCTL_EN) != 0)
   5820 			wm_init_rxdesc(rxq, idx);
   5821 	} else
   5822 		wm_init_rxdesc(rxq, idx);
   5823 
   5824 	return 0;
   5825 }
   5826 
   5827 /*
   5828  * wm_rxdrain:
   5829  *
   5830  *	Drain the receive queue.
   5831  */
   5832 static void
   5833 wm_rxdrain(struct wm_rxqueue *rxq)
   5834 {
   5835 	struct wm_softc *sc = rxq->rxq_sc;
   5836 	struct wm_rxsoft *rxs;
   5837 	int i;
   5838 
   5839 	KASSERT(mutex_owned(rxq->rxq_lock));
   5840 
   5841 	for (i = 0; i < WM_NRXDESC; i++) {
   5842 		rxs = &rxq->rxq_soft[i];
   5843 		if (rxs->rxs_mbuf != NULL) {
   5844 			bus_dmamap_unload(sc->sc_dmat, rxs->rxs_dmamap);
   5845 			m_freem(rxs->rxs_mbuf);
   5846 			rxs->rxs_mbuf = NULL;
   5847 		}
   5848 	}
   5849 }
   5850 
   5851 /*
   5852  * Setup registers for RSS.
   5853  *
   5854  * XXX not yet VMDq support
   5855  */
   5856 static void
   5857 wm_init_rss(struct wm_softc *sc)
   5858 {
   5859 	uint32_t mrqc, reta_reg, rss_key[RSSRK_NUM_REGS];
   5860 	int i;
   5861 
   5862 	CTASSERT(sizeof(rss_key) == RSS_KEYSIZE);
   5863 
   5864 	for (i = 0; i < RETA_NUM_ENTRIES; i++) {
   5865 		unsigned int qid, reta_ent;
   5866 
   5867 		qid  = i % sc->sc_nqueues;
   5868 		switch (sc->sc_type) {
   5869 		case WM_T_82574:
   5870 			reta_ent = __SHIFTIN(qid,
   5871 			    RETA_ENT_QINDEX_MASK_82574);
   5872 			break;
   5873 		case WM_T_82575:
   5874 			reta_ent = __SHIFTIN(qid,
   5875 			    RETA_ENT_QINDEX1_MASK_82575);
   5876 			break;
   5877 		default:
   5878 			reta_ent = __SHIFTIN(qid, RETA_ENT_QINDEX_MASK);
   5879 			break;
   5880 		}
   5881 
   5882 		reta_reg = CSR_READ(sc, WMREG_RETA_Q(i));
   5883 		reta_reg &= ~RETA_ENTRY_MASK_Q(i);
   5884 		reta_reg |= __SHIFTIN(reta_ent, RETA_ENTRY_MASK_Q(i));
   5885 		CSR_WRITE(sc, WMREG_RETA_Q(i), reta_reg);
   5886 	}
   5887 
   5888 	rss_getkey((uint8_t *)rss_key);
   5889 	for (i = 0; i < RSSRK_NUM_REGS; i++)
   5890 		CSR_WRITE(sc, WMREG_RSSRK(i), rss_key[i]);
   5891 
   5892 	if (sc->sc_type == WM_T_82574)
   5893 		mrqc = MRQC_ENABLE_RSS_MQ_82574;
   5894 	else
   5895 		mrqc = MRQC_ENABLE_RSS_MQ;
   5896 
   5897 	/*
   5898 	 * MRQC_RSS_FIELD_IPV6_EX is not set because of an errata.
   5899 	 * See IPV6EXDIS bit in wm_initialize_hardware_bits().
   5900 	 */
   5901 	mrqc |= (MRQC_RSS_FIELD_IPV4 | MRQC_RSS_FIELD_IPV4_TCP);
   5902 	mrqc |= (MRQC_RSS_FIELD_IPV6 | MRQC_RSS_FIELD_IPV6_TCP);
   5903 #if 0
   5904 	mrqc |= (MRQC_RSS_FIELD_IPV4_UDP | MRQC_RSS_FIELD_IPV6_UDP);
   5905 	mrqc |= MRQC_RSS_FIELD_IPV6_UDP_EX;
   5906 #endif
   5907 	mrqc |= MRQC_RSS_FIELD_IPV6_TCP_EX;
   5908 
   5909 	CSR_WRITE(sc, WMREG_MRQC, mrqc);
   5910 }
   5911 
   5912 /*
   5913  * Adjust TX and RX queue numbers which the system actulally uses.
   5914  *
   5915  * The numbers are affected by below parameters.
   5916  *     - The nubmer of hardware queues
   5917  *     - The number of MSI-X vectors (= "nvectors" argument)
   5918  *     - ncpu
   5919  */
   5920 static void
   5921 wm_adjust_qnum(struct wm_softc *sc, int nvectors)
   5922 {
   5923 	int hw_ntxqueues, hw_nrxqueues, hw_nqueues;
   5924 
   5925 	if (nvectors < 2) {
   5926 		sc->sc_nqueues = 1;
   5927 		return;
   5928 	}
   5929 
   5930 	switch (sc->sc_type) {
   5931 	case WM_T_82572:
   5932 		hw_ntxqueues = 2;
   5933 		hw_nrxqueues = 2;
   5934 		break;
   5935 	case WM_T_82574:
   5936 		hw_ntxqueues = 2;
   5937 		hw_nrxqueues = 2;
   5938 		break;
   5939 	case WM_T_82575:
   5940 		hw_ntxqueues = 4;
   5941 		hw_nrxqueues = 4;
   5942 		break;
   5943 	case WM_T_82576:
   5944 		hw_ntxqueues = 16;
   5945 		hw_nrxqueues = 16;
   5946 		break;
   5947 	case WM_T_82580:
   5948 	case WM_T_I350:
   5949 	case WM_T_I354:
   5950 		hw_ntxqueues = 8;
   5951 		hw_nrxqueues = 8;
   5952 		break;
   5953 	case WM_T_I210:
   5954 		hw_ntxqueues = 4;
   5955 		hw_nrxqueues = 4;
   5956 		break;
   5957 	case WM_T_I211:
   5958 		hw_ntxqueues = 2;
   5959 		hw_nrxqueues = 2;
   5960 		break;
   5961 		/*
   5962 		 * The below Ethernet controllers do not support MSI-X;
   5963 		 * this driver doesn't let them use multiqueue.
   5964 		 *     - WM_T_80003
   5965 		 *     - WM_T_ICH8
   5966 		 *     - WM_T_ICH9
   5967 		 *     - WM_T_ICH10
   5968 		 *     - WM_T_PCH
   5969 		 *     - WM_T_PCH2
   5970 		 *     - WM_T_PCH_LPT
   5971 		 */
   5972 	default:
   5973 		hw_ntxqueues = 1;
   5974 		hw_nrxqueues = 1;
   5975 		break;
   5976 	}
   5977 
   5978 	hw_nqueues = uimin(hw_ntxqueues, hw_nrxqueues);
   5979 
   5980 	/*
   5981 	 * As queues more than MSI-X vectors cannot improve scaling, we limit
   5982 	 * the number of queues used actually.
   5983 	 */
   5984 	if (nvectors < hw_nqueues + 1)
   5985 		sc->sc_nqueues = nvectors - 1;
   5986 	else
   5987 		sc->sc_nqueues = hw_nqueues;
   5988 
   5989 	/*
   5990 	 * As queues more than CPUs cannot improve scaling, we limit
   5991 	 * the number of queues used actually.
   5992 	 */
   5993 	if (ncpu < sc->sc_nqueues)
   5994 		sc->sc_nqueues = ncpu;
   5995 }
   5996 
   5997 static inline bool
   5998 wm_is_using_msix(struct wm_softc *sc)
   5999 {
   6000 
   6001 	return (sc->sc_nintrs > 1);
   6002 }
   6003 
   6004 static inline bool
   6005 wm_is_using_multiqueue(struct wm_softc *sc)
   6006 {
   6007 
   6008 	return (sc->sc_nqueues > 1);
   6009 }
   6010 
   6011 static int
   6012 wm_softint_establish_queue(struct wm_softc *sc, int qidx, int intr_idx)
   6013 {
   6014 	struct wm_queue *wmq = &sc->sc_queue[qidx];
   6015 
   6016 	wmq->wmq_id = qidx;
   6017 	wmq->wmq_intr_idx = intr_idx;
   6018 	wmq->wmq_si = softint_establish(SOFTINT_NET | WM_SOFTINT_FLAGS,
   6019 	    wm_handle_queue, wmq);
   6020 	if (wmq->wmq_si != NULL)
   6021 		return 0;
   6022 
   6023 	aprint_error_dev(sc->sc_dev, "unable to establish queue[%d] handler\n",
   6024 	    wmq->wmq_id);
   6025 	pci_intr_disestablish(sc->sc_pc, sc->sc_ihs[wmq->wmq_intr_idx]);
   6026 	sc->sc_ihs[wmq->wmq_intr_idx] = NULL;
   6027 	return ENOMEM;
   6028 }
   6029 
   6030 /*
   6031  * Both single interrupt MSI and INTx can use this function.
   6032  */
   6033 static int
   6034 wm_setup_legacy(struct wm_softc *sc)
   6035 {
   6036 	pci_chipset_tag_t pc = sc->sc_pc;
   6037 	const char *intrstr = NULL;
   6038 	char intrbuf[PCI_INTRSTR_LEN];
   6039 	int error;
   6040 
   6041 	error = wm_alloc_txrx_queues(sc);
   6042 	if (error) {
   6043 		aprint_error_dev(sc->sc_dev, "cannot allocate queues %d\n",
   6044 		    error);
   6045 		return ENOMEM;
   6046 	}
   6047 	intrstr = pci_intr_string(pc, sc->sc_intrs[0], intrbuf,
   6048 	    sizeof(intrbuf));
   6049 #ifdef WM_MPSAFE
   6050 	pci_intr_setattr(pc, &sc->sc_intrs[0], PCI_INTR_MPSAFE, true);
   6051 #endif
   6052 	sc->sc_ihs[0] = pci_intr_establish_xname(pc, sc->sc_intrs[0],
   6053 	    IPL_NET, wm_intr_legacy, sc, device_xname(sc->sc_dev));
   6054 	if (sc->sc_ihs[0] == NULL) {
   6055 		aprint_error_dev(sc->sc_dev,"unable to establish %s\n",
   6056 		    (pci_intr_type(pc, sc->sc_intrs[0])
   6057 			== PCI_INTR_TYPE_MSI) ? "MSI" : "INTx");
   6058 		return ENOMEM;
   6059 	}
   6060 
   6061 	aprint_normal_dev(sc->sc_dev, "interrupting at %s\n", intrstr);
   6062 	sc->sc_nintrs = 1;
   6063 
   6064 	return wm_softint_establish_queue(sc, 0, 0);
   6065 }
   6066 
   6067 static int
   6068 wm_setup_msix(struct wm_softc *sc)
   6069 {
   6070 	void *vih;
   6071 	kcpuset_t *affinity;
   6072 	int qidx, error, intr_idx, txrx_established;
   6073 	pci_chipset_tag_t pc = sc->sc_pc;
   6074 	const char *intrstr = NULL;
   6075 	char intrbuf[PCI_INTRSTR_LEN];
   6076 	char intr_xname[INTRDEVNAMEBUF];
   6077 
   6078 	if (sc->sc_nqueues < ncpu) {
   6079 		/*
   6080 		 * To avoid other devices' interrupts, the affinity of Tx/Rx
   6081 		 * interrupts start from CPU#1.
   6082 		 */
   6083 		sc->sc_affinity_offset = 1;
   6084 	} else {
   6085 		/*
   6086 		 * In this case, this device use all CPUs. So, we unify
   6087 		 * affinitied cpu_index to msix vector number for readability.
   6088 		 */
   6089 		sc->sc_affinity_offset = 0;
   6090 	}
   6091 
   6092 	error = wm_alloc_txrx_queues(sc);
   6093 	if (error) {
   6094 		aprint_error_dev(sc->sc_dev, "cannot allocate queues %d\n",
   6095 		    error);
   6096 		return ENOMEM;
   6097 	}
   6098 
   6099 	kcpuset_create(&affinity, false);
   6100 	intr_idx = 0;
   6101 
   6102 	/*
   6103 	 * TX and RX
   6104 	 */
   6105 	txrx_established = 0;
   6106 	for (qidx = 0; qidx < sc->sc_nqueues; qidx++) {
   6107 		struct wm_queue *wmq = &sc->sc_queue[qidx];
   6108 		int affinity_to = (sc->sc_affinity_offset + intr_idx) % ncpu;
   6109 
   6110 		intrstr = pci_intr_string(pc, sc->sc_intrs[intr_idx], intrbuf,
   6111 		    sizeof(intrbuf));
   6112 #ifdef WM_MPSAFE
   6113 		pci_intr_setattr(pc, &sc->sc_intrs[intr_idx],
   6114 		    PCI_INTR_MPSAFE, true);
   6115 #endif
   6116 		memset(intr_xname, 0, sizeof(intr_xname));
   6117 		snprintf(intr_xname, sizeof(intr_xname), "%sTXRX%d",
   6118 		    device_xname(sc->sc_dev), qidx);
   6119 		vih = pci_intr_establish_xname(pc, sc->sc_intrs[intr_idx],
   6120 		    IPL_NET, wm_txrxintr_msix, wmq, intr_xname);
   6121 		if (vih == NULL) {
   6122 			aprint_error_dev(sc->sc_dev,
   6123 			    "unable to establish MSI-X(for TX and RX)%s%s\n",
   6124 			    intrstr ? " at " : "",
   6125 			    intrstr ? intrstr : "");
   6126 
   6127 			goto fail;
   6128 		}
   6129 		kcpuset_zero(affinity);
   6130 		/* Round-robin affinity */
   6131 		kcpuset_set(affinity, affinity_to);
   6132 		error = interrupt_distribute(vih, affinity, NULL);
   6133 		if (error == 0) {
   6134 			aprint_normal_dev(sc->sc_dev,
   6135 			    "for TX and RX interrupting at %s affinity to %u\n",
   6136 			    intrstr, affinity_to);
   6137 		} else {
   6138 			aprint_normal_dev(sc->sc_dev,
   6139 			    "for TX and RX interrupting at %s\n", intrstr);
   6140 		}
   6141 		sc->sc_ihs[intr_idx] = vih;
   6142 		if (wm_softint_establish_queue(sc, qidx, intr_idx) != 0)
   6143 			goto fail;
   6144 		txrx_established++;
   6145 		intr_idx++;
   6146 	}
   6147 
   6148 	/* LINK */
   6149 	intrstr = pci_intr_string(pc, sc->sc_intrs[intr_idx], intrbuf,
   6150 	    sizeof(intrbuf));
   6151 #ifdef WM_MPSAFE
   6152 	pci_intr_setattr(pc, &sc->sc_intrs[intr_idx], PCI_INTR_MPSAFE, true);
   6153 #endif
   6154 	memset(intr_xname, 0, sizeof(intr_xname));
   6155 	snprintf(intr_xname, sizeof(intr_xname), "%sLINK",
   6156 	    device_xname(sc->sc_dev));
   6157 	vih = pci_intr_establish_xname(pc, sc->sc_intrs[intr_idx],
   6158 	    IPL_NET, wm_linkintr_msix, sc, intr_xname);
   6159 	if (vih == NULL) {
   6160 		aprint_error_dev(sc->sc_dev,
   6161 		    "unable to establish MSI-X(for LINK)%s%s\n",
   6162 		    intrstr ? " at " : "",
   6163 		    intrstr ? intrstr : "");
   6164 
   6165 		goto fail;
   6166 	}
   6167 	/* Keep default affinity to LINK interrupt */
   6168 	aprint_normal_dev(sc->sc_dev,
   6169 	    "for LINK interrupting at %s\n", intrstr);
   6170 	sc->sc_ihs[intr_idx] = vih;
   6171 	sc->sc_link_intr_idx = intr_idx;
   6172 
   6173 	sc->sc_nintrs = sc->sc_nqueues + 1;
   6174 	kcpuset_destroy(affinity);
   6175 	return 0;
   6176 
   6177  fail:
   6178 	for (qidx = 0; qidx < txrx_established; qidx++) {
   6179 		struct wm_queue *wmq = &sc->sc_queue[qidx];
   6180 		pci_intr_disestablish(sc->sc_pc,sc->sc_ihs[wmq->wmq_intr_idx]);
   6181 		sc->sc_ihs[wmq->wmq_intr_idx] = NULL;
   6182 	}
   6183 
   6184 	kcpuset_destroy(affinity);
   6185 	return ENOMEM;
   6186 }
   6187 
   6188 static void
   6189 wm_unset_stopping_flags(struct wm_softc *sc)
   6190 {
   6191 	int i;
   6192 
   6193 	KASSERT(WM_CORE_LOCKED(sc));
   6194 
   6195 	/* Must unset stopping flags in ascending order. */
   6196 	for (i = 0; i < sc->sc_nqueues; i++) {
   6197 		struct wm_txqueue *txq = &sc->sc_queue[i].wmq_txq;
   6198 		struct wm_rxqueue *rxq = &sc->sc_queue[i].wmq_rxq;
   6199 
   6200 		mutex_enter(txq->txq_lock);
   6201 		txq->txq_stopping = false;
   6202 		mutex_exit(txq->txq_lock);
   6203 
   6204 		mutex_enter(rxq->rxq_lock);
   6205 		rxq->rxq_stopping = false;
   6206 		mutex_exit(rxq->rxq_lock);
   6207 	}
   6208 
   6209 	sc->sc_core_stopping = false;
   6210 }
   6211 
   6212 static void
   6213 wm_set_stopping_flags(struct wm_softc *sc)
   6214 {
   6215 	int i;
   6216 
   6217 	KASSERT(WM_CORE_LOCKED(sc));
   6218 
   6219 	sc->sc_core_stopping = true;
   6220 
   6221 	/* Must set stopping flags in ascending order. */
   6222 	for (i = 0; i < sc->sc_nqueues; i++) {
   6223 		struct wm_rxqueue *rxq = &sc->sc_queue[i].wmq_rxq;
   6224 		struct wm_txqueue *txq = &sc->sc_queue[i].wmq_txq;
   6225 
   6226 		mutex_enter(rxq->rxq_lock);
   6227 		rxq->rxq_stopping = true;
   6228 		mutex_exit(rxq->rxq_lock);
   6229 
   6230 		mutex_enter(txq->txq_lock);
   6231 		txq->txq_stopping = true;
   6232 		mutex_exit(txq->txq_lock);
   6233 	}
   6234 }
   6235 
   6236 /*
   6237  * Write interrupt interval value to ITR or EITR
   6238  */
   6239 static void
   6240 wm_itrs_writereg(struct wm_softc *sc, struct wm_queue *wmq)
   6241 {
   6242 
   6243 	if (!wmq->wmq_set_itr)
   6244 		return;
   6245 
   6246 	if ((sc->sc_flags & WM_F_NEWQUEUE) != 0) {
   6247 		uint32_t eitr = __SHIFTIN(wmq->wmq_itr, EITR_ITR_INT_MASK);
   6248 
   6249 		/*
   6250 		 * 82575 doesn't have CNT_INGR field.
   6251 		 * So, overwrite counter field by software.
   6252 		 */
   6253 		if (sc->sc_type == WM_T_82575)
   6254 			eitr |= __SHIFTIN(wmq->wmq_itr, EITR_COUNTER_MASK_82575);
   6255 		else
   6256 			eitr |= EITR_CNT_INGR;
   6257 
   6258 		CSR_WRITE(sc, WMREG_EITR(wmq->wmq_intr_idx), eitr);
   6259 	} else if (sc->sc_type == WM_T_82574 && wm_is_using_msix(sc)) {
   6260 		/*
   6261 		 * 82574 has both ITR and EITR. SET EITR when we use
   6262 		 * the multi queue function with MSI-X.
   6263 		 */
   6264 		CSR_WRITE(sc, WMREG_EITR_82574(wmq->wmq_intr_idx),
   6265 		    wmq->wmq_itr & EITR_ITR_INT_MASK_82574);
   6266 	} else {
   6267 		KASSERT(wmq->wmq_id == 0);
   6268 		CSR_WRITE(sc, WMREG_ITR, wmq->wmq_itr);
   6269 	}
   6270 
   6271 	wmq->wmq_set_itr = false;
   6272 }
   6273 
   6274 /*
   6275  * TODO
   6276  * Below dynamic calculation of itr is almost the same as Linux igb,
   6277  * however it does not fit to wm(4). So, we will have been disable AIM
   6278  * until we will find appropriate calculation of itr.
   6279  */
   6280 /*
   6281  * Calculate interrupt interval value to be going to write register in
   6282  * wm_itrs_writereg(). This function does not write ITR/EITR register.
   6283  */
   6284 static void
   6285 wm_itrs_calculate(struct wm_softc *sc, struct wm_queue *wmq)
   6286 {
   6287 #ifdef NOTYET
   6288 	struct wm_rxqueue *rxq = &wmq->wmq_rxq;
   6289 	struct wm_txqueue *txq = &wmq->wmq_txq;
   6290 	uint32_t avg_size = 0;
   6291 	uint32_t new_itr;
   6292 
   6293 	if (rxq->rxq_packets)
   6294 		avg_size =  rxq->rxq_bytes / rxq->rxq_packets;
   6295 	if (txq->txq_packets)
   6296 		avg_size = uimax(avg_size, txq->txq_bytes / txq->txq_packets);
   6297 
   6298 	if (avg_size == 0) {
   6299 		new_itr = 450; /* restore default value */
   6300 		goto out;
   6301 	}
   6302 
   6303 	/* Add 24 bytes to size to account for CRC, preamble, and gap */
   6304 	avg_size += 24;
   6305 
   6306 	/* Don't starve jumbo frames */
   6307 	avg_size = uimin(avg_size, 3000);
   6308 
   6309 	/* Give a little boost to mid-size frames */
   6310 	if ((avg_size > 300) && (avg_size < 1200))
   6311 		new_itr = avg_size / 3;
   6312 	else
   6313 		new_itr = avg_size / 2;
   6314 
   6315 out:
   6316 	/*
   6317 	 * The usage of 82574 and 82575 EITR is different from otther NEWQUEUE
   6318 	 * controllers. See sc->sc_itr_init setting in wm_init_locked().
   6319 	 */
   6320 	if ((sc->sc_flags & WM_F_NEWQUEUE) == 0 || sc->sc_type != WM_T_82575)
   6321 		new_itr *= 4;
   6322 
   6323 	if (new_itr != wmq->wmq_itr) {
   6324 		wmq->wmq_itr = new_itr;
   6325 		wmq->wmq_set_itr = true;
   6326 	} else
   6327 		wmq->wmq_set_itr = false;
   6328 
   6329 	rxq->rxq_packets = 0;
   6330 	rxq->rxq_bytes = 0;
   6331 	txq->txq_packets = 0;
   6332 	txq->txq_bytes = 0;
   6333 #endif
   6334 }
   6335 
   6336 static void
   6337 wm_init_sysctls(struct wm_softc *sc)
   6338 {
   6339 	struct sysctllog **log;
   6340 	const struct sysctlnode *rnode, *qnode, *cnode;
   6341 	int i, rv;
   6342 	const char *dvname;
   6343 
   6344 	log = &sc->sc_sysctllog;
   6345 	dvname = device_xname(sc->sc_dev);
   6346 
   6347 	rv = sysctl_createv(log, 0, NULL, &rnode,
   6348 	    0, CTLTYPE_NODE, dvname,
   6349 	    SYSCTL_DESCR("wm information and settings"),
   6350 	    NULL, 0, NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL);
   6351 	if (rv != 0)
   6352 		goto err;
   6353 
   6354 	rv = sysctl_createv(log, 0, &rnode, &cnode, CTLFLAG_READWRITE,
   6355 	    CTLTYPE_BOOL, "txrx_workqueue", SYSCTL_DESCR("Use workqueue for packet processing"),
   6356 	    NULL, 0, &sc->sc_txrx_use_workqueue, 0, CTL_CREATE, CTL_EOL);
   6357 	if (rv != 0)
   6358 		goto teardown;
   6359 
   6360 	for (i = 0; i < sc->sc_nqueues; i++) {
   6361 		struct wm_queue *wmq = &sc->sc_queue[i];
   6362 		struct wm_txqueue *txq = &wmq->wmq_txq;
   6363 		struct wm_rxqueue *rxq = &wmq->wmq_rxq;
   6364 
   6365 		snprintf(sc->sc_queue[i].sysctlname,
   6366 		    sizeof(sc->sc_queue[i].sysctlname), "q%d", i);
   6367 
   6368 		if (sysctl_createv(log, 0, &rnode, &qnode,
   6369 		    0, CTLTYPE_NODE,
   6370 		    sc->sc_queue[i].sysctlname, SYSCTL_DESCR("Queue Name"),
   6371 		    NULL, 0, NULL, 0, CTL_CREATE, CTL_EOL) != 0)
   6372 			break;
   6373 
   6374 		if (sysctl_createv(log, 0, &qnode, &cnode,
   6375 		    CTLFLAG_READONLY, CTLTYPE_INT,
   6376 		    "txq_free", SYSCTL_DESCR("TX queue free"),
   6377 		    NULL, 0, &txq->txq_free,
   6378 		    0, CTL_CREATE, CTL_EOL) != 0)
   6379 			break;
   6380 		if (sysctl_createv(log, 0, &qnode, &cnode,
   6381 		    CTLFLAG_READONLY, CTLTYPE_INT,
   6382 		    "txd_head", SYSCTL_DESCR("TX descriptor head"),
   6383 		    wm_sysctl_tdh_handler, 0, (void *)txq,
   6384 		    0, CTL_CREATE, CTL_EOL) != 0)
   6385 			break;
   6386 		if (sysctl_createv(log, 0, &qnode, &cnode,
   6387 		    CTLFLAG_READONLY, CTLTYPE_INT,
   6388 		    "txd_tail", SYSCTL_DESCR("TX descriptor tail"),
   6389 		    wm_sysctl_tdt_handler, 0, (void *)txq,
   6390 		    0, CTL_CREATE, CTL_EOL) != 0)
   6391 			break;
   6392 		if (sysctl_createv(log, 0, &qnode, &cnode,
   6393 		    CTLFLAG_READONLY, CTLTYPE_INT,
   6394 		    "txq_next", SYSCTL_DESCR("TX queue next"),
   6395 		    NULL, 0, &txq->txq_next,
   6396 		    0, CTL_CREATE, CTL_EOL) != 0)
   6397 			break;
   6398 		if (sysctl_createv(log, 0, &qnode, &cnode,
   6399 		    CTLFLAG_READONLY, CTLTYPE_INT,
   6400 		    "txq_sfree", SYSCTL_DESCR("TX queue sfree"),
   6401 		    NULL, 0, &txq->txq_sfree,
   6402 		    0, CTL_CREATE, CTL_EOL) != 0)
   6403 			break;
   6404 		if (sysctl_createv(log, 0, &qnode, &cnode,
   6405 		    CTLFLAG_READONLY, CTLTYPE_INT,
   6406 		    "txq_snext", SYSCTL_DESCR("TX queue snext"),
   6407 		    NULL, 0, &txq->txq_snext,
   6408 		    0, CTL_CREATE, CTL_EOL) != 0)
   6409 			break;
   6410 		if (sysctl_createv(log, 0, &qnode, &cnode,
   6411 		    CTLFLAG_READONLY, CTLTYPE_INT,
   6412 		    "txq_sdirty", SYSCTL_DESCR("TX queue sdirty"),
   6413 		    NULL, 0, &txq->txq_sdirty,
   6414 		    0, CTL_CREATE, CTL_EOL) != 0)
   6415 			break;
   6416 		if (sysctl_createv(log, 0, &qnode, &cnode,
   6417 		    CTLFLAG_READONLY, CTLTYPE_INT,
   6418 		    "txq_flags", SYSCTL_DESCR("TX queue flags"),
   6419 		    NULL, 0, &txq->txq_flags,
   6420 		    0, CTL_CREATE, CTL_EOL) != 0)
   6421 			break;
   6422 		if (sysctl_createv(log, 0, &qnode, &cnode,
   6423 		    CTLFLAG_READONLY, CTLTYPE_BOOL,
   6424 		    "txq_stopping", SYSCTL_DESCR("TX queue stopping"),
   6425 		    NULL, 0, &txq->txq_stopping,
   6426 		    0, CTL_CREATE, CTL_EOL) != 0)
   6427 			break;
   6428 		if (sysctl_createv(log, 0, &qnode, &cnode,
   6429 		    CTLFLAG_READONLY, CTLTYPE_BOOL,
   6430 		    "txq_sending", SYSCTL_DESCR("TX queue sending"),
   6431 		    NULL, 0, &txq->txq_sending,
   6432 		    0, CTL_CREATE, CTL_EOL) != 0)
   6433 			break;
   6434 
   6435 		if (sysctl_createv(log, 0, &qnode, &cnode,
   6436 		    CTLFLAG_READONLY, CTLTYPE_INT,
   6437 		    "rxq_ptr", SYSCTL_DESCR("RX queue pointer"),
   6438 		    NULL, 0, &rxq->rxq_ptr,
   6439 		    0, CTL_CREATE, CTL_EOL) != 0)
   6440 			break;
   6441 	}
   6442 
   6443 #ifdef WM_DEBUG
   6444 	rv = sysctl_createv(log, 0, &rnode, &cnode, CTLFLAG_READWRITE,
   6445 	    CTLTYPE_INT, "debug_flags",
   6446 	    SYSCTL_DESCR(
   6447 		    "Debug flags:\n"	\
   6448 		    "\t0x01 LINK\n"	\
   6449 		    "\t0x02 TX\n"	\
   6450 		    "\t0x04 RX\n"	\
   6451 		    "\t0x08 GMII\n"	\
   6452 		    "\t0x10 MANAGE\n"	\
   6453 		    "\t0x20 NVM\n"	\
   6454 		    "\t0x40 INIT\n"	\
   6455 		    "\t0x80 LOCK"),
   6456 	    wm_sysctl_debug, 0, (void *)sc, 0, CTL_CREATE, CTL_EOL);
   6457 	if (rv != 0)
   6458 		goto teardown;
   6459 #endif
   6460 
   6461 	return;
   6462 
   6463 teardown:
   6464 	sysctl_teardown(log);
   6465 err:
   6466 	sc->sc_sysctllog = NULL;
   6467 	device_printf(sc->sc_dev, "%s: sysctl_createv failed, rv = %d\n",
   6468 	    __func__, rv);
   6469 }
   6470 
   6471 /*
   6472  * wm_init:		[ifnet interface function]
   6473  *
   6474  *	Initialize the interface.
   6475  */
   6476 static int
   6477 wm_init(struct ifnet *ifp)
   6478 {
   6479 	struct wm_softc *sc = ifp->if_softc;
   6480 	int ret;
   6481 
   6482 	KASSERT(IFNET_LOCKED(ifp));
   6483 
   6484 	WM_CORE_LOCK(sc);
   6485 	ret = wm_init_locked(ifp);
   6486 	WM_CORE_UNLOCK(sc);
   6487 
   6488 	return ret;
   6489 }
   6490 
   6491 static int
   6492 wm_init_locked(struct ifnet *ifp)
   6493 {
   6494 	struct wm_softc *sc = ifp->if_softc;
   6495 	struct ethercom *ec = &sc->sc_ethercom;
   6496 	int i, j, trynum, error = 0;
   6497 	uint32_t reg, sfp_mask = 0;
   6498 
   6499 	DPRINTF(sc, WM_DEBUG_INIT, ("%s: %s called\n",
   6500 		device_xname(sc->sc_dev), __func__));
   6501 	KASSERT(IFNET_LOCKED(ifp));
   6502 	KASSERT(WM_CORE_LOCKED(sc));
   6503 
   6504 	/*
   6505 	 * *_HDR_ALIGNED_P is constant 1 if __NO_STRICT_ALIGMENT is set.
   6506 	 * There is a small but measurable benefit to avoiding the adjusment
   6507 	 * of the descriptor so that the headers are aligned, for normal mtu,
   6508 	 * on such platforms.  One possibility is that the DMA itself is
   6509 	 * slightly more efficient if the front of the entire packet (instead
   6510 	 * of the front of the headers) is aligned.
   6511 	 *
   6512 	 * Note we must always set align_tweak to 0 if we are using
   6513 	 * jumbo frames.
   6514 	 */
   6515 #ifdef __NO_STRICT_ALIGNMENT
   6516 	sc->sc_align_tweak = 0;
   6517 #else
   6518 	if ((ifp->if_mtu + ETHER_HDR_LEN + ETHER_CRC_LEN) > (MCLBYTES - 2))
   6519 		sc->sc_align_tweak = 0;
   6520 	else
   6521 		sc->sc_align_tweak = 2;
   6522 #endif /* __NO_STRICT_ALIGNMENT */
   6523 
   6524 	/* Cancel any pending I/O. */
   6525 	wm_stop_locked(ifp, false, false);
   6526 
   6527 	/* Update statistics before reset */
   6528 	if_statadd2(ifp, if_collisions, CSR_READ(sc, WMREG_COLC),
   6529 	    if_ierrors, CSR_READ(sc, WMREG_RXERRC));
   6530 
   6531 	/* >= PCH_SPT hardware workaround before reset. */
   6532 	if (sc->sc_type >= WM_T_PCH_SPT)
   6533 		wm_flush_desc_rings(sc);
   6534 
   6535 	/* Reset the chip to a known state. */
   6536 	wm_reset(sc);
   6537 
   6538 	/*
   6539 	 * AMT based hardware can now take control from firmware
   6540 	 * Do this after reset.
   6541 	 */
   6542 	if ((sc->sc_flags & WM_F_HAS_AMT) != 0)
   6543 		wm_get_hw_control(sc);
   6544 
   6545 	if ((sc->sc_type >= WM_T_PCH_SPT) &&
   6546 	    pci_intr_type(sc->sc_pc, sc->sc_intrs[0]) == PCI_INTR_TYPE_INTX)
   6547 		wm_legacy_irq_quirk_spt(sc);
   6548 
   6549 	/* Init hardware bits */
   6550 	wm_initialize_hardware_bits(sc);
   6551 
   6552 	/* Reset the PHY. */
   6553 	if (sc->sc_flags & WM_F_HAS_MII)
   6554 		wm_gmii_reset(sc);
   6555 
   6556 	if (sc->sc_type >= WM_T_ICH8) {
   6557 		reg = CSR_READ(sc, WMREG_GCR);
   6558 		/*
   6559 		 * ICH8 No-snoop bits are opposite polarity. Set to snoop by
   6560 		 * default after reset.
   6561 		 */
   6562 		if (sc->sc_type == WM_T_ICH8)
   6563 			reg |= GCR_NO_SNOOP_ALL;
   6564 		else
   6565 			reg &= ~GCR_NO_SNOOP_ALL;
   6566 		CSR_WRITE(sc, WMREG_GCR, reg);
   6567 	}
   6568 
   6569 	if ((sc->sc_type >= WM_T_ICH8)
   6570 	    || (sc->sc_pcidevid == PCI_PRODUCT_INTEL_82546GB_QUAD_COPPER)
   6571 	    || (sc->sc_pcidevid == PCI_PRODUCT_INTEL_82546GB_QUAD_COPPER_KSP3)) {
   6572 
   6573 		reg = CSR_READ(sc, WMREG_CTRL_EXT);
   6574 		reg |= CTRL_EXT_RO_DIS;
   6575 		CSR_WRITE(sc, WMREG_CTRL_EXT, reg);
   6576 	}
   6577 
   6578 	/* Calculate (E)ITR value */
   6579 	if ((sc->sc_flags & WM_F_NEWQUEUE) != 0 && sc->sc_type != WM_T_82575) {
   6580 		/*
   6581 		 * For NEWQUEUE's EITR (except for 82575).
   6582 		 * 82575's EITR should be set same throttling value as other
   6583 		 * old controllers' ITR because the interrupt/sec calculation
   6584 		 * is the same, that is, 1,000,000,000 / (N * 256).
   6585 		 *
   6586 		 * 82574's EITR should be set same throttling value as ITR.
   6587 		 *
   6588 		 * For N interrupts/sec, set this value to:
   6589 		 * 1,000,000 / N in contrast to ITR throttling value.
   6590 		 */
   6591 		sc->sc_itr_init = 450;
   6592 	} else if (sc->sc_type >= WM_T_82543) {
   6593 		/*
   6594 		 * Set up the interrupt throttling register (units of 256ns)
   6595 		 * Note that a footnote in Intel's documentation says this
   6596 		 * ticker runs at 1/4 the rate when the chip is in 100Mbit
   6597 		 * or 10Mbit mode.  Empirically, it appears to be the case
   6598 		 * that that is also true for the 1024ns units of the other
   6599 		 * interrupt-related timer registers -- so, really, we ought
   6600 		 * to divide this value by 4 when the link speed is low.
   6601 		 *
   6602 		 * XXX implement this division at link speed change!
   6603 		 */
   6604 
   6605 		/*
   6606 		 * For N interrupts/sec, set this value to:
   6607 		 * 1,000,000,000 / (N * 256).  Note that we set the
   6608 		 * absolute and packet timer values to this value
   6609 		 * divided by 4 to get "simple timer" behavior.
   6610 		 */
   6611 		sc->sc_itr_init = 1500;		/* 2604 ints/sec */
   6612 	}
   6613 
   6614 	error = wm_init_txrx_queues(sc);
   6615 	if (error)
   6616 		goto out;
   6617 
   6618 	if (((sc->sc_flags & WM_F_SGMII) == 0) &&
   6619 	    (sc->sc_mediatype == WM_MEDIATYPE_SERDES) &&
   6620 	    (sc->sc_type >= WM_T_82575))
   6621 		wm_serdes_power_up_link_82575(sc);
   6622 
   6623 	/* Clear out the VLAN table -- we don't use it (yet). */
   6624 	CSR_WRITE(sc, WMREG_VET, 0);
   6625 	if ((sc->sc_type == WM_T_I350) || (sc->sc_type == WM_T_I354))
   6626 		trynum = 10; /* Due to hw errata */
   6627 	else
   6628 		trynum = 1;
   6629 	for (i = 0; i < WM_VLAN_TABSIZE; i++)
   6630 		for (j = 0; j < trynum; j++)
   6631 			CSR_WRITE(sc, WMREG_VFTA + (i << 2), 0);
   6632 
   6633 	/*
   6634 	 * Set up flow-control parameters.
   6635 	 *
   6636 	 * XXX Values could probably stand some tuning.
   6637 	 */
   6638 	if ((sc->sc_type != WM_T_ICH8) && (sc->sc_type != WM_T_ICH9)
   6639 	    && (sc->sc_type != WM_T_ICH10) && (sc->sc_type != WM_T_PCH)
   6640 	    && (sc->sc_type != WM_T_PCH2) && (sc->sc_type != WM_T_PCH_LPT)
   6641 	    && (sc->sc_type != WM_T_PCH_SPT) && (sc->sc_type != WM_T_PCH_CNP)){
   6642 		CSR_WRITE(sc, WMREG_FCAL, FCAL_CONST);
   6643 		CSR_WRITE(sc, WMREG_FCAH, FCAH_CONST);
   6644 		CSR_WRITE(sc, WMREG_FCT, ETHERTYPE_FLOWCONTROL);
   6645 	}
   6646 
   6647 	sc->sc_fcrtl = FCRTL_DFLT;
   6648 	if (sc->sc_type < WM_T_82543) {
   6649 		CSR_WRITE(sc, WMREG_OLD_FCRTH, FCRTH_DFLT);
   6650 		CSR_WRITE(sc, WMREG_OLD_FCRTL, sc->sc_fcrtl);
   6651 	} else {
   6652 		CSR_WRITE(sc, WMREG_FCRTH, FCRTH_DFLT);
   6653 		CSR_WRITE(sc, WMREG_FCRTL, sc->sc_fcrtl);
   6654 	}
   6655 
   6656 	if (sc->sc_type == WM_T_80003)
   6657 		CSR_WRITE(sc, WMREG_FCTTV, 0xffff);
   6658 	else
   6659 		CSR_WRITE(sc, WMREG_FCTTV, FCTTV_DFLT);
   6660 
   6661 	/* Writes the control register. */
   6662 	wm_set_vlan(sc);
   6663 
   6664 	if (sc->sc_flags & WM_F_HAS_MII) {
   6665 		uint16_t kmreg;
   6666 
   6667 		switch (sc->sc_type) {
   6668 		case WM_T_80003:
   6669 		case WM_T_ICH8:
   6670 		case WM_T_ICH9:
   6671 		case WM_T_ICH10:
   6672 		case WM_T_PCH:
   6673 		case WM_T_PCH2:
   6674 		case WM_T_PCH_LPT:
   6675 		case WM_T_PCH_SPT:
   6676 		case WM_T_PCH_CNP:
   6677 			/*
   6678 			 * Set the mac to wait the maximum time between each
   6679 			 * iteration and increase the max iterations when
   6680 			 * polling the phy; this fixes erroneous timeouts at
   6681 			 * 10Mbps.
   6682 			 */
   6683 			wm_kmrn_writereg(sc, KUMCTRLSTA_OFFSET_TIMEOUTS,
   6684 			    0xFFFF);
   6685 			wm_kmrn_readreg(sc, KUMCTRLSTA_OFFSET_INB_PARAM,
   6686 			    &kmreg);
   6687 			kmreg |= 0x3F;
   6688 			wm_kmrn_writereg(sc, KUMCTRLSTA_OFFSET_INB_PARAM,
   6689 			    kmreg);
   6690 			break;
   6691 		default:
   6692 			break;
   6693 		}
   6694 
   6695 		if (sc->sc_type == WM_T_80003) {
   6696 			reg = CSR_READ(sc, WMREG_CTRL_EXT);
   6697 			reg &= ~CTRL_EXT_LINK_MODE_MASK;
   6698 			CSR_WRITE(sc, WMREG_CTRL_EXT, reg);
   6699 
   6700 			/* Bypass RX and TX FIFOs */
   6701 			wm_kmrn_writereg(sc, KUMCTRLSTA_OFFSET_FIFO_CTRL,
   6702 			    KUMCTRLSTA_FIFO_CTRL_RX_BYPASS
   6703 			    | KUMCTRLSTA_FIFO_CTRL_TX_BYPASS);
   6704 			wm_kmrn_writereg(sc, KUMCTRLSTA_OFFSET_INB_CTRL,
   6705 			    KUMCTRLSTA_INB_CTRL_DIS_PADDING |
   6706 			    KUMCTRLSTA_INB_CTRL_LINK_TMOUT_DFLT);
   6707 		}
   6708 	}
   6709 #if 0
   6710 	CSR_WRITE(sc, WMREG_CTRL_EXT, sc->sc_ctrl_ext);
   6711 #endif
   6712 
   6713 	/* Set up checksum offload parameters. */
   6714 	reg = CSR_READ(sc, WMREG_RXCSUM);
   6715 	reg &= ~(RXCSUM_IPOFL | RXCSUM_IPV6OFL | RXCSUM_TUOFL);
   6716 	if (ifp->if_capenable & IFCAP_CSUM_IPv4_Rx)
   6717 		reg |= RXCSUM_IPOFL;
   6718 	if (ifp->if_capenable & (IFCAP_CSUM_TCPv4_Rx | IFCAP_CSUM_UDPv4_Rx))
   6719 		reg |= RXCSUM_IPOFL | RXCSUM_TUOFL;
   6720 	if (ifp->if_capenable & (IFCAP_CSUM_TCPv6_Rx | IFCAP_CSUM_UDPv6_Rx))
   6721 		reg |= RXCSUM_IPV6OFL | RXCSUM_TUOFL;
   6722 	CSR_WRITE(sc, WMREG_RXCSUM, reg);
   6723 
   6724 	/* Set registers about MSI-X */
   6725 	if (wm_is_using_msix(sc)) {
   6726 		uint32_t ivar, qintr_idx;
   6727 		struct wm_queue *wmq;
   6728 		unsigned int qid;
   6729 
   6730 		if (sc->sc_type == WM_T_82575) {
   6731 			/* Interrupt control */
   6732 			reg = CSR_READ(sc, WMREG_CTRL_EXT);
   6733 			reg |= CTRL_EXT_PBA | CTRL_EXT_EIAME | CTRL_EXT_NSICR;
   6734 			CSR_WRITE(sc, WMREG_CTRL_EXT, reg);
   6735 
   6736 			/* TX and RX */
   6737 			for (i = 0; i < sc->sc_nqueues; i++) {
   6738 				wmq = &sc->sc_queue[i];
   6739 				CSR_WRITE(sc, WMREG_MSIXBM(wmq->wmq_intr_idx),
   6740 				    EITR_TX_QUEUE(wmq->wmq_id)
   6741 				    | EITR_RX_QUEUE(wmq->wmq_id));
   6742 			}
   6743 			/* Link status */
   6744 			CSR_WRITE(sc, WMREG_MSIXBM(sc->sc_link_intr_idx),
   6745 			    EITR_OTHER);
   6746 		} else if (sc->sc_type == WM_T_82574) {
   6747 			/* Interrupt control */
   6748 			reg = CSR_READ(sc, WMREG_CTRL_EXT);
   6749 			reg |= CTRL_EXT_PBA | CTRL_EXT_EIAME;
   6750 			CSR_WRITE(sc, WMREG_CTRL_EXT, reg);
   6751 
   6752 			/*
   6753 			 * Work around issue with spurious interrupts
   6754 			 * in MSI-X mode.
   6755 			 * At wm_initialize_hardware_bits(), sc_nintrs has not
   6756 			 * initialized yet. So re-initialize WMREG_RFCTL here.
   6757 			 */
   6758 			reg = CSR_READ(sc, WMREG_RFCTL);
   6759 			reg |= WMREG_RFCTL_ACKDIS;
   6760 			CSR_WRITE(sc, WMREG_RFCTL, reg);
   6761 
   6762 			ivar = 0;
   6763 			/* TX and RX */
   6764 			for (i = 0; i < sc->sc_nqueues; i++) {
   6765 				wmq = &sc->sc_queue[i];
   6766 				qid = wmq->wmq_id;
   6767 				qintr_idx = wmq->wmq_intr_idx;
   6768 
   6769 				ivar |= __SHIFTIN((IVAR_VALID_82574|qintr_idx),
   6770 				    IVAR_TX_MASK_Q_82574(qid));
   6771 				ivar |= __SHIFTIN((IVAR_VALID_82574|qintr_idx),
   6772 				    IVAR_RX_MASK_Q_82574(qid));
   6773 			}
   6774 			/* Link status */
   6775 			ivar |= __SHIFTIN((IVAR_VALID_82574
   6776 				| sc->sc_link_intr_idx), IVAR_OTHER_MASK);
   6777 			CSR_WRITE(sc, WMREG_IVAR, ivar | IVAR_INT_ON_ALL_WB);
   6778 		} else {
   6779 			/* Interrupt control */
   6780 			CSR_WRITE(sc, WMREG_GPIE, GPIE_NSICR | GPIE_MULTI_MSIX
   6781 			    | GPIE_EIAME | GPIE_PBA);
   6782 
   6783 			switch (sc->sc_type) {
   6784 			case WM_T_82580:
   6785 			case WM_T_I350:
   6786 			case WM_T_I354:
   6787 			case WM_T_I210:
   6788 			case WM_T_I211:
   6789 				/* TX and RX */
   6790 				for (i = 0; i < sc->sc_nqueues; i++) {
   6791 					wmq = &sc->sc_queue[i];
   6792 					qid = wmq->wmq_id;
   6793 					qintr_idx = wmq->wmq_intr_idx;
   6794 
   6795 					ivar = CSR_READ(sc, WMREG_IVAR_Q(qid));
   6796 					ivar &= ~IVAR_TX_MASK_Q(qid);
   6797 					ivar |= __SHIFTIN((qintr_idx
   6798 						| IVAR_VALID),
   6799 					    IVAR_TX_MASK_Q(qid));
   6800 					ivar &= ~IVAR_RX_MASK_Q(qid);
   6801 					ivar |= __SHIFTIN((qintr_idx
   6802 						| IVAR_VALID),
   6803 					    IVAR_RX_MASK_Q(qid));
   6804 					CSR_WRITE(sc, WMREG_IVAR_Q(qid), ivar);
   6805 				}
   6806 				break;
   6807 			case WM_T_82576:
   6808 				/* TX and RX */
   6809 				for (i = 0; i < sc->sc_nqueues; i++) {
   6810 					wmq = &sc->sc_queue[i];
   6811 					qid = wmq->wmq_id;
   6812 					qintr_idx = wmq->wmq_intr_idx;
   6813 
   6814 					ivar = CSR_READ(sc,
   6815 					    WMREG_IVAR_Q_82576(qid));
   6816 					ivar &= ~IVAR_TX_MASK_Q_82576(qid);
   6817 					ivar |= __SHIFTIN((qintr_idx
   6818 						| IVAR_VALID),
   6819 					    IVAR_TX_MASK_Q_82576(qid));
   6820 					ivar &= ~IVAR_RX_MASK_Q_82576(qid);
   6821 					ivar |= __SHIFTIN((qintr_idx
   6822 						| IVAR_VALID),
   6823 					    IVAR_RX_MASK_Q_82576(qid));
   6824 					CSR_WRITE(sc, WMREG_IVAR_Q_82576(qid),
   6825 					    ivar);
   6826 				}
   6827 				break;
   6828 			default:
   6829 				break;
   6830 			}
   6831 
   6832 			/* Link status */
   6833 			ivar = __SHIFTIN((sc->sc_link_intr_idx | IVAR_VALID),
   6834 			    IVAR_MISC_OTHER);
   6835 			CSR_WRITE(sc, WMREG_IVAR_MISC, ivar);
   6836 		}
   6837 
   6838 		if (wm_is_using_multiqueue(sc)) {
   6839 			wm_init_rss(sc);
   6840 
   6841 			/*
   6842 			** NOTE: Receive Full-Packet Checksum Offload
   6843 			** is mutually exclusive with Multiqueue. However
   6844 			** this is not the same as TCP/IP checksums which
   6845 			** still work.
   6846 			*/
   6847 			reg = CSR_READ(sc, WMREG_RXCSUM);
   6848 			reg |= RXCSUM_PCSD;
   6849 			CSR_WRITE(sc, WMREG_RXCSUM, reg);
   6850 		}
   6851 	}
   6852 
   6853 	/* Set up the interrupt registers. */
   6854 	CSR_WRITE(sc, WMREG_IMC, 0xffffffffU);
   6855 
   6856 	/* Enable SFP module insertion interrupt if it's required */
   6857 	if ((sc->sc_flags & WM_F_SFP) != 0) {
   6858 		sc->sc_ctrl |= CTRL_EXTLINK_EN;
   6859 		CSR_WRITE(sc, WMREG_CTRL, sc->sc_ctrl);
   6860 		sfp_mask = ICR_GPI(0);
   6861 	}
   6862 
   6863 	if (wm_is_using_msix(sc)) {
   6864 		uint32_t mask;
   6865 		struct wm_queue *wmq;
   6866 
   6867 		switch (sc->sc_type) {
   6868 		case WM_T_82574:
   6869 			mask = 0;
   6870 			for (i = 0; i < sc->sc_nqueues; i++) {
   6871 				wmq = &sc->sc_queue[i];
   6872 				mask |= ICR_TXQ(wmq->wmq_id);
   6873 				mask |= ICR_RXQ(wmq->wmq_id);
   6874 			}
   6875 			mask |= ICR_OTHER;
   6876 			CSR_WRITE(sc, WMREG_EIAC_82574, mask);
   6877 			CSR_WRITE(sc, WMREG_IMS, mask | ICR_LSC);
   6878 			break;
   6879 		default:
   6880 			if (sc->sc_type == WM_T_82575) {
   6881 				mask = 0;
   6882 				for (i = 0; i < sc->sc_nqueues; i++) {
   6883 					wmq = &sc->sc_queue[i];
   6884 					mask |= EITR_TX_QUEUE(wmq->wmq_id);
   6885 					mask |= EITR_RX_QUEUE(wmq->wmq_id);
   6886 				}
   6887 				mask |= EITR_OTHER;
   6888 			} else {
   6889 				mask = 0;
   6890 				for (i = 0; i < sc->sc_nqueues; i++) {
   6891 					wmq = &sc->sc_queue[i];
   6892 					mask |= 1 << wmq->wmq_intr_idx;
   6893 				}
   6894 				mask |= 1 << sc->sc_link_intr_idx;
   6895 			}
   6896 			CSR_WRITE(sc, WMREG_EIAC, mask);
   6897 			CSR_WRITE(sc, WMREG_EIAM, mask);
   6898 			CSR_WRITE(sc, WMREG_EIMS, mask);
   6899 
   6900 			/* For other interrupts */
   6901 			CSR_WRITE(sc, WMREG_IMS, ICR_LSC | sfp_mask);
   6902 			break;
   6903 		}
   6904 	} else {
   6905 		sc->sc_icr = ICR_TXDW | ICR_LSC | ICR_RXSEQ | ICR_RXDMT0 |
   6906 		    ICR_RXO | ICR_RXT0 | sfp_mask;
   6907 		CSR_WRITE(sc, WMREG_IMS, sc->sc_icr);
   6908 	}
   6909 
   6910 	/* Set up the inter-packet gap. */
   6911 	CSR_WRITE(sc, WMREG_TIPG, sc->sc_tipg);
   6912 
   6913 	if (sc->sc_type >= WM_T_82543) {
   6914 		for (int qidx = 0; qidx < sc->sc_nqueues; qidx++) {
   6915 			struct wm_queue *wmq = &sc->sc_queue[qidx];
   6916 			wm_itrs_writereg(sc, wmq);
   6917 		}
   6918 		/*
   6919 		 * Link interrupts occur much less than TX
   6920 		 * interrupts and RX interrupts. So, we don't
   6921 		 * tune EINTR(WM_MSIX_LINKINTR_IDX) value like
   6922 		 * FreeBSD's if_igb.
   6923 		 */
   6924 	}
   6925 
   6926 	/* Set the VLAN EtherType. */
   6927 	CSR_WRITE(sc, WMREG_VET, ETHERTYPE_VLAN);
   6928 
   6929 	/*
   6930 	 * Set up the transmit control register; we start out with
   6931 	 * a collision distance suitable for FDX, but update it when
   6932 	 * we resolve the media type.
   6933 	 */
   6934 	sc->sc_tctl = TCTL_EN | TCTL_PSP | TCTL_RTLC
   6935 	    | TCTL_CT(TX_COLLISION_THRESHOLD)
   6936 	    | TCTL_COLD(TX_COLLISION_DISTANCE_FDX);
   6937 	if (sc->sc_type >= WM_T_82571)
   6938 		sc->sc_tctl |= TCTL_MULR;
   6939 	CSR_WRITE(sc, WMREG_TCTL, sc->sc_tctl);
   6940 
   6941 	if ((sc->sc_flags & WM_F_NEWQUEUE) != 0) {
   6942 		/* Write TDT after TCTL.EN is set. See the document. */
   6943 		CSR_WRITE(sc, WMREG_TDT(0), 0);
   6944 	}
   6945 
   6946 	if (sc->sc_type == WM_T_80003) {
   6947 		reg = CSR_READ(sc, WMREG_TCTL_EXT);
   6948 		reg &= ~TCTL_EXT_GCEX_MASK;
   6949 		reg |= DEFAULT_80003ES2LAN_TCTL_EXT_GCEX;
   6950 		CSR_WRITE(sc, WMREG_TCTL_EXT, reg);
   6951 	}
   6952 
   6953 	/* Set the media. */
   6954 	if ((error = mii_ifmedia_change(&sc->sc_mii)) != 0)
   6955 		goto out;
   6956 
   6957 	/* Configure for OS presence */
   6958 	wm_init_manageability(sc);
   6959 
   6960 	/*
   6961 	 * Set up the receive control register; we actually program the
   6962 	 * register when we set the receive filter. Use multicast address
   6963 	 * offset type 0.
   6964 	 *
   6965 	 * Only the i82544 has the ability to strip the incoming CRC, so we
   6966 	 * don't enable that feature.
   6967 	 */
   6968 	sc->sc_mchash_type = 0;
   6969 	sc->sc_rctl = RCTL_EN | RCTL_LBM_NONE | RCTL_RDMTS_1_2 | RCTL_DPF
   6970 	    | __SHIFTIN(sc->sc_mchash_type, RCTL_MO);
   6971 
   6972 	/* 82574 use one buffer extended Rx descriptor. */
   6973 	if (sc->sc_type == WM_T_82574)
   6974 		sc->sc_rctl |= RCTL_DTYP_ONEBUF;
   6975 
   6976 	if ((sc->sc_flags & WM_F_CRC_STRIP) != 0)
   6977 		sc->sc_rctl |= RCTL_SECRC;
   6978 
   6979 	if (((ec->ec_capabilities & ETHERCAP_JUMBO_MTU) != 0)
   6980 	    && (ifp->if_mtu > ETHERMTU)) {
   6981 		sc->sc_rctl |= RCTL_LPE;
   6982 		if ((sc->sc_flags & WM_F_NEWQUEUE) != 0)
   6983 			CSR_WRITE(sc, WMREG_RLPML, ETHER_MAX_LEN_JUMBO);
   6984 	}
   6985 
   6986 	if (MCLBYTES == 2048)
   6987 		sc->sc_rctl |= RCTL_2k;
   6988 	else {
   6989 		if (sc->sc_type >= WM_T_82543) {
   6990 			switch (MCLBYTES) {
   6991 			case 4096:
   6992 				sc->sc_rctl |= RCTL_BSEX | RCTL_BSEX_4k;
   6993 				break;
   6994 			case 8192:
   6995 				sc->sc_rctl |= RCTL_BSEX | RCTL_BSEX_8k;
   6996 				break;
   6997 			case 16384:
   6998 				sc->sc_rctl |= RCTL_BSEX | RCTL_BSEX_16k;
   6999 				break;
   7000 			default:
   7001 				panic("wm_init: MCLBYTES %d unsupported",
   7002 				    MCLBYTES);
   7003 				break;
   7004 			}
   7005 		} else
   7006 			panic("wm_init: i82542 requires MCLBYTES = 2048");
   7007 	}
   7008 
   7009 	/* Enable ECC */
   7010 	switch (sc->sc_type) {
   7011 	case WM_T_82571:
   7012 		reg = CSR_READ(sc, WMREG_PBA_ECC);
   7013 		reg |= PBA_ECC_CORR_EN;
   7014 		CSR_WRITE(sc, WMREG_PBA_ECC, reg);
   7015 		break;
   7016 	case WM_T_PCH_LPT:
   7017 	case WM_T_PCH_SPT:
   7018 	case WM_T_PCH_CNP:
   7019 		reg = CSR_READ(sc, WMREG_PBECCSTS);
   7020 		reg |= PBECCSTS_UNCORR_ECC_ENABLE;
   7021 		CSR_WRITE(sc, WMREG_PBECCSTS, reg);
   7022 
   7023 		sc->sc_ctrl |= CTRL_MEHE;
   7024 		CSR_WRITE(sc, WMREG_CTRL, sc->sc_ctrl);
   7025 		break;
   7026 	default:
   7027 		break;
   7028 	}
   7029 
   7030 	/*
   7031 	 * Set the receive filter.
   7032 	 *
   7033 	 * For 82575 and 82576, the RX descriptors must be initialized after
   7034 	 * the setting of RCTL.EN in wm_set_filter()
   7035 	 */
   7036 	wm_set_filter(sc);
   7037 
   7038 	/* On 575 and later set RDT only if RX enabled */
   7039 	if ((sc->sc_flags & WM_F_NEWQUEUE) != 0) {
   7040 		int qidx;
   7041 		for (qidx = 0; qidx < sc->sc_nqueues; qidx++) {
   7042 			struct wm_rxqueue *rxq = &sc->sc_queue[qidx].wmq_rxq;
   7043 			for (i = 0; i < WM_NRXDESC; i++) {
   7044 				mutex_enter(rxq->rxq_lock);
   7045 				wm_init_rxdesc(rxq, i);
   7046 				mutex_exit(rxq->rxq_lock);
   7047 
   7048 			}
   7049 		}
   7050 	}
   7051 
   7052 	wm_unset_stopping_flags(sc);
   7053 
   7054 	/* Start the one second link check clock. */
   7055 	callout_schedule(&sc->sc_tick_ch, hz);
   7056 
   7057 	/*
   7058 	 * ...all done! (IFNET_LOCKED asserted above.)
   7059 	 */
   7060 	ifp->if_flags |= IFF_RUNNING;
   7061 
   7062  out:
   7063 	/* Save last flags for the callback */
   7064 	sc->sc_if_flags = ifp->if_flags;
   7065 	sc->sc_ec_capenable = ec->ec_capenable;
   7066 	if (error)
   7067 		log(LOG_ERR, "%s: interface not running\n",
   7068 		    device_xname(sc->sc_dev));
   7069 	return error;
   7070 }
   7071 
   7072 /*
   7073  * wm_stop:		[ifnet interface function]
   7074  *
   7075  *	Stop transmission on the interface.
   7076  */
   7077 static void
   7078 wm_stop(struct ifnet *ifp, int disable)
   7079 {
   7080 	struct wm_softc *sc = ifp->if_softc;
   7081 
   7082 	ASSERT_SLEEPABLE();
   7083 
   7084 	WM_CORE_LOCK(sc);
   7085 	wm_stop_locked(ifp, disable ? true : false, true);
   7086 	WM_CORE_UNLOCK(sc);
   7087 
   7088 	/*
   7089 	 * After wm_set_stopping_flags(), it is guaranteed that
   7090 	 * wm_handle_queue_work() does not call workqueue_enqueue().
   7091 	 * However, workqueue_wait() cannot call in wm_stop_locked()
   7092 	 * because it can sleep...
   7093 	 * so, call workqueue_wait() here.
   7094 	 */
   7095 	for (int i = 0; i < sc->sc_nqueues; i++)
   7096 		workqueue_wait(sc->sc_queue_wq, &sc->sc_queue[i].wmq_cookie);
   7097 }
   7098 
   7099 static void
   7100 wm_stop_locked(struct ifnet *ifp, bool disable, bool wait)
   7101 {
   7102 	struct wm_softc *sc = ifp->if_softc;
   7103 	struct wm_txsoft *txs;
   7104 	int i, qidx;
   7105 
   7106 	DPRINTF(sc, WM_DEBUG_INIT, ("%s: %s called\n",
   7107 		device_xname(sc->sc_dev), __func__));
   7108 	KASSERT(WM_CORE_LOCKED(sc));
   7109 
   7110 	wm_set_stopping_flags(sc);
   7111 
   7112 	if (sc->sc_flags & WM_F_HAS_MII) {
   7113 		/* Down the MII. */
   7114 		mii_down(&sc->sc_mii);
   7115 	} else {
   7116 #if 0
   7117 		/* Should we clear PHY's status properly? */
   7118 		wm_reset(sc);
   7119 #endif
   7120 	}
   7121 
   7122 	/* Stop the transmit and receive processes. */
   7123 	CSR_WRITE(sc, WMREG_TCTL, 0);
   7124 	CSR_WRITE(sc, WMREG_RCTL, 0);
   7125 	sc->sc_rctl &= ~RCTL_EN;
   7126 
   7127 	/*
   7128 	 * Clear the interrupt mask to ensure the device cannot assert its
   7129 	 * interrupt line.
   7130 	 * Clear sc->sc_icr to ensure wm_intr_legacy() makes no attempt to
   7131 	 * service any currently pending or shared interrupt.
   7132 	 */
   7133 	CSR_WRITE(sc, WMREG_IMC, 0xffffffffU);
   7134 	sc->sc_icr = 0;
   7135 	if (wm_is_using_msix(sc)) {
   7136 		if (sc->sc_type != WM_T_82574) {
   7137 			CSR_WRITE(sc, WMREG_EIMC, 0xffffffffU);
   7138 			CSR_WRITE(sc, WMREG_EIAC, 0);
   7139 		} else
   7140 			CSR_WRITE(sc, WMREG_EIAC_82574, 0);
   7141 	}
   7142 
   7143 	/*
   7144 	 * Stop callouts after interrupts are disabled; if we have
   7145 	 * to wait for them, we will be releasing the CORE_LOCK
   7146 	 * briefly, which will unblock interrupts on the current CPU.
   7147 	 */
   7148 
   7149 	/* Stop the one second clock. */
   7150 	if (wait)
   7151 		callout_halt(&sc->sc_tick_ch, sc->sc_core_lock);
   7152 	else
   7153 		callout_stop(&sc->sc_tick_ch);
   7154 
   7155 	/* Stop the 82547 Tx FIFO stall check timer. */
   7156 	if (sc->sc_type == WM_T_82547) {
   7157 		if (wait)
   7158 			callout_halt(&sc->sc_txfifo_ch, sc->sc_core_lock);
   7159 		else
   7160 			callout_stop(&sc->sc_txfifo_ch);
   7161 	}
   7162 
   7163 	/* Release any queued transmit buffers. */
   7164 	for (qidx = 0; qidx < sc->sc_nqueues; qidx++) {
   7165 		struct wm_queue *wmq = &sc->sc_queue[qidx];
   7166 		struct wm_txqueue *txq = &wmq->wmq_txq;
   7167 		struct mbuf *m;
   7168 
   7169 		mutex_enter(txq->txq_lock);
   7170 		txq->txq_sending = false; /* Ensure watchdog disabled */
   7171 		for (i = 0; i < WM_TXQUEUELEN(txq); i++) {
   7172 			txs = &txq->txq_soft[i];
   7173 			if (txs->txs_mbuf != NULL) {
   7174 				bus_dmamap_unload(sc->sc_dmat,txs->txs_dmamap);
   7175 				m_freem(txs->txs_mbuf);
   7176 				txs->txs_mbuf = NULL;
   7177 			}
   7178 		}
   7179 		/* Drain txq_interq */
   7180 		while ((m = pcq_get(txq->txq_interq)) != NULL)
   7181 			m_freem(m);
   7182 		mutex_exit(txq->txq_lock);
   7183 	}
   7184 
   7185 	/* Mark the interface as down and cancel the watchdog timer. */
   7186 	ifp->if_flags &= ~IFF_RUNNING;
   7187 
   7188 	if (disable) {
   7189 		for (i = 0; i < sc->sc_nqueues; i++) {
   7190 			struct wm_rxqueue *rxq = &sc->sc_queue[i].wmq_rxq;
   7191 			mutex_enter(rxq->rxq_lock);
   7192 			wm_rxdrain(rxq);
   7193 			mutex_exit(rxq->rxq_lock);
   7194 		}
   7195 	}
   7196 
   7197 #if 0 /* notyet */
   7198 	if (sc->sc_type >= WM_T_82544)
   7199 		CSR_WRITE(sc, WMREG_WUC, 0);
   7200 #endif
   7201 }
   7202 
   7203 static void
   7204 wm_dump_mbuf_chain(struct wm_softc *sc, struct mbuf *m0)
   7205 {
   7206 	struct mbuf *m;
   7207 	int i;
   7208 
   7209 	log(LOG_DEBUG, "%s: mbuf chain:\n", device_xname(sc->sc_dev));
   7210 	for (m = m0, i = 0; m != NULL; m = m->m_next, i++)
   7211 		log(LOG_DEBUG, "%s:\tm_data = %p, m_len = %d, "
   7212 		    "m_flags = 0x%08x\n", device_xname(sc->sc_dev),
   7213 		    m->m_data, m->m_len, m->m_flags);
   7214 	log(LOG_DEBUG, "%s:\t%d mbuf%s in chain\n", device_xname(sc->sc_dev),
   7215 	    i, i == 1 ? "" : "s");
   7216 }
   7217 
   7218 /*
   7219  * wm_82547_txfifo_stall:
   7220  *
   7221  *	Callout used to wait for the 82547 Tx FIFO to drain,
   7222  *	reset the FIFO pointers, and restart packet transmission.
   7223  */
   7224 static void
   7225 wm_82547_txfifo_stall(void *arg)
   7226 {
   7227 	struct wm_softc *sc = arg;
   7228 	struct wm_txqueue *txq = &sc->sc_queue[0].wmq_txq;
   7229 
   7230 	mutex_enter(txq->txq_lock);
   7231 
   7232 	if (txq->txq_stopping)
   7233 		goto out;
   7234 
   7235 	if (txq->txq_fifo_stall) {
   7236 		if (CSR_READ(sc, WMREG_TDT(0)) == CSR_READ(sc, WMREG_TDH(0)) &&
   7237 		    CSR_READ(sc, WMREG_TDFT) == CSR_READ(sc, WMREG_TDFH) &&
   7238 		    CSR_READ(sc, WMREG_TDFTS) == CSR_READ(sc, WMREG_TDFHS)) {
   7239 			/*
   7240 			 * Packets have drained.  Stop transmitter, reset
   7241 			 * FIFO pointers, restart transmitter, and kick
   7242 			 * the packet queue.
   7243 			 */
   7244 			uint32_t tctl = CSR_READ(sc, WMREG_TCTL);
   7245 			CSR_WRITE(sc, WMREG_TCTL, tctl & ~TCTL_EN);
   7246 			CSR_WRITE(sc, WMREG_TDFT, txq->txq_fifo_addr);
   7247 			CSR_WRITE(sc, WMREG_TDFH, txq->txq_fifo_addr);
   7248 			CSR_WRITE(sc, WMREG_TDFTS, txq->txq_fifo_addr);
   7249 			CSR_WRITE(sc, WMREG_TDFHS, txq->txq_fifo_addr);
   7250 			CSR_WRITE(sc, WMREG_TCTL, tctl);
   7251 			CSR_WRITE_FLUSH(sc);
   7252 
   7253 			txq->txq_fifo_head = 0;
   7254 			txq->txq_fifo_stall = 0;
   7255 			wm_start_locked(&sc->sc_ethercom.ec_if);
   7256 		} else {
   7257 			/*
   7258 			 * Still waiting for packets to drain; try again in
   7259 			 * another tick.
   7260 			 */
   7261 			callout_schedule(&sc->sc_txfifo_ch, 1);
   7262 		}
   7263 	}
   7264 
   7265 out:
   7266 	mutex_exit(txq->txq_lock);
   7267 }
   7268 
   7269 /*
   7270  * wm_82547_txfifo_bugchk:
   7271  *
   7272  *	Check for bug condition in the 82547 Tx FIFO.  We need to
   7273  *	prevent enqueueing a packet that would wrap around the end
   7274  *	if the Tx FIFO ring buffer, otherwise the chip will croak.
   7275  *
   7276  *	We do this by checking the amount of space before the end
   7277  *	of the Tx FIFO buffer. If the packet will not fit, we "stall"
   7278  *	the Tx FIFO, wait for all remaining packets to drain, reset
   7279  *	the internal FIFO pointers to the beginning, and restart
   7280  *	transmission on the interface.
   7281  */
   7282 #define	WM_FIFO_HDR		0x10
   7283 #define	WM_82547_PAD_LEN	0x3e0
   7284 static int
   7285 wm_82547_txfifo_bugchk(struct wm_softc *sc, struct mbuf *m0)
   7286 {
   7287 	struct wm_txqueue *txq = &sc->sc_queue[0].wmq_txq;
   7288 	int space = txq->txq_fifo_size - txq->txq_fifo_head;
   7289 	int len = roundup(m0->m_pkthdr.len + WM_FIFO_HDR, WM_FIFO_HDR);
   7290 
   7291 	/* Just return if already stalled. */
   7292 	if (txq->txq_fifo_stall)
   7293 		return 1;
   7294 
   7295 	if (sc->sc_mii.mii_media_active & IFM_FDX) {
   7296 		/* Stall only occurs in half-duplex mode. */
   7297 		goto send_packet;
   7298 	}
   7299 
   7300 	if (len >= WM_82547_PAD_LEN + space) {
   7301 		txq->txq_fifo_stall = 1;
   7302 		callout_schedule(&sc->sc_txfifo_ch, 1);
   7303 		return 1;
   7304 	}
   7305 
   7306  send_packet:
   7307 	txq->txq_fifo_head += len;
   7308 	if (txq->txq_fifo_head >= txq->txq_fifo_size)
   7309 		txq->txq_fifo_head -= txq->txq_fifo_size;
   7310 
   7311 	return 0;
   7312 }
   7313 
   7314 static int
   7315 wm_alloc_tx_descs(struct wm_softc *sc, struct wm_txqueue *txq)
   7316 {
   7317 	int error;
   7318 
   7319 	/*
   7320 	 * Allocate the control data structures, and create and load the
   7321 	 * DMA map for it.
   7322 	 *
   7323 	 * NOTE: All Tx descriptors must be in the same 4G segment of
   7324 	 * memory.  So must Rx descriptors.  We simplify by allocating
   7325 	 * both sets within the same 4G segment.
   7326 	 */
   7327 	if (sc->sc_type < WM_T_82544)
   7328 		WM_NTXDESC(txq) = WM_NTXDESC_82542;
   7329 	else
   7330 		WM_NTXDESC(txq) = WM_NTXDESC_82544;
   7331 	if ((sc->sc_flags & WM_F_NEWQUEUE) != 0)
   7332 		txq->txq_descsize = sizeof(nq_txdesc_t);
   7333 	else
   7334 		txq->txq_descsize = sizeof(wiseman_txdesc_t);
   7335 
   7336 	if ((error = bus_dmamem_alloc(sc->sc_dmat, WM_TXDESCS_SIZE(txq),
   7337 		    PAGE_SIZE, (bus_size_t) 0x100000000ULL, &txq->txq_desc_seg,
   7338 		    1, &txq->txq_desc_rseg, 0)) != 0) {
   7339 		aprint_error_dev(sc->sc_dev,
   7340 		    "unable to allocate TX control data, error = %d\n",
   7341 		    error);
   7342 		goto fail_0;
   7343 	}
   7344 
   7345 	if ((error = bus_dmamem_map(sc->sc_dmat, &txq->txq_desc_seg,
   7346 		    txq->txq_desc_rseg, WM_TXDESCS_SIZE(txq),
   7347 		    (void **)&txq->txq_descs_u, BUS_DMA_COHERENT)) != 0) {
   7348 		aprint_error_dev(sc->sc_dev,
   7349 		    "unable to map TX control data, error = %d\n", error);
   7350 		goto fail_1;
   7351 	}
   7352 
   7353 	if ((error = bus_dmamap_create(sc->sc_dmat, WM_TXDESCS_SIZE(txq), 1,
   7354 		    WM_TXDESCS_SIZE(txq), 0, 0, &txq->txq_desc_dmamap)) != 0) {
   7355 		aprint_error_dev(sc->sc_dev,
   7356 		    "unable to create TX control data DMA map, error = %d\n",
   7357 		    error);
   7358 		goto fail_2;
   7359 	}
   7360 
   7361 	if ((error = bus_dmamap_load(sc->sc_dmat, txq->txq_desc_dmamap,
   7362 		    txq->txq_descs_u, WM_TXDESCS_SIZE(txq), NULL, 0)) != 0) {
   7363 		aprint_error_dev(sc->sc_dev,
   7364 		    "unable to load TX control data DMA map, error = %d\n",
   7365 		    error);
   7366 		goto fail_3;
   7367 	}
   7368 
   7369 	return 0;
   7370 
   7371  fail_3:
   7372 	bus_dmamap_destroy(sc->sc_dmat, txq->txq_desc_dmamap);
   7373  fail_2:
   7374 	bus_dmamem_unmap(sc->sc_dmat, (void *)txq->txq_descs_u,
   7375 	    WM_TXDESCS_SIZE(txq));
   7376  fail_1:
   7377 	bus_dmamem_free(sc->sc_dmat, &txq->txq_desc_seg, txq->txq_desc_rseg);
   7378  fail_0:
   7379 	return error;
   7380 }
   7381 
   7382 static void
   7383 wm_free_tx_descs(struct wm_softc *sc, struct wm_txqueue *txq)
   7384 {
   7385 
   7386 	bus_dmamap_unload(sc->sc_dmat, txq->txq_desc_dmamap);
   7387 	bus_dmamap_destroy(sc->sc_dmat, txq->txq_desc_dmamap);
   7388 	bus_dmamem_unmap(sc->sc_dmat, (void *)txq->txq_descs_u,
   7389 	    WM_TXDESCS_SIZE(txq));
   7390 	bus_dmamem_free(sc->sc_dmat, &txq->txq_desc_seg, txq->txq_desc_rseg);
   7391 }
   7392 
   7393 static int
   7394 wm_alloc_rx_descs(struct wm_softc *sc, struct wm_rxqueue *rxq)
   7395 {
   7396 	int error;
   7397 	size_t rxq_descs_size;
   7398 
   7399 	/*
   7400 	 * Allocate the control data structures, and create and load the
   7401 	 * DMA map for it.
   7402 	 *
   7403 	 * NOTE: All Tx descriptors must be in the same 4G segment of
   7404 	 * memory.  So must Rx descriptors.  We simplify by allocating
   7405 	 * both sets within the same 4G segment.
   7406 	 */
   7407 	rxq->rxq_ndesc = WM_NRXDESC;
   7408 	if (sc->sc_type == WM_T_82574)
   7409 		rxq->rxq_descsize = sizeof(ext_rxdesc_t);
   7410 	else if ((sc->sc_flags & WM_F_NEWQUEUE) != 0)
   7411 		rxq->rxq_descsize = sizeof(nq_rxdesc_t);
   7412 	else
   7413 		rxq->rxq_descsize = sizeof(wiseman_rxdesc_t);
   7414 	rxq_descs_size = rxq->rxq_descsize * rxq->rxq_ndesc;
   7415 
   7416 	if ((error = bus_dmamem_alloc(sc->sc_dmat, rxq_descs_size,
   7417 		    PAGE_SIZE, (bus_size_t) 0x100000000ULL, &rxq->rxq_desc_seg,
   7418 		    1, &rxq->rxq_desc_rseg, 0)) != 0) {
   7419 		aprint_error_dev(sc->sc_dev,
   7420 		    "unable to allocate RX control data, error = %d\n",
   7421 		    error);
   7422 		goto fail_0;
   7423 	}
   7424 
   7425 	if ((error = bus_dmamem_map(sc->sc_dmat, &rxq->rxq_desc_seg,
   7426 		    rxq->rxq_desc_rseg, rxq_descs_size,
   7427 		    (void **)&rxq->rxq_descs_u, BUS_DMA_COHERENT)) != 0) {
   7428 		aprint_error_dev(sc->sc_dev,
   7429 		    "unable to map RX control data, error = %d\n", error);
   7430 		goto fail_1;
   7431 	}
   7432 
   7433 	if ((error = bus_dmamap_create(sc->sc_dmat, rxq_descs_size, 1,
   7434 		    rxq_descs_size, 0, 0, &rxq->rxq_desc_dmamap)) != 0) {
   7435 		aprint_error_dev(sc->sc_dev,
   7436 		    "unable to create RX control data DMA map, error = %d\n",
   7437 		    error);
   7438 		goto fail_2;
   7439 	}
   7440 
   7441 	if ((error = bus_dmamap_load(sc->sc_dmat, rxq->rxq_desc_dmamap,
   7442 		    rxq->rxq_descs_u, rxq_descs_size, NULL, 0)) != 0) {
   7443 		aprint_error_dev(sc->sc_dev,
   7444 		    "unable to load RX control data DMA map, error = %d\n",
   7445 		    error);
   7446 		goto fail_3;
   7447 	}
   7448 
   7449 	return 0;
   7450 
   7451  fail_3:
   7452 	bus_dmamap_destroy(sc->sc_dmat, rxq->rxq_desc_dmamap);
   7453  fail_2:
   7454 	bus_dmamem_unmap(sc->sc_dmat, (void *)rxq->rxq_descs_u,
   7455 	    rxq_descs_size);
   7456  fail_1:
   7457 	bus_dmamem_free(sc->sc_dmat, &rxq->rxq_desc_seg, rxq->rxq_desc_rseg);
   7458  fail_0:
   7459 	return error;
   7460 }
   7461 
   7462 static void
   7463 wm_free_rx_descs(struct wm_softc *sc, struct wm_rxqueue *rxq)
   7464 {
   7465 
   7466 	bus_dmamap_unload(sc->sc_dmat, rxq->rxq_desc_dmamap);
   7467 	bus_dmamap_destroy(sc->sc_dmat, rxq->rxq_desc_dmamap);
   7468 	bus_dmamem_unmap(sc->sc_dmat, (void *)rxq->rxq_descs_u,
   7469 	    rxq->rxq_descsize * rxq->rxq_ndesc);
   7470 	bus_dmamem_free(sc->sc_dmat, &rxq->rxq_desc_seg, rxq->rxq_desc_rseg);
   7471 }
   7472 
   7473 
   7474 static int
   7475 wm_alloc_tx_buffer(struct wm_softc *sc, struct wm_txqueue *txq)
   7476 {
   7477 	int i, error;
   7478 
   7479 	/* Create the transmit buffer DMA maps. */
   7480 	WM_TXQUEUELEN(txq) =
   7481 	    (sc->sc_type == WM_T_82547 || sc->sc_type == WM_T_82547_2) ?
   7482 	    WM_TXQUEUELEN_MAX_82547 : WM_TXQUEUELEN_MAX;
   7483 	for (i = 0; i < WM_TXQUEUELEN(txq); i++) {
   7484 		if ((error = bus_dmamap_create(sc->sc_dmat, WM_MAXTXDMA,
   7485 			    WM_NTXSEGS, WTX_MAX_LEN, 0, 0,
   7486 			    &txq->txq_soft[i].txs_dmamap)) != 0) {
   7487 			aprint_error_dev(sc->sc_dev,
   7488 			    "unable to create Tx DMA map %d, error = %d\n",
   7489 			    i, error);
   7490 			goto fail;
   7491 		}
   7492 	}
   7493 
   7494 	return 0;
   7495 
   7496  fail:
   7497 	for (i = 0; i < WM_TXQUEUELEN(txq); i++) {
   7498 		if (txq->txq_soft[i].txs_dmamap != NULL)
   7499 			bus_dmamap_destroy(sc->sc_dmat,
   7500 			    txq->txq_soft[i].txs_dmamap);
   7501 	}
   7502 	return error;
   7503 }
   7504 
   7505 static void
   7506 wm_free_tx_buffer(struct wm_softc *sc, struct wm_txqueue *txq)
   7507 {
   7508 	int i;
   7509 
   7510 	for (i = 0; i < WM_TXQUEUELEN(txq); i++) {
   7511 		if (txq->txq_soft[i].txs_dmamap != NULL)
   7512 			bus_dmamap_destroy(sc->sc_dmat,
   7513 			    txq->txq_soft[i].txs_dmamap);
   7514 	}
   7515 }
   7516 
   7517 static int
   7518 wm_alloc_rx_buffer(struct wm_softc *sc, struct wm_rxqueue *rxq)
   7519 {
   7520 	int i, error;
   7521 
   7522 	/* Create the receive buffer DMA maps. */
   7523 	for (i = 0; i < rxq->rxq_ndesc; i++) {
   7524 		if ((error = bus_dmamap_create(sc->sc_dmat, MCLBYTES, 1,
   7525 			    MCLBYTES, 0, 0,
   7526 			    &rxq->rxq_soft[i].rxs_dmamap)) != 0) {
   7527 			aprint_error_dev(sc->sc_dev,
   7528 			    "unable to create Rx DMA map %d error = %d\n",
   7529 			    i, error);
   7530 			goto fail;
   7531 		}
   7532 		rxq->rxq_soft[i].rxs_mbuf = NULL;
   7533 	}
   7534 
   7535 	return 0;
   7536 
   7537  fail:
   7538 	for (i = 0; i < rxq->rxq_ndesc; i++) {
   7539 		if (rxq->rxq_soft[i].rxs_dmamap != NULL)
   7540 			bus_dmamap_destroy(sc->sc_dmat,
   7541 			    rxq->rxq_soft[i].rxs_dmamap);
   7542 	}
   7543 	return error;
   7544 }
   7545 
   7546 static void
   7547 wm_free_rx_buffer(struct wm_softc *sc, struct wm_rxqueue *rxq)
   7548 {
   7549 	int i;
   7550 
   7551 	for (i = 0; i < rxq->rxq_ndesc; i++) {
   7552 		if (rxq->rxq_soft[i].rxs_dmamap != NULL)
   7553 			bus_dmamap_destroy(sc->sc_dmat,
   7554 			    rxq->rxq_soft[i].rxs_dmamap);
   7555 	}
   7556 }
   7557 
   7558 /*
   7559  * wm_alloc_quques:
   7560  *	Allocate {tx,rx}descs and {tx,rx} buffers
   7561  */
   7562 static int
   7563 wm_alloc_txrx_queues(struct wm_softc *sc)
   7564 {
   7565 	int i, error, tx_done, rx_done;
   7566 
   7567 	sc->sc_queue = kmem_zalloc(sizeof(struct wm_queue) * sc->sc_nqueues,
   7568 	    KM_SLEEP);
   7569 	if (sc->sc_queue == NULL) {
   7570 		aprint_error_dev(sc->sc_dev,"unable to allocate wm_queue\n");
   7571 		error = ENOMEM;
   7572 		goto fail_0;
   7573 	}
   7574 
   7575 	/* For transmission */
   7576 	error = 0;
   7577 	tx_done = 0;
   7578 	for (i = 0; i < sc->sc_nqueues; i++) {
   7579 #ifdef WM_EVENT_COUNTERS
   7580 		int j;
   7581 		const char *xname;
   7582 #endif
   7583 		struct wm_txqueue *txq = &sc->sc_queue[i].wmq_txq;
   7584 		txq->txq_sc = sc;
   7585 		txq->txq_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NET);
   7586 
   7587 		error = wm_alloc_tx_descs(sc, txq);
   7588 		if (error)
   7589 			break;
   7590 		error = wm_alloc_tx_buffer(sc, txq);
   7591 		if (error) {
   7592 			wm_free_tx_descs(sc, txq);
   7593 			break;
   7594 		}
   7595 		txq->txq_interq = pcq_create(WM_TXINTERQSIZE, KM_SLEEP);
   7596 		if (txq->txq_interq == NULL) {
   7597 			wm_free_tx_descs(sc, txq);
   7598 			wm_free_tx_buffer(sc, txq);
   7599 			error = ENOMEM;
   7600 			break;
   7601 		}
   7602 
   7603 #ifdef WM_EVENT_COUNTERS
   7604 		xname = device_xname(sc->sc_dev);
   7605 
   7606 		WM_Q_MISC_EVCNT_ATTACH(txq, txsstall, txq, i, xname);
   7607 		WM_Q_MISC_EVCNT_ATTACH(txq, txdstall, txq, i, xname);
   7608 		WM_Q_MISC_EVCNT_ATTACH(txq, fifo_stall, txq, i, xname);
   7609 		WM_Q_INTR_EVCNT_ATTACH(txq, txdw, txq, i, xname);
   7610 		WM_Q_INTR_EVCNT_ATTACH(txq, txqe, txq, i, xname);
   7611 		WM_Q_MISC_EVCNT_ATTACH(txq, ipsum, txq, i, xname);
   7612 		WM_Q_MISC_EVCNT_ATTACH(txq, tusum, txq, i, xname);
   7613 		WM_Q_MISC_EVCNT_ATTACH(txq, tusum6, txq, i, xname);
   7614 		WM_Q_MISC_EVCNT_ATTACH(txq, tso, txq, i, xname);
   7615 		WM_Q_MISC_EVCNT_ATTACH(txq, tso6, txq, i, xname);
   7616 		WM_Q_MISC_EVCNT_ATTACH(txq, tsopain, txq, i, xname);
   7617 
   7618 		for (j = 0; j < WM_NTXSEGS; j++) {
   7619 			snprintf(txq->txq_txseg_evcnt_names[j],
   7620 			    sizeof(txq->txq_txseg_evcnt_names[j]),
   7621 			    "txq%02dtxseg%d", i, j);
   7622 			evcnt_attach_dynamic(&txq->txq_ev_txseg[j],
   7623 			    EVCNT_TYPE_MISC,
   7624 			    NULL, xname, txq->txq_txseg_evcnt_names[j]);
   7625 		}
   7626 
   7627 		WM_Q_MISC_EVCNT_ATTACH(txq, pcqdrop, txq, i, xname);
   7628 		WM_Q_MISC_EVCNT_ATTACH(txq, descdrop, txq, i, xname);
   7629 		WM_Q_MISC_EVCNT_ATTACH(txq, toomanyseg, txq, i, xname);
   7630 		WM_Q_MISC_EVCNT_ATTACH(txq, defrag, txq, i, xname);
   7631 		WM_Q_MISC_EVCNT_ATTACH(txq, underrun, txq, i, xname);
   7632 		WM_Q_MISC_EVCNT_ATTACH(txq, skipcontext, txq, i, xname);
   7633 #endif /* WM_EVENT_COUNTERS */
   7634 
   7635 		tx_done++;
   7636 	}
   7637 	if (error)
   7638 		goto fail_1;
   7639 
   7640 	/* For receive */
   7641 	error = 0;
   7642 	rx_done = 0;
   7643 	for (i = 0; i < sc->sc_nqueues; i++) {
   7644 #ifdef WM_EVENT_COUNTERS
   7645 		const char *xname;
   7646 #endif
   7647 		struct wm_rxqueue *rxq = &sc->sc_queue[i].wmq_rxq;
   7648 		rxq->rxq_sc = sc;
   7649 		rxq->rxq_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NET);
   7650 
   7651 		error = wm_alloc_rx_descs(sc, rxq);
   7652 		if (error)
   7653 			break;
   7654 
   7655 		error = wm_alloc_rx_buffer(sc, rxq);
   7656 		if (error) {
   7657 			wm_free_rx_descs(sc, rxq);
   7658 			break;
   7659 		}
   7660 
   7661 #ifdef WM_EVENT_COUNTERS
   7662 		xname = device_xname(sc->sc_dev);
   7663 
   7664 		WM_Q_INTR_EVCNT_ATTACH(rxq, intr, rxq, i, xname);
   7665 		WM_Q_INTR_EVCNT_ATTACH(rxq, defer, rxq, i, xname);
   7666 
   7667 		WM_Q_MISC_EVCNT_ATTACH(rxq, ipsum, rxq, i, xname);
   7668 		WM_Q_MISC_EVCNT_ATTACH(rxq, tusum, rxq, i, xname);
   7669 #endif /* WM_EVENT_COUNTERS */
   7670 
   7671 		rx_done++;
   7672 	}
   7673 	if (error)
   7674 		goto fail_2;
   7675 
   7676 	return 0;
   7677 
   7678  fail_2:
   7679 	for (i = 0; i < rx_done; i++) {
   7680 		struct wm_rxqueue *rxq = &sc->sc_queue[i].wmq_rxq;
   7681 		wm_free_rx_buffer(sc, rxq);
   7682 		wm_free_rx_descs(sc, rxq);
   7683 		if (rxq->rxq_lock)
   7684 			mutex_obj_free(rxq->rxq_lock);
   7685 	}
   7686  fail_1:
   7687 	for (i = 0; i < tx_done; i++) {
   7688 		struct wm_txqueue *txq = &sc->sc_queue[i].wmq_txq;
   7689 		pcq_destroy(txq->txq_interq);
   7690 		wm_free_tx_buffer(sc, txq);
   7691 		wm_free_tx_descs(sc, txq);
   7692 		if (txq->txq_lock)
   7693 			mutex_obj_free(txq->txq_lock);
   7694 	}
   7695 
   7696 	kmem_free(sc->sc_queue,
   7697 	    sizeof(struct wm_queue) * sc->sc_nqueues);
   7698  fail_0:
   7699 	return error;
   7700 }
   7701 
   7702 /*
   7703  * wm_free_quques:
   7704  *	Free {tx,rx}descs and {tx,rx} buffers
   7705  */
   7706 static void
   7707 wm_free_txrx_queues(struct wm_softc *sc)
   7708 {
   7709 	int i;
   7710 
   7711 	for (i = 0; i < sc->sc_nqueues; i++) {
   7712 		struct wm_rxqueue *rxq = &sc->sc_queue[i].wmq_rxq;
   7713 
   7714 #ifdef WM_EVENT_COUNTERS
   7715 		WM_Q_EVCNT_DETACH(rxq, intr, rxq, i);
   7716 		WM_Q_EVCNT_DETACH(rxq, defer, rxq, i);
   7717 		WM_Q_EVCNT_DETACH(rxq, ipsum, rxq, i);
   7718 		WM_Q_EVCNT_DETACH(rxq, tusum, rxq, i);
   7719 #endif /* WM_EVENT_COUNTERS */
   7720 
   7721 		wm_free_rx_buffer(sc, rxq);
   7722 		wm_free_rx_descs(sc, rxq);
   7723 		if (rxq->rxq_lock)
   7724 			mutex_obj_free(rxq->rxq_lock);
   7725 	}
   7726 
   7727 	for (i = 0; i < sc->sc_nqueues; i++) {
   7728 		struct wm_txqueue *txq = &sc->sc_queue[i].wmq_txq;
   7729 		struct mbuf *m;
   7730 #ifdef WM_EVENT_COUNTERS
   7731 		int j;
   7732 
   7733 		WM_Q_EVCNT_DETACH(txq, txsstall, txq, i);
   7734 		WM_Q_EVCNT_DETACH(txq, txdstall, txq, i);
   7735 		WM_Q_EVCNT_DETACH(txq, fifo_stall, txq, i);
   7736 		WM_Q_EVCNT_DETACH(txq, txdw, txq, i);
   7737 		WM_Q_EVCNT_DETACH(txq, txqe, txq, i);
   7738 		WM_Q_EVCNT_DETACH(txq, ipsum, txq, i);
   7739 		WM_Q_EVCNT_DETACH(txq, tusum, txq, i);
   7740 		WM_Q_EVCNT_DETACH(txq, tusum6, txq, i);
   7741 		WM_Q_EVCNT_DETACH(txq, tso, txq, i);
   7742 		WM_Q_EVCNT_DETACH(txq, tso6, txq, i);
   7743 		WM_Q_EVCNT_DETACH(txq, tsopain, txq, i);
   7744 
   7745 		for (j = 0; j < WM_NTXSEGS; j++)
   7746 			evcnt_detach(&txq->txq_ev_txseg[j]);
   7747 
   7748 		WM_Q_EVCNT_DETACH(txq, pcqdrop, txq, i);
   7749 		WM_Q_EVCNT_DETACH(txq, descdrop, txq, i);
   7750 		WM_Q_EVCNT_DETACH(txq, toomanyseg, txq, i);
   7751 		WM_Q_EVCNT_DETACH(txq, defrag, txq, i);
   7752 		WM_Q_EVCNT_DETACH(txq, underrun, txq, i);
   7753 		WM_Q_EVCNT_DETACH(txq, skipcontext, txq, i);
   7754 #endif /* WM_EVENT_COUNTERS */
   7755 
   7756 		/* Drain txq_interq */
   7757 		while ((m = pcq_get(txq->txq_interq)) != NULL)
   7758 			m_freem(m);
   7759 		pcq_destroy(txq->txq_interq);
   7760 
   7761 		wm_free_tx_buffer(sc, txq);
   7762 		wm_free_tx_descs(sc, txq);
   7763 		if (txq->txq_lock)
   7764 			mutex_obj_free(txq->txq_lock);
   7765 	}
   7766 
   7767 	kmem_free(sc->sc_queue, sizeof(struct wm_queue) * sc->sc_nqueues);
   7768 }
   7769 
   7770 static void
   7771 wm_init_tx_descs(struct wm_softc *sc __unused, struct wm_txqueue *txq)
   7772 {
   7773 
   7774 	KASSERT(mutex_owned(txq->txq_lock));
   7775 
   7776 	/* Initialize the transmit descriptor ring. */
   7777 	memset(txq->txq_descs, 0, WM_TXDESCS_SIZE(txq));
   7778 	wm_cdtxsync(txq, 0, WM_NTXDESC(txq),
   7779 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
   7780 	txq->txq_free = WM_NTXDESC(txq);
   7781 	txq->txq_next = 0;
   7782 }
   7783 
   7784 static void
   7785 wm_init_tx_regs(struct wm_softc *sc, struct wm_queue *wmq,
   7786     struct wm_txqueue *txq)
   7787 {
   7788 
   7789 	DPRINTF(sc, WM_DEBUG_INIT, ("%s: %s called\n",
   7790 		device_xname(sc->sc_dev), __func__));
   7791 	KASSERT(mutex_owned(txq->txq_lock));
   7792 
   7793 	if (sc->sc_type < WM_T_82543) {
   7794 		CSR_WRITE(sc, WMREG_OLD_TDBAH, WM_CDTXADDR_HI(txq, 0));
   7795 		CSR_WRITE(sc, WMREG_OLD_TDBAL, WM_CDTXADDR_LO(txq, 0));
   7796 		CSR_WRITE(sc, WMREG_OLD_TDLEN, WM_TXDESCS_SIZE(txq));
   7797 		CSR_WRITE(sc, WMREG_OLD_TDH, 0);
   7798 		CSR_WRITE(sc, WMREG_OLD_TDT, 0);
   7799 		CSR_WRITE(sc, WMREG_OLD_TIDV, 128);
   7800 	} else {
   7801 		int qid = wmq->wmq_id;
   7802 
   7803 		CSR_WRITE(sc, WMREG_TDBAH(qid), WM_CDTXADDR_HI(txq, 0));
   7804 		CSR_WRITE(sc, WMREG_TDBAL(qid), WM_CDTXADDR_LO(txq, 0));
   7805 		CSR_WRITE(sc, WMREG_TDLEN(qid), WM_TXDESCS_SIZE(txq));
   7806 		CSR_WRITE(sc, WMREG_TDH(qid), 0);
   7807 
   7808 		if ((sc->sc_flags & WM_F_NEWQUEUE) != 0)
   7809 			/*
   7810 			 * Don't write TDT before TCTL.EN is set.
   7811 			 * See the document.
   7812 			 */
   7813 			CSR_WRITE(sc, WMREG_TXDCTL(qid), TXDCTL_QUEUE_ENABLE
   7814 			    | TXDCTL_PTHRESH(0) | TXDCTL_HTHRESH(0)
   7815 			    | TXDCTL_WTHRESH(0));
   7816 		else {
   7817 			/* XXX should update with AIM? */
   7818 			CSR_WRITE(sc, WMREG_TIDV, wmq->wmq_itr / 4);
   7819 			if (sc->sc_type >= WM_T_82540) {
   7820 				/* Should be the same */
   7821 				CSR_WRITE(sc, WMREG_TADV, wmq->wmq_itr / 4);
   7822 			}
   7823 
   7824 			CSR_WRITE(sc, WMREG_TDT(qid), 0);
   7825 			CSR_WRITE(sc, WMREG_TXDCTL(qid), TXDCTL_PTHRESH(0) |
   7826 			    TXDCTL_HTHRESH(0) | TXDCTL_WTHRESH(0));
   7827 		}
   7828 	}
   7829 }
   7830 
   7831 static void
   7832 wm_init_tx_buffer(struct wm_softc *sc __unused, struct wm_txqueue *txq)
   7833 {
   7834 	int i;
   7835 
   7836 	KASSERT(mutex_owned(txq->txq_lock));
   7837 
   7838 	/* Initialize the transmit job descriptors. */
   7839 	for (i = 0; i < WM_TXQUEUELEN(txq); i++)
   7840 		txq->txq_soft[i].txs_mbuf = NULL;
   7841 	txq->txq_sfree = WM_TXQUEUELEN(txq);
   7842 	txq->txq_snext = 0;
   7843 	txq->txq_sdirty = 0;
   7844 }
   7845 
   7846 static void
   7847 wm_init_tx_queue(struct wm_softc *sc, struct wm_queue *wmq,
   7848     struct wm_txqueue *txq)
   7849 {
   7850 
   7851 	KASSERT(mutex_owned(txq->txq_lock));
   7852 
   7853 	/*
   7854 	 * Set up some register offsets that are different between
   7855 	 * the i82542 and the i82543 and later chips.
   7856 	 */
   7857 	if (sc->sc_type < WM_T_82543)
   7858 		txq->txq_tdt_reg = WMREG_OLD_TDT;
   7859 	else
   7860 		txq->txq_tdt_reg = WMREG_TDT(wmq->wmq_id);
   7861 
   7862 	wm_init_tx_descs(sc, txq);
   7863 	wm_init_tx_regs(sc, wmq, txq);
   7864 	wm_init_tx_buffer(sc, txq);
   7865 
   7866 	/* Clear other than WM_TXQ_LINKDOWN_DISCARD */
   7867 	txq->txq_flags &= WM_TXQ_LINKDOWN_DISCARD;
   7868 
   7869 	txq->txq_sending = false;
   7870 }
   7871 
   7872 static void
   7873 wm_init_rx_regs(struct wm_softc *sc, struct wm_queue *wmq,
   7874     struct wm_rxqueue *rxq)
   7875 {
   7876 
   7877 	KASSERT(mutex_owned(rxq->rxq_lock));
   7878 
   7879 	/*
   7880 	 * Initialize the receive descriptor and receive job
   7881 	 * descriptor rings.
   7882 	 */
   7883 	if (sc->sc_type < WM_T_82543) {
   7884 		CSR_WRITE(sc, WMREG_OLD_RDBAH0, WM_CDRXADDR_HI(rxq, 0));
   7885 		CSR_WRITE(sc, WMREG_OLD_RDBAL0, WM_CDRXADDR_LO(rxq, 0));
   7886 		CSR_WRITE(sc, WMREG_OLD_RDLEN0,
   7887 		    rxq->rxq_descsize * rxq->rxq_ndesc);
   7888 		CSR_WRITE(sc, WMREG_OLD_RDH0, 0);
   7889 		CSR_WRITE(sc, WMREG_OLD_RDT0, 0);
   7890 		CSR_WRITE(sc, WMREG_OLD_RDTR0, 28 | RDTR_FPD);
   7891 
   7892 		CSR_WRITE(sc, WMREG_OLD_RDBA1_HI, 0);
   7893 		CSR_WRITE(sc, WMREG_OLD_RDBA1_LO, 0);
   7894 		CSR_WRITE(sc, WMREG_OLD_RDLEN1, 0);
   7895 		CSR_WRITE(sc, WMREG_OLD_RDH1, 0);
   7896 		CSR_WRITE(sc, WMREG_OLD_RDT1, 0);
   7897 		CSR_WRITE(sc, WMREG_OLD_RDTR1, 0);
   7898 	} else {
   7899 		int qid = wmq->wmq_id;
   7900 
   7901 		CSR_WRITE(sc, WMREG_RDBAH(qid), WM_CDRXADDR_HI(rxq, 0));
   7902 		CSR_WRITE(sc, WMREG_RDBAL(qid), WM_CDRXADDR_LO(rxq, 0));
   7903 		CSR_WRITE(sc, WMREG_RDLEN(qid),
   7904 		    rxq->rxq_descsize * rxq->rxq_ndesc);
   7905 
   7906 		if ((sc->sc_flags & WM_F_NEWQUEUE) != 0) {
   7907 			if (MCLBYTES & ((1 << SRRCTL_BSIZEPKT_SHIFT) - 1))
   7908 				panic("%s: MCLBYTES %d unsupported for 82575 "
   7909 				    "or higher\n", __func__, MCLBYTES);
   7910 
   7911 			/*
   7912 			 * Currently, support SRRCTL_DESCTYPE_ADV_ONEBUF
   7913 			 * only.
   7914 			 */
   7915 			CSR_WRITE(sc, WMREG_SRRCTL(qid),
   7916 			    SRRCTL_DESCTYPE_ADV_ONEBUF
   7917 			    | (MCLBYTES >> SRRCTL_BSIZEPKT_SHIFT));
   7918 			CSR_WRITE(sc, WMREG_RXDCTL(qid), RXDCTL_QUEUE_ENABLE
   7919 			    | RXDCTL_PTHRESH(16) | RXDCTL_HTHRESH(8)
   7920 			    | RXDCTL_WTHRESH(1));
   7921 			CSR_WRITE(sc, WMREG_RDH(qid), 0);
   7922 			CSR_WRITE(sc, WMREG_RDT(qid), 0);
   7923 		} else {
   7924 			CSR_WRITE(sc, WMREG_RDH(qid), 0);
   7925 			CSR_WRITE(sc, WMREG_RDT(qid), 0);
   7926 			/* XXX should update with AIM? */
   7927 			CSR_WRITE(sc, WMREG_RDTR,
   7928 			    (wmq->wmq_itr / 4) | RDTR_FPD);
   7929 			/* MUST be same */
   7930 			CSR_WRITE(sc, WMREG_RADV, wmq->wmq_itr / 4);
   7931 			CSR_WRITE(sc, WMREG_RXDCTL(qid), RXDCTL_PTHRESH(0) |
   7932 			    RXDCTL_HTHRESH(0) | RXDCTL_WTHRESH(1));
   7933 		}
   7934 	}
   7935 }
   7936 
   7937 static int
   7938 wm_init_rx_buffer(struct wm_softc *sc, struct wm_rxqueue *rxq)
   7939 {
   7940 	struct wm_rxsoft *rxs;
   7941 	int error, i;
   7942 
   7943 	KASSERT(mutex_owned(rxq->rxq_lock));
   7944 
   7945 	for (i = 0; i < rxq->rxq_ndesc; i++) {
   7946 		rxs = &rxq->rxq_soft[i];
   7947 		if (rxs->rxs_mbuf == NULL) {
   7948 			if ((error = wm_add_rxbuf(rxq, i)) != 0) {
   7949 				log(LOG_ERR, "%s: unable to allocate or map "
   7950 				    "rx buffer %d, error = %d\n",
   7951 				    device_xname(sc->sc_dev), i, error);
   7952 				/*
   7953 				 * XXX Should attempt to run with fewer receive
   7954 				 * XXX buffers instead of just failing.
   7955 				 */
   7956 				wm_rxdrain(rxq);
   7957 				return ENOMEM;
   7958 			}
   7959 		} else {
   7960 			/*
   7961 			 * For 82575 and 82576, the RX descriptors must be
   7962 			 * initialized after the setting of RCTL.EN in
   7963 			 * wm_set_filter()
   7964 			 */
   7965 			if ((sc->sc_flags & WM_F_NEWQUEUE) == 0)
   7966 				wm_init_rxdesc(rxq, i);
   7967 		}
   7968 	}
   7969 	rxq->rxq_ptr = 0;
   7970 	rxq->rxq_discard = 0;
   7971 	WM_RXCHAIN_RESET(rxq);
   7972 
   7973 	return 0;
   7974 }
   7975 
   7976 static int
   7977 wm_init_rx_queue(struct wm_softc *sc, struct wm_queue *wmq,
   7978     struct wm_rxqueue *rxq)
   7979 {
   7980 
   7981 	KASSERT(mutex_owned(rxq->rxq_lock));
   7982 
   7983 	/*
   7984 	 * Set up some register offsets that are different between
   7985 	 * the i82542 and the i82543 and later chips.
   7986 	 */
   7987 	if (sc->sc_type < WM_T_82543)
   7988 		rxq->rxq_rdt_reg = WMREG_OLD_RDT0;
   7989 	else
   7990 		rxq->rxq_rdt_reg = WMREG_RDT(wmq->wmq_id);
   7991 
   7992 	wm_init_rx_regs(sc, wmq, rxq);
   7993 	return wm_init_rx_buffer(sc, rxq);
   7994 }
   7995 
   7996 /*
   7997  * wm_init_quques:
   7998  *	Initialize {tx,rx}descs and {tx,rx} buffers
   7999  */
   8000 static int
   8001 wm_init_txrx_queues(struct wm_softc *sc)
   8002 {
   8003 	int i, error = 0;
   8004 
   8005 	DPRINTF(sc, WM_DEBUG_INIT, ("%s: %s called\n",
   8006 		device_xname(sc->sc_dev), __func__));
   8007 
   8008 	for (i = 0; i < sc->sc_nqueues; i++) {
   8009 		struct wm_queue *wmq = &sc->sc_queue[i];
   8010 		struct wm_txqueue *txq = &wmq->wmq_txq;
   8011 		struct wm_rxqueue *rxq = &wmq->wmq_rxq;
   8012 
   8013 		/*
   8014 		 * TODO
   8015 		 * Currently, use constant variable instead of AIM.
   8016 		 * Furthermore, the interrupt interval of multiqueue which use
   8017 		 * polling mode is less than default value.
   8018 		 * More tuning and AIM are required.
   8019 		 */
   8020 		if (wm_is_using_multiqueue(sc))
   8021 			wmq->wmq_itr = 50;
   8022 		else
   8023 			wmq->wmq_itr = sc->sc_itr_init;
   8024 		wmq->wmq_set_itr = true;
   8025 
   8026 		mutex_enter(txq->txq_lock);
   8027 		wm_init_tx_queue(sc, wmq, txq);
   8028 		mutex_exit(txq->txq_lock);
   8029 
   8030 		mutex_enter(rxq->rxq_lock);
   8031 		error = wm_init_rx_queue(sc, wmq, rxq);
   8032 		mutex_exit(rxq->rxq_lock);
   8033 		if (error)
   8034 			break;
   8035 	}
   8036 
   8037 	return error;
   8038 }
   8039 
   8040 /*
   8041  * wm_tx_offload:
   8042  *
   8043  *	Set up TCP/IP checksumming parameters for the
   8044  *	specified packet.
   8045  */
   8046 static void
   8047 wm_tx_offload(struct wm_softc *sc, struct wm_txqueue *txq,
   8048     struct wm_txsoft *txs, uint32_t *cmdp, uint8_t *fieldsp)
   8049 {
   8050 	struct mbuf *m0 = txs->txs_mbuf;
   8051 	struct livengood_tcpip_ctxdesc *t;
   8052 	uint32_t ipcs, tucs, cmd, cmdlen, seg;
   8053 	uint32_t ipcse;
   8054 	struct ether_header *eh;
   8055 	int offset, iphl;
   8056 	uint8_t fields;
   8057 
   8058 	/*
   8059 	 * XXX It would be nice if the mbuf pkthdr had offset
   8060 	 * fields for the protocol headers.
   8061 	 */
   8062 
   8063 	eh = mtod(m0, struct ether_header *);
   8064 	switch (htons(eh->ether_type)) {
   8065 	case ETHERTYPE_IP:
   8066 	case ETHERTYPE_IPV6:
   8067 		offset = ETHER_HDR_LEN;
   8068 		break;
   8069 
   8070 	case ETHERTYPE_VLAN:
   8071 		offset = ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN;
   8072 		break;
   8073 
   8074 	default:
   8075 		/* Don't support this protocol or encapsulation. */
   8076 		txq->txq_last_hw_cmd = txq->txq_last_hw_fields = 0;
   8077 		txq->txq_last_hw_ipcs = 0;
   8078 		txq->txq_last_hw_tucs = 0;
   8079 		*fieldsp = 0;
   8080 		*cmdp = 0;
   8081 		return;
   8082 	}
   8083 
   8084 	if ((m0->m_pkthdr.csum_flags &
   8085 	    (M_CSUM_TSOv4 | M_CSUM_UDPv4 | M_CSUM_TCPv4 | M_CSUM_IPv4)) != 0) {
   8086 		iphl = M_CSUM_DATA_IPv4_IPHL(m0->m_pkthdr.csum_data);
   8087 	} else
   8088 		iphl = M_CSUM_DATA_IPv6_IPHL(m0->m_pkthdr.csum_data);
   8089 
   8090 	ipcse = offset + iphl - 1;
   8091 
   8092 	cmd = WTX_CMD_DEXT | WTX_DTYP_D;
   8093 	cmdlen = WTX_CMD_DEXT | WTX_DTYP_C | WTX_CMD_IDE;
   8094 	seg = 0;
   8095 	fields = 0;
   8096 
   8097 	if ((m0->m_pkthdr.csum_flags & (M_CSUM_TSOv4 | M_CSUM_TSOv6)) != 0) {
   8098 		int hlen = offset + iphl;
   8099 		bool v4 = (m0->m_pkthdr.csum_flags & M_CSUM_TSOv4) != 0;
   8100 
   8101 		if (__predict_false(m0->m_len <
   8102 				    (hlen + sizeof(struct tcphdr)))) {
   8103 			/*
   8104 			 * TCP/IP headers are not in the first mbuf; we need
   8105 			 * to do this the slow and painful way. Let's just
   8106 			 * hope this doesn't happen very often.
   8107 			 */
   8108 			struct tcphdr th;
   8109 
   8110 			WM_Q_EVCNT_INCR(txq, tsopain);
   8111 
   8112 			m_copydata(m0, hlen, sizeof(th), &th);
   8113 			if (v4) {
   8114 				struct ip ip;
   8115 
   8116 				m_copydata(m0, offset, sizeof(ip), &ip);
   8117 				ip.ip_len = 0;
   8118 				m_copyback(m0,
   8119 				    offset + offsetof(struct ip, ip_len),
   8120 				    sizeof(ip.ip_len), &ip.ip_len);
   8121 				th.th_sum = in_cksum_phdr(ip.ip_src.s_addr,
   8122 				    ip.ip_dst.s_addr, htons(IPPROTO_TCP));
   8123 			} else {
   8124 				struct ip6_hdr ip6;
   8125 
   8126 				m_copydata(m0, offset, sizeof(ip6), &ip6);
   8127 				ip6.ip6_plen = 0;
   8128 				m_copyback(m0,
   8129 				    offset + offsetof(struct ip6_hdr, ip6_plen),
   8130 				    sizeof(ip6.ip6_plen), &ip6.ip6_plen);
   8131 				th.th_sum = in6_cksum_phdr(&ip6.ip6_src,
   8132 				    &ip6.ip6_dst, 0, htonl(IPPROTO_TCP));
   8133 			}
   8134 			m_copyback(m0, hlen + offsetof(struct tcphdr, th_sum),
   8135 			    sizeof(th.th_sum), &th.th_sum);
   8136 
   8137 			hlen += th.th_off << 2;
   8138 		} else {
   8139 			/*
   8140 			 * TCP/IP headers are in the first mbuf; we can do
   8141 			 * this the easy way.
   8142 			 */
   8143 			struct tcphdr *th;
   8144 
   8145 			if (v4) {
   8146 				struct ip *ip =
   8147 				    (void *)(mtod(m0, char *) + offset);
   8148 				th = (void *)(mtod(m0, char *) + hlen);
   8149 
   8150 				ip->ip_len = 0;
   8151 				th->th_sum = in_cksum_phdr(ip->ip_src.s_addr,
   8152 				    ip->ip_dst.s_addr, htons(IPPROTO_TCP));
   8153 			} else {
   8154 				struct ip6_hdr *ip6 =
   8155 				    (void *)(mtod(m0, char *) + offset);
   8156 				th = (void *)(mtod(m0, char *) + hlen);
   8157 
   8158 				ip6->ip6_plen = 0;
   8159 				th->th_sum = in6_cksum_phdr(&ip6->ip6_src,
   8160 				    &ip6->ip6_dst, 0, htonl(IPPROTO_TCP));
   8161 			}
   8162 			hlen += th->th_off << 2;
   8163 		}
   8164 
   8165 		if (v4) {
   8166 			WM_Q_EVCNT_INCR(txq, tso);
   8167 			cmdlen |= WTX_TCPIP_CMD_IP;
   8168 		} else {
   8169 			WM_Q_EVCNT_INCR(txq, tso6);
   8170 			ipcse = 0;
   8171 		}
   8172 		cmd |= WTX_TCPIP_CMD_TSE;
   8173 		cmdlen |= WTX_TCPIP_CMD_TSE |
   8174 		    WTX_TCPIP_CMD_TCP | (m0->m_pkthdr.len - hlen);
   8175 		seg = WTX_TCPIP_SEG_HDRLEN(hlen) |
   8176 		    WTX_TCPIP_SEG_MSS(m0->m_pkthdr.segsz);
   8177 	}
   8178 
   8179 	/*
   8180 	 * NOTE: Even if we're not using the IP or TCP/UDP checksum
   8181 	 * offload feature, if we load the context descriptor, we
   8182 	 * MUST provide valid values for IPCSS and TUCSS fields.
   8183 	 */
   8184 
   8185 	ipcs = WTX_TCPIP_IPCSS(offset) |
   8186 	    WTX_TCPIP_IPCSO(offset + offsetof(struct ip, ip_sum)) |
   8187 	    WTX_TCPIP_IPCSE(ipcse);
   8188 	if (m0->m_pkthdr.csum_flags & (M_CSUM_IPv4 | M_CSUM_TSOv4)) {
   8189 		WM_Q_EVCNT_INCR(txq, ipsum);
   8190 		fields |= WTX_IXSM;
   8191 	}
   8192 
   8193 	offset += iphl;
   8194 
   8195 	if (m0->m_pkthdr.csum_flags &
   8196 	    (M_CSUM_TCPv4 | M_CSUM_UDPv4 | M_CSUM_TSOv4)) {
   8197 		WM_Q_EVCNT_INCR(txq, tusum);
   8198 		fields |= WTX_TXSM;
   8199 		tucs = WTX_TCPIP_TUCSS(offset) |
   8200 		    WTX_TCPIP_TUCSO(offset +
   8201 			M_CSUM_DATA_IPv4_OFFSET(m0->m_pkthdr.csum_data)) |
   8202 		    WTX_TCPIP_TUCSE(0) /* Rest of packet */;
   8203 	} else if ((m0->m_pkthdr.csum_flags &
   8204 	    (M_CSUM_TCPv6 | M_CSUM_UDPv6 | M_CSUM_TSOv6)) != 0) {
   8205 		WM_Q_EVCNT_INCR(txq, tusum6);
   8206 		fields |= WTX_TXSM;
   8207 		tucs = WTX_TCPIP_TUCSS(offset) |
   8208 		    WTX_TCPIP_TUCSO(offset +
   8209 			M_CSUM_DATA_IPv6_OFFSET(m0->m_pkthdr.csum_data)) |
   8210 		    WTX_TCPIP_TUCSE(0) /* Rest of packet */;
   8211 	} else {
   8212 		/* Just initialize it to a valid TCP context. */
   8213 		tucs = WTX_TCPIP_TUCSS(offset) |
   8214 		    WTX_TCPIP_TUCSO(offset + offsetof(struct tcphdr, th_sum)) |
   8215 		    WTX_TCPIP_TUCSE(0) /* Rest of packet */;
   8216 	}
   8217 
   8218 	*cmdp = cmd;
   8219 	*fieldsp = fields;
   8220 
   8221 	/*
   8222 	 * We don't have to write context descriptor for every packet
   8223 	 * except for 82574. For 82574, we must write context descriptor
   8224 	 * for every packet when we use two descriptor queues.
   8225 	 *
   8226 	 * The 82574L can only remember the *last* context used
   8227 	 * regardless of queue that it was use for.  We cannot reuse
   8228 	 * contexts on this hardware platform and must generate a new
   8229 	 * context every time.  82574L hardware spec, section 7.2.6,
   8230 	 * second note.
   8231 	 */
   8232 	if (sc->sc_nqueues < 2) {
   8233 		/*
   8234 		 * Setting up new checksum offload context for every
   8235 		 * frames takes a lot of processing time for hardware.
   8236 		 * This also reduces performance a lot for small sized
   8237 		 * frames so avoid it if driver can use previously
   8238 		 * configured checksum offload context.
   8239 		 * For TSO, in theory we can use the same TSO context only if
   8240 		 * frame is the same type(IP/TCP) and the same MSS. However
   8241 		 * checking whether a frame has the same IP/TCP structure is a
   8242 		 * hard thing so just ignore that and always restablish a
   8243 		 * new TSO context.
   8244 		 */
   8245 		if ((m0->m_pkthdr.csum_flags & (M_CSUM_TSOv4 | M_CSUM_TSOv6))
   8246 		    == 0) {
   8247 			if (txq->txq_last_hw_cmd == cmd &&
   8248 			    txq->txq_last_hw_fields == fields &&
   8249 			    txq->txq_last_hw_ipcs == (ipcs & 0xffff) &&
   8250 			    txq->txq_last_hw_tucs == (tucs & 0xffff)) {
   8251 				WM_Q_EVCNT_INCR(txq, skipcontext);
   8252 				return;
   8253 			}
   8254 		}
   8255 
   8256 		txq->txq_last_hw_cmd = cmd;
   8257 		txq->txq_last_hw_fields = fields;
   8258 		txq->txq_last_hw_ipcs = (ipcs & 0xffff);
   8259 		txq->txq_last_hw_tucs = (tucs & 0xffff);
   8260 	}
   8261 
   8262 	/* Fill in the context descriptor. */
   8263 	t = (struct livengood_tcpip_ctxdesc *)
   8264 	    &txq->txq_descs[txq->txq_next];
   8265 	t->tcpip_ipcs = htole32(ipcs);
   8266 	t->tcpip_tucs = htole32(tucs);
   8267 	t->tcpip_cmdlen = htole32(cmdlen);
   8268 	t->tcpip_seg = htole32(seg);
   8269 	wm_cdtxsync(txq, txq->txq_next, 1, BUS_DMASYNC_PREWRITE);
   8270 
   8271 	txq->txq_next = WM_NEXTTX(txq, txq->txq_next);
   8272 	txs->txs_ndesc++;
   8273 }
   8274 
   8275 static inline int
   8276 wm_select_txqueue(struct ifnet *ifp, struct mbuf *m)
   8277 {
   8278 	struct wm_softc *sc = ifp->if_softc;
   8279 	u_int cpuid = cpu_index(curcpu());
   8280 
   8281 	/*
   8282 	 * Currently, simple distribute strategy.
   8283 	 * TODO:
   8284 	 * distribute by flowid(RSS has value).
   8285 	 */
   8286 	return ((cpuid + ncpu - sc->sc_affinity_offset) % ncpu) % sc->sc_nqueues;
   8287 }
   8288 
   8289 static inline bool
   8290 wm_linkdown_discard(struct wm_txqueue *txq)
   8291 {
   8292 
   8293 	if ((txq->txq_flags & WM_TXQ_LINKDOWN_DISCARD) != 0)
   8294 		return true;
   8295 
   8296 	return false;
   8297 }
   8298 
   8299 /*
   8300  * wm_start:		[ifnet interface function]
   8301  *
   8302  *	Start packet transmission on the interface.
   8303  */
   8304 static void
   8305 wm_start(struct ifnet *ifp)
   8306 {
   8307 	struct wm_softc *sc = ifp->if_softc;
   8308 	struct wm_txqueue *txq = &sc->sc_queue[0].wmq_txq;
   8309 
   8310 #ifdef WM_MPSAFE
   8311 	KASSERT(if_is_mpsafe(ifp));
   8312 #endif
   8313 	/*
   8314 	 * if_obytes and if_omcasts are added in if_transmit()@if.c.
   8315 	 */
   8316 
   8317 	mutex_enter(txq->txq_lock);
   8318 	if (!txq->txq_stopping)
   8319 		wm_start_locked(ifp);
   8320 	mutex_exit(txq->txq_lock);
   8321 }
   8322 
   8323 static void
   8324 wm_start_locked(struct ifnet *ifp)
   8325 {
   8326 	struct wm_softc *sc = ifp->if_softc;
   8327 	struct wm_txqueue *txq = &sc->sc_queue[0].wmq_txq;
   8328 
   8329 	wm_send_common_locked(ifp, txq, false);
   8330 }
   8331 
   8332 static int
   8333 wm_transmit(struct ifnet *ifp, struct mbuf *m)
   8334 {
   8335 	int qid;
   8336 	struct wm_softc *sc = ifp->if_softc;
   8337 	struct wm_txqueue *txq;
   8338 
   8339 	qid = wm_select_txqueue(ifp, m);
   8340 	txq = &sc->sc_queue[qid].wmq_txq;
   8341 
   8342 	if (__predict_false(!pcq_put(txq->txq_interq, m))) {
   8343 		m_freem(m);
   8344 		WM_Q_EVCNT_INCR(txq, pcqdrop);
   8345 		return ENOBUFS;
   8346 	}
   8347 
   8348 	net_stat_ref_t nsr = IF_STAT_GETREF(ifp);
   8349 	if_statadd_ref(nsr, if_obytes, m->m_pkthdr.len);
   8350 	if (m->m_flags & M_MCAST)
   8351 		if_statinc_ref(nsr, if_omcasts);
   8352 	IF_STAT_PUTREF(ifp);
   8353 
   8354 	if (mutex_tryenter(txq->txq_lock)) {
   8355 		if (!txq->txq_stopping)
   8356 			wm_transmit_locked(ifp, txq);
   8357 		mutex_exit(txq->txq_lock);
   8358 	}
   8359 
   8360 	return 0;
   8361 }
   8362 
   8363 static void
   8364 wm_transmit_locked(struct ifnet *ifp, struct wm_txqueue *txq)
   8365 {
   8366 
   8367 	wm_send_common_locked(ifp, txq, true);
   8368 }
   8369 
   8370 static void
   8371 wm_send_common_locked(struct ifnet *ifp, struct wm_txqueue *txq,
   8372     bool is_transmit)
   8373 {
   8374 	struct wm_softc *sc = ifp->if_softc;
   8375 	struct mbuf *m0;
   8376 	struct wm_txsoft *txs;
   8377 	bus_dmamap_t dmamap;
   8378 	int error, nexttx, lasttx = -1, ofree, seg, segs_needed, use_tso;
   8379 	bus_addr_t curaddr;
   8380 	bus_size_t seglen, curlen;
   8381 	uint32_t cksumcmd;
   8382 	uint8_t cksumfields;
   8383 	bool remap = true;
   8384 
   8385 	KASSERT(mutex_owned(txq->txq_lock));
   8386 
   8387 	if ((ifp->if_flags & IFF_RUNNING) == 0)
   8388 		return;
   8389 	if ((txq->txq_flags & WM_TXQ_NO_SPACE) != 0)
   8390 		return;
   8391 
   8392 	if (__predict_false(wm_linkdown_discard(txq))) {
   8393 		do {
   8394 			if (is_transmit)
   8395 				m0 = pcq_get(txq->txq_interq);
   8396 			else
   8397 				IFQ_DEQUEUE(&ifp->if_snd, m0);
   8398 			/*
   8399 			 * increment successed packet counter as in the case
   8400 			 * which the packet is discarded by link down PHY.
   8401 			 */
   8402 			if (m0 != NULL) {
   8403 				if_statinc(ifp, if_opackets);
   8404 				m_freem(m0);
   8405 			}
   8406 		} while (m0 != NULL);
   8407 		return;
   8408 	}
   8409 
   8410 	/* Remember the previous number of free descriptors. */
   8411 	ofree = txq->txq_free;
   8412 
   8413 	/*
   8414 	 * Loop through the send queue, setting up transmit descriptors
   8415 	 * until we drain the queue, or use up all available transmit
   8416 	 * descriptors.
   8417 	 */
   8418 	for (;;) {
   8419 		m0 = NULL;
   8420 
   8421 		/* Get a work queue entry. */
   8422 		if (txq->txq_sfree < WM_TXQUEUE_GC(txq)) {
   8423 			wm_txeof(txq, UINT_MAX);
   8424 			if (txq->txq_sfree == 0) {
   8425 				DPRINTF(sc, WM_DEBUG_TX,
   8426 				    ("%s: TX: no free job descriptors\n",
   8427 					device_xname(sc->sc_dev)));
   8428 				WM_Q_EVCNT_INCR(txq, txsstall);
   8429 				break;
   8430 			}
   8431 		}
   8432 
   8433 		/* Grab a packet off the queue. */
   8434 		if (is_transmit)
   8435 			m0 = pcq_get(txq->txq_interq);
   8436 		else
   8437 			IFQ_DEQUEUE(&ifp->if_snd, m0);
   8438 		if (m0 == NULL)
   8439 			break;
   8440 
   8441 		DPRINTF(sc, WM_DEBUG_TX,
   8442 		    ("%s: TX: have packet to transmit: %p\n",
   8443 			device_xname(sc->sc_dev), m0));
   8444 
   8445 		txs = &txq->txq_soft[txq->txq_snext];
   8446 		dmamap = txs->txs_dmamap;
   8447 
   8448 		use_tso = (m0->m_pkthdr.csum_flags &
   8449 		    (M_CSUM_TSOv4 | M_CSUM_TSOv6)) != 0;
   8450 
   8451 		/*
   8452 		 * So says the Linux driver:
   8453 		 * The controller does a simple calculation to make sure
   8454 		 * there is enough room in the FIFO before initiating the
   8455 		 * DMA for each buffer. The calc is:
   8456 		 *	4 = ceil(buffer len / MSS)
   8457 		 * To make sure we don't overrun the FIFO, adjust the max
   8458 		 * buffer len if the MSS drops.
   8459 		 */
   8460 		dmamap->dm_maxsegsz =
   8461 		    (use_tso && (m0->m_pkthdr.segsz << 2) < WTX_MAX_LEN)
   8462 		    ? m0->m_pkthdr.segsz << 2
   8463 		    : WTX_MAX_LEN;
   8464 
   8465 		/*
   8466 		 * Load the DMA map.  If this fails, the packet either
   8467 		 * didn't fit in the allotted number of segments, or we
   8468 		 * were short on resources.  For the too-many-segments
   8469 		 * case, we simply report an error and drop the packet,
   8470 		 * since we can't sanely copy a jumbo packet to a single
   8471 		 * buffer.
   8472 		 */
   8473 retry:
   8474 		error = bus_dmamap_load_mbuf(sc->sc_dmat, dmamap, m0,
   8475 		    BUS_DMA_WRITE | BUS_DMA_NOWAIT);
   8476 		if (__predict_false(error)) {
   8477 			if (error == EFBIG) {
   8478 				if (remap == true) {
   8479 					struct mbuf *m;
   8480 
   8481 					remap = false;
   8482 					m = m_defrag(m0, M_NOWAIT);
   8483 					if (m != NULL) {
   8484 						WM_Q_EVCNT_INCR(txq, defrag);
   8485 						m0 = m;
   8486 						goto retry;
   8487 					}
   8488 				}
   8489 				WM_Q_EVCNT_INCR(txq, toomanyseg);
   8490 				log(LOG_ERR, "%s: Tx packet consumes too many "
   8491 				    "DMA segments, dropping...\n",
   8492 				    device_xname(sc->sc_dev));
   8493 				wm_dump_mbuf_chain(sc, m0);
   8494 				m_freem(m0);
   8495 				continue;
   8496 			}
   8497 			/* Short on resources, just stop for now. */
   8498 			DPRINTF(sc, WM_DEBUG_TX,
   8499 			    ("%s: TX: dmamap load failed: %d\n",
   8500 				device_xname(sc->sc_dev), error));
   8501 			break;
   8502 		}
   8503 
   8504 		segs_needed = dmamap->dm_nsegs;
   8505 		if (use_tso) {
   8506 			/* For sentinel descriptor; see below. */
   8507 			segs_needed++;
   8508 		}
   8509 
   8510 		/*
   8511 		 * Ensure we have enough descriptors free to describe
   8512 		 * the packet. Note, we always reserve one descriptor
   8513 		 * at the end of the ring due to the semantics of the
   8514 		 * TDT register, plus one more in the event we need
   8515 		 * to load offload context.
   8516 		 */
   8517 		if (segs_needed > txq->txq_free - 2) {
   8518 			/*
   8519 			 * Not enough free descriptors to transmit this
   8520 			 * packet.  We haven't committed anything yet,
   8521 			 * so just unload the DMA map, put the packet
   8522 			 * pack on the queue, and punt. Notify the upper
   8523 			 * layer that there are no more slots left.
   8524 			 */
   8525 			DPRINTF(sc, WM_DEBUG_TX,
   8526 			    ("%s: TX: need %d (%d) descriptors, have %d\n",
   8527 				device_xname(sc->sc_dev), dmamap->dm_nsegs,
   8528 				segs_needed, txq->txq_free - 1));
   8529 			txq->txq_flags |= WM_TXQ_NO_SPACE;
   8530 			bus_dmamap_unload(sc->sc_dmat, dmamap);
   8531 			WM_Q_EVCNT_INCR(txq, txdstall);
   8532 			break;
   8533 		}
   8534 
   8535 		/*
   8536 		 * Check for 82547 Tx FIFO bug. We need to do this
   8537 		 * once we know we can transmit the packet, since we
   8538 		 * do some internal FIFO space accounting here.
   8539 		 */
   8540 		if (sc->sc_type == WM_T_82547 &&
   8541 		    wm_82547_txfifo_bugchk(sc, m0)) {
   8542 			DPRINTF(sc, WM_DEBUG_TX,
   8543 			    ("%s: TX: 82547 Tx FIFO bug detected\n",
   8544 				device_xname(sc->sc_dev)));
   8545 			txq->txq_flags |= WM_TXQ_NO_SPACE;
   8546 			bus_dmamap_unload(sc->sc_dmat, dmamap);
   8547 			WM_Q_EVCNT_INCR(txq, fifo_stall);
   8548 			break;
   8549 		}
   8550 
   8551 		/* WE ARE NOW COMMITTED TO TRANSMITTING THE PACKET. */
   8552 
   8553 		DPRINTF(sc, WM_DEBUG_TX,
   8554 		    ("%s: TX: packet has %d (%d) DMA segments\n",
   8555 		    device_xname(sc->sc_dev), dmamap->dm_nsegs, segs_needed));
   8556 
   8557 		WM_EVCNT_INCR(&txq->txq_ev_txseg[dmamap->dm_nsegs - 1]);
   8558 
   8559 		/*
   8560 		 * Store a pointer to the packet so that we can free it
   8561 		 * later.
   8562 		 *
   8563 		 * Initially, we consider the number of descriptors the
   8564 		 * packet uses the number of DMA segments.  This may be
   8565 		 * incremented by 1 if we do checksum offload (a descriptor
   8566 		 * is used to set the checksum context).
   8567 		 */
   8568 		txs->txs_mbuf = m0;
   8569 		txs->txs_firstdesc = txq->txq_next;
   8570 		txs->txs_ndesc = segs_needed;
   8571 
   8572 		/* Set up offload parameters for this packet. */
   8573 		if (m0->m_pkthdr.csum_flags &
   8574 		    (M_CSUM_TSOv4 | M_CSUM_TSOv6 |
   8575 		    M_CSUM_IPv4 | M_CSUM_TCPv4 | M_CSUM_UDPv4 |
   8576 		    M_CSUM_TCPv6 | M_CSUM_UDPv6)) {
   8577 			wm_tx_offload(sc, txq, txs, &cksumcmd, &cksumfields);
   8578 		} else {
   8579 			txq->txq_last_hw_cmd = txq->txq_last_hw_fields = 0;
   8580 			txq->txq_last_hw_ipcs = txq->txq_last_hw_tucs = 0;
   8581 			cksumcmd = 0;
   8582 			cksumfields = 0;
   8583 		}
   8584 
   8585 		cksumcmd |= WTX_CMD_IDE | WTX_CMD_IFCS;
   8586 
   8587 		/* Sync the DMA map. */
   8588 		bus_dmamap_sync(sc->sc_dmat, dmamap, 0, dmamap->dm_mapsize,
   8589 		    BUS_DMASYNC_PREWRITE);
   8590 
   8591 		/* Initialize the transmit descriptor. */
   8592 		for (nexttx = txq->txq_next, seg = 0;
   8593 		     seg < dmamap->dm_nsegs; seg++) {
   8594 			for (seglen = dmamap->dm_segs[seg].ds_len,
   8595 			     curaddr = dmamap->dm_segs[seg].ds_addr;
   8596 			     seglen != 0;
   8597 			     curaddr += curlen, seglen -= curlen,
   8598 			     nexttx = WM_NEXTTX(txq, nexttx)) {
   8599 				curlen = seglen;
   8600 
   8601 				/*
   8602 				 * So says the Linux driver:
   8603 				 * Work around for premature descriptor
   8604 				 * write-backs in TSO mode.  Append a
   8605 				 * 4-byte sentinel descriptor.
   8606 				 */
   8607 				if (use_tso && seg == dmamap->dm_nsegs - 1 &&
   8608 				    curlen > 8)
   8609 					curlen -= 4;
   8610 
   8611 				wm_set_dma_addr(
   8612 				    &txq->txq_descs[nexttx].wtx_addr, curaddr);
   8613 				txq->txq_descs[nexttx].wtx_cmdlen
   8614 				    = htole32(cksumcmd | curlen);
   8615 				txq->txq_descs[nexttx].wtx_fields.wtxu_status
   8616 				    = 0;
   8617 				txq->txq_descs[nexttx].wtx_fields.wtxu_options
   8618 				    = cksumfields;
   8619 				txq->txq_descs[nexttx].wtx_fields.wtxu_vlan =0;
   8620 				lasttx = nexttx;
   8621 
   8622 				DPRINTF(sc, WM_DEBUG_TX,
   8623 				    ("%s: TX: desc %d: low %#" PRIx64 ", "
   8624 					"len %#04zx\n",
   8625 					device_xname(sc->sc_dev), nexttx,
   8626 					(uint64_t)curaddr, curlen));
   8627 			}
   8628 		}
   8629 
   8630 		KASSERT(lasttx != -1);
   8631 
   8632 		/*
   8633 		 * Set up the command byte on the last descriptor of
   8634 		 * the packet. If we're in the interrupt delay window,
   8635 		 * delay the interrupt.
   8636 		 */
   8637 		txq->txq_descs[lasttx].wtx_cmdlen |=
   8638 		    htole32(WTX_CMD_EOP | WTX_CMD_RS);
   8639 
   8640 		/*
   8641 		 * If VLANs are enabled and the packet has a VLAN tag, set
   8642 		 * up the descriptor to encapsulate the packet for us.
   8643 		 *
   8644 		 * This is only valid on the last descriptor of the packet.
   8645 		 */
   8646 		if (vlan_has_tag(m0)) {
   8647 			txq->txq_descs[lasttx].wtx_cmdlen |=
   8648 			    htole32(WTX_CMD_VLE);
   8649 			txq->txq_descs[lasttx].wtx_fields.wtxu_vlan
   8650 			    = htole16(vlan_get_tag(m0));
   8651 		}
   8652 
   8653 		txs->txs_lastdesc = lasttx;
   8654 
   8655 		DPRINTF(sc, WM_DEBUG_TX,
   8656 		    ("%s: TX: desc %d: cmdlen 0x%08x\n",
   8657 			device_xname(sc->sc_dev),
   8658 			lasttx, le32toh(txq->txq_descs[lasttx].wtx_cmdlen)));
   8659 
   8660 		/* Sync the descriptors we're using. */
   8661 		wm_cdtxsync(txq, txq->txq_next, txs->txs_ndesc,
   8662 		    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
   8663 
   8664 		/* Give the packet to the chip. */
   8665 		CSR_WRITE(sc, txq->txq_tdt_reg, nexttx);
   8666 
   8667 		DPRINTF(sc, WM_DEBUG_TX,
   8668 		    ("%s: TX: TDT -> %d\n", device_xname(sc->sc_dev), nexttx));
   8669 
   8670 		DPRINTF(sc, WM_DEBUG_TX,
   8671 		    ("%s: TX: finished transmitting packet, job %d\n",
   8672 			device_xname(sc->sc_dev), txq->txq_snext));
   8673 
   8674 		/* Advance the tx pointer. */
   8675 		txq->txq_free -= txs->txs_ndesc;
   8676 		txq->txq_next = nexttx;
   8677 
   8678 		txq->txq_sfree--;
   8679 		txq->txq_snext = WM_NEXTTXS(txq, txq->txq_snext);
   8680 
   8681 		/* Pass the packet to any BPF listeners. */
   8682 		bpf_mtap(ifp, m0, BPF_D_OUT);
   8683 	}
   8684 
   8685 	if (m0 != NULL) {
   8686 		txq->txq_flags |= WM_TXQ_NO_SPACE;
   8687 		WM_Q_EVCNT_INCR(txq, descdrop);
   8688 		DPRINTF(sc, WM_DEBUG_TX, ("%s: TX: error after IFQ_DEQUEUE\n",
   8689 			__func__));
   8690 		m_freem(m0);
   8691 	}
   8692 
   8693 	if (txq->txq_sfree == 0 || txq->txq_free <= 2) {
   8694 		/* No more slots; notify upper layer. */
   8695 		txq->txq_flags |= WM_TXQ_NO_SPACE;
   8696 	}
   8697 
   8698 	if (txq->txq_free != ofree) {
   8699 		/* Set a watchdog timer in case the chip flakes out. */
   8700 		txq->txq_lastsent = time_uptime;
   8701 		txq->txq_sending = true;
   8702 	}
   8703 }
   8704 
   8705 /*
   8706  * wm_nq_tx_offload:
   8707  *
   8708  *	Set up TCP/IP checksumming parameters for the
   8709  *	specified packet, for NEWQUEUE devices
   8710  */
   8711 static void
   8712 wm_nq_tx_offload(struct wm_softc *sc, struct wm_txqueue *txq,
   8713     struct wm_txsoft *txs, uint32_t *cmdlenp, uint32_t *fieldsp, bool *do_csum)
   8714 {
   8715 	struct mbuf *m0 = txs->txs_mbuf;
   8716 	uint32_t vl_len, mssidx, cmdc;
   8717 	struct ether_header *eh;
   8718 	int offset, iphl;
   8719 
   8720 	/*
   8721 	 * XXX It would be nice if the mbuf pkthdr had offset
   8722 	 * fields for the protocol headers.
   8723 	 */
   8724 	*cmdlenp = 0;
   8725 	*fieldsp = 0;
   8726 
   8727 	eh = mtod(m0, struct ether_header *);
   8728 	switch (htons(eh->ether_type)) {
   8729 	case ETHERTYPE_IP:
   8730 	case ETHERTYPE_IPV6:
   8731 		offset = ETHER_HDR_LEN;
   8732 		break;
   8733 
   8734 	case ETHERTYPE_VLAN:
   8735 		offset = ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN;
   8736 		break;
   8737 
   8738 	default:
   8739 		/* Don't support this protocol or encapsulation. */
   8740 		*do_csum = false;
   8741 		return;
   8742 	}
   8743 	*do_csum = true;
   8744 	*cmdlenp = NQTX_DTYP_D | NQTX_CMD_DEXT | NQTX_CMD_IFCS;
   8745 	cmdc = NQTX_DTYP_C | NQTX_CMD_DEXT;
   8746 
   8747 	vl_len = (offset << NQTXC_VLLEN_MACLEN_SHIFT);
   8748 	KASSERT((offset & ~NQTXC_VLLEN_MACLEN_MASK) == 0);
   8749 
   8750 	if ((m0->m_pkthdr.csum_flags &
   8751 	    (M_CSUM_TSOv4 | M_CSUM_UDPv4 | M_CSUM_TCPv4 | M_CSUM_IPv4)) != 0) {
   8752 		iphl = M_CSUM_DATA_IPv4_IPHL(m0->m_pkthdr.csum_data);
   8753 	} else {
   8754 		iphl = M_CSUM_DATA_IPv6_IPHL(m0->m_pkthdr.csum_data);
   8755 	}
   8756 	vl_len |= (iphl << NQTXC_VLLEN_IPLEN_SHIFT);
   8757 	KASSERT((iphl & ~NQTXC_VLLEN_IPLEN_MASK) == 0);
   8758 
   8759 	if (vlan_has_tag(m0)) {
   8760 		vl_len |= ((vlan_get_tag(m0) & NQTXC_VLLEN_VLAN_MASK)
   8761 		    << NQTXC_VLLEN_VLAN_SHIFT);
   8762 		*cmdlenp |= NQTX_CMD_VLE;
   8763 	}
   8764 
   8765 	mssidx = 0;
   8766 
   8767 	if ((m0->m_pkthdr.csum_flags & (M_CSUM_TSOv4 | M_CSUM_TSOv6)) != 0) {
   8768 		int hlen = offset + iphl;
   8769 		int tcp_hlen;
   8770 		bool v4 = (m0->m_pkthdr.csum_flags & M_CSUM_TSOv4) != 0;
   8771 
   8772 		if (__predict_false(m0->m_len <
   8773 				    (hlen + sizeof(struct tcphdr)))) {
   8774 			/*
   8775 			 * TCP/IP headers are not in the first mbuf; we need
   8776 			 * to do this the slow and painful way. Let's just
   8777 			 * hope this doesn't happen very often.
   8778 			 */
   8779 			struct tcphdr th;
   8780 
   8781 			WM_Q_EVCNT_INCR(txq, tsopain);
   8782 
   8783 			m_copydata(m0, hlen, sizeof(th), &th);
   8784 			if (v4) {
   8785 				struct ip ip;
   8786 
   8787 				m_copydata(m0, offset, sizeof(ip), &ip);
   8788 				ip.ip_len = 0;
   8789 				m_copyback(m0,
   8790 				    offset + offsetof(struct ip, ip_len),
   8791 				    sizeof(ip.ip_len), &ip.ip_len);
   8792 				th.th_sum = in_cksum_phdr(ip.ip_src.s_addr,
   8793 				    ip.ip_dst.s_addr, htons(IPPROTO_TCP));
   8794 			} else {
   8795 				struct ip6_hdr ip6;
   8796 
   8797 				m_copydata(m0, offset, sizeof(ip6), &ip6);
   8798 				ip6.ip6_plen = 0;
   8799 				m_copyback(m0,
   8800 				    offset + offsetof(struct ip6_hdr, ip6_plen),
   8801 				    sizeof(ip6.ip6_plen), &ip6.ip6_plen);
   8802 				th.th_sum = in6_cksum_phdr(&ip6.ip6_src,
   8803 				    &ip6.ip6_dst, 0, htonl(IPPROTO_TCP));
   8804 			}
   8805 			m_copyback(m0, hlen + offsetof(struct tcphdr, th_sum),
   8806 			    sizeof(th.th_sum), &th.th_sum);
   8807 
   8808 			tcp_hlen = th.th_off << 2;
   8809 		} else {
   8810 			/*
   8811 			 * TCP/IP headers are in the first mbuf; we can do
   8812 			 * this the easy way.
   8813 			 */
   8814 			struct tcphdr *th;
   8815 
   8816 			if (v4) {
   8817 				struct ip *ip =
   8818 				    (void *)(mtod(m0, char *) + offset);
   8819 				th = (void *)(mtod(m0, char *) + hlen);
   8820 
   8821 				ip->ip_len = 0;
   8822 				th->th_sum = in_cksum_phdr(ip->ip_src.s_addr,
   8823 				    ip->ip_dst.s_addr, htons(IPPROTO_TCP));
   8824 			} else {
   8825 				struct ip6_hdr *ip6 =
   8826 				    (void *)(mtod(m0, char *) + offset);
   8827 				th = (void *)(mtod(m0, char *) + hlen);
   8828 
   8829 				ip6->ip6_plen = 0;
   8830 				th->th_sum = in6_cksum_phdr(&ip6->ip6_src,
   8831 				    &ip6->ip6_dst, 0, htonl(IPPROTO_TCP));
   8832 			}
   8833 			tcp_hlen = th->th_off << 2;
   8834 		}
   8835 		hlen += tcp_hlen;
   8836 		*cmdlenp |= NQTX_CMD_TSE;
   8837 
   8838 		if (v4) {
   8839 			WM_Q_EVCNT_INCR(txq, tso);
   8840 			*fieldsp |= NQTXD_FIELDS_IXSM | NQTXD_FIELDS_TUXSM;
   8841 		} else {
   8842 			WM_Q_EVCNT_INCR(txq, tso6);
   8843 			*fieldsp |= NQTXD_FIELDS_TUXSM;
   8844 		}
   8845 		*fieldsp |= ((m0->m_pkthdr.len - hlen) << NQTXD_FIELDS_PAYLEN_SHIFT);
   8846 		KASSERT(((m0->m_pkthdr.len - hlen) & ~NQTXD_FIELDS_PAYLEN_MASK) == 0);
   8847 		mssidx |= (m0->m_pkthdr.segsz << NQTXC_MSSIDX_MSS_SHIFT);
   8848 		KASSERT((m0->m_pkthdr.segsz & ~NQTXC_MSSIDX_MSS_MASK) == 0);
   8849 		mssidx |= (tcp_hlen << NQTXC_MSSIDX_L4LEN_SHIFT);
   8850 		KASSERT((tcp_hlen & ~NQTXC_MSSIDX_L4LEN_MASK) == 0);
   8851 	} else {
   8852 		*fieldsp |= (m0->m_pkthdr.len << NQTXD_FIELDS_PAYLEN_SHIFT);
   8853 		KASSERT((m0->m_pkthdr.len & ~NQTXD_FIELDS_PAYLEN_MASK) == 0);
   8854 	}
   8855 
   8856 	if (m0->m_pkthdr.csum_flags & M_CSUM_IPv4) {
   8857 		*fieldsp |= NQTXD_FIELDS_IXSM;
   8858 		cmdc |= NQTXC_CMD_IP4;
   8859 	}
   8860 
   8861 	if (m0->m_pkthdr.csum_flags &
   8862 	    (M_CSUM_UDPv4 | M_CSUM_TCPv4 | M_CSUM_TSOv4)) {
   8863 		WM_Q_EVCNT_INCR(txq, tusum);
   8864 		if (m0->m_pkthdr.csum_flags & (M_CSUM_TCPv4 | M_CSUM_TSOv4))
   8865 			cmdc |= NQTXC_CMD_TCP;
   8866 		else
   8867 			cmdc |= NQTXC_CMD_UDP;
   8868 
   8869 		cmdc |= NQTXC_CMD_IP4;
   8870 		*fieldsp |= NQTXD_FIELDS_TUXSM;
   8871 	}
   8872 	if (m0->m_pkthdr.csum_flags &
   8873 	    (M_CSUM_UDPv6 | M_CSUM_TCPv6 | M_CSUM_TSOv6)) {
   8874 		WM_Q_EVCNT_INCR(txq, tusum6);
   8875 		if (m0->m_pkthdr.csum_flags & (M_CSUM_TCPv6 | M_CSUM_TSOv6))
   8876 			cmdc |= NQTXC_CMD_TCP;
   8877 		else
   8878 			cmdc |= NQTXC_CMD_UDP;
   8879 
   8880 		cmdc |= NQTXC_CMD_IP6;
   8881 		*fieldsp |= NQTXD_FIELDS_TUXSM;
   8882 	}
   8883 
   8884 	/*
   8885 	 * We don't have to write context descriptor for every packet to
   8886 	 * NEWQUEUE controllers, that is 82575, 82576, 82580, I350, I354,
   8887 	 * I210 and I211. It is enough to write once per a Tx queue for these
   8888 	 * controllers.
   8889 	 * It would be overhead to write context descriptor for every packet,
   8890 	 * however it does not cause problems.
   8891 	 */
   8892 	/* Fill in the context descriptor. */
   8893 	txq->txq_nq_descs[txq->txq_next].nqrx_ctx.nqtxc_vl_len =
   8894 	    htole32(vl_len);
   8895 	txq->txq_nq_descs[txq->txq_next].nqrx_ctx.nqtxc_sn = 0;
   8896 	txq->txq_nq_descs[txq->txq_next].nqrx_ctx.nqtxc_cmd =
   8897 	    htole32(cmdc);
   8898 	txq->txq_nq_descs[txq->txq_next].nqrx_ctx.nqtxc_mssidx =
   8899 	    htole32(mssidx);
   8900 	wm_cdtxsync(txq, txq->txq_next, 1, BUS_DMASYNC_PREWRITE);
   8901 	DPRINTF(sc, WM_DEBUG_TX,
   8902 	    ("%s: TX: context desc %d 0x%08x%08x\n", device_xname(sc->sc_dev),
   8903 		txq->txq_next, 0, vl_len));
   8904 	DPRINTF(sc, WM_DEBUG_TX, ("\t0x%08x%08x\n", mssidx, cmdc));
   8905 	txq->txq_next = WM_NEXTTX(txq, txq->txq_next);
   8906 	txs->txs_ndesc++;
   8907 }
   8908 
   8909 /*
   8910  * wm_nq_start:		[ifnet interface function]
   8911  *
   8912  *	Start packet transmission on the interface for NEWQUEUE devices
   8913  */
   8914 static void
   8915 wm_nq_start(struct ifnet *ifp)
   8916 {
   8917 	struct wm_softc *sc = ifp->if_softc;
   8918 	struct wm_txqueue *txq = &sc->sc_queue[0].wmq_txq;
   8919 
   8920 #ifdef WM_MPSAFE
   8921 	KASSERT(if_is_mpsafe(ifp));
   8922 #endif
   8923 	/*
   8924 	 * if_obytes and if_omcasts are added in if_transmit()@if.c.
   8925 	 */
   8926 
   8927 	mutex_enter(txq->txq_lock);
   8928 	if (!txq->txq_stopping)
   8929 		wm_nq_start_locked(ifp);
   8930 	mutex_exit(txq->txq_lock);
   8931 }
   8932 
   8933 static void
   8934 wm_nq_start_locked(struct ifnet *ifp)
   8935 {
   8936 	struct wm_softc *sc = ifp->if_softc;
   8937 	struct wm_txqueue *txq = &sc->sc_queue[0].wmq_txq;
   8938 
   8939 	wm_nq_send_common_locked(ifp, txq, false);
   8940 }
   8941 
   8942 static int
   8943 wm_nq_transmit(struct ifnet *ifp, struct mbuf *m)
   8944 {
   8945 	int qid;
   8946 	struct wm_softc *sc = ifp->if_softc;
   8947 	struct wm_txqueue *txq;
   8948 
   8949 	qid = wm_select_txqueue(ifp, m);
   8950 	txq = &sc->sc_queue[qid].wmq_txq;
   8951 
   8952 	if (__predict_false(!pcq_put(txq->txq_interq, m))) {
   8953 		m_freem(m);
   8954 		WM_Q_EVCNT_INCR(txq, pcqdrop);
   8955 		return ENOBUFS;
   8956 	}
   8957 
   8958 	net_stat_ref_t nsr = IF_STAT_GETREF(ifp);
   8959 	if_statadd_ref(nsr, if_obytes, m->m_pkthdr.len);
   8960 	if (m->m_flags & M_MCAST)
   8961 		if_statinc_ref(nsr, if_omcasts);
   8962 	IF_STAT_PUTREF(ifp);
   8963 
   8964 	/*
   8965 	 * The situations which this mutex_tryenter() fails at running time
   8966 	 * are below two patterns.
   8967 	 *     (1) contention with interrupt handler(wm_txrxintr_msix())
   8968 	 *     (2) contention with deferred if_start softint(wm_handle_queue())
   8969 	 * In the case of (1), the last packet enqueued to txq->txq_interq is
   8970 	 * dequeued by wm_deferred_start_locked(). So, it does not get stuck.
   8971 	 * In the case of (2), the last packet enqueued to txq->txq_interq is
   8972 	 * also dequeued by wm_deferred_start_locked(). So, it does not get
   8973 	 * stuck, either.
   8974 	 */
   8975 	if (mutex_tryenter(txq->txq_lock)) {
   8976 		if (!txq->txq_stopping)
   8977 			wm_nq_transmit_locked(ifp, txq);
   8978 		mutex_exit(txq->txq_lock);
   8979 	}
   8980 
   8981 	return 0;
   8982 }
   8983 
   8984 static void
   8985 wm_nq_transmit_locked(struct ifnet *ifp, struct wm_txqueue *txq)
   8986 {
   8987 
   8988 	wm_nq_send_common_locked(ifp, txq, true);
   8989 }
   8990 
   8991 static void
   8992 wm_nq_send_common_locked(struct ifnet *ifp, struct wm_txqueue *txq,
   8993     bool is_transmit)
   8994 {
   8995 	struct wm_softc *sc = ifp->if_softc;
   8996 	struct mbuf *m0;
   8997 	struct wm_txsoft *txs;
   8998 	bus_dmamap_t dmamap;
   8999 	int error, nexttx, lasttx = -1, seg, segs_needed;
   9000 	bool do_csum, sent;
   9001 	bool remap = true;
   9002 
   9003 	KASSERT(mutex_owned(txq->txq_lock));
   9004 
   9005 	if ((ifp->if_flags & IFF_RUNNING) == 0)
   9006 		return;
   9007 	if ((txq->txq_flags & WM_TXQ_NO_SPACE) != 0)
   9008 		return;
   9009 
   9010 	if (__predict_false(wm_linkdown_discard(txq))) {
   9011 		do {
   9012 			if (is_transmit)
   9013 				m0 = pcq_get(txq->txq_interq);
   9014 			else
   9015 				IFQ_DEQUEUE(&ifp->if_snd, m0);
   9016 			/*
   9017 			 * increment successed packet counter as in the case
   9018 			 * which the packet is discarded by link down PHY.
   9019 			 */
   9020 			if (m0 != NULL) {
   9021 				if_statinc(ifp, if_opackets);
   9022 				m_freem(m0);
   9023 			}
   9024 		} while (m0 != NULL);
   9025 		return;
   9026 	}
   9027 
   9028 	sent = false;
   9029 
   9030 	/*
   9031 	 * Loop through the send queue, setting up transmit descriptors
   9032 	 * until we drain the queue, or use up all available transmit
   9033 	 * descriptors.
   9034 	 */
   9035 	for (;;) {
   9036 		m0 = NULL;
   9037 
   9038 		/* Get a work queue entry. */
   9039 		if (txq->txq_sfree < WM_TXQUEUE_GC(txq)) {
   9040 			wm_txeof(txq, UINT_MAX);
   9041 			if (txq->txq_sfree == 0) {
   9042 				DPRINTF(sc, WM_DEBUG_TX,
   9043 				    ("%s: TX: no free job descriptors\n",
   9044 					device_xname(sc->sc_dev)));
   9045 				WM_Q_EVCNT_INCR(txq, txsstall);
   9046 				break;
   9047 			}
   9048 		}
   9049 
   9050 		/* Grab a packet off the queue. */
   9051 		if (is_transmit)
   9052 			m0 = pcq_get(txq->txq_interq);
   9053 		else
   9054 			IFQ_DEQUEUE(&ifp->if_snd, m0);
   9055 		if (m0 == NULL)
   9056 			break;
   9057 
   9058 		DPRINTF(sc, WM_DEBUG_TX,
   9059 		    ("%s: TX: have packet to transmit: %p\n",
   9060 		    device_xname(sc->sc_dev), m0));
   9061 
   9062 		txs = &txq->txq_soft[txq->txq_snext];
   9063 		dmamap = txs->txs_dmamap;
   9064 
   9065 		/*
   9066 		 * Load the DMA map.  If this fails, the packet either
   9067 		 * didn't fit in the allotted number of segments, or we
   9068 		 * were short on resources.  For the too-many-segments
   9069 		 * case, we simply report an error and drop the packet,
   9070 		 * since we can't sanely copy a jumbo packet to a single
   9071 		 * buffer.
   9072 		 */
   9073 retry:
   9074 		error = bus_dmamap_load_mbuf(sc->sc_dmat, dmamap, m0,
   9075 		    BUS_DMA_WRITE | BUS_DMA_NOWAIT);
   9076 		if (__predict_false(error)) {
   9077 			if (error == EFBIG) {
   9078 				if (remap == true) {
   9079 					struct mbuf *m;
   9080 
   9081 					remap = false;
   9082 					m = m_defrag(m0, M_NOWAIT);
   9083 					if (m != NULL) {
   9084 						WM_Q_EVCNT_INCR(txq, defrag);
   9085 						m0 = m;
   9086 						goto retry;
   9087 					}
   9088 				}
   9089 				WM_Q_EVCNT_INCR(txq, toomanyseg);
   9090 				log(LOG_ERR, "%s: Tx packet consumes too many "
   9091 				    "DMA segments, dropping...\n",
   9092 				    device_xname(sc->sc_dev));
   9093 				wm_dump_mbuf_chain(sc, m0);
   9094 				m_freem(m0);
   9095 				continue;
   9096 			}
   9097 			/* Short on resources, just stop for now. */
   9098 			DPRINTF(sc, WM_DEBUG_TX,
   9099 			    ("%s: TX: dmamap load failed: %d\n",
   9100 				device_xname(sc->sc_dev), error));
   9101 			break;
   9102 		}
   9103 
   9104 		segs_needed = dmamap->dm_nsegs;
   9105 
   9106 		/*
   9107 		 * Ensure we have enough descriptors free to describe
   9108 		 * the packet. Note, we always reserve one descriptor
   9109 		 * at the end of the ring due to the semantics of the
   9110 		 * TDT register, plus one more in the event we need
   9111 		 * to load offload context.
   9112 		 */
   9113 		if (segs_needed > txq->txq_free - 2) {
   9114 			/*
   9115 			 * Not enough free descriptors to transmit this
   9116 			 * packet.  We haven't committed anything yet,
   9117 			 * so just unload the DMA map, put the packet
   9118 			 * pack on the queue, and punt. Notify the upper
   9119 			 * layer that there are no more slots left.
   9120 			 */
   9121 			DPRINTF(sc, WM_DEBUG_TX,
   9122 			    ("%s: TX: need %d (%d) descriptors, have %d\n",
   9123 				device_xname(sc->sc_dev), dmamap->dm_nsegs,
   9124 				segs_needed, txq->txq_free - 1));
   9125 			txq->txq_flags |= WM_TXQ_NO_SPACE;
   9126 			bus_dmamap_unload(sc->sc_dmat, dmamap);
   9127 			WM_Q_EVCNT_INCR(txq, txdstall);
   9128 			break;
   9129 		}
   9130 
   9131 		/* WE ARE NOW COMMITTED TO TRANSMITTING THE PACKET. */
   9132 
   9133 		DPRINTF(sc, WM_DEBUG_TX,
   9134 		    ("%s: TX: packet has %d (%d) DMA segments\n",
   9135 		    device_xname(sc->sc_dev), dmamap->dm_nsegs, segs_needed));
   9136 
   9137 		WM_EVCNT_INCR(&txq->txq_ev_txseg[dmamap->dm_nsegs - 1]);
   9138 
   9139 		/*
   9140 		 * Store a pointer to the packet so that we can free it
   9141 		 * later.
   9142 		 *
   9143 		 * Initially, we consider the number of descriptors the
   9144 		 * packet uses the number of DMA segments.  This may be
   9145 		 * incremented by 1 if we do checksum offload (a descriptor
   9146 		 * is used to set the checksum context).
   9147 		 */
   9148 		txs->txs_mbuf = m0;
   9149 		txs->txs_firstdesc = txq->txq_next;
   9150 		txs->txs_ndesc = segs_needed;
   9151 
   9152 		/* Set up offload parameters for this packet. */
   9153 		uint32_t cmdlen, fields, dcmdlen;
   9154 		if (m0->m_pkthdr.csum_flags &
   9155 		    (M_CSUM_TSOv4 | M_CSUM_TSOv6 |
   9156 			M_CSUM_IPv4 | M_CSUM_TCPv4 | M_CSUM_UDPv4 |
   9157 			M_CSUM_TCPv6 | M_CSUM_UDPv6)) {
   9158 			wm_nq_tx_offload(sc, txq, txs, &cmdlen, &fields,
   9159 			    &do_csum);
   9160 		} else {
   9161 			do_csum = false;
   9162 			cmdlen = 0;
   9163 			fields = 0;
   9164 		}
   9165 
   9166 		/* Sync the DMA map. */
   9167 		bus_dmamap_sync(sc->sc_dmat, dmamap, 0, dmamap->dm_mapsize,
   9168 		    BUS_DMASYNC_PREWRITE);
   9169 
   9170 		/* Initialize the first transmit descriptor. */
   9171 		nexttx = txq->txq_next;
   9172 		if (!do_csum) {
   9173 			/* Set up a legacy descriptor */
   9174 			wm_set_dma_addr(&txq->txq_descs[nexttx].wtx_addr,
   9175 			    dmamap->dm_segs[0].ds_addr);
   9176 			txq->txq_descs[nexttx].wtx_cmdlen =
   9177 			    htole32(WTX_CMD_IFCS | dmamap->dm_segs[0].ds_len);
   9178 			txq->txq_descs[nexttx].wtx_fields.wtxu_status = 0;
   9179 			txq->txq_descs[nexttx].wtx_fields.wtxu_options = 0;
   9180 			if (vlan_has_tag(m0)) {
   9181 				txq->txq_descs[nexttx].wtx_cmdlen |=
   9182 				    htole32(WTX_CMD_VLE);
   9183 				txq->txq_descs[nexttx].wtx_fields.wtxu_vlan =
   9184 				    htole16(vlan_get_tag(m0));
   9185 			} else
   9186 				txq->txq_descs[nexttx].wtx_fields.wtxu_vlan =0;
   9187 
   9188 			dcmdlen = 0;
   9189 		} else {
   9190 			/* Set up an advanced data descriptor */
   9191 			txq->txq_nq_descs[nexttx].nqtx_data.nqtxd_addr =
   9192 			    htole64(dmamap->dm_segs[0].ds_addr);
   9193 			KASSERT((dmamap->dm_segs[0].ds_len & cmdlen) == 0);
   9194 			txq->txq_nq_descs[nexttx].nqtx_data.nqtxd_cmdlen =
   9195 			    htole32(dmamap->dm_segs[0].ds_len | cmdlen);
   9196 			txq->txq_nq_descs[nexttx].nqtx_data.nqtxd_fields =
   9197 			    htole32(fields);
   9198 			DPRINTF(sc, WM_DEBUG_TX,
   9199 			    ("%s: TX: adv data desc %d 0x%" PRIx64 "\n",
   9200 				device_xname(sc->sc_dev), nexttx,
   9201 				(uint64_t)dmamap->dm_segs[0].ds_addr));
   9202 			DPRINTF(sc, WM_DEBUG_TX,
   9203 			    ("\t 0x%08x%08x\n", fields,
   9204 				(uint32_t)dmamap->dm_segs[0].ds_len | cmdlen));
   9205 			dcmdlen = NQTX_DTYP_D | NQTX_CMD_DEXT;
   9206 		}
   9207 
   9208 		lasttx = nexttx;
   9209 		nexttx = WM_NEXTTX(txq, nexttx);
   9210 		/*
   9211 		 * Fill in the next descriptors. Legacy or advanced format
   9212 		 * is the same here.
   9213 		 */
   9214 		for (seg = 1; seg < dmamap->dm_nsegs;
   9215 		     seg++, nexttx = WM_NEXTTX(txq, nexttx)) {
   9216 			txq->txq_nq_descs[nexttx].nqtx_data.nqtxd_addr =
   9217 			    htole64(dmamap->dm_segs[seg].ds_addr);
   9218 			txq->txq_nq_descs[nexttx].nqtx_data.nqtxd_cmdlen =
   9219 			    htole32(dcmdlen | dmamap->dm_segs[seg].ds_len);
   9220 			KASSERT((dcmdlen & dmamap->dm_segs[seg].ds_len) == 0);
   9221 			txq->txq_nq_descs[nexttx].nqtx_data.nqtxd_fields = 0;
   9222 			lasttx = nexttx;
   9223 
   9224 			DPRINTF(sc, WM_DEBUG_TX,
   9225 			    ("%s: TX: desc %d: %#" PRIx64 ", len %#04zx\n",
   9226 				device_xname(sc->sc_dev), nexttx,
   9227 				(uint64_t)dmamap->dm_segs[seg].ds_addr,
   9228 				dmamap->dm_segs[seg].ds_len));
   9229 		}
   9230 
   9231 		KASSERT(lasttx != -1);
   9232 
   9233 		/*
   9234 		 * Set up the command byte on the last descriptor of
   9235 		 * the packet. If we're in the interrupt delay window,
   9236 		 * delay the interrupt.
   9237 		 */
   9238 		KASSERT((WTX_CMD_EOP | WTX_CMD_RS) ==
   9239 		    (NQTX_CMD_EOP | NQTX_CMD_RS));
   9240 		txq->txq_descs[lasttx].wtx_cmdlen |=
   9241 		    htole32(WTX_CMD_EOP | WTX_CMD_RS);
   9242 
   9243 		txs->txs_lastdesc = lasttx;
   9244 
   9245 		DPRINTF(sc, WM_DEBUG_TX, ("%s: TX: desc %d: cmdlen 0x%08x\n",
   9246 		    device_xname(sc->sc_dev),
   9247 		    lasttx, le32toh(txq->txq_descs[lasttx].wtx_cmdlen)));
   9248 
   9249 		/* Sync the descriptors we're using. */
   9250 		wm_cdtxsync(txq, txq->txq_next, txs->txs_ndesc,
   9251 		    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
   9252 
   9253 		/* Give the packet to the chip. */
   9254 		CSR_WRITE(sc, txq->txq_tdt_reg, nexttx);
   9255 		sent = true;
   9256 
   9257 		DPRINTF(sc, WM_DEBUG_TX,
   9258 		    ("%s: TX: TDT -> %d\n", device_xname(sc->sc_dev), nexttx));
   9259 
   9260 		DPRINTF(sc, WM_DEBUG_TX,
   9261 		    ("%s: TX: finished transmitting packet, job %d\n",
   9262 			device_xname(sc->sc_dev), txq->txq_snext));
   9263 
   9264 		/* Advance the tx pointer. */
   9265 		txq->txq_free -= txs->txs_ndesc;
   9266 		txq->txq_next = nexttx;
   9267 
   9268 		txq->txq_sfree--;
   9269 		txq->txq_snext = WM_NEXTTXS(txq, txq->txq_snext);
   9270 
   9271 		/* Pass the packet to any BPF listeners. */
   9272 		bpf_mtap(ifp, m0, BPF_D_OUT);
   9273 	}
   9274 
   9275 	if (m0 != NULL) {
   9276 		txq->txq_flags |= WM_TXQ_NO_SPACE;
   9277 		WM_Q_EVCNT_INCR(txq, descdrop);
   9278 		DPRINTF(sc, WM_DEBUG_TX, ("%s: TX: error after IFQ_DEQUEUE\n",
   9279 			__func__));
   9280 		m_freem(m0);
   9281 	}
   9282 
   9283 	if (txq->txq_sfree == 0 || txq->txq_free <= 2) {
   9284 		/* No more slots; notify upper layer. */
   9285 		txq->txq_flags |= WM_TXQ_NO_SPACE;
   9286 	}
   9287 
   9288 	if (sent) {
   9289 		/* Set a watchdog timer in case the chip flakes out. */
   9290 		txq->txq_lastsent = time_uptime;
   9291 		txq->txq_sending = true;
   9292 	}
   9293 }
   9294 
   9295 static void
   9296 wm_deferred_start_locked(struct wm_txqueue *txq)
   9297 {
   9298 	struct wm_softc *sc = txq->txq_sc;
   9299 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   9300 	struct wm_queue *wmq = container_of(txq, struct wm_queue, wmq_txq);
   9301 	int qid = wmq->wmq_id;
   9302 
   9303 	KASSERT(mutex_owned(txq->txq_lock));
   9304 	KASSERT(!txq->txq_stopping);
   9305 
   9306 	if ((sc->sc_flags & WM_F_NEWQUEUE) != 0) {
   9307 		/* XXX need for ALTQ or one CPU system */
   9308 		if (qid == 0)
   9309 			wm_nq_start_locked(ifp);
   9310 		wm_nq_transmit_locked(ifp, txq);
   9311 	} else {
   9312 		/* XXX need for ALTQ or one CPU system */
   9313 		if (qid == 0)
   9314 			wm_start_locked(ifp);
   9315 		wm_transmit_locked(ifp, txq);
   9316 	}
   9317 }
   9318 
   9319 /* Interrupt */
   9320 
   9321 /*
   9322  * wm_txeof:
   9323  *
   9324  *	Helper; handle transmit interrupts.
   9325  */
   9326 static bool
   9327 wm_txeof(struct wm_txqueue *txq, u_int limit)
   9328 {
   9329 	struct wm_softc *sc = txq->txq_sc;
   9330 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   9331 	struct wm_txsoft *txs;
   9332 	int count = 0;
   9333 	int i;
   9334 	uint8_t status;
   9335 	bool more = false;
   9336 
   9337 	KASSERT(mutex_owned(txq->txq_lock));
   9338 
   9339 	if (txq->txq_stopping)
   9340 		return false;
   9341 
   9342 	txq->txq_flags &= ~WM_TXQ_NO_SPACE;
   9343 
   9344 	/*
   9345 	 * Go through the Tx list and free mbufs for those
   9346 	 * frames which have been transmitted.
   9347 	 */
   9348 	for (i = txq->txq_sdirty; txq->txq_sfree != WM_TXQUEUELEN(txq);
   9349 	     i = WM_NEXTTXS(txq, i), txq->txq_sfree++) {
   9350 		txs = &txq->txq_soft[i];
   9351 
   9352 		DPRINTF(sc, WM_DEBUG_TX, ("%s: TX: checking job %d\n",
   9353 			device_xname(sc->sc_dev), i));
   9354 
   9355 		wm_cdtxsync(txq, txs->txs_firstdesc, txs->txs_ndesc,
   9356 		    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
   9357 
   9358 		status =
   9359 		    txq->txq_descs[txs->txs_lastdesc].wtx_fields.wtxu_status;
   9360 		if ((status & WTX_ST_DD) == 0) {
   9361 			wm_cdtxsync(txq, txs->txs_lastdesc, 1,
   9362 			    BUS_DMASYNC_PREREAD);
   9363 			break;
   9364 		}
   9365 
   9366 		if (limit-- == 0) {
   9367 			more = true;
   9368 			DPRINTF(sc, WM_DEBUG_TX,
   9369 			    ("%s: TX: loop limited, job %d is not processed\n",
   9370 				device_xname(sc->sc_dev), i));
   9371 			break;
   9372 		}
   9373 
   9374 		count++;
   9375 		DPRINTF(sc, WM_DEBUG_TX,
   9376 		    ("%s: TX: job %d done: descs %d..%d\n",
   9377 		    device_xname(sc->sc_dev), i, txs->txs_firstdesc,
   9378 		    txs->txs_lastdesc));
   9379 
   9380 		/*
   9381 		 * XXX We should probably be using the statistics
   9382 		 * XXX registers, but I don't know if they exist
   9383 		 * XXX on chips before the i82544.
   9384 		 */
   9385 
   9386 #ifdef WM_EVENT_COUNTERS
   9387 		if (status & WTX_ST_TU)
   9388 			WM_Q_EVCNT_INCR(txq, underrun);
   9389 #endif /* WM_EVENT_COUNTERS */
   9390 
   9391 		/*
   9392 		 * 82574 and newer's document says the status field has neither
   9393 		 * EC (Excessive Collision) bit nor LC (Late Collision) bit
   9394 		 * (reserved). Refer "PCIe GbE Controller Open Source Software
   9395 		 * Developer's Manual", 82574 datasheet and newer.
   9396 		 *
   9397 		 * XXX I saw the LC bit was set on I218 even though the media
   9398 		 * was full duplex, so the bit might be used for other
   9399 		 * meaning ...(I have no document).
   9400 		 */
   9401 
   9402 		if (((status & (WTX_ST_EC | WTX_ST_LC)) != 0)
   9403 		    && ((sc->sc_type < WM_T_82574)
   9404 			|| (sc->sc_type == WM_T_80003))) {
   9405 			if_statinc(ifp, if_oerrors);
   9406 			if (status & WTX_ST_LC)
   9407 				log(LOG_WARNING, "%s: late collision\n",
   9408 				    device_xname(sc->sc_dev));
   9409 			else if (status & WTX_ST_EC) {
   9410 				if_statadd(ifp, if_collisions,
   9411 				    TX_COLLISION_THRESHOLD + 1);
   9412 				log(LOG_WARNING, "%s: excessive collisions\n",
   9413 				    device_xname(sc->sc_dev));
   9414 			}
   9415 		} else
   9416 			if_statinc(ifp, if_opackets);
   9417 
   9418 		txq->txq_packets++;
   9419 		txq->txq_bytes += txs->txs_mbuf->m_pkthdr.len;
   9420 
   9421 		txq->txq_free += txs->txs_ndesc;
   9422 		bus_dmamap_sync(sc->sc_dmat, txs->txs_dmamap,
   9423 		    0, txs->txs_dmamap->dm_mapsize, BUS_DMASYNC_POSTWRITE);
   9424 		bus_dmamap_unload(sc->sc_dmat, txs->txs_dmamap);
   9425 		m_freem(txs->txs_mbuf);
   9426 		txs->txs_mbuf = NULL;
   9427 	}
   9428 
   9429 	/* Update the dirty transmit buffer pointer. */
   9430 	txq->txq_sdirty = i;
   9431 	DPRINTF(sc, WM_DEBUG_TX,
   9432 	    ("%s: TX: txsdirty -> %d\n", device_xname(sc->sc_dev), i));
   9433 
   9434 	if (count != 0)
   9435 		rnd_add_uint32(&sc->rnd_source, count);
   9436 
   9437 	/*
   9438 	 * If there are no more pending transmissions, cancel the watchdog
   9439 	 * timer.
   9440 	 */
   9441 	if (txq->txq_sfree == WM_TXQUEUELEN(txq))
   9442 		txq->txq_sending = false;
   9443 
   9444 	return more;
   9445 }
   9446 
   9447 static inline uint32_t
   9448 wm_rxdesc_get_status(struct wm_rxqueue *rxq, int idx)
   9449 {
   9450 	struct wm_softc *sc = rxq->rxq_sc;
   9451 
   9452 	if (sc->sc_type == WM_T_82574)
   9453 		return EXTRXC_STATUS(
   9454 		    le32toh(rxq->rxq_ext_descs[idx].erx_ctx.erxc_err_stat));
   9455 	else if ((sc->sc_flags & WM_F_NEWQUEUE) != 0)
   9456 		return NQRXC_STATUS(
   9457 		    le32toh(rxq->rxq_nq_descs[idx].nqrx_ctx.nrxc_err_stat));
   9458 	else
   9459 		return rxq->rxq_descs[idx].wrx_status;
   9460 }
   9461 
   9462 static inline uint32_t
   9463 wm_rxdesc_get_errors(struct wm_rxqueue *rxq, int idx)
   9464 {
   9465 	struct wm_softc *sc = rxq->rxq_sc;
   9466 
   9467 	if (sc->sc_type == WM_T_82574)
   9468 		return EXTRXC_ERROR(
   9469 		    le32toh(rxq->rxq_ext_descs[idx].erx_ctx.erxc_err_stat));
   9470 	else if ((sc->sc_flags & WM_F_NEWQUEUE) != 0)
   9471 		return NQRXC_ERROR(
   9472 		    le32toh(rxq->rxq_nq_descs[idx].nqrx_ctx.nrxc_err_stat));
   9473 	else
   9474 		return rxq->rxq_descs[idx].wrx_errors;
   9475 }
   9476 
   9477 static inline uint16_t
   9478 wm_rxdesc_get_vlantag(struct wm_rxqueue *rxq, int idx)
   9479 {
   9480 	struct wm_softc *sc = rxq->rxq_sc;
   9481 
   9482 	if (sc->sc_type == WM_T_82574)
   9483 		return rxq->rxq_ext_descs[idx].erx_ctx.erxc_vlan;
   9484 	else if ((sc->sc_flags & WM_F_NEWQUEUE) != 0)
   9485 		return rxq->rxq_nq_descs[idx].nqrx_ctx.nrxc_vlan;
   9486 	else
   9487 		return rxq->rxq_descs[idx].wrx_special;
   9488 }
   9489 
   9490 static inline int
   9491 wm_rxdesc_get_pktlen(struct wm_rxqueue *rxq, int idx)
   9492 {
   9493 	struct wm_softc *sc = rxq->rxq_sc;
   9494 
   9495 	if (sc->sc_type == WM_T_82574)
   9496 		return rxq->rxq_ext_descs[idx].erx_ctx.erxc_pktlen;
   9497 	else if ((sc->sc_flags & WM_F_NEWQUEUE) != 0)
   9498 		return rxq->rxq_nq_descs[idx].nqrx_ctx.nrxc_pktlen;
   9499 	else
   9500 		return rxq->rxq_descs[idx].wrx_len;
   9501 }
   9502 
   9503 #ifdef WM_DEBUG
   9504 static inline uint32_t
   9505 wm_rxdesc_get_rsshash(struct wm_rxqueue *rxq, int idx)
   9506 {
   9507 	struct wm_softc *sc = rxq->rxq_sc;
   9508 
   9509 	if (sc->sc_type == WM_T_82574)
   9510 		return rxq->rxq_ext_descs[idx].erx_ctx.erxc_rsshash;
   9511 	else if ((sc->sc_flags & WM_F_NEWQUEUE) != 0)
   9512 		return rxq->rxq_nq_descs[idx].nqrx_ctx.nrxc_rsshash;
   9513 	else
   9514 		return 0;
   9515 }
   9516 
   9517 static inline uint8_t
   9518 wm_rxdesc_get_rsstype(struct wm_rxqueue *rxq, int idx)
   9519 {
   9520 	struct wm_softc *sc = rxq->rxq_sc;
   9521 
   9522 	if (sc->sc_type == WM_T_82574)
   9523 		return EXTRXC_RSS_TYPE(rxq->rxq_ext_descs[idx].erx_ctx.erxc_mrq);
   9524 	else if ((sc->sc_flags & WM_F_NEWQUEUE) != 0)
   9525 		return NQRXC_RSS_TYPE(rxq->rxq_nq_descs[idx].nqrx_ctx.nrxc_misc);
   9526 	else
   9527 		return 0;
   9528 }
   9529 #endif /* WM_DEBUG */
   9530 
   9531 static inline bool
   9532 wm_rxdesc_is_set_status(struct wm_softc *sc, uint32_t status,
   9533     uint32_t legacy_bit, uint32_t ext_bit, uint32_t nq_bit)
   9534 {
   9535 
   9536 	if (sc->sc_type == WM_T_82574)
   9537 		return (status & ext_bit) != 0;
   9538 	else if ((sc->sc_flags & WM_F_NEWQUEUE) != 0)
   9539 		return (status & nq_bit) != 0;
   9540 	else
   9541 		return (status & legacy_bit) != 0;
   9542 }
   9543 
   9544 static inline bool
   9545 wm_rxdesc_is_set_error(struct wm_softc *sc, uint32_t error,
   9546     uint32_t legacy_bit, uint32_t ext_bit, uint32_t nq_bit)
   9547 {
   9548 
   9549 	if (sc->sc_type == WM_T_82574)
   9550 		return (error & ext_bit) != 0;
   9551 	else if ((sc->sc_flags & WM_F_NEWQUEUE) != 0)
   9552 		return (error & nq_bit) != 0;
   9553 	else
   9554 		return (error & legacy_bit) != 0;
   9555 }
   9556 
   9557 static inline bool
   9558 wm_rxdesc_is_eop(struct wm_rxqueue *rxq, uint32_t status)
   9559 {
   9560 
   9561 	if (wm_rxdesc_is_set_status(rxq->rxq_sc, status,
   9562 		WRX_ST_EOP, EXTRXC_STATUS_EOP, NQRXC_STATUS_EOP))
   9563 		return true;
   9564 	else
   9565 		return false;
   9566 }
   9567 
   9568 static inline bool
   9569 wm_rxdesc_has_errors(struct wm_rxqueue *rxq, uint32_t errors)
   9570 {
   9571 	struct wm_softc *sc = rxq->rxq_sc;
   9572 
   9573 	/* XXX missing error bit for newqueue? */
   9574 	if (wm_rxdesc_is_set_error(sc, errors,
   9575 		WRX_ER_CE | WRX_ER_SE | WRX_ER_SEQ | WRX_ER_CXE | WRX_ER_RXE,
   9576 		EXTRXC_ERROR_CE | EXTRXC_ERROR_SE | EXTRXC_ERROR_SEQ
   9577 		| EXTRXC_ERROR_CXE | EXTRXC_ERROR_RXE,
   9578 		NQRXC_ERROR_RXE)) {
   9579 		if (wm_rxdesc_is_set_error(sc, errors, WRX_ER_SE,
   9580 		    EXTRXC_ERROR_SE, 0))
   9581 			log(LOG_WARNING, "%s: symbol error\n",
   9582 			    device_xname(sc->sc_dev));
   9583 		else if (wm_rxdesc_is_set_error(sc, errors, WRX_ER_SEQ,
   9584 		    EXTRXC_ERROR_SEQ, 0))
   9585 			log(LOG_WARNING, "%s: receive sequence error\n",
   9586 			    device_xname(sc->sc_dev));
   9587 		else if (wm_rxdesc_is_set_error(sc, errors, WRX_ER_CE,
   9588 		    EXTRXC_ERROR_CE, 0))
   9589 			log(LOG_WARNING, "%s: CRC error\n",
   9590 			    device_xname(sc->sc_dev));
   9591 		return true;
   9592 	}
   9593 
   9594 	return false;
   9595 }
   9596 
   9597 static inline bool
   9598 wm_rxdesc_dd(struct wm_rxqueue *rxq, int idx, uint32_t status)
   9599 {
   9600 	struct wm_softc *sc = rxq->rxq_sc;
   9601 
   9602 	if (!wm_rxdesc_is_set_status(sc, status, WRX_ST_DD, EXTRXC_STATUS_DD,
   9603 		NQRXC_STATUS_DD)) {
   9604 		/* We have processed all of the receive descriptors. */
   9605 		wm_cdrxsync(rxq, idx, BUS_DMASYNC_PREREAD);
   9606 		return false;
   9607 	}
   9608 
   9609 	return true;
   9610 }
   9611 
   9612 static inline bool
   9613 wm_rxdesc_input_vlantag(struct wm_rxqueue *rxq, uint32_t status,
   9614     uint16_t vlantag, struct mbuf *m)
   9615 {
   9616 
   9617 	if (wm_rxdesc_is_set_status(rxq->rxq_sc, status,
   9618 		WRX_ST_VP, EXTRXC_STATUS_VP, NQRXC_STATUS_VP)) {
   9619 		vlan_set_tag(m, le16toh(vlantag));
   9620 	}
   9621 
   9622 	return true;
   9623 }
   9624 
   9625 static inline void
   9626 wm_rxdesc_ensure_checksum(struct wm_rxqueue *rxq, uint32_t status,
   9627     uint32_t errors, struct mbuf *m)
   9628 {
   9629 	struct wm_softc *sc = rxq->rxq_sc;
   9630 
   9631 	if (!wm_rxdesc_is_set_status(sc, status, WRX_ST_IXSM, 0, 0)) {
   9632 		if (wm_rxdesc_is_set_status(sc, status,
   9633 		    WRX_ST_IPCS, EXTRXC_STATUS_IPCS, NQRXC_STATUS_IPCS)) {
   9634 			WM_Q_EVCNT_INCR(rxq, ipsum);
   9635 			m->m_pkthdr.csum_flags |= M_CSUM_IPv4;
   9636 			if (wm_rxdesc_is_set_error(sc, errors,
   9637 			    WRX_ER_IPE, EXTRXC_ERROR_IPE, NQRXC_ERROR_IPE))
   9638 				m->m_pkthdr.csum_flags |= M_CSUM_IPv4_BAD;
   9639 		}
   9640 		if (wm_rxdesc_is_set_status(sc, status,
   9641 		    WRX_ST_TCPCS, EXTRXC_STATUS_TCPCS, NQRXC_STATUS_L4I)) {
   9642 			/*
   9643 			 * Note: we don't know if this was TCP or UDP,
   9644 			 * so we just set both bits, and expect the
   9645 			 * upper layers to deal.
   9646 			 */
   9647 			WM_Q_EVCNT_INCR(rxq, tusum);
   9648 			m->m_pkthdr.csum_flags |=
   9649 			    M_CSUM_TCPv4 | M_CSUM_UDPv4 |
   9650 			    M_CSUM_TCPv6 | M_CSUM_UDPv6;
   9651 			if (wm_rxdesc_is_set_error(sc, errors, WRX_ER_TCPE,
   9652 			    EXTRXC_ERROR_TCPE, NQRXC_ERROR_L4E))
   9653 				m->m_pkthdr.csum_flags |= M_CSUM_TCP_UDP_BAD;
   9654 		}
   9655 	}
   9656 }
   9657 
   9658 /*
   9659  * wm_rxeof:
   9660  *
   9661  *	Helper; handle receive interrupts.
   9662  */
   9663 static bool
   9664 wm_rxeof(struct wm_rxqueue *rxq, u_int limit)
   9665 {
   9666 	struct wm_softc *sc = rxq->rxq_sc;
   9667 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   9668 	struct wm_rxsoft *rxs;
   9669 	struct mbuf *m;
   9670 	int i, len;
   9671 	int count = 0;
   9672 	uint32_t status, errors;
   9673 	uint16_t vlantag;
   9674 	bool more = false;
   9675 
   9676 	KASSERT(mutex_owned(rxq->rxq_lock));
   9677 
   9678 	for (i = rxq->rxq_ptr;; i = WM_NEXTRX(i)) {
   9679 		rxs = &rxq->rxq_soft[i];
   9680 
   9681 		DPRINTF(sc, WM_DEBUG_RX,
   9682 		    ("%s: RX: checking descriptor %d\n",
   9683 			device_xname(sc->sc_dev), i));
   9684 		wm_cdrxsync(rxq, i,
   9685 		    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
   9686 
   9687 		status = wm_rxdesc_get_status(rxq, i);
   9688 		errors = wm_rxdesc_get_errors(rxq, i);
   9689 		len = le16toh(wm_rxdesc_get_pktlen(rxq, i));
   9690 		vlantag = wm_rxdesc_get_vlantag(rxq, i);
   9691 #ifdef WM_DEBUG
   9692 		uint32_t rsshash = le32toh(wm_rxdesc_get_rsshash(rxq, i));
   9693 		uint8_t rsstype = wm_rxdesc_get_rsstype(rxq, i);
   9694 #endif
   9695 
   9696 		if (!wm_rxdesc_dd(rxq, i, status))
   9697 			break;
   9698 
   9699 		if (limit-- == 0) {
   9700 			more = true;
   9701 			DPRINTF(sc, WM_DEBUG_RX,
   9702 			    ("%s: RX: loop limited, descriptor %d is not processed\n",
   9703 				device_xname(sc->sc_dev), i));
   9704 			break;
   9705 		}
   9706 
   9707 		count++;
   9708 		if (__predict_false(rxq->rxq_discard)) {
   9709 			DPRINTF(sc, WM_DEBUG_RX,
   9710 			    ("%s: RX: discarding contents of descriptor %d\n",
   9711 				device_xname(sc->sc_dev), i));
   9712 			wm_init_rxdesc(rxq, i);
   9713 			if (wm_rxdesc_is_eop(rxq, status)) {
   9714 				/* Reset our state. */
   9715 				DPRINTF(sc, WM_DEBUG_RX,
   9716 				    ("%s: RX: resetting rxdiscard -> 0\n",
   9717 					device_xname(sc->sc_dev)));
   9718 				rxq->rxq_discard = 0;
   9719 			}
   9720 			continue;
   9721 		}
   9722 
   9723 		bus_dmamap_sync(sc->sc_dmat, rxs->rxs_dmamap, 0,
   9724 		    rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_POSTREAD);
   9725 
   9726 		m = rxs->rxs_mbuf;
   9727 
   9728 		/*
   9729 		 * Add a new receive buffer to the ring, unless of
   9730 		 * course the length is zero. Treat the latter as a
   9731 		 * failed mapping.
   9732 		 */
   9733 		if ((len == 0) || (wm_add_rxbuf(rxq, i) != 0)) {
   9734 			/*
   9735 			 * Failed, throw away what we've done so
   9736 			 * far, and discard the rest of the packet.
   9737 			 */
   9738 			if_statinc(ifp, if_ierrors);
   9739 			bus_dmamap_sync(sc->sc_dmat, rxs->rxs_dmamap, 0,
   9740 			    rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
   9741 			wm_init_rxdesc(rxq, i);
   9742 			if (!wm_rxdesc_is_eop(rxq, status))
   9743 				rxq->rxq_discard = 1;
   9744 			if (rxq->rxq_head != NULL)
   9745 				m_freem(rxq->rxq_head);
   9746 			WM_RXCHAIN_RESET(rxq);
   9747 			DPRINTF(sc, WM_DEBUG_RX,
   9748 			    ("%s: RX: Rx buffer allocation failed, "
   9749 			    "dropping packet%s\n", device_xname(sc->sc_dev),
   9750 				rxq->rxq_discard ? " (discard)" : ""));
   9751 			continue;
   9752 		}
   9753 
   9754 		m->m_len = len;
   9755 		rxq->rxq_len += len;
   9756 		DPRINTF(sc, WM_DEBUG_RX,
   9757 		    ("%s: RX: buffer at %p len %d\n",
   9758 			device_xname(sc->sc_dev), m->m_data, len));
   9759 
   9760 		/* If this is not the end of the packet, keep looking. */
   9761 		if (!wm_rxdesc_is_eop(rxq, status)) {
   9762 			WM_RXCHAIN_LINK(rxq, m);
   9763 			DPRINTF(sc, WM_DEBUG_RX,
   9764 			    ("%s: RX: not yet EOP, rxlen -> %d\n",
   9765 				device_xname(sc->sc_dev), rxq->rxq_len));
   9766 			continue;
   9767 		}
   9768 
   9769 		/*
   9770 		 * Okay, we have the entire packet now. The chip is
   9771 		 * configured to include the FCS except I35[04], I21[01].
   9772 		 * (not all chips can be configured to strip it), so we need
   9773 		 * to trim it. Those chips have an eratta, the RCTL_SECRC bit
   9774 		 * in RCTL register is always set, so we don't trim it.
   9775 		 * PCH2 and newer chip also not include FCS when jumbo
   9776 		 * frame is used to do workaround an errata.
   9777 		 * May need to adjust length of previous mbuf in the
   9778 		 * chain if the current mbuf is too short.
   9779 		 */
   9780 		if ((sc->sc_flags & WM_F_CRC_STRIP) == 0) {
   9781 			if (m->m_len < ETHER_CRC_LEN) {
   9782 				rxq->rxq_tail->m_len
   9783 				    -= (ETHER_CRC_LEN - m->m_len);
   9784 				m->m_len = 0;
   9785 			} else
   9786 				m->m_len -= ETHER_CRC_LEN;
   9787 			len = rxq->rxq_len - ETHER_CRC_LEN;
   9788 		} else
   9789 			len = rxq->rxq_len;
   9790 
   9791 		WM_RXCHAIN_LINK(rxq, m);
   9792 
   9793 		*rxq->rxq_tailp = NULL;
   9794 		m = rxq->rxq_head;
   9795 
   9796 		WM_RXCHAIN_RESET(rxq);
   9797 
   9798 		DPRINTF(sc, WM_DEBUG_RX,
   9799 		    ("%s: RX: have entire packet, len -> %d\n",
   9800 			device_xname(sc->sc_dev), len));
   9801 
   9802 		/* If an error occurred, update stats and drop the packet. */
   9803 		if (wm_rxdesc_has_errors(rxq, errors)) {
   9804 			m_freem(m);
   9805 			continue;
   9806 		}
   9807 
   9808 		/* No errors.  Receive the packet. */
   9809 		m_set_rcvif(m, ifp);
   9810 		m->m_pkthdr.len = len;
   9811 		/*
   9812 		 * TODO
   9813 		 * should be save rsshash and rsstype to this mbuf.
   9814 		 */
   9815 		DPRINTF(sc, WM_DEBUG_RX,
   9816 		    ("%s: RX: RSS type=%" PRIu8 ", RSS hash=%" PRIu32 "\n",
   9817 			device_xname(sc->sc_dev), rsstype, rsshash));
   9818 
   9819 		/*
   9820 		 * If VLANs are enabled, VLAN packets have been unwrapped
   9821 		 * for us.  Associate the tag with the packet.
   9822 		 */
   9823 		if (!wm_rxdesc_input_vlantag(rxq, status, vlantag, m))
   9824 			continue;
   9825 
   9826 		/* Set up checksum info for this packet. */
   9827 		wm_rxdesc_ensure_checksum(rxq, status, errors, m);
   9828 
   9829 		rxq->rxq_packets++;
   9830 		rxq->rxq_bytes += len;
   9831 		/* Pass it on. */
   9832 		if_percpuq_enqueue(sc->sc_ipq, m);
   9833 
   9834 		if (rxq->rxq_stopping)
   9835 			break;
   9836 	}
   9837 	rxq->rxq_ptr = i;
   9838 
   9839 	if (count != 0)
   9840 		rnd_add_uint32(&sc->rnd_source, count);
   9841 
   9842 	DPRINTF(sc, WM_DEBUG_RX,
   9843 	    ("%s: RX: rxptr -> %d\n", device_xname(sc->sc_dev), i));
   9844 
   9845 	return more;
   9846 }
   9847 
   9848 /*
   9849  * wm_linkintr_gmii:
   9850  *
   9851  *	Helper; handle link interrupts for GMII.
   9852  */
   9853 static void
   9854 wm_linkintr_gmii(struct wm_softc *sc, uint32_t icr)
   9855 {
   9856 	device_t dev = sc->sc_dev;
   9857 	uint32_t status, reg;
   9858 	bool link;
   9859 	int rv;
   9860 
   9861 	KASSERT(WM_CORE_LOCKED(sc));
   9862 
   9863 	DPRINTF(sc, WM_DEBUG_LINK, ("%s: %s:\n", device_xname(dev),
   9864 		__func__));
   9865 
   9866 	if ((icr & ICR_LSC) == 0) {
   9867 		if (icr & ICR_RXSEQ)
   9868 			DPRINTF(sc, WM_DEBUG_LINK,
   9869 			    ("%s: LINK Receive sequence error\n",
   9870 				device_xname(dev)));
   9871 		return;
   9872 	}
   9873 
   9874 	/* Link status changed */
   9875 	status = CSR_READ(sc, WMREG_STATUS);
   9876 	link = status & STATUS_LU;
   9877 	if (link) {
   9878 		DPRINTF(sc, WM_DEBUG_LINK, ("%s: LINK: LSC -> up %s\n",
   9879 			device_xname(dev),
   9880 			(status & STATUS_FD) ? "FDX" : "HDX"));
   9881 		if (wm_phy_need_linkdown_discard(sc)) {
   9882 			DPRINTF(sc, WM_DEBUG_LINK,
   9883 			    ("%s: linkintr: Clear linkdown discard flag\n",
   9884 				device_xname(dev)));
   9885 			wm_clear_linkdown_discard(sc);
   9886 		}
   9887 	} else {
   9888 		DPRINTF(sc, WM_DEBUG_LINK, ("%s: LINK: LSC -> down\n",
   9889 			device_xname(dev)));
   9890 		if (wm_phy_need_linkdown_discard(sc)) {
   9891 			DPRINTF(sc, WM_DEBUG_LINK,
   9892 			    ("%s: linkintr: Set linkdown discard flag\n",
   9893 				device_xname(dev)));
   9894 			wm_set_linkdown_discard(sc);
   9895 		}
   9896 	}
   9897 	if ((sc->sc_type == WM_T_ICH8) && (link == false))
   9898 		wm_gig_downshift_workaround_ich8lan(sc);
   9899 
   9900 	if ((sc->sc_type == WM_T_ICH8) && (sc->sc_phytype == WMPHY_IGP_3))
   9901 		wm_kmrn_lock_loss_workaround_ich8lan(sc);
   9902 
   9903 	DPRINTF(sc, WM_DEBUG_LINK, ("%s: LINK: LSC -> mii_pollstat\n",
   9904 		device_xname(dev)));
   9905 	mii_pollstat(&sc->sc_mii);
   9906 	if (sc->sc_type == WM_T_82543) {
   9907 		int miistatus, active;
   9908 
   9909 		/*
   9910 		 * With 82543, we need to force speed and
   9911 		 * duplex on the MAC equal to what the PHY
   9912 		 * speed and duplex configuration is.
   9913 		 */
   9914 		miistatus = sc->sc_mii.mii_media_status;
   9915 
   9916 		if (miistatus & IFM_ACTIVE) {
   9917 			active = sc->sc_mii.mii_media_active;
   9918 			sc->sc_ctrl &= ~(CTRL_SPEED_MASK | CTRL_FD);
   9919 			switch (IFM_SUBTYPE(active)) {
   9920 			case IFM_10_T:
   9921 				sc->sc_ctrl |= CTRL_SPEED_10;
   9922 				break;
   9923 			case IFM_100_TX:
   9924 				sc->sc_ctrl |= CTRL_SPEED_100;
   9925 				break;
   9926 			case IFM_1000_T:
   9927 				sc->sc_ctrl |= CTRL_SPEED_1000;
   9928 				break;
   9929 			default:
   9930 				/*
   9931 				 * Fiber?
   9932 				 * Shoud not enter here.
   9933 				 */
   9934 				device_printf(dev, "unknown media (%x)\n",
   9935 				    active);
   9936 				break;
   9937 			}
   9938 			if (active & IFM_FDX)
   9939 				sc->sc_ctrl |= CTRL_FD;
   9940 			CSR_WRITE(sc, WMREG_CTRL, sc->sc_ctrl);
   9941 		}
   9942 	} else if (sc->sc_type == WM_T_PCH) {
   9943 		wm_k1_gig_workaround_hv(sc,
   9944 		    ((sc->sc_mii.mii_media_status & IFM_ACTIVE) != 0));
   9945 	}
   9946 
   9947 	/*
   9948 	 * When connected at 10Mbps half-duplex, some parts are excessively
   9949 	 * aggressive resulting in many collisions. To avoid this, increase
   9950 	 * the IPG and reduce Rx latency in the PHY.
   9951 	 */
   9952 	if ((sc->sc_type >= WM_T_PCH2) && (sc->sc_type <= WM_T_PCH_CNP)
   9953 	    && link) {
   9954 		uint32_t tipg_reg;
   9955 		uint32_t speed = __SHIFTOUT(status, STATUS_SPEED);
   9956 		bool fdx;
   9957 		uint16_t emi_addr, emi_val;
   9958 
   9959 		tipg_reg = CSR_READ(sc, WMREG_TIPG);
   9960 		tipg_reg &= ~TIPG_IPGT_MASK;
   9961 		fdx = status & STATUS_FD;
   9962 
   9963 		if (!fdx && (speed == STATUS_SPEED_10)) {
   9964 			tipg_reg |= 0xff;
   9965 			/* Reduce Rx latency in analog PHY */
   9966 			emi_val = 0;
   9967 		} else if ((sc->sc_type >= WM_T_PCH_SPT) &&
   9968 		    fdx && speed != STATUS_SPEED_1000) {
   9969 			tipg_reg |= 0xc;
   9970 			emi_val = 1;
   9971 		} else {
   9972 			/* Roll back the default values */
   9973 			tipg_reg |= 0x08;
   9974 			emi_val = 1;
   9975 		}
   9976 
   9977 		CSR_WRITE(sc, WMREG_TIPG, tipg_reg);
   9978 
   9979 		rv = sc->phy.acquire(sc);
   9980 		if (rv)
   9981 			return;
   9982 
   9983 		if (sc->sc_type == WM_T_PCH2)
   9984 			emi_addr = I82579_RX_CONFIG;
   9985 		else
   9986 			emi_addr = I217_RX_CONFIG;
   9987 		rv = wm_write_emi_reg_locked(dev, emi_addr, emi_val);
   9988 
   9989 		if (sc->sc_type >= WM_T_PCH_LPT) {
   9990 			uint16_t phy_reg;
   9991 
   9992 			sc->phy.readreg_locked(dev, 2,
   9993 			    I217_PLL_CLOCK_GATE_REG, &phy_reg);
   9994 			phy_reg &= ~I217_PLL_CLOCK_GATE_MASK;
   9995 			if (speed == STATUS_SPEED_100
   9996 			    || speed == STATUS_SPEED_10)
   9997 				phy_reg |= 0x3e8;
   9998 			else
   9999 				phy_reg |= 0xfa;
   10000 			sc->phy.writereg_locked(dev, 2,
   10001 			    I217_PLL_CLOCK_GATE_REG, phy_reg);
   10002 
   10003 			if (speed == STATUS_SPEED_1000) {
   10004 				sc->phy.readreg_locked(dev, 2,
   10005 				    HV_PM_CTRL, &phy_reg);
   10006 
   10007 				phy_reg |= HV_PM_CTRL_K1_CLK_REQ;
   10008 
   10009 				sc->phy.writereg_locked(dev, 2,
   10010 				    HV_PM_CTRL, phy_reg);
   10011 			}
   10012 		}
   10013 		sc->phy.release(sc);
   10014 
   10015 		if (rv)
   10016 			return;
   10017 
   10018 		if (sc->sc_type >= WM_T_PCH_SPT) {
   10019 			uint16_t data, ptr_gap;
   10020 
   10021 			if (speed == STATUS_SPEED_1000) {
   10022 				rv = sc->phy.acquire(sc);
   10023 				if (rv)
   10024 					return;
   10025 
   10026 				rv = sc->phy.readreg_locked(dev, 2,
   10027 				    I82579_UNKNOWN1, &data);
   10028 				if (rv) {
   10029 					sc->phy.release(sc);
   10030 					return;
   10031 				}
   10032 
   10033 				ptr_gap = (data & (0x3ff << 2)) >> 2;
   10034 				if (ptr_gap < 0x18) {
   10035 					data &= ~(0x3ff << 2);
   10036 					data |= (0x18 << 2);
   10037 					rv = sc->phy.writereg_locked(dev,
   10038 					    2, I82579_UNKNOWN1, data);
   10039 				}
   10040 				sc->phy.release(sc);
   10041 				if (rv)
   10042 					return;
   10043 			} else {
   10044 				rv = sc->phy.acquire(sc);
   10045 				if (rv)
   10046 					return;
   10047 
   10048 				rv = sc->phy.writereg_locked(dev, 2,
   10049 				    I82579_UNKNOWN1, 0xc023);
   10050 				sc->phy.release(sc);
   10051 				if (rv)
   10052 					return;
   10053 
   10054 			}
   10055 		}
   10056 	}
   10057 
   10058 	/*
   10059 	 * I217 Packet Loss issue:
   10060 	 * ensure that FEXTNVM4 Beacon Duration is set correctly
   10061 	 * on power up.
   10062 	 * Set the Beacon Duration for I217 to 8 usec
   10063 	 */
   10064 	if (sc->sc_type >= WM_T_PCH_LPT) {
   10065 		reg = CSR_READ(sc, WMREG_FEXTNVM4);
   10066 		reg &= ~FEXTNVM4_BEACON_DURATION;
   10067 		reg |= FEXTNVM4_BEACON_DURATION_8US;
   10068 		CSR_WRITE(sc, WMREG_FEXTNVM4, reg);
   10069 	}
   10070 
   10071 	/* Work-around I218 hang issue */
   10072 	if ((sc->sc_pcidevid == PCI_PRODUCT_INTEL_I218_LM) ||
   10073 	    (sc->sc_pcidevid == PCI_PRODUCT_INTEL_I218_V) ||
   10074 	    (sc->sc_pcidevid == PCI_PRODUCT_INTEL_I218_LM3) ||
   10075 	    (sc->sc_pcidevid == PCI_PRODUCT_INTEL_I218_V3))
   10076 		wm_k1_workaround_lpt_lp(sc, link);
   10077 
   10078 	if (sc->sc_type >= WM_T_PCH_LPT) {
   10079 		/*
   10080 		 * Set platform power management values for Latency
   10081 		 * Tolerance Reporting (LTR)
   10082 		 */
   10083 		wm_platform_pm_pch_lpt(sc,
   10084 		    ((sc->sc_mii.mii_media_status & IFM_ACTIVE) != 0));
   10085 	}
   10086 
   10087 	/* Clear link partner's EEE ability */
   10088 	sc->eee_lp_ability = 0;
   10089 
   10090 	/* FEXTNVM6 K1-off workaround */
   10091 	if (sc->sc_type == WM_T_PCH_SPT) {
   10092 		reg = CSR_READ(sc, WMREG_FEXTNVM6);
   10093 		if (CSR_READ(sc, WMREG_PCIEANACFG) & FEXTNVM6_K1_OFF_ENABLE)
   10094 			reg |= FEXTNVM6_K1_OFF_ENABLE;
   10095 		else
   10096 			reg &= ~FEXTNVM6_K1_OFF_ENABLE;
   10097 		CSR_WRITE(sc, WMREG_FEXTNVM6, reg);
   10098 	}
   10099 
   10100 	if (!link)
   10101 		return;
   10102 
   10103 	switch (sc->sc_type) {
   10104 	case WM_T_PCH2:
   10105 		wm_k1_workaround_lv(sc);
   10106 		/* FALLTHROUGH */
   10107 	case WM_T_PCH:
   10108 		if (sc->sc_phytype == WMPHY_82578)
   10109 			wm_link_stall_workaround_hv(sc);
   10110 		break;
   10111 	default:
   10112 		break;
   10113 	}
   10114 
   10115 	/* Enable/Disable EEE after link up */
   10116 	if (sc->sc_phytype > WMPHY_82579)
   10117 		wm_set_eee_pchlan(sc);
   10118 }
   10119 
   10120 /*
   10121  * wm_linkintr_tbi:
   10122  *
   10123  *	Helper; handle link interrupts for TBI mode.
   10124  */
   10125 static void
   10126 wm_linkintr_tbi(struct wm_softc *sc, uint32_t icr)
   10127 {
   10128 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   10129 	uint32_t status;
   10130 
   10131 	DPRINTF(sc, WM_DEBUG_LINK, ("%s: %s:\n", device_xname(sc->sc_dev),
   10132 		__func__));
   10133 
   10134 	status = CSR_READ(sc, WMREG_STATUS);
   10135 	if (icr & ICR_LSC) {
   10136 		wm_check_for_link(sc);
   10137 		if (status & STATUS_LU) {
   10138 			DPRINTF(sc, WM_DEBUG_LINK, ("%s: LINK: LSC -> up %s\n",
   10139 				device_xname(sc->sc_dev),
   10140 				(status & STATUS_FD) ? "FDX" : "HDX"));
   10141 			/*
   10142 			 * NOTE: CTRL will update TFCE and RFCE automatically,
   10143 			 * so we should update sc->sc_ctrl
   10144 			 */
   10145 
   10146 			sc->sc_ctrl = CSR_READ(sc, WMREG_CTRL);
   10147 			sc->sc_tctl &= ~TCTL_COLD(0x3ff);
   10148 			sc->sc_fcrtl &= ~FCRTL_XONE;
   10149 			if (status & STATUS_FD)
   10150 				sc->sc_tctl |=
   10151 				    TCTL_COLD(TX_COLLISION_DISTANCE_FDX);
   10152 			else
   10153 				sc->sc_tctl |=
   10154 				    TCTL_COLD(TX_COLLISION_DISTANCE_HDX);
   10155 			if (sc->sc_ctrl & CTRL_TFCE)
   10156 				sc->sc_fcrtl |= FCRTL_XONE;
   10157 			CSR_WRITE(sc, WMREG_TCTL, sc->sc_tctl);
   10158 			CSR_WRITE(sc, (sc->sc_type < WM_T_82543) ?
   10159 			    WMREG_OLD_FCRTL : WMREG_FCRTL, sc->sc_fcrtl);
   10160 			sc->sc_tbi_linkup = 1;
   10161 			if_link_state_change(ifp, LINK_STATE_UP);
   10162 		} else {
   10163 			DPRINTF(sc, WM_DEBUG_LINK, ("%s: LINK: LSC -> down\n",
   10164 				device_xname(sc->sc_dev)));
   10165 			sc->sc_tbi_linkup = 0;
   10166 			if_link_state_change(ifp, LINK_STATE_DOWN);
   10167 		}
   10168 		/* Update LED */
   10169 		wm_tbi_serdes_set_linkled(sc);
   10170 	} else if (icr & ICR_RXSEQ)
   10171 		DPRINTF(sc, WM_DEBUG_LINK,
   10172 		    ("%s: LINK: Receive sequence error\n",
   10173 			device_xname(sc->sc_dev)));
   10174 }
   10175 
   10176 /*
   10177  * wm_linkintr_serdes:
   10178  *
   10179  *	Helper; handle link interrupts for TBI mode.
   10180  */
   10181 static void
   10182 wm_linkintr_serdes(struct wm_softc *sc, uint32_t icr)
   10183 {
   10184 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   10185 	struct mii_data *mii = &sc->sc_mii;
   10186 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
   10187 	uint32_t pcs_adv, pcs_lpab, reg;
   10188 
   10189 	DPRINTF(sc, WM_DEBUG_LINK, ("%s: %s:\n", device_xname(sc->sc_dev),
   10190 		__func__));
   10191 
   10192 	if (icr & ICR_LSC) {
   10193 		/* Check PCS */
   10194 		reg = CSR_READ(sc, WMREG_PCS_LSTS);
   10195 		if ((reg & PCS_LSTS_LINKOK) != 0) {
   10196 			DPRINTF(sc, WM_DEBUG_LINK, ("%s: LINK: LSC -> up\n",
   10197 				device_xname(sc->sc_dev)));
   10198 			mii->mii_media_status |= IFM_ACTIVE;
   10199 			sc->sc_tbi_linkup = 1;
   10200 			if_link_state_change(ifp, LINK_STATE_UP);
   10201 		} else {
   10202 			DPRINTF(sc, WM_DEBUG_LINK, ("%s: LINK: LSC -> down\n",
   10203 				device_xname(sc->sc_dev)));
   10204 			mii->mii_media_status |= IFM_NONE;
   10205 			sc->sc_tbi_linkup = 0;
   10206 			if_link_state_change(ifp, LINK_STATE_DOWN);
   10207 			wm_tbi_serdes_set_linkled(sc);
   10208 			return;
   10209 		}
   10210 		mii->mii_media_active |= IFM_1000_SX;
   10211 		if ((reg & PCS_LSTS_FDX) != 0)
   10212 			mii->mii_media_active |= IFM_FDX;
   10213 		else
   10214 			mii->mii_media_active |= IFM_HDX;
   10215 		if (IFM_SUBTYPE(ife->ifm_media) == IFM_AUTO) {
   10216 			/* Check flow */
   10217 			reg = CSR_READ(sc, WMREG_PCS_LSTS);
   10218 			if ((reg & PCS_LSTS_AN_COMP) == 0) {
   10219 				DPRINTF(sc, WM_DEBUG_LINK,
   10220 				    ("XXX LINKOK but not ACOMP\n"));
   10221 				return;
   10222 			}
   10223 			pcs_adv = CSR_READ(sc, WMREG_PCS_ANADV);
   10224 			pcs_lpab = CSR_READ(sc, WMREG_PCS_LPAB);
   10225 			DPRINTF(sc, WM_DEBUG_LINK,
   10226 			    ("XXX AN result %08x, %08x\n", pcs_adv, pcs_lpab));
   10227 			if ((pcs_adv & TXCW_SYM_PAUSE)
   10228 			    && (pcs_lpab & TXCW_SYM_PAUSE)) {
   10229 				mii->mii_media_active |= IFM_FLOW
   10230 				    | IFM_ETH_TXPAUSE | IFM_ETH_RXPAUSE;
   10231 			} else if (((pcs_adv & TXCW_SYM_PAUSE) == 0)
   10232 			    && (pcs_adv & TXCW_ASYM_PAUSE)
   10233 			    && (pcs_lpab & TXCW_SYM_PAUSE)
   10234 			    && (pcs_lpab & TXCW_ASYM_PAUSE))
   10235 				mii->mii_media_active |= IFM_FLOW
   10236 				    | IFM_ETH_TXPAUSE;
   10237 			else if ((pcs_adv & TXCW_SYM_PAUSE)
   10238 			    && (pcs_adv & TXCW_ASYM_PAUSE)
   10239 			    && ((pcs_lpab & TXCW_SYM_PAUSE) == 0)
   10240 			    && (pcs_lpab & TXCW_ASYM_PAUSE))
   10241 				mii->mii_media_active |= IFM_FLOW
   10242 				    | IFM_ETH_RXPAUSE;
   10243 		}
   10244 		/* Update LED */
   10245 		wm_tbi_serdes_set_linkled(sc);
   10246 	} else
   10247 		DPRINTF(sc, WM_DEBUG_LINK,
   10248 		    ("%s: LINK: Receive sequence error\n",
   10249 		    device_xname(sc->sc_dev)));
   10250 }
   10251 
   10252 /*
   10253  * wm_linkintr:
   10254  *
   10255  *	Helper; handle link interrupts.
   10256  */
   10257 static void
   10258 wm_linkintr(struct wm_softc *sc, uint32_t icr)
   10259 {
   10260 
   10261 	KASSERT(WM_CORE_LOCKED(sc));
   10262 
   10263 	if (sc->sc_flags & WM_F_HAS_MII)
   10264 		wm_linkintr_gmii(sc, icr);
   10265 	else if ((sc->sc_mediatype == WM_MEDIATYPE_SERDES)
   10266 	    && ((sc->sc_type >= WM_T_82575) && (sc->sc_type <= WM_T_I211)))
   10267 		wm_linkintr_serdes(sc, icr);
   10268 	else
   10269 		wm_linkintr_tbi(sc, icr);
   10270 }
   10271 
   10272 
   10273 static inline void
   10274 wm_sched_handle_queue(struct wm_softc *sc, struct wm_queue *wmq)
   10275 {
   10276 
   10277 	if (wmq->wmq_txrx_use_workqueue)
   10278 		workqueue_enqueue(sc->sc_queue_wq, &wmq->wmq_cookie, curcpu());
   10279 	else
   10280 		softint_schedule(wmq->wmq_si);
   10281 }
   10282 
   10283 static inline void
   10284 wm_legacy_intr_disable(struct wm_softc *sc)
   10285 {
   10286 
   10287 	CSR_WRITE(sc, WMREG_IMC, 0xffffffffU);
   10288 }
   10289 
   10290 static inline void
   10291 wm_legacy_intr_enable(struct wm_softc *sc)
   10292 {
   10293 
   10294 	CSR_WRITE(sc, WMREG_IMS, sc->sc_icr);
   10295 }
   10296 
   10297 /*
   10298  * wm_intr_legacy:
   10299  *
   10300  *	Interrupt service routine for INTx and MSI.
   10301  */
   10302 static int
   10303 wm_intr_legacy(void *arg)
   10304 {
   10305 	struct wm_softc *sc = arg;
   10306 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   10307 	struct wm_queue *wmq = &sc->sc_queue[0];
   10308 	struct wm_txqueue *txq = &wmq->wmq_txq;
   10309 	struct wm_rxqueue *rxq = &wmq->wmq_rxq;
   10310 	u_int txlimit = sc->sc_tx_intr_process_limit;
   10311 	u_int rxlimit = sc->sc_rx_intr_process_limit;
   10312 	uint32_t icr, rndval = 0;
   10313 	bool more = false;
   10314 
   10315 	icr = CSR_READ(sc, WMREG_ICR);
   10316 	if ((icr & sc->sc_icr) == 0)
   10317 		return 0;
   10318 
   10319 	DPRINTF(sc, WM_DEBUG_TX,
   10320 	    ("%s: INTx: got intr\n",device_xname(sc->sc_dev)));
   10321 	if (rndval == 0)
   10322 		rndval = icr;
   10323 
   10324 	mutex_enter(txq->txq_lock);
   10325 
   10326 	if (txq->txq_stopping) {
   10327 		mutex_exit(txq->txq_lock);
   10328 		return 1;
   10329 	}
   10330 
   10331 #if defined(WM_DEBUG) || defined(WM_EVENT_COUNTERS)
   10332 	if (icr & ICR_TXDW) {
   10333 		DPRINTF(sc, WM_DEBUG_TX,
   10334 		    ("%s: TX: got TXDW interrupt\n",
   10335 			device_xname(sc->sc_dev)));
   10336 		WM_Q_EVCNT_INCR(txq, txdw);
   10337 	}
   10338 #endif
   10339 	if (txlimit > 0) {
   10340 		more |= wm_txeof(txq, txlimit);
   10341 		if (!IF_IS_EMPTY(&ifp->if_snd))
   10342 			more = true;
   10343 	} else
   10344 		more = true;
   10345 	mutex_exit(txq->txq_lock);
   10346 
   10347 	mutex_enter(rxq->rxq_lock);
   10348 
   10349 	if (rxq->rxq_stopping) {
   10350 		mutex_exit(rxq->rxq_lock);
   10351 		return 1;
   10352 	}
   10353 
   10354 #if defined(WM_DEBUG) || defined(WM_EVENT_COUNTERS)
   10355 	if (icr & (ICR_RXDMT0 | ICR_RXT0)) {
   10356 		DPRINTF(sc, WM_DEBUG_RX,
   10357 		    ("%s: RX: got Rx intr %#" __PRIxBIT "\n",
   10358 			device_xname(sc->sc_dev),
   10359 			icr & (ICR_RXDMT0 | ICR_RXT0)));
   10360 		WM_Q_EVCNT_INCR(rxq, intr);
   10361 	}
   10362 #endif
   10363 	if (rxlimit > 0) {
   10364 		/*
   10365 		 * wm_rxeof() does *not* call upper layer functions directly,
   10366 		 * as if_percpuq_enqueue() just call softint_schedule().
   10367 		 * So, we can call wm_rxeof() in interrupt context.
   10368 		 */
   10369 		more = wm_rxeof(rxq, rxlimit);
   10370 	} else
   10371 		more = true;
   10372 
   10373 	mutex_exit(rxq->rxq_lock);
   10374 
   10375 	WM_CORE_LOCK(sc);
   10376 
   10377 	if (sc->sc_core_stopping) {
   10378 		WM_CORE_UNLOCK(sc);
   10379 		return 1;
   10380 	}
   10381 
   10382 	if (icr & (ICR_LSC | ICR_RXSEQ)) {
   10383 		WM_EVCNT_INCR(&sc->sc_ev_linkintr);
   10384 		wm_linkintr(sc, icr);
   10385 	}
   10386 	if ((icr & ICR_GPI(0)) != 0)
   10387 		device_printf(sc->sc_dev, "got module interrupt\n");
   10388 
   10389 	WM_CORE_UNLOCK(sc);
   10390 
   10391 	if (icr & ICR_RXO) {
   10392 #if defined(WM_DEBUG)
   10393 		log(LOG_WARNING, "%s: Receive overrun\n",
   10394 		    device_xname(sc->sc_dev));
   10395 #endif /* defined(WM_DEBUG) */
   10396 	}
   10397 
   10398 	rnd_add_uint32(&sc->rnd_source, rndval);
   10399 
   10400 	if (more) {
   10401 		/* Try to get more packets going. */
   10402 		wm_legacy_intr_disable(sc);
   10403 		wmq->wmq_txrx_use_workqueue = sc->sc_txrx_use_workqueue;
   10404 		wm_sched_handle_queue(sc, wmq);
   10405 	}
   10406 
   10407 	return 1;
   10408 }
   10409 
   10410 static inline void
   10411 wm_txrxintr_disable(struct wm_queue *wmq)
   10412 {
   10413 	struct wm_softc *sc = wmq->wmq_txq.txq_sc;
   10414 
   10415 	if (__predict_false(!wm_is_using_msix(sc))) {
   10416 		wm_legacy_intr_disable(sc);
   10417 		return;
   10418 	}
   10419 
   10420 	if (sc->sc_type == WM_T_82574)
   10421 		CSR_WRITE(sc, WMREG_IMC,
   10422 		    ICR_TXQ(wmq->wmq_id) | ICR_RXQ(wmq->wmq_id));
   10423 	else if (sc->sc_type == WM_T_82575)
   10424 		CSR_WRITE(sc, WMREG_EIMC,
   10425 		    EITR_TX_QUEUE(wmq->wmq_id) | EITR_RX_QUEUE(wmq->wmq_id));
   10426 	else
   10427 		CSR_WRITE(sc, WMREG_EIMC, 1 << wmq->wmq_intr_idx);
   10428 }
   10429 
   10430 static inline void
   10431 wm_txrxintr_enable(struct wm_queue *wmq)
   10432 {
   10433 	struct wm_softc *sc = wmq->wmq_txq.txq_sc;
   10434 
   10435 	wm_itrs_calculate(sc, wmq);
   10436 
   10437 	if (__predict_false(!wm_is_using_msix(sc))) {
   10438 		wm_legacy_intr_enable(sc);
   10439 		return;
   10440 	}
   10441 
   10442 	/*
   10443 	 * ICR_OTHER which is disabled in wm_linkintr_msix() is enabled here.
   10444 	 * There is no need to care about which of RXQ(0) and RXQ(1) enable
   10445 	 * ICR_OTHER in first, because each RXQ/TXQ interrupt is disabled
   10446 	 * while each wm_handle_queue(wmq) is runnig.
   10447 	 */
   10448 	if (sc->sc_type == WM_T_82574)
   10449 		CSR_WRITE(sc, WMREG_IMS,
   10450 		    ICR_TXQ(wmq->wmq_id) | ICR_RXQ(wmq->wmq_id) | ICR_OTHER);
   10451 	else if (sc->sc_type == WM_T_82575)
   10452 		CSR_WRITE(sc, WMREG_EIMS,
   10453 		    EITR_TX_QUEUE(wmq->wmq_id) | EITR_RX_QUEUE(wmq->wmq_id));
   10454 	else
   10455 		CSR_WRITE(sc, WMREG_EIMS, 1 << wmq->wmq_intr_idx);
   10456 }
   10457 
   10458 static int
   10459 wm_txrxintr_msix(void *arg)
   10460 {
   10461 	struct wm_queue *wmq = arg;
   10462 	struct wm_txqueue *txq = &wmq->wmq_txq;
   10463 	struct wm_rxqueue *rxq = &wmq->wmq_rxq;
   10464 	struct wm_softc *sc = txq->txq_sc;
   10465 	u_int txlimit = sc->sc_tx_intr_process_limit;
   10466 	u_int rxlimit = sc->sc_rx_intr_process_limit;
   10467 	bool txmore;
   10468 	bool rxmore;
   10469 
   10470 	KASSERT(wmq->wmq_intr_idx == wmq->wmq_id);
   10471 
   10472 	DPRINTF(sc, WM_DEBUG_TX,
   10473 	    ("%s: TX: got Tx intr\n", device_xname(sc->sc_dev)));
   10474 
   10475 	wm_txrxintr_disable(wmq);
   10476 
   10477 	mutex_enter(txq->txq_lock);
   10478 
   10479 	if (txq->txq_stopping) {
   10480 		mutex_exit(txq->txq_lock);
   10481 		return 1;
   10482 	}
   10483 
   10484 	WM_Q_EVCNT_INCR(txq, txdw);
   10485 	if (txlimit > 0) {
   10486 		txmore = wm_txeof(txq, txlimit);
   10487 		/* wm_deferred start() is done in wm_handle_queue(). */
   10488 	} else
   10489 		txmore = true;
   10490 	mutex_exit(txq->txq_lock);
   10491 
   10492 	DPRINTF(sc, WM_DEBUG_RX,
   10493 	    ("%s: RX: got Rx intr\n", device_xname(sc->sc_dev)));
   10494 	mutex_enter(rxq->rxq_lock);
   10495 
   10496 	if (rxq->rxq_stopping) {
   10497 		mutex_exit(rxq->rxq_lock);
   10498 		return 1;
   10499 	}
   10500 
   10501 	WM_Q_EVCNT_INCR(rxq, intr);
   10502 	if (rxlimit > 0) {
   10503 		rxmore = wm_rxeof(rxq, rxlimit);
   10504 	} else
   10505 		rxmore = true;
   10506 	mutex_exit(rxq->rxq_lock);
   10507 
   10508 	wm_itrs_writereg(sc, wmq);
   10509 
   10510 	if (txmore || rxmore) {
   10511 		wmq->wmq_txrx_use_workqueue = sc->sc_txrx_use_workqueue;
   10512 		wm_sched_handle_queue(sc, wmq);
   10513 	} else
   10514 		wm_txrxintr_enable(wmq);
   10515 
   10516 	return 1;
   10517 }
   10518 
   10519 static void
   10520 wm_handle_queue(void *arg)
   10521 {
   10522 	struct wm_queue *wmq = arg;
   10523 	struct wm_txqueue *txq = &wmq->wmq_txq;
   10524 	struct wm_rxqueue *rxq = &wmq->wmq_rxq;
   10525 	struct wm_softc *sc = txq->txq_sc;
   10526 	u_int txlimit = sc->sc_tx_process_limit;
   10527 	u_int rxlimit = sc->sc_rx_process_limit;
   10528 	bool txmore;
   10529 	bool rxmore;
   10530 
   10531 	mutex_enter(txq->txq_lock);
   10532 	if (txq->txq_stopping) {
   10533 		mutex_exit(txq->txq_lock);
   10534 		return;
   10535 	}
   10536 	txmore = wm_txeof(txq, txlimit);
   10537 	wm_deferred_start_locked(txq);
   10538 	mutex_exit(txq->txq_lock);
   10539 
   10540 	mutex_enter(rxq->rxq_lock);
   10541 	if (rxq->rxq_stopping) {
   10542 		mutex_exit(rxq->rxq_lock);
   10543 		return;
   10544 	}
   10545 	WM_Q_EVCNT_INCR(rxq, defer);
   10546 	rxmore = wm_rxeof(rxq, rxlimit);
   10547 	mutex_exit(rxq->rxq_lock);
   10548 
   10549 	if (txmore || rxmore) {
   10550 		wmq->wmq_txrx_use_workqueue = sc->sc_txrx_use_workqueue;
   10551 		wm_sched_handle_queue(sc, wmq);
   10552 	} else
   10553 		wm_txrxintr_enable(wmq);
   10554 }
   10555 
   10556 static void
   10557 wm_handle_queue_work(struct work *wk, void *context)
   10558 {
   10559 	struct wm_queue *wmq = container_of(wk, struct wm_queue, wmq_cookie);
   10560 
   10561 	/*
   10562 	 * "enqueued flag" is not required here.
   10563 	 */
   10564 	wm_handle_queue(wmq);
   10565 }
   10566 
   10567 /*
   10568  * wm_linkintr_msix:
   10569  *
   10570  *	Interrupt service routine for link status change for MSI-X.
   10571  */
   10572 static int
   10573 wm_linkintr_msix(void *arg)
   10574 {
   10575 	struct wm_softc *sc = arg;
   10576 	uint32_t reg;
   10577 	bool has_rxo;
   10578 
   10579 	reg = CSR_READ(sc, WMREG_ICR);
   10580 	WM_CORE_LOCK(sc);
   10581 	DPRINTF(sc, WM_DEBUG_LINK,
   10582 	    ("%s: LINK: got link intr. ICR = %08x\n",
   10583 		device_xname(sc->sc_dev), reg));
   10584 
   10585 	if (sc->sc_core_stopping)
   10586 		goto out;
   10587 
   10588 	if ((reg & ICR_LSC) != 0) {
   10589 		WM_EVCNT_INCR(&sc->sc_ev_linkintr);
   10590 		wm_linkintr(sc, ICR_LSC);
   10591 	}
   10592 	if ((reg & ICR_GPI(0)) != 0)
   10593 		device_printf(sc->sc_dev, "got module interrupt\n");
   10594 
   10595 	/*
   10596 	 * XXX 82574 MSI-X mode workaround
   10597 	 *
   10598 	 * 82574 MSI-X mode causes receive overrun(RXO) interrupt as ICR_OTHER
   10599 	 * MSI-X vector, furthermore it does not cause neigher ICR_RXQ(0) nor
   10600 	 * ICR_RXQ(1) vector. So, we generate ICR_RXQ(0) and ICR_RXQ(1)
   10601 	 * interrupts by writing WMREG_ICS to process receive packets.
   10602 	 */
   10603 	if (sc->sc_type == WM_T_82574 && ((reg & ICR_RXO) != 0)) {
   10604 #if defined(WM_DEBUG)
   10605 		log(LOG_WARNING, "%s: Receive overrun\n",
   10606 		    device_xname(sc->sc_dev));
   10607 #endif /* defined(WM_DEBUG) */
   10608 
   10609 		has_rxo = true;
   10610 		/*
   10611 		 * The RXO interrupt is very high rate when receive traffic is
   10612 		 * high rate. We use polling mode for ICR_OTHER like Tx/Rx
   10613 		 * interrupts. ICR_OTHER will be enabled at the end of
   10614 		 * wm_txrxintr_msix() which is kicked by both ICR_RXQ(0) and
   10615 		 * ICR_RXQ(1) interrupts.
   10616 		 */
   10617 		CSR_WRITE(sc, WMREG_IMC, ICR_OTHER);
   10618 
   10619 		CSR_WRITE(sc, WMREG_ICS, ICR_RXQ(0) | ICR_RXQ(1));
   10620 	}
   10621 
   10622 
   10623 
   10624 out:
   10625 	WM_CORE_UNLOCK(sc);
   10626 
   10627 	if (sc->sc_type == WM_T_82574) {
   10628 		if (!has_rxo)
   10629 			CSR_WRITE(sc, WMREG_IMS, ICR_OTHER | ICR_LSC);
   10630 		else
   10631 			CSR_WRITE(sc, WMREG_IMS, ICR_LSC);
   10632 	} else if (sc->sc_type == WM_T_82575)
   10633 		CSR_WRITE(sc, WMREG_EIMS, EITR_OTHER);
   10634 	else
   10635 		CSR_WRITE(sc, WMREG_EIMS, 1 << sc->sc_link_intr_idx);
   10636 
   10637 	return 1;
   10638 }
   10639 
   10640 /*
   10641  * Media related.
   10642  * GMII, SGMII, TBI (and SERDES)
   10643  */
   10644 
   10645 /* Common */
   10646 
   10647 /*
   10648  * wm_tbi_serdes_set_linkled:
   10649  *
   10650  *	Update the link LED on TBI and SERDES devices.
   10651  */
   10652 static void
   10653 wm_tbi_serdes_set_linkled(struct wm_softc *sc)
   10654 {
   10655 
   10656 	if (sc->sc_tbi_linkup)
   10657 		sc->sc_ctrl |= CTRL_SWDPIN(0);
   10658 	else
   10659 		sc->sc_ctrl &= ~CTRL_SWDPIN(0);
   10660 
   10661 	/* 82540 or newer devices are active low */
   10662 	sc->sc_ctrl ^= (sc->sc_type >= WM_T_82540) ? CTRL_SWDPIN(0) : 0;
   10663 
   10664 	CSR_WRITE(sc, WMREG_CTRL, sc->sc_ctrl);
   10665 }
   10666 
   10667 /* GMII related */
   10668 
   10669 /*
   10670  * wm_gmii_reset:
   10671  *
   10672  *	Reset the PHY.
   10673  */
   10674 static void
   10675 wm_gmii_reset(struct wm_softc *sc)
   10676 {
   10677 	uint32_t reg;
   10678 	int rv;
   10679 
   10680 	DPRINTF(sc, WM_DEBUG_INIT, ("%s: %s called\n",
   10681 		device_xname(sc->sc_dev), __func__));
   10682 
   10683 	rv = sc->phy.acquire(sc);
   10684 	if (rv != 0) {
   10685 		aprint_error_dev(sc->sc_dev, "%s: failed to get semaphore\n",
   10686 		    __func__);
   10687 		return;
   10688 	}
   10689 
   10690 	switch (sc->sc_type) {
   10691 	case WM_T_82542_2_0:
   10692 	case WM_T_82542_2_1:
   10693 		/* null */
   10694 		break;
   10695 	case WM_T_82543:
   10696 		/*
   10697 		 * With 82543, we need to force speed and duplex on the MAC
   10698 		 * equal to what the PHY speed and duplex configuration is.
   10699 		 * In addition, we need to perform a hardware reset on the PHY
   10700 		 * to take it out of reset.
   10701 		 */
   10702 		sc->sc_ctrl |= CTRL_FRCSPD | CTRL_FRCFDX;
   10703 		CSR_WRITE(sc, WMREG_CTRL, sc->sc_ctrl);
   10704 
   10705 		/* The PHY reset pin is active-low. */
   10706 		reg = CSR_READ(sc, WMREG_CTRL_EXT);
   10707 		reg &= ~((CTRL_EXT_SWDPIO_MASK << CTRL_EXT_SWDPIO_SHIFT) |
   10708 		    CTRL_EXT_SWDPIN(4));
   10709 		reg |= CTRL_EXT_SWDPIO(4);
   10710 
   10711 		CSR_WRITE(sc, WMREG_CTRL_EXT, reg);
   10712 		CSR_WRITE_FLUSH(sc);
   10713 		delay(10*1000);
   10714 
   10715 		CSR_WRITE(sc, WMREG_CTRL_EXT, reg | CTRL_EXT_SWDPIN(4));
   10716 		CSR_WRITE_FLUSH(sc);
   10717 		delay(150);
   10718 #if 0
   10719 		sc->sc_ctrl_ext = reg | CTRL_EXT_SWDPIN(4);
   10720 #endif
   10721 		delay(20*1000);	/* XXX extra delay to get PHY ID? */
   10722 		break;
   10723 	case WM_T_82544:	/* Reset 10000us */
   10724 	case WM_T_82540:
   10725 	case WM_T_82545:
   10726 	case WM_T_82545_3:
   10727 	case WM_T_82546:
   10728 	case WM_T_82546_3:
   10729 	case WM_T_82541:
   10730 	case WM_T_82541_2:
   10731 	case WM_T_82547:
   10732 	case WM_T_82547_2:
   10733 	case WM_T_82571:	/* Reset 100us */
   10734 	case WM_T_82572:
   10735 	case WM_T_82573:
   10736 	case WM_T_82574:
   10737 	case WM_T_82575:
   10738 	case WM_T_82576:
   10739 	case WM_T_82580:
   10740 	case WM_T_I350:
   10741 	case WM_T_I354:
   10742 	case WM_T_I210:
   10743 	case WM_T_I211:
   10744 	case WM_T_82583:
   10745 	case WM_T_80003:
   10746 		/* Generic reset */
   10747 		CSR_WRITE(sc, WMREG_CTRL, sc->sc_ctrl | CTRL_PHY_RESET);
   10748 		CSR_WRITE_FLUSH(sc);
   10749 		delay(20000);
   10750 		CSR_WRITE(sc, WMREG_CTRL, sc->sc_ctrl);
   10751 		CSR_WRITE_FLUSH(sc);
   10752 		delay(20000);
   10753 
   10754 		if ((sc->sc_type == WM_T_82541)
   10755 		    || (sc->sc_type == WM_T_82541_2)
   10756 		    || (sc->sc_type == WM_T_82547)
   10757 		    || (sc->sc_type == WM_T_82547_2)) {
   10758 			/* Workaround for igp are done in igp_reset() */
   10759 			/* XXX add code to set LED after phy reset */
   10760 		}
   10761 		break;
   10762 	case WM_T_ICH8:
   10763 	case WM_T_ICH9:
   10764 	case WM_T_ICH10:
   10765 	case WM_T_PCH:
   10766 	case WM_T_PCH2:
   10767 	case WM_T_PCH_LPT:
   10768 	case WM_T_PCH_SPT:
   10769 	case WM_T_PCH_CNP:
   10770 		/* Generic reset */
   10771 		CSR_WRITE(sc, WMREG_CTRL, sc->sc_ctrl | CTRL_PHY_RESET);
   10772 		CSR_WRITE_FLUSH(sc);
   10773 		delay(100);
   10774 		CSR_WRITE(sc, WMREG_CTRL, sc->sc_ctrl);
   10775 		CSR_WRITE_FLUSH(sc);
   10776 		delay(150);
   10777 		break;
   10778 	default:
   10779 		panic("%s: %s: unknown type\n", device_xname(sc->sc_dev),
   10780 		    __func__);
   10781 		break;
   10782 	}
   10783 
   10784 	sc->phy.release(sc);
   10785 
   10786 	/* get_cfg_done */
   10787 	wm_get_cfg_done(sc);
   10788 
   10789 	/* Extra setup */
   10790 	switch (sc->sc_type) {
   10791 	case WM_T_82542_2_0:
   10792 	case WM_T_82542_2_1:
   10793 	case WM_T_82543:
   10794 	case WM_T_82544:
   10795 	case WM_T_82540:
   10796 	case WM_T_82545:
   10797 	case WM_T_82545_3:
   10798 	case WM_T_82546:
   10799 	case WM_T_82546_3:
   10800 	case WM_T_82541_2:
   10801 	case WM_T_82547_2:
   10802 	case WM_T_82571:
   10803 	case WM_T_82572:
   10804 	case WM_T_82573:
   10805 	case WM_T_82574:
   10806 	case WM_T_82583:
   10807 	case WM_T_82575:
   10808 	case WM_T_82576:
   10809 	case WM_T_82580:
   10810 	case WM_T_I350:
   10811 	case WM_T_I354:
   10812 	case WM_T_I210:
   10813 	case WM_T_I211:
   10814 	case WM_T_80003:
   10815 		/* Null */
   10816 		break;
   10817 	case WM_T_82541:
   10818 	case WM_T_82547:
   10819 		/* XXX Configure actively LED after PHY reset */
   10820 		break;
   10821 	case WM_T_ICH8:
   10822 	case WM_T_ICH9:
   10823 	case WM_T_ICH10:
   10824 	case WM_T_PCH:
   10825 	case WM_T_PCH2:
   10826 	case WM_T_PCH_LPT:
   10827 	case WM_T_PCH_SPT:
   10828 	case WM_T_PCH_CNP:
   10829 		wm_phy_post_reset(sc);
   10830 		break;
   10831 	default:
   10832 		panic("%s: unknown type\n", __func__);
   10833 		break;
   10834 	}
   10835 }
   10836 
   10837 /*
   10838  * Set up sc_phytype and mii_{read|write}reg.
   10839  *
   10840  *  To identify PHY type, correct read/write function should be selected.
   10841  * To select correct read/write function, PCI ID or MAC type are required
   10842  * without accessing PHY registers.
   10843  *
   10844  *  On the first call of this function, PHY ID is not known yet. Check
   10845  * PCI ID or MAC type. The list of the PCI ID may not be perfect, so the
   10846  * result might be incorrect.
   10847  *
   10848  *  In the second call, PHY OUI and model is used to identify PHY type.
   10849  * It might not be perfect because of the lack of compared entry, but it
   10850  * would be better than the first call.
   10851  *
   10852  *  If the detected new result and previous assumption is different,
   10853  * a diagnostic message will be printed.
   10854  */
   10855 static void
   10856 wm_gmii_setup_phytype(struct wm_softc *sc, uint32_t phy_oui,
   10857     uint16_t phy_model)
   10858 {
   10859 	device_t dev = sc->sc_dev;
   10860 	struct mii_data *mii = &sc->sc_mii;
   10861 	uint16_t new_phytype = WMPHY_UNKNOWN;
   10862 	uint16_t doubt_phytype = WMPHY_UNKNOWN;
   10863 	mii_readreg_t new_readreg;
   10864 	mii_writereg_t new_writereg;
   10865 	bool dodiag = true;
   10866 
   10867 	DPRINTF(sc, WM_DEBUG_INIT, ("%s: %s called\n",
   10868 		device_xname(sc->sc_dev), __func__));
   10869 
   10870 	/*
   10871 	 * 1000BASE-T SFP uses SGMII and the first asumed PHY type is always
   10872 	 * incorrect. So don't print diag output when it's 2nd call.
   10873 	 */
   10874 	if ((sc->sc_sfptype != 0) && (phy_oui == 0) && (phy_model == 0))
   10875 		dodiag = false;
   10876 
   10877 	if (mii->mii_readreg == NULL) {
   10878 		/*
   10879 		 *  This is the first call of this function. For ICH and PCH
   10880 		 * variants, it's difficult to determine the PHY access method
   10881 		 * by sc_type, so use the PCI product ID for some devices.
   10882 		 */
   10883 
   10884 		switch (sc->sc_pcidevid) {
   10885 		case PCI_PRODUCT_INTEL_PCH_M_LM:
   10886 		case PCI_PRODUCT_INTEL_PCH_M_LC:
   10887 			/* 82577 */
   10888 			new_phytype = WMPHY_82577;
   10889 			break;
   10890 		case PCI_PRODUCT_INTEL_PCH_D_DM:
   10891 		case PCI_PRODUCT_INTEL_PCH_D_DC:
   10892 			/* 82578 */
   10893 			new_phytype = WMPHY_82578;
   10894 			break;
   10895 		case PCI_PRODUCT_INTEL_PCH2_LV_LM:
   10896 		case PCI_PRODUCT_INTEL_PCH2_LV_V:
   10897 			/* 82579 */
   10898 			new_phytype = WMPHY_82579;
   10899 			break;
   10900 		case PCI_PRODUCT_INTEL_82801H_82567V_3:
   10901 		case PCI_PRODUCT_INTEL_82801I_BM:
   10902 		case PCI_PRODUCT_INTEL_82801I_IGP_M_AMT: /* Not IGP but BM */
   10903 		case PCI_PRODUCT_INTEL_82801J_R_BM_LM:
   10904 		case PCI_PRODUCT_INTEL_82801J_R_BM_LF:
   10905 		case PCI_PRODUCT_INTEL_82801J_D_BM_LM:
   10906 		case PCI_PRODUCT_INTEL_82801J_D_BM_LF:
   10907 		case PCI_PRODUCT_INTEL_82801J_R_BM_V:
   10908 			/* ICH8, 9, 10 with 82567 */
   10909 			new_phytype = WMPHY_BM;
   10910 			break;
   10911 		default:
   10912 			break;
   10913 		}
   10914 	} else {
   10915 		/* It's not the first call. Use PHY OUI and model */
   10916 		switch (phy_oui) {
   10917 		case MII_OUI_ATTANSIC: /* atphy(4) */
   10918 			switch (phy_model) {
   10919 			case MII_MODEL_ATTANSIC_AR8021:
   10920 				new_phytype = WMPHY_82578;
   10921 				break;
   10922 			default:
   10923 				break;
   10924 			}
   10925 			break;
   10926 		case MII_OUI_xxMARVELL:
   10927 			switch (phy_model) {
   10928 			case MII_MODEL_xxMARVELL_I210:
   10929 				new_phytype = WMPHY_I210;
   10930 				break;
   10931 			case MII_MODEL_xxMARVELL_E1011:
   10932 			case MII_MODEL_xxMARVELL_E1000_3:
   10933 			case MII_MODEL_xxMARVELL_E1000_5:
   10934 			case MII_MODEL_xxMARVELL_E1112:
   10935 				new_phytype = WMPHY_M88;
   10936 				break;
   10937 			case MII_MODEL_xxMARVELL_E1149:
   10938 				new_phytype = WMPHY_BM;
   10939 				break;
   10940 			case MII_MODEL_xxMARVELL_E1111:
   10941 			case MII_MODEL_xxMARVELL_I347:
   10942 			case MII_MODEL_xxMARVELL_E1512:
   10943 			case MII_MODEL_xxMARVELL_E1340M:
   10944 			case MII_MODEL_xxMARVELL_E1543:
   10945 				new_phytype = WMPHY_M88;
   10946 				break;
   10947 			case MII_MODEL_xxMARVELL_I82563:
   10948 				new_phytype = WMPHY_GG82563;
   10949 				break;
   10950 			default:
   10951 				break;
   10952 			}
   10953 			break;
   10954 		case MII_OUI_INTEL:
   10955 			switch (phy_model) {
   10956 			case MII_MODEL_INTEL_I82577:
   10957 				new_phytype = WMPHY_82577;
   10958 				break;
   10959 			case MII_MODEL_INTEL_I82579:
   10960 				new_phytype = WMPHY_82579;
   10961 				break;
   10962 			case MII_MODEL_INTEL_I217:
   10963 				new_phytype = WMPHY_I217;
   10964 				break;
   10965 			case MII_MODEL_INTEL_I82580:
   10966 				new_phytype = WMPHY_82580;
   10967 				break;
   10968 			case MII_MODEL_INTEL_I350:
   10969 				new_phytype = WMPHY_I350;
   10970 				break;
   10971 			default:
   10972 				break;
   10973 			}
   10974 			break;
   10975 		case MII_OUI_yyINTEL:
   10976 			switch (phy_model) {
   10977 			case MII_MODEL_yyINTEL_I82562G:
   10978 			case MII_MODEL_yyINTEL_I82562EM:
   10979 			case MII_MODEL_yyINTEL_I82562ET:
   10980 				new_phytype = WMPHY_IFE;
   10981 				break;
   10982 			case MII_MODEL_yyINTEL_IGP01E1000:
   10983 				new_phytype = WMPHY_IGP;
   10984 				break;
   10985 			case MII_MODEL_yyINTEL_I82566:
   10986 				new_phytype = WMPHY_IGP_3;
   10987 				break;
   10988 			default:
   10989 				break;
   10990 			}
   10991 			break;
   10992 		default:
   10993 			break;
   10994 		}
   10995 
   10996 		if (dodiag) {
   10997 			if (new_phytype == WMPHY_UNKNOWN)
   10998 				aprint_verbose_dev(dev,
   10999 				    "%s: Unknown PHY model. OUI=%06x, "
   11000 				    "model=%04x\n", __func__, phy_oui,
   11001 				    phy_model);
   11002 
   11003 			if ((sc->sc_phytype != WMPHY_UNKNOWN)
   11004 			    && (sc->sc_phytype != new_phytype)) {
   11005 				aprint_error_dev(dev, "Previously assumed PHY "
   11006 				    "type(%u) was incorrect. PHY type from PHY"
   11007 				    "ID = %u\n", sc->sc_phytype, new_phytype);
   11008 			}
   11009 		}
   11010 	}
   11011 
   11012 	/* Next, use sc->sc_flags and sc->sc_type to set read/write funcs. */
   11013 	if (((sc->sc_flags & WM_F_SGMII) != 0) && !wm_sgmii_uses_mdio(sc)) {
   11014 		/* SGMII */
   11015 		new_readreg = wm_sgmii_readreg;
   11016 		new_writereg = wm_sgmii_writereg;
   11017 	} else if ((sc->sc_type == WM_T_82574) || (sc->sc_type == WM_T_82583)){
   11018 		/* BM2 (phyaddr == 1) */
   11019 		if ((sc->sc_phytype != WMPHY_UNKNOWN)
   11020 		    && (new_phytype != WMPHY_BM)
   11021 		    && (new_phytype != WMPHY_UNKNOWN))
   11022 			doubt_phytype = new_phytype;
   11023 		new_phytype = WMPHY_BM;
   11024 		new_readreg = wm_gmii_bm_readreg;
   11025 		new_writereg = wm_gmii_bm_writereg;
   11026 	} else if (sc->sc_type >= WM_T_PCH) {
   11027 		/* All PCH* use _hv_ */
   11028 		new_readreg = wm_gmii_hv_readreg;
   11029 		new_writereg = wm_gmii_hv_writereg;
   11030 	} else if (sc->sc_type >= WM_T_ICH8) {
   11031 		/* non-82567 ICH8, 9 and 10 */
   11032 		new_readreg = wm_gmii_i82544_readreg;
   11033 		new_writereg = wm_gmii_i82544_writereg;
   11034 	} else if (sc->sc_type >= WM_T_80003) {
   11035 		/* 80003 */
   11036 		if ((sc->sc_phytype != WMPHY_UNKNOWN)
   11037 		    && (new_phytype != WMPHY_GG82563)
   11038 		    && (new_phytype != WMPHY_UNKNOWN))
   11039 			doubt_phytype = new_phytype;
   11040 		new_phytype = WMPHY_GG82563;
   11041 		new_readreg = wm_gmii_i80003_readreg;
   11042 		new_writereg = wm_gmii_i80003_writereg;
   11043 	} else if (sc->sc_type >= WM_T_I210) {
   11044 		/* I210 and I211 */
   11045 		if ((sc->sc_phytype != WMPHY_UNKNOWN)
   11046 		    && (new_phytype != WMPHY_I210)
   11047 		    && (new_phytype != WMPHY_UNKNOWN))
   11048 			doubt_phytype = new_phytype;
   11049 		new_phytype = WMPHY_I210;
   11050 		new_readreg = wm_gmii_gs40g_readreg;
   11051 		new_writereg = wm_gmii_gs40g_writereg;
   11052 	} else if (sc->sc_type >= WM_T_82580) {
   11053 		/* 82580, I350 and I354 */
   11054 		new_readreg = wm_gmii_82580_readreg;
   11055 		new_writereg = wm_gmii_82580_writereg;
   11056 	} else if (sc->sc_type >= WM_T_82544) {
   11057 		/* 82544, 0, [56], [17], 8257[1234] and 82583 */
   11058 		new_readreg = wm_gmii_i82544_readreg;
   11059 		new_writereg = wm_gmii_i82544_writereg;
   11060 	} else {
   11061 		new_readreg = wm_gmii_i82543_readreg;
   11062 		new_writereg = wm_gmii_i82543_writereg;
   11063 	}
   11064 
   11065 	if (new_phytype == WMPHY_BM) {
   11066 		/* All BM use _bm_ */
   11067 		new_readreg = wm_gmii_bm_readreg;
   11068 		new_writereg = wm_gmii_bm_writereg;
   11069 	}
   11070 	if ((sc->sc_type >= WM_T_PCH) && (sc->sc_type <= WM_T_PCH_CNP)) {
   11071 		/* All PCH* use _hv_ */
   11072 		new_readreg = wm_gmii_hv_readreg;
   11073 		new_writereg = wm_gmii_hv_writereg;
   11074 	}
   11075 
   11076 	/* Diag output */
   11077 	if (dodiag) {
   11078 		if (doubt_phytype != WMPHY_UNKNOWN)
   11079 			aprint_error_dev(dev, "Assumed new PHY type was "
   11080 			    "incorrect. old = %u, new = %u\n", sc->sc_phytype,
   11081 			    new_phytype);
   11082 		else if ((sc->sc_phytype != WMPHY_UNKNOWN)
   11083 		    && (sc->sc_phytype != new_phytype))
   11084 			aprint_error_dev(dev, "Previously assumed PHY type(%u)"
   11085 			    "was incorrect. New PHY type = %u\n",
   11086 			    sc->sc_phytype, new_phytype);
   11087 
   11088 		if ((mii->mii_readreg != NULL) &&
   11089 		    (new_phytype == WMPHY_UNKNOWN))
   11090 			aprint_error_dev(dev, "PHY type is still unknown.\n");
   11091 
   11092 		if ((mii->mii_readreg != NULL) &&
   11093 		    (mii->mii_readreg != new_readreg))
   11094 			aprint_error_dev(dev, "Previously assumed PHY "
   11095 			    "read/write function was incorrect.\n");
   11096 	}
   11097 
   11098 	/* Update now */
   11099 	sc->sc_phytype = new_phytype;
   11100 	mii->mii_readreg = new_readreg;
   11101 	mii->mii_writereg = new_writereg;
   11102 	if (new_readreg == wm_gmii_hv_readreg) {
   11103 		sc->phy.readreg_locked = wm_gmii_hv_readreg_locked;
   11104 		sc->phy.writereg_locked = wm_gmii_hv_writereg_locked;
   11105 	} else if (new_readreg == wm_sgmii_readreg) {
   11106 		sc->phy.readreg_locked = wm_sgmii_readreg_locked;
   11107 		sc->phy.writereg_locked = wm_sgmii_writereg_locked;
   11108 	} else if (new_readreg == wm_gmii_i82544_readreg) {
   11109 		sc->phy.readreg_locked = wm_gmii_i82544_readreg_locked;
   11110 		sc->phy.writereg_locked = wm_gmii_i82544_writereg_locked;
   11111 	}
   11112 }
   11113 
   11114 /*
   11115  * wm_get_phy_id_82575:
   11116  *
   11117  * Return PHY ID. Return -1 if it failed.
   11118  */
   11119 static int
   11120 wm_get_phy_id_82575(struct wm_softc *sc)
   11121 {
   11122 	uint32_t reg;
   11123 	int phyid = -1;
   11124 
   11125 	/* XXX */
   11126 	if ((sc->sc_flags & WM_F_SGMII) == 0)
   11127 		return -1;
   11128 
   11129 	if (wm_sgmii_uses_mdio(sc)) {
   11130 		switch (sc->sc_type) {
   11131 		case WM_T_82575:
   11132 		case WM_T_82576:
   11133 			reg = CSR_READ(sc, WMREG_MDIC);
   11134 			phyid = (reg & MDIC_PHY_MASK) >> MDIC_PHY_SHIFT;
   11135 			break;
   11136 		case WM_T_82580:
   11137 		case WM_T_I350:
   11138 		case WM_T_I354:
   11139 		case WM_T_I210:
   11140 		case WM_T_I211:
   11141 			reg = CSR_READ(sc, WMREG_MDICNFG);
   11142 			phyid = (reg & MDICNFG_PHY_MASK) >> MDICNFG_PHY_SHIFT;
   11143 			break;
   11144 		default:
   11145 			return -1;
   11146 		}
   11147 	}
   11148 
   11149 	return phyid;
   11150 }
   11151 
   11152 /*
   11153  * wm_gmii_mediainit:
   11154  *
   11155  *	Initialize media for use on 1000BASE-T devices.
   11156  */
   11157 static void
   11158 wm_gmii_mediainit(struct wm_softc *sc, pci_product_id_t prodid)
   11159 {
   11160 	device_t dev = sc->sc_dev;
   11161 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   11162 	struct mii_data *mii = &sc->sc_mii;
   11163 
   11164 	DPRINTF(sc, WM_DEBUG_GMII, ("%s: %s called\n",
   11165 		device_xname(sc->sc_dev), __func__));
   11166 
   11167 	/* We have GMII. */
   11168 	sc->sc_flags |= WM_F_HAS_MII;
   11169 
   11170 	if (sc->sc_type == WM_T_80003)
   11171 		sc->sc_tipg =  TIPG_1000T_80003_DFLT;
   11172 	else
   11173 		sc->sc_tipg = TIPG_1000T_DFLT;
   11174 
   11175 	/*
   11176 	 * Let the chip set speed/duplex on its own based on
   11177 	 * signals from the PHY.
   11178 	 * XXXbouyer - I'm not sure this is right for the 80003,
   11179 	 * the em driver only sets CTRL_SLU here - but it seems to work.
   11180 	 */
   11181 	sc->sc_ctrl |= CTRL_SLU;
   11182 	CSR_WRITE(sc, WMREG_CTRL, sc->sc_ctrl);
   11183 
   11184 	/* Initialize our media structures and probe the GMII. */
   11185 	mii->mii_ifp = ifp;
   11186 
   11187 	mii->mii_statchg = wm_gmii_statchg;
   11188 
   11189 	/* get PHY control from SMBus to PCIe */
   11190 	if ((sc->sc_type == WM_T_PCH) || (sc->sc_type == WM_T_PCH2)
   11191 	    || (sc->sc_type == WM_T_PCH_LPT) || (sc->sc_type == WM_T_PCH_SPT)
   11192 	    || (sc->sc_type == WM_T_PCH_CNP))
   11193 		wm_init_phy_workarounds_pchlan(sc);
   11194 
   11195 	wm_gmii_reset(sc);
   11196 
   11197 	sc->sc_ethercom.ec_mii = &sc->sc_mii;
   11198 	ifmedia_init_with_lock(&mii->mii_media, IFM_IMASK, wm_gmii_mediachange,
   11199 	    wm_gmii_mediastatus, sc->sc_core_lock);
   11200 
   11201 	/* Setup internal SGMII PHY for SFP */
   11202 	wm_sgmii_sfp_preconfig(sc);
   11203 
   11204 	if ((sc->sc_type == WM_T_82575) || (sc->sc_type == WM_T_82576)
   11205 	    || (sc->sc_type == WM_T_82580)
   11206 	    || (sc->sc_type == WM_T_I350) || (sc->sc_type == WM_T_I354)
   11207 	    || (sc->sc_type == WM_T_I210) || (sc->sc_type == WM_T_I211)) {
   11208 		if ((sc->sc_flags & WM_F_SGMII) == 0) {
   11209 			/* Attach only one port */
   11210 			mii_attach(sc->sc_dev, &sc->sc_mii, 0xffffffff, 1,
   11211 			    MII_OFFSET_ANY, MIIF_DOPAUSE);
   11212 		} else {
   11213 			int i, id;
   11214 			uint32_t ctrl_ext;
   11215 
   11216 			id = wm_get_phy_id_82575(sc);
   11217 			if (id != -1) {
   11218 				mii_attach(sc->sc_dev, &sc->sc_mii, 0xffffffff,
   11219 				    id, MII_OFFSET_ANY, MIIF_DOPAUSE);
   11220 			}
   11221 			if ((id == -1)
   11222 			    || (LIST_FIRST(&mii->mii_phys) == NULL)) {
   11223 				/* Power on sgmii phy if it is disabled */
   11224 				ctrl_ext = CSR_READ(sc, WMREG_CTRL_EXT);
   11225 				CSR_WRITE(sc, WMREG_CTRL_EXT,
   11226 				    ctrl_ext &~ CTRL_EXT_SWDPIN(3));
   11227 				CSR_WRITE_FLUSH(sc);
   11228 				delay(300*1000); /* XXX too long */
   11229 
   11230 				/*
   11231 				 * From 1 to 8.
   11232 				 *
   11233 				 * I2C access fails with I2C register's ERROR
   11234 				 * bit set, so prevent error message while
   11235 				 * scanning.
   11236 				 */
   11237 				sc->phy.no_errprint = true;
   11238 				for (i = 1; i < 8; i++)
   11239 					mii_attach(sc->sc_dev, &sc->sc_mii,
   11240 					    0xffffffff, i, MII_OFFSET_ANY,
   11241 					    MIIF_DOPAUSE);
   11242 				sc->phy.no_errprint = false;
   11243 
   11244 				/* Restore previous sfp cage power state */
   11245 				CSR_WRITE(sc, WMREG_CTRL_EXT, ctrl_ext);
   11246 			}
   11247 		}
   11248 	} else
   11249 		mii_attach(sc->sc_dev, &sc->sc_mii, 0xffffffff, MII_PHY_ANY,
   11250 		    MII_OFFSET_ANY, MIIF_DOPAUSE);
   11251 
   11252 	/*
   11253 	 * If the MAC is PCH2 or PCH_LPT and failed to detect MII PHY, call
   11254 	 * wm_set_mdio_slow_mode_hv() for a workaround and retry.
   11255 	 */
   11256 	if (((sc->sc_type == WM_T_PCH2) || (sc->sc_type == WM_T_PCH_LPT)
   11257 		|| (sc->sc_type == WM_T_PCH_SPT)
   11258 		|| (sc->sc_type == WM_T_PCH_CNP))
   11259 	    && (LIST_FIRST(&mii->mii_phys) == NULL)) {
   11260 		wm_set_mdio_slow_mode_hv(sc);
   11261 		mii_attach(sc->sc_dev, &sc->sc_mii, 0xffffffff, MII_PHY_ANY,
   11262 		    MII_OFFSET_ANY, MIIF_DOPAUSE);
   11263 	}
   11264 
   11265 	/*
   11266 	 * (For ICH8 variants)
   11267 	 * If PHY detection failed, use BM's r/w function and retry.
   11268 	 */
   11269 	if (LIST_FIRST(&mii->mii_phys) == NULL) {
   11270 		/* if failed, retry with *_bm_* */
   11271 		aprint_verbose_dev(dev, "Assumed PHY access function "
   11272 		    "(type = %d) might be incorrect. Use BM and retry.\n",
   11273 		    sc->sc_phytype);
   11274 		sc->sc_phytype = WMPHY_BM;
   11275 		mii->mii_readreg = wm_gmii_bm_readreg;
   11276 		mii->mii_writereg = wm_gmii_bm_writereg;
   11277 
   11278 		mii_attach(sc->sc_dev, &sc->sc_mii, 0xffffffff, MII_PHY_ANY,
   11279 		    MII_OFFSET_ANY, MIIF_DOPAUSE);
   11280 	}
   11281 
   11282 	if (LIST_FIRST(&mii->mii_phys) == NULL) {
   11283 		/* Any PHY wasn't found */
   11284 		ifmedia_add(&mii->mii_media, IFM_ETHER | IFM_NONE, 0, NULL);
   11285 		ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_NONE);
   11286 		sc->sc_phytype = WMPHY_NONE;
   11287 	} else {
   11288 		struct mii_softc *child = LIST_FIRST(&mii->mii_phys);
   11289 
   11290 		/*
   11291 		 * PHY found! Check PHY type again by the second call of
   11292 		 * wm_gmii_setup_phytype.
   11293 		 */
   11294 		wm_gmii_setup_phytype(sc, child->mii_mpd_oui,
   11295 		    child->mii_mpd_model);
   11296 
   11297 		ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO);
   11298 	}
   11299 }
   11300 
   11301 /*
   11302  * wm_gmii_mediachange:	[ifmedia interface function]
   11303  *
   11304  *	Set hardware to newly-selected media on a 1000BASE-T device.
   11305  */
   11306 static int
   11307 wm_gmii_mediachange(struct ifnet *ifp)
   11308 {
   11309 	struct wm_softc *sc = ifp->if_softc;
   11310 	struct ifmedia_entry *ife = sc->sc_mii.mii_media.ifm_cur;
   11311 	uint32_t reg;
   11312 	int rc;
   11313 
   11314 	DPRINTF(sc, WM_DEBUG_GMII, ("%s: %s called\n",
   11315 		device_xname(sc->sc_dev), __func__));
   11316 
   11317 	KASSERT(WM_CORE_LOCKED(sc));
   11318 
   11319 	if ((sc->sc_if_flags & IFF_UP) == 0)
   11320 		return 0;
   11321 
   11322 	/* XXX Not for I354? FreeBSD's e1000_82575.c doesn't include it */
   11323 	if ((sc->sc_type == WM_T_82580)
   11324 	    || (sc->sc_type == WM_T_I350) || (sc->sc_type == WM_T_I210)
   11325 	    || (sc->sc_type == WM_T_I211)) {
   11326 		reg = CSR_READ(sc, WMREG_PHPM);
   11327 		reg &= ~PHPM_GO_LINK_D;
   11328 		CSR_WRITE(sc, WMREG_PHPM, reg);
   11329 	}
   11330 
   11331 	/* Disable D0 LPLU. */
   11332 	wm_lplu_d0_disable(sc);
   11333 
   11334 	sc->sc_ctrl &= ~(CTRL_SPEED_MASK | CTRL_FD);
   11335 	sc->sc_ctrl |= CTRL_SLU;
   11336 	if ((IFM_SUBTYPE(ife->ifm_media) == IFM_AUTO)
   11337 	    || (sc->sc_type > WM_T_82543)) {
   11338 		sc->sc_ctrl &= ~(CTRL_FRCSPD | CTRL_FRCFDX);
   11339 	} else {
   11340 		sc->sc_ctrl &= ~CTRL_ASDE;
   11341 		sc->sc_ctrl |= CTRL_FRCSPD | CTRL_FRCFDX;
   11342 		if (ife->ifm_media & IFM_FDX)
   11343 			sc->sc_ctrl |= CTRL_FD;
   11344 		switch (IFM_SUBTYPE(ife->ifm_media)) {
   11345 		case IFM_10_T:
   11346 			sc->sc_ctrl |= CTRL_SPEED_10;
   11347 			break;
   11348 		case IFM_100_TX:
   11349 			sc->sc_ctrl |= CTRL_SPEED_100;
   11350 			break;
   11351 		case IFM_1000_T:
   11352 			sc->sc_ctrl |= CTRL_SPEED_1000;
   11353 			break;
   11354 		case IFM_NONE:
   11355 			/* There is no specific setting for IFM_NONE */
   11356 			break;
   11357 		default:
   11358 			panic("wm_gmii_mediachange: bad media 0x%x",
   11359 			    ife->ifm_media);
   11360 		}
   11361 	}
   11362 	CSR_WRITE(sc, WMREG_CTRL, sc->sc_ctrl);
   11363 	CSR_WRITE_FLUSH(sc);
   11364 
   11365 	if ((sc->sc_type >= WM_T_82575) && (sc->sc_type <= WM_T_I211))
   11366 		wm_serdes_mediachange(ifp);
   11367 
   11368 	if (sc->sc_type <= WM_T_82543)
   11369 		wm_gmii_reset(sc);
   11370 	else if ((sc->sc_type >= WM_T_82575) && (sc->sc_type <= WM_T_I211)
   11371 	    && ((sc->sc_flags & WM_F_SGMII) != 0)) {
   11372 		/* allow time for SFP cage time to power up phy */
   11373 		delay(300 * 1000);
   11374 		wm_gmii_reset(sc);
   11375 	}
   11376 
   11377 	if ((rc = mii_mediachg(&sc->sc_mii)) == ENXIO)
   11378 		return 0;
   11379 	return rc;
   11380 }
   11381 
   11382 /*
   11383  * wm_gmii_mediastatus:	[ifmedia interface function]
   11384  *
   11385  *	Get the current interface media status on a 1000BASE-T device.
   11386  */
   11387 static void
   11388 wm_gmii_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr)
   11389 {
   11390 	struct wm_softc *sc = ifp->if_softc;
   11391 
   11392 	KASSERT(WM_CORE_LOCKED(sc));
   11393 
   11394 	ether_mediastatus(ifp, ifmr);
   11395 	ifmr->ifm_active = (ifmr->ifm_active & ~IFM_ETH_FMASK)
   11396 	    | sc->sc_flowflags;
   11397 }
   11398 
   11399 #define	MDI_IO		CTRL_SWDPIN(2)
   11400 #define	MDI_DIR		CTRL_SWDPIO(2)	/* host -> PHY */
   11401 #define	MDI_CLK		CTRL_SWDPIN(3)
   11402 
   11403 static void
   11404 wm_i82543_mii_sendbits(struct wm_softc *sc, uint32_t data, int nbits)
   11405 {
   11406 	uint32_t i, v;
   11407 
   11408 	v = CSR_READ(sc, WMREG_CTRL);
   11409 	v &= ~(MDI_IO | MDI_CLK | (CTRL_SWDPIO_MASK << CTRL_SWDPIO_SHIFT));
   11410 	v |= MDI_DIR | CTRL_SWDPIO(3);
   11411 
   11412 	for (i = __BIT(nbits - 1); i != 0; i >>= 1) {
   11413 		if (data & i)
   11414 			v |= MDI_IO;
   11415 		else
   11416 			v &= ~MDI_IO;
   11417 		CSR_WRITE(sc, WMREG_CTRL, v);
   11418 		CSR_WRITE_FLUSH(sc);
   11419 		delay(10);
   11420 		CSR_WRITE(sc, WMREG_CTRL, v | MDI_CLK);
   11421 		CSR_WRITE_FLUSH(sc);
   11422 		delay(10);
   11423 		CSR_WRITE(sc, WMREG_CTRL, v);
   11424 		CSR_WRITE_FLUSH(sc);
   11425 		delay(10);
   11426 	}
   11427 }
   11428 
   11429 static uint16_t
   11430 wm_i82543_mii_recvbits(struct wm_softc *sc)
   11431 {
   11432 	uint32_t v, i;
   11433 	uint16_t data = 0;
   11434 
   11435 	v = CSR_READ(sc, WMREG_CTRL);
   11436 	v &= ~(MDI_IO | MDI_CLK | (CTRL_SWDPIO_MASK << CTRL_SWDPIO_SHIFT));
   11437 	v |= CTRL_SWDPIO(3);
   11438 
   11439 	CSR_WRITE(sc, WMREG_CTRL, v);
   11440 	CSR_WRITE_FLUSH(sc);
   11441 	delay(10);
   11442 	CSR_WRITE(sc, WMREG_CTRL, v | MDI_CLK);
   11443 	CSR_WRITE_FLUSH(sc);
   11444 	delay(10);
   11445 	CSR_WRITE(sc, WMREG_CTRL, v);
   11446 	CSR_WRITE_FLUSH(sc);
   11447 	delay(10);
   11448 
   11449 	for (i = 0; i < 16; i++) {
   11450 		data <<= 1;
   11451 		CSR_WRITE(sc, WMREG_CTRL, v | MDI_CLK);
   11452 		CSR_WRITE_FLUSH(sc);
   11453 		delay(10);
   11454 		if (CSR_READ(sc, WMREG_CTRL) & MDI_IO)
   11455 			data |= 1;
   11456 		CSR_WRITE(sc, WMREG_CTRL, v);
   11457 		CSR_WRITE_FLUSH(sc);
   11458 		delay(10);
   11459 	}
   11460 
   11461 	CSR_WRITE(sc, WMREG_CTRL, v | MDI_CLK);
   11462 	CSR_WRITE_FLUSH(sc);
   11463 	delay(10);
   11464 	CSR_WRITE(sc, WMREG_CTRL, v);
   11465 	CSR_WRITE_FLUSH(sc);
   11466 	delay(10);
   11467 
   11468 	return data;
   11469 }
   11470 
   11471 #undef MDI_IO
   11472 #undef MDI_DIR
   11473 #undef MDI_CLK
   11474 
   11475 /*
   11476  * wm_gmii_i82543_readreg:	[mii interface function]
   11477  *
   11478  *	Read a PHY register on the GMII (i82543 version).
   11479  */
   11480 static int
   11481 wm_gmii_i82543_readreg(device_t dev, int phy, int reg, uint16_t *val)
   11482 {
   11483 	struct wm_softc *sc = device_private(dev);
   11484 
   11485 	wm_i82543_mii_sendbits(sc, 0xffffffffU, 32);
   11486 	wm_i82543_mii_sendbits(sc, reg | (phy << 5) |
   11487 	    (MII_COMMAND_READ << 10) | (MII_COMMAND_START << 12), 14);
   11488 	*val = wm_i82543_mii_recvbits(sc) & 0xffff;
   11489 
   11490 	DPRINTF(sc, WM_DEBUG_GMII,
   11491 	    ("%s: GMII: read phy %d reg %d -> 0x%04hx\n",
   11492 		device_xname(dev), phy, reg, *val));
   11493 
   11494 	return 0;
   11495 }
   11496 
   11497 /*
   11498  * wm_gmii_i82543_writereg:	[mii interface function]
   11499  *
   11500  *	Write a PHY register on the GMII (i82543 version).
   11501  */
   11502 static int
   11503 wm_gmii_i82543_writereg(device_t dev, int phy, int reg, uint16_t val)
   11504 {
   11505 	struct wm_softc *sc = device_private(dev);
   11506 
   11507 	wm_i82543_mii_sendbits(sc, 0xffffffffU, 32);
   11508 	wm_i82543_mii_sendbits(sc, val | (MII_COMMAND_ACK << 16) |
   11509 	    (reg << 18) | (phy << 23) | (MII_COMMAND_WRITE << 28) |
   11510 	    (MII_COMMAND_START << 30), 32);
   11511 
   11512 	return 0;
   11513 }
   11514 
   11515 /*
   11516  * wm_gmii_mdic_readreg:	[mii interface function]
   11517  *
   11518  *	Read a PHY register on the GMII.
   11519  */
   11520 static int
   11521 wm_gmii_mdic_readreg(device_t dev, int phy, int reg, uint16_t *val)
   11522 {
   11523 	struct wm_softc *sc = device_private(dev);
   11524 	uint32_t mdic = 0;
   11525 	int i;
   11526 
   11527 	if ((sc->sc_phytype != WMPHY_82579) && (sc->sc_phytype != WMPHY_I217)
   11528 	    && (reg > MII_ADDRMASK)) {
   11529 		device_printf(dev, "%s: PHYTYPE = %d, addr 0x%x > 0x1f\n",
   11530 		    __func__, sc->sc_phytype, reg);
   11531 		reg &= MII_ADDRMASK;
   11532 	}
   11533 
   11534 	CSR_WRITE(sc, WMREG_MDIC, MDIC_OP_READ | MDIC_PHYADD(phy) |
   11535 	    MDIC_REGADD(reg));
   11536 
   11537 	for (i = 0; i < WM_GEN_POLL_TIMEOUT * 3; i++) {
   11538 		delay(50);
   11539 		mdic = CSR_READ(sc, WMREG_MDIC);
   11540 		if (mdic & MDIC_READY)
   11541 			break;
   11542 	}
   11543 
   11544 	if ((mdic & MDIC_READY) == 0) {
   11545 		DPRINTF(sc, WM_DEBUG_GMII,
   11546 		    ("%s: MDIC read timed out: phy %d reg %d\n",
   11547 			device_xname(dev), phy, reg));
   11548 		return ETIMEDOUT;
   11549 	} else if (mdic & MDIC_E) {
   11550 		/* This is normal if no PHY is present. */
   11551 		DPRINTF(sc, WM_DEBUG_GMII,
   11552 		    ("%s: MDIC read error: phy %d reg %d\n",
   11553 			device_xname(sc->sc_dev), phy, reg));
   11554 		return -1;
   11555 	} else
   11556 		*val = MDIC_DATA(mdic);
   11557 
   11558 	/*
   11559 	 * Allow some time after each MDIC transaction to avoid
   11560 	 * reading duplicate data in the next MDIC transaction.
   11561 	 */
   11562 	if (sc->sc_type == WM_T_PCH2)
   11563 		delay(100);
   11564 
   11565 	return 0;
   11566 }
   11567 
   11568 /*
   11569  * wm_gmii_mdic_writereg:	[mii interface function]
   11570  *
   11571  *	Write a PHY register on the GMII.
   11572  */
   11573 static int
   11574 wm_gmii_mdic_writereg(device_t dev, int phy, int reg, uint16_t val)
   11575 {
   11576 	struct wm_softc *sc = device_private(dev);
   11577 	uint32_t mdic = 0;
   11578 	int i;
   11579 
   11580 	if ((sc->sc_phytype != WMPHY_82579) && (sc->sc_phytype != WMPHY_I217)
   11581 	    && (reg > MII_ADDRMASK)) {
   11582 		device_printf(dev, "%s: PHYTYPE = %d, addr 0x%x > 0x1f\n",
   11583 		    __func__, sc->sc_phytype, reg);
   11584 		reg &= MII_ADDRMASK;
   11585 	}
   11586 
   11587 	CSR_WRITE(sc, WMREG_MDIC, MDIC_OP_WRITE | MDIC_PHYADD(phy) |
   11588 	    MDIC_REGADD(reg) | MDIC_DATA(val));
   11589 
   11590 	for (i = 0; i < WM_GEN_POLL_TIMEOUT * 3; i++) {
   11591 		delay(50);
   11592 		mdic = CSR_READ(sc, WMREG_MDIC);
   11593 		if (mdic & MDIC_READY)
   11594 			break;
   11595 	}
   11596 
   11597 	if ((mdic & MDIC_READY) == 0) {
   11598 		DPRINTF(sc, WM_DEBUG_GMII,
   11599 		    ("%s: MDIC write timed out: phy %d reg %d\n",
   11600 			device_xname(dev), phy, reg));
   11601 		return ETIMEDOUT;
   11602 	} else if (mdic & MDIC_E) {
   11603 		DPRINTF(sc, WM_DEBUG_GMII,
   11604 		    ("%s: MDIC write error: phy %d reg %d\n",
   11605 			device_xname(dev), phy, reg));
   11606 		return -1;
   11607 	}
   11608 
   11609 	/*
   11610 	 * Allow some time after each MDIC transaction to avoid
   11611 	 * reading duplicate data in the next MDIC transaction.
   11612 	 */
   11613 	if (sc->sc_type == WM_T_PCH2)
   11614 		delay(100);
   11615 
   11616 	return 0;
   11617 }
   11618 
   11619 /*
   11620  * wm_gmii_i82544_readreg:	[mii interface function]
   11621  *
   11622  *	Read a PHY register on the GMII.
   11623  */
   11624 static int
   11625 wm_gmii_i82544_readreg(device_t dev, int phy, int reg, uint16_t *val)
   11626 {
   11627 	struct wm_softc *sc = device_private(dev);
   11628 	int rv;
   11629 
   11630 	if (sc->phy.acquire(sc)) {
   11631 		device_printf(dev, "%s: failed to get semaphore\n", __func__);
   11632 		return -1;
   11633 	}
   11634 
   11635 	rv = wm_gmii_i82544_readreg_locked(dev, phy, reg, val);
   11636 
   11637 	sc->phy.release(sc);
   11638 
   11639 	return rv;
   11640 }
   11641 
   11642 static int
   11643 wm_gmii_i82544_readreg_locked(device_t dev, int phy, int reg, uint16_t *val)
   11644 {
   11645 	struct wm_softc *sc = device_private(dev);
   11646 	int rv;
   11647 
   11648 	if (reg > BME1000_MAX_MULTI_PAGE_REG) {
   11649 		switch (sc->sc_phytype) {
   11650 		case WMPHY_IGP:
   11651 		case WMPHY_IGP_2:
   11652 		case WMPHY_IGP_3:
   11653 			rv = wm_gmii_mdic_writereg(dev, phy,
   11654 			    IGPHY_PAGE_SELECT, reg);
   11655 			if (rv != 0)
   11656 				return rv;
   11657 			break;
   11658 		default:
   11659 #ifdef WM_DEBUG
   11660 			device_printf(dev, "%s: PHYTYPE = 0x%x, addr = %02x\n",
   11661 			    __func__, sc->sc_phytype, reg);
   11662 #endif
   11663 			break;
   11664 		}
   11665 	}
   11666 
   11667 	return wm_gmii_mdic_readreg(dev, phy, reg & MII_ADDRMASK, val);
   11668 }
   11669 
   11670 /*
   11671  * wm_gmii_i82544_writereg:	[mii interface function]
   11672  *
   11673  *	Write a PHY register on the GMII.
   11674  */
   11675 static int
   11676 wm_gmii_i82544_writereg(device_t dev, int phy, int reg, uint16_t val)
   11677 {
   11678 	struct wm_softc *sc = device_private(dev);
   11679 	int rv;
   11680 
   11681 	if (sc->phy.acquire(sc)) {
   11682 		device_printf(dev, "%s: failed to get semaphore\n", __func__);
   11683 		return -1;
   11684 	}
   11685 
   11686 	rv = wm_gmii_i82544_writereg_locked(dev, phy, reg & MII_ADDRMASK, val);
   11687 	sc->phy.release(sc);
   11688 
   11689 	return rv;
   11690 }
   11691 
   11692 static int
   11693 wm_gmii_i82544_writereg_locked(device_t dev, int phy, int reg, uint16_t val)
   11694 {
   11695 	struct wm_softc *sc = device_private(dev);
   11696 	int rv;
   11697 
   11698 	if (reg > BME1000_MAX_MULTI_PAGE_REG) {
   11699 		switch (sc->sc_phytype) {
   11700 		case WMPHY_IGP:
   11701 		case WMPHY_IGP_2:
   11702 		case WMPHY_IGP_3:
   11703 			rv = wm_gmii_mdic_writereg(dev, phy,
   11704 			    IGPHY_PAGE_SELECT, reg);
   11705 			if (rv != 0)
   11706 				return rv;
   11707 			break;
   11708 		default:
   11709 #ifdef WM_DEBUG
   11710 			device_printf(dev, "%s: PHYTYPE == 0x%x, addr = %02x",
   11711 			    __func__, sc->sc_phytype, reg);
   11712 #endif
   11713 			break;
   11714 		}
   11715 	}
   11716 
   11717 	return wm_gmii_mdic_writereg(dev, phy, reg & MII_ADDRMASK, val);
   11718 }
   11719 
   11720 /*
   11721  * wm_gmii_i80003_readreg:	[mii interface function]
   11722  *
   11723  *	Read a PHY register on the kumeran
   11724  * This could be handled by the PHY layer if we didn't have to lock the
   11725  * resource ...
   11726  */
   11727 static int
   11728 wm_gmii_i80003_readreg(device_t dev, int phy, int reg, uint16_t *val)
   11729 {
   11730 	struct wm_softc *sc = device_private(dev);
   11731 	int page_select;
   11732 	uint16_t temp, temp2;
   11733 	int rv = 0;
   11734 
   11735 	if (phy != 1) /* Only one PHY on kumeran bus */
   11736 		return -1;
   11737 
   11738 	if (sc->phy.acquire(sc)) {
   11739 		device_printf(dev, "%s: failed to get semaphore\n", __func__);
   11740 		return -1;
   11741 	}
   11742 
   11743 	if ((reg & MII_ADDRMASK) < GG82563_MIN_ALT_REG)
   11744 		page_select = GG82563_PHY_PAGE_SELECT;
   11745 	else {
   11746 		/*
   11747 		 * Use Alternative Page Select register to access registers
   11748 		 * 30 and 31.
   11749 		 */
   11750 		page_select = GG82563_PHY_PAGE_SELECT_ALT;
   11751 	}
   11752 	temp = reg >> GG82563_PAGE_SHIFT;
   11753 	if ((rv = wm_gmii_mdic_writereg(dev, phy, page_select, temp)) != 0)
   11754 		goto out;
   11755 
   11756 	if ((sc->sc_flags & WM_F_80003_MDIC_WA) != 0) {
   11757 		/*
   11758 		 * Wait more 200us for a bug of the ready bit in the MDIC
   11759 		 * register.
   11760 		 */
   11761 		delay(200);
   11762 		rv = wm_gmii_mdic_readreg(dev, phy, page_select, &temp2);
   11763 		if ((rv != 0) || (temp2 != temp)) {
   11764 			device_printf(dev, "%s failed\n", __func__);
   11765 			rv = -1;
   11766 			goto out;
   11767 		}
   11768 		delay(200);
   11769 		rv = wm_gmii_mdic_readreg(dev, phy, reg & MII_ADDRMASK, val);
   11770 		delay(200);
   11771 	} else
   11772 		rv = wm_gmii_mdic_readreg(dev, phy, reg & MII_ADDRMASK, val);
   11773 
   11774 out:
   11775 	sc->phy.release(sc);
   11776 	return rv;
   11777 }
   11778 
   11779 /*
   11780  * wm_gmii_i80003_writereg:	[mii interface function]
   11781  *
   11782  *	Write a PHY register on the kumeran.
   11783  * This could be handled by the PHY layer if we didn't have to lock the
   11784  * resource ...
   11785  */
   11786 static int
   11787 wm_gmii_i80003_writereg(device_t dev, int phy, int reg, uint16_t val)
   11788 {
   11789 	struct wm_softc *sc = device_private(dev);
   11790 	int page_select, rv;
   11791 	uint16_t temp, temp2;
   11792 
   11793 	if (phy != 1) /* Only one PHY on kumeran bus */
   11794 		return -1;
   11795 
   11796 	if (sc->phy.acquire(sc)) {
   11797 		device_printf(dev, "%s: failed to get semaphore\n", __func__);
   11798 		return -1;
   11799 	}
   11800 
   11801 	if ((reg & MII_ADDRMASK) < GG82563_MIN_ALT_REG)
   11802 		page_select = GG82563_PHY_PAGE_SELECT;
   11803 	else {
   11804 		/*
   11805 		 * Use Alternative Page Select register to access registers
   11806 		 * 30 and 31.
   11807 		 */
   11808 		page_select = GG82563_PHY_PAGE_SELECT_ALT;
   11809 	}
   11810 	temp = (uint16_t)reg >> GG82563_PAGE_SHIFT;
   11811 	if ((rv = wm_gmii_mdic_writereg(dev, phy, page_select, temp)) != 0)
   11812 		goto out;
   11813 
   11814 	if ((sc->sc_flags & WM_F_80003_MDIC_WA) != 0) {
   11815 		/*
   11816 		 * Wait more 200us for a bug of the ready bit in the MDIC
   11817 		 * register.
   11818 		 */
   11819 		delay(200);
   11820 		rv = wm_gmii_mdic_readreg(dev, phy, page_select, &temp2);
   11821 		if ((rv != 0) || (temp2 != temp)) {
   11822 			device_printf(dev, "%s failed\n", __func__);
   11823 			rv = -1;
   11824 			goto out;
   11825 		}
   11826 		delay(200);
   11827 		rv = wm_gmii_mdic_writereg(dev, phy, reg & MII_ADDRMASK, val);
   11828 		delay(200);
   11829 	} else
   11830 		rv = wm_gmii_mdic_writereg(dev, phy, reg & MII_ADDRMASK, val);
   11831 
   11832 out:
   11833 	sc->phy.release(sc);
   11834 	return rv;
   11835 }
   11836 
   11837 /*
   11838  * wm_gmii_bm_readreg:	[mii interface function]
   11839  *
   11840  *	Read a PHY register on the kumeran
   11841  * This could be handled by the PHY layer if we didn't have to lock the
   11842  * resource ...
   11843  */
   11844 static int
   11845 wm_gmii_bm_readreg(device_t dev, int phy, int reg, uint16_t *val)
   11846 {
   11847 	struct wm_softc *sc = device_private(dev);
   11848 	uint16_t page = reg >> BME1000_PAGE_SHIFT;
   11849 	int rv;
   11850 
   11851 	if (sc->phy.acquire(sc)) {
   11852 		device_printf(dev, "%s: failed to get semaphore\n", __func__);
   11853 		return -1;
   11854 	}
   11855 
   11856 	if ((sc->sc_type != WM_T_82574) && (sc->sc_type != WM_T_82583))
   11857 		phy = ((page >= 768) || ((page == 0) && (reg == 25))
   11858 		    || (reg == 31)) ? 1 : phy;
   11859 	/* Page 800 works differently than the rest so it has its own func */
   11860 	if (page == BM_WUC_PAGE) {
   11861 		rv = wm_access_phy_wakeup_reg_bm(dev, reg, val, true, false);
   11862 		goto release;
   11863 	}
   11864 
   11865 	if (reg > BME1000_MAX_MULTI_PAGE_REG) {
   11866 		if ((phy == 1) && (sc->sc_type != WM_T_82574)
   11867 		    && (sc->sc_type != WM_T_82583))
   11868 			rv = wm_gmii_mdic_writereg(dev, phy,
   11869 			    IGPHY_PAGE_SELECT, page << BME1000_PAGE_SHIFT);
   11870 		else
   11871 			rv = wm_gmii_mdic_writereg(dev, phy,
   11872 			    BME1000_PHY_PAGE_SELECT, page);
   11873 		if (rv != 0)
   11874 			goto release;
   11875 	}
   11876 
   11877 	rv = wm_gmii_mdic_readreg(dev, phy, reg & MII_ADDRMASK, val);
   11878 
   11879 release:
   11880 	sc->phy.release(sc);
   11881 	return rv;
   11882 }
   11883 
   11884 /*
   11885  * wm_gmii_bm_writereg:	[mii interface function]
   11886  *
   11887  *	Write a PHY register on the kumeran.
   11888  * This could be handled by the PHY layer if we didn't have to lock the
   11889  * resource ...
   11890  */
   11891 static int
   11892 wm_gmii_bm_writereg(device_t dev, int phy, int reg, uint16_t val)
   11893 {
   11894 	struct wm_softc *sc = device_private(dev);
   11895 	uint16_t page = reg >> BME1000_PAGE_SHIFT;
   11896 	int rv;
   11897 
   11898 	if (sc->phy.acquire(sc)) {
   11899 		device_printf(dev, "%s: failed to get semaphore\n", __func__);
   11900 		return -1;
   11901 	}
   11902 
   11903 	if ((sc->sc_type != WM_T_82574) && (sc->sc_type != WM_T_82583))
   11904 		phy = ((page >= 768) || ((page == 0) && (reg == 25))
   11905 		    || (reg == 31)) ? 1 : phy;
   11906 	/* Page 800 works differently than the rest so it has its own func */
   11907 	if (page == BM_WUC_PAGE) {
   11908 		rv = wm_access_phy_wakeup_reg_bm(dev, reg, &val, false, false);
   11909 		goto release;
   11910 	}
   11911 
   11912 	if (reg > BME1000_MAX_MULTI_PAGE_REG) {
   11913 		if ((phy == 1) && (sc->sc_type != WM_T_82574)
   11914 		    && (sc->sc_type != WM_T_82583))
   11915 			rv = wm_gmii_mdic_writereg(dev, phy,
   11916 			    IGPHY_PAGE_SELECT, page << BME1000_PAGE_SHIFT);
   11917 		else
   11918 			rv = wm_gmii_mdic_writereg(dev, phy,
   11919 			    BME1000_PHY_PAGE_SELECT, page);
   11920 		if (rv != 0)
   11921 			goto release;
   11922 	}
   11923 
   11924 	rv = wm_gmii_mdic_writereg(dev, phy, reg & MII_ADDRMASK, val);
   11925 
   11926 release:
   11927 	sc->phy.release(sc);
   11928 	return rv;
   11929 }
   11930 
   11931 /*
   11932  *  wm_enable_phy_wakeup_reg_access_bm - enable access to BM wakeup registers
   11933  *  @dev: pointer to the HW structure
   11934  *  @phy_reg: pointer to store original contents of BM_WUC_ENABLE_REG
   11935  *
   11936  *  Assumes semaphore already acquired and phy_reg points to a valid memory
   11937  *  address to store contents of the BM_WUC_ENABLE_REG register.
   11938  */
   11939 static int
   11940 wm_enable_phy_wakeup_reg_access_bm(device_t dev, uint16_t *phy_regp)
   11941 {
   11942 #ifdef WM_DEBUG
   11943 	struct wm_softc *sc = device_private(dev);
   11944 #endif
   11945 	uint16_t temp;
   11946 	int rv;
   11947 
   11948 	DPRINTF(sc, WM_DEBUG_INIT, ("%s: %s called\n",
   11949 		device_xname(dev), __func__));
   11950 
   11951 	if (!phy_regp)
   11952 		return -1;
   11953 
   11954 	/* All page select, port ctrl and wakeup registers use phy address 1 */
   11955 
   11956 	/* Select Port Control Registers page */
   11957 	rv = wm_gmii_mdic_writereg(dev, 1, IGPHY_PAGE_SELECT,
   11958 	    BM_PORT_CTRL_PAGE << IGP3_PAGE_SHIFT);
   11959 	if (rv != 0)
   11960 		return rv;
   11961 
   11962 	/* Read WUCE and save it */
   11963 	rv = wm_gmii_mdic_readreg(dev, 1, BM_WUC_ENABLE_REG, phy_regp);
   11964 	if (rv != 0)
   11965 		return rv;
   11966 
   11967 	/* Enable both PHY wakeup mode and Wakeup register page writes.
   11968 	 * Prevent a power state change by disabling ME and Host PHY wakeup.
   11969 	 */
   11970 	temp = *phy_regp;
   11971 	temp |= BM_WUC_ENABLE_BIT;
   11972 	temp &= ~(BM_WUC_ME_WU_BIT | BM_WUC_HOST_WU_BIT);
   11973 
   11974 	if ((rv = wm_gmii_mdic_writereg(dev, 1, BM_WUC_ENABLE_REG, temp)) != 0)
   11975 		return rv;
   11976 
   11977 	/* Select Host Wakeup Registers page - caller now able to write
   11978 	 * registers on the Wakeup registers page
   11979 	 */
   11980 	return wm_gmii_mdic_writereg(dev, 1, IGPHY_PAGE_SELECT,
   11981 	    BM_WUC_PAGE << IGP3_PAGE_SHIFT);
   11982 }
   11983 
   11984 /*
   11985  *  wm_disable_phy_wakeup_reg_access_bm - disable access to BM wakeup regs
   11986  *  @dev: pointer to the HW structure
   11987  *  @phy_reg: pointer to original contents of BM_WUC_ENABLE_REG
   11988  *
   11989  *  Restore BM_WUC_ENABLE_REG to its original value.
   11990  *
   11991  *  Assumes semaphore already acquired and *phy_reg is the contents of the
   11992  *  BM_WUC_ENABLE_REG before register(s) on BM_WUC_PAGE were accessed by
   11993  *  caller.
   11994  */
   11995 static int
   11996 wm_disable_phy_wakeup_reg_access_bm(device_t dev, uint16_t *phy_regp)
   11997 {
   11998 #ifdef WM_DEBUG
   11999 	struct wm_softc *sc = device_private(dev);
   12000 #endif
   12001 
   12002 	DPRINTF(sc, WM_DEBUG_INIT, ("%s: %s called\n",
   12003 		device_xname(dev), __func__));
   12004 
   12005 	if (!phy_regp)
   12006 		return -1;
   12007 
   12008 	/* Select Port Control Registers page */
   12009 	wm_gmii_mdic_writereg(dev, 1, IGPHY_PAGE_SELECT,
   12010 	    BM_PORT_CTRL_PAGE << IGP3_PAGE_SHIFT);
   12011 
   12012 	/* Restore 769.17 to its original value */
   12013 	wm_gmii_mdic_writereg(dev, 1, BM_WUC_ENABLE_REG, *phy_regp);
   12014 
   12015 	return 0;
   12016 }
   12017 
   12018 /*
   12019  *  wm_access_phy_wakeup_reg_bm - Read/write BM PHY wakeup register
   12020  *  @sc: pointer to the HW structure
   12021  *  @offset: register offset to be read or written
   12022  *  @val: pointer to the data to read or write
   12023  *  @rd: determines if operation is read or write
   12024  *  @page_set: BM_WUC_PAGE already set and access enabled
   12025  *
   12026  *  Read the PHY register at offset and store the retrieved information in
   12027  *  data, or write data to PHY register at offset.  Note the procedure to
   12028  *  access the PHY wakeup registers is different than reading the other PHY
   12029  *  registers. It works as such:
   12030  *  1) Set 769.17.2 (page 769, register 17, bit 2) = 1
   12031  *  2) Set page to 800 for host (801 if we were manageability)
   12032  *  3) Write the address using the address opcode (0x11)
   12033  *  4) Read or write the data using the data opcode (0x12)
   12034  *  5) Restore 769.17.2 to its original value
   12035  *
   12036  *  Steps 1 and 2 are done by wm_enable_phy_wakeup_reg_access_bm() and
   12037  *  step 5 is done by wm_disable_phy_wakeup_reg_access_bm().
   12038  *
   12039  *  Assumes semaphore is already acquired.  When page_set==TRUE, assumes
   12040  *  the PHY page is set to BM_WUC_PAGE (i.e. a function in the call stack
   12041  *  is responsible for calls to wm_[enable|disable]_phy_wakeup_reg_bm()).
   12042  */
   12043 static int
   12044 wm_access_phy_wakeup_reg_bm(device_t dev, int offset, int16_t *val, int rd,
   12045 	bool page_set)
   12046 {
   12047 	struct wm_softc *sc = device_private(dev);
   12048 	uint16_t regnum = BM_PHY_REG_NUM(offset);
   12049 	uint16_t page = BM_PHY_REG_PAGE(offset);
   12050 	uint16_t wuce;
   12051 	int rv = 0;
   12052 
   12053 	DPRINTF(sc, WM_DEBUG_GMII, ("%s: %s called\n",
   12054 		device_xname(dev), __func__));
   12055 	/* XXX Gig must be disabled for MDIO accesses to page 800 */
   12056 	if ((sc->sc_type == WM_T_PCH)
   12057 	    && ((CSR_READ(sc, WMREG_PHY_CTRL) & PHY_CTRL_GBE_DIS) == 0)) {
   12058 		device_printf(dev,
   12059 		    "Attempting to access page %d while gig enabled.\n", page);
   12060 	}
   12061 
   12062 	if (!page_set) {
   12063 		/* Enable access to PHY wakeup registers */
   12064 		rv = wm_enable_phy_wakeup_reg_access_bm(dev, &wuce);
   12065 		if (rv != 0) {
   12066 			device_printf(dev,
   12067 			    "%s: Could not enable PHY wakeup reg access\n",
   12068 			    __func__);
   12069 			return rv;
   12070 		}
   12071 	}
   12072 	DPRINTF(sc, WM_DEBUG_GMII, ("%s: %s: Accessing PHY page %d reg 0x%x\n",
   12073 		device_xname(sc->sc_dev), __func__, page, regnum));
   12074 
   12075 	/*
   12076 	 * 2) Access PHY wakeup register.
   12077 	 * See wm_access_phy_wakeup_reg_bm.
   12078 	 */
   12079 
   12080 	/* Write the Wakeup register page offset value using opcode 0x11 */
   12081 	rv = wm_gmii_mdic_writereg(dev, 1, BM_WUC_ADDRESS_OPCODE, regnum);
   12082 	if (rv != 0)
   12083 		return rv;
   12084 
   12085 	if (rd) {
   12086 		/* Read the Wakeup register page value using opcode 0x12 */
   12087 		rv = wm_gmii_mdic_readreg(dev, 1, BM_WUC_DATA_OPCODE, val);
   12088 	} else {
   12089 		/* Write the Wakeup register page value using opcode 0x12 */
   12090 		rv = wm_gmii_mdic_writereg(dev, 1, BM_WUC_DATA_OPCODE, *val);
   12091 	}
   12092 	if (rv != 0)
   12093 		return rv;
   12094 
   12095 	if (!page_set)
   12096 		rv = wm_disable_phy_wakeup_reg_access_bm(dev, &wuce);
   12097 
   12098 	return rv;
   12099 }
   12100 
   12101 /*
   12102  * wm_gmii_hv_readreg:	[mii interface function]
   12103  *
   12104  *	Read a PHY register on the kumeran
   12105  * This could be handled by the PHY layer if we didn't have to lock the
   12106  * resource ...
   12107  */
   12108 static int
   12109 wm_gmii_hv_readreg(device_t dev, int phy, int reg, uint16_t *val)
   12110 {
   12111 	struct wm_softc *sc = device_private(dev);
   12112 	int rv;
   12113 
   12114 	DPRINTF(sc, WM_DEBUG_GMII, ("%s: %s called\n",
   12115 		device_xname(dev), __func__));
   12116 	if (sc->phy.acquire(sc)) {
   12117 		device_printf(dev, "%s: failed to get semaphore\n", __func__);
   12118 		return -1;
   12119 	}
   12120 
   12121 	rv = wm_gmii_hv_readreg_locked(dev, phy, reg, val);
   12122 	sc->phy.release(sc);
   12123 	return rv;
   12124 }
   12125 
   12126 static int
   12127 wm_gmii_hv_readreg_locked(device_t dev, int phy, int reg, uint16_t *val)
   12128 {
   12129 	uint16_t page = BM_PHY_REG_PAGE(reg);
   12130 	uint16_t regnum = BM_PHY_REG_NUM(reg);
   12131 	int rv;
   12132 
   12133 	phy = (page >= HV_INTC_FC_PAGE_START) ? 1 : phy;
   12134 
   12135 	/* Page 800 works differently than the rest so it has its own func */
   12136 	if (page == BM_WUC_PAGE)
   12137 		return wm_access_phy_wakeup_reg_bm(dev, reg, val, true, false);
   12138 
   12139 	/*
   12140 	 * Lower than page 768 works differently than the rest so it has its
   12141 	 * own func
   12142 	 */
   12143 	if ((page > 0) && (page < HV_INTC_FC_PAGE_START)) {
   12144 		device_printf(dev, "gmii_hv_readreg!!!\n");
   12145 		return -1;
   12146 	}
   12147 
   12148 	/*
   12149 	 * XXX I21[789] documents say that the SMBus Address register is at
   12150 	 * PHY address 01, Page 0 (not 768), Register 26.
   12151 	 */
   12152 	if (page == HV_INTC_FC_PAGE_START)
   12153 		page = 0;
   12154 
   12155 	if (regnum > BME1000_MAX_MULTI_PAGE_REG) {
   12156 		rv = wm_gmii_mdic_writereg(dev, 1, IGPHY_PAGE_SELECT,
   12157 		    page << BME1000_PAGE_SHIFT);
   12158 		if (rv != 0)
   12159 			return rv;
   12160 	}
   12161 
   12162 	return wm_gmii_mdic_readreg(dev, phy, regnum & MII_ADDRMASK, val);
   12163 }
   12164 
   12165 /*
   12166  * wm_gmii_hv_writereg:	[mii interface function]
   12167  *
   12168  *	Write a PHY register on the kumeran.
   12169  * This could be handled by the PHY layer if we didn't have to lock the
   12170  * resource ...
   12171  */
   12172 static int
   12173 wm_gmii_hv_writereg(device_t dev, int phy, int reg, uint16_t val)
   12174 {
   12175 	struct wm_softc *sc = device_private(dev);
   12176 	int rv;
   12177 
   12178 	DPRINTF(sc, WM_DEBUG_GMII, ("%s: %s called\n",
   12179 		device_xname(dev), __func__));
   12180 
   12181 	if (sc->phy.acquire(sc)) {
   12182 		device_printf(dev, "%s: failed to get semaphore\n", __func__);
   12183 		return -1;
   12184 	}
   12185 
   12186 	rv = wm_gmii_hv_writereg_locked(dev, phy, reg, val);
   12187 	sc->phy.release(sc);
   12188 
   12189 	return rv;
   12190 }
   12191 
   12192 static int
   12193 wm_gmii_hv_writereg_locked(device_t dev, int phy, int reg, uint16_t val)
   12194 {
   12195 	struct wm_softc *sc = device_private(dev);
   12196 	uint16_t page = BM_PHY_REG_PAGE(reg);
   12197 	uint16_t regnum = BM_PHY_REG_NUM(reg);
   12198 	int rv;
   12199 
   12200 	phy = (page >= HV_INTC_FC_PAGE_START) ? 1 : phy;
   12201 
   12202 	/* Page 800 works differently than the rest so it has its own func */
   12203 	if (page == BM_WUC_PAGE)
   12204 		return wm_access_phy_wakeup_reg_bm(dev, reg, &val, false,
   12205 		    false);
   12206 
   12207 	/*
   12208 	 * Lower than page 768 works differently than the rest so it has its
   12209 	 * own func
   12210 	 */
   12211 	if ((page > 0) && (page < HV_INTC_FC_PAGE_START)) {
   12212 		device_printf(dev, "gmii_hv_writereg!!!\n");
   12213 		return -1;
   12214 	}
   12215 
   12216 	{
   12217 		/*
   12218 		 * XXX I21[789] documents say that the SMBus Address register
   12219 		 * is at PHY address 01, Page 0 (not 768), Register 26.
   12220 		 */
   12221 		if (page == HV_INTC_FC_PAGE_START)
   12222 			page = 0;
   12223 
   12224 		/*
   12225 		 * XXX Workaround MDIO accesses being disabled after entering
   12226 		 * IEEE Power Down (whenever bit 11 of the PHY control
   12227 		 * register is set)
   12228 		 */
   12229 		if (sc->sc_phytype == WMPHY_82578) {
   12230 			struct mii_softc *child;
   12231 
   12232 			child = LIST_FIRST(&sc->sc_mii.mii_phys);
   12233 			if ((child != NULL) && (child->mii_mpd_rev >= 1)
   12234 			    && (phy == 2) && ((regnum & MII_ADDRMASK) == 0)
   12235 			    && ((val & (1 << 11)) != 0)) {
   12236 				device_printf(dev, "XXX need workaround\n");
   12237 			}
   12238 		}
   12239 
   12240 		if (regnum > BME1000_MAX_MULTI_PAGE_REG) {
   12241 			rv = wm_gmii_mdic_writereg(dev, 1,
   12242 			    IGPHY_PAGE_SELECT, page << BME1000_PAGE_SHIFT);
   12243 			if (rv != 0)
   12244 				return rv;
   12245 		}
   12246 	}
   12247 
   12248 	return wm_gmii_mdic_writereg(dev, phy, regnum & MII_ADDRMASK, val);
   12249 }
   12250 
   12251 /*
   12252  * wm_gmii_82580_readreg:	[mii interface function]
   12253  *
   12254  *	Read a PHY register on the 82580 and I350.
   12255  * This could be handled by the PHY layer if we didn't have to lock the
   12256  * resource ...
   12257  */
   12258 static int
   12259 wm_gmii_82580_readreg(device_t dev, int phy, int reg, uint16_t *val)
   12260 {
   12261 	struct wm_softc *sc = device_private(dev);
   12262 	int rv;
   12263 
   12264 	if (sc->phy.acquire(sc) != 0) {
   12265 		device_printf(dev, "%s: failed to get semaphore\n", __func__);
   12266 		return -1;
   12267 	}
   12268 
   12269 #ifdef DIAGNOSTIC
   12270 	if (reg > MII_ADDRMASK) {
   12271 		device_printf(dev, "%s: PHYTYPE = %d, addr 0x%x > 0x1f\n",
   12272 		    __func__, sc->sc_phytype, reg);
   12273 		reg &= MII_ADDRMASK;
   12274 	}
   12275 #endif
   12276 	rv = wm_gmii_mdic_readreg(dev, phy, reg, val);
   12277 
   12278 	sc->phy.release(sc);
   12279 	return rv;
   12280 }
   12281 
   12282 /*
   12283  * wm_gmii_82580_writereg:	[mii interface function]
   12284  *
   12285  *	Write a PHY register on the 82580 and I350.
   12286  * This could be handled by the PHY layer if we didn't have to lock the
   12287  * resource ...
   12288  */
   12289 static int
   12290 wm_gmii_82580_writereg(device_t dev, int phy, int reg, uint16_t val)
   12291 {
   12292 	struct wm_softc *sc = device_private(dev);
   12293 	int rv;
   12294 
   12295 	if (sc->phy.acquire(sc) != 0) {
   12296 		device_printf(dev, "%s: failed to get semaphore\n", __func__);
   12297 		return -1;
   12298 	}
   12299 
   12300 #ifdef DIAGNOSTIC
   12301 	if (reg > MII_ADDRMASK) {
   12302 		device_printf(dev, "%s: PHYTYPE = %d, addr 0x%x > 0x1f\n",
   12303 		    __func__, sc->sc_phytype, reg);
   12304 		reg &= MII_ADDRMASK;
   12305 	}
   12306 #endif
   12307 	rv = wm_gmii_mdic_writereg(dev, phy, reg, val);
   12308 
   12309 	sc->phy.release(sc);
   12310 	return rv;
   12311 }
   12312 
   12313 /*
   12314  * wm_gmii_gs40g_readreg:	[mii interface function]
   12315  *
   12316  *	Read a PHY register on the I2100 and I211.
   12317  * This could be handled by the PHY layer if we didn't have to lock the
   12318  * resource ...
   12319  */
   12320 static int
   12321 wm_gmii_gs40g_readreg(device_t dev, int phy, int reg, uint16_t *val)
   12322 {
   12323 	struct wm_softc *sc = device_private(dev);
   12324 	int page, offset;
   12325 	int rv;
   12326 
   12327 	/* Acquire semaphore */
   12328 	if (sc->phy.acquire(sc)) {
   12329 		device_printf(dev, "%s: failed to get semaphore\n", __func__);
   12330 		return -1;
   12331 	}
   12332 
   12333 	/* Page select */
   12334 	page = reg >> GS40G_PAGE_SHIFT;
   12335 	rv = wm_gmii_mdic_writereg(dev, phy, GS40G_PAGE_SELECT, page);
   12336 	if (rv != 0)
   12337 		goto release;
   12338 
   12339 	/* Read reg */
   12340 	offset = reg & GS40G_OFFSET_MASK;
   12341 	rv = wm_gmii_mdic_readreg(dev, phy, offset, val);
   12342 
   12343 release:
   12344 	sc->phy.release(sc);
   12345 	return rv;
   12346 }
   12347 
   12348 /*
   12349  * wm_gmii_gs40g_writereg:	[mii interface function]
   12350  *
   12351  *	Write a PHY register on the I210 and I211.
   12352  * This could be handled by the PHY layer if we didn't have to lock the
   12353  * resource ...
   12354  */
   12355 static int
   12356 wm_gmii_gs40g_writereg(device_t dev, int phy, int reg, uint16_t val)
   12357 {
   12358 	struct wm_softc *sc = device_private(dev);
   12359 	uint16_t page;
   12360 	int offset, rv;
   12361 
   12362 	/* Acquire semaphore */
   12363 	if (sc->phy.acquire(sc)) {
   12364 		device_printf(dev, "%s: failed to get semaphore\n", __func__);
   12365 		return -1;
   12366 	}
   12367 
   12368 	/* Page select */
   12369 	page = reg >> GS40G_PAGE_SHIFT;
   12370 	rv = wm_gmii_mdic_writereg(dev, phy, GS40G_PAGE_SELECT, page);
   12371 	if (rv != 0)
   12372 		goto release;
   12373 
   12374 	/* Write reg */
   12375 	offset = reg & GS40G_OFFSET_MASK;
   12376 	rv = wm_gmii_mdic_writereg(dev, phy, offset, val);
   12377 
   12378 release:
   12379 	/* Release semaphore */
   12380 	sc->phy.release(sc);
   12381 	return rv;
   12382 }
   12383 
   12384 /*
   12385  * wm_gmii_statchg:	[mii interface function]
   12386  *
   12387  *	Callback from MII layer when media changes.
   12388  */
   12389 static void
   12390 wm_gmii_statchg(struct ifnet *ifp)
   12391 {
   12392 	struct wm_softc *sc = ifp->if_softc;
   12393 	struct mii_data *mii = &sc->sc_mii;
   12394 
   12395 	sc->sc_ctrl &= ~(CTRL_TFCE | CTRL_RFCE);
   12396 	sc->sc_tctl &= ~TCTL_COLD(0x3ff);
   12397 	sc->sc_fcrtl &= ~FCRTL_XONE;
   12398 
   12399 	/* Get flow control negotiation result. */
   12400 	if (IFM_SUBTYPE(mii->mii_media.ifm_cur->ifm_media) == IFM_AUTO &&
   12401 	    (mii->mii_media_active & IFM_ETH_FMASK) != sc->sc_flowflags) {
   12402 		sc->sc_flowflags = mii->mii_media_active & IFM_ETH_FMASK;
   12403 		mii->mii_media_active &= ~IFM_ETH_FMASK;
   12404 	}
   12405 
   12406 	if (sc->sc_flowflags & IFM_FLOW) {
   12407 		if (sc->sc_flowflags & IFM_ETH_TXPAUSE) {
   12408 			sc->sc_ctrl |= CTRL_TFCE;
   12409 			sc->sc_fcrtl |= FCRTL_XONE;
   12410 		}
   12411 		if (sc->sc_flowflags & IFM_ETH_RXPAUSE)
   12412 			sc->sc_ctrl |= CTRL_RFCE;
   12413 	}
   12414 
   12415 	if (mii->mii_media_active & IFM_FDX) {
   12416 		DPRINTF(sc, WM_DEBUG_LINK,
   12417 		    ("%s: LINK: statchg: FDX\n", ifp->if_xname));
   12418 		sc->sc_tctl |= TCTL_COLD(TX_COLLISION_DISTANCE_FDX);
   12419 	} else {
   12420 		DPRINTF(sc, WM_DEBUG_LINK,
   12421 		    ("%s: LINK: statchg: HDX\n", ifp->if_xname));
   12422 		sc->sc_tctl |= TCTL_COLD(TX_COLLISION_DISTANCE_HDX);
   12423 	}
   12424 
   12425 	CSR_WRITE(sc, WMREG_CTRL, sc->sc_ctrl);
   12426 	CSR_WRITE(sc, WMREG_TCTL, sc->sc_tctl);
   12427 	CSR_WRITE(sc, (sc->sc_type < WM_T_82543) ? WMREG_OLD_FCRTL
   12428 						 : WMREG_FCRTL, sc->sc_fcrtl);
   12429 	if (sc->sc_type == WM_T_80003) {
   12430 		switch (IFM_SUBTYPE(mii->mii_media_active)) {
   12431 		case IFM_1000_T:
   12432 			wm_kmrn_writereg(sc, KUMCTRLSTA_OFFSET_HD_CTRL,
   12433 			    KUMCTRLSTA_HD_CTRL_1000_DEFAULT);
   12434 			sc->sc_tipg =  TIPG_1000T_80003_DFLT;
   12435 			break;
   12436 		default:
   12437 			wm_kmrn_writereg(sc, KUMCTRLSTA_OFFSET_HD_CTRL,
   12438 			    KUMCTRLSTA_HD_CTRL_10_100_DEFAULT);
   12439 			sc->sc_tipg =  TIPG_10_100_80003_DFLT;
   12440 			break;
   12441 		}
   12442 		CSR_WRITE(sc, WMREG_TIPG, sc->sc_tipg);
   12443 	}
   12444 }
   12445 
   12446 /* kumeran related (80003, ICH* and PCH*) */
   12447 
   12448 /*
   12449  * wm_kmrn_readreg:
   12450  *
   12451  *	Read a kumeran register
   12452  */
   12453 static int
   12454 wm_kmrn_readreg(struct wm_softc *sc, int reg, uint16_t *val)
   12455 {
   12456 	int rv;
   12457 
   12458 	if (sc->sc_type == WM_T_80003)
   12459 		rv = wm_get_swfw_semaphore(sc, SWFW_MAC_CSR_SM);
   12460 	else
   12461 		rv = sc->phy.acquire(sc);
   12462 	if (rv != 0) {
   12463 		device_printf(sc->sc_dev, "%s: failed to get semaphore\n",
   12464 		    __func__);
   12465 		return rv;
   12466 	}
   12467 
   12468 	rv = wm_kmrn_readreg_locked(sc, reg, val);
   12469 
   12470 	if (sc->sc_type == WM_T_80003)
   12471 		wm_put_swfw_semaphore(sc, SWFW_MAC_CSR_SM);
   12472 	else
   12473 		sc->phy.release(sc);
   12474 
   12475 	return rv;
   12476 }
   12477 
   12478 static int
   12479 wm_kmrn_readreg_locked(struct wm_softc *sc, int reg, uint16_t *val)
   12480 {
   12481 
   12482 	CSR_WRITE(sc, WMREG_KUMCTRLSTA,
   12483 	    ((reg << KUMCTRLSTA_OFFSET_SHIFT) & KUMCTRLSTA_OFFSET) |
   12484 	    KUMCTRLSTA_REN);
   12485 	CSR_WRITE_FLUSH(sc);
   12486 	delay(2);
   12487 
   12488 	*val = CSR_READ(sc, WMREG_KUMCTRLSTA) & KUMCTRLSTA_MASK;
   12489 
   12490 	return 0;
   12491 }
   12492 
   12493 /*
   12494  * wm_kmrn_writereg:
   12495  *
   12496  *	Write a kumeran register
   12497  */
   12498 static int
   12499 wm_kmrn_writereg(struct wm_softc *sc, int reg, uint16_t val)
   12500 {
   12501 	int rv;
   12502 
   12503 	if (sc->sc_type == WM_T_80003)
   12504 		rv = wm_get_swfw_semaphore(sc, SWFW_MAC_CSR_SM);
   12505 	else
   12506 		rv = sc->phy.acquire(sc);
   12507 	if (rv != 0) {
   12508 		device_printf(sc->sc_dev, "%s: failed to get semaphore\n",
   12509 		    __func__);
   12510 		return rv;
   12511 	}
   12512 
   12513 	rv = wm_kmrn_writereg_locked(sc, reg, val);
   12514 
   12515 	if (sc->sc_type == WM_T_80003)
   12516 		wm_put_swfw_semaphore(sc, SWFW_MAC_CSR_SM);
   12517 	else
   12518 		sc->phy.release(sc);
   12519 
   12520 	return rv;
   12521 }
   12522 
   12523 static int
   12524 wm_kmrn_writereg_locked(struct wm_softc *sc, int reg, uint16_t val)
   12525 {
   12526 
   12527 	CSR_WRITE(sc, WMREG_KUMCTRLSTA,
   12528 	    ((reg << KUMCTRLSTA_OFFSET_SHIFT) & KUMCTRLSTA_OFFSET) | val);
   12529 
   12530 	return 0;
   12531 }
   12532 
   12533 /*
   12534  * EMI register related (82579, WMPHY_I217(PCH2 and newer))
   12535  * This access method is different from IEEE MMD.
   12536  */
   12537 static int
   12538 wm_access_emi_reg_locked(device_t dev, int reg, uint16_t *val, bool rd)
   12539 {
   12540 	struct wm_softc *sc = device_private(dev);
   12541 	int rv;
   12542 
   12543 	rv = sc->phy.writereg_locked(dev, 2, I82579_EMI_ADDR, reg);
   12544 	if (rv != 0)
   12545 		return rv;
   12546 
   12547 	if (rd)
   12548 		rv = sc->phy.readreg_locked(dev, 2, I82579_EMI_DATA, val);
   12549 	else
   12550 		rv = sc->phy.writereg_locked(dev, 2, I82579_EMI_DATA, *val);
   12551 	return rv;
   12552 }
   12553 
   12554 static int
   12555 wm_read_emi_reg_locked(device_t dev, int reg, uint16_t *val)
   12556 {
   12557 
   12558 	return wm_access_emi_reg_locked(dev, reg, val, true);
   12559 }
   12560 
   12561 static int
   12562 wm_write_emi_reg_locked(device_t dev, int reg, uint16_t val)
   12563 {
   12564 
   12565 	return wm_access_emi_reg_locked(dev, reg, &val, false);
   12566 }
   12567 
   12568 /* SGMII related */
   12569 
   12570 /*
   12571  * wm_sgmii_uses_mdio
   12572  *
   12573  * Check whether the transaction is to the internal PHY or the external
   12574  * MDIO interface. Return true if it's MDIO.
   12575  */
   12576 static bool
   12577 wm_sgmii_uses_mdio(struct wm_softc *sc)
   12578 {
   12579 	uint32_t reg;
   12580 	bool ismdio = false;
   12581 
   12582 	switch (sc->sc_type) {
   12583 	case WM_T_82575:
   12584 	case WM_T_82576:
   12585 		reg = CSR_READ(sc, WMREG_MDIC);
   12586 		ismdio = ((reg & MDIC_DEST) != 0);
   12587 		break;
   12588 	case WM_T_82580:
   12589 	case WM_T_I350:
   12590 	case WM_T_I354:
   12591 	case WM_T_I210:
   12592 	case WM_T_I211:
   12593 		reg = CSR_READ(sc, WMREG_MDICNFG);
   12594 		ismdio = ((reg & MDICNFG_DEST) != 0);
   12595 		break;
   12596 	default:
   12597 		break;
   12598 	}
   12599 
   12600 	return ismdio;
   12601 }
   12602 
   12603 /* Setup internal SGMII PHY for SFP */
   12604 static void
   12605 wm_sgmii_sfp_preconfig(struct wm_softc *sc)
   12606 {
   12607 	uint16_t id1, id2, phyreg;
   12608 	int i, rv;
   12609 
   12610 	if (((sc->sc_flags & WM_F_SGMII) == 0)
   12611 	    || ((sc->sc_flags & WM_F_SFP) == 0))
   12612 		return;
   12613 
   12614 	for (i = 0; i < MII_NPHY; i++) {
   12615 		sc->phy.no_errprint = true;
   12616 		rv = sc->phy.readreg_locked(sc->sc_dev, i, MII_PHYIDR1, &id1);
   12617 		if (rv != 0)
   12618 			continue;
   12619 		rv = sc->phy.readreg_locked(sc->sc_dev, i, MII_PHYIDR2, &id2);
   12620 		if (rv != 0)
   12621 			continue;
   12622 		if (MII_OUI(id1, id2) != MII_OUI_xxMARVELL)
   12623 			continue;
   12624 		sc->phy.no_errprint = false;
   12625 
   12626 		sc->phy.readreg_locked(sc->sc_dev, i, MAKPHY_ESSR, &phyreg);
   12627 		phyreg &= ~(ESSR_SER_ANEG_BYPASS | ESSR_HWCFG_MODE);
   12628 		phyreg |= ESSR_SGMII_WOC_COPPER;
   12629 		sc->phy.writereg_locked(sc->sc_dev, i, MAKPHY_ESSR, phyreg);
   12630 		break;
   12631 	}
   12632 
   12633 }
   12634 
   12635 /*
   12636  * wm_sgmii_readreg:	[mii interface function]
   12637  *
   12638  *	Read a PHY register on the SGMII
   12639  * This could be handled by the PHY layer if we didn't have to lock the
   12640  * resource ...
   12641  */
   12642 static int
   12643 wm_sgmii_readreg(device_t dev, int phy, int reg, uint16_t *val)
   12644 {
   12645 	struct wm_softc *sc = device_private(dev);
   12646 	int rv;
   12647 
   12648 	if (sc->phy.acquire(sc)) {
   12649 		device_printf(dev, "%s: failed to get semaphore\n", __func__);
   12650 		return -1;
   12651 	}
   12652 
   12653 	rv = wm_sgmii_readreg_locked(dev, phy, reg, val);
   12654 
   12655 	sc->phy.release(sc);
   12656 	return rv;
   12657 }
   12658 
   12659 static int
   12660 wm_sgmii_readreg_locked(device_t dev, int phy, int reg, uint16_t *val)
   12661 {
   12662 	struct wm_softc *sc = device_private(dev);
   12663 	uint32_t i2ccmd;
   12664 	int i, rv = 0;
   12665 
   12666 	i2ccmd = (reg << I2CCMD_REG_ADDR_SHIFT)
   12667 	    | (phy << I2CCMD_PHY_ADDR_SHIFT) | I2CCMD_OPCODE_READ;
   12668 	CSR_WRITE(sc, WMREG_I2CCMD, i2ccmd);
   12669 
   12670 	/* Poll the ready bit */
   12671 	for (i = 0; i < I2CCMD_PHY_TIMEOUT; i++) {
   12672 		delay(50);
   12673 		i2ccmd = CSR_READ(sc, WMREG_I2CCMD);
   12674 		if (i2ccmd & I2CCMD_READY)
   12675 			break;
   12676 	}
   12677 	if ((i2ccmd & I2CCMD_READY) == 0) {
   12678 		device_printf(dev, "I2CCMD Read did not complete\n");
   12679 		rv = ETIMEDOUT;
   12680 	}
   12681 	if ((i2ccmd & I2CCMD_ERROR) != 0) {
   12682 		if (!sc->phy.no_errprint)
   12683 			device_printf(dev, "I2CCMD Error bit set\n");
   12684 		rv = EIO;
   12685 	}
   12686 
   12687 	*val = (uint16_t)((i2ccmd >> 8) & 0x00ff) | ((i2ccmd << 8) & 0xff00);
   12688 
   12689 	return rv;
   12690 }
   12691 
   12692 /*
   12693  * wm_sgmii_writereg:	[mii interface function]
   12694  *
   12695  *	Write a PHY register on the SGMII.
   12696  * This could be handled by the PHY layer if we didn't have to lock the
   12697  * resource ...
   12698  */
   12699 static int
   12700 wm_sgmii_writereg(device_t dev, int phy, int reg, uint16_t val)
   12701 {
   12702 	struct wm_softc *sc = device_private(dev);
   12703 	int rv;
   12704 
   12705 	if (sc->phy.acquire(sc) != 0) {
   12706 		device_printf(dev, "%s: failed to get semaphore\n", __func__);
   12707 		return -1;
   12708 	}
   12709 
   12710 	rv = wm_sgmii_writereg_locked(dev, phy, reg, val);
   12711 
   12712 	sc->phy.release(sc);
   12713 
   12714 	return rv;
   12715 }
   12716 
   12717 static int
   12718 wm_sgmii_writereg_locked(device_t dev, int phy, int reg, uint16_t val)
   12719 {
   12720 	struct wm_softc *sc = device_private(dev);
   12721 	uint32_t i2ccmd;
   12722 	uint16_t swapdata;
   12723 	int rv = 0;
   12724 	int i;
   12725 
   12726 	/* Swap the data bytes for the I2C interface */
   12727 	swapdata = ((val >> 8) & 0x00FF) | ((val << 8) & 0xFF00);
   12728 	i2ccmd = (reg << I2CCMD_REG_ADDR_SHIFT)
   12729 	    | (phy << I2CCMD_PHY_ADDR_SHIFT) | I2CCMD_OPCODE_WRITE | swapdata;
   12730 	CSR_WRITE(sc, WMREG_I2CCMD, i2ccmd);
   12731 
   12732 	/* Poll the ready bit */
   12733 	for (i = 0; i < I2CCMD_PHY_TIMEOUT; i++) {
   12734 		delay(50);
   12735 		i2ccmd = CSR_READ(sc, WMREG_I2CCMD);
   12736 		if (i2ccmd & I2CCMD_READY)
   12737 			break;
   12738 	}
   12739 	if ((i2ccmd & I2CCMD_READY) == 0) {
   12740 		device_printf(dev, "I2CCMD Write did not complete\n");
   12741 		rv = ETIMEDOUT;
   12742 	}
   12743 	if ((i2ccmd & I2CCMD_ERROR) != 0) {
   12744 		device_printf(dev, "I2CCMD Error bit set\n");
   12745 		rv = EIO;
   12746 	}
   12747 
   12748 	return rv;
   12749 }
   12750 
   12751 /* TBI related */
   12752 
   12753 static bool
   12754 wm_tbi_havesignal(struct wm_softc *sc, uint32_t ctrl)
   12755 {
   12756 	bool sig;
   12757 
   12758 	sig = ctrl & CTRL_SWDPIN(1);
   12759 
   12760 	/*
   12761 	 * On 82543 and 82544, the CTRL_SWDPIN(1) bit will be 0 if the optics
   12762 	 * detect a signal, 1 if they don't.
   12763 	 */
   12764 	if ((sc->sc_type == WM_T_82543) || (sc->sc_type == WM_T_82544))
   12765 		sig = !sig;
   12766 
   12767 	return sig;
   12768 }
   12769 
   12770 /*
   12771  * wm_tbi_mediainit:
   12772  *
   12773  *	Initialize media for use on 1000BASE-X devices.
   12774  */
   12775 static void
   12776 wm_tbi_mediainit(struct wm_softc *sc)
   12777 {
   12778 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   12779 	const char *sep = "";
   12780 
   12781 	if (sc->sc_type < WM_T_82543)
   12782 		sc->sc_tipg = TIPG_WM_DFLT;
   12783 	else
   12784 		sc->sc_tipg = TIPG_LG_DFLT;
   12785 
   12786 	sc->sc_tbi_serdes_anegticks = 5;
   12787 
   12788 	/* Initialize our media structures */
   12789 	sc->sc_mii.mii_ifp = ifp;
   12790 	sc->sc_ethercom.ec_mii = &sc->sc_mii;
   12791 
   12792 	ifp->if_baudrate = IF_Gbps(1);
   12793 	if (((sc->sc_type >= WM_T_82575) && (sc->sc_type <= WM_T_I211))
   12794 	    && (sc->sc_mediatype == WM_MEDIATYPE_SERDES)) {
   12795 		ifmedia_init_with_lock(&sc->sc_mii.mii_media, IFM_IMASK,
   12796 		    wm_serdes_mediachange, wm_serdes_mediastatus,
   12797 		    sc->sc_core_lock);
   12798 	} else {
   12799 		ifmedia_init_with_lock(&sc->sc_mii.mii_media, IFM_IMASK,
   12800 		    wm_tbi_mediachange, wm_tbi_mediastatus, sc->sc_core_lock);
   12801 	}
   12802 
   12803 	/*
   12804 	 * SWD Pins:
   12805 	 *
   12806 	 *	0 = Link LED (output)
   12807 	 *	1 = Loss Of Signal (input)
   12808 	 */
   12809 	sc->sc_ctrl |= CTRL_SWDPIO(0);
   12810 
   12811 	/* XXX Perhaps this is only for TBI */
   12812 	if (sc->sc_mediatype != WM_MEDIATYPE_SERDES)
   12813 		sc->sc_ctrl &= ~CTRL_SWDPIO(1);
   12814 
   12815 	if (sc->sc_mediatype == WM_MEDIATYPE_SERDES)
   12816 		sc->sc_ctrl &= ~CTRL_LRST;
   12817 
   12818 	CSR_WRITE(sc, WMREG_CTRL, sc->sc_ctrl);
   12819 
   12820 #define	ADD(ss, mm, dd)							  \
   12821 do {									  \
   12822 	aprint_normal("%s%s", sep, ss);					  \
   12823 	ifmedia_add(&sc->sc_mii.mii_media, IFM_ETHER | (mm), (dd), NULL); \
   12824 	sep = ", ";							  \
   12825 } while (/*CONSTCOND*/0)
   12826 
   12827 	aprint_normal_dev(sc->sc_dev, "");
   12828 
   12829 	if (sc->sc_type == WM_T_I354) {
   12830 		uint32_t status;
   12831 
   12832 		status = CSR_READ(sc, WMREG_STATUS);
   12833 		if (((status & STATUS_2P5_SKU) != 0)
   12834 		    && ((status & STATUS_2P5_SKU_OVER) == 0)) {
   12835 			ADD("2500baseKX-FDX", IFM_2500_KX | IFM_FDX,ANAR_X_FD);
   12836 		} else
   12837 			ADD("1000baseKX-FDX", IFM_1000_KX | IFM_FDX,ANAR_X_FD);
   12838 	} else if (sc->sc_type == WM_T_82545) {
   12839 		/* Only 82545 is LX (XXX except SFP) */
   12840 		ADD("1000baseLX", IFM_1000_LX, ANAR_X_HD);
   12841 		ADD("1000baseLX-FDX", IFM_1000_LX | IFM_FDX, ANAR_X_FD);
   12842 	} else if (sc->sc_sfptype != 0) {
   12843 		/* XXX wm(4) fiber/serdes don't use ifm_data */
   12844 		switch (sc->sc_sfptype) {
   12845 		default:
   12846 		case SFF_SFP_ETH_FLAGS_1000SX:
   12847 			ADD("1000baseSX", IFM_1000_SX, ANAR_X_HD);
   12848 			ADD("1000baseSX-FDX", IFM_1000_SX | IFM_FDX, ANAR_X_FD);
   12849 			break;
   12850 		case SFF_SFP_ETH_FLAGS_1000LX:
   12851 			ADD("1000baseLX", IFM_1000_LX, ANAR_X_HD);
   12852 			ADD("1000baseLX-FDX", IFM_1000_LX | IFM_FDX, ANAR_X_FD);
   12853 			break;
   12854 		case SFF_SFP_ETH_FLAGS_1000CX:
   12855 			ADD("1000baseCX", IFM_1000_CX, ANAR_X_HD);
   12856 			ADD("1000baseCX-FDX", IFM_1000_CX | IFM_FDX, ANAR_X_FD);
   12857 			break;
   12858 		case SFF_SFP_ETH_FLAGS_1000T:
   12859 			ADD("1000baseT", IFM_1000_T, 0);
   12860 			ADD("1000baseT-FDX", IFM_1000_T | IFM_FDX, 0);
   12861 			break;
   12862 		case SFF_SFP_ETH_FLAGS_100FX:
   12863 			ADD("100baseFX", IFM_100_FX, ANAR_TX);
   12864 			ADD("100baseFX-FDX", IFM_100_FX | IFM_FDX, ANAR_TX_FD);
   12865 			break;
   12866 		}
   12867 	} else {
   12868 		ADD("1000baseSX", IFM_1000_SX, ANAR_X_HD);
   12869 		ADD("1000baseSX-FDX", IFM_1000_SX | IFM_FDX, ANAR_X_FD);
   12870 	}
   12871 	ADD("auto", IFM_AUTO, ANAR_X_FD | ANAR_X_HD);
   12872 	aprint_normal("\n");
   12873 
   12874 #undef ADD
   12875 
   12876 	ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER | IFM_AUTO);
   12877 }
   12878 
   12879 /*
   12880  * wm_tbi_mediachange:	[ifmedia interface function]
   12881  *
   12882  *	Set hardware to newly-selected media on a 1000BASE-X device.
   12883  */
   12884 static int
   12885 wm_tbi_mediachange(struct ifnet *ifp)
   12886 {
   12887 	struct wm_softc *sc = ifp->if_softc;
   12888 	struct ifmedia_entry *ife = sc->sc_mii.mii_media.ifm_cur;
   12889 	uint32_t status, ctrl;
   12890 	bool signal;
   12891 	int i;
   12892 
   12893 	KASSERT(sc->sc_mediatype != WM_MEDIATYPE_COPPER);
   12894 	if (sc->sc_mediatype == WM_MEDIATYPE_SERDES) {
   12895 		/* XXX need some work for >= 82571 and < 82575 */
   12896 		if (sc->sc_type < WM_T_82575)
   12897 			return 0;
   12898 	}
   12899 
   12900 	if ((sc->sc_type == WM_T_82571) || (sc->sc_type == WM_T_82572)
   12901 	    || (sc->sc_type >= WM_T_82575))
   12902 		CSR_WRITE(sc, WMREG_SCTL, SCTL_DISABLE_SERDES_LOOPBACK);
   12903 
   12904 	sc->sc_ctrl &= ~CTRL_LRST;
   12905 	sc->sc_txcw = TXCW_ANE;
   12906 	if (IFM_SUBTYPE(ife->ifm_media) == IFM_AUTO)
   12907 		sc->sc_txcw |= TXCW_FD | TXCW_HD;
   12908 	else if (ife->ifm_media & IFM_FDX)
   12909 		sc->sc_txcw |= TXCW_FD;
   12910 	else
   12911 		sc->sc_txcw |= TXCW_HD;
   12912 
   12913 	if ((sc->sc_mii.mii_media.ifm_media & IFM_FLOW) != 0)
   12914 		sc->sc_txcw |= TXCW_SYM_PAUSE | TXCW_ASYM_PAUSE;
   12915 
   12916 	DPRINTF(sc, WM_DEBUG_LINK,("%s: sc_txcw = 0x%x after autoneg check\n",
   12917 		device_xname(sc->sc_dev), sc->sc_txcw));
   12918 	CSR_WRITE(sc, WMREG_TXCW, sc->sc_txcw);
   12919 	CSR_WRITE(sc, WMREG_CTRL, sc->sc_ctrl);
   12920 	CSR_WRITE_FLUSH(sc);
   12921 	delay(1000);
   12922 
   12923 	ctrl = CSR_READ(sc, WMREG_CTRL);
   12924 	signal = wm_tbi_havesignal(sc, ctrl);
   12925 
   12926 	DPRINTF(sc, WM_DEBUG_LINK,
   12927 	    ("%s: signal = %d\n", device_xname(sc->sc_dev), signal));
   12928 
   12929 	if (signal) {
   12930 		/* Have signal; wait for the link to come up. */
   12931 		for (i = 0; i < WM_LINKUP_TIMEOUT; i++) {
   12932 			delay(10000);
   12933 			if (CSR_READ(sc, WMREG_STATUS) & STATUS_LU)
   12934 				break;
   12935 		}
   12936 
   12937 		DPRINTF(sc, WM_DEBUG_LINK,
   12938 		    ("%s: i = %d after waiting for link\n",
   12939 			device_xname(sc->sc_dev), i));
   12940 
   12941 		status = CSR_READ(sc, WMREG_STATUS);
   12942 		DPRINTF(sc, WM_DEBUG_LINK,
   12943 		    ("%s: status after final read = 0x%x, STATUS_LU = %#"
   12944 			__PRIxBIT "\n",
   12945 			device_xname(sc->sc_dev), status, STATUS_LU));
   12946 		if (status & STATUS_LU) {
   12947 			/* Link is up. */
   12948 			DPRINTF(sc, WM_DEBUG_LINK,
   12949 			    ("%s: LINK: set media -> link up %s\n",
   12950 				device_xname(sc->sc_dev),
   12951 				(status & STATUS_FD) ? "FDX" : "HDX"));
   12952 
   12953 			/*
   12954 			 * NOTE: CTRL will update TFCE and RFCE automatically,
   12955 			 * so we should update sc->sc_ctrl
   12956 			 */
   12957 			sc->sc_ctrl = CSR_READ(sc, WMREG_CTRL);
   12958 			sc->sc_tctl &= ~TCTL_COLD(0x3ff);
   12959 			sc->sc_fcrtl &= ~FCRTL_XONE;
   12960 			if (status & STATUS_FD)
   12961 				sc->sc_tctl |=
   12962 				    TCTL_COLD(TX_COLLISION_DISTANCE_FDX);
   12963 			else
   12964 				sc->sc_tctl |=
   12965 				    TCTL_COLD(TX_COLLISION_DISTANCE_HDX);
   12966 			if (CSR_READ(sc, WMREG_CTRL) & CTRL_TFCE)
   12967 				sc->sc_fcrtl |= FCRTL_XONE;
   12968 			CSR_WRITE(sc, WMREG_TCTL, sc->sc_tctl);
   12969 			CSR_WRITE(sc, (sc->sc_type < WM_T_82543) ?
   12970 			    WMREG_OLD_FCRTL : WMREG_FCRTL, sc->sc_fcrtl);
   12971 			sc->sc_tbi_linkup = 1;
   12972 		} else {
   12973 			if (i == WM_LINKUP_TIMEOUT)
   12974 				wm_check_for_link(sc);
   12975 			/* Link is down. */
   12976 			DPRINTF(sc, WM_DEBUG_LINK,
   12977 			    ("%s: LINK: set media -> link down\n",
   12978 				device_xname(sc->sc_dev)));
   12979 			sc->sc_tbi_linkup = 0;
   12980 		}
   12981 	} else {
   12982 		DPRINTF(sc, WM_DEBUG_LINK,
   12983 		    ("%s: LINK: set media -> no signal\n",
   12984 			device_xname(sc->sc_dev)));
   12985 		sc->sc_tbi_linkup = 0;
   12986 	}
   12987 
   12988 	wm_tbi_serdes_set_linkled(sc);
   12989 
   12990 	return 0;
   12991 }
   12992 
   12993 /*
   12994  * wm_tbi_mediastatus:	[ifmedia interface function]
   12995  *
   12996  *	Get the current interface media status on a 1000BASE-X device.
   12997  */
   12998 static void
   12999 wm_tbi_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr)
   13000 {
   13001 	struct wm_softc *sc = ifp->if_softc;
   13002 	uint32_t ctrl, status;
   13003 
   13004 	ifmr->ifm_status = IFM_AVALID;
   13005 	ifmr->ifm_active = IFM_ETHER;
   13006 
   13007 	status = CSR_READ(sc, WMREG_STATUS);
   13008 	if ((status & STATUS_LU) == 0) {
   13009 		ifmr->ifm_active |= IFM_NONE;
   13010 		return;
   13011 	}
   13012 
   13013 	ifmr->ifm_status |= IFM_ACTIVE;
   13014 	/* Only 82545 is LX */
   13015 	if (sc->sc_type == WM_T_82545)
   13016 		ifmr->ifm_active |= IFM_1000_LX;
   13017 	else
   13018 		ifmr->ifm_active |= IFM_1000_SX;
   13019 	if (CSR_READ(sc, WMREG_STATUS) & STATUS_FD)
   13020 		ifmr->ifm_active |= IFM_FDX;
   13021 	else
   13022 		ifmr->ifm_active |= IFM_HDX;
   13023 	ctrl = CSR_READ(sc, WMREG_CTRL);
   13024 	if (ctrl & CTRL_RFCE)
   13025 		ifmr->ifm_active |= IFM_FLOW | IFM_ETH_RXPAUSE;
   13026 	if (ctrl & CTRL_TFCE)
   13027 		ifmr->ifm_active |= IFM_FLOW | IFM_ETH_TXPAUSE;
   13028 }
   13029 
   13030 /* XXX TBI only */
   13031 static int
   13032 wm_check_for_link(struct wm_softc *sc)
   13033 {
   13034 	struct ifmedia_entry *ife = sc->sc_mii.mii_media.ifm_cur;
   13035 	uint32_t rxcw;
   13036 	uint32_t ctrl;
   13037 	uint32_t status;
   13038 	bool signal;
   13039 
   13040 	DPRINTF(sc, WM_DEBUG_LINK, ("%s: %s called\n",
   13041 		device_xname(sc->sc_dev), __func__));
   13042 
   13043 	if (sc->sc_mediatype == WM_MEDIATYPE_SERDES) {
   13044 		/* XXX need some work for >= 82571 */
   13045 		if (sc->sc_type >= WM_T_82571) {
   13046 			sc->sc_tbi_linkup = 1;
   13047 			return 0;
   13048 		}
   13049 	}
   13050 
   13051 	rxcw = CSR_READ(sc, WMREG_RXCW);
   13052 	ctrl = CSR_READ(sc, WMREG_CTRL);
   13053 	status = CSR_READ(sc, WMREG_STATUS);
   13054 	signal = wm_tbi_havesignal(sc, ctrl);
   13055 
   13056 	DPRINTF(sc, WM_DEBUG_LINK,
   13057 	    ("%s: %s: signal = %d, status_lu = %d, rxcw_c = %d\n",
   13058 		device_xname(sc->sc_dev), __func__, signal,
   13059 		((status & STATUS_LU) != 0), ((rxcw & RXCW_C) != 0)));
   13060 
   13061 	/*
   13062 	 * SWDPIN   LU RXCW
   13063 	 *	0    0	  0
   13064 	 *	0    0	  1	(should not happen)
   13065 	 *	0    1	  0	(should not happen)
   13066 	 *	0    1	  1	(should not happen)
   13067 	 *	1    0	  0	Disable autonego and force linkup
   13068 	 *	1    0	  1	got /C/ but not linkup yet
   13069 	 *	1    1	  0	(linkup)
   13070 	 *	1    1	  1	If IFM_AUTO, back to autonego
   13071 	 *
   13072 	 */
   13073 	if (signal && ((status & STATUS_LU) == 0) && ((rxcw & RXCW_C) == 0)) {
   13074 		DPRINTF(sc, WM_DEBUG_LINK,
   13075 		    ("%s: %s: force linkup and fullduplex\n",
   13076 			device_xname(sc->sc_dev), __func__));
   13077 		sc->sc_tbi_linkup = 0;
   13078 		/* Disable auto-negotiation in the TXCW register */
   13079 		CSR_WRITE(sc, WMREG_TXCW, (sc->sc_txcw & ~TXCW_ANE));
   13080 
   13081 		/*
   13082 		 * Force link-up and also force full-duplex.
   13083 		 *
   13084 		 * NOTE: CTRL was updated TFCE and RFCE automatically,
   13085 		 * so we should update sc->sc_ctrl
   13086 		 */
   13087 		sc->sc_ctrl = ctrl | CTRL_SLU | CTRL_FD;
   13088 		CSR_WRITE(sc, WMREG_CTRL, sc->sc_ctrl);
   13089 	} else if (((status & STATUS_LU) != 0)
   13090 	    && ((rxcw & RXCW_C) != 0)
   13091 	    && (IFM_SUBTYPE(ife->ifm_media) == IFM_AUTO)) {
   13092 		sc->sc_tbi_linkup = 1;
   13093 		DPRINTF(sc, WM_DEBUG_LINK, ("%s: %s: go back to autonego\n",
   13094 			device_xname(sc->sc_dev), __func__));
   13095 		CSR_WRITE(sc, WMREG_TXCW, sc->sc_txcw);
   13096 		CSR_WRITE(sc, WMREG_CTRL, (ctrl & ~CTRL_SLU));
   13097 	} else if (signal && ((rxcw & RXCW_C) != 0)) {
   13098 		DPRINTF(sc, WM_DEBUG_LINK, ("%s: %s: /C/",
   13099 			device_xname(sc->sc_dev), __func__));
   13100 	} else {
   13101 		DPRINTF(sc, WM_DEBUG_LINK, ("%s: %s: linkup %08x,%08x,%08x\n",
   13102 			device_xname(sc->sc_dev), __func__, rxcw, ctrl,
   13103 			status));
   13104 	}
   13105 
   13106 	return 0;
   13107 }
   13108 
   13109 /*
   13110  * wm_tbi_tick:
   13111  *
   13112  *	Check the link on TBI devices.
   13113  *	This function acts as mii_tick().
   13114  */
   13115 static void
   13116 wm_tbi_tick(struct wm_softc *sc)
   13117 {
   13118 	struct mii_data *mii = &sc->sc_mii;
   13119 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
   13120 	uint32_t status;
   13121 
   13122 	KASSERT(WM_CORE_LOCKED(sc));
   13123 
   13124 	status = CSR_READ(sc, WMREG_STATUS);
   13125 
   13126 	/* XXX is this needed? */
   13127 	(void)CSR_READ(sc, WMREG_RXCW);
   13128 	(void)CSR_READ(sc, WMREG_CTRL);
   13129 
   13130 	/* set link status */
   13131 	if ((status & STATUS_LU) == 0) {
   13132 		DPRINTF(sc, WM_DEBUG_LINK, ("%s: LINK: checklink -> down\n",
   13133 			device_xname(sc->sc_dev)));
   13134 		sc->sc_tbi_linkup = 0;
   13135 	} else if (sc->sc_tbi_linkup == 0) {
   13136 		DPRINTF(sc, WM_DEBUG_LINK, ("%s: LINK: checklink -> up %s\n",
   13137 			device_xname(sc->sc_dev),
   13138 			(status & STATUS_FD) ? "FDX" : "HDX"));
   13139 		sc->sc_tbi_linkup = 1;
   13140 		sc->sc_tbi_serdes_ticks = 0;
   13141 	}
   13142 
   13143 	if ((sc->sc_ethercom.ec_if.if_flags & IFF_UP) == 0)
   13144 		goto setled;
   13145 
   13146 	if ((status & STATUS_LU) == 0) {
   13147 		sc->sc_tbi_linkup = 0;
   13148 		/* If the timer expired, retry autonegotiation */
   13149 		if ((IFM_SUBTYPE(ife->ifm_media) == IFM_AUTO)
   13150 		    && (++sc->sc_tbi_serdes_ticks
   13151 			>= sc->sc_tbi_serdes_anegticks)) {
   13152 			DPRINTF(sc, WM_DEBUG_LINK, ("%s: %s: EXPIRE\n",
   13153 				device_xname(sc->sc_dev), __func__));
   13154 			sc->sc_tbi_serdes_ticks = 0;
   13155 			/*
   13156 			 * Reset the link, and let autonegotiation do
   13157 			 * its thing
   13158 			 */
   13159 			sc->sc_ctrl |= CTRL_LRST;
   13160 			CSR_WRITE(sc, WMREG_CTRL, sc->sc_ctrl);
   13161 			CSR_WRITE_FLUSH(sc);
   13162 			delay(1000);
   13163 			sc->sc_ctrl &= ~CTRL_LRST;
   13164 			CSR_WRITE(sc, WMREG_CTRL, sc->sc_ctrl);
   13165 			CSR_WRITE_FLUSH(sc);
   13166 			delay(1000);
   13167 			CSR_WRITE(sc, WMREG_TXCW,
   13168 			    sc->sc_txcw & ~TXCW_ANE);
   13169 			CSR_WRITE(sc, WMREG_TXCW, sc->sc_txcw);
   13170 		}
   13171 	}
   13172 
   13173 setled:
   13174 	wm_tbi_serdes_set_linkled(sc);
   13175 }
   13176 
   13177 /* SERDES related */
   13178 static void
   13179 wm_serdes_power_up_link_82575(struct wm_softc *sc)
   13180 {
   13181 	uint32_t reg;
   13182 
   13183 	if ((sc->sc_mediatype != WM_MEDIATYPE_SERDES)
   13184 	    && ((sc->sc_flags & WM_F_SGMII) == 0))
   13185 		return;
   13186 
   13187 	/* Enable PCS to turn on link */
   13188 	reg = CSR_READ(sc, WMREG_PCS_CFG);
   13189 	reg |= PCS_CFG_PCS_EN;
   13190 	CSR_WRITE(sc, WMREG_PCS_CFG, reg);
   13191 
   13192 	/* Power up the laser */
   13193 	reg = CSR_READ(sc, WMREG_CTRL_EXT);
   13194 	reg &= ~CTRL_EXT_SWDPIN(3);
   13195 	CSR_WRITE(sc, WMREG_CTRL_EXT, reg);
   13196 
   13197 	/* Flush the write to verify completion */
   13198 	CSR_WRITE_FLUSH(sc);
   13199 	delay(1000);
   13200 }
   13201 
   13202 static int
   13203 wm_serdes_mediachange(struct ifnet *ifp)
   13204 {
   13205 	struct wm_softc *sc = ifp->if_softc;
   13206 	bool pcs_autoneg = true; /* XXX */
   13207 	uint32_t ctrl_ext, pcs_lctl, reg;
   13208 
   13209 	if ((sc->sc_mediatype != WM_MEDIATYPE_SERDES)
   13210 	    && ((sc->sc_flags & WM_F_SGMII) == 0))
   13211 		return 0;
   13212 
   13213 	/* XXX Currently, this function is not called on 8257[12] */
   13214 	if ((sc->sc_type == WM_T_82571) || (sc->sc_type == WM_T_82572)
   13215 	    || (sc->sc_type >= WM_T_82575))
   13216 		CSR_WRITE(sc, WMREG_SCTL, SCTL_DISABLE_SERDES_LOOPBACK);
   13217 
   13218 	/* Power on the sfp cage if present */
   13219 	ctrl_ext = CSR_READ(sc, WMREG_CTRL_EXT);
   13220 	ctrl_ext &= ~CTRL_EXT_SWDPIN(3);
   13221 	ctrl_ext |= CTRL_EXT_I2C_ENA;
   13222 	CSR_WRITE(sc, WMREG_CTRL_EXT, ctrl_ext);
   13223 
   13224 	sc->sc_ctrl |= CTRL_SLU;
   13225 
   13226 	if ((sc->sc_type == WM_T_82575) || (sc->sc_type == WM_T_82576)) {
   13227 		sc->sc_ctrl |= CTRL_SWDPIN(0) | CTRL_SWDPIN(1);
   13228 
   13229 		reg = CSR_READ(sc, WMREG_CONNSW);
   13230 		reg |= CONNSW_ENRGSRC;
   13231 		CSR_WRITE(sc, WMREG_CONNSW, reg);
   13232 	}
   13233 
   13234 	pcs_lctl = CSR_READ(sc, WMREG_PCS_LCTL);
   13235 	switch (ctrl_ext & CTRL_EXT_LINK_MODE_MASK) {
   13236 	case CTRL_EXT_LINK_MODE_SGMII:
   13237 		/* SGMII mode lets the phy handle forcing speed/duplex */
   13238 		pcs_autoneg = true;
   13239 		/* Autoneg time out should be disabled for SGMII mode */
   13240 		pcs_lctl &= ~PCS_LCTL_AN_TIMEOUT;
   13241 		break;
   13242 	case CTRL_EXT_LINK_MODE_1000KX:
   13243 		pcs_autoneg = false;
   13244 		/* FALLTHROUGH */
   13245 	default:
   13246 		if ((sc->sc_type == WM_T_82575)
   13247 		    || (sc->sc_type == WM_T_82576)) {
   13248 			if ((sc->sc_flags & WM_F_PCS_DIS_AUTONEGO) != 0)
   13249 				pcs_autoneg = false;
   13250 		}
   13251 		sc->sc_ctrl |= CTRL_SPEED_1000 | CTRL_FRCSPD | CTRL_FD
   13252 		    | CTRL_FRCFDX;
   13253 
   13254 		/* Set speed of 1000/Full if speed/duplex is forced */
   13255 		pcs_lctl |= PCS_LCTL_FSV_1000 | PCS_LCTL_FDV_FULL;
   13256 	}
   13257 	CSR_WRITE(sc, WMREG_CTRL, sc->sc_ctrl);
   13258 
   13259 	pcs_lctl &= ~(PCS_LCTL_AN_ENABLE | PCS_LCTL_FLV_LINK_UP |
   13260 	    PCS_LCTL_FSD | PCS_LCTL_FORCE_LINK);
   13261 
   13262 	if (pcs_autoneg) {
   13263 		/* Set PCS register for autoneg */
   13264 		pcs_lctl |= PCS_LCTL_AN_ENABLE | PCS_LCTL_AN_RESTART;
   13265 
   13266 		/* Disable force flow control for autoneg */
   13267 		pcs_lctl &= ~PCS_LCTL_FORCE_FC;
   13268 
   13269 		/* Configure flow control advertisement for autoneg */
   13270 		reg = CSR_READ(sc, WMREG_PCS_ANADV);
   13271 		reg &= ~(TXCW_ASYM_PAUSE | TXCW_SYM_PAUSE);
   13272 		reg |= TXCW_ASYM_PAUSE | TXCW_SYM_PAUSE;
   13273 		CSR_WRITE(sc, WMREG_PCS_ANADV, reg);
   13274 	} else
   13275 		pcs_lctl |= PCS_LCTL_FSD | PCS_LCTL_FORCE_FC;
   13276 
   13277 	CSR_WRITE(sc, WMREG_PCS_LCTL, pcs_lctl);
   13278 
   13279 	return 0;
   13280 }
   13281 
   13282 static void
   13283 wm_serdes_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr)
   13284 {
   13285 	struct wm_softc *sc = ifp->if_softc;
   13286 	struct mii_data *mii = &sc->sc_mii;
   13287 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
   13288 	uint32_t pcs_adv, pcs_lpab, reg;
   13289 
   13290 	ifmr->ifm_status = IFM_AVALID;
   13291 	ifmr->ifm_active = IFM_ETHER;
   13292 
   13293 	/* Check PCS */
   13294 	reg = CSR_READ(sc, WMREG_PCS_LSTS);
   13295 	if ((reg & PCS_LSTS_LINKOK) == 0) {
   13296 		ifmr->ifm_active |= IFM_NONE;
   13297 		sc->sc_tbi_linkup = 0;
   13298 		goto setled;
   13299 	}
   13300 
   13301 	sc->sc_tbi_linkup = 1;
   13302 	ifmr->ifm_status |= IFM_ACTIVE;
   13303 	if (sc->sc_type == WM_T_I354) {
   13304 		uint32_t status;
   13305 
   13306 		status = CSR_READ(sc, WMREG_STATUS);
   13307 		if (((status & STATUS_2P5_SKU) != 0)
   13308 		    && ((status & STATUS_2P5_SKU_OVER) == 0)) {
   13309 			ifmr->ifm_active |= IFM_2500_KX;
   13310 		} else
   13311 			ifmr->ifm_active |= IFM_1000_KX;
   13312 	} else {
   13313 		switch (__SHIFTOUT(reg, PCS_LSTS_SPEED)) {
   13314 		case PCS_LSTS_SPEED_10:
   13315 			ifmr->ifm_active |= IFM_10_T; /* XXX */
   13316 			break;
   13317 		case PCS_LSTS_SPEED_100:
   13318 			ifmr->ifm_active |= IFM_100_FX; /* XXX */
   13319 			break;
   13320 		case PCS_LSTS_SPEED_1000:
   13321 			ifmr->ifm_active |= IFM_1000_SX; /* XXX */
   13322 			break;
   13323 		default:
   13324 			device_printf(sc->sc_dev, "Unknown speed\n");
   13325 			ifmr->ifm_active |= IFM_1000_SX; /* XXX */
   13326 			break;
   13327 		}
   13328 	}
   13329 	ifp->if_baudrate = ifmedia_baudrate(ifmr->ifm_active);
   13330 	if ((reg & PCS_LSTS_FDX) != 0)
   13331 		ifmr->ifm_active |= IFM_FDX;
   13332 	else
   13333 		ifmr->ifm_active |= IFM_HDX;
   13334 	mii->mii_media_active &= ~IFM_ETH_FMASK;
   13335 	if (IFM_SUBTYPE(ife->ifm_media) == IFM_AUTO) {
   13336 		/* Check flow */
   13337 		reg = CSR_READ(sc, WMREG_PCS_LSTS);
   13338 		if ((reg & PCS_LSTS_AN_COMP) == 0) {
   13339 			DPRINTF(sc, WM_DEBUG_LINK,
   13340 			    ("XXX LINKOK but not ACOMP\n"));
   13341 			goto setled;
   13342 		}
   13343 		pcs_adv = CSR_READ(sc, WMREG_PCS_ANADV);
   13344 		pcs_lpab = CSR_READ(sc, WMREG_PCS_LPAB);
   13345 		DPRINTF(sc, WM_DEBUG_LINK,
   13346 		    ("XXX AN result(2) %08x, %08x\n", pcs_adv, pcs_lpab));
   13347 		if ((pcs_adv & TXCW_SYM_PAUSE)
   13348 		    && (pcs_lpab & TXCW_SYM_PAUSE)) {
   13349 			mii->mii_media_active |= IFM_FLOW
   13350 			    | IFM_ETH_TXPAUSE | IFM_ETH_RXPAUSE;
   13351 		} else if (((pcs_adv & TXCW_SYM_PAUSE) == 0)
   13352 		    && (pcs_adv & TXCW_ASYM_PAUSE)
   13353 		    && (pcs_lpab & TXCW_SYM_PAUSE)
   13354 		    && (pcs_lpab & TXCW_ASYM_PAUSE)) {
   13355 			mii->mii_media_active |= IFM_FLOW
   13356 			    | IFM_ETH_TXPAUSE;
   13357 		} else if ((pcs_adv & TXCW_SYM_PAUSE)
   13358 		    && (pcs_adv & TXCW_ASYM_PAUSE)
   13359 		    && ((pcs_lpab & TXCW_SYM_PAUSE) == 0)
   13360 		    && (pcs_lpab & TXCW_ASYM_PAUSE)) {
   13361 			mii->mii_media_active |= IFM_FLOW
   13362 			    | IFM_ETH_RXPAUSE;
   13363 		}
   13364 	}
   13365 	ifmr->ifm_active = (ifmr->ifm_active & ~IFM_ETH_FMASK)
   13366 	    | (mii->mii_media_active & IFM_ETH_FMASK);
   13367 setled:
   13368 	wm_tbi_serdes_set_linkled(sc);
   13369 }
   13370 
   13371 /*
   13372  * wm_serdes_tick:
   13373  *
   13374  *	Check the link on serdes devices.
   13375  */
   13376 static void
   13377 wm_serdes_tick(struct wm_softc *sc)
   13378 {
   13379 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   13380 	struct mii_data *mii = &sc->sc_mii;
   13381 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
   13382 	uint32_t reg;
   13383 
   13384 	KASSERT(WM_CORE_LOCKED(sc));
   13385 
   13386 	mii->mii_media_status = IFM_AVALID;
   13387 	mii->mii_media_active = IFM_ETHER;
   13388 
   13389 	/* Check PCS */
   13390 	reg = CSR_READ(sc, WMREG_PCS_LSTS);
   13391 	if ((reg & PCS_LSTS_LINKOK) != 0) {
   13392 		mii->mii_media_status |= IFM_ACTIVE;
   13393 		sc->sc_tbi_linkup = 1;
   13394 		sc->sc_tbi_serdes_ticks = 0;
   13395 		mii->mii_media_active |= IFM_1000_SX; /* XXX */
   13396 		if ((reg & PCS_LSTS_FDX) != 0)
   13397 			mii->mii_media_active |= IFM_FDX;
   13398 		else
   13399 			mii->mii_media_active |= IFM_HDX;
   13400 	} else {
   13401 		mii->mii_media_status |= IFM_NONE;
   13402 		sc->sc_tbi_linkup = 0;
   13403 		/* If the timer expired, retry autonegotiation */
   13404 		if ((IFM_SUBTYPE(ife->ifm_media) == IFM_AUTO)
   13405 		    && (++sc->sc_tbi_serdes_ticks
   13406 			>= sc->sc_tbi_serdes_anegticks)) {
   13407 			DPRINTF(sc, WM_DEBUG_LINK, ("%s: %s: EXPIRE\n",
   13408 				device_xname(sc->sc_dev), __func__));
   13409 			sc->sc_tbi_serdes_ticks = 0;
   13410 			/* XXX */
   13411 			wm_serdes_mediachange(ifp);
   13412 		}
   13413 	}
   13414 
   13415 	wm_tbi_serdes_set_linkled(sc);
   13416 }
   13417 
   13418 /* SFP related */
   13419 
   13420 static int
   13421 wm_sfp_read_data_byte(struct wm_softc *sc, uint16_t offset, uint8_t *data)
   13422 {
   13423 	uint32_t i2ccmd;
   13424 	int i;
   13425 
   13426 	i2ccmd = (offset << I2CCMD_REG_ADDR_SHIFT) | I2CCMD_OPCODE_READ;
   13427 	CSR_WRITE(sc, WMREG_I2CCMD, i2ccmd);
   13428 
   13429 	/* Poll the ready bit */
   13430 	for (i = 0; i < I2CCMD_PHY_TIMEOUT; i++) {
   13431 		delay(50);
   13432 		i2ccmd = CSR_READ(sc, WMREG_I2CCMD);
   13433 		if (i2ccmd & I2CCMD_READY)
   13434 			break;
   13435 	}
   13436 	if ((i2ccmd & I2CCMD_READY) == 0)
   13437 		return -1;
   13438 	if ((i2ccmd & I2CCMD_ERROR) != 0)
   13439 		return -1;
   13440 
   13441 	*data = i2ccmd & 0x00ff;
   13442 
   13443 	return 0;
   13444 }
   13445 
   13446 static uint32_t
   13447 wm_sfp_get_media_type(struct wm_softc *sc)
   13448 {
   13449 	uint32_t ctrl_ext;
   13450 	uint8_t val = 0;
   13451 	int timeout = 3;
   13452 	uint32_t mediatype = WM_MEDIATYPE_UNKNOWN;
   13453 	int rv = -1;
   13454 
   13455 	ctrl_ext = CSR_READ(sc, WMREG_CTRL_EXT);
   13456 	ctrl_ext &= ~CTRL_EXT_SWDPIN(3);
   13457 	CSR_WRITE(sc, WMREG_CTRL_EXT, ctrl_ext | CTRL_EXT_I2C_ENA);
   13458 	CSR_WRITE_FLUSH(sc);
   13459 
   13460 	/* Read SFP module data */
   13461 	while (timeout) {
   13462 		rv = wm_sfp_read_data_byte(sc, SFF_SFP_ID_OFF, &val);
   13463 		if (rv == 0)
   13464 			break;
   13465 		delay(100*1000); /* XXX too big */
   13466 		timeout--;
   13467 	}
   13468 	if (rv != 0)
   13469 		goto out;
   13470 
   13471 	switch (val) {
   13472 	case SFF_SFP_ID_SFF:
   13473 		aprint_normal_dev(sc->sc_dev,
   13474 		    "Module/Connector soldered to board\n");
   13475 		break;
   13476 	case SFF_SFP_ID_SFP:
   13477 		sc->sc_flags |= WM_F_SFP;
   13478 		break;
   13479 	case SFF_SFP_ID_UNKNOWN:
   13480 		goto out;
   13481 	default:
   13482 		break;
   13483 	}
   13484 
   13485 	rv = wm_sfp_read_data_byte(sc, SFF_SFP_ETH_FLAGS_OFF, &val);
   13486 	if (rv != 0)
   13487 		goto out;
   13488 
   13489 	sc->sc_sfptype = val;
   13490 	if ((val & (SFF_SFP_ETH_FLAGS_1000SX | SFF_SFP_ETH_FLAGS_1000LX)) != 0)
   13491 		mediatype = WM_MEDIATYPE_SERDES;
   13492 	else if ((val & SFF_SFP_ETH_FLAGS_1000T) != 0) {
   13493 		sc->sc_flags |= WM_F_SGMII;
   13494 		mediatype = WM_MEDIATYPE_COPPER;
   13495 	} else if ((val & SFF_SFP_ETH_FLAGS_100FX) != 0) {
   13496 		sc->sc_flags |= WM_F_SGMII;
   13497 		mediatype = WM_MEDIATYPE_SERDES;
   13498 	} else {
   13499 		device_printf(sc->sc_dev, "%s: unknown media type? (0x%hhx)\n",
   13500 		    __func__, sc->sc_sfptype);
   13501 		sc->sc_sfptype = 0; /* XXX unknown */
   13502 	}
   13503 
   13504 out:
   13505 	/* Restore I2C interface setting */
   13506 	CSR_WRITE(sc, WMREG_CTRL_EXT, ctrl_ext);
   13507 
   13508 	return mediatype;
   13509 }
   13510 
   13511 /*
   13512  * NVM related.
   13513  * Microwire, SPI (w/wo EERD) and Flash.
   13514  */
   13515 
   13516 /* Both spi and uwire */
   13517 
   13518 /*
   13519  * wm_eeprom_sendbits:
   13520  *
   13521  *	Send a series of bits to the EEPROM.
   13522  */
   13523 static void
   13524 wm_eeprom_sendbits(struct wm_softc *sc, uint32_t bits, int nbits)
   13525 {
   13526 	uint32_t reg;
   13527 	int x;
   13528 
   13529 	reg = CSR_READ(sc, WMREG_EECD);
   13530 
   13531 	for (x = nbits; x > 0; x--) {
   13532 		if (bits & (1U << (x - 1)))
   13533 			reg |= EECD_DI;
   13534 		else
   13535 			reg &= ~EECD_DI;
   13536 		CSR_WRITE(sc, WMREG_EECD, reg);
   13537 		CSR_WRITE_FLUSH(sc);
   13538 		delay(2);
   13539 		CSR_WRITE(sc, WMREG_EECD, reg | EECD_SK);
   13540 		CSR_WRITE_FLUSH(sc);
   13541 		delay(2);
   13542 		CSR_WRITE(sc, WMREG_EECD, reg);
   13543 		CSR_WRITE_FLUSH(sc);
   13544 		delay(2);
   13545 	}
   13546 }
   13547 
   13548 /*
   13549  * wm_eeprom_recvbits:
   13550  *
   13551  *	Receive a series of bits from the EEPROM.
   13552  */
   13553 static void
   13554 wm_eeprom_recvbits(struct wm_softc *sc, uint32_t *valp, int nbits)
   13555 {
   13556 	uint32_t reg, val;
   13557 	int x;
   13558 
   13559 	reg = CSR_READ(sc, WMREG_EECD) & ~EECD_DI;
   13560 
   13561 	val = 0;
   13562 	for (x = nbits; x > 0; x--) {
   13563 		CSR_WRITE(sc, WMREG_EECD, reg | EECD_SK);
   13564 		CSR_WRITE_FLUSH(sc);
   13565 		delay(2);
   13566 		if (CSR_READ(sc, WMREG_EECD) & EECD_DO)
   13567 			val |= (1U << (x - 1));
   13568 		CSR_WRITE(sc, WMREG_EECD, reg);
   13569 		CSR_WRITE_FLUSH(sc);
   13570 		delay(2);
   13571 	}
   13572 	*valp = val;
   13573 }
   13574 
   13575 /* Microwire */
   13576 
   13577 /*
   13578  * wm_nvm_read_uwire:
   13579  *
   13580  *	Read a word from the EEPROM using the MicroWire protocol.
   13581  */
   13582 static int
   13583 wm_nvm_read_uwire(struct wm_softc *sc, int word, int wordcnt, uint16_t *data)
   13584 {
   13585 	uint32_t reg, val;
   13586 	int i;
   13587 
   13588 	DPRINTF(sc, WM_DEBUG_NVM, ("%s: %s called\n",
   13589 		device_xname(sc->sc_dev), __func__));
   13590 
   13591 	if (sc->nvm.acquire(sc) != 0)
   13592 		return -1;
   13593 
   13594 	for (i = 0; i < wordcnt; i++) {
   13595 		/* Clear SK and DI. */
   13596 		reg = CSR_READ(sc, WMREG_EECD) & ~(EECD_SK | EECD_DI);
   13597 		CSR_WRITE(sc, WMREG_EECD, reg);
   13598 
   13599 		/*
   13600 		 * XXX: workaround for a bug in qemu-0.12.x and prior
   13601 		 * and Xen.
   13602 		 *
   13603 		 * We use this workaround only for 82540 because qemu's
   13604 		 * e1000 act as 82540.
   13605 		 */
   13606 		if (sc->sc_type == WM_T_82540) {
   13607 			reg |= EECD_SK;
   13608 			CSR_WRITE(sc, WMREG_EECD, reg);
   13609 			reg &= ~EECD_SK;
   13610 			CSR_WRITE(sc, WMREG_EECD, reg);
   13611 			CSR_WRITE_FLUSH(sc);
   13612 			delay(2);
   13613 		}
   13614 		/* XXX: end of workaround */
   13615 
   13616 		/* Set CHIP SELECT. */
   13617 		reg |= EECD_CS;
   13618 		CSR_WRITE(sc, WMREG_EECD, reg);
   13619 		CSR_WRITE_FLUSH(sc);
   13620 		delay(2);
   13621 
   13622 		/* Shift in the READ command. */
   13623 		wm_eeprom_sendbits(sc, UWIRE_OPC_READ, 3);
   13624 
   13625 		/* Shift in address. */
   13626 		wm_eeprom_sendbits(sc, word + i, sc->sc_nvm_addrbits);
   13627 
   13628 		/* Shift out the data. */
   13629 		wm_eeprom_recvbits(sc, &val, 16);
   13630 		data[i] = val & 0xffff;
   13631 
   13632 		/* Clear CHIP SELECT. */
   13633 		reg = CSR_READ(sc, WMREG_EECD) & ~EECD_CS;
   13634 		CSR_WRITE(sc, WMREG_EECD, reg);
   13635 		CSR_WRITE_FLUSH(sc);
   13636 		delay(2);
   13637 	}
   13638 
   13639 	sc->nvm.release(sc);
   13640 	return 0;
   13641 }
   13642 
   13643 /* SPI */
   13644 
   13645 /*
   13646  * Set SPI and FLASH related information from the EECD register.
   13647  * For 82541 and 82547, the word size is taken from EEPROM.
   13648  */
   13649 static int
   13650 wm_nvm_set_addrbits_size_eecd(struct wm_softc *sc)
   13651 {
   13652 	int size;
   13653 	uint32_t reg;
   13654 	uint16_t data;
   13655 
   13656 	reg = CSR_READ(sc, WMREG_EECD);
   13657 	sc->sc_nvm_addrbits = (reg & EECD_EE_ABITS) ? 16 : 8;
   13658 
   13659 	/* Read the size of NVM from EECD by default */
   13660 	size = __SHIFTOUT(reg, EECD_EE_SIZE_EX_MASK);
   13661 	switch (sc->sc_type) {
   13662 	case WM_T_82541:
   13663 	case WM_T_82541_2:
   13664 	case WM_T_82547:
   13665 	case WM_T_82547_2:
   13666 		/* Set dummy value to access EEPROM */
   13667 		sc->sc_nvm_wordsize = 64;
   13668 		if (wm_nvm_read(sc, NVM_OFF_EEPROM_SIZE, 1, &data) != 0) {
   13669 			aprint_error_dev(sc->sc_dev,
   13670 			    "%s: failed to read EEPROM size\n", __func__);
   13671 		}
   13672 		reg = data;
   13673 		size = __SHIFTOUT(reg, EECD_EE_SIZE_EX_MASK);
   13674 		if (size == 0)
   13675 			size = 6; /* 64 word size */
   13676 		else
   13677 			size += NVM_WORD_SIZE_BASE_SHIFT + 1;
   13678 		break;
   13679 	case WM_T_80003:
   13680 	case WM_T_82571:
   13681 	case WM_T_82572:
   13682 	case WM_T_82573: /* SPI case */
   13683 	case WM_T_82574: /* SPI case */
   13684 	case WM_T_82583: /* SPI case */
   13685 		size += NVM_WORD_SIZE_BASE_SHIFT;
   13686 		if (size > 14)
   13687 			size = 14;
   13688 		break;
   13689 	case WM_T_82575:
   13690 	case WM_T_82576:
   13691 	case WM_T_82580:
   13692 	case WM_T_I350:
   13693 	case WM_T_I354:
   13694 	case WM_T_I210:
   13695 	case WM_T_I211:
   13696 		size += NVM_WORD_SIZE_BASE_SHIFT;
   13697 		if (size > 15)
   13698 			size = 15;
   13699 		break;
   13700 	default:
   13701 		aprint_error_dev(sc->sc_dev,
   13702 		    "%s: unknown device(%d)?\n", __func__, sc->sc_type);
   13703 		return -1;
   13704 		break;
   13705 	}
   13706 
   13707 	sc->sc_nvm_wordsize = 1 << size;
   13708 
   13709 	return 0;
   13710 }
   13711 
   13712 /*
   13713  * wm_nvm_ready_spi:
   13714  *
   13715  *	Wait for a SPI EEPROM to be ready for commands.
   13716  */
   13717 static int
   13718 wm_nvm_ready_spi(struct wm_softc *sc)
   13719 {
   13720 	uint32_t val;
   13721 	int usec;
   13722 
   13723 	DPRINTF(sc, WM_DEBUG_NVM, ("%s: %s called\n",
   13724 		device_xname(sc->sc_dev), __func__));
   13725 
   13726 	for (usec = 0; usec < SPI_MAX_RETRIES; delay(5), usec += 5) {
   13727 		wm_eeprom_sendbits(sc, SPI_OPC_RDSR, 8);
   13728 		wm_eeprom_recvbits(sc, &val, 8);
   13729 		if ((val & SPI_SR_RDY) == 0)
   13730 			break;
   13731 	}
   13732 	if (usec >= SPI_MAX_RETRIES) {
   13733 		aprint_error_dev(sc->sc_dev,"EEPROM failed to become ready\n");
   13734 		return -1;
   13735 	}
   13736 	return 0;
   13737 }
   13738 
   13739 /*
   13740  * wm_nvm_read_spi:
   13741  *
   13742  *	Read a work from the EEPROM using the SPI protocol.
   13743  */
   13744 static int
   13745 wm_nvm_read_spi(struct wm_softc *sc, int word, int wordcnt, uint16_t *data)
   13746 {
   13747 	uint32_t reg, val;
   13748 	int i;
   13749 	uint8_t opc;
   13750 	int rv = 0;
   13751 
   13752 	DPRINTF(sc, WM_DEBUG_NVM, ("%s: %s called\n",
   13753 		device_xname(sc->sc_dev), __func__));
   13754 
   13755 	if (sc->nvm.acquire(sc) != 0)
   13756 		return -1;
   13757 
   13758 	/* Clear SK and CS. */
   13759 	reg = CSR_READ(sc, WMREG_EECD) & ~(EECD_SK | EECD_CS);
   13760 	CSR_WRITE(sc, WMREG_EECD, reg);
   13761 	CSR_WRITE_FLUSH(sc);
   13762 	delay(2);
   13763 
   13764 	if ((rv = wm_nvm_ready_spi(sc)) != 0)
   13765 		goto out;
   13766 
   13767 	/* Toggle CS to flush commands. */
   13768 	CSR_WRITE(sc, WMREG_EECD, reg | EECD_CS);
   13769 	CSR_WRITE_FLUSH(sc);
   13770 	delay(2);
   13771 	CSR_WRITE(sc, WMREG_EECD, reg);
   13772 	CSR_WRITE_FLUSH(sc);
   13773 	delay(2);
   13774 
   13775 	opc = SPI_OPC_READ;
   13776 	if (sc->sc_nvm_addrbits == 8 && word >= 128)
   13777 		opc |= SPI_OPC_A8;
   13778 
   13779 	wm_eeprom_sendbits(sc, opc, 8);
   13780 	wm_eeprom_sendbits(sc, word << 1, sc->sc_nvm_addrbits);
   13781 
   13782 	for (i = 0; i < wordcnt; i++) {
   13783 		wm_eeprom_recvbits(sc, &val, 16);
   13784 		data[i] = ((val >> 8) & 0xff) | ((val & 0xff) << 8);
   13785 	}
   13786 
   13787 	/* Raise CS and clear SK. */
   13788 	reg = (CSR_READ(sc, WMREG_EECD) & ~EECD_SK) | EECD_CS;
   13789 	CSR_WRITE(sc, WMREG_EECD, reg);
   13790 	CSR_WRITE_FLUSH(sc);
   13791 	delay(2);
   13792 
   13793 out:
   13794 	sc->nvm.release(sc);
   13795 	return rv;
   13796 }
   13797 
   13798 /* Using with EERD */
   13799 
   13800 static int
   13801 wm_poll_eerd_eewr_done(struct wm_softc *sc, int rw)
   13802 {
   13803 	uint32_t attempts = 100000;
   13804 	uint32_t i, reg = 0;
   13805 	int32_t done = -1;
   13806 
   13807 	for (i = 0; i < attempts; i++) {
   13808 		reg = CSR_READ(sc, rw);
   13809 
   13810 		if (reg & EERD_DONE) {
   13811 			done = 0;
   13812 			break;
   13813 		}
   13814 		delay(5);
   13815 	}
   13816 
   13817 	return done;
   13818 }
   13819 
   13820 static int
   13821 wm_nvm_read_eerd(struct wm_softc *sc, int offset, int wordcnt, uint16_t *data)
   13822 {
   13823 	int i, eerd = 0;
   13824 	int rv = 0;
   13825 
   13826 	DPRINTF(sc, WM_DEBUG_NVM, ("%s: %s called\n",
   13827 		device_xname(sc->sc_dev), __func__));
   13828 
   13829 	if (sc->nvm.acquire(sc) != 0)
   13830 		return -1;
   13831 
   13832 	for (i = 0; i < wordcnt; i++) {
   13833 		eerd = ((offset + i) << EERD_ADDR_SHIFT) | EERD_START;
   13834 		CSR_WRITE(sc, WMREG_EERD, eerd);
   13835 		rv = wm_poll_eerd_eewr_done(sc, WMREG_EERD);
   13836 		if (rv != 0) {
   13837 			aprint_error_dev(sc->sc_dev, "EERD polling failed: "
   13838 			    "offset=%d. wordcnt=%d\n", offset, wordcnt);
   13839 			break;
   13840 		}
   13841 		data[i] = (CSR_READ(sc, WMREG_EERD) >> EERD_DATA_SHIFT);
   13842 	}
   13843 
   13844 	sc->nvm.release(sc);
   13845 	return rv;
   13846 }
   13847 
   13848 /* Flash */
   13849 
   13850 static int
   13851 wm_nvm_valid_bank_detect_ich8lan(struct wm_softc *sc, unsigned int *bank)
   13852 {
   13853 	uint32_t eecd;
   13854 	uint32_t act_offset = ICH_NVM_SIG_WORD * 2 + 1;
   13855 	uint32_t bank1_offset = sc->sc_ich8_flash_bank_size * sizeof(uint16_t);
   13856 	uint32_t nvm_dword = 0;
   13857 	uint8_t sig_byte = 0;
   13858 	int rv;
   13859 
   13860 	switch (sc->sc_type) {
   13861 	case WM_T_PCH_SPT:
   13862 	case WM_T_PCH_CNP:
   13863 		bank1_offset = sc->sc_ich8_flash_bank_size * 2;
   13864 		act_offset = ICH_NVM_SIG_WORD * 2;
   13865 
   13866 		/* Set bank to 0 in case flash read fails. */
   13867 		*bank = 0;
   13868 
   13869 		/* Check bank 0 */
   13870 		rv = wm_read_ich8_dword(sc, act_offset, &nvm_dword);
   13871 		if (rv != 0)
   13872 			return rv;
   13873 		sig_byte = (uint8_t)((nvm_dword & 0xFF00) >> 8);
   13874 		if ((sig_byte & ICH_NVM_VALID_SIG_MASK) == ICH_NVM_SIG_VALUE) {
   13875 			*bank = 0;
   13876 			return 0;
   13877 		}
   13878 
   13879 		/* Check bank 1 */
   13880 		rv = wm_read_ich8_dword(sc, act_offset + bank1_offset,
   13881 		    &nvm_dword);
   13882 		sig_byte = (uint8_t)((nvm_dword & 0xFF00) >> 8);
   13883 		if ((sig_byte & ICH_NVM_VALID_SIG_MASK) == ICH_NVM_SIG_VALUE) {
   13884 			*bank = 1;
   13885 			return 0;
   13886 		}
   13887 		aprint_error_dev(sc->sc_dev,
   13888 		    "%s: no valid NVM bank present (%u)\n", __func__, *bank);
   13889 		return -1;
   13890 	case WM_T_ICH8:
   13891 	case WM_T_ICH9:
   13892 		eecd = CSR_READ(sc, WMREG_EECD);
   13893 		if ((eecd & EECD_SEC1VAL_VALMASK) == EECD_SEC1VAL_VALMASK) {
   13894 			*bank = ((eecd & EECD_SEC1VAL) != 0) ? 1 : 0;
   13895 			return 0;
   13896 		}
   13897 		/* FALLTHROUGH */
   13898 	default:
   13899 		/* Default to 0 */
   13900 		*bank = 0;
   13901 
   13902 		/* Check bank 0 */
   13903 		wm_read_ich8_byte(sc, act_offset, &sig_byte);
   13904 		if ((sig_byte & ICH_NVM_VALID_SIG_MASK) == ICH_NVM_SIG_VALUE) {
   13905 			*bank = 0;
   13906 			return 0;
   13907 		}
   13908 
   13909 		/* Check bank 1 */
   13910 		wm_read_ich8_byte(sc, act_offset + bank1_offset,
   13911 		    &sig_byte);
   13912 		if ((sig_byte & ICH_NVM_VALID_SIG_MASK) == ICH_NVM_SIG_VALUE) {
   13913 			*bank = 1;
   13914 			return 0;
   13915 		}
   13916 	}
   13917 
   13918 	DPRINTF(sc, WM_DEBUG_NVM, ("%s: No valid NVM bank present\n",
   13919 		device_xname(sc->sc_dev)));
   13920 	return -1;
   13921 }
   13922 
   13923 /******************************************************************************
   13924  * This function does initial flash setup so that a new read/write/erase cycle
   13925  * can be started.
   13926  *
   13927  * sc - The pointer to the hw structure
   13928  ****************************************************************************/
   13929 static int32_t
   13930 wm_ich8_cycle_init(struct wm_softc *sc)
   13931 {
   13932 	uint16_t hsfsts;
   13933 	int32_t error = 1;
   13934 	int32_t i     = 0;
   13935 
   13936 	if (sc->sc_type >= WM_T_PCH_SPT)
   13937 		hsfsts = ICH8_FLASH_READ32(sc, ICH_FLASH_HSFSTS) & 0xffffUL;
   13938 	else
   13939 		hsfsts = ICH8_FLASH_READ16(sc, ICH_FLASH_HSFSTS);
   13940 
   13941 	/* May be check the Flash Des Valid bit in Hw status */
   13942 	if ((hsfsts & HSFSTS_FLDVAL) == 0)
   13943 		return error;
   13944 
   13945 	/* Clear FCERR in Hw status by writing 1 */
   13946 	/* Clear DAEL in Hw status by writing a 1 */
   13947 	hsfsts |= HSFSTS_ERR | HSFSTS_DAEL;
   13948 
   13949 	if (sc->sc_type >= WM_T_PCH_SPT)
   13950 		ICH8_FLASH_WRITE32(sc, ICH_FLASH_HSFSTS, hsfsts & 0xffffUL);
   13951 	else
   13952 		ICH8_FLASH_WRITE16(sc, ICH_FLASH_HSFSTS, hsfsts);
   13953 
   13954 	/*
   13955 	 * Either we should have a hardware SPI cycle in progress bit to check
   13956 	 * against, in order to start a new cycle or FDONE bit should be
   13957 	 * changed in the hardware so that it is 1 after hardware reset, which
   13958 	 * can then be used as an indication whether a cycle is in progress or
   13959 	 * has been completed .. we should also have some software semaphore
   13960 	 * mechanism to guard FDONE or the cycle in progress bit so that two
   13961 	 * threads access to those bits can be sequentiallized or a way so that
   13962 	 * 2 threads don't start the cycle at the same time
   13963 	 */
   13964 
   13965 	if ((hsfsts & HSFSTS_FLINPRO) == 0) {
   13966 		/*
   13967 		 * There is no cycle running at present, so we can start a
   13968 		 * cycle
   13969 		 */
   13970 
   13971 		/* Begin by setting Flash Cycle Done. */
   13972 		hsfsts |= HSFSTS_DONE;
   13973 		if (sc->sc_type >= WM_T_PCH_SPT)
   13974 			ICH8_FLASH_WRITE32(sc, ICH_FLASH_HSFSTS,
   13975 			    hsfsts & 0xffffUL);
   13976 		else
   13977 			ICH8_FLASH_WRITE16(sc, ICH_FLASH_HSFSTS, hsfsts);
   13978 		error = 0;
   13979 	} else {
   13980 		/*
   13981 		 * Otherwise poll for sometime so the current cycle has a
   13982 		 * chance to end before giving up.
   13983 		 */
   13984 		for (i = 0; i < ICH_FLASH_COMMAND_TIMEOUT; i++) {
   13985 			if (sc->sc_type >= WM_T_PCH_SPT)
   13986 				hsfsts = ICH8_FLASH_READ32(sc,
   13987 				    ICH_FLASH_HSFSTS) & 0xffffUL;
   13988 			else
   13989 				hsfsts = ICH8_FLASH_READ16(sc,
   13990 				    ICH_FLASH_HSFSTS);
   13991 			if ((hsfsts & HSFSTS_FLINPRO) == 0) {
   13992 				error = 0;
   13993 				break;
   13994 			}
   13995 			delay(1);
   13996 		}
   13997 		if (error == 0) {
   13998 			/*
   13999 			 * Successful in waiting for previous cycle to timeout,
   14000 			 * now set the Flash Cycle Done.
   14001 			 */
   14002 			hsfsts |= HSFSTS_DONE;
   14003 			if (sc->sc_type >= WM_T_PCH_SPT)
   14004 				ICH8_FLASH_WRITE32(sc, ICH_FLASH_HSFSTS,
   14005 				    hsfsts & 0xffffUL);
   14006 			else
   14007 				ICH8_FLASH_WRITE16(sc, ICH_FLASH_HSFSTS,
   14008 				    hsfsts);
   14009 		}
   14010 	}
   14011 	return error;
   14012 }
   14013 
   14014 /******************************************************************************
   14015  * This function starts a flash cycle and waits for its completion
   14016  *
   14017  * sc - The pointer to the hw structure
   14018  ****************************************************************************/
   14019 static int32_t
   14020 wm_ich8_flash_cycle(struct wm_softc *sc, uint32_t timeout)
   14021 {
   14022 	uint16_t hsflctl;
   14023 	uint16_t hsfsts;
   14024 	int32_t error = 1;
   14025 	uint32_t i = 0;
   14026 
   14027 	/* Start a cycle by writing 1 in Flash Cycle Go in Hw Flash Control */
   14028 	if (sc->sc_type >= WM_T_PCH_SPT)
   14029 		hsflctl = ICH8_FLASH_READ32(sc, ICH_FLASH_HSFSTS) >> 16;
   14030 	else
   14031 		hsflctl = ICH8_FLASH_READ16(sc, ICH_FLASH_HSFCTL);
   14032 	hsflctl |= HSFCTL_GO;
   14033 	if (sc->sc_type >= WM_T_PCH_SPT)
   14034 		ICH8_FLASH_WRITE32(sc, ICH_FLASH_HSFSTS,
   14035 		    (uint32_t)hsflctl << 16);
   14036 	else
   14037 		ICH8_FLASH_WRITE16(sc, ICH_FLASH_HSFCTL, hsflctl);
   14038 
   14039 	/* Wait till FDONE bit is set to 1 */
   14040 	do {
   14041 		if (sc->sc_type >= WM_T_PCH_SPT)
   14042 			hsfsts = ICH8_FLASH_READ32(sc, ICH_FLASH_HSFSTS)
   14043 			    & 0xffffUL;
   14044 		else
   14045 			hsfsts = ICH8_FLASH_READ16(sc, ICH_FLASH_HSFSTS);
   14046 		if (hsfsts & HSFSTS_DONE)
   14047 			break;
   14048 		delay(1);
   14049 		i++;
   14050 	} while (i < timeout);
   14051 	if ((hsfsts & HSFSTS_DONE) == 1 && (hsfsts & HSFSTS_ERR) == 0)
   14052 		error = 0;
   14053 
   14054 	return error;
   14055 }
   14056 
   14057 /******************************************************************************
   14058  * Reads a byte or (d)word from the NVM using the ICH8 flash access registers.
   14059  *
   14060  * sc - The pointer to the hw structure
   14061  * index - The index of the byte or word to read.
   14062  * size - Size of data to read, 1=byte 2=word, 4=dword
   14063  * data - Pointer to the word to store the value read.
   14064  *****************************************************************************/
   14065 static int32_t
   14066 wm_read_ich8_data(struct wm_softc *sc, uint32_t index,
   14067     uint32_t size, uint32_t *data)
   14068 {
   14069 	uint16_t hsfsts;
   14070 	uint16_t hsflctl;
   14071 	uint32_t flash_linear_address;
   14072 	uint32_t flash_data = 0;
   14073 	int32_t error = 1;
   14074 	int32_t count = 0;
   14075 
   14076 	if (size < 1  || size > 4 || data == 0x0 ||
   14077 	    index > ICH_FLASH_LINEAR_ADDR_MASK)
   14078 		return error;
   14079 
   14080 	flash_linear_address = (ICH_FLASH_LINEAR_ADDR_MASK & index) +
   14081 	    sc->sc_ich8_flash_base;
   14082 
   14083 	do {
   14084 		delay(1);
   14085 		/* Steps */
   14086 		error = wm_ich8_cycle_init(sc);
   14087 		if (error)
   14088 			break;
   14089 
   14090 		if (sc->sc_type >= WM_T_PCH_SPT)
   14091 			hsflctl = ICH8_FLASH_READ32(sc, ICH_FLASH_HSFSTS)
   14092 			    >> 16;
   14093 		else
   14094 			hsflctl = ICH8_FLASH_READ16(sc, ICH_FLASH_HSFCTL);
   14095 		/* 0b/1b corresponds to 1 or 2 byte size, respectively. */
   14096 		hsflctl |=  ((size - 1) << HSFCTL_BCOUNT_SHIFT)
   14097 		    & HSFCTL_BCOUNT_MASK;
   14098 		hsflctl |= ICH_CYCLE_READ << HSFCTL_CYCLE_SHIFT;
   14099 		if (sc->sc_type >= WM_T_PCH_SPT) {
   14100 			/*
   14101 			 * In SPT, This register is in Lan memory space, not
   14102 			 * flash. Therefore, only 32 bit access is supported.
   14103 			 */
   14104 			ICH8_FLASH_WRITE32(sc, ICH_FLASH_HSFSTS,
   14105 			    (uint32_t)hsflctl << 16);
   14106 		} else
   14107 			ICH8_FLASH_WRITE16(sc, ICH_FLASH_HSFCTL, hsflctl);
   14108 
   14109 		/*
   14110 		 * Write the last 24 bits of index into Flash Linear address
   14111 		 * field in Flash Address
   14112 		 */
   14113 		/* TODO: TBD maybe check the index against the size of flash */
   14114 
   14115 		ICH8_FLASH_WRITE32(sc, ICH_FLASH_FADDR, flash_linear_address);
   14116 
   14117 		error = wm_ich8_flash_cycle(sc, ICH_FLASH_COMMAND_TIMEOUT);
   14118 
   14119 		/*
   14120 		 * Check if FCERR is set to 1, if set to 1, clear it and try
   14121 		 * the whole sequence a few more times, else read in (shift in)
   14122 		 * the Flash Data0, the order is least significant byte first
   14123 		 * msb to lsb
   14124 		 */
   14125 		if (error == 0) {
   14126 			flash_data = ICH8_FLASH_READ32(sc, ICH_FLASH_FDATA0);
   14127 			if (size == 1)
   14128 				*data = (uint8_t)(flash_data & 0x000000FF);
   14129 			else if (size == 2)
   14130 				*data = (uint16_t)(flash_data & 0x0000FFFF);
   14131 			else if (size == 4)
   14132 				*data = (uint32_t)flash_data;
   14133 			break;
   14134 		} else {
   14135 			/*
   14136 			 * If we've gotten here, then things are probably
   14137 			 * completely hosed, but if the error condition is
   14138 			 * detected, it won't hurt to give it another try...
   14139 			 * ICH_FLASH_CYCLE_REPEAT_COUNT times.
   14140 			 */
   14141 			if (sc->sc_type >= WM_T_PCH_SPT)
   14142 				hsfsts = ICH8_FLASH_READ32(sc,
   14143 				    ICH_FLASH_HSFSTS) & 0xffffUL;
   14144 			else
   14145 				hsfsts = ICH8_FLASH_READ16(sc,
   14146 				    ICH_FLASH_HSFSTS);
   14147 
   14148 			if (hsfsts & HSFSTS_ERR) {
   14149 				/* Repeat for some time before giving up. */
   14150 				continue;
   14151 			} else if ((hsfsts & HSFSTS_DONE) == 0)
   14152 				break;
   14153 		}
   14154 	} while (count++ < ICH_FLASH_CYCLE_REPEAT_COUNT);
   14155 
   14156 	return error;
   14157 }
   14158 
   14159 /******************************************************************************
   14160  * Reads a single byte from the NVM using the ICH8 flash access registers.
   14161  *
   14162  * sc - pointer to wm_hw structure
   14163  * index - The index of the byte to read.
   14164  * data - Pointer to a byte to store the value read.
   14165  *****************************************************************************/
   14166 static int32_t
   14167 wm_read_ich8_byte(struct wm_softc *sc, uint32_t index, uint8_t* data)
   14168 {
   14169 	int32_t status;
   14170 	uint32_t word = 0;
   14171 
   14172 	status = wm_read_ich8_data(sc, index, 1, &word);
   14173 	if (status == 0)
   14174 		*data = (uint8_t)word;
   14175 	else
   14176 		*data = 0;
   14177 
   14178 	return status;
   14179 }
   14180 
   14181 /******************************************************************************
   14182  * Reads a word from the NVM using the ICH8 flash access registers.
   14183  *
   14184  * sc - pointer to wm_hw structure
   14185  * index - The starting byte index of the word to read.
   14186  * data - Pointer to a word to store the value read.
   14187  *****************************************************************************/
   14188 static int32_t
   14189 wm_read_ich8_word(struct wm_softc *sc, uint32_t index, uint16_t *data)
   14190 {
   14191 	int32_t status;
   14192 	uint32_t word = 0;
   14193 
   14194 	status = wm_read_ich8_data(sc, index, 2, &word);
   14195 	if (status == 0)
   14196 		*data = (uint16_t)word;
   14197 	else
   14198 		*data = 0;
   14199 
   14200 	return status;
   14201 }
   14202 
   14203 /******************************************************************************
   14204  * Reads a dword from the NVM using the ICH8 flash access registers.
   14205  *
   14206  * sc - pointer to wm_hw structure
   14207  * index - The starting byte index of the word to read.
   14208  * data - Pointer to a word to store the value read.
   14209  *****************************************************************************/
   14210 static int32_t
   14211 wm_read_ich8_dword(struct wm_softc *sc, uint32_t index, uint32_t *data)
   14212 {
   14213 	int32_t status;
   14214 
   14215 	status = wm_read_ich8_data(sc, index, 4, data);
   14216 	return status;
   14217 }
   14218 
   14219 /******************************************************************************
   14220  * Reads a 16 bit word or words from the EEPROM using the ICH8's flash access
   14221  * register.
   14222  *
   14223  * sc - Struct containing variables accessed by shared code
   14224  * offset - offset of word in the EEPROM to read
   14225  * data - word read from the EEPROM
   14226  * words - number of words to read
   14227  *****************************************************************************/
   14228 static int
   14229 wm_nvm_read_ich8(struct wm_softc *sc, int offset, int words, uint16_t *data)
   14230 {
   14231 	int32_t	 rv = 0;
   14232 	uint32_t flash_bank = 0;
   14233 	uint32_t act_offset = 0;
   14234 	uint32_t bank_offset = 0;
   14235 	uint16_t word = 0;
   14236 	uint16_t i = 0;
   14237 
   14238 	DPRINTF(sc, WM_DEBUG_NVM, ("%s: %s called\n",
   14239 		device_xname(sc->sc_dev), __func__));
   14240 
   14241 	if (sc->nvm.acquire(sc) != 0)
   14242 		return -1;
   14243 
   14244 	/*
   14245 	 * We need to know which is the valid flash bank.  In the event
   14246 	 * that we didn't allocate eeprom_shadow_ram, we may not be
   14247 	 * managing flash_bank. So it cannot be trusted and needs
   14248 	 * to be updated with each read.
   14249 	 */
   14250 	rv = wm_nvm_valid_bank_detect_ich8lan(sc, &flash_bank);
   14251 	if (rv) {
   14252 		DPRINTF(sc, WM_DEBUG_NVM, ("%s: failed to detect NVM bank\n",
   14253 			device_xname(sc->sc_dev)));
   14254 		flash_bank = 0;
   14255 	}
   14256 
   14257 	/*
   14258 	 * Adjust offset appropriately if we're on bank 1 - adjust for word
   14259 	 * size
   14260 	 */
   14261 	bank_offset = flash_bank * (sc->sc_ich8_flash_bank_size * 2);
   14262 
   14263 	for (i = 0; i < words; i++) {
   14264 		/* The NVM part needs a byte offset, hence * 2 */
   14265 		act_offset = bank_offset + ((offset + i) * 2);
   14266 		rv = wm_read_ich8_word(sc, act_offset, &word);
   14267 		if (rv) {
   14268 			aprint_error_dev(sc->sc_dev,
   14269 			    "%s: failed to read NVM\n", __func__);
   14270 			break;
   14271 		}
   14272 		data[i] = word;
   14273 	}
   14274 
   14275 	sc->nvm.release(sc);
   14276 	return rv;
   14277 }
   14278 
   14279 /******************************************************************************
   14280  * Reads a 16 bit word or words from the EEPROM using the SPT's flash access
   14281  * register.
   14282  *
   14283  * sc - Struct containing variables accessed by shared code
   14284  * offset - offset of word in the EEPROM to read
   14285  * data - word read from the EEPROM
   14286  * words - number of words to read
   14287  *****************************************************************************/
   14288 static int
   14289 wm_nvm_read_spt(struct wm_softc *sc, int offset, int words, uint16_t *data)
   14290 {
   14291 	int32_t	 rv = 0;
   14292 	uint32_t flash_bank = 0;
   14293 	uint32_t act_offset = 0;
   14294 	uint32_t bank_offset = 0;
   14295 	uint32_t dword = 0;
   14296 	uint16_t i = 0;
   14297 
   14298 	DPRINTF(sc, WM_DEBUG_NVM, ("%s: %s called\n",
   14299 		device_xname(sc->sc_dev), __func__));
   14300 
   14301 	if (sc->nvm.acquire(sc) != 0)
   14302 		return -1;
   14303 
   14304 	/*
   14305 	 * We need to know which is the valid flash bank.  In the event
   14306 	 * that we didn't allocate eeprom_shadow_ram, we may not be
   14307 	 * managing flash_bank. So it cannot be trusted and needs
   14308 	 * to be updated with each read.
   14309 	 */
   14310 	rv = wm_nvm_valid_bank_detect_ich8lan(sc, &flash_bank);
   14311 	if (rv) {
   14312 		DPRINTF(sc, WM_DEBUG_NVM, ("%s: failed to detect NVM bank\n",
   14313 			device_xname(sc->sc_dev)));
   14314 		flash_bank = 0;
   14315 	}
   14316 
   14317 	/*
   14318 	 * Adjust offset appropriately if we're on bank 1 - adjust for word
   14319 	 * size
   14320 	 */
   14321 	bank_offset = flash_bank * (sc->sc_ich8_flash_bank_size * 2);
   14322 
   14323 	for (i = 0; i < words; i++) {
   14324 		/* The NVM part needs a byte offset, hence * 2 */
   14325 		act_offset = bank_offset + ((offset + i) * 2);
   14326 		/* but we must read dword aligned, so mask ... */
   14327 		rv = wm_read_ich8_dword(sc, act_offset & ~0x3, &dword);
   14328 		if (rv) {
   14329 			aprint_error_dev(sc->sc_dev,
   14330 			    "%s: failed to read NVM\n", __func__);
   14331 			break;
   14332 		}
   14333 		/* ... and pick out low or high word */
   14334 		if ((act_offset & 0x2) == 0)
   14335 			data[i] = (uint16_t)(dword & 0xFFFF);
   14336 		else
   14337 			data[i] = (uint16_t)((dword >> 16) & 0xFFFF);
   14338 	}
   14339 
   14340 	sc->nvm.release(sc);
   14341 	return rv;
   14342 }
   14343 
   14344 /* iNVM */
   14345 
   14346 static int
   14347 wm_nvm_read_word_invm(struct wm_softc *sc, uint16_t address, uint16_t *data)
   14348 {
   14349 	int32_t	 rv = 0;
   14350 	uint32_t invm_dword;
   14351 	uint16_t i;
   14352 	uint8_t record_type, word_address;
   14353 
   14354 	DPRINTF(sc, WM_DEBUG_NVM, ("%s: %s called\n",
   14355 		device_xname(sc->sc_dev), __func__));
   14356 
   14357 	for (i = 0; i < INVM_SIZE; i++) {
   14358 		invm_dword = CSR_READ(sc, WM_INVM_DATA_REG(i));
   14359 		/* Get record type */
   14360 		record_type = INVM_DWORD_TO_RECORD_TYPE(invm_dword);
   14361 		if (record_type == INVM_UNINITIALIZED_STRUCTURE)
   14362 			break;
   14363 		if (record_type == INVM_CSR_AUTOLOAD_STRUCTURE)
   14364 			i += INVM_CSR_AUTOLOAD_DATA_SIZE_IN_DWORDS;
   14365 		if (record_type == INVM_RSA_KEY_SHA256_STRUCTURE)
   14366 			i += INVM_RSA_KEY_SHA256_DATA_SIZE_IN_DWORDS;
   14367 		if (record_type == INVM_WORD_AUTOLOAD_STRUCTURE) {
   14368 			word_address = INVM_DWORD_TO_WORD_ADDRESS(invm_dword);
   14369 			if (word_address == address) {
   14370 				*data = INVM_DWORD_TO_WORD_DATA(invm_dword);
   14371 				rv = 0;
   14372 				break;
   14373 			}
   14374 		}
   14375 	}
   14376 
   14377 	return rv;
   14378 }
   14379 
   14380 static int
   14381 wm_nvm_read_invm(struct wm_softc *sc, int offset, int words, uint16_t *data)
   14382 {
   14383 	int rv = 0;
   14384 	int i;
   14385 
   14386 	DPRINTF(sc, WM_DEBUG_NVM, ("%s: %s called\n",
   14387 		device_xname(sc->sc_dev), __func__));
   14388 
   14389 	if (sc->nvm.acquire(sc) != 0)
   14390 		return -1;
   14391 
   14392 	for (i = 0; i < words; i++) {
   14393 		switch (offset + i) {
   14394 		case NVM_OFF_MACADDR:
   14395 		case NVM_OFF_MACADDR1:
   14396 		case NVM_OFF_MACADDR2:
   14397 			rv = wm_nvm_read_word_invm(sc, offset + i, &data[i]);
   14398 			if (rv != 0) {
   14399 				data[i] = 0xffff;
   14400 				rv = -1;
   14401 			}
   14402 			break;
   14403 		case NVM_OFF_CFG1: /* == INVM_AUTOLOAD */
   14404 			rv = wm_nvm_read_word_invm(sc, offset, data);
   14405 			if (rv != 0) {
   14406 				*data = INVM_DEFAULT_AL;
   14407 				rv = 0;
   14408 			}
   14409 			break;
   14410 		case NVM_OFF_CFG2:
   14411 			rv = wm_nvm_read_word_invm(sc, offset, data);
   14412 			if (rv != 0) {
   14413 				*data = NVM_INIT_CTRL_2_DEFAULT_I211;
   14414 				rv = 0;
   14415 			}
   14416 			break;
   14417 		case NVM_OFF_CFG4:
   14418 			rv = wm_nvm_read_word_invm(sc, offset, data);
   14419 			if (rv != 0) {
   14420 				*data = NVM_INIT_CTRL_4_DEFAULT_I211;
   14421 				rv = 0;
   14422 			}
   14423 			break;
   14424 		case NVM_OFF_LED_1_CFG:
   14425 			rv = wm_nvm_read_word_invm(sc, offset, data);
   14426 			if (rv != 0) {
   14427 				*data = NVM_LED_1_CFG_DEFAULT_I211;
   14428 				rv = 0;
   14429 			}
   14430 			break;
   14431 		case NVM_OFF_LED_0_2_CFG:
   14432 			rv = wm_nvm_read_word_invm(sc, offset, data);
   14433 			if (rv != 0) {
   14434 				*data = NVM_LED_0_2_CFG_DEFAULT_I211;
   14435 				rv = 0;
   14436 			}
   14437 			break;
   14438 		case NVM_OFF_ID_LED_SETTINGS:
   14439 			rv = wm_nvm_read_word_invm(sc, offset, data);
   14440 			if (rv != 0) {
   14441 				*data = ID_LED_RESERVED_FFFF;
   14442 				rv = 0;
   14443 			}
   14444 			break;
   14445 		default:
   14446 			DPRINTF(sc, WM_DEBUG_NVM,
   14447 			    ("NVM word 0x%02x is not mapped.\n", offset));
   14448 			*data = NVM_RESERVED_WORD;
   14449 			break;
   14450 		}
   14451 	}
   14452 
   14453 	sc->nvm.release(sc);
   14454 	return rv;
   14455 }
   14456 
   14457 /* Lock, detecting NVM type, validate checksum, version and read */
   14458 
   14459 static int
   14460 wm_nvm_is_onboard_eeprom(struct wm_softc *sc)
   14461 {
   14462 	uint32_t eecd = 0;
   14463 
   14464 	if (sc->sc_type == WM_T_82573 || sc->sc_type == WM_T_82574
   14465 	    || sc->sc_type == WM_T_82583) {
   14466 		eecd = CSR_READ(sc, WMREG_EECD);
   14467 
   14468 		/* Isolate bits 15 & 16 */
   14469 		eecd = ((eecd >> 15) & 0x03);
   14470 
   14471 		/* If both bits are set, device is Flash type */
   14472 		if (eecd == 0x03)
   14473 			return 0;
   14474 	}
   14475 	return 1;
   14476 }
   14477 
   14478 static int
   14479 wm_nvm_flash_presence_i210(struct wm_softc *sc)
   14480 {
   14481 	uint32_t eec;
   14482 
   14483 	eec = CSR_READ(sc, WMREG_EEC);
   14484 	if ((eec & EEC_FLASH_DETECTED) != 0)
   14485 		return 1;
   14486 
   14487 	return 0;
   14488 }
   14489 
   14490 /*
   14491  * wm_nvm_validate_checksum
   14492  *
   14493  * The checksum is defined as the sum of the first 64 (16 bit) words.
   14494  */
   14495 static int
   14496 wm_nvm_validate_checksum(struct wm_softc *sc)
   14497 {
   14498 	uint16_t checksum;
   14499 	uint16_t eeprom_data;
   14500 #ifdef WM_DEBUG
   14501 	uint16_t csum_wordaddr, valid_checksum;
   14502 #endif
   14503 	int i;
   14504 
   14505 	checksum = 0;
   14506 
   14507 	/* Don't check for I211 */
   14508 	if (sc->sc_type == WM_T_I211)
   14509 		return 0;
   14510 
   14511 #ifdef WM_DEBUG
   14512 	if ((sc->sc_type == WM_T_PCH_LPT) || (sc->sc_type == WM_T_PCH_SPT)
   14513 	    || (sc->sc_type == WM_T_PCH_CNP)) {
   14514 		csum_wordaddr = NVM_OFF_COMPAT;
   14515 		valid_checksum = NVM_COMPAT_VALID_CHECKSUM;
   14516 	} else {
   14517 		csum_wordaddr = NVM_OFF_FUTURE_INIT_WORD1;
   14518 		valid_checksum = NVM_FUTURE_INIT_WORD1_VALID_CHECKSUM;
   14519 	}
   14520 
   14521 	/* Dump EEPROM image for debug */
   14522 	if ((sc->sc_type == WM_T_ICH8) || (sc->sc_type == WM_T_ICH9)
   14523 	    || (sc->sc_type == WM_T_ICH10) || (sc->sc_type == WM_T_PCH)
   14524 	    || (sc->sc_type == WM_T_PCH2) || (sc->sc_type == WM_T_PCH_LPT)) {
   14525 		/* XXX PCH_SPT? */
   14526 		wm_nvm_read(sc, csum_wordaddr, 1, &eeprom_data);
   14527 		if ((eeprom_data & valid_checksum) == 0)
   14528 			DPRINTF(sc, WM_DEBUG_NVM,
   14529 			    ("%s: NVM need to be updated (%04x != %04x)\n",
   14530 				device_xname(sc->sc_dev), eeprom_data,
   14531 				    valid_checksum));
   14532 	}
   14533 
   14534 	if ((sc->sc_debug & WM_DEBUG_NVM) != 0) {
   14535 		printf("%s: NVM dump:\n", device_xname(sc->sc_dev));
   14536 		for (i = 0; i < NVM_SIZE; i++) {
   14537 			if (wm_nvm_read(sc, i, 1, &eeprom_data))
   14538 				printf("XXXX ");
   14539 			else
   14540 				printf("%04hx ", eeprom_data);
   14541 			if (i % 8 == 7)
   14542 				printf("\n");
   14543 		}
   14544 	}
   14545 
   14546 #endif /* WM_DEBUG */
   14547 
   14548 	for (i = 0; i < NVM_SIZE; i++) {
   14549 		if (wm_nvm_read(sc, i, 1, &eeprom_data))
   14550 			return 1;
   14551 		checksum += eeprom_data;
   14552 	}
   14553 
   14554 	if (checksum != (uint16_t) NVM_CHECKSUM) {
   14555 #ifdef WM_DEBUG
   14556 		printf("%s: NVM checksum mismatch (%04x != %04x)\n",
   14557 		    device_xname(sc->sc_dev), checksum, NVM_CHECKSUM);
   14558 #endif
   14559 	}
   14560 
   14561 	return 0;
   14562 }
   14563 
   14564 static void
   14565 wm_nvm_version_invm(struct wm_softc *sc)
   14566 {
   14567 	uint32_t dword;
   14568 
   14569 	/*
   14570 	 * Linux's code to decode version is very strange, so we don't
   14571 	 * obey that algorithm and just use word 61 as the document.
   14572 	 * Perhaps it's not perfect though...
   14573 	 *
   14574 	 * Example:
   14575 	 *
   14576 	 *   Word61: 00800030 -> Version 0.6 (I211 spec update notes about 0.6)
   14577 	 */
   14578 	dword = CSR_READ(sc, WM_INVM_DATA_REG(61));
   14579 	dword = __SHIFTOUT(dword, INVM_VER_1);
   14580 	sc->sc_nvm_ver_major = __SHIFTOUT(dword, INVM_MAJOR);
   14581 	sc->sc_nvm_ver_minor = __SHIFTOUT(dword, INVM_MINOR);
   14582 }
   14583 
   14584 static void
   14585 wm_nvm_version(struct wm_softc *sc)
   14586 {
   14587 	uint16_t major, minor, build, patch;
   14588 	uint16_t uid0, uid1;
   14589 	uint16_t nvm_data;
   14590 	uint16_t off;
   14591 	bool check_version = false;
   14592 	bool check_optionrom = false;
   14593 	bool have_build = false;
   14594 	bool have_uid = true;
   14595 
   14596 	/*
   14597 	 * Version format:
   14598 	 *
   14599 	 * XYYZ
   14600 	 * X0YZ
   14601 	 * X0YY
   14602 	 *
   14603 	 * Example:
   14604 	 *
   14605 	 *	82571	0x50a2	5.10.2?	(the spec update notes about 5.6-5.10)
   14606 	 *	82571	0x50a6	5.10.6?
   14607 	 *	82572	0x506a	5.6.10?
   14608 	 *	82572EI	0x5069	5.6.9?
   14609 	 *	82574L	0x1080	1.8.0?	(the spec update notes about 2.1.4)
   14610 	 *		0x2013	2.1.3?
   14611 	 *	82583	0x10a0	1.10.0? (document says it's default value)
   14612 	 * ICH8+82567	0x0040	0.4.0?
   14613 	 * ICH9+82566	0x1040	1.4.0?
   14614 	 *ICH10+82567	0x0043	0.4.3?
   14615 	 *  PCH+82577	0x00c1	0.12.1?
   14616 	 * PCH2+82579	0x00d3	0.13.3?
   14617 	 *		0x00d4	0.13.4?
   14618 	 *  LPT+I218	0x0023	0.2.3?
   14619 	 *  SPT+I219	0x0084	0.8.4?
   14620 	 *  CNP+I219	0x0054	0.5.4?
   14621 	 */
   14622 
   14623 	/*
   14624 	 * XXX
   14625 	 * Qemu's e1000e emulation (82574L)'s SPI has only 64 words.
   14626 	 * I've never seen real 82574 hardware with such small SPI ROM.
   14627 	 */
   14628 	if ((sc->sc_nvm_wordsize < NVM_OFF_IMAGE_UID1)
   14629 	    || (wm_nvm_read(sc, NVM_OFF_IMAGE_UID1, 1, &uid1) != 0))
   14630 		have_uid = false;
   14631 
   14632 	switch (sc->sc_type) {
   14633 	case WM_T_82571:
   14634 	case WM_T_82572:
   14635 	case WM_T_82574:
   14636 	case WM_T_82583:
   14637 		check_version = true;
   14638 		check_optionrom = true;
   14639 		have_build = true;
   14640 		break;
   14641 	case WM_T_ICH8:
   14642 	case WM_T_ICH9:
   14643 	case WM_T_ICH10:
   14644 	case WM_T_PCH:
   14645 	case WM_T_PCH2:
   14646 	case WM_T_PCH_LPT:
   14647 	case WM_T_PCH_SPT:
   14648 	case WM_T_PCH_CNP:
   14649 		check_version = true;
   14650 		have_build = true;
   14651 		have_uid = false;
   14652 		break;
   14653 	case WM_T_82575:
   14654 	case WM_T_82576:
   14655 	case WM_T_82580:
   14656 		if (have_uid && (uid1 & NVM_MAJOR_MASK) != NVM_UID_VALID)
   14657 			check_version = true;
   14658 		break;
   14659 	case WM_T_I211:
   14660 		wm_nvm_version_invm(sc);
   14661 		have_uid = false;
   14662 		goto printver;
   14663 	case WM_T_I210:
   14664 		if (!wm_nvm_flash_presence_i210(sc)) {
   14665 			wm_nvm_version_invm(sc);
   14666 			have_uid = false;
   14667 			goto printver;
   14668 		}
   14669 		/* FALLTHROUGH */
   14670 	case WM_T_I350:
   14671 	case WM_T_I354:
   14672 		check_version = true;
   14673 		check_optionrom = true;
   14674 		break;
   14675 	default:
   14676 		return;
   14677 	}
   14678 	if (check_version
   14679 	    && (wm_nvm_read(sc, NVM_OFF_VERSION, 1, &nvm_data) == 0)) {
   14680 		major = (nvm_data & NVM_MAJOR_MASK) >> NVM_MAJOR_SHIFT;
   14681 		if (have_build || ((nvm_data & 0x0f00) != 0x0000)) {
   14682 			minor = (nvm_data & NVM_MINOR_MASK) >> NVM_MINOR_SHIFT;
   14683 			build = nvm_data & NVM_BUILD_MASK;
   14684 			have_build = true;
   14685 		} else
   14686 			minor = nvm_data & 0x00ff;
   14687 
   14688 		/* Decimal */
   14689 		minor = (minor / 16) * 10 + (minor % 16);
   14690 		sc->sc_nvm_ver_major = major;
   14691 		sc->sc_nvm_ver_minor = minor;
   14692 
   14693 printver:
   14694 		aprint_verbose(", version %d.%d", sc->sc_nvm_ver_major,
   14695 		    sc->sc_nvm_ver_minor);
   14696 		if (have_build) {
   14697 			sc->sc_nvm_ver_build = build;
   14698 			aprint_verbose(".%d", build);
   14699 		}
   14700 	}
   14701 
   14702 	/* Assume the Option ROM area is at avove NVM_SIZE */
   14703 	if ((sc->sc_nvm_wordsize > NVM_SIZE) && check_optionrom
   14704 	    && (wm_nvm_read(sc, NVM_OFF_COMB_VER_PTR, 1, &off) == 0)) {
   14705 		/* Option ROM Version */
   14706 		if ((off != 0x0000) && (off != 0xffff)) {
   14707 			int rv;
   14708 
   14709 			off += NVM_COMBO_VER_OFF;
   14710 			rv = wm_nvm_read(sc, off + 1, 1, &uid1);
   14711 			rv |= wm_nvm_read(sc, off, 1, &uid0);
   14712 			if ((rv == 0) && (uid0 != 0) && (uid0 != 0xffff)
   14713 			    && (uid1 != 0) && (uid1 != 0xffff)) {
   14714 				/* 16bits */
   14715 				major = uid0 >> 8;
   14716 				build = (uid0 << 8) | (uid1 >> 8);
   14717 				patch = uid1 & 0x00ff;
   14718 				aprint_verbose(", option ROM Version %d.%d.%d",
   14719 				    major, build, patch);
   14720 			}
   14721 		}
   14722 	}
   14723 
   14724 	if (have_uid && (wm_nvm_read(sc, NVM_OFF_IMAGE_UID0, 1, &uid0) == 0))
   14725 		aprint_verbose(", Image Unique ID %08x",
   14726 		    ((uint32_t)uid1 << 16) | uid0);
   14727 }
   14728 
   14729 /*
   14730  * wm_nvm_read:
   14731  *
   14732  *	Read data from the serial EEPROM.
   14733  */
   14734 static int
   14735 wm_nvm_read(struct wm_softc *sc, int word, int wordcnt, uint16_t *data)
   14736 {
   14737 	int rv;
   14738 
   14739 	DPRINTF(sc, WM_DEBUG_NVM, ("%s: %s called\n",
   14740 		device_xname(sc->sc_dev), __func__));
   14741 
   14742 	if (sc->sc_flags & WM_F_EEPROM_INVALID)
   14743 		return -1;
   14744 
   14745 	rv = sc->nvm.read(sc, word, wordcnt, data);
   14746 
   14747 	return rv;
   14748 }
   14749 
   14750 /*
   14751  * Hardware semaphores.
   14752  * Very complexed...
   14753  */
   14754 
   14755 static int
   14756 wm_get_null(struct wm_softc *sc)
   14757 {
   14758 
   14759 	DPRINTF(sc, WM_DEBUG_LOCK, ("%s: %s called\n",
   14760 		device_xname(sc->sc_dev), __func__));
   14761 	return 0;
   14762 }
   14763 
   14764 static void
   14765 wm_put_null(struct wm_softc *sc)
   14766 {
   14767 
   14768 	DPRINTF(sc, WM_DEBUG_LOCK, ("%s: %s called\n",
   14769 		device_xname(sc->sc_dev), __func__));
   14770 	return;
   14771 }
   14772 
   14773 static int
   14774 wm_get_eecd(struct wm_softc *sc)
   14775 {
   14776 	uint32_t reg;
   14777 	int x;
   14778 
   14779 	DPRINTF(sc, WM_DEBUG_LOCK | WM_DEBUG_NVM, ("%s: %s called\n",
   14780 		device_xname(sc->sc_dev), __func__));
   14781 
   14782 	reg = CSR_READ(sc, WMREG_EECD);
   14783 
   14784 	/* Request EEPROM access. */
   14785 	reg |= EECD_EE_REQ;
   14786 	CSR_WRITE(sc, WMREG_EECD, reg);
   14787 
   14788 	/* ..and wait for it to be granted. */
   14789 	for (x = 0; x < 1000; x++) {
   14790 		reg = CSR_READ(sc, WMREG_EECD);
   14791 		if (reg & EECD_EE_GNT)
   14792 			break;
   14793 		delay(5);
   14794 	}
   14795 	if ((reg & EECD_EE_GNT) == 0) {
   14796 		aprint_error_dev(sc->sc_dev,
   14797 		    "could not acquire EEPROM GNT\n");
   14798 		reg &= ~EECD_EE_REQ;
   14799 		CSR_WRITE(sc, WMREG_EECD, reg);
   14800 		return -1;
   14801 	}
   14802 
   14803 	return 0;
   14804 }
   14805 
   14806 static void
   14807 wm_nvm_eec_clock_raise(struct wm_softc *sc, uint32_t *eecd)
   14808 {
   14809 
   14810 	*eecd |= EECD_SK;
   14811 	CSR_WRITE(sc, WMREG_EECD, *eecd);
   14812 	CSR_WRITE_FLUSH(sc);
   14813 	if ((sc->sc_flags & WM_F_EEPROM_SPI) != 0)
   14814 		delay(1);
   14815 	else
   14816 		delay(50);
   14817 }
   14818 
   14819 static void
   14820 wm_nvm_eec_clock_lower(struct wm_softc *sc, uint32_t *eecd)
   14821 {
   14822 
   14823 	*eecd &= ~EECD_SK;
   14824 	CSR_WRITE(sc, WMREG_EECD, *eecd);
   14825 	CSR_WRITE_FLUSH(sc);
   14826 	if ((sc->sc_flags & WM_F_EEPROM_SPI) != 0)
   14827 		delay(1);
   14828 	else
   14829 		delay(50);
   14830 }
   14831 
   14832 static void
   14833 wm_put_eecd(struct wm_softc *sc)
   14834 {
   14835 	uint32_t reg;
   14836 
   14837 	DPRINTF(sc, WM_DEBUG_LOCK, ("%s: %s called\n",
   14838 		device_xname(sc->sc_dev), __func__));
   14839 
   14840 	/* Stop nvm */
   14841 	reg = CSR_READ(sc, WMREG_EECD);
   14842 	if ((sc->sc_flags & WM_F_EEPROM_SPI) != 0) {
   14843 		/* Pull CS high */
   14844 		reg |= EECD_CS;
   14845 		wm_nvm_eec_clock_lower(sc, &reg);
   14846 	} else {
   14847 		/* CS on Microwire is active-high */
   14848 		reg &= ~(EECD_CS | EECD_DI);
   14849 		CSR_WRITE(sc, WMREG_EECD, reg);
   14850 		wm_nvm_eec_clock_raise(sc, &reg);
   14851 		wm_nvm_eec_clock_lower(sc, &reg);
   14852 	}
   14853 
   14854 	reg = CSR_READ(sc, WMREG_EECD);
   14855 	reg &= ~EECD_EE_REQ;
   14856 	CSR_WRITE(sc, WMREG_EECD, reg);
   14857 
   14858 	return;
   14859 }
   14860 
   14861 /*
   14862  * Get hardware semaphore.
   14863  * Same as e1000_get_hw_semaphore_generic()
   14864  */
   14865 static int
   14866 wm_get_swsm_semaphore(struct wm_softc *sc)
   14867 {
   14868 	int32_t timeout;
   14869 	uint32_t swsm;
   14870 
   14871 	DPRINTF(sc, WM_DEBUG_LOCK, ("%s: %s called\n",
   14872 		device_xname(sc->sc_dev), __func__));
   14873 	KASSERT(sc->sc_nvm_wordsize > 0);
   14874 
   14875 retry:
   14876 	/* Get the SW semaphore. */
   14877 	timeout = sc->sc_nvm_wordsize + 1;
   14878 	while (timeout) {
   14879 		swsm = CSR_READ(sc, WMREG_SWSM);
   14880 
   14881 		if ((swsm & SWSM_SMBI) == 0)
   14882 			break;
   14883 
   14884 		delay(50);
   14885 		timeout--;
   14886 	}
   14887 
   14888 	if (timeout == 0) {
   14889 		if ((sc->sc_flags & WM_F_WA_I210_CLSEM) != 0) {
   14890 			/*
   14891 			 * In rare circumstances, the SW semaphore may already
   14892 			 * be held unintentionally. Clear the semaphore once
   14893 			 * before giving up.
   14894 			 */
   14895 			sc->sc_flags &= ~WM_F_WA_I210_CLSEM;
   14896 			wm_put_swsm_semaphore(sc);
   14897 			goto retry;
   14898 		}
   14899 		aprint_error_dev(sc->sc_dev, "could not acquire SWSM SMBI\n");
   14900 		return 1;
   14901 	}
   14902 
   14903 	/* Get the FW semaphore. */
   14904 	timeout = sc->sc_nvm_wordsize + 1;
   14905 	while (timeout) {
   14906 		swsm = CSR_READ(sc, WMREG_SWSM);
   14907 		swsm |= SWSM_SWESMBI;
   14908 		CSR_WRITE(sc, WMREG_SWSM, swsm);
   14909 		/* If we managed to set the bit we got the semaphore. */
   14910 		swsm = CSR_READ(sc, WMREG_SWSM);
   14911 		if (swsm & SWSM_SWESMBI)
   14912 			break;
   14913 
   14914 		delay(50);
   14915 		timeout--;
   14916 	}
   14917 
   14918 	if (timeout == 0) {
   14919 		aprint_error_dev(sc->sc_dev,
   14920 		    "could not acquire SWSM SWESMBI\n");
   14921 		/* Release semaphores */
   14922 		wm_put_swsm_semaphore(sc);
   14923 		return 1;
   14924 	}
   14925 	return 0;
   14926 }
   14927 
   14928 /*
   14929  * Put hardware semaphore.
   14930  * Same as e1000_put_hw_semaphore_generic()
   14931  */
   14932 static void
   14933 wm_put_swsm_semaphore(struct wm_softc *sc)
   14934 {
   14935 	uint32_t swsm;
   14936 
   14937 	DPRINTF(sc, WM_DEBUG_LOCK, ("%s: %s called\n",
   14938 		device_xname(sc->sc_dev), __func__));
   14939 
   14940 	swsm = CSR_READ(sc, WMREG_SWSM);
   14941 	swsm &= ~(SWSM_SMBI | SWSM_SWESMBI);
   14942 	CSR_WRITE(sc, WMREG_SWSM, swsm);
   14943 }
   14944 
   14945 /*
   14946  * Get SW/FW semaphore.
   14947  * Same as e1000_acquire_swfw_sync_{80003es2lan,82575}().
   14948  */
   14949 static int
   14950 wm_get_swfw_semaphore(struct wm_softc *sc, uint16_t mask)
   14951 {
   14952 	uint32_t swfw_sync;
   14953 	uint32_t swmask = mask << SWFW_SOFT_SHIFT;
   14954 	uint32_t fwmask = mask << SWFW_FIRM_SHIFT;
   14955 	int timeout;
   14956 
   14957 	DPRINTF(sc, WM_DEBUG_LOCK, ("%s: %s called\n",
   14958 		device_xname(sc->sc_dev), __func__));
   14959 
   14960 	if (sc->sc_type == WM_T_80003)
   14961 		timeout = 50;
   14962 	else
   14963 		timeout = 200;
   14964 
   14965 	while (timeout) {
   14966 		if (wm_get_swsm_semaphore(sc)) {
   14967 			aprint_error_dev(sc->sc_dev,
   14968 			    "%s: failed to get semaphore\n",
   14969 			    __func__);
   14970 			return 1;
   14971 		}
   14972 		swfw_sync = CSR_READ(sc, WMREG_SW_FW_SYNC);
   14973 		if ((swfw_sync & (swmask | fwmask)) == 0) {
   14974 			swfw_sync |= swmask;
   14975 			CSR_WRITE(sc, WMREG_SW_FW_SYNC, swfw_sync);
   14976 			wm_put_swsm_semaphore(sc);
   14977 			return 0;
   14978 		}
   14979 		wm_put_swsm_semaphore(sc);
   14980 		delay(5000);
   14981 		timeout--;
   14982 	}
   14983 	device_printf(sc->sc_dev,
   14984 	    "failed to get swfw semaphore mask 0x%x swfw 0x%x\n",
   14985 	    mask, swfw_sync);
   14986 	return 1;
   14987 }
   14988 
   14989 static void
   14990 wm_put_swfw_semaphore(struct wm_softc *sc, uint16_t mask)
   14991 {
   14992 	uint32_t swfw_sync;
   14993 
   14994 	DPRINTF(sc, WM_DEBUG_LOCK, ("%s: %s called\n",
   14995 		device_xname(sc->sc_dev), __func__));
   14996 
   14997 	while (wm_get_swsm_semaphore(sc) != 0)
   14998 		continue;
   14999 
   15000 	swfw_sync = CSR_READ(sc, WMREG_SW_FW_SYNC);
   15001 	swfw_sync &= ~(mask << SWFW_SOFT_SHIFT);
   15002 	CSR_WRITE(sc, WMREG_SW_FW_SYNC, swfw_sync);
   15003 
   15004 	wm_put_swsm_semaphore(sc);
   15005 }
   15006 
   15007 static int
   15008 wm_get_nvm_80003(struct wm_softc *sc)
   15009 {
   15010 	int rv;
   15011 
   15012 	DPRINTF(sc, WM_DEBUG_LOCK | WM_DEBUG_NVM, ("%s: %s called\n",
   15013 		device_xname(sc->sc_dev), __func__));
   15014 
   15015 	if ((rv = wm_get_swfw_semaphore(sc, SWFW_EEP_SM)) != 0) {
   15016 		aprint_error_dev(sc->sc_dev,
   15017 		    "%s: failed to get semaphore(SWFW)\n", __func__);
   15018 		return rv;
   15019 	}
   15020 
   15021 	if (((sc->sc_flags & WM_F_LOCK_EECD) != 0)
   15022 	    && (rv = wm_get_eecd(sc)) != 0) {
   15023 		aprint_error_dev(sc->sc_dev,
   15024 		    "%s: failed to get semaphore(EECD)\n", __func__);
   15025 		wm_put_swfw_semaphore(sc, SWFW_EEP_SM);
   15026 		return rv;
   15027 	}
   15028 
   15029 	return 0;
   15030 }
   15031 
   15032 static void
   15033 wm_put_nvm_80003(struct wm_softc *sc)
   15034 {
   15035 
   15036 	DPRINTF(sc, WM_DEBUG_LOCK, ("%s: %s called\n",
   15037 		device_xname(sc->sc_dev), __func__));
   15038 
   15039 	if ((sc->sc_flags & WM_F_LOCK_EECD) != 0)
   15040 		wm_put_eecd(sc);
   15041 	wm_put_swfw_semaphore(sc, SWFW_EEP_SM);
   15042 }
   15043 
   15044 static int
   15045 wm_get_nvm_82571(struct wm_softc *sc)
   15046 {
   15047 	int rv;
   15048 
   15049 	DPRINTF(sc, WM_DEBUG_LOCK, ("%s: %s called\n",
   15050 		device_xname(sc->sc_dev), __func__));
   15051 
   15052 	if ((rv = wm_get_swsm_semaphore(sc)) != 0)
   15053 		return rv;
   15054 
   15055 	switch (sc->sc_type) {
   15056 	case WM_T_82573:
   15057 		break;
   15058 	default:
   15059 		if ((sc->sc_flags & WM_F_LOCK_EECD) != 0)
   15060 			rv = wm_get_eecd(sc);
   15061 		break;
   15062 	}
   15063 
   15064 	if (rv != 0) {
   15065 		aprint_error_dev(sc->sc_dev,
   15066 		    "%s: failed to get semaphore\n",
   15067 		    __func__);
   15068 		wm_put_swsm_semaphore(sc);
   15069 	}
   15070 
   15071 	return rv;
   15072 }
   15073 
   15074 static void
   15075 wm_put_nvm_82571(struct wm_softc *sc)
   15076 {
   15077 
   15078 	DPRINTF(sc, WM_DEBUG_LOCK, ("%s: %s called\n",
   15079 		device_xname(sc->sc_dev), __func__));
   15080 
   15081 	switch (sc->sc_type) {
   15082 	case WM_T_82573:
   15083 		break;
   15084 	default:
   15085 		if ((sc->sc_flags & WM_F_LOCK_EECD) != 0)
   15086 			wm_put_eecd(sc);
   15087 		break;
   15088 	}
   15089 
   15090 	wm_put_swsm_semaphore(sc);
   15091 }
   15092 
   15093 static int
   15094 wm_get_phy_82575(struct wm_softc *sc)
   15095 {
   15096 
   15097 	DPRINTF(sc, WM_DEBUG_LOCK, ("%s: %s called\n",
   15098 		device_xname(sc->sc_dev), __func__));
   15099 	return wm_get_swfw_semaphore(sc, swfwphysem[sc->sc_funcid]);
   15100 }
   15101 
   15102 static void
   15103 wm_put_phy_82575(struct wm_softc *sc)
   15104 {
   15105 
   15106 	DPRINTF(sc, WM_DEBUG_LOCK, ("%s: %s called\n",
   15107 		device_xname(sc->sc_dev), __func__));
   15108 	wm_put_swfw_semaphore(sc, swfwphysem[sc->sc_funcid]);
   15109 }
   15110 
   15111 static int
   15112 wm_get_swfwhw_semaphore(struct wm_softc *sc)
   15113 {
   15114 	uint32_t ext_ctrl;
   15115 	int timeout = 200;
   15116 
   15117 	DPRINTF(sc, WM_DEBUG_LOCK, ("%s: %s called\n",
   15118 		device_xname(sc->sc_dev), __func__));
   15119 
   15120 	mutex_enter(sc->sc_ich_phymtx); /* Use PHY mtx for both PHY and NVM */
   15121 	for (timeout = 0; timeout < 200; timeout++) {
   15122 		ext_ctrl = CSR_READ(sc, WMREG_EXTCNFCTR);
   15123 		ext_ctrl |= EXTCNFCTR_MDIO_SW_OWNERSHIP;
   15124 		CSR_WRITE(sc, WMREG_EXTCNFCTR, ext_ctrl);
   15125 
   15126 		ext_ctrl = CSR_READ(sc, WMREG_EXTCNFCTR);
   15127 		if (ext_ctrl & EXTCNFCTR_MDIO_SW_OWNERSHIP)
   15128 			return 0;
   15129 		delay(5000);
   15130 	}
   15131 	device_printf(sc->sc_dev,
   15132 	    "failed to get swfwhw semaphore ext_ctrl 0x%x\n", ext_ctrl);
   15133 	mutex_exit(sc->sc_ich_phymtx); /* Use PHY mtx for both PHY and NVM */
   15134 	return 1;
   15135 }
   15136 
   15137 static void
   15138 wm_put_swfwhw_semaphore(struct wm_softc *sc)
   15139 {
   15140 	uint32_t ext_ctrl;
   15141 
   15142 	DPRINTF(sc, WM_DEBUG_LOCK, ("%s: %s called\n",
   15143 		device_xname(sc->sc_dev), __func__));
   15144 
   15145 	ext_ctrl = CSR_READ(sc, WMREG_EXTCNFCTR);
   15146 	ext_ctrl &= ~EXTCNFCTR_MDIO_SW_OWNERSHIP;
   15147 	CSR_WRITE(sc, WMREG_EXTCNFCTR, ext_ctrl);
   15148 
   15149 	mutex_exit(sc->sc_ich_phymtx); /* Use PHY mtx for both PHY and NVM */
   15150 }
   15151 
   15152 static int
   15153 wm_get_swflag_ich8lan(struct wm_softc *sc)
   15154 {
   15155 	uint32_t ext_ctrl;
   15156 	int timeout;
   15157 
   15158 	DPRINTF(sc, WM_DEBUG_LOCK, ("%s: %s called\n",
   15159 		device_xname(sc->sc_dev), __func__));
   15160 	mutex_enter(sc->sc_ich_phymtx);
   15161 	for (timeout = 0; timeout < WM_PHY_CFG_TIMEOUT; timeout++) {
   15162 		ext_ctrl = CSR_READ(sc, WMREG_EXTCNFCTR);
   15163 		if ((ext_ctrl & EXTCNFCTR_MDIO_SW_OWNERSHIP) == 0)
   15164 			break;
   15165 		delay(1000);
   15166 	}
   15167 	if (timeout >= WM_PHY_CFG_TIMEOUT) {
   15168 		device_printf(sc->sc_dev,
   15169 		    "SW has already locked the resource\n");
   15170 		goto out;
   15171 	}
   15172 
   15173 	ext_ctrl |= EXTCNFCTR_MDIO_SW_OWNERSHIP;
   15174 	CSR_WRITE(sc, WMREG_EXTCNFCTR, ext_ctrl);
   15175 	for (timeout = 0; timeout < 1000; timeout++) {
   15176 		ext_ctrl = CSR_READ(sc, WMREG_EXTCNFCTR);
   15177 		if (ext_ctrl & EXTCNFCTR_MDIO_SW_OWNERSHIP)
   15178 			break;
   15179 		delay(1000);
   15180 	}
   15181 	if (timeout >= 1000) {
   15182 		device_printf(sc->sc_dev, "failed to acquire semaphore\n");
   15183 		ext_ctrl &= ~EXTCNFCTR_MDIO_SW_OWNERSHIP;
   15184 		CSR_WRITE(sc, WMREG_EXTCNFCTR, ext_ctrl);
   15185 		goto out;
   15186 	}
   15187 	return 0;
   15188 
   15189 out:
   15190 	mutex_exit(sc->sc_ich_phymtx);
   15191 	return 1;
   15192 }
   15193 
   15194 static void
   15195 wm_put_swflag_ich8lan(struct wm_softc *sc)
   15196 {
   15197 	uint32_t ext_ctrl;
   15198 
   15199 	DPRINTF(sc, WM_DEBUG_LOCK, ("%s: %s called\n",
   15200 		device_xname(sc->sc_dev), __func__));
   15201 	ext_ctrl = CSR_READ(sc, WMREG_EXTCNFCTR);
   15202 	if (ext_ctrl & EXTCNFCTR_MDIO_SW_OWNERSHIP) {
   15203 		ext_ctrl &= ~EXTCNFCTR_MDIO_SW_OWNERSHIP;
   15204 		CSR_WRITE(sc, WMREG_EXTCNFCTR, ext_ctrl);
   15205 	} else
   15206 		device_printf(sc->sc_dev, "Semaphore unexpectedly released\n");
   15207 
   15208 	mutex_exit(sc->sc_ich_phymtx);
   15209 }
   15210 
   15211 static int
   15212 wm_get_nvm_ich8lan(struct wm_softc *sc)
   15213 {
   15214 
   15215 	DPRINTF(sc, WM_DEBUG_LOCK, ("%s: %s called\n",
   15216 		device_xname(sc->sc_dev), __func__));
   15217 	mutex_enter(sc->sc_ich_nvmmtx);
   15218 
   15219 	return 0;
   15220 }
   15221 
   15222 static void
   15223 wm_put_nvm_ich8lan(struct wm_softc *sc)
   15224 {
   15225 
   15226 	DPRINTF(sc, WM_DEBUG_LOCK, ("%s: %s called\n",
   15227 		device_xname(sc->sc_dev), __func__));
   15228 	mutex_exit(sc->sc_ich_nvmmtx);
   15229 }
   15230 
   15231 static int
   15232 wm_get_hw_semaphore_82573(struct wm_softc *sc)
   15233 {
   15234 	int i = 0;
   15235 	uint32_t reg;
   15236 
   15237 	DPRINTF(sc, WM_DEBUG_LOCK, ("%s: %s called\n",
   15238 		device_xname(sc->sc_dev), __func__));
   15239 
   15240 	reg = CSR_READ(sc, WMREG_EXTCNFCTR);
   15241 	do {
   15242 		CSR_WRITE(sc, WMREG_EXTCNFCTR,
   15243 		    reg | EXTCNFCTR_MDIO_SW_OWNERSHIP);
   15244 		reg = CSR_READ(sc, WMREG_EXTCNFCTR);
   15245 		if ((reg & EXTCNFCTR_MDIO_SW_OWNERSHIP) != 0)
   15246 			break;
   15247 		delay(2*1000);
   15248 		i++;
   15249 	} while (i < WM_MDIO_OWNERSHIP_TIMEOUT);
   15250 
   15251 	if (i == WM_MDIO_OWNERSHIP_TIMEOUT) {
   15252 		wm_put_hw_semaphore_82573(sc);
   15253 		log(LOG_ERR, "%s: Driver can't access the PHY\n",
   15254 		    device_xname(sc->sc_dev));
   15255 		return -1;
   15256 	}
   15257 
   15258 	return 0;
   15259 }
   15260 
   15261 static void
   15262 wm_put_hw_semaphore_82573(struct wm_softc *sc)
   15263 {
   15264 	uint32_t reg;
   15265 
   15266 	DPRINTF(sc, WM_DEBUG_LOCK, ("%s: %s called\n",
   15267 		device_xname(sc->sc_dev), __func__));
   15268 
   15269 	reg = CSR_READ(sc, WMREG_EXTCNFCTR);
   15270 	reg &= ~EXTCNFCTR_MDIO_SW_OWNERSHIP;
   15271 	CSR_WRITE(sc, WMREG_EXTCNFCTR, reg);
   15272 }
   15273 
   15274 /*
   15275  * Management mode and power management related subroutines.
   15276  * BMC, AMT, suspend/resume and EEE.
   15277  */
   15278 
   15279 #ifdef WM_WOL
   15280 static int
   15281 wm_check_mng_mode(struct wm_softc *sc)
   15282 {
   15283 	int rv;
   15284 
   15285 	switch (sc->sc_type) {
   15286 	case WM_T_ICH8:
   15287 	case WM_T_ICH9:
   15288 	case WM_T_ICH10:
   15289 	case WM_T_PCH:
   15290 	case WM_T_PCH2:
   15291 	case WM_T_PCH_LPT:
   15292 	case WM_T_PCH_SPT:
   15293 	case WM_T_PCH_CNP:
   15294 		rv = wm_check_mng_mode_ich8lan(sc);
   15295 		break;
   15296 	case WM_T_82574:
   15297 	case WM_T_82583:
   15298 		rv = wm_check_mng_mode_82574(sc);
   15299 		break;
   15300 	case WM_T_82571:
   15301 	case WM_T_82572:
   15302 	case WM_T_82573:
   15303 	case WM_T_80003:
   15304 		rv = wm_check_mng_mode_generic(sc);
   15305 		break;
   15306 	default:
   15307 		/* Noting to do */
   15308 		rv = 0;
   15309 		break;
   15310 	}
   15311 
   15312 	return rv;
   15313 }
   15314 
   15315 static int
   15316 wm_check_mng_mode_ich8lan(struct wm_softc *sc)
   15317 {
   15318 	uint32_t fwsm;
   15319 
   15320 	fwsm = CSR_READ(sc, WMREG_FWSM);
   15321 
   15322 	if (((fwsm & FWSM_FW_VALID) != 0)
   15323 	    && (__SHIFTOUT(fwsm, FWSM_MODE) == MNG_ICH_IAMT_MODE))
   15324 		return 1;
   15325 
   15326 	return 0;
   15327 }
   15328 
   15329 static int
   15330 wm_check_mng_mode_82574(struct wm_softc *sc)
   15331 {
   15332 	uint16_t data;
   15333 
   15334 	wm_nvm_read(sc, NVM_OFF_CFG2, 1, &data);
   15335 
   15336 	if ((data & NVM_CFG2_MNGM_MASK) != 0)
   15337 		return 1;
   15338 
   15339 	return 0;
   15340 }
   15341 
   15342 static int
   15343 wm_check_mng_mode_generic(struct wm_softc *sc)
   15344 {
   15345 	uint32_t fwsm;
   15346 
   15347 	fwsm = CSR_READ(sc, WMREG_FWSM);
   15348 
   15349 	if (__SHIFTOUT(fwsm, FWSM_MODE) == MNG_IAMT_MODE)
   15350 		return 1;
   15351 
   15352 	return 0;
   15353 }
   15354 #endif /* WM_WOL */
   15355 
   15356 static int
   15357 wm_enable_mng_pass_thru(struct wm_softc *sc)
   15358 {
   15359 	uint32_t manc, fwsm, factps;
   15360 
   15361 	if ((sc->sc_flags & WM_F_ASF_FIRMWARE_PRES) == 0)
   15362 		return 0;
   15363 
   15364 	manc = CSR_READ(sc, WMREG_MANC);
   15365 
   15366 	DPRINTF(sc, WM_DEBUG_MANAGE, ("%s: MANC (%08x)\n",
   15367 		device_xname(sc->sc_dev), manc));
   15368 	if ((manc & MANC_RECV_TCO_EN) == 0)
   15369 		return 0;
   15370 
   15371 	if ((sc->sc_flags & WM_F_ARC_SUBSYS_VALID) != 0) {
   15372 		fwsm = CSR_READ(sc, WMREG_FWSM);
   15373 		factps = CSR_READ(sc, WMREG_FACTPS);
   15374 		if (((factps & FACTPS_MNGCG) == 0)
   15375 		    && (__SHIFTOUT(fwsm, FWSM_MODE) == MNG_ICH_IAMT_MODE))
   15376 			return 1;
   15377 	} else if ((sc->sc_type == WM_T_82574) || (sc->sc_type == WM_T_82583)){
   15378 		uint16_t data;
   15379 
   15380 		factps = CSR_READ(sc, WMREG_FACTPS);
   15381 		wm_nvm_read(sc, NVM_OFF_CFG2, 1, &data);
   15382 		DPRINTF(sc, WM_DEBUG_MANAGE, ("%s: FACTPS = %08x, CFG2=%04x\n",
   15383 			device_xname(sc->sc_dev), factps, data));
   15384 		if (((factps & FACTPS_MNGCG) == 0)
   15385 		    && ((data & NVM_CFG2_MNGM_MASK)
   15386 			== (NVM_CFG2_MNGM_PT << NVM_CFG2_MNGM_SHIFT)))
   15387 			return 1;
   15388 	} else if (((manc & MANC_SMBUS_EN) != 0)
   15389 	    && ((manc & MANC_ASF_EN) == 0))
   15390 		return 1;
   15391 
   15392 	return 0;
   15393 }
   15394 
   15395 static bool
   15396 wm_phy_resetisblocked(struct wm_softc *sc)
   15397 {
   15398 	bool blocked = false;
   15399 	uint32_t reg;
   15400 	int i = 0;
   15401 
   15402 	DPRINTF(sc, WM_DEBUG_INIT, ("%s: %s called\n",
   15403 		device_xname(sc->sc_dev), __func__));
   15404 
   15405 	switch (sc->sc_type) {
   15406 	case WM_T_ICH8:
   15407 	case WM_T_ICH9:
   15408 	case WM_T_ICH10:
   15409 	case WM_T_PCH:
   15410 	case WM_T_PCH2:
   15411 	case WM_T_PCH_LPT:
   15412 	case WM_T_PCH_SPT:
   15413 	case WM_T_PCH_CNP:
   15414 		do {
   15415 			reg = CSR_READ(sc, WMREG_FWSM);
   15416 			if ((reg & FWSM_RSPCIPHY) == 0) {
   15417 				blocked = true;
   15418 				delay(10*1000);
   15419 				continue;
   15420 			}
   15421 			blocked = false;
   15422 		} while (blocked && (i++ < 30));
   15423 		return blocked;
   15424 		break;
   15425 	case WM_T_82571:
   15426 	case WM_T_82572:
   15427 	case WM_T_82573:
   15428 	case WM_T_82574:
   15429 	case WM_T_82583:
   15430 	case WM_T_80003:
   15431 		reg = CSR_READ(sc, WMREG_MANC);
   15432 		if ((reg & MANC_BLK_PHY_RST_ON_IDE) != 0)
   15433 			return true;
   15434 		else
   15435 			return false;
   15436 		break;
   15437 	default:
   15438 		/* No problem */
   15439 		break;
   15440 	}
   15441 
   15442 	return false;
   15443 }
   15444 
   15445 static void
   15446 wm_get_hw_control(struct wm_softc *sc)
   15447 {
   15448 	uint32_t reg;
   15449 
   15450 	DPRINTF(sc, WM_DEBUG_LOCK, ("%s: %s called\n",
   15451 		device_xname(sc->sc_dev), __func__));
   15452 
   15453 	if (sc->sc_type == WM_T_82573) {
   15454 		reg = CSR_READ(sc, WMREG_SWSM);
   15455 		CSR_WRITE(sc, WMREG_SWSM, reg | SWSM_DRV_LOAD);
   15456 	} else if (sc->sc_type >= WM_T_82571) {
   15457 		reg = CSR_READ(sc, WMREG_CTRL_EXT);
   15458 		CSR_WRITE(sc, WMREG_CTRL_EXT, reg | CTRL_EXT_DRV_LOAD);
   15459 	}
   15460 }
   15461 
   15462 static void
   15463 wm_release_hw_control(struct wm_softc *sc)
   15464 {
   15465 	uint32_t reg;
   15466 
   15467 	DPRINTF(sc, WM_DEBUG_LOCK, ("%s: %s called\n",
   15468 		device_xname(sc->sc_dev), __func__));
   15469 
   15470 	if (sc->sc_type == WM_T_82573) {
   15471 		reg = CSR_READ(sc, WMREG_SWSM);
   15472 		CSR_WRITE(sc, WMREG_SWSM, reg & ~SWSM_DRV_LOAD);
   15473 	} else if (sc->sc_type >= WM_T_82571) {
   15474 		reg = CSR_READ(sc, WMREG_CTRL_EXT);
   15475 		CSR_WRITE(sc, WMREG_CTRL_EXT, reg & ~CTRL_EXT_DRV_LOAD);
   15476 	}
   15477 }
   15478 
   15479 static void
   15480 wm_gate_hw_phy_config_ich8lan(struct wm_softc *sc, bool gate)
   15481 {
   15482 	uint32_t reg;
   15483 
   15484 	DPRINTF(sc, WM_DEBUG_INIT, ("%s: %s called\n",
   15485 		device_xname(sc->sc_dev), __func__));
   15486 
   15487 	if (sc->sc_type < WM_T_PCH2)
   15488 		return;
   15489 
   15490 	reg = CSR_READ(sc, WMREG_EXTCNFCTR);
   15491 
   15492 	if (gate)
   15493 		reg |= EXTCNFCTR_GATE_PHY_CFG;
   15494 	else
   15495 		reg &= ~EXTCNFCTR_GATE_PHY_CFG;
   15496 
   15497 	CSR_WRITE(sc, WMREG_EXTCNFCTR, reg);
   15498 }
   15499 
   15500 static int
   15501 wm_init_phy_workarounds_pchlan(struct wm_softc *sc)
   15502 {
   15503 	uint32_t fwsm, reg;
   15504 	int rv = 0;
   15505 
   15506 	DPRINTF(sc, WM_DEBUG_INIT, ("%s: %s called\n",
   15507 		device_xname(sc->sc_dev), __func__));
   15508 
   15509 	/* Gate automatic PHY configuration by hardware on non-managed 82579 */
   15510 	wm_gate_hw_phy_config_ich8lan(sc, true);
   15511 
   15512 	/* Disable ULP */
   15513 	wm_ulp_disable(sc);
   15514 
   15515 	/* Acquire PHY semaphore */
   15516 	rv = sc->phy.acquire(sc);
   15517 	if (rv != 0) {
   15518 		DPRINTF(sc, WM_DEBUG_INIT, ("%s: %s: failed\n",
   15519 		device_xname(sc->sc_dev), __func__));
   15520 		return -1;
   15521 	}
   15522 
   15523 	/* The MAC-PHY interconnect may be in SMBus mode.  If the PHY is
   15524 	 * inaccessible and resetting the PHY is not blocked, toggle the
   15525 	 * LANPHYPC Value bit to force the interconnect to PCIe mode.
   15526 	 */
   15527 	fwsm = CSR_READ(sc, WMREG_FWSM);
   15528 	switch (sc->sc_type) {
   15529 	case WM_T_PCH_LPT:
   15530 	case WM_T_PCH_SPT:
   15531 	case WM_T_PCH_CNP:
   15532 		if (wm_phy_is_accessible_pchlan(sc))
   15533 			break;
   15534 
   15535 		/* Before toggling LANPHYPC, see if PHY is accessible by
   15536 		 * forcing MAC to SMBus mode first.
   15537 		 */
   15538 		reg = CSR_READ(sc, WMREG_CTRL_EXT);
   15539 		reg |= CTRL_EXT_FORCE_SMBUS;
   15540 		CSR_WRITE(sc, WMREG_CTRL_EXT, reg);
   15541 #if 0
   15542 		/* XXX Isn't this required??? */
   15543 		CSR_WRITE_FLUSH(sc);
   15544 #endif
   15545 		/* Wait 50 milliseconds for MAC to finish any retries
   15546 		 * that it might be trying to perform from previous
   15547 		 * attempts to acknowledge any phy read requests.
   15548 		 */
   15549 		delay(50 * 1000);
   15550 		/* FALLTHROUGH */
   15551 	case WM_T_PCH2:
   15552 		if (wm_phy_is_accessible_pchlan(sc) == true)
   15553 			break;
   15554 		/* FALLTHROUGH */
   15555 	case WM_T_PCH:
   15556 		if (sc->sc_type == WM_T_PCH)
   15557 			if ((fwsm & FWSM_FW_VALID) != 0)
   15558 				break;
   15559 
   15560 		if (wm_phy_resetisblocked(sc) == true) {
   15561 			device_printf(sc->sc_dev, "XXX reset is blocked(3)\n");
   15562 			break;
   15563 		}
   15564 
   15565 		/* Toggle LANPHYPC Value bit */
   15566 		wm_toggle_lanphypc_pch_lpt(sc);
   15567 
   15568 		if (sc->sc_type >= WM_T_PCH_LPT) {
   15569 			if (wm_phy_is_accessible_pchlan(sc) == true)
   15570 				break;
   15571 
   15572 			/* Toggling LANPHYPC brings the PHY out of SMBus mode
   15573 			 * so ensure that the MAC is also out of SMBus mode
   15574 			 */
   15575 			reg = CSR_READ(sc, WMREG_CTRL_EXT);
   15576 			reg &= ~CTRL_EXT_FORCE_SMBUS;
   15577 			CSR_WRITE(sc, WMREG_CTRL_EXT, reg);
   15578 
   15579 			if (wm_phy_is_accessible_pchlan(sc) == true)
   15580 				break;
   15581 			rv = -1;
   15582 		}
   15583 		break;
   15584 	default:
   15585 		break;
   15586 	}
   15587 
   15588 	/* Release semaphore */
   15589 	sc->phy.release(sc);
   15590 
   15591 	if (rv == 0) {
   15592 		/* Check to see if able to reset PHY.  Print error if not */
   15593 		if (wm_phy_resetisblocked(sc)) {
   15594 			device_printf(sc->sc_dev, "XXX reset is blocked(4)\n");
   15595 			goto out;
   15596 		}
   15597 
   15598 		/* Reset the PHY before any access to it.  Doing so, ensures
   15599 		 * that the PHY is in a known good state before we read/write
   15600 		 * PHY registers.  The generic reset is sufficient here,
   15601 		 * because we haven't determined the PHY type yet.
   15602 		 */
   15603 		if (wm_reset_phy(sc) != 0)
   15604 			goto out;
   15605 
   15606 		/* On a successful reset, possibly need to wait for the PHY
   15607 		 * to quiesce to an accessible state before returning control
   15608 		 * to the calling function.  If the PHY does not quiesce, then
   15609 		 * return E1000E_BLK_PHY_RESET, as this is the condition that
   15610 		 *  the PHY is in.
   15611 		 */
   15612 		if (wm_phy_resetisblocked(sc))
   15613 			device_printf(sc->sc_dev, "XXX reset is blocked(4)\n");
   15614 	}
   15615 
   15616 out:
   15617 	/* Ungate automatic PHY configuration on non-managed 82579 */
   15618 	if ((sc->sc_type == WM_T_PCH2) && ((fwsm & FWSM_FW_VALID) == 0)) {
   15619 		delay(10*1000);
   15620 		wm_gate_hw_phy_config_ich8lan(sc, false);
   15621 	}
   15622 
   15623 	return 0;
   15624 }
   15625 
   15626 static void
   15627 wm_init_manageability(struct wm_softc *sc)
   15628 {
   15629 
   15630 	DPRINTF(sc, WM_DEBUG_INIT, ("%s: %s called\n",
   15631 		device_xname(sc->sc_dev), __func__));
   15632 	if (sc->sc_flags & WM_F_HAS_MANAGE) {
   15633 		uint32_t manc2h = CSR_READ(sc, WMREG_MANC2H);
   15634 		uint32_t manc = CSR_READ(sc, WMREG_MANC);
   15635 
   15636 		/* Disable hardware interception of ARP */
   15637 		manc &= ~MANC_ARP_EN;
   15638 
   15639 		/* Enable receiving management packets to the host */
   15640 		if (sc->sc_type >= WM_T_82571) {
   15641 			manc |= MANC_EN_MNG2HOST;
   15642 			manc2h |= MANC2H_PORT_623 | MANC2H_PORT_624;
   15643 			CSR_WRITE(sc, WMREG_MANC2H, manc2h);
   15644 		}
   15645 
   15646 		CSR_WRITE(sc, WMREG_MANC, manc);
   15647 	}
   15648 }
   15649 
   15650 static void
   15651 wm_release_manageability(struct wm_softc *sc)
   15652 {
   15653 
   15654 	if (sc->sc_flags & WM_F_HAS_MANAGE) {
   15655 		uint32_t manc = CSR_READ(sc, WMREG_MANC);
   15656 
   15657 		manc |= MANC_ARP_EN;
   15658 		if (sc->sc_type >= WM_T_82571)
   15659 			manc &= ~MANC_EN_MNG2HOST;
   15660 
   15661 		CSR_WRITE(sc, WMREG_MANC, manc);
   15662 	}
   15663 }
   15664 
   15665 static void
   15666 wm_get_wakeup(struct wm_softc *sc)
   15667 {
   15668 
   15669 	/* 0: HAS_AMT, ARC_SUBSYS_VALID, ASF_FIRMWARE_PRES */
   15670 	switch (sc->sc_type) {
   15671 	case WM_T_82573:
   15672 	case WM_T_82583:
   15673 		sc->sc_flags |= WM_F_HAS_AMT;
   15674 		/* FALLTHROUGH */
   15675 	case WM_T_80003:
   15676 	case WM_T_82575:
   15677 	case WM_T_82576:
   15678 	case WM_T_82580:
   15679 	case WM_T_I350:
   15680 	case WM_T_I354:
   15681 		if ((CSR_READ(sc, WMREG_FWSM) & FWSM_MODE) != 0)
   15682 			sc->sc_flags |= WM_F_ARC_SUBSYS_VALID;
   15683 		/* FALLTHROUGH */
   15684 	case WM_T_82541:
   15685 	case WM_T_82541_2:
   15686 	case WM_T_82547:
   15687 	case WM_T_82547_2:
   15688 	case WM_T_82571:
   15689 	case WM_T_82572:
   15690 	case WM_T_82574:
   15691 		sc->sc_flags |= WM_F_ASF_FIRMWARE_PRES;
   15692 		break;
   15693 	case WM_T_ICH8:
   15694 	case WM_T_ICH9:
   15695 	case WM_T_ICH10:
   15696 	case WM_T_PCH:
   15697 	case WM_T_PCH2:
   15698 	case WM_T_PCH_LPT:
   15699 	case WM_T_PCH_SPT:
   15700 	case WM_T_PCH_CNP:
   15701 		sc->sc_flags |= WM_F_HAS_AMT;
   15702 		sc->sc_flags |= WM_F_ASF_FIRMWARE_PRES;
   15703 		break;
   15704 	default:
   15705 		break;
   15706 	}
   15707 
   15708 	/* 1: HAS_MANAGE */
   15709 	if (wm_enable_mng_pass_thru(sc) != 0)
   15710 		sc->sc_flags |= WM_F_HAS_MANAGE;
   15711 
   15712 	/*
   15713 	 * Note that the WOL flags is set after the resetting of the eeprom
   15714 	 * stuff
   15715 	 */
   15716 }
   15717 
   15718 /*
   15719  * Unconfigure Ultra Low Power mode.
   15720  * Only for I217 and newer (see below).
   15721  */
   15722 static int
   15723 wm_ulp_disable(struct wm_softc *sc)
   15724 {
   15725 	uint32_t reg;
   15726 	uint16_t phyreg;
   15727 	int i = 0, rv = 0;
   15728 
   15729 	DPRINTF(sc, WM_DEBUG_INIT, ("%s: %s called\n",
   15730 		device_xname(sc->sc_dev), __func__));
   15731 	/* Exclude old devices */
   15732 	if ((sc->sc_type < WM_T_PCH_LPT)
   15733 	    || (sc->sc_pcidevid == PCI_PRODUCT_INTEL_I217_LM)
   15734 	    || (sc->sc_pcidevid == PCI_PRODUCT_INTEL_I217_V)
   15735 	    || (sc->sc_pcidevid == PCI_PRODUCT_INTEL_I218_LM2)
   15736 	    || (sc->sc_pcidevid == PCI_PRODUCT_INTEL_I218_V2))
   15737 		return 0;
   15738 
   15739 	if ((CSR_READ(sc, WMREG_FWSM) & FWSM_FW_VALID) != 0) {
   15740 		/* Request ME un-configure ULP mode in the PHY */
   15741 		reg = CSR_READ(sc, WMREG_H2ME);
   15742 		reg &= ~H2ME_ULP;
   15743 		reg |= H2ME_ENFORCE_SETTINGS;
   15744 		CSR_WRITE(sc, WMREG_H2ME, reg);
   15745 
   15746 		/* Poll up to 300msec for ME to clear ULP_CFG_DONE. */
   15747 		while ((CSR_READ(sc, WMREG_FWSM) & FWSM_ULP_CFG_DONE) != 0) {
   15748 			if (i++ == 30) {
   15749 				device_printf(sc->sc_dev, "%s timed out\n",
   15750 				    __func__);
   15751 				return -1;
   15752 			}
   15753 			delay(10 * 1000);
   15754 		}
   15755 		reg = CSR_READ(sc, WMREG_H2ME);
   15756 		reg &= ~H2ME_ENFORCE_SETTINGS;
   15757 		CSR_WRITE(sc, WMREG_H2ME, reg);
   15758 
   15759 		return 0;
   15760 	}
   15761 
   15762 	/* Acquire semaphore */
   15763 	rv = sc->phy.acquire(sc);
   15764 	if (rv != 0) {
   15765 		DPRINTF(sc, WM_DEBUG_INIT, ("%s: %s: failed\n",
   15766 		device_xname(sc->sc_dev), __func__));
   15767 		return -1;
   15768 	}
   15769 
   15770 	/* Toggle LANPHYPC */
   15771 	wm_toggle_lanphypc_pch_lpt(sc);
   15772 
   15773 	/* Unforce SMBus mode in PHY */
   15774 	rv = wm_gmii_hv_readreg_locked(sc->sc_dev, 2, CV_SMB_CTRL, &phyreg);
   15775 	if (rv != 0) {
   15776 		uint32_t reg2;
   15777 
   15778 		aprint_debug_dev(sc->sc_dev, "%s: Force SMBus first.\n",
   15779 			__func__);
   15780 		reg2 = CSR_READ(sc, WMREG_CTRL_EXT);
   15781 		reg2 |= CTRL_EXT_FORCE_SMBUS;
   15782 		CSR_WRITE(sc, WMREG_CTRL_EXT, reg2);
   15783 		delay(50 * 1000);
   15784 
   15785 		rv = wm_gmii_hv_readreg_locked(sc->sc_dev, 2, CV_SMB_CTRL,
   15786 		    &phyreg);
   15787 		if (rv != 0)
   15788 			goto release;
   15789 	}
   15790 	phyreg &= ~CV_SMB_CTRL_FORCE_SMBUS;
   15791 	wm_gmii_hv_writereg_locked(sc->sc_dev, 2, CV_SMB_CTRL, phyreg);
   15792 
   15793 	/* Unforce SMBus mode in MAC */
   15794 	reg = CSR_READ(sc, WMREG_CTRL_EXT);
   15795 	reg &= ~CTRL_EXT_FORCE_SMBUS;
   15796 	CSR_WRITE(sc, WMREG_CTRL_EXT, reg);
   15797 
   15798 	rv = wm_gmii_hv_readreg_locked(sc->sc_dev, 2, HV_PM_CTRL, &phyreg);
   15799 	if (rv != 0)
   15800 		goto release;
   15801 	phyreg |= HV_PM_CTRL_K1_ENA;
   15802 	wm_gmii_hv_writereg_locked(sc->sc_dev, 2, HV_PM_CTRL, phyreg);
   15803 
   15804 	rv = wm_gmii_hv_readreg_locked(sc->sc_dev, 2, I218_ULP_CONFIG1,
   15805 		&phyreg);
   15806 	if (rv != 0)
   15807 		goto release;
   15808 	phyreg &= ~(I218_ULP_CONFIG1_IND
   15809 	    | I218_ULP_CONFIG1_STICKY_ULP
   15810 	    | I218_ULP_CONFIG1_RESET_TO_SMBUS
   15811 	    | I218_ULP_CONFIG1_WOL_HOST
   15812 	    | I218_ULP_CONFIG1_INBAND_EXIT
   15813 	    | I218_ULP_CONFIG1_EN_ULP_LANPHYPC
   15814 	    | I218_ULP_CONFIG1_DIS_CLR_STICKY_ON_PERST
   15815 	    | I218_ULP_CONFIG1_DIS_SMB_PERST);
   15816 	wm_gmii_hv_writereg_locked(sc->sc_dev, 2, I218_ULP_CONFIG1, phyreg);
   15817 	phyreg |= I218_ULP_CONFIG1_START;
   15818 	wm_gmii_hv_writereg_locked(sc->sc_dev, 2, I218_ULP_CONFIG1, phyreg);
   15819 
   15820 	reg = CSR_READ(sc, WMREG_FEXTNVM7);
   15821 	reg &= ~FEXTNVM7_DIS_SMB_PERST;
   15822 	CSR_WRITE(sc, WMREG_FEXTNVM7, reg);
   15823 
   15824 release:
   15825 	/* Release semaphore */
   15826 	sc->phy.release(sc);
   15827 	wm_gmii_reset(sc);
   15828 	delay(50 * 1000);
   15829 
   15830 	return rv;
   15831 }
   15832 
   15833 /* WOL in the newer chipset interfaces (pchlan) */
   15834 static int
   15835 wm_enable_phy_wakeup(struct wm_softc *sc)
   15836 {
   15837 	device_t dev = sc->sc_dev;
   15838 	uint32_t mreg, moff;
   15839 	uint16_t wuce, wuc, wufc, preg;
   15840 	int i, rv;
   15841 
   15842 	KASSERT(sc->sc_type >= WM_T_PCH);
   15843 
   15844 	/* Copy MAC RARs to PHY RARs */
   15845 	wm_copy_rx_addrs_to_phy_ich8lan(sc);
   15846 
   15847 	/* Activate PHY wakeup */
   15848 	rv = sc->phy.acquire(sc);
   15849 	if (rv != 0) {
   15850 		device_printf(dev, "%s: failed to acquire semaphore\n",
   15851 		    __func__);
   15852 		return rv;
   15853 	}
   15854 
   15855 	/*
   15856 	 * Enable access to PHY wakeup registers.
   15857 	 * BM_MTA, BM_RCTL, BM_WUFC and BM_WUC are in BM_WUC_PAGE.
   15858 	 */
   15859 	rv = wm_enable_phy_wakeup_reg_access_bm(dev, &wuce);
   15860 	if (rv != 0) {
   15861 		device_printf(dev,
   15862 		    "%s: Could not enable PHY wakeup reg access\n", __func__);
   15863 		goto release;
   15864 	}
   15865 
   15866 	/* Copy MAC MTA to PHY MTA */
   15867 	for (i = 0; i < WM_ICH8_MC_TABSIZE; i++) {
   15868 		uint16_t lo, hi;
   15869 
   15870 		mreg = CSR_READ(sc, WMREG_CORDOVA_MTA + (i * 4));
   15871 		lo = (uint16_t)(mreg & 0xffff);
   15872 		hi = (uint16_t)((mreg >> 16) & 0xffff);
   15873 		wm_access_phy_wakeup_reg_bm(dev, BM_MTA(i), &lo, 0, true);
   15874 		wm_access_phy_wakeup_reg_bm(dev, BM_MTA(i) + 1, &hi, 0, true);
   15875 	}
   15876 
   15877 	/* Configure PHY Rx Control register */
   15878 	wm_access_phy_wakeup_reg_bm(dev, BM_RCTL, &preg, 1, true);
   15879 	mreg = CSR_READ(sc, WMREG_RCTL);
   15880 	if (mreg & RCTL_UPE)
   15881 		preg |= BM_RCTL_UPE;
   15882 	if (mreg & RCTL_MPE)
   15883 		preg |= BM_RCTL_MPE;
   15884 	preg &= ~(BM_RCTL_MO_MASK);
   15885 	moff = __SHIFTOUT(mreg, RCTL_MO);
   15886 	if (moff != 0)
   15887 		preg |= moff << BM_RCTL_MO_SHIFT;
   15888 	if (mreg & RCTL_BAM)
   15889 		preg |= BM_RCTL_BAM;
   15890 	if (mreg & RCTL_PMCF)
   15891 		preg |= BM_RCTL_PMCF;
   15892 	mreg = CSR_READ(sc, WMREG_CTRL);
   15893 	if (mreg & CTRL_RFCE)
   15894 		preg |= BM_RCTL_RFCE;
   15895 	wm_access_phy_wakeup_reg_bm(dev, BM_RCTL, &preg, 0, true);
   15896 
   15897 	wuc = WUC_APME | WUC_PME_EN;
   15898 	wufc = WUFC_MAG;
   15899 	/* Enable PHY wakeup in MAC register */
   15900 	CSR_WRITE(sc, WMREG_WUC,
   15901 	    WUC_PHY_WAKE | WUC_PME_STATUS | WUC_APMPME | wuc);
   15902 	CSR_WRITE(sc, WMREG_WUFC, wufc);
   15903 
   15904 	/* Configure and enable PHY wakeup in PHY registers */
   15905 	wm_access_phy_wakeup_reg_bm(dev, BM_WUC, &wuc, 0, true);
   15906 	wm_access_phy_wakeup_reg_bm(dev, BM_WUFC, &wufc, 0, true);
   15907 
   15908 	wuce |= BM_WUC_ENABLE_BIT | BM_WUC_HOST_WU_BIT;
   15909 	wm_disable_phy_wakeup_reg_access_bm(dev, &wuce);
   15910 
   15911 release:
   15912 	sc->phy.release(sc);
   15913 
   15914 	return 0;
   15915 }
   15916 
   15917 /* Power down workaround on D3 */
   15918 static void
   15919 wm_igp3_phy_powerdown_workaround_ich8lan(struct wm_softc *sc)
   15920 {
   15921 	uint32_t reg;
   15922 	uint16_t phyreg;
   15923 	int i;
   15924 
   15925 	for (i = 0; i < 2; i++) {
   15926 		/* Disable link */
   15927 		reg = CSR_READ(sc, WMREG_PHY_CTRL);
   15928 		reg |= PHY_CTRL_GBE_DIS | PHY_CTRL_NOND0A_GBE_DIS;
   15929 		CSR_WRITE(sc, WMREG_PHY_CTRL, reg);
   15930 
   15931 		/*
   15932 		 * Call gig speed drop workaround on Gig disable before
   15933 		 * accessing any PHY registers
   15934 		 */
   15935 		if (sc->sc_type == WM_T_ICH8)
   15936 			wm_gig_downshift_workaround_ich8lan(sc);
   15937 
   15938 		/* Write VR power-down enable */
   15939 		sc->sc_mii.mii_readreg(sc->sc_dev, 1, IGP3_VR_CTRL, &phyreg);
   15940 		phyreg &= ~IGP3_VR_CTRL_DEV_POWERDOWN_MODE_MASK;
   15941 		phyreg |= IGP3_VR_CTRL_MODE_SHUTDOWN;
   15942 		sc->sc_mii.mii_writereg(sc->sc_dev, 1, IGP3_VR_CTRL, phyreg);
   15943 
   15944 		/* Read it back and test */
   15945 		sc->sc_mii.mii_readreg(sc->sc_dev, 1, IGP3_VR_CTRL, &phyreg);
   15946 		phyreg &= IGP3_VR_CTRL_DEV_POWERDOWN_MODE_MASK;
   15947 		if ((phyreg == IGP3_VR_CTRL_MODE_SHUTDOWN) || (i != 0))
   15948 			break;
   15949 
   15950 		/* Issue PHY reset and repeat at most one more time */
   15951 		CSR_WRITE(sc, WMREG_CTRL, sc->sc_ctrl | CTRL_PHY_RESET);
   15952 	}
   15953 }
   15954 
   15955 /*
   15956  *  wm_suspend_workarounds_ich8lan - workarounds needed during S0->Sx
   15957  *  @sc: pointer to the HW structure
   15958  *
   15959  *  During S0 to Sx transition, it is possible the link remains at gig
   15960  *  instead of negotiating to a lower speed.  Before going to Sx, set
   15961  *  'Gig Disable' to force link speed negotiation to a lower speed based on
   15962  *  the LPLU setting in the NVM or custom setting.  For PCH and newer parts,
   15963  *  the OEM bits PHY register (LED, GbE disable and LPLU configurations) also
   15964  *  needs to be written.
   15965  *  Parts that support (and are linked to a partner which support) EEE in
   15966  *  100Mbps should disable LPLU since 100Mbps w/ EEE requires less power
   15967  *  than 10Mbps w/o EEE.
   15968  */
   15969 static void
   15970 wm_suspend_workarounds_ich8lan(struct wm_softc *sc)
   15971 {
   15972 	device_t dev = sc->sc_dev;
   15973 	struct ethercom *ec = &sc->sc_ethercom;
   15974 	uint32_t phy_ctrl;
   15975 	int rv;
   15976 
   15977 	phy_ctrl = CSR_READ(sc, WMREG_PHY_CTRL);
   15978 	phy_ctrl |= PHY_CTRL_GBE_DIS;
   15979 
   15980 	KASSERT((sc->sc_type >= WM_T_ICH8) && (sc->sc_type <= WM_T_PCH_CNP));
   15981 
   15982 	if (sc->sc_phytype == WMPHY_I217) {
   15983 		uint16_t devid = sc->sc_pcidevid;
   15984 
   15985 		if ((devid == PCI_PRODUCT_INTEL_I218_LM) ||
   15986 		    (devid == PCI_PRODUCT_INTEL_I218_V) ||
   15987 		    (devid == PCI_PRODUCT_INTEL_I218_LM3) ||
   15988 		    (devid == PCI_PRODUCT_INTEL_I218_V3) ||
   15989 		    (sc->sc_type >= WM_T_PCH_SPT))
   15990 			CSR_WRITE(sc, WMREG_FEXTNVM6,
   15991 			    CSR_READ(sc, WMREG_FEXTNVM6)
   15992 			    & ~FEXTNVM6_REQ_PLL_CLK);
   15993 
   15994 		if (sc->phy.acquire(sc) != 0)
   15995 			goto out;
   15996 
   15997 		if ((ec->ec_capenable & ETHERCAP_EEE) != 0) {
   15998 			uint16_t eee_advert;
   15999 
   16000 			rv = wm_read_emi_reg_locked(dev,
   16001 			    I217_EEE_ADVERTISEMENT, &eee_advert);
   16002 			if (rv)
   16003 				goto release;
   16004 
   16005 			/*
   16006 			 * Disable LPLU if both link partners support 100BaseT
   16007 			 * EEE and 100Full is advertised on both ends of the
   16008 			 * link, and enable Auto Enable LPI since there will
   16009 			 * be no driver to enable LPI while in Sx.
   16010 			 */
   16011 			if ((eee_advert & AN_EEEADVERT_100_TX) &&
   16012 			    (sc->eee_lp_ability & AN_EEEADVERT_100_TX)) {
   16013 				uint16_t anar, phy_reg;
   16014 
   16015 				sc->phy.readreg_locked(dev, 2, MII_ANAR,
   16016 				    &anar);
   16017 				if (anar & ANAR_TX_FD) {
   16018 					phy_ctrl &= ~(PHY_CTRL_D0A_LPLU |
   16019 					    PHY_CTRL_NOND0A_LPLU);
   16020 
   16021 					/* Set Auto Enable LPI after link up */
   16022 					sc->phy.readreg_locked(dev, 2,
   16023 					    I217_LPI_GPIO_CTRL, &phy_reg);
   16024 					phy_reg |= I217_LPI_GPIO_CTRL_AUTO_EN_LPI;
   16025 					sc->phy.writereg_locked(dev, 2,
   16026 					    I217_LPI_GPIO_CTRL, phy_reg);
   16027 				}
   16028 			}
   16029 		}
   16030 
   16031 		/*
   16032 		 * For i217 Intel Rapid Start Technology support,
   16033 		 * when the system is going into Sx and no manageability engine
   16034 		 * is present, the driver must configure proxy to reset only on
   16035 		 * power good.	LPI (Low Power Idle) state must also reset only
   16036 		 * on power good, as well as the MTA (Multicast table array).
   16037 		 * The SMBus release must also be disabled on LCD reset.
   16038 		 */
   16039 
   16040 		/*
   16041 		 * Enable MTA to reset for Intel Rapid Start Technology
   16042 		 * Support
   16043 		 */
   16044 
   16045 release:
   16046 		sc->phy.release(sc);
   16047 	}
   16048 out:
   16049 	CSR_WRITE(sc, WMREG_PHY_CTRL, phy_ctrl);
   16050 
   16051 	if (sc->sc_type == WM_T_ICH8)
   16052 		wm_gig_downshift_workaround_ich8lan(sc);
   16053 
   16054 	if (sc->sc_type >= WM_T_PCH) {
   16055 		wm_oem_bits_config_ich8lan(sc, false);
   16056 
   16057 		/* Reset PHY to activate OEM bits on 82577/8 */
   16058 		if (sc->sc_type == WM_T_PCH)
   16059 			wm_reset_phy(sc);
   16060 
   16061 		if (sc->phy.acquire(sc) != 0)
   16062 			return;
   16063 		wm_write_smbus_addr(sc);
   16064 		sc->phy.release(sc);
   16065 	}
   16066 }
   16067 
   16068 /*
   16069  *  wm_resume_workarounds_pchlan - workarounds needed during Sx->S0
   16070  *  @sc: pointer to the HW structure
   16071  *
   16072  *  During Sx to S0 transitions on non-managed devices or managed devices
   16073  *  on which PHY resets are not blocked, if the PHY registers cannot be
   16074  *  accessed properly by the s/w toggle the LANPHYPC value to power cycle
   16075  *  the PHY.
   16076  *  On i217, setup Intel Rapid Start Technology.
   16077  */
   16078 static int
   16079 wm_resume_workarounds_pchlan(struct wm_softc *sc)
   16080 {
   16081 	device_t dev = sc->sc_dev;
   16082 	int rv;
   16083 
   16084 	if (sc->sc_type < WM_T_PCH2)
   16085 		return 0;
   16086 
   16087 	rv = wm_init_phy_workarounds_pchlan(sc);
   16088 	if (rv != 0)
   16089 		return -1;
   16090 
   16091 	/* For i217 Intel Rapid Start Technology support when the system
   16092 	 * is transitioning from Sx and no manageability engine is present
   16093 	 * configure SMBus to restore on reset, disable proxy, and enable
   16094 	 * the reset on MTA (Multicast table array).
   16095 	 */
   16096 	if (sc->sc_phytype == WMPHY_I217) {
   16097 		uint16_t phy_reg;
   16098 
   16099 		if (sc->phy.acquire(sc) != 0)
   16100 			return -1;
   16101 
   16102 		/* Clear Auto Enable LPI after link up */
   16103 		sc->phy.readreg_locked(dev, 1, I217_LPI_GPIO_CTRL, &phy_reg);
   16104 		phy_reg &= ~I217_LPI_GPIO_CTRL_AUTO_EN_LPI;
   16105 		sc->phy.writereg_locked(dev, 1, I217_LPI_GPIO_CTRL, phy_reg);
   16106 
   16107 		if ((CSR_READ(sc, WMREG_FWSM) & FWSM_FW_VALID) == 0) {
   16108 			/* Restore clear on SMB if no manageability engine
   16109 			 * is present
   16110 			 */
   16111 			rv = sc->phy.readreg_locked(dev, 1, I217_MEMPWR,
   16112 			    &phy_reg);
   16113 			if (rv != 0)
   16114 				goto release;
   16115 			phy_reg |= I217_MEMPWR_DISABLE_SMB_RELEASE;
   16116 			sc->phy.writereg_locked(dev, 1, I217_MEMPWR, phy_reg);
   16117 
   16118 			/* Disable Proxy */
   16119 			sc->phy.writereg_locked(dev, 1, I217_PROXY_CTRL, 0);
   16120 		}
   16121 		/* Enable reset on MTA */
   16122 		sc->phy.readreg_locked(dev, 1, I217_CFGREG, &phy_reg);
   16123 		if (rv != 0)
   16124 			goto release;
   16125 		phy_reg &= ~I217_CGFREG_ENABLE_MTA_RESET;
   16126 		sc->phy.writereg_locked(dev, 1, I217_CFGREG, phy_reg);
   16127 
   16128 release:
   16129 		sc->phy.release(sc);
   16130 		return rv;
   16131 	}
   16132 
   16133 	return 0;
   16134 }
   16135 
   16136 static void
   16137 wm_enable_wakeup(struct wm_softc *sc)
   16138 {
   16139 	uint32_t reg, pmreg;
   16140 	pcireg_t pmode;
   16141 	int rv = 0;
   16142 
   16143 	DPRINTF(sc, WM_DEBUG_INIT, ("%s: %s called\n",
   16144 		device_xname(sc->sc_dev), __func__));
   16145 
   16146 	if (pci_get_capability(sc->sc_pc, sc->sc_pcitag, PCI_CAP_PWRMGMT,
   16147 	    &pmreg, NULL) == 0)
   16148 		return;
   16149 
   16150 	if ((sc->sc_flags & WM_F_WOL) == 0)
   16151 		goto pme;
   16152 
   16153 	/* Advertise the wakeup capability */
   16154 	CSR_WRITE(sc, WMREG_CTRL, sc->sc_ctrl | CTRL_SWDPIN(2)
   16155 	    | CTRL_SWDPIN(3));
   16156 
   16157 	/* Keep the laser running on fiber adapters */
   16158 	if ((sc->sc_mediatype == WM_MEDIATYPE_FIBER)
   16159 	    || (sc->sc_mediatype == WM_MEDIATYPE_SERDES)) {
   16160 		reg = CSR_READ(sc, WMREG_CTRL_EXT);
   16161 		reg |= CTRL_EXT_SWDPIN(3);
   16162 		CSR_WRITE(sc, WMREG_CTRL_EXT, reg);
   16163 	}
   16164 
   16165 	if ((sc->sc_type == WM_T_ICH8) || (sc->sc_type == WM_T_ICH9) ||
   16166 	    (sc->sc_type == WM_T_ICH10) || (sc->sc_type == WM_T_PCH) ||
   16167 	    (sc->sc_type == WM_T_PCH2) || (sc->sc_type == WM_T_PCH_LPT) ||
   16168 	    (sc->sc_type == WM_T_PCH_SPT) || (sc->sc_type == WM_T_PCH_CNP))
   16169 		wm_suspend_workarounds_ich8lan(sc);
   16170 
   16171 #if 0	/* For the multicast packet */
   16172 	reg = CSR_READ(sc, WMREG_WUFC) | WUFC_MAG;
   16173 	reg |= WUFC_MC;
   16174 	CSR_WRITE(sc, WMREG_RCTL, CSR_READ(sc, WMREG_RCTL) | RCTL_MPE);
   16175 #endif
   16176 
   16177 	if (sc->sc_type >= WM_T_PCH) {
   16178 		rv = wm_enable_phy_wakeup(sc);
   16179 		if (rv != 0)
   16180 			goto pme;
   16181 	} else {
   16182 		/* Enable wakeup by the MAC */
   16183 		CSR_WRITE(sc, WMREG_WUC, WUC_APME | WUC_PME_EN);
   16184 		CSR_WRITE(sc, WMREG_WUFC, WUFC_MAG);
   16185 	}
   16186 
   16187 	if (((sc->sc_type == WM_T_ICH8) || (sc->sc_type == WM_T_ICH9)
   16188 		|| (sc->sc_type == WM_T_ICH10) || (sc->sc_type == WM_T_PCH)
   16189 		|| (sc->sc_type == WM_T_PCH2))
   16190 	    && (sc->sc_phytype == WMPHY_IGP_3))
   16191 		wm_igp3_phy_powerdown_workaround_ich8lan(sc);
   16192 
   16193 pme:
   16194 	/* Request PME */
   16195 	pmode = pci_conf_read(sc->sc_pc, sc->sc_pcitag, pmreg + PCI_PMCSR);
   16196 	pmode |= PCI_PMCSR_PME_STS; /* in case it's already set (W1C) */
   16197 	if ((rv == 0) && (sc->sc_flags & WM_F_WOL) != 0) {
   16198 		/* For WOL */
   16199 		pmode |= PCI_PMCSR_PME_EN;
   16200 	} else {
   16201 		/* Disable WOL */
   16202 		pmode &= ~PCI_PMCSR_PME_EN;
   16203 	}
   16204 	pci_conf_write(sc->sc_pc, sc->sc_pcitag, pmreg + PCI_PMCSR, pmode);
   16205 }
   16206 
   16207 /* Disable ASPM L0s and/or L1 for workaround */
   16208 static void
   16209 wm_disable_aspm(struct wm_softc *sc)
   16210 {
   16211 	pcireg_t reg, mask = 0;
   16212 	unsigned const char *str = "";
   16213 
   16214 	/*
   16215 	 *  Only for PCIe device which has PCIe capability in the PCI config
   16216 	 * space.
   16217 	 */
   16218 	if (((sc->sc_flags & WM_F_PCIE) == 0) || (sc->sc_pcixe_capoff == 0))
   16219 		return;
   16220 
   16221 	switch (sc->sc_type) {
   16222 	case WM_T_82571:
   16223 	case WM_T_82572:
   16224 		/*
   16225 		 * 8257[12] Errata 13: Device Does Not Support PCIe Active
   16226 		 * State Power management L1 State (ASPM L1).
   16227 		 */
   16228 		mask = PCIE_LCSR_ASPM_L1;
   16229 		str = "L1 is";
   16230 		break;
   16231 	case WM_T_82573:
   16232 	case WM_T_82574:
   16233 	case WM_T_82583:
   16234 		/*
   16235 		 * The 82573 disappears when PCIe ASPM L0s is enabled.
   16236 		 *
   16237 		 * The 82574 and 82583 does not support PCIe ASPM L0s with
   16238 		 * some chipset.  The document of 82574 and 82583 says that
   16239 		 * disabling L0s with some specific chipset is sufficient,
   16240 		 * but we follow as of the Intel em driver does.
   16241 		 *
   16242 		 * References:
   16243 		 * Errata 8 of the Specification Update of i82573.
   16244 		 * Errata 20 of the Specification Update of i82574.
   16245 		 * Errata 9 of the Specification Update of i82583.
   16246 		 */
   16247 		mask = PCIE_LCSR_ASPM_L1 | PCIE_LCSR_ASPM_L0S;
   16248 		str = "L0s and L1 are";
   16249 		break;
   16250 	default:
   16251 		return;
   16252 	}
   16253 
   16254 	reg = pci_conf_read(sc->sc_pc, sc->sc_pcitag,
   16255 	    sc->sc_pcixe_capoff + PCIE_LCSR);
   16256 	reg &= ~mask;
   16257 	pci_conf_write(sc->sc_pc, sc->sc_pcitag,
   16258 	    sc->sc_pcixe_capoff + PCIE_LCSR, reg);
   16259 
   16260 	/* Print only in wm_attach() */
   16261 	if ((sc->sc_flags & WM_F_ATTACHED) == 0)
   16262 		aprint_verbose_dev(sc->sc_dev,
   16263 		    "ASPM %s disabled to workaround the errata.\n", str);
   16264 }
   16265 
   16266 /* LPLU */
   16267 
   16268 static void
   16269 wm_lplu_d0_disable(struct wm_softc *sc)
   16270 {
   16271 	struct mii_data *mii = &sc->sc_mii;
   16272 	uint32_t reg;
   16273 	uint16_t phyval;
   16274 
   16275 	DPRINTF(sc, WM_DEBUG_INIT, ("%s: %s called\n",
   16276 		device_xname(sc->sc_dev), __func__));
   16277 
   16278 	if (sc->sc_phytype == WMPHY_IFE)
   16279 		return;
   16280 
   16281 	switch (sc->sc_type) {
   16282 	case WM_T_82571:
   16283 	case WM_T_82572:
   16284 	case WM_T_82573:
   16285 	case WM_T_82575:
   16286 	case WM_T_82576:
   16287 		mii->mii_readreg(sc->sc_dev, 1, IGPHY_POWER_MGMT, &phyval);
   16288 		phyval &= ~PMR_D0_LPLU;
   16289 		mii->mii_writereg(sc->sc_dev, 1, IGPHY_POWER_MGMT, phyval);
   16290 		break;
   16291 	case WM_T_82580:
   16292 	case WM_T_I350:
   16293 	case WM_T_I210:
   16294 	case WM_T_I211:
   16295 		reg = CSR_READ(sc, WMREG_PHPM);
   16296 		reg &= ~PHPM_D0A_LPLU;
   16297 		CSR_WRITE(sc, WMREG_PHPM, reg);
   16298 		break;
   16299 	case WM_T_82574:
   16300 	case WM_T_82583:
   16301 	case WM_T_ICH8:
   16302 	case WM_T_ICH9:
   16303 	case WM_T_ICH10:
   16304 		reg = CSR_READ(sc, WMREG_PHY_CTRL);
   16305 		reg &= ~(PHY_CTRL_GBE_DIS | PHY_CTRL_D0A_LPLU);
   16306 		CSR_WRITE(sc, WMREG_PHY_CTRL, reg);
   16307 		CSR_WRITE_FLUSH(sc);
   16308 		break;
   16309 	case WM_T_PCH:
   16310 	case WM_T_PCH2:
   16311 	case WM_T_PCH_LPT:
   16312 	case WM_T_PCH_SPT:
   16313 	case WM_T_PCH_CNP:
   16314 		wm_gmii_hv_readreg(sc->sc_dev, 1, HV_OEM_BITS, &phyval);
   16315 		phyval &= ~(HV_OEM_BITS_A1KDIS | HV_OEM_BITS_LPLU);
   16316 		if (wm_phy_resetisblocked(sc) == false)
   16317 			phyval |= HV_OEM_BITS_ANEGNOW;
   16318 		wm_gmii_hv_writereg(sc->sc_dev, 1, HV_OEM_BITS, phyval);
   16319 		break;
   16320 	default:
   16321 		break;
   16322 	}
   16323 }
   16324 
   16325 /* EEE */
   16326 
   16327 static int
   16328 wm_set_eee_i350(struct wm_softc *sc)
   16329 {
   16330 	struct ethercom *ec = &sc->sc_ethercom;
   16331 	uint32_t ipcnfg, eeer;
   16332 	uint32_t ipcnfg_mask
   16333 	    = IPCNFG_EEE_1G_AN | IPCNFG_EEE_100M_AN | IPCNFG_10BASE_TE;
   16334 	uint32_t eeer_mask = EEER_TX_LPI_EN | EEER_RX_LPI_EN | EEER_LPI_FC;
   16335 
   16336 	KASSERT(sc->sc_mediatype == WM_MEDIATYPE_COPPER);
   16337 
   16338 	ipcnfg = CSR_READ(sc, WMREG_IPCNFG);
   16339 	eeer = CSR_READ(sc, WMREG_EEER);
   16340 
   16341 	/* Enable or disable per user setting */
   16342 	if ((ec->ec_capenable & ETHERCAP_EEE) != 0) {
   16343 		ipcnfg |= ipcnfg_mask;
   16344 		eeer |= eeer_mask;
   16345 	} else {
   16346 		ipcnfg &= ~ipcnfg_mask;
   16347 		eeer &= ~eeer_mask;
   16348 	}
   16349 
   16350 	CSR_WRITE(sc, WMREG_IPCNFG, ipcnfg);
   16351 	CSR_WRITE(sc, WMREG_EEER, eeer);
   16352 	CSR_READ(sc, WMREG_IPCNFG); /* XXX flush? */
   16353 	CSR_READ(sc, WMREG_EEER); /* XXX flush? */
   16354 
   16355 	return 0;
   16356 }
   16357 
   16358 static int
   16359 wm_set_eee_pchlan(struct wm_softc *sc)
   16360 {
   16361 	device_t dev = sc->sc_dev;
   16362 	struct ethercom *ec = &sc->sc_ethercom;
   16363 	uint16_t lpa, pcs_status, adv_addr, adv, lpi_ctrl, data;
   16364 	int rv = 0;
   16365 
   16366 	switch (sc->sc_phytype) {
   16367 	case WMPHY_82579:
   16368 		lpa = I82579_EEE_LP_ABILITY;
   16369 		pcs_status = I82579_EEE_PCS_STATUS;
   16370 		adv_addr = I82579_EEE_ADVERTISEMENT;
   16371 		break;
   16372 	case WMPHY_I217:
   16373 		lpa = I217_EEE_LP_ABILITY;
   16374 		pcs_status = I217_EEE_PCS_STATUS;
   16375 		adv_addr = I217_EEE_ADVERTISEMENT;
   16376 		break;
   16377 	default:
   16378 		return 0;
   16379 	}
   16380 
   16381 	if (sc->phy.acquire(sc)) {
   16382 		device_printf(dev, "%s: failed to get semaphore\n", __func__);
   16383 		return 0;
   16384 	}
   16385 
   16386 	rv = sc->phy.readreg_locked(dev, 1, I82579_LPI_CTRL, &lpi_ctrl);
   16387 	if (rv != 0)
   16388 		goto release;
   16389 
   16390 	/* Clear bits that enable EEE in various speeds */
   16391 	lpi_ctrl &= ~I82579_LPI_CTRL_ENABLE;
   16392 
   16393 	if ((ec->ec_capenable & ETHERCAP_EEE) != 0) {
   16394 		/* Save off link partner's EEE ability */
   16395 		rv = wm_read_emi_reg_locked(dev, lpa, &sc->eee_lp_ability);
   16396 		if (rv != 0)
   16397 			goto release;
   16398 
   16399 		/* Read EEE advertisement */
   16400 		if ((rv = wm_read_emi_reg_locked(dev, adv_addr, &adv)) != 0)
   16401 			goto release;
   16402 
   16403 		/*
   16404 		 * Enable EEE only for speeds in which the link partner is
   16405 		 * EEE capable and for which we advertise EEE.
   16406 		 */
   16407 		if (adv & sc->eee_lp_ability & AN_EEEADVERT_1000_T)
   16408 			lpi_ctrl |= I82579_LPI_CTRL_EN_1000;
   16409 		if (adv & sc->eee_lp_ability & AN_EEEADVERT_100_TX) {
   16410 			sc->phy.readreg_locked(dev, 2, MII_ANLPAR, &data);
   16411 			if ((data & ANLPAR_TX_FD) != 0)
   16412 				lpi_ctrl |= I82579_LPI_CTRL_EN_100;
   16413 			else {
   16414 				/*
   16415 				 * EEE is not supported in 100Half, so ignore
   16416 				 * partner's EEE in 100 ability if full-duplex
   16417 				 * is not advertised.
   16418 				 */
   16419 				sc->eee_lp_ability
   16420 				    &= ~AN_EEEADVERT_100_TX;
   16421 			}
   16422 		}
   16423 	}
   16424 
   16425 	if (sc->sc_phytype == WMPHY_82579) {
   16426 		rv = wm_read_emi_reg_locked(dev, I82579_LPI_PLL_SHUT, &data);
   16427 		if (rv != 0)
   16428 			goto release;
   16429 
   16430 		data &= ~I82579_LPI_PLL_SHUT_100;
   16431 		rv = wm_write_emi_reg_locked(dev, I82579_LPI_PLL_SHUT, data);
   16432 	}
   16433 
   16434 	/* R/Clr IEEE MMD 3.1 bits 11:10 - Tx/Rx LPI Received */
   16435 	if ((rv = wm_read_emi_reg_locked(dev, pcs_status, &data)) != 0)
   16436 		goto release;
   16437 
   16438 	rv = sc->phy.writereg_locked(dev, 1, I82579_LPI_CTRL, lpi_ctrl);
   16439 release:
   16440 	sc->phy.release(sc);
   16441 
   16442 	return rv;
   16443 }
   16444 
   16445 static int
   16446 wm_set_eee(struct wm_softc *sc)
   16447 {
   16448 	struct ethercom *ec = &sc->sc_ethercom;
   16449 
   16450 	if ((ec->ec_capabilities & ETHERCAP_EEE) == 0)
   16451 		return 0;
   16452 
   16453 	if (sc->sc_type == WM_T_I354) {
   16454 		/* I354 uses an external PHY */
   16455 		return 0; /* not yet */
   16456 	} else if ((sc->sc_type >= WM_T_I350) && (sc->sc_type <= WM_T_I211))
   16457 		return wm_set_eee_i350(sc);
   16458 	else if (sc->sc_type >= WM_T_PCH2)
   16459 		return wm_set_eee_pchlan(sc);
   16460 
   16461 	return 0;
   16462 }
   16463 
   16464 /*
   16465  * Workarounds (mainly PHY related).
   16466  * Basically, PHY's workarounds are in the PHY drivers.
   16467  */
   16468 
   16469 /* Workaround for 82566 Kumeran PCS lock loss */
   16470 static int
   16471 wm_kmrn_lock_loss_workaround_ich8lan(struct wm_softc *sc)
   16472 {
   16473 	struct mii_data *mii = &sc->sc_mii;
   16474 	uint32_t status = CSR_READ(sc, WMREG_STATUS);
   16475 	int i, reg, rv;
   16476 	uint16_t phyreg;
   16477 
   16478 	DPRINTF(sc, WM_DEBUG_INIT, ("%s: %s called\n",
   16479 		device_xname(sc->sc_dev), __func__));
   16480 
   16481 	/* If the link is not up, do nothing */
   16482 	if ((status & STATUS_LU) == 0)
   16483 		return 0;
   16484 
   16485 	/* Nothing to do if the link is other than 1Gbps */
   16486 	if (__SHIFTOUT(status, STATUS_SPEED) != STATUS_SPEED_1000)
   16487 		return 0;
   16488 
   16489 	for (i = 0; i < 10; i++) {
   16490 		/* read twice */
   16491 		rv = mii->mii_readreg(sc->sc_dev, 1, IGP3_KMRN_DIAG, &phyreg);
   16492 		if (rv != 0)
   16493 			return rv;
   16494 		rv = mii->mii_readreg(sc->sc_dev, 1, IGP3_KMRN_DIAG, &phyreg);
   16495 		if (rv != 0)
   16496 			return rv;
   16497 
   16498 		if ((phyreg & IGP3_KMRN_DIAG_PCS_LOCK_LOSS) == 0)
   16499 			goto out;	/* GOOD! */
   16500 
   16501 		/* Reset the PHY */
   16502 		wm_reset_phy(sc);
   16503 		delay(5*1000);
   16504 	}
   16505 
   16506 	/* Disable GigE link negotiation */
   16507 	reg = CSR_READ(sc, WMREG_PHY_CTRL);
   16508 	reg |= PHY_CTRL_GBE_DIS | PHY_CTRL_NOND0A_GBE_DIS;
   16509 	CSR_WRITE(sc, WMREG_PHY_CTRL, reg);
   16510 
   16511 	/*
   16512 	 * Call gig speed drop workaround on Gig disable before accessing
   16513 	 * any PHY registers.
   16514 	 */
   16515 	wm_gig_downshift_workaround_ich8lan(sc);
   16516 
   16517 out:
   16518 	return 0;
   16519 }
   16520 
   16521 /*
   16522  *  wm_gig_downshift_workaround_ich8lan - WoL from S5 stops working
   16523  *  @sc: pointer to the HW structure
   16524  *
   16525  *  Steps to take when dropping from 1Gb/s (eg. link cable removal (LSC),
   16526  *  LPLU, Gig disable, MDIC PHY reset):
   16527  *    1) Set Kumeran Near-end loopback
   16528  *    2) Clear Kumeran Near-end loopback
   16529  *  Should only be called for ICH8[m] devices with any 1G Phy.
   16530  */
   16531 static void
   16532 wm_gig_downshift_workaround_ich8lan(struct wm_softc *sc)
   16533 {
   16534 	uint16_t kmreg;
   16535 
   16536 	/* Only for igp3 */
   16537 	if (sc->sc_phytype == WMPHY_IGP_3) {
   16538 		if (wm_kmrn_readreg(sc, KUMCTRLSTA_OFFSET_DIAG, &kmreg) != 0)
   16539 			return;
   16540 		kmreg |= KUMCTRLSTA_DIAG_NELPBK;
   16541 		if (wm_kmrn_writereg(sc, KUMCTRLSTA_OFFSET_DIAG, kmreg) != 0)
   16542 			return;
   16543 		kmreg &= ~KUMCTRLSTA_DIAG_NELPBK;
   16544 		wm_kmrn_writereg(sc, KUMCTRLSTA_OFFSET_DIAG, kmreg);
   16545 	}
   16546 }
   16547 
   16548 /*
   16549  * Workaround for pch's PHYs
   16550  * XXX should be moved to new PHY driver?
   16551  */
   16552 static int
   16553 wm_hv_phy_workarounds_ich8lan(struct wm_softc *sc)
   16554 {
   16555 	device_t dev = sc->sc_dev;
   16556 	struct mii_data *mii = &sc->sc_mii;
   16557 	struct mii_softc *child;
   16558 	uint16_t phy_data, phyrev = 0;
   16559 	int phytype = sc->sc_phytype;
   16560 	int rv;
   16561 
   16562 	DPRINTF(sc, WM_DEBUG_INIT, ("%s: %s called\n",
   16563 		device_xname(dev), __func__));
   16564 	KASSERT(sc->sc_type == WM_T_PCH);
   16565 
   16566 	/* Set MDIO slow mode before any other MDIO access */
   16567 	if (phytype == WMPHY_82577)
   16568 		if ((rv = wm_set_mdio_slow_mode_hv(sc)) != 0)
   16569 			return rv;
   16570 
   16571 	child = LIST_FIRST(&mii->mii_phys);
   16572 	if (child != NULL)
   16573 		phyrev = child->mii_mpd_rev;
   16574 
   16575 	/* (82577 && (phy rev 1 or 2)) || (82578 & phy rev 1)*/
   16576 	if ((child != NULL) &&
   16577 	    (((phytype == WMPHY_82577) && ((phyrev == 1) || (phyrev == 2))) ||
   16578 		((phytype == WMPHY_82578) && (phyrev == 1)))) {
   16579 		/* Disable generation of early preamble (0x4431) */
   16580 		rv = mii->mii_readreg(dev, 2, BM_RATE_ADAPTATION_CTRL,
   16581 		    &phy_data);
   16582 		if (rv != 0)
   16583 			return rv;
   16584 		phy_data &= ~(BM_RATE_ADAPTATION_CTRL_RX_RXDV_PRE |
   16585 		    BM_RATE_ADAPTATION_CTRL_RX_CRS_PRE);
   16586 		rv = mii->mii_writereg(dev, 2, BM_RATE_ADAPTATION_CTRL,
   16587 		    phy_data);
   16588 		if (rv != 0)
   16589 			return rv;
   16590 
   16591 		/* Preamble tuning for SSC */
   16592 		rv = mii->mii_writereg(dev, 2, HV_KMRN_FIFO_CTRLSTA, 0xa204);
   16593 		if (rv != 0)
   16594 			return rv;
   16595 	}
   16596 
   16597 	/* 82578 */
   16598 	if (phytype == WMPHY_82578) {
   16599 		/*
   16600 		 * Return registers to default by doing a soft reset then
   16601 		 * writing 0x3140 to the control register
   16602 		 * 0x3140 == BMCR_SPEED0 | BMCR_AUTOEN | BMCR_FDX | BMCR_SPEED1
   16603 		 */
   16604 		if ((child != NULL) && (phyrev < 2)) {
   16605 			PHY_RESET(child);
   16606 			rv = mii->mii_writereg(dev, 2, MII_BMCR, 0x3140);
   16607 			if (rv != 0)
   16608 				return rv;
   16609 		}
   16610 	}
   16611 
   16612 	/* Select page 0 */
   16613 	if ((rv = sc->phy.acquire(sc)) != 0)
   16614 		return rv;
   16615 	rv = wm_gmii_mdic_writereg(dev, 1, IGPHY_PAGE_SELECT, 0);
   16616 	sc->phy.release(sc);
   16617 	if (rv != 0)
   16618 		return rv;
   16619 
   16620 	/*
   16621 	 * Configure the K1 Si workaround during phy reset assuming there is
   16622 	 * link so that it disables K1 if link is in 1Gbps.
   16623 	 */
   16624 	if ((rv = wm_k1_gig_workaround_hv(sc, 1)) != 0)
   16625 		return rv;
   16626 
   16627 	/* Workaround for link disconnects on a busy hub in half duplex */
   16628 	rv = sc->phy.acquire(sc);
   16629 	if (rv)
   16630 		return rv;
   16631 	rv = sc->phy.readreg_locked(dev, 2, BM_PORT_GEN_CFG, &phy_data);
   16632 	if (rv)
   16633 		goto release;
   16634 	rv = sc->phy.writereg_locked(dev, 2, BM_PORT_GEN_CFG,
   16635 	    phy_data & 0x00ff);
   16636 	if (rv)
   16637 		goto release;
   16638 
   16639 	/* Set MSE higher to enable link to stay up when noise is high */
   16640 	rv = wm_write_emi_reg_locked(dev, I82577_MSE_THRESHOLD, 0x0034);
   16641 release:
   16642 	sc->phy.release(sc);
   16643 
   16644 	return rv;
   16645 }
   16646 
   16647 /*
   16648  *  wm_copy_rx_addrs_to_phy_ich8lan - Copy Rx addresses from MAC to PHY
   16649  *  @sc:   pointer to the HW structure
   16650  */
   16651 static void
   16652 wm_copy_rx_addrs_to_phy_ich8lan(struct wm_softc *sc)
   16653 {
   16654 
   16655 	DPRINTF(sc, WM_DEBUG_INIT, ("%s: %s called\n",
   16656 		device_xname(sc->sc_dev), __func__));
   16657 
   16658 	if (sc->phy.acquire(sc) != 0)
   16659 		return;
   16660 
   16661 	wm_copy_rx_addrs_to_phy_ich8lan_locked(sc);
   16662 
   16663 	sc->phy.release(sc);
   16664 }
   16665 
   16666 static void
   16667 wm_copy_rx_addrs_to_phy_ich8lan_locked(struct wm_softc *sc)
   16668 {
   16669 	device_t dev = sc->sc_dev;
   16670 	uint32_t mac_reg;
   16671 	uint16_t i, wuce;
   16672 	int count;
   16673 
   16674 	DPRINTF(sc, WM_DEBUG_INIT, ("%s: %s called\n",
   16675 		device_xname(dev), __func__));
   16676 
   16677 	if (wm_enable_phy_wakeup_reg_access_bm(dev, &wuce) != 0)
   16678 		return;
   16679 
   16680 	/* Copy both RAL/H (rar_entry_count) and SHRAL/H to PHY */
   16681 	count = wm_rar_count(sc);
   16682 	for (i = 0; i < count; i++) {
   16683 		uint16_t lo, hi;
   16684 		mac_reg = CSR_READ(sc, WMREG_CORDOVA_RAL(i));
   16685 		lo = (uint16_t)(mac_reg & 0xffff);
   16686 		hi = (uint16_t)((mac_reg >> 16) & 0xffff);
   16687 		wm_access_phy_wakeup_reg_bm(dev, BM_RAR_L(i), &lo, 0, true);
   16688 		wm_access_phy_wakeup_reg_bm(dev, BM_RAR_M(i), &hi, 0, true);
   16689 
   16690 		mac_reg = CSR_READ(sc, WMREG_CORDOVA_RAH(i));
   16691 		lo = (uint16_t)(mac_reg & 0xffff);
   16692 		hi = (uint16_t)((mac_reg & RAL_AV) >> 16);
   16693 		wm_access_phy_wakeup_reg_bm(dev, BM_RAR_H(i), &lo, 0, true);
   16694 		wm_access_phy_wakeup_reg_bm(dev, BM_RAR_CTRL(i), &hi, 0, true);
   16695 	}
   16696 
   16697 	wm_disable_phy_wakeup_reg_access_bm(dev, &wuce);
   16698 }
   16699 
   16700 /*
   16701  *  wm_lv_jumbo_workaround_ich8lan - required for jumbo frame operation
   16702  *  with 82579 PHY
   16703  *  @enable: flag to enable/disable workaround when enabling/disabling jumbos
   16704  */
   16705 static int
   16706 wm_lv_jumbo_workaround_ich8lan(struct wm_softc *sc, bool enable)
   16707 {
   16708 	device_t dev = sc->sc_dev;
   16709 	int rar_count;
   16710 	int rv;
   16711 	uint32_t mac_reg;
   16712 	uint16_t dft_ctrl, data;
   16713 	uint16_t i;
   16714 
   16715 	DPRINTF(sc, WM_DEBUG_INIT, ("%s: %s called\n",
   16716 		device_xname(dev), __func__));
   16717 
   16718 	if (sc->sc_type < WM_T_PCH2)
   16719 		return 0;
   16720 
   16721 	/* Acquire PHY semaphore */
   16722 	rv = sc->phy.acquire(sc);
   16723 	if (rv != 0)
   16724 		return rv;
   16725 
   16726 	/* Disable Rx path while enabling/disabling workaround */
   16727 	rv = sc->phy.readreg_locked(dev, 2, I82579_DFT_CTRL, &dft_ctrl);
   16728 	if (rv != 0)
   16729 		goto out;
   16730 	rv = sc->phy.writereg_locked(dev, 2, I82579_DFT_CTRL,
   16731 	    dft_ctrl | (1 << 14));
   16732 	if (rv != 0)
   16733 		goto out;
   16734 
   16735 	if (enable) {
   16736 		/* Write Rx addresses (rar_entry_count for RAL/H, and
   16737 		 * SHRAL/H) and initial CRC values to the MAC
   16738 		 */
   16739 		rar_count = wm_rar_count(sc);
   16740 		for (i = 0; i < rar_count; i++) {
   16741 			uint8_t mac_addr[ETHER_ADDR_LEN] = {0};
   16742 			uint32_t addr_high, addr_low;
   16743 
   16744 			addr_high = CSR_READ(sc, WMREG_CORDOVA_RAH(i));
   16745 			if (!(addr_high & RAL_AV))
   16746 				continue;
   16747 			addr_low = CSR_READ(sc, WMREG_CORDOVA_RAL(i));
   16748 			mac_addr[0] = (addr_low & 0xFF);
   16749 			mac_addr[1] = ((addr_low >> 8) & 0xFF);
   16750 			mac_addr[2] = ((addr_low >> 16) & 0xFF);
   16751 			mac_addr[3] = ((addr_low >> 24) & 0xFF);
   16752 			mac_addr[4] = (addr_high & 0xFF);
   16753 			mac_addr[5] = ((addr_high >> 8) & 0xFF);
   16754 
   16755 			CSR_WRITE(sc, WMREG_PCH_RAICC(i),
   16756 			    ~ether_crc32_le(mac_addr, ETHER_ADDR_LEN));
   16757 		}
   16758 
   16759 		/* Write Rx addresses to the PHY */
   16760 		wm_copy_rx_addrs_to_phy_ich8lan_locked(sc);
   16761 	}
   16762 
   16763 	/*
   16764 	 * If enable ==
   16765 	 *	true: Enable jumbo frame workaround in the MAC.
   16766 	 *	false: Write MAC register values back to h/w defaults.
   16767 	 */
   16768 	mac_reg = CSR_READ(sc, WMREG_FFLT_DBG);
   16769 	if (enable) {
   16770 		mac_reg &= ~(1 << 14);
   16771 		mac_reg |= (7 << 15);
   16772 	} else
   16773 		mac_reg &= ~(0xf << 14);
   16774 	CSR_WRITE(sc, WMREG_FFLT_DBG, mac_reg);
   16775 
   16776 	mac_reg = CSR_READ(sc, WMREG_RCTL);
   16777 	if (enable) {
   16778 		mac_reg |= RCTL_SECRC;
   16779 		sc->sc_rctl |= RCTL_SECRC;
   16780 		sc->sc_flags |= WM_F_CRC_STRIP;
   16781 	} else {
   16782 		mac_reg &= ~RCTL_SECRC;
   16783 		sc->sc_rctl &= ~RCTL_SECRC;
   16784 		sc->sc_flags &= ~WM_F_CRC_STRIP;
   16785 	}
   16786 	CSR_WRITE(sc, WMREG_RCTL, mac_reg);
   16787 
   16788 	rv = wm_kmrn_readreg_locked(sc, KUMCTRLSTA_OFFSET_CTRL, &data);
   16789 	if (rv != 0)
   16790 		goto out;
   16791 	if (enable)
   16792 		data |= 1 << 0;
   16793 	else
   16794 		data &= ~(1 << 0);
   16795 	rv = wm_kmrn_writereg_locked(sc, KUMCTRLSTA_OFFSET_CTRL, data);
   16796 	if (rv != 0)
   16797 		goto out;
   16798 
   16799 	rv = wm_kmrn_readreg_locked(sc, KUMCTRLSTA_OFFSET_HD_CTRL, &data);
   16800 	if (rv != 0)
   16801 		goto out;
   16802 	/*
   16803 	 * XXX FreeBSD and Linux do the same thing that they set the same value
   16804 	 * on both the enable case and the disable case. Is it correct?
   16805 	 */
   16806 	data &= ~(0xf << 8);
   16807 	data |= (0xb << 8);
   16808 	rv = wm_kmrn_writereg_locked(sc, KUMCTRLSTA_OFFSET_HD_CTRL, data);
   16809 	if (rv != 0)
   16810 		goto out;
   16811 
   16812 	/*
   16813 	 * If enable ==
   16814 	 *	true: Enable jumbo frame workaround in the PHY.
   16815 	 *	false: Write PHY register values back to h/w defaults.
   16816 	 */
   16817 	rv = sc->phy.readreg_locked(dev, 2, BME1000_REG(769, 23), &data);
   16818 	if (rv != 0)
   16819 		goto out;
   16820 	data &= ~(0x7F << 5);
   16821 	if (enable)
   16822 		data |= (0x37 << 5);
   16823 	rv = sc->phy.writereg_locked(dev, 2, BME1000_REG(769, 23), data);
   16824 	if (rv != 0)
   16825 		goto out;
   16826 
   16827 	rv = sc->phy.readreg_locked(dev, 2, BME1000_REG(769, 16), &data);
   16828 	if (rv != 0)
   16829 		goto out;
   16830 	if (enable)
   16831 		data &= ~(1 << 13);
   16832 	else
   16833 		data |= (1 << 13);
   16834 	rv = sc->phy.writereg_locked(dev, 2, BME1000_REG(769, 16), data);
   16835 	if (rv != 0)
   16836 		goto out;
   16837 
   16838 	rv = sc->phy.readreg_locked(dev, 2, I82579_UNKNOWN1, &data);
   16839 	if (rv != 0)
   16840 		goto out;
   16841 	data &= ~(0x3FF << 2);
   16842 	if (enable)
   16843 		data |= (I82579_TX_PTR_GAP << 2);
   16844 	else
   16845 		data |= (0x8 << 2);
   16846 	rv = sc->phy.writereg_locked(dev, 2, I82579_UNKNOWN1, data);
   16847 	if (rv != 0)
   16848 		goto out;
   16849 
   16850 	rv = sc->phy.writereg_locked(dev, 2, BME1000_REG(776, 23),
   16851 	    enable ? 0xf100 : 0x7e00);
   16852 	if (rv != 0)
   16853 		goto out;
   16854 
   16855 	rv = sc->phy.readreg_locked(dev, 2, HV_PM_CTRL, &data);
   16856 	if (rv != 0)
   16857 		goto out;
   16858 	if (enable)
   16859 		data |= 1 << 10;
   16860 	else
   16861 		data &= ~(1 << 10);
   16862 	rv = sc->phy.writereg_locked(dev, 2, HV_PM_CTRL, data);
   16863 	if (rv != 0)
   16864 		goto out;
   16865 
   16866 	/* Re-enable Rx path after enabling/disabling workaround */
   16867 	rv = sc->phy.writereg_locked(dev, 2, I82579_DFT_CTRL,
   16868 	    dft_ctrl & ~(1 << 14));
   16869 
   16870 out:
   16871 	sc->phy.release(sc);
   16872 
   16873 	return rv;
   16874 }
   16875 
   16876 /*
   16877  *  wm_lv_phy_workarounds_ich8lan - A series of Phy workarounds to be
   16878  *  done after every PHY reset.
   16879  */
   16880 static int
   16881 wm_lv_phy_workarounds_ich8lan(struct wm_softc *sc)
   16882 {
   16883 	device_t dev = sc->sc_dev;
   16884 	int rv;
   16885 
   16886 	DPRINTF(sc, WM_DEBUG_INIT, ("%s: %s called\n",
   16887 		device_xname(dev), __func__));
   16888 	KASSERT(sc->sc_type == WM_T_PCH2);
   16889 
   16890 	/* Set MDIO slow mode before any other MDIO access */
   16891 	rv = wm_set_mdio_slow_mode_hv(sc);
   16892 	if (rv != 0)
   16893 		return rv;
   16894 
   16895 	rv = sc->phy.acquire(sc);
   16896 	if (rv != 0)
   16897 		return rv;
   16898 	/* Set MSE higher to enable link to stay up when noise is high */
   16899 	rv = wm_write_emi_reg_locked(dev, I82579_MSE_THRESHOLD, 0x0034);
   16900 	if (rv != 0)
   16901 		goto release;
   16902 	/* Drop link after 5 times MSE threshold was reached */
   16903 	rv = wm_write_emi_reg_locked(dev, I82579_MSE_LINK_DOWN, 0x0005);
   16904 release:
   16905 	sc->phy.release(sc);
   16906 
   16907 	return rv;
   16908 }
   16909 
   16910 /**
   16911  *  wm_k1_workaround_lpt_lp - K1 workaround on Lynxpoint-LP
   16912  *  @link: link up bool flag
   16913  *
   16914  *  When K1 is enabled for 1Gbps, the MAC can miss 2 DMA completion indications
   16915  *  preventing further DMA write requests.  Workaround the issue by disabling
   16916  *  the de-assertion of the clock request when in 1Gpbs mode.
   16917  *  Also, set appropriate Tx re-transmission timeouts for 10 and 100Half link
   16918  *  speeds in order to avoid Tx hangs.
   16919  **/
   16920 static int
   16921 wm_k1_workaround_lpt_lp(struct wm_softc *sc, bool link)
   16922 {
   16923 	uint32_t fextnvm6 = CSR_READ(sc, WMREG_FEXTNVM6);
   16924 	uint32_t status = CSR_READ(sc, WMREG_STATUS);
   16925 	uint32_t speed = __SHIFTOUT(status, STATUS_SPEED);
   16926 	uint16_t phyreg;
   16927 
   16928 	if (link && (speed == STATUS_SPEED_1000)) {
   16929 		sc->phy.acquire(sc);
   16930 		int rv = wm_kmrn_readreg_locked(sc,
   16931 		    KUMCTRLSTA_OFFSET_K1_CONFIG, &phyreg);
   16932 		if (rv != 0)
   16933 			goto release;
   16934 		rv = wm_kmrn_writereg_locked(sc, KUMCTRLSTA_OFFSET_K1_CONFIG,
   16935 		    phyreg & ~KUMCTRLSTA_K1_ENABLE);
   16936 		if (rv != 0)
   16937 			goto release;
   16938 		delay(20);
   16939 		CSR_WRITE(sc, WMREG_FEXTNVM6, fextnvm6 | FEXTNVM6_REQ_PLL_CLK);
   16940 
   16941 		rv = wm_kmrn_readreg_locked(sc, KUMCTRLSTA_OFFSET_K1_CONFIG,
   16942 		    &phyreg);
   16943 release:
   16944 		sc->phy.release(sc);
   16945 		return rv;
   16946 	}
   16947 
   16948 	fextnvm6 &= ~FEXTNVM6_REQ_PLL_CLK;
   16949 
   16950 	struct mii_softc *child = LIST_FIRST(&sc->sc_mii.mii_phys);
   16951 	if (((child != NULL) && (child->mii_mpd_rev > 5))
   16952 	    || !link
   16953 	    || ((speed == STATUS_SPEED_100) && (status & STATUS_FD)))
   16954 		goto update_fextnvm6;
   16955 
   16956 	wm_gmii_hv_readreg(sc->sc_dev, 2, I217_INBAND_CTRL, &phyreg);
   16957 
   16958 	/* Clear link status transmit timeout */
   16959 	phyreg &= ~I217_INBAND_CTRL_LINK_STAT_TX_TIMEOUT_MASK;
   16960 	if (speed == STATUS_SPEED_100) {
   16961 		/* Set inband Tx timeout to 5x10us for 100Half */
   16962 		phyreg |= 5 << I217_INBAND_CTRL_LINK_STAT_TX_TIMEOUT_SHIFT;
   16963 
   16964 		/* Do not extend the K1 entry latency for 100Half */
   16965 		fextnvm6 &= ~FEXTNVM6_ENABLE_K1_ENTRY_CONDITION;
   16966 	} else {
   16967 		/* Set inband Tx timeout to 50x10us for 10Full/Half */
   16968 		phyreg |= 50 << I217_INBAND_CTRL_LINK_STAT_TX_TIMEOUT_SHIFT;
   16969 
   16970 		/* Extend the K1 entry latency for 10 Mbps */
   16971 		fextnvm6 |= FEXTNVM6_ENABLE_K1_ENTRY_CONDITION;
   16972 	}
   16973 
   16974 	wm_gmii_hv_writereg(sc->sc_dev, 2, I217_INBAND_CTRL, phyreg);
   16975 
   16976 update_fextnvm6:
   16977 	CSR_WRITE(sc, WMREG_FEXTNVM6, fextnvm6);
   16978 	return 0;
   16979 }
   16980 
   16981 /*
   16982  *  wm_k1_gig_workaround_hv - K1 Si workaround
   16983  *  @sc:   pointer to the HW structure
   16984  *  @link: link up bool flag
   16985  *
   16986  *  If K1 is enabled for 1Gbps, the MAC might stall when transitioning
   16987  *  from a lower speed.  This workaround disables K1 whenever link is at 1Gig
   16988  *  If link is down, the function will restore the default K1 setting located
   16989  *  in the NVM.
   16990  */
   16991 static int
   16992 wm_k1_gig_workaround_hv(struct wm_softc *sc, int link)
   16993 {
   16994 	int k1_enable = sc->sc_nvm_k1_enabled;
   16995 
   16996 	DPRINTF(sc, WM_DEBUG_INIT, ("%s: %s called\n",
   16997 		device_xname(sc->sc_dev), __func__));
   16998 
   16999 	if (sc->phy.acquire(sc) != 0)
   17000 		return -1;
   17001 
   17002 	if (link) {
   17003 		k1_enable = 0;
   17004 
   17005 		/* Link stall fix for link up */
   17006 		wm_gmii_hv_writereg_locked(sc->sc_dev, 1, IGP3_KMRN_DIAG,
   17007 		    0x0100);
   17008 	} else {
   17009 		/* Link stall fix for link down */
   17010 		wm_gmii_hv_writereg_locked(sc->sc_dev, 1, IGP3_KMRN_DIAG,
   17011 		    0x4100);
   17012 	}
   17013 
   17014 	wm_configure_k1_ich8lan(sc, k1_enable);
   17015 	sc->phy.release(sc);
   17016 
   17017 	return 0;
   17018 }
   17019 
   17020 /*
   17021  *  wm_k1_workaround_lv - K1 Si workaround
   17022  *  @sc:   pointer to the HW structure
   17023  *
   17024  *  Workaround to set the K1 beacon duration for 82579 parts in 10Mbps
   17025  *  Disable K1 for 1000 and 100 speeds
   17026  */
   17027 static int
   17028 wm_k1_workaround_lv(struct wm_softc *sc)
   17029 {
   17030 	uint32_t reg;
   17031 	uint16_t phyreg;
   17032 	int rv;
   17033 
   17034 	if (sc->sc_type != WM_T_PCH2)
   17035 		return 0;
   17036 
   17037 	/* Set K1 beacon duration based on 10Mbps speed */
   17038 	rv = wm_gmii_hv_readreg(sc->sc_dev, 2, HV_M_STATUS, &phyreg);
   17039 	if (rv != 0)
   17040 		return rv;
   17041 
   17042 	if ((phyreg & (HV_M_STATUS_LINK_UP | HV_M_STATUS_AUTONEG_COMPLETE))
   17043 	    == (HV_M_STATUS_LINK_UP | HV_M_STATUS_AUTONEG_COMPLETE)) {
   17044 		if (phyreg &
   17045 		    (HV_M_STATUS_SPEED_1000 | HV_M_STATUS_SPEED_100)) {
   17046 			/* LV 1G/100 Packet drop issue wa  */
   17047 			rv = wm_gmii_hv_readreg(sc->sc_dev, 1, HV_PM_CTRL,
   17048 			    &phyreg);
   17049 			if (rv != 0)
   17050 				return rv;
   17051 			phyreg &= ~HV_PM_CTRL_K1_ENA;
   17052 			rv = wm_gmii_hv_writereg(sc->sc_dev, 1, HV_PM_CTRL,
   17053 			    phyreg);
   17054 			if (rv != 0)
   17055 				return rv;
   17056 		} else {
   17057 			/* For 10Mbps */
   17058 			reg = CSR_READ(sc, WMREG_FEXTNVM4);
   17059 			reg &= ~FEXTNVM4_BEACON_DURATION;
   17060 			reg |= FEXTNVM4_BEACON_DURATION_16US;
   17061 			CSR_WRITE(sc, WMREG_FEXTNVM4, reg);
   17062 		}
   17063 	}
   17064 
   17065 	return 0;
   17066 }
   17067 
   17068 /*
   17069  *  wm_link_stall_workaround_hv - Si workaround
   17070  *  @sc: pointer to the HW structure
   17071  *
   17072  *  This function works around a Si bug where the link partner can get
   17073  *  a link up indication before the PHY does. If small packets are sent
   17074  *  by the link partner they can be placed in the packet buffer without
   17075  *  being properly accounted for by the PHY and will stall preventing
   17076  *  further packets from being received.  The workaround is to clear the
   17077  *  packet buffer after the PHY detects link up.
   17078  */
   17079 static int
   17080 wm_link_stall_workaround_hv(struct wm_softc *sc)
   17081 {
   17082 	uint16_t phyreg;
   17083 
   17084 	if (sc->sc_phytype != WMPHY_82578)
   17085 		return 0;
   17086 
   17087 	/* Do not apply workaround if in PHY loopback bit 14 set */
   17088 	wm_gmii_hv_readreg(sc->sc_dev, 2, MII_BMCR, &phyreg);
   17089 	if ((phyreg & BMCR_LOOP) != 0)
   17090 		return 0;
   17091 
   17092 	/* Check if link is up and at 1Gbps */
   17093 	wm_gmii_hv_readreg(sc->sc_dev, 2, BM_CS_STATUS, &phyreg);
   17094 	phyreg &= BM_CS_STATUS_LINK_UP | BM_CS_STATUS_RESOLVED
   17095 	    | BM_CS_STATUS_SPEED_MASK;
   17096 	if (phyreg != (BM_CS_STATUS_LINK_UP | BM_CS_STATUS_RESOLVED
   17097 		| BM_CS_STATUS_SPEED_1000))
   17098 		return 0;
   17099 
   17100 	delay(200 * 1000);	/* XXX too big */
   17101 
   17102 	/* Flush the packets in the fifo buffer */
   17103 	wm_gmii_hv_writereg(sc->sc_dev, 1, HV_MUX_DATA_CTRL,
   17104 	    HV_MUX_DATA_CTRL_GEN_TO_MAC | HV_MUX_DATA_CTRL_FORCE_SPEED);
   17105 	wm_gmii_hv_writereg(sc->sc_dev, 1, HV_MUX_DATA_CTRL,
   17106 	    HV_MUX_DATA_CTRL_GEN_TO_MAC);
   17107 
   17108 	return 0;
   17109 }
   17110 
   17111 static int
   17112 wm_set_mdio_slow_mode_hv(struct wm_softc *sc)
   17113 {
   17114 	int rv;
   17115 	uint16_t reg;
   17116 
   17117 	rv = wm_gmii_hv_readreg(sc->sc_dev, 1, HV_KMRN_MODE_CTRL, &reg);
   17118 	if (rv != 0)
   17119 		return rv;
   17120 
   17121 	return wm_gmii_hv_writereg(sc->sc_dev, 1, HV_KMRN_MODE_CTRL,
   17122 	    reg | HV_KMRN_MDIO_SLOW);
   17123 }
   17124 
   17125 /*
   17126  *  wm_configure_k1_ich8lan - Configure K1 power state
   17127  *  @sc: pointer to the HW structure
   17128  *  @enable: K1 state to configure
   17129  *
   17130  *  Configure the K1 power state based on the provided parameter.
   17131  *  Assumes semaphore already acquired.
   17132  */
   17133 static void
   17134 wm_configure_k1_ich8lan(struct wm_softc *sc, int k1_enable)
   17135 {
   17136 	uint32_t ctrl, ctrl_ext, tmp;
   17137 	uint16_t kmreg;
   17138 	int rv;
   17139 
   17140 	KASSERT(CSR_READ(sc, WMREG_EXTCNFCTR) & EXTCNFCTR_MDIO_SW_OWNERSHIP);
   17141 
   17142 	rv = wm_kmrn_readreg_locked(sc, KUMCTRLSTA_OFFSET_K1_CONFIG, &kmreg);
   17143 	if (rv != 0)
   17144 		return;
   17145 
   17146 	if (k1_enable)
   17147 		kmreg |= KUMCTRLSTA_K1_ENABLE;
   17148 	else
   17149 		kmreg &= ~KUMCTRLSTA_K1_ENABLE;
   17150 
   17151 	rv = wm_kmrn_writereg_locked(sc, KUMCTRLSTA_OFFSET_K1_CONFIG, kmreg);
   17152 	if (rv != 0)
   17153 		return;
   17154 
   17155 	delay(20);
   17156 
   17157 	ctrl = CSR_READ(sc, WMREG_CTRL);
   17158 	ctrl_ext = CSR_READ(sc, WMREG_CTRL_EXT);
   17159 
   17160 	tmp = ctrl & ~(CTRL_SPEED_1000 | CTRL_SPEED_100);
   17161 	tmp |= CTRL_FRCSPD;
   17162 
   17163 	CSR_WRITE(sc, WMREG_CTRL, tmp);
   17164 	CSR_WRITE(sc, WMREG_CTRL_EXT, ctrl_ext | CTRL_EXT_SPD_BYPS);
   17165 	CSR_WRITE_FLUSH(sc);
   17166 	delay(20);
   17167 
   17168 	CSR_WRITE(sc, WMREG_CTRL, ctrl);
   17169 	CSR_WRITE(sc, WMREG_CTRL_EXT, ctrl_ext);
   17170 	CSR_WRITE_FLUSH(sc);
   17171 	delay(20);
   17172 
   17173 	return;
   17174 }
   17175 
   17176 /* special case - for 82575 - need to do manual init ... */
   17177 static void
   17178 wm_reset_init_script_82575(struct wm_softc *sc)
   17179 {
   17180 	/*
   17181 	 * Remark: this is untested code - we have no board without EEPROM
   17182 	 *  same setup as mentioned int the FreeBSD driver for the i82575
   17183 	 */
   17184 
   17185 	/* SerDes configuration via SERDESCTRL */
   17186 	wm_82575_write_8bit_ctlr_reg(sc, WMREG_SCTL, 0x00, 0x0c);
   17187 	wm_82575_write_8bit_ctlr_reg(sc, WMREG_SCTL, 0x01, 0x78);
   17188 	wm_82575_write_8bit_ctlr_reg(sc, WMREG_SCTL, 0x1b, 0x23);
   17189 	wm_82575_write_8bit_ctlr_reg(sc, WMREG_SCTL, 0x23, 0x15);
   17190 
   17191 	/* CCM configuration via CCMCTL register */
   17192 	wm_82575_write_8bit_ctlr_reg(sc, WMREG_CCMCTL, 0x14, 0x00);
   17193 	wm_82575_write_8bit_ctlr_reg(sc, WMREG_CCMCTL, 0x10, 0x00);
   17194 
   17195 	/* PCIe lanes configuration */
   17196 	wm_82575_write_8bit_ctlr_reg(sc, WMREG_GIOCTL, 0x00, 0xec);
   17197 	wm_82575_write_8bit_ctlr_reg(sc, WMREG_GIOCTL, 0x61, 0xdf);
   17198 	wm_82575_write_8bit_ctlr_reg(sc, WMREG_GIOCTL, 0x34, 0x05);
   17199 	wm_82575_write_8bit_ctlr_reg(sc, WMREG_GIOCTL, 0x2f, 0x81);
   17200 
   17201 	/* PCIe PLL Configuration */
   17202 	wm_82575_write_8bit_ctlr_reg(sc, WMREG_SCCTL, 0x02, 0x47);
   17203 	wm_82575_write_8bit_ctlr_reg(sc, WMREG_SCCTL, 0x14, 0x00);
   17204 	wm_82575_write_8bit_ctlr_reg(sc, WMREG_SCCTL, 0x10, 0x00);
   17205 }
   17206 
   17207 static void
   17208 wm_reset_mdicnfg_82580(struct wm_softc *sc)
   17209 {
   17210 	uint32_t reg;
   17211 	uint16_t nvmword;
   17212 	int rv;
   17213 
   17214 	if (sc->sc_type != WM_T_82580)
   17215 		return;
   17216 	if ((sc->sc_flags & WM_F_SGMII) == 0)
   17217 		return;
   17218 
   17219 	rv = wm_nvm_read(sc, NVM_OFF_LAN_FUNC_82580(sc->sc_funcid)
   17220 	    + NVM_OFF_CFG3_PORTA, 1, &nvmword);
   17221 	if (rv != 0) {
   17222 		aprint_error_dev(sc->sc_dev, "%s: failed to read NVM\n",
   17223 		    __func__);
   17224 		return;
   17225 	}
   17226 
   17227 	reg = CSR_READ(sc, WMREG_MDICNFG);
   17228 	if (nvmword & NVM_CFG3_PORTA_EXT_MDIO)
   17229 		reg |= MDICNFG_DEST;
   17230 	if (nvmword & NVM_CFG3_PORTA_COM_MDIO)
   17231 		reg |= MDICNFG_COM_MDIO;
   17232 	CSR_WRITE(sc, WMREG_MDICNFG, reg);
   17233 }
   17234 
   17235 #define MII_INVALIDID(x)	(((x) == 0x0000) || ((x) == 0xffff))
   17236 
   17237 static bool
   17238 wm_phy_is_accessible_pchlan(struct wm_softc *sc)
   17239 {
   17240 	uint32_t reg;
   17241 	uint16_t id1, id2;
   17242 	int i, rv;
   17243 
   17244 	DPRINTF(sc, WM_DEBUG_INIT, ("%s: %s called\n",
   17245 		device_xname(sc->sc_dev), __func__));
   17246 	KASSERT(CSR_READ(sc, WMREG_EXTCNFCTR) & EXTCNFCTR_MDIO_SW_OWNERSHIP);
   17247 
   17248 	id1 = id2 = 0xffff;
   17249 	for (i = 0; i < 2; i++) {
   17250 		rv = wm_gmii_hv_readreg_locked(sc->sc_dev, 2, MII_PHYIDR1,
   17251 		    &id1);
   17252 		if ((rv != 0) || MII_INVALIDID(id1))
   17253 			continue;
   17254 		rv = wm_gmii_hv_readreg_locked(sc->sc_dev, 2, MII_PHYIDR2,
   17255 		    &id2);
   17256 		if ((rv != 0) || MII_INVALIDID(id2))
   17257 			continue;
   17258 		break;
   17259 	}
   17260 	if ((rv == 0) && !MII_INVALIDID(id1) && !MII_INVALIDID(id2))
   17261 		goto out;
   17262 
   17263 	/*
   17264 	 * In case the PHY needs to be in mdio slow mode,
   17265 	 * set slow mode and try to get the PHY id again.
   17266 	 */
   17267 	rv = 0;
   17268 	if (sc->sc_type < WM_T_PCH_LPT) {
   17269 		sc->phy.release(sc);
   17270 		wm_set_mdio_slow_mode_hv(sc);
   17271 		rv = wm_gmii_hv_readreg(sc->sc_dev, 2, MII_PHYIDR1, &id1);
   17272 		rv |= wm_gmii_hv_readreg(sc->sc_dev, 2, MII_PHYIDR2, &id2);
   17273 		sc->phy.acquire(sc);
   17274 	}
   17275 	if ((rv != 0) || MII_INVALIDID(id1) || MII_INVALIDID(id2)) {
   17276 		device_printf(sc->sc_dev, "XXX return with false\n");
   17277 		return false;
   17278 	}
   17279 out:
   17280 	if (sc->sc_type >= WM_T_PCH_LPT) {
   17281 		/* Only unforce SMBus if ME is not active */
   17282 		if ((CSR_READ(sc, WMREG_FWSM) & FWSM_FW_VALID) == 0) {
   17283 			uint16_t phyreg;
   17284 
   17285 			/* Unforce SMBus mode in PHY */
   17286 			rv = wm_gmii_hv_readreg_locked(sc->sc_dev, 2,
   17287 			    CV_SMB_CTRL, &phyreg);
   17288 			phyreg &= ~CV_SMB_CTRL_FORCE_SMBUS;
   17289 			wm_gmii_hv_writereg_locked(sc->sc_dev, 2,
   17290 			    CV_SMB_CTRL, phyreg);
   17291 
   17292 			/* Unforce SMBus mode in MAC */
   17293 			reg = CSR_READ(sc, WMREG_CTRL_EXT);
   17294 			reg &= ~CTRL_EXT_FORCE_SMBUS;
   17295 			CSR_WRITE(sc, WMREG_CTRL_EXT, reg);
   17296 		}
   17297 	}
   17298 	return true;
   17299 }
   17300 
   17301 static void
   17302 wm_toggle_lanphypc_pch_lpt(struct wm_softc *sc)
   17303 {
   17304 	uint32_t reg;
   17305 	int i;
   17306 
   17307 	/* Set PHY Config Counter to 50msec */
   17308 	reg = CSR_READ(sc, WMREG_FEXTNVM3);
   17309 	reg &= ~FEXTNVM3_PHY_CFG_COUNTER_MASK;
   17310 	reg |= FEXTNVM3_PHY_CFG_COUNTER_50MS;
   17311 	CSR_WRITE(sc, WMREG_FEXTNVM3, reg);
   17312 
   17313 	/* Toggle LANPHYPC */
   17314 	reg = CSR_READ(sc, WMREG_CTRL);
   17315 	reg |= CTRL_LANPHYPC_OVERRIDE;
   17316 	reg &= ~CTRL_LANPHYPC_VALUE;
   17317 	CSR_WRITE(sc, WMREG_CTRL, reg);
   17318 	CSR_WRITE_FLUSH(sc);
   17319 	delay(1000);
   17320 	reg &= ~CTRL_LANPHYPC_OVERRIDE;
   17321 	CSR_WRITE(sc, WMREG_CTRL, reg);
   17322 	CSR_WRITE_FLUSH(sc);
   17323 
   17324 	if (sc->sc_type < WM_T_PCH_LPT)
   17325 		delay(50 * 1000);
   17326 	else {
   17327 		i = 20;
   17328 
   17329 		do {
   17330 			delay(5 * 1000);
   17331 		} while (((CSR_READ(sc, WMREG_CTRL_EXT) & CTRL_EXT_LPCD) == 0)
   17332 		    && i--);
   17333 
   17334 		delay(30 * 1000);
   17335 	}
   17336 }
   17337 
   17338 static int
   17339 wm_platform_pm_pch_lpt(struct wm_softc *sc, bool link)
   17340 {
   17341 	uint32_t reg = __SHIFTIN(link, LTRV_NONSNOOP_REQ)
   17342 	    | __SHIFTIN(link, LTRV_SNOOP_REQ) | LTRV_SEND;
   17343 	uint32_t rxa;
   17344 	uint16_t scale = 0, lat_enc = 0;
   17345 	int32_t obff_hwm = 0;
   17346 	int64_t lat_ns, value;
   17347 
   17348 	DPRINTF(sc, WM_DEBUG_INIT, ("%s: %s called\n",
   17349 		device_xname(sc->sc_dev), __func__));
   17350 
   17351 	if (link) {
   17352 		uint16_t max_snoop, max_nosnoop, max_ltr_enc;
   17353 		uint32_t status;
   17354 		uint16_t speed;
   17355 		pcireg_t preg;
   17356 
   17357 		status = CSR_READ(sc, WMREG_STATUS);
   17358 		switch (__SHIFTOUT(status, STATUS_SPEED)) {
   17359 		case STATUS_SPEED_10:
   17360 			speed = 10;
   17361 			break;
   17362 		case STATUS_SPEED_100:
   17363 			speed = 100;
   17364 			break;
   17365 		case STATUS_SPEED_1000:
   17366 			speed = 1000;
   17367 			break;
   17368 		default:
   17369 			device_printf(sc->sc_dev, "Unknown speed "
   17370 			    "(status = %08x)\n", status);
   17371 			return -1;
   17372 		}
   17373 
   17374 		/* Rx Packet Buffer Allocation size (KB) */
   17375 		rxa = CSR_READ(sc, WMREG_PBA) & PBA_RXA_MASK;
   17376 
   17377 		/*
   17378 		 * Determine the maximum latency tolerated by the device.
   17379 		 *
   17380 		 * Per the PCIe spec, the tolerated latencies are encoded as
   17381 		 * a 3-bit encoded scale (only 0-5 are valid) multiplied by
   17382 		 * a 10-bit value (0-1023) to provide a range from 1 ns to
   17383 		 * 2^25*(2^10-1) ns.  The scale is encoded as 0=2^0ns,
   17384 		 * 1=2^5ns, 2=2^10ns,...5=2^25ns.
   17385 		 */
   17386 		lat_ns = ((int64_t)rxa * 1024 -
   17387 		    (2 * ((int64_t)sc->sc_ethercom.ec_if.if_mtu
   17388 			+ ETHER_HDR_LEN))) * 8 * 1000;
   17389 		if (lat_ns < 0)
   17390 			lat_ns = 0;
   17391 		else
   17392 			lat_ns /= speed;
   17393 		value = lat_ns;
   17394 
   17395 		while (value > LTRV_VALUE) {
   17396 			scale ++;
   17397 			value = howmany(value, __BIT(5));
   17398 		}
   17399 		if (scale > LTRV_SCALE_MAX) {
   17400 			device_printf(sc->sc_dev,
   17401 			    "Invalid LTR latency scale %d\n", scale);
   17402 			return -1;
   17403 		}
   17404 		lat_enc = (uint16_t)(__SHIFTIN(scale, LTRV_SCALE) | value);
   17405 
   17406 		/* Determine the maximum latency tolerated by the platform */
   17407 		preg = pci_conf_read(sc->sc_pc, sc->sc_pcitag,
   17408 		    WM_PCI_LTR_CAP_LPT);
   17409 		max_snoop = preg & 0xffff;
   17410 		max_nosnoop = preg >> 16;
   17411 
   17412 		max_ltr_enc = MAX(max_snoop, max_nosnoop);
   17413 
   17414 		if (lat_enc > max_ltr_enc) {
   17415 			lat_enc = max_ltr_enc;
   17416 			lat_ns = __SHIFTOUT(lat_enc, PCI_LTR_MAXSNOOPLAT_VAL)
   17417 			    * PCI_LTR_SCALETONS(
   17418 				    __SHIFTOUT(lat_enc,
   17419 					PCI_LTR_MAXSNOOPLAT_SCALE));
   17420 		}
   17421 
   17422 		if (lat_ns) {
   17423 			lat_ns *= speed * 1000;
   17424 			lat_ns /= 8;
   17425 			lat_ns /= 1000000000;
   17426 			obff_hwm = (int32_t)(rxa - lat_ns);
   17427 		}
   17428 		if ((obff_hwm < 0) || (obff_hwm > SVT_OFF_HWM)) {
   17429 			device_printf(sc->sc_dev, "Invalid high water mark %d"
   17430 			    "(rxa = %d, lat_ns = %d)\n",
   17431 			    obff_hwm, (int32_t)rxa, (int32_t)lat_ns);
   17432 			return -1;
   17433 		}
   17434 	}
   17435 	/* Snoop and No-Snoop latencies the same */
   17436 	reg |= lat_enc | __SHIFTIN(lat_enc, LTRV_NONSNOOP);
   17437 	CSR_WRITE(sc, WMREG_LTRV, reg);
   17438 
   17439 	/* Set OBFF high water mark */
   17440 	reg = CSR_READ(sc, WMREG_SVT) & ~SVT_OFF_HWM;
   17441 	reg |= obff_hwm;
   17442 	CSR_WRITE(sc, WMREG_SVT, reg);
   17443 
   17444 	/* Enable OBFF */
   17445 	reg = CSR_READ(sc, WMREG_SVCR);
   17446 	reg |= SVCR_OFF_EN | SVCR_OFF_MASKINT;
   17447 	CSR_WRITE(sc, WMREG_SVCR, reg);
   17448 
   17449 	return 0;
   17450 }
   17451 
   17452 /*
   17453  * I210 Errata 25 and I211 Errata 10
   17454  * Slow System Clock.
   17455  *
   17456  * Note that this function is called on both FLASH and iNVM case on NetBSD.
   17457  */
   17458 static int
   17459 wm_pll_workaround_i210(struct wm_softc *sc)
   17460 {
   17461 	uint32_t mdicnfg, wuc;
   17462 	uint32_t reg;
   17463 	pcireg_t pcireg;
   17464 	uint32_t pmreg;
   17465 	uint16_t nvmword, tmp_nvmword;
   17466 	uint16_t phyval;
   17467 	bool wa_done = false;
   17468 	int i, rv = 0;
   17469 
   17470 	/* Get Power Management cap offset */
   17471 	if (pci_get_capability(sc->sc_pc, sc->sc_pcitag, PCI_CAP_PWRMGMT,
   17472 	    &pmreg, NULL) == 0)
   17473 		return -1;
   17474 
   17475 	/* Save WUC and MDICNFG registers */
   17476 	wuc = CSR_READ(sc, WMREG_WUC);
   17477 	mdicnfg = CSR_READ(sc, WMREG_MDICNFG);
   17478 
   17479 	reg = mdicnfg & ~MDICNFG_DEST;
   17480 	CSR_WRITE(sc, WMREG_MDICNFG, reg);
   17481 
   17482 	if (wm_nvm_read(sc, INVM_AUTOLOAD, 1, &nvmword) != 0) {
   17483 		/*
   17484 		 * The default value of the Initialization Control Word 1
   17485 		 * is the same on both I210's FLASH_HW and I21[01]'s iNVM.
   17486 		 */
   17487 		nvmword = INVM_DEFAULT_AL;
   17488 	}
   17489 	tmp_nvmword = nvmword | INVM_PLL_WO_VAL;
   17490 
   17491 	for (i = 0; i < WM_MAX_PLL_TRIES; i++) {
   17492 		wm_gmii_gs40g_readreg(sc->sc_dev, 1,
   17493 		    GS40G_PHY_PLL_FREQ_PAGE | GS40G_PHY_PLL_FREQ_REG, &phyval);
   17494 
   17495 		if ((phyval & GS40G_PHY_PLL_UNCONF) != GS40G_PHY_PLL_UNCONF) {
   17496 			rv = 0;
   17497 			break; /* OK */
   17498 		} else
   17499 			rv = -1;
   17500 
   17501 		wa_done = true;
   17502 		/* Directly reset the internal PHY */
   17503 		reg = CSR_READ(sc, WMREG_CTRL);
   17504 		CSR_WRITE(sc, WMREG_CTRL, reg | CTRL_PHY_RESET);
   17505 
   17506 		reg = CSR_READ(sc, WMREG_CTRL_EXT);
   17507 		reg |= CTRL_EXT_PHYPDEN | CTRL_EXT_SDLPE;
   17508 		CSR_WRITE(sc, WMREG_CTRL_EXT, reg);
   17509 
   17510 		CSR_WRITE(sc, WMREG_WUC, 0);
   17511 		reg = (INVM_AUTOLOAD << 4) | (tmp_nvmword << 16);
   17512 		CSR_WRITE(sc, WMREG_EEARBC_I210, reg);
   17513 
   17514 		pcireg = pci_conf_read(sc->sc_pc, sc->sc_pcitag,
   17515 		    pmreg + PCI_PMCSR);
   17516 		pcireg |= PCI_PMCSR_STATE_D3;
   17517 		pci_conf_write(sc->sc_pc, sc->sc_pcitag,
   17518 		    pmreg + PCI_PMCSR, pcireg);
   17519 		delay(1000);
   17520 		pcireg &= ~PCI_PMCSR_STATE_D3;
   17521 		pci_conf_write(sc->sc_pc, sc->sc_pcitag,
   17522 		    pmreg + PCI_PMCSR, pcireg);
   17523 
   17524 		reg = (INVM_AUTOLOAD << 4) | (nvmword << 16);
   17525 		CSR_WRITE(sc, WMREG_EEARBC_I210, reg);
   17526 
   17527 		/* Restore WUC register */
   17528 		CSR_WRITE(sc, WMREG_WUC, wuc);
   17529 	}
   17530 
   17531 	/* Restore MDICNFG setting */
   17532 	CSR_WRITE(sc, WMREG_MDICNFG, mdicnfg);
   17533 	if (wa_done)
   17534 		aprint_verbose_dev(sc->sc_dev, "I210 workaround done\n");
   17535 	return rv;
   17536 }
   17537 
   17538 static void
   17539 wm_legacy_irq_quirk_spt(struct wm_softc *sc)
   17540 {
   17541 	uint32_t reg;
   17542 
   17543 	DPRINTF(sc, WM_DEBUG_INIT, ("%s: %s called\n",
   17544 		device_xname(sc->sc_dev), __func__));
   17545 	KASSERT((sc->sc_type == WM_T_PCH_SPT)
   17546 	    || (sc->sc_type == WM_T_PCH_CNP));
   17547 
   17548 	reg = CSR_READ(sc, WMREG_FEXTNVM7);
   17549 	reg |= FEXTNVM7_SIDE_CLK_UNGATE;
   17550 	CSR_WRITE(sc, WMREG_FEXTNVM7, reg);
   17551 
   17552 	reg = CSR_READ(sc, WMREG_FEXTNVM9);
   17553 	reg |= FEXTNVM9_IOSFSB_CLKGATE_DIS | FEXTNVM9_IOSFSB_CLKREQ_DIS;
   17554 	CSR_WRITE(sc, WMREG_FEXTNVM9, reg);
   17555 }
   17556 
   17557 /* Sysctl functions */
   17558 static int
   17559 wm_sysctl_tdh_handler(SYSCTLFN_ARGS)
   17560 {
   17561 	struct sysctlnode node = *rnode;
   17562 	struct wm_txqueue *txq = (struct wm_txqueue *)node.sysctl_data;
   17563 	struct wm_queue *wmq = container_of(txq, struct wm_queue, wmq_txq);
   17564 	struct wm_softc *sc = txq->txq_sc;
   17565 	uint32_t reg;
   17566 
   17567 	reg = CSR_READ(sc, WMREG_TDH(wmq->wmq_id));
   17568 	node.sysctl_data = &reg;
   17569 	return sysctl_lookup(SYSCTLFN_CALL(&node));
   17570 }
   17571 
   17572 static int
   17573 wm_sysctl_tdt_handler(SYSCTLFN_ARGS)
   17574 {
   17575 	struct sysctlnode node = *rnode;
   17576 	struct wm_txqueue *txq = (struct wm_txqueue *)node.sysctl_data;
   17577 	struct wm_queue *wmq = container_of(txq, struct wm_queue, wmq_txq);
   17578 	struct wm_softc *sc = txq->txq_sc;
   17579 	uint32_t reg;
   17580 
   17581 	reg = CSR_READ(sc, WMREG_TDT(wmq->wmq_id));
   17582 	node.sysctl_data = &reg;
   17583 	return sysctl_lookup(SYSCTLFN_CALL(&node));
   17584 }
   17585 
   17586 #ifdef WM_DEBUG
   17587 static int
   17588 wm_sysctl_debug(SYSCTLFN_ARGS)
   17589 {
   17590 	struct sysctlnode node = *rnode;
   17591 	struct wm_softc *sc = (struct wm_softc *)node.sysctl_data;
   17592 	uint32_t dflags;
   17593 	int error;
   17594 
   17595 	dflags = sc->sc_debug;
   17596 	node.sysctl_data = &dflags;
   17597 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   17598 
   17599 	if (error || newp == NULL)
   17600 		return error;
   17601 
   17602 	sc->sc_debug = dflags;
   17603 	device_printf(sc->sc_dev, "TARC0: %08x\n", CSR_READ(sc, WMREG_TARC0));
   17604 	device_printf(sc->sc_dev, "TDT0: %08x\n", CSR_READ(sc, WMREG_TDT(0)));
   17605 
   17606 	return 0;
   17607 }
   17608 #endif
   17609