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