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