Home | History | Annotate | Line # | Download | only in pci
if_dge.c revision 1.2
      1 /*	$NetBSD: if_dge.c,v 1.2 2004/04/13 11:40:06 ragge Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2004, SUNET, Swedish University Computer Network.
      5  * All rights reserved.
      6  *
      7  * Written by Anders Magnusson for SUNET, Swedish University Computer Network.
      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  *	SUNET, Swedish University Computer Network.
     21  * 4. The name of SUNET may not be used to endorse or promote products
     22  *    derived from this software without specific prior written permission.
     23  *
     24  * THIS SOFTWARE IS PROVIDED BY SUNET ``AS IS'' AND
     25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     26  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     27  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
     28  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     29  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     30  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     31  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     32  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     33  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     34  * POSSIBILITY OF SUCH DAMAGE.
     35  */
     36 
     37 /*
     38  * Copyright (c) 2001, 2002, 2003 Wasabi Systems, Inc.
     39  * All rights reserved.
     40  *
     41  * Written by Jason R. Thorpe for Wasabi Systems, Inc.
     42  *
     43  * Redistribution and use in source and binary forms, with or without
     44  * modification, are permitted provided that the following conditions
     45  * are met:
     46  * 1. Redistributions of source code must retain the above copyright
     47  *    notice, this list of conditions and the following disclaimer.
     48  * 2. Redistributions in binary form must reproduce the above copyright
     49  *    notice, this list of conditions and the following disclaimer in the
     50  *    documentation and/or other materials provided with the distribution.
     51  * 3. All advertising materials mentioning features or use of this software
     52  *    must display the following acknowledgement:
     53  *	This product includes software developed for the NetBSD Project by
     54  *	Wasabi Systems, Inc.
     55  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
     56  *    or promote products derived from this software without specific prior
     57  *    written permission.
     58  *
     59  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
     60  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     61  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     62  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
     63  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     64  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     65  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     66  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     67  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     68  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     69  * POSSIBILITY OF SUCH DAMAGE.
     70  */
     71 
     72 /*
     73  * Device driver for the Intel 82597EX Ten Gigabit Ethernet controller.
     74  *
     75  * TODO (in no specific order):
     76  *	HW VLAN support.
     77  *	TSE offloading (needs kernel changes...)
     78  *	RAIDC (receive interrupt delay adaptation)
     79  *	Use memory > 4GB.
     80  */
     81 
     82 #include <sys/cdefs.h>
     83 __KERNEL_RCSID(0, "$NetBSD: if_dge.c,v 1.2 2004/04/13 11:40:06 ragge Exp $");
     84 
     85 #include "bpfilter.h"
     86 #include "rnd.h"
     87 
     88 #include <sys/param.h>
     89 #include <sys/systm.h>
     90 #include <sys/callout.h>
     91 #include <sys/mbuf.h>
     92 #include <sys/malloc.h>
     93 #include <sys/kernel.h>
     94 #include <sys/socket.h>
     95 #include <sys/ioctl.h>
     96 #include <sys/errno.h>
     97 #include <sys/device.h>
     98 #include <sys/queue.h>
     99 
    100 #include <uvm/uvm_extern.h>		/* for PAGE_SIZE */
    101 
    102 #if NRND > 0
    103 #include <sys/rnd.h>
    104 #endif
    105 
    106 #include <net/if.h>
    107 #include <net/if_dl.h>
    108 #include <net/if_media.h>
    109 #include <net/if_ether.h>
    110 
    111 #if NBPFILTER > 0
    112 #include <net/bpf.h>
    113 #endif
    114 
    115 #include <netinet/in.h>			/* XXX for struct ip */
    116 #include <netinet/in_systm.h>		/* XXX for struct ip */
    117 #include <netinet/ip.h>			/* XXX for struct ip */
    118 #include <netinet/tcp.h>		/* XXX for struct tcphdr */
    119 
    120 #include <machine/bus.h>
    121 #include <machine/intr.h>
    122 #include <machine/endian.h>
    123 
    124 #include <dev/mii/mii.h>
    125 #include <dev/mii/miivar.h>
    126 #include <dev/mii/mii_bitbang.h>
    127 
    128 #include <dev/pci/pcireg.h>
    129 #include <dev/pci/pcivar.h>
    130 #include <dev/pci/pcidevs.h>
    131 
    132 #include <dev/pci/if_dgereg.h>
    133 
    134 #define DGE_EVENT_COUNTERS
    135 #define DGE_DEBUG
    136 
    137 #ifdef DGE_DEBUG
    138 #define	DGE_DEBUG_LINK		0x01
    139 #define	DGE_DEBUG_TX		0x02
    140 #define	DGE_DEBUG_RX		0x04
    141 #define	DGE_DEBUG_CKSUM		0x08
    142 int	dge_debug = 0;
    143 
    144 #define	DPRINTF(x, y)	if (dge_debug & (x)) printf y
    145 #else
    146 #define	DPRINTF(x, y)	/* nothing */
    147 #endif /* DGE_DEBUG */
    148 
    149 /*
    150  * Transmit descriptor list size. We allow up to 100 DMA segments per
    151  * packet (Intel reports of jumbo frame packets with as
    152  * many as 80 DMA segments when using 16k buffers).
    153  */
    154 #define	DGE_NTXSEGS		100
    155 #define	DGE_IFQUEUELEN		20000
    156 #define	DGE_TXQUEUELEN		2048
    157 #define	DGE_TXQUEUELEN_MASK	(DGE_TXQUEUELEN - 1)
    158 #define	DGE_TXQUEUE_GC		(DGE_TXQUEUELEN / 8)
    159 #define	DGE_NTXDESC		1024
    160 #define	DGE_NTXDESC_MASK		(DGE_NTXDESC - 1)
    161 #define	DGE_NEXTTX(x)		(((x) + 1) & DGE_NTXDESC_MASK)
    162 #define	DGE_NEXTTXS(x)		(((x) + 1) & DGE_TXQUEUELEN_MASK)
    163 
    164 /*
    165  * Receive descriptor list size.
    166  * Packet is of size MCLBYTES, and for jumbo packets buffers may
    167  * be chained.  Due to the nature of the card (high-speed), keep this
    168  * ring large. With 2k buffers the ring can store 400 jumbo packets,
    169  * which at full speed will be received in just under 3ms.
    170  */
    171 #define	DGE_NRXDESC		2048
    172 #define	DGE_NRXDESC_MASK	(DGE_NRXDESC - 1)
    173 #define	DGE_NEXTRX(x)		(((x) + 1) & DGE_NRXDESC_MASK)
    174 /*
    175  * # of descriptors between head and written descriptors.
    176  * This is to work-around two erratas.
    177  */
    178 #define DGE_RXSPACE		10
    179 #define	DGE_PREVRX(x)		(((x) - DGE_RXSPACE) & DGE_NRXDESC_MASK)
    180 /*
    181  * Receive descriptor fetch threshholds. These are values recommended
    182  * by Intel, do not touch them unless you know what you are doing.
    183  */
    184 #define	RXDCTL_PTHRESH_VAL	128
    185 #define	RXDCTL_HTHRESH_VAL	16
    186 #define	RXDCTL_WTHRESH_VAL	16
    187 
    188 
    189 /*
    190  * Tweakable parameters; default values.
    191  */
    192 #define	FCRTH	0x30000	/* Send XOFF water mark */
    193 #define	FCRTL	0x28000	/* Send XON water mark */
    194 #define	RDTR	0x20	/* Interrupt delay after receive, .8192us units */
    195 #define	TIDV	0x20	/* Interrupt delay after send, .8192us units */
    196 
    197 /*
    198  * Control structures are DMA'd to the i82597 chip.  We allocate them in
    199  * a single clump that maps to a single DMA segment to make serveral things
    200  * easier.
    201  */
    202 struct dge_control_data {
    203 	/*
    204 	 * The transmit descriptors.
    205 	 */
    206 	struct dge_tdes wcd_txdescs[DGE_NTXDESC];
    207 
    208 	/*
    209 	 * The receive descriptors.
    210 	 */
    211 	struct dge_rdes wcd_rxdescs[DGE_NRXDESC];
    212 };
    213 
    214 #define	DGE_CDOFF(x)	offsetof(struct dge_control_data, x)
    215 #define	DGE_CDTXOFF(x)	DGE_CDOFF(wcd_txdescs[(x)])
    216 #define	DGE_CDRXOFF(x)	DGE_CDOFF(wcd_rxdescs[(x)])
    217 
    218 /*
    219  * The DGE interface have a higher max MTU size than normal jumbo frames.
    220  */
    221 #define DGE_MAX_MTU     16288   /* Max MTU size for this interface */
    222 
    223 /*
    224  * Software state for transmit jobs.
    225  */
    226 struct dge_txsoft {
    227 	struct mbuf *txs_mbuf;		/* head of our mbuf chain */
    228 	bus_dmamap_t txs_dmamap;	/* our DMA map */
    229 	int txs_firstdesc;		/* first descriptor in packet */
    230 	int txs_lastdesc;		/* last descriptor in packet */
    231 	int txs_ndesc;			/* # of descriptors used */
    232 };
    233 
    234 /*
    235  * Software state for receive buffers.  Each descriptor gets a
    236  * 2k (MCLBYTES) buffer and a DMA map.  For packets which fill
    237  * more than one buffer, we chain them together.
    238  */
    239 struct dge_rxsoft {
    240 	struct mbuf *rxs_mbuf;		/* head of our mbuf chain */
    241 	bus_dmamap_t rxs_dmamap;	/* our DMA map */
    242 };
    243 
    244 /*
    245  * Software state per device.
    246  */
    247 struct dge_softc {
    248 	struct device sc_dev;		/* generic device information */
    249 	bus_space_tag_t sc_st;		/* bus space tag */
    250 	bus_space_handle_t sc_sh;	/* bus space handle */
    251 	bus_dma_tag_t sc_dmat;		/* bus DMA tag */
    252 	struct ethercom sc_ethercom;	/* ethernet common data */
    253 	void *sc_sdhook;		/* shutdown hook */
    254 
    255 	int sc_flags;			/* flags; see below */
    256 	int sc_bus_speed;		/* PCI/PCIX bus speed */
    257 	int sc_pcix_offset;		/* PCIX capability register offset */
    258 
    259 	pci_chipset_tag_t sc_pc;
    260 	pcitag_t sc_pt;
    261 	int sc_mmrbc;			/* Max PCIX memory read byte count */
    262 
    263 	void *sc_ih;			/* interrupt cookie */
    264 
    265 	struct ifmedia sc_media;
    266 
    267 	bus_dmamap_t sc_cddmamap;	/* control data DMA map */
    268 #define	sc_cddma	sc_cddmamap->dm_segs[0].ds_addr
    269 
    270 	int		sc_align_tweak;
    271 
    272 	/*
    273 	 * Software state for the transmit and receive descriptors.
    274 	 */
    275 	struct dge_txsoft sc_txsoft[DGE_TXQUEUELEN];
    276 	struct dge_rxsoft sc_rxsoft[DGE_NRXDESC];
    277 
    278 	/*
    279 	 * Control data structures.
    280 	 */
    281 	struct dge_control_data *sc_control_data;
    282 #define	sc_txdescs	sc_control_data->wcd_txdescs
    283 #define	sc_rxdescs	sc_control_data->wcd_rxdescs
    284 
    285 #ifdef DGE_EVENT_COUNTERS
    286 	/* Event counters. */
    287 	struct evcnt sc_ev_txsstall;	/* Tx stalled due to no txs */
    288 	struct evcnt sc_ev_txdstall;	/* Tx stalled due to no txd */
    289 	struct evcnt sc_ev_txforceintr;	/* Tx interrupts forced */
    290 	struct evcnt sc_ev_txdw;	/* Tx descriptor interrupts */
    291 	struct evcnt sc_ev_txqe;	/* Tx queue empty interrupts */
    292 	struct evcnt sc_ev_rxintr;	/* Rx interrupts */
    293 	struct evcnt sc_ev_linkintr;	/* Link interrupts */
    294 
    295 	struct evcnt sc_ev_rxipsum;	/* IP checksums checked in-bound */
    296 	struct evcnt sc_ev_rxtusum;	/* TCP/UDP cksums checked in-bound */
    297 	struct evcnt sc_ev_txipsum;	/* IP checksums comp. out-bound */
    298 	struct evcnt sc_ev_txtusum;	/* TCP/UDP cksums comp. out-bound */
    299 
    300 	struct evcnt sc_ev_txctx_init;	/* Tx cksum context cache initialized */
    301 	struct evcnt sc_ev_txctx_hit;	/* Tx cksum context cache hit */
    302 	struct evcnt sc_ev_txctx_miss;	/* Tx cksum context cache miss */
    303 
    304 	struct evcnt sc_ev_txseg[DGE_NTXSEGS]; /* Tx packets w/ N segments */
    305 	struct evcnt sc_ev_txdrop;	/* Tx packets dropped (too many segs) */
    306 #endif /* DGE_EVENT_COUNTERS */
    307 
    308 	int	sc_txfree;		/* number of free Tx descriptors */
    309 	int	sc_txnext;		/* next ready Tx descriptor */
    310 
    311 	int	sc_txsfree;		/* number of free Tx jobs */
    312 	int	sc_txsnext;		/* next free Tx job */
    313 	int	sc_txsdirty;		/* dirty Tx jobs */
    314 
    315 	uint32_t sc_txctx_ipcs;		/* cached Tx IP cksum ctx */
    316 	uint32_t sc_txctx_tucs;		/* cached Tx TCP/UDP cksum ctx */
    317 
    318 	int	sc_rxptr;		/* next ready Rx descriptor/queue ent */
    319 	int	sc_rxdiscard;
    320 	int	sc_rxlen;
    321 	struct mbuf *sc_rxhead;
    322 	struct mbuf *sc_rxtail;
    323 	struct mbuf **sc_rxtailp;
    324 
    325 	uint32_t sc_ctrl0;		/* prototype CTRL0 register */
    326 	uint32_t sc_icr;		/* prototype interrupt bits */
    327 	uint32_t sc_tctl;		/* prototype TCTL register */
    328 	uint32_t sc_rctl;		/* prototype RCTL register */
    329 
    330 	int sc_mchash_type;		/* multicast filter offset */
    331 
    332 	uint16_t sc_eeprom[EEPROM_SIZE];
    333 
    334 #if NRND > 0
    335 	rndsource_element_t rnd_source;	/* random source */
    336 #endif
    337 };
    338 
    339 #define	DGE_RXCHAIN_RESET(sc)						\
    340 do {									\
    341 	(sc)->sc_rxtailp = &(sc)->sc_rxhead;				\
    342 	*(sc)->sc_rxtailp = NULL;					\
    343 	(sc)->sc_rxlen = 0;						\
    344 } while (/*CONSTCOND*/0)
    345 
    346 #define	DGE_RXCHAIN_LINK(sc, m)						\
    347 do {									\
    348 	*(sc)->sc_rxtailp = (sc)->sc_rxtail = (m);			\
    349 	(sc)->sc_rxtailp = &(m)->m_next;				\
    350 } while (/*CONSTCOND*/0)
    351 
    352 /* sc_flags */
    353 #define	DGE_F_BUS64		0x20	/* bus is 64-bit */
    354 #define	DGE_F_PCIX		0x40	/* bus is PCI-X */
    355 
    356 #ifdef DGE_EVENT_COUNTERS
    357 #define	DGE_EVCNT_INCR(ev)	(ev)->ev_count++
    358 #else
    359 #define	DGE_EVCNT_INCR(ev)	/* nothing */
    360 #endif
    361 
    362 #define	CSR_READ(sc, reg)						\
    363 	bus_space_read_4((sc)->sc_st, (sc)->sc_sh, (reg))
    364 #define	CSR_WRITE(sc, reg, val)						\
    365 	bus_space_write_4((sc)->sc_st, (sc)->sc_sh, (reg), (val))
    366 
    367 #define	DGE_CDTXADDR(sc, x)	((sc)->sc_cddma + DGE_CDTXOFF((x)))
    368 #define	DGE_CDRXADDR(sc, x)	((sc)->sc_cddma + DGE_CDRXOFF((x)))
    369 
    370 #define	DGE_CDTXSYNC(sc, x, n, ops)					\
    371 do {									\
    372 	int __x, __n;							\
    373 									\
    374 	__x = (x);							\
    375 	__n = (n);							\
    376 									\
    377 	/* If it will wrap around, sync to the end of the ring. */	\
    378 	if ((__x + __n) > DGE_NTXDESC) {					\
    379 		bus_dmamap_sync((sc)->sc_dmat, (sc)->sc_cddmamap,	\
    380 		    DGE_CDTXOFF(__x), sizeof(struct dge_tdes) *		\
    381 		    (DGE_NTXDESC - __x), (ops));				\
    382 		__n -= (DGE_NTXDESC - __x);				\
    383 		__x = 0;						\
    384 	}								\
    385 									\
    386 	/* Now sync whatever is left. */				\
    387 	bus_dmamap_sync((sc)->sc_dmat, (sc)->sc_cddmamap,		\
    388 	    DGE_CDTXOFF(__x), sizeof(struct dge_tdes) * __n, (ops));	\
    389 } while (/*CONSTCOND*/0)
    390 
    391 #define	DGE_CDRXSYNC(sc, x, ops)						\
    392 do {									\
    393 	bus_dmamap_sync((sc)->sc_dmat, (sc)->sc_cddmamap,		\
    394 	   DGE_CDRXOFF((x)), sizeof(struct dge_rdes), (ops));		\
    395 } while (/*CONSTCOND*/0)
    396 
    397 #define	DGE_INIT_RXDESC(sc, x)						\
    398 do {									\
    399 	struct dge_rxsoft *__rxs = &(sc)->sc_rxsoft[(x)];		\
    400 	struct dge_rdes *__rxd = &(sc)->sc_rxdescs[(x)];		\
    401 	struct mbuf *__m = __rxs->rxs_mbuf;				\
    402 									\
    403 	/*								\
    404 	 * Note: We scoot the packet forward 2 bytes in the buffer	\
    405 	 * so that the payload after the Ethernet header is aligned	\
    406 	 * to a 4-byte boundary.					\
    407 	 *								\
    408 	 * XXX BRAINDAMAGE ALERT!					\
    409 	 * The stupid chip uses the same size for every buffer, which	\
    410 	 * is set in the Receive Control register.  We are using the 2K	\
    411 	 * size option, but what we REALLY want is (2K - 2)!  For this	\
    412 	 * reason, we can't "scoot" packets longer than the standard	\
    413 	 * Ethernet MTU.  On strict-alignment platforms, if the total	\
    414 	 * size exceeds (2K - 2) we set align_tweak to 0 and let	\
    415 	 * the upper layer copy the headers.				\
    416 	 */								\
    417 	__m->m_data = __m->m_ext.ext_buf + (sc)->sc_align_tweak;	\
    418 									\
    419 	__rxd->dr_baddrl =					\
    420 	    htole32(__rxs->rxs_dmamap->dm_segs[0].ds_addr + 		\
    421 		(sc)->sc_align_tweak);					\
    422 	__rxd->dr_baddrh = 0;					\
    423 	__rxd->dr_len = 0;						\
    424 	__rxd->dr_cksum = 0;						\
    425 	__rxd->dr_status = 0;						\
    426 	__rxd->dr_errors = 0;						\
    427 	__rxd->dr_special = 0;						\
    428 	DGE_CDRXSYNC((sc), (x), BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE); \
    429 									\
    430 	CSR_WRITE((sc), DGE_RDT, (x));					\
    431 } while (/*CONSTCOND*/0)
    432 
    433 static void	dge_start(struct ifnet *);
    434 static void	dge_watchdog(struct ifnet *);
    435 static int	dge_ioctl(struct ifnet *, u_long, caddr_t);
    436 static int	dge_init(struct ifnet *);
    437 static void	dge_stop(struct ifnet *, int);
    438 
    439 static void	dge_shutdown(void *);
    440 
    441 static void	dge_reset(struct dge_softc *);
    442 static void	dge_rxdrain(struct dge_softc *);
    443 static int	dge_add_rxbuf(struct dge_softc *, int);
    444 
    445 static void	dge_set_filter(struct dge_softc *);
    446 
    447 static int	dge_intr(void *);
    448 static void	dge_txintr(struct dge_softc *);
    449 static void	dge_rxintr(struct dge_softc *);
    450 static void	dge_linkintr(struct dge_softc *, uint32_t);
    451 
    452 static int	dge_match(struct device *, struct cfdata *, void *);
    453 static void	dge_attach(struct device *, struct device *, void *);
    454 
    455 static int	dge_read_eeprom(struct dge_softc *sc);
    456 static int	dge_eeprom_clockin(struct dge_softc *sc);
    457 static void	dge_eeprom_clockout(struct dge_softc *sc, int bit);
    458 static uint16_t	dge_eeprom_word(struct dge_softc *sc, int addr);
    459 static int	dge_xgmii_mediachange(struct ifnet *);
    460 static void	dge_xgmii_mediastatus(struct ifnet *, struct ifmediareq *);
    461 static void	dge_xgmii_reset(struct dge_softc *);
    462 static void	dge_xgmii_writereg(struct device *, int, int, int);
    463 
    464 
    465 CFATTACH_DECL(dge, sizeof(struct dge_softc),
    466     dge_match, dge_attach, NULL, NULL);
    467 
    468 #ifdef DGE_EVENT_COUNTERS
    469 #if DGE_NTXSEGS > 100
    470 #error Update dge_txseg_evcnt_names
    471 #endif
    472 static char (*dge_txseg_evcnt_names)[DGE_NTXSEGS][8 /* "txseg00" + \0 */];
    473 #endif /* DGE_EVENT_COUNTERS */
    474 
    475 static int
    476 dge_match(struct device *parent, struct cfdata *cf, void *aux)
    477 {
    478 	struct pci_attach_args *pa = aux;
    479 
    480 	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_INTEL &&
    481 	    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_INTEL_82597EX)
    482 		return (1);
    483 
    484 	return (0);
    485 }
    486 
    487 static void
    488 dge_attach(struct device *parent, struct device *self, void *aux)
    489 {
    490 	struct dge_softc *sc = (void *) self;
    491 	struct pci_attach_args *pa = aux;
    492 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    493 	pci_chipset_tag_t pc = pa->pa_pc;
    494 	pci_intr_handle_t ih;
    495 	const char *intrstr = NULL;
    496 	bus_dma_segment_t seg;
    497 	int i, rseg, error;
    498 	uint8_t enaddr[ETHER_ADDR_LEN];
    499 	pcireg_t preg, memtype;
    500 	uint32_t reg;
    501 
    502 	sc->sc_dmat = pa->pa_dmat;
    503 	sc->sc_pc = pa->pa_pc;
    504 	sc->sc_pt = pa->pa_tag;
    505 
    506 	preg = PCI_REVISION(pci_conf_read(pc, pa->pa_tag, PCI_CLASS_REG));
    507 	aprint_naive(": Ethernet controller\n");
    508 	aprint_normal(": Intel i82597EX 10GbE-LR Ethernet, rev. %d\n", preg);
    509 
    510 	memtype = pci_mapreg_type(pa->pa_pc, pa->pa_tag, DGE_PCI_BAR);
    511         if (pci_mapreg_map(pa, DGE_PCI_BAR, memtype, 0,
    512             &sc->sc_st, &sc->sc_sh, NULL, NULL)) {
    513                 aprint_error("%s: unable to map device registers\n",
    514                     sc->sc_dev.dv_xname);
    515                 return;
    516         }
    517 
    518 	/* Enable bus mastering */
    519 	preg = pci_conf_read(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
    520 	preg |= PCI_COMMAND_MASTER_ENABLE;
    521 	pci_conf_write(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG, preg);
    522 
    523 	/*
    524 	 * Map and establish our interrupt.
    525 	 */
    526 	if (pci_intr_map(pa, &ih)) {
    527 		aprint_error("%s: unable to map interrupt\n",
    528 		    sc->sc_dev.dv_xname);
    529 		return;
    530 	}
    531 	intrstr = pci_intr_string(pc, ih);
    532 	sc->sc_ih = pci_intr_establish(pc, ih, IPL_NET, dge_intr, sc);
    533 	if (sc->sc_ih == NULL) {
    534 		aprint_error("%s: unable to establish interrupt",
    535 		    sc->sc_dev.dv_xname);
    536 		if (intrstr != NULL)
    537 			aprint_normal(" at %s", intrstr);
    538 		aprint_normal("\n");
    539 		return;
    540 	}
    541 	aprint_normal("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstr);
    542 
    543 	/*
    544 	 * Determine a few things about the bus we're connected to.
    545 	 */
    546 	reg = CSR_READ(sc, DGE_STATUS);
    547 	if (reg & STATUS_BUS64)
    548 		sc->sc_flags |= DGE_F_BUS64;
    549 
    550 	sc->sc_flags |= DGE_F_PCIX;
    551 	if (pci_get_capability(pa->pa_pc, pa->pa_tag,
    552 			       PCI_CAP_PCIX,
    553 			       &sc->sc_pcix_offset, NULL) == 0)
    554 		aprint_error("%s: unable to find PCIX "
    555 		    "capability\n", sc->sc_dev.dv_xname);
    556 
    557 	if (sc->sc_flags & DGE_F_PCIX) {
    558 		switch (reg & STATUS_PCIX_MSK) {
    559 		case STATUS_PCIX_66:
    560 			sc->sc_bus_speed = 66;
    561 			break;
    562 		case STATUS_PCIX_100:
    563 			sc->sc_bus_speed = 100;
    564 			break;
    565 		case STATUS_PCIX_133:
    566 			sc->sc_bus_speed = 133;
    567 			break;
    568 		default:
    569 			aprint_error(
    570 			    "%s: unknown PCIXSPD %d; assuming 66MHz\n",
    571 			    sc->sc_dev.dv_xname,
    572 			    reg & STATUS_PCIX_MSK);
    573 			sc->sc_bus_speed = 66;
    574 		}
    575 	} else
    576 		sc->sc_bus_speed = (reg & STATUS_BUS64) ? 66 : 33;
    577 	aprint_verbose("%s: %d-bit %dMHz %s bus\n", sc->sc_dev.dv_xname,
    578 	    (sc->sc_flags & DGE_F_BUS64) ? 64 : 32, sc->sc_bus_speed,
    579 	    (sc->sc_flags & DGE_F_PCIX) ? "PCIX" : "PCI");
    580 
    581 	/*
    582 	 * Allocate the control data structures, and create and load the
    583 	 * DMA map for it.
    584 	 */
    585 	if ((error = bus_dmamem_alloc(sc->sc_dmat,
    586 	    sizeof(struct dge_control_data), PAGE_SIZE, 0, &seg, 1, &rseg,
    587 	    0)) != 0) {
    588 		aprint_error(
    589 		    "%s: unable to allocate control data, error = %d\n",
    590 		    sc->sc_dev.dv_xname, error);
    591 		goto fail_0;
    592 	}
    593 
    594 	if ((error = bus_dmamem_map(sc->sc_dmat, &seg, rseg,
    595 	    sizeof(struct dge_control_data), (caddr_t *)&sc->sc_control_data,
    596 	    0)) != 0) {
    597 		aprint_error("%s: unable to map control data, error = %d\n",
    598 		    sc->sc_dev.dv_xname, error);
    599 		goto fail_1;
    600 	}
    601 
    602 	if ((error = bus_dmamap_create(sc->sc_dmat,
    603 	    sizeof(struct dge_control_data), 1,
    604 	    sizeof(struct dge_control_data), 0, 0, &sc->sc_cddmamap)) != 0) {
    605 		aprint_error("%s: unable to create control data DMA map, "
    606 		    "error = %d\n", sc->sc_dev.dv_xname, error);
    607 		goto fail_2;
    608 	}
    609 
    610 	if ((error = bus_dmamap_load(sc->sc_dmat, sc->sc_cddmamap,
    611 	    sc->sc_control_data, sizeof(struct dge_control_data), NULL,
    612 	    0)) != 0) {
    613 		aprint_error(
    614 		    "%s: unable to load control data DMA map, error = %d\n",
    615 		    sc->sc_dev.dv_xname, error);
    616 		goto fail_3;
    617 	}
    618 
    619 	/*
    620 	 * Create the transmit buffer DMA maps.
    621 	 */
    622 	for (i = 0; i < DGE_TXQUEUELEN; i++) {
    623 		if ((error = bus_dmamap_create(sc->sc_dmat, DGE_MAX_MTU,
    624 		    DGE_NTXSEGS, MCLBYTES, 0, 0,
    625 		    &sc->sc_txsoft[i].txs_dmamap)) != 0) {
    626 			aprint_error("%s: unable to create Tx DMA map %d, "
    627 			    "error = %d\n", sc->sc_dev.dv_xname, i, error);
    628 			goto fail_4;
    629 		}
    630 	}
    631 
    632 	/*
    633 	 * Create the receive buffer DMA maps.
    634 	 */
    635 	for (i = 0; i < DGE_NRXDESC; i++) {
    636 		if ((error = bus_dmamap_create(sc->sc_dmat, MCLBYTES, 1,
    637 		    MCLBYTES, 0, 0, &sc->sc_rxsoft[i].rxs_dmamap)) != 0) {
    638 			aprint_error("%s: unable to create Rx DMA map %d, "
    639 			    "error = %d\n", sc->sc_dev.dv_xname, i, error);
    640 			goto fail_5;
    641 		}
    642 		sc->sc_rxsoft[i].rxs_mbuf = NULL;
    643 	}
    644 
    645 	/*
    646 	 * Set bits in ctrl0 register.
    647 	 * Should get the software defined pins out of EEPROM?
    648 	 */
    649 	sc->sc_ctrl0 |= CTRL0_RPE | CTRL0_TPE; /* XON/XOFF */
    650 	sc->sc_ctrl0 |= CTRL0_SDP3_DIR | CTRL0_SDP2_DIR | CTRL0_SDP1_DIR |
    651 	    CTRL0_SDP0_DIR | CTRL0_SDP3 | CTRL0_SDP2 | CTRL0_SDP0;
    652 
    653 	/*
    654 	 * Reset the chip to a known state.
    655 	 */
    656 	dge_reset(sc);
    657 
    658 	/*
    659 	 * Reset the PHY.
    660 	 */
    661 	dge_xgmii_reset(sc);
    662 
    663 	/*
    664 	 * Read in EEPROM data.
    665 	 */
    666 	if (dge_read_eeprom(sc)) {
    667 		aprint_error("%s: couldn't read EEPROM\n", sc->sc_dev.dv_xname);
    668 		return;
    669 	}
    670 
    671 	/*
    672 	 * Get the ethernet address.
    673 	 */
    674 	enaddr[0] = sc->sc_eeprom[EE_ADDR01] & 0377;
    675 	enaddr[1] = sc->sc_eeprom[EE_ADDR01] >> 8;
    676 	enaddr[2] = sc->sc_eeprom[EE_ADDR23] & 0377;
    677 	enaddr[3] = sc->sc_eeprom[EE_ADDR23] >> 8;
    678 	enaddr[4] = sc->sc_eeprom[EE_ADDR45] & 0377;
    679 	enaddr[5] = sc->sc_eeprom[EE_ADDR45] >> 8;
    680 
    681 	aprint_normal("%s: Ethernet address %s\n", sc->sc_dev.dv_xname,
    682 	    ether_sprintf(enaddr));
    683 
    684 	/*
    685 	 * Setup media stuff.
    686 	 */
    687         ifmedia_init(&sc->sc_media, IFM_IMASK, dge_xgmii_mediachange,
    688             dge_xgmii_mediastatus);
    689         ifmedia_add(&sc->sc_media, IFM_ETHER|IFM_10G_LR, 0, NULL);
    690         ifmedia_set(&sc->sc_media, IFM_ETHER|IFM_10G_LR);
    691 
    692 	ifp = &sc->sc_ethercom.ec_if;
    693 	strcpy(ifp->if_xname, sc->sc_dev.dv_xname);
    694 	ifp->if_softc = sc;
    695 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
    696 	ifp->if_ioctl = dge_ioctl;
    697 	ifp->if_start = dge_start;
    698 	ifp->if_watchdog = dge_watchdog;
    699 	ifp->if_init = dge_init;
    700 	ifp->if_stop = dge_stop;
    701 	IFQ_SET_MAXLEN(&ifp->if_snd, max(DGE_IFQUEUELEN, IFQ_MAXLEN));
    702 	IFQ_SET_READY(&ifp->if_snd);
    703 
    704 	sc->sc_ethercom.ec_capabilities |=
    705 	    ETHERCAP_JUMBO_MTU | ETHERCAP_VLAN_MTU;
    706 
    707 	/*
    708 	 * We can perform TCPv4 and UDPv4 checkums in-bound.
    709 	 */
    710 	ifp->if_capabilities |=
    711 	    IFCAP_CSUM_IPv4 | IFCAP_CSUM_TCPv4 | IFCAP_CSUM_UDPv4;
    712 
    713 	/*
    714 	 * Attach the interface.
    715 	 */
    716 	if_attach(ifp);
    717 	ether_ifattach(ifp, enaddr);
    718 #if NRND > 0
    719 	rnd_attach_source(&sc->rnd_source, sc->sc_dev.dv_xname,
    720 	    RND_TYPE_NET, 0);
    721 #endif
    722 
    723 #ifdef DGE_EVENT_COUNTERS
    724 	/* Fix segment event naming */
    725 	if (dge_txseg_evcnt_names == NULL) {
    726 		dge_txseg_evcnt_names =
    727 		    malloc(sizeof(*dge_txseg_evcnt_names), M_DEVBUF, M_WAITOK);
    728 		for (i = 0; i < DGE_NTXSEGS; i++)
    729 			sprintf((*dge_txseg_evcnt_names)[i], "txseg%d", i);
    730 	}
    731 
    732 	/* Attach event counters. */
    733 	evcnt_attach_dynamic(&sc->sc_ev_txsstall, EVCNT_TYPE_MISC,
    734 	    NULL, sc->sc_dev.dv_xname, "txsstall");
    735 	evcnt_attach_dynamic(&sc->sc_ev_txdstall, EVCNT_TYPE_MISC,
    736 	    NULL, sc->sc_dev.dv_xname, "txdstall");
    737 	evcnt_attach_dynamic(&sc->sc_ev_txforceintr, EVCNT_TYPE_MISC,
    738 	    NULL, sc->sc_dev.dv_xname, "txforceintr");
    739 	evcnt_attach_dynamic(&sc->sc_ev_txdw, EVCNT_TYPE_INTR,
    740 	    NULL, sc->sc_dev.dv_xname, "txdw");
    741 	evcnt_attach_dynamic(&sc->sc_ev_txqe, EVCNT_TYPE_INTR,
    742 	    NULL, sc->sc_dev.dv_xname, "txqe");
    743 	evcnt_attach_dynamic(&sc->sc_ev_rxintr, EVCNT_TYPE_INTR,
    744 	    NULL, sc->sc_dev.dv_xname, "rxintr");
    745 	evcnt_attach_dynamic(&sc->sc_ev_linkintr, EVCNT_TYPE_INTR,
    746 	    NULL, sc->sc_dev.dv_xname, "linkintr");
    747 
    748 	evcnt_attach_dynamic(&sc->sc_ev_rxipsum, EVCNT_TYPE_MISC,
    749 	    NULL, sc->sc_dev.dv_xname, "rxipsum");
    750 	evcnt_attach_dynamic(&sc->sc_ev_rxtusum, EVCNT_TYPE_MISC,
    751 	    NULL, sc->sc_dev.dv_xname, "rxtusum");
    752 	evcnt_attach_dynamic(&sc->sc_ev_txipsum, EVCNT_TYPE_MISC,
    753 	    NULL, sc->sc_dev.dv_xname, "txipsum");
    754 	evcnt_attach_dynamic(&sc->sc_ev_txtusum, EVCNT_TYPE_MISC,
    755 	    NULL, sc->sc_dev.dv_xname, "txtusum");
    756 
    757 	evcnt_attach_dynamic(&sc->sc_ev_txctx_init, EVCNT_TYPE_MISC,
    758 	    NULL, sc->sc_dev.dv_xname, "txctx init");
    759 	evcnt_attach_dynamic(&sc->sc_ev_txctx_hit, EVCNT_TYPE_MISC,
    760 	    NULL, sc->sc_dev.dv_xname, "txctx hit");
    761 	evcnt_attach_dynamic(&sc->sc_ev_txctx_miss, EVCNT_TYPE_MISC,
    762 	    NULL, sc->sc_dev.dv_xname, "txctx miss");
    763 
    764 	for (i = 0; i < DGE_NTXSEGS; i++)
    765 		evcnt_attach_dynamic(&sc->sc_ev_txseg[i], EVCNT_TYPE_MISC,
    766 		    NULL, sc->sc_dev.dv_xname, (*dge_txseg_evcnt_names)[i]);
    767 
    768 	evcnt_attach_dynamic(&sc->sc_ev_txdrop, EVCNT_TYPE_MISC,
    769 	    NULL, sc->sc_dev.dv_xname, "txdrop");
    770 
    771 #endif /* DGE_EVENT_COUNTERS */
    772 
    773 	/*
    774 	 * Make sure the interface is shutdown during reboot.
    775 	 */
    776 	sc->sc_sdhook = shutdownhook_establish(dge_shutdown, sc);
    777 	if (sc->sc_sdhook == NULL)
    778 		aprint_error("%s: WARNING: unable to establish shutdown hook\n",
    779 		    sc->sc_dev.dv_xname);
    780 	return;
    781 
    782 	/*
    783 	 * Free any resources we've allocated during the failed attach
    784 	 * attempt.  Do this in reverse order and fall through.
    785 	 */
    786  fail_5:
    787 	for (i = 0; i < DGE_NRXDESC; i++) {
    788 		if (sc->sc_rxsoft[i].rxs_dmamap != NULL)
    789 			bus_dmamap_destroy(sc->sc_dmat,
    790 			    sc->sc_rxsoft[i].rxs_dmamap);
    791 	}
    792  fail_4:
    793 	for (i = 0; i < DGE_TXQUEUELEN; i++) {
    794 		if (sc->sc_txsoft[i].txs_dmamap != NULL)
    795 			bus_dmamap_destroy(sc->sc_dmat,
    796 			    sc->sc_txsoft[i].txs_dmamap);
    797 	}
    798 	bus_dmamap_unload(sc->sc_dmat, sc->sc_cddmamap);
    799  fail_3:
    800 	bus_dmamap_destroy(sc->sc_dmat, sc->sc_cddmamap);
    801  fail_2:
    802 	bus_dmamem_unmap(sc->sc_dmat, (caddr_t)sc->sc_control_data,
    803 	    sizeof(struct dge_control_data));
    804  fail_1:
    805 	bus_dmamem_free(sc->sc_dmat, &seg, rseg);
    806  fail_0:
    807 	return;
    808 }
    809 
    810 /*
    811  * dge_shutdown:
    812  *
    813  *	Make sure the interface is stopped at reboot time.
    814  */
    815 static void
    816 dge_shutdown(void *arg)
    817 {
    818 	struct dge_softc *sc = arg;
    819 
    820 	dge_stop(&sc->sc_ethercom.ec_if, 1);
    821 }
    822 
    823 /*
    824  * dge_tx_cksum:
    825  *
    826  *	Set up TCP/IP checksumming parameters for the
    827  *	specified packet.
    828  */
    829 static int
    830 dge_tx_cksum(struct dge_softc *sc, struct dge_txsoft *txs, uint8_t *fieldsp)
    831 {
    832 	struct mbuf *m0 = txs->txs_mbuf;
    833 	struct dge_ctdes *t;
    834 	uint32_t ipcs, tucs;
    835 	struct ip *ip;
    836 	struct ether_header *eh;
    837 	int offset, iphl;
    838 	uint8_t fields = 0;
    839 
    840 	/*
    841 	 * XXX It would be nice if the mbuf pkthdr had offset
    842 	 * fields for the protocol headers.
    843 	 */
    844 
    845 	eh = mtod(m0, struct ether_header *);
    846 	switch (htons(eh->ether_type)) {
    847 	case ETHERTYPE_IP:
    848 		iphl = sizeof(struct ip);
    849 		offset = ETHER_HDR_LEN;
    850 		break;
    851 
    852 	case ETHERTYPE_VLAN:
    853 		iphl = sizeof(struct ip);
    854 		offset = ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN;
    855 		break;
    856 
    857 	default:
    858 		/*
    859 		 * Don't support this protocol or encapsulation.
    860 		 */
    861 		*fieldsp = 0;
    862 		return (0);
    863 	}
    864 
    865 	if (m0->m_len < (offset + iphl)) {
    866 		if ((txs->txs_mbuf = m_pullup(m0, offset + iphl)) == NULL) {
    867 			printf("%s: dge_tx_cksum: mbuf allocation failed, "
    868 			    "packet dropped\n", sc->sc_dev.dv_xname);
    869 			return (ENOMEM);
    870 		}
    871 		m0 = txs->txs_mbuf;
    872 	}
    873 
    874 	ip = (struct ip *) (mtod(m0, caddr_t) + offset);
    875 	iphl = ip->ip_hl << 2;
    876 
    877 	/*
    878 	 * NOTE: Even if we're not using the IP or TCP/UDP checksum
    879 	 * offload feature, if we load the context descriptor, we
    880 	 * MUST provide valid values for IPCSS and TUCSS fields.
    881 	 */
    882 
    883 	if (m0->m_pkthdr.csum_flags & M_CSUM_IPv4) {
    884 		DGE_EVCNT_INCR(&sc->sc_ev_txipsum);
    885 		fields |= TDESC_POPTS_IXSM;
    886 		ipcs = DGE_TCPIP_IPCSS(offset) |
    887 		    DGE_TCPIP_IPCSO(offset + offsetof(struct ip, ip_sum)) |
    888 		    DGE_TCPIP_IPCSE(offset + iphl - 1);
    889 	} else if (__predict_true(sc->sc_txctx_ipcs != 0xffffffff)) {
    890 		/* Use the cached value. */
    891 		ipcs = sc->sc_txctx_ipcs;
    892 	} else {
    893 		/* Just initialize it to the likely value anyway. */
    894 		ipcs = DGE_TCPIP_IPCSS(offset) |
    895 		    DGE_TCPIP_IPCSO(offset + offsetof(struct ip, ip_sum)) |
    896 		    DGE_TCPIP_IPCSE(offset + iphl - 1);
    897 	}
    898 	DPRINTF(DGE_DEBUG_CKSUM,
    899 	    ("%s: CKSUM: offset %d ipcs 0x%x\n",
    900 	    sc->sc_dev.dv_xname, offset, ipcs));
    901 
    902 	offset += iphl;
    903 
    904 	if (m0->m_pkthdr.csum_flags & (M_CSUM_TCPv4|M_CSUM_UDPv4)) {
    905 		DGE_EVCNT_INCR(&sc->sc_ev_txtusum);
    906 		fields |= TDESC_POPTS_TXSM;
    907 		tucs = DGE_TCPIP_TUCSS(offset) |
    908 		    DGE_TCPIP_TUCSO(offset + m0->m_pkthdr.csum_data) |
    909 		    DGE_TCPIP_TUCSE(0) /* rest of packet */;
    910 	} else if (__predict_true(sc->sc_txctx_tucs != 0xffffffff)) {
    911 		/* Use the cached value. */
    912 		tucs = sc->sc_txctx_tucs;
    913 	} else {
    914 		/* Just initialize it to a valid TCP context. */
    915 		tucs = DGE_TCPIP_TUCSS(offset) |
    916 		    DGE_TCPIP_TUCSO(offset + offsetof(struct tcphdr, th_sum)) |
    917 		    DGE_TCPIP_TUCSE(0) /* rest of packet */;
    918 	}
    919 
    920 	DPRINTF(DGE_DEBUG_CKSUM,
    921 	    ("%s: CKSUM: offset %d tucs 0x%x\n",
    922 	    sc->sc_dev.dv_xname, offset, tucs));
    923 
    924 	if (sc->sc_txctx_ipcs == ipcs &&
    925 	    sc->sc_txctx_tucs == tucs) {
    926 		/* Cached context is fine. */
    927 		DGE_EVCNT_INCR(&sc->sc_ev_txctx_hit);
    928 	} else {
    929 		/* Fill in the context descriptor. */
    930 #ifdef DGE_EVENT_COUNTERS
    931 		if (sc->sc_txctx_ipcs == 0xffffffff &&
    932 		    sc->sc_txctx_tucs == 0xffffffff)
    933 			DGE_EVCNT_INCR(&sc->sc_ev_txctx_init);
    934 		else
    935 			DGE_EVCNT_INCR(&sc->sc_ev_txctx_miss);
    936 #endif
    937 		t = (struct dge_ctdes *)&sc->sc_txdescs[sc->sc_txnext];
    938 		t->dc_tcpip_ipcs = htole32(ipcs);
    939 		t->dc_tcpip_tucs = htole32(tucs);
    940 		t->dc_tcpip_cmdlen = htole32(TDESC_DTYP_CTD);
    941 		t->dc_tcpip_seg = 0;
    942 		DGE_CDTXSYNC(sc, sc->sc_txnext, 1, BUS_DMASYNC_PREWRITE);
    943 
    944 		sc->sc_txctx_ipcs = ipcs;
    945 		sc->sc_txctx_tucs = tucs;
    946 
    947 		sc->sc_txnext = DGE_NEXTTX(sc->sc_txnext);
    948 		txs->txs_ndesc++;
    949 	}
    950 
    951 	*fieldsp = fields;
    952 
    953 	return (0);
    954 }
    955 
    956 /*
    957  * dge_start:		[ifnet interface function]
    958  *
    959  *	Start packet transmission on the interface.
    960  */
    961 static void
    962 dge_start(struct ifnet *ifp)
    963 {
    964 	struct dge_softc *sc = ifp->if_softc;
    965 	struct mbuf *m0;
    966 	struct dge_txsoft *txs;
    967 	bus_dmamap_t dmamap;
    968 	int error, nexttx, lasttx = -1, ofree, seg;
    969 	uint32_t cksumcmd;
    970 	uint8_t cksumfields;
    971 
    972 	if ((ifp->if_flags & (IFF_RUNNING|IFF_OACTIVE)) != IFF_RUNNING)
    973 		return;
    974 
    975 	/*
    976 	 * Remember the previous number of free descriptors.
    977 	 */
    978 	ofree = sc->sc_txfree;
    979 
    980 	/*
    981 	 * Loop through the send queue, setting up transmit descriptors
    982 	 * until we drain the queue, or use up all available transmit
    983 	 * descriptors.
    984 	 */
    985 	for (;;) {
    986 		/* Grab a packet off the queue. */
    987 		IFQ_POLL(&ifp->if_snd, m0);
    988 		if (m0 == NULL)
    989 			break;
    990 
    991 		DPRINTF(DGE_DEBUG_TX,
    992 		    ("%s: TX: have packet to transmit: %p\n",
    993 		    sc->sc_dev.dv_xname, m0));
    994 
    995 		/* Get a work queue entry. */
    996 		if (sc->sc_txsfree < DGE_TXQUEUE_GC) {
    997 			dge_txintr(sc);
    998 			if (sc->sc_txsfree == 0) {
    999 				DPRINTF(DGE_DEBUG_TX,
   1000 				    ("%s: TX: no free job descriptors\n",
   1001 					sc->sc_dev.dv_xname));
   1002 				DGE_EVCNT_INCR(&sc->sc_ev_txsstall);
   1003 				break;
   1004 			}
   1005 		}
   1006 
   1007 		txs = &sc->sc_txsoft[sc->sc_txsnext];
   1008 		dmamap = txs->txs_dmamap;
   1009 
   1010 		/*
   1011 		 * Load the DMA map.  If this fails, the packet either
   1012 		 * didn't fit in the allotted number of segments, or we
   1013 		 * were short on resources.  For the too-many-segments
   1014 		 * case, we simply report an error and drop the packet,
   1015 		 * since we can't sanely copy a jumbo packet to a single
   1016 		 * buffer.
   1017 		 */
   1018 		error = bus_dmamap_load_mbuf(sc->sc_dmat, dmamap, m0,
   1019 		    BUS_DMA_WRITE|BUS_DMA_NOWAIT);
   1020 		if (error) {
   1021 			if (error == EFBIG) {
   1022 				DGE_EVCNT_INCR(&sc->sc_ev_txdrop);
   1023 				printf("%s: Tx packet consumes too many "
   1024 				    "DMA segments, dropping...\n",
   1025 				    sc->sc_dev.dv_xname);
   1026 				IFQ_DEQUEUE(&ifp->if_snd, m0);
   1027 				m_freem(m0);
   1028 				continue;
   1029 			}
   1030 			/*
   1031 			 * Short on resources, just stop for now.
   1032 			 */
   1033 			DPRINTF(DGE_DEBUG_TX,
   1034 			    ("%s: TX: dmamap load failed: %d\n",
   1035 			    sc->sc_dev.dv_xname, error));
   1036 			break;
   1037 		}
   1038 
   1039 		/*
   1040 		 * Ensure we have enough descriptors free to describe
   1041 		 * the packet.  Note, we always reserve one descriptor
   1042 		 * at the end of the ring due to the semantics of the
   1043 		 * TDT register, plus one more in the event we need
   1044 		 * to re-load checksum offload context.
   1045 		 */
   1046 		if (dmamap->dm_nsegs > (sc->sc_txfree - 2)) {
   1047 			/*
   1048 			 * Not enough free descriptors to transmit this
   1049 			 * packet.  We haven't committed anything yet,
   1050 			 * so just unload the DMA map, put the packet
   1051 			 * pack on the queue, and punt.  Notify the upper
   1052 			 * layer that there are no more slots left.
   1053 			 */
   1054 			DPRINTF(DGE_DEBUG_TX,
   1055 			    ("%s: TX: need %d descriptors, have %d\n",
   1056 			    sc->sc_dev.dv_xname, dmamap->dm_nsegs,
   1057 			    sc->sc_txfree - 1));
   1058 			ifp->if_flags |= IFF_OACTIVE;
   1059 			bus_dmamap_unload(sc->sc_dmat, dmamap);
   1060 			DGE_EVCNT_INCR(&sc->sc_ev_txdstall);
   1061 			break;
   1062 		}
   1063 
   1064 		IFQ_DEQUEUE(&ifp->if_snd, m0);
   1065 
   1066 		/*
   1067 		 * WE ARE NOW COMMITTED TO TRANSMITTING THE PACKET.
   1068 		 */
   1069 
   1070 		/* Sync the DMA map. */
   1071 		bus_dmamap_sync(sc->sc_dmat, dmamap, 0, dmamap->dm_mapsize,
   1072 		    BUS_DMASYNC_PREWRITE);
   1073 
   1074 		DPRINTF(DGE_DEBUG_TX,
   1075 		    ("%s: TX: packet has %d DMA segments\n",
   1076 		    sc->sc_dev.dv_xname, dmamap->dm_nsegs));
   1077 
   1078 		DGE_EVCNT_INCR(&sc->sc_ev_txseg[dmamap->dm_nsegs - 1]);
   1079 
   1080 		/*
   1081 		 * Store a pointer to the packet so that we can free it
   1082 		 * later.
   1083 		 *
   1084 		 * Initially, we consider the number of descriptors the
   1085 		 * packet uses the number of DMA segments.  This may be
   1086 		 * incremented by 1 if we do checksum offload (a descriptor
   1087 		 * is used to set the checksum context).
   1088 		 */
   1089 		txs->txs_mbuf = m0;
   1090 		txs->txs_firstdesc = sc->sc_txnext;
   1091 		txs->txs_ndesc = dmamap->dm_nsegs;
   1092 
   1093 		/*
   1094 		 * Set up checksum offload parameters for
   1095 		 * this packet.
   1096 		 */
   1097 		if (m0->m_pkthdr.csum_flags &
   1098 		    (M_CSUM_IPv4|M_CSUM_TCPv4|M_CSUM_UDPv4)) {
   1099 			if (dge_tx_cksum(sc, txs, &cksumfields) != 0) {
   1100 				/* Error message already displayed. */
   1101 				bus_dmamap_unload(sc->sc_dmat, dmamap);
   1102 				continue;
   1103 			}
   1104 		} else {
   1105 			cksumfields = 0;
   1106 		}
   1107 
   1108 		cksumcmd = TDESC_DCMD_IDE | TDESC_DTYP_DATA;
   1109 
   1110 		/*
   1111 		 * Initialize the transmit descriptor.
   1112 		 */
   1113 		for (nexttx = sc->sc_txnext, seg = 0;
   1114 		     seg < dmamap->dm_nsegs;
   1115 		     seg++, nexttx = DGE_NEXTTX(nexttx)) {
   1116 			/*
   1117 			 * Note: we currently only use 32-bit DMA
   1118 			 * addresses.
   1119 			 */
   1120 			sc->sc_txdescs[nexttx].dt_baddrh = 0;
   1121 			sc->sc_txdescs[nexttx].dt_baddrl =
   1122 			    htole32(dmamap->dm_segs[seg].ds_addr);
   1123 			sc->sc_txdescs[nexttx].dt_ctl =
   1124 			    htole32(cksumcmd | dmamap->dm_segs[seg].ds_len);
   1125 			sc->sc_txdescs[nexttx].dt_status = 0;
   1126 			sc->sc_txdescs[nexttx].dt_popts = cksumfields;
   1127 			sc->sc_txdescs[nexttx].dt_vlan = 0;
   1128 			lasttx = nexttx;
   1129 
   1130 			DPRINTF(DGE_DEBUG_TX,
   1131 			    ("%s: TX: desc %d: low 0x%08lx, len 0x%04lx\n",
   1132 			    sc->sc_dev.dv_xname, nexttx,
   1133 			    le32toh(dmamap->dm_segs[seg].ds_addr),
   1134 			    le32toh(dmamap->dm_segs[seg].ds_len)));
   1135 		}
   1136 
   1137 		KASSERT(lasttx != -1);
   1138 
   1139 		/*
   1140 		 * Set up the command byte on the last descriptor of
   1141 		 * the packet.  If we're in the interrupt delay window,
   1142 		 * delay the interrupt.
   1143 		 */
   1144 		sc->sc_txdescs[lasttx].dt_ctl |=
   1145 		    htole32(TDESC_DCMD_EOP | TDESC_DCMD_RS);
   1146 
   1147 		txs->txs_lastdesc = lasttx;
   1148 
   1149 		DPRINTF(DGE_DEBUG_TX,
   1150 		    ("%s: TX: desc %d: cmdlen 0x%08x\n", sc->sc_dev.dv_xname,
   1151 		    lasttx, le32toh(sc->sc_txdescs[lasttx].dt_ctl)));
   1152 
   1153 		/* Sync the descriptors we're using. */
   1154 		DGE_CDTXSYNC(sc, sc->sc_txnext, dmamap->dm_nsegs,
   1155 		    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
   1156 
   1157 		/* Give the packet to the chip. */
   1158 		CSR_WRITE(sc, DGE_TDT, nexttx);
   1159 
   1160 		DPRINTF(DGE_DEBUG_TX,
   1161 		    ("%s: TX: TDT -> %d\n", sc->sc_dev.dv_xname, nexttx));
   1162 
   1163 		DPRINTF(DGE_DEBUG_TX,
   1164 		    ("%s: TX: finished transmitting packet, job %d\n",
   1165 		    sc->sc_dev.dv_xname, sc->sc_txsnext));
   1166 
   1167 		/* Advance the tx pointer. */
   1168 		sc->sc_txfree -= txs->txs_ndesc;
   1169 		sc->sc_txnext = nexttx;
   1170 
   1171 		sc->sc_txsfree--;
   1172 		sc->sc_txsnext = DGE_NEXTTXS(sc->sc_txsnext);
   1173 
   1174 #if NBPFILTER > 0
   1175 		/* Pass the packet to any BPF listeners. */
   1176 		if (ifp->if_bpf)
   1177 			bpf_mtap(ifp->if_bpf, m0);
   1178 #endif /* NBPFILTER > 0 */
   1179 	}
   1180 
   1181 	if (sc->sc_txsfree == 0 || sc->sc_txfree <= 2) {
   1182 		/* No more slots; notify upper layer. */
   1183 		ifp->if_flags |= IFF_OACTIVE;
   1184 	}
   1185 
   1186 	if (sc->sc_txfree != ofree) {
   1187 		/* Set a watchdog timer in case the chip flakes out. */
   1188 		ifp->if_timer = 5;
   1189 	}
   1190 }
   1191 
   1192 /*
   1193  * dge_watchdog:		[ifnet interface function]
   1194  *
   1195  *	Watchdog timer handler.
   1196  */
   1197 static void
   1198 dge_watchdog(struct ifnet *ifp)
   1199 {
   1200 	struct dge_softc *sc = ifp->if_softc;
   1201 
   1202 	/*
   1203 	 * Since we're using delayed interrupts, sweep up
   1204 	 * before we report an error.
   1205 	 */
   1206 	dge_txintr(sc);
   1207 
   1208 	if (sc->sc_txfree != DGE_NTXDESC) {
   1209 		printf("%s: device timeout (txfree %d txsfree %d txnext %d)\n",
   1210 		    sc->sc_dev.dv_xname, sc->sc_txfree, sc->sc_txsfree,
   1211 		    sc->sc_txnext);
   1212 		ifp->if_oerrors++;
   1213 
   1214 		/* Reset the interface. */
   1215 		(void) dge_init(ifp);
   1216 	}
   1217 
   1218 	/* Try to get more packets going. */
   1219 	dge_start(ifp);
   1220 }
   1221 
   1222 /*
   1223  * dge_ioctl:		[ifnet interface function]
   1224  *
   1225  *	Handle control requests from the operator.
   1226  */
   1227 static int
   1228 dge_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
   1229 {
   1230 	struct dge_softc *sc = ifp->if_softc;
   1231 	struct ifreq *ifr = (struct ifreq *) data;
   1232 	pcireg_t preg;
   1233 	int s, error, mmrbc;
   1234 
   1235 	s = splnet();
   1236 
   1237 	switch (cmd) {
   1238 	case SIOCSIFMEDIA:
   1239 	case SIOCGIFMEDIA:
   1240 		error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
   1241 		break;
   1242 
   1243 	case SIOCSIFMTU:
   1244 		if (ifr->ifr_mtu < ETHERMIN || ifr->ifr_mtu > DGE_MAX_MTU) {
   1245 			error = EINVAL;
   1246 		} else {
   1247 			error = 0;
   1248 			ifp->if_mtu = ifr->ifr_mtu;
   1249 			if (ifp->if_flags & IFF_UP)
   1250 				error = (*ifp->if_init)(ifp);
   1251 		}
   1252 		break;
   1253 
   1254         case SIOCSIFFLAGS:
   1255 		/* extract link flags */
   1256 		if ((ifp->if_flags & IFF_LINK0) == 0 &&
   1257 		    (ifp->if_flags & IFF_LINK1) == 0)
   1258 			mmrbc = PCIX_MMRBC_512;
   1259 		else if ((ifp->if_flags & IFF_LINK0) == 0 &&
   1260 		    (ifp->if_flags & IFF_LINK1) != 0)
   1261 			mmrbc = PCIX_MMRBC_1024;
   1262 		else if ((ifp->if_flags & IFF_LINK0) != 0 &&
   1263 		    (ifp->if_flags & IFF_LINK1) == 0)
   1264 			mmrbc = PCIX_MMRBC_2048;
   1265 		else
   1266 			mmrbc = PCIX_MMRBC_4096;
   1267 		if (mmrbc != sc->sc_mmrbc) {
   1268 			preg = pci_conf_read(sc->sc_pc, sc->sc_pt,DGE_PCIX_CMD);
   1269 			preg &= ~PCIX_MMRBC_MSK;
   1270 			preg |= mmrbc;
   1271 			pci_conf_write(sc->sc_pc, sc->sc_pt,DGE_PCIX_CMD, preg);
   1272 			sc->sc_mmrbc = mmrbc;
   1273 		}
   1274                 /* FALLTHROUGH */
   1275 	default:
   1276 		error = ether_ioctl(ifp, cmd, data);
   1277 		if (error == ENETRESET) {
   1278 			/*
   1279 			 * Multicast list has changed; set the hardware filter
   1280 			 * accordingly.
   1281 			 */
   1282 			dge_set_filter(sc);
   1283 			error = 0;
   1284 		}
   1285 		break;
   1286 	}
   1287 
   1288 	/* Try to get more packets going. */
   1289 	dge_start(ifp);
   1290 
   1291 	splx(s);
   1292 	return (error);
   1293 }
   1294 
   1295 /*
   1296  * dge_intr:
   1297  *
   1298  *	Interrupt service routine.
   1299  */
   1300 static int
   1301 dge_intr(void *arg)
   1302 {
   1303 	struct dge_softc *sc = arg;
   1304 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1305 	uint32_t icr;
   1306 	int wantinit, handled = 0;
   1307 
   1308 	for (wantinit = 0; wantinit == 0;) {
   1309 		icr = CSR_READ(sc, DGE_ICR);
   1310 		if ((icr & sc->sc_icr) == 0)
   1311 			break;
   1312 
   1313 #if 0 /*NRND > 0*/
   1314 		if (RND_ENABLED(&sc->rnd_source))
   1315 			rnd_add_uint32(&sc->rnd_source, icr);
   1316 #endif
   1317 
   1318 		handled = 1;
   1319 
   1320 #if defined(DGE_DEBUG) || defined(DGE_EVENT_COUNTERS)
   1321 		if (icr & (ICR_RXDMT0|ICR_RXT0)) {
   1322 			DPRINTF(DGE_DEBUG_RX,
   1323 			    ("%s: RX: got Rx intr 0x%08x\n",
   1324 			    sc->sc_dev.dv_xname,
   1325 			    icr & (ICR_RXDMT0|ICR_RXT0)));
   1326 			DGE_EVCNT_INCR(&sc->sc_ev_rxintr);
   1327 		}
   1328 #endif
   1329 		dge_rxintr(sc);
   1330 
   1331 #if defined(DGE_DEBUG) || defined(DGE_EVENT_COUNTERS)
   1332 		if (icr & ICR_TXDW) {
   1333 			DPRINTF(DGE_DEBUG_TX,
   1334 			    ("%s: TX: got TXDW interrupt\n",
   1335 			    sc->sc_dev.dv_xname));
   1336 			DGE_EVCNT_INCR(&sc->sc_ev_txdw);
   1337 		}
   1338 		if (icr & ICR_TXQE)
   1339 			DGE_EVCNT_INCR(&sc->sc_ev_txqe);
   1340 #endif
   1341 		dge_txintr(sc);
   1342 
   1343 		if (icr & (ICR_LSC|ICR_RXSEQ)) {
   1344 			DGE_EVCNT_INCR(&sc->sc_ev_linkintr);
   1345 			dge_linkintr(sc, icr);
   1346 		}
   1347 
   1348 		if (icr & ICR_RXO) {
   1349 			printf("%s: Receive overrun\n", sc->sc_dev.dv_xname);
   1350 			wantinit = 1;
   1351 		}
   1352 	}
   1353 
   1354 	if (handled) {
   1355 		if (wantinit)
   1356 			dge_init(ifp);
   1357 
   1358 		/* Try to get more packets going. */
   1359 		dge_start(ifp);
   1360 	}
   1361 
   1362 	return (handled);
   1363 }
   1364 
   1365 /*
   1366  * dge_txintr:
   1367  *
   1368  *	Helper; handle transmit interrupts.
   1369  */
   1370 static void
   1371 dge_txintr(struct dge_softc *sc)
   1372 {
   1373 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1374 	struct dge_txsoft *txs;
   1375 	uint8_t status;
   1376 	int i;
   1377 
   1378 	ifp->if_flags &= ~IFF_OACTIVE;
   1379 
   1380 	/*
   1381 	 * Go through the Tx list and free mbufs for those
   1382 	 * frames which have been transmitted.
   1383 	 */
   1384 	for (i = sc->sc_txsdirty; sc->sc_txsfree != DGE_TXQUEUELEN;
   1385 	     i = DGE_NEXTTXS(i), sc->sc_txsfree++) {
   1386 		txs = &sc->sc_txsoft[i];
   1387 
   1388 		DPRINTF(DGE_DEBUG_TX,
   1389 		    ("%s: TX: checking job %d\n", sc->sc_dev.dv_xname, i));
   1390 
   1391 		DGE_CDTXSYNC(sc, txs->txs_firstdesc, txs->txs_dmamap->dm_nsegs,
   1392 		    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
   1393 
   1394 		status =
   1395 		    sc->sc_txdescs[txs->txs_lastdesc].dt_status;
   1396 		if ((status & TDESC_STA_DD) == 0) {
   1397 			DGE_CDTXSYNC(sc, txs->txs_lastdesc, 1,
   1398 			    BUS_DMASYNC_PREREAD);
   1399 			break;
   1400 		}
   1401 
   1402 		DPRINTF(DGE_DEBUG_TX,
   1403 		    ("%s: TX: job %d done: descs %d..%d\n",
   1404 		    sc->sc_dev.dv_xname, i, txs->txs_firstdesc,
   1405 		    txs->txs_lastdesc));
   1406 
   1407 		ifp->if_opackets++;
   1408 		sc->sc_txfree += txs->txs_ndesc;
   1409 		bus_dmamap_sync(sc->sc_dmat, txs->txs_dmamap,
   1410 		    0, txs->txs_dmamap->dm_mapsize, BUS_DMASYNC_POSTWRITE);
   1411 		bus_dmamap_unload(sc->sc_dmat, txs->txs_dmamap);
   1412 		m_freem(txs->txs_mbuf);
   1413 		txs->txs_mbuf = NULL;
   1414 	}
   1415 
   1416 	/* Update the dirty transmit buffer pointer. */
   1417 	sc->sc_txsdirty = i;
   1418 	DPRINTF(DGE_DEBUG_TX,
   1419 	    ("%s: TX: txsdirty -> %d\n", sc->sc_dev.dv_xname, i));
   1420 
   1421 	/*
   1422 	 * If there are no more pending transmissions, cancel the watchdog
   1423 	 * timer.
   1424 	 */
   1425 	if (sc->sc_txsfree == DGE_TXQUEUELEN)
   1426 		ifp->if_timer = 0;
   1427 }
   1428 
   1429 /*
   1430  * dge_rxintr:
   1431  *
   1432  *	Helper; handle receive interrupts.
   1433  */
   1434 static void
   1435 dge_rxintr(struct dge_softc *sc)
   1436 {
   1437 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1438 	struct dge_rxsoft *rxs;
   1439 	struct mbuf *m;
   1440 	int i, len;
   1441 	uint8_t status, errors;
   1442 
   1443 	for (i = sc->sc_rxptr;; i = DGE_NEXTRX(i)) {
   1444 		rxs = &sc->sc_rxsoft[i];
   1445 
   1446 		DPRINTF(DGE_DEBUG_RX,
   1447 		    ("%s: RX: checking descriptor %d\n",
   1448 		    sc->sc_dev.dv_xname, i));
   1449 
   1450 		DGE_CDRXSYNC(sc, i, BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
   1451 
   1452 		status = sc->sc_rxdescs[i].dr_status;
   1453 		errors = sc->sc_rxdescs[i].dr_errors;
   1454 		len = le16toh(sc->sc_rxdescs[i].dr_len);
   1455 
   1456 		if ((status & RDESC_STS_DD) == 0) {
   1457 			/*
   1458 			 * We have processed all of the receive descriptors.
   1459 			 */
   1460 			DGE_CDRXSYNC(sc, i, BUS_DMASYNC_PREREAD);
   1461 			break;
   1462 		}
   1463 
   1464 		if (__predict_false(sc->sc_rxdiscard)) {
   1465 			DPRINTF(DGE_DEBUG_RX,
   1466 			    ("%s: RX: discarding contents of descriptor %d\n",
   1467 			    sc->sc_dev.dv_xname, i));
   1468 			DGE_INIT_RXDESC(sc, i);
   1469 			if (status & RDESC_STS_EOP) {
   1470 				/* Reset our state. */
   1471 				DPRINTF(DGE_DEBUG_RX,
   1472 				    ("%s: RX: resetting rxdiscard -> 0\n",
   1473 				    sc->sc_dev.dv_xname));
   1474 				sc->sc_rxdiscard = 0;
   1475 			}
   1476 			continue;
   1477 		}
   1478 
   1479 		bus_dmamap_sync(sc->sc_dmat, rxs->rxs_dmamap, 0,
   1480 		    rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_POSTREAD);
   1481 
   1482 		m = rxs->rxs_mbuf;
   1483 
   1484 		/*
   1485 		 * Add a new receive buffer to the ring.
   1486 		 */
   1487 		if (dge_add_rxbuf(sc, i) != 0) {
   1488 			/*
   1489 			 * Failed, throw away what we've done so
   1490 			 * far, and discard the rest of the packet.
   1491 			 */
   1492 			ifp->if_ierrors++;
   1493 			bus_dmamap_sync(sc->sc_dmat, rxs->rxs_dmamap, 0,
   1494 			    rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
   1495 			DGE_INIT_RXDESC(sc, i);
   1496 			if ((status & RDESC_STS_EOP) == 0)
   1497 				sc->sc_rxdiscard = 1;
   1498 			if (sc->sc_rxhead != NULL)
   1499 				m_freem(sc->sc_rxhead);
   1500 			DGE_RXCHAIN_RESET(sc);
   1501 			DPRINTF(DGE_DEBUG_RX,
   1502 			    ("%s: RX: Rx buffer allocation failed, "
   1503 			    "dropping packet%s\n", sc->sc_dev.dv_xname,
   1504 			    sc->sc_rxdiscard ? " (discard)" : ""));
   1505 			continue;
   1506 		}
   1507 		DGE_INIT_RXDESC(sc, DGE_PREVRX(i)); /* Write the descriptor */
   1508 
   1509 		DGE_RXCHAIN_LINK(sc, m);
   1510 
   1511 		m->m_len = len;
   1512 
   1513 		DPRINTF(DGE_DEBUG_RX,
   1514 		    ("%s: RX: buffer at %p len %d\n",
   1515 		    sc->sc_dev.dv_xname, m->m_data, len));
   1516 
   1517 		/*
   1518 		 * If this is not the end of the packet, keep
   1519 		 * looking.
   1520 		 */
   1521 		if ((status & RDESC_STS_EOP) == 0) {
   1522 			sc->sc_rxlen += len;
   1523 			DPRINTF(DGE_DEBUG_RX,
   1524 			    ("%s: RX: not yet EOP, rxlen -> %d\n",
   1525 			    sc->sc_dev.dv_xname, sc->sc_rxlen));
   1526 			continue;
   1527 		}
   1528 
   1529 		/*
   1530 		 * Okay, we have the entire packet now...
   1531 		 */
   1532 		*sc->sc_rxtailp = NULL;
   1533 		m = sc->sc_rxhead;
   1534 		len += sc->sc_rxlen;
   1535 
   1536 		DGE_RXCHAIN_RESET(sc);
   1537 
   1538 		DPRINTF(DGE_DEBUG_RX,
   1539 		    ("%s: RX: have entire packet, len -> %d\n",
   1540 		    sc->sc_dev.dv_xname, len));
   1541 
   1542 		/*
   1543 		 * If an error occurred, update stats and drop the packet.
   1544 		 */
   1545 		if (errors &
   1546 		     (RDESC_ERR_CE|RDESC_ERR_SE|RDESC_ERR_P|RDESC_ERR_RXE)) {
   1547 			ifp->if_ierrors++;
   1548 			if (errors & RDESC_ERR_SE)
   1549 				printf("%s: symbol error\n",
   1550 				    sc->sc_dev.dv_xname);
   1551 			else if (errors & RDESC_ERR_P)
   1552 				printf("%s: parity error\n",
   1553 				    sc->sc_dev.dv_xname);
   1554 			else if (errors & RDESC_ERR_CE)
   1555 				printf("%s: CRC error\n",
   1556 				    sc->sc_dev.dv_xname);
   1557 			m_freem(m);
   1558 			continue;
   1559 		}
   1560 
   1561 		/*
   1562 		 * No errors.  Receive the packet.
   1563 		 */
   1564 		m->m_pkthdr.rcvif = ifp;
   1565 		m->m_pkthdr.len = len;
   1566 
   1567 		/*
   1568 		 * Set up checksum info for this packet.
   1569 		 */
   1570 		if (status & RDESC_STS_IPCS) {
   1571 			DGE_EVCNT_INCR(&sc->sc_ev_rxipsum);
   1572 			m->m_pkthdr.csum_flags |= M_CSUM_IPv4;
   1573 			if (errors & RDESC_ERR_IPE)
   1574 				m->m_pkthdr.csum_flags |= M_CSUM_IPv4_BAD;
   1575 		}
   1576 		if (status & RDESC_STS_TCPCS) {
   1577 			/*
   1578 			 * Note: we don't know if this was TCP or UDP,
   1579 			 * so we just set both bits, and expect the
   1580 			 * upper layers to deal.
   1581 			 */
   1582 			DGE_EVCNT_INCR(&sc->sc_ev_rxtusum);
   1583 			m->m_pkthdr.csum_flags |= M_CSUM_TCPv4|M_CSUM_UDPv4;
   1584 			if (errors & RDESC_ERR_TCPE)
   1585 				m->m_pkthdr.csum_flags |= M_CSUM_TCP_UDP_BAD;
   1586 		}
   1587 
   1588 		ifp->if_ipackets++;
   1589 
   1590 #if NBPFILTER > 0
   1591 		/* Pass this up to any BPF listeners. */
   1592 		if (ifp->if_bpf)
   1593 			bpf_mtap(ifp->if_bpf, m);
   1594 #endif /* NBPFILTER > 0 */
   1595 
   1596 		/* Pass it on. */
   1597 		(*ifp->if_input)(ifp, m);
   1598 	}
   1599 
   1600 	/* Update the receive pointer. */
   1601 	sc->sc_rxptr = i;
   1602 
   1603 	DPRINTF(DGE_DEBUG_RX,
   1604 	    ("%s: RX: rxptr -> %d\n", sc->sc_dev.dv_xname, i));
   1605 }
   1606 
   1607 /*
   1608  * dge_linkintr:
   1609  *
   1610  *	Helper; handle link interrupts.
   1611  */
   1612 static void
   1613 dge_linkintr(struct dge_softc *sc, uint32_t icr)
   1614 {
   1615 	uint32_t status;
   1616 
   1617 	if (icr & ICR_LSC) {
   1618 		status = CSR_READ(sc, DGE_STATUS);
   1619 		if (status & STATUS_LINKUP) {
   1620 			DPRINTF(DGE_DEBUG_LINK, ("%s: LINK: LSC -> up\n",
   1621 			    sc->sc_dev.dv_xname));
   1622 		} else {
   1623 			DPRINTF(DGE_DEBUG_LINK, ("%s: LINK: LSC -> down\n",
   1624 			    sc->sc_dev.dv_xname));
   1625 		}
   1626 	} else if (icr & ICR_RXSEQ) {
   1627 		DPRINTF(DGE_DEBUG_LINK,
   1628 		    ("%s: LINK: Receive sequence error\n",
   1629 		    sc->sc_dev.dv_xname));
   1630 	}
   1631 	/* XXX - fix errata */
   1632 }
   1633 
   1634 /*
   1635  * dge_reset:
   1636  *
   1637  *	Reset the i82597 chip.
   1638  */
   1639 static void
   1640 dge_reset(struct dge_softc *sc)
   1641 {
   1642 	int i;
   1643 
   1644 	/*
   1645 	 * Do a chip reset.
   1646 	 */
   1647 	CSR_WRITE(sc, DGE_CTRL0, CTRL0_RST | sc->sc_ctrl0);
   1648 
   1649 	delay(10000);
   1650 
   1651 	for (i = 0; i < 1000; i++) {
   1652 		if ((CSR_READ(sc, DGE_CTRL0) & CTRL0_RST) == 0)
   1653 			break;
   1654 		delay(20);
   1655 	}
   1656 
   1657 	if (CSR_READ(sc, DGE_CTRL0) & CTRL0_RST)
   1658 		printf("%s: WARNING: reset failed to complete\n",
   1659 		    sc->sc_dev.dv_xname);
   1660         /*
   1661          * Reset the EEPROM logic.
   1662          * This will cause the chip to reread its default values,
   1663 	 * which doesn't happen otherwise (errata).
   1664          */
   1665         CSR_WRITE(sc, DGE_CTRL1, CTRL1_EE_RST);
   1666         delay(10000);
   1667 }
   1668 
   1669 /*
   1670  * dge_init:		[ifnet interface function]
   1671  *
   1672  *	Initialize the interface.  Must be called at splnet().
   1673  */
   1674 static int
   1675 dge_init(struct ifnet *ifp)
   1676 {
   1677 	struct dge_softc *sc = ifp->if_softc;
   1678 	struct dge_rxsoft *rxs;
   1679 	int i, error = 0;
   1680 	uint32_t reg;
   1681 
   1682 	/*
   1683 	 * *_HDR_ALIGNED_P is constant 1 if __NO_STRICT_ALIGMENT is set.
   1684 	 * There is a small but measurable benefit to avoiding the adjusment
   1685 	 * of the descriptor so that the headers are aligned, for normal mtu,
   1686 	 * on such platforms.  One possibility is that the DMA itself is
   1687 	 * slightly more efficient if the front of the entire packet (instead
   1688 	 * of the front of the headers) is aligned.
   1689 	 *
   1690 	 * Note we must always set align_tweak to 0 if we are using
   1691 	 * jumbo frames.
   1692 	 */
   1693 #ifdef __NO_STRICT_ALIGNMENT
   1694 	sc->sc_align_tweak = 0;
   1695 #else
   1696 	if ((ifp->if_mtu + ETHER_HDR_LEN + ETHER_CRC_LEN) > (MCLBYTES - 2))
   1697 		sc->sc_align_tweak = 0;
   1698 	else
   1699 		sc->sc_align_tweak = 2;
   1700 #endif /* __NO_STRICT_ALIGNMENT */
   1701 
   1702 	/* Cancel any pending I/O. */
   1703 	dge_stop(ifp, 0);
   1704 
   1705 	/* Reset the chip to a known state. */
   1706 	dge_reset(sc);
   1707 
   1708 	/* Initialize the transmit descriptor ring. */
   1709 	memset(sc->sc_txdescs, 0, sizeof(sc->sc_txdescs));
   1710 	DGE_CDTXSYNC(sc, 0, DGE_NTXDESC,
   1711 	    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
   1712 	sc->sc_txfree = DGE_NTXDESC;
   1713 	sc->sc_txnext = 0;
   1714 
   1715 	sc->sc_txctx_ipcs = 0xffffffff;
   1716 	sc->sc_txctx_tucs = 0xffffffff;
   1717 
   1718 	CSR_WRITE(sc, DGE_TDBAH, 0);
   1719 	CSR_WRITE(sc, DGE_TDBAL, DGE_CDTXADDR(sc, 0));
   1720 	CSR_WRITE(sc, DGE_TDLEN, sizeof(sc->sc_txdescs));
   1721 	CSR_WRITE(sc, DGE_TDH, 0);
   1722 	CSR_WRITE(sc, DGE_TDT, 0);
   1723 	CSR_WRITE(sc, DGE_TIDV, TIDV);
   1724 
   1725 #if 0
   1726 	CSR_WRITE(sc, DGE_TXDCTL, TXDCTL_PTHRESH(0) |
   1727 	    TXDCTL_HTHRESH(0) | TXDCTL_WTHRESH(0));
   1728 #endif
   1729 	CSR_WRITE(sc, DGE_RXDCTL,
   1730 	    RXDCTL_PTHRESH(RXDCTL_PTHRESH_VAL) |
   1731 	    RXDCTL_HTHRESH(RXDCTL_HTHRESH_VAL) |
   1732 	    RXDCTL_WTHRESH(RXDCTL_WTHRESH_VAL));
   1733 
   1734 	/* Initialize the transmit job descriptors. */
   1735 	for (i = 0; i < DGE_TXQUEUELEN; i++)
   1736 		sc->sc_txsoft[i].txs_mbuf = NULL;
   1737 	sc->sc_txsfree = DGE_TXQUEUELEN;
   1738 	sc->sc_txsnext = 0;
   1739 	sc->sc_txsdirty = 0;
   1740 
   1741 	/*
   1742 	 * Initialize the receive descriptor and receive job
   1743 	 * descriptor rings.
   1744 	 */
   1745 	CSR_WRITE(sc, DGE_RDBAH, 0);
   1746 	CSR_WRITE(sc, DGE_RDBAL, DGE_CDRXADDR(sc, 0));
   1747 	CSR_WRITE(sc, DGE_RDLEN, sizeof(sc->sc_rxdescs));
   1748 	CSR_WRITE(sc, DGE_RDH, DGE_RXSPACE);
   1749 	CSR_WRITE(sc, DGE_RDT, 0);
   1750 	CSR_WRITE(sc, DGE_RDTR, RDTR | 0x80000000);
   1751 	CSR_WRITE(sc, DGE_FCRTL, FCRTL | FCRTL_XONE);
   1752 	CSR_WRITE(sc, DGE_FCRTH, FCRTH);
   1753 
   1754 	for (i = 0; i < DGE_NRXDESC; i++) {
   1755 		rxs = &sc->sc_rxsoft[i];
   1756 		if (rxs->rxs_mbuf == NULL) {
   1757 			if ((error = dge_add_rxbuf(sc, i)) != 0) {
   1758 				printf("%s: unable to allocate or map rx "
   1759 				    "buffer %d, error = %d\n",
   1760 				    sc->sc_dev.dv_xname, i, error);
   1761 				/*
   1762 				 * XXX Should attempt to run with fewer receive
   1763 				 * XXX buffers instead of just failing.
   1764 				 */
   1765 				dge_rxdrain(sc);
   1766 				goto out;
   1767 			}
   1768 		}
   1769 		DGE_INIT_RXDESC(sc, i);
   1770 	}
   1771 	sc->sc_rxptr = DGE_RXSPACE;
   1772 	sc->sc_rxdiscard = 0;
   1773 	DGE_RXCHAIN_RESET(sc);
   1774 
   1775 	if (sc->sc_ethercom.ec_capabilities & ETHERCAP_JUMBO_MTU) {
   1776 		sc->sc_ctrl0 |= CTRL0_JFE;
   1777 		CSR_WRITE(sc, DGE_MFS, ETHER_MAX_LEN_JUMBO << 16);
   1778 	}
   1779 
   1780 	/* Write the control registers. */
   1781 	CSR_WRITE(sc, DGE_CTRL0, sc->sc_ctrl0);
   1782 
   1783 	/*
   1784 	 * Set up checksum offload parameters.
   1785 	 */
   1786 	reg = CSR_READ(sc, DGE_RXCSUM);
   1787 	if (ifp->if_capenable & IFCAP_CSUM_IPv4)
   1788 		reg |= RXCSUM_IPOFL;
   1789 	else
   1790 		reg &= ~RXCSUM_IPOFL;
   1791 	if (ifp->if_capenable & (IFCAP_CSUM_TCPv4 | IFCAP_CSUM_UDPv4))
   1792 		reg |= RXCSUM_IPOFL | RXCSUM_TUOFL;
   1793 	else {
   1794 		reg &= ~RXCSUM_TUOFL;
   1795 		if ((ifp->if_capenable & IFCAP_CSUM_IPv4) == 0)
   1796 			reg &= ~RXCSUM_IPOFL;
   1797 	}
   1798 	CSR_WRITE(sc, DGE_RXCSUM, reg);
   1799 
   1800 	/*
   1801 	 * Set up the interrupt registers.
   1802 	 */
   1803 	CSR_WRITE(sc, DGE_IMC, 0xffffffffU);
   1804 	sc->sc_icr = ICR_TXDW | ICR_LSC | ICR_RXSEQ | ICR_RXDMT0 |
   1805 	    ICR_RXO | ICR_RXT0;
   1806 
   1807 	CSR_WRITE(sc, DGE_IMS, sc->sc_icr);
   1808 
   1809 	/*
   1810 	 * Set up the transmit control register.
   1811 	 */
   1812 	sc->sc_tctl = TCTL_TCE|TCTL_TPDE|TCTL_TXEN;
   1813 	CSR_WRITE(sc, DGE_TCTL, sc->sc_tctl);
   1814 
   1815 	/*
   1816 	 * Set up the receive control register; we actually program
   1817 	 * the register when we set the receive filter.  Use multicast
   1818 	 * address offset type 0.
   1819 	 */
   1820 	sc->sc_mchash_type = 0;
   1821 
   1822 	sc->sc_rctl = RCTL_RXEN | RCTL_RDMTS_12 | RCTL_RPDA_MC |
   1823 	    RCTL_CFF | RCTL_SECRC | RCTL_MO(sc->sc_mchash_type);
   1824 
   1825 	switch(MCLBYTES) {
   1826 	case 2048:
   1827 		sc->sc_rctl |= RCTL_BSIZE_2k;
   1828 		break;
   1829 	case 4096:
   1830 		sc->sc_rctl |= RCTL_BSIZE_4k;
   1831 		break;
   1832 	case 8192:
   1833 		sc->sc_rctl |= RCTL_BSIZE_8k;
   1834 		break;
   1835 	case 16384:
   1836 		sc->sc_rctl |= RCTL_BSIZE_16k;
   1837 		break;
   1838 	default:
   1839 		panic("dge_init: MCLBYTES %d unsupported", MCLBYTES);
   1840 	}
   1841 
   1842 	/* Set the receive filter. */
   1843 	/* Also sets RCTL */
   1844 	dge_set_filter(sc);
   1845 
   1846 	/* ...all done! */
   1847 	ifp->if_flags |= IFF_RUNNING;
   1848 	ifp->if_flags &= ~IFF_OACTIVE;
   1849 
   1850  out:
   1851 	if (error)
   1852 		printf("%s: interface not running\n", sc->sc_dev.dv_xname);
   1853 	return (error);
   1854 }
   1855 
   1856 /*
   1857  * dge_rxdrain:
   1858  *
   1859  *	Drain the receive queue.
   1860  */
   1861 static void
   1862 dge_rxdrain(struct dge_softc *sc)
   1863 {
   1864 	struct dge_rxsoft *rxs;
   1865 	int i;
   1866 
   1867 	for (i = 0; i < DGE_NRXDESC; i++) {
   1868 		rxs = &sc->sc_rxsoft[i];
   1869 		if (rxs->rxs_mbuf != NULL) {
   1870 			bus_dmamap_unload(sc->sc_dmat, rxs->rxs_dmamap);
   1871 			m_freem(rxs->rxs_mbuf);
   1872 			rxs->rxs_mbuf = NULL;
   1873 		}
   1874 	}
   1875 }
   1876 
   1877 /*
   1878  * dge_stop:		[ifnet interface function]
   1879  *
   1880  *	Stop transmission on the interface.
   1881  */
   1882 static void
   1883 dge_stop(struct ifnet *ifp, int disable)
   1884 {
   1885 	struct dge_softc *sc = ifp->if_softc;
   1886 	struct dge_txsoft *txs;
   1887 	int i;
   1888 
   1889 	/* Stop the transmit and receive processes. */
   1890 	CSR_WRITE(sc, DGE_TCTL, 0);
   1891 	CSR_WRITE(sc, DGE_RCTL, 0);
   1892 
   1893 	/* Release any queued transmit buffers. */
   1894 	for (i = 0; i < DGE_TXQUEUELEN; i++) {
   1895 		txs = &sc->sc_txsoft[i];
   1896 		if (txs->txs_mbuf != NULL) {
   1897 			bus_dmamap_unload(sc->sc_dmat, txs->txs_dmamap);
   1898 			m_freem(txs->txs_mbuf);
   1899 			txs->txs_mbuf = NULL;
   1900 		}
   1901 	}
   1902 
   1903 	if (disable)
   1904 		dge_rxdrain(sc);
   1905 
   1906 	/* Mark the interface as down and cancel the watchdog timer. */
   1907 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
   1908 	ifp->if_timer = 0;
   1909 }
   1910 
   1911 /*
   1912  * dge_add_rxbuf:
   1913  *
   1914  *	Add a receive buffer to the indiciated descriptor.
   1915  */
   1916 static int
   1917 dge_add_rxbuf(struct dge_softc *sc, int idx)
   1918 {
   1919 	struct dge_rxsoft *rxs = &sc->sc_rxsoft[idx];
   1920 	struct mbuf *m;
   1921 	int error;
   1922 
   1923 	MGETHDR(m, M_DONTWAIT, MT_DATA);
   1924 	if (m == NULL)
   1925 		return (ENOBUFS);
   1926 
   1927 	MCLGET(m, M_DONTWAIT);
   1928 	if ((m->m_flags & M_EXT) == 0) {
   1929 		m_freem(m);
   1930 		return (ENOBUFS);
   1931 	}
   1932 
   1933 	if (rxs->rxs_mbuf != NULL)
   1934 		bus_dmamap_unload(sc->sc_dmat, rxs->rxs_dmamap);
   1935 
   1936 	rxs->rxs_mbuf = m;
   1937 
   1938 	m->m_len = m->m_pkthdr.len = m->m_ext.ext_size;
   1939 	error = bus_dmamap_load_mbuf(sc->sc_dmat, rxs->rxs_dmamap, m,
   1940 	    BUS_DMA_READ|BUS_DMA_NOWAIT);
   1941 	if (error) {
   1942 		printf("%s: unable to load rx DMA map %d, error = %d\n",
   1943 		    sc->sc_dev.dv_xname, idx, error);
   1944 		panic("dge_add_rxbuf");	/* XXX XXX XXX */
   1945 	}
   1946 
   1947 	bus_dmamap_sync(sc->sc_dmat, rxs->rxs_dmamap, 0,
   1948 	    rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
   1949 
   1950 	return (0);
   1951 }
   1952 
   1953 /*
   1954  * dge_set_ral:
   1955  *
   1956  *	Set an entry in the receive address list.
   1957  */
   1958 static void
   1959 dge_set_ral(struct dge_softc *sc, const uint8_t *enaddr, int idx)
   1960 {
   1961 	uint32_t ral_lo, ral_hi;
   1962 
   1963 	if (enaddr != NULL) {
   1964 		ral_lo = enaddr[0] | (enaddr[1] << 8) | (enaddr[2] << 16) |
   1965 		    (enaddr[3] << 24);
   1966 		ral_hi = enaddr[4] | (enaddr[5] << 8);
   1967 		ral_hi |= RAH_AV;
   1968 	} else {
   1969 		ral_lo = 0;
   1970 		ral_hi = 0;
   1971 	}
   1972 	CSR_WRITE(sc, RA_ADDR(DGE_RAL, idx), ral_lo);
   1973 	CSR_WRITE(sc, RA_ADDR(DGE_RAH, idx), ral_hi);
   1974 }
   1975 
   1976 /*
   1977  * dge_mchash:
   1978  *
   1979  *	Compute the hash of the multicast address for the 4096-bit
   1980  *	multicast filter.
   1981  */
   1982 static uint32_t
   1983 dge_mchash(struct dge_softc *sc, const uint8_t *enaddr)
   1984 {
   1985 	static const int lo_shift[4] = { 4, 3, 2, 0 };
   1986 	static const int hi_shift[4] = { 4, 5, 6, 8 };
   1987 	uint32_t hash;
   1988 
   1989 	hash = (enaddr[4] >> lo_shift[sc->sc_mchash_type]) |
   1990 	    (((uint16_t) enaddr[5]) << hi_shift[sc->sc_mchash_type]);
   1991 
   1992 	return (hash & 0xfff);
   1993 }
   1994 
   1995 /*
   1996  * dge_set_filter:
   1997  *
   1998  *	Set up the receive filter.
   1999  */
   2000 static void
   2001 dge_set_filter(struct dge_softc *sc)
   2002 {
   2003 	struct ethercom *ec = &sc->sc_ethercom;
   2004 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   2005 	struct ether_multi *enm;
   2006 	struct ether_multistep step;
   2007 	uint32_t hash, reg, bit;
   2008 	int i;
   2009 
   2010 	sc->sc_rctl &= ~(RCTL_BAM | RCTL_UPE | RCTL_MPE);
   2011 
   2012 	if (ifp->if_flags & IFF_BROADCAST)
   2013 		sc->sc_rctl |= RCTL_BAM;
   2014 	if (ifp->if_flags & IFF_PROMISC) {
   2015 		sc->sc_rctl |= RCTL_UPE;
   2016 		goto allmulti;
   2017 	}
   2018 
   2019 	/*
   2020 	 * Set the station address in the first RAL slot, and
   2021 	 * clear the remaining slots.
   2022 	 */
   2023 	dge_set_ral(sc, LLADDR(ifp->if_sadl), 0);
   2024 	for (i = 1; i < RA_TABSIZE; i++)
   2025 		dge_set_ral(sc, NULL, i);
   2026 
   2027 	/* Clear out the multicast table. */
   2028 	for (i = 0; i < MC_TABSIZE; i++)
   2029 		CSR_WRITE(sc, DGE_MTA + (i << 2), 0);
   2030 
   2031 	ETHER_FIRST_MULTI(step, ec, enm);
   2032 	while (enm != NULL) {
   2033 		if (memcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
   2034 			/*
   2035 			 * We must listen to a range of multicast addresses.
   2036 			 * For now, just accept all multicasts, rather than
   2037 			 * trying to set only those filter bits needed to match
   2038 			 * the range.  (At this time, the only use of address
   2039 			 * ranges is for IP multicast routing, for which the
   2040 			 * range is big enough to require all bits set.)
   2041 			 */
   2042 			goto allmulti;
   2043 		}
   2044 
   2045 		hash = dge_mchash(sc, enm->enm_addrlo);
   2046 
   2047 		reg = (hash >> 5) & 0x7f;
   2048 		bit = hash & 0x1f;
   2049 
   2050 		hash = CSR_READ(sc, DGE_MTA + (reg << 2));
   2051 		hash |= 1U << bit;
   2052 
   2053 		CSR_WRITE(sc, DGE_MTA + (reg << 2), hash);
   2054 
   2055 		ETHER_NEXT_MULTI(step, enm);
   2056 	}
   2057 
   2058 	ifp->if_flags &= ~IFF_ALLMULTI;
   2059 	goto setit;
   2060 
   2061  allmulti:
   2062 	ifp->if_flags |= IFF_ALLMULTI;
   2063 	sc->sc_rctl |= RCTL_MPE;
   2064 
   2065  setit:
   2066 	CSR_WRITE(sc, DGE_RCTL, sc->sc_rctl);
   2067 }
   2068 
   2069 /*
   2070  * Read in the EEPROM info and verify checksum.
   2071  */
   2072 int
   2073 dge_read_eeprom(struct dge_softc *sc)
   2074 {
   2075 	uint16_t cksum;
   2076 	int i;
   2077 
   2078 	cksum = 0;
   2079 	for (i = 0; i < EEPROM_SIZE; i++) {
   2080 		sc->sc_eeprom[i] = dge_eeprom_word(sc, i);
   2081 		cksum += sc->sc_eeprom[i];
   2082 	}
   2083 	return cksum != EEPROM_CKSUM;
   2084 }
   2085 
   2086 
   2087 /*
   2088  * Read a 16-bit word from address addr in the serial EEPROM.
   2089  */
   2090 uint16_t
   2091 dge_eeprom_word(struct dge_softc *sc, int addr)
   2092 {
   2093 	uint32_t reg;
   2094 	uint16_t rval = 0;
   2095 	int i;
   2096 
   2097 	reg = CSR_READ(sc, DGE_EECD) & ~(EECD_SK|EECD_DI|EECD_CS);
   2098 
   2099 	/* Lower clock pulse (and data in to chip) */
   2100 	CSR_WRITE(sc, DGE_EECD, reg);
   2101 	/* Select chip */
   2102 	CSR_WRITE(sc, DGE_EECD, reg|EECD_CS);
   2103 
   2104 	/* Send read command */
   2105 	dge_eeprom_clockout(sc, 1);
   2106 	dge_eeprom_clockout(sc, 1);
   2107 	dge_eeprom_clockout(sc, 0);
   2108 
   2109 	/* Send address */
   2110 	for (i = 5; i >= 0; i--)
   2111 		dge_eeprom_clockout(sc, (addr >> i) & 1);
   2112 
   2113 	/* Read data */
   2114 	for (i = 0; i < 16; i++) {
   2115 		rval <<= 1;
   2116 		rval |= dge_eeprom_clockin(sc);
   2117 	}
   2118 
   2119 	/* Deselect chip */
   2120 	CSR_WRITE(sc, DGE_EECD, reg);
   2121 
   2122 	return rval;
   2123 }
   2124 
   2125 /*
   2126  * Clock out a single bit to the EEPROM.
   2127  */
   2128 void
   2129 dge_eeprom_clockout(struct dge_softc *sc, int bit)
   2130 {
   2131 	int reg;
   2132 
   2133 	reg = CSR_READ(sc, DGE_EECD) & ~(EECD_DI|EECD_SK);
   2134 	if (bit)
   2135 		reg |= EECD_DI;
   2136 
   2137 	CSR_WRITE(sc, DGE_EECD, reg);
   2138 	delay(2);
   2139 	CSR_WRITE(sc, DGE_EECD, reg|EECD_SK);
   2140 	delay(2);
   2141 	CSR_WRITE(sc, DGE_EECD, reg);
   2142 	delay(2);
   2143 }
   2144 
   2145 /*
   2146  * Clock in a single bit from EEPROM.
   2147  */
   2148 int
   2149 dge_eeprom_clockin(struct dge_softc *sc)
   2150 {
   2151 	int reg, rv;
   2152 
   2153 	reg = CSR_READ(sc, DGE_EECD) & ~(EECD_DI|EECD_DO|EECD_SK);
   2154 
   2155 	CSR_WRITE(sc, DGE_EECD, reg|EECD_SK); /* Raise clock */
   2156 	delay(2);
   2157 	rv = (CSR_READ(sc, DGE_EECD) & EECD_DO) != 0; /* Get bit */
   2158 	CSR_WRITE(sc, DGE_EECD, reg); /* Lower clock */
   2159 	delay(2);
   2160 
   2161 	return rv;
   2162 }
   2163 
   2164 static void
   2165 dge_xgmii_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr)
   2166 {
   2167 	struct dge_softc *sc = ifp->if_softc;
   2168 
   2169 	ifmr->ifm_status = IFM_AVALID;
   2170 	ifmr->ifm_active = IFM_ETHER|IFM_10G_LR;
   2171 
   2172 	if (CSR_READ(sc, DGE_STATUS) & STATUS_LINKUP)
   2173 		ifmr->ifm_status |= IFM_ACTIVE;
   2174 }
   2175 
   2176 static inline int
   2177 phwait(struct dge_softc *sc, int p, int r, int d, int type)
   2178 {
   2179         int i, mdic;
   2180 
   2181         CSR_WRITE(sc, DGE_MDIO,
   2182 	    MDIO_PHY(p) | MDIO_REG(r) | MDIO_DEV(d) | type | MDIO_CMD);
   2183         for (i = 0; i < 10; i++) {
   2184                 delay(10);
   2185                 if (((mdic = CSR_READ(sc, DGE_MDIO)) & MDIO_CMD) == 0)
   2186                         break;
   2187         }
   2188         return mdic;
   2189 }
   2190 
   2191 
   2192 static void
   2193 dge_xgmii_writereg(struct device *self, int phy, int reg, int val)
   2194 {
   2195 	struct dge_softc *sc = (void *) self;
   2196 	int mdic;
   2197 
   2198 	CSR_WRITE(sc, DGE_MDIRW, val);
   2199 	if (((mdic = phwait(sc, phy, reg, 1, MDIO_ADDR)) & MDIO_CMD)) {
   2200 		printf("%s: address cycle timeout; phy %d reg %d\n",
   2201 		    sc->sc_dev.dv_xname, phy, reg);
   2202 		return;
   2203 	}
   2204 	if (((mdic = phwait(sc, phy, reg, 1, MDIO_WRITE)) & MDIO_CMD)) {
   2205 		printf("%s: read cycle timeout; phy %d reg %d\n",
   2206 		    sc->sc_dev.dv_xname, phy, reg);
   2207 		return;
   2208 	}
   2209 }
   2210 
   2211 static void
   2212 dge_xgmii_reset(struct dge_softc *sc)
   2213 {
   2214 	dge_xgmii_writereg((void *)sc, 0, 0, BMCR_RESET);
   2215 }
   2216 
   2217 static int
   2218 dge_xgmii_mediachange(struct ifnet *ifp)
   2219 {
   2220 	return 0;
   2221 }
   2222 
   2223