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