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