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