Home | History | Annotate | Line # | Download | only in dev
if_qn.c revision 1.18.2.1
      1 /*	$NetBSD: if_qn.c,v 1.18.2.1 2000/11/20 19:58:37 bouyer Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1995 Mika Kortelainen
      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  Mika Kortelainen
     18  * 4. The name of the author may not be used to endorse or promote products
     19  *    derived from this software without specific prior written permission
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     23  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     24  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     26  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     30  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31  *
     32  * Thanks for Aspecs Oy (Finland) for the data book for the NIC used
     33  * in this card and also many thanks for the Resource Management Force
     34  * (QuickNet card manufacturer) and especially Daniel Koch for providing
     35  * me with the necessary 'inside' information to write the driver.
     36  *
     37  * This is partly based on other code:
     38  * - if_ed.c: basic function structure for Ethernet driver and now also
     39  *	qn_put() is done similarly, i.e. no extra packet buffers.
     40  *
     41  *	Device driver for National Semiconductor DS8390/WD83C690 based ethernet
     42  *	adapters.
     43  *
     44  *	Copyright (c) 1994, 1995 Charles M. Hannum.  All rights reserved.
     45  *
     46  *	Copyright (C) 1993, David Greenman.  This software may be used,
     47  *	modified, copied, distributed, and sold, in both source and binary
     48  *	form provided that the above copyright and these terms are retained.
     49  *	Under no circumstances is the author responsible for the proper
     50  *	functioning of this software, nor does the author assume any
     51  *	responsibility for damages incurred with its use.
     52  *
     53  * - if_es.c: used as an example of -current driver
     54  *
     55  *	Copyright (c) 1995 Michael L. Hitch
     56  *	All rights reserved.
     57  *
     58  * - if_fe.c: some ideas for error handling for qn_rint() which might
     59  *	have fixed those random lock ups, too.
     60  *
     61  *	All Rights Reserved, Copyright (C) Fujitsu Limited 1995
     62  *
     63  *
     64  * TODO:
     65  * - add multicast support
     66  */
     67 
     68 #include "qn.h"
     69 #if NQN > 0
     70 
     71 #define QN_DEBUG
     72 #define QN_DEBUG1_no /* hides some old tests */
     73 #define QN_CHECKS_no /* adds some checks (not needed in normal situations) */
     74 
     75 #include "bpfilter.h"
     76 
     77 /*
     78  * Fujitsu MB86950 Ethernet Controller (as used in the QuickNet QN2000
     79  * Ethernet card)
     80  */
     81 
     82 #include "opt_inet.h"
     83 #include "opt_ns.h"
     84 
     85 #include <sys/param.h>
     86 #include <sys/systm.h>
     87 #include <sys/mbuf.h>
     88 #include <sys/buf.h>
     89 #include <sys/device.h>
     90 #include <sys/protosw.h>
     91 #include <sys/socket.h>
     92 #include <sys/syslog.h>
     93 #include <sys/ioctl.h>
     94 #include <sys/errno.h>
     95 
     96 #include <net/if.h>
     97 #include <net/if_dl.h>
     98 #include <net/if_ether.h>
     99 
    100 #ifdef INET
    101 #include <netinet/in.h>
    102 #include <netinet/in_systm.h>
    103 #include <netinet/in_var.h>
    104 #include <netinet/ip.h>
    105 #include <netinet/if_inarp.h>
    106 #endif
    107 
    108 #ifdef NS
    109 #include <netns/ns.h>
    110 #include <netns/ns_if.h>
    111 #endif
    112 
    113 #include <machine/cpu.h>
    114 #include <machine/mtpr.h>
    115 #include <amiga/amiga/device.h>
    116 #include <amiga/amiga/isr.h>
    117 #include <amiga/dev/zbusvar.h>
    118 #include <amiga/dev/if_qnreg.h>
    119 
    120 
    121 #define	NIC_R_MASK	(R_INT_PKT_RDY | R_INT_ALG_ERR |\
    122 			 R_INT_CRC_ERR | R_INT_OVR_FLO)
    123 #define	MAX_PACKETS	30 /* max number of packets read per interrupt */
    124 
    125 /*
    126  * Ethernet software status per interface
    127  *
    128  * Each interface is referenced by a network interface structure,
    129  * qn_if, which the routing code uses to locate the interface.
    130  * This structure contains the output queue for the interface, its address, ...
    131  */
    132 struct	qn_softc {
    133 	struct	device sc_dev;
    134 	struct	isr sc_isr;
    135 	struct	ethercom sc_ethercom;	/* Common ethernet structures */
    136 	u_char	volatile *sc_base;
    137 	u_char	volatile *sc_nic_base;
    138 	u_short	volatile *nic_fifo;
    139 	u_short	volatile *nic_r_status;
    140 	u_short	volatile *nic_t_status;
    141 	u_short	volatile *nic_r_mask;
    142 	u_short	volatile *nic_t_mask;
    143 	u_short	volatile *nic_r_mode;
    144 	u_short	volatile *nic_t_mode;
    145 	u_short	volatile *nic_reset;
    146 	u_short	volatile *nic_len;
    147 	u_char	transmit_pending;
    148 #if NBPFILTER > 0
    149 	caddr_t	sc_bpf;
    150 #endif
    151 } qn_softc[NQN];
    152 
    153 #if NBPFILTER > 0
    154 #include <net/bpf.h>
    155 #include <net/bpfdesc.h>
    156 #endif
    157 
    158 
    159 int	qnmatch __P((struct device *, struct cfdata *, void *));
    160 void	qnattach __P((struct device *, struct device *, void *));
    161 int	qnintr __P((void *));
    162 int	qnioctl __P((struct ifnet *, u_long, caddr_t));
    163 void	qnstart __P((struct ifnet *));
    164 void	qnwatchdog __P((struct ifnet *));
    165 void	qnreset __P((struct qn_softc *));
    166 void	qninit __P((struct qn_softc *));
    167 void	qnstop __P((struct qn_softc *));
    168 static	u_short qn_put __P((u_short volatile *, struct mbuf *));
    169 static	void qn_rint __P((struct qn_softc *, u_short));
    170 static	void qn_flush __P((struct qn_softc *));
    171 static	void inline word_copy_from_card __P((u_short volatile *, u_short *, u_short));
    172 static	void inline word_copy_to_card __P((u_short *, u_short volatile *, register u_short));
    173 static	void qn_get_packet __P((struct qn_softc *, u_short));
    174 #ifdef QN_DEBUG1
    175 static	void qn_dump __P((struct qn_softc *));
    176 #endif
    177 
    178 struct cfattach qn_ca = {
    179 	sizeof(struct qn_softc), qnmatch, qnattach
    180 };
    181 
    182 int
    183 qnmatch(parent, cfp, aux)
    184 	struct device *parent;
    185 	struct cfdata *cfp;
    186 	void *aux;
    187 {
    188 	struct zbus_args *zap;
    189 
    190 	zap = (struct zbus_args *)aux;
    191 
    192 	/* RMF QuickNet QN2000 EtherNet card */
    193 	if (zap->manid == 2011 && zap->prodid == 2)
    194 		return (1);
    195 
    196 	return (0);
    197 }
    198 
    199 /*
    200  * Interface exists: make available by filling in network interface
    201  * record.  System will initialize the interface when it is ready
    202  * to accept packets.
    203  */
    204 void
    205 qnattach(parent, self, aux)
    206 	struct device *parent, *self;
    207 	void *aux;
    208 {
    209 	struct zbus_args *zap;
    210 	struct qn_softc *sc = (struct qn_softc *)self;
    211 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    212 	u_int8_t myaddr[ETHER_ADDR_LEN];
    213 
    214 	zap = (struct zbus_args *)aux;
    215 
    216 	sc->sc_base = zap->va;
    217 	sc->sc_nic_base = sc->sc_base + QUICKNET_NIC_BASE;
    218 	sc->nic_fifo = (u_short volatile *)(sc->sc_nic_base + NIC_BMPR0);
    219 	sc->nic_len = (u_short volatile *)(sc->sc_nic_base + NIC_BMPR2);
    220 	sc->nic_t_status = (u_short volatile *)(sc->sc_nic_base + NIC_DLCR0);
    221 	sc->nic_r_status = (u_short volatile *)(sc->sc_nic_base + NIC_DLCR2);
    222 	sc->nic_t_mask = (u_short volatile *)(sc->sc_nic_base + NIC_DLCR1);
    223 	sc->nic_r_mask = (u_short volatile *)(sc->sc_nic_base + NIC_DLCR3);
    224 	sc->nic_t_mode = (u_short volatile *)(sc->sc_nic_base + NIC_DLCR4);
    225 	sc->nic_r_mode = (u_short volatile *)(sc->sc_nic_base + NIC_DLCR5);
    226 	sc->nic_reset = (u_short volatile *)(sc->sc_nic_base + NIC_DLCR6);
    227 	sc->transmit_pending = 0;
    228 
    229 	/*
    230 	 * The ethernet address of the board (1st three bytes are the vendor
    231 	 * address, the rest is the serial number of the board).
    232 	 */
    233 	myaddr[0] = 0x5c;
    234 	myaddr[1] = 0x5c;
    235 	myaddr[2] = 0x00;
    236 	myaddr[3] = (zap->serno >> 16) & 0xff;
    237 	myaddr[4] = (zap->serno >> 8) & 0xff;
    238 	myaddr[5] = zap->serno & 0xff;
    239 
    240 	/* set interface to stopped condition (reset) */
    241 	qnstop(sc);
    242 
    243 	bcopy(sc->sc_dev.dv_xname, ifp->if_xname, IFNAMSIZ);
    244 	ifp->if_softc = sc;
    245 	ifp->if_ioctl = qnioctl;
    246 	ifp->if_watchdog = qnwatchdog;
    247 	ifp->if_start = qnstart;
    248 	/* XXX IFF_MULTICAST */
    249 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS;
    250 	ifp->if_mtu = ETHERMTU;
    251 
    252 	/* Attach the interface. */
    253 	if_attach(ifp);
    254 	ether_ifattach(ifp, myaddr);
    255 
    256 #ifdef QN_DEBUG
    257 	printf(": hardware address %s\n", ether_sprintf(myaddr));
    258 #endif
    259 
    260 #if NBPFILTER > 0
    261 	bpfattach(&sc->sc_bpf, ifp, DLT_EN10MB, sizeof(struct ether_header));
    262 #endif
    263 
    264 	sc->sc_isr.isr_intr = qnintr;
    265 	sc->sc_isr.isr_arg = sc;
    266 	sc->sc_isr.isr_ipl = 2;
    267 	add_isr(&sc->sc_isr);
    268 }
    269 
    270 /*
    271  * Initialize device
    272  *
    273  */
    274 void
    275 qninit(sc)
    276 	struct qn_softc *sc;
    277 {
    278 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    279 	u_short i;
    280 	static int retry = 0;
    281 
    282 	*sc->nic_r_mask   = NIC_R_MASK;
    283 	*sc->nic_t_mode   = NO_LOOPBACK;
    284 
    285 	if (sc->sc_ethercom.ec_if.if_flags & IFF_PROMISC) {
    286 		*sc->nic_r_mode = PROMISCUOUS_MODE;
    287 		log(LOG_INFO, "qn: Promiscuous mode (not tested)\n");
    288 	} else
    289 		*sc->nic_r_mode = NORMAL_MODE;
    290 
    291 	/* Set physical ethernet address. */
    292 	for (i = 0; i < ETHER_ADDR_LEN; i++)
    293 		*((u_short volatile *)(sc->sc_nic_base+
    294 				       QNET_HARDWARE_ADDRESS+2*i)) =
    295 		    ((((u_short)LLADDR(ifp->if_sadl)[i]) << 8) |
    296 		    LLADDR(ifp->if_sadl)[i]);
    297 
    298 	ifp->if_flags |= IFF_RUNNING;
    299 	ifp->if_flags &= ~IFF_OACTIVE;
    300 	sc->transmit_pending = 0;
    301 
    302 	qn_flush(sc);
    303 
    304 	/* QuickNet magic. Done ONLY once, otherwise a lockup occurs. */
    305 	if (retry == 0) {
    306 		*((u_short volatile *)(sc->sc_nic_base + QNET_MAGIC)) = 0;
    307 		retry = 1;
    308 	}
    309 
    310 	/* Enable data link controller. */
    311 	*sc->nic_reset = ENABLE_DLC;
    312 
    313 	/* Attempt to start output, if any. */
    314 	qnstart(ifp);
    315 }
    316 
    317 /*
    318  * Device timeout/watchdog routine.  Entered if the device neglects to
    319  * generate an interrupt after a transmit has been started on it.
    320  */
    321 void
    322 qnwatchdog(ifp)
    323 	struct ifnet *ifp;
    324 {
    325 	struct qn_softc *sc = ifp->if_softc;
    326 
    327 	log(LOG_INFO, "qn: device timeout (watchdog)\n");
    328 	++sc->sc_ethercom.ec_if.if_oerrors;
    329 
    330 	qnreset(sc);
    331 }
    332 
    333 /*
    334  * Flush card's buffer RAM.
    335  */
    336 static void
    337 qn_flush(sc)
    338 	struct qn_softc *sc;
    339 {
    340 #if 1
    341 	/* Read data until bus read error (i.e. buffer empty). */
    342 	while (!(*sc->nic_r_status & R_BUS_RD_ERR))
    343 		(void)(*sc->nic_fifo);
    344 #else
    345 	/* Read data twice to clear some internal pipelines. */
    346 	(void)(*sc->nic_fifo);
    347 	(void)(*sc->nic_fifo);
    348 #endif
    349 
    350 	/* Clear bus read error. */
    351 	*sc->nic_r_status = R_BUS_RD_ERR;
    352 }
    353 
    354 /*
    355  * Reset the interface.
    356  *
    357  */
    358 void
    359 qnreset(sc)
    360 	struct qn_softc *sc;
    361 {
    362 	int s;
    363 
    364 	s = splnet();
    365 	qnstop(sc);
    366 	qninit(sc);
    367 	splx(s);
    368 }
    369 
    370 /*
    371  * Take interface offline.
    372  */
    373 void
    374 qnstop(sc)
    375 	struct qn_softc *sc;
    376 {
    377 
    378 	/* Stop the interface. */
    379 	*sc->nic_reset    = DISABLE_DLC;
    380 	delay(200);
    381 	*sc->nic_t_status = CLEAR_T_ERR;
    382 	*sc->nic_t_mask   = CLEAR_T_MASK;
    383 	*sc->nic_r_status = CLEAR_R_ERR;
    384 	*sc->nic_r_mask   = CLEAR_R_MASK;
    385 
    386 	/* Turn DMA off */
    387 	*((u_short volatile *)(sc->sc_nic_base + NIC_BMPR4)) = 0;
    388 
    389 	/* Accept no packets. */
    390 	*sc->nic_r_mode = 0;
    391 	*sc->nic_t_mode = 0;
    392 
    393 	qn_flush(sc);
    394 }
    395 
    396 /*
    397  * Start output on interface. Get another datagram to send
    398  * off the interface queue, and copy it to the
    399  * interface before starting the output.
    400  *
    401  * This assumes that it is called inside a critical section...
    402  *
    403  */
    404 void
    405 qnstart(ifp)
    406 	struct ifnet *ifp;
    407 {
    408 	struct qn_softc *sc = ifp->if_softc;
    409 	struct mbuf *m;
    410 	u_short len;
    411 	int timout = 60000;
    412 
    413 
    414 	if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
    415 		return;
    416 
    417 	IF_DEQUEUE(&ifp->if_snd, m);
    418 	if (m == 0)
    419 		return;
    420 
    421 #if NBPFILTER > 0
    422 	/*
    423 	 * If bpf is listening on this interface, let it
    424 	 * see the packet before we commit it to the wire
    425 	 *
    426 	 * (can't give the copy in QuickNet card RAM to bpf, because
    427 	 * that RAM is not visible to the host but is read from FIFO)
    428 	 *
    429 	 */
    430 	if (sc->sc_bpf)
    431 		bpf_mtap(sc->sc_bpf, m);
    432 #endif
    433 	len = qn_put(sc->nic_fifo, m);
    434 	m_freem(m);
    435 
    436 	/*
    437 	 * Really transmit the packet.
    438 	 */
    439 
    440 	/* Set packet length (byte-swapped). */
    441 	len = ((len >> 8) & 0x0007) | TRANSMIT_START | ((len & 0x00ff) << 8);
    442 	*sc->nic_len = len;
    443 
    444 	/* Wait for the packet to really leave. */
    445 	while (!(*sc->nic_t_status & T_TMT_OK) && --timout) {
    446 		if ((timout % 10000) == 0)
    447 			log(LOG_INFO, "qn: timout...\n");
    448 	}
    449 
    450 	if (timout == 0)
    451 		/* Maybe we should try to recover from this one? */
    452 		/* But now, let's just fall thru and hope the best... */
    453 		log(LOG_INFO, "qn: transmit timout (fatal?)\n");
    454 
    455 	sc->transmit_pending = 1;
    456 	*sc->nic_t_mask = INT_TMT_OK | INT_SIXTEEN_COL;
    457 
    458 	ifp->if_flags |= IFF_OACTIVE;
    459 	ifp->if_timer = 2;
    460 }
    461 
    462 /*
    463  * Memory copy, copies word at a time
    464  */
    465 static void inline
    466 word_copy_from_card(card, b, len)
    467 	u_short volatile *card;
    468 	u_short *b, len;
    469 {
    470 	register u_short l = len/2;
    471 
    472 	while (l--)
    473 		*b++ = *card;
    474 }
    475 
    476 static void inline
    477 word_copy_to_card(a, card, len)
    478 	u_short *a;
    479 	u_short volatile *card;
    480 	register u_short len;
    481 {
    482 	/*register u_short l = len/2;*/
    483 
    484 	while (len--)
    485 		*card = *a++;
    486 }
    487 
    488 /*
    489  * Copy packet from mbuf to the board memory
    490  *
    491  */
    492 static u_short
    493 qn_put(addr, m)
    494 	u_short volatile *addr;
    495 	struct mbuf *m;
    496 {
    497 	u_short *data;
    498 	u_char savebyte[2];
    499 	int len, len1, wantbyte;
    500 	u_short totlen;
    501 
    502 	totlen = wantbyte = 0;
    503 
    504 	for (; m != NULL; m = m->m_next) {
    505 		data = mtod(m, u_short *);
    506 		len = m->m_len;
    507 		if (len > 0) {
    508 			totlen += len;
    509 
    510 			/* Finish the last word. */
    511 			if (wantbyte) {
    512 				savebyte[1] = *((u_char *)data);
    513 				*addr = *((u_short *)savebyte);
    514 				((u_char *)data)++;
    515 				len--;
    516 				wantbyte = 0;
    517 			}
    518 			/* Output contiguous words. */
    519 			if (len > 1) {
    520 				len1 = len/2;
    521 				word_copy_to_card(data, addr, len1);
    522 				data += len1;
    523 				len &= 1;
    524 			}
    525 			/* Save last byte, if necessary. */
    526 			if (len == 1) {
    527 				savebyte[0] = *((u_char *)data);
    528 				wantbyte = 1;
    529 			}
    530 		}
    531 	}
    532 
    533 	if (wantbyte) {
    534 		savebyte[1] = 0;
    535 		*addr = *((u_short *)savebyte);
    536 	}
    537 
    538 	if(totlen < (ETHER_MIN_LEN - ETHER_CRC_LEN)) {
    539 		/*
    540 		 * Fill the rest of the packet with zeros.
    541 		 * N.B.: This is required! Otherwise MB86950 fails.
    542 		 */
    543 		for(len = totlen + 1; len < (ETHER_MIN_LEN - ETHER_CRC_LEN);
    544 		    len += 2)
    545 	  		*addr = (u_short)0x0000;
    546 		totlen = (ETHER_MIN_LEN - ETHER_CRC_LEN);
    547 	}
    548 
    549 	return (totlen);
    550 }
    551 
    552 /*
    553  * Copy packet from board RAM.
    554  *
    555  * Trailers not supported.
    556  *
    557  */
    558 static void
    559 qn_get_packet(sc, len)
    560 	struct qn_softc *sc;
    561 	u_short len;
    562 {
    563 	register u_short volatile *nic_fifo_ptr = sc->nic_fifo;
    564 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    565 	struct mbuf *m, *dst, *head = NULL;
    566 	register u_short len1;
    567 	u_short amount;
    568 
    569 	/* Allocate header mbuf. */
    570 	MGETHDR(m, M_DONTWAIT, MT_DATA);
    571 	if (m == NULL)
    572 		goto bad;
    573 
    574 	/*
    575 	 * Round len to even value.
    576 	 */
    577 	if (len & 1)
    578 		len++;
    579 
    580 	m->m_pkthdr.rcvif = &sc->sc_ethercom.ec_if;
    581 	m->m_pkthdr.len = len;
    582 	m->m_len = 0;
    583 	head = m;
    584 
    585 	word_copy_from_card(nic_fifo_ptr,
    586 	    mtod(head, u_short *),
    587 	    sizeof(struct ether_header));
    588 
    589 	head->m_len += sizeof(struct ether_header);
    590 	len -= sizeof(struct ether_header);
    591 
    592 	while (len > 0) {
    593 		len1 = len;
    594 
    595 		amount = M_TRAILINGSPACE(m);
    596 		if (amount == 0) {
    597 			/* Allocate another mbuf. */
    598 			dst = m;
    599 			MGET(m, M_DONTWAIT, MT_DATA);
    600 			if (m == NULL)
    601 				goto bad;
    602 
    603 			if (len1 >= MINCLSIZE)
    604 				MCLGET(m, M_DONTWAIT);
    605 
    606 			m->m_len = 0;
    607 			dst->m_next = m;
    608 
    609 			amount = M_TRAILINGSPACE(m);
    610 		}
    611 
    612 		if (amount < len1)
    613 			len1 = amount;
    614 
    615 		word_copy_from_card(nic_fifo_ptr,
    616 		    (u_short *)(mtod(m, caddr_t) + m->m_len),
    617 		    len1);
    618 		m->m_len += len1;
    619 		len -= len1;
    620 	}
    621 
    622 #if NBPFILTER > 0
    623 	if (sc->sc_bpf)
    624 		bpf_mtap(sc->sc_bpf, head);
    625 #endif
    626 
    627 	(*ifp->if_input)(ifp, head);
    628 	return;
    629 
    630 bad:
    631 	if (head) {
    632 		m_freem(head);
    633 		log(LOG_INFO, "qn_get_packet: mbuf alloc failed\n");
    634 	}
    635 }
    636 
    637 /*
    638  * Ethernet interface receiver interrupt.
    639  */
    640 static void
    641 qn_rint(sc, rstat)
    642 	struct qn_softc *sc;
    643 	u_short rstat;
    644 {
    645 	int i;
    646 	u_short len, status;
    647 
    648 	/* Clear the status register. */
    649 	*sc->nic_r_status = CLEAR_R_ERR;
    650 
    651 	/*
    652 	 * Was there some error?
    653 	 * Some of them are senseless because they are masked off.
    654 	 * XXX
    655 	 */
    656 	if (rstat & R_INT_OVR_FLO) {
    657 #ifdef QN_DEBUG
    658 		log(LOG_INFO, "Overflow\n");
    659 #endif
    660 		++sc->sc_ethercom.ec_if.if_ierrors;
    661 	}
    662 	if (rstat & R_INT_CRC_ERR) {
    663 #ifdef QN_DEBUG
    664 		log(LOG_INFO, "CRC Error\n");
    665 #endif
    666 		++sc->sc_ethercom.ec_if.if_ierrors;
    667 	}
    668 	if (rstat & R_INT_ALG_ERR) {
    669 #ifdef QN_DEBUG
    670 		log(LOG_INFO, "Alignment error\n");
    671 #endif
    672 		++sc->sc_ethercom.ec_if.if_ierrors;
    673 	}
    674 	if (rstat & R_INT_SRT_PKT) {
    675 		/* Short packet (these may occur and are
    676 		 * no reason to worry about - or maybe
    677 		 * they are?).
    678 		 */
    679 #ifdef QN_DEBUG
    680 		log(LOG_INFO, "Short packet\n");
    681 #endif
    682 		++sc->sc_ethercom.ec_if.if_ierrors;
    683 	}
    684 	if (rstat & 0x4040) {
    685 #ifdef QN_DEBUG
    686 		log(LOG_INFO, "Bus read error\n");
    687 #endif
    688 		++sc->sc_ethercom.ec_if.if_ierrors;
    689 		qnreset(sc);
    690 	}
    691 
    692 	/*
    693 	 * Read at most MAX_PACKETS packets per interrupt
    694 	 */
    695 	for (i = 0; i < MAX_PACKETS; i++) {
    696 		if (*sc->nic_r_mode & RM_BUF_EMP)
    697 			/* Buffer empty. */
    698 			break;
    699 
    700 		/*
    701 		 * Read the first word: upper byte contains useful
    702 		 * information.
    703 		 */
    704 		status = *sc->nic_fifo;
    705 		if ((status & 0x7000) != 0x2000) {
    706 			log(LOG_INFO, "qn: ERROR: status=%04x\n", status);
    707 			continue;
    708 		}
    709 
    710 		/*
    711 		 * Read packet length (byte-swapped).
    712 		 * CRC is stripped off by the NIC.
    713 		 */
    714 		len = *sc->nic_fifo;
    715 		len = ((len << 8) & 0xff00) | ((len >> 8) & 0x00ff);
    716 
    717 #ifdef QN_CHECKS
    718 		if (len > (ETHER_MAX_LEN - ETHER_CRC_LEN) ||
    719 		    len < ETHER_HDR_LEN) {
    720 			log(LOG_WARNING,
    721 			    "%s: received a %s packet? (%u bytes)\n",
    722 			    sc->sc_dev.dv_xname,
    723 			    len < ETHER_HDR_LEN ? "partial" : "big", len);
    724 			++sc->sc_ethercom.ec_if.if_ierrors;
    725 			continue;
    726 		}
    727 #endif
    728 #ifdef QN_CHECKS
    729 		if (len < (ETHER_MIN_LEN - ETHER_CRC_LEN))
    730 			log(LOG_WARNING,
    731 			    "%s: received a short packet? (%u bytes)\n",
    732 			    sc->sc_dev.dv_xname, len);
    733 #endif
    734 
    735 		/* Read the packet. */
    736 		qn_get_packet(sc, len);
    737 
    738 		++sc->sc_ethercom.ec_if.if_ipackets;
    739 	}
    740 
    741 #ifdef QN_DEBUG
    742 	/* This print just to see whether MAX_PACKETS is large enough. */
    743 	if (i == MAX_PACKETS)
    744 		log(LOG_INFO, "used all the %d loops\n", MAX_PACKETS);
    745 #endif
    746 }
    747 
    748 /*
    749  * Our interrupt routine
    750  */
    751 int
    752 qnintr(arg)
    753 	void *arg;
    754 {
    755 	struct qn_softc *sc = arg;
    756 	u_short tint, rint, tintmask;
    757 	char return_tintmask = 0;
    758 
    759 	/*
    760 	 * If the driver has not been initialized, just return immediately.
    761 	 * This also happens if there is no QuickNet board present.
    762 	 */
    763 	if (sc->sc_base == NULL)
    764 		return (0);
    765 
    766 	/* Get interrupt statuses and masks. */
    767 	rint = (*sc->nic_r_status) & NIC_R_MASK;
    768 	tintmask = *sc->nic_t_mask;
    769 	tint = (*sc->nic_t_status) & tintmask;
    770 	if (tint == 0 && rint == 0)
    771 		return (0);
    772 
    773 	/* Disable interrupts so that we won't miss anything. */
    774 	*sc->nic_r_mask = CLEAR_R_MASK;
    775 	*sc->nic_t_mask = CLEAR_T_MASK;
    776 
    777 	/*
    778 	 * Handle transmitter interrupts. Some of them are not asked for
    779 	 * but do happen, anyway.
    780 	 */
    781 
    782 	if (tint != 0) {
    783 		/* Clear transmit interrupt status. */
    784 		*sc->nic_t_status = CLEAR_T_ERR;
    785 
    786 		if (sc->transmit_pending && (tint & T_TMT_OK)) {
    787 			sc->transmit_pending = 0;
    788 			/*
    789 			 * Update total number of successfully
    790 			 * transmitted packets.
    791 			 */
    792 			sc->sc_ethercom.ec_if.if_opackets++;
    793 		}
    794 
    795 		if (tint & T_SIXTEEN_COL) {
    796 			/*
    797 			 * 16 collision (i.e., packet lost).
    798 			 */
    799 			log(LOG_INFO, "qn: 16 collision - packet lost\n");
    800 #ifdef QN_DEBUG1
    801 			qn_dump(sc);
    802 #endif
    803 			sc->sc_ethercom.ec_if.if_oerrors++;
    804 			sc->sc_ethercom.ec_if.if_collisions += 16;
    805 			sc->transmit_pending = 0;
    806 		}
    807 
    808 		if (sc->transmit_pending) {
    809 			log(LOG_INFO, "qn:still pending...\n");
    810 
    811 			/* Must return transmission interrupt mask. */
    812 			return_tintmask = 1;
    813 		} else {
    814 			sc->sc_ethercom.ec_if.if_flags &= ~IFF_OACTIVE;
    815 
    816 			/* Clear watchdog timer. */
    817 			sc->sc_ethercom.ec_if.if_timer = 0;
    818 		}
    819 	} else
    820 		return_tintmask = 1;
    821 
    822 	/*
    823 	 * Handle receiver interrupts.
    824 	 */
    825 	if (rint != 0)
    826 		qn_rint(sc, rint);
    827 
    828 	if ((sc->sc_ethercom.ec_if.if_flags & IFF_OACTIVE) == 0)
    829 		qnstart(&sc->sc_ethercom.ec_if);
    830 	else if (return_tintmask == 1)
    831 		*sc->nic_t_mask = tintmask;
    832 
    833 	/* Set receive interrupt mask back. */
    834 	*sc->nic_r_mask = NIC_R_MASK;
    835 
    836 	return (1);
    837 }
    838 
    839 /*
    840  * Process an ioctl request. This code needs some work - it looks pretty ugly.
    841  * I somehow think that this is quite a common excuse... ;-)
    842  */
    843 int
    844 qnioctl(ifp, command, data)
    845 	register struct ifnet *ifp;
    846 	u_long command;
    847 	caddr_t data;
    848 {
    849 	struct qn_softc *sc = ifp->if_softc;
    850 	register struct ifaddr *ifa = (struct ifaddr *)data;
    851 #if 0
    852 	struct ifreg *ifr = (struct ifreg *)data;
    853 #endif
    854 	int s, error = 0;
    855 
    856 	s = splnet();
    857 
    858 	switch (command) {
    859 
    860 	case SIOCSIFADDR:
    861 		ifp->if_flags |= IFF_UP;
    862 
    863 		switch (ifa->ifa_addr->sa_family) {
    864 #ifdef INET
    865 		case AF_INET:
    866 			qnstop(sc);
    867 			qninit(sc);
    868 			arp_ifinit(ifp, ifa);
    869 			break;
    870 #endif
    871 #ifdef NS
    872 		case AF_NS:
    873 		    {
    874 			register struct ns_addr *ina = &(IA_SNS(ifa)->sns_addr);
    875 
    876 			if (ns_nullhost(*ina))
    877 				ina->x_host =
    878 				    *(union ns_host *)LLADDR(ifp->if_sadl);
    879 			else
    880 				bcopy(ina->x_host.c_host,
    881 				    LLADDR(ifp->if_sadl), ETHER_ADDR_LEN);
    882 			qnstop(sc);
    883 			qninit(sc);
    884 			break;
    885 		    }
    886 #endif
    887 		default:
    888 			log(LOG_INFO, "qn:sa_family:default (not tested)\n");
    889 			qnstop(sc);
    890 			qninit(sc);
    891 			break;
    892 		}
    893 		break;
    894 
    895 	case SIOCSIFFLAGS:
    896 		if ((ifp->if_flags & IFF_UP) == 0 &&
    897 		    (ifp->if_flags & IFF_RUNNING) != 0) {
    898 			/*
    899 			 * If interface is marked down and it is running, then
    900 			 * stop it.
    901 			 */
    902 #ifdef QN_DEBUG1
    903 			qn_dump(sc);
    904 #endif
    905 			qnstop(sc);
    906 			ifp->if_flags &= ~IFF_RUNNING;
    907 		} else if ((ifp->if_flags & IFF_UP) != 0 &&
    908 			   (ifp->if_flags & IFF_RUNNING) == 0) {
    909 			/*
    910 			 * If interface is marked up and it is stopped, then
    911 			 * start it.
    912 			 */
    913 			qninit(sc);
    914 		} else {
    915 			/*
    916 			 * Something else... we won't do anything so we won't
    917 			 * break anything (hope so).
    918 			 */
    919 #ifdef QN_DEBUG1
    920 			log(LOG_INFO, "Else branch...\n");
    921 #endif
    922 		}
    923 		break;
    924 
    925 	case SIOCADDMULTI:
    926 	case SIOCDELMULTI:
    927 		log(LOG_INFO, "qnioctl: multicast not done yet\n");
    928 #if 0
    929 		error = (command == SIOCADDMULTI) ?
    930 		    ether_addmulti(ifr, &sc->sc_ethercom) :
    931 		    ether_delmulti(ifr, &sc->sc_ethercom);
    932 
    933 		if (error == ENETRESET) {
    934 			/*
    935 			 * Multicast list has changed; set the hardware filter
    936 			 * accordingly.
    937 			 */
    938 			log(LOG_INFO, "qnioctl: multicast not done yet\n");
    939 			error = 0;
    940 		}
    941 #else
    942 		error = EINVAL;
    943 #endif
    944 		break;
    945 
    946 	default:
    947 		log(LOG_INFO, "qnioctl: default\n");
    948 		error = EINVAL;
    949 	}
    950 
    951 	splx(s);
    952 	return (error);
    953 }
    954 
    955 /*
    956  * Dump some register information.
    957  */
    958 #ifdef QN_DEBUG1
    959 static void
    960 qn_dump(sc)
    961 	struct qn_softc *sc;
    962 {
    963 
    964 	log(LOG_INFO, "t_status  : %04x\n", *sc->nic_t_status);
    965 	log(LOG_INFO, "t_mask    : %04x\n", *sc->nic_t_mask);
    966 	log(LOG_INFO, "t_mode    : %04x\n", *sc->nic_t_mode);
    967 	log(LOG_INFO, "r_status  : %04x\n", *sc->nic_r_status);
    968 	log(LOG_INFO, "r_mask    : %04x\n", *sc->nic_r_mask);
    969 	log(LOG_INFO, "r_mode    : %04x\n", *sc->nic_r_mode);
    970 	log(LOG_INFO, "pending   : %02x\n", sc->transmit_pending);
    971 	log(LOG_INFO, "if_flags  : %04x\n", sc->sc_ethercom.ec_if.if_flags);
    972 }
    973 #endif
    974 
    975 #endif /* NQN > 0 */
    976