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