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