Home | History | Annotate | Line # | Download | only in dev
if_le.c revision 1.14
      1  1.14    gwr /*	$NetBSD: if_le.c,v 1.14 1994/11/23 08:13:53 gwr Exp $	*/
      2  1.12    cgd 
      3  1.10    gwr /*-
      4  1.10    gwr  * Copyright (c) 1982, 1992, 1993
      5  1.10    gwr  *	The Regents of the University of California.  All rights reserved.
      6   1.1  glass  *
      7   1.1  glass  * Redistribution and use in source and binary forms, with or without
      8   1.1  glass  * modification, are permitted provided that the following conditions
      9   1.1  glass  * are met:
     10   1.1  glass  * 1. Redistributions of source code must retain the above copyright
     11   1.1  glass  *    notice, this list of conditions and the following disclaimer.
     12   1.1  glass  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1  glass  *    notice, this list of conditions and the following disclaimer in the
     14   1.1  glass  *    documentation and/or other materials provided with the distribution.
     15   1.1  glass  * 3. All advertising materials mentioning features or use of this software
     16   1.1  glass  *    must display the following acknowledgement:
     17   1.1  glass  *	This product includes software developed by the University of
     18   1.1  glass  *	California, Berkeley and its contributors.
     19   1.1  glass  * 4. Neither the name of the University nor the names of its contributors
     20   1.1  glass  *    may be used to endorse or promote products derived from this software
     21   1.1  glass  *    without specific prior written permission.
     22   1.1  glass  *
     23   1.1  glass  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24   1.1  glass  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25   1.1  glass  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26   1.1  glass  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27   1.1  glass  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28   1.1  glass  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29   1.1  glass  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30   1.1  glass  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31   1.1  glass  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32   1.1  glass  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33   1.1  glass  * SUCH DAMAGE.
     34   1.1  glass  *
     35  1.13    gwr  *	from: Header: if_le.c,v 1.25 93/10/31 04:47:50 leres Locked
     36  1.13    gwr  *	from: @(#)if_le.c	8.2 (Berkeley) 10/30/93
     37   1.1  glass  */
     38   1.1  glass 
     39   1.1  glass #include "bpfilter.h"
     40   1.1  glass 
     41   1.1  glass /*
     42   1.1  glass  * AMD 7990 LANCE
     43   1.1  glass  */
     44   1.8  glass #include <sys/param.h>
     45  1.10    gwr #include <sys/device.h>
     46   1.8  glass #include <sys/systm.h>
     47  1.10    gwr #include <sys/kernel.h>
     48   1.8  glass #include <sys/mbuf.h>
     49   1.8  glass #include <sys/buf.h>
     50   1.8  glass #include <sys/socket.h>
     51   1.8  glass #include <sys/syslog.h>
     52   1.8  glass #include <sys/ioctl.h>
     53  1.10    gwr #include <sys/malloc.h>
     54   1.8  glass #include <sys/errno.h>
     55   1.8  glass 
     56   1.8  glass #include <net/if.h>
     57   1.8  glass #include <net/netisr.h>
     58   1.8  glass #include <net/route.h>
     59   1.1  glass 
     60  1.10    gwr #if NBPFILTER > 0
     61  1.10    gwr #include <sys/select.h>
     62  1.10    gwr #include <net/bpf.h>
     63  1.10    gwr #include <net/bpfdesc.h>
     64  1.10    gwr #endif
     65  1.10    gwr 
     66   1.1  glass #ifdef INET
     67   1.8  glass #include <netinet/in.h>
     68   1.8  glass #include <netinet/in_systm.h>
     69   1.8  glass #include <netinet/in_var.h>
     70   1.8  glass #include <netinet/ip.h>
     71   1.8  glass #include <netinet/if_ether.h>
     72   1.1  glass #endif
     73   1.1  glass 
     74   1.1  glass #ifdef NS
     75   1.8  glass #include <netns/ns.h>
     76   1.8  glass #include <netns/ns_if.h>
     77   1.1  glass #endif
     78   1.1  glass 
     79  1.10    gwr #ifdef APPLETALK
     80  1.10    gwr #include <netddp/atalk.h>
     81   1.1  glass #endif
     82   1.1  glass 
     83   1.8  glass #include <machine/autoconf.h>
     84  1.10    gwr #include <machine/cpu.h>
     85   1.1  glass 
     86   1.1  glass #include "if_lereg.h"
     87   1.1  glass #include "if_le.h"
     88   1.1  glass #include "if_le_subr.h"
     89   1.1  glass 
     90  1.10    gwr /*
     91  1.10    gwr  * The lance has only 24 address lines.  When it accesses memory,
     92  1.10    gwr  * the high address lines are hard-wired to 0xFF, so we must:
     93  1.10    gwr  * (1) put what we want the LANCE to see above 0xFF000000, and
     94  1.10    gwr  * (2) mask our CPU addresses down to 24 bits for the LANCE.
     95  1.10    gwr  */
     96  1.10    gwr #define	LANCE_ADDR(x)	((u_int)(x) & 0xFFffff)
     97  1.10    gwr #define ISQUADALIGN(a) (((a) & 0x3) == 0)
     98   1.1  glass 
     99  1.10    gwr /* console error messages */
    100  1.10    gwr int	ledebug = 0;
    101   1.1  glass 
    102  1.10    gwr #ifdef PACKETSTATS
    103  1.10    gwr long	lexpacketsizes[LEMTU+1];
    104  1.10    gwr long	lerpacketsizes[LEMTU+1];
    105  1.10    gwr #endif
    106  1.10    gwr 
    107  1.10    gwr /* autoconfiguration driver */
    108  1.10    gwr void	leattach(struct device *, struct device *, void *);
    109  1.14    gwr int 	le_md_match(struct device *, void *, void *args);
    110  1.10    gwr 
    111  1.10    gwr struct	cfdriver lecd = {
    112  1.10    gwr 	NULL, "le",
    113  1.10    gwr 	le_md_match, leattach,
    114  1.10    gwr 	DV_IFNET, sizeof(struct le_softc),
    115  1.10    gwr };
    116  1.10    gwr 
    117  1.10    gwr /* Forwards */
    118  1.10    gwr void	lesetladrf(struct le_softc *);
    119  1.10    gwr void	lereset(struct device *);
    120  1.10    gwr int 	leinit(int);
    121  1.10    gwr int 	lestart(struct ifnet *);
    122  1.10    gwr int 	leintr(void *);
    123  1.10    gwr void	lexint(struct le_softc *);
    124  1.10    gwr void	lerint(struct le_softc *);
    125  1.10    gwr void	leread(struct le_softc *, char *, int);
    126  1.10    gwr int 	leput(char *, struct mbuf *);
    127  1.10    gwr struct mbuf *leget(char *, int, int, struct ifnet *);
    128  1.14    gwr int 	leioctl(struct ifnet *, u_long, caddr_t);
    129  1.10    gwr void	leerror(struct le_softc *, int);
    130  1.10    gwr void	lererror(struct le_softc *, char *);
    131  1.10    gwr void	lexerror(struct le_softc *);
    132  1.10    gwr int 	lewatchdog(int);	/* XXX */
    133   1.1  glass 
    134   1.1  glass /*
    135   1.1  glass  * Interface exists: make available by filling in network interface
    136   1.1  glass  * record.  System will initialize the interface when it is ready
    137   1.1  glass  * to accept packets.
    138   1.1  glass  */
    139  1.10    gwr void
    140  1.10    gwr leattach(parent, self, args)
    141  1.10    gwr 	struct device *parent;
    142  1.10    gwr 	struct device *self;
    143  1.10    gwr 	void *args;
    144  1.10    gwr {
    145  1.10    gwr 	struct le_softc *sc = (struct le_softc *)self;
    146  1.10    gwr 	volatile struct lereg2 *ler2;
    147  1.10    gwr 	struct ifnet *ifp = &sc->sc_if;
    148  1.10    gwr 	int pri;
    149  1.10    gwr 	u_int a;
    150  1.10    gwr 
    151  1.10    gwr 	le_md_attach(parent, self, args);
    152  1.10    gwr 	printf(": ether address %s\n", ether_sprintf(sc->sc_addr));
    153   1.1  glass 
    154   1.1  glass 	/*
    155   1.1  glass 	 * Setup for transmit/receive
    156  1.10    gwr 	 *
    157  1.10    gwr 	 * According to Van, some versions of the Lance only use this
    158  1.10    gwr 	 * address to receive packets; it doesn't put them in
    159  1.10    gwr 	 * output packets. We'll want to make sure that lestart()
    160  1.10    gwr 	 * installs the address.
    161   1.1  glass 	 */
    162  1.10    gwr 	ler2 = sc->sc_r2;
    163  1.10    gwr 	ler2->ler2_padr[0] = sc->sc_addr[1];
    164  1.10    gwr 	ler2->ler2_padr[1] = sc->sc_addr[0];
    165  1.10    gwr 	ler2->ler2_padr[2] = sc->sc_addr[3];
    166  1.10    gwr 	ler2->ler2_padr[3] = sc->sc_addr[2];
    167  1.10    gwr 	ler2->ler2_padr[4] = sc->sc_addr[5];
    168  1.10    gwr 	ler2->ler2_padr[5] = sc->sc_addr[4];
    169   1.3  glass 	a = LANCE_ADDR(ler2->ler2_rmd);
    170  1.10    gwr #ifdef	DIAGNOSTIC
    171   1.3  glass 	if (!ISQUADALIGN(a))
    172   1.3  glass 	    panic("rdra not quad aligned");
    173  1.10    gwr #endif
    174   1.3  glass 	ler2->ler2_rlen = LE_RLEN | (a >> 16);
    175  1.10    gwr 	ler2->ler2_rdra = a;
    176   1.3  glass 	a = LANCE_ADDR(ler2->ler2_tmd);
    177  1.10    gwr #ifdef	DIAGNOSTIC
    178   1.3  glass 	if (!ISQUADALIGN(a))
    179   1.3  glass 	    panic("tdra not quad aligned");
    180  1.10    gwr #endif
    181   1.3  glass 	ler2->ler2_tlen = LE_TLEN | (a >> 16);
    182  1.10    gwr 	ler2->ler2_tdra = a;
    183  1.10    gwr 
    184  1.10    gwr 	/*
    185  1.10    gwr 	 * Set up event counters.
    186  1.10    gwr 	 */
    187  1.10    gwr 	evcnt_attach(&sc->sc_dev, "intr", &sc->sc_intrcnt);
    188  1.10    gwr 	evcnt_attach(&sc->sc_dev, "errs", &sc->sc_errcnt);
    189   1.1  glass 
    190  1.10    gwr 	ifp->if_unit = sc->sc_dev.dv_unit;
    191   1.1  glass 	ifp->if_name = "le";
    192   1.1  glass 	ifp->if_ioctl = leioctl;
    193   1.1  glass 	ifp->if_output = ether_output;
    194   1.1  glass 	ifp->if_start = lestart;
    195  1.10    gwr 	ifp->if_watchdog = lewatchdog;	/* XXX */
    196  1.10    gwr 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
    197  1.10    gwr #ifdef IFF_NOTRAILERS
    198  1.10    gwr 	/* XXX still compile when the blasted things are gone... */
    199  1.10    gwr 	ifp->if_flags |= IFF_NOTRAILERS;
    200  1.10    gwr #endif
    201   1.1  glass #if NBPFILTER > 0
    202  1.10    gwr 	bpfattach(&ifp->if_bpf, ifp, DLT_EN10MB, sizeof(struct ether_header));
    203   1.1  glass #endif
    204   1.1  glass 	if_attach(ifp);
    205   1.9    gwr 	ether_ifattach(ifp);
    206   1.1  glass }
    207   1.1  glass 
    208  1.10    gwr /*
    209  1.10    gwr  * Setup the logical address filter
    210  1.10    gwr  */
    211  1.10    gwr void
    212  1.10    gwr lesetladrf(sc)
    213  1.10    gwr 	register struct le_softc *sc;
    214  1.10    gwr {
    215  1.10    gwr 	register volatile struct lereg2 *ler2 = sc->sc_r2;
    216  1.10    gwr 	register struct ifnet *ifp = &sc->sc_if;
    217  1.10    gwr 	register struct ether_multi *enm;
    218  1.10    gwr 	register u_char *cp, c;
    219  1.10    gwr 	register u_long crc;
    220  1.10    gwr 	register int i, len;
    221  1.10    gwr 	struct ether_multistep step;
    222  1.10    gwr 
    223  1.10    gwr 	/*
    224  1.10    gwr 	 * Set up multicast address filter by passing all multicast
    225  1.10    gwr 	 * addresses through a crc generator, and then using the high
    226  1.10    gwr 	 * order 6 bits as a index into the 64 bit logical address
    227  1.10    gwr 	 * filter. The high order two bits select the word, while the
    228  1.10    gwr 	 * rest of the bits select the bit within the word.
    229  1.10    gwr 	 */
    230  1.10    gwr 
    231  1.10    gwr 	ler2->ler2_ladrf[0] = 0;
    232  1.10    gwr 	ler2->ler2_ladrf[1] = 0;
    233  1.10    gwr 	ler2->ler2_ladrf[2] = 0;
    234  1.10    gwr 	ler2->ler2_ladrf[3] = 0;
    235  1.10    gwr 	ifp->if_flags &= ~IFF_ALLMULTI;
    236  1.10    gwr 	ETHER_FIRST_MULTI(step, &sc->sc_ac, enm);
    237  1.10    gwr 	while (enm != NULL) {
    238  1.10    gwr 		if (bcmp((caddr_t)&enm->enm_addrlo,
    239  1.10    gwr 		    (caddr_t)&enm->enm_addrhi, sizeof(enm->enm_addrlo)) != 0) {
    240  1.10    gwr 			/*
    241  1.10    gwr 			 * We must listen to a range of multicast
    242  1.10    gwr 			 * addresses. For now, just accept all
    243  1.10    gwr 			 * multicasts, rather than trying to set only
    244  1.10    gwr 			 * those filter bits needed to match the range.
    245  1.10    gwr 			 * (At this time, the only use of address
    246  1.10    gwr 			 * ranges is for IP multicast routing, for
    247  1.10    gwr 			 * which the range is big enough to require all
    248  1.10    gwr 			 * bits set.)
    249  1.10    gwr 			 */
    250  1.10    gwr 			ler2->ler2_ladrf[0] = 0xffff;
    251  1.10    gwr 			ler2->ler2_ladrf[1] = 0xffff;
    252  1.10    gwr 			ler2->ler2_ladrf[2] = 0xffff;
    253  1.10    gwr 			ler2->ler2_ladrf[3] = 0xffff;
    254  1.10    gwr 			ifp->if_flags |= IFF_ALLMULTI;
    255  1.10    gwr 			return;
    256  1.10    gwr 		}
    257  1.10    gwr 
    258  1.10    gwr 		/*
    259  1.10    gwr 		 * One would think, given the AM7990 document's polynomial
    260  1.10    gwr 		 * of 0x04c11db6, that this should be 0x6db88320 (the bit
    261  1.10    gwr 		 * reversal of the AMD value), but that is not right.  See
    262  1.10    gwr 		 * the BASIC listing: bit 0 (our bit 31) must then be set.
    263  1.10    gwr 		 */
    264  1.10    gwr 		cp = (unsigned char *)&enm->enm_addrlo;
    265  1.10    gwr 		crc = 0xffffffff;
    266  1.10    gwr 		for (len = 6; --len >= 0;) {
    267  1.10    gwr 			c = *cp++;
    268  1.10    gwr 			for (i = 0; i < 8; i++) {
    269  1.10    gwr 				if ((c & 0x01) ^ (crc & 0x01)) {
    270  1.10    gwr 					crc >>= 1;
    271  1.10    gwr 					crc = crc ^ 0xedb88320;
    272  1.10    gwr 				} else
    273  1.10    gwr 					crc >>= 1;
    274  1.10    gwr 				c >>= 1;
    275  1.10    gwr 			}
    276  1.10    gwr 		}
    277  1.10    gwr 		/* Just want the 6 most significant bits. */
    278  1.10    gwr 		crc = crc >> 26;
    279  1.10    gwr 
    280  1.10    gwr 		/* Turn on the corresponding bit in the filter. */
    281  1.10    gwr 		ler2->ler2_ladrf[crc >> 4] |= 1 << (crc & 0xf);
    282  1.10    gwr 
    283  1.10    gwr 		ETHER_NEXT_MULTI(step, enm);
    284  1.10    gwr 	}
    285  1.10    gwr }
    286  1.10    gwr 
    287  1.10    gwr void
    288  1.10    gwr lereset(dev)
    289  1.10    gwr 	struct device *dev;
    290   1.1  glass {
    291  1.10    gwr 	struct le_softc *sc = (struct le_softc *)dev;
    292  1.10    gwr 	volatile struct lereg1 *ler1 = sc->sc_r1;
    293  1.10    gwr 	volatile struct lereg2 *ler2 = sc->sc_r2;
    294  1.10    gwr 	int i, timo, stat;
    295  1.10    gwr 	u_int a;
    296   1.1  glass 
    297  1.10    gwr 	if (ledebug)
    298  1.10    gwr 	    printf("%s: resetting, reg %x, ram %x\n",
    299  1.10    gwr 			   sc->sc_dev.dv_xname, sc->sc_r1, sc->sc_r2);
    300  1.10    gwr 
    301  1.10    gwr #ifdef	DIAGNOSTIC
    302  1.10    gwr 	i = getsr();
    303  1.10    gwr 	if ((i & PSL_IPL) < PSL_IPL3)
    304  1.10    gwr 		panic("lereset at low ipl, sr=%x", i);
    305  1.10    gwr #endif
    306  1.10    gwr 
    307  1.10    gwr #if NBPFILTER > 0
    308  1.10    gwr 	if (sc->sc_if.if_flags & IFF_PROMISC)
    309  1.10    gwr 		ler2->ler2_mode = LE_MODE_NORMAL | LE_MODE_PROM;
    310  1.10    gwr 	else
    311  1.10    gwr #endif
    312  1.10    gwr 		ler2->ler2_mode = LE_MODE_NORMAL;
    313  1.10    gwr 	ler1->ler1_rap = LE_CSR0;
    314  1.10    gwr 	ler1->ler1_rdp = LE_C0_STOP;
    315  1.10    gwr 
    316  1.10    gwr 	/* Setup the logical address filter */
    317  1.10    gwr 	lesetladrf(sc);
    318  1.10    gwr 
    319  1.10    gwr 	/* init receive and transmit rings */
    320   1.1  glass 	for (i = 0; i < LERBUF; i++) {
    321  1.10    gwr 		a = LANCE_ADDR(&ler2->ler2_rbuf[i][0]);
    322  1.10    gwr 		ler2->ler2_rmd[i].rmd0 = a;
    323   1.3  glass 		ler2->ler2_rmd[i].rmd1_hadr = a >> 16;
    324  1.10    gwr 		ler2->ler2_rmd[i].rmd1_bits = LE_R1_OWN;
    325  1.11    gwr 		ler2->ler2_rmd[i].rmd2 = -LEMTU | LE_XMD2_ONES;
    326   1.1  glass 		ler2->ler2_rmd[i].rmd3 = 0;
    327   1.1  glass 	}
    328   1.1  glass 	for (i = 0; i < LETBUF; i++) {
    329  1.10    gwr 		a = LANCE_ADDR(&ler2->ler2_tbuf[i][0]);
    330  1.10    gwr 		ler2->ler2_tmd[i].tmd0 = a;
    331  1.10    gwr 		ler2->ler2_tmd[i].tmd1_hadr = a >> 16;
    332   1.3  glass 		ler2->ler2_tmd[i].tmd1_bits = 0;
    333  1.11    gwr 		ler2->ler2_tmd[i].tmd2 = LE_XMD2_ONES;
    334   1.1  glass 		ler2->ler2_tmd[i].tmd3 = 0;
    335   1.1  glass 	}
    336  1.10    gwr 
    337  1.10    gwr 	bzero(&ler2->ler2_rbuf[0][0], (LERBUF + LETBUF) * LEMTU);
    338  1.10    gwr 
    339  1.10    gwr 	/* lance will stuff packet into receive buffer 0 next */
    340  1.10    gwr 	sc->sc_rmd = 0;
    341  1.10    gwr 
    342  1.10    gwr 	/*
    343  1.10    gwr 	 * Tell the chip where to find the initialization block.
    344  1.10    gwr 	 * Note that CSR1, CSR2, and CSR3 may only be accessed
    345  1.10    gwr 	 * while the STOP bit is set in CSR0.
    346  1.10    gwr 	 */
    347  1.10    gwr 	a = LANCE_ADDR(&ler2->ler2_mode);
    348  1.10    gwr 	ler1->ler1_rap = LE_CSR1;
    349  1.10    gwr 	ler1->ler1_rdp = a;
    350  1.10    gwr 	ler1->ler1_rap = LE_CSR2;
    351  1.10    gwr 	ler1->ler1_rdp = a >> 16;
    352  1.10    gwr 	ler1->ler1_rap = LE_CSR3;
    353  1.10    gwr 	ler1->ler1_rdp = LE_C3_CONFIG;
    354  1.10    gwr 	ler1->ler1_rap = LE_CSR0;
    355  1.10    gwr 	ler1->ler1_rdp = LE_C0_INIT;
    356  1.10    gwr 	timo = 10000;
    357  1.10    gwr 	while (((stat = ler1->ler1_rdp) & (LE_C0_ERR | LE_C0_IDON)) == 0) {
    358  1.10    gwr 		delay(100); 	/* XXX */
    359  1.10    gwr 		if (--timo == 0) {
    360  1.10    gwr 			printf("%s: init timeout, stat=%b\n",
    361  1.10    gwr 			    sc->sc_dev.dv_xname, stat, LE_C0_BITS);
    362  1.10    gwr 			break;
    363  1.10    gwr 		}
    364  1.10    gwr 	}
    365  1.10    gwr 	if (stat & LE_C0_ERR) {
    366  1.10    gwr 		printf("%s: init failed, stat=%b\n",
    367  1.10    gwr 		    sc->sc_dev.dv_xname, stat, LE_C0_BITS);
    368  1.10    gwr 		sc->sc_if.if_flags &= ~IFF_RUNNING; 	/* XXX */
    369  1.10    gwr 		return;
    370  1.10    gwr 	}
    371  1.10    gwr 	ler1->ler1_rdp = LE_C0_IDON;	/* clear IDON */
    372  1.10    gwr 	ler1->ler1_rdp = LE_C0_STRT | LE_C0_INEA;
    373  1.10    gwr 	sc->sc_if.if_flags &= ~IFF_OACTIVE;
    374  1.10    gwr 	delay(100);		/* XXX */
    375   1.1  glass }
    376   1.1  glass 
    377  1.10    gwr /*
    378  1.10    gwr  * Device timeout/watchdog routine.  Entered if the device neglects to
    379  1.10    gwr  * generate an interrupt after a transmit has been started on it.
    380  1.10    gwr  */
    381  1.10    gwr int
    382  1.10    gwr lewatchdog(unit)
    383  1.10    gwr 	int unit;
    384   1.1  glass {
    385  1.10    gwr 	struct le_softc *sc = lecd.cd_devs[unit];
    386  1.10    gwr 	struct ifnet *ifp = &sc->sc_if;
    387  1.10    gwr 	int s;
    388  1.10    gwr 
    389  1.10    gwr 	printf("%s: watchdog timeout\n", sc->sc_dev.dv_xname);
    390  1.10    gwr 	sc->sc_if.if_oerrors++;
    391   1.1  glass 
    392  1.10    gwr #ifdef	DIAGNOSTIC
    393  1.10    gwr 	s = getsr();
    394  1.10    gwr 	if ((s & PSL_IPL) > PSL_IPL3)
    395  1.10    gwr 		panic("lewatchdog would lower spl, sr=%x", s);
    396   1.1  glass #endif
    397  1.10    gwr 
    398  1.10    gwr 	s = splimp();	/* XXX - Can this lower the IPL? */
    399  1.10    gwr 	lereset(&sc->sc_dev);
    400  1.10    gwr 	lestart(&sc->sc_if);
    401  1.10    gwr 	splx(s);
    402   1.1  glass }
    403   1.1  glass 
    404   1.1  glass /*
    405   1.1  glass  * Initialization of interface
    406   1.1  glass  */
    407  1.10    gwr int
    408  1.10    gwr leinit(unit)
    409   1.1  glass 	int unit;
    410   1.1  glass {
    411  1.10    gwr 	struct le_softc *sc = lecd.cd_devs[unit];
    412  1.10    gwr 	struct ifnet *ifp = &sc->sc_if;
    413   1.1  glass 	int s;
    414   1.1  glass 
    415   1.1  glass 	/* not yet, if address still unknown */
    416  1.10    gwr 	if (ifp->if_addrlist == (struct ifaddr *)0) {
    417  1.10    gwr 		if (ledebug)
    418  1.10    gwr 			printf("leinit: no address yet\n");
    419  1.10    gwr 		return (0);
    420  1.10    gwr 	}
    421   1.1  glass 	if ((ifp->if_flags & IFF_RUNNING) == 0) {
    422   1.1  glass 		s = splimp();
    423   1.3  glass 		if (ledebug)
    424   1.3  glass 		    printf("le: initializing unit %d, reg %x, ram %x\n",
    425  1.10    gwr 				   unit, sc->sc_r1, sc->sc_r2);
    426   1.1  glass 		ifp->if_flags |= IFF_RUNNING;
    427  1.10    gwr 		lereset(&sc->sc_dev);
    428  1.10    gwr 		lestart(ifp);		/* XXX */
    429   1.1  glass 		splx(s);
    430   1.1  glass 	}
    431  1.10    gwr 	return (0);
    432   1.1  glass }
    433   1.1  glass 
    434   1.1  glass /*
    435   1.1  glass  * Start output on interface.  Get another datagram to send
    436   1.1  glass  * off of the interface queue, and copy it to the interface
    437   1.1  glass  * before starting the output.
    438   1.1  glass  */
    439  1.10    gwr int
    440  1.10    gwr lestart(ifp)
    441  1.10    gwr 	register struct ifnet *ifp;
    442   1.1  glass {
    443  1.10    gwr 	register struct le_softc *sc = lecd.cd_devs[ifp->if_unit];
    444  1.10    gwr 	register volatile struct letmd *tmd;
    445   1.1  glass 	register struct mbuf *m;
    446  1.10    gwr 	register int len;
    447  1.10    gwr 
    448  1.10    gwr #ifdef	DIAGNOSTIC
    449  1.10    gwr 	int s = getsr();
    450  1.10    gwr 	if ((s & PSL_IPL) < PSL_IPL3)
    451  1.10    gwr 		panic("lestart at low ipl, sr=%x", s);
    452  1.10    gwr #endif
    453   1.1  glass 
    454  1.10    gwr 	if ((sc->sc_if.if_flags & IFF_RUNNING) == 0) {
    455  1.10    gwr 		if (ledebug)
    456  1.10    gwr 			printf("lestart: not running\n");
    457  1.10    gwr 		return (0);
    458  1.10    gwr 	}
    459  1.10    gwr 	IF_DEQUEUE(&sc->sc_if.if_snd, m);
    460  1.10    gwr 	if (m == 0) {
    461  1.10    gwr 		if (ledebug & 2)
    462  1.10    gwr 			printf("lestart: send queue empty\n");
    463  1.10    gwr 		return (0);
    464  1.10    gwr 	}
    465  1.10    gwr 	len = leput(sc->sc_r2->ler2_tbuf[0], m);
    466   1.1  glass #if NBPFILTER > 0
    467   1.1  glass 	/*
    468   1.1  glass 	 * If bpf is listening on this interface, let it
    469   1.1  glass 	 * see the packet before we commit it to the wire.
    470   1.1  glass 	 */
    471  1.10    gwr 	if (sc->sc_if.if_bpf)
    472  1.10    gwr 		bpf_tap(sc->sc_if.if_bpf, sc->sc_r2->ler2_tbuf[0], len);
    473  1.10    gwr #endif
    474  1.10    gwr 
    475  1.10    gwr #ifdef PACKETSTATS
    476  1.10    gwr 	if (len <= LEMTU)
    477  1.10    gwr 		lexpacketsizes[len]++;
    478   1.1  glass #endif
    479  1.10    gwr 	tmd = sc->sc_r2->ler2_tmd;
    480   1.1  glass 	tmd->tmd3 = 0;
    481  1.11    gwr 	tmd->tmd2 = -len | LE_XMD2_ONES;
    482  1.10    gwr 	tmd->tmd1_bits = LE_T1_OWN | LE_T1_STP | LE_T1_ENP;
    483  1.10    gwr 	sc->sc_if.if_flags |= IFF_OACTIVE;
    484  1.10    gwr 
    485  1.10    gwr 	/* Set a timer just in case we never hear from the board again. */
    486  1.10    gwr 	ifp->if_timer = 2;
    487  1.10    gwr 
    488  1.10    gwr 	return (0);
    489  1.10    gwr }
    490  1.10    gwr 
    491  1.10    gwr int
    492  1.10    gwr leintr(dev)
    493  1.10    gwr 	register void *dev;
    494  1.10    gwr {
    495  1.10    gwr 	register struct le_softc *sc = dev;
    496  1.10    gwr 	register volatile struct lereg1 *ler1 = sc->sc_r1;
    497  1.10    gwr 	register int csr0;
    498  1.10    gwr 
    499  1.10    gwr 	csr0 = ler1->ler1_rdp;
    500  1.10    gwr 	if (ledebug & 2)
    501  1.10    gwr 	    printf("[%s: intr, stat %b]\n",
    502  1.10    gwr 			   sc->sc_dev.dv_xname, csr0, LE_C0_BITS);
    503  1.10    gwr 
    504  1.10    gwr 	if ((csr0 & LE_C0_INTR) == 0)
    505  1.10    gwr 		return (0);
    506  1.10    gwr 	sc->sc_intrcnt.ev_count++;
    507  1.10    gwr 
    508  1.10    gwr 	if (csr0 & LE_C0_ERR) {
    509  1.10    gwr 		sc->sc_errcnt.ev_count++;
    510  1.10    gwr 		leerror(sc, csr0);
    511  1.10    gwr 		if (csr0 & LE_C0_MERR) {
    512  1.10    gwr 			sc->sc_merr++;
    513  1.10    gwr 			lereset(&sc->sc_dev);
    514  1.10    gwr 			return (1);
    515   1.1  glass 		}
    516  1.10    gwr 		if (csr0 & LE_C0_BABL)
    517  1.10    gwr 			sc->sc_babl++;
    518  1.10    gwr 		if (csr0 & LE_C0_CERR)
    519  1.10    gwr 			sc->sc_cerr++;
    520  1.10    gwr 		if (csr0 & LE_C0_MISS)
    521  1.10    gwr 			sc->sc_miss++;
    522  1.10    gwr 		ler1->ler1_rdp = LE_C0_BABL|LE_C0_CERR|LE_C0_MISS|LE_C0_INEA;
    523  1.10    gwr 	}
    524  1.10    gwr 	if ((csr0 & LE_C0_RXON) == 0) {
    525  1.10    gwr 		sc->sc_rxoff++;
    526  1.10    gwr 		lereset(&sc->sc_dev);
    527  1.10    gwr 		return (1);
    528  1.10    gwr 	}
    529  1.10    gwr 	if ((csr0 & LE_C0_TXON) == 0) {
    530  1.10    gwr 		sc->sc_txoff++;
    531  1.10    gwr 		lereset(&sc->sc_dev);
    532  1.10    gwr 		return (1);
    533   1.1  glass 	}
    534  1.10    gwr 	if (csr0 & LE_C0_RINT) {
    535   1.1  glass 		/* interrupt is cleared in lerint */
    536  1.10    gwr 		lerint(sc);
    537   1.1  glass 	}
    538  1.10    gwr 	if (csr0 & LE_C0_TINT) {
    539  1.10    gwr 		ler1->ler1_rdp = LE_C0_TINT|LE_C0_INEA;
    540  1.10    gwr 		lexint(sc);
    541   1.1  glass 	}
    542  1.10    gwr 	return (1);
    543   1.1  glass }
    544   1.1  glass 
    545   1.1  glass /*
    546   1.1  glass  * Ethernet interface transmitter interrupt.
    547   1.1  glass  * Start another output if more data to send.
    548   1.1  glass  */
    549  1.10    gwr void
    550  1.10    gwr lexint(sc)
    551  1.10    gwr 	register struct le_softc *sc;
    552  1.10    gwr {
    553  1.10    gwr 	register volatile struct letmd *tmd = sc->sc_r2->ler2_tmd;
    554  1.10    gwr 
    555  1.10    gwr 	sc->sc_lestats.lexints++;
    556  1.10    gwr 	if ((sc->sc_if.if_flags & IFF_OACTIVE) == 0) {
    557  1.10    gwr 		sc->sc_xint++;
    558   1.1  glass 		return;
    559   1.1  glass 	}
    560  1.10    gwr 	if (tmd->tmd1_bits & LE_T1_OWN) {
    561  1.10    gwr 		sc->sc_xown++;
    562   1.1  glass 		return;
    563   1.1  glass 	}
    564  1.10    gwr 	if (tmd->tmd1_bits & LE_T1_ERR) {
    565   1.1  glass err:
    566  1.10    gwr 		lexerror(sc);
    567  1.10    gwr 		sc->sc_if.if_oerrors++;
    568  1.10    gwr 		if (tmd->tmd3 & (LE_T3_BUFF|LE_T3_UFLO)) {
    569  1.10    gwr 			sc->sc_uflo++;
    570  1.10    gwr 			lereset(&sc->sc_dev);
    571  1.10    gwr 		} else if (tmd->tmd3 & LE_T3_LCOL)
    572  1.10    gwr 			sc->sc_if.if_collisions++;
    573  1.10    gwr 		else if (tmd->tmd3 & LE_T3_RTRY)
    574  1.10    gwr 			sc->sc_if.if_collisions += 16;
    575   1.1  glass 	}
    576  1.10    gwr 	else if (tmd->tmd3 & LE_T3_BUFF)
    577   1.1  glass 		/* XXX documentation says BUFF not included in ERR */
    578   1.1  glass 		goto err;
    579  1.10    gwr 	else if (tmd->tmd1_bits & LE_T1_ONE)
    580  1.10    gwr 		sc->sc_if.if_collisions++;
    581  1.10    gwr 	else if (tmd->tmd1_bits & LE_T1_MORE)
    582   1.1  glass 		/* what is the real number? */
    583  1.10    gwr 		sc->sc_if.if_collisions += 2;
    584   1.1  glass 	else
    585  1.10    gwr 		sc->sc_if.if_opackets++;
    586  1.10    gwr 	sc->sc_if.if_flags &= ~IFF_OACTIVE;
    587  1.10    gwr 	sc->sc_if.if_timer = 0;		/* XXX */
    588  1.10    gwr 	lestart(&sc->sc_if);
    589   1.1  glass }
    590   1.1  glass 
    591   1.1  glass #define	LENEXTRMP \
    592  1.10    gwr 	if (++bix == LERBUF) bix = 0, rmd = sc->sc_r2->ler2_rmd; else ++rmd
    593   1.1  glass 
    594   1.1  glass /*
    595   1.1  glass  * Ethernet interface receiver interrupt.
    596   1.1  glass  * If input error just drop packet.
    597   1.1  glass  * Decapsulate packet based on type and pass to type specific
    598   1.1  glass  * higher-level input routine.
    599   1.1  glass  */
    600  1.10    gwr void
    601  1.10    gwr lerint(sc)
    602  1.10    gwr 	register struct le_softc *sc;
    603   1.1  glass {
    604  1.10    gwr 	register int bix = sc->sc_rmd;
    605  1.10    gwr 	register volatile struct lermd *rmd = &sc->sc_r2->ler2_rmd[bix];
    606   1.1  glass 
    607  1.10    gwr 	sc->sc_lestats.lerints++;
    608   1.1  glass 	/*
    609   1.1  glass 	 * Out of sync with hardware, should never happen?
    610   1.1  glass 	 */
    611  1.10    gwr 	if (rmd->rmd1_bits & LE_R1_OWN) {
    612  1.10    gwr 		do {
    613  1.10    gwr 			sc->sc_lestats.lerscans++;
    614  1.10    gwr 			LENEXTRMP;
    615  1.10    gwr 		} while ((rmd->rmd1_bits & LE_R1_OWN) && bix != sc->sc_rmd);
    616  1.10    gwr 		if (bix == sc->sc_rmd)
    617  1.10    gwr 			printf("%s: RINT with no buffer\n",
    618  1.10    gwr 			    sc->sc_dev.dv_xname);
    619  1.10    gwr 	} else
    620  1.10    gwr 		sc->sc_lestats.lerhits++;
    621   1.1  glass 
    622   1.1  glass 	/*
    623   1.1  glass 	 * Process all buffers with valid data
    624   1.1  glass 	 */
    625  1.10    gwr 	while ((rmd->rmd1_bits & LE_R1_OWN) == 0) {
    626   1.1  glass 		int len = rmd->rmd3;
    627   1.1  glass 
    628   1.1  glass 		/* Clear interrupt to avoid race condition */
    629  1.10    gwr 		sc->sc_r1->ler1_rdp = LE_C0_RINT|LE_C0_INEA;
    630   1.1  glass 
    631  1.10    gwr 		if (rmd->rmd1_bits & LE_R1_ERR) {
    632  1.10    gwr 			sc->sc_rmd = bix;
    633  1.10    gwr 			lererror(sc, "bad packet");
    634  1.10    gwr 			sc->sc_if.if_ierrors++;
    635  1.10    gwr 		} else if ((rmd->rmd1_bits & (LE_R1_STP|LE_R1_ENP)) !=
    636  1.10    gwr 		    (LE_R1_STP|LE_R1_ENP)) {
    637  1.10    gwr 			/* XXX make a define for LE_R1_STP|LE_R1_ENP? */
    638   1.1  glass 			/*
    639   1.1  glass 			 * Find the end of the packet so we can see how long
    640   1.1  glass 			 * it was.  We still throw it away.
    641   1.1  glass 			 */
    642   1.1  glass 			do {
    643  1.10    gwr 				sc->sc_r1->ler1_rdp = LE_C0_RINT|LE_C0_INEA;
    644   1.1  glass 				rmd->rmd3 = 0;
    645  1.10    gwr 				rmd->rmd1_bits = LE_R1_OWN;
    646   1.1  glass 				LENEXTRMP;
    647  1.10    gwr 			} while (!(rmd->rmd1_bits &
    648  1.10    gwr 			    (LE_R1_OWN|LE_R1_ERR|LE_R1_STP|LE_R1_ENP)));
    649  1.10    gwr 			sc->sc_rmd = bix;
    650  1.10    gwr 			lererror(sc, "chained buffer");
    651  1.10    gwr 			sc->sc_rxlen++;
    652   1.1  glass 			/*
    653   1.1  glass 			 * If search terminated without successful completion
    654   1.1  glass 			 * we reset the hardware (conservative).
    655   1.1  glass 			 */
    656  1.10    gwr 			if ((rmd->rmd1_bits &
    657  1.10    gwr 			    (LE_R1_OWN|LE_R1_ERR|LE_R1_STP|LE_R1_ENP)) !=
    658  1.10    gwr 			    LE_R1_ENP) {
    659  1.10    gwr 				lereset(&sc->sc_dev);
    660   1.1  glass 				return;
    661   1.1  glass 			}
    662  1.10    gwr 		} else {
    663  1.10    gwr 			leread(sc, sc->sc_r2->ler2_rbuf[bix], len);
    664  1.10    gwr #ifdef PACKETSTATS
    665  1.10    gwr 			lerpacketsizes[len]++;
    666  1.10    gwr #endif
    667  1.10    gwr 			sc->sc_lestats.lerbufs++;
    668  1.10    gwr 		}
    669   1.1  glass 		rmd->rmd3 = 0;
    670  1.10    gwr 		rmd->rmd1_bits = LE_R1_OWN;
    671   1.1  glass 		LENEXTRMP;
    672   1.1  glass 	}
    673  1.10    gwr 	sc->sc_rmd = bix;
    674   1.1  glass }
    675   1.1  glass 
    676  1.10    gwr void
    677  1.10    gwr leread(sc, pkt, len)
    678  1.10    gwr 	register struct le_softc *sc;
    679  1.10    gwr 	char *pkt;
    680   1.1  glass 	int len;
    681   1.1  glass {
    682   1.1  glass 	register struct ether_header *et;
    683  1.10    gwr 	register struct ifnet *ifp = &sc->sc_if;
    684  1.10    gwr 	struct mbuf *m;
    685  1.10    gwr 	struct ifqueue *inq;
    686  1.10    gwr 	int flags;
    687   1.1  glass 
    688  1.10    gwr 	ifp->if_ipackets++;
    689  1.10    gwr 	et = (struct ether_header *)pkt;
    690   1.1  glass 	et->ether_type = ntohs((u_short)et->ether_type);
    691   1.1  glass 	/* adjust input length to account for header and CRC */
    692  1.10    gwr 	len -= sizeof(struct ether_header) + 4;
    693   1.1  glass 
    694   1.1  glass 	if (len <= 0) {
    695   1.1  glass 		if (ledebug)
    696   1.1  glass 			log(LOG_WARNING,
    697  1.10    gwr 			    "%s: ierror(runt packet): from %s: len=%d\n",
    698  1.10    gwr 			    sc->sc_dev.dv_xname,
    699  1.10    gwr 			    ether_sprintf(et->ether_shost), len);
    700  1.10    gwr 		sc->sc_runt++;
    701  1.10    gwr 		ifp->if_ierrors++;
    702   1.1  glass 		return;
    703   1.1  glass 	}
    704  1.10    gwr 
    705  1.10    gwr 	/* Setup mbuf flags we'll need later */
    706  1.10    gwr 	flags = 0;
    707  1.10    gwr 	if (bcmp((caddr_t)etherbroadcastaddr,
    708  1.10    gwr 	    (caddr_t)et->ether_dhost, sizeof(etherbroadcastaddr)) == 0)
    709  1.10    gwr 		flags |= M_BCAST;
    710  1.10    gwr 	if (et->ether_dhost[0] & 1)
    711  1.10    gwr 		flags |= M_MCAST;
    712  1.10    gwr 
    713   1.1  glass #if NBPFILTER > 0
    714   1.1  glass 	/*
    715   1.1  glass 	 * Check if there's a bpf filter listening on this interface.
    716  1.10    gwr 	 * If so, hand off the raw packet to enet, then discard things
    717  1.10    gwr 	 * not destined for us (but be sure to keep broadcast/multicast).
    718   1.1  glass 	 */
    719  1.10    gwr 	if (ifp->if_bpf) {
    720  1.10    gwr 		bpf_tap(ifp->if_bpf, pkt,
    721  1.10    gwr 		    len + sizeof(struct ether_header));
    722  1.10    gwr 		if ((flags & (M_BCAST | M_MCAST)) == 0 &&
    723  1.10    gwr 		    bcmp(et->ether_dhost, sc->sc_addr,
    724   1.1  glass 			    sizeof(et->ether_dhost)) != 0)
    725   1.1  glass 			return;
    726   1.1  glass 	}
    727   1.1  glass #endif
    728  1.10    gwr 	m = leget(pkt, len, 0, ifp);
    729   1.1  glass 	if (m == 0)
    730   1.1  glass 		return;
    731   1.1  glass 
    732  1.10    gwr 	ether_input(ifp, et, m);
    733   1.1  glass }
    734   1.1  glass 
    735   1.1  glass /*
    736   1.1  glass  * Routine to copy from mbuf chain to transmit
    737   1.1  glass  * buffer in board local memory.
    738  1.10    gwr  *
    739  1.10    gwr  * ### this can be done by remapping in some cases
    740   1.1  glass  */
    741  1.10    gwr int
    742   1.1  glass leput(lebuf, m)
    743   1.1  glass 	register char *lebuf;
    744   1.1  glass 	register struct mbuf *m;
    745   1.1  glass {
    746   1.1  glass 	register struct mbuf *mp;
    747   1.1  glass 	register int len, tlen = 0;
    748   1.1  glass 
    749   1.1  glass 	for (mp = m; mp; mp = mp->m_next) {
    750   1.1  glass 		len = mp->m_len;
    751   1.1  glass 		if (len == 0)
    752   1.1  glass 			continue;
    753   1.1  glass 		tlen += len;
    754   1.1  glass 		bcopy(mtod(mp, char *), lebuf, len);
    755   1.1  glass 		lebuf += len;
    756   1.1  glass 	}
    757   1.1  glass 	m_freem(m);
    758   1.1  glass 	if (tlen < LEMINSIZE) {
    759   1.1  glass 		bzero(lebuf, LEMINSIZE - tlen);
    760   1.1  glass 		tlen = LEMINSIZE;
    761   1.1  glass 	}
    762  1.10    gwr 	return (tlen);
    763   1.1  glass }
    764   1.1  glass 
    765   1.1  glass /*
    766   1.1  glass  * Routine to copy from board local memory into mbufs.
    767   1.1  glass  */
    768   1.1  glass struct mbuf *
    769   1.1  glass leget(lebuf, totlen, off0, ifp)
    770   1.1  glass 	char *lebuf;
    771   1.1  glass 	int totlen, off0;
    772   1.1  glass 	struct ifnet *ifp;
    773   1.1  glass {
    774   1.1  glass 	register struct mbuf *m;
    775   1.1  glass 	struct mbuf *top = 0, **mp = &top;
    776   1.1  glass 	register int off = off0, len;
    777   1.1  glass 	register char *cp;
    778   1.1  glass 	char *epkt;
    779   1.1  glass 
    780  1.10    gwr 	lebuf += sizeof(struct ether_header);
    781   1.1  glass 	cp = lebuf;
    782   1.1  glass 	epkt = cp + totlen;
    783   1.1  glass 	if (off) {
    784   1.1  glass 		cp += off + 2 * sizeof(u_short);
    785   1.1  glass 		totlen -= 2 * sizeof(u_short);
    786   1.1  glass 	}
    787   1.1  glass 
    788   1.1  glass 	MGETHDR(m, M_DONTWAIT, MT_DATA);
    789   1.1  glass 	if (m == 0)
    790   1.1  glass 		return (0);
    791   1.1  glass 	m->m_pkthdr.rcvif = ifp;
    792   1.1  glass 	m->m_pkthdr.len = totlen;
    793   1.1  glass 	m->m_len = MHLEN;
    794   1.1  glass 
    795   1.1  glass 	while (totlen > 0) {
    796   1.1  glass 		if (top) {
    797   1.1  glass 			MGET(m, M_DONTWAIT, MT_DATA);
    798   1.1  glass 			if (m == 0) {
    799   1.1  glass 				m_freem(top);
    800   1.1  glass 				return (0);
    801   1.1  glass 			}
    802   1.1  glass 			m->m_len = MLEN;
    803   1.1  glass 		}
    804   1.1  glass 		len = min(totlen, epkt - cp);
    805   1.1  glass 		if (len >= MINCLSIZE) {
    806   1.1  glass 			MCLGET(m, M_DONTWAIT);
    807   1.1  glass 			if (m->m_flags & M_EXT)
    808   1.1  glass 				m->m_len = len = min(len, MCLBYTES);
    809   1.1  glass 			else
    810   1.1  glass 				len = m->m_len;
    811   1.1  glass 		} else {
    812   1.1  glass 			/*
    813   1.1  glass 			 * Place initial small packet/header at end of mbuf.
    814   1.1  glass 			 */
    815   1.1  glass 			if (len < m->m_len) {
    816   1.1  glass 				if (top == 0 && len + max_linkhdr <= m->m_len)
    817   1.1  glass 					m->m_data += max_linkhdr;
    818   1.1  glass 				m->m_len = len;
    819   1.1  glass 			} else
    820   1.1  glass 				len = m->m_len;
    821   1.1  glass 		}
    822   1.1  glass 		bcopy(cp, mtod(m, caddr_t), (unsigned)len);
    823   1.1  glass 		cp += len;
    824   1.1  glass 		*mp = m;
    825   1.1  glass 		mp = &m->m_next;
    826   1.1  glass 		totlen -= len;
    827   1.1  glass 		if (cp == epkt)
    828   1.1  glass 			cp = lebuf;
    829   1.1  glass 	}
    830   1.1  glass 	return (top);
    831   1.1  glass }
    832   1.1  glass 
    833   1.1  glass /*
    834   1.1  glass  * Process an ioctl request.
    835   1.1  glass  */
    836  1.10    gwr int
    837   1.1  glass leioctl(ifp, cmd, data)
    838   1.1  glass 	register struct ifnet *ifp;
    839  1.14    gwr 	u_long cmd;
    840   1.1  glass 	caddr_t data;
    841   1.1  glass {
    842  1.10    gwr 	register struct ifaddr *ifa;
    843  1.10    gwr 	register struct le_softc *sc = lecd.cd_devs[ifp->if_unit];
    844  1.10    gwr 	register volatile struct lereg1 *ler1;
    845  1.10    gwr 	int s, error;
    846  1.10    gwr 
    847  1.10    gwr 	/* Make sure attach was called. */
    848  1.10    gwr 	if (sc->sc_r1 == NULL)
    849  1.10    gwr 		return (ENXIO);
    850   1.1  glass 
    851  1.10    gwr 	error = 0;
    852  1.10    gwr 	s = splimp();
    853   1.1  glass 	switch (cmd) {
    854   1.1  glass 
    855   1.1  glass 	case SIOCSIFADDR:
    856  1.10    gwr 		ifa = (struct ifaddr *)data;
    857   1.1  glass 		ifp->if_flags |= IFF_UP;
    858   1.1  glass 		switch (ifa->ifa_addr->sa_family) {
    859   1.1  glass #ifdef INET
    860   1.1  glass 		case AF_INET:
    861  1.10    gwr 			/* before arpwhohas */
    862  1.10    gwr 		    if ((ifp->if_flags & IFF_RUNNING) == 0) 	/* XXX */
    863  1.10    gwr 				(void)leinit(ifp->if_unit);
    864   1.1  glass 			((struct arpcom *)ifp)->ac_ipaddr =
    865   1.1  glass 				IA_SIN(ifa)->sin_addr;
    866   1.1  glass 			arpwhohas((struct arpcom *)ifp, &IA_SIN(ifa)->sin_addr);
    867   1.1  glass 			break;
    868   1.1  glass #endif
    869   1.1  glass #ifdef NS
    870   1.1  glass 		case AF_NS:
    871   1.1  glass 		    {
    872   1.1  glass 			register struct ns_addr *ina = &(IA_SNS(ifa)->sns_addr);
    873   1.1  glass 
    874   1.1  glass 			if (ns_nullhost(*ina))
    875  1.10    gwr 				ina->x_host = *(union ns_host *)(sc->sc_addr);
    876   1.1  glass 			else {
    877  1.10    gwr 				/*
    878  1.10    gwr 				 * The manual says we can't change the address
    879   1.1  glass 				 * while the receiver is armed,
    880   1.1  glass 				 * so reset everything
    881   1.1  glass 				 */
    882  1.10    gwr 				ifp->if_flags &= ~IFF_RUNNING;
    883   1.1  glass 				bcopy((caddr_t)ina->x_host.c_host,
    884  1.10    gwr 				    (caddr_t)sc->sc_addr, sizeof(sc->sc_addr));
    885   1.1  glass 			}
    886  1.10    gwr 			(void)leinit(ifp->if_unit);	/* does le_setaddr() */
    887   1.1  glass 			break;
    888   1.1  glass 		    }
    889   1.1  glass #endif
    890   1.1  glass 		default:
    891  1.10    gwr 			(void)leinit(ifp->if_unit);
    892   1.1  glass 			break;
    893   1.1  glass 		}
    894   1.1  glass 		break;
    895   1.1  glass 
    896   1.1  glass 	case SIOCSIFFLAGS:
    897  1.10    gwr 		ler1 = sc->sc_r1;
    898   1.1  glass 		if ((ifp->if_flags & IFF_UP) == 0 &&
    899   1.1  glass 		    ifp->if_flags & IFF_RUNNING) {
    900  1.10    gwr 			ler1->ler1_rdp = LE_C0_STOP;
    901   1.1  glass 			ifp->if_flags &= ~IFF_RUNNING;
    902   1.1  glass 		} else if (ifp->if_flags & IFF_UP &&
    903   1.1  glass 		    (ifp->if_flags & IFF_RUNNING) == 0)
    904  1.10    gwr 			(void)leinit(ifp->if_unit);
    905   1.1  glass 		/*
    906   1.1  glass 		 * If the state of the promiscuous bit changes, the interface
    907   1.1  glass 		 * must be reset to effect the change.
    908   1.1  glass 		 */
    909  1.10    gwr 		if (((ifp->if_flags ^ sc->sc_iflags) & IFF_PROMISC) &&
    910   1.1  glass 		    (ifp->if_flags & IFF_RUNNING)) {
    911  1.10    gwr 			sc->sc_iflags = ifp->if_flags;
    912  1.10    gwr 			lereset(&sc->sc_dev);
    913  1.10    gwr 			lestart(ifp);
    914  1.10    gwr 		}
    915  1.10    gwr 		break;
    916  1.10    gwr 
    917  1.10    gwr 	case SIOCADDMULTI:
    918  1.10    gwr 		error = ether_addmulti((struct ifreq *)data, &sc->sc_ac);
    919  1.10    gwr 		goto update_multicast;
    920  1.10    gwr 
    921  1.10    gwr 	case SIOCDELMULTI:
    922  1.10    gwr 		error = ether_delmulti((struct ifreq *)data, &sc->sc_ac);
    923  1.10    gwr 	update_multicast:
    924  1.10    gwr 		if (error == ENETRESET) {
    925  1.10    gwr 			/*
    926  1.10    gwr 			 * Multicast list has changed; set the hardware
    927  1.10    gwr 			 * filter accordingly.
    928  1.10    gwr 			 */
    929  1.10    gwr 			lereset(&sc->sc_dev);
    930  1.10    gwr 			lestart(ifp);			/* XXX */
    931  1.10    gwr 			error = 0;
    932   1.1  glass 		}
    933   1.1  glass 		break;
    934   1.1  glass 
    935   1.1  glass 	default:
    936   1.1  glass 		error = EINVAL;
    937   1.1  glass 	}
    938   1.1  glass 	splx(s);
    939   1.1  glass 	return (error);
    940   1.1  glass }
    941   1.1  glass 
    942  1.10    gwr void
    943  1.10    gwr leerror(sc, stat)
    944  1.10    gwr 	register struct le_softc *sc;
    945   1.1  glass 	int stat;
    946   1.1  glass {
    947   1.1  glass 	if (!ledebug)
    948   1.1  glass 		return;
    949   1.1  glass 
    950   1.1  glass 	/*
    951   1.1  glass 	 * Not all transceivers implement heartbeat
    952   1.1  glass 	 * so we only log CERR once.
    953   1.1  glass 	 */
    954  1.10    gwr 	if ((stat & LE_C0_CERR) && sc->sc_cerr)
    955   1.1  glass 		return;
    956  1.10    gwr 	log(LOG_WARNING, "%s: error: stat=%b\n",
    957  1.10    gwr 	    sc->sc_dev.dv_xname, stat, LE_C0_BITS);
    958   1.1  glass }
    959   1.1  glass 
    960  1.10    gwr void
    961  1.10    gwr lererror(sc, msg)
    962  1.10    gwr 	register struct le_softc *sc;
    963   1.1  glass 	char *msg;
    964   1.1  glass {
    965  1.10    gwr 	register volatile struct lermd *rmd;
    966   1.1  glass 	int len;
    967   1.1  glass 
    968   1.1  glass 	if (!ledebug)
    969   1.1  glass 		return;
    970   1.1  glass 
    971  1.10    gwr 	rmd = &sc->sc_r2->ler2_rmd[sc->sc_rmd];
    972   1.1  glass 	len = rmd->rmd3;
    973  1.10    gwr 	log(LOG_WARNING, "%s: ierror(%s): from %s: buf=%d, len=%d, rmd1=%b\n",
    974  1.10    gwr 	    sc->sc_dev.dv_xname, msg, len > 11 ?
    975  1.10    gwr 	    ether_sprintf((u_char *)&sc->sc_r2->ler2_rbuf[sc->sc_rmd][6]) :
    976  1.10    gwr 	    "unknown",
    977  1.10    gwr 	    sc->sc_rmd, len, rmd->rmd1_bits, LE_R1_BITS);
    978   1.1  glass }
    979   1.1  glass 
    980  1.10    gwr void
    981  1.10    gwr lexerror(sc)
    982  1.10    gwr 	register struct le_softc *sc;
    983   1.1  glass {
    984  1.10    gwr 	register volatile struct letmd *tmd;
    985  1.10    gwr 	register int len, tmd3, tdr;
    986   1.1  glass 
    987   1.1  glass 	if (!ledebug)
    988   1.1  glass 		return;
    989   1.1  glass 
    990  1.10    gwr 	tmd = sc->sc_r2->ler2_tmd;
    991  1.10    gwr 	tmd3 = tmd->tmd3;
    992  1.10    gwr 	tdr = tmd3 & LE_T3_TDR_MASK;
    993  1.11    gwr 	len = -(tmd->tmd2 & ~LE_XMD2_ONES);
    994   1.1  glass 	log(LOG_WARNING,
    995  1.10    gwr     "%s: oerror: to %s: buf=%d, len=%d, tmd1=%b, tmd3=%b, tdr=%d (%d nsecs)\n",
    996  1.10    gwr 	    sc->sc_dev.dv_xname, len > 5 ?
    997  1.10    gwr 	    ether_sprintf((u_char *)&sc->sc_r2->ler2_tbuf[0][0]) : "unknown",
    998   1.1  glass 	    0, len,
    999  1.10    gwr 	    tmd->tmd1_bits, LE_T1_BITS,
   1000  1.10    gwr 	    tmd3, LE_T3_BITS, tdr, tdr * 100);
   1001   1.1  glass }
   1002