Home | History | Annotate | Line # | Download | only in dev
if_ae.c revision 1.51
      1  1.48  christos /*	$NetBSD: if_ae.c,v 1.51 1997/02/22 02:17:40 scottr Exp $	*/
      2  1.14       cgd 
      3   1.1    briggs /*
      4  1.21    briggs  * Device driver for National Semiconductor DS8390/WD83C690 based ethernet
      5  1.21    briggs  * adapters.
      6   1.1    briggs  *
      7  1.21    briggs  * Copyright (c) 1994, 1995 Charles M. Hannum.  All rights reserved.
      8   1.1    briggs  *
      9  1.21    briggs  * Copyright (C) 1993, David Greenman.  This software may be used, modified,
     10  1.21    briggs  * copied, distributed, and sold, in both source and binary form provided that
     11  1.21    briggs  * the above copyright and these terms are retained.  Under no circumstances is
     12  1.21    briggs  * the author responsible for the proper functioning of this software, nor does
     13  1.21    briggs  * the author assume any responsibility for damages incurred with its use.
     14   1.1    briggs  *
     15  1.21    briggs  * Adapted for MacBSD by Brad Parker <brad (at) fcr.com>.
     16   1.1    briggs  *
     17   1.1    briggs  * Currently supports:
     18   1.1    briggs  *	Apples NB Ethernet card
     19   1.1    briggs  *	Interlan A310 Nubus Ethernet card
     20   1.1    briggs  *	Cayman Systems GatorCard
     21  1.15    briggs  *	Asante MacCon II/E
     22   1.1    briggs  */
     23   1.1    briggs 
     24   1.1    briggs #include "bpfilter.h"
     25   1.1    briggs 
     26   1.9    briggs #include <sys/param.h>
     27  1.22    briggs #include <sys/types.h>
     28   1.9    briggs #include <sys/systm.h>
     29   1.9    briggs #include <sys/errno.h>
     30   1.9    briggs #include <sys/ioctl.h>
     31   1.9    briggs #include <sys/mbuf.h>
     32   1.9    briggs #include <sys/socket.h>
     33   1.9    briggs #include <sys/syslog.h>
     34  1.21    briggs #include <sys/device.h>
     35   1.1    briggs 
     36   1.5    briggs #include <net/if.h>
     37   1.5    briggs #include <net/if_dl.h>
     38   1.5    briggs #include <net/if_types.h>
     39   1.5    briggs #include <net/netisr.h>
     40   1.1    briggs 
     41   1.1    briggs #ifdef INET
     42   1.5    briggs #include <netinet/in.h>
     43   1.5    briggs #include <netinet/in_systm.h>
     44   1.5    briggs #include <netinet/in_var.h>
     45   1.5    briggs #include <netinet/ip.h>
     46   1.5    briggs #include <netinet/if_ether.h>
     47   1.1    briggs #endif
     48   1.1    briggs 
     49   1.1    briggs #ifdef NS
     50   1.5    briggs #include <netns/ns.h>
     51   1.5    briggs #include <netns/ns_if.h>
     52   1.1    briggs #endif
     53   1.1    briggs 
     54   1.1    briggs #if NBPFILTER > 0
     55   1.5    briggs #include <net/bpf.h>
     56   1.5    briggs #include <net/bpfdesc.h>
     57   1.1    briggs #endif
     58   1.1    briggs 
     59  1.51    scottr #include <machine/bus.h>
     60  1.42    briggs #include <machine/viareg.h>
     61  1.51    scottr 
     62   1.3    briggs #include "nubus.h"
     63  1.31       cgd #include <dev/ic/dp8390reg.h>
     64   1.1    briggs #include "if_aereg.h"
     65   1.1    briggs 
     66  1.28    briggs #define INTERFACE_NAME_LEN	32
     67  1.28    briggs 
     68   1.1    briggs /*
     69   1.1    briggs  * ae_softc: per line info and status
     70   1.1    briggs  */
     71  1.25    briggs struct ae_softc {
     72  1.28    briggs 	struct device	sc_dev;
     73  1.51    scottr 	bus_space_tag_t	sc_tag;
     74  1.51    scottr 	bus_space_handle_t sc_handle;
     75  1.51    scottr 
     76  1.28    briggs 	nubus_slot	sc_slot;
     77  1.28    briggs /*	struct	intrhand sc_ih;	*/
     78   1.3    briggs 
     79  1.25    briggs 	struct arpcom sc_arpcom;/* ethernet common */
     80   1.1    briggs 
     81  1.28    briggs 	char	type_str[INTERFACE_NAME_LEN];	/* type string */
     82  1.28    briggs 	u_short	type;		/* interface type code */
     83  1.28    briggs 	u_char	vendor;		/* interface vendor */
     84  1.28    briggs 	u_char	regs_rev;	/* registers are reversed */
     85  1.15    briggs 
     86  1.15    briggs #define	REG_MAP(sc, reg)	((sc)->regs_rev ? (0x0f-(reg))<<2 : (reg)<<2)
     87   1.1    briggs #define NIC_GET(sc, reg)	((sc)->nic_addr[REG_MAP(sc, reg)])
     88   1.1    briggs #define NIC_PUT(sc, reg, val)	((sc)->nic_addr[REG_MAP(sc, reg)] = (val))
     89  1.25    briggs 	volatile caddr_t nic_addr;	/* NIC (DS8390) I/O bus address */
     90  1.25    briggs 	caddr_t rom_addr;	/* on board prom address */
     91   1.1    briggs 
     92  1.25    briggs 	u_char  cr_proto;	/* values always set in CR */
     93  1.21    briggs 
     94  1.25    briggs 	caddr_t mem_start;	/* shared memory start address */
     95  1.25    briggs 	caddr_t mem_end;	/* shared memory end address */
     96  1.25    briggs 	u_long  mem_size;	/* total shared memory size */
     97  1.25    briggs 	caddr_t mem_ring;	/* start of RX ring-buffer (in smem) */
     98  1.25    briggs 
     99  1.25    briggs 	u_char  txb_cnt;	/* Number of transmit buffers */
    100  1.34    briggs 	u_char  txb_inuse;	/* number of transmit buffers active */
    101  1.25    briggs 
    102  1.25    briggs 	u_char  txb_new;	/* pointer to where new buffer will be added */
    103  1.25    briggs 	u_char  txb_next_tx;	/* pointer to next buffer ready to xmit */
    104  1.25    briggs 	u_short txb_len[8];	/* buffered xmit buffer lengths */
    105  1.25    briggs 	u_char  tx_page_start;	/* first page of TX buffer area */
    106  1.25    briggs 	u_char  rec_page_start;	/* first page of RX ring-buffer */
    107  1.25    briggs 	u_char  rec_page_stop;	/* last page of RX ring-buffer */
    108  1.25    briggs 	u_char  next_packet;	/* pointer to next unread RX packet */
    109  1.21    briggs };
    110   1.1    briggs 
    111  1.49    scottr static int	ae_card_vendor __P((struct nubus_attach_args *na));
    112  1.49    scottr static int	ae_size_card_memory __P((caddr_t addr));
    113  1.43    briggs 
    114  1.49    scottr int aematch __P((struct device *, struct cfdata *, void *));
    115  1.22    briggs void aeattach __P((struct device *, struct device *, void *));
    116  1.43    briggs void aeintr __P((void *, int));
    117  1.34    briggs int aeioctl __P((struct ifnet *, u_long, caddr_t));
    118  1.34    briggs void aestart __P((struct ifnet *));
    119  1.45   thorpej void aewatchdog __P((struct ifnet *));
    120  1.34    briggs void aereset __P((struct ae_softc *));
    121  1.34    briggs void aeinit __P((struct ae_softc *));
    122  1.34    briggs void aestop __P((struct ae_softc *));
    123  1.34    briggs 
    124  1.34    briggs void aeread __P((struct ae_softc *, caddr_t, int));
    125  1.34    briggs struct mbuf *aeget __P((struct ae_softc *, caddr_t, int));
    126  1.21    briggs 
    127  1.25    briggs #define inline			/* XXX for debugging porpoises */
    128  1.21    briggs 
    129  1.34    briggs u_short ae_put __P((struct ae_softc *, struct mbuf *, caddr_t));
    130  1.34    briggs void ae_getmcaf __P((struct arpcom *, u_char *));
    131  1.34    briggs 
    132  1.21    briggs static inline void ae_rint __P((struct ae_softc *));
    133  1.21    briggs static inline void ae_xmit __P((struct ae_softc *));
    134  1.28    briggs static inline caddr_t ae_ring_copy __P((
    135  1.43    briggs 		struct ae_softc *, caddr_t, caddr_t, int));
    136  1.28    briggs 
    137  1.41   thorpej struct cfattach ae_ca = {
    138  1.49    scottr 	sizeof(struct ae_softc), aematch, aeattach
    139  1.41   thorpej };
    140  1.41   thorpej 
    141  1.41   thorpej struct cfdriver ae_cd = {
    142  1.41   thorpej 	NULL, "ae", DV_IFNET
    143  1.28    briggs };
    144  1.28    briggs 
    145   1.1    briggs #define	ETHER_MIN_LEN	64
    146   1.1    briggs #define ETHER_MAX_LEN	1518
    147   1.1    briggs #define	ETHER_ADDR_LEN	6
    148   1.1    briggs 
    149  1.28    briggs static char zero = 0;
    150   1.8    briggs 
    151  1.12   lkestel /*
    152  1.12   lkestel  * XXX These two should be moved to locore, and maybe changed to use shorts
    153  1.12   lkestel  * instead of bytes.  The reason for these is that bcopy and bzero use longs,
    154  1.12   lkestel  * which the ethernet cards can't handle.
    155  1.12   lkestel  */
    156  1.12   lkestel 
    157  1.43    briggs void			bszero __P((u_short *addr, int len));
    158  1.43    briggs static inline void	word_copy __P((caddr_t a, caddr_t b, int len));
    159  1.43    briggs static inline void	byte_copy __P((caddr_t a, caddr_t b, int len));
    160  1.43    briggs 
    161  1.12   lkestel void
    162  1.25    briggs bszero(u_short * addr, int len)
    163  1.12   lkestel {
    164  1.21    briggs 	while (len--)
    165  1.12   lkestel 		*addr++ = 0;
    166  1.12   lkestel }
    167  1.30    briggs 
    168  1.21    briggs /*
    169  1.21    briggs  * Memory copy, copies word at time.
    170  1.21    briggs  */
    171  1.21    briggs static inline void
    172  1.21    briggs word_copy(a, b, len)
    173  1.21    briggs 	caddr_t a, b;
    174  1.25    briggs 	int     len;
    175  1.12   lkestel {
    176  1.25    briggs 	u_short *x = (u_short *) a, *y = (u_short *) b;
    177  1.12   lkestel 
    178  1.21    briggs 	len >>= 1;
    179  1.21    briggs 	while (len--)
    180  1.21    briggs 		*y++ = *x++;
    181  1.15    briggs }
    182  1.30    briggs 
    183  1.23    briggs /*
    184  1.23    briggs  * Memory copy, copies bytes at time.
    185  1.23    briggs  */
    186  1.23    briggs static inline void
    187  1.23    briggs byte_copy(a, b, len)
    188  1.23    briggs 	caddr_t a, b;
    189  1.25    briggs 	int     len;
    190  1.23    briggs {
    191  1.23    briggs 	while (len--)
    192  1.23    briggs 		*b++ = *a++;
    193  1.23    briggs }
    194  1.23    briggs 
    195  1.28    briggs static int
    196  1.49    scottr ae_card_vendor(na)
    197  1.49    scottr 	struct nubus_attach_args *na;
    198   1.8    briggs {
    199  1.49    scottr 	int vendor;
    200  1.28    briggs 
    201  1.49    scottr 	switch (na->drsw) {
    202  1.28    briggs 	case NUBUS_DRSW_3COM:
    203  1.28    briggs 	case NUBUS_DRSW_APPLE:
    204  1.36    briggs 	case NUBUS_DRSW_TECHWORKS:
    205  1.49    scottr 		vendor = AE_VENDOR_APPLE;
    206  1.28    briggs 		break;
    207  1.28    briggs 	case NUBUS_DRSW_ASANTE:
    208  1.49    scottr 		vendor = AE_VENDOR_ASANTE;
    209  1.28    briggs 		break;
    210  1.33    briggs 	case NUBUS_DRSW_FARALLON:
    211  1.49    scottr 		vendor = AE_VENDOR_FARALLON;
    212  1.33    briggs 		break;
    213  1.38    briggs 	case NUBUS_DRSW_FOCUS:
    214  1.49    scottr 		vendor = AE_VENDOR_FOCUS;
    215  1.38    briggs 		break;
    216  1.29    briggs 	case NUBUS_DRSW_GATOR:
    217  1.49    scottr 		switch (na->drhw) {
    218  1.29    briggs 		default:
    219  1.29    briggs 		case NUBUS_DRHW_INTERLAN:
    220  1.49    scottr 			vendor = AE_VENDOR_INTERLAN;
    221  1.29    briggs 			break;
    222  1.29    briggs 		case NUBUS_DRHW_KINETICS:
    223  1.49    scottr 			vendor = AE_VENDOR_DAYNA;
    224  1.29    briggs 			break;
    225  1.29    briggs 		}
    226  1.28    briggs 		break;
    227  1.28    briggs 	default:
    228  1.49    scottr #ifdef AE_DEBUG
    229  1.49    scottr 		printf("Unknown ethernet drsw: %x\n", na->drsw);
    230  1.49    scottr #endif
    231  1.49    scottr 		vendor = AE_VENDOR_UNKNOWN;
    232   1.8    briggs 	}
    233  1.49    scottr 	return vendor;
    234  1.15    briggs }
    235  1.15    briggs 
    236  1.43    briggs static int
    237  1.49    scottr ae_size_card_memory(addr)
    238  1.49    scottr 	caddr_t addr;
    239  1.15    briggs {
    240  1.15    briggs 	u_short *p;
    241  1.15    briggs 	u_short i1, i2, i3, i4;
    242  1.21    briggs 
    243  1.49    scottr 	p = (u_short *) addr;
    244  1.15    briggs 
    245  1.15    briggs 	/*
    246  1.15    briggs 	 * very simple size memory, assuming it's installed in 8k
    247  1.15    briggs 	 * banks; also assume it will generally mirror in upper banks
    248  1.15    briggs 	 * if not installed.
    249  1.15    briggs 	 */
    250  1.25    briggs 	i1 = (8192 * 0) / 2;
    251  1.25    briggs 	i2 = (8192 * 1) / 2;
    252  1.25    briggs 	i3 = (8192 * 2) / 2;
    253  1.25    briggs 	i4 = (8192 * 3) / 2;
    254  1.21    briggs 
    255  1.15    briggs 	p[i1] = 0x1111;
    256  1.15    briggs 	p[i2] = 0x2222;
    257  1.15    briggs 	p[i3] = 0x3333;
    258  1.15    briggs 	p[i4] = 0x4444;
    259  1.21    briggs 
    260  1.15    briggs 	if (p[i1] == 0x1111 && p[i2] == 0x2222 &&
    261  1.15    briggs 	    p[i3] == 0x3333 && p[i4] == 0x4444)
    262  1.25    briggs 		return 8192 * 4;
    263  1.21    briggs 
    264  1.21    briggs 	if ((p[i1] == 0x1111 && p[i2] == 0x2222) ||
    265  1.21    briggs 	    (p[i1] == 0x3333 && p[i2] == 0x4444))
    266  1.25    briggs 		return 8192 * 2;
    267  1.15    briggs 
    268  1.21    briggs 	if (p[i1] == 0x1111 || p[i1] == 0x4444)
    269  1.21    briggs 		return 8192;
    270  1.15    briggs 
    271  1.21    briggs 	return 0;
    272   1.8    briggs }
    273   1.8    briggs 
    274   1.1    briggs int
    275  1.49    scottr aematch(parent, cf, aux)
    276  1.21    briggs 	struct device *parent;
    277  1.49    scottr 	struct cfdata *cf;
    278  1.49    scottr 	void *aux;
    279   1.1    briggs {
    280  1.49    scottr 	struct nubus_attach_args *na = (struct nubus_attach_args *) aux;
    281  1.49    scottr 
    282  1.49    scottr 	if (na->category != NUBUS_CATEGORY_NETWORK)
    283  1.49    scottr 		return 0;
    284   1.1    briggs 
    285  1.49    scottr 	if (na->type != NUBUS_TYPE_ETHERNET)
    286  1.49    scottr 		return 0;
    287  1.49    scottr 
    288  1.49    scottr 	switch (ae_card_vendor(na)) {
    289  1.49    scottr 	case AE_VENDOR_APPLE:
    290  1.49    scottr 	case AE_VENDOR_ASANTE:
    291  1.49    scottr 	case AE_VENDOR_FARALLON:
    292  1.49    scottr 	case AE_VENDOR_INTERLAN:
    293  1.49    scottr 		break;
    294  1.49    scottr 
    295  1.49    scottr 	case AE_VENDOR_DAYNA:
    296  1.49    scottr 	case AE_VENDOR_FOCUS:
    297  1.49    scottr 	default:
    298   1.3    briggs 		return 0;
    299  1.49    scottr 	}
    300  1.49    scottr 	return 1;
    301  1.49    scottr }
    302  1.49    scottr 
    303  1.49    scottr /*
    304  1.49    scottr  * Install interface into kernel networking data structures
    305  1.49    scottr  */
    306  1.49    scottr void
    307  1.49    scottr aeattach(parent, self, aux)
    308  1.49    scottr 	struct device *parent, *self;
    309  1.49    scottr 	void   *aux;
    310  1.49    scottr {
    311  1.49    scottr 	struct ae_softc *sc = (struct ae_softc *) self;
    312  1.49    scottr 	struct nubus_attach_args *na = (struct nubus_attach_args *) aux;
    313  1.49    scottr 	struct ifnet *ifp = &sc->sc_arpcom.ac_if;
    314  1.49    scottr 	caddr_t addr;
    315  1.49    scottr 	int i, memsize;
    316  1.49    scottr 	int flags = 0;
    317   1.3    briggs 
    318  1.15    briggs 	sc->regs_rev = 0;
    319  1.49    scottr 	sc->vendor = ae_card_vendor(na);
    320  1.49    scottr 	strncpy(sc->type_str, nubus_get_card_name(na->fmt),
    321  1.49    scottr 	    INTERFACE_NAME_LEN);
    322  1.49    scottr 	sc->type_str[INTERFACE_NAME_LEN-1] = '\0';
    323  1.15    briggs 
    324  1.49    scottr 	addr = (caddr_t) na->fmt->virtual_base;
    325  1.50    scottr 	memsize = 0;
    326  1.28    briggs 
    327   1.1    briggs 	switch (sc->vendor) {
    328  1.25    briggs 	case AE_VENDOR_INTERLAN:
    329  1.28    briggs 		sc->nic_addr = addr + GC_NIC_OFFSET;
    330  1.28    briggs 		sc->rom_addr = addr + GC_ROM_OFFSET;
    331  1.28    briggs 		sc->mem_start = addr + GC_DATA_OFFSET;
    332  1.49    scottr 		if ((memsize = ae_size_card_memory(sc->mem_start)) == 0) {
    333  1.49    scottr 			printf(": failed to determine size of RAM.\n");
    334  1.49    scottr 			return;
    335  1.46    briggs 		}
    336   1.1    briggs 
    337   1.1    briggs 		/* reset the NIC chip */
    338  1.28    briggs 		*((caddr_t) addr + GC_RESET_OFFSET) = (char) zero;
    339  1.21    briggs 
    340   1.1    briggs 		/* Get station address from on-board ROM */
    341   1.1    briggs 		for (i = 0; i < ETHER_ADDR_LEN; ++i)
    342  1.25    briggs 			sc->sc_arpcom.ac_enaddr[i] = *(sc->rom_addr + i * 4);
    343   1.1    briggs 		break;
    344   1.1    briggs 
    345  1.34    briggs 		/* apple-compatible cards */
    346  1.25    briggs 	case AE_VENDOR_ASANTE:
    347  1.25    briggs 	case AE_VENDOR_APPLE:
    348  1.15    briggs 		sc->regs_rev = 1;
    349  1.28    briggs 		sc->nic_addr = addr + AE_NIC_OFFSET;
    350  1.28    briggs 		sc->rom_addr = addr + AE_ROM_OFFSET;
    351  1.28    briggs 		sc->mem_start = addr + AE_DATA_OFFSET;
    352  1.49    scottr 		if ((memsize = ae_size_card_memory(sc->mem_start)) == 0) {
    353  1.49    scottr 			printf(": failed to determine size of RAM.\n");
    354  1.49    scottr 			return;
    355  1.46    briggs 		}
    356   1.1    briggs 
    357   1.1    briggs 		/* Get station address from on-board ROM */
    358   1.1    briggs 		for (i = 0; i < ETHER_ADDR_LEN; ++i)
    359  1.25    briggs 			sc->sc_arpcom.ac_enaddr[i] = *(sc->rom_addr + i * 2);
    360   1.1    briggs 		break;
    361   1.8    briggs 
    362  1.25    briggs 	case AE_VENDOR_DAYNA:
    363  1.28    briggs 		sc->nic_addr = addr + DP_NIC_OFFSET;
    364  1.28    briggs 		sc->rom_addr = addr + DP_ROM_OFFSET;
    365  1.28    briggs 		sc->mem_start = addr + DP_DATA_OFFSET;
    366   1.8    briggs 		memsize = 8192;
    367   1.8    briggs 
    368   1.8    briggs 		/* Get station address from on-board ROM */
    369   1.8    briggs 		for (i = 0; i < ETHER_ADDR_LEN; ++i)
    370  1.25    briggs 			sc->sc_arpcom.ac_enaddr[i] = *(sc->rom_addr + i * 2);
    371  1.49    scottr 
    372  1.49    scottr 		printf(": unsupported Dayna hardware\n");
    373  1.49    scottr 		return;
    374  1.33    briggs 
    375  1.33    briggs 	case AE_VENDOR_FARALLON:
    376  1.33    briggs 		sc->regs_rev = 1;
    377  1.33    briggs 		sc->rom_addr = addr + FE_ROM_OFFSET;
    378  1.33    briggs 		sc->nic_addr = addr + AE_NIC_OFFSET;
    379  1.33    briggs 		sc->mem_start = addr + AE_DATA_OFFSET;
    380  1.49    scottr 		if ((memsize = ae_size_card_memory(sc->mem_start)) == 0) {
    381  1.49    scottr 			printf(": failed to determine size of RAM.\n");
    382  1.49    scottr 			return;
    383  1.46    briggs 		}
    384  1.33    briggs 
    385  1.33    briggs 		/* Get station address from on-board ROM */
    386  1.33    briggs 		for (i = 0; i < ETHER_ADDR_LEN; ++i)
    387  1.33    briggs 			sc->sc_arpcom.ac_enaddr[i] = *(sc->rom_addr + i);
    388   1.8    briggs 		break;
    389  1.38    briggs 	case AE_VENDOR_FOCUS:
    390  1.49    scottr 		printf(": unsupported Focus hardware\n");
    391  1.49    scottr 		return;
    392   1.1    briggs 	}
    393   1.7    briggs 
    394  1.22    briggs 	sc->cr_proto = ED_CR_RD2;
    395  1.21    briggs 
    396  1.21    briggs 	/* Allocate one xmit buffer if < 16k, two buffers otherwise. */
    397  1.21    briggs 	if ((memsize < 16384) || (flags & AE_FLAGS_NO_DOUBLE_BUFFERING))
    398   1.1    briggs 		sc->txb_cnt = 1;
    399  1.21    briggs 	else
    400   1.1    briggs 		sc->txb_cnt = 2;
    401   1.1    briggs 
    402   1.1    briggs 	sc->tx_page_start = 0;
    403  1.22    briggs 	sc->rec_page_start = sc->tx_page_start + sc->txb_cnt * ED_TXBUF_SIZE;
    404  1.22    briggs 	sc->rec_page_stop = sc->tx_page_start + (memsize >> ED_PAGE_SHIFT);
    405  1.22    briggs 	sc->mem_ring = sc->mem_start + (sc->rec_page_start << ED_PAGE_SHIFT);
    406  1.21    briggs 	sc->mem_size = memsize;
    407  1.21    briggs 	sc->mem_end = sc->mem_start + memsize;
    408   1.1    briggs 
    409  1.21    briggs 	/* Now zero memory and verify that it is clear. */
    410  1.25    briggs 	bszero((u_short *) sc->mem_start, memsize / 2);
    411   1.1    briggs 
    412   1.1    briggs 	for (i = 0; i < memsize; ++i)
    413  1.49    scottr 		if (sc->mem_start[i])
    414  1.48  christos printf("%s: failed to clear shared memory at %p - check configuration\n",
    415  1.21    briggs 			    sc->sc_dev.dv_xname,
    416  1.21    briggs 			    sc->mem_start + i);
    417  1.28    briggs 
    418  1.49    scottr 	bcopy(na->fmt, &sc->sc_slot, sizeof(nubus_slot));
    419  1.30    briggs 
    420   1.3    briggs 
    421  1.21    briggs 	/* Set interface to stopped condition (reset). */
    422  1.34    briggs 	aestop(sc);
    423   1.1    briggs 
    424  1.21    briggs 	/* Initialize ifnet structure. */
    425  1.45   thorpej 	bcopy(sc->sc_dev.dv_xname, ifp->if_xname, IFNAMSIZ);
    426  1.45   thorpej 	ifp->if_softc = sc;
    427  1.34    briggs 	ifp->if_start = aestart;
    428  1.34    briggs 	ifp->if_ioctl = aeioctl;
    429  1.34    briggs 	ifp->if_watchdog = aewatchdog;
    430  1.21    briggs 	ifp->if_flags =
    431  1.21    briggs 	    IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS | IFF_MULTICAST;
    432   1.3    briggs 
    433  1.21    briggs 	/* Attach the interface. */
    434   1.1    briggs 	if_attach(ifp);
    435  1.21    briggs 	ether_ifattach(ifp);
    436   1.1    briggs 
    437  1.21    briggs 	/* Print additional info when attached. */
    438  1.48  christos 	printf(": address %s, ", ether_sprintf(sc->sc_arpcom.ac_enaddr));
    439   1.1    briggs 
    440  1.49    scottr 	printf("type %s, %ldKB memory\n", sc->type_str, sc->mem_size / 1024);
    441   1.1    briggs 
    442   1.1    briggs #if NBPFILTER > 0
    443  1.21    briggs 	bpfattach(&ifp->if_bpf, ifp, DLT_EN10MB, sizeof(struct ether_header));
    444   1.1    briggs #endif
    445  1.21    briggs 
    446  1.21    briggs 	/* make sure interrupts are vectored to us */
    447  1.28    briggs 	add_nubus_intr(sc->sc_slot.slot, aeintr, sc);
    448  1.26    briggs 
    449  1.26    briggs 	/*
    450  1.26    briggs 	 * XXX -- enable nubus interrupts here.  Should be done elsewhere,
    451  1.26    briggs 	 *        but that currently breaks with some nubus video cards'
    452  1.26    briggs 	 *	  interrupts.  So we only enable nubus interrupts if we
    453  1.26    briggs 	 *	  have an ethernet card...  i.e., we do it here.
    454  1.26    briggs 	 */
    455  1.26    briggs 	enable_nubus_intr();
    456   1.3    briggs }
    457  1.34    briggs 
    458   1.1    briggs /*
    459   1.1    briggs  * Reset interface.
    460   1.1    briggs  */
    461  1.21    briggs void
    462  1.34    briggs aereset(sc)
    463   1.3    briggs 	struct ae_softc *sc;
    464   1.1    briggs {
    465  1.25    briggs 	int     s;
    466   1.1    briggs 
    467  1.37   mycroft 	s = splnet();
    468  1.34    briggs 	aestop(sc);
    469  1.34    briggs 	aeinit(sc);
    470  1.21    briggs 	splx(s);
    471  1.21    briggs }
    472  1.34    briggs 
    473   1.1    briggs /*
    474   1.1    briggs  * Take interface offline.
    475   1.1    briggs  */
    476   1.1    briggs void
    477  1.34    briggs aestop(sc)
    478   1.3    briggs 	struct ae_softc *sc;
    479   1.1    briggs {
    480  1.25    briggs 	int     n = 5000;
    481   1.1    briggs 
    482  1.21    briggs 	/* Stop everything on the interface, and select page 0 registers. */
    483  1.22    briggs 	NIC_PUT(sc, ED_P0_CR, sc->cr_proto | ED_CR_PAGE_0 | ED_CR_STP);
    484   1.1    briggs 
    485   1.1    briggs 	/*
    486  1.21    briggs 	 * Wait for interface to enter stopped state, but limit # of checks to
    487  1.21    briggs 	 * 'n' (about 5ms).  It shouldn't even take 5us on modern DS8390's, but
    488  1.21    briggs 	 * just in case it's an old one.
    489   1.1    briggs 	 */
    490  1.22    briggs 	while (((NIC_GET(sc, ED_P0_ISR) & ED_ISR_RST) == 0) && --n);
    491   1.1    briggs }
    492  1.34    briggs 
    493   1.1    briggs /*
    494  1.21    briggs  * Device timeout/watchdog routine.  Entered if the device neglects to generate
    495  1.21    briggs  * an interrupt after a transmit has been started on it.
    496   1.1    briggs  */
    497  1.25    briggs static int aeintr_ctr = 0;
    498  1.43    briggs 
    499  1.20    briggs void
    500  1.45   thorpej aewatchdog(ifp)
    501  1.45   thorpej 	struct ifnet *ifp;
    502   1.1    briggs {
    503  1.45   thorpej 	struct ae_softc *sc = ifp->if_softc;
    504  1.15    briggs 
    505  1.18    briggs #if 1
    506  1.18    briggs /*
    507  1.18    briggs  * This is a kludge!  The via code seems to miss slot interrupts
    508  1.18    briggs  * sometimes.  This kludges around that by calling the handler
    509  1.18    briggs  * by hand if the watchdog is activated. -- XXX (akb)
    510  1.18    briggs  */
    511  1.25    briggs 	int     i;
    512  1.18    briggs 
    513  1.18    briggs 	i = aeintr_ctr;
    514  1.18    briggs 
    515  1.43    briggs 	(*via2itab[1]) ((void *) 1);
    516  1.18    briggs 
    517  1.19    briggs 	if (i != aeintr_ctr) {
    518  1.45   thorpej 		log(LOG_ERR, "%s: device timeout, recovered\n",
    519  1.45   thorpej 		    sc->sc_dev.dv_xname);
    520  1.18    briggs 		return;
    521  1.19    briggs 	}
    522  1.18    briggs #endif
    523  1.18    briggs 
    524  1.21    briggs 	log(LOG_ERR, "%s: device timeout\n", sc->sc_dev.dv_xname);
    525  1.21    briggs 	++sc->sc_arpcom.ac_if.if_oerrors;
    526  1.21    briggs 
    527  1.34    briggs 	aereset(sc);
    528   1.1    briggs }
    529  1.34    briggs 
    530   1.1    briggs /*
    531  1.21    briggs  * Initialize device.
    532   1.1    briggs  */
    533  1.21    briggs void
    534  1.34    briggs aeinit(sc)
    535   1.3    briggs 	struct ae_softc *sc;
    536   1.1    briggs {
    537  1.21    briggs 	struct ifnet *ifp = &sc->sc_arpcom.ac_if;
    538  1.34    briggs 	int     i;
    539  1.27    briggs 	u_char  mcaf[8];
    540   1.1    briggs 
    541   1.1    briggs 	/*
    542   1.1    briggs 	 * Initialize the NIC in the exact order outlined in the NS manual.
    543  1.21    briggs 	 * This init procedure is "mandatory"...don't change what or when
    544  1.21    briggs 	 * things happen.
    545   1.1    briggs 	 */
    546   1.1    briggs 
    547  1.21    briggs 	/* Reset transmitter flags. */
    548  1.34    briggs 	ifp->if_timer = 0;
    549   1.1    briggs 
    550  1.21    briggs 	sc->txb_inuse = 0;
    551  1.21    briggs 	sc->txb_new = 0;
    552  1.21    briggs 	sc->txb_next_tx = 0;
    553   1.1    briggs 
    554  1.21    briggs 	/* Set interface for page 0, remote DMA complete, stopped. */
    555  1.22    briggs 	NIC_PUT(sc, ED_P0_CR, sc->cr_proto | ED_CR_PAGE_0 | ED_CR_STP);
    556   1.1    briggs 
    557   1.1    briggs 	/*
    558  1.21    briggs 	 * Set FIFO threshold to 8, No auto-init Remote DMA, byte
    559  1.21    briggs 	 * order=80x86, word-wide DMA xfers,
    560   1.1    briggs 	 */
    561  1.22    briggs 	NIC_PUT(sc, ED_P0_DCR,
    562  1.22    briggs 	    ED_DCR_FT1 | ED_DCR_WTS | ED_DCR_LS);
    563   1.1    briggs 
    564  1.21    briggs 	/* Clear remote byte count registers. */
    565  1.22    briggs 	NIC_PUT(sc, ED_P0_RBCR0, 0);
    566  1.22    briggs 	NIC_PUT(sc, ED_P0_RBCR1, 0);
    567   1.1    briggs 
    568  1.21    briggs 	/* Tell RCR to do nothing for now. */
    569  1.22    briggs 	NIC_PUT(sc, ED_P0_RCR, ED_RCR_MON);
    570   1.1    briggs 
    571  1.21    briggs 	/* Place NIC in internal loopback mode. */
    572  1.22    briggs 	NIC_PUT(sc, ED_P0_TCR, ED_TCR_LB0);
    573   1.1    briggs 
    574  1.21    briggs 	/* Initialize receive buffer ring. */
    575  1.23    briggs 	NIC_PUT(sc, ED_P0_TPSR, sc->rec_page_start);
    576  1.22    briggs 	NIC_PUT(sc, ED_P0_PSTART, sc->rec_page_start);
    577  1.27    briggs 
    578  1.22    briggs 	NIC_PUT(sc, ED_P0_PSTOP, sc->rec_page_stop);
    579  1.27    briggs 	NIC_PUT(sc, ED_P0_BNRY, sc->rec_page_start);
    580   1.1    briggs 
    581   1.1    briggs 	/*
    582  1.21    briggs 	 * Clear all interrupts.  A '1' in each bit position clears the
    583  1.21    briggs 	 * corresponding flag.
    584   1.1    briggs 	 */
    585  1.22    briggs 	NIC_PUT(sc, ED_P0_ISR, 0xff);
    586  1.15    briggs 
    587   1.1    briggs 	/*
    588   1.1    briggs 	 * Enable the following interrupts: receive/transmit complete,
    589  1.21    briggs 	 * receive/transmit error, and Receiver OverWrite.
    590   1.1    briggs 	 *
    591   1.1    briggs 	 * Counter overflow and Remote DMA complete are *not* enabled.
    592   1.1    briggs 	 */
    593  1.22    briggs 	NIC_PUT(sc, ED_P0_IMR,
    594  1.22    briggs 	    ED_IMR_PRXE | ED_IMR_PTXE | ED_IMR_RXEE | ED_IMR_TXEE |
    595  1.22    briggs 	    ED_IMR_OVWE);
    596   1.1    briggs 
    597  1.21    briggs 	/* Program command register for page 1. */
    598  1.22    briggs 	NIC_PUT(sc, ED_P0_CR, sc->cr_proto | ED_CR_PAGE_1 | ED_CR_STP);
    599   1.1    briggs 
    600  1.21    briggs 	/* Copy out our station address. */
    601   1.1    briggs 	for (i = 0; i < ETHER_ADDR_LEN; ++i)
    602  1.22    briggs 		NIC_PUT(sc, ED_P1_PAR0 + i, sc->sc_arpcom.ac_enaddr[i]);
    603   1.1    briggs 
    604  1.21    briggs 	/* Set multicast filter on chip. */
    605  1.21    briggs 	ae_getmcaf(&sc->sc_arpcom, mcaf);
    606  1.21    briggs 	for (i = 0; i < 8; i++)
    607  1.27    briggs 		NIC_PUT(sc, ED_P1_MAR0 + i, mcaf[i]);
    608   1.1    briggs 
    609   1.1    briggs 	/*
    610  1.21    briggs 	 * Set current page pointer to one page after the boundary pointer, as
    611  1.21    briggs 	 * recommended in the National manual.
    612   1.1    briggs 	 */
    613  1.21    briggs 	sc->next_packet = sc->rec_page_start + 1;
    614  1.22    briggs 	NIC_PUT(sc, ED_P1_CURR, sc->next_packet);
    615   1.1    briggs 
    616  1.21    briggs 	/* Program command register for page 0. */
    617  1.22    briggs 	NIC_PUT(sc, ED_P1_CR, sc->cr_proto | ED_CR_PAGE_0 | ED_CR_STP);
    618  1.21    briggs 
    619  1.22    briggs 	i = ED_RCR_AB | ED_RCR_AM;
    620  1.21    briggs 	if (ifp->if_flags & IFF_PROMISC) {
    621  1.21    briggs 		/*
    622  1.21    briggs 		 * Set promiscuous mode.  Multicast filter was set earlier so
    623  1.21    briggs 		 * that we should receive all multicast packets.
    624  1.21    briggs 		 */
    625  1.22    briggs 		i |= ED_RCR_PRO | ED_RCR_AR | ED_RCR_SEP;
    626  1.21    briggs 	}
    627  1.22    briggs 	NIC_PUT(sc, ED_P0_RCR, i);
    628  1.21    briggs 
    629  1.21    briggs 	/* Take interface out of loopback. */
    630  1.22    briggs 	NIC_PUT(sc, ED_P0_TCR, 0);
    631   1.1    briggs 
    632  1.21    briggs 	/* Fire up the interface. */
    633  1.22    briggs 	NIC_PUT(sc, ED_P0_CR, sc->cr_proto | ED_CR_PAGE_0 | ED_CR_STA);
    634   1.1    briggs 
    635  1.21    briggs 	/* Set 'running' flag, and clear output active flag. */
    636   1.1    briggs 	ifp->if_flags |= IFF_RUNNING;
    637   1.1    briggs 	ifp->if_flags &= ~IFF_OACTIVE;
    638   1.1    briggs 
    639  1.21    briggs 	/* ...and attempt to start output. */
    640  1.34    briggs 	aestart(ifp);
    641  1.34    briggs }
    642   1.1    briggs 
    643   1.1    briggs /*
    644  1.21    briggs  * This routine actually starts the transmission on the interface.
    645   1.1    briggs  */
    646  1.21    briggs static inline void
    647  1.21    briggs ae_xmit(sc)
    648  1.21    briggs 	struct ae_softc *sc;
    649   1.1    briggs {
    650  1.21    briggs 	struct ifnet *ifp = &sc->sc_arpcom.ac_if;
    651  1.21    briggs 	u_short len;
    652  1.21    briggs 
    653  1.21    briggs 	len = sc->txb_len[sc->txb_next_tx];
    654   1.1    briggs 
    655  1.21    briggs 	/* Set NIC for page 0 register access. */
    656  1.22    briggs 	NIC_PUT(sc, ED_P0_CR, sc->cr_proto | ED_CR_PAGE_0 | ED_CR_STA);
    657   1.1    briggs 
    658  1.21    briggs 	/* Set TX buffer start page. */
    659  1.22    briggs 	NIC_PUT(sc, ED_P0_TPSR, sc->tx_page_start +
    660  1.22    briggs 	    sc->txb_next_tx * ED_TXBUF_SIZE);
    661   1.1    briggs 
    662  1.21    briggs 	/* Set TX length. */
    663  1.22    briggs 	NIC_PUT(sc, ED_P0_TBCR0, len);
    664  1.22    briggs 	NIC_PUT(sc, ED_P0_TBCR1, len >> 8);
    665   1.1    briggs 
    666  1.21    briggs 	/* Set page 0, remote DMA complete, transmit packet, and *start*. */
    667  1.22    briggs 	NIC_PUT(sc, ED_P0_CR, sc->cr_proto | ED_CR_PAGE_0 | ED_CR_TXP | ED_CR_STA);
    668   1.1    briggs 
    669  1.21    briggs 	/* Point to next transmit buffer slot and wrap if necessary. */
    670  1.21    briggs 	sc->txb_next_tx++;
    671  1.21    briggs 	if (sc->txb_next_tx == sc->txb_cnt)
    672  1.21    briggs 		sc->txb_next_tx = 0;
    673   1.1    briggs 
    674  1.21    briggs 	/* Set a timer just in case we never hear from the board again. */
    675  1.18    briggs 	ifp->if_timer = 2;
    676   1.1    briggs }
    677  1.34    briggs 
    678   1.1    briggs /*
    679   1.1    briggs  * Start output on interface.
    680   1.1    briggs  * We make two assumptions here:
    681  1.37   mycroft  *  1) that the current priority is set to splnet _before_ this code
    682   1.1    briggs  *     is called *and* is returned to the appropriate priority after
    683   1.1    briggs  *     return
    684   1.1    briggs  *  2) that the IFF_OACTIVE flag is checked before this code is called
    685   1.1    briggs  *     (i.e. that the output part of the interface is idle)
    686   1.1    briggs  */
    687  1.20    briggs void
    688  1.34    briggs aestart(ifp)
    689   1.1    briggs 	struct ifnet *ifp;
    690   1.1    briggs {
    691  1.45   thorpej 	struct ae_softc *sc = ifp->if_softc;
    692  1.34    briggs 	struct mbuf *m0;
    693   1.1    briggs 	caddr_t buffer;
    694  1.25    briggs 	int     len;
    695   1.1    briggs 
    696  1.34    briggs 	if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
    697  1.34    briggs 		return;
    698  1.34    briggs 
    699   1.1    briggs outloop:
    700  1.21    briggs 	/* See if there is room to put another packet in the buffer. */
    701  1.21    briggs 	if (sc->txb_inuse == sc->txb_cnt) {
    702  1.21    briggs 		/* No room.  Indicate this to the outside world and exit. */
    703  1.21    briggs 		ifp->if_flags |= IFF_OACTIVE;
    704  1.21    briggs 		return;
    705  1.21    briggs 	}
    706  1.34    briggs 	IF_DEQUEUE(&ifp->if_snd, m0);
    707  1.34    briggs 	if (m0 == 0)
    708   1.1    briggs 		return;
    709  1.34    briggs 
    710  1.34    briggs 	/* We need to use m->m_pkthdr.len, so require the header */
    711  1.34    briggs 	if ((m0->m_flags & M_PKTHDR) == 0)
    712  1.34    briggs 		panic("aestart: no header mbuf");
    713  1.34    briggs 
    714  1.34    briggs #if NBPFILTER > 0
    715  1.34    briggs 	/* Tap off here if there is a BPF listener. */
    716  1.34    briggs 	if (ifp->if_bpf)
    717  1.34    briggs 		bpf_mtap(ifp->if_bpf, m0);
    718  1.34    briggs #endif
    719  1.21    briggs 
    720  1.21    briggs 	/* txb_new points to next open buffer slot. */
    721  1.22    briggs 	buffer = sc->mem_start + ((sc->txb_new * ED_TXBUF_SIZE) << ED_PAGE_SHIFT);
    722  1.21    briggs 
    723  1.34    briggs 	len = ae_put(sc, m0, buffer);
    724  1.34    briggs #if DIAGNOSTIC
    725  1.34    briggs 	if (len != m0->m_pkthdr.len)
    726  1.48  christos 		printf("aestart: len %d != m0->m_pkthdr.len %d.\n",
    727  1.34    briggs 			len, m0->m_pkthdr.len);
    728  1.34    briggs #endif
    729  1.34    briggs 	len = m0->m_pkthdr.len;
    730   1.1    briggs 
    731  1.34    briggs 	m_freem(m0);
    732  1.21    briggs 	sc->txb_len[sc->txb_new] = max(len, ETHER_MIN_LEN);
    733  1.34    briggs 
    734  1.34    briggs 	/* Start the first packet transmitting. */
    735  1.34    briggs 	if (sc->txb_inuse == 0)
    736  1.34    briggs 		ae_xmit(sc);
    737   1.1    briggs 
    738  1.21    briggs 	/* Point to next buffer slot and wrap if necessary. */
    739  1.21    briggs 	if (++sc->txb_new == sc->txb_cnt)
    740  1.21    briggs 		sc->txb_new = 0;
    741   1.1    briggs 
    742  1.34    briggs 	sc->txb_inuse++;
    743   1.1    briggs 
    744  1.21    briggs 	/* Loop back to the top to possibly buffer more packets. */
    745  1.21    briggs 	goto outloop;
    746   1.1    briggs }
    747  1.34    briggs 
    748   1.1    briggs /*
    749   1.1    briggs  * Ethernet interface receiver interrupt.
    750   1.1    briggs  */
    751   1.1    briggs static inline void
    752  1.21    briggs ae_rint(sc)
    753  1.21    briggs 	struct ae_softc *sc;
    754   1.1    briggs {
    755  1.25    briggs 	u_char  boundary, current;
    756  1.22    briggs 	u_short len;
    757  1.34    briggs 	u_char  nlen, *lenp;
    758  1.24    briggs 	struct ae_ring packet_hdr;
    759  1.34    briggs 	caddr_t packet_ptr;
    760  1.21    briggs 
    761  1.21    briggs loop:
    762  1.21    briggs 	/* Set NIC to page 1 registers to get 'current' pointer. */
    763  1.22    briggs 	NIC_PUT(sc, ED_P0_CR, sc->cr_proto | ED_CR_PAGE_1 | ED_CR_STA);
    764   1.1    briggs 
    765   1.1    briggs 	/*
    766   1.1    briggs 	 * 'sc->next_packet' is the logical beginning of the ring-buffer - i.e.
    767  1.21    briggs 	 * it points to where new data has been buffered.  The 'CURR' (current)
    768  1.21    briggs 	 * register points to the logical end of the ring-buffer - i.e. it
    769  1.21    briggs 	 * points to where additional new data will be added.  We loop here
    770  1.21    briggs 	 * until the logical beginning equals the logical end (or in other
    771  1.21    briggs 	 * words, until the ring-buffer is empty).
    772   1.1    briggs 	 */
    773  1.22    briggs 	current = NIC_GET(sc, ED_P1_CURR);
    774  1.21    briggs 	if (sc->next_packet == current)
    775  1.21    briggs 		return;
    776   1.1    briggs 
    777  1.21    briggs 	/* Set NIC to page 0 registers to update boundary register. */
    778  1.22    briggs 	NIC_PUT(sc, ED_P1_CR, sc->cr_proto | ED_CR_PAGE_0 | ED_CR_STA);
    779   1.1    briggs 
    780  1.21    briggs 	do {
    781  1.21    briggs 		/* Get pointer to this buffer's header structure. */
    782  1.21    briggs 		packet_ptr = sc->mem_ring +
    783  1.22    briggs 		    ((sc->next_packet - sc->rec_page_start) << ED_PAGE_SHIFT);
    784   1.1    briggs 
    785   1.1    briggs 		/*
    786  1.21    briggs 		 * The byte count includes a 4 byte header that was added by
    787  1.21    briggs 		 * the NIC.
    788   1.1    briggs 		 */
    789  1.25    briggs 		packet_hdr = *(struct ae_ring *) packet_ptr;
    790  1.34    briggs 		lenp = (u_char *) &((struct ae_ring *) packet_ptr)->count;
    791  1.34    briggs 		len = lenp[0] | (lenp[1] << 8);
    792  1.34    briggs 		packet_hdr.count = len;
    793   1.1    briggs 
    794   1.1    briggs 		/*
    795  1.21    briggs 		 * Try do deal with old, buggy chips that sometimes duplicate
    796  1.21    briggs 		 * the low byte of the length into the high byte.  We do this
    797  1.21    briggs 		 * by simply ignoring the high byte of the length and always
    798  1.21    briggs 		 * recalculating it.
    799  1.21    briggs 		 *
    800  1.21    briggs 		 * NOTE: sc->next_packet is pointing at the current packet.
    801   1.1    briggs 		 */
    802  1.21    briggs 		if (packet_hdr.next_packet >= sc->next_packet)
    803  1.21    briggs 			nlen = (packet_hdr.next_packet - sc->next_packet);
    804  1.21    briggs 		else
    805  1.21    briggs 			nlen = ((packet_hdr.next_packet - sc->rec_page_start) +
    806  1.25    briggs 			    (sc->rec_page_stop - sc->next_packet));
    807  1.21    briggs 		--nlen;
    808  1.22    briggs 		if ((len & ED_PAGE_MASK) + sizeof(packet_hdr) > ED_PAGE_SIZE)
    809  1.21    briggs 			--nlen;
    810  1.22    briggs 		len = (len & ED_PAGE_MASK) | (nlen << ED_PAGE_SHIFT);
    811  1.21    briggs #ifdef DIAGNOSTIC
    812  1.22    briggs 		if (len != packet_hdr.count) {
    813  1.48  christos 			printf("%s: length does not match next packet pointer\n",
    814  1.21    briggs 			    sc->sc_dev.dv_xname);
    815  1.48  christos 			printf("%s: len %04x nlen %04x start %02x first %02x curr %02x next %02x stop %02x\n",
    816  1.21    briggs 			    sc->sc_dev.dv_xname, packet_hdr.count, len,
    817  1.21    briggs 			    sc->rec_page_start, sc->next_packet, current,
    818  1.21    briggs 			    packet_hdr.next_packet, sc->rec_page_stop);
    819  1.21    briggs 		}
    820  1.21    briggs #endif
    821   1.1    briggs 
    822   1.1    briggs 		/*
    823  1.21    briggs 		 * Be fairly liberal about what we allow as a "reasonable"
    824  1.21    briggs 		 * length so that a [crufty] packet will make it to BPF (and
    825  1.21    briggs 		 * can thus be analyzed).  Note that all that is really
    826  1.21    briggs 		 * important is that we have a length that will fit into one
    827  1.21    briggs 		 * mbuf cluster or less; the upper layer protocols can then
    828  1.21    briggs 		 * figure out the length from their own length field(s).
    829   1.1    briggs 		 */
    830  1.21    briggs 		if (len <= MCLBYTES &&
    831  1.21    briggs 		    packet_hdr.next_packet >= sc->rec_page_start &&
    832  1.21    briggs 		    packet_hdr.next_packet < sc->rec_page_stop) {
    833  1.21    briggs 			/* Go get packet. */
    834  1.34    briggs 			aeread(sc, packet_ptr + sizeof(struct ae_ring),
    835  1.24    briggs 			    len - sizeof(struct ae_ring));
    836  1.21    briggs 			++sc->sc_arpcom.ac_if.if_ipackets;
    837  1.21    briggs 		} else {
    838  1.21    briggs 			/* Really BAD.  The ring pointers are corrupted. */
    839  1.21    briggs 			log(LOG_ERR,
    840  1.21    briggs 			    "%s: NIC memory corrupt - invalid packet length %d\n",
    841  1.21    briggs 			    sc->sc_dev.dv_xname, len);
    842  1.21    briggs 			++sc->sc_arpcom.ac_if.if_ierrors;
    843  1.34    briggs 			aereset(sc);
    844  1.21    briggs 			return;
    845  1.21    briggs 		}
    846   1.1    briggs 
    847  1.21    briggs 		/* Update next packet pointer. */
    848  1.21    briggs 		sc->next_packet = packet_hdr.next_packet;
    849   1.1    briggs 
    850   1.1    briggs 		/*
    851  1.21    briggs 		 * Update NIC boundary pointer - being careful to keep it one
    852  1.21    briggs 		 * buffer behind (as recommended by NS databook).
    853   1.1    briggs 		 */
    854  1.21    briggs 		boundary = sc->next_packet - 1;
    855  1.21    briggs 		if (boundary < sc->rec_page_start)
    856  1.21    briggs 			boundary = sc->rec_page_stop - 1;
    857  1.22    briggs 		NIC_PUT(sc, ED_P0_BNRY, boundary);
    858  1.21    briggs 	} while (sc->next_packet != current);
    859  1.21    briggs 
    860  1.21    briggs 	goto loop;
    861   1.1    briggs }
    862  1.34    briggs 
    863  1.21    briggs /* Ethernet interface interrupt processor. */
    864  1.22    briggs void
    865  1.43    briggs aeintr(arg, slot)
    866  1.43    briggs 	void	*arg;
    867  1.43    briggs 	int	slot;
    868   1.1    briggs {
    869  1.34    briggs 	struct ae_softc *sc = arg;
    870  1.34    briggs 	struct ifnet *ifp = &sc->sc_arpcom.ac_if;
    871  1.25    briggs 	u_char  isr;
    872   1.1    briggs 
    873  1.18    briggs 	aeintr_ctr++;
    874   1.1    briggs 
    875  1.21    briggs 	/* Set NIC to page 0 registers. */
    876  1.22    briggs 	NIC_PUT(sc, ED_P0_CR, sc->cr_proto | ED_CR_PAGE_0 | ED_CR_STA);
    877  1.21    briggs 
    878  1.22    briggs 	isr = NIC_GET(sc, ED_P0_ISR);
    879  1.21    briggs 	if (!isr)
    880  1.22    briggs 		return;
    881   1.1    briggs 
    882  1.21    briggs 	/* Loop until there are no more new interrupts. */
    883  1.21    briggs 	for (;;) {
    884   1.1    briggs 		/*
    885  1.21    briggs 		 * Reset all the bits that we are 'acknowledging' by writing a
    886  1.21    briggs 		 * '1' to each bit position that was set.
    887  1.21    briggs 		 * (Writing a '1' *clears* the bit.)
    888   1.1    briggs 		 */
    889  1.22    briggs 		NIC_PUT(sc, ED_P0_ISR, isr);
    890   1.1    briggs 
    891   1.1    briggs 		/*
    892  1.21    briggs 		 * Handle transmitter interrupts.  Handle these first because
    893  1.21    briggs 		 * the receiver will reset the board under some conditions.
    894   1.1    briggs 		 */
    895  1.22    briggs 		if (isr & (ED_ISR_PTX | ED_ISR_TXE)) {
    896  1.25    briggs 			u_char  collisions = NIC_GET(sc, ED_P0_NCR) & 0x0f;
    897   1.1    briggs 
    898   1.1    briggs 			/*
    899  1.21    briggs 			 * Check for transmit error.  If a TX completed with an
    900  1.21    briggs 			 * error, we end up throwing the packet away.  Really
    901   1.1    briggs 			 * the only error that is possible is excessive
    902   1.1    briggs 			 * collisions, and in this case it is best to allow the
    903  1.21    briggs 			 * automatic mechanisms of TCP to backoff the flow.  Of
    904   1.1    briggs 			 * course, with UDP we're screwed, but this is expected
    905   1.1    briggs 			 * when a network is heavily loaded.
    906   1.1    briggs 			 */
    907  1.22    briggs 			(void) NIC_GET(sc, ED_P0_TSR);
    908  1.22    briggs 			if (isr & ED_ISR_TXE) {
    909   1.1    briggs 				/*
    910  1.21    briggs 				 * Excessive collisions (16).
    911   1.1    briggs 				 */
    912  1.22    briggs 				if ((NIC_GET(sc, ED_P0_TSR) & ED_TSR_ABT)
    913  1.21    briggs 				    && (collisions == 0)) {
    914   1.1    briggs 					/*
    915  1.21    briggs 					 * When collisions total 16, the P0_NCR
    916  1.21    briggs 					 * will indicate 0, and the TSR_ABT is
    917  1.21    briggs 					 * set.
    918   1.1    briggs 					 */
    919   1.1    briggs 					collisions = 16;
    920   1.1    briggs 				}
    921  1.21    briggs 				/* Update output errors counter. */
    922  1.34    briggs 				++ifp->if_oerrors;
    923   1.1    briggs 			} else {
    924   1.1    briggs 				/*
    925   1.1    briggs 				 * Update total number of successfully
    926  1.21    briggs 				 * transmitted packets.
    927   1.1    briggs 				 */
    928  1.35    briggs 				++ifp->if_opackets;
    929   1.1    briggs 			}
    930   1.1    briggs 
    931  1.34    briggs 			/* Done with the buffer. */
    932  1.34    briggs 			sc->txb_inuse--;
    933   1.1    briggs 
    934  1.21    briggs 			/* Clear watchdog timer. */
    935  1.34    briggs 			ifp->if_timer = 0;
    936  1.34    briggs 			ifp->if_flags &= ~IFF_OACTIVE;
    937   1.1    briggs 
    938   1.1    briggs 			/*
    939   1.1    briggs 			 * Add in total number of collisions on last
    940  1.21    briggs 			 * transmission.
    941   1.1    briggs 			 */
    942  1.34    briggs 			ifp->if_collisions += collisions;
    943   1.1    briggs 
    944   1.1    briggs 			/*
    945  1.21    briggs 			 * Decrement buffer in-use count if not zero (can only
    946  1.21    briggs 			 * be zero if a transmitter interrupt occured while not
    947  1.21    briggs 			 * actually transmitting).
    948   1.1    briggs 			 * If data is ready to transmit, start it transmitting,
    949  1.21    briggs 			 * otherwise defer until after handling receiver.
    950   1.1    briggs 			 */
    951  1.34    briggs 			if (sc->txb_inuse > 0)
    952  1.21    briggs 				ae_xmit(sc);
    953   1.1    briggs 		}
    954  1.21    briggs 		/* Handle receiver interrupts. */
    955  1.22    briggs 		if (isr & (ED_ISR_PRX | ED_ISR_RXE | ED_ISR_OVW)) {
    956  1.21    briggs 			/*
    957  1.21    briggs 			 * Overwrite warning.  In order to make sure that a
    958  1.21    briggs 			 * lockup of the local DMA hasn't occurred, we reset
    959  1.21    briggs 			 * and re-init the NIC.  The NSC manual suggests only a
    960  1.21    briggs 			 * partial reset/re-init is necessary - but some chips
    961  1.21    briggs 			 * seem to want more.  The DMA lockup has been seen
    962  1.21    briggs 			 * only with early rev chips - Methinks this bug was
    963  1.21    briggs 			 * fixed in later revs.  -DG
    964  1.21    briggs 			 */
    965  1.22    briggs 			if (isr & ED_ISR_OVW) {
    966  1.34    briggs 				++ifp->if_ierrors;
    967  1.21    briggs #ifdef DIAGNOSTIC
    968   1.1    briggs 				log(LOG_WARNING,
    969  1.21    briggs 				    "%s: warning - receiver ring buffer overrun\n",
    970  1.21    briggs 				    sc->sc_dev.dv_xname);
    971  1.21    briggs #endif
    972  1.21    briggs 				/* Stop/reset/re-init NIC. */
    973  1.34    briggs 				aereset(sc);
    974  1.21    briggs 			} else {
    975   1.1    briggs 				/*
    976  1.21    briggs 				 * Receiver Error.  One or more of: CRC error,
    977  1.21    briggs 				 * frame alignment error FIFO overrun, or
    978  1.21    briggs 				 * missed packet.
    979   1.1    briggs 				 */
    980  1.22    briggs 				if (isr & ED_ISR_RXE) {
    981  1.34    briggs 					++ifp->if_ierrors;
    982   1.1    briggs #ifdef AE_DEBUG
    983  1.48  christos 					printf("%s: receive error %x\n",
    984  1.21    briggs 					    sc->sc_dev.dv_xname,
    985  1.22    briggs 					    NIC_GET(sc, ED_P0_RSR));
    986   1.1    briggs #endif
    987   1.1    briggs 				}
    988   1.1    briggs 				/*
    989   1.1    briggs 				 * Go get the packet(s)
    990   1.1    briggs 				 * XXX - Doing this on an error is dubious
    991  1.21    briggs 				 * because there shouldn't be any data to get
    992  1.21    briggs 				 * (we've configured the interface to not
    993  1.21    briggs 				 * accept packets with errors).
    994   1.1    briggs 				 */
    995  1.21    briggs 				ae_rint(sc);
    996   1.1    briggs 			}
    997   1.1    briggs 		}
    998   1.1    briggs 		/*
    999  1.21    briggs 		 * If it looks like the transmitter can take more data, attempt
   1000  1.21    briggs 		 * to start output on the interface.  This is done after
   1001  1.21    briggs 		 * handling the receiver to give the receiver priority.
   1002   1.1    briggs 		 */
   1003  1.34    briggs 		aestart(ifp);
   1004   1.1    briggs 
   1005   1.1    briggs 		/*
   1006  1.21    briggs 		 * Return NIC CR to standard state: page 0, remote DMA
   1007  1.21    briggs 		 * complete, start (toggling the TXP bit off, even if was just
   1008  1.21    briggs 		 * set in the transmit routine, is *okay* - it is 'edge'
   1009  1.21    briggs 		 * triggered from low to high).
   1010   1.1    briggs 		 */
   1011  1.22    briggs 		NIC_PUT(sc, ED_P0_CR, sc->cr_proto | ED_CR_PAGE_0 | ED_CR_STA);
   1012   1.1    briggs 
   1013   1.1    briggs 		/*
   1014  1.21    briggs 		 * If the Network Talley Counters overflow, read them to reset
   1015  1.21    briggs 		 * them.  It appears that old 8390's won't clear the ISR flag
   1016  1.21    briggs 		 * otherwise - resulting in an infinite loop.
   1017   1.1    briggs 		 */
   1018  1.22    briggs 		if (isr & ED_ISR_CNT) {
   1019  1.40    briggs 			static u_char	dummy;
   1020  1.40    briggs 			dummy = NIC_GET(sc, ED_P0_CNTR0);
   1021  1.40    briggs 			dummy = NIC_GET(sc, ED_P0_CNTR1);
   1022  1.40    briggs 			dummy = NIC_GET(sc, ED_P0_CNTR2);
   1023   1.1    briggs 		}
   1024  1.22    briggs 		isr = NIC_GET(sc, ED_P0_ISR);
   1025  1.21    briggs 		if (!isr)
   1026  1.22    briggs 			return;
   1027   1.1    briggs 	}
   1028   1.1    briggs }
   1029  1.34    briggs 
   1030   1.1    briggs /*
   1031  1.21    briggs  * Process an ioctl request.  This code needs some work - it looks pretty ugly.
   1032   1.1    briggs  */
   1033   1.1    briggs int
   1034  1.34    briggs aeioctl(ifp, cmd, data)
   1035   1.1    briggs 	register struct ifnet *ifp;
   1036  1.34    briggs 	u_long  cmd;
   1037   1.1    briggs 	caddr_t data;
   1038   1.1    briggs {
   1039  1.45   thorpej 	struct ae_softc *sc = ifp->if_softc;
   1040  1.25    briggs 	register struct ifaddr *ifa = (struct ifaddr *) data;
   1041  1.25    briggs 	struct ifreq *ifr = (struct ifreq *) data;
   1042  1.25    briggs 	int     s, error = 0;
   1043   1.1    briggs 
   1044  1.37   mycroft 	s = splnet();
   1045   1.1    briggs 
   1046  1.34    briggs 	switch (cmd) {
   1047   1.1    briggs 
   1048   1.1    briggs 	case SIOCSIFADDR:
   1049   1.1    briggs 		ifp->if_flags |= IFF_UP;
   1050   1.1    briggs 
   1051   1.1    briggs 		switch (ifa->ifa_addr->sa_family) {
   1052   1.1    briggs #ifdef INET
   1053   1.1    briggs 		case AF_INET:
   1054  1.34    briggs 			aeinit(sc);
   1055  1.21    briggs 			arp_ifinit(&sc->sc_arpcom, ifa);
   1056   1.1    briggs 			break;
   1057   1.1    briggs #endif
   1058   1.1    briggs #ifdef NS
   1059  1.25    briggs 			/* XXX - This code is probably wrong. */
   1060   1.1    briggs 		case AF_NS:
   1061  1.25    briggs 			{
   1062  1.25    briggs 				register struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
   1063   1.1    briggs 
   1064  1.25    briggs 				if (ns_nullhost(*ina))
   1065  1.25    briggs 					ina->x_host =
   1066  1.25    briggs 					    *(union ns_host *) (sc->sc_arpcom.ac_enaddr);
   1067  1.25    briggs 				else
   1068  1.25    briggs 					bcopy(ina->x_host.c_host,
   1069  1.25    briggs 					    sc->sc_arpcom.ac_enaddr,
   1070  1.25    briggs 					    sizeof(sc->sc_arpcom.ac_enaddr));
   1071  1.25    briggs 				/* Set new address. */
   1072  1.34    briggs 				aeinit(sc);
   1073  1.25    briggs 				break;
   1074  1.25    briggs 			}
   1075   1.1    briggs #endif
   1076   1.1    briggs 		default:
   1077  1.34    briggs 			aeinit(sc);
   1078   1.1    briggs 			break;
   1079   1.1    briggs 		}
   1080   1.1    briggs 		break;
   1081   1.1    briggs 
   1082   1.1    briggs 	case SIOCSIFFLAGS:
   1083  1.21    briggs 		if ((ifp->if_flags & IFF_UP) == 0 &&
   1084  1.21    briggs 		    (ifp->if_flags & IFF_RUNNING) != 0) {
   1085  1.21    briggs 			/*
   1086  1.21    briggs 			 * If interface is marked down and it is running, then
   1087  1.21    briggs 			 * stop it.
   1088  1.21    briggs 			 */
   1089  1.34    briggs 			aestop(sc);
   1090   1.1    briggs 			ifp->if_flags &= ~IFF_RUNNING;
   1091  1.25    briggs 		} else
   1092  1.25    briggs 			if ((ifp->if_flags & IFF_UP) != 0 &&
   1093  1.25    briggs 			    (ifp->if_flags & IFF_RUNNING) == 0) {
   1094  1.25    briggs 				/*
   1095  1.25    briggs 				 * If interface is marked up and it is stopped, then
   1096  1.25    briggs 				 * start it.
   1097  1.25    briggs 				 */
   1098  1.34    briggs 				aeinit(sc);
   1099  1.25    briggs 			} else {
   1100  1.25    briggs 				/*
   1101  1.25    briggs 				 * Reset the interface to pick up changes in any other
   1102  1.25    briggs 				 * flags that affect hardware registers.
   1103  1.25    briggs 				 */
   1104  1.34    briggs 				aestop(sc);
   1105  1.34    briggs 				aeinit(sc);
   1106  1.25    briggs 			}
   1107  1.21    briggs 		break;
   1108  1.21    briggs 
   1109  1.21    briggs 	case SIOCADDMULTI:
   1110  1.21    briggs 	case SIOCDELMULTI:
   1111  1.21    briggs 		/* Update our multicast list. */
   1112  1.34    briggs 		error = (cmd == SIOCADDMULTI) ?
   1113  1.21    briggs 		    ether_addmulti(ifr, &sc->sc_arpcom) :
   1114  1.21    briggs 		    ether_delmulti(ifr, &sc->sc_arpcom);
   1115  1.21    briggs 
   1116  1.21    briggs 		if (error == ENETRESET) {
   1117   1.1    briggs 			/*
   1118  1.21    briggs 			 * Multicast list has changed; set the hardware filter
   1119  1.21    briggs 			 * accordingly.
   1120   1.1    briggs 			 */
   1121  1.34    briggs 			aestop(sc);	/* XXX for ds_setmcaf? */
   1122  1.34    briggs 			aeinit(sc);
   1123  1.21    briggs 			error = 0;
   1124   1.1    briggs 		}
   1125   1.1    briggs 		break;
   1126   1.1    briggs 
   1127   1.1    briggs 	default:
   1128   1.1    briggs 		error = EINVAL;
   1129  1.34    briggs 		break;
   1130   1.1    briggs 	}
   1131  1.21    briggs 
   1132  1.21    briggs 	splx(s);
   1133   1.1    briggs 	return (error);
   1134   1.1    briggs }
   1135  1.34    briggs 
   1136   1.1    briggs /*
   1137   1.1    briggs  * Retreive packet from shared memory and send to the next level up via
   1138  1.21    briggs  * ether_input().  If there is a BPF listener, give a copy to BPF, too.
   1139   1.1    briggs  */
   1140  1.21    briggs void
   1141  1.34    briggs aeread(sc, buf, len)
   1142   1.1    briggs 	struct ae_softc *sc;
   1143  1.21    briggs 	caddr_t buf;
   1144  1.34    briggs 	int len;
   1145   1.1    briggs {
   1146  1.34    briggs 	struct ifnet *ifp = &sc->sc_arpcom.ac_if;
   1147  1.34    briggs 	struct mbuf *m;
   1148   1.1    briggs 	struct ether_header *eh;
   1149   1.1    briggs 
   1150  1.34    briggs 	/* Pull packet off interface. */
   1151  1.34    briggs 	m = aeget(sc, buf, len);
   1152  1.34    briggs 	if (m == 0) {
   1153  1.34    briggs 		ifp->if_ierrors++;
   1154  1.21    briggs 		return;
   1155  1.34    briggs 	}
   1156   1.1    briggs 
   1157  1.34    briggs 	ifp->if_ipackets++;
   1158  1.34    briggs 
   1159  1.34    briggs 	/* We assume that the header fits entirely in one mbuf. */
   1160  1.21    briggs 	eh = mtod(m, struct ether_header *);
   1161  1.21    briggs 
   1162   1.1    briggs #if NBPFILTER > 0
   1163   1.1    briggs 	/*
   1164  1.34    briggs 	 * Check if there's a BPF listener on this interface.
   1165  1.34    briggs 	 * If so, hand off the raw packet to bpf.
   1166   1.1    briggs 	 */
   1167  1.34    briggs 	if (ifp->if_bpf) {
   1168  1.34    briggs 		bpf_mtap(ifp->if_bpf, m);
   1169   1.1    briggs 
   1170   1.1    briggs 		/*
   1171   1.1    briggs 		 * Note that the interface cannot be in promiscuous mode if
   1172   1.1    briggs 		 * there are no BPF listeners.  And if we are in promiscuous
   1173   1.1    briggs 		 * mode, we have to check if this packet is really ours.
   1174   1.1    briggs 		 */
   1175  1.34    briggs 		if ((ifp->if_flags & IFF_PROMISC) &&
   1176  1.25    briggs 		    (eh->ether_dhost[0] & 1) == 0 &&	/* !mcast and !bcast */
   1177  1.21    briggs 		    bcmp(eh->ether_dhost, sc->sc_arpcom.ac_enaddr,
   1178  1.25    briggs 			sizeof(eh->ether_dhost)) != 0) {
   1179  1.21    briggs 			m_freem(m);
   1180   1.1    briggs 			return;
   1181   1.1    briggs 		}
   1182   1.1    briggs 	}
   1183   1.1    briggs #endif
   1184   1.1    briggs 
   1185  1.21    briggs 	/* Fix up data start offset in mbuf to point past ether header. */
   1186  1.21    briggs 	m_adj(m, sizeof(struct ether_header));
   1187  1.34    briggs 	ether_input(ifp, eh, m);
   1188   1.1    briggs }
   1189  1.34    briggs 
   1190   1.1    briggs /*
   1191  1.21    briggs  * Supporting routines.
   1192   1.1    briggs  */
   1193   1.1    briggs /*
   1194  1.21    briggs  * Given a source and destination address, copy 'amount' of a packet from the
   1195  1.21    briggs  * ring buffer into a linear destination buffer.  Takes into account ring-wrap.
   1196   1.1    briggs  */
   1197  1.21    briggs static inline caddr_t
   1198  1.21    briggs ae_ring_copy(sc, src, dst, amount)
   1199   1.1    briggs 	struct ae_softc *sc;
   1200  1.21    briggs 	caddr_t src, dst;
   1201  1.43    briggs 	int amount;
   1202   1.1    briggs {
   1203  1.25    briggs 	u_short tmp_amount;
   1204   1.1    briggs 
   1205  1.21    briggs 	/* Does copy wrap to lower addr in ring buffer? */
   1206  1.21    briggs 	if (src + amount > sc->mem_end) {
   1207  1.21    briggs 		tmp_amount = sc->mem_end - src;
   1208  1.21    briggs 
   1209  1.21    briggs 		/* Copy amount up to end of NIC memory. */
   1210  1.23    briggs 		byte_copy(src, dst, tmp_amount);
   1211  1.21    briggs 
   1212   1.1    briggs 		amount -= tmp_amount;
   1213  1.21    briggs 		src = sc->mem_ring;
   1214   1.1    briggs 		dst += tmp_amount;
   1215   1.1    briggs 	}
   1216  1.23    briggs 	byte_copy(src, dst, amount);
   1217   1.1    briggs 
   1218  1.21    briggs 	return (src + amount);
   1219   1.1    briggs }
   1220  1.34    briggs 
   1221   1.1    briggs /*
   1222  1.21    briggs  * Copy data from receive buffer to end of mbuf chain allocate additional mbufs
   1223  1.21    briggs  * as needed.  Return pointer to last mbuf in chain.
   1224  1.21    briggs  * sc = ae info (softc)
   1225  1.21    briggs  * src = pointer in ae ring buffer
   1226   1.1    briggs  * dst = pointer to last mbuf in mbuf chain to copy to
   1227   1.1    briggs  * amount = amount of data to copy
   1228   1.1    briggs  */
   1229   1.1    briggs struct mbuf *
   1230  1.34    briggs aeget(sc, src, total_len)
   1231   1.1    briggs 	struct ae_softc *sc;
   1232  1.21    briggs 	caddr_t src;
   1233   1.1    briggs 	u_short total_len;
   1234   1.1    briggs {
   1235  1.34    briggs 	struct ifnet *ifp = &sc->sc_arpcom.ac_if;
   1236  1.34    briggs 	struct mbuf *top, **mp, *m;
   1237  1.34    briggs 	int len;
   1238   1.1    briggs 
   1239  1.34    briggs 	MGETHDR(m, M_DONTWAIT, MT_DATA);
   1240  1.34    briggs 	if (m == 0)
   1241  1.34    briggs 		return 0;
   1242  1.34    briggs 	m->m_pkthdr.rcvif = ifp;
   1243  1.34    briggs 	m->m_pkthdr.len = total_len;
   1244  1.34    briggs 	len = MHLEN;
   1245  1.34    briggs 	top = 0;
   1246  1.34    briggs 	mp = &top;
   1247   1.1    briggs 
   1248  1.34    briggs 	while (total_len > 0) {
   1249  1.34    briggs 		if (top) {
   1250   1.1    briggs 			MGET(m, M_DONTWAIT, MT_DATA);
   1251  1.34    briggs 			if (m == 0) {
   1252  1.34    briggs 				m_freem(top);
   1253  1.34    briggs 				return 0;
   1254  1.34    briggs 			}
   1255  1.34    briggs 			len = MLEN;
   1256  1.34    briggs 		}
   1257  1.34    briggs 		if (total_len >= MINCLSIZE) {
   1258  1.34    briggs 			MCLGET(m, M_DONTWAIT);
   1259  1.34    briggs 			if (m->m_flags & M_EXT)
   1260  1.34    briggs 				len = MCLBYTES;
   1261   1.1    briggs 		}
   1262  1.34    briggs 		m->m_len = len = min(total_len, len);
   1263  1.43    briggs 		src = ae_ring_copy(sc, src, mtod(m, caddr_t), (int) len);
   1264  1.34    briggs 		total_len -= len;
   1265  1.34    briggs 		*mp = m;
   1266  1.34    briggs 		mp = &m->m_next;
   1267  1.34    briggs 	}
   1268   1.1    briggs 
   1269  1.34    briggs 	return top;
   1270  1.21    briggs }
   1271  1.21    briggs /*
   1272  1.21    briggs  * Compute the multicast address filter from the list of multicast addresses we
   1273  1.21    briggs  * need to listen to.
   1274  1.21    briggs  */
   1275  1.21    briggs void
   1276  1.21    briggs ae_getmcaf(ac, af)
   1277  1.21    briggs 	struct arpcom *ac;
   1278  1.27    briggs 	u_char *af;
   1279  1.21    briggs {
   1280  1.21    briggs 	struct ifnet *ifp = &ac->ac_if;
   1281  1.21    briggs 	struct ether_multi *enm;
   1282  1.21    briggs 	register u_char *cp, c;
   1283  1.21    briggs 	register u_long crc;
   1284  1.21    briggs 	register int i, len;
   1285  1.21    briggs 	struct ether_multistep step;
   1286  1.21    briggs 
   1287  1.21    briggs 	/*
   1288  1.21    briggs 	 * Set up multicast address filter by passing all multicast addresses
   1289  1.21    briggs 	 * through a crc generator, and then using the high order 6 bits as an
   1290  1.21    briggs 	 * index into the 64 bit logical address filter.  The high order bit
   1291  1.21    briggs 	 * selects the word, while the rest of the bits select the bit within
   1292  1.21    briggs 	 * the word.
   1293  1.21    briggs 	 */
   1294  1.21    briggs 
   1295  1.21    briggs 	if (ifp->if_flags & IFF_PROMISC) {
   1296  1.21    briggs 		ifp->if_flags |= IFF_ALLMULTI;
   1297  1.27    briggs 		for (i = 0; i < 8; i++)
   1298  1.27    briggs 			af[i] = 0xff;
   1299  1.21    briggs 		return;
   1300  1.21    briggs 	}
   1301  1.27    briggs 	for (i = 0; i < 8; i++)
   1302  1.27    briggs 		af[i] = 0;
   1303  1.21    briggs 	ETHER_FIRST_MULTI(step, ac, enm);
   1304  1.21    briggs 	while (enm != NULL) {
   1305  1.21    briggs 		if (bcmp(enm->enm_addrlo, enm->enm_addrhi,
   1306  1.25    briggs 			sizeof(enm->enm_addrlo)) != 0) {
   1307  1.21    briggs 			/*
   1308  1.21    briggs 			 * We must listen to a range of multicast addresses.
   1309  1.21    briggs 			 * For now, just accept all multicasts, rather than
   1310  1.21    briggs 			 * trying to set only those filter bits needed to match
   1311  1.21    briggs 			 * the range.  (At this time, the only use of address
   1312  1.21    briggs 			 * ranges is for IP multicast routing, for which the
   1313  1.21    briggs 			 * range is big enough to require all bits set.)
   1314  1.21    briggs 			 */
   1315  1.21    briggs 			ifp->if_flags |= IFF_ALLMULTI;
   1316  1.27    briggs 			for (i = 0; i < 8; i++)
   1317  1.27    briggs 				af[i] = 0xff;
   1318  1.21    briggs 			return;
   1319  1.21    briggs 		}
   1320  1.21    briggs 		cp = enm->enm_addrlo;
   1321  1.21    briggs 		crc = 0xffffffff;
   1322  1.21    briggs 		for (len = sizeof(enm->enm_addrlo); --len >= 0;) {
   1323  1.21    briggs 			c = *cp++;
   1324  1.21    briggs 			for (i = 8; --i >= 0;) {
   1325  1.21    briggs 				if (((crc & 0x80000000) ? 1 : 0) ^ (c & 0x01)) {
   1326  1.21    briggs 					crc <<= 1;
   1327  1.21    briggs 					crc ^= 0x04c11db6 | 1;
   1328  1.21    briggs 				} else
   1329  1.21    briggs 					crc <<= 1;
   1330  1.21    briggs 				c >>= 1;
   1331  1.21    briggs 			}
   1332  1.21    briggs 		}
   1333  1.21    briggs 		/* Just want the 6 most significant bits. */
   1334  1.21    briggs 		crc >>= 26;
   1335  1.21    briggs 
   1336  1.21    briggs 		/* Turn on the corresponding bit in the filter. */
   1337  1.27    briggs 		af[crc >> 3] |= 1 << (crc & 0x7);
   1338  1.21    briggs 
   1339  1.21    briggs 		ETHER_NEXT_MULTI(step, enm);
   1340   1.1    briggs 	}
   1341  1.21    briggs 	ifp->if_flags &= ~IFF_ALLMULTI;
   1342  1.21    briggs }
   1343  1.21    briggs /*
   1344  1.21    briggs  * Copy packet from mbuf to the board memory
   1345  1.21    briggs  *
   1346  1.21    briggs  * Currently uses an extra buffer/extra memory copy,
   1347  1.21    briggs  * unless the whole packet fits in one mbuf.
   1348  1.21    briggs  *
   1349  1.21    briggs  */
   1350  1.21    briggs u_short
   1351  1.21    briggs ae_put(sc, m, buf)
   1352  1.21    briggs 	struct ae_softc *sc;
   1353  1.21    briggs 	struct mbuf *m;
   1354  1.21    briggs 	caddr_t buf;
   1355  1.21    briggs {
   1356  1.21    briggs 	u_char *data, savebyte[2];
   1357  1.25    briggs 	int     len, wantbyte;
   1358  1.25    briggs 	u_short totlen = 0;
   1359  1.21    briggs 
   1360  1.21    briggs 	wantbyte = 0;
   1361  1.21    briggs 
   1362  1.34    briggs 	for (; m ; m = m->m_next) {
   1363  1.21    briggs 		data = mtod(m, u_char *);
   1364  1.21    briggs 		len = m->m_len;
   1365  1.21    briggs 		totlen += len;
   1366  1.21    briggs 		if (len > 0) {
   1367  1.21    briggs 			/* Finish the last word. */
   1368  1.21    briggs 			if (wantbyte) {
   1369  1.21    briggs 				savebyte[1] = *data;
   1370  1.21    briggs 				word_copy(savebyte, buf, 2);
   1371  1.21    briggs 				buf += 2;
   1372  1.21    briggs 				data++;
   1373  1.21    briggs 				len--;
   1374  1.21    briggs 				wantbyte = 0;
   1375  1.21    briggs 			}
   1376  1.21    briggs 			/* Output contiguous words. */
   1377  1.21    briggs 			if (len > 1) {
   1378  1.21    briggs 				word_copy(data, buf, len);
   1379  1.21    briggs 				buf += len & ~1;
   1380  1.21    briggs 				data += len & ~1;
   1381  1.21    briggs 				len &= 1;
   1382  1.21    briggs 			}
   1383  1.21    briggs 			/* Save last byte, if necessary. */
   1384  1.21    briggs 			if (len == 1) {
   1385  1.21    briggs 				savebyte[0] = *data;
   1386  1.21    briggs 				wantbyte = 1;
   1387  1.21    briggs 			}
   1388  1.21    briggs 		}
   1389  1.21    briggs 	}
   1390  1.21    briggs 
   1391  1.21    briggs 	if (wantbyte) {
   1392  1.21    briggs 		savebyte[1] = 0;
   1393  1.21    briggs 		word_copy(savebyte, buf, 2);
   1394  1.21    briggs 	}
   1395  1.21    briggs 	return (totlen);
   1396   1.1    briggs }
   1397