Home | History | Annotate | Line # | Download | only in ic
smc90cx6.c revision 1.15
      1 /*	$NetBSD: smc90cx6.c,v 1.15 1996/03/17 01:17:30 thorpej Exp $ */
      2 
      3 /*
      4  * Copyright (c) 1994, 1995 Ignatios Souvatzis
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *      This product includes software developed by Ignatios Souvatzis
     18  *      for the NetBSD Project.
     19  * 4. The name of the author may not be used to endorse or promote products
     20  *    derived from this software without specific prior written permission
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     25  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     27  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     31  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     32  */
     33 
     34 /*
     35  * Driver for the Commodore Busines Machines ARCnet card.
     36  */
     37 
     38 #define BAHASMCOPY /**/
     39 #define BAHSOFTCOPY /**/
     40 #define BAHRETRANSMIT /**/
     41 /* #define BAHTIMINGS */
     42 /* #define BAH_DEBUG 3 */
     43 
     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 
     57 #include <net/if.h>
     58 #include <net/if_dl.h>
     59 #include <net/if_types.h>
     60 #include <net/netisr.h>
     61 
     62 #ifdef INET
     63 #include <netinet/in.h>
     64 #include <netinet/in_systm.h>
     65 #include <netinet/in_var.h>
     66 #include <netinet/ip.h>
     67 #include <netinet/if_ether.h>
     68 #include <netinet/if_arc.h>
     69 #endif
     70 
     71 #if NBPFILTER > 0
     72 #include <net/bpf.h>
     73 #include <net/bpfdesc.h>
     74 #endif
     75 
     76 #include <sys/kernel.h>
     77 #include <machine/cpu.h>
     78 #include <machine/mtpr.h>
     79 
     80 #include <amiga/amiga/device.h>
     81 #include <amiga/amiga/isr.h>
     82 #include <amiga/dev/zbusvar.h>
     83 #include <amiga/dev/if_bahreg.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 #ifdef BAHTIMINGS
    130 /*
    131  * ARCnet stats; per interface.
    132  */
    133 struct bah_stats {
    134 	u_long	mincopyin;
    135 	u_long	maxcopyin;		/* divided by byte count */
    136 	u_long	mincopyout;
    137 	u_long	maxcopyout;
    138 	u_long	minsend;
    139 	u_long	maxsend;
    140 	u_long	lasttxstart_mics;
    141 	struct	timeval lasttxstart_tv;
    142 };
    143 
    144 #error BAHTIMINGS CODE IS BROKEN; use of clkread() is bogus
    145 #endif
    146 
    147 /*
    148  * Arcnet software status per interface
    149  */
    150 struct bah_softc {
    151 	struct	device	sc_dev;
    152 	struct	arccom	sc_arccom;	/* Common arcnet structures */
    153 	struct	isr	sc_isr;
    154 	struct	a2060	*sc_base;
    155 	u_long	sc_recontime;		/* seconds only, I'm lazy */
    156 	u_long	sc_reconcount;		/* for the above */
    157 	u_long	sc_reconcount_excessive; /* for the above */
    158 #define ARC_EXCESSIVE_RECONS 20
    159 #define ARC_EXCESSIVE_RECONS_REWARN 400
    160 	u_char	sc_intmask;
    161 	u_char	sc_rx_act;		/* 2..3 */
    162 	u_char	sc_tx_act;		/* 0..1 */
    163 	u_char	sc_rx_fillcount;
    164 	u_char	sc_tx_fillcount;
    165 	u_char	sc_broadcast[2];	/* is it a broadcast packet? */
    166 	u_char	sc_retransmits[2];	/* unused at the moment */
    167 #ifdef BAHTIMINGS
    168 	struct	bah_stats sc_stats;
    169 #endif
    170 };
    171 
    172 int	bah_zbus_match __P((struct device *, void *, void *));
    173 void	bah_zbus_attach __P((struct device *, struct device *, void *));
    174 void	bah_init __P((struct bah_softc *));
    175 void	bah_reset __P((struct bah_softc *));
    176 void	bah_stop __P((struct bah_softc *));
    177 void	bah_start __P((struct ifnet *));
    178 int	bahintr __P((struct bah_softc *sc));
    179 int	bah_ioctl __P((struct ifnet *, unsigned long, caddr_t));
    180 void	bah_watchdog __P((int));
    181 void	movepout __P((u_char *from, u_char __volatile *to, int len));
    182 void	movepin __P((u_char __volatile *from, u_char *to, int len));
    183 void	bah_srint __P((void *vsc, void *dummy));
    184 void	callstart __P((void *vsc, void *dummy));
    185 
    186 #ifdef BAHTIMINGS
    187 int	clkread();
    188 #endif
    189 
    190 struct cfattach bah_zbus_ca = {
    191 	sizeof(struct bah_softc), bah_zbus_match, bah_zbus_attach
    192 };
    193 
    194 struct cfdriver bah_cd = {
    195 	NULL, "bah", DV_IFNET
    196 };
    197 
    198 int
    199 bah_zbus_match(parent, match, aux)
    200 	struct device *parent;
    201 	void *match, *aux;
    202 {
    203 	struct zbus_args *zap = aux;
    204 
    205 	if ((zap->manid == 514 || zap->manid == 1053) && zap->prodid == 9)
    206 		return (1);
    207 
    208 	return (0);
    209 }
    210 
    211 void
    212 bah_zbus_attach(parent, self, aux)
    213 	struct device *parent, *self;
    214 	void *aux;
    215 {
    216 	struct bah_softc *sc = (void *)self;
    217 	struct zbus_args *zap = aux;
    218 	struct ifnet *ifp = &sc->sc_arccom.ac_if;
    219 	int s, linkaddress;
    220 
    221 #if (defined(BAH_DEBUG) && (BAH_DEBUG > 2))
    222 	printf("\n%s: attach(0x%x, 0x%x, 0x%x)\n",
    223 	    sc->sc_dev.dv_xname, parent, self, aux);
    224 #endif
    225 	s = splhigh();
    226 	sc->sc_base = zap->va;
    227 
    228 	/*
    229 	 * read the arcnet address from the board
    230 	 */
    231 
    232 	sc->sc_base->kick1 = 0x0;
    233 	sc->sc_base->kick2 = 0x0;
    234 	DELAY(200);
    235 
    236 	sc->sc_base->kick1 = 0xFF;
    237 	sc->sc_base->kick2 = 0xFF;
    238 	do {
    239 		DELAY(200);
    240 	} while (!(sc->sc_base->status & ARC_POR));
    241 
    242 	linkaddress = sc->sc_base->dipswitches;
    243 
    244 #ifdef BAHTIMINGS
    245 	printf(": link addr 0x%02x(%ld), with timer\n",
    246 	    linkaddress, linkaddress);
    247 #else
    248 	printf(": link addr 0x%02x(%ld)\n", linkaddress, linkaddress);
    249 #endif
    250 
    251 	sc->sc_arccom.ac_anaddr = linkaddress;
    252 
    253 	/* clear the int mask... */
    254 
    255 	sc->sc_base->status = sc->sc_intmask = 0;
    256 
    257 	sc->sc_base->command = ARC_CONF(CONF_LONG);
    258 	sc->sc_base->command = ARC_CLR(CLR_POR|CLR_RECONFIG);
    259 	sc->sc_recontime = sc->sc_reconcount = 0;
    260 
    261 	/* and reenable kernel int level */
    262 	splx(s);
    263 
    264 	/*
    265 	 * set interface to stopped condition (reset)
    266 	 */
    267 	bah_stop(sc);
    268 
    269 	ifp->if_unit = sc->sc_dev.dv_unit;
    270 	ifp->if_name = bah_cd.cd_name;
    271 	ifp->if_output = arc_output;
    272 	ifp->if_start = bah_start;
    273 	ifp->if_ioctl = bah_ioctl;
    274 	ifp->if_timer = 0;
    275 	ifp->if_watchdog  = bah_watchdog;
    276 
    277 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX |
    278 	    IFF_NOTRAILERS | IFF_NOARP;
    279 
    280 	ifp->if_mtu = ARCMTU;
    281 
    282 	if_attach(ifp);
    283 	arc_ifattach(ifp);
    284 
    285 #if NBPFILTER > 0
    286 	bpfattach(&ifp->if_bpf, ifp, DLT_ARCNET, ARC_HDRLEN);
    287 #endif
    288 	/* under heavy load we need four of them: */
    289 	alloc_sicallback();
    290 	alloc_sicallback();
    291 	alloc_sicallback();
    292 	alloc_sicallback();
    293 
    294 	sc->sc_isr.isr_intr = bahintr;
    295 	sc->sc_isr.isr_arg = sc;
    296 	sc->sc_isr.isr_ipl = 2;
    297 	add_isr(&sc->sc_isr);
    298 }
    299 
    300 /*
    301  * Initialize device
    302  *
    303  */
    304 void
    305 bah_init(sc)
    306 	struct bah_softc *sc;
    307 {
    308 	struct ifnet *ifp;
    309 	int s;
    310 
    311 	ifp = &sc->sc_arccom.ac_if;
    312 
    313 	if ((ifp->if_flags & IFF_RUNNING) == 0) {
    314 		s = splnet();
    315 		ifp->if_flags |= IFF_RUNNING;
    316 		bah_reset(sc);
    317 		bah_start(ifp);
    318 		splx(s);
    319 	}
    320 }
    321 
    322 /*
    323  * Reset the interface...
    324  *
    325  * this assumes that it is called inside a critical section...
    326  *
    327  */
    328 void
    329 bah_reset(sc)
    330 	struct bah_softc *sc;
    331 {
    332 	struct ifnet *ifp;
    333 	int linkaddress;
    334 
    335 	ifp = &sc->sc_arccom.ac_if;
    336 
    337 #ifdef BAH_DEBUG
    338 	printf("%s: reset\n", sc->sc_dev.dv_xname);
    339 #endif
    340 	/* stop hardware in case it still runs */
    341 
    342 	sc->sc_base->kick1 = 0;
    343 	sc->sc_base->kick2 = 0;
    344 	DELAY(200);
    345 
    346 	/* and restart it */
    347 	sc->sc_base->kick1 = 0xFF;
    348 	sc->sc_base->kick2 = 0xFF;
    349 
    350 	do {
    351 		DELAY(200);
    352 	} while (!(sc->sc_base->status & ARC_POR));
    353 
    354 	linkaddress = sc->sc_base->dipswitches;
    355 
    356 #if defined(BAH_DEBUG) && (BAH_DEBUG > 2)
    357 	printf("bah%ld: reset: card reset, link addr = 0x%02x (%ld)\n",
    358 	    ifp->if_unit, linkaddress, linkaddress);
    359 #endif
    360 	sc->sc_arccom.ac_anaddr = linkaddress;
    361 
    362 	/* tell the routing level about the (possibly changed) link address */
    363 	arc_ifattach(ifp);
    364 
    365 	/* POR is NMI, but we need it below: */
    366 	sc->sc_intmask = ARC_RECON|ARC_POR;
    367 	sc->sc_base->status	= sc->sc_intmask;
    368 	sc->sc_base->command = ARC_CONF(CONF_LONG);
    369 
    370 #ifdef BAH_DEBUG
    371 	printf("%s: reset: chip configured, status=0x%02x\n",
    372 	    sc->sc_dev.dv_xname, sc->sc_base->status);
    373 #endif
    374 
    375 	sc->sc_base->command = ARC_CLR(CLR_POR|CLR_RECONFIG);
    376 
    377 #ifdef BAH_DEBUG
    378 	printf("%s: reset: bits cleared, status=0x%02x\n",
    379 	    sc->sc_dev.dv_xname, sc->sc_base->status);
    380 #endif
    381 
    382 	sc->sc_reconcount_excessive = ARC_EXCESSIVE_RECONS;
    383 
    384 	/* start receiver */
    385 
    386 	sc->sc_intmask  |= ARC_RI;
    387 	sc->sc_rx_fillcount = 0;
    388 	sc->sc_rx_act = 2;
    389 
    390 	sc->sc_base->command = ARC_RXBC(2);
    391 	sc->sc_base->status	= sc->sc_intmask;
    392 
    393 #ifdef BAH_DEBUG
    394 	printf("%s: reset: started receiver, status=0x%02x\n",
    395 	    sc->sc_dev.dv_xname, sc->sc_base->status);
    396 #endif
    397 
    398 	/* and init transmitter status */
    399 	sc->sc_tx_act = 0;
    400 	sc->sc_tx_fillcount = 0;
    401 
    402 	ifp->if_flags |= IFF_RUNNING;
    403 	ifp->if_flags &= ~IFF_OACTIVE;
    404 
    405 #ifdef BAHTIMINGS
    406 	bzero((caddr_t)&(sc->sc_stats), sizeof(sc->sc_stats));
    407 	sc->sc_stats.mincopyin =
    408 	    sc->sc_stats.mincopyout =
    409 	    sc->sc_stats.minsend = ULONG_MAX;
    410 #endif
    411 
    412 	bah_start(ifp);
    413 }
    414 
    415 /*
    416  * Take interface offline
    417  */
    418 void
    419 bah_stop(sc)
    420 	struct bah_softc *sc;
    421 {
    422 	/* Stop the interrupts */
    423 	sc->sc_base->status = 0;
    424 
    425 	/* Stop the interface */
    426 	sc->sc_base->kick1 = 0;
    427 	sc->sc_base->kick2 = 0;
    428 
    429 	/* Stop watchdog timer */
    430 	sc->sc_arccom.ac_if.if_timer = 0;
    431 
    432 #ifdef BAHTIMINGS
    433 	log(LOG_DEBUG,"%s: to board: %6lu .. %6lu ns/byte\n",
    434 	    sc->sc_dev.dv_xname,
    435 	    sc->sc_stats.mincopyout, sc->sc_stats.maxcopyout);
    436 
    437 	log(LOG_DEBUG,"%s: from board: %6lu .. %6lu ns/byte\n",
    438 	    sc->sc_dev.dv_xname,
    439 	    sc->sc_stats.mincopyin, sc->sc_stats.maxcopyin);
    440 
    441 	log(LOG_DEBUG,"%s: send time: %6lu .. %6lu mics/byte\n",
    442 	    sc->sc_dev.dv_xname,
    443 	    sc->sc_stats.minsend, sc->sc_stats.maxsend);
    444 
    445 	sc->sc_stats.minsend =
    446 	    sc->sc_stats.mincopyout =
    447 	    sc->sc_stats.mincopyin = ULONG_MAX;
    448 	sc->sc_stats.maxsend =
    449 	    sc->sc_stats.maxcopyout =
    450 	    sc->sc_stats.maxcopyin = 0;
    451 #endif
    452 }
    453 
    454 __inline void
    455 movepout(from, to, len)
    456 	u_char *from;
    457 	__volatile u_char *to;
    458 	int len;
    459 {
    460 #ifdef BAHASMCOPY
    461 	u_short shortd;
    462 	u_long longd, longd1, longd2, longd3, longd4;
    463 
    464 	if ((len > 3) && ((long)from) & 3) {
    465 		switch (((long)from) & 3) {
    466 		case 3:
    467 			*to = *from++;
    468 			to += 2; --len;
    469 			break;
    470 		case 1:
    471 			*to = *from++;
    472 			to += 2; --len;
    473 		case 2:
    474 			shortd = *((u_short *)from)++;
    475 			asm("movepw %0,%1@(0)" : : "d"(shortd), "a"(to));
    476 			to += 4; len -= 2;
    477 			break;
    478 		default:
    479 		}
    480 
    481 		while (len >= 32) {
    482 			longd1 = *((u_long *)from)++;
    483 			longd2 = *((u_long *)from)++;
    484 			longd3 = *((u_long *)from)++;
    485 			longd4 = *((u_long *)from)++;
    486 			asm("movepl %0,%1@(0)"  : : "d"(longd1), "a"(to));
    487 			asm("movepl %0,%1@(8)"  : : "d"(longd2), "a"(to));
    488 			asm("movepl %0,%1@(16)" : : "d"(longd3), "a"(to));
    489 			asm("movepl %0,%1@(24)" : : "d"(longd4), "a"(to));
    490 
    491 			longd1 = *((u_long *)from)++;
    492 			longd2 = *((u_long *)from)++;
    493 			longd3 = *((u_long *)from)++;
    494 			longd4 = *((u_long *)from)++;
    495 			asm("movepl %0,%1@(32)" : : "d"(longd1), "a"(to));
    496 			asm("movepl %0,%1@(40)" : : "d"(longd2), "a"(to));
    497 			asm("movepl %0,%1@(48)" : : "d"(longd3), "a"(to));
    498 			asm("movepl %0,%1@(56)" : : "d"(longd4), "a"(to));
    499 
    500 			to += 64; len -= 32;
    501 		}
    502 		while (len > 0) {
    503 			longd = *((u_long *)from)++;
    504 			asm("movepl %0,%1@(0)" : : "d"(longd), "a"(to));
    505 			to += 8; len -= 4;
    506 		}
    507 	}
    508 #endif
    509 	while (len > 0) {
    510 		*to = *from++;
    511 		to += 2;
    512 		--len;
    513 	}
    514 }
    515 
    516 /*
    517  * Start output on interface. Get another datagram to send
    518  * off the interface queue, and copy it to the
    519  * interface becore starting the output
    520  *
    521  * this assumes that it is called inside a critical section...
    522  * XXX hm... does it still?
    523  *
    524  */
    525 void
    526 bah_start(ifp)
    527 	struct ifnet *ifp;
    528 {
    529 	struct bah_softc *sc;
    530 	struct mbuf *m,*mp;
    531 	__volatile u_char *bah_ram_ptr;
    532 	int len, tlen, offset, s, buffer;
    533 #ifdef BAHTIMINGS
    534 	u_long copystart, lencopy, perbyte;
    535 #endif
    536 
    537 	sc = bah_cd.cd_devs[ifp->if_unit];
    538 
    539 #if defined(BAH_DEBUG) && (BAH_DEBUG > 3)
    540 	printf("%s: start(0x%x)\n", sc->sc_dev.dv_xname, ifp);
    541 #endif
    542 
    543 	if ((ifp->if_flags & IFF_RUNNING) == 0)
    544 		return;
    545 
    546 	s = splnet();
    547 
    548 	if (sc->sc_tx_fillcount >= 2) {
    549 		splx(s);
    550 		return;
    551 	}
    552 
    553 	IF_DEQUEUE(&ifp->if_snd, m);
    554 	buffer = sc->sc_tx_act ^ 1;
    555 
    556 	splx(s);
    557 
    558 	if (m == 0)
    559 		return;
    560 
    561 #if NBPFILTER > 0
    562 	/*
    563 	 * If bpf is listening on this interface, let it
    564 	 * see the packet before we commit it to the wire
    565 	 *
    566 	 * (can't give the copy in A2060 card RAM to bpf, because
    567 	 * that RAM is just accessed as on every other byte)
    568 	 */
    569 	if (ifp->if_bpf)
    570 		bpf_mtap(ifp->if_bpf, m);
    571 #endif
    572 
    573 #ifdef BAH_DEBUG
    574 	m = m_pullup(m,3);	/* gcc does structure padding */
    575 	printf("%s: start: filling %ld from %ld to %ld type %ld\n",
    576 	    sc->sc_dev.dv_xname, buffer, mtod(m, u_char *)[0],
    577 	    mtod(m, u_char *)[1], mtod(m, u_char *)[2]);
    578 #else
    579 	m = m_pullup(m, 2);
    580 #endif
    581 	bah_ram_ptr = sc->sc_base->buffers + buffer*512*2;
    582 
    583 	/* write the addresses to RAM and throw them away */
    584 
    585 	/*
    586 	 * Hardware does this: Yet Another Microsecond Saved.
    587 	 * (btw, timing code says usually 2 microseconds)
    588 	 * bah_ram_ptr[0*2] = mtod(m, u_char *)[0];
    589 	 */
    590 	bah_ram_ptr[1 * 2] = mtod(m, u_char *)[1];
    591 	m_adj(m, 2);
    592 
    593 	/* get total length left at this point */
    594 	tlen = m->m_pkthdr.len;
    595 	if (tlen < ARC_MIN_FORBID_LEN) {
    596 		offset = 256 - tlen;
    597 		bah_ram_ptr[2 * 2] = offset;
    598 	} else {
    599 		bah_ram_ptr[2 * 2] = 0;
    600 		if (tlen <= ARC_MAX_FORBID_LEN)
    601 			offset = 255;		/* !!! */
    602 		else {
    603 			if (tlen > ARC_MAX_LEN)
    604 				tlen = ARC_MAX_LEN;
    605 			offset = 512 - tlen;
    606 		}
    607 		bah_ram_ptr[3 * 2] = offset;
    608 
    609 	}
    610 	bah_ram_ptr += offset * 2;
    611 
    612 	/* lets loop through the mbuf chain */
    613 
    614 	for (mp = m; mp; mp = mp->m_next) {
    615 		if ((len = mp->m_len)) {		/* YAMS */
    616 #ifdef BAHTIMINGS
    617 			lencopy = len;
    618 			copystart = clkread();
    619 #endif
    620 			movepout(mtod(mp, caddr_t), bah_ram_ptr, len);
    621 
    622 #ifdef BAHTIMINGS
    623 			perbyte = 1000 * (clkread() - copystart) / lencopy;
    624 			sc->sc_stats.mincopyout =
    625 			    ulmin(sc->sc_stats.mincopyout, perbyte);
    626 			sc->sc_stats.maxcopyout =
    627 			    ulmax(sc->sc_stats.maxcopyout, perbyte);
    628 #endif
    629 			bah_ram_ptr += len*2;
    630 		}
    631 	}
    632 
    633 	sc->sc_broadcast[buffer] = (m->m_flags & M_BCAST) != 0;
    634 	sc->sc_retransmits[buffer] = (m->m_flags & M_BCAST) ? 1 : 5;
    635 
    636 	/* actually transmit the packet */
    637 	s = splnet();
    638 
    639 	if (++sc->sc_tx_fillcount > 1) {
    640 		/*
    641 		 * We are filled up to the rim. No more bufs for the moment,
    642 		 * please.
    643 		 */
    644 		ifp->if_flags |= IFF_OACTIVE;
    645 	} else {
    646 #ifdef BAH_DEBUG
    647 		printf("%s: start: starting transmitter on buffer %d\n",
    648 		    sc->sc_dev.dv_xname, buffer);
    649 #endif
    650 		/* Transmitter was off, start it */
    651 		sc->sc_tx_act = buffer;
    652 
    653 		/*
    654 		 * We still can accept another buf, so don't:
    655 		 * ifp->if_flags |= IFF_OACTIVE;
    656 		 */
    657 		sc->sc_intmask |= ARC_TA;
    658 		sc->sc_base->command = ARC_TX(buffer);
    659 		sc->sc_base->status  = sc->sc_intmask;
    660 
    661 		sc->sc_arccom.ac_if.if_timer = ARCTIMEOUT;
    662 #ifdef BAHTIMINGS
    663 		bcopy((caddr_t)&time,
    664 		    (caddr_t)&(sc->sc_stats.lasttxstart_tv),
    665 		    sizeof(struct timeval));
    666 
    667 		sc->sc_stats.lasttxstart_mics = clkread();
    668 #endif
    669 	}
    670 	splx(s);
    671 	m_freem(m);
    672 
    673 	/*
    674 	 * After 10 times reading the docs, I realized
    675 	 * that in the case the receiver NAKs the buffer request,
    676 	 * the hardware retries till shutdown.
    677 	 * This is integrated now in the code above.
    678 	 */
    679 
    680 	return;
    681 }
    682 
    683 void
    684 callstart(vsc, dummy)
    685 	void *vsc, *dummy;
    686 {
    687 	struct bah_softc *sc;
    688 
    689 	sc = (struct bah_softc *)vsc;
    690 	bah_start(&sc->sc_arccom.ac_if);
    691 }
    692 
    693 __inline void
    694 movepin(from, to, len)
    695 	__volatile u_char *from;
    696 	u_char *to;
    697 	int len;
    698 {
    699 #ifdef BAHASMCOPY
    700 	unsigned long	longd, longd1, longd2, longd3, longd4;
    701 	ushort		shortd;
    702 
    703 	if ((len > 3) && (((long)to) & 3)) {
    704 		switch (((long)to) & 3) {
    705 		case 3: *to++ = *from;
    706 			from += 2; --len;
    707 			break;
    708 		case 1: *to++ = *from;
    709 			from += 2; --len;
    710 		case 2:	asm ("movepw %1@(0),%0": "=d" (shortd) : "a" (from));
    711 			*((ushort *)to)++ = shortd;
    712 			from += 4; len -= 2;
    713 			break;
    714 		default:
    715 		}
    716 
    717 		while (len >= 32) {
    718 			asm("movepl %1@(0),%0"  : "=d"(longd1) : "a" (from));
    719 			asm("movepl %1@(8),%0"  : "=d"(longd2) : "a" (from));
    720 			asm("movepl %1@(16),%0" : "=d"(longd3) : "a" (from));
    721 			asm("movepl %1@(24),%0" : "=d"(longd4) : "a" (from));
    722 			*((unsigned long *)to)++ = longd1;
    723 			*((unsigned long *)to)++ = longd2;
    724 			*((unsigned long *)to)++ = longd3;
    725 			*((unsigned long *)to)++ = longd4;
    726 
    727 			asm("movepl %1@(32),%0" : "=d"(longd1) : "a" (from));
    728 			asm("movepl %1@(40),%0" : "=d"(longd2) : "a" (from));
    729 			asm("movepl %1@(48),%0" : "=d"(longd3) : "a" (from));
    730 			asm("movepl %1@(56),%0" : "=d"(longd4) : "a" (from));
    731 			*((unsigned long *)to)++ = longd1;
    732 			*((unsigned long *)to)++ = longd2;
    733 			*((unsigned long *)to)++ = longd3;
    734 			*((unsigned long *)to)++ = longd4;
    735 
    736 			from += 64; len -= 32;
    737 		}
    738 		while (len > 0) {
    739 			asm("movepl %1@(0),%0" : "=d"(longd) : "a" (from));
    740 			*((unsigned long *)to)++ = longd;
    741 			from += 8; len -= 4;
    742 		}
    743 
    744 	}
    745 #endif /* BAHASMCOPY */
    746 	while (len > 0) {
    747 		*to++ = *from;
    748 		from += 2;
    749 		--len;
    750 	}
    751 
    752 }
    753 
    754 /*
    755  * Arcnet interface receiver soft interrupt:
    756  * get the stuff out of any filled buffer we find.
    757  */
    758 void
    759 bah_srint(vsc, dummy)
    760 	void *vsc, *dummy;
    761 {
    762 	struct bah_softc *sc;
    763 	int buffer, len, len1, amount, offset, s, i, type;
    764 	u_char __volatile *bah_ram_ptr;
    765 	struct mbuf *m, *dst, *head;
    766 	struct arc_header *ah;
    767 	struct ifnet *ifp;
    768 #ifdef BAHTIMINGS
    769 	u_long copystart, lencopy, perbyte;
    770 #endif
    771 	sc = (struct bah_softc *)vsc;
    772 	ifp = &sc->sc_arccom.ac_if;
    773 	head = 0;
    774 
    775 	s = splnet();
    776 	buffer = sc->sc_rx_act ^ 1;
    777 	splx(s);
    778 
    779 	/* Allocate header mbuf */
    780 	MGETHDR(m, M_DONTWAIT, MT_DATA);
    781 
    782 	if (m == 0) {
    783 		/*
    784 	 	 * in case s.th. goes wrong with mem, drop it
    785 	 	 * to make sure the receiver can be started again
    786 		 * count it as input error (we dont have any other
    787 		 * detectable)
    788 	 	 */
    789 		ifp->if_ierrors++;
    790 		goto cleanup;
    791 	}
    792 
    793 	m->m_pkthdr.rcvif = ifp;
    794 
    795 	/*
    796 	 * Align so that IP packet will be longword aligned. Here we
    797 	 * assume that m_data of new packet is longword aligned.
    798 	 * When implementing PHDS, we might have to change it to 2,
    799 	 * (2*sizeof(ulong) - ARC_HDRNEWLEN)), packet type dependent.
    800 	 */
    801 
    802 	bah_ram_ptr = sc->sc_base->buffers + buffer*512*2;
    803 	offset = bah_ram_ptr[2*2];
    804 	if (offset)
    805 		len = 256 - offset;
    806 	else {
    807 		offset = bah_ram_ptr[3*2];
    808 		len = 512 - offset;
    809 	}
    810 	type = bah_ram_ptr[offset*2];
    811 	m->m_data += 1 + arc_isphds(type);
    812 
    813 	head = m;
    814 	ah = mtod(head, struct arc_header *);
    815 
    816 	ah->arc_shost = bah_ram_ptr[0*2];
    817 	ah->arc_dhost = bah_ram_ptr[1*2];
    818 
    819 	m->m_pkthdr.len = len+2; /* whole packet length */
    820 	m->m_len = 2;		 /* mbuf filled with ARCnet addresses */
    821 	bah_ram_ptr += offset*2; /* ram buffer continues there */
    822 
    823 	while (len > 0) {
    824 
    825 		len1 = len;
    826 		amount = M_TRAILINGSPACE(m);
    827 
    828 		if (amount == 0) {
    829 			dst = m;
    830 			MGET(m, M_DONTWAIT, MT_DATA);
    831 
    832 			if (m == 0) {
    833 				ifp->if_ierrors++;
    834 				goto cleanup;
    835 			}
    836 
    837 			if (len1 >= MINCLSIZE)
    838 				MCLGET(m, M_DONTWAIT);
    839 
    840 			m->m_len = 0;
    841 			dst->m_next = m;
    842 			amount = M_TRAILINGSPACE(m);
    843 		}
    844 
    845 		if (amount < len1)
    846 			len1 = amount;
    847 
    848 #ifdef BAHTIMINGS
    849 		lencopy = len;
    850 		copystart = clkread();
    851 #endif
    852 
    853 		movepin(bah_ram_ptr, mtod(m, u_char *) + m->m_len, len1);
    854 
    855 #ifdef BAHTIMINGS
    856 		perbyte = 1000 * (clkread() - copystart) / lencopy;
    857 		sc->sc_stats.mincopyin =
    858 		    ulmin(sc->sc_stats.mincopyin, perbyte);
    859 		sc->sc_stats.maxcopyin =
    860 		    ulmax(sc->sc_stats.maxcopyin, perbyte);
    861 #endif
    862 
    863 		m->m_len += len1;
    864 		bah_ram_ptr += len1*2;
    865 		len -= len1;
    866 	}
    867 
    868 #if NBPFILTER > 0
    869 	if (ifp->if_bpf)
    870 		bpf_mtap(ifp->if_bpf, head);
    871 #endif
    872 
    873 	arc_input(&sc->sc_arccom.ac_if, head);
    874 
    875 	/* arc_input has freed it, we dont need to... */
    876 
    877 	head = NULL;
    878 	ifp->if_ipackets++;
    879 
    880 cleanup:
    881 
    882 	if (head != NULL)
    883 		m_freem(head);
    884 
    885 	s = splnet();
    886 
    887 	if (--sc->sc_rx_fillcount == 2 - 1) {
    888 
    889 		/* was off, restart it on buffer just emptied */
    890 		sc->sc_rx_act = buffer;
    891 		sc->sc_intmask |= ARC_RI;
    892 
    893 		/* this also clears the RI flag interupt: */
    894 		sc->sc_base->command = ARC_RXBC(buffer);
    895 		sc->sc_base->status = sc->sc_intmask;
    896 
    897 #ifdef BAH_DEBUG
    898 		printf("%s: srint: restarted rx on buf %ld\n",
    899 		    sc->sc_dev.dv_xname, buffer);
    900 #endif
    901 	}
    902 	splx(s);
    903 }
    904 
    905 __inline static void
    906 bah_tint(sc, isr)
    907 	struct bah_softc *sc;
    908 	int isr;
    909 {
    910 	struct ifnet *ifp;
    911 
    912 	int buffer;
    913 #ifdef BAHTIMINGS
    914 	int clknow;
    915 #endif
    916 
    917 	ifp = &(sc->sc_arccom.ac_if);
    918 	buffer = sc->sc_tx_act;
    919 
    920 	/*
    921 	 * retransmit code:
    922 	 * Normal situtations first for fast path:
    923 	 * If acknowledgement received ok or broadcast, we're ok.
    924 	 * else if
    925 	 */
    926 
    927 	if (isr & ARC_TMA || sc->sc_broadcast[buffer])
    928 		sc->sc_arccom.ac_if.if_opackets++;
    929 #ifdef BAHRETRANSMIT
    930 	else if (ifp->if_flags & IFF_LINK2 && ifp->if_timer > 0
    931 	    && --sc->sc_retransmits[buffer] > 0) {
    932 		/* retransmit same buffer */
    933 		sc->sc_base->command = ARC_TX(buffer);
    934 		return;
    935 	}
    936 #endif
    937 	else
    938 		ifp->if_oerrors++;
    939 
    940 
    941 #ifdef BAHTIMINGS
    942 	clknow = clkread();
    943 
    944 	sc->sc_stats.minsend = ulmin(sc->sc_stats.minsend,
    945 	    clknow - sc->sc_stats.lasttxstart_mics);
    946 
    947 	sc->sc_stats.maxsend = ulmax(sc->sc_stats.maxsend,
    948 	    clknow - sc->sc_stats.lasttxstart_mics);
    949 #endif
    950 
    951 	/* We know we can accept another buffer at this point. */
    952 	ifp->if_flags &= ~IFF_OACTIVE;
    953 
    954 	if (--sc->sc_tx_fillcount > 0) {
    955 
    956 		/*
    957 		 * start tx on other buffer.
    958 		 * This also clears the int flag
    959 		 */
    960 		buffer ^= 1;
    961 		sc->sc_tx_act = buffer;
    962 
    963 		/*
    964 		 * already given:
    965 		 * sc->sc_intmask |= ARC_TA;
    966 		 * sc->sc_base->status = sc->sc_intmask;
    967 		 */
    968 		sc->sc_base->command = ARC_TX(buffer);
    969 		/* init watchdog timer */
    970 		ifp->if_timer = ARCTIMEOUT;
    971 
    972 #ifdef BAHTIMINGS
    973 		bcopy((caddr_t)&time,
    974 		    (caddr_t)&(sc->sc_stats.lasttxstart_tv),
    975 		    sizeof(struct timeval));
    976 
    977 		sc->sc_stats.lasttxstart_mics = clkread();
    978 #endif
    979 
    980 #if defined(BAH_DEBUG) && (BAH_DEBUG > 1)
    981 		printf("%s: tint: starting tx on buffer %d, status 0x%02x\n",
    982 		    sc->sc_dev.dv_xname, buffer, sc->sc_base->status);
    983 #endif
    984 	} else {
    985 		/* have to disable TX interrupt */
    986 		sc->sc_intmask &= ~ARC_TA;
    987 		sc->sc_base->status = sc->sc_intmask;
    988 		/* ... and watchdog timer */
    989 		ifp->if_timer = 0;
    990 
    991 #ifdef BAH_DEBUG
    992 		printf("%s: tint: no more buffers to send, status 0x%02x\n",
    993 		    sc->sc_dev.dv_xname, sc->sc_base->status);
    994 #endif
    995 	}
    996 
    997 #ifdef BAHSOFTCOPY
    998 	/* schedule soft int to fill a new buffer for us */
    999 	add_sicallback((sifunc_t)callstart, sc, NULL);
   1000 #else
   1001 	/* call it directly */
   1002 	callstart(sc, NULL);
   1003 #endif
   1004 }
   1005 
   1006 /*
   1007  * Our interrupt routine
   1008  */
   1009 int
   1010 bahintr(sc)
   1011 	struct bah_softc *sc;
   1012 {
   1013 	u_char isr, maskedisr;
   1014 	int buffer;
   1015 	u_long newsec;
   1016 
   1017 	isr = sc->sc_base->status;
   1018 	maskedisr = isr & sc->sc_intmask;
   1019 	if (!maskedisr)
   1020 		return (0);
   1021 
   1022 #if defined(BAH_DEBUG) && (BAH_DEBUG>1)
   1023 	printf("%s: intr: status 0x%02x, intmask 0x%02x\n",
   1024 	    sc->sc_dev.dv_xname, isr, sc->sc_intmask);
   1025 #endif
   1026 
   1027 	if (maskedisr & ARC_POR) {
   1028 		sc->sc_arccom.ac_anaddr = sc->sc_base->dipswitches;
   1029 		sc->sc_base->command = ARC_CLR(CLR_POR);
   1030 		log(LOG_WARNING, "%s: intr: got spurious power on reset int\n",
   1031 		    sc->sc_dev.dv_xname);
   1032 	}
   1033 
   1034 	if (maskedisr & ARC_RECON) {
   1035 		/*
   1036 		 * we dont need to:
   1037 		 * sc->sc_base->command = ARC_CONF(CONF_LONG);
   1038 		 */
   1039 		sc->sc_base->command = ARC_CLR(CLR_RECONFIG);
   1040 		sc->sc_arccom.ac_if.if_collisions++;
   1041 
   1042 		/*
   1043 		 * If more than 2 seconds per reconfig:
   1044 		 *	Reset time and counter.
   1045 		 * else:
   1046 		 *	If more than ARC_EXCESSIVE_RECONFIGS reconfigs
   1047 		 *	since last burst, complain and set treshold for
   1048 		 *	warnings to ARC_EXCESSIVE_RECONS_REWARN.
   1049 		 *
   1050 		 * This allows for, e.g., new stations on the cable, or
   1051 		 * cable switching as long as it is over after (normally)
   1052 		 * 16 seconds.
   1053 		 *
   1054 		 * XXX TODO: check timeout bits in status word and double
   1055 		 * time if necessary.
   1056 		 */
   1057 
   1058 		newsec = time.tv_sec;
   1059 		if (newsec - sc->sc_recontime > 2 * sc->sc_reconcount) {
   1060 			sc->sc_recontime = newsec;
   1061 			sc->sc_reconcount = 0;
   1062 			sc->sc_reconcount_excessive = ARC_EXCESSIVE_RECONS;
   1063 		} else if (++sc->sc_reconcount > sc->sc_reconcount_excessive) {
   1064 			sc->sc_reconcount_excessive =
   1065 			    ARC_EXCESSIVE_RECONS_REWARN;
   1066 			log(LOG_WARNING,
   1067 			    "%s: excessive token losses, cable problem?\n",
   1068 			    sc->sc_dev.dv_xname);
   1069 			sc->sc_recontime = newsec;
   1070 			sc->sc_reconcount = 0;
   1071 		}
   1072 	}
   1073 
   1074 	if (maskedisr & ARC_RI) {
   1075 
   1076 #if defined(BAH_DEBUG) && (BAH_DEBUG > 1)
   1077 		printf("%s: intr: hard rint, act %ld 2:%ld 3:%ld\n",
   1078 		    sc->sc_dev.dv_xname,
   1079 		    sc->sc_rx_act, sc->sc_bufstat[2], sc->sc_bufstat[3]);
   1080 #endif
   1081 
   1082 		buffer = sc->sc_rx_act;
   1083 		if (++sc->sc_rx_fillcount > 1) {
   1084 			sc->sc_intmask &= ~ARC_RI;
   1085 			sc->sc_base->status = sc->sc_intmask;
   1086 		} else {
   1087 
   1088 			buffer ^= 1;
   1089 			sc->sc_rx_act = buffer;
   1090 
   1091 			/*
   1092 			 * Start receiver on other receive buffer.
   1093 			 * This also clears the RI interupt flag.
   1094 			 */
   1095 			sc->sc_base->command = ARC_RXBC(buffer);
   1096 			/* we are in the RX intr, so mask is ok for RX */
   1097 
   1098 #ifdef BAH_DEBUG
   1099 			printf("%s: started rx for buffer %ld, status 0x%02x\n",
   1100 			    sc->sc_dev.dv_xname, sc->sc_rx_act,
   1101 			    sc->sc_base->status);
   1102 #endif
   1103 		}
   1104 
   1105 #ifdef BAHSOFTCOPY
   1106 		/* this one starts a soft int to copy out of the hw */
   1107 		add_sicallback((sifunc_t)bah_srint, sc,NULL);
   1108 #else
   1109 		/* this one does the copy here */
   1110 		bah_srint(sc,NULL);
   1111 #endif
   1112 	}
   1113 
   1114 	if (maskedisr & ARC_TA)
   1115 		bah_tint(sc, isr);
   1116 
   1117 	return (1);
   1118 }
   1119 
   1120 /*
   1121  * Process an ioctl request.
   1122  * This code needs some work - it looks pretty ugly.
   1123  */
   1124 int
   1125 bah_ioctl(ifp, command, data)
   1126 	register struct ifnet *ifp;
   1127 	u_long command;
   1128 	caddr_t data;
   1129 {
   1130 	struct bah_softc *sc;
   1131 	register struct ifaddr *ifa;
   1132 	int s, error;
   1133 
   1134 	error = 0;
   1135 	sc = bah_cd.cd_devs[ifp->if_unit];
   1136 	ifa = (struct ifaddr *)data;
   1137 	s = splnet();
   1138 
   1139 #if defined(BAH_DEBUG) && (BAH_DEBUG > 2)
   1140 	printf("%s: ioctl() called, cmd = 0x%x\n",
   1141 	    sc->sc_dev.dv_xname, command);
   1142 #endif
   1143 
   1144 	switch (command) {
   1145 	case SIOCSIFADDR:
   1146 		ifp->if_flags |= IFF_UP;
   1147 		switch (ifa->ifa_addr->sa_family) {
   1148 #ifdef INET
   1149 		case AF_INET:
   1150 			bah_init(sc);
   1151 			break;
   1152 #endif
   1153 		default:
   1154 			bah_init(sc);
   1155 			break;
   1156 		}
   1157 
   1158 	case SIOCSIFFLAGS:
   1159 		if ((ifp->if_flags & IFF_UP) == 0 &&
   1160 		    (ifp->if_flags & IFF_RUNNING) != 0) {
   1161 			/*
   1162 			 * If interface is marked down and it is running,
   1163 			 * then stop it.
   1164 			 */
   1165 			bah_stop(sc);
   1166 			ifp->if_flags &= ~IFF_RUNNING;
   1167 		} else if ((ifp->if_flags & IFF_UP) != 0 &&
   1168 			   (ifp->if_flags & IFF_RUNNING) == 0) {
   1169 			/*
   1170 			 * If interface is marked up and it is stopped, then
   1171 			 * start it.
   1172 			 */
   1173 			bah_init(sc);
   1174 		}
   1175 		break;
   1176 
   1177 		/* Multicast not supported */
   1178 
   1179 	default:
   1180 		error = EINVAL;
   1181 	}
   1182 
   1183 	splx(s);
   1184 	return (error);
   1185 }
   1186 
   1187 /*
   1188  * watchdog routine for transmitter.
   1189  *
   1190  * We need this, because else a receiver whose hardware is alive, but whose
   1191  * software has not enabled the Receiver, would make our hardware wait forever
   1192  * Discovered this after 20 times reading the docs.
   1193  *
   1194  * Only thing we do is disable transmitter. We'll get an transmit timeout,
   1195  * and the int handler will have to decide not to retransmit (in case
   1196  * retransmission is implemented).
   1197  *
   1198  * This one assumes being called inside splnet(), and that imp >= ipl2
   1199  */
   1200 
   1201 void
   1202 bah_watchdog(unit)
   1203 int unit;
   1204 {
   1205 	struct bah_softc *sc;
   1206 	struct ifnet *ifp;
   1207 
   1208 	sc = bah_cd.cd_devs[unit];
   1209 	ifp = &(sc->sc_arccom.ac_if);
   1210 
   1211 	sc->sc_base->command = ARC_TXDIS;
   1212 	return;
   1213 }
   1214