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