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