Home | History | Annotate | Line # | Download | only in ic
hme.c revision 1.27
      1  1.27     tron /*	$NetBSD: hme.c,v 1.27 2001/11/26 06:51:12 tron Exp $	*/
      2   1.1       pk 
      3   1.1       pk /*-
      4   1.1       pk  * Copyright (c) 1999 The NetBSD Foundation, Inc.
      5   1.1       pk  * All rights reserved.
      6   1.1       pk  *
      7   1.1       pk  * This code is derived from software contributed to The NetBSD Foundation
      8   1.1       pk  * by Paul Kranenburg.
      9   1.1       pk  *
     10   1.1       pk  * Redistribution and use in source and binary forms, with or without
     11   1.1       pk  * modification, are permitted provided that the following conditions
     12   1.1       pk  * are met:
     13   1.1       pk  * 1. Redistributions of source code must retain the above copyright
     14   1.1       pk  *    notice, this list of conditions and the following disclaimer.
     15   1.1       pk  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.1       pk  *    notice, this list of conditions and the following disclaimer in the
     17   1.1       pk  *    documentation and/or other materials provided with the distribution.
     18   1.1       pk  * 3. All advertising materials mentioning features or use of this software
     19   1.1       pk  *    must display the following acknowledgement:
     20   1.1       pk  *        This product includes software developed by the NetBSD
     21   1.1       pk  *        Foundation, Inc. and its contributors.
     22   1.1       pk  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23   1.1       pk  *    contributors may be used to endorse or promote products derived
     24   1.1       pk  *    from this software without specific prior written permission.
     25   1.1       pk  *
     26   1.1       pk  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27   1.1       pk  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28   1.1       pk  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29   1.1       pk  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30   1.1       pk  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31   1.1       pk  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32   1.1       pk  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33   1.1       pk  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34   1.1       pk  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35   1.1       pk  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36   1.1       pk  * POSSIBILITY OF SUCH DAMAGE.
     37   1.1       pk  */
     38   1.1       pk 
     39   1.1       pk /*
     40   1.1       pk  * HME Ethernet module driver.
     41   1.1       pk  */
     42  1.25    lukem 
     43  1.25    lukem #include <sys/cdefs.h>
     44  1.27     tron __KERNEL_RCSID(0, "$NetBSD: hme.c,v 1.27 2001/11/26 06:51:12 tron Exp $");
     45   1.1       pk 
     46  1.26     tron #undef HMEDEBUG
     47   1.1       pk 
     48   1.1       pk #include "opt_inet.h"
     49   1.1       pk #include "opt_ns.h"
     50   1.1       pk #include "bpfilter.h"
     51   1.1       pk #include "rnd.h"
     52   1.1       pk 
     53   1.1       pk #include <sys/param.h>
     54   1.1       pk #include <sys/systm.h>
     55   1.5       pk #include <sys/kernel.h>
     56   1.1       pk #include <sys/mbuf.h>
     57   1.1       pk #include <sys/syslog.h>
     58   1.1       pk #include <sys/socket.h>
     59   1.1       pk #include <sys/device.h>
     60   1.1       pk #include <sys/malloc.h>
     61   1.1       pk #include <sys/ioctl.h>
     62   1.1       pk #include <sys/errno.h>
     63   1.1       pk #if NRND > 0
     64   1.1       pk #include <sys/rnd.h>
     65   1.1       pk #endif
     66   1.1       pk 
     67   1.1       pk #include <net/if.h>
     68   1.1       pk #include <net/if_dl.h>
     69   1.1       pk #include <net/if_ether.h>
     70   1.1       pk #include <net/if_media.h>
     71   1.1       pk 
     72   1.1       pk #ifdef INET
     73   1.1       pk #include <netinet/in.h>
     74   1.1       pk #include <netinet/if_inarp.h>
     75   1.1       pk #include <netinet/in_systm.h>
     76   1.1       pk #include <netinet/in_var.h>
     77   1.1       pk #include <netinet/ip.h>
     78   1.1       pk #endif
     79   1.1       pk 
     80   1.1       pk #ifdef NS
     81   1.1       pk #include <netns/ns.h>
     82   1.1       pk #include <netns/ns_if.h>
     83   1.1       pk #endif
     84   1.1       pk 
     85   1.1       pk #if NBPFILTER > 0
     86   1.1       pk #include <net/bpf.h>
     87   1.1       pk #include <net/bpfdesc.h>
     88   1.1       pk #endif
     89   1.1       pk 
     90   1.1       pk #include <dev/mii/mii.h>
     91   1.1       pk #include <dev/mii/miivar.h>
     92   1.1       pk 
     93   1.1       pk #include <machine/bus.h>
     94   1.1       pk 
     95   1.1       pk #include <dev/ic/hmereg.h>
     96   1.1       pk #include <dev/ic/hmevar.h>
     97   1.1       pk 
     98  1.26     tron #define	HME_RX_OFFSET	2
     99  1.26     tron 
    100   1.1       pk void		hme_start __P((struct ifnet *));
    101   1.1       pk void		hme_stop __P((struct hme_softc *));
    102   1.1       pk int		hme_ioctl __P((struct ifnet *, u_long, caddr_t));
    103   1.5       pk void		hme_tick __P((void *));
    104   1.1       pk void		hme_watchdog __P((struct ifnet *));
    105   1.1       pk void		hme_shutdown __P((void *));
    106   1.1       pk void		hme_init __P((struct hme_softc *));
    107   1.1       pk void		hme_meminit __P((struct hme_softc *));
    108   1.4       pk void		hme_mifinit __P((struct hme_softc *));
    109   1.1       pk void		hme_reset __P((struct hme_softc *));
    110   1.1       pk void		hme_setladrf __P((struct hme_softc *));
    111  1.26     tron int		hme_newbuf __P((struct hme_softc *, struct hme_sxd *, int));
    112  1.26     tron int		hme_encap __P((struct hme_softc *, struct mbuf *, int *));
    113   1.1       pk 
    114   1.1       pk /* MII methods & callbacks */
    115   1.1       pk static int	hme_mii_readreg __P((struct device *, int, int));
    116   1.1       pk static void	hme_mii_writereg __P((struct device *, int, int, int));
    117   1.1       pk static void	hme_mii_statchg __P((struct device *));
    118   1.1       pk 
    119   1.1       pk int		hme_mediachange __P((struct ifnet *));
    120   1.1       pk void		hme_mediastatus __P((struct ifnet *, struct ifmediareq *));
    121   1.1       pk 
    122   1.1       pk int		hme_eint __P((struct hme_softc *, u_int));
    123   1.1       pk int		hme_rint __P((struct hme_softc *));
    124   1.1       pk int		hme_tint __P((struct hme_softc *));
    125   1.1       pk 
    126   1.1       pk void
    127   1.1       pk hme_config(sc)
    128   1.1       pk 	struct hme_softc *sc;
    129   1.1       pk {
    130   1.1       pk 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    131   1.1       pk 	struct mii_data *mii = &sc->sc_mii;
    132   1.5       pk 	struct mii_softc *child;
    133  1.11       pk 	bus_dma_tag_t dmatag = sc->sc_dmatag;
    134   1.1       pk 	bus_dma_segment_t seg;
    135   1.1       pk 	bus_size_t size;
    136  1.26     tron 	int rseg, error, i;
    137   1.1       pk 
    138   1.1       pk 	/*
    139   1.1       pk 	 * HME common initialization.
    140   1.1       pk 	 *
    141   1.1       pk 	 * hme_softc fields that must be initialized by the front-end:
    142   1.1       pk 	 *
    143   1.1       pk 	 * the bus tag:
    144   1.1       pk 	 *	sc_bustag
    145   1.1       pk 	 *
    146   1.1       pk 	 * the dma bus tag:
    147   1.1       pk 	 *	sc_dmatag
    148   1.1       pk 	 *
    149   1.1       pk 	 * the bus handles:
    150   1.1       pk 	 *	sc_seb		(Shared Ethernet Block registers)
    151   1.1       pk 	 *	sc_erx		(Receiver Unit registers)
    152   1.1       pk 	 *	sc_etx		(Transmitter Unit registers)
    153   1.1       pk 	 *	sc_mac		(MAC registers)
    154   1.1       pk 	 *	sc_mif		(Managment Interface registers)
    155   1.1       pk 	 *
    156   1.1       pk 	 * the maximum bus burst size:
    157   1.1       pk 	 *	sc_burst
    158   1.1       pk 	 *
    159   1.1       pk 	 * the local Ethernet address:
    160   1.1       pk 	 *	sc_enaddr
    161   1.1       pk 	 *
    162   1.1       pk 	 */
    163   1.1       pk 
    164   1.1       pk 	/* Make sure the chip is stopped. */
    165   1.1       pk 	hme_stop(sc);
    166   1.1       pk 
    167   1.1       pk 
    168  1.26     tron 	for (i = 0; i < HME_TX_RING_SIZE; i++) {
    169  1.26     tron 		if (bus_dmamap_create(sc->sc_dmatag, MCLBYTES, 1,
    170  1.26     tron 		    MCLBYTES, 0, BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW,
    171  1.26     tron 		    &sc->sc_txd[i].sd_map) != 0) {
    172  1.26     tron 			sc->sc_txd[i].sd_map = NULL;
    173  1.26     tron 			goto fail;
    174  1.26     tron 		}
    175  1.26     tron 	}
    176  1.26     tron 	for (i = 0; i < HME_RX_RING_SIZE; i++) {
    177  1.26     tron 		if (bus_dmamap_create(sc->sc_dmatag, MCLBYTES, 1,
    178  1.26     tron 		    MCLBYTES, 0, BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW,
    179  1.26     tron 		    &sc->sc_rxd[i].sd_map) != 0) {
    180  1.26     tron 			sc->sc_rxd[i].sd_map = NULL;
    181  1.26     tron 			goto fail;
    182  1.26     tron 		}
    183  1.26     tron 	}
    184  1.26     tron 	if (bus_dmamap_create(sc->sc_dmatag, MCLBYTES, 1, MCLBYTES, 0,
    185  1.26     tron 	    BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW, &sc->sc_rxmap_spare) != 0) {
    186  1.26     tron 		sc->sc_rxmap_spare = NULL;
    187  1.26     tron 		goto fail;
    188  1.26     tron 	}
    189   1.1       pk 
    190   1.1       pk 	/*
    191   1.1       pk 	 * Allocate DMA capable memory
    192   1.1       pk 	 * Buffer descriptors must be aligned on a 2048 byte boundary;
    193   1.1       pk 	 * take this into account when calculating the size. Note that
    194   1.1       pk 	 * the maximum number of descriptors (256) occupies 2048 bytes,
    195  1.26     tron 	 * so we allocate that much regardless of the number of descriptors.
    196   1.1       pk 	 */
    197  1.26     tron 	size = (HME_XD_SIZE * HME_RX_RING_MAX) +	/* RX descriptors */
    198  1.26     tron 	    (HME_XD_SIZE * HME_TX_RING_MAX);		/* TX descriptors */
    199  1.11       pk 
    200  1.11       pk 	/* Allocate DMA buffer */
    201  1.26     tron 	if ((error = bus_dmamem_alloc(dmatag, size, 2048, 0, &seg, 1, &rseg,
    202  1.26     tron 	    BUS_DMA_NOWAIT)) != 0) {
    203   1.1       pk 		printf("%s: DMA buffer alloc error %d\n",
    204  1.26     tron 		    sc->sc_dev.dv_xname, error);
    205  1.10      mrg 		return;
    206   1.1       pk 	}
    207   1.1       pk 
    208  1.11       pk 	/* Map DMA memory in CPU addressable space */
    209  1.11       pk 	if ((error = bus_dmamem_map(dmatag, &seg, rseg, size,
    210  1.26     tron 	    &sc->sc_rb.rb_membase, BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) != 0) {
    211   1.1       pk 		printf("%s: DMA buffer map error %d\n",
    212  1.26     tron 		    sc->sc_dev.dv_xname, error);
    213  1.11       pk 		bus_dmamap_unload(dmatag, sc->sc_dmamap);
    214  1.11       pk 		bus_dmamem_free(dmatag, &seg, rseg);
    215   1.1       pk 		return;
    216   1.1       pk 	}
    217  1.13      mrg 
    218  1.13      mrg 	if ((error = bus_dmamap_create(dmatag, size, 1, size, 0,
    219  1.26     tron 	    BUS_DMA_NOWAIT, &sc->sc_dmamap)) != 0) {
    220  1.13      mrg 		printf("%s: DMA map create error %d\n",
    221  1.26     tron 		    sc->sc_dev.dv_xname, error);
    222  1.13      mrg 		return;
    223  1.13      mrg 	}
    224  1.13      mrg 
    225  1.13      mrg 	/* Load the buffer */
    226  1.13      mrg 	if ((error = bus_dmamap_load(dmatag, sc->sc_dmamap,
    227  1.17      mrg 	    sc->sc_rb.rb_membase, size, NULL,
    228  1.17      mrg 	    BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) != 0) {
    229  1.13      mrg 		printf("%s: DMA buffer map load error %d\n",
    230  1.26     tron 		    sc->sc_dev.dv_xname, error);
    231  1.13      mrg 		bus_dmamem_free(dmatag, &seg, rseg);
    232  1.13      mrg 		return;
    233  1.13      mrg 	}
    234  1.13      mrg 	sc->sc_rb.rb_dmabase = sc->sc_dmamap->dm_segs[0].ds_addr;
    235   1.1       pk 
    236  1.22  thorpej 	printf("%s: Ethernet address %s\n", sc->sc_dev.dv_xname,
    237  1.22  thorpej 	    ether_sprintf(sc->sc_enaddr));
    238   1.2       pk 
    239   1.1       pk 	/* Initialize ifnet structure. */
    240  1.21  thorpej 	strcpy(ifp->if_xname, sc->sc_dev.dv_xname);
    241   1.1       pk 	ifp->if_softc = sc;
    242   1.1       pk 	ifp->if_start = hme_start;
    243   1.1       pk 	ifp->if_ioctl = hme_ioctl;
    244   1.1       pk 	ifp->if_watchdog = hme_watchdog;
    245   1.1       pk 	ifp->if_flags =
    246   1.1       pk 	    IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS | IFF_MULTICAST;
    247  1.20  thorpej 	IFQ_SET_READY(&ifp->if_snd);
    248   1.1       pk 
    249   1.1       pk 	/* Initialize ifmedia structures and MII info */
    250   1.1       pk 	mii->mii_ifp = ifp;
    251   1.1       pk 	mii->mii_readreg = hme_mii_readreg;
    252   1.1       pk 	mii->mii_writereg = hme_mii_writereg;
    253   1.1       pk 	mii->mii_statchg = hme_mii_statchg;
    254   1.1       pk 
    255   1.1       pk 	ifmedia_init(&mii->mii_media, 0, hme_mediachange, hme_mediastatus);
    256   1.1       pk 
    257   1.4       pk 	hme_mifinit(sc);
    258   1.4       pk 
    259   1.6  thorpej 	mii_attach(&sc->sc_dev, mii, 0xffffffff,
    260  1.26     tron 	    MII_PHY_ANY, MII_OFFSET_ANY, 0);
    261   1.2       pk 
    262   1.5       pk 	child = LIST_FIRST(&mii->mii_phys);
    263   1.5       pk 	if (child == NULL) {
    264   1.1       pk 		/* No PHY attached */
    265   1.1       pk 		ifmedia_add(&sc->sc_media, IFM_ETHER|IFM_MANUAL, 0, NULL);
    266   1.1       pk 		ifmedia_set(&sc->sc_media, IFM_ETHER|IFM_MANUAL);
    267   1.1       pk 	} else {
    268   1.1       pk 		/*
    269   1.5       pk 		 * Walk along the list of attached MII devices and
    270   1.5       pk 		 * establish an `MII instance' to `phy number'
    271   1.5       pk 		 * mapping. We'll use this mapping in media change
    272   1.5       pk 		 * requests to determine which phy to use to program
    273   1.5       pk 		 * the MIF configuration register.
    274   1.5       pk 		 */
    275   1.5       pk 		for (; child != NULL; child = LIST_NEXT(child, mii_list)) {
    276   1.5       pk 			/*
    277   1.5       pk 			 * Note: we support just two PHYs: the built-in
    278   1.5       pk 			 * internal device and an external on the MII
    279   1.5       pk 			 * connector.
    280   1.5       pk 			 */
    281   1.5       pk 			if (child->mii_phy > 1 || child->mii_inst > 1) {
    282   1.5       pk 				printf("%s: cannot accomodate MII device %s"
    283  1.26     tron 				    " at phy %d, instance %d\n",
    284  1.26     tron 				    sc->sc_dev.dv_xname,
    285  1.26     tron 				    child->mii_dev.dv_xname,
    286  1.26     tron 				    child->mii_phy, child->mii_inst);
    287   1.5       pk 				continue;
    288   1.5       pk 			}
    289   1.5       pk 
    290   1.5       pk 			sc->sc_phys[child->mii_inst] = child->mii_phy;
    291   1.5       pk 		}
    292   1.5       pk 
    293   1.5       pk 		/*
    294   1.1       pk 		 * XXX - we can really do the following ONLY if the
    295   1.1       pk 		 * phy indeed has the auto negotiation capability!!
    296   1.1       pk 		 */
    297   1.1       pk 		ifmedia_set(&sc->sc_media, IFM_ETHER|IFM_AUTO);
    298   1.1       pk 	}
    299  1.27     tron 
    300  1.27     tron 	/* We can support 802.1Q VLAN-sized frames. */
    301  1.27     tron 	sc->sc_ethercom.ec_capabilities |= ETHERCAP_VLAN_MTU;
    302   1.1       pk 
    303   1.1       pk 	/* Attach the interface. */
    304   1.1       pk 	if_attach(ifp);
    305   1.1       pk 	ether_ifattach(ifp, sc->sc_enaddr);
    306   1.1       pk 
    307   1.1       pk 	sc->sc_sh = shutdownhook_establish(hme_shutdown, sc);
    308   1.1       pk 	if (sc->sc_sh == NULL)
    309   1.1       pk 		panic("hme_config: can't establish shutdownhook");
    310   1.1       pk 
    311   1.1       pk #if NRND > 0
    312   1.1       pk 	rnd_attach_source(&sc->rnd_source, sc->sc_dev.dv_xname,
    313   1.1       pk 			  RND_TYPE_NET, 0);
    314   1.1       pk #endif
    315   1.5       pk 
    316   1.9  thorpej 	callout_init(&sc->sc_tick_ch);
    317  1.26     tron 	return;
    318  1.26     tron 
    319  1.26     tron fail:
    320  1.26     tron 	if (sc->sc_rxmap_spare != NULL)
    321  1.26     tron 		bus_dmamap_destroy(sc->sc_dmatag, sc->sc_rxmap_spare);
    322  1.26     tron 	for (i = 0; i < HME_TX_RING_SIZE; i++)
    323  1.26     tron 		if (sc->sc_txd[i].sd_map != NULL)
    324  1.26     tron 			bus_dmamap_destroy(sc->sc_dmatag, sc->sc_txd[i].sd_map);
    325  1.26     tron 	for (i = 0; i < HME_RX_RING_SIZE; i++)
    326  1.26     tron 		if (sc->sc_rxd[i].sd_map != NULL)
    327  1.26     tron 			bus_dmamap_destroy(sc->sc_dmatag, sc->sc_rxd[i].sd_map);
    328   1.5       pk }
    329   1.5       pk 
    330   1.5       pk void
    331   1.5       pk hme_tick(arg)
    332   1.5       pk 	void *arg;
    333   1.5       pk {
    334   1.5       pk 	struct hme_softc *sc = arg;
    335  1.26     tron 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    336  1.26     tron 	bus_space_tag_t t = sc->sc_bustag;
    337  1.26     tron 	bus_space_handle_t mac = sc->sc_mac;
    338   1.5       pk 	int s;
    339   1.5       pk 
    340   1.5       pk 	s = splnet();
    341  1.26     tron 	/*
    342  1.26     tron 	 * Unload collision counters
    343  1.26     tron 	 */
    344  1.26     tron 	ifp->if_collisions +=
    345  1.26     tron 	    bus_space_read_4(t, mac, HME_MACI_NCCNT) +
    346  1.26     tron 	    bus_space_read_4(t, mac, HME_MACI_FCCNT) +
    347  1.26     tron 	    bus_space_read_4(t, mac, HME_MACI_EXCNT) +
    348  1.26     tron 	    bus_space_read_4(t, mac, HME_MACI_LTCNT);
    349  1.26     tron 
    350  1.26     tron 	/*
    351  1.26     tron 	 * then clear the hardware counters.
    352  1.26     tron 	 */
    353  1.26     tron 	bus_space_write_4(t, mac, HME_MACI_NCCNT, 0);
    354  1.26     tron 	bus_space_write_4(t, mac, HME_MACI_FCCNT, 0);
    355  1.26     tron 	bus_space_write_4(t, mac, HME_MACI_EXCNT, 0);
    356  1.26     tron 	bus_space_write_4(t, mac, HME_MACI_LTCNT, 0);
    357  1.26     tron 
    358   1.5       pk 	mii_tick(&sc->sc_mii);
    359   1.5       pk 	splx(s);
    360   1.5       pk 
    361   1.9  thorpej 	callout_reset(&sc->sc_tick_ch, hz, hme_tick, sc);
    362   1.1       pk }
    363   1.1       pk 
    364   1.1       pk void
    365   1.1       pk hme_reset(sc)
    366   1.1       pk 	struct hme_softc *sc;
    367   1.1       pk {
    368   1.1       pk 	int s;
    369   1.1       pk 
    370   1.1       pk 	s = splnet();
    371   1.1       pk 	hme_init(sc);
    372   1.1       pk 	splx(s);
    373   1.1       pk }
    374   1.1       pk 
    375   1.1       pk void
    376   1.1       pk hme_stop(sc)
    377   1.1       pk 	struct hme_softc *sc;
    378   1.1       pk {
    379   1.1       pk 	bus_space_tag_t t = sc->sc_bustag;
    380   1.1       pk 	bus_space_handle_t seb = sc->sc_seb;
    381   1.1       pk 	int n;
    382   1.1       pk 
    383   1.9  thorpej 	callout_stop(&sc->sc_tick_ch);
    384   1.5       pk 	mii_down(&sc->sc_mii);
    385   1.5       pk 
    386   1.1       pk 	/* Reset transmitter and receiver */
    387   1.1       pk 	bus_space_write_4(t, seb, HME_SEBI_RESET,
    388  1.26     tron 	    (HME_SEB_RESET_ETX | HME_SEB_RESET_ERX));
    389   1.1       pk 
    390   1.1       pk 	for (n = 0; n < 20; n++) {
    391   1.1       pk 		u_int32_t v = bus_space_read_4(t, seb, HME_SEBI_RESET);
    392   1.1       pk 		if ((v & (HME_SEB_RESET_ETX | HME_SEB_RESET_ERX)) == 0)
    393   1.1       pk 			return;
    394   1.1       pk 		DELAY(20);
    395   1.1       pk 	}
    396   1.1       pk 
    397  1.26     tron 	for (n = 0; n < HME_TX_RING_SIZE; n++) {
    398  1.26     tron 		if (sc->sc_txd[n].sd_loaded) {
    399  1.26     tron 			bus_dmamap_sync(sc->sc_dmatag, sc->sc_txd[n].sd_map,
    400  1.26     tron 			    0, sc->sc_txd[n].sd_map->dm_mapsize,
    401  1.26     tron 			    BUS_DMASYNC_POSTWRITE);
    402  1.26     tron 			bus_dmamap_unload(sc->sc_dmatag, sc->sc_txd[n].sd_map);
    403  1.26     tron 			sc->sc_txd[n].sd_loaded = 0;
    404  1.26     tron 		}
    405  1.26     tron 		if (sc->sc_txd[n].sd_mbuf != NULL) {
    406  1.26     tron 			m_freem(sc->sc_txd[n].sd_mbuf);
    407  1.26     tron 			sc->sc_txd[n].sd_mbuf = NULL;
    408  1.26     tron 		}
    409  1.26     tron 	}
    410  1.26     tron 
    411   1.1       pk 	printf("%s: hme_stop: reset failed\n", sc->sc_dev.dv_xname);
    412   1.1       pk }
    413   1.1       pk 
    414   1.1       pk void
    415   1.1       pk hme_meminit(sc)
    416   1.1       pk 	struct hme_softc *sc;
    417   1.1       pk {
    418   1.1       pk 	bus_addr_t dma;
    419   1.1       pk 	caddr_t p;
    420  1.26     tron 	unsigned int i;
    421   1.1       pk 	struct hme_ring *hr = &sc->sc_rb;
    422   1.1       pk 
    423   1.1       pk 	p = hr->rb_membase;
    424   1.1       pk 	dma = hr->rb_dmabase;
    425   1.1       pk 
    426   1.1       pk 	/*
    427   1.1       pk 	 * Allocate transmit descriptors
    428   1.1       pk 	 */
    429   1.1       pk 	hr->rb_txd = p;
    430   1.1       pk 	hr->rb_txddma = dma;
    431  1.26     tron 	p += HME_TX_RING_SIZE * HME_XD_SIZE;
    432  1.26     tron 	dma += HME_TX_RING_SIZE * HME_XD_SIZE;
    433   1.4       pk 	/* We have reserved descriptor space until the next 2048 byte boundary.*/
    434   1.4       pk 	dma = (bus_addr_t)roundup((u_long)dma, 2048);
    435   1.4       pk 	p = (caddr_t)roundup((u_long)p, 2048);
    436   1.1       pk 
    437   1.1       pk 	/*
    438   1.1       pk 	 * Allocate receive descriptors
    439   1.1       pk 	 */
    440   1.1       pk 	hr->rb_rxd = p;
    441   1.1       pk 	hr->rb_rxddma = dma;
    442  1.26     tron 	p += HME_RX_RING_SIZE * HME_XD_SIZE;
    443  1.26     tron 	dma += HME_RX_RING_SIZE * HME_XD_SIZE;
    444   1.4       pk 	/* Again move forward to the next 2048 byte boundary.*/
    445   1.4       pk 	dma = (bus_addr_t)roundup((u_long)dma, 2048);
    446   1.4       pk 	p = (caddr_t)roundup((u_long)p, 2048);
    447   1.1       pk 
    448   1.1       pk 	/*
    449  1.26     tron 	 * Initialize transmit descriptors
    450   1.1       pk 	 */
    451  1.26     tron 	for (i = 0; i < HME_TX_RING_SIZE; i++) {
    452  1.26     tron 		HME_XD_SETADDR(sc->sc_pci, hr->rb_txd, i, 0);
    453  1.15      eeh 		HME_XD_SETFLAGS(sc->sc_pci, hr->rb_txd, i, 0);
    454  1.26     tron 		sc->sc_txd[i].sd_mbuf = NULL;
    455   1.1       pk 	}
    456   1.1       pk 
    457   1.1       pk 	/*
    458  1.26     tron 	 * Initialize receive descriptors
    459   1.1       pk 	 */
    460  1.26     tron 	for (i = 0; i < HME_RX_RING_SIZE; i++) {
    461  1.26     tron 		if (hme_newbuf(sc, &sc->sc_rxd[i], 1)) {
    462  1.26     tron 			printf("%s: rx allocation failed\n",
    463  1.26     tron 			    sc->sc_dev.dv_xname);
    464  1.26     tron 			break;
    465  1.26     tron 		}
    466  1.26     tron 		HME_XD_SETADDR(sc->sc_pci, hr->rb_rxd, i,
    467  1.26     tron 		    sc->sc_rxd[i].sd_map->dm_segs[0].ds_addr);
    468  1.15      eeh 		HME_XD_SETFLAGS(sc->sc_pci, hr->rb_rxd, i,
    469  1.26     tron 		    HME_XD_OWN | HME_XD_ENCODE_RSIZE(HME_RX_PKTSIZE));
    470   1.1       pk 	}
    471   1.1       pk 
    472  1.26     tron 	sc->sc_tx_prod = sc->sc_tx_cons = sc->sc_tx_cnt = 0;
    473  1.26     tron 	sc->sc_last_rd = 0;
    474   1.1       pk }
    475   1.1       pk 
    476   1.1       pk /*
    477   1.1       pk  * Initialization of interface; set up initialization block
    478   1.1       pk  * and transmit/receive descriptor rings.
    479   1.1       pk  */
    480   1.1       pk void
    481   1.1       pk hme_init(sc)
    482   1.1       pk 	struct hme_softc *sc;
    483   1.1       pk {
    484   1.1       pk 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    485   1.1       pk 	bus_space_tag_t t = sc->sc_bustag;
    486   1.1       pk 	bus_space_handle_t seb = sc->sc_seb;
    487   1.1       pk 	bus_space_handle_t etx = sc->sc_etx;
    488   1.1       pk 	bus_space_handle_t erx = sc->sc_erx;
    489   1.1       pk 	bus_space_handle_t mac = sc->sc_mac;
    490   1.1       pk 	bus_space_handle_t mif = sc->sc_mif;
    491   1.1       pk 	u_int8_t *ea;
    492   1.1       pk 	u_int32_t v;
    493   1.1       pk 
    494   1.1       pk 	/*
    495   1.1       pk 	 * Initialization sequence. The numbered steps below correspond
    496   1.1       pk 	 * to the sequence outlined in section 6.3.5.1 in the Ethernet
    497   1.1       pk 	 * Channel Engine manual (part of the PCIO manual).
    498   1.1       pk 	 * See also the STP2002-STQ document from Sun Microsystems.
    499   1.1       pk 	 */
    500   1.1       pk 
    501   1.1       pk 	/* step 1 & 2. Reset the Ethernet Channel */
    502   1.1       pk 	hme_stop(sc);
    503   1.1       pk 
    504   1.4       pk 	/* Re-initialize the MIF */
    505   1.4       pk 	hme_mifinit(sc);
    506   1.4       pk 
    507   1.1       pk 	/* Call MI reset function if any */
    508   1.1       pk 	if (sc->sc_hwreset)
    509   1.1       pk 		(*sc->sc_hwreset)(sc);
    510   1.1       pk 
    511   1.1       pk #if 0
    512   1.1       pk 	/* Mask all MIF interrupts, just in case */
    513   1.1       pk 	bus_space_write_4(t, mif, HME_MIFI_IMASK, 0xffff);
    514   1.1       pk #endif
    515   1.1       pk 
    516   1.1       pk 	/* step 3. Setup data structures in host memory */
    517   1.1       pk 	hme_meminit(sc);
    518   1.1       pk 
    519   1.1       pk 	/* step 4. TX MAC registers & counters */
    520   1.1       pk 	bus_space_write_4(t, mac, HME_MACI_NCCNT, 0);
    521   1.1       pk 	bus_space_write_4(t, mac, HME_MACI_FCCNT, 0);
    522   1.1       pk 	bus_space_write_4(t, mac, HME_MACI_EXCNT, 0);
    523   1.1       pk 	bus_space_write_4(t, mac, HME_MACI_LTCNT, 0);
    524  1.26     tron 	bus_space_write_4(t, mac, HME_MACI_TXSIZE, HME_MTU);
    525   1.1       pk 
    526   1.1       pk 	/* Load station MAC address */
    527   1.1       pk 	ea = sc->sc_enaddr;
    528   1.1       pk 	bus_space_write_4(t, mac, HME_MACI_MACADDR0, (ea[0] << 8) | ea[1]);
    529   1.1       pk 	bus_space_write_4(t, mac, HME_MACI_MACADDR1, (ea[2] << 8) | ea[3]);
    530   1.1       pk 	bus_space_write_4(t, mac, HME_MACI_MACADDR2, (ea[4] << 8) | ea[5]);
    531   1.1       pk 
    532   1.1       pk 	/*
    533   1.1       pk 	 * Init seed for backoff
    534   1.1       pk 	 * (source suggested by manual: low 10 bits of MAC address)
    535   1.1       pk 	 */
    536   1.1       pk 	v = ((ea[4] << 8) | ea[5]) & 0x3fff;
    537   1.1       pk 	bus_space_write_4(t, mac, HME_MACI_RANDSEED, v);
    538   1.1       pk 
    539   1.1       pk 
    540   1.1       pk 	/* Note: Accepting power-on default for other MAC registers here.. */
    541   1.1       pk 
    542   1.1       pk 
    543   1.1       pk 	/* step 5. RX MAC registers & counters */
    544   1.1       pk 	hme_setladrf(sc);
    545   1.1       pk 
    546   1.1       pk 	/* step 6 & 7. Program Descriptor Ring Base Addresses */
    547   1.1       pk 	bus_space_write_4(t, etx, HME_ETXI_RING, sc->sc_rb.rb_txddma);
    548  1.26     tron 	bus_space_write_4(t, etx, HME_ETXI_RSIZE, HME_TX_RING_SIZE);
    549   1.1       pk 
    550   1.1       pk 	bus_space_write_4(t, erx, HME_ERXI_RING, sc->sc_rb.rb_rxddma);
    551  1.26     tron 	bus_space_write_4(t, mac, HME_MACI_RXSIZE, HME_MTU);
    552   1.1       pk 
    553   1.1       pk 	/* step 8. Global Configuration & Interrupt Mask */
    554   1.1       pk 	bus_space_write_4(t, seb, HME_SEBI_IMASK,
    555  1.26     tron 	    ~(HME_SEB_STAT_HOSTTOTX | HME_SEB_STAT_RXTOHOST |
    556  1.26     tron 	      HME_SEB_STAT_TXALL | HME_SEB_STAT_TXPERR |
    557  1.26     tron 	      HME_SEB_STAT_RCNTEXP | HME_SEB_STAT_ALL_ERRORS));
    558   1.1       pk 
    559   1.1       pk 	switch (sc->sc_burst) {
    560   1.1       pk 	default:
    561   1.1       pk 		v = 0;
    562   1.1       pk 		break;
    563   1.1       pk 	case 16:
    564   1.1       pk 		v = HME_SEB_CFG_BURST16;
    565   1.1       pk 		break;
    566   1.1       pk 	case 32:
    567   1.1       pk 		v = HME_SEB_CFG_BURST32;
    568   1.1       pk 		break;
    569   1.1       pk 	case 64:
    570   1.1       pk 		v = HME_SEB_CFG_BURST64;
    571   1.1       pk 		break;
    572   1.1       pk 	}
    573   1.1       pk 	bus_space_write_4(t, seb, HME_SEBI_CFG, v);
    574   1.1       pk 
    575   1.1       pk 	/* step 9. ETX Configuration: use mostly default values */
    576   1.1       pk 
    577   1.1       pk 	/* Enable DMA */
    578   1.2       pk 	v = bus_space_read_4(t, etx, HME_ETXI_CFG);
    579   1.1       pk 	v |= HME_ETX_CFG_DMAENABLE;
    580   1.2       pk 	bus_space_write_4(t, etx, HME_ETXI_CFG, v);
    581   1.1       pk 
    582   1.3       pk 	/* Transmit Descriptor ring size: in increments of 16 */
    583  1.26     tron 	bus_space_write_4(t, etx, HME_ETXI_RSIZE, HME_TX_RING_SIZE / 16 - 1);
    584   1.1       pk 
    585   1.3       pk 	/* step 10. ERX Configuration */
    586   1.2       pk 	v = bus_space_read_4(t, erx, HME_ERXI_CFG);
    587  1.26     tron 	v &= ~HME_ERX_CFG_RINGSIZE256;
    588  1.26     tron #if HME_RX_RING_SIZE == 32
    589  1.26     tron 	v |= HME_ERX_CFG_RINGSIZE32;
    590  1.26     tron #elif HME_RX_RING_SIZE == 64
    591  1.26     tron 	v |= HME_ERX_CFG_RINGSIZE64;
    592  1.26     tron #elif HME_RX_RING_SIZE == 128
    593  1.26     tron 	v |= HME_ERX_CFG_RINGSIZE128;
    594  1.26     tron #elif HME_RX_RING_SIZE == 256
    595  1.26     tron 	v |= HME_ERX_CFG_RINGSIZE256;
    596  1.26     tron #else
    597  1.26     tron # error	"RX ring size must be 32, 64, 128, or 256"
    598  1.26     tron #endif
    599   1.3       pk 	/* Enable DMA */
    600  1.26     tron 	v |= HME_ERX_CFG_DMAENABLE | (HME_RX_OFFSET << 3);
    601   1.2       pk 	bus_space_write_4(t, erx, HME_ERXI_CFG, v);
    602   1.1       pk 
    603   1.1       pk 	/* step 11. XIF Configuration */
    604   1.1       pk 	v = bus_space_read_4(t, mac, HME_MACI_XIF);
    605   1.1       pk 	v |= HME_MAC_XIF_OE;
    606   1.4       pk 	/* If an external transceiver is connected, enable its MII drivers */
    607   1.2       pk 	if ((bus_space_read_4(t, mif, HME_MIFI_CFG) & HME_MIF_CFG_MDI1) != 0)
    608   1.4       pk 		v |= HME_MAC_XIF_MIIENABLE;
    609   1.1       pk 	bus_space_write_4(t, mac, HME_MACI_XIF, v);
    610   1.1       pk 
    611   1.2       pk 
    612   1.1       pk 	/* step 12. RX_MAC Configuration Register */
    613   1.1       pk 	v = bus_space_read_4(t, mac, HME_MACI_RXCFG);
    614   1.1       pk 	v |= HME_MAC_RXCFG_ENABLE;
    615   1.1       pk 	bus_space_write_4(t, mac, HME_MACI_RXCFG, v);
    616   1.1       pk 
    617   1.1       pk 	/* step 13. TX_MAC Configuration Register */
    618   1.1       pk 	v = bus_space_read_4(t, mac, HME_MACI_TXCFG);
    619   1.2       pk 	v |= (HME_MAC_TXCFG_ENABLE | HME_MAC_TXCFG_DGIVEUP);
    620   1.1       pk 	bus_space_write_4(t, mac, HME_MACI_TXCFG, v);
    621   1.1       pk 
    622   1.1       pk 	/* step 14. Issue Transmit Pending command */
    623   1.1       pk 
    624   1.1       pk 	/* Call MI initialization function if any */
    625   1.1       pk 	if (sc->sc_hwinit)
    626   1.1       pk 		(*sc->sc_hwinit)(sc);
    627   1.9  thorpej 
    628   1.9  thorpej 	/* Start the one second timer. */
    629   1.9  thorpej 	callout_reset(&sc->sc_tick_ch, hz, hme_tick, sc);
    630   1.1       pk 
    631   1.1       pk 	ifp->if_flags |= IFF_RUNNING;
    632   1.1       pk 	ifp->if_flags &= ~IFF_OACTIVE;
    633   1.1       pk 	ifp->if_timer = 0;
    634   1.1       pk 	hme_start(ifp);
    635   1.1       pk }
    636   1.1       pk 
    637   1.1       pk void
    638   1.1       pk hme_start(ifp)
    639   1.1       pk 	struct ifnet *ifp;
    640   1.1       pk {
    641   1.1       pk 	struct hme_softc *sc = (struct hme_softc *)ifp->if_softc;
    642   1.1       pk 	struct mbuf *m;
    643  1.26     tron 	int bix;
    644   1.1       pk 
    645   1.1       pk 	if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
    646   1.1       pk 		return;
    647   1.1       pk 
    648  1.26     tron 	bix = sc->sc_tx_prod;
    649  1.26     tron 	while (sc->sc_txd[bix].sd_mbuf == NULL) {
    650  1.26     tron 		IFQ_POLL(&ifp->if_snd, m);
    651  1.26     tron 		if (m == NULL)
    652   1.1       pk 			break;
    653   1.1       pk 
    654   1.1       pk #if NBPFILTER > 0
    655   1.1       pk 		/*
    656   1.1       pk 		 * If BPF is listening on this interface, let it see the
    657   1.1       pk 		 * packet before we commit it to the wire.
    658   1.1       pk 		 */
    659   1.1       pk 		if (ifp->if_bpf)
    660   1.1       pk 			bpf_mtap(ifp->if_bpf, m);
    661   1.1       pk #endif
    662   1.1       pk 
    663  1.26     tron 		if (hme_encap(sc, m, &bix)) {
    664  1.26     tron 			ifp->if_flags |= IFF_OACTIVE;
    665  1.26     tron 			break;
    666  1.26     tron 		}
    667   1.1       pk 
    668  1.26     tron 		IFQ_DEQUEUE(&ifp->if_snd, m);
    669   1.1       pk 
    670   1.1       pk 		bus_space_write_4(sc->sc_bustag, sc->sc_etx, HME_ETXI_PENDING,
    671  1.26     tron 		    HME_ETX_TP_DMAWAKEUP);
    672   1.1       pk 	}
    673   1.1       pk 
    674  1.26     tron 	sc->sc_tx_prod = bix;
    675  1.26     tron 	ifp->if_timer = 5;
    676   1.1       pk }
    677   1.1       pk 
    678   1.1       pk /*
    679   1.1       pk  * Transmit interrupt.
    680   1.1       pk  */
    681   1.1       pk int
    682   1.1       pk hme_tint(sc)
    683   1.1       pk 	struct hme_softc *sc;
    684   1.1       pk {
    685   1.1       pk 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    686   1.1       pk 	unsigned int ri, txflags;
    687  1.26     tron 	struct hme_sxd *sd;
    688   1.1       pk 
    689   1.1       pk 	/* Fetch current position in the transmit ring */
    690  1.26     tron 	ri = sc->sc_tx_cons;
    691  1.26     tron 	sd = &sc->sc_txd[ri];
    692   1.1       pk 
    693   1.1       pk 	for (;;) {
    694  1.26     tron 		if (sc->sc_tx_cnt <= 0)
    695   1.1       pk 			break;
    696   1.1       pk 
    697  1.15      eeh 		txflags = HME_XD_GETFLAGS(sc->sc_pci, sc->sc_rb.rb_txd, ri);
    698   1.1       pk 
    699   1.1       pk 		if (txflags & HME_XD_OWN)
    700   1.1       pk 			break;
    701   1.1       pk 
    702   1.1       pk 		ifp->if_flags &= ~IFF_OACTIVE;
    703  1.26     tron 		if (txflags & HME_XD_EOP)
    704  1.26     tron 			ifp->if_opackets++;
    705   1.1       pk 
    706  1.26     tron 		bus_dmamap_sync(sc->sc_dmatag, sd->sd_map,
    707  1.26     tron 		    0, sd->sd_map->dm_mapsize, BUS_DMASYNC_POSTWRITE);
    708  1.26     tron 		bus_dmamap_unload(sc->sc_dmatag, sd->sd_map);
    709  1.26     tron 		sd->sd_loaded = 0;
    710  1.26     tron 
    711  1.26     tron 		if (sd->sd_mbuf != NULL) {
    712  1.26     tron 			m_freem(sd->sd_mbuf);
    713  1.26     tron 			sd->sd_mbuf = NULL;
    714  1.26     tron 		}
    715  1.26     tron 
    716  1.26     tron 		if (++ri == HME_TX_RING_SIZE) {
    717   1.1       pk 			ri = 0;
    718  1.26     tron 			sd = sc->sc_txd;
    719  1.26     tron 		} else
    720  1.26     tron 			sd++;
    721   1.1       pk 
    722  1.26     tron 		--sc->sc_tx_cnt;
    723   1.1       pk 	}
    724   1.1       pk 
    725   1.3       pk 	/* Update ring */
    726  1.26     tron 	sc->sc_tx_cons = ri;
    727   1.1       pk 
    728   1.1       pk 	hme_start(ifp);
    729   1.1       pk 
    730  1.26     tron 	if (sc->sc_tx_cnt == 0)
    731   1.1       pk 		ifp->if_timer = 0;
    732   1.1       pk 
    733   1.1       pk 	return (1);
    734   1.1       pk }
    735   1.1       pk 
    736   1.1       pk /*
    737   1.1       pk  * Receive interrupt.
    738   1.1       pk  */
    739   1.1       pk int
    740   1.1       pk hme_rint(sc)
    741   1.1       pk 	struct hme_softc *sc;
    742   1.1       pk {
    743  1.26     tron 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    744  1.26     tron 	struct mbuf *m;
    745  1.26     tron 	struct hme_sxd *sd;
    746   1.1       pk 	unsigned int ri, len;
    747   1.1       pk 	u_int32_t flags;
    748   1.1       pk 
    749  1.26     tron 	ri = sc->sc_last_rd;
    750  1.26     tron 	sd = &sc->sc_rxd[ri];
    751   1.1       pk 
    752   1.1       pk 	/*
    753   1.1       pk 	 * Process all buffers with valid data.
    754   1.1       pk 	 */
    755   1.1       pk 	for (;;) {
    756  1.26     tron 		flags = HME_XD_GETFLAGS(sc->sc_pci, sc->sc_rb.rb_rxd, ri);
    757   1.1       pk 		if (flags & HME_XD_OWN)
    758   1.1       pk 			break;
    759   1.1       pk 
    760   1.4       pk 		if (flags & HME_XD_OFL) {
    761   1.4       pk 			printf("%s: buffer overflow, ri=%d; flags=0x%x\n",
    762  1.26     tron 			    sc->sc_dev.dv_xname, ri, flags);
    763  1.26     tron 			goto again;
    764   1.4       pk 		}
    765   1.1       pk 
    766  1.26     tron 		m = sd->sd_mbuf;
    767  1.26     tron 		len = HME_XD_DECODE_RSIZE(flags);
    768  1.26     tron 		m->m_pkthdr.len = m->m_len = len;
    769   1.1       pk 
    770  1.26     tron 		if (hme_newbuf(sc, sd, 0)) {
    771  1.26     tron 			/*
    772  1.26     tron 			 * Allocation of new mbuf cluster failed, leave the
    773  1.26     tron 			 * old one in place and keep going.
    774  1.26     tron 			 */
    775  1.26     tron 			ifp->if_ierrors++;
    776  1.26     tron 			goto again;
    777  1.26     tron 		}
    778  1.26     tron 
    779  1.26     tron 		ifp->if_ipackets++;
    780  1.26     tron 
    781  1.26     tron #if NBPFILTER > 0
    782  1.26     tron 		if (ifp->if_bpf) {
    783  1.26     tron 			m->m_pkthdr.len = m->m_len = len;
    784  1.26     tron 			bpf_mtap(ifp->if_bpf, m);
    785  1.26     tron 		}
    786  1.26     tron #endif
    787  1.26     tron 
    788  1.26     tron 		/* Pass the packet up. */
    789  1.26     tron 		(*ifp->if_input)(ifp, m);
    790  1.26     tron 
    791  1.26     tron again:
    792  1.26     tron 		HME_XD_SETADDR(sc->sc_pci, sc->sc_rb.rb_rxd, ri,
    793  1.26     tron 		    sd->sd_map->dm_segs[0].ds_addr);
    794  1.26     tron 		HME_XD_SETFLAGS(sc->sc_pci, sc->sc_rb.rb_rxd, ri,
    795  1.26     tron 		    HME_XD_OWN | HME_XD_ENCODE_RSIZE(HME_RX_PKTSIZE));
    796  1.26     tron 
    797  1.26     tron 		if (++ri == HME_RX_RING_SIZE) {
    798   1.1       pk 			ri = 0;
    799  1.26     tron 			sd = sc->sc_rxd;
    800  1.26     tron 		} else
    801  1.26     tron 			sd++;
    802   1.1       pk 	}
    803   1.1       pk 
    804  1.26     tron 	sc->sc_last_rd = ri;
    805   1.1       pk 	return (1);
    806   1.1       pk }
    807   1.1       pk 
    808   1.1       pk int
    809   1.1       pk hme_eint(sc, status)
    810   1.1       pk 	struct hme_softc *sc;
    811   1.1       pk 	u_int status;
    812   1.1       pk {
    813   1.1       pk 	char bits[128];
    814   1.1       pk 
    815   1.1       pk 	if ((status & HME_SEB_STAT_MIFIRQ) != 0) {
    816   1.1       pk 		printf("%s: XXXlink status changed\n", sc->sc_dev.dv_xname);
    817   1.1       pk 		return (1);
    818   1.1       pk 	}
    819   1.1       pk 
    820   1.1       pk 	printf("%s: status=%s\n", sc->sc_dev.dv_xname,
    821  1.26     tron 	    bitmask_snprintf(status, HME_SEB_STAT_BITS, bits, sizeof(bits)));
    822   1.1       pk 	return (1);
    823   1.1       pk }
    824   1.1       pk 
    825   1.1       pk int
    826   1.1       pk hme_intr(v)
    827   1.1       pk 	void *v;
    828   1.1       pk {
    829   1.1       pk 	struct hme_softc *sc = (struct hme_softc *)v;
    830   1.1       pk 	bus_space_tag_t t = sc->sc_bustag;
    831   1.1       pk 	bus_space_handle_t seb = sc->sc_seb;
    832   1.1       pk 	u_int32_t status;
    833   1.1       pk 	int r = 0;
    834   1.1       pk 
    835   1.1       pk 	status = bus_space_read_4(t, seb, HME_SEBI_STAT);
    836   1.1       pk 
    837   1.1       pk 	if ((status & HME_SEB_STAT_ALL_ERRORS) != 0)
    838   1.1       pk 		r |= hme_eint(sc, status);
    839   1.1       pk 
    840   1.1       pk 	if ((status & (HME_SEB_STAT_TXALL | HME_SEB_STAT_HOSTTOTX)) != 0)
    841   1.1       pk 		r |= hme_tint(sc);
    842   1.1       pk 
    843   1.1       pk 	if ((status & HME_SEB_STAT_RXTOHOST) != 0)
    844   1.1       pk 		r |= hme_rint(sc);
    845   1.1       pk 
    846   1.1       pk 	return (r);
    847   1.1       pk }
    848   1.1       pk 
    849   1.1       pk 
    850   1.1       pk void
    851   1.1       pk hme_watchdog(ifp)
    852   1.1       pk 	struct ifnet *ifp;
    853   1.1       pk {
    854   1.1       pk 	struct hme_softc *sc = ifp->if_softc;
    855   1.1       pk 
    856   1.1       pk 	log(LOG_ERR, "%s: device timeout\n", sc->sc_dev.dv_xname);
    857   1.1       pk 	++ifp->if_oerrors;
    858   1.1       pk 
    859   1.1       pk 	hme_reset(sc);
    860   1.4       pk }
    861   1.4       pk 
    862   1.4       pk /*
    863   1.4       pk  * Initialize the MII Management Interface
    864   1.4       pk  */
    865   1.4       pk void
    866   1.4       pk hme_mifinit(sc)
    867   1.4       pk 	struct hme_softc *sc;
    868   1.4       pk {
    869   1.4       pk 	bus_space_tag_t t = sc->sc_bustag;
    870   1.4       pk 	bus_space_handle_t mif = sc->sc_mif;
    871   1.4       pk 	u_int32_t v;
    872   1.4       pk 
    873   1.4       pk 	/* Configure the MIF in frame mode */
    874   1.4       pk 	v = bus_space_read_4(t, mif, HME_MIFI_CFG);
    875   1.4       pk 	v &= ~HME_MIF_CFG_BBMODE;
    876   1.4       pk 	bus_space_write_4(t, mif, HME_MIFI_CFG, v);
    877   1.1       pk }
    878   1.1       pk 
    879   1.1       pk /*
    880   1.1       pk  * MII interface
    881   1.1       pk  */
    882   1.1       pk static int
    883   1.1       pk hme_mii_readreg(self, phy, reg)
    884   1.1       pk 	struct device *self;
    885   1.1       pk 	int phy, reg;
    886   1.1       pk {
    887   1.1       pk 	struct hme_softc *sc = (void *)self;
    888   1.1       pk 	bus_space_tag_t t = sc->sc_bustag;
    889   1.1       pk 	bus_space_handle_t mif = sc->sc_mif;
    890   1.1       pk 	int n;
    891   1.1       pk 	u_int32_t v;
    892   1.1       pk 
    893   1.5       pk 	/* Select the desired PHY in the MIF configuration register */
    894   1.5       pk 	v = bus_space_read_4(t, mif, HME_MIFI_CFG);
    895   1.5       pk 	/* Clear PHY select bit */
    896   1.5       pk 	v &= ~HME_MIF_CFG_PHY;
    897   1.5       pk 	if (phy == HME_PHYAD_EXTERNAL)
    898   1.5       pk 		/* Set PHY select bit to get at external device */
    899   1.5       pk 		v |= HME_MIF_CFG_PHY;
    900   1.5       pk 	bus_space_write_4(t, mif, HME_MIFI_CFG, v);
    901   1.5       pk 
    902   1.1       pk 	/* Construct the frame command */
    903   1.1       pk 	v = (MII_COMMAND_START << HME_MIF_FO_ST_SHIFT) |
    904   1.1       pk 	    HME_MIF_FO_TAMSB |
    905   1.1       pk 	    (MII_COMMAND_READ << HME_MIF_FO_OPC_SHIFT) |
    906   1.1       pk 	    (phy << HME_MIF_FO_PHYAD_SHIFT) |
    907   1.1       pk 	    (reg << HME_MIF_FO_REGAD_SHIFT);
    908   1.1       pk 
    909   1.1       pk 	bus_space_write_4(t, mif, HME_MIFI_FO, v);
    910   1.1       pk 	for (n = 0; n < 100; n++) {
    911   1.2       pk 		DELAY(1);
    912   1.1       pk 		v = bus_space_read_4(t, mif, HME_MIFI_FO);
    913   1.1       pk 		if (v & HME_MIF_FO_TALSB)
    914   1.1       pk 			return (v & HME_MIF_FO_DATA);
    915   1.1       pk 	}
    916   1.1       pk 
    917   1.1       pk 	printf("%s: mii_read timeout\n", sc->sc_dev.dv_xname);
    918   1.1       pk 	return (0);
    919   1.1       pk }
    920   1.1       pk 
    921   1.1       pk static void
    922   1.1       pk hme_mii_writereg(self, phy, reg, val)
    923   1.1       pk 	struct device *self;
    924   1.1       pk 	int phy, reg, val;
    925   1.1       pk {
    926   1.1       pk 	struct hme_softc *sc = (void *)self;
    927   1.1       pk 	bus_space_tag_t t = sc->sc_bustag;
    928   1.1       pk 	bus_space_handle_t mif = sc->sc_mif;
    929   1.1       pk 	int n;
    930   1.1       pk 	u_int32_t v;
    931   1.1       pk 
    932   1.5       pk 	/* Select the desired PHY in the MIF configuration register */
    933   1.5       pk 	v = bus_space_read_4(t, mif, HME_MIFI_CFG);
    934   1.5       pk 	/* Clear PHY select bit */
    935   1.5       pk 	v &= ~HME_MIF_CFG_PHY;
    936   1.5       pk 	if (phy == HME_PHYAD_EXTERNAL)
    937   1.5       pk 		/* Set PHY select bit to get at external device */
    938   1.5       pk 		v |= HME_MIF_CFG_PHY;
    939   1.5       pk 	bus_space_write_4(t, mif, HME_MIFI_CFG, v);
    940   1.5       pk 
    941   1.1       pk 	/* Construct the frame command */
    942   1.1       pk 	v = (MII_COMMAND_START << HME_MIF_FO_ST_SHIFT)	|
    943   1.1       pk 	    HME_MIF_FO_TAMSB				|
    944   1.1       pk 	    (MII_COMMAND_WRITE << HME_MIF_FO_OPC_SHIFT)	|
    945   1.1       pk 	    (phy << HME_MIF_FO_PHYAD_SHIFT)		|
    946   1.1       pk 	    (reg << HME_MIF_FO_REGAD_SHIFT)		|
    947   1.1       pk 	    (val & HME_MIF_FO_DATA);
    948   1.1       pk 
    949   1.1       pk 	bus_space_write_4(t, mif, HME_MIFI_FO, v);
    950   1.1       pk 	for (n = 0; n < 100; n++) {
    951   1.2       pk 		DELAY(1);
    952   1.1       pk 		v = bus_space_read_4(t, mif, HME_MIFI_FO);
    953   1.1       pk 		if (v & HME_MIF_FO_TALSB)
    954   1.1       pk 			return;
    955   1.1       pk 	}
    956   1.1       pk 
    957   1.2       pk 	printf("%s: mii_write timeout\n", sc->sc_dev.dv_xname);
    958   1.1       pk }
    959   1.1       pk 
    960   1.1       pk static void
    961   1.1       pk hme_mii_statchg(dev)
    962   1.1       pk 	struct device *dev;
    963   1.1       pk {
    964   1.3       pk 	struct hme_softc *sc = (void *)dev;
    965   1.5       pk 	int instance = IFM_INST(sc->sc_mii.mii_media.ifm_cur->ifm_media);
    966   1.5       pk 	int phy = sc->sc_phys[instance];
    967   1.1       pk 	bus_space_tag_t t = sc->sc_bustag;
    968   1.5       pk 	bus_space_handle_t mif = sc->sc_mif;
    969   1.1       pk 	bus_space_handle_t mac = sc->sc_mac;
    970   1.1       pk 	u_int32_t v;
    971   1.1       pk 
    972   1.5       pk #ifdef HMEDEBUG
    973   1.5       pk 	if (sc->sc_debug)
    974   1.5       pk 		printf("hme_mii_statchg: status change: phy = %d\n", phy);
    975   1.5       pk #endif
    976   1.1       pk 
    977   1.5       pk 	/* Select the current PHY in the MIF configuration register */
    978   1.5       pk 	v = bus_space_read_4(t, mif, HME_MIFI_CFG);
    979   1.5       pk 	v &= ~HME_MIF_CFG_PHY;
    980   1.5       pk 	if (phy == HME_PHYAD_EXTERNAL)
    981   1.5       pk 		v |= HME_MIF_CFG_PHY;
    982   1.5       pk 	bus_space_write_4(t, mif, HME_MIFI_CFG, v);
    983   1.1       pk 
    984   1.5       pk 	/* Set the MAC Full Duplex bit appropriately */
    985   1.1       pk 	v = bus_space_read_4(t, mac, HME_MACI_TXCFG);
    986   1.1       pk 	if ((IFM_OPTIONS(sc->sc_mii.mii_media_active) & IFM_FDX) != 0)
    987   1.1       pk 		v |= HME_MAC_TXCFG_FULLDPLX;
    988   1.1       pk 	else
    989   1.1       pk 		v &= ~HME_MAC_TXCFG_FULLDPLX;
    990   1.1       pk 	bus_space_write_4(t, mac, HME_MACI_TXCFG, v);
    991   1.1       pk 
    992   1.5       pk 	/* If an external transceiver is selected, enable its MII drivers */
    993   1.5       pk 	v = bus_space_read_4(t, mac, HME_MACI_XIF);
    994   1.5       pk 	v &= ~HME_MAC_XIF_MIIENABLE;
    995   1.5       pk 	if (phy == HME_PHYAD_EXTERNAL)
    996   1.5       pk 		v |= HME_MAC_XIF_MIIENABLE;
    997   1.5       pk 	bus_space_write_4(t, mac, HME_MACI_XIF, v);
    998   1.5       pk }
    999   1.5       pk 
   1000   1.5       pk int
   1001   1.5       pk hme_mediachange(ifp)
   1002   1.5       pk 	struct ifnet *ifp;
   1003   1.5       pk {
   1004   1.5       pk 	struct hme_softc *sc = ifp->if_softc;
   1005   1.5       pk 
   1006   1.5       pk 	if (IFM_TYPE(sc->sc_media.ifm_media) != IFM_ETHER)
   1007   1.5       pk 		return (EINVAL);
   1008   1.5       pk 
   1009   1.5       pk 	return (mii_mediachg(&sc->sc_mii));
   1010   1.1       pk }
   1011   1.1       pk 
   1012   1.1       pk void
   1013   1.1       pk hme_mediastatus(ifp, ifmr)
   1014   1.1       pk 	struct ifnet *ifp;
   1015   1.1       pk 	struct ifmediareq *ifmr;
   1016   1.1       pk {
   1017   1.1       pk 	struct hme_softc *sc = ifp->if_softc;
   1018   1.1       pk 
   1019   1.1       pk 	if ((ifp->if_flags & IFF_UP) == 0)
   1020   1.1       pk 		return;
   1021   1.1       pk 
   1022   1.1       pk 	mii_pollstat(&sc->sc_mii);
   1023   1.1       pk 	ifmr->ifm_active = sc->sc_mii.mii_media_active;
   1024   1.1       pk 	ifmr->ifm_status = sc->sc_mii.mii_media_status;
   1025   1.1       pk }
   1026   1.1       pk 
   1027   1.1       pk /*
   1028   1.1       pk  * Process an ioctl request.
   1029   1.1       pk  */
   1030   1.1       pk int
   1031   1.1       pk hme_ioctl(ifp, cmd, data)
   1032   1.1       pk 	struct ifnet *ifp;
   1033   1.1       pk 	u_long cmd;
   1034   1.1       pk 	caddr_t data;
   1035   1.1       pk {
   1036   1.1       pk 	struct hme_softc *sc = ifp->if_softc;
   1037   1.1       pk 	struct ifaddr *ifa = (struct ifaddr *)data;
   1038   1.1       pk 	struct ifreq *ifr = (struct ifreq *)data;
   1039   1.1       pk 	int s, error = 0;
   1040   1.1       pk 
   1041   1.1       pk 	s = splnet();
   1042   1.1       pk 
   1043   1.1       pk 	switch (cmd) {
   1044   1.1       pk 
   1045   1.1       pk 	case SIOCSIFADDR:
   1046   1.1       pk 		ifp->if_flags |= IFF_UP;
   1047   1.1       pk 
   1048   1.1       pk 		switch (ifa->ifa_addr->sa_family) {
   1049   1.1       pk #ifdef INET
   1050   1.1       pk 		case AF_INET:
   1051   1.1       pk 			hme_init(sc);
   1052   1.1       pk 			arp_ifinit(ifp, ifa);
   1053   1.1       pk 			break;
   1054   1.1       pk #endif
   1055   1.1       pk #ifdef NS
   1056   1.1       pk 		case AF_NS:
   1057   1.1       pk 		    {
   1058   1.1       pk 			struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
   1059   1.1       pk 
   1060   1.1       pk 			if (ns_nullhost(*ina))
   1061   1.1       pk 				ina->x_host =
   1062   1.1       pk 				    *(union ns_host *)LLADDR(ifp->if_sadl);
   1063   1.1       pk 			else {
   1064  1.21  thorpej 				memcpy(LLADDR(ifp->if_sadl),
   1065  1.21  thorpej 				    ina->x_host.c_host, sizeof(sc->sc_enaddr));
   1066   1.1       pk 			}
   1067   1.1       pk 			/* Set new address. */
   1068   1.1       pk 			hme_init(sc);
   1069   1.1       pk 			break;
   1070   1.1       pk 		    }
   1071   1.1       pk #endif
   1072   1.1       pk 		default:
   1073   1.1       pk 			hme_init(sc);
   1074   1.1       pk 			break;
   1075   1.1       pk 		}
   1076   1.1       pk 		break;
   1077   1.1       pk 
   1078   1.1       pk 	case SIOCSIFFLAGS:
   1079   1.1       pk 		if ((ifp->if_flags & IFF_UP) == 0 &&
   1080   1.1       pk 		    (ifp->if_flags & IFF_RUNNING) != 0) {
   1081   1.1       pk 			/*
   1082   1.1       pk 			 * If interface is marked down and it is running, then
   1083   1.1       pk 			 * stop it.
   1084   1.1       pk 			 */
   1085   1.1       pk 			hme_stop(sc);
   1086   1.1       pk 			ifp->if_flags &= ~IFF_RUNNING;
   1087   1.1       pk 		} else if ((ifp->if_flags & IFF_UP) != 0 &&
   1088   1.1       pk 		    	   (ifp->if_flags & IFF_RUNNING) == 0) {
   1089   1.1       pk 			/*
   1090   1.1       pk 			 * If interface is marked up and it is stopped, then
   1091   1.1       pk 			 * start it.
   1092   1.1       pk 			 */
   1093   1.1       pk 			hme_init(sc);
   1094   1.1       pk 		} else if ((ifp->if_flags & IFF_UP) != 0) {
   1095   1.1       pk 			/*
   1096   1.1       pk 			 * Reset the interface to pick up changes in any other
   1097   1.1       pk 			 * flags that affect hardware registers.
   1098   1.1       pk 			 */
   1099   1.1       pk 			hme_init(sc);
   1100   1.1       pk 		}
   1101   1.1       pk #ifdef HMEDEBUG
   1102   1.1       pk 		sc->sc_debug = (ifp->if_flags & IFF_DEBUG) != 0 ? 1 : 0;
   1103   1.1       pk #endif
   1104   1.1       pk 		break;
   1105   1.1       pk 
   1106   1.1       pk 	case SIOCADDMULTI:
   1107   1.1       pk 	case SIOCDELMULTI:
   1108   1.1       pk 		error = (cmd == SIOCADDMULTI) ?
   1109   1.1       pk 		    ether_addmulti(ifr, &sc->sc_ethercom) :
   1110   1.1       pk 		    ether_delmulti(ifr, &sc->sc_ethercom);
   1111   1.1       pk 
   1112   1.1       pk 		if (error == ENETRESET) {
   1113   1.1       pk 			/*
   1114   1.1       pk 			 * Multicast list has changed; set the hardware filter
   1115   1.1       pk 			 * accordingly.
   1116   1.1       pk 			 */
   1117   1.1       pk 			hme_setladrf(sc);
   1118   1.1       pk 			error = 0;
   1119   1.1       pk 		}
   1120   1.1       pk 		break;
   1121   1.1       pk 
   1122   1.1       pk 	case SIOCGIFMEDIA:
   1123   1.1       pk 	case SIOCSIFMEDIA:
   1124   1.1       pk 		error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
   1125   1.1       pk 		break;
   1126   1.1       pk 
   1127   1.1       pk 	default:
   1128   1.1       pk 		error = EINVAL;
   1129   1.1       pk 		break;
   1130   1.1       pk 	}
   1131   1.1       pk 
   1132   1.1       pk 	splx(s);
   1133   1.1       pk 	return (error);
   1134   1.1       pk }
   1135   1.1       pk 
   1136   1.1       pk void
   1137   1.1       pk hme_shutdown(arg)
   1138   1.1       pk 	void *arg;
   1139   1.1       pk {
   1140   1.1       pk 	hme_stop((struct hme_softc *)arg);
   1141   1.1       pk }
   1142   1.1       pk 
   1143   1.1       pk /*
   1144   1.1       pk  * Set up the logical address filter.
   1145   1.1       pk  */
   1146   1.1       pk void
   1147   1.1       pk hme_setladrf(sc)
   1148   1.1       pk 	struct hme_softc *sc;
   1149   1.1       pk {
   1150   1.1       pk 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1151   1.1       pk 	struct ether_multi *enm;
   1152   1.1       pk 	struct ether_multistep step;
   1153  1.26     tron 	struct ethercom *ac = &sc->sc_ethercom;
   1154   1.1       pk 	bus_space_tag_t t = sc->sc_bustag;
   1155   1.1       pk 	bus_space_handle_t mac = sc->sc_mac;
   1156   1.1       pk 	u_char *cp;
   1157   1.1       pk 	u_int32_t crc;
   1158   1.1       pk 	u_int32_t hash[4];
   1159  1.14       pk 	u_int32_t v;
   1160   1.1       pk 	int len;
   1161   1.1       pk 
   1162  1.14       pk 	/* Clear hash table */
   1163  1.14       pk 	hash[3] = hash[2] = hash[1] = hash[0] = 0;
   1164  1.14       pk 
   1165  1.14       pk 	/* Get current RX configuration */
   1166  1.14       pk 	v = bus_space_read_4(t, mac, HME_MACI_RXCFG);
   1167  1.14       pk 
   1168  1.14       pk 	if ((ifp->if_flags & IFF_PROMISC) != 0) {
   1169  1.14       pk 		/* Turn on promiscuous mode; turn off the hash filter */
   1170  1.14       pk 		v |= HME_MAC_RXCFG_PMISC;
   1171  1.14       pk 		v &= ~HME_MAC_RXCFG_HENABLE;
   1172  1.14       pk 		ifp->if_flags |= IFF_ALLMULTI;
   1173  1.14       pk 		goto chipit;
   1174  1.14       pk 	}
   1175  1.14       pk 
   1176  1.14       pk 	/* Turn off promiscuous mode; turn on the hash filter */
   1177  1.14       pk 	v &= ~HME_MAC_RXCFG_PMISC;
   1178  1.14       pk 	v |= HME_MAC_RXCFG_HENABLE;
   1179  1.14       pk 
   1180   1.1       pk 	/*
   1181   1.1       pk 	 * Set up multicast address filter by passing all multicast addresses
   1182   1.1       pk 	 * through a crc generator, and then using the high order 6 bits as an
   1183   1.1       pk 	 * index into the 64 bit logical address filter.  The high order bit
   1184   1.1       pk 	 * selects the word, while the rest of the bits select the bit within
   1185   1.1       pk 	 * the word.
   1186   1.1       pk 	 */
   1187   1.1       pk 
   1188  1.26     tron 	ETHER_FIRST_MULTI(step, ac, enm);
   1189   1.1       pk 	while (enm != NULL) {
   1190  1.26     tron 		if (bcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
   1191   1.1       pk 			/*
   1192   1.1       pk 			 * We must listen to a range of multicast addresses.
   1193   1.1       pk 			 * For now, just accept all multicasts, rather than
   1194   1.1       pk 			 * trying to set only those filter bits needed to match
   1195   1.1       pk 			 * the range.  (At this time, the only use of address
   1196   1.1       pk 			 * ranges is for IP multicast routing, for which the
   1197   1.1       pk 			 * range is big enough to require all bits set.)
   1198   1.1       pk 			 */
   1199  1.14       pk 			hash[3] = hash[2] = hash[1] = hash[0] = 0xffff;
   1200  1.14       pk 			ifp->if_flags |= IFF_ALLMULTI;
   1201  1.14       pk 			goto chipit;
   1202   1.1       pk 		}
   1203   1.1       pk 
   1204   1.1       pk 		cp = enm->enm_addrlo;
   1205   1.1       pk 		crc = 0xffffffff;
   1206   1.1       pk 		for (len = sizeof(enm->enm_addrlo); --len >= 0;) {
   1207   1.1       pk 			int octet = *cp++;
   1208   1.1       pk 			int i;
   1209   1.1       pk 
   1210   1.1       pk #define MC_POLY_LE	0xedb88320UL	/* mcast crc, little endian */
   1211   1.1       pk 			for (i = 0; i < 8; i++) {
   1212   1.1       pk 				if ((crc & 1) ^ (octet & 1)) {
   1213   1.1       pk 					crc >>= 1;
   1214   1.1       pk 					crc ^= MC_POLY_LE;
   1215   1.1       pk 				} else {
   1216   1.1       pk 					crc >>= 1;
   1217   1.1       pk 				}
   1218   1.1       pk 				octet >>= 1;
   1219   1.1       pk 			}
   1220   1.1       pk 		}
   1221   1.1       pk 		/* Just want the 6 most significant bits. */
   1222   1.1       pk 		crc >>= 26;
   1223   1.1       pk 
   1224   1.1       pk 		/* Set the corresponding bit in the filter. */
   1225   1.1       pk 		hash[crc >> 4] |= 1 << (crc & 0xf);
   1226   1.1       pk 
   1227   1.1       pk 		ETHER_NEXT_MULTI(step, enm);
   1228   1.1       pk 	}
   1229   1.1       pk 
   1230  1.14       pk 	ifp->if_flags &= ~IFF_ALLMULTI;
   1231  1.14       pk 
   1232  1.14       pk chipit:
   1233  1.14       pk 	/* Now load the hash table into the chip */
   1234   1.1       pk 	bus_space_write_4(t, mac, HME_MACI_HASHTAB0, hash[0]);
   1235   1.1       pk 	bus_space_write_4(t, mac, HME_MACI_HASHTAB1, hash[1]);
   1236   1.1       pk 	bus_space_write_4(t, mac, HME_MACI_HASHTAB2, hash[2]);
   1237   1.1       pk 	bus_space_write_4(t, mac, HME_MACI_HASHTAB3, hash[3]);
   1238  1.14       pk 	bus_space_write_4(t, mac, HME_MACI_RXCFG, v);
   1239   1.1       pk }
   1240   1.1       pk 
   1241  1.26     tron int
   1242  1.26     tron hme_encap(sc, mhead, bixp)
   1243  1.26     tron 	struct hme_softc *sc;
   1244  1.26     tron 	struct mbuf *mhead;
   1245  1.26     tron 	int *bixp;
   1246  1.26     tron {
   1247  1.26     tron 	struct hme_sxd *sd;
   1248  1.26     tron 	struct mbuf *m;
   1249  1.26     tron 	int frag, cur, cnt = 0;
   1250  1.26     tron 	u_int32_t flags;
   1251  1.26     tron 	struct hme_ring *hr = &sc->sc_rb;
   1252  1.26     tron 
   1253  1.26     tron 	cur = frag = *bixp;
   1254  1.26     tron 	sd = &sc->sc_txd[frag];
   1255  1.26     tron 
   1256  1.26     tron 	for (m = mhead; m != NULL; m = m->m_next) {
   1257  1.26     tron 		if (m->m_len == 0)
   1258  1.26     tron 			continue;
   1259  1.26     tron 
   1260  1.26     tron 		if ((HME_TX_RING_SIZE - (sc->sc_tx_cnt + cnt)) < 5)
   1261  1.26     tron 			goto err;
   1262  1.26     tron 
   1263  1.26     tron 		if (bus_dmamap_load(sc->sc_dmatag, sd->sd_map,
   1264  1.26     tron 		    mtod(m, caddr_t), m->m_len, NULL, BUS_DMA_NOWAIT) != 0)
   1265  1.26     tron 			goto err;
   1266  1.26     tron 
   1267  1.26     tron 		sd->sd_loaded = 1;
   1268  1.26     tron 		bus_dmamap_sync(sc->sc_dmatag, sd->sd_map, 0,
   1269  1.26     tron 		    sd->sd_map->dm_mapsize, BUS_DMASYNC_PREWRITE);
   1270  1.26     tron 
   1271  1.26     tron 		sd->sd_mbuf = NULL;
   1272  1.26     tron 
   1273  1.26     tron 		flags = HME_XD_ENCODE_TSIZE(m->m_len);
   1274  1.26     tron 		if (cnt == 0)
   1275  1.26     tron 			flags |= HME_XD_SOP;
   1276  1.26     tron 		else
   1277  1.26     tron 			flags |= HME_XD_OWN;
   1278  1.26     tron 
   1279  1.26     tron 		HME_XD_SETADDR(sc->sc_pci, hr->rb_txd, frag,
   1280  1.26     tron 		    sd->sd_map->dm_segs[0].ds_addr);
   1281  1.26     tron 		HME_XD_SETFLAGS(sc->sc_pci, hr->rb_txd, frag, flags);
   1282  1.26     tron 
   1283  1.26     tron 		cur = frag;
   1284  1.26     tron 		cnt++;
   1285  1.26     tron 		if (++frag == HME_TX_RING_SIZE) {
   1286  1.26     tron 			frag = 0;
   1287  1.26     tron 			sd = sc->sc_txd;
   1288  1.26     tron 		} else
   1289  1.26     tron 			sd++;
   1290  1.26     tron 	}
   1291  1.26     tron 
   1292  1.26     tron 	/* Set end of packet on last descriptor. */
   1293  1.26     tron 	flags = HME_XD_GETFLAGS(sc->sc_pci, hr->rb_txd, cur);
   1294  1.26     tron 	flags |= HME_XD_EOP;
   1295  1.26     tron 	HME_XD_SETFLAGS(sc->sc_pci, hr->rb_txd, cur, flags);
   1296  1.26     tron 	sc->sc_txd[cur].sd_mbuf = mhead;
   1297  1.26     tron 
   1298  1.26     tron 	/* Give first frame over to the hardware. */
   1299  1.26     tron 	flags = HME_XD_GETFLAGS(sc->sc_pci, hr->rb_txd, (*bixp));
   1300  1.26     tron 	flags |= HME_XD_OWN;
   1301  1.26     tron 	HME_XD_SETFLAGS(sc->sc_pci, hr->rb_txd, (*bixp), flags);
   1302  1.26     tron 
   1303  1.26     tron 	sc->sc_tx_cnt += cnt;
   1304  1.26     tron 	*bixp = frag;
   1305   1.1       pk 
   1306  1.26     tron 	/* sync descriptors */
   1307   1.1       pk 
   1308  1.26     tron 	return (0);
   1309   1.1       pk 
   1310  1.26     tron err:
   1311   1.1       pk 	/*
   1312  1.26     tron 	 * Invalidate the stuff we may have already put into place. We
   1313  1.26     tron 	 * will be called again to queue it later.
   1314   1.1       pk 	 */
   1315  1.26     tron 	for (; cnt > 0; cnt--) {
   1316  1.26     tron 		if (--frag == -1)
   1317  1.26     tron 			frag = HME_TX_RING_SIZE - 1;
   1318  1.26     tron 		sd = &sc->sc_txd[frag];
   1319  1.26     tron 		bus_dmamap_sync(sc->sc_dmatag, sd->sd_map, 0,
   1320  1.26     tron 		    sd->sd_map->dm_mapsize, BUS_DMASYNC_POSTWRITE);
   1321  1.26     tron 		bus_dmamap_unload(sc->sc_dmatag, sd->sd_map);
   1322  1.26     tron 		sd->sd_loaded = 0;
   1323  1.26     tron 		sd->sd_mbuf = NULL;
   1324  1.26     tron 	}
   1325  1.26     tron 	return (ENOBUFS);
   1326   1.1       pk }
   1327   1.1       pk 
   1328  1.26     tron int
   1329  1.26     tron hme_newbuf(sc, d, freeit)
   1330   1.1       pk 	struct hme_softc *sc;
   1331  1.26     tron 	struct hme_sxd *d;
   1332  1.26     tron 	int freeit;
   1333   1.1       pk {
   1334  1.26     tron 	struct mbuf *m;
   1335  1.26     tron 	bus_dmamap_t map;
   1336  1.26     tron 
   1337  1.26     tron 	MGETHDR(m, M_DONTWAIT, MT_DATA);
   1338  1.26     tron 	if (m == NULL)
   1339  1.26     tron 		return (ENOBUFS);
   1340  1.26     tron 	m->m_pkthdr.rcvif = &sc->sc_ethercom.ec_if;
   1341   1.1       pk 
   1342  1.26     tron 	MCLGET(m, M_DONTWAIT);
   1343  1.26     tron 	if ((m->m_flags & M_EXT) == 0) {
   1344  1.26     tron 		m_freem(m);
   1345  1.26     tron 		return (ENOBUFS);
   1346  1.26     tron 	}
   1347  1.26     tron 
   1348  1.26     tron 	if (d->sd_loaded) {
   1349  1.26     tron 		bus_dmamap_sync(sc->sc_dmatag, d->sd_map,
   1350  1.26     tron 		    0, d->sd_map->dm_mapsize, BUS_DMASYNC_POSTREAD);
   1351  1.26     tron 		bus_dmamap_unload(sc->sc_dmatag, d->sd_map);
   1352  1.26     tron 		d->sd_loaded = 0;
   1353  1.26     tron 	}
   1354  1.26     tron 
   1355  1.26     tron 	if (bus_dmamap_load(sc->sc_dmatag, sc->sc_rxmap_spare,
   1356  1.26     tron 	    mtod(m, caddr_t), MCLBYTES - HME_RX_OFFSET, NULL,
   1357  1.26     tron 	    BUS_DMA_NOWAIT) != 0) {
   1358  1.26     tron 		if (d->sd_mbuf == NULL)
   1359  1.26     tron 			return (ENOBUFS);
   1360  1.26     tron 		m_freem(m);
   1361  1.26     tron 		return (ENOBUFS);
   1362  1.26     tron 	}
   1363  1.26     tron 
   1364  1.26     tron 	if (d->sd_loaded) {
   1365  1.26     tron 		bus_dmamap_sync(sc->sc_dmatag, d->sd_map, 0,
   1366  1.26     tron 		    d->sd_map->dm_mapsize, BUS_DMASYNC_POSTREAD);
   1367  1.26     tron 		bus_dmamap_unload(sc->sc_dmatag, d->sd_map);
   1368  1.26     tron 		d->sd_loaded = 0;
   1369  1.26     tron 	}
   1370  1.26     tron 	if ((d->sd_mbuf != NULL) && freeit) {
   1371  1.26     tron 		m_freem(d->sd_mbuf);
   1372  1.26     tron 		d->sd_mbuf = NULL;
   1373  1.26     tron 	}
   1374  1.26     tron 
   1375  1.26     tron 	map = d->sd_map;
   1376  1.26     tron 	d->sd_map = sc->sc_rxmap_spare;
   1377  1.26     tron 	sc->sc_rxmap_spare = map;
   1378  1.26     tron 
   1379  1.26     tron 	d->sd_loaded = 1;
   1380  1.26     tron 
   1381  1.26     tron 	bus_dmamap_sync(sc->sc_dmatag, d->sd_map, 0, d->sd_map->dm_mapsize,
   1382  1.26     tron 	    BUS_DMASYNC_PREREAD);
   1383  1.26     tron 
   1384  1.26     tron 	m->m_data += HME_RX_OFFSET;
   1385  1.26     tron 	d->sd_mbuf = m;
   1386  1.26     tron 	return (0);
   1387   1.1       pk }
   1388