Home | History | Annotate | Line # | Download | only in qbus
if_de.c revision 1.14.6.1
      1  1.14.6.1    skrll /*	$NetBSD: if_de.c,v 1.14.6.1 2004/08/03 10:50:28 skrll Exp $	*/
      2       1.5    ragge 
      3       1.1    ragge /*
      4       1.1    ragge  * Copyright (c) 1982, 1986, 1989 Regents of the University of California.
      5  1.14.6.1    skrll  * All rights reserved.
      6  1.14.6.1    skrll  *
      7  1.14.6.1    skrll  *
      8  1.14.6.1    skrll  * Redistribution and use in source and binary forms, with or without
      9  1.14.6.1    skrll  * modification, are permitted provided that the following conditions
     10  1.14.6.1    skrll  * are met:
     11  1.14.6.1    skrll  * 1. Redistributions of source code must retain the above copyright
     12  1.14.6.1    skrll  *    notice, this list of conditions and the following disclaimer.
     13  1.14.6.1    skrll  * 2. Redistributions in binary form must reproduce the above copyright
     14  1.14.6.1    skrll  *    notice, this list of conditions and the following disclaimer in the
     15  1.14.6.1    skrll  *    documentation and/or other materials provided with the distribution.
     16  1.14.6.1    skrll  * 3. Neither the name of the University nor the names of its contributors
     17  1.14.6.1    skrll  *    may be used to endorse or promote products derived from this software
     18  1.14.6.1    skrll  *    without specific prior written permission.
     19  1.14.6.1    skrll  *
     20  1.14.6.1    skrll  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     21  1.14.6.1    skrll  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     22  1.14.6.1    skrll  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     23  1.14.6.1    skrll  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     24  1.14.6.1    skrll  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     25  1.14.6.1    skrll  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     26  1.14.6.1    skrll  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     27  1.14.6.1    skrll  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     28  1.14.6.1    skrll  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     29  1.14.6.1    skrll  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     30  1.14.6.1    skrll  * SUCH DAMAGE.
     31  1.14.6.1    skrll  *
     32  1.14.6.1    skrll  *	@(#)if_de.c	7.12 (Berkeley) 12/16/90
     33  1.14.6.1    skrll  */
     34  1.14.6.1    skrll 
     35  1.14.6.1    skrll /*
     36       1.1    ragge  * Copyright (c) 2000 Ludd, University of Lule}, Sweden.
     37       1.1    ragge  * All rights reserved.
     38       1.1    ragge  *
     39       1.1    ragge  *
     40       1.1    ragge  * Redistribution and use in source and binary forms, with or without
     41       1.1    ragge  * modification, are permitted provided that the following conditions
     42       1.1    ragge  * are met:
     43       1.1    ragge  * 1. Redistributions of source code must retain the above copyright
     44       1.1    ragge  *    notice, this list of conditions and the following disclaimer.
     45       1.1    ragge  * 2. Redistributions in binary form must reproduce the above copyright
     46       1.1    ragge  *    notice, this list of conditions and the following disclaimer in the
     47       1.1    ragge  *    documentation and/or other materials provided with the distribution.
     48       1.1    ragge  * 3. All advertising materials mentioning features or use of this software
     49       1.1    ragge  *    must display the following acknowledgement:
     50       1.1    ragge  *	This product includes software developed by the University of
     51       1.1    ragge  *	California, Berkeley and its contributors.
     52       1.1    ragge  * 4. Neither the name of the University nor the names of its contributors
     53       1.1    ragge  *    may be used to endorse or promote products derived from this software
     54       1.1    ragge  *    without specific prior written permission.
     55       1.1    ragge  *
     56       1.1    ragge  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     57       1.1    ragge  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     58       1.1    ragge  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     59       1.1    ragge  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     60       1.1    ragge  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     61       1.1    ragge  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     62       1.1    ragge  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     63       1.1    ragge  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     64       1.1    ragge  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     65       1.1    ragge  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     66       1.1    ragge  * SUCH DAMAGE.
     67       1.1    ragge  *
     68       1.1    ragge  *	@(#)if_de.c	7.12 (Berkeley) 12/16/90
     69       1.1    ragge  */
     70       1.1    ragge 
     71       1.1    ragge /*
     72       1.1    ragge  * DEC DEUNA interface
     73       1.1    ragge  *
     74       1.1    ragge  *	Lou Salkind
     75       1.1    ragge  *	New York University
     76       1.1    ragge  *
     77       1.2    ragge  *	Rewritten by Ragge 30 April 2000 to match new world.
     78       1.1    ragge  *
     79       1.1    ragge  * TODO:
     80       1.1    ragge  *	timeout routine (get statistics)
     81       1.1    ragge  */
     82      1.11    lukem 
     83      1.11    lukem #include <sys/cdefs.h>
     84  1.14.6.1    skrll __KERNEL_RCSID(0, "$NetBSD: if_de.c,v 1.14.6.1 2004/08/03 10:50:28 skrll Exp $");
     85       1.1    ragge 
     86       1.1    ragge #include "opt_inet.h"
     87       1.2    ragge #include "bpfilter.h"
     88       1.1    ragge 
     89       1.1    ragge #include <sys/param.h>
     90       1.1    ragge #include <sys/systm.h>
     91       1.1    ragge #include <sys/mbuf.h>
     92       1.1    ragge #include <sys/buf.h>
     93       1.1    ragge #include <sys/protosw.h>
     94       1.1    ragge #include <sys/socket.h>
     95       1.1    ragge #include <sys/ioctl.h>
     96       1.1    ragge #include <sys/errno.h>
     97       1.1    ragge #include <sys/syslog.h>
     98       1.1    ragge #include <sys/device.h>
     99       1.1    ragge 
    100       1.1    ragge #include <net/if.h>
    101       1.1    ragge #include <net/if_ether.h>
    102       1.1    ragge #include <net/if_dl.h>
    103       1.1    ragge 
    104       1.1    ragge #ifdef INET
    105       1.1    ragge #include <netinet/in.h>
    106       1.1    ragge #include <netinet/if_inarp.h>
    107       1.1    ragge #endif
    108       1.1    ragge 
    109       1.2    ragge #if NBPFILTER > 0
    110       1.2    ragge #include <net/bpf.h>
    111       1.2    ragge #include <net/bpfdesc.h>
    112       1.1    ragge #endif
    113       1.1    ragge 
    114       1.1    ragge #include <machine/bus.h>
    115       1.1    ragge 
    116       1.1    ragge #include <dev/qbus/ubavar.h>
    117       1.1    ragge #include <dev/qbus/if_dereg.h>
    118      1.10    ragge #include <dev/qbus/if_uba.h>
    119       1.1    ragge 
    120       1.1    ragge #include "ioconf.h"
    121       1.1    ragge 
    122       1.1    ragge /*
    123       1.1    ragge  * Be careful with transmit/receive buffers, each entry steals 4 map
    124       1.1    ragge  * registers, and there is only 496 on one unibus...
    125       1.1    ragge  */
    126      1.10    ragge #define NRCV	7	/* number of receive buffers (must be > 1) */
    127      1.10    ragge #define NXMT	3	/* number of transmit buffers */
    128       1.1    ragge 
    129       1.1    ragge /*
    130       1.1    ragge  * Structure containing the elements that must be in DMA-safe memory.
    131       1.1    ragge  */
    132       1.1    ragge struct	de_cdata {
    133       1.1    ragge 	/* the following structures are always mapped in */
    134       1.1    ragge 	struct	de_pcbb dc_pcbb;	/* port control block */
    135       1.1    ragge 	struct	de_ring dc_xrent[NXMT]; /* transmit ring entrys */
    136       1.1    ragge 	struct	de_ring dc_rrent[NRCV]; /* receive ring entrys */
    137       1.1    ragge 	struct	de_udbbuf dc_udbbuf;	/* UNIBUS data buffer */
    138       1.1    ragge 	/* end mapped area */
    139       1.1    ragge };
    140       1.1    ragge 
    141       1.1    ragge /*
    142       1.1    ragge  * Ethernet software status per interface.
    143       1.1    ragge  *
    144       1.1    ragge  * Each interface is referenced by a network interface structure,
    145       1.1    ragge  * ds_if, which the routing code uses to locate the interface.
    146       1.1    ragge  * This structure contains the output queue for the interface, its address, ...
    147       1.1    ragge  * We also have, for each interface, a UBA interface structure, which
    148       1.1    ragge  * contains information about the UNIBUS resources held by the interface:
    149       1.1    ragge  * map registers, buffered data paths, etc.  Information is cached in this
    150       1.1    ragge  * structure for use by the if_uba.c routines in running the interface
    151       1.1    ragge  * efficiently.
    152       1.1    ragge  */
    153       1.1    ragge struct	de_softc {
    154       1.1    ragge 	struct	device sc_dev;		/* Configuration common part */
    155       1.3     matt 	struct	evcnt sc_intrcnt;	/* Interrupt counting */
    156       1.1    ragge 	struct	ethercom sc_ec;		/* Ethernet common part */
    157       1.1    ragge #define sc_if	sc_ec.ec_if		/* network-visible interface */
    158       1.1    ragge 	bus_space_tag_t sc_iot;
    159       1.1    ragge 	bus_addr_t sc_ioh;
    160       1.1    ragge 	bus_dma_tag_t sc_dmat;
    161      1.10    ragge 	int	sc_flags;
    162      1.10    ragge #define	DSF_MAPPED	1
    163       1.9    ragge 	struct ubinfo sc_ui;
    164       1.1    ragge 	struct de_cdata *sc_dedata;	/* Control structure */
    165       1.1    ragge 	struct de_cdata *sc_pdedata;	/* Bus-mapped control structure */
    166      1.10    ragge 	struct	ifubinfo sc_ifuba;      /* UNIBUS resources */
    167      1.10    ragge 	struct	ifrw sc_ifr[NRCV];      /* UNIBUS receive buffer maps */
    168      1.10    ragge 	struct	ifxmt sc_ifw[NXMT];     /* UNIBUS receive buffer maps */
    169      1.10    ragge 
    170       1.5    ragge 	int	sc_xindex;		/* UNA index into transmit chain */
    171       1.5    ragge 	int	sc_rindex;		/* UNA index into receive chain */
    172       1.5    ragge 	int	sc_xfree;		/* index for next transmit buffer */
    173       1.5    ragge 	int	sc_nxmit;		/* # of transmits in progress */
    174       1.2    ragge 	void *sc_sh;			/* shutdownhook cookie */
    175       1.1    ragge };
    176       1.1    ragge 
    177       1.1    ragge static	int dematch(struct device *, struct cfdata *, void *);
    178       1.1    ragge static	void deattach(struct device *, struct device *, void *);
    179       1.2    ragge static	void dewait(struct de_softc *, char *);
    180      1.10    ragge static	int deinit(struct ifnet *);
    181       1.1    ragge static	int deioctl(struct ifnet *, u_long, caddr_t);
    182       1.1    ragge static	void dereset(struct device *);
    183      1.10    ragge static	void destop(struct ifnet *, int);
    184       1.1    ragge static	void destart(struct ifnet *);
    185       1.1    ragge static	void derecv(struct de_softc *);
    186       1.1    ragge static	void deintr(void *);
    187       1.2    ragge static	void deshutdown(void *);
    188       1.1    ragge 
    189      1.13  thorpej CFATTACH_DECL(de, sizeof(struct de_softc),
    190      1.14  thorpej     dematch, deattach, NULL, NULL);
    191       1.1    ragge 
    192       1.1    ragge #define DE_WCSR(csr, val) \
    193       1.1    ragge 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, csr, val)
    194       1.1    ragge #define DE_WLOW(val) \
    195       1.1    ragge 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, DE_PCSR0, val)
    196       1.1    ragge #define DE_WHIGH(val) \
    197       1.1    ragge 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, DE_PCSR0 + 1, val)
    198       1.1    ragge #define DE_RCSR(csr) \
    199       1.1    ragge 	bus_space_read_2(sc->sc_iot, sc->sc_ioh, csr)
    200       1.1    ragge 
    201       1.1    ragge #define LOWORD(x)	((int)(x) & 0xffff)
    202       1.1    ragge #define HIWORD(x)	(((int)(x) >> 16) & 0x3)
    203       1.1    ragge /*
    204       1.1    ragge  * Interface exists: make available by filling in network interface
    205       1.1    ragge  * record.  System will initialize the interface when it is ready
    206       1.1    ragge  * to accept packets.  We get the ethernet address here.
    207       1.1    ragge  */
    208       1.1    ragge void
    209       1.1    ragge deattach(struct device *parent, struct device *self, void *aux)
    210       1.1    ragge {
    211       1.1    ragge 	struct uba_attach_args *ua = aux;
    212       1.1    ragge 	struct de_softc *sc = (struct de_softc *)self;
    213       1.1    ragge 	struct ifnet *ifp = &sc->sc_if;
    214       1.1    ragge 	u_int8_t myaddr[ETHER_ADDR_LEN];
    215      1.10    ragge 	int csr1, error;
    216       1.1    ragge 	char *c;
    217       1.1    ragge 
    218       1.1    ragge 	sc->sc_iot = ua->ua_iot;
    219       1.1    ragge 	sc->sc_ioh = ua->ua_ioh;
    220       1.1    ragge 	sc->sc_dmat = ua->ua_dmat;
    221       1.1    ragge 
    222       1.1    ragge 	/*
    223       1.1    ragge 	 * What kind of a board is this?
    224       1.1    ragge 	 * The error bits 4-6 in pcsr1 are a device id as long as
    225       1.1    ragge 	 * the high byte is zero.
    226       1.1    ragge 	 */
    227       1.1    ragge 	csr1 = DE_RCSR(DE_PCSR1);
    228       1.1    ragge 	if (csr1 & 0xff60)
    229       1.1    ragge 		c = "broken";
    230       1.1    ragge 	else if (csr1 & 0x10)
    231       1.1    ragge 		c = "delua";
    232       1.1    ragge 	else
    233       1.1    ragge 		c = "deuna";
    234       1.1    ragge 
    235       1.1    ragge 	/*
    236       1.1    ragge 	 * Reset the board and temporarily map
    237       1.1    ragge 	 * the pcbb buffer onto the Unibus.
    238       1.1    ragge 	 */
    239       1.1    ragge 	DE_WCSR(DE_PCSR0, 0);		/* reset INTE */
    240       1.1    ragge 	DELAY(100);
    241       1.1    ragge 	DE_WCSR(DE_PCSR0, PCSR0_RSET);
    242       1.2    ragge 	dewait(sc, "reset");
    243       1.1    ragge 
    244       1.9    ragge 	sc->sc_ui.ui_size = sizeof(struct de_cdata);
    245      1.10    ragge 	if ((error = ubmemalloc((struct uba_softc *)parent, &sc->sc_ui, 0)))
    246      1.10    ragge 		return printf(": failed ubmemalloc(), error = %d\n", error);
    247       1.9    ragge 	sc->sc_dedata = (struct de_cdata *)sc->sc_ui.ui_vaddr;
    248       1.2    ragge 
    249       1.1    ragge 	/*
    250       1.1    ragge 	 * Tell the DEUNA about our PCB
    251       1.1    ragge 	 */
    252      1.10    ragge 	DE_WCSR(DE_PCSR2, LOWORD(sc->sc_ui.ui_baddr));
    253      1.10    ragge 	DE_WCSR(DE_PCSR3, HIWORD(sc->sc_ui.ui_baddr));
    254       1.1    ragge 	DE_WLOW(CMD_GETPCBB);
    255       1.2    ragge 	dewait(sc, "pcbb");
    256       1.1    ragge 
    257       1.1    ragge 	sc->sc_dedata->dc_pcbb.pcbb0 = FC_RDPHYAD;
    258       1.1    ragge 	DE_WLOW(CMD_GETCMD);
    259       1.2    ragge 	dewait(sc, "read addr ");
    260       1.1    ragge 
    261       1.1    ragge 	bcopy((caddr_t)&sc->sc_dedata->dc_pcbb.pcbb2, myaddr, sizeof (myaddr));
    262       1.2    ragge 	printf("\n%s: %s, hardware address %s\n", sc->sc_dev.dv_xname, c,
    263       1.1    ragge 		ether_sprintf(myaddr));
    264       1.1    ragge 
    265       1.5    ragge 	uba_intr_establish(ua->ua_icookie, ua->ua_cvec, deintr, sc,
    266       1.5    ragge 	    &sc->sc_intrcnt);
    267       1.1    ragge 	uba_reset_establish(dereset, &sc->sc_dev);
    268       1.4     matt 	evcnt_attach_dynamic(&sc->sc_intrcnt, EVCNT_TYPE_INTR, ua->ua_evcnt,
    269       1.5    ragge 	    sc->sc_dev.dv_xname, "intr");
    270       1.1    ragge 
    271       1.1    ragge 	strcpy(ifp->if_xname, sc->sc_dev.dv_xname);
    272       1.1    ragge 	ifp->if_softc = sc;
    273       1.5    ragge 	ifp->if_flags = IFF_BROADCAST|IFF_SIMPLEX|IFF_MULTICAST|IFF_ALLMULTI;
    274       1.1    ragge 	ifp->if_ioctl = deioctl;
    275       1.1    ragge 	ifp->if_start = destart;
    276      1.10    ragge 	ifp->if_init = deinit;
    277      1.10    ragge 	ifp->if_stop = destop;
    278       1.8  thorpej 	IFQ_SET_READY(&ifp->if_snd);
    279       1.8  thorpej 
    280       1.1    ragge 	if_attach(ifp);
    281       1.1    ragge 	ether_ifattach(ifp, myaddr);
    282      1.10    ragge 	ubmemfree((struct uba_softc *)parent, &sc->sc_ui);
    283       1.7  thorpej 
    284       1.2    ragge 	sc->sc_sh = shutdownhook_establish(deshutdown, sc);
    285      1.10    ragge }
    286       1.1    ragge 
    287      1.10    ragge void
    288      1.10    ragge destop(struct ifnet *ifp, int a)
    289      1.10    ragge {
    290      1.10    ragge 	struct de_softc *sc = ifp->if_softc;
    291      1.10    ragge 
    292      1.10    ragge 	DE_WLOW(0);
    293      1.10    ragge 	DELAY(5000);
    294      1.10    ragge 	DE_WLOW(PCSR0_RSET);
    295       1.1    ragge }
    296       1.1    ragge 
    297      1.10    ragge 
    298       1.1    ragge /*
    299       1.1    ragge  * Reset of interface after UNIBUS reset.
    300       1.1    ragge  */
    301       1.1    ragge void
    302       1.1    ragge dereset(struct device *dev)
    303       1.1    ragge {
    304       1.1    ragge 	struct de_softc *sc = (void *)dev;
    305       1.1    ragge 
    306       1.1    ragge 	sc->sc_if.if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
    307      1.10    ragge 	sc->sc_flags &= ~DSF_MAPPED;
    308       1.5    ragge 	sc->sc_pdedata = NULL;	/* All mappings lost */
    309       1.1    ragge 	DE_WCSR(DE_PCSR0, PCSR0_RSET);
    310       1.2    ragge 	dewait(sc, "reset");
    311      1.10    ragge 	deinit(&sc->sc_if);
    312       1.1    ragge }
    313       1.1    ragge 
    314       1.1    ragge /*
    315       1.1    ragge  * Initialization of interface; clear recorded pending
    316       1.1    ragge  * operations, and reinitialize UNIBUS usage.
    317       1.1    ragge  */
    318      1.10    ragge int
    319      1.10    ragge deinit(struct ifnet *ifp)
    320       1.1    ragge {
    321      1.10    ragge 	struct de_softc *sc = ifp->if_softc;
    322       1.2    ragge 	struct de_cdata *dc, *pdc;
    323      1.10    ragge 	struct ifrw *ifrw;
    324      1.10    ragge 	struct ifxmt *ifxp;
    325      1.10    ragge 	struct de_ring *rp;
    326      1.10    ragge 	int s, error;
    327      1.10    ragge 
    328      1.10    ragge 	if (ifp->if_flags & IFF_RUNNING)
    329      1.10    ragge 		return 0;
    330      1.10    ragge 	if ((sc->sc_flags & DSF_MAPPED) == 0) {
    331      1.10    ragge 		if (if_ubaminit(&sc->sc_ifuba, (void *)sc->sc_dev.dv_parent,
    332      1.10    ragge 		    MCLBYTES, sc->sc_ifr, NRCV, sc->sc_ifw, NXMT)) {
    333      1.10    ragge 			printf("%s: can't initialize\n", sc->sc_dev.dv_xname);
    334      1.10    ragge 			ifp->if_flags &= ~IFF_UP;
    335      1.10    ragge 			return 0;
    336      1.10    ragge 		}
    337      1.10    ragge 		sc->sc_ui.ui_size = sizeof(struct de_cdata);
    338      1.10    ragge 		if ((error = ubmemalloc((void *)sc->sc_dev.dv_parent,
    339      1.10    ragge 		    &sc->sc_ui, 0))) {
    340      1.10    ragge 			printf(": unable to ubmemalloc(), error = %d\n", error);
    341      1.10    ragge 			return 0;
    342      1.10    ragge 		}
    343      1.10    ragge 		sc->sc_pdedata = (struct de_cdata *)sc->sc_ui.ui_baddr;
    344      1.10    ragge 		sc->sc_dedata = (struct de_cdata *)sc->sc_ui.ui_vaddr;
    345      1.10    ragge 		sc->sc_flags |= DSF_MAPPED;
    346      1.10    ragge 	}
    347       1.1    ragge 
    348       1.1    ragge 	/*
    349       1.1    ragge 	 * Tell the DEUNA about our PCB
    350       1.1    ragge 	 */
    351       1.1    ragge 	DE_WCSR(DE_PCSR2, LOWORD(sc->sc_pdedata));
    352       1.1    ragge 	DE_WCSR(DE_PCSR3, HIWORD(sc->sc_pdedata));
    353       1.1    ragge 	DE_WLOW(0);		/* reset INTE */
    354       1.1    ragge 	DELAY(500);
    355       1.1    ragge 	DE_WLOW(CMD_GETPCBB);
    356       1.2    ragge 	dewait(sc, "pcbb");
    357       1.1    ragge 
    358       1.1    ragge 	dc = sc->sc_dedata;
    359       1.2    ragge 	pdc = sc->sc_pdedata;
    360       1.1    ragge 	/* set the transmit and receive ring header addresses */
    361       1.1    ragge 	dc->dc_pcbb.pcbb0 = FC_WTRING;
    362       1.2    ragge 	dc->dc_pcbb.pcbb2 = LOWORD(&pdc->dc_udbbuf);
    363       1.2    ragge 	dc->dc_pcbb.pcbb4 = HIWORD(&pdc->dc_udbbuf);
    364       1.1    ragge 
    365       1.2    ragge 	dc->dc_udbbuf.b_tdrbl = LOWORD(&pdc->dc_xrent[0]);
    366       1.2    ragge 	dc->dc_udbbuf.b_tdrbh = HIWORD(&pdc->dc_xrent[0]);
    367       1.1    ragge 	dc->dc_udbbuf.b_telen = sizeof (struct de_ring) / sizeof(u_int16_t);
    368       1.1    ragge 	dc->dc_udbbuf.b_trlen = NXMT;
    369       1.2    ragge 	dc->dc_udbbuf.b_rdrbl = LOWORD(&pdc->dc_rrent[0]);
    370       1.2    ragge 	dc->dc_udbbuf.b_rdrbh = HIWORD(&pdc->dc_rrent[0]);
    371       1.1    ragge 	dc->dc_udbbuf.b_relen = sizeof (struct de_ring) / sizeof(u_int16_t);
    372       1.1    ragge 	dc->dc_udbbuf.b_rrlen = NRCV;
    373       1.1    ragge 
    374       1.1    ragge 	DE_WLOW(CMD_GETCMD);
    375       1.2    ragge 	dewait(sc, "wtring");
    376       1.1    ragge 
    377       1.5    ragge 	sc->sc_dedata->dc_pcbb.pcbb0 = FC_WTMODE;
    378       1.5    ragge 	sc->sc_dedata->dc_pcbb.pcbb2 = MOD_TPAD|MOD_HDX|MOD_DRDC|MOD_ENAL;
    379       1.5    ragge 	DE_WLOW(CMD_GETCMD);
    380       1.5    ragge 	dewait(sc, "wtmode");
    381       1.1    ragge 
    382       1.5    ragge 	/* set up the receive and transmit ring entries */
    383      1.10    ragge 	ifxp = &sc->sc_ifw[0];
    384      1.10    ragge 	for (rp = &dc->dc_xrent[0]; rp < &dc->dc_xrent[NXMT]; rp++) {
    385      1.10    ragge 		rp->r_segbl = LOWORD(ifxp->ifw_info);
    386      1.10    ragge 		rp->r_segbh = HIWORD(ifxp->ifw_info);
    387      1.10    ragge 		rp->r_flags = 0;
    388      1.10    ragge 		ifxp++;
    389      1.10    ragge 	}
    390      1.10    ragge 	ifrw = &sc->sc_ifr[0];
    391      1.10    ragge 	for (rp = &dc->dc_rrent[0]; rp < &dc->dc_rrent[NRCV]; rp++) {
    392      1.10    ragge 		rp->r_slen = MCLBYTES - 2;
    393      1.10    ragge 		rp->r_segbl = LOWORD(ifrw->ifrw_info);
    394      1.10    ragge 		rp->r_segbh = HIWORD(ifrw->ifrw_info);
    395      1.10    ragge 		rp->r_flags = RFLG_OWN;
    396      1.10    ragge 		ifrw++;
    397      1.10    ragge 	}
    398       1.1    ragge 
    399       1.1    ragge 	/* start up the board (rah rah) */
    400       1.1    ragge 	s = splnet();
    401       1.5    ragge 	sc->sc_rindex = sc->sc_xindex = sc->sc_xfree = sc->sc_nxmit = 0;
    402       1.1    ragge 	sc->sc_if.if_flags |= IFF_RUNNING;
    403       1.5    ragge 	DE_WLOW(PCSR0_INTE);			/* avoid interlock */
    404       1.5    ragge 	destart(&sc->sc_if);		/* queue output packets */
    405       1.1    ragge 	DE_WLOW(CMD_START|PCSR0_INTE);
    406       1.1    ragge 	splx(s);
    407      1.10    ragge 	return 0;
    408       1.1    ragge }
    409       1.1    ragge 
    410       1.1    ragge /*
    411       1.1    ragge  * Setup output on interface.
    412       1.1    ragge  * Get another datagram to send off of the interface queue,
    413       1.1    ragge  * and map it to the interface before starting the output.
    414       1.1    ragge  * Must be called from ipl >= our interrupt level.
    415       1.1    ragge  */
    416       1.1    ragge void
    417       1.1    ragge destart(struct ifnet *ifp)
    418       1.1    ragge {
    419       1.1    ragge 	struct de_softc *sc = ifp->if_softc;
    420       1.2    ragge 	struct de_cdata *dc;
    421      1.10    ragge 	struct de_ring *rp;
    422       1.2    ragge 	struct mbuf *m;
    423      1.10    ragge 	int nxmit, len;
    424       1.1    ragge 
    425       1.1    ragge 	/*
    426       1.1    ragge 	 * the following test is necessary, since
    427       1.1    ragge 	 * the code is not reentrant and we have
    428       1.1    ragge 	 * multiple transmission buffers.
    429       1.1    ragge 	 */
    430       1.5    ragge 	if (sc->sc_if.if_flags & IFF_OACTIVE)
    431       1.1    ragge 		return;
    432       1.2    ragge 	dc = sc->sc_dedata;
    433       1.5    ragge 	for (nxmit = sc->sc_nxmit; nxmit < NXMT; nxmit++) {
    434       1.8  thorpej 		IFQ_DEQUEUE(&ifp->if_snd, m);
    435       1.1    ragge 		if (m == 0)
    436       1.5    ragge 			break;
    437      1.10    ragge 
    438      1.10    ragge 		rp = &dc->dc_xrent[sc->sc_xfree];
    439       1.5    ragge 		if (rp->r_flags & XFLG_OWN)
    440       1.5    ragge 			panic("deuna xmit in progress");
    441       1.1    ragge #if NBPFILTER > 0
    442       1.1    ragge 		if (ifp->if_bpf)
    443       1.1    ragge 			bpf_mtap(ifp->if_bpf, m);
    444       1.1    ragge #endif
    445       1.5    ragge 
    446      1.10    ragge 		len = if_ubaput(&sc->sc_ifuba, &sc->sc_ifw[sc->sc_xfree], m);
    447      1.10    ragge 		rp->r_slen = len;
    448      1.10    ragge 		rp->r_tdrerr = 0;
    449      1.10    ragge 		rp->r_flags = XFLG_STP|XFLG_ENP|XFLG_OWN;
    450      1.10    ragge 
    451       1.5    ragge 		sc->sc_xfree++;
    452       1.5    ragge 		if (sc->sc_xfree == NXMT)
    453       1.5    ragge 			sc->sc_xfree = 0;
    454       1.5    ragge 	}
    455      1.10    ragge 	if (sc->sc_nxmit != nxmit) {
    456       1.5    ragge 		sc->sc_nxmit = nxmit;
    457       1.5    ragge 		if (ifp->if_flags & IFF_RUNNING)
    458       1.5    ragge 			DE_WLOW(PCSR0_INTE|CMD_PDMD);
    459       1.1    ragge 	}
    460       1.1    ragge }
    461       1.1    ragge 
    462       1.1    ragge /*
    463       1.1    ragge  * Command done interrupt.
    464       1.1    ragge  */
    465       1.1    ragge void
    466       1.1    ragge deintr(void *arg)
    467       1.1    ragge {
    468      1.10    ragge 	struct ifxmt *ifxp;
    469       1.5    ragge 	struct de_cdata *dc;
    470       1.1    ragge 	struct de_softc *sc = arg;
    471       1.5    ragge 	struct de_ring *rp;
    472       1.5    ragge 	short csr0;
    473       1.1    ragge 
    474       1.1    ragge 	/* save flags right away - clear out interrupt bits */
    475       1.1    ragge 	csr0 = DE_RCSR(DE_PCSR0);
    476       1.1    ragge 	DE_WHIGH(csr0 >> 8);
    477       1.1    ragge 
    478       1.1    ragge 
    479       1.5    ragge 	sc->sc_if.if_flags |= IFF_OACTIVE;	/* prevent entering destart */
    480       1.5    ragge 	/*
    481       1.5    ragge 	 * if receive, put receive buffer on mbuf
    482       1.5    ragge 	 * and hang the request again
    483       1.5    ragge 	 */
    484       1.5    ragge 	derecv(sc);
    485       1.1    ragge 
    486       1.1    ragge 	/*
    487       1.1    ragge 	 * Poll transmit ring and check status.
    488       1.5    ragge 	 * Be careful about loopback requests.
    489       1.1    ragge 	 * Then free buffer space and check for
    490       1.1    ragge 	 * more transmit requests.
    491       1.1    ragge 	 */
    492       1.5    ragge 	dc = sc->sc_dedata;
    493       1.5    ragge 	for ( ; sc->sc_nxmit > 0; sc->sc_nxmit--) {
    494       1.5    ragge 		rp = &dc->dc_xrent[sc->sc_xindex];
    495       1.5    ragge 		if (rp->r_flags & XFLG_OWN)
    496       1.2    ragge 			break;
    497      1.10    ragge 
    498       1.5    ragge 		sc->sc_if.if_opackets++;
    499      1.10    ragge 		ifxp = &sc->sc_ifw[sc->sc_xindex];
    500       1.5    ragge 		/* check for unusual conditions */
    501       1.1    ragge 		if (rp->r_flags & (XFLG_ERRS|XFLG_MTCH|XFLG_ONE|XFLG_MORE)) {
    502       1.1    ragge 			if (rp->r_flags & XFLG_ERRS) {
    503       1.5    ragge 				/* output error */
    504       1.5    ragge 				sc->sc_if.if_oerrors++;
    505       1.1    ragge 			} else if (rp->r_flags & XFLG_ONE) {
    506       1.5    ragge 				/* one collision */
    507       1.5    ragge 				sc->sc_if.if_collisions++;
    508       1.1    ragge 			} else if (rp->r_flags & XFLG_MORE) {
    509       1.5    ragge 				/* more than one collision */
    510       1.5    ragge 				sc->sc_if.if_collisions += 2;	/* guess */
    511       1.1    ragge 			}
    512       1.1    ragge 		}
    513      1.10    ragge 		if_ubaend(&sc->sc_ifuba, ifxp);
    514       1.5    ragge 		/* check if next transmit buffer also finished */
    515       1.5    ragge 		sc->sc_xindex++;
    516       1.5    ragge 		if (sc->sc_xindex == NXMT)
    517       1.5    ragge 			sc->sc_xindex = 0;
    518       1.5    ragge 	}
    519       1.5    ragge 	sc->sc_if.if_flags &= ~IFF_OACTIVE;
    520       1.5    ragge 	destart(&sc->sc_if);
    521       1.5    ragge 
    522       1.5    ragge 	if (csr0 & PCSR0_RCBI) {
    523       1.5    ragge 		DE_WLOW(PCSR0_INTE|CMD_PDMD);
    524       1.5    ragge 	}
    525       1.1    ragge }
    526       1.1    ragge 
    527       1.1    ragge /*
    528       1.1    ragge  * Ethernet interface receiver interface.
    529       1.1    ragge  * If input error just drop packet.
    530       1.1    ragge  * Otherwise purge input buffered data path and examine
    531       1.1    ragge  * packet to determine type.  If can't determine length
    532       1.1    ragge  * from type, then have to drop packet.	 Othewise decapsulate
    533       1.1    ragge  * packet based on type and pass to type specific higher-level
    534       1.1    ragge  * input routine.
    535       1.1    ragge  */
    536       1.1    ragge void
    537       1.1    ragge derecv(struct de_softc *sc)
    538       1.1    ragge {
    539       1.1    ragge 	struct ifnet *ifp = &sc->sc_if;
    540       1.1    ragge 	struct de_ring *rp;
    541       1.5    ragge 	struct de_cdata *dc;
    542       1.1    ragge 	struct mbuf *m;
    543       1.1    ragge 	int len;
    544       1.1    ragge 
    545       1.5    ragge 	dc = sc->sc_dedata;
    546       1.5    ragge 	rp = &dc->dc_rrent[sc->sc_rindex];
    547       1.1    ragge 	while ((rp->r_flags & RFLG_OWN) == 0) {
    548       1.5    ragge 		sc->sc_if.if_ipackets++;
    549       1.5    ragge 		len = (rp->r_lenerr&RERR_MLEN) - ETHER_CRC_LEN;
    550       1.1    ragge 		/* check for errors */
    551       1.1    ragge 		if ((rp->r_flags & (RFLG_ERRS|RFLG_FRAM|RFLG_OFLO|RFLG_CRC)) ||
    552       1.2    ragge 		    (rp->r_lenerr & (RERR_BUFL|RERR_UBTO))) {
    553       1.5    ragge 			sc->sc_if.if_ierrors++;
    554       1.1    ragge 			goto next;
    555       1.1    ragge 		}
    556      1.10    ragge 		m = if_ubaget(&sc->sc_ifuba, &sc->sc_ifr[sc->sc_rindex],
    557      1.10    ragge 		    ifp, len);
    558      1.10    ragge 		if (m == 0) {
    559      1.10    ragge 			sc->sc_if.if_ierrors++;
    560      1.10    ragge 			goto next;
    561      1.10    ragge 		}
    562       1.1    ragge #if NBPFILTER > 0
    563       1.6  thorpej 		if (ifp->if_bpf)
    564       1.1    ragge 			bpf_mtap(ifp->if_bpf, m);
    565       1.1    ragge #endif
    566       1.5    ragge 
    567      1.10    ragge 		(*ifp->if_input)(ifp, m);
    568       1.1    ragge 
    569       1.1    ragge 		/* hang the receive buffer again */
    570       1.1    ragge next:		rp->r_lenerr = 0;
    571       1.1    ragge 		rp->r_flags = RFLG_OWN;
    572       1.1    ragge 
    573       1.1    ragge 		/* check next receive buffer */
    574       1.5    ragge 		sc->sc_rindex++;
    575       1.5    ragge 		if (sc->sc_rindex == NRCV)
    576       1.5    ragge 			sc->sc_rindex = 0;
    577       1.5    ragge 		rp = &dc->dc_rrent[sc->sc_rindex];
    578       1.1    ragge 	}
    579       1.1    ragge }
    580       1.1    ragge 
    581       1.1    ragge /*
    582       1.1    ragge  * Process an ioctl request.
    583       1.1    ragge  */
    584       1.1    ragge int
    585       1.1    ragge deioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
    586       1.1    ragge {
    587      1.10    ragge 	int s, error = 0;
    588       1.1    ragge 
    589      1.10    ragge 	s = splnet();
    590       1.1    ragge 
    591      1.10    ragge 	error = ether_ioctl(ifp, cmd, data);
    592      1.10    ragge 	if (error == ENETRESET)
    593      1.10    ragge 		error = 0;
    594       1.2    ragge 
    595       1.1    ragge 	splx(s);
    596       1.1    ragge 	return (error);
    597       1.1    ragge }
    598       1.1    ragge 
    599       1.1    ragge /*
    600       1.1    ragge  * Await completion of the named function
    601       1.1    ragge  * and check for errors.
    602       1.1    ragge  */
    603       1.2    ragge void
    604       1.1    ragge dewait(struct de_softc *sc, char *fn)
    605       1.1    ragge {
    606       1.5    ragge 	int csr0;
    607       1.1    ragge 
    608       1.1    ragge 	while ((DE_RCSR(DE_PCSR0) & PCSR0_INTR) == 0)
    609       1.1    ragge 		;
    610       1.1    ragge 	csr0 = DE_RCSR(DE_PCSR0);
    611       1.1    ragge 	DE_WHIGH(csr0 >> 8);
    612       1.1    ragge 	if (csr0 & PCSR0_PCEI) {
    613       1.1    ragge 		char bits[64];
    614       1.1    ragge 		printf("%s: %s failed, csr0=%s ", sc->sc_dev.dv_xname, fn,
    615       1.1    ragge 		    bitmask_snprintf(csr0, PCSR0_BITS, bits, sizeof(bits)));
    616       1.1    ragge 		printf("csr1=%s\n", bitmask_snprintf(DE_RCSR(DE_PCSR1),
    617       1.1    ragge 		    PCSR1_BITS, bits, sizeof(bits)));
    618       1.1    ragge 	}
    619       1.1    ragge }
    620       1.1    ragge 
    621       1.1    ragge int
    622       1.1    ragge dematch(struct device *parent, struct cfdata *cf, void *aux)
    623       1.1    ragge {
    624       1.1    ragge 	struct uba_attach_args *ua = aux;
    625       1.1    ragge 	struct de_softc ssc;
    626       1.1    ragge 	struct de_softc *sc = &ssc;
    627       1.1    ragge 	int i;
    628       1.1    ragge 
    629       1.1    ragge 	sc->sc_iot = ua->ua_iot;
    630       1.1    ragge 	sc->sc_ioh = ua->ua_ioh;
    631       1.1    ragge 	/*
    632       1.1    ragge 	 * Make sure self-test is finished before we screw with the board.
    633       1.1    ragge 	 * Self-test on a DELUA can take 15 seconds (argh).
    634       1.1    ragge 	 */
    635       1.1    ragge 	for (i = 0;
    636       1.1    ragge 	    (i < 160) &&
    637       1.1    ragge 	    (DE_RCSR(DE_PCSR0) & PCSR0_FATI) == 0 &&
    638       1.1    ragge 	    (DE_RCSR(DE_PCSR1) & PCSR1_STMASK) == STAT_RESET;
    639       1.1    ragge 	    ++i)
    640       1.1    ragge 		DELAY(50000);
    641       1.1    ragge 	if (((DE_RCSR(DE_PCSR0) & PCSR0_FATI) != 0) ||
    642       1.1    ragge 	    (((DE_RCSR(DE_PCSR1) & PCSR1_STMASK) != STAT_READY) &&
    643       1.1    ragge 	    ((DE_RCSR(DE_PCSR1) & PCSR1_STMASK) != STAT_RUN)))
    644       1.1    ragge 		return(0);
    645       1.1    ragge 
    646       1.1    ragge 	DE_WCSR(DE_PCSR0, 0);
    647       1.1    ragge 	DELAY(5000);
    648       1.1    ragge 	DE_WCSR(DE_PCSR0, PCSR0_RSET);
    649       1.1    ragge 	while ((DE_RCSR(DE_PCSR0) & PCSR0_INTR) == 0)
    650       1.1    ragge 		;
    651       1.1    ragge 	/* make board interrupt by executing a GETPCBB command */
    652       1.1    ragge 	DE_WCSR(DE_PCSR0, PCSR0_INTE);
    653       1.1    ragge 	DE_WCSR(DE_PCSR2, 0);
    654       1.1    ragge 	DE_WCSR(DE_PCSR3, 0);
    655       1.1    ragge 	DE_WCSR(DE_PCSR0, PCSR0_INTE|CMD_GETPCBB);
    656       1.1    ragge 	DELAY(50000);
    657       1.1    ragge 
    658       1.1    ragge 	return 1;
    659       1.1    ragge }
    660       1.2    ragge 
    661       1.2    ragge void
    662       1.2    ragge deshutdown(void *arg)
    663       1.2    ragge {
    664       1.2    ragge 	struct de_softc *sc = arg;
    665       1.2    ragge 
    666       1.5    ragge 	DE_WCSR(DE_PCSR0, 0);
    667       1.5    ragge 	DELAY(1000);
    668       1.2    ragge 	DE_WCSR(DE_PCSR0, PCSR0_RSET);
    669       1.2    ragge 	dewait(sc, "shutdown");
    670       1.2    ragge }
    671