Home | History | Annotate | Line # | Download | only in ic
smc90cx6.c revision 1.37
      1 /*	$NetBSD: smc90cx6.c,v 1.37 2000/12/14 06:27:26 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 	IFQ_SET_READY(&ifp->if_snd);
    207 
    208 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS;
    209 
    210 	ifp->if_mtu = ARCMTU;
    211 
    212 	arc_ifattach(ifp, linkaddress);
    213 
    214 #ifdef BAHSOFTCOPY
    215 	sc->sc_rxcookie = softintr_establish(IPL_SOFTNET, bah_srint, sc);
    216 	sc->sc_txcookie = softintr_establish(IPL_SOFTNET,
    217 		(void (*) __P((void *)))bah_start, ifp);
    218 #endif
    219 
    220 	callout_init(&sc->sc_recon_ch);
    221 }
    222 
    223 /*
    224  * Initialize device
    225  *
    226  */
    227 void
    228 bah_init(sc)
    229 	struct bah_softc *sc;
    230 {
    231 	struct ifnet *ifp;
    232 	int s;
    233 
    234 	ifp = &sc->sc_arccom.ac_if;
    235 
    236 	if ((ifp->if_flags & IFF_RUNNING) == 0) {
    237 		s = splnet();
    238 		ifp->if_flags |= IFF_RUNNING;
    239 		bah_reset(sc);
    240 		bah_start(ifp);
    241 		splx(s);
    242 	}
    243 }
    244 
    245 /*
    246  * Reset the interface...
    247  *
    248  * this assumes that it is called inside a critical section...
    249  *
    250  */
    251 void
    252 bah_reset(sc)
    253 	struct bah_softc *sc;
    254 {
    255 	struct ifnet *ifp;
    256 	int linkaddress;
    257 
    258 	bus_space_tag_t bst_r = sc->sc_bst_r;
    259         bus_space_tag_t bst_m = sc->sc_bst_m;
    260 	bus_space_handle_t regs = sc->sc_regs;
    261 	bus_space_handle_t mem = sc->sc_mem;
    262 
    263 	ifp = &sc->sc_arccom.ac_if;
    264 
    265 #ifdef BAH_DEBUG
    266 	printf("%s: reset\n", sc->sc_dev.dv_xname);
    267 #endif
    268 	/* stop and restart hardware */
    269 
    270 	(*sc->sc_reset)(sc, 1);
    271 	do {
    272 		DELAY(200);
    273 	} while (!(GETREG(BAHSTAT) & BAH_POR));
    274 
    275 	linkaddress = GETMEM(BAHMACOFF);
    276 
    277 #if defined(BAH_DEBUG) && (BAH_DEBUG > 2)
    278 	printf("%s: reset: card reset, link addr = 0x%02x (%ld)\n",
    279 	    sc->sc_dev.dv_xname, linkaddress, linkaddress);
    280 #endif
    281 
    282 	/* tell the routing level about the (possibly changed) link address */
    283 	arc_storelladdr(ifp, linkaddress);
    284 
    285 	/* POR is NMI, but we need it below: */
    286 	sc->sc_intmask = BAH_RECON|BAH_POR;
    287 	PUTREG(BAHSTAT, sc->sc_intmask);
    288 	PUTREG(BAHCMD, BAH_CONF(CONF_LONG));
    289 
    290 #ifdef BAH_DEBUG
    291 	printf("%s: reset: chip configured, status=0x%02x\n",
    292 	    sc->sc_dev.dv_xname, GETREG(BAHSTAT));
    293 #endif
    294 	PUTREG(BAHCMD, BAH_CLR(CLR_POR|CLR_RECONFIG));
    295 
    296 #ifdef BAH_DEBUG
    297 	printf("%s: reset: bits cleared, status=0x%02x\n",
    298 	    sc->sc_dev.dv_xname, GETREG(BAHSTAT);
    299 #endif
    300 
    301 	sc->sc_reconcount_excessive = ARC_EXCESSIVE_RECONS;
    302 
    303 	/* start receiver */
    304 
    305 	sc->sc_intmask  |= BAH_RI;
    306 	sc->sc_rx_fillcount = 0;
    307 	sc->sc_rx_act = 2;
    308 
    309 	PUTREG(BAHCMD, BAH_RXBC(2));
    310 	PUTREG(BAHSTAT, sc->sc_intmask);
    311 
    312 #ifdef BAH_DEBUG
    313 	printf("%s: reset: started receiver, status=0x%02x\n",
    314 	    sc->sc_dev.dv_xname, GETREG(BAHSTAT);
    315 #endif
    316 
    317 	/* and init transmitter status */
    318 	sc->sc_tx_act = 0;
    319 	sc->sc_tx_fillcount = 0;
    320 
    321 	ifp->if_flags |= IFF_RUNNING;
    322 	ifp->if_flags &= ~IFF_OACTIVE;
    323 
    324 	bah_start(ifp);
    325 }
    326 
    327 /*
    328  * Take interface offline
    329  */
    330 void
    331 bah_stop(sc)
    332 	struct bah_softc *sc;
    333 {
    334 	bus_space_tag_t bst_r = sc->sc_bst_r;
    335 	bus_space_handle_t regs = sc->sc_regs;
    336 
    337 	/* Stop the interrupts */
    338 	PUTREG(BAHSTAT, 0);
    339 
    340 	/* Stop the interface */
    341 	(*sc->sc_reset)(sc, 0);
    342 
    343 	/* Stop watchdog timer */
    344 	sc->sc_arccom.ac_if.if_timer = 0;
    345 }
    346 
    347 /*
    348  * Start output on interface. Get another datagram to send
    349  * off the interface queue, and copy it to the
    350  * interface becore starting the output
    351  *
    352  * this assumes that it is called inside a critical section...
    353  * XXX hm... does it still?
    354  *
    355  */
    356 void
    357 bah_start(ifp)
    358 	struct ifnet *ifp;
    359 {
    360 	struct bah_softc *sc = ifp->if_softc;
    361 	struct mbuf *m,*mp;
    362 
    363 	bus_space_tag_t bst_r = sc->sc_bst_r;
    364 	bus_space_handle_t regs = sc->sc_regs;
    365 	bus_space_tag_t bst_m = sc->sc_bst_m;
    366 	bus_space_handle_t mem = sc->sc_mem;
    367 
    368 	int bah_ram_ptr;
    369 	int len, tlen, offset, s, buffer;
    370 #ifdef BAHTIMINGS
    371 	u_long copystart, lencopy, perbyte;
    372 #endif
    373 
    374 #if defined(BAH_DEBUG) && (BAH_DEBUG > 3)
    375 	printf("%s: start(0x%x)\n", sc->sc_dev.dv_xname, ifp);
    376 #endif
    377 
    378 	if ((ifp->if_flags & IFF_RUNNING) == 0)
    379 		return;
    380 
    381 	s = splnet();
    382 
    383 	if (sc->sc_tx_fillcount >= 2) {
    384 		splx(s);
    385 		return;
    386 	}
    387 
    388 	IFQ_DEQUEUE(&ifp->if_snd, m);
    389 	buffer = sc->sc_tx_act ^ 1;
    390 
    391 	splx(s);
    392 
    393 	if (m == 0)
    394 		return;
    395 
    396 #if NBPFILTER > 0
    397 	/*
    398 	 * If bpf is listening on this interface, let it
    399 	 * see the packet before we commit it to the wire
    400 	 *
    401 	 * (can't give the copy in A2060 card RAM to bpf, because
    402 	 * that RAM is just accessed as on every other byte)
    403 	 */
    404 	if (ifp->if_bpf)
    405 		bpf_mtap(ifp->if_bpf, m);
    406 #endif
    407 
    408 #ifdef BAH_DEBUG
    409 	if (m->m_len < ARC_HDRLEN)
    410 		m = m_pullup(m, ARC_HDRLEN);/* 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 	if (m->m_len < 2)
    416 		m = m_pullup(m, 2);
    417 #endif
    418 	bah_ram_ptr = buffer*512;
    419 
    420 	if (m == 0)
    421 		return;
    422 
    423 	/* write the addresses to RAM and throw them away */
    424 
    425 	/*
    426 	 * Hardware does this: Yet Another Microsecond Saved.
    427 	 * (btw, timing code says usually 2 microseconds)
    428 	 * PUTMEM(bah_ram_ptr + 0, mtod(m, u_char *)[0]);
    429 	 */
    430 
    431 	PUTMEM(bah_ram_ptr + 1, mtod(m, u_char *)[1]);
    432 	m_adj(m, 2);
    433 
    434 	/* get total length left at this point */
    435 	tlen = m->m_pkthdr.len;
    436 	if (tlen < ARC_MIN_FORBID_LEN) {
    437 		offset = 256 - tlen;
    438 		PUTMEM(bah_ram_ptr + 2, offset);
    439 	} else {
    440 		PUTMEM(bah_ram_ptr + 2, 0);
    441 		if (tlen <= ARC_MAX_FORBID_LEN)
    442 			offset = 255;		/* !!! */
    443 		else {
    444 			if (tlen > ARC_MAX_LEN)
    445 				tlen = ARC_MAX_LEN;
    446 			offset = 512 - tlen;
    447 		}
    448 		PUTMEM(bah_ram_ptr + 3, offset);
    449 
    450 	}
    451 	bah_ram_ptr += offset;
    452 
    453 	/* lets loop through the mbuf chain */
    454 
    455 	for (mp = m; mp; mp = mp->m_next) {
    456 		if ((len = mp->m_len)) {		/* YAMS */
    457 			bus_space_write_region_1(bst_m, mem, bah_ram_ptr,
    458 			    mtod(mp, caddr_t), len);
    459 
    460 			bah_ram_ptr += len;
    461 		}
    462 	}
    463 
    464 	sc->sc_broadcast[buffer] = (m->m_flags & M_BCAST) != 0;
    465 	sc->sc_retransmits[buffer] = (m->m_flags & M_BCAST) ? 1 : 5;
    466 
    467 	/* actually transmit the packet */
    468 	s = splnet();
    469 
    470 	if (++sc->sc_tx_fillcount > 1) {
    471 		/*
    472 		 * We are filled up to the rim. No more bufs for the moment,
    473 		 * please.
    474 		 */
    475 		ifp->if_flags |= IFF_OACTIVE;
    476 	} else {
    477 #ifdef BAH_DEBUG
    478 		printf("%s: start: starting transmitter on buffer %d\n",
    479 		    sc->sc_dev.dv_xname, buffer);
    480 #endif
    481 		/* Transmitter was off, start it */
    482 		sc->sc_tx_act = buffer;
    483 
    484 		/*
    485 		 * We still can accept another buf, so don't:
    486 		 * ifp->if_flags |= IFF_OACTIVE;
    487 		 */
    488 		sc->sc_intmask |= BAH_TA;
    489 		PUTREG(BAHCMD, BAH_TX(buffer));
    490 		PUTREG(BAHSTAT, sc->sc_intmask);
    491 
    492 		sc->sc_arccom.ac_if.if_timer = ARCTIMEOUT;
    493 	}
    494 	splx(s);
    495 	m_freem(m);
    496 
    497 	/*
    498 	 * After 10 times reading the docs, I realized
    499 	 * that in the case the receiver NAKs the buffer request,
    500 	 * the hardware retries till shutdown.
    501 	 * This is integrated now in the code above.
    502 	 */
    503 
    504 	return;
    505 }
    506 
    507 /*
    508  * Arcnet interface receiver soft interrupt:
    509  * get the stuff out of any filled buffer we find.
    510  */
    511 void
    512 bah_srint(vsc)
    513 	void *vsc;
    514 {
    515 	struct bah_softc *sc = (struct bah_softc *)vsc;
    516 	int buffer, len, len1, amount, offset, s, type;
    517 	int bah_ram_ptr;
    518 	struct mbuf *m, *dst, *head;
    519 	struct arc_header *ah;
    520 	struct ifnet *ifp;
    521 
    522 	bus_space_tag_t bst_r = sc->sc_bst_r;
    523         bus_space_tag_t bst_m = sc->sc_bst_m;
    524 	bus_space_handle_t regs = sc->sc_regs;
    525 	bus_space_handle_t mem = sc->sc_mem;
    526 
    527 	ifp = &sc->sc_arccom.ac_if;
    528 	head = 0;
    529 
    530 	s = splnet();
    531 	buffer = sc->sc_rx_act ^ 1;
    532 	splx(s);
    533 
    534 	/* Allocate header mbuf */
    535 	MGETHDR(m, M_DONTWAIT, MT_DATA);
    536 
    537 	if (m == 0) {
    538 		/*
    539 	 	 * in case s.th. goes wrong with mem, drop it
    540 	 	 * to make sure the receiver can be started again
    541 		 * count it as input error (we dont have any other
    542 		 * detectable)
    543 	 	 */
    544 		ifp->if_ierrors++;
    545 		goto cleanup;
    546 	}
    547 
    548 	m->m_pkthdr.rcvif = ifp;
    549 
    550 	/*
    551 	 * Align so that IP packet will be longword aligned. Here we
    552 	 * assume that m_data of new packet is longword aligned.
    553 	 * When implementing PHDS, we might have to change it to 2,
    554 	 * (2*sizeof(ulong) - ARC_HDRNEWLEN)), packet type dependent.
    555 	 */
    556 
    557 	bah_ram_ptr = buffer*512;
    558 	offset = GETMEM(bah_ram_ptr + 2);
    559 	if (offset)
    560 		len = 256 - offset;
    561 	else {
    562 		offset = GETMEM(bah_ram_ptr + 3);
    563 		len = 512 - offset;
    564 	}
    565 	if (len+2 >= MINCLSIZE)
    566 		MCLGET(m, M_DONTWAIT);
    567 
    568 	if (m == 0) {
    569 		ifp->if_ierrors++;
    570 		goto cleanup;
    571 	}
    572 
    573 	type = GETMEM(bah_ram_ptr + offset);
    574 	m->m_data += 1 + arc_isphds(type);
    575 
    576 	head = m;
    577 	ah = mtod(head, struct arc_header *);
    578 
    579 	ah->arc_shost = GETMEM(bah_ram_ptr + 0);
    580 	ah->arc_dhost = GETMEM(bah_ram_ptr + 1);
    581 
    582 	m->m_pkthdr.len = len+2; /* whole packet length */
    583 	m->m_len = 2;		 /* mbuf filled with ARCnet addresses */
    584 	bah_ram_ptr += offset;	/* ram buffer continues there */
    585 
    586 	while (len > 0) {
    587 
    588 		len1 = len;
    589 		amount = M_TRAILINGSPACE(m);
    590 
    591 		if (amount == 0) {
    592 			dst = m;
    593 			MGET(m, M_DONTWAIT, MT_DATA);
    594 
    595 			if (m == 0) {
    596 				ifp->if_ierrors++;
    597 				goto cleanup;
    598 			}
    599 
    600 			if (len1 >= MINCLSIZE)
    601 				MCLGET(m, M_DONTWAIT);
    602 
    603 			m->m_len = 0;
    604 			dst->m_next = m;
    605 			amount = M_TRAILINGSPACE(m);
    606 		}
    607 
    608 		if (amount < len1)
    609 			len1 = amount;
    610 
    611 		bus_space_read_region_1(bst_m, mem, bah_ram_ptr,
    612 		    mtod(m, u_char *) + m->m_len, len1);
    613 
    614 		m->m_len += len1;
    615 		bah_ram_ptr += len1;
    616 		len -= len1;
    617 	}
    618 
    619 #if NBPFILTER > 0
    620 	if (ifp->if_bpf)
    621 		bpf_mtap(ifp->if_bpf, head);
    622 #endif
    623 
    624 	(*sc->sc_arccom.ac_if.if_input)(&sc->sc_arccom.ac_if, head);
    625 
    626 	head = NULL;
    627 	ifp->if_ipackets++;
    628 
    629 cleanup:
    630 
    631 	if (head != NULL)
    632 		m_freem(head);
    633 
    634 	/* mark buffer as invalid by source id 0 */
    635 	bus_space_write_1(bst_m, mem, buffer*512, 0);
    636 	s = splnet();
    637 
    638 	if (--sc->sc_rx_fillcount == 2 - 1) {
    639 
    640 		/* was off, restart it on buffer just emptied */
    641 		sc->sc_rx_act = buffer;
    642 		sc->sc_intmask |= BAH_RI;
    643 
    644 		/* this also clears the RI flag interupt: */
    645 		PUTREG(BAHCMD, BAH_RXBC(buffer));
    646 		PUTREG(BAHSTAT, sc->sc_intmask);
    647 
    648 #ifdef BAH_DEBUG
    649 		printf("%s: srint: restarted rx on buf %ld\n",
    650 		    sc->sc_dev.dv_xname, buffer);
    651 #endif
    652 	}
    653 	splx(s);
    654 }
    655 
    656 __inline static void
    657 bah_tint(sc, isr)
    658 	struct bah_softc *sc;
    659 	int isr;
    660 {
    661 	struct ifnet *ifp;
    662 
    663 	bus_space_tag_t bst_r = sc->sc_bst_r;
    664 	bus_space_handle_t regs = sc->sc_regs;
    665 
    666 
    667 	int buffer;
    668 #ifdef BAHTIMINGS
    669 	int clknow;
    670 #endif
    671 
    672 	ifp = &(sc->sc_arccom.ac_if);
    673 	buffer = sc->sc_tx_act;
    674 
    675 	/*
    676 	 * retransmit code:
    677 	 * Normal situtations first for fast path:
    678 	 * If acknowledgement received ok or broadcast, we're ok.
    679 	 * else if
    680 	 */
    681 
    682 	if (isr & BAH_TMA || sc->sc_broadcast[buffer])
    683 		sc->sc_arccom.ac_if.if_opackets++;
    684 #ifdef BAHRETRANSMIT
    685 	else if (ifp->if_flags & IFF_LINK2 && ifp->if_timer > 0
    686 	    && --sc->sc_retransmits[buffer] > 0) {
    687 		/* retransmit same buffer */
    688 		PUTREG(BAHCMD, BAH_TX(buffer));
    689 		return;
    690 	}
    691 #endif
    692 	else
    693 		ifp->if_oerrors++;
    694 
    695 
    696 	/* We know we can accept another buffer at this point. */
    697 	ifp->if_flags &= ~IFF_OACTIVE;
    698 
    699 	if (--sc->sc_tx_fillcount > 0) {
    700 
    701 		/*
    702 		 * start tx on other buffer.
    703 		 * This also clears the int flag
    704 		 */
    705 		buffer ^= 1;
    706 		sc->sc_tx_act = buffer;
    707 
    708 		/*
    709 		 * already given:
    710 		 * sc->sc_intmask |= BAH_TA;
    711 		 * PUTREG(BAHSTAT, sc->sc_intmask);
    712 		 */
    713 		PUTREG(BAHCMD, BAH_TX(buffer));
    714 		/* init watchdog timer */
    715 		ifp->if_timer = ARCTIMEOUT;
    716 
    717 #if defined(BAH_DEBUG) && (BAH_DEBUG > 1)
    718 		printf("%s: tint: starting tx on buffer %d, status 0x%02x\n",
    719 		    sc->sc_dev.dv_xname, buffer, GETREG(BAHSTAT));
    720 #endif
    721 	} else {
    722 		/* have to disable TX interrupt */
    723 		sc->sc_intmask &= ~BAH_TA;
    724 		PUTREG(BAHSTAT, sc->sc_intmask);
    725 		/* ... and watchdog timer */
    726 		ifp->if_timer = 0;
    727 
    728 #ifdef BAH_DEBUG
    729 		printf("%s: tint: no more buffers to send, status 0x%02x\n",
    730 		    sc->sc_dev.dv_xname, GETREG(BAHSTAT));
    731 #endif
    732 	}
    733 
    734 	/* XXXX TODO */
    735 #ifdef BAHSOFTCOPY
    736 	/* schedule soft int to fill a new buffer for us */
    737 	softintr_schedule(sc->sc_txcookie);
    738 #else
    739 	/* call it directly */
    740 	bah_start(ifp);
    741 #endif
    742 }
    743 
    744 /*
    745  * Our interrupt routine
    746  */
    747 int
    748 bahintr(arg)
    749 	void *arg;
    750 {
    751 	struct bah_softc *sc = arg;
    752 
    753 	bus_space_tag_t bst_r = sc->sc_bst_r;
    754         bus_space_tag_t bst_m = sc->sc_bst_m;
    755 	bus_space_handle_t regs = sc->sc_regs;
    756 	bus_space_handle_t mem = sc->sc_mem;
    757 
    758 	u_char isr, maskedisr;
    759 	int buffer;
    760 	u_long newsec;
    761 
    762 	isr = GETREG(BAHSTAT);
    763 	maskedisr = isr & sc->sc_intmask;
    764 	if (!maskedisr)
    765 		return (0);
    766 	do {
    767 
    768 #if defined(BAH_DEBUG) && (BAH_DEBUG>1)
    769 		printf("%s: intr: status 0x%02x, intmask 0x%02x\n",
    770 		    sc->sc_dev.dv_xname, isr, sc->sc_intmask);
    771 #endif
    772 
    773 		if (maskedisr & BAH_POR) {
    774 		  	/*
    775 			 * XXX We should never see this. Don't bother to store
    776 			 * the address.
    777 			 * sc->sc_arccom.ac_anaddr = GETMEM(BAHMACOFF);
    778 			 */
    779 			PUTREG(BAHCMD, BAH_CLR(CLR_POR));
    780 			log(LOG_WARNING,
    781 			    "%s: intr: got spurious power on reset int\n",
    782 			    sc->sc_dev.dv_xname);
    783 		}
    784 
    785 		if (maskedisr & BAH_RECON) {
    786 			/*
    787 			 * we dont need to:
    788 			 * PUTREG(BAHCMD, BAH_CONF(CONF_LONG));
    789 			 */
    790 			PUTREG(BAHCMD, BAH_CLR(CLR_RECONFIG));
    791 			sc->sc_arccom.ac_if.if_collisions++;
    792 
    793 			/*
    794 			 * If less than 2 seconds per reconfig:
    795 			 *	If ARC_EXCESSIVE_RECONFIGS
    796 			 *	since last burst, complain and set treshold for
    797 			 *	warnings to ARC_EXCESSIVE_RECONS_REWARN.
    798 			 *
    799 			 * This allows for, e.g., new stations on the cable, or
    800 			 * cable switching as long as it is over after
    801 			 * (normally) 16 seconds.
    802 			 *
    803 			 * XXX TODO: check timeout bits in status word and
    804 			 * double time if necessary.
    805 			 */
    806 
    807 			callout_stop(&sc->sc_recon_ch);
    808 			newsec = time.tv_sec;
    809 			if ((newsec - sc->sc_recontime <= 2) &&
    810 			    (++sc->sc_reconcount == ARC_EXCESSIVE_RECONS)) {
    811 				log(LOG_WARNING,
    812 				    "%s: excessive token losses, "
    813 				    "cable problem?\n", sc->sc_dev.dv_xname);
    814 			}
    815 			sc->sc_recontime = newsec;
    816 			callout_reset(&sc->sc_recon_ch, 15 * hz,
    817 			    bah_reconwatch, (void *)sc);
    818 		}
    819 
    820 		if (maskedisr & BAH_RI) {
    821 #if defined(BAH_DEBUG) && (BAH_DEBUG > 1)
    822 			printf("%s: intr: hard rint, act %ld\n",
    823 			    sc->sc_dev.dv_xname, sc->sc_rx_act);
    824 #endif
    825 
    826 			buffer = sc->sc_rx_act;
    827 			/* look if buffer is marked invalid: */
    828 			if (GETMEM(buffer*512) == 0) {
    829 				/*
    830 				 * invalid marked buffer (or illegally
    831 				 * configured sender)
    832 				 */
    833 				log(LOG_WARNING,
    834 				    "%s: spurious RX interupt or sender 0 "
    835 				    " (ignored)\n", sc->sc_dev.dv_xname);
    836 				/*
    837 				 * restart receiver on same buffer.
    838 				 * XXX maybe better reset interface?
    839 				 */
    840 				PUTREG(BAHCMD, BAH_RXBC(buffer));
    841 			} else {
    842 				if (++sc->sc_rx_fillcount > 1) {
    843 					sc->sc_intmask &= ~BAH_RI;
    844 					PUTREG(BAHSTAT, sc->sc_intmask);
    845 				} else {
    846 					buffer ^= 1;
    847 					sc->sc_rx_act = buffer;
    848 
    849 					/*
    850 					 * Start receiver on other receive
    851 					 * buffer. This also clears the RI
    852 					 * interupt flag.
    853 					 */
    854 					PUTREG(BAHCMD, BAH_RXBC(buffer));
    855 					/* in RX intr, so mask is ok for RX */
    856 
    857 #ifdef BAH_DEBUG
    858 					printf("%s: strt rx for buf %ld, "
    859 					    "stat 0x%02x\n",
    860 					    sc->sc_dev.dv_xname, sc->sc_rx_act,
    861 					    GETREG(BAHSTAT);
    862 #endif
    863 				}
    864 
    865 #ifdef BAHSOFTCOPY
    866 				/*
    867 				 * this one starts a soft int to copy out
    868 				 * of the hw
    869 				 */
    870 				softintr_schedule(sc->sc_rxcookie);
    871 #else
    872 				/* this one does the copy here */
    873 				bah_srint(sc);
    874 #endif
    875 			}
    876 		}
    877 		if (maskedisr & BAH_TA) {
    878 			bah_tint(sc, isr);
    879 		}
    880 		isr = GETREG(BAHSTAT);
    881 		maskedisr = isr & sc->sc_intmask;
    882 	} while (maskedisr);
    883 
    884 	return (1);
    885 }
    886 
    887 void
    888 bah_reconwatch(arg)
    889 	void *arg;
    890 {
    891 	struct bah_softc *sc = arg;
    892 
    893 	if (sc->sc_reconcount >= ARC_EXCESSIVE_RECONS) {
    894 		sc->sc_reconcount = 0;
    895 		log(LOG_WARNING, "%s: token valid again.\n",
    896 		    sc->sc_dev.dv_xname);
    897 	}
    898 	sc->sc_reconcount = 0;
    899 }
    900 
    901 
    902 /*
    903  * Process an ioctl request.
    904  * This code needs some work - it looks pretty ugly.
    905  */
    906 int
    907 bah_ioctl(ifp, command, data)
    908 	struct ifnet *ifp;
    909 	u_long command;
    910 	caddr_t data;
    911 {
    912 	struct bah_softc *sc;
    913 	struct ifaddr *ifa;
    914 	struct ifreq *ifr;
    915 	int s, error;
    916 
    917 	error = 0;
    918 	sc = ifp->if_softc;
    919 	ifa = (struct ifaddr *)data;
    920 	ifr = (struct ifreq *)data;
    921 	s = splnet();
    922 
    923 #if defined(BAH_DEBUG) && (BAH_DEBUG > 2)
    924 	printf("%s: ioctl() called, cmd = 0x%x\n",
    925 	    sc->sc_dev.dv_xname, command);
    926 #endif
    927 
    928 	switch (command) {
    929 	case SIOCSIFADDR:
    930 		ifp->if_flags |= IFF_UP;
    931 		switch (ifa->ifa_addr->sa_family) {
    932 #ifdef INET
    933 		case AF_INET:
    934 			bah_init(sc);
    935 			arp_ifinit(ifp, ifa);
    936 			break;
    937 #endif
    938 		default:
    939 			bah_init(sc);
    940 			break;
    941 		}
    942 
    943 	case SIOCSIFFLAGS:
    944 		if ((ifp->if_flags & IFF_UP) == 0 &&
    945 		    (ifp->if_flags & IFF_RUNNING) != 0) {
    946 			/*
    947 			 * If interface is marked down and it is running,
    948 			 * then stop it.
    949 			 */
    950 			bah_stop(sc);
    951 			ifp->if_flags &= ~IFF_RUNNING;
    952 		} else if ((ifp->if_flags & IFF_UP) != 0 &&
    953 			   (ifp->if_flags & IFF_RUNNING) == 0) {
    954 			/*
    955 			 * If interface is marked up and it is stopped, then
    956 			 * start it.
    957 			 */
    958 			bah_init(sc);
    959 		}
    960 		break;
    961 
    962 	case SIOCADDMULTI:
    963 	case SIOCDELMULTI:
    964 		switch (ifr->ifr_addr.sa_family) {
    965 		case AF_INET:
    966 		case AF_INET6:
    967 			error = 0;
    968 			break;
    969 		default:
    970 			error = EAFNOSUPPORT;
    971 			break;
    972 		}
    973 		break;
    974 
    975 	default:
    976 		error = EINVAL;
    977 	}
    978 
    979 	splx(s);
    980 	return (error);
    981 }
    982 
    983 /*
    984  * watchdog routine for transmitter.
    985  *
    986  * We need this, because else a receiver whose hardware is alive, but whose
    987  * software has not enabled the Receiver, would make our hardware wait forever
    988  * Discovered this after 20 times reading the docs.
    989  *
    990  * Only thing we do is disable transmitter. We'll get an transmit timeout,
    991  * and the int handler will have to decide not to retransmit (in case
    992  * retransmission is implemented).
    993  *
    994  * This one assumes being called inside splnet()
    995  */
    996 
    997 void
    998 bah_watchdog(ifp)
    999 	struct ifnet *ifp;
   1000 {
   1001 	struct bah_softc *sc = ifp->if_softc;
   1002 
   1003 	bus_space_tag_t bst_r = sc->sc_bst_r;
   1004 	bus_space_handle_t regs = sc->sc_regs;
   1005 
   1006 	PUTREG(BAHCMD, BAH_TXDIS);
   1007 	return;
   1008 }
   1009 
   1010