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