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