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