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