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