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