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