Home | History | Annotate | Line # | Download | only in pci
if_bce.c revision 1.14
      1 /* $NetBSD: if_bce.c,v 1.14 2007/03/04 06:02:19 christos Exp $	 */
      2 
      3 /*
      4  * Copyright (c) 2003 Clifford Wright. All rights reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  * 3. The name of the author may not be used to endorse or promote products
     15  *    derived from this software without specific prior written permission.
     16  *
     17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     22  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     24  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     25  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     27  * SUCH DAMAGE.
     28  */
     29 
     30 /*
     31  * Broadcom BCM440x 10/100 ethernet (broadcom.com)
     32  * SiliconBackplane is technology from Sonics, Inc.(sonicsinc.com)
     33  *
     34  * Cliff Wright cliff (at) snipe444.org
     35  */
     36 
     37 #include "bpfilter.h"
     38 #include "vlan.h"
     39 
     40 #include <sys/param.h>
     41 #include <sys/systm.h>
     42 #include <sys/callout.h>
     43 #include <sys/sockio.h>
     44 #include <sys/mbuf.h>
     45 #include <sys/malloc.h>
     46 #include <sys/kernel.h>
     47 #include <sys/device.h>
     48 #include <sys/socket.h>
     49 
     50 #include <net/if.h>
     51 #include <net/if_dl.h>
     52 #include <net/if_media.h>
     53 #include <net/if_ether.h>
     54 
     55 #if NBPFILTER > 0
     56 #include <net/bpf.h>
     57 #endif
     58 
     59 #include <dev/pci/pcireg.h>
     60 #include <dev/pci/pcivar.h>
     61 #include <dev/pci/pcidevs.h>
     62 
     63 #include <dev/mii/mii.h>
     64 #include <dev/mii/miivar.h>
     65 #include <dev/mii/miidevs.h>
     66 #include <dev/mii/brgphyreg.h>
     67 
     68 #include <dev/pci/if_bcereg.h>
     69 
     70 #include <uvm/uvm_extern.h>
     71 
     72 /* transmit buffer max frags allowed */
     73 #define BCE_NTXFRAGS	16
     74 
     75 /* ring descriptor */
     76 struct bce_dma_slot {
     77 	u_int32_t ctrl;
     78 	u_int32_t addr;
     79 };
     80 #define CTRL_BC_MASK	0x1fff	/* buffer byte count */
     81 #define CTRL_EOT	0x10000000	/* end of descriptor table */
     82 #define CTRL_IOC	0x20000000	/* interrupt on completion */
     83 #define CTRL_EOF	0x40000000	/* end of frame */
     84 #define CTRL_SOF	0x80000000	/* start of frame */
     85 
     86 /* Packet status is returned in a pre-packet header */
     87 struct rx_pph {
     88 	u_int16_t len;
     89 	u_int16_t flags;
     90 	u_int16_t pad[12];
     91 };
     92 
     93 /* packet status flags bits */
     94 #define RXF_NO				0x8	/* odd number of nibbles */
     95 #define RXF_RXER			0x4	/* receive symbol error */
     96 #define RXF_CRC				0x2	/* crc error */
     97 #define RXF_OV				0x1	/* fifo overflow */
     98 
     99 /* number of descriptors used in a ring */
    100 #define BCE_NRXDESC		128
    101 #define BCE_NTXDESC		128
    102 
    103 /*
    104  * Mbuf pointers. We need these to keep track of the virtual addresses
    105  * of our mbuf chains since we can only convert from physical to virtual,
    106  * not the other way around.
    107  */
    108 struct bce_chain_data {
    109 	struct mbuf    *bce_tx_chain[BCE_NTXDESC];
    110 	struct mbuf    *bce_rx_chain[BCE_NRXDESC];
    111 	bus_dmamap_t    bce_tx_map[BCE_NTXDESC];
    112 	bus_dmamap_t    bce_rx_map[BCE_NRXDESC];
    113 };
    114 
    115 #define BCE_TIMEOUT		100	/* # 10us for mii read/write */
    116 
    117 struct bce_softc {
    118 	struct device		bce_dev;
    119 	bus_space_tag_t		bce_btag;
    120 	bus_space_handle_t	bce_bhandle;
    121 	bus_dma_tag_t		bce_dmatag;
    122 	struct ethercom		ethercom;	/* interface info */
    123 	void			*bce_intrhand;
    124 	struct pci_attach_args	bce_pa;
    125 	struct mii_data		bce_mii;
    126 	u_int32_t		bce_phy;	/* eeprom indicated phy */
    127 	struct ifmedia		bce_ifmedia;	/* media info *//* Check */
    128 	u_int8_t		enaddr[ETHER_ADDR_LEN];
    129 	struct bce_dma_slot	*bce_rx_ring;	/* receive ring */
    130 	struct bce_dma_slot	*bce_tx_ring;	/* transmit ring */
    131 	struct bce_chain_data	bce_cdata;	/* mbufs */
    132 	bus_dmamap_t		bce_ring_map;
    133 	u_int32_t		bce_intmask;	/* current intr mask */
    134 	u_int32_t		bce_rxin;	/* last rx descriptor seen */
    135 	u_int32_t		bce_txin;	/* last tx descriptor seen */
    136 	int			bce_txsfree;	/* no. tx slots available */
    137 	int			bce_txsnext;	/* next available tx slot */
    138 	struct callout		bce_timeout;
    139 };
    140 
    141 /* for ring descriptors */
    142 #define BCE_RXBUF_LEN	(MCLBYTES - 4)
    143 #define BCE_INIT_RXDESC(sc, x)						\
    144 do {									\
    145 	struct bce_dma_slot *__bced = &sc->bce_rx_ring[x];		\
    146 									\
    147 	*mtod(sc->bce_cdata.bce_rx_chain[x], u_int32_t *) = 0;		\
    148 	__bced->addr =							\
    149 	    htole32(sc->bce_cdata.bce_rx_map[x]->dm_segs[0].ds_addr	\
    150 	    + 0x40000000);						\
    151 	if (x != (BCE_NRXDESC - 1))					\
    152 		__bced->ctrl = htole32(BCE_RXBUF_LEN);			\
    153 	else								\
    154 		__bced->ctrl = htole32(BCE_RXBUF_LEN | CTRL_EOT);	\
    155 	bus_dmamap_sync(sc->bce_dmatag, sc->bce_ring_map,		\
    156 	    sizeof(struct bce_dma_slot) * x,				\
    157 	    sizeof(struct bce_dma_slot),				\
    158 	    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);			\
    159 } while (/* CONSTCOND */ 0)
    160 
    161 static	int	bce_probe(struct device *, struct cfdata *, void *);
    162 static	void	bce_attach(struct device *, struct device *, void *);
    163 static	int	bce_ioctl(struct ifnet *, u_long, void *);
    164 static	void	bce_start(struct ifnet *);
    165 static	void	bce_watchdog(struct ifnet *);
    166 static	int	bce_intr(void *);
    167 static	void	bce_rxintr(struct bce_softc *);
    168 static	void	bce_txintr(struct bce_softc *);
    169 static	int	bce_init(struct ifnet *);
    170 static	void	bce_add_mac(struct bce_softc *, u_int8_t *, unsigned long);
    171 static	int	bce_add_rxbuf(struct bce_softc *, int);
    172 static	void	bce_rxdrain(struct bce_softc *);
    173 static	void	bce_stop(struct ifnet *, int);
    174 static	void	bce_reset(struct bce_softc *);
    175 static	void	bce_set_filter(struct ifnet *);
    176 static	int	bce_mii_read(struct device *, int, int);
    177 static	void	bce_mii_write(struct device *, int, int, int);
    178 static	void	bce_statchg(struct device *);
    179 static	int	bce_mediachange(struct ifnet *);
    180 static	void	bce_mediastatus(struct ifnet *, struct ifmediareq *);
    181 static	void	bce_tick(void *);
    182 
    183 #define BCE_DEBUG
    184 #ifdef BCE_DEBUG
    185 #define DPRINTF(x)	do {		\
    186 	if (bcedebug)			\
    187 		printf x;		\
    188 } while (/* CONSTCOND */ 0)
    189 #define DPRINTFN(n,x)	do {		\
    190 	if (bcedebug >= (n))		\
    191 		printf x;		\
    192 } while (/* CONSTCOND */ 0)
    193 int             bcedebug = 0;
    194 #else
    195 #define DPRINTF(x)
    196 #define DPRINTFN(n,x)
    197 #endif
    198 
    199 #if __NetBSD_Version__ >= 106080000
    200 CFATTACH_DECL(bce, sizeof(struct bce_softc),
    201 	      bce_probe, bce_attach, NULL, NULL);
    202 #else
    203 struct cfattach bce_ca = {
    204 	sizeof(struct bce_softc), bce_probe, bce_attach
    205 };
    206 #endif
    207 
    208 #if __NetBSD_Version__ >= 106120000
    209 #define APRINT_ERROR	aprint_error
    210 #define APRINT_NORMAL	aprint_normal
    211 #else
    212 #define APRINT_ERROR	printf
    213 #define APRINT_NORMAL	printf
    214 #endif
    215 
    216 
    217 static const struct bce_product {
    218 	pci_vendor_id_t bp_vendor;
    219 	pci_product_id_t bp_product;
    220 	const	char *bp_name;
    221 } bce_products[] = {
    222 	{
    223 		PCI_VENDOR_BROADCOM,
    224 		PCI_PRODUCT_BROADCOM_BCM4401,
    225 		"Broadcom BCM4401 10/100 Ethernet"
    226 	},
    227 	{
    228 		PCI_VENDOR_BROADCOM,
    229 		PCI_PRODUCT_BROADCOM_BCM4401_B0,
    230 		"Broadcom BCM4401-B0 10/100 Ethernet"
    231 	},
    232 	{
    233 
    234 		0,
    235 		0,
    236 		NULL
    237 	},
    238 };
    239 
    240 static const struct bce_product *
    241 bce_lookup(const struct pci_attach_args * pa)
    242 {
    243 	const struct bce_product *bp;
    244 
    245 	for (bp = bce_products; bp->bp_name != NULL; bp++) {
    246 		if (PCI_VENDOR(pa->pa_id) == bp->bp_vendor &&
    247 		    PCI_PRODUCT(pa->pa_id) == bp->bp_product)
    248 			return (bp);
    249 	}
    250 
    251 	return (NULL);
    252 }
    253 
    254 /*
    255  * Probe for a Broadcom chip. Check the PCI vendor and device IDs
    256  * against drivers product list, and return its name if a match is found.
    257  */
    258 static int
    259 bce_probe(struct device *parent, struct cfdata *match,
    260     void *aux)
    261 {
    262 	struct pci_attach_args *pa = (struct pci_attach_args *) aux;
    263 
    264 	if (bce_lookup(pa) != NULL)
    265 		return (1);
    266 
    267 	return (0);
    268 }
    269 
    270 static void
    271 bce_attach(struct device *parent, struct device *self, void *aux)
    272 {
    273 	struct bce_softc *sc = (struct bce_softc *) self;
    274 	struct pci_attach_args *pa = aux;
    275 	const struct bce_product *bp;
    276 	pci_chipset_tag_t pc = pa->pa_pc;
    277 	pci_intr_handle_t ih;
    278 	const char     *intrstr = NULL;
    279 	void *        kva;
    280 	bus_dma_segment_t seg;
    281 	int             rseg;
    282 	u_int32_t       command;
    283 	struct ifnet   *ifp;
    284 	pcireg_t        memtype;
    285 	bus_addr_t      memaddr;
    286 	bus_size_t      memsize;
    287 	int             pmreg;
    288 	pcireg_t        pmode;
    289 	int             error;
    290 	int             i;
    291 
    292 	bp = bce_lookup(pa);
    293 	KASSERT(bp != NULL);
    294 
    295 	sc->bce_pa = *pa;
    296 
    297 	/* BCM440x can only address 30 bits (1GB) */
    298 	if (bus_dmatag_subregion(pa->pa_dmat, 0, (1 << 30),
    299 			        &(sc->bce_dmatag), BUS_DMA_NOWAIT) != 0)
    300 	{
    301 		APRINT_ERROR("WARNING: %s failed to restrict dma range,"
    302 			     " falling back to parent bus dma range\n",
    303 			     sc->bce_dev.dv_xname);
    304 		sc->bce_dmatag = pa->pa_dmat;
    305 	}
    306 
    307 #if __NetBSD_Version__ >= 106120000
    308 	 aprint_naive(": Ethernet controller\n");
    309 #endif
    310 	 APRINT_NORMAL(": %s\n", bp->bp_name);
    311 
    312 	/*
    313 	 * Map control/status registers.
    314 	 */
    315 	command = pci_conf_read(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
    316 	command |= PCI_COMMAND_MEM_ENABLE | PCI_COMMAND_MASTER_ENABLE;
    317 	pci_conf_write(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG, command);
    318 	command = pci_conf_read(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
    319 
    320 	if (!(command & PCI_COMMAND_MEM_ENABLE)) {
    321 		APRINT_ERROR("%s: failed to enable memory mapping!\n",
    322 		    sc->bce_dev.dv_xname);
    323 		return;
    324 	}
    325 	memtype = pci_mapreg_type(pa->pa_pc, pa->pa_tag, BCE_PCI_BAR0);
    326 	switch (memtype) {
    327 	case PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT:
    328 	case PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_64BIT:
    329 		if (pci_mapreg_map(pa, BCE_PCI_BAR0, memtype, 0, &sc->bce_btag,
    330 		    &sc->bce_bhandle, &memaddr, &memsize) == 0)
    331 			break;
    332 	default:
    333 		APRINT_ERROR("%s: unable to find mem space\n",
    334 		    sc->bce_dev.dv_xname);
    335 		return;
    336 	}
    337 
    338 	/* Get it out of power save mode if needed. */
    339 	if (pci_get_capability(pc, pa->pa_tag, PCI_CAP_PWRMGMT, &pmreg, 0)) {
    340 		pmode = pci_conf_read(pc, pa->pa_tag, pmreg + 4) & 0x3;
    341 		if (pmode == 3) {
    342 			/*
    343 			 * The card has lost all configuration data in
    344 			 * this state, so punt.
    345 			 */
    346 			printf("%s: unable to wake up from power state D3\n",
    347 			       sc->bce_dev.dv_xname);
    348 			return;
    349 		}
    350 		if (pmode != 0) {
    351 			printf("%s: waking up from power state D%d\n",
    352 			       sc->bce_dev.dv_xname, pmode);
    353 			pci_conf_write(pc, pa->pa_tag, pmreg + 4, 0);
    354 		}
    355 	}
    356 	if (pci_intr_map(pa, &ih)) {
    357 		APRINT_ERROR("%s: couldn't map interrupt\n",
    358 		    sc->bce_dev.dv_xname);
    359 		return;
    360 	}
    361 	intrstr = pci_intr_string(pc, ih);
    362 
    363 	sc->bce_intrhand = pci_intr_establish(pc, ih, IPL_NET, bce_intr, sc);
    364 
    365 	if (sc->bce_intrhand == NULL) {
    366 		APRINT_ERROR("%s: couldn't establish interrupt",
    367 		    sc->bce_dev.dv_xname);
    368 		if (intrstr != NULL)
    369 			APRINT_NORMAL(" at %s", intrstr);
    370 		APRINT_NORMAL("\n");
    371 		return;
    372 	}
    373 	APRINT_NORMAL("%s: interrupting at %s\n",
    374 	    sc->bce_dev.dv_xname, intrstr);
    375 
    376 	/* reset the chip */
    377 	bce_reset(sc);
    378 
    379 	/*
    380 	 * Allocate DMA-safe memory for ring descriptors.
    381 	 * The receive, and transmit rings can not share the same
    382 	 * 4k space, however both are allocated at once here.
    383 	 */
    384 	/*
    385 	 * XXX PAGE_SIZE is wasteful; we only need 1KB + 1KB, but
    386 	 * due to the limition above. ??
    387 	 */
    388 	if ((error = bus_dmamem_alloc(sc->bce_dmatag,
    389 	    2 * PAGE_SIZE, PAGE_SIZE, 2 * PAGE_SIZE,
    390 				      &seg, 1, &rseg, BUS_DMA_NOWAIT))) {
    391 		printf("%s: unable to alloc space for ring descriptors, "
    392 		       "error = %d\n", sc->bce_dev.dv_xname, error);
    393 		return;
    394 	}
    395 	/* map ring space to kernel */
    396 	if ((error = bus_dmamem_map(sc->bce_dmatag, &seg, rseg,
    397 	    2 * PAGE_SIZE, &kva, BUS_DMA_NOWAIT))) {
    398 		printf("%s: unable to map DMA buffers, error = %d\n",
    399 		    sc->bce_dev.dv_xname, error);
    400 		bus_dmamem_free(sc->bce_dmatag, &seg, rseg);
    401 		return;
    402 	}
    403 	/* create a dma map for the ring */
    404 	if ((error = bus_dmamap_create(sc->bce_dmatag,
    405 	    2 * PAGE_SIZE, 1, 2 * PAGE_SIZE, 0, BUS_DMA_NOWAIT,
    406 				       &sc->bce_ring_map))) {
    407 		printf("%s: unable to create ring DMA map, error = %d\n",
    408 		    sc->bce_dev.dv_xname, error);
    409 		bus_dmamem_unmap(sc->bce_dmatag, kva, 2 * PAGE_SIZE);
    410 		bus_dmamem_free(sc->bce_dmatag, &seg, rseg);
    411 		return;
    412 	}
    413 	/* connect the ring space to the dma map */
    414 	if (bus_dmamap_load(sc->bce_dmatag, sc->bce_ring_map, kva,
    415 	    2 * PAGE_SIZE, NULL, BUS_DMA_NOWAIT)) {
    416 		bus_dmamap_destroy(sc->bce_dmatag, sc->bce_ring_map);
    417 		bus_dmamem_unmap(sc->bce_dmatag, kva, 2 * PAGE_SIZE);
    418 		bus_dmamem_free(sc->bce_dmatag, &seg, rseg);
    419 		return;
    420 	}
    421 	/* save the ring space in softc */
    422 	sc->bce_rx_ring = (struct bce_dma_slot *) kva;
    423 	sc->bce_tx_ring = (struct bce_dma_slot *) ((char *)kva + PAGE_SIZE);
    424 
    425 	/* Create the transmit buffer DMA maps. */
    426 	for (i = 0; i < BCE_NTXDESC; i++) {
    427 		if ((error = bus_dmamap_create(sc->bce_dmatag, MCLBYTES,
    428 		    BCE_NTXFRAGS, MCLBYTES, 0, 0, &sc->bce_cdata.bce_tx_map[i])) != 0) {
    429 			printf("%s: unable to create tx DMA map, error = %d\n",
    430 			    sc->bce_dev.dv_xname, error);
    431 		}
    432 		sc->bce_cdata.bce_tx_chain[i] = NULL;
    433 	}
    434 
    435 	/* Create the receive buffer DMA maps. */
    436 	for (i = 0; i < BCE_NRXDESC; i++) {
    437 		if ((error = bus_dmamap_create(sc->bce_dmatag, MCLBYTES, 1,
    438 		    MCLBYTES, 0, 0, &sc->bce_cdata.bce_rx_map[i])) != 0) {
    439 			printf("%s: unable to create rx DMA map, error = %d\n",
    440 			    sc->bce_dev.dv_xname, error);
    441 		}
    442 		sc->bce_cdata.bce_rx_chain[i] = NULL;
    443 	}
    444 
    445 	/* Set up ifnet structure */
    446 	ifp = &sc->ethercom.ec_if;
    447 	strcpy(ifp->if_xname, sc->bce_dev.dv_xname);
    448 	ifp->if_softc = sc;
    449 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
    450 	ifp->if_ioctl = bce_ioctl;
    451 	ifp->if_start = bce_start;
    452 	ifp->if_watchdog = bce_watchdog;
    453 	ifp->if_init = bce_init;
    454 	ifp->if_stop = bce_stop;
    455 	IFQ_SET_READY(&ifp->if_snd);
    456 
    457 	/* Initialize our media structures and probe the MII. */
    458 
    459 	sc->bce_mii.mii_ifp = ifp;
    460 	sc->bce_mii.mii_readreg = bce_mii_read;
    461 	sc->bce_mii.mii_writereg = bce_mii_write;
    462 	sc->bce_mii.mii_statchg = bce_statchg;
    463 	ifmedia_init(&sc->bce_mii.mii_media, 0, bce_mediachange,
    464 	    bce_mediastatus);
    465 	mii_attach(&sc->bce_dev, &sc->bce_mii, 0xffffffff, MII_PHY_ANY,
    466 	    MII_OFFSET_ANY, 0);
    467 	if (LIST_FIRST(&sc->bce_mii.mii_phys) == NULL) {
    468 		ifmedia_add(&sc->bce_mii.mii_media, IFM_ETHER | IFM_NONE, 0, NULL);
    469 		ifmedia_set(&sc->bce_mii.mii_media, IFM_ETHER | IFM_NONE);
    470 	} else
    471 		ifmedia_set(&sc->bce_mii.mii_media, IFM_ETHER | IFM_AUTO);
    472 	/* get the phy */
    473 	sc->bce_phy = bus_space_read_1(sc->bce_btag, sc->bce_bhandle,
    474 	    BCE_MAGIC_PHY) & 0x1f;
    475 	/*
    476 	 * Enable activity led.
    477 	 * XXX This should be in a phy driver, but not currently.
    478 	 */
    479 	bce_mii_write((struct device *) sc, 1, 26,	 /* MAGIC */
    480 	    bce_mii_read((struct device *) sc, 1, 26) & 0x7fff);	 /* MAGIC */
    481 	/* enable traffic meter led mode */
    482 	bce_mii_write((struct device *) sc, 1, 27,	 /* MAGIC */
    483 	    bce_mii_read((struct device *) sc, 1, 27) | (1 << 6));	 /* MAGIC */
    484 
    485 
    486 	/* Attach the interface */
    487 	if_attach(ifp);
    488 	sc->enaddr[0] = bus_space_read_1(sc->bce_btag, sc->bce_bhandle,
    489 	    BCE_MAGIC_ENET0);
    490 	sc->enaddr[1] = bus_space_read_1(sc->bce_btag, sc->bce_bhandle,
    491 	    BCE_MAGIC_ENET1);
    492 	sc->enaddr[2] = bus_space_read_1(sc->bce_btag, sc->bce_bhandle,
    493 	    BCE_MAGIC_ENET2);
    494 	sc->enaddr[3] = bus_space_read_1(sc->bce_btag, sc->bce_bhandle,
    495 	    BCE_MAGIC_ENET3);
    496 	sc->enaddr[4] = bus_space_read_1(sc->bce_btag, sc->bce_bhandle,
    497 	    BCE_MAGIC_ENET4);
    498 	sc->enaddr[5] = bus_space_read_1(sc->bce_btag, sc->bce_bhandle,
    499 	    BCE_MAGIC_ENET5);
    500 	printf("%s: Ethernet address %s\n", sc->bce_dev.dv_xname,
    501 	       ether_sprintf(sc->enaddr));
    502 	ether_ifattach(ifp, sc->enaddr);
    503 	callout_init(&sc->bce_timeout);
    504 }
    505 
    506 /* handle media, and ethernet requests */
    507 static int
    508 bce_ioctl(struct ifnet *ifp, u_long cmd, void *data)
    509 {
    510 	struct bce_softc *sc = ifp->if_softc;
    511 	struct ifreq   *ifr = (struct ifreq *) data;
    512 	int             s, error;
    513 
    514 	s = splnet();
    515 	switch (cmd) {
    516 	case SIOCSIFMEDIA:
    517 	case SIOCGIFMEDIA:
    518 		error = ifmedia_ioctl(ifp, ifr, &sc->bce_mii.mii_media, cmd);
    519 		break;
    520 	default:
    521 		error = ether_ioctl(ifp, cmd, data);
    522 		if (error == ENETRESET) {
    523 			/* change multicast list */
    524 			error = 0;
    525 		}
    526 		break;
    527 	}
    528 
    529 	/* Try to get more packets going. */
    530 	bce_start(ifp);
    531 
    532 	splx(s);
    533 	return error;
    534 }
    535 
    536 /* Start packet transmission on the interface. */
    537 static void
    538 bce_start(struct ifnet *ifp)
    539 {
    540 	struct bce_softc *sc = ifp->if_softc;
    541 	struct mbuf    *m0;
    542 	bus_dmamap_t    dmamap;
    543 	int             txstart;
    544 	int             txsfree;
    545 	int             newpkts = 0;
    546 	int             error;
    547 
    548 	/*
    549          * do not start another if currently transmitting, and more
    550          * descriptors(tx slots) are needed for next packet.
    551          */
    552 	if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
    553 		return;
    554 
    555 	/* determine number of descriptors available */
    556 	if (sc->bce_txsnext >= sc->bce_txin)
    557 		txsfree = BCE_NTXDESC - 1 + sc->bce_txin - sc->bce_txsnext;
    558 	else
    559 		txsfree = sc->bce_txin - sc->bce_txsnext - 1;
    560 
    561 	/*
    562          * Loop through the send queue, setting up transmit descriptors
    563          * until we drain the queue, or use up all available transmit
    564          * descriptors.
    565          */
    566 	while (txsfree > 0) {
    567 		int             seg;
    568 
    569 		/* Grab a packet off the queue. */
    570 		IFQ_POLL(&ifp->if_snd, m0);
    571 		if (m0 == NULL)
    572 			break;
    573 
    574 		/* get the transmit slot dma map */
    575 		dmamap = sc->bce_cdata.bce_tx_map[sc->bce_txsnext];
    576 
    577 		/*
    578 		 * Load the DMA map.  If this fails, the packet either
    579 		 * didn't fit in the alloted number of segments, or we
    580 		 * were short on resources. If the packet will not fit,
    581 		 * it will be dropped. If short on resources, it will
    582 		 * be tried again later.
    583 		 */
    584 		error = bus_dmamap_load_mbuf(sc->bce_dmatag, dmamap, m0,
    585 		    BUS_DMA_WRITE | BUS_DMA_NOWAIT);
    586 		if (error == EFBIG) {
    587 			printf("%s: Tx packet consumes too many DMA segments, "
    588 			    "dropping...\n", sc->bce_dev.dv_xname);
    589 			IFQ_DEQUEUE(&ifp->if_snd, m0);
    590 			m_freem(m0);
    591 			ifp->if_oerrors++;
    592 			continue;
    593 		} else if (error) {
    594 			/* short on resources, come back later */
    595 			printf("%s: unable to load Tx buffer, error = %d\n",
    596 			    sc->bce_dev.dv_xname, error);
    597 			break;
    598 		}
    599 		/* If not enough descriptors available, try again later */
    600 		if (dmamap->dm_nsegs > txsfree) {
    601 			ifp->if_flags |= IFF_OACTIVE;
    602 			bus_dmamap_unload(sc->bce_dmatag, dmamap);
    603 			break;
    604 		}
    605 		/* WE ARE NOW COMMITTED TO TRANSMITTING THE PACKET. */
    606 
    607 		/* So take it off the queue */
    608 		IFQ_DEQUEUE(&ifp->if_snd, m0);
    609 
    610 		/* save the pointer so it can be freed later */
    611 		sc->bce_cdata.bce_tx_chain[sc->bce_txsnext] = m0;
    612 
    613 		/* Sync the data DMA map. */
    614 		bus_dmamap_sync(sc->bce_dmatag, dmamap, 0, dmamap->dm_mapsize,
    615 				BUS_DMASYNC_PREWRITE);
    616 
    617 		/* Initialize the transmit descriptor(s). */
    618 		txstart = sc->bce_txsnext;
    619 		for (seg = 0; seg < dmamap->dm_nsegs; seg++) {
    620 			u_int32_t ctrl;
    621 
    622 			ctrl = dmamap->dm_segs[seg].ds_len & CTRL_BC_MASK;
    623 			if (seg == 0)
    624 				ctrl |= CTRL_SOF;
    625 			if (seg == dmamap->dm_nsegs - 1)
    626 				ctrl |= CTRL_EOF;
    627 			if (sc->bce_txsnext == BCE_NTXDESC - 1)
    628 				ctrl |= CTRL_EOT;
    629 			ctrl |= CTRL_IOC;
    630 			sc->bce_tx_ring[sc->bce_txsnext].ctrl = htole32(ctrl);
    631 			sc->bce_tx_ring[sc->bce_txsnext].addr =
    632 			    htole32(dmamap->dm_segs[seg].ds_addr + 0x40000000);	/* MAGIC */
    633 			if (sc->bce_txsnext + 1 > BCE_NTXDESC - 1)
    634 				sc->bce_txsnext = 0;
    635 			else
    636 				sc->bce_txsnext++;
    637 			txsfree--;
    638 		}
    639 		/* sync descriptors being used */
    640 		bus_dmamap_sync(sc->bce_dmatag, sc->bce_ring_map,
    641 			  sizeof(struct bce_dma_slot) * txstart + PAGE_SIZE,
    642 			     sizeof(struct bce_dma_slot) * dmamap->dm_nsegs,
    643 				BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
    644 
    645 		/* Give the packet to the chip. */
    646 		bus_space_write_4(sc->bce_btag, sc->bce_bhandle, BCE_DMA_DPTR,
    647 			     sc->bce_txsnext * sizeof(struct bce_dma_slot));
    648 
    649 		newpkts++;
    650 
    651 #if NBPFILTER > 0
    652 		/* Pass the packet to any BPF listeners. */
    653 		if (ifp->if_bpf)
    654 			bpf_mtap(ifp->if_bpf, m0);
    655 #endif				/* NBPFILTER > 0 */
    656 	}
    657 	if (txsfree == 0) {
    658 		/* No more slots left; notify upper layer. */
    659 		ifp->if_flags |= IFF_OACTIVE;
    660 	}
    661 	if (newpkts) {
    662 		/* Set a watchdog timer in case the chip flakes out. */
    663 		ifp->if_timer = 5;
    664 	}
    665 }
    666 
    667 /* Watchdog timer handler. */
    668 static void
    669 bce_watchdog(struct ifnet *ifp)
    670 {
    671 	struct bce_softc *sc = ifp->if_softc;
    672 
    673 	printf("%s: device timeout\n", sc->bce_dev.dv_xname);
    674 	ifp->if_oerrors++;
    675 
    676 	(void) bce_init(ifp);
    677 
    678 	/* Try to get more packets going. */
    679 	bce_start(ifp);
    680 }
    681 
    682 int
    683 bce_intr(void *xsc)
    684 {
    685 	struct bce_softc *sc;
    686 	struct ifnet   *ifp;
    687 	u_int32_t intstatus;
    688 	int             wantinit;
    689 	int             handled = 0;
    690 
    691 	sc = xsc;
    692 	ifp = &sc->ethercom.ec_if;
    693 
    694 
    695 	for (wantinit = 0; wantinit == 0;) {
    696 		intstatus = bus_space_read_4(sc->bce_btag, sc->bce_bhandle,
    697 		    BCE_INT_STS);
    698 
    699 		/* ignore if not ours, or unsolicited interrupts */
    700 		intstatus &= sc->bce_intmask;
    701 		if (intstatus == 0)
    702 			break;
    703 
    704 		handled = 1;
    705 
    706 		/* Ack interrupt */
    707 		bus_space_write_4(sc->bce_btag, sc->bce_bhandle, BCE_INT_STS,
    708 		    intstatus);
    709 
    710 		/* Receive interrupts. */
    711 		if (intstatus & I_RI)
    712 			bce_rxintr(sc);
    713 		/* Transmit interrupts. */
    714 		if (intstatus & I_XI)
    715 			bce_txintr(sc);
    716 		/* Error interrupts */
    717 		if (intstatus & ~(I_RI | I_XI)) {
    718 			if (intstatus & I_XU)
    719 				printf("%s: transmit fifo underflow\n",
    720 				    sc->bce_dev.dv_xname);
    721 			if (intstatus & I_RO) {
    722 				printf("%s: receive fifo overflow\n",
    723 				    sc->bce_dev.dv_xname);
    724 				ifp->if_ierrors++;
    725 			}
    726 			if (intstatus & I_RU)
    727 				printf("%s: receive descriptor underflow\n",
    728 				       sc->bce_dev.dv_xname);
    729 			if (intstatus & I_DE)
    730 				printf("%s: descriptor protocol error\n",
    731 				       sc->bce_dev.dv_xname);
    732 			if (intstatus & I_PD)
    733 				printf("%s: data error\n",
    734 				    sc->bce_dev.dv_xname);
    735 			if (intstatus & I_PC)
    736 				printf("%s: descriptor error\n",
    737 				    sc->bce_dev.dv_xname);
    738 			if (intstatus & I_TO)
    739 				printf("%s: general purpose timeout\n",
    740 				    sc->bce_dev.dv_xname);
    741 			wantinit = 1;
    742 		}
    743 	}
    744 
    745 	if (handled) {
    746 		if (wantinit)
    747 			bce_init(ifp);
    748 		/* Try to get more packets going. */
    749 		bce_start(ifp);
    750 	}
    751 	return (handled);
    752 }
    753 
    754 /* Receive interrupt handler */
    755 void
    756 bce_rxintr(struct bce_softc *sc)
    757 {
    758 	struct ifnet   *ifp = &sc->ethercom.ec_if;
    759 	struct rx_pph  *pph;
    760 	struct mbuf    *m;
    761 	int             curr;
    762 	int             len;
    763 	int             i;
    764 
    765 	/* get pointer to active receive slot */
    766 	curr = bus_space_read_4(sc->bce_btag, sc->bce_bhandle, BCE_DMA_RXSTATUS)
    767 	    & RS_CD_MASK;
    768 	curr = curr / sizeof(struct bce_dma_slot);
    769 	if (curr >= BCE_NRXDESC)
    770 		curr = BCE_NRXDESC - 1;
    771 
    772 	/* process packets up to but not current packet being worked on */
    773 	for (i = sc->bce_rxin; i != curr;
    774 	    i + 1 > BCE_NRXDESC - 1 ? i = 0 : i++) {
    775 		/* complete any post dma memory ops on packet */
    776 		bus_dmamap_sync(sc->bce_dmatag, sc->bce_cdata.bce_rx_map[i], 0,
    777 		    sc->bce_cdata.bce_rx_map[i]->dm_mapsize,
    778 		    BUS_DMASYNC_POSTREAD);
    779 
    780 		/*
    781 		 * If the packet had an error, simply recycle the buffer,
    782 		 * resetting the len, and flags.
    783 		 */
    784 		pph = mtod(sc->bce_cdata.bce_rx_chain[i], struct rx_pph *);
    785 		if (pph->flags & (RXF_NO | RXF_RXER | RXF_CRC | RXF_OV)) {
    786 			ifp->if_ierrors++;
    787 			pph->len = 0;
    788 			pph->flags = 0;
    789 			continue;
    790 		}
    791 		/* receive the packet */
    792 		len = pph->len;
    793 		if (len == 0)
    794 			continue;	/* no packet if empty */
    795 		pph->len = 0;
    796 		pph->flags = 0;
    797 		/* bump past pre header to packet */
    798 		sc->bce_cdata.bce_rx_chain[i]->m_data += 30;	/* MAGIC */
    799 
    800 		/*
    801 		 * The chip includes the CRC with every packet.  Trim
    802 		 * it off here.
    803 		 */
    804 		len -= ETHER_CRC_LEN;
    805 
    806 		/*
    807 		 * If the packet is small enough to fit in a
    808 		 * single header mbuf, allocate one and copy
    809 		 * the data into it.  This greatly reduces
    810 		 * memory consumption when receiving lots
    811 		 * of small packets.
    812 		 *
    813 		 * Otherwise, add a new buffer to the receive
    814 		 * chain.  If this fails, drop the packet and
    815 		 * recycle the old buffer.
    816 		 */
    817 		if (len <= (MHLEN - 2)) {
    818 			MGETHDR(m, M_DONTWAIT, MT_DATA);
    819 			if (m == NULL)
    820 				goto dropit;
    821 			m->m_data += 2;
    822 			memcpy(mtod(m, void *),
    823 			 mtod(sc->bce_cdata.bce_rx_chain[i], void *), len);
    824 			sc->bce_cdata.bce_rx_chain[i]->m_data -= 30;	/* MAGIC */
    825 		} else {
    826 			m = sc->bce_cdata.bce_rx_chain[i];
    827 			if (bce_add_rxbuf(sc, i) != 0) {
    828 		dropit:
    829 				ifp->if_ierrors++;
    830 				/* continue to use old buffer */
    831 				sc->bce_cdata.bce_rx_chain[i]->m_data -= 30;
    832 				bus_dmamap_sync(sc->bce_dmatag,
    833 				    sc->bce_cdata.bce_rx_map[i], 0,
    834 				    sc->bce_cdata.bce_rx_map[i]->dm_mapsize,
    835 				    BUS_DMASYNC_PREREAD);
    836 				continue;
    837 			}
    838 		}
    839 
    840 		m->m_pkthdr.rcvif = ifp;
    841 		m->m_pkthdr.len = m->m_len = len;
    842 		ifp->if_ipackets++;
    843 
    844 #if NBPFILTER > 0
    845 		/*
    846 		 * Pass this up to any BPF listeners, but only
    847 		 * pass it up the stack if it's for us.
    848 		 */
    849 		if (ifp->if_bpf)
    850 			bpf_mtap(ifp->if_bpf, m);
    851 #endif				/* NBPFILTER > 0 */
    852 
    853 		/* Pass it on. */
    854 		(*ifp->if_input) (ifp, m);
    855 
    856 		/* re-check current in case it changed */
    857 		curr = (bus_space_read_4(sc->bce_btag, sc->bce_bhandle,
    858 		    BCE_DMA_RXSTATUS) & RS_CD_MASK) /
    859 		    sizeof(struct bce_dma_slot);
    860 		if (curr >= BCE_NRXDESC)
    861 			curr = BCE_NRXDESC - 1;
    862 	}
    863 	sc->bce_rxin = curr;
    864 }
    865 
    866 /* Transmit interrupt handler */
    867 void
    868 bce_txintr(struct bce_softc *sc)
    869 {
    870 	struct ifnet   *ifp = &sc->ethercom.ec_if;
    871 	int             curr;
    872 	int             i;
    873 
    874 	ifp->if_flags &= ~IFF_OACTIVE;
    875 
    876 	/*
    877          * Go through the Tx list and free mbufs for those
    878          * frames which have been transmitted.
    879          */
    880 	curr = bus_space_read_4(sc->bce_btag, sc->bce_bhandle, BCE_DMA_TXSTATUS) &
    881 		RS_CD_MASK;
    882 	curr = curr / sizeof(struct bce_dma_slot);
    883 	if (curr >= BCE_NTXDESC)
    884 		curr = BCE_NTXDESC - 1;
    885 	for (i = sc->bce_txin; i != curr;
    886 	    i + 1 > BCE_NTXDESC - 1 ? i = 0 : i++) {
    887 		/* do any post dma memory ops on transmit data */
    888 		if (sc->bce_cdata.bce_tx_chain[i] == NULL)
    889 			continue;
    890 		bus_dmamap_sync(sc->bce_dmatag, sc->bce_cdata.bce_tx_map[i], 0,
    891 		    sc->bce_cdata.bce_tx_map[i]->dm_mapsize,
    892 		    BUS_DMASYNC_POSTWRITE);
    893 		bus_dmamap_unload(sc->bce_dmatag, sc->bce_cdata.bce_tx_map[i]);
    894 		m_freem(sc->bce_cdata.bce_tx_chain[i]);
    895 		sc->bce_cdata.bce_tx_chain[i] = NULL;
    896 		ifp->if_opackets++;
    897 	}
    898 	sc->bce_txin = curr;
    899 
    900 	/*
    901 	 * If there are no more pending transmissions, cancel the watchdog
    902 	 * timer
    903 	 */
    904 	if (sc->bce_txsnext == sc->bce_txin)
    905 		ifp->if_timer = 0;
    906 }
    907 
    908 /* initialize the interface */
    909 static int
    910 bce_init(struct ifnet *ifp)
    911 {
    912 	struct bce_softc *sc = ifp->if_softc;
    913 	u_int32_t reg_win;
    914 	int             error;
    915 	int             i;
    916 
    917 	/* Cancel any pending I/O. */
    918 	bce_stop(ifp, 0);
    919 
    920 	/* enable pci inerrupts, bursts, and prefetch */
    921 
    922 	/* remap the pci registers to the Sonics config registers */
    923 
    924 	/* save the current map, so it can be restored */
    925 	reg_win = pci_conf_read(sc->bce_pa.pa_pc, sc->bce_pa.pa_tag,
    926 	    BCE_REG_WIN);
    927 
    928 	/* set register window to Sonics registers */
    929 	pci_conf_write(sc->bce_pa.pa_pc, sc->bce_pa.pa_tag, BCE_REG_WIN,
    930 	    BCE_SONICS_WIN);
    931 
    932 	/* enable SB to PCI interrupt */
    933 	bus_space_write_4(sc->bce_btag, sc->bce_bhandle, BCE_SBINTVEC,
    934 	    bus_space_read_4(sc->bce_btag, sc->bce_bhandle, BCE_SBINTVEC) |
    935 	    SBIV_ENET0);
    936 
    937 	/* enable prefetch and bursts for sonics-to-pci translation 2 */
    938 	bus_space_write_4(sc->bce_btag, sc->bce_bhandle, BCE_SPCI_TR2,
    939 	    bus_space_read_4(sc->bce_btag, sc->bce_bhandle, BCE_SPCI_TR2) |
    940 	    SBTOPCI_PREF | SBTOPCI_BURST);
    941 
    942 	/* restore to ethernet register space */
    943 	pci_conf_write(sc->bce_pa.pa_pc, sc->bce_pa.pa_tag, BCE_REG_WIN,
    944 	    reg_win);
    945 
    946 	/* Reset the chip to a known state. */
    947 	bce_reset(sc);
    948 
    949 	/* Initialize transmit descriptors */
    950 	memset(sc->bce_tx_ring, 0, BCE_NTXDESC * sizeof(struct bce_dma_slot));
    951 	sc->bce_txsnext = 0;
    952 	sc->bce_txin = 0;
    953 
    954 	/* enable crc32 generation */
    955 	bus_space_write_4(sc->bce_btag, sc->bce_bhandle, BCE_MACCTL,
    956 	    bus_space_read_4(sc->bce_btag, sc->bce_bhandle, BCE_MACCTL) |
    957 	    BCE_EMC_CG);
    958 
    959 	/* setup DMA interrupt control */
    960 	bus_space_write_4(sc->bce_btag, sc->bce_bhandle, BCE_DMAI_CTL, 1 << 24);	/* MAGIC */
    961 
    962 	/* setup packet filter */
    963 	bce_set_filter(ifp);
    964 
    965 	/* set max frame length, account for possible vlan tag */
    966 	bus_space_write_4(sc->bce_btag, sc->bce_bhandle, BCE_RX_MAX,
    967 	    ETHER_MAX_LEN + 32);
    968 	bus_space_write_4(sc->bce_btag, sc->bce_bhandle, BCE_TX_MAX,
    969 	    ETHER_MAX_LEN + 32);
    970 
    971 	/* set tx watermark */
    972 	bus_space_write_4(sc->bce_btag, sc->bce_bhandle, BCE_TX_WATER, 56);
    973 
    974 	/* enable transmit */
    975 	bus_space_write_4(sc->bce_btag, sc->bce_bhandle, BCE_DMA_TXCTL, XC_XE);
    976 	bus_space_write_4(sc->bce_btag, sc->bce_bhandle, BCE_DMA_TXADDR,
    977 	    sc->bce_ring_map->dm_segs[0].ds_addr + PAGE_SIZE + 0x40000000);	/* MAGIC */
    978 
    979 	/*
    980          * Give the receive ring to the chip, and
    981          * start the receive DMA engine.
    982          */
    983 	sc->bce_rxin = 0;
    984 
    985 	/* clear the rx descriptor ring */
    986 	memset(sc->bce_rx_ring, 0, BCE_NRXDESC * sizeof(struct bce_dma_slot));
    987 	/* enable receive */
    988 	bus_space_write_4(sc->bce_btag, sc->bce_bhandle, BCE_DMA_RXCTL,
    989 	    30 << 1 | 1);	/* MAGIC */
    990 	bus_space_write_4(sc->bce_btag, sc->bce_bhandle, BCE_DMA_RXADDR,
    991 	    sc->bce_ring_map->dm_segs[0].ds_addr + 0x40000000);		/* MAGIC */
    992 
    993 	/* Initalize receive descriptors */
    994 	for (i = 0; i < BCE_NRXDESC; i++) {
    995 		if (sc->bce_cdata.bce_rx_chain[i] == NULL) {
    996 			if ((error = bce_add_rxbuf(sc, i)) != 0) {
    997 				printf("%s: unable to allocate or map rx(%d) "
    998 				    "mbuf, error = %d\n", sc->bce_dev.dv_xname,
    999 				    i, error);
   1000 				bce_rxdrain(sc);
   1001 				return (error);
   1002 			}
   1003 		} else
   1004 			BCE_INIT_RXDESC(sc, i);
   1005 	}
   1006 
   1007 	/* Enable interrupts */
   1008 	sc->bce_intmask =
   1009 	    I_XI | I_RI | I_XU | I_RO | I_RU | I_DE | I_PD | I_PC | I_TO;
   1010 	bus_space_write_4(sc->bce_btag, sc->bce_bhandle, BCE_INT_MASK,
   1011 	    sc->bce_intmask);
   1012 
   1013 	/* start the receive dma */
   1014 	bus_space_write_4(sc->bce_btag, sc->bce_bhandle, BCE_DMA_RXDPTR,
   1015 	    BCE_NRXDESC * sizeof(struct bce_dma_slot));
   1016 
   1017 	/* set media */
   1018 	mii_mediachg(&sc->bce_mii);
   1019 
   1020 	/* turn on the ethernet mac */
   1021 	bus_space_write_4(sc->bce_btag, sc->bce_bhandle, BCE_ENET_CTL,
   1022 	    bus_space_read_4(sc->bce_btag, sc->bce_bhandle,
   1023 	    BCE_ENET_CTL) | EC_EE);
   1024 
   1025 	/* start timer */
   1026 	callout_reset(&sc->bce_timeout, hz, bce_tick, sc);
   1027 
   1028 	/* mark as running, and no outputs active */
   1029 	ifp->if_flags |= IFF_RUNNING;
   1030 	ifp->if_flags &= ~IFF_OACTIVE;
   1031 
   1032 	return 0;
   1033 }
   1034 
   1035 /* add a mac address to packet filter */
   1036 void
   1037 bce_add_mac(struct bce_softc *sc, u_int8_t *mac, u_long idx)
   1038 {
   1039 	int             i;
   1040 	u_int32_t rval;
   1041 
   1042 	bus_space_write_4(sc->bce_btag, sc->bce_bhandle, BCE_FILT_LOW,
   1043 	    mac[2] << 24 | mac[3] << 16 | mac[4] << 8 | mac[5]);
   1044 	bus_space_write_4(sc->bce_btag, sc->bce_bhandle, BCE_FILT_HI,
   1045 	    mac[0] << 8 | mac[1] | 0x10000);	/* MAGIC */
   1046 	bus_space_write_4(sc->bce_btag, sc->bce_bhandle, BCE_FILT_CTL,
   1047 	    idx << 16 | 8);	/* MAGIC */
   1048 	/* wait for write to complete */
   1049 	for (i = 0; i < 100; i++) {
   1050 		rval = bus_space_read_4(sc->bce_btag, sc->bce_bhandle,
   1051 		    BCE_FILT_CTL);
   1052 		if (!(rval & 0x80000000))	/* MAGIC */
   1053 			break;
   1054 		delay(10);
   1055 	}
   1056 	if (i == 100) {
   1057 		printf("%s: timed out writing pkt filter ctl\n",
   1058 		   sc->bce_dev.dv_xname);
   1059 	}
   1060 }
   1061 
   1062 /* Add a receive buffer to the indiciated descriptor. */
   1063 static int
   1064 bce_add_rxbuf(struct bce_softc *sc, int idx)
   1065 {
   1066 	struct mbuf    *m;
   1067 	int             error;
   1068 
   1069 	MGETHDR(m, M_DONTWAIT, MT_DATA);
   1070 	if (m == NULL)
   1071 		return (ENOBUFS);
   1072 
   1073 	MCLGET(m, M_DONTWAIT);
   1074 	if ((m->m_flags & M_EXT) == 0) {
   1075 		m_freem(m);
   1076 		return (ENOBUFS);
   1077 	}
   1078 	if (sc->bce_cdata.bce_rx_chain[idx] != NULL)
   1079 		bus_dmamap_unload(sc->bce_dmatag,
   1080 		    sc->bce_cdata.bce_rx_map[idx]);
   1081 
   1082 	sc->bce_cdata.bce_rx_chain[idx] = m;
   1083 
   1084 	error = bus_dmamap_load(sc->bce_dmatag, sc->bce_cdata.bce_rx_map[idx],
   1085 	    m->m_ext.ext_buf, m->m_ext.ext_size, NULL,
   1086 	    BUS_DMA_READ | BUS_DMA_NOWAIT);
   1087 	if (error)
   1088 		return (error);
   1089 
   1090 	bus_dmamap_sync(sc->bce_dmatag, sc->bce_cdata.bce_rx_map[idx], 0,
   1091 	    sc->bce_cdata.bce_rx_map[idx]->dm_mapsize, BUS_DMASYNC_PREREAD);
   1092 
   1093 	BCE_INIT_RXDESC(sc, idx);
   1094 
   1095 	return (0);
   1096 
   1097 }
   1098 
   1099 /* Drain the receive queue. */
   1100 static void
   1101 bce_rxdrain(struct bce_softc *sc)
   1102 {
   1103 	int             i;
   1104 
   1105 	for (i = 0; i < BCE_NRXDESC; i++) {
   1106 		if (sc->bce_cdata.bce_rx_chain[i] != NULL) {
   1107 			bus_dmamap_unload(sc->bce_dmatag,
   1108 			    sc->bce_cdata.bce_rx_map[i]);
   1109 			m_freem(sc->bce_cdata.bce_rx_chain[i]);
   1110 			sc->bce_cdata.bce_rx_chain[i] = NULL;
   1111 		}
   1112 	}
   1113 }
   1114 
   1115 /* Stop transmission on the interface */
   1116 static void
   1117 bce_stop(struct ifnet *ifp, int disable)
   1118 {
   1119 	struct bce_softc *sc = ifp->if_softc;
   1120 	int             i;
   1121 	u_int32_t val;
   1122 
   1123 	/* Stop the 1 second timer */
   1124 	callout_stop(&sc->bce_timeout);
   1125 
   1126 	/* Down the MII. */
   1127 	mii_down(&sc->bce_mii);
   1128 
   1129 	/* Disable interrupts. */
   1130 	bus_space_write_4(sc->bce_btag, sc->bce_bhandle, BCE_INT_MASK, 0);
   1131 	sc->bce_intmask = 0;
   1132 	delay(10);
   1133 
   1134 	/* Disable emac */
   1135 	bus_space_write_4(sc->bce_btag, sc->bce_bhandle, BCE_ENET_CTL, EC_ED);
   1136 	for (i = 0; i < 200; i++) {
   1137 		val = bus_space_read_4(sc->bce_btag, sc->bce_bhandle,
   1138 		    BCE_ENET_CTL);
   1139 		if (!(val & EC_ED))
   1140 			break;
   1141 		delay(10);
   1142 	}
   1143 
   1144 	/* Stop the DMA */
   1145 	bus_space_write_4(sc->bce_btag, sc->bce_bhandle, BCE_DMA_RXCTL, 0);
   1146 	bus_space_write_4(sc->bce_btag, sc->bce_bhandle, BCE_DMA_TXCTL, 0);
   1147 	delay(10);
   1148 
   1149 	/* Release any queued transmit buffers. */
   1150 	for (i = 0; i < BCE_NTXDESC; i++) {
   1151 		if (sc->bce_cdata.bce_tx_chain[i] != NULL) {
   1152 			bus_dmamap_unload(sc->bce_dmatag,
   1153 			    sc->bce_cdata.bce_tx_map[i]);
   1154 			m_freem(sc->bce_cdata.bce_tx_chain[i]);
   1155 			sc->bce_cdata.bce_tx_chain[i] = NULL;
   1156 		}
   1157 	}
   1158 
   1159 	/* drain receive queue */
   1160 	if (disable)
   1161 		bce_rxdrain(sc);
   1162 
   1163 	/* Mark the interface down and cancel the watchdog timer. */
   1164 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
   1165 	ifp->if_timer = 0;
   1166 }
   1167 
   1168 /* reset the chip */
   1169 static void
   1170 bce_reset(struct bce_softc *sc)
   1171 {
   1172 	u_int32_t val;
   1173 	u_int32_t sbval;
   1174 	int             i;
   1175 
   1176 	/* if SB core is up */
   1177 	sbval = bus_space_read_4(sc->bce_btag, sc->bce_bhandle,
   1178 	    BCE_SBTMSTATELOW);
   1179 	if ((sbval & (SBTML_RESET | SBTML_REJ | SBTML_CLK)) == SBTML_CLK) {
   1180 		bus_space_write_4(sc->bce_btag, sc->bce_bhandle, BCE_DMAI_CTL,
   1181 		    0);
   1182 
   1183 		/* disable emac */
   1184 		bus_space_write_4(sc->bce_btag, sc->bce_bhandle, BCE_ENET_CTL,
   1185 		    EC_ED);
   1186 		for (i = 0; i < 200; i++) {
   1187 			val = bus_space_read_4(sc->bce_btag, sc->bce_bhandle,
   1188 			    BCE_ENET_CTL);
   1189 			if (!(val & EC_ED))
   1190 				break;
   1191 			delay(10);
   1192 		}
   1193 		if (i == 200)
   1194 			printf("%s: timed out disabling ethernet mac\n",
   1195 			       sc->bce_dev.dv_xname);
   1196 
   1197 		/* reset the dma engines */
   1198 		bus_space_write_4(sc->bce_btag, sc->bce_bhandle, BCE_DMA_TXCTL, 0);
   1199 		val = bus_space_read_4(sc->bce_btag, sc->bce_bhandle, BCE_DMA_RXSTATUS);
   1200 		/* if error on receive, wait to go idle */
   1201 		if (val & RS_ERROR) {
   1202 			for (i = 0; i < 100; i++) {
   1203 				val = bus_space_read_4(sc->bce_btag,
   1204 				    sc->bce_bhandle, BCE_DMA_RXSTATUS);
   1205 				if (val & RS_DMA_IDLE)
   1206 					break;
   1207 				delay(10);
   1208 			}
   1209 			if (i == 100)
   1210 				printf("%s: receive dma did not go idle after"
   1211 				    " error\n", sc->bce_dev.dv_xname);
   1212 		}
   1213 		bus_space_write_4(sc->bce_btag, sc->bce_bhandle,
   1214 		   BCE_DMA_RXSTATUS, 0);
   1215 
   1216 		/* reset ethernet mac */
   1217 		bus_space_write_4(sc->bce_btag, sc->bce_bhandle, BCE_ENET_CTL,
   1218 		    EC_ES);
   1219 		for (i = 0; i < 200; i++) {
   1220 			val = bus_space_read_4(sc->bce_btag, sc->bce_bhandle,
   1221 			    BCE_ENET_CTL);
   1222 			if (!(val & EC_ES))
   1223 				break;
   1224 			delay(10);
   1225 		}
   1226 		if (i == 200)
   1227 			printf("%s: timed out restting ethernet mac\n",
   1228 			       sc->bce_dev.dv_xname);
   1229 	} else {
   1230 		u_int32_t reg_win;
   1231 
   1232 		/* remap the pci registers to the Sonics config registers */
   1233 
   1234 		/* save the current map, so it can be restored */
   1235 		reg_win = pci_conf_read(sc->bce_pa.pa_pc, sc->bce_pa.pa_tag,
   1236 		    BCE_REG_WIN);
   1237 		/* set register window to Sonics registers */
   1238 		pci_conf_write(sc->bce_pa.pa_pc, sc->bce_pa.pa_tag,
   1239 		    BCE_REG_WIN, BCE_SONICS_WIN);
   1240 
   1241 		/* enable SB to PCI interrupt */
   1242 		bus_space_write_4(sc->bce_btag, sc->bce_bhandle, BCE_SBINTVEC,
   1243 		    bus_space_read_4(sc->bce_btag, sc->bce_bhandle,
   1244 		        BCE_SBINTVEC) |
   1245 		    SBIV_ENET0);
   1246 
   1247 		/* enable prefetch and bursts for sonics-to-pci translation 2 */
   1248 		bus_space_write_4(sc->bce_btag, sc->bce_bhandle, BCE_SPCI_TR2,
   1249 		    bus_space_read_4(sc->bce_btag, sc->bce_bhandle,
   1250 			BCE_SPCI_TR2) |
   1251 		    SBTOPCI_PREF | SBTOPCI_BURST);
   1252 
   1253 		/* restore to ethernet register space */
   1254 		pci_conf_write(sc->bce_pa.pa_pc, sc->bce_pa.pa_tag, BCE_REG_WIN,
   1255 			       reg_win);
   1256 	}
   1257 
   1258 	/* disable SB core if not in reset */
   1259 	if (!(sbval & SBTML_RESET)) {
   1260 
   1261 		/* set the reject bit */
   1262 		bus_space_write_4(sc->bce_btag, sc->bce_bhandle,
   1263 		    BCE_SBTMSTATELOW, SBTML_REJ | SBTML_CLK);
   1264 		for (i = 0; i < 200; i++) {
   1265 			val = bus_space_read_4(sc->bce_btag, sc->bce_bhandle,
   1266 			    BCE_SBTMSTATELOW);
   1267 			if (val & SBTML_REJ)
   1268 				break;
   1269 			delay(1);
   1270 		}
   1271 		if (i == 200)
   1272 			printf("%s: while restting core, reject did not set\n",
   1273 			    sc->bce_dev.dv_xname);
   1274 		/* wait until busy is clear */
   1275 		for (i = 0; i < 200; i++) {
   1276 			val = bus_space_read_4(sc->bce_btag, sc->bce_bhandle,
   1277 			    BCE_SBTMSTATEHI);
   1278 			if (!(val & 0x4))
   1279 				break;
   1280 			delay(1);
   1281 		}
   1282 		if (i == 200)
   1283 			printf("%s: while restting core, busy did not clear\n",
   1284 			    sc->bce_dev.dv_xname);
   1285 		/* set reset and reject while enabling the clocks */
   1286 		bus_space_write_4(sc->bce_btag, sc->bce_bhandle,
   1287 		    BCE_SBTMSTATELOW,
   1288 		    SBTML_FGC | SBTML_CLK | SBTML_REJ | SBTML_RESET);
   1289 		val = bus_space_read_4(sc->bce_btag, sc->bce_bhandle,
   1290 		    BCE_SBTMSTATELOW);
   1291 		delay(10);
   1292 		bus_space_write_4(sc->bce_btag, sc->bce_bhandle,
   1293 		    BCE_SBTMSTATELOW, SBTML_REJ | SBTML_RESET);
   1294 		delay(1);
   1295 	}
   1296 	/* enable clock */
   1297 	bus_space_write_4(sc->bce_btag, sc->bce_bhandle, BCE_SBTMSTATELOW,
   1298 	    SBTML_FGC | SBTML_CLK | SBTML_RESET);
   1299 	val = bus_space_read_4(sc->bce_btag, sc->bce_bhandle, BCE_SBTMSTATELOW);
   1300 	delay(1);
   1301 
   1302 	/* clear any error bits that may be on */
   1303 	val = bus_space_read_4(sc->bce_btag, sc->bce_bhandle, BCE_SBTMSTATEHI);
   1304 	if (val & 1)
   1305 		bus_space_write_4(sc->bce_btag, sc->bce_bhandle, BCE_SBTMSTATEHI,
   1306 		    0);
   1307 	val = bus_space_read_4(sc->bce_btag, sc->bce_bhandle, BCE_SBIMSTATE);
   1308 	if (val & SBIM_MAGIC_ERRORBITS)
   1309 		bus_space_write_4(sc->bce_btag, sc->bce_bhandle, BCE_SBIMSTATE,
   1310 		    val & ~SBIM_MAGIC_ERRORBITS);
   1311 
   1312 	/* clear reset and allow it to propagate throughout the core */
   1313 	bus_space_write_4(sc->bce_btag, sc->bce_bhandle, BCE_SBTMSTATELOW,
   1314 	    SBTML_FGC | SBTML_CLK);
   1315 	val = bus_space_read_4(sc->bce_btag, sc->bce_bhandle, BCE_SBTMSTATELOW);
   1316 	delay(1);
   1317 
   1318 	/* leave clock enabled */
   1319 	bus_space_write_4(sc->bce_btag, sc->bce_bhandle, BCE_SBTMSTATELOW,
   1320 	    SBTML_CLK);
   1321 	val = bus_space_read_4(sc->bce_btag, sc->bce_bhandle, BCE_SBTMSTATELOW);
   1322 	delay(1);
   1323 
   1324 	/* initialize MDC preamble, frequency */
   1325 	bus_space_write_4(sc->bce_btag, sc->bce_bhandle, BCE_MI_CTL, 0x8d);	/* MAGIC */
   1326 
   1327 	/* enable phy, differs for internal, and external */
   1328 	val = bus_space_read_4(sc->bce_btag, sc->bce_bhandle, BCE_DEVCTL);
   1329 	if (!(val & BCE_DC_IP)) {
   1330 		/* select external phy */
   1331 		bus_space_write_4(sc->bce_btag, sc->bce_bhandle, BCE_ENET_CTL, EC_EP);
   1332 	} else if (val & BCE_DC_ER) {	/* internal, clear reset bit if on */
   1333 		bus_space_write_4(sc->bce_btag, sc->bce_bhandle, BCE_DEVCTL,
   1334 		    val & ~BCE_DC_ER);
   1335 		delay(100);
   1336 	}
   1337 }
   1338 
   1339 /* Set up the receive filter. */
   1340 void
   1341 bce_set_filter(struct ifnet *ifp)
   1342 {
   1343 	struct bce_softc *sc = ifp->if_softc;
   1344 
   1345 	if (ifp->if_flags & IFF_PROMISC) {
   1346 		ifp->if_flags |= IFF_ALLMULTI;
   1347 		bus_space_write_4(sc->bce_btag, sc->bce_bhandle, BCE_RX_CTL,
   1348 		    bus_space_read_4(sc->bce_btag, sc->bce_bhandle, BCE_RX_CTL)
   1349 		    | ERC_PE);
   1350 	} else {
   1351 		ifp->if_flags &= ~IFF_ALLMULTI;
   1352 
   1353 		/* turn off promiscuous */
   1354 		bus_space_write_4(sc->bce_btag, sc->bce_bhandle, BCE_RX_CTL,
   1355 		    bus_space_read_4(sc->bce_btag, sc->bce_bhandle,
   1356 		    BCE_RX_CTL) & ~ERC_PE);
   1357 
   1358 		/* enable/disable broadcast */
   1359 		if (ifp->if_flags & IFF_BROADCAST)
   1360 			bus_space_write_4(sc->bce_btag, sc->bce_bhandle,
   1361 			    BCE_RX_CTL, bus_space_read_4(sc->bce_btag,
   1362 			    sc->bce_bhandle, BCE_RX_CTL) & ~ERC_DB);
   1363 		else
   1364 			bus_space_write_4(sc->bce_btag, sc->bce_bhandle,
   1365 			    BCE_RX_CTL, bus_space_read_4(sc->bce_btag,
   1366 			    sc->bce_bhandle, BCE_RX_CTL) | ERC_DB);
   1367 
   1368 		/* disable the filter */
   1369 		bus_space_write_4(sc->bce_btag, sc->bce_bhandle, BCE_FILT_CTL,
   1370 		    0);
   1371 
   1372 		/* add our own address */
   1373 		bce_add_mac(sc, sc->enaddr, 0);
   1374 
   1375 		/* for now accept all multicast */
   1376 		bus_space_write_4(sc->bce_btag, sc->bce_bhandle, BCE_RX_CTL,
   1377 		bus_space_read_4(sc->bce_btag, sc->bce_bhandle, BCE_RX_CTL) |
   1378 		    ERC_AM);
   1379 		ifp->if_flags |= IFF_ALLMULTI;
   1380 
   1381 		/* enable the filter */
   1382 		bus_space_write_4(sc->bce_btag, sc->bce_bhandle, BCE_FILT_CTL,
   1383 		    bus_space_read_4(sc->bce_btag, sc->bce_bhandle,
   1384 		    BCE_FILT_CTL) | 1);
   1385 	}
   1386 }
   1387 
   1388 /* Read a PHY register on the MII. */
   1389 int
   1390 bce_mii_read(struct device *self, int phy, int reg)
   1391 {
   1392 	struct bce_softc *sc = (struct bce_softc *) self;
   1393 	int             i;
   1394 	u_int32_t val;
   1395 
   1396 	/* clear mii_int */
   1397 	bus_space_write_4(sc->bce_btag, sc->bce_bhandle, BCE_MI_STS, BCE_MIINTR);
   1398 
   1399 	/* Read the PHY register */
   1400 	bus_space_write_4(sc->bce_btag, sc->bce_bhandle, BCE_MI_COMM,
   1401 	    (MII_COMMAND_READ << 28) | (MII_COMMAND_START << 30) |	/* MAGIC */
   1402 	    (MII_COMMAND_ACK << 16) | BCE_MIPHY(phy) | BCE_MIREG(reg));	/* MAGIC */
   1403 
   1404 	for (i = 0; i < BCE_TIMEOUT; i++) {
   1405 		val = bus_space_read_4(sc->bce_btag, sc->bce_bhandle, BCE_MI_STS);
   1406 		if (val & BCE_MIINTR)
   1407 			break;
   1408 		delay(10);
   1409 	}
   1410 	val = bus_space_read_4(sc->bce_btag, sc->bce_bhandle, BCE_MI_COMM);
   1411 	if (i == BCE_TIMEOUT) {
   1412 		printf("%s: PHY read timed out reading phy %d, reg %d, val = "
   1413 		    "0x%08x\n", sc->bce_dev.dv_xname, phy, reg, val);
   1414 		return (0);
   1415 	}
   1416 	return (val & BCE_MICOMM_DATA);
   1417 }
   1418 
   1419 /* Write a PHY register on the MII */
   1420 void
   1421 bce_mii_write(struct device *self, int phy, int reg, int val)
   1422 {
   1423 	struct bce_softc *sc = (struct bce_softc *) self;
   1424 	int             i;
   1425 	u_int32_t rval;
   1426 
   1427 	/* clear mii_int */
   1428 	bus_space_write_4(sc->bce_btag, sc->bce_bhandle, BCE_MI_STS,
   1429 	    BCE_MIINTR);
   1430 
   1431 	/* Write the PHY register */
   1432 	bus_space_write_4(sc->bce_btag, sc->bce_bhandle, BCE_MI_COMM,
   1433 	    (MII_COMMAND_WRITE << 28) | (MII_COMMAND_START << 30) |	/* MAGIC */
   1434 	    (MII_COMMAND_ACK << 16) | (val & BCE_MICOMM_DATA) |	/* MAGIC */
   1435 	    BCE_MIPHY(phy) | BCE_MIREG(reg));
   1436 
   1437 	/* wait for write to complete */
   1438 	for (i = 0; i < BCE_TIMEOUT; i++) {
   1439 		rval = bus_space_read_4(sc->bce_btag, sc->bce_bhandle,
   1440 		    BCE_MI_STS);
   1441 		if (rval & BCE_MIINTR)
   1442 			break;
   1443 		delay(10);
   1444 	}
   1445 	rval = bus_space_read_4(sc->bce_btag, sc->bce_bhandle, BCE_MI_COMM);
   1446 	if (i == BCE_TIMEOUT) {
   1447 		printf("%s: PHY timed out writing phy %d, reg %d, val "
   1448 		    "= 0x%08x\n", sc->bce_dev.dv_xname, phy, reg, val);
   1449 	}
   1450 }
   1451 
   1452 /* sync hardware duplex mode to software state */
   1453 void
   1454 bce_statchg(struct device *self)
   1455 {
   1456 	struct bce_softc *sc = (struct bce_softc *) self;
   1457 	u_int32_t reg;
   1458 
   1459 	/* if needed, change register to match duplex mode */
   1460 	reg = bus_space_read_4(sc->bce_btag, sc->bce_bhandle, BCE_TX_CTL);
   1461 	if (sc->bce_mii.mii_media_active & IFM_FDX && !(reg & EXC_FD))
   1462 		bus_space_write_4(sc->bce_btag, sc->bce_bhandle, BCE_TX_CTL,
   1463 		    reg | EXC_FD);
   1464 	else if (!(sc->bce_mii.mii_media_active & IFM_FDX) && reg & EXC_FD)
   1465 		bus_space_write_4(sc->bce_btag, sc->bce_bhandle, BCE_TX_CTL,
   1466 		    reg & ~EXC_FD);
   1467 
   1468 	/*
   1469          * Enable activity led.
   1470          * XXX This should be in a phy driver, but not currently.
   1471          */
   1472 	bce_mii_write((struct device *) sc, 1, 26,	/* MAGIC */
   1473 	    bce_mii_read((struct device *) sc, 1, 26) & 0x7fff);	/* MAGIC */
   1474 	/* enable traffic meter led mode */
   1475 	bce_mii_write((struct device *) sc, 1, 26,	/* MAGIC */
   1476 	    bce_mii_read((struct device *) sc, 1, 27) | (1 << 6));	/* MAGIC */
   1477 }
   1478 
   1479 /* Set hardware to newly-selected media */
   1480 int
   1481 bce_mediachange(struct ifnet *ifp)
   1482 {
   1483 	struct bce_softc *sc = ifp->if_softc;
   1484 
   1485 	if (ifp->if_flags & IFF_UP)
   1486 		mii_mediachg(&sc->bce_mii);
   1487 	return (0);
   1488 }
   1489 
   1490 /* Get the current interface media status */
   1491 static void
   1492 bce_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr)
   1493 {
   1494 	struct bce_softc *sc = ifp->if_softc;
   1495 
   1496 	mii_pollstat(&sc->bce_mii);
   1497 	ifmr->ifm_active = sc->bce_mii.mii_media_active;
   1498 	ifmr->ifm_status = sc->bce_mii.mii_media_status;
   1499 }
   1500 
   1501 /* One second timer, checks link status */
   1502 static void
   1503 bce_tick(void *v)
   1504 {
   1505 	struct bce_softc *sc = v;
   1506 
   1507 	/* Tick the MII. */
   1508 	mii_tick(&sc->bce_mii);
   1509 
   1510 	callout_reset(&sc->bce_timeout, hz, bce_tick, sc);
   1511 }
   1512