Home | History | Annotate | Line # | Download | only in ic
smc90cx6.c revision 1.30
      1 /*	$NetBSD: smc90cx6.c,v 1.30 1999/05/18 23:55:44 thorpej Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 1994, 1995, 1998 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Ignatios Souvatzis.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *        This product includes software developed by the NetBSD
     21  *        Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 /*
     40  * Chip core driver for the SMC90c26 / SMC90c56 (and SMC90c66 in '56
     41  * compatibility mode) boards
     42  */
     43 
     44 #define BAHSOFTCOPY /**/
     45 #define BAHRETRANSMIT /**/
     46 
     47 #include "opt_inet.h"
     48 #include "bpfilter.h"
     49 
     50 #include <sys/param.h>
     51 #include <sys/systm.h>
     52 #include <sys/mbuf.h>
     53 #include <sys/buf.h>
     54 #include <sys/device.h>
     55 #include <sys/protosw.h>
     56 #include <sys/socket.h>
     57 #include <sys/syslog.h>
     58 #include <sys/ioctl.h>
     59 #include <sys/errno.h>
     60 
     61 #include <net/if.h>
     62 #include <net/if_dl.h>
     63 #include <net/if_types.h>
     64 #include <net/if_arc.h>
     65 
     66 #ifdef INET
     67 #include <netinet/in.h>
     68 #include <netinet/in_systm.h>
     69 #include <netinet/in_var.h>
     70 #include <netinet/ip.h>
     71 #include <netinet/if_inarp.h>
     72 #endif
     73 
     74 #if NBPFILTER > 0
     75 #include <net/bpf.h>
     76 #include <net/bpfdesc.h>
     77 #endif
     78 
     79 #include <sys/kernel.h>
     80 #include <machine/bus.h>
     81 #include <machine/cpu.h>
     82 #include <machine/intr.h>
     83 #include <machine/mtpr.h>
     84 
     85 #include <dev/ic/smc90cx6reg.h>
     86 #include <dev/ic/smc90cx6var.h>
     87 
     88 /* these should be elsewhere */
     89 
     90 #define ARC_MIN_LEN 1
     91 #define ARC_MIN_FORBID_LEN 254
     92 #define ARC_MAX_FORBID_LEN 256
     93 #define ARC_MAX_LEN 508
     94 #define ARC_ADDR_LEN 1
     95 
     96 /* for watchdog timer. This should be more than enough. */
     97 #define ARCTIMEOUT (5*IFNET_SLOWHZ)
     98 
     99 /*
    100  * This currently uses 2 bufs for tx, 2 for rx
    101  *
    102  * New rx protocol:
    103  *
    104  * rx has a fillcount variable. If fillcount > (NRXBUF-1),
    105  * rx can be switched off from rx hard int.
    106  * Else rx is restarted on the other receiver.
    107  * rx soft int counts down. if it is == (NRXBUF-1), it restarts
    108  * the receiver.
    109  * To ensure packet ordering (we need that for 1201 later), we have a counter
    110  * which is incremented modulo 256 on each receive and a per buffer
    111  * variable, which is set to the counter on filling. The soft int can
    112  * compare both values to determine the older packet.
    113  *
    114  * Transmit direction:
    115  *
    116  * bah_start checks tx_fillcount
    117  * case 2: return
    118  *
    119  * else fill tx_act ^ 1 && inc tx_fillcount
    120  *
    121  * check tx_fillcount again.
    122  * case 2: set IFF_OACTIVE to stop arc_output from filling us.
    123  * case 1: start tx
    124  *
    125  * tint clears IFF_OCATIVE, decrements and checks tx_fillcount
    126  * case 1: start tx on tx_act ^ 1, softcall bah_start
    127  * case 0: softcall bah_start
    128  *
    129  * #define fill(i) get mbuf && copy mbuf to chip(i)
    130  */
    131 
    132 void	bah_init __P((struct bah_softc *));
    133 void	bah_reset __P((struct bah_softc *));
    134 void	bah_stop __P((struct bah_softc *));
    135 void	bah_start __P((struct ifnet *));
    136 int	bahintr __P((void *));
    137 int	bah_ioctl __P((struct ifnet *, unsigned long, caddr_t));
    138 void	bah_watchdog __P((struct ifnet *));
    139 void	bah_srint __P((void *vsc));
    140 static	void bah_tint __P((struct bah_softc *, int));
    141 void	bah_reconwatch(void *);
    142 
    143 /* short notation */
    144 
    145 #define GETREG(off)	bus_space_read_1(bst_r, regs, (off))
    146 #define PUTREG(off, v)	bus_space_write_1(bst_r, regs, (off), (v))
    147 #define GETMEM(off)	bus_space_read_1(bst_m, mem, (off))
    148 #define PUTMEM(off, v)	bus_space_write_1(bst_m, mem, (off), (v))
    149 
    150 void
    151 bah_attach_subr(sc)
    152 	struct bah_softc *sc;
    153 {
    154 	struct ifnet *ifp = &sc->sc_arccom.ac_if;
    155 	int s;
    156 	u_int8_t linkaddress;
    157 
    158 	bus_space_tag_t bst_r = sc->sc_bst_r;
    159 	bus_space_tag_t bst_m = sc->sc_bst_m;
    160 	bus_space_handle_t regs = sc->sc_regs;
    161 	bus_space_handle_t mem = sc->sc_mem;
    162 
    163 #if (defined(BAH_DEBUG) && (BAH_DEBUG > 2))
    164 	printf("\n%s: attach(0x%x, 0x%x, 0x%x)\n",
    165 	    sc->sc_dev.dv_xname, parent, self, aux);
    166 #endif
    167 	s = splhigh();
    168 
    169 	/*
    170 	 * read the arcnet address from the board
    171 	 */
    172 
    173 	(*sc->sc_reset)(sc, 1);
    174 
    175 	do {
    176 		delay(200);
    177 	} while (!(GETREG(BAHSTAT) & BAH_POR));
    178 
    179 	linkaddress = GETMEM(BAHMACOFF);
    180 
    181 	printf(": link addr 0x%02x(%d)\n", linkaddress, linkaddress);
    182 
    183 	/* clear the int mask... */
    184 
    185 	sc->sc_intmask = 0;
    186 	PUTREG(BAHSTAT, 0);
    187 
    188 	PUTREG(BAHCMD, BAH_CONF(CONF_LONG));
    189 	PUTREG(BAHCMD, BAH_CLR(CLR_POR|CLR_RECONFIG));
    190 	sc->sc_recontime = sc->sc_reconcount = 0;
    191 
    192 	/* and reenable kernel int level */
    193 	splx(s);
    194 
    195 	/*
    196 	 * set interface to stopped condition (reset)
    197 	 */
    198 	bah_stop(sc);
    199 
    200 	bcopy(sc->sc_dev.dv_xname, ifp->if_xname, IFNAMSIZ);
    201 	ifp->if_softc = sc;
    202 	ifp->if_start = bah_start;
    203 	ifp->if_ioctl = bah_ioctl;
    204 	ifp->if_timer = 0;
    205 	ifp->if_watchdog  = bah_watchdog;
    206 
    207 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS;
    208 
    209 	ifp->if_mtu = ARCMTU;
    210 
    211 	if_attach(ifp);
    212 	arc_ifattach(ifp, linkaddress);
    213 
    214 #if NBPFILTER > 0
    215 	bpfattach(&ifp->if_bpf, ifp, DLT_ARCNET, ARC_HDRLEN);
    216 #endif
    217 #ifdef BAHSOFTCOPY
    218 	sc->sc_rxcookie = softintr_establish(IPL_SOFTNET, bah_srint, sc);
    219 	sc->sc_txcookie = softintr_establish(IPL_SOFTNET,
    220 		(void (*) __P((void *)))bah_start, ifp);
    221 #endif
    222 
    223 }
    224 
    225 /*
    226  * Initialize device
    227  *
    228  */
    229 void
    230 bah_init(sc)
    231 	struct bah_softc *sc;
    232 {
    233 	struct ifnet *ifp;
    234 	int s;
    235 
    236 	ifp = &sc->sc_arccom.ac_if;
    237 
    238 	if ((ifp->if_flags & IFF_RUNNING) == 0) {
    239 		s = splnet();
    240 		ifp->if_flags |= IFF_RUNNING;
    241 		bah_reset(sc);
    242 		bah_start(ifp);
    243 		splx(s);
    244 	}
    245 }
    246 
    247 /*
    248  * Reset the interface...
    249  *
    250  * this assumes that it is called inside a critical section...
    251  *
    252  */
    253 void
    254 bah_reset(sc)
    255 	struct bah_softc *sc;
    256 {
    257 	struct ifnet *ifp;
    258 	int linkaddress;
    259 
    260 	bus_space_tag_t bst_r = sc->sc_bst_r;
    261         bus_space_tag_t bst_m = sc->sc_bst_m;
    262 	bus_space_handle_t regs = sc->sc_regs;
    263 	bus_space_handle_t mem = sc->sc_mem;
    264 
    265 	ifp = &sc->sc_arccom.ac_if;
    266 
    267 #ifdef BAH_DEBUG
    268 	printf("%s: reset\n", sc->sc_dev.dv_xname);
    269 #endif
    270 	/* stop and restart hardware */
    271 
    272 	(*sc->sc_reset)(sc, 1);
    273 	do {
    274 		DELAY(200);
    275 	} while (!(GETREG(BAHSTAT) & BAH_POR));
    276 
    277 	linkaddress = GETMEM(BAHMACOFF);
    278 
    279 #if defined(BAH_DEBUG) && (BAH_DEBUG > 2)
    280 	printf("%s: reset: card reset, link addr = 0x%02x (%ld)\n",
    281 	    sc->sc_dev.dv_xname, linkaddress, linkaddress);
    282 #endif
    283 
    284 	/* tell the routing level about the (possibly changed) link address */
    285 	arc_ifattach(ifp, linkaddress);
    286 
    287 	/* POR is NMI, but we need it below: */
    288 	sc->sc_intmask = BAH_RECON|BAH_POR;
    289 	PUTREG(BAHSTAT, sc->sc_intmask);
    290 	PUTREG(BAHCMD, BAH_CONF(CONF_LONG));
    291 
    292 #ifdef BAH_DEBUG
    293 	printf("%s: reset: chip configured, status=0x%02x\n",
    294 	    sc->sc_dev.dv_xname, GETREG(BAHSTAT));
    295 #endif
    296 	PUTREG(BAHCMD, BAH_CLR(CLR_POR|CLR_RECONFIG));
    297 
    298 #ifdef BAH_DEBUG
    299 	printf("%s: reset: bits cleared, status=0x%02x\n",
    300 	    sc->sc_dev.dv_xname, GETREG(BAHSTAT);
    301 #endif
    302 
    303 	sc->sc_reconcount_excessive = ARC_EXCESSIVE_RECONS;
    304 
    305 	/* start receiver */
    306 
    307 	sc->sc_intmask  |= BAH_RI;
    308 	sc->sc_rx_fillcount = 0;
    309 	sc->sc_rx_act = 2;
    310 
    311 	PUTREG(BAHCMD, BAH_RXBC(2));
    312 	PUTREG(BAHSTAT, sc->sc_intmask);
    313 
    314 #ifdef BAH_DEBUG
    315 	printf("%s: reset: started receiver, status=0x%02x\n",
    316 	    sc->sc_dev.dv_xname, GETREG(BAHSTAT);
    317 #endif
    318 
    319 	/* and init transmitter status */
    320 	sc->sc_tx_act = 0;
    321 	sc->sc_tx_fillcount = 0;
    322 
    323 	ifp->if_flags |= IFF_RUNNING;
    324 	ifp->if_flags &= ~IFF_OACTIVE;
    325 
    326 	bah_start(ifp);
    327 }
    328 
    329 /*
    330  * Take interface offline
    331  */
    332 void
    333 bah_stop(sc)
    334 	struct bah_softc *sc;
    335 {
    336 	bus_space_tag_t bst_r = sc->sc_bst_r;
    337 	bus_space_handle_t regs = sc->sc_regs;
    338 
    339 	/* Stop the interrupts */
    340 	PUTREG(BAHSTAT, 0);
    341 
    342 	/* Stop the interface */
    343 	(*sc->sc_reset)(sc, 0);
    344 
    345 	/* Stop watchdog timer */
    346 	sc->sc_arccom.ac_if.if_timer = 0;
    347 }
    348 
    349 /*
    350  * Start output on interface. Get another datagram to send
    351  * off the interface queue, and copy it to the
    352  * interface becore starting the output
    353  *
    354  * this assumes that it is called inside a critical section...
    355  * XXX hm... does it still?
    356  *
    357  */
    358 void
    359 bah_start(ifp)
    360 	struct ifnet *ifp;
    361 {
    362 	struct bah_softc *sc = ifp->if_softc;
    363 	struct mbuf *m,*mp;
    364 
    365 	bus_space_tag_t bst_r = sc->sc_bst_r;
    366 	bus_space_handle_t regs = sc->sc_regs;
    367 	bus_space_tag_t bst_m = sc->sc_bst_m;
    368 	bus_space_handle_t mem = sc->sc_mem;
    369 
    370 	int bah_ram_ptr;
    371 	int len, tlen, offset, s, buffer;
    372 #ifdef BAHTIMINGS
    373 	u_long copystart, lencopy, perbyte;
    374 #endif
    375 
    376 #if defined(BAH_DEBUG) && (BAH_DEBUG > 3)
    377 	printf("%s: start(0x%x)\n", sc->sc_dev.dv_xname, ifp);
    378 #endif
    379 
    380 	if ((ifp->if_flags & IFF_RUNNING) == 0)
    381 		return;
    382 
    383 	s = splnet();
    384 
    385 	if (sc->sc_tx_fillcount >= 2) {
    386 		splx(s);
    387 		return;
    388 	}
    389 
    390 	IF_DEQUEUE(&ifp->if_snd, m);
    391 	buffer = sc->sc_tx_act ^ 1;
    392 
    393 	splx(s);
    394 
    395 	if (m == 0)
    396 		return;
    397 
    398 #if NBPFILTER > 0
    399 	/*
    400 	 * If bpf is listening on this interface, let it
    401 	 * see the packet before we commit it to the wire
    402 	 *
    403 	 * (can't give the copy in A2060 card RAM to bpf, because
    404 	 * that RAM is just accessed as on every other byte)
    405 	 */
    406 	if (ifp->if_bpf)
    407 		bpf_mtap(ifp->if_bpf, m);
    408 #endif
    409 
    410 #ifdef BAH_DEBUG
    411 	m = m_pullup(m, 3);	/* gcc does structure padding */
    412 	printf("%s: start: filling %ld from %ld to %ld type %ld\n",
    413 	    sc->sc_dev.dv_xname, buffer, mtod(m, u_char *)[0],
    414 	    mtod(m, u_char *)[1], mtod(m, u_char *)[2]);
    415 #else
    416 	m = m_pullup(m, 2);
    417 #endif
    418 	bah_ram_ptr = buffer*512;
    419 
    420 	/* write the addresses to RAM and throw them away */
    421 
    422 	/*
    423 	 * Hardware does this: Yet Another Microsecond Saved.
    424 	 * (btw, timing code says usually 2 microseconds)
    425 	 * PUTMEM(bah_ram_ptr + 0, mtod(m, u_char *)[0]);
    426 	 */
    427 
    428 	PUTMEM(bah_ram_ptr + 1, mtod(m, u_char *)[1]);
    429 	m_adj(m, 2);
    430 
    431 	/* get total length left at this point */
    432 	tlen = m->m_pkthdr.len;
    433 	if (tlen < ARC_MIN_FORBID_LEN) {
    434 		offset = 256 - tlen;
    435 		PUTMEM(bah_ram_ptr + 2, offset);
    436 	} else {
    437 		PUTMEM(bah_ram_ptr + 2, 0);
    438 		if (tlen <= ARC_MAX_FORBID_LEN)
    439 			offset = 255;		/* !!! */
    440 		else {
    441 			if (tlen > ARC_MAX_LEN)
    442 				tlen = ARC_MAX_LEN;
    443 			offset = 512 - tlen;
    444 		}
    445 		PUTMEM(bah_ram_ptr + 3, offset);
    446 
    447 	}
    448 	bah_ram_ptr += offset;
    449 
    450 	/* lets loop through the mbuf chain */
    451 
    452 	for (mp = m; mp; mp = mp->m_next) {
    453 		if ((len = mp->m_len)) {		/* YAMS */
    454 			bus_space_write_region_1(bst_m, mem, bah_ram_ptr,
    455 			    mtod(mp, caddr_t), len);
    456 
    457 			bah_ram_ptr += len;
    458 		}
    459 	}
    460 
    461 	sc->sc_broadcast[buffer] = (m->m_flags & M_BCAST) != 0;
    462 	sc->sc_retransmits[buffer] = (m->m_flags & M_BCAST) ? 1 : 5;
    463 
    464 	/* actually transmit the packet */
    465 	s = splnet();
    466 
    467 	if (++sc->sc_tx_fillcount > 1) {
    468 		/*
    469 		 * We are filled up to the rim. No more bufs for the moment,
    470 		 * please.
    471 		 */
    472 		ifp->if_flags |= IFF_OACTIVE;
    473 	} else {
    474 #ifdef BAH_DEBUG
    475 		printf("%s: start: starting transmitter on buffer %d\n",
    476 		    sc->sc_dev.dv_xname, buffer);
    477 #endif
    478 		/* Transmitter was off, start it */
    479 		sc->sc_tx_act = buffer;
    480 
    481 		/*
    482 		 * We still can accept another buf, so don't:
    483 		 * ifp->if_flags |= IFF_OACTIVE;
    484 		 */
    485 		sc->sc_intmask |= BAH_TA;
    486 		PUTREG(BAHCMD, BAH_TX(buffer));
    487 		PUTREG(BAHSTAT, sc->sc_intmask);
    488 
    489 		sc->sc_arccom.ac_if.if_timer = ARCTIMEOUT;
    490 	}
    491 	splx(s);
    492 	m_freem(m);
    493 
    494 	/*
    495 	 * After 10 times reading the docs, I realized
    496 	 * that in the case the receiver NAKs the buffer request,
    497 	 * the hardware retries till shutdown.
    498 	 * This is integrated now in the code above.
    499 	 */
    500 
    501 	return;
    502 }
    503 
    504 /*
    505  * Arcnet interface receiver soft interrupt:
    506  * get the stuff out of any filled buffer we find.
    507  */
    508 void
    509 bah_srint(vsc)
    510 	void *vsc;
    511 {
    512 	struct bah_softc *sc = (struct bah_softc *)vsc;
    513 	int buffer, len, len1, amount, offset, s, type;
    514 	int bah_ram_ptr;
    515 	struct mbuf *m, *dst, *head;
    516 	struct arc_header *ah;
    517 	struct ifnet *ifp;
    518 
    519 	bus_space_tag_t bst_r = sc->sc_bst_r;
    520         bus_space_tag_t bst_m = sc->sc_bst_m;
    521 	bus_space_handle_t regs = sc->sc_regs;
    522 	bus_space_handle_t mem = sc->sc_mem;
    523 
    524 	ifp = &sc->sc_arccom.ac_if;
    525 	head = 0;
    526 
    527 	s = splnet();
    528 	buffer = sc->sc_rx_act ^ 1;
    529 	splx(s);
    530 
    531 	/* Allocate header mbuf */
    532 	MGETHDR(m, M_DONTWAIT, MT_DATA);
    533 
    534 	if (m == 0) {
    535 		/*
    536 	 	 * in case s.th. goes wrong with mem, drop it
    537 	 	 * to make sure the receiver can be started again
    538 		 * count it as input error (we dont have any other
    539 		 * detectable)
    540 	 	 */
    541 		ifp->if_ierrors++;
    542 		goto cleanup;
    543 	}
    544 
    545 	m->m_pkthdr.rcvif = ifp;
    546 
    547 	/*
    548 	 * Align so that IP packet will be longword aligned. Here we
    549 	 * assume that m_data of new packet is longword aligned.
    550 	 * When implementing PHDS, we might have to change it to 2,
    551 	 * (2*sizeof(ulong) - ARC_HDRNEWLEN)), packet type dependent.
    552 	 */
    553 
    554 	bah_ram_ptr = buffer*512;
    555 	offset = GETMEM(bah_ram_ptr + 2);
    556 	if (offset)
    557 		len = 256 - offset;
    558 	else {
    559 		offset = GETMEM(bah_ram_ptr + 3);
    560 		len = 512 - offset;
    561 	}
    562 	type = GETMEM(bah_ram_ptr + offset);
    563 	m->m_data += 1 + arc_isphds(type);
    564 
    565 	head = m;
    566 	ah = mtod(head, struct arc_header *);
    567 
    568 	ah->arc_shost = GETMEM(bah_ram_ptr + 0);
    569 	ah->arc_dhost = GETMEM(bah_ram_ptr + 1);
    570 
    571 	m->m_pkthdr.len = len+2; /* whole packet length */
    572 	m->m_len = 2;		 /* mbuf filled with ARCnet addresses */
    573 	bah_ram_ptr += offset;	/* ram buffer continues there */
    574 
    575 	while (len > 0) {
    576 
    577 		len1 = len;
    578 		amount = M_TRAILINGSPACE(m);
    579 
    580 		if (amount == 0) {
    581 			dst = m;
    582 			MGET(m, M_DONTWAIT, MT_DATA);
    583 
    584 			if (m == 0) {
    585 				ifp->if_ierrors++;
    586 				goto cleanup;
    587 			}
    588 
    589 			if (len1 >= MINCLSIZE)
    590 				MCLGET(m, M_DONTWAIT);
    591 
    592 			m->m_len = 0;
    593 			dst->m_next = m;
    594 			amount = M_TRAILINGSPACE(m);
    595 		}
    596 
    597 		if (amount < len1)
    598 			len1 = amount;
    599 
    600 		bus_space_read_region_1(bst_m, mem, bah_ram_ptr,
    601 		    mtod(m, u_char *) + m->m_len, len1);
    602 
    603 		m->m_len += len1;
    604 		bah_ram_ptr += len1;
    605 		len -= len1;
    606 	}
    607 
    608 #if NBPFILTER > 0
    609 	if (ifp->if_bpf)
    610 		bpf_mtap(ifp->if_bpf, head);
    611 #endif
    612 
    613 	(*sc->sc_arccom.ac_if.if_input)(&sc->sc_arccom.ac_if, head);
    614 
    615 	head = NULL;
    616 	ifp->if_ipackets++;
    617 
    618 cleanup:
    619 
    620 	if (head != NULL)
    621 		m_freem(head);
    622 
    623 	/* mark buffer as invalid by source id 0 */
    624 	bus_space_write_1(bst_m, mem, buffer*512, 0);
    625 	s = splnet();
    626 
    627 	if (--sc->sc_rx_fillcount == 2 - 1) {
    628 
    629 		/* was off, restart it on buffer just emptied */
    630 		sc->sc_rx_act = buffer;
    631 		sc->sc_intmask |= BAH_RI;
    632 
    633 		/* this also clears the RI flag interupt: */
    634 		PUTREG(BAHCMD, BAH_RXBC(buffer));
    635 		PUTREG(BAHSTAT, sc->sc_intmask);
    636 
    637 #ifdef BAH_DEBUG
    638 		printf("%s: srint: restarted rx on buf %ld\n",
    639 		    sc->sc_dev.dv_xname, buffer);
    640 #endif
    641 	}
    642 	splx(s);
    643 }
    644 
    645 __inline static void
    646 bah_tint(sc, isr)
    647 	struct bah_softc *sc;
    648 	int isr;
    649 {
    650 	struct ifnet *ifp;
    651 
    652 	bus_space_tag_t bst_r = sc->sc_bst_r;
    653 	bus_space_handle_t regs = sc->sc_regs;
    654 
    655 
    656 	int buffer;
    657 #ifdef BAHTIMINGS
    658 	int clknow;
    659 #endif
    660 
    661 	ifp = &(sc->sc_arccom.ac_if);
    662 	buffer = sc->sc_tx_act;
    663 
    664 	/*
    665 	 * retransmit code:
    666 	 * Normal situtations first for fast path:
    667 	 * If acknowledgement received ok or broadcast, we're ok.
    668 	 * else if
    669 	 */
    670 
    671 	if (isr & BAH_TMA || sc->sc_broadcast[buffer])
    672 		sc->sc_arccom.ac_if.if_opackets++;
    673 #ifdef BAHRETRANSMIT
    674 	else if (ifp->if_flags & IFF_LINK2 && ifp->if_timer > 0
    675 	    && --sc->sc_retransmits[buffer] > 0) {
    676 		/* retransmit same buffer */
    677 		PUTREG(BAHCMD, BAH_TX(buffer));
    678 		return;
    679 	}
    680 #endif
    681 	else
    682 		ifp->if_oerrors++;
    683 
    684 
    685 	/* We know we can accept another buffer at this point. */
    686 	ifp->if_flags &= ~IFF_OACTIVE;
    687 
    688 	if (--sc->sc_tx_fillcount > 0) {
    689 
    690 		/*
    691 		 * start tx on other buffer.
    692 		 * This also clears the int flag
    693 		 */
    694 		buffer ^= 1;
    695 		sc->sc_tx_act = buffer;
    696 
    697 		/*
    698 		 * already given:
    699 		 * sc->sc_intmask |= BAH_TA;
    700 		 * PUTREG(BAHSTAT, sc->sc_intmask);
    701 		 */
    702 		PUTREG(BAHCMD, BAH_TX(buffer));
    703 		/* init watchdog timer */
    704 		ifp->if_timer = ARCTIMEOUT;
    705 
    706 #if defined(BAH_DEBUG) && (BAH_DEBUG > 1)
    707 		printf("%s: tint: starting tx on buffer %d, status 0x%02x\n",
    708 		    sc->sc_dev.dv_xname, buffer, GETREG(BAHSTAT));
    709 #endif
    710 	} else {
    711 		/* have to disable TX interrupt */
    712 		sc->sc_intmask &= ~BAH_TA;
    713 		PUTREG(BAHSTAT, sc->sc_intmask);
    714 		/* ... and watchdog timer */
    715 		ifp->if_timer = 0;
    716 
    717 #ifdef BAH_DEBUG
    718 		printf("%s: tint: no more buffers to send, status 0x%02x\n",
    719 		    sc->sc_dev.dv_xname, GETREG(BAHSTAT));
    720 #endif
    721 	}
    722 
    723 	/* XXXX TODO */
    724 #ifdef BAHSOFTCOPY
    725 	/* schedule soft int to fill a new buffer for us */
    726 	softintr_schedule(sc->sc_txcookie);
    727 #else
    728 	/* call it directly */
    729 	bah_start(ifp);
    730 #endif
    731 }
    732 
    733 /*
    734  * Our interrupt routine
    735  */
    736 int
    737 bahintr(arg)
    738 	void *arg;
    739 {
    740 	struct bah_softc *sc = arg;
    741 
    742 	bus_space_tag_t bst_r = sc->sc_bst_r;
    743         bus_space_tag_t bst_m = sc->sc_bst_m;
    744 	bus_space_handle_t regs = sc->sc_regs;
    745 	bus_space_handle_t mem = sc->sc_mem;
    746 
    747 	u_char isr, maskedisr;
    748 	int buffer;
    749 	u_long newsec;
    750 
    751 	isr = GETREG(BAHSTAT);
    752 	maskedisr = isr & sc->sc_intmask;
    753 	if (!maskedisr)
    754 		return (0);
    755 	do {
    756 
    757 #if defined(BAH_DEBUG) && (BAH_DEBUG>1)
    758 		printf("%s: intr: status 0x%02x, intmask 0x%02x\n",
    759 		    sc->sc_dev.dv_xname, isr, sc->sc_intmask);
    760 #endif
    761 
    762 		if (maskedisr & BAH_POR) {
    763 		  	/*
    764 			 * XXX We should never see this. Don't bother to store
    765 			 * the address.
    766 			 * sc->sc_arccom.ac_anaddr = GETMEM(BAHMACOFF);
    767 			 */
    768 			PUTREG(BAHCMD, BAH_CLR(CLR_POR));
    769 			log(LOG_WARNING,
    770 			    "%s: intr: got spurious power on reset int\n",
    771 			    sc->sc_dev.dv_xname);
    772 		}
    773 
    774 		if (maskedisr & BAH_RECON) {
    775 			/*
    776 			 * we dont need to:
    777 			 * PUTREG(BAHCMD, BAH_CONF(CONF_LONG));
    778 			 */
    779 			PUTREG(BAHCMD, BAH_CLR(CLR_RECONFIG));
    780 			sc->sc_arccom.ac_if.if_collisions++;
    781 
    782 			/*
    783 			 * If less than 2 seconds per reconfig:
    784 			 *	If ARC_EXCESSIVE_RECONFIGS
    785 			 *	since last burst, complain and set treshold for
    786 			 *	warnings to ARC_EXCESSIVE_RECONS_REWARN.
    787 			 *
    788 			 * This allows for, e.g., new stations on the cable, or
    789 			 * cable switching as long as it is over after
    790 			 * (normally) 16 seconds.
    791 			 *
    792 			 * XXX TODO: check timeout bits in status word and
    793 			 * double time if necessary.
    794 			 */
    795 
    796 			untimeout(bah_reconwatch, (void *)sc);
    797 			newsec = time.tv_sec;
    798 			if ((newsec - sc->sc_recontime <= 2) &&
    799 			    (++sc->sc_reconcount == ARC_EXCESSIVE_RECONS)) {
    800 				log(LOG_WARNING,
    801 				    "%s: excessive token losses, "
    802 				    "cable problem?\n", sc->sc_dev.dv_xname);
    803 			}
    804 			sc->sc_recontime = newsec;
    805 			timeout(bah_reconwatch, (void *)sc, 15*hz);
    806 		}
    807 
    808 		if (maskedisr & BAH_RI) {
    809 #if defined(BAH_DEBUG) && (BAH_DEBUG > 1)
    810 			printf("%s: intr: hard rint, act %ld\n",
    811 			    sc->sc_dev.dv_xname, sc->sc_rx_act);
    812 #endif
    813 
    814 			buffer = sc->sc_rx_act;
    815 			/* look if buffer is marked invalid: */
    816 			if (GETMEM(buffer*512) == 0) {
    817 				/*
    818 				 * invalid marked buffer (or illegally
    819 				 * configured sender)
    820 				 */
    821 				log(LOG_WARNING,
    822 				    "%s: spurious RX interupt or sender 0 "
    823 				    " (ignored)\n", sc->sc_dev.dv_xname);
    824 				/*
    825 				 * restart receiver on same buffer.
    826 				 * XXX maybe better reset interface?
    827 				 */
    828 				PUTREG(BAHCMD, BAH_RXBC(buffer));
    829 			} else {
    830 				if (++sc->sc_rx_fillcount > 1) {
    831 					sc->sc_intmask &= ~BAH_RI;
    832 					PUTREG(BAHSTAT, sc->sc_intmask);
    833 				} else {
    834 					buffer ^= 1;
    835 					sc->sc_rx_act = buffer;
    836 
    837 					/*
    838 					 * Start receiver on other receive
    839 					 * buffer. This also clears the RI
    840 					 * interupt flag.
    841 					 */
    842 					PUTREG(BAHCMD, BAH_RXBC(buffer));
    843 					/* in RX intr, so mask is ok for RX */
    844 
    845 #ifdef BAH_DEBUG
    846 					printf("%s: strt rx for buf %ld, "
    847 					    "stat 0x%02x\n",
    848 					    sc->sc_dev.dv_xname, sc->sc_rx_act,
    849 					    GETREG(BAHSTAT);
    850 #endif
    851 				}
    852 
    853 #ifdef BAHSOFTCOPY
    854 				/*
    855 				 * this one starts a soft int to copy out
    856 				 * of the hw
    857 				 */
    858 				softintr_schedule(sc->sc_rxcookie);
    859 #else
    860 				/* this one does the copy here */
    861 				bah_srint(sc);
    862 #endif
    863 			}
    864 		}
    865 		if (maskedisr & BAH_TA) {
    866 			bah_tint(sc, isr);
    867 		}
    868 		isr = GETREG(BAHSTAT);
    869 		maskedisr = isr & sc->sc_intmask;
    870 	} while (maskedisr);
    871 
    872 	return (1);
    873 }
    874 
    875 void
    876 bah_reconwatch(arg)
    877 	void *arg;
    878 {
    879 	struct bah_softc *sc = arg;
    880 
    881 	if (sc->sc_reconcount >= ARC_EXCESSIVE_RECONS) {
    882 		sc->sc_reconcount = 0;
    883 		log(LOG_WARNING, "%s: token valid again.\n",
    884 		    sc->sc_dev.dv_xname);
    885 	}
    886 	sc->sc_reconcount = 0;
    887 }
    888 
    889 
    890 /*
    891  * Process an ioctl request.
    892  * This code needs some work - it looks pretty ugly.
    893  */
    894 int
    895 bah_ioctl(ifp, command, data)
    896 	register struct ifnet *ifp;
    897 	u_long command;
    898 	caddr_t data;
    899 {
    900 	struct bah_softc *sc;
    901 	register struct ifaddr *ifa;
    902 	struct ifreq *ifr;
    903 	int s, error;
    904 
    905 	error = 0;
    906 	sc = ifp->if_softc;
    907 	ifa = (struct ifaddr *)data;
    908 	ifr = (struct ifreq *)data;
    909 	s = splnet();
    910 
    911 #if defined(BAH_DEBUG) && (BAH_DEBUG > 2)
    912 	printf("%s: ioctl() called, cmd = 0x%x\n",
    913 	    sc->sc_dev.dv_xname, command);
    914 #endif
    915 
    916 	switch (command) {
    917 	case SIOCSIFADDR:
    918 		ifp->if_flags |= IFF_UP;
    919 		switch (ifa->ifa_addr->sa_family) {
    920 #ifdef INET
    921 		case AF_INET:
    922 			bah_init(sc);
    923 			arp_ifinit(ifp, ifa);
    924 			break;
    925 #endif
    926 		default:
    927 			bah_init(sc);
    928 			break;
    929 		}
    930 
    931 	case SIOCSIFFLAGS:
    932 		if ((ifp->if_flags & IFF_UP) == 0 &&
    933 		    (ifp->if_flags & IFF_RUNNING) != 0) {
    934 			/*
    935 			 * If interface is marked down and it is running,
    936 			 * then stop it.
    937 			 */
    938 			bah_stop(sc);
    939 			ifp->if_flags &= ~IFF_RUNNING;
    940 		} else if ((ifp->if_flags & IFF_UP) != 0 &&
    941 			   (ifp->if_flags & IFF_RUNNING) == 0) {
    942 			/*
    943 			 * If interface is marked up and it is stopped, then
    944 			 * start it.
    945 			 */
    946 			bah_init(sc);
    947 		}
    948 		break;
    949 
    950 	case SIOCADDMULTI:
    951 	case SIOCDELMULTI:
    952 		if (ifr->ifr_addr.sa_family == AF_INET)
    953 			error = 0;
    954 		else
    955 			error = EAFNOSUPPORT;
    956 		break;
    957 
    958 	default:
    959 		error = EINVAL;
    960 	}
    961 
    962 	splx(s);
    963 	return (error);
    964 }
    965 
    966 /*
    967  * watchdog routine for transmitter.
    968  *
    969  * We need this, because else a receiver whose hardware is alive, but whose
    970  * software has not enabled the Receiver, would make our hardware wait forever
    971  * Discovered this after 20 times reading the docs.
    972  *
    973  * Only thing we do is disable transmitter. We'll get an transmit timeout,
    974  * and the int handler will have to decide not to retransmit (in case
    975  * retransmission is implemented).
    976  *
    977  * This one assumes being called inside splnet()
    978  */
    979 
    980 void
    981 bah_watchdog(ifp)
    982 	struct ifnet *ifp;
    983 {
    984 	struct bah_softc *sc = ifp->if_softc;
    985 
    986 	bus_space_tag_t bst_r = sc->sc_bst_r;
    987 	bus_space_handle_t regs = sc->sc_regs;
    988 
    989 	PUTREG(BAHCMD, BAH_TXDIS);
    990 	return;
    991 }
    992 
    993