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