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