Home | History | Annotate | Line # | Download | only in dev
if_le.c revision 1.23
      1  1.23  thorpej /*	$NetBSD: if_le.c,v 1.23 1995/12/02 18:22:02 thorpej Exp $	*/
      2  1.20      cgd 
      3   1.1      cgd /*
      4   1.1      cgd  * Copyright (c) 1982, 1990 The Regents of the University of California.
      5   1.1      cgd  * All rights reserved.
      6   1.1      cgd  *
      7   1.1      cgd  * Redistribution and use in source and binary forms, with or without
      8   1.1      cgd  * modification, are permitted provided that the following conditions
      9   1.1      cgd  * are met:
     10   1.1      cgd  * 1. Redistributions of source code must retain the above copyright
     11   1.1      cgd  *    notice, this list of conditions and the following disclaimer.
     12   1.1      cgd  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1      cgd  *    notice, this list of conditions and the following disclaimer in the
     14   1.1      cgd  *    documentation and/or other materials provided with the distribution.
     15   1.1      cgd  * 3. All advertising materials mentioning features or use of this software
     16   1.1      cgd  *    must display the following acknowledgement:
     17   1.1      cgd  *	This product includes software developed by the University of
     18   1.1      cgd  *	California, Berkeley and its contributors.
     19   1.1      cgd  * 4. Neither the name of the University nor the names of its contributors
     20   1.1      cgd  *    may be used to endorse or promote products derived from this software
     21   1.1      cgd  *    without specific prior written permission.
     22   1.1      cgd  *
     23   1.1      cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24   1.1      cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25   1.1      cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26   1.1      cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27   1.1      cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28   1.1      cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29   1.1      cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30   1.1      cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31   1.1      cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32   1.1      cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33   1.1      cgd  * SUCH DAMAGE.
     34   1.1      cgd  *
     35  1.20      cgd  *	@(#)if_le.c	7.6 (Berkeley) 5/8/91
     36   1.1      cgd  */
     37   1.1      cgd 
     38   1.1      cgd #include "le.h"
     39   1.1      cgd #if NLE > 0
     40   1.1      cgd 
     41   1.1      cgd #include "bpfilter.h"
     42   1.1      cgd 
     43   1.1      cgd /*
     44   1.1      cgd  * AMD 7990 LANCE
     45   1.5  mycroft  */
     46   1.5  mycroft #include <sys/param.h>
     47   1.5  mycroft #include <sys/systm.h>
     48   1.5  mycroft #include <sys/kernel.h>
     49   1.5  mycroft #include <sys/mbuf.h>
     50   1.5  mycroft #include <sys/buf.h>
     51   1.5  mycroft #include <sys/socket.h>
     52   1.5  mycroft #include <sys/syslog.h>
     53   1.5  mycroft #include <sys/ioctl.h>
     54   1.5  mycroft #include <sys/malloc.h>
     55   1.5  mycroft #include <sys/errno.h>
     56   1.5  mycroft 
     57   1.5  mycroft #include <net/if.h>
     58   1.5  mycroft #include <net/netisr.h>
     59   1.5  mycroft #include <net/route.h>
     60   1.5  mycroft #if NBPFILTER > 0
     61   1.5  mycroft #include <net/bpf.h>
     62   1.5  mycroft #include <net/bpfdesc.h>
     63   1.5  mycroft #endif
     64   1.1      cgd 
     65   1.1      cgd #ifdef INET
     66   1.5  mycroft #include <netinet/in.h>
     67   1.5  mycroft #include <netinet/in_systm.h>
     68   1.5  mycroft #include <netinet/in_var.h>
     69   1.5  mycroft #include <netinet/ip.h>
     70   1.5  mycroft #include <netinet/if_ether.h>
     71   1.1      cgd #endif
     72   1.1      cgd 
     73   1.1      cgd #ifdef NS
     74   1.5  mycroft #include <netns/ns.h>
     75   1.5  mycroft #include <netns/ns_if.h>
     76   1.1      cgd #endif
     77   1.1      cgd 
     78   1.5  mycroft #include <machine/cpu.h>
     79  1.18  mycroft #include <machine/mtpr.h>
     80   1.5  mycroft #include <hp300/hp300/isr.h>
     81  1.18  mycroft #ifdef USELEDS
     82  1.18  mycroft #include <hp300/hp300/led.h>
     83  1.18  mycroft #endif
     84   1.1      cgd 
     85   1.5  mycroft #include <hp300/dev/device.h>
     86   1.5  mycroft #include <hp300/dev/if_lereg.h>
     87   1.1      cgd 
     88  1.13  mycroft 
     89  1.13  mycroft #define	ETHER_MIN_LEN	64
     90  1.13  mycroft #define	ETHER_MAX_LEN	1518
     91  1.13  mycroft #define	ETHER_ADDR_LEN	6
     92  1.13  mycroft 
     93  1.13  mycroft 
     94   1.1      cgd /* offsets for:	   ID,   REGS,    MEM,  NVRAM */
     95   1.1      cgd int	lestd[] = { 0, 0x4000, 0x8000, 0xC008 };
     96   1.1      cgd 
     97   1.1      cgd struct	isr le_isr[NLE];
     98   1.1      cgd 
     99   1.1      cgd /*
    100   1.1      cgd  * Ethernet software status per interface.
    101   1.1      cgd  *
    102   1.1      cgd  * Each interface is referenced by a network interface structure,
    103  1.13  mycroft  * arpcom.ac_if, which the routing code uses to locate the interface.
    104   1.1      cgd  * This structure contains the output queue for the interface, its address, ...
    105   1.1      cgd  */
    106   1.1      cgd struct	le_softc {
    107  1.23  thorpej 	struct	hp_device *sc_hd;
    108  1.13  mycroft 	struct	arpcom sc_arpcom;	/* common Ethernet structures */
    109  1.13  mycroft 	struct	lereg0 *sc_r0;		/* DIO registers */
    110  1.13  mycroft 	struct	lereg1 *sc_r1;		/* LANCE registers */
    111  1.13  mycroft 	void	*sc_mem;
    112  1.13  mycroft 	struct	init_block *sc_init;
    113  1.13  mycroft 	struct	mds *sc_rd, *sc_td;
    114  1.13  mycroft 	u_char	*sc_rbuf, *sc_tbuf;
    115  1.13  mycroft 	int	sc_last_rd, sc_last_td;
    116  1.13  mycroft 	int	sc_no_td;
    117  1.13  mycroft #ifdef LEDEBUG
    118  1.13  mycroft 	int	sc_debug;
    119  1.13  mycroft #endif
    120   1.1      cgd } le_softc[NLE];
    121   1.1      cgd 
    122  1.13  mycroft int leintr __P((int));
    123  1.21  mycroft int leioctl __P((struct ifnet *, u_long, caddr_t));
    124  1.21  mycroft void lestart __P((struct ifnet *));
    125  1.21  mycroft void lewatchdog __P((int));
    126  1.13  mycroft static inline void lewrcsr __P((/* struct le_softc *, u_short, u_short */));
    127  1.13  mycroft static inline u_short lerdcsr __P((/* struct le_softc *, u_short */));
    128  1.13  mycroft void leinit __P((struct le_softc *));
    129  1.13  mycroft void lememinit __P((struct le_softc *));
    130  1.13  mycroft void lereset __P((struct le_softc *));
    131  1.13  mycroft void lestop __P((struct le_softc *));
    132  1.13  mycroft void letint __P((int));
    133  1.13  mycroft void lerint __P((int));
    134  1.13  mycroft void leread __P((struct le_softc *, u_char *, int));
    135  1.13  mycroft struct mbuf *leget __P((u_char *, int, struct ifnet *));
    136  1.13  mycroft #ifdef LEDEBUG
    137  1.13  mycroft void recv_print __P((struct le_softc *, int));
    138  1.13  mycroft void xmit_print __P((struct le_softc *, int));
    139  1.13  mycroft #endif
    140  1.13  mycroft void lesetladrf __P((struct arpcom *, u_long *));
    141   1.1      cgd 
    142  1.23  thorpej int lematch __P((struct hp_device *));
    143  1.23  thorpej void leattach __P((struct hp_device *));
    144   1.8  mycroft 
    145   1.8  mycroft struct	driver ledriver = {
    146  1.23  thorpej 	lematch, leattach, "le",
    147   1.8  mycroft };
    148   1.8  mycroft 
    149  1.13  mycroft static inline void
    150  1.13  mycroft lewrcsr(sc, port, val)
    151  1.13  mycroft 	struct le_softc *sc;
    152  1.13  mycroft 	register u_short port;
    153  1.13  mycroft 	register u_short val;
    154  1.13  mycroft {
    155  1.13  mycroft 	register struct lereg0 *ler0 = sc->sc_r0;
    156  1.13  mycroft 	register struct lereg1 *ler1 = sc->sc_r1;
    157  1.13  mycroft 
    158  1.13  mycroft 	do {
    159  1.13  mycroft 		ler1->ler1_rap = port;
    160  1.13  mycroft 	} while ((ler0->ler0_status & LE_ACK) == 0);
    161  1.13  mycroft 	do {
    162  1.13  mycroft 		ler1->ler1_rdp = val;
    163  1.13  mycroft 	} while ((ler0->ler0_status & LE_ACK) == 0);
    164  1.13  mycroft }
    165  1.13  mycroft 
    166  1.13  mycroft static inline u_short
    167  1.13  mycroft lerdcsr(sc, port)
    168  1.13  mycroft 	struct le_softc *sc;
    169  1.13  mycroft 	register u_short port;
    170  1.13  mycroft {
    171  1.13  mycroft 	register struct lereg0 *ler0 = sc->sc_r0;
    172  1.13  mycroft 	register struct lereg1 *ler1 = sc->sc_r1;
    173  1.13  mycroft 	register u_short val;
    174  1.13  mycroft 
    175  1.13  mycroft 	do {
    176  1.13  mycroft 		ler1->ler1_rap = port;
    177  1.13  mycroft 	} while ((ler0->ler0_status & LE_ACK) == 0);
    178  1.13  mycroft 	do {
    179  1.13  mycroft 		val = ler1->ler1_rdp;
    180  1.13  mycroft 	} while ((ler0->ler0_status & LE_ACK) == 0);
    181  1.13  mycroft 	return (val);
    182  1.13  mycroft }
    183  1.13  mycroft 
    184  1.23  thorpej int
    185  1.23  thorpej lematch(hd)
    186  1.23  thorpej 	struct hp_device *hd;
    187  1.23  thorpej {
    188  1.23  thorpej 	register struct lereg0 *ler0;
    189  1.23  thorpej 	struct le_softc *sc = &le_softc[hd->hp_unit];
    190  1.23  thorpej 
    191  1.23  thorpej 	ler0 = (struct lereg0 *)(lestd[0] + (int)hd->hp_addr);
    192  1.23  thorpej 	if (ler0->ler0_id != LEID)
    193  1.23  thorpej 		return (0);
    194  1.23  thorpej 
    195  1.23  thorpej 	hd->hp_ipl = LE_IPL(ler0->ler0_status);
    196  1.23  thorpej 	sc->sc_hd = hd;
    197  1.23  thorpej 
    198  1.23  thorpej 	return (1);
    199  1.23  thorpej }
    200  1.23  thorpej 
    201   1.1      cgd /*
    202   1.1      cgd  * Interface exists: make available by filling in network interface
    203   1.1      cgd  * record.  System will initialize the interface when it is ready
    204   1.1      cgd  * to accept packets.
    205   1.1      cgd  */
    206  1.23  thorpej void
    207   1.1      cgd leattach(hd)
    208   1.1      cgd 	struct hp_device *hd;
    209   1.1      cgd {
    210   1.1      cgd 	register struct lereg0 *ler0;
    211   1.5  mycroft 	struct le_softc *sc = &le_softc[hd->hp_unit];
    212  1.13  mycroft 	struct ifnet *ifp = &sc->sc_arpcom.ac_if;
    213   1.1      cgd 	char *cp;
    214   1.1      cgd 	int i;
    215   1.1      cgd 
    216   1.5  mycroft 	ler0 = sc->sc_r0 = (struct lereg0 *)(lestd[0] + (int)hd->hp_addr);
    217  1.13  mycroft 	sc->sc_r1 = (struct lereg1 *)(lestd[1] + (int)hd->hp_addr);
    218  1.13  mycroft 	sc->sc_mem = (void *)(lestd[2] + (int)hd->hp_addr);
    219   1.1      cgd 	le_isr[hd->hp_unit].isr_intr = leintr;
    220  1.23  thorpej 	le_isr[hd->hp_unit].isr_ipl = hd->hp_ipl;
    221   1.1      cgd 	le_isr[hd->hp_unit].isr_arg = hd->hp_unit;
    222   1.1      cgd 	ler0->ler0_id = 0xFF;
    223   1.1      cgd 	DELAY(100);
    224   1.1      cgd 
    225   1.1      cgd 	/*
    226   1.1      cgd 	 * Read the ethernet address off the board, one nibble at a time.
    227   1.1      cgd 	 */
    228   1.1      cgd 	cp = (char *)(lestd[3] + (int)hd->hp_addr);
    229  1.13  mycroft 	for (i = 0; i < sizeof(sc->sc_arpcom.ac_enaddr); i++) {
    230  1.13  mycroft 		sc->sc_arpcom.ac_enaddr[i] = (*++cp & 0xF) << 4;
    231   1.1      cgd 		cp++;
    232  1.13  mycroft 		sc->sc_arpcom.ac_enaddr[i] |= *++cp & 0xF;
    233   1.1      cgd 		cp++;
    234   1.1      cgd 	}
    235  1.23  thorpej 	printf(": hardware address %s\n",
    236  1.23  thorpej 	    ether_sprintf(sc->sc_arpcom.ac_enaddr));
    237   1.1      cgd 
    238   1.1      cgd 	isrlink(&le_isr[hd->hp_unit]);
    239   1.1      cgd 	ler0->ler0_status = LE_IE;
    240   1.1      cgd 
    241   1.1      cgd 	ifp->if_unit = hd->hp_unit;
    242   1.1      cgd 	ifp->if_name = "le";
    243   1.1      cgd 	ifp->if_output = ether_output;
    244   1.1      cgd 	ifp->if_start = lestart;
    245  1.13  mycroft 	ifp->if_ioctl = leioctl;
    246  1.13  mycroft 	ifp->if_watchdog = lewatchdog;
    247  1.10  mycroft 	ifp->if_flags =
    248  1.10  mycroft 	    IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS | IFF_MULTICAST;
    249  1.13  mycroft 
    250  1.12  mycroft 	if_attach(ifp);
    251  1.12  mycroft 	ether_ifattach(ifp);
    252  1.13  mycroft 
    253   1.1      cgd #if NBPFILTER > 0
    254  1.12  mycroft 	bpfattach(&ifp->if_bpf, ifp, DLT_EN10MB, sizeof(struct ether_header));
    255   1.1      cgd #endif
    256   1.1      cgd }
    257   1.1      cgd 
    258   1.5  mycroft void
    259  1.13  mycroft lereset(sc)
    260  1.13  mycroft 	struct le_softc *sc;
    261  1.13  mycroft {
    262  1.13  mycroft 
    263  1.13  mycroft 	leinit(sc);
    264  1.13  mycroft }
    265  1.13  mycroft 
    266  1.21  mycroft void
    267  1.13  mycroft lewatchdog(unit)
    268  1.21  mycroft 	int unit;
    269   1.5  mycroft {
    270  1.13  mycroft 	struct le_softc *sc = &le_softc[unit];
    271   1.5  mycroft 
    272  1.23  thorpej 	log(LOG_ERR, "%s: device timeout\n", sc->sc_hd->hp_xname);
    273  1.13  mycroft 	++sc->sc_arpcom.ac_if.if_oerrors;
    274  1.21  mycroft 
    275  1.13  mycroft 	lereset(sc);
    276  1.13  mycroft }
    277   1.5  mycroft 
    278  1.13  mycroft #define	LANCE_ADDR(sc, a) \
    279  1.13  mycroft 	((u_long)(a) - (u_long)sc->sc_mem)
    280   1.9  mycroft 
    281  1.13  mycroft /* LANCE initialization block set up. */
    282  1.13  mycroft void
    283  1.13  mycroft lememinit(sc)
    284  1.13  mycroft 	register struct le_softc *sc;
    285  1.13  mycroft {
    286  1.13  mycroft 	struct ifnet *ifp = &sc->sc_arpcom.ac_if;
    287  1.13  mycroft 	int i;
    288  1.13  mycroft 	void *mem;
    289  1.13  mycroft 	u_long a;
    290   1.5  mycroft 
    291  1.13  mycroft 	/*
    292  1.13  mycroft 	 * At this point we assume that the memory allocated to the Lance is
    293  1.13  mycroft 	 * quadword aligned.  If it isn't then the initialisation is going
    294  1.13  mycroft 	 * fail later on.
    295  1.13  mycroft 	 */
    296  1.13  mycroft 	mem = sc->sc_mem;
    297   1.5  mycroft 
    298  1.13  mycroft 	sc->sc_init = mem;
    299  1.13  mycroft #if NBPFILTER > 0
    300  1.13  mycroft 	if (ifp->if_flags & IFF_PROMISC)
    301  1.13  mycroft 		sc->sc_init->mode = LE_NORMAL | LE_PROM;
    302  1.13  mycroft 	else
    303  1.13  mycroft #endif
    304  1.13  mycroft 		sc->sc_init->mode = LE_NORMAL;
    305  1.13  mycroft 	for (i = 0; i < ETHER_ADDR_LEN; i++)
    306  1.13  mycroft 		sc->sc_init->padr[i] = sc->sc_arpcom.ac_enaddr[i^1];
    307  1.13  mycroft 	lesetladrf(&sc->sc_arpcom, sc->sc_init->ladrf);
    308  1.13  mycroft 	mem += sizeof(struct init_block);
    309  1.13  mycroft 
    310  1.13  mycroft 	sc->sc_rd = mem;
    311  1.13  mycroft 	a = LANCE_ADDR(sc, mem);
    312  1.13  mycroft 	sc->sc_init->rdra = a;
    313  1.13  mycroft 	sc->sc_init->rlen = ((a >> 16) & 0xff) | (RLEN << 13);
    314  1.13  mycroft 	mem += NRBUF * sizeof(struct mds);
    315  1.13  mycroft 
    316  1.13  mycroft 	sc->sc_td = mem;
    317  1.13  mycroft 	a = LANCE_ADDR(sc, mem);
    318  1.13  mycroft 	sc->sc_init->tdra = a;
    319  1.13  mycroft 	sc->sc_init->tlen = ((a >> 16) & 0xff) | (TLEN << 13);
    320  1.13  mycroft 	mem += NTBUF * sizeof(struct mds);
    321   1.5  mycroft 
    322  1.13  mycroft 	/*
    323  1.13  mycroft 	 * Set up receive ring descriptors.
    324  1.13  mycroft 	 */
    325  1.13  mycroft 	sc->sc_rbuf = mem;
    326  1.13  mycroft 	for (i = 0; i < NRBUF; i++) {
    327  1.13  mycroft 		a = LANCE_ADDR(sc, mem);
    328  1.13  mycroft 		sc->sc_rd[i].addr = a;
    329  1.13  mycroft 		sc->sc_rd[i].flags = ((a >> 16) & 0xff) | LE_OWN;
    330  1.13  mycroft 		sc->sc_rd[i].bcnt = -BUFSIZE;
    331  1.13  mycroft 		sc->sc_rd[i].mcnt = 0;
    332  1.13  mycroft 		mem += BUFSIZE;
    333   1.5  mycroft 	}
    334   1.5  mycroft 
    335  1.13  mycroft 	/*
    336  1.13  mycroft 	 * Set up transmit ring descriptors.
    337  1.13  mycroft 	 */
    338  1.13  mycroft 	sc->sc_tbuf = mem;
    339  1.13  mycroft 	for (i = 0; i < NTBUF; i++) {
    340  1.13  mycroft 		a = LANCE_ADDR(sc, mem);
    341  1.13  mycroft 		sc->sc_td[i].addr = a;
    342  1.13  mycroft 		sc->sc_td[i].flags= ((a >> 16) & 0xff);
    343  1.13  mycroft 		sc->sc_td[i].bcnt = 0xf000;
    344  1.13  mycroft 		sc->sc_td[i].mcnt = 0;
    345  1.13  mycroft 		mem += BUFSIZE;
    346   1.6  mycroft 	}
    347   1.6  mycroft }
    348   1.6  mycroft 
    349   1.8  mycroft void
    350  1.13  mycroft lestop(sc)
    351  1.13  mycroft 	struct le_softc *sc;
    352   1.1      cgd {
    353   1.1      cgd 
    354  1.13  mycroft 	lewrcsr(sc, 0, LE_STOP);
    355   1.1      cgd }
    356   1.1      cgd 
    357   1.1      cgd /*
    358  1.13  mycroft  * Initialization of interface; set up initialization block
    359  1.13  mycroft  * and transmit/receive descriptor rings.
    360   1.1      cgd  */
    361   1.8  mycroft void
    362  1.13  mycroft leinit(sc)
    363  1.13  mycroft 	register struct le_softc *sc;
    364   1.1      cgd {
    365  1.13  mycroft 	struct ifnet *ifp = &sc->sc_arpcom.ac_if;
    366   1.1      cgd 	int s;
    367  1.13  mycroft 	register int timo;
    368  1.13  mycroft 	u_long a;
    369  1.13  mycroft 
    370  1.13  mycroft 	s = splimp();
    371  1.13  mycroft 
    372  1.13  mycroft 	/* Don't want to get in a weird state. */
    373  1.13  mycroft 	lewrcsr(sc, 0, LE_STOP);
    374  1.13  mycroft 	DELAY(100);
    375  1.13  mycroft 
    376  1.13  mycroft 	sc->sc_last_rd = sc->sc_last_td = sc->sc_no_td = 0;
    377  1.13  mycroft 
    378  1.13  mycroft 	/* Set up LANCE init block. */
    379  1.13  mycroft 	lememinit(sc);
    380  1.13  mycroft 
    381  1.13  mycroft 	/* Turn on byte swapping. */
    382  1.13  mycroft 	lewrcsr(sc, 3, LE_BSWP);
    383  1.13  mycroft 
    384  1.13  mycroft 	/* Give LANCE the physical address of its init block. */
    385  1.13  mycroft 	a = LANCE_ADDR(sc, sc->sc_init);
    386  1.13  mycroft 	lewrcsr(sc, 1, a);
    387  1.13  mycroft 	lewrcsr(sc, 2, (a >> 16) & 0xff);
    388  1.13  mycroft 
    389  1.13  mycroft 	/* Try to initialize the LANCE. */
    390  1.13  mycroft 	DELAY(100);
    391  1.13  mycroft 	lewrcsr(sc, 0, LE_INIT);
    392  1.13  mycroft 
    393  1.13  mycroft 	/* Wait for initialization to finish. */
    394  1.13  mycroft 	for (timo = 100000; timo; timo--)
    395  1.13  mycroft 		if (lerdcsr(sc, 0) & LE_IDON)
    396  1.13  mycroft 			break;
    397  1.13  mycroft 
    398  1.13  mycroft 	if (lerdcsr(sc, 0) & LE_IDON) {
    399  1.13  mycroft 		/* Start the LANCE. */
    400  1.13  mycroft 		lewrcsr(sc, 0, LE_INEA | LE_STRT | LE_IDON);
    401   1.1      cgd 		ifp->if_flags |= IFF_RUNNING;
    402  1.13  mycroft 		ifp->if_flags &= ~IFF_OACTIVE;
    403  1.13  mycroft 		lestart(ifp);
    404  1.13  mycroft 	} else
    405  1.23  thorpej 		printf("%s: card failed to initialize\n", sc->sc_hd->hp_xname);
    406  1.13  mycroft 
    407  1.13  mycroft 	(void) splx(s);
    408   1.1      cgd }
    409   1.1      cgd 
    410   1.1      cgd /*
    411  1.13  mycroft  * Controller interrupt.
    412  1.13  mycroft  */
    413   1.8  mycroft int
    414   1.1      cgd leintr(unit)
    415  1.13  mycroft 	int unit;
    416   1.1      cgd {
    417   1.5  mycroft 	register struct le_softc *sc = &le_softc[unit];
    418  1.13  mycroft 	register u_short isr;
    419  1.13  mycroft 
    420  1.13  mycroft 	isr = lerdcsr(sc, 0);
    421  1.13  mycroft #ifdef LEDEBUG
    422  1.13  mycroft 	if (sc->sc_debug)
    423  1.23  thorpej 		printf("%s: leintr entering with isr=%04x\n",
    424  1.23  thorpej 		    sc->sc_hd->hp_xname, isr);
    425  1.13  mycroft #endif
    426  1.13  mycroft 	if ((isr & LE_INTR) == 0)
    427  1.13  mycroft 		return 0;
    428  1.13  mycroft 
    429  1.13  mycroft 	do {
    430  1.13  mycroft 		lewrcsr(sc, 0,
    431  1.13  mycroft 		    isr & (LE_INEA | LE_BABL | LE_MISS | LE_MERR |
    432  1.13  mycroft 			   LE_RINT | LE_TINT | LE_IDON));
    433  1.13  mycroft 		if (isr & (LE_BABL | LE_CERR | LE_MISS | LE_MERR)) {
    434  1.13  mycroft 			if (isr & LE_BABL) {
    435  1.23  thorpej 				printf("%s: BABL\n", sc->sc_hd->hp_xname);
    436  1.13  mycroft 				sc->sc_arpcom.ac_if.if_oerrors++;
    437  1.13  mycroft 			}
    438  1.13  mycroft #if 0
    439  1.13  mycroft 			if (isr & LE_CERR) {
    440  1.23  thorpej 				printf("%s: CERR\n", sc->sc_hd->hp_xname);
    441  1.13  mycroft 				sc->sc_arpcom.ac_if.if_collisions++;
    442  1.13  mycroft 			}
    443  1.13  mycroft #endif
    444  1.13  mycroft 			if (isr & LE_MISS) {
    445  1.16  mycroft #if 0
    446  1.23  thorpej 				printf("%s: MISS\n", sc->sc_hd->hp_xname);
    447  1.16  mycroft #endif
    448  1.13  mycroft 				sc->sc_arpcom.ac_if.if_ierrors++;
    449  1.13  mycroft 			}
    450  1.13  mycroft 			if (isr & LE_MERR) {
    451  1.23  thorpej 				printf("%s: MERR\n", sc->sc_hd->hp_xname);
    452  1.13  mycroft 				lereset(sc);
    453  1.13  mycroft 				goto out;
    454  1.13  mycroft 			}
    455  1.13  mycroft 		}
    456   1.1      cgd 
    457  1.13  mycroft 		if ((isr & LE_RXON) == 0) {
    458  1.23  thorpej 			printf("%s: receiver disabled\n", sc->sc_hd->hp_xname);
    459  1.13  mycroft 			sc->sc_arpcom.ac_if.if_ierrors++;
    460  1.13  mycroft 			lereset(sc);
    461  1.13  mycroft 			goto out;
    462  1.13  mycroft 		}
    463  1.13  mycroft 		if ((isr & LE_TXON) == 0) {
    464  1.23  thorpej 			printf("%s: transmitter disabled\n",
    465  1.23  thorpej 			    sc->sc_hd->hp_xname);
    466  1.13  mycroft 			sc->sc_arpcom.ac_if.if_oerrors++;
    467   1.5  mycroft 			lereset(sc);
    468  1.13  mycroft 			goto out;
    469  1.13  mycroft 		}
    470  1.13  mycroft 
    471  1.13  mycroft 		if (isr & LE_RINT) {
    472  1.13  mycroft 			/* Reset watchdog timer. */
    473  1.13  mycroft 			sc->sc_arpcom.ac_if.if_timer = 0;
    474  1.13  mycroft 			lerint(unit);
    475  1.13  mycroft 		}
    476  1.13  mycroft 		if (isr & LE_TINT) {
    477  1.13  mycroft 			/* Reset watchdog timer. */
    478  1.13  mycroft 			sc->sc_arpcom.ac_if.if_timer = 0;
    479  1.13  mycroft 			letint(unit);
    480   1.1      cgd 		}
    481  1.13  mycroft 
    482  1.13  mycroft 		isr = lerdcsr(sc, 0);
    483  1.13  mycroft 	} while ((isr & LE_INTR) != 0);
    484  1.13  mycroft 
    485  1.13  mycroft #ifdef LEDEBUG
    486  1.13  mycroft 	if (sc->sc_debug)
    487  1.23  thorpej 		printf("%s: leintr returning with isr=%04x\n",
    488  1.23  thorpej 		    sc->sc_hd->hp_xname, isr);
    489  1.13  mycroft #endif
    490  1.13  mycroft 
    491  1.13  mycroft out:
    492  1.13  mycroft 	return 1;
    493   1.1      cgd }
    494   1.1      cgd 
    495  1.13  mycroft #define NEXTTDS \
    496  1.13  mycroft 	if (++tmd == NTBUF) tmd=0, cdm=sc->sc_td; else ++cdm
    497  1.13  mycroft 
    498  1.15  mycroft /*
    499  1.15  mycroft  * Setup output on interface.
    500  1.15  mycroft  * Get another datagram to send off of the interface queue, and map it to the
    501  1.15  mycroft  * interface before starting the output.
    502  1.15  mycroft  * Called only at splimp or interrupt level.
    503  1.15  mycroft  */
    504  1.21  mycroft void
    505  1.15  mycroft lestart(ifp)
    506  1.15  mycroft 	struct ifnet *ifp;
    507  1.15  mycroft {
    508  1.15  mycroft 	register struct le_softc *sc = &le_softc[ifp->if_unit];
    509  1.15  mycroft 	register int tmd;
    510  1.15  mycroft 	struct mds *cdm;
    511  1.15  mycroft 	struct mbuf *m0, *m;
    512  1.15  mycroft 	u_char *buffer;
    513  1.15  mycroft 	int len;
    514  1.15  mycroft 
    515  1.15  mycroft 	if ((sc->sc_arpcom.ac_if.if_flags & (IFF_RUNNING | IFF_OACTIVE)) !=
    516  1.15  mycroft 	    IFF_RUNNING)
    517  1.15  mycroft 		return;
    518  1.15  mycroft 
    519  1.15  mycroft 	tmd = sc->sc_last_td;
    520  1.15  mycroft 	cdm = &sc->sc_td[tmd];
    521  1.15  mycroft 
    522  1.15  mycroft 	for (;;) {
    523  1.15  mycroft 		if (sc->sc_no_td >= NTBUF) {
    524  1.15  mycroft 			sc->sc_arpcom.ac_if.if_flags |= IFF_OACTIVE;
    525  1.15  mycroft #ifdef LEDEBUG
    526  1.15  mycroft 			if (sc->sc_debug)
    527  1.15  mycroft 				printf("no_td = %d, last_td = %d\n", sc->sc_no_td,
    528  1.15  mycroft 				    sc->sc_last_td);
    529  1.15  mycroft #endif
    530  1.15  mycroft 			break;
    531  1.15  mycroft 		}
    532  1.15  mycroft 
    533  1.15  mycroft #ifdef LEDEBUG
    534  1.15  mycroft 		if (cdm->flags & LE_OWN) {
    535  1.15  mycroft 			sc->sc_arpcom.ac_if.if_flags |= IFF_OACTIVE;
    536  1.15  mycroft 			printf("missing buffer, no_td = %d, last_td = %d\n",
    537  1.15  mycroft 			    sc->sc_no_td, sc->sc_last_td);
    538  1.15  mycroft 		}
    539  1.15  mycroft #endif
    540  1.15  mycroft 
    541  1.15  mycroft 		IF_DEQUEUE(&sc->sc_arpcom.ac_if.if_snd, m);
    542  1.15  mycroft 		if (!m)
    543  1.15  mycroft 			break;
    544  1.15  mycroft 
    545  1.15  mycroft 		++sc->sc_no_td;
    546  1.15  mycroft 
    547  1.15  mycroft 		/*
    548  1.15  mycroft 		 * Copy the mbuf chain into the transmit buffer.
    549  1.15  mycroft 		 */
    550  1.15  mycroft 		buffer = sc->sc_tbuf + (BUFSIZE * sc->sc_last_td);
    551  1.15  mycroft 		len = 0;
    552  1.15  mycroft 		for (m0 = m; m; m = m->m_next) {
    553  1.15  mycroft 			bcopy(mtod(m, caddr_t), buffer, m->m_len);
    554  1.15  mycroft 			buffer += m->m_len;
    555  1.15  mycroft 			len += m->m_len;
    556  1.15  mycroft 		}
    557  1.15  mycroft 
    558  1.15  mycroft #ifdef LEDEBUG
    559  1.15  mycroft 		if (len > ETHER_MAX_LEN)
    560  1.15  mycroft 			printf("packet length %d\n", len);
    561  1.15  mycroft #endif
    562  1.15  mycroft 
    563  1.15  mycroft #if NBPFILTER > 0
    564  1.15  mycroft 		if (sc->sc_arpcom.ac_if.if_bpf)
    565  1.15  mycroft 			bpf_mtap(sc->sc_arpcom.ac_if.if_bpf, m0);
    566  1.15  mycroft #endif
    567  1.15  mycroft 
    568  1.15  mycroft 		m_freem(m0);
    569  1.15  mycroft 		len = max(len, ETHER_MIN_LEN);
    570  1.15  mycroft 
    571  1.15  mycroft 		/*
    572  1.15  mycroft 		 * Init transmit registers, and set transmit start flag.
    573  1.15  mycroft 		 */
    574  1.15  mycroft 		cdm->bcnt = -len;
    575  1.15  mycroft 		cdm->mcnt = 0;
    576  1.16  mycroft 		cdm->flags |= LE_OWN | LE_STP | LE_ENP;
    577  1.15  mycroft 
    578  1.15  mycroft #ifdef LEDEBUG
    579  1.15  mycroft 		if (sc->sc_debug)
    580  1.15  mycroft 			xmit_print(sc, sc->sc_last_td);
    581  1.15  mycroft #endif
    582  1.15  mycroft 
    583  1.15  mycroft 		lewrcsr(sc, 0, LE_INEA | LE_TDMD);
    584  1.15  mycroft 
    585  1.15  mycroft 		NEXTTDS;
    586  1.15  mycroft 	}
    587  1.15  mycroft 
    588  1.15  mycroft 	sc->sc_last_td = tmd;
    589  1.15  mycroft }
    590  1.15  mycroft 
    591   1.8  mycroft void
    592  1.13  mycroft letint(unit)
    593  1.13  mycroft 	int unit;
    594   1.1      cgd {
    595  1.13  mycroft 	register struct le_softc *sc = &le_softc[unit];
    596  1.13  mycroft 	register int tmd = (sc->sc_last_td - sc->sc_no_td + NTBUF) % NTBUF;
    597  1.13  mycroft 	struct mds *cdm = &sc->sc_td[tmd];
    598   1.1      cgd 
    599  1.18  mycroft #ifdef USELEDS
    600  1.18  mycroft 	if (inledcontrol == 0)
    601  1.18  mycroft 		ledcontrol(0, 0, LED_LANXMT);
    602  1.18  mycroft #endif
    603  1.18  mycroft 
    604  1.13  mycroft 	if (cdm->flags & LE_OWN) {
    605  1.13  mycroft 		/* Race condition with loop below. */
    606  1.13  mycroft #ifdef LEDEBUG
    607  1.13  mycroft 		if (sc->sc_debug)
    608  1.23  thorpej 			printf("%s: extra tint\n", sc->sc_hd->hp_xname);
    609  1.13  mycroft #endif
    610   1.1      cgd 		return;
    611   1.1      cgd 	}
    612  1.13  mycroft 
    613  1.13  mycroft 	sc->sc_arpcom.ac_if.if_flags &= ~IFF_OACTIVE;
    614  1.13  mycroft 
    615   1.6  mycroft 	do {
    616  1.13  mycroft 		if (sc->sc_no_td <= 0)
    617  1.13  mycroft 			break;
    618  1.13  mycroft #ifdef LEDEBUG
    619  1.13  mycroft 		if (sc->sc_debug)
    620  1.13  mycroft 			printf("trans cdm = %x\n", cdm);
    621  1.13  mycroft #endif
    622  1.13  mycroft 		sc->sc_arpcom.ac_if.if_opackets++;
    623  1.13  mycroft 		--sc->sc_no_td;
    624  1.17  mycroft 		if (cdm->mcnt & (LE_TBUFF | LE_UFLO | LE_LCOL | LE_LCAR | LE_RTRY)) {
    625  1.17  mycroft 			if (cdm->mcnt & LE_TBUFF)
    626  1.23  thorpej 				printf("%s: TBUFF\n", sc->sc_hd->hp_xname);
    627  1.17  mycroft 			if ((cdm->mcnt & (LE_TBUFF | LE_UFLO)) == LE_UFLO)
    628  1.23  thorpej 				printf("%s: UFLO\n", sc->sc_hd->hp_xname);
    629  1.17  mycroft 			if (cdm->mcnt & LE_UFLO) {
    630  1.13  mycroft 				lereset(sc);
    631  1.13  mycroft 				return;
    632  1.13  mycroft 			}
    633  1.13  mycroft #if 0
    634  1.17  mycroft 			if (cdm->mcnt & LE_LCOL) {
    635  1.23  thorpej 				printf("%s: late collision\n",
    636  1.23  thorpej 				    sc->sc_hd->hp_xname);
    637  1.13  mycroft 				sc->sc_arpcom.ac_if.if_collisions++;
    638  1.13  mycroft 			}
    639  1.17  mycroft 			if (cdm->mcnt & LE_LCAR)
    640  1.23  thorpej 				printf("%s: lost carrier\n",
    641  1.23  thorpej 				    sc->sc_hd->hp_xname);
    642  1.17  mycroft 			if (cdm->mcnt & LE_RTRY) {
    643  1.23  thorpej 				printf("%s: excessive collisions, tdr %d\n",
    644  1.23  thorpej 				    sc->sc_hd->hp_xname, cdm->mcnt & 0x1ff);
    645  1.17  mycroft 				sc->sc_arpcom.ac_if.if_collisions += 16;
    646  1.13  mycroft 			}
    647  1.13  mycroft #endif
    648  1.17  mycroft 		} else if (cdm->flags & LE_ONE)
    649  1.17  mycroft 			sc->sc_arpcom.ac_if.if_collisions++;
    650  1.17  mycroft 		else if (cdm->flags & LE_MORE)
    651  1.17  mycroft 			/* Real number is unknown. */
    652  1.17  mycroft 			sc->sc_arpcom.ac_if.if_collisions += 2;
    653  1.13  mycroft 		NEXTTDS;
    654  1.13  mycroft 	} while ((cdm->flags & LE_OWN) == 0);
    655   1.6  mycroft 
    656  1.13  mycroft 	lestart(&sc->sc_arpcom.ac_if);
    657  1.13  mycroft }
    658   1.6  mycroft 
    659  1.13  mycroft #define NEXTRDS \
    660  1.13  mycroft 	if (++rmd == NRBUF) rmd=0, cdm=sc->sc_rd; else ++cdm
    661  1.13  mycroft 
    662  1.13  mycroft /* only called from one place, so may as well integrate */
    663   1.8  mycroft void
    664  1.13  mycroft lerint(unit)
    665  1.13  mycroft 	int unit;
    666   1.1      cgd {
    667  1.13  mycroft 	register struct le_softc *sc = &le_softc[unit];
    668  1.13  mycroft 	register int rmd = sc->sc_last_rd;
    669  1.13  mycroft 	struct mds *cdm = &sc->sc_rd[rmd];
    670  1.18  mycroft 
    671  1.18  mycroft #ifdef USELEDS
    672  1.18  mycroft 	if (inledcontrol == 0)
    673  1.18  mycroft 		ledcontrol(0, 0, LED_LANRCV);
    674  1.18  mycroft #endif
    675   1.1      cgd 
    676  1.13  mycroft 	if (cdm->flags & LE_OWN) {
    677  1.13  mycroft 		/* Race condition with loop below. */
    678  1.13  mycroft #ifdef LEDEBUG
    679  1.13  mycroft 		if (sc->sc_debug)
    680  1.23  thorpej 			printf("%s: extra rint\n", sc->sc_hd->hp_xname);
    681  1.13  mycroft #endif
    682  1.13  mycroft 		return;
    683   1.1      cgd 	}
    684   1.1      cgd 
    685  1.13  mycroft 	/* Process all buffers with valid data. */
    686  1.13  mycroft 	do {
    687  1.13  mycroft 		if (cdm->flags & (LE_FRAM | LE_OFLO | LE_CRC | LE_RBUFF)) {
    688  1.13  mycroft 			if ((cdm->flags & (LE_FRAM | LE_OFLO | LE_ENP)) == (LE_FRAM | LE_ENP))
    689  1.23  thorpej 				printf("%s: FRAM\n", sc->sc_hd->hp_xname);
    690  1.13  mycroft 			if ((cdm->flags & (LE_OFLO | LE_ENP)) == LE_OFLO)
    691  1.23  thorpej 				printf("%s: OFLO\n", sc->sc_hd->hp_xname);
    692  1.13  mycroft 			if ((cdm->flags & (LE_CRC | LE_OFLO | LE_ENP)) == (LE_CRC | LE_ENP))
    693  1.23  thorpej 				printf("%s: CRC\n", sc->sc_hd->hp_xname);
    694  1.13  mycroft 			if (cdm->flags & LE_RBUFF)
    695  1.23  thorpej 				printf("%s: RBUFF\n", sc->sc_hd->hp_xname);
    696  1.13  mycroft 		} else if (cdm->flags & (LE_STP | LE_ENP) != (LE_STP | LE_ENP)) {
    697   1.1      cgd 			do {
    698  1.13  mycroft 				cdm->mcnt = 0;
    699  1.16  mycroft 				cdm->flags |= LE_OWN;
    700  1.13  mycroft 				NEXTRDS;
    701  1.13  mycroft 			} while ((cdm->flags & (LE_OWN | LE_ERR | LE_STP | LE_ENP)) == 0);
    702  1.13  mycroft 			sc->sc_last_rd = rmd;
    703  1.23  thorpej 			printf("%s: chained buffer\n", sc->sc_hd->hp_xname);
    704  1.13  mycroft 			if ((cdm->flags & (LE_OWN | LE_ERR | LE_STP | LE_ENP)) != LE_ENP) {
    705   1.5  mycroft 				lereset(sc);
    706   1.1      cgd 				return;
    707   1.1      cgd 			}
    708  1.13  mycroft 		} else {
    709  1.13  mycroft #ifdef LEDEBUG
    710  1.13  mycroft 			if (sc->sc_debug)
    711  1.13  mycroft 				recv_print(sc, sc->sc_last_rd);
    712  1.13  mycroft #endif
    713  1.13  mycroft 			leread(sc, sc->sc_rbuf + (BUFSIZE * rmd),
    714  1.13  mycroft 			    (int)cdm->mcnt);
    715  1.13  mycroft 			sc->sc_arpcom.ac_if.if_ipackets++;
    716  1.13  mycroft 		}
    717  1.13  mycroft 
    718  1.13  mycroft 		cdm->mcnt = 0;
    719  1.16  mycroft 		cdm->flags |= LE_OWN;
    720  1.13  mycroft 		NEXTRDS;
    721  1.13  mycroft #ifdef LEDEBUG
    722  1.13  mycroft 		if (sc->sc_debug)
    723  1.13  mycroft 			printf("sc->sc_last_rd = %x, cdm = %x\n",
    724  1.13  mycroft 			    sc->sc_last_rd, cdm);
    725  1.13  mycroft #endif
    726  1.13  mycroft 	} while ((cdm->flags & LE_OWN) == 0);
    727  1.13  mycroft 
    728  1.13  mycroft 	sc->sc_last_rd = rmd;
    729   1.1      cgd }
    730   1.1      cgd 
    731  1.13  mycroft /*
    732  1.13  mycroft  * Pass a packet to the higher levels.
    733  1.13  mycroft  */
    734   1.8  mycroft void
    735   1.5  mycroft leread(sc, buf, len)
    736   1.5  mycroft 	register struct le_softc *sc;
    737  1.13  mycroft 	u_char *buf;
    738   1.1      cgd 	int len;
    739   1.1      cgd {
    740  1.16  mycroft 	struct ifnet *ifp;
    741  1.16  mycroft 	struct mbuf *m;
    742  1.13  mycroft 	struct ether_header *eh;
    743  1.13  mycroft 
    744  1.16  mycroft 	len -= 4;
    745  1.13  mycroft 	if (len <= 0)
    746  1.13  mycroft 		return;
    747   1.1      cgd 
    748  1.13  mycroft 	/* Pull packet off interface. */
    749  1.16  mycroft 	ifp = &sc->sc_arpcom.ac_if;
    750  1.16  mycroft 	m = leget(buf, len, ifp);
    751  1.13  mycroft 	if (m == 0)
    752   1.1      cgd 		return;
    753   1.5  mycroft 
    754  1.16  mycroft 	/* We assume that the header fit entirely in one mbuf. */
    755  1.16  mycroft 	eh = mtod(m, struct ether_header *);
    756  1.16  mycroft 
    757   1.1      cgd #if NBPFILTER > 0
    758   1.1      cgd 	/*
    759  1.13  mycroft 	 * Check if there's a BPF listener on this interface.
    760  1.13  mycroft 	 * If so, hand off the raw packet to BPF.
    761   1.1      cgd 	 */
    762  1.16  mycroft 	if (ifp->if_bpf) {
    763  1.16  mycroft 		bpf_mtap(ifp->if_bpf, m);
    764  1.13  mycroft 
    765  1.13  mycroft 		/*
    766  1.13  mycroft 		 * Note that the interface cannot be in promiscuous mode if
    767  1.13  mycroft 		 * there are no BPF listeners.  And if we are in promiscuous
    768  1.13  mycroft 		 * mode, we have to check if this packet is really ours.
    769  1.13  mycroft 		 */
    770  1.16  mycroft 		if ((ifp->if_flags & IFF_PROMISC) &&
    771  1.13  mycroft 		    (eh->ether_dhost[0] & 1) == 0 && /* !mcast and !bcast */
    772  1.13  mycroft 		    bcmp(eh->ether_dhost, sc->sc_arpcom.ac_enaddr,
    773  1.13  mycroft 			    sizeof(eh->ether_dhost)) != 0) {
    774  1.13  mycroft 			m_freem(m);
    775   1.1      cgd 			return;
    776  1.13  mycroft 		}
    777   1.1      cgd 	}
    778   1.1      cgd #endif
    779   1.5  mycroft 
    780  1.16  mycroft 	/* We assume that the header fit entirely in one mbuf. */
    781  1.16  mycroft 	m->m_pkthdr.len -= sizeof(*eh);
    782  1.16  mycroft 	m->m_len -= sizeof(*eh);
    783  1.16  mycroft 	m->m_data += sizeof(*eh);
    784  1.16  mycroft 
    785  1.16  mycroft 	ether_input(ifp, eh, m);
    786   1.1      cgd }
    787   1.1      cgd 
    788   1.1      cgd /*
    789  1.13  mycroft  * Supporting routines
    790   1.1      cgd  */
    791   1.1      cgd 
    792   1.1      cgd /*
    793  1.13  mycroft  * Pull data off an interface.
    794  1.13  mycroft  * Len is length of data, with local net header stripped.
    795  1.13  mycroft  * We copy the data into mbufs.  When full cluster sized units are present
    796  1.13  mycroft  * we copy into clusters.
    797   1.1      cgd  */
    798   1.1      cgd struct mbuf *
    799  1.13  mycroft leget(buf, totlen, ifp)
    800  1.13  mycroft 	u_char *buf;
    801  1.13  mycroft 	int totlen;
    802   1.1      cgd 	struct ifnet *ifp;
    803   1.1      cgd {
    804  1.16  mycroft 	struct mbuf *top, **mp, *m;
    805  1.13  mycroft 	int len;
    806   1.1      cgd 
    807   1.1      cgd 	MGETHDR(m, M_DONTWAIT, MT_DATA);
    808   1.1      cgd 	if (m == 0)
    809  1.13  mycroft 		return 0;
    810   1.1      cgd 	m->m_pkthdr.rcvif = ifp;
    811   1.1      cgd 	m->m_pkthdr.len = totlen;
    812  1.16  mycroft 	len = MHLEN;
    813  1.13  mycroft 	top = 0;
    814  1.13  mycroft 	mp = &top;
    815   1.1      cgd 
    816   1.1      cgd 	while (totlen > 0) {
    817   1.1      cgd 		if (top) {
    818   1.1      cgd 			MGET(m, M_DONTWAIT, MT_DATA);
    819   1.1      cgd 			if (m == 0) {
    820   1.1      cgd 				m_freem(top);
    821  1.13  mycroft 				return 0;
    822   1.1      cgd 			}
    823  1.16  mycroft 			len = MLEN;
    824   1.1      cgd 		}
    825  1.16  mycroft 		if (totlen >= MINCLSIZE) {
    826   1.1      cgd 			MCLGET(m, M_DONTWAIT);
    827   1.1      cgd 			if (m->m_flags & M_EXT)
    828  1.16  mycroft 				len = MCLBYTES;
    829   1.1      cgd 		}
    830  1.16  mycroft 		m->m_len = len = min(totlen, len);
    831  1.16  mycroft 		bcopy((caddr_t)buf, mtod(m, caddr_t), len);
    832  1.16  mycroft 		buf += len;
    833  1.16  mycroft 		totlen -= len;
    834   1.1      cgd 		*mp = m;
    835   1.1      cgd 		mp = &m->m_next;
    836   1.1      cgd 	}
    837  1.13  mycroft 
    838  1.13  mycroft 	return top;
    839   1.1      cgd }
    840   1.1      cgd 
    841   1.1      cgd /*
    842   1.1      cgd  * Process an ioctl request.
    843   1.1      cgd  */
    844   1.8  mycroft int
    845   1.1      cgd leioctl(ifp, cmd, data)
    846   1.1      cgd 	register struct ifnet *ifp;
    847  1.21  mycroft 	u_long cmd;
    848   1.1      cgd 	caddr_t data;
    849   1.1      cgd {
    850   1.5  mycroft 	struct le_softc *sc = &le_softc[ifp->if_unit];
    851  1.13  mycroft 	struct ifaddr *ifa = (struct ifaddr *)data;
    852  1.13  mycroft 	struct ifreq *ifr = (struct ifreq *)data;
    853  1.13  mycroft 	int s, error = 0;
    854  1.13  mycroft 
    855  1.13  mycroft 	s = splimp();
    856   1.1      cgd 
    857   1.1      cgd 	switch (cmd) {
    858   1.1      cgd 
    859   1.1      cgd 	case SIOCSIFADDR:
    860   1.1      cgd 		ifp->if_flags |= IFF_UP;
    861  1.13  mycroft 
    862   1.1      cgd 		switch (ifa->ifa_addr->sa_family) {
    863   1.1      cgd #ifdef INET
    864   1.1      cgd 		case AF_INET:
    865  1.21  mycroft 			leinit(sc);
    866  1.21  mycroft 			arp_ifinit(&sc->sc_arpcom, ifa);
    867   1.1      cgd 			break;
    868   1.1      cgd #endif
    869   1.1      cgd #ifdef NS
    870  1.13  mycroft 		/* XXX - This code is probably wrong. */
    871   1.1      cgd 		case AF_NS:
    872   1.1      cgd 		    {
    873  1.13  mycroft 			register struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
    874   1.1      cgd 
    875   1.1      cgd 			if (ns_nullhost(*ina))
    876  1.13  mycroft 				ina->x_host =
    877  1.13  mycroft 				    *(union ns_host *)(sc->sc_arpcom.ac_enaddr);
    878  1.13  mycroft 			else
    879  1.13  mycroft 				bcopy(ina->x_host.c_host,
    880  1.13  mycroft 				    sc->sc_arpcom.ac_enaddr,
    881  1.13  mycroft 				    sizeof(sc->sc_arpcom.ac_enaddr));
    882  1.13  mycroft 			/* Set new address. */
    883  1.13  mycroft 			leinit(sc);
    884   1.1      cgd 			break;
    885   1.1      cgd 		    }
    886   1.1      cgd #endif
    887   1.1      cgd 		default:
    888  1.13  mycroft 			leinit(sc);
    889   1.1      cgd 			break;
    890   1.1      cgd 		}
    891   1.1      cgd 		break;
    892   1.1      cgd 
    893   1.1      cgd 	case SIOCSIFFLAGS:
    894  1.13  mycroft 		/*
    895  1.13  mycroft 		 * If interface is marked down and it is running, then stop it
    896  1.13  mycroft 		 */
    897   1.1      cgd 		if ((ifp->if_flags & IFF_UP) == 0 &&
    898  1.13  mycroft 		    (ifp->if_flags & IFF_RUNNING) != 0) {
    899  1.13  mycroft 			/*
    900  1.13  mycroft 			 * If interface is marked down and it is running, then
    901  1.13  mycroft 			 * stop it.
    902  1.13  mycroft 			 */
    903  1.13  mycroft 			lestop(sc);
    904   1.6  mycroft 			ifp->if_flags &= ~IFF_RUNNING;
    905  1.13  mycroft 		} else if ((ifp->if_flags & IFF_UP) != 0 &&
    906  1.13  mycroft 		    	   (ifp->if_flags & IFF_RUNNING) == 0) {
    907  1.13  mycroft 			/*
    908  1.13  mycroft 			 * If interface is marked up and it is stopped, then
    909  1.13  mycroft 			 * start it.
    910  1.13  mycroft 			 */
    911  1.13  mycroft 			leinit(sc);
    912  1.13  mycroft 		} else {
    913  1.13  mycroft 			/*
    914  1.13  mycroft 			 * Reset the interface to pick up changes in any other
    915  1.13  mycroft 			 * flags that affect hardware registers.
    916  1.13  mycroft 			 */
    917  1.13  mycroft 			/*lestop(sc);*/
    918  1.13  mycroft 			leinit(sc);
    919   1.1      cgd 		}
    920  1.13  mycroft #ifdef LEDEBUG
    921  1.13  mycroft 		if (ifp->if_flags & IFF_DEBUG)
    922  1.13  mycroft 			sc->sc_debug = 1;
    923  1.13  mycroft 		else
    924  1.13  mycroft 			sc->sc_debug = 0;
    925  1.13  mycroft #endif
    926   1.1      cgd 		break;
    927   1.1      cgd 
    928   1.5  mycroft 	case SIOCADDMULTI:
    929  1.13  mycroft 	case SIOCDELMULTI:
    930  1.13  mycroft 		error = (cmd == SIOCADDMULTI) ?
    931  1.13  mycroft 		    ether_addmulti(ifr, &sc->sc_arpcom):
    932  1.13  mycroft 		    ether_delmulti(ifr, &sc->sc_arpcom);
    933   1.5  mycroft 
    934   1.5  mycroft 		if (error == ENETRESET) {
    935   1.5  mycroft 			/*
    936  1.13  mycroft 			 * Multicast list has changed; set the hardware filter
    937  1.13  mycroft 			 * accordingly.
    938   1.5  mycroft 			 */
    939  1.13  mycroft 			leinit(sc);
    940   1.5  mycroft 			error = 0;
    941   1.5  mycroft 		}
    942   1.5  mycroft 		break;
    943   1.5  mycroft 
    944   1.1      cgd 	default:
    945   1.1      cgd 		error = EINVAL;
    946   1.1      cgd 	}
    947  1.13  mycroft 	(void) splx(s);
    948  1.13  mycroft 	return error;
    949   1.1      cgd }
    950   1.1      cgd 
    951  1.13  mycroft #ifdef LEDEBUG
    952   1.8  mycroft void
    953  1.13  mycroft recv_print(sc, no)
    954  1.13  mycroft 	struct le_softc *sc;
    955  1.13  mycroft 	int no;
    956  1.13  mycroft {
    957  1.13  mycroft 	struct mds *rmd;
    958  1.13  mycroft 	int i, printed = 0;
    959  1.13  mycroft 	u_short len;
    960  1.13  mycroft 
    961  1.13  mycroft 	rmd = &sc->sc_rd[no];
    962  1.13  mycroft 	len = rmd->mcnt;
    963  1.13  mycroft 	printf("%s: receive buffer %d, len = %d\n", sc->sc_dev.dv_xname, no,
    964  1.13  mycroft 	    len);
    965  1.13  mycroft 	printf("%s: status %x\n", sc->sc_dev.dv_xname, lerdcsr(sc, 0));
    966  1.13  mycroft 	for (i = 0; i < len; i++) {
    967  1.13  mycroft 		if (!printed) {
    968  1.13  mycroft 			printed = 1;
    969  1.13  mycroft 			printf("%s: data: ", sc->sc_dev.dv_xname);
    970  1.13  mycroft 		}
    971  1.13  mycroft 		printf("%x ", *(sc->sc_rbuf + (BUFSIZE*no) + i));
    972  1.13  mycroft 	}
    973  1.13  mycroft 	if (printed)
    974  1.13  mycroft 		printf("\n");
    975  1.13  mycroft }
    976  1.13  mycroft 
    977  1.13  mycroft void
    978  1.13  mycroft xmit_print(sc, no)
    979  1.13  mycroft 	struct le_softc *sc;
    980  1.13  mycroft 	int no;
    981  1.13  mycroft {
    982  1.13  mycroft 	struct mds *rmd;
    983  1.13  mycroft 	int i, printed=0;
    984  1.13  mycroft 	u_short len;
    985  1.13  mycroft 
    986  1.13  mycroft 	rmd = &sc->sc_td[no];
    987  1.13  mycroft 	len = -rmd->bcnt;
    988  1.13  mycroft 	printf("%s: transmit buffer %d, len = %d\n", sc->sc_dev.dv_xname, no,
    989  1.13  mycroft 	    len);
    990  1.13  mycroft 	printf("%s: status %x\n", sc->sc_dev.dv_xname, lerdcsr(sc, 0));
    991  1.13  mycroft 	printf("%s: addr %x, flags %x, bcnt %x, mcnt %x\n",
    992  1.13  mycroft 	    sc->sc_dev.dv_xname, rmd->addr, rmd->flags, rmd->bcnt, rmd->mcnt);
    993  1.13  mycroft 	for (i = 0; i < len; i++)  {
    994  1.13  mycroft 		if (!printed) {
    995  1.13  mycroft 			printed = 1;
    996  1.13  mycroft 			printf("%s: data: ", sc->sc_dev.dv_xname);
    997  1.13  mycroft 		}
    998  1.13  mycroft 		printf("%x ", *(sc->sc_tbuf + (BUFSIZE*no) + i));
    999  1.13  mycroft 	}
   1000  1.13  mycroft 	if (printed)
   1001  1.13  mycroft 		printf("\n");
   1002  1.13  mycroft }
   1003  1.13  mycroft #endif /* LEDEBUG */
   1004  1.13  mycroft 
   1005  1.13  mycroft /*
   1006  1.13  mycroft  * Set up the logical address filter.
   1007  1.13  mycroft  */
   1008  1.13  mycroft void
   1009  1.13  mycroft lesetladrf(ac, af)
   1010  1.13  mycroft 	struct arpcom *ac;
   1011  1.13  mycroft 	u_long *af;
   1012   1.1      cgd {
   1013  1.13  mycroft 	struct ifnet *ifp = &ac->ac_if;
   1014  1.13  mycroft 	struct ether_multi *enm;
   1015  1.13  mycroft 	register u_char *cp, c;
   1016  1.13  mycroft 	register u_long crc;
   1017  1.13  mycroft 	register int i, len;
   1018  1.13  mycroft 	struct ether_multistep step;
   1019   1.1      cgd 
   1020   1.1      cgd 	/*
   1021  1.13  mycroft 	 * Set up multicast address filter by passing all multicast addresses
   1022  1.13  mycroft 	 * through a crc generator, and then using the high order 6 bits as an
   1023  1.13  mycroft 	 * index into the 64 bit logical address filter.  The high order bit
   1024  1.13  mycroft 	 * selects the word, while the rest of the bits select the bit within
   1025  1.13  mycroft 	 * the word.
   1026   1.1      cgd 	 */
   1027   1.1      cgd 
   1028  1.13  mycroft 	if (ifp->if_flags & IFF_PROMISC) {
   1029  1.13  mycroft 		ifp->if_flags |= IFF_ALLMULTI;
   1030  1.13  mycroft 		af[0] = af[1] = 0xffffffff;
   1031   1.1      cgd 		return;
   1032  1.13  mycroft 	}
   1033   1.1      cgd 
   1034  1.13  mycroft 	af[0] = af[1] = 0;
   1035  1.13  mycroft 	ETHER_FIRST_MULTI(step, ac, enm);
   1036  1.13  mycroft 	while (enm != NULL) {
   1037  1.13  mycroft 		if (bcmp(enm->enm_addrlo, enm->enm_addrhi,
   1038  1.13  mycroft 		    sizeof(enm->enm_addrlo)) != 0) {
   1039  1.13  mycroft 			/*
   1040  1.13  mycroft 			 * We must listen to a range of multicast addresses.
   1041  1.13  mycroft 			 * For now, just accept all multicasts, rather than
   1042  1.13  mycroft 			 * trying to set only those filter bits needed to match
   1043  1.13  mycroft 			 * the range.  (At this time, the only use of address
   1044  1.13  mycroft 			 * ranges is for IP multicast routing, for which the
   1045  1.13  mycroft 			 * range is big enough to require all bits set.)
   1046  1.13  mycroft 			 */
   1047  1.13  mycroft 			ifp->if_flags |= IFF_ALLMULTI;
   1048  1.13  mycroft 			af[0] = af[1] = 0xffffffff;
   1049  1.13  mycroft 			return;
   1050  1.13  mycroft 		}
   1051   1.1      cgd 
   1052  1.13  mycroft 		cp = enm->enm_addrlo;
   1053  1.13  mycroft 		crc = 0xffffffff;
   1054  1.13  mycroft 		for (len = sizeof(enm->enm_addrlo); --len >= 0;) {
   1055  1.13  mycroft 			c = *cp++;
   1056  1.13  mycroft 			for (i = 8; --i >= 0;) {
   1057  1.13  mycroft 				if ((crc & 0x01) ^ (c & 0x01)) {
   1058  1.13  mycroft 					crc >>= 1;
   1059  1.13  mycroft 					crc ^= 0x6db88320 | 0x80000000;
   1060  1.13  mycroft 				} else
   1061  1.13  mycroft 					crc >>= 1;
   1062  1.13  mycroft 				c >>= 1;
   1063  1.13  mycroft 			}
   1064  1.13  mycroft 		}
   1065  1.13  mycroft 		/* Just want the 6 most significant bits. */
   1066  1.13  mycroft 		crc >>= 26;
   1067   1.1      cgd 
   1068  1.13  mycroft 		/* Turn on the corresponding bit in the filter. */
   1069  1.19  mycroft 		af[crc >> 5] |= 1 << ((crc & 0x1f) ^ 16);
   1070   1.1      cgd 
   1071  1.13  mycroft 		ETHER_NEXT_MULTI(step, enm);
   1072  1.13  mycroft 	}
   1073  1.13  mycroft 	ifp->if_flags &= ~IFF_ALLMULTI;
   1074   1.1      cgd }
   1075  1.14  mycroft 
   1076  1.14  mycroft #endif /* NLE > 0 */
   1077