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