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