Home | History | Annotate | Line # | Download | only in sbus
be.c revision 1.62
      1 /*	$NetBSD: be.c,v 1.62 2009/03/14 15:36:20 dsl Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1999 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Paul Kranenburg.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 /*
     33  * Copyright (c) 1998 Theo de Raadt and Jason L. Wright.
     34  * All rights reserved.
     35  *
     36  * Redistribution and use in source and binary forms, with or without
     37  * modification, are permitted provided that the following conditions
     38  * are met:
     39  * 1. Redistributions of source code must retain the above copyright
     40  *    notice, this list of conditions and the following disclaimer.
     41  * 2. Redistributions in binary form must reproduce the above copyright
     42  *    notice, this list of conditions and the following disclaimer in the
     43  *    documentation and/or other materials provided with the distribution.
     44  * 3. The name of the authors may not be used to endorse or promote products
     45  *    derived from this software without specific prior written permission.
     46  *
     47  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
     48  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     49  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     50  * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
     51  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     52  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     53  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     54  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     55  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     56  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     57  */
     58 
     59 #include <sys/cdefs.h>
     60 __KERNEL_RCSID(0, "$NetBSD: be.c,v 1.62 2009/03/14 15:36:20 dsl Exp $");
     61 
     62 #include "opt_ddb.h"
     63 #include "opt_inet.h"
     64 #include "bpfilter.h"
     65 #include "rnd.h"
     66 
     67 #include <sys/param.h>
     68 #include <sys/systm.h>
     69 #include <sys/callout.h>
     70 #include <sys/kernel.h>
     71 #include <sys/errno.h>
     72 #include <sys/ioctl.h>
     73 #include <sys/mbuf.h>
     74 #include <sys/socket.h>
     75 #include <sys/syslog.h>
     76 #include <sys/device.h>
     77 #include <sys/malloc.h>
     78 #if NRND > 0
     79 #include <sys/rnd.h>
     80 #endif
     81 
     82 #include <net/if.h>
     83 #include <net/if_dl.h>
     84 #include <net/if_types.h>
     85 #include <net/netisr.h>
     86 #include <net/if_media.h>
     87 #include <net/if_ether.h>
     88 
     89 #ifdef INET
     90 #include <netinet/in.h>
     91 #include <netinet/if_inarp.h>
     92 #include <netinet/in_systm.h>
     93 #include <netinet/in_var.h>
     94 #include <netinet/ip.h>
     95 #endif
     96 
     97 
     98 #if NBPFILTER > 0
     99 #include <net/bpf.h>
    100 #include <net/bpfdesc.h>
    101 #endif
    102 
    103 #include <sys/bus.h>
    104 #include <sys/intr.h>
    105 #include <machine/autoconf.h>
    106 
    107 #include <dev/sbus/sbusvar.h>
    108 
    109 #include <dev/mii/mii.h>
    110 #include <dev/mii/miivar.h>
    111 
    112 #include <dev/sbus/qecreg.h>
    113 #include <dev/sbus/qecvar.h>
    114 #include <dev/sbus/bereg.h>
    115 
    116 struct be_softc {
    117 	struct	device	sc_dev;
    118 	struct	sbusdev sc_sd;		/* sbus device */
    119 	bus_space_tag_t	sc_bustag;	/* bus & DMA tags */
    120 	bus_dma_tag_t	sc_dmatag;
    121 	bus_dmamap_t	sc_dmamap;
    122 	struct	ethercom sc_ethercom;
    123 	/*struct	ifmedia sc_ifmedia;	-* interface media */
    124 	struct mii_data	sc_mii;		/* MII media control */
    125 #define sc_media	sc_mii.mii_media/* shorthand */
    126 	int		sc_phys[2];	/* MII instance -> phy */
    127 
    128 	struct callout sc_tick_ch;
    129 
    130 	/*
    131 	 * Some `mii_softc' items we need to emulate MII operation
    132 	 * for our internal transceiver.
    133 	 */
    134 	int		sc_mii_inst;	/* instance of internal phy */
    135 	int		sc_mii_active;	/* currently active medium */
    136 	int		sc_mii_ticks;	/* tick counter */
    137 	int		sc_mii_flags;	/* phy status flags */
    138 #define MIIF_HAVELINK	0x04000000
    139 	int		sc_intphy_curspeed;	/* Established link speed */
    140 
    141 	struct	qec_softc *sc_qec;	/* QEC parent */
    142 
    143 	bus_space_handle_t	sc_qr;	/* QEC registers */
    144 	bus_space_handle_t	sc_br;	/* BE registers */
    145 	bus_space_handle_t	sc_cr;	/* channel registers */
    146 	bus_space_handle_t	sc_tr;	/* transceiver registers */
    147 
    148 	u_int	sc_rev;
    149 
    150 	int	sc_channel;		/* channel number */
    151 	int	sc_burst;
    152 
    153 	struct  qec_ring	sc_rb;	/* Packet Ring Buffer */
    154 
    155 	/* MAC address */
    156 	u_int8_t sc_enaddr[6];
    157 #ifdef BEDEBUG
    158 	int	sc_debug;
    159 #endif
    160 };
    161 
    162 int	bematch(struct device *, struct cfdata *, void *);
    163 void	beattach(struct device *, struct device *, void *);
    164 
    165 void	beinit(struct be_softc *);
    166 void	bestart(struct ifnet *);
    167 void	bestop(struct be_softc *);
    168 void	bewatchdog(struct ifnet *);
    169 int	beioctl(struct ifnet *, u_long, void *);
    170 void	bereset(struct be_softc *);
    171 
    172 int	beintr(void *);
    173 int	berint(struct be_softc *);
    174 int	betint(struct be_softc *);
    175 int	beqint(struct be_softc *, u_int32_t);
    176 int	beeint(struct be_softc *, u_int32_t);
    177 
    178 static void	be_read(struct be_softc *, int, int);
    179 static int	be_put(struct be_softc *, int, struct mbuf *);
    180 static struct mbuf *be_get(struct be_softc *, int, int);
    181 
    182 void	be_pal_gate(struct be_softc *, int);
    183 
    184 /* ifmedia callbacks */
    185 void	be_ifmedia_sts(struct ifnet *, struct ifmediareq *);
    186 int	be_ifmedia_upd(struct ifnet *);
    187 
    188 void	be_mcreset(struct be_softc *);
    189 
    190 /* MII methods & callbacks */
    191 static int	be_mii_readreg(struct device *, int, int);
    192 static void	be_mii_writereg(struct device *, int, int, int);
    193 static void	be_mii_statchg(struct device *);
    194 
    195 /* MII helpers */
    196 static void	be_mii_sync(struct be_softc *);
    197 static void	be_mii_sendbits(struct be_softc *, int, u_int32_t, int);
    198 static int	be_mii_reset(struct be_softc *, int);
    199 static int	be_tcvr_read_bit(struct be_softc *, int);
    200 static void	be_tcvr_write_bit(struct be_softc *, int, int);
    201 
    202 void	be_tick(void *);
    203 void	be_intphy_auto(struct be_softc *);
    204 void	be_intphy_status(struct be_softc *);
    205 int	be_intphy_service(struct be_softc *, struct mii_data *, int);
    206 
    207 
    208 CFATTACH_DECL(be, sizeof(struct be_softc),
    209     bematch, beattach, NULL, NULL);
    210 
    211 int
    212 bematch(struct device *parent, struct cfdata *cf, void *aux)
    213 {
    214 	struct sbus_attach_args *sa = aux;
    215 
    216 	return (strcmp(cf->cf_name, sa->sa_name) == 0);
    217 }
    218 
    219 void
    220 beattach(parent, self, aux)
    221 	struct device *parent, *self;
    222 	void *aux;
    223 {
    224 	struct sbus_attach_args *sa = aux;
    225 	struct qec_softc *qec = (struct qec_softc *)parent;
    226 	struct be_softc *sc = (struct be_softc *)self;
    227 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    228 	struct mii_data *mii = &sc->sc_mii;
    229 	struct mii_softc *child;
    230 	int node = sa->sa_node;
    231 	bus_dma_tag_t dmatag = sa->sa_dmatag;
    232 	bus_dma_segment_t seg;
    233 	bus_size_t size;
    234 	int instance;
    235 	int rseg, error;
    236 	u_int32_t v;
    237 
    238 	if (sa->sa_nreg < 3) {
    239 		printf("%s: only %d register sets\n",
    240 			device_xname(self), sa->sa_nreg);
    241 		return;
    242 	}
    243 
    244 	if (bus_space_map(sa->sa_bustag,
    245 			  (bus_addr_t)BUS_ADDR(
    246 				sa->sa_reg[0].oa_space,
    247 				sa->sa_reg[0].oa_base),
    248 			  (bus_size_t)sa->sa_reg[0].oa_size,
    249 			  0, &sc->sc_cr) != 0) {
    250 		printf("beattach: cannot map registers\n");
    251 		return;
    252 	}
    253 
    254 	if (bus_space_map(sa->sa_bustag,
    255 			  (bus_addr_t)BUS_ADDR(
    256 				sa->sa_reg[1].oa_space,
    257 				sa->sa_reg[1].oa_base),
    258 			  (bus_size_t)sa->sa_reg[1].oa_size,
    259 			  0, &sc->sc_br) != 0) {
    260 		printf("beattach: cannot map registers\n");
    261 		return;
    262 	}
    263 
    264 	if (bus_space_map(sa->sa_bustag,
    265 			  (bus_addr_t)BUS_ADDR(
    266 				sa->sa_reg[2].oa_space,
    267 				sa->sa_reg[2].oa_base),
    268 			  (bus_size_t)sa->sa_reg[2].oa_size,
    269 			  0, &sc->sc_tr) != 0) {
    270 		printf("beattach: cannot map registers\n");
    271 		return;
    272 	}
    273 
    274 	sc->sc_bustag = sa->sa_bustag;
    275 	sc->sc_qec = qec;
    276 	sc->sc_qr = qec->sc_regs;
    277 
    278 	sc->sc_rev = prom_getpropint(node, "board-version", -1);
    279 	printf(" rev %x", sc->sc_rev);
    280 
    281 	callout_init(&sc->sc_tick_ch, 0);
    282 
    283 	bestop(sc);
    284 
    285 	sc->sc_channel = prom_getpropint(node, "channel#", -1);
    286 	if (sc->sc_channel == -1)
    287 		sc->sc_channel = 0;
    288 
    289 	sc->sc_burst = prom_getpropint(node, "burst-sizes", -1);
    290 	if (sc->sc_burst == -1)
    291 		sc->sc_burst = qec->sc_burst;
    292 
    293 	/* Clamp at parent's burst sizes */
    294 	sc->sc_burst &= qec->sc_burst;
    295 
    296 	/* Establish interrupt handler */
    297 	if (sa->sa_nintr)
    298 		(void)bus_intr_establish(sa->sa_bustag, sa->sa_pri, IPL_NET,
    299 					 beintr, sc);
    300 
    301 	prom_getether(node, sc->sc_enaddr);
    302 	printf(" address %s\n", ether_sprintf(sc->sc_enaddr));
    303 
    304 	/*
    305 	 * Allocate descriptor ring and buffers.
    306 	 */
    307 
    308 	/* for now, allocate as many bufs as there are ring descriptors */
    309 	sc->sc_rb.rb_ntbuf = QEC_XD_RING_MAXSIZE;
    310 	sc->sc_rb.rb_nrbuf = QEC_XD_RING_MAXSIZE;
    311 
    312 	size =	QEC_XD_RING_MAXSIZE * sizeof(struct qec_xd) +
    313 		QEC_XD_RING_MAXSIZE * sizeof(struct qec_xd) +
    314 		sc->sc_rb.rb_ntbuf * BE_PKT_BUF_SZ +
    315 		sc->sc_rb.rb_nrbuf * BE_PKT_BUF_SZ;
    316 
    317 	/* Get a DMA handle */
    318 	if ((error = bus_dmamap_create(dmatag, size, 1, size, 0,
    319 				    BUS_DMA_NOWAIT, &sc->sc_dmamap)) != 0) {
    320 		aprint_error_dev(self, "DMA map create error %d\n", error);
    321 		return;
    322 	}
    323 
    324 	/* Allocate DMA buffer */
    325 	if ((error = bus_dmamem_alloc(sa->sa_dmatag, size, 0, 0,
    326 				      &seg, 1, &rseg, BUS_DMA_NOWAIT)) != 0) {
    327 		aprint_error_dev(self, "DMA buffer alloc error %d\n",
    328 			error);
    329 		return;
    330 	}
    331 
    332 	/* Map DMA memory in CPU addressable space */
    333 	if ((error = bus_dmamem_map(sa->sa_dmatag, &seg, rseg, size,
    334 			            &sc->sc_rb.rb_membase,
    335 			            BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) != 0) {
    336 		aprint_error_dev(self, "DMA buffer map error %d\n",
    337 			error);
    338 		bus_dmamem_free(sa->sa_dmatag, &seg, rseg);
    339 		return;
    340 	}
    341 
    342 	/* Load the buffer */
    343 	if ((error = bus_dmamap_load(dmatag, sc->sc_dmamap,
    344 				     sc->sc_rb.rb_membase, size, NULL,
    345 				     BUS_DMA_NOWAIT)) != 0) {
    346 		aprint_error_dev(self, "DMA buffer map load error %d\n",
    347 			error);
    348 		bus_dmamem_unmap(dmatag, sc->sc_rb.rb_membase, size);
    349 		bus_dmamem_free(dmatag, &seg, rseg);
    350 		return;
    351 	}
    352 	sc->sc_rb.rb_dmabase = sc->sc_dmamap->dm_segs[0].ds_addr;
    353 
    354 	/*
    355 	 * Initialize our media structures and MII info.
    356 	 */
    357 	mii->mii_ifp = ifp;
    358 	mii->mii_readreg = be_mii_readreg;
    359 	mii->mii_writereg = be_mii_writereg;
    360 	mii->mii_statchg = be_mii_statchg;
    361 
    362 	ifmedia_init(&mii->mii_media, 0, be_ifmedia_upd, be_ifmedia_sts);
    363 
    364 	/*
    365 	 * Initialize transceiver and determine which PHY connection to use.
    366 	 */
    367 	be_mii_sync(sc);
    368 	v = bus_space_read_4(sc->sc_bustag, sc->sc_tr, BE_TRI_MGMTPAL);
    369 
    370 	instance = 0;
    371 
    372 	if ((v & MGMT_PAL_EXT_MDIO) != 0) {
    373 
    374 		mii_attach(&sc->sc_dev, mii, 0xffffffff, BE_PHY_EXTERNAL,
    375 		    MII_OFFSET_ANY, 0);
    376 
    377 		child = LIST_FIRST(&mii->mii_phys);
    378 		if (child == NULL) {
    379 			/* No PHY attached */
    380 			ifmedia_add(&sc->sc_media,
    381 				    IFM_MAKEWORD(IFM_ETHER,IFM_NONE,0,instance),
    382 				    0, NULL);
    383 			ifmedia_set(&sc->sc_media,
    384 				   IFM_MAKEWORD(IFM_ETHER,IFM_NONE,0,instance));
    385 		} else {
    386 			/*
    387 			 * Note: we support just one PHY on the external
    388 			 * MII connector.
    389 			 */
    390 #ifdef DIAGNOSTIC
    391 			if (LIST_NEXT(child, mii_list) != NULL) {
    392 				aprint_error_dev(&sc->sc_dev, "spurious MII device %s attached\n",
    393 				       device_xname(child->mii_dev));
    394 			}
    395 #endif
    396 			if (child->mii_phy != BE_PHY_EXTERNAL ||
    397 			    child->mii_inst > 0) {
    398 				aprint_error_dev(&sc->sc_dev, "cannot accommodate MII device %s"
    399 				       " at phy %d, instance %d\n",
    400 				       device_xname(child->mii_dev),
    401 				       child->mii_phy, child->mii_inst);
    402 			} else {
    403 				sc->sc_phys[instance] = child->mii_phy;
    404 			}
    405 
    406 			/*
    407 			 * XXX - we can really do the following ONLY if the
    408 			 * phy indeed has the auto negotiation capability!!
    409 			 */
    410 			ifmedia_set(&sc->sc_media,
    411 				   IFM_MAKEWORD(IFM_ETHER,IFM_AUTO,0,instance));
    412 
    413 			/* Mark our current media setting */
    414 			be_pal_gate(sc, BE_PHY_EXTERNAL);
    415 			instance++;
    416 		}
    417 
    418 	}
    419 
    420 	if ((v & MGMT_PAL_INT_MDIO) != 0) {
    421 		/*
    422 		 * The be internal phy looks vaguely like MII hardware,
    423 		 * but not enough to be able to use the MII device
    424 		 * layer. Hence, we have to take care of media selection
    425 		 * ourselves.
    426 		 */
    427 
    428 		sc->sc_mii_inst = instance;
    429 		sc->sc_phys[instance] = BE_PHY_INTERNAL;
    430 
    431 		/* Use `ifm_data' to store BMCR bits */
    432 		ifmedia_add(&sc->sc_media,
    433 			    IFM_MAKEWORD(IFM_ETHER,IFM_10_T,0,instance),
    434 			    0, NULL);
    435 		ifmedia_add(&sc->sc_media,
    436 			    IFM_MAKEWORD(IFM_ETHER,IFM_100_TX,0,instance),
    437 			    BMCR_S100, NULL);
    438 		ifmedia_add(&sc->sc_media,
    439 			    IFM_MAKEWORD(IFM_ETHER,IFM_AUTO,0,instance),
    440 			    0, NULL);
    441 
    442 		printf("on-board transceiver at %s: 10baseT, 100baseTX, auto\n",
    443 			device_xname(self));
    444 
    445 		be_mii_reset(sc, BE_PHY_INTERNAL);
    446 		/* Only set default medium here if there's no external PHY */
    447 		if (instance == 0) {
    448 			be_pal_gate(sc, BE_PHY_INTERNAL);
    449 			ifmedia_set(&sc->sc_media,
    450 				   IFM_MAKEWORD(IFM_ETHER,IFM_AUTO,0,instance));
    451 		} else
    452 			be_mii_writereg((void *)sc,
    453 				BE_PHY_INTERNAL, MII_BMCR, BMCR_ISO);
    454 	}
    455 
    456 	memcpy(ifp->if_xname, device_xname(&sc->sc_dev), IFNAMSIZ);
    457 	ifp->if_softc = sc;
    458 	ifp->if_start = bestart;
    459 	ifp->if_ioctl = beioctl;
    460 	ifp->if_watchdog = bewatchdog;
    461 	ifp->if_flags =
    462 		IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS | IFF_MULTICAST;
    463 	IFQ_SET_READY(&ifp->if_snd);
    464 
    465 	/* claim 802.1q capability */
    466 	sc->sc_ethercom.ec_capabilities |= ETHERCAP_VLAN_MTU;
    467 
    468 	/* Attach the interface. */
    469 	if_attach(ifp);
    470 	ether_ifattach(ifp, sc->sc_enaddr);
    471 }
    472 
    473 
    474 /*
    475  * Routine to copy from mbuf chain to transmit buffer in
    476  * network buffer memory.
    477  */
    478 static inline int
    479 be_put(struct be_softc *sc, int idx, struct mbuf *m)
    480 {
    481 	struct mbuf *n;
    482 	int len, tlen = 0, boff = 0;
    483 	void *bp;
    484 
    485 	bp = (char *)sc->sc_rb.rb_txbuf + (idx % sc->sc_rb.rb_ntbuf) * BE_PKT_BUF_SZ;
    486 
    487 	for (; m; m = n) {
    488 		len = m->m_len;
    489 		if (len == 0) {
    490 			MFREE(m, n);
    491 			continue;
    492 		}
    493 		memcpy((char *)bp + boff, mtod(m, void *), len);
    494 		boff += len;
    495 		tlen += len;
    496 		MFREE(m, n);
    497 	}
    498 	return (tlen);
    499 }
    500 
    501 /*
    502  * Pull data off an interface.
    503  * Len is the length of data, with local net header stripped.
    504  * We copy the data into mbufs.  When full cluster sized units are present,
    505  * we copy into clusters.
    506  */
    507 static inline struct mbuf *
    508 be_get(sc, idx, totlen)
    509 	struct be_softc *sc;
    510 	int idx, totlen;
    511 {
    512 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    513 	struct mbuf *m;
    514 	struct mbuf *top, **mp;
    515 	int len, pad, boff = 0;
    516 	void *bp;
    517 
    518 	bp = (char *)sc->sc_rb.rb_rxbuf + (idx % sc->sc_rb.rb_nrbuf) * BE_PKT_BUF_SZ;
    519 
    520 	MGETHDR(m, M_DONTWAIT, MT_DATA);
    521 	if (m == NULL)
    522 		return (NULL);
    523 	m->m_pkthdr.rcvif = ifp;
    524 	m->m_pkthdr.len = totlen;
    525 
    526 	pad = ALIGN(sizeof(struct ether_header)) - sizeof(struct ether_header);
    527 	m->m_data += pad;
    528 	len = MHLEN - pad;
    529 	top = NULL;
    530 	mp = &top;
    531 
    532 	while (totlen > 0) {
    533 		if (top) {
    534 			MGET(m, M_DONTWAIT, MT_DATA);
    535 			if (m == NULL) {
    536 				m_freem(top);
    537 				return (NULL);
    538 			}
    539 			len = MLEN;
    540 		}
    541 		if (top && totlen >= MINCLSIZE) {
    542 			MCLGET(m, M_DONTWAIT);
    543 			if (m->m_flags & M_EXT)
    544 				len = MCLBYTES;
    545 		}
    546 		m->m_len = len = min(totlen, len);
    547 		memcpy(mtod(m, void *), (char *)bp + boff, len);
    548 		boff += len;
    549 		totlen -= len;
    550 		*mp = m;
    551 		mp = &m->m_next;
    552 	}
    553 
    554 	return (top);
    555 }
    556 
    557 /*
    558  * Pass a packet to the higher levels.
    559  */
    560 static inline void
    561 be_read(sc, idx, len)
    562 	struct be_softc *sc;
    563 	int idx, len;
    564 {
    565 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    566 	struct mbuf *m;
    567 
    568 	if (len <= sizeof(struct ether_header) ||
    569 	    len > ETHER_MAX_LEN + ETHER_VLAN_ENCAP_LEN) {
    570 #ifdef BEDEBUG
    571 		if (sc->sc_debug)
    572 			printf("%s: invalid packet size %d; dropping\n",
    573 				ifp->if_xname, len);
    574 #endif
    575 		ifp->if_ierrors++;
    576 		return;
    577 	}
    578 
    579 	/*
    580 	 * Pull packet off interface.
    581 	 */
    582 	m = be_get(sc, idx, len);
    583 	if (m == NULL) {
    584 		ifp->if_ierrors++;
    585 		return;
    586 	}
    587 	ifp->if_ipackets++;
    588 
    589 #if NBPFILTER > 0
    590 	/*
    591 	 * Check if there's a BPF listener on this interface.
    592 	 * If so, hand off the raw packet to BPF.
    593 	 */
    594 	if (ifp->if_bpf)
    595 		bpf_mtap(ifp->if_bpf, m);
    596 #endif
    597 	/* Pass the packet up. */
    598 	(*ifp->if_input)(ifp, m);
    599 }
    600 
    601 /*
    602  * Start output on interface.
    603  * We make two assumptions here:
    604  *  1) that the current priority is set to splnet _before_ this code
    605  *     is called *and* is returned to the appropriate priority after
    606  *     return
    607  *  2) that the IFF_OACTIVE flag is checked before this code is called
    608  *     (i.e. that the output part of the interface is idle)
    609  */
    610 void
    611 bestart(struct ifnet *ifp)
    612 {
    613 	struct be_softc *sc = (struct be_softc *)ifp->if_softc;
    614 	struct qec_xd *txd = sc->sc_rb.rb_txd;
    615 	struct mbuf *m;
    616 	unsigned int bix, len;
    617 	unsigned int ntbuf = sc->sc_rb.rb_ntbuf;
    618 
    619 	if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
    620 		return;
    621 
    622 	bix = sc->sc_rb.rb_tdhead;
    623 
    624 	for (;;) {
    625 		IFQ_DEQUEUE(&ifp->if_snd, m);
    626 		if (m == 0)
    627 			break;
    628 
    629 #if NBPFILTER > 0
    630 		/*
    631 		 * If BPF is listening on this interface, let it see the
    632 		 * packet before we commit it to the wire.
    633 		 */
    634 		if (ifp->if_bpf)
    635 			bpf_mtap(ifp->if_bpf, m);
    636 #endif
    637 
    638 		/*
    639 		 * Copy the mbuf chain into the transmit buffer.
    640 		 */
    641 		len = be_put(sc, bix, m);
    642 
    643 		/*
    644 		 * Initialize transmit registers and start transmission
    645 		 */
    646 		txd[bix].xd_flags = QEC_XD_OWN | QEC_XD_SOP | QEC_XD_EOP |
    647 				    (len & QEC_XD_LENGTH);
    648 		bus_space_write_4(sc->sc_bustag, sc->sc_cr, BE_CRI_CTRL,
    649 				  BE_CR_CTRL_TWAKEUP);
    650 
    651 		if (++bix == QEC_XD_RING_MAXSIZE)
    652 			bix = 0;
    653 
    654 		if (++sc->sc_rb.rb_td_nbusy == ntbuf) {
    655 			ifp->if_flags |= IFF_OACTIVE;
    656 			break;
    657 		}
    658 	}
    659 
    660 	sc->sc_rb.rb_tdhead = bix;
    661 }
    662 
    663 void
    664 bestop(struct be_softc *sc)
    665 {
    666 	int n;
    667 	bus_space_tag_t t = sc->sc_bustag;
    668 	bus_space_handle_t br = sc->sc_br;
    669 
    670 	callout_stop(&sc->sc_tick_ch);
    671 
    672 	/* Down the MII. */
    673 	mii_down(&sc->sc_mii);
    674 	(void)be_intphy_service(sc, &sc->sc_mii, MII_DOWN);
    675 
    676 	/* Stop the transmitter */
    677 	bus_space_write_4(t, br, BE_BRI_TXCFG, 0);
    678 	for (n = 32; n > 0; n--) {
    679 		if (bus_space_read_4(t, br, BE_BRI_TXCFG) == 0)
    680 			break;
    681 		DELAY(20);
    682 	}
    683 
    684 	/* Stop the receiver */
    685 	bus_space_write_4(t, br, BE_BRI_RXCFG, 0);
    686 	for (n = 32; n > 0; n--) {
    687 		if (bus_space_read_4(t, br, BE_BRI_RXCFG) == 0)
    688 			break;
    689 		DELAY(20);
    690 	}
    691 }
    692 
    693 /*
    694  * Reset interface.
    695  */
    696 void
    697 bereset(struct be_softc *sc)
    698 {
    699 	int s;
    700 
    701 	s = splnet();
    702 	bestop(sc);
    703 	if ((sc->sc_ethercom.ec_if.if_flags & IFF_UP) != 0)
    704 		beinit(sc);
    705 	splx(s);
    706 }
    707 
    708 void
    709 bewatchdog(struct ifnet *ifp)
    710 {
    711 	struct be_softc *sc = ifp->if_softc;
    712 
    713 	log(LOG_ERR, "%s: device timeout\n", device_xname(&sc->sc_dev));
    714 	++sc->sc_ethercom.ec_if.if_oerrors;
    715 
    716 	bereset(sc);
    717 }
    718 
    719 int
    720 beintr(void *v)
    721 {
    722 	struct be_softc *sc = (struct be_softc *)v;
    723 	bus_space_tag_t t = sc->sc_bustag;
    724 	u_int32_t whyq, whyb, whyc;
    725 	int r = 0;
    726 
    727 	/* Read QEC status, channel status and BE status */
    728 	whyq = bus_space_read_4(t, sc->sc_qr, QEC_QRI_STAT);
    729 	whyc = bus_space_read_4(t, sc->sc_cr, BE_CRI_STAT);
    730 	whyb = bus_space_read_4(t, sc->sc_br, BE_BRI_STAT);
    731 
    732 	if (whyq & QEC_STAT_BM)
    733 		r |= beeint(sc, whyb);
    734 
    735 	if (whyq & QEC_STAT_ER)
    736 		r |= beqint(sc, whyc);
    737 
    738 	if (whyq & QEC_STAT_TX && whyc & BE_CR_STAT_TXIRQ)
    739 		r |= betint(sc);
    740 
    741 	if (whyq & QEC_STAT_RX && whyc & BE_CR_STAT_RXIRQ)
    742 		r |= berint(sc);
    743 
    744 	return (r);
    745 }
    746 
    747 /*
    748  * QEC Interrupt.
    749  */
    750 int
    751 beqint(struct be_softc *sc, u_int32_t why)
    752 {
    753 	int r = 0, rst = 0;
    754 
    755 	if (why & BE_CR_STAT_TXIRQ)
    756 		r |= 1;
    757 	if (why & BE_CR_STAT_RXIRQ)
    758 		r |= 1;
    759 
    760 	if (why & BE_CR_STAT_BERROR) {
    761 		r |= 1;
    762 		rst = 1;
    763 		aprint_error_dev(&sc->sc_dev, "bigmac error\n");
    764 	}
    765 
    766 	if (why & BE_CR_STAT_TXDERR) {
    767 		r |= 1;
    768 		rst = 1;
    769 		aprint_error_dev(&sc->sc_dev, "bogus tx descriptor\n");
    770 	}
    771 
    772 	if (why & (BE_CR_STAT_TXLERR | BE_CR_STAT_TXPERR | BE_CR_STAT_TXSERR)) {
    773 		r |= 1;
    774 		rst = 1;
    775 		aprint_error_dev(&sc->sc_dev, "tx DMA error ( ");
    776 		if (why & BE_CR_STAT_TXLERR)
    777 			printf("Late ");
    778 		if (why & BE_CR_STAT_TXPERR)
    779 			printf("Parity ");
    780 		if (why & BE_CR_STAT_TXSERR)
    781 			printf("Generic ");
    782 		printf(")\n");
    783 	}
    784 
    785 	if (why & BE_CR_STAT_RXDROP) {
    786 		r |= 1;
    787 		rst = 1;
    788 		aprint_error_dev(&sc->sc_dev, "out of rx descriptors\n");
    789 	}
    790 
    791 	if (why & BE_CR_STAT_RXSMALL) {
    792 		r |= 1;
    793 		rst = 1;
    794 		aprint_error_dev(&sc->sc_dev, "rx descriptor too small\n");
    795 	}
    796 
    797 	if (why & (BE_CR_STAT_RXLERR | BE_CR_STAT_RXPERR | BE_CR_STAT_RXSERR)) {
    798 		r |= 1;
    799 		rst = 1;
    800 		aprint_error_dev(&sc->sc_dev, "rx DMA error ( ");
    801 		if (why & BE_CR_STAT_RXLERR)
    802 			printf("Late ");
    803 		if (why & BE_CR_STAT_RXPERR)
    804 			printf("Parity ");
    805 		if (why & BE_CR_STAT_RXSERR)
    806 			printf("Generic ");
    807 		printf(")\n");
    808 	}
    809 
    810 	if (!r) {
    811 		rst = 1;
    812 		aprint_error_dev(&sc->sc_dev, "unexpected error interrupt %08x\n",
    813 			why);
    814 	}
    815 
    816 	if (rst) {
    817 		printf("%s: resetting\n", device_xname(&sc->sc_dev));
    818 		bereset(sc);
    819 	}
    820 
    821 	return (r);
    822 }
    823 
    824 /*
    825  * Error interrupt.
    826  */
    827 int
    828 beeint(struct be_softc *sc, u_int32_t why)
    829 {
    830 	int r = 0, rst = 0;
    831 
    832 	if (why & BE_BR_STAT_RFIFOVF) {
    833 		r |= 1;
    834 		rst = 1;
    835 		aprint_error_dev(&sc->sc_dev, "receive fifo overrun\n");
    836 	}
    837 	if (why & BE_BR_STAT_TFIFO_UND) {
    838 		r |= 1;
    839 		rst = 1;
    840 		aprint_error_dev(&sc->sc_dev, "transmit fifo underrun\n");
    841 	}
    842 	if (why & BE_BR_STAT_MAXPKTERR) {
    843 		r |= 1;
    844 		rst = 1;
    845 		aprint_error_dev(&sc->sc_dev, "max packet size error\n");
    846 	}
    847 
    848 	if (!r) {
    849 		rst = 1;
    850 		aprint_error_dev(&sc->sc_dev, "unexpected error interrupt %08x\n",
    851 			why);
    852 	}
    853 
    854 	if (rst) {
    855 		printf("%s: resetting\n", device_xname(&sc->sc_dev));
    856 		bereset(sc);
    857 	}
    858 
    859 	return (r);
    860 }
    861 
    862 /*
    863  * Transmit interrupt.
    864  */
    865 int
    866 betint(struct be_softc *sc)
    867 {
    868 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    869 	bus_space_tag_t t = sc->sc_bustag;
    870 	bus_space_handle_t br = sc->sc_br;
    871 	unsigned int bix, txflags;
    872 
    873 	/*
    874 	 * Unload collision counters
    875 	 */
    876 	ifp->if_collisions +=
    877 		bus_space_read_4(t, br, BE_BRI_NCCNT) +
    878 		bus_space_read_4(t, br, BE_BRI_FCCNT) +
    879 		bus_space_read_4(t, br, BE_BRI_EXCNT) +
    880 		bus_space_read_4(t, br, BE_BRI_LTCNT);
    881 
    882 	/*
    883 	 * the clear the hardware counters
    884 	 */
    885 	bus_space_write_4(t, br, BE_BRI_NCCNT, 0);
    886 	bus_space_write_4(t, br, BE_BRI_FCCNT, 0);
    887 	bus_space_write_4(t, br, BE_BRI_EXCNT, 0);
    888 	bus_space_write_4(t, br, BE_BRI_LTCNT, 0);
    889 
    890 	bix = sc->sc_rb.rb_tdtail;
    891 
    892 	for (;;) {
    893 		if (sc->sc_rb.rb_td_nbusy <= 0)
    894 			break;
    895 
    896 		txflags = sc->sc_rb.rb_txd[bix].xd_flags;
    897 
    898 		if (txflags & QEC_XD_OWN)
    899 			break;
    900 
    901 		ifp->if_flags &= ~IFF_OACTIVE;
    902 		ifp->if_opackets++;
    903 
    904 		if (++bix == QEC_XD_RING_MAXSIZE)
    905 			bix = 0;
    906 
    907 		--sc->sc_rb.rb_td_nbusy;
    908 	}
    909 
    910 	sc->sc_rb.rb_tdtail = bix;
    911 
    912 	bestart(ifp);
    913 
    914 	if (sc->sc_rb.rb_td_nbusy == 0)
    915 		ifp->if_timer = 0;
    916 
    917 	return (1);
    918 }
    919 
    920 /*
    921  * Receive interrupt.
    922  */
    923 int
    924 berint(struct be_softc *sc)
    925 {
    926 	struct qec_xd *xd = sc->sc_rb.rb_rxd;
    927 	unsigned int bix, len;
    928 	unsigned int nrbuf = sc->sc_rb.rb_nrbuf;
    929 
    930 	bix = sc->sc_rb.rb_rdtail;
    931 
    932 	/*
    933 	 * Process all buffers with valid data.
    934 	 */
    935 	for (;;) {
    936 		len = xd[bix].xd_flags;
    937 		if (len & QEC_XD_OWN)
    938 			break;
    939 
    940 		len &= QEC_XD_LENGTH;
    941 		be_read(sc, bix, len);
    942 
    943 		/* ... */
    944 		xd[(bix+nrbuf) % QEC_XD_RING_MAXSIZE].xd_flags =
    945 			QEC_XD_OWN | (BE_PKT_BUF_SZ & QEC_XD_LENGTH);
    946 
    947 		if (++bix == QEC_XD_RING_MAXSIZE)
    948 			bix = 0;
    949 	}
    950 
    951 	sc->sc_rb.rb_rdtail = bix;
    952 
    953 	return (1);
    954 }
    955 
    956 int
    957 beioctl(struct ifnet *ifp, u_long cmd, void *data)
    958 {
    959 	struct be_softc *sc = ifp->if_softc;
    960 	struct ifaddr *ifa = (struct ifaddr *)data;
    961 	struct ifreq *ifr = (struct ifreq *)data;
    962 	int s, error = 0;
    963 
    964 	s = splnet();
    965 
    966 	switch (cmd) {
    967 	case SIOCINITIFADDR:
    968 		ifp->if_flags |= IFF_UP;
    969 		beinit(sc);
    970 		switch (ifa->ifa_addr->sa_family) {
    971 #ifdef INET
    972 		case AF_INET:
    973 			arp_ifinit(ifp, ifa);
    974 			break;
    975 #endif /* INET */
    976 		default:
    977 			break;
    978 		}
    979 		break;
    980 
    981 	case SIOCSIFFLAGS:
    982 		if ((error = ifioctl_common(ifp, cmd, data)) != 0)
    983 			break;
    984 		/* XXX re-use ether_ioctl() */
    985 		switch (ifp->if_flags & (IFF_UP|IFF_RUNNING)) {
    986 		case IFF_RUNNING:
    987 			/*
    988 			 * If interface is marked down and it is running, then
    989 			 * stop it.
    990 			 */
    991 			bestop(sc);
    992 			ifp->if_flags &= ~IFF_RUNNING;
    993 			break;
    994 		case IFF_UP:
    995 			/*
    996 			 * If interface is marked up and it is stopped, then
    997 			 * start it.
    998 			 */
    999 			beinit(sc);
   1000 			break;
   1001 		default:
   1002 			/*
   1003 			 * Reset the interface to pick up changes in any other
   1004 			 * flags that affect hardware registers.
   1005 			 */
   1006 			bestop(sc);
   1007 			beinit(sc);
   1008 			break;
   1009 		}
   1010 #ifdef BEDEBUG
   1011 		if (ifp->if_flags & IFF_DEBUG)
   1012 			sc->sc_debug = 1;
   1013 		else
   1014 			sc->sc_debug = 0;
   1015 #endif
   1016 		break;
   1017 
   1018 	case SIOCADDMULTI:
   1019 	case SIOCDELMULTI:
   1020 		if ((error = ether_ioctl(ifp, cmd, data)) == ENETRESET) {
   1021 			/*
   1022 			 * Multicast list has changed; set the hardware filter
   1023 			 * accordingly.
   1024 			 */
   1025 			if (ifp->if_flags & IFF_RUNNING)
   1026 				be_mcreset(sc);
   1027 			error = 0;
   1028 		}
   1029 		break;
   1030 	case SIOCGIFMEDIA:
   1031 	case SIOCSIFMEDIA:
   1032 		error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
   1033 		break;
   1034 	default:
   1035 		error = ether_ioctl(ifp, cmd, data);
   1036 		break;
   1037 	}
   1038 	splx(s);
   1039 	return (error);
   1040 }
   1041 
   1042 
   1043 void
   1044 beinit(struct be_softc *sc)
   1045 {
   1046 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1047 	bus_space_tag_t t = sc->sc_bustag;
   1048 	bus_space_handle_t br = sc->sc_br;
   1049 	bus_space_handle_t cr = sc->sc_cr;
   1050 	struct qec_softc *qec = sc->sc_qec;
   1051 	u_int32_t v;
   1052 	u_int32_t qecaddr;
   1053 	u_int8_t *ea;
   1054 	int rc, s;
   1055 
   1056 	s = splnet();
   1057 
   1058 	qec_meminit(&sc->sc_rb, BE_PKT_BUF_SZ);
   1059 
   1060 	bestop(sc);
   1061 
   1062 	ea = sc->sc_enaddr;
   1063 	bus_space_write_4(t, br, BE_BRI_MACADDR0, (ea[0] << 8) | ea[1]);
   1064 	bus_space_write_4(t, br, BE_BRI_MACADDR1, (ea[2] << 8) | ea[3]);
   1065 	bus_space_write_4(t, br, BE_BRI_MACADDR2, (ea[4] << 8) | ea[5]);
   1066 
   1067 	/* Clear hash table */
   1068 	bus_space_write_4(t, br, BE_BRI_HASHTAB0, 0);
   1069 	bus_space_write_4(t, br, BE_BRI_HASHTAB1, 0);
   1070 	bus_space_write_4(t, br, BE_BRI_HASHTAB2, 0);
   1071 	bus_space_write_4(t, br, BE_BRI_HASHTAB3, 0);
   1072 
   1073 	/* Re-initialize RX configuration */
   1074 	v = BE_BR_RXCFG_FIFO;
   1075 	bus_space_write_4(t, br, BE_BRI_RXCFG, v);
   1076 
   1077 	be_mcreset(sc);
   1078 
   1079 	bus_space_write_4(t, br, BE_BRI_RANDSEED, 0xbd);
   1080 
   1081 	bus_space_write_4(t, br, BE_BRI_XIFCFG,
   1082 			  BE_BR_XCFG_ODENABLE | BE_BR_XCFG_RESV);
   1083 
   1084 	bus_space_write_4(t, br, BE_BRI_JSIZE, 4);
   1085 
   1086 	/*
   1087 	 * Turn off counter expiration interrupts as well as
   1088 	 * 'gotframe' and 'sentframe'
   1089 	 */
   1090 	bus_space_write_4(t, br, BE_BRI_IMASK,
   1091 			  BE_BR_IMASK_GOTFRAME	|
   1092 			  BE_BR_IMASK_RCNTEXP	|
   1093 			  BE_BR_IMASK_ACNTEXP	|
   1094 			  BE_BR_IMASK_CCNTEXP	|
   1095 			  BE_BR_IMASK_LCNTEXP	|
   1096 			  BE_BR_IMASK_CVCNTEXP	|
   1097 			  BE_BR_IMASK_SENTFRAME	|
   1098 			  BE_BR_IMASK_NCNTEXP	|
   1099 			  BE_BR_IMASK_ECNTEXP	|
   1100 			  BE_BR_IMASK_LCCNTEXP	|
   1101 			  BE_BR_IMASK_FCNTEXP	|
   1102 			  BE_BR_IMASK_DTIMEXP);
   1103 
   1104 	/* Channel registers: */
   1105 	bus_space_write_4(t, cr, BE_CRI_RXDS, (u_int32_t)sc->sc_rb.rb_rxddma);
   1106 	bus_space_write_4(t, cr, BE_CRI_TXDS, (u_int32_t)sc->sc_rb.rb_txddma);
   1107 
   1108 	qecaddr = sc->sc_channel * qec->sc_msize;
   1109 	bus_space_write_4(t, cr, BE_CRI_RXWBUF, qecaddr);
   1110 	bus_space_write_4(t, cr, BE_CRI_RXRBUF, qecaddr);
   1111 	bus_space_write_4(t, cr, BE_CRI_TXWBUF, qecaddr + qec->sc_rsize);
   1112 	bus_space_write_4(t, cr, BE_CRI_TXRBUF, qecaddr + qec->sc_rsize);
   1113 
   1114 	bus_space_write_4(t, cr, BE_CRI_RIMASK, 0);
   1115 	bus_space_write_4(t, cr, BE_CRI_TIMASK, 0);
   1116 	bus_space_write_4(t, cr, BE_CRI_QMASK, 0);
   1117 	bus_space_write_4(t, cr, BE_CRI_BMASK, 0);
   1118 	bus_space_write_4(t, cr, BE_CRI_CCNT, 0);
   1119 
   1120 	/* Set max packet length */
   1121 	v = ETHER_MAX_LEN;
   1122 	if (sc->sc_ethercom.ec_capenable & ETHERCAP_VLAN_MTU)
   1123 		v += ETHER_VLAN_ENCAP_LEN;
   1124 	bus_space_write_4(t, br, BE_BRI_RXMAX, v);
   1125 	bus_space_write_4(t, br, BE_BRI_TXMAX, v);
   1126 
   1127 	/* Enable transmitter */
   1128 	bus_space_write_4(t, br, BE_BRI_TXCFG,
   1129 			  BE_BR_TXCFG_FIFO | BE_BR_TXCFG_ENABLE);
   1130 
   1131 	/* Enable receiver */
   1132 	v = bus_space_read_4(t, br, BE_BRI_RXCFG);
   1133 	v |= BE_BR_RXCFG_FIFO | BE_BR_RXCFG_ENABLE;
   1134 	bus_space_write_4(t, br, BE_BRI_RXCFG, v);
   1135 
   1136 	if ((rc = be_ifmedia_upd(ifp)) != 0)
   1137 		goto out;
   1138 
   1139 	ifp->if_flags |= IFF_RUNNING;
   1140 	ifp->if_flags &= ~IFF_OACTIVE;
   1141 
   1142 	callout_reset(&sc->sc_tick_ch, hz, be_tick, sc);
   1143 out:
   1144 	splx(s);
   1145 }
   1146 
   1147 void
   1148 be_mcreset(struct be_softc *sc)
   1149 {
   1150 	struct ethercom *ec = &sc->sc_ethercom;
   1151 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1152 	bus_space_tag_t t = sc->sc_bustag;
   1153 	bus_space_handle_t br = sc->sc_br;
   1154 	u_int32_t crc;
   1155 	u_int16_t hash[4];
   1156 	u_int8_t octet;
   1157 	u_int32_t v;
   1158 	int i, j;
   1159 	struct ether_multi *enm;
   1160 	struct ether_multistep step;
   1161 
   1162 	if (ifp->if_flags & IFF_PROMISC) {
   1163 		v = bus_space_read_4(t, br, BE_BRI_RXCFG);
   1164 		v |= BE_BR_RXCFG_PMISC;
   1165 		bus_space_write_4(t, br, BE_BRI_RXCFG, v);
   1166 		return;
   1167 	}
   1168 
   1169 	if (ifp->if_flags & IFF_ALLMULTI) {
   1170 		hash[3] = hash[2] = hash[1] = hash[0] = 0xffff;
   1171 		goto chipit;
   1172 	}
   1173 
   1174 	hash[3] = hash[2] = hash[1] = hash[0] = 0;
   1175 
   1176 	ETHER_FIRST_MULTI(step, ec, enm);
   1177 	while (enm != NULL) {
   1178 		if (memcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
   1179 			/*
   1180 			 * We must listen to a range of multicast
   1181 			 * addresses.  For now, just accept all
   1182 			 * multicasts, rather than trying to set only
   1183 			 * those filter bits needed to match the range.
   1184 			 * (At this time, the only use of address
   1185 			 * ranges is for IP multicast routing, for
   1186 			 * which the range is big enough to require
   1187 			 * all bits set.)
   1188 			 */
   1189 			hash[3] = hash[2] = hash[1] = hash[0] = 0xffff;
   1190 			ifp->if_flags |= IFF_ALLMULTI;
   1191 			goto chipit;
   1192 		}
   1193 
   1194 		crc = 0xffffffff;
   1195 
   1196 		for (i = 0; i < ETHER_ADDR_LEN; i++) {
   1197 			octet = enm->enm_addrlo[i];
   1198 
   1199 			for (j = 0; j < 8; j++) {
   1200 				if ((crc & 1) ^ (octet & 1)) {
   1201 					crc >>= 1;
   1202 					crc ^= MC_POLY_LE;
   1203 				}
   1204 				else
   1205 					crc >>= 1;
   1206 				octet >>= 1;
   1207 			}
   1208 		}
   1209 
   1210 		crc >>= 26;
   1211 		hash[crc >> 4] |= 1 << (crc & 0xf);
   1212 		ETHER_NEXT_MULTI(step, enm);
   1213 	}
   1214 
   1215 	ifp->if_flags &= ~IFF_ALLMULTI;
   1216 
   1217 chipit:
   1218 	/* Enable the hash filter */
   1219 	bus_space_write_4(t, br, BE_BRI_HASHTAB0, hash[0]);
   1220 	bus_space_write_4(t, br, BE_BRI_HASHTAB1, hash[1]);
   1221 	bus_space_write_4(t, br, BE_BRI_HASHTAB2, hash[2]);
   1222 	bus_space_write_4(t, br, BE_BRI_HASHTAB3, hash[3]);
   1223 
   1224 	v = bus_space_read_4(t, br, BE_BRI_RXCFG);
   1225 	v &= ~BE_BR_RXCFG_PMISC;
   1226 	v |= BE_BR_RXCFG_HENABLE;
   1227 	bus_space_write_4(t, br, BE_BRI_RXCFG, v);
   1228 }
   1229 
   1230 /*
   1231  * Set the tcvr to an idle state
   1232  */
   1233 void
   1234 be_mii_sync(struct be_softc *sc)
   1235 {
   1236 	bus_space_tag_t t = sc->sc_bustag;
   1237 	bus_space_handle_t tr = sc->sc_tr;
   1238 	int n = 32;
   1239 
   1240 	while (n--) {
   1241 		bus_space_write_4(t, tr, BE_TRI_MGMTPAL,
   1242 				  MGMT_PAL_INT_MDIO | MGMT_PAL_EXT_MDIO |
   1243 				  MGMT_PAL_OENAB);
   1244 		(void)bus_space_read_4(t, tr, BE_TRI_MGMTPAL);
   1245 		bus_space_write_4(t, tr, BE_TRI_MGMTPAL,
   1246 				  MGMT_PAL_INT_MDIO | MGMT_PAL_EXT_MDIO |
   1247 				  MGMT_PAL_OENAB | MGMT_PAL_DCLOCK);
   1248 		(void)bus_space_read_4(t, tr, BE_TRI_MGMTPAL);
   1249 	}
   1250 }
   1251 
   1252 void
   1253 be_pal_gate(struct be_softc *sc, int phy)
   1254 {
   1255 	bus_space_tag_t t = sc->sc_bustag;
   1256 	bus_space_handle_t tr = sc->sc_tr;
   1257 	u_int32_t v;
   1258 
   1259 	be_mii_sync(sc);
   1260 
   1261 	v = ~(TCVR_PAL_EXTLBACK | TCVR_PAL_MSENSE | TCVR_PAL_LTENABLE);
   1262 	if (phy == BE_PHY_INTERNAL)
   1263 		v &= ~TCVR_PAL_SERIAL;
   1264 
   1265 	bus_space_write_4(t, tr, BE_TRI_TCVRPAL, v);
   1266 	(void)bus_space_read_4(t, tr, BE_TRI_TCVRPAL);
   1267 }
   1268 
   1269 static int
   1270 be_tcvr_read_bit(struct be_softc *sc, int phy)
   1271 {
   1272 	bus_space_tag_t t = sc->sc_bustag;
   1273 	bus_space_handle_t tr = sc->sc_tr;
   1274 	int ret;
   1275 
   1276 	if (phy == BE_PHY_INTERNAL) {
   1277 		bus_space_write_4(t, tr, BE_TRI_MGMTPAL, MGMT_PAL_EXT_MDIO);
   1278 		(void)bus_space_read_4(t, tr, BE_TRI_MGMTPAL);
   1279 		bus_space_write_4(t, tr, BE_TRI_MGMTPAL,
   1280 				  MGMT_PAL_EXT_MDIO | MGMT_PAL_DCLOCK);
   1281 		(void)bus_space_read_4(t, tr, BE_TRI_MGMTPAL);
   1282 		ret = (bus_space_read_4(t, tr, BE_TRI_MGMTPAL) &
   1283 			MGMT_PAL_INT_MDIO) >> MGMT_PAL_INT_MDIO_SHIFT;
   1284 	} else {
   1285 		bus_space_write_4(t, tr, BE_TRI_MGMTPAL, MGMT_PAL_INT_MDIO);
   1286 		(void)bus_space_read_4(t, tr, BE_TRI_MGMTPAL);
   1287 		ret = (bus_space_read_4(t, tr, BE_TRI_MGMTPAL) &
   1288 			MGMT_PAL_EXT_MDIO) >> MGMT_PAL_EXT_MDIO_SHIFT;
   1289 		bus_space_write_4(t, tr, BE_TRI_MGMTPAL,
   1290 				  MGMT_PAL_INT_MDIO | MGMT_PAL_DCLOCK);
   1291 		(void)bus_space_read_4(t, tr, BE_TRI_MGMTPAL);
   1292 	}
   1293 
   1294 	return (ret);
   1295 }
   1296 
   1297 static void
   1298 be_tcvr_write_bit(struct be_softc *sc, int phy, int bit)
   1299 {
   1300 	bus_space_tag_t t = sc->sc_bustag;
   1301 	bus_space_handle_t tr = sc->sc_tr;
   1302 	u_int32_t v;
   1303 
   1304 	if (phy == BE_PHY_INTERNAL) {
   1305 		v = ((bit & 1) << MGMT_PAL_INT_MDIO_SHIFT) |
   1306 			MGMT_PAL_OENAB | MGMT_PAL_EXT_MDIO;
   1307 	} else {
   1308 		v = ((bit & 1) << MGMT_PAL_EXT_MDIO_SHIFT)
   1309 			| MGMT_PAL_OENAB | MGMT_PAL_INT_MDIO;
   1310 	}
   1311 	bus_space_write_4(t, tr, BE_TRI_MGMTPAL, v);
   1312 	(void)bus_space_read_4(t, tr, BE_TRI_MGMTPAL);
   1313 	bus_space_write_4(t, tr, BE_TRI_MGMTPAL, v | MGMT_PAL_DCLOCK);
   1314 	(void)bus_space_read_4(t, tr, BE_TRI_MGMTPAL);
   1315 }
   1316 
   1317 static void
   1318 be_mii_sendbits(struct be_softc *sc, int phy, u_int32_t data, int nbits)
   1319 {
   1320 	int i;
   1321 
   1322 	for (i = 1 << (nbits - 1); i != 0; i >>= 1) {
   1323 		be_tcvr_write_bit(sc, phy, (data & i) != 0);
   1324 	}
   1325 }
   1326 
   1327 static int
   1328 be_mii_readreg(self, phy, reg)
   1329 	struct device *self;
   1330 	int phy, reg;
   1331 {
   1332 	struct be_softc *sc = (struct be_softc *)self;
   1333 	int val = 0, i;
   1334 
   1335 	/*
   1336 	 * Read the PHY register by manually driving the MII control lines.
   1337 	 */
   1338 	be_mii_sync(sc);
   1339 	be_mii_sendbits(sc, phy, MII_COMMAND_START, 2);
   1340 	be_mii_sendbits(sc, phy, MII_COMMAND_READ, 2);
   1341 	be_mii_sendbits(sc, phy, phy, 5);
   1342 	be_mii_sendbits(sc, phy, reg, 5);
   1343 
   1344 	(void) be_tcvr_read_bit(sc, phy);
   1345 	(void) be_tcvr_read_bit(sc, phy);
   1346 
   1347 	for (i = 15; i >= 0; i--)
   1348 		val |= (be_tcvr_read_bit(sc, phy) << i);
   1349 
   1350 	(void) be_tcvr_read_bit(sc, phy);
   1351 	(void) be_tcvr_read_bit(sc, phy);
   1352 	(void) be_tcvr_read_bit(sc, phy);
   1353 
   1354 	return (val);
   1355 }
   1356 
   1357 void
   1358 be_mii_writereg(self, phy, reg, val)
   1359 	struct device *self;
   1360 	int phy, reg, val;
   1361 {
   1362 	struct be_softc *sc = (struct be_softc *)self;
   1363 	int i;
   1364 
   1365 	/*
   1366 	 * Write the PHY register by manually driving the MII control lines.
   1367 	 */
   1368 	be_mii_sync(sc);
   1369 	be_mii_sendbits(sc, phy, MII_COMMAND_START, 2);
   1370 	be_mii_sendbits(sc, phy, MII_COMMAND_WRITE, 2);
   1371 	be_mii_sendbits(sc, phy, phy, 5);
   1372 	be_mii_sendbits(sc, phy, reg, 5);
   1373 
   1374 	be_tcvr_write_bit(sc, phy, 1);
   1375 	be_tcvr_write_bit(sc, phy, 0);
   1376 
   1377 	for (i = 15; i >= 0; i--)
   1378 		be_tcvr_write_bit(sc, phy, (val >> i) & 1);
   1379 }
   1380 
   1381 int
   1382 be_mii_reset(struct be_softc *sc, int phy)
   1383 {
   1384 	int n;
   1385 
   1386 	be_mii_writereg((struct device *)sc, phy, MII_BMCR,
   1387 			BMCR_LOOP | BMCR_PDOWN | BMCR_ISO);
   1388 	be_mii_writereg((struct device *)sc, phy, MII_BMCR, BMCR_RESET);
   1389 
   1390 	for (n = 16; n >= 0; n--) {
   1391 		int bmcr = be_mii_readreg((struct device *)sc, phy, MII_BMCR);
   1392 		if ((bmcr & BMCR_RESET) == 0)
   1393 			break;
   1394 		DELAY(20);
   1395 	}
   1396 	if (n == 0) {
   1397 		aprint_error_dev(&sc->sc_dev, "bmcr reset failed\n");
   1398 		return (EIO);
   1399 	}
   1400 
   1401 	return (0);
   1402 }
   1403 
   1404 void
   1405 be_tick(void *arg)
   1406 {
   1407 	struct be_softc *sc = arg;
   1408 	int s = splnet();
   1409 
   1410 	mii_tick(&sc->sc_mii);
   1411 	(void)be_intphy_service(sc, &sc->sc_mii, MII_TICK);
   1412 
   1413 	splx(s);
   1414 	callout_reset(&sc->sc_tick_ch, hz, be_tick, sc);
   1415 }
   1416 
   1417 void
   1418 be_mii_statchg(struct device *self)
   1419 {
   1420 	struct be_softc *sc = (struct be_softc *)self;
   1421 	bus_space_tag_t t = sc->sc_bustag;
   1422 	bus_space_handle_t br = sc->sc_br;
   1423 	u_int instance;
   1424 	u_int32_t v;
   1425 
   1426 	instance = IFM_INST(sc->sc_mii.mii_media.ifm_cur->ifm_media);
   1427 #ifdef DIAGNOSTIC
   1428 	if (instance > 1)
   1429 		panic("be_mii_statchg: instance %d out of range", instance);
   1430 #endif
   1431 
   1432 	/* Update duplex mode in TX configuration */
   1433 	v = bus_space_read_4(t, br, BE_BRI_TXCFG);
   1434 	if ((IFM_OPTIONS(sc->sc_mii.mii_media_active) & IFM_FDX) != 0)
   1435 		v |= BE_BR_TXCFG_FULLDPLX;
   1436 	else
   1437 		v &= ~BE_BR_TXCFG_FULLDPLX;
   1438 	bus_space_write_4(t, br, BE_BRI_TXCFG, v);
   1439 
   1440 	/* Change to appropriate gate in transceiver PAL */
   1441 	be_pal_gate(sc, sc->sc_phys[instance]);
   1442 }
   1443 
   1444 /*
   1445  * Get current media settings.
   1446  */
   1447 void
   1448 be_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
   1449 {
   1450 	struct be_softc *sc = ifp->if_softc;
   1451 
   1452 	mii_pollstat(&sc->sc_mii);
   1453 	(void)be_intphy_service(sc, &sc->sc_mii, MII_POLLSTAT);
   1454 
   1455 	ifmr->ifm_status = sc->sc_mii.mii_media_status;
   1456 	ifmr->ifm_active = sc->sc_mii.mii_media_active;
   1457 	return;
   1458 }
   1459 
   1460 /*
   1461  * Set media options.
   1462  */
   1463 int
   1464 be_ifmedia_upd(struct ifnet *ifp)
   1465 {
   1466 	struct be_softc *sc = ifp->if_softc;
   1467 	int error;
   1468 
   1469 	if ((error = mii_mediachg(&sc->sc_mii)) == ENXIO)
   1470 		error = 0;
   1471 	else if (error != 0)
   1472 		return error;
   1473 
   1474 	return (be_intphy_service(sc, &sc->sc_mii, MII_MEDIACHG));
   1475 }
   1476 
   1477 /*
   1478  * Service routine for our pseudo-MII internal transceiver.
   1479  */
   1480 int
   1481 be_intphy_service(struct be_softc *sc, struct mii_data *mii, int cmd)
   1482 {
   1483 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
   1484 	int bmcr, bmsr;
   1485 	int error;
   1486 
   1487 	switch (cmd) {
   1488 	case MII_POLLSTAT:
   1489 		/*
   1490 		 * If we're not polling our PHY instance, just return.
   1491 		 */
   1492 		if (IFM_INST(ife->ifm_media) != sc->sc_mii_inst)
   1493 			return (0);
   1494 
   1495 		break;
   1496 
   1497 	case MII_MEDIACHG:
   1498 
   1499 		/*
   1500 		 * If the media indicates a different PHY instance,
   1501 		 * isolate ourselves.
   1502 		 */
   1503 		if (IFM_INST(ife->ifm_media) != sc->sc_mii_inst) {
   1504 			bmcr = be_mii_readreg((void *)sc,
   1505 				BE_PHY_INTERNAL, MII_BMCR);
   1506 			be_mii_writereg((void *)sc,
   1507 				BE_PHY_INTERNAL, MII_BMCR, bmcr | BMCR_ISO);
   1508 			sc->sc_mii_flags &= ~MIIF_HAVELINK;
   1509 			sc->sc_intphy_curspeed = 0;
   1510 			return (0);
   1511 		}
   1512 
   1513 
   1514 		if ((error = be_mii_reset(sc, BE_PHY_INTERNAL)) != 0)
   1515 			return (error);
   1516 
   1517 		bmcr = be_mii_readreg((void *)sc, BE_PHY_INTERNAL, MII_BMCR);
   1518 
   1519 		/*
   1520 		 * Select the new mode and take out of isolation
   1521 		 */
   1522 		if (IFM_SUBTYPE(ife->ifm_media) == IFM_100_TX)
   1523 			bmcr |= BMCR_S100;
   1524 		else if (IFM_SUBTYPE(ife->ifm_media) == IFM_10_T)
   1525 			bmcr &= ~BMCR_S100;
   1526 		else if (IFM_SUBTYPE(ife->ifm_media) == IFM_AUTO) {
   1527 			if ((sc->sc_mii_flags & MIIF_HAVELINK) != 0) {
   1528 				bmcr &= ~BMCR_S100;
   1529 				bmcr |= sc->sc_intphy_curspeed;
   1530 			} else {
   1531 				/* Keep isolated until link is up */
   1532 				bmcr |= BMCR_ISO;
   1533 				sc->sc_mii_flags |= MIIF_DOINGAUTO;
   1534 			}
   1535 		}
   1536 
   1537 		if ((IFM_OPTIONS(ife->ifm_media) & IFM_FDX) != 0)
   1538 			bmcr |= BMCR_FDX;
   1539 		else
   1540 			bmcr &= ~BMCR_FDX;
   1541 
   1542 		be_mii_writereg((void *)sc, BE_PHY_INTERNAL, MII_BMCR, bmcr);
   1543 		break;
   1544 
   1545 	case MII_TICK:
   1546 		/*
   1547 		 * If we're not currently selected, just return.
   1548 		 */
   1549 		if (IFM_INST(ife->ifm_media) != sc->sc_mii_inst)
   1550 			return (0);
   1551 
   1552 		/* Only used for automatic media selection */
   1553 		if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO)
   1554 			return (0);
   1555 
   1556 		/* Is the interface even up? */
   1557 		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
   1558 			return (0);
   1559 
   1560 		/*
   1561 		 * Check link status; if we don't have a link, try another
   1562 		 * speed. We can't detect duplex mode, so half-duplex is
   1563 		 * what we have to settle for.
   1564 		 */
   1565 
   1566 		/* Read twice in case the register is latched */
   1567 		bmsr = be_mii_readreg((void *)sc, BE_PHY_INTERNAL, MII_BMSR) |
   1568 		       be_mii_readreg((void *)sc, BE_PHY_INTERNAL, MII_BMSR);
   1569 
   1570 		if ((bmsr & BMSR_LINK) != 0) {
   1571 			/* We have a carrier */
   1572 			bmcr = be_mii_readreg((void *)sc,
   1573 					BE_PHY_INTERNAL, MII_BMCR);
   1574 
   1575 			if ((sc->sc_mii_flags & MIIF_DOINGAUTO) != 0) {
   1576 				bmcr = be_mii_readreg((void *)sc,
   1577 						BE_PHY_INTERNAL, MII_BMCR);
   1578 
   1579 				sc->sc_mii_flags |= MIIF_HAVELINK;
   1580 				sc->sc_intphy_curspeed = (bmcr & BMCR_S100);
   1581 				sc->sc_mii_flags &= ~MIIF_DOINGAUTO;
   1582 
   1583 				bmcr &= ~BMCR_ISO;
   1584 				be_mii_writereg((void *)sc,
   1585 					BE_PHY_INTERNAL, MII_BMCR, bmcr);
   1586 
   1587 				printf("%s: link up at %s Mbps\n",
   1588 					device_xname(&sc->sc_dev),
   1589 					(bmcr & BMCR_S100) ? "100" : "10");
   1590 			}
   1591 			return (0);
   1592 		}
   1593 
   1594 		if ((sc->sc_mii_flags & MIIF_DOINGAUTO) == 0) {
   1595 			sc->sc_mii_flags |= MIIF_DOINGAUTO;
   1596 			sc->sc_mii_flags &= ~MIIF_HAVELINK;
   1597 			sc->sc_intphy_curspeed = 0;
   1598 			printf("%s: link down\n", device_xname(&sc->sc_dev));
   1599 		}
   1600 
   1601 		/* Only retry autonegotiation every 5 seconds. */
   1602 		if (++sc->sc_mii_ticks < 5)
   1603 			return(0);
   1604 
   1605 		sc->sc_mii_ticks = 0;
   1606 		bmcr = be_mii_readreg((void *)sc, BE_PHY_INTERNAL, MII_BMCR);
   1607 		/* Just flip the fast speed bit */
   1608 		bmcr ^= BMCR_S100;
   1609 		be_mii_writereg((void *)sc, BE_PHY_INTERNAL, MII_BMCR, bmcr);
   1610 
   1611 		break;
   1612 
   1613 	case MII_DOWN:
   1614 		/* Isolate this phy */
   1615 		bmcr = be_mii_readreg((void *)sc, BE_PHY_INTERNAL, MII_BMCR);
   1616 		be_mii_writereg((void *)sc,
   1617 				BE_PHY_INTERNAL, MII_BMCR, bmcr | BMCR_ISO);
   1618 		return (0);
   1619 	}
   1620 
   1621 	/* Update the media status. */
   1622 	be_intphy_status(sc);
   1623 
   1624 	/* Callback if something changed. */
   1625 	if (sc->sc_mii_active != mii->mii_media_active || cmd == MII_MEDIACHG) {
   1626 		(*mii->mii_statchg)((struct device *)sc);
   1627 		sc->sc_mii_active = mii->mii_media_active;
   1628 	}
   1629 	return (0);
   1630 }
   1631 
   1632 /*
   1633  * Determine status of internal transceiver
   1634  */
   1635 void
   1636 be_intphy_status(struct be_softc *sc)
   1637 {
   1638 	struct mii_data *mii = &sc->sc_mii;
   1639 	int media_active, media_status;
   1640 	int bmcr, bmsr;
   1641 
   1642 	media_status = IFM_AVALID;
   1643 	media_active = 0;
   1644 
   1645 	/*
   1646 	 * Internal transceiver; do the work here.
   1647 	 */
   1648 	bmcr = be_mii_readreg((struct device *)sc, BE_PHY_INTERNAL, MII_BMCR);
   1649 
   1650 	switch (bmcr & (BMCR_S100 | BMCR_FDX)) {
   1651 	case (BMCR_S100 | BMCR_FDX):
   1652 		media_active = IFM_ETHER | IFM_100_TX | IFM_FDX;
   1653 		break;
   1654 	case BMCR_S100:
   1655 		media_active = IFM_ETHER | IFM_100_TX | IFM_HDX;
   1656 		break;
   1657 	case BMCR_FDX:
   1658 		media_active = IFM_ETHER | IFM_10_T | IFM_FDX;
   1659 		break;
   1660 	case 0:
   1661 		media_active = IFM_ETHER | IFM_10_T | IFM_HDX;
   1662 		break;
   1663 	}
   1664 
   1665 	/* Read twice in case the register is latched */
   1666 	bmsr = be_mii_readreg((struct device *)sc, BE_PHY_INTERNAL, MII_BMSR)|
   1667 	       be_mii_readreg((struct device *)sc, BE_PHY_INTERNAL, MII_BMSR);
   1668 	if (bmsr & BMSR_LINK)
   1669 		media_status |=  IFM_ACTIVE;
   1670 
   1671 	mii->mii_media_status = media_status;
   1672 	mii->mii_media_active = media_active;
   1673 }
   1674