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