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