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