Home | History | Annotate | Line # | Download | only in ic
dp83932.c revision 1.17
      1 /*	$NetBSD: dp83932.c,v 1.17 2007/06/08 17:08:46 tsutsui Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2001 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Jason R. Thorpe.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *	This product includes software developed by the NetBSD
     21  *	Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 /*
     40  * Device driver for the National Semiconductor DP83932
     41  * Systems-Oriented Network Interface Controller (SONIC).
     42  */
     43 
     44 #include <sys/cdefs.h>
     45 __KERNEL_RCSID(0, "$NetBSD: dp83932.c,v 1.17 2007/06/08 17:08:46 tsutsui Exp $");
     46 
     47 #include "bpfilter.h"
     48 
     49 #include <sys/param.h>
     50 #include <sys/systm.h>
     51 #include <sys/mbuf.h>
     52 #include <sys/malloc.h>
     53 #include <sys/kernel.h>
     54 #include <sys/socket.h>
     55 #include <sys/ioctl.h>
     56 #include <sys/errno.h>
     57 #include <sys/device.h>
     58 
     59 #include <uvm/uvm_extern.h>
     60 
     61 #include <net/if.h>
     62 #include <net/if_dl.h>
     63 #include <net/if_ether.h>
     64 
     65 #if NBPFILTER > 0
     66 #include <net/bpf.h>
     67 #endif
     68 
     69 #include <machine/bus.h>
     70 #include <machine/intr.h>
     71 
     72 #include <dev/ic/dp83932reg.h>
     73 #include <dev/ic/dp83932var.h>
     74 
     75 void	sonic_start(struct ifnet *);
     76 void	sonic_watchdog(struct ifnet *);
     77 int	sonic_ioctl(struct ifnet *, u_long, void *);
     78 int	sonic_init(struct ifnet *);
     79 void	sonic_stop(struct ifnet *, int);
     80 
     81 void	sonic_shutdown(void *);
     82 
     83 void	sonic_reset(struct sonic_softc *);
     84 void	sonic_rxdrain(struct sonic_softc *);
     85 int	sonic_add_rxbuf(struct sonic_softc *, int);
     86 void	sonic_set_filter(struct sonic_softc *);
     87 
     88 uint16_t sonic_txintr(struct sonic_softc *);
     89 void	sonic_rxintr(struct sonic_softc *);
     90 
     91 int	sonic_copy_small = 0;
     92 
     93 #define ETHER_PAD_LEN (ETHER_MIN_LEN - ETHER_CRC_LEN)
     94 
     95 /*
     96  * sonic_attach:
     97  *
     98  *	Attach a SONIC interface to the system.
     99  */
    100 void
    101 sonic_attach(struct sonic_softc *sc, const uint8_t *enaddr)
    102 {
    103 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    104 	int i, rseg, error;
    105 	bus_dma_segment_t seg;
    106 	size_t cdatasize;
    107 	char *nullbuf;
    108 
    109 	/*
    110 	 * Allocate the control data structures, and create and load the
    111 	 * DMA map for it.
    112 	 */
    113 	if (sc->sc_32bit)
    114 		cdatasize = sizeof(struct sonic_control_data32);
    115 	else
    116 		cdatasize = sizeof(struct sonic_control_data16);
    117 
    118 	if ((error = bus_dmamem_alloc(sc->sc_dmat, cdatasize + ETHER_PAD_LEN,
    119 	     PAGE_SIZE, (64 * 1024), &seg, 1, &rseg,
    120 	     BUS_DMA_NOWAIT)) != 0) {
    121 		printf("%s: unable to allocate control data, error = %d\n",
    122 		    sc->sc_dev.dv_xname, error);
    123 		goto fail_0;
    124 	}
    125 
    126 	if ((error = bus_dmamem_map(sc->sc_dmat, &seg, rseg,
    127 	    cdatasize + ETHER_PAD_LEN, (void **) &sc->sc_cdata16,
    128 	    BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) != 0) {
    129 		printf("%s: unable to map control data, error = %d\n",
    130 		    sc->sc_dev.dv_xname, error);
    131 		goto fail_1;
    132 	}
    133 	nullbuf = (char *)sc->sc_cdata16 + cdatasize;
    134 	memset(nullbuf, 0, ETHER_PAD_LEN);
    135 
    136 	if ((error = bus_dmamap_create(sc->sc_dmat,
    137 	     cdatasize, 1, cdatasize, 0, BUS_DMA_NOWAIT,
    138 	     &sc->sc_cddmamap)) != 0) {
    139 		printf("%s: unable to create control data DMA map, "
    140 		    "error = %d\n", sc->sc_dev.dv_xname, error);
    141 		goto fail_2;
    142 	}
    143 
    144 	if ((error = bus_dmamap_load(sc->sc_dmat, sc->sc_cddmamap,
    145 	     sc->sc_cdata16, cdatasize, NULL, BUS_DMA_NOWAIT)) != 0) {
    146 		printf("%s: unable to load control data DMA map, error = %d\n",
    147 		    sc->sc_dev.dv_xname, error);
    148 		goto fail_3;
    149 	}
    150 
    151 	/*
    152 	 * Create the transmit buffer DMA maps.
    153 	 */
    154 	for (i = 0; i < SONIC_NTXDESC; i++) {
    155 		if ((error = bus_dmamap_create(sc->sc_dmat, MCLBYTES,
    156 		     SONIC_NTXFRAGS, MCLBYTES, 0, BUS_DMA_NOWAIT,
    157 		     &sc->sc_txsoft[i].ds_dmamap)) != 0) {
    158 			printf("%s: unable to create tx DMA map %d, "
    159 			    "error = %d\n", sc->sc_dev.dv_xname, i, error);
    160 			goto fail_4;
    161 		}
    162 	}
    163 
    164 	/*
    165 	 * Create the receive buffer DMA maps.
    166 	 */
    167 	for (i = 0; i < SONIC_NRXDESC; i++) {
    168 		if ((error = bus_dmamap_create(sc->sc_dmat, MCLBYTES, 1,
    169 		     MCLBYTES, 0, BUS_DMA_NOWAIT,
    170 		     &sc->sc_rxsoft[i].ds_dmamap)) != 0) {
    171 			printf("%s: unable to create rx DMA map %d, "
    172 			    "error = %d\n", sc->sc_dev.dv_xname, i, error);
    173 			goto fail_5;
    174 		}
    175 		sc->sc_rxsoft[i].ds_mbuf = NULL;
    176 	}
    177 
    178 	/*
    179 	 * create and map the pad buffer
    180 	 */
    181 	if ((error = bus_dmamap_create(sc->sc_dmat, ETHER_PAD_LEN, 1,
    182 	    ETHER_PAD_LEN, 0, BUS_DMA_NOWAIT, &sc->sc_nulldmamap)) != 0) {
    183 		printf("%s: unable to create pad buffer DMA map, "
    184 		    "error = %d\n", sc->sc_dev.dv_xname, error);
    185 		goto fail_5;
    186 	}
    187 
    188 	if ((error = bus_dmamap_load(sc->sc_dmat, sc->sc_nulldmamap,
    189 	    nullbuf, ETHER_PAD_LEN, NULL, BUS_DMA_NOWAIT)) != 0) {
    190 		printf("%s: unable to load pad buffer DMA map, "
    191 		    "error = %d\n", sc->sc_dev.dv_xname, error);
    192 		goto fail_6;
    193 	}
    194 	bus_dmamap_sync(sc->sc_dmat, sc->sc_nulldmamap, 0, ETHER_PAD_LEN,
    195 	    BUS_DMASYNC_PREWRITE);
    196 
    197 	/*
    198 	 * Reset the chip to a known state.
    199 	 */
    200 	sonic_reset(sc);
    201 
    202 	printf("%s: Ethernet address %s\n", sc->sc_dev.dv_xname,
    203 	    ether_sprintf(enaddr));
    204 
    205 	strcpy(ifp->if_xname, sc->sc_dev.dv_xname);
    206 	ifp->if_softc = sc;
    207 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
    208 	ifp->if_ioctl = sonic_ioctl;
    209 	ifp->if_start = sonic_start;
    210 	ifp->if_watchdog = sonic_watchdog;
    211 	ifp->if_init = sonic_init;
    212 	ifp->if_stop = sonic_stop;
    213 	IFQ_SET_READY(&ifp->if_snd);
    214 
    215 	/*
    216 	 * Attach the interface.
    217 	 */
    218 	if_attach(ifp);
    219 	ether_ifattach(ifp, enaddr);
    220 
    221 	/*
    222 	 * Make sure the interface is shutdown during reboot.
    223 	 */
    224 	sc->sc_sdhook = shutdownhook_establish(sonic_shutdown, sc);
    225 	if (sc->sc_sdhook == NULL)
    226 		printf("%s: WARNING: unable to establish shutdown hook\n",
    227 		    sc->sc_dev.dv_xname);
    228 	return;
    229 
    230 	/*
    231 	 * Free any resources we've allocated during the failed attach
    232 	 * attempt.  Do this in reverse order and fall through.
    233 	 */
    234  fail_6:
    235 	bus_dmamap_destroy(sc->sc_dmat, sc->sc_nulldmamap);
    236  fail_5:
    237 	for (i = 0; i < SONIC_NRXDESC; i++) {
    238 		if (sc->sc_rxsoft[i].ds_dmamap != NULL)
    239 			bus_dmamap_destroy(sc->sc_dmat,
    240 			    sc->sc_rxsoft[i].ds_dmamap);
    241 	}
    242  fail_4:
    243 	for (i = 0; i < SONIC_NTXDESC; i++) {
    244 		if (sc->sc_txsoft[i].ds_dmamap != NULL)
    245 			bus_dmamap_destroy(sc->sc_dmat,
    246 			    sc->sc_txsoft[i].ds_dmamap);
    247 	}
    248 	bus_dmamap_unload(sc->sc_dmat, sc->sc_cddmamap);
    249  fail_3:
    250 	bus_dmamap_destroy(sc->sc_dmat, sc->sc_cddmamap);
    251  fail_2:
    252 	bus_dmamem_unmap(sc->sc_dmat, (void *) sc->sc_cdata16, cdatasize);
    253  fail_1:
    254 	bus_dmamem_free(sc->sc_dmat, &seg, rseg);
    255  fail_0:
    256 	return;
    257 }
    258 
    259 /*
    260  * sonic_shutdown:
    261  *
    262  *	Make sure the interface is stopped at reboot.
    263  */
    264 void
    265 sonic_shutdown(void *arg)
    266 {
    267 	struct sonic_softc *sc = arg;
    268 
    269 	sonic_stop(&sc->sc_ethercom.ec_if, 1);
    270 }
    271 
    272 /*
    273  * sonic_start:		[ifnet interface function]
    274  *
    275  *	Start packet transmission on the interface.
    276  */
    277 void
    278 sonic_start(struct ifnet *ifp)
    279 {
    280 	struct sonic_softc *sc = ifp->if_softc;
    281 	struct mbuf *m0, *m;
    282 	struct sonic_tda16 *tda16;
    283 	struct sonic_tda32 *tda32;
    284 	struct sonic_descsoft *ds;
    285 	bus_dmamap_t dmamap;
    286 	int error, olasttx, nexttx, opending, totlen, olseg;
    287 	int seg = 0;	/* XXX: gcc */
    288 
    289 	if ((ifp->if_flags & (IFF_RUNNING|IFF_OACTIVE)) != IFF_RUNNING)
    290 		return;
    291 
    292 	/*
    293 	 * Remember the previous txpending and the current "last txdesc
    294 	 * used" index.
    295 	 */
    296 	opending = sc->sc_txpending;
    297 	olasttx = sc->sc_txlast;
    298 
    299 	/*
    300 	 * Loop through the send queue, setting up transmit descriptors
    301 	 * until we drain the queue, or use up all available transmit
    302 	 * descriptors.  Leave one at the end for sanity's sake.
    303 	 */
    304 	while (sc->sc_txpending < (SONIC_NTXDESC - 1)) {
    305 		/*
    306 		 * Grab a packet off the queue.
    307 		 */
    308 		IFQ_POLL(&ifp->if_snd, m0);
    309 		if (m0 == NULL)
    310 			break;
    311 		m = NULL;
    312 
    313 		/*
    314 		 * Get the next available transmit descriptor.
    315 		 */
    316 		nexttx = SONIC_NEXTTX(sc->sc_txlast);
    317 		ds = &sc->sc_txsoft[nexttx];
    318 		dmamap = ds->ds_dmamap;
    319 
    320 		/*
    321 		 * Load the DMA map.  If this fails, the packet either
    322 		 * didn't fit in the allotted number of frags, or we were
    323 		 * short on resources.  In this case, we'll copy and try
    324 		 * again.
    325 		 */
    326 		if ((error = bus_dmamap_load_mbuf(sc->sc_dmat, dmamap, m0,
    327 		    BUS_DMA_WRITE|BUS_DMA_NOWAIT)) != 0 ||
    328 		    (m0->m_pkthdr.len < ETHER_PAD_LEN &&
    329 		    dmamap->dm_nsegs == SONIC_NTXFRAGS)) {
    330 			if (error == 0)
    331 				bus_dmamap_unload(sc->sc_dmat, dmamap);
    332 			MGETHDR(m, M_DONTWAIT, MT_DATA);
    333 			if (m == NULL) {
    334 				printf("%s: unable to allocate Tx mbuf\n",
    335 				    sc->sc_dev.dv_xname);
    336 				break;
    337 			}
    338 			if (m0->m_pkthdr.len > MHLEN) {
    339 				MCLGET(m, M_DONTWAIT);
    340 				if ((m->m_flags & M_EXT) == 0) {
    341 					printf("%s: unable to allocate Tx "
    342 					    "cluster\n", sc->sc_dev.dv_xname);
    343 					m_freem(m);
    344 					break;
    345 				}
    346 			}
    347 			m_copydata(m0, 0, m0->m_pkthdr.len, mtod(m, void *));
    348 			m->m_pkthdr.len = m->m_len = m0->m_pkthdr.len;
    349 			error = bus_dmamap_load_mbuf(sc->sc_dmat, dmamap,
    350 			    m, BUS_DMA_WRITE|BUS_DMA_NOWAIT);
    351 			if (error) {
    352 				printf("%s: unable to load Tx buffer, "
    353 				    "error = %d\n", sc->sc_dev.dv_xname, error);
    354 				m_freem(m);
    355 				break;
    356 			}
    357 		}
    358 		IFQ_DEQUEUE(&ifp->if_snd, m0);
    359 		if (m != NULL) {
    360 			m_freem(m0);
    361 			m0 = m;
    362 		}
    363 
    364 		/*
    365 		 * WE ARE NOW COMMITTED TO TRANSMITTING THE PACKET.
    366 		 */
    367 
    368 		/* Sync the DMA map. */
    369 		bus_dmamap_sync(sc->sc_dmat, dmamap, 0, dmamap->dm_mapsize,
    370 		    BUS_DMASYNC_PREWRITE);
    371 
    372 		/*
    373 		 * Store a pointer to the packet so we can free it later.
    374 		 */
    375 		ds->ds_mbuf = m0;
    376 
    377 		/*
    378 		 * Initialize the transmit descriptor.
    379 		 */
    380 		totlen = 0;
    381 		if (sc->sc_32bit) {
    382 			tda32 = &sc->sc_tda32[nexttx];
    383 			for (seg = 0; seg < dmamap->dm_nsegs; seg++) {
    384 				tda32->tda_frags[seg].frag_ptr1 =
    385 				    htosonic32(sc,
    386 				    (dmamap->dm_segs[seg].ds_addr >> 16) &
    387 				    0xffff);
    388 				tda32->tda_frags[seg].frag_ptr0 =
    389 				    htosonic32(sc,
    390 				    dmamap->dm_segs[seg].ds_addr & 0xffff);
    391 				tda32->tda_frags[seg].frag_size =
    392 				    htosonic32(sc, dmamap->dm_segs[seg].ds_len);
    393 				totlen += dmamap->dm_segs[seg].ds_len;
    394 			}
    395 			if (totlen < ETHER_PAD_LEN) {
    396 				tda32->tda_frags[seg].frag_ptr1 =
    397 				    htosonic32(sc,
    398 				    (sc->sc_nulldma >> 16) & 0xffff);
    399 				tda32->tda_frags[seg].frag_ptr0 =
    400 				    htosonic32(sc, sc->sc_nulldma & 0xffff);
    401 				tda32->tda_frags[seg].frag_size =
    402 				    htosonic32(sc, ETHER_PAD_LEN - totlen);
    403 				totlen = ETHER_PAD_LEN;
    404 				seg++;
    405 			}
    406 
    407 			tda32->tda_status = 0;
    408 			tda32->tda_pktconfig = 0;
    409 			tda32->tda_pktsize = htosonic32(sc, totlen);
    410 			tda32->tda_fragcnt = htosonic32(sc, seg);
    411 
    412 			/* Link it up. */
    413 			tda32->tda_frags[seg].frag_ptr0 =
    414 			    htosonic32(sc, SONIC_CDTXADDR32(sc,
    415 			    SONIC_NEXTTX(nexttx)) & 0xffff);
    416 
    417 			/* Sync the Tx descriptor. */
    418 			SONIC_CDTXSYNC32(sc, nexttx,
    419 			    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
    420 		} else {
    421 			tda16 = &sc->sc_tda16[nexttx];
    422 			for (seg = 0; seg < dmamap->dm_nsegs; seg++) {
    423 				tda16->tda_frags[seg].frag_ptr1 =
    424 				    htosonic16(sc,
    425 				    (dmamap->dm_segs[seg].ds_addr >> 16) &
    426 				    0xffff);
    427 				tda16->tda_frags[seg].frag_ptr0 =
    428 				    htosonic16(sc,
    429 				    dmamap->dm_segs[seg].ds_addr & 0xffff);
    430 				tda16->tda_frags[seg].frag_size =
    431 				    htosonic16(sc, dmamap->dm_segs[seg].ds_len);
    432 				totlen += dmamap->dm_segs[seg].ds_len;
    433 			}
    434 			if (totlen < ETHER_PAD_LEN) {
    435 				tda16->tda_frags[seg].frag_ptr1 =
    436 				    htosonic16(sc,
    437 				    (sc->sc_nulldma >> 16) & 0xffff);
    438 				tda16->tda_frags[seg].frag_ptr0 =
    439 				    htosonic16(sc, sc->sc_nulldma & 0xffff);
    440 				tda16->tda_frags[seg].frag_size =
    441 				    htosonic16(sc, ETHER_PAD_LEN - totlen);
    442 				totlen = ETHER_PAD_LEN;
    443 				seg++;
    444 			}
    445 
    446 			tda16->tda_status = 0;
    447 			tda16->tda_pktconfig = 0;
    448 			tda16->tda_pktsize = htosonic16(sc, totlen);
    449 			tda16->tda_fragcnt = htosonic16(sc, seg);
    450 
    451 			/* Link it up. */
    452 			tda16->tda_frags[seg].frag_ptr0 =
    453 			    htosonic16(sc, SONIC_CDTXADDR16(sc,
    454 			    SONIC_NEXTTX(nexttx)) & 0xffff);
    455 
    456 			/* Sync the Tx descriptor. */
    457 			SONIC_CDTXSYNC16(sc, nexttx,
    458 			    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
    459 		}
    460 
    461 		/* Advance the Tx pointer. */
    462 		sc->sc_txpending++;
    463 		sc->sc_txlast = nexttx;
    464 
    465 #if NBPFILTER > 0
    466 		/*
    467 		 * Pass the packet to any BPF listeners.
    468 		 */
    469 		if (ifp->if_bpf)
    470 			bpf_mtap(ifp->if_bpf, m0);
    471 #endif
    472 	}
    473 
    474 	if (sc->sc_txpending == (SONIC_NTXDESC - 1)) {
    475 		/* No more slots left; notify upper layer. */
    476 		ifp->if_flags |= IFF_OACTIVE;
    477 	}
    478 
    479 	if (sc->sc_txpending != opending) {
    480 		/*
    481 		 * We enqueued packets.  If the transmitter was idle,
    482 		 * reset the txdirty pointer.
    483 		 */
    484 		if (opending == 0)
    485 			sc->sc_txdirty = SONIC_NEXTTX(olasttx);
    486 
    487 		/*
    488 		 * Stop the SONIC on the last packet we've set up,
    489 		 * and clear end-of-list on the descriptor previous
    490 		 * to our new chain.
    491 		 *
    492 		 * NOTE: our `seg' variable should still be valid!
    493 		 */
    494 		if (sc->sc_32bit) {
    495 			olseg =
    496 			    sonic32toh(sc, sc->sc_tda32[olasttx].tda_fragcnt);
    497 			sc->sc_tda32[sc->sc_txlast].tda_frags[seg].frag_ptr0 |=
    498 			    htosonic32(sc, TDA_LINK_EOL);
    499 			SONIC_CDTXSYNC32(sc, sc->sc_txlast,
    500 			    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
    501 			sc->sc_tda32[olasttx].tda_frags[olseg].frag_ptr0 &=
    502 			    htosonic32(sc, ~TDA_LINK_EOL);
    503 			SONIC_CDTXSYNC32(sc, olasttx,
    504 			    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
    505 		} else {
    506 			olseg =
    507 			    sonic16toh(sc, sc->sc_tda16[olasttx].tda_fragcnt);
    508 			sc->sc_tda16[sc->sc_txlast].tda_frags[seg].frag_ptr0 |=
    509 			    htosonic16(sc, TDA_LINK_EOL);
    510 			SONIC_CDTXSYNC16(sc, sc->sc_txlast,
    511 			    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
    512 			sc->sc_tda16[olasttx].tda_frags[olseg].frag_ptr0 &=
    513 			    htosonic16(sc, ~TDA_LINK_EOL);
    514 			SONIC_CDTXSYNC16(sc, olasttx,
    515 			    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
    516 		}
    517 
    518 		/* Start the transmitter. */
    519 		CSR_WRITE(sc, SONIC_CR, CR_TXP);
    520 
    521 		/* Set a watchdog timer in case the chip flakes out. */
    522 		ifp->if_timer = 5;
    523 	}
    524 }
    525 
    526 /*
    527  * sonic_watchdog:	[ifnet interface function]
    528  *
    529  *	Watchdog timer handler.
    530  */
    531 void
    532 sonic_watchdog(struct ifnet *ifp)
    533 {
    534 	struct sonic_softc *sc = ifp->if_softc;
    535 
    536 	printf("%s: device timeout\n", sc->sc_dev.dv_xname);
    537 	ifp->if_oerrors++;
    538 
    539 	(void) sonic_init(ifp);
    540 }
    541 
    542 /*
    543  * sonic_ioctl:		[ifnet interface function]
    544  *
    545  *	Handle control requests from the operator.
    546  */
    547 int
    548 sonic_ioctl(struct ifnet *ifp, u_long cmd, void *data)
    549 {
    550 	int s, error;
    551 
    552 	s = splnet();
    553 
    554 	switch (cmd) {
    555 	default:
    556 		error = ether_ioctl(ifp, cmd, data);
    557 		if (error == ENETRESET) {
    558 			/*
    559 			 * Multicast list has changed; set the hardware
    560 			 * filter accordingly.
    561 			 */
    562 			if (ifp->if_flags & IFF_RUNNING)
    563 				(void) sonic_init(ifp);
    564 			error = 0;
    565 		}
    566 		break;
    567 	}
    568 
    569 	splx(s);
    570 	return (error);
    571 }
    572 
    573 /*
    574  * sonic_intr:
    575  *
    576  *	Interrupt service routine.
    577  */
    578 int
    579 sonic_intr(void *arg)
    580 {
    581 	struct sonic_softc *sc = arg;
    582 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    583 	uint16_t isr;
    584 	int handled = 0, wantinit;
    585 
    586 	for (wantinit = 0; wantinit == 0;) {
    587 		isr = CSR_READ(sc, SONIC_ISR) & sc->sc_imr;
    588 		if (isr == 0)
    589 			break;
    590 		CSR_WRITE(sc, SONIC_ISR, isr);	/* ACK */
    591 
    592 		handled = 1;
    593 
    594 		if (isr & IMR_PRX)
    595 			sonic_rxintr(sc);
    596 
    597 		if (isr & (IMR_PTX|IMR_TXER)) {
    598 			if (sonic_txintr(sc) & TCR_FU) {
    599 				printf("%s: transmit FIFO underrun\n",
    600 				    sc->sc_dev.dv_xname);
    601 				wantinit = 1;
    602 			}
    603 		}
    604 
    605 		if (isr & (IMR_RFO|IMR_RBA|IMR_RBE|IMR_RDE)) {
    606 #define	PRINTERR(bit, str)						\
    607 			if (isr & (bit))				\
    608 				printf("%s: %s\n", sc->sc_dev.dv_xname, str)
    609 			PRINTERR(IMR_RFO, "receive FIFO overrun");
    610 			PRINTERR(IMR_RBA, "receive buffer exceeded");
    611 			PRINTERR(IMR_RBE, "receive buffers exhausted");
    612 			PRINTERR(IMR_RDE, "receive descriptors exhausted");
    613 			wantinit = 1;
    614 		}
    615 	}
    616 
    617 	if (handled) {
    618 		if (wantinit)
    619 			(void) sonic_init(ifp);
    620 		sonic_start(ifp);
    621 	}
    622 
    623 	return (handled);
    624 }
    625 
    626 /*
    627  * sonic_txintr:
    628  *
    629  *	Helper; handle transmit complete interrupts.
    630  */
    631 uint16_t
    632 sonic_txintr(struct sonic_softc *sc)
    633 {
    634 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    635 	struct sonic_descsoft *ds;
    636 	struct sonic_tda32 *tda32;
    637 	struct sonic_tda16 *tda16;
    638 	uint16_t status, totstat = 0;
    639 	int i;
    640 
    641 	ifp->if_flags &= ~IFF_OACTIVE;
    642 
    643 	for (i = sc->sc_txdirty; sc->sc_txpending != 0;
    644 	     i = SONIC_NEXTTX(i), sc->sc_txpending--) {
    645 		ds = &sc->sc_txsoft[i];
    646 
    647 		if (sc->sc_32bit) {
    648 			SONIC_CDTXSYNC32(sc, i,
    649 			    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
    650 			tda32 = &sc->sc_tda32[i];
    651 			status = sonic32toh(sc, tda32->tda_status);
    652 			SONIC_CDTXSYNC32(sc, i, BUS_DMASYNC_PREREAD);
    653 		} else {
    654 			SONIC_CDTXSYNC16(sc, i,
    655 			    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
    656 			tda16 = &sc->sc_tda16[i];
    657 			status = sonic16toh(sc, tda16->tda_status);
    658 			SONIC_CDTXSYNC16(sc, i, BUS_DMASYNC_PREREAD);
    659 		}
    660 
    661 		if ((status & ~(TCR_EXDIS|TCR_CRCI|TCR_POWC|TCR_PINT)) == 0)
    662 			break;
    663 
    664 		totstat |= status;
    665 
    666 		bus_dmamap_sync(sc->sc_dmat, ds->ds_dmamap, 0,
    667 		    ds->ds_dmamap->dm_mapsize, BUS_DMASYNC_POSTWRITE);
    668 		bus_dmamap_unload(sc->sc_dmat, ds->ds_dmamap);
    669 		m_freem(ds->ds_mbuf);
    670 		ds->ds_mbuf = NULL;
    671 
    672 		/*
    673 		 * Check for errors and collisions.
    674 		 */
    675 		if (status & TCR_PTX)
    676 			ifp->if_opackets++;
    677 		else
    678 			ifp->if_oerrors++;
    679 		ifp->if_collisions += TDA_STATUS_NCOL(status);
    680 	}
    681 
    682 	/* Update the dirty transmit buffer pointer. */
    683 	sc->sc_txdirty = i;
    684 
    685 	/*
    686 	 * Cancel the watchdog timer if there are no pending
    687 	 * transmissions.
    688 	 */
    689 	if (sc->sc_txpending == 0)
    690 		ifp->if_timer = 0;
    691 
    692 	return (totstat);
    693 }
    694 
    695 /*
    696  * sonic_rxintr:
    697  *
    698  *	Helper; handle receive interrupts.
    699  */
    700 void
    701 sonic_rxintr(struct sonic_softc *sc)
    702 {
    703 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    704 	struct sonic_descsoft *ds;
    705 	struct sonic_rda32 *rda32;
    706 	struct sonic_rda16 *rda16;
    707 	struct mbuf *m;
    708 	int i, len;
    709 	uint16_t status, bytecount, ptr0, ptr1, seqno;
    710 
    711 	for (i = sc->sc_rxptr;; i = SONIC_NEXTRX(i)) {
    712 		ds = &sc->sc_rxsoft[i];
    713 
    714 		if (sc->sc_32bit) {
    715 			SONIC_CDRXSYNC32(sc, i,
    716 			    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
    717 			rda32 = &sc->sc_rda32[i];
    718 			SONIC_CDRXSYNC32(sc, i, BUS_DMASYNC_PREREAD);
    719 			if (rda32->rda_inuse != 0)
    720 				break;
    721 			status = sonic32toh(sc, rda32->rda_status);
    722 			bytecount = sonic32toh(sc, rda32->rda_bytecount);
    723 			ptr0 = sonic32toh(sc, rda32->rda_pkt_ptr0);
    724 			ptr1 = sonic32toh(sc, rda32->rda_pkt_ptr1);
    725 			seqno = sonic32toh(sc, rda32->rda_seqno);
    726 		} else {
    727 			SONIC_CDRXSYNC16(sc, i,
    728 			    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
    729 			rda16 = &sc->sc_rda16[i];
    730 			SONIC_CDRXSYNC16(sc, i, BUS_DMASYNC_PREREAD);
    731 			if (rda16->rda_inuse != 0)
    732 				break;
    733 			status = sonic16toh(sc, rda16->rda_status);
    734 			bytecount = sonic16toh(sc, rda16->rda_bytecount);
    735 			ptr0 = sonic16toh(sc, rda16->rda_pkt_ptr0);
    736 			ptr1 = sonic16toh(sc, rda16->rda_pkt_ptr1);
    737 			seqno = sonic16toh(sc, rda16->rda_seqno);
    738 		}
    739 
    740 		/*
    741 		 * Make absolutely sure this is the only packet
    742 		 * in this receive buffer.  Our entire Rx buffer
    743 		 * management scheme depends on this, and if the
    744 		 * SONIC didn't follow our rule, it means we've
    745 		 * misconfigured it.
    746 		 */
    747 		KASSERT(status & RCR_LPKT);
    748 
    749 		/*
    750 		 * Make sure the packet arrived OK.  If an error occurred,
    751 		 * update stats and reset the descriptor.  The buffer will
    752 		 * be reused the next time the descriptor comes up in the
    753 		 * ring.
    754 		 */
    755 		if ((status & RCR_PRX) == 0) {
    756 			if (status & RCR_FAER)
    757 				printf("%s: Rx frame alignment error\n",
    758 				    sc->sc_dev.dv_xname);
    759 			else if (status & RCR_CRCR)
    760 				printf("%s: Rx CRC error\n",
    761 				    sc->sc_dev.dv_xname);
    762 			ifp->if_ierrors++;
    763 			SONIC_INIT_RXDESC(sc, i);
    764 			continue;
    765 		}
    766 
    767 		bus_dmamap_sync(sc->sc_dmat, ds->ds_dmamap, 0,
    768 		    ds->ds_dmamap->dm_mapsize, BUS_DMASYNC_POSTREAD);
    769 
    770 		/*
    771 		 * The SONIC includes the CRC with every packet.
    772 		 */
    773 		len = bytecount - ETHER_CRC_LEN;
    774 
    775 		/*
    776 		 * Ok, if the chip is in 32-bit mode, then receive
    777 		 * buffers must be aligned to 32-bit boundaries,
    778 		 * which means the payload is misaligned.  In this
    779 		 * case, we must allocate a new mbuf, and copy the
    780 		 * packet into it, scooted forward 2 bytes to ensure
    781 		 * proper alignment.
    782 		 *
    783 		 * Note, in 16-bit mode, we can configure the SONIC
    784 		 * to do what we want, and we have.
    785 		 */
    786 #ifndef __NO_STRICT_ALIGNMENT
    787 		if (sc->sc_32bit) {
    788 			MGETHDR(m, M_DONTWAIT, MT_DATA);
    789 			if (m == NULL)
    790 				goto dropit;
    791 			if (len > (MHLEN - 2)) {
    792 				MCLGET(m, M_DONTWAIT);
    793 				if ((m->m_flags & M_EXT) == 0)
    794 					goto dropit;
    795 			}
    796 			m->m_data += 2;
    797 			/*
    798 			 * Note that we use a cluster for incoming frames,
    799 			 * so the buffer is virtually contiguous.
    800 			 */
    801 			memcpy(mtod(m, void *), mtod(ds->ds_mbuf, void *),
    802 			    len);
    803 			SONIC_INIT_RXDESC(sc, i);
    804 			bus_dmamap_sync(sc->sc_dmat, ds->ds_dmamap, 0,
    805 			    ds->ds_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
    806 		} else
    807 #endif /* ! __NO_STRICT_ALIGNMENT */
    808 		/*
    809 		 * If the packet is small enough to fit in a single
    810 		 * header mbuf, allocate one and copy the data into
    811 		 * it.  This greatly reduces memory consumption when
    812 		 * we receive lots of small packets.
    813 		 */
    814 		if (sonic_copy_small != 0 && len <= (MHLEN - 2)) {
    815 			MGETHDR(m, M_DONTWAIT, MT_DATA);
    816 			if (m == NULL)
    817 				goto dropit;
    818 			m->m_data += 2;
    819 			/*
    820 			 * Note that we use a cluster for incoming frames,
    821 			 * so the buffer is virtually contiguous.
    822 			 */
    823 			memcpy(mtod(m, void *), mtod(ds->ds_mbuf, void *),
    824 			    len);
    825 			SONIC_INIT_RXDESC(sc, i);
    826 			bus_dmamap_sync(sc->sc_dmat, ds->ds_dmamap, 0,
    827 			    ds->ds_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
    828 		} else {
    829 			m = ds->ds_mbuf;
    830 			if (sonic_add_rxbuf(sc, i) != 0) {
    831  dropit:
    832 				ifp->if_ierrors++;
    833 				SONIC_INIT_RXDESC(sc, i);
    834 				bus_dmamap_sync(sc->sc_dmat, ds->ds_dmamap, 0,
    835 				    ds->ds_dmamap->dm_mapsize,
    836 				    BUS_DMASYNC_PREREAD);
    837 				continue;
    838 			}
    839 		}
    840 
    841 		ifp->if_ipackets++;
    842 		m->m_pkthdr.rcvif = ifp;
    843 		m->m_pkthdr.len = m->m_len = len;
    844 
    845 #if NBPFILTER > 0
    846 		/*
    847 		 * Pass this up to any BPF listeners.
    848 		 */
    849 		if (ifp->if_bpf)
    850 			bpf_mtap(ifp->if_bpf, m);
    851 #endif /* NBPFILTER > 0 */
    852 
    853 		/* Pass it on. */
    854 		(*ifp->if_input)(ifp, m);
    855 	}
    856 
    857 	/* Update the receive pointer. */
    858 	sc->sc_rxptr = i;
    859 	CSR_WRITE(sc, SONIC_RWR, SONIC_CDRRADDR(sc, SONIC_PREVRX(i)));
    860 }
    861 
    862 /*
    863  * sonic_reset:
    864  *
    865  *	Perform a soft reset on the SONIC.
    866  */
    867 void
    868 sonic_reset(struct sonic_softc *sc)
    869 {
    870 
    871 	/* stop TX, RX and timer, and ensure RST is clear */
    872 	CSR_WRITE(sc, SONIC_CR, CR_STP | CR_RXDIS | CR_HTX);
    873 	delay(1000);
    874 
    875 	CSR_WRITE(sc, SONIC_CR, CR_RST);
    876 	delay(1000);
    877 
    878 	/* clear all interrupts */
    879 	CSR_WRITE(sc, SONIC_IMR, 0);
    880 	CSR_WRITE(sc, SONIC_ISR, IMR_ALL);
    881 
    882 	CSR_WRITE(sc, SONIC_CR, 0);
    883 	delay(1000);
    884 }
    885 
    886 /*
    887  * sonic_init:		[ifnet interface function]
    888  *
    889  *	Initialize the interface.  Must be called at splnet().
    890  */
    891 int
    892 sonic_init(struct ifnet *ifp)
    893 {
    894 	struct sonic_softc *sc = ifp->if_softc;
    895 	struct sonic_descsoft *ds;
    896 	int i, error = 0;
    897 	uint16_t reg;
    898 
    899 	/*
    900 	 * Cancel any pending I/O.
    901 	 */
    902 	sonic_stop(ifp, 0);
    903 
    904 	/*
    905 	 * Reset the SONIC to a known state.
    906 	 */
    907 	sonic_reset(sc);
    908 
    909 	/*
    910 	 * Bring the SONIC into reset state, and program the DCR.
    911 	 *
    912 	 * Note: We don't bother optimizing the transmit and receive
    913 	 * thresholds, here. TFT/RFT values should be set in MD attachments.
    914 	 */
    915 	reg = sc->sc_dcr;
    916 	if (sc->sc_32bit)
    917 		reg |= DCR_DW;
    918 	CSR_WRITE(sc, SONIC_CR, CR_RST);
    919 	CSR_WRITE(sc, SONIC_DCR, reg);
    920 	CSR_WRITE(sc, SONIC_DCR2, sc->sc_dcr2);
    921 	CSR_WRITE(sc, SONIC_CR, 0);
    922 
    923 	/*
    924 	 * Initialize the transmit descriptors.
    925 	 */
    926 	if (sc->sc_32bit) {
    927 		for (i = 0; i < SONIC_NTXDESC; i++) {
    928 			memset(&sc->sc_tda32[i], 0, sizeof(struct sonic_tda32));
    929 			SONIC_CDTXSYNC32(sc, i,
    930 			    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
    931 		}
    932 	} else {
    933 		for (i = 0; i < SONIC_NTXDESC; i++) {
    934 			memset(&sc->sc_tda16[i], 0, sizeof(struct sonic_tda16));
    935 			SONIC_CDTXSYNC16(sc, i,
    936 			    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
    937 		}
    938 	}
    939 	sc->sc_txpending = 0;
    940 	sc->sc_txdirty = 0;
    941 	sc->sc_txlast = SONIC_NTXDESC - 1;
    942 
    943 	/*
    944 	 * Initialize the receive descriptor ring.
    945 	 */
    946 	for (i = 0; i < SONIC_NRXDESC; i++) {
    947 		ds = &sc->sc_rxsoft[i];
    948 		if (ds->ds_mbuf == NULL) {
    949 			if ((error = sonic_add_rxbuf(sc, i)) != 0) {
    950 				printf("%s: unable to allocate or map Rx "
    951 				    "buffer %d, error = %d\n",
    952 				    sc->sc_dev.dv_xname, i, error);
    953 				/*
    954 				 * XXX Should attempt to run with fewer receive
    955 				 * XXX buffers instead of just failing.
    956 				 */
    957 				sonic_rxdrain(sc);
    958 				goto out;
    959 			}
    960 		} else
    961 			SONIC_INIT_RXDESC(sc, i);
    962 	}
    963 	sc->sc_rxptr = 0;
    964 
    965 	/* Give the transmit ring to the SONIC. */
    966 	CSR_WRITE(sc, SONIC_UTDAR, (SONIC_CDTXADDR(sc, 0) >> 16) & 0xffff);
    967 	CSR_WRITE(sc, SONIC_CTDAR, SONIC_CDTXADDR(sc, 0) & 0xffff);
    968 
    969 	/* Give the receive descriptor ring to the SONIC. */
    970 	CSR_WRITE(sc, SONIC_URDAR, (SONIC_CDRXADDR(sc, 0) >> 16) & 0xffff);
    971 	CSR_WRITE(sc, SONIC_CRDAR, SONIC_CDRXADDR(sc, 0) & 0xffff);
    972 
    973 	/* Give the receive buffer ring to the SONIC. */
    974 	CSR_WRITE(sc, SONIC_URRAR, (SONIC_CDRRADDR(sc, 0) >> 16) & 0xffff);
    975 	CSR_WRITE(sc, SONIC_RSAR, SONIC_CDRRADDR(sc, 0) & 0xffff);
    976 	if (sc->sc_32bit)
    977 		CSR_WRITE(sc, SONIC_REAR,
    978 		    (SONIC_CDRRADDR(sc, SONIC_NRXDESC - 1) +
    979 		    sizeof(struct sonic_rra32)) & 0xffff);
    980 	else
    981 		CSR_WRITE(sc, SONIC_REAR,
    982 		    (SONIC_CDRRADDR(sc, SONIC_NRXDESC - 1) +
    983 		    sizeof(struct sonic_rra16)) & 0xffff);
    984 	CSR_WRITE(sc, SONIC_RRR, SONIC_CDRRADDR(sc, 0) & 0xffff);
    985 	CSR_WRITE(sc, SONIC_RWR, SONIC_CDRRADDR(sc, SONIC_NRXDESC - 1));
    986 
    987 	/*
    988 	 * Set the End-Of-Buffer counter such that only one packet
    989 	 * will be placed into each buffer we provide.  Note we are
    990 	 * following the recommendation of section 3.4.4 of the manual
    991 	 * here, and have "lengthened" the receive buffers accordingly.
    992 	 */
    993 	if (sc->sc_32bit)
    994 		CSR_WRITE(sc, SONIC_EOBC, (ETHER_MAX_LEN + 2) / 2);
    995 	else
    996 		CSR_WRITE(sc, SONIC_EOBC, (ETHER_MAX_LEN / 2));
    997 
    998 	/* Reset the receive sequence counter. */
    999 	CSR_WRITE(sc, SONIC_RSC, 0);
   1000 
   1001 	/* Clear the tally registers. */
   1002 	CSR_WRITE(sc, SONIC_CRCETC, 0xffff);
   1003 	CSR_WRITE(sc, SONIC_FAET, 0xffff);
   1004 	CSR_WRITE(sc, SONIC_MPT, 0xffff);
   1005 
   1006 	/* Set the receive filter. */
   1007 	sonic_set_filter(sc);
   1008 
   1009 	/*
   1010 	 * Set the interrupt mask register.
   1011 	 */
   1012 	sc->sc_imr = IMR_RFO | IMR_RBA | IMR_RBE | IMR_RDE |
   1013 	    IMR_TXER | IMR_PTX | IMR_PRX;
   1014 	CSR_WRITE(sc, SONIC_IMR, sc->sc_imr);
   1015 
   1016 	/*
   1017 	 * Start the receive process in motion.  Note, we don't
   1018 	 * start the transmit process until we actually try to
   1019 	 * transmit packets.
   1020 	 */
   1021 	CSR_WRITE(sc, SONIC_CR, CR_RXEN | CR_RRRA);
   1022 
   1023 	/*
   1024 	 * ...all done!
   1025 	 */
   1026 	ifp->if_flags |= IFF_RUNNING;
   1027 	ifp->if_flags &= ~IFF_OACTIVE;
   1028 
   1029  out:
   1030 	if (error)
   1031 		printf("%s: interface not running\n", sc->sc_dev.dv_xname);
   1032 	return (error);
   1033 }
   1034 
   1035 /*
   1036  * sonic_rxdrain:
   1037  *
   1038  *	Drain the receive queue.
   1039  */
   1040 void
   1041 sonic_rxdrain(struct sonic_softc *sc)
   1042 {
   1043 	struct sonic_descsoft *ds;
   1044 	int i;
   1045 
   1046 	for (i = 0; i < SONIC_NRXDESC; i++) {
   1047 		ds = &sc->sc_rxsoft[i];
   1048 		if (ds->ds_mbuf != NULL) {
   1049 			bus_dmamap_unload(sc->sc_dmat, ds->ds_dmamap);
   1050 			m_freem(ds->ds_mbuf);
   1051 			ds->ds_mbuf = NULL;
   1052 		}
   1053 	}
   1054 }
   1055 
   1056 /*
   1057  * sonic_stop:		[ifnet interface function]
   1058  *
   1059  *	Stop transmission on the interface.
   1060  */
   1061 void
   1062 sonic_stop(struct ifnet *ifp, int disable)
   1063 {
   1064 	struct sonic_softc *sc = ifp->if_softc;
   1065 	struct sonic_descsoft *ds;
   1066 	int i;
   1067 
   1068 	/*
   1069 	 * Disable interrupts.
   1070 	 */
   1071 	CSR_WRITE(sc, SONIC_IMR, 0);
   1072 
   1073 	/*
   1074 	 * Stop the transmitter, receiver, and timer.
   1075 	 */
   1076 	CSR_WRITE(sc, SONIC_CR, CR_HTX|CR_RXDIS|CR_STP);
   1077 	for (i = 0; i < 1000; i++) {
   1078 		if ((CSR_READ(sc, SONIC_CR) & (CR_TXP|CR_RXEN|CR_ST)) == 0)
   1079 			break;
   1080 		delay(2);
   1081 	}
   1082 	if ((CSR_READ(sc, SONIC_CR) & (CR_TXP|CR_RXEN|CR_ST)) != 0)
   1083 		printf("%s: SONIC failed to stop\n", sc->sc_dev.dv_xname);
   1084 
   1085 	/*
   1086 	 * Release any queued transmit buffers.
   1087 	 */
   1088 	for (i = 0; i < SONIC_NTXDESC; i++) {
   1089 		ds = &sc->sc_txsoft[i];
   1090 		if (ds->ds_mbuf != NULL) {
   1091 			bus_dmamap_unload(sc->sc_dmat, ds->ds_dmamap);
   1092 			m_freem(ds->ds_mbuf);
   1093 			ds->ds_mbuf = NULL;
   1094 		}
   1095 	}
   1096 
   1097 	if (disable)
   1098 		sonic_rxdrain(sc);
   1099 
   1100 	/*
   1101 	 * Mark the interface down and cancel the watchdog timer.
   1102 	 */
   1103 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
   1104 	ifp->if_timer = 0;
   1105 }
   1106 
   1107 /*
   1108  * sonic_add_rxbuf:
   1109  *
   1110  *	Add a receive buffer to the indicated descriptor.
   1111  */
   1112 int
   1113 sonic_add_rxbuf(struct sonic_softc *sc, int idx)
   1114 {
   1115 	struct sonic_descsoft *ds = &sc->sc_rxsoft[idx];
   1116 	struct mbuf *m;
   1117 	int error;
   1118 
   1119 	MGETHDR(m, M_DONTWAIT, MT_DATA);
   1120 	if (m == NULL)
   1121 		return (ENOBUFS);
   1122 
   1123 	MCLGET(m, M_DONTWAIT);
   1124 	if ((m->m_flags & M_EXT) == 0) {
   1125 		m_freem(m);
   1126 		return (ENOBUFS);
   1127 	}
   1128 
   1129 	if (ds->ds_mbuf != NULL)
   1130 		bus_dmamap_unload(sc->sc_dmat, ds->ds_dmamap);
   1131 
   1132 	ds->ds_mbuf = m;
   1133 
   1134 	error = bus_dmamap_load(sc->sc_dmat, ds->ds_dmamap,
   1135 	    m->m_ext.ext_buf, m->m_ext.ext_size, NULL,
   1136 	    BUS_DMA_READ|BUS_DMA_NOWAIT);
   1137 	if (error) {
   1138 		printf("%s: can't load rx DMA map %d, error = %d\n",
   1139 		    sc->sc_dev.dv_xname, idx, error);
   1140 		panic("sonic_add_rxbuf");	/* XXX */
   1141 	}
   1142 
   1143 	bus_dmamap_sync(sc->sc_dmat, ds->ds_dmamap, 0,
   1144 	    ds->ds_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
   1145 
   1146 	SONIC_INIT_RXDESC(sc, idx);
   1147 
   1148 	return (0);
   1149 }
   1150 
   1151 static void
   1152 sonic_set_camentry(struct sonic_softc *sc, int entry, const uint8_t *enaddr)
   1153 {
   1154 
   1155 	if (sc->sc_32bit) {
   1156 		struct sonic_cda32 *cda = &sc->sc_cda32[entry];
   1157 
   1158 		cda->cda_entry = htosonic32(sc, entry);
   1159 		cda->cda_addr0 = htosonic32(sc, enaddr[0] | (enaddr[1] << 8));
   1160 		cda->cda_addr1 = htosonic32(sc, enaddr[2] | (enaddr[3] << 8));
   1161 		cda->cda_addr2 = htosonic32(sc, enaddr[4] | (enaddr[5] << 8));
   1162 	} else {
   1163 		struct sonic_cda16 *cda = &sc->sc_cda16[entry];
   1164 
   1165 		cda->cda_entry = htosonic16(sc, entry);
   1166 		cda->cda_addr0 = htosonic16(sc, enaddr[0] | (enaddr[1] << 8));
   1167 		cda->cda_addr1 = htosonic16(sc, enaddr[2] | (enaddr[3] << 8));
   1168 		cda->cda_addr2 = htosonic16(sc, enaddr[4] | (enaddr[5] << 8));
   1169 	}
   1170 }
   1171 
   1172 /*
   1173  * sonic_set_filter:
   1174  *
   1175  *	Set the SONIC receive filter.
   1176  */
   1177 void
   1178 sonic_set_filter(struct sonic_softc *sc)
   1179 {
   1180 	struct ethercom *ec = &sc->sc_ethercom;
   1181 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1182 	struct ether_multi *enm;
   1183 	struct ether_multistep step;
   1184 	int i, entry = 0;
   1185 	uint16_t camvalid = 0;
   1186 	uint16_t rcr = 0;
   1187 
   1188 	if (ifp->if_flags & IFF_BROADCAST)
   1189 		rcr |= RCR_BRD;
   1190 
   1191 	if (ifp->if_flags & IFF_PROMISC) {
   1192 		rcr |= RCR_PRO;
   1193 		goto allmulti;
   1194 	}
   1195 
   1196 	/* Put our station address in the first CAM slot. */
   1197 	sonic_set_camentry(sc, entry, LLADDR(ifp->if_sadl));
   1198 	camvalid |= (1U << entry);
   1199 	entry++;
   1200 
   1201 	/* Add the multicast addresses to the CAM. */
   1202 	ETHER_FIRST_MULTI(step, ec, enm);
   1203 	while (enm != NULL) {
   1204 		if (memcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
   1205 			/*
   1206 			 * We must listen to a range of multicast addresses.
   1207 			 * The only way to do this on the SONIC is to enable
   1208 			 * reception of all multicast packets.
   1209 			 */
   1210 			goto allmulti;
   1211 		}
   1212 
   1213 		if (entry == 16) {
   1214 			/*
   1215 			 * Out of CAM slots.  Have to enable reception
   1216 			 * of all multicast addresses.
   1217 			 */
   1218 			goto allmulti;
   1219 		}
   1220 
   1221 		sonic_set_camentry(sc, entry, enm->enm_addrlo);
   1222 		camvalid |= (1U << entry);
   1223 		entry++;
   1224 
   1225 		ETHER_NEXT_MULTI(step, enm);
   1226 	}
   1227 
   1228 	ifp->if_flags &= ~IFF_ALLMULTI;
   1229 	goto setit;
   1230 
   1231  allmulti:
   1232 	/* Use only the first CAM slot (station address). */
   1233 	camvalid = 0x0001;
   1234 	entry = 1;
   1235 	rcr |= RCR_AMC;
   1236 
   1237  setit:
   1238 	/* Load the CAM. */
   1239 	SONIC_CDCAMSYNC(sc, BUS_DMASYNC_PREWRITE);
   1240 	CSR_WRITE(sc, SONIC_CDP, SONIC_CDCAMADDR(sc) & 0xffff);
   1241 	CSR_WRITE(sc, SONIC_CDC, entry);
   1242 	CSR_WRITE(sc, SONIC_CR, CR_LCAM);
   1243 	for (i = 0; i < 10000; i++) {
   1244 		if ((CSR_READ(sc, SONIC_CR) & CR_LCAM) == 0)
   1245 			break;
   1246 		delay(2);
   1247 	}
   1248 	if (CSR_READ(sc, SONIC_CR) & CR_LCAM)
   1249 		printf("%s: CAM load failed\n", sc->sc_dev.dv_xname);
   1250 	SONIC_CDCAMSYNC(sc, BUS_DMASYNC_POSTWRITE);
   1251 
   1252 	/* Set the CAM enable resgiter. */
   1253 	CSR_WRITE(sc, SONIC_CER, camvalid);
   1254 
   1255 	/* Set the receive control register. */
   1256 	CSR_WRITE(sc, SONIC_RCR, rcr);
   1257 }
   1258