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