Home | History | Annotate | Line # | Download | only in cadence
if_cemac.c revision 1.34
      1 /*	$NetBSD: if_cemac.c,v 1.34 2024/08/25 15:49:36 skrll Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2015  Genetec Corporation.  All rights reserved.
      5  * Written by Hashimoto Kenichi for Genetec Corporation.
      6  *
      7  * Based on arch/arm/at91/at91emac.c
      8  *
      9  * Copyright (c) 2007 Embedtronics Oy
     10  * All rights reserved.
     11  *
     12  * Copyright (c) 2004 Jesse Off
     13  * All rights reserved.
     14  *
     15  * Redistribution and use in source and binary forms, with or without
     16  * modification, are permitted provided that the following conditions
     17  * are met:
     18  * 1. Redistributions of source code must retain the above copyright
     19  *    notice, this list of conditions and the following disclaimer.
     20  * 2. Redistributions in binary form must reproduce the above copyright
     21  *    notice, this list of conditions and the following disclaimer in the
     22  *    documentation and/or other materials provided with the distribution.
     23  *
     24  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     25  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     26  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     27  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     28  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     29  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     30  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     31  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     32  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     33  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     34  * POSSIBILITY OF SUCH DAMAGE.
     35  */
     36 
     37 /*
     38  * Cadence EMAC/GEM ethernet controller IP driver
     39  * used by arm/at91, arm/zynq SoC
     40  */
     41 
     42 #include <sys/cdefs.h>
     43 __KERNEL_RCSID(0, "$NetBSD: if_cemac.c,v 1.34 2024/08/25 15:49:36 skrll Exp $");
     44 
     45 #include <sys/param.h>
     46 #include <sys/types.h>
     47 
     48 #include <sys/bus.h>
     49 #include <sys/device.h>
     50 #include <sys/kernel.h>
     51 #include <sys/proc.h>
     52 #include <sys/systm.h>
     53 #include <sys/time.h>
     54 
     55 #include <net/if.h>
     56 #include <net/if_dl.h>
     57 #include <net/if_types.h>
     58 #include <net/if_media.h>
     59 #include <net/if_ether.h>
     60 #include <net/bpf.h>
     61 
     62 #include <dev/mii/mii.h>
     63 #include <dev/mii/miivar.h>
     64 
     65 #ifdef INET
     66 #include <netinet/in.h>
     67 #include <netinet/in_systm.h>
     68 #include <netinet/in_var.h>
     69 #include <netinet/ip.h>
     70 #include <netinet/if_inarp.h>
     71 #endif
     72 
     73 #include <dev/cadence/cemacreg.h>
     74 #include <dev/cadence/if_cemacvar.h>
     75 
     76 #define DEFAULT_MDCDIV	32
     77 
     78 #define CEMAC_READ(x) \
     79 	bus_space_read_4(sc->sc_iot, sc->sc_ioh, (x))
     80 #define CEMAC_WRITE(x, y) \
     81 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, (x), (y))
     82 #define CEMAC_GEM_WRITE(x, y)						      \
     83 	do {								      \
     84 		if (ISSET(sc->cemac_flags, CEMAC_FLAG_GEM))		      \
     85 			bus_space_write_4(sc->sc_iot, sc->sc_ioh, (GEM_##x), (y)); \
     86 		else							      \
     87 			bus_space_write_4(sc->sc_iot, sc->sc_ioh, (ETH_##x), (y)); \
     88 	} while(0)
     89 
     90 static void	cemac_init(struct cemac_softc *);
     91 static int	cemac_gctx(struct cemac_softc *);
     92 static int	cemac_mediachange(struct ifnet *);
     93 static void	cemac_mediastatus(struct ifnet *, struct ifmediareq *);
     94 static int	cemac_mii_readreg(device_t, int, int, uint16_t *);
     95 static int	cemac_mii_writereg(device_t, int, int, uint16_t);
     96 static void	cemac_statchg(struct ifnet *);
     97 static void	cemac_tick(void *);
     98 static int	cemac_ifioctl(struct ifnet *, u_long, void *);
     99 static void	cemac_ifstart(struct ifnet *);
    100 static void	cemac_ifwatchdog(struct ifnet *);
    101 static int	cemac_ifinit(struct ifnet *);
    102 static void	cemac_ifstop(struct ifnet *, int);
    103 static void	cemac_setaddr(struct ifnet *);
    104 
    105 #ifdef	CEMAC_DEBUG
    106 int cemac_debug = CEMAC_DEBUG;
    107 #define	DPRINTFN(n, fmt)	if (cemac_debug >= (n)) printf fmt
    108 #else
    109 #define	DPRINTFN(n, fmt)
    110 #endif
    111 
    112 void
    113 cemac_attach_common(struct cemac_softc *sc)
    114 {
    115 	uint32_t u;
    116 
    117 	aprint_naive("\n");
    118 	if (ISSET(sc->cemac_flags, CEMAC_FLAG_GEM))
    119 		aprint_normal(": Cadence Gigabit Ethernet Controller\n");
    120 	else
    121 		aprint_normal(": Cadence Ethernet Controller\n");
    122 
    123 	/* configure emac: */
    124 	CEMAC_WRITE(ETH_CTL, 0);		// disable everything
    125 	CEMAC_WRITE(ETH_IDR, -1);		// disable interrupts
    126 	CEMAC_WRITE(ETH_RBQP, 0);		// clear receive
    127 	CEMAC_WRITE(ETH_TBQP, 0);		// clear transmit
    128 	if (ISSET(sc->cemac_flags, CEMAC_FLAG_GEM))
    129 		CEMAC_WRITE(ETH_CFG,
    130 		    GEM_CFG_CLK_64 | GEM_CFG_GEN | ETH_CFG_SPD | ETH_CFG_FD);
    131 	else
    132 		CEMAC_WRITE(ETH_CFG,
    133 		    ETH_CFG_CLK_32 | ETH_CFG_SPD | ETH_CFG_FD | ETH_CFG_BIG);
    134 	//CEMAC_WRITE(ETH_TCR, 0);		// send nothing
    135 	//(void)CEMAC_READ(ETH_ISR);
    136 	u = CEMAC_READ(ETH_TSR);
    137 	CEMAC_WRITE(ETH_TSR, (u & (ETH_TSR_UND | ETH_TSR_COMP | ETH_TSR_BNQ
    138 				  | ETH_TSR_IDLE | ETH_TSR_RLE
    139 				  | ETH_TSR_COL | ETH_TSR_OVR)));
    140 	u = CEMAC_READ(ETH_RSR);
    141 	CEMAC_WRITE(ETH_RSR, (u & (ETH_RSR_OVR | ETH_RSR_REC | ETH_RSR_BNA)));
    142 
    143 	/* Fetch the Ethernet address from property if set. */
    144 	prop_dictionary_t prop = device_properties(sc->sc_dev);
    145 	prop_data_t enaddr = prop_dictionary_get(prop, "mac-address");
    146 
    147 	if (enaddr != NULL) {
    148 		KASSERT(prop_object_type(enaddr) == PROP_TYPE_DATA);
    149 		KASSERT(prop_data_size(enaddr) == ETHER_ADDR_LEN);
    150 		memcpy(sc->sc_enaddr, prop_data_value(enaddr),
    151 		       ETHER_ADDR_LEN);
    152 	} else {
    153 		static const uint8_t hardcoded[ETHER_ADDR_LEN] = {
    154 			0x00, 0x0d, 0x10, 0x81, 0x0c, 0x94
    155 		};
    156 		memcpy(sc->sc_enaddr, hardcoded, ETHER_ADDR_LEN);
    157 	}
    158 
    159 	cemac_init(sc);
    160 }
    161 
    162 static int
    163 cemac_gctx(struct cemac_softc *sc)
    164 {
    165 	uint32_t tsr;
    166 
    167 	tsr = CEMAC_READ(ETH_TSR);
    168 	if (!ISSET(sc->cemac_flags, CEMAC_FLAG_GEM)) {
    169 		// no space left
    170 		if (!(tsr & ETH_TSR_BNQ))
    171 			return 0;
    172 	} else {
    173 		if (tsr & GEM_TSR_TXGO)
    174 			return 0;
    175 	}
    176 	CEMAC_WRITE(ETH_TSR, tsr);
    177 
    178 	// free sent frames
    179 	while (sc->txqc > (ISSET(sc->cemac_flags, CEMAC_FLAG_GEM) ? 0 :
    180 		(tsr & ETH_TSR_IDLE ? 0 : 1))) {
    181 		int bi = sc->txqi % TX_QLEN;
    182 
    183 		DPRINTFN(3,("%s: TDSC[%i].Addr 0x%08x\n",
    184 			__FUNCTION__, bi, sc->TDSC[bi].Addr));
    185 		DPRINTFN(3,("%s: TDSC[%i].Info 0x%08x\n",
    186 			__FUNCTION__, bi, sc->TDSC[bi].Info));
    187 
    188 		bus_dmamap_sync(sc->sc_dmat, sc->txq[bi].m_dmamap, 0,
    189 		    sc->txq[bi].m->m_pkthdr.len, BUS_DMASYNC_POSTWRITE);
    190 		bus_dmamap_unload(sc->sc_dmat, sc->txq[bi].m_dmamap);
    191 		m_freem(sc->txq[bi].m);
    192 		DPRINTFN(2,("%s: freed idx #%i mbuf %p (txqc=%i)\n",
    193 		    __FUNCTION__, bi, sc->txq[bi].m, sc->txqc));
    194 		sc->txq[bi].m = NULL;
    195 		sc->txqi = (bi + 1) % TX_QLEN;
    196 		sc->txqc--;
    197 	}
    198 
    199 	// mark we're free
    200 	if (sc->tx_busy) {
    201 		sc->tx_busy = false;
    202 		/* Disable transmit-buffer-free interrupt */
    203 		/*CEMAC_WRITE(ETH_IDR, ETH_ISR_TBRE);*/
    204 	}
    205 
    206 	return 1;
    207 }
    208 
    209 int
    210 cemac_intr(void *arg)
    211 {
    212 	struct cemac_softc *sc = (struct cemac_softc *)arg;
    213 	struct ifnet * ifp = &sc->sc_ethercom.ec_if;
    214 	uint32_t imr, isr, ctl;
    215 #ifdef	CEMAC_DEBUG
    216 	uint32_t rsr;
    217 #endif
    218 	int bi;
    219 
    220 	imr = ~CEMAC_READ(ETH_IMR);
    221 	if (!(imr & (ETH_ISR_RCOM | ETH_ISR_TBRE | ETH_ISR_TIDLE |
    222 	    ETH_ISR_RBNA | ETH_ISR_ROVR | ETH_ISR_TCOM))) {
    223 		// interrupt not enabled, can't be us
    224 		return 0;
    225 	}
    226 
    227 	isr = CEMAC_READ(ETH_ISR);
    228 	CEMAC_WRITE(ETH_ISR, isr);
    229 	isr &= imr;
    230 #ifdef	CEMAC_DEBUG
    231 	rsr = CEMAC_READ(ETH_RSR);		// get receive status register
    232 #endif
    233 	DPRINTFN(2, ("%s: isr=0x%08X rsr=0x%08X imr=0x%08X\n", __FUNCTION__, isr, rsr, imr));
    234 
    235 	net_stat_ref_t nsr = IF_STAT_GETREF(ifp);
    236 	if (isr & ETH_ISR_RBNA) {		// out of receive buffers
    237 		CEMAC_WRITE(ETH_RSR, ETH_RSR_BNA);	// clear interrupt
    238 		ctl = CEMAC_READ(ETH_CTL);		// get current control register value
    239 		CEMAC_WRITE(ETH_CTL, ctl & ~ETH_CTL_RE);	// disable receiver
    240 		CEMAC_WRITE(ETH_RSR, ETH_RSR_BNA);	// clear BNA bit
    241 		CEMAC_WRITE(ETH_CTL, ctl |  ETH_CTL_RE);	// re-enable receiver
    242 		if_statinc_ref(ifp, nsr, if_ierrors);
    243 		if_statinc_ref(ifp, nsr, if_ipackets);
    244 		DPRINTFN(1,("%s: out of receive buffers\n", __FUNCTION__));
    245 	}
    246 	if (isr & ETH_ISR_ROVR) {
    247 		CEMAC_WRITE(ETH_RSR, ETH_RSR_OVR);	// clear interrupt
    248 		if_statinc_ref(ifp, nsr, if_ierrors);
    249 		if_statinc_ref(ifp, nsr, if_ipackets);
    250 		DPRINTFN(1,("%s: receive overrun\n", __FUNCTION__));
    251 	}
    252 
    253 	if (isr & ETH_ISR_RCOM) {			// packet has been received!
    254 		uint32_t nfo;
    255 		DPRINTFN(2,("#2 RDSC[%i].INFO=0x%08X\n", sc->rxqi % RX_QLEN, sc->RDSC[sc->rxqi % RX_QLEN].Info));
    256 		while (sc->RDSC[(bi = sc->rxqi % RX_QLEN)].Addr & ETH_RDSC_F_USED) {
    257 			int fl, csum;
    258 			struct mbuf *m;
    259 
    260 			nfo = sc->RDSC[bi].Info;
    261 			fl = (nfo & ETH_RDSC_I_LEN) - 4;
    262 			DPRINTFN(2,("## nfo=0x%08X\n", nfo));
    263 
    264 			MGETHDR(m, M_DONTWAIT, MT_DATA);
    265 			if (m != NULL)
    266 				 MCLGET(m, M_DONTWAIT);
    267 			if (m != NULL && (m->m_flags & M_EXT)) {
    268 				bus_dmamap_sync(sc->sc_dmat, sc->rxq[bi].m_dmamap, 0,
    269 						MCLBYTES, BUS_DMASYNC_POSTREAD);
    270 				bus_dmamap_unload(sc->sc_dmat,
    271 					sc->rxq[bi].m_dmamap);
    272 				m_set_rcvif(sc->rxq[bi].m, ifp);
    273 				sc->rxq[bi].m->m_pkthdr.len =
    274 					sc->rxq[bi].m->m_len = fl;
    275 				switch (nfo & ETH_RDSC_I_CHKSUM) {
    276 				case ETH_RDSC_I_CHKSUM_IP:
    277 					csum = M_CSUM_IPv4;
    278 					break;
    279 				case ETH_RDSC_I_CHKSUM_UDP:
    280 					csum = M_CSUM_IPv4 | M_CSUM_UDPv4 |
    281 					    M_CSUM_UDPv6;
    282 					break;
    283 				case ETH_RDSC_I_CHKSUM_TCP:
    284 					csum = M_CSUM_IPv4 | M_CSUM_TCPv4 |
    285 					    M_CSUM_TCPv6;
    286 					break;
    287 				default:
    288 					csum = 0;
    289 					break;
    290 				}
    291 				sc->rxq[bi].m->m_pkthdr.csum_flags = csum;
    292 				DPRINTFN(2,("received %u bytes packet\n", fl));
    293 				if_percpuq_enqueue(ifp->if_percpuq,
    294 						   sc->rxq[bi].m);
    295 				if (mtod(m, intptr_t) & 3)
    296 					m_adj(m, mtod(m, intptr_t) & 3);
    297 				sc->rxq[bi].m = m;
    298 				bus_dmamap_load(sc->sc_dmat,
    299 					sc->rxq[bi].m_dmamap,
    300 					m->m_ext.ext_buf, MCLBYTES,
    301 					NULL, BUS_DMA_NOWAIT);
    302 				bus_dmamap_sync(sc->sc_dmat, sc->rxq[bi].m_dmamap, 0,
    303 						MCLBYTES, BUS_DMASYNC_PREREAD);
    304 				sc->RDSC[bi].Info = 0;
    305 				sc->RDSC[bi].Addr =
    306 					sc->rxq[bi].m_dmamap->dm_segs[0].ds_addr
    307 					| (bi == (RX_QLEN-1) ? ETH_RDSC_F_WRAP : 0);
    308 			} else {
    309 				/* Drop packets until we can get replacement
    310 				 * empty mbufs for the RXDQ.
    311 				 */
    312 				m_freem(m);
    313 				if_statinc_ref(ifp, nsr, if_ierrors);
    314 			}
    315 			sc->rxqi++;
    316 		}
    317 	}
    318 
    319 	IF_STAT_PUTREF(ifp);
    320 
    321 	if (cemac_gctx(sc) > 0)
    322 		if_schedule_deferred_start(ifp);
    323 #if 0 // reloop
    324 	irq = CEMAC_READ(IntStsC);
    325 	if ((irq & (IntSts_RxSQ | IntSts_ECI)) != 0)
    326 		goto begin;
    327 #endif
    328 
    329 	return 1;
    330 }
    331 
    332 
    333 static void
    334 cemac_init(struct cemac_softc *sc)
    335 {
    336 	bus_dma_segment_t segs;
    337 	int rsegs, err, i;
    338 	struct ifnet * ifp = &sc->sc_ethercom.ec_if;
    339 	struct mii_data * const mii = &sc->sc_mii;
    340 	uint32_t u;
    341 #if 0
    342 	int mdcdiv = DEFAULT_MDCDIV;
    343 #endif
    344 
    345 	callout_init(&sc->cemac_tick_ch, 0);
    346 
    347 	// ok...
    348 	CEMAC_WRITE(ETH_CTL, ETH_CTL_MPE);	// disable everything
    349 	CEMAC_WRITE(ETH_IDR, -1);		// disable interrupts
    350 	CEMAC_WRITE(ETH_RBQP, 0);		// clear receive
    351 	CEMAC_WRITE(ETH_TBQP, 0);		// clear transmit
    352 	if (ISSET(sc->cemac_flags, CEMAC_FLAG_GEM))
    353 		CEMAC_WRITE(ETH_CFG,
    354 		    GEM_CFG_CLK_64 | ETH_CFG_SPD | ETH_CFG_FD | ETH_CFG_BIG);
    355 	else
    356 		CEMAC_WRITE(ETH_CFG,
    357 		    ETH_CFG_CLK_32 | ETH_CFG_SPD | ETH_CFG_FD | ETH_CFG_BIG);
    358 	if (ISSET(sc->cemac_flags, CEMAC_FLAG_GEM)) {
    359 		CEMAC_WRITE(GEM_DMA_CFG,
    360 		    __SHIFTIN((MCLBYTES + 63) / 64, GEM_DMA_CFG_RX_BUF_SIZE) |
    361 		    __SHIFTIN(3, GEM_DMA_CFG_RX_PKTBUF_MEMSZ_SEL) |
    362 		    GEM_DMA_CFG_TX_PKTBUF_MEMSZ_SEL |
    363 		    __SHIFTIN(16, GEM_DMA_CFG_AHB_FIXED_BURST_LEN) |
    364 		    GEM_DMA_CFG_DISC_WHEN_NO_AHB);
    365 	}
    366 //	CEMAC_WRITE(ETH_TCR, 0);			// send nothing
    367 //	(void)CEMAC_READ(ETH_ISR);
    368 	u = CEMAC_READ(ETH_TSR);
    369 	CEMAC_WRITE(ETH_TSR, (u & (ETH_TSR_UND | ETH_TSR_COMP | ETH_TSR_BNQ
    370 		    | ETH_TSR_IDLE | ETH_TSR_RLE
    371 		    | ETH_TSR_COL | ETH_TSR_OVR)));
    372 	u = CEMAC_READ(ETH_RSR);
    373 	CEMAC_WRITE(ETH_RSR, (u & (ETH_RSR_OVR | ETH_RSR_REC | ETH_RSR_BNA)));
    374 
    375 #if 0
    376 	if (device_cfdata(sc->sc_dev)->cf_flags)
    377 		mdcdiv = device_cfdata(sc->sc_dev)->cf_flags;
    378 #endif
    379 	/* set ethernet address */
    380 	CEMAC_GEM_WRITE(SA1L, (sc->sc_enaddr[3] << 24)
    381 	    | (sc->sc_enaddr[2] << 16) | (sc->sc_enaddr[1] << 8)
    382 	    | (sc->sc_enaddr[0]));
    383 	CEMAC_GEM_WRITE(SA1H, (sc->sc_enaddr[5] << 8)
    384 	    | (sc->sc_enaddr[4]));
    385 	CEMAC_GEM_WRITE(SA2L, 0);
    386 	CEMAC_GEM_WRITE(SA2H, 0);
    387 	CEMAC_GEM_WRITE(SA3L, 0);
    388 	CEMAC_GEM_WRITE(SA3H, 0);
    389 	CEMAC_GEM_WRITE(SA4L, 0);
    390 	CEMAC_GEM_WRITE(SA4H, 0);
    391 
    392 	/* Allocate a page of memory for receive queue descriptors */
    393 	sc->rbqlen = (ETH_DSC_SIZE * (RX_QLEN + 1) * 2 + PAGE_SIZE - 1) / PAGE_SIZE;
    394 	sc->rbqlen *= PAGE_SIZE;
    395 	DPRINTFN(1,("%s: rbqlen=%i\n", __FUNCTION__, sc->rbqlen));
    396 
    397 	err = bus_dmamem_alloc(sc->sc_dmat, sc->rbqlen, 0,
    398 	    MAX(16384, PAGE_SIZE),	// see EMAC errata why forced to 16384 byte boundary
    399 	    &segs, 1, &rsegs, BUS_DMA_WAITOK);
    400 	if (err == 0) {
    401 		DPRINTFN(1,("%s: -> bus_dmamem_map\n", __FUNCTION__));
    402 		err = bus_dmamem_map(sc->sc_dmat, &segs, 1, sc->rbqlen,
    403 		    &sc->rbqpage, (BUS_DMA_WAITOK | BUS_DMA_COHERENT));
    404 	}
    405 	if (err == 0) {
    406 		DPRINTFN(1,("%s: -> bus_dmamap_create\n", __FUNCTION__));
    407 		err = bus_dmamap_create(sc->sc_dmat, sc->rbqlen, 1,
    408 		    sc->rbqlen, MAX(16384, PAGE_SIZE), BUS_DMA_WAITOK,
    409 		    &sc->rbqpage_dmamap);
    410 	}
    411 	if (err == 0) {
    412 		DPRINTFN(1,("%s: -> bus_dmamap_load\n", __FUNCTION__));
    413 		err = bus_dmamap_load(sc->sc_dmat, sc->rbqpage_dmamap,
    414 		    sc->rbqpage, sc->rbqlen, NULL, BUS_DMA_WAITOK);
    415 	}
    416 	if (err != 0)
    417 		panic("%s: Cannot get DMA memory", device_xname(sc->sc_dev));
    418 
    419 	sc->rbqpage_dsaddr = sc->rbqpage_dmamap->dm_segs[0].ds_addr;
    420 	memset(sc->rbqpage, 0, sc->rbqlen);
    421 
    422 	/* Allocate a page of memory for transmit queue descriptors */
    423 	sc->tbqlen = (ETH_DSC_SIZE * (TX_QLEN + 1) * 2 + PAGE_SIZE - 1) / PAGE_SIZE;
    424 	sc->tbqlen *= PAGE_SIZE;
    425 	DPRINTFN(1,("%s: tbqlen=%i\n", __FUNCTION__, sc->tbqlen));
    426 
    427 	err = bus_dmamem_alloc(sc->sc_dmat, sc->tbqlen, 0,
    428 	    MAX(16384, PAGE_SIZE),	// see EMAC errata why forced to 16384 byte boundary
    429 	    &segs, 1, &rsegs, BUS_DMA_WAITOK);
    430 	if (err == 0) {
    431 		DPRINTFN(1,("%s: -> bus_dmamem_map\n", __FUNCTION__));
    432 		err = bus_dmamem_map(sc->sc_dmat, &segs, 1, sc->tbqlen,
    433 		    &sc->tbqpage, (BUS_DMA_WAITOK | BUS_DMA_COHERENT));
    434 	}
    435 	if (err == 0) {
    436 		DPRINTFN(1,("%s: -> bus_dmamap_create\n", __FUNCTION__));
    437 		err = bus_dmamap_create(sc->sc_dmat, sc->tbqlen, 1,
    438 		    sc->tbqlen, MAX(16384, PAGE_SIZE), BUS_DMA_WAITOK,
    439 		    &sc->tbqpage_dmamap);
    440 	}
    441 	if (err == 0) {
    442 		DPRINTFN(1,("%s: -> bus_dmamap_load\n", __FUNCTION__));
    443 		err = bus_dmamap_load(sc->sc_dmat, sc->tbqpage_dmamap,
    444 		    sc->tbqpage, sc->tbqlen, NULL, BUS_DMA_WAITOK);
    445 	}
    446 	if (err != 0)
    447 		panic("%s: Cannot get DMA memory", device_xname(sc->sc_dev));
    448 
    449 	sc->tbqpage_dsaddr = sc->tbqpage_dmamap->dm_segs[0].ds_addr;
    450 	memset(sc->tbqpage, 0, sc->tbqlen);
    451 
    452 	/* Set up pointers to start of each queue in kernel addr space.
    453 	 * Each descriptor queue or status queue entry uses 2 words
    454 	 */
    455 	sc->RDSC = (void *)sc->rbqpage;
    456 	sc->TDSC = (void *)sc->tbqpage;
    457 
    458 	/* init TX queue */
    459 	for (i = 0; i < TX_QLEN; i++) {
    460 		sc->TDSC[i].Addr = 0;
    461 		sc->TDSC[i].Info = ETH_TDSC_I_USED |
    462 		    (i == (TX_QLEN - 1) ? ETH_TDSC_I_WRAP : 0);
    463 	}
    464 
    465 	/* Populate the RXQ with mbufs */
    466 	sc->rxqi = 0;
    467 	for (i = 0; i < RX_QLEN; i++) {
    468 		struct mbuf *m;
    469 
    470 		err = bus_dmamap_create(sc->sc_dmat, MCLBYTES, 1, MCLBYTES, PAGE_SIZE,
    471 		    BUS_DMA_WAITOK, &sc->rxq[i].m_dmamap);
    472 		if (err) {
    473 			panic("%s: dmamap_create failed: %i\n", __FUNCTION__, err);
    474 		}
    475 		MGETHDR(m, M_WAIT, MT_DATA);
    476 		MCLGET(m, M_WAIT);
    477 		sc->rxq[i].m = m;
    478 		if (mtod(m, intptr_t) & 3) {
    479 			m_adj(m, mtod(m, intptr_t) & 3);
    480 		}
    481 		err = bus_dmamap_load(sc->sc_dmat, sc->rxq[i].m_dmamap,
    482 		    m->m_ext.ext_buf, MCLBYTES, NULL,
    483 		    BUS_DMA_WAITOK);
    484 		if (err) {
    485 			panic("%s: dmamap_load failed: %i\n", __FUNCTION__, err);
    486 		}
    487 		sc->RDSC[i].Addr = sc->rxq[i].m_dmamap->dm_segs[0].ds_addr
    488 		    | (i == (RX_QLEN-1) ? ETH_RDSC_F_WRAP : 0);
    489 		sc->RDSC[i].Info = 0;
    490 		bus_dmamap_sync(sc->sc_dmat, sc->rxq[i].m_dmamap, 0,
    491 		    MCLBYTES, BUS_DMASYNC_PREREAD);
    492 	}
    493 
    494 	/* prepare transmit queue */
    495 	for (i = 0; i < TX_QLEN; i++) {
    496 		err = bus_dmamap_create(sc->sc_dmat, MCLBYTES, 1, MCLBYTES, 0,
    497 		    (BUS_DMA_WAITOK | BUS_DMA_ALLOCNOW),
    498 		    &sc->txq[i].m_dmamap);
    499 		if (err)
    500 			panic("ARGH #1");
    501 		sc->txq[i].m = NULL;
    502 	}
    503 
    504 	/* Program each queue's start addr, cur addr, and len registers
    505 	 * with the physical addresses.
    506 	 */
    507 	CEMAC_WRITE(ETH_RBQP, (uint32_t)sc->rbqpage_dsaddr);
    508 	CEMAC_WRITE(ETH_TBQP, (uint32_t)sc->tbqpage_dsaddr);
    509 
    510 	/* Divide HCLK by 32 for MDC clock */
    511 	sc->sc_ethercom.ec_mii = mii;
    512 	mii->mii_ifp = ifp;
    513 	mii->mii_readreg = cemac_mii_readreg;
    514 	mii->mii_writereg = cemac_mii_writereg;
    515 	mii->mii_statchg = cemac_statchg;
    516 	ifmedia_init(&mii->mii_media, IFM_IMASK, cemac_mediachange,
    517 	    cemac_mediastatus);
    518 	mii_attach(sc->sc_dev, mii, 0xffffffff, MII_PHY_ANY, 1, 0);
    519 	ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO);
    520 
    521 #if 0
    522 	// enable / disable interrupts
    523 	CEMAC_WRITE(ETH_IDR, -1);
    524 	CEMAC_WRITE(ETH_IER, ETH_ISR_RCOM | ETH_ISR_TBRE | ETH_ISR_TIDLE
    525 	    | ETH_ISR_RBNA | ETH_ISR_ROVR | ETH_ISR_TCOM);
    526 //	(void)CEMAC_READ(ETH_ISR); // why
    527 
    528 	// enable transmitter / receiver
    529 	CEMAC_WRITE(ETH_CTL, ETH_CTL_TE | ETH_CTL_RE | ETH_CTL_ISR
    530 	    | ETH_CTL_CSR | ETH_CTL_MPE);
    531 #endif
    532 	/*
    533 	 * We can support hardware checksumming.
    534 	 */
    535 	ifp->if_capabilities |=
    536 	    IFCAP_CSUM_IPv4_Tx | IFCAP_CSUM_IPv4_Rx |
    537 	    IFCAP_CSUM_TCPv4_Tx | IFCAP_CSUM_TCPv4_Rx |
    538 	    IFCAP_CSUM_UDPv4_Tx | IFCAP_CSUM_UDPv4_Rx |
    539 	    IFCAP_CSUM_TCPv6_Tx | IFCAP_CSUM_TCPv6_Rx |
    540 	    IFCAP_CSUM_UDPv6_Tx | IFCAP_CSUM_UDPv6_Rx;
    541 
    542 	/*
    543 	 * We can support 802.1Q VLAN-sized frames.
    544 	 */
    545 	sc->sc_ethercom.ec_capabilities |= ETHERCAP_VLAN_MTU;
    546 
    547 	strcpy(ifp->if_xname, device_xname(sc->sc_dev));
    548 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
    549 	ifp->if_ioctl = cemac_ifioctl;
    550 	ifp->if_start = cemac_ifstart;
    551 	ifp->if_watchdog = cemac_ifwatchdog;
    552 	ifp->if_init = cemac_ifinit;
    553 	ifp->if_stop = cemac_ifstop;
    554 	ifp->if_timer = 0;
    555 	ifp->if_softc = sc;
    556 	IFQ_SET_READY(&ifp->if_snd);
    557 	if_attach(ifp);
    558 	if_deferred_start_init(ifp, NULL);
    559 	ether_ifattach(ifp, (sc)->sc_enaddr);
    560 }
    561 
    562 static int
    563 cemac_mediachange(struct ifnet *ifp)
    564 {
    565 	if (ifp->if_flags & IFF_UP)
    566 		cemac_ifinit(ifp);
    567 	return 0;
    568 }
    569 
    570 static void
    571 cemac_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr)
    572 {
    573 	struct cemac_softc *sc = ifp->if_softc;
    574 
    575 	mii_pollstat(&sc->sc_mii);
    576 	ifmr->ifm_active = sc->sc_mii.mii_media_active;
    577 	ifmr->ifm_status = sc->sc_mii.mii_media_status;
    578 }
    579 
    580 
    581 static int
    582 cemac_mii_readreg(device_t self, int phy, int reg, uint16_t *val)
    583 {
    584 	struct cemac_softc *sc;
    585 
    586 	sc = device_private(self);
    587 
    588 	CEMAC_WRITE(ETH_MAN, (ETH_MAN_HIGH | ETH_MAN_RW_RD
    589 			     | ((phy << ETH_MAN_PHYA_SHIFT) & ETH_MAN_PHYA)
    590 			     | ((reg << ETH_MAN_REGA_SHIFT) & ETH_MAN_REGA)
    591 			     | ETH_MAN_CODE_IEEE802_3));
    592 	while (!(CEMAC_READ(ETH_SR) & ETH_SR_IDLE))
    593 		;
    594 
    595 	*val = CEMAC_READ(ETH_MAN) & ETH_MAN_DATA;
    596 	return 0;
    597 }
    598 
    599 static int
    600 cemac_mii_writereg(device_t self, int phy, int reg, uint16_t val)
    601 {
    602 	struct cemac_softc *sc;
    603 
    604 	sc = device_private(self);
    605 
    606 	CEMAC_WRITE(ETH_MAN, (ETH_MAN_HIGH | ETH_MAN_RW_WR
    607 			     | ((phy << ETH_MAN_PHYA_SHIFT) & ETH_MAN_PHYA)
    608 			     | ((reg << ETH_MAN_REGA_SHIFT) & ETH_MAN_REGA)
    609 			     | ETH_MAN_CODE_IEEE802_3
    610 			     | (val & ETH_MAN_DATA)));
    611 	while (!(CEMAC_READ(ETH_SR) & ETH_SR_IDLE))
    612 		;
    613 
    614 	return 0;
    615 }
    616 
    617 
    618 static void
    619 cemac_statchg(struct ifnet *ifp)
    620 {
    621 	struct cemac_softc *sc = ifp->if_softc;
    622 	struct mii_data *mii = &sc->sc_mii;
    623 	uint32_t reg;
    624 
    625 	/*
    626 	 * We must keep the MAC and the PHY in sync as
    627 	 * to the status of full-duplex!
    628 	 */
    629 	reg = CEMAC_READ(ETH_CFG);
    630 	reg &= ~ETH_CFG_FD;
    631 	if (sc->sc_mii.mii_media_active & IFM_FDX)
    632 		reg |= ETH_CFG_FD;
    633 
    634 	reg &= ~ETH_CFG_SPD;
    635 	if (ISSET(sc->cemac_flags, CEMAC_FLAG_GEM))
    636 		reg &= ~GEM_CFG_GEN;
    637 	switch (IFM_SUBTYPE(mii->mii_media_active)) {
    638 	case IFM_10_T:
    639 		break;
    640 	case IFM_100_TX:
    641 		reg |= ETH_CFG_SPD;
    642 		break;
    643 	case IFM_1000_T:
    644 		reg |= ETH_CFG_SPD | GEM_CFG_GEN;
    645 		break;
    646 	default:
    647 		break;
    648 	}
    649 	CEMAC_WRITE(ETH_CFG, reg);
    650 }
    651 
    652 static void
    653 cemac_tick(void *arg)
    654 {
    655 	struct cemac_softc *sc = (struct cemac_softc *)arg;
    656 	struct ifnet * ifp = &sc->sc_ethercom.ec_if;
    657 	int s;
    658 
    659 	if (ISSET(sc->cemac_flags, CEMAC_FLAG_GEM))
    660 		if_statadd(ifp, if_collisions,
    661 		    CEMAC_READ(GEM_SCOL) + CEMAC_READ(GEM_MCOL));
    662 	else
    663 		if_statadd(ifp, if_collisions,
    664 		    CEMAC_READ(ETH_SCOL) + CEMAC_READ(ETH_MCOL));
    665 
    666 	/* These misses are ok, they will happen if the RAM/CPU can't keep up */
    667 	if (!ISSET(sc->cemac_flags, CEMAC_FLAG_GEM)) {
    668 		uint32_t misses = CEMAC_READ(ETH_DRFC);
    669 		if (misses > 0)
    670 			aprint_normal_ifnet(ifp, "%d rx misses\n", misses);
    671 	}
    672 
    673 	s = splnet();
    674 	if (cemac_gctx(sc) > 0 && IFQ_IS_EMPTY(&ifp->if_snd) == 0)
    675 		cemac_ifstart(ifp);
    676 	splx(s);
    677 
    678 	mii_tick(&sc->sc_mii);
    679 	callout_reset(&sc->cemac_tick_ch, hz, cemac_tick, sc);
    680 }
    681 
    682 
    683 static int
    684 cemac_ifioctl(struct ifnet *ifp, u_long cmd, void *data)
    685 {
    686 	int s, error;
    687 
    688 	s = splnet();
    689 	switch (cmd) {
    690 	default:
    691 		error = ether_ioctl(ifp, cmd, data);
    692 		if (error != ENETRESET)
    693 			break;
    694 		error = 0;
    695 
    696 		if (cmd == SIOCSIFCAP) {
    697 			error = if_init(ifp);
    698 		} else if (cmd != SIOCADDMULTI && cmd != SIOCDELMULTI)
    699 			;
    700 		else if (ifp->if_flags & IFF_RUNNING) {
    701 			cemac_setaddr(ifp);
    702 		}
    703 	}
    704 	splx(s);
    705 	return error;
    706 }
    707 
    708 static void
    709 cemac_ifstart(struct ifnet *ifp)
    710 {
    711 	struct cemac_softc *sc = (struct cemac_softc *)ifp->if_softc;
    712 	struct mbuf *m;
    713 	bus_dma_segment_t *segs;
    714 	int s, bi, err, nsegs;
    715 
    716 	s = splnet();
    717 start:
    718 	if (cemac_gctx(sc) == 0) {
    719 		/* Enable transmit-buffer-free interrupt */
    720 		CEMAC_WRITE(ETH_IER, ETH_ISR_TBRE);
    721 		sc->tx_busy = true;
    722 		ifp->if_timer = 10;
    723 		splx(s);
    724 		return;
    725 	}
    726 
    727 	ifp->if_timer = 0;
    728 
    729 	IFQ_POLL(&ifp->if_snd, m);
    730 	if (m == NULL) {
    731 		splx(s);
    732 		return;
    733 	}
    734 
    735 	bi = (sc->txqi + sc->txqc) % TX_QLEN;
    736 	if ((err = bus_dmamap_load_mbuf(sc->sc_dmat, sc->txq[bi].m_dmamap, m,
    737 		BUS_DMA_NOWAIT)) ||
    738 		sc->txq[bi].m_dmamap->dm_segs[0].ds_addr & 0x3 ||
    739 		sc->txq[bi].m_dmamap->dm_nsegs > 1) {
    740 		/* Copy entire mbuf chain to new single */
    741 		struct mbuf *mn;
    742 
    743 		if (err == 0)
    744 			bus_dmamap_unload(sc->sc_dmat, sc->txq[bi].m_dmamap);
    745 
    746 		MGETHDR(mn, M_DONTWAIT, MT_DATA);
    747 		if (mn == NULL)
    748 			 goto stop;
    749 		if (m->m_pkthdr.len > MHLEN) {
    750 			MCLGET(mn, M_DONTWAIT);
    751 			if ((mn->m_flags & M_EXT) == 0) {
    752 				m_freem(mn);
    753 				goto stop;
    754 			}
    755 		}
    756 		m_copydata(m, 0, m->m_pkthdr.len, mtod(mn, void *));
    757 		mn->m_pkthdr.len = mn->m_len = m->m_pkthdr.len;
    758 		IFQ_DEQUEUE(&ifp->if_snd, m);
    759 		m_freem(m);
    760 		m = mn;
    761 		bus_dmamap_load_mbuf(sc->sc_dmat, sc->txq[bi].m_dmamap, m,
    762 		    BUS_DMA_NOWAIT);
    763 	} else {
    764 		IFQ_DEQUEUE(&ifp->if_snd, m);
    765 	}
    766 
    767 	bpf_mtap(ifp, m, BPF_D_OUT);
    768 
    769 	nsegs = sc->txq[bi].m_dmamap->dm_nsegs;
    770 	segs = sc->txq[bi].m_dmamap->dm_segs;
    771 	if (nsegs > 1)
    772 		panic("#### ARGH #2");
    773 
    774 	sc->txq[bi].m = m;
    775 	sc->txqc++;
    776 
    777 	DPRINTFN(2,("%s: start sending idx #%i mbuf %p (txqc=%i, phys %p), len=%u\n",
    778 		__FUNCTION__, bi, sc->txq[bi].m, sc->txqc, (void *)segs->ds_addr,
    779 		(unsigned)m->m_pkthdr.len));
    780 #ifdef	DIAGNOSTIC
    781 	if (sc->txqc > TX_QLEN)
    782 		panic("%s: txqc %i > %i", __FUNCTION__, sc->txqc, TX_QLEN);
    783 #endif
    784 
    785 	bus_dmamap_sync(sc->sc_dmat, sc->txq[bi].m_dmamap, 0,
    786 		sc->txq[bi].m_dmamap->dm_mapsize,
    787 		BUS_DMASYNC_PREWRITE);
    788 
    789 	if (ISSET(sc->cemac_flags, CEMAC_FLAG_GEM)) {
    790 		sc->TDSC[bi].Addr = segs->ds_addr;
    791 		sc->TDSC[bi].Info = __SHIFTIN(m->m_pkthdr.len, ETH_TDSC_I_LEN) |
    792 		    ETH_TDSC_I_LAST_BUF | (bi == (TX_QLEN - 1) ? ETH_TDSC_I_WRAP : 0);
    793 
    794 		DPRINTFN(3,("%s: TDSC[%i].Addr 0x%08x\n",
    795 			__FUNCTION__, bi, sc->TDSC[bi].Addr));
    796 		DPRINTFN(3,("%s: TDSC[%i].Info 0x%08x\n",
    797 			__FUNCTION__, bi, sc->TDSC[bi].Info));
    798 
    799 		uint32_t ctl = CEMAC_READ(ETH_CTL) | GEM_CTL_STARTTX;
    800 		CEMAC_WRITE(ETH_CTL, ctl);
    801 		DPRINTFN(3,("%s: ETH_CTL 0x%08x\n", __FUNCTION__, CEMAC_READ(ETH_CTL)));
    802 	} else {
    803 		CEMAC_WRITE(ETH_TAR, segs->ds_addr);
    804 		CEMAC_WRITE(ETH_TCR, m->m_pkthdr.len);
    805 	}
    806 	if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
    807 		goto start;
    808 stop:
    809 
    810 	splx(s);
    811 	return;
    812 }
    813 
    814 static void
    815 cemac_ifwatchdog(struct ifnet *ifp)
    816 {
    817 	struct cemac_softc *sc = (struct cemac_softc *)ifp->if_softc;
    818 
    819 	if ((ifp->if_flags & IFF_RUNNING) == 0)
    820 		return;
    821 	aprint_error_ifnet(ifp, "device timeout, CTL = 0x%08x, CFG = 0x%08x\n",
    822 		CEMAC_READ(ETH_CTL), CEMAC_READ(ETH_CFG));
    823 }
    824 
    825 static int
    826 cemac_ifinit(struct ifnet *ifp)
    827 {
    828 	struct cemac_softc *sc = ifp->if_softc;
    829 	uint32_t dma, cfg;
    830 	int s = splnet();
    831 
    832 	callout_stop(&sc->cemac_tick_ch);
    833 
    834 	if (ISSET(sc->cemac_flags, CEMAC_FLAG_GEM)) {
    835 
    836 		if (ifp->if_capenable &
    837 		    (IFCAP_CSUM_IPv4_Tx |
    838 			IFCAP_CSUM_TCPv4_Tx | IFCAP_CSUM_UDPv4_Tx |
    839 			IFCAP_CSUM_TCPv6_Tx | IFCAP_CSUM_UDPv6_Tx)) {
    840 			dma = CEMAC_READ(GEM_DMA_CFG);
    841 			dma |= GEM_DMA_CFG_CHKSUM_GEN_OFFLOAD_EN;
    842 			CEMAC_WRITE(GEM_DMA_CFG, dma);
    843 		}
    844 		if (ifp->if_capenable &
    845 		    (IFCAP_CSUM_IPv4_Rx |
    846 			IFCAP_CSUM_TCPv4_Rx | IFCAP_CSUM_UDPv4_Rx |
    847 			IFCAP_CSUM_TCPv6_Rx | IFCAP_CSUM_UDPv6_Rx)) {
    848 			cfg = CEMAC_READ(ETH_CFG);
    849 			cfg |= GEM_CFG_RX_CHKSUM_OFFLD_EN;
    850 			CEMAC_WRITE(ETH_CFG, cfg);
    851 		}
    852 	}
    853 
    854 	// enable interrupts
    855 	CEMAC_WRITE(ETH_IDR, -1);
    856 	CEMAC_WRITE(ETH_IER, ETH_ISR_RCOM | ETH_ISR_TBRE | ETH_ISR_TIDLE
    857 	    | ETH_ISR_RBNA | ETH_ISR_ROVR | ETH_ISR_TCOM);
    858 
    859 	// enable transmitter / receiver
    860 	CEMAC_WRITE(ETH_CTL, ETH_CTL_TE | ETH_CTL_RE | ETH_CTL_ISR
    861 	    | ETH_CTL_CSR | ETH_CTL_MPE);
    862 
    863 	mii_mediachg(&sc->sc_mii);
    864 	callout_reset(&sc->cemac_tick_ch, hz, cemac_tick, sc);
    865 	ifp->if_flags |= IFF_RUNNING;
    866 	splx(s);
    867 	return 0;
    868 }
    869 
    870 static void
    871 cemac_ifstop(struct ifnet *ifp, int disable)
    872 {
    873 //	uint32_t u;
    874 	struct cemac_softc *sc = ifp->if_softc;
    875 
    876 #if 0
    877 	CEMAC_WRITE(ETH_CTL, ETH_CTL_MPE);	// disable everything
    878 	CEMAC_WRITE(ETH_IDR, -1);		// disable interrupts
    879 //	CEMAC_WRITE(ETH_RBQP, 0);		// clear receive
    880 	if (ISSET(sc->cemac_flags, CEMAC_FLAG_GEM))
    881 		CEMAC_WRITE(ETH_CFG,
    882 		    GEM_CFG_CLK_64 | ETH_CFG_SPD | ETH_CFG_FD | ETH_CFG_BIG);
    883 	else
    884 		CEMAC_WRITE(ETH_CFG,
    885 		    ETH_CFG_CLK_32 | ETH_CFG_SPD | ETH_CFG_FD | ETH_CFG_BIG);
    886 //	CEMAC_WRITE(ETH_TCR, 0);			// send nothing
    887 //	(void)CEMAC_READ(ETH_ISR);
    888 	u = CEMAC_READ(ETH_TSR);
    889 	CEMAC_WRITE(ETH_TSR, (u & (ETH_TSR_UND | ETH_TSR_COMP | ETH_TSR_BNQ
    890 				  | ETH_TSR_IDLE | ETH_TSR_RLE
    891 				  | ETH_TSR_COL | ETH_TSR_OVR)));
    892 	u = CEMAC_READ(ETH_RSR);
    893 	CEMAC_WRITE(ETH_RSR, (u & (ETH_RSR_OVR | ETH_RSR_REC | ETH_RSR_BNA)));
    894 #endif
    895 	callout_stop(&sc->cemac_tick_ch);
    896 
    897 	/* Down the MII. */
    898 	mii_down(&sc->sc_mii);
    899 
    900 	ifp->if_flags &= ~IFF_RUNNING;
    901 	ifp->if_timer = 0;
    902 	sc->tx_busy = false;
    903 	sc->sc_mii.mii_media_status &= ~IFM_ACTIVE;
    904 }
    905 
    906 static void
    907 cemac_setaddr(struct ifnet *ifp)
    908 {
    909 	struct cemac_softc *sc = ifp->if_softc;
    910 	struct ethercom *ec = &sc->sc_ethercom;
    911 	struct ether_multi *enm;
    912 	struct ether_multistep step;
    913 	uint8_t ias[3][ETHER_ADDR_LEN];
    914 	uint32_t h, nma = 0, hashes[2] = { 0, 0 };
    915 	uint32_t ctl = CEMAC_READ(ETH_CTL);
    916 	uint32_t cfg = CEMAC_READ(ETH_CFG);
    917 
    918 	/* disable receiver temporarily */
    919 	CEMAC_WRITE(ETH_CTL, ctl & ~ETH_CTL_RE);
    920 
    921 	cfg &= ~(ETH_CFG_MTI | ETH_CFG_UNI | ETH_CFG_CAF | ETH_CFG_UNI);
    922 
    923 	if (ifp->if_flags & IFF_PROMISC) {
    924 		cfg |=	ETH_CFG_CAF;
    925 	} else {
    926 		cfg &= ~ETH_CFG_CAF;
    927 	}
    928 
    929 	// ETH_CFG_BIG?
    930 
    931 	ifp->if_flags &= ~IFF_ALLMULTI;
    932 
    933 	ETHER_LOCK(ec);
    934 	ETHER_FIRST_MULTI(step, ec, enm);
    935 	while (enm != NULL) {
    936 		if (memcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
    937 			/*
    938 			 * We must listen to a range of multicast addresses.
    939 			 * For now, just accept all multicasts, rather than
    940 			 * trying to set only those filter bits needed to match
    941 			 * the range.  (At this time, the only use of address
    942 			 * ranges is for IP multicast routing, for which the
    943 			 * range is big enough to require all bits set.)
    944 			 */
    945 			cfg |= ETH_CFG_MTI;
    946 			hashes[0] = 0xffffffffUL;
    947 			hashes[1] = 0xffffffffUL;
    948 			ifp->if_flags |= IFF_ALLMULTI;
    949 			nma = 0;
    950 			break;
    951 		}
    952 
    953 		if (nma < 3) {
    954 			/* We can program 3 perfect address filters for mcast */
    955 			memcpy(ias[nma], enm->enm_addrlo, ETHER_ADDR_LEN);
    956 		} else {
    957 			/*
    958 			 * XXX: Datasheet is not very clear here, I'm not sure
    959 			 * if I'm doing this right.  --joff
    960 			 */
    961 			h = ether_crc32_le(enm->enm_addrlo, ETHER_ADDR_LEN);
    962 
    963 			/* Just want the 6 most-significant bits. */
    964 			h = h >> 26;
    965 #if 0
    966 			hashes[h / 32] |=  (1 << (h % 32));
    967 #else
    968 			hashes[0] = 0xffffffffUL;
    969 			hashes[1] = 0xffffffffUL;
    970 #endif
    971 			cfg |= ETH_CFG_MTI;
    972 		}
    973 		ETHER_NEXT_MULTI(step, enm);
    974 		nma++;
    975 	}
    976 	ETHER_UNLOCK(ec);
    977 
    978 	// program...
    979 	DPRINTFN(1,("%s: en0 %02x:%02x:%02x:%02x:%02x:%02x\n", __FUNCTION__,
    980 		sc->sc_enaddr[0], sc->sc_enaddr[1], sc->sc_enaddr[2],
    981 		sc->sc_enaddr[3], sc->sc_enaddr[4], sc->sc_enaddr[5]));
    982 	CEMAC_GEM_WRITE(SA1L, (sc->sc_enaddr[3] << 24)
    983 	    | (sc->sc_enaddr[2] << 16) | (sc->sc_enaddr[1] << 8)
    984 	    | (sc->sc_enaddr[0]));
    985 	CEMAC_GEM_WRITE(SA1H, (sc->sc_enaddr[5] << 8)
    986 	    | (sc->sc_enaddr[4]));
    987 	if (nma > 0) {
    988 		DPRINTFN(1,("%s: en1 %02x:%02x:%02x:%02x:%02x:%02x\n", __FUNCTION__,
    989 			ias[0][0], ias[0][1], ias[0][2],
    990 			ias[0][3], ias[0][4], ias[0][5]));
    991 		CEMAC_WRITE(ETH_SA2L, (ias[0][3] << 24)
    992 		    | (ias[0][2] << 16) | (ias[0][1] << 8)
    993 		    | (ias[0][0]));
    994 		CEMAC_WRITE(ETH_SA2H, (ias[0][4] << 8)
    995 		    | (ias[0][5]));
    996 	}
    997 	if (nma > 1) {
    998 		DPRINTFN(1,("%s: en2 %02x:%02x:%02x:%02x:%02x:%02x\n", __FUNCTION__,
    999 			ias[1][0], ias[1][1], ias[1][2],
   1000 			ias[1][3], ias[1][4], ias[1][5]));
   1001 		CEMAC_WRITE(ETH_SA3L, (ias[1][3] << 24)
   1002 		    | (ias[1][2] << 16) | (ias[1][1] << 8)
   1003 		    | (ias[1][0]));
   1004 		CEMAC_WRITE(ETH_SA3H, (ias[1][4] << 8)
   1005 		    | (ias[1][5]));
   1006 	}
   1007 	if (nma > 2) {
   1008 		DPRINTFN(1,("%s: en3 %02x:%02x:%02x:%02x:%02x:%02x\n", __FUNCTION__,
   1009 			ias[2][0], ias[2][1], ias[2][2],
   1010 			ias[2][3], ias[2][4], ias[2][5]));
   1011 		CEMAC_WRITE(ETH_SA4L, (ias[2][3] << 24)
   1012 		    | (ias[2][2] << 16) | (ias[2][1] << 8)
   1013 		    | (ias[2][0]));
   1014 		CEMAC_WRITE(ETH_SA4H, (ias[2][4] << 8)
   1015 		    | (ias[2][5]));
   1016 	}
   1017 	CEMAC_GEM_WRITE(HSH, hashes[0]);
   1018 	CEMAC_GEM_WRITE(HSL, hashes[1]);
   1019 	CEMAC_WRITE(ETH_CFG, cfg);
   1020 	CEMAC_WRITE(ETH_CTL, ctl | ETH_CTL_RE);
   1021 }
   1022