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