Home | History | Annotate | Line # | Download | only in qbus
if_de.c revision 1.7
      1 /*	$NetBSD: if_de.c,v 1.7 2000/11/15 01:02:19 thorpej 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 30 April 2000 to match new world.
     47  *
     48  * TODO:
     49  *	timeout routine (get statistics)
     50  */
     51 
     52 #include "opt_inet.h"
     53 #include "bpfilter.h"
     54 
     55 #include <sys/param.h>
     56 #include <sys/systm.h>
     57 #include <sys/mbuf.h>
     58 #include <sys/buf.h>
     59 #include <sys/protosw.h>
     60 #include <sys/socket.h>
     61 #include <sys/ioctl.h>
     62 #include <sys/errno.h>
     63 #include <sys/syslog.h>
     64 #include <sys/device.h>
     65 
     66 #include <net/if.h>
     67 #include <net/if_ether.h>
     68 #include <net/if_dl.h>
     69 
     70 #ifdef INET
     71 #include <netinet/in.h>
     72 #include <netinet/if_inarp.h>
     73 #endif
     74 
     75 #if NBPFILTER > 0
     76 #include <net/bpf.h>
     77 #include <net/bpfdesc.h>
     78 #endif
     79 
     80 #include <machine/bus.h>
     81 
     82 #include <dev/qbus/ubavar.h>
     83 #include <dev/qbus/if_dereg.h>
     84 
     85 #include "ioconf.h"
     86 
     87 /*
     88  * Be careful with transmit/receive buffers, each entry steals 4 map
     89  * registers, and there is only 496 on one unibus...
     90  */
     91 #define NRCV	10	/* number of receive buffers (must be > 1) */
     92 #define NXMT	10	/* number of transmit buffers */
     93 
     94 /*
     95  * Structure containing the elements that must be in DMA-safe memory.
     96  */
     97 struct	de_cdata {
     98 	/* the following structures are always mapped in */
     99 	struct	de_pcbb dc_pcbb;	/* port control block */
    100 	struct	de_ring dc_xrent[NXMT]; /* transmit ring entrys */
    101 	struct	de_ring dc_rrent[NRCV]; /* receive ring entrys */
    102 	struct	de_udbbuf dc_udbbuf;	/* UNIBUS data buffer */
    103 	char	dc_xbuf[NXMT][ETHER_MAX_LEN];
    104 	/* end mapped area */
    105 };
    106 
    107 /*
    108  * Ethernet software status per interface.
    109  *
    110  * Each interface is referenced by a network interface structure,
    111  * ds_if, which the routing code uses to locate the interface.
    112  * This structure contains the output queue for the interface, its address, ...
    113  * We also have, for each interface, a UBA interface structure, which
    114  * contains information about the UNIBUS resources held by the interface:
    115  * map registers, buffered data paths, etc.  Information is cached in this
    116  * structure for use by the if_uba.c routines in running the interface
    117  * efficiently.
    118  */
    119 struct	de_softc {
    120 	struct	device sc_dev;		/* Configuration common part */
    121 	struct	evcnt sc_intrcnt;	/* Interrupt counting */
    122 	struct	ethercom sc_ec;		/* Ethernet common part */
    123 #define sc_if	sc_ec.ec_if		/* network-visible interface */
    124 	bus_space_tag_t sc_iot;
    125 	bus_addr_t sc_ioh;
    126 	bus_dma_tag_t sc_dmat;
    127 	bus_dmamap_t sc_cmap;
    128 	struct de_cdata *sc_dedata;	/* Control structure */
    129 	struct de_cdata *sc_pdedata;	/* Bus-mapped control structure */
    130 	bus_dmamap_t sc_rcvmap[NRCV];	/* unibus receive maps */
    131 	struct mbuf *sc_rxmbuf[NRCV];
    132 	int	sc_xindex;		/* UNA index into transmit chain */
    133 	int	sc_rindex;		/* UNA index into receive chain */
    134 	int	sc_xfree;		/* index for next transmit buffer */
    135 	int	sc_nxmit;		/* # of transmits in progress */
    136 	void *sc_sh;			/* shutdownhook cookie */
    137 };
    138 
    139 static	int dematch(struct device *, struct cfdata *, void *);
    140 static	void deattach(struct device *, struct device *, void *);
    141 static	void dewait(struct de_softc *, char *);
    142 static	void deinit(struct de_softc *);
    143 static	int deioctl(struct ifnet *, u_long, caddr_t);
    144 static	void dereset(struct device *);
    145 static	void destart(struct ifnet *);
    146 static	void derecv(struct de_softc *);
    147 static	void deintr(void *);
    148 static	int de_add_rxbuf(struct de_softc *, int);
    149 static	void deshutdown(void *);
    150 
    151 struct	cfattach de_ca = {
    152 	sizeof(struct de_softc), dematch, deattach
    153 };
    154 
    155 #define DE_WCSR(csr, val) \
    156 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, csr, val)
    157 #define DE_WLOW(val) \
    158 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, DE_PCSR0, val)
    159 #define DE_WHIGH(val) \
    160 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, DE_PCSR0 + 1, val)
    161 #define DE_RCSR(csr) \
    162 	bus_space_read_2(sc->sc_iot, sc->sc_ioh, csr)
    163 
    164 #define LOWORD(x)	((int)(x) & 0xffff)
    165 #define HIWORD(x)	(((int)(x) >> 16) & 0x3)
    166 /*
    167  * Interface exists: make available by filling in network interface
    168  * record.  System will initialize the interface when it is ready
    169  * to accept packets.  We get the ethernet address here.
    170  */
    171 void
    172 deattach(struct device *parent, struct device *self, void *aux)
    173 {
    174 	struct uba_attach_args *ua = aux;
    175 	struct de_softc *sc = (struct de_softc *)self;
    176 	struct ifnet *ifp = &sc->sc_if;
    177 	u_int8_t myaddr[ETHER_ADDR_LEN];
    178 	int csr1, rseg, error, i;
    179 	bus_dma_segment_t seg;
    180 	char *c;
    181 
    182 	sc->sc_iot = ua->ua_iot;
    183 	sc->sc_ioh = ua->ua_ioh;
    184 	sc->sc_dmat = ua->ua_dmat;
    185 
    186 	/*
    187 	 * What kind of a board is this?
    188 	 * The error bits 4-6 in pcsr1 are a device id as long as
    189 	 * the high byte is zero.
    190 	 */
    191 	csr1 = DE_RCSR(DE_PCSR1);
    192 	if (csr1 & 0xff60)
    193 		c = "broken";
    194 	else if (csr1 & 0x10)
    195 		c = "delua";
    196 	else
    197 		c = "deuna";
    198 
    199 	/*
    200 	 * Reset the board and temporarily map
    201 	 * the pcbb buffer onto the Unibus.
    202 	 */
    203 	DE_WCSR(DE_PCSR0, 0);		/* reset INTE */
    204 	DELAY(100);
    205 	DE_WCSR(DE_PCSR0, PCSR0_RSET);
    206 	dewait(sc, "reset");
    207 
    208 	if ((error = bus_dmamem_alloc(sc->sc_dmat,
    209 	    sizeof(struct de_cdata), NBPG, 0, &seg, 1, &rseg,
    210 	    BUS_DMA_NOWAIT)) != 0) {
    211 		printf(": unable to allocate control data, error = %d\n",
    212 		    error);
    213 		goto fail_0;
    214 	}
    215 	if ((error = bus_dmamem_map(sc->sc_dmat, &seg, rseg,
    216 	    sizeof(struct de_cdata), (caddr_t *)&sc->sc_dedata,
    217 	    BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) != 0) {
    218 		printf(": unable to map control data, error = %d\n", error);
    219 		goto fail_1;
    220 	}
    221 
    222 	if ((error = bus_dmamap_create(sc->sc_dmat, sizeof(struct de_cdata),
    223 	    1, sizeof(struct de_cdata), 0, BUS_DMA_NOWAIT,
    224 	    &sc->sc_cmap)) != 0) {
    225 		printf(": unable to create control data DMA map, error = %d\n",
    226 		    error);
    227 		goto fail_2;
    228 	}
    229 
    230 	if ((error = bus_dmamap_load(sc->sc_dmat, sc->sc_cmap,
    231 	    sc->sc_dedata, sizeof(struct de_cdata), NULL,
    232 	    BUS_DMA_NOWAIT)) != 0) {
    233 		printf(": unable to load control data DMA map, error = %d\n",
    234 		    error);
    235 		goto fail_3;
    236 	}
    237 
    238 	bzero(sc->sc_dedata, sizeof(struct de_cdata));
    239 	sc->sc_pdedata = (struct de_cdata *)sc->sc_cmap->dm_segs[0].ds_addr;
    240 
    241 	/*
    242 	 * Create receive buffer DMA maps.
    243 	 */
    244 	for (i = 0; i < NRCV; i++) {
    245 		if ((error = bus_dmamap_create(sc->sc_dmat, MCLBYTES, 1,
    246 		    MCLBYTES, 0, BUS_DMA_NOWAIT|BUS_DMA_ALLOCNOW,
    247 		    &sc->sc_rcvmap[i]))) {
    248 			printf(": unable to create rx DMA map %d, error = %d\n",
    249 			    i, error);
    250 			goto fail_5;
    251 		}
    252 	}
    253 
    254 	/*
    255 	 * Pre-allocate the receive buffers.
    256 	 */
    257 	for (i = 0; i < NRCV; i++) {
    258 		if ((error = de_add_rxbuf(sc, i)) != 0) {
    259 			printf(": unable to allocate or map rx buffer %d\n,"
    260 			    " error = %d\n", i, error);
    261 			goto fail_6;
    262 		}
    263 	}
    264 
    265 	/*
    266 	 * Tell the DEUNA about our PCB
    267 	 */
    268 	DE_WCSR(DE_PCSR2, LOWORD(sc->sc_pdedata));
    269 	DE_WCSR(DE_PCSR3, HIWORD(sc->sc_pdedata));
    270 	DE_WLOW(CMD_GETPCBB);
    271 	dewait(sc, "pcbb");
    272 
    273 	sc->sc_dedata->dc_pcbb.pcbb0 = FC_RDPHYAD;
    274 	DE_WLOW(CMD_GETCMD);
    275 	dewait(sc, "read addr ");
    276 
    277 	bcopy((caddr_t)&sc->sc_dedata->dc_pcbb.pcbb2, myaddr, sizeof (myaddr));
    278 	printf("\n%s: %s, hardware address %s\n", sc->sc_dev.dv_xname, c,
    279 		ether_sprintf(myaddr));
    280 
    281 	uba_intr_establish(ua->ua_icookie, ua->ua_cvec, deintr, sc,
    282 	    &sc->sc_intrcnt);
    283 	uba_reset_establish(dereset, &sc->sc_dev);
    284 	evcnt_attach_dynamic(&sc->sc_intrcnt, EVCNT_TYPE_INTR, ua->ua_evcnt,
    285 	    sc->sc_dev.dv_xname, "intr");
    286 
    287 	strcpy(ifp->if_xname, sc->sc_dev.dv_xname);
    288 	ifp->if_softc = sc;
    289 	ifp->if_flags = IFF_BROADCAST|IFF_SIMPLEX|IFF_MULTICAST|IFF_ALLMULTI;
    290 	ifp->if_ioctl = deioctl;
    291 	ifp->if_start = destart;
    292 	if_attach(ifp);
    293 	ether_ifattach(ifp, myaddr);
    294 
    295 	sc->sc_sh = shutdownhook_establish(deshutdown, sc);
    296 	return;
    297 
    298 	/*
    299 	 * Free any resources we've allocated during the failed attach
    300 	 * attempt.  Do this in reverse order and fall through.
    301 	 */
    302 fail_6:
    303 	for (i = 0; i < NRCV; i++) {
    304 		if (sc->sc_rxmbuf[i] != NULL) {
    305 			bus_dmamap_unload(sc->sc_dmat, sc->sc_rcvmap[i]);
    306 			m_freem(sc->sc_rxmbuf[i]);
    307 		}
    308 	}
    309 fail_5:
    310 	for (i = 0; i < NRCV; i++) {
    311 		if (sc->sc_rcvmap[i] != NULL)
    312 			bus_dmamap_destroy(sc->sc_dmat, sc->sc_rcvmap[i]);
    313 	}
    314 
    315 fail_3:
    316 	bus_dmamap_destroy(sc->sc_dmat, sc->sc_cmap);
    317 fail_2:
    318 	bus_dmamem_unmap(sc->sc_dmat, (caddr_t)sc->sc_dedata,
    319 	    sizeof(struct de_cdata));
    320 fail_1:
    321 	bus_dmamem_free(sc->sc_dmat, &seg, rseg);
    322 fail_0:
    323 	return;
    324 }
    325 
    326 /*
    327  * Reset of interface after UNIBUS reset.
    328  */
    329 void
    330 dereset(struct device *dev)
    331 {
    332 	struct de_softc *sc = (void *)dev;
    333 
    334 	sc->sc_if.if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
    335 	sc->sc_pdedata = NULL;	/* All mappings lost */
    336 	DE_WCSR(DE_PCSR0, PCSR0_RSET);
    337 	dewait(sc, "reset");
    338 	deinit(sc);
    339 }
    340 
    341 /*
    342  * Initialization of interface; clear recorded pending
    343  * operations, and reinitialize UNIBUS usage.
    344  */
    345 void
    346 deinit(struct de_softc *sc)
    347 {
    348 	struct de_cdata *dc, *pdc;
    349 	int s, i;
    350 
    351 	if (sc->sc_if.if_flags & IFF_RUNNING)
    352 		return;
    353 	/*
    354 	 * Tell the DEUNA about our PCB
    355 	 */
    356 	DE_WCSR(DE_PCSR2, LOWORD(sc->sc_pdedata));
    357 	DE_WCSR(DE_PCSR3, HIWORD(sc->sc_pdedata));
    358 	DE_WLOW(0);		/* reset INTE */
    359 	DELAY(500);
    360 	DE_WLOW(CMD_GETPCBB);
    361 	dewait(sc, "pcbb");
    362 
    363 	dc = sc->sc_dedata;
    364 	pdc = sc->sc_pdedata;
    365 	/* set the transmit and receive ring header addresses */
    366 	dc->dc_pcbb.pcbb0 = FC_WTRING;
    367 	dc->dc_pcbb.pcbb2 = LOWORD(&pdc->dc_udbbuf);
    368 	dc->dc_pcbb.pcbb4 = HIWORD(&pdc->dc_udbbuf);
    369 
    370 	dc->dc_udbbuf.b_tdrbl = LOWORD(&pdc->dc_xrent[0]);
    371 	dc->dc_udbbuf.b_tdrbh = HIWORD(&pdc->dc_xrent[0]);
    372 	dc->dc_udbbuf.b_telen = sizeof (struct de_ring) / sizeof(u_int16_t);
    373 	dc->dc_udbbuf.b_trlen = NXMT;
    374 	dc->dc_udbbuf.b_rdrbl = LOWORD(&pdc->dc_rrent[0]);
    375 	dc->dc_udbbuf.b_rdrbh = HIWORD(&pdc->dc_rrent[0]);
    376 	dc->dc_udbbuf.b_relen = sizeof (struct de_ring) / sizeof(u_int16_t);
    377 	dc->dc_udbbuf.b_rrlen = NRCV;
    378 
    379 	DE_WLOW(CMD_GETCMD);
    380 	dewait(sc, "wtring");
    381 
    382 	sc->sc_dedata->dc_pcbb.pcbb0 = FC_WTMODE;
    383 	sc->sc_dedata->dc_pcbb.pcbb2 = MOD_TPAD|MOD_HDX|MOD_DRDC|MOD_ENAL;
    384 	DE_WLOW(CMD_GETCMD);
    385 	dewait(sc, "wtmode");
    386 
    387 	/* set up the receive and transmit ring entries */
    388 	for (i = 0; i < NXMT; i++) {
    389 		dc->dc_xrent[i].r_flags = 0;
    390 		dc->dc_xrent[i].r_segbl = LOWORD(&pdc->dc_xbuf[i][0]);
    391 		dc->dc_xrent[i].r_segbh = HIWORD(&pdc->dc_xbuf[i][0]);
    392 	}
    393 
    394 	for (i = 0; i < NRCV; i++)
    395 		dc->dc_rrent[i].r_flags = RFLG_OWN;
    396 
    397 	/* start up the board (rah rah) */
    398 	s = splnet();
    399 	sc->sc_rindex = sc->sc_xindex = sc->sc_xfree = sc->sc_nxmit = 0;
    400 	sc->sc_if.if_flags |= IFF_RUNNING;
    401 	DE_WLOW(PCSR0_INTE);			/* avoid interlock */
    402 	destart(&sc->sc_if);		/* queue output packets */
    403 	DE_WLOW(CMD_START|PCSR0_INTE);
    404 	splx(s);
    405 }
    406 
    407 /*
    408  * Setup output on interface.
    409  * Get another datagram to send off of the interface queue,
    410  * and map it to the interface before starting the output.
    411  * Must be called from ipl >= our interrupt level.
    412  */
    413 void
    414 destart(struct ifnet *ifp)
    415 {
    416 	struct de_softc *sc = ifp->if_softc;
    417 	struct de_cdata *dc;
    418 	struct de_ring *rp;
    419 	struct mbuf *m;
    420 	int nxmit;
    421 
    422 	/*
    423 	 * the following test is necessary, since
    424 	 * the code is not reentrant and we have
    425 	 * multiple transmission buffers.
    426 	 */
    427 	if (sc->sc_if.if_flags & IFF_OACTIVE)
    428 		return;
    429 	dc = sc->sc_dedata;
    430 	for (nxmit = sc->sc_nxmit; nxmit < NXMT; nxmit++) {
    431 		IF_DEQUEUE(&sc->sc_if.if_snd, m);
    432 		if (m == 0)
    433 			break;
    434 		rp = &dc->dc_xrent[sc->sc_xfree];
    435 		if (rp->r_flags & XFLG_OWN)
    436 			panic("deuna xmit in progress");
    437 		m_copydata(m, 0, m->m_pkthdr.len, &dc->dc_xbuf[sc->sc_xfree][0]);
    438 		rp->r_slen = m->m_pkthdr.len;
    439 		rp->r_tdrerr = 0;
    440 		rp->r_flags = XFLG_STP|XFLG_ENP|XFLG_OWN;
    441 
    442 #if NBPFILTER > 0
    443 		if (ifp->if_bpf)
    444 			bpf_mtap(ifp->if_bpf, m);
    445 #endif
    446 
    447 		m_freem(m);
    448 		sc->sc_xfree++;
    449 		if (sc->sc_xfree == NXMT)
    450 			sc->sc_xfree = 0;
    451 	}
    452 	if (sc->sc_nxmit != nxmit) {
    453 		sc->sc_nxmit = nxmit;
    454 		if (ifp->if_flags & IFF_RUNNING)
    455 			DE_WLOW(PCSR0_INTE|CMD_PDMD);
    456 	}
    457 }
    458 
    459 /*
    460  * Command done interrupt.
    461  */
    462 void
    463 deintr(void *arg)
    464 {
    465 	struct de_cdata *dc;
    466 	struct de_softc *sc = arg;
    467 	struct de_ring *rp;
    468 	short csr0;
    469 
    470 	/* save flags right away - clear out interrupt bits */
    471 	csr0 = DE_RCSR(DE_PCSR0);
    472 	DE_WHIGH(csr0 >> 8);
    473 
    474 
    475 	sc->sc_if.if_flags |= IFF_OACTIVE;	/* prevent entering destart */
    476 	/*
    477 	 * if receive, put receive buffer on mbuf
    478 	 * and hang the request again
    479 	 */
    480 	derecv(sc);
    481 
    482 	/*
    483 	 * Poll transmit ring and check status.
    484 	 * Be careful about loopback requests.
    485 	 * Then free buffer space and check for
    486 	 * more transmit requests.
    487 	 */
    488 	dc = sc->sc_dedata;
    489 	for ( ; sc->sc_nxmit > 0; sc->sc_nxmit--) {
    490 		rp = &dc->dc_xrent[sc->sc_xindex];
    491 		if (rp->r_flags & XFLG_OWN)
    492 			break;
    493 		sc->sc_if.if_opackets++;
    494 		/* check for unusual conditions */
    495 		if (rp->r_flags & (XFLG_ERRS|XFLG_MTCH|XFLG_ONE|XFLG_MORE)) {
    496 			if (rp->r_flags & XFLG_ERRS) {
    497 				/* output error */
    498 				sc->sc_if.if_oerrors++;
    499 			} else if (rp->r_flags & XFLG_ONE) {
    500 				/* one collision */
    501 				sc->sc_if.if_collisions++;
    502 			} else if (rp->r_flags & XFLG_MORE) {
    503 				/* more than one collision */
    504 				sc->sc_if.if_collisions += 2;	/* guess */
    505 			}
    506 		}
    507 		/* check if next transmit buffer also finished */
    508 		sc->sc_xindex++;
    509 		if (sc->sc_xindex == NXMT)
    510 			sc->sc_xindex = 0;
    511 	}
    512 	sc->sc_if.if_flags &= ~IFF_OACTIVE;
    513 	destart(&sc->sc_if);
    514 
    515 	if (csr0 & PCSR0_RCBI) {
    516 		DE_WLOW(PCSR0_INTE|CMD_PDMD);
    517 	}
    518 }
    519 
    520 /*
    521  * Ethernet interface receiver interface.
    522  * If input error just drop packet.
    523  * Otherwise purge input buffered data path and examine
    524  * packet to determine type.  If can't determine length
    525  * from type, then have to drop packet.	 Othewise decapsulate
    526  * packet based on type and pass to type specific higher-level
    527  * input routine.
    528  */
    529 void
    530 derecv(struct de_softc *sc)
    531 {
    532 	struct ifnet *ifp = &sc->sc_if;
    533 	struct de_ring *rp;
    534 	struct de_cdata *dc;
    535 	struct mbuf *m;
    536 	int len;
    537 
    538 	dc = sc->sc_dedata;
    539 	rp = &dc->dc_rrent[sc->sc_rindex];
    540 	while ((rp->r_flags & RFLG_OWN) == 0) {
    541 		sc->sc_if.if_ipackets++;
    542 		len = (rp->r_lenerr&RERR_MLEN) - ETHER_CRC_LEN;
    543 		/* check for errors */
    544 		if ((rp->r_flags & (RFLG_ERRS|RFLG_FRAM|RFLG_OFLO|RFLG_CRC)) ||
    545 		    (rp->r_lenerr & (RERR_BUFL|RERR_UBTO))) {
    546 			sc->sc_if.if_ierrors++;
    547 			goto next;
    548 		}
    549 		m = sc->sc_rxmbuf[sc->sc_rindex];
    550 #if NBPFILTER > 0
    551 		if (ifp->if_bpf)
    552 			bpf_mtap(ifp->if_bpf, m);
    553 #endif
    554 
    555 		if (de_add_rxbuf(sc, sc->sc_rindex) == 0) {
    556 			m->m_pkthdr.rcvif = ifp;
    557 			m->m_pkthdr.len = m->m_len = len;
    558 			(*ifp->if_input)(ifp, m);
    559 		} else
    560 			sc->sc_if.if_ierrors++;
    561 
    562 		/* hang the receive buffer again */
    563 next:		rp->r_lenerr = 0;
    564 		rp->r_flags = RFLG_OWN;
    565 
    566 		/* check next receive buffer */
    567 		sc->sc_rindex++;
    568 		if (sc->sc_rindex == NRCV)
    569 			sc->sc_rindex = 0;
    570 		rp = &dc->dc_rrent[sc->sc_rindex];
    571 	}
    572 }
    573 
    574 /*
    575  * Add a receive buffer to the indicated descriptor.
    576  */
    577 int
    578 de_add_rxbuf(sc, i)
    579 	struct de_softc *sc;
    580 	int i;
    581 {
    582 	struct mbuf *m;
    583 	struct de_ring *rp;
    584 	vaddr_t addr;
    585 	int error;
    586 
    587 	MGETHDR(m, M_DONTWAIT, MT_DATA);
    588 	if (m == NULL)
    589 		return (ENOBUFS);
    590 
    591 	MCLGET(m, M_DONTWAIT);
    592 	if ((m->m_flags & M_EXT) == 0) {
    593 		m_freem(m);
    594 		return (ENOBUFS);
    595 	}
    596 
    597 	if (sc->sc_rxmbuf[i] != NULL)
    598 		bus_dmamap_unload(sc->sc_dmat, sc->sc_rcvmap[i]);
    599 
    600 	error = bus_dmamap_load(sc->sc_dmat, sc->sc_rcvmap[i],
    601 	    m->m_ext.ext_buf, m->m_ext.ext_size, NULL, BUS_DMA_NOWAIT);
    602 	if (error)
    603 		panic("%s: can't load rx DMA map %d, error = %d\n",
    604 		    sc->sc_dev.dv_xname, i, error);
    605 	sc->sc_rxmbuf[i] = m;
    606 
    607 	bus_dmamap_sync(sc->sc_dmat, sc->sc_rcvmap[i], 0,
    608 	    sc->sc_rcvmap[i]->dm_mapsize, BUS_DMASYNC_PREREAD);
    609 
    610 	/*
    611 	 * We know that the mbuf cluster is page aligned. Also, be sure
    612 	 * that the IP header will be longword aligned.
    613 	 */
    614 	m->m_data += 2;
    615 	addr = sc->sc_rcvmap[i]->dm_segs[0].ds_addr + 2;
    616 	rp = &sc->sc_dedata->dc_rrent[i];
    617 	rp->r_lenerr = 0;
    618 	rp->r_segbl = LOWORD(addr);
    619 	rp->r_segbh = HIWORD(addr);
    620 	rp->r_slen = m->m_ext.ext_size - 2;
    621 	rp->r_flags = RFLG_OWN;
    622 
    623 	return (0);
    624 }
    625 
    626 
    627 /*
    628  * Process an ioctl request.
    629  */
    630 int
    631 deioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
    632 {
    633 	struct ifaddr *ifa = (struct ifaddr *)data;
    634 	struct ifreq *ifr = (struct ifreq *)data;
    635 	struct de_softc *sc = ifp->if_softc;
    636 	int s = splnet(), error = 0;
    637 
    638 	switch (cmd) {
    639 
    640 	case SIOCSIFADDR:
    641 		ifp->if_flags |= IFF_UP;
    642 		switch (ifa->ifa_addr->sa_family) {
    643 #ifdef INET
    644 		case AF_INET:
    645 			deinit(sc);
    646 			arp_ifinit(ifp, ifa);
    647 			break;
    648 #endif
    649 		}
    650 		break;
    651 
    652 	case SIOCSIFFLAGS:
    653 		if ((ifp->if_flags & IFF_UP) == 0 &&
    654 		    (ifp->if_flags & IFF_RUNNING) != 0) {
    655 			/*
    656 			 * If interface is marked down and it is running,
    657 			 * stop it.
    658 			 */
    659 			DE_WLOW(0);
    660 			DELAY(5000);
    661 			DE_WLOW(PCSR0_RSET);
    662 			ifp->if_flags &= ~(IFF_RUNNING|IFF_OACTIVE);
    663 		} else if ((ifp->if_flags & IFF_UP) != 0 &&
    664 			   (ifp->if_flags & IFF_RUNNING) == 0) {
    665 			/*
    666 			 * If interface it marked up and it is stopped, then
    667 			 * start it.
    668 			 */
    669 			deinit(sc);
    670 		} else if ((ifp->if_flags & IFF_UP) != 0) {
    671 			/*
    672 			 * Send a new setup packet to match any new changes.
    673 			 * (Like IFF_PROMISC etc)
    674 			 */
    675 			sc->sc_dedata->dc_pcbb.pcbb0 = FC_WTMODE;
    676 			sc->sc_dedata->dc_pcbb.pcbb2 =
    677 			    MOD_TPAD|MOD_HDX|MOD_DRDC|MOD_ENAL;
    678 			if (ifp->if_flags & IFF_PROMISC)
    679 				sc->sc_dedata->dc_pcbb.pcbb2 |= MOD_PROM;
    680 			DE_WLOW(CMD_GETCMD|PCSR0_INTE);
    681 			dewait(sc, "chgmode");
    682 		}
    683 		break;
    684 
    685 	case SIOCADDMULTI:
    686 	case SIOCDELMULTI:
    687 		/*
    688 		 * Update our multicast list.
    689 		 */
    690 		error = (cmd == SIOCADDMULTI) ?
    691 			ether_addmulti(ifr, &sc->sc_ec):
    692 			ether_delmulti(ifr, &sc->sc_ec);
    693 
    694 		if (error == ENETRESET) {
    695 			/*
    696 			 * Multicast list has changed; set the hardware filter
    697 			 * accordingly.
    698 			 */
    699 			error = 0;
    700 		}
    701 		break;
    702 
    703 	default:
    704 		error = EINVAL;
    705 	}
    706 	splx(s);
    707 	return (error);
    708 }
    709 
    710 /*
    711  * Await completion of the named function
    712  * and check for errors.
    713  */
    714 void
    715 dewait(struct de_softc *sc, char *fn)
    716 {
    717 	int csr0;
    718 
    719 	while ((DE_RCSR(DE_PCSR0) & PCSR0_INTR) == 0)
    720 		;
    721 	csr0 = DE_RCSR(DE_PCSR0);
    722 	DE_WHIGH(csr0 >> 8);
    723 	if (csr0 & PCSR0_PCEI) {
    724 		char bits[64];
    725 		printf("%s: %s failed, csr0=%s ", sc->sc_dev.dv_xname, fn,
    726 		    bitmask_snprintf(csr0, PCSR0_BITS, bits, sizeof(bits)));
    727 		printf("csr1=%s\n", bitmask_snprintf(DE_RCSR(DE_PCSR1),
    728 		    PCSR1_BITS, bits, sizeof(bits)));
    729 	}
    730 }
    731 
    732 int
    733 dematch(struct device *parent, struct cfdata *cf, void *aux)
    734 {
    735 	struct uba_attach_args *ua = aux;
    736 	struct de_softc ssc;
    737 	struct de_softc *sc = &ssc;
    738 	int i;
    739 
    740 	sc->sc_iot = ua->ua_iot;
    741 	sc->sc_ioh = ua->ua_ioh;
    742 	/*
    743 	 * Make sure self-test is finished before we screw with the board.
    744 	 * Self-test on a DELUA can take 15 seconds (argh).
    745 	 */
    746 	for (i = 0;
    747 	    (i < 160) &&
    748 	    (DE_RCSR(DE_PCSR0) & PCSR0_FATI) == 0 &&
    749 	    (DE_RCSR(DE_PCSR1) & PCSR1_STMASK) == STAT_RESET;
    750 	    ++i)
    751 		DELAY(50000);
    752 	if (((DE_RCSR(DE_PCSR0) & PCSR0_FATI) != 0) ||
    753 	    (((DE_RCSR(DE_PCSR1) & PCSR1_STMASK) != STAT_READY) &&
    754 	    ((DE_RCSR(DE_PCSR1) & PCSR1_STMASK) != STAT_RUN)))
    755 		return(0);
    756 
    757 	DE_WCSR(DE_PCSR0, 0);
    758 	DELAY(5000);
    759 	DE_WCSR(DE_PCSR0, PCSR0_RSET);
    760 	while ((DE_RCSR(DE_PCSR0) & PCSR0_INTR) == 0)
    761 		;
    762 	/* make board interrupt by executing a GETPCBB command */
    763 	DE_WCSR(DE_PCSR0, PCSR0_INTE);
    764 	DE_WCSR(DE_PCSR2, 0);
    765 	DE_WCSR(DE_PCSR3, 0);
    766 	DE_WCSR(DE_PCSR0, PCSR0_INTE|CMD_GETPCBB);
    767 	DELAY(50000);
    768 
    769 	return 1;
    770 }
    771 
    772 void
    773 deshutdown(void *arg)
    774 {
    775 	struct de_softc *sc = arg;
    776 
    777 	DE_WCSR(DE_PCSR0, 0);
    778 	DELAY(1000);
    779 	DE_WCSR(DE_PCSR0, PCSR0_RSET);
    780 	dewait(sc, "shutdown");
    781 }
    782