Home | History | Annotate | Line # | Download | only in ic
sgec.c revision 1.29
      1 /*      $NetBSD: sgec.c,v 1.29 2007/04/13 04:16:45 matt Exp $ */
      2 /*
      3  * Copyright (c) 1999 Ludd, University of Lule}, Sweden. All rights reserved.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions
      7  * are met:
      8  * 1. Redistributions of source code must retain the above copyright
      9  *    notice, this list of conditions and the following disclaimer.
     10  * 2. Redistributions in binary form must reproduce the above copyright
     11  *    notice, this list of conditions and the following disclaimer in the
     12  *    documentation and/or other materials provided with the distribution.
     13  * 3. All advertising materials mentioning features or use of this software
     14  *    must display the following acknowledgement:
     15  *      This product includes software developed at Ludd, University of
     16  *      Lule}, Sweden and its contributors.
     17  * 4. The name of the author may not be used to endorse or promote products
     18  *    derived from this software without specific prior written permission
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     21  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     22  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     23  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     25  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     29  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 /*
     33  * Driver for the SGEC (Second Generation Ethernet Controller), sitting
     34  * on for example the VAX 4000/300 (KA670).
     35  *
     36  * The SGEC looks like a mixture of the DEQNA and the TULIP. Fun toy.
     37  *
     38  * Even though the chip is capable to use virtual addresses (read the
     39  * System Page Table directly) this driver doesn't do so, and there
     40  * is no benefit in doing it either in NetBSD of today.
     41  *
     42  * Things that is still to do:
     43  *	Collect statistics.
     44  *	Use imperfect filtering when many multicast addresses.
     45  */
     46 
     47 #include <sys/cdefs.h>
     48 __KERNEL_RCSID(0, "$NetBSD: sgec.c,v 1.29 2007/04/13 04:16:45 matt Exp $");
     49 
     50 #include "opt_inet.h"
     51 #include "bpfilter.h"
     52 
     53 #include <sys/param.h>
     54 #include <sys/mbuf.h>
     55 #include <sys/socket.h>
     56 #include <sys/device.h>
     57 #include <sys/systm.h>
     58 #include <sys/sockio.h>
     59 
     60 #include <uvm/uvm_extern.h>
     61 
     62 #include <net/if.h>
     63 #include <net/if_ether.h>
     64 #include <net/if_dl.h>
     65 
     66 #include <netinet/in.h>
     67 #include <netinet/if_inarp.h>
     68 
     69 #if NBPFILTER > 0
     70 #include <net/bpf.h>
     71 #include <net/bpfdesc.h>
     72 #endif
     73 
     74 #include <machine/bus.h>
     75 
     76 #include <dev/ic/sgecreg.h>
     77 #include <dev/ic/sgecvar.h>
     78 
     79 static	void	zeinit(struct ze_softc *);
     80 static	void	zestart(struct ifnet *);
     81 static	int	zeioctl(struct ifnet *, u_long, void *);
     82 static	int	ze_add_rxbuf(struct ze_softc *, int);
     83 static	void	ze_setup(struct ze_softc *);
     84 static	void	zetimeout(struct ifnet *);
     85 static	int	zereset(struct ze_softc *);
     86 
     87 #define	ZE_WCSR(csr, val) \
     88 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, csr, val)
     89 #define	ZE_RCSR(csr) \
     90 	bus_space_read_4(sc->sc_iot, sc->sc_ioh, csr)
     91 
     92 /*
     93  * Interface exists: make available by filling in network interface
     94  * record.  System will initialize the interface when it is ready
     95  * to accept packets.
     96  */
     97 void
     98 sgec_attach(sc)
     99 	struct ze_softc *sc;
    100 {
    101 	struct	ifnet *ifp = (struct ifnet *)&sc->sc_if;
    102 	struct	ze_tdes *tp;
    103 	struct	ze_rdes *rp;
    104 	bus_dma_segment_t seg;
    105 	int i, rseg, error;
    106 
    107         /*
    108          * Allocate DMA safe memory for descriptors and setup memory.
    109          */
    110 	if ((error = bus_dmamem_alloc(sc->sc_dmat,
    111 	    sizeof(struct ze_cdata), PAGE_SIZE, 0, &seg, 1, &rseg,
    112 	    BUS_DMA_NOWAIT)) != 0) {
    113 		printf(": unable to allocate control data, error = %d\n",
    114 		    error);
    115 		goto fail_0;
    116 	}
    117 
    118 	if ((error = bus_dmamem_map(sc->sc_dmat, &seg, rseg,
    119 	    sizeof(struct ze_cdata), (void **)&sc->sc_zedata,
    120 	    BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) != 0) {
    121 		printf(": unable to map control data, error = %d\n", error);
    122 		goto fail_1;
    123 	}
    124 
    125 	if ((error = bus_dmamap_create(sc->sc_dmat,
    126 	    sizeof(struct ze_cdata), 1,
    127 	    sizeof(struct ze_cdata), 0, BUS_DMA_NOWAIT,
    128 	    &sc->sc_cmap)) != 0) {
    129 		printf(": unable to create control data DMA map, error = %d\n",
    130 		    error);
    131 		goto fail_2;
    132 	}
    133 
    134 	if ((error = bus_dmamap_load(sc->sc_dmat, sc->sc_cmap,
    135 	    sc->sc_zedata, sizeof(struct ze_cdata), NULL,
    136 	    BUS_DMA_NOWAIT)) != 0) {
    137 		printf(": unable to load control data DMA map, error = %d\n",
    138 		    error);
    139 		goto fail_3;
    140 	}
    141 
    142 	/*
    143 	 * Zero the newly allocated memory.
    144 	 */
    145 	memset(sc->sc_zedata, 0, sizeof(struct ze_cdata));
    146 	/*
    147 	 * Create the transmit descriptor DMA maps.
    148 	 */
    149 	for (i = 0; i < TXDESCS; i++) {
    150 		if ((error = bus_dmamap_create(sc->sc_dmat, MCLBYTES,
    151 		    TXDESCS - 1, MCLBYTES, 0, BUS_DMA_NOWAIT|BUS_DMA_ALLOCNOW,
    152 		    &sc->sc_xmtmap[i]))) {
    153 			printf(": unable to create tx DMA map %d, error = %d\n",
    154 			    i, error);
    155 			goto fail_4;
    156 		}
    157 	}
    158 
    159 	/*
    160 	 * Create receive buffer DMA maps.
    161 	 */
    162 	for (i = 0; i < RXDESCS; i++) {
    163 		if ((error = bus_dmamap_create(sc->sc_dmat, MCLBYTES, 1,
    164 		    MCLBYTES, 0, BUS_DMA_NOWAIT,
    165 		    &sc->sc_rcvmap[i]))) {
    166 			printf(": unable to create rx DMA map %d, error = %d\n",
    167 			    i, error);
    168 			goto fail_5;
    169 		}
    170 	}
    171 	/*
    172 	 * Pre-allocate the receive buffers.
    173 	 */
    174 	for (i = 0; i < RXDESCS; i++) {
    175 		if ((error = ze_add_rxbuf(sc, i)) != 0) {
    176 			printf(": unable to allocate or map rx buffer %d\n,"
    177 			    " error = %d\n", i, error);
    178 			goto fail_6;
    179 		}
    180 	}
    181 
    182 	/* For vmstat -i
    183 	 */
    184 	evcnt_attach_dynamic(&sc->sc_intrcnt, EVCNT_TYPE_INTR, NULL,
    185 		sc->sc_dev.dv_xname, "intr");
    186 
    187 	/*
    188 	 * Create ring loops of the buffer chains.
    189 	 * This is only done once.
    190 	 */
    191 	sc->sc_pzedata = (struct ze_cdata *)sc->sc_cmap->dm_segs[0].ds_addr;
    192 
    193 	rp = sc->sc_zedata->zc_recv;
    194 	rp[RXDESCS].ze_framelen = ZE_FRAMELEN_OW;
    195 	rp[RXDESCS].ze_rdes1 = ZE_RDES1_CA;
    196 	rp[RXDESCS].ze_bufaddr = (char *)sc->sc_pzedata->zc_recv;
    197 
    198 	tp = sc->sc_zedata->zc_xmit;
    199 	tp[TXDESCS].ze_tdr = ZE_TDR_OW;
    200 	tp[TXDESCS].ze_tdes1 = ZE_TDES1_CA;
    201 	tp[TXDESCS].ze_bufaddr = (char *)sc->sc_pzedata->zc_xmit;
    202 
    203 	if (zereset(sc))
    204 		return;
    205 
    206 	strcpy(ifp->if_xname, sc->sc_dev.dv_xname);
    207 	ifp->if_softc = sc;
    208 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
    209 	ifp->if_start = zestart;
    210 	ifp->if_ioctl = zeioctl;
    211 	ifp->if_watchdog = zetimeout;
    212 	IFQ_SET_READY(&ifp->if_snd);
    213 
    214 	/*
    215 	 * Attach the interface.
    216 	 */
    217 	if_attach(ifp);
    218 	ether_ifattach(ifp, sc->sc_enaddr);
    219 
    220 	printf("\n%s: hardware address %s\n", sc->sc_dev.dv_xname,
    221 	    ether_sprintf(sc->sc_enaddr));
    222 	return;
    223 
    224 	/*
    225 	 * Free any resources we've allocated during the failed attach
    226 	 * attempt.  Do this in reverse order and fall through.
    227 	 */
    228  fail_6:
    229 	for (i = 0; i < RXDESCS; i++) {
    230 		if (sc->sc_rxmbuf[i] != NULL) {
    231 			bus_dmamap_unload(sc->sc_dmat, sc->sc_xmtmap[i]);
    232 			m_freem(sc->sc_rxmbuf[i]);
    233 		}
    234 	}
    235  fail_5:
    236 	for (i = 0; i < RXDESCS; i++) {
    237 		if (sc->sc_xmtmap[i] != NULL)
    238 			bus_dmamap_destroy(sc->sc_dmat, sc->sc_xmtmap[i]);
    239 	}
    240  fail_4:
    241 	for (i = 0; i < TXDESCS; i++) {
    242 		if (sc->sc_rcvmap[i] != NULL)
    243 			bus_dmamap_destroy(sc->sc_dmat, sc->sc_rcvmap[i]);
    244 	}
    245 	bus_dmamap_unload(sc->sc_dmat, sc->sc_cmap);
    246  fail_3:
    247 	bus_dmamap_destroy(sc->sc_dmat, sc->sc_cmap);
    248  fail_2:
    249 	bus_dmamem_unmap(sc->sc_dmat, (void *)sc->sc_zedata,
    250 	    sizeof(struct ze_cdata));
    251  fail_1:
    252 	bus_dmamem_free(sc->sc_dmat, &seg, rseg);
    253  fail_0:
    254 	return;
    255 }
    256 
    257 /*
    258  * Initialization of interface.
    259  */
    260 void
    261 zeinit(sc)
    262 	struct ze_softc *sc;
    263 {
    264 	struct ifnet *ifp = (struct ifnet *)&sc->sc_if;
    265 	struct ze_cdata *zc = sc->sc_zedata;
    266 	int i;
    267 
    268 	/*
    269 	 * Reset the interface.
    270 	 */
    271 	if (zereset(sc))
    272 		return;
    273 
    274 	sc->sc_nexttx = sc->sc_inq = sc->sc_lastack = 0;
    275 	/*
    276 	 * Release and init transmit descriptors.
    277 	 */
    278 	for (i = 0; i < TXDESCS; i++) {
    279 		if (sc->sc_xmtmap[i]->dm_nsegs > 0)
    280 			bus_dmamap_unload(sc->sc_dmat, sc->sc_xmtmap[i]);
    281 		if (sc->sc_txmbuf[i]) {
    282 			m_freem(sc->sc_txmbuf[i]);
    283 			sc->sc_txmbuf[i] = 0;
    284 		}
    285 		zc->zc_xmit[i].ze_tdr = 0; /* Clear valid bit */
    286 	}
    287 
    288 
    289 	/*
    290 	 * Init receive descriptors.
    291 	 */
    292 	for (i = 0; i < RXDESCS; i++)
    293 		zc->zc_recv[i].ze_framelen = ZE_FRAMELEN_OW;
    294 	sc->sc_nextrx = 0;
    295 
    296 	ZE_WCSR(ZE_CSR6, ZE_NICSR6_IE|ZE_NICSR6_BL_8|ZE_NICSR6_ST|
    297 	    ZE_NICSR6_SR|ZE_NICSR6_DC);
    298 
    299 	ifp->if_flags |= IFF_RUNNING;
    300 	ifp->if_flags &= ~IFF_OACTIVE;
    301 
    302 	/*
    303 	 * Send a setup frame.
    304 	 * This will start the transmit machinery as well.
    305 	 */
    306 	ze_setup(sc);
    307 
    308 }
    309 
    310 /*
    311  * Start output on interface.
    312  */
    313 void
    314 zestart(ifp)
    315 	struct ifnet *ifp;
    316 {
    317 	struct ze_softc *sc = ifp->if_softc;
    318 	struct ze_cdata *zc = sc->sc_zedata;
    319 	paddr_t	buffer;
    320 	struct mbuf *m;
    321 	int nexttx, len, i, totlen, error;
    322 	int old_inq = sc->sc_inq;
    323 	short orword;
    324 	bus_dmamap_t map;
    325 
    326 	while (sc->sc_inq < (TXDESCS - 1)) {
    327 
    328 		if (sc->sc_setup) {
    329 			ze_setup(sc);
    330 			continue;
    331 		}
    332 		nexttx = sc->sc_nexttx;
    333 		IFQ_POLL(&sc->sc_if.if_snd, m);
    334 		if (m == 0)
    335 			goto out;
    336 		/*
    337 		 * Count number of mbufs in chain.
    338 		 * Always do DMA directly from mbufs, therefore the transmit
    339 		 * ring is really big.
    340 		 */
    341 		map = sc->sc_xmtmap[nexttx];
    342 		error = bus_dmamap_load_mbuf(sc->sc_dmat, map, m,
    343 		    BUS_DMA_WRITE);
    344 		if (error) {
    345 			printf("zestart: load_mbuf failed: %d", error);
    346 			goto out;
    347 		}
    348 
    349 		if (map->dm_nsegs >= TXDESCS)
    350 			panic("zestart"); /* XXX */
    351 
    352 		if ((map->dm_nsegs + sc->sc_inq) >= (TXDESCS - 1)) {
    353 			bus_dmamap_unload(sc->sc_dmat, map);
    354 			ifp->if_flags |= IFF_OACTIVE;
    355 			goto out;
    356 		}
    357 
    358 		/*
    359 		 * m now points to a mbuf chain that can be loaded.
    360 		 * Loop around and set it.
    361 		 */
    362 		totlen = 0;
    363 		orword = ZE_TDES1_FS;
    364 		for (i = 0; i < map->dm_nsegs; i++) {
    365 			buffer = map->dm_segs[i].ds_addr;
    366 			len = map->dm_segs[i].ds_len;
    367 
    368 			if (len == 0)
    369 				continue;
    370 
    371 			totlen += len;
    372 			/* Word alignment calc */
    373 			if (totlen == m->m_pkthdr.len) {
    374 				orword |= ZE_TDES1_LS | ZE_TDES1_IC;
    375 				sc->sc_txmbuf[nexttx] = m;
    376 			}
    377 			zc->zc_xmit[nexttx].ze_bufsize = len;
    378 			zc->zc_xmit[nexttx].ze_bufaddr = (char *)buffer;
    379 			zc->zc_xmit[nexttx].ze_tdes1 = orword;
    380 			zc->zc_xmit[nexttx].ze_tdr = ZE_TDR_OW;
    381 
    382 			if (++nexttx == TXDESCS)
    383 				nexttx = 0;
    384 			orword = 0;
    385 		}
    386 
    387 		sc->sc_inq += map->dm_nsegs;
    388 
    389 		IFQ_DEQUEUE(&ifp->if_snd, m);
    390 #ifdef DIAGNOSTIC
    391 		if (totlen != m->m_pkthdr.len)
    392 			panic("zestart: len fault");
    393 #endif
    394 
    395 		/*
    396 		 * Kick off the transmit logic, if it is stopped.
    397 		 */
    398 		if ((ZE_RCSR(ZE_CSR5) & ZE_NICSR5_TS) != ZE_NICSR5_TS_RUN)
    399 			ZE_WCSR(ZE_CSR1, -1);
    400 		sc->sc_nexttx = nexttx;
    401 	}
    402 	if (sc->sc_inq == (TXDESCS - 1))
    403 		ifp->if_flags |= IFF_OACTIVE;
    404 
    405 out:	if (old_inq < sc->sc_inq)
    406 		ifp->if_timer = 5; /* If transmit logic dies */
    407 }
    408 
    409 int
    410 sgec_intr(sc)
    411 	struct ze_softc *sc;
    412 {
    413 	struct ze_cdata *zc = sc->sc_zedata;
    414 	struct ifnet *ifp = &sc->sc_if;
    415 	struct mbuf *m;
    416 	int csr, len;
    417 
    418 	csr = ZE_RCSR(ZE_CSR5);
    419 	if ((csr & ZE_NICSR5_IS) == 0) /* Wasn't we */
    420 		return 0;
    421 	ZE_WCSR(ZE_CSR5, csr);
    422 
    423 	if (csr & ZE_NICSR5_RI) {
    424 		while ((zc->zc_recv[sc->sc_nextrx].ze_framelen &
    425 		    ZE_FRAMELEN_OW) == 0) {
    426 
    427 			ifp->if_ipackets++;
    428 			m = sc->sc_rxmbuf[sc->sc_nextrx];
    429 			len = zc->zc_recv[sc->sc_nextrx].ze_framelen;
    430 			ze_add_rxbuf(sc, sc->sc_nextrx);
    431 			if (++sc->sc_nextrx == RXDESCS)
    432 				sc->sc_nextrx = 0;
    433 			if (len < ETHER_MIN_LEN) {
    434 				ifp->if_ierrors++;
    435 				m_freem(m);
    436 			} else {
    437 				m->m_pkthdr.rcvif = ifp;
    438 				m->m_pkthdr.len = m->m_len =
    439 				    len - ETHER_CRC_LEN;
    440 #if NBPFILTER > 0
    441 				if (ifp->if_bpf)
    442 					bpf_mtap(ifp->if_bpf, m);
    443 #endif
    444 				(*ifp->if_input)(ifp, m);
    445 			}
    446 		}
    447 	}
    448 
    449 	if (csr & ZE_NICSR5_TI) {
    450 		int lastack = sc->sc_lastack;
    451 		while ((zc->zc_xmit[lastack].ze_tdr & ZE_TDR_OW) == 0) {
    452 			bus_dmamap_t map;
    453 			int nlastack;
    454 
    455 			if (lastack == sc->sc_nexttx)
    456 				break;
    457 
    458 			if ((zc->zc_xmit[lastack].ze_tdes1 & ZE_TDES1_DT) ==
    459 			    ZE_TDES1_DT_SETUP) {
    460 				if (++lastack == TXDESCS)
    461 					lastack = 0;
    462 				sc->sc_inq--;
    463 				continue;
    464 			}
    465 
    466 			KASSERT(zc->zc_xmit[lastack].ze_tdes1 & ZE_TDES1_FS);
    467 			map = sc->sc_xmtmap[lastack];
    468 			KASSERT(map->dm_nsegs > 0);
    469 			nlastack = (lastack + map->dm_nsegs - 1) % TXDESCS;
    470 			if (zc->zc_xmit[nlastack].ze_tdr & ZE_TDR_OW)
    471 				break;
    472 			lastack = nlastack;
    473 			sc->sc_inq -= map->dm_nsegs;
    474 			KASSERT(zc->zc_xmit[lastack].ze_tdes1 & ZE_TDES1_LS);
    475 			ifp->if_opackets++;
    476 			bus_dmamap_unload(sc->sc_dmat, map);
    477 			KASSERT(sc->sc_txmbuf[lastack]);
    478 #if NBPFILTER > 0
    479 			if (ifp->if_bpf)
    480 				bpf_mtap(ifp->if_bpf, sc->sc_txmbuf[lastack]);
    481 #endif
    482 			m_freem(sc->sc_txmbuf[lastack]);
    483 			sc->sc_txmbuf[lastack] = 0;
    484 			if (++lastack == TXDESCS)
    485 				lastack = 0;
    486 		}
    487 		sc->sc_lastack = lastack;
    488 		if (sc->sc_inq == 0)
    489 			ifp->if_timer = 0;
    490 		ifp->if_flags &= ~IFF_OACTIVE;
    491 		zestart(ifp); /* Put in more in queue */
    492 	}
    493 	return 1;
    494 }
    495 
    496 /*
    497  * Process an ioctl request.
    498  */
    499 int
    500 zeioctl(ifp, cmd, data)
    501 	struct ifnet *ifp;
    502 	u_long cmd;
    503 	void *data;
    504 {
    505 	struct ze_softc *sc = ifp->if_softc;
    506 	struct ifreq *ifr = (struct ifreq *)data;
    507 	struct ifaddr *ifa = (struct ifaddr *)data;
    508 	int s = splnet(), error = 0;
    509 
    510 	switch (cmd) {
    511 
    512 	case SIOCSIFADDR:
    513 		ifp->if_flags |= IFF_UP;
    514 		switch(ifa->ifa_addr->sa_family) {
    515 #ifdef INET
    516 		case AF_INET:
    517 			zeinit(sc);
    518 			arp_ifinit(ifp, ifa);
    519 			break;
    520 #endif
    521 		}
    522 		break;
    523 
    524 	case SIOCSIFFLAGS:
    525 		if ((ifp->if_flags & IFF_UP) == 0 &&
    526 		    (ifp->if_flags & IFF_RUNNING) != 0) {
    527 			/*
    528 			 * If interface is marked down and it is running,
    529 			 * stop it. (by disabling receive mechanism).
    530 			 */
    531 			ZE_WCSR(ZE_CSR6, ZE_RCSR(ZE_CSR6) &
    532 			    ~(ZE_NICSR6_ST|ZE_NICSR6_SR));
    533 			ifp->if_flags &= ~IFF_RUNNING;
    534 		} else if ((ifp->if_flags & IFF_UP) != 0 &&
    535 			   (ifp->if_flags & IFF_RUNNING) == 0) {
    536 			/*
    537 			 * If interface it marked up and it is stopped, then
    538 			 * start it.
    539 			 */
    540 			zeinit(sc);
    541 		} else if ((ifp->if_flags & IFF_UP) != 0) {
    542 			/*
    543 			 * Send a new setup packet to match any new changes.
    544 			 * (Like IFF_PROMISC etc)
    545 			 */
    546 			ze_setup(sc);
    547 		}
    548 		break;
    549 
    550 	case SIOCADDMULTI:
    551 	case SIOCDELMULTI:
    552 		/*
    553 		 * Update our multicast list.
    554 		 */
    555 		error = (cmd == SIOCADDMULTI) ?
    556 			ether_addmulti(ifr, &sc->sc_ec):
    557 			ether_delmulti(ifr, &sc->sc_ec);
    558 
    559 		if (error == ENETRESET) {
    560 			/*
    561 			 * Multicast list has changed; set the hardware filter
    562 			 * accordingly.
    563 			 */
    564 			if (ifp->if_flags & IFF_RUNNING)
    565 				ze_setup(sc);
    566 			error = 0;
    567 		}
    568 		break;
    569 
    570 	default:
    571 		error = EINVAL;
    572 
    573 	}
    574 	splx(s);
    575 	return (error);
    576 }
    577 
    578 /*
    579  * Add a receive buffer to the indicated descriptor.
    580  */
    581 int
    582 ze_add_rxbuf(sc, i)
    583 	struct ze_softc *sc;
    584 	int i;
    585 {
    586 	struct mbuf *m;
    587 	struct ze_rdes *rp;
    588 	int error;
    589 
    590 	MGETHDR(m, M_DONTWAIT, MT_DATA);
    591 	if (m == NULL)
    592 		return (ENOBUFS);
    593 
    594 	MCLAIM(m, &sc->sc_ec.ec_rx_mowner);
    595 	MCLGET(m, M_DONTWAIT);
    596 	if ((m->m_flags & M_EXT) == 0) {
    597 		m_freem(m);
    598 		return (ENOBUFS);
    599 	}
    600 
    601 	if (sc->sc_rxmbuf[i] != NULL)
    602 		bus_dmamap_unload(sc->sc_dmat, sc->sc_rcvmap[i]);
    603 
    604 	error = bus_dmamap_load(sc->sc_dmat, sc->sc_rcvmap[i],
    605 	    m->m_ext.ext_buf, m->m_ext.ext_size, NULL,
    606 	    BUS_DMA_READ|BUS_DMA_NOWAIT);
    607 	if (error)
    608 		panic("%s: can't load rx DMA map %d, error = %d",
    609 		    sc->sc_dev.dv_xname, i, error);
    610 	sc->sc_rxmbuf[i] = m;
    611 
    612 	bus_dmamap_sync(sc->sc_dmat, sc->sc_rcvmap[i], 0,
    613 	    sc->sc_rcvmap[i]->dm_mapsize, BUS_DMASYNC_PREREAD);
    614 
    615 	/*
    616 	 * We know that the mbuf cluster is page aligned. Also, be sure
    617 	 * that the IP header will be longword aligned.
    618 	 */
    619 	m->m_data += 2;
    620 	rp = &sc->sc_zedata->zc_recv[i];
    621 	rp->ze_bufsize = (m->m_ext.ext_size - 2);
    622 	rp->ze_bufaddr = (char *)sc->sc_rcvmap[i]->dm_segs[0].ds_addr + 2;
    623 	rp->ze_framelen = ZE_FRAMELEN_OW;
    624 
    625 	return (0);
    626 }
    627 
    628 /*
    629  * Create a setup packet and put in queue for sending.
    630  */
    631 void
    632 ze_setup(sc)
    633 	struct ze_softc *sc;
    634 {
    635 	struct ether_multi *enm;
    636 	struct ether_multistep step;
    637 	struct ze_cdata *zc = sc->sc_zedata;
    638 	struct ifnet *ifp = &sc->sc_if;
    639 	u_int8_t *enaddr = LLADDR(ifp->if_sadl);
    640 	int j, idx, reg;
    641 
    642 	if (sc->sc_inq == (TXDESCS - 1)) {
    643 		sc->sc_setup = 1;
    644 		return;
    645 	}
    646 	sc->sc_setup = 0;
    647 	/*
    648 	 * Init the setup packet with valid info.
    649 	 */
    650 	memset(zc->zc_setup, 0xff, sizeof(zc->zc_setup)); /* Broadcast */
    651 	memcpy(zc->zc_setup, enaddr, ETHER_ADDR_LEN);
    652 
    653 	/*
    654 	 * Multicast handling. The SGEC can handle up to 16 direct
    655 	 * ethernet addresses.
    656 	 */
    657 	j = 16;
    658 	ifp->if_flags &= ~IFF_ALLMULTI;
    659 	ETHER_FIRST_MULTI(step, &sc->sc_ec, enm);
    660 	while (enm != NULL) {
    661 		if (memcmp(enm->enm_addrlo, enm->enm_addrhi, 6)) {
    662 			ifp->if_flags |= IFF_ALLMULTI;
    663 			break;
    664 		}
    665 		memcpy(&zc->zc_setup[j], enm->enm_addrlo, ETHER_ADDR_LEN);
    666 		j += 8;
    667 		ETHER_NEXT_MULTI(step, enm);
    668 		if ((enm != NULL)&& (j == 128)) {
    669 			ifp->if_flags |= IFF_ALLMULTI;
    670 			break;
    671 		}
    672 	}
    673 
    674 	/*
    675 	 * ALLMULTI implies PROMISC in this driver.
    676 	 */
    677 	if (ifp->if_flags & IFF_ALLMULTI)
    678 		ifp->if_flags |= IFF_PROMISC;
    679 	else if (ifp->if_pcount == 0)
    680 		ifp->if_flags &= ~IFF_PROMISC;
    681 
    682 	/*
    683 	 * Fiddle with the receive logic.
    684 	 */
    685 	reg = ZE_RCSR(ZE_CSR6);
    686 	DELAY(10);
    687 	ZE_WCSR(ZE_CSR6, reg & ~ZE_NICSR6_SR); /* Stop rx */
    688 	reg &= ~ZE_NICSR6_AF;
    689 	if (ifp->if_flags & IFF_PROMISC)
    690 		reg |= ZE_NICSR6_AF_PROM;
    691 	else if (ifp->if_flags & IFF_ALLMULTI)
    692 		reg |= ZE_NICSR6_AF_ALLM;
    693 	DELAY(10);
    694 	ZE_WCSR(ZE_CSR6, reg);
    695 	/*
    696 	 * Only send a setup packet if needed.
    697 	 */
    698 	if ((ifp->if_flags & (IFF_PROMISC|IFF_ALLMULTI)) == 0) {
    699 		idx = sc->sc_nexttx;
    700 		zc->zc_xmit[idx].ze_tdes1 = ZE_TDES1_DT_SETUP;
    701 		zc->zc_xmit[idx].ze_bufsize = 128;
    702 		zc->zc_xmit[idx].ze_bufaddr = sc->sc_pzedata->zc_setup;
    703 		zc->zc_xmit[idx].ze_tdr = ZE_TDR_OW;
    704 
    705 		if ((ZE_RCSR(ZE_CSR5) & ZE_NICSR5_TS) != ZE_NICSR5_TS_RUN)
    706 			ZE_WCSR(ZE_CSR1, -1);
    707 
    708 		sc->sc_inq++;
    709 		if (++sc->sc_nexttx == TXDESCS)
    710 			sc->sc_nexttx = 0;
    711 	}
    712 }
    713 
    714 /*
    715  * Check for dead transmit logic.
    716  */
    717 void
    718 zetimeout(ifp)
    719 	struct ifnet *ifp;
    720 {
    721 	struct ze_softc *sc = ifp->if_softc;
    722 
    723 	if (sc->sc_inq == 0)
    724 		return;
    725 
    726 	printf("%s: xmit logic died, resetting...\n", sc->sc_dev.dv_xname);
    727 	/*
    728 	 * Do a reset of interface, to get it going again.
    729 	 * Will it work by just restart the transmit logic?
    730 	 */
    731 	zeinit(sc);
    732 }
    733 
    734 /*
    735  * Reset chip:
    736  * Set/reset the reset flag.
    737  *  Write interrupt vector.
    738  *  Write ring buffer addresses.
    739  *  Write SBR.
    740  */
    741 int
    742 zereset(sc)
    743 	struct ze_softc *sc;
    744 {
    745 	int reg, i;
    746 
    747 	ZE_WCSR(ZE_CSR6, ZE_NICSR6_RE);
    748 	DELAY(50000);
    749 	if (ZE_RCSR(ZE_CSR6) & ZE_NICSR5_SF) {
    750 		printf("%s: selftest failed\n", sc->sc_dev.dv_xname);
    751 		return 1;
    752 	}
    753 
    754 	/*
    755 	 * Get the vector that were set at match time, and remember it.
    756 	 * WHICH VECTOR TO USE? Take one unused. XXX
    757 	 * Funny way to set vector described in the programmers manual.
    758 	 */
    759 	reg = ZE_NICSR0_IPL14 | sc->sc_intvec | 0x1fff0003; /* SYNC/ASYNC??? */
    760 	i = 10;
    761 	do {
    762 		if (i-- == 0) {
    763 			printf("Failing SGEC CSR0 init\n");
    764 			return 1;
    765 		}
    766 		ZE_WCSR(ZE_CSR0, reg);
    767 	} while (ZE_RCSR(ZE_CSR0) != reg);
    768 
    769 	ZE_WCSR(ZE_CSR3, (vaddr_t)sc->sc_pzedata->zc_recv);
    770 	ZE_WCSR(ZE_CSR4, (vaddr_t)sc->sc_pzedata->zc_xmit);
    771 	return 0;
    772 }
    773