Home | History | Annotate | Line # | Download | only in ic
hme.c revision 1.99.2.1
      1  1.99.2.1  christos /*	$NetBSD: hme.c,v 1.99.2.1 2019/06/10 22:07:10 christos 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  *
     19       1.1        pk  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20       1.1        pk  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21       1.1        pk  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22       1.1        pk  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23       1.1        pk  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24       1.1        pk  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25       1.1        pk  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26       1.1        pk  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27       1.1        pk  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28       1.1        pk  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29       1.1        pk  * POSSIBILITY OF SUCH DAMAGE.
     30       1.1        pk  */
     31       1.1        pk 
     32       1.1        pk /*
     33       1.1        pk  * HME Ethernet module driver.
     34       1.1        pk  */
     35      1.25     lukem 
     36      1.25     lukem #include <sys/cdefs.h>
     37  1.99.2.1  christos __KERNEL_RCSID(0, "$NetBSD: hme.c,v 1.99.2.1 2019/06/10 22:07:10 christos Exp $");
     38       1.1        pk 
     39      1.39    petrov /* #define HMEDEBUG */
     40       1.1        pk 
     41       1.1        pk #include "opt_inet.h"
     42       1.1        pk 
     43       1.1        pk #include <sys/param.h>
     44       1.1        pk #include <sys/systm.h>
     45       1.5        pk #include <sys/kernel.h>
     46      1.42      heas #include <sys/mbuf.h>
     47       1.1        pk #include <sys/syslog.h>
     48       1.1        pk #include <sys/socket.h>
     49       1.1        pk #include <sys/device.h>
     50       1.1        pk #include <sys/malloc.h>
     51       1.1        pk #include <sys/ioctl.h>
     52       1.1        pk #include <sys/errno.h>
     53      1.91  riastrad #include <sys/rndsource.h>
     54       1.1        pk 
     55       1.1        pk #include <net/if.h>
     56       1.1        pk #include <net/if_dl.h>
     57       1.1        pk #include <net/if_ether.h>
     58       1.1        pk #include <net/if_media.h>
     59      1.98   msaitoh #include <net/bpf.h>
     60       1.1        pk 
     61       1.1        pk #ifdef INET
     62      1.74   tsutsui #include <net/if_vlanvar.h>
     63       1.1        pk #include <netinet/in.h>
     64       1.1        pk #include <netinet/if_inarp.h>
     65       1.1        pk #include <netinet/in_systm.h>
     66       1.1        pk #include <netinet/in_var.h>
     67       1.1        pk #include <netinet/ip.h>
     68      1.46      heas #include <netinet/tcp.h>
     69      1.46      heas #include <netinet/udp.h>
     70       1.1        pk #endif
     71       1.1        pk 
     72       1.1        pk #include <dev/mii/mii.h>
     73       1.1        pk #include <dev/mii/miivar.h>
     74       1.1        pk 
     75      1.60        ad #include <sys/bus.h>
     76       1.1        pk 
     77       1.1        pk #include <dev/ic/hmereg.h>
     78       1.1        pk #include <dev/ic/hmevar.h>
     79       1.1        pk 
     80      1.81   tsutsui static void	hme_start(struct ifnet *);
     81      1.81   tsutsui static void	hme_stop(struct ifnet *, int);
     82      1.81   tsutsui static int	hme_ioctl(struct ifnet *, u_long, void *);
     83      1.81   tsutsui static void	hme_tick(void *);
     84      1.81   tsutsui static void	hme_watchdog(struct ifnet *);
     85      1.81   tsutsui static bool	hme_shutdown(device_t, int);
     86      1.84  jakllsch static int	hme_init(struct ifnet *);
     87      1.81   tsutsui static void	hme_meminit(struct hme_softc *);
     88      1.81   tsutsui static void	hme_mifinit(struct hme_softc *);
     89  1.99.2.1  christos static void	hme_reset(struct hme_softc *);
     90      1.81   tsutsui static void	hme_chipreset(struct hme_softc *);
     91      1.81   tsutsui static void	hme_setladrf(struct hme_softc *);
     92       1.1        pk 
     93       1.1        pk /* MII methods & callbacks */
     94  1.99.2.1  christos static int	hme_mii_readreg(device_t, int, int, uint16_t *);
     95  1.99.2.1  christos static int	hme_mii_writereg(device_t, int, int, uint16_t);
     96      1.89      matt static void	hme_mii_statchg(struct ifnet *);
     97      1.44     perry 
     98      1.81   tsutsui static int	hme_mediachange(struct ifnet *);
     99      1.44     perry 
    100      1.81   tsutsui static struct mbuf *hme_get(struct hme_softc *, int, uint32_t);
    101      1.81   tsutsui static int	hme_put(struct hme_softc *, int, struct mbuf *);
    102      1.81   tsutsui static void	hme_read(struct hme_softc *, int, uint32_t);
    103      1.81   tsutsui static int	hme_eint(struct hme_softc *, u_int);
    104      1.81   tsutsui static int	hme_rint(struct hme_softc *);
    105      1.81   tsutsui static int	hme_tint(struct hme_softc *);
    106       1.1        pk 
    107      1.81   tsutsui #if 0
    108      1.28      tron /* Default buffer copy routines */
    109      1.81   tsutsui static void	hme_copytobuf_contig(struct hme_softc *, void *, int, int);
    110      1.81   tsutsui static void	hme_copyfrombuf_contig(struct hme_softc *, void *, int, int);
    111      1.81   tsutsui #endif
    112      1.28      tron 
    113       1.1        pk void
    114      1.71       dsl hme_config(struct hme_softc *sc)
    115       1.1        pk {
    116       1.1        pk 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    117       1.1        pk 	struct mii_data *mii = &sc->sc_mii;
    118       1.5        pk 	struct mii_softc *child;
    119      1.11        pk 	bus_dma_tag_t dmatag = sc->sc_dmatag;
    120       1.1        pk 	bus_dma_segment_t seg;
    121       1.1        pk 	bus_size_t size;
    122      1.28      tron 	int rseg, error;
    123       1.1        pk 
    124       1.1        pk 	/*
    125       1.1        pk 	 * HME common initialization.
    126       1.1        pk 	 *
    127       1.1        pk 	 * hme_softc fields that must be initialized by the front-end:
    128       1.1        pk 	 *
    129       1.1        pk 	 * the bus tag:
    130       1.1        pk 	 *	sc_bustag
    131       1.1        pk 	 *
    132      1.37       wiz 	 * the DMA bus tag:
    133       1.1        pk 	 *	sc_dmatag
    134       1.1        pk 	 *
    135       1.1        pk 	 * the bus handles:
    136       1.1        pk 	 *	sc_seb		(Shared Ethernet Block registers)
    137       1.1        pk 	 *	sc_erx		(Receiver Unit registers)
    138       1.1        pk 	 *	sc_etx		(Transmitter Unit registers)
    139       1.1        pk 	 *	sc_mac		(MAC registers)
    140      1.36       wiz 	 *	sc_mif		(Management Interface registers)
    141       1.1        pk 	 *
    142       1.1        pk 	 * the maximum bus burst size:
    143       1.1        pk 	 *	sc_burst
    144       1.1        pk 	 *
    145      1.28      tron 	 * (notyet:DMA capable memory for the ring descriptors & packet buffers:
    146      1.28      tron 	 *	rb_membase, rb_dmabase)
    147      1.28      tron 	 *
    148       1.1        pk 	 * the local Ethernet address:
    149       1.1        pk 	 *	sc_enaddr
    150       1.1        pk 	 *
    151       1.1        pk 	 */
    152       1.1        pk 
    153       1.1        pk 	/* Make sure the chip is stopped. */
    154      1.80   tsutsui 	hme_chipreset(sc);
    155       1.1        pk 
    156      1.28      tron 	/*
    157      1.28      tron 	 * Allocate descriptors and buffers
    158      1.28      tron 	 * XXX - do all this differently.. and more configurably,
    159      1.28      tron 	 * eg. use things as `dma_load_mbuf()' on transmit,
    160      1.28      tron 	 *     and a pool of `EXTMEM' mbufs (with buffers DMA-mapped
    161      1.38       wiz 	 *     all the time) on the receiver side.
    162      1.28      tron 	 *
    163      1.28      tron 	 * Note: receive buffers must be 64-byte aligned.
    164      1.28      tron 	 * Also, apparently, the buffers must extend to a DMA burst
    165      1.28      tron 	 * boundary beyond the maximum packet size.
    166      1.28      tron 	 */
    167      1.28      tron #define _HME_NDESC	128
    168      1.28      tron #define _HME_BUFSZ	1600
    169      1.28      tron 
    170      1.28      tron 	/* Note: the # of descriptors must be a multiple of 16 */
    171      1.28      tron 	sc->sc_rb.rb_ntbuf = _HME_NDESC;
    172      1.28      tron 	sc->sc_rb.rb_nrbuf = _HME_NDESC;
    173       1.1        pk 
    174       1.1        pk 	/*
    175       1.1        pk 	 * Allocate DMA capable memory
    176       1.1        pk 	 * Buffer descriptors must be aligned on a 2048 byte boundary;
    177       1.1        pk 	 * take this into account when calculating the size. Note that
    178       1.1        pk 	 * the maximum number of descriptors (256) occupies 2048 bytes,
    179      1.28      tron 	 * so we allocate that much regardless of _HME_NDESC.
    180       1.1        pk 	 */
    181      1.28      tron 	size =	2048 +					/* TX descriptors */
    182      1.28      tron 		2048 +					/* RX descriptors */
    183      1.28      tron 		sc->sc_rb.rb_ntbuf * _HME_BUFSZ +	/* TX buffers */
    184      1.46      heas 		sc->sc_rb.rb_nrbuf * _HME_BUFSZ;	/* RX buffers */
    185      1.11        pk 
    186      1.11        pk 	/* Allocate DMA buffer */
    187      1.28      tron 	if ((error = bus_dmamem_alloc(dmatag, size,
    188      1.28      tron 				      2048, 0,
    189      1.28      tron 				      &seg, 1, &rseg, BUS_DMA_NOWAIT)) != 0) {
    190      1.79   tsutsui 		aprint_error_dev(sc->sc_dev, "DMA buffer alloc error %d\n",
    191      1.64    cegger 			error);
    192      1.10       mrg 		return;
    193       1.1        pk 	}
    194       1.1        pk 
    195      1.11        pk 	/* Map DMA memory in CPU addressable space */
    196      1.11        pk 	if ((error = bus_dmamem_map(dmatag, &seg, rseg, size,
    197      1.28      tron 				    &sc->sc_rb.rb_membase,
    198  1.99.2.1  christos 				    BUS_DMA_NOWAIT | BUS_DMA_COHERENT)) != 0) {
    199      1.79   tsutsui 		aprint_error_dev(sc->sc_dev, "DMA buffer map error %d\n",
    200      1.64    cegger 			error);
    201      1.11        pk 		bus_dmamap_unload(dmatag, sc->sc_dmamap);
    202      1.11        pk 		bus_dmamem_free(dmatag, &seg, rseg);
    203       1.1        pk 		return;
    204       1.1        pk 	}
    205      1.13       mrg 
    206      1.13       mrg 	if ((error = bus_dmamap_create(dmatag, size, 1, size, 0,
    207      1.28      tron 				    BUS_DMA_NOWAIT, &sc->sc_dmamap)) != 0) {
    208      1.79   tsutsui 		aprint_error_dev(sc->sc_dev, "DMA map create error %d\n",
    209      1.64    cegger 			error);
    210      1.13       mrg 		return;
    211      1.13       mrg 	}
    212      1.13       mrg 
    213      1.13       mrg 	/* Load the buffer */
    214      1.13       mrg 	if ((error = bus_dmamap_load(dmatag, sc->sc_dmamap,
    215      1.17       mrg 	    sc->sc_rb.rb_membase, size, NULL,
    216  1.99.2.1  christos 	    BUS_DMA_NOWAIT | BUS_DMA_COHERENT)) != 0) {
    217      1.79   tsutsui 		aprint_error_dev(sc->sc_dev, "DMA buffer map load error %d\n",
    218      1.64    cegger 			error);
    219      1.13       mrg 		bus_dmamem_free(dmatag, &seg, rseg);
    220      1.13       mrg 		return;
    221      1.13       mrg 	}
    222      1.13       mrg 	sc->sc_rb.rb_dmabase = sc->sc_dmamap->dm_segs[0].ds_addr;
    223       1.1        pk 
    224      1.79   tsutsui 	aprint_normal_dev(sc->sc_dev, "Ethernet address %s\n",
    225      1.22   thorpej 	    ether_sprintf(sc->sc_enaddr));
    226       1.2        pk 
    227       1.1        pk 	/* Initialize ifnet structure. */
    228      1.79   tsutsui 	strlcpy(ifp->if_xname, device_xname(sc->sc_dev), IFNAMSIZ);
    229       1.1        pk 	ifp->if_softc = sc;
    230       1.1        pk 	ifp->if_start = hme_start;
    231      1.80   tsutsui 	ifp->if_stop = hme_stop;
    232       1.1        pk 	ifp->if_ioctl = hme_ioctl;
    233      1.84  jakllsch 	ifp->if_init = hme_init;
    234       1.1        pk 	ifp->if_watchdog = hme_watchdog;
    235  1.99.2.1  christos 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
    236      1.41      heas 	sc->sc_if_flags = ifp->if_flags;
    237      1.51      yamt 	ifp->if_capabilities |=
    238      1.51      yamt 	    IFCAP_CSUM_TCPv4_Tx | IFCAP_CSUM_TCPv4_Rx |
    239      1.51      yamt 	    IFCAP_CSUM_UDPv4_Tx | IFCAP_CSUM_UDPv4_Rx;
    240      1.20   thorpej 	IFQ_SET_READY(&ifp->if_snd);
    241       1.1        pk 
    242       1.1        pk 	/* Initialize ifmedia structures and MII info */
    243       1.1        pk 	mii->mii_ifp = ifp;
    244      1.34    petrov 	mii->mii_readreg = hme_mii_readreg;
    245       1.1        pk 	mii->mii_writereg = hme_mii_writereg;
    246       1.1        pk 	mii->mii_statchg = hme_mii_statchg;
    247       1.1        pk 
    248      1.61    dyoung 	sc->sc_ethercom.ec_mii = mii;
    249      1.61    dyoung 	ifmedia_init(&mii->mii_media, 0, hme_mediachange, ether_mediastatus);
    250       1.1        pk 
    251       1.4        pk 	hme_mifinit(sc);
    252       1.4        pk 
    253      1.79   tsutsui 	mii_attach(sc->sc_dev, mii, 0xffffffff,
    254      1.88       jdc 			MII_PHY_ANY, MII_OFFSET_ANY, MIIF_FORCEANEG);
    255       1.2        pk 
    256       1.5        pk 	child = LIST_FIRST(&mii->mii_phys);
    257       1.5        pk 	if (child == NULL) {
    258       1.1        pk 		/* No PHY attached */
    259  1.99.2.1  christos 		ifmedia_add(&mii->mii_media, IFM_ETHER | IFM_MANUAL, 0, NULL);
    260  1.99.2.1  christos 		ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_MANUAL);
    261       1.1        pk 	} else {
    262       1.1        pk 		/*
    263       1.5        pk 		 * Walk along the list of attached MII devices and
    264       1.5        pk 		 * establish an `MII instance' to `phy number'
    265       1.5        pk 		 * mapping. We'll use this mapping in media change
    266       1.5        pk 		 * requests to determine which phy to use to program
    267       1.5        pk 		 * the MIF configuration register.
    268       1.5        pk 		 */
    269       1.5        pk 		for (; child != NULL; child = LIST_NEXT(child, mii_list)) {
    270       1.5        pk 			/*
    271       1.5        pk 			 * Note: we support just two PHYs: the built-in
    272       1.5        pk 			 * internal device and an external on the MII
    273       1.5        pk 			 * connector.
    274       1.5        pk 			 */
    275       1.5        pk 			if (child->mii_phy > 1 || child->mii_inst > 1) {
    276      1.79   tsutsui 				aprint_error_dev(sc->sc_dev,
    277      1.79   tsutsui 				    "cannot accommodate MII device %s"
    278      1.28      tron 				       " at phy %d, instance %d\n",
    279      1.66   xtraeme 				       device_xname(child->mii_dev),
    280      1.28      tron 				       child->mii_phy, child->mii_inst);
    281       1.5        pk 				continue;
    282       1.5        pk 			}
    283       1.5        pk 
    284       1.5        pk 			sc->sc_phys[child->mii_inst] = child->mii_phy;
    285       1.5        pk 		}
    286       1.5        pk 
    287       1.5        pk 		/*
    288      1.77       jdc 		 * Set the default media to auto negotiation if the phy has
    289      1.77       jdc 		 * the auto negotiation capability.
    290      1.77       jdc 		 * XXX; What to do otherwise?
    291       1.1        pk 		 */
    292  1.99.2.1  christos 		if (ifmedia_match(&mii->mii_media, IFM_ETHER | IFM_AUTO, 0))
    293  1.99.2.1  christos 			ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO);
    294      1.77       jdc /*
    295      1.77       jdc 		else
    296      1.77       jdc 			ifmedia_set(&sc->sc_mii.mii_media, sc->sc_defaultmedia);
    297      1.77       jdc */
    298       1.1        pk 	}
    299      1.27      tron 
    300      1.28      tron 	/* claim 802.1q capability */
    301      1.27      tron 	sc->sc_ethercom.ec_capabilities |= ETHERCAP_VLAN_MTU;
    302       1.1        pk 
    303       1.1        pk 	/* Attach the interface. */
    304       1.1        pk 	if_attach(ifp);
    305      1.96     ozaki 	if_deferred_start_init(ifp, NULL);
    306       1.1        pk 	ether_ifattach(ifp, sc->sc_enaddr);
    307       1.1        pk 
    308      1.80   tsutsui 	if (pmf_device_register1(sc->sc_dev, NULL, NULL, hme_shutdown))
    309      1.80   tsutsui 		pmf_class_network_register(sc->sc_dev, ifp);
    310      1.80   tsutsui 	else
    311      1.80   tsutsui 		aprint_error_dev(sc->sc_dev,
    312      1.80   tsutsui 		    "couldn't establish power handler\n");
    313       1.1        pk 
    314      1.79   tsutsui 	rnd_attach_source(&sc->rnd_source, device_xname(sc->sc_dev),
    315      1.90       tls 			  RND_TYPE_NET, RND_FLAG_DEFAULT);
    316       1.5        pk 
    317      1.57        ad 	callout_init(&sc->sc_tick_ch, 0);
    318       1.5        pk }
    319       1.5        pk 
    320       1.5        pk void
    321      1.71       dsl hme_tick(void *arg)
    322       1.5        pk {
    323       1.5        pk 	struct hme_softc *sc = arg;
    324       1.5        pk 	int s;
    325       1.5        pk 
    326       1.5        pk 	s = splnet();
    327       1.5        pk 	mii_tick(&sc->sc_mii);
    328       1.5        pk 	splx(s);
    329       1.5        pk 
    330       1.9   thorpej 	callout_reset(&sc->sc_tick_ch, hz, hme_tick, sc);
    331       1.1        pk }
    332       1.1        pk 
    333       1.1        pk void
    334      1.71       dsl hme_reset(struct hme_softc *sc)
    335       1.1        pk {
    336       1.1        pk 	int s;
    337       1.1        pk 
    338       1.1        pk 	s = splnet();
    339      1.84  jakllsch 	(void)hme_init(&sc->sc_ethercom.ec_if);
    340       1.1        pk 	splx(s);
    341       1.1        pk }
    342       1.1        pk 
    343       1.1        pk void
    344      1.80   tsutsui hme_chipreset(struct hme_softc *sc)
    345       1.1        pk {
    346       1.1        pk 	bus_space_tag_t t = sc->sc_bustag;
    347       1.1        pk 	bus_space_handle_t seb = sc->sc_seb;
    348       1.1        pk 	int n;
    349       1.1        pk 
    350      1.33        pk 	/* Mask all interrupts */
    351      1.33        pk 	bus_space_write_4(t, seb, HME_SEBI_IMASK, 0xffffffff);
    352      1.33        pk 
    353       1.1        pk 	/* Reset transmitter and receiver */
    354       1.1        pk 	bus_space_write_4(t, seb, HME_SEBI_RESET,
    355      1.28      tron 			  (HME_SEB_RESET_ETX | HME_SEB_RESET_ERX));
    356       1.1        pk 
    357       1.1        pk 	for (n = 0; n < 20; n++) {
    358      1.75   tsutsui 		uint32_t v = bus_space_read_4(t, seb, HME_SEBI_RESET);
    359       1.1        pk 		if ((v & (HME_SEB_RESET_ETX | HME_SEB_RESET_ERX)) == 0)
    360       1.1        pk 			return;
    361       1.1        pk 		DELAY(20);
    362       1.1        pk 	}
    363       1.1        pk 
    364      1.80   tsutsui 	printf("%s: %s: reset failed\n", device_xname(sc->sc_dev), __func__);
    365      1.80   tsutsui }
    366      1.80   tsutsui 
    367      1.80   tsutsui void
    368      1.80   tsutsui hme_stop(struct ifnet *ifp, int disable)
    369      1.80   tsutsui {
    370      1.80   tsutsui 	struct hme_softc *sc;
    371      1.80   tsutsui 
    372      1.80   tsutsui 	sc = ifp->if_softc;
    373      1.80   tsutsui 
    374      1.80   tsutsui 	ifp->if_timer = 0;
    375      1.80   tsutsui 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
    376      1.80   tsutsui 
    377      1.80   tsutsui 	callout_stop(&sc->sc_tick_ch);
    378      1.80   tsutsui 	mii_down(&sc->sc_mii);
    379      1.80   tsutsui 
    380      1.80   tsutsui 	hme_chipreset(sc);
    381       1.1        pk }
    382       1.1        pk 
    383       1.1        pk void
    384      1.71       dsl hme_meminit(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.56  christos 	char *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.56  christos 	p = (void *)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.56  christos 	p = (void *)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.61    dyoung int
    464      1.84  jakllsch hme_init(struct ifnet *ifp)
    465       1.1        pk {
    466      1.84  jakllsch 	struct hme_softc *sc = ifp->if_softc;
    467       1.1        pk 	bus_space_tag_t t = sc->sc_bustag;
    468       1.1        pk 	bus_space_handle_t seb = sc->sc_seb;
    469       1.1        pk 	bus_space_handle_t etx = sc->sc_etx;
    470       1.1        pk 	bus_space_handle_t erx = sc->sc_erx;
    471       1.1        pk 	bus_space_handle_t mac = sc->sc_mac;
    472      1.75   tsutsui 	uint8_t *ea;
    473      1.75   tsutsui 	uint32_t v;
    474      1.61    dyoung 	int rc;
    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.80   tsutsui 	hme_stop(ifp, 0);
    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.49      heas 	    ETHER_VLAN_ENCAP_LEN + ETHER_MAX_LEN : ETHER_MAX_LEN);
    509      1.45      heas 	sc->sc_ec_capenable = sc->sc_ethercom.ec_capenable;
    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.42      heas 	 */
    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.49      heas 	    ETHER_VLAN_ENCAP_LEN + ETHER_MAX_LEN : ETHER_MAX_LEN);
    539       1.1        pk 
    540       1.1        pk 	/* step 8. Global Configuration & Interrupt Mask */
    541       1.1        pk 	bus_space_write_4(t, seb, HME_SEBI_IMASK,
    542      1.28      tron 			~(
    543      1.28      tron 			  /*HME_SEB_STAT_GOTFRAME | HME_SEB_STAT_SENTFRAME |*/
    544      1.28      tron 			  HME_SEB_STAT_HOSTTOTX |
    545      1.28      tron 			  HME_SEB_STAT_RXTOHOST |
    546      1.28      tron 			  HME_SEB_STAT_TXALL |
    547      1.28      tron 			  HME_SEB_STAT_TXPERR |
    548      1.28      tron 			  HME_SEB_STAT_RCNTEXP |
    549      1.77       jdc 			  HME_SEB_STAT_MIFIRQ |
    550      1.28      tron 			  HME_SEB_STAT_ALL_ERRORS ));
    551       1.1        pk 
    552       1.1        pk 	switch (sc->sc_burst) {
    553       1.1        pk 	default:
    554       1.1        pk 		v = 0;
    555       1.1        pk 		break;
    556       1.1        pk 	case 16:
    557       1.1        pk 		v = HME_SEB_CFG_BURST16;
    558       1.1        pk 		break;
    559       1.1        pk 	case 32:
    560       1.1        pk 		v = HME_SEB_CFG_BURST32;
    561       1.1        pk 		break;
    562       1.1        pk 	case 64:
    563       1.1        pk 		v = HME_SEB_CFG_BURST64;
    564       1.1        pk 		break;
    565       1.1        pk 	}
    566       1.1        pk 	bus_space_write_4(t, seb, HME_SEBI_CFG, v);
    567       1.1        pk 
    568       1.1        pk 	/* step 9. ETX Configuration: use mostly default values */
    569       1.1        pk 
    570       1.1        pk 	/* Enable DMA */
    571       1.2        pk 	v = bus_space_read_4(t, etx, HME_ETXI_CFG);
    572       1.1        pk 	v |= HME_ETX_CFG_DMAENABLE;
    573       1.2        pk 	bus_space_write_4(t, etx, HME_ETXI_CFG, v);
    574       1.1        pk 
    575       1.3        pk 	/* Transmit Descriptor ring size: in increments of 16 */
    576      1.28      tron 	bus_space_write_4(t, etx, HME_ETXI_RSIZE, _HME_NDESC / 16 - 1);
    577      1.28      tron 
    578       1.1        pk 
    579       1.3        pk 	/* step 10. ERX Configuration */
    580       1.2        pk 	v = bus_space_read_4(t, erx, HME_ERXI_CFG);
    581      1.28      tron 
    582      1.28      tron 	/* Encode Receive Descriptor ring size: four possible values */
    583      1.28      tron 	switch (_HME_NDESC /*XXX*/) {
    584      1.28      tron 	case 32:
    585      1.28      tron 		v |= HME_ERX_CFG_RINGSIZE32;
    586      1.28      tron 		break;
    587      1.28      tron 	case 64:
    588      1.28      tron 		v |= HME_ERX_CFG_RINGSIZE64;
    589      1.28      tron 		break;
    590      1.28      tron 	case 128:
    591      1.28      tron 		v |= HME_ERX_CFG_RINGSIZE128;
    592      1.28      tron 		break;
    593      1.28      tron 	case 256:
    594      1.28      tron 		v |= HME_ERX_CFG_RINGSIZE256;
    595      1.28      tron 		break;
    596      1.28      tron 	default:
    597      1.28      tron 		printf("hme: invalid Receive Descriptor ring size\n");
    598      1.28      tron 		break;
    599      1.28      tron 	}
    600      1.28      tron 
    601       1.3        pk 	/* Enable DMA */
    602      1.28      tron 	v |= HME_ERX_CFG_DMAENABLE;
    603      1.46      heas 
    604      1.46      heas 	/* set h/w rx checksum start offset (# of half-words) */
    605      1.49      heas #ifdef INET
    606      1.74   tsutsui 	v |= (((ETHER_HDR_LEN + sizeof(struct ip)) / sizeof(uint16_t))
    607      1.74   tsutsui 		<< HME_ERX_CFG_CSUMSHIFT) &
    608      1.46      heas 		HME_ERX_CFG_CSUMSTART;
    609      1.49      heas #endif
    610       1.2        pk 	bus_space_write_4(t, erx, HME_ERXI_CFG, v);
    611       1.1        pk 
    612       1.1        pk 	/* step 11. XIF Configuration */
    613       1.1        pk 	v = bus_space_read_4(t, mac, HME_MACI_XIF);
    614       1.1        pk 	v |= HME_MAC_XIF_OE;
    615       1.1        pk 	bus_space_write_4(t, mac, HME_MACI_XIF, v);
    616       1.1        pk 
    617       1.1        pk 	/* step 12. RX_MAC Configuration Register */
    618       1.1        pk 	v = bus_space_read_4(t, mac, HME_MACI_RXCFG);
    619      1.46      heas 	v |= HME_MAC_RXCFG_ENABLE | HME_MAC_RXCFG_PSTRIP;
    620       1.1        pk 	bus_space_write_4(t, mac, HME_MACI_RXCFG, v);
    621       1.1        pk 
    622       1.1        pk 	/* step 13. TX_MAC Configuration Register */
    623       1.1        pk 	v = bus_space_read_4(t, mac, HME_MACI_TXCFG);
    624       1.2        pk 	v |= (HME_MAC_TXCFG_ENABLE | HME_MAC_TXCFG_DGIVEUP);
    625       1.1        pk 	bus_space_write_4(t, mac, HME_MACI_TXCFG, v);
    626       1.1        pk 
    627       1.1        pk 	/* step 14. Issue Transmit Pending command */
    628       1.1        pk 
    629       1.1        pk 	/* Call MI initialization function if any */
    630       1.1        pk 	if (sc->sc_hwinit)
    631       1.1        pk 		(*sc->sc_hwinit)(sc);
    632      1.29   thorpej 
    633      1.29   thorpej 	/* Set the current media. */
    634      1.61    dyoung 	if ((rc = hme_mediachange(ifp)) != 0)
    635      1.61    dyoung 		return rc;
    636       1.9   thorpej 
    637       1.9   thorpej 	/* Start the one second timer. */
    638       1.9   thorpej 	callout_reset(&sc->sc_tick_ch, hz, hme_tick, sc);
    639       1.1        pk 
    640       1.1        pk 	ifp->if_flags |= IFF_RUNNING;
    641       1.1        pk 	ifp->if_flags &= ~IFF_OACTIVE;
    642      1.41      heas 	sc->sc_if_flags = ifp->if_flags;
    643       1.1        pk 	ifp->if_timer = 0;
    644       1.1        pk 	hme_start(ifp);
    645      1.61    dyoung 	return 0;
    646       1.1        pk }
    647       1.1        pk 
    648      1.28      tron /*
    649      1.28      tron  * Routine to copy from mbuf chain to transmit buffer in
    650      1.28      tron  * network buffer memory.
    651      1.28      tron  * Returns the amount of data copied.
    652      1.28      tron  */
    653      1.28      tron int
    654      1.72       dsl hme_put(struct hme_softc *sc, int ri, struct mbuf *m)
    655      1.72       dsl 	/* ri:			 Ring index */
    656      1.28      tron {
    657      1.28      tron 	struct mbuf *n;
    658      1.28      tron 	int len, tlen = 0;
    659      1.56  christos 	char *bp;
    660      1.28      tron 
    661      1.56  christos 	bp = (char *)sc->sc_rb.rb_txbuf + (ri % sc->sc_rb.rb_ntbuf) * _HME_BUFSZ;
    662      1.28      tron 	for (; m; m = n) {
    663      1.28      tron 		len = m->m_len;
    664      1.28      tron 		if (len == 0) {
    665      1.94  christos 			n = m_free(m);
    666      1.28      tron 			continue;
    667      1.28      tron 		}
    668      1.56  christos 		memcpy(bp, mtod(m, void *), len);
    669      1.28      tron 		bp += len;
    670      1.28      tron 		tlen += len;
    671      1.94  christos 		n = m_free(m);
    672      1.28      tron 	}
    673      1.28      tron 	return (tlen);
    674      1.28      tron }
    675      1.28      tron 
    676      1.28      tron /*
    677      1.28      tron  * Pull data off an interface.
    678      1.28      tron  * Len is length of data, with local net header stripped.
    679      1.28      tron  * We copy the data into mbufs.  When full cluster sized units are present
    680      1.28      tron  * we copy into clusters.
    681      1.28      tron  */
    682      1.28      tron struct mbuf *
    683      1.75   tsutsui hme_get(struct hme_softc *sc, int ri, uint32_t flags)
    684      1.28      tron {
    685      1.28      tron 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    686      1.28      tron 	struct mbuf *m, *m0, *newm;
    687      1.56  christos 	char *bp;
    688      1.46      heas 	int len, totlen;
    689      1.76   tsutsui #ifdef INET
    690      1.76   tsutsui 	int csum_flags;
    691      1.76   tsutsui #endif
    692      1.28      tron 
    693      1.46      heas 	totlen = HME_XD_DECODE_RSIZE(flags);
    694      1.28      tron 	MGETHDR(m0, M_DONTWAIT, MT_DATA);
    695      1.28      tron 	if (m0 == 0)
    696      1.28      tron 		return (0);
    697      1.93     ozaki 	m_set_rcvif(m0, ifp);
    698      1.28      tron 	m0->m_pkthdr.len = totlen;
    699      1.28      tron 	len = MHLEN;
    700      1.28      tron 	m = m0;
    701      1.28      tron 
    702      1.56  christos 	bp = (char *)sc->sc_rb.rb_rxbuf + (ri % sc->sc_rb.rb_nrbuf) * _HME_BUFSZ;
    703      1.28      tron 
    704      1.28      tron 	while (totlen > 0) {
    705      1.28      tron 		if (totlen >= MINCLSIZE) {
    706      1.28      tron 			MCLGET(m, M_DONTWAIT);
    707      1.28      tron 			if ((m->m_flags & M_EXT) == 0)
    708      1.28      tron 				goto bad;
    709      1.28      tron 			len = MCLBYTES;
    710      1.28      tron 		}
    711      1.28      tron 
    712      1.28      tron 		if (m == m0) {
    713      1.56  christos 			char *newdata = (char *)
    714      1.28      tron 			    ALIGN(m->m_data + sizeof(struct ether_header)) -
    715      1.28      tron 			    sizeof(struct ether_header);
    716      1.28      tron 			len -= newdata - m->m_data;
    717      1.28      tron 			m->m_data = newdata;
    718      1.28      tron 		}
    719      1.28      tron 
    720  1.99.2.1  christos 		m->m_len = len = uimin(totlen, len);
    721      1.56  christos 		memcpy(mtod(m, void *), bp, len);
    722      1.28      tron 		bp += len;
    723      1.28      tron 
    724      1.28      tron 		totlen -= len;
    725      1.28      tron 		if (totlen > 0) {
    726      1.28      tron 			MGET(newm, M_DONTWAIT, MT_DATA);
    727      1.28      tron 			if (newm == 0)
    728      1.28      tron 				goto bad;
    729      1.28      tron 			len = MLEN;
    730      1.28      tron 			m = m->m_next = newm;
    731      1.28      tron 		}
    732      1.28      tron 	}
    733      1.28      tron 
    734      1.49      heas #ifdef INET
    735      1.49      heas 	/* hardware checksum */
    736      1.76   tsutsui 	csum_flags = 0;
    737      1.50     rafal 	if (ifp->if_csum_flags_rx & (M_CSUM_TCPv4 | M_CSUM_UDPv4)) {
    738      1.46      heas 		struct ether_header *eh;
    739      1.74   tsutsui 		struct ether_vlan_header *evh;
    740      1.46      heas 		struct ip *ip;
    741      1.46      heas 		struct udphdr *uh;
    742      1.46      heas 		uint16_t *opts;
    743      1.46      heas 		int32_t hlen, pktlen;
    744      1.76   tsutsui 		uint32_t csum_data;
    745      1.46      heas 
    746      1.74   tsutsui 		eh = mtod(m0, struct ether_header *);
    747      1.74   tsutsui 		if (ntohs(eh->ether_type) == ETHERTYPE_IP) {
    748      1.74   tsutsui 			ip = (struct ip *)((char *)eh + ETHER_HDR_LEN);
    749      1.46      heas 			pktlen = m0->m_pkthdr.len - ETHER_HDR_LEN;
    750      1.74   tsutsui 		} else if (ntohs(eh->ether_type) == ETHERTYPE_VLAN) {
    751      1.74   tsutsui 			evh = (struct ether_vlan_header *)eh;
    752      1.97  pgoyette 			if (ntohs(evh->evl_proto) != ETHERTYPE_IP)
    753      1.74   tsutsui 				goto swcsum;
    754      1.74   tsutsui 			ip = (struct ip *)((char *)eh + ETHER_HDR_LEN +
    755      1.74   tsutsui 			    ETHER_VLAN_ENCAP_LEN);
    756      1.74   tsutsui 			pktlen = m0->m_pkthdr.len -
    757      1.74   tsutsui 			    ETHER_HDR_LEN - ETHER_VLAN_ENCAP_LEN;
    758      1.74   tsutsui 		} else
    759      1.46      heas 			goto swcsum;
    760      1.46      heas 
    761      1.46      heas 		/* IPv4 only */
    762      1.46      heas 		if (ip->ip_v != IPVERSION)
    763      1.46      heas 			goto swcsum;
    764      1.46      heas 
    765      1.46      heas 		hlen = ip->ip_hl << 2;
    766      1.48     perry 		if (hlen < sizeof(struct ip))
    767      1.46      heas 			goto swcsum;
    768      1.46      heas 
    769      1.49      heas 		/*
    770      1.49      heas 		 * bail if too short, has random trailing garbage, truncated,
    771      1.49      heas 		 * fragment, or has ethernet pad.
    772      1.49      heas 		 */
    773      1.76   tsutsui 		if (ntohs(ip->ip_len) < hlen ||
    774      1.76   tsutsui 		    ntohs(ip->ip_len) != pktlen ||
    775      1.76   tsutsui 		    (ntohs(ip->ip_off) & (IP_MF | IP_OFFMASK)) != 0)
    776      1.49      heas 			goto swcsum;
    777      1.46      heas 
    778      1.46      heas 		switch (ip->ip_p) {
    779      1.46      heas 		case IPPROTO_TCP:
    780      1.76   tsutsui 			if ((ifp->if_csum_flags_rx & M_CSUM_TCPv4) == 0)
    781      1.46      heas 				goto swcsum;
    782      1.46      heas 			if (pktlen < (hlen + sizeof(struct tcphdr)))
    783      1.46      heas 				goto swcsum;
    784      1.76   tsutsui 			csum_flags =
    785      1.76   tsutsui 			    M_CSUM_TCPv4 | M_CSUM_DATA | M_CSUM_NO_PSEUDOHDR;
    786      1.46      heas 			break;
    787      1.46      heas 		case IPPROTO_UDP:
    788      1.76   tsutsui 			if ((ifp->if_csum_flags_rx & M_CSUM_UDPv4) == 0)
    789      1.46      heas 				goto swcsum;
    790      1.46      heas 			if (pktlen < (hlen + sizeof(struct udphdr)))
    791      1.46      heas 				goto swcsum;
    792      1.56  christos 			uh = (struct udphdr *)((char *)ip + hlen);
    793      1.46      heas 			/* no checksum */
    794      1.46      heas 			if (uh->uh_sum == 0)
    795      1.46      heas 				goto swcsum;
    796      1.76   tsutsui 			csum_flags =
    797      1.76   tsutsui 			    M_CSUM_UDPv4 | M_CSUM_DATA | M_CSUM_NO_PSEUDOHDR;
    798      1.46      heas 			break;
    799      1.46      heas 		default:
    800      1.49      heas 			goto swcsum;
    801      1.46      heas 		}
    802      1.46      heas 
    803      1.46      heas 		/* w/ M_CSUM_NO_PSEUDOHDR, the uncomplemented sum is expected */
    804      1.76   tsutsui 		csum_data = ~flags & HME_XD_RXCKSUM;
    805      1.46      heas 
    806      1.74   tsutsui 		/*
    807      1.74   tsutsui 		 * If data offset is different from RX cksum start offset,
    808      1.74   tsutsui 		 * we have to deduct them.
    809      1.74   tsutsui 		 */
    810      1.76   tsutsui 		hlen = ((char *)ip + hlen) -
    811      1.74   tsutsui 		    ((char *)eh + ETHER_HDR_LEN + sizeof(struct ip));
    812      1.76   tsutsui 		if (hlen > 1) {
    813      1.46      heas 			uint32_t optsum;
    814      1.46      heas 
    815      1.46      heas 			optsum = 0;
    816      1.74   tsutsui 			opts = (uint16_t *)((char *)eh +
    817      1.74   tsutsui 			    ETHER_HDR_LEN + sizeof(struct ip));
    818      1.46      heas 
    819      1.76   tsutsui 			while (hlen > 1) {
    820      1.46      heas 				optsum += ntohs(*opts++);
    821      1.76   tsutsui 				hlen -= 2;
    822      1.46      heas 			}
    823      1.46      heas 			while (optsum >> 16)
    824      1.46      heas 				optsum = (optsum >> 16) + (optsum & 0xffff);
    825      1.46      heas 
    826      1.73   tsutsui 			/* Deduct the ip opts sum from the hwsum. */
    827      1.76   tsutsui 			csum_data += (uint16_t)~optsum;
    828      1.46      heas 
    829      1.76   tsutsui 			while (csum_data >> 16)
    830      1.76   tsutsui 				csum_data =
    831      1.76   tsutsui 				    (csum_data >> 16) + (csum_data & 0xffff);
    832      1.46      heas 		}
    833      1.76   tsutsui 		m0->m_pkthdr.csum_data = csum_data;
    834      1.76   tsutsui 	}
    835      1.49      heas swcsum:
    836      1.76   tsutsui 	m0->m_pkthdr.csum_flags = csum_flags;
    837      1.49      heas #endif
    838      1.46      heas 
    839      1.28      tron 	return (m0);
    840      1.28      tron 
    841      1.28      tron bad:
    842      1.28      tron 	m_freem(m0);
    843      1.28      tron 	return (0);
    844      1.28      tron }
    845      1.28      tron 
    846      1.28      tron /*
    847      1.28      tron  * Pass a packet to the higher levels.
    848      1.28      tron  */
    849      1.28      tron void
    850      1.75   tsutsui hme_read(struct hme_softc *sc, int ix, uint32_t flags)
    851      1.28      tron {
    852      1.28      tron 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    853      1.28      tron 	struct mbuf *m;
    854      1.46      heas 	int len;
    855      1.28      tron 
    856      1.46      heas 	len = HME_XD_DECODE_RSIZE(flags);
    857      1.28      tron 	if (len <= sizeof(struct ether_header) ||
    858      1.28      tron 	    len > ((sc->sc_ethercom.ec_capenable & ETHERCAP_VLAN_MTU) ?
    859      1.28      tron 	    ETHER_VLAN_ENCAP_LEN + ETHERMTU + sizeof(struct ether_header) :
    860      1.28      tron 	    ETHERMTU + sizeof(struct ether_header))) {
    861      1.28      tron #ifdef HMEDEBUG
    862      1.28      tron 		printf("%s: invalid packet size %d; dropping\n",
    863      1.79   tsutsui 		    device_xname(sc->sc_dev), len);
    864      1.28      tron #endif
    865      1.28      tron 		ifp->if_ierrors++;
    866      1.28      tron 		return;
    867      1.28      tron 	}
    868      1.28      tron 
    869      1.28      tron 	/* Pull packet off interface. */
    870      1.46      heas 	m = hme_get(sc, ix, flags);
    871      1.28      tron 	if (m == 0) {
    872      1.28      tron 		ifp->if_ierrors++;
    873      1.28      tron 		return;
    874      1.28      tron 	}
    875      1.28      tron 
    876      1.28      tron 	/* Pass the packet up. */
    877      1.92     ozaki 	if_percpuq_enqueue(ifp->if_percpuq, m);
    878      1.28      tron }
    879      1.28      tron 
    880       1.1        pk void
    881      1.71       dsl hme_start(struct ifnet *ifp)
    882       1.1        pk {
    883      1.79   tsutsui 	struct hme_softc *sc = ifp->if_softc;
    884      1.56  christos 	void *txd = sc->sc_rb.rb_txd;
    885       1.1        pk 	struct mbuf *m;
    886      1.46      heas 	unsigned int txflags;
    887      1.80   tsutsui 	unsigned int ri, len, obusy;
    888      1.28      tron 	unsigned int ntbuf = sc->sc_rb.rb_ntbuf;
    889       1.1        pk 
    890       1.1        pk 	if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
    891       1.1        pk 		return;
    892       1.1        pk 
    893      1.28      tron 	ri = sc->sc_rb.rb_tdhead;
    894      1.80   tsutsui 	obusy = sc->sc_rb.rb_td_nbusy;
    895      1.28      tron 
    896      1.28      tron 	for (;;) {
    897      1.28      tron 		IFQ_DEQUEUE(&ifp->if_snd, m);
    898      1.28      tron 		if (m == 0)
    899       1.1        pk 			break;
    900       1.1        pk 
    901       1.1        pk 		/*
    902       1.1        pk 		 * If BPF is listening on this interface, let it see the
    903       1.1        pk 		 * packet before we commit it to the wire.
    904       1.1        pk 		 */
    905      1.99   msaitoh 		bpf_mtap(ifp, m, BPF_D_OUT);
    906       1.1        pk 
    907      1.49      heas #ifdef INET
    908      1.46      heas 		/* collect bits for h/w csum, before hme_put frees the mbuf */
    909      1.46      heas 		if (ifp->if_csum_flags_tx & (M_CSUM_TCPv4 | M_CSUM_UDPv4) &&
    910      1.46      heas 		    m->m_pkthdr.csum_flags & (M_CSUM_TCPv4 | M_CSUM_UDPv4)) {
    911      1.46      heas 			struct ether_header *eh;
    912      1.46      heas 			uint16_t offset, start;
    913      1.46      heas 
    914      1.46      heas 			eh = mtod(m, struct ether_header *);
    915      1.46      heas 			switch (ntohs(eh->ether_type)) {
    916      1.46      heas 			case ETHERTYPE_IP:
    917      1.46      heas 				start = ETHER_HDR_LEN;
    918      1.46      heas 				break;
    919      1.46      heas 			case ETHERTYPE_VLAN:
    920      1.46      heas 				start = ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN;
    921      1.46      heas 				break;
    922      1.46      heas 			default:
    923      1.46      heas 				/* unsupported, drop it */
    924      1.46      heas 				m_free(m);
    925      1.46      heas 				continue;
    926      1.46      heas 			}
    927      1.47   thorpej 			start += M_CSUM_DATA_IPv4_IPHL(m->m_pkthdr.csum_data);
    928      1.47   thorpej 			offset = M_CSUM_DATA_IPv4_OFFSET(m->m_pkthdr.csum_data)
    929      1.47   thorpej 			    + start;
    930      1.46      heas 			txflags = HME_XD_TXCKSUM |
    931      1.46      heas 				  (offset << HME_XD_TXCSSTUFFSHIFT) |
    932  1.99.2.1  christos 				  (start << HME_XD_TXCSSTARTSHIFT);
    933      1.46      heas 		} else
    934      1.49      heas #endif
    935      1.46      heas 			txflags = 0;
    936      1.46      heas 
    937      1.28      tron 		/*
    938      1.28      tron 		 * Copy the mbuf chain into the transmit buffer.
    939      1.28      tron 		 */
    940      1.28      tron 		len = hme_put(sc, ri, m);
    941      1.28      tron 
    942      1.28      tron 		/*
    943      1.28      tron 		 * Initialize transmit registers and start transmission
    944      1.28      tron 		 */
    945      1.28      tron 		HME_XD_SETFLAGS(sc->sc_pci, txd, ri,
    946      1.28      tron 			HME_XD_OWN | HME_XD_SOP | HME_XD_EOP |
    947      1.46      heas 			HME_XD_ENCODE_TSIZE(len) | txflags);
    948      1.28      tron 
    949      1.28      tron 		/*if (sc->sc_rb.rb_td_nbusy <= 0)*/
    950      1.28      tron 		bus_space_write_4(sc->sc_bustag, sc->sc_etx, HME_ETXI_PENDING,
    951      1.28      tron 				  HME_ETX_TP_DMAWAKEUP);
    952      1.28      tron 
    953      1.28      tron 		if (++ri == ntbuf)
    954      1.28      tron 			ri = 0;
    955      1.28      tron 
    956      1.28      tron 		if (++sc->sc_rb.rb_td_nbusy == ntbuf) {
    957      1.26      tron 			ifp->if_flags |= IFF_OACTIVE;
    958      1.26      tron 			break;
    959      1.26      tron 		}
    960       1.1        pk 	}
    961       1.1        pk 
    962      1.80   tsutsui 	if (obusy != sc->sc_rb.rb_td_nbusy) {
    963      1.80   tsutsui 		sc->sc_rb.rb_tdhead = ri;
    964      1.80   tsutsui 		ifp->if_timer = 5;
    965      1.80   tsutsui 	}
    966       1.1        pk }
    967       1.1        pk 
    968       1.1        pk /*
    969       1.1        pk  * Transmit interrupt.
    970       1.1        pk  */
    971       1.1        pk int
    972      1.71       dsl hme_tint(struct hme_softc *sc)
    973       1.1        pk {
    974       1.1        pk 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    975      1.28      tron 	bus_space_tag_t t = sc->sc_bustag;
    976      1.28      tron 	bus_space_handle_t mac = sc->sc_mac;
    977       1.1        pk 	unsigned int ri, txflags;
    978      1.28      tron 
    979      1.28      tron 	/*
    980      1.28      tron 	 * Unload collision counters
    981      1.28      tron 	 */
    982      1.28      tron 	ifp->if_collisions +=
    983      1.28      tron 		bus_space_read_4(t, mac, HME_MACI_NCCNT) +
    984      1.77       jdc 		bus_space_read_4(t, mac, HME_MACI_FCCNT);
    985      1.77       jdc 	ifp->if_oerrors +=
    986      1.28      tron 		bus_space_read_4(t, mac, HME_MACI_EXCNT) +
    987      1.28      tron 		bus_space_read_4(t, mac, HME_MACI_LTCNT);
    988      1.28      tron 
    989      1.28      tron 	/*
    990      1.28      tron 	 * then clear the hardware counters.
    991      1.28      tron 	 */
    992      1.28      tron 	bus_space_write_4(t, mac, HME_MACI_NCCNT, 0);
    993      1.28      tron 	bus_space_write_4(t, mac, HME_MACI_FCCNT, 0);
    994      1.28      tron 	bus_space_write_4(t, mac, HME_MACI_EXCNT, 0);
    995      1.28      tron 	bus_space_write_4(t, mac, HME_MACI_LTCNT, 0);
    996       1.1        pk 
    997       1.1        pk 	/* Fetch current position in the transmit ring */
    998      1.28      tron 	ri = sc->sc_rb.rb_tdtail;
    999       1.1        pk 
   1000       1.1        pk 	for (;;) {
   1001      1.28      tron 		if (sc->sc_rb.rb_td_nbusy <= 0)
   1002       1.1        pk 			break;
   1003       1.1        pk 
   1004      1.15       eeh 		txflags = HME_XD_GETFLAGS(sc->sc_pci, sc->sc_rb.rb_txd, ri);
   1005       1.1        pk 
   1006       1.1        pk 		if (txflags & HME_XD_OWN)
   1007       1.1        pk 			break;
   1008       1.1        pk 
   1009       1.1        pk 		ifp->if_flags &= ~IFF_OACTIVE;
   1010      1.28      tron 		ifp->if_opackets++;
   1011      1.26      tron 
   1012      1.28      tron 		if (++ri == sc->sc_rb.rb_ntbuf)
   1013       1.1        pk 			ri = 0;
   1014       1.1        pk 
   1015      1.28      tron 		--sc->sc_rb.rb_td_nbusy;
   1016       1.1        pk 	}
   1017       1.1        pk 
   1018       1.3        pk 	/* Update ring */
   1019      1.28      tron 	sc->sc_rb.rb_tdtail = ri;
   1020       1.1        pk 
   1021      1.96     ozaki 	if_schedule_deferred_start(ifp);
   1022       1.1        pk 
   1023      1.28      tron 	if (sc->sc_rb.rb_td_nbusy == 0)
   1024       1.1        pk 		ifp->if_timer = 0;
   1025       1.1        pk 
   1026       1.1        pk 	return (1);
   1027       1.1        pk }
   1028       1.1        pk 
   1029       1.1        pk /*
   1030       1.1        pk  * Receive interrupt.
   1031       1.1        pk  */
   1032       1.1        pk int
   1033      1.71       dsl hme_rint(struct hme_softc *sc)
   1034       1.1        pk {
   1035      1.77       jdc 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1036      1.77       jdc 	bus_space_tag_t t = sc->sc_bustag;
   1037      1.77       jdc 	bus_space_handle_t mac = sc->sc_mac;
   1038      1.56  christos 	void *xdr = sc->sc_rb.rb_rxd;
   1039      1.28      tron 	unsigned int nrbuf = sc->sc_rb.rb_nrbuf;
   1040      1.46      heas 	unsigned int ri;
   1041      1.75   tsutsui 	uint32_t flags;
   1042       1.1        pk 
   1043      1.28      tron 	ri = sc->sc_rb.rb_rdtail;
   1044       1.1        pk 
   1045       1.1        pk 	/*
   1046       1.1        pk 	 * Process all buffers with valid data.
   1047       1.1        pk 	 */
   1048       1.1        pk 	for (;;) {
   1049      1.28      tron 		flags = HME_XD_GETFLAGS(sc->sc_pci, xdr, ri);
   1050       1.1        pk 		if (flags & HME_XD_OWN)
   1051       1.1        pk 			break;
   1052       1.1        pk 
   1053       1.4        pk 		if (flags & HME_XD_OFL) {
   1054       1.4        pk 			printf("%s: buffer overflow, ri=%d; flags=0x%x\n",
   1055      1.79   tsutsui 					device_xname(sc->sc_dev), ri, flags);
   1056      1.46      heas 		} else
   1057      1.46      heas 			hme_read(sc, ri, flags);
   1058       1.1        pk 
   1059      1.28      tron 		/* This buffer can be used by the hardware again */
   1060      1.28      tron 		HME_XD_SETFLAGS(sc->sc_pci, xdr, ri,
   1061      1.28      tron 				HME_XD_OWN | HME_XD_ENCODE_RSIZE(_HME_BUFSZ));
   1062      1.26      tron 
   1063      1.28      tron 		if (++ri == nrbuf)
   1064       1.1        pk 			ri = 0;
   1065       1.1        pk 	}
   1066       1.1        pk 
   1067      1.28      tron 	sc->sc_rb.rb_rdtail = ri;
   1068      1.28      tron 
   1069      1.77       jdc 	/* Read error counters ... */
   1070      1.77       jdc 	ifp->if_ierrors +=
   1071      1.77       jdc 	    bus_space_read_4(t, mac, HME_MACI_STAT_LCNT) +
   1072      1.77       jdc 	    bus_space_read_4(t, mac, HME_MACI_STAT_ACNT) +
   1073      1.77       jdc 	    bus_space_read_4(t, mac, HME_MACI_STAT_CCNT) +
   1074      1.77       jdc 	    bus_space_read_4(t, mac, HME_MACI_STAT_CVCNT);
   1075      1.77       jdc 
   1076      1.77       jdc 	/* ... then clear the hardware counters. */
   1077      1.77       jdc 	bus_space_write_4(t, mac, HME_MACI_STAT_LCNT, 0);
   1078      1.77       jdc 	bus_space_write_4(t, mac, HME_MACI_STAT_ACNT, 0);
   1079      1.77       jdc 	bus_space_write_4(t, mac, HME_MACI_STAT_CCNT, 0);
   1080      1.77       jdc 	bus_space_write_4(t, mac, HME_MACI_STAT_CVCNT, 0);
   1081       1.1        pk 	return (1);
   1082       1.1        pk }
   1083       1.1        pk 
   1084       1.1        pk int
   1085      1.71       dsl hme_eint(struct hme_softc *sc, u_int status)
   1086       1.1        pk {
   1087      1.77       jdc 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1088       1.1        pk 	char bits[128];
   1089       1.1        pk 
   1090       1.1        pk 	if ((status & HME_SEB_STAT_MIFIRQ) != 0) {
   1091      1.33        pk 		bus_space_tag_t t = sc->sc_bustag;
   1092      1.33        pk 		bus_space_handle_t mif = sc->sc_mif;
   1093      1.75   tsutsui 		uint32_t cf, st, sm;
   1094      1.33        pk 		cf = bus_space_read_4(t, mif, HME_MIFI_CFG);
   1095      1.33        pk 		st = bus_space_read_4(t, mif, HME_MIFI_STAT);
   1096      1.33        pk 		sm = bus_space_read_4(t, mif, HME_MIFI_SM);
   1097      1.33        pk 		printf("%s: XXXlink status changed: cfg=%x, stat %x, sm %x\n",
   1098      1.79   tsutsui 			device_xname(sc->sc_dev), cf, st, sm);
   1099       1.1        pk 		return (1);
   1100       1.1        pk 	}
   1101      1.77       jdc 
   1102      1.77       jdc 	/* Receive error counters rolled over */
   1103      1.77       jdc 	if (status & HME_SEB_STAT_ACNTEXP)
   1104      1.77       jdc 		ifp->if_ierrors += 0xff;
   1105      1.77       jdc 	if (status & HME_SEB_STAT_CCNTEXP)
   1106      1.77       jdc 		ifp->if_ierrors += 0xff;
   1107      1.77       jdc 	if (status & HME_SEB_STAT_LCNTEXP)
   1108      1.77       jdc 		ifp->if_ierrors += 0xff;
   1109      1.77       jdc 	if (status & HME_SEB_STAT_CVCNTEXP)
   1110      1.77       jdc 		ifp->if_ierrors += 0xff;
   1111      1.77       jdc 
   1112      1.77       jdc 	/* RXTERR locks up the interface, so do a reset */
   1113      1.77       jdc 	if (status & HME_SEB_STAT_RXTERR)
   1114      1.77       jdc 		hme_reset(sc);
   1115      1.77       jdc 
   1116      1.68  christos 	snprintb(bits, sizeof(bits), HME_SEB_STAT_BITS, status);
   1117      1.79   tsutsui 	printf("%s: status=%s\n", device_xname(sc->sc_dev), bits);
   1118  1.99.2.1  christos 
   1119       1.1        pk 	return (1);
   1120       1.1        pk }
   1121       1.1        pk 
   1122       1.1        pk int
   1123      1.71       dsl hme_intr(void *v)
   1124       1.1        pk {
   1125      1.79   tsutsui 	struct hme_softc *sc = v;
   1126       1.1        pk 	bus_space_tag_t t = sc->sc_bustag;
   1127       1.1        pk 	bus_space_handle_t seb = sc->sc_seb;
   1128      1.75   tsutsui 	uint32_t status;
   1129       1.1        pk 	int r = 0;
   1130       1.1        pk 
   1131       1.1        pk 	status = bus_space_read_4(t, seb, HME_SEBI_STAT);
   1132       1.1        pk 
   1133       1.1        pk 	if ((status & HME_SEB_STAT_ALL_ERRORS) != 0)
   1134       1.1        pk 		r |= hme_eint(sc, status);
   1135       1.1        pk 
   1136       1.1        pk 	if ((status & (HME_SEB_STAT_TXALL | HME_SEB_STAT_HOSTTOTX)) != 0)
   1137       1.1        pk 		r |= hme_tint(sc);
   1138       1.1        pk 
   1139       1.1        pk 	if ((status & HME_SEB_STAT_RXTOHOST) != 0)
   1140       1.1        pk 		r |= hme_rint(sc);
   1141       1.1        pk 
   1142      1.40       abs 	rnd_add_uint32(&sc->rnd_source, status);
   1143      1.40       abs 
   1144       1.1        pk 	return (r);
   1145       1.1        pk }
   1146       1.1        pk 
   1147       1.1        pk 
   1148       1.1        pk void
   1149      1.71       dsl hme_watchdog(struct ifnet *ifp)
   1150       1.1        pk {
   1151       1.1        pk 	struct hme_softc *sc = ifp->if_softc;
   1152       1.1        pk 
   1153      1.79   tsutsui 	log(LOG_ERR, "%s: device timeout\n", device_xname(sc->sc_dev));
   1154       1.1        pk 	++ifp->if_oerrors;
   1155       1.1        pk 
   1156       1.1        pk 	hme_reset(sc);
   1157       1.4        pk }
   1158       1.4        pk 
   1159       1.4        pk /*
   1160       1.4        pk  * Initialize the MII Management Interface
   1161       1.4        pk  */
   1162       1.4        pk void
   1163      1.71       dsl hme_mifinit(struct hme_softc *sc)
   1164       1.4        pk {
   1165       1.4        pk 	bus_space_tag_t t = sc->sc_bustag;
   1166       1.4        pk 	bus_space_handle_t mif = sc->sc_mif;
   1167      1.35        pk 	bus_space_handle_t mac = sc->sc_mac;
   1168      1.33        pk 	int instance, phy;
   1169      1.75   tsutsui 	uint32_t v;
   1170       1.4        pk 
   1171      1.61    dyoung 	if (sc->sc_mii.mii_media.ifm_cur != NULL) {
   1172      1.61    dyoung 		instance = IFM_INST(sc->sc_mii.mii_media.ifm_cur->ifm_media);
   1173      1.33        pk 		phy = sc->sc_phys[instance];
   1174      1.33        pk 	} else
   1175      1.33        pk 		/* No media set yet, pick phy arbitrarily.. */
   1176      1.33        pk 		phy = HME_PHYAD_EXTERNAL;
   1177      1.33        pk 
   1178      1.33        pk 	/* Configure the MIF in frame mode, no poll, current phy select */
   1179      1.33        pk 	v = 0;
   1180      1.33        pk 	if (phy == HME_PHYAD_EXTERNAL)
   1181      1.33        pk 		v |= HME_MIF_CFG_PHY;
   1182       1.4        pk 	bus_space_write_4(t, mif, HME_MIFI_CFG, v);
   1183      1.35        pk 
   1184      1.35        pk 	/* If an external transceiver is selected, enable its MII drivers */
   1185      1.35        pk 	v = bus_space_read_4(t, mac, HME_MACI_XIF);
   1186      1.35        pk 	v &= ~HME_MAC_XIF_MIIENABLE;
   1187      1.35        pk 	if (phy == HME_PHYAD_EXTERNAL)
   1188      1.35        pk 		v |= HME_MAC_XIF_MIIENABLE;
   1189      1.35        pk 	bus_space_write_4(t, mac, HME_MACI_XIF, v);
   1190       1.1        pk }
   1191       1.1        pk 
   1192       1.1        pk /*
   1193       1.1        pk  * MII interface
   1194       1.1        pk  */
   1195       1.1        pk static int
   1196  1.99.2.1  christos hme_mii_readreg(device_t self, int phy, int reg, uint16_t *val)
   1197       1.1        pk {
   1198      1.79   tsutsui 	struct hme_softc *sc = device_private(self);
   1199       1.1        pk 	bus_space_tag_t t = sc->sc_bustag;
   1200       1.1        pk 	bus_space_handle_t mif = sc->sc_mif;
   1201      1.35        pk 	bus_space_handle_t mac = sc->sc_mac;
   1202      1.75   tsutsui 	uint32_t v, xif_cfg, mifi_cfg;
   1203  1.99.2.1  christos 	int n, rv;
   1204       1.1        pk 
   1205      1.33        pk 	/* We can at most have two PHYs */
   1206      1.33        pk 	if (phy != HME_PHYAD_EXTERNAL && phy != HME_PHYAD_INTERNAL)
   1207  1.99.2.1  christos 		return -1;
   1208      1.32    martin 
   1209       1.5        pk 	/* Select the desired PHY in the MIF configuration register */
   1210      1.33        pk 	v = mifi_cfg = bus_space_read_4(t, mif, HME_MIFI_CFG);
   1211       1.5        pk 	v &= ~HME_MIF_CFG_PHY;
   1212       1.5        pk 	if (phy == HME_PHYAD_EXTERNAL)
   1213       1.5        pk 		v |= HME_MIF_CFG_PHY;
   1214       1.5        pk 	bus_space_write_4(t, mif, HME_MIFI_CFG, v);
   1215       1.5        pk 
   1216      1.42      heas 	/* Enable MII drivers on external transceiver */
   1217      1.35        pk 	v = xif_cfg = bus_space_read_4(t, mac, HME_MACI_XIF);
   1218      1.35        pk 	if (phy == HME_PHYAD_EXTERNAL)
   1219      1.35        pk 		v |= HME_MAC_XIF_MIIENABLE;
   1220      1.35        pk 	else
   1221      1.35        pk 		v &= ~HME_MAC_XIF_MIIENABLE;
   1222      1.35        pk 	bus_space_write_4(t, mac, HME_MACI_XIF, v);
   1223      1.35        pk 
   1224      1.33        pk #if 0
   1225      1.33        pk /* This doesn't work reliably; the MDIO_1 bit is off most of the time */
   1226      1.33        pk 	/*
   1227      1.33        pk 	 * Check whether a transceiver is connected by testing
   1228      1.33        pk 	 * the MIF configuration register's MDI_X bits. Note that
   1229      1.33        pk 	 * MDI_0 (int) == 0x100 and MDI_1 (ext) == 0x200; see hmereg.h
   1230      1.33        pk 	 */
   1231      1.33        pk 	mif_mdi_bit = 1 << (8 + (1 - phy));
   1232      1.33        pk 	delay(100);
   1233      1.33        pk 	v = bus_space_read_4(t, mif, HME_MIFI_CFG);
   1234  1.99.2.1  christos 	if ((v & mif_mdi_bit) == 0) {
   1235  1.99.2.1  christos 		rv = -1;
   1236  1.99.2.1  christos 		goto out;
   1237  1.99.2.1  christos 	}
   1238      1.33        pk #endif
   1239      1.33        pk 
   1240       1.1        pk 	/* Construct the frame command */
   1241       1.1        pk 	v = (MII_COMMAND_START << HME_MIF_FO_ST_SHIFT) |
   1242       1.1        pk 	    HME_MIF_FO_TAMSB |
   1243       1.1        pk 	    (MII_COMMAND_READ << HME_MIF_FO_OPC_SHIFT) |
   1244       1.1        pk 	    (phy << HME_MIF_FO_PHYAD_SHIFT) |
   1245       1.1        pk 	    (reg << HME_MIF_FO_REGAD_SHIFT);
   1246       1.1        pk 
   1247       1.1        pk 	bus_space_write_4(t, mif, HME_MIFI_FO, v);
   1248       1.1        pk 	for (n = 0; n < 100; n++) {
   1249       1.2        pk 		DELAY(1);
   1250       1.1        pk 		v = bus_space_read_4(t, mif, HME_MIFI_FO);
   1251      1.33        pk 		if (v & HME_MIF_FO_TALSB) {
   1252  1.99.2.1  christos 			*val = v & HME_MIF_FO_DATA;
   1253  1.99.2.1  christos 			rv = 0;
   1254      1.33        pk 			goto out;
   1255      1.33        pk 		}
   1256       1.1        pk 	}
   1257       1.1        pk 
   1258  1.99.2.1  christos 	rv = ETIMEDOUT;
   1259      1.79   tsutsui 	printf("%s: mii_read timeout\n", device_xname(sc->sc_dev));
   1260      1.33        pk 
   1261      1.33        pk out:
   1262      1.33        pk 	/* Restore MIFI_CFG register */
   1263      1.33        pk 	bus_space_write_4(t, mif, HME_MIFI_CFG, mifi_cfg);
   1264      1.35        pk 	/* Restore XIF register */
   1265      1.35        pk 	bus_space_write_4(t, mac, HME_MACI_XIF, xif_cfg);
   1266  1.99.2.1  christos 	return rv;
   1267       1.1        pk }
   1268       1.1        pk 
   1269  1.99.2.1  christos static int
   1270  1.99.2.1  christos hme_mii_writereg(device_t self, int phy, int reg, uint16_t val)
   1271       1.1        pk {
   1272      1.79   tsutsui 	struct hme_softc *sc = device_private(self);
   1273       1.1        pk 	bus_space_tag_t t = sc->sc_bustag;
   1274       1.1        pk 	bus_space_handle_t mif = sc->sc_mif;
   1275      1.35        pk 	bus_space_handle_t mac = sc->sc_mac;
   1276      1.75   tsutsui 	uint32_t v, xif_cfg, mifi_cfg;
   1277  1.99.2.1  christos 	int n, rv;
   1278      1.32    martin 
   1279      1.33        pk 	/* We can at most have two PHYs */
   1280      1.33        pk 	if (phy != HME_PHYAD_EXTERNAL && phy != HME_PHYAD_INTERNAL)
   1281  1.99.2.1  christos 		return -1;
   1282       1.1        pk 
   1283       1.5        pk 	/* Select the desired PHY in the MIF configuration register */
   1284      1.33        pk 	v = mifi_cfg = bus_space_read_4(t, mif, HME_MIFI_CFG);
   1285       1.5        pk 	v &= ~HME_MIF_CFG_PHY;
   1286       1.5        pk 	if (phy == HME_PHYAD_EXTERNAL)
   1287       1.5        pk 		v |= HME_MIF_CFG_PHY;
   1288       1.5        pk 	bus_space_write_4(t, mif, HME_MIFI_CFG, v);
   1289       1.5        pk 
   1290      1.42      heas 	/* Enable MII drivers on external transceiver */
   1291      1.35        pk 	v = xif_cfg = bus_space_read_4(t, mac, HME_MACI_XIF);
   1292      1.35        pk 	if (phy == HME_PHYAD_EXTERNAL)
   1293      1.35        pk 		v |= HME_MAC_XIF_MIIENABLE;
   1294      1.35        pk 	else
   1295      1.35        pk 		v &= ~HME_MAC_XIF_MIIENABLE;
   1296      1.35        pk 	bus_space_write_4(t, mac, HME_MACI_XIF, v);
   1297      1.35        pk 
   1298      1.33        pk #if 0
   1299      1.33        pk /* This doesn't work reliably; the MDIO_1 bit is off most of the time */
   1300      1.33        pk 	/*
   1301      1.33        pk 	 * Check whether a transceiver is connected by testing
   1302      1.33        pk 	 * the MIF configuration register's MDI_X bits. Note that
   1303      1.33        pk 	 * MDI_0 (int) == 0x100 and MDI_1 (ext) == 0x200; see hmereg.h
   1304      1.33        pk 	 */
   1305      1.33        pk 	mif_mdi_bit = 1 << (8 + (1 - phy));
   1306      1.33        pk 	delay(100);
   1307      1.33        pk 	v = bus_space_read_4(t, mif, HME_MIFI_CFG);
   1308  1.99.2.1  christos 	if ((v & mif_mdi_bit) == 0) {
   1309  1.99.2.1  christos 		rv = -1;
   1310  1.99.2.1  christos 		goto out;
   1311  1.99.2.1  christos 	}
   1312      1.33        pk #endif
   1313      1.33        pk 
   1314       1.1        pk 	/* Construct the frame command */
   1315       1.1        pk 	v = (MII_COMMAND_START << HME_MIF_FO_ST_SHIFT)	|
   1316       1.1        pk 	    HME_MIF_FO_TAMSB				|
   1317       1.1        pk 	    (MII_COMMAND_WRITE << HME_MIF_FO_OPC_SHIFT)	|
   1318       1.1        pk 	    (phy << HME_MIF_FO_PHYAD_SHIFT)		|
   1319       1.1        pk 	    (reg << HME_MIF_FO_REGAD_SHIFT)		|
   1320       1.1        pk 	    (val & HME_MIF_FO_DATA);
   1321       1.1        pk 
   1322       1.1        pk 	bus_space_write_4(t, mif, HME_MIFI_FO, v);
   1323       1.1        pk 	for (n = 0; n < 100; n++) {
   1324       1.2        pk 		DELAY(1);
   1325       1.1        pk 		v = bus_space_read_4(t, mif, HME_MIFI_FO);
   1326  1.99.2.1  christos 		if (v & HME_MIF_FO_TALSB) {
   1327  1.99.2.1  christos 			rv = 0;
   1328      1.33        pk 			goto out;
   1329  1.99.2.1  christos 		}
   1330       1.1        pk 	}
   1331       1.1        pk 
   1332  1.99.2.1  christos 	rv = ETIMEDOUT;
   1333      1.79   tsutsui 	printf("%s: mii_write timeout\n", device_xname(sc->sc_dev));
   1334      1.33        pk out:
   1335      1.33        pk 	/* Restore MIFI_CFG register */
   1336      1.33        pk 	bus_space_write_4(t, mif, HME_MIFI_CFG, mifi_cfg);
   1337      1.35        pk 	/* Restore XIF register */
   1338      1.35        pk 	bus_space_write_4(t, mac, HME_MACI_XIF, xif_cfg);
   1339  1.99.2.1  christos 
   1340  1.99.2.1  christos 	return rv;
   1341       1.1        pk }
   1342       1.1        pk 
   1343       1.1        pk static void
   1344      1.89      matt hme_mii_statchg(struct ifnet *ifp)
   1345       1.1        pk {
   1346      1.89      matt 	struct hme_softc *sc = ifp->if_softc;
   1347       1.1        pk 	bus_space_tag_t t = sc->sc_bustag;
   1348       1.1        pk 	bus_space_handle_t mac = sc->sc_mac;
   1349      1.75   tsutsui 	uint32_t v;
   1350       1.1        pk 
   1351       1.5        pk #ifdef HMEDEBUG
   1352       1.5        pk 	if (sc->sc_debug)
   1353      1.33        pk 		printf("hme_mii_statchg: status change\n");
   1354       1.5        pk #endif
   1355       1.1        pk 
   1356       1.5        pk 	/* Set the MAC Full Duplex bit appropriately */
   1357      1.30    martin 	/* Apparently the hme chip is SIMPLEX if working in full duplex mode,
   1358      1.30    martin 	   but not otherwise. */
   1359       1.1        pk 	v = bus_space_read_4(t, mac, HME_MACI_TXCFG);
   1360      1.30    martin 	if ((IFM_OPTIONS(sc->sc_mii.mii_media_active) & IFM_FDX) != 0) {
   1361       1.1        pk 		v |= HME_MAC_TXCFG_FULLDPLX;
   1362      1.30    martin 		sc->sc_ethercom.ec_if.if_flags |= IFF_SIMPLEX;
   1363      1.30    martin 	} else {
   1364       1.1        pk 		v &= ~HME_MAC_TXCFG_FULLDPLX;
   1365      1.30    martin 		sc->sc_ethercom.ec_if.if_flags &= ~IFF_SIMPLEX;
   1366      1.30    martin 	}
   1367      1.41      heas 	sc->sc_if_flags = sc->sc_ethercom.ec_if.if_flags;
   1368       1.1        pk 	bus_space_write_4(t, mac, HME_MACI_TXCFG, v);
   1369       1.5        pk }
   1370       1.5        pk 
   1371       1.5        pk int
   1372      1.71       dsl hme_mediachange(struct ifnet *ifp)
   1373       1.5        pk {
   1374       1.5        pk 	struct hme_softc *sc = ifp->if_softc;
   1375      1.33        pk 	bus_space_tag_t t = sc->sc_bustag;
   1376      1.33        pk 	bus_space_handle_t mif = sc->sc_mif;
   1377      1.33        pk 	bus_space_handle_t mac = sc->sc_mac;
   1378      1.33        pk 	int instance = IFM_INST(sc->sc_mii.mii_media.ifm_cur->ifm_media);
   1379      1.33        pk 	int phy = sc->sc_phys[instance];
   1380      1.61    dyoung 	int rc;
   1381      1.75   tsutsui 	uint32_t v;
   1382       1.5        pk 
   1383      1.33        pk #ifdef HMEDEBUG
   1384      1.33        pk 	if (sc->sc_debug)
   1385      1.33        pk 		printf("hme_mediachange: phy = %d\n", phy);
   1386      1.33        pk #endif
   1387      1.33        pk 
   1388      1.33        pk 	/* Select the current PHY in the MIF configuration register */
   1389      1.33        pk 	v = bus_space_read_4(t, mif, HME_MIFI_CFG);
   1390      1.33        pk 	v &= ~HME_MIF_CFG_PHY;
   1391      1.33        pk 	if (phy == HME_PHYAD_EXTERNAL)
   1392      1.33        pk 		v |= HME_MIF_CFG_PHY;
   1393      1.33        pk 	bus_space_write_4(t, mif, HME_MIFI_CFG, v);
   1394      1.33        pk 
   1395      1.33        pk 	/* If an external transceiver is selected, enable its MII drivers */
   1396      1.33        pk 	v = bus_space_read_4(t, mac, HME_MACI_XIF);
   1397      1.33        pk 	v &= ~HME_MAC_XIF_MIIENABLE;
   1398      1.33        pk 	if (phy == HME_PHYAD_EXTERNAL)
   1399      1.33        pk 		v |= HME_MAC_XIF_MIIENABLE;
   1400      1.33        pk 	bus_space_write_4(t, mac, HME_MACI_XIF, v);
   1401       1.5        pk 
   1402      1.61    dyoung 	if ((rc = mii_mediachg(&sc->sc_mii)) == ENXIO)
   1403      1.61    dyoung 		return 0;
   1404      1.61    dyoung 	return rc;
   1405       1.1        pk }
   1406       1.1        pk 
   1407       1.1        pk /*
   1408       1.1        pk  * Process an ioctl request.
   1409       1.1        pk  */
   1410       1.1        pk int
   1411      1.67    dyoung hme_ioctl(struct ifnet *ifp, unsigned long cmd, void *data)
   1412       1.1        pk {
   1413       1.1        pk 	struct hme_softc *sc = ifp->if_softc;
   1414       1.1        pk 	struct ifaddr *ifa = (struct ifaddr *)data;
   1415       1.1        pk 	int s, error = 0;
   1416       1.1        pk 
   1417       1.1        pk 	s = splnet();
   1418       1.1        pk 
   1419       1.1        pk 	switch (cmd) {
   1420       1.1        pk 
   1421      1.67    dyoung 	case SIOCINITIFADDR:
   1422       1.1        pk 		switch (ifa->ifa_addr->sa_family) {
   1423       1.1        pk #ifdef INET
   1424       1.1        pk 		case AF_INET:
   1425      1.41      heas 			if (ifp->if_flags & IFF_UP)
   1426      1.41      heas 				hme_setladrf(sc);
   1427      1.41      heas 			else {
   1428      1.41      heas 				ifp->if_flags |= IFF_UP;
   1429      1.84  jakllsch 				error = hme_init(ifp);
   1430      1.41      heas 			}
   1431       1.1        pk 			arp_ifinit(ifp, ifa);
   1432       1.1        pk 			break;
   1433       1.1        pk #endif
   1434       1.1        pk 		default:
   1435      1.41      heas 			ifp->if_flags |= IFF_UP;
   1436      1.84  jakllsch 			error = hme_init(ifp);
   1437       1.1        pk 			break;
   1438       1.1        pk 		}
   1439       1.1        pk 		break;
   1440       1.1        pk 
   1441       1.1        pk 	case SIOCSIFFLAGS:
   1442      1.45      heas #ifdef HMEDEBUG
   1443      1.67    dyoung 		{
   1444      1.67    dyoung 			struct ifreq *ifr = data;
   1445      1.67    dyoung 			sc->sc_debug =
   1446      1.67    dyoung 			    (ifr->ifr_flags & IFF_DEBUG) != 0 ? 1 : 0;
   1447      1.67    dyoung 		}
   1448      1.45      heas #endif
   1449      1.67    dyoung 		if ((error = ifioctl_common(ifp, cmd, data)) != 0)
   1450      1.67    dyoung 			break;
   1451      1.45      heas 
   1452  1.99.2.1  christos 		switch (ifp->if_flags & (IFF_UP | IFF_RUNNING)) {
   1453      1.67    dyoung 		case IFF_RUNNING:
   1454       1.1        pk 			/*
   1455       1.1        pk 			 * If interface is marked down and it is running, then
   1456       1.1        pk 			 * stop it.
   1457       1.1        pk 			 */
   1458      1.80   tsutsui 			hme_stop(ifp, 0);
   1459       1.1        pk 			ifp->if_flags &= ~IFF_RUNNING;
   1460      1.67    dyoung 			break;
   1461      1.67    dyoung 		case IFF_UP:
   1462       1.1        pk 			/*
   1463       1.1        pk 			 * If interface is marked up and it is stopped, then
   1464       1.1        pk 			 * start it.
   1465       1.1        pk 			 */
   1466      1.84  jakllsch 			error = hme_init(ifp);
   1467      1.67    dyoung 			break;
   1468  1.99.2.1  christos 		case IFF_UP | IFF_RUNNING:
   1469       1.1        pk 			/*
   1470      1.41      heas 			 * If setting debug or promiscuous mode, do not reset
   1471      1.41      heas 			 * the chip; for everything else, call hme_init()
   1472      1.41      heas 			 * which will trigger a reset.
   1473       1.1        pk 			 */
   1474      1.41      heas #define RESETIGN (IFF_CANTCHANGE | IFF_DEBUG)
   1475      1.46      heas 			if (ifp->if_flags != sc->sc_if_flags) {
   1476      1.45      heas 				if ((ifp->if_flags & (~RESETIGN))
   1477      1.45      heas 				    == (sc->sc_if_flags & (~RESETIGN)))
   1478      1.45      heas 					hme_setladrf(sc);
   1479      1.45      heas 				else
   1480      1.84  jakllsch 					error = hme_init(ifp);
   1481      1.45      heas 			}
   1482      1.41      heas #undef RESETIGN
   1483      1.67    dyoung 			break;
   1484      1.67    dyoung 		case 0:
   1485      1.67    dyoung 			break;
   1486       1.1        pk 		}
   1487      1.45      heas 
   1488      1.45      heas 		if (sc->sc_ec_capenable != sc->sc_ethercom.ec_capenable)
   1489      1.84  jakllsch 			error = hme_init(ifp);
   1490      1.45      heas 
   1491       1.1        pk 		break;
   1492       1.1        pk 
   1493      1.63    dyoung 	default:
   1494      1.63    dyoung 		if ((error = ether_ioctl(ifp, cmd, data)) != ENETRESET)
   1495      1.63    dyoung 			break;
   1496      1.63    dyoung 
   1497      1.63    dyoung 		error = 0;
   1498      1.63    dyoung 
   1499      1.63    dyoung 		if (cmd != SIOCADDMULTI && cmd != SIOCDELMULTI)
   1500      1.63    dyoung 			;
   1501      1.63    dyoung 		else if (ifp->if_flags & IFF_RUNNING) {
   1502       1.1        pk 			/*
   1503       1.1        pk 			 * Multicast list has changed; set the hardware filter
   1504       1.1        pk 			 * accordingly.
   1505       1.1        pk 			 */
   1506      1.63    dyoung 			hme_setladrf(sc);
   1507       1.1        pk 		}
   1508       1.1        pk 		break;
   1509       1.1        pk 	}
   1510       1.1        pk 
   1511      1.41      heas 	sc->sc_if_flags = ifp->if_flags;
   1512       1.1        pk 	splx(s);
   1513       1.1        pk 	return (error);
   1514       1.1        pk }
   1515       1.1        pk 
   1516      1.80   tsutsui bool
   1517      1.80   tsutsui hme_shutdown(device_t self, int howto)
   1518       1.1        pk {
   1519      1.79   tsutsui 	struct hme_softc *sc;
   1520      1.80   tsutsui 	struct ifnet *ifp;
   1521      1.80   tsutsui 
   1522      1.80   tsutsui 	sc = device_private(self);
   1523      1.80   tsutsui 	ifp = &sc->sc_ethercom.ec_if;
   1524      1.80   tsutsui 	hme_stop(ifp, 1);
   1525      1.28      tron 
   1526      1.80   tsutsui 	return true;
   1527       1.1        pk }
   1528       1.1        pk 
   1529       1.1        pk /*
   1530       1.1        pk  * Set up the logical address filter.
   1531       1.1        pk  */
   1532       1.1        pk void
   1533      1.71       dsl hme_setladrf(struct hme_softc *sc)
   1534       1.1        pk {
   1535       1.1        pk 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1536       1.1        pk 	struct ether_multi *enm;
   1537       1.1        pk 	struct ether_multistep step;
   1538      1.28      tron 	struct ethercom *ec = &sc->sc_ethercom;
   1539       1.1        pk 	bus_space_tag_t t = sc->sc_bustag;
   1540       1.1        pk 	bus_space_handle_t mac = sc->sc_mac;
   1541      1.83   tsutsui 	uint32_t v;
   1542      1.75   tsutsui 	uint32_t crc;
   1543      1.75   tsutsui 	uint32_t hash[4];
   1544       1.1        pk 
   1545      1.14        pk 	/* Clear hash table */
   1546      1.14        pk 	hash[3] = hash[2] = hash[1] = hash[0] = 0;
   1547      1.14        pk 
   1548      1.14        pk 	/* Get current RX configuration */
   1549      1.14        pk 	v = bus_space_read_4(t, mac, HME_MACI_RXCFG);
   1550      1.14        pk 
   1551      1.14        pk 	if ((ifp->if_flags & IFF_PROMISC) != 0) {
   1552      1.14        pk 		/* Turn on promiscuous mode; turn off the hash filter */
   1553      1.14        pk 		v |= HME_MAC_RXCFG_PMISC;
   1554      1.14        pk 		v &= ~HME_MAC_RXCFG_HENABLE;
   1555      1.14        pk 		ifp->if_flags |= IFF_ALLMULTI;
   1556      1.14        pk 		goto chipit;
   1557      1.14        pk 	}
   1558      1.14        pk 
   1559      1.14        pk 	/* Turn off promiscuous mode; turn on the hash filter */
   1560      1.14        pk 	v &= ~HME_MAC_RXCFG_PMISC;
   1561      1.14        pk 	v |= HME_MAC_RXCFG_HENABLE;
   1562      1.14        pk 
   1563       1.1        pk 	/*
   1564       1.1        pk 	 * Set up multicast address filter by passing all multicast addresses
   1565       1.1        pk 	 * through a crc generator, and then using the high order 6 bits as an
   1566       1.1        pk 	 * index into the 64 bit logical address filter.  The high order bit
   1567       1.1        pk 	 * selects the word, while the rest of the bits select the bit within
   1568       1.1        pk 	 * the word.
   1569       1.1        pk 	 */
   1570       1.1        pk 
   1571  1.99.2.1  christos 	ETHER_LOCK(ec);
   1572      1.28      tron 	ETHER_FIRST_MULTI(step, ec, enm);
   1573       1.1        pk 	while (enm != NULL) {
   1574      1.70   tsutsui 		if (memcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
   1575       1.1        pk 			/*
   1576       1.1        pk 			 * We must listen to a range of multicast addresses.
   1577       1.1        pk 			 * For now, just accept all multicasts, rather than
   1578       1.1        pk 			 * trying to set only those filter bits needed to match
   1579       1.1        pk 			 * the range.  (At this time, the only use of address
   1580       1.1        pk 			 * ranges is for IP multicast routing, for which the
   1581       1.1        pk 			 * range is big enough to require all bits set.)
   1582       1.1        pk 			 */
   1583      1.14        pk 			hash[3] = hash[2] = hash[1] = hash[0] = 0xffff;
   1584      1.14        pk 			ifp->if_flags |= IFF_ALLMULTI;
   1585  1.99.2.1  christos 			ETHER_UNLOCK(ec);
   1586      1.14        pk 			goto chipit;
   1587       1.1        pk 		}
   1588       1.1        pk 
   1589      1.83   tsutsui 		crc = ether_crc32_le(enm->enm_addrlo, ETHER_ADDR_LEN);
   1590      1.83   tsutsui 
   1591       1.1        pk 		/* Just want the 6 most significant bits. */
   1592       1.1        pk 		crc >>= 26;
   1593       1.1        pk 
   1594       1.1        pk 		/* Set the corresponding bit in the filter. */
   1595       1.1        pk 		hash[crc >> 4] |= 1 << (crc & 0xf);
   1596       1.1        pk 
   1597       1.1        pk 		ETHER_NEXT_MULTI(step, enm);
   1598       1.1        pk 	}
   1599  1.99.2.1  christos 	ETHER_UNLOCK(ec);
   1600       1.1        pk 
   1601      1.14        pk 	ifp->if_flags &= ~IFF_ALLMULTI;
   1602      1.14        pk 
   1603      1.14        pk chipit:
   1604      1.14        pk 	/* Now load the hash table into the chip */
   1605       1.1        pk 	bus_space_write_4(t, mac, HME_MACI_HASHTAB0, hash[0]);
   1606       1.1        pk 	bus_space_write_4(t, mac, HME_MACI_HASHTAB1, hash[1]);
   1607       1.1        pk 	bus_space_write_4(t, mac, HME_MACI_HASHTAB2, hash[2]);
   1608       1.1        pk 	bus_space_write_4(t, mac, HME_MACI_HASHTAB3, hash[3]);
   1609      1.14        pk 	bus_space_write_4(t, mac, HME_MACI_RXCFG, v);
   1610       1.1        pk }
   1611       1.1        pk 
   1612      1.28      tron /*
   1613      1.28      tron  * Routines for accessing the transmit and receive buffers.
   1614      1.28      tron  * The various CPU and adapter configurations supported by this
   1615      1.28      tron  * driver require three different access methods for buffers
   1616      1.28      tron  * and descriptors:
   1617      1.28      tron  *	(1) contig (contiguous data; no padding),
   1618      1.28      tron  *	(2) gap2 (two bytes of data followed by two bytes of padding),
   1619      1.28      tron  *	(3) gap16 (16 bytes of data followed by 16 bytes of padding).
   1620      1.28      tron  */
   1621      1.28      tron 
   1622      1.28      tron #if 0
   1623      1.28      tron /*
   1624      1.28      tron  * contig: contiguous data with no padding.
   1625      1.28      tron  *
   1626      1.28      tron  * Buffers may have any alignment.
   1627      1.28      tron  */
   1628      1.28      tron 
   1629      1.28      tron void
   1630      1.72       dsl hme_copytobuf_contig(struct hme_softc *sc, void *from, int ri, int len)
   1631      1.26      tron {
   1632      1.56  christos 	volatile void *buf = sc->sc_rb.rb_txbuf + (ri * _HME_BUFSZ);
   1633      1.26      tron 
   1634       1.1        pk 	/*
   1635      1.28      tron 	 * Just call memcpy() to do the work.
   1636       1.1        pk 	 */
   1637      1.28      tron 	memcpy(buf, from, len);
   1638       1.1        pk }
   1639       1.1        pk 
   1640      1.28      tron void
   1641      1.72       dsl hme_copyfrombuf_contig(struct hme_softc *sc, void *to, int boff, int len)
   1642       1.1        pk {
   1643      1.56  christos 	volatile void *buf = sc->sc_rb.rb_rxbuf + (ri * _HME_BUFSZ);
   1644      1.26      tron 
   1645      1.28      tron 	/*
   1646      1.28      tron 	 * Just call memcpy() to do the work.
   1647      1.28      tron 	 */
   1648      1.28      tron 	memcpy(to, buf, len);
   1649       1.1        pk }
   1650      1.28      tron #endif
   1651