Home | History | Annotate | Line # | Download | only in qbus
if_de.c revision 1.1
      1 /*	$NetBSD: if_de.c,v 1.1 2000/04/30 11:43:26 ragge Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1982, 1986, 1989 Regents of the University of California.
      5  * Copyright (c) 2000 Ludd, University of Lule}, Sweden.
      6  * All rights reserved.
      7  *
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  * 3. All advertising materials mentioning features or use of this software
     18  *    must display the following acknowledgement:
     19  *	This product includes software developed by the University of
     20  *	California, Berkeley and its contributors.
     21  * 4. Neither the name of the University nor the names of its contributors
     22  *    may be used to endorse or promote products derived from this software
     23  *    without specific prior written permission.
     24  *
     25  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     35  * SUCH DAMAGE.
     36  *
     37  *	@(#)if_de.c	7.12 (Berkeley) 12/16/90
     38  */
     39 
     40 /*
     41  * DEC DEUNA interface
     42  *
     43  *	Lou Salkind
     44  *	New York University
     45  *
     46  *	Rewritten by Ragge 000430 to match new world.
     47  *
     48  * TODO:
     49  *	timeout routine (get statistics)
     50  */
     51 
     52 #include "opt_inet.h"
     53 #include "opt_iso.h"
     54 #include "opt_ns.h"
     55 
     56 #include <sys/param.h>
     57 #include <sys/systm.h>
     58 #include <sys/mbuf.h>
     59 #include <sys/buf.h>
     60 #include <sys/protosw.h>
     61 #include <sys/socket.h>
     62 #include <sys/ioctl.h>
     63 #include <sys/errno.h>
     64 #include <sys/syslog.h>
     65 #include <sys/device.h>
     66 
     67 #include <net/if.h>
     68 #include <net/if_ether.h>
     69 #include <net/if_dl.h>
     70 
     71 #ifdef INET
     72 #include <netinet/in.h>
     73 #include <netinet/if_inarp.h>
     74 #endif
     75 
     76 #ifdef NS
     77 #include <netns/ns.h>
     78 #include <netns/ns_if.h>
     79 #endif
     80 
     81 #ifdef ISO
     82 #include <netiso/iso.h>
     83 #include <netiso/iso_var.h>
     84 extern char all_es_snpa[], all_is_snpa[];
     85 #endif
     86 
     87 #include <machine/bus.h>
     88 
     89 #include <dev/qbus/ubavar.h>
     90 #include <dev/qbus/if_dereg.h>
     91 
     92 #include "ioconf.h"
     93 
     94 /*
     95  * Be careful with transmit/receive buffers, each entry steals 4 map
     96  * registers, and there is only 496 on one unibus...
     97  */
     98 #define NRCV	10	/* number of receive buffers (must be > 1) */
     99 #define NXMT	20	/* number of transmit buffers */
    100 #define NFRAGS	8	/* Number of frags per transmit buffer */
    101 
    102 /*
    103  * Structure containing the elements that must be in DMA-safe memory.
    104  */
    105 struct	de_cdata {
    106 	/* the following structures are always mapped in */
    107 	struct	de_pcbb dc_pcbb;	/* port control block */
    108 	struct	de_ring dc_xrent[NXMT]; /* transmit ring entrys */
    109 	struct	de_ring dc_rrent[NRCV]; /* receive ring entrys */
    110 	struct	de_udbbuf dc_udbbuf;	/* UNIBUS data buffer */
    111 	/* end mapped area */
    112 };
    113 
    114 /*
    115  * Ethernet software status per interface.
    116  *
    117  * Each interface is referenced by a network interface structure,
    118  * ds_if, which the routing code uses to locate the interface.
    119  * This structure contains the output queue for the interface, its address, ...
    120  * We also have, for each interface, a UBA interface structure, which
    121  * contains information about the UNIBUS resources held by the interface:
    122  * map registers, buffered data paths, etc.  Information is cached in this
    123  * structure for use by the if_uba.c routines in running the interface
    124  * efficiently.
    125  */
    126 struct	de_softc {
    127 	struct	device sc_dev;		/* Configuration common part */
    128 	struct	ethercom sc_ec;		/* Ethernet common part */
    129 #define sc_if	sc_ec.ec_if		/* network-visible interface */
    130 	int	sc_flags;
    131 #define DSF_RUNNING	2		/* board is enabled */
    132 #define DSF_SETADDR	4		/* physical address is changed */
    133 	bus_space_tag_t sc_iot;
    134 	bus_addr_t sc_ioh;
    135 	bus_dma_tag_t sc_dmat;
    136 	struct de_cdata *sc_dedata;	/* Control structure */
    137 	struct de_cdata *sc_pdedata;	/* Bus-mapped control structure */
    138 	bus_dmamap_t sc_xmtmap[NXMT];	/* unibus receive maps */
    139 	bus_dmamap_t sc_rcvmap[NRCV];	/* unibus xmt maps */
    140 	struct mbuf *sc_txmbuf[NXMT];
    141 	struct mbuf *sc_rxmbuf[NRCV];
    142 	int sc_nexttx;
    143 	int sc_nextrx;
    144 	int sc_inq;
    145 	int sc_lastack;
    146 };
    147 
    148 static	int dematch(struct device *, struct cfdata *, void *);
    149 static	void deattach(struct device *, struct device *, void *);
    150 static	int dewait(struct de_softc *, char *);
    151 static	void deinit(struct de_softc *);
    152 static	int deioctl(struct ifnet *, u_long, caddr_t);
    153 static	void dereset(struct device *);
    154 static	void destart(struct ifnet *);
    155 static	void derecv(struct de_softc *);
    156 static	void dexmit(struct de_softc *);
    157 static	void de_setaddr(u_char *, struct de_softc *);
    158 static	void deintr(void *);
    159 static	int de_add_rxbuf(struct de_softc *, int);
    160 
    161 struct	cfattach de_ca = {
    162 	sizeof(struct de_softc), dematch, deattach
    163 };
    164 
    165 #define DE_WCSR(csr, val) \
    166 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, csr, val)
    167 #define DE_WLOW(val) \
    168 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, DE_PCSR0, val)
    169 #define DE_WHIGH(val) \
    170 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, DE_PCSR0 + 1, val)
    171 #define DE_RCSR(csr) \
    172 	bus_space_read_2(sc->sc_iot, sc->sc_ioh, csr)
    173 
    174 #define LOWORD(x)	((int)(x) & 0xffff)
    175 #define HIWORD(x)	(((int)(x) >> 16) & 0x3)
    176 /*
    177  * Interface exists: make available by filling in network interface
    178  * record.  System will initialize the interface when it is ready
    179  * to accept packets.  We get the ethernet address here.
    180  */
    181 void
    182 deattach(struct device *parent, struct device *self, void *aux)
    183 {
    184 	struct uba_attach_args *ua = aux;
    185 	struct de_softc *sc = (struct de_softc *)self;
    186 	struct ifnet *ifp = &sc->sc_if;
    187 	u_int8_t myaddr[ETHER_ADDR_LEN];
    188 	bus_dma_segment_t seg;
    189 	int csr1, rseg, error, i;
    190 	char *c;
    191 
    192 	sc->sc_iot = ua->ua_iot;
    193 	sc->sc_ioh = ua->ua_ioh;
    194 	sc->sc_dmat = ua->ua_dmat;
    195 
    196 	/*
    197 	 * What kind of a board is this?
    198 	 * The error bits 4-6 in pcsr1 are a device id as long as
    199 	 * the high byte is zero.
    200 	 */
    201 	csr1 = DE_RCSR(DE_PCSR1);
    202 	if (csr1 & 0xff60)
    203 		c = "broken";
    204 	else if (csr1 & 0x10)
    205 		c = "delua";
    206 	else
    207 		c = "deuna";
    208 
    209 	/*
    210 	 * Reset the board and temporarily map
    211 	 * the pcbb buffer onto the Unibus.
    212 	 */
    213 	DE_WCSR(DE_PCSR0, 0);		/* reset INTE */
    214 	DELAY(100);
    215 	DE_WCSR(DE_PCSR0, PCSR0_RSET);
    216 	(void)dewait(sc, "reset");
    217 
    218 	if ((error = bus_dmamem_alloc(sc->sc_dmat,
    219 	    sizeof(struct de_cdata), NBPG, 0, &seg, 1, &rseg,
    220 	    BUS_DMA_NOWAIT)) != 0) {
    221 		printf(": unable to allocate control data, error = %d\n",
    222 		    error);
    223 		goto fail_0;
    224 	}
    225 	if ((error = bus_dmamem_map(sc->sc_dmat, &seg, rseg,
    226 	    sizeof(struct de_cdata), (caddr_t *)&sc->sc_dedata,
    227 	    BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) != 0) {
    228 		printf(": unable to map control data, error = %d\n", error);
    229 		goto fail_1;
    230 	}
    231 
    232 	/*
    233 	 * Create the transmit descriptor DMA maps.
    234 	 *
    235 	 * XXX - should allocate transmit map pages when needed, not here.
    236 	 */
    237 	for (i = 0; i < NXMT; i++) {
    238 		if ((error = bus_dmamap_create(sc->sc_dmat, MCLBYTES, NFRAGS,
    239 		    MCLBYTES, 0, BUS_DMA_NOWAIT|BUS_DMA_ALLOCNOW,
    240 		    &sc->sc_xmtmap[i]))) {
    241 			printf(": unable to create tx DMA map %d, error = %d\n",
    242 			    i, error);
    243 			goto fail_4;
    244 		}
    245 	}
    246 
    247 	/*
    248 	 * Create receive buffer DMA maps.
    249 	 */
    250 	for (i = 0; i < NRCV; i++) {
    251 		if ((error = bus_dmamap_create(sc->sc_dmat, MCLBYTES, 1,
    252 		    MCLBYTES, 0, BUS_DMA_NOWAIT,
    253 		    &sc->sc_rcvmap[i]))) {
    254 			printf(": unable to create rx DMA map %d, error = %d\n",
    255 			    i, error);
    256 			goto fail_5;
    257 		}
    258 	}
    259 
    260 	/*
    261 	 * Pre-allocate the receive buffers.
    262 	 */
    263 	for (i = 0; i < NRCV; i++) {
    264 		if ((error = de_add_rxbuf(sc, i)) != 0) {
    265 			printf(": unable to allocate or map rx buffer %d\n,"
    266 			    " error = %d\n", i, error);
    267 			goto fail_6;
    268 		}
    269 	}
    270 
    271 	bzero(sc->sc_dedata, sizeof(struct de_cdata));
    272 	sc->sc_pdedata = (struct de_cdata *)seg.ds_addr;
    273 
    274 	/*
    275 	 * Tell the DEUNA about our PCB
    276 	 */
    277 	DE_WCSR(DE_PCSR2, LOWORD(sc->sc_pdedata));
    278 	DE_WCSR(DE_PCSR3, HIWORD(sc->sc_pdedata));
    279 	DE_WLOW(CMD_GETPCBB);
    280 	(void)dewait(sc, "pcbb");
    281 
    282 	sc->sc_dedata->dc_pcbb.pcbb0 = FC_RDPHYAD;
    283 	DE_WLOW(CMD_GETCMD);
    284 	(void)dewait(sc, "read addr ");
    285 
    286 	bcopy((caddr_t)&sc->sc_dedata->dc_pcbb.pcbb2, myaddr, sizeof (myaddr));
    287 	printf("%s: %s, hardware address %s\n", c, sc->sc_dev.dv_xname,
    288 		ether_sprintf(myaddr));
    289 
    290 	uba_intr_establish(ua->ua_icookie, ua->ua_cvec, deintr, sc);
    291 	uba_reset_establish(dereset, &sc->sc_dev);
    292 
    293 	strcpy(ifp->if_xname, sc->sc_dev.dv_xname);
    294 	ifp->if_softc = sc;
    295 	ifp->if_flags = IFF_BROADCAST | IFF_MULTICAST | IFF_SIMPLEX;
    296 	ifp->if_ioctl = deioctl;
    297 	ifp->if_start = destart;
    298 	if_attach(ifp);
    299 	ether_ifattach(ifp, myaddr);
    300 #if NBPFILTER > 0
    301 	bpfattach(&ifp->if_bpf, ifp, DLT_EN10MB, sizeof(struct ether_header));
    302 #endif
    303 	return;
    304 
    305 	/*
    306 	 * Free any resources we've allocated during the failed attach
    307 	 * attempt.  Do this in reverse order and fall through.
    308 	 */
    309 fail_6:
    310 	for (i = 0; i < NRCV; i++) {
    311 		if (sc->sc_rxmbuf[i] != NULL) {
    312 			bus_dmamap_unload(sc->sc_dmat, sc->sc_xmtmap[i]);
    313 			m_freem(sc->sc_rxmbuf[i]);
    314 		}
    315 	}
    316 fail_5:
    317 	for (i = 0; i < NRCV; i++) {
    318 		if (sc->sc_xmtmap[i] != NULL)
    319 			bus_dmamap_destroy(sc->sc_dmat, sc->sc_xmtmap[i]);
    320 	}
    321 fail_4:
    322 	for (i = 0; i < NXMT; i++) {
    323 		if (sc->sc_rcvmap[i] != NULL)
    324 			bus_dmamap_destroy(sc->sc_dmat, sc->sc_rcvmap[i]);
    325 	}
    326 	bus_dmamem_unmap(sc->sc_dmat, (caddr_t)sc->sc_dedata,
    327 	    sizeof(struct de_cdata));
    328 fail_1:
    329 	bus_dmamem_free(sc->sc_dmat, &seg, rseg);
    330 fail_0:
    331 	return;
    332 }
    333 
    334 /*
    335  * Reset of interface after UNIBUS reset.
    336  */
    337 void
    338 dereset(struct device *dev)
    339 {
    340 	struct de_softc *sc = (void *)dev;
    341 
    342 	sc->sc_if.if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
    343 	sc->sc_flags &= ~DSF_RUNNING;
    344 	DE_WCSR(DE_PCSR0, PCSR0_RSET);
    345 	(void)dewait(sc, "reset");
    346 	deinit(sc);
    347 }
    348 
    349 /*
    350  * Initialization of interface; clear recorded pending
    351  * operations, and reinitialize UNIBUS usage.
    352  */
    353 void
    354 deinit(struct de_softc *sc)
    355 {
    356 	struct de_cdata *dc;
    357 	int s, i;
    358 
    359 	if (sc->sc_flags & DSF_RUNNING)
    360 		return;
    361 	/*
    362 	 * Tell the DEUNA about our PCB
    363 	 */
    364 	DE_WCSR(DE_PCSR2, LOWORD(sc->sc_pdedata));
    365 	DE_WCSR(DE_PCSR3, HIWORD(sc->sc_pdedata));
    366 	DE_WLOW(0);		/* reset INTE */
    367 	DELAY(500);
    368 	DE_WLOW(CMD_GETPCBB);
    369 	(void)dewait(sc, "pcbb");
    370 
    371 	dc = sc->sc_dedata;
    372 	/* set the transmit and receive ring header addresses */
    373 	dc->dc_pcbb.pcbb0 = FC_WTRING;
    374 	dc->dc_pcbb.pcbb2 = LOWORD(&sc->sc_pdedata->dc_udbbuf);
    375 	dc->dc_pcbb.pcbb2 = HIWORD(&sc->sc_pdedata->dc_udbbuf);
    376 
    377 	dc->dc_udbbuf.b_tdrbl = LOWORD(&sc->sc_pdedata->dc_xrent[0]);
    378 	dc->dc_udbbuf.b_tdrbh = HIWORD(&sc->sc_pdedata->dc_xrent[0]);
    379 	dc->dc_udbbuf.b_telen = sizeof (struct de_ring) / sizeof(u_int16_t);
    380 	dc->dc_udbbuf.b_trlen = NXMT;
    381 	dc->dc_udbbuf.b_rdrbl = LOWORD(&sc->sc_pdedata->dc_rrent[0]);
    382 	dc->dc_udbbuf.b_rdrbh = HIWORD(&sc->sc_pdedata->dc_rrent[0]);
    383 	dc->dc_udbbuf.b_relen = sizeof (struct de_ring) / sizeof(u_int16_t);
    384 	dc->dc_udbbuf.b_rrlen = NRCV;
    385 
    386 	DE_WLOW(CMD_GETCMD);
    387 	(void)dewait(sc, "wtring");
    388 
    389 	/* initialize the mode - enable hardware padding */
    390 	dc->dc_pcbb.pcbb0 = FC_WTMODE;
    391 	/* let hardware do padding - set MTCH bit on broadcast */
    392 	dc->dc_pcbb.pcbb2 = MOD_TPAD|MOD_HDX;
    393 	DE_WLOW(CMD_GETCMD);
    394 	(void)dewait(sc, "wtmode");
    395 
    396 	/* set up the receive and transmit ring entries */
    397 	for (i = 0; i < NXMT; i++) {
    398 		if (sc->sc_txmbuf[i]) {
    399 			bus_dmamap_unload(sc->sc_dmat, sc->sc_xmtmap[i]);
    400 			m_freem(sc->sc_txmbuf[i]);
    401 			sc->sc_txmbuf[i] = 0;
    402 		}
    403 		dc->dc_xrent[i].r_flags = 0;
    404 	}
    405 	for (i = 0; i < NRCV; i++)
    406 		dc->dc_rrent[i].r_flags = RFLG_OWN;
    407 	sc->sc_nexttx = sc->sc_inq = sc->sc_lastack = sc->sc_nextrx = 0;
    408 
    409 	/* start up the board (rah rah) */
    410 	s = splnet();
    411 	sc->sc_if.if_flags |= IFF_RUNNING;
    412 	DE_WLOW(PCSR0_INTE);
    413 	destart(&sc->sc_if);		/* queue output packets */
    414 	sc->sc_flags |= DSF_RUNNING;		/* need before de_setaddr */
    415 	if (sc->sc_flags & DSF_SETADDR)
    416 		de_setaddr(LLADDR(sc->sc_if.if_sadl), sc);
    417 	DE_WLOW(CMD_START|PCSR0_INTE);
    418 	splx(s);
    419 }
    420 
    421 /*
    422  * Setup output on interface.
    423  * Get another datagram to send off of the interface queue,
    424  * and map it to the interface before starting the output.
    425  * Must be called from ipl >= our interrupt level.
    426  */
    427 void
    428 destart(struct ifnet *ifp)
    429 {
    430 	struct de_softc *sc = ifp->if_softc;
    431 	bus_dmamap_t map;
    432 	struct de_ring *rp = 0;
    433 	struct mbuf *m, *m0;
    434 	int idx, i, err, seg, s;
    435 
    436 	/*
    437 	 * the following test is necessary, since
    438 	 * the code is not reentrant and we have
    439 	 * multiple transmission buffers.
    440 	 */
    441 	if (ifp->if_flags & IFF_OACTIVE)
    442 		return;
    443 	s = splimp();
    444 	while (sc->sc_inq < (NXMT - 1)) {
    445 		idx = sc->sc_nexttx;
    446 		IF_DEQUEUE(&ifp->if_snd, m);
    447 		if (m == 0)
    448 			goto out;
    449 		/*
    450 		 * Count number of mbufs in chain.
    451 		 * Always do DMA directly from mbufs.
    452 		 */
    453 		for (m0 = m, i = 0; m0; m0 = m0->m_next)
    454 			if (m0->m_len)
    455 				i++;
    456 		if (i >= NFRAGS) { /* XXX pullup */
    457 			panic("destart");
    458 		}
    459 
    460 		if ((i + sc->sc_inq) >= (NXMT - 1)) {
    461 			IF_PREPEND(&sc->sc_if.if_snd, m);
    462 			ifp->if_flags |= IFF_OACTIVE;
    463 			goto out;
    464 		}
    465 #if NBPFILTER > 0
    466 		if (ifp->if_bpf)
    467 			bpf_mtap(ifp->if_bpf, m);
    468 #endif
    469 		/*
    470 		 * m now points to a mbuf chain that can be loaded.
    471 		 * Loop around and set it.
    472 		 */
    473 		err = bus_dmamap_load_mbuf(sc->sc_dmat, sc->sc_xmtmap[idx],
    474 		    m, BUS_DMA_NOWAIT);
    475 		if (err) /* Can't fail here */
    476 			panic("destart: load_mbuf failed, err %d", err);
    477 
    478 		sc->sc_txmbuf[idx] = m;
    479 		map = sc->sc_xmtmap[idx];
    480 		for (seg = 0; seg < map->dm_nsegs; seg++) {
    481 			rp = &sc->sc_dedata->dc_xrent[idx];
    482 
    483 			rp->r_flags = seg == 0 ? XFLG_STP : 0;
    484 			rp->r_slen = map->dm_segs[seg].ds_len;
    485 			rp->r_segbl = LOWORD(map->dm_segs[seg].ds_addr);
    486 			rp->r_segbh = HIWORD(map->dm_segs[seg].ds_addr);
    487 
    488 			if (++idx == NXMT)
    489 				idx = 0;
    490 			sc->sc_inq++;
    491 		}
    492 		rp->r_flags |= XFLG_ENP|XFLG_OWN;
    493 		sc->sc_nexttx = idx;
    494 	}
    495 	if (sc->sc_inq == (NXMT - 1))
    496 		ifp->if_flags |= IFF_OACTIVE;
    497 
    498 out:	if (sc->sc_inq)
    499 		ifp->if_timer = 5; /* If transmit logic dies */
    500 
    501 	if (sc->sc_flags & DSF_RUNNING)
    502 		DE_WLOW(PCSR0_INTE|CMD_PDMD);
    503 	splx(s);
    504 }
    505 
    506 /*
    507  * Command done interrupt.
    508  */
    509 void
    510 deintr(void *arg)
    511 {
    512 	struct de_softc *sc = arg;
    513 	short csr0;
    514 
    515 	/* save flags right away - clear out interrupt bits */
    516 	csr0 = DE_RCSR(DE_PCSR0);
    517 	DE_WHIGH(csr0 >> 8);
    518 
    519 	if (csr0 & PCSR0_RXI)
    520 		derecv(sc);
    521 
    522 	if (csr0 & PCSR0_TXI)
    523 		dexmit(sc);
    524 
    525 #ifdef notyet
    526 	Handle error interrupts
    527 #endif
    528 	destart(&sc->sc_if);
    529 }
    530 
    531 void
    532 dexmit(struct de_softc *sc)
    533 {
    534 	struct ifnet *ifp = &sc->sc_if;
    535 	struct de_ring *rp;
    536 	int midx;
    537 
    538 	/*
    539 	 * Poll transmit ring and check status.
    540 	 * Then free buffer space and check for
    541 	 * more transmit requests.
    542 	 */
    543 	midx = -1;
    544 	rp = &sc->sc_dedata->dc_xrent[sc->sc_lastack];
    545 	while ((rp->r_flags & XFLG_OWN) == 0) {
    546 		if (sc->sc_txmbuf[sc->sc_lastack])
    547 			midx = sc->sc_lastack;
    548 		if (rp->r_flags & XFLG_ENP) {
    549 			if (midx >= 0)
    550 				m_freem(sc->sc_txmbuf[midx]);
    551 			ifp->if_opackets++;
    552 		}
    553 		if (rp->r_flags & (XFLG_ERRS|XFLG_MTCH|XFLG_ONE|XFLG_MORE)) {
    554 			if (rp->r_flags & XFLG_ERRS) {
    555 				ifp->if_oerrors++;
    556 			} else if (rp->r_flags & XFLG_ONE) {
    557 				ifp->if_collisions++;
    558 			} else if (rp->r_flags & XFLG_MORE) {
    559 				ifp->if_collisions += 3;
    560 			}
    561 			/* else if (rp->r_flags & XFLG_MTCH)
    562 			 * Matches ourself, but why care?
    563 			 * Let upper layer deal with this.
    564 			 */
    565 		}
    566 		if (++sc->sc_lastack == NXMT)
    567 			sc->sc_lastack = 0;
    568 		sc->sc_inq--;
    569 		rp = &sc->sc_dedata->dc_xrent[sc->sc_lastack];
    570 	}
    571 	ifp->if_flags &= ~IFF_OACTIVE;
    572 	if (sc->sc_inq == 0)
    573 		ifp->if_timer = 0;
    574 }
    575 
    576 /*
    577  * Ethernet interface receiver interface.
    578  * If input error just drop packet.
    579  * Otherwise purge input buffered data path and examine
    580  * packet to determine type.  If can't determine length
    581  * from type, then have to drop packet.	 Othewise decapsulate
    582  * packet based on type and pass to type specific higher-level
    583  * input routine.
    584  */
    585 void
    586 derecv(struct de_softc *sc)
    587 {
    588 	struct ifnet *ifp = &sc->sc_if;
    589 	struct ether_header *eh;
    590 	struct de_ring *rp;
    591 	struct mbuf *m;
    592 	int len;
    593 
    594 	rp = &sc->sc_dedata->dc_rrent[sc->sc_nextrx];
    595 	while ((rp->r_flags & RFLG_OWN) == 0) {
    596 		ifp->if_ipackets++;
    597 		len = (rp->r_lenerr&RERR_MLEN) - 4; /* don't forget checksum! */
    598 		/* check for errors */
    599 		if ((rp->r_flags & (RFLG_ERRS|RFLG_FRAM|RFLG_OFLO|RFLG_CRC)) ||
    600 		    (rp->r_flags&(RFLG_STP|RFLG_ENP)) != (RFLG_STP|RFLG_ENP) ||
    601 		    (rp->r_lenerr & (RERR_BUFL|RERR_UBTO|RERR_NCHN)) ||
    602 		    len < ETHERMIN || len > ETHERMTU) {
    603 			ifp->if_ierrors++;
    604 			goto next;
    605 		}
    606 		m = sc->sc_rxmbuf[sc->sc_nextrx];
    607 		de_add_rxbuf(sc, sc->sc_nextrx);
    608 		m->m_pkthdr.rcvif = ifp;
    609 		m->m_pkthdr.len = m->m_len = len;
    610 		eh = mtod(m, struct ether_header *);
    611 #if NBPFILTER > 0
    612 		if (ifp->if_bpf) {
    613 			bpf_mtap(ifp->if_bpf, m);
    614 			if ((ifp->if_flags & IFF_PROMISC) != 0 &&
    615 			    bcmp(LLADDR(ifp->if_sadl), eh->ether_dhost,
    616 			    ETHER_ADDR_LEN) != 0 &&
    617 			    ((eh->ether_dhost[0] & 1) == 0)) {
    618 				m_freem(m);
    619 				goto next;
    620 			}
    621 		}
    622 #endif
    623 		(*ifp->if_input)(ifp, m);
    624 
    625 		/* hang the receive buffer again */
    626 next:		rp->r_lenerr = 0;
    627 		rp->r_flags = RFLG_OWN;
    628 
    629 		/* check next receive buffer */
    630 		if (++sc->sc_nextrx == NRCV)
    631 			sc->sc_nextrx = 0;
    632 		rp = &sc->sc_dedata->dc_rrent[sc->sc_nextrx];
    633 	}
    634 }
    635 
    636 /*
    637  * Add a receive buffer to the indicated descriptor.
    638  */
    639 int
    640 de_add_rxbuf(sc, i)
    641 	struct de_softc *sc;
    642 	int i;
    643 {
    644 	struct mbuf *m;
    645 	struct de_ring *rp;
    646 	vaddr_t addr;
    647 	int error;
    648 
    649 	MGETHDR(m, M_DONTWAIT, MT_DATA);
    650 	if (m == NULL)
    651 		return (ENOBUFS);
    652 
    653 	MCLGET(m, M_DONTWAIT);
    654 	if ((m->m_flags & M_EXT) == 0) {
    655 		m_freem(m);
    656 		return (ENOBUFS);
    657 	}
    658 
    659 	if (sc->sc_rxmbuf[i] != NULL)
    660 		bus_dmamap_unload(sc->sc_dmat, sc->sc_rcvmap[i]);
    661 
    662 	error = bus_dmamap_load(sc->sc_dmat, sc->sc_rcvmap[i],
    663 	    m->m_ext.ext_buf, m->m_ext.ext_size, NULL, BUS_DMA_NOWAIT);
    664 	if (error)
    665 		panic("%s: can't load rx DMA map %d, error = %d\n",
    666 		    sc->sc_dev.dv_xname, i, error);
    667 	sc->sc_rxmbuf[i] = m;
    668 
    669 	bus_dmamap_sync(sc->sc_dmat, sc->sc_rcvmap[i], 0,
    670 	    sc->sc_rcvmap[i]->dm_mapsize, BUS_DMASYNC_PREREAD);
    671 
    672 	/*
    673 	 * We know that the mbuf cluster is page aligned. Also, be sure
    674 	 * that the IP header will be longword aligned.
    675 	 */
    676 	m->m_data += 2;
    677 	addr = sc->sc_rcvmap[i]->dm_segs[0].ds_addr + 2;
    678 	rp = &sc->sc_dedata->dc_rrent[i];
    679 	rp->r_lenerr = 0;
    680 	rp->r_segbl = LOWORD(addr);
    681 	rp->r_segbh = HIWORD(addr);
    682 	rp->r_slen = m->m_ext.ext_size - 2;
    683 	rp->r_flags = RFLG_OWN;
    684 
    685 	return (0);
    686 }
    687 
    688 
    689 /*
    690  * Process an ioctl request.
    691  */
    692 int
    693 deioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
    694 {
    695 	struct ifaddr *ifa = (struct ifaddr *)data;
    696 	struct de_softc *sc = ifp->if_softc;
    697 	int s = splnet(), error = 0;
    698 
    699 	switch (cmd) {
    700 
    701 	case SIOCSIFADDR:
    702 		ifp->if_flags |= IFF_UP;
    703 		deinit(sc);
    704 
    705 		switch (ifa->ifa_addr->sa_family) {
    706 #ifdef INET
    707 		case AF_INET:
    708 			arp_ifinit(ifp, ifa);
    709 			break;
    710 #endif
    711 #ifdef NS
    712 		case AF_NS:
    713 		    {
    714 			register struct ns_addr *ina = &(IA_SNS(ifa)->sns_addr);
    715 
    716 			if (ns_nullhost(*ina))
    717 				ina->x_host =
    718 				    *(union ns_host *)LLADDR(ifp->if_sadl);
    719 			else
    720 				de_setaddr(ina->x_host.c_host, ds);
    721 			break;
    722 		    }
    723 #endif
    724 		}
    725 		break;
    726 
    727 	case SIOCSIFFLAGS:
    728 		if ((ifp->if_flags & IFF_UP) == 0 &&
    729 		    sc->sc_flags & DSF_RUNNING) {
    730 			DE_WLOW(0);
    731 			DELAY(5000);
    732 			DE_WLOW(PCSR0_RSET);
    733 			sc->sc_flags &= ~DSF_RUNNING;
    734 			ifp->if_flags &= ~IFF_OACTIVE;
    735 		} else if (ifp->if_flags & IFF_UP &&
    736 		    (sc->sc_flags & DSF_RUNNING) == 0)
    737 			deinit(sc);
    738 		break;
    739 
    740 	default:
    741 		error = EINVAL;
    742 	}
    743 	splx(s);
    744 	return (error);
    745 }
    746 
    747 /*
    748  * set ethernet address for unit
    749  */
    750 void
    751 de_setaddr(u_char *physaddr, struct de_softc *sc)
    752 {
    753 
    754 	if (! (sc->sc_flags & DSF_RUNNING))
    755 		return;
    756 
    757 	bcopy((caddr_t) physaddr, (caddr_t) &sc->sc_dedata->dc_pcbb.pcbb2, 6);
    758 	sc->sc_dedata->dc_pcbb.pcbb0 = FC_WTPHYAD;
    759 	DE_WLOW(PCSR0_INTE|CMD_GETCMD);
    760 	if (dewait(sc, "address change") == 0) {
    761 		sc->sc_flags |= DSF_SETADDR;
    762 		bcopy((caddr_t) physaddr, LLADDR(sc->sc_if.if_sadl), 6);
    763 	}
    764 }
    765 
    766 /*
    767  * Await completion of the named function
    768  * and check for errors.
    769  */
    770 int
    771 dewait(struct de_softc *sc, char *fn)
    772 {
    773 	int csr0;
    774 
    775 	while ((DE_RCSR(DE_PCSR0) & PCSR0_INTR) == 0)
    776 		;
    777 	csr0 = DE_RCSR(DE_PCSR0);
    778 	DE_WHIGH(csr0 >> 8);
    779 	if (csr0 & PCSR0_PCEI) {
    780 		char bits[64];
    781 
    782 		printf("%s: %s failed, csr0=%s ", sc->sc_dev.dv_xname, fn,
    783 		    bitmask_snprintf(csr0, PCSR0_BITS, bits, sizeof(bits)));
    784 		printf("csr1=%s\n", bitmask_snprintf(DE_RCSR(DE_PCSR1),
    785 		    PCSR1_BITS, bits, sizeof(bits)));
    786 	}
    787 	return (csr0 & PCSR0_PCEI);
    788 }
    789 
    790 int
    791 dematch(struct device *parent, struct cfdata *cf, void *aux)
    792 {
    793 	struct uba_attach_args *ua = aux;
    794 	struct de_softc ssc;
    795 	struct de_softc *sc = &ssc;
    796 	int i;
    797 
    798 	sc->sc_iot = ua->ua_iot;
    799 	sc->sc_ioh = ua->ua_ioh;
    800 	/*
    801 	 * Make sure self-test is finished before we screw with the board.
    802 	 * Self-test on a DELUA can take 15 seconds (argh).
    803 	 */
    804 	for (i = 0;
    805 	    (i < 160) &&
    806 	    (DE_RCSR(DE_PCSR0) & PCSR0_FATI) == 0 &&
    807 	    (DE_RCSR(DE_PCSR1) & PCSR1_STMASK) == STAT_RESET;
    808 	    ++i)
    809 		DELAY(50000);
    810 	if (((DE_RCSR(DE_PCSR0) & PCSR0_FATI) != 0) ||
    811 	    (((DE_RCSR(DE_PCSR1) & PCSR1_STMASK) != STAT_READY) &&
    812 	    ((DE_RCSR(DE_PCSR1) & PCSR1_STMASK) != STAT_RUN)))
    813 		return(0);
    814 
    815 	DE_WCSR(DE_PCSR0, 0);
    816 	DELAY(5000);
    817 	DE_WCSR(DE_PCSR0, PCSR0_RSET);
    818 	while ((DE_RCSR(DE_PCSR0) & PCSR0_INTR) == 0)
    819 		;
    820 	/* make board interrupt by executing a GETPCBB command */
    821 	DE_WCSR(DE_PCSR0, PCSR0_INTE);
    822 	DE_WCSR(DE_PCSR2, 0);
    823 	DE_WCSR(DE_PCSR3, 0);
    824 	DE_WCSR(DE_PCSR0, PCSR0_INTE|CMD_GETPCBB);
    825 	DELAY(50000);
    826 
    827 	return 1;
    828 }
    829