if_ae.c revision 1.51 1 /* $NetBSD: if_ae.c,v 1.51 1997/02/22 02:17:40 scottr Exp $ */
2
3 /*
4 * Device driver for National Semiconductor DS8390/WD83C690 based ethernet
5 * adapters.
6 *
7 * Copyright (c) 1994, 1995 Charles M. Hannum. All rights reserved.
8 *
9 * Copyright (C) 1993, David Greenman. This software may be used, modified,
10 * copied, distributed, and sold, in both source and binary form provided that
11 * the above copyright and these terms are retained. Under no circumstances is
12 * the author responsible for the proper functioning of this software, nor does
13 * the author assume any responsibility for damages incurred with its use.
14 *
15 * Adapted for MacBSD by Brad Parker <brad (at) fcr.com>.
16 *
17 * Currently supports:
18 * Apples NB Ethernet card
19 * Interlan A310 Nubus Ethernet card
20 * Cayman Systems GatorCard
21 * Asante MacCon II/E
22 */
23
24 #include "bpfilter.h"
25
26 #include <sys/param.h>
27 #include <sys/types.h>
28 #include <sys/systm.h>
29 #include <sys/errno.h>
30 #include <sys/ioctl.h>
31 #include <sys/mbuf.h>
32 #include <sys/socket.h>
33 #include <sys/syslog.h>
34 #include <sys/device.h>
35
36 #include <net/if.h>
37 #include <net/if_dl.h>
38 #include <net/if_types.h>
39 #include <net/netisr.h>
40
41 #ifdef INET
42 #include <netinet/in.h>
43 #include <netinet/in_systm.h>
44 #include <netinet/in_var.h>
45 #include <netinet/ip.h>
46 #include <netinet/if_ether.h>
47 #endif
48
49 #ifdef NS
50 #include <netns/ns.h>
51 #include <netns/ns_if.h>
52 #endif
53
54 #if NBPFILTER > 0
55 #include <net/bpf.h>
56 #include <net/bpfdesc.h>
57 #endif
58
59 #include <machine/bus.h>
60 #include <machine/viareg.h>
61
62 #include "nubus.h"
63 #include <dev/ic/dp8390reg.h>
64 #include "if_aereg.h"
65
66 #define INTERFACE_NAME_LEN 32
67
68 /*
69 * ae_softc: per line info and status
70 */
71 struct ae_softc {
72 struct device sc_dev;
73 bus_space_tag_t sc_tag;
74 bus_space_handle_t sc_handle;
75
76 nubus_slot sc_slot;
77 /* struct intrhand sc_ih; */
78
79 struct arpcom sc_arpcom;/* ethernet common */
80
81 char type_str[INTERFACE_NAME_LEN]; /* type string */
82 u_short type; /* interface type code */
83 u_char vendor; /* interface vendor */
84 u_char regs_rev; /* registers are reversed */
85
86 #define REG_MAP(sc, reg) ((sc)->regs_rev ? (0x0f-(reg))<<2 : (reg)<<2)
87 #define NIC_GET(sc, reg) ((sc)->nic_addr[REG_MAP(sc, reg)])
88 #define NIC_PUT(sc, reg, val) ((sc)->nic_addr[REG_MAP(sc, reg)] = (val))
89 volatile caddr_t nic_addr; /* NIC (DS8390) I/O bus address */
90 caddr_t rom_addr; /* on board prom address */
91
92 u_char cr_proto; /* values always set in CR */
93
94 caddr_t mem_start; /* shared memory start address */
95 caddr_t mem_end; /* shared memory end address */
96 u_long mem_size; /* total shared memory size */
97 caddr_t mem_ring; /* start of RX ring-buffer (in smem) */
98
99 u_char txb_cnt; /* Number of transmit buffers */
100 u_char txb_inuse; /* number of transmit buffers active */
101
102 u_char txb_new; /* pointer to where new buffer will be added */
103 u_char txb_next_tx; /* pointer to next buffer ready to xmit */
104 u_short txb_len[8]; /* buffered xmit buffer lengths */
105 u_char tx_page_start; /* first page of TX buffer area */
106 u_char rec_page_start; /* first page of RX ring-buffer */
107 u_char rec_page_stop; /* last page of RX ring-buffer */
108 u_char next_packet; /* pointer to next unread RX packet */
109 };
110
111 static int ae_card_vendor __P((struct nubus_attach_args *na));
112 static int ae_size_card_memory __P((caddr_t addr));
113
114 int aematch __P((struct device *, struct cfdata *, void *));
115 void aeattach __P((struct device *, struct device *, void *));
116 void aeintr __P((void *, int));
117 int aeioctl __P((struct ifnet *, u_long, caddr_t));
118 void aestart __P((struct ifnet *));
119 void aewatchdog __P((struct ifnet *));
120 void aereset __P((struct ae_softc *));
121 void aeinit __P((struct ae_softc *));
122 void aestop __P((struct ae_softc *));
123
124 void aeread __P((struct ae_softc *, caddr_t, int));
125 struct mbuf *aeget __P((struct ae_softc *, caddr_t, int));
126
127 #define inline /* XXX for debugging porpoises */
128
129 u_short ae_put __P((struct ae_softc *, struct mbuf *, caddr_t));
130 void ae_getmcaf __P((struct arpcom *, u_char *));
131
132 static inline void ae_rint __P((struct ae_softc *));
133 static inline void ae_xmit __P((struct ae_softc *));
134 static inline caddr_t ae_ring_copy __P((
135 struct ae_softc *, caddr_t, caddr_t, int));
136
137 struct cfattach ae_ca = {
138 sizeof(struct ae_softc), aematch, aeattach
139 };
140
141 struct cfdriver ae_cd = {
142 NULL, "ae", DV_IFNET
143 };
144
145 #define ETHER_MIN_LEN 64
146 #define ETHER_MAX_LEN 1518
147 #define ETHER_ADDR_LEN 6
148
149 static char zero = 0;
150
151 /*
152 * XXX These two should be moved to locore, and maybe changed to use shorts
153 * instead of bytes. The reason for these is that bcopy and bzero use longs,
154 * which the ethernet cards can't handle.
155 */
156
157 void bszero __P((u_short *addr, int len));
158 static inline void word_copy __P((caddr_t a, caddr_t b, int len));
159 static inline void byte_copy __P((caddr_t a, caddr_t b, int len));
160
161 void
162 bszero(u_short * addr, int len)
163 {
164 while (len--)
165 *addr++ = 0;
166 }
167
168 /*
169 * Memory copy, copies word at time.
170 */
171 static inline void
172 word_copy(a, b, len)
173 caddr_t a, b;
174 int len;
175 {
176 u_short *x = (u_short *) a, *y = (u_short *) b;
177
178 len >>= 1;
179 while (len--)
180 *y++ = *x++;
181 }
182
183 /*
184 * Memory copy, copies bytes at time.
185 */
186 static inline void
187 byte_copy(a, b, len)
188 caddr_t a, b;
189 int len;
190 {
191 while (len--)
192 *b++ = *a++;
193 }
194
195 static int
196 ae_card_vendor(na)
197 struct nubus_attach_args *na;
198 {
199 int vendor;
200
201 switch (na->drsw) {
202 case NUBUS_DRSW_3COM:
203 case NUBUS_DRSW_APPLE:
204 case NUBUS_DRSW_TECHWORKS:
205 vendor = AE_VENDOR_APPLE;
206 break;
207 case NUBUS_DRSW_ASANTE:
208 vendor = AE_VENDOR_ASANTE;
209 break;
210 case NUBUS_DRSW_FARALLON:
211 vendor = AE_VENDOR_FARALLON;
212 break;
213 case NUBUS_DRSW_FOCUS:
214 vendor = AE_VENDOR_FOCUS;
215 break;
216 case NUBUS_DRSW_GATOR:
217 switch (na->drhw) {
218 default:
219 case NUBUS_DRHW_INTERLAN:
220 vendor = AE_VENDOR_INTERLAN;
221 break;
222 case NUBUS_DRHW_KINETICS:
223 vendor = AE_VENDOR_DAYNA;
224 break;
225 }
226 break;
227 default:
228 #ifdef AE_DEBUG
229 printf("Unknown ethernet drsw: %x\n", na->drsw);
230 #endif
231 vendor = AE_VENDOR_UNKNOWN;
232 }
233 return vendor;
234 }
235
236 static int
237 ae_size_card_memory(addr)
238 caddr_t addr;
239 {
240 u_short *p;
241 u_short i1, i2, i3, i4;
242
243 p = (u_short *) addr;
244
245 /*
246 * very simple size memory, assuming it's installed in 8k
247 * banks; also assume it will generally mirror in upper banks
248 * if not installed.
249 */
250 i1 = (8192 * 0) / 2;
251 i2 = (8192 * 1) / 2;
252 i3 = (8192 * 2) / 2;
253 i4 = (8192 * 3) / 2;
254
255 p[i1] = 0x1111;
256 p[i2] = 0x2222;
257 p[i3] = 0x3333;
258 p[i4] = 0x4444;
259
260 if (p[i1] == 0x1111 && p[i2] == 0x2222 &&
261 p[i3] == 0x3333 && p[i4] == 0x4444)
262 return 8192 * 4;
263
264 if ((p[i1] == 0x1111 && p[i2] == 0x2222) ||
265 (p[i1] == 0x3333 && p[i2] == 0x4444))
266 return 8192 * 2;
267
268 if (p[i1] == 0x1111 || p[i1] == 0x4444)
269 return 8192;
270
271 return 0;
272 }
273
274 int
275 aematch(parent, cf, aux)
276 struct device *parent;
277 struct cfdata *cf;
278 void *aux;
279 {
280 struct nubus_attach_args *na = (struct nubus_attach_args *) aux;
281
282 if (na->category != NUBUS_CATEGORY_NETWORK)
283 return 0;
284
285 if (na->type != NUBUS_TYPE_ETHERNET)
286 return 0;
287
288 switch (ae_card_vendor(na)) {
289 case AE_VENDOR_APPLE:
290 case AE_VENDOR_ASANTE:
291 case AE_VENDOR_FARALLON:
292 case AE_VENDOR_INTERLAN:
293 break;
294
295 case AE_VENDOR_DAYNA:
296 case AE_VENDOR_FOCUS:
297 default:
298 return 0;
299 }
300 return 1;
301 }
302
303 /*
304 * Install interface into kernel networking data structures
305 */
306 void
307 aeattach(parent, self, aux)
308 struct device *parent, *self;
309 void *aux;
310 {
311 struct ae_softc *sc = (struct ae_softc *) self;
312 struct nubus_attach_args *na = (struct nubus_attach_args *) aux;
313 struct ifnet *ifp = &sc->sc_arpcom.ac_if;
314 caddr_t addr;
315 int i, memsize;
316 int flags = 0;
317
318 sc->regs_rev = 0;
319 sc->vendor = ae_card_vendor(na);
320 strncpy(sc->type_str, nubus_get_card_name(na->fmt),
321 INTERFACE_NAME_LEN);
322 sc->type_str[INTERFACE_NAME_LEN-1] = '\0';
323
324 addr = (caddr_t) na->fmt->virtual_base;
325 memsize = 0;
326
327 switch (sc->vendor) {
328 case AE_VENDOR_INTERLAN:
329 sc->nic_addr = addr + GC_NIC_OFFSET;
330 sc->rom_addr = addr + GC_ROM_OFFSET;
331 sc->mem_start = addr + GC_DATA_OFFSET;
332 if ((memsize = ae_size_card_memory(sc->mem_start)) == 0) {
333 printf(": failed to determine size of RAM.\n");
334 return;
335 }
336
337 /* reset the NIC chip */
338 *((caddr_t) addr + GC_RESET_OFFSET) = (char) zero;
339
340 /* Get station address from on-board ROM */
341 for (i = 0; i < ETHER_ADDR_LEN; ++i)
342 sc->sc_arpcom.ac_enaddr[i] = *(sc->rom_addr + i * 4);
343 break;
344
345 /* apple-compatible cards */
346 case AE_VENDOR_ASANTE:
347 case AE_VENDOR_APPLE:
348 sc->regs_rev = 1;
349 sc->nic_addr = addr + AE_NIC_OFFSET;
350 sc->rom_addr = addr + AE_ROM_OFFSET;
351 sc->mem_start = addr + AE_DATA_OFFSET;
352 if ((memsize = ae_size_card_memory(sc->mem_start)) == 0) {
353 printf(": failed to determine size of RAM.\n");
354 return;
355 }
356
357 /* Get station address from on-board ROM */
358 for (i = 0; i < ETHER_ADDR_LEN; ++i)
359 sc->sc_arpcom.ac_enaddr[i] = *(sc->rom_addr + i * 2);
360 break;
361
362 case AE_VENDOR_DAYNA:
363 sc->nic_addr = addr + DP_NIC_OFFSET;
364 sc->rom_addr = addr + DP_ROM_OFFSET;
365 sc->mem_start = addr + DP_DATA_OFFSET;
366 memsize = 8192;
367
368 /* Get station address from on-board ROM */
369 for (i = 0; i < ETHER_ADDR_LEN; ++i)
370 sc->sc_arpcom.ac_enaddr[i] = *(sc->rom_addr + i * 2);
371
372 printf(": unsupported Dayna hardware\n");
373 return;
374
375 case AE_VENDOR_FARALLON:
376 sc->regs_rev = 1;
377 sc->rom_addr = addr + FE_ROM_OFFSET;
378 sc->nic_addr = addr + AE_NIC_OFFSET;
379 sc->mem_start = addr + AE_DATA_OFFSET;
380 if ((memsize = ae_size_card_memory(sc->mem_start)) == 0) {
381 printf(": failed to determine size of RAM.\n");
382 return;
383 }
384
385 /* Get station address from on-board ROM */
386 for (i = 0; i < ETHER_ADDR_LEN; ++i)
387 sc->sc_arpcom.ac_enaddr[i] = *(sc->rom_addr + i);
388 break;
389 case AE_VENDOR_FOCUS:
390 printf(": unsupported Focus hardware\n");
391 return;
392 }
393
394 sc->cr_proto = ED_CR_RD2;
395
396 /* Allocate one xmit buffer if < 16k, two buffers otherwise. */
397 if ((memsize < 16384) || (flags & AE_FLAGS_NO_DOUBLE_BUFFERING))
398 sc->txb_cnt = 1;
399 else
400 sc->txb_cnt = 2;
401
402 sc->tx_page_start = 0;
403 sc->rec_page_start = sc->tx_page_start + sc->txb_cnt * ED_TXBUF_SIZE;
404 sc->rec_page_stop = sc->tx_page_start + (memsize >> ED_PAGE_SHIFT);
405 sc->mem_ring = sc->mem_start + (sc->rec_page_start << ED_PAGE_SHIFT);
406 sc->mem_size = memsize;
407 sc->mem_end = sc->mem_start + memsize;
408
409 /* Now zero memory and verify that it is clear. */
410 bszero((u_short *) sc->mem_start, memsize / 2);
411
412 for (i = 0; i < memsize; ++i)
413 if (sc->mem_start[i])
414 printf("%s: failed to clear shared memory at %p - check configuration\n",
415 sc->sc_dev.dv_xname,
416 sc->mem_start + i);
417
418 bcopy(na->fmt, &sc->sc_slot, sizeof(nubus_slot));
419
420
421 /* Set interface to stopped condition (reset). */
422 aestop(sc);
423
424 /* Initialize ifnet structure. */
425 bcopy(sc->sc_dev.dv_xname, ifp->if_xname, IFNAMSIZ);
426 ifp->if_softc = sc;
427 ifp->if_start = aestart;
428 ifp->if_ioctl = aeioctl;
429 ifp->if_watchdog = aewatchdog;
430 ifp->if_flags =
431 IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS | IFF_MULTICAST;
432
433 /* Attach the interface. */
434 if_attach(ifp);
435 ether_ifattach(ifp);
436
437 /* Print additional info when attached. */
438 printf(": address %s, ", ether_sprintf(sc->sc_arpcom.ac_enaddr));
439
440 printf("type %s, %ldKB memory\n", sc->type_str, sc->mem_size / 1024);
441
442 #if NBPFILTER > 0
443 bpfattach(&ifp->if_bpf, ifp, DLT_EN10MB, sizeof(struct ether_header));
444 #endif
445
446 /* make sure interrupts are vectored to us */
447 add_nubus_intr(sc->sc_slot.slot, aeintr, sc);
448
449 /*
450 * XXX -- enable nubus interrupts here. Should be done elsewhere,
451 * but that currently breaks with some nubus video cards'
452 * interrupts. So we only enable nubus interrupts if we
453 * have an ethernet card... i.e., we do it here.
454 */
455 enable_nubus_intr();
456 }
457
458 /*
459 * Reset interface.
460 */
461 void
462 aereset(sc)
463 struct ae_softc *sc;
464 {
465 int s;
466
467 s = splnet();
468 aestop(sc);
469 aeinit(sc);
470 splx(s);
471 }
472
473 /*
474 * Take interface offline.
475 */
476 void
477 aestop(sc)
478 struct ae_softc *sc;
479 {
480 int n = 5000;
481
482 /* Stop everything on the interface, and select page 0 registers. */
483 NIC_PUT(sc, ED_P0_CR, sc->cr_proto | ED_CR_PAGE_0 | ED_CR_STP);
484
485 /*
486 * Wait for interface to enter stopped state, but limit # of checks to
487 * 'n' (about 5ms). It shouldn't even take 5us on modern DS8390's, but
488 * just in case it's an old one.
489 */
490 while (((NIC_GET(sc, ED_P0_ISR) & ED_ISR_RST) == 0) && --n);
491 }
492
493 /*
494 * Device timeout/watchdog routine. Entered if the device neglects to generate
495 * an interrupt after a transmit has been started on it.
496 */
497 static int aeintr_ctr = 0;
498
499 void
500 aewatchdog(ifp)
501 struct ifnet *ifp;
502 {
503 struct ae_softc *sc = ifp->if_softc;
504
505 #if 1
506 /*
507 * This is a kludge! The via code seems to miss slot interrupts
508 * sometimes. This kludges around that by calling the handler
509 * by hand if the watchdog is activated. -- XXX (akb)
510 */
511 int i;
512
513 i = aeintr_ctr;
514
515 (*via2itab[1]) ((void *) 1);
516
517 if (i != aeintr_ctr) {
518 log(LOG_ERR, "%s: device timeout, recovered\n",
519 sc->sc_dev.dv_xname);
520 return;
521 }
522 #endif
523
524 log(LOG_ERR, "%s: device timeout\n", sc->sc_dev.dv_xname);
525 ++sc->sc_arpcom.ac_if.if_oerrors;
526
527 aereset(sc);
528 }
529
530 /*
531 * Initialize device.
532 */
533 void
534 aeinit(sc)
535 struct ae_softc *sc;
536 {
537 struct ifnet *ifp = &sc->sc_arpcom.ac_if;
538 int i;
539 u_char mcaf[8];
540
541 /*
542 * Initialize the NIC in the exact order outlined in the NS manual.
543 * This init procedure is "mandatory"...don't change what or when
544 * things happen.
545 */
546
547 /* Reset transmitter flags. */
548 ifp->if_timer = 0;
549
550 sc->txb_inuse = 0;
551 sc->txb_new = 0;
552 sc->txb_next_tx = 0;
553
554 /* Set interface for page 0, remote DMA complete, stopped. */
555 NIC_PUT(sc, ED_P0_CR, sc->cr_proto | ED_CR_PAGE_0 | ED_CR_STP);
556
557 /*
558 * Set FIFO threshold to 8, No auto-init Remote DMA, byte
559 * order=80x86, word-wide DMA xfers,
560 */
561 NIC_PUT(sc, ED_P0_DCR,
562 ED_DCR_FT1 | ED_DCR_WTS | ED_DCR_LS);
563
564 /* Clear remote byte count registers. */
565 NIC_PUT(sc, ED_P0_RBCR0, 0);
566 NIC_PUT(sc, ED_P0_RBCR1, 0);
567
568 /* Tell RCR to do nothing for now. */
569 NIC_PUT(sc, ED_P0_RCR, ED_RCR_MON);
570
571 /* Place NIC in internal loopback mode. */
572 NIC_PUT(sc, ED_P0_TCR, ED_TCR_LB0);
573
574 /* Initialize receive buffer ring. */
575 NIC_PUT(sc, ED_P0_TPSR, sc->rec_page_start);
576 NIC_PUT(sc, ED_P0_PSTART, sc->rec_page_start);
577
578 NIC_PUT(sc, ED_P0_PSTOP, sc->rec_page_stop);
579 NIC_PUT(sc, ED_P0_BNRY, sc->rec_page_start);
580
581 /*
582 * Clear all interrupts. A '1' in each bit position clears the
583 * corresponding flag.
584 */
585 NIC_PUT(sc, ED_P0_ISR, 0xff);
586
587 /*
588 * Enable the following interrupts: receive/transmit complete,
589 * receive/transmit error, and Receiver OverWrite.
590 *
591 * Counter overflow and Remote DMA complete are *not* enabled.
592 */
593 NIC_PUT(sc, ED_P0_IMR,
594 ED_IMR_PRXE | ED_IMR_PTXE | ED_IMR_RXEE | ED_IMR_TXEE |
595 ED_IMR_OVWE);
596
597 /* Program command register for page 1. */
598 NIC_PUT(sc, ED_P0_CR, sc->cr_proto | ED_CR_PAGE_1 | ED_CR_STP);
599
600 /* Copy out our station address. */
601 for (i = 0; i < ETHER_ADDR_LEN; ++i)
602 NIC_PUT(sc, ED_P1_PAR0 + i, sc->sc_arpcom.ac_enaddr[i]);
603
604 /* Set multicast filter on chip. */
605 ae_getmcaf(&sc->sc_arpcom, mcaf);
606 for (i = 0; i < 8; i++)
607 NIC_PUT(sc, ED_P1_MAR0 + i, mcaf[i]);
608
609 /*
610 * Set current page pointer to one page after the boundary pointer, as
611 * recommended in the National manual.
612 */
613 sc->next_packet = sc->rec_page_start + 1;
614 NIC_PUT(sc, ED_P1_CURR, sc->next_packet);
615
616 /* Program command register for page 0. */
617 NIC_PUT(sc, ED_P1_CR, sc->cr_proto | ED_CR_PAGE_0 | ED_CR_STP);
618
619 i = ED_RCR_AB | ED_RCR_AM;
620 if (ifp->if_flags & IFF_PROMISC) {
621 /*
622 * Set promiscuous mode. Multicast filter was set earlier so
623 * that we should receive all multicast packets.
624 */
625 i |= ED_RCR_PRO | ED_RCR_AR | ED_RCR_SEP;
626 }
627 NIC_PUT(sc, ED_P0_RCR, i);
628
629 /* Take interface out of loopback. */
630 NIC_PUT(sc, ED_P0_TCR, 0);
631
632 /* Fire up the interface. */
633 NIC_PUT(sc, ED_P0_CR, sc->cr_proto | ED_CR_PAGE_0 | ED_CR_STA);
634
635 /* Set 'running' flag, and clear output active flag. */
636 ifp->if_flags |= IFF_RUNNING;
637 ifp->if_flags &= ~IFF_OACTIVE;
638
639 /* ...and attempt to start output. */
640 aestart(ifp);
641 }
642
643 /*
644 * This routine actually starts the transmission on the interface.
645 */
646 static inline void
647 ae_xmit(sc)
648 struct ae_softc *sc;
649 {
650 struct ifnet *ifp = &sc->sc_arpcom.ac_if;
651 u_short len;
652
653 len = sc->txb_len[sc->txb_next_tx];
654
655 /* Set NIC for page 0 register access. */
656 NIC_PUT(sc, ED_P0_CR, sc->cr_proto | ED_CR_PAGE_0 | ED_CR_STA);
657
658 /* Set TX buffer start page. */
659 NIC_PUT(sc, ED_P0_TPSR, sc->tx_page_start +
660 sc->txb_next_tx * ED_TXBUF_SIZE);
661
662 /* Set TX length. */
663 NIC_PUT(sc, ED_P0_TBCR0, len);
664 NIC_PUT(sc, ED_P0_TBCR1, len >> 8);
665
666 /* Set page 0, remote DMA complete, transmit packet, and *start*. */
667 NIC_PUT(sc, ED_P0_CR, sc->cr_proto | ED_CR_PAGE_0 | ED_CR_TXP | ED_CR_STA);
668
669 /* Point to next transmit buffer slot and wrap if necessary. */
670 sc->txb_next_tx++;
671 if (sc->txb_next_tx == sc->txb_cnt)
672 sc->txb_next_tx = 0;
673
674 /* Set a timer just in case we never hear from the board again. */
675 ifp->if_timer = 2;
676 }
677
678 /*
679 * Start output on interface.
680 * We make two assumptions here:
681 * 1) that the current priority is set to splnet _before_ this code
682 * is called *and* is returned to the appropriate priority after
683 * return
684 * 2) that the IFF_OACTIVE flag is checked before this code is called
685 * (i.e. that the output part of the interface is idle)
686 */
687 void
688 aestart(ifp)
689 struct ifnet *ifp;
690 {
691 struct ae_softc *sc = ifp->if_softc;
692 struct mbuf *m0;
693 caddr_t buffer;
694 int len;
695
696 if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
697 return;
698
699 outloop:
700 /* See if there is room to put another packet in the buffer. */
701 if (sc->txb_inuse == sc->txb_cnt) {
702 /* No room. Indicate this to the outside world and exit. */
703 ifp->if_flags |= IFF_OACTIVE;
704 return;
705 }
706 IF_DEQUEUE(&ifp->if_snd, m0);
707 if (m0 == 0)
708 return;
709
710 /* We need to use m->m_pkthdr.len, so require the header */
711 if ((m0->m_flags & M_PKTHDR) == 0)
712 panic("aestart: no header mbuf");
713
714 #if NBPFILTER > 0
715 /* Tap off here if there is a BPF listener. */
716 if (ifp->if_bpf)
717 bpf_mtap(ifp->if_bpf, m0);
718 #endif
719
720 /* txb_new points to next open buffer slot. */
721 buffer = sc->mem_start + ((sc->txb_new * ED_TXBUF_SIZE) << ED_PAGE_SHIFT);
722
723 len = ae_put(sc, m0, buffer);
724 #if DIAGNOSTIC
725 if (len != m0->m_pkthdr.len)
726 printf("aestart: len %d != m0->m_pkthdr.len %d.\n",
727 len, m0->m_pkthdr.len);
728 #endif
729 len = m0->m_pkthdr.len;
730
731 m_freem(m0);
732 sc->txb_len[sc->txb_new] = max(len, ETHER_MIN_LEN);
733
734 /* Start the first packet transmitting. */
735 if (sc->txb_inuse == 0)
736 ae_xmit(sc);
737
738 /* Point to next buffer slot and wrap if necessary. */
739 if (++sc->txb_new == sc->txb_cnt)
740 sc->txb_new = 0;
741
742 sc->txb_inuse++;
743
744 /* Loop back to the top to possibly buffer more packets. */
745 goto outloop;
746 }
747
748 /*
749 * Ethernet interface receiver interrupt.
750 */
751 static inline void
752 ae_rint(sc)
753 struct ae_softc *sc;
754 {
755 u_char boundary, current;
756 u_short len;
757 u_char nlen, *lenp;
758 struct ae_ring packet_hdr;
759 caddr_t packet_ptr;
760
761 loop:
762 /* Set NIC to page 1 registers to get 'current' pointer. */
763 NIC_PUT(sc, ED_P0_CR, sc->cr_proto | ED_CR_PAGE_1 | ED_CR_STA);
764
765 /*
766 * 'sc->next_packet' is the logical beginning of the ring-buffer - i.e.
767 * it points to where new data has been buffered. The 'CURR' (current)
768 * register points to the logical end of the ring-buffer - i.e. it
769 * points to where additional new data will be added. We loop here
770 * until the logical beginning equals the logical end (or in other
771 * words, until the ring-buffer is empty).
772 */
773 current = NIC_GET(sc, ED_P1_CURR);
774 if (sc->next_packet == current)
775 return;
776
777 /* Set NIC to page 0 registers to update boundary register. */
778 NIC_PUT(sc, ED_P1_CR, sc->cr_proto | ED_CR_PAGE_0 | ED_CR_STA);
779
780 do {
781 /* Get pointer to this buffer's header structure. */
782 packet_ptr = sc->mem_ring +
783 ((sc->next_packet - sc->rec_page_start) << ED_PAGE_SHIFT);
784
785 /*
786 * The byte count includes a 4 byte header that was added by
787 * the NIC.
788 */
789 packet_hdr = *(struct ae_ring *) packet_ptr;
790 lenp = (u_char *) &((struct ae_ring *) packet_ptr)->count;
791 len = lenp[0] | (lenp[1] << 8);
792 packet_hdr.count = len;
793
794 /*
795 * Try do deal with old, buggy chips that sometimes duplicate
796 * the low byte of the length into the high byte. We do this
797 * by simply ignoring the high byte of the length and always
798 * recalculating it.
799 *
800 * NOTE: sc->next_packet is pointing at the current packet.
801 */
802 if (packet_hdr.next_packet >= sc->next_packet)
803 nlen = (packet_hdr.next_packet - sc->next_packet);
804 else
805 nlen = ((packet_hdr.next_packet - sc->rec_page_start) +
806 (sc->rec_page_stop - sc->next_packet));
807 --nlen;
808 if ((len & ED_PAGE_MASK) + sizeof(packet_hdr) > ED_PAGE_SIZE)
809 --nlen;
810 len = (len & ED_PAGE_MASK) | (nlen << ED_PAGE_SHIFT);
811 #ifdef DIAGNOSTIC
812 if (len != packet_hdr.count) {
813 printf("%s: length does not match next packet pointer\n",
814 sc->sc_dev.dv_xname);
815 printf("%s: len %04x nlen %04x start %02x first %02x curr %02x next %02x stop %02x\n",
816 sc->sc_dev.dv_xname, packet_hdr.count, len,
817 sc->rec_page_start, sc->next_packet, current,
818 packet_hdr.next_packet, sc->rec_page_stop);
819 }
820 #endif
821
822 /*
823 * Be fairly liberal about what we allow as a "reasonable"
824 * length so that a [crufty] packet will make it to BPF (and
825 * can thus be analyzed). Note that all that is really
826 * important is that we have a length that will fit into one
827 * mbuf cluster or less; the upper layer protocols can then
828 * figure out the length from their own length field(s).
829 */
830 if (len <= MCLBYTES &&
831 packet_hdr.next_packet >= sc->rec_page_start &&
832 packet_hdr.next_packet < sc->rec_page_stop) {
833 /* Go get packet. */
834 aeread(sc, packet_ptr + sizeof(struct ae_ring),
835 len - sizeof(struct ae_ring));
836 ++sc->sc_arpcom.ac_if.if_ipackets;
837 } else {
838 /* Really BAD. The ring pointers are corrupted. */
839 log(LOG_ERR,
840 "%s: NIC memory corrupt - invalid packet length %d\n",
841 sc->sc_dev.dv_xname, len);
842 ++sc->sc_arpcom.ac_if.if_ierrors;
843 aereset(sc);
844 return;
845 }
846
847 /* Update next packet pointer. */
848 sc->next_packet = packet_hdr.next_packet;
849
850 /*
851 * Update NIC boundary pointer - being careful to keep it one
852 * buffer behind (as recommended by NS databook).
853 */
854 boundary = sc->next_packet - 1;
855 if (boundary < sc->rec_page_start)
856 boundary = sc->rec_page_stop - 1;
857 NIC_PUT(sc, ED_P0_BNRY, boundary);
858 } while (sc->next_packet != current);
859
860 goto loop;
861 }
862
863 /* Ethernet interface interrupt processor. */
864 void
865 aeintr(arg, slot)
866 void *arg;
867 int slot;
868 {
869 struct ae_softc *sc = arg;
870 struct ifnet *ifp = &sc->sc_arpcom.ac_if;
871 u_char isr;
872
873 aeintr_ctr++;
874
875 /* Set NIC to page 0 registers. */
876 NIC_PUT(sc, ED_P0_CR, sc->cr_proto | ED_CR_PAGE_0 | ED_CR_STA);
877
878 isr = NIC_GET(sc, ED_P0_ISR);
879 if (!isr)
880 return;
881
882 /* Loop until there are no more new interrupts. */
883 for (;;) {
884 /*
885 * Reset all the bits that we are 'acknowledging' by writing a
886 * '1' to each bit position that was set.
887 * (Writing a '1' *clears* the bit.)
888 */
889 NIC_PUT(sc, ED_P0_ISR, isr);
890
891 /*
892 * Handle transmitter interrupts. Handle these first because
893 * the receiver will reset the board under some conditions.
894 */
895 if (isr & (ED_ISR_PTX | ED_ISR_TXE)) {
896 u_char collisions = NIC_GET(sc, ED_P0_NCR) & 0x0f;
897
898 /*
899 * Check for transmit error. If a TX completed with an
900 * error, we end up throwing the packet away. Really
901 * the only error that is possible is excessive
902 * collisions, and in this case it is best to allow the
903 * automatic mechanisms of TCP to backoff the flow. Of
904 * course, with UDP we're screwed, but this is expected
905 * when a network is heavily loaded.
906 */
907 (void) NIC_GET(sc, ED_P0_TSR);
908 if (isr & ED_ISR_TXE) {
909 /*
910 * Excessive collisions (16).
911 */
912 if ((NIC_GET(sc, ED_P0_TSR) & ED_TSR_ABT)
913 && (collisions == 0)) {
914 /*
915 * When collisions total 16, the P0_NCR
916 * will indicate 0, and the TSR_ABT is
917 * set.
918 */
919 collisions = 16;
920 }
921 /* Update output errors counter. */
922 ++ifp->if_oerrors;
923 } else {
924 /*
925 * Update total number of successfully
926 * transmitted packets.
927 */
928 ++ifp->if_opackets;
929 }
930
931 /* Done with the buffer. */
932 sc->txb_inuse--;
933
934 /* Clear watchdog timer. */
935 ifp->if_timer = 0;
936 ifp->if_flags &= ~IFF_OACTIVE;
937
938 /*
939 * Add in total number of collisions on last
940 * transmission.
941 */
942 ifp->if_collisions += collisions;
943
944 /*
945 * Decrement buffer in-use count if not zero (can only
946 * be zero if a transmitter interrupt occured while not
947 * actually transmitting).
948 * If data is ready to transmit, start it transmitting,
949 * otherwise defer until after handling receiver.
950 */
951 if (sc->txb_inuse > 0)
952 ae_xmit(sc);
953 }
954 /* Handle receiver interrupts. */
955 if (isr & (ED_ISR_PRX | ED_ISR_RXE | ED_ISR_OVW)) {
956 /*
957 * Overwrite warning. In order to make sure that a
958 * lockup of the local DMA hasn't occurred, we reset
959 * and re-init the NIC. The NSC manual suggests only a
960 * partial reset/re-init is necessary - but some chips
961 * seem to want more. The DMA lockup has been seen
962 * only with early rev chips - Methinks this bug was
963 * fixed in later revs. -DG
964 */
965 if (isr & ED_ISR_OVW) {
966 ++ifp->if_ierrors;
967 #ifdef DIAGNOSTIC
968 log(LOG_WARNING,
969 "%s: warning - receiver ring buffer overrun\n",
970 sc->sc_dev.dv_xname);
971 #endif
972 /* Stop/reset/re-init NIC. */
973 aereset(sc);
974 } else {
975 /*
976 * Receiver Error. One or more of: CRC error,
977 * frame alignment error FIFO overrun, or
978 * missed packet.
979 */
980 if (isr & ED_ISR_RXE) {
981 ++ifp->if_ierrors;
982 #ifdef AE_DEBUG
983 printf("%s: receive error %x\n",
984 sc->sc_dev.dv_xname,
985 NIC_GET(sc, ED_P0_RSR));
986 #endif
987 }
988 /*
989 * Go get the packet(s)
990 * XXX - Doing this on an error is dubious
991 * because there shouldn't be any data to get
992 * (we've configured the interface to not
993 * accept packets with errors).
994 */
995 ae_rint(sc);
996 }
997 }
998 /*
999 * If it looks like the transmitter can take more data, attempt
1000 * to start output on the interface. This is done after
1001 * handling the receiver to give the receiver priority.
1002 */
1003 aestart(ifp);
1004
1005 /*
1006 * Return NIC CR to standard state: page 0, remote DMA
1007 * complete, start (toggling the TXP bit off, even if was just
1008 * set in the transmit routine, is *okay* - it is 'edge'
1009 * triggered from low to high).
1010 */
1011 NIC_PUT(sc, ED_P0_CR, sc->cr_proto | ED_CR_PAGE_0 | ED_CR_STA);
1012
1013 /*
1014 * If the Network Talley Counters overflow, read them to reset
1015 * them. It appears that old 8390's won't clear the ISR flag
1016 * otherwise - resulting in an infinite loop.
1017 */
1018 if (isr & ED_ISR_CNT) {
1019 static u_char dummy;
1020 dummy = NIC_GET(sc, ED_P0_CNTR0);
1021 dummy = NIC_GET(sc, ED_P0_CNTR1);
1022 dummy = NIC_GET(sc, ED_P0_CNTR2);
1023 }
1024 isr = NIC_GET(sc, ED_P0_ISR);
1025 if (!isr)
1026 return;
1027 }
1028 }
1029
1030 /*
1031 * Process an ioctl request. This code needs some work - it looks pretty ugly.
1032 */
1033 int
1034 aeioctl(ifp, cmd, data)
1035 register struct ifnet *ifp;
1036 u_long cmd;
1037 caddr_t data;
1038 {
1039 struct ae_softc *sc = ifp->if_softc;
1040 register struct ifaddr *ifa = (struct ifaddr *) data;
1041 struct ifreq *ifr = (struct ifreq *) data;
1042 int s, error = 0;
1043
1044 s = splnet();
1045
1046 switch (cmd) {
1047
1048 case SIOCSIFADDR:
1049 ifp->if_flags |= IFF_UP;
1050
1051 switch (ifa->ifa_addr->sa_family) {
1052 #ifdef INET
1053 case AF_INET:
1054 aeinit(sc);
1055 arp_ifinit(&sc->sc_arpcom, ifa);
1056 break;
1057 #endif
1058 #ifdef NS
1059 /* XXX - This code is probably wrong. */
1060 case AF_NS:
1061 {
1062 register struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
1063
1064 if (ns_nullhost(*ina))
1065 ina->x_host =
1066 *(union ns_host *) (sc->sc_arpcom.ac_enaddr);
1067 else
1068 bcopy(ina->x_host.c_host,
1069 sc->sc_arpcom.ac_enaddr,
1070 sizeof(sc->sc_arpcom.ac_enaddr));
1071 /* Set new address. */
1072 aeinit(sc);
1073 break;
1074 }
1075 #endif
1076 default:
1077 aeinit(sc);
1078 break;
1079 }
1080 break;
1081
1082 case SIOCSIFFLAGS:
1083 if ((ifp->if_flags & IFF_UP) == 0 &&
1084 (ifp->if_flags & IFF_RUNNING) != 0) {
1085 /*
1086 * If interface is marked down and it is running, then
1087 * stop it.
1088 */
1089 aestop(sc);
1090 ifp->if_flags &= ~IFF_RUNNING;
1091 } else
1092 if ((ifp->if_flags & IFF_UP) != 0 &&
1093 (ifp->if_flags & IFF_RUNNING) == 0) {
1094 /*
1095 * If interface is marked up and it is stopped, then
1096 * start it.
1097 */
1098 aeinit(sc);
1099 } else {
1100 /*
1101 * Reset the interface to pick up changes in any other
1102 * flags that affect hardware registers.
1103 */
1104 aestop(sc);
1105 aeinit(sc);
1106 }
1107 break;
1108
1109 case SIOCADDMULTI:
1110 case SIOCDELMULTI:
1111 /* Update our multicast list. */
1112 error = (cmd == SIOCADDMULTI) ?
1113 ether_addmulti(ifr, &sc->sc_arpcom) :
1114 ether_delmulti(ifr, &sc->sc_arpcom);
1115
1116 if (error == ENETRESET) {
1117 /*
1118 * Multicast list has changed; set the hardware filter
1119 * accordingly.
1120 */
1121 aestop(sc); /* XXX for ds_setmcaf? */
1122 aeinit(sc);
1123 error = 0;
1124 }
1125 break;
1126
1127 default:
1128 error = EINVAL;
1129 break;
1130 }
1131
1132 splx(s);
1133 return (error);
1134 }
1135
1136 /*
1137 * Retreive packet from shared memory and send to the next level up via
1138 * ether_input(). If there is a BPF listener, give a copy to BPF, too.
1139 */
1140 void
1141 aeread(sc, buf, len)
1142 struct ae_softc *sc;
1143 caddr_t buf;
1144 int len;
1145 {
1146 struct ifnet *ifp = &sc->sc_arpcom.ac_if;
1147 struct mbuf *m;
1148 struct ether_header *eh;
1149
1150 /* Pull packet off interface. */
1151 m = aeget(sc, buf, len);
1152 if (m == 0) {
1153 ifp->if_ierrors++;
1154 return;
1155 }
1156
1157 ifp->if_ipackets++;
1158
1159 /* We assume that the header fits entirely in one mbuf. */
1160 eh = mtod(m, struct ether_header *);
1161
1162 #if NBPFILTER > 0
1163 /*
1164 * Check if there's a BPF listener on this interface.
1165 * If so, hand off the raw packet to bpf.
1166 */
1167 if (ifp->if_bpf) {
1168 bpf_mtap(ifp->if_bpf, m);
1169
1170 /*
1171 * Note that the interface cannot be in promiscuous mode if
1172 * there are no BPF listeners. And if we are in promiscuous
1173 * mode, we have to check if this packet is really ours.
1174 */
1175 if ((ifp->if_flags & IFF_PROMISC) &&
1176 (eh->ether_dhost[0] & 1) == 0 && /* !mcast and !bcast */
1177 bcmp(eh->ether_dhost, sc->sc_arpcom.ac_enaddr,
1178 sizeof(eh->ether_dhost)) != 0) {
1179 m_freem(m);
1180 return;
1181 }
1182 }
1183 #endif
1184
1185 /* Fix up data start offset in mbuf to point past ether header. */
1186 m_adj(m, sizeof(struct ether_header));
1187 ether_input(ifp, eh, m);
1188 }
1189
1190 /*
1191 * Supporting routines.
1192 */
1193 /*
1194 * Given a source and destination address, copy 'amount' of a packet from the
1195 * ring buffer into a linear destination buffer. Takes into account ring-wrap.
1196 */
1197 static inline caddr_t
1198 ae_ring_copy(sc, src, dst, amount)
1199 struct ae_softc *sc;
1200 caddr_t src, dst;
1201 int amount;
1202 {
1203 u_short tmp_amount;
1204
1205 /* Does copy wrap to lower addr in ring buffer? */
1206 if (src + amount > sc->mem_end) {
1207 tmp_amount = sc->mem_end - src;
1208
1209 /* Copy amount up to end of NIC memory. */
1210 byte_copy(src, dst, tmp_amount);
1211
1212 amount -= tmp_amount;
1213 src = sc->mem_ring;
1214 dst += tmp_amount;
1215 }
1216 byte_copy(src, dst, amount);
1217
1218 return (src + amount);
1219 }
1220
1221 /*
1222 * Copy data from receive buffer to end of mbuf chain allocate additional mbufs
1223 * as needed. Return pointer to last mbuf in chain.
1224 * sc = ae info (softc)
1225 * src = pointer in ae ring buffer
1226 * dst = pointer to last mbuf in mbuf chain to copy to
1227 * amount = amount of data to copy
1228 */
1229 struct mbuf *
1230 aeget(sc, src, total_len)
1231 struct ae_softc *sc;
1232 caddr_t src;
1233 u_short total_len;
1234 {
1235 struct ifnet *ifp = &sc->sc_arpcom.ac_if;
1236 struct mbuf *top, **mp, *m;
1237 int len;
1238
1239 MGETHDR(m, M_DONTWAIT, MT_DATA);
1240 if (m == 0)
1241 return 0;
1242 m->m_pkthdr.rcvif = ifp;
1243 m->m_pkthdr.len = total_len;
1244 len = MHLEN;
1245 top = 0;
1246 mp = ⊤
1247
1248 while (total_len > 0) {
1249 if (top) {
1250 MGET(m, M_DONTWAIT, MT_DATA);
1251 if (m == 0) {
1252 m_freem(top);
1253 return 0;
1254 }
1255 len = MLEN;
1256 }
1257 if (total_len >= MINCLSIZE) {
1258 MCLGET(m, M_DONTWAIT);
1259 if (m->m_flags & M_EXT)
1260 len = MCLBYTES;
1261 }
1262 m->m_len = len = min(total_len, len);
1263 src = ae_ring_copy(sc, src, mtod(m, caddr_t), (int) len);
1264 total_len -= len;
1265 *mp = m;
1266 mp = &m->m_next;
1267 }
1268
1269 return top;
1270 }
1271 /*
1272 * Compute the multicast address filter from the list of multicast addresses we
1273 * need to listen to.
1274 */
1275 void
1276 ae_getmcaf(ac, af)
1277 struct arpcom *ac;
1278 u_char *af;
1279 {
1280 struct ifnet *ifp = &ac->ac_if;
1281 struct ether_multi *enm;
1282 register u_char *cp, c;
1283 register u_long crc;
1284 register int i, len;
1285 struct ether_multistep step;
1286
1287 /*
1288 * Set up multicast address filter by passing all multicast addresses
1289 * through a crc generator, and then using the high order 6 bits as an
1290 * index into the 64 bit logical address filter. The high order bit
1291 * selects the word, while the rest of the bits select the bit within
1292 * the word.
1293 */
1294
1295 if (ifp->if_flags & IFF_PROMISC) {
1296 ifp->if_flags |= IFF_ALLMULTI;
1297 for (i = 0; i < 8; i++)
1298 af[i] = 0xff;
1299 return;
1300 }
1301 for (i = 0; i < 8; i++)
1302 af[i] = 0;
1303 ETHER_FIRST_MULTI(step, ac, enm);
1304 while (enm != NULL) {
1305 if (bcmp(enm->enm_addrlo, enm->enm_addrhi,
1306 sizeof(enm->enm_addrlo)) != 0) {
1307 /*
1308 * We must listen to a range of multicast addresses.
1309 * For now, just accept all multicasts, rather than
1310 * trying to set only those filter bits needed to match
1311 * the range. (At this time, the only use of address
1312 * ranges is for IP multicast routing, for which the
1313 * range is big enough to require all bits set.)
1314 */
1315 ifp->if_flags |= IFF_ALLMULTI;
1316 for (i = 0; i < 8; i++)
1317 af[i] = 0xff;
1318 return;
1319 }
1320 cp = enm->enm_addrlo;
1321 crc = 0xffffffff;
1322 for (len = sizeof(enm->enm_addrlo); --len >= 0;) {
1323 c = *cp++;
1324 for (i = 8; --i >= 0;) {
1325 if (((crc & 0x80000000) ? 1 : 0) ^ (c & 0x01)) {
1326 crc <<= 1;
1327 crc ^= 0x04c11db6 | 1;
1328 } else
1329 crc <<= 1;
1330 c >>= 1;
1331 }
1332 }
1333 /* Just want the 6 most significant bits. */
1334 crc >>= 26;
1335
1336 /* Turn on the corresponding bit in the filter. */
1337 af[crc >> 3] |= 1 << (crc & 0x7);
1338
1339 ETHER_NEXT_MULTI(step, enm);
1340 }
1341 ifp->if_flags &= ~IFF_ALLMULTI;
1342 }
1343 /*
1344 * Copy packet from mbuf to the board memory
1345 *
1346 * Currently uses an extra buffer/extra memory copy,
1347 * unless the whole packet fits in one mbuf.
1348 *
1349 */
1350 u_short
1351 ae_put(sc, m, buf)
1352 struct ae_softc *sc;
1353 struct mbuf *m;
1354 caddr_t buf;
1355 {
1356 u_char *data, savebyte[2];
1357 int len, wantbyte;
1358 u_short totlen = 0;
1359
1360 wantbyte = 0;
1361
1362 for (; m ; m = m->m_next) {
1363 data = mtod(m, u_char *);
1364 len = m->m_len;
1365 totlen += len;
1366 if (len > 0) {
1367 /* Finish the last word. */
1368 if (wantbyte) {
1369 savebyte[1] = *data;
1370 word_copy(savebyte, buf, 2);
1371 buf += 2;
1372 data++;
1373 len--;
1374 wantbyte = 0;
1375 }
1376 /* Output contiguous words. */
1377 if (len > 1) {
1378 word_copy(data, buf, len);
1379 buf += len & ~1;
1380 data += len & ~1;
1381 len &= 1;
1382 }
1383 /* Save last byte, if necessary. */
1384 if (len == 1) {
1385 savebyte[0] = *data;
1386 wantbyte = 1;
1387 }
1388 }
1389 }
1390
1391 if (wantbyte) {
1392 savebyte[1] = 0;
1393 word_copy(savebyte, buf, 2);
1394 }
1395 return (totlen);
1396 }
1397