xref: /netbsd-src/sys/arch/amiga/dev/if_qn.c (revision 946379e7b37692fc43f68eb0d1c10daa0a7f3b6c)
1 /*	$NetBSD: if_qn.c,v 1.40 2015/05/20 09:17:17 ozaki-r 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.40 2015/05/20 09:17:17 ozaki-r 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 
102 #ifdef INET
103 #include <netinet/in.h>
104 #include <netinet/in_systm.h>
105 #include <netinet/in_var.h>
106 #include <netinet/ip.h>
107 #include <netinet/if_inarp.h>
108 #endif
109 
110 #include <machine/cpu.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	NIC_R_MASK	(R_INT_PKT_RDY | R_INT_ALG_ERR |\
118 			 R_INT_CRC_ERR | R_INT_OVR_FLO)
119 #define	MAX_PACKETS	30 /* max number of packets read per interrupt */
120 
121 /*
122  * Ethernet software status per interface
123  *
124  * Each interface is referenced by a network interface structure,
125  * qn_if, which the routing code uses to locate the interface.
126  * This structure contains the output queue for the interface, its address, ...
127  */
128 struct	qn_softc {
129 	device_t sc_dev;
130 	struct	isr sc_isr;
131 	struct	ethercom sc_ethercom;	/* Common ethernet structures */
132 	u_char	volatile *sc_base;
133 	u_char	volatile *sc_nic_base;
134 	u_short	volatile *nic_fifo;
135 	u_short	volatile *nic_r_status;
136 	u_short	volatile *nic_t_status;
137 	u_short	volatile *nic_r_mask;
138 	u_short	volatile *nic_t_mask;
139 	u_short	volatile *nic_r_mode;
140 	u_short	volatile *nic_t_mode;
141 	u_short	volatile *nic_reset;
142 	u_short	volatile *nic_len;
143 	u_char	transmit_pending;
144 } qn_softc[NQN];
145 
146 #include <net/bpf.h>
147 #include <net/bpfdesc.h>
148 
149 
150 int	qnmatch(device_t, cfdata_t, void *);
151 void	qnattach(device_t, device_t, void *);
152 int	qnintr(void *);
153 int	qnioctl(struct ifnet *, u_long, void *);
154 void	qnstart(struct ifnet *);
155 void	qnwatchdog(struct ifnet *);
156 void	qnreset(struct qn_softc *);
157 void	qninit(struct qn_softc *);
158 void	qnstop(struct qn_softc *);
159 static	u_short qn_put(u_short volatile *, struct mbuf *);
160 static	void qn_rint(struct qn_softc *, u_short);
161 static	void qn_flush(struct qn_softc *);
162 static	void inline word_copy_from_card(u_short volatile *, u_short *, u_short);
163 static	void inline word_copy_to_card(u_short *, u_short volatile *,
164 				register u_short);
165 static	void qn_get_packet(struct qn_softc *, u_short);
166 #ifdef QN_DEBUG1
167 static	void qn_dump(struct qn_softc *);
168 #endif
169 
170 CFATTACH_DECL_NEW(qn, sizeof(struct qn_softc),
171     qnmatch, qnattach, NULL, NULL);
172 
173 int
174 qnmatch(device_t parent, cfdata_t cf, void *aux)
175 {
176 	struct zbus_args *zap;
177 
178 	zap = (struct zbus_args *)aux;
179 
180 	/* RMF QuickNet QN2000 EtherNet card */
181 	if (zap->manid == 2011 && zap->prodid == 2)
182 		return (1);
183 
184 	return (0);
185 }
186 
187 /*
188  * Interface exists: make available by filling in network interface
189  * record.  System will initialize the interface when it is ready
190  * to accept packets.
191  */
192 void
193 qnattach(device_t parent, device_t self, void *aux)
194 {
195 	struct zbus_args *zap;
196 	struct qn_softc *sc = device_private(self);
197 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
198 	u_int8_t myaddr[ETHER_ADDR_LEN];
199 
200 	zap = (struct zbus_args *)aux;
201 
202 	sc->sc_base = zap->va;
203 	sc->sc_nic_base = sc->sc_base + QUICKNET_NIC_BASE;
204 	sc->nic_fifo = (u_short volatile *)(sc->sc_nic_base + NIC_BMPR0);
205 	sc->nic_len = (u_short volatile *)(sc->sc_nic_base + NIC_BMPR2);
206 	sc->nic_t_status = (u_short volatile *)(sc->sc_nic_base + NIC_DLCR0);
207 	sc->nic_r_status = (u_short volatile *)(sc->sc_nic_base + NIC_DLCR2);
208 	sc->nic_t_mask = (u_short volatile *)(sc->sc_nic_base + NIC_DLCR1);
209 	sc->nic_r_mask = (u_short volatile *)(sc->sc_nic_base + NIC_DLCR3);
210 	sc->nic_t_mode = (u_short volatile *)(sc->sc_nic_base + NIC_DLCR4);
211 	sc->nic_r_mode = (u_short volatile *)(sc->sc_nic_base + NIC_DLCR5);
212 	sc->nic_reset = (u_short volatile *)(sc->sc_nic_base + NIC_DLCR6);
213 	sc->transmit_pending = 0;
214 
215 	/*
216 	 * The ethernet address of the board (1st three bytes are the vendor
217 	 * address, the rest is the serial number of the board).
218 	 */
219 	myaddr[0] = 0x5c;
220 	myaddr[1] = 0x5c;
221 	myaddr[2] = 0x00;
222 	myaddr[3] = (zap->serno >> 16) & 0xff;
223 	myaddr[4] = (zap->serno >> 8) & 0xff;
224 	myaddr[5] = zap->serno & 0xff;
225 
226 	/* set interface to stopped condition (reset) */
227 	qnstop(sc);
228 
229 	memcpy(ifp->if_xname, device_xname(sc->sc_dev), IFNAMSIZ);
230 	ifp->if_softc = sc;
231 	ifp->if_ioctl = qnioctl;
232 	ifp->if_watchdog = qnwatchdog;
233 	ifp->if_start = qnstart;
234 	/* XXX IFF_MULTICAST */
235 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS;
236 	ifp->if_mtu = ETHERMTU;
237 
238 	/* Attach the interface. */
239 	if_attach(ifp);
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 	qnstart(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 	++sc->sc_ethercom.ec_if.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);
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: timout...\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 timout (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->m_pkthdr.rcvif = &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 	/* Tap off BPF listeners */
586 	bpf_mtap(ifp, head);
587 
588 	(*ifp->if_input)(ifp, head);
589 	return;
590 
591 bad:
592 	if (head) {
593 		m_freem(head);
594 		log(LOG_INFO, "qn_get_packet: mbuf alloc failed\n");
595 	}
596 }
597 
598 /*
599  * Ethernet interface receiver interrupt.
600  */
601 static void
602 qn_rint(struct qn_softc *sc, u_short rstat)
603 {
604 	int i;
605 	u_short len, status;
606 
607 	/* Clear the status register. */
608 	*sc->nic_r_status = CLEAR_R_ERR;
609 
610 	/*
611 	 * Was there some error?
612 	 * Some of them are senseless because they are masked off.
613 	 * XXX
614 	 */
615 	if (rstat & R_INT_OVR_FLO) {
616 #ifdef QN_DEBUG
617 		log(LOG_INFO, "Overflow\n");
618 #endif
619 		++sc->sc_ethercom.ec_if.if_ierrors;
620 	}
621 	if (rstat & R_INT_CRC_ERR) {
622 #ifdef QN_DEBUG
623 		log(LOG_INFO, "CRC Error\n");
624 #endif
625 		++sc->sc_ethercom.ec_if.if_ierrors;
626 	}
627 	if (rstat & R_INT_ALG_ERR) {
628 #ifdef QN_DEBUG
629 		log(LOG_INFO, "Alignment error\n");
630 #endif
631 		++sc->sc_ethercom.ec_if.if_ierrors;
632 	}
633 	if (rstat & R_INT_SRT_PKT) {
634 		/* Short packet (these may occur and are
635 		 * no reason to worry about - or maybe
636 		 * they are?).
637 		 */
638 #ifdef QN_DEBUG
639 		log(LOG_INFO, "Short packet\n");
640 #endif
641 		++sc->sc_ethercom.ec_if.if_ierrors;
642 	}
643 	if (rstat & 0x4040) {
644 #ifdef QN_DEBUG
645 		log(LOG_INFO, "Bus read error\n");
646 #endif
647 		++sc->sc_ethercom.ec_if.if_ierrors;
648 		qnreset(sc);
649 	}
650 
651 	/*
652 	 * Read at most MAX_PACKETS packets per interrupt
653 	 */
654 	for (i = 0; i < MAX_PACKETS; i++) {
655 		if (*sc->nic_r_mode & RM_BUF_EMP)
656 			/* Buffer empty. */
657 			break;
658 
659 		/*
660 		 * Read the first word: upper byte contains useful
661 		 * information.
662 		 */
663 		status = *sc->nic_fifo;
664 		if ((status & 0x7000) != 0x2000) {
665 			log(LOG_INFO, "qn: ERROR: status=%04x\n", status);
666 			continue;
667 		}
668 
669 		/*
670 		 * Read packet length (byte-swapped).
671 		 * CRC is stripped off by the NIC.
672 		 */
673 		len = *sc->nic_fifo;
674 		len = ((len << 8) & 0xff00) | ((len >> 8) & 0x00ff);
675 
676 #ifdef QN_CHECKS
677 		if (len > (ETHER_MAX_LEN - ETHER_CRC_LEN) ||
678 		    len < ETHER_HDR_LEN) {
679 			log(LOG_WARNING,
680 			    "%s: received a %s packet? (%u bytes)\n",
681 			    device_xname(sc->sc_dev),
682 			    len < ETHER_HDR_LEN ? "partial" : "big", len);
683 			++sc->sc_ethercom.ec_if.if_ierrors;
684 			continue;
685 		}
686 #endif
687 #ifdef QN_CHECKS
688 		if (len < (ETHER_MIN_LEN - ETHER_CRC_LEN))
689 			log(LOG_WARNING,
690 			    "%s: received a short packet? (%u bytes)\n",
691 			    device_xname(sc->sc_dev), len);
692 #endif
693 
694 		/* Read the packet. */
695 		qn_get_packet(sc, len);
696 
697 		++sc->sc_ethercom.ec_if.if_ipackets;
698 	}
699 
700 #ifdef QN_DEBUG
701 	/* This print just to see whether MAX_PACKETS is large enough. */
702 	if (i == MAX_PACKETS)
703 		log(LOG_INFO, "used all the %d loops\n", MAX_PACKETS);
704 #endif
705 }
706 
707 /*
708  * Our interrupt routine
709  */
710 int
711 qnintr(void *arg)
712 {
713 	struct qn_softc *sc = arg;
714 	u_short tint, rint, tintmask;
715 	char return_tintmask = 0;
716 
717 	/*
718 	 * If the driver has not been initialized, just return immediately.
719 	 * This also happens if there is no QuickNet board present.
720 	 */
721 	if (sc->sc_base == NULL)
722 		return (0);
723 
724 	/* Get interrupt statuses and masks. */
725 	rint = (*sc->nic_r_status) & NIC_R_MASK;
726 	tintmask = *sc->nic_t_mask;
727 	tint = (*sc->nic_t_status) & tintmask;
728 	if (tint == 0 && rint == 0)
729 		return (0);
730 
731 	/* Disable interrupts so that we won't miss anything. */
732 	*sc->nic_r_mask = CLEAR_R_MASK;
733 	*sc->nic_t_mask = CLEAR_T_MASK;
734 
735 	/*
736 	 * Handle transmitter interrupts. Some of them are not asked for
737 	 * but do happen, anyway.
738 	 */
739 
740 	if (tint != 0) {
741 		/* Clear transmit interrupt status. */
742 		*sc->nic_t_status = CLEAR_T_ERR;
743 
744 		if (sc->transmit_pending && (tint & T_TMT_OK)) {
745 			sc->transmit_pending = 0;
746 			/*
747 			 * Update total number of successfully
748 			 * transmitted packets.
749 			 */
750 			sc->sc_ethercom.ec_if.if_opackets++;
751 		}
752 
753 		if (tint & T_SIXTEEN_COL) {
754 			/*
755 			 * 16 collision (i.e., packet lost).
756 			 */
757 			log(LOG_INFO, "qn: 16 collision - packet lost\n");
758 #ifdef QN_DEBUG1
759 			qn_dump(sc);
760 #endif
761 			sc->sc_ethercom.ec_if.if_oerrors++;
762 			sc->sc_ethercom.ec_if.if_collisions += 16;
763 			sc->transmit_pending = 0;
764 		}
765 
766 		if (sc->transmit_pending) {
767 			log(LOG_INFO, "qn:still pending...\n");
768 
769 			/* Must return transmission interrupt mask. */
770 			return_tintmask = 1;
771 		} else {
772 			sc->sc_ethercom.ec_if.if_flags &= ~IFF_OACTIVE;
773 
774 			/* Clear watchdog timer. */
775 			sc->sc_ethercom.ec_if.if_timer = 0;
776 		}
777 	} else
778 		return_tintmask = 1;
779 
780 	/*
781 	 * Handle receiver interrupts.
782 	 */
783 	if (rint != 0)
784 		qn_rint(sc, rint);
785 
786 	if ((sc->sc_ethercom.ec_if.if_flags & IFF_OACTIVE) == 0)
787 		qnstart(&sc->sc_ethercom.ec_if);
788 	else if (return_tintmask == 1)
789 		*sc->nic_t_mask = tintmask;
790 
791 	/* Set receive interrupt mask back. */
792 	*sc->nic_r_mask = NIC_R_MASK;
793 
794 	return (1);
795 }
796 
797 /*
798  * Process an ioctl request. This code needs some work - it looks pretty ugly.
799  * I somehow think that this is quite a common excuse... ;-)
800  */
801 int
802 qnioctl(register struct ifnet *ifp, u_long cmd, void *data)
803 {
804 	struct qn_softc *sc = ifp->if_softc;
805 	register struct ifaddr *ifa = (struct ifaddr *)data;
806 #if 0
807 	struct ifreg *ifr = (struct ifreg *)data;
808 #endif
809 	int s, error = 0;
810 
811 	s = splnet();
812 
813 	switch (cmd) {
814 
815 	case SIOCINITIFADDR:
816 		ifp->if_flags |= IFF_UP;
817 
818 		switch (ifa->ifa_addr->sa_family) {
819 #ifdef INET
820 		case AF_INET:
821 			qnstop(sc);
822 			qninit(sc);
823 			arp_ifinit(ifp, ifa);
824 			break;
825 #endif
826 		default:
827 			log(LOG_INFO, "qn:sa_family:default (not tested)\n");
828 			qnstop(sc);
829 			qninit(sc);
830 			break;
831 		}
832 		break;
833 
834 	case SIOCSIFFLAGS:
835 		if ((error = ifioctl_common(ifp, cmd, data)) != 0)
836 			break;
837 		/* XXX see the comment in ed_ioctl() about code re-use */
838 		if ((ifp->if_flags & IFF_UP) == 0 &&
839 		    (ifp->if_flags & IFF_RUNNING) != 0) {
840 			/*
841 			 * If interface is marked down and it is running, then
842 			 * stop it.
843 			 */
844 #ifdef QN_DEBUG1
845 			qn_dump(sc);
846 #endif
847 			qnstop(sc);
848 			ifp->if_flags &= ~IFF_RUNNING;
849 		} else if ((ifp->if_flags & IFF_UP) != 0 &&
850 			   (ifp->if_flags & IFF_RUNNING) == 0) {
851 			/*
852 			 * If interface is marked up and it is stopped, then
853 			 * start it.
854 			 */
855 			qninit(sc);
856 		} else {
857 			/*
858 			 * Something else... we won't do anything so we won't
859 			 * break anything (hope so).
860 			 */
861 #ifdef QN_DEBUG1
862 			log(LOG_INFO, "Else branch...\n");
863 #endif
864 		}
865 		break;
866 
867 	case SIOCADDMULTI:
868 	case SIOCDELMULTI:
869 		log(LOG_INFO, "qnioctl: multicast not done yet\n");
870 #if 0
871 		if ((error = ether_ioctl(ifp, cmd, data)) == ENETRESET) {
872 			/*
873 			 * Multicast list has changed; set the hardware filter
874 			 * accordingly.
875 			 */
876 			log(LOG_INFO, "qnioctl: multicast not done yet\n");
877 			error = 0;
878 		}
879 #else
880 		error = EINVAL;
881 #endif
882 		break;
883 
884 	default:
885 		error = ether_ioctl(ifp, cmd, data);
886 	}
887 
888 	splx(s);
889 	return (error);
890 }
891 
892 /*
893  * Dump some register information.
894  */
895 #ifdef QN_DEBUG1
896 static void
897 qn_dump(struct qn_softc *sc)
898 {
899 
900 	log(LOG_INFO, "t_status  : %04x\n", *sc->nic_t_status);
901 	log(LOG_INFO, "t_mask    : %04x\n", *sc->nic_t_mask);
902 	log(LOG_INFO, "t_mode    : %04x\n", *sc->nic_t_mode);
903 	log(LOG_INFO, "r_status  : %04x\n", *sc->nic_r_status);
904 	log(LOG_INFO, "r_mask    : %04x\n", *sc->nic_r_mask);
905 	log(LOG_INFO, "r_mode    : %04x\n", *sc->nic_r_mode);
906 	log(LOG_INFO, "pending   : %02x\n", sc->transmit_pending);
907 	log(LOG_INFO, "if_flags  : %04x\n", sc->sc_ethercom.ec_if.if_flags);
908 }
909 #endif
910 
911 #endif /* NQN > 0 */
912