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