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