Home | History | Annotate | Line # | Download | only in pci
if_et.c revision 1.17.2.2
      1  1.17.2.2    martin /*	$NetBSD: if_et.c,v 1.17.2.2 2020/04/13 08:04:26 martin Exp $	*/
      2  1.17.2.2    martin /*	$OpenBSD: if_et.c,v 1.12 2008/07/11 09:29:02 kevlo $	*/
      3       1.1   jnemeth /*
      4       1.1   jnemeth  * Copyright (c) 2007 The DragonFly Project.  All rights reserved.
      5       1.7  christos  *
      6       1.1   jnemeth  * This code is derived from software contributed to The DragonFly Project
      7       1.1   jnemeth  * by Sepherosa Ziehau <sepherosa (at) gmail.com>
      8       1.7  christos  *
      9       1.1   jnemeth  * Redistribution and use in source and binary forms, with or without
     10       1.1   jnemeth  * modification, are permitted provided that the following conditions
     11       1.1   jnemeth  * are met:
     12       1.7  christos  *
     13       1.1   jnemeth  * 1. Redistributions of source code must retain the above copyright
     14       1.1   jnemeth  *    notice, this list of conditions and the following disclaimer.
     15       1.1   jnemeth  * 2. Redistributions in binary form must reproduce the above copyright
     16       1.1   jnemeth  *    notice, this list of conditions and the following disclaimer in
     17       1.1   jnemeth  *    the documentation and/or other materials provided with the
     18       1.1   jnemeth  *    distribution.
     19       1.1   jnemeth  * 3. Neither the name of The DragonFly Project nor the names of its
     20       1.1   jnemeth  *    contributors may be used to endorse or promote products derived
     21       1.1   jnemeth  *    from this software without specific, prior written permission.
     22       1.7  christos  *
     23       1.1   jnemeth  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     24       1.1   jnemeth  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     25       1.1   jnemeth  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
     26       1.1   jnemeth  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
     27       1.1   jnemeth  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
     28       1.1   jnemeth  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
     29       1.1   jnemeth  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     30       1.1   jnemeth  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     31       1.1   jnemeth  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     32       1.1   jnemeth  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
     33       1.1   jnemeth  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34       1.1   jnemeth  * SUCH DAMAGE.
     35       1.7  christos  *
     36       1.1   jnemeth  * $DragonFly: src/sys/dev/netif/et/if_et.c,v 1.1 2007/10/12 14:12:42 sephe Exp $
     37       1.1   jnemeth  */
     38       1.1   jnemeth 
     39       1.1   jnemeth #include <sys/cdefs.h>
     40  1.17.2.2    martin __KERNEL_RCSID(0, "$NetBSD: if_et.c,v 1.17.2.2 2020/04/13 08:04:26 martin Exp $");
     41       1.1   jnemeth 
     42       1.1   jnemeth #include "opt_inet.h"
     43       1.1   jnemeth #include "vlan.h"
     44       1.1   jnemeth 
     45       1.1   jnemeth #include <sys/param.h>
     46       1.1   jnemeth #include <sys/endian.h>
     47       1.1   jnemeth #include <sys/systm.h>
     48       1.1   jnemeth #include <sys/types.h>
     49       1.1   jnemeth #include <sys/sockio.h>
     50       1.1   jnemeth #include <sys/mbuf.h>
     51       1.1   jnemeth #include <sys/queue.h>
     52       1.1   jnemeth #include <sys/kernel.h>
     53       1.1   jnemeth #include <sys/device.h>
     54       1.1   jnemeth #include <sys/callout.h>
     55       1.1   jnemeth #include <sys/socket.h>
     56       1.1   jnemeth 
     57       1.2    dyoung #include <sys/bus.h>
     58       1.7  christos 
     59       1.1   jnemeth #include <net/if.h>
     60       1.1   jnemeth #include <net/if_dl.h>
     61       1.1   jnemeth #include <net/if_media.h>
     62       1.1   jnemeth #include <net/if_ether.h>
     63       1.1   jnemeth #include <net/if_arp.h>
     64       1.1   jnemeth 
     65       1.1   jnemeth #ifdef INET
     66       1.1   jnemeth #include <netinet/in.h>
     67       1.1   jnemeth #include <netinet/in_systm.h>
     68       1.1   jnemeth #include <netinet/in_var.h>
     69       1.1   jnemeth #include <netinet/ip.h>
     70       1.1   jnemeth #include <netinet/if_inarp.h>
     71       1.1   jnemeth #endif
     72       1.1   jnemeth 
     73       1.1   jnemeth #include <net/bpf.h>
     74       1.7  christos 
     75       1.1   jnemeth #include <dev/mii/mii.h>
     76       1.1   jnemeth #include <dev/mii/miivar.h>
     77       1.1   jnemeth 
     78       1.1   jnemeth #include <dev/pci/pcireg.h>
     79       1.1   jnemeth #include <dev/pci/pcivar.h>
     80       1.1   jnemeth #include <dev/pci/pcidevs.h>
     81       1.1   jnemeth 
     82       1.1   jnemeth #include <dev/pci/if_etreg.h>
     83       1.1   jnemeth 
     84  1.17.2.2    martin static int	et_match(device_t, cfdata_t, void *);
     85  1.17.2.2    martin static void	et_attach(device_t, device_t, void *);
     86  1.17.2.2    martin static int	et_detach(device_t, int);
     87  1.17.2.2    martin 
     88  1.17.2.2    martin static int	et_miibus_readreg(device_t, int, int, uint16_t *);
     89  1.17.2.2    martin static int	et_miibus_writereg(device_t, int, int, uint16_t);
     90  1.17.2.2    martin static void	et_miibus_statchg(struct ifnet *);
     91  1.17.2.2    martin 
     92  1.17.2.2    martin static int	et_init(struct ifnet *);
     93  1.17.2.2    martin static int	et_ioctl(struct ifnet *, u_long, void *);
     94  1.17.2.2    martin static void	et_start(struct ifnet *);
     95  1.17.2.2    martin static void	et_watchdog(struct ifnet *);
     96  1.17.2.2    martin static void	et_ifmedia_sts(struct ifnet *, struct ifmediareq *);
     97  1.17.2.2    martin 
     98  1.17.2.2    martin static int	et_intr(void *);
     99  1.17.2.2    martin static void	et_enable_intrs(struct et_softc *, uint32_t);
    100  1.17.2.2    martin static void	et_disable_intrs(struct et_softc *);
    101  1.17.2.2    martin static void	et_rxeof(struct et_softc *);
    102  1.17.2.2    martin static void	et_txeof(struct et_softc *);
    103  1.17.2.2    martin static void	et_txtick(void *);
    104  1.17.2.2    martin 
    105  1.17.2.2    martin static int	et_dma_alloc(struct et_softc *);
    106  1.17.2.2    martin static void	et_dma_free(struct et_softc *);
    107  1.17.2.2    martin static int	et_dma_mem_create(struct et_softc *, bus_size_t,
    108       1.1   jnemeth 	    void **, bus_addr_t *, bus_dmamap_t *, bus_dma_segment_t *);
    109  1.17.2.2    martin static void	et_dma_mem_destroy(struct et_softc *, void *, bus_dmamap_t);
    110  1.17.2.2    martin static int	et_dma_mbuf_create(struct et_softc *);
    111  1.17.2.2    martin static void	et_dma_mbuf_destroy(struct et_softc *, int, const int[]);
    112  1.17.2.2    martin 
    113  1.17.2.2    martin static int	et_init_tx_ring(struct et_softc *);
    114  1.17.2.2    martin static int	et_init_rx_ring(struct et_softc *);
    115  1.17.2.2    martin static void	et_free_tx_ring(struct et_softc *);
    116  1.17.2.2    martin static void	et_free_rx_ring(struct et_softc *);
    117  1.17.2.2    martin static int	et_encap(struct et_softc *, struct mbuf **);
    118  1.17.2.2    martin static int	et_newbuf(struct et_rxbuf_data *, int, int, int);
    119  1.17.2.2    martin static int	et_newbuf_cluster(struct et_rxbuf_data *, int, int);
    120  1.17.2.2    martin static int	et_newbuf_hdr(struct et_rxbuf_data *, int, int);
    121  1.17.2.2    martin 
    122  1.17.2.2    martin static void	et_stop(struct et_softc *);
    123  1.17.2.2    martin static int	et_chip_init(struct et_softc *);
    124  1.17.2.2    martin static void	et_chip_attach(struct et_softc *);
    125  1.17.2.2    martin static void	et_init_mac(struct et_softc *);
    126  1.17.2.2    martin static void	et_init_rxmac(struct et_softc *);
    127  1.17.2.2    martin static void	et_init_txmac(struct et_softc *);
    128  1.17.2.2    martin static int	et_init_rxdma(struct et_softc *);
    129  1.17.2.2    martin static int	et_init_txdma(struct et_softc *);
    130  1.17.2.2    martin static int	et_start_rxdma(struct et_softc *);
    131  1.17.2.2    martin static int	et_start_txdma(struct et_softc *);
    132  1.17.2.2    martin static int	et_stop_rxdma(struct et_softc *);
    133  1.17.2.2    martin static int	et_stop_txdma(struct et_softc *);
    134  1.17.2.2    martin static void	et_reset(struct et_softc *);
    135  1.17.2.2    martin static int	et_bus_config(struct et_softc *);
    136  1.17.2.2    martin static void	et_get_eaddr(struct et_softc *, uint8_t[]);
    137  1.17.2.2    martin static void	et_setmulti(struct et_softc *);
    138  1.17.2.2    martin static void	et_tick(void *);
    139       1.1   jnemeth 
    140       1.1   jnemeth static int	et_rx_intr_npkts = 32;
    141       1.1   jnemeth static int	et_rx_intr_delay = 20;		/* x10 usec */
    142       1.1   jnemeth static int	et_tx_intr_nsegs = 128;
    143       1.1   jnemeth static uint32_t	et_timer = 1000 * 1000 * 1000;	/* nanosec */
    144       1.1   jnemeth 
    145       1.1   jnemeth struct et_bsize {
    146       1.1   jnemeth 	int		bufsize;
    147       1.1   jnemeth 	et_newbuf_t	newbuf;
    148       1.1   jnemeth };
    149       1.1   jnemeth 
    150       1.1   jnemeth static const struct et_bsize	et_bufsize[ET_RX_NRING] = {
    151       1.1   jnemeth 	{ .bufsize = 0,	.newbuf = et_newbuf_hdr },
    152       1.1   jnemeth 	{ .bufsize = 0,	.newbuf = et_newbuf_cluster },
    153       1.1   jnemeth };
    154       1.1   jnemeth 
    155  1.17.2.2    martin static const struct et_product {
    156       1.1   jnemeth 	pci_vendor_id_t		vendor;
    157       1.1   jnemeth 	pci_product_id_t	product;
    158       1.1   jnemeth } et_devices[] = {
    159       1.1   jnemeth 	{ PCI_VENDOR_LUCENT, PCI_PRODUCT_LUCENT_ET1310 },
    160       1.1   jnemeth 	{ PCI_VENDOR_LUCENT, PCI_PRODUCT_LUCENT_ET1301 }
    161       1.1   jnemeth };
    162       1.1   jnemeth 
    163       1.1   jnemeth CFATTACH_DECL_NEW(et, sizeof(struct et_softc), et_match, et_attach, et_detach,
    164       1.1   jnemeth 	NULL);
    165       1.1   jnemeth 
    166  1.17.2.2    martin static int
    167       1.1   jnemeth et_match(device_t dev, cfdata_t match, void *aux)
    168       1.1   jnemeth {
    169       1.1   jnemeth 	struct pci_attach_args *pa = aux;
    170       1.1   jnemeth 	const struct et_product *ep;
    171       1.1   jnemeth 	int i;
    172       1.1   jnemeth 
    173       1.6    dyoung 	for (i = 0; i < __arraycount(et_devices); i++) {
    174       1.1   jnemeth 		ep = &et_devices[i];
    175       1.1   jnemeth 		if (PCI_VENDOR(pa->pa_id) == ep->vendor &&
    176       1.1   jnemeth 		    PCI_PRODUCT(pa->pa_id) == ep->product)
    177       1.1   jnemeth 			return 1;
    178       1.1   jnemeth 	}
    179       1.1   jnemeth 	return 0;
    180       1.1   jnemeth }
    181       1.1   jnemeth 
    182  1.17.2.2    martin static void
    183       1.1   jnemeth et_attach(device_t parent, device_t self, void *aux)
    184       1.1   jnemeth {
    185       1.1   jnemeth 	struct et_softc *sc = device_private(self);
    186       1.1   jnemeth 	struct pci_attach_args *pa = aux;
    187       1.1   jnemeth 	pci_chipset_tag_t pc = pa->pa_pc;
    188       1.1   jnemeth 	pci_intr_handle_t ih;
    189       1.1   jnemeth 	const char *intrstr;
    190       1.1   jnemeth 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    191  1.17.2.1  christos 	struct mii_data * const mii = &sc->sc_miibus;
    192  1.17.2.2    martin 	uint32_t pmcfg;
    193       1.1   jnemeth 	pcireg_t memtype;
    194       1.1   jnemeth 	int error;
    195       1.8  christos 	char intrbuf[PCI_INTRSTR_LEN];
    196       1.1   jnemeth 
    197       1.3  drochner 	pci_aprint_devinfo(pa, "Ethernet controller");
    198       1.1   jnemeth 
    199       1.1   jnemeth 	sc->sc_dev = self;
    200       1.1   jnemeth 
    201       1.1   jnemeth 	/*
    202       1.1   jnemeth 	 * Initialize tunables
    203       1.1   jnemeth 	 */
    204       1.1   jnemeth 	sc->sc_rx_intr_npkts = et_rx_intr_npkts;
    205       1.1   jnemeth 	sc->sc_rx_intr_delay = et_rx_intr_delay;
    206       1.1   jnemeth 	sc->sc_tx_intr_nsegs = et_tx_intr_nsegs;
    207       1.1   jnemeth 	sc->sc_timer = et_timer;
    208       1.1   jnemeth 
    209       1.1   jnemeth 	memtype = pci_mapreg_type(pa->pa_pc, pa->pa_tag, ET_PCIR_BAR);
    210       1.1   jnemeth 	if (pci_mapreg_map(pa, ET_PCIR_BAR, memtype, 0, &sc->sc_mem_bt,
    211       1.1   jnemeth 	    &sc->sc_mem_bh, NULL, &sc->sc_mem_size)) {
    212       1.1   jnemeth 		aprint_error_dev(self, "could not map mem space\n");
    213       1.1   jnemeth 		return;
    214       1.1   jnemeth 	}
    215       1.1   jnemeth 
    216       1.1   jnemeth 	if (pci_intr_map(pa, &ih) != 0) {
    217       1.1   jnemeth 		aprint_error_dev(self, "could not map interrupt\n");
    218       1.1   jnemeth 		goto fail;
    219       1.1   jnemeth 	}
    220       1.1   jnemeth 
    221       1.8  christos 	intrstr = pci_intr_string(pc, ih, intrbuf, sizeof(intrbuf));
    222  1.17.2.1  christos 	sc->sc_irq_handle = pci_intr_establish_xname(pc, ih, IPL_NET, et_intr,
    223  1.17.2.1  christos 	    sc, device_xname(self));
    224       1.1   jnemeth 	if (sc->sc_irq_handle == NULL) {
    225       1.1   jnemeth 		aprint_error_dev(self, "could not establish interrupt");
    226       1.1   jnemeth 		if (intrstr != NULL)
    227       1.1   jnemeth 			aprint_error(" at %s", intrstr);
    228       1.1   jnemeth 		aprint_error("\n");
    229       1.1   jnemeth 		goto fail;
    230       1.1   jnemeth 	}
    231       1.1   jnemeth 	aprint_normal_dev(self, "interrupting at %s\n", intrstr);
    232       1.1   jnemeth 
    233       1.1   jnemeth 	sc->sc_pct = pa->pa_pc;
    234       1.1   jnemeth 	sc->sc_pcitag = pa->pa_tag;
    235       1.1   jnemeth 
    236  1.17.2.2    martin 	if (pci_dma64_available(pa))
    237  1.17.2.2    martin 		sc->sc_dmat = pa->pa_dmat64;
    238  1.17.2.2    martin 	else
    239  1.17.2.2    martin 		sc->sc_dmat = pa->pa_dmat;
    240  1.17.2.2    martin 
    241  1.17.2.2    martin 	if (pa->pa_id == PCI_PRODUCT_LUCENT_ET1301)
    242  1.17.2.2    martin 		sc->sc_flags |= ET_FLAG_FASTETHER;
    243  1.17.2.2    martin 
    244       1.1   jnemeth 	error = et_bus_config(sc);
    245       1.1   jnemeth 	if (error)
    246       1.1   jnemeth 		goto fail;
    247       1.1   jnemeth 
    248       1.1   jnemeth 	et_get_eaddr(sc, sc->sc_enaddr);
    249       1.1   jnemeth 
    250       1.1   jnemeth 	aprint_normal_dev(self, "Ethernet address %s\n",
    251       1.1   jnemeth 	    ether_sprintf(sc->sc_enaddr));
    252       1.1   jnemeth 
    253  1.17.2.2    martin 	/* Take PHY out of COMA and enable clocks. */
    254  1.17.2.2    martin 	pmcfg = ET_PM_SYSCLK_GATE | ET_PM_TXCLK_GATE | ET_PM_RXCLK_GATE;
    255  1.17.2.2    martin 	if ((sc->sc_flags & ET_FLAG_FASTETHER) == 0)
    256  1.17.2.2    martin 		pmcfg |= EM_PM_GIGEPHY_ENB;
    257  1.17.2.2    martin 	CSR_WRITE_4(sc, ET_PM, pmcfg);
    258       1.1   jnemeth 
    259       1.1   jnemeth 	et_reset(sc);
    260       1.1   jnemeth 
    261       1.1   jnemeth 	et_disable_intrs(sc);
    262       1.1   jnemeth 
    263       1.1   jnemeth 	error = et_dma_alloc(sc);
    264       1.1   jnemeth 	if (error)
    265       1.1   jnemeth 		goto fail;
    266       1.1   jnemeth 
    267       1.1   jnemeth 	ifp->if_softc = sc;
    268       1.1   jnemeth 	ifp->if_mtu = ETHERMTU;
    269       1.1   jnemeth 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
    270       1.1   jnemeth 	ifp->if_init = et_init;
    271       1.1   jnemeth 	ifp->if_ioctl = et_ioctl;
    272       1.1   jnemeth 	ifp->if_start = et_start;
    273       1.1   jnemeth 	ifp->if_watchdog = et_watchdog;
    274       1.1   jnemeth 	IFQ_SET_MAXLEN(&ifp->if_snd, ET_TX_NDESC);
    275       1.1   jnemeth 	IFQ_SET_READY(&ifp->if_snd);
    276       1.1   jnemeth 	strlcpy(ifp->if_xname, device_xname(self), IFNAMSIZ);
    277       1.1   jnemeth 
    278       1.1   jnemeth 	et_chip_attach(sc);
    279       1.1   jnemeth 
    280  1.17.2.1  christos 	mii->mii_ifp = ifp;
    281  1.17.2.1  christos 	mii->mii_readreg = et_miibus_readreg;
    282  1.17.2.1  christos 	mii->mii_writereg = et_miibus_writereg;
    283  1.17.2.1  christos 	mii->mii_statchg = et_miibus_statchg;
    284       1.1   jnemeth 
    285  1.17.2.1  christos 	sc->sc_ethercom.ec_mii = mii;
    286  1.17.2.1  christos 	ifmedia_init(&mii->mii_media, 0, ether_mediachange,
    287  1.17.2.2    martin 	    et_ifmedia_sts);
    288  1.17.2.1  christos 	mii_attach(self, mii, 0xffffffff, MII_PHY_ANY, MII_OFFSET_ANY, 0);
    289  1.17.2.1  christos 	if (LIST_FIRST(&mii->mii_phys) == NULL) {
    290       1.1   jnemeth 		aprint_error_dev(self, "no PHY found!\n");
    291  1.17.2.1  christos 		ifmedia_add(&mii->mii_media, IFM_ETHER | IFM_MANUAL,
    292       1.1   jnemeth 		    0, NULL);
    293  1.17.2.1  christos 		ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_MANUAL);
    294       1.1   jnemeth 	} else
    295  1.17.2.1  christos 		ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO);
    296       1.1   jnemeth 
    297       1.1   jnemeth 	if_attach(ifp);
    298      1.13     ozaki 	if_deferred_start_init(ifp, NULL);
    299       1.1   jnemeth 	ether_ifattach(ifp, sc->sc_enaddr);
    300       1.1   jnemeth 
    301       1.1   jnemeth 	callout_init(&sc->sc_tick, 0);
    302       1.1   jnemeth 	callout_setfunc(&sc->sc_tick, et_tick, sc);
    303       1.1   jnemeth 	callout_init(&sc->sc_txtick, 0);
    304       1.1   jnemeth 	callout_setfunc(&sc->sc_txtick, et_txtick, sc);
    305       1.1   jnemeth 
    306       1.1   jnemeth 	if (pmf_device_register(self, NULL, NULL))
    307       1.1   jnemeth 		pmf_class_network_register(self, ifp);
    308       1.1   jnemeth 	else
    309       1.1   jnemeth 		aprint_error_dev(self, "couldn't establish power handler\n");
    310       1.1   jnemeth 
    311       1.1   jnemeth 	return;
    312       1.1   jnemeth 
    313       1.1   jnemeth fail:
    314       1.1   jnemeth 	et_dma_free(sc);
    315       1.1   jnemeth 	if (sc->sc_irq_handle != NULL) {
    316       1.1   jnemeth 		pci_intr_disestablish(sc->sc_pct, sc->sc_irq_handle);
    317       1.1   jnemeth 		sc->sc_irq_handle = NULL;
    318       1.1   jnemeth 	}
    319       1.1   jnemeth 	if (sc->sc_mem_size) {
    320       1.1   jnemeth 		bus_space_unmap(sc->sc_mem_bt, sc->sc_mem_bh, sc->sc_mem_size);
    321       1.1   jnemeth 		sc->sc_mem_size = 0;
    322       1.1   jnemeth 	}
    323       1.1   jnemeth }
    324       1.1   jnemeth 
    325  1.17.2.2    martin static int
    326       1.1   jnemeth et_detach(device_t self, int flags)
    327       1.1   jnemeth {
    328       1.1   jnemeth 	struct et_softc *sc = device_private(self);
    329       1.1   jnemeth 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    330       1.1   jnemeth 	int s;
    331       1.1   jnemeth 
    332       1.1   jnemeth 	pmf_device_deregister(self);
    333       1.1   jnemeth 	s = splnet();
    334       1.1   jnemeth 	et_stop(sc);
    335       1.1   jnemeth 	splx(s);
    336       1.1   jnemeth 
    337       1.1   jnemeth 	mii_detach(&sc->sc_miibus, MII_PHY_ANY, MII_OFFSET_ANY);
    338       1.1   jnemeth 
    339       1.1   jnemeth 	ether_ifdetach(ifp);
    340       1.1   jnemeth 	if_detach(ifp);
    341       1.1   jnemeth 	et_dma_free(sc);
    342       1.1   jnemeth 
    343  1.17.2.2    martin 	/* Delete all remaining media. */
    344  1.17.2.2    martin 	ifmedia_fini(&sc->sc_miibus.mii_media);
    345  1.17.2.2    martin 
    346       1.1   jnemeth 	if (sc->sc_irq_handle != NULL) {
    347       1.1   jnemeth 		pci_intr_disestablish(sc->sc_pct, sc->sc_irq_handle);
    348       1.1   jnemeth 		sc->sc_irq_handle = NULL;
    349       1.1   jnemeth 	}
    350       1.1   jnemeth 
    351       1.1   jnemeth 	if (sc->sc_mem_size) {
    352       1.1   jnemeth 		bus_space_unmap(sc->sc_mem_bt, sc->sc_mem_bh, sc->sc_mem_size);
    353       1.1   jnemeth 		sc->sc_mem_size = 0;
    354       1.1   jnemeth 	}
    355       1.1   jnemeth 
    356       1.1   jnemeth 	return 0;
    357       1.1   jnemeth }
    358       1.1   jnemeth 
    359  1.17.2.2    martin #if 0 /* XXX XXX XXX UNUSED */
    360  1.17.2.2    martin static int
    361       1.1   jnemeth et_shutdown(device_t self)
    362       1.1   jnemeth {
    363       1.1   jnemeth 	struct et_softc *sc = device_private(self);
    364       1.1   jnemeth 	int s;
    365       1.1   jnemeth 
    366       1.1   jnemeth 	s = splnet();
    367       1.1   jnemeth 	et_stop(sc);
    368       1.1   jnemeth 	splx(s);
    369       1.1   jnemeth 
    370       1.1   jnemeth 	return 0;
    371       1.1   jnemeth }
    372  1.17.2.2    martin #endif
    373       1.1   jnemeth 
    374  1.17.2.2    martin static int
    375  1.17.2.1  christos et_miibus_readreg(device_t dev, int phy, int reg, uint16_t *val)
    376       1.1   jnemeth {
    377       1.1   jnemeth 	struct et_softc *sc = device_private(dev);
    378  1.17.2.1  christos 	uint32_t data;
    379       1.1   jnemeth 	int i, ret;
    380       1.1   jnemeth 
    381       1.1   jnemeth 	/* Stop any pending operations */
    382       1.1   jnemeth 	CSR_WRITE_4(sc, ET_MII_CMD, 0);
    383       1.1   jnemeth 
    384  1.17.2.1  christos 	data = __SHIFTIN(phy, ET_MII_ADDR_PHY) |
    385       1.1   jnemeth 	      __SHIFTIN(reg, ET_MII_ADDR_REG);
    386  1.17.2.1  christos 	CSR_WRITE_4(sc, ET_MII_ADDR, data);
    387       1.1   jnemeth 
    388       1.1   jnemeth 	/* Start reading */
    389       1.1   jnemeth 	CSR_WRITE_4(sc, ET_MII_CMD, ET_MII_CMD_READ);
    390       1.1   jnemeth 
    391       1.1   jnemeth #define NRETRY	50
    392       1.1   jnemeth 
    393       1.1   jnemeth 	for (i = 0; i < NRETRY; ++i) {
    394  1.17.2.1  christos 		data = CSR_READ_4(sc, ET_MII_IND);
    395  1.17.2.1  christos 		if ((data & (ET_MII_IND_BUSY | ET_MII_IND_INVALID)) == 0)
    396       1.1   jnemeth 			break;
    397       1.1   jnemeth 		DELAY(50);
    398       1.1   jnemeth 	}
    399       1.1   jnemeth 	if (i == NRETRY) {
    400       1.1   jnemeth 		aprint_error_dev(sc->sc_dev, "read phy %d, reg %d timed out\n",
    401       1.1   jnemeth 		    phy, reg);
    402  1.17.2.1  christos 		ret = ETIMEDOUT;
    403       1.1   jnemeth 		goto back;
    404       1.1   jnemeth 	}
    405       1.1   jnemeth 
    406       1.1   jnemeth #undef NRETRY
    407       1.1   jnemeth 
    408  1.17.2.1  christos 	data = CSR_READ_4(sc, ET_MII_STAT);
    409  1.17.2.1  christos 	*val = __SHIFTOUT(data, ET_MII_STAT_VALUE);
    410  1.17.2.1  christos 	ret = 0;
    411       1.1   jnemeth 
    412       1.1   jnemeth back:
    413       1.1   jnemeth 	/* Make sure that the current operation is stopped */
    414       1.1   jnemeth 	CSR_WRITE_4(sc, ET_MII_CMD, 0);
    415       1.1   jnemeth 	return ret;
    416       1.1   jnemeth }
    417       1.1   jnemeth 
    418  1.17.2.2    martin static int
    419  1.17.2.1  christos et_miibus_writereg(device_t dev, int phy, int reg, uint16_t val)
    420       1.1   jnemeth {
    421       1.1   jnemeth 	struct et_softc *sc = device_private(dev);
    422  1.17.2.1  christos 	uint32_t data;
    423  1.17.2.1  christos 	uint16_t tmp;
    424  1.17.2.1  christos 	int rv = 0;
    425       1.1   jnemeth 	int i;
    426       1.1   jnemeth 
    427       1.1   jnemeth 	/* Stop any pending operations */
    428       1.1   jnemeth 	CSR_WRITE_4(sc, ET_MII_CMD, 0);
    429       1.1   jnemeth 
    430  1.17.2.1  christos 	data = __SHIFTIN(phy, ET_MII_ADDR_PHY) |
    431       1.1   jnemeth 	      __SHIFTIN(reg, ET_MII_ADDR_REG);
    432  1.17.2.1  christos 	CSR_WRITE_4(sc, ET_MII_ADDR, data);
    433       1.1   jnemeth 
    434       1.1   jnemeth 	/* Start writing */
    435  1.17.2.1  christos 	CSR_WRITE_4(sc, ET_MII_CTRL, __SHIFTIN(val, ET_MII_CTRL_VALUE));
    436       1.1   jnemeth 
    437       1.1   jnemeth #define NRETRY 100
    438       1.1   jnemeth 
    439       1.1   jnemeth 	for (i = 0; i < NRETRY; ++i) {
    440  1.17.2.1  christos 		data = CSR_READ_4(sc, ET_MII_IND);
    441  1.17.2.1  christos 		if ((data & ET_MII_IND_BUSY) == 0)
    442       1.1   jnemeth 			break;
    443       1.1   jnemeth 		DELAY(50);
    444       1.1   jnemeth 	}
    445       1.1   jnemeth 	if (i == NRETRY) {
    446       1.1   jnemeth 		aprint_error_dev(sc->sc_dev, "write phy %d, reg %d timed out\n",
    447       1.1   jnemeth 		    phy, reg);
    448  1.17.2.1  christos 		et_miibus_readreg(dev, phy, reg, &tmp);
    449  1.17.2.1  christos 		rv = ETIMEDOUT;
    450       1.1   jnemeth 	}
    451       1.1   jnemeth 
    452       1.1   jnemeth #undef NRETRY
    453       1.1   jnemeth 
    454       1.1   jnemeth 	/* Make sure that the current operation is stopped */
    455       1.1   jnemeth 	CSR_WRITE_4(sc, ET_MII_CMD, 0);
    456  1.17.2.1  christos 
    457  1.17.2.1  christos 	return rv;
    458       1.1   jnemeth }
    459       1.1   jnemeth 
    460  1.17.2.2    martin static void
    461       1.4      matt et_miibus_statchg(struct ifnet *ifp)
    462       1.1   jnemeth {
    463       1.4      matt 	struct et_softc *sc = ifp->if_softc;
    464       1.1   jnemeth 	struct mii_data *mii = &sc->sc_miibus;
    465  1.17.2.2    martin 	uint32_t cfg1, cfg2, ctrl;
    466  1.17.2.2    martin 	int i;
    467       1.1   jnemeth 
    468  1.17.2.2    martin 	sc->sc_flags &= ~ET_FLAG_LINK;
    469  1.17.2.2    martin 	if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID)) ==
    470  1.17.2.2    martin 	    (IFM_ACTIVE | IFM_AVALID)) {
    471  1.17.2.2    martin 		switch (IFM_SUBTYPE(mii->mii_media_active)) {
    472  1.17.2.2    martin 		case IFM_10_T:
    473  1.17.2.2    martin 		case IFM_100_TX:
    474  1.17.2.2    martin 			sc->sc_flags |= ET_FLAG_LINK;
    475  1.17.2.2    martin 			break;
    476  1.17.2.2    martin 		case IFM_1000_T:
    477  1.17.2.2    martin 			if ((sc->sc_flags & ET_FLAG_FASTETHER) == 0)
    478  1.17.2.2    martin 				sc->sc_flags |= ET_FLAG_LINK;
    479  1.17.2.2    martin 			break;
    480  1.17.2.2    martin 		}
    481  1.17.2.2    martin 	}
    482  1.17.2.2    martin 
    483  1.17.2.2    martin 	/* XXX Stop TX/RX MAC? */
    484  1.17.2.2    martin 	if ((sc->sc_flags & ET_FLAG_LINK) == 0)
    485  1.17.2.2    martin 		return;
    486  1.17.2.2    martin 
    487  1.17.2.2    martin 	/* Program MACs with resolved speed/duplex/flow-control. */
    488  1.17.2.2    martin 	ctrl = CSR_READ_4(sc, ET_MAC_CTRL);
    489  1.17.2.2    martin 	ctrl &= ~(ET_MAC_CTRL_GHDX | ET_MAC_CTRL_MODE_MII);
    490  1.17.2.2    martin 	cfg1 = CSR_READ_4(sc, ET_MAC_CFG1);
    491  1.17.2.2    martin 	cfg1 &= ~(ET_MAC_CFG1_TXFLOW | ET_MAC_CFG1_RXFLOW |
    492  1.17.2.2    martin 	    ET_MAC_CFG1_LOOPBACK);
    493       1.1   jnemeth 	cfg2 = CSR_READ_4(sc, ET_MAC_CFG2);
    494       1.1   jnemeth 	cfg2 &= ~(ET_MAC_CFG2_MODE_MII | ET_MAC_CFG2_MODE_GMII |
    495  1.17.2.2    martin 	    ET_MAC_CFG2_FDX | ET_MAC_CFG2_BIGFRM);
    496       1.1   jnemeth 	cfg2 |= ET_MAC_CFG2_LENCHK | ET_MAC_CFG2_CRC | ET_MAC_CFG2_PADCRC |
    497  1.17.2.2    martin 	    __SHIFTIN(7, ET_MAC_CFG2_PREAMBLE_LEN);
    498       1.1   jnemeth 
    499       1.1   jnemeth 
    500  1.17.2.2    martin 	if (IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_T)
    501       1.1   jnemeth 		cfg2 |= ET_MAC_CFG2_MODE_GMII;
    502  1.17.2.2    martin 	else {
    503       1.1   jnemeth 		cfg2 |= ET_MAC_CFG2_MODE_MII;
    504       1.1   jnemeth 		ctrl |= ET_MAC_CTRL_MODE_MII;
    505       1.1   jnemeth 	}
    506       1.1   jnemeth 
    507  1.17.2.2    martin 	if (IFM_OPTIONS(mii->mii_media_active) & IFM_FDX) {
    508       1.1   jnemeth 		cfg2 |= ET_MAC_CFG2_FDX;
    509  1.17.2.2    martin 		/*
    510  1.17.2.2    martin 		 * Controller lacks automatic TX pause frame
    511  1.17.2.2    martin 		 * generation so it should be handled by driver.
    512  1.17.2.2    martin 		 * Even though driver can send pause frame with
    513  1.17.2.2    martin 		 * arbitrary pause time, controller does not
    514  1.17.2.2    martin 		 * provide a way that tells how many free RX
    515  1.17.2.2    martin 		 * buffers are available in controller.  This
    516  1.17.2.2    martin 		 * limitation makes it hard to generate XON frame
    517  1.17.2.2    martin 		 * in time on driver side so don't enable TX flow
    518  1.17.2.2    martin 		 * control.
    519  1.17.2.2    martin 		 */
    520  1.17.2.2    martin #ifdef notyet
    521  1.17.2.2    martin 		if (IFM_OPTIONS(mii->mii_media_active) & IFM_ETH_TXPAUSE)
    522  1.17.2.2    martin 			cfg1 |= ET_MAC_CFG1_TXFLOW;
    523  1.17.2.2    martin #endif
    524  1.17.2.2    martin 		if (IFM_OPTIONS(mii->mii_media_active) & IFM_ETH_RXPAUSE)
    525  1.17.2.2    martin 			cfg1 |= ET_MAC_CFG1_RXFLOW;
    526  1.17.2.2    martin 	} else
    527       1.1   jnemeth 		ctrl |= ET_MAC_CTRL_GHDX;
    528       1.1   jnemeth 
    529       1.1   jnemeth 	CSR_WRITE_4(sc, ET_MAC_CTRL, ctrl);
    530       1.1   jnemeth 	CSR_WRITE_4(sc, ET_MAC_CFG2, cfg2);
    531  1.17.2.2    martin 	cfg1 |= ET_MAC_CFG1_TXEN | ET_MAC_CFG1_RXEN;
    532  1.17.2.2    martin 	CSR_WRITE_4(sc, ET_MAC_CFG1, cfg1);
    533  1.17.2.2    martin 
    534  1.17.2.2    martin #define NRETRY	100
    535  1.17.2.2    martin 
    536  1.17.2.2    martin 	for (i = 0; i < NRETRY; ++i) {
    537  1.17.2.2    martin 		cfg1 = CSR_READ_4(sc, ET_MAC_CFG1);
    538  1.17.2.2    martin 		if ((cfg1 & (ET_MAC_CFG1_SYNC_TXEN | ET_MAC_CFG1_SYNC_RXEN)) ==
    539  1.17.2.2    martin 		    (ET_MAC_CFG1_SYNC_TXEN | ET_MAC_CFG1_SYNC_RXEN))
    540  1.17.2.2    martin 			break;
    541  1.17.2.2    martin 
    542  1.17.2.2    martin 		DELAY(10);
    543  1.17.2.2    martin 	}
    544  1.17.2.2    martin 	/* Note: Timeout always happens when cable is not plugged in. */
    545  1.17.2.2    martin 
    546  1.17.2.2    martin 	sc->sc_flags |= ET_FLAG_TXRX_ENABLED;
    547  1.17.2.2    martin 
    548  1.17.2.2    martin #undef NRETRY
    549  1.17.2.2    martin }
    550  1.17.2.2    martin 
    551  1.17.2.2    martin static void
    552  1.17.2.2    martin et_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
    553  1.17.2.2    martin {
    554  1.17.2.2    martin 	struct et_softc *sc;
    555  1.17.2.2    martin 	struct mii_data *mii;
    556  1.17.2.2    martin 
    557  1.17.2.2    martin 	sc = ifp->if_softc;
    558  1.17.2.2    martin 	mii = &sc->sc_miibus;
    559  1.17.2.2    martin 	mii_pollstat(mii);
    560  1.17.2.2    martin 	ifmr->ifm_active = mii->mii_media_active;
    561  1.17.2.2    martin 	ifmr->ifm_status = mii->mii_media_status;
    562       1.1   jnemeth }
    563       1.1   jnemeth 
    564  1.17.2.2    martin static void
    565       1.1   jnemeth et_stop(struct et_softc *sc)
    566       1.1   jnemeth {
    567       1.1   jnemeth 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    568       1.1   jnemeth 
    569       1.1   jnemeth 	callout_stop(&sc->sc_tick);
    570       1.1   jnemeth 	callout_stop(&sc->sc_txtick);
    571       1.1   jnemeth 
    572       1.1   jnemeth 	et_stop_rxdma(sc);
    573       1.1   jnemeth 	et_stop_txdma(sc);
    574       1.1   jnemeth 
    575       1.1   jnemeth 	et_disable_intrs(sc);
    576       1.1   jnemeth 
    577       1.1   jnemeth 	et_free_tx_ring(sc);
    578       1.1   jnemeth 	et_free_rx_ring(sc);
    579       1.1   jnemeth 
    580       1.1   jnemeth 	et_reset(sc);
    581       1.1   jnemeth 
    582       1.1   jnemeth 	sc->sc_tx = 0;
    583       1.1   jnemeth 	sc->sc_tx_intr = 0;
    584  1.17.2.2    martin 	sc->sc_flags &= ~ET_FLAG_TXRX_ENABLED;
    585       1.1   jnemeth 
    586       1.1   jnemeth 	ifp->if_timer = 0;
    587       1.1   jnemeth 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
    588       1.1   jnemeth }
    589       1.1   jnemeth 
    590  1.17.2.2    martin static int
    591       1.1   jnemeth et_bus_config(struct et_softc *sc)
    592       1.1   jnemeth {
    593       1.1   jnemeth 	uint32_t val; //, max_plsz;
    594       1.1   jnemeth //	uint16_t ack_latency, replay_timer;
    595       1.1   jnemeth 
    596       1.1   jnemeth 	/*
    597       1.1   jnemeth 	 * Test whether EEPROM is valid
    598       1.1   jnemeth 	 * NOTE: Read twice to get the correct value
    599       1.1   jnemeth 	 */
    600       1.1   jnemeth 	pci_conf_read(sc->sc_pct, sc->sc_pcitag, ET_PCIR_EEPROM_MISC);
    601       1.1   jnemeth 	val = pci_conf_read(sc->sc_pct, sc->sc_pcitag, ET_PCIR_EEPROM_MISC);
    602       1.7  christos 
    603       1.1   jnemeth 	if (val & ET_PCIM_EEPROM_STATUS_ERROR) {
    604       1.1   jnemeth 		aprint_error_dev(sc->sc_dev, "EEPROM status error 0x%02x\n", val);
    605       1.1   jnemeth 		return ENXIO;
    606       1.1   jnemeth 	}
    607       1.1   jnemeth 
    608       1.1   jnemeth 	/* TODO: LED */
    609       1.1   jnemeth #if 0
    610       1.1   jnemeth 	/*
    611       1.1   jnemeth 	 * Configure ACK latency and replay timer according to
    612       1.1   jnemeth 	 * max playload size
    613       1.1   jnemeth 	 */
    614       1.1   jnemeth 	val = pci_conf_read(sc->sc_pct, sc->sc_pcitag, ET_PCIR_DEVICE_CAPS);
    615       1.1   jnemeth 	max_plsz = val & ET_PCIM_DEVICE_CAPS_MAX_PLSZ;
    616       1.1   jnemeth 
    617       1.1   jnemeth 	switch (max_plsz) {
    618       1.1   jnemeth 	case ET_PCIV_DEVICE_CAPS_PLSZ_128:
    619       1.1   jnemeth 		ack_latency = ET_PCIV_ACK_LATENCY_128;
    620       1.1   jnemeth 		replay_timer = ET_PCIV_REPLAY_TIMER_128;
    621       1.1   jnemeth 		break;
    622       1.1   jnemeth 
    623       1.1   jnemeth 	case ET_PCIV_DEVICE_CAPS_PLSZ_256:
    624       1.1   jnemeth 		ack_latency = ET_PCIV_ACK_LATENCY_256;
    625       1.1   jnemeth 		replay_timer = ET_PCIV_REPLAY_TIMER_256;
    626       1.1   jnemeth 		break;
    627       1.1   jnemeth 
    628       1.1   jnemeth 	default:
    629       1.1   jnemeth 		ack_latency = pci_conf_read(sc->sc_pct, sc->sc_pcitag,
    630       1.1   jnemeth 		    ET_PCIR_ACK_LATENCY) >> 16;
    631       1.1   jnemeth 		replay_timer = pci_conf_read(sc->sc_pct, sc->sc_pcitag,
    632       1.1   jnemeth 		    ET_PCIR_REPLAY_TIMER) >> 16;
    633       1.1   jnemeth 		aprint_normal_dev(sc->sc_dev, "ack latency %u, replay timer %u\n",
    634       1.1   jnemeth 		    ack_latency, replay_timer);
    635       1.1   jnemeth 		break;
    636       1.1   jnemeth 	}
    637       1.1   jnemeth 	if (ack_latency != 0) {
    638       1.1   jnemeth 		pci_conf_write(sc->sc_pct, sc->sc_pcitag,
    639       1.1   jnemeth 		    ET_PCIR_ACK_LATENCY, ack_latency << 16);
    640       1.1   jnemeth 		pci_conf_write(sc->sc_pct, sc->sc_pcitag,
    641       1.1   jnemeth 		    ET_PCIR_REPLAY_TIMER, replay_timer << 16);
    642       1.1   jnemeth 	}
    643       1.1   jnemeth 
    644       1.1   jnemeth 	/*
    645       1.1   jnemeth 	 * Set L0s and L1 latency timer to 2us
    646       1.1   jnemeth 	 */
    647       1.1   jnemeth 	val = ET_PCIV_L0S_LATENCY(2) | ET_PCIV_L1_LATENCY(2);
    648       1.1   jnemeth 	pci_conf_write(sc->sc_pct, sc->sc_pcitag, ET_PCIR_L0S_L1_LATENCY,
    649       1.1   jnemeth 	    val << 24);
    650       1.1   jnemeth 
    651       1.1   jnemeth 	/*
    652       1.1   jnemeth 	 * Set max read request size to 2048 bytes
    653       1.1   jnemeth 	 */
    654       1.1   jnemeth 	val = pci_conf_read(sc->sc_pct, sc->sc_pcitag,
    655       1.1   jnemeth 	    ET_PCIR_DEVICE_CTRL) >> 16;
    656       1.1   jnemeth 	val &= ~ET_PCIM_DEVICE_CTRL_MAX_RRSZ;
    657       1.1   jnemeth 	val |= ET_PCIV_DEVICE_CTRL_RRSZ_2K;
    658       1.1   jnemeth 	pci_conf_write(sc->sc_pct, sc->sc_pcitag, ET_PCIR_DEVICE_CTRL,
    659       1.1   jnemeth 	    val << 16);
    660       1.1   jnemeth #endif
    661       1.1   jnemeth 
    662       1.1   jnemeth 	return 0;
    663       1.1   jnemeth }
    664       1.1   jnemeth 
    665  1.17.2.2    martin static void
    666       1.1   jnemeth et_get_eaddr(struct et_softc *sc, uint8_t eaddr[])
    667       1.1   jnemeth {
    668       1.1   jnemeth 	uint32_t r;
    669       1.1   jnemeth 
    670       1.1   jnemeth 	r = pci_conf_read(sc->sc_pct, sc->sc_pcitag, ET_PCIR_MACADDR_LO);
    671       1.1   jnemeth 	eaddr[0] = r & 0xff;
    672       1.1   jnemeth 	eaddr[1] = (r >> 8) & 0xff;
    673       1.1   jnemeth 	eaddr[2] = (r >> 16) & 0xff;
    674       1.1   jnemeth 	eaddr[3] = (r >> 24) & 0xff;
    675       1.1   jnemeth 	r = pci_conf_read(sc->sc_pct, sc->sc_pcitag, ET_PCIR_MACADDR_HI);
    676       1.1   jnemeth 	eaddr[4] = r & 0xff;
    677       1.1   jnemeth 	eaddr[5] = (r >> 8) & 0xff;
    678       1.1   jnemeth }
    679       1.1   jnemeth 
    680  1.17.2.2    martin static void
    681       1.1   jnemeth et_reset(struct et_softc *sc)
    682       1.1   jnemeth {
    683  1.17.2.2    martin 
    684       1.1   jnemeth 	CSR_WRITE_4(sc, ET_MAC_CFG1,
    685       1.1   jnemeth 		    ET_MAC_CFG1_RST_TXFUNC | ET_MAC_CFG1_RST_RXFUNC |
    686       1.1   jnemeth 		    ET_MAC_CFG1_RST_TXMC | ET_MAC_CFG1_RST_RXMC |
    687       1.1   jnemeth 		    ET_MAC_CFG1_SIM_RST | ET_MAC_CFG1_SOFT_RST);
    688       1.1   jnemeth 
    689       1.1   jnemeth 	CSR_WRITE_4(sc, ET_SWRST,
    690       1.1   jnemeth 		    ET_SWRST_TXDMA | ET_SWRST_RXDMA |
    691       1.1   jnemeth 		    ET_SWRST_TXMAC | ET_SWRST_RXMAC |
    692       1.1   jnemeth 		    ET_SWRST_MAC | ET_SWRST_MAC_STAT | ET_SWRST_MMC);
    693       1.1   jnemeth 
    694       1.1   jnemeth 	CSR_WRITE_4(sc, ET_MAC_CFG1,
    695       1.1   jnemeth 		    ET_MAC_CFG1_RST_TXFUNC | ET_MAC_CFG1_RST_RXFUNC |
    696       1.1   jnemeth 		    ET_MAC_CFG1_RST_TXMC | ET_MAC_CFG1_RST_RXMC);
    697       1.1   jnemeth 	CSR_WRITE_4(sc, ET_MAC_CFG1, 0);
    698       1.1   jnemeth }
    699       1.1   jnemeth 
    700  1.17.2.2    martin static void
    701       1.1   jnemeth et_disable_intrs(struct et_softc *sc)
    702       1.1   jnemeth {
    703       1.1   jnemeth 	CSR_WRITE_4(sc, ET_INTR_MASK, 0xffffffff);
    704       1.1   jnemeth }
    705       1.1   jnemeth 
    706  1.17.2.2    martin static void
    707       1.1   jnemeth et_enable_intrs(struct et_softc *sc, uint32_t intrs)
    708       1.1   jnemeth {
    709       1.1   jnemeth 	CSR_WRITE_4(sc, ET_INTR_MASK, ~intrs);
    710       1.1   jnemeth }
    711       1.1   jnemeth 
    712  1.17.2.2    martin static int
    713       1.1   jnemeth et_dma_alloc(struct et_softc *sc)
    714       1.1   jnemeth {
    715       1.1   jnemeth 	struct et_txdesc_ring *tx_ring = &sc->sc_tx_ring;
    716       1.1   jnemeth 	struct et_txstatus_data *txsd = &sc->sc_tx_status;
    717       1.1   jnemeth 	struct et_rxstat_ring *rxst_ring = &sc->sc_rxstat_ring;
    718       1.1   jnemeth 	struct et_rxstatus_data *rxsd = &sc->sc_rx_status;
    719       1.1   jnemeth 	int i, error;
    720       1.1   jnemeth 
    721       1.1   jnemeth 	/*
    722       1.1   jnemeth 	 * Create TX ring DMA stuffs
    723       1.1   jnemeth 	 */
    724       1.1   jnemeth 	error = et_dma_mem_create(sc, ET_TX_RING_SIZE,
    725       1.1   jnemeth 	    (void **)&tx_ring->tr_desc, &tx_ring->tr_paddr, &tx_ring->tr_dmap,
    726       1.1   jnemeth 	    &tx_ring->tr_seg);
    727       1.1   jnemeth 	if (error) {
    728       1.1   jnemeth 		aprint_error_dev(sc->sc_dev, "can't create TX ring DMA stuffs\n");
    729       1.1   jnemeth 		return error;
    730       1.1   jnemeth 	}
    731       1.1   jnemeth 
    732       1.1   jnemeth 	/*
    733       1.1   jnemeth 	 * Create TX status DMA stuffs
    734       1.1   jnemeth 	 */
    735       1.1   jnemeth 	error = et_dma_mem_create(sc, sizeof(uint32_t),
    736       1.1   jnemeth 	    (void **)&txsd->txsd_status,
    737       1.1   jnemeth 	    &txsd->txsd_paddr, &txsd->txsd_dmap, &txsd->txsd_seg);
    738       1.1   jnemeth 	if (error) {
    739       1.1   jnemeth 		aprint_error_dev(sc->sc_dev, "can't create TX status DMA stuffs\n");
    740       1.1   jnemeth 		return error;
    741       1.1   jnemeth 	}
    742       1.1   jnemeth 
    743       1.1   jnemeth 	/*
    744       1.1   jnemeth 	 * Create DMA stuffs for RX rings
    745       1.1   jnemeth 	 */
    746       1.1   jnemeth 	for (i = 0; i < ET_RX_NRING; ++i) {
    747       1.1   jnemeth 		static const uint32_t rx_ring_posreg[ET_RX_NRING] =
    748       1.1   jnemeth 		{ ET_RX_RING0_POS, ET_RX_RING1_POS };
    749       1.1   jnemeth 
    750       1.1   jnemeth 		struct et_rxdesc_ring *rx_ring = &sc->sc_rx_ring[i];
    751       1.1   jnemeth 
    752       1.1   jnemeth 		error = et_dma_mem_create(sc, ET_RX_RING_SIZE,
    753       1.1   jnemeth 		    (void **)&rx_ring->rr_desc,
    754       1.1   jnemeth 		    &rx_ring->rr_paddr, &rx_ring->rr_dmap, &rx_ring->rr_seg);
    755       1.1   jnemeth 		if (error) {
    756       1.1   jnemeth 			aprint_error_dev(sc->sc_dev, "can't create DMA stuffs for "
    757       1.1   jnemeth 			    "the %d RX ring\n", i);
    758       1.1   jnemeth 			return error;
    759       1.1   jnemeth 		}
    760       1.1   jnemeth 		rx_ring->rr_posreg = rx_ring_posreg[i];
    761       1.1   jnemeth 	}
    762       1.1   jnemeth 
    763       1.1   jnemeth 	/*
    764       1.1   jnemeth 	 * Create RX stat ring DMA stuffs
    765       1.1   jnemeth 	 */
    766       1.1   jnemeth 	error = et_dma_mem_create(sc, ET_RXSTAT_RING_SIZE,
    767       1.1   jnemeth 	    (void **)&rxst_ring->rsr_stat,
    768       1.1   jnemeth 	    &rxst_ring->rsr_paddr, &rxst_ring->rsr_dmap, &rxst_ring->rsr_seg);
    769       1.1   jnemeth 	if (error) {
    770       1.1   jnemeth 		aprint_error_dev(sc->sc_dev, "can't create RX stat ring DMA stuffs\n");
    771       1.1   jnemeth 		return error;
    772       1.1   jnemeth 	}
    773       1.1   jnemeth 
    774       1.1   jnemeth 	/*
    775       1.1   jnemeth 	 * Create RX status DMA stuffs
    776       1.1   jnemeth 	 */
    777       1.1   jnemeth 	error = et_dma_mem_create(sc, sizeof(struct et_rxstatus),
    778       1.1   jnemeth 	    (void **)&rxsd->rxsd_status,
    779       1.1   jnemeth 	    &rxsd->rxsd_paddr, &rxsd->rxsd_dmap, &rxsd->rxsd_seg);
    780       1.1   jnemeth 	if (error) {
    781       1.1   jnemeth 		aprint_error_dev(sc->sc_dev, "can't create RX status DMA stuffs\n");
    782       1.1   jnemeth 		return error;
    783       1.1   jnemeth 	}
    784       1.1   jnemeth 
    785       1.1   jnemeth 	/*
    786       1.1   jnemeth 	 * Create mbuf DMA stuffs
    787       1.1   jnemeth 	 */
    788       1.1   jnemeth 	error = et_dma_mbuf_create(sc);
    789       1.1   jnemeth 	if (error)
    790       1.1   jnemeth 		return error;
    791       1.1   jnemeth 
    792       1.1   jnemeth 	return 0;
    793       1.1   jnemeth }
    794       1.1   jnemeth 
    795  1.17.2.2    martin static void
    796       1.1   jnemeth et_dma_free(struct et_softc *sc)
    797       1.1   jnemeth {
    798       1.1   jnemeth 	struct et_txdesc_ring *tx_ring = &sc->sc_tx_ring;
    799       1.1   jnemeth 	struct et_txstatus_data *txsd = &sc->sc_tx_status;
    800       1.1   jnemeth 	struct et_rxstat_ring *rxst_ring = &sc->sc_rxstat_ring;
    801       1.1   jnemeth 	struct et_rxstatus_data *rxsd = &sc->sc_rx_status;
    802       1.1   jnemeth 	int i, rx_done[ET_RX_NRING];
    803       1.1   jnemeth 
    804       1.1   jnemeth 	/*
    805       1.1   jnemeth 	 * Destroy TX ring DMA stuffs
    806       1.1   jnemeth 	 */
    807       1.1   jnemeth 	et_dma_mem_destroy(sc, tx_ring->tr_desc, tx_ring->tr_dmap);
    808       1.1   jnemeth 
    809       1.1   jnemeth 	/*
    810       1.1   jnemeth 	 * Destroy TX status DMA stuffs
    811       1.1   jnemeth 	 */
    812       1.1   jnemeth 	et_dma_mem_destroy(sc, txsd->txsd_status, txsd->txsd_dmap);
    813       1.1   jnemeth 
    814       1.1   jnemeth 	/*
    815       1.1   jnemeth 	 * Destroy DMA stuffs for RX rings
    816       1.1   jnemeth 	 */
    817       1.1   jnemeth 	for (i = 0; i < ET_RX_NRING; ++i) {
    818       1.1   jnemeth 		struct et_rxdesc_ring *rx_ring = &sc->sc_rx_ring[i];
    819       1.1   jnemeth 
    820       1.1   jnemeth 		et_dma_mem_destroy(sc, rx_ring->rr_desc, rx_ring->rr_dmap);
    821       1.1   jnemeth 	}
    822       1.1   jnemeth 
    823       1.1   jnemeth 	/*
    824       1.1   jnemeth 	 * Destroy RX stat ring DMA stuffs
    825       1.1   jnemeth 	 */
    826       1.1   jnemeth 	et_dma_mem_destroy(sc, rxst_ring->rsr_stat, rxst_ring->rsr_dmap);
    827  1.17.2.1  christos 
    828       1.1   jnemeth 	/*
    829       1.1   jnemeth 	 * Destroy RX status DMA stuffs
    830       1.1   jnemeth 	 */
    831       1.1   jnemeth 	et_dma_mem_destroy(sc, rxsd->rxsd_status, rxsd->rxsd_dmap);
    832       1.1   jnemeth 
    833       1.1   jnemeth 	/*
    834       1.1   jnemeth 	 * Destroy mbuf DMA stuffs
    835       1.1   jnemeth 	 */
    836       1.1   jnemeth 	for (i = 0; i < ET_RX_NRING; ++i)
    837       1.1   jnemeth 		rx_done[i] = ET_RX_NDESC;
    838       1.1   jnemeth 	et_dma_mbuf_destroy(sc, ET_TX_NDESC, rx_done);
    839       1.1   jnemeth }
    840       1.1   jnemeth 
    841  1.17.2.2    martin static int
    842       1.1   jnemeth et_dma_mbuf_create(struct et_softc *sc)
    843       1.1   jnemeth {
    844       1.1   jnemeth 	struct et_txbuf_data *tbd = &sc->sc_tx_data;
    845       1.1   jnemeth 	int i, error, rx_done[ET_RX_NRING];
    846       1.1   jnemeth 
    847       1.1   jnemeth 	/*
    848       1.1   jnemeth 	 * Create spare DMA map for RX mbufs
    849       1.1   jnemeth 	 */
    850       1.1   jnemeth 	error = bus_dmamap_create(sc->sc_dmat, MCLBYTES, 1, MCLBYTES, 0,
    851       1.1   jnemeth 	    BUS_DMA_NOWAIT, &sc->sc_mbuf_tmp_dmap);
    852       1.1   jnemeth 	if (error) {
    853       1.1   jnemeth 		aprint_error_dev(sc->sc_dev, "can't create spare mbuf DMA map\n");
    854       1.1   jnemeth 		return error;
    855       1.1   jnemeth 	}
    856       1.1   jnemeth 
    857       1.1   jnemeth 	/*
    858       1.1   jnemeth 	 * Create DMA maps for RX mbufs
    859       1.1   jnemeth 	 */
    860       1.1   jnemeth 	bzero(rx_done, sizeof(rx_done));
    861       1.1   jnemeth 	for (i = 0; i < ET_RX_NRING; ++i) {
    862       1.1   jnemeth 		struct et_rxbuf_data *rbd = &sc->sc_rx_data[i];
    863       1.1   jnemeth 		int j;
    864       1.1   jnemeth 
    865       1.1   jnemeth 		for (j = 0; j < ET_RX_NDESC; ++j) {
    866       1.1   jnemeth 			error = bus_dmamap_create(sc->sc_dmat, MCLBYTES, 1,
    867       1.1   jnemeth 			    MCLBYTES, 0, BUS_DMA_NOWAIT,
    868       1.1   jnemeth 			    &rbd->rbd_buf[j].rb_dmap);
    869       1.1   jnemeth 			if (error) {
    870       1.1   jnemeth 				aprint_error_dev(sc->sc_dev, "can't create %d RX mbuf "
    871       1.1   jnemeth 				    "for %d RX ring\n", j, i);
    872       1.1   jnemeth 				rx_done[i] = j;
    873       1.1   jnemeth 				et_dma_mbuf_destroy(sc, 0, rx_done);
    874       1.1   jnemeth 				return error;
    875       1.1   jnemeth 			}
    876       1.1   jnemeth 		}
    877       1.1   jnemeth 		rx_done[i] = ET_RX_NDESC;
    878       1.1   jnemeth 
    879       1.1   jnemeth 		rbd->rbd_softc = sc;
    880       1.1   jnemeth 		rbd->rbd_ring = &sc->sc_rx_ring[i];
    881       1.1   jnemeth 	}
    882       1.1   jnemeth 
    883       1.1   jnemeth 	/*
    884       1.1   jnemeth 	 * Create DMA maps for TX mbufs
    885       1.1   jnemeth 	 */
    886       1.1   jnemeth 	for (i = 0; i < ET_TX_NDESC; ++i) {
    887       1.1   jnemeth 		error = bus_dmamap_create(sc->sc_dmat, MCLBYTES, 1, MCLBYTES,
    888       1.1   jnemeth 		    0, BUS_DMA_NOWAIT, &tbd->tbd_buf[i].tb_dmap);
    889       1.1   jnemeth 		if (error) {
    890       1.1   jnemeth 			aprint_error_dev(sc->sc_dev, "can't create %d TX mbuf "
    891       1.1   jnemeth 			    "DMA map\n", i);
    892       1.1   jnemeth 			et_dma_mbuf_destroy(sc, i, rx_done);
    893       1.1   jnemeth 			return error;
    894       1.1   jnemeth 		}
    895       1.1   jnemeth 	}
    896       1.1   jnemeth 
    897       1.1   jnemeth 	return 0;
    898       1.1   jnemeth }
    899       1.1   jnemeth 
    900  1.17.2.2    martin static void
    901       1.1   jnemeth et_dma_mbuf_destroy(struct et_softc *sc, int tx_done, const int rx_done[])
    902       1.1   jnemeth {
    903       1.1   jnemeth 	struct et_txbuf_data *tbd = &sc->sc_tx_data;
    904       1.1   jnemeth 	int i;
    905       1.1   jnemeth 
    906       1.1   jnemeth 	/*
    907       1.1   jnemeth 	 * Destroy DMA maps for RX mbufs
    908       1.1   jnemeth 	 */
    909       1.1   jnemeth 	for (i = 0; i < ET_RX_NRING; ++i) {
    910       1.1   jnemeth 		struct et_rxbuf_data *rbd = &sc->sc_rx_data[i];
    911       1.1   jnemeth 		int j;
    912       1.1   jnemeth 
    913       1.1   jnemeth 		for (j = 0; j < rx_done[i]; ++j) {
    914       1.1   jnemeth 			struct et_rxbuf *rb = &rbd->rbd_buf[j];
    915       1.1   jnemeth 
    916       1.6    dyoung 			KASSERTMSG(rb->rb_mbuf == NULL,
    917       1.6    dyoung 			    "RX mbuf in %d RX ring is not freed yet\n", i);
    918       1.7  christos 			bus_dmamap_destroy(sc->sc_dmat, rb->rb_dmap);
    919       1.1   jnemeth 		}
    920       1.1   jnemeth 	}
    921       1.1   jnemeth 
    922       1.1   jnemeth 	/*
    923       1.1   jnemeth 	 * Destroy DMA maps for TX mbufs
    924       1.1   jnemeth 	 */
    925       1.1   jnemeth 	for (i = 0; i < tx_done; ++i) {
    926       1.1   jnemeth 		struct et_txbuf *tb = &tbd->tbd_buf[i];
    927       1.1   jnemeth 
    928       1.6    dyoung 		KASSERTMSG(tb->tb_mbuf == NULL, "TX mbuf is not freed yet\n");
    929       1.1   jnemeth 		bus_dmamap_destroy(sc->sc_dmat, tb->tb_dmap);
    930       1.1   jnemeth 	}
    931       1.1   jnemeth 
    932       1.1   jnemeth 	/*
    933       1.1   jnemeth 	 * Destroy spare mbuf DMA map
    934       1.1   jnemeth 	 */
    935       1.1   jnemeth 	bus_dmamap_destroy(sc->sc_dmat, sc->sc_mbuf_tmp_dmap);
    936       1.1   jnemeth }
    937       1.1   jnemeth 
    938  1.17.2.2    martin static int
    939       1.1   jnemeth et_dma_mem_create(struct et_softc *sc, bus_size_t size,
    940       1.1   jnemeth     void **addr, bus_addr_t *paddr, bus_dmamap_t *dmap, bus_dma_segment_t *seg)
    941       1.1   jnemeth {
    942       1.1   jnemeth 	int error, nsegs;
    943       1.1   jnemeth 
    944       1.1   jnemeth 	error = bus_dmamap_create(sc->sc_dmat, size, 1, size, 0, BUS_DMA_NOWAIT,
    945       1.1   jnemeth 	    dmap);
    946       1.1   jnemeth 	if (error) {
    947       1.1   jnemeth 		aprint_error_dev(sc->sc_dev, "can't create DMA map\n");
    948       1.1   jnemeth 		return error;
    949       1.1   jnemeth 	}
    950       1.1   jnemeth 
    951       1.1   jnemeth 	error = bus_dmamem_alloc(sc->sc_dmat, size, ET_ALIGN, 0, seg,
    952       1.1   jnemeth 	    1, &nsegs, BUS_DMA_WAITOK);
    953       1.1   jnemeth 	if (error) {
    954       1.1   jnemeth 		aprint_error_dev(sc->sc_dev, "can't allocate DMA mem\n");
    955       1.1   jnemeth 		return error;
    956       1.1   jnemeth 	}
    957       1.1   jnemeth 
    958       1.1   jnemeth 	error = bus_dmamem_map(sc->sc_dmat, seg, nsegs,
    959       1.1   jnemeth 	    size, (void **)addr, BUS_DMA_NOWAIT);
    960       1.1   jnemeth 	if (error) {
    961       1.1   jnemeth 		aprint_error_dev(sc->sc_dev, "can't map DMA mem\n");
    962       1.1   jnemeth 		return (error);
    963       1.1   jnemeth 	}
    964       1.1   jnemeth 
    965       1.1   jnemeth 	error = bus_dmamap_load(sc->sc_dmat, *dmap, *addr, size, NULL,
    966       1.1   jnemeth 	    BUS_DMA_WAITOK);
    967       1.1   jnemeth 	if (error) {
    968       1.1   jnemeth 		aprint_error_dev(sc->sc_dev, "can't load DMA mem\n");
    969       1.1   jnemeth 		bus_dmamem_free(sc->sc_dmat, (bus_dma_segment_t *)addr, 1);
    970       1.1   jnemeth 		return error;
    971       1.1   jnemeth 	}
    972       1.1   jnemeth 
    973       1.1   jnemeth 	memset(*addr, 0, size);
    974       1.1   jnemeth 
    975       1.1   jnemeth 	*paddr = (*dmap)->dm_segs[0].ds_addr;
    976       1.1   jnemeth 
    977       1.1   jnemeth 	return 0;
    978       1.1   jnemeth }
    979       1.1   jnemeth 
    980  1.17.2.2    martin static void
    981       1.1   jnemeth et_dma_mem_destroy(struct et_softc *sc, void *addr, bus_dmamap_t dmap)
    982       1.1   jnemeth {
    983       1.1   jnemeth 	bus_dmamap_unload(sc->sc_dmat, dmap);
    984       1.1   jnemeth 	bus_dmamem_free(sc->sc_dmat, (bus_dma_segment_t *)&addr, 1);
    985       1.1   jnemeth }
    986       1.1   jnemeth 
    987  1.17.2.2    martin static void
    988       1.1   jnemeth et_chip_attach(struct et_softc *sc)
    989       1.1   jnemeth {
    990       1.1   jnemeth 	uint32_t val;
    991       1.1   jnemeth 
    992       1.1   jnemeth 	/*
    993       1.1   jnemeth 	 * Perform minimal initialization
    994       1.1   jnemeth 	 */
    995       1.1   jnemeth 
    996       1.1   jnemeth 	/* Disable loopback */
    997       1.1   jnemeth 	CSR_WRITE_4(sc, ET_LOOPBACK, 0);
    998       1.1   jnemeth 
    999       1.1   jnemeth 	/* Reset MAC */
   1000       1.1   jnemeth 	CSR_WRITE_4(sc, ET_MAC_CFG1,
   1001       1.1   jnemeth 		    ET_MAC_CFG1_RST_TXFUNC | ET_MAC_CFG1_RST_RXFUNC |
   1002       1.1   jnemeth 		    ET_MAC_CFG1_RST_TXMC | ET_MAC_CFG1_RST_RXMC |
   1003       1.1   jnemeth 		    ET_MAC_CFG1_SIM_RST | ET_MAC_CFG1_SOFT_RST);
   1004       1.1   jnemeth 
   1005       1.1   jnemeth 	/*
   1006       1.1   jnemeth 	 * Setup half duplex mode
   1007       1.1   jnemeth 	 */
   1008       1.1   jnemeth 	val = __SHIFTIN(10, ET_MAC_HDX_ALT_BEB_TRUNC) |
   1009       1.1   jnemeth 	      __SHIFTIN(15, ET_MAC_HDX_REXMIT_MAX) |
   1010       1.1   jnemeth 	      __SHIFTIN(55, ET_MAC_HDX_COLLWIN) |
   1011       1.1   jnemeth 	      ET_MAC_HDX_EXC_DEFER;
   1012       1.1   jnemeth 	CSR_WRITE_4(sc, ET_MAC_HDX, val);
   1013       1.1   jnemeth 
   1014       1.1   jnemeth 	/* Clear MAC control */
   1015       1.1   jnemeth 	CSR_WRITE_4(sc, ET_MAC_CTRL, 0);
   1016       1.1   jnemeth 
   1017       1.1   jnemeth 	/* Reset MII */
   1018       1.1   jnemeth 	CSR_WRITE_4(sc, ET_MII_CFG, ET_MII_CFG_CLKRST);
   1019       1.1   jnemeth 
   1020       1.1   jnemeth 	/* Bring MAC out of reset state */
   1021       1.1   jnemeth 	CSR_WRITE_4(sc, ET_MAC_CFG1, 0);
   1022       1.1   jnemeth 
   1023       1.1   jnemeth 	/* Enable memory controllers */
   1024       1.1   jnemeth 	CSR_WRITE_4(sc, ET_MMC_CTRL, ET_MMC_CTRL_ENABLE);
   1025       1.1   jnemeth }
   1026       1.1   jnemeth 
   1027  1.17.2.2    martin static int
   1028       1.1   jnemeth et_intr(void *xsc)
   1029       1.1   jnemeth {
   1030       1.1   jnemeth 	struct et_softc *sc = xsc;
   1031       1.1   jnemeth 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1032       1.1   jnemeth 	uint32_t intrs;
   1033       1.1   jnemeth 
   1034       1.1   jnemeth 	if ((ifp->if_flags & IFF_RUNNING) == 0)
   1035       1.1   jnemeth 		return (0);
   1036       1.1   jnemeth 
   1037       1.1   jnemeth 	intrs = CSR_READ_4(sc, ET_INTR_STATUS);
   1038       1.1   jnemeth 	if (intrs == 0 || intrs == 0xffffffff)
   1039       1.1   jnemeth 		return (0);
   1040       1.1   jnemeth 
   1041       1.1   jnemeth 	et_disable_intrs(sc);
   1042       1.1   jnemeth 	intrs &= ET_INTRS;
   1043       1.1   jnemeth 	if (intrs == 0)	/* Not interested */
   1044       1.1   jnemeth 		goto back;
   1045       1.1   jnemeth 
   1046       1.1   jnemeth 	if (intrs & ET_INTR_RXEOF)
   1047       1.1   jnemeth 		et_rxeof(sc);
   1048       1.1   jnemeth 	if (intrs & (ET_INTR_TXEOF | ET_INTR_TIMER))
   1049       1.1   jnemeth 		et_txeof(sc);
   1050       1.1   jnemeth 	if (intrs & ET_INTR_TIMER)
   1051       1.1   jnemeth 		CSR_WRITE_4(sc, ET_TIMER, sc->sc_timer);
   1052       1.1   jnemeth back:
   1053       1.1   jnemeth 	et_enable_intrs(sc, ET_INTRS);
   1054       1.1   jnemeth 
   1055       1.1   jnemeth 	return (1);
   1056       1.1   jnemeth }
   1057       1.1   jnemeth 
   1058  1.17.2.2    martin static int
   1059       1.1   jnemeth et_init(struct ifnet *ifp)
   1060       1.1   jnemeth {
   1061       1.1   jnemeth 	struct et_softc *sc = ifp->if_softc;
   1062       1.1   jnemeth 	int error, i, s;
   1063       1.1   jnemeth 
   1064       1.1   jnemeth 	if (ifp->if_flags & IFF_RUNNING)
   1065       1.1   jnemeth 		return 0;
   1066       1.1   jnemeth 
   1067       1.1   jnemeth 	s = splnet();
   1068       1.1   jnemeth 
   1069       1.1   jnemeth 	et_stop(sc);
   1070  1.17.2.2    martin 	et_reset(sc);
   1071       1.1   jnemeth 
   1072       1.1   jnemeth 	for (i = 0; i < ET_RX_NRING; ++i) {
   1073       1.1   jnemeth 		sc->sc_rx_data[i].rbd_bufsize = et_bufsize[i].bufsize;
   1074       1.1   jnemeth 		sc->sc_rx_data[i].rbd_newbuf = et_bufsize[i].newbuf;
   1075       1.1   jnemeth 	}
   1076       1.1   jnemeth 
   1077       1.1   jnemeth 	error = et_init_tx_ring(sc);
   1078       1.1   jnemeth 	if (error)
   1079       1.1   jnemeth 		goto back;
   1080       1.1   jnemeth 
   1081       1.1   jnemeth 	error = et_init_rx_ring(sc);
   1082       1.1   jnemeth 	if (error)
   1083       1.1   jnemeth 		goto back;
   1084       1.1   jnemeth 
   1085       1.1   jnemeth 	error = et_chip_init(sc);
   1086       1.1   jnemeth 	if (error)
   1087       1.1   jnemeth 		goto back;
   1088       1.1   jnemeth 
   1089       1.1   jnemeth 	error = et_start_rxdma(sc);
   1090       1.1   jnemeth 	if (error)
   1091       1.1   jnemeth 		goto back;
   1092       1.1   jnemeth 
   1093       1.1   jnemeth 	error = et_start_txdma(sc);
   1094       1.1   jnemeth 	if (error)
   1095       1.1   jnemeth 		goto back;
   1096       1.1   jnemeth 
   1097  1.17.2.2    martin 	/* Enable interrupts. */
   1098       1.1   jnemeth 	et_enable_intrs(sc, ET_INTRS);
   1099       1.1   jnemeth 
   1100       1.1   jnemeth 	callout_schedule(&sc->sc_tick, hz);
   1101       1.1   jnemeth 
   1102       1.1   jnemeth 	CSR_WRITE_4(sc, ET_TIMER, sc->sc_timer);
   1103       1.1   jnemeth 
   1104       1.1   jnemeth 	ifp->if_flags |= IFF_RUNNING;
   1105       1.1   jnemeth 	ifp->if_flags &= ~IFF_OACTIVE;
   1106  1.17.2.2    martin 
   1107  1.17.2.2    martin 	sc->sc_flags &= ~ET_FLAG_LINK;
   1108  1.17.2.2    martin 	ether_mediachange(ifp);
   1109       1.1   jnemeth back:
   1110       1.1   jnemeth 	if (error)
   1111       1.1   jnemeth 		et_stop(sc);
   1112       1.1   jnemeth 
   1113       1.1   jnemeth 	splx(s);
   1114       1.1   jnemeth 
   1115       1.1   jnemeth 	return (0);
   1116       1.1   jnemeth }
   1117       1.1   jnemeth 
   1118  1.17.2.2    martin static int
   1119       1.1   jnemeth et_ioctl(struct ifnet *ifp, u_long cmd, void *data)
   1120       1.1   jnemeth {
   1121       1.1   jnemeth 	struct et_softc *sc = ifp->if_softc;
   1122       1.1   jnemeth 	int s, error = 0;
   1123       1.1   jnemeth 
   1124       1.1   jnemeth 	s = splnet();
   1125       1.1   jnemeth 
   1126       1.1   jnemeth 	switch (cmd) {
   1127       1.1   jnemeth 	case SIOCSIFFLAGS:
   1128  1.17.2.2    martin 		if ((error = ifioctl_common(ifp, cmd, data)) != 0)
   1129  1.17.2.2    martin 			break;
   1130       1.1   jnemeth 		if (ifp->if_flags & IFF_UP) {
   1131       1.1   jnemeth 			/*
   1132       1.1   jnemeth 			 * If only the PROMISC or ALLMULTI flag changes, then
   1133       1.1   jnemeth 			 * don't do a full re-init of the chip, just update
   1134       1.1   jnemeth 			 * the Rx filter.
   1135       1.1   jnemeth 			 */
   1136       1.1   jnemeth 			if ((ifp->if_flags & IFF_RUNNING) &&
   1137       1.1   jnemeth 			    ((ifp->if_flags ^ sc->sc_if_flags) &
   1138       1.1   jnemeth 			     (IFF_ALLMULTI | IFF_PROMISC)) != 0) {
   1139       1.1   jnemeth 				et_setmulti(sc);
   1140       1.1   jnemeth 			} else {
   1141       1.1   jnemeth 				if (!(ifp->if_flags & IFF_RUNNING))
   1142       1.1   jnemeth 					et_init(ifp);
   1143       1.1   jnemeth 			}
   1144       1.1   jnemeth 		} else {
   1145       1.1   jnemeth 			if (ifp->if_flags & IFF_RUNNING)
   1146       1.1   jnemeth 				et_stop(sc);
   1147       1.1   jnemeth 		}
   1148       1.1   jnemeth 		sc->sc_if_flags = ifp->if_flags;
   1149       1.1   jnemeth 		break;
   1150       1.1   jnemeth 	default:
   1151       1.1   jnemeth 		error = ether_ioctl(ifp, cmd, data);
   1152       1.1   jnemeth 		if (error == ENETRESET) {
   1153       1.1   jnemeth 			if (ifp->if_flags & IFF_RUNNING)
   1154       1.1   jnemeth 				et_setmulti(sc);
   1155       1.1   jnemeth 			error = 0;
   1156       1.1   jnemeth 		}
   1157       1.1   jnemeth 		break;
   1158       1.1   jnemeth 	}
   1159       1.1   jnemeth 
   1160       1.1   jnemeth 	splx(s);
   1161       1.1   jnemeth 
   1162       1.1   jnemeth 	return error;
   1163       1.1   jnemeth }
   1164       1.1   jnemeth 
   1165  1.17.2.2    martin static void
   1166       1.1   jnemeth et_start(struct ifnet *ifp)
   1167       1.1   jnemeth {
   1168       1.1   jnemeth 	struct et_softc *sc = ifp->if_softc;
   1169       1.1   jnemeth 	struct et_txbuf_data *tbd = &sc->sc_tx_data;
   1170       1.1   jnemeth 	int trans;
   1171       1.1   jnemeth 	struct mbuf *m;
   1172       1.1   jnemeth 
   1173  1.17.2.2    martin 	if (((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING) ||
   1174  1.17.2.2    martin 	    ((sc->sc_flags & (ET_FLAG_LINK | ET_FLAG_TXRX_ENABLED)) !=
   1175  1.17.2.2    martin 		(ET_FLAG_LINK | ET_FLAG_TXRX_ENABLED)))
   1176       1.1   jnemeth 		return;
   1177       1.1   jnemeth 
   1178       1.1   jnemeth 	trans = 0;
   1179       1.1   jnemeth 	for (;;) {
   1180       1.1   jnemeth 		IFQ_DEQUEUE(&ifp->if_snd, m);
   1181       1.1   jnemeth 		if (m == NULL)
   1182       1.1   jnemeth 			break;
   1183       1.1   jnemeth 
   1184       1.1   jnemeth 		if ((tbd->tbd_used + ET_NSEG_SPARE) > ET_TX_NDESC) {
   1185       1.1   jnemeth 			ifp->if_flags |= IFF_OACTIVE;
   1186       1.1   jnemeth 			break;
   1187       1.1   jnemeth 		}
   1188       1.1   jnemeth 
   1189       1.1   jnemeth 		if (et_encap(sc, &m)) {
   1190  1.17.2.2    martin 			if_statinc(ifp, if_oerrors);
   1191       1.1   jnemeth 			ifp->if_flags |= IFF_OACTIVE;
   1192       1.1   jnemeth 			break;
   1193       1.1   jnemeth 		}
   1194       1.1   jnemeth 
   1195       1.1   jnemeth 		trans = 1;
   1196       1.1   jnemeth 
   1197      1.17   msaitoh 		bpf_mtap(ifp, m, BPF_D_OUT);
   1198       1.1   jnemeth 	}
   1199       1.1   jnemeth 
   1200       1.1   jnemeth 	if (trans) {
   1201       1.1   jnemeth 		callout_schedule(&sc->sc_txtick, hz);
   1202       1.1   jnemeth 		ifp->if_timer = 5;
   1203       1.1   jnemeth 	}
   1204       1.1   jnemeth }
   1205       1.1   jnemeth 
   1206  1.17.2.2    martin static void
   1207       1.1   jnemeth et_watchdog(struct ifnet *ifp)
   1208       1.1   jnemeth {
   1209       1.1   jnemeth 	struct et_softc *sc = ifp->if_softc;
   1210       1.1   jnemeth 	aprint_error_dev(sc->sc_dev, "watchdog timed out\n");
   1211       1.1   jnemeth 
   1212       1.1   jnemeth 	ifp->if_flags &= ~IFF_RUNNING;
   1213       1.1   jnemeth 	et_init(ifp);
   1214       1.1   jnemeth 	et_start(ifp);
   1215       1.1   jnemeth }
   1216       1.1   jnemeth 
   1217  1.17.2.2    martin static int
   1218       1.1   jnemeth et_stop_rxdma(struct et_softc *sc)
   1219       1.1   jnemeth {
   1220  1.17.2.2    martin 
   1221       1.1   jnemeth 	CSR_WRITE_4(sc, ET_RXDMA_CTRL,
   1222       1.1   jnemeth 		    ET_RXDMA_CTRL_HALT | ET_RXDMA_CTRL_RING1_ENABLE);
   1223       1.1   jnemeth 
   1224       1.1   jnemeth 	DELAY(5);
   1225       1.1   jnemeth 	if ((CSR_READ_4(sc, ET_RXDMA_CTRL) & ET_RXDMA_CTRL_HALTED) == 0) {
   1226       1.1   jnemeth 		aprint_error_dev(sc->sc_dev, "can't stop RX DMA engine\n");
   1227       1.1   jnemeth 		return ETIMEDOUT;
   1228       1.1   jnemeth 	}
   1229       1.1   jnemeth 	return 0;
   1230       1.1   jnemeth }
   1231       1.1   jnemeth 
   1232  1.17.2.2    martin static int
   1233       1.1   jnemeth et_stop_txdma(struct et_softc *sc)
   1234       1.1   jnemeth {
   1235  1.17.2.2    martin 
   1236       1.1   jnemeth 	CSR_WRITE_4(sc, ET_TXDMA_CTRL,
   1237       1.1   jnemeth 		    ET_TXDMA_CTRL_HALT | ET_TXDMA_CTRL_SINGLE_EPKT);
   1238       1.1   jnemeth 	return 0;
   1239       1.1   jnemeth }
   1240       1.1   jnemeth 
   1241  1.17.2.2    martin static void
   1242       1.1   jnemeth et_free_tx_ring(struct et_softc *sc)
   1243       1.1   jnemeth {
   1244       1.1   jnemeth 	struct et_txbuf_data *tbd = &sc->sc_tx_data;
   1245       1.1   jnemeth 	struct et_txdesc_ring *tx_ring = &sc->sc_tx_ring;
   1246       1.1   jnemeth 	int i;
   1247       1.1   jnemeth 
   1248       1.1   jnemeth 	for (i = 0; i < ET_TX_NDESC; ++i) {
   1249       1.1   jnemeth 		struct et_txbuf *tb = &tbd->tbd_buf[i];
   1250       1.1   jnemeth 
   1251       1.1   jnemeth 		if (tb->tb_mbuf != NULL) {
   1252       1.1   jnemeth 			bus_dmamap_unload(sc->sc_dmat, tb->tb_dmap);
   1253       1.1   jnemeth 			m_freem(tb->tb_mbuf);
   1254       1.1   jnemeth 			tb->tb_mbuf = NULL;
   1255       1.1   jnemeth 		}
   1256       1.1   jnemeth 	}
   1257       1.1   jnemeth 
   1258       1.1   jnemeth 	bzero(tx_ring->tr_desc, ET_TX_RING_SIZE);
   1259       1.1   jnemeth 	bus_dmamap_sync(sc->sc_dmat, tx_ring->tr_dmap, 0,
   1260       1.1   jnemeth 	    tx_ring->tr_dmap->dm_mapsize, BUS_DMASYNC_PREWRITE);
   1261       1.1   jnemeth }
   1262       1.1   jnemeth 
   1263  1.17.2.2    martin static void
   1264       1.1   jnemeth et_free_rx_ring(struct et_softc *sc)
   1265       1.1   jnemeth {
   1266       1.1   jnemeth 	int n;
   1267       1.1   jnemeth 
   1268       1.1   jnemeth 	for (n = 0; n < ET_RX_NRING; ++n) {
   1269       1.1   jnemeth 		struct et_rxbuf_data *rbd = &sc->sc_rx_data[n];
   1270       1.1   jnemeth 		struct et_rxdesc_ring *rx_ring = &sc->sc_rx_ring[n];
   1271       1.1   jnemeth 		int i;
   1272       1.1   jnemeth 
   1273       1.1   jnemeth 		for (i = 0; i < ET_RX_NDESC; ++i) {
   1274       1.1   jnemeth 			struct et_rxbuf *rb = &rbd->rbd_buf[i];
   1275       1.1   jnemeth 
   1276       1.1   jnemeth 			if (rb->rb_mbuf != NULL) {
   1277       1.1   jnemeth 				bus_dmamap_unload(sc->sc_dmat, rb->rb_dmap);
   1278       1.1   jnemeth 				m_freem(rb->rb_mbuf);
   1279       1.1   jnemeth 				rb->rb_mbuf = NULL;
   1280       1.1   jnemeth 			}
   1281       1.1   jnemeth 		}
   1282       1.1   jnemeth 
   1283       1.1   jnemeth 		bzero(rx_ring->rr_desc, ET_RX_RING_SIZE);
   1284       1.1   jnemeth 		bus_dmamap_sync(sc->sc_dmat, rx_ring->rr_dmap, 0,
   1285       1.1   jnemeth 		    rx_ring->rr_dmap->dm_mapsize, BUS_DMASYNC_PREWRITE);
   1286       1.1   jnemeth 	}
   1287       1.1   jnemeth }
   1288       1.1   jnemeth 
   1289  1.17.2.2    martin static void
   1290       1.1   jnemeth et_setmulti(struct et_softc *sc)
   1291       1.1   jnemeth {
   1292       1.1   jnemeth 	struct ethercom *ec = &sc->sc_ethercom;
   1293       1.1   jnemeth 	struct ifnet *ifp = &ec->ec_if;
   1294       1.1   jnemeth 	uint32_t hash[4] = { 0, 0, 0, 0 };
   1295       1.1   jnemeth 	uint32_t rxmac_ctrl, pktfilt;
   1296       1.1   jnemeth 	struct ether_multi *enm;
   1297       1.1   jnemeth 	struct ether_multistep step;
   1298       1.1   jnemeth 	int i, count;
   1299       1.1   jnemeth 
   1300       1.1   jnemeth 	pktfilt = CSR_READ_4(sc, ET_PKTFILT);
   1301       1.1   jnemeth 	rxmac_ctrl = CSR_READ_4(sc, ET_RXMAC_CTRL);
   1302       1.1   jnemeth 
   1303       1.1   jnemeth 	pktfilt &= ~(ET_PKTFILT_BCAST | ET_PKTFILT_MCAST | ET_PKTFILT_UCAST);
   1304       1.1   jnemeth 	if (ifp->if_flags & (IFF_PROMISC | IFF_ALLMULTI)) {
   1305       1.1   jnemeth 		rxmac_ctrl |= ET_RXMAC_CTRL_NO_PKTFILT;
   1306       1.1   jnemeth 		goto back;
   1307       1.1   jnemeth 	}
   1308       1.1   jnemeth 
   1309       1.1   jnemeth 	count = 0;
   1310  1.17.2.1  christos 	ETHER_LOCK(ec);
   1311       1.1   jnemeth 	ETHER_FIRST_MULTI(step, ec, enm);
   1312       1.1   jnemeth 	while (enm != NULL) {
   1313       1.1   jnemeth 		uint32_t *hp, h;
   1314       1.1   jnemeth 
   1315  1.17.2.2    martin 		h = ether_crc32_be(enm->enm_addrlo, ETHER_ADDR_LEN);
   1316       1.1   jnemeth 		h = (h & 0x3f800000) >> 23;
   1317       1.1   jnemeth 
   1318       1.1   jnemeth 		hp = &hash[0];
   1319       1.1   jnemeth 		if (h >= 32 && h < 64) {
   1320       1.1   jnemeth 			h -= 32;
   1321       1.1   jnemeth 			hp = &hash[1];
   1322       1.1   jnemeth 		} else if (h >= 64 && h < 96) {
   1323       1.1   jnemeth 			h -= 64;
   1324       1.1   jnemeth 			hp = &hash[2];
   1325       1.1   jnemeth 		} else if (h >= 96) {
   1326       1.1   jnemeth 			h -= 96;
   1327       1.1   jnemeth 			hp = &hash[3];
   1328       1.1   jnemeth 		}
   1329       1.1   jnemeth 		*hp |= (1 << h);
   1330       1.1   jnemeth 
   1331       1.1   jnemeth 		++count;
   1332       1.1   jnemeth 		ETHER_NEXT_MULTI(step, enm);
   1333       1.1   jnemeth 	}
   1334  1.17.2.1  christos 	ETHER_UNLOCK(ec);
   1335       1.1   jnemeth 
   1336       1.1   jnemeth 	for (i = 0; i < 4; ++i)
   1337       1.1   jnemeth 		CSR_WRITE_4(sc, ET_MULTI_HASH + (i * 4), hash[i]);
   1338       1.1   jnemeth 
   1339       1.1   jnemeth 	if (count > 0)
   1340       1.1   jnemeth 		pktfilt |= ET_PKTFILT_MCAST;
   1341       1.1   jnemeth 	rxmac_ctrl &= ~ET_RXMAC_CTRL_NO_PKTFILT;
   1342       1.1   jnemeth back:
   1343       1.1   jnemeth 	CSR_WRITE_4(sc, ET_PKTFILT, pktfilt);
   1344       1.1   jnemeth 	CSR_WRITE_4(sc, ET_RXMAC_CTRL, rxmac_ctrl);
   1345       1.1   jnemeth }
   1346       1.1   jnemeth 
   1347  1.17.2.2    martin static int
   1348       1.1   jnemeth et_chip_init(struct et_softc *sc)
   1349       1.1   jnemeth {
   1350       1.1   jnemeth 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1351       1.1   jnemeth 	uint32_t rxq_end;
   1352       1.1   jnemeth 	int error;
   1353       1.1   jnemeth 
   1354       1.1   jnemeth 	/*
   1355       1.1   jnemeth 	 * Split internal memory between TX and RX according to MTU
   1356       1.1   jnemeth 	 */
   1357       1.1   jnemeth 	if (ifp->if_mtu < 2048)
   1358       1.1   jnemeth 		rxq_end = 0x2bc;
   1359       1.1   jnemeth 	else if (ifp->if_mtu < 8192)
   1360       1.1   jnemeth 		rxq_end = 0x1ff;
   1361       1.1   jnemeth 	else
   1362       1.1   jnemeth 		rxq_end = 0x1b3;
   1363       1.1   jnemeth 	CSR_WRITE_4(sc, ET_RXQ_START, 0);
   1364       1.1   jnemeth 	CSR_WRITE_4(sc, ET_RXQ_END, rxq_end);
   1365       1.1   jnemeth 	CSR_WRITE_4(sc, ET_TXQ_START, rxq_end + 1);
   1366       1.1   jnemeth 	CSR_WRITE_4(sc, ET_TXQ_END, ET_INTERN_MEM_END);
   1367       1.1   jnemeth 
   1368       1.1   jnemeth 	/* No loopback */
   1369       1.1   jnemeth 	CSR_WRITE_4(sc, ET_LOOPBACK, 0);
   1370       1.1   jnemeth 
   1371       1.1   jnemeth 	/* Clear MSI configure */
   1372       1.1   jnemeth 	CSR_WRITE_4(sc, ET_MSI_CFG, 0);
   1373       1.1   jnemeth 
   1374       1.1   jnemeth 	/* Disable timer */
   1375       1.1   jnemeth 	CSR_WRITE_4(sc, ET_TIMER, 0);
   1376       1.1   jnemeth 
   1377       1.1   jnemeth 	/* Initialize MAC */
   1378       1.1   jnemeth 	et_init_mac(sc);
   1379       1.1   jnemeth 
   1380       1.1   jnemeth 	/* Enable memory controllers */
   1381       1.1   jnemeth 	CSR_WRITE_4(sc, ET_MMC_CTRL, ET_MMC_CTRL_ENABLE);
   1382       1.1   jnemeth 
   1383       1.1   jnemeth 	/* Initialize RX MAC */
   1384       1.1   jnemeth 	et_init_rxmac(sc);
   1385       1.1   jnemeth 
   1386       1.1   jnemeth 	/* Initialize TX MAC */
   1387       1.1   jnemeth 	et_init_txmac(sc);
   1388       1.1   jnemeth 
   1389       1.1   jnemeth 	/* Initialize RX DMA engine */
   1390       1.1   jnemeth 	error = et_init_rxdma(sc);
   1391       1.1   jnemeth 	if (error)
   1392       1.1   jnemeth 		return error;
   1393       1.1   jnemeth 
   1394       1.1   jnemeth 	/* Initialize TX DMA engine */
   1395       1.1   jnemeth 	error = et_init_txdma(sc);
   1396       1.1   jnemeth 	if (error)
   1397       1.1   jnemeth 		return error;
   1398       1.1   jnemeth 
   1399       1.1   jnemeth 	return 0;
   1400       1.1   jnemeth }
   1401       1.1   jnemeth 
   1402  1.17.2.2    martin static int
   1403       1.1   jnemeth et_init_tx_ring(struct et_softc *sc)
   1404       1.1   jnemeth {
   1405       1.1   jnemeth 	struct et_txdesc_ring *tx_ring = &sc->sc_tx_ring;
   1406       1.1   jnemeth 	struct et_txstatus_data *txsd = &sc->sc_tx_status;
   1407       1.1   jnemeth 	struct et_txbuf_data *tbd = &sc->sc_tx_data;
   1408       1.1   jnemeth 
   1409       1.1   jnemeth 	bzero(tx_ring->tr_desc, ET_TX_RING_SIZE);
   1410       1.1   jnemeth 	bus_dmamap_sync(sc->sc_dmat, tx_ring->tr_dmap, 0,
   1411       1.1   jnemeth 	    tx_ring->tr_dmap->dm_mapsize, BUS_DMASYNC_PREWRITE);
   1412       1.1   jnemeth 
   1413       1.1   jnemeth 	tbd->tbd_start_index = 0;
   1414       1.1   jnemeth 	tbd->tbd_start_wrap = 0;
   1415       1.1   jnemeth 	tbd->tbd_used = 0;
   1416       1.1   jnemeth 
   1417       1.1   jnemeth 	bzero(txsd->txsd_status, sizeof(uint32_t));
   1418       1.1   jnemeth 	bus_dmamap_sync(sc->sc_dmat, txsd->txsd_dmap, 0,
   1419       1.1   jnemeth 	    txsd->txsd_dmap->dm_mapsize, BUS_DMASYNC_PREWRITE);
   1420       1.1   jnemeth 	return 0;
   1421       1.1   jnemeth }
   1422       1.1   jnemeth 
   1423  1.17.2.2    martin static int
   1424       1.1   jnemeth et_init_rx_ring(struct et_softc *sc)
   1425       1.1   jnemeth {
   1426       1.1   jnemeth 	struct et_rxstatus_data *rxsd = &sc->sc_rx_status;
   1427       1.1   jnemeth 	struct et_rxstat_ring *rxst_ring = &sc->sc_rxstat_ring;
   1428       1.1   jnemeth 	int n;
   1429       1.1   jnemeth 
   1430       1.1   jnemeth 	for (n = 0; n < ET_RX_NRING; ++n) {
   1431       1.1   jnemeth 		struct et_rxbuf_data *rbd = &sc->sc_rx_data[n];
   1432       1.1   jnemeth 		int i, error;
   1433       1.1   jnemeth 
   1434       1.1   jnemeth 		for (i = 0; i < ET_RX_NDESC; ++i) {
   1435       1.1   jnemeth 			error = rbd->rbd_newbuf(rbd, i, 1);
   1436       1.1   jnemeth 			if (error) {
   1437       1.1   jnemeth 				aprint_error_dev(sc->sc_dev, "%d ring %d buf, newbuf failed: "
   1438       1.1   jnemeth 				    "%d\n", n, i, error);
   1439       1.1   jnemeth 				return error;
   1440       1.1   jnemeth 			}
   1441       1.1   jnemeth 		}
   1442       1.1   jnemeth 	}
   1443       1.1   jnemeth 
   1444       1.1   jnemeth 	bzero(rxsd->rxsd_status, sizeof(struct et_rxstatus));
   1445       1.1   jnemeth 	bus_dmamap_sync(sc->sc_dmat, rxsd->rxsd_dmap, 0,
   1446       1.1   jnemeth 	    rxsd->rxsd_dmap->dm_mapsize, BUS_DMASYNC_PREWRITE);
   1447       1.1   jnemeth 
   1448       1.1   jnemeth 	bzero(rxst_ring->rsr_stat, ET_RXSTAT_RING_SIZE);
   1449       1.1   jnemeth 	bus_dmamap_sync(sc->sc_dmat, rxst_ring->rsr_dmap, 0,
   1450       1.1   jnemeth 	    rxst_ring->rsr_dmap->dm_mapsize, BUS_DMASYNC_PREWRITE);
   1451       1.1   jnemeth 
   1452       1.1   jnemeth 	return 0;
   1453       1.1   jnemeth }
   1454       1.1   jnemeth 
   1455  1.17.2.2    martin static int
   1456       1.1   jnemeth et_init_rxdma(struct et_softc *sc)
   1457       1.1   jnemeth {
   1458       1.1   jnemeth 	struct et_rxstatus_data *rxsd = &sc->sc_rx_status;
   1459       1.1   jnemeth 	struct et_rxstat_ring *rxst_ring = &sc->sc_rxstat_ring;
   1460       1.1   jnemeth 	struct et_rxdesc_ring *rx_ring;
   1461       1.1   jnemeth 	int error;
   1462       1.1   jnemeth 
   1463       1.1   jnemeth 	error = et_stop_rxdma(sc);
   1464       1.1   jnemeth 	if (error) {
   1465       1.1   jnemeth 		aprint_error_dev(sc->sc_dev, "can't init RX DMA engine\n");
   1466       1.1   jnemeth 		return error;
   1467       1.1   jnemeth 	}
   1468       1.1   jnemeth 
   1469       1.1   jnemeth 	/*
   1470       1.1   jnemeth 	 * Install RX status
   1471       1.1   jnemeth 	 */
   1472       1.1   jnemeth 	CSR_WRITE_4(sc, ET_RX_STATUS_HI, ET_ADDR_HI(rxsd->rxsd_paddr));
   1473       1.1   jnemeth 	CSR_WRITE_4(sc, ET_RX_STATUS_LO, ET_ADDR_LO(rxsd->rxsd_paddr));
   1474       1.1   jnemeth 
   1475       1.1   jnemeth 	/*
   1476       1.1   jnemeth 	 * Install RX stat ring
   1477       1.1   jnemeth 	 */
   1478       1.1   jnemeth 	CSR_WRITE_4(sc, ET_RXSTAT_HI, ET_ADDR_HI(rxst_ring->rsr_paddr));
   1479       1.1   jnemeth 	CSR_WRITE_4(sc, ET_RXSTAT_LO, ET_ADDR_LO(rxst_ring->rsr_paddr));
   1480       1.1   jnemeth 	CSR_WRITE_4(sc, ET_RXSTAT_CNT, ET_RX_NSTAT - 1);
   1481       1.1   jnemeth 	CSR_WRITE_4(sc, ET_RXSTAT_POS, 0);
   1482       1.1   jnemeth 	CSR_WRITE_4(sc, ET_RXSTAT_MINCNT, ((ET_RX_NSTAT * 15) / 100) - 1);
   1483       1.1   jnemeth 
   1484       1.1   jnemeth 	/* Match ET_RXSTAT_POS */
   1485       1.1   jnemeth 	rxst_ring->rsr_index = 0;
   1486       1.1   jnemeth 	rxst_ring->rsr_wrap = 0;
   1487       1.1   jnemeth 
   1488       1.1   jnemeth 	/*
   1489       1.1   jnemeth 	 * Install the 2nd RX descriptor ring
   1490       1.1   jnemeth 	 */
   1491       1.1   jnemeth 	rx_ring = &sc->sc_rx_ring[1];
   1492       1.1   jnemeth 	CSR_WRITE_4(sc, ET_RX_RING1_HI, ET_ADDR_HI(rx_ring->rr_paddr));
   1493       1.1   jnemeth 	CSR_WRITE_4(sc, ET_RX_RING1_LO, ET_ADDR_LO(rx_ring->rr_paddr));
   1494       1.1   jnemeth 	CSR_WRITE_4(sc, ET_RX_RING1_CNT, ET_RX_NDESC - 1);
   1495       1.1   jnemeth 	CSR_WRITE_4(sc, ET_RX_RING1_POS, ET_RX_RING1_POS_WRAP);
   1496       1.1   jnemeth 	CSR_WRITE_4(sc, ET_RX_RING1_MINCNT, ((ET_RX_NDESC * 15) / 100) - 1);
   1497       1.1   jnemeth 
   1498       1.1   jnemeth 	/* Match ET_RX_RING1_POS */
   1499       1.1   jnemeth 	rx_ring->rr_index = 0;
   1500       1.1   jnemeth 	rx_ring->rr_wrap = 1;
   1501       1.1   jnemeth 
   1502       1.1   jnemeth 	/*
   1503       1.1   jnemeth 	 * Install the 1st RX descriptor ring
   1504       1.1   jnemeth 	 */
   1505       1.1   jnemeth 	rx_ring = &sc->sc_rx_ring[0];
   1506       1.1   jnemeth 	CSR_WRITE_4(sc, ET_RX_RING0_HI, ET_ADDR_HI(rx_ring->rr_paddr));
   1507       1.1   jnemeth 	CSR_WRITE_4(sc, ET_RX_RING0_LO, ET_ADDR_LO(rx_ring->rr_paddr));
   1508       1.1   jnemeth 	CSR_WRITE_4(sc, ET_RX_RING0_CNT, ET_RX_NDESC - 1);
   1509       1.1   jnemeth 	CSR_WRITE_4(sc, ET_RX_RING0_POS, ET_RX_RING0_POS_WRAP);
   1510       1.1   jnemeth 	CSR_WRITE_4(sc, ET_RX_RING0_MINCNT, ((ET_RX_NDESC * 15) / 100) - 1);
   1511       1.1   jnemeth 
   1512       1.1   jnemeth 	/* Match ET_RX_RING0_POS */
   1513       1.1   jnemeth 	rx_ring->rr_index = 0;
   1514       1.1   jnemeth 	rx_ring->rr_wrap = 1;
   1515       1.1   jnemeth 
   1516       1.1   jnemeth 	/*
   1517       1.1   jnemeth 	 * RX intr moderation
   1518       1.1   jnemeth 	 */
   1519       1.1   jnemeth 	CSR_WRITE_4(sc, ET_RX_INTR_NPKTS, sc->sc_rx_intr_npkts);
   1520       1.1   jnemeth 	CSR_WRITE_4(sc, ET_RX_INTR_DELAY, sc->sc_rx_intr_delay);
   1521       1.1   jnemeth 
   1522       1.1   jnemeth 	return 0;
   1523       1.1   jnemeth }
   1524       1.1   jnemeth 
   1525  1.17.2.2    martin static int
   1526       1.1   jnemeth et_init_txdma(struct et_softc *sc)
   1527       1.1   jnemeth {
   1528       1.1   jnemeth 	struct et_txdesc_ring *tx_ring = &sc->sc_tx_ring;
   1529       1.1   jnemeth 	struct et_txstatus_data *txsd = &sc->sc_tx_status;
   1530       1.1   jnemeth 	int error;
   1531       1.1   jnemeth 
   1532       1.1   jnemeth 	error = et_stop_txdma(sc);
   1533       1.1   jnemeth 	if (error) {
   1534       1.1   jnemeth 		aprint_error_dev(sc->sc_dev, "can't init TX DMA engine\n");
   1535       1.1   jnemeth 		return error;
   1536       1.1   jnemeth 	}
   1537       1.1   jnemeth 
   1538       1.1   jnemeth 	/*
   1539       1.1   jnemeth 	 * Install TX descriptor ring
   1540       1.1   jnemeth 	 */
   1541       1.1   jnemeth 	CSR_WRITE_4(sc, ET_TX_RING_HI, ET_ADDR_HI(tx_ring->tr_paddr));
   1542       1.1   jnemeth 	CSR_WRITE_4(sc, ET_TX_RING_LO, ET_ADDR_LO(tx_ring->tr_paddr));
   1543       1.1   jnemeth 	CSR_WRITE_4(sc, ET_TX_RING_CNT, ET_TX_NDESC - 1);
   1544       1.1   jnemeth 
   1545       1.1   jnemeth 	/*
   1546       1.1   jnemeth 	 * Install TX status
   1547       1.1   jnemeth 	 */
   1548       1.1   jnemeth 	CSR_WRITE_4(sc, ET_TX_STATUS_HI, ET_ADDR_HI(txsd->txsd_paddr));
   1549       1.1   jnemeth 	CSR_WRITE_4(sc, ET_TX_STATUS_LO, ET_ADDR_LO(txsd->txsd_paddr));
   1550       1.1   jnemeth 
   1551       1.1   jnemeth 	CSR_WRITE_4(sc, ET_TX_READY_POS, 0);
   1552       1.1   jnemeth 
   1553       1.1   jnemeth 	/* Match ET_TX_READY_POS */
   1554       1.1   jnemeth 	tx_ring->tr_ready_index = 0;
   1555       1.1   jnemeth 	tx_ring->tr_ready_wrap = 0;
   1556       1.1   jnemeth 
   1557       1.1   jnemeth 	return 0;
   1558       1.1   jnemeth }
   1559       1.1   jnemeth 
   1560  1.17.2.2    martin static void
   1561       1.1   jnemeth et_init_mac(struct et_softc *sc)
   1562       1.1   jnemeth {
   1563       1.1   jnemeth 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1564       1.1   jnemeth 	const uint8_t *eaddr = CLLADDR(ifp->if_sadl);
   1565       1.1   jnemeth 	uint32_t val;
   1566       1.1   jnemeth 
   1567       1.1   jnemeth 	/* Reset MAC */
   1568       1.1   jnemeth 	CSR_WRITE_4(sc, ET_MAC_CFG1,
   1569       1.1   jnemeth 		    ET_MAC_CFG1_RST_TXFUNC | ET_MAC_CFG1_RST_RXFUNC |
   1570       1.1   jnemeth 		    ET_MAC_CFG1_RST_TXMC | ET_MAC_CFG1_RST_RXMC |
   1571       1.1   jnemeth 		    ET_MAC_CFG1_SIM_RST | ET_MAC_CFG1_SOFT_RST);
   1572       1.1   jnemeth 
   1573       1.1   jnemeth 	/*
   1574       1.1   jnemeth 	 * Setup inter packet gap
   1575       1.1   jnemeth 	 */
   1576       1.1   jnemeth 	val = __SHIFTIN(56, ET_IPG_NONB2B_1) |
   1577       1.1   jnemeth 	      __SHIFTIN(88, ET_IPG_NONB2B_2) |
   1578       1.1   jnemeth 	      __SHIFTIN(80, ET_IPG_MINIFG) |
   1579       1.1   jnemeth 	      __SHIFTIN(96, ET_IPG_B2B);
   1580       1.1   jnemeth 	CSR_WRITE_4(sc, ET_IPG, val);
   1581       1.1   jnemeth 
   1582       1.1   jnemeth 	/*
   1583       1.1   jnemeth 	 * Setup half duplex mode
   1584       1.1   jnemeth 	 */
   1585       1.1   jnemeth 	val = __SHIFTIN(10, ET_MAC_HDX_ALT_BEB_TRUNC) |
   1586       1.1   jnemeth 	      __SHIFTIN(15, ET_MAC_HDX_REXMIT_MAX) |
   1587       1.1   jnemeth 	      __SHIFTIN(55, ET_MAC_HDX_COLLWIN) |
   1588       1.1   jnemeth 	      ET_MAC_HDX_EXC_DEFER;
   1589       1.1   jnemeth 	CSR_WRITE_4(sc, ET_MAC_HDX, val);
   1590       1.1   jnemeth 
   1591       1.1   jnemeth 	/* Clear MAC control */
   1592       1.1   jnemeth 	CSR_WRITE_4(sc, ET_MAC_CTRL, 0);
   1593       1.1   jnemeth 
   1594       1.1   jnemeth 	/* Reset MII */
   1595       1.1   jnemeth 	CSR_WRITE_4(sc, ET_MII_CFG, ET_MII_CFG_CLKRST);
   1596       1.1   jnemeth 
   1597       1.1   jnemeth 	/*
   1598       1.1   jnemeth 	 * Set MAC address
   1599       1.1   jnemeth 	 */
   1600       1.1   jnemeth 	val = eaddr[2] | (eaddr[3] << 8) | (eaddr[4] << 16) | (eaddr[5] << 24);
   1601       1.1   jnemeth 	CSR_WRITE_4(sc, ET_MAC_ADDR1, val);
   1602       1.1   jnemeth 	val = (eaddr[0] << 16) | (eaddr[1] << 24);
   1603       1.1   jnemeth 	CSR_WRITE_4(sc, ET_MAC_ADDR2, val);
   1604       1.1   jnemeth 
   1605       1.1   jnemeth 	/* Set max frame length */
   1606       1.1   jnemeth 	CSR_WRITE_4(sc, ET_MAX_FRMLEN,
   1607       1.1   jnemeth 		    ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN + ifp->if_mtu + ETHER_CRC_LEN);
   1608       1.1   jnemeth 
   1609       1.1   jnemeth 	/* Bring MAC out of reset state */
   1610       1.1   jnemeth 	CSR_WRITE_4(sc, ET_MAC_CFG1, 0);
   1611       1.1   jnemeth }
   1612       1.1   jnemeth 
   1613  1.17.2.2    martin static void
   1614       1.1   jnemeth et_init_rxmac(struct et_softc *sc)
   1615       1.1   jnemeth {
   1616       1.1   jnemeth 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1617       1.1   jnemeth 	const uint8_t *eaddr = CLLADDR(ifp->if_sadl);
   1618       1.1   jnemeth 	uint32_t val;
   1619       1.1   jnemeth 	int i;
   1620       1.1   jnemeth 
   1621       1.1   jnemeth 	/* Disable RX MAC and WOL */
   1622       1.1   jnemeth 	CSR_WRITE_4(sc, ET_RXMAC_CTRL, ET_RXMAC_CTRL_WOL_DISABLE);
   1623       1.1   jnemeth 
   1624       1.1   jnemeth 	/*
   1625       1.1   jnemeth 	 * Clear all WOL related registers
   1626       1.1   jnemeth 	 */
   1627       1.1   jnemeth 	for (i = 0; i < 3; ++i)
   1628       1.1   jnemeth 		CSR_WRITE_4(sc, ET_WOL_CRC + (i * 4), 0);
   1629       1.1   jnemeth 	for (i = 0; i < 20; ++i)
   1630       1.1   jnemeth 		CSR_WRITE_4(sc, ET_WOL_MASK + (i * 4), 0);
   1631       1.1   jnemeth 
   1632       1.1   jnemeth 	/*
   1633       1.1   jnemeth 	 * Set WOL source address.  XXX is this necessary?
   1634       1.1   jnemeth 	 */
   1635       1.1   jnemeth 	val = (eaddr[2] << 24) | (eaddr[3] << 16) | (eaddr[4] << 8) | eaddr[5];
   1636       1.1   jnemeth 	CSR_WRITE_4(sc, ET_WOL_SA_LO, val);
   1637       1.1   jnemeth 	val = (eaddr[0] << 8) | eaddr[1];
   1638       1.1   jnemeth 	CSR_WRITE_4(sc, ET_WOL_SA_HI, val);
   1639       1.1   jnemeth 
   1640       1.1   jnemeth 	/* Clear packet filters */
   1641       1.1   jnemeth 	CSR_WRITE_4(sc, ET_PKTFILT, 0);
   1642       1.1   jnemeth 
   1643       1.1   jnemeth 	/* No ucast filtering */
   1644       1.1   jnemeth 	CSR_WRITE_4(sc, ET_UCAST_FILTADDR1, 0);
   1645       1.1   jnemeth 	CSR_WRITE_4(sc, ET_UCAST_FILTADDR2, 0);
   1646       1.1   jnemeth 	CSR_WRITE_4(sc, ET_UCAST_FILTADDR3, 0);
   1647       1.1   jnemeth 
   1648       1.1   jnemeth 	if (ifp->if_mtu > 8192) {
   1649       1.1   jnemeth 		/*
   1650       1.1   jnemeth 		 * In order to transmit jumbo packets greater than 8k,
   1651       1.1   jnemeth 		 * the FIFO between RX MAC and RX DMA needs to be reduced
   1652       1.1   jnemeth 		 * in size to (16k - MTU).  In order to implement this, we
   1653       1.1   jnemeth 		 * must use "cut through" mode in the RX MAC, which chops
   1654       1.1   jnemeth 		 * packets down into segments which are (max_size * 16).
   1655       1.1   jnemeth 		 * In this case we selected 256 bytes, since this is the
   1656       1.1   jnemeth 		 * size of the PCI-Express TLP's that the 1310 uses.
   1657       1.1   jnemeth 		 */
   1658       1.1   jnemeth 		val = __SHIFTIN(16, ET_RXMAC_MC_SEGSZ_MAX) |
   1659       1.1   jnemeth 		      ET_RXMAC_MC_SEGSZ_ENABLE;
   1660       1.1   jnemeth 	} else {
   1661       1.1   jnemeth 		val = 0;
   1662       1.1   jnemeth 	}
   1663       1.1   jnemeth 	CSR_WRITE_4(sc, ET_RXMAC_MC_SEGSZ, val);
   1664       1.1   jnemeth 
   1665       1.1   jnemeth 	CSR_WRITE_4(sc, ET_RXMAC_MC_WATERMARK, 0);
   1666       1.1   jnemeth 
   1667       1.1   jnemeth 	/* Initialize RX MAC management register */
   1668       1.1   jnemeth 	CSR_WRITE_4(sc, ET_RXMAC_MGT, 0);
   1669       1.1   jnemeth 
   1670       1.1   jnemeth 	CSR_WRITE_4(sc, ET_RXMAC_SPACE_AVL, 0);
   1671       1.1   jnemeth 
   1672       1.1   jnemeth 	CSR_WRITE_4(sc, ET_RXMAC_MGT,
   1673       1.1   jnemeth 		    ET_RXMAC_MGT_PASS_ECRC |
   1674       1.1   jnemeth 		    ET_RXMAC_MGT_PASS_ELEN |
   1675       1.1   jnemeth 		    ET_RXMAC_MGT_PASS_ETRUNC |
   1676       1.1   jnemeth 		    ET_RXMAC_MGT_CHECK_PKT);
   1677       1.1   jnemeth 
   1678       1.1   jnemeth 	/*
   1679       1.1   jnemeth 	 * Configure runt filtering (may not work on certain chip generation)
   1680       1.1   jnemeth 	 */
   1681       1.1   jnemeth 	val = __SHIFTIN(ETHER_MIN_LEN, ET_PKTFILT_MINLEN) | ET_PKTFILT_FRAG;
   1682       1.1   jnemeth 	CSR_WRITE_4(sc, ET_PKTFILT, val);
   1683       1.1   jnemeth 
   1684       1.1   jnemeth 	/* Enable RX MAC but leave WOL disabled */
   1685       1.1   jnemeth 	CSR_WRITE_4(sc, ET_RXMAC_CTRL,
   1686       1.1   jnemeth 		    ET_RXMAC_CTRL_WOL_DISABLE | ET_RXMAC_CTRL_ENABLE);
   1687       1.1   jnemeth 
   1688       1.1   jnemeth 	/*
   1689       1.1   jnemeth 	 * Setup multicast hash and allmulti/promisc mode
   1690       1.1   jnemeth 	 */
   1691       1.1   jnemeth 	et_setmulti(sc);
   1692       1.1   jnemeth }
   1693       1.1   jnemeth 
   1694  1.17.2.2    martin static void
   1695       1.1   jnemeth et_init_txmac(struct et_softc *sc)
   1696       1.1   jnemeth {
   1697  1.17.2.2    martin 
   1698       1.1   jnemeth 	/* Disable TX MAC and FC(?) */
   1699       1.1   jnemeth 	CSR_WRITE_4(sc, ET_TXMAC_CTRL, ET_TXMAC_CTRL_FC_DISABLE);
   1700       1.1   jnemeth 
   1701       1.1   jnemeth 	/* No flow control yet */
   1702       1.1   jnemeth 	CSR_WRITE_4(sc, ET_TXMAC_FLOWCTRL, 0);
   1703       1.1   jnemeth 
   1704       1.1   jnemeth 	/* Enable TX MAC but leave FC(?) diabled */
   1705       1.1   jnemeth 	CSR_WRITE_4(sc, ET_TXMAC_CTRL,
   1706       1.1   jnemeth 		    ET_TXMAC_CTRL_ENABLE | ET_TXMAC_CTRL_FC_DISABLE);
   1707       1.1   jnemeth }
   1708       1.1   jnemeth 
   1709  1.17.2.2    martin static int
   1710       1.1   jnemeth et_start_rxdma(struct et_softc *sc)
   1711       1.1   jnemeth {
   1712       1.1   jnemeth 	uint32_t val = 0;
   1713       1.1   jnemeth 
   1714       1.1   jnemeth 	val |= __SHIFTIN(sc->sc_rx_data[0].rbd_bufsize,
   1715       1.1   jnemeth 			 ET_RXDMA_CTRL_RING0_SIZE) |
   1716       1.1   jnemeth 	       ET_RXDMA_CTRL_RING0_ENABLE;
   1717       1.1   jnemeth 	val |= __SHIFTIN(sc->sc_rx_data[1].rbd_bufsize,
   1718       1.1   jnemeth 			 ET_RXDMA_CTRL_RING1_SIZE) |
   1719       1.1   jnemeth 	       ET_RXDMA_CTRL_RING1_ENABLE;
   1720       1.1   jnemeth 
   1721       1.1   jnemeth 	CSR_WRITE_4(sc, ET_RXDMA_CTRL, val);
   1722       1.1   jnemeth 
   1723       1.1   jnemeth 	DELAY(5);
   1724       1.1   jnemeth 
   1725       1.1   jnemeth 	if (CSR_READ_4(sc, ET_RXDMA_CTRL) & ET_RXDMA_CTRL_HALTED) {
   1726       1.1   jnemeth 		aprint_error_dev(sc->sc_dev, "can't start RX DMA engine\n");
   1727       1.1   jnemeth 		return ETIMEDOUT;
   1728       1.1   jnemeth 	}
   1729       1.1   jnemeth 	return 0;
   1730       1.1   jnemeth }
   1731       1.1   jnemeth 
   1732  1.17.2.2    martin static int
   1733       1.1   jnemeth et_start_txdma(struct et_softc *sc)
   1734       1.1   jnemeth {
   1735       1.1   jnemeth 
   1736  1.17.2.2    martin 	CSR_WRITE_4(sc, ET_TXDMA_CTRL, ET_TXDMA_CTRL_SINGLE_EPKT);
   1737       1.1   jnemeth 	return 0;
   1738       1.1   jnemeth }
   1739       1.1   jnemeth 
   1740  1.17.2.2    martin static void
   1741       1.1   jnemeth et_rxeof(struct et_softc *sc)
   1742       1.1   jnemeth {
   1743       1.1   jnemeth 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1744       1.1   jnemeth 	struct et_rxstatus_data *rxsd = &sc->sc_rx_status;
   1745       1.1   jnemeth 	struct et_rxstat_ring *rxst_ring = &sc->sc_rxstat_ring;
   1746       1.1   jnemeth 	uint32_t rxs_stat_ring;
   1747       1.1   jnemeth 	int rxst_wrap, rxst_index;
   1748       1.1   jnemeth 
   1749  1.17.2.2    martin 	if ((sc->sc_flags & ET_FLAG_TXRX_ENABLED) == 0)
   1750  1.17.2.2    martin 		return;
   1751  1.17.2.2    martin 
   1752       1.1   jnemeth 	bus_dmamap_sync(sc->sc_dmat, rxsd->rxsd_dmap, 0,
   1753       1.1   jnemeth 	    rxsd->rxsd_dmap->dm_mapsize, BUS_DMASYNC_POSTREAD);
   1754       1.1   jnemeth 	bus_dmamap_sync(sc->sc_dmat, rxst_ring->rsr_dmap, 0,
   1755       1.1   jnemeth 	    rxst_ring->rsr_dmap->dm_mapsize, BUS_DMASYNC_POSTREAD);
   1756       1.1   jnemeth 
   1757       1.1   jnemeth 	rxs_stat_ring = rxsd->rxsd_status->rxs_stat_ring;
   1758       1.1   jnemeth 	rxst_wrap = (rxs_stat_ring & ET_RXS_STATRING_WRAP) ? 1 : 0;
   1759       1.1   jnemeth 	rxst_index = __SHIFTOUT(rxs_stat_ring, ET_RXS_STATRING_INDEX);
   1760       1.1   jnemeth 
   1761       1.1   jnemeth 	while (rxst_index != rxst_ring->rsr_index ||
   1762       1.1   jnemeth 	       rxst_wrap != rxst_ring->rsr_wrap) {
   1763       1.1   jnemeth 		struct et_rxbuf_data *rbd;
   1764       1.1   jnemeth 		struct et_rxdesc_ring *rx_ring;
   1765       1.1   jnemeth 		struct et_rxstat *st;
   1766       1.1   jnemeth 		struct et_rxbuf *rb;
   1767       1.1   jnemeth 		struct mbuf *m;
   1768       1.1   jnemeth 		int buflen, buf_idx, ring_idx;
   1769       1.1   jnemeth 		uint32_t rxstat_pos, rxring_pos;
   1770       1.1   jnemeth 
   1771       1.6    dyoung 		KASSERT(rxst_ring->rsr_index < ET_RX_NSTAT);
   1772       1.1   jnemeth 		st = &rxst_ring->rsr_stat[rxst_ring->rsr_index];
   1773       1.1   jnemeth 
   1774       1.1   jnemeth 		buflen = __SHIFTOUT(st->rxst_info2, ET_RXST_INFO2_LEN);
   1775       1.1   jnemeth 		buf_idx = __SHIFTOUT(st->rxst_info2, ET_RXST_INFO2_BUFIDX);
   1776       1.1   jnemeth 		ring_idx = __SHIFTOUT(st->rxst_info2, ET_RXST_INFO2_RINGIDX);
   1777       1.1   jnemeth 
   1778       1.1   jnemeth 		if (++rxst_ring->rsr_index == ET_RX_NSTAT) {
   1779       1.1   jnemeth 			rxst_ring->rsr_index = 0;
   1780       1.1   jnemeth 			rxst_ring->rsr_wrap ^= 1;
   1781       1.1   jnemeth 		}
   1782       1.1   jnemeth 		rxstat_pos = __SHIFTIN(rxst_ring->rsr_index,
   1783       1.1   jnemeth 				       ET_RXSTAT_POS_INDEX);
   1784       1.1   jnemeth 		if (rxst_ring->rsr_wrap)
   1785       1.1   jnemeth 			rxstat_pos |= ET_RXSTAT_POS_WRAP;
   1786       1.1   jnemeth 		CSR_WRITE_4(sc, ET_RXSTAT_POS, rxstat_pos);
   1787       1.1   jnemeth 
   1788       1.1   jnemeth 		if (ring_idx >= ET_RX_NRING) {
   1789  1.17.2.2    martin 			if_statinc(ifp, if_ierrors);
   1790       1.1   jnemeth 			aprint_error_dev(sc->sc_dev, "invalid ring index %d\n",
   1791       1.1   jnemeth 			    ring_idx);
   1792       1.1   jnemeth 			continue;
   1793       1.1   jnemeth 		}
   1794       1.1   jnemeth 		if (buf_idx >= ET_RX_NDESC) {
   1795  1.17.2.2    martin 			if_statinc(ifp, if_ierrors);
   1796       1.1   jnemeth 			aprint_error_dev(sc->sc_dev, "invalid buf index %d\n",
   1797       1.1   jnemeth 			    buf_idx);
   1798       1.1   jnemeth 			continue;
   1799       1.1   jnemeth 		}
   1800       1.1   jnemeth 
   1801       1.1   jnemeth 		rbd = &sc->sc_rx_data[ring_idx];
   1802       1.1   jnemeth 		rb = &rbd->rbd_buf[buf_idx];
   1803       1.1   jnemeth 		m = rb->rb_mbuf;
   1804       1.1   jnemeth 		bus_dmamap_sync(sc->sc_dmat, rb->rb_dmap, 0,
   1805       1.1   jnemeth 		    rb->rb_dmap->dm_mapsize, BUS_DMASYNC_POSTREAD);
   1806       1.1   jnemeth 
   1807       1.1   jnemeth 		if (rbd->rbd_newbuf(rbd, buf_idx, 0) == 0) {
   1808       1.1   jnemeth 			if (buflen < ETHER_CRC_LEN) {
   1809       1.1   jnemeth 				m_freem(m);
   1810  1.17.2.2    martin 				if_statinc(ifp, if_ierrors);
   1811       1.1   jnemeth 			} else {
   1812       1.1   jnemeth 				m->m_pkthdr.len = m->m_len = buflen -
   1813       1.1   jnemeth 				    ETHER_CRC_LEN;
   1814      1.12     ozaki 				m_set_rcvif(m, ifp);
   1815       1.1   jnemeth 
   1816      1.10     ozaki 				if_percpuq_enqueue(ifp->if_percpuq, m);
   1817       1.1   jnemeth 			}
   1818       1.1   jnemeth 		} else {
   1819  1.17.2.2    martin 			if_statinc(ifp, if_ierrors);
   1820       1.1   jnemeth 		}
   1821       1.1   jnemeth 
   1822       1.1   jnemeth 		rx_ring = &sc->sc_rx_ring[ring_idx];
   1823       1.1   jnemeth 
   1824       1.1   jnemeth 		if (buf_idx != rx_ring->rr_index) {
   1825       1.1   jnemeth 			aprint_error_dev(sc->sc_dev, "WARNING!! ring %d, "
   1826       1.1   jnemeth 			    "buf_idx %d, rr_idx %d\n",
   1827       1.1   jnemeth 			    ring_idx, buf_idx, rx_ring->rr_index);
   1828       1.1   jnemeth 		}
   1829       1.1   jnemeth 
   1830       1.6    dyoung 		KASSERT(rx_ring->rr_index < ET_RX_NDESC);
   1831       1.1   jnemeth 		if (++rx_ring->rr_index == ET_RX_NDESC) {
   1832       1.1   jnemeth 			rx_ring->rr_index = 0;
   1833       1.1   jnemeth 			rx_ring->rr_wrap ^= 1;
   1834       1.1   jnemeth 		}
   1835       1.1   jnemeth 		rxring_pos = __SHIFTIN(rx_ring->rr_index, ET_RX_RING_POS_INDEX);
   1836       1.1   jnemeth 		if (rx_ring->rr_wrap)
   1837       1.1   jnemeth 			rxring_pos |= ET_RX_RING_POS_WRAP;
   1838       1.1   jnemeth 		CSR_WRITE_4(sc, rx_ring->rr_posreg, rxring_pos);
   1839       1.1   jnemeth 	}
   1840       1.1   jnemeth }
   1841       1.1   jnemeth 
   1842  1.17.2.2    martin static int
   1843       1.1   jnemeth et_encap(struct et_softc *sc, struct mbuf **m0)
   1844       1.1   jnemeth {
   1845       1.1   jnemeth 	struct mbuf *m = *m0;
   1846       1.1   jnemeth 	struct et_txdesc_ring *tx_ring = &sc->sc_tx_ring;
   1847       1.1   jnemeth 	struct et_txbuf_data *tbd = &sc->sc_tx_data;
   1848       1.1   jnemeth 	struct et_txdesc *td;
   1849       1.1   jnemeth 	bus_dmamap_t map;
   1850       1.1   jnemeth 	int error, maxsegs, first_idx, last_idx, i;
   1851       1.1   jnemeth 	uint32_t tx_ready_pos, last_td_ctrl2;
   1852       1.1   jnemeth 
   1853       1.1   jnemeth 	maxsegs = ET_TX_NDESC - tbd->tbd_used;
   1854       1.1   jnemeth 	if (maxsegs > ET_NSEG_MAX)
   1855       1.1   jnemeth 		maxsegs = ET_NSEG_MAX;
   1856       1.6    dyoung 	KASSERTMSG(maxsegs >= ET_NSEG_SPARE,
   1857       1.6    dyoung 		"not enough spare TX desc (%d)\n", maxsegs);
   1858       1.1   jnemeth 
   1859       1.6    dyoung 	KASSERT(tx_ring->tr_ready_index < ET_TX_NDESC);
   1860       1.1   jnemeth 	first_idx = tx_ring->tr_ready_index;
   1861       1.1   jnemeth 	map = tbd->tbd_buf[first_idx].tb_dmap;
   1862       1.1   jnemeth 
   1863       1.1   jnemeth 	error = bus_dmamap_load_mbuf(sc->sc_dmat, map, m,
   1864       1.1   jnemeth 	    BUS_DMA_NOWAIT);
   1865       1.1   jnemeth 	if (!error && map->dm_nsegs == 0) {
   1866       1.1   jnemeth 		bus_dmamap_unload(sc->sc_dmat, map);
   1867       1.1   jnemeth 		error = EFBIG;
   1868       1.1   jnemeth 	}
   1869       1.1   jnemeth 	if (error && error != EFBIG) {
   1870       1.1   jnemeth 		aprint_error_dev(sc->sc_dev, "can't load TX mbuf");
   1871       1.1   jnemeth 		goto back;
   1872       1.1   jnemeth 	}
   1873       1.1   jnemeth 	if (error) {	/* error == EFBIG */
   1874       1.1   jnemeth 		struct mbuf *m_new;
   1875       1.1   jnemeth 
   1876       1.1   jnemeth 		error = 0;
   1877       1.1   jnemeth 
   1878       1.1   jnemeth 		MGETHDR(m_new, M_DONTWAIT, MT_DATA);
   1879       1.1   jnemeth 		if (m_new == NULL) {
   1880       1.1   jnemeth 			aprint_error_dev(sc->sc_dev, "can't defrag TX mbuf\n");
   1881       1.1   jnemeth 			error = ENOBUFS;
   1882       1.1   jnemeth 			goto back;
   1883       1.1   jnemeth 		}
   1884       1.1   jnemeth 
   1885  1.17.2.1  christos 		m_copy_pkthdr(m_new, m);
   1886       1.1   jnemeth 		if (m->m_pkthdr.len > MHLEN) {
   1887       1.1   jnemeth 			MCLGET(m_new, M_DONTWAIT);
   1888       1.1   jnemeth 			if (!(m_new->m_flags & M_EXT)) {
   1889       1.1   jnemeth 				m_freem(m_new);
   1890       1.1   jnemeth 				error = ENOBUFS;
   1891       1.1   jnemeth 			}
   1892       1.1   jnemeth 		}
   1893       1.1   jnemeth 
   1894       1.1   jnemeth 		if (error) {
   1895       1.1   jnemeth 			aprint_error_dev(sc->sc_dev, "can't defrag TX buffer\n");
   1896       1.1   jnemeth 			goto back;
   1897       1.1   jnemeth 		}
   1898       1.1   jnemeth 
   1899       1.1   jnemeth 		m_copydata(m, 0, m->m_pkthdr.len, mtod(m_new, void *));
   1900       1.1   jnemeth 		m_freem(m);
   1901       1.1   jnemeth 		m_new->m_len = m_new->m_pkthdr.len;
   1902       1.1   jnemeth 		*m0 = m = m_new;
   1903       1.1   jnemeth 
   1904       1.1   jnemeth 		error = bus_dmamap_load_mbuf(sc->sc_dmat, map, m,
   1905       1.1   jnemeth 					     BUS_DMA_NOWAIT);
   1906       1.1   jnemeth 		if (error || map->dm_nsegs == 0) {
   1907       1.1   jnemeth 			if (map->dm_nsegs == 0) {
   1908       1.1   jnemeth 				bus_dmamap_unload(sc->sc_dmat, map);
   1909       1.1   jnemeth 				error = EFBIG;
   1910       1.1   jnemeth 			}
   1911       1.1   jnemeth 			aprint_error_dev(sc->sc_dev, "can't load defraged TX mbuf\n");
   1912       1.1   jnemeth 			goto back;
   1913       1.1   jnemeth 		}
   1914       1.1   jnemeth 	}
   1915       1.1   jnemeth 
   1916       1.1   jnemeth 	bus_dmamap_sync(sc->sc_dmat, map, 0, map->dm_mapsize,
   1917       1.1   jnemeth 	    BUS_DMASYNC_PREWRITE);
   1918       1.1   jnemeth 
   1919       1.1   jnemeth 	last_td_ctrl2 = ET_TDCTRL2_LAST_FRAG;
   1920       1.1   jnemeth 	sc->sc_tx += map->dm_nsegs;
   1921       1.1   jnemeth 	if (sc->sc_tx / sc->sc_tx_intr_nsegs != sc->sc_tx_intr) {
   1922       1.1   jnemeth 		sc->sc_tx_intr = sc->sc_tx / sc->sc_tx_intr_nsegs;
   1923       1.1   jnemeth 		last_td_ctrl2 |= ET_TDCTRL2_INTR;
   1924       1.1   jnemeth 	}
   1925       1.1   jnemeth 
   1926       1.1   jnemeth 	last_idx = -1;
   1927       1.1   jnemeth 	for (i = 0; i < map->dm_nsegs; ++i) {
   1928       1.1   jnemeth 		int idx;
   1929       1.1   jnemeth 
   1930       1.1   jnemeth 		idx = (first_idx + i) % ET_TX_NDESC;
   1931       1.1   jnemeth 		td = &tx_ring->tr_desc[idx];
   1932       1.1   jnemeth 		td->td_addr_hi = ET_ADDR_HI(map->dm_segs[i].ds_addr);
   1933       1.1   jnemeth 		td->td_addr_lo = ET_ADDR_LO(map->dm_segs[i].ds_addr);
   1934       1.1   jnemeth 		td->td_ctrl1 =
   1935       1.1   jnemeth 		    __SHIFTIN(map->dm_segs[i].ds_len, ET_TDCTRL1_LEN);
   1936       1.1   jnemeth 
   1937       1.1   jnemeth 		if (i == map->dm_nsegs - 1) {	/* Last frag */
   1938       1.1   jnemeth 			td->td_ctrl2 = last_td_ctrl2;
   1939       1.1   jnemeth 			last_idx = idx;
   1940       1.1   jnemeth 		}
   1941       1.1   jnemeth 
   1942       1.6    dyoung 		KASSERT(tx_ring->tr_ready_index < ET_TX_NDESC);
   1943       1.1   jnemeth 		if (++tx_ring->tr_ready_index == ET_TX_NDESC) {
   1944       1.1   jnemeth 			tx_ring->tr_ready_index = 0;
   1945       1.1   jnemeth 			tx_ring->tr_ready_wrap ^= 1;
   1946       1.1   jnemeth 		}
   1947       1.1   jnemeth 	}
   1948       1.1   jnemeth 	td = &tx_ring->tr_desc[first_idx];
   1949       1.1   jnemeth 	td->td_ctrl2 |= ET_TDCTRL2_FIRST_FRAG;	/* First frag */
   1950       1.1   jnemeth 
   1951       1.6    dyoung 	KASSERT(last_idx >= 0);
   1952       1.1   jnemeth 	tbd->tbd_buf[first_idx].tb_dmap = tbd->tbd_buf[last_idx].tb_dmap;
   1953       1.1   jnemeth 	tbd->tbd_buf[last_idx].tb_dmap = map;
   1954       1.1   jnemeth 	tbd->tbd_buf[last_idx].tb_mbuf = m;
   1955       1.1   jnemeth 
   1956       1.1   jnemeth 	tbd->tbd_used += map->dm_nsegs;
   1957       1.6    dyoung 	KASSERT(tbd->tbd_used <= ET_TX_NDESC);
   1958       1.1   jnemeth 
   1959       1.1   jnemeth 	bus_dmamap_sync(sc->sc_dmat, tx_ring->tr_dmap, 0,
   1960       1.1   jnemeth 	    tx_ring->tr_dmap->dm_mapsize, BUS_DMASYNC_PREWRITE);
   1961       1.1   jnemeth 
   1962       1.1   jnemeth 	tx_ready_pos = __SHIFTIN(tx_ring->tr_ready_index,
   1963       1.1   jnemeth 		       ET_TX_READY_POS_INDEX);
   1964       1.1   jnemeth 	if (tx_ring->tr_ready_wrap)
   1965       1.1   jnemeth 		tx_ready_pos |= ET_TX_READY_POS_WRAP;
   1966       1.1   jnemeth 	CSR_WRITE_4(sc, ET_TX_READY_POS, tx_ready_pos);
   1967       1.1   jnemeth 
   1968       1.1   jnemeth 	error = 0;
   1969       1.1   jnemeth back:
   1970       1.1   jnemeth 	if (error) {
   1971       1.1   jnemeth 		m_freem(m);
   1972       1.1   jnemeth 		*m0 = NULL;
   1973       1.1   jnemeth 	}
   1974       1.1   jnemeth 	return error;
   1975       1.1   jnemeth }
   1976       1.1   jnemeth 
   1977  1.17.2.2    martin static void
   1978       1.1   jnemeth et_txeof(struct et_softc *sc)
   1979       1.1   jnemeth {
   1980       1.1   jnemeth 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1981       1.1   jnemeth 	struct et_txdesc_ring *tx_ring = &sc->sc_tx_ring;
   1982       1.1   jnemeth 	struct et_txbuf_data *tbd = &sc->sc_tx_data;
   1983       1.1   jnemeth 	uint32_t tx_done;
   1984       1.1   jnemeth 	int end, wrap;
   1985       1.1   jnemeth 
   1986  1.17.2.2    martin 	if ((sc->sc_flags & ET_FLAG_TXRX_ENABLED) == 0)
   1987  1.17.2.2    martin 		return;
   1988  1.17.2.2    martin 
   1989       1.1   jnemeth 	if (tbd->tbd_used == 0)
   1990       1.1   jnemeth 		return;
   1991       1.1   jnemeth 
   1992       1.1   jnemeth 	tx_done = CSR_READ_4(sc, ET_TX_DONE_POS);
   1993       1.1   jnemeth 	end = __SHIFTOUT(tx_done, ET_TX_DONE_POS_INDEX);
   1994       1.1   jnemeth 	wrap = (tx_done & ET_TX_DONE_POS_WRAP) ? 1 : 0;
   1995       1.1   jnemeth 
   1996       1.1   jnemeth 	while (tbd->tbd_start_index != end || tbd->tbd_start_wrap != wrap) {
   1997       1.1   jnemeth 		struct et_txbuf *tb;
   1998       1.1   jnemeth 
   1999       1.6    dyoung 		KASSERT(tbd->tbd_start_index < ET_TX_NDESC);
   2000       1.1   jnemeth 		tb = &tbd->tbd_buf[tbd->tbd_start_index];
   2001       1.1   jnemeth 
   2002       1.1   jnemeth 		bzero(&tx_ring->tr_desc[tbd->tbd_start_index],
   2003       1.1   jnemeth 		      sizeof(struct et_txdesc));
   2004       1.1   jnemeth 		bus_dmamap_sync(sc->sc_dmat, tx_ring->tr_dmap, 0,
   2005       1.1   jnemeth 		    tx_ring->tr_dmap->dm_mapsize, BUS_DMASYNC_PREWRITE);
   2006       1.1   jnemeth 
   2007       1.1   jnemeth 		if (tb->tb_mbuf != NULL) {
   2008       1.1   jnemeth 			bus_dmamap_unload(sc->sc_dmat, tb->tb_dmap);
   2009       1.1   jnemeth 			m_freem(tb->tb_mbuf);
   2010       1.1   jnemeth 			tb->tb_mbuf = NULL;
   2011  1.17.2.2    martin 			if_statinc(ifp, if_opackets);
   2012       1.1   jnemeth 		}
   2013       1.1   jnemeth 
   2014       1.1   jnemeth 		if (++tbd->tbd_start_index == ET_TX_NDESC) {
   2015       1.1   jnemeth 			tbd->tbd_start_index = 0;
   2016       1.1   jnemeth 			tbd->tbd_start_wrap ^= 1;
   2017       1.1   jnemeth 		}
   2018       1.1   jnemeth 
   2019       1.6    dyoung 		KASSERT(tbd->tbd_used > 0);
   2020       1.1   jnemeth 		tbd->tbd_used--;
   2021       1.1   jnemeth 	}
   2022       1.1   jnemeth 
   2023       1.1   jnemeth 	if (tbd->tbd_used == 0) {
   2024       1.1   jnemeth 		callout_stop(&sc->sc_txtick);
   2025       1.1   jnemeth 		ifp->if_timer = 0;
   2026       1.1   jnemeth 	}
   2027       1.1   jnemeth 	if (tbd->tbd_used + ET_NSEG_SPARE <= ET_TX_NDESC)
   2028       1.1   jnemeth 		ifp->if_flags &= ~IFF_OACTIVE;
   2029       1.1   jnemeth 
   2030      1.13     ozaki 	if_schedule_deferred_start(ifp);
   2031       1.1   jnemeth }
   2032       1.1   jnemeth 
   2033  1.17.2.2    martin static void
   2034       1.1   jnemeth et_txtick(void *xsc)
   2035       1.1   jnemeth {
   2036       1.1   jnemeth 	struct et_softc *sc = xsc;
   2037       1.1   jnemeth 	int s;
   2038       1.1   jnemeth 
   2039       1.1   jnemeth 	s = splnet();
   2040       1.1   jnemeth 	et_txeof(sc);
   2041       1.1   jnemeth 	splx(s);
   2042       1.1   jnemeth }
   2043       1.1   jnemeth 
   2044  1.17.2.2    martin static void
   2045       1.1   jnemeth et_tick(void *xsc)
   2046       1.1   jnemeth {
   2047       1.1   jnemeth 	struct et_softc *sc = xsc;
   2048       1.1   jnemeth 	int s;
   2049       1.1   jnemeth 
   2050       1.1   jnemeth 	s = splnet();
   2051       1.1   jnemeth 	mii_tick(&sc->sc_miibus);
   2052       1.1   jnemeth 	callout_schedule(&sc->sc_tick, hz);
   2053       1.1   jnemeth 	splx(s);
   2054       1.1   jnemeth }
   2055       1.1   jnemeth 
   2056  1.17.2.2    martin static int
   2057       1.1   jnemeth et_newbuf_cluster(struct et_rxbuf_data *rbd, int buf_idx, int init)
   2058       1.1   jnemeth {
   2059       1.1   jnemeth 	return et_newbuf(rbd, buf_idx, init, MCLBYTES);
   2060       1.1   jnemeth }
   2061       1.1   jnemeth 
   2062  1.17.2.2    martin static int
   2063       1.1   jnemeth et_newbuf_hdr(struct et_rxbuf_data *rbd, int buf_idx, int init)
   2064       1.1   jnemeth {
   2065       1.1   jnemeth 	return et_newbuf(rbd, buf_idx, init, MHLEN);
   2066       1.1   jnemeth }
   2067       1.1   jnemeth 
   2068  1.17.2.2    martin static int
   2069       1.1   jnemeth et_newbuf(struct et_rxbuf_data *rbd, int buf_idx, int init, int len0)
   2070       1.1   jnemeth {
   2071       1.1   jnemeth 	struct et_softc *sc = rbd->rbd_softc;
   2072       1.1   jnemeth 	struct et_rxdesc_ring *rx_ring;
   2073       1.1   jnemeth 	struct et_rxdesc *desc;
   2074       1.1   jnemeth 	struct et_rxbuf *rb;
   2075       1.1   jnemeth 	struct mbuf *m;
   2076       1.1   jnemeth 	bus_dmamap_t dmap;
   2077       1.1   jnemeth 	int error, len;
   2078       1.1   jnemeth 
   2079       1.6    dyoung 	KASSERT(buf_idx < ET_RX_NDESC);
   2080       1.1   jnemeth 	rb = &rbd->rbd_buf[buf_idx];
   2081       1.1   jnemeth 
   2082       1.1   jnemeth 	if (len0 >= MINCLSIZE) {
   2083       1.1   jnemeth 		MGETHDR(m, init ? M_WAITOK : M_DONTWAIT, MT_DATA);
   2084       1.1   jnemeth 		if (m == NULL)
   2085       1.1   jnemeth 			return (ENOBUFS);
   2086       1.1   jnemeth 		MCLGET(m, init ? M_WAITOK : M_DONTWAIT);
   2087      1.15  riastrad 		if ((m->m_flags & M_EXT) == 0) {
   2088      1.15  riastrad 			m_freem(m);
   2089      1.15  riastrad 			return (ENOBUFS);
   2090      1.15  riastrad 		}
   2091       1.1   jnemeth 		len = MCLBYTES;
   2092       1.1   jnemeth 	} else {
   2093       1.1   jnemeth 		MGETHDR(m, init ? M_WAITOK : M_DONTWAIT, MT_DATA);
   2094       1.1   jnemeth 		len = MHLEN;
   2095       1.1   jnemeth 	}
   2096       1.1   jnemeth 
   2097       1.1   jnemeth 	if (m == NULL) {
   2098       1.1   jnemeth 		error = ENOBUFS;
   2099       1.1   jnemeth 
   2100       1.1   jnemeth 		/* XXX for debug */
   2101       1.1   jnemeth 		aprint_error_dev(sc->sc_dev, "M_CLGET failed, size %d\n", len0);
   2102       1.1   jnemeth 		if (init) {
   2103       1.1   jnemeth 			return error;
   2104       1.1   jnemeth 		} else {
   2105       1.1   jnemeth 			goto back;
   2106       1.1   jnemeth 		}
   2107       1.1   jnemeth 	}
   2108       1.1   jnemeth 	m->m_len = m->m_pkthdr.len = len;
   2109       1.1   jnemeth 
   2110       1.1   jnemeth 	/*
   2111       1.1   jnemeth 	 * Try load RX mbuf into temporary DMA tag
   2112       1.1   jnemeth 	 */
   2113       1.1   jnemeth 	error = bus_dmamap_load_mbuf(sc->sc_dmat, sc->sc_mbuf_tmp_dmap, m,
   2114       1.1   jnemeth 				     init ? BUS_DMA_WAITOK : BUS_DMA_NOWAIT);
   2115       1.1   jnemeth 	if (error) {
   2116       1.1   jnemeth 		m_freem(m);
   2117       1.1   jnemeth 
   2118       1.1   jnemeth 		/* XXX for debug */
   2119       1.1   jnemeth 		aprint_error_dev(sc->sc_dev, "can't load RX mbuf\n");
   2120       1.1   jnemeth 		if (init) {
   2121       1.1   jnemeth 			return error;
   2122       1.1   jnemeth 		} else {
   2123       1.1   jnemeth 			goto back;
   2124       1.1   jnemeth 		}
   2125       1.1   jnemeth 	}
   2126       1.1   jnemeth 
   2127       1.1   jnemeth 	if (!init)
   2128       1.1   jnemeth 		bus_dmamap_unload(sc->sc_dmat, rb->rb_dmap);
   2129       1.1   jnemeth 	rb->rb_mbuf = m;
   2130       1.1   jnemeth 
   2131       1.1   jnemeth 	/*
   2132       1.1   jnemeth 	 * Swap RX buf's DMA map with the loaded temporary one
   2133       1.1   jnemeth 	 */
   2134       1.1   jnemeth 	dmap = rb->rb_dmap;
   2135       1.1   jnemeth 	rb->rb_dmap = sc->sc_mbuf_tmp_dmap;
   2136       1.1   jnemeth 	rb->rb_paddr = rb->rb_dmap->dm_segs[0].ds_addr;
   2137       1.1   jnemeth 	sc->sc_mbuf_tmp_dmap = dmap;
   2138       1.1   jnemeth 
   2139       1.1   jnemeth 	error = 0;
   2140       1.1   jnemeth back:
   2141       1.1   jnemeth 	rx_ring = rbd->rbd_ring;
   2142       1.1   jnemeth 	desc = &rx_ring->rr_desc[buf_idx];
   2143       1.1   jnemeth 
   2144       1.1   jnemeth 	desc->rd_addr_hi = ET_ADDR_HI(rb->rb_paddr);
   2145       1.1   jnemeth 	desc->rd_addr_lo = ET_ADDR_LO(rb->rb_paddr);
   2146       1.1   jnemeth 	desc->rd_ctrl = __SHIFTIN(buf_idx, ET_RDCTRL_BUFIDX);
   2147       1.1   jnemeth 
   2148       1.1   jnemeth 	bus_dmamap_sync(sc->sc_dmat, rx_ring->rr_dmap, 0,
   2149       1.1   jnemeth 	    rx_ring->rr_dmap->dm_mapsize, BUS_DMASYNC_PREWRITE);
   2150       1.1   jnemeth 	return error;
   2151       1.1   jnemeth }
   2152