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