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