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