Home | History | Annotate | Line # | Download | only in pci
if_sip.c revision 1.24.2.2
      1 /*	$NetBSD: if_sip.c,v 1.24.2.2 2001/06/21 20:04:49 nathanw Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2001 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Jason R. Thorpe.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *	This product includes software developed by the NetBSD
     21  *	Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 /*-
     40  * Copyright (c) 1999 Network Computer, Inc.
     41  * All rights reserved.
     42  *
     43  * Redistribution and use in source and binary forms, with or without
     44  * modification, are permitted provided that the following conditions
     45  * are met:
     46  * 1. Redistributions of source code must retain the above copyright
     47  *    notice, this list of conditions and the following disclaimer.
     48  * 2. Redistributions in binary form must reproduce the above copyright
     49  *    notice, this list of conditions and the following disclaimer in the
     50  *    documentation and/or other materials provided with the distribution.
     51  * 3. Neither the name of Network Computer, Inc. nor the names of its
     52  *    contributors may be used to endorse or promote products derived
     53  *    from this software without specific prior written permission.
     54  *
     55  * THIS SOFTWARE IS PROVIDED BY NETWORK COMPUTER, INC. AND CONTRIBUTORS
     56  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     57  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     58  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     59  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     60  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     61  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     62  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     63  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     64  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     65  * POSSIBILITY OF SUCH DAMAGE.
     66  */
     67 
     68 /*
     69  * Device driver for the Silicon Integrated Systems SiS 900,
     70  * SiS 7016 10/100, National Semiconductor DP83815 10/100, and
     71  * National Semiconductor DP83820 10/100/1000 PCI Ethernet
     72  * controllers.
     73  *
     74  * Originally written to support the SiS 900 by Jason R. Thorpe for
     75  * Network Computer, Inc.
     76  *
     77  * TODO:
     78  *
     79  *	- Support the 10-bit interface on the DP83820 (for fiber).
     80  *
     81  *	- Support jumbo packets on the DP83820.
     82  *
     83  *	- Reduce the interrupt load.
     84  */
     85 
     86 #include "bpfilter.h"
     87 
     88 #include <sys/param.h>
     89 #include <sys/systm.h>
     90 #include <sys/callout.h>
     91 #include <sys/mbuf.h>
     92 #include <sys/malloc.h>
     93 #include <sys/kernel.h>
     94 #include <sys/socket.h>
     95 #include <sys/ioctl.h>
     96 #include <sys/errno.h>
     97 #include <sys/device.h>
     98 #include <sys/queue.h>
     99 
    100 #include <uvm/uvm_extern.h>		/* for PAGE_SIZE */
    101 
    102 #include <net/if.h>
    103 #include <net/if_dl.h>
    104 #include <net/if_media.h>
    105 #include <net/if_ether.h>
    106 
    107 #if NBPFILTER > 0
    108 #include <net/bpf.h>
    109 #endif
    110 
    111 #include <machine/bus.h>
    112 #include <machine/intr.h>
    113 #include <machine/endian.h>
    114 
    115 #include <dev/mii/mii.h>
    116 #include <dev/mii/miivar.h>
    117 #ifdef DP83820
    118 #include <dev/mii/mii_bitbang.h>
    119 #endif /* DP83820 */
    120 
    121 #include <dev/pci/pcireg.h>
    122 #include <dev/pci/pcivar.h>
    123 #include <dev/pci/pcidevs.h>
    124 
    125 #include <dev/pci/if_sipreg.h>
    126 
    127 #ifdef DP83820		/* DP83820 Gigabit Ethernet */
    128 #define	SIP_DECL(x)	__CONCAT(gsip_,x)
    129 #else			/* SiS900 and DP83815 */
    130 #define	SIP_DECL(x)	__CONCAT(sip_,x)
    131 #endif
    132 
    133 #define	SIP_STR(x)	__STRING(SIP_DECL(x))
    134 
    135 /*
    136  * Transmit descriptor list size.  This is arbitrary, but allocate
    137  * enough descriptors for 128 pending transmissions, and 8 segments
    138  * per packet.  This MUST work out to a power of 2.
    139  */
    140 #define	SIP_NTXSEGS		8
    141 
    142 #define	SIP_TXQUEUELEN		256
    143 #define	SIP_NTXDESC		(SIP_TXQUEUELEN * SIP_NTXSEGS)
    144 #define	SIP_NTXDESC_MASK	(SIP_NTXDESC - 1)
    145 #define	SIP_NEXTTX(x)		(((x) + 1) & SIP_NTXDESC_MASK)
    146 
    147 /*
    148  * Receive descriptor list size.  We have one Rx buffer per incoming
    149  * packet, so this logic is a little simpler.
    150  */
    151 #define	SIP_NRXDESC		128
    152 #define	SIP_NRXDESC_MASK	(SIP_NRXDESC - 1)
    153 #define	SIP_NEXTRX(x)		(((x) + 1) & SIP_NRXDESC_MASK)
    154 
    155 /*
    156  * Control structures are DMA'd to the SiS900 chip.  We allocate them in
    157  * a single clump that maps to a single DMA segment to make several things
    158  * easier.
    159  */
    160 struct sip_control_data {
    161 	/*
    162 	 * The transmit descriptors.
    163 	 */
    164 	struct sip_desc scd_txdescs[SIP_NTXDESC];
    165 
    166 	/*
    167 	 * The receive descriptors.
    168 	 */
    169 	struct sip_desc scd_rxdescs[SIP_NRXDESC];
    170 };
    171 
    172 #define	SIP_CDOFF(x)	offsetof(struct sip_control_data, x)
    173 #define	SIP_CDTXOFF(x)	SIP_CDOFF(scd_txdescs[(x)])
    174 #define	SIP_CDRXOFF(x)	SIP_CDOFF(scd_rxdescs[(x)])
    175 
    176 /*
    177  * Software state for transmit jobs.
    178  */
    179 struct sip_txsoft {
    180 	struct mbuf *txs_mbuf;		/* head of our mbuf chain */
    181 	bus_dmamap_t txs_dmamap;	/* our DMA map */
    182 	int txs_firstdesc;		/* first descriptor in packet */
    183 	int txs_lastdesc;		/* last descriptor in packet */
    184 	SIMPLEQ_ENTRY(sip_txsoft) txs_q;
    185 };
    186 
    187 SIMPLEQ_HEAD(sip_txsq, sip_txsoft);
    188 
    189 /*
    190  * Software state for receive jobs.
    191  */
    192 struct sip_rxsoft {
    193 	struct mbuf *rxs_mbuf;		/* head of our mbuf chain */
    194 	bus_dmamap_t rxs_dmamap;	/* our DMA map */
    195 };
    196 
    197 /*
    198  * Software state per device.
    199  */
    200 struct sip_softc {
    201 	struct device sc_dev;		/* generic device information */
    202 	bus_space_tag_t sc_st;		/* bus space tag */
    203 	bus_space_handle_t sc_sh;	/* bus space handle */
    204 	bus_dma_tag_t sc_dmat;		/* bus DMA tag */
    205 	struct ethercom sc_ethercom;	/* ethernet common data */
    206 	void *sc_sdhook;		/* shutdown hook */
    207 
    208 	const struct sip_product *sc_model; /* which model are we? */
    209 
    210 	void *sc_ih;			/* interrupt cookie */
    211 
    212 	struct mii_data sc_mii;		/* MII/media information */
    213 
    214 	struct callout sc_tick_ch;	/* tick callout */
    215 
    216 	bus_dmamap_t sc_cddmamap;	/* control data DMA map */
    217 #define	sc_cddma	sc_cddmamap->dm_segs[0].ds_addr
    218 
    219 	/*
    220 	 * Software state for transmit and receive descriptors.
    221 	 */
    222 	struct sip_txsoft sc_txsoft[SIP_TXQUEUELEN];
    223 	struct sip_rxsoft sc_rxsoft[SIP_NRXDESC];
    224 
    225 	/*
    226 	 * Control data structures.
    227 	 */
    228 	struct sip_control_data *sc_control_data;
    229 #define	sc_txdescs	sc_control_data->scd_txdescs
    230 #define	sc_rxdescs	sc_control_data->scd_rxdescs
    231 
    232 #ifdef SIP_EVENT_COUNTERS
    233 	/*
    234 	 * Event counters.
    235 	 */
    236 	struct evcnt sc_ev_txsstall;	/* Tx stalled due to no txs */
    237 	struct evcnt sc_ev_txdstall;	/* Tx stalled due to no txd */
    238 	struct evcnt sc_ev_txintr;	/* Tx interrupts */
    239 	struct evcnt sc_ev_rxintr;	/* Rx interrupts */
    240 #ifdef DP83820
    241 	struct evcnt sc_ev_rxipsum;	/* IP checksums checked in-bound */
    242 	struct evcnt sc_ev_rxtcpsum;	/* TCP checksums checked in-bound */
    243 	struct evcnt sc_ev_rxudpsum;	/* UDP checksums checked in-boudn */
    244 	struct evcnt sc_ev_txipsum;	/* IP checksums comp. out-bound */
    245 	struct evcnt sc_ev_txtcpsum;	/* TCP checksums comp. out-bound */
    246 	struct evcnt sc_ev_txudpsum;	/* UDP checksums comp. out-bound */
    247 #endif /* DP83820 */
    248 #endif /* SIP_EVENT_COUNTERS */
    249 
    250 	u_int32_t sc_txcfg;		/* prototype TXCFG register */
    251 	u_int32_t sc_rxcfg;		/* prototype RXCFG register */
    252 	u_int32_t sc_imr;		/* prototype IMR register */
    253 	u_int32_t sc_rfcr;		/* prototype RFCR register */
    254 
    255 	u_int32_t sc_cfg;		/* prototype CFG register */
    256 
    257 #ifdef DP83820
    258 	u_int32_t sc_gpior;		/* prototype GPIOR register */
    259 #endif /* DP83820 */
    260 
    261 	u_int32_t sc_tx_fill_thresh;	/* transmit fill threshold */
    262 	u_int32_t sc_tx_drain_thresh;	/* transmit drain threshold */
    263 
    264 	u_int32_t sc_rx_drain_thresh;	/* receive drain threshold */
    265 
    266 	int	sc_flags;		/* misc. flags; see below */
    267 
    268 	int	sc_txfree;		/* number of free Tx descriptors */
    269 	int	sc_txnext;		/* next ready Tx descriptor */
    270 
    271 	struct sip_txsq sc_txfreeq;	/* free Tx descsofts */
    272 	struct sip_txsq sc_txdirtyq;	/* dirty Tx descsofts */
    273 
    274 	int	sc_rxptr;		/* next ready Rx descriptor/descsoft */
    275 };
    276 
    277 /* sc_flags */
    278 #define	SIPF_PAUSED	0x00000001	/* paused (802.3x flow control) */
    279 
    280 #ifdef SIP_EVENT_COUNTERS
    281 #define	SIP_EVCNT_INCR(ev)	(ev)->ev_count++
    282 #else
    283 #define	SIP_EVCNT_INCR(ev)	/* nothing */
    284 #endif
    285 
    286 #define	SIP_CDTXADDR(sc, x)	((sc)->sc_cddma + SIP_CDTXOFF((x)))
    287 #define	SIP_CDRXADDR(sc, x)	((sc)->sc_cddma + SIP_CDRXOFF((x)))
    288 
    289 #define	SIP_CDTXSYNC(sc, x, n, ops)					\
    290 do {									\
    291 	int __x, __n;							\
    292 									\
    293 	__x = (x);							\
    294 	__n = (n);							\
    295 									\
    296 	/* If it will wrap around, sync to the end of the ring. */	\
    297 	if ((__x + __n) > SIP_NTXDESC) {				\
    298 		bus_dmamap_sync((sc)->sc_dmat, (sc)->sc_cddmamap,	\
    299 		    SIP_CDTXOFF(__x), sizeof(struct sip_desc) *		\
    300 		    (SIP_NTXDESC - __x), (ops));			\
    301 		__n -= (SIP_NTXDESC - __x);				\
    302 		__x = 0;						\
    303 	}								\
    304 									\
    305 	/* Now sync whatever is left. */				\
    306 	bus_dmamap_sync((sc)->sc_dmat, (sc)->sc_cddmamap,		\
    307 	    SIP_CDTXOFF(__x), sizeof(struct sip_desc) * __n, (ops));	\
    308 } while (0)
    309 
    310 #define	SIP_CDRXSYNC(sc, x, ops)					\
    311 	bus_dmamap_sync((sc)->sc_dmat, (sc)->sc_cddmamap,		\
    312 	    SIP_CDRXOFF((x)), sizeof(struct sip_desc), (ops))
    313 
    314 /*
    315  * Note we rely on MCLBYTES being a power of two below.
    316  */
    317 #ifdef DP83820
    318 #define	SIP_INIT_RXDESC_EXTSTS	__sipd->sipd_extsts = 0;
    319 #else
    320 #define	SIP_INIT_RXDESC_EXTSTS	/* nothing */
    321 #endif
    322 #define	SIP_INIT_RXDESC(sc, x)						\
    323 do {									\
    324 	struct sip_rxsoft *__rxs = &(sc)->sc_rxsoft[(x)];		\
    325 	struct sip_desc *__sipd = &(sc)->sc_rxdescs[(x)];		\
    326 									\
    327 	__sipd->sipd_link = htole32(SIP_CDRXADDR((sc), SIP_NEXTRX((x)))); \
    328 	__sipd->sipd_bufptr = htole32(__rxs->rxs_dmamap->dm_segs[0].ds_addr); \
    329 	__sipd->sipd_cmdsts = htole32(CMDSTS_INTR |			\
    330 	    ((MCLBYTES - 1) & CMDSTS_SIZE_MASK));			\
    331 	SIP_INIT_RXDESC_EXTSTS						\
    332 	SIP_CDRXSYNC((sc), (x), BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE); \
    333 } while (0)
    334 
    335 #define SIP_TIMEOUT 1000
    336 
    337 void	SIP_DECL(start)(struct ifnet *);
    338 void	SIP_DECL(watchdog)(struct ifnet *);
    339 int	SIP_DECL(ioctl)(struct ifnet *, u_long, caddr_t);
    340 int	SIP_DECL(init)(struct ifnet *);
    341 void	SIP_DECL(stop)(struct ifnet *, int);
    342 
    343 void	SIP_DECL(shutdown)(void *);
    344 
    345 void	SIP_DECL(reset)(struct sip_softc *);
    346 void	SIP_DECL(rxdrain)(struct sip_softc *);
    347 int	SIP_DECL(add_rxbuf)(struct sip_softc *, int);
    348 void	SIP_DECL(read_eeprom)(struct sip_softc *, int, int, u_int16_t *);
    349 void	SIP_DECL(tick)(void *);
    350 
    351 #if !defined(DP83820)
    352 void	SIP_DECL(sis900_set_filter)(struct sip_softc *);
    353 #endif /* ! DP83820 */
    354 void	SIP_DECL(dp83815_set_filter)(struct sip_softc *);
    355 
    356 #if defined(DP83820)
    357 void	SIP_DECL(dp83820_read_macaddr)(struct sip_softc *, u_int8_t *);
    358 #else
    359 void	SIP_DECL(sis900_read_macaddr)(struct sip_softc *, u_int8_t *);
    360 void	SIP_DECL(dp83815_read_macaddr)(struct sip_softc *, u_int8_t *);
    361 #endif /* DP83820 */
    362 
    363 int	SIP_DECL(intr)(void *);
    364 void	SIP_DECL(txintr)(struct sip_softc *);
    365 void	SIP_DECL(rxintr)(struct sip_softc *);
    366 
    367 #if defined(DP83820)
    368 int	SIP_DECL(dp83820_mii_readreg)(struct device *, int, int);
    369 void	SIP_DECL(dp83820_mii_writereg)(struct device *, int, int, int);
    370 void	SIP_DECL(dp83820_mii_statchg)(struct device *);
    371 #else
    372 int	SIP_DECL(sis900_mii_readreg)(struct device *, int, int);
    373 void	SIP_DECL(sis900_mii_writereg)(struct device *, int, int, int);
    374 void	SIP_DECL(sis900_mii_statchg)(struct device *);
    375 
    376 int	SIP_DECL(dp83815_mii_readreg)(struct device *, int, int);
    377 void	SIP_DECL(dp83815_mii_writereg)(struct device *, int, int, int);
    378 void	SIP_DECL(dp83815_mii_statchg)(struct device *);
    379 #endif /* DP83820 */
    380 
    381 int	SIP_DECL(mediachange)(struct ifnet *);
    382 void	SIP_DECL(mediastatus)(struct ifnet *, struct ifmediareq *);
    383 
    384 int	SIP_DECL(match)(struct device *, struct cfdata *, void *);
    385 void	SIP_DECL(attach)(struct device *, struct device *, void *);
    386 
    387 int	SIP_DECL(copy_small) = 0;
    388 
    389 struct cfattach SIP_DECL(ca) = {
    390 	sizeof(struct sip_softc), SIP_DECL(match), SIP_DECL(attach),
    391 };
    392 
    393 /*
    394  * Descriptions of the variants of the SiS900.
    395  */
    396 struct sip_variant {
    397 	int	(*sipv_mii_readreg)(struct device *, int, int);
    398 	void	(*sipv_mii_writereg)(struct device *, int, int, int);
    399 	void	(*sipv_mii_statchg)(struct device *);
    400 	void	(*sipv_set_filter)(struct sip_softc *);
    401 	void	(*sipv_read_macaddr)(struct sip_softc *, u_int8_t *);
    402 };
    403 
    404 #if defined(DP83820)
    405 u_int32_t SIP_DECL(dp83820_mii_bitbang_read)(struct device *);
    406 void	SIP_DECL(dp83820_mii_bitbang_write)(struct device *, u_int32_t);
    407 
    408 const struct mii_bitbang_ops SIP_DECL(dp83820_mii_bitbang_ops) = {
    409 	SIP_DECL(dp83820_mii_bitbang_read),
    410 	SIP_DECL(dp83820_mii_bitbang_write),
    411 	{
    412 		EROMAR_MDIO,		/* MII_BIT_MDO */
    413 		EROMAR_MDIO,		/* MII_BIT_MDI */
    414 		EROMAR_MDC,		/* MII_BIT_MDC */
    415 		EROMAR_MDDIR,		/* MII_BIT_DIR_HOST_PHY */
    416 		0,			/* MII_BIT_DIR_PHY_HOST */
    417 	}
    418 };
    419 #endif /* DP83820 */
    420 
    421 #if defined(DP83820)
    422 const struct sip_variant SIP_DECL(variant_dp83820) = {
    423 	SIP_DECL(dp83820_mii_readreg),
    424 	SIP_DECL(dp83820_mii_writereg),
    425 	SIP_DECL(dp83820_mii_statchg),
    426 	SIP_DECL(dp83815_set_filter),
    427 	SIP_DECL(dp83820_read_macaddr),
    428 };
    429 #else
    430 const struct sip_variant SIP_DECL(variant_sis900) = {
    431 	SIP_DECL(sis900_mii_readreg),
    432 	SIP_DECL(sis900_mii_writereg),
    433 	SIP_DECL(sis900_mii_statchg),
    434 	SIP_DECL(sis900_set_filter),
    435 	SIP_DECL(sis900_read_macaddr),
    436 };
    437 
    438 const struct sip_variant SIP_DECL(variant_dp83815) = {
    439 	SIP_DECL(dp83815_mii_readreg),
    440 	SIP_DECL(dp83815_mii_writereg),
    441 	SIP_DECL(dp83815_mii_statchg),
    442 	SIP_DECL(dp83815_set_filter),
    443 	SIP_DECL(dp83815_read_macaddr),
    444 };
    445 #endif /* DP83820 */
    446 
    447 /*
    448  * Devices supported by this driver.
    449  */
    450 const struct sip_product {
    451 	pci_vendor_id_t		sip_vendor;
    452 	pci_product_id_t	sip_product;
    453 	const char		*sip_name;
    454 	const struct sip_variant *sip_variant;
    455 } SIP_DECL(products)[] = {
    456 #if defined(DP83820)
    457 	{ PCI_VENDOR_NS,	PCI_PRODUCT_NS_DP83820,
    458 	  "NatSemi DP83820 Gigabit Ethernet",
    459 	  &SIP_DECL(variant_dp83820) },
    460 #else
    461 	{ PCI_VENDOR_SIS,	PCI_PRODUCT_SIS_900,
    462 	  "SiS 900 10/100 Ethernet",
    463 	  &SIP_DECL(variant_sis900) },
    464 	{ PCI_VENDOR_SIS,	PCI_PRODUCT_SIS_7016,
    465 	  "SiS 7016 10/100 Ethernet",
    466 	  &SIP_DECL(variant_sis900) },
    467 
    468 	{ PCI_VENDOR_NS,	PCI_PRODUCT_NS_DP83815,
    469 	  "NatSemi DP83815 10/100 Ethernet",
    470 	  &SIP_DECL(variant_dp83815) },
    471 #endif /* DP83820 */
    472 
    473 	{ 0,			0,
    474 	  NULL,
    475 	  NULL },
    476 };
    477 
    478 static const struct sip_product *
    479 SIP_DECL(lookup)(const struct pci_attach_args *pa)
    480 {
    481 	const struct sip_product *sip;
    482 
    483 	for (sip = SIP_DECL(products); sip->sip_name != NULL; sip++) {
    484 		if (PCI_VENDOR(pa->pa_id) == sip->sip_vendor &&
    485 		    PCI_PRODUCT(pa->pa_id) == sip->sip_product)
    486 			return (sip);
    487 	}
    488 	return (NULL);
    489 }
    490 
    491 int
    492 SIP_DECL(match)(struct device *parent, struct cfdata *cf, void *aux)
    493 {
    494 	struct pci_attach_args *pa = aux;
    495 
    496 	if (SIP_DECL(lookup)(pa) != NULL)
    497 		return (1);
    498 
    499 	return (0);
    500 }
    501 
    502 void
    503 SIP_DECL(attach)(struct device *parent, struct device *self, void *aux)
    504 {
    505 	struct sip_softc *sc = (struct sip_softc *) self;
    506 	struct pci_attach_args *pa = aux;
    507 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    508 	pci_chipset_tag_t pc = pa->pa_pc;
    509 	pci_intr_handle_t ih;
    510 	const char *intrstr = NULL;
    511 	bus_space_tag_t iot, memt;
    512 	bus_space_handle_t ioh, memh;
    513 	bus_dma_segment_t seg;
    514 	int ioh_valid, memh_valid;
    515 	int i, rseg, error;
    516 	const struct sip_product *sip;
    517 	pcireg_t pmode;
    518 	u_int8_t enaddr[ETHER_ADDR_LEN];
    519 	int pmreg;
    520 #ifdef DP83820
    521 	pcireg_t memtype;
    522 	u_int32_t reg;
    523 #endif /* DP83820 */
    524 
    525 	callout_init(&sc->sc_tick_ch);
    526 
    527 	sip = SIP_DECL(lookup)(pa);
    528 	if (sip == NULL) {
    529 		printf("\n");
    530 		panic(SIP_STR(attach) ": impossible");
    531 	}
    532 
    533 	printf(": %s\n", sip->sip_name);
    534 
    535 	sc->sc_model = sip;
    536 
    537 	/*
    538 	 * Map the device.
    539 	 */
    540 	ioh_valid = (pci_mapreg_map(pa, SIP_PCI_CFGIOA,
    541 	    PCI_MAPREG_TYPE_IO, 0,
    542 	    &iot, &ioh, NULL, NULL) == 0);
    543 #ifdef DP83820
    544 	memtype = pci_mapreg_type(pa->pa_pc, pa->pa_tag, SIP_PCI_CFGMA);
    545 	switch (memtype) {
    546 	case PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT:
    547 	case PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_64BIT:
    548 		memh_valid = (pci_mapreg_map(pa, SIP_PCI_CFGMA,
    549 		    memtype, 0, &memt, &memh, NULL, NULL) == 0);
    550 		break;
    551 	default:
    552 		memh_valid = 0;
    553 	}
    554 #else
    555 	memh_valid = (pci_mapreg_map(pa, SIP_PCI_CFGMA,
    556 	    PCI_MAPREG_TYPE_MEM|PCI_MAPREG_MEM_TYPE_32BIT, 0,
    557 	    &memt, &memh, NULL, NULL) == 0);
    558 #endif /* DP83820 */
    559 
    560 	if (memh_valid) {
    561 		sc->sc_st = memt;
    562 		sc->sc_sh = memh;
    563 	} else if (ioh_valid) {
    564 		sc->sc_st = iot;
    565 		sc->sc_sh = ioh;
    566 	} else {
    567 		printf("%s: unable to map device registers\n",
    568 		    sc->sc_dev.dv_xname);
    569 		return;
    570 	}
    571 
    572 	sc->sc_dmat = pa->pa_dmat;
    573 
    574 	/* Enable bus mastering. */
    575 	pci_conf_write(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
    576 	    pci_conf_read(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG) |
    577 	    PCI_COMMAND_MASTER_ENABLE);
    578 
    579 	/* Get it out of power save mode if needed. */
    580 	if (pci_get_capability(pc, pa->pa_tag, PCI_CAP_PWRMGMT, &pmreg, 0)) {
    581 		pmode = pci_conf_read(pc, pa->pa_tag, pmreg + 4) & 0x3;
    582 		if (pmode == 3) {
    583 			/*
    584 			 * The card has lost all configuration data in
    585 			 * this state, so punt.
    586 			 */
    587 			printf("%s: unable to wake up from power state D3\n",
    588 			    sc->sc_dev.dv_xname);
    589 			return;
    590 		}
    591 		if (pmode != 0) {
    592 			printf("%s: waking up from power state D%d\n",
    593 			    sc->sc_dev.dv_xname, pmode);
    594 			pci_conf_write(pc, pa->pa_tag, pmreg + 4, 0);
    595 		}
    596 	}
    597 
    598 	/*
    599 	 * Map and establish our interrupt.
    600 	 */
    601 	if (pci_intr_map(pa, &ih)) {
    602 		printf("%s: unable to map interrupt\n", sc->sc_dev.dv_xname);
    603 		return;
    604 	}
    605 	intrstr = pci_intr_string(pc, ih);
    606 	sc->sc_ih = pci_intr_establish(pc, ih, IPL_NET, SIP_DECL(intr), sc);
    607 	if (sc->sc_ih == NULL) {
    608 		printf("%s: unable to establish interrupt",
    609 		    sc->sc_dev.dv_xname);
    610 		if (intrstr != NULL)
    611 			printf(" at %s", intrstr);
    612 		printf("\n");
    613 		return;
    614 	}
    615 	printf("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstr);
    616 
    617 	SIMPLEQ_INIT(&sc->sc_txfreeq);
    618 	SIMPLEQ_INIT(&sc->sc_txdirtyq);
    619 
    620 	/*
    621 	 * Allocate the control data structures, and create and load the
    622 	 * DMA map for it.
    623 	 */
    624 	if ((error = bus_dmamem_alloc(sc->sc_dmat,
    625 	    sizeof(struct sip_control_data), PAGE_SIZE, 0, &seg, 1, &rseg,
    626 	    0)) != 0) {
    627 		printf("%s: unable to allocate control data, error = %d\n",
    628 		    sc->sc_dev.dv_xname, error);
    629 		goto fail_0;
    630 	}
    631 
    632 	if ((error = bus_dmamem_map(sc->sc_dmat, &seg, rseg,
    633 	    sizeof(struct sip_control_data), (caddr_t *)&sc->sc_control_data,
    634 	    BUS_DMA_COHERENT)) != 0) {
    635 		printf("%s: unable to map control data, error = %d\n",
    636 		    sc->sc_dev.dv_xname, error);
    637 		goto fail_1;
    638 	}
    639 
    640 	if ((error = bus_dmamap_create(sc->sc_dmat,
    641 	    sizeof(struct sip_control_data), 1,
    642 	    sizeof(struct sip_control_data), 0, 0, &sc->sc_cddmamap)) != 0) {
    643 		printf("%s: unable to create control data DMA map, "
    644 		    "error = %d\n", sc->sc_dev.dv_xname, error);
    645 		goto fail_2;
    646 	}
    647 
    648 	if ((error = bus_dmamap_load(sc->sc_dmat, sc->sc_cddmamap,
    649 	    sc->sc_control_data, sizeof(struct sip_control_data), NULL,
    650 	    0)) != 0) {
    651 		printf("%s: unable to load control data DMA map, error = %d\n",
    652 		    sc->sc_dev.dv_xname, error);
    653 		goto fail_3;
    654 	}
    655 
    656 	/*
    657 	 * Create the transmit buffer DMA maps.
    658 	 */
    659 	for (i = 0; i < SIP_TXQUEUELEN; i++) {
    660 		if ((error = bus_dmamap_create(sc->sc_dmat, MCLBYTES,
    661 		    SIP_NTXSEGS, MCLBYTES, 0, 0,
    662 		    &sc->sc_txsoft[i].txs_dmamap)) != 0) {
    663 			printf("%s: unable to create tx DMA map %d, "
    664 			    "error = %d\n", sc->sc_dev.dv_xname, i, error);
    665 			goto fail_4;
    666 		}
    667 	}
    668 
    669 	/*
    670 	 * Create the receive buffer DMA maps.
    671 	 */
    672 	for (i = 0; i < SIP_NRXDESC; i++) {
    673 		if ((error = bus_dmamap_create(sc->sc_dmat, MCLBYTES, 1,
    674 		    MCLBYTES, 0, 0, &sc->sc_rxsoft[i].rxs_dmamap)) != 0) {
    675 			printf("%s: unable to create rx DMA map %d, "
    676 			    "error = %d\n", sc->sc_dev.dv_xname, i, error);
    677 			goto fail_5;
    678 		}
    679 		sc->sc_rxsoft[i].rxs_mbuf = NULL;
    680 	}
    681 
    682 	/*
    683 	 * Reset the chip to a known state.
    684 	 */
    685 	SIP_DECL(reset)(sc);
    686 
    687 	/*
    688 	 * Read the Ethernet address from the EEPROM.  This might
    689 	 * also fetch other stuff from the EEPROM and stash it
    690 	 * in the softc.
    691 	 */
    692 	sc->sc_cfg = 0;
    693 	(*sip->sip_variant->sipv_read_macaddr)(sc, enaddr);
    694 
    695 	printf("%s: Ethernet address %s\n", sc->sc_dev.dv_xname,
    696 	    ether_sprintf(enaddr));
    697 
    698 	/*
    699 	 * Initialize the configuration register: aggressive PCI
    700 	 * bus request algorithm, default backoff, default OW timer,
    701 	 * default parity error detection.
    702 	 *
    703 	 * NOTE: "Big endian mode" is useless on the SiS900 and
    704 	 * friends -- it affects packet data, not descriptors.
    705 	 */
    706 #ifdef DP83820
    707 	reg = bus_space_read_4(sc->sc_st, sc->sc_sh, SIP_CFG);
    708 	if (reg & CFG_PCI64_DET) {
    709 		printf("%s: 64-bit PCI slot detected\n", sc->sc_dev.dv_xname);
    710 		/*
    711 		 * XXX Need some PCI flags indicating support for
    712 		 * XXX 64-bit addressing (SAC or DAC) and 64-bit
    713 		 * XXX data path.
    714 		 */
    715 	}
    716 	if (sc->sc_cfg & (CFG_TBI_EN|CFG_EXT_125)) {
    717 		const char *sep = "";
    718 		printf("%s: using ", sc->sc_dev.dv_xname);
    719 		if (sc->sc_cfg & CFG_EXT_125) {
    720 			printf("%s125MHz clock", sep);
    721 			sep = ", ";
    722 		}
    723 		if (sc->sc_cfg & CFG_TBI_EN) {
    724 			printf("%sten-bit interface", sep);
    725 			sep = ", ";
    726 		}
    727 		printf("\n");
    728 	}
    729 	if ((pa->pa_flags & PCI_FLAGS_MRM_OKAY) == 0)
    730 		sc->sc_cfg |= CFG_MRM_DIS;
    731 	if ((pa->pa_flags & PCI_FLAGS_MWI_OKAY) == 0)
    732 		sc->sc_cfg |= CFG_MWI_DIS;
    733 
    734 	/*
    735 	 * Use the extended descriptor format on the DP83820.  This
    736 	 * gives us an interface to VLAN tagging and IPv4/TCP/UDP
    737 	 * checksumming.
    738 	 */
    739 	sc->sc_cfg |= CFG_EXTSTS_EN;
    740 #endif /* DP83820 */
    741 
    742 	/*
    743 	 * Initialize our media structures and probe the MII.
    744 	 */
    745 	sc->sc_mii.mii_ifp = ifp;
    746 	sc->sc_mii.mii_readreg = sip->sip_variant->sipv_mii_readreg;
    747 	sc->sc_mii.mii_writereg = sip->sip_variant->sipv_mii_writereg;
    748 	sc->sc_mii.mii_statchg = sip->sip_variant->sipv_mii_statchg;
    749 	ifmedia_init(&sc->sc_mii.mii_media, 0, SIP_DECL(mediachange),
    750 	    SIP_DECL(mediastatus));
    751 #ifdef DP83820
    752 	if (sc->sc_cfg & CFG_TBI_EN) {
    753 		/* Using ten-bit interface. */
    754 		printf("%s: TBI -- FIXME\n", sc->sc_dev.dv_xname);
    755 	} else {
    756 		mii_attach(&sc->sc_dev, &sc->sc_mii, 0xffffffff, MII_PHY_ANY,
    757 		    MII_OFFSET_ANY, 0);
    758 		if (LIST_FIRST(&sc->sc_mii.mii_phys) == NULL) {
    759 			ifmedia_add(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE,
    760 			    0, NULL);
    761 			ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE);
    762 		} else
    763 			ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_AUTO);
    764 	}
    765 #else
    766 	mii_attach(&sc->sc_dev, &sc->sc_mii, 0xffffffff, MII_PHY_ANY,
    767 	    MII_OFFSET_ANY, 0);
    768 	if (LIST_FIRST(&sc->sc_mii.mii_phys) == NULL) {
    769 		ifmedia_add(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE, 0, NULL);
    770 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE);
    771 	} else
    772 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_AUTO);
    773 #endif /* DP83820 */
    774 
    775 	ifp = &sc->sc_ethercom.ec_if;
    776 	strcpy(ifp->if_xname, sc->sc_dev.dv_xname);
    777 	ifp->if_softc = sc;
    778 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
    779 	ifp->if_ioctl = SIP_DECL(ioctl);
    780 	ifp->if_start = SIP_DECL(start);
    781 	ifp->if_watchdog = SIP_DECL(watchdog);
    782 	ifp->if_init = SIP_DECL(init);
    783 	ifp->if_stop = SIP_DECL(stop);
    784 	IFQ_SET_READY(&ifp->if_snd);
    785 
    786 	/*
    787 	 * We can support 802.1Q VLAN-sized frames.
    788 	 */
    789 	sc->sc_ethercom.ec_capabilities |= ETHERCAP_VLAN_MTU;
    790 
    791 #ifdef DP83820
    792 	/*
    793 	 * And the DP83820 can do VLAN tagging in hardware.
    794 	 */
    795 	sc->sc_ethercom.ec_capabilities |= ETHERCAP_VLAN_HWTAGGING;
    796 
    797 	/*
    798 	 * The DP83820 can do IPv4, TCPv4, and UDPv4 checksums
    799 	 * in hardware.
    800 	 */
    801 	ifp->if_capabilities |= IFCAP_CSUM_IPv4 | IFCAP_CSUM_TCPv4 |
    802 	    IFCAP_CSUM_UDPv4;
    803 #endif /* DP83820 */
    804 
    805 	/*
    806 	 * Attach the interface.
    807 	 */
    808 	if_attach(ifp);
    809 	ether_ifattach(ifp, enaddr);
    810 
    811 #ifdef SIP_EVENT_COUNTERS
    812 	/*
    813 	 * Attach event counters.
    814 	 */
    815 	evcnt_attach_dynamic(&sc->sc_ev_txsstall, EVCNT_TYPE_MISC,
    816 	    NULL, sc->sc_dev.dv_xname, "txsstall");
    817 	evcnt_attach_dynamic(&sc->sc_ev_txdstall, EVCNT_TYPE_MISC,
    818 	    NULL, sc->sc_dev.dv_xname, "txdstall");
    819 	evcnt_attach_dynamic(&sc->sc_ev_txintr, EVCNT_TYPE_INTR,
    820 	    NULL, sc->sc_dev.dv_xname, "txintr");
    821 	evcnt_attach_dynamic(&sc->sc_ev_rxintr, EVCNT_TYPE_INTR,
    822 	    NULL, sc->sc_dev.dv_xname, "rxintr");
    823 #ifdef DP83820
    824 	evcnt_attach_dynamic(&sc->sc_ev_rxipsum, EVCNT_TYPE_MISC,
    825 	    NULL, sc->sc_dev.dv_xname, "rxipsum");
    826 	evcnt_attach_dynamic(&sc->sc_ev_rxtcpsum, EVCNT_TYPE_MISC,
    827 	    NULL, sc->sc_dev.dv_xname, "rxtcpsum");
    828 	evcnt_attach_dynamic(&sc->sc_ev_rxudpsum, EVCNT_TYPE_MISC,
    829 	    NULL, sc->sc_dev.dv_xname, "rxudpsum");
    830 	evcnt_attach_dynamic(&sc->sc_ev_txipsum, EVCNT_TYPE_MISC,
    831 	    NULL, sc->sc_dev.dv_xname, "txipsum");
    832 	evcnt_attach_dynamic(&sc->sc_ev_txtcpsum, EVCNT_TYPE_MISC,
    833 	    NULL, sc->sc_dev.dv_xname, "txtcpsum");
    834 	evcnt_attach_dynamic(&sc->sc_ev_txudpsum, EVCNT_TYPE_MISC,
    835 	    NULL, sc->sc_dev.dv_xname, "txudpsum");
    836 #endif /* DP83820 */
    837 #endif /* SIP_EVENT_COUNTERS */
    838 
    839 	/*
    840 	 * Make sure the interface is shutdown during reboot.
    841 	 */
    842 	sc->sc_sdhook = shutdownhook_establish(SIP_DECL(shutdown), sc);
    843 	if (sc->sc_sdhook == NULL)
    844 		printf("%s: WARNING: unable to establish shutdown hook\n",
    845 		    sc->sc_dev.dv_xname);
    846 	return;
    847 
    848 	/*
    849 	 * Free any resources we've allocated during the failed attach
    850 	 * attempt.  Do this in reverse order and fall through.
    851 	 */
    852  fail_5:
    853 	for (i = 0; i < SIP_NRXDESC; i++) {
    854 		if (sc->sc_rxsoft[i].rxs_dmamap != NULL)
    855 			bus_dmamap_destroy(sc->sc_dmat,
    856 			    sc->sc_rxsoft[i].rxs_dmamap);
    857 	}
    858  fail_4:
    859 	for (i = 0; i < SIP_TXQUEUELEN; i++) {
    860 		if (sc->sc_txsoft[i].txs_dmamap != NULL)
    861 			bus_dmamap_destroy(sc->sc_dmat,
    862 			    sc->sc_txsoft[i].txs_dmamap);
    863 	}
    864 	bus_dmamap_unload(sc->sc_dmat, sc->sc_cddmamap);
    865  fail_3:
    866 	bus_dmamap_destroy(sc->sc_dmat, sc->sc_cddmamap);
    867  fail_2:
    868 	bus_dmamem_unmap(sc->sc_dmat, (caddr_t)sc->sc_control_data,
    869 	    sizeof(struct sip_control_data));
    870  fail_1:
    871 	bus_dmamem_free(sc->sc_dmat, &seg, rseg);
    872  fail_0:
    873 	return;
    874 }
    875 
    876 /*
    877  * sip_shutdown:
    878  *
    879  *	Make sure the interface is stopped at reboot time.
    880  */
    881 void
    882 SIP_DECL(shutdown)(void *arg)
    883 {
    884 	struct sip_softc *sc = arg;
    885 
    886 	SIP_DECL(stop)(&sc->sc_ethercom.ec_if, 1);
    887 }
    888 
    889 /*
    890  * sip_start:		[ifnet interface function]
    891  *
    892  *	Start packet transmission on the interface.
    893  */
    894 void
    895 SIP_DECL(start)(struct ifnet *ifp)
    896 {
    897 	struct sip_softc *sc = ifp->if_softc;
    898 	struct mbuf *m0, *m;
    899 	struct sip_txsoft *txs;
    900 	bus_dmamap_t dmamap;
    901 	int error, firsttx, nexttx, lasttx, ofree, seg;
    902 #ifdef DP83820
    903 	u_int32_t extsts;
    904 #endif
    905 
    906 	/*
    907 	 * If we've been told to pause, don't transmit any more packets.
    908 	 */
    909 	if (sc->sc_flags & SIPF_PAUSED)
    910 		ifp->if_flags |= IFF_OACTIVE;
    911 
    912 	if ((ifp->if_flags & (IFF_RUNNING|IFF_OACTIVE)) != IFF_RUNNING)
    913 		return;
    914 
    915 	/*
    916 	 * Remember the previous number of free descriptors and
    917 	 * the first descriptor we'll use.
    918 	 */
    919 	ofree = sc->sc_txfree;
    920 	firsttx = sc->sc_txnext;
    921 
    922 	/*
    923 	 * Loop through the send queue, setting up transmit descriptors
    924 	 * until we drain the queue, or use up all available transmit
    925 	 * descriptors.
    926 	 */
    927 	for (;;) {
    928 		/* Get a work queue entry. */
    929 		if ((txs = SIMPLEQ_FIRST(&sc->sc_txfreeq)) == NULL) {
    930 			SIP_EVCNT_INCR(&sc->sc_ev_txsstall);
    931 			break;
    932 		}
    933 
    934 		/*
    935 		 * Grab a packet off the queue.
    936 		 */
    937 		IFQ_POLL(&ifp->if_snd, m0);
    938 		if (m0 == NULL)
    939 			break;
    940 		m = NULL;
    941 
    942 		dmamap = txs->txs_dmamap;
    943 
    944 		/*
    945 		 * Load the DMA map.  If this fails, the packet either
    946 		 * didn't fit in the alloted number of segments, or we
    947 		 * were short on resources.  In this case, we'll copy
    948 		 * and try again.
    949 		 */
    950 		if (bus_dmamap_load_mbuf(sc->sc_dmat, dmamap, m0,
    951 		    BUS_DMA_NOWAIT) != 0) {
    952 			MGETHDR(m, M_DONTWAIT, MT_DATA);
    953 			if (m == NULL) {
    954 				printf("%s: unable to allocate Tx mbuf\n",
    955 				    sc->sc_dev.dv_xname);
    956 				break;
    957 			}
    958 			if (m0->m_pkthdr.len > MHLEN) {
    959 				MCLGET(m, M_DONTWAIT);
    960 				if ((m->m_flags & M_EXT) == 0) {
    961 					printf("%s: unable to allocate Tx "
    962 					    "cluster\n", sc->sc_dev.dv_xname);
    963 					m_freem(m);
    964 					break;
    965 				}
    966 			}
    967 			m_copydata(m0, 0, m0->m_pkthdr.len, mtod(m, caddr_t));
    968 			m->m_pkthdr.len = m->m_len = m0->m_pkthdr.len;
    969 			error = bus_dmamap_load_mbuf(sc->sc_dmat, dmamap,
    970 			    m, BUS_DMA_NOWAIT);
    971 			if (error) {
    972 				printf("%s: unable to load Tx buffer, "
    973 				    "error = %d\n", sc->sc_dev.dv_xname, error);
    974 				break;
    975 			}
    976 		}
    977 
    978 		/*
    979 		 * Ensure we have enough descriptors free to describe
    980 		 * the packet.  Note, we always reserve one descriptor
    981 		 * at the end of the ring as a termination point, to
    982 		 * prevent wrap-around.
    983 		 */
    984 		if (dmamap->dm_nsegs > (sc->sc_txfree - 1)) {
    985 			/*
    986 			 * Not enough free descriptors to transmit this
    987 			 * packet.  We haven't committed anything yet,
    988 			 * so just unload the DMA map, put the packet
    989 			 * back on the queue, and punt.  Notify the upper
    990 			 * layer that there are not more slots left.
    991 			 *
    992 			 * XXX We could allocate an mbuf and copy, but
    993 			 * XXX is it worth it?
    994 			 */
    995 			ifp->if_flags |= IFF_OACTIVE;
    996 			bus_dmamap_unload(sc->sc_dmat, dmamap);
    997 			if (m != NULL)
    998 				m_freem(m);
    999 			SIP_EVCNT_INCR(&sc->sc_ev_txdstall);
   1000 			break;
   1001 		}
   1002 
   1003 		IFQ_DEQUEUE(&ifp->if_snd, m0);
   1004 		if (m != NULL) {
   1005 			m_freem(m0);
   1006 			m0 = m;
   1007 		}
   1008 
   1009 		/*
   1010 		 * WE ARE NOW COMMITTED TO TRANSMITTING THE PACKET.
   1011 		 */
   1012 
   1013 		/* Sync the DMA map. */
   1014 		bus_dmamap_sync(sc->sc_dmat, dmamap, 0, dmamap->dm_mapsize,
   1015 		    BUS_DMASYNC_PREWRITE);
   1016 
   1017 		/*
   1018 		 * Initialize the transmit descriptors.
   1019 		 */
   1020 		for (nexttx = sc->sc_txnext, seg = 0;
   1021 		     seg < dmamap->dm_nsegs;
   1022 		     seg++, nexttx = SIP_NEXTTX(nexttx)) {
   1023 			/*
   1024 			 * If this is the first descriptor we're
   1025 			 * enqueueing, don't set the OWN bit just
   1026 			 * yet.  That could cause a race condition.
   1027 			 * We'll do it below.
   1028 			 */
   1029 			sc->sc_txdescs[nexttx].sipd_bufptr =
   1030 			    htole32(dmamap->dm_segs[seg].ds_addr);
   1031 			sc->sc_txdescs[nexttx].sipd_cmdsts =
   1032 			    htole32((nexttx == firsttx ? 0 : CMDSTS_OWN) |
   1033 			    CMDSTS_MORE | dmamap->dm_segs[seg].ds_len);
   1034 #ifdef DP83820
   1035 			sc->sc_txdescs[nexttx].sipd_extsts = 0;
   1036 #endif /* DP83820 */
   1037 			lasttx = nexttx;
   1038 		}
   1039 
   1040 		/* Clear the MORE bit on the last segment. */
   1041 		sc->sc_txdescs[lasttx].sipd_cmdsts &= htole32(~CMDSTS_MORE);
   1042 
   1043 #ifdef DP83820
   1044 		/*
   1045 		 * If VLANs are enabled and the packet has a VLAN tag, set
   1046 		 * up the descriptor to encapsulate the packet for us.
   1047 		 *
   1048 		 * This apparently has to be on the last descriptor of
   1049 		 * the packet.
   1050 		 */
   1051 		if (sc->sc_ethercom.ec_nvlans != 0 &&
   1052 		    (m = m_aux_find(m0, AF_LINK, ETHERTYPE_VLAN)) != NULL) {
   1053 			sc->sc_txdescs[lasttx].sipd_extsts |=
   1054 			    htole32(EXTSTS_VPKT |
   1055 				    htons(*mtod(m, int *) & EXTSTS_VTCI));
   1056 		}
   1057 
   1058 		/*
   1059 		 * If the upper-layer has requested IPv4/TCPv4/UDPv4
   1060 		 * checksumming, set up the descriptor to do this work
   1061 		 * for us.
   1062 		 *
   1063 		 * This apparently has to be on the first descriptor of
   1064 		 * the packet.
   1065 		 *
   1066 		 * Byte-swap constants so the compiler can optimize.
   1067 		 */
   1068 		extsts = 0;
   1069 		if (m0->m_pkthdr.csum_flags & M_CSUM_IPv4) {
   1070 			KDASSERT(ifp->if_capenable & IFCAP_CSUM_IPv4);
   1071 			SIP_EVCNT_INCR(&sc->sc_ev_txipsum);
   1072 			extsts |= htole32(EXTSTS_IPPKT);
   1073 		}
   1074 		if (m0->m_pkthdr.csum_flags & M_CSUM_TCPv4) {
   1075 			KDASSERT(ifp->if_capenable & IFCAP_CSUM_TCPv4);
   1076 			SIP_EVCNT_INCR(&sc->sc_ev_txtcpsum);
   1077 			extsts |= htole32(EXTSTS_TCPPKT);
   1078 		} else if (m0->m_pkthdr.csum_flags & M_CSUM_UDPv4) {
   1079 			KDASSERT(ifp->if_capenable & IFCAP_CSUM_UDPv4);
   1080 			SIP_EVCNT_INCR(&sc->sc_ev_txudpsum);
   1081 			extsts |= htole32(EXTSTS_UDPPKT);
   1082 		}
   1083 		sc->sc_txdescs[sc->sc_txnext].sipd_extsts |= extsts;
   1084 #endif /* DP83820 */
   1085 
   1086 		/* Sync the descriptors we're using. */
   1087 		SIP_CDTXSYNC(sc, sc->sc_txnext, dmamap->dm_nsegs,
   1088 		    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
   1089 
   1090 		/*
   1091 		 * Store a pointer to the packet so we can free it later,
   1092 		 * and remember what txdirty will be once the packet is
   1093 		 * done.
   1094 		 */
   1095 		txs->txs_mbuf = m0;
   1096 		txs->txs_firstdesc = sc->sc_txnext;
   1097 		txs->txs_lastdesc = lasttx;
   1098 
   1099 		/* Advance the tx pointer. */
   1100 		sc->sc_txfree -= dmamap->dm_nsegs;
   1101 		sc->sc_txnext = nexttx;
   1102 
   1103 		SIMPLEQ_REMOVE_HEAD(&sc->sc_txfreeq, txs, txs_q);
   1104 		SIMPLEQ_INSERT_TAIL(&sc->sc_txdirtyq, txs, txs_q);
   1105 
   1106 #if NBPFILTER > 0
   1107 		/*
   1108 		 * Pass the packet to any BPF listeners.
   1109 		 */
   1110 		if (ifp->if_bpf)
   1111 			bpf_mtap(ifp->if_bpf, m0);
   1112 #endif /* NBPFILTER > 0 */
   1113 	}
   1114 
   1115 	if (txs == NULL || sc->sc_txfree == 0) {
   1116 		/* No more slots left; notify upper layer. */
   1117 		ifp->if_flags |= IFF_OACTIVE;
   1118 	}
   1119 
   1120 	if (sc->sc_txfree != ofree) {
   1121 		/*
   1122 		 * Cause a descriptor interrupt to happen on the
   1123 		 * last packet we enqueued.
   1124 		 */
   1125 		sc->sc_txdescs[lasttx].sipd_cmdsts |= htole32(CMDSTS_INTR);
   1126 		SIP_CDTXSYNC(sc, lasttx, 1,
   1127 		    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
   1128 
   1129 		/*
   1130 		 * The entire packet chain is set up.  Give the
   1131 		 * first descrptor to the chip now.
   1132 		 */
   1133 		sc->sc_txdescs[firsttx].sipd_cmdsts |= htole32(CMDSTS_OWN);
   1134 		SIP_CDTXSYNC(sc, firsttx, 1,
   1135 		    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
   1136 
   1137 		/*
   1138 		 * Start the transmit process.  Note, the manual says
   1139 		 * that if there are no pending transmissions in the
   1140 		 * chip's internal queue (indicated by TXE being clear),
   1141 		 * then the driver software must set the TXDP to the
   1142 		 * first descriptor to be transmitted.  However, if we
   1143 		 * do this, it causes serious performance degredation on
   1144 		 * the DP83820 under load, not setting TXDP doesn't seem
   1145 		 * to adversely affect the SiS 900 or DP83815.
   1146 		 *
   1147 		 * Well, I guess it wouldn't be the first time a manual
   1148 		 * has lied -- and they could be speaking of the NULL-
   1149 		 * terminated descriptor list case, rather than OWN-
   1150 		 * terminated rings.
   1151 		 */
   1152 #if 0
   1153 		if ((bus_space_read_4(sc->sc_st, sc->sc_sh, SIP_CR) &
   1154 		     CR_TXE) == 0) {
   1155 			bus_space_write_4(sc->sc_st, sc->sc_sh, SIP_TXDP,
   1156 			    SIP_CDTXADDR(sc, firsttx));
   1157 			bus_space_write_4(sc->sc_st, sc->sc_sh, SIP_CR, CR_TXE);
   1158 		}
   1159 #else
   1160 		bus_space_write_4(sc->sc_st, sc->sc_sh, SIP_CR, CR_TXE);
   1161 #endif
   1162 
   1163 		/* Set a watchdog timer in case the chip flakes out. */
   1164 		ifp->if_timer = 5;
   1165 	}
   1166 }
   1167 
   1168 /*
   1169  * sip_watchdog:	[ifnet interface function]
   1170  *
   1171  *	Watchdog timer handler.
   1172  */
   1173 void
   1174 SIP_DECL(watchdog)(struct ifnet *ifp)
   1175 {
   1176 	struct sip_softc *sc = ifp->if_softc;
   1177 
   1178 	/*
   1179 	 * The chip seems to ignore the CMDSTS_INTR bit sometimes!
   1180 	 * If we get a timeout, try and sweep up transmit descriptors.
   1181 	 * If we manage to sweep them all up, ignore the lack of
   1182 	 * interrupt.
   1183 	 */
   1184 	SIP_DECL(txintr)(sc);
   1185 
   1186 	if (sc->sc_txfree != SIP_NTXDESC) {
   1187 		printf("%s: device timeout\n", sc->sc_dev.dv_xname);
   1188 		ifp->if_oerrors++;
   1189 
   1190 		/* Reset the interface. */
   1191 		(void) SIP_DECL(init)(ifp);
   1192 	} else if (ifp->if_flags & IFF_DEBUG)
   1193 		printf("%s: recovered from device timeout\n",
   1194 		    sc->sc_dev.dv_xname);
   1195 
   1196 	/* Try to get more packets going. */
   1197 	SIP_DECL(start)(ifp);
   1198 }
   1199 
   1200 /*
   1201  * sip_ioctl:		[ifnet interface function]
   1202  *
   1203  *	Handle control requests from the operator.
   1204  */
   1205 int
   1206 SIP_DECL(ioctl)(struct ifnet *ifp, u_long cmd, caddr_t data)
   1207 {
   1208 	struct sip_softc *sc = ifp->if_softc;
   1209 	struct ifreq *ifr = (struct ifreq *)data;
   1210 	int s, error;
   1211 
   1212 	s = splnet();
   1213 
   1214 	switch (cmd) {
   1215 	case SIOCSIFMEDIA:
   1216 	case SIOCGIFMEDIA:
   1217 		error = ifmedia_ioctl(ifp, ifr, &sc->sc_mii.mii_media, cmd);
   1218 		break;
   1219 
   1220 	default:
   1221 		error = ether_ioctl(ifp, cmd, data);
   1222 		if (error == ENETRESET) {
   1223 			/*
   1224 			 * Multicast list has changed; set the hardware filter
   1225 			 * accordingly.
   1226 			 */
   1227 			(*sc->sc_model->sip_variant->sipv_set_filter)(sc);
   1228 			error = 0;
   1229 		}
   1230 		break;
   1231 	}
   1232 
   1233 	/* Try to get more packets going. */
   1234 	SIP_DECL(start)(ifp);
   1235 
   1236 	splx(s);
   1237 	return (error);
   1238 }
   1239 
   1240 /*
   1241  * sip_intr:
   1242  *
   1243  *	Interrupt service routine.
   1244  */
   1245 int
   1246 SIP_DECL(intr)(void *arg)
   1247 {
   1248 	struct sip_softc *sc = arg;
   1249 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1250 	u_int32_t isr;
   1251 	int handled = 0;
   1252 
   1253 	for (;;) {
   1254 		/* Reading clears interrupt. */
   1255 		isr = bus_space_read_4(sc->sc_st, sc->sc_sh, SIP_ISR);
   1256 		if ((isr & sc->sc_imr) == 0)
   1257 			break;
   1258 
   1259 		handled = 1;
   1260 
   1261 		if (isr & (ISR_RXORN|ISR_RXIDLE|ISR_RXDESC)) {
   1262 			SIP_EVCNT_INCR(&sc->sc_ev_rxintr);
   1263 
   1264 			/* Grab any new packets. */
   1265 			SIP_DECL(rxintr)(sc);
   1266 
   1267 			if (isr & ISR_RXORN) {
   1268 				printf("%s: receive FIFO overrun\n",
   1269 				    sc->sc_dev.dv_xname);
   1270 
   1271 				/* XXX adjust rx_drain_thresh? */
   1272 			}
   1273 
   1274 			if (isr & ISR_RXIDLE) {
   1275 				printf("%s: receive ring overrun\n",
   1276 				    sc->sc_dev.dv_xname);
   1277 
   1278 				/* Get the receive process going again. */
   1279 				bus_space_write_4(sc->sc_st, sc->sc_sh,
   1280 				    SIP_RXDP, SIP_CDRXADDR(sc, sc->sc_rxptr));
   1281 				bus_space_write_4(sc->sc_st, sc->sc_sh,
   1282 				    SIP_CR, CR_RXE);
   1283 			}
   1284 		}
   1285 
   1286 		if (isr & (ISR_TXURN|ISR_TXDESC)) {
   1287 			SIP_EVCNT_INCR(&sc->sc_ev_txintr);
   1288 
   1289 			/* Sweep up transmit descriptors. */
   1290 			SIP_DECL(txintr)(sc);
   1291 
   1292 			if (isr & ISR_TXURN) {
   1293 				u_int32_t thresh;
   1294 
   1295 				printf("%s: transmit FIFO underrun",
   1296 				    sc->sc_dev.dv_xname);
   1297 
   1298 				thresh = sc->sc_tx_drain_thresh + 1;
   1299 				if (thresh <= TXCFG_DRTH &&
   1300 				    (thresh * 32) <= (SIP_TXFIFO_SIZE -
   1301 				     (sc->sc_tx_fill_thresh * 32))) {
   1302 					printf("; increasing Tx drain "
   1303 					    "threshold to %u bytes\n",
   1304 					    thresh * 32);
   1305 					sc->sc_tx_drain_thresh = thresh;
   1306 					(void) SIP_DECL(init)(ifp);
   1307 				} else {
   1308 					(void) SIP_DECL(init)(ifp);
   1309 					printf("\n");
   1310 				}
   1311 			}
   1312 		}
   1313 
   1314 #if !defined(DP83820)
   1315 		if (sc->sc_imr & (ISR_PAUSE_END|ISR_PAUSE_ST)) {
   1316 			if (isr & ISR_PAUSE_ST) {
   1317 				sc->sc_flags |= SIPF_PAUSED;
   1318 				ifp->if_flags |= IFF_OACTIVE;
   1319 			}
   1320 			if (isr & ISR_PAUSE_END) {
   1321 				sc->sc_flags &= ~SIPF_PAUSED;
   1322 				ifp->if_flags &= ~IFF_OACTIVE;
   1323 			}
   1324 		}
   1325 #endif /* ! DP83820 */
   1326 
   1327 		if (isr & ISR_HIBERR) {
   1328 #define	PRINTERR(bit, str)						\
   1329 			if (isr & (bit))				\
   1330 				printf("%s: %s\n", sc->sc_dev.dv_xname, str)
   1331 			PRINTERR(ISR_DPERR, "parity error");
   1332 			PRINTERR(ISR_SSERR, "system error");
   1333 			PRINTERR(ISR_RMABT, "master abort");
   1334 			PRINTERR(ISR_RTABT, "target abort");
   1335 			PRINTERR(ISR_RXSOVR, "receive status FIFO overrun");
   1336 			(void) SIP_DECL(init)(ifp);
   1337 #undef PRINTERR
   1338 		}
   1339 	}
   1340 
   1341 	/* Try to get more packets going. */
   1342 	SIP_DECL(start)(ifp);
   1343 
   1344 	return (handled);
   1345 }
   1346 
   1347 /*
   1348  * sip_txintr:
   1349  *
   1350  *	Helper; handle transmit interrupts.
   1351  */
   1352 void
   1353 SIP_DECL(txintr)(struct sip_softc *sc)
   1354 {
   1355 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1356 	struct sip_txsoft *txs;
   1357 	u_int32_t cmdsts;
   1358 
   1359 	if ((sc->sc_flags & SIPF_PAUSED) == 0)
   1360 		ifp->if_flags &= ~IFF_OACTIVE;
   1361 
   1362 	/*
   1363 	 * Go through our Tx list and free mbufs for those
   1364 	 * frames which have been transmitted.
   1365 	 */
   1366 	while ((txs = SIMPLEQ_FIRST(&sc->sc_txdirtyq)) != NULL) {
   1367 		SIP_CDTXSYNC(sc, txs->txs_firstdesc, txs->txs_dmamap->dm_nsegs,
   1368 		    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
   1369 
   1370 		cmdsts = le32toh(sc->sc_txdescs[txs->txs_lastdesc].sipd_cmdsts);
   1371 		if (cmdsts & CMDSTS_OWN)
   1372 			break;
   1373 
   1374 		SIMPLEQ_REMOVE_HEAD(&sc->sc_txdirtyq, txs, txs_q);
   1375 
   1376 		sc->sc_txfree += txs->txs_dmamap->dm_nsegs;
   1377 
   1378 		bus_dmamap_sync(sc->sc_dmat, txs->txs_dmamap,
   1379 		    0, txs->txs_dmamap->dm_mapsize, BUS_DMASYNC_POSTWRITE);
   1380 		bus_dmamap_unload(sc->sc_dmat, txs->txs_dmamap);
   1381 		m_freem(txs->txs_mbuf);
   1382 		txs->txs_mbuf = NULL;
   1383 
   1384 		SIMPLEQ_INSERT_TAIL(&sc->sc_txfreeq, txs, txs_q);
   1385 
   1386 		/*
   1387 		 * Check for errors and collisions.
   1388 		 */
   1389 		if (cmdsts &
   1390 		    (CMDSTS_Tx_TXA|CMDSTS_Tx_TFU|CMDSTS_Tx_ED|CMDSTS_Tx_EC)) {
   1391 			ifp->if_oerrors++;
   1392 			if (cmdsts & CMDSTS_Tx_EC)
   1393 				ifp->if_collisions += 16;
   1394 			if (ifp->if_flags & IFF_DEBUG) {
   1395 				if (cmdsts & CMDSTS_Tx_ED)
   1396 					printf("%s: excessive deferral\n",
   1397 					    sc->sc_dev.dv_xname);
   1398 				if (cmdsts & CMDSTS_Tx_EC)
   1399 					printf("%s: excessive collisions\n",
   1400 					    sc->sc_dev.dv_xname);
   1401 			}
   1402 		} else {
   1403 			/* Packet was transmitted successfully. */
   1404 			ifp->if_opackets++;
   1405 			ifp->if_collisions += CMDSTS_COLLISIONS(cmdsts);
   1406 		}
   1407 	}
   1408 
   1409 	/*
   1410 	 * If there are no more pending transmissions, cancel the watchdog
   1411 	 * timer.
   1412 	 */
   1413 	if (txs == NULL)
   1414 		ifp->if_timer = 0;
   1415 }
   1416 
   1417 /*
   1418  * sip_rxintr:
   1419  *
   1420  *	Helper; handle receive interrupts.
   1421  */
   1422 void
   1423 SIP_DECL(rxintr)(struct sip_softc *sc)
   1424 {
   1425 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1426 	struct sip_rxsoft *rxs;
   1427 	struct mbuf *m;
   1428 	u_int32_t cmdsts;
   1429 #ifdef DP83820
   1430 	u_int32_t extsts;
   1431 #endif /* DP83820 */
   1432 	int i, len;
   1433 
   1434 	for (i = sc->sc_rxptr;; i = SIP_NEXTRX(i)) {
   1435 		rxs = &sc->sc_rxsoft[i];
   1436 
   1437 		SIP_CDRXSYNC(sc, i, BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
   1438 
   1439 		cmdsts = le32toh(sc->sc_rxdescs[i].sipd_cmdsts);
   1440 #ifdef DP83820
   1441 		extsts = le32toh(sc->sc_rxdescs[i].sipd_extsts);
   1442 #endif /* DP83820 */
   1443 
   1444 		/*
   1445 		 * NOTE: OWN is set if owned by _consumer_.  We're the
   1446 		 * consumer of the receive ring, so if the bit is clear,
   1447 		 * we have processed all of the packets.
   1448 		 */
   1449 		if ((cmdsts & CMDSTS_OWN) == 0) {
   1450 			/*
   1451 			 * We have processed all of the receive buffers.
   1452 			 */
   1453 			break;
   1454 		}
   1455 
   1456 #if !defined(DP83820)
   1457 		/*
   1458 		 * If any collisions were seen on the wire, count one.
   1459 		 */
   1460 		if (cmdsts & CMDSTS_Rx_COL)
   1461 			ifp->if_collisions++;
   1462 #endif /* ! DP83820 */
   1463 
   1464 		/*
   1465 		 * If an error occurred, update stats, clear the status
   1466 		 * word, and leave the packet buffer in place.  It will
   1467 		 * simply be reused the next time the ring comes around.
   1468 		 */
   1469 		if (cmdsts & (CMDSTS_Rx_RXA|CMDSTS_Rx_LONG|CMDSTS_Rx_RUNT|
   1470 		    CMDSTS_Rx_ISE|CMDSTS_Rx_CRCE|CMDSTS_Rx_FAE)) {
   1471 			ifp->if_ierrors++;
   1472 			if ((cmdsts & CMDSTS_Rx_RXA) != 0 &&
   1473 			    (cmdsts & CMDSTS_Rx_RXO) == 0) {
   1474 				/* Receive overrun handled elsewhere. */
   1475 				printf("%s: receive descriptor error\n",
   1476 				    sc->sc_dev.dv_xname);
   1477 			}
   1478 #define	PRINTERR(bit, str)						\
   1479 			if (cmdsts & (bit))				\
   1480 				printf("%s: %s\n", sc->sc_dev.dv_xname, str)
   1481 			PRINTERR(CMDSTS_Rx_LONG, "packet too long");
   1482 			PRINTERR(CMDSTS_Rx_RUNT, "runt packet");
   1483 			PRINTERR(CMDSTS_Rx_ISE, "invalid symbol error");
   1484 			PRINTERR(CMDSTS_Rx_CRCE, "CRC error");
   1485 			PRINTERR(CMDSTS_Rx_FAE, "frame alignment error");
   1486 #undef PRINTERR
   1487 			SIP_INIT_RXDESC(sc, i);
   1488 			continue;
   1489 		}
   1490 
   1491 		bus_dmamap_sync(sc->sc_dmat, rxs->rxs_dmamap, 0,
   1492 		    rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_POSTREAD);
   1493 
   1494 		/*
   1495 		 * No errors; receive the packet.  Note, the SiS 900
   1496 		 * includes the CRC with every packet.
   1497 		 */
   1498 		len = CMDSTS_SIZE(cmdsts);
   1499 
   1500 #ifdef __NO_STRICT_ALIGNMENT
   1501 		/*
   1502 		 * If the packet is small enough to fit in a
   1503 		 * single header mbuf, allocate one and copy
   1504 		 * the data into it.  This greatly reduces
   1505 		 * memory consumption when we receive lots
   1506 		 * of small packets.
   1507 		 *
   1508 		 * Otherwise, we add a new buffer to the receive
   1509 		 * chain.  If this fails, we drop the packet and
   1510 		 * recycle the old buffer.
   1511 		 */
   1512 		if (SIP_DECL(copy_small) != 0 && len <= MHLEN) {
   1513 			MGETHDR(m, M_DONTWAIT, MT_DATA);
   1514 			if (m == NULL)
   1515 				goto dropit;
   1516 			memcpy(mtod(m, caddr_t),
   1517 			    mtod(rxs->rxs_mbuf, caddr_t), len);
   1518 			SIP_INIT_RXDESC(sc, i);
   1519 			bus_dmamap_sync(sc->sc_dmat, rxs->rxs_dmamap, 0,
   1520 			    rxs->rxs_dmamap->dm_mapsize,
   1521 			    BUS_DMASYNC_PREREAD);
   1522 		} else {
   1523 			m = rxs->rxs_mbuf;
   1524 			if (SIP_DECL(add_rxbuf)(sc, i) != 0) {
   1525  dropit:
   1526 				ifp->if_ierrors++;
   1527 				SIP_INIT_RXDESC(sc, i);
   1528 				bus_dmamap_sync(sc->sc_dmat,
   1529 				    rxs->rxs_dmamap, 0,
   1530 				    rxs->rxs_dmamap->dm_mapsize,
   1531 				    BUS_DMASYNC_PREREAD);
   1532 				continue;
   1533 			}
   1534 		}
   1535 #else
   1536 		/*
   1537 		 * The SiS 900's receive buffers must be 4-byte aligned.
   1538 		 * But this means that the data after the Ethernet header
   1539 		 * is misaligned.  We must allocate a new buffer and
   1540 		 * copy the data, shifted forward 2 bytes.
   1541 		 */
   1542 		MGETHDR(m, M_DONTWAIT, MT_DATA);
   1543 		if (m == NULL) {
   1544  dropit:
   1545 			ifp->if_ierrors++;
   1546 			SIP_INIT_RXDESC(sc, i);
   1547 			bus_dmamap_sync(sc->sc_dmat, rxs->rxs_dmamap, 0,
   1548 			    rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
   1549 			continue;
   1550 		}
   1551 		if (len > (MHLEN - 2)) {
   1552 			MCLGET(m, M_DONTWAIT);
   1553 			if ((m->m_flags & M_EXT) == 0) {
   1554 				m_freem(m);
   1555 				goto dropit;
   1556 			}
   1557 		}
   1558 		m->m_data += 2;
   1559 
   1560 		/*
   1561 		 * Note that we use clusters for incoming frames, so the
   1562 		 * buffer is virtually contiguous.
   1563 		 */
   1564 		memcpy(mtod(m, caddr_t), mtod(rxs->rxs_mbuf, caddr_t), len);
   1565 
   1566 		/* Allow the receive descriptor to continue using its mbuf. */
   1567 		SIP_INIT_RXDESC(sc, i);
   1568 		bus_dmamap_sync(sc->sc_dmat, rxs->rxs_dmamap, 0,
   1569 		    rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
   1570 #endif /* __NO_STRICT_ALIGNMENT */
   1571 
   1572 		ifp->if_ipackets++;
   1573 		m->m_flags |= M_HASFCS;
   1574 		m->m_pkthdr.rcvif = ifp;
   1575 		m->m_pkthdr.len = m->m_len = len;
   1576 
   1577 #if NBPFILTER > 0
   1578 		/*
   1579 		 * Pass this up to any BPF listeners, but only
   1580 		 * pass if up the stack if it's for us.
   1581 		 */
   1582 		if (ifp->if_bpf)
   1583 			bpf_mtap(ifp->if_bpf, m);
   1584 #endif /* NBPFILTER > 0 */
   1585 
   1586 #ifdef DP83820
   1587 		/*
   1588 		 * If VLANs are enabled, VLAN packets have been unwrapped
   1589 		 * for us.  Associate the tag with the packet.
   1590 		 */
   1591 		if (sc->sc_ethercom.ec_nvlans != 0 &&
   1592 		    (extsts & EXTSTS_VPKT) != 0) {
   1593 			struct mbuf *vtag;
   1594 
   1595 			vtag = m_aux_add(m, AF_LINK, ETHERTYPE_VLAN);
   1596 			if (vtag == NULL) {
   1597 				printf("%s: unable to allocate VLAN tag\n",
   1598 				    sc->sc_dev.dv_xname);
   1599 				m_freem(m);
   1600 				continue;
   1601 			}
   1602 
   1603 			*mtod(vtag, int *) = ntohs(extsts & EXTSTS_VTCI);
   1604 			vtag->m_len = sizeof(int);
   1605 		}
   1606 
   1607 		/*
   1608 		 * Set the incoming checksum information for the
   1609 		 * packet.
   1610 		 */
   1611 		if ((extsts & EXTSTS_IPPKT) != 0) {
   1612 			SIP_EVCNT_INCR(&sc->sc_ev_rxipsum);
   1613 			m->m_pkthdr.csum_flags |= M_CSUM_IPv4;
   1614 			if (extsts & EXTSTS_Rx_IPERR)
   1615 				m->m_pkthdr.csum_flags |= M_CSUM_IPv4_BAD;
   1616 			if (extsts & EXTSTS_TCPPKT) {
   1617 				SIP_EVCNT_INCR(&sc->sc_ev_rxtcpsum);
   1618 				m->m_pkthdr.csum_flags |= M_CSUM_TCPv4;
   1619 				if (extsts & EXTSTS_Rx_TCPERR)
   1620 					m->m_pkthdr.csum_flags |=
   1621 					    M_CSUM_TCP_UDP_BAD;
   1622 			} else if (extsts & EXTSTS_UDPPKT) {
   1623 				SIP_EVCNT_INCR(&sc->sc_ev_rxudpsum);
   1624 				m->m_pkthdr.csum_flags |= M_CSUM_UDPv4;
   1625 				if (extsts & EXTSTS_Rx_UDPERR)
   1626 					m->m_pkthdr.csum_flags |=
   1627 					    M_CSUM_TCP_UDP_BAD;
   1628 			}
   1629 		}
   1630 #endif /* DP83820 */
   1631 
   1632 		/* Pass it on. */
   1633 		(*ifp->if_input)(ifp, m);
   1634 	}
   1635 
   1636 	/* Update the receive pointer. */
   1637 	sc->sc_rxptr = i;
   1638 }
   1639 
   1640 /*
   1641  * sip_tick:
   1642  *
   1643  *	One second timer, used to tick the MII.
   1644  */
   1645 void
   1646 SIP_DECL(tick)(void *arg)
   1647 {
   1648 	struct sip_softc *sc = arg;
   1649 	int s;
   1650 
   1651 	s = splnet();
   1652 	mii_tick(&sc->sc_mii);
   1653 	splx(s);
   1654 
   1655 	callout_reset(&sc->sc_tick_ch, hz, SIP_DECL(tick), sc);
   1656 }
   1657 
   1658 /*
   1659  * sip_reset:
   1660  *
   1661  *	Perform a soft reset on the SiS 900.
   1662  */
   1663 void
   1664 SIP_DECL(reset)(struct sip_softc *sc)
   1665 {
   1666 	bus_space_tag_t st = sc->sc_st;
   1667 	bus_space_handle_t sh = sc->sc_sh;
   1668 	int i;
   1669 
   1670 	bus_space_write_4(st, sh, SIP_CR, CR_RST);
   1671 
   1672 	for (i = 0; i < SIP_TIMEOUT; i++) {
   1673 		if ((bus_space_read_4(st, sh, SIP_CR) & CR_RST) == 0)
   1674 			break;
   1675 		delay(2);
   1676 	}
   1677 
   1678 	if (i == SIP_TIMEOUT)
   1679 		printf("%s: reset failed to complete\n", sc->sc_dev.dv_xname);
   1680 
   1681 	delay(1000);
   1682 
   1683 #ifdef DP83820
   1684 	/*
   1685 	 * Set the general purpose I/O bits.  Do it here in case we
   1686 	 * need to have GPIO set up to talk to the media interface.
   1687 	 */
   1688 	bus_space_write_4(st, sh, SIP_GPIOR, sc->sc_gpior);
   1689 	delay(1000);
   1690 #endif /* DP83820 */
   1691 }
   1692 
   1693 /*
   1694  * sip_init:		[ ifnet interface function ]
   1695  *
   1696  *	Initialize the interface.  Must be called at splnet().
   1697  */
   1698 int
   1699 SIP_DECL(init)(struct ifnet *ifp)
   1700 {
   1701 	struct sip_softc *sc = ifp->if_softc;
   1702 	bus_space_tag_t st = sc->sc_st;
   1703 	bus_space_handle_t sh = sc->sc_sh;
   1704 	struct sip_txsoft *txs;
   1705 	struct sip_rxsoft *rxs;
   1706 	struct sip_desc *sipd;
   1707 	u_int32_t reg;
   1708 	int i, error = 0;
   1709 
   1710 	/*
   1711 	 * Cancel any pending I/O.
   1712 	 */
   1713 	SIP_DECL(stop)(ifp, 0);
   1714 
   1715 	/*
   1716 	 * Reset the chip to a known state.
   1717 	 */
   1718 	SIP_DECL(reset)(sc);
   1719 
   1720 #if !defined(DP83820)
   1721 	if (sc->sc_model->sip_vendor == PCI_VENDOR_NS &&
   1722 	    sc->sc_model->sip_product == PCI_PRODUCT_NS_DP83815) {
   1723 		/*
   1724 		 * DP83815 manual, page 78:
   1725 		 *    4.4 Recommended Registers Configuration
   1726 		 *    For optimum performance of the DP83815, version noted
   1727 		 *    as DP83815CVNG (SRR = 203h), the listed register
   1728 		 *    modifications must be followed in sequence...
   1729 		 *
   1730 		 * It's not clear if this should be 302h or 203h because that
   1731 		 * chip name is listed as SRR 302h in the description of the
   1732 		 * SRR register.  However, my revision 302h DP83815 on the
   1733 		 * Netgear FA311 purchased in 02/2001 needs these settings
   1734 		 * to avoid tons of errors in AcceptPerfectMatch (non-
   1735 		 * IFF_PROMISC) mode.  I do not know if other revisions need
   1736 		 * this set or not.  [briggs -- 09 March 2001]
   1737 		 *
   1738 		 * Note that only the low-order 12 bits of 0xe4 are documented
   1739 		 * and that this sets reserved bits in that register.
   1740 		 */
   1741 		reg = bus_space_read_4(st, sh, SIP_NS_SRR);
   1742 		if (reg == 0x302) {
   1743 			bus_space_write_4(st, sh, 0x00cc, 0x0001);
   1744 			bus_space_write_4(st, sh, 0x00e4, 0x189C);
   1745 			bus_space_write_4(st, sh, 0x00fc, 0x0000);
   1746 			bus_space_write_4(st, sh, 0x00f4, 0x5040);
   1747 			bus_space_write_4(st, sh, 0x00f8, 0x008c);
   1748 		}
   1749 	}
   1750 #endif /* ! DP83820 */
   1751 
   1752 	/*
   1753 	 * Initialize the transmit descriptor ring.
   1754 	 */
   1755 	for (i = 0; i < SIP_NTXDESC; i++) {
   1756 		sipd = &sc->sc_txdescs[i];
   1757 		memset(sipd, 0, sizeof(struct sip_desc));
   1758 		sipd->sipd_link = htole32(SIP_CDTXADDR(sc, SIP_NEXTTX(i)));
   1759 	}
   1760 	SIP_CDTXSYNC(sc, 0, SIP_NTXDESC,
   1761 	    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
   1762 	sc->sc_txfree = SIP_NTXDESC;
   1763 	sc->sc_txnext = 0;
   1764 
   1765 	/*
   1766 	 * Initialize the transmit job descriptors.
   1767 	 */
   1768 	SIMPLEQ_INIT(&sc->sc_txfreeq);
   1769 	SIMPLEQ_INIT(&sc->sc_txdirtyq);
   1770 	for (i = 0; i < SIP_TXQUEUELEN; i++) {
   1771 		txs = &sc->sc_txsoft[i];
   1772 		txs->txs_mbuf = NULL;
   1773 		SIMPLEQ_INSERT_TAIL(&sc->sc_txfreeq, txs, txs_q);
   1774 	}
   1775 
   1776 	/*
   1777 	 * Initialize the receive descriptor and receive job
   1778 	 * descriptor rings.
   1779 	 */
   1780 	for (i = 0; i < SIP_NRXDESC; i++) {
   1781 		rxs = &sc->sc_rxsoft[i];
   1782 		if (rxs->rxs_mbuf == NULL) {
   1783 			if ((error = SIP_DECL(add_rxbuf)(sc, i)) != 0) {
   1784 				printf("%s: unable to allocate or map rx "
   1785 				    "buffer %d, error = %d\n",
   1786 				    sc->sc_dev.dv_xname, i, error);
   1787 				/*
   1788 				 * XXX Should attempt to run with fewer receive
   1789 				 * XXX buffers instead of just failing.
   1790 				 */
   1791 				SIP_DECL(rxdrain)(sc);
   1792 				goto out;
   1793 			}
   1794 		}
   1795 	}
   1796 	sc->sc_rxptr = 0;
   1797 
   1798 	/*
   1799 	 * Set the configuration register; it's already initialized
   1800 	 * in sip_attach().
   1801 	 */
   1802 	bus_space_write_4(st, sh, SIP_CFG, sc->sc_cfg);
   1803 
   1804 	/*
   1805 	 * Initialize the transmit fill and drain thresholds if
   1806 	 * we have never done so.
   1807 	 */
   1808 	if (sc->sc_tx_fill_thresh == 0) {
   1809 		/*
   1810 		 * XXX This value should be tuned.  This is the
   1811 		 * minimum (32 bytes), and we may be able to
   1812 		 * improve performance by increasing it.
   1813 		 */
   1814 		sc->sc_tx_fill_thresh = 1;
   1815 	}
   1816 	if (sc->sc_tx_drain_thresh == 0) {
   1817 		/*
   1818 		 * Start at a drain threshold of 512 bytes.  We will
   1819 		 * increase it if a DMA underrun occurs.
   1820 		 *
   1821 		 * XXX The minimum value of this variable should be
   1822 		 * tuned.  We may be able to improve performance
   1823 		 * by starting with a lower value.  That, however,
   1824 		 * may trash the first few outgoing packets if the
   1825 		 * PCI bus is saturated.
   1826 		 */
   1827 		sc->sc_tx_drain_thresh = 512 / 32;
   1828 	}
   1829 
   1830 	/*
   1831 	 * Initialize the prototype TXCFG register.
   1832 	 */
   1833 	sc->sc_txcfg = TXCFG_ATP | TXCFG_MXDMA_512 |
   1834 	    (sc->sc_tx_fill_thresh << TXCFG_FLTH_SHIFT) |
   1835 	    sc->sc_tx_drain_thresh;
   1836 	bus_space_write_4(st, sh, SIP_TXCFG, sc->sc_txcfg);
   1837 
   1838 	/*
   1839 	 * Initialize the receive drain threshold if we have never
   1840 	 * done so.
   1841 	 */
   1842 	if (sc->sc_rx_drain_thresh == 0) {
   1843 		/*
   1844 		 * XXX This value should be tuned.  This is set to the
   1845 		 * maximum of 248 bytes, and we may be able to improve
   1846 		 * performance by decreasing it (although we should never
   1847 		 * set this value lower than 2; 14 bytes are required to
   1848 		 * filter the packet).
   1849 		 */
   1850 		sc->sc_rx_drain_thresh = RXCFG_DRTH >> RXCFG_DRTH_SHIFT;
   1851 	}
   1852 
   1853 	/*
   1854 	 * Initialize the prototype RXCFG register.
   1855 	 */
   1856 	sc->sc_rxcfg = RXCFG_MXDMA_512 |
   1857 	    (sc->sc_rx_drain_thresh << RXCFG_DRTH_SHIFT);
   1858 	bus_space_write_4(st, sh, SIP_RXCFG, sc->sc_rxcfg);
   1859 
   1860 	/* Set up the receive filter. */
   1861 	(*sc->sc_model->sip_variant->sipv_set_filter)(sc);
   1862 
   1863 #ifdef DP83820
   1864 	/*
   1865 	 * Initialize the VLAN/IP receive control register.
   1866 	 * We enable checksum computation on all incoming
   1867 	 * packets, and do not reject packets w/ bad checksums.
   1868 	 */
   1869 	reg = 0;
   1870 	if (ifp->if_capenable &
   1871 	    (IFCAP_CSUM_IPv4|IFCAP_CSUM_TCPv4|IFCAP_CSUM_UDPv4))
   1872 		reg |= VRCR_IPEN;
   1873 	if (sc->sc_ethercom.ec_nvlans != 0)
   1874 		reg |= VRCR_VTDEN|VRCR_VTREN;
   1875 	bus_space_write_4(st, sh, SIP_VRCR, reg);
   1876 
   1877 	/*
   1878 	 * Initialize the VLAN/IP transmit control register.
   1879 	 * We enable outgoing checksum computation on a
   1880 	 * per-packet basis.
   1881 	 */
   1882 	reg = 0;
   1883 	if (ifp->if_capenable &
   1884 	    (IFCAP_CSUM_IPv4|IFCAP_CSUM_TCPv4|IFCAP_CSUM_UDPv4))
   1885 		reg |= VTCR_PPCHK;
   1886 	if (sc->sc_ethercom.ec_nvlans != 0)
   1887 		reg |= VTCR_VPPTI;
   1888 	bus_space_write_4(st, sh, SIP_VTCR, reg);
   1889 
   1890 	/*
   1891 	 * If we're using VLANs, initialize the VLAN data register.
   1892 	 * To understand why we bswap the VLAN Ethertype, see section
   1893 	 * 4.2.36 of the DP83820 manual.
   1894 	 */
   1895 	if (sc->sc_ethercom.ec_nvlans != 0)
   1896 		bus_space_write_4(st, sh, SIP_VDR, bswap16(ETHERTYPE_VLAN));
   1897 #endif /* DP83820 */
   1898 
   1899 	/*
   1900 	 * Give the transmit and receive rings to the chip.
   1901 	 */
   1902 	bus_space_write_4(st, sh, SIP_TXDP, SIP_CDTXADDR(sc, sc->sc_txnext));
   1903 	bus_space_write_4(st, sh, SIP_RXDP, SIP_CDRXADDR(sc, sc->sc_rxptr));
   1904 
   1905 	/*
   1906 	 * Initialize the interrupt mask.
   1907 	 */
   1908 	sc->sc_imr = ISR_DPERR|ISR_SSERR|ISR_RMABT|ISR_RTABT|ISR_RXSOVR|
   1909 	    ISR_TXURN|ISR_TXDESC|ISR_RXORN|ISR_RXIDLE|ISR_RXDESC;
   1910 	bus_space_write_4(st, sh, SIP_IMR, sc->sc_imr);
   1911 
   1912 	/*
   1913 	 * Set the current media.  Do this after initializing the prototype
   1914 	 * IMR, since sip_mii_statchg() modifies the IMR for 802.3x flow
   1915 	 * control.
   1916 	 */
   1917 	mii_mediachg(&sc->sc_mii);
   1918 
   1919 	/*
   1920 	 * Enable interrupts.
   1921 	 */
   1922 	bus_space_write_4(st, sh, SIP_IER, IER_IE);
   1923 
   1924 	/*
   1925 	 * Start the transmit and receive processes.
   1926 	 */
   1927 	bus_space_write_4(st, sh, SIP_CR, CR_RXE | CR_TXE);
   1928 
   1929 	/*
   1930 	 * Start the one second MII clock.
   1931 	 */
   1932 	callout_reset(&sc->sc_tick_ch, hz, SIP_DECL(tick), sc);
   1933 
   1934 	/*
   1935 	 * ...all done!
   1936 	 */
   1937 	ifp->if_flags |= IFF_RUNNING;
   1938 	ifp->if_flags &= ~IFF_OACTIVE;
   1939 
   1940  out:
   1941 	if (error)
   1942 		printf("%s: interface not running\n", sc->sc_dev.dv_xname);
   1943 	return (error);
   1944 }
   1945 
   1946 /*
   1947  * sip_drain:
   1948  *
   1949  *	Drain the receive queue.
   1950  */
   1951 void
   1952 SIP_DECL(rxdrain)(struct sip_softc *sc)
   1953 {
   1954 	struct sip_rxsoft *rxs;
   1955 	int i;
   1956 
   1957 	for (i = 0; i < SIP_NRXDESC; i++) {
   1958 		rxs = &sc->sc_rxsoft[i];
   1959 		if (rxs->rxs_mbuf != NULL) {
   1960 			bus_dmamap_unload(sc->sc_dmat, rxs->rxs_dmamap);
   1961 			m_freem(rxs->rxs_mbuf);
   1962 			rxs->rxs_mbuf = NULL;
   1963 		}
   1964 	}
   1965 }
   1966 
   1967 /*
   1968  * sip_stop:		[ ifnet interface function ]
   1969  *
   1970  *	Stop transmission on the interface.
   1971  */
   1972 void
   1973 SIP_DECL(stop)(struct ifnet *ifp, int disable)
   1974 {
   1975 	struct sip_softc *sc = ifp->if_softc;
   1976 	bus_space_tag_t st = sc->sc_st;
   1977 	bus_space_handle_t sh = sc->sc_sh;
   1978 	struct sip_txsoft *txs;
   1979 	u_int32_t cmdsts = 0;		/* DEBUG */
   1980 
   1981 	/*
   1982 	 * Stop the one second clock.
   1983 	 */
   1984 	callout_stop(&sc->sc_tick_ch);
   1985 
   1986 	/* Down the MII. */
   1987 	mii_down(&sc->sc_mii);
   1988 
   1989 	/*
   1990 	 * Disable interrupts.
   1991 	 */
   1992 	bus_space_write_4(st, sh, SIP_IER, 0);
   1993 
   1994 	/*
   1995 	 * Stop receiver and transmitter.
   1996 	 */
   1997 	bus_space_write_4(st, sh, SIP_CR, CR_RXD | CR_TXD);
   1998 
   1999 	/*
   2000 	 * Release any queued transmit buffers.
   2001 	 */
   2002 	while ((txs = SIMPLEQ_FIRST(&sc->sc_txdirtyq)) != NULL) {
   2003 		if ((ifp->if_flags & IFF_DEBUG) != 0 &&
   2004 		    SIMPLEQ_NEXT(txs, txs_q) == NULL &&
   2005 		    (le32toh(sc->sc_txdescs[txs->txs_lastdesc].sipd_cmdsts) &
   2006 		     CMDSTS_INTR) == 0)
   2007 			printf("%s: sip_stop: last descriptor does not "
   2008 			    "have INTR bit set\n", sc->sc_dev.dv_xname);
   2009 		SIMPLEQ_REMOVE_HEAD(&sc->sc_txdirtyq, txs, txs_q);
   2010 #ifdef DIAGNOSTIC
   2011 		if (txs->txs_mbuf == NULL) {
   2012 			printf("%s: dirty txsoft with no mbuf chain\n",
   2013 			    sc->sc_dev.dv_xname);
   2014 			panic("sip_stop");
   2015 		}
   2016 #endif
   2017 		cmdsts |=		/* DEBUG */
   2018 		    le32toh(sc->sc_txdescs[txs->txs_lastdesc].sipd_cmdsts);
   2019 		bus_dmamap_unload(sc->sc_dmat, txs->txs_dmamap);
   2020 		m_freem(txs->txs_mbuf);
   2021 		txs->txs_mbuf = NULL;
   2022 		SIMPLEQ_INSERT_TAIL(&sc->sc_txfreeq, txs, txs_q);
   2023 	}
   2024 
   2025 	if (disable)
   2026 		SIP_DECL(rxdrain)(sc);
   2027 
   2028 	/*
   2029 	 * Mark the interface down and cancel the watchdog timer.
   2030 	 */
   2031 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
   2032 	ifp->if_timer = 0;
   2033 
   2034 	if ((ifp->if_flags & IFF_DEBUG) != 0 &&
   2035 	    (cmdsts & CMDSTS_INTR) == 0 && sc->sc_txfree != SIP_NTXDESC)
   2036 		printf("%s: sip_stop: no INTR bits set in dirty tx "
   2037 		    "descriptors\n", sc->sc_dev.dv_xname);
   2038 }
   2039 
   2040 /*
   2041  * sip_read_eeprom:
   2042  *
   2043  *	Read data from the serial EEPROM.
   2044  */
   2045 void
   2046 SIP_DECL(read_eeprom)(struct sip_softc *sc, int word, int wordcnt,
   2047     u_int16_t *data)
   2048 {
   2049 	bus_space_tag_t st = sc->sc_st;
   2050 	bus_space_handle_t sh = sc->sc_sh;
   2051 	u_int16_t reg;
   2052 	int i, x;
   2053 
   2054 	for (i = 0; i < wordcnt; i++) {
   2055 		/* Send CHIP SELECT. */
   2056 		reg = EROMAR_EECS;
   2057 		bus_space_write_4(st, sh, SIP_EROMAR, reg);
   2058 
   2059 		/* Shift in the READ opcode. */
   2060 		for (x = 3; x > 0; x--) {
   2061 			if (SIP_EEPROM_OPC_READ & (1 << (x - 1)))
   2062 				reg |= EROMAR_EEDI;
   2063 			else
   2064 				reg &= ~EROMAR_EEDI;
   2065 			bus_space_write_4(st, sh, SIP_EROMAR, reg);
   2066 			bus_space_write_4(st, sh, SIP_EROMAR,
   2067 			    reg | EROMAR_EESK);
   2068 			delay(4);
   2069 			bus_space_write_4(st, sh, SIP_EROMAR, reg);
   2070 			delay(4);
   2071 		}
   2072 
   2073 		/* Shift in address. */
   2074 		for (x = 6; x > 0; x--) {
   2075 			if ((word + i) & (1 << (x - 1)))
   2076 				reg |= EROMAR_EEDI;
   2077 			else
   2078 				reg &= ~EROMAR_EEDI;
   2079 			bus_space_write_4(st, sh, SIP_EROMAR, reg);
   2080 			bus_space_write_4(st, sh, SIP_EROMAR,
   2081 			    reg | EROMAR_EESK);
   2082 			delay(4);
   2083 			bus_space_write_4(st, sh, SIP_EROMAR, reg);
   2084 			delay(4);
   2085 		}
   2086 
   2087 		/* Shift out data. */
   2088 		reg = EROMAR_EECS;
   2089 		data[i] = 0;
   2090 		for (x = 16; x > 0; x--) {
   2091 			bus_space_write_4(st, sh, SIP_EROMAR,
   2092 			    reg | EROMAR_EESK);
   2093 			delay(4);
   2094 			if (bus_space_read_4(st, sh, SIP_EROMAR) & EROMAR_EEDO)
   2095 				data[i] |= (1 << (x - 1));
   2096 			bus_space_write_4(st, sh, SIP_EROMAR, reg);
   2097 			delay(4);
   2098 		}
   2099 
   2100 		/* Clear CHIP SELECT. */
   2101 		bus_space_write_4(st, sh, SIP_EROMAR, 0);
   2102 		delay(4);
   2103 	}
   2104 }
   2105 
   2106 /*
   2107  * sip_add_rxbuf:
   2108  *
   2109  *	Add a receive buffer to the indicated descriptor.
   2110  */
   2111 int
   2112 SIP_DECL(add_rxbuf)(struct sip_softc *sc, int idx)
   2113 {
   2114 	struct sip_rxsoft *rxs = &sc->sc_rxsoft[idx];
   2115 	struct mbuf *m;
   2116 	int error;
   2117 
   2118 	MGETHDR(m, M_DONTWAIT, MT_DATA);
   2119 	if (m == NULL)
   2120 		return (ENOBUFS);
   2121 
   2122 	MCLGET(m, M_DONTWAIT);
   2123 	if ((m->m_flags & M_EXT) == 0) {
   2124 		m_freem(m);
   2125 		return (ENOBUFS);
   2126 	}
   2127 
   2128 	if (rxs->rxs_mbuf != NULL)
   2129 		bus_dmamap_unload(sc->sc_dmat, rxs->rxs_dmamap);
   2130 
   2131 	rxs->rxs_mbuf = m;
   2132 
   2133 	error = bus_dmamap_load(sc->sc_dmat, rxs->rxs_dmamap,
   2134 	    m->m_ext.ext_buf, m->m_ext.ext_size, NULL, BUS_DMA_NOWAIT);
   2135 	if (error) {
   2136 		printf("%s: can't load rx DMA map %d, error = %d\n",
   2137 		    sc->sc_dev.dv_xname, idx, error);
   2138 		panic("sip_add_rxbuf");		/* XXX */
   2139 	}
   2140 
   2141 	bus_dmamap_sync(sc->sc_dmat, rxs->rxs_dmamap, 0,
   2142 	    rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
   2143 
   2144 	SIP_INIT_RXDESC(sc, idx);
   2145 
   2146 	return (0);
   2147 }
   2148 
   2149 #if !defined(DP83820)
   2150 /*
   2151  * sip_sis900_set_filter:
   2152  *
   2153  *	Set up the receive filter.
   2154  */
   2155 void
   2156 SIP_DECL(sis900_set_filter)(struct sip_softc *sc)
   2157 {
   2158 	bus_space_tag_t st = sc->sc_st;
   2159 	bus_space_handle_t sh = sc->sc_sh;
   2160 	struct ethercom *ec = &sc->sc_ethercom;
   2161 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   2162 	struct ether_multi *enm;
   2163 	u_int8_t *cp;
   2164 	struct ether_multistep step;
   2165 	u_int32_t crc, mchash[8];
   2166 
   2167 	/*
   2168 	 * Initialize the prototype RFCR.
   2169 	 */
   2170 	sc->sc_rfcr = RFCR_RFEN;
   2171 	if (ifp->if_flags & IFF_BROADCAST)
   2172 		sc->sc_rfcr |= RFCR_AAB;
   2173 	if (ifp->if_flags & IFF_PROMISC) {
   2174 		sc->sc_rfcr |= RFCR_AAP;
   2175 		goto allmulti;
   2176 	}
   2177 
   2178 	/*
   2179 	 * Set up the multicast address filter by passing all multicast
   2180 	 * addresses through a CRC generator, and then using the high-order
   2181 	 * 6 bits as an index into the 128 bit multicast hash table (only
   2182 	 * the lower 16 bits of each 32 bit multicast hash register are
   2183 	 * valid).  The high order bits select the register, while the
   2184 	 * rest of the bits select the bit within the register.
   2185 	 */
   2186 
   2187 	memset(mchash, 0, sizeof(mchash));
   2188 
   2189 	ETHER_FIRST_MULTI(step, ec, enm);
   2190 	while (enm != NULL) {
   2191 		if (bcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
   2192 			/*
   2193 			 * We must listen to a range of multicast addresses.
   2194 			 * For now, just accept all multicasts, rather than
   2195 			 * trying to set only those filter bits needed to match
   2196 			 * the range.  (At this time, the only use of address
   2197 			 * ranges is for IP multicast routing, for which the
   2198 			 * range is big enough to require all bits set.)
   2199 			 */
   2200 			goto allmulti;
   2201 		}
   2202 
   2203 		crc = ether_crc32_le(enm->enm_addrlo, ETHER_ADDR_LEN);
   2204 
   2205 		/* Just want the 7 most significant bits. */
   2206 		crc >>= 25;
   2207 
   2208 		/* Set the corresponding bit in the hash table. */
   2209 		mchash[crc >> 4] |= 1 << (crc & 0xf);
   2210 
   2211 		ETHER_NEXT_MULTI(step, enm);
   2212 	}
   2213 
   2214 	ifp->if_flags &= ~IFF_ALLMULTI;
   2215 	goto setit;
   2216 
   2217  allmulti:
   2218 	ifp->if_flags |= IFF_ALLMULTI;
   2219 	sc->sc_rfcr |= RFCR_AAM;
   2220 
   2221  setit:
   2222 #define	FILTER_EMIT(addr, data)						\
   2223 	bus_space_write_4(st, sh, SIP_RFCR, (addr));			\
   2224 	delay(1);							\
   2225 	bus_space_write_4(st, sh, SIP_RFDR, (data));			\
   2226 	delay(1)
   2227 
   2228 	/*
   2229 	 * Disable receive filter, and program the node address.
   2230 	 */
   2231 	cp = LLADDR(ifp->if_sadl);
   2232 	FILTER_EMIT(RFCR_RFADDR_NODE0, (cp[1] << 8) | cp[0]);
   2233 	FILTER_EMIT(RFCR_RFADDR_NODE2, (cp[3] << 8) | cp[2]);
   2234 	FILTER_EMIT(RFCR_RFADDR_NODE4, (cp[5] << 8) | cp[4]);
   2235 
   2236 	if ((ifp->if_flags & IFF_ALLMULTI) == 0) {
   2237 		/*
   2238 		 * Program the multicast hash table.
   2239 		 */
   2240 		FILTER_EMIT(RFCR_RFADDR_MC0, mchash[0]);
   2241 		FILTER_EMIT(RFCR_RFADDR_MC1, mchash[1]);
   2242 		FILTER_EMIT(RFCR_RFADDR_MC2, mchash[2]);
   2243 		FILTER_EMIT(RFCR_RFADDR_MC3, mchash[3]);
   2244 		FILTER_EMIT(RFCR_RFADDR_MC4, mchash[4]);
   2245 		FILTER_EMIT(RFCR_RFADDR_MC5, mchash[5]);
   2246 		FILTER_EMIT(RFCR_RFADDR_MC6, mchash[6]);
   2247 		FILTER_EMIT(RFCR_RFADDR_MC7, mchash[7]);
   2248 	}
   2249 #undef FILTER_EMIT
   2250 
   2251 	/*
   2252 	 * Re-enable the receiver filter.
   2253 	 */
   2254 	bus_space_write_4(st, sh, SIP_RFCR, sc->sc_rfcr);
   2255 }
   2256 #endif /* ! DP83820 */
   2257 
   2258 /*
   2259  * sip_dp83815_set_filter:
   2260  *
   2261  *	Set up the receive filter.
   2262  */
   2263 void
   2264 SIP_DECL(dp83815_set_filter)(struct sip_softc *sc)
   2265 {
   2266 	bus_space_tag_t st = sc->sc_st;
   2267 	bus_space_handle_t sh = sc->sc_sh;
   2268 	struct ethercom *ec = &sc->sc_ethercom;
   2269 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   2270 	struct ether_multi *enm;
   2271 	u_int8_t *cp;
   2272 	struct ether_multistep step;
   2273 	u_int32_t crc, hash, slot, bit;
   2274 #ifdef DP83820
   2275 #define	MCHASH_NWORDS	128
   2276 #else
   2277 #define	MCHASH_NWORDS	32
   2278 #endif /* DP83820 */
   2279 	u_int16_t mchash[MCHASH_NWORDS];
   2280 	int i;
   2281 
   2282 	/*
   2283 	 * Initialize the prototype RFCR.
   2284 	 * Enable the receive filter, and accept on
   2285 	 *    Perfect (destination address) Match
   2286 	 * If IFF_BROADCAST, also accept all broadcast packets.
   2287 	 * If IFF_PROMISC, accept all unicast packets (and later, set
   2288 	 *    IFF_ALLMULTI and accept all multicast, too).
   2289 	 */
   2290 	sc->sc_rfcr = RFCR_RFEN | RFCR_APM;
   2291 	if (ifp->if_flags & IFF_BROADCAST)
   2292 		sc->sc_rfcr |= RFCR_AAB;
   2293 	if (ifp->if_flags & IFF_PROMISC) {
   2294 		sc->sc_rfcr |= RFCR_AAP;
   2295 		goto allmulti;
   2296 	}
   2297 
   2298 #ifdef DP83820
   2299 	/*
   2300 	 * Set up the DP83820 multicast address filter by passing all multicast
   2301 	 * addresses through a CRC generator, and then using the high-order
   2302 	 * 11 bits as an index into the 2048 bit multicast hash table.  The
   2303 	 * high-order 7 bits select the slot, while the low-order 4 bits
   2304 	 * select the bit within the slot.  Note that only the low 16-bits
   2305 	 * of each filter word are used, and there are 128 filter words.
   2306 	 */
   2307 #else
   2308 	/*
   2309 	 * Set up the DP83815 multicast address filter by passing all multicast
   2310 	 * addresses through a CRC generator, and then using the high-order
   2311 	 * 9 bits as an index into the 512 bit multicast hash table.  The
   2312 	 * high-order 5 bits select the slot, while the low-order 4 bits
   2313 	 * select the bit within the slot.  Note that only the low 16-bits
   2314 	 * of each filter word are used, and there are 32 filter words.
   2315 	 */
   2316 #endif /* DP83820 */
   2317 
   2318 	memset(mchash, 0, sizeof(mchash));
   2319 
   2320 	ifp->if_flags &= ~IFF_ALLMULTI;
   2321 	ETHER_FIRST_MULTI(step, ec, enm);
   2322 	if (enm != NULL) {
   2323 		while (enm != NULL) {
   2324 			if (memcmp(enm->enm_addrlo, enm->enm_addrhi,
   2325 			    ETHER_ADDR_LEN)) {
   2326 			/*
   2327 			 * We must listen to a range of multicast addresses.
   2328 			 * For now, just accept all multicasts, rather than
   2329 			 * trying to set only those filter bits needed to match
   2330 			 * the range.  (At this time, the only use of address
   2331 			 * ranges is for IP multicast routing, for which the
   2332 			 * range is big enough to require all bits set.)
   2333 			 */
   2334 				goto allmulti;
   2335 			}
   2336 
   2337 #ifdef DP83820
   2338 			crc = ether_crc32_be(enm->enm_addrlo, ETHER_ADDR_LEN);
   2339 
   2340 			/* Just want the 11 most significant bits. */
   2341 			hash = crc >> 21;
   2342 #else
   2343 			crc = ether_crc32_le(enm->enm_addrlo, ETHER_ADDR_LEN);
   2344 
   2345 			/* Just want the 9 most significant bits. */
   2346 			hash = crc >> 23;
   2347 #endif /* DP83820 */
   2348 			slot = hash >> 4;
   2349 			bit = hash & 0xf;
   2350 
   2351 			/* Set the corresponding bit in the hash table. */
   2352 			mchash[slot] |= 1 << bit;
   2353 
   2354 			ETHER_NEXT_MULTI(step, enm);
   2355 		}
   2356 
   2357 		sc->sc_rfcr |= RFCR_MHEN;
   2358 	}
   2359 	goto setit;
   2360 
   2361  allmulti:
   2362 	ifp->if_flags |= IFF_ALLMULTI;
   2363 	sc->sc_rfcr |= RFCR_AAM;
   2364 
   2365  setit:
   2366 #define	FILTER_EMIT(addr, data)						\
   2367 	bus_space_write_4(st, sh, SIP_RFCR, (addr));			\
   2368 	delay(1);							\
   2369 	bus_space_write_4(st, sh, SIP_RFDR, (data));			\
   2370 	delay(1);
   2371 
   2372 	/*
   2373 	 * Disable receive filter, and program the node address.
   2374 	 */
   2375 	cp = LLADDR(ifp->if_sadl);
   2376 	FILTER_EMIT(RFCR_NS_RFADDR_PMATCH0, (cp[1] << 8) | cp[0]);
   2377 	FILTER_EMIT(RFCR_NS_RFADDR_PMATCH2, (cp[3] << 8) | cp[2]);
   2378 	FILTER_EMIT(RFCR_NS_RFADDR_PMATCH4, (cp[5] << 8) | cp[4]);
   2379 
   2380 	if ((ifp->if_flags & IFF_ALLMULTI) == 0) {
   2381 		/*
   2382 		 * Program the multicast hash table.
   2383 		 */
   2384 		for (i = 0; i < MCHASH_NWORDS; i++)
   2385 			FILTER_EMIT(RFCR_NS_RFADDR_FILTMEM + (i * 2),
   2386 			    mchash[i]);
   2387 	}
   2388 #undef FILTER_EMIT
   2389 #undef MCHASH_NWORDS
   2390 
   2391 	/*
   2392 	 * Re-enable the receiver filter.
   2393 	 */
   2394 	bus_space_write_4(st, sh, SIP_RFCR, sc->sc_rfcr);
   2395 }
   2396 
   2397 #if defined(DP83820)
   2398 /*
   2399  * sip_dp83820_mii_readreg:	[mii interface function]
   2400  *
   2401  *	Read a PHY register on the MII of the DP83820.
   2402  */
   2403 int
   2404 SIP_DECL(dp83820_mii_readreg)(struct device *self, int phy, int reg)
   2405 {
   2406 
   2407 	return (mii_bitbang_readreg(self, &SIP_DECL(dp83820_mii_bitbang_ops),
   2408 	    phy, reg));
   2409 }
   2410 
   2411 /*
   2412  * sip_dp83820_mii_writereg:	[mii interface function]
   2413  *
   2414  *	Write a PHY register on the MII of the DP83820.
   2415  */
   2416 void
   2417 SIP_DECL(dp83820_mii_writereg)(struct device *self, int phy, int reg, int val)
   2418 {
   2419 
   2420 	mii_bitbang_writereg(self, &SIP_DECL(dp83820_mii_bitbang_ops),
   2421 	    phy, reg, val);
   2422 }
   2423 
   2424 /*
   2425  * sip_dp83815_mii_statchg:	[mii interface function]
   2426  *
   2427  *	Callback from MII layer when media changes.
   2428  */
   2429 void
   2430 SIP_DECL(dp83820_mii_statchg)(struct device *self)
   2431 {
   2432 	struct sip_softc *sc = (struct sip_softc *) self;
   2433 	u_int32_t cfg;
   2434 
   2435 	/*
   2436 	 * Update TXCFG for full-duplex operation.
   2437 	 */
   2438 	if ((sc->sc_mii.mii_media_active & IFM_FDX) != 0)
   2439 		sc->sc_txcfg |= (TXCFG_CSI | TXCFG_HBI);
   2440 	else
   2441 		sc->sc_txcfg &= ~(TXCFG_CSI | TXCFG_HBI);
   2442 
   2443 	/*
   2444 	 * Update RXCFG for full-duplex or loopback.
   2445 	 */
   2446 	if ((sc->sc_mii.mii_media_active & IFM_FDX) != 0 ||
   2447 	    IFM_SUBTYPE(sc->sc_mii.mii_media_active) == IFM_LOOP)
   2448 		sc->sc_rxcfg |= RXCFG_ATX;
   2449 	else
   2450 		sc->sc_rxcfg &= ~RXCFG_ATX;
   2451 
   2452 	/*
   2453 	 * Update CFG for MII/GMII.
   2454 	 */
   2455 	if (sc->sc_ethercom.ec_if.if_baudrate == IF_Mbps(1000))
   2456 		cfg = sc->sc_cfg | CFG_MODE_1000;
   2457 	else
   2458 		cfg = sc->sc_cfg;
   2459 
   2460 	/*
   2461 	 * XXX 802.3x flow control.
   2462 	 */
   2463 
   2464 	bus_space_write_4(sc->sc_st, sc->sc_sh, SIP_CFG, cfg);
   2465 	bus_space_write_4(sc->sc_st, sc->sc_sh, SIP_TXCFG, sc->sc_txcfg);
   2466 	bus_space_write_4(sc->sc_st, sc->sc_sh, SIP_RXCFG, sc->sc_rxcfg);
   2467 }
   2468 
   2469 /*
   2470  * sip_dp83820_mii_bitbang_read: [mii bit-bang interface function]
   2471  *
   2472  *	Read the MII serial port for the MII bit-bang module.
   2473  */
   2474 u_int32_t
   2475 SIP_DECL(dp83820_mii_bitbang_read)(struct device *self)
   2476 {
   2477 	struct sip_softc *sc = (void *) self;
   2478 
   2479 	return (bus_space_read_4(sc->sc_st, sc->sc_sh, SIP_EROMAR));
   2480 }
   2481 
   2482 /*
   2483  * sip_dp83820_mii_bitbang_write: [mii big-bang interface function]
   2484  *
   2485  *	Write the MII serial port for the MII bit-bang module.
   2486  */
   2487 void
   2488 SIP_DECL(dp83820_mii_bitbang_write)(struct device *self, u_int32_t val)
   2489 {
   2490 	struct sip_softc *sc = (void *) self;
   2491 
   2492 	bus_space_write_4(sc->sc_st, sc->sc_sh, SIP_EROMAR, val);
   2493 }
   2494 #else /* ! DP83820 */
   2495 /*
   2496  * sip_sis900_mii_readreg:	[mii interface function]
   2497  *
   2498  *	Read a PHY register on the MII.
   2499  */
   2500 int
   2501 SIP_DECL(sis900_mii_readreg)(struct device *self, int phy, int reg)
   2502 {
   2503 	struct sip_softc *sc = (struct sip_softc *) self;
   2504 	u_int32_t enphy;
   2505 
   2506 	/*
   2507 	 * The SiS 900 has only an internal PHY on the MII.  Only allow
   2508 	 * MII address 0.
   2509 	 */
   2510 	if (sc->sc_model->sip_product == PCI_PRODUCT_SIS_900 && phy != 0)
   2511 		return (0);
   2512 
   2513 	bus_space_write_4(sc->sc_st, sc->sc_sh, SIP_ENPHY,
   2514 	    (phy << ENPHY_PHYADDR_SHIFT) | (reg << ENPHY_REGADDR_SHIFT) |
   2515 	    ENPHY_RWCMD | ENPHY_ACCESS);
   2516 	do {
   2517 		enphy = bus_space_read_4(sc->sc_st, sc->sc_sh, SIP_ENPHY);
   2518 	} while (enphy & ENPHY_ACCESS);
   2519 	return ((enphy & ENPHY_PHYDATA) >> ENPHY_DATA_SHIFT);
   2520 }
   2521 
   2522 /*
   2523  * sip_sis900_mii_writereg:	[mii interface function]
   2524  *
   2525  *	Write a PHY register on the MII.
   2526  */
   2527 void
   2528 SIP_DECL(sis900_mii_writereg)(struct device *self, int phy, int reg, int val)
   2529 {
   2530 	struct sip_softc *sc = (struct sip_softc *) self;
   2531 	u_int32_t enphy;
   2532 
   2533 	/*
   2534 	 * The SiS 900 has only an internal PHY on the MII.  Only allow
   2535 	 * MII address 0.
   2536 	 */
   2537 	if (sc->sc_model->sip_product == PCI_PRODUCT_SIS_900 && phy != 0)
   2538 		return;
   2539 
   2540 	bus_space_write_4(sc->sc_st, sc->sc_sh, SIP_ENPHY,
   2541 	    (val << ENPHY_DATA_SHIFT) | (phy << ENPHY_PHYADDR_SHIFT) |
   2542 	    (reg << ENPHY_REGADDR_SHIFT) | ENPHY_ACCESS);
   2543 	do {
   2544 		enphy = bus_space_read_4(sc->sc_st, sc->sc_sh, SIP_ENPHY);
   2545 	} while (enphy & ENPHY_ACCESS);
   2546 }
   2547 
   2548 /*
   2549  * sip_sis900_mii_statchg:	[mii interface function]
   2550  *
   2551  *	Callback from MII layer when media changes.
   2552  */
   2553 void
   2554 SIP_DECL(sis900_mii_statchg)(struct device *self)
   2555 {
   2556 	struct sip_softc *sc = (struct sip_softc *) self;
   2557 	u_int32_t flowctl;
   2558 
   2559 	/*
   2560 	 * Update TXCFG for full-duplex operation.
   2561 	 */
   2562 	if ((sc->sc_mii.mii_media_active & IFM_FDX) != 0)
   2563 		sc->sc_txcfg |= (TXCFG_CSI | TXCFG_HBI);
   2564 	else
   2565 		sc->sc_txcfg &= ~(TXCFG_CSI | TXCFG_HBI);
   2566 
   2567 	/*
   2568 	 * Update RXCFG for full-duplex or loopback.
   2569 	 */
   2570 	if ((sc->sc_mii.mii_media_active & IFM_FDX) != 0 ||
   2571 	    IFM_SUBTYPE(sc->sc_mii.mii_media_active) == IFM_LOOP)
   2572 		sc->sc_rxcfg |= RXCFG_ATX;
   2573 	else
   2574 		sc->sc_rxcfg &= ~RXCFG_ATX;
   2575 
   2576 	/*
   2577 	 * Update IMR for use of 802.3x flow control.
   2578 	 */
   2579 	if ((sc->sc_mii.mii_media_active & IFM_FLOW) != 0) {
   2580 		sc->sc_imr |= (ISR_PAUSE_END|ISR_PAUSE_ST);
   2581 		flowctl = FLOWCTL_FLOWEN;
   2582 	} else {
   2583 		sc->sc_imr &= ~(ISR_PAUSE_END|ISR_PAUSE_ST);
   2584 		flowctl = 0;
   2585 	}
   2586 
   2587 	bus_space_write_4(sc->sc_st, sc->sc_sh, SIP_TXCFG, sc->sc_txcfg);
   2588 	bus_space_write_4(sc->sc_st, sc->sc_sh, SIP_RXCFG, sc->sc_rxcfg);
   2589 	bus_space_write_4(sc->sc_st, sc->sc_sh, SIP_IMR, sc->sc_imr);
   2590 	bus_space_write_4(sc->sc_st, sc->sc_sh, SIP_FLOWCTL, flowctl);
   2591 }
   2592 
   2593 /*
   2594  * sip_dp83815_mii_readreg:	[mii interface function]
   2595  *
   2596  *	Read a PHY register on the MII.
   2597  */
   2598 int
   2599 SIP_DECL(dp83815_mii_readreg)(struct device *self, int phy, int reg)
   2600 {
   2601 	struct sip_softc *sc = (struct sip_softc *) self;
   2602 	u_int32_t val;
   2603 
   2604 	/*
   2605 	 * The DP83815 only has an internal PHY.  Only allow
   2606 	 * MII address 0.
   2607 	 */
   2608 	if (phy != 0)
   2609 		return (0);
   2610 
   2611 	/*
   2612 	 * Apparently, after a reset, the DP83815 can take a while
   2613 	 * to respond.  During this recovery period, the BMSR returns
   2614 	 * a value of 0.  Catch this -- it's not supposed to happen
   2615 	 * (the BMSR has some hardcoded-to-1 bits), and wait for the
   2616 	 * PHY to come back to life.
   2617 	 *
   2618 	 * This works out because the BMSR is the first register
   2619 	 * read during the PHY probe process.
   2620 	 */
   2621 	do {
   2622 		val = bus_space_read_4(sc->sc_st, sc->sc_sh, SIP_NS_PHY(reg));
   2623 	} while (reg == MII_BMSR && val == 0);
   2624 
   2625 	return (val & 0xffff);
   2626 }
   2627 
   2628 /*
   2629  * sip_dp83815_mii_writereg:	[mii interface function]
   2630  *
   2631  *	Write a PHY register to the MII.
   2632  */
   2633 void
   2634 SIP_DECL(dp83815_mii_writereg)(struct device *self, int phy, int reg, int val)
   2635 {
   2636 	struct sip_softc *sc = (struct sip_softc *) self;
   2637 
   2638 	/*
   2639 	 * The DP83815 only has an internal PHY.  Only allow
   2640 	 * MII address 0.
   2641 	 */
   2642 	if (phy != 0)
   2643 		return;
   2644 
   2645 	bus_space_write_4(sc->sc_st, sc->sc_sh, SIP_NS_PHY(reg), val);
   2646 }
   2647 
   2648 /*
   2649  * sip_dp83815_mii_statchg:	[mii interface function]
   2650  *
   2651  *	Callback from MII layer when media changes.
   2652  */
   2653 void
   2654 SIP_DECL(dp83815_mii_statchg)(struct device *self)
   2655 {
   2656 	struct sip_softc *sc = (struct sip_softc *) self;
   2657 
   2658 	/*
   2659 	 * Update TXCFG for full-duplex operation.
   2660 	 */
   2661 	if ((sc->sc_mii.mii_media_active & IFM_FDX) != 0)
   2662 		sc->sc_txcfg |= (TXCFG_CSI | TXCFG_HBI);
   2663 	else
   2664 		sc->sc_txcfg &= ~(TXCFG_CSI | TXCFG_HBI);
   2665 
   2666 	/*
   2667 	 * Update RXCFG for full-duplex or loopback.
   2668 	 */
   2669 	if ((sc->sc_mii.mii_media_active & IFM_FDX) != 0 ||
   2670 	    IFM_SUBTYPE(sc->sc_mii.mii_media_active) == IFM_LOOP)
   2671 		sc->sc_rxcfg |= RXCFG_ATX;
   2672 	else
   2673 		sc->sc_rxcfg &= ~RXCFG_ATX;
   2674 
   2675 	/*
   2676 	 * XXX 802.3x flow control.
   2677 	 */
   2678 
   2679 	bus_space_write_4(sc->sc_st, sc->sc_sh, SIP_TXCFG, sc->sc_txcfg);
   2680 	bus_space_write_4(sc->sc_st, sc->sc_sh, SIP_RXCFG, sc->sc_rxcfg);
   2681 }
   2682 #endif /* DP83820 */
   2683 
   2684 #if defined(DP83820)
   2685 void
   2686 SIP_DECL(dp83820_read_macaddr)(struct sip_softc *sc, u_int8_t *enaddr)
   2687 {
   2688 	u_int16_t eeprom_data[SIP_DP83820_EEPROM_LENGTH / 2];
   2689 	u_int8_t cksum, *e, match;
   2690 	int i;
   2691 
   2692 	/*
   2693 	 * EEPROM data format for the DP83820 can be found in
   2694 	 * the DP83820 manual, section 4.2.4.
   2695 	 */
   2696 
   2697 	SIP_DECL(read_eeprom)(sc, 0,
   2698 	    sizeof(eeprom_data) / sizeof(eeprom_data[0]), eeprom_data);
   2699 
   2700 	match = eeprom_data[SIP_DP83820_EEPROM_CHECKSUM / 2] >> 8;
   2701 	match = ~(match - 1);
   2702 
   2703 	cksum = 0x55;
   2704 	e = (u_int8_t *) eeprom_data;
   2705 	for (i = 0; i < SIP_DP83820_EEPROM_CHECKSUM; i++)
   2706 		cksum += *e++;
   2707 
   2708 	if (cksum != match)
   2709 		printf("%s: Checksum (%x) mismatch (%x)",
   2710 		    sc->sc_dev.dv_xname, cksum, match);
   2711 
   2712 	enaddr[0] = eeprom_data[SIP_DP83820_EEPROM_PMATCH2 / 2] & 0xff;
   2713 	enaddr[1] = eeprom_data[SIP_DP83820_EEPROM_PMATCH2 / 2] >> 8;
   2714 	enaddr[2] = eeprom_data[SIP_DP83820_EEPROM_PMATCH1 / 2] & 0xff;
   2715 	enaddr[3] = eeprom_data[SIP_DP83820_EEPROM_PMATCH1 / 2] >> 8;
   2716 	enaddr[4] = eeprom_data[SIP_DP83820_EEPROM_PMATCH0 / 2] & 0xff;
   2717 	enaddr[5] = eeprom_data[SIP_DP83820_EEPROM_PMATCH0 / 2] >> 8;
   2718 
   2719 	/* Get the GPIOR bits. */
   2720 	sc->sc_gpior = eeprom_data[0x04];
   2721 
   2722 	/* Get various CFG related bits. */
   2723 	if ((eeprom_data[0x05] >> 0) & 1)
   2724 		sc->sc_cfg |= CFG_EXT_125;
   2725 	if ((eeprom_data[0x05] >> 9) & 1)
   2726 		sc->sc_cfg |= CFG_TBI_EN;
   2727 }
   2728 #else /* ! DP83820 */
   2729 void
   2730 SIP_DECL(sis900_read_macaddr)(struct sip_softc *sc, u_int8_t *enaddr)
   2731 {
   2732 	u_int16_t myea[ETHER_ADDR_LEN / 2];
   2733 
   2734 	SIP_DECL(read_eeprom)(sc, SIP_EEPROM_ETHERNET_ID0 >> 1,
   2735 	    sizeof(myea) / sizeof(myea[0]), myea);
   2736 
   2737 	enaddr[0] = myea[0] & 0xff;
   2738 	enaddr[1] = myea[0] >> 8;
   2739 	enaddr[2] = myea[1] & 0xff;
   2740 	enaddr[3] = myea[1] >> 8;
   2741 	enaddr[4] = myea[2] & 0xff;
   2742 	enaddr[5] = myea[2] >> 8;
   2743 }
   2744 
   2745 /* Table and macro to bit-reverse an octet. */
   2746 static const u_int8_t bbr4[] = {0,8,4,12,2,10,6,14,1,9,5,13,3,11,7,15};
   2747 #define bbr(v)	((bbr4[(v)&0xf] << 4) | bbr4[((v)>>4) & 0xf])
   2748 
   2749 void
   2750 SIP_DECL(dp83815_read_macaddr)(struct sip_softc *sc, u_int8_t *enaddr)
   2751 {
   2752 	u_int16_t eeprom_data[SIP_DP83815_EEPROM_LENGTH / 2], *ea;
   2753 	u_int8_t cksum, *e, match;
   2754 	int i;
   2755 
   2756 	SIP_DECL(read_eeprom)(sc, 0, sizeof(eeprom_data) /
   2757 	    sizeof(eeprom_data[0]), eeprom_data);
   2758 
   2759 	match = eeprom_data[SIP_DP83815_EEPROM_CHECKSUM/2] >> 8;
   2760 	match = ~(match - 1);
   2761 
   2762 	cksum = 0x55;
   2763 	e = (u_int8_t *) eeprom_data;
   2764 	for (i=0 ; i<SIP_DP83815_EEPROM_CHECKSUM ; i++) {
   2765 		cksum += *e++;
   2766 	}
   2767 	if (cksum != match) {
   2768 		printf("%s: Checksum (%x) mismatch (%x)",
   2769 		    sc->sc_dev.dv_xname, cksum, match);
   2770 	}
   2771 
   2772 	/*
   2773 	 * Unrolled because it makes slightly more sense this way.
   2774 	 * The DP83815 stores the MAC address in bit 0 of word 6
   2775 	 * through bit 15 of word 8.
   2776 	 */
   2777 	ea = &eeprom_data[6];
   2778 	enaddr[0] = ((*ea & 0x1) << 7);
   2779 	ea++;
   2780 	enaddr[0] |= ((*ea & 0xFE00) >> 9);
   2781 	enaddr[1] = ((*ea & 0x1FE) >> 1);
   2782 	enaddr[2] = ((*ea & 0x1) << 7);
   2783 	ea++;
   2784 	enaddr[2] |= ((*ea & 0xFE00) >> 9);
   2785 	enaddr[3] = ((*ea & 0x1FE) >> 1);
   2786 	enaddr[4] = ((*ea & 0x1) << 7);
   2787 	ea++;
   2788 	enaddr[4] |= ((*ea & 0xFE00) >> 9);
   2789 	enaddr[5] = ((*ea & 0x1FE) >> 1);
   2790 
   2791 	/*
   2792 	 * In case that's not weird enough, we also need to reverse
   2793 	 * the bits in each byte.  This all actually makes more sense
   2794 	 * if you think about the EEPROM storage as an array of bits
   2795 	 * being shifted into bytes, but that's not how we're looking
   2796 	 * at it here...
   2797 	 */
   2798 	for (i = 0; i < 6 ;i++)
   2799 		enaddr[i] = bbr(enaddr[i]);
   2800 }
   2801 #endif /* DP83820 */
   2802 
   2803 /*
   2804  * sip_mediastatus:	[ifmedia interface function]
   2805  *
   2806  *	Get the current interface media status.
   2807  */
   2808 void
   2809 SIP_DECL(mediastatus)(struct ifnet *ifp, struct ifmediareq *ifmr)
   2810 {
   2811 	struct sip_softc *sc = ifp->if_softc;
   2812 
   2813 	mii_pollstat(&sc->sc_mii);
   2814 	ifmr->ifm_status = sc->sc_mii.mii_media_status;
   2815 	ifmr->ifm_active = sc->sc_mii.mii_media_active;
   2816 }
   2817 
   2818 /*
   2819  * sip_mediachange:	[ifmedia interface function]
   2820  *
   2821  *	Set hardware to newly-selected media.
   2822  */
   2823 int
   2824 SIP_DECL(mediachange)(struct ifnet *ifp)
   2825 {
   2826 	struct sip_softc *sc = ifp->if_softc;
   2827 
   2828 	if (ifp->if_flags & IFF_UP)
   2829 		mii_mediachg(&sc->sc_mii);
   2830 	return (0);
   2831 }
   2832