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