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