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