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