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