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