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