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