Home | History | Annotate | Line # | Download | only in ic
dp8390.c revision 1.54
      1  1.54   thorpej /*	$NetBSD: dp8390.c,v 1.54 2004/10/30 18:08:36 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.49     lukem 
     16  1.49     lukem #include <sys/cdefs.h>
     17  1.54   thorpej __KERNEL_RCSID(0, "$NetBSD: dp8390.c,v 1.54 2004/10/30 18:08:36 thorpej Exp $");
     18   1.1    scottr 
     19  1.35        ws #include "opt_ipkdb.h"
     20  1.12  jonathan #include "opt_inet.h"
     21  1.13  jonathan #include "opt_ns.h"
     22   1.1    scottr #include "bpfilter.h"
     23  1.20   thorpej #include "rnd.h"
     24   1.1    scottr 
     25   1.1    scottr #include <sys/param.h>
     26   1.1    scottr #include <sys/systm.h>
     27   1.1    scottr #include <sys/device.h>
     28   1.1    scottr #include <sys/errno.h>
     29   1.1    scottr #include <sys/ioctl.h>
     30   1.1    scottr #include <sys/mbuf.h>
     31   1.1    scottr #include <sys/socket.h>
     32   1.1    scottr #include <sys/syslog.h>
     33   1.1    scottr 
     34  1.20   thorpej #if NRND > 0
     35  1.20   thorpej #include <sys/rnd.h>
     36  1.20   thorpej #endif
     37  1.20   thorpej 
     38   1.1    scottr #include <net/if.h>
     39   1.1    scottr #include <net/if_dl.h>
     40   1.1    scottr #include <net/if_types.h>
     41   1.8   thorpej #include <net/if_media.h>
     42   1.1    scottr #include <net/if_ether.h>
     43   1.1    scottr 
     44   1.1    scottr #ifdef INET
     45   1.1    scottr #include <netinet/in.h>
     46   1.1    scottr #include <netinet/in_systm.h>
     47   1.1    scottr #include <netinet/in_var.h>
     48   1.1    scottr #include <netinet/ip.h>
     49   1.1    scottr #include <netinet/if_inarp.h>
     50   1.1    scottr #endif
     51   1.1    scottr 
     52   1.1    scottr #ifdef NS
     53   1.1    scottr #include <netns/ns.h>
     54   1.1    scottr #include <netns/ns_if.h>
     55   1.1    scottr #endif
     56   1.1    scottr 
     57   1.1    scottr #if NBPFILTER > 0
     58   1.1    scottr #include <net/bpf.h>
     59   1.1    scottr #include <net/bpfdesc.h>
     60   1.1    scottr #endif
     61   1.1    scottr 
     62   1.1    scottr #include <machine/bus.h>
     63   1.1    scottr 
     64  1.35        ws #ifdef IPKDB_DP8390
     65  1.35        ws #include <ipkdb/ipkdb.h>
     66  1.35        ws #endif
     67  1.35        ws 
     68   1.1    scottr #include <dev/ic/dp8390reg.h>
     69   1.1    scottr #include <dev/ic/dp8390var.h>
     70   1.1    scottr 
     71   1.1    scottr #ifdef DEBUG
     72   1.1    scottr #define __inline__	/* XXX for debugging porpoises */
     73  1.51  kristerw int	dp8390_debug = 0;
     74   1.1    scottr #endif
     75   1.1    scottr 
     76   1.1    scottr static __inline__ void	dp8390_xmit __P((struct dp8390_softc *));
     77   1.1    scottr 
     78   1.1    scottr static __inline__ void	dp8390_read_hdr __P((struct dp8390_softc *,
     79   1.1    scottr 			    int, struct dp8390_ring *));
     80   1.1    scottr static __inline__ int	dp8390_ring_copy __P((struct dp8390_softc *,
     81   1.1    scottr 			    int, caddr_t, u_short));
     82   1.1    scottr static __inline__ int	dp8390_write_mbuf __P((struct dp8390_softc *,
     83   1.1    scottr 			    struct mbuf *, int));
     84   1.1    scottr 
     85   1.1    scottr static int		dp8390_test_mem __P((struct dp8390_softc *));
     86   1.1    scottr 
     87  1.42   thorpej /*
     88  1.42   thorpej  * Standard media init routine for the dp8390.
     89  1.42   thorpej  */
     90  1.42   thorpej void
     91  1.42   thorpej dp8390_media_init(struct dp8390_softc *sc)
     92  1.42   thorpej {
     93  1.42   thorpej 
     94  1.42   thorpej 	ifmedia_init(&sc->sc_media, 0, dp8390_mediachange, dp8390_mediastatus);
     95  1.42   thorpej 	ifmedia_add(&sc->sc_media, IFM_ETHER|IFM_MANUAL, 0, NULL);
     96  1.42   thorpej 	ifmedia_set(&sc->sc_media, IFM_ETHER|IFM_MANUAL);
     97  1.42   thorpej }
     98   1.4    scottr 
     99   1.1    scottr /*
    100   1.1    scottr  * Do bus-independent setup.
    101   1.1    scottr  */
    102   1.1    scottr int
    103  1.42   thorpej dp8390_config(sc)
    104   1.1    scottr 	struct dp8390_softc *sc;
    105   1.1    scottr {
    106   1.1    scottr 	struct ifnet *ifp = &sc->sc_ec.ec_if;
    107  1.42   thorpej 	int rv;
    108   1.1    scottr 
    109   1.1    scottr 	rv = 1;
    110   1.1    scottr 
    111   1.1    scottr 	if (!sc->test_mem)
    112   1.1    scottr 		sc->test_mem = dp8390_test_mem;
    113   1.1    scottr 
    114   1.1    scottr 	/* Allocate one xmit buffer if < 16k, two buffers otherwise. */
    115   1.1    scottr 	if ((sc->mem_size < 16384) ||
    116   1.1    scottr 	    (sc->sc_flags & DP8390_NO_MULTI_BUFFERING))
    117   1.1    scottr 		sc->txb_cnt = 1;
    118  1.27     enami 	else if (sc->mem_size < 8192 * 3)
    119  1.27     enami 		sc->txb_cnt = 2;
    120   1.1    scottr 	else
    121  1.27     enami 		sc->txb_cnt = 3;
    122   1.1    scottr 
    123   1.7   thorpej 	sc->tx_page_start = sc->mem_start >> ED_PAGE_SHIFT;
    124   1.1    scottr 	sc->rec_page_start = sc->tx_page_start + sc->txb_cnt * ED_TXBUF_SIZE;
    125   1.1    scottr 	sc->rec_page_stop = sc->tx_page_start + (sc->mem_size >> ED_PAGE_SHIFT);
    126   1.1    scottr 	sc->mem_ring = sc->mem_start + (sc->rec_page_start << ED_PAGE_SHIFT);
    127   1.1    scottr 	sc->mem_end = sc->mem_start + sc->mem_size;
    128   1.1    scottr 
    129   1.1    scottr 	/* Now zero memory and verify that it is clear. */
    130   1.1    scottr 	if ((*sc->test_mem)(sc))
    131   1.1    scottr 		goto out;
    132   1.1    scottr 
    133   1.1    scottr 	/* Set interface to stopped condition (reset). */
    134   1.1    scottr 	dp8390_stop(sc);
    135   1.1    scottr 
    136   1.1    scottr 	/* Initialize ifnet structure. */
    137  1.46   thorpej 	strcpy(ifp->if_xname, sc->sc_dev.dv_xname);
    138   1.1    scottr 	ifp->if_softc = sc;
    139   1.1    scottr 	ifp->if_start = dp8390_start;
    140   1.1    scottr 	ifp->if_ioctl = dp8390_ioctl;
    141   1.1    scottr 	if (!ifp->if_watchdog)
    142   1.1    scottr 		ifp->if_watchdog = dp8390_watchdog;
    143   1.1    scottr 	ifp->if_flags =
    144   1.1    scottr 	    IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS | IFF_MULTICAST;
    145  1.41   thorpej 	IFQ_SET_READY(&ifp->if_snd);
    146   1.1    scottr 
    147  1.43   thorpej 	/* Print additional info when attached. */
    148  1.43   thorpej 	printf("%s: Ethernet address %s\n", sc->sc_dev.dv_xname,
    149  1.43   thorpej 	    ether_sprintf(sc->sc_enaddr));
    150  1.43   thorpej 
    151   1.8   thorpej 	/* Initialize media goo. */
    152  1.42   thorpej 	(*sc->sc_media_init)(sc);
    153  1.42   thorpej 
    154  1.39    bouyer 	/*
    155  1.39    bouyer 	 * We can support 802.1Q VLAN-sized frames.
    156  1.39    bouyer 	 */
    157  1.39    bouyer 	sc->sc_ec.ec_capabilities |= ETHERCAP_VLAN_MTU;
    158   1.8   thorpej 
    159   1.1    scottr 	/* Attach the interface. */
    160   1.1    scottr 	if_attach(ifp);
    161   1.1    scottr 	ether_ifattach(ifp, sc->sc_enaddr);
    162   1.1    scottr 
    163  1.20   thorpej #if NRND > 0
    164  1.22  explorer 	rnd_attach_source(&sc->rnd_source, sc->sc_dev.dv_xname,
    165  1.29     enami 	    RND_TYPE_NET, 0);
    166  1.20   thorpej #endif
    167   1.1    scottr 
    168  1.37     jhawk 	/* The attach is successful. */
    169  1.37     jhawk 	sc->sc_flags |= DP8390_ATTACHED;
    170  1.37     jhawk 
    171   1.1    scottr 	rv = 0;
    172   1.1    scottr out:
    173   1.1    scottr 	return (rv);
    174   1.1    scottr }
    175   1.1    scottr 
    176   1.1    scottr /*
    177   1.8   thorpej  * Media change callback.
    178   1.8   thorpej  */
    179   1.8   thorpej int
    180   1.8   thorpej dp8390_mediachange(ifp)
    181   1.8   thorpej 	struct ifnet *ifp;
    182   1.8   thorpej {
    183   1.8   thorpej 	struct dp8390_softc *sc = ifp->if_softc;
    184   1.8   thorpej 
    185   1.8   thorpej 	if (sc->sc_mediachange)
    186   1.8   thorpej 		return ((*sc->sc_mediachange)(sc));
    187  1.25       abs 	return (0);
    188   1.8   thorpej }
    189   1.8   thorpej 
    190   1.8   thorpej /*
    191   1.8   thorpej  * Media status callback.
    192   1.8   thorpej  */
    193   1.8   thorpej void
    194   1.8   thorpej dp8390_mediastatus(ifp, ifmr)
    195   1.8   thorpej 	struct ifnet *ifp;
    196   1.8   thorpej 	struct ifmediareq *ifmr;
    197   1.8   thorpej {
    198   1.8   thorpej 	struct dp8390_softc *sc = ifp->if_softc;
    199   1.8   thorpej 
    200   1.8   thorpej 	if (sc->sc_enabled == 0) {
    201   1.8   thorpej 		ifmr->ifm_active = IFM_ETHER | IFM_NONE;
    202   1.8   thorpej 		ifmr->ifm_status = 0;
    203   1.8   thorpej 		return;
    204   1.8   thorpej 	}
    205   1.8   thorpej 
    206   1.8   thorpej 	if (sc->sc_mediastatus)
    207   1.8   thorpej 		(*sc->sc_mediastatus)(sc, ifmr);
    208   1.8   thorpej }
    209   1.8   thorpej 
    210   1.8   thorpej /*
    211   1.1    scottr  * Reset interface.
    212   1.1    scottr  */
    213   1.1    scottr void
    214   1.1    scottr dp8390_reset(sc)
    215   1.1    scottr 	struct dp8390_softc *sc;
    216   1.1    scottr {
    217   1.1    scottr 	int     s;
    218   1.1    scottr 
    219   1.1    scottr 	s = splnet();
    220   1.1    scottr 	dp8390_stop(sc);
    221   1.1    scottr 	dp8390_init(sc);
    222   1.1    scottr 	splx(s);
    223   1.1    scottr }
    224   1.1    scottr 
    225   1.1    scottr /*
    226   1.1    scottr  * Take interface offline.
    227   1.1    scottr  */
    228   1.1    scottr void
    229   1.1    scottr dp8390_stop(sc)
    230   1.1    scottr 	struct dp8390_softc *sc;
    231   1.1    scottr {
    232   1.1    scottr 	bus_space_tag_t regt = sc->sc_regt;
    233   1.1    scottr 	bus_space_handle_t regh = sc->sc_regh;
    234   1.1    scottr 	int n = 5000;
    235   1.1    scottr 
    236   1.1    scottr 	/* Stop everything on the interface, and select page 0 registers. */
    237  1.34        ws 	NIC_BARRIER(regt, regh);
    238   1.1    scottr 	NIC_PUT(regt, regh, ED_P0_CR,
    239   1.1    scottr 	    sc->cr_proto | ED_CR_PAGE_0 | ED_CR_STP);
    240  1.34        ws 	NIC_BARRIER(regt, regh);
    241   1.1    scottr 
    242   1.1    scottr 	/*
    243   1.1    scottr 	 * Wait for interface to enter stopped state, but limit # of checks to
    244   1.1    scottr 	 * 'n' (about 5ms).  It shouldn't even take 5us on modern DS8390's, but
    245   1.1    scottr 	 * just in case it's an old one.
    246   1.1    scottr 	 */
    247   1.1    scottr 	while (((NIC_GET(regt, regh,
    248   1.1    scottr 	    ED_P0_ISR) & ED_ISR_RST) == 0) && --n)
    249  1.34        ws 		DELAY(1);
    250  1.44   thorpej 
    251  1.44   thorpej 	if (sc->stop_card != NULL)
    252  1.44   thorpej 		(*sc->stop_card)(sc);
    253   1.1    scottr }
    254   1.1    scottr 
    255   1.1    scottr /*
    256   1.1    scottr  * Device timeout/watchdog routine.  Entered if the device neglects to generate
    257   1.1    scottr  * an interrupt after a transmit has been started on it.
    258   1.1    scottr  */
    259   1.1    scottr 
    260   1.1    scottr void
    261   1.1    scottr dp8390_watchdog(ifp)
    262   1.1    scottr 	struct ifnet *ifp;
    263   1.1    scottr {
    264   1.1    scottr 	struct dp8390_softc *sc = ifp->if_softc;
    265   1.1    scottr 
    266   1.1    scottr 	log(LOG_ERR, "%s: device timeout\n", sc->sc_dev.dv_xname);
    267   1.1    scottr 	++sc->sc_ec.ec_if.if_oerrors;
    268   1.1    scottr 
    269   1.1    scottr 	dp8390_reset(sc);
    270   1.1    scottr }
    271   1.1    scottr 
    272   1.1    scottr /*
    273   1.1    scottr  * Initialize device.
    274   1.1    scottr  */
    275   1.1    scottr void
    276   1.1    scottr dp8390_init(sc)
    277   1.1    scottr 	struct dp8390_softc *sc;
    278   1.1    scottr {
    279   1.1    scottr 	bus_space_tag_t regt = sc->sc_regt;
    280   1.1    scottr 	bus_space_handle_t regh = sc->sc_regh;
    281   1.1    scottr 	struct ifnet *ifp = &sc->sc_ec.ec_if;
    282   1.1    scottr 	u_int8_t mcaf[8];
    283   1.1    scottr 	int i;
    284   1.1    scottr 
    285   1.1    scottr 	/*
    286   1.1    scottr 	 * Initialize the NIC in the exact order outlined in the NS manual.
    287   1.1    scottr 	 * This init procedure is "mandatory"...don't change what or when
    288   1.1    scottr 	 * things happen.
    289   1.1    scottr 	 */
    290   1.1    scottr 
    291   1.1    scottr 	/* Reset transmitter flags. */
    292   1.1    scottr 	ifp->if_timer = 0;
    293   1.1    scottr 
    294   1.1    scottr 	sc->txb_inuse = 0;
    295   1.1    scottr 	sc->txb_new = 0;
    296   1.1    scottr 	sc->txb_next_tx = 0;
    297   1.1    scottr 
    298   1.1    scottr 	/* Set interface for page 0, remote DMA complete, stopped. */
    299  1.34        ws 	NIC_BARRIER(regt, regh);
    300   1.1    scottr 	NIC_PUT(regt, regh, ED_P0_CR,
    301   1.1    scottr 	    sc->cr_proto | ED_CR_PAGE_0 | ED_CR_STP);
    302  1.34        ws 	NIC_BARRIER(regt, regh);
    303   1.1    scottr 
    304   1.2    scottr 	if (sc->dcr_reg & ED_DCR_LS) {
    305   1.3    scottr 		NIC_PUT(regt, regh, ED_P0_DCR, sc->dcr_reg);
    306   1.2    scottr 	} else {
    307   1.1    scottr 		/*
    308   1.1    scottr 		 * Set FIFO threshold to 8, No auto-init Remote DMA, byte
    309   1.2    scottr 		 * order=80x86, byte-wide DMA xfers,
    310   1.1    scottr 		 */
    311   1.1    scottr 		NIC_PUT(regt, regh, ED_P0_DCR, ED_DCR_FT1 | ED_DCR_LS);
    312   1.1    scottr 	}
    313   1.1    scottr 
    314   1.1    scottr 	/* Clear remote byte count registers. */
    315   1.1    scottr 	NIC_PUT(regt, regh, ED_P0_RBCR0, 0);
    316   1.1    scottr 	NIC_PUT(regt, regh, ED_P0_RBCR1, 0);
    317   1.1    scottr 
    318   1.1    scottr 	/* Tell RCR to do nothing for now. */
    319  1.33     enami 	NIC_PUT(regt, regh, ED_P0_RCR, ED_RCR_MON | sc->rcr_proto);
    320   1.1    scottr 
    321   1.1    scottr 	/* Place NIC in internal loopback mode. */
    322   1.1    scottr 	NIC_PUT(regt, regh, ED_P0_TCR, ED_TCR_LB0);
    323   1.1    scottr 
    324   1.1    scottr 	/* Set lower bits of byte addressable framing to 0. */
    325   1.1    scottr 	if (sc->is790)
    326   1.1    scottr 		NIC_PUT(regt, regh, 0x09, 0);
    327   1.1    scottr 
    328   1.1    scottr 	/* Initialize receive buffer ring. */
    329   1.1    scottr 	NIC_PUT(regt, regh, ED_P0_BNRY, sc->rec_page_start);
    330   1.1    scottr 	NIC_PUT(regt, regh, ED_P0_PSTART, sc->rec_page_start);
    331   1.1    scottr 	NIC_PUT(regt, regh, ED_P0_PSTOP, sc->rec_page_stop);
    332   1.1    scottr 
    333   1.1    scottr 	/*
    334   1.1    scottr 	 * Enable the following interrupts: receive/transmit complete,
    335   1.1    scottr 	 * receive/transmit error, and Receiver OverWrite.
    336   1.1    scottr 	 *
    337   1.1    scottr 	 * Counter overflow and Remote DMA complete are *not* enabled.
    338   1.1    scottr 	 */
    339   1.1    scottr 	NIC_PUT(regt, regh, ED_P0_IMR,
    340   1.1    scottr 	    ED_IMR_PRXE | ED_IMR_PTXE | ED_IMR_RXEE | ED_IMR_TXEE |
    341   1.1    scottr 	    ED_IMR_OVWE);
    342   1.1    scottr 
    343  1.14   mycroft 	/*
    344  1.14   mycroft 	 * Clear all interrupts.  A '1' in each bit position clears the
    345  1.14   mycroft 	 * corresponding flag.
    346  1.14   mycroft 	 */
    347  1.14   mycroft 	NIC_PUT(regt, regh, ED_P0_ISR, 0xff);
    348  1.14   mycroft 
    349   1.1    scottr 	/* Program command register for page 1. */
    350  1.34        ws 	NIC_BARRIER(regt, regh);
    351   1.1    scottr 	NIC_PUT(regt, regh, ED_P0_CR,
    352   1.1    scottr 	    sc->cr_proto | ED_CR_PAGE_1 | ED_CR_STP);
    353  1.34        ws 	NIC_BARRIER(regt, regh);
    354   1.1    scottr 
    355   1.1    scottr 	/* Copy out our station address. */
    356   1.1    scottr 	for (i = 0; i < ETHER_ADDR_LEN; ++i)
    357   1.1    scottr 		NIC_PUT(regt, regh, ED_P1_PAR0 + i,
    358   1.1    scottr 		    LLADDR(ifp->if_sadl)[i]);
    359   1.1    scottr 
    360   1.1    scottr 	/* Set multicast filter on chip. */
    361   1.1    scottr 	dp8390_getmcaf(&sc->sc_ec, mcaf);
    362   1.1    scottr 	for (i = 0; i < 8; i++)
    363   1.1    scottr 		NIC_PUT(regt, regh, ED_P1_MAR0 + i, mcaf[i]);
    364   1.1    scottr 
    365   1.1    scottr 	/*
    366   1.1    scottr 	 * Set current page pointer to one page after the boundary pointer, as
    367   1.1    scottr 	 * recommended in the National manual.
    368   1.1    scottr 	 */
    369   1.1    scottr 	sc->next_packet = sc->rec_page_start + 1;
    370   1.1    scottr 	NIC_PUT(regt, regh, ED_P1_CURR, sc->next_packet);
    371   1.1    scottr 
    372   1.1    scottr 	/* Program command register for page 0. */
    373  1.34        ws 	NIC_BARRIER(regt, regh);
    374   1.1    scottr 	NIC_PUT(regt, regh, ED_P1_CR,
    375   1.1    scottr 	    sc->cr_proto | ED_CR_PAGE_0 | ED_CR_STP);
    376  1.34        ws 	NIC_BARRIER(regt, regh);
    377   1.1    scottr 
    378  1.14   mycroft 	/* Accept broadcast and multicast packets by default. */
    379  1.33     enami 	i = ED_RCR_AB | ED_RCR_AM | sc->rcr_proto;
    380   1.1    scottr 	if (ifp->if_flags & IFF_PROMISC) {
    381   1.1    scottr 		/*
    382   1.1    scottr 		 * Set promiscuous mode.  Multicast filter was set earlier so
    383   1.1    scottr 		 * that we should receive all multicast packets.
    384   1.1    scottr 		 */
    385   1.1    scottr 		i |= ED_RCR_PRO | ED_RCR_AR | ED_RCR_SEP;
    386   1.1    scottr 	}
    387   1.1    scottr 	NIC_PUT(regt, regh, ED_P0_RCR, i);
    388   1.1    scottr 
    389   1.1    scottr 	/* Take interface out of loopback. */
    390   1.1    scottr 	NIC_PUT(regt, regh, ED_P0_TCR, 0);
    391   1.1    scottr 
    392   1.1    scottr 	/* Do any card-specific initialization, if applicable. */
    393   1.1    scottr 	if (sc->init_card)
    394   1.1    scottr 		(*sc->init_card)(sc);
    395   1.1    scottr 
    396   1.1    scottr 	/* Fire up the interface. */
    397  1.34        ws 	NIC_BARRIER(regt, regh);
    398   1.1    scottr 	NIC_PUT(regt, regh, ED_P0_CR,
    399   1.1    scottr 	    sc->cr_proto | ED_CR_PAGE_0 | ED_CR_STA);
    400   1.1    scottr 
    401   1.1    scottr 	/* Set 'running' flag, and clear output active flag. */
    402   1.1    scottr 	ifp->if_flags |= IFF_RUNNING;
    403   1.1    scottr 	ifp->if_flags &= ~IFF_OACTIVE;
    404   1.1    scottr 
    405   1.1    scottr 	/* ...and attempt to start output. */
    406   1.1    scottr 	dp8390_start(ifp);
    407   1.1    scottr }
    408   1.1    scottr 
    409   1.1    scottr /*
    410   1.1    scottr  * This routine actually starts the transmission on the interface.
    411   1.1    scottr  */
    412   1.1    scottr static __inline__ void
    413   1.1    scottr dp8390_xmit(sc)
    414   1.1    scottr 	struct dp8390_softc *sc;
    415   1.1    scottr {
    416   1.1    scottr 	bus_space_tag_t regt = sc->sc_regt;
    417   1.1    scottr 	bus_space_handle_t regh = sc->sc_regh;
    418   1.1    scottr 	struct ifnet *ifp = &sc->sc_ec.ec_if;
    419   1.1    scottr 	u_short len;
    420   1.1    scottr 
    421  1.14   mycroft #ifdef DIAGNOSTIC
    422  1.14   mycroft 	if ((sc->txb_next_tx + sc->txb_inuse) % sc->txb_cnt != sc->txb_new)
    423  1.14   mycroft 		panic("dp8390_xmit: desync, next_tx=%d inuse=%d cnt=%d new=%d",
    424  1.14   mycroft 		    sc->txb_next_tx, sc->txb_inuse, sc->txb_cnt, sc->txb_new);
    425  1.14   mycroft 
    426  1.14   mycroft 	if (sc->txb_inuse == 0)
    427  1.50    provos 		panic("dp8390_xmit: no packets to xmit");
    428  1.14   mycroft #endif
    429  1.14   mycroft 
    430   1.1    scottr 	len = sc->txb_len[sc->txb_next_tx];
    431   1.1    scottr 
    432   1.1    scottr 	/* Set NIC for page 0 register access. */
    433  1.34        ws 	NIC_BARRIER(regt, regh);
    434   1.1    scottr 	NIC_PUT(regt, regh, ED_P0_CR,
    435   1.1    scottr 	    sc->cr_proto | ED_CR_PAGE_0 | ED_CR_STA);
    436  1.34        ws 	NIC_BARRIER(regt, regh);
    437   1.1    scottr 
    438   1.1    scottr 	/* Set TX buffer start page. */
    439   1.1    scottr 	NIC_PUT(regt, regh, ED_P0_TPSR, sc->tx_page_start +
    440   1.1    scottr 	    sc->txb_next_tx * ED_TXBUF_SIZE);
    441   1.1    scottr 
    442   1.1    scottr 	/* Set TX length. */
    443   1.1    scottr 	NIC_PUT(regt, regh, ED_P0_TBCR0, len);
    444   1.1    scottr 	NIC_PUT(regt, regh, ED_P0_TBCR1, len >> 8);
    445   1.1    scottr 
    446   1.1    scottr 	/* Set page 0, remote DMA complete, transmit packet, and *start*. */
    447  1.34        ws 	NIC_BARRIER(regt, regh);
    448   1.1    scottr 	NIC_PUT(regt, regh, ED_P0_CR,
    449   1.1    scottr 	    sc->cr_proto | ED_CR_PAGE_0 | ED_CR_TXP | ED_CR_STA);
    450   1.1    scottr 
    451   1.1    scottr 	/* Point to next transmit buffer slot and wrap if necessary. */
    452  1.14   mycroft 	if (++sc->txb_next_tx == sc->txb_cnt)
    453   1.1    scottr 		sc->txb_next_tx = 0;
    454   1.1    scottr 
    455   1.1    scottr 	/* Set a timer just in case we never hear from the board again. */
    456   1.1    scottr 	ifp->if_timer = 2;
    457   1.1    scottr }
    458   1.1    scottr 
    459   1.1    scottr /*
    460   1.1    scottr  * Start output on interface.
    461   1.1    scottr  * We make two assumptions here:
    462   1.1    scottr  *  1) that the current priority is set to splnet _before_ this code
    463   1.1    scottr  *     is called *and* is returned to the appropriate priority after
    464   1.1    scottr  *     return
    465   1.1    scottr  *  2) that the IFF_OACTIVE flag is checked before this code is called
    466   1.1    scottr  *     (i.e. that the output part of the interface is idle)
    467   1.1    scottr  */
    468   1.1    scottr void
    469   1.1    scottr dp8390_start(ifp)
    470   1.1    scottr 	struct ifnet *ifp;
    471   1.1    scottr {
    472   1.1    scottr 	struct dp8390_softc *sc = ifp->if_softc;
    473   1.1    scottr 	struct mbuf *m0;
    474   1.1    scottr 	int buffer;
    475   1.1    scottr 	int len;
    476   1.1    scottr 
    477   1.1    scottr 	if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
    478   1.1    scottr 		return;
    479   1.1    scottr 
    480   1.1    scottr outloop:
    481   1.1    scottr 	/* See if there is room to put another packet in the buffer. */
    482   1.1    scottr 	if (sc->txb_inuse == sc->txb_cnt) {
    483   1.1    scottr 		/* No room.  Indicate this to the outside world and exit. */
    484   1.1    scottr 		ifp->if_flags |= IFF_OACTIVE;
    485   1.1    scottr 		return;
    486   1.1    scottr 	}
    487  1.41   thorpej 	IFQ_DEQUEUE(&ifp->if_snd, m0);
    488   1.1    scottr 	if (m0 == 0)
    489   1.1    scottr 		return;
    490   1.1    scottr 
    491   1.1    scottr 	/* We need to use m->m_pkthdr.len, so require the header */
    492   1.1    scottr 	if ((m0->m_flags & M_PKTHDR) == 0)
    493   1.1    scottr 		panic("dp8390_start: no header mbuf");
    494   1.1    scottr 
    495   1.1    scottr #if NBPFILTER > 0
    496   1.1    scottr 	/* Tap off here if there is a BPF listener. */
    497   1.1    scottr 	if (ifp->if_bpf)
    498   1.1    scottr 		bpf_mtap(ifp->if_bpf, m0);
    499   1.1    scottr #endif
    500   1.1    scottr 
    501   1.1    scottr 	/* txb_new points to next open buffer slot. */
    502   1.1    scottr 	buffer = sc->mem_start +
    503   1.1    scottr 	    ((sc->txb_new * ED_TXBUF_SIZE) << ED_PAGE_SHIFT);
    504   1.1    scottr 
    505   1.1    scottr 	if (sc->write_mbuf)
    506   1.1    scottr 		len = (*sc->write_mbuf)(sc, m0, buffer);
    507   1.1    scottr 	else
    508   1.1    scottr 		len = dp8390_write_mbuf(sc, m0, buffer);
    509   1.1    scottr 
    510   1.1    scottr 	m_freem(m0);
    511  1.52    bouyer 	sc->txb_len[sc->txb_new] = len;
    512   1.1    scottr 
    513   1.1    scottr 	/* Point to next buffer slot and wrap if necessary. */
    514   1.1    scottr 	if (++sc->txb_new == sc->txb_cnt)
    515   1.1    scottr 		sc->txb_new = 0;
    516   1.1    scottr 
    517  1.14   mycroft 	/* Start the first packet transmitting. */
    518  1.14   mycroft 	if (sc->txb_inuse++ == 0)
    519  1.14   mycroft 		dp8390_xmit(sc);
    520   1.1    scottr 
    521   1.1    scottr 	/* Loop back to the top to possibly buffer more packets. */
    522   1.1    scottr 	goto outloop;
    523   1.1    scottr }
    524   1.1    scottr 
    525   1.1    scottr /*
    526   1.1    scottr  * Ethernet interface receiver interrupt.
    527   1.1    scottr  */
    528   1.1    scottr void
    529   1.1    scottr dp8390_rint(sc)
    530   1.1    scottr 	struct dp8390_softc *sc;
    531   1.1    scottr {
    532   1.1    scottr 	bus_space_tag_t regt = sc->sc_regt;
    533   1.1    scottr 	bus_space_handle_t regh = sc->sc_regh;
    534   1.1    scottr 	struct dp8390_ring packet_hdr;
    535   1.1    scottr 	int packet_ptr;
    536   1.1    scottr 	u_short len;
    537   1.1    scottr 	u_char boundary, current;
    538   1.1    scottr 	u_char nlen;
    539   1.1    scottr 
    540   1.1    scottr loop:
    541   1.1    scottr 	/* Set NIC to page 1 registers to get 'current' pointer. */
    542  1.34        ws 	NIC_BARRIER(regt, regh);
    543   1.1    scottr 	NIC_PUT(regt, regh, ED_P0_CR,
    544   1.1    scottr 	    sc->cr_proto | ED_CR_PAGE_1 | ED_CR_STA);
    545  1.34        ws 	NIC_BARRIER(regt, regh);
    546   1.1    scottr 
    547   1.1    scottr 	/*
    548   1.1    scottr 	 * 'sc->next_packet' is the logical beginning of the ring-buffer - i.e.
    549   1.1    scottr 	 * it points to where new data has been buffered.  The 'CURR' (current)
    550   1.1    scottr 	 * register points to the logical end of the ring-buffer - i.e. it
    551   1.1    scottr 	 * points to where additional new data will be added.  We loop here
    552   1.1    scottr 	 * until the logical beginning equals the logical end (or in other
    553   1.1    scottr 	 * words, until the ring-buffer is empty).
    554   1.1    scottr 	 */
    555   1.1    scottr 	current = NIC_GET(regt, regh, ED_P1_CURR);
    556   1.1    scottr 	if (sc->next_packet == current)
    557   1.1    scottr 		return;
    558   1.1    scottr 
    559   1.1    scottr 	/* Set NIC to page 0 registers to update boundary register. */
    560  1.34        ws 	NIC_BARRIER(regt, regh);
    561   1.1    scottr 	NIC_PUT(regt, regh, ED_P1_CR,
    562   1.1    scottr 	    sc->cr_proto | ED_CR_PAGE_0 | ED_CR_STA);
    563  1.34        ws 	NIC_BARRIER(regt, regh);
    564   1.1    scottr 
    565   1.1    scottr 	do {
    566   1.1    scottr 		/* Get pointer to this buffer's header structure. */
    567   1.1    scottr 		packet_ptr = sc->mem_ring +
    568   1.1    scottr 		    ((sc->next_packet - sc->rec_page_start) << ED_PAGE_SHIFT);
    569   1.1    scottr 
    570   1.1    scottr 		if (sc->read_hdr)
    571   1.1    scottr 			(*sc->read_hdr)(sc, packet_ptr, &packet_hdr);
    572   1.1    scottr 		else
    573   1.1    scottr 			dp8390_read_hdr(sc, packet_ptr, &packet_hdr);
    574   1.1    scottr 		len = packet_hdr.count;
    575   1.1    scottr 
    576   1.1    scottr 		/*
    577   1.1    scottr 		 * Try do deal with old, buggy chips that sometimes duplicate
    578   1.1    scottr 		 * the low byte of the length into the high byte.  We do this
    579   1.1    scottr 		 * by simply ignoring the high byte of the length and always
    580   1.1    scottr 		 * recalculating it.
    581   1.1    scottr 		 *
    582   1.1    scottr 		 * NOTE: sc->next_packet is pointing at the current packet.
    583   1.1    scottr 		 */
    584   1.1    scottr 		if (packet_hdr.next_packet >= sc->next_packet)
    585   1.1    scottr 			nlen = (packet_hdr.next_packet - sc->next_packet);
    586   1.1    scottr 		else
    587   1.1    scottr 			nlen = ((packet_hdr.next_packet - sc->rec_page_start) +
    588   1.1    scottr 			    (sc->rec_page_stop - sc->next_packet));
    589   1.1    scottr 		--nlen;
    590   1.1    scottr 		if ((len & ED_PAGE_MASK) + sizeof(packet_hdr) > ED_PAGE_SIZE)
    591   1.1    scottr 			--nlen;
    592   1.1    scottr 		len = (len & ED_PAGE_MASK) | (nlen << ED_PAGE_SHIFT);
    593   1.1    scottr #ifdef DIAGNOSTIC
    594   1.1    scottr 		if (len != packet_hdr.count) {
    595   1.1    scottr 			printf("%s: length does not match "
    596   1.1    scottr 			    "next packet pointer\n", sc->sc_dev.dv_xname);
    597   1.1    scottr 			printf("%s: len %04x nlen %04x start %02x "
    598   1.1    scottr 			    "first %02x curr %02x next %02x stop %02x\n",
    599   1.1    scottr 			    sc->sc_dev.dv_xname, packet_hdr.count, len,
    600   1.1    scottr 			    sc->rec_page_start, sc->next_packet, current,
    601   1.1    scottr 			    packet_hdr.next_packet, sc->rec_page_stop);
    602   1.1    scottr 		}
    603   1.1    scottr #endif
    604   1.1    scottr 
    605   1.1    scottr 		/*
    606   1.1    scottr 		 * Be fairly liberal about what we allow as a "reasonable"
    607   1.1    scottr 		 * length so that a [crufty] packet will make it to BPF (and
    608   1.1    scottr 		 * can thus be analyzed).  Note that all that is really
    609   1.1    scottr 		 * important is that we have a length that will fit into one
    610   1.1    scottr 		 * mbuf cluster or less; the upper layer protocols can then
    611   1.1    scottr 		 * figure out the length from their own length field(s).
    612   1.1    scottr 		 */
    613   1.1    scottr 		if (len <= MCLBYTES &&
    614   1.1    scottr 		    packet_hdr.next_packet >= sc->rec_page_start &&
    615   1.1    scottr 		    packet_hdr.next_packet < sc->rec_page_stop) {
    616   1.1    scottr 			/* Go get packet. */
    617   1.1    scottr 			dp8390_read(sc,
    618   1.1    scottr 			    packet_ptr + sizeof(struct dp8390_ring),
    619   1.1    scottr 			    len - sizeof(struct dp8390_ring));
    620   1.1    scottr 		} else {
    621   1.1    scottr 			/* Really BAD.  The ring pointers are corrupted. */
    622   1.1    scottr 			log(LOG_ERR, "%s: NIC memory corrupt - "
    623   1.1    scottr 			    "invalid packet length %d\n",
    624   1.1    scottr 			    sc->sc_dev.dv_xname, len);
    625   1.1    scottr 			++sc->sc_ec.ec_if.if_ierrors;
    626   1.1    scottr 			dp8390_reset(sc);
    627   1.1    scottr 			return;
    628   1.1    scottr 		}
    629   1.1    scottr 
    630   1.1    scottr 		/* Update next packet pointer. */
    631   1.1    scottr 		sc->next_packet = packet_hdr.next_packet;
    632   1.1    scottr 
    633   1.1    scottr 		/*
    634   1.1    scottr 		 * Update NIC boundary pointer - being careful to keep it one
    635   1.1    scottr 		 * buffer behind (as recommended by NS databook).
    636   1.1    scottr 		 */
    637   1.1    scottr 		boundary = sc->next_packet - 1;
    638   1.1    scottr 		if (boundary < sc->rec_page_start)
    639   1.1    scottr 			boundary = sc->rec_page_stop - 1;
    640   1.1    scottr 		NIC_PUT(regt, regh, ED_P0_BNRY, boundary);
    641   1.1    scottr 	} while (sc->next_packet != current);
    642   1.1    scottr 
    643   1.1    scottr 	goto loop;
    644   1.1    scottr }
    645   1.1    scottr 
    646   1.1    scottr /* Ethernet interface interrupt processor. */
    647   1.7   thorpej int
    648   1.7   thorpej dp8390_intr(arg)
    649   1.1    scottr 	void *arg;
    650   1.1    scottr {
    651   1.1    scottr 	struct dp8390_softc *sc = (struct dp8390_softc *)arg;
    652   1.1    scottr 	bus_space_tag_t regt = sc->sc_regt;
    653   1.1    scottr 	bus_space_handle_t regh = sc->sc_regh;
    654   1.1    scottr 	struct ifnet *ifp = &sc->sc_ec.ec_if;
    655   1.1    scottr 	u_char isr;
    656  1.20   thorpej #if NRND > 0
    657  1.20   thorpej 	u_char rndisr;
    658  1.20   thorpej #endif
    659   1.1    scottr 
    660  1.29     enami 	if (sc->sc_enabled == 0 ||
    661  1.29     enami 	    (sc->sc_dev.dv_flags & DVF_ACTIVE) == 0)
    662   1.7   thorpej 		return (0);
    663   1.7   thorpej 
    664   1.1    scottr 	/* Set NIC to page 0 registers. */
    665  1.34        ws 	NIC_BARRIER(regt, regh);
    666   1.1    scottr 	NIC_PUT(regt, regh, ED_P0_CR,
    667   1.1    scottr 	    sc->cr_proto | ED_CR_PAGE_0 | ED_CR_STA);
    668  1.34        ws 	NIC_BARRIER(regt, regh);
    669   1.1    scottr 
    670   1.1    scottr 	isr = NIC_GET(regt, regh, ED_P0_ISR);
    671   1.1    scottr 	if (!isr)
    672   1.7   thorpej 		return (0);
    673   1.1    scottr 
    674  1.20   thorpej #if NRND > 0
    675  1.20   thorpej 	rndisr = isr;
    676  1.20   thorpej #endif
    677  1.20   thorpej 
    678   1.1    scottr 	/* Loop until there are no more new interrupts. */
    679   1.1    scottr 	for (;;) {
    680   1.1    scottr 		/*
    681   1.1    scottr 		 * Reset all the bits that we are 'acknowledging' by writing a
    682   1.1    scottr 		 * '1' to each bit position that was set.
    683   1.1    scottr 		 * (Writing a '1' *clears* the bit.)
    684   1.1    scottr 		 */
    685   1.1    scottr 		NIC_PUT(regt, regh, ED_P0_ISR, isr);
    686  1.33     enami 
    687  1.33     enami 		/* Work around for AX88190 bug */
    688  1.33     enami 		if ((sc->sc_flags & DP8390_DO_AX88190_WORKAROUND) != 0)
    689  1.33     enami 			while ((NIC_GET(regt, regh, ED_P0_ISR) & isr) != 0) {
    690  1.33     enami 				NIC_PUT(regt, regh, ED_P0_ISR, 0);
    691  1.33     enami 				NIC_PUT(regt, regh, ED_P0_ISR, isr);
    692  1.33     enami 			}
    693   1.1    scottr 
    694   1.1    scottr 		/*
    695   1.1    scottr 		 * Handle transmitter interrupts.  Handle these first because
    696   1.1    scottr 		 * the receiver will reset the board under some conditions.
    697  1.14   mycroft 		 *
    698  1.14   mycroft 		 * If the chip was reset while a packet was transmitting, it
    699  1.14   mycroft 		 * may still deliver a TX interrupt.  In this case, just ignore
    700  1.14   mycroft 		 * the interrupt.
    701   1.1    scottr 		 */
    702  1.14   mycroft 		if (isr & (ED_ISR_PTX | ED_ISR_TXE) &&
    703  1.14   mycroft 		    sc->txb_inuse != 0) {
    704  1.14   mycroft 			u_char collisions =
    705  1.14   mycroft 			    NIC_GET(regt, regh, ED_P0_NCR) & 0x0f;
    706   1.1    scottr 
    707   1.1    scottr 			/*
    708   1.1    scottr 			 * Check for transmit error.  If a TX completed with an
    709   1.1    scottr 			 * error, we end up throwing the packet away.  Really
    710   1.1    scottr 			 * the only error that is possible is excessive
    711   1.1    scottr 			 * collisions, and in this case it is best to allow the
    712   1.1    scottr 			 * automatic mechanisms of TCP to backoff the flow.  Of
    713   1.1    scottr 			 * course, with UDP we're screwed, but this is expected
    714   1.1    scottr 			 * when a network is heavily loaded.
    715   1.1    scottr 			 */
    716   1.1    scottr 			if (isr & ED_ISR_TXE) {
    717   1.1    scottr 				/*
    718   1.1    scottr 				 * Excessive collisions (16).
    719   1.1    scottr 				 */
    720   1.1    scottr 				if ((NIC_GET(regt, regh, ED_P0_TSR)
    721   1.1    scottr 				    & ED_TSR_ABT) && (collisions == 0)) {
    722   1.1    scottr 					/*
    723   1.1    scottr 					 * When collisions total 16, the P0_NCR
    724   1.1    scottr 					 * will indicate 0, and the TSR_ABT is
    725   1.1    scottr 					 * set.
    726   1.1    scottr 					 */
    727   1.1    scottr 					collisions = 16;
    728   1.1    scottr 				}
    729   1.1    scottr 
    730   1.1    scottr 				/* Update output errors counter. */
    731   1.1    scottr 				++ifp->if_oerrors;
    732   1.1    scottr 			} else {
    733   1.1    scottr 				/*
    734  1.14   mycroft 				 * Throw away the non-error status bits.
    735  1.14   mycroft 				 *
    736  1.14   mycroft 				 * XXX
    737  1.14   mycroft 				 * It may be useful to detect loss of carrier
    738  1.14   mycroft 				 * and late collisions here.
    739  1.14   mycroft 				 */
    740  1.14   mycroft 				(void)NIC_GET(regt, regh, ED_P0_TSR);
    741  1.14   mycroft 
    742  1.14   mycroft 				/*
    743   1.1    scottr 				 * Update total number of successfully
    744   1.1    scottr 				 * transmitted packets.
    745   1.1    scottr 				 */
    746   1.1    scottr 				++ifp->if_opackets;
    747   1.1    scottr 			}
    748   1.1    scottr 
    749   1.1    scottr 			/* Clear watchdog timer. */
    750   1.1    scottr 			ifp->if_timer = 0;
    751   1.1    scottr 			ifp->if_flags &= ~IFF_OACTIVE;
    752   1.1    scottr 
    753   1.1    scottr 			/*
    754   1.1    scottr 			 * Add in total number of collisions on last
    755   1.1    scottr 			 * transmission.
    756   1.1    scottr 			 */
    757   1.1    scottr 			ifp->if_collisions += collisions;
    758   1.1    scottr 
    759   1.1    scottr 			/*
    760   1.1    scottr 			 * Decrement buffer in-use count if not zero (can only
    761  1.48       wiz 			 * be zero if a transmitter interrupt occurred while not
    762   1.1    scottr 			 * actually transmitting).
    763   1.1    scottr 			 * If data is ready to transmit, start it transmitting,
    764   1.1    scottr 			 * otherwise defer until after handling receiver.
    765   1.1    scottr 			 */
    766  1.14   mycroft 			if (--sc->txb_inuse != 0)
    767   1.1    scottr 				dp8390_xmit(sc);
    768   1.1    scottr 		}
    769   1.1    scottr 
    770   1.1    scottr 		/* Handle receiver interrupts. */
    771   1.1    scottr 		if (isr & (ED_ISR_PRX | ED_ISR_RXE | ED_ISR_OVW)) {
    772   1.1    scottr 			/*
    773   1.1    scottr 			 * Overwrite warning.  In order to make sure that a
    774   1.1    scottr 			 * lockup of the local DMA hasn't occurred, we reset
    775   1.1    scottr 			 * and re-init the NIC.  The NSC manual suggests only a
    776   1.1    scottr 			 * partial reset/re-init is necessary - but some chips
    777   1.1    scottr 			 * seem to want more.  The DMA lockup has been seen
    778   1.1    scottr 			 * only with early rev chips - Methinks this bug was
    779   1.1    scottr 			 * fixed in later revs.  -DG
    780   1.1    scottr 			 */
    781   1.1    scottr 			if (isr & ED_ISR_OVW) {
    782   1.1    scottr 				++ifp->if_ierrors;
    783   1.1    scottr #ifdef DIAGNOSTIC
    784   1.1    scottr 				log(LOG_WARNING, "%s: warning - receiver "
    785   1.1    scottr 				    "ring buffer overrun\n",
    786   1.1    scottr 				    sc->sc_dev.dv_xname);
    787   1.1    scottr #endif
    788   1.1    scottr 				/* Stop/reset/re-init NIC. */
    789   1.1    scottr 				dp8390_reset(sc);
    790   1.1    scottr 			} else {
    791   1.1    scottr 				/*
    792   1.1    scottr 				 * Receiver Error.  One or more of: CRC error,
    793   1.1    scottr 				 * frame alignment error FIFO overrun, or
    794   1.1    scottr 				 * missed packet.
    795   1.1    scottr 				 */
    796   1.1    scottr 				if (isr & ED_ISR_RXE) {
    797   1.1    scottr 					++ifp->if_ierrors;
    798   1.1    scottr #ifdef DEBUG
    799   1.4    scottr 					if (dp8390_debug) {
    800   1.4    scottr 						printf("%s: receive error %x\n",
    801   1.4    scottr 						    sc->sc_dev.dv_xname,
    802   1.4    scottr 						    NIC_GET(regt, regh,
    803   1.4    scottr 							ED_P0_RSR));
    804   1.4    scottr 					}
    805   1.1    scottr #endif
    806   1.1    scottr 				}
    807   1.1    scottr 
    808   1.1    scottr 				/*
    809   1.1    scottr 				 * Go get the packet(s)
    810   1.1    scottr 				 * XXX - Doing this on an error is dubious
    811   1.1    scottr 				 * because there shouldn't be any data to get
    812   1.1    scottr 				 * (we've configured the interface to not
    813   1.1    scottr 				 * accept packets with errors).
    814   1.1    scottr 				 */
    815   1.1    scottr 				if (sc->recv_int)
    816   1.1    scottr 					(*sc->recv_int)(sc);
    817   1.1    scottr 				else
    818   1.1    scottr 					dp8390_rint(sc);
    819   1.1    scottr 			}
    820   1.1    scottr 		}
    821   1.1    scottr 
    822   1.1    scottr 		/*
    823   1.1    scottr 		 * If it looks like the transmitter can take more data, attempt
    824   1.1    scottr 		 * to start output on the interface.  This is done after
    825   1.1    scottr 		 * handling the receiver to give the receiver priority.
    826   1.1    scottr 		 */
    827   1.1    scottr 		dp8390_start(ifp);
    828   1.1    scottr 
    829   1.1    scottr 		/*
    830   1.1    scottr 		 * Return NIC CR to standard state: page 0, remote DMA
    831   1.1    scottr 		 * complete, start (toggling the TXP bit off, even if was just
    832   1.1    scottr 		 * set in the transmit routine, is *okay* - it is 'edge'
    833   1.1    scottr 		 * triggered from low to high).
    834   1.1    scottr 		 */
    835  1.34        ws 		NIC_BARRIER(regt, regh);
    836   1.1    scottr 		NIC_PUT(regt, regh, ED_P0_CR,
    837   1.1    scottr 		    sc->cr_proto | ED_CR_PAGE_0 | ED_CR_STA);
    838  1.34        ws 		NIC_BARRIER(regt, regh);
    839   1.1    scottr 
    840   1.1    scottr 		/*
    841   1.1    scottr 		 * If the Network Talley Counters overflow, read them to reset
    842   1.1    scottr 		 * them.  It appears that old 8390's won't clear the ISR flag
    843   1.1    scottr 		 * otherwise - resulting in an infinite loop.
    844   1.1    scottr 		 */
    845   1.1    scottr 		if (isr & ED_ISR_CNT) {
    846   1.1    scottr 			(void)NIC_GET(regt, regh, ED_P0_CNTR0);
    847   1.1    scottr 			(void)NIC_GET(regt, regh, ED_P0_CNTR1);
    848   1.1    scottr 			(void)NIC_GET(regt, regh, ED_P0_CNTR2);
    849   1.1    scottr 		}
    850   1.1    scottr 
    851   1.1    scottr 		isr = NIC_GET(regt, regh, ED_P0_ISR);
    852   1.1    scottr 		if (!isr)
    853  1.20   thorpej 			goto out;
    854   1.1    scottr 	}
    855  1.20   thorpej 
    856  1.20   thorpej  out:
    857  1.20   thorpej #if NRND > 0
    858  1.20   thorpej 	rnd_add_uint32(&sc->rnd_source, rndisr);
    859  1.20   thorpej #endif
    860  1.20   thorpej 	return (1);
    861   1.1    scottr }
    862   1.1    scottr 
    863   1.1    scottr /*
    864   1.1    scottr  * Process an ioctl request.  This code needs some work - it looks pretty ugly.
    865   1.1    scottr  */
    866   1.1    scottr int
    867   1.1    scottr dp8390_ioctl(ifp, cmd, data)
    868   1.1    scottr 	struct ifnet *ifp;
    869   1.1    scottr 	u_long cmd;
    870   1.1    scottr 	caddr_t data;
    871   1.1    scottr {
    872   1.1    scottr 	struct dp8390_softc *sc = ifp->if_softc;
    873   1.1    scottr 	struct ifaddr *ifa = (struct ifaddr *) data;
    874   1.1    scottr 	struct ifreq *ifr = (struct ifreq *) data;
    875   1.8   thorpej 	int s, error = 0;
    876   1.1    scottr 
    877   1.1    scottr 	s = splnet();
    878   1.1    scottr 
    879   1.1    scottr 	switch (cmd) {
    880   1.1    scottr 
    881   1.1    scottr 	case SIOCSIFADDR:
    882   1.7   thorpej 		if ((error = dp8390_enable(sc)) != 0)
    883   1.7   thorpej 			break;
    884   1.1    scottr 		ifp->if_flags |= IFF_UP;
    885   1.1    scottr 
    886   1.1    scottr 		switch (ifa->ifa_addr->sa_family) {
    887   1.1    scottr #ifdef INET
    888   1.1    scottr 		case AF_INET:
    889   1.1    scottr 			dp8390_init(sc);
    890   1.1    scottr 			arp_ifinit(ifp, ifa);
    891   1.1    scottr 			break;
    892   1.1    scottr #endif
    893   1.1    scottr #ifdef NS
    894   1.1    scottr 			/* XXX - This code is probably wrong. */
    895   1.1    scottr 		case AF_NS:
    896   1.1    scottr 		    {
    897   1.1    scottr 			struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
    898   1.1    scottr 
    899   1.1    scottr 			if (ns_nullhost(*ina))
    900   1.1    scottr 				ina->x_host =
    901   1.1    scottr 				    *(union ns_host *)LLADDR(ifp->if_sadl);
    902   1.1    scottr 			else
    903  1.46   thorpej 				memcpy(LLADDR(ifp->if_sadl),
    904  1.46   thorpej 				    ina->x_host.c_host, ETHER_ADDR_LEN);
    905   1.1    scottr 			/* Set new address. */
    906   1.1    scottr 			dp8390_init(sc);
    907   1.1    scottr 			break;
    908   1.1    scottr 		    }
    909   1.1    scottr #endif
    910   1.1    scottr 		default:
    911   1.1    scottr 			dp8390_init(sc);
    912   1.1    scottr 			break;
    913   1.1    scottr 		}
    914   1.1    scottr 		break;
    915   1.1    scottr 
    916   1.1    scottr 	case SIOCSIFFLAGS:
    917   1.1    scottr 		if ((ifp->if_flags & IFF_UP) == 0 &&
    918   1.1    scottr 		    (ifp->if_flags & IFF_RUNNING) != 0) {
    919   1.1    scottr 			/*
    920   1.1    scottr 			 * If interface is marked down and it is running, then
    921   1.1    scottr 			 * stop it.
    922   1.1    scottr 			 */
    923   1.1    scottr 			dp8390_stop(sc);
    924   1.1    scottr 			ifp->if_flags &= ~IFF_RUNNING;
    925   1.7   thorpej 			dp8390_disable(sc);
    926   1.1    scottr 		} else if ((ifp->if_flags & IFF_UP) != 0 &&
    927   1.1    scottr 		    (ifp->if_flags & IFF_RUNNING) == 0) {
    928   1.1    scottr 			/*
    929   1.1    scottr 			 * If interface is marked up and it is stopped, then
    930   1.1    scottr 			 * start it.
    931   1.1    scottr 			 */
    932   1.7   thorpej 			if ((error = dp8390_enable(sc)) != 0)
    933   1.7   thorpej 				break;
    934   1.1    scottr 			dp8390_init(sc);
    935  1.21   thorpej 		} else if ((ifp->if_flags & IFF_UP) != 0) {
    936   1.1    scottr 			/*
    937   1.1    scottr 			 * Reset the interface to pick up changes in any other
    938   1.1    scottr 			 * flags that affect hardware registers.
    939   1.1    scottr 			 */
    940   1.1    scottr 			dp8390_stop(sc);
    941   1.1    scottr 			dp8390_init(sc);
    942   1.1    scottr 		}
    943   1.1    scottr 		break;
    944   1.1    scottr 
    945   1.1    scottr 	case SIOCADDMULTI:
    946   1.1    scottr 	case SIOCDELMULTI:
    947   1.7   thorpej 		if (sc->sc_enabled == 0) {
    948   1.7   thorpej 			error = EIO;
    949   1.7   thorpej 			break;
    950   1.7   thorpej 		}
    951   1.7   thorpej 
    952   1.1    scottr 		/* Update our multicast list. */
    953   1.1    scottr 		error = (cmd == SIOCADDMULTI) ?
    954   1.1    scottr 		    ether_addmulti(ifr, &sc->sc_ec) :
    955   1.1    scottr 		    ether_delmulti(ifr, &sc->sc_ec);
    956   1.1    scottr 
    957   1.1    scottr 		if (error == ENETRESET) {
    958   1.1    scottr 			/*
    959   1.1    scottr 			 * Multicast list has changed; set the hardware filter
    960   1.1    scottr 			 * accordingly.
    961   1.1    scottr 			 */
    962  1.54   thorpej 			if (ifp->if_flags & IFF_RUNNING) {
    963  1.54   thorpej 				dp8390_stop(sc); /* XXX for ds_setmcaf? */
    964  1.54   thorpej 				dp8390_init(sc);
    965  1.54   thorpej 			}
    966   1.1    scottr 			error = 0;
    967   1.1    scottr 		}
    968   1.8   thorpej 		break;
    969   1.8   thorpej 
    970   1.8   thorpej 	case SIOCGIFMEDIA:
    971   1.8   thorpej 	case SIOCSIFMEDIA:
    972   1.8   thorpej 		error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
    973   1.1    scottr 		break;
    974   1.1    scottr 
    975   1.1    scottr 	default:
    976   1.1    scottr 		error = EINVAL;
    977   1.1    scottr 		break;
    978   1.1    scottr 	}
    979   1.1    scottr 
    980   1.1    scottr 	splx(s);
    981   1.1    scottr 	return (error);
    982   1.1    scottr }
    983   1.1    scottr 
    984   1.1    scottr /*
    985   1.1    scottr  * Retrieve packet from buffer memory and send to the next level up via
    986   1.1    scottr  * ether_input().  If there is a BPF listener, give a copy to BPF, too.
    987   1.1    scottr  */
    988   1.1    scottr void
    989   1.1    scottr dp8390_read(sc, buf, len)
    990   1.1    scottr 	struct dp8390_softc *sc;
    991   1.1    scottr 	int buf;
    992   1.1    scottr 	u_short len;
    993   1.1    scottr {
    994   1.1    scottr 	struct ifnet *ifp = &sc->sc_ec.ec_if;
    995   1.1    scottr 	struct mbuf *m;
    996   1.1    scottr 
    997   1.1    scottr 	/* Pull packet off interface. */
    998   1.1    scottr 	m = dp8390_get(sc, buf, len);
    999   1.1    scottr 	if (m == 0) {
   1000   1.1    scottr 		ifp->if_ierrors++;
   1001   1.1    scottr 		return;
   1002   1.1    scottr 	}
   1003   1.1    scottr 
   1004   1.1    scottr 	ifp->if_ipackets++;
   1005   1.1    scottr 
   1006   1.1    scottr #if NBPFILTER > 0
   1007   1.1    scottr 	/*
   1008   1.1    scottr 	 * Check if there's a BPF listener on this interface.
   1009   1.1    scottr 	 * If so, hand off the raw packet to bpf.
   1010   1.1    scottr 	 */
   1011  1.38   thorpej 	if (ifp->if_bpf)
   1012   1.1    scottr 		bpf_mtap(ifp->if_bpf, m);
   1013   1.1    scottr #endif
   1014   1.1    scottr 
   1015  1.24   thorpej 	(*ifp->if_input)(ifp, m);
   1016   1.1    scottr }
   1017   1.1    scottr 
   1018   1.1    scottr 
   1019   1.1    scottr /*
   1020   1.1    scottr  * Supporting routines.
   1021   1.1    scottr  */
   1022   1.1    scottr 
   1023   1.1    scottr /*
   1024   1.1    scottr  * Compute the multicast address filter from the list of multicast addresses we
   1025   1.1    scottr  * need to listen to.
   1026   1.1    scottr  */
   1027   1.1    scottr void
   1028   1.1    scottr dp8390_getmcaf(ec, af)
   1029   1.1    scottr 	struct ethercom *ec;
   1030   1.1    scottr 	u_int8_t *af;
   1031   1.1    scottr {
   1032   1.1    scottr 	struct ifnet *ifp = &ec->ec_if;
   1033   1.1    scottr 	struct ether_multi *enm;
   1034   1.1    scottr 	u_int32_t crc;
   1035  1.36   thorpej 	int i;
   1036   1.1    scottr 	struct ether_multistep step;
   1037   1.1    scottr 
   1038   1.1    scottr 	/*
   1039   1.1    scottr 	 * Set up multicast address filter by passing all multicast addresses
   1040   1.1    scottr 	 * through a crc generator, and then using the high order 6 bits as an
   1041   1.1    scottr 	 * index into the 64 bit logical address filter.  The high order bit
   1042   1.1    scottr 	 * selects the word, while the rest of the bits select the bit within
   1043   1.1    scottr 	 * the word.
   1044   1.1    scottr 	 */
   1045   1.1    scottr 
   1046   1.1    scottr 	if (ifp->if_flags & IFF_PROMISC) {
   1047   1.1    scottr 		ifp->if_flags |= IFF_ALLMULTI;
   1048   1.1    scottr 		for (i = 0; i < 8; i++)
   1049   1.1    scottr 			af[i] = 0xff;
   1050   1.1    scottr 		return;
   1051   1.1    scottr 	}
   1052   1.1    scottr 	for (i = 0; i < 8; i++)
   1053   1.1    scottr 		af[i] = 0;
   1054   1.1    scottr 	ETHER_FIRST_MULTI(step, ec, enm);
   1055   1.1    scottr 	while (enm != NULL) {
   1056  1.45   thorpej 		if (memcmp(enm->enm_addrlo, enm->enm_addrhi,
   1057   1.1    scottr 		    sizeof(enm->enm_addrlo)) != 0) {
   1058   1.1    scottr 			/*
   1059   1.1    scottr 			 * We must listen to a range of multicast addresses.
   1060   1.1    scottr 			 * For now, just accept all multicasts, rather than
   1061   1.1    scottr 			 * trying to set only those filter bits needed to match
   1062   1.1    scottr 			 * the range.  (At this time, the only use of address
   1063   1.1    scottr 			 * ranges is for IP multicast routing, for which the
   1064   1.1    scottr 			 * range is big enough to require all bits set.)
   1065   1.1    scottr 			 */
   1066   1.1    scottr 			ifp->if_flags |= IFF_ALLMULTI;
   1067   1.1    scottr 			for (i = 0; i < 8; i++)
   1068   1.1    scottr 				af[i] = 0xff;
   1069   1.1    scottr 			return;
   1070   1.1    scottr 		}
   1071  1.36   thorpej 
   1072  1.36   thorpej 		crc = ether_crc32_be(enm->enm_addrlo, ETHER_ADDR_LEN);
   1073  1.36   thorpej 
   1074   1.1    scottr 		/* Just want the 6 most significant bits. */
   1075   1.1    scottr 		crc >>= 26;
   1076   1.1    scottr 
   1077   1.1    scottr 		/* Turn on the corresponding bit in the filter. */
   1078   1.1    scottr 		af[crc >> 3] |= 1 << (crc & 0x7);
   1079   1.1    scottr 
   1080   1.1    scottr 		ETHER_NEXT_MULTI(step, enm);
   1081   1.1    scottr 	}
   1082   1.1    scottr 	ifp->if_flags &= ~IFF_ALLMULTI;
   1083   1.1    scottr }
   1084   1.1    scottr 
   1085   1.1    scottr /*
   1086  1.17       bad  * Copy data from receive buffer to a new mbuf chain allocating mbufs
   1087  1.17       bad  * as needed.  Return pointer to first mbuf in chain.
   1088   1.1    scottr  * sc = dp8390 info (softc)
   1089   1.1    scottr  * src = pointer in dp8390 ring buffer
   1090  1.17       bad  * total_len = amount of data to copy
   1091   1.1    scottr  */
   1092   1.1    scottr struct mbuf *
   1093   1.1    scottr dp8390_get(sc, src, total_len)
   1094   1.1    scottr 	struct dp8390_softc *sc;
   1095   1.1    scottr 	int src;
   1096   1.1    scottr 	u_short total_len;
   1097   1.1    scottr {
   1098   1.1    scottr 	struct ifnet *ifp = &sc->sc_ec.ec_if;
   1099  1.18       bad 	struct mbuf *m, *m0, *newm;
   1100   1.1    scottr 	u_short len;
   1101   1.1    scottr 
   1102  1.19   mycroft 	MGETHDR(m0, M_DONTWAIT, MT_DATA);
   1103  1.19   mycroft 	if (m0 == 0)
   1104  1.19   mycroft 		return (0);
   1105  1.19   mycroft 	m0->m_pkthdr.rcvif = ifp;
   1106  1.19   mycroft 	m0->m_pkthdr.len = total_len;
   1107   1.1    scottr 	len = MHLEN;
   1108  1.19   mycroft 	m = m0;
   1109   1.1    scottr 
   1110   1.1    scottr 	while (total_len > 0) {
   1111   1.1    scottr 		if (total_len >= MINCLSIZE) {
   1112   1.1    scottr 			MCLGET(m, M_DONTWAIT);
   1113  1.19   mycroft 			if ((m->m_flags & M_EXT) == 0)
   1114  1.19   mycroft 				goto bad;
   1115   1.1    scottr 			len = MCLBYTES;
   1116   1.1    scottr 		}
   1117  1.11   thorpej 
   1118  1.11   thorpej 		/*
   1119  1.11   thorpej 		 * Make sure the data after the Ethernet header is aligned.
   1120  1.11   thorpej 		 */
   1121  1.18       bad 		if (m == m0) {
   1122  1.11   thorpej 			caddr_t newdata = (caddr_t)
   1123  1.11   thorpej 			    ALIGN(m->m_data + sizeof(struct ether_header)) -
   1124  1.11   thorpej 			    sizeof(struct ether_header);
   1125  1.11   thorpej 			len -= newdata - m->m_data;
   1126  1.11   thorpej 			m->m_data = newdata;
   1127  1.11   thorpej 		}
   1128  1.11   thorpej 
   1129   1.1    scottr 		m->m_len = len = min(total_len, len);
   1130   1.1    scottr 		if (sc->ring_copy)
   1131   1.1    scottr 			src = (*sc->ring_copy)(sc, src, mtod(m, caddr_t), len);
   1132   1.1    scottr 		else
   1133   1.1    scottr 			src = dp8390_ring_copy(sc, src, mtod(m, caddr_t), len);
   1134  1.19   mycroft 
   1135   1.1    scottr 		total_len -= len;
   1136  1.18       bad 		if (total_len > 0) {
   1137  1.18       bad 			MGET(newm, M_DONTWAIT, MT_DATA);
   1138  1.19   mycroft 			if (newm == 0)
   1139  1.19   mycroft 				goto bad;
   1140  1.18       bad 			len = MLEN;
   1141  1.19   mycroft 			m = m->m_next = newm;
   1142  1.18       bad 		}
   1143   1.1    scottr 	}
   1144   1.1    scottr 
   1145  1.19   mycroft 	return (m0);
   1146  1.19   mycroft 
   1147  1.19   mycroft bad:
   1148  1.19   mycroft 	m_freem(m0);
   1149  1.19   mycroft 	return (0);
   1150   1.1    scottr }
   1151   1.1    scottr 
   1152   1.1    scottr 
   1153   1.1    scottr /*
   1154   1.1    scottr  * Default driver support functions.
   1155   1.1    scottr  *
   1156   1.1    scottr  * NOTE: all support functions assume 8-bit shared memory.
   1157   1.1    scottr  */
   1158   1.1    scottr /*
   1159   1.1    scottr  * Zero NIC buffer memory and verify that it is clear.
   1160   1.1    scottr  */
   1161   1.1    scottr static int
   1162   1.1    scottr dp8390_test_mem(sc)
   1163   1.1    scottr 	struct dp8390_softc *sc;
   1164   1.1    scottr {
   1165   1.1    scottr 	bus_space_tag_t buft = sc->sc_buft;
   1166   1.1    scottr 	bus_space_handle_t bufh = sc->sc_bufh;
   1167   1.1    scottr 	int i;
   1168   1.1    scottr 
   1169   1.1    scottr 	bus_space_set_region_1(buft, bufh, sc->mem_start, 0, sc->mem_size);
   1170   1.1    scottr 
   1171   1.1    scottr 	for (i = 0; i < sc->mem_size; ++i) {
   1172   1.1    scottr 		if (bus_space_read_1(buft, bufh, sc->mem_start + i)) {
   1173   1.1    scottr 			printf(": failed to clear NIC buffer at offset %x - "
   1174   1.1    scottr 			    "check configuration\n", (sc->mem_start + i));
   1175   1.1    scottr 			return 1;
   1176   1.1    scottr 		}
   1177   1.1    scottr 	}
   1178   1.1    scottr 
   1179   1.1    scottr 	return 0;
   1180   1.1    scottr }
   1181   1.1    scottr 
   1182   1.1    scottr /*
   1183   1.1    scottr  * Read a packet header from the ring, given the source offset.
   1184   1.1    scottr  */
   1185   1.1    scottr static __inline__ void
   1186   1.1    scottr dp8390_read_hdr(sc, src, hdrp)
   1187   1.1    scottr 	struct dp8390_softc *sc;
   1188   1.1    scottr 	int src;
   1189   1.1    scottr 	struct dp8390_ring *hdrp;
   1190   1.1    scottr {
   1191   1.1    scottr 	bus_space_tag_t buft = sc->sc_buft;
   1192   1.1    scottr 	bus_space_handle_t bufh = sc->sc_bufh;
   1193   1.1    scottr 
   1194   1.1    scottr 	/*
   1195   1.1    scottr 	 * The byte count includes a 4 byte header that was added by
   1196   1.1    scottr 	 * the NIC.
   1197   1.1    scottr 	 */
   1198   1.1    scottr 	hdrp->rsr = bus_space_read_1(buft, bufh, src);
   1199   1.1    scottr 	hdrp->next_packet = bus_space_read_1(buft, bufh, src + 1);
   1200   1.1    scottr 	hdrp->count = bus_space_read_1(buft, bufh, src + 2) |
   1201   1.1    scottr 	    (bus_space_read_1(buft, bufh, src + 3) << 8);
   1202   1.1    scottr }
   1203   1.1    scottr 
   1204   1.1    scottr /*
   1205   1.1    scottr  * Copy `amount' bytes from a packet in the ring buffer to a linear
   1206   1.1    scottr  * destination buffer, given a source offset and destination address.
   1207   1.1    scottr  * Takes into account ring-wrap.
   1208   1.1    scottr  */
   1209   1.1    scottr static __inline__ int
   1210   1.1    scottr dp8390_ring_copy(sc, src, dst, amount)
   1211   1.1    scottr 	struct dp8390_softc *sc;
   1212   1.1    scottr 	int src;
   1213   1.1    scottr 	caddr_t dst;
   1214   1.1    scottr 	u_short amount;
   1215   1.1    scottr {
   1216   1.1    scottr 	bus_space_tag_t buft = sc->sc_buft;
   1217   1.1    scottr 	bus_space_handle_t bufh = sc->sc_bufh;
   1218   1.1    scottr 	u_short tmp_amount;
   1219   1.1    scottr 
   1220   1.1    scottr 	/* Does copy wrap to lower addr in ring buffer? */
   1221   1.1    scottr 	if (src + amount > sc->mem_end) {
   1222   1.1    scottr 		tmp_amount = sc->mem_end - src;
   1223   1.1    scottr 
   1224   1.1    scottr 		/* Copy amount up to end of NIC memory. */
   1225   1.1    scottr 		bus_space_read_region_1(buft, bufh, src, dst, tmp_amount);
   1226   1.1    scottr 
   1227   1.1    scottr 		amount -= tmp_amount;
   1228   1.1    scottr 		src = sc->mem_ring;
   1229   1.1    scottr 		dst += tmp_amount;
   1230   1.1    scottr 	}
   1231   1.1    scottr 	bus_space_read_region_1(buft, bufh, src, dst, amount);
   1232   1.1    scottr 
   1233   1.1    scottr 	return (src + amount);
   1234   1.1    scottr }
   1235   1.1    scottr 
   1236   1.1    scottr /*
   1237   1.1    scottr  * Copy a packet from an mbuf to the transmit buffer on the card.
   1238   1.1    scottr  *
   1239   1.1    scottr  * Currently uses an extra buffer/extra memory copy, unless the whole
   1240   1.1    scottr  * packet fits in one mbuf.
   1241   1.1    scottr  */
   1242   1.1    scottr static __inline__ int
   1243   1.1    scottr dp8390_write_mbuf(sc, m, buf)
   1244   1.1    scottr 	struct dp8390_softc *sc;
   1245   1.1    scottr 	struct mbuf *m;
   1246   1.1    scottr 	int buf;
   1247   1.1    scottr {
   1248   1.1    scottr 	bus_space_tag_t buft = sc->sc_buft;
   1249   1.1    scottr 	bus_space_handle_t bufh = sc->sc_bufh;
   1250   1.1    scottr 	u_char *data;
   1251   1.1    scottr 	int len, totlen = 0;
   1252   1.1    scottr 
   1253   1.1    scottr 	for (; m ; m = m->m_next) {
   1254   1.1    scottr 		data = mtod(m, u_char *);
   1255   1.1    scottr 		len = m->m_len;
   1256   1.1    scottr 		if (len > 0) {
   1257   1.9    scottr 			bus_space_write_region_1(buft, bufh, buf, data, len);
   1258   1.1    scottr 			totlen += len;
   1259   1.9    scottr 			buf += len;
   1260   1.1    scottr 		}
   1261   1.1    scottr 	}
   1262  1.52    bouyer 	if (totlen < ETHER_MIN_LEN - ETHER_CRC_LEN) {
   1263  1.52    bouyer 		bus_space_set_region_1(buft, bufh, buf, 0,
   1264  1.52    bouyer 		    ETHER_MIN_LEN - ETHER_CRC_LEN - totlen);
   1265  1.52    bouyer 		totlen = ETHER_MIN_LEN - ETHER_CRC_LEN;
   1266  1.52    bouyer 	}
   1267   1.1    scottr 	return (totlen);
   1268   1.7   thorpej }
   1269   1.7   thorpej 
   1270   1.7   thorpej /*
   1271   1.7   thorpej  * Enable power on the interface.
   1272   1.7   thorpej  */
   1273   1.7   thorpej int
   1274   1.7   thorpej dp8390_enable(sc)
   1275   1.7   thorpej 	struct dp8390_softc *sc;
   1276   1.7   thorpej {
   1277   1.7   thorpej 
   1278   1.7   thorpej 	if (sc->sc_enabled == 0 && sc->sc_enable != NULL) {
   1279   1.7   thorpej 		if ((*sc->sc_enable)(sc) != 0) {
   1280   1.7   thorpej 			printf("%s: device enable failed\n",
   1281   1.7   thorpej 			    sc->sc_dev.dv_xname);
   1282   1.7   thorpej 			return (EIO);
   1283   1.7   thorpej 		}
   1284   1.7   thorpej 	}
   1285   1.7   thorpej 
   1286   1.7   thorpej 	sc->sc_enabled = 1;
   1287   1.7   thorpej 	return (0);
   1288   1.7   thorpej }
   1289   1.7   thorpej 
   1290   1.7   thorpej /*
   1291   1.7   thorpej  * Disable power on the interface.
   1292   1.7   thorpej  */
   1293   1.7   thorpej void
   1294   1.7   thorpej dp8390_disable(sc)
   1295   1.7   thorpej 	struct dp8390_softc *sc;
   1296   1.7   thorpej {
   1297   1.7   thorpej 
   1298   1.7   thorpej 	if (sc->sc_enabled != 0 && sc->sc_disable != NULL) {
   1299   1.7   thorpej 		(*sc->sc_disable)(sc);
   1300   1.7   thorpej 		sc->sc_enabled = 0;
   1301   1.7   thorpej 	}
   1302  1.16   thorpej }
   1303  1.16   thorpej 
   1304  1.16   thorpej int
   1305  1.16   thorpej dp8390_activate(self, act)
   1306  1.16   thorpej 	struct device *self;
   1307  1.16   thorpej 	enum devact act;
   1308  1.16   thorpej {
   1309  1.16   thorpej 	struct dp8390_softc *sc = (struct dp8390_softc *)self;
   1310  1.16   thorpej 	int rv = 0, s;
   1311  1.16   thorpej 
   1312  1.16   thorpej 	s = splnet();
   1313  1.16   thorpej 	switch (act) {
   1314  1.16   thorpej 	case DVACT_ACTIVATE:
   1315  1.16   thorpej 		rv = EOPNOTSUPP;
   1316  1.16   thorpej 		break;
   1317  1.16   thorpej 
   1318  1.16   thorpej 	case DVACT_DEACTIVATE:
   1319  1.28    itojun 		if_deactivate(&sc->sc_ec.ec_if);
   1320  1.16   thorpej 		break;
   1321  1.16   thorpej 	}
   1322  1.16   thorpej 	splx(s);
   1323  1.16   thorpej 	return (rv);
   1324  1.28    itojun }
   1325  1.28    itojun 
   1326  1.28    itojun int
   1327  1.30    itojun dp8390_detach(sc, flags)
   1328  1.28    itojun 	struct dp8390_softc *sc;
   1329  1.30    itojun 	int flags;
   1330  1.28    itojun {
   1331  1.28    itojun 	struct ifnet *ifp = &sc->sc_ec.ec_if;
   1332  1.37     jhawk 
   1333  1.37     jhawk 	/* Succeed now if there's no work to do. */
   1334  1.37     jhawk 	if ((sc->sc_flags & DP8390_ATTACHED) == 0)
   1335  1.37     jhawk 		return (0);
   1336  1.28    itojun 
   1337  1.31    itojun 	/* dp8390_disable() checks sc->sc_enabled */
   1338  1.28    itojun 	dp8390_disable(sc);
   1339  1.29     enami 
   1340  1.42   thorpej 	if (sc->sc_media_fini != NULL)
   1341  1.42   thorpej 		(*sc->sc_media_fini)(sc);
   1342  1.42   thorpej 
   1343  1.42   thorpej 	/* Delete all remaining media. */
   1344  1.32     enami 	ifmedia_delete_instance(&sc->sc_media, IFM_INST_ANY);
   1345  1.28    itojun 
   1346  1.28    itojun #if NRND > 0
   1347  1.32     enami 	rnd_detach_source(&sc->rnd_source);
   1348  1.28    itojun #endif
   1349  1.32     enami 	ether_ifdetach(ifp);
   1350  1.32     enami 	if_detach(ifp);
   1351  1.28    itojun 
   1352  1.28    itojun 	return (0);
   1353   1.1    scottr }
   1354  1.35        ws 
   1355  1.35        ws #ifdef IPKDB_DP8390
   1356  1.35        ws static void dp8390_ipkdb_hwinit __P((struct ipkdb_if *));
   1357  1.35        ws static void dp8390_ipkdb_init __P((struct ipkdb_if *));
   1358  1.35        ws static void dp8390_ipkdb_leave __P((struct ipkdb_if *));
   1359  1.35        ws static int dp8390_ipkdb_rcv __P((struct ipkdb_if *, u_char *, int));
   1360  1.35        ws static void dp8390_ipkdb_send __P((struct ipkdb_if *, u_char *, int));
   1361  1.35        ws 
   1362  1.35        ws /*
   1363  1.35        ws  * This is essentially similar to dp8390_config above.
   1364  1.35        ws  */
   1365  1.35        ws int
   1366  1.35        ws dp8390_ipkdb_attach(kip)
   1367  1.35        ws 	struct ipkdb_if *kip;
   1368  1.35        ws {
   1369  1.35        ws 	struct dp8390_softc *sc = kip->port;
   1370  1.35        ws 
   1371  1.35        ws 	if (sc->mem_size < 8192 * 2)
   1372  1.35        ws 		sc->txb_cnt = 1;
   1373  1.35        ws 	else if (sc->mem_size < 8192 * 3)
   1374  1.35        ws 		sc->txb_cnt = 2;
   1375  1.35        ws 	else
   1376  1.35        ws 		sc->txb_cnt = 3;
   1377  1.35        ws 
   1378  1.35        ws 	sc->tx_page_start = sc->mem_start >> ED_PAGE_SHIFT;
   1379  1.35        ws 	sc->rec_page_start = sc->tx_page_start + sc->txb_cnt * ED_TXBUF_SIZE;
   1380  1.35        ws 	sc->rec_page_stop = sc->tx_page_start + (sc->mem_size >> ED_PAGE_SHIFT);
   1381  1.35        ws 	sc->mem_ring = sc->mem_start + (sc->rec_page_start << ED_PAGE_SHIFT);
   1382  1.35        ws 	sc->mem_end = sc->mem_start + sc->mem_size;
   1383  1.35        ws 
   1384  1.35        ws 	dp8390_stop(sc);
   1385  1.35        ws 
   1386  1.35        ws 	kip->start = dp8390_ipkdb_init;
   1387  1.35        ws 	kip->leave = dp8390_ipkdb_leave;
   1388  1.35        ws 	kip->receive = dp8390_ipkdb_rcv;
   1389  1.35        ws 	kip->send = dp8390_ipkdb_send;
   1390  1.35        ws 
   1391  1.35        ws 	return 0;
   1392  1.35        ws }
   1393  1.35        ws 
   1394  1.35        ws /*
   1395  1.35        ws  * Similar to dp8390_init above.
   1396  1.35        ws  */
   1397  1.35        ws static void
   1398  1.35        ws dp8390_ipkdb_hwinit(kip)
   1399  1.35        ws 	struct ipkdb_if *kip;
   1400  1.35        ws {
   1401  1.35        ws 	struct dp8390_softc *sc = kip->port;
   1402  1.35        ws 	struct ifnet *ifp = &sc->sc_ec.ec_if;
   1403  1.35        ws 	bus_space_tag_t regt = sc->sc_regt;
   1404  1.35        ws 	bus_space_handle_t regh = sc->sc_regh;
   1405  1.35        ws 	int i;
   1406  1.35        ws 
   1407  1.35        ws 	sc->txb_inuse = 0;
   1408  1.35        ws 	sc->txb_new = 0;
   1409  1.35        ws 	sc->txb_next_tx = 0;
   1410  1.35        ws 	dp8390_stop(sc);
   1411  1.35        ws 
   1412  1.35        ws 	if (sc->dcr_reg & ED_DCR_LS)
   1413  1.35        ws 		NIC_PUT(regt, regh, ED_P0_DCR, sc->dcr_reg);
   1414  1.35        ws 	else
   1415  1.35        ws 		NIC_PUT(regt, regh, ED_P0_DCR, ED_DCR_FT1 | ED_DCR_LS);
   1416  1.35        ws 	NIC_PUT(regt, regh, ED_P0_RBCR0, 0);
   1417  1.35        ws 	NIC_PUT(regt, regh, ED_P0_RBCR1, 0);
   1418  1.35        ws 	NIC_PUT(regt, regh, ED_P0_RCR, ED_RCR_MON | sc->rcr_proto);
   1419  1.35        ws 	NIC_PUT(regt, regh, ED_P0_TCR, ED_TCR_LB0);
   1420  1.35        ws 	if (sc->is790)
   1421  1.35        ws 		NIC_PUT(regt, regh, 0x09, 0);
   1422  1.35        ws 	NIC_PUT(regt, regh, ED_P0_BNRY, sc->rec_page_start);
   1423  1.35        ws 	NIC_PUT(regt, regh, ED_P0_PSTART, sc->rec_page_start);
   1424  1.35        ws 	NIC_PUT(regt, regh, ED_P0_PSTOP, sc->rec_page_stop);
   1425  1.35        ws 	NIC_PUT(regt, regh, ED_P0_IMR, 0);
   1426  1.35        ws 	NIC_BARRIER(regt, regh);
   1427  1.35        ws 	NIC_PUT(regt, regh, ED_P0_ISR, 0xff);
   1428  1.35        ws 
   1429  1.35        ws 	NIC_BARRIER(regt, regh);
   1430  1.35        ws 	NIC_PUT(regt, regh, ED_P0_CR,
   1431  1.35        ws 		sc->cr_proto | ED_CR_PAGE_1 | ED_CR_STP);
   1432  1.35        ws 	NIC_BARRIER(regt, regh);
   1433  1.35        ws 
   1434  1.35        ws 	for (i = 0; i < sizeof kip->myenetaddr; i++)
   1435  1.35        ws 		NIC_PUT(regt, regh, ED_P1_PAR0 + i, kip->myenetaddr[i]);
   1436  1.35        ws 	/* multicast filter? */
   1437  1.35        ws 
   1438  1.35        ws 	sc->next_packet = sc->rec_page_start + 1;
   1439  1.35        ws 	NIC_PUT(regt, regh, ED_P1_CURR, sc->next_packet);
   1440  1.35        ws 
   1441  1.35        ws 	NIC_BARRIER(regt, regh);
   1442  1.35        ws 	NIC_PUT(regt, regh, ED_P1_CR,
   1443  1.35        ws 		sc->cr_proto | ED_CR_PAGE_0 | ED_CR_STP);
   1444  1.35        ws 	NIC_BARRIER(regt, regh);
   1445  1.35        ws 
   1446  1.35        ws 	/* promiscuous mode? */
   1447  1.35        ws 	NIC_PUT(regt, regh, ED_P0_RCR, ED_RCR_AB | ED_RCR_AM | sc->rcr_proto);
   1448  1.35        ws 	NIC_PUT(regt, regh, ED_P0_TCR, 0);
   1449  1.35        ws 
   1450  1.35        ws 	/* card-specific initialization? */
   1451  1.35        ws 
   1452  1.35        ws 	NIC_BARRIER(regt, regh);
   1453  1.35        ws 	NIC_PUT(regt, regh, ED_P0_CR,
   1454  1.35        ws 		sc->cr_proto | ED_CR_PAGE_0 | ED_CR_STA);
   1455  1.35        ws 
   1456  1.35        ws 	ifp->if_flags &= ~IFF_OACTIVE;
   1457  1.35        ws }
   1458  1.35        ws 
   1459  1.35        ws static void
   1460  1.35        ws dp8390_ipkdb_init(kip)
   1461  1.35        ws 	struct ipkdb_if *kip;
   1462  1.35        ws {
   1463  1.35        ws 	struct dp8390_softc *sc = kip->port;
   1464  1.35        ws 	bus_space_tag_t regt = sc->sc_regt;
   1465  1.35        ws 	bus_space_handle_t regh = sc->sc_regh;
   1466  1.35        ws 	u_char cmd;
   1467  1.35        ws 
   1468  1.35        ws 	cmd = NIC_GET(regt, regh, ED_P0_CR) & ~(ED_CR_PAGE_3 | ED_CR_STA);
   1469  1.35        ws 
   1470  1.35        ws 	/* Select page 0 */
   1471  1.35        ws 	NIC_BARRIER(regt, regh);
   1472  1.35        ws 	NIC_PUT(regt, regh, ED_P0_CR, cmd | ED_CR_PAGE_0 | ED_CR_STP);
   1473  1.35        ws 	NIC_BARRIER(regt, regh);
   1474  1.35        ws 
   1475  1.35        ws 	/* If not started, init chip */
   1476  1.35        ws 	if (cmd & ED_CR_STP)
   1477  1.35        ws 		dp8390_ipkdb_hwinit(kip);
   1478  1.35        ws 
   1479  1.35        ws 	/* If output active, wait for packets to drain */
   1480  1.35        ws 	while (sc->txb_inuse) {
   1481  1.35        ws 		while (!(cmd = (NIC_GET(regt, regh, ED_P0_ISR)
   1482  1.35        ws 				& (ED_ISR_PTX | ED_ISR_TXE))))
   1483  1.35        ws 			DELAY(1);
   1484  1.35        ws 		NIC_PUT(regt, regh, ED_P0_ISR, cmd);
   1485  1.35        ws 		if (--sc->txb_inuse)
   1486  1.35        ws 			dp8390_xmit(sc);
   1487  1.35        ws 	}
   1488  1.35        ws }
   1489  1.35        ws 
   1490  1.35        ws static void
   1491  1.35        ws dp8390_ipkdb_leave(kip)
   1492  1.35        ws 	struct ipkdb_if *kip;
   1493  1.35        ws {
   1494  1.35        ws 	struct dp8390_softc *sc = kip->port;
   1495  1.35        ws 	struct ifnet *ifp = &sc->sc_ec.ec_if;
   1496  1.35        ws 
   1497  1.35        ws 	ifp->if_timer = 0;
   1498  1.35        ws }
   1499  1.35        ws 
   1500  1.35        ws /*
   1501  1.35        ws  * Similar to dp8390_intr above.
   1502  1.35        ws  */
   1503  1.35        ws static int
   1504  1.35        ws dp8390_ipkdb_rcv(kip, buf, poll)
   1505  1.35        ws 	struct ipkdb_if *kip;
   1506  1.35        ws 	u_char *buf;
   1507  1.35        ws 	int poll;
   1508  1.35        ws {
   1509  1.35        ws 	struct dp8390_softc *sc = kip->port;
   1510  1.35        ws 	bus_space_tag_t regt = sc->sc_regt;
   1511  1.35        ws 	bus_space_handle_t regh = sc->sc_regh;
   1512  1.35        ws 	u_char bnry, current, isr;
   1513  1.35        ws 	int len, nlen, packet_ptr;
   1514  1.35        ws 	struct dp8390_ring packet_hdr;
   1515  1.35        ws 
   1516  1.35        ws 	/* Switch to page 0. */
   1517  1.35        ws 	NIC_BARRIER(regt, regh);
   1518  1.35        ws 	NIC_PUT(regt, regh, ED_P0_CR,
   1519  1.35        ws 		sc->cr_proto | ED_CR_PAGE_0 | ED_CR_STA);
   1520  1.35        ws 	NIC_BARRIER(regt, regh);
   1521  1.35        ws 
   1522  1.35        ws 	while (1) {
   1523  1.35        ws 		isr = NIC_GET(regt, regh, ED_P0_ISR);
   1524  1.35        ws 		NIC_PUT(regt, regh, ED_P0_ISR, isr);
   1525  1.35        ws 
   1526  1.35        ws 		if (isr & (ED_ISR_PRX | ED_ISR_TXE)) {
   1527  1.35        ws 			NIC_GET(regt, regh, ED_P0_NCR);
   1528  1.35        ws 			NIC_GET(regt, regh, ED_P0_TSR);
   1529  1.35        ws 		}
   1530  1.35        ws 
   1531  1.35        ws 		if (isr & ED_ISR_OVW) {
   1532  1.35        ws 			dp8390_ipkdb_hwinit(kip);
   1533  1.35        ws 			continue;
   1534  1.35        ws 		}
   1535  1.35        ws 
   1536  1.35        ws 		if (isr & ED_ISR_CNT) {
   1537  1.35        ws 			NIC_GET(regt, regh, ED_P0_CNTR0);
   1538  1.35        ws 			NIC_GET(regt, regh, ED_P0_CNTR1);
   1539  1.35        ws 			NIC_GET(regt, regh, ED_P0_CNTR2);
   1540  1.35        ws 		}
   1541  1.35        ws 
   1542  1.35        ws 		/* Similar to dp8390_rint above. */
   1543  1.35        ws 		NIC_BARRIER(regt, regh);
   1544  1.35        ws 		NIC_PUT(regt, regh, ED_P0_CR,
   1545  1.35        ws 			sc->cr_proto | ED_CR_PAGE_1 | ED_CR_STA);
   1546  1.35        ws 		NIC_BARRIER(regt, regh);
   1547  1.35        ws 
   1548  1.35        ws 		current = NIC_GET(regt, regh, ED_P1_CURR);
   1549  1.35        ws 
   1550  1.35        ws 		NIC_BARRIER(regt, regh);
   1551  1.35        ws 		NIC_PUT(regt, regh, ED_P1_CR,
   1552  1.35        ws 			sc->cr_proto | ED_CR_PAGE_0 | ED_CR_STA);
   1553  1.35        ws 		NIC_BARRIER(regt, regh);
   1554  1.35        ws 
   1555  1.35        ws 		if (sc->next_packet == current) {
   1556  1.35        ws 			if (poll)
   1557  1.35        ws 				return 0;
   1558  1.35        ws 			continue;
   1559  1.35        ws 		}
   1560  1.35        ws 
   1561  1.35        ws 		packet_ptr = sc->mem_ring
   1562  1.35        ws 			+ ((sc->next_packet - sc->rec_page_start) << ED_PAGE_SHIFT);
   1563  1.35        ws 		sc->read_hdr(sc, packet_ptr, &packet_hdr);
   1564  1.35        ws 		len = packet_hdr.count;
   1565  1.35        ws 		nlen = packet_hdr.next_packet - sc->next_packet;
   1566  1.35        ws 		if (nlen < 0)
   1567  1.35        ws 			nlen += sc->rec_page_stop - sc->rec_page_start;
   1568  1.35        ws 		nlen--;
   1569  1.35        ws 		if ((len & ED_PAGE_MASK) + sizeof(packet_hdr) > ED_PAGE_SIZE)
   1570  1.35        ws 			nlen--;
   1571  1.35        ws 		len = (len & ED_PAGE_MASK) | (nlen << ED_PAGE_SHIFT);
   1572  1.35        ws 		len -= sizeof(packet_hdr);
   1573  1.35        ws 
   1574  1.35        ws 		if (len <= ETHERMTU
   1575  1.35        ws 		    && packet_hdr.next_packet >= sc->rec_page_start
   1576  1.35        ws 		    && packet_hdr.next_packet < sc->rec_page_stop) {
   1577  1.35        ws 			sc->ring_copy(sc, packet_ptr + sizeof(packet_hdr),
   1578  1.35        ws 				buf, len);
   1579  1.35        ws 			sc->next_packet = packet_hdr.next_packet;
   1580  1.35        ws 			bnry = sc->next_packet - 1;
   1581  1.35        ws 			if (bnry < sc->rec_page_start)
   1582  1.35        ws 				bnry = sc->rec_page_stop - 1;
   1583  1.35        ws 			NIC_PUT(regt, regh, ED_P0_BNRY, bnry);
   1584  1.35        ws 			return len;
   1585  1.35        ws 		}
   1586  1.35        ws 
   1587  1.35        ws 		dp8390_ipkdb_hwinit(kip);
   1588  1.35        ws 	}
   1589  1.35        ws }
   1590  1.35        ws 
   1591  1.35        ws static void
   1592  1.35        ws dp8390_ipkdb_send(kip, buf, l)
   1593  1.35        ws 	struct ipkdb_if *kip;
   1594  1.35        ws 	u_char *buf;
   1595  1.35        ws 	int l;
   1596  1.35        ws {
   1597  1.35        ws 	struct dp8390_softc *sc = kip->port;
   1598  1.35        ws 	bus_space_tag_t regt = sc->sc_regt;
   1599  1.35        ws 	bus_space_handle_t regh = sc->sc_regh;
   1600  1.35        ws 	struct mbuf mb;
   1601  1.35        ws 
   1602  1.35        ws 	mb.m_next = NULL;
   1603  1.35        ws 	mb.m_pkthdr.len = mb.m_len = l;
   1604  1.35        ws 	mtod(&mb, u_char *) = buf;
   1605  1.47   thorpej 	mb.m_flags = M_EXT | M_PKTHDR;
   1606  1.35        ws 	mb.m_type = MT_DATA;
   1607  1.35        ws 
   1608  1.35        ws 	l = sc->write_mbuf(sc, &mb,
   1609  1.35        ws 	    sc->mem_start + ((sc->txb_new * ED_TXBUF_SIZE) << ED_PAGE_SHIFT));
   1610  1.35        ws 	sc->txb_len[sc->txb_new] = max(l, ETHER_MIN_LEN - ETHER_CRC_LEN);
   1611  1.35        ws 
   1612  1.35        ws 	if (++sc->txb_new == sc->txb_cnt)
   1613  1.35        ws 		sc->txb_new = 0;
   1614  1.35        ws 
   1615  1.35        ws 	sc->txb_inuse++;
   1616  1.35        ws 	dp8390_xmit(sc);
   1617  1.35        ws 
   1618  1.35        ws 	while (!(NIC_GET(regt, regh, ED_P0_ISR) & (ED_ISR_PTX | ED_ISR_TXE)))
   1619  1.35        ws 		DELAY(1);
   1620  1.35        ws 
   1621  1.35        ws 	sc->txb_inuse--;
   1622  1.35        ws }
   1623  1.35        ws #endif
   1624