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