Home | History | Annotate | Line # | Download | only in dev
if_bm.c revision 1.36
      1  1.36    dyoung /*	$NetBSD: if_bm.c,v 1.36 2008/01/19 22:10:15 dyoung Exp $	*/
      2   1.1    tsubai 
      3   1.1    tsubai /*-
      4   1.4    tsubai  * Copyright (C) 1998, 1999, 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.36    dyoung __KERNEL_RCSID(0, "$NetBSD: if_bm.c,v 1.36 2008/01/19 22:10:15 dyoung Exp $");
     31   1.1    tsubai 
     32   1.1    tsubai #include "opt_inet.h"
     33   1.1    tsubai #include "bpfilter.h"
     34   1.1    tsubai 
     35   1.1    tsubai #include <sys/param.h>
     36   1.1    tsubai #include <sys/device.h>
     37   1.1    tsubai #include <sys/ioctl.h>
     38   1.4    tsubai #include <sys/kernel.h>
     39   1.1    tsubai #include <sys/mbuf.h>
     40   1.1    tsubai #include <sys/socket.h>
     41   1.1    tsubai #include <sys/systm.h>
     42   1.7   thorpej #include <sys/callout.h>
     43   1.1    tsubai 
     44  1.10       mrg #include <uvm/uvm_extern.h>
     45   1.4    tsubai 
     46   1.1    tsubai #include <net/if.h>
     47  1.22      matt #include <net/if_dl.h>
     48   1.1    tsubai #include <net/if_ether.h>
     49   1.1    tsubai #include <net/if_media.h>
     50   1.1    tsubai 
     51   1.1    tsubai #if NBPFILTER > 0
     52   1.1    tsubai #include <net/bpf.h>
     53   1.1    tsubai #endif
     54   1.1    tsubai 
     55   1.4    tsubai #ifdef INET
     56   1.4    tsubai #include <netinet/in.h>
     57   1.4    tsubai #include <netinet/if_inarp.h>
     58   1.4    tsubai #endif
     59   1.1    tsubai 
     60  1.22      matt 
     61   1.1    tsubai #include <dev/ofw/openfirm.h>
     62   1.1    tsubai 
     63   1.4    tsubai #include <dev/mii/mii.h>
     64   1.4    tsubai #include <dev/mii/miivar.h>
     65   1.4    tsubai #include <dev/mii/mii_bitbang.h>
     66   1.4    tsubai 
     67  1.24    kleink #include <powerpc/spr.h>
     68  1.24    kleink 
     69   1.1    tsubai #include <machine/autoconf.h>
     70   1.1    tsubai #include <machine/pio.h>
     71   1.1    tsubai 
     72   1.1    tsubai #include <macppc/dev/dbdma.h>
     73   1.1    tsubai #include <macppc/dev/if_bmreg.h>
     74   1.1    tsubai 
     75   1.1    tsubai #define BMAC_TXBUFS 2
     76   1.1    tsubai #define BMAC_RXBUFS 16
     77   1.1    tsubai #define BMAC_BUFLEN 2048
     78   1.1    tsubai 
     79   1.1    tsubai struct bmac_softc {
     80   1.1    tsubai 	struct device sc_dev;
     81   1.1    tsubai 	struct ethercom sc_ethercom;
     82   1.1    tsubai #define sc_if sc_ethercom.ec_if
     83   1.7   thorpej 	struct callout sc_tick_ch;
     84  1.34   garbled 	bus_space_tag_t sc_iot;
     85  1.34   garbled 	bus_space_handle_t sc_ioh;
     86   1.1    tsubai 	dbdma_regmap_t *sc_txdma;
     87   1.1    tsubai 	dbdma_regmap_t *sc_rxdma;
     88   1.1    tsubai 	dbdma_command_t *sc_txcmd;
     89   1.1    tsubai 	dbdma_command_t *sc_rxcmd;
     90  1.30  christos 	void *sc_txbuf;
     91  1.30  christos 	void *sc_rxbuf;
     92   1.1    tsubai 	int sc_rxlast;
     93   1.1    tsubai 	int sc_flags;
     94   1.4    tsubai 	struct mii_data sc_mii;
     95   1.1    tsubai 	u_char sc_enaddr[6];
     96   1.1    tsubai };
     97   1.1    tsubai 
     98   1.1    tsubai #define BMAC_BMACPLUS	0x01
     99   1.4    tsubai #define BMAC_DEBUGFLAG	0x02
    100   1.1    tsubai 
    101  1.34   garbled extern volatile uint32_t *heathrow_FCR;
    102   1.1    tsubai 
    103  1.35    dyoung int bmac_match(struct device *, struct cfdata *, void *);
    104  1.35    dyoung void bmac_attach(struct device *, struct device *, void *);
    105  1.35    dyoung void bmac_reset_chip(struct bmac_softc *);
    106  1.35    dyoung void bmac_init(struct bmac_softc *);
    107  1.35    dyoung void bmac_init_dma(struct bmac_softc *);
    108  1.35    dyoung int bmac_intr(void *);
    109  1.35    dyoung int bmac_rint(void *);
    110  1.35    dyoung void bmac_reset(struct bmac_softc *);
    111  1.35    dyoung void bmac_stop(struct bmac_softc *);
    112  1.35    dyoung void bmac_start(struct ifnet *);
    113  1.35    dyoung void bmac_transmit_packet(struct bmac_softc *, void *, int);
    114  1.35    dyoung int bmac_put(struct bmac_softc *, void *, struct mbuf *);
    115  1.35    dyoung struct mbuf *bmac_get(struct bmac_softc *, void *, int);
    116  1.35    dyoung void bmac_watchdog(struct ifnet *);
    117  1.35    dyoung int bmac_ioctl(struct ifnet *, u_long, void *);
    118  1.35    dyoung void bmac_setladrf(struct bmac_softc *);
    119  1.35    dyoung 
    120  1.35    dyoung int bmac_mii_readreg(struct device *, int, int);
    121  1.35    dyoung void bmac_mii_writereg(struct device *, int, int, int);
    122  1.35    dyoung void bmac_mii_statchg(struct device *);
    123  1.35    dyoung void bmac_mii_tick(void *);
    124  1.35    dyoung u_int32_t bmac_mbo_read(struct device *);
    125  1.35    dyoung void bmac_mbo_write(struct device *, u_int32_t);
    126   1.1    tsubai 
    127  1.19   thorpej CFATTACH_DECL(bm, sizeof(struct bmac_softc),
    128  1.19   thorpej     bmac_match, bmac_attach, NULL, NULL);
    129   1.1    tsubai 
    130  1.34   garbled const struct mii_bitbang_ops bmac_mbo = {
    131   1.4    tsubai 	bmac_mbo_read, bmac_mbo_write,
    132   1.4    tsubai 	{ MIFDO, MIFDI, MIFDC, MIFDIR, 0 }
    133   1.4    tsubai };
    134   1.4    tsubai 
    135  1.34   garbled static inline uint16_t
    136  1.34   garbled bmac_read_reg(struct bmac_softc *sc, bus_size_t off)
    137   1.1    tsubai {
    138  1.34   garbled 	return bus_space_read_2(sc->sc_iot, sc->sc_ioh, off);
    139   1.1    tsubai }
    140   1.1    tsubai 
    141  1.34   garbled static inline void
    142  1.34   garbled bmac_write_reg(struct bmac_softc *sc, bus_size_t off, uint16_t val)
    143   1.1    tsubai {
    144  1.34   garbled 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, off, val);
    145   1.1    tsubai }
    146   1.1    tsubai 
    147  1.34   garbled static inline void
    148  1.34   garbled bmac_set_bits(struct bmac_softc *sc, bus_size_t off, uint16_t val)
    149   1.1    tsubai {
    150   1.1    tsubai 	val |= bmac_read_reg(sc, off);
    151   1.1    tsubai 	bmac_write_reg(sc, off, val);
    152   1.1    tsubai }
    153   1.1    tsubai 
    154  1.34   garbled static inline void
    155  1.34   garbled bmac_reset_bits(struct bmac_softc *sc, bus_size_t off, uint16_t val)
    156   1.1    tsubai {
    157   1.1    tsubai 	bmac_write_reg(sc, off, bmac_read_reg(sc, off) & ~val);
    158   1.1    tsubai }
    159   1.1    tsubai 
    160   1.1    tsubai int
    161  1.34   garbled bmac_match(struct device *parent, struct cfdata *cf, void *aux)
    162   1.1    tsubai {
    163   1.1    tsubai 	struct confargs *ca = aux;
    164   1.1    tsubai 
    165   1.1    tsubai 	if (ca->ca_nreg < 24 || ca->ca_nintr < 12)
    166   1.1    tsubai 		return 0;
    167   1.1    tsubai 
    168   1.1    tsubai 	if (strcmp(ca->ca_name, "bmac") == 0)		/* bmac */
    169   1.1    tsubai 		return 1;
    170   1.1    tsubai 	if (strcmp(ca->ca_name, "ethernet") == 0)	/* bmac+ */
    171   1.1    tsubai 		return 1;
    172   1.1    tsubai 
    173   1.1    tsubai 	return 0;
    174   1.1    tsubai }
    175   1.1    tsubai 
    176   1.1    tsubai void
    177  1.34   garbled bmac_attach(struct device *parent, struct device *self, void *aux)
    178   1.1    tsubai {
    179   1.1    tsubai 	struct confargs *ca = aux;
    180   1.1    tsubai 	struct bmac_softc *sc = (void *)self;
    181   1.1    tsubai 	struct ifnet *ifp = &sc->sc_if;
    182   1.4    tsubai 	struct mii_data *mii = &sc->sc_mii;
    183   1.1    tsubai 	u_char laddr[6];
    184   1.1    tsubai 
    185  1.32        ad 	callout_init(&sc->sc_tick_ch, 0);
    186   1.7   thorpej 
    187   1.1    tsubai 	sc->sc_flags =0;
    188   1.3    tsubai 	if (strcmp(ca->ca_name, "ethernet") == 0) {
    189   1.3    tsubai 		char name[64];
    190   1.3    tsubai 
    191  1.16       wiz 		memset(name, 0, 64);
    192   1.3    tsubai 		OF_package_to_path(ca->ca_node, name, sizeof(name));
    193   1.3    tsubai 		OF_open(name);
    194   1.1    tsubai 		sc->sc_flags |= BMAC_BMACPLUS;
    195   1.3    tsubai 	}
    196   1.1    tsubai 
    197   1.1    tsubai 	ca->ca_reg[0] += ca->ca_baseaddr;
    198   1.1    tsubai 	ca->ca_reg[2] += ca->ca_baseaddr;
    199   1.1    tsubai 	ca->ca_reg[4] += ca->ca_baseaddr;
    200   1.1    tsubai 
    201  1.34   garbled 	sc->sc_iot = ca->ca_tag;
    202  1.34   garbled 	if (bus_space_map(sc->sc_iot, ca->ca_reg[0], ca->ca_reg[1], 0,
    203  1.34   garbled 	    &sc->sc_ioh) != 0) {
    204  1.34   garbled 		aprint_error(": couldn't map %#x", ca->ca_reg[0]);
    205  1.34   garbled 		return;
    206  1.34   garbled 	}
    207   1.1    tsubai 
    208   1.1    tsubai 	bmac_write_reg(sc, INTDISABLE, NoEventsMask);
    209   1.1    tsubai 
    210   1.1    tsubai 	if (OF_getprop(ca->ca_node, "local-mac-address", laddr, 6) == -1 &&
    211   1.1    tsubai 	    OF_getprop(ca->ca_node, "mac-address", laddr, 6) == -1) {
    212   1.1    tsubai 		printf(": cannot get mac-address\n");
    213   1.1    tsubai 		return;
    214   1.1    tsubai 	}
    215  1.16       wiz 	memcpy(sc->sc_enaddr, laddr, 6);
    216   1.1    tsubai 
    217  1.20   thorpej 	sc->sc_txdma = mapiodev(ca->ca_reg[2], PAGE_SIZE);
    218  1.20   thorpej 	sc->sc_rxdma = mapiodev(ca->ca_reg[4], PAGE_SIZE);
    219   1.1    tsubai 	sc->sc_txcmd = dbdma_alloc(BMAC_TXBUFS * sizeof(dbdma_command_t));
    220   1.1    tsubai 	sc->sc_rxcmd = dbdma_alloc((BMAC_RXBUFS + 1) * sizeof(dbdma_command_t));
    221   1.1    tsubai 	sc->sc_txbuf = malloc(BMAC_BUFLEN * BMAC_TXBUFS, M_DEVBUF, M_NOWAIT);
    222   1.1    tsubai 	sc->sc_rxbuf = malloc(BMAC_BUFLEN * BMAC_RXBUFS, M_DEVBUF, M_NOWAIT);
    223   1.1    tsubai 	if (sc->sc_txbuf == NULL || sc->sc_rxbuf == NULL ||
    224   1.1    tsubai 	    sc->sc_txcmd == NULL || sc->sc_rxcmd == NULL) {
    225   1.1    tsubai 		printf("cannot allocate memory\n");
    226   1.1    tsubai 		return;
    227   1.1    tsubai 	}
    228   1.1    tsubai 
    229   1.1    tsubai 	printf(" irq %d,%d: address %s\n", ca->ca_intr[0], ca->ca_intr[2],
    230   1.1    tsubai 		ether_sprintf(laddr));
    231   1.1    tsubai 
    232  1.34   garbled 	intr_establish(ca->ca_intr[0], IST_EDGE, IPL_NET, bmac_intr, sc);
    233  1.34   garbled 	intr_establish(ca->ca_intr[2], IST_EDGE, IPL_NET, bmac_rint, sc);
    234   1.1    tsubai 
    235  1.16       wiz 	memcpy(ifp->if_xname, sc->sc_dev.dv_xname, IFNAMSIZ);
    236   1.1    tsubai 	ifp->if_softc = sc;
    237   1.1    tsubai 	ifp->if_ioctl = bmac_ioctl;
    238   1.1    tsubai 	ifp->if_start = bmac_start;
    239   1.1    tsubai 	ifp->if_flags =
    240   1.1    tsubai 		IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS | IFF_MULTICAST;
    241   1.1    tsubai 	ifp->if_watchdog = bmac_watchdog;
    242  1.17    itojun 	IFQ_SET_READY(&ifp->if_snd);
    243   1.1    tsubai 
    244   1.4    tsubai 	mii->mii_ifp = ifp;
    245   1.4    tsubai 	mii->mii_readreg = bmac_mii_readreg;
    246   1.4    tsubai 	mii->mii_writereg = bmac_mii_writereg;
    247   1.4    tsubai 	mii->mii_statchg = bmac_mii_statchg;
    248   1.4    tsubai 
    249  1.36    dyoung 	sc->sc_ethercom.ec_mii = mii;
    250  1.36    dyoung 	ifmedia_init(&mii->mii_media, 0, ether_mediachange, ether_mediastatus);
    251   1.5   thorpej 	mii_attach(&sc->sc_dev, mii, 0xffffffff, MII_PHY_ANY,
    252   1.6   thorpej 		      MII_OFFSET_ANY, 0);
    253   1.4    tsubai 
    254   1.4    tsubai 	/* Choose a default media. */
    255   1.4    tsubai 	if (LIST_FIRST(&mii->mii_phys) == NULL) {
    256   1.4    tsubai 		ifmedia_add(&mii->mii_media, IFM_ETHER|IFM_10_T, 0, NULL);
    257   1.4    tsubai 		ifmedia_set(&mii->mii_media, IFM_ETHER|IFM_10_T);
    258   1.4    tsubai 	} else
    259   1.4    tsubai 		ifmedia_set(&mii->mii_media, IFM_ETHER|IFM_AUTO);
    260   1.1    tsubai 
    261   1.1    tsubai 	bmac_reset_chip(sc);
    262   1.1    tsubai 
    263   1.1    tsubai 	if_attach(ifp);
    264   1.1    tsubai 	ether_ifattach(ifp, sc->sc_enaddr);
    265   1.1    tsubai }
    266   1.1    tsubai 
    267   1.1    tsubai /*
    268   1.1    tsubai  * Reset and enable bmac by heathrow FCR.
    269   1.1    tsubai  */
    270   1.1    tsubai void
    271   1.1    tsubai bmac_reset_chip(sc)
    272   1.1    tsubai 	struct bmac_softc *sc;
    273   1.1    tsubai {
    274   1.1    tsubai 	u_int v;
    275   1.1    tsubai 
    276   1.1    tsubai 	dbdma_reset(sc->sc_txdma);
    277   1.1    tsubai 	dbdma_reset(sc->sc_rxdma);
    278   1.1    tsubai 
    279   1.1    tsubai 	v = in32rb(heathrow_FCR);
    280   1.1    tsubai 
    281   1.1    tsubai 	v |= EnetEnable;
    282   1.1    tsubai 	out32rb(heathrow_FCR, v);
    283   1.1    tsubai 	delay(50000);
    284   1.1    tsubai 
    285   1.1    tsubai 	v |= ResetEnetCell;
    286   1.1    tsubai 	out32rb(heathrow_FCR, v);
    287   1.1    tsubai 	delay(50000);
    288   1.1    tsubai 
    289   1.1    tsubai 	v &= ~ResetEnetCell;
    290   1.1    tsubai 	out32rb(heathrow_FCR, v);
    291   1.1    tsubai 	delay(50000);
    292   1.1    tsubai 
    293   1.1    tsubai 	out32rb(heathrow_FCR, v);
    294   1.1    tsubai }
    295   1.1    tsubai 
    296   1.1    tsubai void
    297   1.1    tsubai bmac_init(sc)
    298   1.1    tsubai 	struct bmac_softc *sc;
    299   1.1    tsubai {
    300   1.1    tsubai 	struct ifnet *ifp = &sc->sc_if;
    301   1.1    tsubai 	struct ether_header *eh;
    302  1.30  christos 	void *data;
    303   1.4    tsubai 	int i, tb, bmcr;
    304   1.1    tsubai 	u_short *p;
    305   1.1    tsubai 
    306   1.1    tsubai 	bmac_reset_chip(sc);
    307   1.1    tsubai 
    308   1.4    tsubai 	/* XXX */
    309   1.4    tsubai 	bmcr = bmac_mii_readreg((struct device *)sc, 0, MII_BMCR);
    310   1.4    tsubai 	bmcr &= ~BMCR_ISO;
    311   1.4    tsubai 	bmac_mii_writereg((struct device *)sc, 0, MII_BMCR, bmcr);
    312   1.4    tsubai 
    313   1.1    tsubai 	bmac_write_reg(sc, RXRST, RxResetValue);
    314   1.1    tsubai 	bmac_write_reg(sc, TXRST, TxResetBit);
    315   1.1    tsubai 
    316   1.1    tsubai 	/* Wait for reset completion. */
    317   1.4    tsubai 	for (i = 1000; i > 0; i -= 10) {
    318   1.4    tsubai 		if ((bmac_read_reg(sc, TXRST) & TxResetBit) == 0)
    319   1.4    tsubai 			break;
    320   1.4    tsubai 		delay(10);
    321   1.4    tsubai 	}
    322   1.4    tsubai 	if (i <= 0)
    323   1.4    tsubai 		printf("%s: reset timeout\n", ifp->if_xname);
    324   1.1    tsubai 
    325   1.1    tsubai 	if (! (sc->sc_flags & BMAC_BMACPLUS))
    326   1.1    tsubai 		bmac_set_bits(sc, XCVRIF, ClkBit|SerialMode|COLActiveLow);
    327   1.1    tsubai 
    328  1.24    kleink 	if ((mfpvr() >> 16) == MPC601)
    329  1.24    kleink 		tb = mfrtcl();
    330  1.24    kleink 	else
    331  1.24    kleink 		tb = mftbl();
    332   1.1    tsubai 	bmac_write_reg(sc, RSEED, tb);
    333   1.1    tsubai 	bmac_set_bits(sc, XIFC, TxOutputEnable);
    334   1.1    tsubai 	bmac_read_reg(sc, PAREG);
    335   1.1    tsubai 
    336   1.1    tsubai 	/* Reset various counters. */
    337   1.1    tsubai 	bmac_write_reg(sc, NCCNT, 0);
    338   1.1    tsubai 	bmac_write_reg(sc, NTCNT, 0);
    339   1.1    tsubai 	bmac_write_reg(sc, EXCNT, 0);
    340   1.1    tsubai 	bmac_write_reg(sc, LTCNT, 0);
    341   1.1    tsubai 	bmac_write_reg(sc, FRCNT, 0);
    342   1.1    tsubai 	bmac_write_reg(sc, LECNT, 0);
    343   1.1    tsubai 	bmac_write_reg(sc, AECNT, 0);
    344   1.1    tsubai 	bmac_write_reg(sc, FECNT, 0);
    345   1.1    tsubai 	bmac_write_reg(sc, RXCV, 0);
    346   1.1    tsubai 
    347   1.1    tsubai 	/* Set tx fifo information. */
    348   1.1    tsubai 	bmac_write_reg(sc, TXTH, 4);	/* 4 octets before tx starts */
    349   1.1    tsubai 
    350   1.1    tsubai 	bmac_write_reg(sc, TXFIFOCSR, 0);
    351   1.1    tsubai 	bmac_write_reg(sc, TXFIFOCSR, TxFIFOEnable);
    352   1.1    tsubai 
    353   1.1    tsubai 	/* Set rx fifo information. */
    354   1.1    tsubai 	bmac_write_reg(sc, RXFIFOCSR, 0);
    355   1.1    tsubai 	bmac_write_reg(sc, RXFIFOCSR, RxFIFOEnable);
    356   1.1    tsubai 
    357   1.1    tsubai 	/* Clear status register. */
    358   1.1    tsubai 	bmac_read_reg(sc, STATUS);
    359   1.1    tsubai 
    360   1.1    tsubai 	bmac_write_reg(sc, HASH3, 0);
    361   1.1    tsubai 	bmac_write_reg(sc, HASH2, 0);
    362   1.1    tsubai 	bmac_write_reg(sc, HASH1, 0);
    363   1.1    tsubai 	bmac_write_reg(sc, HASH0, 0);
    364   1.1    tsubai 
    365   1.1    tsubai 	/* Set MAC address. */
    366   1.1    tsubai 	p = (u_short *)sc->sc_enaddr;
    367   1.1    tsubai 	bmac_write_reg(sc, MADD0, *p++);
    368   1.1    tsubai 	bmac_write_reg(sc, MADD1, *p++);
    369   1.1    tsubai 	bmac_write_reg(sc, MADD2, *p);
    370   1.1    tsubai 
    371   1.1    tsubai 	bmac_write_reg(sc, RXCFG,
    372   1.1    tsubai 		RxCRCEnable | RxHashFilterEnable | RxRejectOwnPackets);
    373   1.1    tsubai 
    374   1.1    tsubai 	if (ifp->if_flags & IFF_PROMISC)
    375   1.1    tsubai 		bmac_set_bits(sc, RXCFG, RxPromiscEnable);
    376   1.1    tsubai 
    377   1.1    tsubai 	bmac_init_dma(sc);
    378   1.1    tsubai 
    379   1.1    tsubai 	/* Enable TX/RX */
    380   1.1    tsubai 	bmac_set_bits(sc, RXCFG, RxMACEnable);
    381   1.1    tsubai 	bmac_set_bits(sc, TXCFG, TxMACEnable);
    382   1.1    tsubai 
    383   1.1    tsubai 	bmac_write_reg(sc, INTDISABLE, NormalIntEvents);
    384   1.1    tsubai 
    385   1.1    tsubai 	ifp->if_flags |= IFF_RUNNING;
    386   1.1    tsubai 	ifp->if_flags &= ~IFF_OACTIVE;
    387   1.1    tsubai 	ifp->if_timer = 0;
    388   1.1    tsubai 
    389   1.1    tsubai 	data = sc->sc_txbuf;
    390   1.1    tsubai 	eh = (struct ether_header *)data;
    391   1.1    tsubai 
    392  1.16       wiz 	memset(data, 0, sizeof(eh) + ETHERMIN);
    393  1.16       wiz 	memcpy(eh->ether_dhost, sc->sc_enaddr, ETHER_ADDR_LEN);
    394  1.16       wiz 	memcpy(eh->ether_shost, sc->sc_enaddr, ETHER_ADDR_LEN);
    395   1.1    tsubai 	bmac_transmit_packet(sc, data, sizeof(eh) + ETHERMIN);
    396   1.1    tsubai 
    397   1.1    tsubai 	bmac_start(ifp);
    398   1.4    tsubai 
    399   1.7   thorpej 	callout_reset(&sc->sc_tick_ch, hz, bmac_mii_tick, sc);
    400   1.1    tsubai }
    401   1.1    tsubai 
    402   1.1    tsubai void
    403   1.1    tsubai bmac_init_dma(sc)
    404   1.1    tsubai 	struct bmac_softc *sc;
    405   1.1    tsubai {
    406   1.1    tsubai 	dbdma_command_t *cmd = sc->sc_rxcmd;
    407   1.1    tsubai 	int i;
    408   1.1    tsubai 
    409   1.1    tsubai 	dbdma_reset(sc->sc_txdma);
    410   1.1    tsubai 	dbdma_reset(sc->sc_rxdma);
    411   1.1    tsubai 
    412  1.16       wiz 	memset(sc->sc_txcmd, 0, BMAC_TXBUFS * sizeof(dbdma_command_t));
    413  1.16       wiz 	memset(sc->sc_rxcmd, 0, (BMAC_RXBUFS + 1) * sizeof(dbdma_command_t));
    414   1.1    tsubai 
    415   1.1    tsubai 	for (i = 0; i < BMAC_RXBUFS; i++) {
    416   1.1    tsubai 		DBDMA_BUILD(cmd, DBDMA_CMD_IN_LAST, 0, BMAC_BUFLEN,
    417   1.8    tsubai 			vtophys((vaddr_t)sc->sc_rxbuf + BMAC_BUFLEN * i),
    418   1.1    tsubai 			DBDMA_INT_ALWAYS, DBDMA_WAIT_NEVER, DBDMA_BRANCH_NEVER);
    419   1.1    tsubai 		cmd++;
    420   1.1    tsubai 	}
    421   1.1    tsubai 	DBDMA_BUILD(cmd, DBDMA_CMD_NOP, 0, 0, 0,
    422   1.1    tsubai 		DBDMA_INT_NEVER, DBDMA_WAIT_NEVER, DBDMA_BRANCH_ALWAYS);
    423  1.34   garbled 	out32rb(&cmd->d_cmddep, vtophys((vaddr_t)sc->sc_rxcmd));
    424   1.1    tsubai 
    425   1.1    tsubai 	sc->sc_rxlast = 0;
    426   1.1    tsubai 
    427   1.1    tsubai 	dbdma_start(sc->sc_rxdma, sc->sc_rxcmd);
    428   1.1    tsubai }
    429   1.1    tsubai 
    430   1.1    tsubai int
    431   1.1    tsubai bmac_intr(v)
    432   1.1    tsubai 	void *v;
    433   1.1    tsubai {
    434   1.1    tsubai 	struct bmac_softc *sc = v;
    435   1.1    tsubai 	int stat;
    436   1.1    tsubai 
    437   1.1    tsubai 	stat = bmac_read_reg(sc, STATUS);
    438   1.1    tsubai 	if (stat == 0)
    439   1.1    tsubai 		return 0;
    440   1.1    tsubai 
    441   1.1    tsubai #ifdef BMAC_DEBUG
    442   1.1    tsubai 	printf("bmac_intr status = 0x%x\n", stat);
    443   1.1    tsubai #endif
    444   1.1    tsubai 
    445   1.1    tsubai 	if (stat & IntFrameSent) {
    446   1.1    tsubai 		sc->sc_if.if_flags &= ~IFF_OACTIVE;
    447   1.1    tsubai 		sc->sc_if.if_timer = 0;
    448   1.1    tsubai 		sc->sc_if.if_opackets++;
    449   1.1    tsubai 		bmac_start(&sc->sc_if);
    450   1.1    tsubai 	}
    451   1.1    tsubai 
    452   1.1    tsubai 	/* XXX should do more! */
    453   1.1    tsubai 
    454   1.1    tsubai 	return 1;
    455   1.1    tsubai }
    456   1.1    tsubai 
    457   1.1    tsubai int
    458   1.1    tsubai bmac_rint(v)
    459   1.1    tsubai 	void *v;
    460   1.1    tsubai {
    461   1.1    tsubai 	struct bmac_softc *sc = v;
    462   1.1    tsubai 	struct ifnet *ifp = &sc->sc_if;
    463   1.1    tsubai 	struct mbuf *m;
    464   1.1    tsubai 	dbdma_command_t *cmd;
    465   1.1    tsubai 	int status, resid, count, datalen;
    466   1.1    tsubai 	int i, n;
    467   1.1    tsubai 	void *data;
    468   1.1    tsubai 
    469   1.1    tsubai 	i = sc->sc_rxlast;
    470   1.1    tsubai 	for (n = 0; n < BMAC_RXBUFS; n++, i++) {
    471   1.1    tsubai 		if (i == BMAC_RXBUFS)
    472   1.1    tsubai 			i = 0;
    473   1.1    tsubai 		cmd = &sc->sc_rxcmd[i];
    474  1.34   garbled 		status = in16rb(&cmd->d_status);
    475  1.34   garbled 		resid = in16rb(&cmd->d_resid);
    476   1.1    tsubai 
    477   1.1    tsubai #ifdef BMAC_DEBUG
    478   1.1    tsubai 		if (status != 0 && status != 0x8440 && status != 0x9440)
    479   1.1    tsubai 			printf("bmac_rint status = 0x%x\n", status);
    480   1.1    tsubai #endif
    481   1.1    tsubai 
    482   1.1    tsubai 		if ((status & DBDMA_CNTRL_ACTIVE) == 0)	/* 0x9440 | 0x8440 */
    483   1.1    tsubai 			continue;
    484  1.34   garbled 		count = in16rb(&cmd->d_count);
    485  1.12    tsubai 		datalen = count - resid - 2;		/* 2 == framelen */
    486   1.1    tsubai 		if (datalen < sizeof(struct ether_header)) {
    487   1.1    tsubai 			printf("%s: short packet len = %d\n",
    488   1.1    tsubai 				ifp->if_xname, datalen);
    489   1.1    tsubai 			goto next;
    490   1.1    tsubai 		}
    491   1.1    tsubai 		DBDMA_BUILD_CMD(cmd, DBDMA_CMD_STOP, 0, 0, 0, 0);
    492  1.31  macallan 		data = (char *)sc->sc_rxbuf + BMAC_BUFLEN * i;
    493  1.12    tsubai 
    494  1.12    tsubai 		/* XXX Sometimes bmac reads one extra byte. */
    495  1.12    tsubai 		if (datalen == ETHER_MAX_LEN + 1)
    496  1.12    tsubai 			datalen--;
    497  1.25   thorpej 
    498  1.25   thorpej 		/* Trim the CRC. */
    499  1.25   thorpej 		datalen -= ETHER_CRC_LEN;
    500  1.25   thorpej 
    501   1.1    tsubai 		m = bmac_get(sc, data, datalen);
    502   1.1    tsubai 		if (m == NULL) {
    503   1.1    tsubai 			ifp->if_ierrors++;
    504   1.1    tsubai 			goto next;
    505   1.1    tsubai 		}
    506   1.1    tsubai 
    507   1.1    tsubai #if NBPFILTER > 0
    508   1.1    tsubai 		/*
    509   1.1    tsubai 		 * Check if there's a BPF listener on this interface.
    510   1.1    tsubai 		 * If so, hand off the raw packet to BPF.
    511   1.1    tsubai 		 */
    512   1.1    tsubai 		if (ifp->if_bpf)
    513   1.1    tsubai 			bpf_mtap(ifp->if_bpf, m);
    514   1.1    tsubai #endif
    515   1.2   thorpej 		(*ifp->if_input)(ifp, m);
    516   1.1    tsubai 		ifp->if_ipackets++;
    517   1.1    tsubai 
    518   1.1    tsubai next:
    519   1.1    tsubai 		DBDMA_BUILD_CMD(cmd, DBDMA_CMD_IN_LAST, 0, DBDMA_INT_ALWAYS,
    520   1.1    tsubai 			DBDMA_WAIT_NEVER, DBDMA_BRANCH_NEVER);
    521   1.1    tsubai 
    522   1.1    tsubai 		cmd->d_status = 0;
    523   1.1    tsubai 		cmd->d_resid = 0;
    524   1.1    tsubai 		sc->sc_rxlast = i + 1;
    525   1.1    tsubai 	}
    526  1.36    dyoung 	ether_mediachange(ifp);
    527  1.29     jklos 
    528   1.1    tsubai 	dbdma_continue(sc->sc_rxdma);
    529   1.1    tsubai 
    530   1.1    tsubai 	return 1;
    531   1.1    tsubai }
    532   1.1    tsubai 
    533   1.1    tsubai void
    534   1.1    tsubai bmac_reset(sc)
    535   1.1    tsubai 	struct bmac_softc *sc;
    536   1.1    tsubai {
    537   1.1    tsubai 	int s;
    538   1.1    tsubai 
    539   1.1    tsubai 	s = splnet();
    540   1.1    tsubai 	bmac_init(sc);
    541   1.1    tsubai 	splx(s);
    542   1.1    tsubai }
    543   1.1    tsubai 
    544   1.1    tsubai void
    545   1.1    tsubai bmac_stop(sc)
    546   1.1    tsubai 	struct bmac_softc *sc;
    547   1.1    tsubai {
    548   1.1    tsubai 	struct ifnet *ifp = &sc->sc_if;
    549   1.1    tsubai 	int s;
    550   1.1    tsubai 
    551   1.1    tsubai 	s = splnet();
    552   1.1    tsubai 
    553   1.7   thorpej 	callout_stop(&sc->sc_tick_ch);
    554   1.4    tsubai 	mii_down(&sc->sc_mii);
    555   1.4    tsubai 
    556   1.1    tsubai 	/* Disable TX/RX. */
    557   1.1    tsubai 	bmac_reset_bits(sc, TXCFG, TxMACEnable);
    558   1.1    tsubai 	bmac_reset_bits(sc, RXCFG, RxMACEnable);
    559   1.1    tsubai 
    560   1.1    tsubai 	/* Disable all interrupts. */
    561   1.1    tsubai 	bmac_write_reg(sc, INTDISABLE, NoEventsMask);
    562   1.1    tsubai 
    563   1.1    tsubai 	dbdma_stop(sc->sc_txdma);
    564   1.1    tsubai 	dbdma_stop(sc->sc_rxdma);
    565   1.1    tsubai 
    566   1.1    tsubai 	ifp->if_flags &= ~(IFF_UP | IFF_RUNNING);
    567   1.1    tsubai 	ifp->if_timer = 0;
    568   1.1    tsubai 
    569   1.1    tsubai 	splx(s);
    570   1.1    tsubai }
    571   1.1    tsubai 
    572   1.1    tsubai void
    573   1.1    tsubai bmac_start(ifp)
    574   1.1    tsubai 	struct ifnet *ifp;
    575   1.1    tsubai {
    576   1.1    tsubai 	struct bmac_softc *sc = ifp->if_softc;
    577   1.1    tsubai 	struct mbuf *m;
    578   1.1    tsubai 	int tlen;
    579   1.1    tsubai 
    580   1.1    tsubai 	if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
    581   1.1    tsubai 		return;
    582   1.1    tsubai 
    583   1.1    tsubai 	while (1) {
    584   1.1    tsubai 		if (ifp->if_flags & IFF_OACTIVE)
    585   1.1    tsubai 			return;
    586   1.1    tsubai 
    587  1.17    itojun 		IFQ_DEQUEUE(&ifp->if_snd, m);
    588   1.1    tsubai 		if (m == 0)
    589   1.1    tsubai 			break;
    590   1.1    tsubai #if NBPFILTER > 0
    591   1.1    tsubai 		/*
    592   1.1    tsubai 		 * If BPF is listening on this interface, let it see the
    593   1.1    tsubai 		 * packet before we commit it to the wire.
    594   1.1    tsubai 		 */
    595   1.1    tsubai 		if (ifp->if_bpf)
    596   1.1    tsubai 			bpf_mtap(ifp->if_bpf, m);
    597   1.1    tsubai #endif
    598   1.1    tsubai 
    599   1.1    tsubai 		ifp->if_flags |= IFF_OACTIVE;
    600   1.1    tsubai 		tlen = bmac_put(sc, sc->sc_txbuf, m);
    601   1.1    tsubai 
    602   1.1    tsubai 		/* 5 seconds to watch for failing to transmit */
    603   1.1    tsubai 		ifp->if_timer = 5;
    604   1.1    tsubai 		ifp->if_opackets++;		/* # of pkts */
    605   1.1    tsubai 
    606   1.1    tsubai 		bmac_transmit_packet(sc, sc->sc_txbuf, tlen);
    607   1.1    tsubai 	}
    608   1.1    tsubai }
    609   1.1    tsubai 
    610   1.1    tsubai void
    611   1.1    tsubai bmac_transmit_packet(sc, buff, len)
    612   1.1    tsubai 	struct bmac_softc *sc;
    613   1.1    tsubai 	void *buff;
    614   1.1    tsubai 	int len;
    615   1.1    tsubai {
    616   1.1    tsubai 	dbdma_command_t *cmd = sc->sc_txcmd;
    617   1.1    tsubai 	vaddr_t va = (vaddr_t)buff;
    618   1.1    tsubai 
    619   1.1    tsubai #ifdef BMAC_DEBUG
    620   1.1    tsubai 	if (vtophys(va) + len - 1 != vtophys(va + len - 1))
    621   1.1    tsubai 		panic("bmac_transmit_packet");
    622   1.1    tsubai #endif
    623   1.1    tsubai 
    624   1.1    tsubai 	DBDMA_BUILD(cmd, DBDMA_CMD_OUT_LAST, 0, len, vtophys(va),
    625   1.1    tsubai 		DBDMA_INT_NEVER, DBDMA_WAIT_NEVER, DBDMA_BRANCH_NEVER);
    626   1.1    tsubai 	cmd++;
    627   1.1    tsubai 	DBDMA_BUILD(cmd, DBDMA_CMD_STOP, 0, 0, 0,
    628   1.1    tsubai 		DBDMA_INT_ALWAYS, DBDMA_WAIT_NEVER, DBDMA_BRANCH_NEVER);
    629   1.1    tsubai 
    630   1.1    tsubai 	dbdma_start(sc->sc_txdma, sc->sc_txcmd);
    631   1.1    tsubai }
    632   1.1    tsubai 
    633   1.1    tsubai int
    634   1.1    tsubai bmac_put(sc, buff, m)
    635   1.1    tsubai 	struct bmac_softc *sc;
    636  1.30  christos 	void *buff;
    637   1.1    tsubai 	struct mbuf *m;
    638   1.1    tsubai {
    639   1.1    tsubai 	struct mbuf *n;
    640   1.1    tsubai 	int len, tlen = 0;
    641   1.1    tsubai 
    642   1.1    tsubai 	for (; m; m = n) {
    643   1.1    tsubai 		len = m->m_len;
    644   1.1    tsubai 		if (len == 0) {
    645   1.1    tsubai 			MFREE(m, n);
    646   1.1    tsubai 			continue;
    647   1.1    tsubai 		}
    648  1.30  christos 		memcpy(buff, mtod(m, void *), len);
    649  1.31  macallan 		buff = (char *)buff + len;
    650   1.1    tsubai 		tlen += len;
    651   1.1    tsubai 		MFREE(m, n);
    652   1.1    tsubai 	}
    653  1.20   thorpej 	if (tlen > PAGE_SIZE)
    654   1.1    tsubai 		panic("%s: putpacket packet overflow", sc->sc_dev.dv_xname);
    655   1.1    tsubai 
    656   1.1    tsubai 	return tlen;
    657   1.1    tsubai }
    658   1.1    tsubai 
    659   1.1    tsubai struct mbuf *
    660   1.1    tsubai bmac_get(sc, pkt, totlen)
    661   1.1    tsubai 	struct bmac_softc *sc;
    662  1.30  christos 	void *pkt;
    663   1.1    tsubai 	int totlen;
    664   1.1    tsubai {
    665   1.1    tsubai 	struct mbuf *m;
    666   1.1    tsubai 	struct mbuf *top, **mp;
    667   1.1    tsubai 	int len;
    668   1.1    tsubai 
    669   1.1    tsubai 	MGETHDR(m, M_DONTWAIT, MT_DATA);
    670   1.1    tsubai 	if (m == 0)
    671   1.1    tsubai 		return 0;
    672   1.1    tsubai 	m->m_pkthdr.rcvif = &sc->sc_if;
    673   1.1    tsubai 	m->m_pkthdr.len = totlen;
    674   1.1    tsubai 	len = MHLEN;
    675   1.1    tsubai 	top = 0;
    676   1.1    tsubai 	mp = &top;
    677   1.1    tsubai 
    678   1.1    tsubai 	while (totlen > 0) {
    679   1.1    tsubai 		if (top) {
    680   1.1    tsubai 			MGET(m, M_DONTWAIT, MT_DATA);
    681   1.1    tsubai 			if (m == 0) {
    682   1.1    tsubai 				m_freem(top);
    683   1.1    tsubai 				return 0;
    684   1.1    tsubai 			}
    685   1.1    tsubai 			len = MLEN;
    686   1.1    tsubai 		}
    687   1.1    tsubai 		if (totlen >= MINCLSIZE) {
    688   1.1    tsubai 			MCLGET(m, M_DONTWAIT);
    689   1.1    tsubai 			if ((m->m_flags & M_EXT) == 0) {
    690   1.1    tsubai 				m_free(m);
    691   1.1    tsubai 				m_freem(top);
    692   1.1    tsubai 				return 0;
    693   1.1    tsubai 			}
    694   1.1    tsubai 			len = MCLBYTES;
    695   1.1    tsubai 		}
    696   1.1    tsubai 		m->m_len = len = min(totlen, len);
    697  1.30  christos 		memcpy(mtod(m, void *), pkt, len);
    698  1.31  macallan 		pkt = (char *)pkt + len;
    699   1.1    tsubai 		totlen -= len;
    700   1.1    tsubai 		*mp = m;
    701   1.1    tsubai 		mp = &m->m_next;
    702   1.1    tsubai 	}
    703   1.1    tsubai 
    704   1.1    tsubai 	return top;
    705   1.1    tsubai }
    706   1.1    tsubai 
    707   1.1    tsubai void
    708   1.1    tsubai bmac_watchdog(ifp)
    709   1.1    tsubai 	struct ifnet *ifp;
    710   1.1    tsubai {
    711   1.1    tsubai 	struct bmac_softc *sc = ifp->if_softc;
    712   1.1    tsubai 
    713   1.1    tsubai 	bmac_reset_bits(sc, RXCFG, RxMACEnable);
    714   1.1    tsubai 	bmac_reset_bits(sc, TXCFG, TxMACEnable);
    715   1.1    tsubai 
    716   1.1    tsubai 	printf("%s: device timeout\n", ifp->if_xname);
    717   1.1    tsubai 	ifp->if_oerrors++;
    718   1.1    tsubai 
    719   1.1    tsubai 	bmac_reset(sc);
    720   1.1    tsubai }
    721   1.1    tsubai 
    722   1.1    tsubai int
    723   1.1    tsubai bmac_ioctl(ifp, cmd, data)
    724   1.1    tsubai 	struct ifnet *ifp;
    725   1.1    tsubai 	u_long cmd;
    726  1.30  christos 	void *data;
    727   1.1    tsubai {
    728   1.1    tsubai 	struct bmac_softc *sc = ifp->if_softc;
    729   1.1    tsubai 	struct ifaddr *ifa = (struct ifaddr *)data;
    730   1.1    tsubai 	int s, error = 0;
    731   1.1    tsubai 
    732   1.1    tsubai 	s = splnet();
    733   1.1    tsubai 
    734   1.1    tsubai 	switch (cmd) {
    735   1.1    tsubai 
    736   1.1    tsubai 	case SIOCSIFADDR:
    737   1.1    tsubai 		ifp->if_flags |= IFF_UP;
    738   1.1    tsubai 
    739   1.1    tsubai 		switch (ifa->ifa_addr->sa_family) {
    740   1.1    tsubai #ifdef INET
    741   1.1    tsubai 		case AF_INET:
    742   1.1    tsubai 			bmac_init(sc);
    743   1.1    tsubai 			arp_ifinit(ifp, ifa);
    744   1.1    tsubai 			break;
    745   1.1    tsubai #endif
    746   1.1    tsubai 		default:
    747   1.1    tsubai 			bmac_init(sc);
    748   1.1    tsubai 			break;
    749   1.1    tsubai 		}
    750   1.1    tsubai 		break;
    751   1.1    tsubai 
    752   1.1    tsubai 	case SIOCSIFFLAGS:
    753   1.1    tsubai 		if ((ifp->if_flags & IFF_UP) == 0 &&
    754   1.1    tsubai 		    (ifp->if_flags & IFF_RUNNING) != 0) {
    755   1.1    tsubai 			/*
    756   1.1    tsubai 			 * If interface is marked down and it is running, then
    757   1.1    tsubai 			 * stop it.
    758   1.1    tsubai 			 */
    759   1.1    tsubai 			bmac_stop(sc);
    760   1.1    tsubai 			ifp->if_flags &= ~IFF_RUNNING;
    761   1.1    tsubai 		} else if ((ifp->if_flags & IFF_UP) != 0 &&
    762   1.1    tsubai 		    (ifp->if_flags & IFF_RUNNING) == 0) {
    763   1.1    tsubai 			/*
    764   1.1    tsubai 			 * If interface is marked up and it is stopped, then
    765   1.1    tsubai 			 * start it.
    766   1.1    tsubai 			 */
    767   1.1    tsubai 			bmac_init(sc);
    768   1.1    tsubai 		} else {
    769   1.1    tsubai 			/*
    770   1.1    tsubai 			 * Reset the interface to pick up changes in any other
    771   1.1    tsubai 			 * flags that affect hardware registers.
    772   1.1    tsubai 			 */
    773   1.1    tsubai 			/*bmac_stop(sc);*/
    774   1.1    tsubai 			bmac_init(sc);
    775   1.1    tsubai 		}
    776   1.1    tsubai #ifdef BMAC_DEBUG
    777   1.1    tsubai 		if (ifp->if_flags & IFF_DEBUG)
    778   1.4    tsubai 			sc->sc_flags |= BMAC_DEBUGFLAG;
    779   1.1    tsubai #endif
    780   1.1    tsubai 		break;
    781   1.1    tsubai 
    782   1.1    tsubai 	case SIOCADDMULTI:
    783   1.1    tsubai 	case SIOCDELMULTI:
    784  1.36    dyoung 	case SIOCGIFMEDIA:
    785  1.36    dyoung 	case SIOCSIFMEDIA:
    786  1.33    dyoung 		if ((error = ether_ioctl(ifp, cmd, data)) == ENETRESET) {
    787   1.1    tsubai 			/*
    788   1.1    tsubai 			 * Multicast list has changed; set the hardware filter
    789   1.1    tsubai 			 * accordingly.
    790   1.1    tsubai 			 */
    791  1.23   thorpej 			if (ifp->if_flags & IFF_RUNNING) {
    792  1.23   thorpej 				bmac_init(sc);
    793  1.23   thorpej 				bmac_setladrf(sc);
    794  1.23   thorpej 			}
    795   1.1    tsubai 			error = 0;
    796   1.1    tsubai 		}
    797   1.1    tsubai 		break;
    798   1.1    tsubai 	default:
    799   1.1    tsubai 		error = EINVAL;
    800   1.1    tsubai 	}
    801   1.1    tsubai 
    802   1.1    tsubai 	splx(s);
    803   1.1    tsubai 	return error;
    804   1.1    tsubai }
    805   1.1    tsubai 
    806   1.1    tsubai /*
    807   1.1    tsubai  * Set up the logical address filter.
    808   1.1    tsubai  */
    809   1.1    tsubai void
    810   1.1    tsubai bmac_setladrf(sc)
    811   1.1    tsubai 	struct bmac_softc *sc;
    812   1.1    tsubai {
    813   1.1    tsubai 	struct ifnet *ifp = &sc->sc_if;
    814   1.1    tsubai 	struct ether_multi *enm;
    815   1.1    tsubai 	struct ether_multistep step;
    816   1.1    tsubai 	u_int32_t crc;
    817   1.1    tsubai 	u_int16_t hash[4];
    818  1.11    tsubai 	int x;
    819   1.1    tsubai 
    820   1.1    tsubai 	/*
    821   1.1    tsubai 	 * Set up multicast address filter by passing all multicast addresses
    822   1.1    tsubai 	 * through a crc generator, and then using the high order 6 bits as an
    823   1.1    tsubai 	 * index into the 64 bit logical address filter.  The high order bit
    824   1.1    tsubai 	 * selects the word, while the rest of the bits select the bit within
    825   1.1    tsubai 	 * the word.
    826   1.1    tsubai 	 */
    827   1.1    tsubai 
    828   1.1    tsubai 	if (ifp->if_flags & IFF_PROMISC) {
    829   1.1    tsubai 		bmac_set_bits(sc, RXCFG, RxPromiscEnable);
    830   1.9    tsubai 		return;
    831   1.9    tsubai 	}
    832   1.9    tsubai 
    833   1.9    tsubai 	if (ifp->if_flags & IFF_ALLMULTI) {
    834   1.9    tsubai 		hash[3] = hash[2] = hash[1] = hash[0] = 0xffff;
    835   1.9    tsubai 		goto chipit;
    836   1.1    tsubai 	}
    837   1.1    tsubai 
    838   1.1    tsubai 	hash[3] = hash[2] = hash[1] = hash[0] = 0;
    839   1.9    tsubai 
    840   1.1    tsubai 	ETHER_FIRST_MULTI(step, &sc->sc_ethercom, enm);
    841   1.1    tsubai 	while (enm != NULL) {
    842  1.16       wiz 		if (memcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
    843   1.1    tsubai 			/*
    844   1.1    tsubai 			 * We must listen to a range of multicast addresses.
    845   1.1    tsubai 			 * For now, just accept all multicasts, rather than
    846   1.1    tsubai 			 * trying to set only those filter bits needed to match
    847   1.1    tsubai 			 * the range.  (At this time, the only use of address
    848   1.1    tsubai 			 * ranges is for IP multicast routing, for which the
    849   1.1    tsubai 			 * range is big enough to require all bits set.)
    850   1.1    tsubai 			 */
    851   1.9    tsubai 			hash[3] = hash[2] = hash[1] = hash[0] = 0xffff;
    852   1.9    tsubai 			ifp->if_flags |= IFF_ALLMULTI;
    853   1.9    tsubai 			goto chipit;
    854   1.1    tsubai 		}
    855   1.1    tsubai 
    856   1.9    tsubai 		crc = ether_crc32_le(enm->enm_addrlo, ETHER_ADDR_LEN);
    857   1.1    tsubai 
    858   1.1    tsubai 		/* Just want the 6 most significant bits. */
    859   1.1    tsubai 		crc >>= 26;
    860   1.1    tsubai 
    861   1.1    tsubai 		/* Set the corresponding bit in the filter. */
    862   1.1    tsubai 		hash[crc >> 4] |= 1 << (crc & 0xf);
    863   1.1    tsubai 
    864   1.1    tsubai 		ETHER_NEXT_MULTI(step, enm);
    865   1.1    tsubai 	}
    866   1.9    tsubai 
    867   1.1    tsubai 	ifp->if_flags &= ~IFF_ALLMULTI;
    868   1.1    tsubai 
    869   1.9    tsubai chipit:
    870   1.9    tsubai 	bmac_write_reg(sc, HASH0, hash[0]);
    871   1.9    tsubai 	bmac_write_reg(sc, HASH1, hash[1]);
    872   1.9    tsubai 	bmac_write_reg(sc, HASH2, hash[2]);
    873   1.9    tsubai 	bmac_write_reg(sc, HASH3, hash[3]);
    874  1.11    tsubai 	x = bmac_read_reg(sc, RXCFG);
    875  1.11    tsubai 	x &= ~RxPromiscEnable;
    876  1.11    tsubai 	x |= RxHashFilterEnable;
    877  1.11    tsubai 	bmac_write_reg(sc, RXCFG, x);
    878   1.4    tsubai }
    879   1.4    tsubai 
    880   1.4    tsubai int
    881   1.4    tsubai bmac_mii_readreg(dev, phy, reg)
    882   1.4    tsubai 	struct device *dev;
    883   1.4    tsubai 	int phy, reg;
    884   1.4    tsubai {
    885   1.4    tsubai 	return mii_bitbang_readreg(dev, &bmac_mbo, phy, reg);
    886   1.4    tsubai }
    887   1.4    tsubai 
    888   1.4    tsubai void
    889   1.4    tsubai bmac_mii_writereg(dev, phy, reg, val)
    890   1.4    tsubai 	struct device *dev;
    891   1.4    tsubai 	int phy, reg, val;
    892   1.4    tsubai {
    893   1.4    tsubai 	mii_bitbang_writereg(dev, &bmac_mbo, phy, reg, val);
    894   1.4    tsubai }
    895   1.4    tsubai 
    896   1.4    tsubai u_int32_t
    897   1.4    tsubai bmac_mbo_read(dev)
    898   1.4    tsubai 	struct device *dev;
    899   1.4    tsubai {
    900   1.4    tsubai 	struct bmac_softc *sc = (void *)dev;
    901   1.4    tsubai 
    902   1.4    tsubai 	return bmac_read_reg(sc, MIFCSR);
    903   1.4    tsubai }
    904   1.4    tsubai 
    905   1.4    tsubai void
    906   1.4    tsubai bmac_mbo_write(dev, val)
    907   1.4    tsubai 	struct device *dev;
    908   1.4    tsubai 	u_int32_t val;
    909   1.4    tsubai {
    910   1.4    tsubai 	struct bmac_softc *sc = (void *)dev;
    911   1.4    tsubai 
    912   1.4    tsubai 	bmac_write_reg(sc, MIFCSR, val);
    913   1.4    tsubai }
    914   1.4    tsubai 
    915   1.4    tsubai void
    916   1.4    tsubai bmac_mii_statchg(dev)
    917   1.4    tsubai 	struct device *dev;
    918   1.4    tsubai {
    919   1.4    tsubai 	struct bmac_softc *sc = (void *)dev;
    920   1.4    tsubai 	int x;
    921   1.4    tsubai 
    922   1.4    tsubai 	/* Update duplex mode in TX configuration */
    923   1.4    tsubai 	x = bmac_read_reg(sc, TXCFG);
    924   1.4    tsubai 	if ((IFM_OPTIONS(sc->sc_mii.mii_media_active) & IFM_FDX) != 0)
    925   1.4    tsubai 		x |= TxFullDuplex;
    926   1.4    tsubai 	else
    927   1.4    tsubai 		x &= ~TxFullDuplex;
    928   1.4    tsubai 	bmac_write_reg(sc, TXCFG, x);
    929   1.4    tsubai 
    930   1.4    tsubai #ifdef BMAC_DEBUG
    931   1.4    tsubai 	printf("bmac_mii_statchg 0x%x\n",
    932   1.4    tsubai 		IFM_OPTIONS(sc->sc_mii.mii_media_active));
    933   1.4    tsubai #endif
    934   1.4    tsubai }
    935   1.4    tsubai 
    936   1.4    tsubai void
    937   1.4    tsubai bmac_mii_tick(v)
    938   1.4    tsubai 	void *v;
    939   1.4    tsubai {
    940   1.4    tsubai 	struct bmac_softc *sc = v;
    941   1.4    tsubai 	int s;
    942   1.4    tsubai 
    943   1.4    tsubai 	s = splnet();
    944   1.4    tsubai 	mii_tick(&sc->sc_mii);
    945   1.4    tsubai 	splx(s);
    946   1.4    tsubai 
    947   1.7   thorpej 	callout_reset(&sc->sc_tick_ch, hz, bmac_mii_tick, sc);
    948   1.1    tsubai }
    949