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