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