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