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