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