Home | History | Annotate | Line # | Download | only in dev
if_gm.c revision 1.10
      1 /*	$NetBSD: if_gm.c,v 1.10 2000/12/28 22:59:09 sommerfeld Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2000 Tsubai Masanari.  All rights reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  * 3. The name of the author may not be used to endorse or promote products
     15  *    derived from this software without specific prior written permission.
     16  *
     17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     26  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     27  */
     28 
     29 #include "opt_inet.h"
     30 #include "opt_ns.h"
     31 #include "bpfilter.h"
     32 
     33 #include <sys/param.h>
     34 #include <sys/device.h>
     35 #include <sys/ioctl.h>
     36 #include <sys/kernel.h>
     37 #include <sys/mbuf.h>
     38 #include <sys/socket.h>
     39 #include <sys/systm.h>
     40 #include <sys/callout.h>
     41 
     42 #include <uvm/uvm_extern.h>
     43 
     44 #include <net/if.h>
     45 #include <net/if_ether.h>
     46 #include <net/if_media.h>
     47 
     48 #if NBPFILTER > 0
     49 #include <net/bpf.h>
     50 #endif
     51 
     52 #ifdef INET
     53 #include <netinet/in.h>
     54 #include <netinet/if_inarp.h>
     55 #endif
     56 
     57 #include <dev/mii/mii.h>
     58 #include <dev/mii/miivar.h>
     59 
     60 #include <dev/pci/pcivar.h>
     61 #include <dev/pci/pcireg.h>
     62 #include <dev/pci/pcidevs.h>
     63 
     64 #include <dev/ofw/openfirm.h>
     65 #include <macppc/dev/if_gmreg.h>
     66 #include <machine/pio.h>
     67 
     68 #define NTXBUF 4
     69 #define NRXBUF 32
     70 
     71 struct gmac_softc {
     72 	struct device sc_dev;
     73 	struct ethercom sc_ethercom;
     74 	vaddr_t sc_reg;
     75 	struct gmac_dma *sc_txlist;
     76 	struct gmac_dma *sc_rxlist;
     77 	int sc_txnext;
     78 	int sc_rxlast;
     79 	caddr_t sc_txbuf[NTXBUF];
     80 	caddr_t sc_rxbuf[NRXBUF];
     81 	struct mii_data sc_mii;
     82 	struct callout sc_tick_ch;
     83 	char sc_laddr[6];
     84 };
     85 
     86 #define sc_if sc_ethercom.ec_if
     87 
     88 int gmac_match __P((struct device *, struct cfdata *, void *));
     89 void gmac_attach __P((struct device *, struct device *, void *));
     90 
     91 static __inline u_int gmac_read_reg __P((struct gmac_softc *, int));
     92 static __inline void gmac_write_reg __P((struct gmac_softc *, int, u_int));
     93 
     94 static __inline void gmac_start_txdma __P((struct gmac_softc *));
     95 static __inline void gmac_start_rxdma __P((struct gmac_softc *));
     96 static __inline void gmac_stop_txdma __P((struct gmac_softc *));
     97 static __inline void gmac_stop_rxdma __P((struct gmac_softc *));
     98 
     99 int gmac_intr __P((void *));
    100 void gmac_tint __P((struct gmac_softc *));
    101 void gmac_rint __P((struct gmac_softc *));
    102 struct mbuf * gmac_get __P((struct gmac_softc *, caddr_t, int));
    103 void gmac_start __P((struct ifnet *));
    104 int gmac_put __P((struct gmac_softc *, caddr_t, struct mbuf *));
    105 
    106 void gmac_stop __P((struct gmac_softc *));
    107 void gmac_reset __P((struct gmac_softc *));
    108 void gmac_init __P((struct gmac_softc *));
    109 void gmac_init_mac __P((struct gmac_softc *));
    110 void gmac_setladrf __P((struct gmac_softc *));
    111 
    112 int gmac_ioctl __P((struct ifnet *, u_long, caddr_t));
    113 void gmac_watchdog __P((struct ifnet *));
    114 
    115 int gmac_mediachange __P((struct ifnet *));
    116 void gmac_mediastatus __P((struct ifnet *, struct ifmediareq *));
    117 int gmac_mii_readreg __P((struct device *, int, int));
    118 void gmac_mii_writereg __P((struct device *, int, int, int));
    119 void gmac_mii_statchg __P((struct device *));
    120 void gmac_mii_tick __P((void *));
    121 
    122 struct cfattach gm_ca = {
    123 	sizeof(struct gmac_softc), gmac_match, gmac_attach
    124 };
    125 
    126 int
    127 gmac_match(parent, match, aux)
    128 	struct device *parent;
    129 	struct cfdata *match;
    130 	void *aux;
    131 {
    132 	struct pci_attach_args *pa = aux;
    133 
    134 	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_APPLE &&
    135 	    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_APPLE_GMAC)
    136 		return 1;
    137 
    138 	return 0;
    139 }
    140 
    141 void
    142 gmac_attach(parent, self, aux)
    143 	struct device *parent, *self;
    144 	void *aux;
    145 {
    146 	struct gmac_softc *sc = (void *)self;
    147 	struct pci_attach_args *pa = aux;
    148 	struct ifnet *ifp = &sc->sc_if;
    149 	struct mii_data *mii = &sc->sc_mii;
    150 	pci_intr_handle_t ih;
    151 	const char *intrstr = NULL;
    152 	int node, i;
    153 	char *p;
    154 	struct gmac_dma *dp;
    155 	u_int32_t reg[10];
    156 	u_char laddr[6];
    157 
    158 	node = pcidev_to_ofdev(pa->pa_pc, pa->pa_tag);
    159 	if (node == 0) {
    160 		printf(": cannot find gmac node\n");
    161 		return;
    162 	}
    163 
    164 	OF_getprop(node, "local-mac-address", laddr, sizeof laddr);
    165 	OF_getprop(node, "assigned-addresses", reg, sizeof reg);
    166 
    167 	bcopy(laddr, sc->sc_laddr, sizeof laddr);
    168 	sc->sc_reg = reg[2];
    169 
    170 	if (pci_intr_map(pa, &ih)) {
    171 		printf(": unable to map interrupt\n");
    172 		return;
    173 	}
    174 	intrstr = pci_intr_string(pa->pa_pc, ih);
    175 
    176 	if (pci_intr_establish(pa->pa_pc, ih, IPL_NET, gmac_intr, sc) == NULL) {
    177 		printf(": unable to establish interrupt");
    178 		if (intrstr)
    179 			printf(" at %s", intrstr);
    180 		printf("\n");
    181 		return;
    182 	}
    183 
    184 	/* Setup packet buffers and dma descriptors. */
    185 	p = malloc((NRXBUF + NTXBUF) * 2048 + 3 * 0x800, M_DEVBUF, M_NOWAIT);
    186 	if (p == NULL) {
    187 		printf(": cannot malloc buffers\n");
    188 		return;
    189 	}
    190 	p = (void *)roundup((vaddr_t)p, 0x800);
    191 	bzero(p, 2048 * (NRXBUF + NTXBUF) + 2 * 0x800);
    192 
    193 	sc->sc_rxlist = (void *)p;
    194 	p += 0x800;
    195 	sc->sc_txlist = (void *)p;
    196 	p += 0x800;
    197 
    198 	dp = sc->sc_rxlist;
    199 	for (i = 0; i < NRXBUF; i++) {
    200 		sc->sc_rxbuf[i] = p;
    201 		dp->address = htole32(vtophys((vaddr_t)p));
    202 		dp->cmd = htole32(GMAC_OWN);
    203 		dp++;
    204 		p += 2048;
    205 	}
    206 
    207 	dp = sc->sc_txlist;
    208 	for (i = 0; i < NTXBUF; i++) {
    209 		sc->sc_txbuf[i] = p;
    210 		dp->address = htole32(vtophys((vaddr_t)p));
    211 		dp++;
    212 		p += 2048;
    213 	}
    214 
    215 	printf(": Ethernet address %s\n", ether_sprintf(laddr));
    216 	printf("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstr);
    217 
    218 	callout_init(&sc->sc_tick_ch);
    219 
    220 	gmac_reset(sc);
    221 	gmac_init_mac(sc);
    222 
    223 	bcopy(sc->sc_dev.dv_xname, ifp->if_xname, IFNAMSIZ);
    224 	ifp->if_softc = sc;
    225 	ifp->if_ioctl = gmac_ioctl;
    226 	ifp->if_start = gmac_start;
    227 	ifp->if_watchdog = gmac_watchdog;
    228 	ifp->if_flags =
    229 		IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS | IFF_MULTICAST;
    230 
    231 	mii->mii_ifp = ifp;
    232 	mii->mii_readreg = gmac_mii_readreg;
    233 	mii->mii_writereg = gmac_mii_writereg;
    234 	mii->mii_statchg = gmac_mii_statchg;
    235 
    236 	ifmedia_init(&mii->mii_media, 0, gmac_mediachange, gmac_mediastatus);
    237 	mii_attach(self, mii, 0xffffffff, MII_PHY_ANY, MII_OFFSET_ANY, 0);
    238 
    239 	/* Choose a default media. */
    240 	if (LIST_FIRST(&mii->mii_phys) == NULL) {
    241 		ifmedia_add(&mii->mii_media, IFM_ETHER|IFM_NONE, 0, NULL);
    242 		ifmedia_set(&mii->mii_media, IFM_ETHER|IFM_NONE);
    243 	} else
    244 		ifmedia_set(&mii->mii_media, IFM_ETHER|IFM_AUTO);
    245 
    246 	if_attach(ifp);
    247 	ether_ifattach(ifp, laddr);
    248 }
    249 
    250 u_int
    251 gmac_read_reg(sc, reg)
    252 	struct gmac_softc *sc;
    253 	int reg;
    254 {
    255 	return in32rb(sc->sc_reg + reg);
    256 }
    257 
    258 void
    259 gmac_write_reg(sc, reg, val)
    260 	struct gmac_softc *sc;
    261 	int reg;
    262 	u_int val;
    263 {
    264 	out32rb(sc->sc_reg + reg, val);
    265 }
    266 
    267 void
    268 gmac_start_txdma(sc)
    269 	struct gmac_softc *sc;
    270 {
    271 	u_int x;
    272 
    273 	x = gmac_read_reg(sc, GMAC_TXDMACONFIG);
    274 	x |= 1;
    275 	gmac_write_reg(sc, GMAC_TXDMACONFIG, x);
    276 	x = gmac_read_reg(sc, GMAC_TXMACCONFIG);
    277 	x |= 1;
    278 	gmac_write_reg(sc, GMAC_TXMACCONFIG, x);
    279 }
    280 
    281 void
    282 gmac_start_rxdma(sc)
    283 	struct gmac_softc *sc;
    284 {
    285 	u_int x;
    286 
    287 	x = gmac_read_reg(sc, GMAC_RXDMACONFIG);
    288 	x |= 1;
    289 	gmac_write_reg(sc, GMAC_RXDMACONFIG, x);
    290 	x = gmac_read_reg(sc, GMAC_RXMACCONFIG);
    291 	x |= 1;
    292 	gmac_write_reg(sc, GMAC_RXMACCONFIG, x);
    293 }
    294 
    295 void
    296 gmac_stop_txdma(sc)
    297 	struct gmac_softc *sc;
    298 {
    299 	u_int x;
    300 
    301 	x = gmac_read_reg(sc, GMAC_TXDMACONFIG);
    302 	x &= ~1;
    303 	gmac_write_reg(sc, GMAC_TXDMACONFIG, x);
    304 	x = gmac_read_reg(sc, GMAC_TXMACCONFIG);
    305 	x &= ~1;
    306 	gmac_write_reg(sc, GMAC_TXMACCONFIG, x);
    307 }
    308 
    309 void
    310 gmac_stop_rxdma(sc)
    311 	struct gmac_softc *sc;
    312 {
    313 	u_int x;
    314 
    315 	x = gmac_read_reg(sc, GMAC_RXDMACONFIG);
    316 	x &= ~1;
    317 	gmac_write_reg(sc, GMAC_RXDMACONFIG, x);
    318 	x = gmac_read_reg(sc, GMAC_RXMACCONFIG);
    319 	x &= ~1;
    320 	gmac_write_reg(sc, GMAC_RXMACCONFIG, x);
    321 }
    322 
    323 int
    324 gmac_intr(v)
    325 	void *v;
    326 {
    327 	struct gmac_softc *sc = v;
    328 	u_int status;
    329 
    330 	status = gmac_read_reg(sc, GMAC_STATUS) & 0xff;
    331 	if (status == 0)
    332 		return 0;
    333 
    334 	if (status & GMAC_INT_RXDONE)
    335 		gmac_rint(sc);
    336 
    337 	if (status & GMAC_INT_TXEMPTY)
    338 		gmac_tint(sc);
    339 
    340 	return 1;
    341 }
    342 
    343 void
    344 gmac_tint(sc)
    345 	struct gmac_softc *sc;
    346 {
    347 	struct ifnet *ifp = &sc->sc_if;
    348 
    349 	ifp->if_flags &= ~IFF_OACTIVE;
    350 	ifp->if_timer = 0;
    351 	gmac_start(ifp);
    352 }
    353 
    354 void
    355 gmac_rint(sc)
    356 	struct gmac_softc *sc;
    357 {
    358 	struct ifnet *ifp = &sc->sc_if;
    359 	volatile struct gmac_dma *dp;
    360 	struct mbuf *m;
    361 	int i, len;
    362 	u_int cmd;
    363 
    364 	for (i = sc->sc_rxlast;; i++) {
    365 		if (i == NRXBUF)
    366 			i = 0;
    367 
    368 		dp = &sc->sc_rxlist[i];
    369 		cmd = le32toh(dp->cmd);
    370 		if (cmd & GMAC_OWN)
    371 			break;
    372 		len = (cmd >> 16) & GMAC_LEN_MASK;
    373 		len -= 4;	/* CRC */
    374 
    375 		if (le32toh(dp->cmd_hi) & 0x40000000) {
    376 			ifp->if_ierrors++;
    377 			goto next;
    378 		}
    379 
    380 		m = gmac_get(sc, sc->sc_rxbuf[i], len);
    381 		if (m == NULL) {
    382 			ifp->if_ierrors++;
    383 			goto next;
    384 		}
    385 
    386 #if NBPFILTER > 0
    387 		/*
    388 		 * Check if there's a BPF listener on this interface.
    389 		 * If so, hand off the raw packet to BPF.
    390 		 */
    391 		if (ifp->if_bpf)
    392 			bpf_tap(ifp->if_bpf, sc->sc_rxbuf[i], len);
    393 #endif
    394 		(*ifp->if_input)(ifp, m);
    395 		ifp->if_ipackets++;
    396 
    397 next:
    398 		dp->cmd_hi = 0;
    399 		__asm __volatile ("sync");
    400 		dp->cmd = htole32(GMAC_OWN);
    401 	}
    402 	sc->sc_rxlast = i;
    403 }
    404 
    405 struct mbuf *
    406 gmac_get(sc, pkt, totlen)
    407 	struct gmac_softc *sc;
    408 	caddr_t pkt;
    409 	int totlen;
    410 {
    411 	struct mbuf *m;
    412 	struct mbuf *top, **mp;
    413 	int len;
    414 
    415 	MGETHDR(m, M_DONTWAIT, MT_DATA);
    416 	if (m == 0)
    417 		return 0;
    418 	m->m_pkthdr.rcvif = &sc->sc_if;
    419 	m->m_pkthdr.len = totlen;
    420 	len = MHLEN;
    421 	top = 0;
    422 	mp = &top;
    423 
    424 	while (totlen > 0) {
    425 		if (top) {
    426 			MGET(m, M_DONTWAIT, MT_DATA);
    427 			if (m == 0) {
    428 				m_freem(top);
    429 				return 0;
    430 			}
    431 			len = MLEN;
    432 		}
    433 		if (totlen >= MINCLSIZE) {
    434 			MCLGET(m, M_DONTWAIT);
    435 			if ((m->m_flags & M_EXT) == 0) {
    436 				m_free(m);
    437 				m_freem(top);
    438 				return 0;
    439 			}
    440 			len = MCLBYTES;
    441 		}
    442 		m->m_len = len = min(totlen, len);
    443 		bcopy(pkt, mtod(m, caddr_t), len);
    444 		pkt += len;
    445 		totlen -= len;
    446 		*mp = m;
    447 		mp = &m->m_next;
    448 	}
    449 
    450 	return top;
    451 }
    452 
    453 void
    454 gmac_start(ifp)
    455 	struct ifnet *ifp;
    456 {
    457 	struct gmac_softc *sc = ifp->if_softc;
    458 	struct mbuf *m;
    459 	caddr_t buff;
    460 	int i, tlen;
    461 	volatile struct gmac_dma *dp;
    462 
    463 	if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
    464 		return;
    465 
    466 	for (;;) {
    467 		if (ifp->if_flags & IFF_OACTIVE)
    468 			break;
    469 
    470 		IF_DEQUEUE(&ifp->if_snd, m);
    471 		if (m == 0)
    472 			break;
    473 
    474 		/* 5 seconds to watch for failing to transmit */
    475 		ifp->if_timer = 5;
    476 		ifp->if_opackets++;		/* # of pkts */
    477 
    478 		i = sc->sc_txnext;
    479 		buff = sc->sc_txbuf[i];
    480 		tlen = gmac_put(sc, buff, m);
    481 
    482 		dp = &sc->sc_txlist[i];
    483 		dp->cmd_hi = 0;
    484 		dp->address_hi = 0;
    485 		dp->cmd = htole32(tlen | GMAC_OWN | GMAC_SOP);
    486 
    487 		i++;
    488 		if (i == NTXBUF)
    489 			i = 0;
    490 		__asm __volatile ("sync");
    491 
    492 		gmac_write_reg(sc, GMAC_TXDMAKICK, i);
    493 		sc->sc_txnext = i;
    494 
    495 #if NBPFILTER > 0
    496 		/*
    497 		 * If BPF is listening on this interface, let it see the
    498 		 * packet before we commit it to the wire.
    499 		 */
    500 		if (ifp->if_bpf)
    501 			bpf_tap(ifp->if_bpf, buff, tlen);
    502 #endif
    503 
    504 		i++;
    505 		if (i == NTXBUF)
    506 			i = 0;
    507 		if (i == gmac_read_reg(sc, GMAC_TXDMACOMPLETE)) {
    508 			ifp->if_flags |= IFF_OACTIVE;
    509 			break;
    510 		}
    511 	}
    512 }
    513 
    514 int
    515 gmac_put(sc, buff, m)
    516 	struct gmac_softc *sc;
    517 	caddr_t buff;
    518 	struct mbuf *m;
    519 {
    520 	struct mbuf *n;
    521 	int len, tlen = 0;
    522 
    523 	for (; m; m = n) {
    524 		len = m->m_len;
    525 		if (len == 0) {
    526 			MFREE(m, n);
    527 			continue;
    528 		}
    529 		bcopy(mtod(m, caddr_t), buff, len);
    530 		buff += len;
    531 		tlen += len;
    532 		MFREE(m, n);
    533 	}
    534 	if (tlen > 2048)
    535 		panic("%s: gmac_put packet overflow", sc->sc_dev.dv_xname);
    536 
    537 	return tlen;
    538 }
    539 
    540 void
    541 gmac_reset(sc)
    542 	struct gmac_softc *sc;
    543 {
    544 	int i, s;
    545 
    546 	s = splnet();
    547 
    548 	gmac_stop_txdma(sc);
    549 	gmac_stop_rxdma(sc);
    550 
    551 	gmac_write_reg(sc, GMAC_SOFTWARERESET, 3);
    552 	for (i = 10; i > 0; i--) {
    553 		delay(300000);				/* XXX long delay */
    554 		if ((gmac_read_reg(sc, GMAC_SOFTWARERESET) & 3) == 0)
    555 			break;
    556 	}
    557 	if (i == 0)
    558 		printf("%s: reset timeout\n", sc->sc_dev.dv_xname);
    559 
    560 	sc->sc_txnext = 0;
    561 	sc->sc_rxlast = 0;
    562 	for (i = 0; i < NRXBUF; i++)
    563 		sc->sc_rxlist[i].cmd = htole32(GMAC_OWN);
    564 	__asm __volatile ("sync");
    565 
    566 	gmac_write_reg(sc, GMAC_TXDMADESCBASEHI, 0);
    567 	gmac_write_reg(sc, GMAC_TXDMADESCBASELO,
    568 		       vtophys((vaddr_t)sc->sc_txlist));
    569 	gmac_write_reg(sc, GMAC_RXDMADESCBASEHI, 0);
    570 	gmac_write_reg(sc, GMAC_RXDMADESCBASELO,
    571 		       vtophys((vaddr_t)sc->sc_rxlist));
    572 	gmac_write_reg(sc, GMAC_RXDMAKICK, NRXBUF);
    573 
    574 	splx(s);
    575 }
    576 
    577 void
    578 gmac_stop(sc)
    579 	struct gmac_softc *sc;
    580 {
    581 	struct ifnet *ifp = &sc->sc_if;
    582 	int s;
    583 
    584 	s = splnet();
    585 
    586 	callout_stop(&sc->sc_tick_ch);
    587 	mii_down(&sc->sc_mii);
    588 
    589 	gmac_stop_txdma(sc);
    590 	gmac_stop_rxdma(sc);
    591 
    592 	gmac_write_reg(sc, GMAC_INTMASK, 0xffffffff);
    593 
    594 	ifp->if_flags &= ~(IFF_UP | IFF_RUNNING);
    595 	ifp->if_timer = 0;
    596 
    597 	splx(s);
    598 }
    599 
    600 void
    601 gmac_init_mac(sc)
    602 	struct gmac_softc *sc;
    603 {
    604 	int i, tb;
    605 	char *laddr = sc->sc_laddr;
    606 
    607 	__asm ("mftb %0" : "=r"(tb));
    608 	gmac_write_reg(sc, GMAC_RANDOMSEED, tb);
    609 
    610 	/* init-mii */
    611 	gmac_write_reg(sc, GMAC_DATAPATHMODE, 4);
    612 	gmac_mii_writereg(&sc->sc_dev, 0, 0, 0x1000);
    613 
    614 	gmac_write_reg(sc, GMAC_TXDMACONFIG, 0xffc00);
    615 	gmac_write_reg(sc, GMAC_RXDMACONFIG, 0);
    616 	gmac_write_reg(sc, GMAC_MACPAUSE, 0x1bf0);
    617 	gmac_write_reg(sc, GMAC_INTERPACKETGAP0, 0);
    618 	gmac_write_reg(sc, GMAC_INTERPACKETGAP1, 8);
    619 	gmac_write_reg(sc, GMAC_INTERPACKETGAP2, 4);
    620 	gmac_write_reg(sc, GMAC_MINFRAMESIZE, ETHER_MIN_LEN);
    621 	gmac_write_reg(sc, GMAC_MAXFRAMESIZE, ETHER_MAX_LEN);
    622 	gmac_write_reg(sc, GMAC_PASIZE, 7);
    623 	gmac_write_reg(sc, GMAC_JAMSIZE, 4);
    624 	gmac_write_reg(sc, GMAC_ATTEMPTLIMIT,0x10);
    625 	gmac_write_reg(sc, GMAC_MACCNTLTYPE, 0x8808);
    626 
    627 	gmac_write_reg(sc, GMAC_MACADDRESS0, (laddr[4] << 8) | laddr[5]);
    628 	gmac_write_reg(sc, GMAC_MACADDRESS1, (laddr[2] << 8) | laddr[3]);
    629 	gmac_write_reg(sc, GMAC_MACADDRESS2, (laddr[0] << 8) | laddr[1]);
    630 	gmac_write_reg(sc, GMAC_MACADDRESS3, 0);
    631 	gmac_write_reg(sc, GMAC_MACADDRESS4, 0);
    632 	gmac_write_reg(sc, GMAC_MACADDRESS5, 0);
    633 	gmac_write_reg(sc, GMAC_MACADDRESS6, 1);
    634 	gmac_write_reg(sc, GMAC_MACADDRESS7, 0xc200);
    635 	gmac_write_reg(sc, GMAC_MACADDRESS8, 0x0180);
    636 	gmac_write_reg(sc, GMAC_MACADDRFILT0, 0);
    637 	gmac_write_reg(sc, GMAC_MACADDRFILT1, 0);
    638 	gmac_write_reg(sc, GMAC_MACADDRFILT2, 0);
    639 	gmac_write_reg(sc, GMAC_MACADDRFILT2_1MASK, 0);
    640 	gmac_write_reg(sc, GMAC_MACADDRFILT0MASK, 0);
    641 
    642 	for (i = 0; i < 0x6c; i += 4)
    643 		gmac_write_reg(sc, GMAC_HASHTABLE0 + i, 0);
    644 
    645 	gmac_write_reg(sc, GMAC_SLOTTIME, 0x40);
    646 
    647 	if (IFM_OPTIONS(sc->sc_mii.mii_media_active) & IFM_FDX) {
    648 		gmac_write_reg(sc, GMAC_TXMACCONFIG, 6);
    649 		gmac_write_reg(sc, GMAC_XIFCONFIG, 1);
    650 	} else {
    651 		gmac_write_reg(sc, GMAC_TXMACCONFIG, 0);
    652 		gmac_write_reg(sc, GMAC_XIFCONFIG, 5);
    653 	}
    654 
    655 	if (0)	/* g-bit? */
    656 		gmac_write_reg(sc, GMAC_MACCTRLCONFIG, 3);
    657 	else
    658 		gmac_write_reg(sc, GMAC_MACCTRLCONFIG, 0);
    659 }
    660 
    661 void
    662 gmac_setladrf(sc)
    663 	struct gmac_softc *sc;
    664 {
    665 	struct ifnet *ifp = &sc->sc_if;
    666 	struct ether_multi *enm;
    667 	struct ether_multistep step;
    668 	struct ethercom *ec = &sc->sc_ethercom;
    669 	u_int32_t crc;
    670 	u_int32_t hash[16];
    671 	u_int v;
    672 	int i;
    673 
    674 	/* Clear hash table */
    675 	for (i = 0; i < 16; i++)
    676 		hash[i] = 0;
    677 
    678 	/* Get current RX configuration */
    679 	v = gmac_read_reg(sc, GMAC_RXMACCONFIG);
    680 
    681 	if ((ifp->if_flags & IFF_PROMISC) != 0) {
    682 		/* Turn on promiscuous mode; turn off the hash filter */
    683 		v |= GMAC_RXMAC_PR;
    684 		v &= ~GMAC_RXMAC_HEN;
    685 		ifp->if_flags |= IFF_ALLMULTI;
    686 		goto chipit;
    687 	}
    688 
    689 	/* Turn off promiscuous mode; turn on the hash filter */
    690 	v &= ~GMAC_RXMAC_PR;
    691 	v |= GMAC_RXMAC_HEN;
    692 
    693 	/*
    694 	 * Set up multicast address filter by passing all multicast addresses
    695 	 * through a crc generator, and then using the high order 8 bits as an
    696 	 * index into the 256 bit logical address filter.  The high order bit
    697 	 * selects the word, while the rest of the bits select the bit within
    698 	 * the word.
    699 	 */
    700 
    701 	ETHER_FIRST_MULTI(step, ec, enm);
    702 	while (enm != NULL) {
    703 		if (bcmp(enm->enm_addrlo, enm->enm_addrhi, 6)) {
    704 			/*
    705 			 * We must listen to a range of multicast addresses.
    706 			 * For now, just accept all multicasts, rather than
    707 			 * trying to set only those filter bits needed to match
    708 			 * the range.  (At this time, the only use of address
    709 			 * ranges is for IP multicast routing, for which the
    710 			 * range is big enough to require all bits set.)
    711 			 */
    712 			for (i = 0; i < 16; i++)
    713 				hash[i] = 0xffff;
    714 			ifp->if_flags |= IFF_ALLMULTI;
    715 			goto chipit;
    716 		}
    717 
    718 		crc = ether_crc32_le(enm->enm_addrlo, ETHER_ADDR_LEN);
    719 
    720 		/* Just want the 8 most significant bits. */
    721 		crc >>= 24;
    722 
    723 		/* Set the corresponding bit in the filter. */
    724 		hash[crc >> 4] |= 1 << (crc & 0xf);
    725 
    726 		ETHER_NEXT_MULTI(step, enm);
    727 	}
    728 
    729 	ifp->if_flags &= ~IFF_ALLMULTI;
    730 
    731 chipit:
    732 	/* Now load the hash table into the chip */
    733 	for (i = 0; i < 16; i++)
    734 		gmac_write_reg(sc, GMAC_HASHTABLE0 + i * 4, hash[i]);
    735 
    736 	gmac_write_reg(sc, GMAC_RXMACCONFIG, v);
    737 }
    738 
    739 void
    740 gmac_init(sc)
    741 	struct gmac_softc *sc;
    742 {
    743 	struct ifnet *ifp = &sc->sc_if;
    744 
    745 	gmac_stop_txdma(sc);
    746 	gmac_stop_rxdma(sc);
    747 
    748 	gmac_init_mac(sc);
    749 	gmac_setladrf(sc);
    750 
    751 	gmac_start_txdma(sc);
    752 	gmac_start_rxdma(sc);
    753 
    754 	gmac_write_reg(sc, GMAC_INTMASK, ~(GMAC_INT_TXEMPTY | GMAC_INT_RXDONE));
    755 
    756 	ifp->if_flags |= IFF_RUNNING;
    757 	ifp->if_flags &= ~IFF_OACTIVE;
    758 	ifp->if_timer = 0;
    759 
    760 	callout_reset(&sc->sc_tick_ch, 1, gmac_mii_tick, sc);
    761 
    762 	gmac_start(ifp);
    763 }
    764 
    765 int
    766 gmac_ioctl(ifp, cmd, data)
    767 	struct ifnet *ifp;
    768 	u_long cmd;
    769 	caddr_t data;
    770 {
    771 	struct gmac_softc *sc = ifp->if_softc;
    772 	struct ifaddr *ifa = (struct ifaddr *)data;
    773 	struct ifreq *ifr = (struct ifreq *)data;
    774 	int s, error = 0;
    775 
    776 	s = splnet();
    777 
    778 	switch (cmd) {
    779 
    780 	case SIOCSIFADDR:
    781 		ifp->if_flags |= IFF_UP;
    782 
    783 		switch (ifa->ifa_addr->sa_family) {
    784 #ifdef INET
    785 		case AF_INET:
    786 			gmac_init(sc);
    787 			arp_ifinit(ifp, ifa);
    788 			break;
    789 #endif
    790 #ifdef NS
    791 		case AF_NS:
    792 		    {
    793 			struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
    794 
    795 			if (ns_nullhost(*ina))
    796 				ina->x_host =
    797 				    *(union ns_host *)LLADDR(ifp->if_sadl);
    798 			else {
    799 				bcopy(ina->x_host.c_host,
    800 				    LLADDR(ifp->if_sadl),
    801 				    sizeof(sc->sc_enaddr));
    802 			}
    803 			/* Set new address. */
    804 			gmac_init(sc);
    805 			break;
    806 		    }
    807 #endif
    808 		default:
    809 			gmac_init(sc);
    810 			break;
    811 		}
    812 		break;
    813 
    814 	case SIOCSIFFLAGS:
    815 		if ((ifp->if_flags & IFF_UP) == 0 &&
    816 		    (ifp->if_flags & IFF_RUNNING) != 0) {
    817 			/*
    818 			 * If interface is marked down and it is running, then
    819 			 * stop it.
    820 			 */
    821 			gmac_stop(sc);
    822 			ifp->if_flags &= ~IFF_RUNNING;
    823 		} else if ((ifp->if_flags & IFF_UP) != 0 &&
    824 		    (ifp->if_flags & IFF_RUNNING) == 0) {
    825 			/*
    826 			 * If interface is marked up and it is stopped, then
    827 			 * start it.
    828 			 */
    829 			gmac_init(sc);
    830 		} else {
    831 			/*
    832 			 * Reset the interface to pick up changes in any other
    833 			 * flags that affect hardware registers.
    834 			 */
    835 			gmac_reset(sc);
    836 			gmac_init(sc);
    837 		}
    838 #ifdef GMAC_DEBUG
    839 		if (ifp->if_flags & IFF_DEBUG)
    840 			sc->sc_flags |= GMAC_DEBUGFLAG;
    841 #endif
    842 		break;
    843 
    844 	case SIOCADDMULTI:
    845 	case SIOCDELMULTI:
    846 		error = (cmd == SIOCADDMULTI) ?
    847 		    ether_addmulti(ifr, &sc->sc_ethercom) :
    848 		    ether_delmulti(ifr, &sc->sc_ethercom);
    849 
    850 		if (error == ENETRESET) {
    851 			/*
    852 			 * Multicast list has changed; set the hardware filter
    853 			 * accordingly.
    854 			 */
    855 			gmac_init(sc);
    856 			/* gmac_setladrf(sc); */
    857 			error = 0;
    858 		}
    859 		break;
    860 
    861 	case SIOCGIFMEDIA:
    862 	case SIOCSIFMEDIA:
    863 		error = ifmedia_ioctl(ifp, ifr, &sc->sc_mii.mii_media, cmd);
    864 		break;
    865 
    866 	default:
    867 		error = EINVAL;
    868 	}
    869 
    870 	splx(s);
    871 	return error;
    872 }
    873 
    874 void
    875 gmac_watchdog(ifp)
    876 	struct ifnet *ifp;
    877 {
    878 	struct gmac_softc *sc = ifp->if_softc;
    879 
    880 	printf("%s: device timeout\n", ifp->if_xname);
    881 	ifp->if_oerrors++;
    882 
    883 	gmac_reset(sc);
    884 	gmac_init(sc);
    885 }
    886 
    887 int
    888 gmac_mediachange(ifp)
    889 	struct ifnet *ifp;
    890 {
    891 	struct gmac_softc *sc = ifp->if_softc;
    892 
    893 	return mii_mediachg(&sc->sc_mii);
    894 }
    895 
    896 void
    897 gmac_mediastatus(ifp, ifmr)
    898 	struct ifnet *ifp;
    899 	struct ifmediareq *ifmr;
    900 {
    901 	struct gmac_softc *sc = ifp->if_softc;
    902 
    903 	mii_pollstat(&sc->sc_mii);
    904 
    905 	ifmr->ifm_status = sc->sc_mii.mii_media_status;
    906 	ifmr->ifm_active = sc->sc_mii.mii_media_active;
    907 }
    908 
    909 int
    910 gmac_mii_readreg(dev, phy, reg)
    911 	struct device *dev;
    912 	int phy, reg;
    913 {
    914 	struct gmac_softc *sc = (void *)dev;
    915 	int i;
    916 
    917 	gmac_write_reg(sc, GMAC_MIFFRAMEOUTPUT,
    918 		0x60020000 | (phy << 23) | (reg << 18));
    919 
    920 	for (i = 1000; i >= 0; i -= 10) {
    921 		if (gmac_read_reg(sc, GMAC_MIFFRAMEOUTPUT) & 0x10000)
    922 			break;
    923 		delay(10);
    924 	}
    925 	if (i < 0) {
    926 		printf("%s: gmac_mii_readreg: timeout\n", sc->sc_dev.dv_xname);
    927 		return 0;
    928 	}
    929 
    930 	return gmac_read_reg(sc, GMAC_MIFFRAMEOUTPUT) & 0xffff;
    931 }
    932 
    933 void
    934 gmac_mii_writereg(dev, phy, reg, val)
    935 	struct device *dev;
    936 	int phy, reg, val;
    937 {
    938 	struct gmac_softc *sc = (void *)dev;
    939 	int i;
    940 
    941 	gmac_write_reg(sc, GMAC_MIFFRAMEOUTPUT,
    942 		0x50020000 | (phy << 23) | (reg << 18) | (val & 0xffff));
    943 
    944 	for (i = 1000; i >= 0; i -= 10) {
    945 		if (gmac_read_reg(sc, GMAC_MIFFRAMEOUTPUT) & 0x10000)
    946 			break;
    947 		delay(10);
    948 	}
    949 	if (i < 0)
    950 		printf("%s: gmac_mii_writereg: timeout\n", sc->sc_dev.dv_xname);
    951 }
    952 
    953 void
    954 gmac_mii_statchg(dev)
    955 	struct device *dev;
    956 {
    957 	struct gmac_softc *sc = (void *)dev;
    958 
    959 	gmac_stop_txdma(sc);
    960 	gmac_stop_rxdma(sc);
    961 
    962 	if (IFM_OPTIONS(sc->sc_mii.mii_media_active) & IFM_FDX) {
    963 		gmac_write_reg(sc, GMAC_TXMACCONFIG, 6);
    964 		gmac_write_reg(sc, GMAC_XIFCONFIG, 1);
    965 	} else {
    966 		gmac_write_reg(sc, GMAC_TXMACCONFIG, 0);
    967 		gmac_write_reg(sc, GMAC_XIFCONFIG, 5);
    968 	}
    969 
    970 	if (0)	/* g-bit? */
    971 		gmac_write_reg(sc, GMAC_MACCTRLCONFIG, 3);
    972 	else
    973 		gmac_write_reg(sc, GMAC_MACCTRLCONFIG, 0);
    974 
    975 	gmac_start_txdma(sc);
    976 	gmac_start_rxdma(sc);
    977 }
    978 
    979 void
    980 gmac_mii_tick(v)
    981 	void *v;
    982 {
    983 	struct gmac_softc *sc = v;
    984 	int s;
    985 
    986 	s = splnet();
    987 	mii_tick(&sc->sc_mii);
    988 	splx(s);
    989 
    990 	callout_reset(&sc->sc_tick_ch, hz, gmac_mii_tick, sc);
    991 }
    992