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