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