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