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