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