Home | History | Annotate | Line # | Download | only in ic
smc90cx6.c revision 1.32
      1 /*	$NetBSD: smc90cx6.c,v 1.32 1999/08/27 19:23:19 is 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 	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_storelladdr(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 
    546 	/*
    547 	 * Align so that IP packet will be longword aligned. Here we
    548 	 * assume that m_data of new packet is longword aligned.
    549 	 * When implementing PHDS, we might have to change it to 2,
    550 	 * (2*sizeof(ulong) - ARC_HDRNEWLEN)), packet type dependent.
    551 	 */
    552 
    553 	bah_ram_ptr = buffer*512;
    554 	offset = GETMEM(bah_ram_ptr + 2);
    555 	if (offset)
    556 		len = 256 - offset;
    557 	else {
    558 		offset = GETMEM(bah_ram_ptr + 3);
    559 		len = 512 - offset;
    560 	}
    561 	type = GETMEM(bah_ram_ptr + offset);
    562 	m->m_data += 1 + arc_isphds(type);
    563 
    564 	head = m;
    565 	ah = mtod(head, struct arc_header *);
    566 
    567 	ah->arc_shost = GETMEM(bah_ram_ptr + 0);
    568 	ah->arc_dhost = GETMEM(bah_ram_ptr + 1);
    569 
    570 	m->m_pkthdr.len = len+2; /* whole packet length */
    571 	m->m_len = 2;		 /* mbuf filled with ARCnet addresses */
    572 	bah_ram_ptr += offset;	/* ram buffer continues there */
    573 
    574 	while (len > 0) {
    575 
    576 		len1 = len;
    577 		amount = M_TRAILINGSPACE(m);
    578 
    579 		if (amount == 0) {
    580 			dst = m;
    581 			MGET(m, M_DONTWAIT, MT_DATA);
    582 
    583 			if (m == 0) {
    584 				ifp->if_ierrors++;
    585 				goto cleanup;
    586 			}
    587 
    588 			if (len1 >= MINCLSIZE)
    589 				MCLGET(m, M_DONTWAIT);
    590 
    591 			m->m_len = 0;
    592 			dst->m_next = m;
    593 			amount = M_TRAILINGSPACE(m);
    594 		}
    595 
    596 		if (amount < len1)
    597 			len1 = amount;
    598 
    599 		bus_space_read_region_1(bst_m, mem, bah_ram_ptr,
    600 		    mtod(m, u_char *) + m->m_len, len1);
    601 
    602 		m->m_len += len1;
    603 		bah_ram_ptr += len1;
    604 		len -= len1;
    605 	}
    606 
    607 #if NBPFILTER > 0
    608 	if (ifp->if_bpf)
    609 		bpf_mtap(ifp->if_bpf, head);
    610 #endif
    611 
    612 	(*sc->sc_arccom.ac_if.if_input)(&sc->sc_arccom.ac_if, head);
    613 
    614 	head = NULL;
    615 	ifp->if_ipackets++;
    616 
    617 cleanup:
    618 
    619 	if (head != NULL)
    620 		m_freem(head);
    621 
    622 	/* mark buffer as invalid by source id 0 */
    623 	bus_space_write_1(bst_m, mem, buffer*512, 0);
    624 	s = splnet();
    625 
    626 	if (--sc->sc_rx_fillcount == 2 - 1) {
    627 
    628 		/* was off, restart it on buffer just emptied */
    629 		sc->sc_rx_act = buffer;
    630 		sc->sc_intmask |= BAH_RI;
    631 
    632 		/* this also clears the RI flag interupt: */
    633 		PUTREG(BAHCMD, BAH_RXBC(buffer));
    634 		PUTREG(BAHSTAT, sc->sc_intmask);
    635 
    636 #ifdef BAH_DEBUG
    637 		printf("%s: srint: restarted rx on buf %ld\n",
    638 		    sc->sc_dev.dv_xname, buffer);
    639 #endif
    640 	}
    641 	splx(s);
    642 }
    643 
    644 __inline static void
    645 bah_tint(sc, isr)
    646 	struct bah_softc *sc;
    647 	int isr;
    648 {
    649 	struct ifnet *ifp;
    650 
    651 	bus_space_tag_t bst_r = sc->sc_bst_r;
    652 	bus_space_handle_t regs = sc->sc_regs;
    653 
    654 
    655 	int buffer;
    656 #ifdef BAHTIMINGS
    657 	int clknow;
    658 #endif
    659 
    660 	ifp = &(sc->sc_arccom.ac_if);
    661 	buffer = sc->sc_tx_act;
    662 
    663 	/*
    664 	 * retransmit code:
    665 	 * Normal situtations first for fast path:
    666 	 * If acknowledgement received ok or broadcast, we're ok.
    667 	 * else if
    668 	 */
    669 
    670 	if (isr & BAH_TMA || sc->sc_broadcast[buffer])
    671 		sc->sc_arccom.ac_if.if_opackets++;
    672 #ifdef BAHRETRANSMIT
    673 	else if (ifp->if_flags & IFF_LINK2 && ifp->if_timer > 0
    674 	    && --sc->sc_retransmits[buffer] > 0) {
    675 		/* retransmit same buffer */
    676 		PUTREG(BAHCMD, BAH_TX(buffer));
    677 		return;
    678 	}
    679 #endif
    680 	else
    681 		ifp->if_oerrors++;
    682 
    683 
    684 	/* We know we can accept another buffer at this point. */
    685 	ifp->if_flags &= ~IFF_OACTIVE;
    686 
    687 	if (--sc->sc_tx_fillcount > 0) {
    688 
    689 		/*
    690 		 * start tx on other buffer.
    691 		 * This also clears the int flag
    692 		 */
    693 		buffer ^= 1;
    694 		sc->sc_tx_act = buffer;
    695 
    696 		/*
    697 		 * already given:
    698 		 * sc->sc_intmask |= BAH_TA;
    699 		 * PUTREG(BAHSTAT, sc->sc_intmask);
    700 		 */
    701 		PUTREG(BAHCMD, BAH_TX(buffer));
    702 		/* init watchdog timer */
    703 		ifp->if_timer = ARCTIMEOUT;
    704 
    705 #if defined(BAH_DEBUG) && (BAH_DEBUG > 1)
    706 		printf("%s: tint: starting tx on buffer %d, status 0x%02x\n",
    707 		    sc->sc_dev.dv_xname, buffer, GETREG(BAHSTAT));
    708 #endif
    709 	} else {
    710 		/* have to disable TX interrupt */
    711 		sc->sc_intmask &= ~BAH_TA;
    712 		PUTREG(BAHSTAT, sc->sc_intmask);
    713 		/* ... and watchdog timer */
    714 		ifp->if_timer = 0;
    715 
    716 #ifdef BAH_DEBUG
    717 		printf("%s: tint: no more buffers to send, status 0x%02x\n",
    718 		    sc->sc_dev.dv_xname, GETREG(BAHSTAT));
    719 #endif
    720 	}
    721 
    722 	/* XXXX TODO */
    723 #ifdef BAHSOFTCOPY
    724 	/* schedule soft int to fill a new buffer for us */
    725 	softintr_schedule(sc->sc_txcookie);
    726 #else
    727 	/* call it directly */
    728 	bah_start(ifp);
    729 #endif
    730 }
    731 
    732 /*
    733  * Our interrupt routine
    734  */
    735 int
    736 bahintr(arg)
    737 	void *arg;
    738 {
    739 	struct bah_softc *sc = arg;
    740 
    741 	bus_space_tag_t bst_r = sc->sc_bst_r;
    742         bus_space_tag_t bst_m = sc->sc_bst_m;
    743 	bus_space_handle_t regs = sc->sc_regs;
    744 	bus_space_handle_t mem = sc->sc_mem;
    745 
    746 	u_char isr, maskedisr;
    747 	int buffer;
    748 	u_long newsec;
    749 
    750 	isr = GETREG(BAHSTAT);
    751 	maskedisr = isr & sc->sc_intmask;
    752 	if (!maskedisr)
    753 		return (0);
    754 	do {
    755 
    756 #if defined(BAH_DEBUG) && (BAH_DEBUG>1)
    757 		printf("%s: intr: status 0x%02x, intmask 0x%02x\n",
    758 		    sc->sc_dev.dv_xname, isr, sc->sc_intmask);
    759 #endif
    760 
    761 		if (maskedisr & BAH_POR) {
    762 		  	/*
    763 			 * XXX We should never see this. Don't bother to store
    764 			 * the address.
    765 			 * sc->sc_arccom.ac_anaddr = GETMEM(BAHMACOFF);
    766 			 */
    767 			PUTREG(BAHCMD, BAH_CLR(CLR_POR));
    768 			log(LOG_WARNING,
    769 			    "%s: intr: got spurious power on reset int\n",
    770 			    sc->sc_dev.dv_xname);
    771 		}
    772 
    773 		if (maskedisr & BAH_RECON) {
    774 			/*
    775 			 * we dont need to:
    776 			 * PUTREG(BAHCMD, BAH_CONF(CONF_LONG));
    777 			 */
    778 			PUTREG(BAHCMD, BAH_CLR(CLR_RECONFIG));
    779 			sc->sc_arccom.ac_if.if_collisions++;
    780 
    781 			/*
    782 			 * If less than 2 seconds per reconfig:
    783 			 *	If ARC_EXCESSIVE_RECONFIGS
    784 			 *	since last burst, complain and set treshold for
    785 			 *	warnings to ARC_EXCESSIVE_RECONS_REWARN.
    786 			 *
    787 			 * This allows for, e.g., new stations on the cable, or
    788 			 * cable switching as long as it is over after
    789 			 * (normally) 16 seconds.
    790 			 *
    791 			 * XXX TODO: check timeout bits in status word and
    792 			 * double time if necessary.
    793 			 */
    794 
    795 			untimeout(bah_reconwatch, (void *)sc);
    796 			newsec = time.tv_sec;
    797 			if ((newsec - sc->sc_recontime <= 2) &&
    798 			    (++sc->sc_reconcount == ARC_EXCESSIVE_RECONS)) {
    799 				log(LOG_WARNING,
    800 				    "%s: excessive token losses, "
    801 				    "cable problem?\n", sc->sc_dev.dv_xname);
    802 			}
    803 			sc->sc_recontime = newsec;
    804 			timeout(bah_reconwatch, (void *)sc, 15*hz);
    805 		}
    806 
    807 		if (maskedisr & BAH_RI) {
    808 #if defined(BAH_DEBUG) && (BAH_DEBUG > 1)
    809 			printf("%s: intr: hard rint, act %ld\n",
    810 			    sc->sc_dev.dv_xname, sc->sc_rx_act);
    811 #endif
    812 
    813 			buffer = sc->sc_rx_act;
    814 			/* look if buffer is marked invalid: */
    815 			if (GETMEM(buffer*512) == 0) {
    816 				/*
    817 				 * invalid marked buffer (or illegally
    818 				 * configured sender)
    819 				 */
    820 				log(LOG_WARNING,
    821 				    "%s: spurious RX interupt or sender 0 "
    822 				    " (ignored)\n", sc->sc_dev.dv_xname);
    823 				/*
    824 				 * restart receiver on same buffer.
    825 				 * XXX maybe better reset interface?
    826 				 */
    827 				PUTREG(BAHCMD, BAH_RXBC(buffer));
    828 			} else {
    829 				if (++sc->sc_rx_fillcount > 1) {
    830 					sc->sc_intmask &= ~BAH_RI;
    831 					PUTREG(BAHSTAT, sc->sc_intmask);
    832 				} else {
    833 					buffer ^= 1;
    834 					sc->sc_rx_act = buffer;
    835 
    836 					/*
    837 					 * Start receiver on other receive
    838 					 * buffer. This also clears the RI
    839 					 * interupt flag.
    840 					 */
    841 					PUTREG(BAHCMD, BAH_RXBC(buffer));
    842 					/* in RX intr, so mask is ok for RX */
    843 
    844 #ifdef BAH_DEBUG
    845 					printf("%s: strt rx for buf %ld, "
    846 					    "stat 0x%02x\n",
    847 					    sc->sc_dev.dv_xname, sc->sc_rx_act,
    848 					    GETREG(BAHSTAT);
    849 #endif
    850 				}
    851 
    852 #ifdef BAHSOFTCOPY
    853 				/*
    854 				 * this one starts a soft int to copy out
    855 				 * of the hw
    856 				 */
    857 				softintr_schedule(sc->sc_rxcookie);
    858 #else
    859 				/* this one does the copy here */
    860 				bah_srint(sc);
    861 #endif
    862 			}
    863 		}
    864 		if (maskedisr & BAH_TA) {
    865 			bah_tint(sc, isr);
    866 		}
    867 		isr = GETREG(BAHSTAT);
    868 		maskedisr = isr & sc->sc_intmask;
    869 	} while (maskedisr);
    870 
    871 	return (1);
    872 }
    873 
    874 void
    875 bah_reconwatch(arg)
    876 	void *arg;
    877 {
    878 	struct bah_softc *sc = arg;
    879 
    880 	if (sc->sc_reconcount >= ARC_EXCESSIVE_RECONS) {
    881 		sc->sc_reconcount = 0;
    882 		log(LOG_WARNING, "%s: token valid again.\n",
    883 		    sc->sc_dev.dv_xname);
    884 	}
    885 	sc->sc_reconcount = 0;
    886 }
    887 
    888 
    889 /*
    890  * Process an ioctl request.
    891  * This code needs some work - it looks pretty ugly.
    892  */
    893 int
    894 bah_ioctl(ifp, command, data)
    895 	register struct ifnet *ifp;
    896 	u_long command;
    897 	caddr_t data;
    898 {
    899 	struct bah_softc *sc;
    900 	register struct ifaddr *ifa;
    901 	struct ifreq *ifr;
    902 	int s, error;
    903 
    904 	error = 0;
    905 	sc = ifp->if_softc;
    906 	ifa = (struct ifaddr *)data;
    907 	ifr = (struct ifreq *)data;
    908 	s = splnet();
    909 
    910 #if defined(BAH_DEBUG) && (BAH_DEBUG > 2)
    911 	printf("%s: ioctl() called, cmd = 0x%x\n",
    912 	    sc->sc_dev.dv_xname, command);
    913 #endif
    914 
    915 	switch (command) {
    916 	case SIOCSIFADDR:
    917 		ifp->if_flags |= IFF_UP;
    918 		switch (ifa->ifa_addr->sa_family) {
    919 #ifdef INET
    920 		case AF_INET:
    921 			bah_init(sc);
    922 			arp_ifinit(ifp, ifa);
    923 			break;
    924 #endif
    925 		default:
    926 			bah_init(sc);
    927 			break;
    928 		}
    929 
    930 	case SIOCSIFFLAGS:
    931 		if ((ifp->if_flags & IFF_UP) == 0 &&
    932 		    (ifp->if_flags & IFF_RUNNING) != 0) {
    933 			/*
    934 			 * If interface is marked down and it is running,
    935 			 * then stop it.
    936 			 */
    937 			bah_stop(sc);
    938 			ifp->if_flags &= ~IFF_RUNNING;
    939 		} else if ((ifp->if_flags & IFF_UP) != 0 &&
    940 			   (ifp->if_flags & IFF_RUNNING) == 0) {
    941 			/*
    942 			 * If interface is marked up and it is stopped, then
    943 			 * start it.
    944 			 */
    945 			bah_init(sc);
    946 		}
    947 		break;
    948 
    949 	case SIOCADDMULTI:
    950 	case SIOCDELMULTI:
    951 		if (ifr->ifr_addr.sa_family == AF_INET)
    952 			error = 0;
    953 		else
    954 			error = EAFNOSUPPORT;
    955 		break;
    956 
    957 	default:
    958 		error = EINVAL;
    959 	}
    960 
    961 	splx(s);
    962 	return (error);
    963 }
    964 
    965 /*
    966  * watchdog routine for transmitter.
    967  *
    968  * We need this, because else a receiver whose hardware is alive, but whose
    969  * software has not enabled the Receiver, would make our hardware wait forever
    970  * Discovered this after 20 times reading the docs.
    971  *
    972  * Only thing we do is disable transmitter. We'll get an transmit timeout,
    973  * and the int handler will have to decide not to retransmit (in case
    974  * retransmission is implemented).
    975  *
    976  * This one assumes being called inside splnet()
    977  */
    978 
    979 void
    980 bah_watchdog(ifp)
    981 	struct ifnet *ifp;
    982 {
    983 	struct bah_softc *sc = ifp->if_softc;
    984 
    985 	bus_space_tag_t bst_r = sc->sc_bst_r;
    986 	bus_space_handle_t regs = sc->sc_regs;
    987 
    988 	PUTREG(BAHCMD, BAH_TXDIS);
    989 	return;
    990 }
    991 
    992