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