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