Home | History | Annotate | Line # | Download | only in ic
hme.c revision 1.19
      1 /*	$NetBSD: hme.c,v 1.19 2000/11/17 19:08:00 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 	/* claim 802.1q capability */
    306 	sc->sc_ethercom.ec_capabilities |= ETHERCAP_VLAN_MTU;
    307 
    308 	/* Attach the interface. */
    309 	if_attach(ifp);
    310 	ether_ifattach(ifp, sc->sc_enaddr);
    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(sc->sc_pci, hr->rb_txd, i, txbufdma + i * _HME_BUFSZ);
    443 		HME_XD_SETFLAGS(sc->sc_pci, 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(sc->sc_pci, hr->rb_rxd, i, rxbufdma + i * _HME_BUFSZ);
    451 		HME_XD_SETFLAGS(sc->sc_pci, 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 	bus_space_write_4(t, mac, HME_MACI_TXSIZE,
    509 	    (sc->sc_ethercom.ec_capenable & ETHERCAP_VLAN_MTU) ?
    510 	    ETHER_VLAN_ENCAP_LEN + ETHER_MAX_LEN :
    511             ETHER_MAX_LEN);
    512 
    513 	/* Load station MAC address */
    514 	ea = sc->sc_enaddr;
    515 	bus_space_write_4(t, mac, HME_MACI_MACADDR0, (ea[0] << 8) | ea[1]);
    516 	bus_space_write_4(t, mac, HME_MACI_MACADDR1, (ea[2] << 8) | ea[3]);
    517 	bus_space_write_4(t, mac, HME_MACI_MACADDR2, (ea[4] << 8) | ea[5]);
    518 
    519 	/*
    520 	 * Init seed for backoff
    521 	 * (source suggested by manual: low 10 bits of MAC address)
    522 	 */
    523 	v = ((ea[4] << 8) | ea[5]) & 0x3fff;
    524 	bus_space_write_4(t, mac, HME_MACI_RANDSEED, v);
    525 
    526 
    527 	/* Note: Accepting power-on default for other MAC registers here.. */
    528 
    529 
    530 	/* step 5. RX MAC registers & counters */
    531 	hme_setladrf(sc);
    532 
    533 	/* step 6 & 7. Program Descriptor Ring Base Addresses */
    534 	bus_space_write_4(t, etx, HME_ETXI_RING, sc->sc_rb.rb_txddma);
    535 	bus_space_write_4(t, etx, HME_ETXI_RSIZE, sc->sc_rb.rb_ntbuf);
    536 
    537 	bus_space_write_4(t, erx, HME_ERXI_RING, sc->sc_rb.rb_rxddma);
    538 	bus_space_write_4(t, mac, HME_MACI_RXSIZE,
    539 	    (sc->sc_ethercom.ec_capenable & ETHERCAP_VLAN_MTU) ?
    540 	    ETHER_VLAN_ENCAP_LEN + ETHER_MAX_LEN :
    541             ETHER_MAX_LEN);
    542 
    543 
    544 	/* step 8. Global Configuration & Interrupt Mask */
    545 	bus_space_write_4(t, seb, HME_SEBI_IMASK,
    546 			~(
    547 			  /*HME_SEB_STAT_GOTFRAME | HME_SEB_STAT_SENTFRAME |*/
    548 			  HME_SEB_STAT_HOSTTOTX |
    549 			  HME_SEB_STAT_RXTOHOST |
    550 			  HME_SEB_STAT_TXALL |
    551 			  HME_SEB_STAT_TXPERR |
    552 			  HME_SEB_STAT_RCNTEXP |
    553 			  HME_SEB_STAT_ALL_ERRORS ));
    554 
    555 	switch (sc->sc_burst) {
    556 	default:
    557 		v = 0;
    558 		break;
    559 	case 16:
    560 		v = HME_SEB_CFG_BURST16;
    561 		break;
    562 	case 32:
    563 		v = HME_SEB_CFG_BURST32;
    564 		break;
    565 	case 64:
    566 		v = HME_SEB_CFG_BURST64;
    567 		break;
    568 	}
    569 	bus_space_write_4(t, seb, HME_SEBI_CFG, v);
    570 
    571 	/* step 9. ETX Configuration: use mostly default values */
    572 
    573 	/* Enable DMA */
    574 	v = bus_space_read_4(t, etx, HME_ETXI_CFG);
    575 	v |= HME_ETX_CFG_DMAENABLE;
    576 	bus_space_write_4(t, etx, HME_ETXI_CFG, v);
    577 
    578 	/* Transmit Descriptor ring size: in increments of 16 */
    579 	bus_space_write_4(t, etx, HME_ETXI_RSIZE, _HME_NDESC / 16 - 1);
    580 
    581 
    582 	/* step 10. ERX Configuration */
    583 	v = bus_space_read_4(t, erx, HME_ERXI_CFG);
    584 
    585 	/* Encode Receive Descriptor ring size: four possible values */
    586 	switch (_HME_NDESC /*XXX*/) {
    587 	case 32:
    588 		v |= HME_ERX_CFG_RINGSIZE32;
    589 		break;
    590 	case 64:
    591 		v |= HME_ERX_CFG_RINGSIZE64;
    592 		break;
    593 	case 128:
    594 		v |= HME_ERX_CFG_RINGSIZE128;
    595 		break;
    596 	case 256:
    597 		v |= HME_ERX_CFG_RINGSIZE256;
    598 		break;
    599 	default:
    600 		printf("hme: invalid Receive Descriptor ring size\n");
    601 		break;
    602 	}
    603 
    604 	/* Enable DMA */
    605 	v |= HME_ERX_CFG_DMAENABLE;
    606 	bus_space_write_4(t, erx, HME_ERXI_CFG, v);
    607 
    608 	/* step 11. XIF Configuration */
    609 	v = bus_space_read_4(t, mac, HME_MACI_XIF);
    610 	v |= HME_MAC_XIF_OE;
    611 	/* If an external transceiver is connected, enable its MII drivers */
    612 	if ((bus_space_read_4(t, mif, HME_MIFI_CFG) & HME_MIF_CFG_MDI1) != 0)
    613 		v |= HME_MAC_XIF_MIIENABLE;
    614 	bus_space_write_4(t, mac, HME_MACI_XIF, v);
    615 
    616 
    617 	/* step 12. RX_MAC Configuration Register */
    618 	v = bus_space_read_4(t, mac, HME_MACI_RXCFG);
    619 	v |= HME_MAC_RXCFG_ENABLE;
    620 	bus_space_write_4(t, mac, HME_MACI_RXCFG, v);
    621 
    622 	/* step 13. TX_MAC Configuration Register */
    623 	v = bus_space_read_4(t, mac, HME_MACI_TXCFG);
    624 	v |= (HME_MAC_TXCFG_ENABLE | HME_MAC_TXCFG_DGIVEUP);
    625 	bus_space_write_4(t, mac, HME_MACI_TXCFG, v);
    626 
    627 	/* step 14. Issue Transmit Pending command */
    628 
    629 	/* Call MI initialization function if any */
    630 	if (sc->sc_hwinit)
    631 		(*sc->sc_hwinit)(sc);
    632 
    633 	/* Start the one second timer. */
    634 	callout_reset(&sc->sc_tick_ch, hz, hme_tick, sc);
    635 
    636 	ifp->if_flags |= IFF_RUNNING;
    637 	ifp->if_flags &= ~IFF_OACTIVE;
    638 	ifp->if_timer = 0;
    639 	hme_start(ifp);
    640 }
    641 
    642 /*
    643  * Compare two Ether/802 addresses for equality, inlined and unrolled for
    644  * speed.
    645  */
    646 static __inline__ int
    647 ether_cmp(a, b)
    648 	u_char *a, *b;
    649 {
    650 
    651 	if (a[5] != b[5] || a[4] != b[4] || a[3] != b[3] ||
    652 	    a[2] != b[2] || a[1] != b[1] || a[0] != b[0])
    653 		return (0);
    654 	return (1);
    655 }
    656 
    657 
    658 /*
    659  * Routine to copy from mbuf chain to transmit buffer in
    660  * network buffer memory.
    661  * Returns the amount of data copied.
    662  */
    663 int
    664 hme_put(sc, ri, m)
    665 	struct hme_softc *sc;
    666 	int ri;			/* Ring index */
    667 	struct mbuf *m;
    668 {
    669 	struct mbuf *n;
    670 	int len, tlen = 0;
    671 	caddr_t bp;
    672 
    673 	bp = sc->sc_rb.rb_txbuf + (ri % sc->sc_rb.rb_ntbuf) * _HME_BUFSZ;
    674 	for (; m; m = n) {
    675 		len = m->m_len;
    676 		if (len == 0) {
    677 			MFREE(m, n);
    678 			continue;
    679 		}
    680 		bcopy(mtod(m, caddr_t), bp, len);
    681 		bp += len;
    682 		tlen += len;
    683 		MFREE(m, n);
    684 	}
    685 	return (tlen);
    686 }
    687 
    688 /*
    689  * Pull data off an interface.
    690  * Len is length of data, with local net header stripped.
    691  * We copy the data into mbufs.  When full cluster sized units are present
    692  * we copy into clusters.
    693  */
    694 struct mbuf *
    695 hme_get(sc, ri, totlen)
    696 	struct hme_softc *sc;
    697 	int ri, totlen;
    698 {
    699 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    700 	struct mbuf *m, *m0, *newm;
    701 	caddr_t bp;
    702 	int len;
    703 
    704 	MGETHDR(m0, M_DONTWAIT, MT_DATA);
    705 	if (m0 == 0)
    706 		return (0);
    707 	m0->m_pkthdr.rcvif = ifp;
    708 	m0->m_pkthdr.len = totlen;
    709 	len = MHLEN;
    710 	m = m0;
    711 
    712 	bp = sc->sc_rb.rb_rxbuf + (ri % sc->sc_rb.rb_nrbuf) * _HME_BUFSZ;
    713 
    714 	while (totlen > 0) {
    715 		if (totlen >= MINCLSIZE) {
    716 			MCLGET(m, M_DONTWAIT);
    717 			if ((m->m_flags & M_EXT) == 0)
    718 				goto bad;
    719 			len = MCLBYTES;
    720 		}
    721 
    722 		if (m == m0) {
    723 			caddr_t newdata = (caddr_t)
    724 			    ALIGN(m->m_data + sizeof(struct ether_header)) -
    725 			    sizeof(struct ether_header);
    726 			len -= newdata - m->m_data;
    727 			m->m_data = newdata;
    728 		}
    729 
    730 		m->m_len = len = min(totlen, len);
    731 		bcopy(bp, mtod(m, caddr_t), len);
    732 		bp += len;
    733 
    734 		totlen -= len;
    735 		if (totlen > 0) {
    736 			MGET(newm, M_DONTWAIT, MT_DATA);
    737 			if (newm == 0)
    738 				goto bad;
    739 			len = MLEN;
    740 			m = m->m_next = newm;
    741 		}
    742 	}
    743 
    744 	return (m0);
    745 
    746 bad:
    747 	m_freem(m0);
    748 	return (0);
    749 }
    750 
    751 /*
    752  * Pass a packet to the higher levels.
    753  */
    754 void
    755 hme_read(sc, ix, len)
    756 	struct hme_softc *sc;
    757 	int ix, len;
    758 {
    759 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    760 	struct mbuf *m;
    761 
    762 	if (len <= sizeof(struct ether_header) ||
    763 	    len > ((sc->sc_ethercom.ec_capenable & ETHERCAP_VLAN_MTU) ?
    764 	    ETHER_VLAN_ENCAP_LEN + ETHERMTU + sizeof(struct ether_header) :
    765 	    ETHERMTU + sizeof(struct ether_header))) {
    766 #ifdef HMEDEBUG
    767 		printf("%s: invalid packet size %d; dropping\n",
    768 		    sc->sc_dev.dv_xname, len);
    769 #endif
    770 		ifp->if_ierrors++;
    771 		return;
    772 	}
    773 
    774 	/* Pull packet off interface. */
    775 	m = hme_get(sc, ix, len);
    776 	if (m == 0) {
    777 		ifp->if_ierrors++;
    778 		return;
    779 	}
    780 
    781 	ifp->if_ipackets++;
    782 
    783 #if NBPFILTER > 0
    784 	/*
    785 	 * Check if there's a BPF listener on this interface.
    786 	 * If so, hand off the raw packet to BPF.
    787 	 */
    788 	if (ifp->if_bpf)
    789 		bpf_mtap(ifp->if_bpf, m);
    790 #endif
    791 
    792 	/* Pass the packet up. */
    793 	(*ifp->if_input)(ifp, m);
    794 }
    795 
    796 void
    797 hme_start(ifp)
    798 	struct ifnet *ifp;
    799 {
    800 	struct hme_softc *sc = (struct hme_softc *)ifp->if_softc;
    801 	caddr_t txd = sc->sc_rb.rb_txd;
    802 	struct mbuf *m;
    803 	unsigned int ri, len;
    804 	unsigned int ntbuf = sc->sc_rb.rb_ntbuf;
    805 
    806 	if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
    807 		return;
    808 
    809 	ri = sc->sc_rb.rb_tdhead;
    810 
    811 	for (;;) {
    812 		IF_DEQUEUE(&ifp->if_snd, m);
    813 		if (m == 0)
    814 			break;
    815 
    816 #if NBPFILTER > 0
    817 		/*
    818 		 * If BPF is listening on this interface, let it see the
    819 		 * packet before we commit it to the wire.
    820 		 */
    821 		if (ifp->if_bpf)
    822 			bpf_mtap(ifp->if_bpf, m);
    823 #endif
    824 
    825 		/*
    826 		 * Copy the mbuf chain into the transmit buffer.
    827 		 */
    828 		len = hme_put(sc, ri, m);
    829 
    830 		/*
    831 		 * Initialize transmit registers and start transmission
    832 		 */
    833 		HME_XD_SETFLAGS(sc->sc_pci, txd, ri,
    834 			HME_XD_OWN | HME_XD_SOP | HME_XD_EOP |
    835 			HME_XD_ENCODE_TSIZE(len));
    836 
    837 		/*if (sc->sc_rb.rb_td_nbusy <= 0)*/
    838 		bus_space_write_4(sc->sc_bustag, sc->sc_etx, HME_ETXI_PENDING,
    839 				  HME_ETX_TP_DMAWAKEUP);
    840 
    841 		if (++ri == ntbuf)
    842 			ri = 0;
    843 
    844 		if (++sc->sc_rb.rb_td_nbusy == ntbuf) {
    845 			ifp->if_flags |= IFF_OACTIVE;
    846 			break;
    847 		}
    848 	}
    849 
    850 	sc->sc_rb.rb_tdhead = ri;
    851 }
    852 
    853 /*
    854  * Transmit interrupt.
    855  */
    856 int
    857 hme_tint(sc)
    858 	struct hme_softc *sc;
    859 {
    860 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    861 	bus_space_tag_t t = sc->sc_bustag;
    862 	bus_space_handle_t mac = sc->sc_mac;
    863 	unsigned int ri, txflags;
    864 
    865 	/*
    866 	 * Unload collision counters
    867 	 */
    868 	ifp->if_collisions +=
    869 		bus_space_read_4(t, mac, HME_MACI_NCCNT) +
    870 		bus_space_read_4(t, mac, HME_MACI_FCCNT) +
    871 		bus_space_read_4(t, mac, HME_MACI_EXCNT) +
    872 		bus_space_read_4(t, mac, HME_MACI_LTCNT);
    873 
    874 	/*
    875 	 * then clear the hardware counters.
    876 	 */
    877 	bus_space_write_4(t, mac, HME_MACI_NCCNT, 0);
    878 	bus_space_write_4(t, mac, HME_MACI_FCCNT, 0);
    879 	bus_space_write_4(t, mac, HME_MACI_EXCNT, 0);
    880 	bus_space_write_4(t, mac, HME_MACI_LTCNT, 0);
    881 
    882 	/* Fetch current position in the transmit ring */
    883 	ri = sc->sc_rb.rb_tdtail;
    884 
    885 	for (;;) {
    886 		if (sc->sc_rb.rb_td_nbusy <= 0)
    887 			break;
    888 
    889 		txflags = HME_XD_GETFLAGS(sc->sc_pci, sc->sc_rb.rb_txd, ri);
    890 
    891 		if (txflags & HME_XD_OWN)
    892 			break;
    893 
    894 		ifp->if_flags &= ~IFF_OACTIVE;
    895 		ifp->if_opackets++;
    896 
    897 		if (++ri == sc->sc_rb.rb_ntbuf)
    898 			ri = 0;
    899 
    900 		--sc->sc_rb.rb_td_nbusy;
    901 	}
    902 
    903 	/* Update ring */
    904 	sc->sc_rb.rb_tdtail = ri;
    905 
    906 	hme_start(ifp);
    907 
    908 	if (sc->sc_rb.rb_td_nbusy == 0)
    909 		ifp->if_timer = 0;
    910 
    911 	return (1);
    912 }
    913 
    914 /*
    915  * Receive interrupt.
    916  */
    917 int
    918 hme_rint(sc)
    919 	struct hme_softc *sc;
    920 {
    921 	caddr_t xdr = sc->sc_rb.rb_rxd;
    922 	unsigned int nrbuf = sc->sc_rb.rb_nrbuf;
    923 	unsigned int ri, len;
    924 	u_int32_t flags;
    925 
    926 	ri = sc->sc_rb.rb_rdtail;
    927 
    928 	/*
    929 	 * Process all buffers with valid data.
    930 	 */
    931 	for (;;) {
    932 		flags = HME_XD_GETFLAGS(sc->sc_pci, xdr, ri);
    933 		if (flags & HME_XD_OWN)
    934 			break;
    935 
    936 		if (flags & HME_XD_OFL) {
    937 			printf("%s: buffer overflow, ri=%d; flags=0x%x\n",
    938 					sc->sc_dev.dv_xname, ri, flags);
    939 		} else {
    940 			len = HME_XD_DECODE_RSIZE(flags);
    941 			hme_read(sc, ri, len);
    942 		}
    943 
    944 		/* This buffer can be used by the hardware again */
    945 		HME_XD_SETFLAGS(sc->sc_pci, xdr, ri,
    946 				HME_XD_OWN | HME_XD_ENCODE_RSIZE(_HME_BUFSZ));
    947 
    948 		if (++ri == nrbuf)
    949 			ri = 0;
    950 	}
    951 
    952 	sc->sc_rb.rb_rdtail = ri;
    953 
    954 	return (1);
    955 }
    956 
    957 int
    958 hme_eint(sc, status)
    959 	struct hme_softc *sc;
    960 	u_int status;
    961 {
    962 	char bits[128];
    963 
    964 	if ((status & HME_SEB_STAT_MIFIRQ) != 0) {
    965 		printf("%s: XXXlink status changed\n", sc->sc_dev.dv_xname);
    966 		return (1);
    967 	}
    968 
    969 	printf("%s: status=%s\n", sc->sc_dev.dv_xname,
    970 		bitmask_snprintf(status, HME_SEB_STAT_BITS, bits,sizeof(bits)));
    971 	return (1);
    972 }
    973 
    974 int
    975 hme_intr(v)
    976 	void *v;
    977 {
    978 	struct hme_softc *sc = (struct hme_softc *)v;
    979 	bus_space_tag_t t = sc->sc_bustag;
    980 	bus_space_handle_t seb = sc->sc_seb;
    981 	u_int32_t status;
    982 	int r = 0;
    983 
    984 	status = bus_space_read_4(t, seb, HME_SEBI_STAT);
    985 
    986 	if ((status & HME_SEB_STAT_ALL_ERRORS) != 0)
    987 		r |= hme_eint(sc, status);
    988 
    989 	if ((status & (HME_SEB_STAT_TXALL | HME_SEB_STAT_HOSTTOTX)) != 0)
    990 		r |= hme_tint(sc);
    991 
    992 	if ((status & HME_SEB_STAT_RXTOHOST) != 0)
    993 		r |= hme_rint(sc);
    994 
    995 	return (r);
    996 }
    997 
    998 
    999 void
   1000 hme_watchdog(ifp)
   1001 	struct ifnet *ifp;
   1002 {
   1003 	struct hme_softc *sc = ifp->if_softc;
   1004 
   1005 	log(LOG_ERR, "%s: device timeout\n", sc->sc_dev.dv_xname);
   1006 	++ifp->if_oerrors;
   1007 
   1008 	hme_reset(sc);
   1009 }
   1010 
   1011 /*
   1012  * Initialize the MII Management Interface
   1013  */
   1014 void
   1015 hme_mifinit(sc)
   1016 	struct hme_softc *sc;
   1017 {
   1018 	bus_space_tag_t t = sc->sc_bustag;
   1019 	bus_space_handle_t mif = sc->sc_mif;
   1020 	u_int32_t v;
   1021 
   1022 	/* Configure the MIF in frame mode */
   1023 	v = bus_space_read_4(t, mif, HME_MIFI_CFG);
   1024 	v &= ~HME_MIF_CFG_BBMODE;
   1025 	bus_space_write_4(t, mif, HME_MIFI_CFG, v);
   1026 }
   1027 
   1028 /*
   1029  * MII interface
   1030  */
   1031 static int
   1032 hme_mii_readreg(self, phy, reg)
   1033 	struct device *self;
   1034 	int phy, reg;
   1035 {
   1036 	struct hme_softc *sc = (void *)self;
   1037 	bus_space_tag_t t = sc->sc_bustag;
   1038 	bus_space_handle_t mif = sc->sc_mif;
   1039 	int n;
   1040 	u_int32_t v;
   1041 
   1042 	/* Select the desired PHY in the MIF configuration register */
   1043 	v = bus_space_read_4(t, mif, HME_MIFI_CFG);
   1044 	/* Clear PHY select bit */
   1045 	v &= ~HME_MIF_CFG_PHY;
   1046 	if (phy == HME_PHYAD_EXTERNAL)
   1047 		/* Set PHY select bit to get at external device */
   1048 		v |= HME_MIF_CFG_PHY;
   1049 	bus_space_write_4(t, mif, HME_MIFI_CFG, v);
   1050 
   1051 	/* Construct the frame command */
   1052 	v = (MII_COMMAND_START << HME_MIF_FO_ST_SHIFT) |
   1053 	    HME_MIF_FO_TAMSB |
   1054 	    (MII_COMMAND_READ << HME_MIF_FO_OPC_SHIFT) |
   1055 	    (phy << HME_MIF_FO_PHYAD_SHIFT) |
   1056 	    (reg << HME_MIF_FO_REGAD_SHIFT);
   1057 
   1058 	bus_space_write_4(t, mif, HME_MIFI_FO, v);
   1059 	for (n = 0; n < 100; n++) {
   1060 		DELAY(1);
   1061 		v = bus_space_read_4(t, mif, HME_MIFI_FO);
   1062 		if (v & HME_MIF_FO_TALSB)
   1063 			return (v & HME_MIF_FO_DATA);
   1064 	}
   1065 
   1066 	printf("%s: mii_read timeout\n", sc->sc_dev.dv_xname);
   1067 	return (0);
   1068 }
   1069 
   1070 static void
   1071 hme_mii_writereg(self, phy, reg, val)
   1072 	struct device *self;
   1073 	int phy, reg, val;
   1074 {
   1075 	struct hme_softc *sc = (void *)self;
   1076 	bus_space_tag_t t = sc->sc_bustag;
   1077 	bus_space_handle_t mif = sc->sc_mif;
   1078 	int n;
   1079 	u_int32_t v;
   1080 
   1081 	/* Select the desired PHY in the MIF configuration register */
   1082 	v = bus_space_read_4(t, mif, HME_MIFI_CFG);
   1083 	/* Clear PHY select bit */
   1084 	v &= ~HME_MIF_CFG_PHY;
   1085 	if (phy == HME_PHYAD_EXTERNAL)
   1086 		/* Set PHY select bit to get at external device */
   1087 		v |= HME_MIF_CFG_PHY;
   1088 	bus_space_write_4(t, mif, HME_MIFI_CFG, v);
   1089 
   1090 	/* Construct the frame command */
   1091 	v = (MII_COMMAND_START << HME_MIF_FO_ST_SHIFT)	|
   1092 	    HME_MIF_FO_TAMSB				|
   1093 	    (MII_COMMAND_WRITE << HME_MIF_FO_OPC_SHIFT)	|
   1094 	    (phy << HME_MIF_FO_PHYAD_SHIFT)		|
   1095 	    (reg << HME_MIF_FO_REGAD_SHIFT)		|
   1096 	    (val & HME_MIF_FO_DATA);
   1097 
   1098 	bus_space_write_4(t, mif, HME_MIFI_FO, v);
   1099 	for (n = 0; n < 100; n++) {
   1100 		DELAY(1);
   1101 		v = bus_space_read_4(t, mif, HME_MIFI_FO);
   1102 		if (v & HME_MIF_FO_TALSB)
   1103 			return;
   1104 	}
   1105 
   1106 	printf("%s: mii_write timeout\n", sc->sc_dev.dv_xname);
   1107 }
   1108 
   1109 static void
   1110 hme_mii_statchg(dev)
   1111 	struct device *dev;
   1112 {
   1113 	struct hme_softc *sc = (void *)dev;
   1114 	int instance = IFM_INST(sc->sc_mii.mii_media.ifm_cur->ifm_media);
   1115 	int phy = sc->sc_phys[instance];
   1116 	bus_space_tag_t t = sc->sc_bustag;
   1117 	bus_space_handle_t mif = sc->sc_mif;
   1118 	bus_space_handle_t mac = sc->sc_mac;
   1119 	u_int32_t v;
   1120 
   1121 #ifdef HMEDEBUG
   1122 	if (sc->sc_debug)
   1123 		printf("hme_mii_statchg: status change: phy = %d\n", phy);
   1124 #endif
   1125 
   1126 	/* Select the current PHY in the MIF configuration register */
   1127 	v = bus_space_read_4(t, mif, HME_MIFI_CFG);
   1128 	v &= ~HME_MIF_CFG_PHY;
   1129 	if (phy == HME_PHYAD_EXTERNAL)
   1130 		v |= HME_MIF_CFG_PHY;
   1131 	bus_space_write_4(t, mif, HME_MIFI_CFG, v);
   1132 
   1133 	/* Set the MAC Full Duplex bit appropriately */
   1134 	v = bus_space_read_4(t, mac, HME_MACI_TXCFG);
   1135 	if ((IFM_OPTIONS(sc->sc_mii.mii_media_active) & IFM_FDX) != 0)
   1136 		v |= HME_MAC_TXCFG_FULLDPLX;
   1137 	else
   1138 		v &= ~HME_MAC_TXCFG_FULLDPLX;
   1139 	bus_space_write_4(t, mac, HME_MACI_TXCFG, v);
   1140 
   1141 	/* If an external transceiver is selected, enable its MII drivers */
   1142 	v = bus_space_read_4(t, mac, HME_MACI_XIF);
   1143 	v &= ~HME_MAC_XIF_MIIENABLE;
   1144 	if (phy == HME_PHYAD_EXTERNAL)
   1145 		v |= HME_MAC_XIF_MIIENABLE;
   1146 	bus_space_write_4(t, mac, HME_MACI_XIF, v);
   1147 }
   1148 
   1149 int
   1150 hme_mediachange(ifp)
   1151 	struct ifnet *ifp;
   1152 {
   1153 	struct hme_softc *sc = ifp->if_softc;
   1154 
   1155 	if (IFM_TYPE(sc->sc_media.ifm_media) != IFM_ETHER)
   1156 		return (EINVAL);
   1157 
   1158 	return (mii_mediachg(&sc->sc_mii));
   1159 }
   1160 
   1161 void
   1162 hme_mediastatus(ifp, ifmr)
   1163 	struct ifnet *ifp;
   1164 	struct ifmediareq *ifmr;
   1165 {
   1166 	struct hme_softc *sc = ifp->if_softc;
   1167 
   1168 	if ((ifp->if_flags & IFF_UP) == 0)
   1169 		return;
   1170 
   1171 	mii_pollstat(&sc->sc_mii);
   1172 	ifmr->ifm_active = sc->sc_mii.mii_media_active;
   1173 	ifmr->ifm_status = sc->sc_mii.mii_media_status;
   1174 }
   1175 
   1176 /*
   1177  * Process an ioctl request.
   1178  */
   1179 int
   1180 hme_ioctl(ifp, cmd, data)
   1181 	struct ifnet *ifp;
   1182 	u_long cmd;
   1183 	caddr_t data;
   1184 {
   1185 	struct hme_softc *sc = ifp->if_softc;
   1186 	struct ifaddr *ifa = (struct ifaddr *)data;
   1187 	struct ifreq *ifr = (struct ifreq *)data;
   1188 	int s, error = 0;
   1189 
   1190 	s = splnet();
   1191 
   1192 	switch (cmd) {
   1193 
   1194 	case SIOCSIFADDR:
   1195 		ifp->if_flags |= IFF_UP;
   1196 
   1197 		switch (ifa->ifa_addr->sa_family) {
   1198 #ifdef INET
   1199 		case AF_INET:
   1200 			hme_init(sc);
   1201 			arp_ifinit(ifp, ifa);
   1202 			break;
   1203 #endif
   1204 #ifdef NS
   1205 		case AF_NS:
   1206 		    {
   1207 			struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
   1208 
   1209 			if (ns_nullhost(*ina))
   1210 				ina->x_host =
   1211 				    *(union ns_host *)LLADDR(ifp->if_sadl);
   1212 			else {
   1213 				bcopy(ina->x_host.c_host,
   1214 				    LLADDR(ifp->if_sadl),
   1215 				    sizeof(sc->sc_enaddr));
   1216 			}
   1217 			/* Set new address. */
   1218 			hme_init(sc);
   1219 			break;
   1220 		    }
   1221 #endif
   1222 		default:
   1223 			hme_init(sc);
   1224 			break;
   1225 		}
   1226 		break;
   1227 
   1228 	case SIOCSIFFLAGS:
   1229 		if ((ifp->if_flags & IFF_UP) == 0 &&
   1230 		    (ifp->if_flags & IFF_RUNNING) != 0) {
   1231 			/*
   1232 			 * If interface is marked down and it is running, then
   1233 			 * stop it.
   1234 			 */
   1235 			hme_stop(sc);
   1236 			ifp->if_flags &= ~IFF_RUNNING;
   1237 		} else if ((ifp->if_flags & IFF_UP) != 0 &&
   1238 		    	   (ifp->if_flags & IFF_RUNNING) == 0) {
   1239 			/*
   1240 			 * If interface is marked up and it is stopped, then
   1241 			 * start it.
   1242 			 */
   1243 			hme_init(sc);
   1244 		} else if ((ifp->if_flags & IFF_UP) != 0) {
   1245 			/*
   1246 			 * Reset the interface to pick up changes in any other
   1247 			 * flags that affect hardware registers.
   1248 			 */
   1249 			/*hme_stop(sc);*/
   1250 			hme_init(sc);
   1251 		}
   1252 #ifdef HMEDEBUG
   1253 		sc->sc_debug = (ifp->if_flags & IFF_DEBUG) != 0 ? 1 : 0;
   1254 #endif
   1255 		break;
   1256 
   1257 	case SIOCADDMULTI:
   1258 	case SIOCDELMULTI:
   1259 		error = (cmd == SIOCADDMULTI) ?
   1260 		    ether_addmulti(ifr, &sc->sc_ethercom) :
   1261 		    ether_delmulti(ifr, &sc->sc_ethercom);
   1262 
   1263 		if (error == ENETRESET) {
   1264 			/*
   1265 			 * Multicast list has changed; set the hardware filter
   1266 			 * accordingly.
   1267 			 */
   1268 			hme_setladrf(sc);
   1269 			error = 0;
   1270 		}
   1271 		break;
   1272 
   1273 	case SIOCGIFMEDIA:
   1274 	case SIOCSIFMEDIA:
   1275 		error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
   1276 		break;
   1277 
   1278 	default:
   1279 		error = EINVAL;
   1280 		break;
   1281 	}
   1282 
   1283 	splx(s);
   1284 	return (error);
   1285 }
   1286 
   1287 void
   1288 hme_shutdown(arg)
   1289 	void *arg;
   1290 {
   1291 
   1292 	hme_stop((struct hme_softc *)arg);
   1293 }
   1294 
   1295 /*
   1296  * Set up the logical address filter.
   1297  */
   1298 void
   1299 hme_setladrf(sc)
   1300 	struct hme_softc *sc;
   1301 {
   1302 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1303 	struct ether_multi *enm;
   1304 	struct ether_multistep step;
   1305 	struct ethercom *ec = &sc->sc_ethercom;
   1306 	bus_space_tag_t t = sc->sc_bustag;
   1307 	bus_space_handle_t mac = sc->sc_mac;
   1308 	u_char *cp;
   1309 	u_int32_t crc;
   1310 	u_int32_t hash[4];
   1311 	u_int32_t v;
   1312 	int len;
   1313 
   1314 	/* Clear hash table */
   1315 	hash[3] = hash[2] = hash[1] = hash[0] = 0;
   1316 
   1317 	/* Get current RX configuration */
   1318 	v = bus_space_read_4(t, mac, HME_MACI_RXCFG);
   1319 
   1320 	if ((ifp->if_flags & IFF_PROMISC) != 0) {
   1321 		/* Turn on promiscuous mode; turn off the hash filter */
   1322 		v |= HME_MAC_RXCFG_PMISC;
   1323 		v &= ~HME_MAC_RXCFG_HENABLE;
   1324 		ifp->if_flags |= IFF_ALLMULTI;
   1325 		goto chipit;
   1326 	}
   1327 
   1328 	/* Turn off promiscuous mode; turn on the hash filter */
   1329 	v &= ~HME_MAC_RXCFG_PMISC;
   1330 	v |= HME_MAC_RXCFG_HENABLE;
   1331 
   1332 	/*
   1333 	 * Set up multicast address filter by passing all multicast addresses
   1334 	 * through a crc generator, and then using the high order 6 bits as an
   1335 	 * index into the 64 bit logical address filter.  The high order bit
   1336 	 * selects the word, while the rest of the bits select the bit within
   1337 	 * the word.
   1338 	 */
   1339 
   1340 	ETHER_FIRST_MULTI(step, ec, enm);
   1341 	while (enm != NULL) {
   1342 		if (ether_cmp(enm->enm_addrlo, enm->enm_addrhi)) {
   1343 			/*
   1344 			 * We must listen to a range of multicast addresses.
   1345 			 * For now, just accept all multicasts, rather than
   1346 			 * trying to set only those filter bits needed to match
   1347 			 * the range.  (At this time, the only use of address
   1348 			 * ranges is for IP multicast routing, for which the
   1349 			 * range is big enough to require all bits set.)
   1350 			 */
   1351 			hash[3] = hash[2] = hash[1] = hash[0] = 0xffff;
   1352 			ifp->if_flags |= IFF_ALLMULTI;
   1353 			goto chipit;
   1354 		}
   1355 
   1356 		cp = enm->enm_addrlo;
   1357 		crc = 0xffffffff;
   1358 		for (len = sizeof(enm->enm_addrlo); --len >= 0;) {
   1359 			int octet = *cp++;
   1360 			int i;
   1361 
   1362 #define MC_POLY_LE	0xedb88320UL	/* mcast crc, little endian */
   1363 			for (i = 0; i < 8; i++) {
   1364 				if ((crc & 1) ^ (octet & 1)) {
   1365 					crc >>= 1;
   1366 					crc ^= MC_POLY_LE;
   1367 				} else {
   1368 					crc >>= 1;
   1369 				}
   1370 				octet >>= 1;
   1371 			}
   1372 		}
   1373 		/* Just want the 6 most significant bits. */
   1374 		crc >>= 26;
   1375 
   1376 		/* Set the corresponding bit in the filter. */
   1377 		hash[crc >> 4] |= 1 << (crc & 0xf);
   1378 
   1379 		ETHER_NEXT_MULTI(step, enm);
   1380 	}
   1381 
   1382 	ifp->if_flags &= ~IFF_ALLMULTI;
   1383 
   1384 chipit:
   1385 	/* Now load the hash table into the chip */
   1386 	bus_space_write_4(t, mac, HME_MACI_HASHTAB0, hash[0]);
   1387 	bus_space_write_4(t, mac, HME_MACI_HASHTAB1, hash[1]);
   1388 	bus_space_write_4(t, mac, HME_MACI_HASHTAB2, hash[2]);
   1389 	bus_space_write_4(t, mac, HME_MACI_HASHTAB3, hash[3]);
   1390 	bus_space_write_4(t, mac, HME_MACI_RXCFG, v);
   1391 }
   1392 
   1393 /*
   1394  * Routines for accessing the transmit and receive buffers.
   1395  * The various CPU and adapter configurations supported by this
   1396  * driver require three different access methods for buffers
   1397  * and descriptors:
   1398  *	(1) contig (contiguous data; no padding),
   1399  *	(2) gap2 (two bytes of data followed by two bytes of padding),
   1400  *	(3) gap16 (16 bytes of data followed by 16 bytes of padding).
   1401  */
   1402 
   1403 #if 0
   1404 /*
   1405  * contig: contiguous data with no padding.
   1406  *
   1407  * Buffers may have any alignment.
   1408  */
   1409 
   1410 void
   1411 hme_copytobuf_contig(sc, from, ri, len)
   1412 	struct hme_softc *sc;
   1413 	void *from;
   1414 	int ri, len;
   1415 {
   1416 	volatile caddr_t buf = sc->sc_rb.rb_txbuf + (ri * _HME_BUFSZ);
   1417 
   1418 	/*
   1419 	 * Just call bcopy() to do the work.
   1420 	 */
   1421 	bcopy(from, buf, len);
   1422 }
   1423 
   1424 void
   1425 hme_copyfrombuf_contig(sc, to, boff, len)
   1426 	struct hme_softc *sc;
   1427 	void *to;
   1428 	int boff, len;
   1429 {
   1430 	volatile caddr_t buf = sc->sc_rb.rb_rxbuf + (ri * _HME_BUFSZ);
   1431 
   1432 	/*
   1433 	 * Just call bcopy() to do the work.
   1434 	 */
   1435 	bcopy(buf, to, len);
   1436 }
   1437 #endif
   1438