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