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