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