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