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