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