Home | History | Annotate | Line # | Download | only in sbus
qe.c revision 1.3
      1 /*	$NetBSD: qe.c,v 1.3 1999/02/03 06:03:37 mrg 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 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 /*
     67  * Driver for the SBus qec+qe QuadEthernet board.
     68  *
     69  * This driver was written using the AMD MACE Am79C940 documentation, some
     70  * ideas gleaned from the S/Linux driver for this card, Solaris header files,
     71  * and a loan of a card from Paul Southworth of the Internet Engineering
     72  * Group (www.ieng.com).
     73  */
     74 
     75 #include "opt_ddb.h"
     76 #include "opt_inet.h"
     77 #include "opt_ccitt.h"
     78 #include "opt_llc.h"
     79 #include "opt_ns.h"
     80 #include "bpfilter.h"
     81 #include "rnd.h"
     82 
     83 #include <sys/param.h>
     84 #include <sys/systm.h>
     85 #include <sys/kernel.h>
     86 #include <sys/errno.h>
     87 #include <sys/ioctl.h>
     88 #include <sys/mbuf.h>
     89 #include <sys/socket.h>
     90 #include <sys/syslog.h>
     91 #include <sys/device.h>
     92 #include <sys/malloc.h>
     93 #if NRND > 0
     94 #include <sys/rnd.h>
     95 #endif
     96 
     97 #include <net/if.h>
     98 #include <net/if_dl.h>
     99 #include <net/if_types.h>
    100 #include <net/netisr.h>
    101 #include <net/if_media.h>
    102 #include <net/if_ether.h>
    103 
    104 #ifdef INET
    105 #include <netinet/in.h>
    106 #include <netinet/if_inarp.h>
    107 #include <netinet/in_systm.h>
    108 #include <netinet/in_var.h>
    109 #include <netinet/ip.h>
    110 #endif
    111 
    112 #ifdef NS
    113 #include <netns/ns.h>
    114 #include <netns/ns_if.h>
    115 #endif
    116 
    117 #if NBPFILTER > 0
    118 #include <net/bpf.h>
    119 #include <net/bpfdesc.h>
    120 #endif
    121 
    122 #include <machine/autoconf.h>
    123 #include <machine/cpu.h>
    124 
    125 #include <dev/sbus/sbusvar.h>
    126 #include <dev/sbus/qecreg.h>
    127 #include <dev/sbus/qecvar.h>
    128 #include <dev/sbus/qereg.h>
    129 
    130 struct qe_softc {
    131 	struct	device	sc_dev;		/* base device */
    132 	struct	sbusdev sc_sd;		/* sbus device */
    133 	bus_space_tag_t	sc_bustag;	/* bus & dma tags */
    134 	bus_dma_tag_t	sc_dmatag;
    135 	struct	ethercom sc_ethercom;
    136 	struct	ifmedia sc_ifmedia;	/* interface media */
    137 
    138 	struct	qec_softc *sc_qec;	/* QEC parent */
    139 
    140 	bus_space_handle_t	sc_qr;	/* QEC registers */
    141 	bus_space_handle_t	sc_mr;	/* MACE registers */
    142 	bus_space_handle_t	sc_cr;	/* channel registers */
    143 
    144 	int	sc_channel;		/* channel number */
    145 	u_int	sc_rev;			/* board revision */
    146 
    147 	int	sc_promisc;
    148 	int	sc_burst;
    149 
    150 	struct  qec_ring	sc_rb;	/* Packet Ring Buffer */
    151 
    152 	/* MAC address */
    153 	u_int8_t sc_enaddr[6];
    154 };
    155 
    156 int	qematch __P((struct device *, struct cfdata *, void *));
    157 void	qeattach __P((struct device *, struct device *, void *));
    158 
    159 void	qeinit __P((struct qe_softc *));
    160 void	qestart __P((struct ifnet *));
    161 void	qestop __P((struct qe_softc *));
    162 void	qewatchdog __P((struct ifnet *));
    163 int	qeioctl __P((struct ifnet *, u_long, caddr_t));
    164 void	qereset __P((struct qe_softc *));
    165 
    166 int	qeintr __P((void *));
    167 int	qe_eint __P((struct qe_softc *, u_int32_t));
    168 int	qe_rint __P((struct qe_softc *));
    169 int	qe_tint __P((struct qe_softc *));
    170 void	qe_mcreset __P((struct qe_softc *));
    171 
    172 static int	qe_put __P((struct qe_softc *, int, struct mbuf *));
    173 static void	qe_read __P((struct qe_softc *, int, int));
    174 static struct mbuf	*qe_get __P((struct qe_softc *, int, int));
    175 
    176 /* ifmedia callbacks */
    177 void	qe_ifmedia_sts __P((struct ifnet *, struct ifmediareq *));
    178 int	qe_ifmedia_upd __P((struct ifnet *));
    179 
    180 struct cfattach qe_ca = {
    181 	sizeof(struct qe_softc), qematch, qeattach
    182 };
    183 
    184 int
    185 qematch(parent, cf, aux)
    186 	struct device *parent;
    187 	struct cfdata *cf;
    188 	void *aux;
    189 {
    190 	struct sbus_attach_args *sa = aux;
    191 
    192 	return (strcmp(cf->cf_driver->cd_name, sa->sa_name) == 0);
    193 }
    194 
    195 void
    196 qeattach(parent, self, aux)
    197 	struct device *parent, *self;
    198 	void *aux;
    199 {
    200 	struct sbus_attach_args *sa = aux;
    201 	struct qec_softc *qec = (struct qec_softc *)parent;
    202 	struct qe_softc *sc = (struct qe_softc *)self;
    203 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    204 	int node = sa->sa_node;
    205 	bus_dma_segment_t seg;
    206 	bus_size_t size;
    207 	int rseg, error;
    208 	extern void myetheraddr __P((u_char *));
    209 
    210 	if (sa->sa_nreg < 2) {
    211 		printf("%s: only %d register sets\n",
    212 			self->dv_xname, sa->sa_nreg);
    213 		return;
    214 	}
    215 
    216 	if (bus_space_map2(sa->sa_bustag,
    217 			  (bus_type_t)sa->sa_reg[0].sbr_slot,
    218 			  (bus_addr_t)sa->sa_reg[0].sbr_offset,
    219 			  (bus_size_t)sa->sa_reg[0].sbr_size,
    220 			  BUS_SPACE_MAP_LINEAR, 0, &sc->sc_cr) != 0) {
    221 		printf("%s: cannot map registers\n", self->dv_xname);
    222 		return;
    223 	}
    224 
    225 	if (bus_space_map2(sa->sa_bustag,
    226 			  (bus_type_t)sa->sa_reg[1].sbr_slot,
    227 			  (bus_addr_t)sa->sa_reg[1].sbr_offset,
    228 			  (bus_size_t)sa->sa_reg[1].sbr_size,
    229 			  BUS_SPACE_MAP_LINEAR, 0, &sc->sc_mr) != 0) {
    230 		printf("%s: cannot map registers\n", self->dv_xname);
    231 		return;
    232 	}
    233 
    234 	sc->sc_rev = getpropint(node, "mace-version", -1);
    235 	printf(" rev %x", sc->sc_rev);
    236 
    237 	sc->sc_qec = qec;
    238 	sc->sc_qr = qec->sc_regs;
    239 
    240 	sc->sc_channel = getpropint(node, "channel#", -1);
    241 	sc->sc_burst = qec->sc_burst;
    242 
    243 	qestop(sc);
    244 
    245 	/* Note: no interrupt level passed */
    246 	(void)bus_intr_establish(sa->sa_bustag, 0, 0, qeintr, sc);
    247 	myetheraddr(sc->sc_enaddr);
    248 
    249 	/*
    250 	 * Allocate descriptor ring and buffers.
    251 	 */
    252 
    253 	/* for now, allocate as many bufs as there are ring descriptors */
    254 	sc->sc_rb.rb_ntbuf = QEC_XD_RING_MAXSIZE;
    255 	sc->sc_rb.rb_nrbuf = QEC_XD_RING_MAXSIZE;
    256 
    257 	size =	QEC_XD_RING_MAXSIZE * sizeof(struct qec_xd) +
    258 		QEC_XD_RING_MAXSIZE * sizeof(struct qec_xd) +
    259 		sc->sc_rb.rb_ntbuf * QE_PKT_BUF_SZ +
    260 		sc->sc_rb.rb_nrbuf * QE_PKT_BUF_SZ;
    261 	if ((error = bus_dmamem_alloc(sa->sa_dmatag, size,
    262 				      NBPG, 0,
    263 				      &seg, 1, &rseg, BUS_DMA_NOWAIT)) != 0) {
    264 		printf("%s: DMA buffer alloc error %d\n",
    265 			self->dv_xname, error);
    266 		return;
    267 	}
    268 	sc->sc_rb.rb_dmabase = seg.ds_addr;
    269 
    270 	if ((error = bus_dmamem_map(sa->sa_dmatag, &seg, rseg, size,
    271 			            &sc->sc_rb.rb_membase,
    272 			            BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) != 0) {
    273 		printf("%s: DMA buffer map error %d\n",
    274 			self->dv_xname, error);
    275 		bus_dmamem_free(sa->sa_dmatag, &seg, rseg);
    276 		return;
    277 	}
    278 
    279 	/* Initialize media properties */
    280 	ifmedia_init(&sc->sc_ifmedia, 0, qe_ifmedia_upd, qe_ifmedia_sts);
    281 	ifmedia_add(&sc->sc_ifmedia,
    282 		    IFM_MAKEWORD(IFM_ETHER,IFM_10_T,0,0),
    283 		    0, NULL);
    284 	ifmedia_add(&sc->sc_ifmedia,
    285 		    IFM_MAKEWORD(IFM_ETHER,IFM_10_5,0,0),
    286 		    0, NULL);
    287 	ifmedia_add(&sc->sc_ifmedia,
    288 		    IFM_MAKEWORD(IFM_ETHER,IFM_AUTO,0,0),
    289 		    0, NULL);
    290 	ifmedia_set(&sc->sc_ifmedia, IFM_ETHER|IFM_AUTO);
    291 
    292 	bcopy(sc->sc_dev.dv_xname, ifp->if_xname, IFNAMSIZ);
    293 	ifp->if_softc = sc;
    294 	ifp->if_start = qestart;
    295 	ifp->if_ioctl = qeioctl;
    296 	ifp->if_watchdog = qewatchdog;
    297 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS |
    298 	    IFF_MULTICAST;
    299 
    300 	/* Attach the interface. */
    301 	if_attach(ifp);
    302 	ether_ifattach(ifp, sc->sc_enaddr);
    303 
    304 	printf(" address %s\n", ether_sprintf(sc->sc_enaddr));
    305 
    306 #if NBPFILTER > 0
    307 	bpfattach(&ifp->if_bpf, ifp, DLT_EN10MB,
    308 	    sizeof(struct ether_header));
    309 #endif
    310 }
    311 
    312 /*
    313  * Pull data off an interface.
    314  * Len is the length of data, with local net header stripped.
    315  * We copy the data into mbufs.  When full cluster sized units are present,
    316  * we copy into clusters.
    317  */
    318 static __inline__ struct mbuf *
    319 qe_get(sc, idx, totlen)
    320 	struct qe_softc *sc;
    321 	int idx, totlen;
    322 {
    323 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    324 	struct mbuf *m;
    325 	struct mbuf *top, **mp;
    326 	int len, pad, boff = 0;
    327 	caddr_t bp;
    328 
    329 	bp = sc->sc_rb.rb_rxbuf + (idx % sc->sc_rb.rb_nrbuf) * QE_PKT_BUF_SZ;
    330 
    331 	MGETHDR(m, M_DONTWAIT, MT_DATA);
    332 	if (m == NULL)
    333 		return (NULL);
    334 	m->m_pkthdr.rcvif = ifp;
    335 	m->m_pkthdr.len = totlen;
    336 	pad = ALIGN(sizeof(struct ether_header)) - sizeof(struct ether_header);
    337 	m->m_data += pad;
    338 	len = MHLEN - pad;
    339 	top = NULL;
    340 	mp = &top;
    341 
    342 	while (totlen > 0) {
    343 		if (top) {
    344 			MGET(m, M_DONTWAIT, MT_DATA);
    345 			if (m == NULL) {
    346 				m_freem(top);
    347 				return (NULL);
    348 			}
    349 			len = MLEN;
    350 		}
    351 		if (top && totlen >= MINCLSIZE) {
    352 			MCLGET(m, M_DONTWAIT);
    353 			if (m->m_flags & M_EXT)
    354 				len = MCLBYTES;
    355 		}
    356 		m->m_len = len = min(totlen, len);
    357 		bcopy(bp + boff, mtod(m, caddr_t), len);
    358 		boff += len;
    359 		totlen -= len;
    360 		*mp = m;
    361 		mp = &m->m_next;
    362 	}
    363 
    364 	return (top);
    365 }
    366 
    367 /*
    368  * Routine to copy from mbuf chain to transmit buffer in
    369  * network buffer memory.
    370  */
    371 __inline__ int
    372 qe_put(sc, idx, m)
    373 	struct qe_softc *sc;
    374 	int idx;
    375 	struct mbuf *m;
    376 {
    377 	struct mbuf *n;
    378 	int len, tlen = 0, boff = 0;
    379 	caddr_t bp;
    380 
    381 	bp = sc->sc_rb.rb_txbuf + (idx % sc->sc_rb.rb_ntbuf) * QE_PKT_BUF_SZ;
    382 
    383 	for (; m; m = n) {
    384 		len = m->m_len;
    385 		if (len == 0) {
    386 			MFREE(m, n);
    387 			continue;
    388 		}
    389 		bcopy(mtod(m, caddr_t), bp+boff, len);
    390 		boff += len;
    391 		tlen += len;
    392 		MFREE(m, n);
    393 	}
    394 	return (tlen);
    395 }
    396 
    397 /*
    398  * Pass a packet to the higher levels.
    399  */
    400 __inline__ void
    401 qe_read(sc, idx, len)
    402 	struct qe_softc *sc;
    403 	int idx, len;
    404 {
    405 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    406 	struct ether_header *eh;
    407 	struct mbuf *m;
    408 
    409 	if (len <= sizeof(struct ether_header) ||
    410 	    len > ETHERMTU + sizeof(struct ether_header)) {
    411 
    412 		printf("%s: invalid packet size %d; dropping\n",
    413 			ifp->if_xname, len);
    414 
    415 		ifp->if_ierrors++;
    416 		return;
    417 	}
    418 
    419 	/*
    420 	 * Pull packet off interface.
    421 	 */
    422 	m = qe_get(sc, idx, len);
    423 	if (m == NULL) {
    424 		ifp->if_ierrors++;
    425 		return;
    426 	}
    427 	ifp->if_ipackets++;
    428 
    429 	/* We assume that the header fit entirely in one mbuf. */
    430 	eh = mtod(m, struct ether_header *);
    431 
    432 #if NBPFILTER > 0
    433 	/*
    434 	 * Check if there's a BPF listener on this interface.
    435 	 * If so, hand off the raw packet to BPF.
    436 	 */
    437 	if (ifp->if_bpf)
    438 		bpf_mtap(ifp->if_bpf, m);
    439 #endif
    440 	/* Pass the packet up, with the ether header sort-of removed. */
    441 	m_adj(m, sizeof(struct ether_header));
    442 	ether_input(ifp, eh, m);
    443 }
    444 
    445 /*
    446  * Start output on interface.
    447  * We make two assumptions here:
    448  *  1) that the current priority is set to splnet _before_ this code
    449  *     is called *and* is returned to the appropriate priority after
    450  *     return
    451  *  2) that the IFF_OACTIVE flag is checked before this code is called
    452  *     (i.e. that the output part of the interface is idle)
    453  */
    454 void
    455 qestart(ifp)
    456 	struct ifnet *ifp;
    457 {
    458 	struct qe_softc *sc = (struct qe_softc *)ifp->if_softc;
    459 	struct qec_xd *txd = sc->sc_rb.rb_txd;
    460 	struct mbuf *m;
    461 	unsigned int bix, len;
    462 	unsigned int ntbuf = sc->sc_rb.rb_ntbuf;
    463 
    464 	if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
    465 		return;
    466 
    467 	bix = sc->sc_rb.rb_tdhead;
    468 
    469 	for (;;) {
    470 		IF_DEQUEUE(&ifp->if_snd, m);
    471 		if (m == 0)
    472 			break;
    473 
    474 #if NBPFILTER > 0
    475 		/*
    476 		 * If BPF is listening on this interface, let it see the
    477 		 * packet before we commit it to the wire.
    478 		 */
    479 		if (ifp->if_bpf)
    480 			bpf_mtap(ifp->if_bpf, m);
    481 #endif
    482 
    483 		/*
    484 		 * Copy the mbuf chain into the transmit buffer.
    485 		 */
    486 		len = qe_put(sc, bix, m);
    487 
    488 		/*
    489 		 * Initialize transmit registers and start transmission
    490 		 */
    491 		txd[bix].xd_flags = QEC_XD_OWN | QEC_XD_SOP | QEC_XD_EOP |
    492 				    (len & QEC_XD_LENGTH);
    493 		bus_space_write_4(sc->sc_bustag, sc->sc_cr, QE_CRI_CTRL,
    494 				  QE_CR_CTRL_TWAKEUP);
    495 
    496 		if (++bix == QEC_XD_RING_MAXSIZE)
    497 			bix = 0;
    498 
    499 		if (++sc->sc_rb.rb_td_nbusy == ntbuf) {
    500 			ifp->if_flags |= IFF_OACTIVE;
    501 			break;
    502 		}
    503 	}
    504 
    505 	sc->sc_rb.rb_tdhead = bix;
    506 }
    507 
    508 void
    509 qestop(sc)
    510 	struct qe_softc *sc;
    511 {
    512 	bus_space_tag_t t = sc->sc_bustag;
    513 	bus_space_handle_t mr = sc->sc_mr;
    514 	bus_space_handle_t cr = sc->sc_cr;
    515 	int n;
    516 
    517 	/* Stop the schwurst */
    518 	bus_space_write_1(t, mr, QE_MRI_BIUCC, QE_MR_BIUCC_SWRST);
    519 	for (n = 200; n > 0; n--) {
    520 		if ((bus_space_read_1(t, mr, QE_MRI_BIUCC) &
    521 			QE_MR_BIUCC_SWRST) == 0)
    522 			break;
    523 		DELAY(20);
    524 	}
    525 
    526 	/* then reset */
    527 	bus_space_write_4(t, cr, QE_CRI_CTRL, QE_CR_CTRL_RESET);
    528 	for (n = 200; n > 0; n--) {
    529 		if ((bus_space_read_4(t, cr, QE_CRI_CTRL) &
    530 			QE_CR_CTRL_RESET) == 0)
    531 			break;
    532 		DELAY(20);
    533 	}
    534 }
    535 
    536 /*
    537  * Reset interface.
    538  */
    539 void
    540 qereset(sc)
    541 	struct qe_softc *sc;
    542 {
    543 	int s;
    544 
    545 	s = splnet();
    546 	qestop(sc);
    547 	qeinit(sc);
    548 	splx(s);
    549 }
    550 
    551 void
    552 qewatchdog(ifp)
    553 	struct ifnet *ifp;
    554 {
    555 	struct qe_softc *sc = ifp->if_softc;
    556 
    557 	log(LOG_ERR, "%s: device timeout\n", sc->sc_dev.dv_xname);
    558 	++sc->sc_ethercom.ec_if.if_oerrors;
    559 
    560 	qereset(sc);
    561 }
    562 
    563 /*
    564  * Interrupt dispatch.
    565  */
    566 int
    567 qeintr(arg)
    568 	void *arg;
    569 {
    570 	struct qe_softc *sc = (struct qe_softc *)arg;
    571 	bus_space_tag_t t = sc->sc_bustag;
    572 	u_int32_t qecstat, qestat;
    573 	int r = 0;
    574 
    575 	/* Read QEC status and channel status */
    576 	qecstat = bus_space_read_4(t, sc->sc_qr, QEC_QRI_STAT);
    577 
    578 	/* Filter out status for this channel */
    579 	qecstat = qecstat >> (4 * sc->sc_channel);
    580 	if ((qecstat & 0xf) == 0)
    581 		return (r);
    582 
    583 	qestat = bus_space_read_4(t, sc->sc_cr, QE_CRI_STAT);
    584 
    585 	if (qestat & QE_CR_STAT_ALLERRORS) {
    586 		r |= qe_eint(sc, qestat);
    587 		if (r == -1)
    588 			return (1);
    589 	}
    590 
    591 	if (qestat & QE_CR_STAT_TXIRQ)
    592 		r |= qe_tint(sc);
    593 
    594 	if (qestat & QE_CR_STAT_RXIRQ)
    595 		r |= qe_rint(sc);
    596 
    597 	return (r);
    598 }
    599 
    600 /*
    601  * Transmit interrupt.
    602  */
    603 int
    604 qe_tint(sc)
    605 	struct qe_softc *sc;
    606 {
    607 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    608 	unsigned int bix, txflags;
    609 
    610 	bix = sc->sc_rb.rb_tdtail;
    611 
    612 	for (;;) {
    613 		if (sc->sc_rb.rb_td_nbusy <= 0)
    614 			break;
    615 
    616 		txflags = sc->sc_rb.rb_txd[bix].xd_flags;
    617 
    618 		if (txflags & QEC_XD_OWN)
    619 			break;
    620 
    621 		ifp->if_flags &= ~IFF_OACTIVE;
    622 		ifp->if_opackets++;
    623 
    624 		if (++bix == QEC_XD_RING_MAXSIZE)
    625 			bix = 0;
    626 
    627 		--sc->sc_rb.rb_td_nbusy;
    628 	}
    629 
    630 	sc->sc_rb.rb_tdtail = bix;
    631 
    632 	qestart(ifp);
    633 
    634 	if (sc->sc_rb.rb_td_nbusy == 0)
    635 		ifp->if_timer = 0;
    636 
    637 	return (1);
    638 }
    639 
    640 /*
    641  * Receive interrupt.
    642  */
    643 int
    644 qe_rint(sc)
    645 	struct qe_softc *sc;
    646 {
    647 	struct qec_xd *xd = sc->sc_rb.rb_rxd;
    648 	unsigned int bix, len;
    649 	unsigned int nrbuf = sc->sc_rb.rb_nrbuf;
    650 
    651 	bix = sc->sc_rb.rb_rdtail;
    652 
    653 	/*
    654 	 * Process all buffers with valid data.
    655 	 */
    656 	for (;;) {
    657 		len = xd[bix].xd_flags;
    658 		if (len & QEC_XD_OWN)
    659 			break;
    660 
    661 		len &= QEC_XD_LENGTH;
    662 		len -= 4;
    663 		qe_read(sc, bix, len);
    664 
    665 		/* ... */
    666 		xd[(bix+nrbuf) % QEC_XD_RING_MAXSIZE].xd_flags =
    667 			QEC_XD_OWN | (QE_PKT_BUF_SZ & QEC_XD_LENGTH);
    668 
    669 		if (++bix == QEC_XD_RING_MAXSIZE)
    670 			bix = 0;
    671 	}
    672 
    673 	sc->sc_rb.rb_rdtail = bix;
    674 
    675 	return (1);
    676 }
    677 
    678 /*
    679  * Error interrupt.
    680  */
    681 int
    682 qe_eint(sc, why)
    683 	struct qe_softc *sc;
    684 	u_int32_t why;
    685 {
    686 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    687 	int r = 0, rst = 0;
    688 
    689 	if (why & QE_CR_STAT_EDEFER) {
    690 		printf("%s: excessive tx defers.\n", sc->sc_dev.dv_xname);
    691 		r |= 1;
    692 		ifp->if_oerrors++;
    693 	}
    694 
    695 	if (why & QE_CR_STAT_CLOSS) {
    696 		printf("%s: no carrier, link down?\n", sc->sc_dev.dv_xname);
    697 		ifp->if_oerrors++;
    698 		r |= 1;
    699 	}
    700 
    701 	if (why & QE_CR_STAT_ERETRIES) {
    702 		printf("%s: excessive tx retries\n", sc->sc_dev.dv_xname);
    703 		ifp->if_oerrors++;
    704 		r |= 1;
    705 		rst = 1;
    706 	}
    707 
    708 
    709 	if (why & QE_CR_STAT_LCOLL) {
    710 		printf("%s: late tx transmission\n", sc->sc_dev.dv_xname);
    711 		ifp->if_oerrors++;
    712 		r |= 1;
    713 		rst = 1;
    714 	}
    715 
    716 	if (why & QE_CR_STAT_FUFLOW) {
    717 		printf("%s: tx fifo underflow\n", sc->sc_dev.dv_xname);
    718 		ifp->if_oerrors++;
    719 		r |= 1;
    720 		rst = 1;
    721 	}
    722 
    723 	if (why & QE_CR_STAT_JERROR) {
    724 		printf("%s: jabber seen\n", sc->sc_dev.dv_xname);
    725 		r |= 1;
    726 	}
    727 
    728 	if (why & QE_CR_STAT_BERROR) {
    729 		printf("%s: babble seen\n", sc->sc_dev.dv_xname);
    730 		r |= 1;
    731 	}
    732 
    733 	if (why & QE_CR_STAT_TCCOFLOW) {
    734 		ifp->if_collisions += 256;
    735 		ifp->if_oerrors += 256;
    736 		r |= 1;
    737 	}
    738 
    739 	if (why & QE_CR_STAT_TXDERROR) {
    740 		printf("%s: tx descriptor is bad\n", sc->sc_dev.dv_xname);
    741 		rst = 1;
    742 		r |= 1;
    743 	}
    744 
    745 	if (why & QE_CR_STAT_TXLERR) {
    746 		printf("%s: tx late error\n", sc->sc_dev.dv_xname);
    747 		ifp->if_oerrors++;
    748 		rst = 1;
    749 		r |= 1;
    750 	}
    751 
    752 	if (why & QE_CR_STAT_TXPERR) {
    753 		printf("%s: tx dma parity error\n", sc->sc_dev.dv_xname);
    754 		ifp->if_oerrors++;
    755 		rst = 1;
    756 		r |= 1;
    757 	}
    758 
    759 	if (why & QE_CR_STAT_TXSERR) {
    760 		printf("%s: tx dma sbus error ack\n", sc->sc_dev.dv_xname);
    761 		ifp->if_oerrors++;
    762 		rst = 1;
    763 		r |= 1;
    764 	}
    765 
    766 	if (why & QE_CR_STAT_RCCOFLOW) {
    767 		ifp->if_collisions += 256;
    768 		ifp->if_ierrors += 256;
    769 		r |= 1;
    770 	}
    771 
    772 	if (why & QE_CR_STAT_RUOFLOW) {
    773 		ifp->if_ierrors += 256;
    774 		r |= 1;
    775 	}
    776 
    777 	if (why & QE_CR_STAT_MCOFLOW) {
    778 		ifp->if_ierrors += 256;
    779 		r |= 1;
    780 	}
    781 
    782 	if (why & QE_CR_STAT_RXFOFLOW) {
    783 		printf("%s: rx fifo overflow\n", sc->sc_dev.dv_xname);
    784 		ifp->if_ierrors++;
    785 		r |= 1;
    786 	}
    787 
    788 	if (why & QE_CR_STAT_RLCOLL) {
    789 		printf("%s: rx late collision\n", sc->sc_dev.dv_xname);
    790 		ifp->if_ierrors++;
    791 		ifp->if_collisions++;
    792 		r |= 1;
    793 	}
    794 
    795 	if (why & QE_CR_STAT_FCOFLOW) {
    796 		ifp->if_ierrors += 256;
    797 		r |= 1;
    798 	}
    799 
    800 	if (why & QE_CR_STAT_CECOFLOW) {
    801 		ifp->if_ierrors += 256;
    802 		r |= 1;
    803 	}
    804 
    805 	if (why & QE_CR_STAT_RXDROP) {
    806 		printf("%s: rx packet dropped\n", sc->sc_dev.dv_xname);
    807 		ifp->if_ierrors++;
    808 		r |= 1;
    809 	}
    810 
    811 	if (why & QE_CR_STAT_RXSMALL) {
    812 		printf("%s: rx buffer too small\n", sc->sc_dev.dv_xname);
    813 		ifp->if_ierrors++;
    814 		r |= 1;
    815 		rst = 1;
    816 	}
    817 
    818 	if (why & QE_CR_STAT_RXLERR) {
    819 		printf("%s: rx late error\n", sc->sc_dev.dv_xname);
    820 		ifp->if_ierrors++;
    821 		r |= 1;
    822 		rst = 1;
    823 	}
    824 
    825 	if (why & QE_CR_STAT_RXPERR) {
    826 		printf("%s: rx dma parity error\n", sc->sc_dev.dv_xname);
    827 		ifp->if_ierrors++;
    828 		r |= 1;
    829 		rst = 1;
    830 	}
    831 
    832 	if (why & QE_CR_STAT_RXSERR) {
    833 		printf("%s: rx dma sbus error ack\n", sc->sc_dev.dv_xname);
    834 		ifp->if_ierrors++;
    835 		r |= 1;
    836 		rst = 1;
    837 	}
    838 
    839 	if (r == 0)
    840 		printf("%s: unexpected interrupt error: %08x\n",
    841 			sc->sc_dev.dv_xname, why);
    842 
    843 	if (rst) {
    844 		printf("%s: resetting...\n", sc->sc_dev.dv_xname);
    845 		qereset(sc);
    846 		return (-1);
    847 	}
    848 
    849 	return (r);
    850 }
    851 
    852 int
    853 qeioctl(ifp, cmd, data)
    854 	struct ifnet *ifp;
    855 	u_long cmd;
    856 	caddr_t data;
    857 {
    858 	struct qe_softc *sc = ifp->if_softc;
    859 	struct ifaddr *ifa = (struct ifaddr *)data;
    860 	struct ifreq *ifr = (struct ifreq *)data;
    861 	int s, error = 0;
    862 
    863 	s = splnet();
    864 
    865 	switch (cmd) {
    866 	case SIOCSIFADDR:
    867 		ifp->if_flags |= IFF_UP;
    868 		switch (ifa->ifa_addr->sa_family) {
    869 #ifdef INET
    870 		case AF_INET:
    871 			qeinit(sc);
    872 			arp_ifinit(ifp, ifa);
    873 			break;
    874 #endif /* INET */
    875 #ifdef NS
    876 		case AF_NS:
    877 		    {
    878 			struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
    879 
    880 			if (ns_nullhost(*ina))
    881 				ina->x_host =
    882 					*(union ns_host *)LLADDR(ifp->if_sadl);
    883 			else
    884 				bcopy(ina->x_host.c_host, LLADDR(ifp->if_sadl),
    885 				      sizeof(sc->sc_enaddr));
    886 			/* Set new address. */
    887 			qeinit(sc);
    888 			break;
    889 		    }
    890 #endif /* NS */
    891 		default:
    892 			qeinit(sc);
    893 			break;
    894 		}
    895 		break;
    896 
    897 	case SIOCSIFFLAGS:
    898 		sc->sc_promisc = ifp->if_flags & (IFF_PROMISC | IFF_ALLMULTI);
    899 		if ((ifp->if_flags & IFF_UP) == 0 &&
    900 		    (ifp->if_flags & IFF_RUNNING) != 0) {
    901 			/*
    902 			 * If interface is marked down and it is running, then
    903 			 * stop it.
    904 			 */
    905 			qestop(sc);
    906 			ifp->if_flags &= ~IFF_RUNNING;
    907 		} else if ((ifp->if_flags & IFF_UP) != 0 &&
    908 		    (ifp->if_flags & IFF_RUNNING) == 0) {
    909 			/*
    910 			 * If interface is marked up and it is stopped, then
    911 			 * start it.
    912 			 */
    913 			qeinit(sc);
    914 		} else {
    915 			/*
    916 			 * Reset the interface to pick up changes in any other
    917 			 * flags that affect hardware registers.
    918 			 */
    919 			qestop(sc);
    920 			qeinit(sc);
    921 		}
    922 #ifdef QEDEBUG
    923 		if (ifp->if_flags & IFF_DEBUG)
    924 			sc->sc_debug = 1;
    925 		else
    926 			sc->sc_debug = 0;
    927 #endif
    928 		break;
    929 
    930 	case SIOCADDMULTI:
    931 	case SIOCDELMULTI:
    932 		error = (cmd == SIOCADDMULTI) ?
    933 		    ether_addmulti(ifr, &sc->sc_ethercom):
    934 		    ether_delmulti(ifr, &sc->sc_ethercom);
    935 
    936 		if (error == ENETRESET) {
    937 			/*
    938 			 * Multicast list has changed; set the hardware filter
    939 			 * accordingly.
    940 			 */
    941 			qe_mcreset(sc);
    942 			error = 0;
    943 		}
    944 		break;
    945 
    946 	case SIOCGIFMEDIA:
    947 	case SIOCSIFMEDIA:
    948 		error = ifmedia_ioctl(ifp, ifr, &sc->sc_ifmedia, cmd);
    949 		break;
    950 
    951 	default:
    952 		error = EINVAL;
    953 		break;
    954 	}
    955 
    956 	splx(s);
    957 	return (error);
    958 }
    959 
    960 
    961 void
    962 qeinit(sc)
    963 	struct qe_softc *sc;
    964 {
    965 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    966 	bus_space_tag_t t = sc->sc_bustag;
    967 	bus_space_handle_t cr = sc->sc_cr;
    968 	bus_space_handle_t mr = sc->sc_mr;
    969 	struct qec_softc *qec = sc->sc_qec;
    970 	u_int32_t qecaddr;
    971 	u_int8_t *ea;
    972 	int i, s;
    973 
    974 	s = splimp();
    975 	qestop(sc);
    976 
    977 	/*
    978 	 * Allocate descriptor ring and buffers
    979 	 */
    980 	qec_meminit(&sc->sc_rb, QE_PKT_BUF_SZ);
    981 
    982 	/* Channel registers: */
    983 	bus_space_write_4(t, cr, QE_CRI_RXDS, (u_int32_t)sc->sc_rb.rb_rxddma);
    984 	bus_space_write_4(t, cr, QE_CRI_TXDS, (u_int32_t)sc->sc_rb.rb_txddma);
    985 
    986 	bus_space_write_4(t, cr, QE_CRI_RIMASK, 0);
    987 	bus_space_write_4(t, cr, QE_CRI_TIMASK, 0);
    988 	bus_space_write_4(t, cr, QE_CRI_QMASK, 0);
    989 	bus_space_write_4(t, cr, QE_CRI_MMASK, QE_CR_MMASK_RXCOLL);
    990 	bus_space_write_4(t, cr, QE_CRI_CCNT, 0);
    991 	bus_space_write_4(t, cr, QE_CRI_PIPG, 0);
    992 
    993 	qecaddr = sc->sc_channel * qec->sc_msize;
    994 	bus_space_write_4(t, cr, QE_CRI_RXWBUF, qecaddr);
    995 	bus_space_write_4(t, cr, QE_CRI_RXRBUF, qecaddr);
    996 	bus_space_write_4(t, cr, QE_CRI_TXWBUF, qecaddr + qec->sc_rsize);
    997 	bus_space_write_4(t, cr, QE_CRI_TXRBUF, qecaddr + qec->sc_rsize);
    998 
    999 	/* MACE registers: */
   1000 	bus_space_write_1(t, mr, QE_MRI_PHYCC, QE_MR_PHYCC_ASEL);
   1001 	bus_space_write_1(t, mr, QE_MRI_XMTFC, QE_MR_XMTFC_APADXMT);
   1002 	bus_space_write_1(t, mr, QE_MRI_RCVFC, 0);
   1003 	bus_space_write_1(t, mr, QE_MRI_IMR,
   1004 			  QE_MR_IMR_CERRM | QE_MR_IMR_RCVINTM);
   1005 	bus_space_write_1(t, mr, QE_MRI_BIUCC,
   1006 			  QE_MR_BIUCC_BSWAP | QE_MR_BIUCC_64TS);
   1007 
   1008 	bus_space_write_1(t, mr, QE_MRI_FIFOFC,
   1009 			  QE_MR_FIFOCC_TXF16 | QE_MR_FIFOCC_RXF32 |
   1010 			  QE_MR_FIFOCC_RFWU | QE_MR_FIFOCC_TFWU);
   1011 
   1012 	bus_space_write_1(t, mr, QE_MRI_PLSCC, QE_MR_PLSCC_TP);
   1013 
   1014 	/*
   1015 	 * Station address
   1016 	 */
   1017 	ea = sc->sc_enaddr;
   1018 	bus_space_write_1(t, mr, QE_MRI_IAC,
   1019 			  QE_MR_IAC_ADDRCHG | QE_MR_IAC_PHYADDR);
   1020 	bus_space_write_1(t, mr, QE_MRI_PADR, ea[0]);
   1021 	bus_space_write_1(t, mr, QE_MRI_PADR, ea[1]);
   1022 	bus_space_write_1(t, mr, QE_MRI_PADR, ea[2]);
   1023 	bus_space_write_1(t, mr, QE_MRI_PADR, ea[3]);
   1024 	bus_space_write_1(t, mr, QE_MRI_PADR, ea[4]);
   1025 	bus_space_write_1(t, mr, QE_MRI_PADR, ea[5]);
   1026 
   1027 	/* Apply media settings */
   1028 	qe_ifmedia_upd(ifp);
   1029 
   1030 	/*
   1031 	 * Logical address filter
   1032 	 */
   1033 	bus_space_write_1(t, mr, QE_MRI_IAC,
   1034 			  QE_MR_IAC_ADDRCHG | QE_MR_IAC_LOGADDR);
   1035 	for (i = 0; i < 8; i++)
   1036 		bus_space_write_1(t, mr, QE_MRI_LADRF, 0);
   1037 	bus_space_write_1(t, mr, QE_MRI_IAC, 0);
   1038 
   1039 	/* Clear missed packet count (register cleared on read) */
   1040 	(void)bus_space_read_1(t, mr, QE_MRI_MPC);
   1041 
   1042 	/* Enable transmitter & receiver */
   1043 	bus_space_write_1(t, mr, QE_MRI_MACCC,
   1044 			  QE_MR_MACCC_ENXMT | QE_MR_MACCC_ENRCV |
   1045 			  ((ifp->if_flags&IFF_PROMISC) ? QE_MR_MACCC_PROM : 0));
   1046 
   1047 	ifp->if_flags |= IFF_RUNNING;
   1048 	ifp->if_flags &= ~IFF_OACTIVE;
   1049 	splx(s);
   1050 }
   1051 
   1052 /*
   1053  * Reset multicast filter.
   1054  */
   1055 void
   1056 qe_mcreset(sc)
   1057 	struct qe_softc *sc;
   1058 {
   1059 	struct ethercom *ec = &sc->sc_ethercom;
   1060 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1061 	bus_space_tag_t t = sc->sc_bustag;
   1062 	bus_space_handle_t mr = sc->sc_mr;
   1063 	struct ether_multi *enm;
   1064 	struct ether_multistep step;
   1065 	u_int32_t crc;
   1066 	u_int16_t hash[4];
   1067 	u_int8_t octet, maccc = 0, *ladrp = (u_int8_t *)&hash[0];
   1068 	int i, j;
   1069 
   1070 	if (ifp->if_flags & IFF_ALLMULTI) {
   1071 		bus_space_write_1(t, mr, QE_MRI_IAC,
   1072 				  QE_MR_IAC_ADDRCHG | QE_MR_IAC_LOGADDR);
   1073 		for (i = 0; i < 8; i++)
   1074 			bus_space_write_1(t, mr, QE_MRI_LADRF, 0xff);
   1075 		bus_space_write_1(t, mr, QE_MRI_IAC, 0);
   1076 	} else if (ifp->if_flags & IFF_PROMISC) {
   1077 		maccc |= QE_MR_MACCC_PROM;
   1078 	} else {
   1079 		hash[3] = hash[2] = hash[1] = hash[0] = 0;
   1080 
   1081 		ETHER_FIRST_MULTI(step, ec, enm);
   1082 		while (enm != NULL) {
   1083 			if (bcmp(enm->enm_addrlo, enm->enm_addrhi,
   1084 				 ETHER_ADDR_LEN) != 0) {
   1085 				/*
   1086 				 * We must listen to a range of multicast
   1087 				 * addresses. For now, just accept all
   1088 				 * multicasts, rather than trying to set only
   1089 				 * those filter bits needed to match the range.
   1090 				 * (At this time, the only use of address
   1091 				 * ranges is for IP multicast routing, for
   1092 				 * which the range is big enough to require
   1093 				 * all bits set.)
   1094 				 */
   1095 				bus_space_write_1(t, mr, QE_MRI_IAC,
   1096 					 QE_MR_IAC_ADDRCHG | QE_MR_IAC_LOGADDR);
   1097 				for (i = 0; i < 8; i++)
   1098 					bus_space_write_1(t, mr, QE_MRI_LADRF,
   1099 							  0xff);
   1100 				bus_space_write_1(t, mr, QE_MRI_IAC, 0);
   1101 				ifp->if_flags |= IFF_ALLMULTI;
   1102 				break;
   1103 			}
   1104 
   1105 			crc = 0xffffffff;
   1106 
   1107 			for (i = 0; i < ETHER_ADDR_LEN; i++) {
   1108 				octet = enm->enm_addrlo[i];
   1109 
   1110 				for (j = 0; j < 8; j++) {
   1111 					if ((crc & 1) ^ (octet & 1)) {
   1112 						crc >>= 1;
   1113 						crc ^= MC_POLY_LE;
   1114 					}
   1115 					else
   1116 						crc >>= 1;
   1117 					octet >>= 1;
   1118 				}
   1119 			}
   1120 
   1121 			crc >>= 26;
   1122 			hash[crc >> 4] |= 1 << (crc & 0xf);
   1123 			ETHER_NEXT_MULTI(step, enm);
   1124 		}
   1125 
   1126 		bus_space_write_1(t, mr, QE_MRI_IAC,
   1127 				  QE_MR_IAC_ADDRCHG | QE_MR_IAC_LOGADDR);
   1128 		for (i = 0; i < 8; i++)
   1129 			bus_space_write_1(t, mr, QE_MRI_LADRF, ladrp[i]);
   1130 		bus_space_write_1(t, mr, QE_MRI_IAC, 0);
   1131 	}
   1132 
   1133 	bus_space_write_1(t, mr, QE_MRI_MACCC,
   1134 			  maccc | QE_MR_MACCC_ENXMT | QE_MR_MACCC_ENRCV);
   1135 }
   1136 
   1137 /*
   1138  * Get current media settings.
   1139  */
   1140 void
   1141 qe_ifmedia_sts(ifp, ifmr)
   1142 	struct ifnet *ifp;
   1143 	struct ifmediareq *ifmr;
   1144 {
   1145 	struct qe_softc *sc = ifp->if_softc;
   1146 	bus_space_tag_t t = sc->sc_bustag;
   1147 	bus_space_handle_t mr = sc->sc_mr;
   1148 	u_int8_t v;
   1149 
   1150 	v = bus_space_read_1(t, mr, QE_MRI_PLSCC);
   1151 
   1152 	switch (bus_space_read_1(t, mr, QE_MRI_PLSCC) & QE_MR_PLSCC_PORTMASK) {
   1153 	case QE_MR_PLSCC_TP:
   1154 		ifmr->ifm_active = IFM_ETHER | IFM_10_T;
   1155 		break;
   1156 	case QE_MR_PLSCC_AUI:
   1157 		ifmr->ifm_active = IFM_ETHER | IFM_10_5;
   1158 		break;
   1159 	case QE_MR_PLSCC_GPSI:
   1160 	case QE_MR_PLSCC_DAI:
   1161 		/* ... */
   1162 		break;
   1163 	}
   1164 
   1165 	v = bus_space_read_1(t, mr, QE_MRI_PHYCC);
   1166 	ifmr->ifm_status |=  IFM_AVALID;
   1167 	if ((v & QE_MR_PHYCC_LNKFL) != 0)
   1168 		ifmr->ifm_status &= ~IFM_ACTIVE;
   1169 	else
   1170 		ifmr->ifm_status |=  IFM_ACTIVE;
   1171 
   1172 }
   1173 
   1174 /*
   1175  * Set media options.
   1176  */
   1177 int
   1178 qe_ifmedia_upd(ifp)
   1179 	struct ifnet *ifp;
   1180 {
   1181 	struct qe_softc *sc = ifp->if_softc;
   1182 	struct ifmedia *ifm = &sc->sc_ifmedia;
   1183 	bus_space_tag_t t = sc->sc_bustag;
   1184 	bus_space_handle_t mr = sc->sc_mr;
   1185 	int newmedia = ifm->ifm_media;
   1186 	u_int8_t plscc, phycc;
   1187 
   1188 	if (IFM_TYPE(newmedia) != IFM_ETHER)
   1189 		return (EINVAL);
   1190 
   1191 	plscc = bus_space_read_1(t, mr, QE_MRI_PLSCC) & ~QE_MR_PLSCC_PORTMASK;
   1192 	phycc = bus_space_read_1(t, mr, QE_MRI_PHYCC) & ~QE_MR_PHYCC_ASEL;
   1193 
   1194 	if (IFM_SUBTYPE(newmedia) == IFM_AUTO)
   1195 		phycc |= QE_MR_PHYCC_ASEL;
   1196 	else if (IFM_SUBTYPE(newmedia) == IFM_10_T)
   1197 		plscc |= QE_MR_PLSCC_TP;
   1198 	else if (IFM_SUBTYPE(newmedia) == IFM_10_5)
   1199 		plscc |= QE_MR_PLSCC_AUI;
   1200 
   1201 	bus_space_write_1(t, mr, QE_MRI_PLSCC, plscc);
   1202 	bus_space_write_1(t, mr, QE_MRI_PHYCC, phycc);
   1203 
   1204 	return (0);
   1205 }
   1206