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