Home | History | Annotate | Line # | Download | only in ic
hme.c revision 1.36
      1  1.36      wiz /*	$NetBSD: hme.c,v 1.36 2003/04/26 22:08:29 wiz 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.36      wiz __KERNEL_RCSID(0, "$NetBSD: hme.c,v 1.36 2003/04/26 22:08:29 wiz Exp $");
     45   1.1       pk 
     46  1.28     tron #define 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.1       pk void		hme_start __P((struct ifnet *));
     99   1.1       pk void		hme_stop __P((struct hme_softc *));
    100   1.1       pk int		hme_ioctl __P((struct ifnet *, u_long, caddr_t));
    101   1.5       pk void		hme_tick __P((void *));
    102   1.1       pk void		hme_watchdog __P((struct ifnet *));
    103   1.1       pk void		hme_shutdown __P((void *));
    104   1.1       pk void		hme_init __P((struct hme_softc *));
    105   1.1       pk void		hme_meminit __P((struct hme_softc *));
    106   1.4       pk void		hme_mifinit __P((struct hme_softc *));
    107   1.1       pk void		hme_reset __P((struct hme_softc *));
    108   1.1       pk void		hme_setladrf __P((struct hme_softc *));
    109   1.1       pk 
    110   1.1       pk /* MII methods & callbacks */
    111   1.1       pk static int	hme_mii_readreg __P((struct device *, int, int));
    112   1.1       pk static void	hme_mii_writereg __P((struct device *, int, int, int));
    113   1.1       pk static void	hme_mii_statchg __P((struct device *));
    114   1.1       pk 
    115   1.1       pk int		hme_mediachange __P((struct ifnet *));
    116   1.1       pk void		hme_mediastatus __P((struct ifnet *, struct ifmediareq *));
    117   1.1       pk 
    118  1.28     tron struct mbuf	*hme_get __P((struct hme_softc *, int, int));
    119  1.28     tron int		hme_put __P((struct hme_softc *, int, struct mbuf *));
    120  1.28     tron void		hme_read __P((struct hme_softc *, int, int));
    121   1.1       pk int		hme_eint __P((struct hme_softc *, u_int));
    122   1.1       pk int		hme_rint __P((struct hme_softc *));
    123   1.1       pk int		hme_tint __P((struct hme_softc *));
    124   1.1       pk 
    125  1.28     tron static int	ether_cmp __P((u_char *, u_char *));
    126  1.28     tron 
    127  1.28     tron /* Default buffer copy routines */
    128  1.28     tron void	hme_copytobuf_contig __P((struct hme_softc *, void *, int, int));
    129  1.28     tron void	hme_copyfrombuf_contig __P((struct hme_softc *, void *, int, int));
    130  1.28     tron void	hme_zerobuf_contig __P((struct hme_softc *, int, int));
    131  1.28     tron 
    132  1.28     tron 
    133   1.1       pk void
    134   1.1       pk hme_config(sc)
    135   1.1       pk 	struct hme_softc *sc;
    136   1.1       pk {
    137   1.1       pk 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    138   1.1       pk 	struct mii_data *mii = &sc->sc_mii;
    139   1.5       pk 	struct mii_softc *child;
    140  1.11       pk 	bus_dma_tag_t dmatag = sc->sc_dmatag;
    141   1.1       pk 	bus_dma_segment_t seg;
    142   1.1       pk 	bus_size_t size;
    143  1.28     tron 	int rseg, error;
    144   1.1       pk 
    145   1.1       pk 	/*
    146   1.1       pk 	 * HME common initialization.
    147   1.1       pk 	 *
    148   1.1       pk 	 * hme_softc fields that must be initialized by the front-end:
    149   1.1       pk 	 *
    150   1.1       pk 	 * the bus tag:
    151   1.1       pk 	 *	sc_bustag
    152   1.1       pk 	 *
    153   1.1       pk 	 * the dma bus tag:
    154   1.1       pk 	 *	sc_dmatag
    155   1.1       pk 	 *
    156   1.1       pk 	 * the bus handles:
    157   1.1       pk 	 *	sc_seb		(Shared Ethernet Block registers)
    158   1.1       pk 	 *	sc_erx		(Receiver Unit registers)
    159   1.1       pk 	 *	sc_etx		(Transmitter Unit registers)
    160   1.1       pk 	 *	sc_mac		(MAC registers)
    161  1.36      wiz 	 *	sc_mif		(Management Interface registers)
    162   1.1       pk 	 *
    163   1.1       pk 	 * the maximum bus burst size:
    164   1.1       pk 	 *	sc_burst
    165   1.1       pk 	 *
    166  1.28     tron 	 * (notyet:DMA capable memory for the ring descriptors & packet buffers:
    167  1.28     tron 	 *	rb_membase, rb_dmabase)
    168  1.28     tron 	 *
    169   1.1       pk 	 * the local Ethernet address:
    170   1.1       pk 	 *	sc_enaddr
    171   1.1       pk 	 *
    172   1.1       pk 	 */
    173   1.1       pk 
    174   1.1       pk 	/* Make sure the chip is stopped. */
    175   1.1       pk 	hme_stop(sc);
    176   1.1       pk 
    177   1.1       pk 
    178  1.28     tron 	/*
    179  1.28     tron 	 * Allocate descriptors and buffers
    180  1.28     tron 	 * XXX - do all this differently.. and more configurably,
    181  1.28     tron 	 * eg. use things as `dma_load_mbuf()' on transmit,
    182  1.28     tron 	 *     and a pool of `EXTMEM' mbufs (with buffers DMA-mapped
    183  1.28     tron 	 *     all the time) on the reveiver side.
    184  1.28     tron 	 *
    185  1.28     tron 	 * Note: receive buffers must be 64-byte aligned.
    186  1.28     tron 	 * Also, apparently, the buffers must extend to a DMA burst
    187  1.28     tron 	 * boundary beyond the maximum packet size.
    188  1.28     tron 	 */
    189  1.28     tron #define _HME_NDESC	128
    190  1.28     tron #define _HME_BUFSZ	1600
    191  1.28     tron 
    192  1.28     tron 	/* Note: the # of descriptors must be a multiple of 16 */
    193  1.28     tron 	sc->sc_rb.rb_ntbuf = _HME_NDESC;
    194  1.28     tron 	sc->sc_rb.rb_nrbuf = _HME_NDESC;
    195   1.1       pk 
    196   1.1       pk 	/*
    197   1.1       pk 	 * Allocate DMA capable memory
    198   1.1       pk 	 * Buffer descriptors must be aligned on a 2048 byte boundary;
    199   1.1       pk 	 * take this into account when calculating the size. Note that
    200   1.1       pk 	 * the maximum number of descriptors (256) occupies 2048 bytes,
    201  1.28     tron 	 * so we allocate that much regardless of _HME_NDESC.
    202   1.1       pk 	 */
    203  1.28     tron 	size =	2048 +					/* TX descriptors */
    204  1.28     tron 		2048 +					/* RX descriptors */
    205  1.28     tron 		sc->sc_rb.rb_ntbuf * _HME_BUFSZ +	/* TX buffers */
    206  1.28     tron 		sc->sc_rb.rb_nrbuf * _HME_BUFSZ;	/* TX buffers */
    207  1.11       pk 
    208  1.11       pk 	/* Allocate DMA buffer */
    209  1.28     tron 	if ((error = bus_dmamem_alloc(dmatag, size,
    210  1.28     tron 				      2048, 0,
    211  1.28     tron 				      &seg, 1, &rseg, BUS_DMA_NOWAIT)) != 0) {
    212   1.1       pk 		printf("%s: DMA buffer alloc error %d\n",
    213  1.28     tron 			sc->sc_dev.dv_xname, error);
    214  1.10      mrg 		return;
    215   1.1       pk 	}
    216   1.1       pk 
    217  1.11       pk 	/* Map DMA memory in CPU addressable space */
    218  1.11       pk 	if ((error = bus_dmamem_map(dmatag, &seg, rseg, size,
    219  1.28     tron 				    &sc->sc_rb.rb_membase,
    220  1.28     tron 				    BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) != 0) {
    221   1.1       pk 		printf("%s: DMA buffer map error %d\n",
    222  1.28     tron 			sc->sc_dev.dv_xname, error);
    223  1.11       pk 		bus_dmamap_unload(dmatag, sc->sc_dmamap);
    224  1.11       pk 		bus_dmamem_free(dmatag, &seg, rseg);
    225   1.1       pk 		return;
    226   1.1       pk 	}
    227  1.13      mrg 
    228  1.13      mrg 	if ((error = bus_dmamap_create(dmatag, size, 1, size, 0,
    229  1.28     tron 				    BUS_DMA_NOWAIT, &sc->sc_dmamap)) != 0) {
    230  1.13      mrg 		printf("%s: DMA map create error %d\n",
    231  1.28     tron 			sc->sc_dev.dv_xname, error);
    232  1.13      mrg 		return;
    233  1.13      mrg 	}
    234  1.13      mrg 
    235  1.13      mrg 	/* Load the buffer */
    236  1.13      mrg 	if ((error = bus_dmamap_load(dmatag, sc->sc_dmamap,
    237  1.17      mrg 	    sc->sc_rb.rb_membase, size, NULL,
    238  1.17      mrg 	    BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) != 0) {
    239  1.13      mrg 		printf("%s: DMA buffer map load error %d\n",
    240  1.28     tron 			sc->sc_dev.dv_xname, error);
    241  1.13      mrg 		bus_dmamem_free(dmatag, &seg, rseg);
    242  1.13      mrg 		return;
    243  1.13      mrg 	}
    244  1.13      mrg 	sc->sc_rb.rb_dmabase = sc->sc_dmamap->dm_segs[0].ds_addr;
    245   1.1       pk 
    246  1.22  thorpej 	printf("%s: Ethernet address %s\n", sc->sc_dev.dv_xname,
    247  1.22  thorpej 	    ether_sprintf(sc->sc_enaddr));
    248   1.2       pk 
    249   1.1       pk 	/* Initialize ifnet structure. */
    250  1.21  thorpej 	strcpy(ifp->if_xname, sc->sc_dev.dv_xname);
    251   1.1       pk 	ifp->if_softc = sc;
    252   1.1       pk 	ifp->if_start = hme_start;
    253   1.1       pk 	ifp->if_ioctl = hme_ioctl;
    254   1.1       pk 	ifp->if_watchdog = hme_watchdog;
    255   1.1       pk 	ifp->if_flags =
    256   1.1       pk 	    IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS | IFF_MULTICAST;
    257  1.20  thorpej 	IFQ_SET_READY(&ifp->if_snd);
    258   1.1       pk 
    259   1.1       pk 	/* Initialize ifmedia structures and MII info */
    260   1.1       pk 	mii->mii_ifp = ifp;
    261  1.34   petrov 	mii->mii_readreg = hme_mii_readreg;
    262   1.1       pk 	mii->mii_writereg = hme_mii_writereg;
    263   1.1       pk 	mii->mii_statchg = hme_mii_statchg;
    264   1.1       pk 
    265  1.33       pk 	ifmedia_init(&mii->mii_media, 0, hme_mediachange, hme_mediastatus);
    266   1.1       pk 
    267   1.4       pk 	hme_mifinit(sc);
    268   1.4       pk 
    269   1.6  thorpej 	mii_attach(&sc->sc_dev, mii, 0xffffffff,
    270  1.34   petrov 			MII_PHY_ANY, MII_OFFSET_ANY, MIIF_FORCEANEG);
    271   1.2       pk 
    272   1.5       pk 	child = LIST_FIRST(&mii->mii_phys);
    273   1.5       pk 	if (child == NULL) {
    274   1.1       pk 		/* No PHY attached */
    275   1.1       pk 		ifmedia_add(&sc->sc_media, IFM_ETHER|IFM_MANUAL, 0, NULL);
    276   1.1       pk 		ifmedia_set(&sc->sc_media, IFM_ETHER|IFM_MANUAL);
    277   1.1       pk 	} else {
    278   1.1       pk 		/*
    279   1.5       pk 		 * Walk along the list of attached MII devices and
    280   1.5       pk 		 * establish an `MII instance' to `phy number'
    281   1.5       pk 		 * mapping. We'll use this mapping in media change
    282   1.5       pk 		 * requests to determine which phy to use to program
    283   1.5       pk 		 * the MIF configuration register.
    284   1.5       pk 		 */
    285   1.5       pk 		for (; child != NULL; child = LIST_NEXT(child, mii_list)) {
    286   1.5       pk 			/*
    287   1.5       pk 			 * Note: we support just two PHYs: the built-in
    288   1.5       pk 			 * internal device and an external on the MII
    289   1.5       pk 			 * connector.
    290   1.5       pk 			 */
    291   1.5       pk 			if (child->mii_phy > 1 || child->mii_inst > 1) {
    292   1.5       pk 				printf("%s: cannot accomodate MII device %s"
    293  1.28     tron 				       " at phy %d, instance %d\n",
    294  1.28     tron 				       sc->sc_dev.dv_xname,
    295  1.28     tron 				       child->mii_dev.dv_xname,
    296  1.28     tron 				       child->mii_phy, child->mii_inst);
    297   1.5       pk 				continue;
    298   1.5       pk 			}
    299   1.5       pk 
    300   1.5       pk 			sc->sc_phys[child->mii_inst] = child->mii_phy;
    301   1.5       pk 		}
    302   1.5       pk 
    303   1.5       pk 		/*
    304   1.1       pk 		 * XXX - we can really do the following ONLY if the
    305   1.1       pk 		 * phy indeed has the auto negotiation capability!!
    306   1.1       pk 		 */
    307   1.1       pk 		ifmedia_set(&sc->sc_media, IFM_ETHER|IFM_AUTO);
    308   1.1       pk 	}
    309  1.27     tron 
    310  1.28     tron 	/* claim 802.1q capability */
    311  1.27     tron 	sc->sc_ethercom.ec_capabilities |= ETHERCAP_VLAN_MTU;
    312   1.1       pk 
    313   1.1       pk 	/* Attach the interface. */
    314   1.1       pk 	if_attach(ifp);
    315   1.1       pk 	ether_ifattach(ifp, sc->sc_enaddr);
    316   1.1       pk 
    317   1.1       pk 	sc->sc_sh = shutdownhook_establish(hme_shutdown, sc);
    318   1.1       pk 	if (sc->sc_sh == NULL)
    319   1.1       pk 		panic("hme_config: can't establish shutdownhook");
    320   1.1       pk 
    321   1.1       pk #if NRND > 0
    322   1.1       pk 	rnd_attach_source(&sc->rnd_source, sc->sc_dev.dv_xname,
    323   1.1       pk 			  RND_TYPE_NET, 0);
    324   1.1       pk #endif
    325   1.5       pk 
    326   1.9  thorpej 	callout_init(&sc->sc_tick_ch);
    327   1.5       pk }
    328   1.5       pk 
    329   1.5       pk void
    330   1.5       pk hme_tick(arg)
    331   1.5       pk 	void *arg;
    332   1.5       pk {
    333   1.5       pk 	struct hme_softc *sc = arg;
    334   1.5       pk 	int s;
    335   1.5       pk 
    336   1.5       pk 	s = splnet();
    337   1.5       pk 	mii_tick(&sc->sc_mii);
    338   1.5       pk 	splx(s);
    339   1.5       pk 
    340   1.9  thorpej 	callout_reset(&sc->sc_tick_ch, hz, hme_tick, sc);
    341   1.1       pk }
    342   1.1       pk 
    343   1.1       pk void
    344   1.1       pk hme_reset(sc)
    345   1.1       pk 	struct hme_softc *sc;
    346   1.1       pk {
    347   1.1       pk 	int s;
    348   1.1       pk 
    349   1.1       pk 	s = splnet();
    350   1.1       pk 	hme_init(sc);
    351   1.1       pk 	splx(s);
    352   1.1       pk }
    353   1.1       pk 
    354   1.1       pk void
    355   1.1       pk hme_stop(sc)
    356   1.1       pk 	struct hme_softc *sc;
    357   1.1       pk {
    358   1.1       pk 	bus_space_tag_t t = sc->sc_bustag;
    359   1.1       pk 	bus_space_handle_t seb = sc->sc_seb;
    360   1.1       pk 	int n;
    361   1.1       pk 
    362   1.9  thorpej 	callout_stop(&sc->sc_tick_ch);
    363   1.5       pk 	mii_down(&sc->sc_mii);
    364   1.5       pk 
    365  1.33       pk 	/* Mask all interrupts */
    366  1.33       pk 	bus_space_write_4(t, seb, HME_SEBI_IMASK, 0xffffffff);
    367  1.33       pk 
    368   1.1       pk 	/* Reset transmitter and receiver */
    369   1.1       pk 	bus_space_write_4(t, seb, HME_SEBI_RESET,
    370  1.28     tron 			  (HME_SEB_RESET_ETX | HME_SEB_RESET_ERX));
    371   1.1       pk 
    372   1.1       pk 	for (n = 0; n < 20; n++) {
    373   1.1       pk 		u_int32_t v = bus_space_read_4(t, seb, HME_SEBI_RESET);
    374   1.1       pk 		if ((v & (HME_SEB_RESET_ETX | HME_SEB_RESET_ERX)) == 0)
    375   1.1       pk 			return;
    376   1.1       pk 		DELAY(20);
    377   1.1       pk 	}
    378   1.1       pk 
    379   1.1       pk 	printf("%s: hme_stop: reset failed\n", sc->sc_dev.dv_xname);
    380   1.1       pk }
    381   1.1       pk 
    382   1.1       pk void
    383   1.1       pk hme_meminit(sc)
    384   1.1       pk 	struct hme_softc *sc;
    385   1.1       pk {
    386  1.28     tron 	bus_addr_t txbufdma, rxbufdma;
    387   1.1       pk 	bus_addr_t dma;
    388   1.1       pk 	caddr_t p;
    389  1.28     tron 	unsigned int ntbuf, nrbuf, i;
    390   1.1       pk 	struct hme_ring *hr = &sc->sc_rb;
    391   1.1       pk 
    392   1.1       pk 	p = hr->rb_membase;
    393   1.1       pk 	dma = hr->rb_dmabase;
    394   1.1       pk 
    395  1.28     tron 	ntbuf = hr->rb_ntbuf;
    396  1.28     tron 	nrbuf = hr->rb_nrbuf;
    397  1.28     tron 
    398   1.1       pk 	/*
    399   1.1       pk 	 * Allocate transmit descriptors
    400   1.1       pk 	 */
    401   1.1       pk 	hr->rb_txd = p;
    402   1.1       pk 	hr->rb_txddma = dma;
    403  1.28     tron 	p += ntbuf * HME_XD_SIZE;
    404  1.28     tron 	dma += ntbuf * HME_XD_SIZE;
    405   1.4       pk 	/* We have reserved descriptor space until the next 2048 byte boundary.*/
    406   1.4       pk 	dma = (bus_addr_t)roundup((u_long)dma, 2048);
    407   1.4       pk 	p = (caddr_t)roundup((u_long)p, 2048);
    408   1.1       pk 
    409   1.1       pk 	/*
    410   1.1       pk 	 * Allocate receive descriptors
    411   1.1       pk 	 */
    412   1.1       pk 	hr->rb_rxd = p;
    413   1.1       pk 	hr->rb_rxddma = dma;
    414  1.28     tron 	p += nrbuf * HME_XD_SIZE;
    415  1.28     tron 	dma += nrbuf * HME_XD_SIZE;
    416   1.4       pk 	/* Again move forward to the next 2048 byte boundary.*/
    417   1.4       pk 	dma = (bus_addr_t)roundup((u_long)dma, 2048);
    418   1.4       pk 	p = (caddr_t)roundup((u_long)p, 2048);
    419   1.1       pk 
    420  1.28     tron 
    421   1.1       pk 	/*
    422  1.28     tron 	 * Allocate transmit buffers
    423   1.1       pk 	 */
    424  1.28     tron 	hr->rb_txbuf = p;
    425  1.28     tron 	txbufdma = dma;
    426  1.28     tron 	p += ntbuf * _HME_BUFSZ;
    427  1.28     tron 	dma += ntbuf * _HME_BUFSZ;
    428  1.28     tron 
    429  1.28     tron 	/*
    430  1.28     tron 	 * Allocate receive buffers
    431  1.28     tron 	 */
    432  1.28     tron 	hr->rb_rxbuf = p;
    433  1.28     tron 	rxbufdma = dma;
    434  1.28     tron 	p += nrbuf * _HME_BUFSZ;
    435  1.28     tron 	dma += nrbuf * _HME_BUFSZ;
    436  1.28     tron 
    437  1.28     tron 	/*
    438  1.28     tron 	 * Initialize transmit buffer descriptors
    439  1.28     tron 	 */
    440  1.28     tron 	for (i = 0; i < ntbuf; i++) {
    441  1.28     tron 		HME_XD_SETADDR(sc->sc_pci, hr->rb_txd, i, txbufdma + i * _HME_BUFSZ);
    442  1.15      eeh 		HME_XD_SETFLAGS(sc->sc_pci, hr->rb_txd, i, 0);
    443   1.1       pk 	}
    444   1.1       pk 
    445   1.1       pk 	/*
    446  1.28     tron 	 * Initialize receive buffer descriptors
    447   1.1       pk 	 */
    448  1.28     tron 	for (i = 0; i < nrbuf; i++) {
    449  1.28     tron 		HME_XD_SETADDR(sc->sc_pci, hr->rb_rxd, i, rxbufdma + i * _HME_BUFSZ);
    450  1.15      eeh 		HME_XD_SETFLAGS(sc->sc_pci, hr->rb_rxd, i,
    451  1.28     tron 				HME_XD_OWN | HME_XD_ENCODE_RSIZE(_HME_BUFSZ));
    452   1.1       pk 	}
    453   1.1       pk 
    454  1.28     tron 	hr->rb_tdhead = hr->rb_tdtail = 0;
    455  1.28     tron 	hr->rb_td_nbusy = 0;
    456  1.28     tron 	hr->rb_rdtail = 0;
    457   1.1       pk }
    458   1.1       pk 
    459   1.1       pk /*
    460   1.1       pk  * Initialization of interface; set up initialization block
    461   1.1       pk  * and transmit/receive descriptor rings.
    462   1.1       pk  */
    463   1.1       pk void
    464   1.1       pk hme_init(sc)
    465   1.1       pk 	struct hme_softc *sc;
    466   1.1       pk {
    467   1.1       pk 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    468   1.1       pk 	bus_space_tag_t t = sc->sc_bustag;
    469   1.1       pk 	bus_space_handle_t seb = sc->sc_seb;
    470   1.1       pk 	bus_space_handle_t etx = sc->sc_etx;
    471   1.1       pk 	bus_space_handle_t erx = sc->sc_erx;
    472   1.1       pk 	bus_space_handle_t mac = sc->sc_mac;
    473   1.1       pk 	u_int8_t *ea;
    474   1.1       pk 	u_int32_t v;
    475   1.1       pk 
    476   1.1       pk 	/*
    477   1.1       pk 	 * Initialization sequence. The numbered steps below correspond
    478   1.1       pk 	 * to the sequence outlined in section 6.3.5.1 in the Ethernet
    479   1.1       pk 	 * Channel Engine manual (part of the PCIO manual).
    480   1.1       pk 	 * See also the STP2002-STQ document from Sun Microsystems.
    481   1.1       pk 	 */
    482   1.1       pk 
    483   1.1       pk 	/* step 1 & 2. Reset the Ethernet Channel */
    484   1.1       pk 	hme_stop(sc);
    485   1.1       pk 
    486   1.4       pk 	/* Re-initialize the MIF */
    487   1.4       pk 	hme_mifinit(sc);
    488   1.4       pk 
    489   1.1       pk 	/* Call MI reset function if any */
    490   1.1       pk 	if (sc->sc_hwreset)
    491   1.1       pk 		(*sc->sc_hwreset)(sc);
    492   1.1       pk 
    493   1.1       pk #if 0
    494   1.1       pk 	/* Mask all MIF interrupts, just in case */
    495   1.1       pk 	bus_space_write_4(t, mif, HME_MIFI_IMASK, 0xffff);
    496   1.1       pk #endif
    497   1.1       pk 
    498   1.1       pk 	/* step 3. Setup data structures in host memory */
    499   1.1       pk 	hme_meminit(sc);
    500   1.1       pk 
    501   1.1       pk 	/* step 4. TX MAC registers & counters */
    502   1.1       pk 	bus_space_write_4(t, mac, HME_MACI_NCCNT, 0);
    503   1.1       pk 	bus_space_write_4(t, mac, HME_MACI_FCCNT, 0);
    504   1.1       pk 	bus_space_write_4(t, mac, HME_MACI_EXCNT, 0);
    505   1.1       pk 	bus_space_write_4(t, mac, HME_MACI_LTCNT, 0);
    506  1.28     tron 	bus_space_write_4(t, mac, HME_MACI_TXSIZE,
    507  1.28     tron 	    (sc->sc_ethercom.ec_capenable & ETHERCAP_VLAN_MTU) ?
    508  1.28     tron 	    ETHER_VLAN_ENCAP_LEN + ETHER_MAX_LEN :
    509  1.28     tron             ETHER_MAX_LEN);
    510   1.1       pk 
    511   1.1       pk 	/* Load station MAC address */
    512   1.1       pk 	ea = sc->sc_enaddr;
    513   1.1       pk 	bus_space_write_4(t, mac, HME_MACI_MACADDR0, (ea[0] << 8) | ea[1]);
    514   1.1       pk 	bus_space_write_4(t, mac, HME_MACI_MACADDR1, (ea[2] << 8) | ea[3]);
    515   1.1       pk 	bus_space_write_4(t, mac, HME_MACI_MACADDR2, (ea[4] << 8) | ea[5]);
    516   1.1       pk 
    517   1.1       pk 	/*
    518   1.1       pk 	 * Init seed for backoff
    519   1.1       pk 	 * (source suggested by manual: low 10 bits of MAC address)
    520   1.1       pk 	 */
    521   1.1       pk 	v = ((ea[4] << 8) | ea[5]) & 0x3fff;
    522   1.1       pk 	bus_space_write_4(t, mac, HME_MACI_RANDSEED, v);
    523   1.1       pk 
    524   1.1       pk 
    525   1.1       pk 	/* Note: Accepting power-on default for other MAC registers here.. */
    526   1.1       pk 
    527   1.1       pk 
    528   1.1       pk 	/* step 5. RX MAC registers & counters */
    529   1.1       pk 	hme_setladrf(sc);
    530   1.1       pk 
    531   1.1       pk 	/* step 6 & 7. Program Descriptor Ring Base Addresses */
    532   1.1       pk 	bus_space_write_4(t, etx, HME_ETXI_RING, sc->sc_rb.rb_txddma);
    533  1.28     tron 	bus_space_write_4(t, etx, HME_ETXI_RSIZE, sc->sc_rb.rb_ntbuf);
    534   1.1       pk 
    535   1.1       pk 	bus_space_write_4(t, erx, HME_ERXI_RING, sc->sc_rb.rb_rxddma);
    536  1.28     tron 	bus_space_write_4(t, mac, HME_MACI_RXSIZE,
    537  1.28     tron 	    (sc->sc_ethercom.ec_capenable & ETHERCAP_VLAN_MTU) ?
    538  1.28     tron 	    ETHER_VLAN_ENCAP_LEN + ETHER_MAX_LEN :
    539  1.28     tron             ETHER_MAX_LEN);
    540  1.28     tron 
    541   1.1       pk 
    542   1.1       pk 	/* step 8. Global Configuration & Interrupt Mask */
    543   1.1       pk 	bus_space_write_4(t, seb, HME_SEBI_IMASK,
    544  1.28     tron 			~(
    545  1.28     tron 			  /*HME_SEB_STAT_GOTFRAME | HME_SEB_STAT_SENTFRAME |*/
    546  1.28     tron 			  HME_SEB_STAT_HOSTTOTX |
    547  1.28     tron 			  HME_SEB_STAT_RXTOHOST |
    548  1.28     tron 			  HME_SEB_STAT_TXALL |
    549  1.28     tron 			  HME_SEB_STAT_TXPERR |
    550  1.28     tron 			  HME_SEB_STAT_RCNTEXP |
    551  1.33       pk 			  /*HME_SEB_STAT_MIFIRQ |*/
    552  1.28     tron 			  HME_SEB_STAT_ALL_ERRORS ));
    553   1.1       pk 
    554   1.1       pk 	switch (sc->sc_burst) {
    555   1.1       pk 	default:
    556   1.1       pk 		v = 0;
    557   1.1       pk 		break;
    558   1.1       pk 	case 16:
    559   1.1       pk 		v = HME_SEB_CFG_BURST16;
    560   1.1       pk 		break;
    561   1.1       pk 	case 32:
    562   1.1       pk 		v = HME_SEB_CFG_BURST32;
    563   1.1       pk 		break;
    564   1.1       pk 	case 64:
    565   1.1       pk 		v = HME_SEB_CFG_BURST64;
    566   1.1       pk 		break;
    567   1.1       pk 	}
    568   1.1       pk 	bus_space_write_4(t, seb, HME_SEBI_CFG, v);
    569   1.1       pk 
    570   1.1       pk 	/* step 9. ETX Configuration: use mostly default values */
    571   1.1       pk 
    572   1.1       pk 	/* Enable DMA */
    573   1.2       pk 	v = bus_space_read_4(t, etx, HME_ETXI_CFG);
    574   1.1       pk 	v |= HME_ETX_CFG_DMAENABLE;
    575   1.2       pk 	bus_space_write_4(t, etx, HME_ETXI_CFG, v);
    576   1.1       pk 
    577   1.3       pk 	/* Transmit Descriptor ring size: in increments of 16 */
    578  1.28     tron 	bus_space_write_4(t, etx, HME_ETXI_RSIZE, _HME_NDESC / 16 - 1);
    579  1.28     tron 
    580   1.1       pk 
    581   1.3       pk 	/* step 10. ERX Configuration */
    582   1.2       pk 	v = bus_space_read_4(t, erx, HME_ERXI_CFG);
    583  1.28     tron 
    584  1.28     tron 	/* Encode Receive Descriptor ring size: four possible values */
    585  1.28     tron 	switch (_HME_NDESC /*XXX*/) {
    586  1.28     tron 	case 32:
    587  1.28     tron 		v |= HME_ERX_CFG_RINGSIZE32;
    588  1.28     tron 		break;
    589  1.28     tron 	case 64:
    590  1.28     tron 		v |= HME_ERX_CFG_RINGSIZE64;
    591  1.28     tron 		break;
    592  1.28     tron 	case 128:
    593  1.28     tron 		v |= HME_ERX_CFG_RINGSIZE128;
    594  1.28     tron 		break;
    595  1.28     tron 	case 256:
    596  1.28     tron 		v |= HME_ERX_CFG_RINGSIZE256;
    597  1.28     tron 		break;
    598  1.28     tron 	default:
    599  1.28     tron 		printf("hme: invalid Receive Descriptor ring size\n");
    600  1.28     tron 		break;
    601  1.28     tron 	}
    602  1.28     tron 
    603   1.3       pk 	/* Enable DMA */
    604  1.28     tron 	v |= HME_ERX_CFG_DMAENABLE;
    605   1.2       pk 	bus_space_write_4(t, erx, HME_ERXI_CFG, v);
    606   1.1       pk 
    607   1.1       pk 	/* step 11. XIF Configuration */
    608   1.1       pk 	v = bus_space_read_4(t, mac, HME_MACI_XIF);
    609   1.1       pk 	v |= HME_MAC_XIF_OE;
    610   1.1       pk 	bus_space_write_4(t, mac, HME_MACI_XIF, v);
    611   1.1       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.29  thorpej 
    628  1.29  thorpej 	/* Set the current media. */
    629  1.29  thorpej 	mii_mediachg(&sc->sc_mii);
    630   1.9  thorpej 
    631   1.9  thorpej 	/* Start the one second timer. */
    632   1.9  thorpej 	callout_reset(&sc->sc_tick_ch, hz, hme_tick, sc);
    633   1.1       pk 
    634   1.1       pk 	ifp->if_flags |= IFF_RUNNING;
    635   1.1       pk 	ifp->if_flags &= ~IFF_OACTIVE;
    636   1.1       pk 	ifp->if_timer = 0;
    637   1.1       pk 	hme_start(ifp);
    638   1.1       pk }
    639   1.1       pk 
    640  1.28     tron /*
    641  1.28     tron  * Compare two Ether/802 addresses for equality, inlined and unrolled for
    642  1.28     tron  * speed.
    643  1.28     tron  */
    644  1.28     tron static __inline__ int
    645  1.28     tron ether_cmp(a, b)
    646  1.28     tron 	u_char *a, *b;
    647  1.28     tron {
    648  1.28     tron 
    649  1.28     tron 	if (a[5] != b[5] || a[4] != b[4] || a[3] != b[3] ||
    650  1.28     tron 	    a[2] != b[2] || a[1] != b[1] || a[0] != b[0])
    651  1.28     tron 		return (0);
    652  1.28     tron 	return (1);
    653  1.28     tron }
    654  1.28     tron 
    655  1.28     tron 
    656  1.28     tron /*
    657  1.28     tron  * Routine to copy from mbuf chain to transmit buffer in
    658  1.28     tron  * network buffer memory.
    659  1.28     tron  * Returns the amount of data copied.
    660  1.28     tron  */
    661  1.28     tron int
    662  1.28     tron hme_put(sc, ri, m)
    663  1.28     tron 	struct hme_softc *sc;
    664  1.28     tron 	int ri;			/* Ring index */
    665  1.28     tron 	struct mbuf *m;
    666  1.28     tron {
    667  1.28     tron 	struct mbuf *n;
    668  1.28     tron 	int len, tlen = 0;
    669  1.28     tron 	caddr_t bp;
    670  1.28     tron 
    671  1.28     tron 	bp = sc->sc_rb.rb_txbuf + (ri % sc->sc_rb.rb_ntbuf) * _HME_BUFSZ;
    672  1.28     tron 	for (; m; m = n) {
    673  1.28     tron 		len = m->m_len;
    674  1.28     tron 		if (len == 0) {
    675  1.28     tron 			MFREE(m, n);
    676  1.28     tron 			continue;
    677  1.28     tron 		}
    678  1.28     tron 		memcpy(bp, mtod(m, caddr_t), len);
    679  1.28     tron 		bp += len;
    680  1.28     tron 		tlen += len;
    681  1.28     tron 		MFREE(m, n);
    682  1.28     tron 	}
    683  1.28     tron 	return (tlen);
    684  1.28     tron }
    685  1.28     tron 
    686  1.28     tron /*
    687  1.28     tron  * Pull data off an interface.
    688  1.28     tron  * Len is length of data, with local net header stripped.
    689  1.28     tron  * We copy the data into mbufs.  When full cluster sized units are present
    690  1.28     tron  * we copy into clusters.
    691  1.28     tron  */
    692  1.28     tron struct mbuf *
    693  1.28     tron hme_get(sc, ri, totlen)
    694  1.28     tron 	struct hme_softc *sc;
    695  1.28     tron 	int ri, totlen;
    696  1.28     tron {
    697  1.28     tron 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    698  1.28     tron 	struct mbuf *m, *m0, *newm;
    699  1.28     tron 	caddr_t bp;
    700  1.28     tron 	int len;
    701  1.28     tron 
    702  1.28     tron 	MGETHDR(m0, M_DONTWAIT, MT_DATA);
    703  1.28     tron 	if (m0 == 0)
    704  1.28     tron 		return (0);
    705  1.28     tron 	m0->m_pkthdr.rcvif = ifp;
    706  1.28     tron 	m0->m_pkthdr.len = totlen;
    707  1.28     tron 	len = MHLEN;
    708  1.28     tron 	m = m0;
    709  1.28     tron 
    710  1.28     tron 	bp = sc->sc_rb.rb_rxbuf + (ri % sc->sc_rb.rb_nrbuf) * _HME_BUFSZ;
    711  1.28     tron 
    712  1.28     tron 	while (totlen > 0) {
    713  1.28     tron 		if (totlen >= MINCLSIZE) {
    714  1.28     tron 			MCLGET(m, M_DONTWAIT);
    715  1.28     tron 			if ((m->m_flags & M_EXT) == 0)
    716  1.28     tron 				goto bad;
    717  1.28     tron 			len = MCLBYTES;
    718  1.28     tron 		}
    719  1.28     tron 
    720  1.28     tron 		if (m == m0) {
    721  1.28     tron 			caddr_t newdata = (caddr_t)
    722  1.28     tron 			    ALIGN(m->m_data + sizeof(struct ether_header)) -
    723  1.28     tron 			    sizeof(struct ether_header);
    724  1.28     tron 			len -= newdata - m->m_data;
    725  1.28     tron 			m->m_data = newdata;
    726  1.28     tron 		}
    727  1.28     tron 
    728  1.28     tron 		m->m_len = len = min(totlen, len);
    729  1.28     tron 		memcpy(mtod(m, caddr_t), bp, len);
    730  1.28     tron 		bp += len;
    731  1.28     tron 
    732  1.28     tron 		totlen -= len;
    733  1.28     tron 		if (totlen > 0) {
    734  1.28     tron 			MGET(newm, M_DONTWAIT, MT_DATA);
    735  1.28     tron 			if (newm == 0)
    736  1.28     tron 				goto bad;
    737  1.28     tron 			len = MLEN;
    738  1.28     tron 			m = m->m_next = newm;
    739  1.28     tron 		}
    740  1.28     tron 	}
    741  1.28     tron 
    742  1.28     tron 	return (m0);
    743  1.28     tron 
    744  1.28     tron bad:
    745  1.28     tron 	m_freem(m0);
    746  1.28     tron 	return (0);
    747  1.28     tron }
    748  1.28     tron 
    749  1.28     tron /*
    750  1.28     tron  * Pass a packet to the higher levels.
    751  1.28     tron  */
    752  1.28     tron void
    753  1.28     tron hme_read(sc, ix, len)
    754  1.28     tron 	struct hme_softc *sc;
    755  1.28     tron 	int ix, len;
    756  1.28     tron {
    757  1.28     tron 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    758  1.28     tron 	struct mbuf *m;
    759  1.28     tron 
    760  1.28     tron 	if (len <= sizeof(struct ether_header) ||
    761  1.28     tron 	    len > ((sc->sc_ethercom.ec_capenable & ETHERCAP_VLAN_MTU) ?
    762  1.28     tron 	    ETHER_VLAN_ENCAP_LEN + ETHERMTU + sizeof(struct ether_header) :
    763  1.28     tron 	    ETHERMTU + sizeof(struct ether_header))) {
    764  1.28     tron #ifdef HMEDEBUG
    765  1.28     tron 		printf("%s: invalid packet size %d; dropping\n",
    766  1.28     tron 		    sc->sc_dev.dv_xname, len);
    767  1.28     tron #endif
    768  1.28     tron 		ifp->if_ierrors++;
    769  1.28     tron 		return;
    770  1.28     tron 	}
    771  1.28     tron 
    772  1.28     tron 	/* Pull packet off interface. */
    773  1.28     tron 	m = hme_get(sc, ix, len);
    774  1.28     tron 	if (m == 0) {
    775  1.28     tron 		ifp->if_ierrors++;
    776  1.28     tron 		return;
    777  1.28     tron 	}
    778  1.28     tron 
    779  1.28     tron 	ifp->if_ipackets++;
    780  1.28     tron 
    781  1.28     tron #if NBPFILTER > 0
    782  1.28     tron 	/*
    783  1.28     tron 	 * Check if there's a BPF listener on this interface.
    784  1.28     tron 	 * If so, hand off the raw packet to BPF.
    785  1.28     tron 	 */
    786  1.28     tron 	if (ifp->if_bpf)
    787  1.28     tron 		bpf_mtap(ifp->if_bpf, m);
    788  1.28     tron #endif
    789  1.28     tron 
    790  1.28     tron 	/* Pass the packet up. */
    791  1.28     tron 	(*ifp->if_input)(ifp, m);
    792  1.28     tron }
    793  1.28     tron 
    794   1.1       pk void
    795   1.1       pk hme_start(ifp)
    796   1.1       pk 	struct ifnet *ifp;
    797   1.1       pk {
    798   1.1       pk 	struct hme_softc *sc = (struct hme_softc *)ifp->if_softc;
    799  1.28     tron 	caddr_t txd = sc->sc_rb.rb_txd;
    800   1.1       pk 	struct mbuf *m;
    801  1.28     tron 	unsigned int ri, len;
    802  1.28     tron 	unsigned int ntbuf = sc->sc_rb.rb_ntbuf;
    803   1.1       pk 
    804   1.1       pk 	if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
    805   1.1       pk 		return;
    806   1.1       pk 
    807  1.28     tron 	ri = sc->sc_rb.rb_tdhead;
    808  1.28     tron 
    809  1.28     tron 	for (;;) {
    810  1.28     tron 		IFQ_DEQUEUE(&ifp->if_snd, m);
    811  1.28     tron 		if (m == 0)
    812   1.1       pk 			break;
    813   1.1       pk 
    814   1.1       pk #if NBPFILTER > 0
    815   1.1       pk 		/*
    816   1.1       pk 		 * If BPF is listening on this interface, let it see the
    817   1.1       pk 		 * packet before we commit it to the wire.
    818   1.1       pk 		 */
    819   1.1       pk 		if (ifp->if_bpf)
    820   1.1       pk 			bpf_mtap(ifp->if_bpf, m);
    821   1.1       pk #endif
    822   1.1       pk 
    823  1.28     tron 		/*
    824  1.28     tron 		 * Copy the mbuf chain into the transmit buffer.
    825  1.28     tron 		 */
    826  1.28     tron 		len = hme_put(sc, ri, m);
    827  1.28     tron 
    828  1.28     tron 		/*
    829  1.28     tron 		 * Initialize transmit registers and start transmission
    830  1.28     tron 		 */
    831  1.28     tron 		HME_XD_SETFLAGS(sc->sc_pci, txd, ri,
    832  1.28     tron 			HME_XD_OWN | HME_XD_SOP | HME_XD_EOP |
    833  1.28     tron 			HME_XD_ENCODE_TSIZE(len));
    834  1.28     tron 
    835  1.28     tron 		/*if (sc->sc_rb.rb_td_nbusy <= 0)*/
    836  1.28     tron 		bus_space_write_4(sc->sc_bustag, sc->sc_etx, HME_ETXI_PENDING,
    837  1.28     tron 				  HME_ETX_TP_DMAWAKEUP);
    838  1.28     tron 
    839  1.28     tron 		if (++ri == ntbuf)
    840  1.28     tron 			ri = 0;
    841  1.28     tron 
    842  1.28     tron 		if (++sc->sc_rb.rb_td_nbusy == ntbuf) {
    843  1.26     tron 			ifp->if_flags |= IFF_OACTIVE;
    844  1.26     tron 			break;
    845  1.26     tron 		}
    846   1.1       pk 	}
    847   1.1       pk 
    848  1.28     tron 	sc->sc_rb.rb_tdhead = ri;
    849   1.1       pk }
    850   1.1       pk 
    851   1.1       pk /*
    852   1.1       pk  * Transmit interrupt.
    853   1.1       pk  */
    854   1.1       pk int
    855   1.1       pk hme_tint(sc)
    856   1.1       pk 	struct hme_softc *sc;
    857   1.1       pk {
    858   1.1       pk 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    859  1.28     tron 	bus_space_tag_t t = sc->sc_bustag;
    860  1.28     tron 	bus_space_handle_t mac = sc->sc_mac;
    861   1.1       pk 	unsigned int ri, txflags;
    862  1.28     tron 
    863  1.28     tron 	/*
    864  1.28     tron 	 * Unload collision counters
    865  1.28     tron 	 */
    866  1.28     tron 	ifp->if_collisions +=
    867  1.28     tron 		bus_space_read_4(t, mac, HME_MACI_NCCNT) +
    868  1.28     tron 		bus_space_read_4(t, mac, HME_MACI_FCCNT) +
    869  1.28     tron 		bus_space_read_4(t, mac, HME_MACI_EXCNT) +
    870  1.28     tron 		bus_space_read_4(t, mac, HME_MACI_LTCNT);
    871  1.28     tron 
    872  1.28     tron 	/*
    873  1.28     tron 	 * then clear the hardware counters.
    874  1.28     tron 	 */
    875  1.28     tron 	bus_space_write_4(t, mac, HME_MACI_NCCNT, 0);
    876  1.28     tron 	bus_space_write_4(t, mac, HME_MACI_FCCNT, 0);
    877  1.28     tron 	bus_space_write_4(t, mac, HME_MACI_EXCNT, 0);
    878  1.28     tron 	bus_space_write_4(t, mac, HME_MACI_LTCNT, 0);
    879   1.1       pk 
    880   1.1       pk 	/* Fetch current position in the transmit ring */
    881  1.28     tron 	ri = sc->sc_rb.rb_tdtail;
    882   1.1       pk 
    883   1.1       pk 	for (;;) {
    884  1.28     tron 		if (sc->sc_rb.rb_td_nbusy <= 0)
    885   1.1       pk 			break;
    886   1.1       pk 
    887  1.15      eeh 		txflags = HME_XD_GETFLAGS(sc->sc_pci, sc->sc_rb.rb_txd, ri);
    888   1.1       pk 
    889   1.1       pk 		if (txflags & HME_XD_OWN)
    890   1.1       pk 			break;
    891   1.1       pk 
    892   1.1       pk 		ifp->if_flags &= ~IFF_OACTIVE;
    893  1.28     tron 		ifp->if_opackets++;
    894  1.26     tron 
    895  1.28     tron 		if (++ri == sc->sc_rb.rb_ntbuf)
    896   1.1       pk 			ri = 0;
    897   1.1       pk 
    898  1.28     tron 		--sc->sc_rb.rb_td_nbusy;
    899   1.1       pk 	}
    900   1.1       pk 
    901   1.3       pk 	/* Update ring */
    902  1.28     tron 	sc->sc_rb.rb_tdtail = ri;
    903   1.1       pk 
    904   1.1       pk 	hme_start(ifp);
    905   1.1       pk 
    906  1.28     tron 	if (sc->sc_rb.rb_td_nbusy == 0)
    907   1.1       pk 		ifp->if_timer = 0;
    908   1.1       pk 
    909   1.1       pk 	return (1);
    910   1.1       pk }
    911   1.1       pk 
    912   1.1       pk /*
    913   1.1       pk  * Receive interrupt.
    914   1.1       pk  */
    915   1.1       pk int
    916   1.1       pk hme_rint(sc)
    917   1.1       pk 	struct hme_softc *sc;
    918   1.1       pk {
    919  1.28     tron 	caddr_t xdr = sc->sc_rb.rb_rxd;
    920  1.28     tron 	unsigned int nrbuf = sc->sc_rb.rb_nrbuf;
    921   1.1       pk 	unsigned int ri, len;
    922   1.1       pk 	u_int32_t flags;
    923   1.1       pk 
    924  1.28     tron 	ri = sc->sc_rb.rb_rdtail;
    925   1.1       pk 
    926   1.1       pk 	/*
    927   1.1       pk 	 * Process all buffers with valid data.
    928   1.1       pk 	 */
    929   1.1       pk 	for (;;) {
    930  1.28     tron 		flags = HME_XD_GETFLAGS(sc->sc_pci, xdr, ri);
    931   1.1       pk 		if (flags & HME_XD_OWN)
    932   1.1       pk 			break;
    933   1.1       pk 
    934   1.4       pk 		if (flags & HME_XD_OFL) {
    935   1.4       pk 			printf("%s: buffer overflow, ri=%d; flags=0x%x\n",
    936  1.28     tron 					sc->sc_dev.dv_xname, ri, flags);
    937  1.28     tron 		} else {
    938  1.28     tron 			len = HME_XD_DECODE_RSIZE(flags);
    939  1.28     tron 			hme_read(sc, ri, len);
    940   1.4       pk 		}
    941   1.1       pk 
    942  1.28     tron 		/* This buffer can be used by the hardware again */
    943  1.28     tron 		HME_XD_SETFLAGS(sc->sc_pci, xdr, ri,
    944  1.28     tron 				HME_XD_OWN | HME_XD_ENCODE_RSIZE(_HME_BUFSZ));
    945  1.26     tron 
    946  1.28     tron 		if (++ri == nrbuf)
    947   1.1       pk 			ri = 0;
    948   1.1       pk 	}
    949   1.1       pk 
    950  1.28     tron 	sc->sc_rb.rb_rdtail = ri;
    951  1.28     tron 
    952   1.1       pk 	return (1);
    953   1.1       pk }
    954   1.1       pk 
    955   1.1       pk int
    956   1.1       pk hme_eint(sc, status)
    957   1.1       pk 	struct hme_softc *sc;
    958   1.1       pk 	u_int status;
    959   1.1       pk {
    960   1.1       pk 	char bits[128];
    961   1.1       pk 
    962   1.1       pk 	if ((status & HME_SEB_STAT_MIFIRQ) != 0) {
    963  1.33       pk 		bus_space_tag_t t = sc->sc_bustag;
    964  1.33       pk 		bus_space_handle_t mif = sc->sc_mif;
    965  1.33       pk 		u_int32_t cf, st, sm;
    966  1.33       pk 		cf = bus_space_read_4(t, mif, HME_MIFI_CFG);
    967  1.33       pk 		st = bus_space_read_4(t, mif, HME_MIFI_STAT);
    968  1.33       pk 		sm = bus_space_read_4(t, mif, HME_MIFI_SM);
    969  1.33       pk 		printf("%s: XXXlink status changed: cfg=%x, stat %x, sm %x\n",
    970  1.33       pk 			sc->sc_dev.dv_xname, cf, st, sm);
    971   1.1       pk 		return (1);
    972   1.1       pk 	}
    973   1.1       pk 
    974   1.1       pk 	printf("%s: status=%s\n", sc->sc_dev.dv_xname,
    975  1.28     tron 		bitmask_snprintf(status, HME_SEB_STAT_BITS, bits,sizeof(bits)));
    976   1.1       pk 	return (1);
    977   1.1       pk }
    978   1.1       pk 
    979   1.1       pk int
    980   1.1       pk hme_intr(v)
    981   1.1       pk 	void *v;
    982   1.1       pk {
    983   1.1       pk 	struct hme_softc *sc = (struct hme_softc *)v;
    984   1.1       pk 	bus_space_tag_t t = sc->sc_bustag;
    985   1.1       pk 	bus_space_handle_t seb = sc->sc_seb;
    986   1.1       pk 	u_int32_t status;
    987   1.1       pk 	int r = 0;
    988   1.1       pk 
    989   1.1       pk 	status = bus_space_read_4(t, seb, HME_SEBI_STAT);
    990   1.1       pk 
    991   1.1       pk 	if ((status & HME_SEB_STAT_ALL_ERRORS) != 0)
    992   1.1       pk 		r |= hme_eint(sc, status);
    993   1.1       pk 
    994   1.1       pk 	if ((status & (HME_SEB_STAT_TXALL | HME_SEB_STAT_HOSTTOTX)) != 0)
    995   1.1       pk 		r |= hme_tint(sc);
    996   1.1       pk 
    997   1.1       pk 	if ((status & HME_SEB_STAT_RXTOHOST) != 0)
    998   1.1       pk 		r |= hme_rint(sc);
    999   1.1       pk 
   1000   1.1       pk 	return (r);
   1001   1.1       pk }
   1002   1.1       pk 
   1003   1.1       pk 
   1004   1.1       pk void
   1005   1.1       pk hme_watchdog(ifp)
   1006   1.1       pk 	struct ifnet *ifp;
   1007   1.1       pk {
   1008   1.1       pk 	struct hme_softc *sc = ifp->if_softc;
   1009   1.1       pk 
   1010   1.1       pk 	log(LOG_ERR, "%s: device timeout\n", sc->sc_dev.dv_xname);
   1011   1.1       pk 	++ifp->if_oerrors;
   1012   1.1       pk 
   1013   1.1       pk 	hme_reset(sc);
   1014   1.4       pk }
   1015   1.4       pk 
   1016   1.4       pk /*
   1017   1.4       pk  * Initialize the MII Management Interface
   1018   1.4       pk  */
   1019   1.4       pk void
   1020   1.4       pk hme_mifinit(sc)
   1021   1.4       pk 	struct hme_softc *sc;
   1022   1.4       pk {
   1023   1.4       pk 	bus_space_tag_t t = sc->sc_bustag;
   1024   1.4       pk 	bus_space_handle_t mif = sc->sc_mif;
   1025  1.35       pk 	bus_space_handle_t mac = sc->sc_mac;
   1026  1.33       pk 	int instance, phy;
   1027   1.4       pk 	u_int32_t v;
   1028   1.4       pk 
   1029  1.33       pk 	if (sc->sc_media.ifm_cur != NULL) {
   1030  1.33       pk 		instance = IFM_INST(sc->sc_media.ifm_cur->ifm_media);
   1031  1.33       pk 		phy = sc->sc_phys[instance];
   1032  1.33       pk 	} else
   1033  1.33       pk 		/* No media set yet, pick phy arbitrarily.. */
   1034  1.33       pk 		phy = HME_PHYAD_EXTERNAL;
   1035  1.33       pk 
   1036  1.33       pk 	/* Configure the MIF in frame mode, no poll, current phy select */
   1037  1.33       pk 	v = 0;
   1038  1.33       pk 	if (phy == HME_PHYAD_EXTERNAL)
   1039  1.33       pk 		v |= HME_MIF_CFG_PHY;
   1040   1.4       pk 	bus_space_write_4(t, mif, HME_MIFI_CFG, v);
   1041  1.35       pk 
   1042  1.35       pk 	/* If an external transceiver is selected, enable its MII drivers */
   1043  1.35       pk 	v = bus_space_read_4(t, mac, HME_MACI_XIF);
   1044  1.35       pk 	v &= ~HME_MAC_XIF_MIIENABLE;
   1045  1.35       pk 	if (phy == HME_PHYAD_EXTERNAL)
   1046  1.35       pk 		v |= HME_MAC_XIF_MIIENABLE;
   1047  1.35       pk 	bus_space_write_4(t, mac, HME_MACI_XIF, v);
   1048   1.1       pk }
   1049   1.1       pk 
   1050   1.1       pk /*
   1051   1.1       pk  * MII interface
   1052   1.1       pk  */
   1053   1.1       pk static int
   1054   1.1       pk hme_mii_readreg(self, phy, reg)
   1055   1.1       pk 	struct device *self;
   1056   1.1       pk 	int phy, reg;
   1057   1.1       pk {
   1058   1.1       pk 	struct hme_softc *sc = (void *)self;
   1059   1.1       pk 	bus_space_tag_t t = sc->sc_bustag;
   1060   1.1       pk 	bus_space_handle_t mif = sc->sc_mif;
   1061  1.35       pk 	bus_space_handle_t mac = sc->sc_mac;
   1062  1.35       pk 	u_int32_t v, xif_cfg, mifi_cfg;
   1063   1.1       pk 	int n;
   1064   1.1       pk 
   1065  1.33       pk 	/* We can at most have two PHYs */
   1066  1.33       pk 	if (phy != HME_PHYAD_EXTERNAL && phy != HME_PHYAD_INTERNAL)
   1067  1.32   martin 		return (0);
   1068  1.32   martin 
   1069   1.5       pk 	/* Select the desired PHY in the MIF configuration register */
   1070  1.33       pk 	v = mifi_cfg = bus_space_read_4(t, mif, HME_MIFI_CFG);
   1071   1.5       pk 	v &= ~HME_MIF_CFG_PHY;
   1072   1.5       pk 	if (phy == HME_PHYAD_EXTERNAL)
   1073   1.5       pk 		v |= HME_MIF_CFG_PHY;
   1074   1.5       pk 	bus_space_write_4(t, mif, HME_MIFI_CFG, v);
   1075   1.5       pk 
   1076  1.35       pk 	/* Enable MII drivers on external transceiver */
   1077  1.35       pk 	v = xif_cfg = bus_space_read_4(t, mac, HME_MACI_XIF);
   1078  1.35       pk 	if (phy == HME_PHYAD_EXTERNAL)
   1079  1.35       pk 		v |= HME_MAC_XIF_MIIENABLE;
   1080  1.35       pk 	else
   1081  1.35       pk 		v &= ~HME_MAC_XIF_MIIENABLE;
   1082  1.35       pk 	bus_space_write_4(t, mac, HME_MACI_XIF, v);
   1083  1.35       pk 
   1084  1.33       pk #if 0
   1085  1.33       pk /* This doesn't work reliably; the MDIO_1 bit is off most of the time */
   1086  1.33       pk 	/*
   1087  1.33       pk 	 * Check whether a transceiver is connected by testing
   1088  1.33       pk 	 * the MIF configuration register's MDI_X bits. Note that
   1089  1.33       pk 	 * MDI_0 (int) == 0x100 and MDI_1 (ext) == 0x200; see hmereg.h
   1090  1.33       pk 	 */
   1091  1.33       pk 	mif_mdi_bit = 1 << (8 + (1 - phy));
   1092  1.33       pk 	delay(100);
   1093  1.33       pk 	v = bus_space_read_4(t, mif, HME_MIFI_CFG);
   1094  1.33       pk 	if ((v & mif_mdi_bit) == 0)
   1095  1.33       pk 		return (0);
   1096  1.33       pk #endif
   1097  1.33       pk 
   1098   1.1       pk 	/* Construct the frame command */
   1099   1.1       pk 	v = (MII_COMMAND_START << HME_MIF_FO_ST_SHIFT) |
   1100   1.1       pk 	    HME_MIF_FO_TAMSB |
   1101   1.1       pk 	    (MII_COMMAND_READ << HME_MIF_FO_OPC_SHIFT) |
   1102   1.1       pk 	    (phy << HME_MIF_FO_PHYAD_SHIFT) |
   1103   1.1       pk 	    (reg << HME_MIF_FO_REGAD_SHIFT);
   1104   1.1       pk 
   1105   1.1       pk 	bus_space_write_4(t, mif, HME_MIFI_FO, v);
   1106   1.1       pk 	for (n = 0; n < 100; n++) {
   1107   1.2       pk 		DELAY(1);
   1108   1.1       pk 		v = bus_space_read_4(t, mif, HME_MIFI_FO);
   1109  1.33       pk 		if (v & HME_MIF_FO_TALSB) {
   1110  1.33       pk 			v &= HME_MIF_FO_DATA;
   1111  1.33       pk 			goto out;
   1112  1.33       pk 		}
   1113   1.1       pk 	}
   1114   1.1       pk 
   1115  1.33       pk 	v = 0;
   1116   1.1       pk 	printf("%s: mii_read timeout\n", sc->sc_dev.dv_xname);
   1117  1.33       pk 
   1118  1.33       pk out:
   1119  1.33       pk 	/* Restore MIFI_CFG register */
   1120  1.33       pk 	bus_space_write_4(t, mif, HME_MIFI_CFG, mifi_cfg);
   1121  1.35       pk 	/* Restore XIF register */
   1122  1.35       pk 	bus_space_write_4(t, mac, HME_MACI_XIF, xif_cfg);
   1123  1.33       pk 	return (v);
   1124   1.1       pk }
   1125   1.1       pk 
   1126   1.1       pk static void
   1127   1.1       pk hme_mii_writereg(self, phy, reg, val)
   1128   1.1       pk 	struct device *self;
   1129   1.1       pk 	int phy, reg, val;
   1130   1.1       pk {
   1131   1.1       pk 	struct hme_softc *sc = (void *)self;
   1132   1.1       pk 	bus_space_tag_t t = sc->sc_bustag;
   1133   1.1       pk 	bus_space_handle_t mif = sc->sc_mif;
   1134  1.35       pk 	bus_space_handle_t mac = sc->sc_mac;
   1135  1.35       pk 	u_int32_t v, xif_cfg, mifi_cfg;
   1136   1.1       pk 	int n;
   1137  1.32   martin 
   1138  1.33       pk 	/* We can at most have two PHYs */
   1139  1.33       pk 	if (phy != HME_PHYAD_EXTERNAL && phy != HME_PHYAD_INTERNAL)
   1140  1.32   martin 		return;
   1141   1.1       pk 
   1142   1.5       pk 	/* Select the desired PHY in the MIF configuration register */
   1143  1.33       pk 	v = mifi_cfg = bus_space_read_4(t, mif, HME_MIFI_CFG);
   1144   1.5       pk 	v &= ~HME_MIF_CFG_PHY;
   1145   1.5       pk 	if (phy == HME_PHYAD_EXTERNAL)
   1146   1.5       pk 		v |= HME_MIF_CFG_PHY;
   1147   1.5       pk 	bus_space_write_4(t, mif, HME_MIFI_CFG, v);
   1148   1.5       pk 
   1149  1.35       pk 	/* Enable MII drivers on external transceiver */
   1150  1.35       pk 	v = xif_cfg = bus_space_read_4(t, mac, HME_MACI_XIF);
   1151  1.35       pk 	if (phy == HME_PHYAD_EXTERNAL)
   1152  1.35       pk 		v |= HME_MAC_XIF_MIIENABLE;
   1153  1.35       pk 	else
   1154  1.35       pk 		v &= ~HME_MAC_XIF_MIIENABLE;
   1155  1.35       pk 	bus_space_write_4(t, mac, HME_MACI_XIF, v);
   1156  1.35       pk 
   1157  1.33       pk #if 0
   1158  1.33       pk /* This doesn't work reliably; the MDIO_1 bit is off most of the time */
   1159  1.33       pk 	/*
   1160  1.33       pk 	 * Check whether a transceiver is connected by testing
   1161  1.33       pk 	 * the MIF configuration register's MDI_X bits. Note that
   1162  1.33       pk 	 * MDI_0 (int) == 0x100 and MDI_1 (ext) == 0x200; see hmereg.h
   1163  1.33       pk 	 */
   1164  1.33       pk 	mif_mdi_bit = 1 << (8 + (1 - phy));
   1165  1.33       pk 	delay(100);
   1166  1.33       pk 	v = bus_space_read_4(t, mif, HME_MIFI_CFG);
   1167  1.33       pk 	if ((v & mif_mdi_bit) == 0)
   1168  1.33       pk 		return;
   1169  1.33       pk #endif
   1170  1.33       pk 
   1171   1.1       pk 	/* Construct the frame command */
   1172   1.1       pk 	v = (MII_COMMAND_START << HME_MIF_FO_ST_SHIFT)	|
   1173   1.1       pk 	    HME_MIF_FO_TAMSB				|
   1174   1.1       pk 	    (MII_COMMAND_WRITE << HME_MIF_FO_OPC_SHIFT)	|
   1175   1.1       pk 	    (phy << HME_MIF_FO_PHYAD_SHIFT)		|
   1176   1.1       pk 	    (reg << HME_MIF_FO_REGAD_SHIFT)		|
   1177   1.1       pk 	    (val & HME_MIF_FO_DATA);
   1178   1.1       pk 
   1179   1.1       pk 	bus_space_write_4(t, mif, HME_MIFI_FO, v);
   1180   1.1       pk 	for (n = 0; n < 100; n++) {
   1181   1.2       pk 		DELAY(1);
   1182   1.1       pk 		v = bus_space_read_4(t, mif, HME_MIFI_FO);
   1183   1.1       pk 		if (v & HME_MIF_FO_TALSB)
   1184  1.33       pk 			goto out;
   1185   1.1       pk 	}
   1186   1.1       pk 
   1187   1.2       pk 	printf("%s: mii_write timeout\n", sc->sc_dev.dv_xname);
   1188  1.33       pk out:
   1189  1.33       pk 	/* Restore MIFI_CFG register */
   1190  1.33       pk 	bus_space_write_4(t, mif, HME_MIFI_CFG, mifi_cfg);
   1191  1.35       pk 	/* Restore XIF register */
   1192  1.35       pk 	bus_space_write_4(t, mac, HME_MACI_XIF, xif_cfg);
   1193   1.1       pk }
   1194   1.1       pk 
   1195   1.1       pk static void
   1196   1.1       pk hme_mii_statchg(dev)
   1197   1.1       pk 	struct device *dev;
   1198   1.1       pk {
   1199   1.3       pk 	struct hme_softc *sc = (void *)dev;
   1200   1.1       pk 	bus_space_tag_t t = sc->sc_bustag;
   1201   1.1       pk 	bus_space_handle_t mac = sc->sc_mac;
   1202   1.1       pk 	u_int32_t v;
   1203   1.1       pk 
   1204   1.5       pk #ifdef HMEDEBUG
   1205   1.5       pk 	if (sc->sc_debug)
   1206  1.33       pk 		printf("hme_mii_statchg: status change\n");
   1207   1.5       pk #endif
   1208   1.1       pk 
   1209   1.5       pk 	/* Set the MAC Full Duplex bit appropriately */
   1210  1.30   martin 	/* Apparently the hme chip is SIMPLEX if working in full duplex mode,
   1211  1.30   martin 	   but not otherwise. */
   1212   1.1       pk 	v = bus_space_read_4(t, mac, HME_MACI_TXCFG);
   1213  1.30   martin 	if ((IFM_OPTIONS(sc->sc_mii.mii_media_active) & IFM_FDX) != 0) {
   1214   1.1       pk 		v |= HME_MAC_TXCFG_FULLDPLX;
   1215  1.30   martin 		sc->sc_ethercom.ec_if.if_flags |= IFF_SIMPLEX;
   1216  1.30   martin 	} else {
   1217   1.1       pk 		v &= ~HME_MAC_TXCFG_FULLDPLX;
   1218  1.30   martin 		sc->sc_ethercom.ec_if.if_flags &= ~IFF_SIMPLEX;
   1219  1.30   martin 	}
   1220   1.1       pk 	bus_space_write_4(t, mac, HME_MACI_TXCFG, v);
   1221   1.5       pk }
   1222   1.5       pk 
   1223   1.5       pk int
   1224   1.5       pk hme_mediachange(ifp)
   1225   1.5       pk 	struct ifnet *ifp;
   1226   1.5       pk {
   1227   1.5       pk 	struct hme_softc *sc = ifp->if_softc;
   1228  1.33       pk 	bus_space_tag_t t = sc->sc_bustag;
   1229  1.33       pk 	bus_space_handle_t mif = sc->sc_mif;
   1230  1.33       pk 	bus_space_handle_t mac = sc->sc_mac;
   1231  1.33       pk 	int instance = IFM_INST(sc->sc_mii.mii_media.ifm_cur->ifm_media);
   1232  1.33       pk 	int phy = sc->sc_phys[instance];
   1233  1.33       pk 	u_int32_t v;
   1234   1.5       pk 
   1235  1.33       pk #ifdef HMEDEBUG
   1236  1.33       pk 	if (sc->sc_debug)
   1237  1.33       pk 		printf("hme_mediachange: phy = %d\n", phy);
   1238  1.33       pk #endif
   1239   1.5       pk 	if (IFM_TYPE(sc->sc_media.ifm_media) != IFM_ETHER)
   1240   1.5       pk 		return (EINVAL);
   1241  1.33       pk 
   1242  1.33       pk 	/* Select the current PHY in the MIF configuration register */
   1243  1.33       pk 	v = bus_space_read_4(t, mif, HME_MIFI_CFG);
   1244  1.33       pk 	v &= ~HME_MIF_CFG_PHY;
   1245  1.33       pk 	if (phy == HME_PHYAD_EXTERNAL)
   1246  1.33       pk 		v |= HME_MIF_CFG_PHY;
   1247  1.33       pk 	bus_space_write_4(t, mif, HME_MIFI_CFG, v);
   1248  1.33       pk 
   1249  1.33       pk 	/* If an external transceiver is selected, enable its MII drivers */
   1250  1.33       pk 	v = bus_space_read_4(t, mac, HME_MACI_XIF);
   1251  1.33       pk 	v &= ~HME_MAC_XIF_MIIENABLE;
   1252  1.33       pk 	if (phy == HME_PHYAD_EXTERNAL)
   1253  1.33       pk 		v |= HME_MAC_XIF_MIIENABLE;
   1254  1.33       pk 	bus_space_write_4(t, mac, HME_MACI_XIF, v);
   1255   1.5       pk 
   1256   1.5       pk 	return (mii_mediachg(&sc->sc_mii));
   1257   1.1       pk }
   1258   1.1       pk 
   1259   1.1       pk void
   1260   1.1       pk hme_mediastatus(ifp, ifmr)
   1261   1.1       pk 	struct ifnet *ifp;
   1262   1.1       pk 	struct ifmediareq *ifmr;
   1263   1.1       pk {
   1264   1.1       pk 	struct hme_softc *sc = ifp->if_softc;
   1265   1.1       pk 
   1266   1.1       pk 	if ((ifp->if_flags & IFF_UP) == 0)
   1267   1.1       pk 		return;
   1268   1.1       pk 
   1269   1.1       pk 	mii_pollstat(&sc->sc_mii);
   1270   1.1       pk 	ifmr->ifm_active = sc->sc_mii.mii_media_active;
   1271   1.1       pk 	ifmr->ifm_status = sc->sc_mii.mii_media_status;
   1272   1.1       pk }
   1273   1.1       pk 
   1274   1.1       pk /*
   1275   1.1       pk  * Process an ioctl request.
   1276   1.1       pk  */
   1277   1.1       pk int
   1278   1.1       pk hme_ioctl(ifp, cmd, data)
   1279   1.1       pk 	struct ifnet *ifp;
   1280   1.1       pk 	u_long cmd;
   1281   1.1       pk 	caddr_t data;
   1282   1.1       pk {
   1283   1.1       pk 	struct hme_softc *sc = ifp->if_softc;
   1284   1.1       pk 	struct ifaddr *ifa = (struct ifaddr *)data;
   1285   1.1       pk 	struct ifreq *ifr = (struct ifreq *)data;
   1286   1.1       pk 	int s, error = 0;
   1287   1.1       pk 
   1288   1.1       pk 	s = splnet();
   1289   1.1       pk 
   1290   1.1       pk 	switch (cmd) {
   1291   1.1       pk 
   1292   1.1       pk 	case SIOCSIFADDR:
   1293   1.1       pk 		ifp->if_flags |= IFF_UP;
   1294   1.1       pk 
   1295   1.1       pk 		switch (ifa->ifa_addr->sa_family) {
   1296   1.1       pk #ifdef INET
   1297   1.1       pk 		case AF_INET:
   1298   1.1       pk 			hme_init(sc);
   1299   1.1       pk 			arp_ifinit(ifp, ifa);
   1300   1.1       pk 			break;
   1301   1.1       pk #endif
   1302   1.1       pk #ifdef NS
   1303   1.1       pk 		case AF_NS:
   1304   1.1       pk 		    {
   1305   1.1       pk 			struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
   1306   1.1       pk 
   1307   1.1       pk 			if (ns_nullhost(*ina))
   1308   1.1       pk 				ina->x_host =
   1309   1.1       pk 				    *(union ns_host *)LLADDR(ifp->if_sadl);
   1310   1.1       pk 			else {
   1311  1.21  thorpej 				memcpy(LLADDR(ifp->if_sadl),
   1312  1.21  thorpej 				    ina->x_host.c_host, sizeof(sc->sc_enaddr));
   1313   1.1       pk 			}
   1314   1.1       pk 			/* Set new address. */
   1315   1.1       pk 			hme_init(sc);
   1316   1.1       pk 			break;
   1317   1.1       pk 		    }
   1318   1.1       pk #endif
   1319   1.1       pk 		default:
   1320   1.1       pk 			hme_init(sc);
   1321   1.1       pk 			break;
   1322   1.1       pk 		}
   1323   1.1       pk 		break;
   1324   1.1       pk 
   1325   1.1       pk 	case SIOCSIFFLAGS:
   1326   1.1       pk 		if ((ifp->if_flags & IFF_UP) == 0 &&
   1327   1.1       pk 		    (ifp->if_flags & IFF_RUNNING) != 0) {
   1328   1.1       pk 			/*
   1329   1.1       pk 			 * If interface is marked down and it is running, then
   1330   1.1       pk 			 * stop it.
   1331   1.1       pk 			 */
   1332   1.1       pk 			hme_stop(sc);
   1333   1.1       pk 			ifp->if_flags &= ~IFF_RUNNING;
   1334   1.1       pk 		} else if ((ifp->if_flags & IFF_UP) != 0 &&
   1335   1.1       pk 		    	   (ifp->if_flags & IFF_RUNNING) == 0) {
   1336   1.1       pk 			/*
   1337   1.1       pk 			 * If interface is marked up and it is stopped, then
   1338   1.1       pk 			 * start it.
   1339   1.1       pk 			 */
   1340   1.1       pk 			hme_init(sc);
   1341   1.1       pk 		} else if ((ifp->if_flags & IFF_UP) != 0) {
   1342   1.1       pk 			/*
   1343   1.1       pk 			 * Reset the interface to pick up changes in any other
   1344   1.1       pk 			 * flags that affect hardware registers.
   1345   1.1       pk 			 */
   1346  1.28     tron 			/*hme_stop(sc);*/
   1347   1.1       pk 			hme_init(sc);
   1348   1.1       pk 		}
   1349   1.1       pk #ifdef HMEDEBUG
   1350   1.1       pk 		sc->sc_debug = (ifp->if_flags & IFF_DEBUG) != 0 ? 1 : 0;
   1351   1.1       pk #endif
   1352   1.1       pk 		break;
   1353   1.1       pk 
   1354   1.1       pk 	case SIOCADDMULTI:
   1355   1.1       pk 	case SIOCDELMULTI:
   1356   1.1       pk 		error = (cmd == SIOCADDMULTI) ?
   1357   1.1       pk 		    ether_addmulti(ifr, &sc->sc_ethercom) :
   1358   1.1       pk 		    ether_delmulti(ifr, &sc->sc_ethercom);
   1359   1.1       pk 
   1360   1.1       pk 		if (error == ENETRESET) {
   1361   1.1       pk 			/*
   1362   1.1       pk 			 * Multicast list has changed; set the hardware filter
   1363   1.1       pk 			 * accordingly.
   1364   1.1       pk 			 */
   1365   1.1       pk 			hme_setladrf(sc);
   1366   1.1       pk 			error = 0;
   1367   1.1       pk 		}
   1368   1.1       pk 		break;
   1369   1.1       pk 
   1370   1.1       pk 	case SIOCGIFMEDIA:
   1371   1.1       pk 	case SIOCSIFMEDIA:
   1372   1.1       pk 		error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
   1373   1.1       pk 		break;
   1374   1.1       pk 
   1375   1.1       pk 	default:
   1376   1.1       pk 		error = EINVAL;
   1377   1.1       pk 		break;
   1378   1.1       pk 	}
   1379   1.1       pk 
   1380   1.1       pk 	splx(s);
   1381   1.1       pk 	return (error);
   1382   1.1       pk }
   1383   1.1       pk 
   1384   1.1       pk void
   1385   1.1       pk hme_shutdown(arg)
   1386   1.1       pk 	void *arg;
   1387   1.1       pk {
   1388  1.28     tron 
   1389   1.1       pk 	hme_stop((struct hme_softc *)arg);
   1390   1.1       pk }
   1391   1.1       pk 
   1392   1.1       pk /*
   1393   1.1       pk  * Set up the logical address filter.
   1394   1.1       pk  */
   1395   1.1       pk void
   1396   1.1       pk hme_setladrf(sc)
   1397   1.1       pk 	struct hme_softc *sc;
   1398   1.1       pk {
   1399   1.1       pk 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1400   1.1       pk 	struct ether_multi *enm;
   1401   1.1       pk 	struct ether_multistep step;
   1402  1.28     tron 	struct ethercom *ec = &sc->sc_ethercom;
   1403   1.1       pk 	bus_space_tag_t t = sc->sc_bustag;
   1404   1.1       pk 	bus_space_handle_t mac = sc->sc_mac;
   1405   1.1       pk 	u_char *cp;
   1406   1.1       pk 	u_int32_t crc;
   1407   1.1       pk 	u_int32_t hash[4];
   1408  1.14       pk 	u_int32_t v;
   1409   1.1       pk 	int len;
   1410   1.1       pk 
   1411  1.14       pk 	/* Clear hash table */
   1412  1.14       pk 	hash[3] = hash[2] = hash[1] = hash[0] = 0;
   1413  1.14       pk 
   1414  1.14       pk 	/* Get current RX configuration */
   1415  1.14       pk 	v = bus_space_read_4(t, mac, HME_MACI_RXCFG);
   1416  1.14       pk 
   1417  1.14       pk 	if ((ifp->if_flags & IFF_PROMISC) != 0) {
   1418  1.14       pk 		/* Turn on promiscuous mode; turn off the hash filter */
   1419  1.14       pk 		v |= HME_MAC_RXCFG_PMISC;
   1420  1.14       pk 		v &= ~HME_MAC_RXCFG_HENABLE;
   1421  1.14       pk 		ifp->if_flags |= IFF_ALLMULTI;
   1422  1.14       pk 		goto chipit;
   1423  1.14       pk 	}
   1424  1.14       pk 
   1425  1.14       pk 	/* Turn off promiscuous mode; turn on the hash filter */
   1426  1.14       pk 	v &= ~HME_MAC_RXCFG_PMISC;
   1427  1.14       pk 	v |= HME_MAC_RXCFG_HENABLE;
   1428  1.14       pk 
   1429   1.1       pk 	/*
   1430   1.1       pk 	 * Set up multicast address filter by passing all multicast addresses
   1431   1.1       pk 	 * through a crc generator, and then using the high order 6 bits as an
   1432   1.1       pk 	 * index into the 64 bit logical address filter.  The high order bit
   1433   1.1       pk 	 * selects the word, while the rest of the bits select the bit within
   1434   1.1       pk 	 * the word.
   1435   1.1       pk 	 */
   1436   1.1       pk 
   1437  1.28     tron 	ETHER_FIRST_MULTI(step, ec, enm);
   1438   1.1       pk 	while (enm != NULL) {
   1439  1.28     tron 		if (ether_cmp(enm->enm_addrlo, enm->enm_addrhi)) {
   1440   1.1       pk 			/*
   1441   1.1       pk 			 * We must listen to a range of multicast addresses.
   1442   1.1       pk 			 * For now, just accept all multicasts, rather than
   1443   1.1       pk 			 * trying to set only those filter bits needed to match
   1444   1.1       pk 			 * the range.  (At this time, the only use of address
   1445   1.1       pk 			 * ranges is for IP multicast routing, for which the
   1446   1.1       pk 			 * range is big enough to require all bits set.)
   1447   1.1       pk 			 */
   1448  1.14       pk 			hash[3] = hash[2] = hash[1] = hash[0] = 0xffff;
   1449  1.14       pk 			ifp->if_flags |= IFF_ALLMULTI;
   1450  1.14       pk 			goto chipit;
   1451   1.1       pk 		}
   1452   1.1       pk 
   1453   1.1       pk 		cp = enm->enm_addrlo;
   1454   1.1       pk 		crc = 0xffffffff;
   1455   1.1       pk 		for (len = sizeof(enm->enm_addrlo); --len >= 0;) {
   1456   1.1       pk 			int octet = *cp++;
   1457   1.1       pk 			int i;
   1458   1.1       pk 
   1459   1.1       pk #define MC_POLY_LE	0xedb88320UL	/* mcast crc, little endian */
   1460   1.1       pk 			for (i = 0; i < 8; i++) {
   1461   1.1       pk 				if ((crc & 1) ^ (octet & 1)) {
   1462   1.1       pk 					crc >>= 1;
   1463   1.1       pk 					crc ^= MC_POLY_LE;
   1464   1.1       pk 				} else {
   1465   1.1       pk 					crc >>= 1;
   1466   1.1       pk 				}
   1467   1.1       pk 				octet >>= 1;
   1468   1.1       pk 			}
   1469   1.1       pk 		}
   1470   1.1       pk 		/* Just want the 6 most significant bits. */
   1471   1.1       pk 		crc >>= 26;
   1472   1.1       pk 
   1473   1.1       pk 		/* Set the corresponding bit in the filter. */
   1474   1.1       pk 		hash[crc >> 4] |= 1 << (crc & 0xf);
   1475   1.1       pk 
   1476   1.1       pk 		ETHER_NEXT_MULTI(step, enm);
   1477   1.1       pk 	}
   1478   1.1       pk 
   1479  1.14       pk 	ifp->if_flags &= ~IFF_ALLMULTI;
   1480  1.14       pk 
   1481  1.14       pk chipit:
   1482  1.14       pk 	/* Now load the hash table into the chip */
   1483   1.1       pk 	bus_space_write_4(t, mac, HME_MACI_HASHTAB0, hash[0]);
   1484   1.1       pk 	bus_space_write_4(t, mac, HME_MACI_HASHTAB1, hash[1]);
   1485   1.1       pk 	bus_space_write_4(t, mac, HME_MACI_HASHTAB2, hash[2]);
   1486   1.1       pk 	bus_space_write_4(t, mac, HME_MACI_HASHTAB3, hash[3]);
   1487  1.14       pk 	bus_space_write_4(t, mac, HME_MACI_RXCFG, v);
   1488   1.1       pk }
   1489   1.1       pk 
   1490  1.28     tron /*
   1491  1.28     tron  * Routines for accessing the transmit and receive buffers.
   1492  1.28     tron  * The various CPU and adapter configurations supported by this
   1493  1.28     tron  * driver require three different access methods for buffers
   1494  1.28     tron  * and descriptors:
   1495  1.28     tron  *	(1) contig (contiguous data; no padding),
   1496  1.28     tron  *	(2) gap2 (two bytes of data followed by two bytes of padding),
   1497  1.28     tron  *	(3) gap16 (16 bytes of data followed by 16 bytes of padding).
   1498  1.28     tron  */
   1499  1.28     tron 
   1500  1.28     tron #if 0
   1501  1.28     tron /*
   1502  1.28     tron  * contig: contiguous data with no padding.
   1503  1.28     tron  *
   1504  1.28     tron  * Buffers may have any alignment.
   1505  1.28     tron  */
   1506  1.28     tron 
   1507  1.28     tron void
   1508  1.28     tron hme_copytobuf_contig(sc, from, ri, len)
   1509  1.26     tron 	struct hme_softc *sc;
   1510  1.28     tron 	void *from;
   1511  1.28     tron 	int ri, len;
   1512  1.26     tron {
   1513  1.28     tron 	volatile caddr_t buf = sc->sc_rb.rb_txbuf + (ri * _HME_BUFSZ);
   1514  1.26     tron 
   1515   1.1       pk 	/*
   1516  1.28     tron 	 * Just call memcpy() to do the work.
   1517   1.1       pk 	 */
   1518  1.28     tron 	memcpy(buf, from, len);
   1519   1.1       pk }
   1520   1.1       pk 
   1521  1.28     tron void
   1522  1.28     tron hme_copyfrombuf_contig(sc, to, boff, len)
   1523   1.1       pk 	struct hme_softc *sc;
   1524  1.28     tron 	void *to;
   1525  1.28     tron 	int boff, len;
   1526   1.1       pk {
   1527  1.28     tron 	volatile caddr_t buf = sc->sc_rb.rb_rxbuf + (ri * _HME_BUFSZ);
   1528  1.26     tron 
   1529  1.28     tron 	/*
   1530  1.28     tron 	 * Just call memcpy() to do the work.
   1531  1.28     tron 	 */
   1532  1.28     tron 	memcpy(to, buf, len);
   1533   1.1       pk }
   1534  1.28     tron #endif
   1535