Home | History | Annotate | Line # | Download | only in ic
aic6915.c revision 1.15
      1  1.15     perry /*	$NetBSD: aic6915.c,v 1.15 2005/12/24 20:27:29 perry Exp $	*/
      2   1.1   thorpej 
      3   1.1   thorpej /*-
      4   1.1   thorpej  * Copyright (c) 2001 The NetBSD Foundation, Inc.
      5   1.1   thorpej  * All rights reserved.
      6   1.1   thorpej  *
      7   1.1   thorpej  * This code is derived from software contributed to The NetBSD Foundation
      8   1.1   thorpej  * by Jason R. Thorpe.
      9  1.13     perry  *
     10   1.1   thorpej  * Redistribution and use in source and binary forms, with or without
     11   1.1   thorpej  * modification, are permitted provided that the following conditions
     12   1.1   thorpej  * are met:
     13   1.1   thorpej  * 1. Redistributions of source code must retain the above copyright
     14   1.1   thorpej  *    notice, this list of conditions and the following disclaimer.
     15   1.1   thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.1   thorpej  *    notice, this list of conditions and the following disclaimer in the
     17   1.1   thorpej  *    documentation and/or other materials provided with the distribution.
     18   1.1   thorpej  * 3. All advertising materials mentioning features or use of this software
     19   1.1   thorpej  *    must display the following acknowledgement:
     20   1.1   thorpej  *	This product includes software developed by the NetBSD
     21   1.1   thorpej  *	Foundation, Inc. and its contributors.
     22   1.1   thorpej  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23   1.1   thorpej  *    contributors may be used to endorse or promote products derived
     24   1.1   thorpej  *    from this software without specific prior written permission.
     25   1.1   thorpej  *
     26   1.1   thorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27   1.1   thorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28   1.1   thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29   1.1   thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30   1.1   thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31   1.1   thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32   1.1   thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33   1.1   thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34   1.1   thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35   1.1   thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36   1.1   thorpej  * POSSIBILITY OF SUCH DAMAGE.
     37   1.1   thorpej  */
     38   1.1   thorpej 
     39   1.1   thorpej /*
     40   1.1   thorpej  * Device driver for the Adaptec AIC-6915 (``Starfire'')
     41   1.1   thorpej  * 10/100 Ethernet controller.
     42   1.1   thorpej  */
     43   1.5     lukem 
     44   1.5     lukem #include <sys/cdefs.h>
     45  1.15     perry __KERNEL_RCSID(0, "$NetBSD: aic6915.c,v 1.15 2005/12/24 20:27:29 perry Exp $");
     46   1.1   thorpej 
     47   1.1   thorpej #include "bpfilter.h"
     48   1.1   thorpej 
     49   1.1   thorpej #include <sys/param.h>
     50   1.1   thorpej #include <sys/systm.h>
     51  1.13     perry #include <sys/callout.h>
     52   1.1   thorpej #include <sys/mbuf.h>
     53   1.1   thorpej #include <sys/malloc.h>
     54   1.1   thorpej #include <sys/kernel.h>
     55   1.1   thorpej #include <sys/socket.h>
     56   1.1   thorpej #include <sys/ioctl.h>
     57   1.1   thorpej #include <sys/errno.h>
     58   1.1   thorpej #include <sys/device.h>
     59   1.1   thorpej 
     60   1.1   thorpej #include <uvm/uvm_extern.h>
     61   1.1   thorpej 
     62  1.13     perry #include <net/if.h>
     63   1.1   thorpej #include <net/if_dl.h>
     64   1.1   thorpej #include <net/if_media.h>
     65   1.1   thorpej #include <net/if_ether.h>
     66   1.1   thorpej 
     67   1.1   thorpej #if NBPFILTER > 0
     68   1.1   thorpej #include <net/bpf.h>
     69   1.1   thorpej #endif
     70  1.13     perry 
     71   1.1   thorpej #include <machine/bus.h>
     72   1.1   thorpej #include <machine/intr.h>
     73   1.1   thorpej 
     74   1.1   thorpej #include <dev/mii/miivar.h>
     75   1.1   thorpej 
     76   1.1   thorpej #include <dev/ic/aic6915reg.h>
     77   1.1   thorpej #include <dev/ic/aic6915var.h>
     78   1.1   thorpej 
     79  1.11   thorpej static void	sf_start(struct ifnet *);
     80  1.11   thorpej static void	sf_watchdog(struct ifnet *);
     81  1.11   thorpej static int	sf_ioctl(struct ifnet *, u_long, caddr_t);
     82  1.11   thorpej static int	sf_init(struct ifnet *);
     83  1.11   thorpej static void	sf_stop(struct ifnet *, int);
     84  1.11   thorpej 
     85  1.11   thorpej static void	sf_shutdown(void *);
     86  1.11   thorpej 
     87  1.11   thorpej static void	sf_txintr(struct sf_softc *);
     88  1.11   thorpej static void	sf_rxintr(struct sf_softc *);
     89  1.11   thorpej static void	sf_stats_update(struct sf_softc *);
     90  1.11   thorpej 
     91  1.11   thorpej static void	sf_reset(struct sf_softc *);
     92  1.11   thorpej static void	sf_macreset(struct sf_softc *);
     93  1.11   thorpej static void	sf_rxdrain(struct sf_softc *);
     94  1.11   thorpej static int	sf_add_rxbuf(struct sf_softc *, int);
     95  1.11   thorpej static uint8_t	sf_read_eeprom(struct sf_softc *, int);
     96  1.11   thorpej static void	sf_set_filter(struct sf_softc *);
     97  1.11   thorpej 
     98  1.11   thorpej static int	sf_mii_read(struct device *, int, int);
     99  1.11   thorpej static void	sf_mii_write(struct device *, int, int, int);
    100  1.11   thorpej static void	sf_mii_statchg(struct device *);
    101   1.1   thorpej 
    102  1.11   thorpej static void	sf_tick(void *);
    103   1.1   thorpej 
    104  1.11   thorpej static int	sf_mediachange(struct ifnet *);
    105  1.11   thorpej static void	sf_mediastatus(struct ifnet *, struct ifmediareq *);
    106   1.1   thorpej 
    107   1.1   thorpej #define	sf_funcreg_read(sc, reg)					\
    108   1.1   thorpej 	bus_space_read_4((sc)->sc_st, (sc)->sc_sh_func, (reg))
    109   1.1   thorpej #define	sf_funcreg_write(sc, reg, val)					\
    110   1.1   thorpej 	bus_space_write_4((sc)->sc_st, (sc)->sc_sh_func, (reg), (val))
    111   1.1   thorpej 
    112  1.15     perry static inline uint32_t
    113   1.1   thorpej sf_reg_read(struct sf_softc *sc, bus_addr_t reg)
    114   1.1   thorpej {
    115   1.1   thorpej 
    116   1.1   thorpej 	if (__predict_false(sc->sc_iomapped)) {
    117   1.1   thorpej 		bus_space_write_4(sc->sc_st, sc->sc_sh, SF_IndirectIoAccess,
    118   1.1   thorpej 		    reg);
    119   1.1   thorpej 		return (bus_space_read_4(sc->sc_st, sc->sc_sh,
    120   1.1   thorpej 		    SF_IndirectIoDataPort));
    121   1.1   thorpej 	}
    122   1.1   thorpej 
    123   1.1   thorpej 	return (bus_space_read_4(sc->sc_st, sc->sc_sh, reg));
    124   1.1   thorpej }
    125   1.1   thorpej 
    126  1.15     perry static inline void
    127   1.1   thorpej sf_reg_write(struct sf_softc *sc, bus_addr_t reg, uint32_t val)
    128   1.1   thorpej {
    129   1.1   thorpej 
    130   1.1   thorpej 	if (__predict_false(sc->sc_iomapped)) {
    131   1.1   thorpej 		bus_space_write_4(sc->sc_st, sc->sc_sh, SF_IndirectIoAccess,
    132   1.1   thorpej 		    reg);
    133   1.1   thorpej 		bus_space_write_4(sc->sc_st, sc->sc_sh, SF_IndirectIoDataPort,
    134   1.1   thorpej 		    val);
    135   1.1   thorpej 		return;
    136   1.1   thorpej 	}
    137   1.1   thorpej 
    138   1.1   thorpej 	bus_space_write_4(sc->sc_st, sc->sc_sh, reg, val);
    139   1.1   thorpej }
    140   1.1   thorpej 
    141   1.1   thorpej #define	sf_genreg_read(sc, reg)						\
    142   1.1   thorpej 	sf_reg_read((sc), (reg) + SF_GENREG_OFFSET)
    143   1.1   thorpej #define	sf_genreg_write(sc, reg, val)					\
    144   1.1   thorpej 	sf_reg_write((sc), (reg) + SF_GENREG_OFFSET, (val))
    145   1.1   thorpej 
    146   1.1   thorpej /*
    147   1.1   thorpej  * sf_attach:
    148   1.1   thorpej  *
    149   1.1   thorpej  *	Attach a Starfire interface to the system.
    150   1.1   thorpej  */
    151   1.1   thorpej void
    152   1.1   thorpej sf_attach(struct sf_softc *sc)
    153   1.1   thorpej {
    154   1.1   thorpej 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    155   1.1   thorpej 	int i, rseg, error;
    156   1.1   thorpej 	bus_dma_segment_t seg;
    157   1.1   thorpej 	u_int8_t enaddr[ETHER_ADDR_LEN];
    158   1.1   thorpej 
    159   1.1   thorpej 	callout_init(&sc->sc_tick_callout);
    160   1.1   thorpej 
    161   1.1   thorpej 	/*
    162   1.1   thorpej 	 * If we're I/O mapped, the functional register handle is
    163   1.1   thorpej 	 * the same as the base handle.  If we're memory mapped,
    164   1.1   thorpej 	 * carve off a chunk of the register space for the functional
    165   1.1   thorpej 	 * registers, to save on arithmetic later.
    166   1.1   thorpej 	 */
    167   1.1   thorpej 	if (sc->sc_iomapped)
    168   1.1   thorpej 		sc->sc_sh_func = sc->sc_sh;
    169   1.1   thorpej 	else {
    170   1.1   thorpej 		if ((error = bus_space_subregion(sc->sc_st, sc->sc_sh,
    171   1.1   thorpej 		    SF_GENREG_OFFSET, SF_FUNCREG_SIZE, &sc->sc_sh_func)) != 0) {
    172   1.1   thorpej 			printf("%s: unable to sub-region functional "
    173   1.1   thorpej 			    "registers, error = %d\n", sc->sc_dev.dv_xname,
    174   1.1   thorpej 			    error);
    175   1.1   thorpej 			return;
    176   1.1   thorpej 		}
    177   1.1   thorpej 	}
    178   1.1   thorpej 
    179   1.1   thorpej 	/*
    180   1.1   thorpej 	 * Initialize the transmit threshold for this interface.  The
    181   1.1   thorpej 	 * manual describes the default as 4 * 16 bytes.  We start out
    182   1.1   thorpej 	 * at 10 * 16 bytes, to avoid a bunch of initial underruns on
    183   1.1   thorpej 	 * several platforms.
    184   1.1   thorpej 	 */
    185   1.1   thorpej 	sc->sc_txthresh = 10;
    186   1.1   thorpej 
    187   1.1   thorpej 	/*
    188   1.1   thorpej 	 * Allocate the control data structures, and create and load the
    189   1.1   thorpej 	 * DMA map for it.
    190   1.1   thorpej 	 */
    191   1.1   thorpej 	if ((error = bus_dmamem_alloc(sc->sc_dmat,
    192   1.1   thorpej 	    sizeof(struct sf_control_data), PAGE_SIZE, 0, &seg, 1, &rseg,
    193   1.1   thorpej 	    BUS_DMA_NOWAIT)) != 0) {
    194   1.1   thorpej 		printf("%s: unable to allocate control data, error = %d\n",
    195   1.1   thorpej 		    sc->sc_dev.dv_xname, error);
    196   1.1   thorpej 		goto fail_0;
    197   1.1   thorpej 	}
    198   1.1   thorpej 
    199   1.1   thorpej 	if ((error = bus_dmamem_map(sc->sc_dmat, &seg, rseg,
    200   1.1   thorpej 	    sizeof(struct sf_control_data), (caddr_t *)&sc->sc_control_data,
    201   1.1   thorpej 	    BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) != 0) {
    202   1.1   thorpej 		printf("%s: unable to map control data, error = %d\n",
    203   1.1   thorpej 		    sc->sc_dev.dv_xname, error);
    204   1.1   thorpej 		goto fail_1;
    205   1.1   thorpej 	}
    206   1.1   thorpej 
    207   1.1   thorpej 	if ((error = bus_dmamap_create(sc->sc_dmat,
    208   1.1   thorpej 	    sizeof(struct sf_control_data), 1,
    209   1.1   thorpej 	    sizeof(struct sf_control_data), 0, BUS_DMA_NOWAIT,
    210   1.1   thorpej 	    &sc->sc_cddmamap)) != 0) {
    211   1.1   thorpej 		printf("%s: unable to create control data DMA map, "
    212   1.1   thorpej 		    "error = %d\n", sc->sc_dev.dv_xname, error);
    213   1.1   thorpej 		goto fail_2;
    214   1.1   thorpej 	}
    215   1.1   thorpej 
    216   1.1   thorpej 	if ((error = bus_dmamap_load(sc->sc_dmat, sc->sc_cddmamap,
    217   1.1   thorpej 	    sc->sc_control_data, sizeof(struct sf_control_data), NULL,
    218   1.1   thorpej 	    BUS_DMA_NOWAIT)) != 0) {
    219   1.1   thorpej 		printf("%s: unable to load control data DMA map, error = %d\n",
    220   1.1   thorpej 		    sc->sc_dev.dv_xname, error);
    221   1.1   thorpej 		goto fail_3;
    222   1.1   thorpej 	}
    223   1.1   thorpej 
    224   1.1   thorpej 	/*
    225   1.1   thorpej 	 * Create the transmit buffer DMA maps.
    226   1.1   thorpej 	 */
    227   1.1   thorpej 	for (i = 0; i < SF_NTXDESC; i++) {
    228   1.1   thorpej 		if ((error = bus_dmamap_create(sc->sc_dmat, MCLBYTES,
    229   1.1   thorpej 		    SF_NTXFRAGS, MCLBYTES, 0, BUS_DMA_NOWAIT,
    230   1.1   thorpej 		    &sc->sc_txsoft[i].ds_dmamap)) != 0) {
    231   1.1   thorpej 			printf("%s: unable to create tx DMA map %d, "
    232   1.1   thorpej 			    "error = %d\n", sc->sc_dev.dv_xname, i, error);
    233   1.1   thorpej 			goto fail_4;
    234   1.1   thorpej 		}
    235   1.1   thorpej 	}
    236   1.1   thorpej 
    237   1.1   thorpej 	/*
    238   1.1   thorpej 	 * Create the receive buffer DMA maps.
    239   1.1   thorpej 	 */
    240   1.1   thorpej 	for (i = 0; i < SF_NRXDESC; i++) {
    241   1.1   thorpej 		if ((error = bus_dmamap_create(sc->sc_dmat, MCLBYTES, 1,
    242   1.1   thorpej 		    MCLBYTES, 0, BUS_DMA_NOWAIT,
    243   1.1   thorpej 		    &sc->sc_rxsoft[i].ds_dmamap)) != 0) {
    244   1.1   thorpej 			printf("%s: unable to create rx DMA map %d, "
    245   1.1   thorpej 			    "error = %d\n", sc->sc_dev.dv_xname, i, error);
    246   1.1   thorpej 			goto fail_5;
    247   1.1   thorpej 		}
    248   1.1   thorpej 	}
    249   1.1   thorpej 
    250   1.1   thorpej 	/*
    251   1.1   thorpej 	 * Reset the chip to a known state.
    252   1.1   thorpej 	 */
    253   1.1   thorpej 	sf_reset(sc);
    254   1.1   thorpej 
    255   1.1   thorpej 	/*
    256   1.1   thorpej 	 * Read the Ethernet address from the EEPROM.
    257   1.1   thorpej 	 */
    258   1.1   thorpej 	for (i = 0; i < ETHER_ADDR_LEN; i++)
    259   1.1   thorpej 		enaddr[i] = sf_read_eeprom(sc, (15 + (ETHER_ADDR_LEN - 1)) - i);
    260   1.1   thorpej 
    261   1.1   thorpej 	printf("%s: Ethernet address %s\n", sc->sc_dev.dv_xname,
    262   1.1   thorpej 	    ether_sprintf(enaddr));
    263   1.1   thorpej 
    264   1.1   thorpej 	if (sf_funcreg_read(sc, SF_PciDeviceConfig) & PDC_System64)
    265   1.1   thorpej 		printf("%s: 64-bit PCI slot detected\n", sc->sc_dev.dv_xname);
    266   1.1   thorpej 
    267   1.1   thorpej 	/*
    268   1.1   thorpej 	 * Initialize our media structures and probe the MII.
    269   1.1   thorpej 	 */
    270   1.1   thorpej 	sc->sc_mii.mii_ifp = ifp;
    271   1.1   thorpej 	sc->sc_mii.mii_readreg = sf_mii_read;
    272   1.1   thorpej 	sc->sc_mii.mii_writereg = sf_mii_write;
    273   1.1   thorpej 	sc->sc_mii.mii_statchg = sf_mii_statchg;
    274   1.7      fair 	ifmedia_init(&sc->sc_mii.mii_media, IFM_IMASK, sf_mediachange,
    275   1.1   thorpej 	    sf_mediastatus);
    276   1.1   thorpej 	mii_attach(&sc->sc_dev, &sc->sc_mii, 0xffffffff, MII_PHY_ANY,
    277   1.1   thorpej 	    MII_OFFSET_ANY, 0);
    278   1.1   thorpej 	if (LIST_FIRST(&sc->sc_mii.mii_phys) == NULL) {
    279   1.1   thorpej 		ifmedia_add(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE, 0, NULL);
    280   1.1   thorpej 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE);
    281   1.1   thorpej 	} else
    282   1.1   thorpej 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_AUTO);
    283   1.1   thorpej 
    284   1.1   thorpej 	strcpy(ifp->if_xname, sc->sc_dev.dv_xname);
    285   1.1   thorpej 	ifp->if_softc = sc;
    286   1.1   thorpej 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
    287   1.1   thorpej 	ifp->if_ioctl = sf_ioctl;
    288   1.1   thorpej 	ifp->if_start = sf_start;
    289   1.1   thorpej 	ifp->if_watchdog = sf_watchdog;
    290   1.1   thorpej 	ifp->if_init = sf_init;
    291   1.1   thorpej 	ifp->if_stop = sf_stop;
    292   1.1   thorpej 	IFQ_SET_READY(&ifp->if_snd);
    293   1.1   thorpej 
    294   1.1   thorpej 	/*
    295   1.1   thorpej 	 * Attach the interface.
    296   1.1   thorpej 	 */
    297   1.1   thorpej 	if_attach(ifp);
    298   1.1   thorpej 	ether_ifattach(ifp, enaddr);
    299   1.1   thorpej 
    300   1.1   thorpej 	/*
    301   1.1   thorpej 	 * Make sure the interface is shutdown during reboot.
    302   1.1   thorpej 	 */
    303   1.1   thorpej 	sc->sc_sdhook = shutdownhook_establish(sf_shutdown, sc);
    304   1.1   thorpej 	if (sc->sc_sdhook == NULL)
    305   1.1   thorpej 		printf("%s: WARNING: unable to establish shutdown hook\n",
    306   1.1   thorpej 		    sc->sc_dev.dv_xname);
    307   1.1   thorpej 	return;
    308   1.1   thorpej 
    309   1.1   thorpej 	/*
    310   1.1   thorpej 	 * Free any resources we've allocated during the failed attach
    311   1.1   thorpej 	 * attempt.  Do this in reverse order an fall through.
    312   1.1   thorpej 	 */
    313   1.1   thorpej  fail_5:
    314   1.1   thorpej 	for (i = 0; i < SF_NRXDESC; i++) {
    315   1.1   thorpej 		if (sc->sc_rxsoft[i].ds_dmamap != NULL)
    316   1.1   thorpej 			bus_dmamap_destroy(sc->sc_dmat,
    317   1.1   thorpej 			    sc->sc_rxsoft[i].ds_dmamap);
    318   1.1   thorpej 	}
    319   1.1   thorpej  fail_4:
    320   1.1   thorpej 	for (i = 0; i < SF_NTXDESC; i++) {
    321   1.1   thorpej 		if (sc->sc_txsoft[i].ds_dmamap != NULL)
    322   1.1   thorpej 			bus_dmamap_destroy(sc->sc_dmat,
    323   1.1   thorpej 			    sc->sc_txsoft[i].ds_dmamap);
    324   1.1   thorpej 	}
    325   1.1   thorpej 	bus_dmamap_unload(sc->sc_dmat, sc->sc_cddmamap);
    326   1.1   thorpej  fail_3:
    327   1.1   thorpej 	bus_dmamap_destroy(sc->sc_dmat, sc->sc_cddmamap);
    328   1.1   thorpej  fail_2:
    329   1.1   thorpej 	bus_dmamem_unmap(sc->sc_dmat, (caddr_t) sc->sc_control_data,
    330   1.1   thorpej 	    sizeof(struct sf_control_data));
    331   1.1   thorpej  fail_1:
    332   1.1   thorpej 	bus_dmamem_free(sc->sc_dmat, &seg, rseg);
    333   1.1   thorpej  fail_0:
    334   1.1   thorpej 	return;
    335   1.1   thorpej }
    336   1.1   thorpej 
    337   1.1   thorpej /*
    338   1.1   thorpej  * sf_shutdown:
    339   1.1   thorpej  *
    340   1.1   thorpej  *	Shutdown hook -- make sure the interface is stopped at reboot.
    341   1.1   thorpej  */
    342  1.11   thorpej static void
    343   1.1   thorpej sf_shutdown(void *arg)
    344   1.1   thorpej {
    345   1.1   thorpej 	struct sf_softc *sc = arg;
    346   1.1   thorpej 
    347   1.1   thorpej 	sf_stop(&sc->sc_ethercom.ec_if, 1);
    348   1.1   thorpej }
    349   1.1   thorpej 
    350   1.1   thorpej /*
    351   1.1   thorpej  * sf_start:		[ifnet interface function]
    352   1.1   thorpej  *
    353   1.1   thorpej  *	Start packet transmission on the interface.
    354   1.1   thorpej  */
    355  1.11   thorpej static void
    356   1.1   thorpej sf_start(struct ifnet *ifp)
    357   1.1   thorpej {
    358   1.1   thorpej 	struct sf_softc *sc = ifp->if_softc;
    359   1.1   thorpej 	struct mbuf *m0, *m;
    360   1.1   thorpej 	struct sf_txdesc0 *txd;
    361   1.1   thorpej 	struct sf_descsoft *ds;
    362   1.1   thorpej 	bus_dmamap_t dmamap;
    363  1.10  christos 	int error, producer, last = -1, opending, seg;
    364   1.1   thorpej 
    365   1.1   thorpej 	/*
    366   1.1   thorpej 	 * Remember the previous number of pending transmits.
    367   1.1   thorpej 	 */
    368   1.1   thorpej 	opending = sc->sc_txpending;
    369   1.1   thorpej 
    370   1.1   thorpej 	/*
    371   1.1   thorpej 	 * Find out where we're sitting.
    372   1.1   thorpej 	 */
    373   1.1   thorpej 	producer = SF_TXDINDEX_TO_HOST(
    374   1.1   thorpej 	    TDQPI_HiPrTxProducerIndex_get(
    375   1.1   thorpej 	    sf_funcreg_read(sc, SF_TxDescQueueProducerIndex)));
    376   1.1   thorpej 
    377   1.1   thorpej 	/*
    378   1.1   thorpej 	 * Loop through the send queue, setting up transmit descriptors
    379   1.1   thorpej 	 * until we drain the queue, or use up all available transmit
    380   1.1   thorpej 	 * descriptors.  Leave a blank one at the end for sanity's sake.
    381   1.1   thorpej 	 */
    382   1.1   thorpej 	while (sc->sc_txpending < (SF_NTXDESC - 1)) {
    383   1.1   thorpej 		/*
    384   1.1   thorpej 		 * Grab a packet off the queue.
    385   1.1   thorpej 		 */
    386   1.1   thorpej 		IFQ_POLL(&ifp->if_snd, m0);
    387   1.1   thorpej 		if (m0 == NULL)
    388   1.1   thorpej 			break;
    389   1.1   thorpej 		m = NULL;
    390   1.1   thorpej 
    391   1.1   thorpej 		/*
    392   1.1   thorpej 		 * Get the transmit descriptor.
    393   1.1   thorpej 		 */
    394   1.1   thorpej 		txd = &sc->sc_txdescs[producer];
    395   1.1   thorpej 		ds = &sc->sc_txsoft[producer];
    396   1.1   thorpej 		dmamap = ds->ds_dmamap;
    397   1.1   thorpej 
    398   1.1   thorpej 		/*
    399   1.1   thorpej 		 * Load the DMA map.  If this fails, the packet either
    400   1.1   thorpej 		 * didn't fit in the allotted number of frags, or we were
    401   1.1   thorpej 		 * short on resources.  In this case, we'll copy and try
    402   1.1   thorpej 		 * again.
    403   1.1   thorpej 		 */
    404   1.1   thorpej 		if (bus_dmamap_load_mbuf(sc->sc_dmat, dmamap, m0,
    405   1.3   thorpej 		    BUS_DMA_WRITE|BUS_DMA_NOWAIT) != 0) {
    406   1.1   thorpej 			MGETHDR(m, M_DONTWAIT, MT_DATA);
    407   1.1   thorpej 			if (m == NULL) {
    408   1.1   thorpej 				printf("%s: unable to allocate Tx mbuf\n",
    409   1.1   thorpej 				    sc->sc_dev.dv_xname);
    410   1.1   thorpej 				break;
    411   1.1   thorpej 			}
    412   1.1   thorpej 			if (m0->m_pkthdr.len > MHLEN) {
    413   1.1   thorpej 				MCLGET(m, M_DONTWAIT);
    414   1.1   thorpej 				if ((m->m_flags & M_EXT) == 0) {
    415   1.1   thorpej 					printf("%s: unable to allocate Tx "
    416   1.1   thorpej 					    "cluster\n", sc->sc_dev.dv_xname);
    417   1.1   thorpej 					m_freem(m);
    418   1.1   thorpej 					break;
    419   1.1   thorpej 				}
    420   1.1   thorpej 			}
    421   1.1   thorpej 			m_copydata(m0, 0, m0->m_pkthdr.len, mtod(m, caddr_t));
    422   1.1   thorpej 			m->m_pkthdr.len = m->m_len = m0->m_pkthdr.len;
    423   1.1   thorpej 			error = bus_dmamap_load_mbuf(sc->sc_dmat, dmamap,
    424   1.3   thorpej 			    m, BUS_DMA_WRITE|BUS_DMA_NOWAIT);
    425   1.1   thorpej 			if (error) {
    426   1.1   thorpej 				printf("%s: unable to load Tx buffer, "
    427   1.1   thorpej 				    "error = %d\n", sc->sc_dev.dv_xname, error);
    428   1.1   thorpej 				break;
    429   1.1   thorpej 			}
    430   1.1   thorpej 		}
    431   1.1   thorpej 
    432   1.1   thorpej 		/*
    433   1.1   thorpej 		 * WE ARE NOW COMMITTED TO TRANSMITTING THE PACKET.
    434   1.1   thorpej 		 */
    435   1.1   thorpej 		IFQ_DEQUEUE(&ifp->if_snd, m0);
    436   1.1   thorpej 		if (m != NULL) {
    437   1.1   thorpej 			m_freem(m0);
    438   1.1   thorpej 			m0 = m;
    439   1.1   thorpej 		}
    440   1.1   thorpej 
    441   1.1   thorpej 		/* Initialize the descriptor. */
    442   1.1   thorpej 		txd->td_word0 =
    443   1.1   thorpej 		    htole32(TD_W0_ID | TD_W0_CRCEN | m0->m_pkthdr.len);
    444   1.1   thorpej 		if (producer == (SF_NTXDESC - 1))
    445   1.1   thorpej 			txd->td_word0 |= TD_W0_END;
    446   1.1   thorpej 		txd->td_word1 = htole32(dmamap->dm_nsegs);
    447   1.1   thorpej 		for (seg = 0; seg < dmamap->dm_nsegs; seg++) {
    448   1.1   thorpej 			txd->td_frags[seg].fr_addr =
    449   1.1   thorpej 			    htole32(dmamap->dm_segs[seg].ds_addr);
    450   1.1   thorpej 			txd->td_frags[seg].fr_len =
    451   1.1   thorpej 			    htole32(dmamap->dm_segs[seg].ds_len);
    452   1.1   thorpej 		}
    453   1.1   thorpej 
    454   1.1   thorpej 		/* Sync the descriptor and the DMA map. */
    455   1.1   thorpej 		SF_CDTXDSYNC(sc, producer, BUS_DMASYNC_PREWRITE);
    456   1.1   thorpej 		bus_dmamap_sync(sc->sc_dmat, dmamap, 0, dmamap->dm_mapsize,
    457   1.1   thorpej 		    BUS_DMASYNC_PREWRITE);
    458   1.1   thorpej 
    459   1.1   thorpej 		/*
    460   1.1   thorpej 		 * Store a pointer to the packet so we can free it later.
    461   1.1   thorpej 		 */
    462   1.1   thorpej 		ds->ds_mbuf = m0;
    463   1.1   thorpej 
    464   1.1   thorpej 		/* Advance the Tx pointer. */
    465   1.1   thorpej 		sc->sc_txpending++;
    466   1.1   thorpej 		last = producer;
    467   1.1   thorpej 		producer = SF_NEXTTX(producer);
    468   1.1   thorpej 
    469   1.1   thorpej #if NBPFILTER > 0
    470   1.1   thorpej 		/*
    471   1.1   thorpej 		 * Pass the packet to any BPF listeners.
    472   1.1   thorpej 		 */
    473   1.1   thorpej 		if (ifp->if_bpf)
    474   1.1   thorpej 			bpf_mtap(ifp->if_bpf, m0);
    475   1.1   thorpej #endif
    476   1.1   thorpej 	}
    477   1.1   thorpej 
    478   1.1   thorpej 	if (sc->sc_txpending == (SF_NTXDESC - 1)) {
    479   1.1   thorpej 		/* No more slots left; notify upper layer. */
    480   1.1   thorpej 		ifp->if_flags |= IFF_OACTIVE;
    481   1.1   thorpej 	}
    482   1.1   thorpej 
    483   1.1   thorpej 	if (sc->sc_txpending != opending) {
    484  1.10  christos 		KASSERT(last != -1);
    485   1.1   thorpej 		/*
    486   1.1   thorpej 		 * We enqueued packets.  Cause a transmit interrupt to
    487   1.1   thorpej 		 * happen on the last packet we enqueued, and give the
    488   1.1   thorpej 		 * new descriptors to the chip by writing the new
    489   1.1   thorpej 		 * producer index.
    490   1.1   thorpej 		 */
    491   1.1   thorpej 		sc->sc_txdescs[last].td_word0 |= TD_W0_INTR;
    492   1.1   thorpej 		SF_CDTXDSYNC(sc, last, BUS_DMASYNC_PREWRITE);
    493   1.1   thorpej 
    494   1.1   thorpej 		sf_funcreg_write(sc, SF_TxDescQueueProducerIndex,
    495   1.1   thorpej 		    TDQPI_HiPrTxProducerIndex(SF_TXDINDEX_TO_CHIP(producer)));
    496   1.1   thorpej 
    497   1.1   thorpej 		/* Set a watchdog timer in case the chip flakes out. */
    498   1.1   thorpej 		ifp->if_timer = 5;
    499   1.1   thorpej 	}
    500   1.1   thorpej }
    501   1.1   thorpej 
    502   1.1   thorpej /*
    503   1.1   thorpej  * sf_watchdog:		[ifnet interface function]
    504   1.1   thorpej  *
    505   1.1   thorpej  *	Watchdog timer handler.
    506   1.1   thorpej  */
    507  1.11   thorpej static void
    508   1.1   thorpej sf_watchdog(struct ifnet *ifp)
    509   1.1   thorpej {
    510   1.1   thorpej 	struct sf_softc *sc = ifp->if_softc;
    511   1.1   thorpej 
    512   1.1   thorpej 	printf("%s: device timeout\n", sc->sc_dev.dv_xname);
    513   1.1   thorpej 	ifp->if_oerrors++;
    514   1.1   thorpej 
    515   1.1   thorpej 	(void) sf_init(ifp);
    516   1.1   thorpej 
    517   1.1   thorpej 	/* Try to get more packets going. */
    518   1.1   thorpej 	sf_start(ifp);
    519   1.1   thorpej }
    520   1.1   thorpej 
    521   1.1   thorpej /*
    522   1.1   thorpej  * sf_ioctl:		[ifnet interface function]
    523   1.1   thorpej  *
    524   1.1   thorpej  *	Handle control requests from the operator.
    525   1.1   thorpej  */
    526  1.11   thorpej static int
    527   1.1   thorpej sf_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
    528   1.1   thorpej {
    529   1.1   thorpej 	struct sf_softc *sc = ifp->if_softc;
    530   1.1   thorpej 	struct ifreq *ifr = (struct ifreq *) data;
    531   1.1   thorpej 	int s, error;
    532   1.1   thorpej 
    533   1.1   thorpej 	s = splnet();
    534   1.1   thorpej 
    535   1.1   thorpej 	switch (cmd) {
    536   1.1   thorpej 	case SIOCSIFMEDIA:
    537   1.1   thorpej 	case SIOCGIFMEDIA:
    538   1.1   thorpej 		error = ifmedia_ioctl(ifp, ifr, &sc->sc_mii.mii_media, cmd);
    539   1.1   thorpej 		break;
    540   1.1   thorpej 
    541   1.1   thorpej 	default:
    542   1.1   thorpej 		error = ether_ioctl(ifp, cmd, data);
    543   1.1   thorpej 		if (error == ENETRESET) {
    544   1.1   thorpej 			/*
    545   1.1   thorpej 			 * Multicast list has changed; set the hardware filter
    546   1.1   thorpej 			 * accordingly.
    547   1.1   thorpej 			 */
    548  1.12   thorpej 			if (ifp->if_flags & IFF_RUNNING)
    549  1.12   thorpej 				sf_set_filter(sc);
    550   1.1   thorpej 			error = 0;
    551   1.1   thorpej 		}
    552   1.1   thorpej 		break;
    553   1.1   thorpej 	}
    554   1.1   thorpej 
    555   1.1   thorpej 	/* Try to get more packets going. */
    556   1.1   thorpej 	sf_start(ifp);
    557   1.1   thorpej 
    558   1.1   thorpej 	splx(s);
    559   1.1   thorpej 	return (error);
    560   1.1   thorpej }
    561   1.1   thorpej 
    562   1.1   thorpej /*
    563   1.1   thorpej  * sf_intr:
    564   1.1   thorpej  *
    565   1.1   thorpej  *	Interrupt service routine.
    566   1.1   thorpej  */
    567   1.1   thorpej int
    568   1.1   thorpej sf_intr(void *arg)
    569   1.1   thorpej {
    570   1.1   thorpej 	struct sf_softc *sc = arg;
    571   1.1   thorpej 	uint32_t isr;
    572   1.1   thorpej 	int handled = 0, wantinit = 0;
    573   1.1   thorpej 
    574   1.1   thorpej 	for (;;) {
    575   1.1   thorpej 		/* Reading clears all interrupts we're interested in. */
    576   1.1   thorpej 		isr = sf_funcreg_read(sc, SF_InterruptStatus);
    577   1.1   thorpej 		if ((isr & IS_PCIPadInt) == 0)
    578   1.1   thorpej 			break;
    579   1.1   thorpej 
    580   1.1   thorpej 		handled = 1;
    581   1.1   thorpej 
    582   1.1   thorpej 		/* Handle receive interrupts. */
    583   1.1   thorpej 		if (isr & IS_RxQ1DoneInt)
    584   1.1   thorpej 			sf_rxintr(sc);
    585   1.1   thorpej 
    586   1.1   thorpej 		/* Handle transmit completion interrupts. */
    587   1.1   thorpej 		if (isr & (IS_TxDmaDoneInt|IS_TxQueueDoneInt))
    588   1.1   thorpej 			sf_txintr(sc);
    589   1.1   thorpej 
    590   1.1   thorpej 		/* Handle abnormal interrupts. */
    591   1.1   thorpej 		if (isr & IS_AbnormalInterrupt) {
    592   1.1   thorpej 			/* Statistics. */
    593   1.1   thorpej 			if (isr & IS_StatisticWrapInt)
    594   1.1   thorpej 				sf_stats_update(sc);
    595   1.1   thorpej 
    596   1.1   thorpej 			/* DMA errors. */
    597   1.1   thorpej 			if (isr & IS_DmaErrInt) {
    598   1.1   thorpej 				wantinit = 1;
    599   1.1   thorpej 				printf("%s: WARNING: DMA error\n",
    600   1.1   thorpej 				    sc->sc_dev.dv_xname);
    601   1.1   thorpej 			}
    602   1.1   thorpej 
    603   1.1   thorpej 			/* Transmit FIFO underruns. */
    604   1.1   thorpej 			if (isr & IS_TxDataLowInt) {
    605   1.1   thorpej 				if (sc->sc_txthresh < 0xff)
    606   1.1   thorpej 					sc->sc_txthresh++;
    607   1.1   thorpej 				printf("%s: transmit FIFO underrun, new "
    608   1.1   thorpej 				    "threshold: %d bytes\n",
    609   1.1   thorpej 				    sc->sc_dev.dv_xname,
    610   1.1   thorpej 				    sc->sc_txthresh * 16);
    611   1.1   thorpej 				sf_funcreg_write(sc, SF_TransmitFrameCSR,
    612   1.1   thorpej 				    sc->sc_TransmitFrameCSR |
    613   1.1   thorpej 				    TFCSR_TransmitThreshold(sc->sc_txthresh));
    614   1.1   thorpej 				sf_funcreg_write(sc, SF_TxDescQueueCtrl,
    615   1.1   thorpej 				    sc->sc_TxDescQueueCtrl |
    616   1.1   thorpej 				    TDQC_TxHighPriorityFifoThreshold(
    617   1.1   thorpej 							sc->sc_txthresh));
    618   1.1   thorpej 			}
    619   1.1   thorpej 		}
    620   1.1   thorpej 	}
    621   1.1   thorpej 
    622   1.1   thorpej 	if (handled) {
    623   1.1   thorpej 		/* Reset the interface, if necessary. */
    624   1.1   thorpej 		if (wantinit)
    625   1.1   thorpej 			sf_init(&sc->sc_ethercom.ec_if);
    626   1.1   thorpej 
    627   1.1   thorpej 		/* Try and get more packets going. */
    628   1.1   thorpej 		sf_start(&sc->sc_ethercom.ec_if);
    629   1.1   thorpej 	}
    630   1.1   thorpej 
    631   1.1   thorpej 	return (handled);
    632   1.1   thorpej }
    633   1.1   thorpej 
    634   1.1   thorpej /*
    635   1.1   thorpej  * sf_txintr:
    636   1.1   thorpej  *
    637   1.1   thorpej  *	Helper -- handle transmit completion interrupts.
    638   1.1   thorpej  */
    639  1.11   thorpej static void
    640   1.1   thorpej sf_txintr(struct sf_softc *sc)
    641   1.1   thorpej {
    642   1.1   thorpej 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    643   1.1   thorpej 	struct sf_descsoft *ds;
    644   1.1   thorpej 	uint32_t cqci, tcd;
    645   1.1   thorpej 	int consumer, producer, txidx;
    646   1.1   thorpej 
    647   1.1   thorpej  try_again:
    648   1.1   thorpej 	cqci = sf_funcreg_read(sc, SF_CompletionQueueConsumerIndex);
    649   1.1   thorpej 
    650   1.1   thorpej 	consumer = CQCI_TxCompletionConsumerIndex_get(cqci);
    651   1.1   thorpej 	producer = CQPI_TxCompletionProducerIndex_get(
    652   1.1   thorpej 	    sf_funcreg_read(sc, SF_CompletionQueueProducerIndex));
    653   1.1   thorpej 
    654   1.1   thorpej 	if (consumer == producer)
    655   1.1   thorpej 		return;
    656   1.1   thorpej 
    657   1.1   thorpej 	ifp->if_flags &= ~IFF_OACTIVE;
    658   1.1   thorpej 
    659   1.1   thorpej 	while (consumer != producer) {
    660   1.1   thorpej 		SF_CDTXCSYNC(sc, consumer, BUS_DMASYNC_POSTREAD);
    661   1.1   thorpej 		tcd = le32toh(sc->sc_txcomp[consumer].tcd_word0);
    662   1.1   thorpej 
    663   1.1   thorpej 		txidx = SF_TCD_INDEX_TO_HOST(TCD_INDEX(tcd));
    664   1.1   thorpej #ifdef DIAGNOSTIC
    665   1.1   thorpej 		if ((tcd & TCD_PR) == 0)
    666   1.1   thorpej 			printf("%s: Tx queue mismatch, index %d\n",
    667   1.1   thorpej 			    sc->sc_dev.dv_xname, txidx);
    668   1.1   thorpej #endif
    669   1.1   thorpej 		/*
    670   1.1   thorpej 		 * NOTE: stats are updated later.  We're just
    671   1.1   thorpej 		 * releasing packets that have been DMA'd to
    672   1.1   thorpej 		 * the chip.
    673   1.1   thorpej 		 */
    674   1.1   thorpej 		ds = &sc->sc_txsoft[txidx];
    675   1.1   thorpej 		SF_CDTXDSYNC(sc, txidx, BUS_DMASYNC_POSTWRITE);
    676   1.1   thorpej 		bus_dmamap_sync(sc->sc_dmat, ds->ds_dmamap,
    677   1.1   thorpej 		    0, ds->ds_dmamap->dm_mapsize,
    678   1.1   thorpej 		    BUS_DMASYNC_POSTWRITE);
    679   1.1   thorpej 		m_freem(ds->ds_mbuf);
    680   1.1   thorpej 		ds->ds_mbuf = NULL;
    681   1.1   thorpej 
    682   1.1   thorpej 		consumer = SF_NEXTTCD(consumer);
    683   1.1   thorpej 		sc->sc_txpending--;
    684   1.1   thorpej 	}
    685   1.1   thorpej 
    686   1.1   thorpej 	/* XXXJRT -- should be KDASSERT() */
    687   1.1   thorpej 	KASSERT(sc->sc_txpending >= 0);
    688   1.1   thorpej 
    689   1.1   thorpej 	/* If all packets are done, cancel the watchdog timer. */
    690   1.1   thorpej 	if (sc->sc_txpending == 0)
    691   1.1   thorpej 		ifp->if_timer = 0;
    692   1.1   thorpej 
    693   1.1   thorpej 	/* Update the consumer index. */
    694   1.1   thorpej 	sf_funcreg_write(sc, SF_CompletionQueueConsumerIndex,
    695   1.1   thorpej 	    (cqci & ~CQCI_TxCompletionConsumerIndex(0x7ff)) |
    696   1.1   thorpej 	     CQCI_TxCompletionConsumerIndex(consumer));
    697   1.1   thorpej 
    698   1.1   thorpej 	/* Double check for new completions. */
    699   1.1   thorpej 	goto try_again;
    700   1.1   thorpej }
    701   1.1   thorpej 
    702   1.1   thorpej /*
    703   1.1   thorpej  * sf_rxintr:
    704   1.1   thorpej  *
    705   1.1   thorpej  *	Helper -- handle receive interrupts.
    706   1.1   thorpej  */
    707  1.11   thorpej static void
    708   1.1   thorpej sf_rxintr(struct sf_softc *sc)
    709   1.1   thorpej {
    710   1.1   thorpej 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    711   1.1   thorpej 	struct sf_descsoft *ds;
    712   1.1   thorpej 	struct sf_rcd_full *rcd;
    713   1.1   thorpej 	struct mbuf *m;
    714   1.1   thorpej 	uint32_t cqci, word0;
    715   1.1   thorpej 	int consumer, producer, bufproducer, rxidx, len;
    716   1.1   thorpej 
    717   1.1   thorpej  try_again:
    718   1.1   thorpej 	cqci = sf_funcreg_read(sc, SF_CompletionQueueConsumerIndex);
    719   1.1   thorpej 
    720   1.1   thorpej 	consumer = CQCI_RxCompletionQ1ConsumerIndex_get(cqci);
    721   1.1   thorpej 	producer = CQPI_RxCompletionQ1ProducerIndex_get(
    722   1.1   thorpej 	    sf_funcreg_read(sc, SF_CompletionQueueProducerIndex));
    723   1.1   thorpej 	bufproducer = RXQ1P_RxDescQ1Producer_get(
    724   1.1   thorpej 	    sf_funcreg_read(sc, SF_RxDescQueue1Ptrs));
    725   1.1   thorpej 
    726   1.1   thorpej 	if (consumer == producer)
    727   1.1   thorpej 		return;
    728   1.1   thorpej 
    729   1.1   thorpej 	while (consumer != producer) {
    730   1.1   thorpej 		rcd = &sc->sc_rxcomp[consumer];
    731   1.1   thorpej 		SF_CDRXCSYNC(sc, consumer,
    732   1.1   thorpej 		    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
    733   1.1   thorpej 		SF_CDRXCSYNC(sc, consumer,
    734   1.1   thorpej 		    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
    735   1.1   thorpej 
    736   1.1   thorpej 		word0 = le32toh(rcd->rcd_word0);
    737   1.1   thorpej 		rxidx = RCD_W0_EndIndex(word0);
    738   1.1   thorpej 
    739   1.1   thorpej 		ds = &sc->sc_rxsoft[rxidx];
    740   1.1   thorpej 
    741   1.1   thorpej 		consumer = SF_NEXTRCD(consumer);
    742   1.1   thorpej 		bufproducer = SF_NEXTRX(bufproducer);
    743   1.1   thorpej 
    744   1.1   thorpej 		if ((word0 & RCD_W0_OK) == 0) {
    745   1.1   thorpej 			SF_INIT_RXDESC(sc, rxidx);
    746   1.1   thorpej 			continue;
    747   1.1   thorpej 		}
    748   1.1   thorpej 
    749   1.1   thorpej 		bus_dmamap_sync(sc->sc_dmat, ds->ds_dmamap, 0,
    750   1.1   thorpej 		    ds->ds_dmamap->dm_mapsize, BUS_DMASYNC_POSTREAD);
    751   1.1   thorpej 
    752   1.1   thorpej 		/*
    753   1.1   thorpej 		 * No errors; receive the packet.  Note that we have
    754   1.1   thorpej 		 * configured the Starfire to NOT transfer the CRC
    755   1.1   thorpej 		 * with the packet.
    756   1.1   thorpej 		 */
    757   1.1   thorpej 		len = RCD_W0_Length(word0);
    758   1.1   thorpej 
    759   1.1   thorpej #ifdef __NO_STRICT_ALIGNMENT
    760   1.1   thorpej 		/*
    761   1.1   thorpej 		 * Allocate a new mbuf cluster.  If that fails, we are
    762   1.1   thorpej 		 * out of memory, and must drop the packet and recycle
    763   1.1   thorpej 		 * the buffer that's already attached to this descriptor.
    764   1.1   thorpej 		 */
    765   1.1   thorpej 		m = ds->ds_mbuf;
    766   1.1   thorpej 		if (sf_add_rxbuf(sc, rxidx) != 0) {
    767   1.1   thorpej 			ifp->if_ierrors++;
    768   1.1   thorpej 			SF_INIT_RXDESC(sc, rxidx);
    769   1.1   thorpej 			bus_dmamap_sync(sc->sc_dmat, ds->ds_dmamap, 0,
    770   1.1   thorpej 			    ds->ds_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
    771   1.1   thorpej 			continue;
    772   1.1   thorpej 		}
    773   1.1   thorpej #else
    774   1.1   thorpej 		/*
    775   1.1   thorpej 		 * The Starfire's receive buffer must be 4-byte aligned.
    776   1.1   thorpej 		 * But this means that the data after the Ethernet header
    777   1.1   thorpej 		 * is misaligned.  We must allocate a new buffer and
    778   1.1   thorpej 		 * copy the data, shifted forward 2 bytes.
    779   1.1   thorpej 		 */
    780   1.1   thorpej 		MGETHDR(m, M_DONTWAIT, MT_DATA);
    781   1.1   thorpej 		if (m == NULL) {
    782   1.1   thorpej  dropit:
    783   1.1   thorpej 			ifp->if_ierrors++;
    784   1.1   thorpej 			SF_INIT_RXDESC(sc, rxidx);
    785   1.1   thorpej 			bus_dmamap_sync(sc->sc_dmat, ds->ds_dmamap, 0,
    786   1.1   thorpej 			    ds->ds_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
    787   1.1   thorpej 			continue;
    788   1.1   thorpej 		}
    789   1.1   thorpej 		if (len > (MHLEN - 2)) {
    790   1.1   thorpej 			MCLGET(m, M_DONTWAIT);
    791   1.1   thorpej 			if ((m->m_flags & M_EXT) == 0) {
    792   1.1   thorpej 				m_freem(m);
    793   1.1   thorpej 				goto dropit;
    794   1.1   thorpej 			}
    795   1.1   thorpej 		}
    796   1.1   thorpej 		m->m_data += 2;
    797   1.1   thorpej 
    798   1.1   thorpej 		/*
    799   1.1   thorpej 		 * Note that we use cluster for incoming frames, so the
    800   1.1   thorpej 		 * buffer is virtually contiguous.
    801   1.1   thorpej 		 */
    802   1.2   thorpej 		memcpy(mtod(m, caddr_t), mtod(ds->ds_mbuf, caddr_t), len);
    803   1.1   thorpej 
    804   1.1   thorpej 		/* Allow the receive descriptor to continue using its mbuf. */
    805   1.1   thorpej 		SF_INIT_RXDESC(sc, rxidx);
    806   1.1   thorpej 		bus_dmamap_sync(sc->sc_dmat, ds->ds_dmamap, 0,
    807   1.1   thorpej 		    ds->ds_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
    808   1.1   thorpej #endif /* __NO_STRICT_ALIGNMENT */
    809   1.1   thorpej 
    810   1.1   thorpej 		m->m_pkthdr.rcvif = ifp;
    811   1.1   thorpej 		m->m_pkthdr.len = m->m_len = len;
    812   1.1   thorpej 
    813   1.1   thorpej #if NBPFILTER > 0
    814   1.1   thorpej 		/*
    815   1.1   thorpej 		 * Pass this up to any BPF listeners.
    816   1.1   thorpej 		 */
    817   1.1   thorpej 		if (ifp->if_bpf)
    818   1.1   thorpej 			bpf_mtap(ifp->if_bpf, m);
    819   1.1   thorpej #endif /* NBPFILTER > 0 */
    820   1.1   thorpej 
    821   1.1   thorpej 		/* Pass it on. */
    822   1.1   thorpej 		(*ifp->if_input)(ifp, m);
    823   1.1   thorpej 	}
    824   1.1   thorpej 
    825   1.1   thorpej 	/* Update the chip's pointers. */
    826   1.1   thorpej 	sf_funcreg_write(sc, SF_CompletionQueueConsumerIndex,
    827   1.1   thorpej 	    (cqci & ~CQCI_RxCompletionQ1ConsumerIndex(0x7ff)) |
    828   1.1   thorpej 	     CQCI_RxCompletionQ1ConsumerIndex(consumer));
    829   1.1   thorpej 	sf_funcreg_write(sc, SF_RxDescQueue1Ptrs,
    830   1.1   thorpej 	    RXQ1P_RxDescQ1Producer(bufproducer));
    831   1.1   thorpej 
    832   1.1   thorpej 	/* Double-check for any new completions. */
    833   1.1   thorpej 	goto try_again;
    834   1.1   thorpej }
    835   1.1   thorpej 
    836   1.1   thorpej /*
    837   1.1   thorpej  * sf_tick:
    838   1.1   thorpej  *
    839   1.1   thorpej  *	One second timer, used to tick the MII and update stats.
    840   1.1   thorpej  */
    841  1.11   thorpej static void
    842   1.1   thorpej sf_tick(void *arg)
    843   1.1   thorpej {
    844   1.1   thorpej 	struct sf_softc *sc = arg;
    845   1.1   thorpej 	int s;
    846   1.1   thorpej 
    847   1.1   thorpej 	s = splnet();
    848   1.1   thorpej 	mii_tick(&sc->sc_mii);
    849   1.1   thorpej 	sf_stats_update(sc);
    850   1.1   thorpej 	splx(s);
    851   1.1   thorpej 
    852   1.1   thorpej 	callout_reset(&sc->sc_tick_callout, hz, sf_tick, sc);
    853   1.1   thorpej }
    854   1.1   thorpej 
    855   1.1   thorpej /*
    856   1.1   thorpej  * sf_stats_update:
    857   1.1   thorpej  *
    858   1.1   thorpej  *	Read the statitistics counters.
    859   1.1   thorpej  */
    860  1.11   thorpej static void
    861   1.1   thorpej sf_stats_update(struct sf_softc *sc)
    862   1.1   thorpej {
    863   1.1   thorpej 	struct sf_stats stats;
    864   1.1   thorpej 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    865   1.1   thorpej 	uint32_t *p;
    866   1.8   thorpej 	u_int i;
    867   1.1   thorpej 
    868   1.1   thorpej 	p = &stats.TransmitOKFrames;
    869   1.1   thorpej 	for (i = 0; i < (sizeof(stats) / sizeof(uint32_t)); i++) {
    870   1.1   thorpej 		*p++ = sf_genreg_read(sc,
    871   1.1   thorpej 		    SF_STATS_BASE + (i * sizeof(uint32_t)));
    872   1.1   thorpej 		sf_genreg_write(sc, SF_STATS_BASE + (i * sizeof(uint32_t)), 0);
    873   1.1   thorpej 	}
    874   1.1   thorpej 
    875   1.1   thorpej 	ifp->if_opackets += stats.TransmitOKFrames;
    876   1.1   thorpej 
    877   1.1   thorpej 	ifp->if_collisions += stats.SingleCollisionFrames +
    878   1.1   thorpej 	    stats.MultipleCollisionFrames;
    879   1.1   thorpej 
    880   1.1   thorpej 	ifp->if_oerrors += stats.TransmitAbortDueToExcessiveCollisions +
    881   1.1   thorpej 	    stats.TransmitAbortDueToExcessingDeferral +
    882   1.1   thorpej 	    stats.FramesLostDueToInternalTransmitErrors;
    883   1.1   thorpej 
    884   1.1   thorpej 	ifp->if_ipackets += stats.ReceiveOKFrames;
    885   1.1   thorpej 
    886   1.1   thorpej 	ifp->if_ierrors += stats.ReceiveCRCErrors + stats.AlignmentErrors +
    887   1.1   thorpej 	    stats.ReceiveFramesTooLong + stats.ReceiveFramesTooShort +
    888   1.1   thorpej 	    stats.ReceiveFramesJabbersError +
    889   1.1   thorpej 	    stats.FramesLostDueToInternalReceiveErrors;
    890   1.1   thorpej }
    891   1.1   thorpej 
    892   1.1   thorpej /*
    893   1.1   thorpej  * sf_reset:
    894   1.1   thorpej  *
    895   1.1   thorpej  *	Perform a soft reset on the Starfire.
    896   1.1   thorpej  */
    897  1.11   thorpej static void
    898   1.1   thorpej sf_reset(struct sf_softc *sc)
    899   1.1   thorpej {
    900   1.1   thorpej 	int i;
    901   1.1   thorpej 
    902   1.1   thorpej 	sf_funcreg_write(sc, SF_GeneralEthernetCtrl, 0);
    903   1.1   thorpej 
    904   1.1   thorpej 	sf_macreset(sc);
    905   1.1   thorpej 
    906   1.1   thorpej 	sf_funcreg_write(sc, SF_PciDeviceConfig, PDC_SoftReset);
    907   1.1   thorpej 	for (i = 0; i < 1000; i++) {
    908   1.1   thorpej 		delay(10);
    909   1.1   thorpej 		if ((sf_funcreg_read(sc, SF_PciDeviceConfig) &
    910   1.1   thorpej 		     PDC_SoftReset) == 0)
    911   1.1   thorpej 			break;
    912   1.1   thorpej 	}
    913   1.1   thorpej 
    914   1.1   thorpej 	if (i == 1000) {
    915   1.1   thorpej 		printf("%s: reset failed to complete\n", sc->sc_dev.dv_xname);
    916   1.1   thorpej 		sf_funcreg_write(sc, SF_PciDeviceConfig, 0);
    917   1.1   thorpej 	}
    918   1.1   thorpej 
    919   1.1   thorpej 	delay(1000);
    920   1.1   thorpej }
    921   1.1   thorpej 
    922   1.1   thorpej /*
    923   1.1   thorpej  * sf_macreset:
    924   1.1   thorpej  *
    925   1.1   thorpej  *	Reset the MAC portion of the Starfire.
    926   1.1   thorpej  */
    927  1.11   thorpej static void
    928   1.1   thorpej sf_macreset(struct sf_softc *sc)
    929   1.1   thorpej {
    930   1.1   thorpej 
    931   1.1   thorpej 	sf_genreg_write(sc, SF_MacConfig1, sc->sc_MacConfig1 | MC1_SoftRst);
    932   1.1   thorpej 	delay(1000);
    933   1.1   thorpej 	sf_genreg_write(sc, SF_MacConfig1, sc->sc_MacConfig1);
    934   1.1   thorpej }
    935   1.1   thorpej 
    936   1.1   thorpej /*
    937   1.1   thorpej  * sf_init:		[ifnet interface function]
    938   1.1   thorpej  *
    939   1.1   thorpej  *	Initialize the interface.  Must be called at splnet().
    940   1.1   thorpej  */
    941  1.11   thorpej static int
    942   1.1   thorpej sf_init(struct ifnet *ifp)
    943   1.1   thorpej {
    944   1.1   thorpej 	struct sf_softc *sc = ifp->if_softc;
    945   1.1   thorpej 	struct sf_descsoft *ds;
    946   1.8   thorpej 	int error = 0;
    947   1.8   thorpej 	u_int i;
    948   1.1   thorpej 
    949   1.1   thorpej 	/*
    950   1.1   thorpej 	 * Cancel any pending I/O.
    951   1.1   thorpej 	 */
    952   1.1   thorpej 	sf_stop(ifp, 0);
    953   1.1   thorpej 
    954   1.1   thorpej 	/*
    955   1.1   thorpej 	 * Reset the Starfire to a known state.
    956   1.1   thorpej 	 */
    957   1.1   thorpej 	sf_reset(sc);
    958   1.1   thorpej 
    959   1.1   thorpej 	/* Clear the stat counters. */
    960   1.1   thorpej 	for (i = 0; i < sizeof(struct sf_stats); i += sizeof(uint32_t))
    961   1.1   thorpej 		sf_genreg_write(sc, SF_STATS_BASE + i, 0);
    962   1.1   thorpej 
    963   1.1   thorpej 	/*
    964   1.1   thorpej 	 * Initialize the transmit descriptor ring.
    965   1.1   thorpej 	 */
    966   1.1   thorpej 	memset(sc->sc_txdescs, 0, sizeof(sc->sc_txdescs));
    967   1.1   thorpej 	sf_funcreg_write(sc, SF_TxDescQueueHighAddr, 0);
    968   1.1   thorpej 	sf_funcreg_write(sc, SF_HiPrTxDescQueueBaseAddr, SF_CDTXDADDR(sc, 0));
    969   1.1   thorpej 	sf_funcreg_write(sc, SF_LoPrTxDescQueueBaseAddr, 0);
    970   1.1   thorpej 
    971   1.1   thorpej 	/*
    972   1.1   thorpej 	 * Initialize the transmit completion ring.
    973   1.1   thorpej 	 */
    974   1.1   thorpej 	for (i = 0; i < SF_NTCD; i++) {
    975   1.1   thorpej 		sc->sc_txcomp[i].tcd_word0 = TCD_DMA_ID;
    976   1.1   thorpej 		SF_CDTXCSYNC(sc, i, BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
    977   1.1   thorpej 	}
    978   1.1   thorpej 	sf_funcreg_write(sc, SF_CompletionQueueHighAddr, 0);
    979   1.1   thorpej 	sf_funcreg_write(sc, SF_TxCompletionQueueCtrl, SF_CDTXCADDR(sc, 0));
    980   1.1   thorpej 
    981   1.1   thorpej 	/*
    982   1.1   thorpej 	 * Initialize the receive descriptor ring.
    983   1.1   thorpej 	 */
    984   1.1   thorpej 	for (i = 0; i < SF_NRXDESC; i++) {
    985   1.1   thorpej 		ds = &sc->sc_rxsoft[i];
    986   1.1   thorpej 		if (ds->ds_mbuf == NULL) {
    987   1.1   thorpej 			if ((error = sf_add_rxbuf(sc, i)) != 0) {
    988   1.1   thorpej 				printf("%s: unable to allocate or map rx "
    989   1.1   thorpej 				    "buffer %d, error = %d\n",
    990   1.1   thorpej 				    sc->sc_dev.dv_xname, i, error);
    991   1.1   thorpej 				/*
    992   1.1   thorpej 				 * XXX Should attempt to run with fewer receive
    993   1.1   thorpej 				 * XXX buffers instead of just failing.
    994   1.1   thorpej 				 */
    995   1.1   thorpej 				sf_rxdrain(sc);
    996   1.1   thorpej 				goto out;
    997   1.1   thorpej 			}
    998   1.4   thorpej 		} else
    999   1.4   thorpej 			SF_INIT_RXDESC(sc, i);
   1000   1.1   thorpej 	}
   1001   1.1   thorpej 	sf_funcreg_write(sc, SF_RxDescQueueHighAddress, 0);
   1002   1.1   thorpej 	sf_funcreg_write(sc, SF_RxDescQueue1LowAddress, SF_CDRXDADDR(sc, 0));
   1003   1.1   thorpej 	sf_funcreg_write(sc, SF_RxDescQueue2LowAddress, 0);
   1004   1.1   thorpej 
   1005   1.1   thorpej 	/*
   1006   1.1   thorpej 	 * Initialize the receive completion ring.
   1007   1.1   thorpej 	 */
   1008   1.1   thorpej 	for (i = 0; i < SF_NRCD; i++) {
   1009   1.1   thorpej 		sc->sc_rxcomp[i].rcd_word0 = RCD_W0_ID;
   1010   1.1   thorpej 		sc->sc_rxcomp[i].rcd_word1 = 0;
   1011   1.1   thorpej 		sc->sc_rxcomp[i].rcd_word2 = 0;
   1012   1.1   thorpej 		sc->sc_rxcomp[i].rcd_timestamp = 0;
   1013   1.1   thorpej 		SF_CDRXCSYNC(sc, i, BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
   1014   1.1   thorpej 	}
   1015   1.1   thorpej 	sf_funcreg_write(sc, SF_RxCompletionQueue1Ctrl, SF_CDRXCADDR(sc, 0) |
   1016   1.1   thorpej 	    RCQ1C_RxCompletionQ1Type(3));
   1017   1.1   thorpej 	sf_funcreg_write(sc, SF_RxCompletionQueue2Ctrl, 0);
   1018   1.1   thorpej 
   1019   1.1   thorpej 	/*
   1020   1.1   thorpej 	 * Initialize the Tx CSR.
   1021   1.1   thorpej 	 */
   1022   1.1   thorpej 	sc->sc_TransmitFrameCSR = 0;
   1023   1.1   thorpej 	sf_funcreg_write(sc, SF_TransmitFrameCSR,
   1024   1.1   thorpej 	    sc->sc_TransmitFrameCSR |
   1025   1.1   thorpej 	    TFCSR_TransmitThreshold(sc->sc_txthresh));
   1026   1.1   thorpej 
   1027   1.1   thorpej 	/*
   1028   1.1   thorpej 	 * Initialize the Tx descriptor control register.
   1029   1.1   thorpej 	 */
   1030   1.1   thorpej 	sc->sc_TxDescQueueCtrl = TDQC_SkipLength(0) |
   1031   1.1   thorpej 	    TDQC_TxDmaBurstSize(4) |	/* default */
   1032   1.6   thorpej 	    TDQC_MinFrameSpacing(3) |	/* 128 bytes */
   1033   1.1   thorpej 	    TDQC_TxDescType(0);
   1034   1.1   thorpej 	sf_funcreg_write(sc, SF_TxDescQueueCtrl,
   1035   1.1   thorpej 	    sc->sc_TxDescQueueCtrl |
   1036   1.1   thorpej 	    TDQC_TxHighPriorityFifoThreshold(sc->sc_txthresh));
   1037   1.1   thorpej 
   1038   1.1   thorpej 	/*
   1039   1.1   thorpej 	 * Initialize the Rx descriptor control registers.
   1040   1.1   thorpej 	 */
   1041   1.1   thorpej 	sf_funcreg_write(sc, SF_RxDescQueue1Ctrl,
   1042   1.1   thorpej 	    RDQ1C_RxQ1BufferLength(MCLBYTES) |
   1043   1.1   thorpej 	    RDQ1C_RxDescSpacing(0));
   1044   1.1   thorpej 	sf_funcreg_write(sc, SF_RxDescQueue2Ctrl, 0);
   1045   1.1   thorpej 
   1046   1.1   thorpej 	/*
   1047   1.1   thorpej 	 * Initialize the Tx descriptor producer indices.
   1048   1.1   thorpej 	 */
   1049   1.1   thorpej 	sf_funcreg_write(sc, SF_TxDescQueueProducerIndex,
   1050   1.1   thorpej 	    TDQPI_HiPrTxProducerIndex(0) |
   1051   1.1   thorpej 	    TDQPI_LoPrTxProducerIndex(0));
   1052   1.1   thorpej 
   1053   1.1   thorpej 	/*
   1054   1.1   thorpej 	 * Initialize the Rx descriptor producer indices.
   1055   1.1   thorpej 	 */
   1056   1.1   thorpej 	sf_funcreg_write(sc, SF_RxDescQueue1Ptrs,
   1057   1.1   thorpej 	    RXQ1P_RxDescQ1Producer(SF_NRXDESC - 1));
   1058   1.1   thorpej 	sf_funcreg_write(sc, SF_RxDescQueue2Ptrs,
   1059   1.1   thorpej 	    RXQ2P_RxDescQ2Producer(0));
   1060   1.1   thorpej 
   1061   1.1   thorpej 	/*
   1062   1.1   thorpej 	 * Initialize the Tx and Rx completion queue consumer indices.
   1063   1.1   thorpej 	 */
   1064   1.1   thorpej 	sf_funcreg_write(sc, SF_CompletionQueueConsumerIndex,
   1065   1.1   thorpej 	    CQCI_TxCompletionConsumerIndex(0) |
   1066   1.1   thorpej 	    CQCI_RxCompletionQ1ConsumerIndex(0));
   1067   1.1   thorpej 	sf_funcreg_write(sc, SF_RxHiPrCompletionPtrs, 0);
   1068   1.1   thorpej 
   1069   1.1   thorpej 	/*
   1070   1.1   thorpej 	 * Initialize the Rx DMA control register.
   1071   1.1   thorpej 	 */
   1072   1.1   thorpej 	sf_funcreg_write(sc, SF_RxDmaCtrl,
   1073   1.1   thorpej 	    RDC_RxHighPriorityThreshold(6) |	/* default */
   1074   1.1   thorpej 	    RDC_RxBurstSize(4));		/* default */
   1075   1.1   thorpej 
   1076   1.1   thorpej 	/*
   1077   1.1   thorpej 	 * Set the receive filter.
   1078   1.1   thorpej 	 */
   1079   1.1   thorpej 	sc->sc_RxAddressFilteringCtl = 0;
   1080   1.1   thorpej 	sf_set_filter(sc);
   1081   1.1   thorpej 
   1082   1.1   thorpej 	/*
   1083   1.1   thorpej 	 * Set MacConfig1.  When we set the media, MacConfig1 will
   1084   1.1   thorpej 	 * actually be written and the MAC part reset.
   1085   1.1   thorpej 	 */
   1086   1.1   thorpej 	sc->sc_MacConfig1 = MC1_PadEn;
   1087   1.1   thorpej 
   1088   1.1   thorpej 	/*
   1089   1.1   thorpej 	 * Set the media.
   1090   1.1   thorpej 	 */
   1091   1.1   thorpej 	mii_mediachg(&sc->sc_mii);
   1092   1.1   thorpej 
   1093   1.1   thorpej 	/*
   1094   1.1   thorpej 	 * Initialize the interrupt register.
   1095   1.1   thorpej 	 */
   1096   1.1   thorpej 	sc->sc_InterruptEn = IS_PCIPadInt | IS_RxQ1DoneInt |
   1097   1.1   thorpej 	    IS_TxQueueDoneInt | IS_TxDmaDoneInt | IS_DmaErrInt |
   1098   1.1   thorpej 	    IS_StatisticWrapInt;
   1099   1.1   thorpej 	sf_funcreg_write(sc, SF_InterruptEn, sc->sc_InterruptEn);
   1100   1.1   thorpej 
   1101   1.1   thorpej 	sf_funcreg_write(sc, SF_PciDeviceConfig, PDC_IntEnable |
   1102   1.1   thorpej 	    PDC_PCIMstDmaEn | (1 << PDC_FifoThreshold_SHIFT));
   1103   1.1   thorpej 
   1104   1.1   thorpej 	/*
   1105   1.1   thorpej 	 * Start the transmit and receive processes.
   1106   1.1   thorpej 	 */
   1107   1.1   thorpej 	sf_funcreg_write(sc, SF_GeneralEthernetCtrl,
   1108   1.1   thorpej 	    GEC_TxDmaEn|GEC_RxDmaEn|GEC_TransmitEn|GEC_ReceiveEn);
   1109   1.1   thorpej 
   1110   1.1   thorpej 	/* Start the on second clock. */
   1111   1.1   thorpej 	callout_reset(&sc->sc_tick_callout, hz, sf_tick, sc);
   1112   1.1   thorpej 
   1113   1.1   thorpej 	/*
   1114   1.1   thorpej 	 * Note that the interface is now running.
   1115   1.1   thorpej 	 */
   1116   1.1   thorpej 	ifp->if_flags |= IFF_RUNNING;
   1117   1.1   thorpej 	ifp->if_flags &= ~IFF_OACTIVE;
   1118   1.1   thorpej 
   1119   1.1   thorpej  out:
   1120   1.1   thorpej 	if (error) {
   1121   1.1   thorpej 		ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
   1122   1.1   thorpej 		ifp->if_timer = 0;
   1123   1.1   thorpej 		printf("%s: interface not running\n", sc->sc_dev.dv_xname);
   1124   1.1   thorpej 	}
   1125   1.1   thorpej 	return (error);
   1126   1.1   thorpej }
   1127   1.1   thorpej 
   1128   1.1   thorpej /*
   1129   1.1   thorpej  * sf_rxdrain:
   1130   1.1   thorpej  *
   1131   1.1   thorpej  *	Drain the receive queue.
   1132   1.1   thorpej  */
   1133  1.11   thorpej static void
   1134   1.1   thorpej sf_rxdrain(struct sf_softc *sc)
   1135   1.1   thorpej {
   1136   1.1   thorpej 	struct sf_descsoft *ds;
   1137   1.1   thorpej 	int i;
   1138   1.1   thorpej 
   1139   1.1   thorpej 	for (i = 0; i < SF_NRXDESC; i++) {
   1140   1.1   thorpej 		ds = &sc->sc_rxsoft[i];
   1141   1.1   thorpej 		if (ds->ds_mbuf != NULL) {
   1142   1.1   thorpej 			bus_dmamap_unload(sc->sc_dmat, ds->ds_dmamap);
   1143   1.1   thorpej 			m_freem(ds->ds_mbuf);
   1144   1.1   thorpej 			ds->ds_mbuf = NULL;
   1145   1.1   thorpej 		}
   1146   1.1   thorpej 	}
   1147   1.1   thorpej }
   1148   1.1   thorpej 
   1149   1.1   thorpej /*
   1150   1.1   thorpej  * sf_stop:		[ifnet interface function]
   1151   1.1   thorpej  *
   1152   1.1   thorpej  *	Stop transmission on the interface.
   1153   1.1   thorpej  */
   1154  1.11   thorpej static void
   1155   1.1   thorpej sf_stop(struct ifnet *ifp, int disable)
   1156   1.1   thorpej {
   1157   1.1   thorpej 	struct sf_softc *sc = ifp->if_softc;
   1158   1.1   thorpej 	struct sf_descsoft *ds;
   1159   1.1   thorpej 	int i;
   1160   1.1   thorpej 
   1161   1.1   thorpej 	/* Stop the one second clock. */
   1162   1.1   thorpej 	callout_stop(&sc->sc_tick_callout);
   1163   1.1   thorpej 
   1164   1.1   thorpej 	/* Down the MII. */
   1165   1.1   thorpej 	mii_down(&sc->sc_mii);
   1166   1.1   thorpej 
   1167   1.1   thorpej 	/* Disable interrupts. */
   1168   1.1   thorpej 	sf_funcreg_write(sc, SF_InterruptEn, 0);
   1169   1.1   thorpej 
   1170   1.1   thorpej 	/* Stop the transmit and receive processes. */
   1171   1.1   thorpej 	sf_funcreg_write(sc, SF_GeneralEthernetCtrl, 0);
   1172   1.1   thorpej 
   1173   1.1   thorpej 	/*
   1174   1.1   thorpej 	 * Release any queued transmit buffers.
   1175   1.1   thorpej 	 */
   1176   1.1   thorpej 	for (i = 0; i < SF_NTXDESC; i++) {
   1177   1.1   thorpej 		ds = &sc->sc_txsoft[i];
   1178   1.1   thorpej 		if (ds->ds_mbuf != NULL) {
   1179   1.1   thorpej 			bus_dmamap_unload(sc->sc_dmat, ds->ds_dmamap);
   1180   1.1   thorpej 			m_freem(ds->ds_mbuf);
   1181   1.1   thorpej 			ds->ds_mbuf = NULL;
   1182   1.1   thorpej 		}
   1183   1.1   thorpej 	}
   1184   1.1   thorpej 
   1185   1.1   thorpej 	if (disable)
   1186   1.1   thorpej 		sf_rxdrain(sc);
   1187   1.1   thorpej 
   1188   1.1   thorpej 	/*
   1189   1.1   thorpej 	 * Mark the interface down and cancel the watchdog timer.
   1190   1.1   thorpej 	 */
   1191   1.1   thorpej 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
   1192   1.1   thorpej 	ifp->if_timer = 0;
   1193   1.1   thorpej }
   1194   1.1   thorpej 
   1195   1.1   thorpej /*
   1196   1.1   thorpej  * sf_read_eeprom:
   1197   1.1   thorpej  *
   1198   1.1   thorpej  *	Read from the Starfire EEPROM.
   1199   1.1   thorpej  */
   1200  1.11   thorpej static uint8_t
   1201   1.1   thorpej sf_read_eeprom(struct sf_softc *sc, int offset)
   1202   1.1   thorpej {
   1203   1.1   thorpej 	uint32_t reg;
   1204   1.1   thorpej 
   1205   1.1   thorpej 	reg = sf_genreg_read(sc, SF_EEPROM_BASE + (offset & ~3));
   1206   1.1   thorpej 
   1207   1.1   thorpej 	return ((reg >> (8 * (offset & 3))) & 0xff);
   1208   1.1   thorpej }
   1209   1.1   thorpej 
   1210   1.1   thorpej /*
   1211   1.1   thorpej  * sf_add_rxbuf:
   1212   1.1   thorpej  *
   1213   1.1   thorpej  *	Add a receive buffer to the indicated descriptor.
   1214   1.1   thorpej  */
   1215  1.11   thorpej static int
   1216   1.1   thorpej sf_add_rxbuf(struct sf_softc *sc, int idx)
   1217   1.1   thorpej {
   1218   1.1   thorpej 	struct sf_descsoft *ds = &sc->sc_rxsoft[idx];
   1219   1.1   thorpej 	struct mbuf *m;
   1220   1.1   thorpej 	int error;
   1221   1.1   thorpej 
   1222   1.1   thorpej 	MGETHDR(m, M_DONTWAIT, MT_DATA);
   1223   1.1   thorpej 	if (m == NULL)
   1224   1.1   thorpej 		return (ENOBUFS);
   1225   1.1   thorpej 
   1226   1.1   thorpej 	MCLGET(m, M_DONTWAIT);
   1227   1.1   thorpej 	if ((m->m_flags & M_EXT) == 0) {
   1228   1.1   thorpej 		m_freem(m);
   1229   1.1   thorpej 		return (ENOBUFS);
   1230   1.1   thorpej 	}
   1231   1.1   thorpej 
   1232   1.1   thorpej 	if (ds->ds_mbuf != NULL)
   1233   1.1   thorpej 		bus_dmamap_unload(sc->sc_dmat, ds->ds_dmamap);
   1234   1.1   thorpej 
   1235   1.1   thorpej 	ds->ds_mbuf = m;
   1236   1.1   thorpej 
   1237   1.1   thorpej 	error = bus_dmamap_load(sc->sc_dmat, ds->ds_dmamap,
   1238   1.3   thorpej 	    m->m_ext.ext_buf, m->m_ext.ext_size, NULL,
   1239   1.3   thorpej 	    BUS_DMA_READ|BUS_DMA_NOWAIT);
   1240   1.1   thorpej 	if (error) {
   1241   1.1   thorpej 		printf("%s: can't load rx DMA map %d, error = %d\n",
   1242   1.1   thorpej 		    sc->sc_dev.dv_xname, idx, error);
   1243   1.1   thorpej 		panic("sf_add_rxbuf"); /* XXX */
   1244   1.1   thorpej 	}
   1245   1.1   thorpej 
   1246   1.1   thorpej 	bus_dmamap_sync(sc->sc_dmat, ds->ds_dmamap, 0,
   1247   1.1   thorpej 	    ds->ds_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
   1248   1.1   thorpej 
   1249   1.1   thorpej 	SF_INIT_RXDESC(sc, idx);
   1250   1.1   thorpej 
   1251   1.1   thorpej 	return (0);
   1252   1.1   thorpej }
   1253   1.1   thorpej 
   1254   1.1   thorpej static void
   1255   1.1   thorpej sf_set_filter_perfect(struct sf_softc *sc, int slot, uint8_t *enaddr)
   1256   1.1   thorpej {
   1257   1.1   thorpej 	uint32_t reg0, reg1, reg2;
   1258   1.1   thorpej 
   1259   1.1   thorpej 	reg0 = enaddr[5] | (enaddr[4] << 8);
   1260   1.1   thorpej 	reg1 = enaddr[3] | (enaddr[2] << 8);
   1261   1.1   thorpej 	reg2 = enaddr[1] | (enaddr[0] << 8);
   1262   1.1   thorpej 
   1263   1.1   thorpej 	sf_genreg_write(sc, SF_PERFECT_BASE + (slot * 0x10) + 0, reg0);
   1264   1.1   thorpej 	sf_genreg_write(sc, SF_PERFECT_BASE + (slot * 0x10) + 4, reg1);
   1265   1.1   thorpej 	sf_genreg_write(sc, SF_PERFECT_BASE + (slot * 0x10) + 8, reg2);
   1266   1.1   thorpej }
   1267   1.1   thorpej 
   1268   1.1   thorpej static void
   1269   1.1   thorpej sf_set_filter_hash(struct sf_softc *sc, uint8_t *enaddr)
   1270   1.1   thorpej {
   1271   1.1   thorpej 	uint32_t hash, slot, reg;
   1272   1.1   thorpej 
   1273   1.1   thorpej 	hash = ether_crc32_be(enaddr, ETHER_ADDR_LEN) >> 23;
   1274   1.1   thorpej 	slot = hash >> 4;
   1275   1.1   thorpej 
   1276   1.1   thorpej 	reg = sf_genreg_read(sc, SF_HASH_BASE + (slot * 0x10));
   1277   1.1   thorpej 	reg |= 1 << (hash & 0xf);
   1278   1.1   thorpej 	sf_genreg_write(sc, SF_HASH_BASE + (slot * 0x10), reg);
   1279   1.1   thorpej }
   1280   1.1   thorpej 
   1281   1.1   thorpej /*
   1282   1.1   thorpej  * sf_set_filter:
   1283   1.1   thorpej  *
   1284   1.1   thorpej  *	Set the Starfire receive filter.
   1285   1.1   thorpej  */
   1286  1.11   thorpej static void
   1287   1.1   thorpej sf_set_filter(struct sf_softc *sc)
   1288   1.1   thorpej {
   1289   1.1   thorpej 	struct ethercom *ec = &sc->sc_ethercom;
   1290   1.1   thorpej 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1291   1.1   thorpej 	struct ether_multi *enm;
   1292   1.1   thorpej 	struct ether_multistep step;
   1293   1.1   thorpej 	int i;
   1294   1.1   thorpej 
   1295   1.1   thorpej 	/* Start by clearing the perfect and hash tables. */
   1296   1.1   thorpej 	for (i = 0; i < SF_PERFECT_SIZE; i += sizeof(uint32_t))
   1297   1.1   thorpej 		sf_genreg_write(sc, SF_PERFECT_BASE + i, 0);
   1298   1.1   thorpej 
   1299   1.1   thorpej 	for (i = 0; i < SF_HASH_SIZE; i += sizeof(uint32_t))
   1300   1.1   thorpej 		sf_genreg_write(sc, SF_HASH_BASE + i, 0);
   1301   1.1   thorpej 
   1302   1.1   thorpej 	/*
   1303   1.1   thorpej 	 * Clear the perfect and hash mode bits.
   1304   1.1   thorpej 	 */
   1305   1.1   thorpej 	sc->sc_RxAddressFilteringCtl &=
   1306   1.1   thorpej 	    ~(RAFC_PerfectFilteringMode(3) | RAFC_HashFilteringMode(3));
   1307   1.1   thorpej 
   1308   1.1   thorpej 	if (ifp->if_flags & IFF_BROADCAST)
   1309   1.1   thorpej 		sc->sc_RxAddressFilteringCtl |= RAFC_PassBroadcast;
   1310   1.1   thorpej 	else
   1311   1.1   thorpej 		sc->sc_RxAddressFilteringCtl &= ~RAFC_PassBroadcast;
   1312   1.1   thorpej 
   1313   1.1   thorpej 	if (ifp->if_flags & IFF_PROMISC) {
   1314   1.1   thorpej 		sc->sc_RxAddressFilteringCtl |= RAFC_PromiscuousMode;
   1315   1.1   thorpej 		goto allmulti;
   1316   1.1   thorpej 	} else
   1317   1.1   thorpej 		sc->sc_RxAddressFilteringCtl &= ~RAFC_PromiscuousMode;
   1318   1.1   thorpej 
   1319   1.1   thorpej 	/*
   1320   1.1   thorpej 	 * Set normal perfect filtering mode.
   1321   1.1   thorpej 	 */
   1322   1.1   thorpej 	sc->sc_RxAddressFilteringCtl |= RAFC_PerfectFilteringMode(1);
   1323   1.1   thorpej 
   1324   1.1   thorpej 	/*
   1325   1.1   thorpej 	 * First, write the station address to the perfect filter
   1326   1.1   thorpej 	 * table.
   1327   1.1   thorpej 	 */
   1328   1.1   thorpej 	sf_set_filter_perfect(sc, 0, LLADDR(ifp->if_sadl));
   1329   1.1   thorpej 
   1330   1.1   thorpej 	/*
   1331   1.1   thorpej 	 * Now set the hash bits for each multicast address in our
   1332   1.1   thorpej 	 * list.
   1333   1.1   thorpej 	 */
   1334   1.1   thorpej 	ETHER_FIRST_MULTI(step, ec, enm);
   1335   1.1   thorpej 	if (enm == NULL)
   1336   1.1   thorpej 		goto done;
   1337   1.1   thorpej 	while (enm != NULL) {
   1338   1.1   thorpej 		if (memcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
   1339   1.1   thorpej 			/*
   1340   1.1   thorpej 			 * We must listen to a range of multicast addresses.
   1341   1.1   thorpej 			 * For now, just accept all multicasts, rather than
   1342   1.1   thorpej 			 * trying to set only those filter bits needed to match
   1343   1.1   thorpej 			 * the range.  (At this time, the only use of address
   1344   1.1   thorpej 			 * ranges is for IP multicast routing, for which the
   1345   1.1   thorpej 			 * range is big enough to require all bits set.)
   1346   1.1   thorpej 			 */
   1347   1.1   thorpej 			goto allmulti;
   1348   1.1   thorpej 		}
   1349   1.1   thorpej 		sf_set_filter_hash(sc, enm->enm_addrlo);
   1350   1.1   thorpej 		ETHER_NEXT_MULTI(step, enm);
   1351   1.1   thorpej 	}
   1352  1.13     perry 
   1353   1.1   thorpej 	/*
   1354   1.1   thorpej 	 * Set "hash only multicast dest, match regardless of VLAN ID".
   1355   1.1   thorpej 	 */
   1356   1.1   thorpej 	sc->sc_RxAddressFilteringCtl |= RAFC_HashFilteringMode(2);
   1357   1.1   thorpej 	goto done;
   1358   1.1   thorpej 
   1359   1.1   thorpej  allmulti:
   1360   1.1   thorpej 	/*
   1361   1.1   thorpej 	 * XXX RAFC_PassMulticast is sub-optimal if using VLAN mode.
   1362   1.1   thorpej 	 */
   1363   1.1   thorpej 	sc->sc_RxAddressFilteringCtl |= RAFC_PassMulticast;
   1364   1.1   thorpej 	ifp->if_flags |= IFF_ALLMULTI;
   1365   1.1   thorpej 
   1366   1.1   thorpej  done:
   1367   1.1   thorpej 	sf_funcreg_write(sc, SF_RxAddressFilteringCtl,
   1368   1.1   thorpej 	    sc->sc_RxAddressFilteringCtl);
   1369   1.1   thorpej }
   1370   1.1   thorpej 
   1371   1.1   thorpej /*
   1372   1.1   thorpej  * sf_mii_read:		[mii interface function]
   1373   1.1   thorpej  *
   1374   1.1   thorpej  *	Read from the MII.
   1375   1.1   thorpej  */
   1376  1.11   thorpej static int
   1377   1.1   thorpej sf_mii_read(struct device *self, int phy, int reg)
   1378   1.1   thorpej {
   1379   1.1   thorpej 	struct sf_softc *sc = (void *) self;
   1380   1.1   thorpej 	uint32_t v;
   1381   1.1   thorpej 	int i;
   1382   1.1   thorpej 
   1383   1.1   thorpej 	for (i = 0; i < 1000; i++) {
   1384   1.1   thorpej 		v = sf_genreg_read(sc, SF_MII_PHY_REG(phy, reg));
   1385   1.1   thorpej 		if (v & MiiDataValid)
   1386   1.1   thorpej 			break;
   1387   1.1   thorpej 		delay(1);
   1388   1.1   thorpej 	}
   1389   1.1   thorpej 
   1390   1.1   thorpej 	if ((v & MiiDataValid) == 0)
   1391   1.1   thorpej 		return (0);
   1392   1.1   thorpej 
   1393   1.1   thorpej 	if (MiiRegDataPort(v) == 0xffff)
   1394   1.1   thorpej 		return (0);
   1395   1.1   thorpej 
   1396   1.1   thorpej 	return (MiiRegDataPort(v));
   1397   1.1   thorpej }
   1398   1.1   thorpej 
   1399   1.1   thorpej /*
   1400   1.1   thorpej  * sf_mii_write:	[mii interface function]
   1401   1.1   thorpej  *
   1402   1.1   thorpej  *	Write to the MII.
   1403   1.1   thorpej  */
   1404  1.11   thorpej static void
   1405   1.1   thorpej sf_mii_write(struct device *self, int phy, int reg, int val)
   1406   1.1   thorpej {
   1407   1.1   thorpej 	struct sf_softc *sc = (void *) self;
   1408   1.1   thorpej 	int i;
   1409   1.1   thorpej 
   1410   1.1   thorpej 	sf_genreg_write(sc, SF_MII_PHY_REG(phy, reg), val);
   1411   1.1   thorpej 
   1412   1.1   thorpej 	for (i = 0; i < 1000; i++) {
   1413   1.1   thorpej 		if ((sf_genreg_read(sc, SF_MII_PHY_REG(phy, reg)) &
   1414   1.1   thorpej 		     MiiBusy) == 0)
   1415   1.1   thorpej 			return;
   1416   1.1   thorpej 		delay(1);
   1417   1.1   thorpej 	}
   1418   1.1   thorpej 
   1419   1.1   thorpej 	printf("%s: MII write timed out\n", sc->sc_dev.dv_xname);
   1420   1.1   thorpej }
   1421   1.1   thorpej 
   1422   1.1   thorpej /*
   1423   1.1   thorpej  * sf_mii_statchg:	[mii interface function]
   1424   1.1   thorpej  *
   1425   1.1   thorpej  *	Callback from the PHY when the media changes.
   1426   1.1   thorpej  */
   1427  1.11   thorpej static void
   1428   1.1   thorpej sf_mii_statchg(struct device *self)
   1429   1.1   thorpej {
   1430   1.1   thorpej 	struct sf_softc *sc = (void *) self;
   1431   1.1   thorpej 	uint32_t ipg;
   1432   1.1   thorpej 
   1433   1.1   thorpej 	if (sc->sc_mii.mii_media_active & IFM_FDX) {
   1434   1.1   thorpej 		sc->sc_MacConfig1 |= MC1_FullDuplex;
   1435   1.1   thorpej 		ipg = 0x15;
   1436   1.1   thorpej 	} else {
   1437   1.1   thorpej 		sc->sc_MacConfig1 &= ~MC1_FullDuplex;
   1438   1.1   thorpej 		ipg = 0x11;
   1439   1.1   thorpej 	}
   1440   1.1   thorpej 
   1441   1.1   thorpej 	sf_genreg_write(sc, SF_MacConfig1, sc->sc_MacConfig1);
   1442   1.1   thorpej 	sf_macreset(sc);
   1443   1.1   thorpej 
   1444   1.1   thorpej 	sf_genreg_write(sc, SF_BkToBkIPG, ipg);
   1445   1.1   thorpej }
   1446   1.1   thorpej 
   1447   1.1   thorpej /*
   1448   1.1   thorpej  * sf_mediastatus:	[ifmedia interface function]
   1449   1.1   thorpej  *
   1450   1.1   thorpej  *	Callback from ifmedia to request current media status.
   1451   1.1   thorpej  */
   1452  1.11   thorpej static void
   1453   1.1   thorpej sf_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr)
   1454   1.1   thorpej {
   1455   1.1   thorpej 	struct sf_softc *sc = ifp->if_softc;
   1456   1.1   thorpej 
   1457   1.1   thorpej 	mii_pollstat(&sc->sc_mii);
   1458   1.1   thorpej 	ifmr->ifm_status = sc->sc_mii.mii_media_status;
   1459   1.1   thorpej 	ifmr->ifm_active = sc->sc_mii.mii_media_active;
   1460   1.1   thorpej }
   1461   1.1   thorpej 
   1462   1.1   thorpej /*
   1463   1.1   thorpej  * sf_mediachange:	[ifmedia interface function]
   1464   1.1   thorpej  *
   1465   1.1   thorpej  *	Callback from ifmedia to request new media setting.
   1466   1.1   thorpej  */
   1467  1.11   thorpej static int
   1468   1.1   thorpej sf_mediachange(struct ifnet *ifp)
   1469   1.1   thorpej {
   1470   1.1   thorpej 	struct sf_softc *sc = ifp->if_softc;
   1471   1.1   thorpej 
   1472   1.1   thorpej 	if (ifp->if_flags & IFF_UP)
   1473   1.1   thorpej 		mii_mediachg(&sc->sc_mii);
   1474   1.1   thorpej 	return (0);
   1475   1.1   thorpej }
   1476