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