Home | History | Annotate | Line # | Download | only in ic
smc90cx6.c revision 1.60
      1 /*	$NetBSD: smc90cx6.c,v 1.60 2009/05/31 14:11:17 he 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  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 /*
     33  * Chip core driver for the SMC90c26 / SMC90c56 (and SMC90c66 in '56
     34  * compatibility mode) boards
     35  */
     36 
     37 #include <sys/cdefs.h>
     38 __KERNEL_RCSID(0, "$NetBSD: smc90cx6.c,v 1.60 2009/05/31 14:11:17 he Exp $");
     39 
     40 /* #define BAHSOFTCOPY */
     41 #define BAHRETRANSMIT /**/
     42 
     43 #include "opt_inet.h"
     44 #include "bpfilter.h"
     45 
     46 #include <sys/param.h>
     47 #include <sys/systm.h>
     48 #include <sys/mbuf.h>
     49 #include <sys/buf.h>
     50 #include <sys/device.h>
     51 #include <sys/protosw.h>
     52 #include <sys/socket.h>
     53 #include <sys/syslog.h>
     54 #include <sys/ioctl.h>
     55 #include <sys/errno.h>
     56 #include <sys/kernel.h>
     57 #include <sys/intr.h>
     58 
     59 #include <net/if.h>
     60 #include <net/if_dl.h>
     61 #include <net/if_ether.h>
     62 #include <net/if_types.h>
     63 #include <net/if_arc.h>
     64 
     65 #ifdef INET
     66 #include <netinet/in.h>
     67 #include <netinet/in_systm.h>
     68 #include <netinet/in_var.h>
     69 #include <netinet/ip.h>
     70 #include <netinet/if_inarp.h>
     71 #endif
     72 
     73 #if NBPFILTER > 0
     74 #include <net/bpf.h>
     75 #include <net/bpfdesc.h>
     76 #endif
     77 
     78 #include <sys/bus.h>
     79 #include <sys/cpu.h>
     80 
     81 #include <dev/ic/smc90cx6reg.h>
     82 #include <dev/ic/smc90cx6var.h>
     83 
     84 /* these should be elsewhere */
     85 
     86 #define ARC_MIN_LEN 1
     87 #define ARC_MIN_FORBID_LEN 254
     88 #define ARC_MAX_FORBID_LEN 256
     89 #define ARC_MAX_LEN 508
     90 #define ARC_ADDR_LEN 1
     91 
     92 /* for watchdog timer. This should be more than enough. */
     93 #define ARCTIMEOUT (5*IFNET_SLOWHZ)
     94 
     95 /*
     96  * This currently uses 2 bufs for tx, 2 for rx
     97  *
     98  * New rx protocol:
     99  *
    100  * rx has a fillcount variable. If fillcount > (NRXBUF-1),
    101  * rx can be switched off from rx hard int.
    102  * Else rx is restarted on the other receiver.
    103  * rx soft int counts down. if it is == (NRXBUF-1), it restarts
    104  * the receiver.
    105  * To ensure packet ordering (we need that for 1201 later), we have a counter
    106  * which is incremented modulo 256 on each receive and a per buffer
    107  * variable, which is set to the counter on filling. The soft int can
    108  * compare both values to determine the older packet.
    109  *
    110  * Transmit direction:
    111  *
    112  * bah_start checks tx_fillcount
    113  * case 2: return
    114  *
    115  * else fill tx_act ^ 1 && inc tx_fillcount
    116  *
    117  * check tx_fillcount again.
    118  * case 2: set IFF_OACTIVE to stop arc_output from filling us.
    119  * case 1: start tx
    120  *
    121  * tint clears IFF_OCATIVE, decrements and checks tx_fillcount
    122  * case 1: start tx on tx_act ^ 1, softcall bah_start
    123  * case 0: softcall bah_start
    124  *
    125  * #define fill(i) get mbuf && copy mbuf to chip(i)
    126  */
    127 
    128 void	bah_init(struct bah_softc *);
    129 void	bah_reset(struct bah_softc *);
    130 void	bah_stop(struct bah_softc *);
    131 void	bah_start(struct ifnet *);
    132 int	bahintr(void *);
    133 int	bah_ioctl(struct ifnet *, unsigned long, void *);
    134 void	bah_watchdog(struct ifnet *);
    135 void	bah_srint(void *vsc);
    136 static	void bah_tint(struct bah_softc *, int);
    137 void	bah_reconwatch(void *);
    138 
    139 /* short notation */
    140 
    141 #define GETREG(off)	bus_space_read_1(bst_r, regs, (off))
    142 #define PUTREG(off, v)	bus_space_write_1(bst_r, regs, (off), (v))
    143 #define GETMEM(off)	bus_space_read_1(bst_m, mem, (off))
    144 #define PUTMEM(off, v)	bus_space_write_1(bst_m, mem, (off), (v))
    145 
    146 void
    147 bah_attach_subr(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 	    device_xname(&sc->sc_dev), 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 	strlcpy(ifp->if_xname, device_xname(&sc->sc_dev), IFNAMSIZ);
    196 	ifp->if_softc = sc;
    197 	ifp->if_start = bah_start;
    198 	ifp->if_ioctl = bah_ioctl;
    199 	ifp->if_timer = 0;
    200 	ifp->if_watchdog  = bah_watchdog;
    201 	IFQ_SET_READY(&ifp->if_snd);
    202 
    203 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS;
    204 
    205 	ifp->if_mtu = ARCMTU;
    206 
    207 	arc_ifattach(ifp, linkaddress);
    208 
    209 #ifdef BAHSOFTCOPY
    210 	sc->sc_rxcookie = softint_establish(SOFTINT_NET, bah_srint, sc);
    211 	sc->sc_txcookie = softint_establish(SOFTINT_NET,
    212 		(void (*)(void *))bah_start, ifp);
    213 #endif
    214 
    215 	callout_init(&sc->sc_recon_ch, 0);
    216 }
    217 
    218 /*
    219  * Initialize device
    220  *
    221  */
    222 void
    223 bah_init(struct bah_softc *sc)
    224 {
    225 	struct ifnet *ifp;
    226 	int s;
    227 
    228 	ifp = &sc->sc_arccom.ac_if;
    229 
    230 	if ((ifp->if_flags & IFF_RUNNING) == 0) {
    231 		s = splnet();
    232 		ifp->if_flags |= IFF_RUNNING;
    233 		bah_reset(sc);
    234 		bah_start(ifp);
    235 		splx(s);
    236 	}
    237 }
    238 
    239 /*
    240  * Reset the interface...
    241  *
    242  * this assumes that it is called inside a critical section...
    243  *
    244  */
    245 void
    246 bah_reset(struct bah_softc *sc)
    247 {
    248 	struct ifnet *ifp;
    249 	uint8_t linkaddress;
    250 
    251 	bus_space_tag_t bst_r = sc->sc_bst_r;
    252         bus_space_tag_t bst_m = sc->sc_bst_m;
    253 	bus_space_handle_t regs = sc->sc_regs;
    254 	bus_space_handle_t mem = sc->sc_mem;
    255 
    256 	ifp = &sc->sc_arccom.ac_if;
    257 
    258 #ifdef BAH_DEBUG
    259 	printf("%s: reset\n", device_xname(&sc->sc_dev));
    260 #endif
    261 	/* stop and restart hardware */
    262 
    263 	(*sc->sc_reset)(sc, 1);
    264 	do {
    265 		DELAY(200);
    266 	} while (!(GETREG(BAHSTAT) & BAH_POR));
    267 
    268 	linkaddress = GETMEM(BAHMACOFF);
    269 
    270 #if defined(BAH_DEBUG) && (BAH_DEBUG > 2)
    271 	printf("%s: reset: card reset, link addr = 0x%02x (%ld)\n",
    272 	    device_xname(&sc->sc_dev), linkaddress, linkaddress);
    273 #endif
    274 
    275 	/* tell the routing level about the (possibly changed) link address */
    276 	if_set_sadl(ifp, &linkaddress, sizeof(linkaddress), false);
    277 
    278 	/* POR is NMI, but we need it below: */
    279 	sc->sc_intmask = BAH_RECON|BAH_POR;
    280 	PUTREG(BAHSTAT, sc->sc_intmask);
    281 	PUTREG(BAHCMD, BAH_CONF(CONF_LONG));
    282 
    283 #ifdef BAH_DEBUG
    284 	printf("%s: reset: chip configured, status=0x%02x\n",
    285 	    device_xname(&sc->sc_dev), GETREG(BAHSTAT));
    286 #endif
    287 	PUTREG(BAHCMD, BAH_CLR(CLR_POR|CLR_RECONFIG));
    288 
    289 #ifdef BAH_DEBUG
    290 	printf("%s: reset: bits cleared, status=0x%02x\n",
    291 	    device_xname(&sc->sc_dev), GETREG(BAHSTAT);
    292 #endif
    293 
    294 	sc->sc_reconcount_excessive = ARC_EXCESSIVE_RECONS;
    295 
    296 	/* start receiver */
    297 
    298 	sc->sc_intmask  |= BAH_RI;
    299 	sc->sc_rx_fillcount = 0;
    300 	sc->sc_rx_act = 2;
    301 
    302 	PUTREG(BAHCMD, BAH_RXBC(2));
    303 	PUTREG(BAHSTAT, sc->sc_intmask);
    304 
    305 #ifdef BAH_DEBUG
    306 	printf("%s: reset: started receiver, status=0x%02x\n",
    307 	    device_xname(&sc->sc_dev), GETREG(BAHSTAT);
    308 #endif
    309 
    310 	/* and init transmitter status */
    311 	sc->sc_tx_act = 0;
    312 	sc->sc_tx_fillcount = 0;
    313 
    314 	ifp->if_flags |= IFF_RUNNING;
    315 	ifp->if_flags &= ~IFF_OACTIVE;
    316 
    317 	bah_start(ifp);
    318 }
    319 
    320 /*
    321  * Take interface offline
    322  */
    323 void
    324 bah_stop(struct bah_softc *sc)
    325 {
    326 	bus_space_tag_t bst_r = sc->sc_bst_r;
    327 	bus_space_handle_t regs = sc->sc_regs;
    328 
    329 	/* Stop the interrupts */
    330 	PUTREG(BAHSTAT, 0);
    331 
    332 	/* Stop the interface */
    333 	(*sc->sc_reset)(sc, 0);
    334 
    335 	/* Stop watchdog timer */
    336 	sc->sc_arccom.ac_if.if_timer = 0;
    337 }
    338 
    339 /*
    340  * Start output on interface. Get another datagram to send
    341  * off the interface queue, and copy it to the
    342  * interface before starting the output
    343  *
    344  * this assumes that it is called inside a critical section...
    345  * XXX hm... does it still?
    346  *
    347  */
    348 void
    349 bah_start(struct ifnet *ifp)
    350 {
    351 	struct bah_softc *sc = ifp->if_softc;
    352 	struct mbuf *m,*mp;
    353 
    354 	bus_space_tag_t bst_r = sc->sc_bst_r;
    355 	bus_space_handle_t regs = sc->sc_regs;
    356 	bus_space_tag_t bst_m = sc->sc_bst_m;
    357 	bus_space_handle_t mem = sc->sc_mem;
    358 
    359 	int bah_ram_ptr;
    360 	int len, tlen, offset, s, buffer;
    361 #ifdef BAHTIMINGS
    362 	u_long copystart, lencopy, perbyte;
    363 #endif
    364 
    365 #if defined(BAH_DEBUG) && (BAH_DEBUG > 3)
    366 	printf("%s: start(0x%x)\n", device_xname(&sc->sc_dev), ifp);
    367 #endif
    368 
    369 	if ((ifp->if_flags & IFF_RUNNING) == 0)
    370 		return;
    371 
    372 	s = splnet();
    373 
    374 	if (sc->sc_tx_fillcount >= 2) {
    375 		splx(s);
    376 		return;
    377 	}
    378 
    379 	IFQ_DEQUEUE(&ifp->if_snd, m);
    380 	buffer = sc->sc_tx_act ^ 1;
    381 
    382 	splx(s);
    383 
    384 	if (m == 0)
    385 		return;
    386 
    387 #if NBPFILTER > 0
    388 	/*
    389 	 * If bpf is listening on this interface, let it
    390 	 * see the packet before we commit it to the wire
    391 	 *
    392 	 * (can't give the copy in A2060 card RAM to bpf, because
    393 	 * that RAM is just accessed as on every other byte)
    394 	 */
    395 	if (ifp->if_bpf)
    396 		bpf_mtap(ifp->if_bpf, m);
    397 #endif
    398 
    399 #ifdef BAH_DEBUG
    400 	if (m->m_len < ARC_HDRLEN)
    401 		m = m_pullup(m, ARC_HDRLEN);/* gcc does structure padding */
    402 	printf("%s: start: filling %ld from %ld to %ld type %ld\n",
    403 	    device_xname(&sc->sc_dev), buffer, mtod(m, u_char *)[0],
    404 	    mtod(m, u_char *)[1], mtod(m, u_char *)[2]);
    405 #else
    406 	if (m->m_len < 2)
    407 		m = m_pullup(m, 2);
    408 #endif
    409 	bah_ram_ptr = buffer*512;
    410 
    411 	if (m == 0)
    412 		return;
    413 
    414 	/* write the addresses to RAM and throw them away */
    415 
    416 	/*
    417 	 * Hardware does this: Yet Another Microsecond Saved.
    418 	 * (btw, timing code says usually 2 microseconds)
    419 	 * PUTMEM(bah_ram_ptr + 0, mtod(m, u_char *)[0]);
    420 	 */
    421 
    422 	PUTMEM(bah_ram_ptr + 1, mtod(m, u_char *)[1]);
    423 	m_adj(m, 2);
    424 
    425 	/* get total length left at this point */
    426 	tlen = m->m_pkthdr.len;
    427 	if (tlen < ARC_MIN_FORBID_LEN) {
    428 		offset = 256 - tlen;
    429 		PUTMEM(bah_ram_ptr + 2, offset);
    430 	} else {
    431 		PUTMEM(bah_ram_ptr + 2, 0);
    432 		if (tlen <= ARC_MAX_FORBID_LEN)
    433 			offset = 255;		/* !!! */
    434 		else {
    435 			if (tlen > ARC_MAX_LEN)
    436 				tlen = ARC_MAX_LEN;
    437 			offset = 512 - tlen;
    438 		}
    439 		PUTMEM(bah_ram_ptr + 3, offset);
    440 
    441 	}
    442 	bah_ram_ptr += offset;
    443 
    444 	/* lets loop through the mbuf chain */
    445 
    446 	for (mp = m; mp; mp = mp->m_next) {
    447 		if ((len = mp->m_len)) {		/* YAMS */
    448 			bus_space_write_region_1(bst_m, mem, bah_ram_ptr,
    449 			    mtod(mp, void *), len);
    450 
    451 			bah_ram_ptr += len;
    452 		}
    453 	}
    454 
    455 	sc->sc_broadcast[buffer] = (m->m_flags & M_BCAST) != 0;
    456 	sc->sc_retransmits[buffer] = (m->m_flags & M_BCAST) ? 1 : 5;
    457 
    458 	/* actually transmit the packet */
    459 	s = splnet();
    460 
    461 	if (++sc->sc_tx_fillcount > 1) {
    462 		/*
    463 		 * We are filled up to the rim. No more bufs for the moment,
    464 		 * please.
    465 		 */
    466 		ifp->if_flags |= IFF_OACTIVE;
    467 	} else {
    468 #ifdef BAH_DEBUG
    469 		printf("%s: start: starting transmitter on buffer %d\n",
    470 		    device_xname(&sc->sc_dev), buffer);
    471 #endif
    472 		/* Transmitter was off, start it */
    473 		sc->sc_tx_act = buffer;
    474 
    475 		/*
    476 		 * We still can accept another buf, so don't:
    477 		 * ifp->if_flags |= IFF_OACTIVE;
    478 		 */
    479 		sc->sc_intmask |= BAH_TA;
    480 		PUTREG(BAHCMD, BAH_TX(buffer));
    481 		PUTREG(BAHSTAT, sc->sc_intmask);
    482 
    483 		sc->sc_arccom.ac_if.if_timer = ARCTIMEOUT;
    484 	}
    485 	splx(s);
    486 	m_freem(m);
    487 
    488 	/*
    489 	 * After 10 times reading the docs, I realized
    490 	 * that in the case the receiver NAKs the buffer request,
    491 	 * the hardware retries till shutdown.
    492 	 * This is integrated now in the code above.
    493 	 */
    494 
    495 	return;
    496 }
    497 
    498 /*
    499  * Arcnet interface receiver soft interrupt:
    500  * get the stuff out of any filled buffer we find.
    501  */
    502 void
    503 bah_srint(void *vsc)
    504 {
    505 	struct bah_softc *sc = (struct bah_softc *)vsc;
    506 	int buffer, len, len1, amount, offset, s, type;
    507 	int bah_ram_ptr;
    508 	struct mbuf *m, *dst, *head;
    509 	struct arc_header *ah;
    510 	struct ifnet *ifp;
    511 
    512 	bus_space_tag_t bst_r = sc->sc_bst_r;
    513         bus_space_tag_t bst_m = sc->sc_bst_m;
    514 	bus_space_handle_t regs = sc->sc_regs;
    515 	bus_space_handle_t mem = sc->sc_mem;
    516 
    517 	ifp = &sc->sc_arccom.ac_if;
    518 	head = 0;
    519 
    520 	s = splnet();
    521 	buffer = sc->sc_rx_act ^ 1;
    522 	splx(s);
    523 
    524 	/* Allocate header mbuf */
    525 	MGETHDR(m, M_DONTWAIT, MT_DATA);
    526 
    527 	if (m == 0) {
    528 		/*
    529 	 	 * in case s.th. goes wrong with mem, drop it
    530 	 	 * to make sure the receiver can be started again
    531 		 * count it as input error (we dont have any other
    532 		 * detectable)
    533 	 	 */
    534 		ifp->if_ierrors++;
    535 		goto cleanup;
    536 	}
    537 
    538 	m->m_pkthdr.rcvif = ifp;
    539 
    540 	/*
    541 	 * Align so that IP packet will be longword aligned. Here we
    542 	 * assume that m_data of new packet is longword aligned.
    543 	 * When implementing PHDS, we might have to change it to 2,
    544 	 * (2*sizeof(ulong) - ARC_HDRNEWLEN)), packet type dependent.
    545 	 */
    546 
    547 	bah_ram_ptr = buffer*512;
    548 	offset = GETMEM(bah_ram_ptr + 2);
    549 	if (offset)
    550 		len = 256 - offset;
    551 	else {
    552 		offset = GETMEM(bah_ram_ptr + 3);
    553 		len = 512 - offset;
    554 	}
    555 	if (len+2 >= MINCLSIZE)
    556 		MCLGET(m, M_DONTWAIT);
    557 
    558 	if (m == 0) {
    559 		ifp->if_ierrors++;
    560 		goto cleanup;
    561 	}
    562 
    563 	type = GETMEM(bah_ram_ptr + offset);
    564 	m->m_data += 1 + arc_isphds(type);
    565 
    566 	head = m;
    567 	ah = mtod(head, struct arc_header *);
    568 
    569 	ah->arc_shost = GETMEM(bah_ram_ptr + 0);
    570 	ah->arc_dhost = GETMEM(bah_ram_ptr + 1);
    571 
    572 	m->m_pkthdr.len = len+2; /* whole packet length */
    573 	m->m_len = 2;		 /* mbuf filled with ARCnet addresses */
    574 	bah_ram_ptr += offset;	/* ram buffer continues there */
    575 
    576 	while (len > 0) {
    577 
    578 		len1 = len;
    579 		amount = M_TRAILINGSPACE(m);
    580 
    581 		if (amount == 0) {
    582 			dst = m;
    583 			MGET(m, M_DONTWAIT, MT_DATA);
    584 
    585 			if (m == 0) {
    586 				ifp->if_ierrors++;
    587 				goto cleanup;
    588 			}
    589 
    590 			if (len1 >= MINCLSIZE)
    591 				MCLGET(m, M_DONTWAIT);
    592 
    593 			m->m_len = 0;
    594 			dst->m_next = m;
    595 			amount = M_TRAILINGSPACE(m);
    596 		}
    597 
    598 		if (amount < len1)
    599 			len1 = amount;
    600 
    601 		bus_space_read_region_1(bst_m, mem, bah_ram_ptr,
    602 		    mtod(m, u_char *) + m->m_len, len1);
    603 
    604 		m->m_len += len1;
    605 		bah_ram_ptr += len1;
    606 		len -= len1;
    607 	}
    608 
    609 #if NBPFILTER > 0
    610 	if (ifp->if_bpf)
    611 		bpf_mtap(ifp->if_bpf, head);
    612 #endif
    613 
    614 	(*sc->sc_arccom.ac_if.if_input)(&sc->sc_arccom.ac_if, head);
    615 
    616 	head = NULL;
    617 	ifp->if_ipackets++;
    618 
    619 cleanup:
    620 
    621 	if (head != NULL)
    622 		m_freem(head);
    623 
    624 	/* mark buffer as invalid by source id 0 */
    625 	bus_space_write_1(bst_m, mem, buffer*512, 0);
    626 	s = splnet();
    627 
    628 	if (--sc->sc_rx_fillcount == 2 - 1) {
    629 
    630 		/* was off, restart it on buffer just emptied */
    631 		sc->sc_rx_act = buffer;
    632 		sc->sc_intmask |= BAH_RI;
    633 
    634 		/* this also clears the RI flag interrupt: */
    635 		PUTREG(BAHCMD, BAH_RXBC(buffer));
    636 		PUTREG(BAHSTAT, sc->sc_intmask);
    637 
    638 #ifdef BAH_DEBUG
    639 		printf("%s: srint: restarted rx on buf %ld\n",
    640 		    device_xname(&sc->sc_dev), buffer);
    641 #endif
    642 	}
    643 	splx(s);
    644 }
    645 
    646 inline static void
    647 bah_tint(struct bah_softc *sc, 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 situations 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 		    device_xname(&sc->sc_dev), 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 		    device_xname(&sc->sc_dev), GETREG(BAHSTAT));
    719 #endif
    720 	}
    721 
    722 	/* XXXX TODO */
    723 #ifdef BAHSOFTCOPY
    724 	/* schedule soft int to fill a new buffer for us */
    725 	softint_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(void *arg)
    737 {
    738 	struct bah_softc *sc = arg;
    739 
    740 	bus_space_tag_t bst_r = sc->sc_bst_r;
    741         bus_space_tag_t bst_m = sc->sc_bst_m;
    742 	bus_space_handle_t regs = sc->sc_regs;
    743 	bus_space_handle_t mem = sc->sc_mem;
    744 
    745 	u_char isr, maskedisr;
    746 	int buffer;
    747 	u_long newsec;
    748 
    749 	isr = GETREG(BAHSTAT);
    750 	maskedisr = isr & sc->sc_intmask;
    751 	if (!maskedisr)
    752 		return (0);
    753 	do {
    754 
    755 #if defined(BAH_DEBUG) && (BAH_DEBUG>1)
    756 		printf("%s: intr: status 0x%02x, intmask 0x%02x\n",
    757 		    device_xname(&sc->sc_dev), isr, sc->sc_intmask);
    758 #endif
    759 
    760 		if (maskedisr & BAH_POR) {
    761 		  	/*
    762 			 * XXX We should never see this. Don't bother to store
    763 			 * the address.
    764 			 * sc->sc_arccom.ac_anaddr = GETMEM(BAHMACOFF);
    765 			 */
    766 			PUTREG(BAHCMD, BAH_CLR(CLR_POR));
    767 			log(LOG_WARNING,
    768 			    "%s: intr: got spurious power on reset int\n",
    769 			    device_xname(&sc->sc_dev));
    770 		}
    771 
    772 		if (maskedisr & BAH_RECON) {
    773 			/*
    774 			 * we dont need to:
    775 			 * PUTREG(BAHCMD, BAH_CONF(CONF_LONG));
    776 			 */
    777 			PUTREG(BAHCMD, BAH_CLR(CLR_RECONFIG));
    778 			sc->sc_arccom.ac_if.if_collisions++;
    779 
    780 			/*
    781 			 * If less than 2 seconds per reconfig:
    782 			 *	If ARC_EXCESSIVE_RECONFIGS
    783 			 *	since last burst, complain and set threshold for
    784 			 *	warnings to ARC_EXCESSIVE_RECONS_REWARN.
    785 			 *
    786 			 * This allows for, e.g., new stations on the cable, or
    787 			 * cable switching as long as it is over after
    788 			 * (normally) 16 seconds.
    789 			 *
    790 			 * XXX TODO: check timeout bits in status word and
    791 			 * double time if necessary.
    792 			 */
    793 
    794 			callout_stop(&sc->sc_recon_ch);
    795 			newsec = time_second;
    796 			if ((newsec - sc->sc_recontime <= 2) &&
    797 			    (++sc->sc_reconcount == ARC_EXCESSIVE_RECONS)) {
    798 				log(LOG_WARNING,
    799 				    "%s: excessive token losses, "
    800 				    "cable problem?\n", device_xname(&sc->sc_dev));
    801 			}
    802 			sc->sc_recontime = newsec;
    803 			callout_reset(&sc->sc_recon_ch, 15 * hz,
    804 			    bah_reconwatch, (void *)sc);
    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 			    device_xname(&sc->sc_dev), 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 interrupt or sender 0 "
    822 				    " (ignored)\n", device_xname(&sc->sc_dev));
    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 					 * interrupt 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 					    device_xname(&sc->sc_dev), 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 				softint_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(void *arg)
    876 {
    877 	struct bah_softc *sc = arg;
    878 
    879 	if (sc->sc_reconcount >= ARC_EXCESSIVE_RECONS) {
    880 		sc->sc_reconcount = 0;
    881 		log(LOG_WARNING, "%s: token valid again.\n",
    882 		    device_xname(&sc->sc_dev));
    883 	}
    884 	sc->sc_reconcount = 0;
    885 }
    886 
    887 
    888 /*
    889  * Process an ioctl request.
    890  * This code needs some work - it looks pretty ugly.
    891  */
    892 int
    893 bah_ioctl(struct ifnet *ifp, u_long cmd, void *data)
    894 {
    895 	struct bah_softc *sc;
    896 	struct ifaddr *ifa;
    897 	struct ifreq *ifr;
    898 	int s, error;
    899 
    900 	error = 0;
    901 	sc = ifp->if_softc;
    902 	ifa = (struct ifaddr *)data;
    903 	ifr = (struct ifreq *)data;
    904 	s = splnet();
    905 
    906 #if defined(BAH_DEBUG) && (BAH_DEBUG > 2)
    907 	printf("%s: ioctl() called, cmd = 0x%x\n",
    908 	    device_xname(&sc->sc_dev), cmd);
    909 #endif
    910 
    911 	switch (cmd) {
    912 	case SIOCINITIFADDR:
    913 		ifp->if_flags |= IFF_UP;
    914 		bah_init(sc);
    915 		switch (ifa->ifa_addr->sa_family) {
    916 #ifdef INET
    917 		case AF_INET:
    918 			arp_ifinit(ifp, ifa);
    919 			break;
    920 #endif
    921 		default:
    922 			break;
    923 		}
    924 
    925 	case SIOCSIFFLAGS:
    926 		if ((error = ifioctl_common(ifp, cmd, data)) != 0)
    927 			break;
    928 		/* XXX re-use ether_ioctl() */
    929 		switch (ifp->if_flags & (IFF_UP|IFF_RUNNING)) {
    930 		case IFF_RUNNING:
    931 			/*
    932 			 * If interface is marked down and it is running,
    933 			 * then stop it.
    934 			 */
    935 			bah_stop(sc);
    936 			ifp->if_flags &= ~IFF_RUNNING;
    937 			break;
    938 		case IFF_UP:
    939 			/*
    940 			 * If interface is marked up and it is stopped, then
    941 			 * start it.
    942 			 */
    943 			bah_init(sc);
    944 			break;
    945 		}
    946 		break;
    947 
    948 	case SIOCADDMULTI:
    949 	case SIOCDELMULTI:
    950 		switch (ifreq_getaddr(cmd, ifr)->sa_family) {
    951 		case AF_INET:
    952 		case AF_INET6:
    953 			error = 0;
    954 			break;
    955 		default:
    956 			error = EAFNOSUPPORT;
    957 			break;
    958 		}
    959 		break;
    960 
    961 	default:
    962 		error = ether_ioctl(ifp, cmd, data);
    963 	}
    964 
    965 	splx(s);
    966 	return (error);
    967 }
    968 
    969 /*
    970  * watchdog routine for transmitter.
    971  *
    972  * We need this, because else a receiver whose hardware is alive, but whose
    973  * software has not enabled the Receiver, would make our hardware wait forever
    974  * Discovered this after 20 times reading the docs.
    975  *
    976  * Only thing we do is disable transmitter. We'll get an transmit timeout,
    977  * and the int handler will have to decide not to retransmit (in case
    978  * retransmission is implemented).
    979  *
    980  * This one assumes being called inside splnet()
    981  */
    982 
    983 void
    984 bah_watchdog(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