Home | History | Annotate | Line # | Download | only in ic
am7990.c revision 1.14
      1  1.14   mycroft /*	$NetBSD: am7990.c,v 1.14 1996/03/26 17:06:46 mycroft Exp $	*/
      2   1.1       cgd 
      3   1.1       cgd /*-
      4   1.1       cgd  * Copyright (c) 1995 Charles M. Hannum.  All rights reserved.
      5   1.1       cgd  * Copyright (c) 1992, 1993
      6   1.1       cgd  *	The Regents of the University of California.  All rights reserved.
      7   1.1       cgd  *
      8   1.1       cgd  * This code is derived from software contributed to Berkeley by
      9   1.1       cgd  * Ralph Campbell and Rick Macklem.
     10   1.1       cgd  *
     11   1.1       cgd  * Redistribution and use in source and binary forms, with or without
     12   1.1       cgd  * modification, are permitted provided that the following conditions
     13   1.1       cgd  * are met:
     14   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     15   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     16   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     17   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     18   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     19   1.1       cgd  * 3. All advertising materials mentioning features or use of this software
     20   1.1       cgd  *    must display the following acknowledgement:
     21   1.1       cgd  *	This product includes software developed by the University of
     22   1.1       cgd  *	California, Berkeley and its contributors.
     23   1.1       cgd  * 4. Neither the name of the University nor the names of its contributors
     24   1.1       cgd  *    may be used to endorse or promote products derived from this software
     25   1.1       cgd  *    without specific prior written permission.
     26   1.1       cgd  *
     27   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     28   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     29   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     30   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     31   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     32   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     33   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     34   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     35   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     36   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     37   1.1       cgd  * SUCH DAMAGE.
     38   1.1       cgd  *
     39   1.1       cgd  *	@(#)if_le.c	8.2 (Berkeley) 11/16/93
     40   1.1       cgd  */
     41   1.1       cgd 
     42   1.1       cgd #include <sys/ioctl.h>
     43   1.1       cgd #include <sys/errno.h>
     44   1.1       cgd 
     45   1.1       cgd #ifdef INET
     46   1.1       cgd #include <netinet/in_systm.h>
     47   1.1       cgd #include <netinet/in_var.h>
     48   1.1       cgd #include <netinet/ip.h>
     49   1.1       cgd #endif
     50   1.1       cgd 
     51   1.1       cgd #ifdef NS
     52   1.1       cgd #include <netns/ns.h>
     53   1.1       cgd #include <netns/ns_if.h>
     54   1.1       cgd #endif
     55   1.1       cgd 
     56   1.1       cgd #if defined(CCITT) && defined(LLC)
     57   1.1       cgd #include <sys/socketvar.h>
     58   1.1       cgd #include <netccitt/x25.h>
     59  1.11  christos #include <netccitt/pk.h>
     60  1.11  christos #include <netccitt/pk_var.h>
     61  1.11  christos #include <netccitt/pk_extern.h>
     62   1.1       cgd #endif
     63   1.1       cgd 
     64   1.1       cgd #if NBPFILTER > 0
     65   1.1       cgd #include <net/bpf.h>
     66   1.1       cgd #include <net/bpfdesc.h>
     67   1.1       cgd #endif
     68   1.1       cgd 
     69   1.1       cgd #ifdef LEDEBUG
     70   1.1       cgd void recv_print __P((struct le_softc *, int));
     71   1.1       cgd void xmit_print __P((struct le_softc *, int));
     72   1.1       cgd #endif
     73   1.1       cgd 
     74   1.7   mycroft #define	ifp	(&sc->sc_arpcom.ac_if)
     75   1.7   mycroft 
     76  1.13       gwr #ifndef	ETHER_CMP
     77  1.13       gwr #define	ETHER_CMP(a, b) bcmp((a), (b), ETHER_ADDR_LEN)
     78  1.13       gwr #endif
     79  1.13       gwr 
     80   1.1       cgd void
     81   1.1       cgd leconfig(sc)
     82   1.1       cgd 	struct le_softc *sc;
     83   1.1       cgd {
     84   1.1       cgd 	int mem;
     85   1.1       cgd 
     86   1.1       cgd 	/* Make sure the chip is stopped. */
     87   1.1       cgd 	lestop(sc);
     88   1.1       cgd 
     89   1.1       cgd 	/* Initialize ifnet structure. */
     90   1.1       cgd 	ifp->if_unit = sc->sc_dev.dv_unit;
     91   1.1       cgd 	ifp->if_start = lestart;
     92   1.1       cgd 	ifp->if_ioctl = leioctl;
     93   1.1       cgd 	ifp->if_watchdog = lewatchdog;
     94   1.1       cgd 	ifp->if_flags =
     95   1.1       cgd 	    IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS | IFF_MULTICAST;
     96   1.5   mycroft #ifdef LANCE_REVC_BUG
     97   1.5   mycroft 	ifp->if_flags &= ~IFF_MULTICAST;
     98   1.5   mycroft #endif
     99   1.1       cgd 
    100   1.1       cgd 	/* Attach the interface. */
    101   1.1       cgd 	if_attach(ifp);
    102   1.1       cgd 	ether_ifattach(ifp);
    103   1.1       cgd 
    104   1.1       cgd #if NBPFILTER > 0
    105   1.1       cgd 	bpfattach(&ifp->if_bpf, ifp, DLT_EN10MB, sizeof(struct ether_header));
    106   1.1       cgd #endif
    107   1.1       cgd 
    108   1.1       cgd 	switch (sc->sc_memsize) {
    109   1.1       cgd 	case 8192:
    110   1.1       cgd 		sc->sc_nrbuf = 4;
    111   1.1       cgd 		sc->sc_ntbuf = 1;
    112   1.1       cgd 		break;
    113   1.1       cgd 	case 16384:
    114   1.1       cgd 		sc->sc_nrbuf = 8;
    115   1.1       cgd 		sc->sc_ntbuf = 2;
    116   1.1       cgd 		break;
    117   1.1       cgd 	case 32768:
    118   1.1       cgd 		sc->sc_nrbuf = 16;
    119   1.1       cgd 		sc->sc_ntbuf = 4;
    120   1.1       cgd 		break;
    121   1.1       cgd 	case 65536:
    122   1.1       cgd 		sc->sc_nrbuf = 32;
    123   1.1       cgd 		sc->sc_ntbuf = 8;
    124   1.1       cgd 		break;
    125   1.1       cgd 	default:
    126   1.1       cgd 		panic("leconfig: weird memory size");
    127   1.1       cgd 	}
    128   1.1       cgd 
    129  1.13       gwr 	printf(": address %s\n", ether_sprintf(sc->sc_arpcom.ac_enaddr));
    130  1.13       gwr 	printf("%s: %d receive buffers, %d transmit buffers\n",
    131   1.5   mycroft 	    sc->sc_dev.dv_xname, sc->sc_nrbuf, sc->sc_ntbuf);
    132   1.1       cgd 
    133   1.1       cgd 	mem = 0;
    134   1.1       cgd 	sc->sc_initaddr = mem;
    135   1.1       cgd 	mem += sizeof(struct leinit);
    136   1.1       cgd 	sc->sc_rmdaddr = mem;
    137   1.1       cgd 	mem += sizeof(struct lermd) * sc->sc_nrbuf;
    138   1.1       cgd 	sc->sc_tmdaddr = mem;
    139   1.1       cgd 	mem += sizeof(struct letmd) * sc->sc_ntbuf;
    140   1.1       cgd 	sc->sc_rbufaddr = mem;
    141   1.1       cgd 	mem += LEBLEN * sc->sc_nrbuf;
    142   1.1       cgd 	sc->sc_tbufaddr = mem;
    143   1.1       cgd 	mem += LEBLEN * sc->sc_ntbuf;
    144   1.1       cgd #ifdef notyet
    145   1.1       cgd 	if (mem > ...)
    146   1.1       cgd 		panic(...);
    147   1.1       cgd #endif
    148   1.1       cgd }
    149   1.1       cgd 
    150   1.1       cgd void
    151   1.1       cgd lereset(sc)
    152   1.1       cgd 	struct le_softc *sc;
    153   1.1       cgd {
    154   1.2   mycroft 	int s;
    155   1.1       cgd 
    156   1.2   mycroft 	s = splimp();
    157   1.1       cgd 	leinit(sc);
    158   1.2   mycroft 	splx(s);
    159   1.1       cgd }
    160   1.1       cgd 
    161   1.1       cgd void
    162   1.1       cgd lewatchdog(unit)
    163   1.8   mycroft 	int unit;
    164   1.1       cgd {
    165   1.1       cgd 	struct le_softc *sc = LE_SOFTC(unit);
    166   1.1       cgd 
    167   1.1       cgd 	log(LOG_ERR, "%s: device timeout\n", sc->sc_dev.dv_xname);
    168   1.7   mycroft 	++ifp->if_oerrors;
    169   1.1       cgd 
    170   1.1       cgd 	lereset(sc);
    171   1.1       cgd }
    172   1.1       cgd 
    173   1.1       cgd /*
    174   1.1       cgd  * Set up the initialization block and the descriptor rings.
    175   1.1       cgd  */
    176   1.1       cgd void
    177   1.1       cgd lememinit(sc)
    178   1.1       cgd 	register struct le_softc *sc;
    179   1.1       cgd {
    180   1.1       cgd 	u_long a;
    181   1.1       cgd 	int bix;
    182   1.1       cgd 	struct leinit init;
    183   1.1       cgd 	struct lermd rmd;
    184   1.1       cgd 	struct letmd tmd;
    185   1.1       cgd 
    186   1.1       cgd #if NBPFILTER > 0
    187   1.1       cgd 	if (ifp->if_flags & IFF_PROMISC)
    188   1.1       cgd 		init.init_mode = LE_MODE_NORMAL | LE_MODE_PROM;
    189   1.1       cgd 	else
    190   1.1       cgd #endif
    191   1.1       cgd 		init.init_mode = LE_MODE_NORMAL;
    192   1.1       cgd 	init.init_padr[0] =
    193   1.1       cgd 	    (sc->sc_arpcom.ac_enaddr[1] << 8) | sc->sc_arpcom.ac_enaddr[0];
    194   1.1       cgd 	init.init_padr[1] =
    195   1.1       cgd 	    (sc->sc_arpcom.ac_enaddr[3] << 8) | sc->sc_arpcom.ac_enaddr[2];
    196   1.1       cgd 	init.init_padr[2] =
    197   1.1       cgd 	    (sc->sc_arpcom.ac_enaddr[5] << 8) | sc->sc_arpcom.ac_enaddr[4];
    198   1.1       cgd 	lesetladrf(&sc->sc_arpcom, init.init_ladrf);
    199   1.1       cgd 
    200   1.1       cgd 	sc->sc_last_rd = 0;
    201   1.1       cgd 	sc->sc_first_td = sc->sc_last_td = sc->sc_no_td = 0;
    202   1.1       cgd 
    203   1.1       cgd 	a = sc->sc_addr + LE_RMDADDR(sc, 0);
    204   1.1       cgd 	init.init_rdra = a;
    205   1.1       cgd 	init.init_rlen = (a >> 16) | ((ffs(sc->sc_nrbuf) - 1) << 13);
    206   1.1       cgd 
    207   1.1       cgd 	a = sc->sc_addr + LE_TMDADDR(sc, 0);
    208   1.1       cgd 	init.init_tdra = a;
    209   1.1       cgd 	init.init_tlen = (a >> 16) | ((ffs(sc->sc_ntbuf) - 1) << 13);
    210   1.1       cgd 
    211   1.1       cgd 	(*sc->sc_copytodesc)(sc, &init, LE_INITADDR(sc), sizeof(init));
    212   1.1       cgd 
    213   1.1       cgd 	/*
    214   1.1       cgd 	 * Set up receive ring descriptors.
    215   1.1       cgd 	 */
    216   1.1       cgd 	for (bix = 0; bix < sc->sc_nrbuf; bix++) {
    217   1.1       cgd 		a = sc->sc_addr + LE_RBUFADDR(sc, bix);
    218   1.1       cgd 		rmd.rmd0 = a;
    219   1.1       cgd 		rmd.rmd1_hadr = a >> 16;
    220   1.1       cgd 		rmd.rmd1_bits = LE_R1_OWN;
    221   1.1       cgd 		rmd.rmd2 = -LEBLEN | LE_XMD2_ONES;
    222   1.1       cgd 		rmd.rmd3 = 0;
    223   1.1       cgd 		(*sc->sc_copytodesc)(sc, &rmd, LE_RMDADDR(sc, bix),
    224   1.1       cgd 		    sizeof(rmd));
    225   1.1       cgd 	}
    226   1.1       cgd 
    227   1.1       cgd 	/*
    228   1.1       cgd 	 * Set up transmit ring descriptors.
    229   1.1       cgd 	 */
    230   1.1       cgd 	for (bix = 0; bix < sc->sc_ntbuf; bix++) {
    231   1.1       cgd 		a = sc->sc_addr + LE_TBUFADDR(sc, bix);
    232   1.1       cgd 		tmd.tmd0 = a;
    233   1.1       cgd 		tmd.tmd1_hadr = a >> 16;
    234   1.1       cgd 		tmd.tmd1_bits = 0;
    235   1.1       cgd 		tmd.tmd2 = 0 | LE_XMD2_ONES;
    236   1.1       cgd 		tmd.tmd3 = 0;
    237   1.1       cgd 		(*sc->sc_copytodesc)(sc, &tmd, LE_TMDADDR(sc, bix),
    238   1.1       cgd 		    sizeof(tmd));
    239   1.1       cgd 	}
    240   1.1       cgd }
    241   1.1       cgd 
    242   1.1       cgd void
    243   1.1       cgd lestop(sc)
    244   1.1       cgd 	struct le_softc *sc;
    245   1.1       cgd {
    246   1.1       cgd 
    247   1.1       cgd 	lewrcsr(sc, LE_CSR0, LE_C0_STOP);
    248   1.1       cgd }
    249   1.1       cgd 
    250   1.1       cgd /*
    251   1.1       cgd  * Initialization of interface; set up initialization block
    252   1.1       cgd  * and transmit/receive descriptor rings.
    253   1.1       cgd  */
    254   1.1       cgd void
    255   1.1       cgd leinit(sc)
    256   1.1       cgd 	register struct le_softc *sc;
    257   1.1       cgd {
    258   1.1       cgd 	register int timo;
    259   1.1       cgd 	u_long a;
    260   1.1       cgd 
    261   1.1       cgd 	lewrcsr(sc, LE_CSR0, LE_C0_STOP);
    262   1.1       cgd 	LE_DELAY(100);
    263   1.1       cgd 
    264   1.1       cgd 	/* Set the correct byte swapping mode, etc. */
    265   1.1       cgd 	lewrcsr(sc, LE_CSR3, sc->sc_conf3);
    266   1.1       cgd 
    267   1.1       cgd 	/* Set up LANCE init block. */
    268   1.1       cgd 	lememinit(sc);
    269   1.1       cgd 
    270   1.1       cgd 	/* Give LANCE the physical address of its init block. */
    271   1.1       cgd 	a = sc->sc_addr + LE_INITADDR(sc);
    272   1.1       cgd 	lewrcsr(sc, LE_CSR1, a);
    273   1.1       cgd 	lewrcsr(sc, LE_CSR2, a >> 16);
    274   1.1       cgd 
    275   1.1       cgd 	/* Try to initialize the LANCE. */
    276   1.1       cgd 	LE_DELAY(100);
    277   1.1       cgd 	lewrcsr(sc, LE_CSR0, LE_C0_INIT);
    278   1.1       cgd 
    279   1.1       cgd 	/* Wait for initialization to finish. */
    280   1.1       cgd 	for (timo = 100000; timo; timo--)
    281   1.1       cgd 		if (lerdcsr(sc, LE_CSR0) & LE_C0_IDON)
    282   1.1       cgd 			break;
    283   1.1       cgd 
    284   1.1       cgd 	if (lerdcsr(sc, LE_CSR0) & LE_C0_IDON) {
    285   1.1       cgd 		/* Start the LANCE. */
    286   1.1       cgd 		lewrcsr(sc, LE_CSR0, LE_C0_INEA | LE_C0_STRT | LE_C0_IDON);
    287   1.1       cgd 		ifp->if_flags |= IFF_RUNNING;
    288   1.1       cgd 		ifp->if_flags &= ~IFF_OACTIVE;
    289   1.1       cgd 		ifp->if_timer = 0;
    290   1.1       cgd 		lestart(ifp);
    291   1.1       cgd 	} else
    292   1.1       cgd 		printf("%s: card failed to initialize\n", sc->sc_dev.dv_xname);
    293   1.1       cgd }
    294   1.1       cgd 
    295   1.1       cgd /*
    296   1.1       cgd  * Routine to copy from mbuf chain to transmit buffer in
    297   1.1       cgd  * network buffer memory.
    298   1.1       cgd  */
    299   1.1       cgd integrate int
    300   1.1       cgd leput(sc, boff, m)
    301   1.1       cgd 	struct le_softc *sc;
    302   1.1       cgd 	int boff;
    303   1.1       cgd 	register struct mbuf *m;
    304   1.1       cgd {
    305   1.1       cgd 	register struct mbuf *n;
    306   1.1       cgd 	register int len, tlen = 0;
    307   1.1       cgd 
    308   1.1       cgd 	for (; m; m = n) {
    309   1.1       cgd 		len = m->m_len;
    310   1.1       cgd 		if (len == 0) {
    311   1.1       cgd 			MFREE(m, n);
    312   1.1       cgd 			continue;
    313   1.1       cgd 		}
    314   1.1       cgd 		(*sc->sc_copytobuf)(sc, mtod(m, caddr_t), boff, len);
    315   1.1       cgd 		boff += len;
    316   1.1       cgd 		tlen += len;
    317   1.1       cgd 		MFREE(m, n);
    318   1.1       cgd 	}
    319   1.1       cgd 	if (tlen < LEMINSIZE) {
    320   1.1       cgd 		(*sc->sc_zerobuf)(sc, boff, LEMINSIZE - tlen);
    321   1.1       cgd 		tlen = LEMINSIZE;
    322   1.1       cgd 	}
    323   1.1       cgd 	return (tlen);
    324   1.1       cgd }
    325   1.1       cgd 
    326   1.1       cgd /*
    327   1.1       cgd  * Pull data off an interface.
    328   1.1       cgd  * Len is length of data, with local net header stripped.
    329   1.1       cgd  * We copy the data into mbufs.  When full cluster sized units are present
    330   1.1       cgd  * we copy into clusters.
    331   1.1       cgd  */
    332   1.1       cgd integrate struct mbuf *
    333   1.1       cgd leget(sc, boff, totlen)
    334   1.1       cgd 	struct le_softc *sc;
    335   1.1       cgd 	int boff, totlen;
    336   1.1       cgd {
    337   1.1       cgd 	register struct mbuf *m;
    338   1.1       cgd 	struct mbuf *top, **mp;
    339   1.1       cgd 	int len, pad;
    340   1.1       cgd 
    341   1.1       cgd 	MGETHDR(m, M_DONTWAIT, MT_DATA);
    342   1.1       cgd 	if (m == 0)
    343   1.1       cgd 		return (0);
    344   1.7   mycroft 	m->m_pkthdr.rcvif = ifp;
    345   1.1       cgd 	m->m_pkthdr.len = totlen;
    346   1.1       cgd 	pad = ALIGN(sizeof(struct ether_header)) - sizeof(struct ether_header);
    347   1.1       cgd 	m->m_data += pad;
    348   1.1       cgd 	len = MHLEN - pad;
    349   1.1       cgd 	top = 0;
    350   1.1       cgd 	mp = &top;
    351   1.1       cgd 
    352   1.1       cgd 	while (totlen > 0) {
    353   1.1       cgd 		if (top) {
    354   1.1       cgd 			MGET(m, M_DONTWAIT, MT_DATA);
    355   1.1       cgd 			if (m == 0) {
    356   1.1       cgd 				m_freem(top);
    357   1.1       cgd 				return 0;
    358   1.1       cgd 			}
    359   1.1       cgd 			len = MLEN;
    360   1.1       cgd 		}
    361   1.1       cgd 		if (top && totlen >= MINCLSIZE) {
    362   1.1       cgd 			MCLGET(m, M_DONTWAIT);
    363   1.1       cgd 			if (m->m_flags & M_EXT)
    364   1.1       cgd 				len = MCLBYTES;
    365   1.1       cgd 		}
    366   1.1       cgd 		m->m_len = len = min(totlen, len);
    367   1.1       cgd 		(*sc->sc_copyfrombuf)(sc, mtod(m, caddr_t), boff, len);
    368   1.1       cgd 		boff += len;
    369   1.1       cgd 		totlen -= len;
    370   1.1       cgd 		*mp = m;
    371   1.1       cgd 		mp = &m->m_next;
    372   1.1       cgd 	}
    373   1.1       cgd 
    374   1.1       cgd 	return (top);
    375   1.1       cgd }
    376   1.1       cgd 
    377   1.1       cgd /*
    378   1.1       cgd  * Pass a packet to the higher levels.
    379   1.1       cgd  */
    380   1.1       cgd integrate void
    381   1.1       cgd leread(sc, boff, len)
    382   1.1       cgd 	register struct le_softc *sc;
    383   1.1       cgd 	int boff, len;
    384   1.1       cgd {
    385   1.1       cgd 	struct mbuf *m;
    386   1.1       cgd 	struct ether_header *eh;
    387   1.1       cgd 
    388   1.2   mycroft 	if (len <= sizeof(struct ether_header) ||
    389   1.3   mycroft 	    len > ETHERMTU + sizeof(struct ether_header)) {
    390   1.6   mycroft #ifdef LEDEBUG
    391   1.2   mycroft 		printf("%s: invalid packet size %d; dropping\n",
    392   1.2   mycroft 		    sc->sc_dev.dv_xname, len);
    393   1.6   mycroft #endif
    394   1.2   mycroft 		ifp->if_ierrors++;
    395   1.1       cgd 		return;
    396   1.2   mycroft 	}
    397   1.1       cgd 
    398   1.1       cgd 	/* Pull packet off interface. */
    399   1.1       cgd 	m = leget(sc, boff, len);
    400   1.2   mycroft 	if (m == 0) {
    401   1.2   mycroft 		ifp->if_ierrors++;
    402   1.1       cgd 		return;
    403   1.2   mycroft 	}
    404   1.2   mycroft 
    405   1.2   mycroft 	ifp->if_ipackets++;
    406   1.1       cgd 
    407   1.1       cgd 	/* We assume that the header fit entirely in one mbuf. */
    408   1.1       cgd 	eh = mtod(m, struct ether_header *);
    409   1.1       cgd 
    410   1.1       cgd #if NBPFILTER > 0
    411   1.1       cgd 	/*
    412   1.1       cgd 	 * Check if there's a BPF listener on this interface.
    413   1.1       cgd 	 * If so, hand off the raw packet to BPF.
    414   1.1       cgd 	 */
    415   1.1       cgd 	if (ifp->if_bpf) {
    416   1.1       cgd 		bpf_mtap(ifp->if_bpf, m);
    417   1.1       cgd 
    418   1.5   mycroft #ifndef LANCE_REVC_BUG
    419   1.1       cgd 		/*
    420   1.1       cgd 		 * Note that the interface cannot be in promiscuous mode if
    421   1.1       cgd 		 * there are no BPF listeners.  And if we are in promiscuous
    422   1.1       cgd 		 * mode, we have to check if this packet is really ours.
    423   1.1       cgd 		 */
    424   1.1       cgd 		if ((ifp->if_flags & IFF_PROMISC) != 0 &&
    425   1.1       cgd 		    (eh->ether_dhost[0] & 1) == 0 && /* !mcast and !bcast */
    426  1.14   mycroft 		    ETHER_CMP(eh->ether_dhost, sc->sc_arpcom.ac_enaddr)) {
    427   1.1       cgd 			m_freem(m);
    428   1.1       cgd 			return;
    429   1.1       cgd 		}
    430   1.5   mycroft #endif
    431   1.5   mycroft 	}
    432   1.5   mycroft #endif
    433   1.5   mycroft 
    434   1.5   mycroft #ifdef LANCE_REVC_BUG
    435  1.13       gwr 	/*
    436  1.13       gwr 	 * The old LANCE (Rev. C) chips have a bug which causes
    437  1.13       gwr 	 * garbage to be inserted in front of the received packet.
    438  1.13       gwr 	 * The work-around is to ignore packets with an invalid
    439  1.13       gwr 	 * destination address (garbage will usually not match).
    440  1.13       gwr 	 * Of course, this precludes multicast support...
    441  1.13       gwr 	 */
    442  1.13       gwr 	if (ETHER_CMP(eh->ether_dhost, sc->sc_arpcom.ac_enaddr) &&
    443  1.14   mycroft 	    ETHER_CMP(eh->ether_dhost, etherbroadcastaddr)) {
    444   1.5   mycroft 		m_freem(m);
    445   1.5   mycroft 		return;
    446   1.1       cgd 	}
    447   1.1       cgd #endif
    448   1.1       cgd 
    449   1.1       cgd 	/* Pass the packet up, with the ether header sort-of removed. */
    450   1.1       cgd 	m_adj(m, sizeof(struct ether_header));
    451   1.1       cgd 	ether_input(ifp, eh, m);
    452   1.1       cgd }
    453   1.1       cgd 
    454   1.1       cgd integrate void
    455   1.1       cgd lerint(sc)
    456   1.1       cgd 	struct le_softc *sc;
    457   1.1       cgd {
    458   1.1       cgd 	register int bix;
    459   1.1       cgd 	int rp;
    460   1.1       cgd 	struct lermd rmd;
    461   1.1       cgd 
    462   1.1       cgd 	bix = sc->sc_last_rd;
    463   1.1       cgd 
    464   1.1       cgd 	/* Process all buffers with valid data. */
    465   1.1       cgd 	for (;;) {
    466   1.1       cgd 		rp = LE_RMDADDR(sc, bix);
    467   1.1       cgd 		(*sc->sc_copyfromdesc)(sc, &rmd, rp, sizeof(rmd));
    468   1.1       cgd 
    469   1.1       cgd 		if (rmd.rmd1_bits & LE_R1_OWN)
    470   1.1       cgd 			break;
    471   1.1       cgd 
    472   1.1       cgd 		if (rmd.rmd1_bits & LE_R1_ERR) {
    473   1.1       cgd 			if (rmd.rmd1_bits & LE_R1_ENP) {
    474   1.6   mycroft #ifdef LEDEBUG
    475   1.1       cgd 				if ((rmd.rmd1_bits & LE_R1_OFLO) == 0) {
    476   1.1       cgd 					if (rmd.rmd1_bits & LE_R1_FRAM)
    477   1.1       cgd 						printf("%s: framing error\n",
    478   1.1       cgd 						    sc->sc_dev.dv_xname);
    479   1.1       cgd 					if (rmd.rmd1_bits & LE_R1_CRC)
    480   1.1       cgd 						printf("%s: crc mismatch\n",
    481   1.1       cgd 						    sc->sc_dev.dv_xname);
    482   1.1       cgd 				}
    483   1.6   mycroft #endif
    484   1.1       cgd 			} else {
    485   1.1       cgd 				if (rmd.rmd1_bits & LE_R1_OFLO)
    486   1.1       cgd 					printf("%s: overflow\n",
    487   1.1       cgd 					    sc->sc_dev.dv_xname);
    488   1.1       cgd 			}
    489   1.1       cgd 			if (rmd.rmd1_bits & LE_R1_BUFF)
    490   1.1       cgd 				printf("%s: receive buffer error\n",
    491   1.1       cgd 				    sc->sc_dev.dv_xname);
    492   1.7   mycroft 			ifp->if_ierrors++;
    493  1.12  christos 		} else if ((rmd.rmd1_bits & (LE_R1_STP | LE_R1_ENP)) !=
    494   1.1       cgd 		    (LE_R1_STP | LE_R1_ENP)) {
    495   1.1       cgd 			printf("%s: dropping chained buffer\n",
    496   1.1       cgd 			    sc->sc_dev.dv_xname);
    497   1.7   mycroft 			ifp->if_ierrors++;
    498   1.1       cgd 		} else {
    499   1.1       cgd #ifdef LEDEBUG
    500   1.1       cgd 			if (sc->sc_debug)
    501   1.1       cgd 				recv_print(sc, sc->sc_last_rd);
    502   1.1       cgd #endif
    503   1.2   mycroft 			leread(sc, LE_RBUFADDR(sc, bix), (int)rmd.rmd3 - 4);
    504   1.1       cgd 		}
    505   1.1       cgd 
    506   1.1       cgd 		rmd.rmd1_bits = LE_R1_OWN;
    507   1.1       cgd 		rmd.rmd2 = -LEBLEN | LE_XMD2_ONES;
    508   1.1       cgd 		rmd.rmd3 = 0;
    509   1.1       cgd 		(*sc->sc_copytodesc)(sc, &rmd, rp, sizeof(rmd));
    510   1.1       cgd 
    511   1.1       cgd #ifdef LEDEBUG
    512   1.1       cgd 		if (sc->sc_debug)
    513   1.1       cgd 			printf("sc->sc_last_rd = %x, rmd = %x\n",
    514   1.1       cgd 			    sc->sc_last_rd, rmd);
    515   1.1       cgd #endif
    516   1.1       cgd 
    517   1.1       cgd 		if (++bix == sc->sc_nrbuf)
    518   1.1       cgd 			bix = 0;
    519   1.1       cgd 	}
    520   1.1       cgd 
    521   1.1       cgd 	sc->sc_last_rd = bix;
    522   1.1       cgd }
    523   1.1       cgd 
    524   1.1       cgd integrate void
    525   1.1       cgd letint(sc)
    526   1.1       cgd 	register struct le_softc *sc;
    527   1.1       cgd {
    528   1.1       cgd 	register int bix;
    529   1.1       cgd 	struct letmd tmd;
    530   1.9   thorpej 
    531   1.1       cgd 	bix = sc->sc_first_td;
    532   1.1       cgd 
    533   1.1       cgd 	for (;;) {
    534   1.1       cgd 		if (sc->sc_no_td <= 0)
    535   1.1       cgd 			break;
    536   1.1       cgd 
    537   1.1       cgd #ifdef LEDEBUG
    538   1.1       cgd 		if (sc->sc_debug)
    539   1.1       cgd 			printf("trans tmd = %x\n", tmd);
    540   1.1       cgd #endif
    541   1.1       cgd 
    542   1.1       cgd 		(*sc->sc_copyfromdesc)(sc, &tmd, LE_TMDADDR(sc, bix),
    543   1.1       cgd 		    sizeof(tmd));
    544   1.1       cgd 
    545   1.1       cgd 		if (tmd.tmd1_bits & LE_T1_OWN)
    546   1.1       cgd 			break;
    547   1.1       cgd 
    548   1.1       cgd 		ifp->if_flags &= ~IFF_OACTIVE;
    549   1.1       cgd 
    550   1.1       cgd 		if (tmd.tmd1_bits & LE_T1_ERR) {
    551   1.1       cgd 			if (tmd.tmd3 & LE_T3_BUFF)
    552   1.1       cgd 				printf("%s: transmit buffer error\n", sc->sc_dev.dv_xname);
    553   1.1       cgd 			else if (tmd.tmd3 & LE_T3_UFLO)
    554   1.1       cgd 				printf("%s: underflow\n", sc->sc_dev.dv_xname);
    555   1.1       cgd 			if (tmd.tmd3 & (LE_T3_BUFF | LE_T3_UFLO)) {
    556   1.1       cgd 				lereset(sc);
    557   1.1       cgd 				return;
    558   1.1       cgd 			}
    559   1.1       cgd 			if (tmd.tmd3 & LE_T3_LCAR)
    560   1.1       cgd 				printf("%s: lost carrier\n", sc->sc_dev.dv_xname);
    561   1.1       cgd 			if (tmd.tmd3 & LE_T3_LCOL)
    562   1.1       cgd 				ifp->if_collisions++;
    563   1.1       cgd 			if (tmd.tmd3 & LE_T3_RTRY) {
    564   1.1       cgd 				printf("%s: excessive collisions, tdr %d\n",
    565   1.1       cgd 				    sc->sc_dev.dv_xname, tmd.tmd3 & LE_T3_TDR_MASK);
    566   1.1       cgd 				ifp->if_collisions += 16;
    567   1.1       cgd 			}
    568   1.1       cgd 			ifp->if_oerrors++;
    569   1.1       cgd 		} else {
    570   1.1       cgd 			if (tmd.tmd1_bits & LE_T1_ONE)
    571   1.1       cgd 				ifp->if_collisions++;
    572   1.1       cgd 			else if (tmd.tmd1_bits & LE_T1_MORE)
    573   1.1       cgd 				/* Real number is unknown. */
    574   1.1       cgd 				ifp->if_collisions += 2;
    575   1.1       cgd 			ifp->if_opackets++;
    576   1.1       cgd 		}
    577   1.1       cgd 
    578   1.1       cgd 		if (++bix == sc->sc_ntbuf)
    579   1.1       cgd 			bix = 0;
    580   1.1       cgd 
    581   1.1       cgd 		--sc->sc_no_td;
    582   1.1       cgd 	}
    583   1.1       cgd 
    584   1.1       cgd 	sc->sc_first_td = bix;
    585   1.1       cgd 
    586   1.1       cgd 	lestart(ifp);
    587   1.1       cgd 
    588   1.1       cgd 	if (sc->sc_no_td == 0)
    589   1.1       cgd 		ifp->if_timer = 0;
    590   1.1       cgd }
    591   1.1       cgd 
    592   1.1       cgd /*
    593   1.1       cgd  * Controller interrupt.
    594   1.1       cgd  */
    595   1.1       cgd int
    596   1.1       cgd leintr(arg)
    597   1.1       cgd 	register void *arg;
    598   1.1       cgd {
    599   1.1       cgd 	register struct le_softc *sc = arg;
    600   1.1       cgd 	register u_int16_t isr;
    601   1.1       cgd 
    602   1.1       cgd 	isr = lerdcsr(sc, LE_CSR0);
    603   1.1       cgd #ifdef LEDEBUG
    604   1.1       cgd 	if (sc->sc_debug)
    605   1.1       cgd 		printf("%s: leintr entering with isr=%04x\n",
    606   1.1       cgd 		    sc->sc_dev.dv_xname, isr);
    607   1.1       cgd #endif
    608   1.1       cgd 	if ((isr & LE_C0_INTR) == 0)
    609   1.1       cgd 		return (0);
    610   1.1       cgd 
    611   1.1       cgd 	lewrcsr(sc, LE_CSR0,
    612   1.1       cgd 	    isr & (LE_C0_INEA | LE_C0_BABL | LE_C0_MISS | LE_C0_MERR |
    613   1.1       cgd 		   LE_C0_RINT | LE_C0_TINT | LE_C0_IDON));
    614   1.1       cgd 	if (isr & LE_C0_ERR) {
    615   1.1       cgd 		if (isr & LE_C0_BABL) {
    616   1.6   mycroft #ifdef LEDEBUG
    617   1.1       cgd 			printf("%s: babble\n", sc->sc_dev.dv_xname);
    618   1.6   mycroft #endif
    619   1.7   mycroft 			ifp->if_oerrors++;
    620   1.1       cgd 		}
    621   1.1       cgd #if 0
    622   1.1       cgd 		if (isr & LE_C0_CERR) {
    623   1.1       cgd 			printf("%s: collision error\n", sc->sc_dev.dv_xname);
    624   1.7   mycroft 			ifp->if_collisions++;
    625   1.1       cgd 		}
    626   1.1       cgd #endif
    627   1.6   mycroft 		if (isr & LE_C0_MISS) {
    628   1.6   mycroft #ifdef LEDEBUG
    629   1.6   mycroft 			printf("%s: missed packet\n", sc->sc_dev.dv_xname);
    630   1.6   mycroft #endif
    631   1.7   mycroft 			ifp->if_ierrors++;
    632   1.6   mycroft 		}
    633   1.1       cgd 		if (isr & LE_C0_MERR) {
    634   1.1       cgd 			printf("%s: memory error\n", sc->sc_dev.dv_xname);
    635   1.1       cgd 			lereset(sc);
    636   1.1       cgd 			return (1);
    637   1.1       cgd 		}
    638   1.1       cgd 	}
    639   1.1       cgd 
    640   1.1       cgd 	if ((isr & LE_C0_RXON) == 0) {
    641   1.1       cgd 		printf("%s: receiver disabled\n", sc->sc_dev.dv_xname);
    642   1.7   mycroft 		ifp->if_ierrors++;
    643   1.1       cgd 		lereset(sc);
    644   1.1       cgd 		return (1);
    645   1.1       cgd 	}
    646   1.1       cgd 	if ((isr & LE_C0_TXON) == 0) {
    647   1.1       cgd 		printf("%s: transmitter disabled\n", sc->sc_dev.dv_xname);
    648   1.7   mycroft 		ifp->if_oerrors++;
    649   1.1       cgd 		lereset(sc);
    650   1.1       cgd 		return (1);
    651   1.1       cgd 	}
    652   1.1       cgd 
    653   1.1       cgd 	if (isr & LE_C0_RINT)
    654   1.1       cgd 		lerint(sc);
    655   1.1       cgd 	if (isr & LE_C0_TINT)
    656   1.1       cgd 		letint(sc);
    657   1.1       cgd 
    658   1.1       cgd 	return (1);
    659   1.1       cgd }
    660   1.7   mycroft 
    661   1.7   mycroft #undef	ifp
    662   1.1       cgd 
    663   1.1       cgd /*
    664   1.1       cgd  * Setup output on interface.
    665   1.1       cgd  * Get another datagram to send off of the interface queue, and map it to the
    666   1.1       cgd  * interface before starting the output.
    667   1.1       cgd  * Called only at splimp or interrupt level.
    668   1.1       cgd  */
    669   1.1       cgd void
    670   1.1       cgd lestart(ifp)
    671   1.1       cgd 	register struct ifnet *ifp;
    672   1.1       cgd {
    673   1.1       cgd 	register struct le_softc *sc = LE_SOFTC(ifp->if_unit);
    674   1.1       cgd 	register int bix;
    675   1.1       cgd 	register struct mbuf *m;
    676   1.1       cgd 	struct letmd tmd;
    677   1.1       cgd 	int rp;
    678   1.1       cgd 	int len;
    679   1.1       cgd 
    680   1.1       cgd 	if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
    681   1.1       cgd 		return;
    682   1.1       cgd 
    683   1.1       cgd 	bix = sc->sc_last_td;
    684   1.1       cgd 
    685   1.1       cgd 	for (;;) {
    686   1.1       cgd 		rp = LE_TMDADDR(sc, bix);
    687   1.1       cgd 		(*sc->sc_copyfromdesc)(sc, &tmd, rp, sizeof(tmd));
    688   1.1       cgd 
    689   1.1       cgd 		if (tmd.tmd1_bits & LE_T1_OWN) {
    690   1.1       cgd 			ifp->if_flags |= IFF_OACTIVE;
    691   1.1       cgd 			printf("missing buffer, no_td = %d, last_td = %d\n",
    692   1.1       cgd 			    sc->sc_no_td, sc->sc_last_td);
    693   1.1       cgd 		}
    694   1.1       cgd 
    695   1.1       cgd 		IF_DEQUEUE(&ifp->if_snd, m);
    696   1.1       cgd 		if (m == 0)
    697   1.1       cgd 			break;
    698   1.1       cgd 
    699   1.1       cgd #if NBPFILTER > 0
    700   1.1       cgd 		/*
    701   1.1       cgd 		 * If BPF is listening on this interface, let it see the packet
    702   1.1       cgd 		 * before we commit it to the wire.
    703   1.1       cgd 		 */
    704   1.1       cgd 		if (ifp->if_bpf)
    705   1.1       cgd 			bpf_mtap(ifp->if_bpf, m);
    706   1.1       cgd #endif
    707   1.1       cgd 
    708   1.1       cgd 		/*
    709   1.1       cgd 		 * Copy the mbuf chain into the transmit buffer.
    710   1.1       cgd 		 */
    711   1.1       cgd 		len = leput(sc, LE_TBUFADDR(sc, bix), m);
    712   1.1       cgd 
    713   1.1       cgd #ifdef LEDEBUG
    714   1.3   mycroft 		if (len > ETHERMTU + sizeof(struct ether_header))
    715   1.1       cgd 			printf("packet length %d\n", len);
    716   1.1       cgd #endif
    717   1.1       cgd 
    718   1.1       cgd 		ifp->if_timer = 5;
    719   1.1       cgd 
    720   1.1       cgd 		/*
    721   1.1       cgd 		 * Init transmit registers, and set transmit start flag.
    722   1.1       cgd 		 */
    723   1.1       cgd 		tmd.tmd1_bits = LE_T1_OWN | LE_T1_STP | LE_T1_ENP;
    724   1.1       cgd 		tmd.tmd2 = -len | LE_XMD2_ONES;
    725   1.1       cgd 		tmd.tmd3 = 0;
    726   1.1       cgd 
    727   1.1       cgd 		(*sc->sc_copytodesc)(sc, &tmd, rp, sizeof(tmd));
    728   1.1       cgd 
    729   1.1       cgd #ifdef LEDEBUG
    730   1.1       cgd 		if (sc->sc_debug)
    731   1.1       cgd 			xmit_print(sc, sc->sc_last_td);
    732   1.1       cgd #endif
    733   1.1       cgd 
    734   1.1       cgd 		lewrcsr(sc, LE_CSR0, LE_C0_INEA | LE_C0_TDMD);
    735   1.1       cgd 
    736   1.1       cgd 		if (++bix == sc->sc_ntbuf)
    737   1.1       cgd 			bix = 0;
    738   1.1       cgd 
    739   1.1       cgd 		if (++sc->sc_no_td == sc->sc_ntbuf) {
    740   1.1       cgd 			ifp->if_flags |= IFF_OACTIVE;
    741   1.1       cgd 			break;
    742   1.1       cgd 		}
    743   1.1       cgd 
    744   1.1       cgd 	}
    745   1.1       cgd 
    746   1.1       cgd 	sc->sc_last_td = bix;
    747   1.1       cgd }
    748   1.1       cgd 
    749   1.1       cgd /*
    750   1.1       cgd  * Process an ioctl request.
    751   1.1       cgd  */
    752   1.1       cgd int
    753   1.1       cgd leioctl(ifp, cmd, data)
    754   1.1       cgd 	register struct ifnet *ifp;
    755   1.1       cgd 	u_long cmd;
    756   1.1       cgd 	caddr_t data;
    757   1.1       cgd {
    758   1.1       cgd 	struct le_softc *sc = LE_SOFTC(ifp->if_unit);
    759   1.1       cgd 	struct ifaddr *ifa = (struct ifaddr *)data;
    760   1.1       cgd 	struct ifreq *ifr = (struct ifreq *)data;
    761   1.1       cgd 	int s, error = 0;
    762   1.1       cgd 
    763   1.1       cgd 	s = splimp();
    764   1.1       cgd 
    765   1.1       cgd 	switch (cmd) {
    766   1.1       cgd 
    767   1.1       cgd 	case SIOCSIFADDR:
    768   1.1       cgd 		ifp->if_flags |= IFF_UP;
    769   1.1       cgd 
    770   1.1       cgd 		switch (ifa->ifa_addr->sa_family) {
    771   1.1       cgd #ifdef INET
    772   1.1       cgd 		case AF_INET:
    773   1.1       cgd 			leinit(sc);
    774   1.1       cgd 			arp_ifinit(&sc->sc_arpcom, ifa);
    775   1.1       cgd 			break;
    776   1.1       cgd #endif
    777   1.1       cgd #ifdef NS
    778   1.1       cgd 		case AF_NS:
    779   1.1       cgd 		    {
    780   1.1       cgd 			register struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
    781   1.1       cgd 
    782   1.1       cgd 			if (ns_nullhost(*ina))
    783   1.1       cgd 				ina->x_host =
    784   1.1       cgd 				    *(union ns_host *)(sc->sc_arpcom.ac_enaddr);
    785   1.1       cgd 			else
    786   1.1       cgd 				bcopy(ina->x_host.c_host,
    787   1.1       cgd 				    sc->sc_arpcom.ac_enaddr,
    788   1.1       cgd 				    sizeof(sc->sc_arpcom.ac_enaddr));
    789   1.1       cgd 			/* Set new address. */
    790   1.1       cgd 			leinit(sc);
    791   1.1       cgd 			break;
    792   1.1       cgd 		    }
    793   1.1       cgd #endif
    794   1.1       cgd 		default:
    795   1.1       cgd 			leinit(sc);
    796   1.1       cgd 			break;
    797   1.1       cgd 		}
    798   1.1       cgd 		break;
    799   1.1       cgd 
    800   1.1       cgd #if defined(CCITT) && defined(LLC)
    801   1.1       cgd 	case SIOCSIFCONF_X25:
    802   1.1       cgd 		ifp->if_flags |= IFF_UP;
    803  1.11  christos 		ifa->ifa_rtrequest = cons_rtrequest; /* XXX */
    804   1.1       cgd 		error = x25_llcglue(PRC_IFUP, ifa->ifa_addr);
    805   1.1       cgd 		if (error == 0)
    806   1.1       cgd 			leinit(sc);
    807   1.1       cgd 		break;
    808   1.1       cgd #endif /* CCITT && LLC */
    809   1.1       cgd 
    810   1.1       cgd 	case SIOCSIFFLAGS:
    811   1.1       cgd 		if ((ifp->if_flags & IFF_UP) == 0 &&
    812   1.1       cgd 		    (ifp->if_flags & IFF_RUNNING) != 0) {
    813   1.1       cgd 			/*
    814   1.1       cgd 			 * If interface is marked down and it is running, then
    815   1.1       cgd 			 * stop it.
    816   1.1       cgd 			 */
    817   1.1       cgd 			lestop(sc);
    818   1.1       cgd 			ifp->if_flags &= ~IFF_RUNNING;
    819   1.1       cgd 		} else if ((ifp->if_flags & IFF_UP) != 0 &&
    820   1.1       cgd 		    	   (ifp->if_flags & IFF_RUNNING) == 0) {
    821   1.1       cgd 			/*
    822   1.1       cgd 			 * If interface is marked up and it is stopped, then
    823   1.1       cgd 			 * start it.
    824   1.1       cgd 			 */
    825   1.1       cgd 			leinit(sc);
    826   1.1       cgd 		} else {
    827   1.1       cgd 			/*
    828   1.1       cgd 			 * Reset the interface to pick up changes in any other
    829   1.1       cgd 			 * flags that affect hardware registers.
    830   1.1       cgd 			 */
    831   1.1       cgd 			/*lestop(sc);*/
    832   1.1       cgd 			leinit(sc);
    833   1.1       cgd 		}
    834   1.1       cgd #ifdef LEDEBUG
    835   1.1       cgd 		if (ifp->if_flags & IFF_DEBUG)
    836   1.1       cgd 			sc->sc_debug = 1;
    837   1.1       cgd 		else
    838   1.1       cgd 			sc->sc_debug = 0;
    839   1.1       cgd #endif
    840   1.1       cgd 		break;
    841   1.1       cgd 
    842   1.1       cgd 	case SIOCADDMULTI:
    843   1.1       cgd 	case SIOCDELMULTI:
    844   1.1       cgd 		error = (cmd == SIOCADDMULTI) ?
    845   1.2   mycroft 		    ether_addmulti(ifr, &sc->sc_arpcom) :
    846   1.1       cgd 		    ether_delmulti(ifr, &sc->sc_arpcom);
    847   1.1       cgd 
    848   1.1       cgd 		if (error == ENETRESET) {
    849   1.1       cgd 			/*
    850   1.1       cgd 			 * Multicast list has changed; set the hardware filter
    851   1.1       cgd 			 * accordingly.
    852   1.1       cgd 			 */
    853   1.2   mycroft 			lereset(sc);
    854   1.1       cgd 			error = 0;
    855   1.1       cgd 		}
    856   1.1       cgd 		break;
    857   1.1       cgd 
    858   1.1       cgd 	default:
    859   1.1       cgd 		error = EINVAL;
    860   1.2   mycroft 		break;
    861   1.1       cgd 	}
    862   1.1       cgd 
    863   1.1       cgd 	splx(s);
    864   1.1       cgd 	return (error);
    865   1.1       cgd }
    866   1.1       cgd 
    867   1.1       cgd #ifdef LEDEBUG
    868   1.1       cgd void
    869   1.1       cgd recv_print(sc, no)
    870   1.1       cgd 	struct le_softc *sc;
    871   1.1       cgd 	int no;
    872   1.1       cgd {
    873   1.1       cgd 	struct lermd rmd;
    874   1.1       cgd 	u_int16_t len;
    875   1.1       cgd 	struct ether_header eh;
    876   1.1       cgd 
    877   1.1       cgd 	(*sc->sc_copyfromdesc)(sc, &rmd, LE_RMDADDR(sc, no), sizeof(rmd));
    878   1.1       cgd 	len = rmd.rmd3;
    879   1.1       cgd 	printf("%s: receive buffer %d, len = %d\n", sc->sc_dev.dv_xname, no,
    880   1.1       cgd 	    len);
    881   1.1       cgd 	printf("%s: status %04x\n", sc->sc_dev.dv_xname, lerdcsr(sc, LE_CSR0));
    882   1.1       cgd 	printf("%s: ladr %04x, hadr %02x, flags %02x, bcnt %04x, mcnt %04x\n",
    883   1.1       cgd 	    sc->sc_dev.dv_xname,
    884   1.1       cgd 	    rmd.rmd0, rmd.rmd1_hadr, rmd.rmd1_bits, rmd.rmd2, rmd.rmd3);
    885   1.1       cgd 	if (len >= sizeof(eh)) {
    886   1.1       cgd 		(*sc->sc_copyfrombuf)(sc, &eh, LE_RBUFADDR(sc, no), sizeof(eh));
    887   1.1       cgd 		printf("%s: dst %s", ether_sprintf(eh.ether_dhost));
    888   1.1       cgd 		printf(" src %s type %04x\n", ether_sprintf(eh.ether_shost),
    889   1.1       cgd 		    ntohs(eh.ether_type));
    890   1.1       cgd 	}
    891   1.1       cgd }
    892   1.1       cgd 
    893   1.1       cgd void
    894   1.1       cgd xmit_print(sc, no)
    895   1.1       cgd 	struct le_softc *sc;
    896   1.1       cgd 	int no;
    897   1.1       cgd {
    898   1.1       cgd 	struct letmd tmd;
    899   1.1       cgd 	u_int16_t len;
    900   1.1       cgd 	struct ether_header eh;
    901   1.1       cgd 
    902   1.1       cgd 	(*sc->sc_copyfromdesc)(sc, &tmd, LE_TMDADDR(sc, no), sizeof(tmd));
    903   1.1       cgd 	len = -tmd.tmd2;
    904   1.1       cgd 	printf("%s: transmit buffer %d, len = %d\n", sc->sc_dev.dv_xname, no,
    905   1.1       cgd 	    len);
    906   1.1       cgd 	printf("%s: status %04x\n", sc->sc_dev.dv_xname, lerdcsr(sc, LE_CSR0));
    907   1.1       cgd 	printf("%s: ladr %04x, hadr %02x, flags %02x, bcnt %04x, mcnt %04x\n",
    908   1.1       cgd 	    sc->sc_dev.dv_xname,
    909   1.1       cgd 	    tmd.tmd0, tmd.tmd1_hadr, tmd.tmd1_bits, tmd.tmd2, tmd.tmd3);
    910   1.1       cgd 	if (len >= sizeof(eh)) {
    911   1.1       cgd 		(*sc->sc_copyfrombuf)(sc, &eh, LE_TBUFADDR(sc, no), sizeof(eh));
    912   1.1       cgd 		printf("%s: dst %s", ether_sprintf(eh.ether_dhost));
    913   1.1       cgd 		printf(" src %s type %04x\n", ether_sprintf(eh.ether_shost),
    914   1.1       cgd 		    ntohs(eh.ether_type));
    915   1.1       cgd 	}
    916   1.1       cgd }
    917   1.1       cgd #endif /* LEDEBUG */
    918   1.1       cgd 
    919   1.1       cgd /*
    920   1.1       cgd  * Set up the logical address filter.
    921   1.1       cgd  */
    922   1.1       cgd void
    923   1.1       cgd lesetladrf(ac, af)
    924   1.1       cgd 	struct arpcom *ac;
    925   1.1       cgd 	u_int16_t *af;
    926   1.1       cgd {
    927   1.1       cgd 	struct ifnet *ifp = &ac->ac_if;
    928   1.1       cgd 	struct ether_multi *enm;
    929   1.1       cgd 	register u_char *cp, c;
    930   1.1       cgd 	register u_int32_t crc;
    931   1.1       cgd 	register int i, len;
    932   1.1       cgd 	struct ether_multistep step;
    933   1.1       cgd 
    934   1.1       cgd 	/*
    935   1.1       cgd 	 * Set up multicast address filter by passing all multicast addresses
    936   1.1       cgd 	 * through a crc generator, and then using the high order 6 bits as an
    937   1.1       cgd 	 * index into the 64 bit logical address filter.  The high order bit
    938   1.1       cgd 	 * selects the word, while the rest of the bits select the bit within
    939   1.1       cgd 	 * the word.
    940   1.1       cgd 	 */
    941   1.1       cgd 
    942   1.1       cgd 	if (ifp->if_flags & IFF_PROMISC)
    943   1.1       cgd 		goto allmulti;
    944   1.1       cgd 
    945   1.1       cgd 	af[0] = af[1] = af[2] = af[3] = 0x0000;
    946   1.1       cgd 	ETHER_FIRST_MULTI(step, ac, enm);
    947   1.1       cgd 	while (enm != NULL) {
    948  1.13       gwr 		if (ETHER_CMP(enm->enm_addrlo, enm->enm_addrhi)) {
    949   1.1       cgd 			/*
    950   1.1       cgd 			 * We must listen to a range of multicast addresses.
    951   1.1       cgd 			 * For now, just accept all multicasts, rather than
    952   1.1       cgd 			 * trying to set only those filter bits needed to match
    953   1.1       cgd 			 * the range.  (At this time, the only use of address
    954   1.1       cgd 			 * ranges is for IP multicast routing, for which the
    955   1.1       cgd 			 * range is big enough to require all bits set.)
    956   1.1       cgd 			 */
    957   1.1       cgd 			goto allmulti;
    958   1.1       cgd 		}
    959   1.1       cgd 
    960   1.1       cgd 		cp = enm->enm_addrlo;
    961   1.1       cgd 		crc = 0xffffffff;
    962   1.1       cgd 		for (len = sizeof(enm->enm_addrlo); --len >= 0;) {
    963   1.1       cgd 			c = *cp++;
    964   1.1       cgd 			for (i = 8; --i >= 0;) {
    965   1.1       cgd 				if ((crc & 0x01) ^ (c & 0x01)) {
    966   1.1       cgd 					crc >>= 1;
    967   1.1       cgd 					crc ^= 0xedb88320;
    968   1.1       cgd 				} else
    969   1.1       cgd 					crc >>= 1;
    970   1.1       cgd 				c >>= 1;
    971   1.1       cgd 			}
    972   1.1       cgd 		}
    973   1.1       cgd 		/* Just want the 6 most significant bits. */
    974   1.1       cgd 		crc >>= 26;
    975   1.1       cgd 
    976   1.1       cgd 		/* Set the corresponding bit in the filter. */
    977   1.1       cgd 		af[crc >> 4] |= 1 << (crc & 0xf);
    978   1.1       cgd 
    979   1.1       cgd 		ETHER_NEXT_MULTI(step, enm);
    980   1.1       cgd 	}
    981   1.1       cgd 	ifp->if_flags &= ~IFF_ALLMULTI;
    982   1.1       cgd 	return;
    983   1.1       cgd 
    984   1.1       cgd allmulti:
    985   1.1       cgd 	ifp->if_flags |= IFF_ALLMULTI;
    986   1.1       cgd 	af[0] = af[1] = af[2] = af[3] = 0xffff;
    987   1.1       cgd }
    988   1.1       cgd 
    989   1.1       cgd 
    990   1.1       cgd /*
    991   1.4       cgd  * Routines for accessing the transmit and receive buffers.
    992   1.4       cgd  * The various CPU and adapter configurations supported by this
    993   1.4       cgd  * driver require three different access methods for buffers
    994   1.4       cgd  * and descriptors:
    995   1.4       cgd  *	(1) contig (contiguous data; no padding),
    996   1.4       cgd  *	(2) gap2 (two bytes of data followed by two bytes of padding),
    997   1.4       cgd  *	(3) gap16 (16 bytes of data followed by 16 bytes of padding).
    998   1.1       cgd  */
    999   1.1       cgd 
   1000   1.4       cgd #ifdef LE_NEED_BUF_CONTIG
   1001   1.4       cgd /*
   1002   1.4       cgd  * contig: contiguous data with no padding.
   1003   1.4       cgd  *
   1004   1.4       cgd  * Buffers may have any alignment.
   1005   1.4       cgd  */
   1006   1.1       cgd 
   1007   1.4       cgd integrate void
   1008   1.1       cgd copytobuf_contig(sc, from, boff, len)
   1009   1.1       cgd 	struct le_softc *sc;
   1010   1.4       cgd 	void *from;
   1011   1.1       cgd 	int boff, len;
   1012   1.1       cgd {
   1013   1.1       cgd 	volatile caddr_t buf = sc->sc_mem;
   1014   1.1       cgd 
   1015   1.1       cgd 	/*
   1016   1.1       cgd 	 * Just call bcopy() to do the work.
   1017   1.1       cgd 	 */
   1018   1.1       cgd 	bcopy(from, buf + boff, len);
   1019   1.1       cgd }
   1020   1.1       cgd 
   1021   1.4       cgd integrate void
   1022   1.1       cgd copyfrombuf_contig(sc, to, boff, len)
   1023   1.1       cgd 	struct le_softc *sc;
   1024   1.4       cgd 	void *to;
   1025   1.1       cgd 	int boff, len;
   1026   1.1       cgd {
   1027   1.1       cgd 	volatile caddr_t buf = sc->sc_mem;
   1028   1.1       cgd 
   1029   1.1       cgd 	/*
   1030   1.1       cgd 	 * Just call bcopy() to do the work.
   1031   1.1       cgd 	 */
   1032   1.1       cgd 	bcopy(buf + boff, to, len);
   1033   1.1       cgd }
   1034   1.1       cgd 
   1035   1.4       cgd integrate void
   1036   1.1       cgd zerobuf_contig(sc, boff, len)
   1037   1.1       cgd 	struct le_softc *sc;
   1038   1.1       cgd 	int boff, len;
   1039   1.1       cgd {
   1040   1.1       cgd 	volatile caddr_t buf = sc->sc_mem;
   1041   1.1       cgd 
   1042   1.1       cgd 	/*
   1043   1.1       cgd 	 * Just let bzero() do the work
   1044   1.1       cgd 	 */
   1045   1.1       cgd 	bzero(buf + boff, len);
   1046   1.1       cgd }
   1047   1.4       cgd #endif /* LE_NEED_BUF_CONTIG */
   1048   1.1       cgd 
   1049   1.4       cgd #ifdef LE_NEED_BUF_GAP2
   1050   1.1       cgd /*
   1051   1.4       cgd  * gap2: two bytes of data followed by two bytes of pad.
   1052   1.4       cgd  *
   1053   1.4       cgd  * Buffers must be 4-byte aligned.  The code doesn't worry about
   1054   1.4       cgd  * doing an extra byte.
   1055   1.1       cgd  */
   1056   1.4       cgd 
   1057   1.4       cgd integrate void
   1058   1.4       cgd copytobuf_gap2(sc, fromv, boff, len)
   1059   1.1       cgd 	struct le_softc *sc;
   1060   1.4       cgd 	void *fromv;
   1061   1.1       cgd 	int boff;
   1062   1.1       cgd 	register int len;
   1063   1.1       cgd {
   1064   1.1       cgd 	volatile caddr_t buf = sc->sc_mem;
   1065   1.4       cgd 	register caddr_t from = fromv;
   1066   1.4       cgd 	register volatile u_int16_t *bptr;
   1067   1.1       cgd 	register int xfer;
   1068   1.1       cgd 
   1069   1.1       cgd 	if (boff & 0x1) {
   1070   1.1       cgd 		/* handle unaligned first byte */
   1071   1.4       cgd 		bptr = ((volatile u_int16_t *)buf) + (boff - 1);
   1072   1.1       cgd 		*bptr = (*from++ << 8) | (*bptr & 0xff);
   1073   1.1       cgd 		bptr += 2;
   1074   1.1       cgd 		len--;
   1075   1.1       cgd 	} else
   1076   1.4       cgd 		bptr = ((volatile u_int16_t *)buf) + boff;
   1077   1.4       cgd 	while (len > 1) {
   1078   1.4       cgd 		*bptr = (from[1] << 8) | (from[0] & 0xff);
   1079   1.4       cgd 		bptr += 2;
   1080   1.4       cgd 		from += 2;
   1081   1.4       cgd 		len -= 2;
   1082   1.1       cgd 	}
   1083   1.1       cgd 	if (len == 1)
   1084   1.4       cgd 		*bptr = (u_int16_t)*from;
   1085   1.1       cgd }
   1086   1.1       cgd 
   1087   1.4       cgd integrate void
   1088   1.4       cgd copyfrombuf_gap2(sc, tov, boff, len)
   1089   1.1       cgd 	struct le_softc *sc;
   1090   1.4       cgd 	void *tov;
   1091   1.1       cgd 	int boff, len;
   1092   1.1       cgd {
   1093   1.1       cgd 	volatile caddr_t buf = sc->sc_mem;
   1094   1.4       cgd 	register caddr_t to = tov;
   1095   1.4       cgd 	register volatile u_int16_t *bptr;
   1096   1.4       cgd 	register u_int16_t tmp;
   1097   1.1       cgd 	register int xfer;
   1098   1.1       cgd 
   1099   1.1       cgd 	if (boff & 0x1) {
   1100   1.1       cgd 		/* handle unaligned first byte */
   1101   1.4       cgd 		bptr = ((volatile u_int16_t *)buf) + (boff - 1);
   1102   1.1       cgd 		*to++ = (*bptr >> 8) & 0xff;
   1103   1.1       cgd 		bptr += 2;
   1104   1.1       cgd 		len--;
   1105   1.1       cgd 	} else
   1106   1.4       cgd 		bptr = ((volatile u_int16_t *)buf) + boff;
   1107   1.4       cgd 	while (len > 1) {
   1108   1.4       cgd 		tmp = *bptr;
   1109   1.4       cgd 		*to++ = tmp & 0xff;
   1110   1.4       cgd 		*to++ = (tmp >> 8) & 0xff;
   1111   1.4       cgd 		bptr += 2;
   1112   1.4       cgd 		len -= 2;
   1113   1.1       cgd 	}
   1114   1.1       cgd 	if (len == 1)
   1115   1.1       cgd 		*to = *bptr & 0xff;
   1116   1.1       cgd }
   1117   1.1       cgd 
   1118   1.4       cgd integrate void
   1119   1.1       cgd zerobuf_gap2(sc, boff, len)
   1120   1.1       cgd 	struct le_softc *sc;
   1121   1.1       cgd 	int boff, len;
   1122   1.1       cgd {
   1123   1.1       cgd 	volatile caddr_t buf = sc->sc_mem;
   1124   1.4       cgd 	register volatile u_int16_t *bptr;
   1125   1.1       cgd 
   1126   1.1       cgd 	if ((unsigned)boff & 0x1) {
   1127   1.4       cgd 		bptr = ((volatile u_int16_t *)buf) + (boff - 1);
   1128   1.1       cgd 		*bptr &= 0xff;
   1129   1.1       cgd 		bptr += 2;
   1130   1.1       cgd 		len--;
   1131   1.1       cgd 	} else
   1132   1.4       cgd 		bptr = ((volatile u_int16_t *)buf) + boff;
   1133   1.1       cgd 	while (len > 0) {
   1134   1.1       cgd 		*bptr = 0;
   1135   1.1       cgd 		bptr += 2;
   1136   1.1       cgd 		len -= 2;
   1137   1.1       cgd 	}
   1138   1.1       cgd }
   1139   1.4       cgd #endif /* LE_NEED_BUF_GAP2 */
   1140   1.1       cgd 
   1141   1.4       cgd #ifdef LE_NEED_BUF_GAP16
   1142   1.1       cgd /*
   1143   1.4       cgd  * gap16: 16 bytes of data followed by 16 bytes of pad.
   1144   1.4       cgd  *
   1145   1.4       cgd  * Buffers must be 32-byte aligned.
   1146   1.1       cgd  */
   1147   1.4       cgd 
   1148   1.4       cgd integrate void
   1149   1.4       cgd copytobuf_gap16(sc, fromv, boff, len)
   1150   1.1       cgd 	struct le_softc *sc;
   1151   1.4       cgd 	void *fromv;
   1152   1.1       cgd 	int boff;
   1153   1.1       cgd 	register int len;
   1154   1.1       cgd {
   1155   1.1       cgd 	volatile caddr_t buf = sc->sc_mem;
   1156   1.4       cgd 	register caddr_t from = fromv;
   1157   1.1       cgd 	register caddr_t bptr;
   1158   1.1       cgd 	register int xfer;
   1159   1.1       cgd 
   1160   1.1       cgd 	bptr = buf + ((boff << 1) & ~0x1f);
   1161   1.1       cgd 	boff &= 0xf;
   1162   1.1       cgd 	xfer = min(len, 16 - boff);
   1163   1.1       cgd 	while (len > 0) {
   1164   1.1       cgd 		bcopy(from, bptr + boff, xfer);
   1165   1.1       cgd 		from += xfer;
   1166   1.1       cgd 		bptr += 32;
   1167   1.1       cgd 		boff = 0;
   1168   1.1       cgd 		len -= xfer;
   1169   1.1       cgd 		xfer = min(len, 16);
   1170   1.1       cgd 	}
   1171   1.1       cgd }
   1172   1.1       cgd 
   1173   1.4       cgd integrate void
   1174   1.4       cgd copyfrombuf_gap16(sc, tov, boff, len)
   1175   1.1       cgd 	struct le_softc *sc;
   1176   1.4       cgd 	void *tov;
   1177   1.1       cgd 	int boff, len;
   1178   1.1       cgd {
   1179   1.1       cgd 	volatile caddr_t buf = sc->sc_mem;
   1180   1.4       cgd 	register caddr_t to = tov;
   1181   1.1       cgd 	register caddr_t bptr;
   1182   1.1       cgd 	register int xfer;
   1183   1.1       cgd 
   1184   1.1       cgd 	bptr = buf + ((boff << 1) & ~0x1f);
   1185   1.1       cgd 	boff &= 0xf;
   1186   1.1       cgd 	xfer = min(len, 16 - boff);
   1187   1.1       cgd 	while (len > 0) {
   1188   1.1       cgd 		bcopy(bptr + boff, to, xfer);
   1189   1.1       cgd 		to += xfer;
   1190   1.1       cgd 		bptr += 32;
   1191   1.1       cgd 		boff = 0;
   1192   1.1       cgd 		len -= xfer;
   1193   1.1       cgd 		xfer = min(len, 16);
   1194   1.1       cgd 	}
   1195   1.1       cgd }
   1196   1.1       cgd 
   1197   1.4       cgd integrate void
   1198   1.1       cgd zerobuf_gap16(sc, boff, len)
   1199   1.1       cgd 	struct le_softc *sc;
   1200   1.1       cgd 	int boff, len;
   1201   1.1       cgd {
   1202   1.1       cgd 	volatile caddr_t buf = sc->sc_mem;
   1203   1.1       cgd 	register caddr_t bptr;
   1204   1.1       cgd 	register int xfer;
   1205   1.1       cgd 
   1206   1.1       cgd 	bptr = buf + ((boff << 1) & ~0x1f);
   1207   1.1       cgd 	boff &= 0xf;
   1208   1.1       cgd 	xfer = min(len, 16 - boff);
   1209   1.1       cgd 	while (len > 0) {
   1210   1.1       cgd 		bzero(bptr + boff, xfer);
   1211   1.1       cgd 		bptr += 32;
   1212   1.1       cgd 		boff = 0;
   1213   1.1       cgd 		len -= xfer;
   1214   1.1       cgd 		xfer = min(len, 16);
   1215   1.1       cgd 	}
   1216   1.1       cgd }
   1217   1.4       cgd #endif /* LE_NEED_BUF_GAP16 */
   1218