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