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