if_ae.c revision 1.4 1 /*
2 * Device driver for National Semiconductor DS8390 based ethernet adapters.
3 *
4 * Based on original ISA bus driver by David Greenman, 29-April-1993
5 *
6 * Copyright (C) 1993, David Greenman. This software may be used, modified,
7 * copied, distributed, and sold, in both source and binary form provided
8 * that the above copyright and these terms are retained. Under no
9 * circumstances is the author responsible for the proper functioning
10 * of this software, nor does the author assume any responsibility
11 * for damages incurred with its use.
12 *
13 * Adapted for MacBSD by Brad Parker <brad (at) fcr.com>
14 *
15 * Currently supports:
16 * Apples NB Ethernet card
17 * Interlan A310 Nubus Ethernet card
18 * Cayman Systems GatorCard
19 */
20
21 /*
22 * $Id: if_ae.c,v 1.4 1994/01/24 22:29:40 briggs Exp $
23 */
24
25 /*
26 * Modification history
27 *
28 * $Log: if_ae.c,v $
29 * Revision 1.4 1994/01/24 22:29:40 briggs
30 * Apply patch as suggested by Theo.
31 *
32 * Revision 1.3 1993/12/21 03:18:04 briggs
33 * Update ethernet driver to use config.new. At least, it's a first stab
34 * working from mycroft's magnum changes to if_ed.c.
35 *
36 * Revision 1.2 1993/12/15 03:38:20 briggs
37 * Get rid of IFF_ALTPHYS and hence IFF_LLC0 reference. It doesn't appear
38 * to have been used in this driver ;-)
39 *
40 * Revision 1.1 1993/11/29 00:32:43 briggs
41 * Update to current work in progress. This includes an update to
42 * use config.new.
43 * Numerous updates to console so it works better on the SE/30 screen.
44 * Some nice changes from Brad Parker for handling NuBUS and an ethernet
45 * driver that I haven't worked on, yet.
46 *
47 *
48 */
49
50 #include "ae.h"
51 #if NAE > 0
52 /* bpfilter included here in case it is needed in future net includes */
53 #include "bpfilter.h"
54
55 #include "param.h"
56 #include "systm.h"
57 #include "errno.h"
58 #include "ioctl.h"
59 #include "mbuf.h"
60 #include "socket.h"
61 #include "syslog.h"
62
63 #include "net/if.h"
64 #include "net/if_dl.h"
65 #include "net/if_types.h"
66 #include "net/netisr.h"
67
68 #ifdef INET
69 #include "netinet/in.h"
70 #include "netinet/in_systm.h"
71 #include "netinet/in_var.h"
72 #include "netinet/ip.h"
73 #include "netinet/if_ether.h"
74 #endif
75
76 #ifdef NS
77 #include "netns/ns.h"
78 #include "netns/ns_if.h"
79 #endif
80
81 #if NBPFILTER > 0
82 #include "net/bpf.h"
83 #include "net/bpfdesc.h"
84 #endif
85
86 #include "sys/device.h"
87 #include "nubus.h"
88 #include "if_aereg.h"
89
90 struct ae_device {
91 struct device ae_dev;
92 /* struct nubusdev ae_nu;
93 struct intrhand ae_ih; */
94 };
95
96 /*
97 * ae_softc: per line info and status
98 */
99 struct ae_softc {
100 struct ae_device *sc_ae;
101
102 struct arpcom arpcom; /* ethernet common */
103
104 char *type_str; /* pointer to type string */
105 u_char vendor; /* interface vendor */
106 u_char type; /* interface type code */
107 #define APPLE_CARD(sc) ((sc)->vendor == AE_VENDOR_APPLE)
108 #define REG_MAP(sc, reg) (APPLE_CARD(sc) ? (0x0f-(reg))<<2 : (reg)<<2)
109 #define NIC_GET(sc, reg) ((sc)->nic_addr[REG_MAP(sc, reg)])
110 #define NIC_PUT(sc, reg, val) ((sc)->nic_addr[REG_MAP(sc, reg)] = (val))
111 volatile caddr_t nic_addr; /* NIC (DS8390) I/O bus address */
112 caddr_t rom_addr; /* on board prom address */
113 caddr_t smem_start; /* shared memory start address */
114 caddr_t smem_end; /* shared memory end address */
115 u_long smem_size; /* total shared memory size */
116 caddr_t smem_ring; /* start of RX ring-buffer (in smem) */
117
118 caddr_t bpf; /* BPF "magic cookie" */
119
120 u_char xmit_busy; /* transmitter is busy */
121 u_char txb_cnt; /* Number of transmit buffers */
122 u_char txb_next; /* Pointer to next buffer ready to xmit */
123 u_short txb_next_len; /* next xmit buffer length */
124 u_char data_buffered; /* data has been buffered in interface memory */
125 u_char tx_page_start; /* first page of TX buffer area */
126
127 u_char rec_page_start; /* first page of RX ring-buffer */
128 u_char rec_page_stop; /* last page of RX ring-buffer */
129 u_char next_packet; /* pointer to next unread RX packet */
130 } ae_softc[NAE];
131
132 void ae_find();
133 int ae_attach(), ae_init(), aeintr(), ae_ioctl(), ae_probe(),
134 ae_start(), ae_reset(), ae_watchdog();
135
136 struct cfdriver aecd =
137 { NULL, "ae", ae_probe, ae_attach, DV_IFNET, sizeof(struct ae_device), NULL, 0 };
138
139 static void ae_stop();
140 static inline void ae_rint();
141 static inline void ae_xmit();
142 static inline char *ae_ring_copy();
143
144 extern int ether_output();
145
146 #define ETHER_MIN_LEN 64
147 #define ETHER_MAX_LEN 1518
148 #define ETHER_ADDR_LEN 6
149 #define ETHER_HDR_SIZE 14
150
151 char ae_name[] = "8390 Nubus Ethernet card";
152 static char zero = 0;
153 static u_char ones = 0xff;
154
155 int
156 ae_probe(parent, cf, aux)
157 struct cfdriver *parent;
158 struct cfdata *cf;
159 void *aux;
160 {
161 register struct nubus_hw *nu = (struct nubus_hw *) aux;
162 struct ae_softc *sc = &ae_softc[cf->cf_unit];
163 int i, memsize;
164 int flags = 0;
165
166 if (nu->Slot.type != NUBUS_NETWORK)
167 return 0;
168
169 /*
170 * Try to determine what type of card this is...
171 sc->vendor == AE_VENDOR_APPLE;
172 */
173
174 /* see if it's an Interlan/GatorCard */
175 sc->rom_addr = nu->addr + GC_ROM_OFFSET;
176 if (sc->rom_addr[0x18] == 0x0 &&
177 sc->rom_addr[0x1c] == 0x55) {
178 sc->vendor = AE_VENDOR_INTERLAN;
179 }
180
181 sc->type = 0;
182
183 switch (sc->vendor) {
184 case AE_VENDOR_INTERLAN:
185 sc->nic_addr = nu->addr + GC_NIC_OFFSET;
186 sc->rom_addr = nu->addr + GC_ROM_OFFSET;
187 sc->smem_start = nu->addr + GC_DATA_OFFSET;
188 sc->type_str = "Interlan";
189 memsize = 8192;
190
191 /* reset the NIC chip */
192 *((caddr_t)nu->addr + GC_RESET_OFFSET) = (char)zero;
193
194 /* Get station address from on-board ROM */
195 for (i = 0; i < ETHER_ADDR_LEN; ++i)
196 sc->arpcom.ac_enaddr[i] = *(sc->rom_addr + i*4);
197 break;
198
199 case AE_VENDOR_APPLE:
200 default:
201 sc->nic_addr = nu->addr + AE_NIC_OFFSET;
202 sc->rom_addr = nu->addr + AE_ROM_OFFSET;
203 sc->smem_start = nu->addr + AE_DATA_OFFSET;
204 sc->type_str = "Apple";
205 memsize = 8192;
206
207 /* Get station address from on-board ROM */
208 for (i = 0; i < ETHER_ADDR_LEN; ++i)
209 sc->arpcom.ac_enaddr[i] = *(sc->rom_addr + i*2);
210 break;
211 }
212
213 /*
214 * allocate one xmit buffer if < 16k, two buffers otherwise
215 */
216 if ((memsize < 16384) || (flags & AE_FLAGS_NO_DOUBLE_BUFFERING)) {
217 sc->smem_ring = sc->smem_start + (AE_PAGE_SIZE * AE_TXBUF_SIZE);
218 sc->txb_cnt = 1;
219 sc->rec_page_start = AE_TXBUF_SIZE;
220 } else {
221 sc->smem_ring = sc->smem_start + (AE_PAGE_SIZE * AE_TXBUF_SIZE * 2);
222 sc->txb_cnt = 2;
223 sc->rec_page_start = AE_TXBUF_SIZE * 2;
224 }
225
226 sc->smem_size = memsize;
227 sc->smem_end = sc->smem_start + memsize;
228 sc->rec_page_stop = memsize / AE_PAGE_SIZE;
229 sc->tx_page_start = 0;
230
231 /*
232 * Now zero memory and verify that it is clear
233 */
234 bzero(sc->smem_start, memsize);
235
236 for (i = 0; i < memsize; ++i)
237 if (sc->smem_start[i]) {
238 printf(": failed to clear shared memory at %x\n",
239 sc->smem_start + i);
240
241 return(0);
242 }
243
244 #ifdef DEBUG_PRINT
245 printf("nic_addr %x, rom_addr %x\n",
246 sc->nic_addr, sc->rom_addr);
247 printf("smem_size %d\n", sc->smem_size);
248 printf("smem_start %x, smem_ring %x, smem_end %x\n",
249 sc->smem_start, sc->smem_ring, sc->smem_end);
250 printf("phys address %02x:%02x:%02x:%02x:%02x:%02x\n",
251 sc->arpcom.ac_enaddr[0],
252 sc->arpcom.ac_enaddr[1],
253 sc->arpcom.ac_enaddr[2],
254 sc->arpcom.ac_enaddr[3],
255 sc->arpcom.ac_enaddr[4],
256 sc->arpcom.ac_enaddr[5]);
257 #endif
258
259 return(1);
260 }
261
262 /*
263 * Install interface into kernel networking data structures
264 */
265 int
266 ae_attach(parent, self, aux)
267 struct cfdriver *parent, *self;
268 void *aux;
269 {
270 struct nubus_hw *nu = aux;
271 struct ae_device *ae = (struct ae_device *) self;
272 struct ae_softc *sc = &ae_softc[ae->ae_dev.dv_unit];
273 struct cfdata *cf = ae->ae_dev.dv_cfdata;
274 struct ifnet *ifp = &sc->arpcom.ac_if;
275 struct ifaddr *ifa;
276 struct sockaddr_dl *sdl;
277
278 sc->sc_ae = ae;
279
280 /*
281 * Set interface to stopped condition (reset)
282 */
283 ae_stop(sc);
284
285 /*
286 * Initialize ifnet structure
287 */
288 ifp->if_unit = ae->ae_dev.dv_unit;
289 ifp->if_name = aecd.cd_name;
290 ifp->if_mtu = ETHERMTU;
291 ifp->if_output = ether_output;
292 ifp->if_init = ae_init;
293 ifp->if_start = ae_start;
294 ifp->if_ioctl = ae_ioctl;
295 ifp->if_reset = ae_reset;
296 ifp->if_watchdog = ae_watchdog;
297 ifp->if_flags = (IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS);
298
299 #if 0
300 /*
301 * Set default state for ALTPHYS flag (used to disable the transceiver
302 * for AUI operation), based on compile-time config option.
303 */
304 if (cf->cf_flags & AE_FLAGS_DISABLE_TRANSCEIVER)
305 ifp->if_flags |= IFF_ALTPHYS;
306 #endif
307
308 /*
309 * Attach the interface
310 */
311 if_attach(ifp);
312
313 /*
314 * Search down the ifa address list looking for the AF_LINK type entry
315 */
316 ifa = ifp->if_addrlist;
317 while ((ifa != 0) && (ifa->ifa_addr != 0) &&
318 (ifa->ifa_addr->sa_family != AF_LINK))
319 ifa = ifa->ifa_next;
320 /*
321 * If we find an AF_LINK type entry we fill in the hardware address.
322 * This is useful for netstat(1) to keep track of which interface
323 * is which.
324 */
325 if ((ifa != 0) && (ifa->ifa_addr != 0)) {
326 /*
327 * Fill in the link-level address for this interface
328 */
329 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
330 sdl->sdl_type = IFT_ETHER;
331 sdl->sdl_alen = ETHER_ADDR_LEN;
332 sdl->sdl_slen = 0;
333 bcopy(sc->arpcom.ac_enaddr, LLADDR(sdl), ETHER_ADDR_LEN);
334 }
335
336 /*
337 * Print additional info when attached
338 */
339 printf(": address %s, ", ether_sprintf(sc->arpcom.ac_enaddr));
340
341 if (sc->type_str && (*sc->type_str != 0))
342 printf("type %s ", sc->type_str);
343 else
344 printf("type unknown (0x%x) ", sc->type);
345
346 printf("\n");
347
348 /*
349 * If BPF is in the kernel, call the attach for it
350 */
351 #if NBPFILTER > 0
352 bpfattach(&sc->bpf, ifp, DLT_EN10MB, sizeof(struct ether_header));
353 #endif
354 }
355
356 /*
357 * Reset interface.
358 */
359 int
360 ae_reset(sc)
361 struct ae_softc *sc;
362 {
363 int s;
364
365 s = splnet();
366
367 /*
368 * Stop interface and re-initialize.
369 */
370 ae_stop(sc);
371 ae_init(sc);
372
373 (void) splx(s);
374 }
375
376 /*
377 * Take interface offline.
378 */
379 void
380 ae_stop(sc)
381 struct ae_softc *sc;
382 {
383 int n = 5000;
384
385 /*
386 * Stop everything on the interface, and select page 0 registers.
387 */
388 NIC_PUT(sc, AE_P0_CR, AE_CR_RD2|AE_CR_STP);
389
390 /*
391 * Wait for interface to enter stopped state, but limit # of checks
392 * to 'n' (about 5ms). It shouldn't even take 5us on modern
393 * DS8390's, but just in case it's an old one.
394 */
395 while (((NIC_GET(sc, AE_P0_ISR) & AE_ISR_RST) == 0) && --n);
396 }
397
398 /*
399 * Device timeout/watchdog routine. Entered if the device neglects to
400 * generate an interrupt after a transmit has been started on it.
401 */
402 int
403 ae_watchdog(unit)
404 short unit;
405 {
406 log(LOG_ERR, "ae%d: device timeout\n", unit);
407 {
408 struct ae_softc *sc = &ae_softc[unit];
409 printf("cr %x, isr %x\n", NIC_GET(sc, AE_P0_CR), NIC_GET(sc, AE_P0_ISR));
410 /* via_dump(); */
411 if (NIC_GET(sc, AE_P0_ISR)) {
412 aeintr(0);
413 return;
414 }
415 }
416 ae_reset(unit);
417 }
418
419 /*
420 * Initialize device.
421 */
422 ae_init(sc)
423 struct ae_softc *sc;
424 {
425 struct ifnet *ifp = &sc->arpcom.ac_if;
426 int i, s;
427 u_char command;
428
429
430 /* address not known */
431 if (ifp->if_addrlist == (struct ifaddr *)0) return;
432
433 /*
434 * Initialize the NIC in the exact order outlined in the NS manual.
435 * This init procedure is "mandatory"...don't change what or when
436 * things happen.
437 */
438 s = splnet();
439
440 /* reset transmitter flags */
441 sc->data_buffered = 0;
442 sc->xmit_busy = 0;
443 sc->arpcom.ac_if.if_timer = 0;
444
445 sc->txb_next = 0;
446
447 /* This variable is used below - don't move this assignment */
448 sc->next_packet = sc->rec_page_start + 1;
449
450 #ifdef DEBUG_PRINT
451 printf("page_start %d, page_stop %d, next %d\n",
452 sc->rec_page_start, sc->rec_page_stop, sc->next_packet);
453 #endif
454
455 /*
456 * Set interface for page 0, Remote DMA complete, Stopped
457 */
458 NIC_PUT(sc, AE_P0_CR, AE_CR_RD2|AE_CR_STP);
459
460 /*
461 * Set FIFO threshold to 4, No auto-init Remote DMA, Burst mode,
462 * byte order=80x86, word-wide DMA xfers,
463 */
464 NIC_PUT(sc, AE_P0_DCR, AE_DCR_FT1|AE_DCR_BMS|AE_DCR_WTS);
465
466 /*
467 * Clear Remote Byte Count Registers
468 */
469 NIC_PUT(sc, AE_P0_RBCR0, zero);
470 NIC_PUT(sc, AE_P0_RBCR1, zero);
471
472 /*
473 * Enable reception of broadcast packets
474 */
475 NIC_PUT(sc, AE_P0_RCR, AE_RCR_AB);
476
477 /*
478 * Place NIC in internal loopback mode
479 */
480 NIC_PUT(sc, AE_P0_TCR, AE_TCR_LB0);
481
482 /*
483 * Initialize transmit/receive (ring-buffer) Page Start
484 */
485 NIC_PUT(sc, AE_P0_TPSR, sc->tx_page_start);
486 NIC_PUT(sc, AE_P0_PSTART, sc->rec_page_start);
487
488 /*
489 * Initialize Receiver (ring-buffer) Page Stop and Boundry
490 */
491 NIC_PUT(sc, AE_P0_PSTOP, sc->rec_page_stop);
492 NIC_PUT(sc, AE_P0_BNRY, sc->rec_page_start);
493
494 /*
495 * Clear all interrupts. A '1' in each bit position clears the
496 * corresponding flag.
497 */
498 NIC_PUT(sc, AE_P0_ISR, ones);
499
500 /*
501 * Enable the following interrupts: receive/transmit complete,
502 * receive/transmit error, and Receiver OverWrite.
503 *
504 * Counter overflow and Remote DMA complete are *not* enabled.
505 */
506 NIC_PUT(sc, AE_P0_IMR,
507 AE_IMR_PRXE|AE_IMR_PTXE|AE_IMR_RXEE|AE_IMR_TXEE|AE_IMR_OVWE);
508
509 /*
510 * Program Command Register for page 1
511 */
512 NIC_PUT(sc, AE_P0_CR, AE_CR_PAGE_1|AE_CR_RD2|AE_CR_STP);
513
514 /*
515 * Copy out our station address
516 */
517 for (i = 0; i < ETHER_ADDR_LEN; ++i)
518 NIC_PUT(sc, AE_P1_PAR0 + i, sc->arpcom.ac_enaddr[i]);
519
520 #if NBPFILTER > 0
521 /*
522 * Initialize multicast address hashing registers to accept
523 * all multicasts (only used when in promiscuous mode)
524 */
525 for (i = 0; i < 8; ++i)
526 NIC_PUT(sc, AE_P1_MAR0 + i, 0xff);
527 #endif
528
529 /*
530 * Set Current Page pointer to next_packet (initialized above)
531 */
532 NIC_PUT(sc, AE_P1_CURR, sc->next_packet);
533
534 /*
535 * Set Command Register for page 0, Remote DMA complete,
536 * and interface Start.
537 */
538 NIC_PUT(sc, AE_P1_CR, AE_CR_RD2|AE_CR_STA);
539
540 /*
541 * Take interface out of loopback
542 */
543 NIC_PUT(sc, AE_P0_TCR, zero);
544
545 /*
546 * Set 'running' flag, and clear output active flag.
547 */
548 ifp->if_flags |= IFF_RUNNING;
549 ifp->if_flags &= ~IFF_OACTIVE;
550
551 /* XXXXXX */
552 add_nubus_intr(sc->rom_addr - GC_ROM_OFFSET, aeintr, sc - ae_softc);
553
554 /*
555 * ...and attempt to start output
556 */
557 ae_start(ifp);
558
559 (void) splx(s);
560 }
561
562 /*
563 * This routine actually starts the transmission on the interface
564 */
565 static inline void ae_xmit(ifp)
566 struct ifnet *ifp;
567 {
568 struct ae_softc *sc = &ae_softc[ifp->if_unit];
569 u_short len = sc->txb_next_len;
570
571 /*
572 * Set NIC for page 0 register access
573 */
574 NIC_PUT(sc, AE_P0_CR, AE_CR_RD2|AE_CR_STA);
575
576 /*
577 * Set TX buffer start page
578 */
579 NIC_PUT(sc, AE_P0_TPSR, sc->tx_page_start +
580 sc->txb_next * AE_TXBUF_SIZE);
581
582 /*
583 * Set TX length
584 */
585 NIC_PUT(sc, AE_P0_TBCR0, len & 0xff);
586 NIC_PUT(sc, AE_P0_TBCR1, len >> 8);
587
588 /*
589 * Set page 0, Remote DMA complete, Transmit Packet, and *Start*
590 */
591 NIC_PUT(sc, AE_P0_CR, AE_CR_RD2|AE_CR_TXP|AE_CR_STA);
592
593 sc->xmit_busy = 1;
594 sc->data_buffered = 0;
595
596 /*
597 * Switch buffers if we are doing double-buffered transmits
598 */
599 if ((sc->txb_next == 0) && (sc->txb_cnt > 1))
600 sc->txb_next = 1;
601 else
602 sc->txb_next = 0;
603
604 /*
605 * Set a timer just in case we never hear from the board again
606 */
607 ifp->if_timer = 2;
608 }
609
610 /*
611 * Start output on interface.
612 * We make two assumptions here:
613 * 1) that the current priority is set to splnet _before_ this code
614 * is called *and* is returned to the appropriate priority after
615 * return
616 * 2) that the IFF_OACTIVE flag is checked before this code is called
617 * (i.e. that the output part of the interface is idle)
618 */
619 int
620 ae_start(ifp)
621 struct ifnet *ifp;
622 {
623 struct ae_softc *sc = &ae_softc[ifp->if_unit];
624 struct mbuf *m0, *m;
625 caddr_t buffer;
626 int len;
627
628 outloop:
629 /*
630 * See if there is room to send more data (i.e. one or both of the
631 * buffers is empty).
632 */
633 if (sc->data_buffered)
634 if (sc->xmit_busy) {
635 /*
636 * No room. Indicate this to the outside world
637 * and exit.
638 */
639 ifp->if_flags |= IFF_OACTIVE;
640 return;
641 } else {
642 /*
643 * Data is buffered, but we're not transmitting, so
644 * start the xmit on the buffered data.
645 * Note that ae_xmit() resets the data_buffered flag
646 * before returning.
647 */
648 ae_xmit(ifp);
649 }
650
651 IF_DEQUEUE(&sc->arpcom.ac_if.if_snd, m);
652 if (m == 0) {
653 /*
654 * The following isn't pretty; we are using the !OACTIVE flag to
655 * indicate to the outside world that we can accept an additional
656 * packet rather than that the transmitter is _actually_
657 * active. Indeed, the transmitter may be active, but if we haven't
658 * filled the secondary buffer with data then we still want to
659 * accept more.
660 * Note that it isn't necessary to test the data_buffered flag -
661 * we wouldn't have tried to de-queue the packet in the first place
662 * if it was set.
663 */
664 ifp->if_flags &= ~IFF_OACTIVE;
665 return;
666 }
667
668 /*
669 * Copy the mbuf chain into the transmit buffer
670 */
671 buffer = sc->smem_start + (sc->txb_next * AE_TXBUF_SIZE * AE_PAGE_SIZE);
672 len = 0;
673 for (m0 = m; m != 0; m = m->m_next) {
674 /*printf("ae: copy %d bytes @ %x\n", m->m_len, buffer);*/
675 bcopy(mtod(m, caddr_t), buffer, m->m_len);
676 buffer += m->m_len;
677 len += m->m_len;
678 }
679 if (len & 1) len++;
680
681 sc->txb_next_len = MAX(len, ETHER_MIN_LEN);
682
683 if (sc->txb_cnt > 1)
684 /*
685 * only set 'buffered' flag if doing multiple buffers
686 */
687 sc->data_buffered = 1;
688
689 if (sc->xmit_busy == 0)
690 ae_xmit(ifp);
691 /*
692 * If there is BPF support in the configuration, tap off here.
693 * The following has support for converting trailer packets
694 * back to normal.
695 */
696 #if NBPFILTER > 0
697 if (sc->bpf) {
698 u_short etype;
699 int off, datasize, resid;
700 struct ether_header *eh;
701 struct trailer_header {
702 u_short ether_type;
703 u_short ether_residual;
704 } trailer_header;
705 char ether_packet[ETHER_MAX_LEN];
706 char *ep;
707
708 ep = ether_packet;
709
710 /*
711 * We handle trailers below:
712 * Copy ether header first, then residual data,
713 * then data. Put all this in a temporary buffer
714 * 'ether_packet' and send off to bpf. Since the
715 * system has generated this packet, we assume
716 * that all of the offsets in the packet are
717 * correct; if they're not, the system will almost
718 * certainly crash in m_copydata.
719 * We make no assumptions about how the data is
720 * arranged in the mbuf chain (i.e. how much
721 * data is in each mbuf, if mbuf clusters are
722 * used, etc.), which is why we use m_copydata
723 * to get the ether header rather than assume
724 * that this is located in the first mbuf.
725 */
726 /* copy ether header */
727 m_copydata(m0, 0, sizeof(struct ether_header), ep);
728 eh = (struct ether_header *) ep;
729 ep += sizeof(struct ether_header);
730 etype = ntohs(eh->ether_type);
731 if (etype >= ETHERTYPE_TRAIL &&
732 etype < ETHERTYPE_TRAIL+ETHERTYPE_NTRAILER) {
733 datasize = ((etype - ETHERTYPE_TRAIL) << 9);
734 off = datasize + sizeof(struct ether_header);
735
736 /* copy trailer_header into a data structure */
737 m_copydata(m0, off, sizeof(struct trailer_header),
738 &trailer_header.ether_type);
739
740 /* copy residual data */
741 m_copydata(m0, off+sizeof(struct trailer_header),
742 resid = ntohs(trailer_header.ether_residual) -
743 sizeof(struct trailer_header), ep);
744 ep += resid;
745
746 /* copy data */
747 m_copydata(m0, sizeof(struct ether_header),
748 datasize, ep);
749 ep += datasize;
750
751 /* restore original ether packet type */
752 eh->ether_type = trailer_header.ether_type;
753
754 bpf_tap(sc->bpf, ether_packet, ep - ether_packet);
755 } else
756 bpf_mtap(sc->bpf, m0);
757 }
758 #endif
759
760 m_freem(m0);
761
762 /*
763 * If we are doing double-buffering, a buffer might be free to
764 * fill with another packet, so loop back to the top.
765 */
766 if (sc->txb_cnt > 1)
767 goto outloop;
768 else {
769 ifp->if_flags |= IFF_OACTIVE;
770 return;
771 }
772 }
773
774 /*
775 * Ethernet interface receiver interrupt.
776 */
777 static inline void
778 ae_rint(unit)
779 int unit;
780 {
781 register struct ae_softc *sc = &ae_softc[unit];
782 u_char boundry, current;
783 u_short len;
784 struct ae_ring *packet_ptr;
785
786 /*
787 * Set NIC to page 1 registers to get 'current' pointer
788 */
789 NIC_PUT(sc, AE_P0_CR, AE_CR_PAGE_1|AE_CR_RD2|AE_CR_STA);
790
791 /*
792 * 'sc->next_packet' is the logical beginning of the ring-buffer - i.e.
793 * it points to where new data has been buffered. The 'CURR'
794 * (current) register points to the logical end of the ring-buffer
795 * - i.e. it points to where additional new data will be added.
796 * We loop here until the logical beginning equals the logical
797 * end (or in other words, until the ring-buffer is empty).
798 */
799 while (sc->next_packet != NIC_GET(sc, AE_P1_CURR)) {
800
801 /* get pointer to this buffer header structure */
802 packet_ptr = (struct ae_ring *)(sc->smem_ring +
803 (sc->next_packet - sc->rec_page_start) * AE_PAGE_SIZE);
804
805 /*
806 * The byte count includes the FCS - Frame Check Sequence (a
807 * 32 bit CRC).
808 */
809 len = packet_ptr->count[0] | (packet_ptr->count[1] << 8);
810 if ((len >= ETHER_MIN_LEN) && (len <= ETHER_MAX_LEN)) {
811 /*
812 * Go get packet. len - 4 removes CRC from length.
813 * (packet_ptr + 1) points to data just after the packet ring
814 * header (+4 bytes)
815 */
816 ae_get_packet(sc, (caddr_t)(packet_ptr + 1), len - 4);
817 ++sc->arpcom.ac_if.if_ipackets;
818 } else {
819 /*
820 * Really BAD...probably indicates that the ring pointers
821 * are corrupted. Also seen on early rev chips under
822 * high load - the byte order of the length gets switched.
823 */
824 log(LOG_ERR,
825 "ae%d: shared memory corrupt - invalid packet length %d\n",
826 unit, len);
827 ae_reset(unit);
828 return;
829 }
830
831 /*
832 * Update next packet pointer
833 */
834 sc->next_packet = packet_ptr->next_packet;
835
836 /*
837 * Update NIC boundry pointer - being careful to keep it
838 * one buffer behind. (as recommended by NS databook)
839 */
840 boundry = sc->next_packet - 1;
841 if (boundry < sc->rec_page_start)
842 boundry = sc->rec_page_stop - 1;
843
844 /*
845 * Set NIC to page 0 registers to update boundry register
846 */
847 NIC_PUT(sc, AE_P0_CR, AE_CR_RD2|AE_CR_STA);
848
849 NIC_PUT(sc, AE_P0_BNRY, boundry);
850
851 /*
852 * Set NIC to page 1 registers before looping to top (prepare to
853 * get 'CURR' current pointer)
854 */
855 NIC_PUT(sc, AE_P0_CR, AE_CR_PAGE_1|AE_CR_RD2|AE_CR_STA);
856 }
857 }
858
859 /*
860 * Ethernet interface interrupt processor
861 */
862 int
863 aeintr(unit)
864 int unit;
865 {
866 struct ae_softc *sc = &ae_softc[unit];
867 u_char isr;
868
869 /*
870 * Set NIC to page 0 registers
871 */
872 NIC_PUT(sc, AE_P0_CR, AE_CR_RD2|AE_CR_STA);
873
874 /*
875 * loop until there are no more new interrupts
876 */
877 while (isr = NIC_GET(sc, AE_P0_ISR)) {
878
879 /*
880 * reset all the bits that we are 'acknowledging'
881 * by writing a '1' to each bit position that was set
882 * (writing a '1' *clears* the bit)
883 */
884 NIC_PUT(sc, AE_P0_ISR, isr);
885
886 /*
887 * Handle transmitter interrupts. Handle these first
888 * because the receiver will reset the board under
889 * some conditions.
890 */
891 if (isr & (AE_ISR_PTX|AE_ISR_TXE)) {
892 u_char collisions = NIC_GET(sc, AE_P0_NCR);
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 if (isr & AE_ISR_TXE) {
904
905 /*
906 * Excessive collisions (16)
907 */
908 if ((NIC_GET(sc, AE_P0_TSR) & AE_TSR_ABT)
909 && (collisions == 0)) {
910 /*
911 * When collisions total 16, the
912 * P0_NCR will indicate 0, and the
913 * TSR_ABT is set.
914 */
915 collisions = 16;
916 }
917
918 /*
919 * update output errors counter
920 */
921 ++sc->arpcom.ac_if.if_oerrors;
922 } else {
923 /*
924 * Update total number of successfully
925 * transmitted packets.
926 */
927 ++sc->arpcom.ac_if.if_opackets;
928 }
929
930 /*
931 * reset tx busy and output active flags
932 */
933 sc->xmit_busy = 0;
934 sc->arpcom.ac_if.if_flags &= ~IFF_OACTIVE;
935
936 /*
937 * clear watchdog timer
938 */
939 sc->arpcom.ac_if.if_timer = 0;
940
941 /*
942 * Add in total number of collisions on last
943 * transmission.
944 */
945 sc->arpcom.ac_if.if_collisions += collisions;
946
947 /*
948 * If data is ready to transmit, start it transmitting,
949 * otherwise defer until after handling receiver
950 */
951 if (sc->data_buffered)
952 ae_xmit(&sc->arpcom.ac_if);
953 }
954
955 /*
956 * Handle receiver interrupts
957 */
958 if (isr & (AE_ISR_PRX|AE_ISR_RXE|AE_ISR_OVW)) {
959 /*
960 * Overwrite warning. In order to make sure that a lockup
961 * of the local DMA hasn't occurred, we reset and
962 * re-init the NIC. The NSC manual suggests only a
963 * partial reset/re-init is necessary - but some
964 * chips seem to want more. The DMA lockup has been
965 * seen only with early rev chips - Methinks this
966 * bug was fixed in later revs. -DG
967 */
968 if (isr & AE_ISR_OVW) {
969 ++sc->arpcom.ac_if.if_ierrors;
970 log(LOG_WARNING,
971 "ae%d: warning - receiver ring buffer overrun\n",
972 unit);
973 /*
974 * Stop/reset/re-init NIC
975 */
976 ae_reset(unit);
977 } else {
978
979 /*
980 * Receiver Error. One or more of: CRC error, frame
981 * alignment error FIFO overrun, or missed packet.
982 */
983 if (isr & AE_ISR_RXE) {
984 ++sc->arpcom.ac_if.if_ierrors;
985 #ifdef AE_DEBUG
986 printf("ae%d: receive error %x\n", unit,
987 NIC_GET(sc, AE_P0_RSR));
988 #endif
989 }
990
991 /*
992 * Go get the packet(s)
993 * XXX - Doing this on an error is dubious
994 * because there shouldn't be any data to
995 * get (we've configured the interface to
996 * not accept packets with errors).
997 */
998 ae_rint (unit);
999 }
1000 }
1001
1002 /*
1003 * If it looks like the transmitter can take more data,
1004 * attempt to start output on the interface.
1005 * This is done after handling the receiver to
1006 * give the receiver priority.
1007 */
1008 if ((sc->arpcom.ac_if.if_flags & IFF_OACTIVE) == 0)
1009 ae_start(&sc->arpcom.ac_if);
1010
1011 /*
1012 * return NIC CR to standard state: page 0, remote DMA complete,
1013 * start (toggling the TXP bit off, even if was just set
1014 * in the transmit routine, is *okay* - it is 'edge'
1015 * triggered from low to high)
1016 */
1017 NIC_PUT(sc, AE_P0_CR, AE_CR_RD2|AE_CR_STA);
1018
1019 /*
1020 * If the Network Talley Counters overflow, read them to
1021 * reset them. It appears that old 8390's won't
1022 * clear the ISR flag otherwise - resulting in an
1023 * infinite loop.
1024 */
1025 if (isr & AE_ISR_CNT) {
1026 (void) NIC_GET(sc, AE_P0_CNTR0);
1027 (void) NIC_GET(sc, AE_P0_CNTR1);
1028 (void) NIC_GET(sc, AE_P0_CNTR2);
1029 }
1030 }
1031 }
1032
1033 /*
1034 * Process an ioctl request. This code needs some work - it looks
1035 * pretty ugly.
1036 */
1037 int
1038 ae_ioctl(ifp, command, data)
1039 register struct ifnet *ifp;
1040 int command;
1041 caddr_t data;
1042 {
1043 register struct ifaddr *ifa = (struct ifaddr *)data;
1044 struct ae_softc *sc = &ae_softc[ifp->if_unit];
1045 struct ifreq *ifr = (struct ifreq *)data;
1046 int s, error = 0;
1047
1048 s = splnet();
1049
1050 switch (command) {
1051
1052 case SIOCSIFADDR:
1053 ifp->if_flags |= IFF_UP;
1054
1055 switch (ifa->ifa_addr->sa_family) {
1056 #ifdef INET
1057 case AF_INET:
1058 ae_init(ifp->if_unit); /* before arpwhohas */
1059 /*
1060 * See if another station has *our* IP address.
1061 * i.e.: There is an address conflict! If a
1062 * conflict exists, a message is sent to the
1063 * console.
1064 */
1065 ((struct arpcom *)ifp)->ac_ipaddr =
1066 IA_SIN(ifa)->sin_addr;
1067 arpwhohas((struct arpcom *)ifp, &IA_SIN(ifa)->sin_addr);
1068 break;
1069 #endif
1070 #ifdef NS
1071 /*
1072 * XXX - This code is probably wrong
1073 */
1074 case AF_NS:
1075 {
1076 register struct ns_addr *ina = &(IA_SNS(ifa)->sns_addr);
1077
1078 if (ns_nullhost(*ina))
1079 ina->x_host =
1080 *(union ns_host *)(sc->arpcom.ac_enaddr);
1081 else {
1082 /*
1083 *
1084 */
1085 bcopy((caddr_t)ina->x_host.c_host,
1086 (caddr_t)sc->arpcom.ac_enaddr,
1087 sizeof(sc->arpcom.ac_enaddr));
1088 }
1089 /*
1090 * Set new address
1091 */
1092 ae_init(ifp->if_unit);
1093 break;
1094 }
1095 #endif
1096 default:
1097 ae_init(ifp->if_unit);
1098 break;
1099 }
1100 break;
1101
1102 case SIOCSIFFLAGS:
1103 /*
1104 * If interface is marked down and it is running, then stop it
1105 */
1106 if (((ifp->if_flags & IFF_UP) == 0) &&
1107 (ifp->if_flags & IFF_RUNNING)) {
1108 ae_stop(ifp->if_unit);
1109 ifp->if_flags &= ~IFF_RUNNING;
1110 } else {
1111 /*
1112 * If interface is marked up and it is stopped, then start it
1113 */
1114 if ((ifp->if_flags & IFF_UP) &&
1115 ((ifp->if_flags & IFF_RUNNING) == 0))
1116 ae_init(ifp->if_unit);
1117 }
1118 #if NBPFILTER > 0
1119 if (ifp->if_flags & IFF_PROMISC) {
1120 /*
1121 * Set promiscuous mode on interface.
1122 * XXX - for multicasts to work, we would need to
1123 * write 1's in all bits of multicast
1124 * hashing array. For now we assume that
1125 * this was done in ae_init().
1126 */
1127 NIC_PUT(sc, AE_P0_RCR,
1128 AE_RCR_PRO|AE_RCR_AM|AE_RCR_AB);
1129 } else {
1130 /*
1131 * XXX - for multicasts to work, we would need to
1132 * rewrite the multicast hashing array with the
1133 * proper hash (would have been destroyed above).
1134 */
1135 NIC_PUT(sc, AE_P0_RCR, AE_RCR_AB);
1136 }
1137 #endif
1138 break;
1139
1140 default:
1141 error = EINVAL;
1142 }
1143 (void) splx(s);
1144 return (error);
1145 }
1146
1147 /*
1148 * Macro to calculate a new address within shared memory when given an offset
1149 * from an address, taking into account ring-wrap.
1150 */
1151 #define ringoffset(sc, start, off, type) \
1152 ((type)( ((caddr_t)(start)+(off) >= (sc)->smem_end) ? \
1153 (((caddr_t)(start)+(off))) - (sc)->smem_end \
1154 + (sc)->smem_ring: \
1155 ((caddr_t)(start)+(off)) ))
1156
1157 /*
1158 * Retreive packet from shared memory and send to the next level up via
1159 * ether_input(). If there is a BPF listener, give a copy to BPF, too.
1160 */
1161 ae_get_packet(sc, buf, len)
1162 struct ae_softc *sc;
1163 char *buf;
1164 u_short len;
1165 {
1166 struct ether_header *eh;
1167 struct mbuf *m, *head, *ae_ring_to_mbuf();
1168 u_short off;
1169 int resid;
1170 u_short etype;
1171 struct trailer_header {
1172 u_short trail_type;
1173 u_short trail_residual;
1174 } trailer_header;
1175
1176 /* Allocate a header mbuf */
1177 MGETHDR(m, M_DONTWAIT, MT_DATA);
1178 if (m == 0)
1179 goto bad;
1180 m->m_pkthdr.rcvif = &sc->arpcom.ac_if;
1181 m->m_pkthdr.len = len;
1182 m->m_len = 0;
1183 head = m;
1184
1185 eh = (struct ether_header *)buf;
1186
1187 /* The following sillines is to make NFS happy */
1188 #define EROUND ((sizeof(struct ether_header) + 3) & ~3)
1189 #define EOFF (EROUND - sizeof(struct ether_header))
1190
1191 /*
1192 * The following assumes there is room for
1193 * the ether header in the header mbuf
1194 */
1195 head->m_data += EOFF;
1196 bcopy(buf, mtod(head, caddr_t), sizeof(struct ether_header));
1197 buf += sizeof(struct ether_header);
1198 head->m_len += sizeof(struct ether_header);
1199 len -= sizeof(struct ether_header);
1200
1201 etype = ntohs((u_short)eh->ether_type);
1202
1203 /*
1204 * Deal with trailer protocol:
1205 * If trailer protocol, calculate the datasize as 'off',
1206 * which is also the offset to the trailer header.
1207 * Set resid to the amount of packet data following the
1208 * trailer header.
1209 * Finally, copy residual data into mbuf chain.
1210 */
1211 if (etype >= ETHERTYPE_TRAIL &&
1212 etype < ETHERTYPE_TRAIL+ETHERTYPE_NTRAILER) {
1213
1214 off = (etype - ETHERTYPE_TRAIL) << 9;
1215 if ((off + sizeof(struct trailer_header)) > len)
1216 goto bad; /* insanity */
1217
1218 eh->ether_type = *ringoffset(sc, buf, off, u_short *);
1219 resid = ntohs(*ringoffset(sc, buf, off+2, u_short *));
1220
1221 if ((off + resid) > len) goto bad; /* insanity */
1222
1223 resid -= sizeof(struct trailer_header);
1224 if (resid < 0) goto bad; /* insanity */
1225
1226 m = ae_ring_to_mbuf(sc, ringoffset(sc, buf, off+4, char *), head, resid);
1227 if (m == 0) goto bad;
1228
1229 len = off;
1230 head->m_pkthdr.len -= 4; /* subtract trailer header */
1231 }
1232
1233 /*
1234 * Pull packet off interface. Or if this was a trailer packet,
1235 * the data portion is appended.
1236 */
1237 m = ae_ring_to_mbuf(sc, buf, m, len);
1238 if (m == 0) goto bad;
1239
1240 #if NBPFILTER > 0
1241 /*
1242 * Check if there's a BPF listener on this interface.
1243 * If so, hand off the raw packet to bpf.
1244 */
1245 if (sc->bpf) {
1246 bpf_mtap(sc->bpf, head);
1247
1248 /*
1249 * Note that the interface cannot be in promiscuous mode if
1250 * there are no BPF listeners. And if we are in promiscuous
1251 * mode, we have to check if this packet is really ours.
1252 *
1253 * XXX This test does not support multicasts.
1254 */
1255 if ((sc->arpcom.ac_if.if_flags & IFF_PROMISC) &&
1256 bcmp(eh->ether_dhost, sc->arpcom.ac_enaddr,
1257 sizeof(eh->ether_dhost)) != 0 &&
1258 bcmp(eh->ether_dhost, etherbroadcastaddr,
1259 sizeof(eh->ether_dhost)) != 0) {
1260
1261 m_freem(head);
1262 return;
1263 }
1264 }
1265 #endif
1266
1267 /*
1268 * Fix up data start offset in mbuf to point past ether header
1269 */
1270 m_adj(head, sizeof(struct ether_header));
1271
1272 ether_input(&sc->arpcom.ac_if, eh, head);
1273 return;
1274
1275 bad: if (head)
1276 m_freem(head);
1277 return;
1278 }
1279
1280 /*
1281 * Supporting routines
1282 */
1283
1284 /*
1285 * Given a source and destination address, copy 'amount' of a packet from
1286 * the ring buffer into a linear destination buffer. Takes into account
1287 * ring-wrap.
1288 */
1289 static inline char *
1290 ae_ring_copy(sc,src,dst,amount)
1291 struct ae_softc *sc;
1292 char *src;
1293 char *dst;
1294 u_short amount;
1295 {
1296 u_short tmp_amount;
1297
1298 /* does copy wrap to lower addr in ring buffer? */
1299 if (src + amount > sc->smem_end) {
1300 tmp_amount = sc->smem_end - src;
1301 bcopy(src, dst, tmp_amount); /* copy amount up to end of smem */
1302 amount -= tmp_amount;
1303 src = sc->smem_ring;
1304 dst += tmp_amount;
1305 }
1306
1307 bcopy(src, dst, amount);
1308
1309 return(src + amount);
1310 }
1311
1312 /*
1313 * Copy data from receive buffer to end of mbuf chain
1314 * allocate additional mbufs as needed. return pointer
1315 * to last mbuf in chain.
1316 * sc = ed info (softc)
1317 * src = pointer in ed ring buffer
1318 * dst = pointer to last mbuf in mbuf chain to copy to
1319 * amount = amount of data to copy
1320 */
1321 struct mbuf *
1322 ae_ring_to_mbuf(sc,src,dst,total_len)
1323 struct ae_softc *sc;
1324 char *src;
1325 struct mbuf *dst;
1326 u_short total_len;
1327 {
1328 register struct mbuf *m = dst;
1329
1330 while (total_len) {
1331 register u_short amount = min(total_len, M_TRAILINGSPACE(m));
1332
1333 if (amount == 0) { /* no more data in this mbuf, alloc another */
1334 /*
1335 * If there is enough data for an mbuf cluster, attempt
1336 * to allocate one of those, otherwise, a regular
1337 * mbuf will do.
1338 * Note that a regular mbuf is always required, even if
1339 * we get a cluster - getting a cluster does not
1340 * allocate any mbufs, and one is needed to assign
1341 * the cluster to. The mbuf that has a cluster
1342 * extension can not be used to contain data - only
1343 * the cluster can contain data.
1344 */
1345 dst = m;
1346 MGET(m, M_DONTWAIT, MT_DATA);
1347 if (m == 0)
1348 return (0);
1349
1350 if (total_len >= MINCLSIZE)
1351 MCLGET(m, M_DONTWAIT);
1352
1353 m->m_len = 0;
1354 dst->m_next = m;
1355 amount = min(total_len, M_TRAILINGSPACE(m));
1356 }
1357
1358 src = ae_ring_copy(sc, src, mtod(m, caddr_t) + m->m_len, amount);
1359
1360 m->m_len += amount;
1361 total_len -= amount;
1362
1363 }
1364 return (m);
1365 }
1366 #endif
1367
1368